Skip to content

RFC: allow moveaxis(x, (i, j), (j, i)) when i and j refer to the same axis? #1020

Open

Description

@danggeun

This came up in data-apis/array-api-extra#97 (adding swapaxes). If moveaxis(x, (i, j), (j, i)) were allowed when i and j refer to the same axis, e.g. (1, -1) on a 2D array, and returned x unchanged, then swapaxes would be a one-liner on top of moveaxis. array-api-extra would not need it.

None of the libraries I tried allow it:

Library Version moveaxis(x, (i, j), (j, i)), same axis swapaxes(x, i, j), same axis
NumPy 2.5.5 ValueError: repeated axis in `source` argument returns x
Dask 2026.8.0 ValueError: repeated axis in `source` argument returns x
JAX 0.12.1 TypeError: transpose permutation isn't a permutation of operand dimensions returns x
PyTorch 2.15.0 RuntimeError: movedim: repeated dim in `source` ([1, -1]) returns x
array-api-strict 2.6.1 ValueError: repeated axis in `source` argument no swapaxes
pydata/sparse 0.19.3 ValueError: repeated axis in transpose no swapaxes

Cases: (i, j) = (1, -1), (0, 0), (-2, 0) on shape (2, 3) and (2, -1) on shape (2, 3, 4), directly and through array-api-compat (Python 3.12.3, Windows). I couldn't run CuPy (no GPU).

The standard requires the source axes to be unique and doesn't specify the error. moveaxis was requested together with swapaxes in #483 and added in #656. swapaxes was left out; in #483 it was noted as problematic (pointing to #228) and as simple to implement in terms of moveaxis. That holds except in the same-axis case, which needs special handling.

Is there room to allow this case, or should it stay an error?

The survey script was written with Claude (Opus 5.5); I ran it myself.

Activity

  1. lucascolley commented on Nov 22, 2026

    @lucascolley
    Member

    thanks for the investigation @danggeun ! I think it would be ideal if the standard did allow this, but it sounds like it will be a major change for all prominent libraries indeed, so probably not the highest priority change in the short term.

    I'd be open to adding swapaxes to array-api-extra to address this use-case in the meantime.

  2. added
    RFC
    API change
    topic: Manipulation
    Needs Discussion
    on Nov 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    API changeNeeds DiscussionRFCtopic: Manipulation

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions