All files / ndarray/concat/lib assign.js

90.71% Statements 166/183
81.48% Branches 22/27
100% Functions 1/1
90.71% Lines 166/183

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 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 1842x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x     25x     25x 25x 25x 25x 25x     25x 10x 10x 13x 13x 13x 13x 13x 25x 12x 4x 4x 8x 8x 8x 8x 8x 8x 9x 9x 25x 4x 1x 1x 4x 9x 25x 8x 4x 4x 4x 4x 4x 9x 9x 9x 9x 9x 25x     4x 25x                   4x 4x 4x 4x 25x 8x 8x 25x 25x 8x 8x 17x 17x 8x 4x 25x 2x 2x 2x 2x 2x  
/**
* @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 broadcastArrayExceptDimensions = require( '@stdlib/ndarray/base/broadcast-array-except-dimensions' );
var normalizeIndex = require( '@stdlib/ndarray/base/normalize-index' );
var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' );
var isNonNegativeInteger = require( '@stdlib/assert/is-nonnegative-integer' );
var isInteger = require( '@stdlib/assert/is-integer' );
var indicesComplement = require( '@stdlib/array/base/indices-complement' );
var nditerStacks = require( '@stdlib/ndarray/iter/stacks' );
var getShape = require( '@stdlib/ndarray/shape' );
var getStrides = require( '@stdlib/ndarray/strides' );
var shape2strides = require( '@stdlib/ndarray/base/shape2strides' );
var getDtype = require( '@stdlib/ndarray/dtype' );
var getOrder = require( '@stdlib/ndarray/order' );
var getData = require( '@stdlib/ndarray/data-buffer' );
var ndarray = require( '@stdlib/ndarray/ctor' );
var format = require( '@stdlib/string/format' );
var base = require( '@stdlib/ndarray/base/assign' );
var output = require( './output.js' );
 
 
// MAIN //
 
/**
* Concatenates a list of ndarrays along a specified ndarray dimension and assigns results to a provided output ndarray.
*
* @param {ArrayLikeObject<ndarray>} arrays - array-like object containing input ndarrays
* @param {ndarray} out - output ndarray
* @param {NegativeInteger} dim - dimension along which the ndarrays are concatenated
* @throws {TypeError} first argument must be an array of ndarray-like objects
* @throws {RangeError} first argument must have more than one ndarray
* @throws {TypeError} second argument must be an array of ndarray-like objects
* @returns {ndarray} output ndarray
*
* @example
* var ndarray = require( '@stdlib/ndarray/ctor' );
* var Float64Array = require( '@stdlib/array/float64' );
* var ndarray2array = require( '@stdlib/ndarray/to-array' );
*
* var xbuf = new Float64Array( [ -1.0, 2.0, -3.0, 4.0 ] );
* var x = new ndarray( 'float64', xbuf, [ 2, 2 ], [ 2, 1 ], 0, 'row-major' );
*
* var ybuf = new Float64Array( [ -5.0, 6.0, -7.0, 8.0, -9.0, 10.0 ] );
* var y = new ndarray( 'float64', ybuf, [ 2, 3 ], [ 3, 1 ], 0, 'row-major' );
*
* var z = new ndarray( 'float64', new Float64Array( 10 ), [ 2, 5 ], [ 5, 1 ], 0, 'row-major' );
*
* var out = assign( [ x, y ], z, -1 );
* // returns <ndarray>
*
* var bool = ( out === z );
* // returns true
*
* var arr = ndarray2array( z );
* // returns [ [ -1.0, 2.0, -5.0, 6.0, -7.0 ], [ -3.0, 4.0, 8.0, -9.0, 10.0 ] ]
*/
function assign( arrays, out, dim ) {
	var istacks;
	var ostacks;
	var strides;
	var shapes;
	var dtypes;
	var orders;
	var odata;
	var oord;
	var arrs;
	var err;
	var osh;
	var odt;
	var mi;
	var N;
	var d;
	var s;
	var i;
	var v;
 
	if ( arguments.length > 1 ) {
		if ( !isInteger( dim ) ) {
			throw new TypeError( format( 'invalid argument. Second argument must be a negative integer. Value: `%s`.', dim ) );
		}
		if ( isNonNegativeInteger( dim ) ) {
			throw new TypeError( format( 'invalid argument. Second argument must be a negative integer. Value: `%s`.', dim ) );
		}
	}
 
	N = arrays.length;
	arrs = [];
	if ( N < 1 ) {
		throw new RangeError( format( 'invalid argument. First argument must have one or more ndarrays. Value: `%s`.', N ) );
	}
	if ( !isndarrayLike( out ) ) {
		throw new TypeError( format( 'invalid argument. Second argument must be an ndarray-like object. Value: `%s`.', out ) );
	}
	// Unpack the ndarrays and standardize ndarray meta data:
	shapes = [];
	strides = [];
	dtypes = [];
	orders = [];
	for ( i = 0; i < N; i++ ) {
		if ( !isndarrayLike( arrays[ i ] ) ) {
			throw new TypeError( format( 'invalid argument. First argument must be an array of ndarray-like objects. Value: `%s`.', arrays[ i ] ) );
		}
		arrs[ i ] = arrays[ i ];
		shapes.push( getShape( arrs[ i ] ) );
		strides.push( getStrides( arrs[ i ] ) );
		dtypes.push( getDtype( arrs[ i ] ) );
		orders.push( getOrder( arrs[ i ] ) );
	}
	// Determine the ndarray with max-rank:
	mi = 0;
	for ( i = 1; i < N; i++ ) {
		if ( shapes[ i ].length > shapes[ mi ].length ) {
			mi = i;
		}
	}
	// Broadcast all ndarrays to shape of max-rank ndarray:
	for ( i = 0; i < N; i++ ) {
		if ( i === mi ) {
			continue;
		}
		arrs[ i ] = broadcastArrayExceptDimensions( arrs[ i ], shapes[ mi ], [ dim ] ); // eslint-disable-line max-len
		shapes[ i ] = getShape( arrs[ i ] );
	}
	// Normalize dimension index:
	d = normalizeIndex( dim, shapes[ mi ].length - 1 );
 
	// Validate the output ndarray:
	err = output( shapes, dtypes, orders, d, out );
	if ( err ) {
		throw err;
	}
	osh = getShape( out );
	if ( osh.length === 1 ) {
		odt = getDtype( out );
		odata = getData( out );
		oord = getOrder( out );
		for ( i = 0; i < arrs.length; i++ ) {
			v = ndarray( odt, odata, arrs[ i ].shape, shape2strides( arrs[ i ].shape, oord ), 0, oord ); // eslint-disable-line max-len
			base( [ arrs[ i ], v ] );
		}
		return out;
	}
	// Create iterator for output subarrays:
	ostacks = nditerStacks( out, indicesComplement( osh.length, [ d ] ) );
 
	// Assign each input subarray to relevant output subarray:
	for ( i = 0; i < arrs.length; i++ ) {
		istacks = nditerStacks( arrs[ i ], indicesComplement( arrs[ i ].shape.length, [ d ] ) ); // eslint-disable-line max-len
		while ( true ) {
			s = istacks.next();
			if ( s.done ) {
				break;
			}
			base( [ s.value, ostacks.next().value ] );
		}
	}
	return out;
}
 
 
// EXPORTS //
 
module.exports = assign;