Changes in response to CR

This commit is contained in:
Ozzie Gooen 2022-04-02 11:01:53 -04:00
parent e7c2a7db01
commit 688cf0b19e
2 changed files with 39 additions and 31 deletions

View File

@ -1,28 +1,28 @@
open Jest open Jest
open Reducer_TestHelpers open Reducer_TestHelpers
let makeTest = (str, result) => test(str, () => expectEvalToBe(str, result)) let testEval = (str, result) => test(str, () => expectEvalToBe(str, result))
describe("eval", () => { describe("eval", () => {
Only.describe("expressions", () => { Only.describe("expressions", () => {
makeTest("normal(5,2)", "Ok(Normal(5,2))") testEval("normal(5,2)", "Ok(Normal(5,2))")
makeTest("5 to 2", "Error(TODO: Low value must be less than high value.)") testEval("5 to 2", "Error(TODO: Low value must be less than high value.)")
makeTest("to(2,5)", "Ok(Lognormal(1.1512925464970227,0.278507821238345))") testEval("to(2,5)", "Ok(Lognormal(1.1512925464970227,0.278507821238345))")
makeTest("to(-2,2)", "Ok(Normal(0,1.215913388057542))") testEval("to(-2,2)", "Ok(Normal(0,1.215913388057542))")
makeTest("lognormal(5,2)", "Ok(Lognormal(5,2))") testEval("lognormal(5,2)", "Ok(Lognormal(5,2))")
makeTest("mean(normal(5,2))", "Ok(5)") testEval("mean(normal(5,2))", "Ok(5)")
makeTest("mean(lognormal(1,2))", "Ok(20.085536923187668)") testEval("mean(lognormal(1,2))", "Ok(20.085536923187668)")
makeTest("normalize(normal(5,2))", "Ok(Normal(5,2))") testEval("normalize(normal(5,2))", "Ok(Normal(5,2))")
makeTest("toPointSet(normal(5,2))", "Ok(Point Set Distribution)") testEval("toPointSet(normal(5,2))", "Ok(Point Set Distribution)")
makeTest("toSampleSet(normal(5,2), 100)", "Ok(Sample Set Distribution)") testEval("toSampleSet(normal(5,2), 100)", "Ok(Sample Set Distribution)")
makeTest("add(normal(5,2), normal(10,2))", "Ok(Normal(15,2.8284271247461903))") testEval("add(normal(5,2), normal(10,2))", "Ok(Normal(15,2.8284271247461903))")
makeTest("add(normal(5,2), lognormal(10,2))", "Ok(Sample Set Distribution)") testEval("add(normal(5,2), lognormal(10,2))", "Ok(Sample Set Distribution)")
makeTest("dotAdd(normal(5,2), lognormal(10,2))", "Ok(Point Set Distribution)") testEval("dotAdd(normal(5,2), lognormal(10,2))", "Ok(Point Set Distribution)")
makeTest("dotAdd(normal(5,2), 3)", "Ok(Point Set Distribution)") testEval("dotAdd(normal(5,2), 3)", "Ok(Point Set Distribution)")
makeTest("add(normal(5,2), 3)", "Ok(Point Set Distribution)") testEval("add(normal(5,2), 3)", "Ok(Point Set Distribution)")
makeTest("add(3, normal(5,2))", "Ok(Point Set Distribution)") testEval("add(3, normal(5,2))", "Ok(Point Set Distribution)")
makeTest("3+normal(5,2)", "Ok(Point Set Distribution)") testEval("3+normal(5,2)", "Ok(Point Set Distribution)")
makeTest("add(3, 3)", "Ok(6)") testEval("add(3, 3)", "Ok(6)")
makeTest("truncateLeft(normal(5,2), 3)", "Ok(Point Set Distribution)") testEval("truncateLeft(normal(5,2), 3)", "Ok(Point Set Distribution)")
}) })
}) })

View File

