time-to-botec

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

ndarray.js (2613B)


      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 floor = require( '@stdlib/math/base/special/floor' );
     24 
     25 
     26 // VARIABLES //
     27 
     28 // Blocksize for pairwise summation:
     29 var BLOCKSIZE = 128;
     30 
     31 
     32 // MAIN //
     33 
     34 /**
     35 * Computes the cumulative sum of strided array elements using pairwise summation.
     36 *
     37 * ## Method
     38 *
     39 * -   This implementation uses pairwise summation, which accrues rounding error `O(log2 N)` instead of `O(N)`. The recursion depth is also `O(log2 N)`.
     40 *
     41 * ## References
     42 *
     43 * -   Higham, Nicholas J. 1993. "The Accuracy of Floating Point Summation." _SIAM Journal on Scientific Computing_ 14 (4): 783–99. doi:[10.1137/0914050](https://doi.org/10.1137/0914050).
     44 *
     45 * @param {PositiveInteger} N - number of indexed elements
     46 * @param {number} sum - initial sum
     47 * @param {NumericArray} x - input array
     48 * @param {integer} strideX - `x` stride length
     49 * @param {NonNegativeInteger} offsetX - starting index for `x`
     50 * @param {NumericArray} y - output array
     51 * @param {integer} strideY - `y` stride length
     52 * @param {NonNegativeInteger} offsetY - starting index for `y`
     53 * @returns {NumericArray} output array
     54 *
     55 * @example
     56 * var floor = require( '@stdlib/math/base/special/floor' );
     57 *
     58 * var x = [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ];
     59 * var y = [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ];
     60 * var N = floor( x.length / 2 );
     61 *
     62 * gcusumpw( N, 0.0, x, 2, 1, y, 1, 0 );
     63 * // y => [ 1.0, -1.0, 1.0, 5.0, 0.0, 0.0, 0.0, 0.0 ]
     64 */
     65 function gcusumpw( N, sum, x, strideX, offsetX, y, strideY, offsetY ) {
     66 	var ix;
     67 	var iy;
     68 	var s;
     69 	var n;
     70 	var i;
     71 
     72 	if ( N <= 0 ) {
     73 		return y;
     74 	}
     75 	ix = offsetX;
     76 	iy = offsetY;
     77 	if ( N <= BLOCKSIZE ) {
     78 		s = 0.0;
     79 		for ( i = 0; i < N; i++ ) {
     80 			s += x[ ix ];
     81 			y[ iy ] = sum + s;
     82 			ix += strideX;
     83 			iy += strideY;
     84 		}
     85 		return y;
     86 	}
     87 	n = floor( N/2 );
     88 	gcusumpw( n, sum, x, strideX, ix, y, strideY, iy );
     89 	iy += (n-1) * strideY;
     90 	gcusumpw( N-n, y[ iy ], x, strideX, ix+(n*strideX), y, strideY, iy+strideY ); // eslint-disable-line max-len
     91 	return y;
     92 }
     93 
     94 
     95 // EXPORTS //
     96 
     97 module.exports = gcusumpw;