Find file
Fetching contributors…
Cannot retrieve contributors at this time
124 lines (88 sloc) 3.61 KB

Getting Started


Installing with Matrix is pretty easy. You can download the latest copy from GitHub, or, if you want to use Matrix with Node.js, install it with NPM:

npm install matrixjs

Using matrix

If you are using Matrix for a client-side project, simply include Matrix at the bottom of your HTML file with the rest of your scripts:

<script src="path/to/matrix.js"></script>

Or, if you are using Matrix on node, require it:

var Matrix = require('matrixjs');

Matrix also works with AMD loaders, such as RequireJS. Just require it like you normally would:

require('path/to/matrix.js', function(Matrix) {
    // Do stuff with Matrix here

Creating a Matrix object

A Matrix object is created by calling new Matrix. You have two options when creating a new Matrix object:

  1. A two-dimensional array with the data


    var m = new Matrix([
        [1, 2, 3],
        [4, 5, 6]
  2. A row and column count—this will create a Matrix object with the desired dimensions with zeros at each index.


    var m = new Matrix(2, 2);

Creating an identity Matrix

When creating a new Matrix object you can easily create an n by n matrix by calling new Matrix.identity, like so:

var m = new Matrix.identity(3);
// => Creates a new 3x3 identity matrix

Working with a Matrix object

Once you have created your Matrix object, these methods are available to you:

  • add: Adds two or more matrices to the current matrix
  • subtract: Subtracts a matrix from the current matrix
  • multiply: Multiplies the current matrix by another matrix
  • determinant: Finds the determinant of the current n by n matrix
  • raise: Raises the current matrix to the xth power
    • raise comes with a couple helper functions—calling square() is the same thing as calling raise(2), and calling cube() is the same thing as calling raise(3)
  • scalar: Multiplies each element in the matrix by a scalar
  • inverse: Inverts an n by n matrix
  • equals: Compares against another matrix

Important note about all Matrix methods

All methods on the Matrix methods modify the original object. If you do not want to modify the original object, you can call clone() on the matrix to get a fresh copy of the matrix that will not affect the original.

A note on the use of errors

By default, Matrix will throw an error if the API is used improperly (e.g. the wrong arguments are passed) or if the operation that was requested is not possible (e.g. finding the determinant of a non-square matrix). To turn off this error reporting, set Matrix.silent to true.

Method chaining

Methods can be chained together. For instance:

new Matrix.identity(3)
        [1, 2, 3],
        [4, 5, 6],
        [7, 8, 9]
        [9, 8, 7],
        [6, 5, 4],
        [3, 2, 1]

Getting and setting elements of the matrix

Matrix elements can be get and set using bracket notation. (Remember that row and column numbers are zero indexed.) For example:

// Create a 2 by 2 all zero matrix
var m = new Matrix(2, 2);

// Set m[1,1] to 42
m[1][1] = 42;

// Retrieve the value of m[1,0]
// => Outputs 0