time-to-botec

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

README.md (5935B)


      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 # rangeBy
     22 
     23 > Calculate the [range][range] of a strided array via a callback function.
     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 rangeBy = require( '@stdlib/stats/base/range-by' );
     39 ```
     40 
     41 #### rangeBy( N, x, stride, clbk\[, thisArg] )
     42 
     43 Calculates the [range][range] of strided array `x` via a callback function.
     44 
     45 ```javascript
     46 function accessor( v ) {
     47     return v * 2.0;
     48 }
     49 
     50 var x = [ -2.0, 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, -3.0 ];
     51 
     52 var v = rangeBy( x.length, x, 1, accessor );
     53 // returns 18.0
     54 ```
     55 
     56 The function has the following parameters:
     57 
     58 -   **N**: number of indexed elements.
     59 -   **x**: input [`Array`][mdn-array], [`typed array`][mdn-typed-array], or an array-like object (excluding strings and functions). 
     60 -   **stride**: index increment.
     61 -   **clbk**: callback function.
     62 -   **thisArg**: execution context (_optional_).
     63 
     64 The invoked callback is provided four arguments:
     65 
     66 -   **value**: array element.
     67 -   **aidx**: array index.
     68 -   **sidx**: strided index (`offset + aidx*stride`).
     69 -   **array**: input array/collection.
     70 
     71 To set the callback execution context, provide a `thisArg`.
     72 
     73 ```javascript
     74 function accessor( v ) {
     75     this.count += 1;
     76     return v * 2.0;
     77 }
     78 
     79 var x = [ -2.0, 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, -3.0 ];
     80 
     81 var context = {
     82     'count': 0
     83 };
     84 
     85 var v = rangeBy( x.length, x, 1, accessor, context );
     86 // returns 18.0
     87 
     88 var cnt = context.count;
     89 // returns 8
     90 ```
     91 
     92 The `N` and `stride` parameters determine which elements in `x` are accessed at runtime. For example, to access every other element
     93 
     94 ```javascript
     95 var floor = require( '@stdlib/math/base/special/floor' );
     96 
     97 function accessor( v ) {
     98     return v * 2.0;
     99 }
    100 
    101 var x = [ -2.0, 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, -3.0 ];
    102 var N = floor( x.length / 2 );
    103 
    104 var v = rangeBy( N, x, 2, accessor );
    105 // returns 12.0
    106 ```
    107 
    108 Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views.
    109 
    110 ```javascript
    111 var Float64Array = require( '@stdlib/array/float64' );
    112 var floor = require( '@stdlib/math/base/special/floor' );
    113 
    114 function accessor( v ) {
    115     return v * 2.0;
    116 }
    117 
    118 // Initial array...
    119 var x0 = new Float64Array( [ 1.0, -2.0, 3.0, -4.0, 5.0, -6.0 ] );
    120 
    121 // Create an offset view...
    122 var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
    123 var N = floor( x0.length/2 );
    124 
    125 // Access every other element...
    126 var v = rangeBy( N, x1, 2, accessor );
    127 // returns 8.0
    128 ```
    129 
    130 #### rangeBy.ndarray( N, x, stride, offset, clbk\[, thisArg] )
    131 
    132 Calculates the [range][range] of strided array `x` via a callback function and using alternative indexing semantics.
    133 
    134 ```javascript
    135 function accessor( v ) {
    136     return v * 2.0;
    137 }
    138 
    139 var x = [ -2.0, 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, -3.0 ];
    140 
    141 var v = rangeBy.ndarray( x.length, x, 1, 0, accessor );
    142 // returns 18.0
    143 ```
    144 
    145 The function has the following additional parameters:
    146 
    147 -   **offset**: starting index.
    148 
    149 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 access only the last three elements of `x`
    150 
    151 ```javascript
    152 function accessor( v ) {
    153     return v * 2.0;
    154 }
    155 
    156 var x = [ 1.0, -2.0, 3.0, -4.0, 5.0, -6.0 ];
    157 
    158 var v = rangeBy.ndarray( 3, x, 1, x.length-3, accessor );
    159 // returns 22.0
    160 ```
    161 
    162 </section>
    163 
    164 <!-- /.usage -->
    165 
    166 <section class="notes">
    167 
    168 ## Notes
    169 
    170 -   If `N <= 0`, both functions return `NaN`.
    171 -   A provided callback function should return a numeric value.
    172 -   If a provided callback function does not return any value (or equivalently, explicitly returns `undefined`), the value is **ignored**.
    173 -   When possible, prefer using [`drange`][@stdlib/stats/base/drange], [`srange`][@stdlib/stats/base/srange], and/or [`range`][@stdlib/stats/base/range], as, depending on the environment, these interfaces are likely to be significantly more performant.
    174 
    175 </section>
    176 
    177 <!-- /.notes -->
    178 
    179 <section class="examples">
    180 
    181 ## Examples
    182 
    183 <!-- eslint no-undef: "error" -->
    184 
    185 ```javascript
    186 var round = require( '@stdlib/math/base/special/round' );
    187 var randu = require( '@stdlib/random/base/randu' );
    188 var Float64Array = require( '@stdlib/array/float64' );
    189 var gfillBy = require( '@stdlib/blas/ext/base/gfill-by' );
    190 var rangeBy = require( '@stdlib/stats/base/range-by' );
    191 
    192 function fill() {
    193     return round( ( randu()*100.0 ) - 50.0 );
    194 }
    195 
    196 function accessor( v ) {
    197     return v * 2.0;
    198 }
    199 
    200 var x = new Float64Array( 10 );
    201 
    202 gfillBy( x.length, x, 1, fill );
    203 console.log( x );
    204 
    205 var v = rangeBy( x.length, x, 1, accessor );
    206 console.log( v );
    207 ```
    208 
    209 </section>
    210 
    211 <!-- /.examples -->
    212 
    213 <section class="links">
    214 
    215 [range]: https://en.wikipedia.org/wiki/Range_%28statistics%29
    216 
    217 [mdn-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array
    218 
    219 [mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
    220 
    221 [@stdlib/stats/base/drange]: https://www.npmjs.com/package/@stdlib/stats/tree/main/base/drange
    222 
    223 [@stdlib/stats/base/range]: https://www.npmjs.com/package/@stdlib/stats/tree/main/base/range
    224 
    225 [@stdlib/stats/base/srange]: https://www.npmjs.com/package/@stdlib/stats/tree/main/base/srange
    226 
    227 </section>
    228 
    229 <!-- /.links -->