Matrix

Matrix

new Matrix(A)

Source:
Creates a new Matrix
Parameters:
Name Type Description
A Array.<Array.<number>> Two dimensional array where A[i][j] represents the i-th row and j-th column of a matrix

Methods

(static) LU(A, optimizedopt) → {Array.<Matrix>}

Source:
Calculates the LUP decomposition of the Matrix, where L is lower triangular matrix which diagonal entries are always 1, U is upper triangular matrix, and P is permutation matrix.

It is implemented using Gaussian Elimination with Partial Pivoting in order to reduce the error caused by floating-point arithmetic.

Note that if optimized is true, P is a Permutation Array and both L and U are merged into one matrix in order to improve performance.
Parameters:
Name Type Attributes Default Description
A Matrix Any matrix
optimized boolean <optional>
false Returns [P, LU] if it is true, [P, L, U] if it is false
Returns:
The LUP decomposition of Matrix
Type
Array.<Matrix>

(static) QR(A) → {Array.<Matrix>}

Source:
Calculates the QR decomposition of the Matrix where Q is orthogonal matrix, R is upper triangular matrix.

The algorithm is implemented using Householder Transform instead of Gram–Schmidt process because the Householder Transform is more numerically stable.
Parameters:
Name Type Description
A Matrix Any matrix
Returns:
The QR decomposition of matrix in the form of [Q, R]
Type
Array.<Matrix>

(static) backward(U, y) → {Matrix}

Source:
Solve system of linear equations Ux = y using backward substitution, where U is an upper triangular matrix. If there is no unique solutions, an error is thrown.
Parameters:
Name Type Description
U Matrix Any n x n upper triangular Matrix
y Matrix Any n x 1 Matrix
Returns:
n x 1 Matrix which is the solution of Ux = y
Type
Matrix

(static) forward(L, y) → {Matrix}

Source:
Solve system of linear equations Lx = y using forward substitution, where L is a lower triangular matrix. If there is no unique solutions, an error is thrown.
Parameters:
Name Type Description
L Matrix Any n x n lower triangular Matrix
y Matrix Any n x 1 Matrix
Returns:
n x 1 Matrix which is the solution of Lx = y
Type
Matrix

(static) solve(L, y) → {Matrix}

Source:
Solve system of linear equations Ax = y using LU decomposition. If there is no unique solutions, an error is thrown.
Parameters:
Name Type Description
L Matrix Any n x n square Matrix
y Matrix Any n x 1 Matrix
Returns:
n x 1 Matrix which is the solution of Ax = y
Type
Matrix

(static) add(A, B) → {Matrix}

Source:
Calculates the sum of two Matrices.
Parameters:
Name Type Description
A Matrix Any Matrix
B Matrix Any Matrix that has same size with A
Returns:
The sum of two Matrices
Type
Matrix

(static) inverse(A) → {Matrix}

Source:
Find the inverse of non-singular matrix using Elementary Row Operations. If the matrix is singular, an error is thrown.
Parameters:
Name Type Description
A Matrix Any square Matrix
Returns:
The inverse of A
Type
Matrix

(static) multiply(A, B) → {Matrix}

Source:
Calculates the product of two Matrices.
Parameters:
Name Type Description
A Matrix Any Matrix
B Matrix Any Matrix that is size-compatible with A
Returns:
The product of two Matrices
Type
Matrix

(static) pow(A, exponent) → {Matrix}

Source:
Calculates the power of any square matrix. The algorithm is implemented recursively.
Parameters:
Name Type Description
A Matrix Any square Matrix
exponent number Any Non-negative integer
Returns:
The power of A
Type
Matrix

(static) transpose(A) → {Matrix}

Source:
Find the transpose of a matrix.
Parameters:
Name Type Description
A Matrix Any Matrix
Returns:
Returns transpose of A
Type
Matrix

cond(p) → {number}

Source:
Calculations the condition number of square Matrix with respect to the choice of Matrix norm. If the Matrix is singular, returns Infinity.

The condition number is not cached.
Parameters:
Name Type Default Description
p 1 | 2 | Infinity | 'F' 2 Type of Matrix norm
Returns:
The condition number of Matrix
Type
number

det() → {number}

Source:
Calculates the determinant of square Matrix. If the Matrix size is larger than 3, it calculates the determinant using LU decomposition, otherwise, using Leibniz Formula.

