time-to-botec

Benchmark sampling in different programming languages
Log | Files | Refs | README

ndarray.js (2789B)


      1 /**
      2 * @license Apache-2.0
      3 *
      4 * Copyright (c) 2020 The Stdlib Authors.
      5 *
      6 * Licensed under the Apache License, Version 2.0 (the "License");
      7 * you may not use this file except in compliance with the License.
      8 * You may obtain a copy of the License at
      9 *
     10 *    http://www.apache.org/licenses/LICENSE-2.0
     11 *
     12 * Unless required by applicable law or agreed to in writing, software
     13 * distributed under the License is distributed on an "AS IS" BASIS,
     14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     15 * See the License for the specific language governing permissions and
     16 * limitations under the License.
     17 */
     18 
     19 'use strict';
     20 
     21 // MODULES //
     22 
     23 var isnan = require( '@stdlib/math/base/assert/is-nan' );
     24 var abs = require( '@stdlib/math/base/special/abs' );
     25 
     26 
     27 // MAIN //
     28 
     29 /**
     30 * Computes the sum of strided array elements, ignoring `NaN` values and using an improved Kahan–Babuška algorithm.
     31 *
     32 * ## Method
     33 *
     34 * -   This implementation uses an "improved Kahan–Babuška algorithm", as described by Neumaier (1974).
     35 *
     36 * ## References
     37 *
     38 * -   Neumaier, Arnold. 1974. "Rounding Error Analysis of Some Methods for Summing Finite Sums." _Zeitschrift Für Angewandte Mathematik Und Mechanik_ 54 (1): 39–51. doi:[10.1002/zamm.19740540106](https://doi.org/10.1002/zamm.19740540106).
     39 *
     40 * @param {PositiveInteger} N - number of indexed elements
     41 * @param {NumericArray} x - input array
     42 * @param {integer} strideX - `x` stride length
     43 * @param {NonNegativeInteger} offsetX - `x` starting index
     44 * @param {NumericArray} out - output array
     45 * @param {integer} strideOut - `out` stride length
     46 * @param {NonNegativeInteger} offsetOut - `out` starting index
     47 * @returns {NumericArray} output array
     48 *
     49 * @example
     50 * var floor = require( '@stdlib/math/base/special/floor' );
     51 *
     52 * var x = [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0, NaN, NaN ];
     53 * var out = [ 0.0, 0 ];
     54 *
     55 * var N = floor( x.length / 2 );
     56 *
     57 * var v = gnannsumkbn( N, x, 2, 1, out, 1, 0 );
     58 * // returns [ 5.0, 4 ]
     59 */
     60 function gnannsumkbn( N, x, strideX, offsetX, out, strideOut, offsetOut ) {
     61 	var sum;
     62 	var ix;
     63 	var io;
     64 	var v;
     65 	var t;
     66 	var c;
     67 	var n;
     68 	var i;
     69 
     70 	ix = offsetX;
     71 	io = offsetOut;
     72 
     73 	sum = 0.0;
     74 	if ( N <= 0 ) {
     75 		out[ io ] = sum;
     76 		out[ io+strideOut ] = 0;
     77 		return out;
     78 	}
     79 	if ( N === 1 || strideX === 0 ) {
     80 		if ( isnan( x[ ix ] ) ) {
     81 			out[ io ] = sum;
     82 			out[ io+strideOut ] = 0;
     83 			return out;
     84 		}
     85 		out[ io ] = x[ ix ];
     86 		out[ io+strideOut ] = 1;
     87 		return out;
     88 	}
     89 	c = 0.0;
     90 	n = 0;
     91 	for ( i = 0; i < N; i++ ) {
     92 		v = x[ ix ];
     93 		if ( isnan( v ) === false ) {
     94 			t = sum + v;
     95 			if ( abs( sum ) >= abs( v ) ) {
     96 				c += (sum-t) + v;
     97 			} else {
     98 				c += (v-t) + sum;
     99 			}
    100 			sum = t;
    101 			n += 1;
    102 		}
    103 		ix += strideX;
    104 	}
    105 	out[ io ] = sum + c;
    106 	out[ io+strideOut ] = n;
    107 	return out;
    108 }
    109 
    110 
    111 // EXPORTS //
    112 
    113 module.exports = gnannsumkbn;