All files / ndarray/concat/lib output.js

93.49% Statements 115/123
63.63% Branches 7/11
100% Functions 2/2
93.49% Lines 115/123

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 1243x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 4x 4x 4x 4x 4x 4x 8x     8x 4x 4x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 4x 4x 4x 8x 8x     8x 4x 4x 4x     4x 8x     8x 4x 4x 4x 4x 4x 4x 4x 4x 4x 8x 3x 3x 3x 3x 3x  
/**
* @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 promoteDataTypes = require( '@stdlib/ndarray/base/promote-dtypes' );
var mostlySafeCasts = require( '@stdlib/ndarray/mostly-safe-casts' );
var defaults = require( '@stdlib/ndarray/defaults' );
var empty = require( '@stdlib/ndarray/empty' );
var slice = require( '@stdlib/array/slice' );
var getShape = require( '@stdlib/ndarray/shape' );
var getDtype = require( '@stdlib/ndarray/dtype' );
var format = require( '@stdlib/string/format' );
 
 
// FUNCTIONS //
 
/**
* Returns output ndarray order.
*
* @private
* @param {ArrayLikeObject<IntegerArray>} orders - list of input ndarray orders
* @returns {integer} output ndarray order
*/
function resolveOutputOrder( orders ) {
	var o;
	var i;
 
	o = orders[ 0 ];
	for ( i = 0; i < orders.length; i++ ) {
		if ( orders[ i ] !== o ) {
			return defaults.order;
		}
	}
	return o;
}
 
 
// MAIN //
 
/**
* Returns an output ndarray.
*
* @param {ArrayLikeObject<IntegerArray>} shapes - input ndarray shapes
* @param {ArrayLikeObject<StringArray>} dtypes - input ndarray dtypes
* @param {ArrayLikeObject<StringArray>} orders - input ndarray orders
* @param {integer} dim - irregular dimension
* @param {ndarray} [out] - output ndarray
* @throws {TypeError} output ndarray must have a valid data type
* @throws {TypeError} output ndarray must have a valid order
* @throws {RangeError} output ndarray must have valid number of dimensions
* @throws {RangeError} output ndarray must have a valid shape
* @returns {ndarray} output ndarray
*
* @example
*/
function output( shapes, dtypes, orders, dim, out ) {
	var allowedCasts;
	var ord;
	var osh;
	var odt;
	var sh;
	var dt;
	var s;
	var i;
 
	// Resolve the output shape:
	sh = slice( shapes[ 0 ] );
	for ( i = 1; i < shapes.length; i++ ) {
		s = shapes[ i ];
		sh[ dim ] += s[ dim ];
	}
	if ( out ) {
		// When output is provided: validate that inputs can cast to output dtype
		odt = getDtype( out );
		for ( i = 0; i < dtypes.length; i++ ) {
			allowedCasts = mostlySafeCasts( dtypes[ i ] );
			if ( allowedCasts.indexOf( odt ) === -1 ) {
				throw new TypeError( format( 'invalid argument. Input ndarray at index %d has dtype `%s` which cannot be safely cast to output dtype `%s`.', i, dtypes[ i ], odt ) );
			}
		}
		// Validate output shape:
		osh = getShape( out );
		if ( osh.length !== sh.length ) {
			throw new RangeError( format( 'invalid argument. Output ndarray must have %d dimensions. Value: %d.', sh.length, getShape( out ).length ) );
		}
		for ( i = 0; i < sh.length; i++ ) {
			if ( osh[ i ] !== sh[ i ] ) {
				throw new RangeError( format( 'invalid argument. Output ndarray dimension %d must have size %d. Value: %d.', i, sh[ i ], getShape( out )[ i ] ) );
			}
		}
		return null;
	}
	dt = promoteDataTypes( dtypes );
	ord = resolveOutputOrder( orders );
	out = empty( sh, {
		'dtype': dt,
		'order': ord
	});
	return out;
}
 
 
// EXPORTS //
 
module.exports = output;