Skip to content

Commit ec51b4f

Browse files
committed
feat: add ndarray/base/promote-dtypes
--- 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 ad9966a commit ec51b4f

File tree

10 files changed

+655
-0
lines changed

10 files changed

+655
-0
lines changed
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
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+
# promoteDataTypes
22+
23+
> Resolve the data type that results from applying [promotion rules][@stdlib/ndarray/promotion-rules] to a provided list of data types.
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 promoteDataTypes = require( '@stdlib/ndarray/base/promote-dtypes' );
41+
```
42+
43+
#### promoteDataTypes( dtypes )
44+
45+
Returns a data type that results from applying [promotion rules][@stdlib/ndarray/promotion-rules] to a provided list of data types.
46+
47+
```javascript
48+
var dt = promoteDataTypes( [ 'float32', 'float64' ] );
49+
// returns 'float64'
50+
```
51+
52+
The function returns `null` if provided data types which cannot be safely cast to a promoted data type.
53+
54+
```javascript
55+
var dt = promoteDataTypes( [ 'binary', 'complex128' ] );
56+
// returns null
57+
```
58+
59+
</section>
60+
61+
<!-- /.usage -->
62+
63+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
64+
65+
<section class="notes">
66+
67+
</section>
68+
69+
<!-- /.notes -->
70+
71+
<!-- Package usage examples. -->
72+
73+
<section class="examples">
74+
75+
## Examples
76+
77+
<!-- eslint no-undef: "error" -->
78+
79+
```javascript
80+
var dtypes = require( '@stdlib/ndarray/dtypes' );
81+
var cartesianProduct = require( '@stdlib/array/base/cartesian-product' );
82+
var logEachMap = require( '@stdlib/console/log-each-map' );
83+
var promoteDataTypes = require( '@stdlib/ndarray/base/promote-dtypes' );
84+
85+
// Get the list of supported data types:
86+
var dt = dtypes();
87+
88+
// Generate data type pairs:
89+
var pairs = cartesianProduct( dt, dt );
90+
91+
// Resolve promoted data types:
92+
logEachMap( '(%s) => %s', pairs, promoteDataTypes );
93+
```
94+
95+
</section>
96+
97+
<!-- /.examples -->
98+
99+
<!-- 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. -->
100+
101+
<section class="references">
102+
103+
</section>
104+
105+
<!-- /.references -->
106+
107+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
108+
109+
<section class="related">
110+
111+
</section>
112+
113+
<!-- /.related -->
114+
115+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
116+
117+
<section class="links">
118+
119+
[@stdlib/ndarray/promotion-rules]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/promotion-rules
120+
121+
</section>
122+
123+
<!-- /.links -->
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
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 isString = require( '@stdlib/assert/is-string' ).isPrimitive;
25+
var nCartesianProduct = require( '@stdlib/array/base/n-cartesian-product' );
26+
var dtypes = require( '@stdlib/ndarray/dtypes' );
27+
var pkg = require( './../package.json' ).name;
28+
var promoteDataTypes = require( './../lib' );
29+
30+
31+
// VARIABLES //
32+
33+
var DTYPES = dtypes();
34+
35+
36+
// MAIN //
37+
38+
bench( pkg+'::one_dtype', function benchmark( b ) {
39+
var v;
40+
var i;
41+
42+
b.tic();
43+
for ( i = 0; i < b.iterations; i++ ) {
44+
v = promoteDataTypes( [ DTYPES[ i%DTYPES.length ] ] );
45+
if ( typeof v !== 'string' && v !== null ) {
46+
b.fail( 'unexpected result' );
47+
}
48+
}
49+
b.toc();
50+
if ( !isString( v ) && v !== null ) {
51+
b.fail( 'unexpected result' );
52+
}
53+
b.pass( 'benchmark finished' );
54+
b.end();
55+
});
56+
57+
bench( pkg+'::two_dtypes', function benchmark( b ) {
58+
var values;
59+
var v;
60+
var i;
61+
62+
values = nCartesianProduct( DTYPES, DTYPES );
63+
64+
b.tic();
65+
for ( i = 0; i < b.iterations; i++ ) {
66+
v = promoteDataTypes( values[ i%values.length ] );
67+
if ( typeof v !== 'string' && v !== null ) {
68+
b.fail( 'unexpected result' );
69+
}
70+
}
71+
b.toc();
72+
if ( !isString( v ) && v !== null ) {
73+
b.fail( 'unexpected result' );
74+
}
75+
b.pass( 'benchmark finished' );
76+
b.end();
77+
});
78+
79+
bench( pkg+'::three_dtypes', function benchmark( b ) {
80+
var values;
81+
var v;
82+
var i;
83+
84+
values = nCartesianProduct( DTYPES, DTYPES, DTYPES );
85+
86+
b.tic();
87+
for ( i = 0; i < b.iterations; i++ ) {
88+
v = promoteDataTypes( values[ i%values.length ] );
89+
if ( typeof v !== 'string' && v !== null ) {
90+
b.fail( 'unexpected result' );
91+
}
92+
}
93+
b.toc();
94+
if ( !isString( v ) && v !== null ) {
95+
b.fail( 'unexpected result' );
96+
}
97+
b.pass( 'benchmark finished' );
98+
b.end();
99+
});
100+
101+
bench( pkg+'::four_dtypes', function benchmark( b ) {
102+
var values;
103+
var v;
104+
var i;
105+
106+
values = nCartesianProduct( DTYPES, DTYPES, DTYPES, DTYPES );
107+
108+
b.tic();
109+
for ( i = 0; i < b.iterations; i++ ) {
110+
v = promoteDataTypes( values[ i%values.length ] );
111+
if ( typeof v !== 'string' && v !== null ) {
112+
b.fail( 'unexpected result' );
113+
}
114+
}
115+
b.toc();
116+
if ( !isString( v ) && v !== null ) {
117+
b.fail( 'unexpected result' );
118+
}
119+
b.pass( 'benchmark finished' );
120+
b.end();
121+
});
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
2+
{{alias}}( dtypes )
3+
Returns a data type that results from applying promotion rules to a provided
4+
list of data types.
5+
6+
The function returns `null` if provided data types which cannot be safely
7+
cast to a promoted data type.
8+
9+
Parameters
10+
----------
11+
dtypes: Array<string>
12+
List of data types.
13+
14+
Returns
15+
-------
16+
out: string|null
17+
Result.
18+
19+
Examples
20+
--------
21+
> var dt = {{alias}}( [ 'float32', 'float64' ] )
22+
'float64'
23+
> dt = {{alias}}( [ 'binary', 'complex128' ] )
24+
null
25+
26+
See Also
27+
--------
28+
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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 } from '@stdlib/types/ndarray';
24+
25+
/**
26+
* Returns a data type that results from applying promotion rules to a provided list of data types.
27+
*
28+
* ## Notes
29+
*
30+
* - The function returns `null` if provided data types which cannot be safely cast to a promoted data type.
31+
*
32+
* @param dtypes - list of data types
33+
* @returns result
34+
*
35+
* @example
36+
* var dt = promoteDataTypes( [ 'float32', 'float64' ] );
37+
* // returns 'float64'
38+
*
39+
* @example
40+
* var dt = promoteDataTypes( [ 'binary', 'complex128' ] );
41+
* // returns null
42+
*/
43+
declare function promoteDataTypes( dtypes: ArrayLike<DataType> ): DataType | null;
44+
45+
46+
// EXPORTS //
47+
48+
export = promoteDataTypes;
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
import promoteDataTypes = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns a data type or null...
25+
{
26+
promoteDataTypes( [ 'float64', 'float64' ] ); // $ExpectType DataType | null
27+
}
28+
29+
// The compiler throws an error if the function is not provided an array-like object containing data types...
30+
{
31+
promoteDataTypes( '5' ); // $ExpectError
32+
promoteDataTypes( 5 ); // $ExpectError
33+
promoteDataTypes( true ); // $ExpectError
34+
promoteDataTypes( false ); // $ExpectError
35+
promoteDataTypes( null ); // $ExpectError
36+
promoteDataTypes( {} ); // $ExpectError
37+
promoteDataTypes( [ '5' ] ); // $ExpectError
38+
promoteDataTypes( ( x: number ): number => x ); // $ExpectError
39+
}
40+
41+
// The compiler throws an error if the function is provided an unsupported number of arguments...
42+
{
43+
promoteDataTypes(); // $ExpectError
44+
promoteDataTypes( [ 'float64' ], {} ); // $ExpectError
45+
}

0 commit comments

Comments
 (0)