/** @typedef {(i: number, j: number) => number} Accessor */
/**
* @class
* @category Matrix
*/
export 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>;
}
export type Accessor = (i: number, j: number) => number;
import { Randomizer } from "../util/index.js";
//# sourceMappingURL=Matrix.d.ts.map