Skip to content

broadcast_to

Broadcasts an array to a specified shape.

Parameters:

Name Type Description Default
x

array to broadcast.

required
shape

array shape. Must be compatible with x. If the array is incompatible with the specified shape, the function raises an exception.

required

Returns:

Name Type Description
out array

an array having a specified shape and having the same data type as x.

Examples:

>>> a = sparse.COO.from_numpy(np.array([[0, 1], [2, 0]]))
>>> o = sparse.broadcast_to(a, shape=(1, 2, 2))
>>> o.todense()
array([[[0, 1],
        [2, 0]]])
Source code in sparse/numba_backend/_common.py
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
@_support_numpy
def broadcast_to(x, /, shape):
    """
    Broadcasts an array to a specified shape.

    Parameters
    ----------
    x: array
        array to broadcast.
    shape: Tuple[int, ...]
        array shape. Must be compatible with ``x``.
        If the array is incompatible with the specified shape, the function raises an exception.

    Returns
    -------
    out: array
        an array having a specified shape and having the same data type as ``x``.

    Examples
    --------
    >>> a = sparse.COO.from_numpy(np.array([[0, 1], [2, 0]]))
    >>> o = sparse.broadcast_to(a, shape=(1, 2, 2))
    >>> o.todense()  # doctest: +NORMALIZE_WHITESPACE
    array([[[0, 1],
            [2, 0]]])
    """
    return x.broadcast_to(shape)