time-to-botec

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

ndarray.js (2733B)


      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 double-precision floating-point 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 {Float64Array} x - input array
     48 * @param {integer} strideX - `x` stride length
     49 * @param {NonNegativeInteger} offsetX - starting index for `x`
     50 * @param {Float64Array} y - output array
     51 * @param {integer} strideY - `y` stride length
     52 * @param {NonNegativeInteger} offsetY - starting index for `y`
     53 * @returns {Float64Array} output array
     54 *
     55 * @example
     56 * var Float64Array = require( '@stdlib/array/float64' );
     57 * var floor = require( '@stdlib/math/base/special/floor' );
     58 *
     59 * var x = new Float64Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ] );
     60 * var y = new Float64Array( x.length );
     61 * var N = floor( x.length / 2 );
     62 *
     63 * var v = dcusumpw( N, 0.0, x, 2, 1, y, 1, 0 );
     64 * // returns <Float64Array>[ 1.0, -1.0, 1.0, 5.0, 0.0, 0.0, 0.0, 0.0 ]
     65 */
     66 function dcusumpw( N, sum, x, strideX, offsetX, y, strideY, offsetY ) {
     67 	var ix;
     68 	var iy;
     69 	var s;
     70 	var n;
     71 	var i;
     72 
     73 	if ( N <= 0 ) {
     74 		return y;
     75 	}
     76 	ix = offsetX;
     77 	iy = offsetY;
     78 	if ( N <= BLOCKSIZE ) {
     79 		s = 0.0;
     80 		for ( i = 0; i < N; i++ ) {
     81 			s += x[ ix ];
     82 			y[ iy ] = sum + s;
     83 			ix += strideX;
     84 			iy += strideY;
     85 		}
     86 		return y;
     87 	}
     88 	n = floor( N/2 );
     89 	dcusumpw( n, sum, x, strideX, ix, y, strideY, iy );
     90 	iy += (n-1) * strideY;
     91 	dcusumpw( N-n, y[ iy ], x, strideX, ix+(n*strideX), y, strideY, iy+strideY ); // eslint-disable-line max-len
     92 	return y;
     93 }
     94 
     95 
     96 // EXPORTS //
     97 
     98 module.exports = dcusumpw;