compute-erfc
Version:
Complementary error function.
118 lines (105 loc) • 3.39 kB
JavaScript
;
// MODULES //
var isNumber = require( 'validate.io-number-primitive' ),
isnan = require( 'validate.io-nan' ),
isArrayLike = require( 'validate.io-array-like' ),
isTypedArrayLike = require( 'validate.io-typed-array-like' ),
isMatrixLike = require( 'validate.io-matrix-like' ),
ctors = require( 'compute-array-constructors' ),
matrix = require( 'dstructs-matrix' ),
validate = require( './validate.js' );
// FUNCTIONS //
var erfc1 = require( './number.js' ),
erfc2 = require( './array.js' ),
erfc3 = require( './accessor.js' ),
erfc4 = require( './deepset.js' ),
erfc5 = require( './matrix.js' ),
erfc6 = require( './typedarray.js' );
// COMPLEMENTARY ERROR FUNCTION //
/**
* FUNCTION: erfc( x[, opts] )
* Computes the complementary error function.
*
* @param {Number|Number[]|Array|Int8Array|Uint8Array|Uint8ClampedArray|Int16Array|Uint16Array|Int32Array|Uint32Array|Float32Array|Float64Array|Matrix} x - input value
* @param {Object} [opts] - function options
* @param {Boolean} [opts.copy=true] - boolean indicating if the function should return a new data structure
* @param {Function} [opts.accessor] - accessor function for accessing array values
* @param {String} [opts.path] - deep get/set key path
* @param {String} [opts.sep="."] - deep get/set key path separator
* @param {String} [opts.dtype="float64"] - output data type
* @returns {Number|Number[]|Array|Int8Array|Uint8Array|Uint8ClampedArray|Int16Array|Uint16Array|Int32Array|Uint32Array|Float32Array|Float64Array|Matrix} complementary error function value(s)
*/
function erfc( x, options ) {
/* jshint newcap:false */
var opts = {},
ctor,
err,
out,
dt,
d;
if ( isNumber( x ) || isnan( x ) ) {
return erfc1( x );
}
if ( arguments.length > 1 ) {
err = validate( opts, options );
if ( err ) {
throw err;
}
}
if ( isMatrixLike( x ) ) {
if ( opts.copy !== false ) {
dt = opts.dtype || 'float64';
ctor = ctors( dt );
if ( ctor === null ) {
throw new Error( 'erfc()::invalid option. Data type option does not have a corresponding array constructor. Option: `' + dt + '`.' );
}
// Create an output matrix:
d = new ctor( x.length );
out = matrix( d, x.shape, dt );
} else {
out = x;
}
return erfc5( out, x );
}
if ( isTypedArrayLike( x ) ) {
if ( opts.copy === false ) {
out = x;
} else {
dt = opts.dtype || 'float64';
ctor = ctors( dt );
if ( ctor === null ) {
throw new Error( 'erfc()::invalid option. Data type option does not have a corresponding array constructor. Option: `' + dt + '`.' );
}
out = new ctor( x.length );
}
return erfc6( out, x );
}
if ( isArrayLike( x ) ) {
// Handle deepset first...
if ( opts.path ) {
opts.sep = opts.sep || '.';
return erfc4( x, opts.path, opts.sep );
}
// Handle regular and accessor arrays next...
if ( opts.copy === false ) {
out = x;
}
else if ( opts.dtype ) {
ctor = ctors( opts.dtype );
if ( ctor === null ) {
throw new TypeError( 'erfc()::invalid option. Data type option does not have a corresponding array constructor. Option: `' + opts.dtype + '`.' );
}
out = new ctor( x.length );
}
else {
out = new Array( x.length );
}
if ( opts.accessor ) {
return erfc3( out, x, opts.accessor );
}
return erfc2( out, x );
}
return NaN;
} // end FUNCTION erfc()
// EXPORTS //
module.exports = erfc;