First part of refactor

This commit is contained in:
Ozzie Gooen 2020-07-23 11:17:39 +01:00
parent e86d959c1b
commit bfd9ca5533

View File

@ -239,56 +239,61 @@ module MathAdtToDistDst = {
let toOkPointwise = r => Ok(`PointwiseCombination(r)); let toOkPointwise = r => Ok(`PointwiseCombination(r));
let toOkTruncate = r => Ok(`Truncate(r)); let toOkTruncate = r => Ok(`Truncate(r));
let toOkFloatFromDist = r => Ok(`FloatFromDist(r)); let toOkFloatFromDist = r => Ok(`FloatFromDist(r));
switch (name, args) { E.A.R.firstErrorOrOpen(args)
| ("add", [|Ok(l), Ok(r)|]) => toOkAlgebraic((`Add, l, r)) |> E.R.bind(_, args => {
| ("add", _) => Error("Addition needs two operands") switch (name, args) {
| ("subtract", [|Ok(l), Ok(r)|]) => toOkAlgebraic((`Subtract, l, r)) | ("add", [|l, r|]) => toOkAlgebraic((`Add, l, r))
| ("subtract", _) => Error("Subtraction needs two operands") | ("add", _) => Error("Addition needs two operands")
| ("multiply", [|Ok(l), Ok(r)|]) => toOkAlgebraic((`Multiply, l, r)) | ("subtract", [|l, r|]) => toOkAlgebraic((`Subtract, l, r))
| ("multiply", _) => Error("Multiplication needs two operands") | ("subtract", _) => Error("Subtraction needs two operands")
| ("dotMultiply", [|Ok(l), Ok(r)|]) => toOkPointwise((`Multiply, l, r)) | ("multiply", [|l, r|]) => toOkAlgebraic((`Multiply, l, r))
| ("dotMultiply", _) => | ("multiply", _) => Error("Multiplication needs two operands")
Error("Dotwise multiplication needs two operands") | ("dotMultiply", [|l, r|]) => toOkPointwise((`Multiply, l, r))
| ("rightLogShift", [|Ok(l), Ok(r)|]) => toOkPointwise((`Add, l, r)) | ("dotMultiply", _) =>
| ("rightLogShift", _) => Error("Dotwise addition needs two operands") Error("Dotwise multiplication needs two operands")
| ("divide", [|Ok(l), Ok(r)|]) => toOkAlgebraic((`Divide, l, r)) | ("rightLogShift", [|l, r|]) => toOkPointwise((`Add, l, r))
| ("divide", _) => Error("Division needs two operands") | ("rightLogShift", _) =>
| ("pow", _) => Error("Exponentiation is not yet supported.") Error("Dotwise addition needs two operands")
| ("leftTruncate", [|Ok(d), Ok(`SymbolicDist(`Float(lc)))|]) => | ("divide", [|l, r|]) => toOkAlgebraic((`Divide, l, r))
toOkTruncate((Some(lc), None, d)) | ("divide", _) => Error("Division needs two operands")
| ("leftTruncate", _) => | ("pow", _) => Error("Exponentiation is not yet supported.")
Error("leftTruncate needs two arguments: the expression and the cutoff") | ("leftTruncate", [|d, `SymbolicDist(`Float(lc))|]) =>
| ("rightTruncate", [|Ok(d), Ok(`SymbolicDist(`Float(rc)))|]) => toOkTruncate((Some(lc), None, d))
toOkTruncate((None, Some(rc), d)) | ("leftTruncate", _) =>
| ("rightTruncate", _) => Error(
Error( "leftTruncate needs two arguments: the expression and the cutoff",
"rightTruncate needs two arguments: the expression and the cutoff", )
) | ("rightTruncate", [|d, `SymbolicDist(`Float(rc))|]) =>
| ( toOkTruncate((None, Some(rc), d))
"truncate", | ("rightTruncate", _) =>
[| Error(
Ok(d), "rightTruncate needs two arguments: the expression and the cutoff",
Ok(`SymbolicDist(`Float(lc))), )
Ok(`SymbolicDist(`Float(rc))), | (
|], "truncate",
) => [|d, `SymbolicDist(`Float(lc)), `SymbolicDist(`Float(rc))|],
toOkTruncate((Some(lc), Some(rc), d)) ) =>
| ("truncate", _) => toOkTruncate((Some(lc), Some(rc), d))
Error("truncate needs three arguments: the expression and both cutoffs") | ("truncate", _) =>
| ("pdf", [|Ok(d), Ok(`SymbolicDist(`Float(v)))|]) => Error(
toOkFloatFromDist((`Pdf(v), d)) "truncate needs three arguments: the expression and both cutoffs",
| ("cdf", [|Ok(d), Ok(`SymbolicDist(`Float(v)))|]) => )
toOkFloatFromDist((`Cdf(v), d)) | ("pdf", [|d, `SymbolicDist(`Float(v))|]) =>
| ("inv", [|Ok(d), Ok(`SymbolicDist(`Float(v)))|]) => toOkFloatFromDist((`Pdf(v), d))
toOkFloatFromDist((`Inv(v), d)) | ("cdf", [|d, `SymbolicDist(`Float(v))|]) =>
| ("mean", [|Ok(d)|]) => toOkFloatFromDist((`Mean, d)) toOkFloatFromDist((`Cdf(v), d))
| ("sample", [|Ok(d)|]) => toOkFloatFromDist((`Sample, d)) | ("inv", [|d, `SymbolicDist(`Float(v))|]) =>
| _ => Error("This type not currently supported") toOkFloatFromDist((`Inv(v), d))
}; | ("mean", [|d|]) => toOkFloatFromDist((`Mean, d))
| ("sample", [|d|]) => toOkFloatFromDist((`Sample, d))
| _ => Error("This type not currently supported")
}
});
}; };
let functionParser = (nodeParser, name, args) => { let functionParser = (nodeParser, name, args) => {
let parseArgs = () => args |> E.A.fmap(nodeParser); let parseArgs = () => args |> E.A.fmap(nodeParser);
Js.log2("Parseargs", parseArgs);
switch (name) { switch (name) {
| "normal" => normal(args) | "normal" => normal(args)
| "lognormal" => lognormal(args) | "lognormal" => lognormal(args)
@ -379,6 +384,7 @@ let fromString = str => {
Inside of this function, MathAdtToDistDst is called whenever a distribution function is encountered. Inside of this function, MathAdtToDistDst is called whenever a distribution function is encountered.
*/ */
let mathJsToJson = str |> pointwiseToRightLogShift |> Mathjs.parseMath; let mathJsToJson = str |> pointwiseToRightLogShift |> Mathjs.parseMath;
Js.log(mathJsToJson);
let mathJsParse = let mathJsParse =
E.R.bind(mathJsToJson, r => { E.R.bind(mathJsToJson, r => {
switch (MathJsonToMathJsAdt.run(r)) { switch (MathJsonToMathJsAdt.run(r)) {
@ -387,6 +393,7 @@ let fromString = str => {
} }
}); });
Js.log(mathJsParse);
let value = E.R.bind(mathJsParse, MathAdtToDistDst.run); let value = E.R.bind(mathJsParse, MathAdtToDistDst.run);
value; value;
}; };