Skip to content

Commit

Permalink
Merge cf0c2e9 into 1d8c933
Browse files Browse the repository at this point in the history
  • Loading branch information
Planeshifter committed Jun 9, 2015
2 parents 1d8c933 + cf0c2e9 commit b94263d
Show file tree
Hide file tree
Showing 16 changed files with 887 additions and 105 deletions.
11 changes: 9 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
language: node_js
node_js:
- "0.10"
- '0.12'
- '0.11'
- '0.10'
- '0.8'
- 'iojs'
before_install:
- npm update -g npm
after_script:
- npm run coveralls
- npm run coveralls

4 changes: 2 additions & 2 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2015 Philipp Burckhardt.
Copyright (c) 2015 The Compute.io Authors. All rights reserved.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand All @@ -18,4 +18,4 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
SOFTWARE.
15 changes: 10 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@
# Set the node.js environment to test:
NODE_ENV ?= test

# Kernel name:
KERNEL ?= $(shell uname -s)

ifeq ($(KERNEL), Darwin)
OPEN ?= open
else
OPEN ?= xdg-open
endif

# NOTES #

Expand All @@ -30,7 +38,7 @@ ISTANBUL_HTML_REPORT_PATH ?= $(ISTANBUL_OUT)/lcov-report/index.html
# JSHINT #

JSHINT ?= ./node_modules/.bin/jshint
JSHINT_REPORTER ?= ./node_modules/jshint-stylish/stylish.js
JSHINT_REPORTER ?= ./node_modules/jshint-stylish



Expand Down Expand Up @@ -98,8 +106,7 @@ test-istanbul-mocha: node_modules
view-cov: view-istanbul-report

view-istanbul-report:
open $(ISTANBUL_HTML_REPORT_PATH)

$(OPEN) $(ISTANBUL_HTML_REPORT_PATH)


# LINT #
Expand All @@ -114,7 +121,6 @@ lint-jshint: node_modules
./



# NODE #

# Installing node_modules:
Expand All @@ -132,7 +138,6 @@ clean-node:


# CLEAN #

.PHONY: clean

clean:
Expand Down
178 changes: 166 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Product
===
[![NPM version][npm-image]][npm-url] [![Build Status][travis-image]][travis-url] [![Coverage Status][coveralls-image]][coveralls-url] [![Dependencies][dependencies-image]][dependencies-url]

> Computes the product of an array.
> Computes the product of an array or matrix.

