compute-typed-array-function

1.0.3 • Public • Published

Typed Array Function

NPM version Build Status Coverage Status Dependencies

Applies a function to each typed array element.

Installation

$ npm install compute-typed-array-function

For use in the browser, use browserify.

Usage

var arrayfun = require( 'compute-typed-array-function' );

arrayfun( fcn, ...array[, options] )

Applies a function to each typed array element.

var arr = new Int8Array( [1,2,3,4,5] );
 
function add5( val ) {
    return val + 5;
}
 
var out = arrayfun( add5, arr );
// returns Float64Array( [6,7,8,9,10] )

The function accepts the following options:

  • dtype: output data type. Default: float64.
  • out: boolean indicating whether an output typed 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( add5, arr, {
    '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( add5, out, arr, {
    'out': 'true';
});
// returns Uint8Array( [6,7,8,9,10] )
 
// Works with generic arrays, as well...
out = [ 0, 0, 0, 0, 0 ];
 
arrayfun( add5, out, arr, {
    'out': 'true';
});
// returns [ 6, 7, 8, 9, 10 ]

===

Factory

The main exported function does not make any assumptions regarding the number of input typed arrays. To create a reusable typed array function where the number of input typed arrays is known, a factory method is provided.

arrayfun.factory( [fcn,] num[, options] )

Creates an apply function to apply a function to each typed array element.

var afun = arrayfun.factory( 2 );
 
function add( x, y ) {
    return x + y;
}
 
var arr1 = new Int16Array( 5 ),
    arr2 = new Uint32Array( 5 );
 
for ( var i = 0; i < 5; i++ ) {
    arr1[ i ] = 5;
    arr2[ i ] = i + 5;
}
// arr1 = Int16Array( [5,5,5,5,5] )
// arr2 = Uint32Array( [5,6,7,8,9] )
 
var out = afun( add, arr1, arr2 );
// returns Float64Array( [10,11,12,13,14] )

An apply function may be provided during function creation.

var aadd = arrayfun.factory( add, 2 );
 
var out = aadd( arr1, arr2 );
// 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, 2, {
    'dtype': 'int32';
});
 
var out = aadd( arr1, arr2 );
// returns Int32Array( [10,11,12,13,14] )
 
// ...and for all subsequent calls...
out = aadd( arr1, arr2 );
// returns Int32Array( [10,11,12,13,14] )

Note: a factory function always returns a new typed array.

===

Create

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.

arrayfun.create( [fcn,] num )

Creates an apply function to apply a function to each typed array element, where num is the number of input typed arrays excluding the output typed array.

var afcn = arrayfun.create( 2 ),
    out = new Array( 5 );
 
out = afcn( add, out, arr1, arr2 );
// returns [ 10, 11, 12, 13, 14 ]
 
function subtract( x, y ) {
    return x - y;
}
 
out = afcn( subtract, out, arr1, arr2 );
// returns [ 0, -1, -2, -3, -4 ]

An apply function may be provided during function creation.

var aadd = arrayfun.create( add, 2 );
 
var out = aadd( out, arr1, arr2 );
// returns [ 10, 11, 12, 13, 14 ]

===

Raw

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.

arrayfun.raw( fcn, ...array[, options] )

Applies a function to each typed array element.

var arr = new Float32Array( 5 );
 
var out = arrayfun.raw( add5, arr );
// returns Float64Array( [5,5,5,5,5] )

The function accepts the same options as the main exported function.

arrayfun.rawFactory( [fcn,] num[, options] )

Creates an apply function to apply a function to each typed array element.

var afun = arrayfun.rawFactory( 2 );
 
var out = afun( add, arr1, arr2 );
// returns Float64Array( [10,11,12,13,14] )

The function accepts the same options as arrayfun.factory().

Notes

  • 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).

Examples

var arrayfun = require( 'compute-typed-array-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 ) {
    return x + y;
}
 
out = arrayfun( add, arr1, arr2 );
console.log( out );

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

MIT license.

Copyright

Copyright © 2015. The Compute.io Authors.

Package Sidebar

Install

npm i compute-typed-array-function

Weekly Downloads

1

Version

1.0.3

License

MIT

Last publish

Collaborators

  • kgryte