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

Commit

Permalink
24440: Infinite loop from converting to QQbar
Browse files Browse the repository at this point in the history
  • Loading branch information
rwst committed Feb 9, 2018
1 parent 0a674fd commit 39bfd96
Showing 1 changed file with 26 additions and 4 deletions.
30 changes: 26 additions & 4 deletions src/sage/symbolic/expression_conversions.py
Original file line number Diff line number Diff line change
Expand Up @@ -956,13 +956,28 @@ def composition(self, ex, operator):
sage: a.composition(exp(pi*I*RR(1), hold=True), exp)
Traceback (most recent call last):
...
TypeError: no canonical coercion from Real Field with 53 bits of precision to Rational Field
TypeError: unable to convert e^(1.00000000000000*I*pi) to Algebraic Field
sage: a.composition(exp(pi*CC.gen(), hold=True), exp)
Traceback (most recent call last):
...
TypeError: no canonical coercion from Real Field with 53 bits of precision to Rational Field
TypeError: unable to convert e^(1.00000000000000*I*pi) to Algebraic Field
sage: bool(sin(pi*RR("0.7000000000000002")) > 0)
True
Check that :trac:`24440` is fixed::
sage: QQbar(tanh(pi + 0.1))
Traceback (most recent call last):
...
ValueError: unable to represent as an algebraic number
sage: QQbar(sin(I*pi/7))
Traceback (most recent call last):
...
ValueError: unable to represent as an algebraic number
sage: QQbar(sin(I*pi/7, hold=True))
Traceback (most recent call last):
...
ValueError: unable to represent as an algebraic number
"""
func = operator
operand, = ex.operands()
Expand All @@ -971,11 +986,16 @@ def composition(self, ex, operator):
# Note that comparing functions themselves goes via maxima, and is SLOW
func_name = repr(func)
if func_name == 'exp':
if operand.real():
if operand.is_trivial_zero():
return self.field(1)
if not (SR(-1).sqrt()*operand).is_real():
raise ValueError("unable to represent as an algebraic number")
# Coerce (not convert, see #22571) arg to a rational
arg = operand.imag()/(2*ex.parent().pi())
rat_arg = QQ.coerce(arg.pyobject())
try:
rat_arg = QQ.coerce(arg.pyobject())
except TypeError:
raise TypeError("unable to convert %r to %s"%(ex, self.field))
res = QQbar.zeta(rat_arg.denom())**rat_arg.numer()
elif func_name in ['sin', 'cos', 'tan']:
exp_ia = exp(SR(-1).sqrt()*operand)._algebraic_(QQbar)
Expand All @@ -986,6 +1006,8 @@ def composition(self, ex, operator):
else:
res = -QQbar.zeta(4)*(exp_ia - ~exp_ia)/(exp_ia + ~exp_ia)
elif func_name in ['sinh', 'cosh', 'tanh']:
if not (SR(-1).sqrt()*operand).is_real():
raise ValueError("unable to represent as an algebraic number")
exp_a = exp(operand)._algebraic_(QQbar)
if func_name == 'sinh':
res = (exp_a - ~exp_a)/2
Expand Down

0 comments on commit 39bfd96

Please sign in to comment.