## Installation
Expand All @@ -19,14 +19,18 @@ For use in the browser, use [browserify](https://github.com/substack/node-browse
var prod = require( 'compute-prod' );
```

#### prod( arr[, accessor] )
#### prod( x[, options] )

Computes the product of an `array`. For numeric `arrays`,
Computes the product of the elements in `x`. `x` may be either an [`array`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array), [`typed array`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Typed_arrays), or [`matrix`](https://github.com/dstructs/matrix).

``` javascript
var arr = [ 1, 2, 3, 4 ];
var data = [ 1, 2, 3, 4 ];

var value = prod( arr );
var p = prod( data );
// returns 24

data = new Int8Array( data );
p = prod( data );
// returns 24
```

Expand All @@ -44,24 +48,174 @@ function getValue( d ) {
return d.x;
}

var value = prod( arr, getValue );
var value = prod( arr, {'accessor': getValue} );
// returns 24
```

__Note__: if provided an empty `array`, the method returns `null`.
If provided a [`matrix`](https://github.com/dstructs/matrix), the function accepts the following `options`:

* __dim__: dimension along which to compute the product. Default: `2` (along the columns).
* __dtype__: output [`matrix`](https://github.com/dstructs/matrix) data type. Default: `float64`.

By default, the function computes the product along the columns (`dim=2`).

``` javascript
var matrix = require( 'dstructs-matrix' ),
data,
mat,
p,
i;

data = new Int8Array( 9 );
for ( i = 0; i < data.length; i++ ) {
data[ i ] = i + 1;
}
mat = matrix( data, [3,3], 'int8' );
/*
[ 1 2 3
4 5 6
7 8 9 ]
*/

p = prod( mat );
/*
[ 6
120
504 ]
*/
```

To compute the product along the rows, set the `dim` option to `1`.

``` javascript
p = prod( mat, {
'dim': 1
});
/*
[ 28 80 162 ]
*/
```

By default, the output [`matrix`](https://github.com/dstructs/matrix) data type is `float64`. To specify a different output data type, set the `dtype` option.

``` javascript
p = prod( mat, {
'dim': 1,
'dtype': 'uint8'
});
/*
[ 28 80 162 ]
*/

var dtype = p.dtype;
// returns 'uint8'
```

If provided a [`matrix`](https://github.com/dstructs/matrix) having either dimension equal to `1`, the function treats the [`matrix`](https://github.com/dstructs/matrix) as a [`typed array`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Typed_arrays) and returns a `numeric` value.

``` javascript
data = [ 2, 4, 5 ];

// Row vector:
mat = matrix( new Int8Array( data ), [1,3], 'int8' );
p = prod( mat );
// returns 40

// Column vector:
mat = matrix( new Int8Array( data ), [3,1], 'int8' );
p = prod( mat );
// returns 40
```

If provided an empty [`array`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array), [`typed array`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Typed_arrays), or [`matrix`](https://github.com/dstructs/matrix), the function returns `null`.

``` javascript
p = prod( [] );
// returns null

p = prod( new Int8Array( [] ) );
// returns null

p = prod( matrix( [0,0] ) );
// returns null

p = prod( matrix( [0,10] ) );
// returns null

p = prod( matrix( [10,0] ) );
// returns null
```

## Examples

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

var data = new Array( 10 );
var matrix = require( 'dstructs-matrix' ),
prod = require( 'compute-prod' );

var data,
mat,
p,
i;

// ----
// Plain arrays...
var data = new Array( 100 );
for ( var i = 0; i < data.length; i++ ) {
data[ i ] = Math.round( Math.random() * 10 + 1 ) ;
data[ i ] = Math.round( Math.random() * 10 + 1 );
}
p = prod( data );
console.log( 'Arrays: %d\n', p );


// ----
// Object arrays (accessors)...
function getValue( d ) {
return d.x;
}
for ( i = 0; i < data.length; i++ ) {
data[ i ] = {
'x': data[ i ]
};
}
p = prod( data, {
'accessor': getValue
});
console.log( 'Accessors: %d\n', p );


// ----
// Typed arrays...
data = new Int32Array( 100 );
for ( i = 0; i < data.length; i++ ) {
data[ i ] = Math.round( Math.random() * 10 + 1 );
}
p = prod( data );


// ----
// Matrices (along rows)...
mat = matrix( data, [10,10], 'int32' );
p = prod( mat, {
'dim': 1
});
console.log( 'Matrix (rows): %s\n', p.toString() );


// ----
// Matrices (along columns)...
p = prod( mat, {
'dim': 2
});
console.log( 'Matrix (columns): %s\n', p.toString() );


console.log( prod( data ) );
// ----
// Matrices (custom output data type)...
p = prod( mat, {
'dtype': 'uint8'
});
console.log( 'Matrix (%s): %s\n', p.dtype, p.toString() );
```

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

## Copyright

Copyright &copy; 2015. Philipp Burckhardt.
Copyright &copy; 2015. The Compute.io Authors.


[npm-image]: http://img.shields.io/npm/v/compute-prod.svg
Expand Down
66 changes: 62 additions & 4 deletions examples/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,68 @@
'use strict';

var prod = require( './../lib' );
var matrix = require( 'dstructs-matrix' ),
prod = require( './../lib' );

var data = new Array( 10 );
var data,
mat,
p,
i;

// ----
// Plain arrays...
var data = new Array( 100 );
for ( var i = 0; i < data.length; i++ ) {
data[ i ] = Math.round( Math.random() * 10 + 1 ) ;
data[ i ] = Math.round( Math.random() * 10 + 1 );
}
p = prod( data );
console.log( 'Arrays: %d\n', p );


// ----
// Object arrays (accessors)...
function getValue( d ) {
return d.x;
}
for ( i = 0; i < data.length; i++ ) {
data[ i ] = {
'x': data[ i ]
};
}
p = prod( data, {
'accessor': getValue
});
console.log( 'Accessors: %d\n', p );


// ----
// Typed arrays...
data = new Int32Array( 100 );
for ( i = 0; i < data.length; i++ ) {
data[ i ] = Math.round( Math.random() * 10 + 1 );
}
p = prod( data );


// ----
// Matrices (along rows)...
mat = matrix( data, [10,10], 'int32' );
p = prod( mat, {
'dim': 1
});
console.log( 'Matrix (rows): %s\n', p.toString() );


// ----
// Matrices (along columns)...
p = prod( mat, {
'dim': 2
});
console.log( 'Matrix (columns): %s\n', p.toString() );


console.log( prod( data ) );
// ----
// Matrices (custom output data type)...
p = prod( mat, {
'dtype': 'uint8'
});
console.log( 'Matrix (%s): %s\n', p.dtype, p.toString() );

0 comments on commit b94263d

Please sign in to comment.