This commit is contained in:
Roman Galochkin 2020-02-25 11:29:28 +03:00
parent 790791eab1
commit f9b04a1ca6
2 changed files with 35 additions and 7 deletions

View File

@ -14,20 +14,46 @@ let order = (shape: DistTypes.xyShape): DistTypes.xyShape => {
}; };
module Make = (Config: Config) => { module Make = (Config: Config) => {
let validateHasLength = (): bool => Array.length(Config.shape.xs) > 0; let xs = Config.shape.xs;
let validateSize = (): bool => let ys = Config.shape.ys;
Array.length(Config.shape.xs) == Array.length(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()) { if (!validateHasLength()) {
raise(ShapeWrong("You need at least one element.")); raise(ShapeWrong("You need at least one element."));
}; };
if (!validateSize()) { if (!validateSize()) {
raise(ShapeWrong("Arrays of \"xs\" and \"ys\" have different sizes.")); 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.")); raise(ShapeWrong("Arrays of \"xs\" and \"ys\" have different sizes."));
}; };
let minX = () => Config.shape.xs |> Array.get(_, 0); let minX = () => xs |> get(_, 0);
let maxX = () => let maxX = () => xs |> get(_, Array.length(xs) - 1);
Config.shape.xs |> Array.get(_, Array.length(Config.shape.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; 1;
}; };

View File

@ -86,8 +86,10 @@ class ContinuousDistribution {
let firstHigherIndex = this.xs.findIndex(X => X >= x); let firstHigherIndex = this.xs.findIndex(X => X >= x);
if (firstHigherIndex < 0) return this.ys[this.ys.length - 1]; if (firstHigherIndex < 0) return this.ys[this.ys.length - 1];
if (firstHigherIndex === 0) return this.ys[0]; if (firstHigherIndex === 0) return this.ys[0];
let lowerOrEqualIndex = firstHigherIndex - 1; let lowerOrEqualIndex = firstHigherIndex - 1;
if (lowerOrEqualIndex < 0) lowerOrEqualIndex = 0; if (lowerOrEqualIndex < 0) lowerOrEqualIndex = 0;
let needsInterpolation = this.xs[lowerOrEqualIndex] !== x; let needsInterpolation = this.xs[lowerOrEqualIndex] !== x;
if (needsInterpolation) { if (needsInterpolation) {
return interpolate( return interpolate(