# Bug when testing if a matrix is in SR On sagemath 8.9, I find the behavior of SR with matrix a bit strange.

sage: M = matrix([[0, -1], [1, 0]])
sage: print(SR(M)+M)
[[ 0 -1]
[ 1  0]              -1]
[              1 [ 0 -1]
[ 1  0]]


What is the use of coercing a matrix to the symbolic ring if it cannot be used as a matrix afterward?

These two lines of code also both raise an attribute error:

sage: SR(M) == M
sage: M in SR
AttributeError: 'ComplexIntervalField_class_with_category' object has no attribute 'complex_field'


By itself, it is not very relevant, but some element constructors (e.g. lie_algebra) contain a line

if x in self.base_ring():
do something


which means SR cannot be used as a base ring.

Is this a known bug?

A workaround would be to add a single line in SR.__contains__ to return false when x is matrix (or maybe there are case when a matrix is considered in SR, I don't know), but this would not solve the underlying issues.

edit retag close merge delete

Sort by » oldest newest most voted Use M.change_ring(SR) to change the entries of M to lie in the symbolic ring. Note that M.change_ring(SR) will still be a matrix. On the other hand, SR(M) is not a matrix. For most uses, I think that M.change_ring(SR) is the right choice.

sage: M = matrix([[0, -1], [1, 0]])
sage: type(SR(M))
<class 'sage.symbolic.expression.Expression'>
sage: type(M.change_ring(SR))
<class 'sage.matrix.matrix_symbolic_dense.Matrix_symbolic_dense'>
sage: M.change_ring(SR) == M
True
sage: M.change_ring(SR) + M
[ 0 -2]
[ 2  0]

more

1

I am even surprised that SR(M) does not throw any error. What kind of beast is this matrix in the Symbolic RIng?