Added SampleSet.min and Sampleset.max

This commit is contained in:
Ozzie Gooen 2022-08-31 21:43:36 -07:00
parent 5e9bf521fa
commit 255541a679
2 changed files with 67 additions and 1 deletions

View File

@ -140,3 +140,9 @@ let truncate = (t, ~leftCutoff: option<float>, ~rightCutoff: option<float>) => {
let withTruncatedRight = t => rightCutoff |> E.O.dimap(left => truncateRight(t, left), _ => Ok(t)) let withTruncatedRight = t => rightCutoff |> E.O.dimap(left => truncateRight(t, left), _ => Ok(t))
t->withTruncatedLeft |> E.R2.bind(withTruncatedRight) t->withTruncatedLeft |> E.R2.bind(withTruncatedRight)
} }
let minOfTwo = (t1: t, t2: t) => map2(~fn=(a, b) => Ok(Js.Math.min_float(a, b)), ~t1, ~t2)
let maxOfTwo = (t1: t, t2: t) => map2(~fn=(a, b) => Ok(Js.Math.max_float(a, b)), ~t1, ~t2)
let minOfFloat = (t: t, f: float) => samplesMap(~fn=a => Ok(Js.Math.min_float(a, f)), t)
let maxOfFloat = (t: t, f: float) => samplesMap(~fn=a => Ok(Js.Math.max_float(a, f)), t)

View File

@ -75,7 +75,7 @@ module Internal = {
} }
} }
let library = [ let library1 = [
Function.make( Function.make(
~name="fromDist", ~name="fromDist",
~nameSpace, ~nameSpace,
@ -274,3 +274,63 @@ let library = [
(), (),
), ),
] ]
module Comparison = {
let template = (name, inputs, run) => {
FnDefinition.make(
~name,
~inputs,
~run=(inputs, _, _, _) => {
run(inputs)
},
(),
)
}
let wrapper = r =>
r
->E.R2.fmap(r => r->Wrappers.sampleSet->Wrappers.evDistribution)
->E.R2.errMap(SampleSetDist.Error.toString)
let mkBig = (name, withDist, withFloat) =>
Function.make(
~name,
~nameSpace,
~requiresNamespace=false,
~examples=[
`SampleSet.${name}(SampleSet.fromDist(normal(5,2)), SampleSet.fromDist(normal(6,2)))`,
`SampleSet.${name}(SampleSet.fromDist(normal(5,2)), 3.0)`,
`SampleSet.${name}(4.0, SampleSet.fromDist(normal(6,2)))`,
],
~output=ReducerInterface_InternalExpressionValue.EvtDistribution,
~definitions=[
template(name, [FRTypeDist, FRTypeDist], inputs => {
switch inputs {
| [IEvDistribution(SampleSet(dist1)), IEvDistribution(SampleSet(dist2))] =>
withDist(dist1, dist2)->wrapper
| _ => Error(impossibleError)
}
}),
template(name, [FRTypeDist, FRTypeNumber], inputs => {
switch inputs {
| [IEvDistribution(SampleSet(dist)), IEvNumber(f)] => withFloat(dist, f)->wrapper
| _ => Error(impossibleError)
}
}),
template(name, [FRTypeNumber, FRTypeDist], inputs => {
switch inputs {
| [IEvNumber(f), IEvDistribution(SampleSet(dist))] => withFloat(dist, f)->wrapper
| _ => Error(impossibleError)
}
}),
],
(),
)
let library = [
mkBig("min", SampleSetDist.minOfTwo, SampleSetDist.minOfFloat),
mkBig("max", SampleSetDist.maxOfTwo, SampleSetDist.maxOfFloat),
]
}
let library = E.A.append(library1, Comparison.library)