@thi.ng/distance
Version:
N-dimensional distance metrics & K-nearest neighborhoods for point queries
67 lines • 2.14 kB
TypeScript
import type { ReadonlyVec } from "@thi.ng/vectors";
import type { IDistance, Metric } from "./api.js";
/**
* Manhattan distance metric and conversion to/from Eucledian distances.
*
* @remarks
* The conversion are always based on n-D squares, i.e. the assumption that the
* distance is equally shared in each dimension.
*
* E.g. a Manhattan distance of 30 could be obtained from `[0,0]` -> `[10,20]`,
* but would be interpreted here as distance from `[0,0]` -> `[15,15]`, which
* produces the same Manhattan value, but yields a different Eucledian result.
* For lack of any other information about the distance values, this is however
* the only way to approach conversion and is sufficient for the purposes of
* this package...
*
* @example
* ```ts tangle:../export/manhattan.ts
* import { MANHATTAN2 } from "@thi.ng/distance";
*
* console.log(MANHATTAN2.metric([0,0], [10,20]));
* // 30
*
* console.log(MANHATTAN2.from(30));
* // 21.213203435596427
*
* console.log(Math.hypot(15, 15)); // <-- diagonal of manhattan square
* // 21.213203435596427
*
* console.log(Math.hypot(10, 20)); // <-- actual eucledian dist
* // 22.360679774997898
*
* console.log(MANHATTAN2.to(21.213203435596427));
* // 30
*
* // however, starting w/ eucledian dist first
* const e = Math.hypot(10, 20)
* // 22.360679774997898
*
* const m = MANHATTAN2.to(e)
* // 31.622776601683793
*
* console.log(MANHATTAN2.from(m) === e);
* // true
* ```
*/
export declare class Manhattan<T> implements IDistance<T> {
readonly dim: number;
readonly metric: Metric<T>;
protected _invD: number;
constructor(dim: number, metric: Metric<T>);
to(x: number): number;
from(x: number): number;
}
/**
* Returns a new Manhattan distance metric for n-D vectors of dimension `dim`.
*/
export declare const defManhattan: (dim: number) => Manhattan<ReadonlyVec>;
/**
* Manhattan distance metric for 2d vectors.
*/
export declare const MANHATTAN2: Manhattan<ReadonlyVec>;
/**
* Manhattan distance metric for 3d vectors.
*/
export declare const MANHATTAN3: Manhattan<ReadonlyVec>;
//# sourceMappingURL=manhattan.d.ts.map