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