|
| 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 | +# ternaryOutputDataType |
| 22 | + |
| 23 | +> Resolve the output ndarray [data type][@stdlib/ndarray/dtypes] for a ternary function. |
| 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 ternaryOutputDataType = require( '@stdlib/ndarray/base/ternary-output-dtype' ); |
| 41 | +``` |
| 42 | + |
| 43 | +#### ternaryOutputDataType( xdtype, ydtype, zdtype, policy ) |
| 44 | + |
| 45 | +Resolves the output ndarray [data type][@stdlib/ndarray/dtypes] for a ternary function according to a [data type policy][@stdlib/ndarray/output-dtype-policies]. |
| 46 | + |
| 47 | +```javascript |
| 48 | +var dt = ternaryOutputDataType( 'int32', 'float32', 'float32', 'floating_point' ); |
| 49 | + |
| 50 | +var s = String( dt ); |
| 51 | +// returns 'float64' |
| 52 | +``` |
| 53 | + |
| 54 | +The function supports the following parameters: |
| 55 | + |
| 56 | +- **xdtype**: first input ndarray [data type][@stdlib/ndarray/dtypes]. |
| 57 | +- **ydtype**: second input ndarray [data type][@stdlib/ndarray/dtypes]. |
| 58 | +- **zdtype**: third input ndarray [data type][@stdlib/ndarray/dtypes]. |
| 59 | +- **policy**: output [data type policy][@stdlib/ndarray/output-dtype-policies]. |
| 60 | + |
| 61 | +If `policy` is a [data type][@stdlib/ndarray/dtypes], the function always returns the `policy` value (i.e., the third argument). |
| 62 | + |
| 63 | +```javascript |
| 64 | +var dt = ternaryOutputDataType( 'float32', 'float32', 'float32', 'float64' ); |
| 65 | + |
| 66 | +var s = String( dt ); |
| 67 | +// returns 'float64' |
| 68 | + |
| 69 | +dt = ternaryOutputDataType( 'int32', 'int8', 'int32', 'float64' ); |
| 70 | + |
| 71 | +s = String( dt ); |
| 72 | +// returns 'float64' |
| 73 | + |
| 74 | +// ... |
| 75 | +``` |
| 76 | + |
| 77 | +</section> |
| 78 | + |
| 79 | +<!-- /.usage --> |
| 80 | + |
| 81 | +<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 82 | + |
| 83 | +<section class="notes"> |
| 84 | + |
| 85 | +## Notes |
| 86 | + |
| 87 | +- The function **always** applies [type promotion][@stdlib/ndarray/promotion-rules] to the provided data types, except for the following [data type policies][@stdlib/ndarray/output-dtype-policies]: |
| 88 | + |
| 89 | + - `default` |
| 90 | + - `default_index` |
| 91 | + - `same` |
| 92 | + - `<dtype>` |
| 93 | + |
| 94 | +</section> |
| 95 | + |
| 96 | +<!-- /.notes --> |
| 97 | + |
| 98 | +<!-- Package usage examples. --> |
| 99 | + |
| 100 | +<section class="examples"> |
| 101 | + |
| 102 | +## Examples |
| 103 | + |
| 104 | +<!-- eslint no-undef: "error" --> |
| 105 | + |
| 106 | +```javascript |
| 107 | +var naryFunction = require( '@stdlib/utils/nary-function' ); |
| 108 | +var unzip = require( '@stdlib/utils/unzip' ); |
| 109 | +var nCartesianProduct = require( '@stdlib/array/base/n-cartesian-product' ); |
| 110 | +var dtypes = require( '@stdlib/ndarray/dtypes' ); |
| 111 | +var logEachMap = require( '@stdlib/console/log-each-map' ); |
| 112 | +var ternaryOutputDataType = require( '@stdlib/ndarray/base/ternary-output-dtype' ); |
| 113 | + |
| 114 | +// Get the list of real-valued floating-point data types: |
| 115 | +var dt = dtypes( 'real_floating_point' ); |
| 116 | + |
| 117 | +// Define a list of output data type policies: |
| 118 | +var policies = [ |
| 119 | + 'default', |
| 120 | + 'real', |
| 121 | + 'floating_point', |
| 122 | + 'complex_floating_point' |
| 123 | +]; |
| 124 | + |
| 125 | +// Generate dtype-policy Cartesian products: |
| 126 | +var args = nCartesianProduct( dt, dt, dt, policies ); |
| 127 | + |
| 128 | +// Unzip the argument pair array: |
| 129 | +args = unzip( args ); |
| 130 | + |
| 131 | +// Resolve output data types: |
| 132 | +logEachMap( 'dtypes: (%7s, %7s, %7s). policy: %-24s. output dtype: %s.', args[ 0 ], args[ 1 ], args[ 2 ], args[ 3 ], naryFunction( ternaryOutputDataType, 4 ) ); |
| 133 | +``` |
| 134 | + |
| 135 | +</section> |
| 136 | + |
| 137 | +<!-- /.examples --> |
| 138 | + |
| 139 | +<!-- 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. --> |
| 140 | + |
| 141 | +<section class="references"> |
| 142 | + |
| 143 | +</section> |
| 144 | + |
| 145 | +<!-- /.references --> |
| 146 | + |
| 147 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 148 | + |
| 149 | +<section class="related"> |
| 150 | + |
| 151 | +</section> |
| 152 | + |
| 153 | +<!-- /.related --> |
| 154 | + |
| 155 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 156 | + |
| 157 | +<section class="links"> |
| 158 | + |
| 159 | +[@stdlib/ndarray/dtypes]: https://github.com/stdlib-js/ndarray/tree/main/dtypes |
| 160 | + |
| 161 | +[@stdlib/ndarray/output-dtype-policies]: https://github.com/stdlib-js/ndarray/tree/main/output-dtype-policies |
| 162 | + |
| 163 | +[@stdlib/ndarray/promotion-rules]: https://github.com/stdlib-js/ndarray/tree/main/promotion-rules |
| 164 | + |
| 165 | +</section> |
| 166 | + |
| 167 | +<!-- /.links --> |
0 commit comments