|
|
from sympy import *
|
|
|
from convert_formula import *
|
|
|
|
|
|
def function_limitation(function, independent_variable, point, direction='+'):
|
|
|
"""
|
|
|
计算函数在指定点的极限值
|
|
|
:param function: 函数表达式
|
|
|
:param independent_variable: 自变量
|
|
|
:param point: 自变量的取值点
|
|
|
:return: 极限值
|
|
|
"""
|
|
|
# 转换函数表达式为SymPy表达式
|
|
|
function_sympy = sympify(convert_formula(function))
|
|
|
independent_variable_sympy = symbols(independent_variable)
|
|
|
# 计算函数在指定点的极限值
|
|
|
limit_value = limit(function_sympy, independent_variable_sympy, float(point), dir=direction)
|
|
|
if limit_value == oo or limit_value == -oo:
|
|
|
return limit_value
|
|
|
return round(limit_value, 3)
|
|
|
|
|
|
def function_limitation_line(function, independent_variable):
|
|
|
"""
|
|
|
求函数的渐近线
|
|
|
:param function: 函数表达式
|
|
|
:param independent_variable: 自变量
|
|
|
:return: 渐近线方程
|
|
|
"""
|
|
|
function1 = '(%s)/%s'%(function, independent_variable)
|
|
|
k = function_limitation(function1, independent_variable, oo)
|
|
|
function2 = '%s-%s*%s'%(function, str(k), independent_variable)
|
|
|
b = function_limitation(function2, independent_variable, oo)
|
|
|
if k==1:
|
|
|
k = ''
|
|
|
if b==0:
|
|
|
b = ''
|
|
|
elif b>0:
|
|
|
b = '+'+str(b)
|
|
|
if k==0:
|
|
|
return 'y=%s'%str(b)
|
|
|
return 'y=%s%s%s'%(str(k), independent_variable, str(b))
|
|
|
|
|
|
def function_continue(function, independent_variable, point):
|
|
|
"""
|
|
|
判断函数在指定点的连续性
|
|
|
:param function: 函数表达式
|
|
|
:param independent_variable: 自变量
|
|
|
:param point: 自变量的取值点
|
|
|
:return: 连续性值
|
|
|
"""
|
|
|
left_limitation=function_limitation(function, independent_variable, point, direction='-')
|
|
|
right_limitation=function_limitation(function, independent_variable, point, direction='+')
|
|
|
if left_limitation==right_limitation:
|
|
|
return '连续'
|
|
|
else:
|
|
|
return '不连续'
|
|
|
|
|
|
def test_function_limitation():
|
|
|
"""
|
|
|
测试 function_limitation 函数
|
|
|
"""
|
|
|
# 测试函数在指定点的极限值
|
|
|
result = function_limitation('x**2', 'x', 2)
|
|
|
assert result == 4.000
|
|
|
result = function_limitation('1/x', 'x', 0)
|
|
|
assert result == oo
|
|
|
# 测试函数在无穷远处的极限值
|
|
|
result = function_limitation('x**2', 'x', oo)
|
|
|
assert result == oo
|
|
|
result = function_limitation('1/x', 'x', oo)
|
|
|
assert result == 0
|
|
|
|
|
|
def test_function_limitation_line():
|
|
|
result = function_limitation_line('(2*x^2-3)/(x+1)', 'x')
|
|
|
assert result == 'y=2x-2'
|
|
|
|
|
|
def test_function_continue():
|
|
|
# 测试连续的情况
|
|
|
result = function_continue('x**2', 'x', 2)
|
|
|
assert result == '连续'
|
|
|
# 测试不连续的情况
|
|
|
result = function_continue('1/x', 'x', 0)
|
|
|
assert result == '不连续'
|
|
|
|
|
|
def UI():
|
|
|
while True:
|
|
|
command=input("请选择功能:1.求函数极限 2.求函数渐近线 3.判断函数连续性 4.返回上一层")
|
|
|
if command=='1':
|
|
|
function=input("请输入函数表达式:(注:只支持形如f(x)=的表达式,不支持y=的表达式)")
|
|
|
independent_variable, functions=split_function(function)
|
|
|
point=input("请输入自变量的取值点(如0,0+,0-):")
|
|
|
if point.isdigit():
|
|
|
points=float(point)
|
|
|
result=function_limitation(functions, independent_variable, points)
|
|
|
elif point == 'oo' or point == '-oo':
|
|
|
result=function_limitation(functions, independent_variable, eval(point))
|
|
|
else:
|
|
|
direction=point[-1]
|
|
|
pointsfloat(point[:-1])
|
|
|
result=function_limitation(functions, independent_variable, points, direction)
|
|
|
print("函数在%s=%s的极限值为:%s"%(independent_variable, point, result))
|
|
|
elif command=='2':
|
|
|
function=input("请输入函数表达式:(注:只支持形如f(x)=的表达式,不支持y=的表达式)")
|
|
|
independent_variable, function=split_function(function)
|
|
|
result=function_limitation_line(function, independent_variable)
|
|
|
print("函数的渐近线方程为:%s"%result)
|
|
|
elif command=='3':
|
|
|
function=input("请输入函数表达式:(注:只支持形如f(x)=的表达式,不支持y=的表达式)")
|
|
|
independent_variable, function=split_function(function)
|
|
|
point=float(input("请输入自变量的取值点:"))
|
|
|
result=function_continue(function, independent_variable, point)
|
|
|
print("函数在该点处的连续性为:%s"%result)
|
|
|
elif command=='4':
|
|
|
break
|
|
|
else:
|
|
|
print("输入错误,请重新输入")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
test_function_limitation()
|
|
|
test_function_limitation_line()
|
|
|
test_function_continue()
|
|
|
UI() |