Skip to content

Commit 604e204

Browse files
committed
Add pkg for returning data type promotion rules
1 parent c921c64 commit 604e204

File tree

11 files changed

+989
-0
lines changed

11 files changed

+989
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2018 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+
# Promotion Rules
22+
23+
> Return the ndarray [data type][@stdlib/ndarray/dtypes] with the smallest size to which ndarray [data types][@stdlib/ndarray/dtypes] can be **safely** cast.
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 promotionRules = require( '@stdlib/ndarray/promotion-rules' );
41+
```
42+
43+
#### promotionRules( \[dtype1, dtype2] )
44+
45+
If provided [data types][@stdlib/ndarray/dtypes], returns the ndarray [data type][@stdlib/ndarray/dtypes] with the smallest size to which ndarray [data types][@stdlib/ndarray/dtypes] can be **safely** cast.
46+
47+
```javascript
48+
var out = promotionRules( 'float32', 'uint32' );
49+
// e.g., returns [ 'float64' ]
50+
```
51+
52+
If a [data type][@stdlib/ndarray/dtypes] to which [data types][@stdlib/ndarray/dtypes] can be safely cast does **not** exist (or is not supported), the function returns `-1`.
53+
54+
```javascript
55+
var out = promotionRules( 'binary', 'generic' );
56+
// returns -1
57+
```
58+
59+
If not provided [data types][@stdlib/ndarray/dtypes], the function returns a promotion table.
60+
61+
```javascript
62+
var out = promotionRules();
63+
// returns {...}
64+
```
65+
66+
If provided an unrecognized or unsupported `dtype`, the function returns `null`.
67+
68+
```javascript
69+
var out = promotionRules( 'foo', 'generic' );
70+
// returns null
71+
```
72+
73+
</section>
74+
75+
<!-- /.usage -->
76+
77+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
78+
79+
<section class="notes">
80+
81+
</section>
82+
83+
<!-- /.notes -->
84+
85+
<!-- Package usage examples. -->
86+
87+
<section class="examples">
88+
89+
## Examples
90+
91+
<!-- eslint no-undef: "error" -->
92+
93+
```javascript
94+
var dtypes = require( '@stdlib/ndarray/dtypes' );
95+
var promotionRules = require( '@stdlib/ndarray/promotion-rules' );
96+
97+
var DTYPES;
98+
var dt1;
99+
var dt2;
100+
var dt;
101+
var i;
102+
var j;
103+
104+
// Get the list of supported ndarray data types:
105+
DTYPES = dtypes();
106+
107+
// Print the promotion rule for each pair of ndarray data types...
108+
for ( i = 0; i < DTYPES.length; i++ ) {
109+
dt1 = DTYPES[ i ];
110+
for ( j = 0; j < DTYPES.length; j++ ) {
111+
dt2 = DTYPES[ j ];
112+
dt = promotionRules( dt1, dt2 );
113+
console.log( '(%s, %s) => %s', dt1, dt2, dt );
114+
}
115+
}
116+
```
117+
118+
</section>
119+
120+
<!-- /.examples -->
121+
122+
<!-- 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. -->
123+
124+
<section class="references">
125+
126+
</section>
127+
128+
<!-- /.references -->
129+
130+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
131+
132+
<section class="links">
133+
134+
[@stdlib/ndarray/dtypes]: https://github.com/stdlib-js/stdlib
135+
136+
</section>
137+
138+
<!-- /.links -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2018 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 dtypes = require( '@stdlib/ndarray/dtypes' );
26+
var pkg = require( './../package.json' ).name;
27+
var promotionRules = require( './../lib' );
28+
29+
30+
// MAIN //
31+
32+
bench( pkg, function benchmark( b ) {
33+
var out;
34+
var i;
35+
36+
b.tic();
37+
for ( i = 0; i < b.iterations; i++ ) {
38+
out = promotionRules();
39+
if ( typeof out !== 'object' ) {
40+
b.fail( 'should return an object' );
41+
}
42+
}
43+
b.toc();
44+
if ( typeof out !== 'object' ) {
45+
b.fail( 'should return an object' );
46+
}
47+
b.pass( 'benchmark finished' );
48+
b.end();
49+
});
50+
51+
bench( pkg+'::dtypes', function benchmark( b ) {
52+
var out;
53+
var dt;
54+
var N;
55+
var i;
56+
var j;
57+
var k;
58+
59+
dt = dtypes();
60+
N = dt.length;
61+
62+
b.tic();
63+
for ( i = 0; i < b.iterations; i++ ) {
64+
j = i % N;
65+
k = (i+1) % N;
66+
out = promotionRules( dt[ j ], dt[ k ] );
67+
if ( typeof out !== 'string' && out !== -1 ) {
68+
b.fail( 'should return a string or -1' );
69+
}
70+
}
71+
b.toc();
72+
if ( !isString( out ) && out !== -1 ) {
73+
b.fail( 'should return a string or -1' );
74+
}
75+
b.pass( 'benchmark finished' );
76+
b.end();
77+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
2+
{{alias}}( [dtype1, dtype2] )
3+
Returns the ndarray data type with the smallest size to which ndarray data
4+
types can be safely cast.
5+
6+
If not provided an ndarray data type, the function returns a type promotion
7+
table.
8+
9+
If a data type to which data types can be safely cast does *not* exist (or
10+
is not supported), the function returns `-1`.
11+
12+
If provided an unrecognized ndarray data type, the function returns `null`.
13+
14+
Parameters
15+
----------
16+
dtype1: string (optional)
17+
ndarray data type.
18+
19+
dtype2: string (optional)
20+
ndarray data type.
21+
22+
Returns
23+
-------
24+
out: Object|string|integer|null
25+
Promotion rule(s).
26+
27+
Examples
28+
--------
29+
> var out = {{alias}}( 'float32', 'int32' )
30+
'float64'
31+
32+
See Also
33+
--------
34+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2018 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+
var dtypes = require( '@stdlib/ndarray/dtypes' );
22+
var promotionRules = require( './../lib' );
23+
24+
var DTYPES;
25+
var dt1;
26+
var dt2;
27+
var dt;
28+
var i;
29+
var j;
30+
31+
// Get the list of supported ndarray data types:
32+
DTYPES = dtypes();
33+
34+
// Print the promotion rule for each pair of ndarray data types...
35+
for ( i = 0; i < DTYPES.length; i++ ) {
36+
dt1 = DTYPES[ i ];
37+
for ( j = 0; j < DTYPES.length; j++ ) {
38+
dt2 = DTYPES[ j ];
39+
dt = promotionRules( dt1, dt2 );
40+
console.log( '(%s, %s) => %s', dt1, dt2, dt );
41+
}
42+
}

0 commit comments

Comments
 (0)