README.md (8046B)
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 # Dispatch 22 23 > Create a strided array 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/strided/dispatch' ); 37 ``` 38 39 #### dispatch( fcns, types, data, nargs, nin, nout ) 40 41 Returns a strided array function interface which performs multiple dispatch. 42 43 <!-- eslint-disable array-element-newline --> 44 45 ```javascript 46 var unary = require( '@stdlib/strided/base/unary' ); 47 var Float64Array = require( '@stdlib/array/float64' ); 48 var Float32Array = require( '@stdlib/array/float32' ); 49 50 function foo( x ) { 51 return x * 10.0; 52 } 53 54 function bar( x ) { 55 return x * 5.0; 56 } 57 58 // Define a list of strided array functions for applying a unary callback: 59 var fcns = [ 60 unary, 61 unary 62 ]; 63 64 // Define a one-dimensional list of input and output array types: 65 var types = [ 66 'float64', 'float64', // input, output 67 'float32', 'float32' // input, output 68 ]; 69 70 // Define a list of callbacks which should be applied based on the provided array types: 71 var data = [ 72 foo, 73 bar 74 ]; 75 76 // Define the total number of input arguments: 77 var nargs = 5; // N + input_array + input_array_stride + output_array + output_array_stride 78 79 // Define the number of input strided arrays: 80 var nin = 1; 81 82 // Define the number of output strided arrays: 83 var nout = 1; 84 85 // Create a strided array function interface: 86 var strided = dispatch( fcns, types, data, nargs, nin, nout ); 87 88 // ... 89 90 var x = new Float64Array( [ 1.0, 2.0, 3.0 ] ); 91 var y = new Float64Array( x.length ); 92 93 strided( x.length, x, 1, y, 1 ); 94 // y => <Float64Array>[ 10.0, 20.0, 30.0 ] 95 96 x = new Float32Array( [ 1.0, 2.0, 3.0 ] ); 97 y = new Float32Array( x.length ); 98 99 strided( x.length, x, 1, y, 1 ); 100 // y => <Float32Array>[ 5.0, 10.0, 15.0 ] 101 ``` 102 103 The function accepts the following arguments: 104 105 - **fcns**: list of strided array functions. 106 - **types**: one-dimensional list of strided array argument data types. The length of `types` must be the number of strided array functions multiplied by `nin+nout`. If `fcns` is a function, rather than a list, the number of strided array functions is computed as `types.length / (nin+nout)`. 107 - **data**: strided array function data (e.g., callbacks). If a list, the length of `data` must equal the number of strided array functions. If `null`, a returned strided array function interface does **not** provide a `data` argument to an invoked strided array function. 108 - **nargs**: total number of strided array function interface arguments (including strides and offsets). 109 - **nin**: number of input strided arrays. 110 - **nout**: number of output strided arrays. 111 112 </section> 113 114 <!-- /.usage --> 115 116 <section class="notes"> 117 118 ## Notes 119 120 - Without offsets, a returned strided array function interface has the following signature: 121 122 ```text 123 f( N, x, strideX, y, strideY, ... ) 124 ``` 125 126 where 127 128 - **N**: number of indexed elements. 129 - **x**: strided array. 130 - **strideX**: index increment for `x`. 131 - **y**: strided array. 132 - **strideY**: index increment for `y`. 133 - **...**: additional strided arrays and associated strides. 134 135 - The number of strided array function interface parameters is derived from `nargs`, the number of input strided arrays is derived from `nin`, and the number of output strided arrays is derived from `nout`. 136 137 - Without offsets, the number of parameters must obey the following relation: 138 139 ```text 140 nargs = 2*(nout+nin) + 1 141 ``` 142 143 - With offsets, the number of parameters must obey the following relation: 144 145 ```text 146 nargs = 3*(nout+nin) + 1 147 ``` 148 149 - With offsets, a returned strided array function interface has the following signature: 150 151 ```text 152 f( N, x, strideX, offsetX, y, strideY, offsetY, ... ) 153 ``` 154 155 where 156 157 - **N**: number of indexed elements. 158 - **x**: strided array. 159 - **strideX**: index increment for `x`. 160 - **offsetX**: starting index for `x`. 161 - **y**: strided array. 162 - **strideY**: index increment for `y`. 163 - **offsetY**: starting index for `y`. 164 - **...**: additional strided arrays and associated strides and offsets. 165 166 The choice of which strided array function interface to return depends on the use case. The former is suitable for typed array views; while the latter affords alternative indexing semantics more suitable for n-dimensional arrays (ndarrays). 167 168 - Without offsets, a strided array function (i.e., a value provided for the `fcns` argument) should have the following signature: 169 170 ```text 171 f( arrays, shape, strides[, data] ) 172 ``` 173 174 where 175 176 - **arrays**: array containing strided input and output arrays. 177 - **shape**: array containing a single element, the number of indexed elements. 178 - **strides**: array containing the stride lengths for the strided input and output arrays. 179 - **data**: strided array function data (e.g., a callback). 180 181 - With offsets, a strided array function should have the following signature: 182 183 ```text 184 f( arrays, shape, strides, offsets[, data] ) 185 ``` 186 187 where 188 189 - **offsets**: array containing the starting indices (i.e., index offsets) for the strided input and output arrays. 190 191 - For convenience, a single strided array function may be provided which will be invoked whenever the strided array argument data types match a sequence of types in `types`. Providing a single strided array function is particularly convenient for the case where, regardless of array data types, traversing arrays remains the same, but the strided array function `data` differs (e.g., callbacks which differ based on the array data types). For example, the following 192 193 <!-- eslint-disable array-element-newline --> 194 195 ```javascript 196 var unary = require( '@stdlib/strided/base/unary' ); 197 198 function foo( x ) { 199 return x * 10.0; 200 } 201 202 function bar( x ) { 203 return x * 5.0; 204 } 205 206 var fcns = [ 207 unary, 208 unary 209 ]; 210 var types = [ 211 'float64', 'float64', 212 'float32', 'float32' 213 ]; 214 var data = [ 215 foo, 216 bar 217 ]; 218 219 var strided = dispatch( fcns, types, data, 5, 1, 1 ); 220 ``` 221 222 is equivalent to 223 224 <!-- eslint-disable array-element-newline --> 225 226 ```javascript 227 var unary = require( '@stdlib/strided/base/unary' ); 228 229 function foo( x ) { 230 return x * 10.0; 231 } 232 233 function bar( x ) { 234 return x * 5.0; 235 } 236 237 var types = [ 238 'float64', 'float64', 239 'float32', 'float32' 240 ]; 241 var data = [ 242 foo, 243 bar 244 ]; 245 246 var strided = dispatch( unary, types, data, 5, 1, 1 ); 247 ``` 248 249 </section> 250 251 <!-- /.notes --> 252 253 <section class="examples"> 254 255 ## Examples 256 257 <!-- eslint no-undef: "error" --> 258 259 ```javascript 260 var unary = require( '@stdlib/strided/base/unary' ).ndarray; 261 var abs = require( '@stdlib/math/base/special/abs' ); 262 var Float64Array = require( '@stdlib/array/float64' ); 263 var dispatch = require( '@stdlib/strided/dispatch' ); 264 265 var types = [ 'float64', 'float64' ]; 266 267 var data = [ 268 abs 269 ]; 270 271 var strided = dispatch( unary, types, data, 7, 1, 1 ); 272 273 var x = new Float64Array( [ -1.0, -2.0, -3.0, -4.0, -5.0 ] ); 274 var y = new Float64Array( [ 0.0, 0.0, 0.0, 0.0, 0.0 ] ); 275 276 strided( x.length, x, 1, 2, y, 1, 2 ); 277 console.log( y ); 278 // => <Float64Array>[ 0.0, 0.0, 3.0, 4.0, 5.0 ] 279 ``` 280 281 </section> 282 283 <!-- /.examples --> 284 285 <section class="links"> 286 287 </section> 288 289 <!-- /.links -->