node-matrices
node-matrices is a simple, lightweight matrix manipulation library supporting many common matrix operations.
Installation
npm install node-matrices
var Matrix = ;
Documentation
General notes:
- All Matrix objects are immutable.
- All indices are zero-based.
Basic manipulation
.constructor(...rows)
- Each parameter to the constructor should be an array of integers corresponding to a single row of the matrix. All of the rows should have the same length.
var m = 1 2 3 4 5 6 7 8 9;// -> Matrix { data: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] }
#numRows()
- Returns the number of rows in the matrix.#numColumns()
- Returns the number of columns in the matrix.
var m = 1 2 3 4 5 6;m// -> 2m// -> 3
#get(rowIndex, columnIndex)
- Returns the value at a specific location. This will returnundefined
if either index is out of range.
var m = 1 2 3 4 5 6;m// -> 2m// -> 6m// -> undefined
#getRow(rowIndex)
- Returns a new matrix containing only the row at the specified index.#getColumn(columnIndex)
- Returns a new matrix containing only the column at the specified index.
var m = 1 2 3 4 5 6 7 8 9;m// -> Matrix { data: [ [ 4, 5, 6 ] ] }m// -> Matrix { data: [ [ 3 ], [ 6 ], [ 9 ] ] }
#sliceRows(startIndex[, endIndex])
#sliceColumns(startIndex[, endIndex])
#sliceBlock(startRowIndex, endRowIndex, startColumnIndex, endColumnIndex)
- Returns a new matrix containing only the rows between
startIndex
andendIndex - 1
, inclusive. IfendIndex
is not provided, the rows/columns will be sliced until the end of the matrix.
var m = 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16;m// -> Matrix { data: [ [ 5, 6, 7, 8 ], [ 9, 10, 11, 12 ] ] }m// -> Matrix { data: [ [ 1, 2 ], [ 5, 6 ], [ 9, 10 ], [ 13, 14 ] ] }m// -> Matrix { data: [ [ 2, 3 ], [ 6, 7 ], [ 10, 11 ] ] }
#omitRow(rowIndex)
#omitColumn(columnIndex)
- Returns a new matrix with the specified row or column omitted.
var m = 1 2 3 4 5 6 7 8 9;m// -> Matrix { data: [ [ 4, 5, 6 ], [ 7, 8, 9 ] ] }m// -> Matrix { data: [ [ 1, 3 ], [ 4, 6 ], [ 7, 9 ] ] }
#combineHorizontal(otherMatrix)
#combineVertical(otherMatrix)
- Combines two matrices as blocks. An error will be thrown if the matrices have a different number of rows (for
combineHorizontal
) or a different number of columns (forcombineVertical
).
var m1 = 1 2 3 4;var m2 = 5 6 7 8;m1// -> Matrix { data: [ [ 1, 2, 5, 6 ], [ 3, 4, 7, 8 ] ] }m1// -> Matrix { data: [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ], [ 7, 8 ] ] }
#replace(rowIndex, columnIndex, value)
- Returns a new matrix where the which is exactly the same as the old matrix, except that the value at
rowIndex
andcolumnIndex
isvalue
.
var m = 1 2 3 4 5 6 7 8 9;m// -> Matrix { data: [ [ 1, 2, 3 ], [ 4, 5, Infinity ], [ 7, 8, 9 ] ] }
Matrix operations
#transpose()
- Returns the transpose of this matrix.
var m = 1 2 3 4 5 6;m// -> Matrix { data: [ [ 1, 4 ], [ 2, 5 ], [ 3, 6 ] ] }
#determinant()
- Returns the determinant of this matrix. An error will be thrown if this matrix is not square.
var m = 1 2 3 4;m// -> -2
#adjugate()
- Returns the adjugate of this matrix. An error will be thrown if this matrix is not square.
var m = 1 2 3 4;m// -> Matrix { data: [ [ 4, -2 ], [ -3, 1 ] ] }
#inverse()
- Returns the inverse of this matrix. An error will be thrown if this matrix is not square, or if this matrix is singular.
var m = 1 2 3 4;m// -> Matrix { data: [ [ -2, 1 ], [ 1.5, -0.5 ] ] }
#add(otherMatrix)
#subtract(otherMatrix)
- Returns the sum or difference of this matrix and another matrix. An error will be thrown if the two matrices have different dimensions.
var m1 = 1 2 3 4;var m2 = 5 5 5 5;m1// -> Matrix { data: [ [ 6, 7 ], [ 8, 9 ] ] }m1// -> Matrix { data: [ [ -4, -3 ], [ -2, -1 ] ] }
#multiply(otherMatrix)
- Returns the product of this matrix and another matrix. An error will be thrown if the matrices do not have compatible sizes.
- To multiply a matrix by a scalar, use
#scale()
.
var m1 = 1 2 3 4 5 6;var m2 = 7 8 9 10 11 12;m1// -> Matrix { data: [ [ 58, 64 ], [ 139, 154 ] ] }m1// (throws an error)
#scale(scalar)
- Returns the scalar product of this matrix and
scalar
.
var m1 = 1 2 3 4;m1// -> Matrix { data: [ [ 3, 6 ], [ 9, 12 ] ] }
#pow(exponent)
- Raises this matrix to the
exponent
power. An error will be thrown if this matrix is not square, or ifexponent
is not an integer.
var m1 = 1 2 3 4;m1// -> Matrix { data: [ [ 37, 54 ], [ 81, 118 ] ] }
Value-checking
#equals(otherMatrix)
- Returns
true
ifthis
andotherMatrix
are equivalent, andfalse
otherwise. Equivalent matrices contain all of the same values in the same locations.
var m1 = 1 2 3;var m2 = 1 2 3;var m3 = 1 2 4;m1// -> truem1// -> false
#isSquare()
- Returns
true
if this matrix is square (i.e. has the same number of rows and columns), andfalse
otherwise.
var m1 = 1 2 3 4;var m2 = 1 2 3 4 5 6;m1// -> truem2// -> false
#isSymmetric()
#isSkewSymmetric()
- Returns
true
if this matrix is symmetric (forisSymmetric
) or skew-symmetric (forisSkewSymmetric
). Otherwise, returnsfalse
.
var m1 = 1 2 2 4;var m2 = 0 2 -2 0;m1// -> truem1// -> falsem2// -> falsem2// -> true
#isUpperTriangular()
#isLowerTriangular()
#isDiagonal()
- Returns
true
if this matrix is upper triangular, lower triangular, or diagonal, respectively; otherwise, returnsfalse
.
var m1 = 1 2 0 5;var m2 = 1 0 0 5;m1// -> truem1// -> falsem1// -> falsem2// -> truem2// -> truem2// -> true
#isIdentity()
- Returns
true
if this matrix is an identity matrix, otherwisefalse
.
var m1 = 1 0 0 1;var m2 = 1 1 0 1;m1// -> truem2// -> false
#isNonZero()
- Returns
true
if this matrix contains any nonzero values. Otherwise, returnsfalse
.
var m1 = 0 0 0 0;var m2 = 0 1 0 0;m1// -> falsem2// -> true
#isSingular()
- Returns
true
if this matrix is singular, otherwisefalse
. An error will be thrown if this matrix is not square.
var m1 = 1 2 3 4;var m2 = 1 2 2 4;m1// -> falsem2// -> true
Static methods
.identity(size)
- Returns an identity matrix of the specified size.
Matrix// -> Matrix { data: [ [ 1, 0, 0 ], [ 0, 1, 0 ], [ 0, 0, 1 ] ] }
.zeros(numRows, numColumns)
- Returns a matrix of the specified dimensions which only contains zeros.
Matrix// -> Matrix { data: [ [ 0, 0, 0 ], [ 0, 0, 0 ] ] }