/
lerp.js
44 lines (42 loc) · 992 Bytes
/
lerp.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
/**
* Linear interpolation
*/
export default class LinearInterpolation {
// https://ja.wikipedia.org/wiki/%E7%B7%9A%E5%BD%A2%E8%A3%9C%E9%96%93
constructor() {}
/**
* Fit model parameters.
*
* @param {number[]} x Training data
* @param {number[]} y Target values
*/
fit(x, y) {
const d = x.map((v, i) => [v, y[i]])
d.sort((a, b) => a[0] - b[0])
this._x = d.map(v => v[0])
this._y = d.map(v => v[1])
}
/**
* Returns predicted interpolated values.
*
* @param {number[]} target Sample data
* @returns {number[]} Predicted values
*/
predict(target) {
const n = this._x.length
return target.map(t => {
if (t <= this._x[0]) {
return this._y[0]
} else if (t >= this._x[n - 1]) {
return this._y[n - 1]
}
for (let i = 1; i < n; i++) {
if (t <= this._x[i]) {
const p = (t - this._x[i - 1]) / (this._x[i] - this._x[i - 1])
return p * (this._y[i] - this._y[i - 1]) + this._y[i - 1]
}
}
return this._y[n - 1]
})
}
}