Compute the sum of a one-dimensional single-precision floating-point ndarray, ignoring
NaNvalues and using pairwise summation.
var snansumpw = require( '@stdlib/blas/ext/base/ndarray/snansumpw' );Computes the sum of a one-dimensional single-precision floating-point ndarray, ignoring NaN values and using pairwise summation.
var Float32Array = require( '@stdlib/array/float32' );
var ndarray = require( '@stdlib/ndarray/base/ctor' );
var xbuf = new Float32Array( [ 1.0, -2.0, NaN, 2.0 ] );
var x = new ndarray( 'float32', xbuf, [ 4 ], [ 1 ], 0, 'row-major' );
var v = snansumpw( [ x ] );
// returns 1.0The function has the following parameters:
- arrays: array-like object containing a one-dimensional input ndarray.
- If provided an empty one-dimensional ndarray, the function returns
0.0. - In general, pairwise summation is more numerically stable than ordinary recursive summation (i.e., "simple" summation), with slightly worse performance. While not the most numerically stable summation technique (e.g., compensated summation techniques such as the Kahan–Babuška-Neumaier algorithm are generally more numerically stable), pairwise summation strikes a reasonable balance between numerical stability and performance. If either numerical stability or performance is more desirable for your use case, consider alternative summation techniques.
var bernoulli = require( '@stdlib/random/base/bernoulli' );
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' );
var filledarrayBy = require( '@stdlib/array/filled-by' );
var ndarray = require( '@stdlib/ndarray/base/ctor' );
var ndarray2array = require( '@stdlib/ndarray/to-array' );
var snansumpw = require( '@stdlib/blas/ext/base/ndarray/snansumpw' );
function clbk() {
if ( bernoulli( 0.7 ) > 0 ) {
return discreteUniform( 0, 100 );
}
return NaN;
}
var xbuf = filledarrayBy( 10, 'float32', clbk );
var x = new ndarray( 'float32', xbuf, [ xbuf.length ], [ 1 ], 0, 'row-major' );
console.log( ndarray2array( x ) );
var v = snansumpw( [ x ] );
console.log( v );- Higham, Nicholas J. 1993. "The Accuracy of Floating Point Summation." SIAM Journal on Scientific Computing 14 (4): 783–99. doi:10.1137/0914050.