time-to-botec

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

README.md (7203B)


      1 <!--
      2 
      3 @license Apache-2.0
      4 
      5 Copyright (c) 2021 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 # Dispatch
     22 
     23 > Create an [ndarray][@stdlib/ndarray/ctor] function interface which performs multiple dispatch.
     24 
     25 <section class="intro">
     26 
     27 </section>
     28 
     29 <!-- /.intro -->
     30 
     31 <section class="usage">
     32 
     33 ## Usage
     34 
     35 ```javascript
     36 var dispatch = require( '@stdlib/ndarray/dispatch' );
     37 ```
     38 
     39 #### dispatch( fcns, types, data, nargs, nin, nout )
     40 
     41 Returns an [ndarray][@stdlib/ndarray/ctor] function interface which performs multiple dispatch.
     42 
     43 <!-- eslint-disable array-element-newline -->
     44 
     45 ```javascript
     46 var unary = require( '@stdlib/ndarray/base/unary' );
     47 var Float64Array = require( '@stdlib/array/float64' );
     48 var Float32Array = require( '@stdlib/array/float32' );
     49 var ndarray = require( '@stdlib/ndarray/ctor' );
     50 
     51 function foo( x ) {
     52     return x * 10.0;
     53 }
     54 
     55 function bar( x ) {
     56     return x * 5.0;
     57 }
     58 
     59 // Define a list of ndarray functions for applying a unary callback:
     60 var fcns = [
     61     unary,
     62     unary
     63 ];
     64 
     65 // Define a one-dimensional list of input and output array types:
     66 var types = [
     67     'float64', 'float64', // input, output
     68     'float32', 'float32'  // input, output
     69 ];
     70 
     71 // Define a list of callbacks which should be applied based on the provided array types:
     72 var data = [
     73     foo,
     74     bar
     75 ];
     76 
     77 // Define the total number of input arguments:
     78 var nargs = 2; // input_array + output_array
     79 
     80 // Define the number of input ndarrays:
     81 var nin = 1;
     82 
     83 // Define the number of output ndarrays:
     84 var nout = 1;
     85 
     86 // Create an ndarray function interface:
     87 var fcn = dispatch( fcns, types, data, nargs, nin, nout );
     88 
     89 // ...
     90 
     91 var xbuf = new Float64Array( [ 1.0, 2.0, 3.0 ] );
     92 var ybuf = new Float64Array( xbuf.length );
     93 
     94 var x = ndarray( 'float64', xbuf, [ 3 ], [ 1 ], 0, 'row-major' );
     95 var y = ndarray( 'float64', ybuf, [ 3 ], [ 1 ], 0, 'row-major' );
     96 
     97 fcn( x, y );
     98 // ybuf => <Float64Array>[ 10.0, 20.0, 30.0 ]
     99 
    100 xbuf = new Float32Array( [ 1.0, 2.0, 3.0 ] );
    101 ybuf = new Float32Array( xbuf.length );
    102 
    103 x = ndarray( 'float32', xbuf, [ 3 ], [ 1 ], 0, 'row-major' );
    104 y = ndarray( 'float32', ybuf, [ 3 ], [ 1 ], 0, 'row-major' );
    105 
    106 fcn( x, y );
    107 // ybuf => <Float32Array>[ 5.0, 10.0, 15.0 ]
    108 ```
    109 
    110 The function accepts the following arguments:
    111 
    112 -   **fcns**: list of [ndarray][@stdlib/ndarray/ctor] functions.
    113 -   **types**: one-dimensional list of [ndarray][@stdlib/ndarray/ctor] argument data types. The length of `types` must be the number of [ndarray][@stdlib/ndarray/ctor] functions multiplied by `nin+nout`. If `fcns` is a function, rather than a list, the number of [ndarray][@stdlib/ndarray/ctor] functions is computed as `types.length / (nin+nout)`.
    114 -   **data**: [ndarray][@stdlib/ndarray/ctor] function data (e.g., callbacks). If a list, the length of `data` must equal the number of [ndarray][@stdlib/ndarray/ctor] functions. If `null`, a returned [ndarray][@stdlib/ndarray/ctor] function interface does **not** provide a `data` argument to an invoked [ndarray][@stdlib/ndarray/ctor] function.
    115 -   **nargs**: total number of [ndarray][@stdlib/ndarray/ctor] function interface arguments.
    116 -   **nin**: number of input [ndarrays][@stdlib/ndarray/ctor].
    117 -   **nout**: number of output [ndarrays][@stdlib/ndarray/ctor].
    118 
    119 </section>
    120 
    121 <!-- /.usage -->
    122 
    123 <section class="notes">
    124 
    125 ## Notes
    126 
    127 -   A returned [ndarray][@stdlib/ndarray/ctor] function interface has the following signature:
    128 
    129     ```text
    130     f( x, y, ... )
    131     ```
    132 
    133     where
    134 
    135     -   **x**: [ndarray][@stdlib/ndarray/ctor].
    136     -   **y**: [ndarray][@stdlib/ndarray/ctor].
    137     -   **...**: additional [ndarrays][@stdlib/ndarray/ctor].
    138 
    139 -   The number of [ndarray][@stdlib/ndarray/ctor] function interface parameters is derived from `nargs`, the number of input [ndarrays][@stdlib/ndarray/ctor] is derived from `nin`, and the number of output [ndarrays][@stdlib/ndarray/ctor] is derived from `nout`.
    140 
    141 -   An [ndarray][@stdlib/ndarray/ctor] function (i.e., a value provided for the `fcns` argument) should have the following signature:
    142 
    143     ```text
    144     f( arrays[, data] )
    145     ```
    146 
    147     where
    148 
    149     -   **arrays**: array containing input and output [ndarrays][@stdlib/ndarray/ctor].
    150     -   **data**: [ndarray][@stdlib/ndarray/ctor] function data (e.g., a callback).
    151 
    152 -   For convenience, a single [ndarray][@stdlib/ndarray/ctor] function may be provided which will be invoked whenever the [ndarray][@stdlib/ndarray/ctor] argument data types match a sequence of types in `types`. Providing a single [ndarray][@stdlib/ndarray/ctor] function is particularly convenient for the case where, regardless of array data types, traversing arrays remains the same, but the [ndarray][@stdlib/ndarray/ctor] function `data` differs (e.g., callbacks which differ based on the array data types). For example, the following
    153 
    154     <!-- eslint-disable array-element-newline -->
    155 
    156     ```javascript
    157     var unary = require( '@stdlib/ndarray/base/unary' );
    158 
    159     function foo( x ) {
    160         return x * 10.0;
    161     }
    162 
    163     function bar( x ) {
    164         return x * 5.0;
    165     }
    166 
    167     var fcns = [
    168         unary,
    169         unary
    170     ];
    171     var types = [
    172         'float64', 'float64',
    173         'float32', 'float32'
    174     ];
    175     var data = [
    176         foo,
    177         bar
    178     ];
    179 
    180     var fcn = dispatch( fcns, types, data, 2, 1, 1 );
    181     ```
    182 
    183     is equivalent to
    184 
    185     <!-- eslint-disable array-element-newline -->
    186 
    187     ```javascript
    188     var unary = require( '@stdlib/ndarray/base/unary' );
    189 
    190     function foo( x ) {
    191         return x * 10.0;
    192     }
    193 
    194     function bar( x ) {
    195         return x * 5.0;
    196     }
    197 
    198     var types = [
    199         'float64', 'float64',
    200         'float32', 'float32'
    201     ];
    202     var data = [
    203         foo,
    204         bar
    205     ];
    206 
    207     var fcn = dispatch( unary, types, data, 2, 1, 1 );
    208     ```
    209 
    210 </section>
    211 
    212 <!-- /.notes -->
    213 
    214 <section class="examples">
    215 
    216 ## Examples
    217 
    218 <!-- eslint no-undef: "error" -->
    219 
    220 ```javascript
    221 var unary = require( '@stdlib/ndarray/base/unary' );
    222 var ndarray = require( '@stdlib/ndarray/ctor' );
    223 var abs = require( '@stdlib/math/base/special/abs' );
    224 var Float64Array = require( '@stdlib/array/float64' );
    225 var dispatch = require( '@stdlib/ndarray/dispatch' );
    226 
    227 var types = [ 'float64', 'float64' ];
    228 
    229 var data = [
    230     abs
    231 ];
    232 
    233 var absolute = dispatch( unary, types, data, 2, 1, 1 );
    234 
    235 var xbuf = new Float64Array( [ -1.0, -2.0, -3.0, -4.0, -5.0 ] );
    236 var ybuf = new Float64Array( [ 0.0, 0.0, 0.0, 0.0, 0.0 ] );
    237 
    238 var x = ndarray( 'float64', xbuf, [ 5 ], [ 1 ], 0, 'row-major' );
    239 var y = ndarray( 'float64', ybuf, [ 5 ], [ 1 ], 0, 'row-major' );
    240 
    241 absolute( x, y );
    242 console.log( ybuf );
    243 // => <Float64Array>[ 1.0, 2.0, 3.0, 4.0, 5.0 ]
    244 ```
    245 
    246 </section>
    247 
    248 <!-- /.examples -->
    249 
    250 <section class="links">
    251 
    252 [@stdlib/ndarray/ctor]: https://www.npmjs.com/package/@stdlib/ndarray/tree/main/ctor
    253 
    254 </section>
    255 
    256 <!-- /.links -->