Skip to content

Commit f572d78

Browse files
headlessNodekgryte
andauthored
feat: add accessor array support to blas/base/gnrm2
PR-URL: #5778 Closes: stdlib-js/metr-issue-tracker#26 Co-authored-by: Athan Reines <[email protected]> Reviewed-by: Athan Reines <[email protected]> Signed-off-by: Athan Reines <[email protected]>
1 parent 460cd8f commit f572d78

File tree

10 files changed

+343
-74
lines changed

10 files changed

+343
-74
lines changed

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

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ var gnrm2 = require( '@stdlib/blas/base/gnrm2' );
4949

5050
#### gnrm2( N, x, stride )
5151

52-
Computes the [L2-norm][l2-norm] of a vector `x`.
52+
Computes the [L2-norm][l2-norm] of a vector.
5353

5454
```javascript
5555
var x = [ 1.0, -2.0, 2.0 ];
@@ -62,9 +62,9 @@ The function has the following parameters:
6262

6363
- **N**: number of indexed elements.
6464
- **x**: input [`Array`][mdn-array] or [`typed array`][mdn-typed-array].
65-
- **stride**: index increment for `x`.
65+
- **stride**: stride length.
6666

67-
The `N` and `stride` parameters determine which elements in `x` are accessed at runtime. For example, to compute the [L2-norm][l2-norm] of every other element in `x`,
67+
The `N` and stride parameters determine which elements in the strided array are accessed at runtime. For example, to compute the [L2-norm][l2-norm] of every other element:
6868

6969
```javascript
7070
var x = [ 1.0, 2.0, 2.0, -7.0, -2.0, 3.0, 4.0, 2.0 ];
@@ -87,7 +87,7 @@ var z = gnrm2( 4, x1, 2 );
8787
// returns 5.0
8888
```
8989

90-
If either `N` or `stride` is less than or equal to `0`, the function returns `0`.
90+
If `N` is less than or equal to `0`, the function returns `0`.
9191

9292
#### gnrm2.ndarray( N, x, stride, offset )
9393

@@ -102,9 +102,9 @@ var z = gnrm2.ndarray( x.length, x, 1, 0 );
102102

103103
The function has the following additional parameters:
104104

105-
- **offset**: starting index for `x`.
105+
- **offset**: starting index.
106106

107-
While [`typed array`][mdn-typed-array] views mandate a view offset based on the underlying buffer, the `offset` parameter supports indexing semantics based on a starting index. For example, to calculate the [L2-norm][l2-norm] for every other value in `x` starting from the second value
107+
While [`typed array`][mdn-typed-array] views mandate a view offset based on the underlying buffer, the offset parameter supports indexing semantics based on a starting index. For example, to calculate the [L2-norm][l2-norm] for every other value in the strided array starting from the second value:
108108

109109
```javascript
110110
var x = [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ];
@@ -123,6 +123,7 @@ var z = gnrm2.ndarray( 4, x, 2, 1 );
123123

124124
- If `N <= 0`, both functions return `0.0`.
125125
- `gnrm2()` corresponds to the [BLAS][blas] level 1 function [`dnrm2`][dnrm2] with the exception that this implementation works with any array type, not just Float64Arrays. Depending on the environment, the typed versions ([`dnrm2`][@stdlib/blas/base/dnrm2], [`snrm2`][@stdlib/blas/base/snrm2], etc.) are likely to be significantly more performant.
126+
- 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]).
126127

127128
</section>
128129

@@ -181,6 +182,8 @@ console.log( out );
181182

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

