Skip to content

Commit 8310c1f

Browse files
authored
feat: add accessor arrays support to blas/ext/base/gcusumors
PR-URL: #5010 Reviewed-by: Athan Reines <[email protected]>
1 parent 4714429 commit 8310c1f

File tree

7 files changed

+567
-35
lines changed

7 files changed

+567
-35
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ gcusumors.ndarray( 4, 0.0, x, 2, 1, y, -1, y.length-1 );
129129

130130
- If `N <= 0`, both functions return `y` unchanged.
131131
- 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.
132+
- 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])
132133
- Depending on the environment, the typed versions ([`dcusumors`][@stdlib/blas/ext/base/dcusumors], [`scusumors`][@stdlib/blas/ext/base/scusumors], etc.) are likely to be significantly more performant.
133134

134135
</section>
@@ -190,6 +191,8 @@ console.log( y );
190191

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

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

195198
<!-- <related-links> -->

lib/node_modules/@stdlib/blas/ext/base/gcusumors/docs/types/index.d.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,17 @@
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>;
29+
30+
/**
31+
* Output array.
32+
*/
33+
type OutputArray = NumericArray | Collection<number> | AccessorArrayLike<number>;
2434

2535
/**
2636
* Interface describing `gcusumors`.
@@ -44,7 +54,7 @@ interface Routine {
4454
* gcusumors( x.length, 0.0, x, 1, y, 1 );
4555
* // y => [ 1.0, -1.0, 1.0 ]
4656
*/
47-
( N: number, sum: number, x: NumericArray, strideX: number, y: NumericArray, strideY: number ): NumericArray;
57+
<T extends OutputArray>( N: number, sum: number, x: InputArray, strideX: number, y: T, strideY: number ): T;
4858

4959
/**
5060
* Computes the cumulative sum of strided array elements using ordinary recursive summation and alternative indexing semantics.
@@ -66,7 +76,7 @@ interface Routine {
6676
* gcusumors.ndarray( x.length, 0.0, x, 1, 0, y, 1, 0 );
6777
* // y => [ 1.0, -1.0, 1.0 ]
6878
*/
69-
ndarray( N: number, sum: number, x: NumericArray, strideX: number, offsetX: number, y: NumericArray, strideY: number, offsetY: number ): NumericArray;
79+
ndarray<T extends OutputArray>( N: number, sum: number, x: InputArray, strideX: number, offsetX: number, y: T, strideY: number, offsetY: number ): T;
7080
}
7181

7282
/**

lib/node_modules/@stdlib/blas/ext/base/gcusumors/docs/types/test.ts

Lines changed: 5 additions & 2 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 gcusumors = require( './index' );
2021

2122

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

29-
gcusumors( x.length, 0.0, x, 1, y, 1 ); // $ExpectType NumericArray
30+
gcusumors( x.length, 0.0, x, 1, y, 1 ); // $ExpectType Float64Array
31+
gcusumors( x.length, 0.0, new AccessorArray( x ), 1, new AccessorArray( y ), 1 ); // $ExpectType AccessorArray<number>
3032
}
3133

3234
// The compiler throws an error if the function is provided a first argument which is not a number...
@@ -139,7 +141,8 @@ import gcusumors = require( './index' );
139141
const x = new Float64Array( 10 );
140142
const y = new Float64Array( 10 );
141143

142-
gcusumors.ndarray( x.length, 0.0, x, 1, 0, y, 1, 0 ); // $ExpectType NumericArray
144+
gcusumors.ndarray( x.length, 0.0, x, 1, 0, y, 1, 0 ); // $ExpectType Float64Array
145+
gcusumors.ndarray( x.length, 0.0, new AccessorArray( x ), 1, 0, new AccessorArray( y ), 1, 0 ); // $ExpectType AccessorArray<number>
143146
}
144147

145148
// The compiler throws an error if the `ndarray` method is provided a first argument which is not a number...
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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+
// MAIN //
22+
23+
/**
24+
* Computes the cumulative sum of strided array elements using ordinary recursive summation.
25+
*
26+
* @private
27+
* @param {PositiveInteger} N - number of indexed elements
28+
* @param {number} sum - initial sum
29+
* @param {Object} x - input array object
30+
* @param {Collection} x.data - input array data
31+
* @param {Array<Function>} x.accessors - array element accessors
32+
* @param {integer} strideX - stride length for `x`
33+
* @param {NonNegativeInteger} offsetX - starting index for `x`
34+
* @param {Object} y - output array object
35+
* @param {Collection} y.data - output array data
36+
* @param {Array<Function>} y.accessors - array element accessors
37+
* @param {integer} strideY - stride length for `y`
38+
* @param {NonNegativeInteger} offsetY - starting index for `y`
39+
* @returns {Object} output array object
40+
*
41+
* @example
42+
* var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' );
43+
* var arraylike2object = require( '@stdlib/array/base/arraylike2object' );
44+
*
45+
* var x = [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ];
46+
* var y = [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ];
47+
*
48+
* gcusumors( 4, 0.0, arraylike2object( toAccessorArray( x ) ), 2, 1, arraylike2object( toAccessorArray( y ) ), 1, 0 );
49+
* // y => [ 1.0, -1.0, 1.0, 5.0, 0.0, 0.0, 0.0, 0.0 ]
50+
*/
51+
function gcusumors( N, sum, x, strideX, offsetX, y, strideY, offsetY ) {
52+
var xbuf;
53+
var ybuf;
54+
var xget;
55+
var yset;
56+
var ix;
57+
var iy;
58+
var i;
59+
60+
// Cache reference to array data:
61+
xbuf = x.data;
62+
ybuf = y.data;
63+
64+
// Cache reference to the element accessors:
65+
xget = x.accessors[ 0 ];
66+
yset = y.accessors[ 1 ];
67+
68+
ix = offsetX;
69+
iy = offsetY;
70+
for ( i = 0; i < N; i++ ) {
71+
sum += xget( xbuf, ix );
72+
yset( ybuf, iy, sum );
73+
ix += strideX;
74+
iy += strideY;
75+
}
76+
return y;
77+
}
78+
79+
80+
// EXPORTS //
81+
82+
module.exports = gcusumors;

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

Lines changed: 14 additions & 0 deletions
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
// MAIN //
2228

2329
/**
@@ -43,11 +49,19 @@
4349
function gcusumors( N, sum, x, strideX, offsetX, y, strideY, offsetY ) {
4450
var ix;
4551
var iy;
52+
var ox;
53+
var oy;
4654
var i;
4755

4856
if ( N <= 0 ) {
4957
return y;
5058
}
59+
ox = arraylike2object( x );
60+
oy = arraylike2object( y );
61+
if ( ox.accessorProtocol || oy.accessorProtocol ) {
62+
accessors( N, sum, ox, strideX, offsetX, oy, strideY, offsetY );
63+
return y;
64+
}
5165
ix = offsetX;
5266
iy = offsetY;
5367
for ( i = 0; i < N; i++ ) {

0 commit comments

Comments
 (0)