time-to-botec

Benchmark sampling in different programming languages
Log | Files | Refs | README

README.md (5258B)


      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="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@f8997c489e47eb1a9d993ef4ab3a522a095331f5/lib/node_modules/@stdlib/blas/base/gnrm2/docs/img/equation_l2_norm.svg" 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 has 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 If either `N` or `stride` is less than or equal to `0`, the function returns `0`.
     97 
     98 #### gnrm2.ndarray( N, x, stride, offset )
     99 
    100 Computes the [L2-norm][l2-norm] of a vector using alternative indexing semantics.
    101 
    102 ```javascript
    103 var x = [ 1.0, -2.0, 2.0 ];
    104 
    105 var z = gnrm2.ndarray( x.length, x, 1, 0 );
    106 // returns 3.0
    107 ```
    108 
    109 The function has the following additional parameters:
    110 
    111 -   **offset**: starting index for `x`.
    112 
    113 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
    114 
    115 ```javascript
    116 var floor = require( '@stdlib/math/base/special/floor' );
    117 
    118 var x = [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ];
    119 var N = floor( x.length / 2 );
    120 
    121 var z = gnrm2.ndarray( N, x, 2, 1 );
    122 // returns 5.0
    123 ```
    124 
    125 </section>
    126 
    127 <!-- /.usage -->
    128 
    129 <section class="notes">
    130 
    131 ## Notes
    132 
    133 -   If `N <= 0`, both functions return `0.0`.
    134 -   `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.
    135 
    136 </section>
    137 
    138 <!-- /.notes -->
    139 
    140 <section class="examples">
    141 
    142 ## Examples
    143 
    144 <!-- eslint no-undef: "error" -->
    145 
    146 ```javascript
    147 var randu = require( '@stdlib/random/base/randu' );
    148 var round = require( '@stdlib/math/base/special/round' );
    149 var Float64Array = require( '@stdlib/array/float64' );
    150 var gnrm2 = require( '@stdlib/blas/base/gnrm2' );
    151 
    152 var x;
    153 var i;
    154 
    155 x = new Float64Array( 10 );
    156 for ( i = 0; i < x.length; i++ ) {
    157     x[ i ] = round( randu()*100.0 );
    158 }
    159 console.log( x );
    160 
    161 var z = gnrm2( x.length, x, 1 );
    162 console.log( z );
    163 ```
    164 
    165 </section>
    166 
    167 <!-- /.examples -->
    168 
    169 <section class="links">
    170 
    171 [l2-norm]: https://en.wikipedia.org/wiki/Euclidean_distance
    172 
    173 [blas]: http://www.netlib.org/blas
    174 
    175 [dnrm2]: http://www.netlib.org/lapack/explore-html/de/da4/group__double__blas__level1.html
    176 
    177 [mdn-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array
    178 
    179 [mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
    180 
    181 [@stdlib/blas/base/dnrm2]: https://www.npmjs.com/package/@stdlib/blas/tree/main/base/dnrm2
    182 
    183 [@stdlib/blas/base/snrm2]: https://www.npmjs.com/package/@stdlib/blas/tree/main/base/snrm2
    184 
    185 </section>
    186 
    187 <!-- /.links -->