185+
[@stdlib/array/base/accessor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/base/accessor
186+
184187
[@stdlib/blas/base/dnrm2]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/blas/base/dnrm2
185188

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

lib/node_modules/@stdlib/blas/base/gnrm2/docs/repl.txt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
{{alias}}( N, x, stride )
33
Computes the L2-norm of a vector.
44

5-
The `N` and `stride` parameters determine which elements in `x` are accessed
6-
at runtime.
5+
The `N` and stride parameters determine which elements in the strided array
6+
are accessed at runtime.
77

88
Indexing is relative to the first index. To introduce an offset, use a typed
99
array view.
1010

11-
If `N <= 0` or `stride <= 0`, the function returns `0`.
11+
If `N <= 0`, the function returns `0`.
1212

1313
Parameters
1414
----------
@@ -19,7 +19,7 @@
1919
Input array.
2020

2121
stride: integer
22-
Index increment.
22+
Stride length.
2323

2424
Returns
2525
-------
@@ -49,8 +49,8 @@
4949
Computes the L2-norm of a vector using alternative indexing semantics.
5050

5151
While typed array views mandate a view offset based on the underlying
52-
buffer, the `offset` parameter supports indexing semantics based on a
53-
starting index.
52+
buffer, the offset parameter supports indexing semantics based on a starting
53+
index.
5454

5555
Parameters
5656
----------
@@ -61,7 +61,7 @@
6161
Input array.
6262

6363
stride: integer
64-
Index increment.
64+
Stride length.
6565

6666
offset: integer
6767
Starting index.

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

Lines changed: 8 additions & 3 deletions
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 `gnrm2`.
@@ -40,7 +45,7 @@ interface Routine {
4045
* var z = gnrm2( x.length, x, 1 );
4146
* // returns 3.0
4247
*/
43-
( N: number, x: NumericArray, stride: number ): number;
48+
( N: number, x: InputArray, stride: number ): number;
4449

4550
/**
4651
* Computes the L2-norm of a vector using alternative indexing semantics.
@@ -57,7 +62,7 @@ interface Routine {
5762
* var z = gnrm2.ndarray( x.length, x, 1, 0 );
5863
* // returns 3.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/gnrm2/docs/types/test.ts

Lines changed: 3 additions & 0 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 gnrm2 = require( './index' );
2021

2122

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

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

8789
gnrm2.ndarray( x.length, x, 1, 0 ); // $ExpectType number
90+
gnrm2.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...
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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 sqrt = require( '@stdlib/math/base/special/sqrt' );
24+
var abs = require( '@stdlib/math/base/special/abs' );
25+
var pow = require( '@stdlib/math/base/special/pow' );
26+
27+
28+
// MAIN //
29+
30+
/**
31+
* Computes the L2-norm of a vector.
32+
*
33+
* @private
34+
* @param {PositiveInteger} N - number of indexed elements
35+
* @param {Object} x - input array object
36+
* @param {Collection} x.data - input array data
37+
* @param {Array<Function>} x.accessors - array element accessors
38+
* @param {integer} stride - stride length
39+
* @param {NonNegativeInteger} offset - starting index
40+
* @returns {number} L2-norm
41+
*
42+
* @example
43+
* var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' );
44+
* var arraylike2object = require( '@stdlib/array/base/arraylike2object' );
45+
*
46+
* var x = [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ];
47+
*
48+
* var z = gnrm2( 4, arraylike2object( toAccessorArray( x ) ), 2, 1 );
49+
* // returns 5.0
50+
*/
51+
function gnrm2( N, x, stride, offset ) {
52+
var scale;
53+
var buf;
54+
var get;
55+
var ssq;
56+
var ax;
57+
var ix;
58+
var i;
59+
60+
buf = x.data;
61+
get = x.accessors[ 0 ];
62+
63+
ix = offset;
64+
if ( N === 1 ) {
65+
return abs( get( buf, ix ) );
66+
}
67+
if ( stride === 0 ) {
68+
return sqrt( N ) * abs( get( buf, ix ) );
69+
}
70+
scale = 0.0;
71+
ssq = 1.0;
72+
for ( i = 0; i < N; i++ ) {
73+
if ( get( buf, ix ) !== 0.0 ) {
74+
ax = abs( get( buf, ix ) );
75+
if ( scale < ax ) {
76+
ssq = 1.0 + ( ssq * pow( scale/ax, 2 ) );
77+
scale = ax;
78+
} else {
79+
ssq += pow( ax/scale, 2 );
80+
}
81+
}
82+
ix += stride;
83+
}
84+
return scale * sqrt( ssq );
85+
}
86+
87+
88+
// EXPORTS //
89+
90+
module.exports = gnrm2;

lib/node_modules/@stdlib/blas/base/gnrm2/lib/index.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,11 @@
3232
* // returns 3.0
3333
*
3434
* @example
35-
* var floor = require( '@stdlib/math/base/special/floor' );
3635
* var gnrm2 = require( '@stdlib/blas/base/gnrm2' );
3736
*
3837
* var x = [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ];
39-
* var N = floor( x.length / 2 );
4038
*
41-
* var z = gnrm2.ndarray( N, x, 2, 1 );
39+
* var z = gnrm2.ndarray( 4, x, 2, 1 );
4240
* // returns 5.0
4341
*/
4442

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

Lines changed: 3 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,8 @@
2020

2121
// MODULES //
2222

23-
var sqrt = require( '@stdlib/math/base/special/sqrt' );
24-
var abs = require( '@stdlib/math/base/special/abs' );
25-
var pow = require( '@stdlib/math/base/special/pow' );
23+
var stride2offset = require( '@stdlib/strided/base/stride2offset' );
24+
var ndarray = require( './ndarray.js' );
2625

2726

2827
// MAIN //
@@ -42,32 +41,7 @@ var pow = require( '@stdlib/math/base/special/pow' );
4241
* // returns 3.0
4342
*/
4443
function gnrm2( N, x, stride ) {
45-
var scale;
46-
var ssq;
47-
var ax;
48-
var i;
49-
50-
if ( N <= 0 || stride <= 0 ) {
51-
return 0.0;
52-
}
53-
if ( N === 1 ) {
54-
return abs( x[ 0 ] );
55-
}
56-
scale = 0.0;
57-
ssq = 1.0;
58-
N *= stride;
59-
for ( i = 0; i < N; i += stride ) {
60-
if ( x[ i ] !== 0.0 ) {
61-
ax = abs( x[ i ] );
62-
if ( scale < ax ) {
63-
ssq = 1.0 + ( ssq * pow( scale/ax, 2 ) );
64-
scale = ax;
65-
} else {
66-
ssq += pow( ax/scale, 2 );
67-
}
68-
}
69-
}
70-
return scale * sqrt( ssq );
44+
return ndarray( N, x, stride, stride2offset( N, stride ) );
7145
}
7246

7347

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

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,11 @@
2020

2121
// MODULES //
2222

23+
var arraylike2object = require( '@stdlib/array/base/arraylike2object' );
2324
var sqrt = require( '@stdlib/math/base/special/sqrt' );
2425
var abs = require( '@stdlib/math/base/special/abs' );
2526
var pow = require( '@stdlib/math/base/special/pow' );
27+
var accessors = require( './accessors.js' );
2628

2729

2830
// MAIN //
@@ -37,28 +39,35 @@ var pow = require( '@stdlib/math/base/special/pow' );
3739
* @returns {number} L2-norm
3840
*
3941
* @example
40-
* var floor = require( '@stdlib/math/base/special/floor' );
41-
*
4242
* var x = [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ];
43-
* var N = floor( x.length / 2 );
4443
*
45-
* var z = gnrm2( N, x, 2, 1 );
44+
* var z = gnrm2( 4, x, 2, 1 );
4645
* // returns 5.0
4746
*/
4847
function gnrm2( N, x, stride, offset ) {
4948
var scale;
5049
var ssq;
5150
var ax;
5251
var ix;
52+
var o;
5353
var i;
5454

5555
if ( N <= 0 ) {
5656
return 0.0;
5757
}
58-
if ( N === 1 ) {
59-
return abs( x[ offset ] );
58+
59+
o = arraylike2object( x );
60+
if ( o.accessorProtocol ) {
61+
return accessors( N, o, stride, offset );
6062
}
63+
6164
ix = offset;
65+
if ( N === 1 ) {
66+
return abs( x[ ix ] );
67+
}
68+
if ( stride === 0 ) {
69+
return sqrt( N ) * abs( x[ ix ] );
70+
}
6271
scale = 0.0;
6372
ssq = 1.0;
6473
for ( i = 0; i < N; i++ ) {

0 commit comments

Comments
 (0)