forked from be5invis/vsc-theme-verdandi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinterpolate.js
More file actions
64 lines (58 loc) · 1.73 KB
/
interpolate.js
File metadata and controls
64 lines (58 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
"use strict";
// Raw monotonic interpolation function
// xs must be ordered
module.exports = function(xs, ys) {
let i, length = xs.length;
// Get consecutive differences and slopes
let dys = [], dxs = [], ms = [];
for (i = 0; i < length - 1; i++) {
let dx = xs[i + 1] - xs[i], dy = ys[i + 1] - ys[i];
dxs.push(dx);
dys.push(dy);
ms.push(dy / dx);
}
// Get degree-1 coefficients
let c1s = [ms[0]];
for (i = 0; i < dxs.length - 1; i++) {
let m = ms[i], mNext = ms[i + 1];
if (m * mNext <= 0) {
c1s.push(0);
} else {
let dx = dxs[i], dxNext = dxs[i + 1], common = dx + dxNext;
c1s.push(3 * common / ((common + dxNext) / m + (common + dx) / mNext));
}
}
c1s.push(ms[ms.length - 1]);
// Get degree-2 and degree-3 coefficients
let c2s = [], c3s = [];
for (i = 0; i < c1s.length - 1; i++) {
let c1 = c1s[i], m = ms[i], invDx = 1 / dxs[i], common = c1 + c1s[i + 1] - m - m;
c2s.push((m - c1 - common) * invDx);
c3s.push(common * invDx * invDx);
}
// Return interpolant function
return function(x) {
// The rightmost point in the dataset should give an exact result
let i = xs.length - 1;
if (x == xs[i]) {
return ys[i];
}
// Search for the interval x is in, returning the corresponding y if x is one of the original xs
let low = 0, mid, high = c3s.length - 1;
while (low <= high) {
mid = Math.floor(0.5 * (low + high));
let xHere = xs[mid];
if (xHere < x) {
low = mid + 1;
} else if (xHere > x) {
high = mid - 1;
} else {
return ys[mid];
}
}
i = Math.max(0, high);
// Interpolate
let diff = x - xs[i], diffSq = diff * diff;
return ys[i] + c1s[i] * diff + c2s[i] * diffSq + c3s[i] * diff * diffSq;
};
};