Skip to content

Commit 3f3edff

Browse files
authored
feat: add support for accessor arrays in blas/ext/base/gsumkbn
PR-URL: #4923 Reviewed-by: Athan Reines <[email protected]>
1 parent 7456e9f commit 3f3edff

File tree

7 files changed

+281
-2
lines changed

7 files changed

+281
-2
lines changed

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

+3
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ var v = gsumkbn.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 ([`dsum`][@stdlib/blas/ext/base/dsum], [`ssum`][@stdlib/blas/ext/base/ssum], 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
[@stdlib/blas/ext/base/dsum]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/blas/ext/base/dsum
182185

183186
[@stdlib/blas/ext/base/ssum]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/blas/ext/base/ssum

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

+7-2
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 `gsumkbn`.
@@ -40,7 +45,7 @@ interface Routine {
4045
* var v = gsumkbn( 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 an improved Kahan–Babuška algorithm and alternative indexing semantics.

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

+3
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 gsumkbn = require( './index' );
2021

2122

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

2829
gsumkbn( x.length, x, 1 ); // $ExpectType number
30+
gsumkbn( 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 gsumkbn = require( './index' );
8587
const x = new Float64Array( 10 );
8688

8789
gsumkbn.ndarray( x.length, x, 1, 0 ); // $ExpectType number
90+
gsumkbn.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...
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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 an improved Kahan–Babuška algorithm.
30+
*
31+
* ## Method
32+
*
33+
* - This implementation uses an "improved Kahan–Babuška algorithm", as described by Neumaier (1974).
34+
*
35+
* ## References
36+
*
37+
* - Neumaier, Arnold. 1974. "Rounding Error Analysis of Some Methods for Summing Finite Sums." _Zeitschrift Für Angewandte Mathematik Und Mechanik_ 54 (1): 39–51. doi:[10.1002/zamm.19740540106](https://doi.org/10.1002/zamm.19740540106).
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 = gsumkbn( 4, arraylike2object( x ), 2, 1 );
55+
* // returns 5.0
56+
*/
57+
function gsumkbn( N, x, strideX, offsetX ) {
58+
var xbuf;
59+
var get;
60+
var sum;
61+
var ix;
62+
var v;
63+
var t;
64+
var c;
65+
var i;
66+
67+
if ( N <= 0 ) {
68+
return 0.0;
69+
}
70+
71+
// Cache reference to array data:
72+
xbuf = x.data;
73+
74+
// Cache a reference to the element accessor:
75+
get = x.accessors[ 0 ];
76+
77+
ix = offsetX;
78+
if ( strideX === 0 ) {
79+
return N * get( xbuf, ix );
80+
}
81+
sum = 0.0;
82+
c = 0.0;
83+
for ( i = 0; i < N; i++ ) {
84+
v = get( xbuf, ix );
85+
t = sum + v;
86+
if ( abs( sum ) >= abs( v ) ) {
87+
c += (sum-t) + v;
88+
} else {
89+
c += (v-t) + sum;
90+
}
91+
sum = t;
92+
ix += strideX;
93+
}
94+
return sum + c;
95+
}
96+
97+
98+
// EXPORTS //
99+
100+
module.exports = gsumkbn;

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

+7
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 //
@@ -51,6 +53,7 @@ var abs = require( '@stdlib/math/base/special/abs' );
5153
function gsumkbn( N, x, strideX, offsetX ) {
5254
var sum;
5355
var ix;
56+
var o;
5457
var v;
5558
var t;
5659
var c;
@@ -59,6 +62,10 @@ function gsumkbn( N, x, strideX, offsetX ) {
5962
if ( N <= 0 ) {
6063
return 0.0;
6164
}
65+
o = arraylike2object( x );
66+
if ( o.accessorProtocol ) {
67+
return accessors( N, o, strideX, offsetX );
68+
}
6269
ix = offsetX;
6370
if ( strideX === 0 ) {
6471
return N * x[ ix ];

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

+70
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 gsumkbn = 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 = gsumkbn( 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 = gsumkbn( x.length, toAccessorArray( x ), 1 );
80+
t.strictEqual( v, 3.0, 'returns expected value' );
81+
82+
x = [ -4.0, -4.0 ];
83+
v = gsumkbn( x.length, toAccessorArray( x ), 1 );
84+
t.strictEqual( v, -8.0, 'returns expected value' );
85+
86+
x = [ NaN, 4.0 ];
87+
v = gsumkbn( 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 = gsumkbn( 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 = gsumkbn( 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 = gsumkbn( 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)