Home

Awesome

ndarray-ops

A collection of common mathematical operations for ndarrays. Implemented using cwise

Usage

First, install the library using npm:

npm install ndarray-ops

Then you can import the library by doing:

var ops = require("ndarray-ops")

Then you can use the functions as in the following example:

//First, import libraries
var ndarray = require("ndarray")
  , ops = require("ndarray-ops")


//Next, create some arrays
var a = ndarray(new Float32Array(128*128))
  , b = ndarray(new Float32Array(128*128))
  , c = ndarray(new Float32Array(128*128))

//Initialize b with some random numbers:
ops.random(b)

//Set c to a constant 1
ops.assigns(c, 1.0)

//Add b and c, store result in a:
ops.add(a, b, c)

//Multiply a by 0.5 in place
ops.mulseq(a, 0.5)

//Print some statistics about a:
console.log(
  "inf(a) = ", ops.inf(a),
  "sup(a) = ", ops.sup(a),
  "argmin(a) = ", ops.argmin(a),
  "argmax(a) = ", ops.argmax(a),
  "norm1(a) = ", ops.norm1(a))

Conventions

This library implements component-wise operations for all of the operators and Math.* functions in JS, along with a few commonly used aggregate operations. Most of the functions in the library work by applying some symmetric binary operator to a pair of arrays. You call them like this:

ops.add(dest, arg1, arg2)

Which translates into code that works (approximately) like this:

for(var i=0; i<dest.shape[0]; ++i) {
  dest[i] = arg1[i] + arg2[i]
}

It is up to you to specify where the result gets stored. This library does not create new arrays for you to avoid performing expensive intermediate allocations. There are also a few other variations:

ops.addeq(dest, arg1)

Operators with the -eq suffix perform an assignment.

for(var i=0; i<dest.shape[0]; ++i) {
  dest[i] += arg1[i]
}
ops.adds(dest, arg1, 1.0)

The -s suffix denotes scalar/broadcast operations; so the above would translate to:

for(var i=0; i<dest.shape[0]; ++i) {
  dest[i] = arg1[i] + 1.0
}
ops.addseq(dest, 1.0)

The -seq suffix is basically the combination of the above, and translates to:

for(var i=0; i<dest.shape[0]; ++i) {
  dest[i] += 1.0
}

The following operators follow this rule:

Special Cases

There are a few corner cases that follow slightly different rules. These can be grouped using the following general categories:

Assignment

There are two assignment operators:

ops.assign(dest, src) copies one array into another, while ops.assigns(dest, val) broadcasts a scalar to all elements of an array.

Nullary operators

Nullary operators only take on argument for the array they are assigning to, and don't have any variations. Currently there is only one of these:

Unary operators

Unary operators have one of two forms, they can be written as either:

ops.abs(dest, arg)

Or:

ops.abseq(dest)

The former version sets dest = |arg|, while in the latter the operation is applied in place. ndarray-ops exposes the following unary operators:

Non-symmetric binary operators

There are also a few non-symmetric binary operators. These operators have an extra suffix op which flips the order of the arguments. There are only two of these:

Map-reduce (aggregate) operators

Finally, there are aggregate operators that take an array as input and compute some aggregate result or summary. These functions don't have any special suffixes and all of them take a single array as input.

Credits

(c) 2013 Mikola Lysenko. MIT License