|
| 1 | +<!-- |
| 2 | +
|
| 3 | +@license Apache-2.0 |
| 4 | +
|
| 5 | +Copyright (c) 2018 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 | +# Promotion Rules |
| 22 | + |
| 23 | +> Return the ndarray [data type][@stdlib/ndarray/dtypes] with the smallest size to which ndarray [data types][@stdlib/ndarray/dtypes] can be **safely** cast. |
| 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 promotionRules = require( '@stdlib/ndarray/promotion-rules' ); |
| 41 | +``` |
| 42 | + |
| 43 | +#### promotionRules( \[dtype1, dtype2] ) |
| 44 | + |
| 45 | +If provided [data types][@stdlib/ndarray/dtypes], returns the ndarray [data type][@stdlib/ndarray/dtypes] with the smallest size to which ndarray [data types][@stdlib/ndarray/dtypes] can be **safely** cast. |
| 46 | + |
| 47 | +```javascript |
| 48 | +var out = promotionRules( 'float32', 'uint32' ); |
| 49 | +// e.g., returns [ 'float64' ] |
| 50 | +``` |
| 51 | + |
| 52 | +If a [data type][@stdlib/ndarray/dtypes] to which [data types][@stdlib/ndarray/dtypes] can be safely cast does **not** exist (or is not supported), the function returns `-1`. |
| 53 | + |
| 54 | +```javascript |
| 55 | +var out = promotionRules( 'binary', 'generic' ); |
| 56 | +// returns -1 |
| 57 | +``` |
| 58 | + |
| 59 | +If not provided [data types][@stdlib/ndarray/dtypes], the function returns a promotion table. |
| 60 | + |
| 61 | +```javascript |
| 62 | +var out = promotionRules(); |
| 63 | +// returns {...} |
| 64 | +``` |
| 65 | + |
| 66 | +If provided an unrecognized or unsupported `dtype`, the function returns `null`. |
| 67 | + |
| 68 | +```javascript |
| 69 | +var out = promotionRules( 'foo', 'generic' ); |
| 70 | +// returns null |
| 71 | +``` |
| 72 | + |
| 73 | +</section> |
| 74 | + |
| 75 | +<!-- /.usage --> |
| 76 | + |
| 77 | +<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 78 | + |
| 79 | +<section class="notes"> |
| 80 | + |
| 81 | +</section> |
| 82 | + |
| 83 | +<!-- /.notes --> |
| 84 | + |
| 85 | +<!-- Package usage examples. --> |
| 86 | + |
| 87 | +<section class="examples"> |
| 88 | + |
| 89 | +## Examples |
| 90 | + |
| 91 | +<!-- eslint no-undef: "error" --> |
| 92 | + |
| 93 | +```javascript |
| 94 | +var dtypes = require( '@stdlib/ndarray/dtypes' ); |
| 95 | +var promotionRules = require( '@stdlib/ndarray/promotion-rules' ); |
| 96 | + |
| 97 | +var DTYPES; |
| 98 | +var dt1; |
| 99 | +var dt2; |
| 100 | +var dt; |
| 101 | +var i; |
| 102 | +var j; |
| 103 | + |
| 104 | +// Get the list of supported ndarray data types: |
| 105 | +DTYPES = dtypes(); |
| 106 | + |
| 107 | +// Print the promotion rule for each pair of ndarray data types... |
| 108 | +for ( i = 0; i < DTYPES.length; i++ ) { |
| 109 | + dt1 = DTYPES[ i ]; |
| 110 | + for ( j = 0; j < DTYPES.length; j++ ) { |
| 111 | + dt2 = DTYPES[ j ]; |
| 112 | + dt = promotionRules( dt1, dt2 ); |
| 113 | + console.log( '(%s, %s) => %s', dt1, dt2, dt ); |
| 114 | + } |
| 115 | +} |
| 116 | +``` |
| 117 | + |
| 118 | +</section> |
| 119 | + |
| 120 | +<!-- /.examples --> |
| 121 | + |
| 122 | +<!-- 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. --> |
| 123 | + |
| 124 | +<section class="references"> |
| 125 | + |
| 126 | +</section> |
| 127 | + |
| 128 | +<!-- /.references --> |
| 129 | + |
| 130 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 131 | + |
| 132 | +<section class="links"> |
| 133 | + |
| 134 | +[@stdlib/ndarray/dtypes]: https://github.com/stdlib-js/stdlib |
| 135 | + |
| 136 | +</section> |
| 137 | + |
| 138 | +<!-- /.links --> |
0 commit comments