The determinant is cached.
Returns:
Returns the determinant of square matrirx
Type
number

eigenvalues() → {Array.<Complex>}

Source:
Calculates the eigenvalues of any square Matrix using QR Algorithm.

The eigenvalues can be either real number or complex number. Note that all eigenvalues are instance of Complex, for more details please visit Complex.js.

The eigenvalues are cached.
Returns:
Array of eigenvalues
Type
Array.<Complex>

norm(p) → {number}

Source:
Calculates the Matrix norm of any Matrix with respect to the choice of norm.

1-norm: Maximum absolute column sum of the Matrix.
2-norm: The largest singular value of Matrix.
Infinity-norm: Maximum absolute row sum of the Matrix.
Frobenius-norm: Euclidean norm invloving all entries.

The norms are not cached.
Parameters:
Name Type Default Description
p 1 | 2 | Infinity | 'F' 2 The choice of Matrix norm
Returns:
The norm of the Matrix.
Type
number

nullity() → {number}

Source:
Calculates the nullity of any Matrix, which is the dimension of the nullspace.

The nullity is cached.
Returns:
The nullity of the matrix
Type
number

rank() → {number}

Source:
Calculates the rank of any Matrix, which is the dimension of the row space.

The rank is cached.
Returns:
The rank of the Matrix
Type
number

size() → {Array.<number>}

Source:
Calculates the size of any Matrix, which is in the form of [row, column].

The size of Matrix is cached.
Returns:
The number of rows and columns of a Matrix
Type
Array.<number>

trace() → {number}

Source:
Calculates the trace of any square Matrix, which is the sum of all entries on the main diagonal.

The trace is cached.
Returns:
The trace of the square Matrix.
Type
number

isDiagonal(digitopt) → {boolean}

Source:
Determines whether a Matrix is diagonal or not.

Diagonal Matrix is a Matrix in which the entries outside the main diagonal are all zero. Note that the term diagonal refers to rectangular diagonal.

The result is cached.
Parameters:
Name Type Attributes Default Description
digit number <optional>
8 Number of significant digits
Returns:
Returns rue if the Matrix is diagonal Matrix
Type
boolean

isLowerTriangular(digitopt) → {boolean}

Source:
Determines whether a Matrix is lower triangular Matrix or not.

Lower triangular Matrix is a Matrix in which all the entries above the main diagonal are zero. Note that it can be applied to any non-square Matrix.

The result is cached.
Parameters:
Name Type Attributes Default Description
digit number <optional>
8 Number of significant digits
Returns:
Returns true if the Matrix is lower triangular
Type
boolean

isOrthogonal(digitopt) → {boolean}

Source:
Determines whether a square Matrix is orthogonal or not.

Orthogonal Matrix is a Matrix in which all rows and columns are orthonormal vectors.

The result is cached.
Parameters:
Name Type Attributes Default Description
digit number <optional>
8 Number of significant digits
Returns:
Returns true if the square Matrix is orthogonal
Type
boolean

isSkewSymmetric(digitopt) → {boolean}

Source:
Determines whether a square Matrix is skew symmetric or not.

Skew symmetric Matrix is a square Matrix whose transpose equals its negative.

The result is cached.
Parameters:
Name Type Attributes Default Description
digit number <optional>
8 Number of significant digits
Returns:
Returns true if the square Matrix is skew symmetric
Type
boolean

isSquare() → {boolean}

Source:
Determines whether a Matrix is square or not.

Square Matrix is a Matrix with same number of rows and columns.

The result is cached.
Returns:
Returns true if the Matrix is square
Type
boolean

isSymmetric(digitopt) → {boolean}

Source:
Determines whether a square Matrix is symmetric or not.

Symmetric Matrix is a square Matrix that is equal to its transpose.

The result is cached.
Parameters:
Name Type Attributes Default Description
digit number <optional>
8 Number of significant digits
Returns:
Returns true if the square Matrix is symmetric
Type
boolean

isUpperTriangular(digitopt) → {boolean}

Source:
Determines whether a Matrix is upper triangular Matrix or not.

Upper triangular Matrix is a Matrix in which all the entries below the main diagonal are zero. Note that it can be applied to any non-square Matrix.

