|
|
import unittest
|
|
|
from numpy import *
|
|
|
from sympy import *
|
|
|
from convert_formula import *
|
|
|
|
|
|
def definite_calculation(function,upper,lower):
|
|
|
"""
|
|
|
定积分计算
|
|
|
:param function: 被积函数
|
|
|
:param upper: 积分上限
|
|
|
:param lower: 积分下限
|
|
|
:return: 定积分值
|
|
|
"""
|
|
|
function= sympify(convert_formula(function))
|
|
|
x= symbols('x')
|
|
|
result= integrate(function, (x,lower,upper))
|
|
|
return float(result)
|
|
|
|
|
|
def variable_integral_calculation(function,upper,lower):
|
|
|
"""
|
|
|
变限积分计算
|
|
|
:param function: 被积函数
|
|
|
:param upper: 积分上限
|
|
|
:param lower: 积分下限
|
|
|
:return: 变限积分积分后函数
|
|
|
"""
|
|
|
function= sympify(convert_formula(function))
|
|
|
t,x= symbols('t x')
|
|
|
in_function= integrate(function, t)
|
|
|
if upper=='x':
|
|
|
num= in_function.subs(t,lower)
|
|
|
up=in_function.replace(t,x)
|
|
|
result=up-num
|
|
|
elif lower=='x':
|
|
|
num = in_function.subs(t, upper)
|
|
|
low = in_function.replace(t, x)
|
|
|
result = num - low
|
|
|
else:
|
|
|
return None
|
|
|
return result
|
|
|
|
|
|
def improper_integral_calculation(function,upper,lower):
|
|
|
"""
|
|
|
反常积分计算并判断敛散性
|
|
|
:param function: 被积函数
|
|
|
:param upper: 积分上限
|
|
|
:param lower: 积分下限
|
|
|
:return: 积分值
|
|
|
"""
|
|
|
function = sympify(convert_formula(function))
|
|
|
x = symbols('x')
|
|
|
in_function = integrate(function,x)
|
|
|
try:
|
|
|
if upper=='oo' and lower!= '-oo':
|
|
|
upper_num = limit(in_function, x, oo, dir="-")
|
|
|
lower_num = limit(in_function, x, lower, dir="+")
|
|
|
elif upper!='oo' and lower=='-oo':
|
|
|
upper_num = limit(in_function, x, upper, dir="-")
|
|
|
lower_num = limit(in_function, x, -oo, dir="+")
|
|
|
elif upper=='oo' and lower=='-oo':
|
|
|
upper_num = float(limit(in_function, x, oo, dir="-"))
|
|
|
lower_num = float(limit(in_function, x, -oo, dir="+"))
|
|
|
else:
|
|
|
in_function = integrate(function, x)
|
|
|
upper_num = in_function.subs(x,upper)
|
|
|
lower_num = in_function.subs(x,lower)
|
|
|
result = upper_num- lower_num
|
|
|
return result,True
|
|
|
except:
|
|
|
return None,False
|
|
|
|
|
|
class TestIntegralFunctions(unittest.TestCase):
|
|
|
def test_definite_integral(self):
|
|
|
"""测试定积分计算"""
|
|
|
# 测试案例1: ∫[0,1] x^2 dx = 1/3
|
|
|
self.assertEqual(definite_calculation('x^2', 1, 0), 1/3)
|
|
|
|
|
|
# 测试案例2: ∫[0,π] sin(x) dx = 2
|
|
|
self.assertEqual(definite_calculation('sin(x)', pi, 0), 2)
|
|
|
|
|
|
# 测试案例3: ∫[1,e] 1/x dx = 1
|
|
|
self.assertEqual(definite_calculation('1/x', E, 1), 1)
|
|
|
|
|
|
def test_variable_integral(self):
|
|
|
"""测试变限积分计算"""
|
|
|
x = symbols('x')
|
|
|
|
|
|
# 测试案例1: ∫[0,x] t dt = x^2/2
|
|
|
result = variable_integral_calculation('t', 'x', 0)
|
|
|
expected = x ** 2 / 2
|
|
|
self.assertEqual(simplify(result - expected), 0)
|
|
|
|
|
|
# 测试案例2: ∫[x,1] t^2 dt = 1/3 - x^3/3
|
|
|
result = variable_integral_calculation('t^2', 1, 'x')
|
|
|
expected = Rational(1, 3) - x ** 3 / 3
|
|
|
self.assertEqual(simplify(result - expected), 0)
|
|
|
|
|
|
def test_improper_integral(self):
|
|
|
"""测试反常积分计算"""
|
|
|
# 测试案例1: ∫[1,∞] 1/x^2 dx = 1 (收敛)
|
|
|
result, convergent = improper_integral_calculation('1/x^2', 'oo', 1)
|
|
|
self.assertEqual(result, 1)
|
|
|
self.assertTrue(convergent)
|
|
|
|
|
|
# 测试案例2: ∫[-∞,0] e^x dx = 1 (收敛)
|
|
|
result, convergent = improper_integral_calculation('exp(x)', 0, '-oo')
|
|
|
self.assertEqual(result, 1)
|
|
|
self.assertTrue(convergent)
|
|
|
|
|
|
# 测试案例3: ∫[-∞,∞] sin(x) dx (发散)
|
|
|
result, convergent = improper_integral_calculation('sin(x)', 'oo', '-oo')
|
|
|
self.assertIsNone(result)
|
|
|
self.assertFalse(convergent)
|
|
|
|
|
|
# 测试案例4: ∫[0,1] 1/sqrt(x) dx = 2 (收敛)
|
|
|
result, convergent = improper_integral_calculation('1/sqrt(x)', 1, 0)
|
|
|
self.assertEqual(result, 2)
|
|
|
self.assertTrue(convergent)
|
|
|
|
|
|
def UI():
|
|
|
while True:
|
|
|
command= input('请选择功能:1.定积分计算2.变限积分计算3.反常积分计算和敛散性4.退出')
|
|
|
if command=='1':
|
|
|
function = input('请输入以x为变量的被积函数:')
|
|
|
upper = input('请输入积分上限:')
|
|
|
lower = input('请输入积分下限:')
|
|
|
print('定积分积分结果:',definite_calculation(function, upper, lower))
|
|
|
elif command=='2':
|
|
|
function = input('请输入以t为变量的被积函数:')
|
|
|
upper = input('请输入积分上限:')
|
|
|
lower = input('请输入积分下限:')
|
|
|
print('变限积分积分结果:',variable_integral_calculation(function, upper, lower))
|
|
|
elif command=='3':
|
|
|
function = input('请输入以x为变量的被积函数:')
|
|
|
upper = input('请输入积分上限:')
|
|
|
lower = input('请输入积分下限:')
|
|
|
num,convergent= improper_integral_calculation(function,upper,lower)
|
|
|
print('反常积分的计算值:',num)
|
|
|
if convergent:
|
|
|
print('反常积分收敛')
|
|
|
else:
|
|
|
print('反常积分发散')
|
|
|
elif command=='4':
|
|
|
break
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
# 运行测试但不退出,允许后续代码执行
|
|
|
unittest.main(exit=False)
|
|
|
UI()
|