Skip to content

@saehrimnir/druidjs / Matrix

Class: Matrix ​

Defined in: matrix/Matrix.js:13

Constructors ​

Constructor ​

ts
new Matrix(
   rows: number, 
   cols: number, 
   value?: string | number | Accessor): Matrix;

Defined in: matrix/Matrix.js:33

Creates a new Matrix. Entries are stored in a Float64Array.

Parameters ​

ParameterTypeDefault valueDescription
rowsnumberundefinedThe amount of rows of the matrix.
colsnumberundefinedThe amount of columns of the matrix.
valuestring | number | Accessor0Can 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 ​

ts
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 ​

PropertyTypeDefined in
_colsnumbermatrix/Matrix.js:35
_dataFloat64Array<ArrayBufferLike>matrix/Matrix.js:36
_rowsnumbermatrix/Matrix.js:34

Accessors ​

shape ​

Get Signature ​

ts
get shape(): number[];

Defined in: matrix/Matrix.js:945

Returns the number of rows and columns of the Matrix.

Returns ​

number[]

An Array in the form [rows, columns].

Set Signature ​

ts
set shape(parameter: [number, number, Accessor]): void;

Defined in: matrix/Matrix.js:956

Returns the matrix in the given shape with the given function which returns values for the entries of the matrix.

Parameters ​
ParameterTypeDescription
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 ​

ts
get T(): Matrix;

Defined in: matrix/Matrix.js:311

Returns a new transposed Matrix. Short-form of transpose.

Returns ​

Matrix


values ​

Get Signature ​

ts
get values(): Float64Array<ArrayBufferLike>;

Defined in: matrix/Matrix.js:1035

Returns the entries of the Matrix.

Returns ​

Float64Array<ArrayBufferLike>

Methods ​

_apply() ​

ts
_apply(value: number | number[] | Float64Array<ArrayBufferLike> | Matrix, f: (d: number, v: number) => number): Matrix;

Defined in: matrix/Matrix.js:791

Parameters ​

ParameterTypeDescription
valuenumber | number[] | Float64Array<ArrayBufferLike> | Matrix-
f(d: number, v: number) => number-

Returns ​

Matrix


_apply_colwise_array() ​

ts
_apply_colwise_array(values: number[] | Float64Array<ArrayBufferLike>, f: (d: number, v: number) => number): Matrix;

Defined in: matrix/Matrix.js:774

Parameters ​

ParameterTypeDescription
valuesnumber[] | Float64Array<ArrayBufferLike>-
f(d: number, v: number) => number-

Returns ​

Matrix


_apply_rowwise_array() ​

ts
_apply_rowwise_array(values: number[] | Float64Array<ArrayBufferLike>, f: (d: number, v: number) => number): Matrix;

Defined in: matrix/Matrix.js:766

Parameters ​

ParameterTypeDescription
valuesnumber[] | Float64Array<ArrayBufferLike>-
f(d: number, v: number) => number-

Returns ​

Matrix


[iterator]() ​

ts
iterator: Generator<Float64Array<ArrayBufferLike>, void, unknown>;

Defined in: matrix/Matrix.js:180

Makes a Matrix object an iterable object.

Returns ​

Generator<Float64Array<ArrayBufferLike>, void, unknown>

Yields ​


add() ​

ts
add(value: number | number[] | Float64Array<ArrayBufferLike> | Matrix, options?: {
  inline?: boolean;
}): Matrix;

Defined in: matrix/Matrix.js:916

Entrywise addition with value.

Parameters ​

ParameterTypeDescription
valuenumber | number[] | Float64Array<ArrayBufferLike> | Matrix-
options?{ inline?: boolean; }-
options.inline?booleanIf 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 ​

ts
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() ​

ts
add_entry(
   row: number, 
   col: number, 
   value: number): Matrix;

Defined in: matrix/Matrix.js:277

Adds a given value to the colth entry from the rowth row of the Matrix.

Parameters ​

ParameterTypeDescription
rownumber-
colnumber-
valuenumber-

Returns ​

Matrix


asArray() ​

ts
asArray(): number[][];

Defined in: matrix/Matrix.js:985

Returns the Matrix as a Array of Arrays.

Returns ​

number[][]


clone() ​

ts
clone(): Matrix;

Defined in: matrix/Matrix.js:854

Clones the Matrix.

Returns ​

Matrix


col() ​

ts
col(col: number): Float64Array<ArrayBufferLike>;

Defined in: matrix/Matrix.js:235

Returns the colth column from the Matrix.

Parameters ​

ParameterTypeDescription
colnumber-

Returns ​

Float64Array<ArrayBufferLike>


concat() ​

ts
concat(B: Matrix, type?: "horizontal" | "vertical" | "diag"): Matrix;

Defined in: matrix/Matrix.js:627

Appends matrix B to the matrix.

Parameters ​

ParameterTypeDefault valueDescription
BMatrixundefinedMatrix to append.
type?"horizontal" | "vertical" | "diag""horizontal"Type of concatenation. Default is "horizontal"

Returns ​

Matrix

Example ​

ts
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() ​

ts
diag(): Float64Array<ArrayBufferLike>;

Defined in: matrix/Matrix.js:998

Returns the diagonal of the Matrix.

Returns ​

Float64Array<ArrayBufferLike>


divide() ​

ts
divide(value: number | number[] | Float64Array<ArrayBufferLike> | Matrix, options?: {
  inline?: boolean;
}): Matrix;

Defined in: matrix/Matrix.js:897

Entrywise division with value.

Parameters ​

ParameterTypeDescription
valuenumber | number[] | Float64Array<ArrayBufferLike> | Matrix-
options?{ inline?: boolean; }-
options.inline?booleanIf 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 ​

ts
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() ​

ts
dot(B: number[] | Float64Array<ArrayBufferLike> | Matrix): Matrix;

Defined in: matrix/Matrix.js:388

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 ​

ParameterTypeDescription
Bnumber[] | Float64Array<ArrayBufferLike> | MatrixThe right side

Returns ​

Matrix


dotTrans() ​

ts
dotTrans(B: number[] | Float64Array<ArrayBufferLike> | Matrix): Matrix;

Defined in: matrix/Matrix.js:527

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 ​

ParameterTypeDescription
Bnumber[] | Float64Array<ArrayBufferLike> | MatrixThe right side

Returns ​

Matrix


entry() ​

ts
entry(row: number, col: number): number;

Defined in: matrix/Matrix.js:250

Returns the colth entry from the rowth row of the Matrix.

Parameters ​

ParameterTypeDescription
rownumber-
colnumber-

Returns ​

number


gather() ​

ts
gather(row_indices: number[], col_indices: number[]): Matrix;

Defined in: matrix/Matrix.js:724

Returns a new array gathering entries defined by the indices given by argument.

Parameters ​

ParameterTypeDescription
row_indicesnumber[]Array consists of indices of rows for gathering entries of this matrix
col_indicesnumber[]Array consists of indices of cols for gathering entries of this matrix

Returns ​

Matrix


get_block() ​

ts
get_block(
   start_row: number, 
   start_col: number, 
   end_row?: number | null, 
   end_col?: number | null): Matrix;

Defined in: matrix/Matrix.js:698

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 ​

ParameterTypeDescription
start_rownumber-
start_colnumber-
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 ​

ts
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() ​

ts
inverse(): Matrix;

Defined in: matrix/Matrix.js:320

Returns the inverse of the Matrix.

Returns ​

Matrix


iterate_rows() ​

ts
iterate_rows(): Generator<Float64Array<ArrayBufferLike>, void, unknown>;

Defined in: matrix/Matrix.js:166

Returns an generator yielding each row of the Matrix.

Returns ​

Generator<Float64Array<ArrayBufferLike>, void, unknown>

Yields ​


mean() ​

ts
mean(): number;

Defined in: matrix/Matrix.js:1014

Returns the mean of all entries of the Matrix.

Returns ​

number


meanCols() ​

ts
meanCols(): Float64Array<ArrayBufferLike>;

Defined in: matrix/Matrix.js:1065

Returns the mean of each column of the matrix.

Returns ​

Float64Array<ArrayBufferLike>


meanRows() ​

ts
meanRows(): Float64Array<ArrayBufferLike>;

Defined in: matrix/Matrix.js:1045

Returns the mean of each row of the matrix.

Returns ​

Float64Array<ArrayBufferLike>


mult() ​

ts
mult(value: number | number[] | Float64Array<ArrayBufferLike> | Matrix, options?: {
  inline?: boolean;
}): Matrix;

Defined in: matrix/Matrix.js:878

Entrywise multiplication with value.

Parameters ​

ParameterTypeDescription
valuenumber | number[] | Float64Array<ArrayBufferLike> | Matrix-
options?{ inline?: boolean; }-
options.inline?booleanIf 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 ​

ts
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() ​

ts
outer(B: Matrix): Matrix;

Defined in: matrix/Matrix.js:587

Computes the outer product from this and B.

Parameters ​

ParameterTypeDescription
BMatrix-

Returns ​

Matrix


row() ​

ts
row(row: number): Float64Array<ArrayBufferLike>;

Defined in: matrix/Matrix.js:155

Returns the rowth row from the Matrix.

Parameters ​

ParameterTypeDescription
rownumber-

Returns ​

Float64Array<ArrayBufferLike>


set_block() ​

ts
set_block(
   offset_row: number, 
   offset_col: number, 
   B: Matrix): Matrix;

Defined in: matrix/Matrix.js:668

Writes the entries of B in A at an offset position given by offset_row and offset_col.

Parameters ​

ParameterTypeDescription
offset_rownumber-
offset_colnumber-
BMatrix-

Returns ​

Matrix


set_entry() ​

ts
set_entry(
   row: number, 
   col: number, 
   value: number): Matrix;

Defined in: matrix/Matrix.js:263

Sets the colth entry from the rowth row of the Matrix to the given value.

Parameters ​

ParameterTypeDescription
rownumber-
colnumber-
valuenumber-

Returns ​

Matrix


set_row() ​

ts
set_row(row: number, values: number[]): Matrix;

Defined in: matrix/Matrix.js:193

Sets the entries of rowth row from the Matrix to the entries from values.

Parameters ​

ParameterTypeDescription
rownumber-
valuesnumber[]-

Returns ​

Matrix


sub() ​

ts
sub(value: number | number[] | Float64Array<ArrayBufferLike> | Matrix, options?: {
  inline?: boolean;
}): Matrix;

Defined in: matrix/Matrix.js:935

Entrywise subtraction with value.

Parameters ​

ParameterTypeDescription
valuenumber | number[] | Float64Array<ArrayBufferLike> | Matrix-
options?{ inline?: boolean; }-
options.inline?booleanIf 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 ​

ts
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() ​

ts
sub_entry(
   row: number, 
   col: number, 
   value: number): Matrix;

Defined in: matrix/Matrix.js:291

Subtracts a given value from the colth entry from the rowth row of the Matrix.

Parameters ​

ParameterTypeDescription
rownumber-
colnumber-
valuenumber-

Returns ​

Matrix


sum() ​

ts
sum(): number;

Defined in: matrix/Matrix.js:1025

Returns the sum oof all entries of the Matrix.

Returns ​

number


swap_rows() ​

ts
swap_rows(row1: number, row2: number): Matrix;

Defined in: matrix/Matrix.js:218

Swaps the rows row1 and row2 of the Matrix.

Parameters ​

ParameterTypeDescription
row1number-
row2number-

Returns ​

Matrix


to2dArray() ​

ts
to2dArray(): Float64Array<ArrayBufferLike>[];

Defined in: matrix/Matrix.js:972

Returns the Matrix as a Array of Float64Arrays.

Returns ​

Float64Array<ArrayBufferLike>[]


transDot() ​

ts
transDot(B: number[] | Float64Array<ArrayBufferLike> | Matrix): Matrix;

Defined in: matrix/Matrix.js:462

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 ​

ParameterTypeDescription
Bnumber[] | Float64Array<ArrayBufferLike> | MatrixThe right side

Returns ​

Matrix


transpose() ​

ts
transpose(): Matrix;

Defined in: matrix/Matrix.js:301

Returns a new transposed Matrix.

Returns ​

Matrix


det() ​

ts
static det(A: Matrix): number;

Defined in: matrix/Matrix.js:1189

Computes the determinante of A, by using the LU decomposition of A.

Parameters ​

ParameterTypeDescription
AMatrix-

Returns ​

number

The determinate of the Matrix A.


from() ​

ts
static from(A: Matrix | Float64Array<ArrayBufferLike>[] | number[][]): Matrix;

Defined in: matrix/Matrix.js:101

Creates a Matrix out of A.

Parameters ​

ParameterTypeDescription
AMatrix | Float64Array<ArrayBufferLike>[] | number[][]The matrix, array, or number, which should converted to a Matrix.

Returns ​

