From ee3816fae4976c26afadbfb090251549b2549272 Mon Sep 17 00:00:00 2001 From: Roman Galochkin Date: Tue, 25 Feb 2020 11:36:13 +0300 Subject: [PATCH] Findx --- src/utility/lib/CDFunctor.re | 26 +++++++++++++++++++++-- src/utility/lib/continuousDistribution.js | 2 ++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/src/utility/lib/CDFunctor.re b/src/utility/lib/CDFunctor.re index 94a34331..11bd6c77 100644 --- a/src/utility/lib/CDFunctor.re +++ b/src/utility/lib/CDFunctor.re @@ -34,8 +34,8 @@ module Make = (Config: Config) => { let maxX = () => get(xs, len(xs) - 1); let minY = () => get(ys, 0); let maxY = () => get(ys, len(ys) - 1); - let findY = x => { - let firstHigherIndex = Belt.Array.getIndexBy(xs, e => e > x); + let findY = (x: float): float => { + let firstHigherIndex = Belt.Array.getIndexBy(xs, e => e >= x); switch (firstHigherIndex) { | None => maxY() | Some(1) => minY() @@ -56,5 +56,27 @@ module Make = (Config: Config) => { }; }; }; + let findX = (y: float): float => { + let firstHigherIndex = Belt.Array.getIndexBy(ys, e => e >= y); + switch (firstHigherIndex) { + | None => maxX() + | Some(1) => minX() + | Some(firstHigherIndex) => + let lowerOrEqualIndex = + firstHigherIndex - 1 < 0 ? 0 : firstHigherIndex - 1; + let needsInterpolation = get(ys, lowerOrEqualIndex) != y; + if (needsInterpolation) { + Functions.interpolate( + get(ys, lowerOrEqualIndex), + get(ys, firstHigherIndex), + get(xs, lowerOrEqualIndex), + get(xs, firstHigherIndex), + y, + ); + } else { + xs[lowerOrEqualIndex]; + }; + }; + }; 1; }; diff --git a/src/utility/lib/continuousDistribution.js b/src/utility/lib/continuousDistribution.js index 11495891..75355a36 100644 --- a/src/utility/lib/continuousDistribution.js +++ b/src/utility/lib/continuousDistribution.js @@ -77,6 +77,7 @@ class ContinuousDistribution { } /** + * @Done * If xs=[1,2,3], and ys=[5,6,7], * then findY(1) = 5, findY(3) = 7, findY(1.5) = 5.5 * @param {number} x @@ -105,6 +106,7 @@ class ContinuousDistribution { } /** + * @Done * If xs=[1,2,3], and ys=[5,6,7], * then findX(5) = 1, findX(7) = 3, findY(5.5) = 1.5 * This should do the same thing as `findY`, but for Y.