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