Git Product home page Git Product logo

nd4js's Introduction

Introduction

nd4js is a lightweight JavaScript library for ND-Arrays including some optimization functionality and one of the most complete linear algebra modules for the web. It is strongly inspired by NumPy. There are, however, some key differences. Broadcasting, slicing and reshape work in a similar way as in NumPy. Instead of the predefined operations (+, -, *, /, sin, cos, ...), nd4js relies on functional-style map- and zip-like methods.

The goal of this library is to explore and improve the author's understanding of linear algebra, optimization and computational numerics in general. The author is therefore constantly working on the edge of his knowledge. Despite his best efforts, no guarantees can be made about performance, numeric accuracy, reproducability or anything at all. There will be bugs, underflows, overflows, NaNs, memory issues and the like. If You find a bug - and don't want to keep it - feel free file an issue or a PR.

A function reference can be found here.

Installation

npm i nd4js

Building and Testing

nd4js is built and tested using NPM. To initialize the project open the command line, navigate to the project directory and call:

npm i

To cover as many test cases as possible with little effort, nd4js mostly uses randomized testing instead of hand crafted test cases. As a result, testing the entire project takes rather long (~50 minutes). If You want to run only a subset of the tests during development, change the glob pattern of the file setting inside of karma.conf.js, e.g. use 'src/la/**/*_test.js' instead of 'src/**/*_test.js' to test linear algebra methods only.

In order to run the tests call:

npm run test

To build/bundle the library, call:

npm run build

nd4js has some development dependencies, most notably Babel and Webpack for bundling and Jasmine for testing. There are however no deployment dependencies as of yet.

Array Instantiation

nd.array allows to create NDArray instances in a well-readable and intuitive way, using nested JavaScript Arrays.

const nd = require('nd4js')

const a = nd.array([
  [1,0,0],
  [0,2,0],
  [0,0,3]
])

An NDArray can also be created from its entries' indices using nd.tabulate.

Input:

const a = nd.tabulate([1000,1000], (i,j) => i==j ? 1 : 0 )
console.log( a.toString() )

Output:

[[ 1,  0,  0,  0,  0,  ...990 more...,  0,  0,  0,  0,  0],
 [ 0,  1,  0,  0,  0,  ...990 more...,  0,  0,  0,  0,  0],
 [ 0,  0,  1,  0,  0,  ...990 more...,  0,  0,  0,  0,  0],
 [ 0,  0,  0,  1,  0,  ...990 more...,  0,  0,  0,  0,  0],
 [ 0,  0,  0,  0,  1,  ...990 more...,  0,  0,  0,  0,  0],
  ...990 more...,
 [ 0,  0,  0,  0,  0,  ...990 more...,  1,  0,  0,  0,  0],
 [ 0,  0,  0,  0,  0,  ...990 more...,  0,  1,  0,  0,  0],
 [ 0,  0,  0,  0,  0,  ...990 more...,  0,  0,  1,  0,  0],
 [ 0,  0,  0,  0,  0,  ...990 more...,  0,  0,  0,  1,  0],
 [ 0,  0,  0,  0,  0,  ...990 more...,  0,  0,  0,  0,  1]]

Random Access

Since nd.Array extends Function, elements can be read by calling the array object.

Input:

const a = nd.array([
  [1,2],
  [3,4]
])

console.log( a(0,0) )
console.log( a(0,1) )
console.log( a(1,0) )
console.log( a(1,1) )

Output:

1
2
3
4

nd.NDArray.set allows writing array entries.

Input:

const a = nd.tabulate([3,3], () => 0 )

for( let i=3; i-- > 0; )
for( let j=3; j-- > 0; )
  a.set( [i,j], 10*(i+1)+(j+1) );

console.log( a.toString() );

Output:

[[ 11, 12, 13 ],
 [ 21, 22, 23 ],
 [ 31, 32, 33 ]]

If array elements are to be modified, nd.NDArray.modify is a concise alternative to using nd.NDArray.get and nd.NDArray.set.

Input:

const a = nd.array([[1, 2, 3],
                    [4, 5, 6]])

a.modify([0,1], x => 7*x)

console.log( a.toString() );

Output:

[[  1, 14,  3 ],
 [  4,  5,  6 ]]

Unary Operations (sin, cos, exp, ...)

nd.NDArray.mapElems is used to apply unary operations on an NDArray.

Input:

const a = nd.array([
  [1,2],
  [3,4]
])

const b = a.mapElems( (a_ij, i,j) => i==j ? a_ij : a_ij*a_ij )
console.log( b.toString() )

Output:

[[         1,          4],
 [         9,          4]]

Binary Operations (+, -, *, /, ...)

nd.zip_elems can be used to apply binary operations on two NDArrays.

Input:

