Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 | 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 24x 126x 86x 102x 16x 16x 126x 474x 474x 474x 474x 101x 101x 373x 373x 287x 287x 108x 108x 108x 179x 179x 179x 179x 179x 287x 86x 86x 84x 84x 18x 18x 3x 3x 15x 15x 9x 9x 9x 9x 9x 6x 6x 6x 6x 18x 66x 66x 49x 49x 17x 17x 9x 9x 9x 9x 9x 8x 8x 8x 8x 84x 2x 2x 2x 2x 2x 2x 474x 112x 126x 4x 4x 4x 4x 4x | /**
* @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 isMostlySafeCast = require( '@stdlib/ndarray/base/assert/is-mostly-safe-data-type-cast' );
var isEqualDataType = require( '@stdlib/ndarray/base/assert/is-equal-data-type' );
var maybeBroadcastArray = require( '@stdlib/ndarray/base/maybe-broadcast-array' );
var broadcastScalar = require( '@stdlib/ndarray/base/broadcast-scalar' );
var getOrder = require( '@stdlib/ndarray/base/order' );
var getShape = require( '@stdlib/ndarray/base/shape' );
var baseEmpty = require( '@stdlib/ndarray/base/empty' );
var assign = require( '@stdlib/ndarray/base/assign' );
var format = require( '@stdlib/string/format' );
var ENUMS = require( './type_enums.js' );
// MAIN //
/**
* Normalizes a list of arguments to a list of ndarrays.
*
* @private
* @param {Array} args - list of arguments
* @param {Array} types - argument types
* @param {Array} dtypes - argument data types
* @param {NonNegativeIntegerArray} shape - array shape
* @param {string} order - array order
* @throws {TypeError} only (mostly) safe casts are supported
* @returns {Array<ndarray>} list of ndarrays
*/
function normalizeArguments( args, types, dtypes, shape, order ) {
var odt;
var bdt;
var out;
var N;
var t;
var v;
var i;
N = args.length;
out = [ null, null, null, null ]; // [ base, start, stop, endpoint ]
odt = dtypes[ 4 ];
// Resolve the real-valued base data type matching the output precision:
if ( odt === 'float32' || odt === 'complex64' ) {
bdt = 'float32';
} else if ( odt === 'float64' || odt === 'complex128' ) {
bdt = 'float64';
} else {
bdt = dtypes[ 0 ];
}
for ( i = 0; i < N; i++ ) {
t = types[ i ];
// Case: args[ i ] === endpoint
if ( t === ENUMS.BOOLEAN ) {
out[ i ] = broadcastScalar( args[ i ], dtypes[ i ], shape, order );
}
// Case: args[ i ] === <number>
else if ( t === ENUMS.NUMBER ) {
// Case: args[ i ] === base
if ( i === 0 ) {
// Kernels expect a real-valued base matching the output precision:
out[ i ] = broadcastScalar( args[ i ], bdt, shape, order );
}
// Case: args[ i ] === start/stop
else {
// A number primitive should be able to cast to any supported output data type (real or complex):
out[ i ] = broadcastScalar( args[ i ], odt, shape, order );
}
}
// Case: args[ i ] === <ndarray>
else if ( t === ENUMS.NDARRAY ) {
// Case: args[ i ] === base
if ( i === 0 ) {
// Case: base has the same dtype as expected by the selected kernel
if ( isEqualDataType( dtypes[ i ], bdt ) ) {
out[ i ] = maybeBroadcastArray( args[ i ], shape );
}
// Case: base has a different dtype than expected by the selected kernel
else if ( isMostlySafeCast( dtypes[ i ], bdt ) ) {
// If we have unequal data types, we need to perform a copy...
v = baseEmpty( bdt, getShape( args[ i ], false ), getOrder( args[ i ] ) ); // eslint-disable-line max-len
assign( [ args[ i ], v ] );
out[ i ] = maybeBroadcastArray( v, shape );
}
// Case: disallowed data type cast
else {
throw new TypeError( format( 'invalid argument. Argument %d cannot be safely cast to the desired output data type. Output data type: %s. Argument data type: %s.', i+1, String( odt ), String( dtypes[ i ] ) ) );
}
}
// Case: args[ i ] === endpoint || start/stop which have the same dtype as the output dtype
else if ( i === 3 || isEqualDataType( dtypes[ i ], odt ) ) {
out[ i ] = maybeBroadcastArray( args[ i ], shape );
}
// Case: args[ i ] === start/stop which have different dtype than output dtype
else if ( isMostlySafeCast( dtypes[ i ], odt ) ) {
// If we have unequal data types, we need to perform a copy...
v = baseEmpty( odt, getShape( args[ i ], false ), getOrder( args[ i ] ) ); // eslint-disable-line max-len
assign( [ args[ i ], v ] );
out[ i ] = maybeBroadcastArray( v, shape );
}
// Case: disallowed complex-to-real data type cast
else {
throw new TypeError( format( 'invalid argument. Argument %d cannot be safely cast to the desired output data type. Output data type: %s. Argument data type: %s.', i+1, String( odt ), String( dtypes[ i ] ) ) );
}
}
// Case: args[ i ] === <complex>
else {
// Complex number scalars should only be able to cast to complex data types...
if ( !isMostlySafeCast( dtypes[ i ], odt ) ) {
throw new TypeError( format( 'invalid argument. Argument %d cannot be safely cast to the desired output data type. Output data type: %s. Argument data type: %s.', i+1, String( odt ), String( dtypes[ i ] ) ) );
}
out[ i ] = broadcastScalar( args[ i ], odt, shape, order );
}
}
return out;
}
// EXPORTS //
module.exports = normalizeArguments;
|