Skip to content

flip

Reverses the order of elements in an array along the given axis.

The shape of the array is preserved.

Parameters:

Name Type Description Default
a COO

Input COO array.

required
axis int or tuple of ints

Axis (or axes) along which to flip. If axis is None, the function must flip all input array axes. If axis is negative, the function must count from the last dimension. If provided more than one axis, the function must flip only the specified axes. Default: None.

None

Returns:

Name Type Description
result COO

An output array having the same data type and shape as x and whose elements, relative to x, are reordered.

Source code in sparse/numba_backend/_coo/common.py
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
def flip(x, /, *, axis=None):
    """
    Reverses the order of elements in an array along the given axis.

    The shape of the array is preserved.

    Parameters
    ----------
    a : COO
        Input COO array.
    axis : int or tuple of ints, optional
        Axis (or axes) along which to flip. If ``axis`` is ``None``, the function must
        flip all input array axes. If ``axis`` is negative, the function must count from
        the last dimension. If provided more than one axis, the function must flip only
        the specified axes. Default: ``None``.

    Returns
    -------
    result : COO
        An output array having the same data type and shape as ``x`` and whose elements,
        relative to ``x``, are reordered.

    """

    x = _validate_coo_input(x)

    if axis is None:
        axis = range(x.ndim)
    if not isinstance(axis, Iterable):
        axis = (axis,)

    new_coords = x.coords.copy()
    for ax in axis:
        new_coords[ax, :] = x.shape[ax] - 1 - x.coords[ax, :]

    from .core import COO

    return COO(
        new_coords,
        x.data,
        shape=x.shape,
        fill_value=x.fill_value,
    )