Coder Social home page Coder Social logo

numjs's Introduction

Build Status npm version Bower version Built with Grunt

NumJs 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
  • tools for basic image processing

Besides its obvious scientific uses, NumJs can also be used as an efficient multi-dimensional container of generic data.

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

NumJs is licensed under the MIT license, enabling reuse with almost no restrictions.

See this jsfiddle for a concrete example of how to use the library to manipulate images in the browser.

Installation

on node.js

npm install numjs
var nj = require('numjs');
...

on the browser

bower install numjs
<script src="bower_packages/numjs/dist/numjs.min.js"></script>
<!-- or include it directly from a CDN -->
<script src="https://cdn.jsdelivr.net/gh/nicolaspanel/[email protected]/dist/numjs.min.js"></script>

Basics

Array Creation

> var a = nj.array([2,3,4]);
> a
array([ 2, 3, 4])
> var b = nj.array([[1,2,3], [4,5,6]]);
> b
array([[ 1, 2, 3],
       [ 4, 5, 6]])

Note: Default data container is Javascript Array object. If needed, you can also use typed array such as Uint8Array:

> var a = nj.uint8([1,2,3]);
> a
array([ 1, 2, 3], dtype=uint8)

Note: possible types are int8, uint8, int16, uint16, int32, uint32, float32, float64 and array (the default)

To create arrays with a given shape, you can use zeros, ones or random functions:

> nj.zeros([2,3]);
array([[ 0, 0, 0],
       [ 0, 0, 0]])
> nj.ones([2,3,4], 'int32')     // dtype can also be specified
array([[[ 1, 1, 1, 1],
        [ 1, 1, 1, 1],
        [ 1, 1, 1, 1]],
       [[ 1, 1, 1, 1],
        [ 1, 1, 1, 1],
        [ 1, 1, 1, 1]]], dtype=int32)

> nj.random([4,3])
array([[ 0.9182 , 0.85176, 0.22587],
       [ 0.50088, 0.74376, 0.84024],
       [ 0.74045, 0.23345, 0.20289],
       [ 0.00612, 0.37732, 0.06932]])

To create sequences of numbers, NumJs provides a function called arange:

> nj.arange(4);
array([ 0, 1, 2, 3])

> nj.arange( 10, 30, 5 )
array([ 10, 15, 20, 25])

> nj.arange(1, 5, 'uint8');
array([ 1, 2, 3, 4], dtype=uint8)

More info about the array

NumJs’s array class is called NdArray. It is also known by the alias array. The more important properties of an NdArray object are:

  • NdArray#ndim: the number of axes (dimensions) of the array.
  • NdArray#shape: the dimensions of the array. This is a list of integers indicating the size of the array in each dimension. For a matrix with n rows and m columns, shape will be [n,m]. The length of the shape is therefore the number of dimensions, ndim.
  • NdArray#size: the total number of elements of the array. This is equal to the product of the elements of shape.
  • NdArray#dtype: a string describing the type of the elements in the array. int32, int16, and float64 are some examples. Default dtype is array.

An NdArray can always be converted to a native JavaScript Array using NdArray#tolist() method.

Example:

> a = nj.arange(15).reshape(3, 5);
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]])

Printing arrays

When you print an array, NumJs displays it in a similar way to nested lists, but with the following layout:

  • the last axis is printed from left to right,
  • the second-to-last is printed from top to bottom,
  • the rest are also printed from top to bottom, with each slice separated from the next by an empty line.

One-dimensional arrays are then printed as rows, bidimensionals as matrices and tridimensionals as lists of matrices.

> var a = nj.arange(6);                 // 1d array
> console.log(a);
array([ 0, 1, 2, 3, 4, 5])
>
> var b = nj.arange(12).reshape(4,3);   // 2d array
> console.log(b);
array([[  0,  1,  2],
       [  3,  4,  5],
       [  6,  7,  8],
       [  9, 10, 11]])
>
> var c = nj.arange(24).reshape(2,3,4); // 3d array
> console.log(c);
array([[[  0,  1,  2,  3],
        [  4,  5,  6,  7],
        [  8,  9, 10, 11]],
       [[ 12, 13, 14, 15],
        [ 16, 17, 18, 19],
        [ 20, 21, 22, 23]]])

If an array is too large to be printed, NumJs automatically skips the central part of the array and only prints the corners:

> console.log(nj.arange(10000).reshape(100,100))
array([[    0,    1, ...,   98,   99],
       [  100,  101, ...,  198,  199],
        ...
       [ 9800, 9801, ..., 9898, 9899],
       [ 9900, 9901, ..., 9998, 9999]])

To customize this behaviour, you can change the printing options using nj.config.printThreshold (default is 7):

> nj.config.printThreshold = 9;
> console.log(nj.arange(10000).reshape(100,100))
array([[    0,    1,    2,    3, ...,   96,   97,   98,   99],
       [  100,  101,  102,  103, ...,  196,  197,  198,  199],
       [  200,  201,  202,  203, ...,  296,  297,  298,  299],
       [  300,  301,  302,  303, ...,  396,  397,  398,  399],
        ...
       [ 9600, 9601, 9602, 9603, ..., 9696, 9697, 9698, 9699],
       [ 9700, 9701, 9702, 9703, ..., 9796, 9797, 9798, 9799],
       [ 9800, 9801, 9802, 9803, ..., 9896, 9897, 9898, 9899],
       [ 9900, 9901, 9902, 9903, ..., 9996, 9997, 9998, 9999]])

Indexing

Single element indexing uses get and set methods. It is 0-based, and accepts negative indices for indexing from the end of the array:

