All files main.js

100% Statements 162/162
100% Branches 23/23
100% Functions 1/1
100% Lines 162/162

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 1631x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 38x 2x 2x 36x 36x 38x 4x 4x 38x 2x 2x 38x 3x 3x 38x 2x 2x 25x 38x 1x 1x 38x 24x 24x 24x 24x 24x 72x 48x 48x 48x 48x 72x 8x 8x 8x 8x 2x 2x 24x 16x 16x 16x 16x 12x 12x 16x 72x 72x 72x 24x 25x 25x 25x 38x 1x 1x 1x 1x 1x  
/**
* @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 isRowMajor = require( '@stdlib/ndarray/base/assert/is-row-major-string' );
var isSortedAscending = require( '@stdlib/array/base/assert/is-sorted-ascending' );
var toNormalizedIndices = require( '@stdlib/ndarray/base/to-unique-normalized-indices' );
var getDType = require( '@stdlib/ndarray/base/dtype' );
var getShape = require( '@stdlib/ndarray/base/shape' );
var getStrides = require( '@stdlib/ndarray/base/strides' );
var getOffset = require( '@stdlib/ndarray/base/offset' );
var getOrder = require( '@stdlib/ndarray/base/order' );
var getData = require( '@stdlib/ndarray/base/data-buffer' );
var ones = require( '@stdlib/array/base/ones' );
var zeros = require( '@stdlib/array/base/zeros' );
var format = require( '@stdlib/string/format' );
 
 
// MAIN //
 
/**
* Expands the shape of an array to a specified dimensionality by spreading its dimensions to specified dimension indices and inserting dimensions of size one for the remaining dimensions.
*
* ## Notes
*
* -   Each provided dimension index must reside on the interval `[-ndims, ndims-1]`. If provided a negative dimension index, the position at which to place a respective dimension is computed as `ndims + index`.
* -   Provided dimension indices must resolve to normalized dimension indices arranged in ascending order.
*
* @param {NonNegativeInteger} ndims - number of dimensions in the output array
* @param {ndarray} x - input array
* @param {IntegerArray} dims - dimension indices at which to spread array dimensions
* @param {boolean} writable - boolean indicating whether a returned array should be writable
* @throws {RangeError} first argument must be greater than or equal to the number of dimensions in the input array
* @throws {RangeError} must provide the same number of dimension indices as the number of dimensions in the input array
* @throws {RangeError} must provide valid dimension indices
* @throws {Error} must provide unique dimension indices
* @throws {Error} dimension indices must resolve to normalized dimension indices arranged in ascending order
* @returns {ndarray} output array
*
* @example
* var array = require( '@stdlib/ndarray/array' );
*
* var x = array( [ [ 1, 2 ], [ 3, 4 ] ] );
* // returns <ndarray>
*
* var shx = x.shape;
* // returns [ 2, 2 ]
*
* var y = spreadDimensions( 5, x, [ 1, 3 ], false );
* // returns <ndarray>
*
* var shy = y.shape;
* // returns [ 1, 2, 1, 2, 1 ]
*
* var v = y.get( 0, 0, 0, 0, 0 );
* // returns 1
*
* v = y.get( 0, 0, 0, 1, 0 );
* // returns 2
*
* v = y.get( 0, 1, 0, 0, 0 );
* // returns 3
*
* v = y.get( 0, 1, 0, 1, 0 );
* // returns 4
*/
function spreadDimensions( ndims, x, dims, writable ) {
	var strides;
	var shape;
	var isrm;
	var ord;
	var sh;
	var st;
	var d;
	var S;
	var s;
	var i;
	var j;
 
	sh = getShape( x, false );
	st = getStrides( x, false );
	ord = getOrder( x );
	isrm = isRowMajor( ord );
 
	if ( sh.length > ndims ) {
		throw new RangeError( format( 'invalid argument. First argument must be greater than or equal to the number of dimensions in the input ndarray. Number of dimensions: %d. Value: `%d`.', sh.length, ndims ) );
	}
	// Resolve dimension indices...
	d = toNormalizedIndices( dims, ndims-1 );
	if ( d === null ) {
		throw new RangeError( format( 'invalid argument. Specified dimension index is out-of-bounds. Must be on the interval: [-%u, %u]. Value: `[%s]`.', ndims, ndims-1, dims.join( ', ' ) ) );
	}
	if ( d.length !== dims.length ) {
		throw new Error( format( 'invalid argument. Must provide unique dimension indices. Value: `[%s]`.', dims.join( ', ' ) ) );
	}
	if ( d.length !== sh.length ) {
		throw new RangeError( format( 'invalid argument. Must provide the same number of dimension indices as the number of dimensions in the input ndarray. Number of dimensions: %d. Value: `[%s]`.', sh.length, dims.join( ', ' ) ) );
	}
	if ( d.length && !isSortedAscending( d ) ) {
		throw new Error( format( 'invalid argument. Must provide dimension indices which resolve to nonnegative indices arranged in ascending order. Value: `[%s]`.', dims.join( ', ' ) ) );
	}
	// When provided a zero-dimensional array, every expanded dimension is a singleton dimension having zero stride...
	if ( sh.length === 0 ) {
		shape = ones( ndims );
		strides = zeros( ndims );
	} else {
		// Interweave singleton dimensions...
		strides = [];
		shape = [];
		j = 0;
		for ( i = 0; i < ndims; i++ ) {
			if ( i === d[ j ] ) {
				// If we have a match, we can move on to inserting singleton dimensions before the next dimension index...
				S = sh[ j ];
				s = st[ j ];
				j += 1;
			} else if ( j === sh.length ) { // append
				// We should only get here after exhausting all the dimension indices...
				S = 1;
				s = st[ j-1 ];
				if ( !isrm ) {
					s *= sh[ j-1 ];
				}
			} else { // insert before
				// We have yet to reach the next specified dimension index so we insert a singleton dimension...
				S = 1;
				s = st[ j ];
				if ( isrm ) {
					s *= sh[ j ];
				}
			}
			shape.push( S );
			strides.push( s );
		}
	}
	return new x.constructor( getDType( x ), getData( x ), shape, strides, getOffset( x ), ord, { // eslint-disable-line max-len
		'readonly': !writable
	});
}
 
 
// EXPORTS //
 
module.exports = spreadDimensions;