main.js (4291B)
1 /** 2 * @license Apache-2.0 3 * 4 * Copyright (c) 2021 The Stdlib Authors. 5 * 6 * Licensed under the Apache License, Version 2.0 (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 */ 18 19 'use strict'; 20 21 // MODULES // 22 23 var ndarray = require( './../../../base/ctor' ); 24 var copy = require( './copy_array.js' ); 25 26 27 // MAIN // 28 29 /** 30 * Broadcasts an ndarray to a specified shape. 31 * 32 * ## Notes 33 * 34 * - The returned array is a view on the input array data buffer. The view is typically **not** contiguous. As more than one element of a returned view may refer to the same memory location, writing to the view may affect multiple elements. If you need to write to the returned array, copy the array before performing operations which may mutate elements. 35 * 36 * @param {ndarray} arr - input array 37 * @param {NonNegativeIntegerArray} shape - desired shape 38 * @throws {Error} input array cannot have more dimensions than the desired shape 39 * @throws {Error} input array dimension sizes must be `1` or equal to the corresponding dimension in the provided shape 40 * @throws {Error} input array and desired shape must be broadcast compatible 41 * @returns {ndarray} broadcasted array 42 * 43 * @example 44 * var array = require( '@stdlib/ndarray/array' ); 45 * 46 * var x = array( [ [ 1, 2 ], [ 3, 4 ] ] ); 47 * // returns <ndarray> 48 * 49 * var shx = x.shape; 50 * // returns [ 2, 2 ] 51 * 52 * var y = broadcastArray( x, [ 3, 2, 2 ] ); 53 * // returns <ndarray> 54 * 55 * var shy = y.shape; 56 * // returns [ 3, 2, 2 ] 57 * 58 * var v = y.get( 0, 0, 0 ); 59 * // returns 1 60 * 61 * v = y.get( 0, 0, 1 ); 62 * // returns 2 63 * 64 * v = y.get( 1, 0, 0 ); 65 * // returns 1 66 * 67 * v = y.get( 1, 1, 0 ); 68 * // returns 3 69 * 70 * v = y.get( 2, 0, 0 ); 71 * // returns 1 72 * 73 * v = y.get( 2, 1, 1 ); 74 * // returns 4 75 * 76 * @example 77 * var array = require( '@stdlib/ndarray/array' ); 78 * 79 * var x = array( [ [ 1, 2 ], [ 3, 4 ] ] ); 80 * // returns <ndarray> 81 * 82 * var y = broadcastArray( x, [ 3, 2 ] ); 83 * // throws <Error> 84 */ 85 function broadcastArray( arr, shape ) { 86 var strides; 87 var dim; 88 var sh; 89 var st; 90 var N; 91 var M; 92 var d; 93 var i; 94 var j; 95 96 N = shape.length; 97 sh = arr.shape; 98 M = sh.length; 99 if ( N < M ) { 100 throw new Error( 'invalid argument. Cannot broadcast an array to a shape having fewer dimensions. Arrays can only be broadcasted to shapes having the same or more dimensions.' ); 101 } 102 // Initialize a strides array... 103 strides = []; 104 for ( i = 0; i < N; i++ ) { 105 strides.push( 0 ); 106 } 107 // Determine the output array strides... 108 st = arr.strides; 109 for ( i = N-1; i >= 0; i-- ) { 110 j = M - N + i; 111 if ( j < 0 ) { 112 // Prepended singleton dimension; stride is zero... 113 continue; 114 } 115 d = sh[ j ]; 116 dim = shape[ i ]; 117 if ( dim !== 0 && dim < d ) { 118 throw new Error( 'invalid argument. Input array cannot be broadcast to the specified shape, as the specified shape has a dimension whose size is less than the size of the corresponding dimension in the input array. Array shape: ('+copy( sh ).join( ', ' )+'). Desired shape: ('+copy( shape ).join( ', ' )+'). Dimension: '+i+'.' ); 119 } 120 if ( d === dim ) { 121 strides[ i ] = st[ j ]; 122 } else if ( d === 1 ) { 123 // In order to broadcast dimensions, we set the stride for that dimension to zero... 124 strides[ i ] = 0; 125 } else { 126 // At this point, we know that `dim > d` and that `d` does not equal `1` (e.g., `dim=3` and `d=2`); in which case, the shapes are considered incompatible (even for desired shapes which are multiples of array dimensions, as might be desired when "tiling" an array; e.g., `dim=4` and `d=2`)... 127 throw new Error( 'invalid argument. Input array and the specified shape are broadcast incompatible. Array shape: ('+copy( sh ).join( ', ' )+'). Desired shape: ('+copy( shape ).join( ', ' )+'). Dimension: '+i+'.' ); 128 } 129 } 130 return ndarray( arr.dtype, arr.data, copy( shape ), strides, arr.offset, arr.order ); // eslint-disable-line max-len 131 } 132 133 134 // EXPORTS // 135 136 module.exports = broadcastArray;