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