Skip to content

Commit c6195ff

Browse files
committed
feat: add ndarray/base/unary-input-casting-dtype
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: passed - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: passed - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed ---
1 parent 79f6ac4 commit c6195ff

File tree

10 files changed

+984
-0
lines changed

10 files changed

+984
-0
lines changed
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
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+
# unaryInputCastingDataType
22+
23+
> Resolve the input ndarray casting [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 unaryInputCastingDataType = require( '@stdlib/ndarray/base/unary-input-casting-dtype' );
41+
```
42+
43+
#### unaryInputCastingDataType( idtype, odtype, policy )
44+
45+
Resolves the input ndarray casting [data type][@stdlib/ndarray/dtypes] for a unary function according to a [data type policy][@stdlib/ndarray/input-casting-policies].
46+
47+
```javascript
48+
var dt = unaryInputCastingDataType( 'int32', 'float64', 'promoted' );
49+
// returns 'float64'
50+
```
51+
52+
The function supports the following parameters:
53+
54+
- **idtype**: input ndarray data type.
55+
- **odtype**: output ndarray data type.
56+
- **policy**: input ndarray [casting policy][@stdlib/ndarray/input-casting-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 = unaryInputCastingDataType( 'float32', 'float64', 'complex128' );
62+
// returns 'complex128'
63+
64+
dt = unaryInputCastingDataType( 'int32', 'float64', 'complex128' );
65+
// returns 'complex128'
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+
</section>
79+
80+
<!-- /.notes -->
81+
82+
<!-- Package usage examples. -->
83+
84+
<section class="examples">
85+
86+
## Examples
87+
88+
<!-- eslint no-undef: "error" -->
89+
90+
```javascript
91+
var naryFunction = require( '@stdlib/utils/nary-function' );
92+
var unzip = require( '@stdlib/utils/unzip' );
93+
var nCartesianProduct = require( '@stdlib/array/base/n-cartesian-product' );
94+
var dtypes = require( '@stdlib/ndarray/dtypes' );
95+
var logEachMap = require( '@stdlib/console/log-each-map' );
96+
var inputCastingDataType = require( '@stdlib/ndarray/base/unary-input-casting-dtype' );
97+
98+
// Get the list of real-valued data types:
99+
var dt = dtypes( 'real' );
100+
101+
// Define a list of casting policies:
102+
var policies = [
103+
'none',
104+
'promoted',
105+
'accumulation',
106+
'output'
107+
];
108+
109+
// Generate dtype-policy argument groups:
110+
var args = nCartesianProduct( dt, dt, policies );
111+
112+
// Unzip the argument arrays:
113+
args = unzip( args );
114+
115+
// Resolve casting data types:
116+
logEachMap( 'dtypes: (%10s, %10s). policy: %20s. result: %10s.', args[ 0 ], args[ 1 ], args[ 2 ], naryFunction( inputCastingDataType, 3 ) );
117+
```
118+
119+
</section>
120+
121+
<!-- /.examples -->
122+
123+
<!-- 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. -->
124+
125+
<section class="references">
126+
127+
</section>
128+
129+
<!-- /.references -->
130+
131+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
132+
133+
<section class="related">
134+
135+
</section>
136+
137+
<!-- /.related -->
138+
139+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
140+
141+
<section class="links">
142+
143+
[@stdlib/ndarray/dtypes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/dtypes
144+
145+
[@stdlib/ndarray/input-casting-policies]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/input-casting-policies
146+
147+
</section>
148+
149+
<!-- /.links -->
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var isDataType = require( '@stdlib/ndarray/base/assert/is-data-type' );
25+
var dtypes = require( '@stdlib/ndarray/dtypes' );
26+
var pkg = require( './../package.json' ).name;
27+
var resolve = require( './../lib' );
28+
29+
30+
// VARIABLES //
31+
32+
var DTYPES = dtypes( 'numeric' );
33+
34+
35+
// MAIN //
36+
37+
bench( pkg+':policy=none', function benchmark( b ) {
38+
var out;
39+
var dt1;
40+
var dt2;
41+
var i;
42+
43+
b.tic();
44+
for ( i = 0; i < b.iterations; i++ ) {
45+
dt1 = DTYPES[ i%DTYPES.length ];
46+
dt2 = DTYPES[ (i+1)%DTYPES.length ];
47+
out = resolve( dt1, dt2, 'none' );
48+
if ( typeof out !== 'string' ) {
49+
b.fail( 'should return a string' );
50+
}
51+
}
52+
b.toc();
53+
if ( !isDataType( out ) ) {
54+
b.fail( 'should return a data type' );
55+
}
56+
b.pass( 'benchmark finished' );
57+
b.end();
58+
});
59+
60+
bench( pkg+':policy=promoted', function benchmark( b ) {
61+
var out;
62+
var dt1;
63+
var dt2;
64+
var i;
65+
66+
b.tic();
67+
for ( i = 0; i < b.iterations; i++ ) {
68+
dt1 = DTYPES[ i%DTYPES.length ];
69+
dt2 = DTYPES[ (i+1)%DTYPES.length ];
70+
out = resolve( dt1, dt2, 'promoted' );
71+
if ( typeof out !== 'string' ) {
72+
b.fail( 'should return a string' );
73+
}
74+
}
75+
b.toc();
76+
if ( !isDataType( out ) ) {
77+
b.fail( 'should return a data type' );
78+
}
79+
b.pass( 'benchmark finished' );
80+
b.end();
81+
});
82+
83+
bench( pkg+':policy=accumulation', function benchmark( b ) {
84+
var out;
85+
var dt1;
86+
var dt2;
87+
var i;
88+
89+
b.tic();
90+
for ( i = 0; i < b.iterations; i++ ) {
91+
dt1 = DTYPES[ i%DTYPES.length ];
92+
dt2 = DTYPES[ (i+1)%DTYPES.length ];
93+
out = resolve( dt1, dt2, 'accumulation' );
94+
if ( typeof out !== 'string' ) {
95+
b.fail( 'should return a string' );
96+
}
97+
}
98+
b.toc();
99+
if ( !isDataType( out ) ) {
100+
b.fail( 'should return a data type' );
101+
}
102+
b.pass( 'benchmark finished' );
103+
b.end();
104+
});
105+
106+
bench( pkg+':policy=output', function benchmark( b ) {
107+
var out;
108+
var dt1;
109+
var dt2;
110+
var i;
111+
112+
b.tic();
113+
for ( i = 0; i < b.iterations; i++ ) {
114+
dt1 = DTYPES[ i%DTYPES.length ];
115+
dt2 = DTYPES[ (i+1)%DTYPES.length ];
116+
out = resolve( dt1, dt2, 'output' );
117+
if ( typeof out !== 'string' ) {
118+
b.fail( 'should return a string' );
119+
}
120+
}
121+
b.toc();
122+
if ( !isDataType( out ) ) {
123+
b.fail( 'should return a data type' );
124+
}
125+
b.pass( 'benchmark finished' );
126+
b.end();
127+
});
128+
129+
bench( pkg+':policy=<dtype>', function benchmark( b ) {
130+
var out;
131+
var dt1;
132+
var dt2;
133+
var i;
134+
135+
b.tic();
136+
for ( i = 0; i < b.iterations; i++ ) {
137+
dt1 = DTYPES[ i%DTYPES.length ];
138+
dt2 = DTYPES[ (i+1)%DTYPES.length ];
139+
out = resolve( dt1, dt2, 'int32' );
140+
if ( typeof out !== 'string' ) {
141+
b.fail( 'should return a string' );
142+
}
143+
}
144+
b.toc();
145+
if ( !isDataType( out ) ) {
146+
b.fail( 'should return a data type' );
147+
}
148+
b.pass( 'benchmark finished' );
149+
b.end();
150+
});
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
2+
{{alias}}( idtype, odtype, policy )
3+
Resolves the input ndarray casting data type for a unary function.
4+
5+
Parameters
6+
----------
7+
idtype: string
8+
Input ndarray data type.
9+
10+
odtype: string
11+
Output ndarray data type.
12+
13+
policy: string
14+
Input ndarray casting data type policy. If `policy` is a data type, the
15+
function returns the `policy` value.
16+
17+
Returns
18+
-------
19+
out: string
20+
Input ndarray casting data type.
21+
22+
Examples
23+
--------
24+
> var out = {{alias}}( 'float64', 'float64', 'none' )
25+
'float64'
26+
27+
See Also
28+
--------
29+
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
// TypeScript Version: 4.1
20+
21+
/// <reference types="@stdlib/types"/>
22+
23+
import { DataType, InputCastingPolicy } from '@stdlib/types/ndarray';
24+
25+
/**
26+
* Resolves the input ndarray casting data type for a unary function.
27+
*
28+
* @param idtype - input ndarray data type
29+
* @param odtype - output ndarray data type
30+
* @param policy - input ndarray casting data type policy
31+
* @returns input ndarray casting data type
32+
*
33+
* @example
34+
* var dt = outputDataType( 'float64', 'float64', 'none' );
35+
* // returns <string>
36+
*/
37+
declare function outputDataType( idtype: DataType, odtype: DataType, policy: InputCastingPolicy | DataType ): DataType;
38+
39+
40+
// EXPORTS //
41+
42+
export = outputDataType;

0 commit comments

Comments
 (0)