broadcast_to#
- named_arrays.broadcast_to(array: float | complex, shape: dict[str, int], append: bool = False) ScalarArray[ndarray]#
- named_arrays.broadcast_to(array: NDArrayT, shape: dict[str, int], append: bool = False) ScalarArray[NDArrayT]
- named_arrays.broadcast_to(array: ArrayT, shape: dict[str, int], append: bool = False) ArrayT
Broadcast the given array to a given shape.
- Parameters:
array – The array to broadcast.
shape – The desired shape of the output array.
append – A boolean flag indicating whether to throw an error if there are axes in array that aren’t in shape. If append is
False, the axes of array must be a subset of shape, otherwise aValueErroris raised. If append isTrue, the array will be broadcasted to the shape:na.broadcast_shapes(array.shape, shape).
See also
numpy.broadcast_to()Equivalent Numpy function.
Examples
If we define some array shapes as
import named_arrays as na shape_x = dict(x=3) shape_y = dict(y=4)
and a random 1D array as an example
a = na.random.uniform(0, 1, shape_x)
Then we can broadcast it to 2 dimensions using the union of shape_x and shape_y
na.broadcast_to(a, shape_x | shape_y)
ScalarArray( ndarray=[[0.93703776, 0.93703776, 0.93703776, 0.93703776], [0.98034735, 0.98034735, 0.98034735, 0.98034735], [0.49277887, 0.49277887, 0.49277887, 0.49277887]], axes=('x', 'y'), )Alternatively, we can set the append keyword to
Trueso that we only need to provide shape_y since the shape of the array is already shape_x.na.broadcast_to(a, shape_y, append=True)
ScalarArray( ndarray=[[0.93703776, 0.93703776, 0.93703776, 0.93703776], [0.98034735, 0.98034735, 0.98034735, 0.98034735], [0.49277887, 0.49277887, 0.49277887, 0.49277887]], axes=('x', 'y'), )