ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Wed, 20 Jul 2016 15:40:01 +0200Singular objects break after using in Sagehttps://ask.sagemath.org/question/34142/singular-objects-break-after-using-in-sage/ I don't understand why this code breaks:
R1 = singular.ring('(0,a)','(x,y)','dp')
f1 = singular('x^2')
g1 = singular('x')
fsage = f1.sage()
gsage = g1.sage()
print(f1)
print(g1)
print(fsage)
print(gsage)
g2 = fsage.reduce(gsage)
print(g2)
print(f1)
It gives this result:
x^2
x
x^2
x
0
Traceback (most recent call last): print(g1)
File "", line 1, in <module>
File "/tmp/tmpO7_2ph/___code___.py", line 13, in <module>
exec compile(u'print(f1)
File "", line 1, in <module>
File "/home/sage/sage-7.2/local/lib/python2.7/site-packages/sage/interfaces/singular.py", line 1318, in __repr__
elif self.type() == 'matrix':
File "/home/sage/sage-7.2/local/lib/python2.7/site-packages/sage/interfaces/singular.py", line 2059, in type
return m.group(1)
AttributeError: 'NoneType' object has no attribute 'group'
f1 prints fine the first time, but after running the reduce, it seems like it no longer exists. I'm trying to use f1 for other things after doing a reduce, but can't seem to find any way to do it.Wed, 20 Jul 2016 00:32:09 +0200https://ask.sagemath.org/question/34142/singular-objects-break-after-using-in-sage/Answer by nbruin for <p>I don't understand why this code breaks:</p>
<pre><code>R1 = singular.ring('(0,a)','(x,y)','dp')
f1 = singular('x^2')
g1 = singular('x')
fsage = f1.sage()
gsage = g1.sage()
print(f1)
print(g1)
print(fsage)
print(gsage)
g2 = fsage.reduce(gsage)
print(g2)
print(f1)
</code></pre>
<p>It gives this result:</p>
<pre><code>x^2
x
x^2
x
0
Traceback (most recent call last): print(g1)
File "", line 1, in <module>
File "/tmp/tmpO7_2ph/___code___.py", line 13, in <module>
exec compile(u'print(f1)
File "", line 1, in <module>
File "/home/sage/sage-7.2/local/lib/python2.7/site-packages/sage/interfaces/singular.py", line 1318, in __repr__
elif self.type() == 'matrix':
File "/home/sage/sage-7.2/local/lib/python2.7/site-packages/sage/interfaces/singular.py", line 2059, in type
return m.group(1)
AttributeError: 'NoneType' object has no attribute 'group'
</code></pre>
<p>f1 prints fine the first time, but after running the reduce, it seems like it no longer exists. I'm trying to use f1 for other things after doing a reduce, but can't seem to find any way to do it.</p>
https://ask.sagemath.org/question/34142/singular-objects-break-after-using-in-sage/?answer=34145#post-id-34145It looks like the singular workspace gets clobbered. Peeling apart what happens (where the error occurs) indicates this:
sage: R1 = singular.ring('(0,a)','(x,y)','dp')
sage: f1 = singular('x^2')
sage: g1 = singular('x')
sage: fsage = f1.sage()
sage: gsage = g1.sage()
sage: f1.parent().eval("type(%s)"%f1.name())
'// sage8 poly x2'
sage: f1.parent().eval("type(%s)"%g1.name())
'// sage9 poly x'
sage: g2 = fsage.reduce(gsage)
sage: f1.parent().eval("type(%s)"%g1.name())
'// sage9 $INVALID$ `sage9`'
sage: f1.parent().eval("type(%s)"%f1.name())
'// sage8 $INVALID$ `sage8`'
My guess is that singular has global state: it has the concept of a current ring. A routine like "reduce" which calls singular has to set the current ring. It probably doesn't restore the previous current ring when it's done. Probably "reduce" should use libsingular.
A reasonable workaround is to define your own singular interface instance, which can keep its global state without getting clobbered by sage's interactions with its "own" singular interface instance:
sage: sng = Singular()
sage: R1 = sng.ring('(0,a)','(x,y)','dp')
sage: f1 = sng('x^2')
sage: g1 = sng('x')
sage: f1,g1
(x^2, x)
sage: g2 = fsage.reduce(gsage)
sage: f1,g1,g2
(x^2, x, 0)
Respecting that sage needs its own singular instance where it's free to clobber global state and defining your own if you need persistent singular global state is programmatically much cleaner, easier and efficient than trying to get sage to preserve/restore global state.Wed, 20 Jul 2016 07:51:27 +0200https://ask.sagemath.org/question/34142/singular-objects-break-after-using-in-sage/?answer=34145#post-id-34145Comment by visor841 for <p>It looks like the singular workspace gets clobbered. Peeling apart what happens (where the error occurs) indicates this:</p>
<pre><code>sage: R1 = singular.ring('(0,a)','(x,y)','dp')
sage: f1 = singular('x^2')
sage: g1 = singular('x')
sage: fsage = f1.sage()
sage: gsage = g1.sage()
sage: f1.parent().eval("type(%s)"%f1.name())
'// sage8 poly x2'
sage: f1.parent().eval("type(%s)"%g1.name())
'// sage9 poly x'
sage: g2 = fsage.reduce(gsage)
sage: f1.parent().eval("type(%s)"%g1.name())
'// sage9 $INVALID$ `sage9`'
sage: f1.parent().eval("type(%s)"%f1.name())
'// sage8 $INVALID$ `sage8`'
</code></pre>
<p>My guess is that singular has global state: it has the concept of a current ring. A routine like "reduce" which calls singular has to set the current ring. It probably doesn't restore the previous current ring when it's done. Probably "reduce" should use libsingular.</p>
<p>A reasonable workaround is to define your own singular interface instance, which can keep its global state without getting clobbered by sage's interactions with its "own" singular interface instance:</p>
<pre><code>sage: sng = Singular()
sage: R1 = sng.ring('(0,a)','(x,y)','dp')
sage: f1 = sng('x^2')
sage: g1 = sng('x')
sage: f1,g1
(x^2, x)
sage: g2 = fsage.reduce(gsage)
sage: f1,g1,g2
(x^2, x, 0)
</code></pre>
<p>Respecting that sage needs its own singular instance where it's free to clobber global state and defining your own if you need persistent singular global state is programmatically much cleaner, easier and efficient than trying to get sage to preserve/restore global state.</p>
https://ask.sagemath.org/question/34142/singular-objects-break-after-using-in-sage/?comment=34154#post-id-34154Ah, this makes so much sense. Defining my own singular interface instance is a much better idea. Thanks.Wed, 20 Jul 2016 15:40:01 +0200https://ask.sagemath.org/question/34142/singular-objects-break-after-using-in-sage/?comment=34154#post-id-34154