Skip to content

Commit 65e4261

Browse files
committed
Add Brahmagupta's interpolation
1 parent b4ef926 commit 65e4261

File tree

3 files changed

+57
-1
lines changed

3 files changed

+57
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ This is a site where you can easily try out AI in your browser.
2121
| classification | Linear discriminant (FLD, LDA), Quadratic discriminant, Mixture discriminant, Least squares, Ridge, Naive bayes (gaussian), AODE, k nearest neighbor, Nearest centroid, Decision tree, Random forest, GBDT, XGBoost, ALMA, ROMMA, Online gradient descent, Passive aggressive, RLS, Second order perceptron, AROW, NAROW, Confidence weighted, CELLIP, IELLIP, Normal herd, (Multinomial) Logistic regression, (Multinomial) Probit, SVM, Gaussian process, HMM, LVQ, Perceptron, ADALINE, MLP |
2222
| semi-supervised classification | k nearest neighbor, Label propagation, Label spreading, k-means, GMM |
2323
| regression | Least squares, Ridge, Lasso, Elastic net, RLS, Bayesian linear, Poisson, Least absolute deviations, Least trimmed squares, Least median squares, Lp norm linear, Segmented, LOWESS, spline, Gaussian process, Principal components, Partial least squares, Projection pursuit, Quantile regression, k nearest neighbor, IDW, Nadaraya Watson, Priestley Chao, RBF Network, RVM, Decision tree, Random forest, GBDT, XGBoost, SVR, MLP, GMR, Isotonic, Ramer Douglas Peucker |
24-
| interpolation | Nearest neighbor, IDW, Linear, Logarithmic, Cosine, (Inverse) Smoothstep, Cubic, (Centripetal) Catmull-Rom, Hermit, Polynomial, Lagrange, Trigonometric, Spline, RBF Network, Akima |
24+
| interpolation | Nearest neighbor, IDW, Linear, Brahmagupta, Logarithmic, Cosine, (Inverse) Smoothstep, Cubic, (Centripetal) Catmull-Rom, Hermit, Polynomial, Lagrange, Trigonometric, Spline, RBF Network, Akima |
2525
| anomaly detection | Percentile, MAD, Grubbs's test, Thompson test, Tietjen Moore test, Generalized ESD, Hotelling, MT, MCD, k nearest neighbor, LOF, PCA, OCSVM, KDE, GMM, Isolation forest, Autoencoder, GAN |
2626
| dimension reduction | Random projection, PCA (kernel), LSA, MDS, Linear discriminant analysis, NCA, ICA, Principal curve, Sammon, FastMap, Sliced inverse regression, LLE, Laplacian eigenmaps, Isomap, SNE, t-SNE, SOM, GTM, NMF, Autoencoder, VAE |
2727
| feature selection | Mutual information, Ridge, Lasso, Elastic net, Decision tree, NCA |

js/model_selector.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ const AIMethods = [
196196
{ value: "knearestneighbor", title: "nearest neighbor" },
197197
{ value: "inverse_distance_weighting", title: "IDW" },
198198
{ value: "lerp", title: "Linear" },
199+
{ value: "brahmagupta_interpolation", title: "Brahmagupta" },
199200
// { value: "slerp", title: "Slerp" },
200201
{ value: "logarithmic_interpolation", title: "Logarithmic" },
201202
{ value: "cosine_interpolation", title: "Cosine" },

model/brahmagupta_interpolation.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
class BrahmaguptaInterpolation {
2+
// https://en.wikipedia.org/wiki/Brahmagupta%27s_interpolation_formula
3+
constructor() {}
4+
5+
fit(x, y) {
6+
const d = x.map((v, i) => [v, y[i]])
7+
d.sort((a, b) => a[0] - b[0])
8+
this._x = d.map((v) => v[0])
9+
this._y = d.map((v) => v[1])
10+
}
11+
12+
predict(target) {
13+
const n = this._x.length
14+
return target.map((t) => {
15+
if (t <= this._x[0]) {
16+
return this._y[0]
17+
} else if (t >= this._x[n - 1]) {
18+
return this._y[n - 1]
19+
}
20+
for (let i = 1; i < n; i++) {
21+
if (t <= this._x[i]) {
22+
const p = (t - this._x[i - 1]) / (this._x[i] - this._x[i - 1])
23+
const y0 = i > 1 ? this._y[i - 2] : this._y[i - 1] * 2 - this._y[i]
24+
const d1 = this._y[i] - this._y[i - 1]
25+
const d0 = this._y[i - 1] - y0
26+
return ((d1 - d0) / 2) * p ** 2 + ((d1 + d0) / 2) * p + this._y[i - 1]
27+
}
28+
}
29+
return this._y[n - 1]
30+
})
31+
}
32+
}
33+
34+
var dispBrahmagupta = function (elm, platform) {
35+
const calcBrahmagupta = function () {
36+
platform.fit((tx, ty) => {
37+
const model = new BrahmaguptaInterpolation()
38+
model.fit(
39+
tx.map((v) => v[0]),
40+
ty.map((v) => v[0])
41+
)
42+
platform.predict((px, cb) => {
43+
const pred = model.predict(px.map((v) => v[0]))
44+
cb(pred)
45+
}, 1)
46+
})
47+
}
48+
49+
elm.append('input').attr('type', 'button').attr('value', 'Calculate').on('click', calcBrahmagupta)
50+
}
51+
52+
export default function (platform) {
53+
platform.setting.ml.usage = 'Click and add data point. Then, click "Calculate".'
54+
dispBrahmagupta(platform.setting.ml.configElement, platform)
55+
}

0 commit comments

Comments
 (0)