@saehrimnir/druidjs / Matrix
Class: Matrix
Defined in: matrix/Matrix.js:11
Constructors
Constructor
new Matrix(
rows: number,
cols: number,
value?: string | number | Accessor): Matrix;Defined in: matrix/Matrix.js:31
Creates a new Matrix. Entries are stored in a Float64Array.
Parameters
| Parameter | Type | Default value | Description |
|---|---|---|---|
rows | number | undefined | The amount of rows of the matrix. |
cols | number | undefined | The amount of columns of the matrix. |
value | string | number | Accessor | 0 | 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. |
Returns
Matrix
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.Properties
| Property | Type | Defined in |
|---|---|---|
_cols | number | matrix/Matrix.js:33 |
_data | Float64Array<ArrayBufferLike> | matrix/Matrix.js:34 |
_rows | number | matrix/Matrix.js:32 |
Accessors
shape
Get Signature
get shape(): number[];Defined in: matrix/Matrix.js:879
Returns the number of rows and columns of the Matrix.
Returns
number[]
An Array in the form [rows, columns].
Set Signature
set shape(parameter: [number, number, Accessor]): void;Defined in: matrix/Matrix.js:891
Returns the matrix in the given shape with the given function which returns values for the entries of the matrix.
Parameters
| Parameter | Type | Description |
|---|---|---|
parameter | [number, number, Accessor] | 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
void
T
Get Signature
get T(): Matrix;Defined in: matrix/Matrix.js:309
Returns a new transposed Matrix. Short-form of transpose.
Returns
Matrix
values
Get Signature
get values(): Float64Array<ArrayBufferLike>;Defined in: matrix/Matrix.js:970
Returns the entries of the Matrix.
Returns
Float64Array<ArrayBufferLike>
Methods
_apply()
_apply(value: number | number[] | Float64Array<ArrayBufferLike> | Matrix, f: (d: number, v: number) => number): Matrix;Defined in: matrix/Matrix.js:725
Parameters
| Parameter | Type | Description |
|---|---|---|
value | number | number[] | Float64Array<ArrayBufferLike> | Matrix | - |
f | (d: number, v: number) => number | - |
Returns
Matrix
_apply_colwise_array()
_apply_colwise_array(values: number[] | Float64Array<ArrayBufferLike>, f: (d: number, v: number) => number): Matrix;Defined in: matrix/Matrix.js:708
Parameters
| Parameter | Type | Description |
|---|---|---|
values | number[] | Float64Array<ArrayBufferLike> | - |
f | (d: number, v: number) => number | - |
Returns
Matrix
_apply_rowwise_array()
_apply_rowwise_array(values: number[] | Float64Array<ArrayBufferLike>, f: (d: number, v: number) => number): Matrix;Defined in: matrix/Matrix.js:700
Parameters
| Parameter | Type | Description |
|---|---|---|
values | number[] | Float64Array<ArrayBufferLike> | - |
f | (d: number, v: number) => number | - |
Returns
Matrix
[iterator]()
iterator: Generator<Float64Array<ArrayBufferLike>, void, unknown>;Defined in: matrix/Matrix.js:178
Makes a Matrix object an iterable object.
Returns
Generator<Float64Array<ArrayBufferLike>, void, unknown>
Yields
add()
add(value: number | number[] | Float64Array<ArrayBufferLike> | Matrix, options?: {
inline?: boolean;
}): Matrix;Defined in: matrix/Matrix.js:850
Entrywise addition with value.
Parameters
| Parameter | Type | Description |
|---|---|---|
value | number | number[] | Float64Array<ArrayBufferLike> | Matrix | - |
options? | { inline?: boolean; } | - |
options.inline? | boolean | 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
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]];add_entry()
add_entry(
row: number,
col: number,
value: number): Matrix;Defined in: matrix/Matrix.js:275
Adds a given value to the colth entry from the rowth row of the Matrix.
Parameters
| Parameter | Type | Description |
|---|---|---|
row | number | - |
col | number | - |
value | number | - |
Returns
Matrix
asArray()
asArray(): number[][];Defined in: matrix/Matrix.js:920
Returns the Matrix as a Array of Arrays.
Returns
number[][]
clone()
clone(): Matrix;Defined in: matrix/Matrix.js:788
Clones the Matrix.
Returns
Matrix
col()
col(col: number): Float64Array<ArrayBufferLike>;Defined in: matrix/Matrix.js:233
Returns the colth column from the Matrix.
Parameters
| Parameter | Type | Description |
|---|---|---|
col | number | - |
Returns
Float64Array<ArrayBufferLike>
concat()
concat(B: Matrix, type?: "horizontal" | "vertical" | "diag"): Matrix;Defined in: matrix/Matrix.js:561
Appends matrix B to the matrix.
Parameters
| Parameter | Type | Default value | Description |
|---|---|---|---|
B | Matrix | undefined | Matrix to append. |
type? | "horizontal" | "vertical" | "diag" | "horizontal" | Type of concatenation. Default is "horizontal" |
Returns
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]]diag()
diag(): Float64Array<ArrayBufferLike>;Defined in: matrix/Matrix.js:933
Returns the diagonal of the Matrix.
Returns
Float64Array<ArrayBufferLike>
divide()
divide(value: number | number[] | Float64Array<ArrayBufferLike> | Matrix, options?: {
inline?: boolean;
}): Matrix;Defined in: matrix/Matrix.js:831
Entrywise division with value.
Parameters
| Parameter | Type | Description |
|---|---|---|
value | number | number[] | Float64Array<ArrayBufferLike> | Matrix | - |
options? | { inline?: boolean; } | - |
options.inline? | boolean | 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
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]];dot()
dot(B: number[] | Float64Array<ArrayBufferLike> | Matrix): Matrix;Defined in: matrix/Matrix.js:386
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.
Parameters
| Parameter | Type | Description |
|---|---|---|
B | number[] | Float64Array<ArrayBufferLike> | Matrix | The right side |
Returns
Matrix
dotTrans()
dotTrans(B: number[] | Float64Array<ArrayBufferLike> | Matrix): Matrix;Defined in: matrix/Matrix.js:487
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.
Parameters
| Parameter | Type | Description |
|---|---|---|
B | number[] | Float64Array<ArrayBufferLike> | Matrix | The right side |
Returns
Matrix
entry()
entry(row: number, col: number): number;Defined in: matrix/Matrix.js:248
Returns the colth entry from the rowth row of the Matrix.
Parameters
| Parameter | Type | Description |
|---|---|---|
row | number | - |
col | number | - |
Returns
number
gather()
gather(row_indices: number[], col_indices: number[]): Matrix;Defined in: matrix/Matrix.js:658
Returns a new array gathering entries defined by the indices given by argument.
Parameters
| Parameter | Type | Description |
|---|---|---|
row_indices | number[] | Array consists of indices of rows for gathering entries of this matrix |
col_indices | number[] | Array consists of indices of cols for gathering entries of this matrix |
Returns
Matrix
get_block()
get_block(
start_row: number,
start_col: number,
end_row?: number | null,
end_col?: number | null): Matrix;Defined in: matrix/Matrix.js:632
Extracts the entries from the start_rowth row to the end_rowth row, the start_colth column to the end_colth column of the matrix. If end_row or end_col is empty, the respective value is set to this.rows or this.cols.
Parameters
| Parameter | Type | Description |
|---|---|---|
start_row | number | - |
start_col | number | - |
end_row? | number | null | - |
end_col? | number | null | - |
Returns
Matrix
Returns a end_row - start_row times end_col - start_col matrix, with respective entries from the matrix.
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]]inverse()
inverse(): Matrix;Defined in: matrix/Matrix.js:318
Returns the inverse of the Matrix.
Returns
Matrix
iterate_rows()
iterate_rows(): Generator<Float64Array<ArrayBufferLike>, void, unknown>;Defined in: matrix/Matrix.js:164
Returns an generator yielding each row of the Matrix.
Returns
Generator<Float64Array<ArrayBufferLike>, void, unknown>
Yields
mean()
mean(): number;Defined in: matrix/Matrix.js:949
Returns the mean of all entries of the Matrix.
Returns
number
meanCols()
meanCols(): Float64Array<ArrayBufferLike>;Defined in: matrix/Matrix.js:1000
Returns the mean of each column of the matrix.
Returns
Float64Array<ArrayBufferLike>
meanRows()
meanRows(): Float64Array<ArrayBufferLike>;Defined in: matrix/Matrix.js:980
Returns the mean of each row of the matrix.
Returns
Float64Array<ArrayBufferLike>
mult()
mult(value: number | number[] | Float64Array<ArrayBufferLike> | Matrix, options?: {
inline?: boolean;
}): Matrix;Defined in: matrix/Matrix.js:812
Entrywise multiplication with value.
Parameters
| Parameter | Type | Description |
|---|---|---|
value | number | number[] | Float64Array<ArrayBufferLike> | Matrix | - |
options? | { inline?: boolean; } | - |
options.inline? | boolean | 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
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]];outer()
outer(B: Matrix): Matrix;Defined in: matrix/Matrix.js:527
Computes the outer product from this and B.
Parameters
| Parameter | Type | Description |
|---|---|---|
B | Matrix | - |
Returns
Matrix
row()
row(row: number): Float64Array<ArrayBufferLike>;Defined in: matrix/Matrix.js:153
Returns the rowth row from the Matrix.
Parameters
| Parameter | Type | Description |
|---|---|---|
row | number | - |
Returns
Float64Array<ArrayBufferLike>
set_block()
set_block(
offset_row: number,
offset_col: number,
B: Matrix): Matrix;Defined in: matrix/Matrix.js:602
Writes the entries of B in A at an offset position given by offset_row and offset_col.
Parameters
| Parameter | Type | Description |
|---|---|---|
offset_row | number | - |
offset_col | number | - |
B | Matrix | - |
Returns
Matrix
set_entry()
set_entry(
row: number,
col: number,
value: number): Matrix;Defined in: matrix/Matrix.js:261
Sets the colth entry from the rowth row of the Matrix to the given value.
Parameters
| Parameter | Type | Description |
|---|---|---|
row | number | - |
col | number | - |
value | number | - |
Returns
Matrix
set_row()
set_row(row: number, values: number[]): Matrix;Defined in: matrix/Matrix.js:191
Sets the entries of rowth row from the Matrix to the entries from values.
Parameters
| Parameter | Type | Description |
|---|---|---|
row | number | - |
values | number[] | - |
Returns
Matrix
sub()
sub(value: number | number[] | Float64Array<ArrayBufferLike> | Matrix, options?: {
inline?: boolean;
}): Matrix;Defined in: matrix/Matrix.js:869
Entrywise subtraction with value.
Parameters
| Parameter | Type | Description |
|---|---|---|
value | number | number[] | Float64Array<ArrayBufferLike> | Matrix | - |
options? | { inline?: boolean; } | - |
options.inline? | boolean | 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
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]];sub_entry()
sub_entry(
row: number,
col: number,
value: number): Matrix;Defined in: matrix/Matrix.js:289
Subtracts a given value from the colth entry from the rowth row of the Matrix.
Parameters
| Parameter | Type | Description |
|---|---|---|
row | number | - |
col | number | - |
value | number | - |
Returns
Matrix
sum()
sum(): number;Defined in: matrix/Matrix.js:960
Returns the sum oof all entries of the Matrix.
Returns
number
swap_rows()
swap_rows(row1: number, row2: number): Matrix;Defined in: matrix/Matrix.js:216
Swaps the rows row1 and row2 of the Matrix.
Parameters
| Parameter | Type | Description |
|---|---|---|
row1 | number | - |
row2 | number | - |
Returns
Matrix
to2dArray()
to2dArray(): Float64Array<ArrayBufferLike>[];Defined in: matrix/Matrix.js:907
Returns the Matrix as a Array of Float64Arrays.
Returns
Float64Array<ArrayBufferLike>[]
transDot()
transDot(B: number[] | Float64Array<ArrayBufferLike> | Matrix): Matrix;Defined in: matrix/Matrix.js:436
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.
Parameters
| Parameter | Type | Description |
|---|---|---|
B | number[] | Float64Array<ArrayBufferLike> | Matrix | The right side |
Returns
Matrix
transpose()
transpose(): Matrix;Defined in: matrix/Matrix.js:299
Returns a new transposed Matrix.
Returns
Matrix
det()
static det(A: Matrix): number;Defined in: matrix/Matrix.js:1124
Computes the determinante of A, by using the LU decomposition of A.
Parameters
| Parameter | Type | Description |
|---|---|---|
A | Matrix | - |
Returns
number
The determinate of the Matrix A.
from()
static from(A: Matrix | Float64Array<ArrayBufferLike>[] | number[][]): Matrix;Defined in: matrix/Matrix.js:99
Creates a Matrix out of A.
Parameters
| Parameter | Type | Description |
|---|---|---|
A | Matrix | Float64Array<ArrayBufferLike>[] | number[][] | 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.from_diag()
static from_diag(v: number[] | Float64Array<ArrayBufferLike>): Matrix;Defined in: matrix/Matrix.js:124
Creates a Matrix with the diagonal being the values of v.
Parameters
| Parameter | Type | Description |
|---|---|---|
v | number[] | Float64Array<ArrayBufferLike> | - |
Returns
Matrix
Example
let S = Matrix.from_diag([1, 2, 3]); // creates [[1, 0, 0], [0, 2, 0], [0, 0, 3]]from_vector()
static from_vector(v: number[] | Float64Array<ArrayBufferLike>, type: "col" | "row"): Matrix;Defined in: matrix/Matrix.js:138
Creates a Matrix with the diagonal being the values of v.
Parameters
| Parameter | Type | Description |
|---|---|---|
v | number[] | Float64Array<ArrayBufferLike> | - |
type | "col" | "row" | - |
Returns
Matrix
Example
let S = Matrix.from_diag([1, 2, 3]); // creates [[1, 0, 0], [0, 2, 0], [0, 0, 3]]is2dArray()
static is2dArray(A: any[]): A is Float64Array<ArrayBufferLike>[] | number[][];Defined in: matrix/Matrix.js:1190
Parameters
| Parameter | Type | Description |
|---|---|---|
A | any[] | - |
Returns
A is Float64Array<ArrayBufferLike>[] | number[][]
isArray()
static isArray(A: unknown): A is unknown[] | number[] | Float32Array<ArrayBufferLike> | Float64Array<ArrayBufferLike>;Defined in: matrix/Matrix.js:1182
Parameters
| Parameter | Type | Description |
|---|---|---|
A | unknown | - |
Returns
A is unknown[] | number[] | Float32Array<ArrayBufferLike> | Float64Array<ArrayBufferLike>
LU()
static LU(A: Matrix): {
L: Matrix;
U: Matrix;
};Defined in: matrix/Matrix.js:1090
LU decomposition of the Matrix A. Creates two matrices, so that the dot product LU equals A.
Parameters
| Parameter | Type | Description |
|---|---|---|
A | Matrix | - |
Returns
{
L: Matrix;
U: Matrix;
}The left triangle matrix L and the upper triangle matrix U.
| Name | Type | Defined in |
|---|---|---|
L | Matrix | matrix/Matrix.js:1088 |
U | Matrix | matrix/Matrix.js:1088 |
solve()
static solve(A:
| Matrix
| {
L: Matrix;
U: Matrix;
}, b: Matrix): Matrix;Defined in: matrix/Matrix.js:1060
Solves the equation Ax = b. Returns the result x.
Parameters
| Parameter | Type | Description |
|---|---|---|
A | | Matrix | { L: Matrix; U: Matrix; } | Matrix or LU Decomposition |
b | Matrix | Matrix |
Returns
Matrix
solve_CG()
static solve_CG(
A: Matrix,
b: Matrix,
randomizer?: Randomizer | null,
tol?: number): Matrix;Defined in: matrix/Matrix.js:1024
Solves the equation Ax = b using the conjugate gradient method. Returns the result x.
Parameters
| Parameter | Type | Default value | Description |
|---|---|---|---|
A | Matrix | undefined | Matrix |
b | Matrix | undefined | Matrix |
randomizer? | Randomizer | null | undefined | - |
tol? | number | 1e-3 | Default is 1e-3 |
Returns
Matrix
SVD()
static SVD(M: Matrix, k?: number): {
Sigma: Float64Array;
U: Float64Array<ArrayBufferLike>[];
V: Float64Array<ArrayBufferLike>[];
};Defined in: matrix/Matrix.js:1160
Computes the k components of the SVD decomposition of the matrix M.
Parameters
| Parameter | Type | Default value | Description |
|---|---|---|---|
M | Matrix | undefined | - |
k? | number | 2 | Default is 2 |
Returns
{
Sigma: Float64Array;
U: Float64Array<ArrayBufferLike>[];
V: Float64Array<ArrayBufferLike>[];
}| Name | Type | Defined in |
|---|---|---|
Sigma | Float64Array | matrix/Matrix.js:1158 |
U | Float64Array<ArrayBufferLike>[] | matrix/Matrix.js:1158 |
V | Float64Array<ArrayBufferLike>[] | matrix/Matrix.js:1158 |