compute-abs
v3.0.0
Published
Computes an element-wise absolute value.
Downloads
955
Readme
Absolute Value
Computes an element-wise absolute value.
The absolute value is defined as
Installation
$ npm install compute-abs
For use in the browser, use browserify.
Usage
var abs = require( 'compute-abs' );
abs( x[, opts] )
Computes an element-wise absolute value. x
may be either a number
, an array
, a typed array
, or a matrix
.
var matrix = require( 'dstructs-matrix' ),
data,
mat,
out,
i;
out = abs( 3 );
// returns 3
out = abs( -3 );
// returns 3
data = [ -2, 1, -3 ];
out = abs( data );
// returns [ 2, 1, 3 ]
data = new Int8Array( data );
out = abs( data );
// returns Float64Array( [2, 1, 3] )
data = new Int16Array( 6 );
for ( i = 0; i < 6; i++ ) {
data[ i ] = i - 3;
}
mat = matrix( data, [3,2], 'int16' );
/*
[ -3 -2
-1 0
1 2 ]
*/
out = abs( mat );
/*
[ 3 2
1 0
1 2 ]
*/
The function accepts the following options
:
- accessor: accessor
function
for accessingarray
values. - dtype: output
typed array
ormatrix
data type. Default:float64
. - copy:
boolean
indicating if thefunction
should return a new data structure. Default:true
. - path: deepget/deepset key path.
- sep: deepget/deepset key path separator. Default:
'.'
.
For non-numeric arrays
, provide an accessor function
for accessing array
values.
var data = [
[0,4],
[1,-9],
[2,16],
[3,-25],
[4,36]
];
function getValue( d, i ) {
return d[ 1 ];
}
var out = abs( data, {
'accessor': getValue
});
// returns [ 4, 9, 16, 25, 36 ]
To deepset an object array
, provide a key path and, optionally, a key path separator.
var data = [
{'x':[0,4]},
{'x':[1,-9]},
{'x':[2,16]},
{'x':[3,-25]},
{'x':[4,36]}
];
var out = abs( data, {
'path': 'x|1',
'sep': '|'
});
/*
[
{'x':[0,4]},
{'x':[1,9]},
{'x':[2,16]},
{'x':[3,25]},
{'x':[4,36]}
]
*/
var bool = ( data === out );
// returns true
By default, when provided a typed array
or matrix
, the output data structure is float64
in order to preserve precision. To specify a different data type, set the dtype
option (see matrix
for a list of acceptable data types).
var data, out;
data = new Float32Array( [ -0.1, 0.1, 10, -10] );
out = abs( data, {
'dtype': 'int32'
});
// returns Int32Array( [0, 0, 10, 10] )
// Works for plain arrays, as well...
out = abs( [ -0.1, 0.1, 10, -10], {
'dtype': 'uint8'
});
// returns Uint8Array( [0, 0, 10, 10] )
By default, the function returns a new data structure. To mutate the input data structure (e.g., when input values can be discarded or when optimizing memory usage), set the copy
option to false
.
var data,
bool,
mat,
out,
i;
data = [ -4, 9, -16 ];
out = abs( data, {
'copy': false
});
// returns [ 4, 9, 16 ]
bool = ( data === out );
// returns true
data = new Int16Array( 6 );
for ( i = 0; i < 6; i++ ) {
data[ i ] = i - 3;
}
mat = matrix( data, [3,2], 'int16' );
/*
[ -3 -2
-1 0
1 2 ]
*/
out = abs( mat, {
'copy': false
});
/*
[ 3 2
1 0
1 2 ]
*/
bool = ( mat === out );
// returns true
Notes
If an element is not a numeric value, the element's absolute value is
NaN
.var data, out; out = abs( null ); // returns NaN out = abs( true ); // returns NaN out = abs( {'a':'b'} ); // returns NaN out = abs( [ -1, null, -2 ] ); // returns [ 1, NaN, 2 ] function getValue( d, i ) { return d.x; } data = [ {'x':4}, {'x':-9}, {'x':16}, {'x':null}, {'x':-25}, {'x':36} ]; out = abs( data, { 'accessor': getValue }); // returns [ 4, 9, 16, NaN, 25, 36 ] out = abs( data, { 'path': 'x' }); /* [ {'x':4}, {'x':9}, {'x':16}, {'x':NaN}, {'x':25}, {'x':36} ] */
Be careful when providing a data structure which contains non-numeric elements and specifying an
integer
output data type, asNaN
values are cast to0
.var out = abs( [ -1, null, -2 ], { 'dtype': 'int8' }); // returns Int8Array( [1, 0, 2] );
Examples
var matrix = require( 'dstructs-matrix' ),
abs = require( 'compute-abs' );
var data,
mat,
out,
tmp,
i;
// Plain arrays...
data = new Array( 10 );
for ( i = 0; i < data.length; i++ ) {
data[ i ] = Math.random()*20 - 100;
}
out = abs( data );
// Object arrays (accessors)...
function getValue( d ) {
return d.x;
}
for ( i = 0; i < data.length; i++ ) {
data[ i ] = {
'x': data[ i ]
};
}
out = abs( data, {
'accessor': getValue
});
// Deep set arrays...
for ( i = 0; i < data.length; i++ ) {
data[ i ] = {
'x': [ i, data[ i ].x ]
};
}
out = abs( data, {
'path': 'x/1',
'sep': '/'
});
// Typed arrays...
data = new Int32Array( 10 );
for ( i = 0; i < data.length; i++ ) {
data[ i ] = Math.random()*20 - 100;
}
tmp = abs( data );
out = '';
for ( i = 0; i < data.length; i++ ) {
out += tmp[ i ];
if ( i < data.length-1 ) {
out += ',';
}
}
// Matrices...
mat = matrix( data, [5,2], 'int32' );
out = abs( mat );
// Matrices (custom output data type)...
out = abs( mat, {
'dtype': 'uint8'
});
To run the example code from the top-level application directory,
$ node ./examples/index.js
Tests
Unit
Unit tests use the Mocha test framework with Chai assertions. To run the tests, execute the following command in the top-level application directory:
$ make test
All new feature development should have corresponding unit tests to validate correct functionality.
Test Coverage
This repository uses Istanbul as its code coverage tool. To generate a test coverage report, execute the following command in the top-level application directory:
$ make test-cov
Istanbul creates a ./reports/coverage
directory. To access an HTML version of the report,
$ make view-cov
License
Copyright
Copyright © 2014-2015. The Compute.io Authors.