跳到内容

翻转

沿给定轴反转数组中元素的顺序。

数组的形状保持不变。

参数

名称 类型 描述 默认值
a COO

输入 COO 数组。

必需
axis 整型或整数元组

要翻转的轴(或多个轴)。如果 axisNone,函数必须翻转所有输入数组的轴。如果 axis 为负,函数必须从最后一个维度开始计数。如果提供了多个轴,函数必须只翻转指定的轴。默认值:None

None

返回值

名称 类型 描述
result COO

一个与 x 具有相同数据类型和形状的输出数组,并且其元素相对于 x 重新排序。

源代码位于 sparse/numba_backend/_coo/common.py
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
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,
    )