Skip to content

matrix_transpose

Transposes a matrix or a stack of matrices.

Parameters:

Name Type Description Default
x SparseArray

Input array.

required

Returns:

Name Type Description
out COO

Transposed COO array.

Raises:

Type Description
ValueError

If the input array isn't and can't be converted to COO format, or if x.ndim < 2.

Source code in sparse/numba_backend/_coo/common.py
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
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)