跳到内容

矩阵转置

转置一个矩阵或一个矩阵堆栈。

参数

名称 类型 描述 默认值
x SparseArray

输入数组。

必需

返回值

名称 类型 描述
out COO

转置后的 COO 数组。

引发

类型 描述
ValueError

如果输入数组不是 COO 格式且无法转换为 COO 格式,或者如果 x.ndim < 2

源代码位于 sparse/numba_backend/_coo/common.py
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
def matrix_transpose(x, /):
    """
    Transposes a matrix or a stack of matrices.

    Parameters
    ----------
    x : SparseArray
        Input array.

    Returns
    -------
    out : COO
        Transposed COO array.

    Raises
    ------
    ValueError
        If the input array isn't and can't be converted to COO format, or if ``x.ndim < 2``.
    """
    if hasattr(x, "ndim") and x.ndim < 2:
        raise ValueError("`x.ndim >= 2` must hold.")
    x = _validate_coo_input(x)
    transpose_axes = list(range(x.ndim))
    transpose_axes[-2:] = transpose_axes[-2:][::-1]

    return x.transpose(transpose_axes)