Skip to content

Commit

Permalink
series: support Piecewise expressions in mrv()
Browse files Browse the repository at this point in the history
  • Loading branch information
skirpichev committed Mar 20, 2022
1 parent 59ae3b6 commit 6fa4f2d
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
10 changes: 10 additions & 0 deletions diofant/series/gruntz.py
Expand Up @@ -134,6 +134,16 @@ def mrv(e, x):
return mrv(e.base, x)
elif isinstance(e, log):
return mrv(e.args[0], x)
elif e.is_Piecewise:
for a, c in e.args:
if not c.is_Atom:
c = c.func(c.lhs - c.rhs)
c = c.func(limitinf(c.lhs, x))
if not c.is_Atom:
raise NotImplementedError("Parametric limits aren't supported yet.")
if c:
break
return mrv(a, x)
elif e.is_Function and not isinstance(e.func, UndefinedFunction):
return functools.reduce(lambda a, b: mrv_max(a, b, x),
[mrv(a, x) for a in e.args])
Expand Down
13 changes: 13 additions & 0 deletions diofant/tests/series/test_limits.py
Expand Up @@ -99,6 +99,19 @@ def test_basic4():
e = ((n**(n + 1) + (n + 1)**n)/n**(n + 1))**n
assert limit(e, n, oo) == E**E

# issue sympy/sympy#18492
e1 = 2*sqrt(x)*Piecewise(((4*x - 2)/abs(sqrt(4 - 4*(2*x - 1)**2)),
4*x - 2 >= 0),
((2 - 4*x)/abs(sqrt(4 - 4*(2*x - 1)**2)), True))
e2 = Piecewise((x**2/2, x <= Rational(1, 2)), (x/2 - Rational(1, 8), True))
e3 = Piecewise(((x - 9)/5, x < -1), ((x - 9)/5, x > 4),
(sqrt(abs(x - 3)), True))
assert limit(e1, x, 0) == 1
assert limit(e2, x, 0) == 0
assert limit(e2, x, oo) == oo
assert limit(e3, x, -1) == 2
assert limit(e3, x, oo) == oo


def test_basic5():
class MyFunction(Function):
Expand Down
3 changes: 3 additions & 0 deletions docs/release/notes-0.14.rst
Expand Up @@ -7,6 +7,8 @@ Not Released Yet
New features
============

* Support calculating limits with :class:`~diofant.functions.elementary.piecewise.Piecewise` functions, see :pull:`1214`.

Major changes
=============

Expand Down Expand Up @@ -67,3 +69,4 @@ These Sympy issues also were addressed:
* :sympyissue:`14387`: Tutorial on limits creates impression that they are two-sided by default
* :sympyissue:`8166`: Limit assumes function is continuous?
* :sympyissue:`14502`: Problem with limit including factorial.
* :sympyissue:`18492`: Limit of Piecewise function - NotImplementedError: Don't know how to calculate the mrv

0 comments on commit 6fa4f2d

Please sign in to comment.