Repository navigation
Inverse, RowReduce, MatrixPower, ...: keep a one-column matrix a matrix - #2041
Merged
Merged
Conversation
from_sympy turns any SymPy matrix with one column into a vector. That is
right for a solution of a linear system, but Inverse, RowReduce,
MatrixPower, MatrixExp, PseudoInverse and QRDecomposition return matrices,
and lost a level whenever the result had one column:
Inverse[{{2}}] gave {1/2} instead of {{1/2}}
RowReduce[{{1}, {2}}] gave {1, 0} instead of {{1}, {0}}
MatrixPower[{{2}}, 3] gave {8} instead of {{8}}
PseudoInverse[{{1, 2}}] gave {1/5, 2/5} instead of {{1/5}, {2/5}}
These now convert their result row by row. LinearSolve and LeastSquares
still return vectors.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
rocky
reviewed
Oct 18, 2026
| ) | ||
|
|
||
|
|
||
| def from_sympy_matrix_as_matrix(matrix): |
Member
There was a problem hiding this comment.
This does not belong here, but instead belongs under mathics.core.convert
rocky
reviewed
Oct 18, 2026
| None, # "Sympy issue #1156", | ||
| ), | ||
| # A matrix with one column stays a matrix. | ||
| ("Inverse[{{2}}]", None, "{{1 / 2}}", None), |
Member
There was a problem hiding this comment.
Please enhance the final "None", parameter.
Address review: the shape-keeping SymPy-matrix conversion belongs with the other SymPy conversions, next to from_sympy_matrix, not in mathics.builtin.numbers.linalg. Give each new one-column test case a specific failure message instead of None. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Member
|
LGTM |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
from_sympy turns any SymPy matrix with one column into a vector. That is
right for a solution of a linear system, but Inverse, RowReduce,
MatrixPower, MatrixExp, PseudoInverse and QRDecomposition return matrices,
and lost a level whenever the result had one column:
These now convert their result row by row. LinearSolve and LeastSquares
still return vectors.
To reproduce on master:
Tests: new cases in
test/builtin/numbers/test_linalg.py(test_linalg). They fail on current master and pass with this change.The full pytest suite and the doctests pass with this change (run together with the other fixes from the same audit; the only pytest failures, in
test_main.pyandtest_returncode.py, are the same as on master and come from themathicsscript not being on PATH in our environment).Found in a correctness audit of Mathics3 (differential checks against SymPy/mpmath/exact computation, and calls of every builtin with edge-case arguments).
🤖 Generated with Claude Code