The result is cached.
Parameters:
Name Type Attributes Default Description
digit number <optional>
8 Number of significant digits
Returns:
Returns true if the Matrix is upper triangular
Type
boolean

(static) clone(A) → {Matrix}

Source:
Creates a copy of Matrix. Note that it resets the cached data.
Parameters:
Name Type Description
A Matrix Any Matrix
Returns:
Copy of A
Type
Matrix

(static) column(A, index) → {Matrix}

Source:
Gets the column of a Matrix with valid index.
Parameters:
Name Type Description
A Matrix Any Matrix
index number Any valid column index
Returns:
Column of A
Type
Matrix

(static) diag(values) → {Matrix}

Source:
Generates diagonal Matrix if the argument is an array of numbers, generates block diagonal Matrix if the argument is an array of Matrices.
Parameters:
Name Type Description
values Array.<number> | Array.<Matrix> Array of numbers or Matrices
Returns:
Block diagonal Matrix
Type
Matrix

(static) elementwise(A, cb) → {Matrix}

Source:
Applys a function over each entry of a Matrix and returns a new copy of the new Matrix.
Parameters:
Name Type Description
A Matrix Any Matrix
cb entryCallback Callback function which applies on each entry of A
Returns:
A copy of new Matrix
Type
Matrix

entry(row, col) → {number}

Source:
Gets the entry of a Matrix.
Parameters:
Name Type Description
row number Any valid row index
col number Any valid column index
Returns:
Entry of the Matrix
Type
number

(static) fromArray(arr, row, col) → {Matrix}

Source:
Generate a matrix from an array with compatible dimensions
Parameters:
Name Type Description
arr Array Source array
row number Row of the matrix
col number Column of the matrix
Returns:
Matrix
Type
Matrix

(static) generate(row, col, cb) → {Matrix}

Source:
Generates a Matrix which entries are the returned value of callback function.
Parameters:
Name Type Description
row number Number of rows of Matrix
col number Number of columns of Matrix
cb generateCallback Callback function which takes row and column as arguments and generates the corresponding entry
Returns:
- Generated Matrix
Type
Matrix

(static) getDiag(A) → {Array.<number>}

Source:
Gets the entries on the main diagonal.
Parameters:
Name Type Description
A Matrix Any Matrix
Returns:
Array of entries of A on the main diagonal
Type
Array.<number>

(static) getRandomMatrix(row, col, min, max, toFixed) → {Matrix}

Source:
Generates a random Matrix.
Parameters:
Name Type Default Description
row number Number of rows of a Matrix
col number Number of columns of a Matrix
min number 0 Lower bound of each entry
max number 1 Upper bound of each entry
toFixed number 0 Number of decimal places
Returns:
Generated random Matrix
Type
Matrix

(static) identity(size) → {Matrix}

Source:
Generates identity Matrix with given size.
Parameters:
Name Type Description
size number The size of Matrix
Returns:
Identity Matrix
Type
Matrix

(static) isEqual(A, B, digit) → {boolean}

Source:
Determines whether two Matrices are considered as equal.

The test criterion is Math.abs(x - y) < 1 / (10 ** digit * 2). For default value 5, it should be 5e-5. That means if the difference of two numbers is less than 5e-5, they are considered as same value.
Parameters:
Name Type Default Description
A Matrix Any Matrix
B Matrix Any Matrix
digit number 5 Number of significant digits
Returns:
Returns true if two Matrices are considered as same
Type
boolean

(static) row(A, index) → {Matrix}

Source:
Gets the row of a Matrix with valid index.
Parameters:
Name Type Description
A Matrix Any Matrix
index number Any valid row index
Returns:
Row of A
Type
Matrix

(static) submatrix(A, rows, cols) → {Matrix}

Source:
Generates a submatrix of a matrix.
Parameters:
Name Type Description
A Matrix Any matrix
rows string | number Rows expression
cols string | number Columns expression
Returns:
Submatrix of A
Type
Matrix

toString() → {string}

Source:
Gets the stringified Matrix
Returns:
Stringified Matrix
Type
string

(static) zero(row, col) → {Matrix}

Source:
Generates a zero Matrix
Parameters:
Name Type Description
row number Number of rows of the Matrix
col number Number of columns of the Matrix
Returns:
Zero Matrix
Type
Matrix