
Security News
How Enterprise Security Is Adapting to AI-Accelerated Threats
Socket CTO Ahmad Nassri discusses why supply chain attacks now target developer machines and what AI means for the future of enterprise security.
Multidimensional arrays for JavaScript. For more discussion regarding the technical details see the following blog posts:
ndarrays provide higher dimensional views of 1D arrays. For example, here is how you can turn a length 4 typed array into an nd-array:
var mat = ndarray(new Float64Array([1, 0, 0, 1]), [2,2])
//Now:
//
// mat = 1 0
// 0 1
//
Once you have an nd-array you can access elements using .set and .get. For example, here is an implementation of Conway's game of life using ndarrays:
function stepLife(next_state, cur_state) {
//Loop over all cells
for(var i=1; i<cur_state.shape[0]-1; ++i) {
for(var j=1; j<cur_state.shape[1]-1; ++j) {
//Count neighbors
var n = 0
for(var dx=-1; dx<=1; ++dx) {
for(var dy=-1; dy<=1; ++dy) {
if(dx === 0 && dy === 0) {
continue
}
n += cur_state.get(i+dx, j+dy)
}
}
//Update state according to rule
if(n === 3 || n === 3 + cur_state.get(i,j)) {
next_state.set(i,j,1)
} else {
next_state.set(i,j,0)
}
}
}
}
You can also pull out views of ndarrays without copying the underlying elements. Here is an example showing how to update part of a subarray:
var x = ndarray(new Float32Array(25), [5, 5])
var y = x.hi(4,4).lo(1,1)
for(var i=0; i<y.shape[0]; ++i) {
for(var j=0; j<y.shape[1]; ++j) {
y.set(i,j,1)
}
}
//Now:
// x = 0 0 0 0 0
// 0 1 1 1 0
// 0 1 1 1 0
// 0 1 1 1 0
// 0 0 0 0 0
Install the library using npm:
npm install ndarray
You can also use ndarrays in a browser with any tool that follows the CommonJS/node module conventions. The most direct way to do this is to use browserify. If you want live-reloading for faster debugging, check out beefy.
Once you have ndarray installed, you can use it in your project as follows:
var ndarray = require("ndarray")
ndarray(data[, shape, stride, offset])The default module.exports method is the constructor for ndarrays. It creates an n-dimensional array view wrapping an underlying storage type
data is a 1D array storage. It is either an instance of Array, a typed array, or an object that implements get(), set(), .lengthshape is the shape of the view (Default: data.length)stride is the resulting stride of the new array. (Default: row major)offset is the offset to start the view (Default: 0)Returns an n-dimensional array view of the buffer
The central concept in ndarray is the idea of a view. The way these work is very similar to SciPy's array slices. Views are affine projections to 1D storage types. To better understand what this means, let's first look at the properties of the view object. It has exactly 4 variables:
array.data - The underlying 1D storage for the multidimensional arrayarray.shape - The shape of the typed arrayarray.stride - The layout of the typed array in memoryarray.offset - The starting offset of the array in memoryKeeping a separate stride means that we can use the same data structure to support both row major and column major storage
To access elements of the array, you can use the set/get methods:
array.get(i,j,...)Retrieves element i,j,... from the array. In psuedocode, this is implemented as follows:
function get(i,j, ...) {
return this.data[this.offset + this.stride[0] * i + this.stride[1] * j + ... ];
}
array.set(i,j ..., v)Sets element i,j,... to v. Again, in psuedocode this works like this:
function set(i,j, ..., v) {
return this.data[this.offset + this.stride[0] * i + this.stride[1] * j + ... ] = v;
}
The following properties are created using Object.defineProperty and do not take up any physical memory. They can be useful in calculations involving ndarrays
array.dtypeReturns a string representing the undelying data type of the ndarray. Excluding generic data stores these types are compatible with typedarray-pool. This is mapped according to the following rules:
| Data type | String |
|---|---|
Int8Array | "int8" |
Int16Array | "int16" |
Int32Array | "int32" |
Uint8Array | "uint8" |
Uint16Array | "uint16" |
Uint32Array | "uint32" |
Float32Array | "float32" |
Float64Array | "float64" |
Array | "array" |
| Other | "generic" |
Generic arrays access elements of the underlying 1D store using get()/set() instead of array accessors.
array.sizeReturns the size of the array in logical elements.
array.orderReturns the order of the stride of the array, sorted in ascending length. The first element is the first index of the shortest stride and the last is the index the longest stride.
Given a view, we can change the indexing by shifting, truncating or permuting the strides. This lets us perform operations like array reversals or matrix transpose in constant time (well, technically O(shape.length), but since shape.length is typically less than 4, it might as well be). To make life simpler, the following interfaces are exposed:
array.lo(i,j,k,...)This creates a shifted view of the array. Think of it as taking the upper left corner of the image and dragging it inward by an amount equal to (i,j,k...).
array.hi(i,j,k,...)This does the dual of array.lo(). Instead of shifting from the top-left, it truncates from the bottom-right of the array, returning a smaller array object. Using hi and lo in combination lets you select ranges in the middle of an array.
Note: hi and lo do not commute. In general:
a.hi(3,3).lo(3,3) != a.lo(3,3).hi(3,3)
array.step(i,j,k...)Changes the stride length by rescaling. Negative indices flip axes. For example, here is how you create a reversed view of a 1D array:
var reversed = a.step(-1)
You can also change the step size to be greater than 1 if you like, letting you skip entries of a list. For example, here is how to split an array into even and odd components:
var evens = a.step(2)
var odds = a.lo(1).step(2)
array.transpose(p0, p1, ...)Finally, for higher dimensional arrays you can transpose the indices in place. This has the effect of permuting the shape and stride values. For example, in a 2D array you can calculate the matrix transpose by:
M.transpose(1, 0)
Or if you have a 3D volume image, you can shift the axes using more generic transformations:
volume.transpose(2, 0, 1)
array.pick(p0, p1, ...)You can also pull out a subarray from an ndarray by fixing a particular axis. The way this works is you specify the direction you are picking by giving a list of values. For example, if you have an image stored as an nxmx3 array you can pull out the channel as follows:
var red = image.pick(null, null, 0)
var green = image.pick(null, null, 1)
var blue = image.pick(null, null, 2)
As the above example illustrates, passing a non-numeric value to a coordinate in pick skips that index.
(c) 2013 Mikola Lysenko. MIT License
Numjs is a library that provides similar functionality to ndarray, offering a NumPy-like API for JavaScript. It supports operations on multi-dimensional arrays and matrices, and is designed for scientific computing. Compared to ndarray, numjs provides a higher-level API that is more similar to NumPy, making it easier for users familiar with Python's NumPy library.
Math.js is a comprehensive math library for JavaScript and Node.js. It includes support for complex numbers, matrices, and multi-dimensional arrays. While it offers a broader range of mathematical functions compared to ndarray, it is also more complex and may be overkill for users who only need basic multi-dimensional array manipulation.
SciJS is a collection of scientific computing libraries for JavaScript. It includes modules for linear algebra, statistics, and multi-dimensional array manipulation. SciJS is more modular compared to ndarray, allowing users to pick and choose the specific functionalities they need. However, it may require more effort to integrate the different modules compared to using a single library like ndarray.
FAQs
Multidimensional Arrays
The npm package ndarray receives a total of 531,716 weekly downloads. As such, ndarray popularity was classified as popular.
We found that ndarray demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 6 open source maintainers collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Socket CTO Ahmad Nassri discusses why supply chain attacks now target developer machines and what AI means for the future of enterprise security.

Security News
Learn the essential steps every developer should take to stay secure on npm and reduce exposure to supply chain attacks.

Security News
Experts push back on new claims about AI-driven ransomware, warning that hype and sponsored research are distorting how the threat is understood.