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

2 Answers

1
Laurent LAPORTE On Best Solutions

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

0
Supreet Agrawal On

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.