feat: Worked out kinks of the integral!
This commit is contained in:
parent
fd7720b045
commit
27ca86e729
|
@ -108,8 +108,8 @@ module Internals = {
|
||||||
}
|
}
|
||||||
result
|
result
|
||||||
}
|
}
|
||||||
let xsLength = Js.Math.ceil((max -. min) /. increment)
|
let xsLength = Js.Math.floor((max -. min) /. increment) // Note that we are loosing a bit of the tail
|
||||||
let xs = Belt.Array.makeBy(xsLength + 1, i => min +. Belt_Float.fromInt(i) *. increment)
|
let xs = Belt.Array.makeBy(xsLength, i => min +. (Belt_Float.fromInt(i) +. 0.5) *. increment)
|
||||||
// makeBy goes from 0 to (n-1): <https://rescript-lang.org/docs/manual/latest/api/belt/array#makeby>
|
// makeBy goes from 0 to (n-1): <https://rescript-lang.org/docs/manual/latest/api/belt/array#makeby>
|
||||||
let ysOptions = Belt.Array.map(xs, x => applyFunctionAtFloatToFloatOption(x))
|
let ysOptions = Belt.Array.map(xs, x => applyFunctionAtFloatToFloatOption(x))
|
||||||
let okYs = E.A.R.filterOk(ysOptions)
|
let okYs = E.A.R.filterOk(ysOptions)
|
||||||
|
@ -149,20 +149,47 @@ module Internals = {
|
||||||
}
|
}
|
||||||
result
|
result
|
||||||
}
|
}
|
||||||
let xsLengthCandidate = Belt.Float.toInt(Js.Math.round(numIntervals))
|
// worked example in comments below, assuming min=0, max = 10
|
||||||
let xsLength = xsLengthCandidate > 0 ? xsLengthCandidate : 1
|
let numTotalPoints = Belt.Float.toInt(numIntervals)
|
||||||
let increment = (max -. min) /. Belt.Int.toFloat(xsLength)
|
let numInnerPoints = numTotalPoints - 2
|
||||||
let xs = Belt.Array.makeBy(xsLength + 1, i => min +. Belt_Float.fromInt(i) *. increment)
|
let numOuterPoints = 2
|
||||||
// makeBy goes from 0 to (n-1): <https://rescript-lang.org/docs/manual/latest/api/belt/array#makeby>
|
let totalWeight = (max -. min)
|
||||||
let ysOptions = Belt.Array.map(xs, x => applyFunctionAtFloatToFloatOption(x))
|
let weightForAnInnerPoint = totalWeight/.E.I.toFloat(numTotalPoints-1)
|
||||||
|
let weightForAnOuterPoint = totalWeight/.E.I.toFloat(numTotalPoints-1)/. 2.0
|
||||||
|
let innerPointIncrement = (max -. min) /. E.I.toFloat(numTotalPoints -1)
|
||||||
|
let innerXs = Belt.Array.makeBy(numInnerPoints, i => min +. Belt_Float.fromInt(i + 1) *. innerPointIncrement)
|
||||||
|
// Note that makeBy goes from 0 to (n-1): <https://rescript-lang.org/docs/manual/latest/api/belt/array#makeby>
|
||||||
|
let ysOptions = Belt.Array.map(innerXs, x => applyFunctionAtFloatToFloatOption(x))
|
||||||
let okYs = E.A.R.filterOk(ysOptions)
|
let okYs = E.A.R.filterOk(ysOptions)
|
||||||
|
|
||||||
|
// Logging
|
||||||
|
// assuming min = 0, max = 10, results below:
|
||||||
|
Js.Console.log2("numTotalPoints", numTotalPoints) // 5
|
||||||
|
Js.Console.log2("numInnerPoints", numInnerPoints) // 3
|
||||||
|
Js.Console.log2("numOuterPoints", numOuterPoints) // always 2
|
||||||
|
Js.Console.log2("totalWeight", totalWeight) // 10 - 0 = 10
|
||||||
|
Js.Console.log2("weightForAnInnerPoint", weightForAnInnerPoint) // 10/4 = 2.5
|
||||||
|
Js.Console.log2("weightForAnOuterPoint", weightForAnOuterPoint) // 10/4/2 = 1.25
|
||||||
|
Js.Console.log2("weightForAnInnerPoint * numInnerPoints + weightForAnOuterPoint * numOuterPoints", weightForAnInnerPoint *. E.I.toFloat(numInnerPoints) +. weightForAnOuterPoint *. E.I.toFloat(numOuterPoints)) // should be 10
|
||||||
|
Js.Console.log2("sum of weights == totalWeight", (weightForAnInnerPoint *. E.I.toFloat(numInnerPoints) +. weightForAnOuterPoint *. E.I.toFloat(numOuterPoints)) == totalWeight ) // true
|
||||||
|
Js.Console.log2("innerPointIncrement", innerPointIncrement) // (10-0)/4 = 2.5
|
||||||
|
Js.Console.log2("innerXs", innerXs) // 2.5, 5, 7.5
|
||||||
|
Js.Console.log2("ysOptions", ysOptions)
|
||||||
|
Js.Console.log2("okYs", okYs)
|
||||||
|
|
||||||
let result = switch E.A.length(ysOptions) == E.A.length(okYs) {
|
let result = switch E.A.length(ysOptions) == E.A.length(okYs) {
|
||||||
| true => {
|
| true => {
|
||||||
let numericIntermediate = okYs->E.A.reduce(0.0, (a, b) => a +. b)
|
let innerPointsSum = okYs->E.A.reduce(0.0, (a, b) => a +. b)
|
||||||
let numericIntermediate2 = numericIntermediate *. increment
|
let resultWithOuterPoints = switch(applyFunctionAtFloatToFloatOption(min), applyFunctionAtFloatToFloatOption(max)){
|
||||||
let resultWrapped =
|
| (Ok(yMin), Ok(yMax)) => {
|
||||||
numericIntermediate2->ReducerInterface_InternalExpressionValue.IEvNumber->Ok
|
let result = ((yMin +. yMax)*.weightForAnOuterPoint +. innerPointsSum*.weightForAnInnerPoint)
|
||||||
resultWrapped
|
let wrappedResult = result -> ReducerInterface_InternalExpressionValue.IEvNumber -> Ok
|
||||||
|
wrappedResult
|
||||||
|
}
|
||||||
|
| (Error(b), _) => Error(b)
|
||||||
|
| (_, Error(b)) => Error(b)
|
||||||
|
}
|
||||||
|
resultWithOuterPoints
|
||||||
}
|
}
|
||||||
| false => Error("Integration error in Danger.integrate")
|
| false => Error("Integration error in Danger.integrate")
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user