Skip to content

Commit

Permalink
Merge pull request #4 from compute-io/develop
Browse files Browse the repository at this point in the history
[UPDATE] support new matrix api.
  • Loading branch information
Planeshifter committed Jun 9, 2015
2 parents 1bd5d5d + f84b841 commit 9e670e1
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 10 deletions.
8 changes: 4 additions & 4 deletions README.md
Expand Up @@ -4,7 +4,7 @@ Mean

> Computes the [arithmetic mean](http://en.wikipedia.org/wiki/Arithmetic_mean).
The [arithmetic mean](http://en.wikipedia.org/wiki/Arithmetic_mean) is defined by
The [arithmetic mean](http://en.wikipedia.org/wiki/Arithmetic_mean) is defined as

<div class="equation" align="center" data-raw-text="\mu = \frac{1}{N} \sum_{i=0}^{N-1} x_i" data-equation="eq:arithmetic_mean">
<img src="https://cdn.rawgit.com/compute-io/mean/c98aa32b6fea5b040092dbf950cba79eb25e25b8/docs/img/eqn.svg" alt="Equation for the arithmetic mean.">
Expand All @@ -14,13 +14,13 @@ The [arithmetic mean](http://en.wikipedia.org/wiki/Arithmetic_mean) is defined b
where `x_0, x_1,...,x_{N-1}` are individual data values and `N` is the total number of values in the data set.


### Installation
## Installation

``` bash
$ npm install compute-mean
```

### Usage
## Usage

``` javascript
var mean = require( 'compute-mean' );
Expand Down Expand Up @@ -164,7 +164,7 @@ mu = mean( matrix( [10,0] ) );



### Examples
## Examples

``` javascript
var matrix = require( 'dstructs-matrix' ),
Expand Down
4 changes: 3 additions & 1 deletion lib/matrix.js
Expand Up @@ -14,6 +14,7 @@ function mean( out, mat, dim ) {
mu,
M, N,
s0, s1,
o,
i, j, k;

if ( dim === 1 ) {
Expand All @@ -32,8 +33,9 @@ function mean( out, mat, dim ) {
if ( M === 0 || N === 0 ) {
return null;
}
o = mat.offset;
for ( i = 0; i < M; i++ ) {
k = i * s0;
k = o + i*s0;
mu = 0;
for ( j = 0; j < N; j++ ) {
delta = mat.data[ k + j*s1 ] - mu;
Expand Down
6 changes: 3 additions & 3 deletions package.json
Expand Up @@ -44,16 +44,16 @@
},
"dependencies": {
"compute-array-constructors": "^1.0.0",
"dstructs-matrix": "^1.0.0",
"dstructs-matrix": "^2.0.0",
"validate.io-array-like": "^1.0.0",
"validate.io-function": "^1.0.2",
"validate.io-matrix-like": "^1.0.0",
"validate.io-matrix-like": "^1.0.2",
"validate.io-object": "^1.0.4",
"validate.io-positive-integer": "^1.0.0",
"validate.io-string-primitive": "^1.0.0"
},
"devDependencies": {
"chai": "2.x.x",
"chai": "3.x.x",
"coveralls": "^2.11.1",
"istanbul": "^0.3.0",
"jshint": "^2.x.x",
Expand Down
24 changes: 22 additions & 2 deletions test/test.matrix.js
@@ -1,4 +1,4 @@
/* global describe, it, require */
/* global describe, it, require, beforeEach */
'use strict';

// MODULES //
Expand Down Expand Up @@ -31,8 +31,10 @@ describe( 'matrix mean', function tests() {
for ( i = 0; i < data.length; i++ ) {
data[ i ] = i;
}
mat = matrix( data, [5,5], 'int8' );

beforeEach( function before() {
mat = matrix( data, [5,5], 'int8' );
});

it( 'should export a function', function test() {
expect( mean ).to.be.a( 'function' );
Expand All @@ -52,6 +54,15 @@ describe( 'matrix mean', function tests() {
expected = '2;7;12;17;22';

assert.strictEqual( mu.toString(), expected );

// Flip a matrix up-down:
mat.strides[ 0 ] *= -1;
mat.offset = mat.length + mat.strides[ 0 ];

mu = mean( out, mat );
expected = '22;17;12;7;2';

assert.strictEqual( mu.toString(), expected, 'flipud' );
});

it( 'should compute the arithmetic mean along matrix rows', function test() {
Expand All @@ -63,6 +74,15 @@ describe( 'matrix mean', function tests() {
expected = '10,11,12,13,14';

assert.strictEqual( mu.toString(), expected );

// Flip a matrix left-right:
mat.strides[ 1 ] *= -1;
mat.offset = mat.strides[ 0 ] - 1;

mu = mean( out, mat, 1 );
expected = '14,13,12,11,10';

assert.strictEqual( mu.toString(), expected, 'fliplr' );
});

it( 'should return null if provided a matrix having one or more zero dimensions', function test() {
Expand Down

0 comments on commit 9e670e1

Please sign in to comment.