Skip to content

Commit f8997c4

Browse files
committed
Add generic BLAS routine for calculating the L2-norm
1 parent 0ed0f9d commit f8997c4

File tree

16 files changed

+1422
-0
lines changed

16 files changed

+1422
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2020 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+
# gnrm2
22+
23+
> Calculate the L2-norm of a vector.
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+
<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">
32+
<img src="" alt="L2-norm definition.">
33+
<br>
34+
</div>
35+
36+
<!-- </equation> -->
37+
38+
</section>
39+
40+
<!-- /.intro -->
41+
42+
<section class="usage">
43+
44+
## Usage
45+
46+
```javascript
47+
var gnrm2 = require( '@stdlib/blas/base/gnrm2' );
48+
```
49+
50+
#### gnrm2( N, x, stride )
51+
52+
Computes the [L2-norm][l2-norm] of a vector `x`.
53+
54+
```javascript
55+
var x = [ 1.0, -2.0, 2.0 ];
56+
57+
var z = gnrm2( x.length, x, 1 );
58+
// returns 3.0
59+
```
60+
61+
The function accepts the following parameters:
62+
63+
- **N**: number of indexed elements.
64+
- **x**: input [`Array`][mdn-array] or [`typed array`][mdn-typed-array].
65+
- **stride**: index increment for `x`.
66+
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`,
68+
69+
```javascript
70+
var floor = require( '@stdlib/math/base/special/floor' );
71+
72+
var x = [ 1.0, 2.0, 2.0, -7.0, -2.0, 3.0, 4.0, 2.0 ];
73+
var N = floor( x.length / 2 );
74+
75+
var z = gnrm2( N, x, 2 );
76+
// returns 5.0
77+
```
78+
79+
Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views.
80+
81+
<!-- eslint-disable stdlib/capitalized-comments -->
82+
83+
```javascript
84+
var Float64Array = require( '@stdlib/array/float64' );
85+
var floor = require( '@stdlib/math/base/special/floor' );
86+
87+
var x0 = new Float64Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ]);
88+
var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
89+
90+
var N = floor( x0.length / 2 );
91+
92+
var z = gnrm2( N, x1, 2 );
93+
// returns 5.0
94+
```
95+
96+
#### gnrm2.ndarray( N, x, stride, offset )
97+
98+
Computes the [L2-norm][l2-norm] of a vector using alternative indexing semantics.
99+
100+
```javascript
101+
var x = [ 1.0, -2.0, 2.0 ];
102+
103+
var z = gnrm2.ndarray( x.length, x, 1, 0 );
104+
// returns 3.0
105+
```
106+
107+
The function accepts the following additional parameters:
108+
109+
- **offset**: starting index for `x`.
110+
111+
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
112+
113+
```javascript
114+
var floor = require( '@stdlib/math/base/special/floor' );
115+
116+
var x = [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ];
117+
var N = floor( x.length / 2 );
118+
119+
var z = gnrm2.ndarray( N, x, 2, 1 );
120+
// returns 5.0
121+
```
122+
123+
</section>
124+
125+
<!-- /.usage -->
126+
127+
<section class="notes">
128+
129+
## Notes
130+
131+
- If `N <= 0` or `stride <= 0`, both functions return `0.0`.
132+
- `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.
133+
134+
</section>
135+
136+
<!-- /.notes -->
137+
138+
<section class="examples">
139+
140+
## Examples
141+
142+
```javascript
143+
var randu = require( '@stdlib/random/base/randu' );
144+
var round = require( '@stdlib/math/base/special/round' );
145+
var Float64Array = require( '@stdlib/array/float64' );
146+
var gnrm2 = require( '@stdlib/blas/base/gnrm2' );
147+
148+
var x;
149+
var i;
150+
151+
x = new Float64Array( 10 );
152+
for ( i = 0; i < x.length; i++ ) {
153+
x[ i ] = round( randu() * 100.0 );
154+
}
155+
console.log( x );
156+
157+
var z = gnrm2( x.length, x, 1 );
158+
console.log(z);
159+
```
160+
161+
</section>
162+
163+
<!-- /.examples -->
164+
165+
<section class="links">
166+
167+
[l2-norm]: https://en.wikipedia.org/wiki/Euclidean_distance
168+
169+
[blas]: http://www.netlib.org/blas
170+
171+
[dnrm2]: http://www.netlib.org/lapack/explore-html/df/d28/group__double__blas__level1.html
172+
173+
[mdn-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array
174+
175+
[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
176+
177+
[@stdlib/blas/base/dnrm2]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/blas/base/dnrm2
178+
179+
[@stdlib/blas/base/snrm2]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/blas/base/snrm2
180+
181+
</section>
182+
183+
<!-- /.links -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2020 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 randu = require( '@stdlib/random/base/randu' );
25+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
26+
var pow = require( '@stdlib/math/base/special/pow' );
27+
var pkg = require( './../package.json' ).name;
28+
var gnrm2 = require( './../lib/main.js' );
29+
30+
31+
// FUNCTIONS //
32+
33+
/**
34+
* Creates a benchmark function.
35+
*
36+
* @private
37+
* @param {PositiveInteger} len - array length
38+
* @returns {Function} benchmark function
39+
*/
40+
function createBenchmark( len ) {
41+
var x;
42+
var i;
43+
44+
x = [];
45+
for ( i = 0; i < len; i++ ) {
46+
x.push( ( randu()*10.0 ) - 20.0 );
47+
}
48+
return benchmark;
49+
50+
function benchmark( b ) {
51+
var d;
52+
var i;
53+
54+
b.tic();
55+
for ( i = 0; i < b.iterations; i++ ) {
56+
d = gnrm2( x.length, x, 1 );
57+
if ( isnan( d ) ) {
58+
b.fail( 'should not return NaN' );
59+
}
60+
}
61+
b.toc();
62+
if ( isnan( d ) ) {
63+
b.fail( 'should not return NaN' );
64+
}
65+
b.pass( 'benchmark finished' );
66+
b.end();
67+
}
68+
}
69+
70+
71+
// MAIN //
72+
73+
/**
74+
* Main execution sequence.
75+
*
76+
* @private
77+
*/
78+
function main() {
79+
var len;
80+
var min;
81+
var max;
82+
var f;
83+
var i;
84+
85+
min = 1; // 10^min
86+
max = 6; // 10^max
87+
88+
for ( i = min; i <= max; i++ ) {
89+
len = pow( 10, i );
90+
f = createBenchmark( len );
91+
bench( pkg+':len='+len, f );
92+
}
93+
}
94+
95+
main();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2020 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 randu = require( '@stdlib/random/base/randu' );
25+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
26+
var pow = require( '@stdlib/math/base/special/pow' );
27+
var pkg = require( './../package.json' ).name;
28+
var gnrm2 = require( './../lib/main.js' ).ndarray;
29+
30+
31+
// FUNCTIONS //
32+
33+
/**
34+
* Creates a benchmark function.
35+
*
36+
* @private
37+
* @param {PositiveInteger} len - array length
38+
* @returns {Function} benchmark function
39+
*/
40+
function createBenchmark( len ) {
41+
var x;
42+
var i;
43+
44+
x = [];
45+
for ( i = 0; i < len; i++ ) {
46+
x.push( ( randu()*10.0 ) - 20.0 );
47+
}
48+
return benchmark;
49+
50+
function benchmark( b ) {
51+
var d;
52+
var i;
53+
54+
b.tic();
55+
for ( i = 0; i < b.iterations; i++ ) {
56+
d = gnrm2( x.length, x, 1, 0 );
57+
if ( isnan( d ) ) {
58+
b.fail( 'should not return NaN' );
59+
}
60+
}
61+
b.toc();
62+
if ( isnan( d ) ) {
63+
b.fail( 'should not return NaN' );
64+
}
65+
b.pass( 'benchmark finished' );
66+
b.end();
67+
}
68+
}
69+
70+
71+
// MAIN //
72+
73+
/**
74+
* Main execution sequence.
75+
*
76+
* @private
77+
*/
78+
function main() {
79+
var len;
80+
var min;
81+
var max;
82+
var f;
83+
var i;
84+
85+
min = 1; // 10^min
86+
max = 6; // 10^max
87+
88+
for ( i = min; i <= max; i++ ) {
89+
len = pow( 10, i );
90+
f = createBenchmark( len );
91+
bench( pkg+':ndarray:len='+len, f );
92+
}
93+
}
94+
95+
main();

0 commit comments

Comments
 (0)