Ensure xyShape is ordered by x coordinates when multiplying by negatives

Value: [0.00001 to 0.0005]
This commit is contained in:
Sam Nolan 2022-04-25 11:44:50 -04:00
parent 4e77448f02
commit d7151907d3
3 changed files with 23 additions and 3 deletions

View File

@ -0,0 +1,16 @@
open Jest
open TestHelpers
describe("combine with discrete", () => {
makeTest(
"keep order when multiplying by negative number",
AlgebraicShapeCombination.checkOrdered(
AlgebraicShapeCombination.combineShapesContinuousDiscrete(
#Multiply,
{xs: [0., 1.], ys: [1., 1.]},
{xs: [-1.], ys: [1.]},
),
), // Multiply distribution by -1
true,
)
})

View File

@ -2,7 +2,6 @@
module.exports = {
preset: "ts-jest",
testEnvironment: "node",
bail: true,
setupFilesAfterEnv: [
"<rootdir>/../../node_modules/bisect_ppx/src/runtime/js/jest.bs.js",
],

View File

@ -215,7 +215,6 @@ let combineShapesContinuousDiscrete = (
continuousShape.ys[i] *. discreteShape.ys[j],
),
) |> ignore
()
}
Belt.Array.set(outXYShapes, j, dxyShape) |> ignore
()
@ -225,9 +224,13 @@ let combineShapesContinuousDiscrete = (
// creates a new continuous shape for each one of the discrete points, and collects them in outXYShapes.
let dxyShape: array<(float, float)> = Belt.Array.makeUninitializedUnsafe(t1n)
for i in 0 to t1n - 1 {
// If this operation would flip the x axis (such as -1 * normal(5, 2)),
// then we want to fill the shape in backwards to ensure all the points
// are still in the right order
let index = discreteShape.xs[j] > 0.0 ? i : t1n - 1 - i
Belt.Array.set(
dxyShape,
i,
index,
(
fn(continuousShape.xs[i], discreteShape.xs[j]),
continuousShape.ys[i] *. discreteShape.ys[j] /. Js.Math.abs_float(discreteShape.xs[j]),
@ -251,3 +254,5 @@ let combineShapesContinuousDiscrete = (
XYShape.T.empty,
)
}
let checkOrdered = (a: XYShape.T.t): bool => a.xs[0] < a.xs[1]