Skip to content

Commit db837d8

Browse files
headlessNodekgryte
authored andcommitted
feat: add accessor arrays support to blas/ext/base/gapxsumors
PR-URL: stdlib-js#4930 Co-authored-by: Athan Reines <[email protected]> Reviewed-by: Athan Reines <[email protected]>
1 parent d0871cf commit db837d8

File tree

7 files changed

+289
-23
lines changed

7 files changed

+289
-23
lines changed

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

+3
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

+8-3
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

+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 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...
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

+11
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

+85-10
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,34 @@ 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 );
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 = gapxsumors( N, 5.0, x, -2 );
181+
v = gapxsumors( 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', 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 = gapxsumors( 4, 5.0, toAccessorArray( x ), -2 );
138203

139204
t.strictEqual( v, 25.0, 'returns expected value' );
140205
t.end();
@@ -152,10 +217,21 @@ tape( 'if provided a `stride` parameter equal to `0`, the function returns the f
152217
t.end();
153218
});
154219

220+
tape( 'if provided a `stride` parameter equal to `0`, the function returns the first element plus a constant repeated N times (accessors)', function test( t ) {
221+
var x;
222+
var v;
223+
224+
x = [ 1.0, -2.0, -4.0, 5.0, 3.0 ];
225+
226+
v = gapxsumors( x.length, 5.0, toAccessorArray( x ), 0 );
227+
t.strictEqual( v, x.length * (x[0]+5.0), 'returns expected value' );
228+
229+
t.end();
230+
});
231+
155232
tape( 'the function supports view offsets', function test( t ) {
156233
var x0;
157234
var x1;
158-
var N;
159235
var v;
160236

161237
x0 = new Float64Array([
@@ -171,9 +247,8 @@ tape( 'the function supports view offsets', function test( t ) {
171247
]);
172248

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

176-
v = gapxsumors( N, 5.0, x1, 2 );
251+
v = gapxsumors( 4, 5.0, x1, 2 );
177252
t.strictEqual( v, 25.0, 'returns expected value' );
178253

179254
t.end();

0 commit comments

Comments
 (0)