2022-04-01 00:24:59 +00:00
|
|
|
module ExpressionValue = ReducerInterface_ExpressionValue
|
|
|
|
type expressionValue = ReducerInterface_ExpressionValue.expressionValue
|
|
|
|
|
2022-04-29 18:41:30 +00:00
|
|
|
let defaultEnv: DistributionOperation.env = {
|
2022-04-29 18:42:34 +00:00
|
|
|
sampleCount: MagicNumbers.Environment.defaultSampleCount,
|
|
|
|
xyPointLength: MagicNumbers.Environment.defaultXYPointLength,
|
|
|
|
}
|
2022-04-29 18:41:30 +00:00
|
|
|
|
2022-04-29 18:42:34 +00:00
|
|
|
let runGenericOperation = DistributionOperation.run(~env=defaultEnv)
|
2022-04-01 00:24:59 +00:00
|
|
|
|
2022-04-01 19:41:11 +00:00
|
|
|
module Helpers = {
|
|
|
|
let arithmeticMap = r =>
|
|
|
|
switch r {
|
|
|
|
| "add" => #Add
|
2022-04-02 20:25:41 +00:00
|
|
|
| "dotAdd" => #Add
|
2022-04-01 19:41:11 +00:00
|
|
|
| "subtract" => #Subtract
|
2022-04-02 20:25:41 +00:00
|
|
|
| "dotSubtract" => #Subtract
|
2022-04-01 19:41:11 +00:00
|
|
|
| "divide" => #Divide
|
2022-04-02 20:25:41 +00:00
|
|
|
| "log" => #Logarithm
|
|
|
|
| "dotDivide" => #Divide
|
2022-04-09 16:37:26 +00:00
|
|
|
| "pow" => #Power
|
|
|
|
| "dotPow" => #Power
|
2022-04-01 19:41:11 +00:00
|
|
|
| "multiply" => #Multiply
|
2022-04-02 20:25:41 +00:00
|
|
|
| "dotMultiply" => #Multiply
|
2022-04-01 19:41:11 +00:00
|
|
|
| _ => #Multiply
|
|
|
|
}
|
2022-04-01 00:24:59 +00:00
|
|
|
|
2022-04-01 19:41:11 +00:00
|
|
|
let catchAndConvertTwoArgsToDists = (args: array<expressionValue>): option<(
|
2022-04-22 20:27:17 +00:00
|
|
|
DistributionTypes.genericDist,
|
|
|
|
DistributionTypes.genericDist,
|
2022-04-29 18:42:34 +00:00
|
|
|
)> =>
|
2022-04-01 19:41:11 +00:00
|
|
|
switch args {
|
|
|
|
| [EvDistribution(a), EvDistribution(b)] => Some((a, b))
|
|
|
|
| [EvNumber(a), EvDistribution(b)] => Some((GenericDist.fromFloat(a), b))
|
|
|
|
| [EvDistribution(a), EvNumber(b)] => Some((a, GenericDist.fromFloat(b)))
|
|
|
|
| _ => None
|
|
|
|
}
|
2022-04-01 00:24:59 +00:00
|
|
|
|
2022-04-01 19:41:11 +00:00
|
|
|
let toFloatFn = (
|
2022-04-23 18:09:06 +00:00
|
|
|
fnCall: DistributionTypes.DistributionOperation.toFloat,
|
2022-04-22 20:27:17 +00:00
|
|
|
dist: DistributionTypes.genericDist,
|
2022-04-01 19:41:11 +00:00
|
|
|
) => {
|
2022-04-22 20:27:17 +00:00
|
|
|
FromDist(DistributionTypes.DistributionOperation.ToFloat(fnCall), dist)
|
|
|
|
->runGenericOperation
|
|
|
|
->Some
|
2022-04-01 00:24:59 +00:00
|
|
|
}
|
|
|
|
|
2022-04-08 13:08:49 +00:00
|
|
|
let toStringFn = (
|
2022-04-22 20:27:17 +00:00
|
|
|
fnCall: DistributionTypes.DistributionOperation.toString,
|
|
|
|
dist: DistributionTypes.genericDist,
|
2022-04-08 13:08:49 +00:00
|
|
|
) => {
|
2022-04-22 20:27:17 +00:00
|
|
|
FromDist(DistributionTypes.DistributionOperation.ToString(fnCall), dist)
|
|
|
|
->runGenericOperation
|
|
|
|
->Some
|
2022-04-08 13:08:49 +00:00
|
|
|
}
|
|
|
|
|
2022-04-15 17:58:00 +00:00
|
|
|
let toBoolFn = (
|
2022-04-22 20:27:17 +00:00
|
|
|
fnCall: DistributionTypes.DistributionOperation.toBool,
|
|
|
|
dist: DistributionTypes.genericDist,
|
2022-04-15 17:58:00 +00:00
|
|
|
) => {
|
2022-04-22 20:27:17 +00:00
|
|
|
FromDist(DistributionTypes.DistributionOperation.ToBool(fnCall), dist)
|
|
|
|
->runGenericOperation
|
|
|
|
->Some
|
2022-04-15 17:58:00 +00:00
|
|
|
}
|
|
|
|
|
2022-04-22 20:27:17 +00:00
|
|
|
let toDistFn = (fnCall: DistributionTypes.DistributionOperation.toDist, dist) => {
|
|
|
|
FromDist(DistributionTypes.DistributionOperation.ToDist(fnCall), dist)
|
|
|
|
->runGenericOperation
|
|
|
|
->Some
|
2022-04-01 19:41:11 +00:00
|
|
|
}
|
2022-04-01 00:24:59 +00:00
|
|
|
|
2022-04-01 19:41:11 +00:00
|
|
|
let twoDiststoDistFn = (direction, arithmetic, dist1, dist2) => {
|
|
|
|
FromDist(
|
2022-04-22 20:27:17 +00:00
|
|
|
DistributionTypes.DistributionOperation.ToDistCombination(
|
2022-04-01 19:41:11 +00:00
|
|
|
direction,
|
|
|
|
arithmeticMap(arithmetic),
|
|
|
|
#Dist(dist2),
|
|
|
|
),
|
|
|
|
dist1,
|
|
|
|
)->runGenericOperation
|
2022-04-01 00:24:59 +00:00
|
|
|
}
|
2022-04-29 00:24:13 +00:00
|
|
|
|
2022-04-13 02:03:04 +00:00
|
|
|
let parseNumber = (args: expressionValue): Belt.Result.t<float, string> =>
|
2022-04-12 07:39:38 +00:00
|
|
|
switch args {
|
|
|
|
| EvNumber(x) => Ok(x)
|
|
|
|
| _ => Error("Not a number")
|
|
|
|
}
|
|
|
|
|
2022-04-13 02:03:04 +00:00
|
|
|
let parseNumberArray = (ags: array<expressionValue>): Belt.Result.t<array<float>, string> =>
|
|
|
|
E.A.fmap(parseNumber, ags) |> E.A.R.firstErrorOrOpen
|
2022-04-12 07:39:38 +00:00
|
|
|
|
2022-04-22 20:27:17 +00:00
|
|
|
let parseDist = (args: expressionValue): Belt.Result.t<DistributionTypes.genericDist, string> =>
|
2022-04-12 07:39:38 +00:00
|
|
|
switch args {
|
|
|
|
| EvDistribution(x) => Ok(x)
|
|
|
|
| EvNumber(x) => Ok(GenericDist.fromFloat(x))
|
|
|
|
| _ => Error("Not a distribution")
|
|
|
|
}
|
|
|
|
|
2022-04-13 02:03:04 +00:00
|
|
|
let parseDistributionArray = (ags: array<expressionValue>): Belt.Result.t<
|
2022-04-22 20:27:17 +00:00
|
|
|
array<DistributionTypes.genericDist>,
|
2022-04-13 02:03:04 +00:00
|
|
|
string,
|
|
|
|
> => E.A.fmap(parseDist, ags) |> E.A.R.firstErrorOrOpen
|
2022-04-12 07:39:38 +00:00
|
|
|
|
2022-04-13 02:03:04 +00:00
|
|
|
let mixtureWithGivenWeights = (
|
2022-04-22 20:27:17 +00:00
|
|
|
distributions: array<DistributionTypes.genericDist>,
|
2022-04-13 02:03:04 +00:00
|
|
|
weights: array<float>,
|
|
|
|
): DistributionOperation.outputType =>
|
|
|
|
E.A.length(distributions) == E.A.length(weights)
|
|
|
|
? Mixture(Belt.Array.zip(distributions, weights))->runGenericOperation
|
|
|
|
: GenDistError(
|
|
|
|
ArgumentError("Error, mixture call has different number of distributions and weights"),
|
|
|
|
)
|
|
|
|
|
|
|
|
let mixtureWithDefaultWeights = (
|
2022-04-22 20:27:17 +00:00
|
|
|
distributions: array<DistributionTypes.genericDist>,
|
2022-04-13 02:03:04 +00:00
|
|
|
): DistributionOperation.outputType => {
|
|
|
|
let length = E.A.length(distributions)
|
|
|
|
let weights = Belt.Array.make(length, 1.0 /. Belt.Int.toFloat(length))
|
|
|
|
mixtureWithGivenWeights(distributions, weights)
|
|
|
|
}
|
|
|
|
|
2022-05-09 19:24:46 +00:00
|
|
|
let mixture = (args: array<expressionValue>): DistributionOperation.outputType => {
|
2022-05-10 20:27:34 +00:00
|
|
|
let error = (err: string): DistributionOperation.outputType => err->DistributionTypes.ArgumentError->GenDistError
|
2022-05-09 19:19:56 +00:00
|
|
|
switch args {
|
|
|
|
| [EvArray(distributions)] =>
|
|
|
|
switch parseDistributionArray(distributions) {
|
|
|
|
| Ok(distrs) => mixtureWithDefaultWeights(distrs)
|
2022-05-09 19:24:46 +00:00
|
|
|
| Error(err) => error(err)
|
2022-05-09 19:19:56 +00:00
|
|
|
}
|
|
|
|
| [EvArray(distributions), EvArray(weights)] =>
|
|
|
|
switch (parseDistributionArray(distributions), parseNumberArray(weights)) {
|
|
|
|
| (Ok(distrs), Ok(wghts)) => mixtureWithGivenWeights(distrs, wghts)
|
2022-05-09 19:24:46 +00:00
|
|
|
| (Error(err), Ok(_)) => error(err)
|
|
|
|
| (Ok(_), Error(err)) => error(err)
|
|
|
|
| (Error(err1), Error(err2)) => error(`${err1}|${err2}`)
|
2022-05-09 19:19:56 +00:00
|
|
|
}
|
|
|
|
| _ =>
|
|
|
|
switch E.A.last(args) {
|
|
|
|
| Some(EvArray(b)) => {
|
|
|
|
let weights = parseNumberArray(b)
|
|
|
|
let distributions = parseDistributionArray(
|
|
|
|
Belt.Array.slice(args, ~offset=0, ~len=E.A.length(args) - 1),
|
|
|
|
)
|
|
|
|
switch E.R.merge(distributions, weights) {
|
|
|
|
| Ok(d, w) => mixtureWithGivenWeights(d, w)
|
2022-05-09 19:24:46 +00:00
|
|
|
| Error(err) => error(err)
|
2022-05-09 19:19:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
| Some(EvNumber(_))
|
|
|
|
| Some(EvDistribution(_)) =>
|
|
|
|
switch parseDistributionArray(args) {
|
|
|
|
| Ok(distributions) => mixtureWithDefaultWeights(distributions)
|
2022-05-09 19:24:46 +00:00
|
|
|
| Error(err) => error(err)
|
2022-04-12 07:39:38 +00:00
|
|
|
}
|
2022-05-09 19:24:46 +00:00
|
|
|
| _ => error("Last argument of mx must be array or distribution")
|
2022-04-13 02:03:04 +00:00
|
|
|
}
|
2022-04-12 07:39:38 +00:00
|
|
|
}
|
2022-05-09 19:24:46 +00:00
|
|
|
}
|
2022-04-01 19:41:11 +00:00
|
|
|
}
|
2022-04-01 00:24:59 +00:00
|
|
|
|
2022-04-01 19:41:11 +00:00
|
|
|
module SymbolicConstructors = {
|
2022-04-01 17:21:24 +00:00
|
|
|
let oneFloat = name =>
|
|
|
|
switch name {
|
|
|
|
| "exponential" => Ok(SymbolicDist.Exponential.make)
|
2022-04-02 15:01:53 +00:00
|
|
|
| _ => Error("Unreachable state")
|
2022-04-01 17:21:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let twoFloat = name =>
|
|
|
|
switch name {
|
|
|
|
| "normal" => Ok(SymbolicDist.Normal.make)
|
|
|
|
| "uniform" => Ok(SymbolicDist.Uniform.make)
|
|
|
|
| "beta" => Ok(SymbolicDist.Beta.make)
|
|
|
|
| "lognormal" => Ok(SymbolicDist.Lognormal.make)
|
2022-04-20 15:50:08 +00:00
|
|
|
| "cauchy" => Ok(SymbolicDist.Cauchy.make)
|
2022-05-04 15:53:41 +00:00
|
|
|
| "gamma" => Ok(SymbolicDist.Gamma.make)
|
2022-04-01 19:41:11 +00:00
|
|
|
| "to" => Ok(SymbolicDist.From90thPercentile.make)
|
2022-04-02 15:01:53 +00:00
|
|
|
| _ => Error("Unreachable state")
|
2022-04-01 17:21:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let threeFloat = name =>
|
|
|
|
switch name {
|
|
|
|
| "triangular" => Ok(SymbolicDist.Triangular.make)
|
2022-04-02 15:01:53 +00:00
|
|
|
| _ => Error("Unreachable state")
|
2022-04-01 17:21:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let symbolicResultToOutput = (
|
|
|
|
symbolicResult: result<SymbolicDistTypes.symbolicDist, string>,
|
2022-04-04 15:59:14 +00:00
|
|
|
): option<DistributionOperation.outputType> =>
|
2022-04-01 17:21:24 +00:00
|
|
|
switch symbolicResult {
|
|
|
|
| Ok(r) => Some(Dist(Symbolic(r)))
|
2022-04-23 18:13:38 +00:00
|
|
|
| Error(r) => Some(GenDistError(OtherError(r)))
|
2022-04-01 17:21:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-27 20:00:42 +00:00
|
|
|
let dispatchToGenericOutput = (call: ExpressionValue.functionCall, _environment): option<
|
2022-04-04 15:59:14 +00:00
|
|
|
DistributionOperation.outputType,
|
2022-04-01 00:24:59 +00:00
|
|
|
> => {
|
|
|
|
let (fnName, args) = call
|
|
|
|
switch (fnName, args) {
|
2022-05-01 13:00:56 +00:00
|
|
|
| ("exponential" as fnName, [EvNumber(f)]) =>
|
2022-04-01 19:41:11 +00:00
|
|
|
SymbolicConstructors.oneFloat(fnName)
|
2022-05-01 13:00:56 +00:00
|
|
|
->E.R.bind(r => r(f))
|
2022-04-01 19:41:11 +00:00
|
|
|
->SymbolicConstructors.symbolicResultToOutput
|
2022-05-01 13:00:56 +00:00
|
|
|
| ("delta", [EvNumber(f)]) =>
|
|
|
|
SymbolicDist.Float.makeSafe(f)->SymbolicConstructors.symbolicResultToOutput
|
2022-04-01 19:41:11 +00:00
|
|
|
| (
|
2022-05-04 15:53:41 +00:00
|
|
|
("normal" | "uniform" | "beta" | "lognormal" | "cauchy" | "gamma" | "to") as fnName,
|
2022-04-01 19:41:11 +00:00
|
|
|
[EvNumber(f1), EvNumber(f2)],
|
|
|
|
) =>
|
|
|
|
SymbolicConstructors.twoFloat(fnName)
|
2022-04-01 17:21:24 +00:00
|
|
|
->E.R.bind(r => r(f1, f2))
|
2022-04-01 19:41:11 +00:00
|
|
|
->SymbolicConstructors.symbolicResultToOutput
|
2022-04-01 17:21:24 +00:00
|
|
|
| ("triangular" as fnName, [EvNumber(f1), EvNumber(f2), EvNumber(f3)]) =>
|
2022-04-01 19:41:11 +00:00
|
|
|
SymbolicConstructors.threeFloat(fnName)
|
2022-04-01 17:21:24 +00:00
|
|
|
->E.R.bind(r => r(f1, f2, f3))
|
2022-04-01 19:41:11 +00:00
|
|
|
->SymbolicConstructors.symbolicResultToOutput
|
|
|
|
| ("sample", [EvDistribution(dist)]) => Helpers.toFloatFn(#Sample, dist)
|
|
|
|
| ("mean", [EvDistribution(dist)]) => Helpers.toFloatFn(#Mean, dist)
|
2022-04-29 01:31:15 +00:00
|
|
|
| ("integralSum", [EvDistribution(dist)]) => Helpers.toFloatFn(#IntegralSum, dist)
|
2022-04-08 13:08:49 +00:00
|
|
|
| ("toString", [EvDistribution(dist)]) => Helpers.toStringFn(ToString, dist)
|
|
|
|
| ("toSparkline", [EvDistribution(dist)]) => Helpers.toStringFn(ToSparkline(20), dist)
|
2022-04-11 14:51:15 +00:00
|
|
|
| ("toSparkline", [EvDistribution(dist), EvNumber(n)]) =>
|
|
|
|
Helpers.toStringFn(ToSparkline(Belt.Float.toInt(n)), dist)
|
2022-04-02 20:25:41 +00:00
|
|
|
| ("exp", [EvDistribution(a)]) =>
|
|
|
|
// https://mathjs.org/docs/reference/functions/exp.html
|
2022-04-27 17:59:33 +00:00
|
|
|
Helpers.twoDiststoDistFn(
|
|
|
|
Algebraic(AsDefault),
|
|
|
|
"pow",
|
|
|
|
GenericDist.fromFloat(MagicNumbers.Math.e),
|
|
|
|
a,
|
|
|
|
)->Some
|
2022-04-01 19:41:11 +00:00
|
|
|
| ("normalize", [EvDistribution(dist)]) => Helpers.toDistFn(Normalize, dist)
|
2022-05-04 16:21:30 +00:00
|
|
|
| ("klDivergence", [EvDistribution(a), EvDistribution(b)]) =>
|
|
|
|
Some(runGenericOperation(FromDist(ToScore(KLDivergence(b)), a)))
|
2022-04-15 17:58:00 +00:00
|
|
|
| ("isNormalized", [EvDistribution(dist)]) => Helpers.toBoolFn(IsNormalized, dist)
|
2022-04-01 19:41:11 +00:00
|
|
|
| ("toPointSet", [EvDistribution(dist)]) => Helpers.toDistFn(ToPointSet, dist)
|
2022-04-29 01:14:03 +00:00
|
|
|
| ("scaleLog", [EvDistribution(dist)]) =>
|
|
|
|
Helpers.toDistFn(Scale(#Logarithm, MagicNumbers.Math.e), dist)
|
|
|
|
| ("scaleLog10", [EvDistribution(dist)]) => Helpers.toDistFn(Scale(#Logarithm, 10.0), dist)
|
|
|
|
| ("scaleLog", [EvDistribution(dist), EvNumber(float)]) =>
|
|
|
|
Helpers.toDistFn(Scale(#Logarithm, float), dist)
|
2022-05-04 17:02:58 +00:00
|
|
|
| ("scaleLogWithThreshold", [EvDistribution(dist), EvNumber(base), EvNumber(eps)]) =>
|
|
|
|
Helpers.toDistFn(Scale(#LogarithmWithThreshold(eps), base), dist)
|
2022-04-29 01:14:03 +00:00
|
|
|
| ("scalePow", [EvDistribution(dist), EvNumber(float)]) =>
|
|
|
|
Helpers.toDistFn(Scale(#Power, float), dist)
|
|
|
|
| ("scaleExp", [EvDistribution(dist)]) =>
|
|
|
|
Helpers.toDistFn(Scale(#Power, MagicNumbers.Math.e), dist)
|
2022-04-01 19:41:11 +00:00
|
|
|
| ("cdf", [EvDistribution(dist), EvNumber(float)]) => Helpers.toFloatFn(#Cdf(float), dist)
|
|
|
|
| ("pdf", [EvDistribution(dist), EvNumber(float)]) => Helpers.toFloatFn(#Pdf(float), dist)
|
|
|
|
| ("inv", [EvDistribution(dist), EvNumber(float)]) => Helpers.toFloatFn(#Inv(float), dist)
|
2022-04-01 00:28:48 +00:00
|
|
|
| ("toSampleSet", [EvDistribution(dist), EvNumber(float)]) =>
|
2022-04-01 19:41:11 +00:00
|
|
|
Helpers.toDistFn(ToSampleSet(Belt.Int.fromFloat(float)), dist)
|
2022-04-15 17:58:00 +00:00
|
|
|
| ("toSampleSet", [EvDistribution(dist)]) =>
|
2022-04-27 17:59:33 +00:00
|
|
|
Helpers.toDistFn(ToSampleSet(MagicNumbers.Environment.defaultSampleCount), dist)
|
2022-04-30 01:41:09 +00:00
|
|
|
| ("fromSamples", [EvArray(inputArray)]) => {
|
|
|
|
let _wrapInputErrors = x => SampleSetDist.NonNumericInput(x)
|
|
|
|
let parsedArray = Helpers.parseNumberArray(inputArray)->E.R2.errMap(_wrapInputErrors)
|
|
|
|
switch parsedArray {
|
|
|
|
| Ok(array) => runGenericOperation(FromSamples(array))
|
|
|
|
| Error(e) => GenDistError(SampleSetError(e))
|
|
|
|
}->Some
|
|
|
|
}
|
2022-04-15 18:10:17 +00:00
|
|
|
| ("inspect", [EvDistribution(dist)]) => Helpers.toDistFn(Inspect, dist)
|
2022-04-01 17:21:24 +00:00
|
|
|
| ("truncateLeft", [EvDistribution(dist), EvNumber(float)]) =>
|
2022-04-01 19:41:11 +00:00
|
|
|
Helpers.toDistFn(Truncate(Some(float), None), dist)
|
2022-04-01 00:28:48 +00:00
|
|
|
| ("truncateRight", [EvDistribution(dist), EvNumber(float)]) =>
|
2022-04-01 19:41:11 +00:00
|
|
|
Helpers.toDistFn(Truncate(None, Some(float)), dist)
|
2022-04-01 00:28:48 +00:00
|
|
|
| ("truncate", [EvDistribution(dist), EvNumber(float1), EvNumber(float2)]) =>
|
2022-04-01 19:41:11 +00:00
|
|
|
Helpers.toDistFn(Truncate(Some(float1), Some(float2)), dist)
|
2022-04-13 02:03:04 +00:00
|
|
|
| ("mx" | "mixture", args) => Helpers.mixture(args)->Some
|
2022-04-02 20:25:41 +00:00
|
|
|
| ("log", [EvDistribution(a)]) =>
|
2022-04-27 17:59:33 +00:00
|
|
|
Helpers.twoDiststoDistFn(
|
|
|
|
Algebraic(AsDefault),
|
|
|
|
"log",
|
|
|
|
a,
|
|
|
|
GenericDist.fromFloat(MagicNumbers.Math.e),
|
|
|
|
)->Some
|
2022-04-02 20:25:41 +00:00
|
|
|
| ("log10", [EvDistribution(a)]) =>
|
2022-04-26 20:06:51 +00:00
|
|
|
Helpers.twoDiststoDistFn(Algebraic(AsDefault), "log", a, GenericDist.fromFloat(10.0))->Some
|
2022-04-02 21:06:57 +00:00
|
|
|
| ("unaryMinus", [EvDistribution(a)]) =>
|
2022-04-26 20:06:51 +00:00
|
|
|
Helpers.twoDiststoDistFn(Algebraic(AsDefault), "multiply", a, GenericDist.fromFloat(-1.0))->Some
|
2022-04-21 22:09:06 +00:00
|
|
|
| (("add" | "multiply" | "subtract" | "divide" | "pow" | "log") as arithmetic, [_, _] as args) =>
|
2022-04-01 19:41:11 +00:00
|
|
|
Helpers.catchAndConvertTwoArgsToDists(args)->E.O2.fmap(((fst, snd)) =>
|
2022-04-26 20:06:51 +00:00
|
|
|
Helpers.twoDiststoDistFn(Algebraic(AsDefault), arithmetic, fst, snd)
|
2022-04-01 00:24:59 +00:00
|
|
|
)
|
|
|
|
| (
|
2022-04-02 20:25:41 +00:00
|
|
|
("dotAdd"
|
|
|
|
| "dotMultiply"
|
|
|
|
| "dotSubtract"
|
|
|
|
| "dotDivide"
|
2022-04-23 18:09:06 +00:00
|
|
|
| "dotPow") as arithmetic,
|
2022-04-21 22:09:06 +00:00
|
|
|
[_, _] as args,
|
2022-04-01 00:24:59 +00:00
|
|
|
) =>
|
2022-04-01 19:41:11 +00:00
|
|
|
Helpers.catchAndConvertTwoArgsToDists(args)->E.O2.fmap(((fst, snd)) =>
|
|
|
|
Helpers.twoDiststoDistFn(Pointwise, arithmetic, fst, snd)
|
2022-04-01 00:24:59 +00:00
|
|
|
)
|
2022-04-02 20:25:41 +00:00
|
|
|
| ("dotExp", [EvDistribution(a)]) =>
|
2022-04-27 17:59:33 +00:00
|
|
|
Helpers.twoDiststoDistFn(
|
|
|
|
Pointwise,
|
|
|
|
"dotPow",
|
|
|
|
GenericDist.fromFloat(MagicNumbers.Math.e),
|
|
|
|
a,
|
|
|
|
)->Some
|
2022-04-01 00:24:59 +00:00
|
|
|
| _ => None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-04 15:59:14 +00:00
|
|
|
let genericOutputToReducerValue = (o: DistributionOperation.outputType): result<
|
2022-04-01 19:41:11 +00:00
|
|
|
expressionValue,
|
|
|
|
Reducer_ErrorValue.errorValue,
|
|
|
|
> =>
|
|
|
|
switch o {
|
|
|
|
| Dist(d) => Ok(ReducerInterface_ExpressionValue.EvDistribution(d))
|
|
|
|
| Float(d) => Ok(EvNumber(d))
|
|
|
|
| String(d) => Ok(EvString(d))
|
2022-04-15 17:58:00 +00:00
|
|
|
| Bool(d) => Ok(EvBool(d))
|
2022-04-22 20:27:17 +00:00
|
|
|
| GenDistError(err) => Error(REDistributionError(err))
|
2022-04-01 19:41:11 +00:00
|
|
|
}
|
|
|
|
|
2022-04-27 20:00:42 +00:00
|
|
|
let dispatch = (call, environment) => {
|
|
|
|
dispatchToGenericOutput(call, environment)->E.O2.fmap(genericOutputToReducerValue)
|
2022-04-01 00:24:59 +00:00
|
|
|
}
|