Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* text=auto
14 changes: 14 additions & 0 deletions .jshintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

# Directories #
###############
build/
reports/
dist/

# Node.js #
###########
/node_modules/

# Git #
#######
.git*
2 changes: 1 addition & 1 deletion .jshintrc
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,4 @@
"wsh": false,
"yui": false,
"globals": {}
}
}
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,6 @@ Desktop.ini
# Utilities #
#############
.jshintrc
.jshintignore
.travis.yml
.editorconfig
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) 2014 Athan Reines.
Copyright (c) 2014-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.
34 changes: 27 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,42 @@
# 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 #

NOTES ?= 'TODO|FIXME'
NOTES ?= 'TODO|FIXME|WARNING|HACK|NOTE'


# MOCHA #

# Specify the test framework bin locations:
MOCHA ?= ./node_modules/.bin/mocha
_MOCHA ?= ./node_modules/.bin/_mocha

# Specify the mocha reporter:
MOCHA_REPORTER ?= spec


# ISTANBUL #

# Istanbul configuration:
ISTANBUL ?= ./node_modules/.bin/istanbul
ISTANBUL_OUT ?= ./reports/coverage
ISTANBUL_REPORT ?= lcov
ISTANBUL_LCOV_INFO_PATH ?= $(ISTANBUL_OUT)/lcov.info
ISTANBUL_HTML_REPORT_PATH ?= $(ISTANBUL_OUT)/lcov-report/index.html


# JSHINT #

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



# FILES #

Expand Down Expand Up @@ -96,9 +106,20 @@ test-istanbul-mocha: node_modules
view-cov: view-istanbul-report

view-istanbul-report:
open $(ISTANBUL_HTML_REPORT_PATH)
$(OPEN) $(ISTANBUL_HTML_REPORT_PATH)


# LINT #

.PHONY: lint lint-jshint

lint: lint-jshint

lint-jshint: node_modules
$(JSHINT) \
--reporter $(JSHINT_REPORTER) \
./


# NODE #

Expand All @@ -117,7 +138,6 @@ clean-node:


# CLEAN #

.PHONY: clean

clean:
Expand Down
153 changes: 135 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ argmax
===
[![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 maximum value of a numeric array and returns the corresponding array indices.
> Returns the element indices corresponding to the maximum value.


## Installation
Expand All @@ -16,36 +16,154 @@ For use in the browser, use [browserify](https://github.com/substack/node-browse

## Usage

To use the module,

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

#### argmax( arr )
#### argmax( x[, options] )

Returns the indices corresponding to the maximum value of input `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 data, idx;

data = [ 2, 4, 8, 3, 8, 2 ];
idx = argmax( data );
// returns [ 2, 4 ]

data = new Int8Array( data );
idx = argmax( data );
// returns [ 2, 4 ]
```

Computes the maximum value of a numeric `array` and returns the corresponding `array` indices.
For non-numeric `arrays`, provide an accessor `function` for accessing numeric values

``` javascript
var data = [ 3, 2, 5, 2, 5 ];
var arr = [
{'x':3},
{'x':2},
{'x':5},
{'x':4},
{'x':4},
{'x':5}
];

function getValue( d ) {
return d.x;
}

var idx = argmax( data );
// returns [2,4]
var val = argmax( arr, getValue );
// returns [ 2, 5 ]
```
__Note__: if provided an empty `array`, the function returns `null`.

If provided a [`matrix`](https://github.com/dstructs/matrix), the function accepts the following `option`:

* __dim__: dimension along which to compute the maximum. Default: `2` (along the columns).

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

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

data = new Int8Array( [ 1, 2, 3, 6, 5, 4, 7, 9, 8 ] );
mat = matrix( data, [3,3], 'int8' );
/*
[ 1 2 3
6 5 4
7 9 8 ]
*/

idx = argmax( mat );
/*
[ [ 2 ],
[ 0 ],
[ 1 ] ]
*/
```

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

``` javascript
idx = argmax( mat, {
'dim': 1
});
/*
[ [ 2 ],
[ 2 ],
[ 2 ] ]
*/
```

## Examples

``` javascript
var argmax = require( 'compute-argmax' );
var matrix = require( 'dstructs-matrix' ),
argmax = require( 'compute-argmax' ),
util = require( 'util' );

var data,
mat,
idx,
i;

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


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


// ----
// Typed arrays...
data = new Int32Array( 1000 );
for ( i = 0; i < data.length; i++ ) {
data[ i ] = Math.round( Math.random() * 100 );
}
idx = argmax( data );
console.log( 'Typed Arrays: \n' );
console.log( util.inspect( idx ) );

// ----
// Matrices (along rows)...
mat = matrix( data, [20,50], 'int32' );
idx = argmax( mat, {
'dim': 1
});
console.log( 'Matrix (rows): \n' );
console.log( util.inspect( idx ) );

// ----
// Matrices (along columns)...
idx = argmax( mat, {
'dim': 2
});
console.log( 'Matrix (columns): \n');
console.log( util.inspect( idx ) );

```

To run the example code from the top-level application directory,
Expand All @@ -59,7 +177,7 @@ $ node ./examples/index.js

### Unit

Unit tests use the [Mocha](http://visionmedia.github.io/mocha) test framework with [Chai](http://chaijs.com) assertions. To run the tests, execute the following command in the top-level application directory:
Unit tests use the [Mocha](http://mochajs.org) test framework with [Chai](http://chaijs.com) assertions. To run the tests, execute the following command in the top-level application directory:

``` bash
$ make test
Expand All @@ -83,16 +201,15 @@ $ make view-cov
```


---
## License

[MIT license](http://opensource.org/licenses/MIT).
[MIT license](http://opensource.org/licenses/MIT).


---
## Copyright

Copyright &copy; 2014. Athan Reines.

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

[npm-image]: http://img.shields.io/npm/v/compute-argmax.svg
[npm-url]: https://npmjs.org/package/compute-argmax
Expand Down
Loading