> var a = nj.array([0,1,2]);
> a.get(1)
1
>
> a.get(-1)
2
>
> var b = nj.arange(3*3).reshape(3,3);
> b
array([[  0,  1,  2],
       [  3,  4,  5],
       [  6,  7,  8])
>
> b.get(1, 1);
4
>
> b.get(-1, -1);
8
> b.set(0,0,1);
> b
array([[ 1, 1, 2],
       [ 3, 4, 5],
       [ 6, 7, 8]])

Slicing and Striding

It is possible to slice and stride arrays to extract arrays of the same number of dimensions, but of different sizes than the original. The slicing and striding works exactly the same way it does in NumPy:

> var a = nj.arange(5);
> a
array([  0,  1,  2,  3,  4])
>
> a.slice(1) // skip the first item, same as a[1:]
array([ 1, 2, 3, 4])
>
> a.slice(-3) // takes the last 3 items, same as a[-3:]
array([ 2, 3, 4])
>
> a.slice([4]) // takes the first 4 items, same as a[:4]
array([ 0, 1, 2, 3])
>
> a.slice([-2]) // skip the last 2 items, same as a[:-2]
array([ 0, 1, 2])
>
> a.slice([1,4]) // same as a[1:4]
array([ 1, 2, 3])
>
> a.slice([1,4,-1]) // same as a[1:4:-1]
array([ 3, 2, 1])
>
> a.slice([null,null,-1]) // same as a[::-1]
array([ 4, 3, 2, 1, 0])
>
> var b = nj.arange(5*5).reshape(5,5);
> b
array([[  0,  1,  2,  3,  4],
       [  5,  6,  7,  8,  9],
       [ 10, 11, 12, 13, 14],
       [ 15, 16, 17, 18, 19],
       [ 20, 21, 22, 23, 24]])
>
> b.slice(1,2) //  skip the first row and the 2 first  columns, same as b[1:,2:]
array([[  7,  8,  9],
       [ 12, 13, 14],
       [ 17, 18, 19],
       [ 22, 23, 24]])
>
> b.slice(null, [null, null, -1]) // reverse rows, same as b[:, ::-1]
array([[  4,  3,  2,  1,  0],
       [  9,  8,  7,  6,  5],
       [ 14, 13, 12, 11, 10],
       [ 19, 18, 17, 16, 15],
       [ 24, 23, 22, 21, 20]])

Note that slices do not copy the internal array data, it produces a new views of the original data.

Basic operations

Arithmetic operators such as * (multiply), + (add), - (subtract), / (divide), ** (pow), = (assign) apply elemen-twise. A new array is created and filled with the result:

> zeros = nj.zeros([3,4]);
array([[ 0, 0, 0, 0],
       [ 0, 0, 0, 0],
       [ 0, 0, 0, 0]])
>
> ones = nj.ones([3,4]);
array([[ 1, 1, 1, 1],
       [ 1, 1, 1, 1],
       [ 1, 1, 1, 1]])
>
> ones.add(ones)
array([[ 2, 2, 2, 2],
       [ 2, 2, 2, 2],
       [ 2, 2, 2, 2]])
>
> ones.subtract(ones)
array([[ 0, 0, 0, 0],
       [ 0, 0, 0, 0],
       [ 0, 0, 0, 0]])
>
> zeros.pow(zeros)
array([[ 1, 1, 1, 1],
       [ 1, 1, 1, 1],
       [ 1, 1, 1, 1]])
>

To modify an existing array rather than create a new one you can set the copy parameter to false:

> ones = nj.ones([3,4]);
array([[ 1, 1, 1, 1],
       [ 1, 1, 1, 1],
       [ 1, 1, 1, 1]])
>
> ones.add(ones, false)
array([[ 2, 2, 2, 2],
       [ 2, 2, 2, 2],
       [ 2, 2, 2, 2]])
>
> ones
array([[ 2, 2, 2, 2],
       [ 2, 2, 2, 2],
       [ 2, 2, 2, 2]])
>
> zeros = nj.zeros([3,4])
> zeros.slice([1,-1],[1,-1]).assign(1, false);
> zeros
array([[ 0, 0, 0, 0],
       [ 0, 1, 1, 0],
       [ 0, 0, 0, 0]])

Note: available for add, subtract, multiply, divide, assign and pow methods.

The matrix product can be performed using the dot function:

> a = nj.arange(12).reshape(3,4);
array([[  0,  1,  2,  3],
       [  4,  5,  6,  7],
       [  8,  9, 10, 11]])
>
> nj.dot(a.T, a)
array([[  80,  92, 104, 116],
       [  92, 107, 122, 137],
       [ 104, 122, 140, 158],
       [ 116, 137, 158, 179]])
>
> nj.dot(a, a.T)
array([[  14,  38,  62],
       [  38, 126, 214],
       [  62, 214, 366]])

Many unary operations, such as computing the sum of all the elements in the array, are implemented as methods of the NdArray class:

> a = nj.random([2,3])
array([[0.62755, 0.8278,0.21384],
       [ 0.7029,0.27584,0.46472]])
> a.sum()
3.1126488673035055
>
> a.min()
0.2138431086204946
>
> a.max()
0.8278025290928781
>
> a.mean()
0.5187748112172509
>
> a.std()
0.22216977543691244

Universal Functions

NumJs provides familiar mathematical functions such as sin, cos, and exp. These functions operate element-wise on an array, producing an NdArray as output:

> a = nj.array([-1, 0, 1])
array([-1, 0, 1])
>
> nj.negative(a)
array([ 1, 0,-1])
>
> nj.abs(a)
array([ 1, 0, 1])
>
> nj.exp(a)
array([ 0.36788,       1, 2.71828])
>
> nj.tanh(a)
array([-0.76159,       0, 0.76159])
>
> nj.softmax(a)
array([ 0.09003, 0.24473, 0.66524])
>
> nj.sigmoid(a)
array([ 0.26894,     0.5, 0.73106])
>
> nj.exp(a)
array([ 0.36788,       1, 2.71828])
>
> nj.log(nj.exp(a))
array([-1, 0, 1])
>
> nj.sqrt(nj.abs(a))
array([ 1, 0, 1])
>
> nj.sin(nj.arcsin(a))
array([-1, 0, 1])
>
> nj.cos(nj.arccos(a))
array([-1, 0, 1])
>
> nj.tan(nj.arctan(a))
array([-1, 0, 1])

Shape Manipulation

An array has a shape given by the number of elements along each axis:

> a = nj.array([[  0,  1,  2,  3], [  4,  5,  6,  7], [  8,  9, 10, 11]]);
array([[  0,  1,  2,  3],
       [  4,  5,  6,  7],
       [  8,  9, 10, 11]])

> a.shape
[ 3, 4 ]

The shape of an array can be changed with various commands:

> a.flatten();
array([  0,  1,  2, ...,  9, 10, 11])
>
> a.T                   // equivalent to a.transpose(1,0)
array([[  0,  4,  8],
       [  1,  5,  9],
       [  2,  6, 10],
       [  3,  7, 11]])
>
> a.reshape(4,3)
array([[  0,  1,  2],
       [  3,  4,  5],
       [  6,  7,  8],
       [  9, 10, 11]])
>

Since a is matrix we may want its diagonal:

> nj.diag(a)
array([  0,  5, 10])
>

Identity matrix

The identity array is a square array with ones on the main diagonal:

> nj.identity(3)
array([[ 1, 0, 0],
       [ 0, 1, 0],
       [ 0, 0, 1]])

Concatenate different arrays

Several arrays can be stacked together using concatenate function:

> a = nj.arange(12).reshape(3,4)
array([[  0,  1,  2,  3],
       [  4,  5,  6,  7],
       [  8,  9, 10, 11]])
>
> b = nj.arange(3)
array([ 0, 1, 2])
>
> nj.concatenate(a,b.reshape(3,1))
array([[  0,  1,  2,  3,  0],
       [  4,  5,  6,  7,  1],
       [  8,  9, 10, 11,  2]])

Notes:

  • the arrays must have the same shape, except in the last dimension
  • arrays are concatenated along the last axis

It is still possible to concatenate along other dimensions using transpositions:

> a = nj.arange(12).reshape(3,4)
array([[  0,  1,  2,  3],
       [  4,  5,  6,  7],
       [  8,  9, 10, 11]])
>
> b = nj.arange(4)
array([ 0, 1, 2, 3])
>
> nj.concatenate(a.T,b.reshape(4,1)).T
array([[  0,  1,  2,  3],
       [  4,  5,  6,  7],
       [  8,  9, 10, 11],
       [  0,  1,  2,  3]])

Stack multiple arrays

> a = nj.array([1, 2, 3])
> b = nj.array([2, 3, 4])

> nj.stack([a, b])
array([[1, 2, 3],
       [2, 3, 4]])
> nj.stack([a, b], -1)
array([[1, 2],
       [2, 3],
       [3, 4]])

Notes:

  • the arrays must have the same shape
  • take an optional axis argument which can be negative

Deep Copy

The clone method makes a complete copy of the array and its data.

> a = nj.arange(12).reshape(3,4)
array([[  0,  1,  2,  3],
       [  4,  5,  6,  7],
       [  8,  9, 10, 11]])
>
> b = a.clone()
array([[  0,  1,  2,  3],
       [  4,  5,  6,  7],
       [  8,  9, 10, 11]])
>
> a === b
false
>
> a.set(0,0,1)
> a
array([[  1,  1,  2,  3],
       [  4,  5,  6,  7],
       [  8,  9, 10, 11]])
> b
array([[  0,  1,  2,  3],
       [  4,  5,  6,  7],
       [  8,  9, 10, 11]])

Fast Fourier Transform (FFT)

fft and ifft functions can be used to compute the N-dimensional discrete Fourier Transform and its inverse.

Example:

> RI = nj.concatenate(nj.ones([10,1]), nj.zeros([10,1]))
array([[ 1, 0],
       [ 1, 0],
       [ 1, 0],
        ...
       [ 1, 0],
       [ 1, 0],
       [ 1, 0]])
>
> fft = nj.fft(RI)
array([[ 10,  0],
       [  0,  0],
       [  0,  0],
        ...
       [  0,  0],
       [  0,  0],
       [  0,  0]])
>
> nj.ifft(fft)
array([[ 1, 0],
       [ 1, 0],
       [ 1, 0],
        ...
       [ 1, 0],
       [ 1, 0],
       [ 1, 0]])

Note: both fft and ifft expect last dimension of the array to contain 2 values: the real and the imaginary value

Convolution

convolve function compute the discrete, linear convolution of two multi-dimensional arrays.

Note: The convolution product is only given for points where the signals overlap completely. Values outside the signal boundary have no effect. This behaviour is also known as the 'valid' mode.

Example:

> x = nj.array([0,0,1,2,1,0,0])
array([ 0, 0, 1, 2, 1, 0, 0])
>
> nj.convolve(x, [-1,0,1])
array([-1,-2, 0, 2, 1])
>
> var a = nj.arange(25).reshape(5,5)
> a
array([[  0,  1,  2,  3,  4],
       [  5,  6,  7,  8,  9],
       [ 10, 11, 12, 13, 14],
       [ 15, 16, 17, 18, 19],
       [ 20, 21, 22, 23, 24]])
> nj.convolve(a, [[ 1, 2, 1], [ 0, 0, 0], [-1,-2,-1]])
array([[ 40, 40, 40],
       [ 40, 40, 40],
       [ 40, 40, 40]])
> nj.convolve(nj.convolve(a, [[1, 2, 1]]), [[1],[0],[-1]])
array([[ 40, 40, 40],
       [ 40, 40, 40],
       [ 40, 40, 40]])

Note: convolve uses Fast Fourier Transform (FFT) to speed up computation on large arrays.

Other utils

rot90

> m = nj.array([[1,2],[3,4]], 'int')
> m
array([[1, 2],
       [3, 4]])
> nj.rot90(m)
array([[2, 4],
       [1, 3]])
> nj.rot90(m, 2)
array([[4, 3],
       [2, 1]])
> m = nj.arange(8).reshape([2,2,2])
> nj.rot90(m, 1, [1,2])
array([[[1, 3],
        [0, 2]],
      [[5, 7],
       [4, 6]]])

mod (since v0.16.0)

> nj.mod(nj.arange(7), 5)
> m
array([0, 1, 2, 3, 4, 0, 1])

Images manipulation

NumJs’s comes with powerful functions for image processing. Theses function are located in nj.images module.

The different color bands/channels are stored using the NdArray object such that a grey-image is [H,W], an RGB-image is [H,W,3] and an RGBA-image is [H,W,4].

Use nj.images.read, nj.images.write and nj.images.resize functions to (respectively) read, write or resize images.

Example:

> nj.config.printThreshold = 28;
>
> var img = nj.images.data.digit;  // WARN: this is a property, not a function. See also `nj.images.data.moon`, `nj.images.data.lenna` and `nj.images.data.node`
>
> img
array([[   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0],
       [   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0],
       [   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0],
       [   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0],
       [   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0],
       [   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   3,  18,  18,  18, 126, 136, 175,  26, 166, 255, 247, 127,   0,   0,   0,   0],
       [   0,   0,   0,   0,   0,   0,   0,   0,  30,  36,  94, 154, 170, 253, 253, 253, 253, 253, 225, 172, 253, 242, 195,  64,   0,   0,   0,   0],
       [   0,   0,   0,   0,   0,   0,   0,  49, 238, 253, 253, 253, 253, 253, 253, 253, 253, 251,  93,  82,  82,  56,  39,   0,   0,   0,   0,   0],
       [   0,   0,   0,   0,   0,   0,   0,  18, 219, 253, 253, 253, 253, 253, 198, 182, 247, 241,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0],
       [   0,   0,   0,   0,   0,   0,   0,   0,  80, 156, 107, 253, 253, 205,  11,   0,  43, 154,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0],
       [   0,   0,   0,   0,   0,   0,   0,   0,   0,  14,   1, 154, 253,  90,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0],
       [   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0, 139, 253, 190,   2,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0],
       [   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,  11, 190, 253,  70,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0],
       [   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,  35, 241, 225, 160, 108,   1,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0],
       [   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,  81, 240, 253, 253, 119,  25,   0,   0,   0,   0,   0,   0,   0,   0,   0],
       [   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,  45, 186, 253, 253, 150,  27,   0,   0,   0,   0,   0,   0,   0,   0],
       [   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,  16,  93, 252, 253, 187,   0,   0,   0,   0,   0,   0,   0,   0],
       [   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0, 249, 253, 249,  64,   0,   0,   0,   0,   0,   0,   0],
       [   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,  46, 130, 183, 253, 253, 207,   2,   0,   0,   0,   0,   0,   0,   0],
       [   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,  39, 148, 229, 253, 253, 253, 250, 182,   0,   0,   0,   0,   0,   0,   0,   0],
       [   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,  24, 114, 221, 253, 253, 253, 253, 201,  78,   0,   0,   0,   0,   0,   0,   0,   0,   0],
       [   0,   0,   0,   0,   0,   0,   0,   0,  23,  66, 213, 253, 253, 253, 253, 198,  81,   2,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0],
       [   0,   0,   0,   0,   0,   0,  18, 171, 219, 253, 253, 253, 253, 195,  80,   9,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0],
       [   0,   0,   0,   0,  55, 172, 226, 253, 253, 253, 253, 244, 133,  11,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0],
       [   0,   0,   0,   0, 136, 253, 253, 253, 212, 135, 132,  16,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0],
       [   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0],
       [   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0],
       [   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0]], dtype=uint8)
> var resized = nj.images.resize(img, 14, 12)
>
> resized.shape
[ 14, 12 ]
>
> resized
array([[   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0],
       [   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0],
       [   0,   0,   0,   0,   0,   6,   9,  66,  51, 106,  94,   0],
       [   0,   0,  13, 140, 189, 233, 253, 253, 143, 159,  75,   0],
       [   0,   0,   5, 178, 217, 241,  98, 172,   0,   0,   0,   0],
       [   0,   0,   0,   4,  74, 197,   1,   0,   0,   0,   0,   0],
       [   0,   0,   0,   0,   3, 180, 114,  28,   0,   0,   0,   0],
       [   0,   0,   0,   0,   0,  21, 182, 220,  51,   0,   0,   0],
       [   0,   0,   0,   0,   0,   0,   4, 149, 236,  16,   0,   0],
       [   0,   0,   0,   0,   0,  47, 165, 236, 224,   1,   0,   0],
       [   0,   0,   0,  23, 152, 245, 240, 135,  20,   0,   0,   0],
       [   0,  57, 167, 245, 251, 148,  23,   0,   0,   0,   0,   0],
       [   0,  98, 127,  87,  37,   0,   0,   0,   0,   0,   0,   0],
       [   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0]], dtype=uint8)

See also this jsfiddle for more details on what is possible from the browser.

More ?

See documentation on numjs globals and NdArray methods.

Credits

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

numjs's People

Contributors

jordanhart avatar laxminarayanmn avatar lordnox avatar lukasdrgon avatar manzt avatar mationai avatar mjschock avatar nicolaspanel 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  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  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  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 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  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

numjs's Issues

Is browserify-shim necessary?

When I tried to use NumJs on Node.js, I got an error: Cannot find module 'browserify-shim' .

NumJs requires browserify-shim in package.json, but it seems not working -- it is used nowhere. Is browserify-shim necessary?

fromstring

any implementation of numpy.fromstring?

Reshape Not allowing resizing

Doing a little REPL testing I found the reshape commend is not working.

> let rangeArray = np.arange(6,12)
> rangeArray
array([  6,  7,  8,  9, 10, 11])

> rangeArray.reshape( (2,3) )
ValueError: total size of new array must be unchanged
    at new ValueError (./node_modules/numjs/src/errors.js:5:21)
    at NdArray.reshape (./node_modules/numjs/src/ndarray.js:260:11)
> rangeArray.size
6
> rangeArray.reshape( (1,6) )
array([  6,  7,  8,  9, 10, 11])
> rangeArray.reshape( (6,1) )
ValueError: total size of new array must be unchanged
    at new ValueError (./node_modules/numjs/src/errors.js:5:21)
    at NdArray.reshape (./node_modules/numjs/src/ndarray.js:260:11)

Support for React Native

First off, thanks for all your work on this project!

I've been trying to use it in a ReactNative project as a numpy replacement for some TensorFlow preprocessing and I ran into a small issue with using the library outside of a NodeJS environment.

Specifically, the functions in src/images/ have dependencies on the path module and __dirname variable which don't exist outside of node.

I was able to work around this by only conditionally exporting the images code at the bottom of src/index.js.

I'd be happy to clean it up a bit and submit a PR if this is a use case you'd be interested in supporting. Cheers.

Migrate to ES6

Migration is quite simple, but it's possible to achieve a visually cleaner code as a result, especially when it comes to the functional programming. It's not critical - but I can take care of it.

I think it would simplify further development

Install warnings

I got a couple of warnings about Sandbox breakout.
Just thought I should post it. I'm thinking there is a manual solution to upgrade static-eval to version >=2
Any advice or thoughts on if this is a concern, how to change the source dependency etc.

                       === npm audit security report ===

┌──────────────────────────────────────────────────────────────────────────────┐
│                                Manual Review                                 │
│            Some vulnerabilities require your attention to resolve            │
│                                                                              │
│         Visit https://go.npm.me/audit-guide for additional guidance          │
└──────────────────────────────────────────────────────────────────────────────┘
┌───────────────┬──────────────────────────────────────────────────────────────┐
│ Moderate      │ Sandbox Breakout / Arbitrary Code Execution                  │
├───────────────┼──────────────────────────────────────────────────────────────┤
│ Package       │ static-eval                                                  │
├───────────────┼──────────────────────────────────────────────────────────────┤
│ Patched in    │ >=2.0.0                                                      │
├───────────────┼──────────────────────────────────────────────────────────────┤
│ Dependency of │ numjs                                                        │
├───────────────┼──────────────────────────────────────────────────────────────┤
│ Path          │ numjs > cwise > static-module > static-eval                  │
├───────────────┼──────────────────────────────────────────────────────────────┤
│ More info     │ https://nodesecurity.io/advisories/548                       │
└───────────────┴──────────────────────────────────────────────────────────────┘
┌───────────────┬──────────────────────────────────────────────────────────────┐
│ Moderate      │ Sandbox Breakout / Arbitrary Code Execution                  │
├───────────────┼──────────────────────────────────────────────────────────────┤
│ Package       │ static-eval                                                  │
├───────────────┼──────────────────────────────────────────────────────────────┤
│ Patched in    │ >=2.0.0                                                      │
├───────────────┼──────────────────────────────────────────────────────────────┤
│ Dependency of │ numjs                                                        │
├───────────────┼──────────────────────────────────────────────────────────────┤
│ Path          │ numjs > ndarray-fft > cwise > static-module > static-eval    │
├───────────────┼──────────────────────────────────────────────────────────────┤
│ More info     │ https://nodesecurity.io/advisories/548                       │
└───────────────┴──────────────────────────────────────────────────────────────┘
found 2 moderate severity vulnerabilities in 272 scanned packages
  2 vulnerabilities require manual review. See the full report for details.

https://nodesecurity.io/advisories/548

It's odd because it is that version 2.0.0 running an update made no change?

Installation fails on Windows with Python 3.5.1 due to Sharp dependency

Log below.


> gyp ERR! configure error
> gyp ERR! stack Error: Python executable "C:\Python35\python.EXE" is v3.5.1, which is not supported by gyp.
> gyp ERR! stack You can pass the --python switch to point to Python >= v2.5.0 & < 3.0.0.
> gyp ERR! stack     at failPythonVersion (C:\Program Files\nodejs\node_modules\npm\node_modules\node-gyp\lib\configure.js:406:14)
> gyp ERR! stack     at C:\Program Files\nodejs\node_modules\npm\node_modules\node-gyp\lib\configure.js:395:9
> gyp ERR! stack     at ChildProcess.exithandler (child_process.js:193:7)
> gyp ERR! stack     at emitTwo (events.js:100:13)
> gyp ERR! stack     at ChildProcess.emit (events.js:185:7)
> gyp ERR! stack     at maybeClose (internal/child_process.js:827:16)
> gyp ERR! stack     at Socket.<anonymous> (internal/child_process.js:319:11)
> gyp ERR! stack     at emitOne (events.js:90:13)
> gyp ERR! stack     at Socket.emit (events.js:182:7)
> gyp ERR! stack     at Pipe._onclose (net.js:475:12)
> gyp ERR! System Windows_NT 10.0.10586
> gyp ERR! command "C:\\Program Files\\nodejs\\node.exe" "C:\\Program Files\\nodejs\\node_modules\\npm\\node_modules\\node-gyp\\bin\\node-gyp.js" "rebuild"
> gyp ERR! cwd C:\Users\nash\node_modules\sharp
> gyp ERR! node -v v5.7.0
> gyp ERR! node-gyp -v v3.2.1
> gyp ERR! not ok
> npm WARN enoent ENOENT: no such file or directory, open 'C:\Users\nash\package.json'
> npm WARN nash No description
> npm WARN nash No repository field.
> npm WARN nash No README data
> npm WARN nash No license field.
> npm ERR! Windows_NT 10.0.10586
> npm ERR! argv "C:\\Program Files\\nodejs\\node.exe" "C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js" "install" "sharp"
> npm ERR! node v5.7.0
> npm ERR! npm  v3.6.0
> npm ERR! code ELIFECYCLE
> 
> npm ERR! [email protected] install: `node-gyp rebuild`
> npm ERR! Exit status 1
> npm ERR!
> npm ERR! Failed at the [email protected] install script 'node-gyp rebuild'.
> npm ERR! Make sure you have the latest version of node.js and npm installed.
> npm ERR! If you do, this is most likely a problem with the sharp package,
> npm ERR! not with npm itself.
> npm ERR! Tell the author that this fails on your system:
> npm ERR!     node-gyp rebuild
> npm ERR! You can get information on how to open an issue for this project with:
> npm ERR!     npm bugs sharp
> npm ERR! Or if that isn't available, you can get their info via:
> npm ERR!     npm owner ls sharp
> npm ERR! There is likely additional logging output above.

numjs.round acts not same with numpy.round

According to numpy document:

The result of rounding a float is a float. For values exactly halfway between rounded decimal 
values, NumPy rounds to the nearest even value. Thus 1.5 and 2.5 round to 2.0, -0.5 and 0.5 
round to 0.0, etc.

but numjs.round to the nearest value. I have test:

const x = numjs.array([1.5, 2.5, -0.5, -1, 100])
console.log(nj.round(x).tolist())

got result:

[ 2, 3, -0, -1, 100 ]

Behavior not the same: numjs.slice v.s. python array[1::2]

NumJs:

> a = nj.array([1,2,3,4,5,6])
array([ 1, 2, 3, 4, 5, 6])
> a.slice([null, null, 2])
array([ 1, 3, 5])
> a.slice([1, null, 2])
array([ 1])

Python:

>>> a = [1,2,3,4,5,6]
>>> a[::2]
[1, 3, 5]
>>> a[1::2]
[2, 4, 6]

Numjs: > a.slice([1, null, 2]): array([ 1])
Python: >>> a[1::2]: [2, 4, 6]

Which as a.slice([1, null, 2]) in numjs behavior is not right.


UPDATE: I can use the following syntax to get the right slice. Does that be designed, or a bug?

> a.slice([1, 6, 2])
array([ 2, 4, 6])

FFT nj.concatenate Dimension mistmatch

Following the instructions for taking FFT, I am trying to concatenate the real and imaginary parts like this:

RI = nj.concatenate(nj.array(my_sound_array), nj.zeros(my_sound_array.length))

This gives me the following error:

numjs.js:22114 Uncaught Error: all the input arrays must have same number of dimensions
at new ValueError (numjs.js:22114)
at Object.concatenate (numjs.js:23242)

How do I overcome this error ?

How could I get the Buffer of NdArray returned by numjs?

What I want to do, is to save the memory usage.

I have a project that will deal with large photos, like a 4000 x 4000 JPEG file. what I want to do is:

const image = nj.images.read('file-path.jpeg')`
image.flatten() // this will cause MEMORY OUT
const typedData = image.XXX? // HOW TO GET THIS?
const base64Text = new Buffer(typedData).toString('base64')

The current operation is use a image.flatten().tolist() and uses lots of memory, which caused a nodejs OUT OF HEAP MEMORY ERROR. (only add node --max-old-space-size=4096 can make it)

I saw a method of nj.getRawData(), may I use that? And another question: does it the same function like np.getbuffer()? If so, why not the same name.

Thanks!


UPDATE: I make a reproduce-able code to demo the problem:

Both flatten() and reshape() will not work with very large NdArray, like [8000, 10000].
But a for-loop with NdArray will be ok.

const a = nj.ones([8000, 10000])
console.log('DONE1', a)

/**
 * The following two functions will cause
 * FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory
 * flatten()
 * reshape()
 */
// a.flatten()
// a.reshape(1, -1)

/**
 * The following raw TypeArray will did the job without problem
 */
const [row, col] = a.shape
const typedData = new Float64Array(row * col)

let n = 0
for (let i = 0; i < row; i++) {
  for (let j = 0; j < col; j++) {
    typedData[n++] = a.get(i, j)
  }
}

Diagonal / identity matrices

What is the standard way to create diagonal matrices or identity matrices?

I searched the source very quickly and couldn't find something promising. An example in the readme would be nice.

Publish to unpkg.com?

Great package... would like to use it from observablehq.com. As far as I can tell, this requires the dist folder to be published on unpkg.com (the package is already there, but is incomplete). I don't know how that would be achieved.

Thanks!

Error installing the module

I am getting the following error while installing this lib. I don't seem to get the hang of what is the exact problem

$ npm i numjs

> [email protected] install C:\Users\Prashant\abc\node_modules\deasync
> node ./build.js

`win32-x64-node-8` exists; testing
Binary is fine; exiting

> [email protected] install C:\Users\Prashant\abc\node_modules\sharp
> node-gyp rebuild


C:\Users\Prashant\abc\node_modules\sharp>if not defined npm_config_node_gyp (node "C:\Program Files\nodejs\node_modules\npm\bin\node-gyp-bin\\..\..\node_modules\node-gyp\bin\node-gyp.js" rebuild )  else (node "" rebuild )
gyp WARN install got an error, rolling back install
gyp ERR! configure error
gyp ERR! stack Error: ENOENT: no such file or directory, open 'C:\Program'
gyp ERR! stack     at Object.fs.openSync (fs.js:652:18)
gyp ERR! stack     at Object.fs.readFileSync (fs.js:553:33)
gyp ERR! stack     at readCAFile (C:\Program Files\nodejs\node_modules\npm\node_modules\node-gyp\lib\install.js:466:15)
gyp ERR! stack     at download (C:\Program Files\nodejs\node_modules\npm\node_modules\node-gyp\lib\install.js:438:22)
gyp ERR! stack     at C:\Program Files\nodejs\node_modules\npm\node_modules\node-gyp\lib\install.js:185:19
gyp ERR! stack     at C:\Program Files\nodejs\node_modules\npm\node_modules\mkdirp\index.js:48:26
gyp ERR! stack     at FSReqWrap.oncomplete (fs.js:153:5)
gyp ERR! System Windows_NT 10.0.15063
gyp ERR! command "C:\\Program Files\\nodejs\\node.exe" "C:\\Program Files\\nodejs\\node_modules\\npm\\node_modules\\node-gyp\\bin\\node-gyp.js" "rebuild"
gyp ERR! cwd C:\Users\Prashant\abc\node_modules\sharp
gyp ERR! node -v v8.4.0
gyp ERR! node-gyp -v v3.6.2
gyp ERR! not ok
npm WARN [email protected] No repository field.

npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] install: `node-gyp rebuild`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] install script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\Prashant\AppData\Roaming\npm-cache\_logs\2017-08-31T12_53_03_906Z-debug.log

Any clue ?
Also, do I need python for installing this library?

How do you do python things: like array references?

I'm realizing this is not python.
So I'm wondering if there's away to do square bracket reference to the array like.

> rangeArray
array([  6,  7,  8,  9, 10, 11])
> rangeArray[0,1]
undefined

> rangeArray[0,0:2]
rangeArray[0,0:2]
              ^

SyntaxError: Unexpected token :

It is possible to overload the square bracket operators. I made a comment about it in this dated thread, but in ES6 and so NodeJS 10 it should be possible.
https://stackoverflow.com/a/46438904/4928388

Hello,Bro. I use "npm install numjs" in windows 7,node.js v6.11.3-32bit,npm report ERROR like that!

npm ERR! argv "F:\nodejs\node.exe" "F:\nodejs\node_modules\npm\bin\npm-cl
i.js" "install" "numjs" "--save"
npm ERR! node v6.11.3
npm ERR! npm v3.10.10
npm ERR! code ELIFECYCLE

npm ERR! [email protected] install: node-gyp rebuild
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] install script 'node-gyp rebuild'.
npm ERR! Make sure you have the latest version of node.js and npm installed.
npm ERR! If you do, this is most likely a problem with the sharp package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR! node-gyp rebuild
npm ERR! You can get information on how to open an issue for this project with:
npm ERR! npm bugs sharp
npm ERR! Or if that isn't available, you can get their info via:
npm ERR! npm owner ls sharp
npm ERR! There is likely additional logging output above.

npm ERR! Please include the following file with any support request:
npm ERR! F:\nodequant\nodequant\npm-debug.log

Ones and zeros without shape

Allow creation of ones and zeros matrices that automatically set their shape based on the operations they are submitted to.

This way, I could stop having to do const ones = nj.ones(x.shape);

Edit: Since the reason this would be useful has mostly to do with elementwise operations, maybe an alternative to this would be providing a way to define custom elementwise operations, like the ones provided out of the box (such as nj.sigmoid())

nj.array with un-fixed shape

I do not know if sometimes the data array has the un-standarded shape, like the following code:

const a = nj.array([0, [1, 2], [2, 3], [[4, [5, 6]]], 4])
console.log(a.shape) // Output: [ 5 ]

My question is: a.shape will be calculated only with the first-row element.

Should I never use this kind of un-fixed shape?

A related TypeScript issue: DefinitelyTyped/DefinitelyTyped#18508 (comment)

React-Native?

Will this library work for react-native projects?

Broken dependency chain

This module uses Sharp as a dependency, which uses node-gyp as a dependency, which is hard-broken unless you have an out-of-date version of Python installed in your system. Is there a way to use this package without that dependency?

Support -1 parameter for numjs.reshape

-1 as a parameter (one dimension of the shape) of reshape infers the size of the resulting reshaped array with the other shape parameter and the length

From scipy.org's numpy docs

newshape : int or tuple of ints
The new shape should be compatible with the original shape. If an integer, then the result will be a 1-D array of that length. One shape dimension can be -1. In this case, the value is inferred from the length of the array and remaining dimensions.

Would be happy to contribute

mean/sum/min/max/std on axis

It would be very helpful if numjs supported mean/sum/min/max etc. over a specific axis like numpy.

a = nj.random([2,3]);
nj.mean(a, axis=1)

multiply got error for different shape

see my project:
https://github.com/RexHuang/GaussNewton.js

I want to get result of following:
Jf = nj.array([[1.3009595627444526,9.007230966684432],[1.6924957839670183,23.436086522021778],[2.201868575113508,45.734101316607926],[2.8645419789086186,79.33095528080969],[3.726653280367031,129.00795614214644],[4.848225222175984,201.4009610995515],[6.30734496560592,305.6835907468525],[8.205600748567576,454.4937036239815]]);
Jf.T.multiply(Jf)

but got error:
Uncaught Error: cwise: Arrays do not all have the same shape!
at muleq_cwise_thunk (eval at n.exports (numjs.min.js:3), :8:71)
at Object.muleq_ndarrayops [as muleq] (eval at o (numjs.min.js:3), :3:42)
at T.multiply (numjs.min.js:3)
at :2:6

I had use matrixjs to solve the problem, pls study from https://github.com/Airblader/matrixjs

Here's some of my implementation:
// var delta = (Jf.T.multiply(Jf)).inv().multiply(Jf.T).multiply(r);
var Mat_Jf=Jf.flatten().tolist().toMatrix(Jf.shape[0],Jf.shape[1]);
var Mat_r=r.flatten().tolist().toMatrix(r.shape[0],r.shape[1]);
var Mat_delta = Mat_Jf.transpose().multiply(Mat_Jf).inverse().multiply(Mat_Jf.transpose()).multiply(Mat_r);
delta=nj.array(Mat_delta.toArray()).reshape(params.shape[0],params.shape[1]);

We also need the implement of inv()
I like numjs, hope we can improve:)

Errors trying to run numjs on npm or in jsbin

Hi,

I tried to run numjs with RunKit, it gives me the following error messages:

Error Stack Trace Viewer 
Error: Cannot find module './build/Release/sharp.node'
at Module._resolveFilename in core module.js — line 325
at Module._load in core module.js — line 276
at Module.require in core module.js — line 353
at require in core internal/module.js — line 12
at sharp/index.js — line 12
node.js startup…

and when trying to use numjs in jsbin I don't get the expected results.
It looks like it's giving me back the full constructor in the console instead of the array.

Question: browser support for sharp dependent features?

This is a more a question out of curiosity: I notice your package relies on sharp, which is a package for image resizing written for Node.js. On the readme it says numjs runs in both: the browser and node.js. I am wondering which parts of numjs will work and which ones will fail when using numjs in the browser, assuming this statement is correct and that at least some part of sharp are incompatible lovell/sharp#766 (comment).

Thanks for the clarification and thanks for developing numjs! :)

More closely mimic numpy API, WebAssembly

I'm on a quest to allow full on scientific computing in JavaScript, essentially to replace the need for Python source code. To achieve that, I imagine we'll need libraries that more closely mimic the APIs of popular Python libraries. Has this project considered more closely mimicking the numpy API, and how about WebAssembly? With WebAssembly we could potentially tap into the underlying libraries that numpy and others are built off of and get near-native capabilities in Node.js and the browser.

Question: provide cpu intensive algorithms examples

Hi Nicolas, it would be nice to provide some real-world and cpu intensive algorithm examples, like by example LDA that I have put here in modified version of a known JavaScript implementation
This algorithm, when applied to large set of documents or small set with large number of tokens (words) is cpu bound and I beat that it could be improved using your numjs in some way.

Thanks a lot.

Can't install numjs

Config: Windows 10, Node 7.8, Python 3.53
Do I need special requirements? When I try to install it I have this errors:

~\Documents\GitHub\Theta> npm install numjs

> [email protected] install C:\Users\Orion\Documents\GitHub\Theta\node_modules\sharp
> node-gyp rebuild


C:\Users\Orion\Documents\GitHub\Theta\node_modules\sharp>if not defined npm_config_node_gyp (node "C:\Program Files\nodejs\node_modules\npm\bin\node-gyp-bin\\..\..\node_modules\node-gyp\bin\node-gyp.js" rebuild )  else (node "" rebuild )
gyp ERR! configure error
gyp ERR! stack Error: Can't find Python executable "C:\Users\Orion\AppData\Local\Programs\Python\Python35\python.EXE", you can set the PYTHON env variable.
gyp ERR! stack     at PythonFinder.failNoPython (C:\Program Files\nodejs\node_modules\npm\node_modules\node-gyp\lib\configure.js:454:19)
gyp ERR! stack     at PythonFinder.<anonymous> (C:\Program Files\nodejs\node_modules\npm\node_modules\node-gyp\lib\configure.js:480:16)
gyp ERR! stack     at C:\Program Files\nodejs\node_modules\npm\node_modules\graceful-fs\polyfills.js:284:29
gyp ERR! stack     at FSReqWrap.oncomplete (fs.js:114:15)
gyp ERR! System Windows_NT 10.0.14393
gyp ERR! command "C:\\Program Files\\nodejs\\node.exe" "C:\\Program Files\\nodejs\\node_modules\\npm\\node_modules\\node-gyp\\bin\\node-gyp.js" "rebuild"
gyp ERR! cwd C:\Users\Orion\Documents\GitHub\Theta\node_modules\sharp
gyp ERR! node -v v7.8.0
gyp ERR! node-gyp -v v3.5.0
gyp ERR! not ok
npm WARN enoent ENOENT: no such file or directory, open 'C:\Users\Orion\Documents\GitHub\Theta\package.json'
npm WARN Theta No description
npm WARN Theta No repository field.
npm WARN Theta No README data
npm WARN Theta No license field.
npm ERR! Windows_NT 10.0.14393
npm ERR! argv "C:\\Program Files\\nodejs\\node.exe" "C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js" "install" "numjs"
npm ERR! node v7.8.0
npm ERR! npm  v4.2.0
npm ERR! code ELIFECYCLE
npm ERR! errno 1

npm ERR! [email protected] install: `node-gyp rebuild`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] install script 'node-gyp rebuild'.
npm ERR! Make sure you have the latest version of node.js and npm installed.
npm ERR! If you do, this is most likely a problem with the sharp package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR!     node-gyp rebuild
npm ERR! You can get information on how to open an issue for this project with:
npm ERR!     npm bugs sharp
npm ERR! Or if that isn't available, you can get their info via:
npm ERR!     npm owner ls sharp
npm ERR! There is likely additional logging output above.

npm ERR! Please include the following file with any support request:
npm ERR!     C:\Users\Orion\AppData\Roaming\npm-cache\_logs\2017-04-07T19_39_38_295Z-debug.log

Pointwise log(x)

Just curious why there's nj.exp(A) but not nj,log(A). I was hoping to use log in a pointwise inverse sigmoid.

Thanks for the awesome library!

Missing functions like 'average'

I was wondering if there is something somewhere even though it's not working like numpy

> squareArray
[ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ]
> let sqAverage = nj.average(squareArray)
TypeError: nj.average is not a function

numpy.linalg ?

Hi, do you know any way to get something similar to linalg, ( with at least linalg.norm) easily ?

Cannot save image in reactjs canvas element

I am trying to do something similar to the following in reactjs:

const img = nj.images.read(nimg);
const rimage = nj.images.rgb2gray(img);
let rimg = React.createElement("canvas", {width: 500, height: 500});
console.log(rimg);
nj.images.save(rimage, rimg);

Whenever some condition occurs, I take an input image and try to return a canvas after manipulating it through numjs. But it always throw me an error in the last line (while saving it). The error is like this:
ValueError: expect input to be either an HTML Canvas or a (loaded) Image

How can I resolve this problem ? Please help me out here.

Creating a NdArray from ndarray

This is a question.
I have a data (nda below) of type ndarray (scijs/ndarray). Its dtype is float32 and underlying storage (nda.data) is a Buffer.

I was able to convert ndarray to NdArray by doing this:

const nja = nj.array(nda.data, 'float32').reshape(nda.shape);

I'd like to know if this is the right way to do. Would it allocate a new buffer under the hood?
(Some documents about migrating from ndarray to numjs(NdArray) would be great.)

Thanks in advance.

Extract image-related features / remove sharp

I think sharp is quite a heavy dependency for a package, which main concern is working with numbers. Thus I propose to extract the image-related features and put them into their own package.

As I see it, sharp is also only used for file-io and resizing, the latter I can understand. For file io I'm not sure, why it belongs into this package anyway.

p.s. my personal motivation is using numjs in electron, though. Had trouble because of sharp compilation errors, though that doesn't make the other points less valid ;)

Random seed

Allow providing a seed for the random generator, with nj.random.seed(something)

Split into smaller pieces that can be loaded individually

Hi Nicolas,
Thank you for your work on this package, I think it would be great to be able to only load an indiviual function in the same way that gl-mat4 does : https://github.com/stackgl/gl-mat4
With that package, you're able to write:

var scale = require('gl-mat4').scale
var scale = require('gl-mat4/scale')

And it would be amazing it the same thing were possible with this package, like:

var linspace = require('numjs/linspace');

If this is something you'd consider, I might be able to help, even though it looks like the numpy methods I'm looking for are not included in your library, but I'll do more digging to find out.
Cheers,
Alvin

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.