Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assume real root in limit #18363

Closed
proy87 opened this issue Jan 17, 2020 · 7 comments · Fixed by #21775
Closed

Assume real root in limit #18363

proy87 opened this issue Jan 17, 2020 · 7 comments · Fixed by #21775
Labels

Comments

@proy87
Copy link

proy87 commented Jan 17, 2020

Limit(((x - 6)**(Integer(1)/3) + 2)/(x + 2),x,-2,'-').doit() returns -oo*sign(2+2*(-1)^(1/3)). How to make it real-valued?

@gschintgen
Copy link
Contributor

gschintgen commented Jan 17, 2020

This is independent of limit.

You can use real_root:
https://docs.sympy.org/1.5.1/modules/functions/elementary.html#sympy.functions.elementary.miscellaneous.real_root

In [1]: limit((real_root(x - 6, 3) + 2)/(x + 2), x, -2, '-')
Out[1]: 1/12

@proy87
Copy link
Author

proy87 commented Jan 17, 2020

@gschintgen , got some weird answer for right-hand limit: limit((real_root(x - 6, 3) + 2)/(x + 2), x, -2, '+') is 2*Subs(Derivative(sign(_x - 8), _x), _x, 0) + 1/12

@gschintgen
Copy link
Contributor

Manually rewriting the root does the trick:

In [6]: l = Limit((-(6 - x)**(Integer(1)/3) + 2)/(x + 2),x,-2,'-'); l
Out[6]:
      ⎛    3 _______⎞
      ⎜2 - ╲╱ 6 - x ⎟
 lim  ⎜─────────────⎟
x─→-2⁻⎝    x + 2    ⎠

In [7]: _.doit()
Out[7]: 1/12

So the piecewise expression returned by real_root is not quite amenable to all operations, in particular derviatives and sign don't seem to mix well. For reference:

In [8]: real_root(x - 6, 3)
Out[8]:
⎧3 _________
⎪╲╱ │x - 6│ ⋅sign(x - 6)  for im(x) = 0
⎨
⎪       3 _______
⎩       ╲╱ x - 6            otherwise

I think the underlying issue is has to do with this: if x is real computing the diff is no problem:

In [16]: diff(sign(x-8))
Out[16]: 2⋅δ(x - 8)

In [17]: diff(sign(x-8)).subs(x, 0)
Out[17]: 0

But if I set x to be real before calling limit, this assumption is lost (or discarded on purpose?) when the Dummy in the diff.subs is created. And then we're left with the behavior for a general complex variable:

In [18]: diff(sign(y-8))
Out[18]:
d
──(sign(y - 8))
dy

In [19]: _.subs(y, 0)
Out[19]:
⎛d              ⎞│
⎜──(sign(y - 8))⎟│
⎝dy             ⎠│y=0

Since I'm not at all familiar with this part of the codebase that's as far as I'm going with this.

@gschintgen
Copy link
Contributor

Also noteworthy:

In [26]: var('x', real=True)
Out[26]: x

In [27]: l = Limit((real_root(x - 6, 3) + 2)/(x + 2), x, -2, '+'); l
Out[27]:
      ⎛3 _________                ⎞
      ⎜╲╱ │x - 6│ ⋅sign(x - 6) + 2⎟
 lim  ⎜───────────────────────────⎟
x─→-2⁺⎝           x + 2           ⎠

A simple substitution of x = -2 would reveal that both abs and sign could be simplified even before the actual limit computation. But substitution is a dangerous idea in a limit computation, so I'm not sure if this will lead anywhere...

@proy87
Copy link
Author

proy87 commented Jan 18, 2020

@gschintgen , thank you for the thorough answer.

@sachin-4099
Copy link
Contributor

@gschintgen @jksuom Is there anything left to handle in this issue?

@gschintgen
Copy link
Contributor

gschintgen commented May 31, 2020

@gschintgen @jksuom Is there anything left to handle in this issue?

Yes, this:

In [1]: limit((real_root(x - 6, 3) + 2)/(x + 2), x, -2, '+')
Out[1]:
  ⎛d              ⎞│      1
2⋅⎜──(sign(x - 8))⎟│    + ──
  ⎝dx             ⎠│x=0   12

The first term shouldn't be there: it equals zero.
(Edit: same output if x is explicitly created with real=True.)

0sidharth added a commit to 0sidharth/sympy that referenced this issue Jul 24, 2021
Add._eval_as_leading_term piecewise_folds Piecewise expressions (sympy#18363)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants