Skip to content

Commit cbd656d

Browse files
committed
feat: add blas/ext/base/gnannsumpw
--- 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: passed - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: passed - 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 ---
1 parent 67273e4 commit cbd656d

File tree

15 files changed

+2061
-0
lines changed

15 files changed

+2061
-0
lines changed
Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2025 The Stdlib Authors.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
19+
-->
20+
21+
# gnannsumpw
22+
23+
> Calculate the sum of strided array elements, ignoring `NaN` values and using pairwise summation.
24+
25+
<section class="intro">
26+
27+
</section>
28+
29+
<!-- /.intro -->
30+
31+
<section class="usage">
32+
33+
## Usage
34+
35+
```javascript
36+
var gnannsumpw = require( '@stdlib/blas/ext/base/gnannsumpw' );
37+
```
38+
39+
#### gnannsumpw( N, x, strideX, out, strideOut )
40+
41+
Computes the sum of strided array elements, ignoring `NaN` values and using pairwise summation.
42+
43+
```javascript
44+
var x = [ 1.0, -2.0, NaN, 2.0 ];
45+
var out = [ 0.0, 0 ];
46+
47+
var v = gnannsumpw( x.length, x, 1, out, 1 );
48+
// returns [ 1.0, 3 ]
49+
```
50+
51+
The function has the following parameters:
52+
53+
- **N**: number of indexed elements.
54+
- **x**: input array.
55+
- **strideX**: stride length for `x`.
56+
- **out**: output array whose first element is the sum and whose second element is the number of non-NaN elements.
57+
- **strideOut**: stride length for `out`.
58+
59+
The `N` and stride parameters determine which elements are accessed at runtime. For example, to compute the sum of every other element in the strided array:
60+
61+
```javascript
62+
var x = [ 1.0, 2.0, NaN, -7.0, NaN, 3.0, 4.0, 2.0 ];
63+
var out = [ 0.0, 0 ];
64+
65+
var v = gnannsumpw( 4, x, 2, out, 1 );
66+
// returns [ 5.0, 2 ]
67+
```
68+
69+
Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views.
70+
71+
<!-- eslint-disable stdlib/capitalized-comments -->
72+
73+
```javascript
74+
var Float64Array = require( '@stdlib/array/float64' );
75+
76+
var x0 = new Float64Array( [ 2.0, 1.0, NaN, -2.0, -2.0, 2.0, 3.0, 4.0 ] );
77+
var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
78+
79+
var out0 = new Float64Array( 4 );
80+
var out1 = new Float64Array( out0.buffer, out0.BYTES_PER_ELEMENT*2 ); // start at 3rd element
81+
82+
var v = gnannsumpw( 4, x1, 2, out1, 1 );
83+
// returns <Float64Array>[ 5.0, 4 ]
84+
```
85+
86+
#### gnannsumpw.ndarray( N, x, strideX, offsetX, out, strideOut, offsetOut )
87+
88+
Computes the sum of strided array elements, ignoring `NaN` values and using pairwise summation and alternative indexing semantics.
89+
90+
```javascript
91+
var x = [ 1.0, -2.0, NaN, 2.0 ];
92+
var out = [ 0.0, 0 ];
93+
94+
var v = gnannsumpw.ndarray( x.length, x, 1, 0, out, 1, 0 );
95+
// returns [ 1.0, 3 ]
96+
```
97+
98+
The function has the following additional parameters:
99+
100+
- **offsetX**: starting index for `x`.
101+
- **offsetOut**: starting index for `out`.
102+
103+
While [`typed array`][mdn-typed-array] views mandate a view offset based on the underlying buffer, the offset parameters support indexing semantics based on starting indices. For example, to calculate the sum of every other element starting from the second element:
104+
105+
```javascript
106+
var x = [ 2.0, 1.0, NaN, -2.0, -2.0, 2.0, 3.0, 4.0 ];
107+
var out = [ 0.0, 0.0, 0.0, 0.0 ];
108+
109+
var v = gnannsumpw.ndarray( 4, x, 2, 1, out, 2, 1 );
110+
// returns [ 0.0, 5.0, 0.0, 4.0 ]
111+
```
112+
113+
</section>
114+
115+
<!-- /.usage -->
116+
117+
<section class="notes">
118+
119+
## Notes
120+
121+
- If `N <= 0`, both functions return a sum equal to `0.0`.
122+
- 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.
123+
- 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]).
124+
- Depending on the environment, the typed versions ([`dnannsumpw`][@stdlib/blas/ext/base/dnannsumpw], [`snannsumpw`][@stdlib/blas/ext/base/snannsumpw], etc.) are likely to be significantly more performant.
125+
126+
</section>
127+
128+
<!-- /.notes -->
129+
130+
<section class="examples">
131+
132+
## Examples
133+
134+
<!-- eslint no-undef: "error" -->
135+
136+
```javascript
137+
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' );
138+
var bernoulli = require( '@stdlib/random/base/bernoulli' );
139+
var filledarrayBy = require( '@stdlib/array/filled-by' );
140+
var gnannsumpw = require( '@stdlib/blas/ext/base/gnannsumpw' );
141+
142+
function rand() {
143+
if ( bernoulli( 0.5 ) < 1 ) {
144+
return discreteUniform( 0, 100 );
145+
}
146+
return NaN;
147+
}
148+
149+
var x = filledarrayBy( 10, 'generic', rand );
150+
console.log( x );
151+
152+
var out = [ 0.0, 0 ];
153+
gnannsumpw( x.length, x, 1, out, 1 );
154+
console.log( out );
155+
```
156+
157+
</section>
158+
159+
<!-- /.examples -->
160+
161+
<section class="references">
162+
163+
## References
164+
165+
- Higham, Nicholas J. 1993. "The Accuracy of Floating Point Summation." _SIAM Journal on Scientific Computing_ 14 (4): 783–99. doi:[10.1137/0914050][@higham:1993a].
166+
167+
</section>
168+
169+
<!-- /.references -->
170+
171+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
172+
173+
<section class="related">
174+
175+
</section>
176+
177+
<!-- /.related -->
178+
179+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
180+
181+
<section class="links">
182+
183+
[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
184+
185+
[@higham:1993a]: https://doi.org/10.1137/0914050
186+
187+
[@stdlib/array/base/accessor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/base/accessor
188+
189+
[@stdlib/blas/ext/base/dnannsumpw]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/blas/ext/base/dnannsumpw
190+
191+
[@stdlib/blas/ext/base/snannsumpw]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/blas/ext/base/snannsumpw
192+
193+
</section>
194+
195+
<!-- /.links -->
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
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 bench = require( '@stdlib/bench' );
24+
var uniform = require( '@stdlib/random/base/uniform' );
25+
var bernoulli = require( '@stdlib/random/base/bernoulli' );
26+
var filledarrayBy = require( '@stdlib/array/filled-by' );
27+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
28+
var pow = require( '@stdlib/math/base/special/pow' );
29+
var pkg = require( './../package.json' ).name;
30+
var gnannsumpw = require( './../lib' );
31+
32+
33+
// FUNCTIONS //
34+
35+
/**
36+
* Returns a random number.
37+
*
38+
* @private
39+
* @returns {number} random number
40+
*/
41+
function rand() {
42+
if ( bernoulli( 0.5 ) < 1 ) {
43+
return uniform( -10.0, 10.0 );
44+
}
45+
return NaN;
46+
}
47+
48+
/**
49+
* Creates a benchmark function.
50+
*
51+
* @private
52+
* @param {PositiveInteger} len - array length
53+
* @returns {Function} benchmark function
54+
*/
55+
function createBenchmark( len ) {
56+
var out;
57+
var x;
58+
59+
x = filledarrayBy( len, 'generic', rand );
60+
out = [ 0.0, 0.0 ];
61+
return benchmark;
62+
63+
function benchmark( b ) {
64+
var i;
65+
66+
b.tic();
67+
for ( i = 0; i < b.iterations; i++ ) {
68+
gnannsumpw( x.length, x, 1, out, 1 );
69+
if ( isnan( out[ i%2 ] ) ) {
70+
b.fail( 'should not return NaN' );
71+
}
72+
}
73+
b.toc();
74+
if ( isnan( out[ i%2 ] ) ) {
75+
b.fail( 'should not return NaN' );
76+
}
77+
b.pass( 'benchmark finished' );
78+
b.end();
79+
}
80+
}
81+
82+
83+
// MAIN //
84+
85+
/**
86+
* Main execution sequence.
87+
*
88+
* @private
89+
*/
90+
function main() {
91+
var len;
92+
var min;
93+
var max;
94+
var f;
95+
var i;
96+
97+
min = 1; // 10^min
98+
max = 6; // 10^max
99+
100+
for ( i = min; i <= max; i++ ) {
101+
len = pow( 10, i );
102+
f = createBenchmark( len );
103+
bench( pkg+':len='+len, f );
104+
}
105+
}
106+
107+
main();

0 commit comments

Comments
 (0)