matrix-reef.js
v0.5.5
Published
Fast Matrix Library for Java Script
Downloads
11
Readme
A faster Matrix for the Java Script World.
There are some awesome matrix libraries for Java Script in the wild. But if you are looking for something faster consider using this library.
Benchmarks
Some Matrix multiplication benchmarks:
| | 2x2 | 3x3 | 4x4 | 16x16 | 32x32 | 256x256 | 512x512 | |----------------|:---------:|:---------:|:-------:|:------:|:-----:|:-------:|:-------:| | Matrix-reef JS | 1,787,425 | 1,557,114 | 869,244 | 45,497 | 6,312 | 16.06 | 1.92 | | ml-Matrix | 188,606 | 156,872 | 127,508 | 15,417 | 5,319 | 15.76 | 1.89 | | Math.js | 129,336 | 101,767 | 63,528 | 3,574 | 553 | 0.92 | 0.08 |
# of operations per second. See How to run the benchmarks for details.
Disclaimer
Matrix-reef.js is still in its early stages. Some must-to-do features are not available, there is no docs, tutorials and no TypeScript. Thus, I'm afraid that Matrix-reef.js is not ready for production yet.
How to use
const A = new Matrix([
[1, 2],
[3, 4],
[5, 6]
]); // create a 3x2 matrix
const B = new Matrix([
[2, 1],
[2, 0]
]); // create a 2x2 matrix
const C = A.multiply(B);
See examples below for a complete list of functionalities and demonstration of how to use them.
How to install
Node.js
$ npm install matrix-reef.js
and include
const { Matrix } = require('matrix-reef.js');
CDN
<script src="https://cdn.jsdelivr.net/npm/[email protected]/index.min.js"></script>
API by examples
- creating: constructors, zeros, ones, identity, diagonal and cloning.
- accessing: get, set, row, col, diagonal and path.
- arithmetics: add, subtract, add scalar, add row to matrix and add column to matrix.
- multiplication: multiply by scalar, dot multiply and matrix multiplication.
- functional: map, max, min, sum, product, rowWise, colWise and chaining calls.
- matrices: dimension, rows, cols, transpose, determinant, inverse, reshape, flatten, rotate90, rotate180 and rank.
- query: isSquared, isDiagonal, isUpperTriangular and isLowerTriangular.
- memory saving: in-place operations, slices and views.
- optimization: Matrix2D, Matrix3D, Matrix4D, Vector, RowVector, ColumnVector, Vector2D, Vector3D, Vector4D and Sparse Matrix.
- miscellaneous: print out, convert to array, shuffle and comparing.
Real world examples
This example shows how to train a neural network using the Iris dataset and only add
, multiply
, dotMultiply
and transpose
:
$ node examples/training.ann.js
Should output:
Elapsed time: 0.76 secs for 5000 epochs.
How to run the benchmarks
The benchmark code is here. This program uses Benchmark.js to compare the matrix-multiplication performance of Matrix-reef.js and two other Java Script Matrix implementations: Math.js and ml-Matrix. Execute the following command to run a benchmark yourself:
node benchmarks/multiplication.benchmark.js 10 4 10 5
The command above executes chunks of 10 4x10
by 10x5
matrix multiplications. Ps.: Use --max-old-space-size=4096
when running huge matrices.
New features, bugs and collaboration
Do not hesitate to open a new issue whenever you find something wrong or does not working as expected. Suggestions of new features are pretty welcome as well as collaborations by PR test cases, benchmarks or even new features.