Sped up integrateWithTriangles a lot

This commit is contained in:
Ozzie Gooen 2020-03-18 22:52:40 +00:00
parent 37c1814a60
commit 01da434fc3
2 changed files with 26 additions and 11 deletions

View File

@ -50,4 +50,14 @@ describe("XYShapes", () => {
[|1.0, 3.0, 6.0, 10.0|], [|1.0, 3.0, 6.0, 10.0|],
) )
}); });
describe("integrateWithTriangles", () => {
makeTest(
"integrates correctly",
XYShape.Range.integrateWithTriangles(shape1),
Some({
xs: [|1., 4., 8.|],
ys: [|0.0, 0.9000000000000001, 3.3000000000000007|],
}),
)
});
}); });

View File

@ -207,6 +207,7 @@ module T = {
array |> Belt.Array.unzip |> fromArray; array |> Belt.Array.unzip |> fromArray;
}; };
// TODO: I'd bet this is pretty slow
let intersperce = (t1: t, t2: t) => { let intersperce = (t1: t, t2: t) => {
let items: ref(array((float, float))) = ref([||]); let items: ref(array((float, float))) = ref([||]);
let t1 = zip(t1); let t1 = zip(t1);
@ -315,17 +316,21 @@ module Range = {
|> E.R.toOption |> E.R.toOption
|> E.O.fmap(r => r |> Belt.Array.map(_, r => (nextX(r), fn(r)))); |> E.O.fmap(r => r |> Belt.Array.map(_, r => (nextX(r), fn(r))));
let integrateWithTriangles = z => { let integrateWithTriangles = ({xs, ys}) => {
let rangeItems = mapYsBasedOnRanges(rangeAreaAssumingTriangles, z); let length = E.A.length(xs);
( let cumulativeY = Belt.Array.make(length, 0.0);
switch (rangeItems, z |> T.first) { let _ = Belt.Array.set(cumulativeY, 0, 0.0);
| (Some(r), Some((firstX, _))) => for (x in 0 to E.A.length(xs) - 2) {
Some(Belt.Array.concat([|(firstX, 0.0)|], r)) let area =
| _ => None rangeAreaAssumingTriangles((
} (xs[x], ys[x]),
) (xs[x + 1], ys[x + 1]),
|> E.O.fmap(toT) ));
|> E.O.fmap(T.accumulateYs); let cumulative = area +. cumulativeY[x];
let _ = Belt.Array.set(cumulativeY, x + 1, cumulative);
();
};
Some({xs, ys: cumulativeY});
}; };
let derivative = mapYsBasedOnRanges(delta_y_over_delta_x); let derivative = mapYsBasedOnRanges(delta_y_over_delta_x);