Repository: sympy/sympy
Pull request: Added tests for different values of k for limit(x**n - x**(n - k), x, oo)  
<!-- Your title above should be a short description of what
was changed. Do not include the issue number in the title. -->

#### References to other Issues or PRs
<!-- If this pull request fixes an issue, write "Fixes #NNNN" in that exact
format, e.g. "Fixes #1234" (see
https://tinyurl.com/auto-closing for more information). Also, please
write a comment on that issue linking back to this pull request once it is
open. -->
Closes #18176

#### Brief description of what is fixed or changed

Added Appropriate tests . The following limit wasn't working properly( all cases except `k=0`) when the issue was reported ( we used to get `NotImplementedError: Result depends on the sign of sign(n - k)` which was wrong ). This has been addressed somewhere else and is working properly. It gives the correct `NotImplementedError` now which is `Result depends on the sign of sign(k)`
```
>>> x = Symbol('x', real=True , positive= True)
>>> n = Symbol('n', integer=True, positive=True)
>>> k = Symbol('k')
>>> limit(x**n-x**(n-k),x,oo)
NotImplementedError: Result depends on the sign of sign(k)
>>> k = 1
>>> limit(x**n-x**(n-k), x, oo)
oo
>>> k = -2
>>> limit(x**n-x**(n-k), x, oo)
-oo
```

#### Other comments


#### Release Notes

<!-- Write the release notes for this release below between the BEGIN and END
statements. The basic format is a bulleted list with the name of the subpackage
and the release note for this PR. For example:

* solvers
  * Added a new solver for logarithmic equations.

* functions
  * Fixed a bug with log of integers.

or if no release note(s) should be included use:

NO ENTRY

See https://github.com/sympy/sympy/wiki/Writing-Release-Notes for more
information on how to write release notes. The bot will check your release
notes automatically to see if they are formatted correctly. -->

<!-- BEGIN RELEASE NOTES -->
NO ENTRY
<!-- END RELEASE NOTES -->


Problem statement:
Incorrect value for limit(x**n-x**(n-k),x,oo) when k is a natural number
```
In [64]: x = Symbol('x', real=True , positive= True)                                                     

In [65]: n = Symbol('n', integer=True, positive=True);                                                   

In [66]: k=0                                                                                             

In [67]: limit(x**n-x**(n-k),x,oo)                                                                       
Out[67]: 0

In [68]: k=1                                                                                             

In [69]: limit(x**n-x**(n-k),x,oo)                                                                       
---------------------------------------------------------------------------
NotImplementedError                       Traceback (most recent call last)
<ipython-input-69-e74c61d1f6e7> in <module>
----> 1 limit(x**n-x**(n-k),x,oo)

~/timepass/reee/gsoc/sympy/sympy/series/limits.py in limit(e, z, z0, dir)
     69     """
     70 
---> 71     return Limit(e, z, z0, dir).doit(deep=False)
     72 
     73 

~/timepass/reee/gsoc/sympy/sympy/series/limits.py in doit(self, **hints)
    259                             % (l, r))
    260             else:
--> 261                 r = gruntz(e, z, z0, dir)
    262             if r is S.NaN or l is S.NaN:
    263                 raise PoleError()

~/timepass/reee/gsoc/sympy/sympy/series/gruntz.py in gruntz(e, z, z0, dir)
    669 
    670     try:
--> 671         r = limitinf(e0, z)
    672     except ValueError:
    673         r = limitinf(e0, z, leadsimp=True)

~/timepass/reee/gsoc/sympy/sympy/series/gruntz.py in limitinf(e, x, leadsimp)
    448         if leadsimp:
    449             c0 = c0.simplify()
--> 450         return limitinf(c0, x, leadsimp)  # e0=0: lim f = lim c0
    451     else:
    452         raise ValueError("{} could not be evaluated".format(sig))

~/timepass/reee/gsoc/sympy/sympy/series/gruntz.py in limitinf(e, x, leadsimp)
    433         e = e.subs(x, p)
    434         x = p
--> 435     c0, e0 = mrv_leadterm(e, x)
    436     sig = sign(e0, x)
    437     if sig == 1:

~/timepass/reee/gsoc/sympy/sympy/series/gruntz.py in mrv_leadterm(e, x)
    521     #
    522     w = Dummy("w", real=True, positive=True, finite=True)
--> 523     f, logw = rewrite(exps, Omega, x, w)
    524     series = calculate_series(f, w, logx=logw)
    525     return series.leadterm(w)

~/timepass/reee/gsoc/sympy/sympy/series/gruntz.py in rewrite(e, Omega, x, wsym)
    593         sig = sign(g.args[0], x)
    594         if sig != 1 and sig != -1:
--> 595             raise NotImplementedError('Result depends on the sign of %s' % sig)
    596     if sig == 1:
    597         wsym = 1/wsym  # if g goes to oo, substitute 1/w

NotImplementedError: Result depends on the sign of sign(n - 1)

```

The answer should be `oo` right?

Hints from issue discussion:
This seems to be fixed on master , could close after adding relevant tests !
@sylee957 you could mark this as could close and easy to fix. Thank You
```
>>> k = Symbol('k')
>>> limit(x**n-x**(n-k),x,oo)
NotImplementedError: Result depends on the sign of sign(k)
>>> k = 1
>>> limit(x**n-x**(n-k), x, oo)
oo
>>> k = -2
>>> limit(x**n-x**(n-k), x, oo)
-oo
```

Commit message: Added Appropriate tests to close the issue

Patch to review:
diff --git a/sympy/series/tests/test_limits.py b/sympy/series/tests/test_limits.py
index 22201e479d45..28c16cb1cedd 100644
--- a/sympy/series/tests/test_limits.py
+++ b/sympy/series/tests/test_limits.py
@@ -880,6 +880,14 @@ def test_issue_18118():
     assert limit(sign(sin(x)), x, 0, "-") == -1
     assert limit(sign(sin(x)), x, 0, "+") == 1
 
+def test_issue_18176():
+    x = Symbol('x', real=True , positive= True)
+    n = Symbol('n', integer=True, positive=True)
+    k = 1
+    assert limit(x**n-x**(n-k), x, oo) == oo
+    k = -2
+    assert limit(x**n-x**(n-k), x, oo) == -oo
+
 
 def test_issue_18306():
     assert limit(sin(sqrt(x))/sqrt(sin(x)), x, 0, '+') == 1


Code review comments:
diff_hunk: @@ -880,6 +880,14 @@ def test_issue_18118():
     assert limit(sign(sin(x)), x, 0, "-") == -1
     assert limit(sign(sin(x)), x, 0, "+") == 1
 
+def test_issue_18176():
path: sympy/series/tests/test_limits.py
text: @user1:
These seem quite similar to #22230. Perhaps both of these can be mergeg and put into another appropriate test function (like `test_exponential`) rather than making new functions.

@author:
Thanks @user1, I had completely forgotten about the earlier Pr . Do we even need this Pr then both address similar issues ! Or maybe as that has been merged I could put these tests there and change the name of that function ?

diff_hunk: @@ -880,6 +880,14 @@ def test_issue_18118():
     assert limit(sign(sin(x)), x, 0, "-") == -1
     assert limit(sign(sin(x)), x, 0, "+") == 1
 
+def test_issue_18176():
+    x = Symbol('x', real=True , positive= True)
+    n = Symbol('n', integer=True, positive=True)
+    k = 1
+    assert limit(x**n-x**(n-k), x, oo) == oo
path: sympy/series/tests/test_limits.py
text: @user1:
`k` can directly be substituted here without a separate line for assigning value.

@author:
Yeah sure , I'll do the needful . I just thought that the issue was reported using k so I should go forward with using k explicitly !