Skip to content

Commit

Permalink
[UPDATE] README, examples. matrix dep.
Browse files Browse the repository at this point in the history
  • Loading branch information
kgryte committed Jun 9, 2015
1 parent 08b7e4b commit 08d8d35
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 106 deletions.
95 changes: 52 additions & 43 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ to-matrix
===
[![NPM version][npm-image]][npm-url] [![Build Status][travis-image]][travis-url] [![Coverage Status][coveralls-image]][coveralls-url] [![Dependencies][dependencies-image]][dependencies-url]

> Construct a matrix from an array-of-arrays.
> Construct a [matrix](https://github.com/dstructs/matrix) from an array of arrays.

## Installation
Expand All @@ -22,8 +22,7 @@ var toMatrix = require( 'compute-to-matrix' );

#### toMatrix( arr[, options ] )

This function converts the input argument `arr`, an array of arrays, into
a [matrix](https://github.com/compute-io/matrix).
Constructs a [`matrix`](https://github.com/dstructs/matrix) from an `array` of `arrays`.

``` javascript
var arr = [
Expand All @@ -34,86 +33,96 @@ var arr = [

var mat = toMatrix( arr );
/*
[ 1, 2, 3
4, 5, 6,
7, 8, 9 ]
[ 1 2 3
4 5 6
7 8 9 ]
*/
```

The function accepts the following `options`:

* __accessor__: accessor `function` for accessing `array` values
* __dtype__: data type of the `matrix`
* __dtype__: [`matrix`](https://github.com/dstructs/matrix) data type. Default: `float64`.

For non-numeric `arrays`, provide an accessor `function` for accessing `array` values.
For non-numeric nested `arrays`, provide an accessor `function` for accessing `array` values.

``` javascript
var X = [
var arr = [
[ {'x': 1}, {'x': 0}, {'x': 0} ],
[ {'x': 0}, {'x': 1}, {'x': 0} ],
[ {'x': 0}, {'x': 0}, {'x': 1} ],
]
];

function getValue( d ) {
return d.x
function getValue( d, i, j ) {
return d.x;
}

var mat = toMatrix( X, {'accessor': getValue} );
var mat = toMatrix( arr, {
'accessor': getValue
});
/*
[ 1, 0, 0
0, 1, 0,
0, 0, 1 ]
[ 1 0 0
0 1 0
0 0 1 ]
*/
```

The `accessor` function receives three parameters:

- `d`: the current datum
- `i`: the row index of the current element
- `j`: the column index of the current element
The `accessor` function is provided three arguments:

The function also accepts an `dtype` option, which specifies the data type of the matrix to be created. The default value is `float64`.
- __d__: the current element
- __i__: the row index of the current element
- __j__: the column index of the current element

The following `dtypes` are accepted:

* `int8`
* `uint8`
* `uint8_clamped`
* `int16`
* `uint16`
* `int32`
* `uint32`
* `float32`
* `float64`
By default, the [`matrix`](https://github.com/dstructs/matrix) elements are floating-point 64-bit numbers (`float64`). To specify a different data type, set the `dtype` option.

``` javascript
var X = [
var arr = [
[ 1.1, 2.3 ],
[ 3.2, 4.1 ]
]
var mat = toMatrix( X, {'dtype': 'int32'} );
];

var mat = toMatrix( arr, {
'dtype': 'int32'
});
/*
[ 1, 2,
3, 4 ]
[ 1 2
3 4 ]
*/
```

For supported data types, see [dstructs-matrix](https://github.com/dstructs/matrix).



## Examples

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

var X = [
var nRows,
nCols,
arr,
mat,
i, j;

arr = [
[ 2, 4, 3, 1],
[ 1, 2, 2, 1],
[ 7, 3, 9, 7],
[ 11, 9, 9, 8],
[ 3, 2, 3, 1]
];

var m = toMatrix( X );
var val = m.get( 3, 0 );
// returns 11
nRows = arr.length;
nCols = arr[ 0 ].length;

mat = toMatrix( arr );

for ( i = 0; i < nRows; i++ ) {
for ( j = 0; j < nCols; j++ ) {
console.log( '(%d,%d) -> %d', i, j, mat.get( i, j ) );
}
}
```

To run the example code from the top-level application directory,
Expand Down Expand Up @@ -159,7 +168,7 @@ $ make view-cov

## Copyright

Copyright &copy; 2015. The Compute.io Authors.
Copyright &copy; 2015. The [Compute.io](https://github.com/compute-io) Authors.


[npm-image]: http://img.shields.io/npm/v/compute-to-matrix.svg
Expand Down
19 changes: 16 additions & 3 deletions examples/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,27 @@

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

var X = [
var nRows,
nCols,
arr,
mat,
i, j;

arr = [
[ 2, 4, 3, 1],
[ 1, 2, 2, 1],
[ 7, 3, 9, 7],
[ 11, 9, 9, 8],
[ 3, 2, 3, 1]
];

var m = toMatrix( X );
nRows = arr.length;
nCols = arr[ 0 ].length;

mat = toMatrix( arr );

console.log( m.get( 3, 0 ) );
for ( i = 0; i < nRows; i++ ) {
for ( j = 0; j < nCols; j++ ) {
console.log( '(%d,%d) -> %d', i, j, mat.get( i, j ) );
}
}
20 changes: 0 additions & 20 deletions lib/dtypes.js

This file was deleted.

75 changes: 38 additions & 37 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,84 +5,85 @@
var isArrayArray = require( 'validate.io-array-array' ),
isFunction = require( 'validate.io-function' ),
isObject = require( 'validate.io-object' ),
contains = require( 'validate.io-contains' );
ctors = require( 'compute-array-constructors' ),
matrix = require( 'dstructs-matrix' ).raw;

var matrix = require( 'compute-matrix' );

// VARIABLES //

var DTYPES = require( './dtypes.js' );

// TOMATRIX //
// TO MATRIX //

/**
* FUNCTION: toMatrix( arr[, options ] )
* Construct a matrix from an array of arrays.
*
* @param {Array} arr - input array of arrays
* @param {Array[]} arr - input array of arrays
* @param {Object} [options] - function options
* @param {String} [options.dtype='float64'] - specifies the data type of the created matrix
* @param {Function} [options.accessor] - accessor function for accessing object array values
* @returns {Matrix} an instance of `Matrix` with values equal to those in arr
* @returns {Matrix} new Matrix instance
*/
function toMatrix( arr, opts ) {

var nCols, nRows, i, j, clbk, dtype, ret, val;
/* jshint newcap:false */
var dtype = 'float64',
nCols,
nRows,
clbk,
ctor,
out,
d,
r, i, j;

if ( !isArrayArray( arr ) ) {
throw new TypeError( 'to-matrix()::invalid input argument. The first argument must be an array of arrays. Value: `' + arr + '`.' );
throw new TypeError( 'toMatrix()::invalid input argument. The first argument must be an array of arrays. Value: `' + arr + '`.' );
}

if ( arguments.length > 1 ) {
if ( !isObject( opts ) ) {
throw new TypeError( 'to-matrix()::invalid input argument. Options argument must be an object. Value: `' + opts + '`.' );
throw new TypeError( 'toMatrix()::invalid input argument. Options argument must be an object. Value: `' + opts + '`.' );
}

if ( opts.hasOwnProperty( 'dtype' ) ) {
dtype = opts.dtype;
if ( !contains( DTYPES, dtype ) ) {
throw new TypeError( 'to-matrix()::invalid input argument. Unrecognized/unsupported data type. Value: `' + dtype + '`.' );
ctor = ctors( dtype );
if ( dtype === 'generic' || ctor === null ) {
throw new TypeError( 'toMatrix()::invalid option. Unrecognized/unsupported data type. Option: `' + dtype + '`.' );
}
}

if ( opts.hasOwnProperty( 'accessor' ) ) {
clbk = opts.accessor;
if ( !isFunction( clbk ) ) {
throw new TypeError( 'to-matrix()::invalid option. Accessor must be a function. Option: `' + clbk + '`.' );
throw new TypeError( 'toMatrix()::invalid option. Accessor must be a function. Option: `' + clbk + '`.' );
}
}
}

nRows = arr.length;
nCols = arr[0].length;

if ( dtype ) {
ret = matrix( [ nRows, nCols ], dtype );
} else {
ret = matrix( [ nRows, nCols ] );
nCols = arr[ 0 ].length;
for ( i = 1; i < nRows; i++ ) {
for ( j = 0; j < nCols; j++ ) {
if ( arr[ i ].length !== nCols ) {
throw new TypeError( 'toMatrix()::invalid input argument. Every nested array must have the same length. Value: `' + arr + '`.' );
}
}
}
if ( !ctor ) {
ctor = ctors( dtype );
}
d = new ctor( nRows*nCols );
out = matrix( d, [nRows,nCols], dtype );

if ( !clbk ) {
if ( clbk ) {
for ( i = 0; i < nRows; i++ ) {
r = i * nCols;
for ( j = 0; j < nCols; j++ ) {
if ( arr[i].length !== nCols ) {
throw new TypeError( 'to-matrix()::invalid input argument. Each array in the input array of arrays must have the same length. Value: `' + arr + '`.' );
}
ret.set( i, j, arr[ i ][ j ] );
d[ r + j ] = clbk( arr[ i ][ j ], i, j );
}
}
} else {
for ( i = 0; i < nRows; i++ ) {
r = i * nCols;
for ( j = 0; j < nCols; j++ ) {
if ( arr[i].length !== nCols ) {
throw new TypeError( 'to-matrix()::invalid input argument. Each array in the input array of arrays must have the same length. Value: `' + arr + '`.' );
}
val = clbk( arr[ i ][ j ], i, j );
ret.set( i, j, val );
d[ r + j ] = arr[ i ][ j ];
}
}
}
return ret;
return out;
} // end FUNCTION toMatrix()


Expand Down
13 changes: 10 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
{
"name": "Philipp Burckhardt",
"email": "pburckhardt@outlook.com"
},
{
"name": "Athan Reines",
"email": "kgryte@gmail.com"
}
],
"scripts": {
Expand Down Expand Up @@ -38,19 +42,22 @@
"structure",
"data structure",
"array",
"array of arrays"
"array of arrays",
"nested",
"arrays"
],
"bugs": {
"url": "https://github.com/compute-io/to-matrix/issues"
},
"dependencies": {
"compute-array-constructors": "^1.0.0",
"dstructs-matrix": "^2.0.0",
"validate.io-array-array": "^1.0.0",
"validate.io-contains": "^1.0.0",
"validate.io-function": "^1.0.2",
"validate.io-object": "^1.0.4"
},
"devDependencies": {
"chai": "2.x.x",
"chai": "3.x.x",
"mocha": "2.x.x",
"coveralls": "^2.11.1",
"istanbul": "^0.3.0",
Expand Down

0 comments on commit 08d8d35

Please sign in to comment.