diff --git a/diofant/core/expr.py b/diofant/core/expr.py index 5871995aa4f..3b3aa3d9113 100644 --- a/diofant/core/expr.py +++ b/diofant/core/expr.py @@ -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: @@ -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 diff --git a/diofant/series/gruntz.py b/diofant/series/gruntz.py index f5d6b2782e3..d9ca5033832 100644 --- a/diofant/series/gruntz.py +++ b/diofant/series/gruntz.py @@ -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]) diff --git a/diofant/tests/series/test_limits.py b/diofant/tests/series/test_limits.py index e775d2f6f4c..577372fa6bb 100644 --- a/diofant/tests/series/test_limits.py +++ b/diofant/tests/series/test_limits.py @@ -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): diff --git a/docs/release/notes-0.14.rst b/docs/release/notes-0.14.rst index 16a3e887073..5cee93a6039 100644 --- a/docs/release/notes-0.14.rst +++ b/docs/release/notes-0.14.rst @@ -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 ================= @@ -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