Adding math utility functions

This commit is contained in:
Ozzie Gooen 2022-06-07 09:40:24 -07:00
parent b022ea2fae
commit ff3efeac4c
3 changed files with 115 additions and 1 deletions

View File

@ -5,6 +5,8 @@ let impossibleError = "Wrong inputs / Logically impossible"
module Wrappers = { module Wrappers = {
let symbolic = r => DistributionTypes.Symbolic(r) let symbolic = r => DistributionTypes.Symbolic(r)
let evDistribution = r => ReducerInterface_ExpressionValue.EvDistribution(r) let evDistribution = r => ReducerInterface_ExpressionValue.EvDistribution(r)
let evNumber = r => ReducerInterface_ExpressionValue.EvNumber(r)
let evArray = r => ReducerInterface_ExpressionValue.EvArray(r)
let symbolicEvDistribution = r => r->DistributionTypes.Symbolic->evDistribution let symbolicEvDistribution = r => r->DistributionTypes.Symbolic->evDistribution
} }
@ -77,6 +79,21 @@ module Prepare = {
pairs pairs
} }
} }
let oneNumber = (values: t): result<float, err> => {
switch values {
| FRValueNumber(a1) => Ok(a1)
| _ => Error(impossibleError)
}
}
module ToTypedArray = {
let numbers = (inputs: ts): result<array<float>, err> => {
let openNumbers = (elements: array<t>) =>
elements->E.A2.fmap(oneNumber)->E.A.R.firstErrorOrOpen
inputs->E.A.unsafe_get(0)->ToValueArray.Array.openA->E.R.bind(openNumbers)
}
}
} }
module Process = { module Process = {
@ -183,9 +200,25 @@ module OneArgDist = {
->E.R.bind(Process.DistOrNumberToDist.oneValueUsingSymbolicDist(~fn, ~value=_, ~env)) ->E.R.bind(Process.DistOrNumberToDist.oneValueUsingSymbolicDist(~fn, ~value=_, ~env))
->E.R2.fmap(Wrappers.evDistribution) ->E.R2.fmap(Wrappers.evDistribution)
let make = (name, fn) => { let make = (name, fn) =>
FnDefinition.make(~name, ~inputs=[FRTypeDistOrNumber], ~run=(inputs, env) => FnDefinition.make(~name, ~inputs=[FRTypeDistOrNumber], ~run=(inputs, env) =>
inputs->Prepare.ToValueTuple.oneDistOrNumber->process(~fn, ~env) inputs->Prepare.ToValueTuple.oneDistOrNumber->process(~fn, ~env)
) )
}
module ArrayNumberDist = {
let make = (name, fn) => {
FnDefinition.make(~name, ~inputs=[FRTypeArray(FRTypeNumber)], ~run=(inputs, _) =>
Prepare.ToTypedArray.numbers(inputs)->E.R2.fmap(fn)
)
} }
} }
module NumberToNumber = {
let make = (name, fn) =>
FnDefinition.make(~name, ~inputs=[FRTypeNumber], ~run=(inputs, _) => {
let num =
inputs->E.A.unsafe_get(0)->Prepare.oneNumber->E.R2.fmap(fn)->E.R2.fmap(Wrappers.evNumber)
num
})
}

View File

@ -134,4 +134,84 @@ let registry = [
~name="Bernoulli", ~name="Bernoulli",
~definitions=[OneArgDist.make("bernoulli", SymbolicDist.Bernoulli.make)], ~definitions=[OneArgDist.make("bernoulli", SymbolicDist.Bernoulli.make)],
), ),
Function.make(
~name="Floor",
~definitions=[NumberToNumber.make("floor", Js.Math.floor_float)]
),
Function.make(
~name="Ceiling",
~definitions=[NumberToNumber.make("ceil", Js.Math.ceil_float)]
),
Function.make(
~name="Absolute Value",
~definitions=[NumberToNumber.make("abs", Js.Math.abs_float)]
),
Function.make(
~name="Exponent",
~definitions=[NumberToNumber.make("exp", Js.Math.exp)]
),
Function.make(
~name="Log",
~definitions=[NumberToNumber.make("log", Js.Math.log)]
),
Function.make(
~name="Log Base 10",
~definitions=[NumberToNumber.make("log10", Js.Math.log10)]
),
Function.make(
~name="Log Base 2",
~definitions=[NumberToNumber.make("log2", Js.Math.log2)]
),
Function.make(
~name="Round",
~definitions=[NumberToNumber.make("round", Js.Math.round)]
),
Function.make(
~name="Sum",
~definitions=[ArrayNumberDist.make("sum", r => r->E.A.Floats.sum->Wrappers.evNumber)],
),
Function.make(
~name="Product",
~definitions=[ArrayNumberDist.make("product", r => r->E.A.Floats.product->Wrappers.evNumber)],
),
Function.make(
~name="Min",
~definitions=[ArrayNumberDist.make("min", r => r->E.A.Floats.min->Wrappers.evNumber)],
),
Function.make(
~name="Max",
~definitions=[ArrayNumberDist.make("max", r => r->E.A.Floats.max->Wrappers.evNumber)],
),
Function.make(
~name="Mean",
~definitions=[ArrayNumberDist.make("mean", r => r->E.A.Floats.mean->Wrappers.evNumber)],
),
Function.make(
~name="Geometric Mean",
~definitions=[ArrayNumberDist.make("geomean", r => r->E.A.Floats.geomean->Wrappers.evNumber)],
),
Function.make(
~name="Standard Deviation",
~definitions=[ArrayNumberDist.make("stdev", r => r->E.A.Floats.stdev->Wrappers.evNumber)],
),
Function.make(
~name="Variance",
~definitions=[ArrayNumberDist.make("variance", r => r->E.A.Floats.stdev->Wrappers.evNumber)],
),
Function.make(
~name="Sort",
~definitions=[
ArrayNumberDist.make("sort", r =>
r->E.A.Floats.sort->E.A2.fmap(Wrappers.evNumber)->Wrappers.evArray
),
],
),
Function.make(
~name="Reverse",
~definitions=[
ArrayNumberDist.make("reverse", r =>
r->Belt_Array.reverse->E.A2.fmap(Wrappers.evNumber)->Wrappers.evArray
),
],
),
] ]

View File

@ -720,6 +720,7 @@ module A = {
let variance = Jstat.variance let variance = Jstat.variance
let stdev = Jstat.stdev let stdev = Jstat.stdev
let sum = Jstat.sum let sum = Jstat.sum
let product = Jstat.product
let random = Js.Math.random_int let random = Js.Math.random_int
let floatCompare: (float, float) => int = compare let floatCompare: (float, float) => int = compare