node-matrices
v1.0.0
Published
A simple and lightweight matrix library
Readme
node-matrices 
node-matrices is a simple, lightweight matrix manipulation library supporting many common matrix operations.
Installation
npm install node-matricesvar Matrix = require('node-matrices');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 = new Matrix(
[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 = new Matrix(
[1, 2, 3],
[4, 5, 6]
);
m.numRows()
// -> 2
m.numColumns()
// -> 3#get(rowIndex, columnIndex)- Returns the value at a specific location. This will returnundefinedif either index is out of range.
var m = new Matrix(
[1, 2, 3],
[4, 5, 6]
);
m.get(0, 1)
// -> 2
m.get(1, 2)
// -> 6
m.get(500, 0)
// -> 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 = new Matrix(
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
);
m.getRow(1)
// -> Matrix { data: [ [ 4, 5, 6 ] ] }
m.getColumn(2)
// -> 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
startIndexandendIndex - 1, inclusive. IfendIndexis not provided, the rows/columns will be sliced until the end of the matrix.
var m = new Matrix(
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
[13, 14, 15, 16]
);
m.sliceRows(1, 3)
// -> Matrix { data: [ [ 5, 6, 7, 8 ], [ 9, 10, 11, 12 ] ] }
m.sliceColumns(0, 2)
// -> Matrix { data: [ [ 1, 2 ], [ 5, 6 ], [ 9, 10 ], [ 13, 14 ] ] }
m.sliceBlock(0, 3, 1, 3)
// -> 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 = new Matrix(
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
);
m.omitRow(0)
// -> Matrix { data: [ [ 4, 5, 6 ], [ 7, 8, 9 ] ] }
m.omitColumn(1)
// -> 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 = new Matrix(
[1, 2],
[3, 4]
);
var m2 = new Matrix(
[5, 6],
[7, 8]
);
m1.combineHorizontal(m2)
// -> Matrix { data: [ [ 1, 2, 5, 6 ], [ 3, 4, 7, 8 ] ] }
m1.combineVertical(m2)
// -> 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
rowIndexandcolumnIndexisvalue.
var m = new Matrix(
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
);
m.replace(1, 2, Infinity)
// -> Matrix { data: [ [ 1, 2, 3 ], [ 4, 5, Infinity ], [ 7, 8, 9 ] ] }Matrix operations
#transpose()- Returns the transpose of this matrix.
var m = new Matrix(
[1, 2, 3],
[4, 5, 6],
);
m.transpose()
// -> 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 = new Matrix(
[1, 2],
[3, 4]
);
m.determinant()
// -> -2#adjugate()- Returns the adjugate of this matrix. An error will be thrown if this matrix is not square.
var m = new Matrix(
[1, 2],
[3, 4]
);
m.adjugate()
// -> 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 = new Matrix(
[1, 2],
[3, 4]
);
m.inverse()
// -> 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 = new Matrix(
[1, 2],
[3, 4]
);
var m2 = new Matrix(
[5, 5],
[5, 5]
);
m1.add(m2)
// -> Matrix { data: [ [ 6, 7 ], [ 8, 9 ] ] }
m1.subtract(m2)
// -> 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 = new Matrix(
[1, 2, 3],
[4, 5, 6]
);
var m2 = new Matrix(
[7, 8],
[9, 10],
[11, 12]
);
m1.multiply(m2)
// -> Matrix { data: [ [ 58, 64 ], [ 139, 154 ] ] }
m1.multiply(m1)
// (throws an error)#scale(scalar)- Returns the scalar product of this matrix and
scalar.
var m1 = new Matrix(
[1, 2],
[3, 4]
);
m1.scale(3)
// -> Matrix { data: [ [ 3, 6 ], [ 9, 12 ] ] }#pow(exponent)- Raises this matrix to the
exponentpower. An error will be thrown if this matrix is not square, or ifexponentis not an integer.
var m1 = new Matrix(
[1, 2],
[3, 4]
);
m1.pow(5)
// -> Matrix { data: [ [ 37, 54 ], [ 81, 118 ] ] }Value-checking
#equals(otherMatrix)- Returns
trueifthisandotherMatrixare equivalent, andfalseotherwise. Equivalent matrices contain all of the same values in the same locations.
var m1 = new Matrix([1, 2, 3]);
var m2 = new Matrix([1, 2, 3]);
var m3 = new Matrix([1, 2, 4]);
m1.equals(m2)
// -> true
m1.equals(m3)
// -> false#isSquare()- Returns
trueif this matrix is square (i.e. has the same number of rows and columns), andfalseotherwise.
var m1 = new Matrix(
[1, 2],
[3, 4]
);
var m2 = new Matrix(
[1, 2, 3],
[4, 5, 6]
);
m1.isSquare()
// -> true
m2.isSquare()
// -> false#isSymmetric()#isSkewSymmetric()- Returns
trueif this matrix is symmetric (forisSymmetric) or skew-symmetric (forisSkewSymmetric). Otherwise, returnsfalse.
var m1 = new Matrix(
[1, 2],
[2, 4]
);
var m2 = new Matrix(
[0, 2],
[-2, 0]
);
m1.isSymmetric()
// -> true
m1.isSkewSymmetric()
// -> false
m2.isSymmetric()
// -> false
m2.isSkewSymmetric()
// -> true#isUpperTriangular()#isLowerTriangular()#isDiagonal()- Returns
trueif this matrix is upper triangular, lower triangular, or diagonal, respectively; otherwise, returnsfalse.
var m1 = new Matrix(
[1, 2],
[0, 5]
);
var m2 = new Matrix(
[1, 0],
[0, 5]
);
m1.isUpperTriangular()
// -> true
m1.isLowerTriangular()
// -> false
m1.isDiagonal()
// -> false
m2.isUpperTriangular()
// -> true
m2.isLowerTriangular()
// -> true
m2.isDiagonal()
// -> true#isIdentity()- Returns
trueif this matrix is an identity matrix, otherwisefalse.
var m1 = new Matrix(
[1, 0],
[0, 1]
);
var m2 = new Matrix(
[1, 1],
[0, 1]
);
m1.isIdentity()
// -> true
m2.isIdentity()
// -> false#isNonZero()- Returns
trueif this matrix contains any nonzero values. Otherwise, returnsfalse.
var m1 = new Matrix(
[0, 0],
[0, 0]
);
var m2 = new Matrix(
[0, 1],
[0, 0]
);
m1.isNonZero()
// -> false
m2.isNonZero()
// -> true#isSingular()- Returns
trueif this matrix is singular, otherwisefalse. An error will be thrown if this matrix is not square.
var m1 = new Matrix(
[1, 2],
[3, 4]
);
var m2 = new Matrix(
[1, 2],
[2, 4]
);
m1.isSingular()
// -> false
m2.isSingular()
// -> trueStatic methods
.identity(size)- Returns an identity matrix of the specified size.
Matrix.identity(3)
// -> 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.zeros(2, 3)
// -> Matrix { data: [ [ 0, 0, 0 ], [ 0, 0, 0 ] ] }