From f9b04a1ca688de893993877d2452659ac94016de Mon Sep 17 00:00:00 2001 From: Roman Galochkin Date: Tue, 25 Feb 2020 11:29:28 +0300 Subject: [PATCH] FindY --- src/utility/lib/CDFunctor.re | 40 +++++++++++++++++++---- src/utility/lib/continuousDistribution.js | 2 ++ 2 files changed, 35 insertions(+), 7 deletions(-) diff --git a/src/utility/lib/CDFunctor.re b/src/utility/lib/CDFunctor.re index c9e0a19e..588fa8e4 100644 --- a/src/utility/lib/CDFunctor.re +++ b/src/utility/lib/CDFunctor.re @@ -14,20 +14,46 @@ let order = (shape: DistTypes.xyShape): DistTypes.xyShape => { }; module Make = (Config: Config) => { - let validateHasLength = (): bool => Array.length(Config.shape.xs) > 0; - let validateSize = (): bool => - Array.length(Config.shape.xs) == Array.length(Config.shape.ys); + let xs = Config.shape.xs; + let ys = Config.shape.ys; + let get = Array.get; + + let validateHasLength = (): bool => Array.length(xs) > 0; + let validateSize = (): bool => Array.length(xs) == Array.length(ys); if (!validateHasLength()) { raise(ShapeWrong("You need at least one element.")); }; if (!validateSize()) { raise(ShapeWrong("Arrays of \"xs\" and \"ys\" have different sizes.")); }; - if (!Belt.SortArray.isSorted(Config.shape.xs, (a, b) => a > b ? 1 : (-1))) { + if (!Belt.SortArray.isSorted(xs, (a, b) => a > b ? 1 : (-1))) { raise(ShapeWrong("Arrays of \"xs\" and \"ys\" have different sizes.")); }; - let minX = () => Config.shape.xs |> Array.get(_, 0); - let maxX = () => - Config.shape.xs |> Array.get(_, Array.length(Config.shape.xs) - 1); + let minX = () => xs |> get(_, 0); + let maxX = () => xs |> get(_, Array.length(xs) - 1); + let minY = () => ys |> get(_, 0); + let maxY = () => ys |> get(_, Array.length(ys) - 1); + let findY = x => { + let firstHigherIndex = Belt.Array.getIndexBy(xs, e => e > x); + switch (firstHigherIndex) { + | None => maxY() + | Some(1) => minY() + | Some(firstHigherIndex) => + let lowerOrEqualIndex = + firstHigherIndex - 1 < 0 ? 0 : firstHigherIndex - 1; + let needsInterpolation = get(xs, lowerOrEqualIndex) != x; + if (needsInterpolation) { + Functions.interpolate( + get(xs, lowerOrEqualIndex), + get(xs, firstHigherIndex), + get(ys, lowerOrEqualIndex), + get(ys, firstHigherIndex), + x, + ); + } else { + ys[lowerOrEqualIndex]; + }; + }; + }; 1; }; diff --git a/src/utility/lib/continuousDistribution.js b/src/utility/lib/continuousDistribution.js index a1eb3117..11495891 100644 --- a/src/utility/lib/continuousDistribution.js +++ b/src/utility/lib/continuousDistribution.js @@ -86,8 +86,10 @@ class ContinuousDistribution { let firstHigherIndex = this.xs.findIndex(X => X >= x); if (firstHigherIndex < 0) return this.ys[this.ys.length - 1]; if (firstHigherIndex === 0) return this.ys[0]; + let lowerOrEqualIndex = firstHigherIndex - 1; if (lowerOrEqualIndex < 0) lowerOrEqualIndex = 0; + let needsInterpolation = this.xs[lowerOrEqualIndex] !== x; if (needsInterpolation) { return interpolate(