The type of elements contained in the matrix.
Constructs a matrix object.
The entries of the matrix.
Optional
options: { The options for the matrix class
Optional
columns?: numberThe number of columns in the matrix.
Optional
numerical?: Numerical<T>The Numerical class that controls the operations for the matrix.
Optional
rows?: numberThe number of rows in the matrix.
// Create a new instance of Matrix with number type and 2 rows and 2 columns
const matrix: Matrix<number> = new Matrix([[1, 2], [3, 4]]);
// Create a new instance of Matrix with bigint type and 2 rows and 2 columns
const matrix2: Matrix<bigint> = new Matrix([BigInt(1), BigInt(2), BigInt(3), BigInt(4)], { rows: 2, columns: 2});
// Create a new instance of Matrix with a custom type and 2 rows and 2 columns
const matrix3: Matrix<ctype> = new Matrix([c1, c2], [c3, c4], { rows: 2, columns: 2, numerical: "The custom class that implements the Numerical interface goes here" });
If the entries are not an array, or if the entries are a 1D array but rows and columns are not defined or not numbers or are 0 or negative, or if the length of the 1D array does not equal rows * columns, or if the entries are a 2D array but not all rows have the same length, or if the matrix has a depth greater than one.
If the matrix datatype is neither a number nor a bigint and no appropriate Numeric implementation was provided.
The number of columns in the matrix.
The data type of the matrix elements.
Indicates whether the matrix is square.
Indicates whether the matrix is tall (more rows than columns).
Indicates whether the matrix is wide (more columns than rows).
The elements of the matrix.
The numercal class to use for calculations
The number of rows in the matrix.
The shape of the matrix.
The total number of elements in the matrix.
Performs LUP decomposition on the matrix with partial pivoting. This method does not modify the original matrix.
An object containing the L, U, and P matrices.
If the matrix is not square or is singular.
const matrix: Matrix<number> = new Matrix([[2, -1, 3], [4, 3, -1], [-2, 2, 1]]);
const result: { L: Matrix<number>, U: Matrix<number>, P: Matrix<number>, permutationCount: number } = matrix.LUPDecomposition();
console.log(`L Matrix:\n ${result.L.toString()}`);
console.log(`U Matrix:\n ${result.U.toString()}`);
console.log(`P Matrix:\n ${result.P.toString()}`);
// Output:
L Matrix:
" 1.00 0.00 0.00"
"-0.50 1.00 0.00"
" 0.50 -0.71 1.00"
U Matrix:
"4.00 3.00 -1.00"
"0.00 3.50 0.50"
"0.00 0.00 3.86"
P Matrix:
"0.00 1.00 0.00"
"0.00 0.00 1.00"
"1.00 0.00 0.00"
Performs QR decomposition on the matrix. This method does not modify the original matrix.
An object containing the Q and R matrices.
const matrix: Matrix<number> = new Matrix([[12, -51, 4], [6, 167, -68], [-4, 24, -41]]);
const result: { Q: Matrix<number>, R: Matrix<number> } = matrix.QRDecomposition();
console.log(`Q Matrix:\n ${result.Q.toString()}`);
console.log(`R Matrix:\n ${result.R.toString()}`);
// Output:
Q Matrix:
" 0.86 -0.39 0.33"
" 0.43 0.90 -0.03"
"-0.29 0.17 -0.94"
R Matrix:
"14.00 21.00 -14.00"
"0.00 175.00 -70.00"
"0.00 0.00 35.00"
Returns a new matrix where each element is the absolute value of the corresponding element in the original matrix.
A new matrix with absolute values of the elements.
const matrix: Matrix<number> = new Matrix<number>([[-2, 4], [-6, 8]]);
const absoluteMatrix: Matrix<number> = matrix.abs();
console.log(absoluteMatrix.toString());
// Output:
"2 4"
"6 8"
Adds another matrix to this matrix.
The matrix to add.
The resulting matrix.
If the dimensions of the two matrices are not the same.
If the argument is not an instance of Matrix.
const matrixA: Matrix<number> = new Matrix([[1, 2], [3, 4]]);
const matrixB: Matrix<number> = new Matrix([[5, 6], [7, 8]]);
const resultMatrix: Matrix<number> = matrixA.add(matrixB);
console.log(resultMatrix.toString());
// Output:
"6 8"
"10 12"
Computes the adjugate of the matrix.
The adjugate matrix
If the matrix is not square.
const matrix:Matrix<number> = new Matrix<number>([[1, 2], [3, 4]]);
const adjugateMatrix:Matrix<number> = matrix.adjugate();
console.log(adjugateMatrix.toString());
// Output:
" 4 -2"
"-3 1"
Augments the current matrix with another matrix.
The matrix to be augmented with.
A new matrix that is the result of augmenting the current matrix with the provided matrix.
If the argument is not an instance of Matrix, or if the current matrix and the provided matrix do not have the same number of rows.
const matrixA: Matrix<number> = new Matrix([[1, 2], [3, 4]]);
const matrixB: Matrix<number> = new Matrix([[5, 6], [7, 8]]);
const resultMatrix: Matrix<number> = matrixA.augment(matrixB);
console.log(resultMatrix.toString());
// Output:
"1 2 5 6"
"3 4 7 8"
Performs back-substitution on an upper triangular matrix to solve a system of linear equations.
The right-hand side of the system of linear equations.
Solution to the system of linear equations.
If the matrix is not upper triangular, if b is not an array, or if the matrix contains a zero on the diagonal (unsolvable system).
const matrix: Matrix<number> = new Matrix([[1, 2], [0, 1]]);
const b: number[] = [8, 5];
const solution: number[] = matrix.backSubstitution(b);
console.log(solution);
// Output: [-2, 5]
Computes the cofactor of an element given a row and column
The cofactor of the element
If the matrix is not square.
const matrix: Matix<number> = new Matrix([[1, 4, 7], [3, 0, 5], [-1, 9, 11]]);
const cofactor: number = matrix.cofactor(1,2);
console.log(cofactor);
// Output: -13
Computes the cofactor matrix.
The cofactor matrix
If the matrix is not square.
const matrix: Matrix<number> = new Matrix([[1, 2, 3], [0, 4, 5], [1, 0, 6]]);
const cofactorMatrix: Matrix<number> = matrix.cofactorMatrix();
console.log(cofactorMatrix.toString());
// Output:
" 24 5 -4"
"-12 3 2"
"-2 -5 -4"
Computes the condition number of this matrix.
The condition number of the matrix.
If the matrix is not square.
const matrix: Matrix<number> = new Matrix([[1, 2], [3, 4]]);
const conditionNumber: number = matrix.conditionNumber();
console.log(conditionNumber);
// Output: 14.933034373659268
Computes the determinant of this matrix.
The determinant of the matrix.
If the matrix is not square.
If the matrix is singular and LU decomposition cannot be performed.
const matrix: Matrix<number> = new Matrix([[1, 2], [3, 4]]);
const determinant: number = matrix.det();
console.log(determinant);
// Output: -2
Returns the diagonal elements of the matrix.
The offset from the main diagonal. Default is 0 which represents the main diagonal.
An array of the diagonal elements.
If the absolute value of k is greater than or equal to the number of rows for a square matrix, or if k is greater than or equal to the number of columns or the absolute value of k is greater than or equal to the number of rows for a wide or tall matrix.
const matrix: Matrix<number> = new Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]]);
const mainDiag: number[] = matrix.diag();
const fistDiag: number[] = matrix.diag(1);
console.log(mainDiag);
// Output: [1, 5, 9]
console.log(fistDiag);
// Output: [2,6]
Checks if the current matrix is equal to another matrix.
The matrix to compare with.
Threshold for the method to return true defaults to 1e-12
'true' if the matrices are equal, 'false' otherwise.
const matrixA: Matrix<number> = new Matrix([[1, 2], [3, 4]]);
const matrixB: Matrix<number> = new Matrix([[1, 2], [3, 4]]);
const isEqual: boolean = matrixA.equal(matrixB);
console.log(isEqual);
// Output: true
Performs forward-substitution on a lower triangular matrix to solve a system of linear equations.
The right-hand side of the system of linear equations.
Solution to the system of linear equations.
If the matrix is not lower triangular, if b is not an array, or if the matrix contains a zero on the diagonal (unsolvable system).
const matrix: Matrix<number> = new Matrix([[1, 0], [2, 3]]);
const b: number[] = [4, 5];
const solution: number[] = matrix.forwardSubstitution(b);
console.log(solution);
// Output: [4, -1]
Calculates the Fourier transformation of a matrix.
The options for the Fourier transform.
The method to use for the Fourier transform ("dft" or "fft"). Default is "fft".
The sequence in which the Fourier transform is applied ("row" or "column"). Default is "row".
The Fourier transformed matrix.
Throws an error if the matrix data type is not "number" or if the input sequences are not a power of two (only for "dft" method).
const matrix: Matrix<number> = new Matrix([[1, 2], [3, 4]]);
const result: Matrix<ComplexNumber> = matrix.fourier({ method: "fft", sequence: "row" });
console.log(result.toString());
// Output:
"3 + 0i -1 + 0i"
"7 + 0i -1 + 0i"
Converts the matrix to Reduced Row Echelon Form (RREF) using the Gauss-Jordan elimination method. This method does not modify the original matrix.
The options for the Gauss-Jordan elimination.
Optional
solve?: booleanIndicates whether to solve the system of equations. Default is false.
options.solve
is true.const matrix: Matrix<number> = new Matrix([[3, 2, 1], [4, 2, 2], [5, 4, 4]]);
const resultMatrix: Matrix<number> = matrix.gaussJordan();
console.log(resultMatrix.toString());
// Output:
"1 0 0"
"0 1 0"
"0 0 1"
Converts the matrix to Row Echelon Form (REF) using the Gaussian elimination method. This method does not modify the original matrix.
The options for Gaussian elimination.
Optional
solve?: booleanIndicates whether to solve the system of equations. Default is false.
options.solve
is true.const matrix: Matrix<number> = new Matrix([[3, 2, 1], [4, 2, 2], [5, 4, 4]]);
const resultMatrix: Matrix<number> = matrix.gaussianElimination();
console.log(resultMatrix.toString());
// Output:
"1 4 1"
"0 -6 0"
"0 0 -1"
Gets a specific column of the matrix.
The index of the column to retrieve.
An array representing the specified column of the matrix.
If the columnIndex is out of bounds.
const matrix: Matrix<number> = new Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]]);
const column: Number[] = matrix.getColumn(1);
console.log(column);
// Output: [2, 5, 8]
Gets the value of an element in the matrix.
The row index of the element zero indexed.
The column index of the element zero indexed.
The value of the element.
const matrix: Matrix<number> = new Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]]);
const value: number = matrix.getElement(1, 2);
console.log(value);
// Output: 6
Private
getGets a specific row of the matrix.
The index of the row to retrieve (starting from 0).
An array representing the specified row of the matrix.
If the rowIndex is out of bounds.
const matrix: Matrix<number> = new Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]]);
const row: number[] = matrix.getRow(2);
console.log(row);
// Output: [7, 8, 9]
Retrieves a submatrix from the current matrix.
The starting row index of the submatrix.
The ending row index of the submatrix (inclusive).
The starting column index of the submatrix.
The ending column index of the submatrix (inclusive).
A new Matrix object representing the submatrix.
const matrix: Matrix<number> = new Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]]);
const subMatrix: Matrix<number> = matrix.getSubMatrix(1, 2, 1, 2);
console.log(subMatrix.toString());
// Output:
"5 6"
"8 9"
Private
getPerforms the Gram-Schmidt process for the columns of the given matrix. The process is an algorithm to orthonormalize a set of vectors in an inner product space, generally Euclidean n-space.
The method takes the columns (considered as vectors) of the current matrix instance and generates an orthogonal set of vectors that spans the same column space as the original set. The set of orthonormal vectors is computed sequentially by subtracting the projections of a matrix column vector onto the previously computed orthogonal vectors from the column vector itself.
A new Matrix instance constructed using the orthonormal vectors as columns.
If any column obtained during the process is nearly zero (having euclidean norm lesser than a small
constant - DELTA
). In this case, this means that the provided set is not linearly independent.
const matrix: Matrix<number> = new Matrix([[1, 1, 1], [0, 1, 0], [0, 0, 1]]);
const resultMatrixx: Matrix<number> = matrix.gramSmith();
console.log(resultMatrix.toString());
// Output:
"1 0 0"
"0 1 0"
"0 0 1"
Calculates the infinity norm of the matrix, which is the maximum value of the sum of absolute values in each row.
The infinity norm of the matrix.
const matrix: Matrix<number> = new Matrix([[1, 2], [-3, 4], [-5, -6]]);
const infNorm: number = matrix.infNorm();
console.log(infNorm);
// Output: 11
Inverts a lower triangular matrix.
The inverted lower triangular matrix.
If the original matrix is not square or a lower triangular matrix , an error is thrown.
const matrix = new Matrix([[1, 0], [2, 1]]);
const resultMatrix = matrix.invertLower();
console.log(resultMatrix.toString());
// Output:
" 1 0"
"-2 1"
Inverts a square matrix.
The inverse of the square matrix.
If the matrix is not square.
const matrix: Matrix<number> = new Matrix([[4, 7], [2, 6]]);
const resultMatrix: Matrix<number> = matrix.invertSquare();
console.log(resultMatrix.toString());
// Output:
" 0.6 -0.7"
"-0.2 0.4"
Inverts an upper triangular matrix.
The inverted upper triangular matrix.
If the original matrix is not square or an upper triangular matrix, an error is thrown.
const matrix: Matrix<number> = new Matrix([[1, 2], [0, 1]]);
const resultMatrix: Matrix<number> = matrix.invertUpper();
console.log(resultMatrix.toString());
// Output:
"1 -2"
"0 1"
Private
is1dCalculates the Manhattan norm of the matrix, which is the maximum value of the sum of absolute values in each column.
The Manhattan norm of the matrix.
const matrix: Matrix<number> = new Matrix<number>([[1, 2], [-3, 4], [-5, -6]]);
const manhattanNorm: number = matrix.manhattanNorm();
console.log(manhattanNorm);
// Output: 12
Applies a callback function to each element in the matrix and returns a new matrix with the result.
A function to execute for each element in the matrix. It takes three arguments: the current element being processed, the index of the current element, and the matrix.mElements on which map() was called. Its return value is added as a single element in the new mElement.
A new matrix with the callback function applied to each element.
const matrix: Matrix<number> = new Matrix<number>([[1, 2], [3, 4]]);
const mappedMatrix: Matrix<number> = matrix.map((element: number) => {
return element * 2;
});
console.log(mappedMatrix.toString());
// Output:
" 1 0"
"-2 1"
Multiplies this matrix with another matrix using the naive algorithm.
The matrix to multiply with.
The resulting matrix.
If the dimensions of the two matrices are not compatible for multiplication.
const matrixA: Matrix<number> = new Matrix([[1, 2], [3, 4]]);
const matrixB: Matrix<number> = new Matrix([[5, 6], [7, 8]]);
const resultMatrix: Matrix<number> = matrixA.multiply(matrixB);
console.log(resultMatrix.toString());
// Output:
"19 22"
"43 50"
Calculates the p-norm of the matrix.
The power to which each element is raised.
The p-norm of the matrix.
If the provided p is not a positive integer.
const matrix: Matrix<number> = new Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]]);
const pNorm: number = matrix.pNorm(2);
console.log(pNorm);
// Output: 16.881943016134134
Raises the matrix to the power of exp
.
The exponent to raise the matrix to.
The resulting matrix after raising it to the power of exp
.
const matrix: Matrix<number> = new Matrix([[1, 2], [3, 4]]);
const resultMatrix: Matrix<number> = matrix.pow(2);
console.log(resultMatrix.toString());
// Output:
"7 10"
"15 22"
if the matrix is not square.
Calculates the rank of the matrix. The rank of a matrix is defined as the maximum number of linearly independent rows or columns.
The rank of the matrix.
*
const matrix: Matrix<number> = new Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]]);
const rank: number = matrix.rank();
console.log(rank);
// Output: 2
Removes a column from the matrix and returns a new matrix with the column removed.
The index of the column to remove.
A new matrix with the specified column removed.
If the columnNum argument is not a number.
If the columnNum argument is greater than the number of columns in the matrix.
const matrix:Matrix<number> = new Matrix<number>([[1, 2, 3], [4, 5, 6], [7, 8, 9]]);
const newMatrix:Matrix<number> = matrix.removeColumn(1);
console.log(newMatrix.toString());
// Output:
"1 3"
"4 6"
"7 9"
Removes a row from the matrix and returns a new matrix with the row removed.
The index of the row to remove.
A new matrix with the specified row removed.
If the rowNum argument is not a number.
If the rowNum argument is greater than the number of rows in the matrix.
const matrix: Matrix<number> = new Matrix([[1, 2], [3, 4], [5, 6]]);
const newMatrix: Matrix<number> = matrix.removeRow(1);
console.log(newMatrix.toString());
// Output:
"1 2"
"5 6"
Scales the matrix and returns a new matrix with the result of the scaling.
The scalar to scale the matrix with.
The scaled matrix.
const matrix: Matrix<number> = new Matrix([[1, 2], [3, 4]]);
const scaledMatrix: Matrix<number> = matrix.scale(2);
console.log(scaledMatrix.toString());
// Output:
"2 4"
"6 8"
Sets a specific column of the matrix.
The index of the column to set .
An array comprising the values to set.
If the columnIndex is out of bounds or the size of the input array does not match the row size of the matrix.
const matrix: Matrix<number> = new Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]]);
matrix.setColumn(2, [7, 8, 9]);
console.log(matrix.toString());
// Output:
"1 2 7"
"4 5 8"
"7 8 9"
Sets the value of an element in the matrix.
The row index of the element starts from zero.
The column index of the element starts from zero.
The value to set.
const matrix: Matrix<Number> = new Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]]);
matrix.setElement(0, 1, 10);
console.log(matrix.getElement(0, 1));
// Output: 10
Sets a specific row of the matrix.
The index of the row to set .
An array comprising the values to set.
If the rowIndex is out of bounds or the size of the input array does not match the column size of the matrix.
const matrix: Matrix<number> = new Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]]);
matrix.setRow(1, [3, 2, 1]);
console.log(matrix.toString());
// Output:
"1 2 7"
"3 2 1"
"7 8 9"
Sets the submatrix of a given matrix
The starting row index of the submatrix.
The ending row index of the submatrix (exclusive).
The starting column index of the submatrix.
The ending column index of the submatrix (exclusive).
The elements of the submatrix to be set.
If the start or end indices are out of bounds, or if the dimensions of the submatrix do not match the specified dimensions.
const matrix: Matrix<number> = new Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]]);
const subMatrix: Matrix<number> = new Matrix([[3, 3], [3, 3]]);
matrix.setSubMatrix(1, 3, 1, 3, subMatrix);
console.log(matrix.toString());
// Output:
"1 2 7"
"4 3 3"
"7 3 3"
Performs matrix multiplication using the Strassen's algorithm.
The matrix to multiply with.
The result of matrix multiplication.
If both matrixes are not square
const matrixA: Matrix<number> = new Matrix([[1, 2], [3, 4]]);
const matrixB: Matrix<number> = new Matrix([[5, 6], [7, 8]]);
const resultMatrix: Matrix<number> = matrixA.strassenMultiply(matrixB);
console.log(resultMatrix.toString());
// Output:
"19 22"
"43 50"
If the matrices are not square.
Subtracts another matrix from this matrix.
The matrix to subtract.
The resulting matrix.
If the dimensions of the two matrices are not compatible for subtraction.
const matrixA = new Matrix([[1, 2], [3, 4]]);
const matrixB = new Matrix([[5, 6], [7, 8]]);
const resultMatrix = matrixA.subtract(matrixB);
console.log(resultMatrix.toString());
// Output:
"-4 -4"
"-4 -4"
If the dimensions of the two matrices are not compatible for subtraction.
Swaps two columns of the matrix.
The index of the first column to swap.
The index of the second column to swap.
If the matrix has less than two columns or either column index is out of bounds.
const matrix: Matrix<number> = new Matrix([[1, 2], [3, 4]]);
matrix.swapColumns(0, 1);
console.log(matrix.toString());
// Output:
"2 1"
"4 3"
Swaps two rows of the matrix.
The index of the first row to swap.
The index of the second row to swap.
If either row index is out of bounds.
const matrix: Matrix<number> = new Matrix([[1, 2], [3, 4]]);
matrix.swapRows(0, 1);
console.log(matrix.toString());
// Output:
"3 4"
"1 2"
Calculates the trace of the matrix. Throws an error if the matrix is not square.
The trace value.
If the matrix is not square.
const matrix: Matrix<number> = new Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]]);
const trace: number = matrix.trace();
console.log(trace);
// Output: 15
Private
updatePrivate
updatePrivate
updatePerforms vector-matrix multiplication by multiplying each element of the matrix by the corresponding element in the input vector.
The input vector.
A new matrix resulting from the vector-matrix multiplication.
If the input vector is not an array or if its length doesn't match the number of columns in the matrix.
const matrix: Matrix<number> = new Matrix([[1, 2], [3, 4]]);
const vector: number[] = [2, 3];
const resultMatrix: Matrix<number> = matrix.vMultiply(vector);
console.log(resultMatrix.toString());
// Output:
"2 6"
"6 12"
Private
valida1Static
cloneDeep clones the matrix instance and returns the clone
The cloned matrix
const matrix: Matrix<number> = new Matrix([[1, 2], [3, 4]]);
const clonedMatrix: Matrix<number> = Matrix.clone(matrix);
console.log(clonedMatrix.toString());
// Output:
"1 2"
"3 4"
Static
identityCreates an identity matrix with the specified dimension.
The dimension of the identity matrix.
Optional
numerical: Numerical<T>The identity matrix.
const identityMatrix: Matrix<number> = Matrix.identity(3);
console.log(identityMatrix.toString());
// Output:
"1 0 0"
"0 1 0"
"0 0 1"
Static
isChecks if the given matrix is diagonal.
The type of elements in the matrix.
The matrix to check.
const matrix: Matrix<number> = new Matrix([[1, 0], [0, 2]]);
const diagonal: Boolean = Matrix.isDiagonal(matrix);
console.log(diagonal);
// Output: true
Static
isChecks if the given matrix is empty.
The type of elements in the matrix.
The matrix to check.
const matrix: Matrix<number> = new Matrix([[1, 2], [3, 4]]);
const empty: boolean = Matrix.isEmpty(matrix);
console.log(empty);
// Output: false
Static
isChecks if the given matrix is an identity matrix.
The type of elements in the matrix.
The matrix to check.
const matrix: Matrix<number> = new Matrix([[1, 0], [0, 1]]);
const identity: boolean = Matrix.isIdentity(matrix);
console.log(identity);
// Output: true
Static
isChecks if a given matrix contains only integer elements.
The matrix to check.
True if all elements in the matrix are integers, false otherwise.
If the argument is not an instance of Matrix.
const matrix: Matrix<number> = new Matrix([[1, 2], [3, 4]]);
console.log(Matrix.isIntMatrix(matrix));
// Output: true
const nonIntMatrix: Matrix<number> = new Matrix([[1.5, 2.7], [3.1, 4.6]]);
console.log(Matrix.isIntMatrix(nonIntMatrix));
// Output: false
Static
isChecks if a matrix is invertible.
The type of elements in the matrix.
The matrix to check for invertibility.
True if the matrix is invertible, false otherwise.
const matrixA: Matrix<number> = new Matrix([[1, 2], [3, 4]]);
const invertible: Boolean = Matrix.isInvertible(matrixA);
console.log(invertible);
// Output: true
const matrixB: Matrix<number> = new Matrix([[1, 2], [2, 4]]);
const invertible: boolean = Matrix.isInvertible(matrixB);
console.log(invertible);
// Output: false
Static
isThis method checks if the matrix is lower triangular. A matrix is said to be lower triangular if all its entries above the main diagonal are zero.
The matrix to check
const lowerTriangularMatrix: Matrix<number> = new Matrix([[1, 0, 0], [2, 3, 0], [4, 5, 6]]);
console.log(Matrix.isLowerTriangular(lowerTriangularMatrix));
// Output: true
const nonLowerTriangularMatrix: Matrix<number> = new Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]]);
console.log(Matrix.isLowerTriangular(nonLowerTriangularMatrix));
// Output: false
Static
isChecks if the given matrix is symmetric.
The type of elements in the matrix.
The matrix to check.
const matrix: Matrix<number> = new Matrix([[1, 2], [2, 3]]);
const symmetric: boolean = Matrix.isSymmetric(matrix);
console.log(symmetric);
// Output: true
Static
isThis method checks if the given matrix is upper triangular. A matrix is said to be upper triangular if all its entries below the main diagonal are zero.
The matrix to check
Returns true if the matrix is upper triangular, false otherwise.
const upperTriangularMatrix: Matrix<number> = new Matrix([[1, 2, 3], [0, 4, 5], [0, 0, 6]]);
console.log(Matrix.isUpperTriangular(upperTriangularMatrix));
// Output: true
const nonUpperTriangularMatrix: Matrix<number> = new Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]]);
console.log(Matrix.isUpperTriangular(nonUpperTriangularMatrix));
// Output: false
Static
nullifySets all elements of the matrix to their respective zero values.
The type of elements in the matrix.
The matrix to nullify.
If the argument is not an instance of Matrix.
const matrix: Matrix<number> = new Matrix<number>([[1, 2], [3, 4]]);
Matrix.null(matrix);
console.log(matrix.toString());
// Output:
"0 0"
"0 0"
Static
onesCreates a matrix filled with ones with the specified number of rows and columns.
The number of rows in the matrix.
The number of columns in the matrix.
Optional
numerical: Numerical<T>const onesMatrix: Matrix<number> = Matrix.ones(2, 2);
console.log(onesMatrix.toString());
// Output:
"1 1"
"1 1"
Static
padMethod used to pad the matrix dimensions to the nearest power of two.
The matrix to pad
The padded matrix with dimensions as a power of two.
const matrix: Matrix<number> = new Matrix([[1, 2, 3], [4, 5, 6]]);
const paddedMatrix: Matrix<number> = Matrix.padMatrixToPowerOfTwo(matrix);
console.log(paddedMatrix.toString());
// Output:
"1 2 3 0"
"4 5 6 0"
"0 0 0 0"
"0 0 0 0"
Static
randomCreates a random matrix with the specified number of rows and columns.
The number of rows in the matrix.
The number of columns in the matrix
Optional
numerical: Numerical<T>The randomized matrix
const randomMatrix: Matrix<number> = Matrix.random(2, 2);
console.log(randomMatrix.toString());
// Output: (will vary due to randomness)
"45 78"
"12 67"
Static
reshapeReshapes a matrix into a new matrix with the specified number of rows and columns.
The matrix to reshape
The number of rows in the reshaped matrix.
The number of columns in the reshaped matrix.
The reshaped matrix.
const matrix: Matrix<number> = new Matrix([[1,2,3],[4,5,6]])
const reshapedMatrix: Matrix<number> = Matrix.reshape(matrix, 3, 2);
console.log(reshapedMatrix.toString());
// Output:
"1 2"
"3 4"
"5 6"
Reshapes a 1D array into a matrix with the specified number of rows and columns.
The 1D array to reshape.
The number of rows in the reshaped matrix.
The number of columns in the reshaped matrix.
Optional
numerical: Numerical<T>The reshaped matrix.
const array: number[] = [1, 2, 3, 4];
const reshapedMatrix: Matrix<number> = Matrix.reshape(array, 2, 2);
console.log(reshapedMatrix.toString());
// Output:
"1 2"
"3 4"
Static
roundRounds values close to zero in the given array and modifies the matrix in place
Matrix to round
const matrix: Matrix<number> = new Matrix([[0.001, 1], [2, 0.0001]]);
Matrix.roundMatrixToZero(matrix);
console.log(matrix.toString());
// Output:
"0 1"
"2 0"
Static
toRounds all elements of a matrix in place to a specified number of decimal places using a specified base.
The matrix to round.
The number of decimal places to round to.
The base to use for rounding. Defaults to 10 if not provided.
const matrix: Matrix<number> = new Matrix([[1.2345, 2.3456], [3.4567, 4.5678]]);
Matrix.toFixedMatrix(matrix, 2);
console.log(matrix.toString());
// Output:
"1.23 2.35"
"3.46 4.57"
Static
zerosCreates a matrix filled with zeros with the specified number of rows and columns.
The number of rows in the matrix.
The number of columns in the matrix.
Optional
numerical: Numerical<T>const zerosMatrix: Matrix<number> = Matrix.zeros(2, 2);
console.log(zerosMatrix.toString());
// Output:
"0 0"
"0 0"
Generated using TypeDoc
The
Matrix
class represents a mathematical matrix with generic typeT
. It implements theMatrixInterface<T, any>
interface.Type Param
The type of of the Numerical implementation
Remarks
This class provides methods for matrix operations such as addition, subtraction, multiplication, solving systems, etc.
Example