abtExp / vecto

🚧 A Javascript mini library to deal with n-dimensional arrays(referenced from numpy). 🚧🚀
https://npmjs.com/vecto
MIT License
1 stars 0 forks source link
javascript javascript-array ndarray ndimensional-vectors numpy vectors

Codacy Badge Code Climate Build Status npm Coverage Status codecov

vecto :construction: :sparkles: :art:

@0.7.1

A JavaScript Mini Library to deal with N-dimensional arrays.

What's new

Upcoming





Installation

npm install vecto

API

// for whole pkg
const vecto = require('vecto');

//selective 
const { Ndarray, core, math } = require('vecto');

Api List :

The Ndarray provides a class to construct ndarrays and operate on them.

Ndarray

Constructor
let v1 = new Ndarray(?[shape], ?'dtype', ?'initializer', ?[array]);
Other Methods For creation

/ Ndarray.array : This Constructs an ndarray object for passed in array. @array : [[Number]] Returns : { NdarrayObject } **/

ex :

let v = Ndarray.array([[1,2,3],[4,5,6]]);

/ Ndarray.zeros : Constructs an ndarray object of given shape filled with placeholder 0's. @shape : [Number], the shape of the ndarray Returns : { NdarrayObject } **/

ex :

let zv = Ndarray.zeroes([2,2,3]);

Ndarray properties and methods

Properties
Methods

This Method is used to alter the shape of an already existing ndarray, The new shape in form of array is provided and the current ndarray object is rearranged according to this new shape.

ex :

let nv = new ndarray([2,2,3],[[[5,5,5],[5,5,5]],[[5,5,5],[5,5,5]]],'float32');
// nv.array = [[[5,5,5],[5,5,5]],[[5,5,5],[5,5,5]]];
nv.resize([2,3]);
//nv.array = [[5,5,5],[5,5,5]]

This method is used to rearrange an already existing ndarray object, The new shape in form of array is provided and the current ndarray object is rearranged only if the size for the new shape === the size for the old shape , i.e., the total number of elements in both configurations remain the same.

ex :

let nv = new ndarray([2,2,3],[[[5,5,5],[5,5,5]],[[5,5,5],[5,5,5]]],'float32');
// nv.array = [[[5,5,5],[5,5,5]],[[5,5,5],[5,5,5]]];
nv.reshape([2,3]); // not possible
nv.reshape([6,2]);
//nv.array = [[5,5],[5,5],[5,5],[5,5],[5,5],[5,5]]

Creates 1-D form of the ndarray. * = called implicitly (don't need to call explicitly).

Arranges The passed in 1-d array according to the shape of the NdarrayObject

ex :

let nv = new ndarray([2,3]);
nv.arrange([2]);
//nv.array = [[2,2,2],[2,2,2]]
nv.arrange([1,2]);
//nv.array = [[1,2,1],[2,1,2]]
nv.arrange([1,2,3,4,5,6,7]);
//nv.array = [[1,2,3],[4,5,6]]

Performs transpose operation on 2d ndarrays (nd not supported yet) and returns the transposed array.
: return type = array.

ex :

let nv = new ndarray([2,3]);
nv.arrange([1,2,3,4,5,6]);
//nv.array = [[1,2,3],[4,5,6]]
console.log(nv.transpose())
//[[1,4],[2,5],[3,6]]

math

Provides The Following functions :

// Most of them are just wrappers around the normal Math class methods

product([arg1],[arg2],mode='string');

return type : array

Modes :

ex :

b = [[10,20,30],[10,10,10]]

console.log(product(a,b,'dot'));
// [[10,40,90],[40,50,60]]
**************************

let a = [[1,2,3],[4,5,6],[2,4,6]],
b = [2,4,5];
console.log(product(a,b,'dot'));
//[[2,4,6],[16,20,24],[10,20,30]]
console.log(product(a,b,'matrix'));
//[ [ 2, 4, 6 ], [ 16, 20, 24 ], [ 35, 40, 45 ] ]
**************************

let a = [1,2,3],
b = [1,2,3];
console.log(product(a,b,'dot'));
//[1,4,9]

**************************

Performs addition on two ndarrays.

Core

The core object exports all the methods to normal arrays instead of just ndarray objects.

Methods provided by the core are :

Takes in the shape of the ndarray in which the passed elements are to be arranged.

ex :

let elems_arr = [1,2,3,4,5,6,7,8,9,10];
let nd = core.arrange([2,5],elems_arr);
//nd = [[1,2,3,4,5],[6,7,8,9,10]]

Takes in an array and outputs the shape(configuration) of that array.

ex :

let ar = [[[1,2],[4,5]],[[2,3],[6,7]],[[7,8],[9,1]]],
shape = core.calc_shape(ar);
//shape = [3,2,2];

Takes in a array and calculates the size of the ndarray.

ex :

let shape = [4,5];
console.log(core.calc_size(shape));
//20

Makes the array elements contained in a limit provided as min_val and max_val.

[array] : the array to be clipped. min_val/[min_val] : the minimum limit for the data in the array. if provided as array, it's shape should match the shape of the provided array, with every element of the [min_val]describing the min val for the respective element of the provided array.

max_val/[max_val] : the maximum limit for the data in the array. if provided as array, it's shape should match the shape of the provided array, with every element of the [max_val]describing the max val for the respective element of the provided array.

( ^ = only one of min_val or max_val could be null.)

example :


let a = [[10,24,12,3],[7,1,20,9]];
clip(a,10,12);
// returns [[10,12,12,10],[10,10,12,10]]

clip(a,[[10,12,14,4],[8,0,10,10]],[[12,20,20,31],[10,1,10,10]]);
//returns [ [ 10, 20, 14, 4 ], [ 8, 1, 10, 10 ] ]

clip(a,[[10,12,14,4],[8,0,10,10]],2);
//returns [ [ 2, 2, 14, 4 ], [ 8, 1, 2, 10 ] ]

Return the dimension of the array

Takes in an ndarray. Return the flattened version(1d) version of ndarray.

example :


let a = [[[1,2],[3,4]],[[5,6],[7,8]]];
core.flatten(a);
//returns [1,2,3,4,5,6,7,8];

size : The Size of a chunk. number : the number of chunks to be formed array : The array from which the chunks will be formed.

ex :

let ar = [1,2,3,4,5];
console.log(core.form_arr(2,2,ar));
//[[1,2],[3,4]]

let br = [[1,2,3],[4,5,6],[7,8,9]];
console.log(core.form_chunks(2,2,br));
//[[[1,2,3],[4,5,6]],[[7,8,9],[1,2,3]]]

Performs transpose operation on passed array.

@abtExp