Some bugs (?) in MPolynomialRing_polydict

0 views
Skip to first unread message

Simon King

unread,
Dec 7, 2009, 7:19:27 AM12/7/09
to sage-devel
Hi!

I tested some corner cases of MPolynomialRing_polydict and found the
following:

1.
sage: P.<x> = ZZ['x'][]
sage: P
Univariate Polynomial Ring in x over Univariate Polynomial Ring in x
over Integer Ring

I think that name conflicts should give rise to raising an error. Or
is there a reason for allowing coinciding names?

###########

2.
The code in test.py is as below. It defines something that to some
extent behaves like a ring (mathematically, it isn't a ring). Let's
try to construct a polynomial ring over the fake ring:

sage: attach test.py
sage: R.<a,b> = FakeRing(ZZ)
sage: 2*a
(2)*(a)
sage: P.<x,y> = PolynomialRing(R)
sage: a*x
(1)*(a)*x
sage: P(a)
a
sage: P('a')

---------------------------------------------------------------------------
TypeError Traceback (most recent
call last)

/home/king/SAGE/work/SymmetricIdeals/test2.py in <module>()

/home/king/SAGE/sage-4.1/local/lib/python2.6/site-packages/sage/
rings/polynomial /multi_polynomial_ring.pyc in __call__(self, x,
check)
437 return sage_eval(x, self.gens_dict())
438 except NameError, e:
--> 439 raise TypeError, "unable to convert string"
440
441 elif is_Macaulay2Element(x):

TypeError: unable to convert string
sage: P('1*x')

---------------------------------------------------------------------------
TypeError Traceback (most recent
call last)

/home/king/SAGE/work/SymmetricIdeals/test2.py in <module>()

/home/king/SAGE/sage-4.1/local/lib/python2.6/site-packages/sage/
rings/polynomial/multi_polynomial_ring.pyc in __call__(self, x, check)
437 return sage_eval(x, self.gens_dict())
438 except NameError, e:
--> 439 raise TypeError, "unable to convert string"
440
441 elif is_Macaulay2Element(x):

TypeError: unable to convert string

Shouldn't these conversions work?

Clearly the critical part of P.__call__ is:
...
elif isinstance(x, str):
try:
from sage.misc.sage_eval import sage_eval
return sage_eval(x, self.gens_dict())
except NameError, e:
raise TypeError, "unable to convert string"

The obvious problem is that 'a' is not traced in P.gens_dict()

I also find it rather odd that 'x' is not traced by its name but by
its string representation:
sage: P.gens_dict()
{'1*x': 1*x, '1*y': 1*y}
This is easy to fix:
sage: P._gens_dict = {'x':x, 'y':y}
sage: P('x')
1*x

Certainly the FakeRing class is nonsense. But I still think the fact
that P('a') and P('x') raise errors reveals two bugs.
Do you agree? Or do you think such nonsense rings are irrelevant?

Cheers,
Simon


########################


This is in test.py:
from sage.all import CommutativeRing, CommutativeRingElement,
SageObject

class FakeElement(CommutativeRingElement):
def __init__(self,parent,name):
self._name = name
CommutativeRingElement.__init__(self,parent)
def __repr__(self):
return self._name
def _cmp_(self,other):
return cmp(self._name,other._name)
def _add_(self,other):
return FakeElement(self.parent(), '('+self._name+')+
('+other._name+')')
def _mul_(self,other):
return FakeElement(self.parent(), '('+self._name+')*
('+other._name+')')
def _sub_(self,other):
return FakeElement(self.parent(), '('+self._name+')-
('+other._name+')')

class FakeRing(CommutativeRing):
def __init__(self, R, names):
CommutativeRing.__init__(self,base=R)
self._names = names
self._gens = [FakeElement(self, n) for n in names]
self._populate_coercion_lists_()
def __repr__(self):
return 'Fake ring over %s with generators %s'%(repr
(self.base_ring()),','.join(self._names))

def _element_constructor_(self, r):
return FakeElement(self, str(r))

def _coerce_map_from_(self, R):
return self.base_ring().has_coerce_map_from(R)

def an_element(self):
return self.gen()

def gen(self,i=None):
if i is not None:
return self._gens[i]
return self._gens[0]
Reply all
Reply to author
Forward
0 new messages