npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

node-matrices

v1.0.0

Published

A simple and lightweight matrix library

Downloads

467

Readme

node-matrices Build Status

node-matrices is a simple, lightweight matrix manipulation library supporting many common matrix operations.

Installation

npm install node-matrices
var 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 return undefined if 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 startIndex and endIndex - 1, inclusive. If endIndex is 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 (for combineVertical).
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 rowIndex and columnIndex is value.
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 exponent power. An error will be thrown if this matrix is not square, or if exponent is 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 true if this and otherMatrix are equivalent, and false otherwise. 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 true if this matrix is square (i.e. has the same number of rows and columns), and false otherwise.
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 true if this matrix is symmetric (for isSymmetric) or skew-symmetric (for isSkewSymmetric). Otherwise, returns false.
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 true if this matrix is upper triangular, lower triangular, or diagonal, respectively; otherwise, returns false.
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 true if this matrix is an identity matrix, otherwise false.
var m1 = new Matrix(
  [1, 0],
  [0, 1]
);
var m2 = new Matrix(
  [1, 1],
  [0, 1]
);
m1.isIdentity()
// -> true
m2.isIdentity()
// -> false

  • #isNonZero()
  • Returns true if this matrix contains any nonzero values. Otherwise, returns false.
var m1 = new Matrix(
  [0, 0],
  [0, 0]
);
var m2 = new Matrix(
  [0, 1],
  [0, 0]
);
m1.isNonZero()
// -> false
m2.isNonZero()
// -> true

  • #isSingular()
  • Returns true if this matrix is singular, otherwise false. 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()
// -> true

Static 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 ] ] }