time-to-botec

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

README.md (4686B)


      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 # nanrange
     22 
     23 > Calculate the [range][range] of a strided array, ignoring `NaN` values.
     24 
     25 <section class="intro">
     26 
     27 The [**range**][range] is defined as the difference between the maximum and minimum values.
     28 
     29 </section>
     30 
     31 <!-- /.intro -->
     32 
     33 <section class="usage">
     34 
     35 ## Usage
     36 
     37 ```javascript
     38 var nanrange = require( '@stdlib/stats/base/nanrange' );
     39 ```
     40 
     41 #### nanrange( N, x, stride )
     42 
     43 Computes the [range][range] of a strided array `x`, ignoring `NaN` values.
     44 
     45 ```javascript
     46 var x = [ 1.0, -2.0, NaN, 2.0 ];
     47 var N = x.length;
     48 
     49 var v = nanrange( N, x, 1 );
     50 // returns 4.0
     51 ```
     52 
     53 The function has the following parameters:
     54 
     55 -   **N**: number of indexed elements.
     56 -   **x**: input [`Array`][mdn-array] or [`typed array`][mdn-typed-array].
     57 -   **stride**: index increment for `x`.
     58 
     59 The `N` and `stride` parameters determine which elements in `x` are accessed at runtime. For example, to compute the [range][range] of every other element in `x`,
     60 
     61 ```javascript
     62 var floor = require( '@stdlib/math/base/special/floor' );
     63 
     64 var x = [ 1.0, 2.0, 2.0, -7.0, -2.0, 3.0, 4.0, 2.0, NaN, NaN ];
     65 var N = floor( x.length / 2 );
     66 
     67 var v = nanrange( N, x, 2 );
     68 // returns 6.0
     69 ```
     70 
     71 Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views.
     72 
     73 <!-- eslint-disable stdlib/capitalized-comments -->
     74 
     75 ```javascript
     76 var Float64Array = require( '@stdlib/array/float64' );
     77 var floor = require( '@stdlib/math/base/special/floor' );
     78 
     79 var x0 = new Float64Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, NaN, NaN, 4.0 ] );
     80 var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
     81 
     82 var N = floor( x0.length / 2 );
     83 
     84 var v = nanrange( N, x1, 2 );
     85 // returns 6.0
     86 ```
     87 
     88 #### nanrange.ndarray( N, x, stride, offset )
     89 
     90 Computes the [range][range] of a strided array, ignoring `NaN` values and using alternative indexing semantics.
     91 
     92 ```javascript
     93 var x = [ 1.0, -2.0, NaN, 2.0 ];
     94 var N = x.length;
     95 
     96 var v = nanrange.ndarray( N, x, 1, 0 );
     97 // returns 4.0
     98 ```
     99 
    100 The function has the following additional parameters:
    101 
    102 -   **offset**: starting index for `x`.
    103 
    104 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 [range][range] for every other value in `x` starting from the second value
    105 
    106 ```javascript
    107 var floor = require( '@stdlib/math/base/special/floor' );
    108 
    109 var x = [ 2.0, 1.0, 2.0, -2.0, -2.0, NaN, NaN, 2.0, 3.0, 4.0 ];
    110 var N = floor( x.length / 2 );
    111 
    112 var v = nanrange.ndarray( N, x, 2, 1 );
    113 // returns 6.0
    114 ```
    115 
    116 </section>
    117 
    118 <!-- /.usage -->
    119 
    120 <section class="notes">
    121 
    122 ## Notes
    123 
    124 -   If `N <= 0`, both functions return `NaN`.
    125 -   Depending on the environment, the typed versions ([`dnanrange`][@stdlib/stats/base/dnanrange], [`snanrange`][@stdlib/stats/base/snanrange], etc.) are likely to be significantly more performant.
    126 
    127 </section>
    128 
    129 <!-- /.notes -->
    130 
    131 <section class="examples">
    132 
    133 ## Examples
    134 
    135 <!-- eslint no-undef: "error" -->
    136 
    137 ```javascript
    138 var randu = require( '@stdlib/random/base/randu' );
    139 var round = require( '@stdlib/math/base/special/round' );
    140 var Float64Array = require( '@stdlib/array/float64' );
    141 var nanrange = require( '@stdlib/stats/base/nanrange' );
    142 
    143 var x;
    144 var i;
    145 
    146 x = new Float64Array( 10 );
    147 for ( i = 0; i < x.length; i++ ) {
    148     if ( randu() < 0.2 ) {
    149         x[ i ] = NaN;
    150     } else {
    151         x[ i ] = round( (randu()*100.0) - 50.0 );
    152     }
    153 }
    154 console.log( x );
    155 
    156 var v = nanrange( x.length, x, 1 );
    157 console.log( v );
    158 ```
    159 
    160 </section>
    161 
    162 <!-- /.examples -->
    163 
    164 <section class="links">
    165 
    166 [range]: https://en.wikipedia.org/wiki/Range_%28statistics%29
    167 
    168 [mdn-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array
    169 
    170 [mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
    171 
    172 [@stdlib/stats/base/dnanrange]: https://www.npmjs.com/package/@stdlib/stats/tree/main/base/dnanrange
    173 
    174 [@stdlib/stats/base/snanrange]: https://www.npmjs.com/package/@stdlib/stats/tree/main/base/snanrange
    175 
    176 </section>
    177 
    178 <!-- /.links -->