Creates an instance of matrix with the specified number of rows and columns, initialized with the provided data or with zeroes.
Optional
rows: number | number[][]The number of rows in the matrix.
Optional
columns: number | number[][]The number of columns in the matrix.
Optional
data: number[][]The initial data to populate the matrix (optional).
// Initialize a 1x1 matrix with zero
const matrix = new Matrix();
// Initialize a 3x3 matrix with zeroes
const matrix = new Matrix(3, 3);
// Initialize a 3x3 matrix with provided data
const matrix = new Matrix(3, 3, data);
// Initialize a 3x3 matrix with provided data
const matrix = new Matrix(data);
// Initialize a nxn matrix with provided data
const matrix = new Matrix(3, data);
// Initialize a nxn matrix with zeroes
const matrix = new Matrix(3);
Private
_columnsPrivate
_dataPrivate
_rowsGets the columns of the matrix.
The number of columns in the matrix.
Gets the data of the matrix.
The data of the matrix.
Gets the rows of the matrix.
The number of rows in the matrix.
Private
_initializePrivate
Initializes the matrix.
The number of rows.
The number of columns.
The data of the matrix.
Checks if the specified row and column indices are within the matrix boundaries. If the data is provided, it is used to initialize the matrix. Otherwise, the matrix is initialized with zeroes.
Private
_isPrivate
Retrieves the null space of the matrix.
The row index.
The column index.
Checks if the specified row and column indices are within the matrix boundaries.
Throws an error if the indices are out of bounds.
Adds the specified matrix to the current matrix.
The matrix to add.
Returns the resulting matrix.
Throws an error if the matrix dimensions are not same.
const matrix1 = new Matrix(2, 3, [[1, 2, 3], [4, 5, 6]]);
const matrix2 = new Matrix(2, 3, [[1, 2, 3], [4, 5, 6]]);
matrix1.add(matrix2); // [[2, 4, 6], [8, 10, 12]]
Retrieves the value at the specified row and column indices.
The row index.
The column index.
Returns the value at the specified indices.
const matrix = new Matrix(2, 3, [[1, 2, 3], [4, 5, 6]]);
matrix.at(0, 0); // 1
matrix.at(0, 1); // 2
Retrieves the column space of the matrix.
The column space of the matrix.
The column space of a matrix is the collection of all linear combinations of its column vectors.
const matrix = new Matrix(3, 3, [
[1, 2, 3],
[0, 1, 4],
[5, 6, 0]
]);
matrix.colSpace();
// => [
// [1, 0, 5],
// [2, 1, 6],
// [3, 4, 0]
// ]
Calculates the determinant of the matrix.
Returns the determinant of the matrix.
Throws an error if the matrix is not square.
const matrix = new Matrix(3, 3, [[1, 2, 3], [4, 5, 6], [7, 8, 9]]);
const result = matrix.determinant();
console.log(result);
// => 0
Checks if the matrix is equal to another matrix.
The matrix to compare.
Returns true if the matrix is equal to another matrix, false otherwise.
const matrix = new Matrix(3, 3, [
[1, 2, 3],
[0, 1, 4],
[5, 6, 0]
]);
const otherMatrix = new Matrix(3, 3, [
[1, 2, 3],
[0, 1, 4],
[5, 6, 0]
]);
matrix.equals(otherMatrix); // true
Calculates the cofactor of the matrix for the specified row and column.
The row index.
The column index.
Returns the cofactor of the matrix for the specified row and column.
const matrix = new Matrix(3, 3, [[1, 2, 3], [4, 5, 6], [7, 8, 9]]);
const result = matrix.getCofactor(0, 0);
console.log(result);
// => 0
Calculates the minor of the matrix for the specified row and column.
The row index.
The column index.
Returns the minor matrix.
const matrix = new Matrix(3, 3, [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]);
const result = matrix.getMinor(0, 0);
console.log(result);
// => Matrix {
// rows: 2,
// columns: 2,
// data: [
// [ 5, 6 ],
// [ 8, 9 ]
// ]
// }
Inverse the matrix.
Returns the inverse of the matrix.
Throws an error if the matrix is not square.
Throws an error if the matrix is not invertible.
const matrix = new Matrix(3, 3, [
[1, 2, 3],
[0, 1, 4],
[5, 6, 0]
]);
matrix.inverse();
// => Matrix {
// rows: 3,
// columns: 3,
// data: [
// [ -24, 18, 5 ],
// [ 20, -15, -4 ],
// [ -5, 4, 1 ]
// ]
// }
Checks if the matrix is identity.
Returns true
if the matrix is identity, false
otherwise.
An identity matrix is a square matrix with ones on the main diagonal.
const matrix = new Matrix(3, 3, [
[1, 0, 0],
[0, 1, 0],
[0, 0, 1]
]);
matrix.isIdentity(); // true
Checks if the matrix has the same order as the specified matrix.
The matrix to compare against.
Returns true
if the matrices have the same order, false
otherwise.
const matrix1 = new Matrix(2, 3, [[1, 2, 3], [4, 5, 6]]);
const matrix2 = new Matrix(2, 3, [[7, 8, 9], [10, 11, 12]]);
const result = matrix1.isSameOrder(matrix2);
console.log(result);
// => true
https://en.wikipedia.org/wiki/Matrix_(mathematics)#Equality_and_inequality
Applies the modulo operation to each element in the matrix.
The modulo to apply.
Returns the matrix with the modulo applied.
This method performs the modulo operation on each element of the matrix, using the specified modulo value. The modulo operation calculates the remainder when dividing each element by the modulo value.
const matrix = new Matrix(2, 2, [
[1, 2],
[3, 4]
]);
matrix.modulo(3);
// => Matrix {
// rows: 2,
// columns: 2,
// data: [
// [ 1, 2 ],
// [ 0, 1 ]
// ]
// }
Multiplies the current matrix with the specified matrix or scalar. If the specified value is a scalar, it is multiplied with each element of the matrix. If the specified value is a matrix, it is multiplied with the current matrix.
The matrix or scalar to multiply with.
Returns the resulting matrix.
Throws an error if the matrix dimensions are not compatible.
const matrix1 = new Matrix(2, 3, [[1, 2, 3], [4, 5, 6]]);
const matrix2 = new Matrix(3, 2, [[1, 2], [3, 4], [5, 6]]);
matrix1.multiply(matrix2); // [[22, 28], [49, 64]]
matrix1.multiply(2); // [[2, 4, 6], [8, 10, 12]]
Rotate the matrix by a given angle.
The angle to rotate the matrix by, in radians.
Returns the rotated matrix.
Throws an error if the angle is not a multiple of 90 degrees.
const matrix = new Matrix(2, 2, [
[1, 2],
[3, 4]
]);
matrix.rotate(Math.PI / 2);
// => Matrix {
// rows: 2,
// columns: 2,
// data: [
// [ 3, 1 ],
// [ 4, 2 ]
// ]
// }
Retrieves the row space of the matrix.
The row space of the matrix.
The row space of a matrix is the collection of all linear combinations of its row vectors.
const matrix = new Matrix(3, 3, [
[1, 2, 3],
[0, 1, 4],
[5, 6, 0]
]);
matrix.rowSpace();
// => [
// [1, 2, 3],
// [0, 1, 4],
// [5, 6, 0]
// ]
Sets the data of the matrix
The data to set.
Optional
override: booleanWhether to override the dimensions of the matrix.
Sets the data of the matrix. If the data provided does not match the dimensions of the matrix, an error will be thrown. If the override flag is set to true, the dimensions of the matrix will be changed to match the data provided.
Subtracts the specified matrix from the current matrix.
The matrix to subtract.
Returns the resulting matrix.
Throws an error if the matrix dimensions are not same.
const matrix1 = new Matrix(2, 3, [[1, 2, 3], [4, 5, 6]]);
const matrix2 = new Matrix(2, 3, [[1, 2, 3], [4, 5, 6]]);
matrix1.subtract(matrix2); // [[0, 0, 0], [0, 0, 0]]
Transposes the matrix.
Returns the transposed matrix.
const matrix = new Matrix(2, 3, [
[1, 2, 3],
[4, 5, 6]
]);
const result = matrix.transpose();
console.log(result);
// => Matrix {
// rows: 3,
// columns: 2,
// data: [
// [ 1, 4 ],
// [ 2, 5 ],
// [ 3, 6 ]
// ]
// }
Generated using TypeDoc
Classdesc
A TypeScript matrix math library for matrix operations and calculations.
Version
1.0.0
Author
Liron Abutbul (ladunjexa)
License
MIT
See
(Wikipedia)