Skip to content

Commit 906a39e

Browse files
authored
feat: add accessor arrays support to blas/ext/base/gapxsumkbn
PR-URL: #4888 Reviewed-by: Athan Reines <kgryte@gmail.com>
1 parent cce9542 commit 906a39e

File tree

7 files changed

+277
-23
lines changed

7 files changed

+277
-23
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ var v = gapxsumkbn.ndarray( 4, 5.0, 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 ([`dapxsumkbn`][@stdlib/blas/ext/base/dapxsumkbn], [`sapxsumkbn`][@stdlib/blas/ext/base/sapxsumkbn], etc.) are likely to be significantly more performant.
113114

114115
</section>
@@ -175,6 +176,8 @@ console.log( v );
175176

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

179+
[@stdlib/array/base/accessor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/base/accessor
180+
178181
[@neumaier:1974a]: https://doi.org/10.1002/zamm.19740540106
179182

180183
<!-- <related-links> -->

lib/node_modules/@stdlib/blas/ext/base/gapxsumkbn/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 `gapxsumkbn`.
@@ -41,7 +46,7 @@ interface Routine {
4146
* var v = gapxsumkbn( x.length, 5.0, x, 1 );
4247
* // returns 16.0
4348
*/
44-
( N: number, alpha: number, x: NumericArray, strideX: number ): number;
49+
( N: number, alpha: number, x: InputArray, strideX: number ): number;
4550

4651
/**
4752
* Adds a scalar constant to each strided array element and computes the sum using an improved Kahan–Babuška algorithm and alternative indexing semantics.
@@ -59,7 +64,7 @@ interface Routine {
5964
* var v = gapxsumkbn.ndarray( x.length, 5.0, x, 1, 0 );
6065
* // returns 16.0
6166
*/
62-
ndarray( N: number, alpha: number, x: NumericArray, strideX: number, offsetX: number ): number;
67+
ndarray( N: number, alpha: number, x: InputArray, strideX: number, offsetX: number ): number;
6368
}
6469

6570
/**

lib/node_modules/@stdlib/blas/ext/base/gapxsumkbn/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 gapxsumkbn = require( './index' );
2021

2122

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

2829
gapxsumkbn( x.length, 5.0, x, 1 ); // $ExpectType number
30+
gapxsumkbn( x.length, 5.0, 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...
@@ -100,6 +102,7 @@ import gapxsumkbn = require( './index' );
100102
const x = new Float64Array( 10 );
101103

102104
gapxsumkbn.ndarray( x.length, 5.0, x, 1, 0 ); // $ExpectType number
105+
gapxsumkbn.ndarray( x.length, 5.0, new AccessorArray( x ), 1, 0 ); // $ExpectType number
103106
}
104107

105108
// The compiler throws an error if the `ndarray` method is provided a first argument which is not a number...
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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+
* Adds a scalar constant to each strided array element and computes the sum using an improved Kahan–Babuška algorithm.
30+
*
31+
* @private
32+
* @param {PositiveInteger} N - number of indexed elements
33+
* @param {number} alpha - scalar constant
34+
* @param {Object} x - input array object
35+
* @param {Collection} x.data - input array data
36+
* @param {Array<Function>} x.accessors - array element accessors
37+
* @param {integer} strideX - stride length
38+
* @param {NonNegativeInteger} offsetX - starting index
39+
* @returns {number} sum
40+
*
41+
* @example
42+
* var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' );
43+
* var arraylike2object = require( '@stdlib/array/base/arraylike2object' );
44+
*
45+
* var x = toAccessorArray( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ] );
46+
*
47+
* var v = gapxsumkbn( 4, 5.0, arraylike2object( x ), 2, 1 );
48+
* // returns 25.0
49+
*/
50+
function gapxsumkbn( N, alpha, x, strideX, offsetX ) {
51+
var xbuf;
52+
var get;
53+
var sum;
54+
var ix;
55+
var v;
56+
var t;
57+
var c;
58+
var i;
59+
60+
// Cache reference to array data:
61+
xbuf = x.data;
62+
63+
// Cache a reference to the element accessor:
64+
get = x.accessors[ 0 ];
65+
66+
ix = offsetX;
67+
if ( strideX === 0 ) {
68+
return N * ( alpha + get( xbuf, ix ) );
69+
}
70+
sum = 0.0;
71+
c = 0.0;
72+
for ( i = 0; i < N; i++ ) {
73+
v = alpha + get( xbuf, ix );
74+
t = sum + v;
75+
if ( abs( sum ) >= abs( v ) ) {
76+
c += (sum-t) + v;
77+
} else {
78+
c += (v-t) + sum;
79+
}
80+
sum = t;
81+
ix += strideX;
82+
}
83+
return sum + c;
84+
}
85+
86+
87+
// EXPORTS //
88+
89+
module.exports = gapxsumkbn;

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@
2020

2121
// MODULES //
2222

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

2527

2628
// MAIN //
@@ -55,11 +57,16 @@ function gapxsumkbn( N, alpha, x, strideX, offsetX ) {
5557
var v;
5658
var t;
5759
var c;
60+
var o;
5861
var i;
5962

6063
if ( N <= 0 ) {
6164
return 0.0;
6265
}
66+
o = arraylike2object( x );
67+
if ( o.accessorProtocol ) {
68+
return accessors( N, alpha, o, strideX, offsetX );
69+
}
6370
ix = offsetX;
6471
if ( strideX === 0 ) {
6572
return N * ( alpha + x[ ix ] );

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

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

2323
var tape = require( 'tape' );
24-
var floor = require( '@stdlib/math/base/special/floor' );
2524
var isnan = require( '@stdlib/math/base/assert/is-nan' );
25+
var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' );
2626
var Float64Array = require( '@stdlib/array/float64' );
2727
var gapxsumkbn = require( './../lib' );
2828

@@ -67,6 +67,33 @@ tape( 'the function adds a constant and calculates the sum of all strided array
6767
t.end();
6868
});
6969

70+
tape( 'the function adds a constant and 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 = gapxsumkbn( x.length, 5.0, toAccessorArray( x ), 1 );
76+
t.strictEqual( v, 48.0, 'returns expected value' );
77+
78+
x = [ 1.0, -2.0, -4.0, 5.0, 0.0, 3.0 ];
79+
v = gapxsumkbn( x.length, 5.0, toAccessorArray( x ), 1 );
80+
t.strictEqual( v, 33.0, 'returns expected value' );
81+
82+
x = [ -4.0, -4.0 ];
83+
v = gapxsumkbn( x.length, 5.0, toAccessorArray( x ), 1 );
84+
t.strictEqual( v, 2.0, 'returns expected value' );
85+
86+
x = [ NaN, 4.0 ];
87+
v = gapxsumkbn( x.length, 5.0, toAccessorArray( x ), 1 );
88+
t.strictEqual( isnan( v ), true, 'returns expected value' );
89+
90+
x = [ 1.0, 1e100, 1.0, -1.0e100 ];
91+
v = gapxsumkbn( x.length, 5.0, toAccessorArray( x ), 1 );
92+
t.strictEqual( v, 12.0, 'returns expected value' );
93+
94+
t.end();
95+
});
96+
7097
tape( 'if provided an `N` parameter less than or equal to `0`, the function returns `0.0`', function test( t ) {
7198
var x;
7299
var v;
@@ -95,7 +122,6 @@ tape( 'if provided an `N` parameter equal to `1`, the function returns the first
95122
});
96123

97124
tape( 'the function supports a `stride` parameter', function test( t ) {
98-
var N;
99125
var x;
100126
var v;
101127

@@ -110,15 +136,34 @@ tape( 'the function supports a `stride` parameter', function test( t ) {
110136
2.0
111137
];
112138

113-
N = floor( x.length / 2 );
114-
v = gapxsumkbn( N, 5.0, x, 2 );
139+
v = gapxsumkbn( 4, 5.0, x, 2 );
140+
141+
t.strictEqual( v, 25.0, 'returns expected value' );
142+
t.end();
143+
});
144+
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 = gapxsumkbn( 4, 5.0, toAccessorArray( x ), 2 );
115161

116162
t.strictEqual( v, 25.0, 'returns expected value' );
117163
t.end();
118164
});
119165

120166
tape( 'the function supports a negative `stride` parameter', function test( t ) {
121-
var N;
122167
var x;
123168
var v;
124169

@@ -133,8 +178,28 @@ tape( 'the function supports a negative `stride` parameter', function test( t )
133178
2.0
134179
];
135180

136-
N = floor( x.length / 2 );
137-
v = gapxsumkbn( N, 5.0, x, -2 );
181+
v = gapxsumkbn( 4, 5.0, x, -2 );
182+
183+
t.strictEqual( v, 25.0, 'returns expected value' );
184+
t.end();
185+
});
186+
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 = gapxsumkbn( 4, 5.0, toAccessorArray( x ), -2 );
138203

139204
t.strictEqual( v, 25.0, 'returns expected value' );
140205
t.end();
@@ -155,7 +220,6 @@ tape( 'if provided a `stride` parameter equal to `0`, the function returns the f
155220
tape( 'the function supports view offsets', function test( t ) {
156221
var x0;
157222
var x1;
158-
var N;
159223
var v;
160224

161225
x0 = new Float64Array([
@@ -171,9 +235,8 @@ tape( 'the function supports view offsets', function test( t ) {
171235
]);
172236

173237
x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
174-
N = floor(x1.length / 2);
175238

176-
v = gapxsumkbn( N, 5.0, x1, 2 );
239+
v = gapxsumkbn( 4, 5.0, x1, 2 );
177240
t.strictEqual( v, 25.0, 'returns expected value' );
178241

179242
t.end();

0 commit comments

Comments
 (0)