First attempt at refactoring algebraicCombination code

This commit is contained in:
Ozzie Gooen 2022-04-27 11:13:10 -04:00
parent bd5dce4829
commit d104494f02

View File

@ -247,11 +247,11 @@ module AlgebraicCombination = {
arithmeticOperation: Operation.algebraicOperation, arithmeticOperation: Operation.algebraicOperation,
t1: t, t1: t,
t2: t, t2: t,
): option<SymbolicDistTypes.analyticalSimplificationResult> => { ): SymbolicDistTypes.analyticalSimplificationResult => {
switch (t1, t2) { switch (t1, t2) {
| (DistributionTypes.Symbolic(d1), DistributionTypes.Symbolic(d2)) => | (DistributionTypes.Symbolic(d1), DistributionTypes.Symbolic(d2)) =>
Some(SymbolicDist.T.tryAnalyticalSimplification(d1, d2, arithmeticOperation)) SymbolicDist.T.tryAnalyticalSimplification(d1, d2, arithmeticOperation)
| _ => None | _ => #NoSolution
} }
} }
@ -263,20 +263,13 @@ module AlgebraicCombination = {
~t2: t, ~t2: t,
): result<t, error> => { ): result<t, error> => {
switch tryAnalyticalSimplification(arithmeticOperation, t1, t2) { switch tryAnalyticalSimplification(arithmeticOperation, t1, t2) {
| Some(#AnalyticalSolution(symbolicDist)) => Ok(Symbolic(symbolicDist)) | #AnalyticalSolution(symbolicDist) => Ok(Symbolic(symbolicDist))
| Some(#Error(e)) => Error(OperationError(e)) | #Error(e) => Error(OperationError(e))
| Some(#NoSolution) | #NoSolution =>
| None => switch chooseConvolutionOrMonteCarloDefault(arithmeticOperation, t1, t2) {
switch getInvalidOperationError(t1, t2, ~toPointSetFn, ~arithmeticOperation) { | MonteCarloStrat => runMonteCarlo(toSampleSetFn, arithmeticOperation, t1, t2)
| Some(e) => Error(e) | ConvolutionStrat(convOp) =>
| None => runConvolution(toPointSetFn, convOp, t1, t2)->E.R2.fmap(r => DistributionTypes.PointSet(r))
switch chooseConvolutionOrMonteCarloDefault(arithmeticOperation, t1, t2) {
| MonteCarloStrat => runMonteCarlo(toSampleSetFn, arithmeticOperation, t1, t2)
| ConvolutionStrat(convOp) =>
runConvolution(toPointSetFn, convOp, t1, t2)->E.R2.fmap(r => DistributionTypes.PointSet(
r,
))
}
} }
} }
} }
@ -289,27 +282,29 @@ module AlgebraicCombination = {
~arithmeticOperation: Operation.algebraicOperation, ~arithmeticOperation: Operation.algebraicOperation,
~t2: t, ~t2: t,
): result<t, error> => { ): result<t, error> => {
switch strategy { let invalidOperationError = getInvalidOperationError(
| AsDefault => runDefault(t1, ~toPointSetFn, ~toSampleSetFn, ~arithmeticOperation, ~t2) t1,
| AsSymbolic => t2,
~toPointSetFn,
~arithmeticOperation,
)
switch (invalidOperationError, strategy, arithmeticOperation) {
| (Some(e), _, _) => Error(e)
| (None, AsDefault, _) =>
runDefault(t1, ~toPointSetFn, ~toSampleSetFn, ~arithmeticOperation, ~t2)
| (None, AsMonteCarlo, _) => runMonteCarlo(toSampleSetFn, arithmeticOperation, t1, t2)
| (None, AsSymbolic, _) =>
switch tryAnalyticalSimplification(arithmeticOperation, t1, t2) { switch tryAnalyticalSimplification(arithmeticOperation, t1, t2) {
| Some(#AnalyticalSolution(symbolicDist)) => Ok(Symbolic(symbolicDist)) | #AnalyticalSolution(symbolicDist) => Ok(Symbolic(symbolicDist))
| Some(#NoSolution) => Error(RequestedStrategyInvalidError(`No analytical solution`)) | #NoSolution => Error(RequestedStrategyInvalidError(`No analytic solution for inputs`))
| None => Error(RequestedStrategyInvalidError("Inputs were not even symbolic")) | #Error(err) => Error(OperationError(err))
| Some(#Error(err)) => Error(OperationError(err))
} }
| AsConvolution => { | (None, AsConvolution, (#Divide | #Power | #Logarithm) as convOp) => {
let errString = opString => `Can't convolve on ${opString}` let errString = `Can't convolve on ${Operation.Algebraic.toString(convOp)}`
switch arithmeticOperation { Error(RequestedStrategyInvalidError(errString))
| (#Add | #Subtract | #Multiply) as convOp =>
runConvolution(toPointSetFn, convOp, t1, t2)->E.R2.fmap(r => DistributionTypes.PointSet(
r,
))
| (#Divide | #Power | #Logarithm) as op =>
op->Operation.Algebraic.toString->errString->RequestedStrategyInvalidError->Error
}
} }
| AsMonteCarlo => runMonteCarlo(toSampleSetFn, arithmeticOperation, t1, t2) | (None, AsConvolution, (#Add | #Subtract | #Multiply) as convOp) =>
runConvolution(toPointSetFn, convOp, t1, t2)->E.R2.fmap(r => DistributionTypes.PointSet(r))
} }
} }
} }