Skip to content

Commit eec20f6

Browse files
committed
feat: add ndarray/base/unary-output-dtype
1 parent 66c36f4 commit eec20f6

File tree

10 files changed

+1251
-0
lines changed

10 files changed

+1251
-0
lines changed
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2023 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+
# unaryOutputDataType
22+
23+
> Resolve the output ndarray [data type][@stdlib/ndarray/dtypes] for a unary 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 unaryOutputDataType = require( '@stdlib/ndarray/base/unary-output-dtype' );
41+
```
42+
43+
#### unaryOutputDataType( dtype, policy )
44+
45+
Resolves the output ndarray [data type][@stdlib/ndarray/dtypes] for a unary function according to a [data type policy][@stdlib/ndarray/output-dtype-policies].
46+
47+
```javascript
48+
var dt = unaryOutputDataType( 'int32', 'floating_point' );
49+
// returns 'float64'
50+
```
51+
52+
If `policy` is a [data type][@stdlib/ndarray/dtypes], the function always returns the `policy` value (i.e., the second argument).
53+
54+
```javascript
55+
var dt = unaryOutputDataType( 'float32', 'float64' );
56+
// returns 'float64'
57+
58+
dt = unaryOutputDataType( 'int32', 'float64' );
59+
// returns 'float64'
60+
61+
// ...
62+
```
63+
64+
</section>
65+
66+
<!-- /.usage -->
67+
68+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
69+
70+
<section class="notes">
71+
72+
</section>
73+
74+
<!-- /.notes -->
75+
76+
<!-- Package usage examples. -->
77+
78+
<section class="examples">
79+
80+
## Examples
81+
82+
<!-- eslint no-undef: "error" -->
83+
84+
```javascript
85+
var naryFunction = require( '@stdlib/utils/nary-function' );
86+
var map2 = require( '@stdlib/utils/map2' );
87+
var unzip = require( '@stdlib/utils/unzip' );
88+
var cartesianProduct = require( '@stdlib/array/base/cartesian-product' );
89+
var dtypes = require( '@stdlib/ndarray/dtypes' );
90+
var logEach = require( '@stdlib/console/log-each' );
91+
var unaryOutputDataType = require( '@stdlib/ndarray/base/unary-output-dtype' );
92+
93+
// Get the list of real-valued data types:
94+
var dt = dtypes( 'real' );
95+
96+
// Define a list of output data type policies:
97+
var policies = [
98+
'default',
99+
'real',
100+
'floating_point',
101+
'complex_floating_point'
102+
];
103+
104+
// Generate dtype-policy argument pairs:
105+
var args = cartesianProduct( dt, policies );
106+
107+
// Unzip the argument pair array:
108+
args = unzip( args );
109+
110+
// Resolve output data types:
111+
var out = map2( args[ 0 ], args[ 1 ], naryFunction( unaryOutputDataType, 2 ) );
112+
113+
// Print results:
114+
logEach( 'dtypes: (%10s, %10s). policy: %s.', args[ 0 ], out, args[ 1 ] );
115+
```
116+
117+
</section>
118+
119+
<!-- /.examples -->
120+
121+
<!-- 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. -->
122+
123+
<section class="references">
124+
125+
</section>
126+
127+
<!-- /.references -->
128+
129+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
130+
131+
<section class="related">
132+
133+
</section>
134+
135+
<!-- /.related -->
136+
137+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
138+
139+
<section class="links">
140+
141+
[@stdlib/ndarray/dtypes]: https://github.com/stdlib-js/stdlib
142+
143+
[@stdlib/ndarray/output-dtype-policies]: https://github.com/stdlib-js/stdlib
144+
145+
</section>
146+
147+
<!-- /.links -->

0 commit comments

Comments
 (0)