Formatted AlgebraicShapeCombination and added |> ignore statements

This commit is contained in:
Ozzie Gooen 2020-07-17 11:29:51 +01:00
parent b3ccabae73
commit 1dfea41ab8

View File

@ -80,7 +80,6 @@ let toDiscretePointMassesFromTriangulars =
{n: n - 2, masses, means, variances};
} else {
for (i in 1 to n - 2) {
// area of triangle = width * height / 2
let _ =
Belt.Array.set(
@ -115,7 +114,11 @@ let toDiscretePointMassesFromTriangulars =
};
let combineShapesContinuousContinuous =
(op: ExpressionTypes.algebraicOperation, s1: DistTypes.xyShape, s2: DistTypes.xyShape)
(
op: ExpressionTypes.algebraicOperation,
s1: DistTypes.xyShape,
s2: DistTypes.xyShape,
)
: DistTypes.xyShape => {
let t1n = s1 |> XYShape.T.length;
let t2n = s2 |> XYShape.T.length;
@ -123,7 +126,8 @@ let combineShapesContinuousContinuous =
// if we add the two distributions, we should probably use normal filters.
// if we multiply the two distributions, we should probably use lognormal filters.
let t1m = toDiscretePointMassesFromTriangulars(s1);
let t2m = switch (op) {
let t2m =
switch (op) {
| `Divide => toDiscretePointMassesFromTriangulars(~inverse=true, s2)
| _ => toDiscretePointMassesFromTriangulars(~inverse=false, s2)
};
@ -190,27 +194,30 @@ let combineShapesContinuousContinuous =
// we now want to create a set of target points. For now, let's just evenly distribute 200 points between
// between the outputMinX and outputMaxX
let nOut = 300;
let outputXs: array(float) = E.A.Floats.range(outputMinX^, outputMaxX^, nOut);
let outputXs: array(float) =
E.A.Floats.range(outputMinX^, outputMaxX^, nOut);
let outputYs: array(float) = Belt.Array.make(nOut, 0.0);
// now, for each of the outputYs, accumulate from a Gaussian kernel over each input point.
for (j in 0 to E.A.length(masses) - 1) { // go through all of the result points
let _ = if (variances[j] > 0. && masses[j] > 0.) {
for (i in 0 to E.A.length(outputXs) - 1) { // go through all of the target points
for (j in 0 to E.A.length(masses) - 1) {
// go through all of the result points
if (variances[j] > 0. && masses[j] > 0.) {
for (i in 0 to E.A.length(outputXs) - 1) {
// go through all of the target points
let dx = outputXs[i] -. means[j];
let contribution = masses[j] *. exp(-. (dx ** 2.) /. (2. *. variances[j])) /. (sqrt(2. *. 3.14159276 *. variances[j]));
let _ = Belt.Array.set(outputYs, i, outputYs[i] +. contribution);
();
let contribution =
masses[j]
*. exp(-. (dx ** 2.) /. (2. *. variances[j]))
/. sqrt(2. *. 3.14159276 *. variances[j]);
Belt.Array.set(outputYs, i, outputYs[i] +. contribution) |> ignore;
};
();
};
();
};
{xs: outputXs, ys: outputYs};
};
let toDiscretePointMassesFromDiscrete = (s: DistTypes.xyShape): pointMassesWithMoments => {
let n = s |> XYShape.T.length;
let toDiscretePointMassesFromDiscrete =
(s: DistTypes.xyShape): pointMassesWithMoments => {
let {xs, ys}: XYShape.T.t = s;
let n = E.A.length(xs);
@ -219,36 +226,21 @@ let toDiscretePointMassesFromDiscrete = (s: DistTypes.xyShape): pointMassesWithM
let variances: array(float) = Belt.Array.makeUninitializedUnsafe(n);
for (i in 0 to n - 1) {
let _ =
Belt.Array.set(
masses,
i,
ys[i]
);
let _ =
Belt.Array.set(
means,
i,
xs[i]
);
let _ =
Belt.Array.set(
variances,
i,
0.0
);
();
Belt.Array.set(masses, i, ys[i]) |> ignore;
Belt.Array.set(means, i, xs[i]) |> ignore;
Belt.Array.set(variances, i, 0.0) |> ignore;
};
{n, masses, means, variances};
};
let combineShapesContinuousDiscrete =
(op: ExpressionTypes.algebraicOperation, s1: DistTypes.xyShape, s2: DistTypes.xyShape)
(
op: ExpressionTypes.algebraicOperation,
s1: DistTypes.xyShape,
s2: DistTypes.xyShape,
)
: array(DistTypes.xyShape) => {
let t1n = s1 |> XYShape.T.length;
let t2n = s2 |> XYShape.T.length;
@ -260,7 +252,7 @@ let combineShapesContinuousDiscrete =
switch (op) {
| `Add
| `Subtract => {
| `Subtract =>
for (j in 0 to t2n - 1) {
// for each one of the discrete points
// create a new distribution, as long as the original continuous one
@ -268,40 +260,33 @@ let combineShapesContinuousDiscrete =
Belt.Array.makeUninitializedUnsafe(t1n);
for (i in 0 to t1n - 1) {
let _ =
Belt.Array.set(
dxyShape,
i,
(fn(s1.xs[i], s2.xs[j]), s1.ys[i] *. s2.ys[j]),
);
();
)
|> ignore;
};
let _ = Belt.Array.set(outXYShapes, j, dxyShape);
();
}
Belt.Array.set(outXYShapes, j, dxyShape) |> ignore;
}
| `Multiply
| `Divide => {
| `Divide =>
for (j in 0 to t2n - 1) {
// for each one of the discrete points
// create a new distribution, as long as the original continuous one
let dxyShape: array((float, float)) =
Belt.Array.makeUninitializedUnsafe(t1n);
for (i in 0 to t1n - 1) {
let _ =
Belt.Array.set(
dxyShape,
i,
(fn(s1.xs[i], s2.xs[j]), s1.ys[i] *. s2.ys[j] /. s2.xs[j]),
);
();
)
|> ignore;
};
let _ = Belt.Array.set(outXYShapes, j, dxyShape);
();
}
Belt.Array.set(outXYShapes, j, dxyShape) |> ignore;
}
};
outXYShapes
|> E.A.fmap(XYShape.T.fromZippedArray);
outXYShapes |> E.A.fmap(XYShape.T.fromZippedArray);
};