This repository has been archived by the owner on Oct 17, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
416 additions
and
237 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.