const
  a = nd.array([
    [11,12,13],
    [21,22,23],
    [31,32,33]
  ]),
  b = nd.array([
    [1,2,3],
    [4,5,6],
    [7,8,9]
  ])

const c = nd.zip_elems([a,b], (a_ij,b_ij, i,j) => i==j ? a_ij : b_ij )
console.log( c.toString() )

Output:

[[        11,          2,          3],
 [         4,         22,          6],
 [         7,          8,         33]]

nd.zip_elems supports NumPy-style broadcasting.

Input:

const
  a = nd.array([
    [1],
    [2],
    [3]
  ]),
  b = nd.array([1,2,3])

const c = nd.zip_elems([a,b], (x,y) => 10*x + y )
console.log( c.toString() )

Output:

[[        11,         12,         13],
 [        21,         22,         23],
 [        31,         32,         33]]

Ternary Operations (?:, ...)

nd.zip_elems can also be used for any n-ary operation, such as ternary conditional operator in JavaScript.

Input:

const
  flags = nd.array([true, false, false, true]),
  a = nd.array([
    [10],
    [20],
    [30],
    [40]
  ]),
  b = nd.array([1,2,3,4])

const c = nd.zip_elems([flags,a,b], (f,x,y) => f ? x : y )
console.log( c.toString() )

Output:

[[        10,          2,          3,         10],
 [        20,          2,          3,         20],
 [        30,          2,          3,         30],
 [        40,          2,          3,         40]]

Linear Algebra

nd4js now offers a fairly wide variety of Linear Algebra operations in the nd.la subpackage. la.matmul computes the matrix product of two or more matrices. The order of multiplication is automatically optimized to minimize the number of floating point operations.

Input:

const v = nd.array([[1,2,-3]]).T,
      A = nd.array([
        [1,2,3],
        [4,5,6],
        [7,8,9]
      ]);

console.log( nd.la.matmul(v.T, A, v) );

Output:

[[ 144 ]]

Available operations and decompositions:

Optimization

nd4js offers the following linear and nonlinear optimization methods and solvers:

nd4js's People

Contributors

dirktoewe avatar fil avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

Forkers

shalevy1 foo123

nd4js's Issues

PCA?

Hello,

I'm trying to do PCA with nd.la.svd_decomp on a list of n arrays of p values (each of the p dimensions are centered by substracting the mean, and optionally rescaled). This returns A == U @ diag(sv) @ V where V contains the vectors in the PCA dimensions (PC0, PC1, …) ordered by eigenrank. So far so good, for the learning phase.

But now I'd like to project new data points onto the learned PCA dimensions; to the best of my understanding, this would require inverting U? But I couldn't find a nd.la.matrix_invert method :) Unless there is a shortcut somewhere?

thanks!

Automated Testing

Testing on different platforms and browsers is a good idea, so is automatically testing pushes and pull requests. Travis CI offers free capacities for open source projects. Setting up automated testing should be one of the next steps for ND4JS.

The following issues with testing need to be fixed first:

  • Testing must be faster
  • Testing must be reproducible (e.g. via seedrandom)

Improve & Rethink Documentation

One of the many amazing features of Python is the way it handles docstrings. Documentation is not just available as generated (HTML) documents. It is also available at runtime, e.g. by using the help function, not unlike the --help argument offered by most Linux command line tools.

The docstring functionality is one of the key features that I miss most in JS, which is why I've tried to emulate it by creating the documentation at runtime inside of a single help.js module. It seemed like a good idea at the time given the pros:

  • No need to learn JSDoc and JSDoc's conventions
  • Documentation was available at runtime
  • Documentation was concentrated in a single place.

With increasing size of the ND4JS library, the downsides of this approach become more and more appearent:

  • Creating structured, good looking docs without something like JSDocs is a lot of work
  • Centralized documentation of increasing number of modules lacks clarity.

Which leads to this issue: The way of documentation needs to be rethought. The best solution is likely JSDoc. Maybe the jsdoc-api can be used to still generate runtime docstrings.

lstsq produces incorrect result

The following code snippet illustrates an incorrect result for the la.lstsq method.

The least squares solution should obviously be [[1], [1]], and not [[0], [0]].

const nd = require('nd4js');

const a = nd.array([
    [1, 1],
    [1, 2],
    [1, 3],
    [1, 4],
    [1, 5],
    [1, 6]
]);

const b = nd.array([
    [2],
    [3],
    [4],
    [5],
    [6],
    [7]
]);

const x = nd.la.lstsq(a, b);

console.log(x);

Publish to npm?

I just stumpled upon this library and it looks quite mature and capable.

It does not seem to be published to the public npm registry, however. Any intention of doing so?

I can see there is a .npmignore file, so I guess it has been considered?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.