Skip to content

Commit e242eb1

Browse files
authored
feat: add accessor arrays support to blas/ext/base/gnansum
PR-URL: #5066 Reviewed-by: Athan Reines <[email protected]>
1 parent e5a8c65 commit e242eb1

File tree

5 files changed

+249
-3
lines changed

5 files changed

+249
-3
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ var v = gnansum.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
- Depending on the environment, the typed versions ([`dnansum`][@stdlib/blas/ext/base/dnansum], [`snansum`][@stdlib/blas/ext/base/snansum], etc.) are likely to be significantly more performant.
113114

114115
</section>
@@ -176,6 +177,8 @@ console.log( v );
176177

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

180+
[@stdlib/array/base/accessor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/base/accessor
181+
179182
<!-- <related-links> -->
180183

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

lib/node_modules/@stdlib/blas/ext/base/gnansum/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 `gnansum`.
@@ -40,7 +45,7 @@ interface Routine {
4045
* var v = gnansum( 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 alternative indexing semantics.
@@ -57,7 +62,7 @@ interface Routine {
5762
* var v = gnansum.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/gnansum/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 gnansum = require( './index' );
2021

2122

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

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

8789
gnansum.ndarray( x.length, x, 1, 0 ); // $ExpectType number
90+
gnansum.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...

lib/node_modules/@stdlib/blas/ext/base/gnansum/test/test.main.js

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
// MODULES //
2222

2323
var tape = require( 'tape' );
24+
var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' );
2425
var Float64Array = require( '@stdlib/array/float64' );
2526
var gnansum = require( './../lib' );
2627

@@ -73,6 +74,41 @@ tape( 'the function calculates the sum of strided array elements (ignoring NaN v
7374
t.end();
7475
});
7576

77+
tape( 'the function calculates the sum of strided array elements (ignoring NaN values, accessors)', function test( t ) {
78+
var x;
79+
var v;
80+
81+
x = [ 1.0, -2.0, -4.0, 5.0, 0.0, NaN, 3.0, 0.0, -3.0, 3.0 ];
82+
v = gnansum( x.length, toAccessorArray( x ), 1 );
83+
t.strictEqual( v, 3.0, 'returns expected value' );
84+
85+
x = [ 1.0, -2.0, -4.0, NaN, 5.0, 0.0, 3.0 ];
86+
v = gnansum( x.length, toAccessorArray( x ), 1 );
87+
t.strictEqual( v, 3.0, 'returns expected value' );
88+
89+
x = [ -4.0, NaN, -4.0 ];
90+
v = gnansum( x.length, toAccessorArray( x ), 1 );
91+
t.strictEqual( v, -8.0, 'returns expected value' );
92+
93+
x = [ NaN, 4.0 ];
94+
v = gnansum( x.length, toAccessorArray( x ), 1 );
95+
t.strictEqual( v, 4.0, 'returns expected value' );
96+
97+
x = [ NaN, NaN ];
98+
v = gnansum( x.length, toAccessorArray( x ), 1 );
99+
t.strictEqual( v, 0.0, 'returns expected value' );
100+
101+
x = [ NaN ];
102+
v = gnansum( x.length, toAccessorArray( x ), 1 );
103+
t.strictEqual( v, 0.0, 'returns expected value' );
104+
105+
x = [ 1.0, 1.0e100, 1.0, -1.0e100 ];
106+
v = gnansum( x.length, toAccessorArray( x ), 1 );
107+
t.strictEqual( v, 2.0, 'returns expected value' );
108+
109+
t.end();
110+
});
111+
76112
tape( 'if provided an `N` parameter less than or equal to `0`, the function returns `0.0`', function test( t ) {
77113
var x;
78114
var v;
@@ -123,6 +159,29 @@ tape( 'the function supports a `stride` parameter', function test( t ) {
123159
t.end();
124160
});
125161

162+
tape( 'the function supports a `stride` parameter (accessors)', function test( t ) {
163+
var x;
164+
var v;
165+
166+
x = [
167+
1.0, // 0
168+
2.0,
169+
2.0, // 1
170+
-7.0,
171+
-2.0, // 2
172+
3.0,
173+
4.0, // 3
174+
2.0,
175+
NaN, // 4
176+
NaN
177+
];
178+
179+
v = gnansum( 5, toAccessorArray( x ), 2 );
180+
181+
t.strictEqual( v, 5.0, 'returns expected value' );
182+
t.end();
183+
});
184+
126185
tape( 'the function supports a negative `stride` parameter', function test( t ) {
127186
var x;
128187
var v;
@@ -146,6 +205,29 @@ tape( 'the function supports a negative `stride` parameter', function test( t )
146205
t.end();
147206
});
148207

208+
tape( 'the function supports a negative `stride` parameter (accessors)', function test( t ) {
209+
var x;
210+
var v;
211+
212+
x = [
213+
NaN, // 4
214+
NaN,
215+
1.0, // 3
216+
2.0,
217+
2.0, // 2
218+
-7.0,
219+
-2.0, // 1
220+
3.0,
221+
4.0, // 0
222+
2.0
223+
];
224+
225+
v = gnansum( 5, toAccessorArray( x ), -2 );
226+
227+
t.strictEqual( v, 5.0, 'returns expected value' );
228+
t.end();
229+
});
230+
149231
tape( 'if provided a `stride` parameter equal to `0`, the function returns the sum of the first element repeated N times', function test( t ) {
150232
var x;
151233
var v;
@@ -158,6 +240,18 @@ tape( 'if provided a `stride` parameter equal to `0`, the function returns the s
158240
t.end();
159241
});
160242

243+
tape( 'if provided a `stride` parameter equal to `0`, the function returns the sum of the first element repeated N times (accessors)', function test( t ) {
244+
var x;
245+
var v;
246+
247+
x = [ 1.0, -2.0, -4.0, 5.0, 3.0 ];
248+
249+
v = gnansum( x.length, toAccessorArray( x ), 0 );
250+
t.strictEqual( v, 5.0, 'returns expected value' );
251+
252+
t.end();
253+
});
254+
161255
tape( 'if provided a `stride` parameter equal to `0` and the first element is NaN, the function returns 0', function test( t ) {
162256
var x;
163257
var v;
@@ -170,6 +264,18 @@ tape( 'if provided a `stride` parameter equal to `0` and the first element is Na
170264
t.end();
171265
});
172266

267+
tape( 'if provided a `stride` parameter equal to `0` and the first element is NaN, the function returns 0 (accessors)', function test( t ) {
268+
var x;
269+
var v;
270+
271+
x = [ NaN, -2.0, -4.0, 5.0, 3.0 ];
272+
273+
v = gnansum( x.length, toAccessorArray( x ), 0 );
274+
t.strictEqual( v, 0.0, 'returns expected value' );
275+
276+
t.end();
277+
});
278+
173279
tape( 'the function supports view offsets', function test( t ) {
174280
var x0;
175281
var x1;

lib/node_modules/@stdlib/blas/ext/base/gnansum/test/test.ndarray.js

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
// MODULES //
2222

2323
var tape = require( 'tape' );
24+
var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' );
2425
var gnansum = require( './../lib/ndarray.js' );
2526

2627

@@ -72,6 +73,41 @@ tape( 'the function calculates the sum of strided array elements (ignoring NaN v
7273
t.end();
7374
});
7475

76+
tape( 'the function calculates the sum of strided array elements (ignoring NaN values, accessors)', function test( t ) {
77+
var x;
78+
var v;
79+
80+
x = [ 1.0, -2.0, -4.0, 5.0, 0.0, NaN, 3.0, 0.0, -3.0, 3.0 ];
81+
v = gnansum( x.length, toAccessorArray( x ), 1, 0 );
82+
t.strictEqual( v, 3.0, 'returns expected value' );
83+
84+
x = [ 1.0, -2.0, -4.0, NaN, 5.0, 0.0, 3.0 ];
85+
v = gnansum( x.length, toAccessorArray( x ), 1, 0 );
86+
t.strictEqual( v, 3.0, 'returns expected value' );
87+
88+
x = [ -4.0, NaN, -4.0 ];
89+
v = gnansum( x.length, toAccessorArray( x ), 1, 0 );
90+
t.strictEqual( v, -8.0, 'returns expected value' );
91+
92+
x = [ NaN, 4.0 ];
93+
v = gnansum( x.length, toAccessorArray( x ), 1, 0 );
94+
t.strictEqual( v, 4.0, 'returns expected value' );
95+
96+
x = [ NaN, NaN ];
97+
v = gnansum( x.length, toAccessorArray( x ), 1, 0 );
98+
t.strictEqual( v, 0.0, 'returns expected value' );
99+
100+
x = [ NaN ];
101+
v = gnansum( x.length, toAccessorArray( x ), 1, 0 );
102+
t.strictEqual( v, 0.0, 'returns expected value' );
103+
104+
x = [ 1.0, 1.0e100, 1.0, -1.0e100 ];
105+
v = gnansum( x.length, toAccessorArray( x ), 1, 0 );
106+
t.strictEqual( v, 2.0, 'returns expected value' );
107+
108+
t.end();
109+
});
110+
75111
tape( 'if provided an `N` parameter less than or equal to `0`, the function returns `0.0`', function test( t ) {
76112
var x;
77113
var v;
@@ -122,6 +158,29 @@ tape( 'the function supports a `stride` parameter', function test( t ) {
122158
t.end();
123159
});
124160

161+
tape( 'the function supports a `stride` parameter (accessors)', function test( t ) {
162+
var x;
163+
var v;
164+
165+
x = [
166+
1.0, // 0
167+
2.0,
168+
2.0, // 1
169+
-7.0,
170+
-2.0, // 2
171+
3.0,
172+
4.0, // 3
173+
2.0,
174+
NaN, // 4
175+
NaN
176+
];
177+
178+
v = gnansum( 5, toAccessorArray( x ), 2, 0 );
179+
180+
t.strictEqual( v, 5.0, 'returns expected value' );
181+
t.end();
182+
});
183+
125184
tape( 'the function supports a negative `stride` parameter', function test( t ) {
126185
var x;
127186
var v;
@@ -145,6 +204,29 @@ tape( 'the function supports a negative `stride` parameter', function test( t )
145204
t.end();
146205
});
147206

207+
tape( 'the function supports a negative `stride` parameter (accessors)', function test( t ) {
208+
var x;
209+
var v;
210+
211+
x = [
212+
NaN, // 4
213+
NaN,
214+
1.0, // 3
215+
2.0,
216+
2.0, // 2
217+
-7.0,
218+
-2.0, // 1
219+
3.0,
220+
4.0, // 0
221+
2.0
222+
];
223+
224+
v = gnansum( 5, toAccessorArray( x ), -2, 8 );
225+
226+
t.strictEqual( v, 5.0, 'returns expected value' );
227+
t.end();
228+
});
229+
148230
tape( 'if provided a `stride` parameter equal to `0`, the function returns the sum of the first element repeated N times', function test( t ) {
149231
var x;
150232
var v;
@@ -157,6 +239,18 @@ tape( 'if provided a `stride` parameter equal to `0`, the function returns the s
157239
t.end();
158240
});
159241

242+
tape( 'if provided a `stride` parameter equal to `0`, the function returns the sum of the first element repeated N times (accessors)', function test( t ) {
243+
var x;
244+
var v;
245+
246+
x = [ 1.0, -2.0, -4.0, 5.0, 3.0 ];
247+
248+
v = gnansum( x.length, toAccessorArray( x ), 0, 0 );
249+
t.strictEqual( v, 5.0, 'returns expected value' );
250+
251+
t.end();
252+
});
253+
160254
tape( 'if provided a `stride` parameter equal to `0` and the first element is NaN, the function returns 0', function test( t ) {
161255
var x;
162256
var v;
@@ -169,6 +263,18 @@ tape( 'if provided a `stride` parameter equal to `0` and the first element is Na
169263
t.end();
170264
});
171265

266+
tape( 'if provided a `stride` parameter equal to `0` and the first element is NaN, the function returns 0 (accessors)', function test( t ) {
267+
var x;
268+
var v;
269+
270+
x = [ NaN, -2.0, -4.0, 5.0, 3.0 ];
271+
272+
v = gnansum( x.length, toAccessorArray( x ), 0, 0 );
273+
t.strictEqual( v, 0.0, 'returns expected value' );
274+
275+
t.end();
276+
});
277+
172278
tape( 'the function supports an `offset` parameter', function test( t ) {
173279
var x;
174280
var v;
@@ -191,3 +297,26 @@ tape( 'the function supports an `offset` parameter', function test( t ) {
191297

192298
t.end();
193299
});
300+
301+
tape( 'the function supports an `offset` parameter', function test( t ) {
302+
var x;
303+
var v;
304+
305+
x = [
306+
2.0,
307+
1.0, // 0
308+
2.0,
309+
-2.0, // 1
310+
-2.0,
311+
2.0, // 2
312+
3.0,
313+
4.0, // 3
314+
NaN,
315+
NaN // 4
316+
];
317+
318+
v = gnansum( 5, toAccessorArray( x ), 2, 1 );
319+
t.strictEqual( v, 5.0, 'returns expected value' );
320+
321+
t.end();
322+
});

0 commit comments

Comments
 (0)