Mianzhi Wang

Ph.D. in Electrical Engineering

Introducing JASMAL - My JavaScript Matrix Library


As mentioned in my previous post on implementing DOA estimation algorithms in the browser, I was working on my own JavaScript matrix library. Now, I am releasing its source code on my Github. I named it JASMAL (JAvaScript MAtrix Library)[1]. Initially I thought it should be a small project because I only need to implement the minimal set of matrix operations required for the DOA estimation algorithms. It turns out that the implementation of these DOA estimation algorithms actually requires quite a lot of matrix operations. Eventually, JASMAL becomes a quite large JavaScript library, whose file size exceeds 200kB after minimization. To quickly show what JASMAL can do, I assembled a notebook-like page with the help of CodeMirror and Plotly:

Note that the actual computations are conducted on a web worker. Therefore you will need a browser that supports web workers[2]. Here are two more examples demonstrating the capability of JASMAL:

As highlighted on the Github repository, JASMAL

  • can handle both real and complex data
  • supports manipulate ndarrays (just like NumPy)
  • supports broadcasting for most of the binary operations
  • supports a flexible indexing/slicing scheme similar to that of NumPy
  • has more than 100 operations implemented

Similar to MATLAB and NumPy, you can create new matrices via zeros(), ones(), eye(), or from a JavaScript array:

let A = T.zeros([3, 4]);
let B = T.ones([4, 4]);
let C = T.eye(5);
let D = T.fromArray([[1, 2], [3, 4]]);

Here T is the JASMAL instance. Indexing and slicing is very similar to that of NumPy:

let A = T.zeros([3, 3]);
A.set(0, 1, 10); // Equivalent to A[0, 1] = 10 in NumPy
A.get('::-1', ':'); // Equivalent to A[::-1, :] in NumPy. Reverses all the rows.
A.set([0, -1], ':', 8); // Sets the first and the last row to 8. 

Note:Because JavaScript does not support overloading indexers, strings representations are used here. Similarly, because JavaScript does not support operator overloading, using JASMAL is more cumbersome than NumPy or MATLAB. For instance, you have to use T.matmul(A, T.add(B, C)) instead of A * (B + C).

JASMAL provides a large collection of functions for

  • manipulating ndarrays (e.g., reshape(), tile(), permuteAxis)
  • random number generation (e.g., randn(), unifrnd())
  • math operations for both real and complex numbers (e.g. sin(), log(), acosh())
  • matrix operations for both real and complex matrices (e.g., matmul(), diag(), det())
  • matrix decompositions for both real and complex matrices (e.g., svd(), eig(), qr())
  • statistics (e.g., mean(), var(), hist())
  • set operations (e.g., setdiff(), union())
  • polynomials (e.g., polyval(), roots())
  • ...

For a complete list of references, you are welcomed to check out my Github. I hope you find JASMAL useful :)


  1. Probably should be called Just Another javaScript MAtrix Library.

  2. It should also work on most modern mobile devices. It even works on my old Windows phone.