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

Commit

Permalink
12967: get relations with infinities right
Browse files Browse the repository at this point in the history
  • Loading branch information
rwst committed Mar 19, 2015
1 parent 544450e commit c5845f6
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 15 deletions.
12 changes: 4 additions & 8 deletions src/sage/functions/orthogonal_polys.py
Original file line number Diff line number Diff line change
Expand Up @@ -549,8 +549,8 @@ def _eval_(self, n, x):
sage: chebyshev_T(100001/2, 2)
doctest:...: RuntimeWarning: mpmath failed, keeping expression unevaluated
chebyshev_T(100001/2, 2)
sage: chebyshev_U._eval_(1.5, Mod(8,9)) is None
True
sage: chebyshev_U._eval_(1.5, Mod(8,9))
63.8753125671848
"""
# n is an integer => evaluate algebraically (as polynomial)
if n in ZZ:
Expand Down Expand Up @@ -669,9 +669,7 @@ def _evalf_(self, n, x, **kwds):
sage: chebyshev_T(1/2, 3/2)
1.11803398874989
sage: chebyshev_T._evalf_(1.5, Mod(8,9))
Traceback (most recent call last):
...
TypeError: cannot evaluate chebyshev_T with parent Ring of integers modulo 9
31.8198051533946
This simply evaluates using :class:`RealField` or :class:`ComplexField`::
Expand Down Expand Up @@ -1049,9 +1047,7 @@ def _evalf_(self, n, x, **kwds):
sage: chebyshev_U(10,3).n(75)
4.661117900000000000000e7
sage: chebyshev_U._evalf_(1.5, Mod(8,9))
Traceback (most recent call last):
...
TypeError: cannot evaluate chebyshev_U with parent Ring of integers modulo 9
63.8753125671848
"""
try:
real_parent = kwds['parent']
Expand Down
62 changes: 55 additions & 7 deletions src/sage/symbolic/expression.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -2161,7 +2161,10 @@ cdef class Expression(CommutativeRingElement):
sage: assert( SR(-oo) <= SR(oo) )
sage: assert( SR(oo) >= SR(-oo) )
sage: assert( SR(oo) != SR(-oo) )
sage: assert( sqrt(2)*oo != I*oo )
sage: bool( sqrt(2)*oo != I*oo )
Traceback (most recent call last):
...
ValueError: infinite but not with +/- phase
The expression may be zero with integers but is not
when in the complex domain (:trac:`15571`)::
Expand All @@ -2178,16 +2181,61 @@ cdef class Expression(CommutativeRingElement):
sage: expr.is_zero()
False
sage: forget()
Treat constants, wrapped pyobjects, infinity correctly (:trac:`12967`)::
sage: assert( pi < Infinity )
sage: assert( pi > -Infinity )
sage: assert( SR(pi) < Infinity )
sage: assert( SR(pi) > -Infinity )
sage: assert( SR(2) < Infinity )
sage: assert( SR(2) > -Infinity )
sage: assert( SR(2) < SR(pi) )
sage: assert( sqrt(2) < Infinity )
sage: assert( sqrt(2) > -Infinity )
"""
if self.is_relational():
# constants are wrappers around Sage objects, compare directly
if is_a_constant(self._gobj.lhs()) and is_a_constant(self._gobj.rhs()):
return self.operator()(self.lhs().pyobject(), self.rhs().pyobject())
lhs = self.lhs()
rhs = self.rhs()
lhs_is_numeric = rhs_is_numeric = False
if lhs.is_constant():
lhs = lhs.n()
lhs_is_numeric = True
elif lhs.is_numeric():
lhs = lhs.pyobject()
lhs_is_numeric = True
if rhs.is_constant():
rhs = rhs.n()
rhs_is_numeric = True
elif rhs.is_numeric():
rhs = rhs.pyobject()
rhs_is_numeric = True

if lhs_is_numeric and rhs_is_numeric:
return self.operator()(lhs, rhs)

from sage.rings.infinity import InfinityRing
P = InfinityRing
lhs_is_infinity = rhs_is_infinity = False
if hasattr(lhs, 'is_infinity') and lhs.is_infinity():
try:
_ = rhs.n()
except (TypeError, AttributeError, ValueError):
pass
else:
return self.operator()(P(lhs), P(rhs))
lhs_is_infinity = True
if hasattr(rhs, 'is_infinity') and rhs.is_infinity():
try:
_ = lhs.n()
except (TypeError, AttributeError, ValueError):
pass
else:
return self.operator()(P(lhs), P(rhs))
rhs_is_infinity = True

pynac_result = relational_to_bool(self._gobj)

# pynac is guaranteed to give the correct answer for comparing infinities
if is_a_infinity(self._gobj.lhs()) or is_a_infinity(self._gobj.rhs()):
if lhs_is_infinity or rhs_is_infinity:
return pynac_result

if pynac_result:
Expand Down

0 comments on commit c5845f6

Please sign in to comment.