Skip to content

Commit

Permalink
[TESTS] mget and raw mget methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
kgryte committed Jun 2, 2015
1 parent 81993c0 commit 8aaae36
Show file tree
Hide file tree
Showing 8 changed files with 295 additions and 73 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ __Note__: out-of-bounds indices will return a value of `undefined`.
<a name="matrix-mget"></a>
#### Matrix.prototype.mget( idx[, cols] )

Returns multiple `Matrix` elements. If provided a single argument, the method treats `idx` as an `array` of [linear indices](#linear-indexing) (`idx[i] >= 0`) and returns a plain `array` of numeric values. Otherwise, `idx` and `cols` are `integer` arrays which specify row and column indices and the method returns a new `Matrix` instance.
Returns multiple `Matrix` elements. If provided a single argument, the method treats `idx` as an `array` of [linear indices](#linear-indexing) (`idx[i] >= 0`) and returns a new `Matrix` instance having a single row. Otherwise, `idx` and `cols` are `integer` arrays which specify row and column indices and the method returns a new `Matrix` instance having dimensions determined by the number of defined rows and columns.

``` javascript
var data = new Int8Array( 10 );
Expand All @@ -484,7 +484,9 @@ var mat = matrix( data, [5,2] );

// Scramble the second column:
var vals = mat.mget( [1,5,3,9,7] );
// returns [2,10,6,18,14]
/*
[ 2, 10, 6, 18, 14 ]
*/

// Extract select rows and columns in arbitrary order:
var mat1 = mat.mget( [1,3,2], [1] );
Expand Down
14 changes: 6 additions & 8 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,20 @@ TODO
- lower-level API should __not__ accept plain `arrays`
- document this discrepancy
- document what input `data` is accepted
3. if provided `data` and not a `dtype`, should determine the type from the `data`
3.
4.validate.io-matrix / validate.io-matrix-like
5. `mget`
- should it return typed array results?
- or just a new 1d matrix or the same data type?
6.
5.



#### Tests

1.
2. matrix
3. matrix.raw
4. mget
5. mget.raw
- casting
3.
4.
5.
6. mset
7. mset.raw
8. sget
Expand Down
9 changes: 4 additions & 5 deletions lib/matrix.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,19 @@ var BTYPES = require( './btypes.js' ),
* @returns {Matrix} Matrix instance
*/
function matrix() {
var nargs,
dtype,
var dtype,
ndims,
shape,
data,
vFLG,
len,
i;

nargs = arguments.length;
if ( nargs === 1 ) {
if ( arguments.length === 1 ) {
shape = arguments[ 0 ];
vFLG = 2; // arg #s
}
else if ( nargs === 2 ) {
else if ( arguments.length === 2 ) {
if ( isString( arguments[ 1 ] ) ) {
shape = arguments[ 0 ];
dtype = arguments[ 1 ];
Expand Down Expand Up @@ -80,6 +78,7 @@ function matrix() {
len *= shape[ i ];
}
if ( vFLG === 123 || vFLG === 12 ) {
// FIXME: this currently overrides when `dtype` has been explicitly specified; need to set to diff var name and then compare to determine if need to cast.
dtype = getType( data );
if ( dtype === null ) {
throw new TypeError( 'matrix()::invalid input argument. Input data must be a valid type. Consult the documentation for a list of valid data types. Value: `' + data + '`.' );
Expand Down
9 changes: 3 additions & 6 deletions lib/matrix.raw.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,17 @@ var BTYPES = require( './btypes.js' );
* @returns {Matrix} Matrix instance
*/
function matrix() {
var nargs,
dtype,
var dtype,
ndims,
shape,
data,
len,
i;

nargs = arguments.length;
if ( nargs === 1 ) {
if ( arguments.length === 1 ) {
shape = arguments[ 0 ];
}
else if ( nargs === 2 ) {
else if ( arguments.length === 2 ) {
if ( isString( arguments[ 1 ] ) ) {
shape = arguments[ 0 ];
dtype = arguments[ 1 ];
Expand Down Expand Up @@ -76,7 +74,6 @@ function matrix() {
}
data = new BTYPES[ dtype ]( len );
}

// Return a new Matrix instance:
return new Matrix( data, shape, dtype );
} // end FUNCTION matrix()
Expand Down
78 changes: 46 additions & 32 deletions lib/mget.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,77 +18,91 @@ var BTYPES = require( './btypes.js' );
*
* @param {Number[]|Null} i - linear/row indices
* @param {Number[]|Null} [j] - column indices
* @returns {Number[]|Matrix} matrix elements or a Matrix instance
* @returns {Matrix} a new Matrix instance
*/
function mget( rows, cols ) {
/*jshint validthis:true */
var nRows,
nCols,
out,
d,
v,
s0, s1, s2, s3,
r, dr,
i, j, m, n;

if ( arguments.length < 2 ) {
i = rows;
if ( !isNonNegativeIntegerArray( i ) ) {
throw new TypeError( 'mget()::invalid input argument. Linear indices must be specified as a nonnegative integer array. Value: `' + i + '`.' );
if ( !isNonNegativeIntegerArray( rows ) ) {
throw new TypeError( 'mget()::invalid input argument. Linear indices must be specified as a nonnegative integer array. Value: `' + rows + '`.' );
}
out = [];
for ( n = 0; n < i.length; n++ ) {
v = this.data[ i[n] ];
if ( v !== void 0 ) {
out.push( v );
// Filter the input indices to ensure within bounds...
i = [];
for ( n = 0; n < rows.length; n++ ) {
if ( rows[ n ] < this.length ) {
i.push( rows[ n ] );
}
}
j = i.length;

// Create a row vector (matrix):
d = new BTYPES[ this.dtype ]( j );
out = new this.constructor( d, [1,j], this.dtype );

for ( n = 0; n < j; n++ ) {
d[ n ] = this.data[ i[n] ];
}
} else {
nRows = this.shape[ 0 ];
if ( rows === null ) {
nRows = this.shape[ 0 ];
i = new Array( nRows );
for ( n = 0; n < nRows; n++ ) {
i[ n ] = n;
}
}
else if ( !isNonNegativeIntegerArray( rows ) ) {
throw new TypeError( 'mget()::invalid input argument. Row indices must be specified as a nonnegative integer array. Value: `' + rows + '`.' );
else if ( isNonNegativeIntegerArray( rows ) ) {
i = [];
for ( n = 0; n < rows.length; n++ ) {
if ( rows[ n ] < nRows ) {
i.push( rows[ n ] );
}
}
}
else {
nRows = rows.length;
i = rows;
throw new TypeError( 'mget()::invalid input argument. Row indices must be specified as a nonnegative integer array. Value: `' + rows + '`.' );
}

nCols = this.shape[ 1 ];
if ( cols === null ) {
nCols = this.shape[ 1 ];
j = new Array( nCols );
for ( n = 0; n < nCols; n++ ) {
j[ n ] = n;
}
}
else if ( !isNonNegativeIntegerArray( cols ) ) {
throw new TypeError( 'mget()::invalid input argument. Column indices must be specified as a nonnegative integer array. Value: `' + cols + '`.' );
else if ( isNonNegativeIntegerArray( cols ) ) {
j = [];
for ( n = 0; n < cols.length; n++ ) {
if ( cols[ n ] < nCols ) {
j.push( cols[ n ] );
}
}
}
else {
nCols = cols.length;
j = cols;
throw new TypeError( 'mget()::invalid input argument. Column indices must be specified as a nonnegative integer array. Value: `' + cols + '`.' );
}
nRows = i.length;
nCols = j.length;

d = new BTYPES[ this.dtype ]( nRows*nCols );
out = new this.constructor( d, [nRows,nCols], this.dtype );

if ( nRows && nCols ) {
s0 = this.strides[ 0 ];
s1 = this.strides[ 1 ];
s2 = out.strides[ 0 ];
s3 = out.strides[ 1 ];
for ( m = 0; m < nRows; m++ ) {
r = i[ m ] * s0;
dr = m * s2;
for ( n = 0; n < nCols; n++ ) {
d[ dr + n*s3 ] = this.data[ r + j[n]*s1 ];
}

s0 = this.strides[ 0 ];
s1 = this.strides[ 1 ];
s2 = out.strides[ 0 ];
s3 = out.strides[ 1 ];
for ( m = 0; m < nRows; m++ ) {
r = i[ m ] * s0;
dr = m * s2;
for ( n = 0; n < nCols; n++ ) {
d[ dr + n*s3 ] = this.data[ r + j[n]*s1 ];
}
}
}
Expand Down
38 changes: 18 additions & 20 deletions lib/mget.raw.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,28 @@ var BTYPES = require( './btypes.js' );
*
* @param {Number[]|Null} i - linear/row indices
* @param {Number[]|Null} [j] - column indices
* @returns {Number[]|Matrix} matrix elements or a Matrix instance
* @returns {Matrix} a new Matrix instance
*/
function mget( rows, cols ) {
/*jshint validthis:true */
var nRows,
nCols,
out,
d,
v,
s0, s1, s2, s3,
r, dr,
i, j, m, n;

if ( arguments.length < 2 ) {
i = rows;
out = [];
for ( n = 0; n < i.length; n++ ) {
v = this.data[ i[n] ];
if ( v !== void 0 ) {
out.push( v );
}
j = i.length;

// Create a row vector (matrix):
d = new BTYPES[ this.dtype ]( j );
out = new this.constructor( d, [1,j], this.dtype );

for ( n = 0; n < j; n++ ) {
d[ n ] = this.data[ i[n] ];
}
} else {
if ( rows === null ) {
Expand Down Expand Up @@ -61,18 +62,15 @@ function mget( rows, cols ) {
d = new BTYPES[ this.dtype ]( nRows*nCols );
out = new this.constructor( d, [nRows,nCols], this.dtype );

if ( nRows && nCols ) {
s0 = this.strides[ 0 ];
s1 = this.strides[ 1 ];
s2 = out.strides[ 0 ];
s3 = out.strides[ 1 ];
for ( m = 0; m < nRows; m++ ) {
r = i[ m ] * s0;
dr = m * s2;
for ( n = 0; n < nCols; n++ ) {
d[ dr + n*s3 ] = this.data[ r + j[n]*s1 ];
}

s0 = this.strides[ 0 ];
s1 = this.strides[ 1 ];
s2 = out.strides[ 0 ];
s3 = out.strides[ 1 ];
for ( m = 0; m < nRows; m++ ) {
r = i[ m ] * s0;
dr = m * s2;
for ( n = 0; n < nCols; n++ ) {
d[ dr + n*s3 ] = this.data[ r + j[n]*s1 ];
}
}
}
Expand Down
Loading

0 comments on commit 8aaae36

Please sign in to comment.