Skip to content

Transpose exchanges the first two levels of any array - #2090

Open
tombouctou wants to merge 4 commits into
masterfrom
fix-transpose-deep
Open

tombouctou wants to merge 4 commits into
masterfrom
fix-transpose-deep

Conversation

@tombouctou

Copy link
Copy Markdown
Collaborator

Found while running the examples of Skiena and Pemmaraju's Computational Discrete Mathematics with the Combinatorica package and comparing every result with WMA 15.

Transpose[m] left a list of rank 3 unevaluated unless all its dimensions were equal, and any list of rank 4 or more; rank 3 also went through SymPy. As in WMA, the first two levels are now exchanged, whatever lies below them.

This includes rows like {{{1, 2}}, {EdgeWeight -> 1}}, which Combinatorica's SetGraphOptions transposes. Because of this, SetEdgeWeights gave Part::partw messages, and Dijkstra and MinimumSpanningTree on weighted graphs hung.

Rows of different lengths, as in Transpose[{{1, 2}, {3}}], now give Transpose::nmtx, as in WMA.

All the fixes from this series together, merged on one branch: full pytest 4336 passed, 0 failed; doctests 4679 passed, 0 failed.

🤖 Generated with Claude Code

tombouctou and others added 3 commits October 30, 2026 11:11
Transpose[m] left a list of rank 3 unevaluated unless all its
dimensions were equal, and any list of rank 4 or more; rank 3 also went
through SymPy. As in WMA, the first two levels are now exchanged
whatever lies below them, as in the rows {{{1, 2}}, {a -> 1}} that
Combinatorica's SetGraphOptions transposes (SetEdgeWeights, Dijkstra).
Rows of different lengths give Transpose::nmtx.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
#> Clear[matrix, matrix3D, square]
"""

messages = {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Final please. Maybe there's a way to declare this in the class so we don't have to do all the duplication every time messages is set?

("The first two levels of {{1, 2}, {3}} cannot be transposed.",),
"Transpose[{{1, 2}, {3}}]",
None,
),

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe add a 4D test?

@rocky

rocky commented Oct 30, 2026

Copy link
Copy Markdown
Member

LGTM - decide whether you want to add a 4D test and add the Final type annotation.

Merge when you want.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants