matrix Matrix

new Matrix(rows, cols, value) → {Matrix}

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

Parameters:
NameTypeDefaultDescription
rowsnumbernull

The amount of rows of the matrix.

colsnumbernull

The amount of columns of the matrix.

valuefunction | string | number0

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.
Requires:
  • module:numerical/neumair_sum
Returns:

returns a rows times cols Matrix filled with value.

Type: 
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.

Requires

  • module:numerical/neumair_sum

Members

asArray

Returns the Matrix as a Array of Arrays.

diag

Returns the diagonal of the Matrix.

mean

Returns the mean of all entries of the Matrix.

meanCols

Returns the mean of each column of the matrix.

meanRows

Returns the mean of each row of the matrix.

shape

Returns the number of rows and columns of the Matrix.

shape

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

sum

Returns the sum oof all entries of the Matrix.

T

Returns a new transposed Matrix. Short-form of {@function transpose}.

to2dArray

Returns the Matrix as a Array of Float64Arrays.

values

Returns the entries of the Matrix.

Methods

(static) det(A) → {Number}

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

Parameters:
NameTypeDescription
AMatrix
Returns:

det - Returns the determinate of the Matrix A.

Type: 
Number

(static) from(A, typeopt) → {Matrix}

Creates a Matrix out of A.

Parameters:
NameTypeAttributesDefaultDescription
AMatrix | Array | Float64Array | number

The matrix, array, or number, which should converted to a Matrix.

type"row" | "col" | "diag"<optional>
"row"

If A is a Array or Float64Array, then type defines if it is a row- or a column vector.

Returns:
Type: 
Matrix
Example
let A = Matrix.from([[1, 0], [0, 1]]); //creates a two by two identity matrix.
let S = Matrix.from([1, 2, 3], "diag"); // creates a 3 by 3 matrix with 1, 2, 3 on its diagonal. [[1, 0, 0], [0, 2, 0], [0, 0, 3]]

(static) LU(A) → {Object}

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

Parameters:
NameTypeDescription
AMatrix
Returns:

result - Returns the left triangle matrix L and the upper triangle matrix U.

Type: 
Object

(static) solve(A, b) → {Matrix}

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

Parameters:
NameTypeDescription
AMatrix

Matrix or LU Decomposition

bMatrix

Matrix

Returns:
Type: 
Matrix

(static) solve_CG(A, b, randomizeropt, tolopt) → {Matrix}

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

Parameters:
NameTypeAttributesDefaultDescription
AMatrix

Matrix

bMatrix

Matrix

randomizerRandomizer<optional>
null
tolNumber<optional>
1e-3
Returns:
Type: 
Matrix

(static) SVD(M, kopt) → {Object}

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

Parameters:
NameTypeAttributesDefaultDescription
MMatrix
kint<optional>
2
Returns:
Type: 
Object

add(value, optionsopt) → {Matrix}

Entrywise addition with value.

Parameters:
NameTypeAttributesDescription
valueMatrix | Array | Number
optionsObject<optional>
Properties
NameTypeAttributesDefaultDescription
inlineBoolean<optional>
false

If true, applies addition to the element, otherwise it creates first a copy and applies the addition on the copy.

Returns:
Type: 
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(row, col, value) → {Matrix}

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

Parameters:
NameTypeDescription
rowint
colint
valuefloat64
Returns:
Type: 
Matrix

clone() → {Matrix}

Clones the Matrix.

Returns:
Type: 
Matrix

col(col) → {Array}

Returns the colth column from the Matrix.

Parameters:
NameTypeDescription
colNumber
Returns:
Type: 
Array

concat(B, typeopt) → {Matrix}

Appends matrix B to the matrix.

Parameters:
NameTypeAttributesDefaultDescription
BMatrix

matrix to append.

type"horizontal" | "vertical" | "diag"<optional>
"horizontal"

type of concatenation.

Returns:
Type: 
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]]

divide(value, optionsopt) → {Matrix}

Entrywise division with value.

Parameters:
NameTypeAttributesDescription
valueMatrix | Array | Number
optionsObject<optional>
Properties
NameTypeAttributesDefaultDescription
inlineBoolean<optional>
false

If true, applies division to the element, otherwise it creates first a copy and applies the division on the copy.

Returns:
Type: 
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(B) → {Matrix|Array}

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:
NameTypeDescription
BMatrix | Array | Float64Array

the right side

Returns:
Type: 
Matrix | Array

dotTrans(B) → {Matrix|Array}

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:
NameTypeDescription
BMatrix | Array | Float64Array

the right side

Returns:
Type: 
Matrix | Array

entry(row, col) → {float64}

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

Parameters:
NameTypeDescription
rowint
colint
Returns:
Type: 
float64

gather(row_indices, col_indices) → {Matrix}

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

Parameters:
NameTypeDescription
row_indicesArray.<Number>

Array consists of indices of rows for gathering entries of this matrix

col_indicesArray.<Number>

Array consists of indices of cols for gathering entries of this matrix

Returns:
Type: 
Matrix

get_block(start_row, start_col, end_rowopt, end_colopt) → {Matrix}

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:
NameTypeAttributesDefaultDescription
start_rowNumber
start_colNumber
end_rowNumber<optional>
null
end_colNumber<optional>
null
Returns:

Returns a end_row - start_row times end_col - start_col matrix, with respective entries from the matrix.

Type: 
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() → {Matrix}

Returns the inverse of the Matrix.

Returns:
Type: 
Matrix

(generator) iterate_rows() → {Float64Array}

Returns an generator yielding each row of the Matrix.

Yields:
Type: 
Float64Array

mult(value, optionsopt) → {Matrix}

Entrywise multiplication with value.

Parameters:
NameTypeAttributesDescription
valueMatrix | Array | Number
optionsObject<optional>
Properties
NameTypeAttributesDefaultDescription
inlineBoolean<optional>
false

If true, applies multiplication to the element, otherwise it creates first a copy and applies the multiplication on the copy.

Returns:
Type: 
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(B) → {Matrix}

Computes the outer product from this and B.

Parameters:
NameTypeDescription
BMatrix
Returns:
Type: 
Matrix

row(row) → {Float64Array}

Returns the rowth row from the Matrix.

Parameters:
NameTypeDescription
rowNumber
Returns:
Type: 
Float64Array

set_block(offset_row, offset_col, B) → {Matrix}

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

Parameters:
NameTypeDescription
offset_rowint
offset_colint
BMatrix
Returns:
Type: 
Matrix

set_entry(row, col, value) → {Matrix}

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

Parameters:
NameTypeDescription
rowint
colint
valuefloat64
Returns:
Type: 
Matrix

set_row(row, values) → {Matrix}

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

Parameters:
NameTypeDescription
rowNumber
valuesArray
Returns:
Type: 
Matrix

sub(value, optionsopt) → {Matrix}

Entrywise subtraction with value.

Parameters:
NameTypeAttributesDescription
valueMatrix | Array | Number
optionsObject<optional>
Properties
NameTypeAttributesDefaultDescription
inlineBoolean<optional>
false

If true, applies subtraction to the element, otherwise it creates first a copy and applies the subtraction on the copy.

Returns:
Type: 
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(row, col, value) → {Matrix}

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

Parameters:
NameTypeDescription
rowint
colint
valuefloat64
Returns:
Type: 
Matrix

swap_rows(row1, row2) → {Matrix}

Swaps the rows row1 and row2 of the Matrix.

Parameters:
NameTypeDescription
row1Number
row2Number
Returns:
Type: 
Matrix

transDot(B) → {Matrix|Array}

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:
NameTypeDescription
BMatrix | Array | Float64Array

the right side

Returns:
Type: 
Matrix | Array

transpose() → {Matrix}

Returns a new transposed Matrix.

Returns:
Type: 
Matrix