Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 2 additions & 19 deletions lib/node_modules/@stdlib/ndarray/base/atleastnd/lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@
// MODULES //

var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' );
var isNumber = require( '@stdlib/assert/is-number' ).isPrimitive;
var isComplexLike = require( '@stdlib/assert/is-complex-like' );
var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive;
var complexDataType = require( '@stdlib/complex/dtype' );
var broadcastScalar = require( '@stdlib/ndarray/base/broadcast-scalar' );
var dims = require( '@stdlib/ndarray/base/ndims' );
var defaults = require( '@stdlib/ndarray/defaults' );
Expand All @@ -34,15 +30,13 @@ var getStrides = require( '@stdlib/ndarray/base/strides' );
var getOffset = require( '@stdlib/ndarray/base/offset' );
var getOrder = require( '@stdlib/ndarray/base/order' );
var getDType = require( '@stdlib/ndarray/base/dtype' );
var scalarDType = require( '@stdlib/ndarray/base/scalar-dtype' );
var getData = require( '@stdlib/ndarray/base/data-buffer' );
var ones = require( '@stdlib/array/base/ones' );


// VARIABLES //

var DEFAULT_REAL = defaults.get( 'dtypes.real_floating_point' );
var DEFAULT_CMPLX = defaults.get( 'dtypes.complex_floating_point' );
var DEFAULT_BOOL = defaults.get( 'dtypes.boolean' );
var ORDER = defaults.get( 'order' );


Expand Down Expand Up @@ -108,18 +102,7 @@ function atleastnd( ndims, arrays ) {
continue;
}
// For scalar values, resolve a corresponding ndarray data type...
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
dt = DEFAULT_REAL;
} else if ( isBoolean( v ) ) {
dt = DEFAULT_BOOL;
} else if ( isComplexLike( v ) ) {
dt = complexDataType( v );
if ( dt === null ) {
dt = DEFAULT_CMPLX;
}
} else {
dt = 'generic';
}
dt = scalarDType(v);
out.push( broadcastScalar( v, dt, ones( ndims ), ORDER ) );
}
return out;
Expand Down
9 changes: 9 additions & 0 deletions lib/node_modules/@stdlib/ndarray/base/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1084,6 +1084,15 @@ setReadOnly( ns, 'reverse', require( '@stdlib/ndarray/base/reverse' ) );
*/
setReadOnly( ns, 'reverseDimension', require( '@stdlib/ndarray/base/reverse-dimension' ) );

/**
* @name scalarDType
* @memberof ns
* @readonly
* @type {Function}
* @see {@link module:@stdlib/ndarray/base/scalar-dtype}
*/
setReadOnly( ns, 'scalarDType', require( '@stdlib/ndarray/base/scalar-dtype' ) );

