|
| 1 | +/** |
| 2 | +* @license Apache-2.0 |
| 3 | +* |
| 4 | +* Copyright (c) 2025 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 | +* @private |
| 46 | +* @param {PositiveInteger} N - number of indexed elements |
| 47 | +* @param {number} sum - initial sum |
| 48 | +* @param {Object} x - input array object |
| 49 | +* @param {Collection} x.data - input array data |
| 50 | +* @param {Array<Function>} x.accessors - array element accessors |
| 51 | +* @param {integer} strideX - stride length for `x` |
| 52 | +* @param {NonNegativeInteger} offsetX - starting index for `x` |
| 53 | +* @param {Object} y - output array object |
| 54 | +* @param {Collection} y.data - output array data |
| 55 | +* @param {Array<Function>} y.accessors - array element accessors |
| 56 | +* @param {integer} strideY - stride length for `y` |
| 57 | +* @param {NonNegativeInteger} offsetY - starting index for `y` |
| 58 | +* @returns {Object} output array object |
| 59 | +* |
| 60 | +* @example |
| 61 | +* var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); |
| 62 | +* var arraylike2object = require( '@stdlib/array/base/arraylike2object' ); |
| 63 | +* |
| 64 | +* var x = [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ]; |
| 65 | +* var y = [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ]; |
| 66 | +* |
| 67 | +* gcusumpw( 4, 0.0, arraylike2object( toAccessorArray( x ) ), 2, 1, arraylike2object( toAccessorArray( y ) ), 1, 0 ); |
| 68 | +* // y => [ 1.0, -1.0, 1.0, 5.0, 0.0, 0.0, 0.0, 0.0 ] |
| 69 | +*/ |
| 70 | +function gcusumpw( N, sum, x, strideX, offsetX, y, strideY, offsetY ) { |
| 71 | + var xbuf; |
| 72 | + var ybuf; |
| 73 | + var xget; |
| 74 | + var yget; |
| 75 | + var yset; |
| 76 | + var ix; |
| 77 | + var iy; |
| 78 | + var s; |
| 79 | + var n; |
| 80 | + var i; |
| 81 | + |
| 82 | + // Cache reference to array data: |
| 83 | + xbuf = x.data; |
| 84 | + ybuf = y.data; |
| 85 | + |
| 86 | + // Cache reference to the element accessors: |
| 87 | + xget = x.accessors[ 0 ]; |
| 88 | + yget = y.accessors[ 0 ]; |
| 89 | + yset = y.accessors[ 1 ]; |
| 90 | + |
| 91 | + ix = offsetX; |
| 92 | + iy = offsetY; |
| 93 | + if ( N <= BLOCKSIZE ) { |
| 94 | + s = 0.0; |
| 95 | + for ( i = 0; i < N; i++ ) { |
| 96 | + s += xget( xbuf, ix ); |
| 97 | + yset( ybuf, iy, sum + s); |
| 98 | + ix += strideX; |
| 99 | + iy += strideY; |
| 100 | + } |
| 101 | + return y; |
| 102 | + } |
| 103 | + n = floor( N/2 ); |
| 104 | + gcusumpw( n, sum, x, strideX, ix, y, strideY, iy ); |
| 105 | + iy += (n-1) * strideY; |
| 106 | + gcusumpw( N-n, yget( ybuf, iy ), x, strideX, ix+(n*strideX), y, strideY, iy+strideY ); // eslint-disable-line max-len |
| 107 | + return y; |
| 108 | +} |
| 109 | + |
| 110 | + |
| 111 | +// EXPORTS // |
| 112 | + |
| 113 | +module.exports = gcusumpw; |
0 commit comments