Skip to content

Commit f4e4e04

Browse files
authored
feat: add blas/base/ndarray/snrm2
PR-URL: #12156 Reviewed-by: Athan Reines <kgryte@gmail.com>
1 parent b28f7b8 commit f4e4e04

11 files changed

Lines changed: 783 additions & 0 deletions

File tree

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2026 The Stdlib Authors.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
19+
-->
20+
21+
# snrm2
22+
23+
> Compute the L2-norm of a one-dimensional single-precision floating-point ndarray.
24+
25+
<section class="intro">
26+
27+
The [L2-norm][l2-norm] is defined as
28+
29+
<!-- <equation class="equation" label="eq:l2_norm" align="center" raw="\|\mathbf{x}\|_2 = \sqrt{x_0^2 + x_1^2 + \ldots + x_{N-1}^2}" alt="L2-norm definition."> -->
30+
31+
```math
32+
\|\mathbf{x}\|_2 = \sqrt{x_0^2 + x_1^2 + \ldots + x_{N-1}^2}
33+
```
34+
35+
<!-- <div class="equation" align="center" data-raw-text="\|\mathbf{x}\|_2 = \sqrt{x_0^2 + x_1^2 + \ldots + x_{N-1}^2}" data-equation="eq:l2_norm">
36+
<img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@f766d7eeb56ff14cbceeeeef03d7f7b88c467515/lib/node_modules/@stdlib/blas/base/snrm2/docs/img/equation_l2_norm.svg" alt="L2-norm definition.">
37+
<br>
38+
</div> -->
39+
40+
<!-- </equation> -->
41+
42+
</section>
43+
44+
<!-- /.intro -->
45+
46+
<section class="usage">
47+
48+
## Usage
49+
50+
```javascript
51+
var snrm2 = require( '@stdlib/blas/base/ndarray/snrm2' );
52+
```
53+
54+
#### snrm2( arrays )
55+
56+
Computes the [L2-norm][l2-norm] of a one-dimensional single-precision floating-point ndarray.
57+
58+
```javascript
59+
var Float32Vector = require( '@stdlib/ndarray/vector/float32' );
60+
61+
var x = new Float32Vector( [ 1.0, -2.0, 2.0 ] );
62+
63+
var y = snrm2( [ x ] );
64+
// returns 3.0
65+
```
66+
67+
The function has the following parameters:
68+
69+
- **arrays**: array-like object containing the following ndarrays:
70+
71+
- a one-dimensional input ndarray.
72+
73+
</section>
74+
75+
<!-- /.usage -->
76+
77+
<section class="notes">
78+
79+
</section>
80+
81+
<!-- /.notes -->
82+
83+
<section class="examples">
84+
85+
## Examples
86+
87+
<!-- eslint no-undef: "error" -->
88+
89+
```javascript
90+
var discreteUniform = require( '@stdlib/random/discrete-uniform' );
91+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
92+
var snrm2 = require( '@stdlib/blas/base/ndarray/snrm2' );
93+
94+
var opts = {
95+
'dtype': 'float32'
96+
};
97+
98+
var x = discreteUniform( [ 10 ], -500, 500, opts );
99+
console.log( ndarray2array( x ) );
100+
101+
var out = snrm2( [ x ] );
102+
console.log( out );
103+
```
104+
105+
</section>
106+
107+
<!-- /.examples -->
108+
109+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
110+
111+
<section class="related">
112+
113+
</section>
114+
115+
<!-- /.related -->
116+
117+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
118+
119+
<section class="links">
120+
121+
[l2-norm]: https://en.wikipedia.org/wiki/Euclidean_distance
122+
123+
</section>
124+
125+
<!-- /.links -->
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 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 bench = require( '@stdlib/bench' );
24+
var uniform = require( '@stdlib/random/uniform' );
25+
var isnanf = require( '@stdlib/math/base/assert/is-nanf' );
26+
var pow = require( '@stdlib/math/base/special/pow' );
27+
var format = require( '@stdlib/string/format' );
28+
var pkg = require( './../package.json' ).name;
29+
var snrm2 = require( './../lib' );
30+
31+
32+
// VARIABLES //
33+
34+
var options = {
35+
'dtype': 'float32'
36+
};
37+
38+
39+
// FUNCTIONS //
40+
41+
/**
42+
* Creates a benchmark function.
43+
*
44+
* @private
45+
* @param {PositiveInteger} len - array length
46+
* @returns {Function} benchmark function
47+
*/
48+
function createBenchmark( len ) {
49+
var x = uniform( [ len ], -100.0, 100.0, options );
50+
return benchmark;
51+
52+
/**
53+
* Benchmark function.
54+
*
55+
* @private
56+
* @param {Benchmark} b - benchmark instance
57+
*/
58+
function benchmark( b ) {
59+
var z;
60+
var i;
61+
62+
b.tic();
63+
for ( i = 0; i < b.iterations; i++ ) {
64+
z = snrm2( [ x ] );
65+
if ( isnanf( z ) ) {
66+
b.fail( 'should not return NaN' );
67+
}
68+
}
69+
b.toc();
70+
if ( isnanf( z ) ) {
71+
b.fail( 'should not return NaN' );
72+
}
73+
b.pass( 'benchmark finished' );
74+
b.end();
75+
}
76+
}
77+
78+
79+
// MAIN //
80+
81+
/**
82+
* Main execution sequence.
83+
*
84+
* @private
85+
*/
86+
function main() {
87+
var len;
88+
var min;
89+
var max;
90+
var f;
91+
var i;
92+
93+
min = 1; // 10^min
94+
max = 6; // 10^max
95+
96+
for ( i = min; i <= max; i++ ) {
97+
len = pow( 10, i );
98+
f = createBenchmark( len );
99+
bench( format( '%s:len=%d', pkg, len ), f );
100+
}
101+
}
102+
103+
main();
Lines changed: 53 additions & 0 deletions
Loading
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
2+
{{alias}}( arrays )
3+
Computes the L2-norm of a one-dimensional single-precision floating-point
4+
ndarray.
5+
6+
If provided an empty input ndarray, the function returns `0.0`.
7+
8+
Parameters
9+
----------
10+
arrays: ArrayLikeObject<ndarray>
11+
Array-like object containing a one-dimensional input ndarray.
12+
13+
Returns
14+
-------
15+
out: number
16+
The L2-norm.
17+
18+
Examples
19+
--------
20+
> var x = new {{alias:@stdlib/ndarray/vector/float32}}( [ 1.0, -2.0, 2.0 ] );
21+
> {{alias}}( [ x ] )
22+
3.0
23+
24+
See Also
25+
--------
26+
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 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+
// TypeScript Version: 4.1
20+
21+
/// <reference types="@stdlib/types"/>
22+
23+
import { float32ndarray } from '@stdlib/types/ndarray';
24+
25+
/**
26+
* Computes the L2-norm of a one-dimensional single-precision floating-point ndarray.
27+
*
28+
* ## Notes
29+
*
30+
* - The function expects the following ndarrays:
31+
*
32+
* - a one-dimensional input ndarray.
33+
*
34+
* @param arrays - array-like object containing ndarrays
35+
* @returns L2-norm
36+
*
37+
* @example
38+
* var Float32Vector = require( '@stdlib/ndarray/vector/float32' );
39+
*
40+
* var x = new Float32Vector( [ 1.0, -2.0, 2.0 ] );
41+
*
42+
* var y = snrm2( [ x ] );
43+
* // returns 3.0
44+
*/
45+
declare function snrm2( arrays: [ float32ndarray ] ): number;
46+
47+
48+
// EXPORTS //
49+
50+
export = snrm2;

0 commit comments

Comments
 (0)