time-to-botec

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

README.md (5680B)


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