Skip to content

Keep single-row and single-column matrices as matrices in Transpose - #2007

Open
tombouctou wants to merge 7 commits into
Mathics3:masterfrom
tombouctou:fix-transpose-row-matrix
Open

tombouctou wants to merge 7 commits into
Mathics3:masterfrom
tombouctou:fix-transpose-row-matrix

Conversation

@tombouctou

Copy link
Copy Markdown
Collaborator

Transpose turned a matrix with a single row into a vector:

Transpose[{{1, 2, 3}}]          (* gave {1, 2, 3}, WMA gives {{1}, {2}, {3}} *)
Dimensions[Transpose[{{1, 2}}]] (* gave {2}, should be {2, 1} *)

The 2D case went through a SymPy matrix, and from_sympy_matrix returns any result with one column as a flat vector. This changes the 2D case of eval_Transpose to rearrange the elements directly, which also leaves them as they are instead of converting them to SymPy and back. The 3D case is unchanged.

The same flattening affects other functions that return a SymPy matrix, e.g. Inverse[{{2}}] gives {1/2} and RowReduce[{{1}, {2}}] gives {1, 0}; that is reported separately in #2006.

Tests: new cases in test/builtin/test_tensor.py. pytest and the doctests give the same results as on master, apart from environmental failures that also fail on master.

🤖 Generated with Claude Code

Transpose[{{1, 2, 3}}] gave {1, 2, 3} instead of {{1}, {2}, {3}}:
the 2D case went through a SymPy matrix, and from_sympy_matrix turns
any one-column result into a vector. Transpose a 2D list by moving its
elements directly instead, which also leaves them untouched.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@mmatera

mmatera commented Oct 16, 2026

Copy link
Copy Markdown
Contributor

LGTM

Comment thread test/builtin/test_tensor.py Outdated
Comment thread test/builtin/test_tensor.py Outdated
Comment thread test/builtin/test_tensor.py Outdated
Comment thread test/builtin/test_tensor.py Outdated
rocky added 2 commits October 16, 2026 10:44
Co-authored-by: R. Bernstein <rocky@users.noreply.github.com>
Add assert failure messages to new tests.

Co-authored-by: R. Bernstein <rocky@users.noreply.github.com>
Comment thread test/builtin/test_tensor.py Outdated
Co-authored-by: R. Bernstein <rocky@users.noreply.github.com>
Comment thread test/builtin/test_tensor.py Outdated
@rocky

rocky commented Oct 16, 2026 •

Copy link
Copy Markdown
Member

@tombouctou I am having problems editing the commit using the GitHub web interface. Would you run "black" on the revised code, please?

A couple of things. First, please let's add assert fail messages on asserts rather than keeping these None. (Please tell Claude for me.)

I've added an invitation to join this repository so you can make branches here rather than under the forked tombuctou repository. Making branches here will make it easier to suggest changes. What we often do is create a branch of the PR and then submit a PR to the PR. That way, you can remove and modify any suggested changes while I can use tools that are more black, isort, and mypy-friendly. Thanks.

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.

3 participants