|
|
#重积分:包括直角坐标、极坐标下二重积分的计算、直角坐标、柱坐标、球坐标下三重积分的计算等
|
|
|
|
|
|
import unittest
|
|
|
import sympy as sp
|
|
|
from convert_formula import *
|
|
|
import math
|
|
|
|
|
|
def rectangular_double_integration(region1, region2, function):
|
|
|
"""
|
|
|
求直角坐标系下二重积分
|
|
|
:param region1: y的积分区域(以元组或列表形式给出)
|
|
|
:param region2: x的积分区域(同region1)
|
|
|
:param function: 被积函数
|
|
|
:return: 二重积分值
|
|
|
"""
|
|
|
lower1, upper1= region1[0], region1[1]
|
|
|
lower2, upper2= region2[0], region2[1]
|
|
|
function = sp.sympify(convert_formula(function))
|
|
|
x, y= sp.symbols('x y')
|
|
|
integration1 = sp.integrate(function, (y, lower1, upper1))
|
|
|
result = sp.integrate(integration1, (x,lower2,upper2))
|
|
|
return result
|
|
|
|
|
|
def polar_double_integration(function, rou, theta):
|
|
|
"""
|
|
|
求极坐标下的二重积分
|
|
|
:param function: 用极坐标表示的被积函数
|
|
|
:param rou: ρ的积分区间(以元组或列表给出)
|
|
|
:param theta: θ的积分区间(同ρ)
|
|
|
:return: 极坐标下的积分值
|
|
|
"""
|
|
|
rou1, rou2= rou[0], rou[1]
|
|
|
theta1, theta2= theta[0], theta[1]
|
|
|
rou, theta= sp.symbols('rou theta')
|
|
|
function= sp.sympify(convert_formula(function))
|
|
|
function= function* rou
|
|
|
integration1= sp.integrate(function, (rou, rou1, rou2))
|
|
|
result= sp.integrate(integration1, (theta, theta1, theta2))
|
|
|
return result
|
|
|
|
|
|
def rectangular_triple_integration(region1, region2, region3, function):
|
|
|
"""
|
|
|
求三重积分值
|
|
|
:param region1: z积分区域(以元组或列表形式给出)
|
|
|
:param region2: y积分区域(同上)
|
|
|
:param region3: x积分区域(同上)
|
|
|
:param function: 被积函数
|
|
|
:return: 三重积分值
|
|
|
"""
|
|
|
lower1, upper1= region1[0], region1[1]
|
|
|
lower2, upper2= region2[0], region2[1]
|
|
|
lower3, upper3 = region3[0], region3[1]
|
|
|
function = sp.sympify(convert_formula(function))
|
|
|
x, y, z= sp.symbols('x y z')
|
|
|
integration1 = sp.integrate(function, (z, lower1, upper1))
|
|
|
integration2 = sp.integrate(integration1, (y, lower2, upper2))
|
|
|
result = sp.integrate(integration2, (x, lower3, upper3))
|
|
|
return result
|
|
|
|
|
|
def cylindrical_triple_integration(rou, theta, region, function):
|
|
|
"""
|
|
|
求柱坐标下三重积分值
|
|
|
:param rou: ρ的积分区域(以元组或列表形式给出)
|
|
|
:param theta: θ的积分区间(以元组或列表形式给出)
|
|
|
:param region: z的积分区间(以元组或列表形式给出)
|
|
|
:param function: 被积函数(以柱坐标形式给出)
|
|
|
:return: 三重积分值
|
|
|
"""
|
|
|
rou1, rou2 = rou[0], rou[1]
|
|
|
theta1, theta2 = theta[0], theta[1]
|
|
|
z1, z2 = region[0], region[1]
|
|
|
function = sp.sympify(convert_formula(function))
|
|
|
rou, theta, z= sp.symbols('rou theta z')
|
|
|
function = function * rou
|
|
|
integration1 = sp.integrate(function, (rou, rou1, rou2))
|
|
|
integration2 = sp.integrate(integration1, (theta, theta1, theta2))
|
|
|
result = sp.integrate(integration2, (z, z1, z2))
|
|
|
return result
|
|
|
|
|
|
def spherical_triple_integration(rou, theta, gamma, function):
|
|
|
"""
|
|
|
求球坐标下三重积分值
|
|
|
:param rou: ρ的积分区域(以元组或列表形式给出)
|
|
|
:param theta: θ的积分区间(以元组或列表形式给出)
|
|
|
:param gamma: γ的积分区间(以元组或列表形式给出)
|
|
|
:param function: 被积函数(以柱坐标形式给出)
|
|
|
:return: 三重积分值
|
|
|
"""
|
|
|
rou1, rou2 = rou[0], rou[1]
|
|
|
theta1, theta2 = theta[0], theta[1]
|
|
|
gamma1, gamma2 = gamma[0], gamma[1]
|
|
|
function = sp.sympify(convert_formula(function))
|
|
|
rou, theta, gamma = sp.symbols('rou theta gamma')
|
|
|
function = function * rou**2
|
|
|
integration1 = sp.integrate(function, (rou, rou1, rou2))
|
|
|
integration1 = integration1* sp.sin(theta)
|
|
|
integration2 = sp.integrate(integration1, (theta, theta1, theta2))
|
|
|
result = sp.integrate(integration2, (gamma, gamma1, gamma2))
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestIntegralFunctions(unittest.TestCase):
|
|
|
|
|
|
def test_rectangular_double_integration(self):
|
|
|
"""测试直角坐标系下的二重积分"""
|
|
|
# 测试案例1: ∫∫(x^2 + y^2) dxdy,其中 x∈[0,1], y∈[0,1] = 2/3
|
|
|
result = rectangular_double_integration(
|
|
|
region1=(0, 1),
|
|
|
region2=(0, 1),
|
|
|
function="x^2 + y^2"
|
|
|
)
|
|
|
self.assertEqual(float(result), 2/3)
|
|
|
|
|
|
# 测试案例2: ∫∫1 dxdy,其中 x∈[0,2], y∈[0,3] = 6
|
|
|
result = rectangular_double_integration(
|
|
|
region1=(0, 3),
|
|
|
region2=(0, 2),
|
|
|
function="1"
|
|
|
)
|
|
|
self.assertEqual(result, 6)
|
|
|
|
|
|
def test_polar_double_integration(self):
|
|
|
"""测试极坐标系下的二重积分"""
|
|
|
# 测试案例1: ∫∫(r) rdrdθ,其中 r∈[0,1], θ∈[0,2π] = π
|
|
|
result = polar_double_integration(
|
|
|
function="rou",
|
|
|
rou=(0, 1),
|
|
|
theta=(0, 2*math.pi)
|
|
|
)
|
|
|
self.assertEqual(result, 2*math.pi/3)
|
|
|
|
|
|
# 测试案例2: ∫∫(r^2) rdrdθ,其中 r∈[0,2], θ∈[0,π] = 4π
|
|
|
result = polar_double_integration(
|
|
|
function="rou^2",
|
|
|
rou=(0, 2),
|
|
|
theta=(0, math.pi)
|
|
|
)
|
|
|
self.assertEqual(result, 4*math.pi)
|
|
|
|
|
|
def test_rectangular_triple_integration(self):
|
|
|
"""测试直角坐标系下的三重积分"""
|
|
|
# 测试案例1: ∫∫∫(x + y + z) dxdydz,其中 x∈[0,1], y∈[0,1], z∈[0,1] = 3/2
|
|
|
result = rectangular_triple_integration(
|
|
|
region1=(0, 1),
|
|
|
region2=(0, 1),
|
|
|
region3=(0, 1),
|
|
|
function="x + y + z"
|
|
|
)
|
|
|
self.assertEqual(float(result), 3/2)
|
|
|
|
|
|
# 测试案例2: ∫∫∫1 dxdydz,其中 x∈[0,1], y∈[0,2], z∈[0,3] = 6
|
|
|
result = rectangular_triple_integration(
|
|
|
region1=(0, 3),
|
|
|
region2=(0, 2),
|
|
|
region3=(0, 1),
|
|
|
function="1"
|
|
|
)
|
|
|
self.assertEqual(result, 6)
|
|
|
|
|
|
def test_cylindrical_triple_integration(self):
|
|
|
"""测试柱坐标系下的三重积分"""
|
|
|
# 测试案例1: ∫∫∫(r) r**2dzdrdθ,其中 r∈[0,1], θ∈[0,2π], z∈[0,1] = π
|
|
|
result = cylindrical_triple_integration(
|
|
|
rou=(0, 1),
|
|
|
theta=(0, 2*math.pi),
|
|
|
region=(0, 1),
|
|
|
function="rou"
|
|
|
)
|
|
|
self.assertEqual(result, 2*math.pi/3)
|
|
|
|
|
|
# 测试案例2: ∫∫∫(z) rdzdrdθ,其中 r∈[0,2], θ∈[0,π], z∈[0,1] = 2π
|
|
|
result = cylindrical_triple_integration(
|
|
|
rou=(0, 2),
|
|
|
theta=(0, math.pi),
|
|
|
region=(0, 1),
|
|
|
function="z"
|
|
|
)
|
|
|
self.assertEqual(result, math.pi)
|
|
|
|
|
|
def test_spherical_triple_integration(self):
|
|
|
"""测试球坐标系下的三重积分"""
|
|
|
# 测试案例1: ∫∫∫(r) r²sin(θ)drdθdφ,其中 r∈[0,1], θ∈[0,π], φ∈[0,2π] = π
|
|
|
result = spherical_triple_integration(
|
|
|
rou=(0, 1),
|
|
|
theta=(0, math.pi),
|
|
|
gamma=(0, 2*math.pi),
|
|
|
function="rou"
|
|
|
)
|
|
|
self.assertEqual(result, math.pi)
|
|
|
|
|
|
# 测试案例2: ∫∫∫(1) r²sin(θ)drdθdφ,其中 r∈[0,1], θ∈[0,π], φ∈[0,2π] = 4π/3
|
|
|
result = spherical_triple_integration(
|
|
|
rou=(0, 1),
|
|
|
theta=(0, math.pi),
|
|
|
gamma=(0, 2*math.pi),
|
|
|
function="1"
|
|
|
)
|
|
|
self.assertEqual(result, 4*math.pi/3)
|
|
|
|
|
|
def UI():
|
|
|
while True:
|
|
|
command = input('请选择将要进行的操作:1.直角坐标下二重积分 2.极坐标下二重积分 3.直角坐标系下三重积分 4.柱坐标下三重积分 5.球坐标下三重积分 6.退出(所有积分区间请以元组或列表的形式给出)')
|
|
|
if command=='1':
|
|
|
function = input('请输入被积函数:')
|
|
|
region1 = input('请输入第一重积分区间:')
|
|
|
region2 = input('请输入第二重积分区间:')
|
|
|
print('直角坐标系下二重积分值:', rectangular_double_integration(region1, region2, function))
|
|
|
if command=='2':
|
|
|
function = input('请输入用极坐标表示的被积函数:')
|
|
|
rou = input('请输入ρ的积分区间:')
|
|
|
theta = input('请输入θ的积分区间:')
|
|
|
print('极坐标下二重积分值:', polar_double_integration(function, rou, theta))
|
|
|
if command=='3':
|
|
|
function = input('请输入被积函数:')
|
|
|
region1 = input('请输入第一重积分区间:')
|
|
|
region2 = input('请输入第二重积分区间:')
|
|
|
region3 = input('请输入第三重积分区间:')
|
|
|
print('直角坐标系下三重积分值:', rectangular_triple_integration(region1, region2, region3, function))
|
|
|
if command=='4':
|
|
|
function = input('请输入用柱坐标表示的被积函数:')
|
|
|
rou = input('请输入ρ的积分区间:')
|
|
|
theta = input('请输入θ的积分区间:')
|
|
|
region = input('请输入z的积分区间:')
|
|
|
print('柱坐标系下三重积分值:',cylindrical_triple_integration(rou, theta, region, function))
|
|
|
if command=='5':
|
|
|
function = input('请输入用球坐标表示的被积函数:')
|
|
|
rou = input('请输入ρ的积分区间:')
|
|
|
theta = input('请输入θ的积分区间:')
|
|
|
gamma = input('请输入γ的积分区间:')
|
|
|
print('球坐标系下三重积分值:',spherical_triple_integration(rou, theta, gamma, function))
|
|
|
if command=='6':
|
|
|
break
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
UI()
|
|
|
unittest.main(exit=False)
|
|
|
|