Besides its obvious scientific uses, NumJs can also be used as an efficient multi-dimensional container of generic data.
It works both in node.js and in the browser (with or without browserify)
NumJs is licensed under the MIT license, enabling reuse with almost no restrictions.
See this jsfiddle for a concrete example of how to use the library to manipulate images in the browser.
npm install numjs
var nj = ;...
bower install numjs
> var a = nj;> a> var b = nj;> b
Array object. If needed, you can also use typed array such as
> var a = nj;> a
Note: possible types are int8, uint8, int16, uint16, int32, uint32, float32, float64 and array (the default)
To create arrays with a given shape, you can use
> nj;> nj // dtype can also be specified> nj
To create sequences of numbers, NumJs provides a function called
> nj;> nj> nj;
NumJs’s array class is called
NdArray. It is also known by the alias
array. The more important properties of an
NdArray object are:
NdArray#ndim: the number of axes (dimensions) of the array.
NdArray#shape: the dimensions of the array. This is a list of integers indicating the size of the array in each dimension. For a matrix with n rows and m columns, shape will be [n,m]. The length of the shape is therefore the number of dimensions, ndim.
NdArray#size: the total number of elements of the array. This is equal to the product of the elements of shape.
NdArray#dtype: a string describing the type of the elements in the array.
float64are some examples. Default dtype is
> a = nj;> ashape3 5> andim2> adtype'array'> a instanceof njNdArraytrue> a instanceof Arraytrue> a6> a> a
When you print an array, NumJs displays it in a similar way to nested lists, but with the following layout:
One-dimensional arrays are then printed as rows, bidimensionals as matrices and tridimensionals as lists of matrices.
> var a = nj; // 1d array> console;>> var b = nj; // 2d array> console;>> var c = nj; // 3d array> console;
If an array is too large to be printed, NumJs automatically skips the central part of the array and only prints the corners:
To customize this behaviour, you can change the printing options using
nj.config.printThreshold (default is
> njconfigprintThreshold = 9;> console
Single element indexing uses
set methods. It is 0-based, and accepts negative indices for indexing from the end of the array:
> var a = nj;> a1>> a2>> var b = nj;> b>> b;4>> b;8> b;> b
It is possible to slice and stride arrays to extract arrays of the same number of dimensions, but of different sizes than the original. The slicing and striding works exactly the same way it does in NumPy:
> var a = nj;> a>> a // skip the first item, same as a[1:]>> a // takes the last 3 items, same as a[-3:]>> a // takes the first 4 items, same as a[:4]>> a // skip the last 2 items, same as a[:-2]>> a // same as a[1:4]>> a // same as a[1:4:-1]>> a // same as a[::-1]>> var b = nj;> b>> b // skip the first row and the 2 first columns, same as b[1:,2:]>> b // reverse rows, same as b[:, ::-1]
Note that slices do not copy the internal array data, it produces a new views of the original data.
Arithmetic operators such as
assign) apply elemen-twise. A new array is created and filled with the result:
> zeros = nj;>> ones = nj;>> ones>> ones>> zeros>
To modify an existing array rather than create a new one you can set the
copy parameter to
> ones = nj;>> ones>> ones>> zeros = nj> zeros;> zeros
Note: available for
The matrix product can be performed using the
> a = nj;>> nj>> nj
Many unary operations, such as computing the sum of all the elements in the array, are implemented as methods of the
> a = nj> a31126488673035055>> a02138431086204946>> a08278025290928781>> a05187748112172509>> a022216977543691244
NumJs provides familiar mathematical functions such as
exp. These functions operate element-wise on an array, producing an
NdArray as output:
> a = nj>> nj>> nj>> nj>> nj>> nj>> nj>> nj>> nj>> nj>> nj>> nj>> nj
An array has a shape given by the number of elements along each axis:
> a = nj;> ashape3 4
The shape of an array can be changed with various commands:
> a;>> aT // equivalent to a.transpose(1,0)>> a>
a is matrix we may want its diagonal:
The identity array is a square array with ones on the main diagonal:
Several arrays can be stacked together using
> a = nj>> b = nj>> nj
It is still possible to concatenate along other dimensions using transpositions:
> a = nj>> b = nj>> njT
> a = nj> b = nj> np> np
clone method makes a complete copy of the array and its data.
> a = nj>> b = a>> a === bfalse>> a> a> b
ifft functions can be used to compute the N-dimensional discrete Fourier Transform and its inverse.
> RI = nj>> fft = nj>> nj
ifft expect last dimension of the array to contain 2 values: the real and the imaginary value
convolve function compute the discrete, linear convolution of two multi-dimensional arrays.
Note: The convolution product is only given for points where the signals overlap completely. Values outside the signal boundary have no effect. This behaviour is also known as the 'valid' mode.
> x = nj>> nj>> var a = nj> a> nj> nj
convolve uses Fast Fourier Transform (FFT) to speed up computation on large arrays.
> m = nj> m> nj> nj> m = nj> nj
NumJs’s comes with powerful functions for image processing. Theses function are located in
The different color bands/channels are stored using the
NdArray object such that a grey-image is
[H,W], an RGB-image is
[H,W,3] and an RGBA-image is
nj.images.resize functions to (respectively) read, write or resize images.
> njconfigprintThreshold = 28;>> var img = njimagesdatadigit; // WARN: this is a property, not a function. See also `nj.images.data.moon`, `nj.images.data.lenna` and `nj.images.data.node`>> img> var resized = njimages>> resizedshape14 12>> resized
See also this jsfiddle for more details on what is possible from the browser.