Skip to content

Commit 8af68c2

Browse files
committed
feat: add accessor arrays support
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: na - task: lint_repl_help status: na - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: na - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed --- --- type: pre_push_report description: Results of running various checks prior to pushing changes. report: - task: run_javascript_examples status: na - task: run_c_examples status: na - task: run_cpp_examples status: na - task: run_javascript_readme_examples status: na - task: run_c_benchmarks status: na - task: run_cpp_benchmarks status: na - task: run_fortran_benchmarks status: na - task: run_javascript_benchmarks status: na - task: run_julia_benchmarks status: na - task: run_python_benchmarks status: na - task: run_r_benchmarks status: na - task: run_javascript_tests status: na ---
1 parent 611fed3 commit 8af68c2

File tree

7 files changed

+276
-24
lines changed

7 files changed

+276
-24
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ var v = gapxsumkbn2.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 ([`dapxsumkbn2`][@stdlib/blas/ext/base/dapxsumkbn2], [`sapxsumkbn2`][@stdlib/blas/ext/base/sapxsumkbn2], 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
[@klein:2005a]: https://doi.org/10.1007/s00607-005-0139-x
179182

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

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@
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+
* Input array.
26+
*/
27+
type InputArray = NumericArray | Collection<number> | AccessorArrayLike<number>;
2428

2529
/**
2630
* Interface describing `gapxsumkbn2`.
@@ -41,7 +45,7 @@ interface Routine {
4145
* var v = gapxsumkbn2( x.length, 5.0, x, 1 );
4246
* // returns 16.0
4347
*/
44-
( N: number, alpha: number, x: NumericArray, strideX: number ): number;
48+
( N: number, alpha: number, x: InputArray, strideX: number ): number;
4549

4650
/**
4751
* Adds a scalar constant to each strided array element and computes the sum using a second-order iterative Kahan–Babuška algorithm and alternative indexing semantics.
@@ -59,7 +63,7 @@ interface Routine {
5963
* var v = gapxsumkbn2.ndarray( x.length, 5.0, x, 1, 0 );
6064
* // returns 16.0
6165
*/
62-
ndarray( N: number, alpha: number, x: NumericArray, strideX: number, offsetX: number ): number;
66+
ndarray( N: number, alpha: number, x: InputArray, strideX: number, offsetX: number ): number;
6367
}
6468

6569
/**

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

2122

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

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

102104
gapxsumkbn2.ndarray( x.length, 5.0, x, 1, 0 ); // $ExpectType number
105+
gapxsumkbn2.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: 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+
* Adds a scalar constant to each strided array element and computes the sum 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} alpha - scalar constant
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
46+
* @param {NonNegativeInteger} offsetX - starting index
47+
* @returns {number} sum
48+
*
49+
* @example
50+
* var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' );
51+
* var arraylike2object = require( '@stdlib/array/base/arraylike2object' );
52+
*
53+
* var x = toAccessorArray( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ] );
54+
*
55+
* var v = gapxsumkbn2( 4, 5.0, arraylike2object( x ), 2, 1 );
56+
* // returns 25.0
57+
*/
58+
function gapxsumkbn2( N, alpha, x, strideX, offsetX ) {
59+
var xbuf;
60+
var get;
61+
var sum;
62+
var ccs;
63+
var ix;
64+
var cs;
65+
var cc;
66+
var v;
67+
var t;
68+
var c;
69+
var i;
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 * ( alpha + get( xbuf, ix ) );
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 = alpha + 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 = gapxsumkbn2;

lib/node_modules/@stdlib/blas/ext/base/gapxsumkbn2/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 //
@@ -58,11 +60,16 @@ function gapxsumkbn2( N, alpha, x, strideX, offsetX ) {
5860
var v;
5961
var t;
6062
var c;
63+
var o;
6164
var i;
6265

6366
if ( N <= 0 ) {
6467
return 0.0;
6568
}
69+
o = arraylike2object( x );
70+
if ( o.accessorProtocol ) {
71+
return accessors( N, alpha, o, strideX, offsetX );
72+
}
6673
ix = offsetX;
6774
if ( strideX === 0 ) {
6875
return N * ( alpha + x[ ix ] );

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

Lines changed: 53 additions & 11 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 gapxsumkbn2 = 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 = gapxsumkbn2( 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 = gapxsumkbn2( 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 = gapxsumkbn2( x.length, 5.0, toAccessorArray( x ), 1 );
84+
t.strictEqual( v, 2.0, 'returns expected value' );
85+
86+
x = [ NaN, 4.0 ];
87+
v = gapxsumkbn2( 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 = gapxsumkbn2( 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 = gapxsumkbn2( N, 5.0, x, 2 );
139+
v = gapxsumkbn2( 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 = gapxsumkbn2( 4, 5.0, toAccessorArray( x ), 2 );
115161

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

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

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

136-
N = floor( x.length / 2 );
137-
v = gapxsumkbn2( N, 5.0, x, -2 );
181+
v = gapxsumkbn2( 4, 5.0, toAccessorArray( x ), -2 );
138182

139183
t.strictEqual( v, 25.0, 'returns expected value' );
140184
t.end();
@@ -155,7 +199,6 @@ tape( 'if provided a `stride` parameter equal to `0`, the function returns the f
155199
tape( 'the function supports view offsets', function test( t ) {
156200
var x0;
157201
var x1;
158-
var N;
159202
var v;
160203

161204
x0 = new Float64Array([
@@ -171,9 +214,8 @@ tape( 'the function supports view offsets', function test( t ) {
171214
]);
172215

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

176-
v = gapxsumkbn2( N, 5.0, x1, 2 );
218+
v = gapxsumkbn2( 4, 5.0, x1, 2 );
177219
t.strictEqual( v, 25.0, 'returns expected value' );
178220

179221
t.end();

0 commit comments

Comments
 (0)