Skip to content

Commit 3db1791

Browse files
committed
feat: add accessor array support
--- 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: passed - 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: na - task: lint_typescript_tests status: na - 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: na - 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 aab2efb commit 3db1791

File tree

8 files changed

+326
-71
lines changed

8 files changed

+326
-71
lines changed

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

Lines changed: 6 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` parameter 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 ];

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.
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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+
* @param {PositiveInteger} N - number of indexed elements
34+
* @param {Object} x - input array object
35+
* @param {Collection} x.data - input array data
36+
* @param {Array<Function>} x.accessors - array element accessors
37+
* @param {integer} stride - stride length
38+
* @param {NonNegativeInteger} offset - starting index
39+
* @returns {number} L2-norm
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+
*
47+
* var z = gnrm2( 4, arraylike2object( toAccessorArray( x ) ), 2, 1 );
48+
* // returns 5.0
49+
*/
50+
function gnrm2( N, x, stride, offset ) {
51+
var scale;
52+
var xbuf;
53+
var xget;
54+
var ssq;
55+
var ax;
56+
var ix;
57+
var i;
58+
59+
xbuf = x.data;
60+
xget = x.accessors[ 0 ];
61+
62+
ix = offset;
63+
if ( N === 1 ) {
64+
return abs( xget( xbuf, ix ) );
65+
}
66+
if ( stride === 0 ) {
67+
return abs( N * xget( xbuf, ix ) );
68+
}
69+
scale = 0.0;
70+
ssq = 1.0;
71+
for ( i = 0; i < N; i++ ) {
72+
if ( xget( xbuf, ix ) !== 0.0 ) {
73+
ax = abs( xget( xbuf, ix ) );
74+
if ( scale < ax ) {
75+
ssq = 1.0 + ( ssq * pow( scale/ax, 2 ) );
76+
scale = ax;
77+
} else {
78+
ssq += pow( ax/scale, 2 );
79+
}
80+
}
81+
ix += stride;
82+
}
83+
return scale * sqrt( ssq );
84+
}
85+
86+
87+
// EXPORTS //
88+
89+
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 abs( N * x[ ix ] );
70+
}
6271
scale = 0.0;
6372
ssq = 1.0;
6473
for ( i = 0; i < N; i++ ) {

0 commit comments

Comments
 (0)