Skip to content
This repository has been archived by the owner on Jan 30, 2023. It is now read-only.

Commit

Permalink
correct handling of parent in __invert__
Browse files Browse the repository at this point in the history
  • Loading branch information
dkrenn committed Aug 28, 2015
1 parent 92861ed commit 18bc9e8
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 15 deletions.
27 changes: 20 additions & 7 deletions src/sage/rings/asymptotic/asymptotic_ring.py
Original file line number Diff line number Diff line change
Expand Up @@ -910,18 +910,21 @@ def __invert__(self):
sage: A.<a> = AsymptoticRing(growth_group='a^ZZ', coefficient_ring=ZZ)
sage: (1 / a).parent()
Asymptotic Ring <a^ZZ> over Integer Ring
Asymptotic Ring <a^ZZ> over Rational Field
sage: (a / 2).parent()
Asymptotic Ring <a^ZZ> over Rational Field
"""
if len(self.summands) == 0:
raise ZeroDivisionError('Division by zero in %s.' % (self,))

elif len(self.summands) == 1:
new_element = ~next(self.summands.elements())
try:
element = next(self.summands.elements())
new_element = ~element
if new_element.parent() is element.parent():
return self.parent()(new_element)
except (ValueError, TypeError):
else:
# Insert an 'if' here once terms can have different
# coefficient rings, as this will be for L-terms.
new_parent = self.parent().change_parameter(
growth_group=new_element.parent().growth_group,
coefficient_ring=new_element.parent().coefficient_ring)
Expand All @@ -931,11 +934,21 @@ def __invert__(self):
if len(max_elem) != 1:
raise ValueError('Expression %s cannot be inverted, since there '
'are several maximal elements: %s.' % (self, max_elem))

max_elem = max_elem[0]

imax_elem = ~max_elem
one = self.parent().one()
geom = one - self._mul_term_(imax_elem)
if imax_elem.parent() is max_elem.parent():
new_self = self
else:
# Insert an 'if' here once terms can have different
# coefficient rings, as this will be for L-terms.
new_parent = self.parent().change_parameter(
growth_group=imax_elem.parent().growth_group,
coefficient_ring=imax_elem.parent().coefficient_ring)
new_self = new_parent(self)

one = new_self.parent().one()
geom = one - new_self._mul_term_(imax_elem)

expanding = True
result = one
Expand Down
13 changes: 9 additions & 4 deletions src/sage/rings/asymptotic/growth_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -2078,9 +2078,9 @@ def __invert__(self):
True
"""
new_element = -self.exponent
try:
if new_element.parent() is self.exponent.parent():
return self.parent()(raw_element=new_element)
except (ValueError, TypeError):
else:
new_parent = self.parent().__class__(new_element.parent(),
self.parent()._var_)
return new_parent(raw_element=new_element)
Expand Down Expand Up @@ -2627,11 +2627,16 @@ def __invert__(self):
True
sage: e2.parent()
Growth Group QQ^x
::
sage: (~P(raw_element=1)).parent()
Growth Group QQ^x
"""
new_element = 1 / self.base
try:
if new_element.parent() is self.base.parent():
return self.parent()(raw_element=new_element)
except (ValueError, TypeError):
else:
new_parent = self.parent().__class__(new_element.parent(),
self.parent()._var_)
return new_parent(raw_element=new_element)
Expand Down
5 changes: 3 additions & 2 deletions src/sage/rings/asymptotic/growth_group_cartesian.py
Original file line number Diff line number Diff line change
Expand Up @@ -878,9 +878,10 @@ def __invert__(self):
Growth Group QQ^x * x^ZZ
"""
new_element = tuple(~x for x in self.cartesian_factors())
try:
if all(n.parent() is x.parent()
for n, x in zip(new_element, self.cartesian_factors())):
return self.parent()(new_element)
except (ValueError, TypeError):
else:
from sage.categories.cartesian_product import cartesian_product
new_parent = cartesian_product(
tuple(x.parent() for x in new_element))
Expand Down
4 changes: 2 additions & 2 deletions src/sage/rings/asymptotic/term_monoid.py
Original file line number Diff line number Diff line change
Expand Up @@ -2285,9 +2285,9 @@ def __invert__(self):
raise ZeroDivisionError('Cannot invert %s since its coefficient %s '
'cannot be inverted.' % (self, self.coefficient))
g = ~self.growth
try:
if c.parent() is self.coefficient.parent() and g.parent() is self.growth.parent():
return self.parent()(g, c)
except (ValueError, TypeError):
else:
new_parent = self.parent().__class__(g.parent(), c.parent())
return new_parent(g, c)

Expand Down

0 comments on commit 18bc9e8

Please sign in to comment.