Skip to content

Commit a2b2606

Browse files
committed
feat: add ndarray/base/binary-input-casting-dtype
1 parent ca26c53 commit a2b2606

File tree

10 files changed

+1089
-0
lines changed

10 files changed

+1089
-0
lines changed
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
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+
# binaryInputCastingDataType
22+
23+
> Resolve the input ndarray casting [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 binaryInputCastingDataType = require( '@stdlib/ndarray/base/binary-input-casting-dtype' );
41+
```
42+
43+
#### binaryInputCastingDataType( idtype1, idtype2, odtype, policy )
44+
45+
Resolves the input ndarray casting [data type][@stdlib/ndarray/dtypes] for a binary function according to a [data type policy][@stdlib/ndarray/input-casting-policies].
46+
47+
```javascript
48+
var dt = binaryInputCastingDataType( 'int32', 'int32', 'float64', 'promoted' );
49+
// returns 'float64'
50+
```
51+
52+
The function supports the following parameters:
53+
54+
- **idtype1**: first input ndarray data type.
55+
- **idtype2**: second input ndarray data type.
56+
- **odtype**: output ndarray data type.
57+
- **policy**: input ndarray [casting policy][@stdlib/ndarray/input-casting-policies].
58+
59+
If `policy` is a [data type][@stdlib/ndarray/dtypes], the function always returns the `policy` value (i.e., the fourth argument).
60+
61+
```javascript
62+
var dt = binaryInputCastingDataType( 'float32', 'float32', 'float64', 'complex128' );
63+
// returns 'complex128'
64+
65+
dt = binaryInputCastingDataType( 'int32', 'float64', 'float64', 'complex128' );
66+
// returns 'complex128'
67+
68+
// ...
69+
```
70+
71+
</section>
72+
73+
<!-- /.usage -->
74+
75+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
76+
77+
<section class="notes">
78+
79+
</section>
80+
81+
<!-- /.notes -->
82+
83+
<!-- Package usage examples. -->
84+
85+
<section class="examples">
86+
87+
## Examples
88+
89+
<!-- eslint no-undef: "error" -->
90+
91+
```javascript
92+
var naryFunction = require( '@stdlib/utils/nary-function' );
93+
var unzip = require( '@stdlib/utils/unzip' );
94+
var nCartesianProduct = require( '@stdlib/array/base/n-cartesian-product' );
95+
var dtypes = require( '@stdlib/ndarray/dtypes' );
96+
var logEachMap = require( '@stdlib/console/log-each-map' );
97+
var inputCastingDataType = require( '@stdlib/ndarray/base/binary-input-casting-dtype' );
98+
99+
// Get the list of real-valued data types:
100+
var dt = dtypes( 'real' );
101+
102+
// Define a list of casting policies:
103+
var policies = [
104+
'none',
105+
'promoted',
106+
'accumulation',
107+
'output'
108+
];
109+
110+
// Generate dtype-policy argument groups:
111+
var args = nCartesianProduct( dt, dt, policies );
112+
113+
// Unzip the argument arrays:
114+
args = unzip( args );
115+
116+
// Resolve casting data types:
117+
logEachMap( 'dtypes: (%10s, %10s). policy: %20s. result: %10s.', args[ 0 ], args[ 1 ], args[ 2 ], naryFunction( inputCastingDataType, 3 ) );
118+
```
119+
120+
</section>
121+
122+
<!-- /.examples -->
123+
124+
<!-- 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. -->
125+
126+
<section class="references">
127+
128+
</section>
129+
130+
<!-- /.references -->
131+
132+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
133+
134+
<section class="related">
135+
136+
</section>
137+
138+
<!-- /.related -->
139+
140+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
141+
142+
<section class="links">
143+
144+
[@stdlib/ndarray/dtypes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/dtypes
145+
146+
[@stdlib/ndarray/input-casting-policies]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/input-casting-policies
147+
148+
</section>
149+
150+
<!-- /.links -->
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
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 dt3;
42+
var i;
43+
44+
b.tic();
45+
for ( i = 0; i < b.iterations; i++ ) {
46+
dt1 = DTYPES[ i%DTYPES.length ];
47+
dt2 = DTYPES[ i%DTYPES.length ];
48+
dt3 = DTYPES[ (i+1)%DTYPES.length ];
49+
out = resolve( dt1, dt2, dt3, 'none' );
50+
if ( typeof out !== 'string' ) {
51+
b.fail( 'should return a string' );
52+
}
53+
}
54+
b.toc();
55+
if ( !isDataType( out ) ) {
56+
b.fail( 'should return a data type' );
57+
}
58+
b.pass( 'benchmark finished' );
59+
b.end();
60+
});
61+
62+
bench( pkg+':policy=promoted', function benchmark( b ) {
63+
var out;
64+
var dt1;
65+
var dt2;
66+
var dt3;
67+
var i;
68+
69+
b.tic();
70+
for ( i = 0; i < b.iterations; i++ ) {
71+
dt1 = DTYPES[ i%DTYPES.length ];
72+
dt2 = DTYPES[ i%DTYPES.length ];
73+
dt3 = DTYPES[ (i+1)%DTYPES.length ];
74+
out = resolve( dt1, dt2, dt3, 'promoted' );
75+
if ( typeof out !== 'string' ) {
76+
b.fail( 'should return a string' );
77+
}
78+
}
79+
b.toc();
80+
if ( !isDataType( out ) ) {
81+
b.fail( 'should return a data type' );
82+
}
83+
b.pass( 'benchmark finished' );
84+
b.end();
85+
});
86+
87+
bench( pkg+':policy=accumulation', function benchmark( b ) {
88+
var out;
89+
var dt1;
90+
var dt2;
91+
var dt3;
92+
var i;
93+
94+
b.tic();
95+
for ( i = 0; i < b.iterations; i++ ) {
96+
dt1 = DTYPES[ i%DTYPES.length ];
97+
dt2 = DTYPES[ i%DTYPES.length ];
98+
dt3 = DTYPES[ (i+1)%DTYPES.length ];
99+
out = resolve( dt1, dt2, dt3, 'accumulation' );
100+
if ( typeof out !== 'string' ) {
101+
b.fail( 'should return a string' );
102+
}
103+
}
104+
b.toc();
105+
if ( !isDataType( out ) ) {
106+
b.fail( 'should return a data type' );
107+
}
108+
b.pass( 'benchmark finished' );
109+
b.end();
110+
});
111+
112+
bench( pkg+':policy=output', function benchmark( b ) {
113+
var out;
114+
var dt1;
115+
var dt2;
116+
var dt3;
117+
var i;
118+
119+
b.tic();
120+
for ( i = 0; i < b.iterations; i++ ) {
121+
dt1 = DTYPES[ i%DTYPES.length ];
122+
dt2 = DTYPES[ i%DTYPES.length ];
123+
dt3 = DTYPES[ (i+1)%DTYPES.length ];
124+
out = resolve( dt1, dt2, dt3, 'output' );
125+
if ( typeof out !== 'string' ) {
126+
b.fail( 'should return a string' );
127+
}
128+
}
129+
b.toc();
130+
if ( !isDataType( out ) ) {
131+
b.fail( 'should return a data type' );
132+
}
133+
b.pass( 'benchmark finished' );
134+
b.end();
135+
});
136+
137+
bench( pkg+':policy=<dtype>', function benchmark( b ) {
138+
var out;
139+
var dt1;
140+
var dt2;
141+
var dt3;
142+
var i;
143+
144+
b.tic();
145+
for ( i = 0; i < b.iterations; i++ ) {
146+
dt1 = DTYPES[ i%DTYPES.length ];
147+
dt2 = DTYPES[ i%DTYPES.length ];
148+
dt3 = DTYPES[ (i+1)%DTYPES.length ];
149+
out = resolve( dt1, dt2, dt3, 'int32' );
150+
if ( typeof out !== 'string' ) {
151+
b.fail( 'should return a string' );
152+
}
153+
}
154+
b.toc();
155+
if ( !isDataType( out ) ) {
156+
b.fail( 'should return a data type' );
157+
}
158+
b.pass( 'benchmark finished' );
159+
b.end();
160+
});
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
2+
{{alias}}( idtype, odtype, policy )
3+
Resolves the input ndarray casting data type for a binary function.
4+
5+
Parameters
6+
----------
7+
idtype1: string
8+
First input ndarray data type.
9+
10+
idtype2: string
11+
Second input ndarray data type.
12+
13+
odtype: string
14+
Output ndarray data type.
15+
16+
policy: string
17+
Input ndarray casting data type policy. If `policy` is a data type, the
18+
function returns the `policy` value.
19+
20+
Returns
21+
-------
22+
out: string
23+
Input ndarray casting data type.
24+
25+
Examples
26+
--------
27+
> var out = {{alias}}( 'float64', 'float64', 'float64', 'none' )
28+
'float64'
29+
30+
See Also
31+
--------
32+
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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 binary function.
27+
*
28+
* @param idtype1 - first input ndarray data type
29+
* @param idtype2 - second input ndarray data type
30+
* @param odtype - output ndarray data type
31+
* @param policy - input ndarray casting data type policy
32+
* @returns input ndarray casting data type
33+
*
34+
* @example
35+
* var dt = outputDataType( 'float64', 'float64', 'float64', 'none' );
36+
* // returns <string>
37+
*/
38+
declare function outputDataType( idtype1: DataType, idtype2: DataType, odtype: DataType, policy: InputCastingPolicy | DataType ): DataType;
39+
40+
41+
// EXPORTS //
42+
43+
export = outputDataType;

0 commit comments

Comments
 (0)