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

Commit

Permalink
Merge branch 'asy/inversion' into asy/prototype
Browse files Browse the repository at this point in the history
* asy/inversion:
  adapt doc
  rewrite has_same_summands to use iterators efficiently
  term: is_same --> __eq__
  • Loading branch information
dkrenn committed Aug 28, 2015
2 parents d833544 + 51cfce6 commit ef4160e
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 65 deletions.
12 changes: 6 additions & 6 deletions src/sage/rings/asymptotic/asymptotic_ring.py
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,8 @@ def has_same_summands(self, other):
.. NOTE::
While for example ``O(x) == O(x)`` yields ``False``,
these expressions *do* have the same summands.
these expressions *do* have the same summands and this method
returns ``True``.
Moreover, this method uses the coercion model in order to
find a common parent for this asymptotic expression and
Expand All @@ -577,12 +578,10 @@ def has_same_summands(self, other):
False
"""
from sage.structure.element import have_same_parent

if have_same_parent(self, other):
return self._has_same_summands_(other)

from sage.structure.element import get_coercion_model

return get_coercion_model().bin_op(self, other,
lambda self, other:
self._has_same_summands_(other))
Expand Down Expand Up @@ -616,9 +615,10 @@ def _has_same_summands_(self, other):
"""
if len(self.summands) != len(other.summands):
return False
pairs = zip(self.summands.elements_topological(),
other.summands.elements_topological())
return all(p[0].is_same(p[1]) for p in pairs)
from itertools import izip
return all(s == o for s, o in
izip(self.summands.elements_topological(),
other.summands.elements_topological()))


def _simplify_(self):
Expand Down
93 changes: 34 additions & 59 deletions src/sage/rings/asymptotic/term_monoid.py
Original file line number Diff line number Diff line change
Expand Up @@ -801,9 +801,9 @@ def _le_(self, other):
return self.growth <= other.growth


def is_same(self, other):
def __eq__(self, other):
r"""
Return if this asymptotic term is the same as ``other``.
Return if this asymptotic term is equal to ``other``.
INPUT:
Expand All @@ -829,30 +829,28 @@ def is_same(self, other):
sage: g = GT.an_element(); e = ET.an_element(); o = OT.an_element()
sage: g, e, o
(Generic Term with growth x, x, O(x))
sage: g.is_same(g^2)
Traceback (most recent call last):
...
NotImplementedError: Only implemented in concrete realizations.
sage: e.is_same(e^2)
sage: g == g^2 # indirect doctest
False
sage: e == e^2 # indirect doctest
False
sage: e.is_same(ET(x,1))
sage: e == ET(x,1) # indirect doctest
True
sage: o.is_same(OT(x^2))
sage: o == OT(x^2) # indirect doctest
False
"""
from sage.structure.element import have_same_parent

if have_same_parent(self, other):
return self._is_same_(other)
return self._eq_(other)

from sage.structure.element import get_coercion_model

return get_coercion_model().bin_op(self, other,
lambda self, other:
self._is_same_(other))
import operator
try:
return get_coercion_model().bin_op(self, other, operator.eq)
except TypeError:
return False


def _is_same_(self, other):
def _eq_(self, other):
r"""
Return if this asymptotic term is the same as ``other``.
Expand All @@ -878,12 +876,21 @@ def _is_same_(self, other):
sage: from sage.rings.asymptotic.term_monoid import GenericTermMonoid
sage: T = GenericTermMonoid(GrowthGroup('x^ZZ'), QQ)
sage: t = T.an_element()
sage: t.is_same(t)
Traceback (most recent call last):
...
NotImplementedError: Only implemented in concrete realizations.
sage: t == t
True
::
sage: from sage.rings.asymptotic.term_monoid import OTermMonoid
sage: OT = OTermMonoid(GrowthGroup('x^ZZ'))
sage: t = OT.an_element(); t
O(x)
sage: t == OT(x) # indirect doctest
True
sage: t == OT(x^2) # indirect doctest
False
"""
raise NotImplementedError('Only implemented in concrete realizations.')
return self.growth == other.growth


def _repr_(self):
Expand Down Expand Up @@ -1616,39 +1623,6 @@ def _absorb_(self, other):
return self


def _is_same_(self, other):
r"""
Return if this :class:`OTerm` is the same as ``other``.
INPUT:
- ``other`` -- an :class:`OTerm`.
OUTPUT:
A boolean.
.. NOTE::
This method gets called by the coercion model, so it can
be assumed that this :class:`OTerm` and ``other`` come
from the same parent.
EXAMPLES::
sage: from sage.rings.asymptotic.growth_group import GrowthGroup
sage: from sage.rings.asymptotic.term_monoid import OTermMonoid
sage: OT = OTermMonoid(GrowthGroup('x^ZZ'), QQ)
sage: t = OT.an_element(); t
O(x)
sage: t.is_same(OT(x)) # indirect doctest
True
sage: t.is_same(OT(x^2)) # indirect doctest
False
"""
return self.growth == other.growth


class OTermMonoid(GenericTermMonoid):
r"""
Parent for asymptotic big `O`-terms.
Expand Down Expand Up @@ -1990,7 +1964,7 @@ def _le_(self, other):
return super(TermWithCoefficient, self)._le_(other)


def _is_same_(self, other):
def _eq_(self, other):
r"""
Return if this :class:`TermWithCoefficient` is the same as
``other``.
Expand All @@ -2016,14 +1990,15 @@ def _is_same_(self, other):
sage: T = TermWithCoefficientMonoid(GrowthGroup('x^ZZ'), ZZ)
sage: t = T.an_element(); t
Asymptotic Term with coefficient 1 and growth x
sage: t.is_same(T(x, 1))
sage: t == T(x, 1)
True
sage: t.is_same(T(x, 2))
sage: t == T(x, 2)
False
sage: t.is_same(T(x^2, 1))
sage: t == T(x^2, 1)
False
"""
return self.growth == other.growth and self.coefficient == other.coefficient
return super(TermWithCoefficient, self)._eq_(other) and \
self.coefficient == other.coefficient



Expand Down

0 comments on commit ef4160e

Please sign in to comment.