README.md (7698B)
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 # Quaternary 22 23 > Apply a quaternary callback to strided input array elements 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 quaternary = require( '@stdlib/strided/base/quaternary' ); 37 ``` 38 39 #### quaternary( arrays, shape, strides, fcn ) 40 41 Applies a quaternary callback to strided input array elements and assigns results to elements in a strided output array. 42 43 ```javascript 44 var Float64Array = require( '@stdlib/array/float64' ); 45 46 function add( x, y, z, w ) { 47 return x + y + z + w; 48 } 49 50 var x = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] ); 51 var y = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] ); 52 var z = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] ); 53 var w = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] ); 54 var u = new Float64Array( [ 0.0, 0.0, 0.0, 0.0, 0.0 ] ); 55 56 quaternary( [ x, y, z, w, u ], [ x.length ], [ 1, 1, 1, 1, 1 ], add ); 57 // u => <Float64Array>[ 4.0, 8.0, 12.0, 16.0, 20.0 ] 58 ``` 59 60 The function accepts the following arguments: 61 62 - **arrays**: array-like object containing four strided input arrays and one strided output array. 63 - **shape**: array-like object containing a single element, the number of indexed elements. 64 - **strides**: array-like object containing the stride lengths for the strided input and output arrays. 65 - **fcn**: quaternary function to apply. 66 67 The `shape` and `strides` parameters determine which elements in the strided input and output arrays are accessed at runtime. For example, to index every other value in the strided input arrays and to index the first `N` elements of the strided output array in reverse order, 68 69 ```javascript 70 var Float64Array = require( '@stdlib/array/float64' ); 71 var floor = require( '@stdlib/math/base/special/floor' ); 72 73 function add( x, y, z, w ) { 74 return x + y + z + w; 75 } 76 77 var x = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] ); 78 var y = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] ); 79 var z = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] ); 80 var w = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] ); 81 var u = new Float64Array( [ 0.0, 0.0, 0.0, 0.0, 0.0 ] ); 82 83 var N = floor( x.length / 2 ); 84 85 quaternary( [ x, y, z, w, u ], [ N ], [ 2, 2, 2, 2, -1 ], add ); 86 // u => <Float64Array>[ 20.0, 12.0, 4.0, 0.0, 0.0, 0.0 ] 87 ``` 88 89 Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views. 90 91 ```javascript 92 var Float64Array = require( '@stdlib/array/float64' ); 93 var floor = require( '@stdlib/math/base/special/floor' ); 94 95 function add( x, y, z, w ) { 96 return x + y + z + w; 97 } 98 99 // Initial arrays... 100 var x0 = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); 101 var y0 = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); 102 var z0 = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); 103 var w0 = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); 104 var u0 = new Float64Array( [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] ); 105 106 // Create offset views... 107 var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element 108 var y1 = new Float64Array( y0.buffer, y0.BYTES_PER_ELEMENT*1 ); // start at 2nd element 109 var z1 = new Float64Array( z0.buffer, z0.BYTES_PER_ELEMENT*1 ); // start at 2nd element 110 var w1 = new Float64Array( w0.buffer, w0.BYTES_PER_ELEMENT*1 ); // start at 2nd element 111 var u1 = new Float64Array( u0.buffer, u0.BYTES_PER_ELEMENT*3 ); // start at 4th element 112 113 var N = floor( x0.length / 2 ); 114 115 quaternary( [ x1, y1, z1, w1, u1 ], [ N ], [ -2, -2, -2, -2, 1 ], add ); 116 // u0 => <Float64Array>[ 0.0, 0.0, 0.0, 24.0, 16.0, 8.0 ] 117 ``` 118 119 #### quaternary.ndarray( arrays, shape, strides, offsets, fcn ) 120 121 Applies a quaternary callback to strided input array elements and assigns results to elements in a strided output array using alternative indexing semantics. 122 123 <!-- eslint-disable max-len --> 124 125 ```javascript 126 var Float64Array = require( '@stdlib/array/float64' ); 127 128 function add( x, y, z, w ) { 129 return x + y + z + w; 130 } 131 132 var x = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] ); 133 var y = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] ); 134 var z = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] ); 135 var w = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0 ] ); 136 var u = new Float64Array( [ 0.0, 0.0, 0.0, 0.0, 0.0 ] ); 137 138 quaternary.ndarray( [ x, y, z, w, u ], [ x.length ], [ 1, 1, 1, 1, 1 ], [ 0, 0, 0, 0, 0 ], add ); 139 // u => <Float64Array>[ 4.0, 8.0, 12.0, 16.0, 20.0 ] 140 ``` 141 142 The function accepts the following additional arguments: 143 144 - **offsets**: array-like object containing the starting indices (i.e., index offsets) for the strided input and output arrays. 145 146 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 every other value in the strided input arrays starting from the second value and to index the last `N` elements in the strided output array, 147 148 <!-- eslint-disable max-len --> 149 150 ```javascript 151 var Float64Array = require( '@stdlib/array/float64' ); 152 var floor = require( '@stdlib/math/base/special/floor' ); 153 154 function add( x, y, z, w ) { 155 return x + y + z + w; 156 } 157 158 var x = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); 159 var y = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); 160 var z = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); 161 var w = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); 162 var u = new Float64Array( [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] ); 163 164 var N = floor( x.length / 2 ); 165 166 quaternary.ndarray( [ x, y, z, w, u ], [ N ], [ 2, 2, 2, 2, -1 ], [ 1, 1, 1, 1, u.length-1 ], add ); 167 // u => <Float64Array>[ 0.0, 0.0, 0.0, 24.0, 16.0, 8.0 ] 168 ``` 169 170 </section> 171 172 <!-- /.usage --> 173 174 <section class="notes"> 175 176 </section> 177 178 <!-- /.notes --> 179 180 <section class="examples"> 181 182 ## Examples 183 184 <!-- eslint no-undef: "error" --> 185 186 ```javascript 187 var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ).factory; 188 var filledarray = require( '@stdlib/array/filled' ); 189 var gfillBy = require( '@stdlib/blas/ext/base/gfill-by' ); 190 var quaternary = require( '@stdlib/strided/base/quaternary' ); 191 192 function add( x, y, z, w ) { 193 return x + y + z + w; 194 } 195 196 var N = 10; 197 198 var x = filledarray( 0.0, N, 'generic' ); 199 gfillBy( x.length, x, 1, discreteUniform( -100, 100 ) ); 200 console.log( x ); 201 202 var y = filledarray( 0.0, N, 'generic' ); 203 gfillBy( y.length, y, 1, discreteUniform( -100, 100 ) ); 204 console.log( y ); 205 206 var z = filledarray( 0.0, N, 'generic' ); 207 gfillBy( z.length, z, 1, discreteUniform( -100, 100 ) ); 208 console.log( z ); 209 210 var w = filledarray( 0.0, N, 'generic' ); 211 gfillBy( w.length, w, 1, discreteUniform( -100, 100 ) ); 212 console.log( w ); 213 214 var u = filledarray( 0.0, N, 'generic' ); 215 console.log( u ); 216 217 var shape = [ N ]; 218 var strides = [ 1, 1, 1, 1, -1 ]; 219 var offsets = [ 0, 0, 0, 0, N-1 ]; 220 221 quaternary.ndarray( [ x, y, z, w, u ], shape, strides, offsets, add ); 222 console.log( u ); 223 ``` 224 225 </section> 226 227 <!-- /.examples --> 228 229 <section class="links"> 230 231 [mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray 232 233 </section> 234 235 <!-- /.links -->