Skip to content

Commit 152dfd2

Browse files
authored
feat: add accessor arrays support to blas/ext/base/gsum
PR-URL: #5134 Reviewed-by: Athan Reines <[email protected]>
1 parent ece4002 commit 152dfd2

File tree

5 files changed

+199
-3
lines changed

5 files changed

+199
-3
lines changed

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

+3
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ var v = gsum.ndarray( 4, 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 ([`dsum`][@stdlib/blas/ext/base/dsum], [`ssum`][@stdlib/blas/ext/base/ssum], etc.) are likely to be significantly more performant.
113114

114115
</section>
@@ -167,6 +168,8 @@ console.log( v );
167168

168169
[mdn-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array
169170

171+
[@stdlib/array/base/accessor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/base/accessor
172+
170173
[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
171174

172175
<!-- <related-links> -->

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

2122

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

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

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

+82
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
var tape = require( 'tape' );
2424
var isnan = require( '@stdlib/math/base/assert/is-nan' );
25+
var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' );
2526
var Float64Array = require( '@stdlib/array/float64' );
2627
var gsum = require( './../lib' );
2728

@@ -66,6 +67,33 @@ tape( 'the function calculates the sum of all strided array elements', function
6667
t.end();
6768
});
6869

70+
tape( 'the function 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 = gsum( x.length, toAccessorArray( x ), 1 );
76+
t.strictEqual( v, 3.0, 'returns expected value' );
77+
78+
x = [ 1.0, -2.0, -4.0, 5.0, 0.0, 3.0 ];
79+
v = gsum( x.length, toAccessorArray( x ), 1 );
80+
t.strictEqual( v, 3.0, 'returns expected value' );
81+
82+
x = [ -4.0, -4.0 ];
83+
v = gsum( x.length, toAccessorArray( x ), 1 );
84+
t.strictEqual( v, -8.0, 'returns expected value' );
85+
86+
x = [ NaN, 4.0 ];
87+
v = gsum( x.length, toAccessorArray( x ), 1 );
88+
t.strictEqual( isnan( v ), true, 'returns expected value' );
89+
90+
x = [ 1.0, 1.0e100, 1.0, -1.0e100 ];
91+
v = gsum( x.length, toAccessorArray( x ), 1 );
92+
t.strictEqual( v, 2.0, 'returns expected value' );
93+
94+
t.end();
95+
});
96+
6997
tape( 'if provided an `N` parameter less than or equal to `0`, the function returns `0.0`', function test( t ) {
7098
var x;
7199
var v;
@@ -114,6 +142,27 @@ tape( 'the function supports a `stride` parameter', function test( t ) {
114142
t.end();
115143
});
116144

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 = gsum( 4, toAccessorArray( x ), 2 );
161+
162+
t.strictEqual( v, 5.0, 'returns expected value' );
163+
t.end();
164+
});
165+
117166
tape( 'the function supports a negative `stride` parameter', function test( t ) {
118167
var x;
119168
var v;
@@ -135,6 +184,27 @@ tape( 'the function supports a negative `stride` parameter', function test( t )
135184
t.end();
136185
});
137186

187+
tape( 'the function supports a negative `stride` parameter (accessors)', 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 = gsum( 4, toAccessorArray( x ), -2 );
203+
204+
t.strictEqual( v, 5.0, 'returns expected value' );
205+
t.end();
206+
});
207+
138208
tape( 'if provided a `stride` parameter equal to `0`, the function returns the sum of the first element repeated N times', function test( t ) {
139209
var x;
140210
var v;
@@ -147,6 +217,18 @@ tape( 'if provided a `stride` parameter equal to `0`, the function returns the s
147217
t.end();
148218
});
149219

220+
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 ) {
221+
var x;
222+
var v;
223+
224+
x = [ 1.0, -2.0, -4.0, 5.0, 3.0 ];
225+
226+
v = gsum( x.length, toAccessorArray( x ), 0 );
227+
t.strictEqual( v, 5.0, 'returns expected value' );
228+
229+
t.end();
230+
});
231+
150232
tape( 'the function supports view offsets', function test( t ) {
151233
var x0;
152234
var x1;

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

+103
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
var tape = require( 'tape' );
2424
var isnan = require( '@stdlib/math/base/assert/is-nan' );
25+
var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' );
2526
var gsum = require( './../lib/ndarray.js' );
2627

2728

@@ -65,6 +66,33 @@ tape( 'the function calculates the sum of all strided array elements', function
6566
t.end();
6667
});
6768

69+
tape( 'the function calculates the sum of all strided array elements', function test( t ) {
70+
var x;
71+
var v;
72+
73+
x = [ 1.0, -2.0, -4.0, 5.0, 0.0, 3.0, 0.0, -3.0, 3.0 ];
74+
v = gsum( x.length, toAccessorArray( x ), 1, 0 );
75+
t.strictEqual( v, 3.0, 'returns expected value' );
76+
77+
x = [ 1.0, -2.0, -4.0, 5.0, 0.0, 3.0 ];
78+
v = gsum( x.length, toAccessorArray( x ), 1, 0 );
79+
t.strictEqual( v, 3.0, 'returns expected value' );
80+
81+
x = [ -4.0, -4.0 ];
82+
v = gsum( x.length, toAccessorArray( x ), 1, 0 );
83+
t.strictEqual( v, -8.0, 'returns expected value' );
84+
85+
x = [ NaN, 4.0 ];
86+
v = gsum( x.length, toAccessorArray( x ), 1, 0 );
87+
t.strictEqual( isnan( v ), true, 'returns expected value' );
88+
89+
x = [ 1.0, 1.0e100, 1.0, -1.0e100 ];
90+
v = gsum( x.length, toAccessorArray( x ), 1, 0 );
91+
t.strictEqual( v, 2.0, 'returns expected value' );
92+
93+
t.end();
94+
});
95+
6896
tape( 'if provided an `N` parameter less than or equal to `0`, the function returns `0.0`', function test( t ) {
6997
var x;
7098
var v;
@@ -113,6 +141,27 @@ tape( 'the function supports a `stride` parameter', function test( t ) {
113141
t.end();
114142
});
115143

144+
tape( 'the function supports a `stride` parameter (accessors)', function test( t ) {
145+
var x;
146+
var v;
147+
148+
x = [
149+
1.0, // 0
150+
2.0,
151+
2.0, // 1
152+
-7.0,
153+
-2.0, // 2
154+
3.0,
155+
4.0, // 3
156+
2.0
157+
];
158+
159+
v = gsum( 4, toAccessorArray( x ), 2, 0 );
160+
161+
t.strictEqual( v, 5.0, 'returns expected value' );
162+
t.end();
163+
});
164+
116165
tape( 'the function supports a negative `stride` parameter', function test( t ) {
117166
var x;
118167
var v;
@@ -134,6 +183,27 @@ tape( 'the function supports a negative `stride` parameter', function test( t )
134183
t.end();
135184
});
136185

186+
tape( 'the function supports a negative `stride` parameter (accessors)', function test( t ) {
187+
var x;
188+
var v;
189+
190+
x = [
191+
1.0, // 3
192+
2.0,
193+
2.0, // 2
194+
-7.0,
195+
-2.0, // 1
196+
3.0,
197+
4.0, // 0
198+
2.0
199+
];
200+
201+
v = gsum( 4, toAccessorArray( x ), -2, 6 );
202+
203+
t.strictEqual( v, 5.0, 'returns expected value' );
204+
t.end();
205+
});
206+
137207
tape( 'if provided a `stride` parameter equal to `0`, the function returns the sum of the first element repeated N times', function test( t ) {
138208
var x;
139209
var v;
@@ -146,6 +216,18 @@ tape( 'if provided a `stride` parameter equal to `0`, the function returns the s
146216
t.end();
147217
});
148218

219+
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 ) {
220+
var x;
221+
var v;
222+
223+
x = [ 1.0, -2.0, -4.0, 5.0, 3.0 ];
224+
225+
v = gsum( x.length, toAccessorArray( x ), 0, 0 );
226+
t.strictEqual( v, 5.0, 'returns expected value' );
227+
228+
t.end();
229+
});
230+
149231
tape( 'the function supports an `offset` parameter', function test( t ) {
150232
var x;
151233
var v;
@@ -166,3 +248,24 @@ tape( 'the function supports an `offset` parameter', function test( t ) {
166248

167249
t.end();
168250
});
251+
252+
tape( 'the function supports an `offset` parameter (accessors)', function test( t ) {
253+
var x;
254+
var v;
255+
256+
x = [
257+
2.0,
258+
1.0, // 0
259+
2.0,
260+
-2.0, // 1
261+
-2.0,
262+
2.0, // 2
263+
3.0,
264+
4.0 // 3
265+
];
266+
267+
v = gsum( 4, toAccessorArray( x ), 2, 1 );
268+
t.strictEqual( v, 5.0, 'returns expected value' );
269+
270+
t.end();
271+
});

0 commit comments

Comments
 (0)