/**
* @name serializeMetaData
* @memberof ns
Expand Down
147 changes: 147 additions & 0 deletions lib/node_modules/@stdlib/ndarray/base/scalar-dtype/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
<!--

@license Apache-2.0

Copyright (c) 2026 The Stdlib Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

-->

# scalar-dtype

> Return the default [ndarray][@stdlib/ndarray/base/ctor] data type for a given scalar value.

<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->

<section class="intro">

</section>

<!-- /.intro -->

<!-- Package usage documentation. -->

<section class="usage">

## Usage

```javascript
var scalarDType = require( '@stdlib/ndarray/base/scalar-dtype' );
```

#### scalarDType( value )

Returns the default [ndarray][@stdlib/ndarray/base/ctor] data type for a given scalar value.

```javascript
var dt = scalarDType( 3.14 );
// returns 'float64'

dt = scalarDType( true );
// returns 'bool'

dt = scalarDType( 'hello' );
// returns 'generic'
```

</section>

<!-- /.usage -->

<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="notes">

## Notes

If the `value`

- is a number, returns the default real-valued floating-point data type.
- is a boolean, returns the default boolean data type.
- is a complex number object of a known complex data type, returns the data type.
- is a complex number object of an unknown complex data type, returns the default complex-valued floating-point data type.
- is any other value type, returns `'generic'`.

</section>

<!-- /.notes -->

<!-- Package usage examples. -->

<section class="examples">

## Examples

<!-- eslint no-undef: "error" -->

```javascript
var Complex64 = require( '@stdlib/complex/float32/ctor' );
var Complex128 = require( '@stdlib/complex/float64/ctor' );
var scalarDType = require( '@stdlib/ndarray/base/scalar-dtype' );

// Number:
var dt = scalarDType( 3.14 );
console.log( dt );
// => 'float64'

// Boolean:
dt = scalarDType( true );
console.log( dt );
// => 'bool'

// Known complex dtype (complex64):
dt = scalarDType( new Complex64( 1.0, 2.0 ) );
console.log( dt );
// => 'complex64'

// Known complex dtype (complex128):
dt = scalarDType( new Complex128( 1.0, 2.0 ) );
console.log( dt );
// => 'complex128'

// Generic fallback:
dt = scalarDType( 'hello' );
console.log( dt );
// => 'generic'
```

</section>

<!-- /.examples -->

<!-- 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. -->

<section class="references">

</section>

<!-- /.references -->

<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->

<section class="related">

</section>

<!-- /.related -->

<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="links">

[@stdlib/ndarray/base/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/ctor

</section>

<!-- /.links -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2026 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

// MODULES //

var bench = require( '@stdlib/bench' );
var Complex64 = require( '@stdlib/complex/float32/ctor' );
var pkg = require( './../package.json' ).name;
var scalarDType = require( './../lib' );


// MAIN //

bench( pkg, function benchmark( b ) {
var values;
var dt;
var i;

values = [
3.14,
true,
new Complex64( 1.0, 2.0 ),
'hello'
];

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
dt = scalarDType( values[ i%values.length ] );
if ( typeof dt !== 'string' ) {
b.fail( 'should return a string' );
}
}
b.toc();
if ( typeof dt !== 'string' ) {
b.fail( 'should return a string' );
}
b.pass( 'benchmark finished' );
b.end();
});
22 changes: 22 additions & 0 deletions lib/node_modules/@stdlib/ndarray/base/scalar-dtype/docs/repl.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

{{alias}}( value )
Returns the default ndarray data type for a given scalar value.

Parameters
----------
value: scalar
Input scalar.

Returns
-------
dt: string
ndarray data type.

Examples
--------
> var dt = {{alias}}( 3.14 )
'float64'

See Also
--------

Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* @license Apache-2.0
*
* Copyright (c) 2026 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

// TypeScript Version: 4.1

/// <reference types="@stdlib/types"/>

/**
* Returns the number of ndarray dimensions.
*
* @param value - input scalar
* @returns ndarray data type
*
* @example
* var dt = scalarDType( 3.14 );
* // returns 'float64'
*
* @example
* var dt = scalarDType( true );
* // returns 'bool'
*
* @example
* var dt = scalarDType( 'hello' );
* // returns 'generic'
*/
declare function scalarDType( value: any ): string;

Check warning on line 41 in lib/node_modules/@stdlib/ndarray/base/scalar-dtype/docs/types/index.d.ts

View workflow job for this annotation

GitHub Actions / Lint Changed Files

Unexpected any. Specify a different type


// EXPORTS //

export = scalarDType;
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* @license Apache-2.0
*
* Copyright (c) 2026 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import Complex64 = require( '@stdlib/complex/float32/ctor' );
import Complex128 = require( '@stdlib/complex/float64/ctor' );
import scalarDType = require( './index' );


// TESTS //

// The function returns a string...
{
scalarDType( 3.14 ); // $ExpectType string
scalarDType( true ); // $ExpectType string
scalarDType( new Complex64( 1.0, 2.0 ) ); // $ExpectType string
scalarDType( new Complex128( 1.0, 2.0 ) ); // $ExpectType string
scalarDType( 'hello' ); // $ExpectType string
}


// The compiler throws an error if the function is provided an unsupported number of arguments...
{
scalarDType(); // $ExpectError
scalarDType( 3.14, {} ); // $ExpectError
}
Loading
Loading