Merge pull request #972 from quantified-uncertainty/pointset-mapy

Pointset mapY
This commit is contained in:
Ozzie Gooen 2022-08-22 11:28:12 -07:00 committed by GitHub
commit b8d7737aeb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 67 additions and 8 deletions

View File

@ -23,6 +23,30 @@ let inputsTodist = (inputs: array<FunctionRegistry_Core.frValue>, makeDist) => {
expressionValue expressionValue
} }
module Internal = {
type t = PointSetDist.t
let toType = (r): result<
ReducerInterface_InternalExpressionValue.t,
Reducer_ErrorValue.errorValue,
> =>
switch r {
| Ok(r) => Ok(Wrappers.evDistribution(PointSet(r)))
| Error(err) => Error(REOperationError(err))
}
let doLambdaCall = (aLambdaValue, list, environment, reducer) =>
switch Reducer_Expression_Lambda.doLambdaCall(aLambdaValue, list, environment, reducer) {
| Ok(IEvNumber(f)) => Ok(f)
| _ => Error(Operation.SampleMapNeedsNtoNFunction)
}
let mapY = (pointSetDist: t, aLambdaValue, env, reducer) => {
let fn = r => doLambdaCall(aLambdaValue, list{IEvNumber(r)}, env, reducer)
PointSetDist.T.mapYResult(~fn, pointSetDist)->toType
}
}
let library = [ let library = [
Function.make( Function.make(
~name="fromDist", ~name="fromDist",
@ -53,6 +77,27 @@ let library = [
], ],
(), (),
), ),
Function.make(
~name="mapY",
~nameSpace,
~requiresNamespace=true,
~examples=[`PointSet.mapY(mx(normal(5,2)), {|x| x + 1})`],
~output=ReducerInterface_InternalExpressionValue.EvtDistribution,
~definitions=[
FnDefinition.make(
~name="mapY",
~inputs=[FRTypeDist, FRTypeLambda],
~run=(inputs, _, env, reducer) =>
switch inputs {
| [IEvDistribution(PointSet(dist)), IEvLambda(lambda)] =>
Internal.mapY(dist, lambda, env, reducer)->E.R2.errMap(Reducer_ErrorValue.errorToString)
| _ => Error(impossibleError)
},
(),
),
],
(),
),
Function.make( Function.make(
~name="makeContinuous", ~name="makeContinuous",
~nameSpace, ~nameSpace,

View File

@ -92,7 +92,7 @@ let library = [
GenericDist.toSampleSetDist(dist, env.sampleCount) GenericDist.toSampleSetDist(dist, env.sampleCount)
->E.R2.fmap(Wrappers.sampleSet) ->E.R2.fmap(Wrappers.sampleSet)
->E.R2.fmap(Wrappers.evDistribution) ->E.R2.fmap(Wrappers.evDistribution)
->E.R2.errMap(_ => "") ->E.R2.errMap(DistributionTypes.Error.toString)
| _ => Error(impossibleError) | _ => Error(impossibleError)
}, },
(), (),
@ -158,7 +158,7 @@ let library = [
| [IEvLambda(lambda)] => | [IEvLambda(lambda)] =>
switch Internal.fromFn(lambda, env, reducer) { switch Internal.fromFn(lambda, env, reducer) {
| Ok(r) => Ok(r->Wrappers.sampleSet->Wrappers.evDistribution) | Ok(r) => Ok(r->Wrappers.sampleSet->Wrappers.evDistribution)
| Error(_) => Error("issue") | Error(e) => Error(Operation.Error.toString(e))
} }
| _ => Error(impossibleError) | _ => Error(impossibleError)
}, },
@ -180,7 +180,7 @@ let library = [
~run=(inputs, _, env, reducer) => ~run=(inputs, _, env, reducer) =>
switch inputs { switch inputs {
| [IEvDistribution(SampleSet(dist)), IEvLambda(lambda)] => | [IEvDistribution(SampleSet(dist)), IEvLambda(lambda)] =>
Internal.map1(dist, lambda, env, reducer)->E.R2.errMap(_ => "") Internal.map1(dist, lambda, env, reducer)->E.R2.errMap(Reducer_ErrorValue.errorToString)
| _ => Error(impossibleError) | _ => Error(impossibleError)
}, },
(), (),
@ -207,7 +207,9 @@ let library = [
IEvDistribution(SampleSet(dist2)), IEvDistribution(SampleSet(dist2)),
IEvLambda(lambda), IEvLambda(lambda),
] => ] =>
Internal.map2(dist1, dist2, lambda, env, reducer)->E.R2.errMap(_ => "") Internal.map2(dist1, dist2, lambda, env, reducer)->E.R2.errMap(
Reducer_ErrorValue.errorToString,
)
| _ => Error(impossibleError) | _ => Error(impossibleError)
} }
}, },
@ -236,7 +238,9 @@ let library = [
IEvDistribution(SampleSet(dist3)), IEvDistribution(SampleSet(dist3)),
IEvLambda(lambda), IEvLambda(lambda),
] => ] =>
Internal.map3(dist1, dist2, dist3, lambda, env, reducer)->E.R2.errMap(_ => "") Internal.map3(dist1, dist2, dist3, lambda, env, reducer)->E.R2.errMap(
Reducer_ErrorValue.errorToString,
)
| _ => Error(impossibleError) | _ => Error(impossibleError)
}, },
(), (),
@ -259,9 +263,9 @@ let library = [
~run=(inputs, _, env, reducer) => ~run=(inputs, _, env, reducer) =>
switch inputs { switch inputs {
| [IEvArray(dists), IEvLambda(lambda)] => | [IEvArray(dists), IEvLambda(lambda)] =>
Internal.mapN(dists, lambda, env, reducer)->E.R2.errMap(_e => { Internal.mapN(dists, lambda, env, reducer)->E.R2.errMap(
"AHHH doesn't work" Reducer_ErrorValue.errorToString,
}) )
| _ => Error(impossibleError) | _ => Error(impossibleError)
}, },
(), (),

View File

@ -46,3 +46,13 @@ PointSet.makeDiscrete([
{ x: 3, y: 0.1 }, { x: 3, y: 0.1 },
]); ]);
``` ```
### mapY
```
PointSet.mapY: (pointSetDist, (number => number)) => pointSetDist
```
```javascript
normal(5,3) |> PointSet.fromDist |> PointSet.mapY({|x| x ^ 2}) |> normalize
```