|
| 1 | +<!-- |
| 2 | +
|
| 3 | +@license Apache-2.0 |
| 4 | +
|
| 5 | +Copyright (c) 2026 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 elements in input ndarrays and assign results to elements in an output ndarray. |
| 24 | +
|
| 25 | +<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. --> |
| 26 | + |
| 27 | +<section class="intro"> |
| 28 | + |
| 29 | +</section> |
| 30 | + |
| 31 | +<!-- /.intro --> |
| 32 | + |
| 33 | +<!-- Package usage documentation. --> |
| 34 | + |
| 35 | +<section class="usage"> |
| 36 | + |
| 37 | +## Usage |
| 38 | + |
| 39 | +```javascript |
| 40 | +var ternary = require( '@stdlib/ndarray/base/ternary' ); |
| 41 | +``` |
| 42 | + |
| 43 | +#### ternary( arrays, fcn ) |
| 44 | + |
| 45 | +Applies a ternary callback to elements in input ndarrays and assigns results to elements in an output ndarray. |
| 46 | + |
| 47 | +```javascript |
| 48 | +var Float64Array = require( '@stdlib/array/float64' ); |
| 49 | +var add3 = require( '@stdlib/number/float64/base/add3' ); |
| 50 | + |
| 51 | +// Create data buffers: |
| 52 | +var xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); |
| 53 | +var ybuf = new Float64Array( [ 1.0, 1.0, 1.0, 1.0, 1.0, 1.0 ] ); |
| 54 | +var zbuf = new Float64Array( [ 0.5, 0.5, 0.5, 0.5, 0.5, 0.5 ] ); |
| 55 | +var wbuf = new Float64Array( 6 ); |
| 56 | + |
| 57 | +// Define the shape of the input and output arrays: |
| 58 | +var shape = [ 3, 1, 2 ]; |
| 59 | + |
| 60 | +// Define the array strides: |
| 61 | +var sx = [ 2, 2, 1 ]; |
| 62 | +var sy = [ 2, 2, 1 ]; |
| 63 | +var sz = [ 2, 2, 1 ]; |
| 64 | +var sw = [ 2, 2, 1 ]; |
| 65 | + |
| 66 | +// Define the index offsets: |
| 67 | +var ox = 0; |
| 68 | +var oy = 0; |
| 69 | +var oz = 0; |
| 70 | +var ow = 0; |
| 71 | + |
| 72 | +// Create the input and output ndarray-like objects: |
| 73 | +var x = { |
| 74 | + 'dtype': 'float64', |
| 75 | + 'data': xbuf, |
| 76 | + 'shape': shape, |
| 77 | + 'strides': sx, |
| 78 | + 'offset': ox, |
| 79 | + 'order': 'row-major' |
| 80 | +}; |
| 81 | +var y = { |
| 82 | + 'dtype': 'float64', |
| 83 | + 'data': ybuf, |
| 84 | + 'shape': shape, |
| 85 | + 'strides': sy, |
| 86 | + 'offset': oy, |
| 87 | + 'order': 'row-major' |
| 88 | +}; |
| 89 | +var z = { |
| 90 | + 'dtype': 'float64', |
| 91 | + 'data': zbuf, |
| 92 | + 'shape': shape, |
| 93 | + 'strides': sz, |
| 94 | + 'offset': oz, |
| 95 | + 'order': 'row-major' |
| 96 | +}; |
| 97 | +var w = { |
| 98 | + 'dtype': 'float64', |
| 99 | + 'data': wbuf, |
| 100 | + 'shape': shape, |
| 101 | + 'strides': sw, |
| 102 | + 'offset': ow, |
| 103 | + 'order': 'row-major' |
| 104 | +}; |
| 105 | + |
| 106 | +// Apply the ternary function: |
| 107 | +ternary( [ x, y, z, w ], add3 ); |
| 108 | + |
| 109 | +console.log( w.data ); |
| 110 | +// => <Float64Array>[ 2.5, 3.5, 4.5, 5.5, 6.5, 7.5 ] |
| 111 | +``` |
| 112 | + |
| 113 | +The function accepts the following arguments: |
| 114 | + |
| 115 | +- **arrays**: array-like object containing three input ndarrays and one output ndarray. |
| 116 | +- **fcn**: ternary function to apply. |
| 117 | + |
| 118 | +</section> |
| 119 | + |
| 120 | +<!-- /.usage --> |
| 121 | + |
| 122 | +<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 123 | + |
| 124 | +<section class="notes"> |
| 125 | + |
| 126 | +## Notes |
| 127 | + |
| 128 | + |
| 129 | +- Each provided ndarray should be an object with the following properties: |
| 130 | + |
| 131 | + - **dtype**: data type. |
| 132 | + - **data**: data buffer. |
| 133 | + - **shape**: dimensions. |
| 134 | + - **strides**: stride lengths. |
| 135 | + - **offset**: index offset. |
| 136 | + - **order**: specifies whether an ndarray is row-major (C-style) or column major (Fortran-style). |
| 137 | + |
| 138 | +- For very high-dimensional ndarrays which are non-contiguous, one should consider copying the underlying data to contiguous memory before applying a ternary function in order to achieve better performance. |
| 139 | + |
| 140 | +</section> |
| 141 | + |
| 142 | +<!-- /.notes --> |
| 143 | + |
| 144 | +<!-- Package usage examples. --> |
| 145 | + |
| 146 | +<section class="examples"> |
| 147 | + |
| 148 | +## Examples |
| 149 | + |
| 150 | +<!-- eslint no-undef: "error" --> |
| 151 | + |
| 152 | +```javascript |
| 153 | +var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ).factory; |
| 154 | +var filledarray = require( '@stdlib/array/filled' ); |
| 155 | +var filledarrayBy = require( '@stdlib/array/filled-by' ); |
| 156 | +var add3 = require( '@stdlib/number/float64/base/add3' ); |
| 157 | +var shape2strides = require( '@stdlib/ndarray/base/shape2strides' ); |
| 158 | +var ndarray2array = require( '@stdlib/ndarray/base/to-array' ); |
| 159 | +var ternary = require( '@stdlib/ndarray/base/ternary' ); |
| 160 | + |
| 161 | +var N = 10; |
| 162 | +var x = { |
| 163 | + 'dtype': 'generic', |
| 164 | + 'data': filledarrayBy( N, 'generic', discreteUniform( -100, 100 ) ), |
| 165 | + 'shape': [ 5, 2 ], |
| 166 | + 'strides': [ 2, 1 ], |
| 167 | + 'offset': 0, |
| 168 | + 'order': 'row-major' |
| 169 | +}; |
| 170 | +console.log( ndarray2array( x.data, x.shape, x.strides, x.offset, x.order ) ); |
| 171 | + |
| 172 | +var y = { |
| 173 | + 'dtype': 'generic', |
| 174 | + 'data': filledarrayBy( N, 'generic', discreteUniform( -100, 100 ) ), |
| 175 | + 'shape': x.shape.slice(), |
| 176 | + 'strides': shape2strides( x.shape, 'column-major' ), |
| 177 | + 'offset': 0, |
| 178 | + 'order': 'column-major' |
| 179 | +}; |
| 180 | +console.log( ndarray2array( y.data, y.shape, y.strides, y.offset, y.order ) ); |
| 181 | + |
| 182 | +var z = { |
| 183 | + 'dtype': 'generic', |
| 184 | + 'data': filledarrayBy( N, 'generic', discreteUniform( -100, 100 ) ), |
| 185 | + 'shape': x.shape.slice(), |
| 186 | + 'strides': shape2strides( x.shape, 'row-major' ), |
| 187 | + 'offset': 0, |
| 188 | + 'order': 'row-major' |
| 189 | +}; |
| 190 | +console.log( ndarray2array( z.data, z.shape, z.strides, z.offset, z.order ) ); |
| 191 | + |
| 192 | +var w = { |
| 193 | + 'dtype': 'generic', |
| 194 | + 'data': filledarray( 0, N, 'generic' ), |
| 195 | + 'shape': x.shape.slice(), |
| 196 | + 'strides': shape2strides( x.shape, 'column-major' ), |
| 197 | + 'offset': 0, |
| 198 | + 'order': 'column-major' |
| 199 | +}; |
| 200 | + |
| 201 | +ternary( [ x, y, z, w ], add3 ); |
| 202 | +console.log( ndarray2array( w.data, w.shape, w.strides, w.offset, w.order ) ); |
| 203 | +``` |
| 204 | + |
| 205 | +</section> |
| 206 | + |
| 207 | +<!-- /.examples --> |
| 208 | + |
| 209 | +<!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 210 | + |
| 211 | +<section class="references"> |
| 212 | + |
| 213 | +</section> |
| 214 | + |
| 215 | +<!-- /.references --> |
| 216 | + |
| 217 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 218 | + |
| 219 | +<section class="related"> |
| 220 | + |
| 221 | +</section> |
| 222 | + |
| 223 | +<!-- /.related --> |
| 224 | + |
| 225 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 226 | + |
| 227 | +<section class="links"> |
| 228 | + |
| 229 | +</section> |
| 230 | + |
| 231 | +<!-- /.links --> |
0 commit comments