Skip to content

Commit 751fa41

Browse files
headlessNodekgryte
authored andcommitted
feat: add accessor arrays support to blas/ext/base/gcusumkbn2
PR-URL: stdlib-js#5009 Co-authored-by: Athan Reines <[email protected]> Reviewed-by: Athan Reines <[email protected]>
1 parent d836b4d commit 751fa41

File tree

7 files changed

+602
-41
lines changed

7 files changed

+602
-41
lines changed

Diff for: lib/node_modules/@stdlib/blas/ext/base/gcusumkbn2/README.md

+3
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ gcusumkbn2.ndarray( 4, 0.0, x, 2, 1, y, -1, y.length-1 );
128128
## Notes
129129

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

133134
</section>
@@ -197,6 +198,8 @@ console.log( y );
197198

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

201+
[@stdlib/array/base/accessor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/base/accessor
202+
200203
[@klein:2005a]: https://doi.org/10.1007/s00607-005-0139-x
201204

202205
<!-- <related-links> -->

Diff for: lib/node_modules/@stdlib/blas/ext/base/gcusumkbn2/docs/types/index.d.ts

+13-3
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 `gcusumkbn2`.
@@ -44,7 +54,7 @@ interface Routine {
4454
* gcusumkbn2( 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 a second-order iterative Kahan–Babuška algorithm and alternative indexing semantics.
@@ -66,7 +76,7 @@ interface Routine {
6676
* gcusumkbn2.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
/**

Diff for: lib/node_modules/@stdlib/blas/ext/base/gcusumkbn2/docs/types/test.ts

+5-2
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 gcusumkbn2 = require( './index' );
2021

2122

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

29-
gcusumkbn2( x.length, 0.0, x, 1, y, 1 ); // $ExpectType NumericArray
30+
gcusumkbn2( x.length, 0.0, x, 1, y, 1 ); // $ExpectType Float64Array
31+
gcusumkbn2( 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 gcusumkbn2 = require( './index' );
139141
const x = new Float64Array( 10 );
140142
const y = new Float64Array( 10 );
141143

142-
gcusumkbn2.ndarray( x.length, 0.0, x, 1, 0, y, 1, 0 ); // $ExpectType NumericArray
144+
gcusumkbn2.ndarray( x.length, 0.0, x, 1, 0, y, 1, 0 ); // $ExpectType Float64Array
145+
gcusumkbn2.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...
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
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 abs = require( '@stdlib/math/base/special/abs' );
24+
25+
26+
// MAIN //
27+
28+
/**
29+
* Computes the cumulative sum of strided array elements using a second-order iterative Kahan–Babuška algorithm.
30+
*
31+
* ## Method
32+
*
33+
* - This implementation uses a second-order iterative Kahan–Babuška algorithm, as described by Klein (2005).
34+
*
35+
* ## References
36+
*
37+
* - Klein, Andreas. 2005. "A Generalized Kahan-Babuška-Summation-Algorithm." _Computing_ 76 (3): 279–93. doi:[10.1007/s00607-005-0139-x](https://doi.org/10.1007/s00607-005-0139-x).
38+
*
39+
* @private
40+
* @param {PositiveInteger} N - number of indexed elements
41+
* @param {number} sum - initial sum
42+
* @param {Object} x - input array object
43+
* @param {Collection} x.data - input array data
44+
* @param {Array<Function>} x.accessors - array element accessors
45+
* @param {integer} strideX - stride length for `x`
46+
* @param {NonNegativeInteger} offsetX - starting index for `x`
47+
* @param {Object} y - output array object
48+
* @param {Collection} y.data - output array data
49+
* @param {Array<Function>} y.accessors - array element accessors
50+
* @param {integer} strideY - stride length for `y`
51+
* @param {NonNegativeInteger} offsetY - starting index for `y`
52+
* @returns {Object} output array object
53+
*
54+
* @example
55+
* var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' );
56+
* var arraylike2object = require( '@stdlib/array/base/arraylike2object' );
57+
*
58+
* var x = [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ];
59+
* var y = [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ];
60+
*
61+
* gcusumkbn2( 4, 0.0, arraylike2object( toAccessorArray( x ) ), 2, 1, arraylike2object( toAccessorArray( y ) ), 1, 0 );
62+
* // y => [ 1.0, -1.0, 1.0, 5.0, 0.0, 0.0, 0.0, 0.0 ]
63+
*/
64+
function gcusumkbn2( N, sum, x, strideX, offsetX, y, strideY, offsetY ) {
65+
var xbuf;
66+
var ybuf;
67+
var xget;
68+
var yset;
69+
var ccs;
70+
var ix;
71+
var iy;
72+
var cs;
73+
var cc;
74+
var v;
75+
var t;
76+
var c;
77+
var i;
78+
79+
// Cache reference to array data:
80+
xbuf = x.data;
81+
ybuf = y.data;
82+
83+
// Cache reference to the element accessors:
84+
xget = x.accessors[ 0 ];
85+
yset = y.accessors[ 1 ];
86+
87+
ix = offsetX;
88+
iy = offsetY;
89+
ccs = 0.0; // second order correction term for lost low order bits
90+
cs = 0.0; // first order correction term for lost low order bits
91+
for ( i = 0; i < N; i++ ) {
92+
v = xget( xbuf, ix );
93+
t = sum + v;
94+
if ( abs( sum ) >= abs( v ) ) {
95+
c = (sum-t) + v;
96+
} else {
97+
c = (v-t) + sum;
98+
}
99+
sum = t;
100+
t = cs + c;
101+
if ( abs( cs ) >= abs( c ) ) {
102+
cc = (cs-t) + c;
103+
} else {
104+
cc = (c-t) + cs;
105+
}
106+
cs = t;
107+
ccs += cc;
108+
109+
yset( ybuf, iy, sum + cs + ccs );
110+
ix += strideX;
111+
iy += strideY;
112+
}
113+
return y;
114+
}
115+
116+
117+
// EXPORTS //
118+
119+
module.exports = gcusumkbn2;

Diff for: lib/node_modules/@stdlib/blas/ext/base/gcusumkbn2/lib/ndarray.js

+10
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@
2020

2121
// MODULES //
2222

23+
var arraylike2object = require( '@stdlib/array/base/arraylike2object' );
2324
var abs = require( '@stdlib/math/base/special/abs' );
25+
var accessors = require( './accessors.js' );
2426

2527

2628
// MAIN //
@@ -57,6 +59,8 @@ function gcusumkbn2( N, sum, x, strideX, offsetX, y, strideY, offsetY ) {
5759
var ccs;
5860
var ix;
5961
var iy;
62+
var ox;
63+
var oy;
6064
var cs;
6165
var cc;
6266
var v;
@@ -67,6 +71,12 @@ function gcusumkbn2( N, sum, x, strideX, offsetX, y, strideY, offsetY ) {
6771
if ( N <= 0 ) {
6872
return y;
6973
}
74+
ox = arraylike2object( x );
75+
oy = arraylike2object( y );
76+
if ( ox.accessorProtocol || oy.accessorProtocol ) {
77+
accessors( N, sum, ox, strideX, offsetX, oy, strideY, offsetY );
78+
return y;
79+
}
7080
ix = offsetX;
7181
iy = offsetY;
7282

0 commit comments

Comments
 (0)