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