|
| 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 abs = require( '@stdlib/math/base/special/abs' ); |
| 24 | + |
| 25 | + |
| 26 | +// MAIN // |
| 27 | + |
| 28 | +/** |
| 29 | +* Computes the cumulative sum of strided array elements using a second-order iterative Kahan–Babuška algorithm. |
| 30 | +* |
| 31 | +* ## Method |
| 32 | +* |
| 33 | +* - This implementation uses a second-order iterative Kahan–Babuška algorithm, as described by Klein (2005). |
| 34 | +* |
| 35 | +* ## References |
| 36 | +* |
| 37 | +* - Klein, Andreas. 2005. "A Generalized Kahan-Babuška-Summation-Algorithm." _Computing_ 76 (3): 279–93. doi:[10.1007/s00607-005-0139-x](https://doi.org/10.1007/s00607-005-0139-x). |
| 38 | +* |
| 39 | +* @private |
| 40 | +* @param {PositiveInteger} N - number of indexed elements |
| 41 | +* @param {number} sum - initial sum |
| 42 | +* @param {Object} x - input array object |
| 43 | +* @param {Collection} x.data - input array data |
| 44 | +* @param {Array<Function>} x.accessors - array element accessors |
| 45 | +* @param {integer} strideX - stride length for `x` |
| 46 | +* @param {NonNegativeInteger} offsetX - starting index for `x` |
| 47 | +* @param {Object} y - output array object |
| 48 | +* @param {Collection} y.data - output array data |
| 49 | +* @param {Array<Function>} y.accessors - array element accessors |
| 50 | +* @param {integer} strideY - stride length for `y` |
| 51 | +* @param {NonNegativeInteger} offsetY - starting index for `y` |
| 52 | +* @returns {Object} output array object |
| 53 | +* |
| 54 | +* @example |
| 55 | +* var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); |
| 56 | +* var arraylike2object = require( '@stdlib/array/base/arraylike2object' ); |
| 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 | +* |
| 61 | +* gcusumkbn2( 4, 0.0, arraylike2object( toAccessorArray( x ) ), 2, 1, arraylike2object( toAccessorArray( y ) ), 1, 0 ); |
| 62 | +* // y => [ 1.0, -1.0, 1.0, 5.0, 0.0, 0.0, 0.0, 0.0 ] |
| 63 | +*/ |
| 64 | +function gcusumkbn2( N, sum, x, strideX, offsetX, y, strideY, offsetY ) { |
| 65 | + var xbuf; |
| 66 | + var ybuf; |
| 67 | + var xget; |
| 68 | + var yset; |
| 69 | + var ccs; |
| 70 | + var ix; |
| 71 | + var iy; |
| 72 | + var cs; |
| 73 | + var cc; |
| 74 | + var v; |
| 75 | + var t; |
| 76 | + var c; |
| 77 | + var i; |
| 78 | + |
| 79 | + // Cache reference to array data: |
| 80 | + xbuf = x.data; |
| 81 | + ybuf = y.data; |
| 82 | + |
| 83 | + // Cache reference to the element accessors: |
| 84 | + xget = x.accessors[ 0 ]; |
| 85 | + yset = y.accessors[ 1 ]; |
| 86 | + |
| 87 | + ix = offsetX; |
| 88 | + iy = offsetY; |
| 89 | + ccs = 0.0; // second order correction term for lost low order bits |
| 90 | + cs = 0.0; // first order correction term for lost low order bits |
| 91 | + for ( i = 0; i < N; i++ ) { |
| 92 | + v = xget( xbuf, ix ); |
| 93 | + t = sum + v; |
| 94 | + if ( abs( sum ) >= abs( v ) ) { |
| 95 | + c = (sum-t) + v; |
| 96 | + } else { |
| 97 | + c = (v-t) + sum; |
| 98 | + } |
| 99 | + sum = t; |
| 100 | + t = cs + c; |
| 101 | + if ( abs( cs ) >= abs( c ) ) { |
| 102 | + cc = (cs-t) + c; |
| 103 | + } else { |
| 104 | + cc = (c-t) + cs; |
| 105 | + } |
| 106 | + cs = t; |
| 107 | + ccs += cc; |
| 108 | + |
| 109 | + yset( ybuf, iy, sum + cs + ccs ); |
| 110 | + ix += strideX; |
| 111 | + iy += strideY; |
| 112 | + } |
| 113 | + return y; |
| 114 | +} |
| 115 | + |
| 116 | + |
| 117 | +// EXPORTS // |
| 118 | + |
| 119 | +module.exports = gcusumkbn2; |
0 commit comments