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 19, 2022
1 parent 3568916 commit 15fe853
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 2 deletions.
4 changes: 2 additions & 2 deletions diofant/core/expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,7 @@ def is_constant(self, *wrt, **flags):
if a is None or a is nan:
# try random real
a = expr._random(None, -1, 0, 1, 0)
except ZeroDivisionError:
except (ZeroDivisionError, TypeError):
a = None
if a is not None and a is not nan:
try:
Expand All @@ -494,7 +494,7 @@ def is_constant(self, *wrt, **flags):
if b is nan:
# evaluation may succeed when substitution fails
b = expr._random(None, 1, 0, 1, 0)
except ZeroDivisionError:
except (ZeroDivisionError, TypeError):
b = None
if b is not None and b is not nan and b.equals(a) is False:
return False
Expand Down
8 changes: 8 additions & 0 deletions diofant/series/gruntz.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,14 @@ 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 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
11 changes: 11 additions & 0 deletions diofant/tests/series/test_limits.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,17 @@ 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(e3, x, -1) == 2


def test_basic5():
class MyFunction(Function):
Expand Down
2 changes: 2 additions & 0 deletions docs/release/notes-0.14.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Minor changes
=============

* Support unevaluated :class:`~diofant.polys.rootoftools.RootOf`'s over finite fields, see :pull:`1209`.
* Support calculating limits with :class:`~diofant.functions.elementary.piecewise.Piecewise` functions, see :pull:`1214`.

Developer changes
=================
Expand Down Expand Up @@ -65,3 +66,4 @@ These Sympy issues also were addressed:
* :sympyissue:`23231`: Sympy giving the wrong solution
* :sympyissue:`14387`: Tutorial on limits creates impression that they are two-sided by default
* :sympyissue:`8166`: Limit assumes function is continuous?
* :sympyissue:`18492`: Limit of Piecewise function - NotImplementedError: Don't know how to calculate the mrv

0 comments on commit 15fe853

Please sign in to comment.