Skip to content

Commit 059850c

Browse files
authored
feat: add blas/base/ndarray/scnrm2
PR-URL: #12232 Reviewed-by: Athan Reines <kgryte@gmail.com>
1 parent 6615546 commit 059850c

11 files changed

Lines changed: 820 additions & 0 deletions

File tree

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
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+
# scnrm2
22+
23+
> Compute the L2-norm of a one-dimensional single-precision complex 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/scnrm2/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 scnrm2 = require( '@stdlib/blas/base/ndarray/scnrm2' );
52+
```
53+
54+
#### scnrm2( arrays )
55+
56+
Computes the [L2-norm][l2-norm] of a one-dimensional single-precision complex floating-point ndarray.
57+
58+
```javascript
59+
var Complex64Vector = require( '@stdlib/ndarray/vector/complex64' );
60+
61+
var x = new Complex64Vector( [ 1.0, 2.0, 2.0, 4.0 ] );
62+
63+
var y = scnrm2( [ x ] );
64+
// returns 5.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/array/discrete-uniform' );
91+
var Complex64Vector = require( '@stdlib/ndarray/vector/complex64' );
92+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
93+
var scnrm2 = require( '@stdlib/blas/base/ndarray/scnrm2' );
94+
95+
var opts = {
96+
'dtype': 'float32'
97+
};
98+
99+
var x = new Complex64Vector( discreteUniform( 10, -500, 500, opts ) );
100+
console.log( ndarray2array( x ) );
101+
102+
var out = scnrm2( [ x ] );
103+
console.log( out );
104+
```
105+
106+
</section>
107+
108+
<!-- /.examples -->
109+
110+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
111+
112+
<section class="related">
113+
114+
</section>
115+
116+
<!-- /.related -->
117+
118+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
119+
120+
<section class="links">
121+
122+
[l2-norm]: https://en.wikipedia.org/wiki/Euclidean_distance
123+
124+
</section>
125+
126+
<!-- /.links -->
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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/array/uniform' );
25+
var isnanf = require( '@stdlib/math/base/assert/is-nanf' );
26+
var pow = require( '@stdlib/math/base/special/pow' );
27+
var Complex64Vector = require( '@stdlib/ndarray/vector/complex64' );
28+
var format = require( '@stdlib/string/format' );
29+
var pkg = require( './../package.json' ).name;
30+
var scnrm2 = require( './../lib' );
31+
32+
33+
// FUNCTIONS //
34+
35+
/**
36+
* Creates a benchmark function.
37+
*
38+
* @private
39+
* @param {PositiveInteger} len - array length
40+
* @returns {Function} benchmark function
41+
*/
42+
function createBenchmark( len ) {
43+
var xbuf;
44+
var x;
45+
46+
xbuf = uniform( len*2, -100.0, 100.0, {
47+
'dtype': 'float32'
48+
});
49+
x = new Complex64Vector( xbuf.buffer );
50+
51+
return benchmark;
52+
53+
/**
54+
* Benchmark function.
55+
*
56+
* @private
57+
* @param {Benchmark} b - benchmark instance
58+
*/
59+
function benchmark( b ) {
60+
var z;
61+
var i;
62+
63+
b.tic();
64+
for ( i = 0; i < b.iterations; i++ ) {
65+
z = scnrm2( [ x ] );
66+
if ( isnanf( z ) ) {
67+
b.fail( 'should not return NaN' );
68+
}
69+
}
70+
b.toc();
71+
if ( isnanf( z ) ) {
72+
b.fail( 'should not return NaN' );
73+
}
74+
b.pass( 'benchmark finished' );
75+
b.end();
76+
}
77+
}
78+
79+
80+
// MAIN //
81+
82+
/**
83+
* Main execution sequence.
84+
*
85+
* @private
86+
*/
87+
function main() {
88+
var len;
89+
var min;
90+
var max;
91+
var f;
92+
var i;
93+
94+
min = 1; // 10^min
95+
max = 6; // 10^max
96+
97+
for ( i = min; i <= max; i++ ) {
98+
len = pow( 10, i );
99+
f = createBenchmark( len );
100+
bench( format( '%s:len=%d', pkg, len ), f );
101+
}
102+
}
103+
104+
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 complex
4+
floating-point 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/complex64}}( [ 1.0, 2.0, 2.0, 4.0 ] );
21+
> {{alias}}( [ x ] )
22+
5.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 { complex64ndarray } from '@stdlib/types/ndarray';
24+
25+
/**
26+
* Computes the L2-norm of a one-dimensional single-precision complex 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 Complex64Vector = require( '@stdlib/ndarray/vector/complex64' );
39+
*
40+
* var x = new Complex64Vector( [ 1.0, 2.0, 2.0, 4.0 ] );
41+
*
42+
* var y = scnrm2( [ x ] );
43+
* // returns 5.0
44+
*/
45+
declare function scnrm2( arrays: [ complex64ndarray ] ): number;
46+
47+
48+
// EXPORTS //
49+
50+
export = scnrm2;

0 commit comments

Comments
 (0)