time-to-botec

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

ndarray.js (2859B)


      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 float64ToFloat32 = require( '@stdlib/number/float64/base/to-float32' );
     24 var abs = require( '@stdlib/math/base/special/abs' );
     25 
     26 
     27 // MAIN //
     28 
     29 /**
     30 * Computes the cumulative sum of single-precision floating-point strided array elements 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 {number} sum - initial sum
     42 * @param {Float32Array} x - input array
     43 * @param {integer} strideX - `x` stride length
     44 * @param {NonNegativeInteger} offsetX - starting index for `x`
     45 * @param {Float32Array} y - output array
     46 * @param {integer} strideY - `y` stride length
     47 * @param {NonNegativeInteger} offsetY - starting index for `y`
     48 * @returns {Float32Array} output array
     49 *
     50 * @example
     51 * var Float32Array = require( '@stdlib/array/float32' );
     52 * var floor = require( '@stdlib/math/base/special/floor' );
     53 *
     54 * var x = new Float32Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ] );
     55 * var y = new Float32Array( x.length );
     56 * var N = floor( x.length / 2 );
     57 *
     58 * var v = scusumkbn( N, 0.0, x, 2, 1, y, 1, 0 );
     59 * // returns <Float32Array>[ 1.0, -1.0, 1.0, 5.0, 0.0, 0.0, 0.0, 0.0 ]
     60 */
     61 function scusumkbn( N, sum, x, strideX, offsetX, y, strideY, offsetY ) {
     62 	var ix;
     63 	var iy;
     64 	var s;
     65 	var v;
     66 	var t;
     67 	var c;
     68 	var i;
     69 
     70 	if ( N <= 0 ) {
     71 		return y;
     72 	}
     73 	ix = offsetX;
     74 	iy = offsetY;
     75 	s = sum;
     76 	c = 0.0;
     77 	for ( i = 0; i < N; i++ ) {
     78 		v = x[ ix ];
     79 		t = float64ToFloat32( s + v );
     80 		if ( abs( s ) >= abs( v ) ) {
     81 			c = float64ToFloat32( c + float64ToFloat32( float64ToFloat32( s-t ) + v ) ); // eslint-disable-line max-len
     82 		} else {
     83 			c = float64ToFloat32( c + float64ToFloat32( float64ToFloat32( v-t ) + s ) ); // eslint-disable-line max-len
     84 		}
     85 		s = t;
     86 		y[ iy ] = float64ToFloat32( s + c );
     87 		ix += strideX;
     88 		iy += strideY;
     89 	}
     90 	return y;
     91 }
     92 
     93 
     94 // EXPORTS //
     95 
     96 module.exports = scusumkbn;