Skip to content

Commit 57184e0

Browse files
authored
feat: add accessor array support to blas/base/gasum
PR-URL: #5439 Closes: stdlib-js/metr-issue-tracker#22 Reviewed-by: Athan Reines <[email protected]>
1 parent 2d26990 commit 57184e0

File tree

8 files changed

+415
-78
lines changed

8 files changed

+415
-78
lines changed

lib/node_modules/@stdlib/blas/base/gasum/README.md

+3
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ sum = gasum.ndarray( 3, x, -1, x.length-1 );
135135

136136
- If `N <= 0`, both functions return `0`.
137137
- `gasum()` corresponds to the [BLAS][blas] level 1 function [`dasum`][dasum] with the exception that this implementation works with any array type, not just Float64Arrays. Depending on the environment, the typed versions ([`dasum`][@stdlib/blas/base/dasum], [`sasum`][@stdlib/blas/base/sasum], etc.) are likely to be significantly more performant.
138+
- 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]).
138139

139140
</section>
140141

@@ -199,6 +200,8 @@ console.log( y );
199200

200201
[@stdlib/blas/base/sasum]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/blas/base/sasum
201202

203+
[@stdlib/array/base/accessor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/base/accessor
204+
202205
<!-- <related-links> -->
203206

204207
<!-- </related-links> -->

lib/node_modules/@stdlib/blas/base/gasum/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 `gasum`.
@@ -40,7 +45,7 @@ interface Routine {
4045
* var z = gasum( x.length, x, 1 );
4146
* // returns 15.0
4247
*/
43-
( N: number, x: NumericArray, stride: number ): number;
48+
( N: number, x: InputArray, stride: number ): number;
4449

4550
/**
4651
* Computes the sum of the absolute values using alternative indexing semantics.
@@ -57,7 +62,7 @@ interface Routine {
5762
* var z = gasum.ndarray( x.length, x, 1, 0 );
5863
* // returns 21.0
5964
*/
60-
ndarray( N: number, x: NumericArray, stride: number, offset: number ): number;
65+
ndarray( N: number, x: InputArray, stride: number, offset: number ): number;
6166
}
6267

6368
/**

lib/node_modules/@stdlib/blas/base/gasum/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 gasum = require( './index' );
2021

2122

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

2829
gasum( x.length, x, 1 ); // $ExpectType number
30+
gasum( 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...
@@ -86,6 +88,7 @@ import gasum = require( './index' );
8688
const x = new Float64Array( 10 );
8789

8890
gasum.ndarray( x.length, x, 1, 0 ); // $ExpectType number
91+
gasum.ndarray( x.length, new AccessorArray( x ), 1, 0 ); // $ExpectType number
8992
}
9093

9194
// 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,101 @@
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+
// VARIABLES //
27+
28+
var M = 6;
29+
30+
31+
// MAIN //
32+
33+
/**
34+
* Computes the sum of absolute values.
35+
*
36+
* @param {PositiveInteger} N - number of indexed elements
37+
* @param {Object} x - input array object
38+
* @param {Collection} x.data - input array data
39+
* @param {Array<Function>} x.accessors - array element accessors
40+
* @param {integer} stride - index increment
41+
* @param {NonNegativeInteger} offset - starting index
42+
* @returns {number} sum
43+
*
44+
* @example
45+
* var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' );
46+
* var arraylike2object = require( '@stdlib/array/base/arraylike2object' );
47+
*
48+
* var x = [ 1.0, -2.0, 3.0, -4.0, 5.0 ];
49+
*
50+
* var s = gasum( x.length, arraylike2object( toAccessorArray( x ) ), 1, 0 );
51+
* // returns 15.0
52+
*/
53+
function gasum( N, x, stride, offset ) {
54+
var buf;
55+
var get;
56+
var sum;
57+
var ix;
58+
var m;
59+
var i;
60+
61+
buf = x.data;
62+
get = x.accessors[ 0 ];
63+
64+
sum = 0.0;
65+
ix = offset;
66+
if ( stride === 0 ) {
67+
sum = abs( get( buf, ix ) * N );
68+
return sum;
69+
}
70+
71+
// Use unrolled loops if the stride is equal to `1`...
72+
if ( stride === 1 ) {
73+
m = N % M;
74+
75+
// If we have a remainder, run a clean-up loop...
76+
if ( m > 0 ) {
77+
for ( i = 0; i < m; i++ ) {
78+
sum += abs( get( buf, ix ) );
79+
ix += stride;
80+
}
81+
}
82+
if ( N < M ) {
83+
return sum;
84+
}
85+
for ( i = m; i < N; i += M ) {
86+
sum += abs( get( buf, ix ) ) + abs( get( buf, ix+1 ) ) + abs( get( buf, ix+2 ) ) + abs( get( buf, ix+3 ) ) + abs( get( buf, ix+4 ) ) + abs( get( buf, ix+5 ) ); // eslint-disable-line max-len
87+
ix += M;
88+
}
89+
return sum;
90+
}
91+
for ( i = 0; i < N; i++ ) {
92+
sum += abs( get( buf, ix ) );
93+
ix += stride;
94+
}
95+
return sum;
96+
}
97+
98+
99+
// EXPORTS //
100+
101+
module.exports = gasum;

lib/node_modules/@stdlib/blas/base/gasum/lib/main.js

+4-38
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,8 @@
2020

2121
// MODULES //
2222

23-
var abs = require( '@stdlib/math/base/special/abs' );
24-
25-
26-
// VARIABLES //
27-
28-
var M = 6;
23+
var stride2offset = require( '@stdlib/strided/base/stride2offset' );
24+
var ndarray = require( './ndarray.js' );
2925

3026

3127
// MAIN //
@@ -42,40 +38,10 @@ var M = 6;
4238
* var x = new Float64Array( [ 1.0, -2.0, 3.0, -4.0, 5.0 ] );
4339
*
4440
* var s = gasum( x.length, x, 1 );
45-
* // 15.0
41+
* // returns 15.0
4642
*/
4743
function gasum( N, x, stride ) {
48-
var sum;
49-
var m;
50-
var i;
51-
52-
sum = 0.0;
53-
if ( N <= 0 || stride <= 0 ) {
54-
return sum;
55-
}
56-
// Use unrolled loops if the stride is equal to `1`...
57-
if ( stride === 1 ) {
58-
m = N % M;
59-
60-
// If we have a remainder, run a clean-up loop...
61-
if ( m > 0 ) {
62-
for ( i = 0; i < m; i++ ) {
63-
sum += abs( x[i] );
64-
}
65-
}
66-
if ( N < M ) {
67-
return sum;
68-
}
69-
for ( i = m; i < N; i += M ) {
70-
sum += abs(x[i]) + abs(x[i+1]) + abs(x[i+2]) + abs(x[i+3]) + abs(x[i+4]) + abs(x[i+5]); // eslint-disable-line max-len
71-
}
72-
return sum;
73-
}
74-
N *= stride;
75-
for ( i = 0; i < N; i += stride ) {
76-
sum += abs( x[i] );
77-
}
78-
return sum;
44+
return ndarray( N, x, stride, stride2offset( N, stride ) );
7945
}
8046

8147

lib/node_modules/@stdlib/blas/base/gasum/lib/ndarray.js

+13-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
// MODULES //
2222

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

2527

2628
// VARIABLES //
@@ -43,19 +45,29 @@ var M = 6;
4345
* var x = new Float64Array( [ 1.0, -2.0, 3.0, -4.0, 5.0 ] );
4446
*
4547
* var s = gasum( x.length, x, 1, 0 );
46-
* // 15.0
48+
* // returns 15.0
4749
*/
4850
function gasum( N, x, stride, offset ) {
4951
var sum;
5052
var ix;
5153
var m;
5254
var i;
55+
var o;
5356

5457
sum = 0.0;
5558
if ( N <= 0 ) {
5659
return sum;
5760
}
61+
o = arraylike2object( x );
62+
if ( o.accessorProtocol ) {
63+
return accessors( N, o, stride, offset );
64+
}
65+
5866
ix = offset;
67+
if ( stride === 0 ) {
68+
sum = abs( x[ix] * N );
69+
return sum;
70+
}
5971

6072
// Use unrolled loops if the stride is equal to `1`...
6173
if ( stride === 1 ) {

0 commit comments

Comments
 (0)