ASKSAGE: Sage Q&A Forum - Individual question feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Sat, 02 Nov 2019 12:47:04 -0500Bug when testing if a matrix is in SRhttps://ask.sagemath.org/question/48591/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.Sat, 02 Nov 2019 11:09:26 -0500https://ask.sagemath.org/question/48591/bug-when-testing-if-a-matrix-is-in-sr/Answer by John Palmieri for <p>On sagemath 8.9, I find the behavior of <code>SR</code> with matrix a bit strange.</p>
<pre><code>sage: M = matrix([[0, -1], [1, 0]])
sage: print(SR(M)+M)
[[ 0 -1]
[ 1 0] -1]
[ 1 [ 0 -1]
[ 1 0]]
</code></pre>
<p>What is the use of coercing a matrix to the symbolic ring if it cannot be used as a matrix afterward?</p>
<p>These two lines of code also both raise an attribute error:</p>
<pre><code>sage: SR(M) == M
sage: M in SR
AttributeError: 'ComplexIntervalField_class_with_category' object has no attribute 'complex_field'
</code></pre>
<p>By itself, it is not very relevant, but some element constructors (e.g. <code>lie_algebra</code>) contain a line </p>
<pre><code>if x in self.base_ring():
do something
</code></pre>
<p>which means <code>SR</code> cannot be used as a base ring.</p>
<p>Is this a known bug? </p>
<p>A workaround would be to add a single line in <code>SR.__contains__</code> to return false when <code>x</code> is matrix (or maybe there are case when a matrix is considered in <code>SR</code>, I don't know), but this would not solve the underlying issues.</p>
https://ask.sagemath.org/question/48591/bug-when-testing-if-a-matrix-is-in-sr/?answer=48594#post-id-48594Use `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]Sat, 02 Nov 2019 12:28:30 -0500https://ask.sagemath.org/question/48591/bug-when-testing-if-a-matrix-is-in-sr/?answer=48594#post-id-48594Comment by eric_g for <p>Use <code>M.change_ring(SR)</code> to change the entries of <code>M</code> to lie in the symbolic ring. Note that <code>M.change_ring(SR)</code> will still be a matrix. On the other hand, <code>SR(M)</code> is not a matrix. For most uses, I think that <code>M.change_ring(SR)</code> is the right choice.</p>
<pre><code>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]
</code></pre>
https://ask.sagemath.org/question/48591/bug-when-testing-if-a-matrix-is-in-sr/?comment=48596#post-id-48596I am even surprised that `SR(M)` does not throw any error. What kind of beast is this matrix in the Symbolic RIng?Sat, 02 Nov 2019 12:47:04 -0500https://ask.sagemath.org/question/48591/bug-when-testing-if-a-matrix-is-in-sr/?comment=48596#post-id-48596