How to know whether a function is continuous with sympy?

ε祈祈猫儿з 提交于 2019-12-12 23:49:34

问题


I need to define a function that checks if the input function is continuous at a point with sympy.

I searched the sympy documents with the keyword "continuity" and there is no existing function for that. I think maybe I should consider doing it with limits, but I'm not sure how.

def check_continuity(f, var, a):
    try:
            f = sympify(f)
        except SympifyError:
            return("Invaild input")
        else:
            x1 = Symbol(var, positive = True)
            x2 = Symbol(var, negative = True)
            //I don't know what to do after this

回答1:


Yes, you need to use the limits.

The formal definition of continuity at a point has three conditions that must be met. A function f(x) is continuous at a point where x = c if

  • lim x —> c f(x) exists
  • f(c) exists (That is, c is in the domain of f.)
  • lim x —> c f(x) = f(c)

SymPy can compute symbolic limits with the limit function.

>>> limit(sin(x)/x, x, 0)
1

See: https://docs.sympy.org/latest/tutorial/calculus.html#limits




回答2:


I would suggest you use the function continuous_domain. This is defined in the calculus.util

Example usage:

>>> from sympy import Symbol, S
>>> from sympy.calculus.util import continuous_domain
>>> x = Symbol("x")
>>> f = sin(x)/x
>>> continuous_domain(f, x, S.Reals)
Union(Interval.open(-oo, 0), Interval.open(0, oo))

This is not yet added to docs. You can check further details here.



来源:https://stackoverflow.com/questions/55881078/how-to-know-whether-a-function-is-continuous-with-sympy

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!