Skip to content

Commit d40825e

Browse files
committed
feat: add ndarray-base-scalar-dtype package
--- 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 51981c7 commit d40825e

12 files changed

Lines changed: 683 additions & 19 deletions

File tree

lib/node_modules/@stdlib/ndarray/base/atleastnd/lib/main.js

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,6 @@
2121
// MODULES //
2222

2323
var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' );
24-
var isNumber = require( '@stdlib/assert/is-number' ).isPrimitive;
25-
var isComplexLike = require( '@stdlib/assert/is-complex-like' );
26-
var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive;
27-
var complexDataType = require( '@stdlib/complex/dtype' );
2824
var broadcastScalar = require( '@stdlib/ndarray/base/broadcast-scalar' );
2925
var dims = require( '@stdlib/ndarray/base/ndims' );
3026
var defaults = require( '@stdlib/ndarray/defaults' );
@@ -34,15 +30,13 @@ var getStrides = require( '@stdlib/ndarray/base/strides' );
3430
var getOffset = require( '@stdlib/ndarray/base/offset' );
3531
var getOrder = require( '@stdlib/ndarray/base/order' );
3632
var getDType = require( '@stdlib/ndarray/base/dtype' );
33+
var scalarDType = require( '@stdlib/ndarray/base/scalar-dtype' );
3734
var getData = require( '@stdlib/ndarray/base/data-buffer' );
3835
var ones = require( '@stdlib/array/base/ones' );
3936

4037

4138
// VARIABLES //
4239

43-
var DEFAULT_REAL = defaults.get( 'dtypes.real_floating_point' );
44-
var DEFAULT_CMPLX = defaults.get( 'dtypes.complex_floating_point' );
45-
var DEFAULT_BOOL = defaults.get( 'dtypes.boolean' );
4640
var ORDER = defaults.get( 'order' );
4741

4842

@@ -108,18 +102,7 @@ function atleastnd( ndims, arrays ) {
108102
continue;
109103
}
110104
// For scalar values, resolve a corresponding ndarray data type...
111-
if ( isNumber( v ) ) { // TODO: consider abstracting this logic to an `ndarray/base/scalar-dtype` (???) package, as this logic is found elsewhere (e.g., `ndarray/from-scalar`) and it would be good to avoid duplication, especially as we add support for more ndarray data types
112-
dt = DEFAULT_REAL;
113-
} else if ( isBoolean( v ) ) {
114-
dt = DEFAULT_BOOL;
115-
} else if ( isComplexLike( v ) ) {
116-
dt = complexDataType( v );
117-
if ( dt === null ) {
118-
dt = DEFAULT_CMPLX;
119-
}
120-
} else {
121-
dt = 'generic';
122-
}
105+
dt = scalarDType(v);
123106
out.push( broadcastScalar( v, dt, ones( ndims ), ORDER ) );
124107
}
125108
return out;

lib/node_modules/@stdlib/ndarray/base/lib/index.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1084,6 +1084,15 @@ setReadOnly( ns, 'reverse', require( '@stdlib/ndarray/base/reverse' ) );
10841084
*/
10851085
setReadOnly( ns, 'reverseDimension', require( '@stdlib/ndarray/base/reverse-dimension' ) );
10861086

