Skip to content

Commit 826bd55

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 826bd55

File tree

7 files changed

+244
-23
lines changed

7 files changed

+244
-23
lines changed

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

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

111111
- If `N <= 0`, both functions return `0.0`.
112112
- Ordinary recursive summation (i.e., a "simple" sum) is performant, but can incur significant numerical error. If performance is paramount and error tolerated, using ordinary recursive summation is acceptable; in all other cases, exercise due caution.
113+
- 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])
113114
- Depending on the environment, the typed versions ([`dapxsumors`][@stdlib/blas/ext/base/dapxsumors], [`sapxsumors`][@stdlib/blas/ext/base/sapxsumors], etc.) are likely to be significantly more performant.
114115

115116
</section>
@@ -170,6 +171,8 @@ console.log( v );
170171

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

174+
[@stdlib/array/base/accessor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/base/accessor
175+
173176
<!-- <related-links> -->
174177

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

lib/node_modules/@stdlib/blas/ext/base/gapxsumors/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 `gapxsumors`.
@@ -41,7 +46,7 @@ interface Routine {
4146
* var v = gapxsumors( 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 ordinary recursive summation and alternative indexing semantics.
@@ -59,7 +64,7 @@ interface Routine {
5964
* var v = gapxsumors.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/gapxsumors/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 gapxsumors = require( './index' );
2021

2122

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

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

102104
gapxsumors.ndarray( x.length, 5.0, x, 1, 0 ); // $ExpectType number
105+
gapxsumors.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: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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+
// MAIN //
22+
23+
/**
24+
* Adds a scalar constant to each strided array element and computes the sum using ordinary recursive summation.
25+
*
26+
* @private
27+
* @param {PositiveInteger} N - number of indexed elements
28+
* @param {number} alpha - scalar constant
29+
* @param {Object} x - input array object
30+
* @param {Collection} x.data - input array data
31+
* @param {Array<Function>} x.accessors - array element accessors
32+
* @param {integer} strideX - stride length
33+
* @param {NonNegativeInteger} offsetX - starting index
34+
* @returns {number} sum
35+
*
36+
* @example
37+
* var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' );
38+
* var arraylike2object = require( '@stdlib/array/base/arraylike2object' );
39+
*
40+
* var x = toAccessorArray( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ] );
41+
*
42+
* var v = gapxsumors( 4, 5.0, arraylike2object( x ), 2, 1 );
43+
* // returns 25.0
44+
*/
45+
function gapxsumors( N, alpha, x, strideX, offsetX ) {
46+
var xbuf;
47+
var sum;
48+
var get;
49+
var ix;
50+
var i;
51+
52+
// Cache reference to array data:
53+
xbuf = x.data;
54+
55+
// Cache a reference to the element accessor:
56+
get = x.accessors[ 0 ];
57+
58+
ix = offsetX;
59+
if ( strideX === 0 ) {
60+
return N * ( alpha + get( xbuf, ix ) );
61+
}
62+
sum = 0.0;
63+
for ( i = 0; i < N; i++ ) {
64+
sum += alpha + get( xbuf, ix );
65+
ix += strideX;
66+
}
67+
return sum;
68+
}
69+
70+
71+
// EXPORTS //
72+
73+
module.exports = gapxsumors;

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@
1818

1919
'use strict';
2020

21+
// MODULES //
22+
23+
var arraylike2object = require( '@stdlib/array/base/arraylike2object' );
24+
var accessors = require( './accessors.js' );
25+
26+
2127
// MAIN //
2228

2329
/**
@@ -39,11 +45,16 @@
3945
function gapxsumors( N, alpha, x, strideX, offsetX ) {
4046
var sum;
4147
var ix;
48+
var o;
4249
var i;
4350

4451
if ( N <= 0 ) {
4552
return 0.0;
4653
}
54+
o = arraylike2object( x );
55+
if ( o.accessorProtocol ) {
56+
return accessors( N, alpha, o, strideX, offsetX );
57+
}
4758
ix = offsetX;
4859
if ( strideX === 0 ) {
4960
return N * ( alpha + x[ ix ] );

lib/node_modules/@stdlib/blas/ext/base/gapxsumors/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 gapxsumors = 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 = gapxsumors( 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 = gapxsumors( 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 = gapxsumors( x.length, 5.0, toAccessorArray( x ), 1 );
84+
t.strictEqual( v, 2.0, 'returns expected value' );
85+
86+
x = [ NaN, 4.0 ];
87+
v = gapxsumors( 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 = gapxsumors( x.length, 5.0, toAccessorArray( x ), 1 );
92+
t.strictEqual( v, 0.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,55 @@ tape( 'the function supports a `stride` parameter', function test( t ) {
110136
2.0
111137
];
112138

113-
N = floor( x.length / 2 );
114-
v = gapxsumors( N, 5.0, x, 2 );
139+
v = gapxsumors( 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 = gapxsumors( 4, 5.0, toAccessorArray( x ), 2 );
161+
162+
t.strictEqual( v, 25.0, 'returns expected value' );
163+
t.end();
164+
});
165+
166+
tape( 'the function supports a negative `stride` parameter', function test( t ) {
167+
var x;
168+
var v;
169+
170+
x = [
171+
1.0, // 3
172+
2.0,
173+
2.0, // 2
174+
-7.0,
175+
-2.0, // 1
176+
3.0,
177+
4.0, // 0
178+
2.0
179+
];
180+
181+
v = gapxsumors( 4, 5.0, x, -2 );
115182

116183
t.strictEqual( v, 25.0, 'returns expected value' );
117184
t.end();
118185
});
119186

120187
tape( 'the function supports a negative `stride` parameter', function test( t ) {
121-
var N;
122188
var x;
123189
var v;
124190

@@ -133,8 +199,7 @@ tape( 'the function supports a negative `stride` parameter', function test( t )
133199
2.0
134200
];
135201

136-
N = floor( x.length / 2 );
137-
v = gapxsumors( N, 5.0, x, -2 );
202+
v = gapxsumors( 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 = gapxsumors( N, 5.0, x1, 2 );
239+
v = gapxsumors( 4, 5.0, x1, 2 );
177240
t.strictEqual( v, 25.0, 'returns expected value' );
178241

179242
t.end();

0 commit comments

Comments
 (0)