-
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
10 changed files
with
310 additions
and
116 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
'use strict'; | ||
|
||
// MATRIX // | ||
|
||
/** | ||
* FUNCTION: Matrix( data, shape, dtype ) | ||
* Matrix constructor. | ||
* | ||
* @constructor | ||
* @param {Int8Array|Uint8Array|Uint8ClampedArray|Int16Array|Uint16Array|Int32Array|Uint32Array|Float32Array|Float64Array} data - input typed array | ||
* @param {Number[]} shape - matrix dimensions/shape | ||
* @param {String} dtype - matrix data type | ||
* @returns {Matrix} Matrix instance | ||
*/ | ||
function Matrix( data, shape, dtype ) { | ||
var strides, | ||
ndims, | ||
s, i; | ||
|
||
if ( !( this instanceof Matrix ) ) { | ||
return new Matrix( data, shape, dtype ); | ||
} | ||
ndims = shape.length; | ||
|
||
// Determine the matrix strides... | ||
strides = new Array( ndims ); | ||
s = 1; | ||
for ( i = ndims-1; i >= 0; i-- ) { | ||
strides[ i ] = s; | ||
s *= shape[ i ]; | ||
} | ||
|
||
// Underlying data type: | ||
Object.defineProperty( this, 'dtype', { | ||
'value': dtype, | ||
'configurable': false, | ||
'enumerable': true, | ||
'writable': false | ||
}); | ||
|
||
// Matrix dimensions: | ||
Object.defineProperty( this, 'shape', { | ||
'value': shape, | ||
'configurable': false, | ||
'enumerable': true, | ||
'writable': false | ||
}); | ||
|
||
// Matrix strides (non-enumerable): | ||
Object.defineProperty( this, 'strides', { | ||
'value': strides, | ||
'configurable': false, | ||
'enumerable': true, | ||
'writable': false | ||
}); | ||
|
||
// Number of matrix dimensions: | ||
Object.defineProperty( this, 'ndims', { | ||
'value': ndims, | ||
'configurable': false, | ||
'enumerable': true, | ||
'writable': false | ||
}); | ||
|
||
// Matrix length: | ||
Object.defineProperty( this, 'length', { | ||
'value': data.length, | ||
'configurable': false, | ||
'enumerable': true, | ||
'writable': false | ||
}); | ||
|
||
// Number of bytes used by the matrix elements: | ||
Object.defineProperty( this, 'nbytes', { | ||
'value': data.byteLength, | ||
'configurable': false, | ||
'enumerable': true, | ||
'writable': false | ||
}); | ||
|
||
// Matrix data store: | ||
Object.defineProperty( this, 'data', { | ||
'value': data, | ||
'configurable': false, | ||
'enumerable': true, | ||
'writable': false | ||
}); | ||
|
||
return this; | ||
} // end FUNCTION Matrix() | ||
|
||
/** | ||
* METHOD: get( i, j ) | ||
* Returns a matrix element based on the provided subscripts. | ||
* | ||
* @param {Number} i - row index | ||
* @param {Number} j - column index | ||
* @returns {Number} matrix element | ||
*/ | ||
Matrix.prototype.get = function( i, j ) { | ||
return this.data[ i*this.strides[0] + j*this.strides[1] ]; | ||
}; // end METHOD get() | ||
|
||
/** | ||
* METHOD: set( i, j, value ) | ||
* Sets a matrix element based on the provided subscripts. | ||
* | ||
* @param {Number} i - row index | ||
* @param {Number} j - column index | ||
* @param {Number} value - value to set | ||
* @returns {Number} matrix element | ||
*/ | ||
Matrix.prototype.set = function( i, j, v ) { | ||
return this.data[ i*this.strides[0] + j*this.strides[1] ] = v; | ||
}; // end METHOD get() | ||
|
||
|
||
// EXPORTS // | ||
|
||
module.exports = Matrix; |
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,72 @@ | ||
'use strict'; | ||
|
||
// MATRIX // | ||
|
||
/** | ||
* FUNCTION: Matrix( data, shape, dtype ) | ||
* Matrix constructor. | ||
* | ||
* @constructor | ||
* @param {Int8Array|Uint8Array|Uint8ClampedArray|Int16Array|Uint16Array|Int32Array|Uint32Array|Float32Array|Float64Array} data - input typed array | ||
* @param {Number[]} shape - matrix dimensions/shape | ||
* @param {String} dtype - matrix data type | ||
* @returns {Matrix} Matrix instance | ||
*/ | ||
function Matrix( data, shape, dtype ) { | ||
var strides, | ||
ndims, | ||
s, i; | ||
|
||
if ( !( this instanceof Matrix ) ) { | ||
return new Matrix( data, shape, dtype ); | ||
} | ||
ndims = shape.length; | ||
|
||
// Determine the matrix strides... | ||
strides = new Array( ndims ); | ||
s = 1; | ||
for ( i = ndims-1; i >= 0; i-- ) { | ||
strides[ i ] = s; | ||
s *= shape[ i ]; | ||
} | ||
|
||
this.dtype = dtype; | ||
this.shape = shape; | ||
this.strides = strides; | ||
this.ndims = ndims; | ||
this.length = data.length; | ||
this.nbytes = data.byteLength; | ||
this.data = data; | ||
|
||
return this; | ||
} // end FUNCTION Matrix() | ||
|
||
/** | ||
* METHOD: get( i, j ) | ||
* Returns a matrix element based on the provided subscripts. | ||
* | ||
* @param {Number} i - row index | ||
* @param {Number} j - column index | ||
* @returns {Number} matrix element | ||
*/ | ||
Matrix.prototype.get = function( i, j ) { | ||
return this.data[ i*this.strides[0] + j*this.strides[1] ]; | ||
}; // end METHOD get() | ||
|
||
/** | ||
* METHOD: set( i, j, value ) | ||
* Sets a matrix element based on the provided subscripts. | ||
* | ||
* @param {Number} i - row index | ||
* @param {Number} j - column index | ||
* @param {Number} value - value to set | ||
* @returns {Number} matrix element | ||
*/ | ||
Matrix.prototype.set = function( i, j, v ) { | ||
return this.data[ i*this.strides[0] + j*this.strides[1] ] = v; | ||
}; // end METHOD get() | ||
|
||
|
||
// EXPORTS // | ||
|
||
module.exports = Matrix; |
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 |
---|---|---|
|
@@ -3,3 +3,4 @@ | |
// EXPORTS // | ||
|
||
module.exports = require( './matrix.js' ); | ||
module.exports.raw = require( './matrix.raw.js' ); |
Oops, something went wrong.