McNumJS
McNumJS is a high-performance JavaScript library for numerical computations which provides an easy-to-use API like NumPy. McNumJS makes use of TypedArrays which provide a mechanism for accessing data much more efficiently. McNumJS uses implicitly typed objects specified by asm.js so that modern javascript engines can optimize the code more efficiently and provide higher performance. Compiler writers who target javascript language and other web developers or scientists can make use of this library for numeric computations on the web.
McNumJS Modules
Module | Description | Example properties/methods |
---|---|---|
Core | Extends JavaScript typed arrays to provide multi-dimension support | shape, stride, size, get, set, index, map |
Generation | Provides API to generate different matrices | zeroes, ones, random |
Unary Operations | Creates Unary operation functions | sum, average, sin, cos, fill |
Binary Operations | Creates Binary operation functions | add, subtract, multiply, divide |
Examples
- Core Module
// constructor: Int32Array(object, shape) | |
var intView = new Int32Array([1,2,3,4,5,6], [2,3]); | |
// 2x3 Int32 Matrix default row-major | |
/** 1 2 3 | |
* 4 5 6 | |
* => [1, 2, 3, 4, 5, 6] | |
*/ | |
console.log(intView.size); // 6 | |
console.log(intView.shape); // [2, 3] | |
console.log(intView.stride); // [3, 1] | |
console.log(intView.get(0,1)); // 2 | |
// index: 0*3+1*1 = 1 | |
var intView2 = intView.clone().reshape(3,2); | |
/** 1 2 | |
* 3 4 | |
* 5 6 | |
* => [1, 2, 3, 4, 5, 6] | |
*/ | |
- Generation Module
// mn.zeros(shape [, className]); | |
var z = mn.zeros([6, 6]); | |
// Float64Array of shape 6x6 | |
// mn.ones(shape [, className]); | |
var o = mn.ones([6, 6], Int32Array); | |
// Int32Array of shape 6x6 filled with 1 | |
// mn.rand(shape [, className]); | |
var r = mn.rand([6, 6], Int32Array); | |
// Int32Array of shape 6x6 filled with random numbers | |
// mn.linspace(start, stop [, n [, className]]); | |
var l = mn.linspace(0, 99); | |
// Float64Array of size 100 filled 0 ... 99 | |
// mn.range(start, stop [, step, [className]]); | |
var rn = mn.range(10, 1); | |
// Float64Array of size 10 filled 10 ... 1 | |
// mn.identity(N [, className]); | |
var i = mn.identity(5, Uint8Array); | |
// Identity matrix of type Uint8Array and shape 5x5 |
- Unary / Binary Operations
mn.fill(matrix, value); | |
// Fills the matrix with given value | |
mn.cos(matrix); | |
// Point-vise cosine of elements. | |
var s = mn.sum(matrix); | |
// Sum of all matrix elements | |
var t = mn.transpose(matrix); | |
// Transpose of the matrix. | |
// This function does not return a new | |
// copy of data but changes the shape | |
// and stride of the array. | |
var out = mn.add(in1, in2); | |
// pointwise addition of in1 and in2 matrices |
Performance
- McnumJS vs. Regular JavaScript: (Higher is better)
- McNumJS vs. JavaScript with typed arrays: (Lower is better)
- McNumJS vs. Compiled asm.js: (Lower is better)
- McNumJS vs. C: (Lower is better)
Development
For the development, you will need to install following dependencies.
Run following command to install other dependencies:
npm install
To build and test the library, run following command:
grunt && grunt test