Rename invalidOperationError

This commit is contained in:
Sam Nolan 2022-04-23 14:35:49 -04:00
parent 4544268429
commit df4b734a49
8 changed files with 24 additions and 23 deletions

View File

@ -11,7 +11,7 @@ type error =
| DistributionVerticalShiftIsInvalid | DistributionVerticalShiftIsInvalid
| TooFewSamples | TooFewSamples
| ArgumentError(string) | ArgumentError(string)
| OperationError(Operation.Error.invalidOperationError) | OperationError(Operation.Error.t)
| PointSetConversionError(SampleSetDist.pointsetConversionError) | PointSetConversionError(SampleSetDist.pointsetConversionError)
| SparklineError(PointSetTypes.sparklineError) // This type of error is for when we find a sparkline of a discrete distribution. This should probably at some point be actually implemented | SparklineError(PointSetTypes.sparklineError) // This type of error is for when we find a sparkline of a discrete distribution. This should probably at some point be actually implemented
| OtherError(string) | OtherError(string)
@ -30,7 +30,7 @@ module Error = {
| DistributionVerticalShiftIsInvalid => "Distribution Vertical Shift is Invalid" | DistributionVerticalShiftIsInvalid => "Distribution Vertical Shift is Invalid"
| ArgumentError(s) => `Argument Error ${s}` | ArgumentError(s) => `Argument Error ${s}`
| TooFewSamples => "Too Few Samples" | TooFewSamples => "Too Few Samples"
| OperationError(err) => Operation.Error.invalidOperationErrorToString(err) | OperationError(err) => Operation.Error.toString(err)
| PointSetConversionError(err) => SampleSetDist.pointsetConversionErrorToString(err) | PointSetConversionError(err) => SampleSetDist.pointsetConversionErrorToString(err)
| SparklineError(err) => PointSetTypes.sparklineErrorToString(err) | SparklineError(err) => PointSetTypes.sparklineErrorToString(err)
| OtherError(s) => s | OtherError(s) => s

View File

@ -151,7 +151,7 @@ module AlgebraicCombination = {
arithmeticOperation: Operation.algebraicOperation, arithmeticOperation: Operation.algebraicOperation,
t1: t, t1: t,
t2: t, t2: t,
): option<result<SymbolicDistTypes.symbolicDist, Operation.Error.invalidOperationError>> => ): option<result<SymbolicDistTypes.symbolicDist, Operation.Error.t>> =>
switch (arithmeticOperation, t1, t2) { switch (arithmeticOperation, t1, t2) {
| (arithmeticOperation, Symbolic(d1), Symbolic(d2)) => | (arithmeticOperation, Symbolic(d1), Symbolic(d2)) =>
switch SymbolicDist.T.tryAnalyticalSimplification(d1, d2, arithmeticOperation) { switch SymbolicDist.T.tryAnalyticalSimplification(d1, d2, arithmeticOperation) {

View File

@ -88,7 +88,7 @@ let stepwiseToLinear = (t: t): t =>
let combinePointwise = ( let combinePointwise = (
~integralSumCachesFn=(_, _) => None, ~integralSumCachesFn=(_, _) => None,
~distributionType: PointSetTypes.distributionType=#PDF, ~distributionType: PointSetTypes.distributionType=#PDF,
fn: (float, float) => result<float, Operation.Error.invalidOperationError>, fn: (float, float) => result<float, Operation.Error.t>,
t1: PointSetTypes.continuousShape, t1: PointSetTypes.continuousShape,
t2: PointSetTypes.continuousShape, t2: PointSetTypes.continuousShape,
): result<PointSetTypes.continuousShape, 'e> => { ): result<PointSetTypes.continuousShape, 'e> => {

View File

@ -60,10 +60,10 @@ let combinePointwise = (
PointSetTypes.continuousShape, PointSetTypes.continuousShape,
PointSetTypes.continuousShape, PointSetTypes.continuousShape,
) => option<PointSetTypes.continuousShape>=(_, _) => None, ) => option<PointSetTypes.continuousShape>=(_, _) => None,
fn: (float, float) => result<float, Operation.Error.invalidOperationError>, fn: (float, float) => result<float, Operation.Error.t>,
t1: t, t1: t,
t2: t, t2: t,
): result<PointSetTypes.pointSetDist, Operation.Error.invalidOperationError> => ): result<PointSetTypes.pointSetDist, Operation.Error.t> =>
switch (t1, t2) { switch (t1, t2) {
| (Continuous(m1), Continuous(m2)) => | (Continuous(m1), Continuous(m2)) =>
Continuous.combinePointwise( Continuous.combinePointwise(

View File

@ -83,11 +83,10 @@ let sampleN = (t: t, n) => {
} }
//TODO: Figure out what to do if distributions are different lengths. ``zip`` is kind of inelegant for this. //TODO: Figure out what to do if distributions are different lengths. ``zip`` is kind of inelegant for this.
let map2 = ( let map2 = (~fn: (float, float) => result<float, Operation.Error.t>, ~t1: t, ~t2: t): result<
~fn: (float, float) => result<float, Operation.Error.invalidOperationError>, t,
~t1: t, Operation.Error.t,
~t2: t, > => {
): result<t, Operation.Error.invalidOperationError> => {
let samples = Belt.Array.zip(get(t1), get(t2))->E.A2.fmap(((a, b)) => fn(a, b)) let samples = Belt.Array.zip(get(t1), get(t2))->E.A2.fmap(((a, b)) => fn(a, b))
// This assertion should never be reached. In order for it to be reached, one // This assertion should never be reached. In order for it to be reached, one

View File

@ -45,6 +45,6 @@ type symbolicDist = [
type analyticalSimplificationResult = [ type analyticalSimplificationResult = [
| #AnalyticalSolution(symbolicDist) | #AnalyticalSolution(symbolicDist)
| #Error(Operation.Error.invalidOperationError) | #Error(Operation.Error.t)
| #NoSolution | #NoSolution
] ]

View File

@ -37,35 +37,37 @@ module Convolution = {
} }
} }
type operationError =
| DivisionByZeroError
| ComplexNumberError
@genType @genType
module Error = { module Error = {
@genType @genType
type invalidOperationError = type t = operationError
| DivisionByZeroError
| ComplexNumberError
let invalidOperationErrorToString = (err: invalidOperationError): string => let toString = (err: t): string =>
switch err { switch err {
| DivisionByZeroError => "Cannot divide by zero" | DivisionByZeroError => "Cannot divide by zero"
| ComplexNumberError => "Operation returned complex result" | ComplexNumberError => "Operation returned complex result"
} }
} }
let power = (a: float, b: float): result<float, Error.invalidOperationError> => let power = (a: float, b: float): result<float, Error.t> =>
if a >= 0.0 { if a >= 0.0 {
Ok(a ** b) Ok(a ** b)
} else { } else {
Error(ComplexNumberError) Error(ComplexNumberError)
} }
let divide = (a: float, b: float): result<float, Error.invalidOperationError> => let divide = (a: float, b: float): result<float, Error.t> =>
if b != 0.0 { if b != 0.0 {
Ok(a /. b) Ok(a /. b)
} else { } else {
Error(DivisionByZeroError) Error(DivisionByZeroError)
} }
let logarithm = (a: float, b: float): result<float, Error.invalidOperationError> => let logarithm = (a: float, b: float): result<float, Error.t> =>
if b == 1. { if b == 1. {
Error(DivisionByZeroError) Error(DivisionByZeroError)
} else if b == 0. { } else if b == 0. {
@ -80,7 +82,7 @@ let logarithm = (a: float, b: float): result<float, Error.invalidOperationError>
module Algebraic = { module Algebraic = {
@genType @genType
type t = algebraicOperation type t = algebraicOperation
let toFn: (t, float, float) => result<float, Error.invalidOperationError> = (x, a, b) => let toFn: (t, float, float) => result<float, Error.t> = (x, a, b) =>
switch x { switch x {
| #Add => Ok(a +. b) | #Add => Ok(a +. b)
| #Subtract => Ok(a -. b) | #Subtract => Ok(a -. b)
@ -131,7 +133,7 @@ module DistToFloat = {
// Note that different logarithms don't really do anything. // Note that different logarithms don't really do anything.
module Scale = { module Scale = {
type t = scaleOperation type t = scaleOperation
let toFn = (x: t, a: float, b: float): result<float, Error.invalidOperationError> => let toFn = (x: t, a: float, b: float): result<float, Error.t> =>
switch x { switch x {
| #Multiply => Ok(a *. b) | #Multiply => Ok(a *. b)
| #Divide => divide(a, b) | #Divide => divide(a, b)

View File

@ -234,11 +234,11 @@ module Zipped = {
module PointwiseCombination = { module PointwiseCombination = {
// t1Interpolator and t2Interpolator are functions from XYShape.XtoY, e.g. linearBetweenPointsExtrapolateFlat. // t1Interpolator and t2Interpolator are functions from XYShape.XtoY, e.g. linearBetweenPointsExtrapolateFlat.
let combine: ( let combine: (
(float, float) => result<float, Operation.Error.invalidOperationError>, (float, float) => result<float, Operation.Error.t>,
interpolator, interpolator,
T.t, T.t,
T.t, T.t,
) => result<T.t, Operation.Error.invalidOperationError> = %raw(` ) => result<T.t, Operation.Error.t> = %raw(`
// This function combines two xyShapes by looping through both of them simultaneously. // This function combines two xyShapes by looping through both of them simultaneously.
// It always moves on to the next smallest x, whether that's in the first or second input's xs, // It always moves on to the next smallest x, whether that's in the first or second input's xs,
// and interpolates the value on the other side, thus accumulating xs and ys. // and interpolates the value on the other side, thus accumulating xs and ys.