Skip to content
This repository has been archived by the owner on Oct 17, 2022. It is now read-only.

Commit

Permalink
Merge b5291a6 into 16a0117
Browse files Browse the repository at this point in the history
  • Loading branch information
abtExp committed Jan 19, 2018
2 parents 16a0117 + b5291a6 commit 8ce9c5e
Show file tree
Hide file tree
Showing 19 changed files with 416 additions and 237 deletions.
2 changes: 1 addition & 1 deletion docs/index.json
Original file line number Diff line number Diff line change
Expand Up @@ -573,7 +573,7 @@
},
{
"kind": "packageJSON",
"content": "{\n \"name\": \"vecto\",\n \"jest\": {\n \"collectCoverage\": true,\n \"coverageReporters\": [\n \"lcov\",\n \"text-summary\"\n ],\n \"coverageDirectory\": \"./coverage\"\n },\n \"version\": \"0.7.6\",\n \"description\": \"A JS mini library to deal with ndarrays and vectors\",\n \"main\": \"vecto.js\",\n \"scripts\": {\n \"test\": \"jest\",\n \"build\": \"webpack\",\n \"coverage\": \"cat ./coverage/lcov.info | ./node_modules/.bin/coveralls\"\n },\n \"repository\": {\n \"type\": \"git\",\n \"url\": \"git+https://github.com/abtexp/vecto.git\"\n },\n \"keywords\": [\n \"ndarray\",\n \"vectors\"\n ],\n \"author\": \"abtExp <atworkstudios@gmail.com>\",\n \"license\": \"MIT\",\n \"bugs\": {\n \"url\": \"https://github.com/abtexp/vecto/issues\"\n },\n \"homepage\": \"https://abtexp.github.io/vecto\",\n \"devDependencies\": {\n \"babel-core\": \"^6.26.0\",\n \"babel-loader\": \"^7.1.2\",\n \"babel-preset-es2015\": \"^6.24.1\",\n \"benchmark\": \"^2.1.4\",\n \"coveralls\": \"^2.13.1\",\n \"esdoc\": \"^1.0.4\",\n \"esdoc-standard-plugin\": \"^1.0.0\",\n \"gpu.js\": \"0.0.0\",\n \"istanbul\": \"^0.4.5\",\n \"jest\": \"^20.0.4\",\n \"lodash\": \"^4.17.4\",\n \"uglify-js\": \"^3.1.0\",\n \"uglifyjs-webpack-plugin\": \"^0.4.6\",\n \"webpack\": \"^3.5.5\"\n }\n}\n",
"content": "{\n \"name\": \"vecto\",\n \"jest\": {\n \"collectCoverage\": true,\n \"coverageReporters\": [\n \"lcov\",\n \"text-summary\"\n ],\n \"coverageDirectory\": \"./coverage\"\n },\n \"version\": \"0.7.8\",\n \"description\": \"A JS mini library to deal with ndarrays and vectors\",\n \"main\": \"vecto.js\",\n \"scripts\": {\n \"test\": \"jest\",\n \"build\": \"webpack\",\n \"coverage\": \"cat ./coverage/lcov.info | ./node_modules/.bin/coveralls\"\n },\n \"repository\": {\n \"type\": \"git\",\n \"url\": \"git+https://github.com/abtexp/vecto.git\"\n },\n \"keywords\": [\n \"ndarray\",\n \"vectors\"\n ],\n \"author\": \"abtExp <atworkstudios@gmail.com>\",\n \"license\": \"MIT\",\n \"bugs\": {\n \"url\": \"https://github.com/abtexp/vecto/issues\"\n },\n \"homepage\": \"https://abtexp.github.io/vecto\",\n \"devDependencies\": {\n \"babel-core\": \"^6.26.0\",\n \"babel-loader\": \"^7.1.2\",\n \"babel-preset-es2015\": \"^6.24.1\",\n \"benchmark\": \"^2.1.4\",\n \"coveralls\": \"^2.13.1\",\n \"esdoc\": \"^1.0.4\",\n \"esdoc-standard-plugin\": \"^1.0.0\",\n \"gpu.js\": \"0.0.0\",\n \"istanbul\": \"^0.4.5\",\n \"jest\": \"^20.0.4\",\n \"lodash\": \"^4.17.4\",\n \"uglify-js\": \"^3.1.0\",\n \"uglifyjs-webpack-plugin\": \"^0.4.6\",\n \"webpack\": \"^3.5.5\"\n }\n}\n",
"longname": "/home/abtexp/Desktop/devWorks/mlDevSuite/vecto/package.json",
"name": "package.json",
"static": true,
Expand Down
14 changes: 9 additions & 5 deletions lib/arrange.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
/* arrange : A method to arrange or create a Vector from the given elements
/**
* @function arrange : A method to arrange or create a Vector from the given elements
*
* @shape : [number] : Required, The shape of the array to be filled or arranged
* @param {Array} shape - @requires The shape of the array to be filled or arranged
*
* ? @elems_arr : [number] : The elements to be arranged
* @param {Array} elems_arr - The elements to be arranged
*
* Returns : [number] : Array of the provided shape filled with the passed elements or random elements
* @returns {Array} - Array of the provided shape filled with the passed elements or random elements
*
*/

Expand All @@ -14,7 +15,10 @@ module.exports = function arrange(shape, elems_arr) {
curr_arr = [];
for (let i = shape.length - 1; i > 0; i--) {
let size = shape[i],
no = i > 1 ? shape[i - 1] * shape[i - 2] : shape[i - 1];
no = 1;
for (let j = 0; j < i; j++) {
no *= shape[j];
}
curr_arr = formChunks(size, no, base_arr);
base_arr = curr_arr;
}
Expand Down
1 change: 1 addition & 0 deletions lib/calcShape.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* find the shape of array */
module.exports = function calcShape(arr) {
const shape = [];
if (!Array.isArray(arr)) return [1];
shape.push(arr.length);
let elem = arr[0];
while (Array.isArray(elem)) {
Expand Down
2 changes: 1 addition & 1 deletion lib/choose.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ module.exports = function choose(array, ...args) {
return array;
} else {
let arr = flatten(array),
shape = calc_shape(array),
shape = calcShape(array),
axis = args[0],
idx = args[1] || null,
range = args[2] || null,
Expand Down
35 changes: 19 additions & 16 deletions lib/fill.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,34 @@
/* fill: fills the vector according to passed args
"use strict";

/**
*
* @function fill - fills the vector according to passed args
*
* @shape : [Number] : The shape of the array to be filled (Required)
* @param {Array} shape - The shape of the array to be filled (Required)
*
* @initializer : 'String' : The distribution of elements to be filled
* @param {String} initializer - The distribution of elements to be filled
* {
* 1. 'linear' (random or uniform filling or single elem filling)
* 2. 'zeros' (filling with zeroes)
* 3. 'gaussian' (gaussian distribution)
* 4. 'custom' (fills the value acc to a passed function)
* }
*
* @...args : [Number] (Rest Parameters) :
* {
* 1. for linear, args.len === 0 => random filling
* args.len === 1 => fill with that value
* args.len === 2 => the max and min ranges with steps of min
* args.len === 3 => the max, min and the step
* 2. for gaussian, args[0] = mean (defaults to 0)
* args[1] = variance (defaults to 1)
* }
* @param {rest} ...args - {
* 1. for linear, args.len === 0 => random filling
* args.len === 1 => fill with that value
* args.len === 2 => the max and min ranges with steps of min
* args.len === 3 => the max, min and the step
* 2. for gaussian, args[0] = mean (defaults to 0)
* args[1] = variance (defaults to 1)
* }
*
* Returns : [Number] : The filled array
* @returns {Array} - The filled array
*
*/

module.exports = function fill(shape, dtype = 'float32', initializer = 'linear', ...args) {
const { calcSize, arrange, formArr } = require('./core');
module.exports = function fill(shape, initializer = 'linear', ...args) {
const { calcSize, arrange } = require('./core');
initializer = initializer || 'zeros';
let arr = [],
size = calcSize(shape, 'shape');
Expand Down Expand Up @@ -72,7 +75,7 @@ module.exports = function fill(shape, dtype = 'float32', initializer = 'linear',
arr[i] = args[0]();
}
}
let retval = arrange(shape, formArr(arr, dtype));
let retval = arrange(shape, arr);
return retval;
}

Expand Down
5 changes: 3 additions & 2 deletions lib/findDim.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* find the dimension of the array */
module.exports = function findDim(array) {
module.exports = function findDim(array, flag = 'array') {
const calcShape = require('./calcShape');
const shape = calcShape(array);
let shape = array;
if (flag === 'array') shape = calcShape(array);
return shape.length;
}
1 change: 1 addition & 0 deletions lib/flatten.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* function to convert n-dimension array into 1-D array */
module.exports = function flatten(arr) {
let flatarr = [];
if (!Array.isArray(arr)) return [arr];
for (let i of arr) {
if (Array.isArray(i))
flatarr = Array.prototype.concat.apply(flatarr, Array.prototype.concat.apply([], i));
Expand Down
4 changes: 2 additions & 2 deletions lib/formArr.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* Just constructors for typed arrays */
module.exports = function formArr(arr, dtype = 'float64') {
module.exports = function formArr(arr, dtype = 'array') {
switch (dtype) {
case 'uint8':
return new Uint8Array(arr);
Expand Down Expand Up @@ -29,6 +29,6 @@ module.exports = function formArr(arr, dtype = 'float64') {
return new Uint8ClampedArray(arr);

default:
return new Array(arr);
return Array.from(arr);
}
}
66 changes: 39 additions & 27 deletions lib/ndarray.js
Original file line number Diff line number Diff line change
@@ -1,84 +1,96 @@
"use strict";

/* A JS library for dealing with n-dimensional arrays.
* Referenced from numpy.
* Author : Anubhav Tiwari <atworkstudios@gmail.com>
*/


/* Make the Ndarray interface and make vecto faster ************
* _______ _____ _____ _____ ************
**** |#######| /#####\ |####\ /#####\ ************
**** |#| |#| |#| _____ |#| |#| |#| |#| ************
**** |#| |#| |#| |_____| |#|_|#| |#| |#| ************
**** |#| \#####/ |####/ \#####/ ************
***************************************************************/

const core = require('./core'),
math = require('../util/math');

class Ndarray {
constructor({ shape = [], dtype = 'float32', initializer = 'zeros', arr = [] } = {}, ...args) {
this.array = arr.length === 0 ? core.fill(shape, dtype, initializer, ...args) : arr;
this.shape = ((shape.length) > 0) ? shape : (core.calcShape(this.array));
this.size = core.calcSize(this.array);
this.dim = core.findDim(this.array);
constructor({ shape = [], dtype = 'float32', initializer = 'zeros', data = [] } = {}, ...args) {
this.shape = shape;
this.dtype = dtype;
this.flat = core.formArr(core.flatten(this.array), this.dtype);
this.size = core.calcSize(this.shape, 'shape');
this.dim = core.findDim(this.shape, 'shape');
this.data = data.length > 0 ? core.formArr(core.flatten(data), this.dtype) :
core.formArr(core.fill([this.size], initializer, ...args), this.dtype);
}

/* form a new Ndarray for the given array */
static array(arr, dtype = 'float32') {
return new Ndarray({ dtype: dtype, arr: arr });
return new Ndarray({ dtype: dtype, data: arr });
}

/* make a new zero Ndarray */
static zeroes(shape) {
return new Ndarray({ shape, dtype: 'float32', initializer: 'zeros' });
}

/* ---------------------------------------------------------------- */


/* sum of 2 Ndarrays */
add(v2) {
return math.sum(this.array, v2.array);
return math.sum(this.data, v2.array);
}

/* get the shaped data out as ndarray */
val() {
return core.arrange(this.shape, core.formArr(this.data));
}

/* reshapes the Ndarray only if for the new shape the number of elements remain same */
reshape(newShape) {
if (core.calcSize(newShape, 'shape') === this.size) {
const tempArr = this.flat;
this.shape = newShape;
this.arrange(tempArr);
} else {
return new Error(`Resizing error : can't change the shape from ${this.shape} to ${newShape}`);
throw new Error(
`Resizing error : can't change the shape from ${this.shape} to ${newShape}`
);
}
}

/* changes the shape and size of the Ndarray in place */
resize(newShape) {
const tempArr = this.flat;
this.shape = newShape;
this.size = core.calcSize(newShape);
this.arrange(tempArr);
this.size = core.calcSize(newShape, 'shape');
this.dim = core.findDim(this.shape, 'shape');
this.arrange(this.val());
}

arrange(elemsArr) {
this.array = core.arrange(this.shape, elemsArr);
this.flatten();
this.size = core.calcSize(this.array);
this.shape = core.calcShape(this.array);
this.dim = core.findDim(this.array);
this.data = core.formArr(core.flatten(core.arrange(this.shape, elemsArr)), this.dtype);
}

fill(initializer, ...args) {
this.array = core.fill(this.shape, this.dtype, initializer, ...args);
this.flatten();
this.data = core.formArr(core.fill([this.size], initializer, ...args), this.dtype);
}

clip(minVal, maxVal) {
this.arrange(core.clip(this.array, minVal, maxVal));
this.arrange(core.clip(this.val(), minVal, maxVal));
}

flatten() {
this.flat = core.formArr(core.flatten(this.array), this.dtype);
return this.flat;
return this.data;
}

transpose() {
return core.transpose(this.array, this.dtype);
return core.transpose(this.val(), this.dtype);
}

pad(padding) {
return math.pad(this.val(), padding);
}
}


module.exports = Ndarray;
84 changes: 84 additions & 0 deletions lib/ndarray.proto.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
'use strict';

/**
* A JS library for dealing with n-dimensional arrays.
* Referenced from numpy.
* Author : Anubhav Tiwari <atworkstudios@gmail.com>
*
*/

/* Make the Ndarray interface and make vecto faster ************
* _______ _____ _____ _____ ************
**** |#######| /#####\ |####\ /#####\ ************
**** |#| |#| |#| _____ |#| |#| |#| |#| ************
**** |#| |#| |#| |_____| |#|_|#| |#| |#| ************
**** |#| \#####/ |####/ \#####/ ************
***************************************************************/

const core = require('./core'),
math = require('../util/math');

class Ndarray {
constructor({ shape = [], dtype = 'float32', initializer = 'zeros', data = [] } = {}, ...args) {
this.shape = shape;
this.size = core.calcSize(this.shape, 'shape');
this.dim = core.findDim(this.shape, 'shape');
this.dtype = dtype;
this.flat = [];
data.length > 0 ? this.flatten(data) :
this.flatten(core.fill(this.shape, initializer, ...args));
}

/* form a new Ndarray for the given array */
static array(arr, dtype = 'float32') {
return new Ndarray({ dtype: dtype, data: arr });
}

/* sum of 2 Ndarrays */
add(v2) {
return math.sum(this.array, v2.array);
}

/* reshapes the Ndarray only if for the new shape the number of elements remain same */
reshape(newShape) {
if (core.calcSize(newShape, 'shape') === this.size) {
const tempArr = core.formArr(this.flat);
this.shape = newShape;
this.flatten(tempArr);
} else {
return new Error(`Resizing error : can't change the shape from ${this.shape} to ${newShape}`);
}
}

/* changes the shape and size of the Ndarray in place */
resize(newShape) {
const tempArr = core.formArr(this.flat);
this.shape = newShape;
this.size = core.calcSize(newShape);
this.flatten(tempArr);
}

arrange(elemsArr) {
this.flatten(elemsArr);
}

fill(initializer, ...args) {
let array = core.fill(this.shape, this.dtype, initializer, ...args);
this.flatten(array);
}

clip(minVal, maxVal) {
this.arrange(core.clip(this.array, minVal, maxVal));
}

flatten(data) {
this.flat = core.formArr(core.flatten(data), this.dtype);
}

transpose() {
return core.transpose(this.array, this.dtype);
}

}

module.exports = Ndarray;
4 changes: 3 additions & 1 deletion lib/transpose.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"use strict";

/* function to find the transpose */
module.exports = function transpose(arr, dtype = 'float32') {
const { calcShape, flatten, formArr, arrange } = require('./core');
Expand All @@ -14,4 +16,4 @@ module.exports = function transpose(arr, dtype = 'float32') {
}
}
return arrange([c, r], b);
}
}

0 comments on commit 8ce9c5e

Please sign in to comment.