Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 266240f

Browse files
gururaj1512kgryte
andauthored
feat: add ndarray/base/binary-input-casting-dtype
PR-URL: #7904 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com>
1 parent 5d9761c commit 266240f

File tree

10 files changed

+1089
-0
lines changed

10 files changed

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

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /