Skip to content

reshape

Reshapes an array without changing its data.

Parameters:

Name Type Description Default
x

input array to reshape.

required
shape

a new shape compatible with the original shape. One shape dimension is allowed to be -1. When a shape dimension is -1, the corresponding output array shape dimension must be inferred from the length of the array and the remaining dimensions.

required
copy

whether or not to copy the input array. If True, the function always copies. If False, the function must never copies. If None, the function avoids copying, if possible. Default: None.

None

Returns:

Name Type Description
out array

an output array having the same data type and elements as x.

Raises:

Type Description
ValueError

If copy=False and a copy would be necessary, a ValueError will be raised.

Examples:

>>> a = sparse.COO.from_numpy(np.array([[0, 1], [2, 0]]))
>>> o = sparse.reshape(a, shape=(1, 4))
>>> o.todense()
array([[0, 1, 2, 0]])
Source code in sparse/numba_backend/_common.py
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
def reshape(x, /, shape, *, copy=None):
    """
    Reshapes an array without changing its data.

    Parameters
    ----------
    x: array
        input array to reshape.
    shape: Tuple[int, ...]
        a new shape compatible with the original shape. One shape dimension is allowed to be ``-1``.
        When a shape dimension is ``-1``, the corresponding output array shape dimension must be inferred
        from the length of the array and the remaining dimensions.
    copy: Optional[bool]
        whether or not to copy the input array.
        If ``True``, the function always copies.
        If ``False``, the function must never copies.
        If ``None``, the function avoids copying, if possible.
        Default: ``None``.

    Returns
    -------
    out: array
        an output array having the same data type and elements as ``x``.

    Raises
    ------
    ValueError
        If ``copy=False`` and a copy would be necessary, a ``ValueError``
        will be raised.

    Examples
    --------
    >>> a = sparse.COO.from_numpy(np.array([[0, 1], [2, 0]]))
    >>> o = sparse.reshape(a, shape=(1, 4))
    >>> o.todense()
    array([[0, 1, 2, 0]])
    """
    return x.reshape(shape=shape)