All files main.js

100% Statements 146/146
100% Branches 21/21
100% Functions 1/1
100% Lines 146/146

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 1471x 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 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 3x 3x 25x 25x 25x 25x 28x 9x 9x 28x 12x 16x 4x 4x 25x 25x 25x 25x 28x 53x 53x 53x 53x 53x 43x 43x 43x 53x 10x 10x 10x 10x 53x 28x 23x 23x 28x 2x 2x 2x 25x 25x 25x 25x 25x 25x 25x 25x 28x 1x 1x 1x 1x 1x  
/**
* @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 ndarray = require( '@stdlib/ndarray/base/ctor' );
var assign = require( '@stdlib/ndarray/base/assign' );
var buffer = require( '@stdlib/ndarray/base/buffer' );
var numel = require( '@stdlib/ndarray/base/numel' );
var shape2strides = require( '@stdlib/ndarray/base/shape2strides' );
var strides2order = require( '@stdlib/ndarray/base/strides2order' );
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 getDType = require( '@stdlib/ndarray/base/dtype' );
var getData = require( '@stdlib/ndarray/base/data-buffer' );
var format = require( '@stdlib/string/format' );
var zeros = require( '@stdlib/array/base/zeros' );
 
 
// MAIN //
 
/**
* Returns an ndarray created by repeating the elements of an input ndarray a specified number of times along each dimension.
*
* ## Notes
*
* -   The number of repetitions must have at least as many elements as the number of input dimensions. When the number of repetitions exceeds the number of input dimensions, the input array is treated as if singleton dimensions were prepended.
* -   The function always copies data to a new ndarray.
*
* @param {ndarray} x - input array
* @param {NonNegativeIntegerArray} reps - number of repetitions along each dimension
* @throws {RangeError} second argument must have at least as many elements as the number of input dimensions
* @returns {ndarray} output array
*
* @example
* var array = require( '@stdlib/ndarray/array' );
*
* var x = array( [ [ 1, 2 ], [ 3, 4 ] ] );
* // returns <ndarray>[ [ 1, 2 ], [ 3, 4 ] ]
*
* var y = tile( x, [ 2, 2 ] );
* // returns <ndarray>[ [ 1, 2, 1, 2 ], [ 3, 4, 3, 4 ], [ 1, 2, 1, 2 ], [ 3, 4, 3, 4 ] ]
*/
function tile( x, reps ) {
	var dtype;
	var order;
	var shx;
	var sho;
	var shi;
	var sto;
	var buf;
	var out;
	var Ds;
	var si;
	var sx;
	var v1;
	var v2;
	var L;
	var M;
	var N;
	var o;
	var s;
	var t;
	var i;
	var j;
 
	shx = getShape( x, false );
	sx = getStrides( x, false );
	dtype = getDType( x );
	N = reps.length;
	M = shx.length;
 
	if ( N < M ) {
		throw new RangeError( format( 'invalid argument. Second argument must have at least as many elements as the number of dimensions of the first argument. Number of input dimensions: `%u`. Number of repetitions: `%u`.', M, N ) );
	}
	// Compute the implicit left-padding amount for the input shape:
	Ds = N - M;
 
	o = strides2order( sx );
	if ( o === 0 || o === 3 ) {
		// Fallback to stated layout when unable to infer the underlying physical layout:
		order = getOrder( x );
	} else if ( o === 1 ) {
		order = 'row-major';
	} else {
		order = 'column-major';
	}
	// Compute an interleaved 2N-D shape and strides for broadcasting the input, along with the final N-D output shape. By inserting a repetition axis next to each original axis, a contiguous 2N-D buffer can be reinterpreted as the N-D tiled output without an additional copy...
	shi = zeros( 2*N );
	si = zeros( 2*N );
	sho = zeros( N );
	for ( i = 0; i < N; i++ ) {
		j = i - Ds;
		s = ( j < 0 ) ? 1 : shx[ j ];
		t = ( j < 0 ) ? 0 : sx[ j ];
		sho[ i ] = reps[ i ] * s;
		if ( order === 'row-major' ) {
			shi[ 2*i ] = reps[ i ];
			shi[ (2*i)+1 ] = s;
			si[ (2*i)+1 ] = t;
		} else {
			shi[ 2*i ] = s;
			shi[ (2*i)+1 ] = reps[ i ];
			si[ 2*i ] = t;
		}
	}
	if ( N > 0 ) {
		L = numel( sho );
		sto = shape2strides( sho, order );
	} else {
		L = 1;
		sto = [ 0 ];
	}
	buf = buffer( dtype, L );
	out = new x.constructor( dtype, buf, sho, sto, 0, order );
 
	// Assign the broadcasted input to a 2N-D view over the output data buffer...
	v1 = ndarray( dtype, getData( x ), shi, si, getOffset( x ), order );
	v2 = ndarray( dtype, buf, shi, shape2strides( shi, order ), 0, order );
	assign( [ v1, v2 ] );
	return out;
}
 
 
// EXPORTS //
 
module.exports = tile;