Skip to content

Commit f78899e

Browse files
authored
feat: add accessor arrays support to blas/ext/base/gnansumpw
PR-URL: #5037 Reviewed-by: Athan Reines <[email protected]>
1 parent 6af5789 commit f78899e

File tree

7 files changed

+478
-3
lines changed

7 files changed

+478
-3
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ var v = gnansumpw.ndarray( 5, 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
- In general, pairwise summation is more numerically stable than ordinary recursive summation (i.e., "simple" summation), with slightly worse performance. While not the most numerically stable summation technique (e.g., compensated summation techniques such as the Kahan–Babuška-Neumaier algorithm are generally more numerically stable), pairwise summation strikes a reasonable balance between numerical stability and performance. If either numerical stability or performance is more desirable for your use case, consider alternative summation techniques.
113114
- Depending on the environment, the typed versions ([`dnansumpw`][@stdlib/blas/ext/base/dnansumpw], [`snansumpw`][@stdlib/blas/ext/base/snansumpw], etc.) are likely to be significantly more performant.
114115

@@ -185,6 +186,8 @@ console.log( v );
185186

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

189+
[@stdlib/array/base/accessor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/base/accessor
190+
188191
[@higham:1993a]: https://doi.org/10.1137/0914050
189192

190193
<!-- <related-links> -->

lib/node_modules/@stdlib/blas/ext/base/gnansumpw/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 `gnansumpw`.
@@ -40,7 +45,7 @@ interface Routine {
4045
* var v = gnansumpw( 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, ignoring `NaN` values and using pairwise summation and alternative indexing semantics.
@@ -57,7 +62,7 @@ interface Routine {
5762
* var v = gnansumpw.ndarray( x.length, x, 1, 0 );
5863
* // returns 1.0
5964
*/
60-
ndarray( N: number, x: NumericArray, strideX: number, offsetX: number ): number;
65+
ndarray( N: number, x: InputArray, strideX: number, offsetX: number ): number;
6166
}
6267

6368
/**

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

2122

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

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

8789
gnansumpw.ndarray( x.length, x, 1, 0 ); // $ExpectType number
90+
gnansumpw.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...
Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
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 isnan = require( '@stdlib/math/base/assert/is-nan' );
24+
var floor = require( '@stdlib/math/base/special/floor' );
25+
26+
27+
// VARIABLES //
28+
29+
// Blocksize for pairwise summation (NOTE: decreasing the blocksize decreases rounding error as more pairs are summed, but also decreases performance. Because the inner loop is unrolled eight times, the blocksize is effectively `16`.):
30+
var BLOCKSIZE = 128;
31+
32+
33+
// MAIN //
34+
35+
/**
36+
* Computes the sum of strided array elements, ignoring `NaN` values and using pairwise summation.
37+
*
38+
* ## Method
39+
*
40+
* - This implementation uses pairwise summation, which accrues rounding error `O(log2 N)` instead of `O(N)`. The recursion depth is also `O(log2 N)`.
41+
*
42+
* ## References
43+
*
44+
* - Higham, Nicholas J. 1993. "The Accuracy of Floating Point Summation." _SIAM Journal on Scientific Computing_ 14 (4): 783–99. doi:[10.1137/0914050](https://doi.org/10.1137/0914050).
45+
*
46+
* @private
47+
* @param {PositiveInteger} N - number of indexed elements
48+
* @param {Object} x - input array object
49+
* @param {Collection} x.data - input array data
50+
* @param {Array<Function>} x.accessors - array element accessors
51+
* @param {integer} strideX - stride length for `x`
52+
* @param {NonNegativeInteger} offsetX - starting index for `x`
53+
* @returns {number} sum
54+
*
55+
* @example
56+
* var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' );
57+
* var arraylike2object = require( '@stdlib/array/base/arraylike2object' );
58+
*
59+
* var x = toAccessorArray( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ] );
60+
*
61+
* var v = gnansumpw( 4, arraylike2object( x ), 2, 1 );
62+
* // returns 5.0
63+
*/
64+
function gnansumpw( N, x, strideX, offsetX ) {
65+
var xbuf;
66+
var xget;
67+
var ix;
68+
var s0;
69+
var s1;
70+
var s2;
71+
var s3;
72+
var s4;
73+
var s5;
74+
var s6;
75+
var s7;
76+
var M;
77+
var s;
78+
var n;
79+
var v;
80+
var i;
81+
82+
// Cache reference to array data:
83+
xbuf = x.data;
84+
85+
// Cache reference to the element accessors:
86+
xget = x.accessors[ 0 ];
87+
88+
ix = offsetX;
89+
if ( strideX === 0 ) {
90+
v = xget( xbuf, ix );
91+
if ( isnan( v ) ) {
92+
return 0.0;
93+
}
94+
return N * v;
95+
}
96+
if ( N < 8 ) {
97+
// Use simple summation...
98+
s = 0.0;
99+
for ( i = 0; i < N; i++ ) {
100+
v = xget( xbuf, ix );
101+
if ( isnan( v ) === false ) {
102+
s += v;
103+
}
104+
ix += strideX;
105+
}
106+
return s;
107+
}
108+
if ( N <= BLOCKSIZE ) {
109+
// Sum a block with 8 accumulators (by loop unrolling, we lower the effective blocksize to 16)...
110+
v = xget( xbuf, ix );
111+
s0 = ( isnan( v ) ) ? 0.0 : v;
112+
ix += strideX;
113+
v = xget( xbuf, ix );
114+
s1 = ( isnan( v ) ) ? 0.0 : v;
115+
ix += strideX;
116+
v = xget( xbuf, ix );
117+
s2 = ( isnan( v ) ) ? 0.0 : v;
118+
ix += strideX;
119+
v = xget( xbuf, ix );
120+
s3 = ( isnan( v ) ) ? 0.0 : v;
121+
ix += strideX;
122+
v = xget( xbuf, ix );
123+
s4 = ( isnan( v ) ) ? 0.0 : v;
124+
ix += strideX;
125+
v = xget( xbuf, ix );
126+
s5 = ( isnan( v ) ) ? 0.0 : v;
127+
ix += strideX;
128+
v = xget( xbuf, ix );
129+
s6 = ( isnan( v ) ) ? 0.0 : v;
130+
ix += strideX;
131+
v = xget( xbuf, ix );
132+
s7 = ( isnan( v ) ) ? 0.0 : v;
133+
ix += strideX;
134+
135+
M = N % 8;
136+
for ( i = 8; i < N-M; i += 8 ) {
137+
v = xget( xbuf, ix );
138+
s0 += ( isnan( v ) ) ? 0.0 : v;
139+
ix += strideX;
140+
v = xget( xbuf, ix );
141+
s1 += ( isnan( v ) ) ? 0.0 : v;
142+
ix += strideX;
143+
v = xget( xbuf, ix );
144+
s2 += ( isnan( v ) ) ? 0.0 : v;
145+
ix += strideX;
146+
v = xget( xbuf, ix );
147+
s3 += ( isnan( v ) ) ? 0.0 : v;
148+
ix += strideX;
149+
v = xget( xbuf, ix );
150+
s4 += ( isnan( v ) ) ? 0.0 : v;
151+
ix += strideX;
152+
v = xget( xbuf, ix );
153+
s5 += ( isnan( v ) ) ? 0.0 : v;
154+
ix += strideX;
155+
v = xget( xbuf, ix );
156+
s6 += ( isnan( v ) ) ? 0.0 : v;
157+
ix += strideX;
158+
v = xget( xbuf, ix );
159+
s7 += ( isnan( v ) ) ? 0.0 : v;
160+
ix += strideX;
161+
}
162+
// Pairwise sum the accumulators:
163+
s = ( (s0+s1) + (s2+s3) ) + ( (s4+s5) + (s6+s7) );
164+
165+
// Clean-up loop...
166+
for ( i; i < N; i++ ) {
167+
v = xget( xbuf, ix );
168+
if ( isnan( v ) === false ) {
169+
s += v;
170+
}
171+
ix += strideX;
172+
}
173+
return s;
174+
}
175+
// Recurse by dividing by two, but avoiding non-multiples of unroll factor...
176+
n = floor( N/2 );
177+
n -= n % 8;
178+
return gnansumpw( n, x, strideX, ix ) + gnansumpw( N-n, x, strideX, ix+(n*strideX) ); // eslint-disable-line max-len
179+
}
180+
181+
182+
// EXPORTS //
183+
184+
module.exports = gnansumpw;

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

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

2121
// MODULES //
2222

23+
var arraylike2object = require( '@stdlib/array/base/arraylike2object' );
2324
var isnan = require( '@stdlib/math/base/assert/is-nan' );
2425
var floor = require( '@stdlib/math/base/special/floor' );
26+
var accessors = require( './accessors.js' );
2527

2628

2729
// VARIABLES //
@@ -68,11 +70,16 @@ function gnansumpw( N, x, strideX, offsetX ) {
6870
var M;
6971
var s;
7072
var n;
73+
var o;
7174
var i;
7275

7376
if ( N <= 0 ) {
7477
return 0.0;
7578
}
79+
o = arraylike2object( x );
80+
if ( o.accessorProtocol ) {
81+
return accessors( N, o, strideX, offsetX );
82+
}
7683
ix = offsetX;
7784
if ( strideX === 0 ) {
7885
if ( isnan( x[ ix ] ) ) {

0 commit comments

Comments
 (0)