UNPKG

@thi.ng/distance

Version:

N-dimensional distance metrics & K-nearest neighborhoods for point queries

29 lines (28 loc) 604 B
import { distManhattan, distManhattan2, distManhattan3 } from "@thi.ng/vectors/dist-manhattan"; class Manhattan { constructor(dim, metric) { this.dim = dim; this.metric = metric; this._invD = this.dim / Math.sqrt(dim); } _invD; to(x) { return x * this._invD; } from(x) { return Math.sqrt((x / this.dim) ** 2 * this.dim); } } const defManhattan = (dim) => new Manhattan(dim, distManhattan); const MANHATTAN2 = new Manhattan(2, distManhattan2); const MANHATTAN3 = new Manhattan(3, distManhattan3); export { MANHATTAN2, MANHATTAN3, Manhattan, defManhattan };