betaln
Evaluates the natural logarithm of the Beta function.
This function evaluates the natural logarithm of the Beta function which can be defined as follows:
Installation
$ npm install compute-betaln
For use in the browser, use browserify.
Usage
var betaln = ;
betaln( x, y[, options] )
Evaluates the natural logarithm of 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 =datamatouti;out = ;// returns +Infinityout = ;// returns ~7.601out = ;// return NaNout = ;// returns [ ~0, ~-0.693, ~-1.099, ~-1.386 ]out = ;// returns [ ~0, ~-0.693, ~-1.099, ~-1.386 ]out = ;// returns [ -1, -0.8427, 0, 0.8427, 1 ]data = 0 05 1 15 2 ;out = ;// returns [ +Infintiy, ~-1.729, ~-4.605, ~-7.032, ~-9.22 ]data = data ;out = ;// returns Float64Array( [ +Infintiy, +Infinity, ~-4.605, ~-4.605, ~-9.22 ] )data = 6 ;for i = 0; i < 6; i++data i = i / 2;mat = ;/*[ 0 0.51 1.52 2.5 ]*/out = ;/*[ +Inf ~1.145~0.693 ~0.452~0.288 ~0.164 ]*/
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' 0'boop' 05'bip' 1'bap' 15'baz' 2;{return d 1 ;}var out =;// returns [ +Infintiy, ~-1.729, ~-4.605, ~-7.032, ~-9.22 ]
When evaluating the betaln
function for values of two object arrays
, provide an accessor function
which accepts 3
arguments.
var data ='beep' 2'boop' 3'bip' 4'bap' 5'baz' 6;var arr ='x': 2'x': 3'x': 4'x': 5'x': 6;{if j === 0return d 1 ;return dx;}var out =;// returns [ ~-1.792, ~-3.402, ~-4.942, ~-6.446, ~-7.927 ]
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':010'x':1100'x':21000'x':310000'x':4100000;var out = ;/*[{'x':[0,~2.0.27]},{'x':[1,~1.793]},{'x':[2,~1.562]},{'x':[3,~1.332]},{'x':[4,~1.101]}]*/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 = 1234 ;out =;// returns Int32Array( [-2,-4,-5,-7] )// Works for plain arrays, as well...out =;// returns Int8Array( [-2,-4,-5,-7] )
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 databoolmatouti;var data = 1 2 3 4 ;var out =;// returns [ ~0, ~-0.693, ~-1.099, ~-1.386 ]bool = data === out ;// returns truedata = 6 ;for i = 0; i < 6; i++data i = i / 2;mat = ;/*[ 0 0.51 1.52 2.5 ]*/out =;/*[ +Inf ~1.145~0.693 ~0.452~0.288 ~0.164 ]*/bool = mat === out ;// returns true
Notes
-
If an element is not a numeric value, the evaluated error function is
NaN
.var data out;out = ;// returns NaNout = ;// returns NaNout = ;// returns NaNout = ;// returns [ NaN, NaN, NaN ]{return dx;}data ='x':true'x':'x':{}'x':null;out =;// returns [ NaN, NaN, NaN, NaN ]out =;/*[{'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 =;// returns Int8Array( [0,0,0] );
Examples
var matrix =betaln = ;var datamatouttmpi;// Plain arrays...data = 10 ;for i = 0; i < datalength; i++data i = Math;out = ;// Object arrays (accessors)...{return dx;}for i = 0; i < datalength; i++data i ='x': data i;out =;// Deep set arrays...for i = 0; i < datalength; i++data i ='x': i data i x;out =;// Typed arrays...data = 10 ;for i = 0; i < datalength; i++data i = Math;tmp = ;out = '';for i = 0; i < datalength; i++out += tmp i ;if i < datalength-1out += ',';// Matrices...mat = ;out = ;// Matrices (custom output data type)...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
Copyright
Copyright © 2015. The Compute.io Authors.