Applies a function to each typed array element while broadcasting numeric arguments.
$ npm install compute-typed-array-number-function
For use in the browser, use browserify.
var arrayfun = require( 'compute-typed-array-number-function' );
Applies a function
to each typed array
element while broadcasting numeric
arguments. value
arguments may be either typed arrays
or number
primitives.
var arr = new Int8Array( [1,2,3,4,5] );
function add( x, y ) {
return x + y;
}
var out = arrayfun( add, arr, 5 );
// returns Float64Array( [6,7,8,9,10] )
The function accepts the following options
:
- dtype: output data type. Default:
float64
. - out:
boolean
indicating whether an outputtyped array
has been provided. Default:false
.
By default, the output typed array
data type is float64
in order to preserve precision. To specify a different data type, set the dtype
option (see compute-array-constructors
for a list of acceptable data types).
var out = arrayfun( add, arr, 5, {
'dtype': 'int8';
});
// return Int8Array( [6,7,8,9,10] )
By default, the function
returns a new typed array
. To mutate a typed array
(e.g., when input values can be discarded or when optimizing memory usage), set the out
option to true
to indicate that an output typed array
has been provided as the first typed array
argument.
var out = Uint8Array( 5 );
arrayfun( add, out, arr, 5, {
'out': 'true';
});
// returns Uint8Array( [6,7,8,9,10] )
// Works with generic arrays, as well...
out = [ 0, 0, 0, 0, 0 ];
arrayfun( add, out, arr, 5, {
'out': 'true';
});
// returns [ 6, 7, 8, 9, 10 ]
===
The main exported function
does not make any assumptions regarding the number of input typed arrays
or numbers
. To create a reusable typed array
function where argument types
are known, a factory method is provided.
<a name=
"arrayfun-factory">
Creates an apply function
to apply a function
to each typed array
element. The types
argument defines the input argument types (either 'array'
or 'number'
).
var afun = arrayfun.factory( ['array', 'number'] );
function add( x, y ) {
return x + y;
}
var arr = new Int16Array( 5 );
for ( var i = 0; i < 5; i++ ) {
arr[ i ] = i + 5;
}
// arr = Int16Array( [5,6,7,8,9] )
var out = afun( add, arr, 5 );
// returns Float64Array( [10,11,12,13,14] )
An apply function
may be provided during function
creation.
var aadd = arrayfun.factory( add, ['array', 'number'] );
var out = aadd( arr, 5 );
// returns Float64Array( [10,11,12,13,14] )
The function accepts the following options
:
- dtype: output data type. Default:
float64
.
By default, the output typed array
data type is float64
. To specify a different data type, set the dtype
option.
var aadd = arrayfun.factory( add, ['array', 'number'], {
'dtype': 'int32';
});
var out = aadd( arr, 5 );
// returns Int32Array( [10,11,12,13,14] )
// ...and for all subsequent calls...
out = aadd( arr, 5 );
// returns Int32Array( [10,11,12,13,14] )
Note: a factory function
always returns a new typed array
.
===
To facilitate using typed array
functions within an application where input arguments are of known types and where memory management occurs externally, a method to create minimal typed array
functions is provided.
Creates an apply function
to apply a function
to each typed array
element. The types
argument defines the input argument types (either 'array'
or 'number'
).
var afcn = arrayfun.create( ['number','array'] ),
out = new Array( 5 );
out = afcn( add, out, 5, arr );
// returns [ 10, 11, 12, 13, 14 ]
function subtract( x, y ) {
return x - y;
}
out = afcn( subtract, out, 5, arr );
// returns [ 0, -1, -2, -3, -4 ]
An apply function
may be provided during function
creation.
var aadd = arrayfun.create( add, ['array','number'] );
var out = aadd( out, arr, 5 );
// returns [ 10, 11, 12, 13, 14 ]
===
Lower-level APIs are provided which forgo some of the guarantees of the above APIs, such as input argument validation. While use of the above APIs is encouraged in REPL environments, use of the lower-level interfaces may be warranted when arguments are of a known type or when performance is paramount.
Applies a function
to each typed array
element.
var arr = new Float32Array( 5 );
var out = arrayfun.raw( add, arr, 5 );
// returns Float64Array( [10,11,12,13,14] )
The function accepts the same options
as the main exported function.
Creates an apply function
to apply a function
to each typed array
element.
var afun = arrayfun.rawFactory( ['number','array'] );
var out = afun( add, 5, arr );
// returns Float64Array( [10,11,12,13,14] )
The function accepts the same options
as arrayfun.factory()
.
- Both factory methods, as well as the
.create()
method, use dynamic code evaluation. Beware when using these methods in the browser as they may violate your content security policy (CSP).
var arrayfun = require( 'compute-typed-array-number-function' );
var arr1,
arr2,
out,
i;
arr1 = new Float32Array( 25 );
for ( i = 0; i < arr1.length; i++ ) {
arr1[ i ] = i;
}
arr2 = new Uint8Array( 25 );
for ( i = 0; i < arr2.length; i++ ) {
arr2[ i ] = 5;
}
function add( x, y, z ) {
return x + y + z;
}
out = arrayfun( add, arr1, 10, arr2 );
console.log( out );
To run the example code from the top-level application directory,
$ node ./examples/index.js
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.
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
Copyright © 2015. The Compute.io Authors.