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)
f.limit(x, 0)
– Endor