Skip to content

Commit 9bcaa75

Browse files
kgrytesaurabhraghuvanshii
authored andcommitted
feat: add support for accessor arrays
--- 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: na - task: lint_repl_help status: na - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: na - 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 --- --- type: pre_push_report description: Results of running various checks prior to pushing changes. report: - task: run_javascript_examples status: na - task: run_c_examples status: na - task: run_cpp_examples status: na - task: run_javascript_readme_examples status: passed - task: run_c_benchmarks status: na - task: run_cpp_benchmarks status: na - task: run_fortran_benchmarks status: na - task: run_javascript_benchmarks status: na - task: run_julia_benchmarks status: na - task: run_python_benchmarks status: na - task: run_r_benchmarks status: na - task: run_javascript_tests status: passed ---
1 parent 2f8319a commit 9bcaa75

File tree

7 files changed

+258
-3
lines changed

7 files changed

+258
-3
lines changed

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

+3
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ var v = gsumors.ndarray( 4, x, 2, 1 );
110110

111111
- If `N <= 0`, both functions return `0.0`.
112112
- Ordinary recursive summation (i.e., a "simple" sum) is performant, but can incur significant numerical error. If performance is paramount and error tolerated, using ordinary recursive summation is acceptable; in all other cases, exercise due caution.
113+
- 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]).
113114
- Depending on the environment, the typed versions ([`dsumors`][@stdlib/blas/ext/base/dsumors], [`ssumors`][@stdlib/blas/ext/base/ssumors], etc.) are likely to be significantly more performant.
114115

115116
</section>
@@ -166,6 +167,8 @@ console.log( v );
166167

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

170+
[@stdlib/array/base/accessor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/base/accessor
171+
169172
<!-- <related-links> -->
170173

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

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

2122

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

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

8789
gsumors.ndarray( x.length, x, 1, 0 ); // $ExpectType number
90+
gsumors.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...
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2020 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+
// MAIN //
22+
23+
/**
24+
* Computes the sum of strided array elements using ordinary recursive summation.
25+
*
26+
* @private
27+
* @param {PositiveInteger} N - number of indexed elements
28+
* @param {Object} x - input array object
29+
* @param {Collection} x.data - input array data
30+
* @param {Array<Function>} x.accessors - array element accessors
31+
* @param {integer} strideX - stride length
32+
* @param {NonNegativeInteger} offsetX - starting index
33+
* @returns {number} sum
34+
*
35+
* @example
36+
* var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' );
37+
* var arraylike2object = require( '@stdlib/array/base/arraylike2object' );
38+
*
39+
* var x = toAccessorArray( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ] );
40+
*
41+
* var v = gsumors( 4, arraylike2object( x ), 2, 1 );
42+
* // returns 5.0
43+
*/
44+
function gsumors( N, x, strideX, offsetX ) {
45+
var xbuf;
46+
var get;
47+
var ix;
48+
var s;
49+
var i;
50+
51+
// Cache reference to array data:
52+
xbuf = x.data;
53+
54+
// Cache a reference to the element accessor:
55+
get = x.accessors[ 0 ];
56+
57+
ix = offsetX;
58+
if ( strideX === 0 ) {
59+
return N * get( xbuf, ix );
60+
}
61+
s = 0.0;
62+
for ( i = 0; i < N; i++ ) {
63+
s += get( xbuf, ix );
64+
ix += strideX;
65+
}
66+
return s;
67+
}
68+
69+
70+
// EXPORTS //
71+
72+
module.exports = gsumors;

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

+11
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@
1818

1919
'use strict';
2020

21+
// MODULES //
22+
23+
var arraylike2object = require( '@stdlib/array/base/arraylike2object' );
24+
var accessors = require( './accessors.js' );
25+
26+
2127
// VARIABLES //
2228

2329
var M = 6;
@@ -44,12 +50,17 @@ function gsumors( N, x, strideX, offsetX ) {
4450
var ix;
4551
var m;
4652
var s;
53+
var o;
4754
var i;
4855

4956
s = 0.0;
5057
if ( N <= 0 ) {
5158
return s;
5259
}
60+
o = arraylike2object( x );
61+
if ( o.accessorProtocol ) {
62+
return accessors( N, o, strideX, offsetX );
63+
}
5364
ix = offsetX;
5465
if ( strideX === 0 ) {
5566
return N * x[ ix ];

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

+70
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 gsumors = 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 = gsumors( 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 = gsumors( x.length, toAccessorArray( x ), 1 );
80+
t.strictEqual( v, 3.0, 'returns expected value' );
81+
82+
x = [ -4.0, -4.0 ];
83+
v = gsumors( x.length, toAccessorArray( x ), 1 );
84+
t.strictEqual( v, -8.0, 'returns expected value' );
85+
86+
x = [ NaN, 4.0 ];
87+
v = gsumors( 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 = gsumors( x.length, toAccessorArray( x ), 1 );
92+
t.strictEqual( v, 0.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 = gsumors( 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 = gsumors( 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;

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

+91
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 gsumors = 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 (accessors)', 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 = gsumors( 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 = gsumors( x.length, toAccessorArray( x ), 1, 0 );
79+
t.strictEqual( v, 3.0, 'returns expected value' );
80+
81+
x = [ -4.0, -4.0 ];
82+
v = gsumors( x.length, toAccessorArray( x ), 1, 0 );
83+
t.strictEqual( v, -8.0, 'returns expected value' );
84+
85+
x = [ NaN, 4.0 ];
86+
v = gsumors( 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 = gsumors( x.length, toAccessorArray( x ), 1, 0 );
91+
t.strictEqual( v, 0.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 = gsumors( 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 = gsumors( 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;
@@ -166,3 +236,24 @@ tape( 'the function supports an `offset` parameter', function test( t ) {
166236

167237
t.end();
168238
});
239+
240+
tape( 'the function supports an `offset` parameter (accessors)', function test( t ) {
241+
var x;
242+
var v;
243+
244+
x = [
245+
2.0,
246+
1.0, // 0
247+
2.0,
248+
-2.0, // 1
249+
-2.0,
250+
2.0, // 2
251+
3.0,
252+
4.0 // 3
253+
];
254+
255+
v = gsumors( 4, toAccessorArray( x ), 2, 1 );
256+
t.strictEqual( v, 5.0, 'returns expected value' );
257+
258+
t.end();
259+
});

0 commit comments

Comments
 (0)