Repository: scipy/scipy
File: scipy/sparse/_construct.py (Python)

Code before:
        an axis, as does ``N``. The new axis has shape ``1`` and indices are
        created with the value ``0``.

    Returns
    -------
    out : sparse array
        A expanded copy output in COO format with the same dtype as A.

    Raises
    ------
    ValueError
        If provided a non-integer or out of range ``[-N-1, N]`` axis,
        where ``N`` is ``A.ndim``.
    """
    if not isintlike(axis):
        raise ValueError(f"Invalid axis {axis}. Must be an integer.")
    idx = axis if axis >= 0 else axis + A.ndim + 1
    if idx < 0 or idx > A.ndim:
        raise ValueError(f"Invalid axis {axis} for N={A.ndim}. Must be in [-N-1, N].")

    A = A.tocoo(copy=True)
    new_coord = np.zeros_like(A.coords[0])

    A.coords = A.coords[:idx] + (new_coord,) + A.coords[idx:]
    A._shape = A.shape[:idx] + (1,) + A.shape[idx:]
    A.has_canonical_format = False  # data not guaranteed sorted
    return A


def swapaxes(A, axis1, axis2):
    """Interchange two axes of an array.

    Parameters
    ----------
    A : sparse array
    axis1 : int
        First axis.
    axis2 : int
        Second axis.

    Returns
    -------
    a_swapped : sparse array in COO format
        A copy of the input array with the two identified axes swapped.

    Raises
    ------
    ValueError
        If provided a non-integer or out of range ``[-N, N-1]`` axis,
        where ``N`` is ``A.ndim``.
    """

Review comment (refactor):
I don't think we need to set this here:
 - adding a new trivial dimension won't create any duplicate indices
 - if indices were already sorted, adding a zero at any position won't change the sort order

If I'm missing something, please let me know.

Code after:
        an axis, as does ``N``. The new axis has shape ``1`` and indices are
        created with the value ``0``.

    Returns
    -------
    out : sparse array
        A expanded copy output in COO format with the same dtype as `A`.

    Raises
    ------
    ValueError
        If provided a non-integer or out of range ``[-N-1, N]`` axis,
        where ``N`` is ``A.ndim``.
    """
    if not isintlike(axis):
        raise ValueError(f"Invalid axis {axis}. Must be an integer.")
    idx = axis if axis >= 0 else axis + A.ndim + 1
    if idx < 0 or idx > A.ndim:
        raise ValueError(f"Invalid axis {axis} for N={A.ndim}. Must be in [-N-1, N].")

    newA = A.tocoo(copy=True)
    new_coord = np.zeros_like(newA.coords[0])

    newA.coords = newA.coords[:idx] + (new_coord,) + newA.coords[idx:]
    newA._shape = newA.shape[:idx] + (1,) + newA.shape[idx:]
    if A.format == "coo":
        newA.has_canonical_format = A.has_canonical_format
    return newA


def swapaxes(A, axis1, axis2):
    """Interchange two axes of an array.

    Parameters
    ----------
    A : sparse array
    axis1 : int
        First axis.
    axis2 : int
        Second axis.

    Returns
    -------
    a_swapped : sparse array in COO format
        A copy of the input array with the two identified axes swapped.

    Raises
    ------
    ValueError
        If provided a non-integer or out of range ``[-N, N-1]`` axis,
        where ``N`` is ``A.ndim``.