Absolute Value
!NPM versionnpm-imagenpm-url !Build Statustravis-imagetravis-url !Coverage Statuscoveralls-imagecoveralls-url !Dependenciesdependencies-imagedependencies-urlComputes an element-wise absolute value.
The absolute value is defined as
|x| = \begin{cases} x & \textrm{if}\ x \geq 0 \\ -x & \textrm{if}\ x < 0\end{cases}" data-equation="eq:absolute_value">
<img src="https://cdn.rawgit.com/compute-io/abs/35f3f35f5a7ea712dc1e5bb5a4e8a9909ce76507/docs/img/eqn.svg" alt="Absolute value definition.">
<br>
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 the `function` should return a new data structure. Default: `true`.
- __path__: [deepget](https://github.com/kgryte/utils-deep-get)/[deepset](https://github.com/kgryte/utils-deep-set) key path.
- __sep__: [deepget](https://github.com/kgryte/utils-deep-get)/[deepset](https://github.com/kgryte/utils-deep-set) 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
.
``` javascript
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
.
``` javascript
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