/**
* Computes the Bray-Curtis distance between `a` and `b`.
*
* @category Metrics
* @param {number[] | Float64Array} a
* @param {number[] | Float64Array} b
* @returns {number} The Bray-Curtis distance between `a` and `b`.
* @see {@link https://en.wikipedia.org/wiki/Bray%E2%80%93Curtis_dissimilarity}
*/
declare function bray_curtis(a: number[] | Float64Array, b: number[] | Float64Array): number;
/**
* Computes the canberra distance between `a` and `b`.
*
* @category Metrics
* @param {number[] | Float64Array} a
* @param {number[] | Float64Array} b
* @returns {number} The canberra distance between `a` and `b`.
* @see {@link https://en.wikipedia.org/wiki/Canberra_distance}
*/
declare function canberra(a: number[] | Float64Array, b: number[] | Float64Array): number;
/**
* Computes the chebyshev distance (L∞) between `a` and `b`.
*
* @category Metrics
* @param {number[] | Float64Array} a
* @param {number[] | Float64Array} b
* @returns {number} The chebyshev distance between `a` and `b`.
*/
declare function chebyshev(a: number[] | Float64Array, b: number[] | Float64Array): number;
/**
* Computes the cosine distance (not similarity) between `a` and `b`.
*
* @category Metrics
* @param {number[] | Float64Array} a
* @param {number[] | Float64Array} b
* @returns {number} The cosine distance between `a` and `b`.
* @example
* import { cosine } from "@saehrimnir/druidjs";
* const a = [1, 2, 3];
* const b = [4, 5, 6];
* const distance = cosine(a, b); // 0.9746318461970762
*/
declare function cosine(a: number[] | Float64Array, b: number[] | Float64Array): number;
/**
* Computes the euclidean distance (`l_2`) between `a` and `b`.
*
* @category Metrics
* @param {number[] | Float64Array} a
* @param {number[] | Float64Array} b
* @returns {number} The euclidean distance between `a` and `b`.
*/
declare function euclidean(a: number[] | Float64Array, b: number[] | Float64Array): number;
/**
* Computes the squared euclidean distance (l_2^2) between `a` and `b`.
*
* @category Metrics
* @param {number[] | Float64Array} a
* @param {number[] | Float64Array} b
* @returns {number} The squared euclidean distance between `a` and `b`.
*/
declare function euclidean_squared(a: number[] | Float64Array, b: number[] | Float64Array): number;
/**
* Computes the Goodman-Kruskal gamma coefficient for ordinal association.
*
* @category Metrics
* @param {number[] | Float64Array} a - First categorical/ordinal variable
* @param {number[] | Float64Array} b - Second categorical/ordinal variable
* @returns {number} The Goodman-Kruskal gamma coefficient between `a` and `b` (-1 to 1).
* @see {@link https://en.wikipedia.org/wiki/Goodman_and_Kruskal%27s_gamma}
*/
declare function goodman_kruskal(a: number[] | Float64Array, b: number[] | Float64Array): number;
/**
* Computes the hamming distance between `a` and `b`.
*
* @category Metrics
* @param {number[] | Float64Array} a
* @param {number[] | Float64Array} b
* @returns {number} The hamming distance between `a` and `b`.
*/
declare function hamming(a: number[] | Float64Array, b: number[] | Float64Array): number;
/**
* Computes the Haversine distance between two points on a sphere of unit length 1. Multiply the result with the radius of the sphere. (For instance Earth's radius is 6371km)
*
* @category Metrics
* @param {number[] | Float64Array} a - Point [lat1, lon1] in radians
* @param {number[] | Float64Array} b - Point [lat2, lon2] in radians
* @returns {number} The Haversine distance between `a` and `b`.
* @see {@link https://en.wikipedia.org/wiki/Haversine_formula}
*/
declare function haversine(a: number[] | Float64Array, b: number[] | Float64Array): number;
/**
* Computes the jaccard distance between `a` and `b`.
*
* @category Metrics
* @param {number[] | Float64Array} a
* @param {number[] | Float64Array} b
* @returns {number} The jaccard distance between `a` and `b`.
*/
declare function jaccard(a: number[] | Float64Array, b: number[] | Float64Array): number;
/**
* Computes the manhattan distance (`l_1`) between `a` and `b`.
*
* @category Metrics
* @param {number[] | Float64Array} a
* @param {number[] | Float64Array} b
* @returns {number} The manhattan distance between `a` and `b`.
*/
declare function manhattan(a: number[] | Float64Array, b: number[] | Float64Array): number;
/**
* Computes the Sokal-Michener distance between `a` and `b`.
*
* @category Metrics
* @param {number[] | Float64Array} a
* @param {number[] | Float64Array} b
* @returns {number} The Sokal-Michener distance between `a` and `b`.
*/
declare function sokal_michener(a: number[] | Float64Array, b: number[] | Float64Array): number;
/**
* Computes the 1D Wasserstein distance (Earth Mover's Distance) between two distributions.
*
* @category Metrics
* @param {number[] | Float64Array} a - First distribution (histogram or probability mass)
* @param {number[] | Float64Array} b - Second distribution (histogram or probability mass)
* @returns {number} The Wasserstein/EMD distance between `a` and `b`.
* @see {@link https://en.wikipedia.org/wiki/Wasserstein_metric}
*/
declare function wasserstein(a: number[] | Float64Array, b: number[] | Float64Array): number;
/**
* Computes the yule distance between `a` and `b`.
*
* @category Metrics
* @param {number[] | Float64Array} a
* @param {number[] | Float64Array} b
* @returns {number} The yule distance between `a` and `b`.
*/
declare function yule(a: number[] | Float64Array, b: number[] | Float64Array): number;
type Metric = (a: number[] | Float64Array, b: number[] | Float64Array) => number;
/**
* Computes the distance matrix of datamatrix `A`.
*
* @category Matrix
* @param {Matrix | Float64Array[] | number[][]} A - Matrix.
* @param {Metric} [metric=euclidean] - The diistance metric. Default is `euclidean`
* @returns {Matrix} The distance matrix of `A`.
*/
declare function distance_matrix(A: Matrix | Float64Array[] | number[][], metric?: Metric): Matrix;
/** @import { Metric } from "../metrics/index.js" */
/**
* Computes the k-nearest neighbors of each row of `A`.
*
* @category Matrix
* @param {Matrix} A - Either the data matrix, or a distance matrix.
* @param {number} k - The number of neighbors to compute.
* @param {Metric | "precomputed"} [metric=euclidean] Default is `euclidean`
* @returns {{ i: number; j: number; distance: number }[][]} The kNN graph.
*/
declare function k_nearest_neighbors(
A: Matrix,
k: number,
metric?: Metric | "precomputed",
): {
i: number;
j: number;
distance: number;
}[][];
/**
* Creates an Array containing `number` numbers from `start` to `end`. If `number = null`.
*
* @category Matrix
* @param {number} start - Start value.
* @param {number} end - End value.
* @param {number} [number] - Number of number between `start` and `end`.
* @returns {number[]} An array with `number` entries, beginning at `start` ending at `end`.
*/
declare function linspace(start: number, end: number, number?: number): number[];
/**
* Returns maximum in Array `values`.
*
* @category Utils
* @param {Iterable} values
* @returns {number}
*/
declare function max(values: Iterable): number;
/**
* Returns maximum in Array `values`.
*
* @category Utils
* @param {Iterable} values
* @returns {number}
*/
declare function min(values: Iterable): number;
/**
* @category Utils
* @class
*/
declare class Randomizer {
/**
* @template T Returns samples from an input Matrix or Array.
* @param {T[]} A - The input Matrix or Array.
* @param {number} n - The number of samples.
* @param {number} seed - The seed for the random number generator.
* @returns {T[]} - A random selection form `A` of `n` samples.
*/
static choice(A: T[], n: number, seed?: number): T[];
/**
* Mersenne Twister random number generator.
*
* @param {number} [_seed=new Date().getTime()] - The seed for the random number generator. If `_seed == null` then
* the actual time gets used as seed. Default is `new Date().getTime()`
* @see https://github.com/bmurray7/mersenne-twister-examples/blob/master/javascript-mersenne-twister.js
*/
constructor(_seed?: number);
_N: number;
_M: number;
_MATRIX_A: number;
_UPPER_MASK: number;
_LOWER_MASK: number;
/** @type {number[]} */
_mt: number[];
/** @type {number} */
_mti: number;
/** @type {number} */
_seed: number;
/** @type {number} seed */
set seed(_seed: number);
/**
* Returns the seed of the random number generator.
*
* @returns {number} - The seed.
*/
get seed(): number;
/**
* Returns a float between 0 and 1.
*
* @returns {number} - A random number between [0, 1]
*/
get random(): number;
/**
* Returns an integer between 0 and MAX_INTEGER.
*
* @returns {number} - A random integer.
*/
get random_int(): number;
gauss_random(): number;
_val: number | null | undefined;
/**
* @template T Returns samples from an input Matrix or Array.
* @param {T[]} A - The input Matrix or Array.
* @param {number} n - The number of samples.
* @returns {T[]} A random selection form `A` of `n` samples.
*/
choice(A: T[], n: number): T[];
}
/** @typedef {(i: number, j: number) => number} Accessor */
/**
* @class
* @category Matrix
*/
declare class Matrix {
/**
* Creates a Matrix out of `A`.
* @param {Matrix | Float64Array[] | number[][]} A - The matrix, array, or number, which should converted to a Matrix.
* @returns {Matrix}
* @example
* let A = Matrix.from([ [1, 0], [0, 1], ]); //creates a two by two identity matrix.
*/
static from(A: Matrix | Float64Array[] | number[][]): Matrix;
/**
* Creates a Matrix with the diagonal being the values of `v`.
*
* @example let S = Matrix.from_diag([1, 2, 3]); // creates [[1, 0, 0], [0, 2, 0], [0, 0, 3]]
*
* @param {number[] | Float64Array} v
* @returns {Matrix}
*/
static from_diag(v: number[] | Float64Array): Matrix;
/**
* Creates a Matrix with the diagonal being the values of `v`.
*
* @example let S = Matrix.from_diag([1, 2, 3]); // creates [[1, 0, 0], [0, 2, 0], [0, 0, 3]]
*
* @param {number[] | Float64Array} v
* @param {"col" | "row"} type
* @returns {Matrix}
*/
static from_vector(v: number[] | Float64Array, type: "col" | "row"): Matrix;
/**
* Solves the equation `Ax = b` using the conjugate gradient method. Returns the result `x`.
*
* @param {Matrix} A - Matrix
* @param {Matrix} b - Matrix
* @param {Randomizer | null} [randomizer]
* @param {number} [tol=1e-3] Default is `1e-3`
* @returns {Matrix}
*/
static solve_CG(A: Matrix, b: Matrix, randomizer?: Randomizer | null, tol?: number): Matrix;
/**
* Solves the equation `Ax = b`. Returns the result `x`.
*
* @param {Matrix | { L: Matrix; U: Matrix }} A - Matrix or LU Decomposition
* @param {Matrix} b - Matrix
* @returns {Matrix}
*/
static solve(
A:
| Matrix
| {
L: Matrix;
U: Matrix;
},
b: Matrix,
): Matrix;
/**
* `LU` decomposition of the Matrix `A`. Creates two matrices, so that the dot product `LU` equals `A`.
*
* @param {Matrix} A
* @returns {{ L: Matrix; U: Matrix }} The left triangle matrix `L` and the upper triangle matrix `U`.
*/
static LU(A: Matrix): {
L: Matrix;
U: Matrix;
};
/**
* Computes the determinante of `A`, by using the `LU` decomposition of `A`.
*
* @param {Matrix} A
* @returns {number} The determinate of the Matrix `A`.
*/
static det(A: Matrix): number;
/**
* Computes the `k` components of the SVD decomposition of the matrix `M`.
*
* @param {Matrix} M
* @param {number} [k=2] Default is `2`
* @returns {{ U: Float64Array[]; Sigma: Float64Array; V: Float64Array[] }}
*/
static SVD(
M: Matrix,
k?: number,
): {
U: Float64Array[];
Sigma: Float64Array;
V: Float64Array[];
};
/**
* @param {unknown} A
* @returns {A is unknown[]|number[]|Float64Array|Float32Array}
*/
static isArray(A: unknown): A is unknown[] | number[] | Float64Array | Float32Array;
/**
* @param {any[]} A
* @returns {A is number[][]|Float64Array[]}
*/
static is2dArray(A: any[]): A is number[][] | Float64Array[];
/**
* Creates a new Matrix. Entries are stored in a Float64Array.
*
* @example let A = new Matrix(10, 10, () => Math.random()); //creates a 10 times 10 random matrix. let B = new
* Matrix(3, 3, "I"); // creates a 3 times 3 identity matrix.
*
* @param {number} rows - The amount of rows of the matrix.
* @param {number} cols - The amount of columns of the matrix.
* @param {Accessor | string | number} value - Can be a function with row and col as parameters, a number, or
* "zeros", "identity" or "I", or "center".
*
* - **function**: for each entry the function gets called with the parameters for the actual row and column.
* - **string**: allowed are
*
* - "zero", creates a zero matrix.
* - "identity" or "I", creates an identity matrix.
* - "center", creates an center matrix.
* - **number**: create a matrix filled with the given value.
*/
constructor(rows: number, cols: number, value?: Accessor | string | number);
/** @type {number} */ _rows: number;
/** @type {number} */ _cols: number;
/** @type {Float64Array} */ _data: Float64Array;
/**
* Returns the `row`th row from the Matrix.
*
* @param {number} row
* @returns {Float64Array}
*/
row(row: number): Float64Array;
/**
* Returns an generator yielding each row of the Matrix.
*
* @yields {Float64Array}
*/
iterate_rows(): Generator, void, unknown>;
/**
* Sets the entries of `row`th row from the Matrix to the entries from `values`.
*
* @param {number} row
* @param {number[]} values
* @returns {Matrix}
*/
set_row(row: number, values: number[]): Matrix;
/**
* Swaps the rows `row1` and `row2` of the Matrix.
*
* @param {number} row1
* @param {number} row2
* @returns {Matrix}
*/
swap_rows(row1: number, row2: number): Matrix;
/**
* Returns the colth column from the Matrix.
*
* @param {number} col
* @returns {Float64Array}
*/
col(col: number): Float64Array;
/**
* Returns the `col`th entry from the `row`th row of the Matrix.
*
* @param {number} row
* @param {number} col
* @returns {number}
*/
entry(row: number, col: number): number;
/**
* Sets the {@link col}th entry from the {@link row}th row of the Matrix to the given
* {@link value}.
*
* @param {number} row
* @param {number} col
* @param {number} value
* @returns {Matrix}
*/
set_entry(row: number, col: number, value: number): Matrix;
/**
* Adds a given {@link value} to the {@link col}th entry from the {@link row}th row of the
* Matrix.
*
* @param {number} row
* @param {number} col
* @param {number} value
* @returns {Matrix}
*/
add_entry(row: number, col: number, value: number): Matrix;
/**
* Subtracts a given {@link value} from the {@link col}th entry from the {@link row}th row of the
* Matrix.
*
* @param {number} row
* @param {number} col
* @param {number} value
* @returns {Matrix}
*/
sub_entry(row: number, col: number, value: number): Matrix;
/**
* Returns a new transposed Matrix.
*
* @returns {Matrix}
*/
transpose(): Matrix;
/**
* Returns a new transposed Matrix. Short-form of `transpose`.
*
* @returns {Matrix}
*/
get T(): Matrix;
/**
* Returns the inverse of the Matrix.
*
* @returns {Matrix}
*/
inverse(): Matrix;
/**
* Returns the dot product. If `B` is an Array or Float64Array then an Array gets returned. If `B` is a Matrix then
* a Matrix gets returned.
*
* @param {Matrix | number[] | Float64Array} B The right side
* @returns {Matrix}
*/
dot(B: Matrix | number[] | Float64Array): Matrix;
/**
* Transposes the current matrix and returns the dot product with `B`. If `B` is an Array or Float64Array then an
* Array gets returned. If `B` is a Matrix then a Matrix gets returned.
*
* @param {Matrix | number[] | Float64Array} B The right side
* @returns {Matrix}
*/
transDot(B: Matrix | number[] | Float64Array): Matrix;
/**
* Returns the dot product with the transposed version of `B`. If `B` is an Array or Float64Array then an Array gets
* returned. If `B` is a Matrix then a Matrix gets returned.
*
* @param {Matrix | number[] | Float64Array} B The right side
* @returns {Matrix}
*/
dotTrans(B: Matrix | number[] | Float64Array): Matrix;
/**
* Computes the outer product from `this` and `B`.
*
* @param {Matrix} B
* @returns {Matrix}
*/
outer(B: Matrix): Matrix;
/**
* Appends matrix `B` to the matrix.
*
* @example let A = Matrix.from([ [1, 1], [1, 1], ]); // 2 by 2 matrix filled with ones. let B = Matrix.from([ [2,
* 2], [2, 2], ]); // 2 by 2 matrix filled with twos.
*
* A.concat(B, "horizontal"); // 2 by 4 matrix. [[1, 1, 2, 2], [1, 1, 2, 2]]
* A.concat(B, "vertical"); // 4 by 2 matrix. [[1, 1], [1, 1], [2, 2], [2, 2]]
* A.concat(B, "diag"); // 4 by 4 matrix. [[1, 1, 0, 0], [1, 1, 0, 0], [0, 0, 2, 2], [0, 0, 2, 2]]
*
* @param {Matrix} B - Matrix to append.
* @param {"horizontal" | "vertical" | "diag"} [type="horizontal"] - Type of concatenation. Default is
* `"horizontal"`
* @returns {Matrix}
*/
concat(B: Matrix, type?: "horizontal" | "vertical" | "diag"): Matrix;
/**
* Writes the entries of B in A at an offset position given by `offset_row` and `offset_col`.
*
* @param {number} offset_row
* @param {number} offset_col
* @param {Matrix} B
* @returns {Matrix}
*/
set_block(offset_row: number, offset_col: number, B: Matrix): Matrix;
/**
* Extracts the entries from the `start_row`th row to the `end_row`th row, the
* `start_col`th column to the `end_col`th column of the matrix. If `end_row` or `end_col` is
* empty, the respective value is set to `this.rows` or `this.cols`.
*
* @example let A = Matrix.from([ [1, 2, 3], [4, 5, 6], [7, 8, 9], ]); // a 3 by 3 matrix.
*
* A.get_block(1, 1); // [[5, 6], [8, 9]]
* A.get_block(0, 0, 1, 1); // [[1]]
* A.get_block(1, 1, 2, 2); // [[5]]
* A.get_block(0, 0, 2, 2); // [[1, 2], [4, 5]]
*
* @param {number} start_row
* @param {number} start_col
* @param {number | null} [end_row]
* @param {number | null} [end_col]
* @returns {Matrix} Returns a `end_row` - `start_row` times `end_col` - `start_col` matrix, with respective entries
* from the matrix.
*/
get_block(
start_row: number,
start_col: number,
end_row?: number | null,
end_col?: number | null,
): Matrix;
/**
* Returns a new array gathering entries defined by the indices given by argument.
*
* @param {number[]} row_indices - Array consists of indices of rows for gathering entries of this matrix
* @param {number[]} col_indices - Array consists of indices of cols for gathering entries of this matrix
* @returns {Matrix}
*/
gather(row_indices: number[], col_indices: number[]): Matrix;
/**
* Applies a function to each entry of the matrix.
*
* @private
* @param {(d: number, v: number) => number} f Function takes 2 parameters, the value of the actual entry and a
* value given by the function `v`. The result of `f` gets writen to the Matrix.
* @param {Accessor} v Function takes 2 parameters for `row` and `col`, and returns a value witch should be applied
* to the `col`th entry of the `row`th row of the matrix.
* @returns {Matrix}
*/
private _apply_array;
/**
* @param {number[] | Float64Array} values
* @param {(d: number, v: number) => number} f
* @returns {Matrix}
*/
_apply_rowwise_array(
values: number[] | Float64Array,
f: (d: number, v: number) => number,
): Matrix;
/**
* @param {number[] | Float64Array} values
* @param {(d: number, v: number) => number} f
* @returns {Matrix}
*/
_apply_colwise_array(
values: number[] | Float64Array,
f: (d: number, v: number) => number,
): Matrix;
/**
* @param {Matrix | number[] | Float64Array | number} value
* @param {(d: number, v: number) => number} f
* @returns {Matrix}
*/
_apply(
value: Matrix | number[] | Float64Array | number,
f: (d: number, v: number) => number,
): Matrix;
/**
* Clones the Matrix.
*
* @returns {Matrix}
*/
clone(): Matrix;
/**
* Entrywise multiplication with `value`.
*
* @example let A = Matrix.from([ [1, 2], [3, 4], ]); // a 2 by 2 matrix. let B = A.clone(); // B == A;
*
* A.mult(2); // [[2, 4], [6, 8]];
* A.mult(B); // [[1, 4], [9, 16]];
*
* @param {Matrix | Float64Array | number[] | number} value
* @param {Object} [options]
* @param {boolean} [options.inline=false] - If true, applies multiplication to the element, otherwise it creates
* first a copy and applies the multiplication on the copy. Default is `false`
* @returns {Matrix}
*/
mult(
value: Matrix | Float64Array | number[] | number,
{
inline,
}?: {
inline?: boolean | undefined;
},
): Matrix;
/**
* Entrywise division with `value`.
*
* @example let A = Matrix.from([ [1, 2], [3, 4], ]); // a 2 by 2 matrix. let B = A.clone(); // B == A;
*
* A.divide(2); // [[0.5, 1], [1.5, 2]];
* A.divide(B); // [[1, 1], [1, 1]];
*
* @param {Matrix | Float64Array | number[] | number} value
* @param {Object} [options]
* @param {Boolean} [options.inline=false] - If true, applies division to the element, otherwise it creates first a
* copy and applies the division on the copy. Default is `false`
* @returns {Matrix}
*/
divide(
value: Matrix | Float64Array | number[] | number,
{
inline,
}?: {
inline?: boolean | undefined;
},
): Matrix;
/**
* Entrywise addition with `value`.
*
* @example let A = Matrix.from([ [1, 2], [3, 4], ]); // a 2 by 2 matrix. let B = A.clone(); // B == A;
*
* A.add(2); // [[3, 4], [5, 6]];
* A.add(B); // [[2, 4], [6, 8]];
*
* @param {Matrix | Float64Array | number[] | number} value
* @param {Object} [options]
* @param {boolean} [options.inline=false] - If true, applies addition to the element, otherwise it creates first a
* copy and applies the addition on the copy. Default is `false`
* @returns {Matrix}
*/
add(
value: Matrix | Float64Array | number[] | number,
{
inline,
}?: {
inline?: boolean | undefined;
},
): Matrix;
/**
* Entrywise subtraction with `value`.
*
* @example let A = Matrix.from([ [1, 2], [3, 4], ]); // a 2 by 2 matrix. let B = A.clone(); // B == A;
*
* A.sub(2); // [[-1, 0], [1, 2]];
* A.sub(B); // [[0, 0], [0, 0]];
*
* @param {Matrix | Float64Array | number[] | number} value
* @param {Object} [options]
* @param {boolean} [options.inline=false] - If true, applies subtraction to the element, otherwise it creates first
* a copy and applies the subtraction on the copy. Default is `false`
* @returns {Matrix}
*/
sub(
value: Matrix | Float64Array | number[] | number,
{
inline,
}?: {
inline?: boolean | undefined;
},
): Matrix;
/**
* Returns the matrix in the given shape with the given function which returns values for the entries of the matrix.
*
* @param {[number, number, Accessor]} parameter - Takes an Array in the form [rows, cols, value], where rows and
* cols are the number of rows and columns of the matrix, and value is a function which takes two parameters (row
* and col) which has to return a value for the colth entry of the rowth row.
* @returns {Matrix}
*/
set shape([rows, cols, value]: [number, number, Accessor]);
/**
* Returns the number of rows and columns of the Matrix.
*
* @returns {number[]} An Array in the form [rows, columns].
*/
get shape(): number[];
/**
* Returns the Matrix as a Array of Float64Arrays.
*
* @returns {Float64Array[]}
*/
to2dArray(): Float64Array[];
/**
* Returns the Matrix as a Array of Arrays.
*
* @returns {number[][]}
*/
asArray(): number[][];
/**
* Returns the diagonal of the Matrix.
*
* @returns {Float64Array}
*/
diag(): Float64Array;
/**
* Returns the mean of all entries of the Matrix.
*
* @returns {number}
*/
mean(): number;
/**
* Returns the sum oof all entries of the Matrix.
*
* @returns {number}
*/
sum(): number;
/**
* Returns the entries of the Matrix.
*
* @returns {Float64Array}
*/
get values(): Float64Array;
/**
* Returns the mean of each row of the matrix.
*
* @returns {Float64Array}
*/
meanRows(): Float64Array;
/**
* Returns the mean of each column of the matrix.
*
* @returns {Float64Array}
*/
meanCols(): Float64Array;
/**
* Makes a `Matrix` object an iterable object.
*
* @yields {Float64Array}
*/
[Symbol.iterator](): Generator, void, unknown>;
}
type Accessor = (i: number, j: number) => number;
/** @import { Metric } from "../metrics/index.js" */
/**
* Computes the norm of a vector, by computing its distance to **0**.
*
* @category Matrix
* @param {Matrix | number[] | Float64Array} v - Vector.
* @param {Metric} [metric=euclidean] - Which metric should be used to compute the norm. Default is `euclidean`
* @returns {number} - The norm of `v`.
*/
declare function norm(v: Matrix | number[] | Float64Array, metric?: Metric): number;
/** @import { Metric } from "../metrics/index.js" */
/**
* Normalizes Vector `v`.
*
* @category Matrix
* @param {number[] | Float64Array} v - Vector
* @param {Metric} metric
* @returns {number[] | Float64Array} - The normalized vector with length 1.
*/
declare function normalize(v: number[] | Float64Array, metric?: Metric): number[] | Float64Array;
/** @import {InputType} from "../index.js" */
/**
* Base class for all clustering algorithms.
* @template Para
*/
declare class Clustering {
/**
* Compute the respective Clustering with given parameters
* @param {InputType} points
* @param {Para} parameters
*/
constructor(points: InputType, parameters: Para);
/** @type {InputType} */
_points: InputType;
/** @type {Para} */
_parameters: Para;
/** @type {Matrix} */
_matrix: Matrix;
/** @type {number} */
_N: number;
/** @type {number} */
_D: number;
/**
* @abstract
* @param {...unknown} args
* @returns {number[][]} An array with the indices of the clusters.
*/
get_clusters(...args: unknown[]): number[][];
/**
* @abstract
* @param {...unknown} args
* @returns {number[]} An array with the clusters id's for each point.
*/
get_cluster_list(...args: unknown[]): number[];
}
/** @import { InputType } from "../index.js" */
/** @import { ParametersCURE } from "./index.js" */
/**
* CURE (Clustering Using REpresentatives)
*
* An efficient clustering algorithm for large databases that is robust to outliers
* and identifies clusters with non-spherical shapes and wide variances in size.
*
* @class
* @extends Clustering
* @category Clustering
*/
declare class CURE extends Clustering {
/**
* @param {InputType} points
* @param {Partial} parameters
*/
constructor(points: InputType, parameters?: Partial);
/** @type {number} */
_K: number;
/** @type {number} */
_num_representatives: number;
/** @type {number} */
_shrink_factor: number;
/**
* @private
* @type {CURECluster[]}
*/
private _clusters;
/** @type {number[]} */
_cluster_ids: number[];
/**
* Initialize each point as its own cluster
* @private
*/
private _initialize_clusters;
/**
* Compute distance between two clusters using representative points
* @private
* @param {CURECluster} cluster1
* @param {CURECluster} cluster2
* @returns {number}
*/
private _cluster_distance;
/**
* Find the closest pair of clusters
* @private
* @returns {[number, number, number]} [index1, index2, distance]
*/
private _find_closest_clusters;
/**
* Merge two clusters
* @private
* @param {CURECluster} cluster1
* @param {CURECluster} cluster2
* @returns {CURECluster}
*/
private _merge_clusters;
/**
* Run CURE clustering algorithm
* @private
*/
private _cure;
/**
* Build the cluster list (point -> cluster assignment)
* @private
*/
private _build_cluster_ids;
/**
* @returns {number[][]}
*/
get_clusters(): number[][];
/**
* @returns {number[]}
*/
get_cluster_list(): number[];
}
/** @import { InputType } from "../index.js" */
/** @import { ParametersHierarchicalClustering } from "./index.js" */
/**
* Hierarchical Clustering
*
* A bottom-up approach (agglomerative) to clustering that builds a tree of clusters (dendrogram).
* Supports different linkage criteria: single, complete, and average.
*
* @class
* @extends Clustering
* @category Clustering
*/
declare class HierarchicalClustering extends Clustering {
/**
* @param {InputType} points - Data or distance matrix if metric is 'precomputed'
* @param {Partial} parameters
*/
constructor(points: InputType, parameters?: Partial);
/** @type {Cluster | null} */
root: Cluster | null;
_id: number;
_d_min: Float64Array;
_distance_matrix: Matrix;
_clusters: any[];
_c_size: Uint16Array;
/**
* @param {number} value - Value where to cut the tree.
* @param {"distance" | "depth"} [type="distance"] - Type of value. Default is `"distance"`
* @returns {Cluster[][]} - Array of clusters with the indices of the rows in given points.
*/
get_clusters_raw(value: number, type?: "distance" | "depth"): Cluster[][];
/**
* @param {number} value - Value where to cut the tree.
* @param {"distance" | "depth"} [type="distance"] - Type of value. Default is `"distance"`
* @returns {number[][]} - Array of clusters with the indices of the rows in given points.
*/
get_clusters(value: number, type?: "distance" | "depth"): number[][];
/**
* @param {number} value - Value where to cut the tree.
* @param {"distance" | "depth"} [type="distance"] - Type of value. Default is `"distance"`
* @returns {number[]} - Array of clusters with the indices of the rows in given points.
*/
get_cluster_list(value: number, type?: "distance" | "depth"): number[];
/**
* @private
* @param {Cluster} node
* @param {(d: {dist: number, depth: number}) => number} f
* @param {number} value
* @param {Cluster[][]} result
*/
private _traverse;
}
/** @private */
declare class Cluster {
/**
*
* @param {number} id
* @param {Cluster?} left
* @param {Cluster?} right
* @param {number} dist
* @param {Float64Array?} centroid
* @param {number} index
* @param {number} [size]
* @param {number} [depth]
*/
constructor(
id: number,
left: Cluster | null,
right: Cluster | null,
dist: number,
centroid: Float64Array | null,
index: number,
size?: number,
depth?: number,
);
/**@type {number} */
size: number;
/**@type {number} */
depth: number;
/**@type {Cluster | null} */
parent: Cluster | null;
id: number;
left: Cluster | null;
right: Cluster | null;
dist: number;
index: number;
centroid: Float64Array;
/**
*
* @param {Cluster} left
* @param {Cluster} right
* @returns {Float64Array}
*/
_calculate_centroid(left: Cluster, right: Cluster): Float64Array;
get isLeaf(): boolean;
/**
*
* @returns {Cluster[]}
*/
leaves(): Cluster[];
/**
*
* @returns {Cluster[]}
*/
descendants(): Cluster[];
}
/** @import { InputType } from "../index.js" */
/** @import { ParametersKMeans } from "./index.js" */
/**
* K-Means Clustering
*
* A popular clustering algorithm that partitions data into K clusters where each point
* belongs to the cluster with the nearest mean (centroid).
*
* @class
* @extends Clustering
* @category Clustering
* @see {@link KMedoids} for a more robust alternative
*
* @example
* import * as druid from "@saehrimnir/druidjs";
*
* const points = [[1, 1], [1.5, 1.5], [5, 5], [5.5, 5.5]];
* const kmeans = new druid.KMeans(points, { K: 2 });
*
* const clusters = kmeans.get_cluster_list(); // [0, 0, 1, 1]
* const centroids = kmeans.centroids; // center points
*/
declare class KMeans extends Clustering {
/**
* @param {InputType} points
* @param {Partial} parameters
*/
constructor(points: InputType, parameters?: Partial);
_K: number;
_randomizer: Randomizer;
/** @type {number[]} */
_clusters: number[];
_cluster_centroids: Float64Array[];
/** @returns {number} The number of clusters */
get k(): number;
/** @returns {Float64Array[]} The cluster centroids */
get centroids(): Float64Array[];
/** @returns {number[]} The cluster list */
get_cluster_list(): number[];
/** @returns {number[][]} An Array of clusters with the indices of the points. */
get_clusters(): number[][];
/**
* @private
* @param {number[]} point_indices
* @param {number[]} candidates
* @returns {number}
*/
private _furthest_point;
/**
* @private
* @param {number} K
* @returns {Float64Array[]}
*/
private _get_random_centroids;
/**
* @private
* @param {Float64Array[]} cluster_centroids
* @returns {{ clusters_changed: boolean; cluster_centroids: Float64Array[] }}
*/
private _iteration;
/**
* @private
* @param {number} K
* @returns {Float64Array[]}
*/
private _compute_centroid;
}
/** @import {InputType} from "../index.js" */
/** @import { ParametersKMedoids } from "./index.js" */
/**
* K-Medoids (PAM - Partitioning Around Medoids)
*
* A robust clustering algorithm similar to K-Means, but uses actual data points (medoids)
* as cluster centers and can work with any distance metric.
*
* @class
* @extends Clustering
* @category Clustering
* @see {@link KMeans} for a faster but less robust alternative
*/
declare class KMedoids extends Clustering {
/**
* @param {InputType} points - Data matrix
* @param {Partial} parameters
* @see {@link https://link.springer.com/chapter/10.1007/978-3-030-32047-8_16} Faster k-Medoids Clustering: Improving the PAM, CLARA, and CLARANS Algorithms
*/
constructor(points: InputType, parameters?: Partial);
_A: Float64Array[];
_max_iter: number;
_distance_matrix: Matrix;
_randomizer: Randomizer;
_clusters: any[];
_cluster_medoids: number[];
_is_initialized: boolean;
/** @returns {number[]} The cluster list */
get_cluster_list(): number[];
/** @returns {number[][]} - Array of clusters with the indices of the rows in given points. */
get_clusters(): number[][];
/** @returns {number} */
get k(): number;
/** @returns {number[]} */
get medoids(): number[];
/** @returns {number[]} */
get_medoids(): number[];
generator(): AsyncGenerator;
/** Algorithm 1. FastPAM1: Improved SWAP algorithm */
/** FastPAM1: One best swap per iteration */
_iteration(): boolean;
/**
*
* @param {number} i
* @param {number} j
* @param {Float64Array?} x_i
* @param {Float64Array?} x_j
* @returns
*/
_get_distance(i: number, j: number, x_i?: Float64Array | null, x_j?: Float64Array | null): number;
/**
*
* @param {Float64Array} x_j
* @param {number} j
* @returns
*/
_nearest_medoid(
x_j: Float64Array,
j: number,
): {
distance_nearest: number;
index_nearest: number;
distance_second: number;
index_second: number;
};
_update_clusters(): void;
/**
* Computes `K` clusters out of the `matrix`.
* @param {number} K - Number of clusters.
* @param {number[]} cluster_medoids
*/
init(K: number, cluster_medoids: number[]): this;
/**
* Algorithm 3. FastPAM LAB: Linear Approximate BUILD initialization.
*
* @param {number} K - Number of clusters
* @returns {number[]}
*/
_get_random_medoids(K: number): number[];
}
/** @import { ParametersMeanShift } from "./index.js" */
/** @import { InputType } from "../index.js" */
/**
* Mean Shift Clustering
*
* A non-parametric clustering technique that does not require prior knowledge of the
* number of clusters. It identifies centers of density in the data.
*
* @class
* @extends Clustering
* @category Clustering
*/
declare class MeanShift extends Clustering {
/**
*
* @param {InputType} points
* @param {Partial} parameters
*/
constructor(points: InputType, parameters?: Partial);
/** @type {number} */
_bandwidth: number;
/** @type {number} */
_max_iter: number;
/** @type {number} */
_tolerance: number;
/** @type {(dist: number) => number} */
_kernel: (dist: number) => number;
/** @type {Matrix} */
_points: Matrix;
/** @type {number[] | undefined} */
_clusters: number[] | undefined;
/** @type {number[][] | undefined} */
_cluster_list: number[][] | undefined;
/**
* @param {Matrix} matrix
* @returns {number}
*/
_compute_bandwidth(matrix: Matrix): number;
/**
* @param {number} dist
* @returns {number}
*/
_kernel_weight(dist: number): number;
_mean_shift(): void;
_assign_clusters(): void;
/**
* @returns {number[][]}
*/
get_clusters(): number[][];
/**
*
* @returns {number[]}
*/
get_cluster_list(): number[];
}
/** @import { InputType } from "../index.js" */
/** @import { ParametersOptics } from "./index.js" */
/** @typedef {Object} DBEntry
* @property {Float64Array} element
* @property {number} index
* @property {number} [reachability_distance]
* @property {boolean} processed
* @property {DBEntry[]} [neighbors]
*/
/**
* OPTICS (Ordering Points To Identify the Clustering Structure)
*
* A density-based clustering algorithm that extends DBSCAN. It handles clusters of varying
* densities and produces a reachability plot that can be used to extract clusters.
*
* @class
* @extends Clustering
* @category Clustering
*/
declare class OPTICS extends Clustering {
/**
* **O**rdering **P**oints **T**o **I**dentify the **C**lustering **S**tructure.
*
* @param {InputType} points - The data.
* @param {Partial} [parameters={}]
* @see {@link https://www.dbs.ifi.lmu.de/Publikationen/Papers/OPTICS.pdf}
* @see {@link https://en.wikipedia.org/wiki/OPTICS_algorithm}
*/
constructor(points: InputType, parameters?: Partial);
/**
* @private
* @type {DBEntry[]}
*/
private _ordered_list;
/** @type {number[][]} */
_clusters: number[][];
/**
* @private
* @type {DBEntry[]}
*/
private _DB;
_cluster_index: number;
/**
* @private
* @param {DBEntry} p - A point of the data.
* @returns {DBEntry[]} An array consisting of the `epsilon`-neighborhood of `p`.
*/
private _get_neighbors;
/**
* @private
* @param {DBEntry} p - A point of `matrix`.
* @returns {number|undefined} The distance to the `min_points`-th nearest point of `p`, or undefined if the
* `epsilon`-neighborhood has fewer elements than `min_points`.
*/
private _core_distance;
/**
* Updates the reachability distance of the points.
*
* @private
* @param {DBEntry} p
* @param {Heap} seeds
*/
private _update;
/**
* Expands the `cluster` with points in `seeds`.
*
* @private
* @param {Heap} seeds
* @param {number[]} cluster
*/
private _expand_cluster;
/**
* Returns an array of clusters.
*
* @returns {number[][]} Array of clusters with the indices of the rows in given `matrix`.
*/
get_clusters(): number[][];
/**
* @returns {number[]} Returns an array, where the ith entry defines the cluster affirmation of the ith point of
* given data. (-1 stands for outlier)
*/
get_cluster_list(): number[];
}
/** @import { InputType } from "../index.js" */
/** @import { ParametersXMeans } from "./index.js" */
/**
* @typedef SplitResult
* @property {number} index - Index of the cluster being split
* @property {number} bic_parent - BIC score of the parent cluster
* @property {number} bic_children - BIC score of the split children
* @property {number[][]} child_clusters - Clusters after splitting
* @property {Float64Array[]} child_centroids - Centroids of child clusters
*/
/**
* @typedef CandidateResult
* @property {KMeans} kmeans - The KMeans instance for this K
* @property {number} score - BIC score
*/
/**
* X-Means Clustering
*
* An extension of K-Means that automatically determines the number of clusters (K)
* using the Bayesian Information Criterion (BIC).
*
* @class
* @extends Clustering
* @category Clustering
*/
declare class XMeans extends Clustering {
/**
* XMeans clustering algorithm that automatically determines the optimal number of clusters.
*
* X-Means extends K-Means by starting with a minimum number of clusters and iteratively
* splitting clusters to improve the Bayesian Information Criterion (BIC).
*
* Algorithm:
* 1. Start with K_min clusters using KMeans
* 2. For each cluster, try splitting it into 2 sub-clusters
* 3. If BIC improves after splitting, keep the split
* 4. Run KMeans again with all (old + new) centroids
* 5. Repeat until K_max is reached or no more improvements
*
* @param {InputType} points - The data points to cluster
* @param {Partial} [parameters={}] - Configuration parameters
* @see {@link https://www.cs.cmu.edu/~dpelleg/download/xmeans.pdf}
* @see {@link https://github.com/annoviko/pyclustering/blob/master/pyclustering/cluster/xmeans.py}
* @see {@link https://github.com/haifengl/smile/blob/master/core/src/main/java/smile/clustering/XMeans.java}
*/
constructor(points: InputType, parameters?: Partial);
_randomizer: Randomizer;
/** @type {KMeans | null} */
_best_kmeans: KMeans | null;
/**
* Run the XMeans algorithm
*
* @private
*/
private _run;
/**
* Select the best candidate based on BIC score
*
* @private
* @param {Map} candidates
* @returns {KMeans}
*/
private _select_best_candidate;
/**
* Calculate Bayesian Information Criterion for a set of clusters.
*
* Uses Kass's formula for BIC calculation:
* BIC(θ) = L(D) - 0.5 * p * ln(N)
*
* Where:
* - L(D) is the log-likelihood of the data
* - p is the number of free parameters: (K-1) + D*K + 1
* - N is the total number of points
*
* @private
* @param {number[][]} clusters - Array of clusters with point indices
* @param {Float64Array[]} centroids - Array of centroids
* @returns {number} BIC score (higher is better)
*/
private _bic;
/**
* Get the computed clusters
*
* @returns {number[][]} Array of clusters, each containing indices of points
*/
get_clusters(): number[][];
/** @returns {number[]} The cluster list */
get_cluster_list(): number[];
/**
* Get the final centroids
*
* @returns {Float64Array[]} Array of centroids
*/
get centroids(): Float64Array[];
/**
* Get the optimal number of clusters found
*
* @returns {number} The number of clusters
*/
get k(): number;
}
type ParametersHierarchicalClustering = {
linkage: "single" | "complete" | "average";
metric: Metric | "precomputed";
};
type ParametersKMeans = {
K: number;
/**
* Default is `euclidean`
*/
metric: Metric;
/**
* Default is `1212`
*/
seed: number;
/**
* - Initial centroids. Default is `null`
*/
initial_centroids?: Float64Array[] | number[][] | undefined;
};
type ParametersKMedoids = {
/**
* - Number of clusters
*/
K: number;
/**
* - Maximum number of iterations. Default is 10 * Math.log10(N). Default is `null`
*/
max_iter: number | null;
/**
* - Metric defining the dissimilarity. Default is `euclidean`
*/
metric: Metric;
/**
* - Seed value for random number generator. Default is `1212`
*/
seed: number;
};
type ParametersOptics = {
/**
* - The minimum distance which defines whether a point is a neighbor or not.
*/
epsilon: number;
/**
* - The minimum number of points which a point needs to create a cluster. (Should be higher than 1, else each point creates a cluster.)
*/
min_points: number;
/**
* - The distance metric which defines the distance between two points of the points. Default is `euclidean`
*/
metric: Metric;
};
type ParametersXMeans = {
/**
* - Minimum number of clusters. Default is `2`
*/
K_min: number;
/**
* - Maximum number of clusters. Default is `10`
*/
K_max: number;
/**
* - Distance metric function. Default is `euclidean`
*/
metric: Metric;
/**
* - Random seed. Default is `1212`
*/
seed: number;
/**
* - Minimum points required to consider splitting a cluster. Default is `25`
*/
min_cluster_size: number;
/**
* - Convergence tolerance for KMeans. Default is `0.001`
*/
tolerance: number;
};
type ParametersMeanShift = {
/**
* - bandwidth
*/
bandwidth: number;
/**
* - Metric defining the dissimilarity. Default is `euclidean`
*/
metric: Metric;
/**
* - Seed value for random number generator. Default is `1212`
*/
seed: number;
/**
* - Kernel function. Default is `gaussian`
*/
kernel: "flat" | "gaussian" | ((dist: number) => number);
/**
* - Maximum number of iterations. Default is `Math.max(10, Math.floor(10 * Math.log10(N)))`
*/
max_iter?: number | undefined;
/**
* - Convergence tolerance. Default is `1e-3`
*/
tolerance?: number | undefined;
};
type ParametersCURE = {
/**
* - Target number of clusters. Default is `2`
*/
K: number;
/**
* - Number of representative points per cluster. Default is `5`
*/
num_representatives: number;
/**
* - Factor to shrink representatives toward centroid (0-1). Default is `0.5`
*/
shrink_factor: number;
/**
* - Distance metric function. Default is `euclidean`
*/
metric: Metric;
/**
* - Random seed. Default is `1212`
*/
seed: number;
};
/**
* @template T
* @typedef {Object} DisjointSetPayload
* @property {T} parent
* @property {Set} children
* @property {number} size
*/
/**
* @template T
* @class
* @category Data Structures
* @see {@link https://en.wikipedia.org/wiki/Disjoint-set_data_structure}
*/
declare class DisjointSet {
/**
* @param {T[]?} elements
*/
constructor(elements?: T[] | null);
/**
* @private
* @type {Map>}
*/
private _list;
/**
* @private
* @param {T} x
* @returns {DisjointSet}
*/
private make_set;
/**
* @param {T} x
* @returns
*/
find(x: T): T | null;
/**
* @param {T} x
* @param {T} y
* @returns
*/
union(x: T, y: T): this;
/** @param {T} x */
get_children(x: T): Set | null;
}
/** @import { Comparator } from "./index.js" */
/**
* @template T
* @class
* @category Data Structures
*/
declare class Heap {
/**
* Creates a Heap from an Array
*
* @template T
* @param {T[]} elements - Contains the elements for the Heap.
* @param {(d: T) => number} accessor - Function returns the value of the element.
* @param {"min" | "max" | Comparator} [comparator="min"] - Function returning true or false
* defining the wished order of the Heap, or String for predefined function. ("min" for a Min-Heap, "max" for a
* Max_heap). Default is `"min"`
* @returns {Heap}
*/
static heapify(
elements: T_1[],
accessor: (d: T_1) => number,
comparator?: "min" | "max" | Comparator,
): Heap;
/**
* A heap is a datastructure holding its elements in a specific way, so that the top element would be the first
* entry of an ordered list.
*
* @param {T[]?} elements - Contains the elements for the Heap. `elements` can be null.
* @param {(d: T) => number} accessor - Function returns the value of the element.
* @param {"min" | "max" | Comparator} [comparator="min"] - Function returning true or false
* defining the wished order of the Heap, or String for predefined function. ("min" for a Min-Heap, "max" for a
* Max_heap). Default is `"min"`
* @see {@link https://en.wikipedia.org/wiki/Binary_heap}
*/
constructor(
elements: (T[] | null) | undefined,
accessor: (d: T) => number,
comparator?: "min" | "max" | Comparator,
);
/** @type {{ element: T; value: number }[]} */
_container: {
element: T;
value: number;
}[];
/** @type {Comparator} */
_comparator: Comparator;
/** @type {(d: T) => number} */
_accessor: (d: T) => number;
/**
* Swaps elements of container array.
*
* @private
* @param {number} index_a
* @param {number} index_b
*/
private _swap;
/** @private */
private _heapify_up;
/**
* Pushes the element to the heap.
*
* @param {T} element
* @returns {Heap}
*/
push(element: T): Heap;
/**
* @private
* @param {Number} [start_index=0] Default is `0`
*/
private _heapify_down;
/**
* Removes and returns the top entry of the heap.
*
* @returns {{ element: T; value: number } | null} Object consists of the element and its value (computed by
* `accessor`}).
*/
pop(): {
element: T;
value: number;
} | null;
/**
* Returns the top entry of the heap without removing it.
*
* @returns {{ element: T; value: number } | null} Object consists of the element and its value (computed by
* `accessor`).
*/
get first(): {
element: T;
value: number;
} | null;
/**
* Yields the raw data
*
* @yields {T} Object consists of the element and its value (computed by `accessor`}).
*/
iterate(): Generator;
/**
* Returns the heap as ordered array.
*
* @returns {T[]} Array consisting the elements ordered by `comparator`.
*/
toArray(): T[];
/**
* Returns elements of container array.
*
* @returns {T[]} Array consisting the elements.
*/
data(): T[];
/**
* Returns the container array.
*
* @returns {{ element: T; value: number }[]} The container array.
*/
raw_data(): {
element: T;
value: number;
}[];
/**
* The size of the heap.
*
* @returns {number}
*/
get length(): number;
/**
* Returns false if the the heap has entries, true if the heap has no entries.
*
* @returns {boolean}
*/
get empty(): boolean;
}
type Comparator = (a: number, b: number) => boolean;
/** @import {InputType} from "../index.js" */
/**
* @abstract
* @template {InputType} T
* @template {{ seed?: number }} Para
*
* Base class for all Dimensionality Reduction (DR) algorithms.
*
* Provides a common interface for parameters management, data initialization,
* and transformation (both synchronous and asynchronous).
*
* @class
*/
declare class DR<
T extends InputType,
Para extends {
seed?: number;
},
> {
/**
* Computes the projection.
*
* @template {InputType} T
* @template {{ seed?: number }} Para
* @param {T} X
* @param {Para} parameters
* @param {...unknown} args - Takes the same arguments of the constructor of the respective DR method.
* @returns {T} The dimensionality reduced dataset.
*/
static transform<
T_1 extends InputType,
Para_1 extends {
seed?: number;
},
>(X: T_1, parameters: Para_1, ...args: unknown[]): T_1;
/**
* Computes the projection.
*
* @template {{ seed?: number }} Para
* @param {InputType} X
* @param {Para} parameters
* @param {...unknown} args - Takes the same arguments of the constructor of the respective DR method.
* @returns {Generator} A generator yielding the intermediate steps of the dimensionality
* reduction method.
*/
static generator<
Para_1 extends {
seed?: number;
},
>(X: InputType, parameters: Para_1, ...args: unknown[]): Generator;
/**
* Computes the projection.
*
* @template {{ seed?: number }} Para
* @param {InputType} X
* @param {Para} parameters
* @param {...unknown} args - Takes the same arguments of the constructor of the respective DR method.
* @returns {Promise} A promise yielding the dimensionality reduced dataset.
*/
static transform_async<
Para_1 extends {
seed?: number;
},
>(X: InputType, parameters: Para_1, ...args: unknown[]): Promise;
/**
* Takes the default parameters and seals them, remembers the type of input `X`, and initializes the random number
* generator.
*
* @param {T} X - The high-dimensional data.
* @param {Para} default_parameters - Object containing default parameterization of the DR method.
* @param {Partial} parameters - Object containing parameterization of the DR method to override defaults.
*/
constructor(X: T, default_parameters: Para, parameters?: Partial);
/** @type {number} */
_D: number;
/** @type {number} */
_N: number;
/** @type {Randomizer} */
_randomizer: Randomizer;
/** @type {boolean} */
_is_initialized: boolean;
/** @type {T} */
__input: T;
/** @type {Para} */
_parameters: Para;
/** @type {"array" | "matrix" | "typed"} */
_type: "array" | "matrix" | "typed";
/** @type {Matrix} */
X: Matrix;
/** @type {Matrix} */
Y: Matrix;
/**
* Get all Parameters.
* @overload
* @returns {Para}
*/
parameter(): Para;
/**
* Get value of given parameter.
* @template {keyof Para} K
* @overload
* @param {K} name - Name of the parameter.
* @returns {Para[K]}
*/
parameter(name: K): Para[K];
/**
* Set value of given parameter.
* @template {keyof Para} K
* @overload
* @param {K} name - Name of the parameter.
* @param {Para[K]} value - Value of the parameter to set.
* @returns {this}
*/
parameter(name: K, value: Para[K]): this;
/**
* Computes the projection.
*
* @abstract
* @param {...unknown} args
* @returns {T} The projection.
*/
transform(...args: unknown[]): T;
/**
* Computes the projection.
*
* @abstract
* @param {...unknown} args
* @returns {Generator} The intermediate steps of the projection.
*/
generator(...args: unknown[]): Generator;
/**
* @abstract
* @param {...unknown} args
*/
init(...args: unknown[]): void;
/**
* If the respective DR method has an `init` function, call it before `transform`.
*
* @returns {DR}
*/
check_init(): DR;
/** @returns {T} The projection in the type of input `X`. */
get projection(): T;
/**
* Computes the projection.
*
* @param {...unknown} args - Arguments the transform method of the respective DR method takes.
* @returns {Promise} The dimensionality reduced dataset.
*/
transform_async(...args: unknown[]): Promise;
}
/** @import { InputType } from "../index.js" */
/** @import { ParametersFASTMAP } from "./index.js"; */
/**
* FastMap algorithm for dimensionality reduction.
*
* A very fast algorithm for projecting high-dimensional data into a lower-dimensional
* space while preserving pairwise distances. It works similarly to PCA but uses
* only a subset of the data to find projection axes.
*
* @class
* @template {InputType} T
* @extends DR
* @category Dimensionality Reduction
*/
declare class FASTMAP extends DR {
/**
* @template {InputType} T
* @param {T} X
* @param {Partial} parameters
* @returns {T}
*/
static transform(X: T_1, parameters: Partial): T_1;
/**
* @template {InputType} T
* @param {T} X
* @param {Partial} parameters
* @returns {Generator}
*/
static generator(
X: T_1,
parameters: Partial,
): Generator;
/**
* @template {InputType} T
* @param {T} X
* @param {Partial} parameters
* @returns {Promise}
*/
static transform_async(
X: T_1,
parameters: Partial,
): Promise;
/**
* FastMap: a fast algorithm for indexing, data-mining and visualization of traditional and multimedia datasets.
* @param {T} X - The high-dimensional data.
* @param {Partial} parameters - Object containing parameterization of the DR method.
* @see {@link https://doi.org/10.1145/223784.223812}
*/
constructor(X: T, parameters: Partial);
/**
* Chooses two points which are the most distant in the actual projection.
*
* @private
* @param {(a: number, b: number) => number} dist
* @returns {[number, number, number]} An array consisting of first index, second index, and distance between the
* two points.
*/
private _choose_distant_objects;
/**
* Computes the projection.
*
* @returns {T} The `d`-dimensional projection of the data matrix `X`.
*/
transform(): T;
generator(): Generator;
}
/** @import {InputType} from "../index.js" */
/** @import {ParametersISOMAP} from "./index.js" */
/** @import {EigenArgs} from "../linear_algebra/index.js" */
/**
* Isomap (Isometric Mapping)
*
* A nonlinear dimensionality reduction algorithm that uses geodesic distances
* between points on a manifold to perform embedding. It builds a neighborhood
* graph and uses MDS on the shortest-path distances.
*
* @class
* @template {InputType} T
* @extends DR
* @category Dimensionality Reduction
* @see {@link LLE} for another nonlinear alternative
*/
declare class ISOMAP extends DR {
/**
* @template {InputType} T
* @param {T} X
* @param {Partial} [parameters]
* @returns {T}
*/
static transform(X: T_1, parameters?: Partial): T_1;
/**
* @template {InputType} T
* @param {T} X
* @param {Partial} [parameters]
* @returns {Generator}
*/
static generator(
X: T_1,
parameters?: Partial,
): Generator;
/**
* @template {InputType} T
* @param {T} X
* @param {Partial} [parameters]
* @returns {Promise}
*/
static transform_async(
X: T_1,
parameters?: Partial,
): Promise;
/**
* Isometric feature mapping (ISOMAP).
*
* @param {T} X - The high-dimensional data.
* @param {Partial} [parameters] - Object containing parameterization of the DR method.
* @see {@link https://doi.org/10.1126/science.290.5500.2319}
*/
constructor(X: T, parameters?: Partial);
defaults: ParametersISOMAP;
/**
* Computes the projection.
*
* @returns {Generator} A generator yielding the intermediate steps of the projection.
*/
generator(): Generator;
/**
* @returns {T}
*/
transform(): T;
}
/** @import {InputType} from "../index.js" */
/** @import {ParametersLDA} from "./index.js" */
/** @import {EigenArgs} from "../linear_algebra/index.js" */
/**
* Linear Discriminant Analysis (LDA)
*
* A supervised dimensionality reduction technique that finds the axes that
* maximize the separation between multiple classes.
*
* @class
* @template {InputType} T
* @extends DR
* @category Dimensionality Reduction
*/
declare class LDA extends DR {
/**
* @template {InputType} T
* @template {{ seed?: number }} Para
* @param {T} X
* @param {Para} parameters
* @returns {T}
*/
static transform<
T_1 extends InputType,
Para extends {
seed?: number;
},
>(X: T_1, parameters: Para): T_1;
/**
* @template {InputType} T
* @template {{ seed?: number }} Para
* @param {T} X
* @param {Para} parameters
* @returns {Generator}
*/
static generator<
T_1 extends InputType,
Para extends {
seed?: number;
},
>(X: T_1, parameters: Para): Generator;
/**
* @template {InputType} T
* @template {{ seed?: number }} Para
* @param {T} X
* @param {Para} parameters
* @returns {Promise}
*/
static transform_async<
T_1 extends InputType,
Para extends {
seed?: number;
},
>(X: T_1, parameters: Para): Promise;
/**
* Linear Discriminant Analysis.
*
* @param {T} X - The high-dimensional data.
* @param {Partial & { labels: any[] | Float64Array }} parameters - Object containing parameterization of the DR method.
* @see {@link https://onlinelibrary.wiley.com/doi/10.1111/j.1469-1809.1936.tb02137.x}
*/
constructor(
X: T,
parameters: Partial & {
labels: any[] | Float64Array;
},
);
/**
* Transforms the inputdata `X` to dimensionality `d`.
*
* @returns {Generator} A generator yielding the intermediate steps of the projection.
*/
generator(): Generator;
/**
* Transforms the inputdata `X` to dimensionality `d`.
*
* @returns {T} - The projected data.
*/
transform(): T;
}
/** @import {InputType} from "../index.js" */
/** @import {ParametersLLE} from "./index.js" */
/** @import {EigenArgs} from "../linear_algebra/index.js" */
/**
* Locally Linear Embedding (LLE)
*
* A nonlinear dimensionality reduction technique that preserves local
* linear relationships between points. It represents each point as a linear
* combination of its neighbors.
*
* @class
* @template {InputType} T
* @extends DR
* @category Dimensionality Reduction
* @see {@link ISOMAP} for another nonlinear alternative
*/
declare class LLE extends DR {
/**
* @template {InputType} T
* @param {T} X
* @param {Partial} parameters
* @returns {T}
*/
static transform(X: T_1, parameters: Partial): T_1;
/**
* @template {InputType} T
* @param {T} X
* @param {Partial} parameters
* @returns {Generator}
*/
static generator(
X: T_1,
parameters: Partial,
): Generator;
/**
* @template {InputType} T
* @param {T} X
* @param {Partial} parameters
* @returns {Promise}
*/
static transform_async(
X: T_1,
parameters: Partial,
): Promise;
/**
* Locally Linear Embedding.
*
* @param {T} X - The high-dimensional data.
* @param {Partial} parameters - Object containing parameterization of the DR method.
* @see {@link https://doi.org/10.1126/science.290.5500.2323}
*/
constructor(X: T, parameters: Partial);
/**
* Transforms the inputdata `X` to dimensionality `d`.
*
* @returns {Generator} A generator yielding the intermediate steps of the projection.
*/
generator(): Generator;
/**
* Transforms the inputdata `X` to dimensionality `d`.
*
* @returns {T}
*/
transform(): T;
}
/** @import {InputType} from "../index.js" */
/** @import {ParametersLSP} from "./index.js" */
/**
* Least Square Projection (LSP)
*
* A dimensionality reduction technique that uses a small set of control points
* (projected with MDS) to define the projection for the rest of the data
* using a Laplacian-based optimization.
*
* @class
* @template {InputType} T
* @extends DR
* @category Dimensionality Reduction
*/
declare class LSP extends DR {
/**
* @template {InputType} T
* @param {T} X
* @param {Partial} [parameters]
* @returns {T}
*/
static transform(X: T_1, parameters?: Partial): T_1;
/**
* @template {InputType} T
* @param {T} X
* @param {Partial} [parameters]
* @returns {Generator}
*/
static generator(
X: T_1,
parameters?: Partial,
): Generator;
/**
* @template {InputType} T
* @param {T} X
* @param {Partial} [parameters]
* @returns {Promise}
*/
static transform_async(
X: T_1,
parameters?: Partial,
): Promise;
/**
* Least Squares Projection.
*
* @param {T} X - The high-dimensional data.
* @param {Partial} [parameters] - Object containing parameterization of the DR method.
* @see {@link https://ieeexplore.ieee.org/document/4378370}
*/
constructor(X: T, parameters?: Partial);
/**
* @returns {LSP}
*/
init(): LSP;
_A: Matrix | undefined;
_b: Matrix | undefined;
/**
* Computes the projection.
*
* @returns {T} Returns the projection.
*/
transform(): T;
}
/** @import {InputType} from "../index.js" */
/** @import {ParametersLTSA} from "./index.js" */
/** @import {EigenArgs} from "../linear_algebra/index.js" */
/**
* Local Tangent Space Alignment (LTSA)
*
* A nonlinear dimensionality reduction algorithm that represents the local
* geometry of the manifold by tangent spaces and then aligns them to reveal
* the global structure.
*
* @class
* @template {InputType} T
* @extends DR
* @category Dimensionality Reduction
*/
declare class LTSA extends DR {
/**
* @template {InputType} T
* @param {T} X
* @param {Partial} parameters
* @returns {T}
*/
static transform(X: T_1, parameters: Partial): T_1;
/**
* @template {InputType} T
* @param {T} X
* @param {Partial} parameters
* @returns {Generator}
*/
static generator(
X: T_1,
parameters: Partial,
): Generator;
/**
* @template {InputType} T
* @param {T} X
* @param {Partial} parameters
* @returns {Promise}
*/
static transform_async(
X: T_1,
parameters: Partial,
): Promise;
/**
* Local Tangent Space Alignment
*
* @param {T} X - The high-dimensional data.
* @param {Partial} parameters - Object containing parameterization of the DR method.
* @see {@link https://epubs.siam.org/doi/abs/10.1137/S1064827502419154}
*/
constructor(X: T, parameters: Partial);
/**
* Transforms the inputdata `X` to dimensionality `d`.
*
* @returns {Generator} A generator yielding the intermediate steps of the projection.
*/
generator(): Generator;
/**
* Transforms the inputdata `X` to dimenionality `d`.
*
* @returns {T}
*/
transform(): T;
}
/** @import {InputType} from "../index.js" */
/** @import {ParametersMDS} from "./index.js" */
/** @import {EigenArgs} from "../linear_algebra/index.js" */
/**
* Classical Multidimensional Scaling (MDS)
*
* A linear dimensionality reduction technique that seeks to preserve the
* pairwise distances between points as much as possible in the lower-dimensional
* space.
*
* @class
* @template {InputType} T
* @extends DR
* @category Dimensionality Reduction
* @see {@link PCA} for another linear alternative
*/
declare class MDS extends DR {
/**
* @template {InputType} T
* @param {T} X
* @param {Partial} [parameters]
* @returns {T}
*/
static transform(X: T_1, parameters?: Partial): T_1;
/**
* @template {InputType} T
* @param {T} X
* @param {Partial} [parameters]
* @returns {Generator}
*/
static generator(
X: T_1,
parameters?: Partial,
): Generator;
/**
* @template {InputType} T
* @param {T} X
* @param {Partial} [parameters]
* @returns {Promise}
*/
static transform_async(
X: T_1,
parameters?: Partial,
): Promise;
/**
* Classical MDS.
*
* @param {T} X - The high-dimensional data.
* @param {Partial} [parameters] - Object containing parameterization of the DR method.
*/
constructor(X: T, parameters?: Partial);
/**
* Transforms the inputdata `X` to dimensionality `d`.
*
* @returns {Generator} A generator yielding the intermediate steps of the projection.
*/
generator(): Generator;
/**
* Transforms the inputdata `X` to dimensionality `d`.
*
* @returns {T}
*/
transform(): T;
_d_X: Matrix | undefined;
/** @returns {number} - The stress of the projection. */
stress(): number;
}
/** @import {InputType} from "../index.js" */
/** @import {ParametersPCA} from "./index.js" */
/** @import {EigenArgs} from "../linear_algebra/index.js" */
/**
* Principal Component Analysis (PCA)
*
* A linear dimensionality reduction technique that identifies the axes (principal components)
* along which the variance of the data is maximized.
*
* @class
* @template {InputType} T
* @extends DR
* @category Dimensionality Reduction
* @see {@link MDS} for another linear alternative
*
* @example
* import * as druid from "@saehrimnir/druidjs";
*
* const X = [[1, 2], [3, 4], [5, 6]];
* const pca = new druid.PCA(X, { d: 2 });
* const Y = pca.transform();
* // [[x1, y1], [x2, y2], [x3, y3]]
*/
declare class PCA extends DR {
/**
* @template {InputType} T
* @param {T} X
* @param {Partial} parameters
* @returns {T}
*/
static transform(X: T_1, parameters: Partial): T_1;
/**
* @template {InputType} T
* @param {T} X
* @param {Partial} parameters
* @returns {Matrix}
*/
static principal_components(
X: T_1,
parameters: Partial,
): Matrix;
/**
* @template {InputType} T
* @param {T} X
* @param {Partial} [parameters]
* @returns {Generator}
*/
static generator(
X: T_1,
parameters?: Partial,
): Generator;
/**
* @template {InputType} T
* @param {T} X
* @param {Partial} [parameters]
* @returns {Promise}
*/
static transform_async(
X: T_1,
parameters?: Partial,
): Promise;
/**
* @param {T} X - The high-dimensional data.
* @param {Partial} [parameters] - Object containing parameterization of the DR method.
*/
constructor(X: T, parameters?: Partial);
/**
* Transforms the inputdata `X` to dimensionality `d`.
*
* @returns {Generator} A generator yielding the intermediate steps of the projection.
*/
generator(): Generator;
/**
* Transforms the inputdata `X` to dimensionality `d`.
*
* @returns {T} - The projected data.
*/
transform(): T;
/**
* Computes the `d` principal components of Matrix `X`.
*
* @returns {Matrix}
*/
principal_components(): Matrix;
V: Matrix | undefined;
}
/** @import {InputType} from "../index.js" */
/** @import {ParametersPCA, ParametersMDS, ParametersSAMMON} from "./index.js" */
/** @typedef {"PCA" | "MDS" | "random"} AvailableInit */
/** @typedef {{ PCA: ParametersPCA; MDS: ParametersMDS; random: {} }} ChooseDR */
/**
* Sammon's Mapping
*
* A nonlinear dimensionality reduction technique that minimizes a stress
* function based on the ratio of pairwise distances in high and low dimensional spaces.
*
* @class
* @template {InputType} T
* @extends DR>
* @category Dimensionality Reduction
*/
declare class SAMMON extends DR> {
/**
* @template {InputType} T
* @param {T} X
* @param {Partial>} [parameters]
* @returns {T}
*/
static transform(
X: T_1,
parameters?: Partial>,
): T_1;
/**
* @template {InputType} T
* @param {T} X
* @param {Partial>} [parameters]
* @returns {Generator}
*/
static generator(
X: T_1,
parameters?: Partial>,
): Generator;
/**
* @template {InputType} T
* @param {T} X
* @param {Partial>} [parameters]
* @returns {Promise}
*/
static transform_async(
X: T_1,
parameters?: Partial>,
): Promise;
/**
* SAMMON's Mapping
*
* @param {T} X - The high-dimensional data.
* @param {Partial>} [parameters] - Object containing parameterization of the DR
* method.
* @see {@link https://arxiv.org/pdf/2009.01512.pdf}
*/
constructor(X: T, parameters?: Partial>);
/** @type {Matrix | undefined} */
distance_matrix: Matrix | undefined;
/**
* Initializes the projection.
*
* @param {Matrix | undefined} D
* @returns {asserts D is Matrix}
*/
init(D: Matrix | undefined): asserts D is Matrix;
/**
* Transforms the inputdata `X` to dimensionality 2.
*
* @param {number} [max_iter=200] - Maximum number of iteration steps. Default is `200`
* @returns {T} The projection of `X`.
*/
transform(max_iter?: number): T;
/**
* Transforms the inputdata `X` to dimenionality 2.
*
* @param {number} [max_iter=200] - Maximum number of iteration steps. Default is `200`
* @returns {Generator} A generator yielding the intermediate steps of the projection of
* `X`.
*/
generator(max_iter?: number): Generator;
_step(): Matrix;
}
type AvailableInit = "PCA" | "MDS" | "random";
type ChooseDR = {
PCA: ParametersPCA;
MDS: ParametersMDS;
random: {};
};
/** @import {InputType} from "../index.js" */
/** @import {ParametersSMACOF} from "./index.js" */
/**
* Metric Multidimensional Scaling (MDS) via SMACOF.
*
* SMACOF (Scaling by Majorizing a Complicated Function) is an iterative majorization
* algorithm for solving metric multidimensional scaling problems, which aims to
* minimize the stress function.
*
* @class
* @template {InputType} T
* @extends DR
* @category Dimensionality Reduction
* @see {@link MDS} for the classical approach.
*/
declare class SMACOF extends DR {
/**
* @template {InputType} T
* @param {T} X
* @param {Partial} [parameters]
* @returns {T}
*/
static transform(X: T_1, parameters?: Partial): T_1;
/**
* @template {InputType} T
* @param {T} X
* @param {Partial} [parameters]
* @returns {Generator}
*/
static generator