Repo of the Day
nicolaspanel/numjs
Published: Jan 19, 2024
Open repository ↗Like NumPy, in JavaScript
Summary
numjs is a JavaScript port of NumPy's core ideas, providing an N-dimensional array object (NdArray) along with linear algebra, universal math functions, FFT, convolution, and basic image processing. It works in both Node.js and the browser, is MIT-licensed, and builds on the scijs ndarray package.
What it is useful for
The library is useful when you need to do numerical work without leaving JavaScript. Concrete scenarios the README supports:
- Multi-dimensional data handling in Node.js, with typed dtypes (
int8,uint8,int16,uint16,int32,uint32,float32,float64) or the default JSArray. - Browser image pipelines via
nj.images.read,nj.images.write, andnj.images.resize, with images represented as[H,W]or[H,W,3]arrays. - Signal processing using
nj.fft,nj.ifft, andnj.convolve(the README notesconvolveuses FFT internally for large arrays). - Element-wise math functions like
nj.exp,nj.softmax,nj.sigmoid,nj.sin, andnj.sqrt, plus reductions such as.sum(),.mean(),.std(),.min(),.max().
It is not a drop-in for high-performance scientific Python: the README makes no performance claims and the default storage is plain JS arrays.
How engineers can use it
Install on Node.js (documented):
npm install numjs
var nj = require('numjs');
Install in the browser (documented):
bower install numjs
<script src="https://cdn.jsdelivr.net/gh/nicolaspanel/[email protected]/dist/numjs.min.js"></script>
Working with arrays:
// Create a 3x4 matrix from a range and reshape
var a = nj.arange(12).reshape(3, 4);
// Matrix multiplication
nj.dot(a.T, a);
// Element-wise arithmetic returns a new array
var ones = nj.ones([3, 4]);
ones.add(ones); // [[2, 2, 2, 2], ...]
// Slice with [start, stop, step] semantics
var b = nj.arange(25).reshape(5, 5);
b.slice(null, [null, null, -1]); // reverse each row, like b[:, ::-1]
Image processing:
var img = nj.images.data.lenna; // built-in sample
var resized = nj.images.resize(img, 14, 12);
Documented limitations to be aware of:
- Slices are views, not copies. Use
.clone()for an independent array. convolveonly supports "valid" mode, where signals fully overlap; values outside the signal boundary are ignored.fftandifftexpect the last axis to be size 2 (real and imaginary components).concatenaterequires arrays with matching shape outside the joining axis.
The README points to the numjs globals and NdArray documentation for the full API.