node-matrix

1.0.0 • Public • Published

matrix

NPM version build status license

A tiny matrix library written in JavaScript

Installation

$ npm install node-matrix

Example

var m1 = Matrix([
  [1, 3, 1],
  [1, 0, 0]
]);
 
var m2 = Matrix([
  [0, 0, 5],
  [7, 5, 0]
]);
 
Matrix.add(m1, m2); // [ [1, 3, 6], [8, 5, 0] ]

API

Matrix()

Create a new instance of Matrix. You can pass in a multidimensional array:

var matrix = Matrix([
  [4, 1, 9],
  [3, 8, 2]
]);

Or you can pass in an object specifying the dimensions and default values. If you don't pass in a default value, or a function to be executed for each element to calculate it's value, then it will set the value of each element to 0.

var matrix = Matrix({ rows: 3, columns: 2 }); // Set the value of each element to 0
var matrix = Matrix({ rows: 3, columns: 2, values: 5 });  // // Set the value of each element to 5
var matrix = Matrix({ rows: 3, columns: 2, values: Math.random }); // Set the value of each element to a random number

You can access the matrix's values the same way you can access them from an array: matrix[0][1], with 0 corresponding to the row number and 1 corresponding to the column number. You also now have access to matrix.dimensions, which will return an array where the first element is the number of rows in the matrix, and the second element is the number of columns in the matrix.

Matrix.add()

Matrix.add(m1, m2);

Matrix.subtract()

Matrix.subtract(m1, m2);

Matrix.multiply()

Matrix.multiply(m1, m2);  // matrix multiplication

Matrix.multiplyScalar()

Matrix.multiplyScalar(m1, 4); // scalar multiplication

Matrix.multiplyElements()

Matrix.multiplyElements(m1, m2); // element-wise multiplication

matrix.transpose()

matrix.transpose();

matrix.transform()

matrix.transform(function(num) { return num - 2 });   // subtract two from each element in the matrix

License

MIT

Readme

Keywords

none

Package Sidebar

Install

npm i node-matrix

Weekly Downloads

1

Version

1.0.0

License

MIT

Last publish

Collaborators

  • npm-support