@stdlib/blas-ext-base-gnansumpw 0.2.1 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/NOTICE +1 -1
- package/README.md +28 -39
- package/dist/index.js +8 -5
- package/dist/index.js.map +4 -4
- package/docs/types/index.d.ts +12 -7
- package/lib/accessors.js +184 -0
- package/lib/index.js +2 -5
- package/lib/main.js +6 -36
- package/lib/ndarray.js +35 -31
- package/package.json +6 -5
package/NOTICE
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
Copyright (c) 2016-
|
|
1
|
+
Copyright (c) 2016-2026 The Stdlib Authors.
|
package/README.md
CHANGED
|
@@ -59,15 +59,14 @@ npm install @stdlib/blas-ext-base-gnansumpw
|
|
|
59
59
|
var gnansumpw = require( '@stdlib/blas-ext-base-gnansumpw' );
|
|
60
60
|
```
|
|
61
61
|
|
|
62
|
-
#### gnansumpw( N, x,
|
|
62
|
+
#### gnansumpw( N, x, strideX )
|
|
63
63
|
|
|
64
64
|
Computes the sum of strided array elements, ignoring `NaN` values and using pairwise summation.
|
|
65
65
|
|
|
66
66
|
```javascript
|
|
67
67
|
var x = [ 1.0, -2.0, NaN, 2.0 ];
|
|
68
|
-
var N = x.length;
|
|
69
68
|
|
|
70
|
-
var v = gnansumpw(
|
|
69
|
+
var v = gnansumpw( x.length, x, 1 );
|
|
71
70
|
// returns 1.0
|
|
72
71
|
```
|
|
73
72
|
|
|
@@ -75,17 +74,14 @@ The function has the following parameters:
|
|
|
75
74
|
|
|
76
75
|
- **N**: number of indexed elements.
|
|
77
76
|
- **x**: input [`Array`][mdn-array] or [`typed array`][mdn-typed-array].
|
|
78
|
-
- **
|
|
77
|
+
- **strideX**: stride length for `x`.
|
|
79
78
|
|
|
80
|
-
The `N` and
|
|
79
|
+
The `N` and stride parameters determine which elements in the strided array are accessed at runtime. For example, to compute the sum of every other element:
|
|
81
80
|
|
|
82
81
|
```javascript
|
|
83
|
-
var floor = require( '@stdlib/math-base-special-floor' );
|
|
84
|
-
|
|
85
82
|
var x = [ 1.0, 2.0, 2.0, -7.0, -2.0, 3.0, 4.0, 2.0, NaN, NaN ];
|
|
86
|
-
var N = floor( x.length / 2 );
|
|
87
83
|
|
|
88
|
-
var v = gnansumpw(
|
|
84
|
+
var v = gnansumpw( 5, x, 2 );
|
|
89
85
|
// returns 5.0
|
|
90
86
|
```
|
|
91
87
|
|
|
@@ -95,42 +91,35 @@ Note that indexing is relative to the first index. To introduce an offset, use [
|
|
|
95
91
|
|
|
96
92
|
```javascript
|
|
97
93
|
var Float64Array = require( '@stdlib/array-float64' );
|
|
98
|
-
var floor = require( '@stdlib/math-base-special-floor' );
|
|
99
94
|
|
|
100
95
|
var x0 = new Float64Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ] );
|
|
101
96
|
var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
|
|
102
97
|
|
|
103
|
-
var
|
|
104
|
-
|
|
105
|
-
var v = gnansumpw( N, x1, 2 );
|
|
98
|
+
var v = gnansumpw( 4, x1, 2 );
|
|
106
99
|
// returns 5.0
|
|
107
100
|
```
|
|
108
101
|
|
|
109
|
-
#### gnansumpw.ndarray( N, x,
|
|
102
|
+
#### gnansumpw.ndarray( N, x, strideX, offsetX )
|
|
110
103
|
|
|
111
104
|
Computes the sum of strided array elements, ignoring `NaN` values and using pairwise summation and alternative indexing semantics.
|
|
112
105
|
|
|
113
106
|
```javascript
|
|
114
107
|
var x = [ 1.0, -2.0, NaN, 2.0 ];
|
|
115
|
-
var N = x.length;
|
|
116
108
|
|
|
117
|
-
var v = gnansumpw.ndarray(
|
|
109
|
+
var v = gnansumpw.ndarray( x.length, x, 1, 0 );
|
|
118
110
|
// returns 1.0
|
|
119
111
|
```
|
|
120
112
|
|
|
121
113
|
The function has the following additional parameters:
|
|
122
114
|
|
|
123
|
-
- **
|
|
115
|
+
- **offsetX**: starting index for `x`.
|
|
124
116
|
|
|
125
|
-
While [`typed array`][mdn-typed-array] views mandate a view offset based on the underlying
|
|
117
|
+
While [`typed array`][mdn-typed-array] views mandate a view offset based on the underlying buffer, the offset parameter supports indexing semantics based on a starting index. For example, to calculate the sum of every other element starting from the second element:
|
|
126
118
|
|
|
127
119
|
```javascript
|
|
128
|
-
var floor = require( '@stdlib/math-base-special-floor' );
|
|
129
|
-
|
|
130
120
|
var x = [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0, NaN, NaN ];
|
|
131
|
-
var N = floor( x.length / 2 );
|
|
132
121
|
|
|
133
|
-
var v = gnansumpw.ndarray(
|
|
122
|
+
var v = gnansumpw.ndarray( 5, x, 2, 1 );
|
|
134
123
|
// returns 5.0
|
|
135
124
|
```
|
|
136
125
|
|
|
@@ -143,6 +132,7 @@ var v = gnansumpw.ndarray( N, x, 2, 1 );
|
|
|
143
132
|
## Notes
|
|
144
133
|
|
|
145
134
|
- If `N <= 0`, both functions return `0.0`.
|
|
135
|
+
- 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]).
|
|
146
136
|
- 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.
|
|
147
137
|
- 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.
|
|
148
138
|
|
|
@@ -157,22 +147,19 @@ var v = gnansumpw.ndarray( N, x, 2, 1 );
|
|
|
157
147
|
<!-- eslint no-undef: "error" -->
|
|
158
148
|
|
|
159
149
|
```javascript
|
|
160
|
-
var
|
|
161
|
-
var
|
|
162
|
-
var
|
|
150
|
+
var discreteUniform = require( '@stdlib/random-base-discrete-uniform' );
|
|
151
|
+
var bernoulli = require( '@stdlib/random-base-bernoulli' );
|
|
152
|
+
var filledarrayBy = require( '@stdlib/array-filled-by' );
|
|
163
153
|
var gnansumpw = require( '@stdlib/blas-ext-base-gnansumpw' );
|
|
164
154
|
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
x = new Float64Array( 10 );
|
|
169
|
-
for ( i = 0; i < x.length; i++ ) {
|
|
170
|
-
if ( randu() < 0.2 ) {
|
|
171
|
-
x[ i ] = NaN;
|
|
172
|
-
} else {
|
|
173
|
-
x[ i ] = round( randu()*100.0 );
|
|
155
|
+
function rand() {
|
|
156
|
+
if ( bernoulli( 0.7 ) > 0 ) {
|
|
157
|
+
return discreteUniform( 0, 100 );
|
|
174
158
|
}
|
|
159
|
+
return NaN;
|
|
175
160
|
}
|
|
161
|
+
|
|
162
|
+
var x = filledarrayBy( 10, 'float64', rand );
|
|
176
163
|
console.log( x );
|
|
177
164
|
|
|
178
165
|
var v = gnansumpw( x.length, x, 1 );
|
|
@@ -240,7 +227,7 @@ See [LICENSE][stdlib-license].
|
|
|
240
227
|
|
|
241
228
|
## Copyright
|
|
242
229
|
|
|
243
|
-
Copyright © 2016-
|
|
230
|
+
Copyright © 2016-2026. The Stdlib [Authors][stdlib-authors].
|
|
244
231
|
|
|
245
232
|
</section>
|
|
246
233
|
|
|
@@ -253,8 +240,8 @@ Copyright © 2016-2024. The Stdlib [Authors][stdlib-authors].
|
|
|
253
240
|
[npm-image]: http://img.shields.io/npm/v/@stdlib/blas-ext-base-gnansumpw.svg
|
|
254
241
|
[npm-url]: https://npmjs.org/package/@stdlib/blas-ext-base-gnansumpw
|
|
255
242
|
|
|
256
|
-
[test-image]: https://github.com/stdlib-js/blas-ext-base-gnansumpw/actions/workflows/test.yml/badge.svg?branch=v0.
|
|
257
|
-
[test-url]: https://github.com/stdlib-js/blas-ext-base-gnansumpw/actions/workflows/test.yml?query=branch:v0.
|
|
243
|
+
[test-image]: https://github.com/stdlib-js/blas-ext-base-gnansumpw/actions/workflows/test.yml/badge.svg?branch=v0.3.0
|
|
244
|
+
[test-url]: https://github.com/stdlib-js/blas-ext-base-gnansumpw/actions/workflows/test.yml?query=branch:v0.3.0
|
|
258
245
|
|
|
259
246
|
[coverage-image]: https://img.shields.io/codecov/c/github/stdlib-js/blas-ext-base-gnansumpw/main.svg
|
|
260
247
|
[coverage-url]: https://codecov.io/github/stdlib-js/blas-ext-base-gnansumpw?branch=main
|
|
@@ -266,8 +253,8 @@ Copyright © 2016-2024. The Stdlib [Authors][stdlib-authors].
|
|
|
266
253
|
|
|
267
254
|
-->
|
|
268
255
|
|
|
269
|
-
[chat-image]: https://img.shields.io/
|
|
270
|
-
[chat-url]: https://
|
|
256
|
+
[chat-image]: https://img.shields.io/badge/zulip-join_chat-brightgreen.svg
|
|
257
|
+
[chat-url]: https://stdlib.zulipchat.com
|
|
271
258
|
|
|
272
259
|
[stdlib]: https://github.com/stdlib-js/stdlib
|
|
273
260
|
|
|
@@ -290,6 +277,8 @@ Copyright © 2016-2024. The Stdlib [Authors][stdlib-authors].
|
|
|
290
277
|
|
|
291
278
|
[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
|
|
292
279
|
|
|
280
|
+
[@stdlib/array/base/accessor]: https://www.npmjs.com/package/@stdlib/array-base-accessor
|
|
281
|
+
|
|
293
282
|
[@higham:1993a]: https://doi.org/10.1137/0914050
|
|
294
283
|
|
|
295
284
|
<!-- <related-links> -->
|
package/dist/index.js
CHANGED
|
@@ -1,7 +1,10 @@
|
|
|
1
|
-
"use strict";var
|
|
2
|
-
var
|
|
3
|
-
});var
|
|
4
|
-
var
|
|
5
|
-
});var
|
|
1
|
+
"use strict";var I=function(f,v){return function(){return v||f((v={exports:{}}).exports,v),v.exports}};var Z=I(function(W,S){
|
|
2
|
+
var c=require('@stdlib/math-base-assert-is-nan/dist'),h=require('@stdlib/math-base-special-floor/dist'),z=128;function K(f,v,n,E){var r,o,u,g,m,w,O,b,B,C,l,s,p,q,a,y;if(r=v.data,o=v.accessors[0],u=E,n===0)return a=o(r,u),c(a)?0:f*a;if(f<8){for(p=0,y=0;y<f;y++)a=o(r,u),c(a)===!1&&(p+=a),u+=n;return p}if(f<=z){for(a=o(r,u),g=c(a)?0:a,u+=n,a=o(r,u),m=c(a)?0:a,u+=n,a=o(r,u),w=c(a)?0:a,u+=n,a=o(r,u),O=c(a)?0:a,u+=n,a=o(r,u),b=c(a)?0:a,u+=n,a=o(r,u),B=c(a)?0:a,u+=n,a=o(r,u),C=c(a)?0:a,u+=n,a=o(r,u),l=c(a)?0:a,u+=n,s=f%8,y=8;y<f-s;y+=8)a=o(r,u),g+=c(a)?0:a,u+=n,a=o(r,u),m+=c(a)?0:a,u+=n,a=o(r,u),w+=c(a)?0:a,u+=n,a=o(r,u),O+=c(a)?0:a,u+=n,a=o(r,u),b+=c(a)?0:a,u+=n,a=o(r,u),B+=c(a)?0:a,u+=n,a=o(r,u),C+=c(a)?0:a,u+=n,a=o(r,u),l+=c(a)?0:a,u+=n;for(p=g+m+(w+O)+(b+B+(C+l)),y;y<f;y++)a=o(r,u),c(a)===!1&&(p+=a),u+=n;return p}return q=h(f/2),q-=q%8,K(q,v,n,u)+K(f-q,v,n,u+q*n)}S.exports=K
|
|
3
|
+
});var M=I(function(Y,j){
|
|
4
|
+
var A=require('@stdlib/array-base-arraylike2object/dist'),e=require('@stdlib/math-base-assert-is-nan/dist'),D=require('@stdlib/math-base-special-floor/dist'),F=Z(),G=128;function L(f,v,n,E){var r,o,u,g,m,w,O,b,B,C,l,s,p,q;if(f<=0)return 0;if(p=A(v),p.accessorProtocol)return F(f,p,n,E);if(r=E,n===0)return e(v[r])?0:f*v[r];if(f<8){for(l=0,q=0;q<f;q++)e(v[r])===!1&&(l+=v[r]),r+=n;return l}if(f<=G){for(o=e(v[r])?0:v[r],r+=n,u=e(v[r])?0:v[r],r+=n,g=e(v[r])?0:v[r],r+=n,m=e(v[r])?0:v[r],r+=n,w=e(v[r])?0:v[r],r+=n,O=e(v[r])?0:v[r],r+=n,b=e(v[r])?0:v[r],r+=n,B=e(v[r])?0:v[r],r+=n,C=f%8,q=8;q<f-C;q+=8)o+=e(v[r])?0:v[r],r+=n,u+=e(v[r])?0:v[r],r+=n,g+=e(v[r])?0:v[r],r+=n,m+=e(v[r])?0:v[r],r+=n,w+=e(v[r])?0:v[r],r+=n,O+=e(v[r])?0:v[r],r+=n,b+=e(v[r])?0:v[r],r+=n,B+=e(v[r])?0:v[r],r+=n;for(l=o+u+(g+m)+(w+O+(b+B)),q;q<f;q++)e(v[r])===!1&&(l+=v[r]),r+=n;return l}return s=D(f/2),s-=s%8,L(s,v,n,r)+L(f-s,v,n,r+s*n)}j.exports=L
|
|
5
|
+
});var P=I(function(_,k){
|
|
6
|
+
var H=require('@stdlib/strided-base-stride2offset/dist'),J=M();function Q(f,v,n){return J(f,v,n,H(f,n))}k.exports=Q
|
|
7
|
+
});var T=require('@stdlib/utils-define-nonenumerable-read-only-property/dist'),R=P(),U=M();T(R,"ndarray",U);module.exports=R;
|
|
8
|
+
/** @license Apache-2.0 */
|
|
6
9
|
/** @license Apache-2.0 */
|
|
7
10
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
|
-
"sources": ["../lib/ndarray.js", "../lib/main.js", "../lib/index.js"],
|
|
4
|
-
"sourcesContent": ["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2020 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar isnan = require( '@stdlib/math-base-assert-is-nan' );\nvar floor = require( '@stdlib/math-base-special-floor' );\n\n\n// VARIABLES //\n\n// 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`.):\nvar BLOCKSIZE = 128;\n\n\n// MAIN //\n\n/**\n* Computes the sum of strided array elements, ignoring `NaN` values and using pairwise summation.\n*\n* ## Method\n*\n* - This implementation uses pairwise summation, which accrues rounding error `O(log2 N)` instead of `O(N)`. The recursion depth is also `O(log2 N)`.\n*\n* ## References\n*\n* - Higham, Nicholas J. 1993. \"The Accuracy of Floating Point Summation.\" _SIAM Journal on Scientific Computing_ 14 (4): 783\u201399. doi:[10.1137/0914050](https://doi.org/10.1137/0914050).\n*\n* @param {PositiveInteger} N - number of indexed elements\n* @param {NumericArray} x - input array\n* @param {integer} stride - stride length\n* @param {NonNegativeInteger} offset - starting index\n* @returns {number} sum\n*\n* @example\n* var floor = require( '@stdlib/math-base-special-floor' );\n*\n* var x = [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ];\n* var N = floor( x.length / 2 );\n*\n* var v = gnansumpw( N, x, 2, 1 );\n* // returns 5.0\n*/\nfunction gnansumpw( N, x, stride, offset ) {\n\tvar ix;\n\tvar s0;\n\tvar s1;\n\tvar s2;\n\tvar s3;\n\tvar s4;\n\tvar s5;\n\tvar s6;\n\tvar s7;\n\tvar M;\n\tvar s;\n\tvar n;\n\tvar i;\n\n\tif ( N <= 0 ) {\n\t\treturn 0.0;\n\t}\n\tif ( N === 1 || stride === 0 ) {\n\t\tif ( isnan( x[ offset ] ) ) {\n\t\t\treturn 0.0;\n\t\t}\n\t\treturn x[ offset ];\n\t}\n\tix = offset;\n\tif ( N < 8 ) {\n\t\t// Use simple summation...\n\t\ts = 0.0;\n\t\tfor ( i = 0; i < N; i++ ) {\n\t\t\tif ( isnan( x[ ix ] ) === false ) {\n\t\t\t\ts += x[ ix ];\n\t\t\t}\n\t\t\tix += stride;\n\t\t}\n\t\treturn s;\n\t}\n\tif ( N <= BLOCKSIZE ) {\n\t\t// Sum a block with 8 accumulators (by loop unrolling, we lower the effective blocksize to 16)...\n\t\ts0 = ( isnan( x[ ix ] ) ) ? 0.0 : x[ ix ];\n\t\tix += stride;\n\t\ts1 = ( isnan( x[ ix ] ) ) ? 0.0 : x[ ix ];\n\t\tix += stride;\n\t\ts2 = ( isnan( x[ ix ] ) ) ? 0.0 : x[ ix ];\n\t\tix += stride;\n\t\ts3 = ( isnan( x[ ix ] ) ) ? 0.0 : x[ ix ];\n\t\tix += stride;\n\t\ts4 = ( isnan( x[ ix ] ) ) ? 0.0 : x[ ix ];\n\t\tix += stride;\n\t\ts5 = ( isnan( x[ ix ] ) ) ? 0.0 : x[ ix ];\n\t\tix += stride;\n\t\ts6 = ( isnan( x[ ix ] ) ) ? 0.0 : x[ ix ];\n\t\tix += stride;\n\t\ts7 = ( isnan( x[ ix ] ) ) ? 0.0 : x[ ix ];\n\t\tix += stride;\n\n\t\tM = N % 8;\n\t\tfor ( i = 8; i < N-M; i += 8 ) {\n\t\t\ts0 += ( isnan( x[ ix ] ) ) ? 0.0 : x[ ix ];\n\t\t\tix += stride;\n\t\t\ts1 += ( isnan( x[ ix ] ) ) ? 0.0 : x[ ix ];\n\t\t\tix += stride;\n\t\t\ts2 += ( isnan( x[ ix ] ) ) ? 0.0 : x[ ix ];\n\t\t\tix += stride;\n\t\t\ts3 += ( isnan( x[ ix ] ) ) ? 0.0 : x[ ix ];\n\t\t\tix += stride;\n\t\t\ts4 += ( isnan( x[ ix ] ) ) ? 0.0 : x[ ix ];\n\t\t\tix += stride;\n\t\t\ts5 += ( isnan( x[ ix ] ) ) ? 0.0 : x[ ix ];\n\t\t\tix += stride;\n\t\t\ts6 += ( isnan( x[ ix ] ) ) ? 0.0 : x[ ix ];\n\t\t\tix += stride;\n\t\t\ts7 += ( isnan( x[ ix ] ) ) ? 0.0 : x[ ix ];\n\t\t\tix += stride;\n\t\t}\n\t\t// Pairwise sum the accumulators:\n\t\ts = ((s0+s1) + (s2+s3)) + ((s4+s5) + (s6+s7));\n\n\t\t// Clean-up loop...\n\t\tfor ( i; i < N; i++ ) {\n\t\t\tif ( isnan( x[ ix ] ) === false ) {\n\t\t\t\ts += x[ ix ];\n\t\t\t}\n\t\t\tix += stride;\n\t\t}\n\t\treturn s;\n\t}\n\t// Recurse by dividing by two, but avoiding non-multiples of unroll factor...\n\tn = floor( N/2 );\n\tn -= n % 8;\n\treturn gnansumpw( n, x, stride, ix ) + gnansumpw( N-n, x, stride, ix+(n*stride) ); // eslint-disable-line max-len\n}\n\n\n// EXPORTS //\n\nmodule.exports = gnansumpw;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2020 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar isnan = require( '@stdlib/math-base-assert-is-nan' );\nvar sum = require( './ndarray.js' );\n\n\n// MAIN //\n\n/**\n* Computes the sum of strided array elements, ignoring `NaN` values and using pairwise summation.\n*\n* ## Method\n*\n* - This implementation uses pairwise summation, which accrues rounding error `O(log2 N)` instead of `O(N)`. The recursion depth is also `O(log2 N)`.\n*\n* ## References\n*\n* - Higham, Nicholas J. 1993. \"The Accuracy of Floating Point Summation.\" _SIAM Journal on Scientific Computing_ 14 (4): 783\u201399. doi:[10.1137/0914050](https://doi.org/10.1137/0914050).\n*\n* @param {PositiveInteger} N - number of indexed elements\n* @param {NumericArray} x - input array\n* @param {integer} stride - stride length\n* @returns {number} sum\n*\n* @example\n* var x = [ 1.0, -2.0, NaN, 2.0 ];\n* var N = x.length;\n*\n* var v = gnansumpw( N, x, 1 );\n* // returns 1.0\n*/\nfunction gnansumpw( N, x, stride ) {\n\tvar ix;\n\tvar s;\n\tvar i;\n\n\tif ( N <= 0 ) {\n\t\treturn 0.0;\n\t}\n\tif ( N === 1 || stride === 0 ) {\n\t\tif ( isnan( x[ 0 ] ) ) {\n\t\t\treturn 0.0;\n\t\t}\n\t\treturn x[ 0 ];\n\t}\n\tif ( stride < 0 ) {\n\t\tix = (1-N) * stride;\n\t} else {\n\t\tix = 0;\n\t}\n\tif ( N < 8 ) {\n\t\t// Use simple summation...\n\t\ts = 0.0;\n\t\tfor ( i = 0; i < N; i++ ) {\n\t\t\tif ( isnan( x[ ix ] ) === false ) {\n\t\t\t\ts += x[ ix ];\n\t\t\t}\n\t\t\tix += stride;\n\t\t}\n\t\treturn s;\n\t}\n\treturn sum( N, x, stride, ix );\n}\n\n\n// EXPORTS //\n\nmodule.exports = gnansumpw;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2020 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Compute the sum of strided array elements, ignoring `NaN` values and using pairwise summation.\n*\n* @module @stdlib/blas-ext-base-gnansumpw\n*\n* @example\n* var gnansumpw = require( '@stdlib/blas-ext-base-gnansumpw' );\n*\n* var x = [ 1.0, -2.0, NaN, 2.0 ];\n* var N = x.length;\n*\n* var v = gnansumpw( N, x, 1 );\n* // returns 1.0\n*\n* @example\n* var floor = require( '@stdlib/math-base-special-floor' );\n* var gnansumpw = require( '@stdlib/blas-ext-base-gnansumpw' );\n*\n* var x = [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0, NaN, NaN ];\n* var N = floor( x.length / 2 );\n*\n* var v = gnansumpw.ndarray( N, x, 2, 1 );\n* // returns 5.0\n*/\n\n// MODULES //\n\nvar setReadOnly = require( '@stdlib/utils-define-nonenumerable-read-only-property' );\nvar main = require( './main.js' );\nvar ndarray = require( './ndarray.js' );\n\n\n// MAIN //\n\nsetReadOnly( main, 'ndarray', ndarray );\n\n\n// EXPORTS //\n\nmodule.exports = main;\n"],
|
|
5
|
-
"mappings": "uGAAA,IAAAA,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAQ,QAAS,iCAAkC,EACnDC,EAAQ,QAAS,iCAAkC,EAMnDC,EAAY,
|
|
6
|
-
"names": ["
|
|
3
|
+
"sources": ["../lib/accessors.js", "../lib/ndarray.js", "../lib/main.js", "../lib/index.js"],
|
|
4
|
+
"sourcesContent": ["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2025 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar isnan = require( '@stdlib/math-base-assert-is-nan' );\nvar floor = require( '@stdlib/math-base-special-floor' );\n\n\n// VARIABLES //\n\n// 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`.):\nvar BLOCKSIZE = 128;\n\n\n// MAIN //\n\n/**\n* Computes the sum of strided array elements, ignoring `NaN` values and using pairwise summation.\n*\n* ## Method\n*\n* - This implementation uses pairwise summation, which accrues rounding error `O(log2 N)` instead of `O(N)`. The recursion depth is also `O(log2 N)`.\n*\n* ## References\n*\n* - Higham, Nicholas J. 1993. \"The Accuracy of Floating Point Summation.\" _SIAM Journal on Scientific Computing_ 14 (4): 783\u201399. doi:[10.1137/0914050](https://doi.org/10.1137/0914050).\n*\n* @private\n* @param {PositiveInteger} N - number of indexed elements\n* @param {Object} x - input array object\n* @param {Collection} x.data - input array data\n* @param {Array<Function>} x.accessors - array element accessors\n* @param {integer} strideX - stride length for `x`\n* @param {NonNegativeInteger} offsetX - starting index for `x`\n* @returns {number} sum\n*\n* @example\n* var toAccessorArray = require( '@stdlib/array-base-to-accessor-array' );\n* var arraylike2object = require( '@stdlib/array-base-arraylike2object' );\n*\n* var x = toAccessorArray( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ] );\n*\n* var v = gnansumpw( 4, arraylike2object( x ), 2, 1 );\n* // returns 5.0\n*/\nfunction gnansumpw( N, x, strideX, offsetX ) {\n\tvar xbuf;\n\tvar xget;\n\tvar ix;\n\tvar s0;\n\tvar s1;\n\tvar s2;\n\tvar s3;\n\tvar s4;\n\tvar s5;\n\tvar s6;\n\tvar s7;\n\tvar M;\n\tvar s;\n\tvar n;\n\tvar v;\n\tvar i;\n\n\t// Cache reference to array data:\n\txbuf = x.data;\n\n\t// Cache reference to the element accessors:\n\txget = x.accessors[ 0 ];\n\n\tix = offsetX;\n\tif ( strideX === 0 ) {\n\t\tv = xget( xbuf, ix );\n\t\tif ( isnan( v ) ) {\n\t\t\treturn 0.0;\n\t\t}\n\t\treturn N * v;\n\t}\n\tif ( N < 8 ) {\n\t\t// Use simple summation...\n\t\ts = 0.0;\n\t\tfor ( i = 0; i < N; i++ ) {\n\t\t\tv = xget( xbuf, ix );\n\t\t\tif ( isnan( v ) === false ) {\n\t\t\t\ts += v;\n\t\t\t}\n\t\t\tix += strideX;\n\t\t}\n\t\treturn s;\n\t}\n\tif ( N <= BLOCKSIZE ) {\n\t\t// Sum a block with 8 accumulators (by loop unrolling, we lower the effective blocksize to 16)...\n\t\tv = xget( xbuf, ix );\n\t\ts0 = ( isnan( v ) ) ? 0.0 : v;\n\t\tix += strideX;\n\t\tv = xget( xbuf, ix );\n\t\ts1 = ( isnan( v ) ) ? 0.0 : v;\n\t\tix += strideX;\n\t\tv = xget( xbuf, ix );\n\t\ts2 = ( isnan( v ) ) ? 0.0 : v;\n\t\tix += strideX;\n\t\tv = xget( xbuf, ix );\n\t\ts3 = ( isnan( v ) ) ? 0.0 : v;\n\t\tix += strideX;\n\t\tv = xget( xbuf, ix );\n\t\ts4 = ( isnan( v ) ) ? 0.0 : v;\n\t\tix += strideX;\n\t\tv = xget( xbuf, ix );\n\t\ts5 = ( isnan( v ) ) ? 0.0 : v;\n\t\tix += strideX;\n\t\tv = xget( xbuf, ix );\n\t\ts6 = ( isnan( v ) ) ? 0.0 : v;\n\t\tix += strideX;\n\t\tv = xget( xbuf, ix );\n\t\ts7 = ( isnan( v ) ) ? 0.0 : v;\n\t\tix += strideX;\n\n\t\tM = N % 8;\n\t\tfor ( i = 8; i < N-M; i += 8 ) {\n\t\t\tv = xget( xbuf, ix );\n\t\t\ts0 += ( isnan( v ) ) ? 0.0 : v;\n\t\t\tix += strideX;\n\t\t\tv = xget( xbuf, ix );\n\t\t\ts1 += ( isnan( v ) ) ? 0.0 : v;\n\t\t\tix += strideX;\n\t\t\tv = xget( xbuf, ix );\n\t\t\ts2 += ( isnan( v ) ) ? 0.0 : v;\n\t\t\tix += strideX;\n\t\t\tv = xget( xbuf, ix );\n\t\t\ts3 += ( isnan( v ) ) ? 0.0 : v;\n\t\t\tix += strideX;\n\t\t\tv = xget( xbuf, ix );\n\t\t\ts4 += ( isnan( v ) ) ? 0.0 : v;\n\t\t\tix += strideX;\n\t\t\tv = xget( xbuf, ix );\n\t\t\ts5 += ( isnan( v ) ) ? 0.0 : v;\n\t\t\tix += strideX;\n\t\t\tv = xget( xbuf, ix );\n\t\t\ts6 += ( isnan( v ) ) ? 0.0 : v;\n\t\t\tix += strideX;\n\t\t\tv = xget( xbuf, ix );\n\t\t\ts7 += ( isnan( v ) ) ? 0.0 : v;\n\t\t\tix += strideX;\n\t\t}\n\t\t// Pairwise sum the accumulators:\n\t\ts = ( (s0+s1) + (s2+s3) ) + ( (s4+s5) + (s6+s7) );\n\n\t\t// Clean-up loop...\n\t\tfor ( i; i < N; i++ ) {\n\t\t\tv = xget( xbuf, ix );\n\t\t\tif ( isnan( v ) === false ) {\n\t\t\t\ts += v;\n\t\t\t}\n\t\t\tix += strideX;\n\t\t}\n\t\treturn s;\n\t}\n\t// Recurse by dividing by two, but avoiding non-multiples of unroll factor...\n\tn = floor( N/2 );\n\tn -= n % 8;\n\treturn gnansumpw( n, x, strideX, ix ) + gnansumpw( N-n, x, strideX, ix+(n*strideX) ); // eslint-disable-line max-len\n}\n\n\n// EXPORTS //\n\nmodule.exports = gnansumpw;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2020 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar arraylike2object = require( '@stdlib/array-base-arraylike2object' );\nvar isnan = require( '@stdlib/math-base-assert-is-nan' );\nvar floor = require( '@stdlib/math-base-special-floor' );\nvar accessors = require( './accessors.js' );\n\n\n// VARIABLES //\n\n// 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`.):\nvar BLOCKSIZE = 128;\n\n\n// MAIN //\n\n/**\n* Computes the sum of strided array elements, ignoring `NaN` values and using pairwise summation.\n*\n* ## Method\n*\n* - This implementation uses pairwise summation, which accrues rounding error `O(log2 N)` instead of `O(N)`. The recursion depth is also `O(log2 N)`.\n*\n* ## References\n*\n* - Higham, Nicholas J. 1993. \"The Accuracy of Floating Point Summation.\" _SIAM Journal on Scientific Computing_ 14 (4): 783\u201399. doi:[10.1137/0914050](https://doi.org/10.1137/0914050).\n*\n* @param {PositiveInteger} N - number of indexed elements\n* @param {NumericArray} x - input array\n* @param {integer} strideX - stride length\n* @param {NonNegativeInteger} offsetX - starting index\n* @returns {number} sum\n*\n* @example\n* var x = [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ];\n*\n* var v = gnansumpw( 4, x, 2, 1 );\n* // returns 5.0\n*/\nfunction gnansumpw( N, x, strideX, offsetX ) {\n\tvar ix;\n\tvar s0;\n\tvar s1;\n\tvar s2;\n\tvar s3;\n\tvar s4;\n\tvar s5;\n\tvar s6;\n\tvar s7;\n\tvar M;\n\tvar s;\n\tvar n;\n\tvar o;\n\tvar i;\n\n\tif ( N <= 0 ) {\n\t\treturn 0.0;\n\t}\n\to = arraylike2object( x );\n\tif ( o.accessorProtocol ) {\n\t\treturn accessors( N, o, strideX, offsetX );\n\t}\n\tix = offsetX;\n\tif ( strideX === 0 ) {\n\t\tif ( isnan( x[ ix ] ) ) {\n\t\t\treturn 0.0;\n\t\t}\n\t\treturn N * x[ ix ];\n\t}\n\tif ( N < 8 ) {\n\t\t// Use simple summation...\n\t\ts = 0.0;\n\t\tfor ( i = 0; i < N; i++ ) {\n\t\t\tif ( isnan( x[ ix ] ) === false ) {\n\t\t\t\ts += x[ ix ];\n\t\t\t}\n\t\t\tix += strideX;\n\t\t}\n\t\treturn s;\n\t}\n\tif ( N <= BLOCKSIZE ) {\n\t\t// Sum a block with 8 accumulators (by loop unrolling, we lower the effective blocksize to 16)...\n\t\ts0 = ( isnan( x[ ix ] ) ) ? 0.0 : x[ ix ];\n\t\tix += strideX;\n\t\ts1 = ( isnan( x[ ix ] ) ) ? 0.0 : x[ ix ];\n\t\tix += strideX;\n\t\ts2 = ( isnan( x[ ix ] ) ) ? 0.0 : x[ ix ];\n\t\tix += strideX;\n\t\ts3 = ( isnan( x[ ix ] ) ) ? 0.0 : x[ ix ];\n\t\tix += strideX;\n\t\ts4 = ( isnan( x[ ix ] ) ) ? 0.0 : x[ ix ];\n\t\tix += strideX;\n\t\ts5 = ( isnan( x[ ix ] ) ) ? 0.0 : x[ ix ];\n\t\tix += strideX;\n\t\ts6 = ( isnan( x[ ix ] ) ) ? 0.0 : x[ ix ];\n\t\tix += strideX;\n\t\ts7 = ( isnan( x[ ix ] ) ) ? 0.0 : x[ ix ];\n\t\tix += strideX;\n\n\t\tM = N % 8;\n\t\tfor ( i = 8; i < N-M; i += 8 ) {\n\t\t\ts0 += ( isnan( x[ ix ] ) ) ? 0.0 : x[ ix ];\n\t\t\tix += strideX;\n\t\t\ts1 += ( isnan( x[ ix ] ) ) ? 0.0 : x[ ix ];\n\t\t\tix += strideX;\n\t\t\ts2 += ( isnan( x[ ix ] ) ) ? 0.0 : x[ ix ];\n\t\t\tix += strideX;\n\t\t\ts3 += ( isnan( x[ ix ] ) ) ? 0.0 : x[ ix ];\n\t\t\tix += strideX;\n\t\t\ts4 += ( isnan( x[ ix ] ) ) ? 0.0 : x[ ix ];\n\t\t\tix += strideX;\n\t\t\ts5 += ( isnan( x[ ix ] ) ) ? 0.0 : x[ ix ];\n\t\t\tix += strideX;\n\t\t\ts6 += ( isnan( x[ ix ] ) ) ? 0.0 : x[ ix ];\n\t\t\tix += strideX;\n\t\t\ts7 += ( isnan( x[ ix ] ) ) ? 0.0 : x[ ix ];\n\t\t\tix += strideX;\n\t\t}\n\t\t// Pairwise sum the accumulators:\n\t\ts = ( (s0+s1) + (s2+s3) ) + ( (s4+s5) + (s6+s7) );\n\n\t\t// Clean-up loop...\n\t\tfor ( i; i < N; i++ ) {\n\t\t\tif ( isnan( x[ ix ] ) === false ) {\n\t\t\t\ts += x[ ix ];\n\t\t\t}\n\t\t\tix += strideX;\n\t\t}\n\t\treturn s;\n\t}\n\t// Recurse by dividing by two, but avoiding non-multiples of unroll factor...\n\tn = floor( N/2 );\n\tn -= n % 8;\n\treturn gnansumpw( n, x, strideX, ix ) + gnansumpw( N-n, x, strideX, ix+(n*strideX) ); // eslint-disable-line max-len\n}\n\n\n// EXPORTS //\n\nmodule.exports = gnansumpw;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2020 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar stride2offset = require( '@stdlib/strided-base-stride2offset' );\nvar ndarray = require( './ndarray.js' );\n\n\n// MAIN //\n\n/**\n* Computes the sum of strided array elements, ignoring `NaN` values and using pairwise summation.\n*\n* ## Method\n*\n* - This implementation uses pairwise summation, which accrues rounding error `O(log2 N)` instead of `O(N)`. The recursion depth is also `O(log2 N)`.\n*\n* ## References\n*\n* - Higham, Nicholas J. 1993. \"The Accuracy of Floating Point Summation.\" _SIAM Journal on Scientific Computing_ 14 (4): 783\u201399. doi:[10.1137/0914050](https://doi.org/10.1137/0914050).\n*\n* @param {PositiveInteger} N - number of indexed elements\n* @param {NumericArray} x - input array\n* @param {integer} strideX - stride length\n* @returns {number} sum\n*\n* @example\n* var x = [ 1.0, -2.0, NaN, 2.0 ];\n*\n* var v = gnansumpw( x.length, x, 1 );\n* // returns 1.0\n*/\nfunction gnansumpw( N, x, strideX ) {\n\treturn ndarray( N, x, strideX, stride2offset( N, strideX ) );\n}\n\n\n// EXPORTS //\n\nmodule.exports = gnansumpw;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2020 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Compute the sum of strided array elements, ignoring `NaN` values and using pairwise summation.\n*\n* @module @stdlib/blas-ext-base-gnansumpw\n*\n* @example\n* var gnansumpw = require( '@stdlib/blas-ext-base-gnansumpw' );\n*\n* var x = [ 1.0, -2.0, NaN, 2.0 ];\n*\n* var v = gnansumpw( x.length, x, 1 );\n* // returns 1.0\n*\n* @example\n* var gnansumpw = require( '@stdlib/blas-ext-base-gnansumpw' );\n*\n* var x = [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0, NaN, NaN ];\n*\n* var v = gnansumpw.ndarray( 5, x, 2, 1 );\n* // returns 5.0\n*/\n\n// MODULES //\n\nvar setReadOnly = require( '@stdlib/utils-define-nonenumerable-read-only-property' );\nvar main = require( './main.js' );\nvar ndarray = require( './ndarray.js' );\n\n\n// MAIN //\n\nsetReadOnly( main, 'ndarray', ndarray );\n\n\n// EXPORTS //\n\nmodule.exports = main;\n"],
|
|
5
|
+
"mappings": "uGAAA,IAAAA,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAQ,QAAS,iCAAkC,EACnDC,EAAQ,QAAS,iCAAkC,EAMnDC,EAAY,IAkChB,SAASC,EAAWC,EAAGC,EAAGC,EAASC,EAAU,CAC5C,IAAIC,EACAC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,EAAEe,EAAGd,EAAGC,EAASE,EAAIW,EAAEb,CAAS,CACpF,CAKAT,EAAO,QAAUM,IC/JjB,IAAAmB,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAgB,QAAS,oCAAqC,EAC9DC,EAAU,IA2Bd,SAASC,EAAWC,EAAGC,EAAGC,EAAU,CACnC,OAAOJ,EAASE,EAAGC,EAAGC,EAASL,EAAeG,EAAGE,CAAQ,CAAE,CAC5D,CAKAN,EAAO,QAAUG,ICbjB,IAAII,EAAc,QAAS,uDAAwD,EAC/EC,EAAO,IACPC,EAAU,IAKdF,EAAaC,EAAM,UAAWC,CAAQ,EAKtC,OAAO,QAAUD",
|
|
6
|
+
"names": ["require_accessors", "__commonJSMin", "exports", "module", "isnan", "floor", "BLOCKSIZE", "gnansumpw", "N", "x", "strideX", "offsetX", "xbuf", "xget", "ix", "s0", "s1", "s2", "s3", "s4", "s5", "s6", "s7", "M", "s", "n", "v", "i", "require_ndarray", "__commonJSMin", "exports", "module", "arraylike2object", "isnan", "floor", "accessors", "BLOCKSIZE", "gnansumpw", "N", "x", "strideX", "offsetX", "ix", "s0", "s1", "s2", "s3", "s4", "s5", "s6", "s7", "M", "s", "n", "o", "i", "require_main", "__commonJSMin", "exports", "module", "stride2offset", "ndarray", "gnansumpw", "N", "x", "strideX", "setReadOnly", "main", "ndarray"]
|
|
7
7
|
}
|
package/docs/types/index.d.ts
CHANGED
|
@@ -20,7 +20,12 @@
|
|
|
20
20
|
|
|
21
21
|
/// <reference types="@stdlib/types"/>
|
|
22
22
|
|
|
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>;
|
|
24
29
|
|
|
25
30
|
/**
|
|
26
31
|
* Interface describing `gnansumpw`.
|
|
@@ -31,7 +36,7 @@ interface Routine {
|
|
|
31
36
|
*
|
|
32
37
|
* @param N - number of indexed elements
|
|
33
38
|
* @param x - input array
|
|
34
|
-
* @param
|
|
39
|
+
* @param strideX - stride length
|
|
35
40
|
* @returns sum
|
|
36
41
|
*
|
|
37
42
|
* @example
|
|
@@ -40,15 +45,15 @@ interface Routine {
|
|
|
40
45
|
* var v = gnansumpw( x.length, x, 1 );
|
|
41
46
|
* // returns 1.0
|
|
42
47
|
*/
|
|
43
|
-
( N: number, x:
|
|
48
|
+
( N: number, x: InputArray, strideX: number ): number;
|
|
44
49
|
|
|
45
50
|
/**
|
|
46
51
|
* Computes the sum of strided array elements, ignoring `NaN` values and using pairwise summation and alternative indexing semantics.
|
|
47
52
|
*
|
|
48
53
|
* @param N - number of indexed elements
|
|
49
54
|
* @param x - input array
|
|
50
|
-
* @param
|
|
51
|
-
* @param
|
|
55
|
+
* @param strideX - stride length
|
|
56
|
+
* @param offsetX - starting index
|
|
52
57
|
* @returns sum
|
|
53
58
|
*
|
|
54
59
|
* @example
|
|
@@ -57,7 +62,7 @@ interface Routine {
|
|
|
57
62
|
* var v = gnansumpw.ndarray( x.length, x, 1, 0 );
|
|
58
63
|
* // returns 1.0
|
|
59
64
|
*/
|
|
60
|
-
ndarray( N: number, x:
|
|
65
|
+
ndarray( N: number, x: InputArray, strideX: number, offsetX: number ): number;
|
|
61
66
|
}
|
|
62
67
|
|
|
63
68
|
/**
|
|
@@ -65,7 +70,7 @@ interface Routine {
|
|
|
65
70
|
*
|
|
66
71
|
* @param N - number of indexed elements
|
|
67
72
|
* @param x - input array
|
|
68
|
-
* @param
|
|
73
|
+
* @param strideX - stride length
|
|
69
74
|
* @returns sum
|
|
70
75
|
*
|
|
71
76
|
* @example
|
package/lib/accessors.js
ADDED
|
@@ -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;
|
package/lib/index.js
CHANGED
|
@@ -27,19 +27,16 @@
|
|
|
27
27
|
* var gnansumpw = require( '@stdlib/blas-ext-base-gnansumpw' );
|
|
28
28
|
*
|
|
29
29
|
* var x = [ 1.0, -2.0, NaN, 2.0 ];
|
|
30
|
-
* var N = x.length;
|
|
31
30
|
*
|
|
32
|
-
* var v = gnansumpw(
|
|
31
|
+
* var v = gnansumpw( x.length, x, 1 );
|
|
33
32
|
* // returns 1.0
|
|
34
33
|
*
|
|
35
34
|
* @example
|
|
36
|
-
* var floor = require( '@stdlib/math-base-special-floor' );
|
|
37
35
|
* var gnansumpw = require( '@stdlib/blas-ext-base-gnansumpw' );
|
|
38
36
|
*
|
|
39
37
|
* var x = [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0, NaN, NaN ];
|
|
40
|
-
* var N = floor( x.length / 2 );
|
|
41
38
|
*
|
|
42
|
-
* var v = gnansumpw.ndarray(
|
|
39
|
+
* var v = gnansumpw.ndarray( 5, x, 2, 1 );
|
|
43
40
|
* // returns 5.0
|
|
44
41
|
*/
|
|
45
42
|
|
package/lib/main.js
CHANGED
|
@@ -20,8 +20,8 @@
|
|
|
20
20
|
|
|
21
21
|
// MODULES //
|
|
22
22
|
|
|
23
|
-
var
|
|
24
|
-
var
|
|
23
|
+
var stride2offset = require( '@stdlib/strided-base-stride2offset' );
|
|
24
|
+
var ndarray = require( './ndarray.js' );
|
|
25
25
|
|
|
26
26
|
|
|
27
27
|
// MAIN //
|
|
@@ -39,47 +39,17 @@ var sum = require( './ndarray.js' );
|
|
|
39
39
|
*
|
|
40
40
|
* @param {PositiveInteger} N - number of indexed elements
|
|
41
41
|
* @param {NumericArray} x - input array
|
|
42
|
-
* @param {integer}
|
|
42
|
+
* @param {integer} strideX - stride length
|
|
43
43
|
* @returns {number} sum
|
|
44
44
|
*
|
|
45
45
|
* @example
|
|
46
46
|
* var x = [ 1.0, -2.0, NaN, 2.0 ];
|
|
47
|
-
* var N = x.length;
|
|
48
47
|
*
|
|
49
|
-
* var v = gnansumpw(
|
|
48
|
+
* var v = gnansumpw( x.length, x, 1 );
|
|
50
49
|
* // returns 1.0
|
|
51
50
|
*/
|
|
52
|
-
function gnansumpw( N, x,
|
|
53
|
-
|
|
54
|
-
var s;
|
|
55
|
-
var i;
|
|
56
|
-
|
|
57
|
-
if ( N <= 0 ) {
|
|
58
|
-
return 0.0;
|
|
59
|
-
}
|
|
60
|
-
if ( N === 1 || stride === 0 ) {
|
|
61
|
-
if ( isnan( x[ 0 ] ) ) {
|
|
62
|
-
return 0.0;
|
|
63
|
-
}
|
|
64
|
-
return x[ 0 ];
|
|
65
|
-
}
|
|
66
|
-
if ( stride < 0 ) {
|
|
67
|
-
ix = (1-N) * stride;
|
|
68
|
-
} else {
|
|
69
|
-
ix = 0;
|
|
70
|
-
}
|
|
71
|
-
if ( N < 8 ) {
|
|
72
|
-
// Use simple summation...
|
|
73
|
-
s = 0.0;
|
|
74
|
-
for ( i = 0; i < N; i++ ) {
|
|
75
|
-
if ( isnan( x[ ix ] ) === false ) {
|
|
76
|
-
s += x[ ix ];
|
|
77
|
-
}
|
|
78
|
-
ix += stride;
|
|
79
|
-
}
|
|
80
|
-
return s;
|
|
81
|
-
}
|
|
82
|
-
return sum( N, x, stride, ix );
|
|
51
|
+
function gnansumpw( N, x, strideX ) {
|
|
52
|
+
return ndarray( N, x, strideX, stride2offset( N, strideX ) );
|
|
83
53
|
}
|
|
84
54
|
|
|
85
55
|
|
package/lib/ndarray.js
CHANGED
|
@@ -20,8 +20,10 @@
|
|
|
20
20
|
|
|
21
21
|
// MODULES //
|
|
22
22
|
|
|
23
|
+
var arraylike2object = require( '@stdlib/array-base-arraylike2object' );
|
|
23
24
|
var isnan = require( '@stdlib/math-base-assert-is-nan' );
|
|
24
25
|
var floor = require( '@stdlib/math-base-special-floor' );
|
|
26
|
+
var accessors = require( './accessors.js' );
|
|
25
27
|
|
|
26
28
|
|
|
27
29
|
// VARIABLES //
|
|
@@ -45,20 +47,17 @@ var BLOCKSIZE = 128;
|
|
|
45
47
|
*
|
|
46
48
|
* @param {PositiveInteger} N - number of indexed elements
|
|
47
49
|
* @param {NumericArray} x - input array
|
|
48
|
-
* @param {integer}
|
|
49
|
-
* @param {NonNegativeInteger}
|
|
50
|
+
* @param {integer} strideX - stride length
|
|
51
|
+
* @param {NonNegativeInteger} offsetX - starting index
|
|
50
52
|
* @returns {number} sum
|
|
51
53
|
*
|
|
52
54
|
* @example
|
|
53
|
-
* var floor = require( '@stdlib/math-base-special-floor' );
|
|
54
|
-
*
|
|
55
55
|
* var x = [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ];
|
|
56
|
-
* var N = floor( x.length / 2 );
|
|
57
56
|
*
|
|
58
|
-
* var v = gnansumpw(
|
|
57
|
+
* var v = gnansumpw( 4, x, 2, 1 );
|
|
59
58
|
* // returns 5.0
|
|
60
59
|
*/
|
|
61
|
-
function gnansumpw( N, x,
|
|
60
|
+
function gnansumpw( N, x, strideX, offsetX ) {
|
|
62
61
|
var ix;
|
|
63
62
|
var s0;
|
|
64
63
|
var s1;
|
|
@@ -71,18 +70,23 @@ function gnansumpw( N, x, stride, offset ) {
|
|
|
71
70
|
var M;
|
|
72
71
|
var s;
|
|
73
72
|
var n;
|
|
73
|
+
var o;
|
|
74
74
|
var i;
|
|
75
75
|
|
|
76
76
|
if ( N <= 0 ) {
|
|
77
77
|
return 0.0;
|
|
78
78
|
}
|
|
79
|
-
|
|
80
|
-
|
|
79
|
+
o = arraylike2object( x );
|
|
80
|
+
if ( o.accessorProtocol ) {
|
|
81
|
+
return accessors( N, o, strideX, offsetX );
|
|
82
|
+
}
|
|
83
|
+
ix = offsetX;
|
|
84
|
+
if ( strideX === 0 ) {
|
|
85
|
+
if ( isnan( x[ ix ] ) ) {
|
|
81
86
|
return 0.0;
|
|
82
87
|
}
|
|
83
|
-
return x[
|
|
88
|
+
return N * x[ ix ];
|
|
84
89
|
}
|
|
85
|
-
ix = offset;
|
|
86
90
|
if ( N < 8 ) {
|
|
87
91
|
// Use simple summation...
|
|
88
92
|
s = 0.0;
|
|
@@ -90,64 +94,64 @@ function gnansumpw( N, x, stride, offset ) {
|
|
|
90
94
|
if ( isnan( x[ ix ] ) === false ) {
|
|
91
95
|
s += x[ ix ];
|
|
92
96
|
}
|
|
93
|
-
ix +=
|
|
97
|
+
ix += strideX;
|
|
94
98
|
}
|
|
95
99
|
return s;
|
|
96
100
|
}
|
|
97
101
|
if ( N <= BLOCKSIZE ) {
|
|
98
102
|
// Sum a block with 8 accumulators (by loop unrolling, we lower the effective blocksize to 16)...
|
|
99
103
|
s0 = ( isnan( x[ ix ] ) ) ? 0.0 : x[ ix ];
|
|
100
|
-
ix +=
|
|
104
|
+
ix += strideX;
|
|
101
105
|
s1 = ( isnan( x[ ix ] ) ) ? 0.0 : x[ ix ];
|
|
102
|
-
ix +=
|
|
106
|
+
ix += strideX;
|
|
103
107
|
s2 = ( isnan( x[ ix ] ) ) ? 0.0 : x[ ix ];
|
|
104
|
-
ix +=
|
|
108
|
+
ix += strideX;
|
|
105
109
|
s3 = ( isnan( x[ ix ] ) ) ? 0.0 : x[ ix ];
|
|
106
|
-
ix +=
|
|
110
|
+
ix += strideX;
|
|
107
111
|
s4 = ( isnan( x[ ix ] ) ) ? 0.0 : x[ ix ];
|
|
108
|
-
ix +=
|
|
112
|
+
ix += strideX;
|
|
109
113
|
s5 = ( isnan( x[ ix ] ) ) ? 0.0 : x[ ix ];
|
|
110
|
-
ix +=
|
|
114
|
+
ix += strideX;
|
|
111
115
|
s6 = ( isnan( x[ ix ] ) ) ? 0.0 : x[ ix ];
|
|
112
|
-
ix +=
|
|
116
|
+
ix += strideX;
|
|
113
117
|
s7 = ( isnan( x[ ix ] ) ) ? 0.0 : x[ ix ];
|
|
114
|
-
ix +=
|
|
118
|
+
ix += strideX;
|
|
115
119
|
|
|
116
120
|
M = N % 8;
|
|
117
121
|
for ( i = 8; i < N-M; i += 8 ) {
|
|
118
122
|
s0 += ( isnan( x[ ix ] ) ) ? 0.0 : x[ ix ];
|
|
119
|
-
ix +=
|
|
123
|
+
ix += strideX;
|
|
120
124
|
s1 += ( isnan( x[ ix ] ) ) ? 0.0 : x[ ix ];
|
|
121
|
-
ix +=
|
|
125
|
+
ix += strideX;
|
|
122
126
|
s2 += ( isnan( x[ ix ] ) ) ? 0.0 : x[ ix ];
|
|
123
|
-
ix +=
|
|
127
|
+
ix += strideX;
|
|
124
128
|
s3 += ( isnan( x[ ix ] ) ) ? 0.0 : x[ ix ];
|
|
125
|
-
ix +=
|
|
129
|
+
ix += strideX;
|
|
126
130
|
s4 += ( isnan( x[ ix ] ) ) ? 0.0 : x[ ix ];
|
|
127
|
-
ix +=
|
|
131
|
+
ix += strideX;
|
|
128
132
|
s5 += ( isnan( x[ ix ] ) ) ? 0.0 : x[ ix ];
|
|
129
|
-
ix +=
|
|
133
|
+
ix += strideX;
|
|
130
134
|
s6 += ( isnan( x[ ix ] ) ) ? 0.0 : x[ ix ];
|
|
131
|
-
ix +=
|
|
135
|
+
ix += strideX;
|
|
132
136
|
s7 += ( isnan( x[ ix ] ) ) ? 0.0 : x[ ix ];
|
|
133
|
-
ix +=
|
|
137
|
+
ix += strideX;
|
|
134
138
|
}
|
|
135
139
|
// Pairwise sum the accumulators:
|
|
136
|
-
s = ((s0+s1) + (s2+s3)) + ((s4+s5) + (s6+s7));
|
|
140
|
+
s = ( (s0+s1) + (s2+s3) ) + ( (s4+s5) + (s6+s7) );
|
|
137
141
|
|
|
138
142
|
// Clean-up loop...
|
|
139
143
|
for ( i; i < N; i++ ) {
|
|
140
144
|
if ( isnan( x[ ix ] ) === false ) {
|
|
141
145
|
s += x[ ix ];
|
|
142
146
|
}
|
|
143
|
-
ix +=
|
|
147
|
+
ix += strideX;
|
|
144
148
|
}
|
|
145
149
|
return s;
|
|
146
150
|
}
|
|
147
151
|
// Recurse by dividing by two, but avoiding non-multiples of unroll factor...
|
|
148
152
|
n = floor( N/2 );
|
|
149
153
|
n -= n % 8;
|
|
150
|
-
return gnansumpw( n, x,
|
|
154
|
+
return gnansumpw( n, x, strideX, ix ) + gnansumpw( N-n, x, strideX, ix+(n*strideX) ); // eslint-disable-line max-len
|
|
151
155
|
}
|
|
152
156
|
|
|
153
157
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@stdlib/blas-ext-base-gnansumpw",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "Calculate the sum of strided array elements, ignoring NaN values and using pairwise summation.",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"author": {
|
|
@@ -30,9 +30,11 @@
|
|
|
30
30
|
"url": "https://github.com/stdlib-js/stdlib/issues"
|
|
31
31
|
},
|
|
32
32
|
"dependencies": {
|
|
33
|
-
"@stdlib/
|
|
34
|
-
"@stdlib/math-base-
|
|
35
|
-
"@stdlib/
|
|
33
|
+
"@stdlib/array-base-arraylike2object": "^0.2.1",
|
|
34
|
+
"@stdlib/math-base-assert-is-nan": "^0.2.2",
|
|
35
|
+
"@stdlib/math-base-special-floor": "^0.2.3",
|
|
36
|
+
"@stdlib/strided-base-stride2offset": "^0.1.0",
|
|
37
|
+
"@stdlib/utils-define-nonenumerable-read-only-property": "^0.2.2"
|
|
36
38
|
},
|
|
37
39
|
"devDependencies": {},
|
|
38
40
|
"engines": {
|
|
@@ -68,7 +70,6 @@
|
|
|
68
70
|
"strided array",
|
|
69
71
|
"array"
|
|
70
72
|
],
|
|
71
|
-
"__stdlib__": {},
|
|
72
73
|
"funding": {
|
|
73
74
|
"type": "opencollective",
|
|
74
75
|
"url": "https://opencollective.com/stdlib"
|