Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversion from Sage matrix to sympy matrix fails for matrices with rows full of zeros #26596

Closed
AlbertoJimenezRuiz opened this issue May 14, 2024 · 1 comment · Fixed by #26605

Comments

@AlbertoJimenezRuiz
Copy link

Title: Conversion from Sage matrix to sympy matrix fails for matrices with rows full of zeros

Description: If a matrix created with the sage package features a row with only zeros, the constructor of ImmutableMatrix produces a matrix without those rows.

Steps to reproduce:
Run this code (needs sage):

from sage.all import Matrix as sage_matrix
from sympy.matrices import ImmutableMatrix

A=sage_matrix([[1,0],[0,0]])
# print(A)
rows_A=A.rows()
# print(rows_A)
matrix1 = ImmutableMatrix(rows_A)
print(matrix1)
matrix2 = ImmutableMatrix([(1, 0), (0, 0)])
print(matrix2)
  • Output:
Matrix([[1, 0]])
Matrix([[1, 0], [0, 0]])
  • Expected output:
Matrix([[1, 0], [0, 0]])
Matrix([[1, 0], [0, 0]])

The problem:
After investigating, the rows method of sage returns a list of sage.modules.vector_integer_dense.Vector_integer_dense. The vector_integer_dense class evaluates to false if all its elements are zero, while the python list type does so only if it is empty.

Possible fix:

The function _handle_creation_inputs of the class MatrixBase in the file sympy/sympy/matrices/matrixbase.py includes logic which discard rows that evaluate to false. Commenting out lines 3854-3855 fixes the issue.

class MatrixBase(Printable):
...
def _handle_creation_inputs(cls, *args, **kwargs):
...
    if not is_sequence(row) and \
            not getattr(row, 'is_Matrix', False):
        raise ValueError('expecting list of lists')

    if hasattr(row, '__array__'):
        if 0 in row.shape:
            continue
        #elif not row:
        #    continue

Workaround:

Converting the Vector_integer_dense as shown: rows_A = [list(x) for x in rows_A].

Tested Versions:

  • OS: Ubuntu 24.04 x86_64
  • python: Python 3.11.9 | packaged by conda-forge | (main, Apr 19 2024, 18:36:13) [GCC 12.3.0] on linux
  • sympy: 1.12
  • sage: 10.2
@oscarbenjamin
Copy link
Contributor

The _handle_creation_inputs function is an abomination. It would have been better not to overload the constructor so much.

I don't know why there is a check to skip a row. It seems to come from gh-16462.

Removing the two indicated lines does not seem to cause any test failures. I think it seems like a reasonable change.

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 a pull request may close this issue.

2 participants