This commit is contained in:
Roman Galochkin 2020-02-25 11:36:13 +03:00
parent aafccda8a1
commit ee3816fae4
2 changed files with 26 additions and 2 deletions

View File

@ -34,8 +34,8 @@ module Make = (Config: Config) => {
let maxX = () => get(xs, len(xs) - 1); let maxX = () => get(xs, len(xs) - 1);
let minY = () => get(ys, 0); let minY = () => get(ys, 0);
let maxY = () => get(ys, len(ys) - 1); let maxY = () => get(ys, len(ys) - 1);
let findY = x => { let findY = (x: float): float => {
let firstHigherIndex = Belt.Array.getIndexBy(xs, e => e > x); let firstHigherIndex = Belt.Array.getIndexBy(xs, e => e >= x);
switch (firstHigherIndex) { switch (firstHigherIndex) {
| None => maxY() | None => maxY()
| Some(1) => minY() | 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; 1;
}; };

View File

@ -77,6 +77,7 @@ class ContinuousDistribution {
} }
/** /**
* @Done
* If xs=[1,2,3], and ys=[5,6,7], * If xs=[1,2,3], and ys=[5,6,7],
* then findY(1) = 5, findY(3) = 7, findY(1.5) = 5.5 * then findY(1) = 5, findY(3) = 7, findY(1.5) = 5.5
* @param {number} x * @param {number} x
@ -105,6 +106,7 @@ class ContinuousDistribution {
} }
/** /**
* @Done
* If xs=[1,2,3], and ys=[5,6,7], * If xs=[1,2,3], and ys=[5,6,7],
* then findX(5) = 1, findX(7) = 3, findY(5.5) = 1.5 * then findX(5) = 1, findX(7) = 3, findY(5.5) = 1.5
* This should do the same thing as `findY`, but for Y. * This should do the same thing as `findY`, but for Y.