Skip to content

Commit

Permalink
[UPDATE] provide raw api.
Browse files Browse the repository at this point in the history
  • Loading branch information
kgryte committed May 29, 2015
1 parent b965ec0 commit 009eecb
Show file tree
Hide file tree
Showing 10 changed files with 310 additions and 116 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,18 @@ For use in the browser, use [browserify](https://github.com/substack/node-browse
## Usage

``` javascript
var Matrix = require( 'compute-matrix' );
var matrix = require( 'compute-matrix' );
```

#### Matrix()
#### matrix()

What does this function do?


## Examples

``` javascript
var Matrix = require( 'compute-matrix' );
var matrix = require( 'compute-matrix' );
```

To run the example code from the top-level application directory,
Expand Down
4 changes: 2 additions & 2 deletions benchmark/b.get.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

// MODULES //

var Matrix = require( './../lib' );
var matrix = require( './../lib' );


// VARIABLES //
Expand Down Expand Up @@ -32,7 +32,7 @@ len = 1e6;

res = new Array( 2 );

m = new Matrix( [128,128] );
m = matrix( [128,128] );

start = process.hrtime();
for ( i = 0; i < len; i++ ) {
Expand Down
4 changes: 2 additions & 2 deletions benchmark/b.set.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

// MODULES //

var Matrix = require( './../lib' );
var matrix = require( './../lib' );


// VARIABLES //
Expand Down Expand Up @@ -31,7 +31,7 @@ len = 1e6;

res = new Array( 2 );

m = new Matrix( [128,128] );
m = matrix( [128,128] );

start = process.hrtime();
for ( i = 0; i < len; i++ ) {
Expand Down
4 changes: 2 additions & 2 deletions examples/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
'use strict';

var Matrix = require( './../lib' );
var matrix = require( './../lib' );

var m = new Matrix( [2,2] );
var m = matrix( [2,2] );

console.log( m );

Expand Down
120 changes: 120 additions & 0 deletions lib/ctor.js
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;
72 changes: 72 additions & 0 deletions lib/ctor.raw.js
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;
1 change: 1 addition & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
// EXPORTS //

module.exports = require( './matrix.js' );
module.exports.raw = require( './matrix.raw.js' );
Loading

0 comments on commit 009eecb

Please sign in to comment.