Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Replace Float's with Rational's early in the limit code
Limit algorithms (i.e. Gruntz) doesn't work well with
non-exact numbers.

TODO: Perhaps, it's a user task to convert them to
Rationals/whatever and it's better to raise an exception
instead of this conversion.

Fixes diofant#296
Fixes sympy/sympy#11270

Few outdated comments in test_compute_leading_term are removed.

This also closes sympy/sympy#5383 (regression test added).
  • Loading branch information
skirpichev committed Jun 23, 2016
1 parent 5fb963b commit 76e0913
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 9 deletions.
3 changes: 2 additions & 1 deletion sympy/series/gruntz.py
Expand Up @@ -67,7 +67,7 @@

from functools import reduce

from sympy.core import S, Dummy, Mul, Add, evaluate
from sympy.core import S, Dummy, Mul, Add, evaluate, Float
from sympy.core.compatibility import default_sort_key
from sympy.functions import log, exp, sign as sgn
from sympy.core.cache import cacheit
Expand Down Expand Up @@ -217,6 +217,7 @@ def limitinf(e, x):
oo
"""
assert x.is_real and x.is_positive
assert not e.has(Float)

# Rewrite e in terms of tractable functions only:
e = e.rewrite('tractable', deep=True)
Expand Down
10 changes: 9 additions & 1 deletion sympy/series/limits.py
@@ -1,4 +1,4 @@
from sympy.core import S, Symbol, sympify, Expr, PoleError
from sympy.core import S, Symbol, sympify, Expr, Rational, Float, PoleError
from sympy.core.symbol import Dummy
from sympy.functions.elementary.trigonometric import sin, cos
from sympy.series.order import Order
Expand Down Expand Up @@ -145,6 +145,11 @@ def doit(self, **hints):

use_heuristics = hints.get('heuristics', True)

has_Floats = e.has(Float)
if has_Floats:
e = e.subs({k: Rational(k) for k in e.atoms(Float)},
simultaneous=True)

if z0.has(z):
newz = z.as_dummy()
r = limit(e.subs(z, newz), newz, z0, dir)
Expand Down Expand Up @@ -193,4 +198,7 @@ def doit(self, **hints):
if r is None:
return self

if has_Floats:
r = r.evalf()

return r
22 changes: 15 additions & 7 deletions sympy/series/tests/test_limits.py
Expand Up @@ -4,9 +4,9 @@
import pytest

from sympy import (limit, exp, oo, log, sqrt, Limit, sin, floor, cos,
acos, ceiling, atan, gamma, Symbol, S, pi, Integral,
acos, ceiling, atan, gamma, Symbol, S, pi, E, Integral,
cot, Rational, I, tan, cot, integrate, Sum, sign,
Function, subfactorial, PoleError, Integer)
Function, subfactorial, PoleError, Integer, Float)
from sympy.series.limits import heuristics
from sympy.series.order import O

Expand Down Expand Up @@ -237,10 +237,10 @@ def test_issue_4547():

def test_issue_5164():
assert limit(x**0.5, x, oo) == oo**0.5 == oo
assert limit(x**0.5, x, 16) == Integer(2)**2.0
assert limit(x**0.5, x, 16) == 4.0
assert limit(x**0.5, x, 0) == 0
assert limit(x**(-0.5), x, oo) == 0
assert limit(x**(-0.5), x, 4) == Integer(2)**(-1.0)
assert limit(x**(-0.5), x, 4) == 0.5


def test_issue_5183():
Expand Down Expand Up @@ -295,10 +295,8 @@ def test_issue_3934():


def test_compute_leading_term():
# needs series to go to n = 32
assert limit(x**Rational(77, 3)/(1 + x**Rational(77, 3)), x, oo) == 1
# needs series to go to n = 128
assert limit(x**101.1/(1 + x**101.1), x, oo) == 1
assert limit(x**Rational('101.1')/(1 + x**Rational('101.1')), x, oo) == 1


def test_issue_5955():
Expand Down Expand Up @@ -461,3 +459,13 @@ def test_issue_8634():

def test_issue_9558():
assert limit(sin(x)**15, x, 0, '-') == 0 # should be fast


def test_diofantissue_296():
e = log(exp(1/x)/Float(2) + exp(-1/x)/2)*x**2
assert e.limit(x, oo) == 0.5


def test_issue_5383():
e = (1.0 + 1.0*x)**(1.0/x)
assert e.limit(x, 0) == E.n()

0 comments on commit 76e0913

Please sign in to comment.