-
Notifications
You must be signed in to change notification settings - Fork 0
/
monotone_cubic_spline_interpolation.js
128 lines (120 loc) · 3.19 KB
/
monotone_cubic_spline_interpolation.js
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/**
* https://en.wikipedia.org/wiki/Monotone_cubic_interpolation
* Monotone cubic spline interpolation:
*
* Usage example:
* var f = createInterpolant([0, 1, 2, 3, 4], [0, 1, 4, 9, 16]);
* var message = '';
* for (var x = 0; x <= 4; x += 0.5) {
* var xSquared = f(x);
* message += x + ' squared is about ' + xSquared + '\n';
* }
* alert(message);
*
* @param {array} xs the x values
* @param {array} ys the y values
*/
export default function createInterpolant(xs, ys) {
var i,
length = xs.length;
// Deal with length issues
if (length !== ys.length) {
throw Error("Need an equal count of xs and ys.");
}
if (length === 0) {
return function (x) {
return 0;
};
}
if (length === 1) {
// Impl: Precomputing the result prevents problems if ys is mutated later and allows garbage collection of ys
// Impl: Unary plus properly converts values to numbers
var result = +ys[0];
return function (x) {
return result;
};
}
// Rearrange xs and ys so that xs is sorted
var indexes = [];
for (i = 0; i < length; i++) {
indexes.push(i);
}
indexes.sort(function (a, b) {
return xs[a] < xs[b] ? -1 : 1;
});
var oldXs = xs,
oldYs = ys;
// Impl: Creating new arrays also prevents problems if the input arrays are mutated later
xs = [];
ys = [];
// Impl: Unary plus properly converts values to numbers
for (i = 0; i < length; i++) {
xs.push(+oldXs[indexes[i]]);
ys.push(+oldYs[indexes[i]]);
}
// Get consecutive differences and slopes
var dys = [],
dxs = [],
ms = [];
for (i = 0; i < length - 1; i++) {
var 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
var c1s = [ms[0]];
for (i = 0; i < dxs.length - 1; i++) {
var m = ms[i],
mNext = ms[i + 1];
if (m * mNext <= 0) {
c1s.push(0);
} else {
var 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
var c2s = [],
c3s = [];
for (i = 0; i < c1s.length - 1; i++) {
var 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
var 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
var low = 0,
mid,
high = c3s.length - 1;
while (low <= high) {
mid = Math.floor(0.5 * (low + high));
var 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
var diff = x - xs[i],
diffSq = diff * diff;
return ys[i] + c1s[i] * diff + c2s[i] * diffSq + c3s[i] * diff * diffSq;
};
}