diff --git a/lib/node_modules/@stdlib/ndarray/base/binary-input-casting-dtype/README.md b/lib/node_modules/@stdlib/ndarray/base/binary-input-casting-dtype/README.md new file mode 100644 index 000000000000..dc89a52d5b8e --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/binary-input-casting-dtype/README.md @@ -0,0 +1,150 @@ + + +# binaryInputCastingDataType + +> Resolve the input ndarray casting [data type][@stdlib/ndarray/dtypes] for a binary function. + + + +
+ +
+ + + + + +
+ +## Usage + +```javascript +var binaryInputCastingDataType = require( '@stdlib/ndarray/base/binary-input-casting-dtype' ); +``` + +#### binaryInputCastingDataType( idtype1, idtype2, odtype, policy ) + +Resolves the input ndarray casting [data type][@stdlib/ndarray/dtypes] for a binary function according to a [data type policy][@stdlib/ndarray/input-casting-policies]. + +```javascript +var dt = binaryInputCastingDataType( 'int32', 'int32', 'float64', 'promoted' ); +// returns 'float64' +``` + +The function supports the following parameters: + +- **idtype1**: first input ndarray data type. +- **idtype2**: second input ndarray data type. +- **odtype**: output ndarray data type. +- **policy**: input ndarray [casting policy][@stdlib/ndarray/input-casting-policies]. + +If `policy` is a [data type][@stdlib/ndarray/dtypes], the function always returns the `policy` value (i.e., the fourth argument). + +```javascript +var dt = binaryInputCastingDataType( 'float32', 'float32', 'float64', 'complex128' ); +// returns 'complex128' + +dt = binaryInputCastingDataType( 'int32', 'float64', 'float64', 'complex128' ); +// returns 'complex128' + +// ... +``` + +
+ + + + + +
+ +
+ + + + + +
+ +## Examples + + + +```javascript +var naryFunction = require( '@stdlib/utils/nary-function' ); +var unzip = require( '@stdlib/utils/unzip' ); +var nCartesianProduct = require( '@stdlib/array/base/n-cartesian-product' ); +var dtypes = require( '@stdlib/ndarray/dtypes' ); +var logEachMap = require( '@stdlib/console/log-each-map' ); +var inputCastingDataType = require( '@stdlib/ndarray/base/binary-input-casting-dtype' ); + +// Get the list of real-valued data types: +var dt = dtypes( 'real' ); + +// Define a list of casting policies: +var policies = [ + 'none', + 'promoted', + 'accumulation', + 'output' +]; + +// Generate dtype-policy argument groups: +var args = nCartesianProduct( dt, dt, policies ); + +// Unzip the argument arrays: +args = unzip( args ); + +// Resolve casting data types: +logEachMap( 'dtypes: (%10s, %10s). policy: %20s. result: %10s.', args[ 0 ], args[ 1 ], args[ 2 ], naryFunction( inputCastingDataType, 3 ) ); +``` + +
+ + + + + +
+ +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/ndarray/base/binary-input-casting-dtype/benchmark/benchmark.js b/lib/node_modules/@stdlib/ndarray/base/binary-input-casting-dtype/benchmark/benchmark.js new file mode 100644 index 000000000000..ba0333dd08de --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/binary-input-casting-dtype/benchmark/benchmark.js @@ -0,0 +1,160 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 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 isDataType = require( '@stdlib/ndarray/base/assert/is-data-type' ); +var dtypes = require( '@stdlib/ndarray/dtypes' ); +var pkg = require( './../package.json' ).name; +var resolve = require( './../lib' ); + + +// VARIABLES // + +var DTYPES = dtypes( 'numeric' ); + + +// MAIN // + +bench( pkg+':policy=none', function benchmark( b ) { + var out; + var dt1; + var dt2; + var dt3; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + dt1 = DTYPES[ i%DTYPES.length ]; + dt2 = DTYPES[ i%DTYPES.length ]; + dt3 = DTYPES[ (i+1)%DTYPES.length ]; + out = resolve( dt1, dt2, dt3, 'none' ); + if ( typeof out !== 'string' ) { + b.fail( 'should return a string' ); + } + } + b.toc(); + if ( !isDataType( out ) ) { + b.fail( 'should return a data type' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+':policy=promoted', function benchmark( b ) { + var out; + var dt1; + var dt2; + var dt3; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + dt1 = DTYPES[ i%DTYPES.length ]; + dt2 = DTYPES[ i%DTYPES.length ]; + dt3 = DTYPES[ (i+1)%DTYPES.length ]; + out = resolve( dt1, dt2, dt3, 'promoted' ); + if ( typeof out !== 'string' ) { + b.fail( 'should return a string' ); + } + } + b.toc(); + if ( !isDataType( out ) ) { + b.fail( 'should return a data type' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+':policy=accumulation', function benchmark( b ) { + var out; + var dt1; + var dt2; + var dt3; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + dt1 = DTYPES[ i%DTYPES.length ]; + dt2 = DTYPES[ i%DTYPES.length ]; + dt3 = DTYPES[ (i+1)%DTYPES.length ]; + out = resolve( dt1, dt2, dt3, 'accumulation' ); + if ( typeof out !== 'string' ) { + b.fail( 'should return a string' ); + } + } + b.toc(); + if ( !isDataType( out ) ) { + b.fail( 'should return a data type' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+':policy=output', function benchmark( b ) { + var out; + var dt1; + var dt2; + var dt3; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + dt1 = DTYPES[ i%DTYPES.length ]; + dt2 = DTYPES[ i%DTYPES.length ]; + dt3 = DTYPES[ (i+1)%DTYPES.length ]; + out = resolve( dt1, dt2, dt3, 'output' ); + if ( typeof out !== 'string' ) { + b.fail( 'should return a string' ); + } + } + b.toc(); + if ( !isDataType( out ) ) { + b.fail( 'should return a data type' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+':policy=', function benchmark( b ) { + var out; + var dt1; + var dt2; + var dt3; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + dt1 = DTYPES[ i%DTYPES.length ]; + dt2 = DTYPES[ i%DTYPES.length ]; + dt3 = DTYPES[ (i+1)%DTYPES.length ]; + out = resolve( dt1, dt2, dt3, 'int32' ); + if ( typeof out !== 'string' ) { + b.fail( 'should return a string' ); + } + } + b.toc(); + if ( !isDataType( out ) ) { + b.fail( 'should return a data type' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/lib/node_modules/@stdlib/ndarray/base/binary-input-casting-dtype/docs/repl.txt b/lib/node_modules/@stdlib/ndarray/base/binary-input-casting-dtype/docs/repl.txt new file mode 100644 index 000000000000..9134f7ab17d7 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/binary-input-casting-dtype/docs/repl.txt @@ -0,0 +1,32 @@ + +{{alias}}( idtype, odtype, policy ) + Resolves the input ndarray casting data type for a binary function. + + Parameters + ---------- + idtype1: string + First input ndarray data type. + + idtype2: string + Second input ndarray data type. + + odtype: string + Output ndarray data type. + + policy: string + Input ndarray casting data type policy. If `policy` is a data type, the + function returns the `policy` value. + + Returns + ------- + out: string + Input ndarray casting data type. + + Examples + -------- + > var out = {{alias}}( 'float64', 'float64', 'float64', 'none' ) + 'float64' + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/ndarray/base/binary-input-casting-dtype/docs/types/index.d.ts b/lib/node_modules/@stdlib/ndarray/base/binary-input-casting-dtype/docs/types/index.d.ts new file mode 100644 index 000000000000..b95837b02b3b --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/binary-input-casting-dtype/docs/types/index.d.ts @@ -0,0 +1,43 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2025 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, InputCastingPolicy } from '@stdlib/types/ndarray'; + +/** +* Resolves the input ndarray casting data type for a binary function. +* +* @param idtype1 - first input ndarray data type +* @param idtype2 - second input ndarray data type +* @param odtype - output ndarray data type +* @param policy - input ndarray casting data type policy +* @returns input ndarray casting data type +* +* @example +* var dt = outputDataType( 'float64', 'float64', 'float64', 'none' ); +* // returns +*/ +declare function outputDataType( idtype1: DataType, idtype2: DataType, odtype: DataType, policy: InputCastingPolicy | DataType ): DataType; + + +// EXPORTS // + +export = outputDataType; diff --git a/lib/node_modules/@stdlib/ndarray/base/binary-input-casting-dtype/docs/types/test.ts b/lib/node_modules/@stdlib/ndarray/base/binary-input-casting-dtype/docs/types/test.ts new file mode 100644 index 000000000000..29c87a8cf5ff --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/binary-input-casting-dtype/docs/types/test.ts @@ -0,0 +1,84 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2025 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 inputCastingDataType = require( './index' ); + + +// TESTS // + +// The function returns a data type... +{ + inputCastingDataType( 'float64', 'float64', 'float64', 'none' ); // $ExpectType DataType +} + +// The compiler throws an error if not provided a first argument which is a data type... +{ + inputCastingDataType( '10', 'float64', 'float64', 'none' ); // $ExpectError + inputCastingDataType( true, 'float64', 'float64', 'none' ); // $ExpectError + inputCastingDataType( false, 'float64', 'float64', 'none' ); // $ExpectError + inputCastingDataType( null, 'float64', 'float64', 'none' ); // $ExpectError + inputCastingDataType( undefined, 'float64', 'float64', 'none' ); // $ExpectError + inputCastingDataType( [], 'float64', 'float64', 'none' ); // $ExpectError + inputCastingDataType( {}, 'float64', 'float64', 'none' ); // $ExpectError + inputCastingDataType( ( x: number ): number => x, 'float64', 'float64', 'none' ); // $ExpectError +} + +// The compiler throws an error if not provided a second argument which is a data type... +{ + inputCastingDataType( 'float64', '10', 'float64', 'none' ); // $ExpectError + inputCastingDataType( 'float64', true, 'float64', 'none' ); // $ExpectError + inputCastingDataType( 'float64', false, 'float64', 'none' ); // $ExpectError + inputCastingDataType( 'float64', null, 'float64', 'none' ); // $ExpectError + inputCastingDataType( 'float64', undefined, 'float64', 'none' ); // $ExpectError + inputCastingDataType( 'float64', [], 'float64', 'none' ); // $ExpectError + inputCastingDataType( 'float64', {}, 'float64', 'none' ); // $ExpectError + inputCastingDataType( 'float64', ( x: number ): number => x, 'float64', 'none' ); // $ExpectError +} + +// The compiler throws an error if not provided a third argument which is a data type... +{ + inputCastingDataType( 'float64', 'float64', '10', 'none' ); // $ExpectError + inputCastingDataType( 'float64', 'float64', true, 'none' ); // $ExpectError + inputCastingDataType( 'float64', 'float64', false, 'none' ); // $ExpectError + inputCastingDataType( 'float64', 'float64', null, 'none' ); // $ExpectError + inputCastingDataType( 'float64', 'float64', undefined, 'none' ); // $ExpectError + inputCastingDataType( 'float64', 'float64', [], 'none' ); // $ExpectError + inputCastingDataType( 'float64', 'float64', {}, 'none' ); // $ExpectError + inputCastingDataType( 'float64', 'float64', ( x: number ): number => x, 'none' ); // $ExpectError +} + +// The compiler throws an error if not provided a fourth argument which is either a data type or data type policy... +{ + inputCastingDataType( 'float64', 'float64', 'float64', '10' ); // $ExpectError + inputCastingDataType( 'float64', 'float64', 'float64', true ); // $ExpectError + inputCastingDataType( 'float64', 'float64', 'float64', false ); // $ExpectError + inputCastingDataType( 'float64', 'float64', 'float64', null ); // $ExpectError + inputCastingDataType( 'float64', 'float64', 'float64', undefined ); // $ExpectError + inputCastingDataType( 'float64', 'float64', 'float64', [] ); // $ExpectError + inputCastingDataType( 'float64', 'float64', 'float64', {} ); // $ExpectError + inputCastingDataType( 'float64', 'float64', 'float64', ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + inputCastingDataType(); // $ExpectError + inputCastingDataType( 'float64' ); // $ExpectError + inputCastingDataType( 'float64', 'float64' ); // $ExpectError + inputCastingDataType( 'float64', 'float64', 'float64' ); // $ExpectError + inputCastingDataType( 'float64', 'float64', 'float64', 'none', 'foo' ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/ndarray/base/binary-input-casting-dtype/examples/index.js b/lib/node_modules/@stdlib/ndarray/base/binary-input-casting-dtype/examples/index.js new file mode 100644 index 000000000000..9ec427341fa9 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/binary-input-casting-dtype/examples/index.js @@ -0,0 +1,46 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 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 naryFunction = require( '@stdlib/utils/nary-function' ); +var unzip = require( '@stdlib/utils/unzip' ); +var nCartesianProduct = require( '@stdlib/array/base/n-cartesian-product' ); +var dtypes = require( '@stdlib/ndarray/dtypes' ); +var logEachMap = require( '@stdlib/console/log-each-map' ); +var inputCastingDataType = require( './../lib' ); + +// Get the list of real-valued data types: +var dt = dtypes( 'real' ); + +// Define a list of casting policies: +var policies = [ + 'none', + 'promoted', + 'accumulation', + 'output' +]; + +// Generate dtype-policy argument groups: +var args = nCartesianProduct( dt, dt, dt, policies ); + +// Unzip the argument arrays: +args = unzip( args ); + +// Resolve casting data types: +logEachMap( 'dtypes: (%10s, %10s, %10s). policy: %20s. result: %10s.', args[ 0 ], args[ 1 ], args[ 2 ], args[ 3 ], naryFunction( inputCastingDataType, 4 ) ); diff --git a/lib/node_modules/@stdlib/ndarray/base/binary-input-casting-dtype/lib/index.js b/lib/node_modules/@stdlib/ndarray/base/binary-input-casting-dtype/lib/index.js new file mode 100644 index 000000000000..669558b17fcd --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/binary-input-casting-dtype/lib/index.js @@ -0,0 +1,40 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 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'; + +/** +* Resolve the input ndarray casting data type for a binary function. +* +* @module @stdlib/ndarray/base/binary-input-casting-dtype +* +* @example +* var resolve = require( '@stdlib/ndarray/base/binary-input-casting-dtype' ); +* +* var dt = resolve( 'float64', 'float64', 'float64', 'none' ); +* // returns +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/ndarray/base/binary-input-casting-dtype/lib/main.js b/lib/node_modules/@stdlib/ndarray/base/binary-input-casting-dtype/lib/main.js new file mode 100644 index 000000000000..6eeb7ce0c140 --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/binary-input-casting-dtype/lib/main.js @@ -0,0 +1,108 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 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. +*/ + +/* eslint-disable id-length, function-paren-newline */ + +'use strict'; + +// MODULES // + +var isFloatingPointDataType = require( '@stdlib/ndarray/base/assert/is-floating-point-data-type' ); +var isSignedIntegerDataType = require( '@stdlib/ndarray/base/assert/is-signed-integer-data-type' ); +var isUnsignedIntegerDataType = require( '@stdlib/ndarray/base/assert/is-unsigned-integer-data-type' ); +var isDataType = require( '@stdlib/ndarray/base/assert/is-data-type' ); +var promotionRules = require( '@stdlib/ndarray/promotion-rules' ); +var defaults = require( '@stdlib/ndarray/defaults' ); +var format = require( '@stdlib/string/format' ); + + +// VARIABLES // + +var DEFAULT_SIGNED_INTEGER_DTYPE = defaults.get( 'dtypes.signed_integer' ); +var DEFAULT_UNSIGNED_INTEGER_DTYPE = defaults.get( 'dtypes.unsigned_integer' ); +var DEFAULT_REAL_FLOATING_POINT_DTYPE = defaults.get( 'dtypes.real_floating_point' ); + + +// MAIN // + +/** +* Resolves the input ndarray casting data type for a binary function. +* +* @param {string} idtype1 - first input ndarray data type +* @param {string} idtype2 - second input ndarray data type +* @param {string} odtype - output ndarray data type +* @param {string} policy - input ndarray data type casting policy +* @throws {TypeError} fourth argument must be a recognized data type policy +* @throws {Error} unexpected error +* @returns {string} data type +* +* @example +* var dt = resolve( 'float32', 'float32', 'float64', 'none' ); +* // returns +*/ +function resolve( idtype1, idtype2, odtype, policy ) { + var dt; + if ( policy === 'none' ) { + if (idtype1 !== idtype2) { + throw new Error( format( 'Inputs must match under "none" policy. Got %s and %s.', idtype1, idtype2 ) ); + } + return idtype1; + } + if ( policy === 'output' ) { + return odtype; + } + if ( policy === 'promoted' ) { + dt = promotionRules( idtype1, idtype2 ); + if ( dt === -1 ) { + throw new Error( format( 'invalid operation. Unable to promote the first input and second input data types. First input data type: %s. Second input data type: %s.', idtype1, idtype2 ) ); + } + dt = promotionRules( dt, odtype ); + if ( dt === -1 ) { + throw new Error( format( 'invalid operation. Unable to promote the input and output data types. Input data type: %s. Output data type: %s.', dt, odtype ) ); + } + return dt; + } + if ( policy === 'accumulation' ) { + if ( isFloatingPointDataType( idtype1 ) || isFloatingPointDataType( idtype2 ) || idtype1 === 'generic' || idtype2 === 'generic' ) { // NOTE: we may want to revisit this in the future for float16/complex32, where the value range is much more limited + return promotionRules(idtype1, idtype2); + } + // Unless the input data type value range is larger than the default un/signed integer data type, accumulate in the default un/signed integer data type, as accumulating in smaller range integer data types (e.g., `int8`) are at high risk for overflow, especially for ndarrays containing many elements... + if ( isUnsignedIntegerDataType( idtype1 ) && isUnsignedIntegerDataType( idtype2 ) ) { // eslint-disable-line max-len + return promotionRules( + promotionRules(idtype1, idtype2), DEFAULT_UNSIGNED_INTEGER_DTYPE + ); + } + if ( isSignedIntegerDataType( idtype1 ) && isSignedIntegerDataType( idtype2 ) ) { // eslint-disable-line max-len + return promotionRules( + promotionRules(idtype1, idtype2), DEFAULT_SIGNED_INTEGER_DTYPE + ); + } + // For all other input data types, accumulate in the default real-valued floating-point data type... + return DEFAULT_REAL_FLOATING_POINT_DTYPE; + } + // Check for an explicit data type... + if ( isDataType( policy ) ) { + return policy; + } + throw new TypeError( format( 'invalid argument. Fourth argument must be a supported casting policy. Value: `%s`.', policy ) ); +} + + +// EXPORTS // + +module.exports = resolve; diff --git a/lib/node_modules/@stdlib/ndarray/base/binary-input-casting-dtype/package.json b/lib/node_modules/@stdlib/ndarray/base/binary-input-casting-dtype/package.json new file mode 100644 index 000000000000..92a7051baffb --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/binary-input-casting-dtype/package.json @@ -0,0 +1,69 @@ +{ + "name": "@stdlib/ndarray/base/binary-input-casting-dtype", + "version": "0.0.0", + "description": "Resolve the input ndarray casting data type for a binary function.", + "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", + "dtypes", + "dtype", + "data", + "multidimensional", + "array", + "utilities", + "utility", + "utils", + "util", + "resolve", + "casting" + ], + "__stdlib__": {} +} diff --git a/lib/node_modules/@stdlib/ndarray/base/binary-input-casting-dtype/test/test.js b/lib/node_modules/@stdlib/ndarray/base/binary-input-casting-dtype/test/test.js new file mode 100644 index 000000000000..4b3f0b20564a --- /dev/null +++ b/lib/node_modules/@stdlib/ndarray/base/binary-input-casting-dtype/test/test.js @@ -0,0 +1,357 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 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 resolve = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof resolve, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function throws an error if provided an unsupported/unrecognized casting policy', function test( t ) { + var values; + var i; + + values = [ + 'float', + 'cmplx', + 'beep', + 'boop', + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided '+values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + resolve( 'float64', 'float64', 'float64', value ); + }; + } +}); + +tape( 'the function throws an error if provided data types which do not promote when the casting policy is "promoted"', function test( t ) { + var dt1; + var dt2; + var dt3; + var i; + + dt1 = [ + 'binary', + 'bool', + 'int32', + 'uint32', + 'binary' + ]; + dt2 = [ + 'generic', + 'float32', + 'bool', + 'int8', + 'int32' + ]; + dt3 = [ + 'float32', + 'int16', + 'float64', + 'bool', + 'float64' + ]; + for ( i = 0; i < dt1.length; i++ ) { + t.throws( badValue( dt1[ i ], dt2[ i ], dt3[ i ] ), Error, 'throws an error when provided '+dt1[ i ]+' and '+dt2[ i ]+' and '+dt3[ i ] ); + } + t.end(); + + function badValue( v1, v2, v3 ) { + return function badValue() { + resolve( v1, v2, v3, 'promoted' ); + }; + } +}); + +tape( 'the function throws an error if provided two diffrent data types when the casting policy is "none"', function test( t ) { + var dt1; + var dt2; + var dt3; + var i; + + dt1 = [ + 'float64', + 'float32', + 'int32', + 'uint16', + 'generic', + 'int8', + 'complex64' + ]; + dt2 = [ + 'float32', + 'float64', + 'uint16', + 'int32', + 'int8', + 'complex64', + 'generic' + ]; + dt3 = [ + 'float64', + 'float64', + 'float64', + 'float64', + 'float64', + 'float64', + 'float64' + ]; + for ( i = 0; i < dt1.length; i++ ) { + t.throws( badValue( dt1[ i ], dt2[ i ], dt3[ i ] ), Error, 'throws an error when provided '+dt1[ i ]+' and '+dt2[ i ]+' and '+dt3[ i ] ); + } + t.end(); + + function badValue( v1, v2, v3 ) { + return function badValue() { + resolve( v1, v2, v3, 'none' ); + }; + } +}); + +tape( 'the function resolves a casting data type (policy=none)', function test( t ) { + var expected; + var dt1; + var dt2; + var dt3; + var dt; + var i; + + dt1 = [ + 'float64', + 'float32', + 'int32', + 'uint16', + 'complex64' + ]; + dt2 = [ + 'float64', + 'float32', + 'int32', + 'uint16', + 'complex64' + ]; + dt3 = [ + 'float64', + 'float64', + 'float64', + 'float64', + 'float64' + ]; + + expected = dt1; + for ( i = 0; i < dt1.length; i++ ) { + dt = resolve( dt1[ i ], dt2[ i ], dt3[ i ], 'none' ); + t.strictEqual( dt, expected[ i ], 'returns expected value' ); + } + t.end(); +}); + +tape( 'the function resolves a casting data type (policy=output)', function test( t ) { + var expected; + var dt1; + var dt2; + var dt3; + var dt; + var i; + + dt1 = [ + 'float64', + 'float32', + 'int32', + 'uint16', + 'complex64' + ]; + dt2 = [ + 'float64', + 'float32', + 'int32', + 'uint16', + 'complex64' + ]; + dt3 = [ + 'complex128', + 'float64', + 'uint32', + 'int16', + 'generic' + ]; + + expected = dt3; + for ( i = 0; i < dt1.length; i++ ) { + dt = resolve( dt1[ i ], dt2[ i ], dt3[ i ], 'output' ); + t.strictEqual( dt, expected[ i ], 'returns expected value' ); + } + t.end(); +}); + +tape( 'the function resolves a casting data type (policy=promoted)', function test( t ) { + var expected; + var dt1; + var dt2; + var dt3; + var dt; + var i; + + dt1 = [ + 'float32', + 'int8', + 'uint16', + 'complex64', + 'complex64' + ]; + dt2 = [ + 'float64', + 'int16', + 'int32', + 'float64', + 'float64' + ]; + dt3 = [ + 'float32', + 'int8', + 'uint16', + 'complex64', + 'float32' + ]; + expected = [ + 'float64', + 'int16', + 'int32', + 'complex128', + 'complex128' + ]; + + for ( i = 0; i < dt1.length; i++ ) { + dt = resolve( dt1[ i ], dt2[ i ], dt3[ i ], 'promoted' ); + t.strictEqual( dt, expected[ i ], 'returns expected value' ); + } + t.end(); +}); + +tape( 'the function resolves a casting data type (policy=accumulation)', function test( t ) { + var expected; + var dt1; + var dt2; + var dt3; + var dt; + var i; + + dt1 = [ + 'float16', + 'float32', + 'float32', + 'float64', + 'int16' + ]; + dt2 = [ + 'float16', + 'float64', + 'uint8c', + 'float64', + 'int32' + ]; + dt3 = [ + 'int8', + 'float64', + 'float16', + 'int16', + 'int32' + ]; + expected = [ + 'float16', + 'float64', + 'float32', + 'float64', + 'int32' + ]; + + for ( i = 0; i < dt1.length; i++ ) { + dt = resolve( dt1[ i ], dt2[ i ], dt3[ i ], 'accumulation' ); + t.strictEqual( dt, expected[ i ], 'returns expected value' ); + } + t.end(); +}); + +tape( 'the function resolves a casting data type (policy=)', function test( t ) { + var expected; + var dt1; + var dt2; + var dt3; + var dt; + var i; + + dt1 = [ + 'float64', + 'float32', + 'int32', + 'uint16', + 'generic', + 'int8', + 'complex64' + ]; + dt2 = [ + 'float64', + 'float32', + 'int32', + 'uint16', + 'generic', + 'int8', + 'complex64' + ]; + dt3 = [ + 'float64', + 'float64', + 'float64', + 'float64', + 'float64', + 'float64', + 'int8' + ]; + + expected = dt3; + for ( i = 0; i < dt1.length; i++ ) { + dt = resolve( dt1[ i ], dt2[ i ], dt3[ i ], dt3[ i ] ); + t.strictEqual( dt, expected[ i ], 'returns expected value' ); + } + t.end(); +});