JavaScript · Dense Distances

Dense Distances

import * as numkong from 'numkong'; 8.0.0-rc.1 · b1f866ab

Dense distance entrypoints work directly on the standard numeric TypedArray types.

import { sqeuclidean, euclidean, angular } from "numkong";

const a = new Float32Array([1, 2, 3]);
const b = new Float32Array([4, 5, 6]);

console.log(sqeuclidean(a, b)); // equivalent shape to a manual sum((a[i] - b[i]) ** 2)
console.log(euclidean(a, b));
console.log(angular(a, b));

When the storage type is one of the standard JS typed arrays, dtype inference is automatic.

API

Functions

sqeuclideanComputes the squared Euclidean distance between two vectors.
numkong.ts:233 ↗
function sqeuclidean(a: NumericArray, b: NumericArray): number
function sqeuclidean(
    a: DistanceArray,
    b: DistanceArray,
    dtype: DType,
): number
function sqeuclidean(a: TensorBase, b: TensorBase): number

Parameters

aThe first vector, as TypedArray or TensorBase.
bThe second vector, matching the type and length of a.
dtypeDTypeOptional dtype string for custom types, e.g. 'f16', 'bf16', 'e4m3'.

Returns

The squared Euclidean distance between a and b.
euclideanComputes the Euclidean distance between two vectors.
numkong.ts:248 ↗
function euclidean(a: NumericArray, b: NumericArray): number
function euclidean(
    a: DistanceArray,
    b: DistanceArray,
    dtype: DType,
): number
function euclidean(a: TensorBase, b: TensorBase): number

Parameters

aThe first vector, as TypedArray or TensorBase.
bThe second vector, matching the type and length of a.
dtypeDTypeOptional dtype string for custom types, e.g. 'f16', 'bf16', 'e4m3'.

Returns

The Euclidean distance between a and b.
angularComputes the angular distance between two vectors.
numkong.ts:263 ↗
function angular(a: NumericArray, b: NumericArray): number
function angular(
    a: DistanceArray,
    b: DistanceArray,
    dtype: DType,
): number
function angular(a: TensorBase, b: TensorBase): number

Parameters

aThe first vector, as TypedArray or TensorBase.
bThe second vector, matching the type and length of a.
dtypeDTypeOptional dtype string for custom types, e.g. 'f16', 'bf16', 'e4m3'.

Returns

The angular distance between a and b.