Matrix

Example ​

ts
let A = Matrix.from([ [1, 0], [0, 1], ]); //creates a two by two identity matrix.

from_diag() ​

ts
static from_diag(v: number[] | Float64Array<ArrayBufferLike>): Matrix;

Defined in: matrix/Matrix.js:126

Creates a Matrix with the diagonal being the values of v.

Parameters ​

ParameterTypeDescription
vnumber[] | Float64Array<ArrayBufferLike>-

Returns ​

Matrix

Example ​

ts
let S = Matrix.from_diag([1, 2, 3]); // creates [[1, 0, 0], [0, 2, 0], [0, 0, 3]]

from_vector() ​

ts
static from_vector(v: number[] | Float64Array<ArrayBufferLike>, type: "col" | "row"): Matrix;

Defined in: matrix/Matrix.js:140

Creates a Matrix with the diagonal being the values of v.

Parameters ​

ParameterTypeDescription
vnumber[] | Float64Array<ArrayBufferLike>-
type"col" | "row"-

Returns ​

Matrix

Example ​

ts
let S = Matrix.from_diag([1, 2, 3]); // creates [[1, 0, 0], [0, 2, 0], [0, 0, 3]]

is2dArray() ​

ts
static is2dArray(A: any[]): A is Float64Array<ArrayBufferLike>[] | number[][];

Defined in: matrix/Matrix.js:1255

Parameters ​

ParameterTypeDescription
Aany[]-

Returns ​

A is Float64Array<ArrayBufferLike>[] | number[][]


isArray() ​

ts
static isArray(A: unknown): A is unknown[] | number[] | Float32Array<ArrayBufferLike> | Float64Array<ArrayBufferLike>;

Defined in: matrix/Matrix.js:1247

Parameters ​

ParameterTypeDescription
Aunknown-

Returns ​

A is unknown[] | number[] | Float32Array<ArrayBufferLike> | Float64Array<ArrayBufferLike>


LU() ​

ts
static LU(A: Matrix): {
  L: Matrix;
  U: Matrix;
};

Defined in: matrix/Matrix.js:1155

LU decomposition of the Matrix A. Creates two matrices, so that the dot product LU equals A.

Parameters ​

ParameterTypeDescription
AMatrix-

Returns ​

ts
{
  L: Matrix;
  U: Matrix;
}

The left triangle matrix L and the upper triangle matrix U.

NameTypeDefined in
LMatrixmatrix/Matrix.js:1153
UMatrixmatrix/Matrix.js:1153

solve() ​

ts
static solve(A: 
  | Matrix
  | {
  L: Matrix;
  U: Matrix;
}, b: Matrix): Matrix;

Defined in: matrix/Matrix.js:1125

Solves the equation Ax = b. Returns the result x.

Parameters ​

ParameterTypeDescription
A| Matrix | { L: Matrix; U: Matrix; }Matrix or LU Decomposition
bMatrixMatrix

Returns ​

Matrix


solve_CG() ​

ts
static solve_CG(
   A: Matrix, 
   b: Matrix, 
   randomizer?: Randomizer | null, 
   tol?: number): Matrix;

Defined in: matrix/Matrix.js:1089

Solves the equation Ax = b using the conjugate gradient method. Returns the result x.

Parameters ​

ParameterTypeDefault valueDescription
AMatrixundefinedMatrix
bMatrixundefinedMatrix
randomizer?Randomizer | nullundefined-
tol?number1e-3Default is 1e-3

Returns ​

Matrix


SVD() ​

ts
static SVD(M: Matrix, k?: number): {
  Sigma: Float64Array;
  U: Float64Array<ArrayBufferLike>[];
  V: Float64Array<ArrayBufferLike>[];
};

Defined in: matrix/Matrix.js:1225

Computes the k components of the SVD decomposition of the matrix M.

Parameters ​

ParameterTypeDefault valueDescription
MMatrixundefined-
k?number2Default is 2

Returns ​

ts
{
  Sigma: Float64Array;
  U: Float64Array<ArrayBufferLike>[];
  V: Float64Array<ArrayBufferLike>[];
}
NameTypeDefined in
SigmaFloat64Arraymatrix/Matrix.js:1223
UFloat64Array<ArrayBufferLike>[]matrix/Matrix.js:1223
VFloat64Array<ArrayBufferLike>[]matrix/Matrix.js:1223