time-to-botec

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

README.md (4935B)


      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 # Nullary
     22 
     23 > Apply a nullary callback and assign results to elements in a strided output array.
     24 
     25 <section class="intro">
     26 
     27 </section>
     28 
     29 <!-- /.intro -->
     30 
     31 <section class="usage">
     32 
     33 ## Usage
     34 
     35 ```javascript
     36 var nullary = require( '@stdlib/strided/base/nullary' );
     37 ```
     38 
     39 #### nullary( arrays, shape, strides, fcn )
     40 
     41 Applies a nullary callback and assigns results to elements in a strided output array.
     42 
     43 ```javascript
     44 var Float64Array = require( '@stdlib/array/float64' );
     45 
     46 function fill() {
     47     return 3.0;
     48 }
     49 
     50 var x = new Float64Array( [ -2.0, 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, -3.0 ] );
     51 
     52 nullary( [ x ], [ x.length ], [ 1 ], fill );
     53 // x => <Float64Array>[ 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0 ]
     54 ```
     55 
     56 The function accepts the following arguments:
     57 
     58 -   **arrays**: array-like object containing one strided output array.
     59 -   **shape**: array-like object containing a single element, the number of indexed elements.
     60 -   **strides**: array-like object containing the stride length for the strided output array.
     61 -   **fcn**: nullary function to apply.
     62 
     63 The `shape` and `strides` parameters determine which elements in the strided output array are accessed at runtime. For example, to index the first `N` elements of the strided output array in reverse order,
     64 
     65 ```javascript
     66 var Float64Array = require( '@stdlib/array/float64' );
     67 var floor = require( '@stdlib/math/base/special/floor' );
     68 
     69 function fill() {
     70     return 3.0;
     71 }
     72 
     73 var x = new Float64Array( [ -1.0, -2.0, -3.0, -4.0, -5.0, -6.0 ] );
     74 var N = floor( x.length / 2 );
     75 
     76 nullary( [ x ], [ N ], [ -1 ], fill );
     77 // x => <Float64Array>[ 3.0, 3.0, 3.0, -4.0, -5.0, -6.0 ]
     78 ```
     79 
     80 Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views.
     81 
     82 ```javascript
     83 var Float64Array = require( '@stdlib/array/float64' );
     84 var floor = require( '@stdlib/math/base/special/floor' );
     85 
     86 function fill() {
     87     return 3.0;
     88 }
     89 
     90 // Initial arrays...
     91 var x0 = new Float64Array( [ -1.0, -2.0, -3.0, -4.0, -5.0, -6.0 ] );
     92 
     93 // Create offset views...
     94 var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
     95 
     96 var N = floor( x0.length / 2 );
     97 
     98 nullary( [ x1 ], [ N ], [ 1 ], fill );
     99 // x0 => <Float64Array>[ -1.0, 3.0, -3.0, 3.0, -5.0, 3.0 ]
    100 ```
    101 
    102 #### nullary.ndarray( arrays, shape, strides, offsets, fcn )
    103 
    104 Applies a nullary callback and assigns results to elements in a strided output array using alternative indexing semantics.
    105 
    106 ```javascript
    107 var Float64Array = require( '@stdlib/array/float64' );
    108 
    109 function fill() {
    110     return 3.0;
    111 }
    112 
    113 var x = new Float64Array( [ -1.0, -2.0, -3.0, -4.0, -5.0 ] );
    114 
    115 nullary.ndarray( [ x ], [ x.length ], [ 1 ], [ 0 ], fill );
    116 // x => <Float64Array>[ 3.0, 3.0, 3.0, 3.0, 3.0 ]
    117 ```
    118 
    119 The function accepts the following additional arguments:
    120 
    121 -   **offsets**: array-like object containing the starting index (i.e., index offset) for the strided output array.
    122 
    123 While [`typed array`][mdn-typed-array] views mandate a view offset based on the underlying `buffer`, the `offsets` parameter supports indexing semantics based on starting indices. For example, to index the last `N` elements in the strided output array,
    124 
    125 ```javascript
    126 var Float64Array = require( '@stdlib/array/float64' );
    127 var floor = require( '@stdlib/math/base/special/floor' );
    128 
    129 function fill() {
    130     return 3.0;
    131 }
    132 
    133 var x = new Float64Array( [ -1.0, -2.0, -3.0, -4.0, -5.0, -6.0 ] );
    134 var N = floor( x.length / 2 );
    135 
    136 nullary.ndarray( [ x ], [ N ], [ -1 ], [ x.length-1 ], fill );
    137 // x => <Float64Array>[ -1.0, -2.0, -3.0, 3.0, 3.0, 3.0 ]
    138 ```
    139 
    140 </section>
    141 
    142 <!-- /.usage -->
    143 
    144 <section class="notes">
    145 
    146 </section>
    147 
    148 <!-- /.notes -->
    149 
    150 <section class="examples">
    151 
    152 ## Examples
    153 
    154 <!-- eslint no-undef: "error" -->
    155 
    156 ```javascript
    157 var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ).factory;
    158 var filledarray = require( '@stdlib/array/filled' );
    159 var nullary = require( '@stdlib/strided/base/nullary' );
    160 
    161 var N = 10;
    162 
    163 var x = filledarray( 0.0, N, 'generic' );
    164 console.log( x );
    165 
    166 var shape = [ N ];
    167 var strides = [ 1 ];
    168 var offsets = [ 0 ];
    169 
    170 nullary.ndarray( [ x ], shape, strides, offsets, discreteUniform( -100, 100 ) );
    171 console.log( x );
    172 ```
    173 
    174 </section>
    175 
    176 <!-- /.examples -->
    177 
    178 <section class="links">
    179 
    180 [mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
    181 
    182 </section>
    183 
    184 <!-- /.links -->