JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • 0
  • Score
    100M100P100Q26392F
  • License MIT

Like NumPy, in JavaScript

Package Exports

  • num4js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (num4js) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Num4JS is a npm/bower package for scientific computing with JavaScript. It contains among other things:

  • a powerful N-dimensional array object
  • linear algebra function
  • fast Fourier transform
  • image manipulation capabilities

It works both in node.js and in the browser (with or without browserify)

Getting started

on node.js

npm install num4js

on the browser

bower install num4js
<script src="bower_packages/lodash/lodash.min.js"></script>
<script src="bower_packages/num4js/num4js.min.js"></script>

Basics

> var nj = require('num4js') // for nodejs only

// array manipulations
> var a = nj.arange(15).reshape(3, 5)
> a
array([[  0,  1,  2,  3,  4],
       [  5,  6,  7,  8,  9],
       [ 10, 11, 12, 13, 14]])

> a.shape
[ 3, 5]
> a.ndim
2
> a.dtype
'array'
> a instanceof nj.NdArray
true
> a.tolist() instanceof Array
true
> a.get(1,1)
6
> a.set(0,0,1)
> a
array([[  1,  1,  2,  3,  4],
       [  5,  6,  7,  8,  9],
       [ 10, 11, 12, 13, 14]])


// array types
> var b = nj.array([0,1,2], nj.dtypes.uint8)
> b
array([ 0, 1, 2], dtype=uint8)
// NOTES: possible types are int8, uint8, int16, uint16, int32, uint32, float32, float64 and array (default)


// Operations
> var zeros = nj.zeros([3,4])
> zeros
array([[ 0, 0, 0, 0],
       [ 0, 0, 0, 0],
       [ 0, 0, 0, 0]])
> var ones = nj.ones([3,4])
> ones
array([[ 1, 1, 1, 1],
       [ 1, 1, 1, 1],
       [ 1, 1, 1, 1]])
> nj.equal(zeros.add(1), ones)
true
> nj.equal(ones.multiply(-1), ones.negative())
true
> nj.dot(a.T, a)
array([[ 3, 3, 3, 3],
       [ 3, 3, 3, 3],
       [ 3, 3, 3, 3],
       [ 3, 3, 3, 3]])

> var c = nj.arange(16).reshape(4,4)
> c
array([[  0,  1,  2,  3],
       [  4,  5,  6,  7],
       [  8,  9, 10, 11],
       [ 12, 13, 14, 15]])


// Slicing and selections
> var selection = c.lo(1,1).hi(2,2)
> selection
array([[  5,  6],
       [  9, 10]])

> selection.add(1) // create a new array
array([[  6,  7],
       [ 10, 11]])
> selection
array([[  5,  6],
       [  9, 10]])

> selection.add(1, false) // do NOT create a new Array, modify selection's data instead
> selection
array([[  6,  7],
       [ 10, 11]])
> selection.T
array([[  6, 10],
       [  7, 11]])

> c  // since the selection shares its data with c, c has changed too
array([[  0,  1,  2,  3],
       [  4,  6,  7,  7],
       [  8, 10, 11, 11],
       [ 12, 13, 14, 15]])

> selection.pick(1)
array([ 6, 7])

> selection.pick(null, 1)
array([ 7, 11])

Doc

See documentation

TODO

  • Support broadcasting for additions and multiplications
  • Remove lodash dependency

Credits

Num4JS is built on top of ndarray and uses many scijs packages