Skip to content

Commit

Permalink
[UPDATE] lib, tests, examples, benchmarks.
Browse files Browse the repository at this point in the history
  • Loading branch information
kgryte committed Jun 2, 2015
1 parent 6e333cc commit bc754b0
Show file tree
Hide file tree
Showing 6 changed files with 180 additions and 27 deletions.
45 changes: 39 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
matrix
Matrix
===
[![NPM version][npm-image]][npm-url] [![Build Status][travis-image]][travis-url] [![Coverage Status][coveralls-image]][coveralls-url] [![Dependencies][dependencies-image]][dependencies-url]

> Validates if a value is a Matrix.
> Validates if a value is a [Matrix](https://github.com/compute-io/matrix).

## Installation
Expand All @@ -17,18 +17,51 @@ For use in the browser, use [browserify](https://github.com/substack/node-browse
## Usage

``` javascript
var foo = require( 'validate.io-matrix' );
var isMatrix = require( 'validate.io-matrix' );
```

#### foo( value )
#### isMatrix( value )

What does this function do?
Validates if a value is a [Matrix](https://github.com/compute-io/matrix).

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

var mat = matrix( [10,10] );

var bool = isMatrix( mat );
// returns true
```


## Examples

``` javascript
var foo = require( 'validate.io-matrix' );
var matrix = require( 'compute-matrix' ),
isMatrix = require( 'validate.io-matrix' );

var mat = matrix( [10,10] );

console.log( isMatrix( mat ) );
// returns true

console.log( isMatrix( [] ) );
// returns false

console.log( isMatrix( {} ) );
// returns false

console.log( isMatrix({
'data': new Int8Array( 10 ),
'shape': [5,2],
'strides': [2,1],
'dtype': 'int8',
'length': 10
}));
// returns false

console.log( isMatrix( null ) );
// returns false
```

To run the example code from the top-level application directory,
Expand Down
63 changes: 63 additions & 0 deletions benchmark/b.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
'use strict';

// MODULES //

var matrix = require( 'compute-matrix' ),
isMatrixLike = require( 'validate.io-matrix-like' ),
isMatrix = require( './../lib' );


// VARIABLES //

var start,
stop,
len,
res,
mat,
b,
i;


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

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


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

len = 1e6;
res = new Array( 1 );

mat = matrix( [10,10] );

// Matrix-like check:
start = process.hrtime();
for ( i = 0; i < len; i++ ) {
b = isMatrixLike( mat );
}
stop = process.hrtime( start );

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

// Matrix check:
start = process.hrtime();
for ( i = 0; i < len; i++ ) {
b = isMatrix( mat );
}
stop = process.hrtime( start );

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


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

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

26 changes: 25 additions & 1 deletion examples/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,27 @@
'use strict';

var foo = require( './../lib' );
var matrix = require( 'compute-matrix' ),
isMatrix = require( './../lib' );

var mat = matrix( [10,10] );

console.log( isMatrix( mat ) );
// returns true

console.log( isMatrix( [] ) );
// returns false

console.log( isMatrix( {} ) );
// returns false

console.log( isMatrix({
'data': new Int8Array( 10 ),
'shape': [5,2],
'strides': [2,1],
'dtype': 'int8',
'length': 10
}));
// returns false

console.log( isMatrix( null ) );
// returns false
24 changes: 11 additions & 13 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,23 @@

// MODULES //

// var module_alias = require( 'module_name' );
var typeName = require( 'type-name' );


// FUNCTIONS //

// any private utility functions go here...


// {{ FOO }} //
// ISMATRIX //

/**
* FUNCTION: foo()
* {{ foo description }}.
* FUNCTION: isMatrix( value )
* Validates if a value is a Matrix.
*
* @param {*} value - value to validate
* @returns {Boolean} boolean indicating if a value is a Matrix
*/
function foo() {

} // end FUNCTION foo()
function isMatrix( value ) {
return typeName( value ) === 'Matrix';
} // end FUNCTION isMatrix()


// EXPORTS //

module.exports = foo;
module.exports = isMatrix;
20 changes: 16 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,31 @@
"validate.io",
"validate",
"validation",
"validator"
"validator",
"valid",
"matrix",
"compute.io",
"compute",
"is",
"ismatrix",
"constructor",
"class",
"type"
],
"bugs": {
"url": "https://github.com/validate-io/matrix/issues"
},
"dependencies": {},
"dependencies": {
"type-name": "^1.0.1"
},
"devDependencies": {
"chai": "2.x.x",
"mocha": "2.x.x",
"coveralls": "^2.11.1",
"istanbul": "^0.3.0",
"jshint": "2.x.x",
"jshint-stylish": "^1.0.0"
"jshint-stylish": "^1.0.0",
"mocha": "2.x.x",
"validate.io-matrix-like": "^1.0.0"
},
"license": "MIT"
}
29 changes: 26 additions & 3 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@
var // Expectation library:
chai = require( 'chai' ),

// Module to create matrices:
matrix = require( 'compute-ndarray' ),

// Module to be tested:
lib = require( './../lib' );
isMatrix = require( './../lib' );


// VARIABLES //
Expand All @@ -21,9 +24,29 @@ var expect = chai.expect,
describe( 'validate.io-matrix', function tests() {

it( 'should export a function', function test() {
expect( lib ).to.be.a( 'function' );
expect( isMatrix ).to.be.a( 'function' );
});

it( 'should positively validate', function test() {
assert.isTrue( isMatrix( matrix( [10,10] ) ) );
});

it( 'should do something' );
it( 'should negatively validate', function test() {
var values = [
5,
'5',
null,
undefined,
NaN,
true,
[],
{},
function ndarray(){}
];

for ( var i = 0; i < values.length; i++ ) {
assert.notOk( isMatrix( values[i] ), values[ i ] );
}
});

});

0 comments on commit bc754b0

Please sign in to comment.