diff --git a/lib/node_modules/@stdlib/ndarray/base/scalar-dtype/README.md b/lib/node_modules/@stdlib/ndarray/base/scalar-dtype/README.md new file mode 100644 index 000000000000..caea5e390bfe --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/scalar-dtype/README.md @@ -0,0 +1,138 @@ + + +# Scalar Data Type + +> Determine the default ndarray [data type][@stdlib/ndarray/dtypes] for a provided scalar value. + + + +
+ +
+ + + + + +
+ +## Usage + +```javascript +var scalarDataType = require( '@stdlib/ndarray/base/scalar-dtype' ); +``` + +#### scalarDataType( value ) + +Returns the default ndarray [data type][@stdlib/ndarray/dtypes] for a provided scalar value. + +```javascript +var dt = scalarDataType( 3.0 ); +// returns 'float64' + +dt = scalarDataType( true ); +// returns 'bool' + +dt = scalarDataType( null ); +// returns 'generic' +``` + +
+ + + + + +
+ +## Notes + +- The function resolves data types as follows: + + - **number**: the default real-valued floating-point data type. + - **boolean**: the default boolean data type. + - **complex number**: a corresponding complex number data type, falling back to the default complex-valued floating-point data type when a provided complex-like object has an unrecognized data type. + - **everything else**: `'generic'`. + +- Only primitive numbers and booleans are recognized. A `Number` or `Boolean` object resolves to `'generic'`. + +
+ + + + + +
+ +## Examples + + + +```javascript +var Complex128 = require( '@stdlib/complex/float64/ctor' ); +var scalarDataType = require( '@stdlib/ndarray/base/scalar-dtype' ); + +var values = [ + 3.14, + -1.0, + true, + false, + new Complex128( 1.0, 2.0 ), + 'beep', + null, + [ 1, 2, 3 ] +]; + +var i; +for ( i = 0; i < values.length; i++ ) { + console.log( '%s => %s', String( values[ i ] ), scalarDataType( values[ i ] ) ); +} +``` + +
+ + + + + +
+ +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/ndarray/base/scalar-dtype/benchmark/benchmark.js b/lib/node_modules/@stdlib/ndarray/base/scalar-dtype/benchmark/benchmark.js new file mode 100644 index 000000000000..daedfc1a53b0 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/scalar-dtype/benchmark/benchmark.js @@ -0,0 +1,61 @@ +/** +* @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 isString = require( '@stdlib/assert/is-string' ).isPrimitive; +var Complex128 = require( '@stdlib/complex/float64/ctor' ); +var pkg = require( './../package.json' ).name; +var scalarDataType = require( './../lib' ); + + +// MAIN // + +bench( pkg, function benchmark( b ) { + var values; + var out; + var i; + + values = [ + 3.14, + -1.0, + true, + false, + new Complex128( 1.0, 2.0 ), + 'beep', + null, + [ 1, 2, 3 ] + ]; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + out = scalarDataType( values[ i%values.length ] ); + if ( typeof out !== 'string' ) { + b.fail( 'should return a string' ); + } + } + b.toc(); + if ( !isString( out ) ) { + b.fail( 'should return a string' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/lib/node_modules/@stdlib/ndarray/base/scalar-dtype/docs/repl.txt b/lib/node_modules/@stdlib/ndarray/base/scalar-dtype/docs/repl.txt new file mode 100644 index 000000000000..96897942de74 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/scalar-dtype/docs/repl.txt @@ -0,0 +1,32 @@ + +{{alias}}( value ) + Returns the default ndarray data type for a provided scalar value. + + If provided a number, the function returns the default real-valued + floating-point data type. If provided a boolean, the function returns the + default boolean data type. If provided a complex number, the function + returns a corresponding complex number data type. For all other values, + the function returns 'generic'. + + Parameters + ---------- + value: any + Scalar value. + + Returns + ------- + dt: string + ndarray data type. + + Examples + -------- + > var dt = {{alias}}( 3.0 ) + 'float64' + > dt = {{alias}}( true ) + 'bool' + > dt = {{alias}}( null ) + 'generic' + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/ndarray/base/scalar-dtype/docs/types/index.d.ts b/lib/node_modules/@stdlib/ndarray/base/scalar-dtype/docs/types/index.d.ts new file mode 100644 index 000000000000..fac7a19223d4 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/scalar-dtype/docs/types/index.d.ts @@ -0,0 +1,44 @@ +/** +* @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 + +/// + +import { DataType } from '@stdlib/types/ndarray'; + +/** +* Returns the default ndarray data type for a provided scalar value. +* +* @param value - scalar value +* @returns ndarray data type +* +* @example +* var dt = scalarDataType( 3.0 ); +* // returns 'float64' +* +* @example +* var dt = scalarDataType( true ); +* // returns 'bool' +*/ +declare function scalarDataType( value: any ): DataType; + + +// EXPORTS // + +export = scalarDataType; diff --git a/lib/node_modules/@stdlib/ndarray/base/scalar-dtype/docs/types/test.ts b/lib/node_modules/@stdlib/ndarray/base/scalar-dtype/docs/types/test.ts new file mode 100644 index 000000000000..d92342bce156 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/scalar-dtype/docs/types/test.ts @@ -0,0 +1,35 @@ +/** +* @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 scalarDataType = require( './index' ); + + +// TESTS // + +// The function returns a string... +{ + scalarDataType( 3.0 ); // $ExpectType DataType + scalarDataType( true ); // $ExpectType DataType + scalarDataType( null ); // $ExpectType DataType +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + scalarDataType(); // $ExpectError + scalarDataType( 3.0, 2.0 ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/ndarray/base/scalar-dtype/examples/index.js b/lib/node_modules/@stdlib/ndarray/base/scalar-dtype/examples/index.js new file mode 100644 index 000000000000..8490fd13b505 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/scalar-dtype/examples/index.js @@ -0,0 +1,38 @@ +/** +* @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'; + +var Complex128 = require( '@stdlib/complex/float64/ctor' ); +var scalarDataType = require( './../lib' ); + +var values = [ + 3.14, + -1.0, + true, + false, + new Complex128( 1.0, 2.0 ), + 'beep', + null, + [ 1, 2, 3 ] +]; + +var i; +for ( i = 0; i < values.length; i++ ) { + console.log( '%s => %s', String( values[ i ] ), scalarDataType( values[ i ] ) ); +} diff --git a/lib/node_modules/@stdlib/ndarray/base/scalar-dtype/lib/index.js b/lib/node_modules/@stdlib/ndarray/base/scalar-dtype/lib/index.js new file mode 100644 index 000000000000..b8836c736784 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/scalar-dtype/lib/index.js @@ -0,0 +1,43 @@ +/** +* @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'; + +/** +* Determine the default ndarray data type for a provided scalar value. +* +* @module @stdlib/ndarray/base/scalar-dtype +* +* @example +* var scalarDataType = require( '@stdlib/ndarray/base/scalar-dtype' ); +* +* var dt = scalarDataType( 3.0 ); +* // returns 'float64' +* +* dt = scalarDataType( true ); +* // returns 'bool' +*/ + +// MODULES // + +var scalarDataType = require( './main.js' ); + + +// EXPORTS // + +module.exports = scalarDataType; diff --git a/lib/node_modules/@stdlib/ndarray/base/scalar-dtype/lib/main.js b/lib/node_modules/@stdlib/ndarray/base/scalar-dtype/lib/main.js new file mode 100644 index 000000000000..4a33865b2abb --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/scalar-dtype/lib/main.js @@ -0,0 +1,78 @@ +/** +* @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 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 defaults = require( '@stdlib/ndarray/defaults' ); + + +// 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' ); + + +// MAIN // + +/** +* Returns the default ndarray data type for a provided scalar value. +* +* @param {*} value - scalar value +* @returns {string} ndarray data type +* +* @example +* var dt = scalarDataType( 3.0 ); +* // returns 'float64' +* +* @example +* var dt = scalarDataType( true ); +* // returns 'bool' +* +* @example +* var dt = scalarDataType( null ); +* // returns 'generic' +*/ +function scalarDataType( value ) { + var dt; + if ( isNumber( value ) ) { + return DEFAULT_REAL; + } + if ( isBoolean( value ) ) { + return DEFAULT_BOOL; + } + if ( isComplexLike( value ) ) { + dt = complexDataType( value ); + if ( dt === null ) { + return DEFAULT_CMPLX; + } + return dt; + } + return 'generic'; +} + + +// EXPORTS // + +module.exports = scalarDataType; diff --git a/lib/node_modules/@stdlib/ndarray/base/scalar-dtype/package.json b/lib/node_modules/@stdlib/ndarray/base/scalar-dtype/package.json new file mode 100644 index 000000000000..0b457d138ebb --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/scalar-dtype/package.json @@ -0,0 +1,69 @@ +{ + "name": "@stdlib/ndarray/base/scalar-dtype", + "version": "0.0.0", + "description": "Determine the default ndarray data type for a provided scalar value.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdtypes", + "types", + "ndarray", + "base", + "scalar", + "value", + "dtype", + "data", + "type", + "default", + "utilities", + "utility", + "utils", + "util" + ], + "__stdlib__": {} +} diff --git a/lib/node_modules/@stdlib/ndarray/base/scalar-dtype/test/test.js b/lib/node_modules/@stdlib/ndarray/base/scalar-dtype/test/test.js new file mode 100644 index 000000000000..f26c3af393bb --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/scalar-dtype/test/test.js @@ -0,0 +1,104 @@ +/** +* @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 tape = require( 'tape' ); +var Complex64 = require( '@stdlib/complex/float32/ctor' ); +var Complex128 = require( '@stdlib/complex/float64/ctor' ); +var defaults = require( '@stdlib/ndarray/defaults' ); +var scalarDataType = require( './../lib' ); + + +// 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' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof scalarDataType, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function returns the default real-valued floating-point data type for a number', function test( t ) { + var values; + var i; + + values = [ + 0.0, + 3.0, + -3.0, + 3.14, + NaN, + 1.0/0.0, + -1.0/0.0 + ]; + for ( i = 0; i < values.length; i++ ) { + t.strictEqual( scalarDataType( values[ i ] ), DEFAULT_REAL, 'returns expected value when provided '+values[ i ] ); + } + t.end(); +}); + +tape( 'the function returns the default boolean data type for a boolean', function test( t ) { + t.strictEqual( scalarDataType( true ), DEFAULT_BOOL, 'returns expected value when provided true' ); + t.strictEqual( scalarDataType( false ), DEFAULT_BOOL, 'returns expected value when provided false' ); + t.end(); +}); + +tape( 'the function returns a corresponding complex number data type for a complex number', function test( t ) { + t.strictEqual( scalarDataType( new Complex128( 1.0, 2.0 ) ), 'complex128', 'returns expected value when provided a Complex128' ); + t.strictEqual( scalarDataType( new Complex64( 1.0, 2.0 ) ), 'complex64', 'returns expected value when provided a Complex64' ); + t.end(); +}); + +tape( 'the function returns the default complex-valued floating-point data type for a complex-like object having an unrecognized data type', function test( t ) { + var v = { + 're': 1.0, + 'im': 2.0 + }; + t.strictEqual( scalarDataType( v ), DEFAULT_CMPLX, 'returns expected value' ); + t.end(); +}); + +tape( 'the function returns "generic" for all other values', function test( t ) { + var values; + var i; + + values = [ + '5', + 'beep', + null, + void 0, + [], + {}, + function noop() {}, + new Number( 3.0 ), // eslint-disable-line no-new-wrappers + new Boolean( true ) // eslint-disable-line no-new-wrappers + ]; + for ( i = 0; i < values.length; i++ ) { + t.strictEqual( scalarDataType( values[ i ] ), 'generic', 'returns expected value when provided '+String( values[ i ] ) ); + } + t.end(); +});