@ -12,17 +12,17 @@ module Helpers = {
let arithmeticMap = r => let arithmeticMap = r =>
switch r { switch r {
| "add" => #Add | "add" => #Add
| "dotAdd" => #Add | "pointwiseAdd" => #Add
| "subtract" => #Subtract | "subtract" => #Subtract
| "dotSubtract" => #Subtract | "pointwiseSubtract" => #Subtract
| "divide" => #Divide | "divide" => #Divide
| "logarithm" => #Logarithm | "logarithm" => #Logarithm
| "dotDivide" => #Divide | "pointwiseDivide" => #Divide
| "exponentiate" => #Exponentiate | "exponentiate" => #Exponentiate
| "dotExponentiate" => #Exponentiate | "pointwiseExponentiate" => #Exponentiate
| "multiply" => #Multiply | "multiply" => #Multiply
| "dotMultiply" => #Multiply | "pointwiseMultiply" => #Multiply
| "dotLogarithm" => #Logarithm | "pointwiseLogarithm" => #Logarithm
| _ => #Multiply | _ => #Multiply
} }
@ -65,7 +65,7 @@ module SymbolicConstructors = {
let oneFloat = name => let oneFloat = name =>
switch name { switch name {
| "exponential" => Ok(SymbolicDist.Exponential.make) | "exponential" => Ok(SymbolicDist.Exponential.make)
| _ => Error("impossible path") | _ => Error("Unreachable state")
} }
let twoFloat = name => let twoFloat = name =>
@ -75,13 +75,13 @@ module SymbolicConstructors = {
| "beta" => Ok(SymbolicDist.Beta.make) | "beta" => Ok(SymbolicDist.Beta.make)
| "lognormal" => Ok(SymbolicDist.Lognormal.make) | "lognormal" => Ok(SymbolicDist.Lognormal.make)
| "to" => Ok(SymbolicDist.From90thPercentile.make) | "to" => Ok(SymbolicDist.From90thPercentile.make)
| _ => Error("impossible path") | _ => Error("Unreachable state")
} }
let threeFloat = name => let threeFloat = name =>
switch name { switch name {
| "triangular" => Ok(SymbolicDist.Triangular.make) | "triangular" => Ok(SymbolicDist.Triangular.make)
| _ => Error("impossible path") | _ => Error("Unreachable state")
} }
let symbolicResultToOutput = ( let symbolicResultToOutput = (
@ -128,12 +128,20 @@ let dispatchToGenericOutput = (call: ExpressionValue.functionCall): option<
Helpers.toDistFn(Truncate(None, Some(float)), dist) Helpers.toDistFn(Truncate(None, Some(float)), dist)
| ("truncate", [EvDistribution(dist), EvNumber(float1), EvNumber(float2)]) => | ("truncate", [EvDistribution(dist), EvNumber(float1), EvNumber(float2)]) =>
Helpers.toDistFn(Truncate(Some(float1), Some(float2)), dist) Helpers.toDistFn(Truncate(Some(float1), Some(float2)), dist)
| (("add" | "multiply" | "subtract" | "divide" | "exponentiate" | "log") as arithmetic, [a, b] as args) => | (
("add" | "multiply" | "subtract" | "divide" | "exponentiate" | "log") as arithmetic,
[a, b] as args,
) =>
Helpers.catchAndConvertTwoArgsToDists(args)->E.O2.fmap(((fst, snd)) => Helpers.catchAndConvertTwoArgsToDists(args)->E.O2.fmap(((fst, snd)) =>
Helpers.twoDiststoDistFn(Algebraic, arithmetic, fst, snd) Helpers.twoDiststoDistFn(Algebraic, arithmetic, fst, snd)
) )
| ( | (
("dotAdd" | "dotMultiply" | "dotSubtract" | "dotDivide" | "dotExponentiate" | "dotLogarithm") as arithmetic, ("pointwiseAdd"
| "pointwiseMultiply"
| "pointwiseSubtract"
| "pointwiseDivide"
| "pointwiseExponentiate"
| "pointwiseLogarithm") as arithmetic,
[a, b] as args, [a, b] as args,
) => ) =>
Helpers.catchAndConvertTwoArgsToDists(args)->E.O2.fmap(((fst, snd)) => Helpers.catchAndConvertTwoArgsToDists(args)->E.O2.fmap(((fst, snd)) =>