Skip to content

Commit 97f01ab

Browse files
authored
feat: add accessor arrays support to blas/ext/base/gsumkbn2
PR-URL: #4960 Reviewed-by: Athan Reines <[email protected]>
1 parent 1d1e8b7 commit 97f01ab

File tree

7 files changed

+291
-3
lines changed

7 files changed

+291
-3
lines changed

lib/node_modules/@stdlib/blas/ext/base/gsumkbn2/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ var v = gsumkbn2.ndarray( 4, x, 2, 1 );
109109
## Notes
110110

111111
- If `N <= 0`, both functions return `0.0`.
112+
- Both functions support array-like objects having getter and setter accessors for array element access (e.g., [`@stdlib/array/base/accessor`][@stdlib/array/base/accessor]).
112113
- Depending on the environment, the typed versions ([`dsumkbn2`][@stdlib/blas/ext/base/dsumkbn2], [`ssumkbn2`][@stdlib/blas/ext/base/ssumkbn2], etc.) are likely to be significantly more performant.
113114

114115
</section>
@@ -178,6 +179,8 @@ console.log( v );
178179

179180
[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
180181

182+
[@stdlib/array/base/accessor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/base/accessor
183+
181184
[@klein:2005a]: https://doi.org/10.1007/s00607-005-0139-x
182185

183186
<!-- <related-links> -->

lib/node_modules/@stdlib/blas/ext/base/gsumkbn2/docs/types/index.d.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,12 @@
2020

2121
/// <reference types="@stdlib/types"/>
2222

23-
import { NumericArray } from '@stdlib/types/array';
23+
import { NumericArray, Collection, AccessorArrayLike } from '@stdlib/types/array';
24+
25+
/**
26+
* Input array.
27+
*/
28+
type InputArray = NumericArray | Collection<number> | AccessorArrayLike<number>;
2429

2530
/**
2631
* Interface describing `gsumkbn2`.
@@ -40,7 +45,7 @@ interface Routine {
4045
* var v = gsumkbn2( x.length, x, 1 );
4146
* // returns 1.0
4247
*/
43-
( N: number, x: NumericArray, strideX: number ): number;
48+
( N: number, x: InputArray, strideX: number ): number;
4449

4550
/**
4651
* Computes the sum of strided array elements using a second-order iterative Kahan–Babuška algorithm and alternative indexing semantics.
@@ -57,7 +62,7 @@ interface Routine {
5762
* var v = gsumkbn2.ndarray( x.length, x, 1, 0 );
5863
* // returns 1.0
5964
*/
60-
ndarray( N: number, x: NumericArray, strideX: number, offsetX: number ): number;
65+
ndarray( N: number, x: InputArray, strideX: number, offsetX: number ): number;
6166
}
6267

6368
/**

lib/node_modules/@stdlib/blas/ext/base/gsumkbn2/docs/types/test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
* limitations under the License.
1717
*/
1818

19+
import AccessorArray = require( '@stdlib/array/base/accessor' );
1920
import gsumkbn2 = require( './index' );
2021

2122

@@ -26,6 +27,7 @@ import gsumkbn2 = require( './index' );
2627
const x = new Float64Array( 10 );
2728

2829
gsumkbn2( x.length, x, 1 ); // $ExpectType number
30+
gsumkbn2( x.length, new AccessorArray( x ), 1 ); // $ExpectType number
2931
}
3032

3133
// The compiler throws an error if the function is provided a first argument which is not a number...
@@ -85,6 +87,7 @@ import gsumkbn2 = require( './index' );
8587
const x = new Float64Array( 10 );
8688

8789
gsumkbn2.ndarray( x.length, x, 1, 0 ); // $ExpectType number
90+
gsumkbn2.ndarray( x.length, new AccessorArray( x ), 1, 0 ); // $ExpectType number
8891
}
8992

9093
// The compiler throws an error if the `ndarray` method is provided a first argument which is not a number...
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
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 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 {Object} x - input array object
42+
* @param {Collection} x.data - input array data
43+
* @param {Array<Function>} x.accessors - array element accessors
44+
* @param {integer} strideX - stride length
45+
* @param {NonNegativeInteger} offsetX - starting index
46+
* @returns {number} sum
47+
*
48+
* @example
49+
* var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' );
50+
* var arraylike2object = require( '@stdlib/array/base/arraylike2object' );
51+
*
52+
* var x = toAccessorArray( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ] );
53+
*
54+
* var v = gsumkbn2( 4, arraylike2object( x ), 2, 1 );
55+
* // returns 5.0
56+
*/
57+
function gsumkbn2( N, x, strideX, offsetX ) {
58+
var xbuf;
59+
var get;
60+
var sum;
61+
var ccs;
62+
var ix;
63+
var cs;
64+
var cc;
65+
var v;
66+
var t;
67+
var c;
68+
var i;
69+
70+
// Cache reference to array data:
71+
xbuf = x.data;
72+
73+
// Cache a reference to the element accessor:
74+
get = x.accessors[ 0 ];
75+
76+
ix = offsetX;
77+
if ( strideX === 0 ) {
78+
return N * get( xbuf, ix );
79+
}
80+
81+
sum = 0.0;
82+
ccs = 0.0; // second order correction term for lost low order bits
83+
cs = 0.0; // first order correction term for lost low order bits
84+
for ( i = 0; i < N; i++ ) {
85+
v = get( xbuf, ix );
86+
t = sum + v;
87+
if ( abs( sum ) >= abs( v ) ) {
88+
c = (sum-t) + v;
89+
} else {
90+
c = (v-t) + sum;
91+
}
92+
sum = t;
93+
t = cs + c;
94+
if ( abs( cs ) >= abs( c ) ) {
95+
cc = (cs-t) + c;
96+
} else {
97+
cc = (c-t) + cs;
98+
}
99+
cs = t;
100+
ccs += cc;
101+
ix += strideX;
102+
}
103+
return sum + cs + ccs;
104+
}
105+
106+
107+
// EXPORTS //
108+
109+
module.exports = gsumkbn2;

lib/node_modules/@stdlib/blas/ext/base/gsumkbn2/lib/ndarray.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
// MODULES //
2222

2323
var abs = require( '@stdlib/math/base/special/abs' );
24+
var arraylike2object = require( '@stdlib/array/base/arraylike2object' );
25+
var accessors = require( './accessors.js' );
2426

2527

2628
// MAIN //
@@ -54,6 +56,7 @@ function gsumkbn2( N, x, strideX, offsetX ) {
5456
var ix;
5557
var cs;
5658
var cc;
59+
var o;
5760
var v;
5861
var t;
5962
var c;
@@ -62,6 +65,10 @@ function gsumkbn2( N, x, strideX, offsetX ) {
6265
if ( N <= 0 ) {
6366
return 0.0;
6467
}
68+
o = arraylike2object( x );
69+
if ( o.accessorProtocol ) {
70+
return accessors( N, o, strideX, offsetX );
71+
}
6572
ix = offsetX;
6673
if ( strideX === 0 ) {
6774
return N * x[ ix ];

lib/node_modules/@stdlib/blas/ext/base/gsumkbn2/test/test.main.js

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
var tape = require( 'tape' );
2424
var isnan = require( '@stdlib/math/base/assert/is-nan' );
25+
var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' );
2526
var Float64Array = require( '@stdlib/array/float64' );
2627
var gsumkbn2 = require( './../lib' );
2728

@@ -66,6 +67,33 @@ tape( 'the function calculates the sum of all strided array elements', function
6667
t.end();
6768
});
6869

70+
tape( 'the function calculates the sum of all strided array elements (accessors)', function test( t ) {
71+
var x;
72+
var v;
73+
74+
x = [ 1.0, -2.0, -4.0, 5.0, 0.0, 3.0, 0.0, -3.0, 3.0 ];
75+
v = gsumkbn2( x.length, toAccessorArray( x ), 1 );
76+
t.strictEqual( v, 3.0, 'returns expected value' );
77+
78+
x = [ 1.0, -2.0, -4.0, 5.0, 0.0, 3.0 ];
79+
v = gsumkbn2( x.length, toAccessorArray( x ), 1 );
80+
t.strictEqual( v, 3.0, 'returns expected value' );
81+
82+
x = [ -4.0, -4.0 ];
83+
v = gsumkbn2( x.length, toAccessorArray( x ), 1 );
84+
t.strictEqual( v, -8.0, 'returns expected value' );
85+
86+
x = [ NaN, 4.0 ];
87+
v = gsumkbn2( x.length, toAccessorArray( x ), 1 );
88+
t.strictEqual( isnan( v ), true, 'returns expected value' );
89+
90+
x = [ 1.0, 1.0e100, 1.0, -1.0e100 ];
91+
v = gsumkbn2( x.length, toAccessorArray( x ), 1 );
92+
t.strictEqual( v, 2.0, 'returns expected value' );
93+
94+
t.end();
95+
});
96+
6997
tape( 'if provided an `N` parameter less than or equal to `0`, the function returns `0.0`', function test( t ) {
7098
var x;
7199
var v;
@@ -114,6 +142,27 @@ tape( 'the function supports a `stride` parameter', function test( t ) {
114142
t.end();
115143
});
116144

145+
tape( 'the function supports a `stride` parameter (accessors)', function test( t ) {
146+
var x;
147+
var v;
148+
149+
x = [
150+
1.0, // 0
151+
2.0,
152+
2.0, // 1
153+
-7.0,
154+
-2.0, // 2
155+
3.0,
156+
4.0, // 3
157+
2.0
158+
];
159+
160+
v = gsumkbn2( 4, toAccessorArray( x ), 2 );
161+
162+
t.strictEqual( v, 5.0, 'returns expected value' );
163+
t.end();
164+
});
165+
117166
tape( 'the function supports a negative `stride` parameter', function test( t ) {
118167
var x;
119168
var v;
@@ -135,6 +184,27 @@ tape( 'the function supports a negative `stride` parameter', function test( t )
135184
t.end();
136185
});
137186

187+
tape( 'the function supports a negative `stride` parameter (accessors)', function test( t ) {
188+
var x;
189+
var v;
190+
191+
x = [
192+
1.0, // 3
193+
2.0,
194+
2.0, // 2
195+
-7.0,
196+
-2.0, // 1
197+
3.0,
198+
4.0, // 0
199+
2.0
200+
];
201+
202+
v = gsumkbn2( 4, toAccessorArray( x ), -2 );
203+
204+
t.strictEqual( v, 5.0, 'returns expected value' );
205+
t.end();
206+
});
207+
138208
tape( 'if provided a `stride` parameter equal to `0`, the function returns the sum of the first element repeated N times', function test( t ) {
139209
var x;
140210
var v;

0 commit comments

Comments
 (0)