# sympy.limit yields incorrect result #9075

Closed
opened this Issue Feb 28, 2015 · 5 comments

Projects
None yet
5 participants
Contributor

### cstoudt commented Feb 28, 2015

 Python 2.7.9; SymPy 0.7.6 Consider the function `g=(6**(n+1)+n+1)/(6**n+n)` where x is real and n is an integer It is easy to show that the limit of g as n->oo is 6 Yet, look at the output of the following code: ``````from sympy import symbols, limit, oo, Pow n=symbols('n',integer=True) x=symbols('x',real=True) g=(Pow(6,n+1)+n+1)/(Pow(6,n)+n) limit(g,n,oo) `````` out[4]: 1
Member

### smichr commented Mar 1, 2015

 Interestingly, if you make n positive ``````>>> n=symbols('n',integer=True,positive=True) >>> limit(g,n,oo) (6**(n + 1) + n + 1)/(6**n + n) ``````
Contributor

### raoulb commented Mar 1, 2015

 Consider the function `g=(6**(n+1)+n+1)/(6**n+n)` where x is real and n is an integer Some heuristics seem to fail here. Use the superior Gruntz algorithm: ``` In [9]: g Out[9]: (6**(n + 1) + n + 1)/(6**n + n) In [10]: gruntz(g, n, oo) Out[10]: 6 ```
Contributor

### cstoudt commented Mar 1, 2015

 According to the documentation, limit uses some heuristics to solve the "easy" cases for speed, and uses the Gruntz algorithm for all other cases. So something is failing in the heuristics. Interestingly, gruntz also fails for the case that smichr pointed out: ``````>>> from sympy.series import gruntz >>> n=symbols('n',integer=True,positive=True) >>> gruntz(g, n, oo) (6**(n + 1) + n + 1)/(6**n + n) ``````
Contributor

### zanzibar7 commented Mar 3, 2015

 Bug traces back to atleast 0.7.2. Also in a reduced version... ``````In [3]: limit( (6**(n+1)+1)/(6**n+1), n, oo) Out[3]: 1 `````` Looks like the heuristic is equating `n+1` and `n` when n is large.

Open

Member

### leosartaj commented Mar 20, 2015

 On some debugging, This is due to incorrect result by inve.as_leading_term(). It returns `1` rather than `6`. This is because in general ``````>>> (6**(n+1))._eval_leading_term(n) 1 # incorrect >>>(6*6**n)._eval_leading_term(n) 6 # correct `````` This ultimately boils down to `_eval_leading_term` method in `Pow`. I am working on this and will send a pull request shortly.

### leosartaj added a commit to leosartaj/sympy that referenced this issue Mar 20, 2015

``` fixes sympy#9075: sympy.limit yields incorrect result ```
``` 61987fc ```

Closed

### skirpichev added a commit to diofant/diofant that referenced this issue Feb 20, 2016

``` Removed is_Mul heuristics in Limit.doit() ```
```This patch introduces a performance regression (very
non-trivial to fix), but given the number of bugs fixed and
that old heuristics was completely wrong - I believe this

Closes sympy/sympy#10610
Closes sympy/sympy#9075
(I don't think this is all, but who cares.)```
``` 7e233e2 ```

### Upabjojr added a commit to Upabjojr/sympy that referenced this issue Oct 3, 2017

``` Removed is_Mul heuristics in Limit.doit() ```
```This patch introduces a performance regression (very
non-trivial to fix), but given the number of bugs fixed and
that old heuristics was completely wrong - I believe this
``` e64e3f0 ```