Evaluates the beta function.
The Beta function, also called the Euler integral, is defined as
It is related to the Gamma function via the following equation
$ npm install compute-beta
For use in the browser, use browserify.
var beta = require( 'compute-beta' );
Evaluates the Beta function (element-wise). x
may be either a number
, an array
, a typed array
, or a matrix
. y
has to be either an array
or matrix
of equal dimensions as x
or a single number. Correspondingly, the function returns either an array
with the same length as the input array(s)
, a matrix
with the same dimensions as the input matrix/matrices
or a single number.
var matrix = require( 'dstructs-matrix' ),
data,
mat,
out,
i;
out = beta( 1, 1 );
// returns ~1
out = beta( 0, 0 );
// returns +Infinity
out = beta( -1, 1 );
// returns NaN
out = beta( [ 5, 10, 15, 20, 25, 30 ], 0.2 );
// returns [ ~3.382, ~2.920, ~2.685, ~2.532, ~2.419, ~2.331 ]
data = new Int8Array( data );
out = beta( data );
// returns Float64Array( [~3.382,~2.920,~2.685,~2.532,~2.419,~2.331] )
data = new Float64Array( 6 );
for ( i = 0; i < 6; i++ ) {
data[ i ] = i * 5 + 5;
}
mat = matrix( data, [3,2], 'float64' );
/*
[ 5 10
15 20
25 30 ]
*/
out = beta( mat, 0.2 );
/*
[ ~3.382 ~2.920
~2.685 ~2.532
~2.419 ~2.331 ]
*/
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 = [
['beep', 5],
['boop', 10],
['bip', 15],
['bap', 20],
['baz', 25]
];
function getValue( d, i ) {
return d[ 1 ];
}
var out = beta( data, 0.2, {
'accessor': getValue
});
// returns [ ~3.382, ~2.920, ~2.685, ~2.532, ~2.419 ]
When evaluating the Beta function for values of two object arrays
, provide an accessor function
which accepts 3
arguments.
var data = [
['beep', 1],
['boop', 2],
['bip', 3],
['bap', 4],
['baz', 5]
];
var y = [
{'x': 1},
{'x': 2},
{'x': 3},
{'x': 4},
{'x': 5}
];
function getValue( d, i, j ) {
if ( j === 0 ) {
return d[ 1 ];
}
return d.x;
}
var out = beta( data, y, {
'accessor': getValue
});
// returns [ ~1, ~0.167, ~0.033, ~0.007, ~0.002 ]
Note: j
corresponds to the input array
index, where j=0
is the index for the first input array
and j=1
is the index for the second input array
.
To deepset an object array
, provide a key path and, optionally, a key path separator.
var data = [
{'x':[0,5]},
{'x':[1,10]},
{'x':[2,15]},
{'x':[3,20]},
{'x':[4,25]}
];
var out = beta( data, 0.2, 'x|1', '|' );
/*
[
{'x':[0,~3.382]},
{'x':[1,~2.920]},
{'x':[2,~2.685]},
{'x':[3,~2.532]},
{'x':[4,~2.419]}
]
*/
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 Int8Array( [10,20,30] );
out = beta( data, 0.01, {
'dtype': 'int32'
});
// returns Int32Array( [97,96,96] )
// Works for plain arrays, as well...
out = beta( [ 10, 20, 30 ], 0.01, {
'dtype': 'uint8'
});
// returns Uint8Array( [97,96,96] )
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;
var data = [ 5, 10, 15, 20, 25, 30 ];
var out = beta( data, 0.2, {
'copy': false
});
// returns [ ~3.382, ~2.920, ~2.685, ~2.532, ~2.419, ~2.331 ]
bool = ( data === out );
// returns true
data = new Float64Array( 6 );
for ( i = 0; i < 6; i++ ) {
data[ i ] = i * 5 + 5;
}
mat = matrix( data, [3,2], 'float64' );
/*
[ 5 10
15 20
25 30 ]
*/
out = beta( mat, {
'copy': false
});
/*
[ ~3.382 ~2.920
~2.685 ~2.532
~2.419 ~2.331 ]
*/
bool = ( mat === out );
// returns true
-
If an element is not a numeric value, the evaluated error function is
NaN
.var data, out; out = beta( null, 1 ); // returns NaN out = beta( true, 1 ); // returns NaN out = beta( {'a':'b'}, 1 ); // returns NaN out = beta( [ true, null, [] ], 1 ); // returns [ NaN, NaN, NaN ] function getValue( d, i ) { return d.x; } data = [ {'x':true}, {'x':[]}, {'x':{}}, {'x':null} ]; out = beta( data, 1, { 'accessor': getValue }); // returns [ NaN, NaN, NaN, NaN ] out = beta( data, 1, { 'path': 'x' }); /* [ {'x':NaN}, {'x':NaN}, {'x':NaN, {'x':NaN} ] */
-
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 = beta( [ true, null, [] ], 1, { 'dtype': 'int8' }); // returns Int8Array( [0,0,0] );
-
When calling the function with a numeric value as the first argument and a
matrix
orarray
as the second argument, only thedtype
option is applicable.// Valid: var out = beta( 2, [ 1, 2, 3 ], { 'dtype': 'int8' }); // returns Int8Array( [0,0,0] ) // Not valid: var out = beta( 2, [ 1, 2, 3 ], { 'copy': false }); // throws an error
var matrix = require( 'dstructs-matrix' ),
beta = require( 'compute-beta' );
var data,
mat,
out,
tmp,
i;
// ----
// Plain arrays...
data = new Array( 10 );
for ( i = 0; i < data.length; i++ ) {
data[ i ] = Math.random();
}
out = beta( data, 0.5 );
// Object arrays (accessors)...
function getValue( d ) {
return d.x;
}
for ( i = 0; i < data.length; i++ ) {
data[ i ] = {
'x': data[ i ]
};
}
out = beta( data, 0.5, {
'accessor': getValue
});
// Deep set arrays...
for ( i = 0; i < data.length; i++ ) {
data[ i ] = {
'x': [ i, data[ i ].x ]
};
}
out = beta( data, 0.5, {
'path': 'x/1',
'sep': '/'
});
// Typed arrays...
data = new Float32Array( 10 );
for ( i = 0; i < data.length; i++ ) {
data[ i ] = Math.random();
}
tmp = beta( data, 0.5 );
out = '';
for ( i = 0; i < data.length; i++ ) {
out += tmp[ i ];
if ( i < data.length-1 ) {
out += ',';
}
}
// Matrices...
mat = matrix( data, [5,2], 'float32' );
out = beta( mat, 0.5 );
// Matrices (custom output data type)...
out = beta( mat, 0.5, {
'dtype': 'uint8'
});
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.