1087+
/**
1088+
* @name scalarDType
1089+
* @memberof ns
1090+
* @readonly
1091+
* @type {Function}
1092+
* @see {@link module:@stdlib/ndarray/base/scalar-dtype}
1093+
*/
1094+
setReadOnly( ns, 'scalarDType', require( '@stdlib/ndarray/base/scalar-dtype' ) );
1095+
10871096
/**
10881097
* @name serializeMetaData
10891098
* @memberof ns
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2026 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+
# scalar-dtype
22+
23+
> Return the default [ndarray][@stdlib/ndarray/base/ctor] data type for a given scalar value.
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 scalarDType = require( '@stdlib/ndarray/base/scalar-dtype' );
41+
```
42+
43+
#### scalarDType( value )
44+
45+
Returns the default [ndarray][@stdlib/ndarray/base/ctor] data type for a given scalar value.
46+
47+
```javascript
48+
var dt = scalarDType( 3.14 );
49+
// returns 'float64'
50+
51+
dt = scalarDType( true );
52+
// returns 'bool'
53+
54+
dt = scalarDType( 'hello' );
55+
// returns 'generic'
56+
```
57+
58+
</section>
59+
60+
<!-- /.usage -->
61+
62+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
63+
64+
<section class="notes">
65+
66+
## Notes
67+
68+
If the `value`
69+
70+
- is a number, returns the default real-valued floating-point data type.
71+
- is a boolean, returns the default boolean data type.
72+
- is a complex number object of a known complex data type, returns the data type.
73+
- is a complex number object of an unknown complex data type, returns the default complex-valued floating-point data type.
74+
- is any other value type, returns `'generic'`.
75+
76+
</section>
77+
78+
<!-- /.notes -->
79+
80+
<!-- Package usage examples. -->
81+
82+
<section class="examples">
83+
84+
## Examples
85+
86+
<!-- eslint no-undef: "error" -->
87+
88+
```javascript
89+
var Complex64 = require( '@stdlib/complex/float32/ctor' );
90+
var Complex128 = require( '@stdlib/complex/float64/ctor' );
91+
var scalarDType = require( '@stdlib/ndarray/base/scalar-dtype' );
92+
93+
// Number:
94+
var dt = scalarDType( 3.14 );
95+
console.log( dt );
96+
// => 'float64'
97+
98+
// Boolean:
99+
dt = scalarDType( true );
100+
console.log( dt );
101+
// => 'bool'
102+
103+
// Known complex dtype (complex64):
104+
dt = scalarDType( new Complex64( 1.0, 2.0 ) );
105+
console.log( dt );
106+
// => 'complex64'
107+
108+
// Known complex dtype (complex128):
109+
dt = scalarDType( new Complex128( 1.0, 2.0 ) );
110+
console.log( dt );
111+
// => 'complex128'
112+
113+
// Generic fallback:
114+
dt = scalarDType( 'hello' );
115+
console.log( dt );
116+
// => 'generic'
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/base/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/ctor
144+
145+
</section>
146+
147+
<!-- /.links -->
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 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 Complex64 = require( '@stdlib/complex/float32/ctor' );
25+
var pkg = require( './../package.json' ).name;
26+
var scalarDType = require( './../lib' );
27+
28+
29+
// MAIN //
30+
31+
bench( pkg, function benchmark( b ) {
32+
var values;
33+
var dt;
34+
var i;
35+
36+
values = [
37+
3.14,
38+
true,
39+
new Complex64( 1.0, 2.0 ),
40+
'hello'
41+
];
42+
43+
b.tic();
44+
for ( i = 0; i < b.iterations; i++ ) {
45+
dt = scalarDType( values[ i%values.length ] );
46+
if ( typeof dt !== 'string' ) {
47+
b.fail( 'should return a string' );
48+
}
49+
}
50+
b.toc();
51+
if ( typeof dt !== 'string' ) {
52+
b.fail( 'should return a string' );
53+
}
54+
b.pass( 'benchmark finished' );
55+
b.end();
56+
});
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
{{alias}}( value )
3+
Returns the default ndarray data type for a given scalar value.
4+
5+
Parameters
6+
----------
7+
value: scalar
8+
Input scalar.
9+
10+
Returns
11+
-------
12+
dt: string
13+
ndarray data type.
14+
15+
Examples
16+
--------
17+
> var dt = {{alias}}( 3.14 )
18+
'float64'
19+
20+
See Also
21+
--------
22+
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 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+
/**
24+
* Returns the number of ndarray dimensions.
25+
*
26+
* @param value - input scalar
27+
* @returns ndarray data type
28+
*
29+
* @example
30+
* var dt = scalarDType( 3.14 );
31+
* // returns 'float64'
32+
*
33+
* @example
34+
* var dt = scalarDType( true );
35+
* // returns 'bool'
36+
*
37+
* @example
38+
* var dt = scalarDType( 'hello' );
39+
* // returns 'generic'
40+
*/
41+
declare function scalarDType( value: any ): string;
42+
43+
44+
// EXPORTS //
45+
46+
export = scalarDType;
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 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 Complex64 = require( '@stdlib/complex/float32/ctor' );
20+
import Complex128 = require( '@stdlib/complex/float64/ctor' );
21+
import scalarDType = require( './index' );
22+
23+
24+
// TESTS //
25+
26+
// The function returns a string...
27+
{
28+
scalarDType( 3.14 ); // $ExpectType string
29+
scalarDType( true ); // $ExpectType string
30+
scalarDType( new Complex64( 1.0, 2.0 ) ); // $ExpectType string
31+
scalarDType( new Complex128( 1.0, 2.0 ) ); // $ExpectType string
32+
scalarDType( 'hello' ); // $ExpectType string
33+
}
34+
35+
36+
// The compiler throws an error if the function is provided an unsupported number of arguments...
37+
{
38+
scalarDType(); // $ExpectError
39+
scalarDType( 3.14, {} ); // $ExpectError
40+
}

0 commit comments

Comments
 (0)