Ask Your Question
1

Singular objects break after using in Sage

asked 2016-07-20 00:32:09 +0200

visor841 gravatar image

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.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2016-07-20 07:51:27 +0200

nbruin gravatar image

It 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.

edit flag offensive delete link more

Comments

Ah, this makes so much sense. Defining my own singular interface instance is a much better idea. Thanks.

visor841 gravatar imagevisor841 ( 2016-07-20 15:40:01 +0200 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2016-07-20 00:32:09 +0200

Seen: 193 times

Last updated: Jul 20 '16