Skip to content

Commit 68cf561

Browse files
Kaushikgtmclaude
andcommitted
feat: add ndarray/base/scalar-dtype
Introduce a reusable package that resolves a default ndarray data type from a scalar value, and update `ndarray/base/atleastnd` and `ndarray/from-scalar` to consume it, removing duplicated logic. Ref: #11002 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 903b21e commit 68cf561

12 files changed

Lines changed: 563 additions & 37 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,7 @@
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' );
24+
var scalarDataType = require( '@stdlib/ndarray/base/scalar-dtype' );
2825
var broadcastScalar = require( '@stdlib/ndarray/base/broadcast-scalar' );
2926
var dims = require( '@stdlib/ndarray/base/ndims' );
3027
var defaults = require( '@stdlib/ndarray/defaults' );
@@ -40,9 +37,6 @@ var ones = require( '@stdlib/array/base/ones' );
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 = scalarDataType( v );
123106
out.push( broadcastScalar( v, dt, ones( ndims ), ORDER ) );
124107
}
125108
return out;
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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+
# scalarDataType
22+
23+
> Resolve a default [ndarray][@stdlib/ndarray/ctor] data type from a scalar value.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var scalarDataType = require( '@stdlib/ndarray/base/scalar-dtype' );
31+
```
32+
33+
#### scalarDataType( value )
34+
35+
Resolves a default [ndarray][@stdlib/ndarray/ctor] data type from a scalar value.
36+
37+
```javascript
38+
var dt = scalarDataType( 3.14 );
39+
// returns <string>
40+
41+
dt = scalarDataType( 'beep' );
42+
// returns 'generic'
43+
```
44+
45+
The function applies the following resolution rules:
46+
47+
- If `value` is a number, the function returns the default real-valued floating-point data type.
48+
- If `value` is a boolean, the function returns the default boolean data type.
49+
- If `value` is a complex number object of a known complex data type, the function returns the corresponding complex data type.
50+
- If `value` is a complex number object of an unknown complex data type, the function returns the default complex-valued floating-point data type.
51+
- For any other value, the function returns `'generic'`.
52+
53+
</section>
54+
55+
<section class="examples">
56+
57+
## Examples
58+
59+
<!-- eslint no-undef: "error" -->
60+
61+
```javascript
62+
var Complex128 = require( '@stdlib/complex/float64/ctor' );
63+
var scalarDataType = require( '@stdlib/ndarray/base/scalar-dtype' );
64+
65+
console.log( scalarDataType( 3.14 ) );
66+
console.log( scalarDataType( true ) );
67+
console.log( scalarDataType( new Complex128( 1.0, 2.0 ) ) );
68+
console.log( scalarDataType( 'beep' ) );
69+
```
70+
71+
</section>
72+
73+
<section class="links">
74+
75+
[@stdlib/ndarray/ctor]: https://github.com/stdlib-js/stdlib
76+
77+
</section>
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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 isString = require( '@stdlib/assert/is-string' ).isPrimitive;
25+
var Complex128 = require( '@stdlib/complex/float64/ctor' );
26+
var pkg = require( './../package.json' ).name;
27+
var scalarDataType = require( './../lib' );
28+
29+
30+
// MAIN //
31+
32+
bench( pkg, function benchmark( b ) {
33+
var values;
34+
var dt;
35+
var i;
36+
37+
values = [
38+
3.14,
39+
true,
40+
new Complex128( 1.0, 2.0 ),
41+
'beep'
42+
];
43+
44+
b.tic();
45+
for ( i = 0; i < b.iterations; i++ ) {
46+
dt = scalarDataType( values[ i % values.length ] );
47+
if ( typeof dt !== 'string' ) {
48+
b.fail( 'should return a string' );
49+
}
50+
}
51+
b.toc();
52+
if ( !isString( dt ) ) {
53+
b.fail( 'should return a string' );
54+
}
55+
b.pass( 'benchmark finished' );
56+
b.end();
57+
});
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
2+
{{alias}}( value )
3+
Resolves a default ndarray data type from a scalar value.
4+
5+
If `value` is a number, the function returns the default real-valued
6+
floating-point data type.
7+
8+
If `value` is a boolean, the function returns the default boolean data
9+
type.
10+
11+
If `value` is a complex number object of a known complex data type, the
12+
function returns the corresponding complex data type.
13+
14+
If `value` is a complex number object of an unknown complex data type, the
15+
function returns the default complex-valued floating-point data type.
16+
17+
For any other value, the function returns 'generic'.
18+
19+
Parameters
20+
----------
21+
value: any
22+
Scalar value.
23+
24+
Returns
25+
-------
26+
out: string
27+
Data type.
28+
29+
Examples
30+
--------
31+
> var dt = {{alias}}( 3.14 )
32+
<string>
33+
> dt = {{alias}}( 'beep' )
34+
'generic'
35+
36+
See Also
37+
--------
38+
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+
// TypeScript Version: 4.1
20+
21+
/**
22+
* Resolves a default ndarray data type from a scalar value.
23+
*
24+
* @param value - scalar value
25+
* @returns data type
26+
*
27+
* @example
28+
* var dt = scalarDataType( 3.14 );
29+
* // returns <string>
30+
*
31+
* @example
32+
* var dt = scalarDataType( 'beep' );
33+
* // returns 'generic'
34+
*/
35+
declare function scalarDataType( value: any ): string;
36+
37+
38+
// EXPORTS //
39+
40+
export = scalarDataType;
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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 scalarDataType = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns a string...
25+
{
26+
scalarDataType( 3.14 ); // $ExpectType string
27+
scalarDataType( 'beep' ); // $ExpectType string
28+
}
29+
30+
// The compiler throws an error if the function is provided an unsupported number of arguments...
31+
{
32+
scalarDataType(); // $ExpectError
33+
scalarDataType( 3.14, 3 ); // $ExpectError
34+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
var Complex128 = require( '@stdlib/complex/float64/ctor' );
22+
var scalarDataType = require( './../lib' );
23+
24+
console.log( scalarDataType( 3.14 ) );
25+
console.log( scalarDataType( true ) );
26+
console.log( scalarDataType( new Complex128( 1.0, 2.0 ) ) );
27+
console.log( scalarDataType( 'beep' ) );
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) 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+
/**
22+
* Resolve a default ndarray data type from a scalar value.
23+
*
24+
* @module @stdlib/ndarray/base/scalar-dtype
25+
*
26+
* @example
27+
* var scalarDataType = require( '@stdlib/ndarray/base/scalar-dtype' );
28+
*
29+
* var dt = scalarDataType( 3.14 );
30+
* // returns <string>
31+
*
32+
* dt = scalarDataType( 'beep' );
33+
* // returns 'generic'
34+
*/
35+
36+
// MODULES //
37+
38+
var main = require( './main.js' );
39+
40+
41+
// EXPORTS //
42+
43+
module.exports = main;

0 commit comments

Comments
 (0)