Skip to content

Commit

Permalink
[UPDATE] add toString method.
Browse files Browse the repository at this point in the history
  • Loading branch information
kgryte committed Jun 1, 2015
1 parent 9b24fa2 commit b7d706b
Show file tree
Hide file tree
Showing 7 changed files with 144 additions and 3 deletions.
34 changes: 34 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,40 @@ For further subsequence documentation, see [compute-indexspace](https://github.c



<a name="matrix-tostring"></a>
#### mat.toString()

Returns a `string` representation of a `Matrix`. This method is similar to [`Array#toString`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/toString), except that rows are delineated by __semicolons__ and column values are delineated by __commas__.

``` javascript
var data = new Int8Array( 10 );
for ( var i = 0; i < data.length; i++ ) {
data[ i ] = i;
}

var mat = matrix( data, [5,2] );

var str = mat.toString();
// 0,1;2,3;4,5;6,7;8,9
```

To construct an `array` of `arrays` from the `string` representation,

``` javascript
var rows,
cols,
i, j;

rows = str.split( ';' );
for ( i = 0; i < rows.length; i++ ) {
cols = rows[ i ].split( ',' );
rows[ i ] = new Array( cols.length );
for ( j = 0; j < cols.length; j++ ) {
rows[ i ][ j ] = parseFloat( cols[ j ] );
}
}
```


===
<a name="matrix-constructor"></a>
Expand Down
7 changes: 4 additions & 3 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,16 @@ TODO
- essentially an in-place map
- `mset`, `mget`
- use linear index
5. `toString` method
- `'a00,a01;a10,a11;a20,a21'` => use semicolons to demarcate rows
6. `toArray` method (???)
5.
6.
7.
8. should we allow negative indices for `iset`, `iget`, etc?
9.
10. pretty print a matrix
- separate module? => `compute-print-matrix`
- does not seem essential to core functionality
- what about a general `print` module, which accepts `arrays`, `matrices`, `ndarrays`, and `dataframes`?
- similar to `to-matrix`, `to-array`, etc.
11. validate.io-matrix / validate.io-matrix-like
12. benchmark fancy (subsequence) get versus normal `get`
13.
Expand Down
60 changes: 60 additions & 0 deletions benchmark/b.toString.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
'use strict';

// MODULES //

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


// VARIABLES //

var start,
stop,
iArr,
nRows,
nCols,
res,
len,
m, s,
i;


// --------------------------------------
// WARM-UP

len = 1e6;
for ( i = 0; i < len; i++ ) {
i = i;
}


// --------------------------------------
// BENCHMARK

len = 1e3;
nRows = 128;
nCols = 128;

res = new Array( 1 );

iArr = new Float64Array( nRows*nCols );
for ( i = 0; i < nRows*nCols; i++ ) {
iArr[ i ] = i;
}

m = matrix( iArr, [nRows,nCols] );

start = process.hrtime();
for ( i = 0; i < len; i++ ) {
s = m.toString();
}
stop = process.hrtime( start );

res[ 0 ] = stop[ 0 ] + stop[ 1 ]*1e-9;


// --------------------------------------
// RESULTS

console.log( 'toString:\t%d ops/sec', Math.floor( len/res[ 0 ] ) );
console.log( '\n' );

3 changes: 3 additions & 0 deletions examples/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,6 @@ mat.set( 1, 1, 5 );

// Confirm that the matrix element was set:
console.log( mat.get( 1, 1 ) );

// Convert the matrix to a string:
console.log( mat.toString() );
2 changes: 2 additions & 0 deletions lib/ctor.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ Matrix.prototype.get = require( './get.js' );
Matrix.prototype.iget = require( './iget.js' );
Matrix.prototype.sget = require( './sget.js' );

Matrix.prototype.toString = require( './toString.js' );


// EXPORTS //

Expand Down
2 changes: 2 additions & 0 deletions lib/ctor.raw.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ Matrix.prototype.get = require( './get.js' );
Matrix.prototype.iget = require( './iget.js' );
Matrix.prototype.sget = require( './sget.js' );

Matrix.prototype.toString = require( './toString.js' );


// EXPORTS //

Expand Down
39 changes: 39 additions & 0 deletions lib/toString.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
'use strict';

/**
* FUNCTION: toString()
* Returns a string representation of Matrix elements. Rows are delineated by semicolons. Column values are comma-delimited.
*
* @returns {String} string representation
*/
function toString() {
/* jshint validthis: true */
var nRows = this.shape[ 0 ],
nCols = this.shape[ 1 ],
s0 = this.strides[ 0 ],
s1 = this.strides[ 1 ],
m = nRows - 1,
n = nCols - 1,
str = '',
r,
i, j;

for ( i = 0; i < nRows; i++ ) {
r = i * s0;
for ( j = 0; j < nCols; j++ ) {
str += this.data[ r + j*s1 ];
if ( j < n ) {
str += ',';
}
}
if ( i < m ) {
str += ';';
}
}
return str;
} // end FUNCTION toString()


// EXPORTS //

module.exports = toString;

0 comments on commit b7d706b

Please sign in to comment.