$ npm i universal-matrix --save
import Matrix from 'universal-matrix';
const matrix = new Matrix([1, 2, 3], [4, 5, 6], [7, 8, 9]);
const Matrix = require('universal-matrix');
const matrix = new Matrix([1, 2, 3], [4, 5, 6], [7, 8, 9]);
All methods are chainable and work directly on the matrix data (except for clone
).
Create a zero matrix of size rows * cols
, or a square matrix of size rows * rows
.
- rows {Number} : the number or rows
- cols {Number} (optional) : the number of columns (equals to rows if not specified)
- {Matrix} : a zero matrix of size
rows * columns
.
const square = Matrix.zero(3);
// 0 0 0
// 0 0 0
// 0 0 0
const matrix = Matrix.zero(2, 4);
// 0 0 0 0
// 0 0 0 0
Create an identity matrix of size n * n
.
- n {Number} : the size of the identity matrix
- {Matrix} : an identity matrix of size
n
.
const matrix = Matrix.identity(3);
// 1 0 0
// 0 1 0
// 0 0 1
Randomize the given matrix data.
- matrix {Matrix} : the matrix to randomize
- min {Number} (optional) : the minimum value (default
0
) - max {Number} (optional) : the maximum value (default
1
)
- {Matrix} : matrix instance for method chaining
const matrices = [
// random matrix of size 3 x 3, with values between [0, 1[
Matrix.randomize(Matrix.zero(3)),
// random matrix of size 3 x 3, with values between [0, 10[
Matrix.randomize(Matrix.zero(3), 10),
// random matrix of size 3 x 3, with values between [-3, 5[
Matrix.randomize(Matrix.zero(3), -3, 5),
];
Return the matrix determinant. The given matrix must be square, and contain at least 2 rows.
- matrix {Matrix} : the matrix to calculate the determinant for
- {Number} : the determinant
const matrix = new Matrix([6, 1, 1], [4, -2, 5], [2, 8, 7]);
Matrix.determinant(matrix);
// -306
Create a new matrix with the given row values. Every row must be of same size, otherwise an error will be thrown.
- ...rows {Array} : a list of arrays identifying each rows of the Matrix
const matrix = new Matrix([1, 2, 3], [4, 5, 6], [7, 8, 9]);
// 1 2 3
// 4 5 6
// 7 8 9
matrix.rows;
// 3
matrix.columns;
// 3
matrix.data;
// [1, 2, 3, 4, 5, 6, 7, 8, 9];
Get the value from the matrix. The rows and columns are 0-based.
- row {Number} : the row from
0
tomatrix.rows - 1
- col {Number} : the column from
0
tomatrix.columns - 1
- {Number} : the matrix value
const matrix = new Matrix([1, 2, 3], [4, 5, 6], [7, 8, 9]);
matrix.get(2, 0);
// 7
Get the entire row from the matrix. The rows are 0-based.
- row {Number} : the row from
0
tomatrix.rows - 1
- {Array} : the matrix row
const matrix = new Matrix([1, 2, 3], [4, 5, 6], [7, 8, 9]);
matrix.getRow(2);
// [7 8 9]
Get the entire column from the matrix. The columns are 0-based.
- col {Number} : the col from
0
tomatrix.columns - 1
- {Array} : the matrix column
const matrix = new Matrix([1, 2, 3], [4, 5, 6], [7, 8, 9]);
matrix.getColumn(2);
// [3, 6, 9]
Set the value of the matrix. The rows and columns are 0-based.
- row {Number} : the row from
0
tomatrix.rows - 1
- col {Number} : the column from
0
tomatrix.columns - 1
- value {Number} : the value to set
- {Matrix} : matrix instance for method chaining
const matrix = new Matrix([1, 2, 3], [4, 5, 6], [7, 8, 9]);
matrix.set(2, 1, -88);
// 1 2 3
// 4 5 6
// 7 -88 9
Set the entire row of the matrix. The rows are 0-based.
- row {Number} : the row from
0
tomatrix.rows - 1
- data {Array} : an array of data having the same length as
matrix.columns
- {Matrix} : matrix instance for method chaining
const matrix = new Matrix([1, 2, 3], [4, 5, 6], [7, 8, 9]);
matrix.setRow(2, [-9, -8, -7]);
// 1 2 3
// 4 5 6
// -9 -8 -7
Set the entire column of the matrix. The columns are 0-based.
- col {Number} : the col from
0
tomatrix.columns - 1
- data {Array} : an array of data having the same length as
matrix.rows
- {Matrix} : matrix instance for method chaining
const matrix = new Matrix([1, 2, 3], [4, 5, 6], [7, 8, 9]);
matrix.setColumn(2, [11, 12, 13]);
// 1 2 11
// 4 5 12
// 7 8 13
Apply a given function to the given values of the matrix. The function will receive three (3) arguments:
- val {Number} : the matrix value to apply the function to
- row {Number} : the current row in the Matrix
- col {Number} : the current column in the matrix
- fn {Function} : the function to call on each value
- {Matrix} : matrix instance for method chaining
const matrix = new Matrix([1, 2], [3, 4]);
// matrix.apply(function (val) { return val * 2; });
matrix.apply(val => val * 2);
// 2 4
// 6 8
Add a scalar value n
to every matrix element, or add two matrices together.
For matrix additions, both must have the same number of rows
and columns
.
- n {Number} | matrix {Matrix} : a scalar or matrix to add to the current matrix elements
- {Matrix} : matrix instance for method chaining
const matrix = new Matrix([-6, 2], [4, 0]);
matrix.add(-3);
// -9 -1
// 1 -3
matrix.add(new Matrix([0, 1], [-1, 0]));
// -9 0
// 0 -3
Multiply with a given scalar n
or a specified matrix. For matrix multiplication,
both rows and columns from each matrices must be compatible.
- n {Number} | matrix {Matrix} : a scalar or matrix to multiply to the current matrix elements
- {Matrix} : matrix instance for method chaining
const matrix = new Matrix([2, 3, 4], [1, 0, 0]);
matrix.multiply(3);
// 6 9 12
// 3 0 0
matrix.multiply(new Matrix([0, 1000], [1, 100], [0, 10]));
// 9 7020
// 0 3000
Take one row and merge (add) it to another row. All rows are zero-based, meaning
that the first row is at position 0
, and the last row is at position rows - 1
- removeRow {Number} : the row to remove from the matrix
- addToRow {Number} : the row to add the removed row to
- {Matrix} : matrix instance for method chaining
const a = new Matrix([1, 2, 3], [0, 1, -6], [8, 2, 1]);
// merge the second row into the third row
a.mergeRows(1, 2);
// 1 2 3
// 8 3 -5
a.mergeRows(1, 0);
// 9 5 -2
Multiply only a single row by a scalar.
- row {Number} : the row to multiply
- n {Number} : the scalar value to multiply the row with
- {Matrix} : matrix instance for method chaining
const matrix = new Matrix([1, 2, 3], [0, 0, 7]);
matrix.multiplyRow(1, 1 / 7);
// 1 2 3
// 0 0 1
Delete an entire row from the Matrix.
- row {Number} : the row to remove
- {Matrix} : matrix instance for method chaining
const matrix = new Matrix([1, 2, 3], [4, 5, 6], [7, 8, 9]);
matrix.removeRow(0);
// 4 5 6
// 7 8 9
Delete an entire column from the Matrix.
- col {Number} : the column to remove
- {Matrix} : matrix instance for method chaining
const matrix = new Matrix([1, 2, 3], [4, 5, 6], [7, 8, 9]);
matrix.removeColumn(1);
// 1 3
// 4 6
// 7 9
Transpose the matrix side ways, swaping rows and columns.
- {Matrix} : matrix instance for method chaining
const matrix = new Matrix([1, 2, 3], [4, 5, 6]);
matrix.transpose();
// 1 4
// 2 5
// 3 6
Return the inverse matrix. The matrix must be square, and contain at least 2 rows.
- {Matrix} : matrix instance for method chaining
const matrix = new Matrix([3, 0, 2], [2, 0, -2], [0, 1, 1]);
matrix.inverse();
// 0.2 0.2 0
// -0.2 0.3 1
// 0.2 -0.3 0
matrix.inverse();
// 3 0 2
// 2 0 -2
// 0 1 1
Because all matrix operations operate directly on the data, some cases may require working on a different instance to preserve original data.
- {Matrix} : matrix instance for method chaining
const matrix = new Matrix([1, 2, 3], [4, 5, 6]);
const copy = matrix.clone();
copy.multiply(-3);
// -3 -6 -9
// -12 -15 -18
matrix.add(1);
// 2 3 4
// 5 6 7
MIT