broadcast_to#

ivy.broadcast_to(x, /, shape, *, out=None)[source]#

Broadcasts an array to a specified shape.

Parameters:
  • x (Union[Array, NativeArray]) – array to broadcast.

  • shape (Tuple[int, ...]) – array shape. Must be compatible with x (see Broadcasting). If the array is incompatible with the specified shape, the function should raise an exception.

  • out (Optional[Array], default: None) – optional output array, for writing the result to. It must have a shape that the inputs broadcast to.

Return type:

Array

Returns:

ret – an array having a specified shape. Must have the same data type as x.

Examples

With ivy.Array input:

>>> x = ivy.array([1, 2, 3])
>>> y = ivy.broadcast_to(x, (3, 3))
>>> print(y)
ivy.array([[1, 2, 3],
        [1, 2, 3],
        [1, 2, 3]])

With ivy.NativeArray input:

>>> x = ivy.native_array([0.1 , 0.3])
>>> y = ivy.broadcast_to(x, (3, 2))
>>> print(y)
ivy.array([[0.1, 0.3],
        [0.1, 0.3],
        [0.1, 0.3]])

With ivy.Container input:

>>> x = ivy.Container(a=ivy.array([1, 2, 3]),
...                   b=ivy.array([4, 5, 6]))
>>> y = ivy.broadcast_to(x, (3, 3))
>>> print(y)
{
    a: ivy.array([[1, 2, 3],
                [1, 2, 3],
                [1, 2, 3]]),
    b: ivy.array([[4, 5, 6],
                [4, 5, 6],
                [4, 5, 6]])
}
Array.broadcast_to(self, /, shape, *, out=None)[source]#

ivy.Array instance method variant of ivy.broadcast_to. This method simply wraps the function, and so the docstring for ivy.broadcast_to also applies to this method with minimal changes.

Parameters:
  • self (Array) – input array to be broadcasted.

  • shape (Tuple[int, ...]) – desired shape to be broadcasted to.

  • out (Optional[Array], default: None) – Optional array to store the broadcasted array.

Return type:

Array

Returns:

ret – Returns the broadcasted array of shape ‘shape’

Examples

With ivy.Array instance method:

>>> x = ivy.array([1, 2, 3])
>>> y = x.broadcast_to((3,3))
>>> print(y)
ivy.array([[1, 2, 3],
           [1, 2, 3],
           [1, 2, 3]])
Container.broadcast_to(self, /, shape, *, key_chains=None, to_apply=True, prune_unapplied=False, map_sequences=False, out=None)[source]#

ivy.Container instance method variant of ivy.broadcast_to. This method simply wraps the function, and so the docstring for ivy.broadcast_to also applies to this method with minimal changes.

Parameters:
  • self (Container) – input array to be broadcasted.

  • shape (Union[Tuple[int, ...], Container]) – desired shape to be broadcasted to.

  • out (Optional[Container], default: None) – Optional array to store the broadcasted array.

Return type:

Container

Returns:

ret – Returns the broadcasted array of shape ‘shape’

Examples

With ivy.Container instance method:

>>> x = ivy.Container(a=ivy.array([0, 0.5]),
...                   b=ivy.array([4, 5]))
>>> y = x.broadcast_to((3,2))
>>> print(y)
{
    a: ivy.array([[0., 0.5],
                  [0., 0.5],
                  [0., 0.5]]),
    b: ivy.array([[4, 5],
                  [4, 5],
                  [4, 5]])
}