How to know whether a function is continuous with sympy?
Asked Answered
S

4

6

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
Schizo answered 27/4, 2019 at 13:31 Comment(0)
D
3

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

Derick answered 27/4, 2019 at 14:2 Comment(1)
as a method call: f.limit(x, 0)Endor
T
6

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

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 documented in the SymPy docs here. You can also view the source code here.

Timberlake answered 27/4, 2019 at 22:10 Comment(0)
D
3

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

Derick answered 27/4, 2019 at 14:2 Comment(1)
as a method call: f.limit(x, 0)Endor
I
2

Here is a more simple way to check if a function is continues for a specific value:

import sympy as sp

x = sp.Symbol("x")
f = 1/x
value = 0

def checkifcontinus(func,x,symbol):
    return (sp.limit(func, symbol, x).is_real)

print(checkifcontinus(f,value,x))

This code output will be - False

Idaline answered 13/6, 2021 at 18:24 Comment(0)
E
0

By solving the complementary problem, whether or not a function has singular points. This is done with singularities. It works for univariate (real or complex) functions.

from sympy import singularities, Symbol

def is_continuous(f, symbol):
  return (False if singularities(f, symbol) else True)


x = Symbol('x', real=True)

f = x**2 + x + 1
print(is_continuous(f, x)
#True

f = 1 / (2*x+1)
print(is_continuous(f, x)
#False

It seems that when f is a Piecewise function all presented answers fails

from sympy import Piecewise

f_pw = Piecewise((1, x<1), (0, x>=1)) # has discontinuity at x=1

# cards
print(is_continuous(f_pw, x))
#True

# Supreet Agrawal
print(continuous_domain(f_pw, x, S.Reals))
#Reals

# Laurent LAPORTE
try:
  print(limit(f_pw, x, 1))
except Exception as e:
  print(e)
#'ExprCondPair' object has no attribute '_eval_is_meromorphic'

Check the type of the function could be a possible solution. If Piecewise then use the method as_expr_set_pairs to get a list of pairs (value, interval) to perform a pairwise evaluation to check continuity.

def is_continuous(f, symbol):
    # compatible with Piecewise functions

    if f.is_Piecewise:
        pw_conditions = f.as_expr_set_pairs() # assumed to be ordered!
      
        for (value_l, interval_l), (value_r, interval_r) in zip(pw_conditions, pw_conditions[1:]):
            # intersection should be only a singleton
            dicontinuity_points = interval_l.boundary.intersect(interval_r.boundary)
            # interation of one element
            for dp in dicontinuity_points:
                # check left and right values 
                if value_l.limit(symbol, dp) != value_r.limit(symbol, dp):
                    return False
        return True
    
    return (False if singularities(f, symbol) else True)
Endor answered 6/11, 2023 at 8:0 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.