forked from morganherlocker/cubic-spline
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
143 lines (131 loc) · 3.2 KB
/
index.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
module.exports = class Spline {
constructor(xs, ys) {
this.xs = xs;
this.ys = ys;
this.ks = this.getNaturalKs(new Float64Array(this.xs.length));
}
getNaturalKs(ks) {
const n = this.xs.length - 1;
const A = zerosMat(n + 1, n + 2);
for (
let i = 1;
i < n;
i++ // rows
) {
A[i][i - 1] = 1 / (this.xs[i] - this.xs[i - 1]);
A[i][i] =
2 *
(1 / (this.xs[i] - this.xs[i - 1]) + 1 / (this.xs[i + 1] - this.xs[i]));
A[i][i + 1] = 1 / (this.xs[i + 1] - this.xs[i]);
A[i][n + 1] =
3 *
((this.ys[i] - this.ys[i - 1]) /
((this.xs[i] - this.xs[i - 1]) * (this.xs[i] - this.xs[i - 1])) +
(this.ys[i + 1] - this.ys[i]) /
((this.xs[i + 1] - this.xs[i]) * (this.xs[i + 1] - this.xs[i])));
}
A[0][0] = 2 / (this.xs[1] - this.xs[0]);
A[0][1] = 1 / (this.xs[1] - this.xs[0]);
A[0][n + 1] =
(3 * (this.ys[1] - this.ys[0])) /
((this.xs[1] - this.xs[0]) * (this.xs[1] - this.xs[0]));
A[n][n - 1] = 1 / (this.xs[n] - this.xs[n - 1]);
A[n][n] = 2 / (this.xs[n] - this.xs[n - 1]);
A[n][n + 1] =
(3 * (this.ys[n] - this.ys[n - 1])) /
((this.xs[n] - this.xs[n - 1]) * (this.xs[n] - this.xs[n - 1]));
return solve(A, ks);
}
/**
* inspired by https://stackoverflow.com/a/40850313/4417327
*/
getIndexBefore(target) {
let low = 0;
let high = this.xs.length;
let mid = 0;
while (low < high) {
mid = Math.floor((low + high) / 2);
if (this.xs[mid] < target && mid !== low) {
low = mid;
} else if (this.xs[mid] >= target && mid !== high) {
high = mid;
} else {
high = low;
}
}
return low + 1;
}
at(x) {
let i = this.getIndexBefore(x);
const t = (x - this.xs[i - 1]) / (this.xs[i] - this.xs[i - 1]);
const a =
this.ks[i - 1] * (this.xs[i] - this.xs[i - 1]) -
(this.ys[i] - this.ys[i - 1]);
const b =
-this.ks[i] * (this.xs[i] - this.xs[i - 1]) +
(this.ys[i] - this.ys[i - 1]);
const q =
(1 - t) * this.ys[i - 1] +
t * this.ys[i] +
t * (1 - t) * (a * (1 - t) + b * t);
return q;
}
};
function solve(A, ks) {
const m = A.length;
let h = 0;
let k = 0;
while (h < m && k <= m) {
let i_max = 0;
let max = -Infinity;
for (let i = h; i < m; i++) {
const v = Math.abs(A[i][k]);
if (v > max) {
i_max = i;
max = v;
}
}
if (A[i_max][k] === 0) {
k++;
} else {
swapRows(A, h, i_max);
for (let i = h + 1; i < m; i++) {
const f = A[i][k] / A[h][k];
A[i][k] = 0;
for (let j = k + 1; j <= m; j++) A[i][j] -= A[h][j] * f;
}
h++;
k++;
}
}
for (
let i = m - 1;
i >= 0;
i-- // rows = columns
) {
var v = 0;
if (A[i][i]) {
v = A[i][m] / A[i][i];
}
ks[i] = v;
for (
let j = i - 1;
j >= 0;
j-- // rows
) {
A[j][m] -= A[j][i] * v;
A[j][i] = 0;
}
}
return ks;
}
function zerosMat(r, c) {
const A = [];
for (let i = 0; i < r; i++) A.push(new Float64Array(c));
return A;
}
function swapRows(m, k, l) {
let p = m[k];
m[k] = m[l];
m[l] = p;
}