Matrix Function
Applies a function to each matrix element.
Installation
$ npm install compute-matrix-function
For use in the browser, use browserify.
Usage
var matrixfun = ;
matrixfun( fcn, ...matrix[, options] )
Applies a function
to each matrix
element.
var matrix = ;var mat = ;/*[ 0 0 0 0 00 0 0 0 00 0 0 0 00 0 0 0 00 0 0 0 0 ]*/{return val + 5;}var out = ;/*[ 5 5 5 5 55 5 5 5 55 5 5 5 55 5 5 5 55 5 5 5 5 ]*/
The function accepts the following options
:
- dtype: output data type. Default:
float64
. - out:
boolean
indicating whether an outputmatrix
has been provided. Default:false
.
By default, the output matrix
data type 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 out =;/*[ 5 5 5 5 55 5 5 5 55 5 5 5 55 5 5 5 55 5 5 5 5 ]*/var dtype = outdtype;// returns 'int8'
By default, the function
returns a new matrix
. To mutate a matrix
(e.g., when input values can be discarded or when optimizing memory usage), set the out
option to true
to indicate that an output matrix
has been provided as the first matrix
argument.
var out = ;/*[ 0 0 0 0 00 0 0 0 00 0 0 0 00 0 0 0 00 0 0 0 0 ]*/;/*[ 5 5 5 5 55 5 5 5 5out = 5 5 5 5 55 5 5 5 55 5 5 5 5 ]*/
===
Factory
The main exported function
does not make any assumptions regarding the number of input matrices
. To create a reusable matrix
function where the number of input matrices
is known, a factory method is provided.
matrixfun.factory( [fcn,] num[, options] )
Creates an apply function
to apply a function
to each matrix
element.
var mfun = matrixfun;{return x + y;}var mat1 =mat2 = ;for var i = 0; i < 5; i++for var j = 0; j < 5; j++mat1;mat2;/*[ 5 5 5 5 55 5 5 5 5mat1 = 5 5 5 5 55 5 5 5 55 5 5 5 5 ][ 0 1 2 3 45 6 7 8 9mat2 = 10 11 12 13 1415 16 17 18 1920 21 22 23 24 ]*/var out = ;/*[ 5 6 7 8 910 11 12 13 1415 16 17 18 1920 21 22 23 2425 26 27 28 29 ]*/
An apply function
may be provided during function
creation.
var madd = matrixfun;var out = ;/*[ 5 6 7 8 910 11 12 13 1415 16 17 18 1920 21 22 23 2425 26 27 28 29 ]*/
The function accepts the following options
:
- dtype: output data type. Default:
float64
.
By default, the output matrix
data type is float64
. To specify a different data type, set the dtype
option.
var madd = matrixfun;var out = ;/*[ 5 6 7 8 910 11 12 13 1415 16 17 18 1920 21 22 23 2425 26 27 28 29 ]*/var dtype = outdtype;// returns 'int32'// ...and for all subsequent calls...out = ;dtype = outdtype;// returns 'int32'
Note: a factory function
always returns a new matrix
.
===
Create
To facilitate using matrix
functions within an application where input arguments are of known types and where memory management occurs externally, a method to create minimal matrix
functions is provided.
matrixfun.create( [fcn,] num )
Creates an apply function
to apply a function
to each matrix
element, where num
is the number of input matrices
excluding the output matrix
.
var mfcn = matrixfun;var out = ;/*[ 5 6 7 8 910 11 12 13 1415 16 17 18 1920 21 22 23 2425 26 27 28 29 ]*/{return x - y;}out = ;/*[ -5 -4 -3 -2 -10 1 2 3 45 6 7 8 910 11 12 13 1415 16 17 18 19 ]*/
An apply function
may be provided during function
creation.
var madd = matrixfun;var out = ;/*[ 5 6 7 8 910 11 12 13 1415 16 17 18 1920 21 22 23 2425 26 27 28 29 ]*/
===
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.
matrixfun.raw( fcn, ...matrix[, options] )
Applies a function
to each matrix
element.
var mat = ;/*[ 0 0 0 0 00 0 0 0 00 0 0 0 00 0 0 0 00 0 0 0 0 ]*/var out = matrixfun;/*[ 5 5 5 5 55 5 5 5 55 5 5 5 55 5 5 5 55 5 5 5 5 ]*/
The function accepts the same options
as the main exported function.
matrixfun.rawFactory( [fcn,] num[, options] )
Creates an apply function
to apply a function
to each matrix
element.
var mfun = matrixfun;var out = ;/*[ 5 6 7 8 910 11 12 13 1415 16 17 18 1920 21 22 23 2425 26 27 28 29 ]*/
The function accepts the same options
as matrixfun.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 matrix =matrixfun = ;var mat1mat2outd i;d = 25 ;for i = 0; i < dlength; i++d i = i;mat1 = ;/*[ 0 1 2 3 45 6 7 8 910 11 12 13 1415 16 17 18 1920 21 22 23 24 ]*/d = 25 ;for i = 0; i < dlength; i++d i = 5;mat2 = ;/*[ 5 5 5 5 55 5 5 5 55 5 5 5 55 5 5 5 55 5 5 5 5 ]*/{return x + y;}out = ;/*[ 5 6 7 8 910 11 12 13 1415 16 17 18 1920 21 22 23 2425 26 27 28 29 ]*/console;
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 © 2015. The Compute.io Authors.