|
|
|
|
@ -0,0 +1,171 @@
|
|
|
|
|
#数列极限计算、数列敛散性判定、部分和数列计算、级数敛散性判定
|
|
|
|
|
|
|
|
|
|
from sympy import*
|
|
|
|
|
from convert_formula import *
|
|
|
|
|
|
|
|
|
|
def calculate_sequence_limit(sequence, n=1, tolerance=1e-10, max=1000):
|
|
|
|
|
"""
|
|
|
|
|
数列极限求和并判断敛散性
|
|
|
|
|
:param sequence: 数列通式
|
|
|
|
|
:param n:开始计算的n值
|
|
|
|
|
:param tolerance:判断收敛的容差
|
|
|
|
|
:param max: 最大计算项数
|
|
|
|
|
:return:数列极限
|
|
|
|
|
"""
|
|
|
|
|
prev_term = sequence(n)
|
|
|
|
|
current_term = sequence(n + 1)
|
|
|
|
|
|
|
|
|
|
#判断当n足够大时,第n项与第n+1项是否相等
|
|
|
|
|
for i in range(n + 2, n + max + 1):
|
|
|
|
|
if abs(current_term - prev_term) < tolerance:
|
|
|
|
|
return current_term,True
|
|
|
|
|
|
|
|
|
|
pre_term = current_term
|
|
|
|
|
current_term = sequence(i)
|
|
|
|
|
|
|
|
|
|
return None,False
|
|
|
|
|
|
|
|
|
|
def calculate_partial_sums(series_term, n_terms):
|
|
|
|
|
"""
|
|
|
|
|
部分和数列求和
|
|
|
|
|
:param series_term:数列通式
|
|
|
|
|
:param n_terms:总共求和的项数
|
|
|
|
|
:return:数列和
|
|
|
|
|
"""
|
|
|
|
|
#将数列前n项求和
|
|
|
|
|
sum_list=[]
|
|
|
|
|
|
|
|
|
|
for i in range(n_terms):
|
|
|
|
|
sum_list.append(series_term(i))
|
|
|
|
|
|
|
|
|
|
return sum(sum_list)
|
|
|
|
|
|
|
|
|
|
def is_series_convergent(series_term, n=1, tolerance=1e-10, max=1000):
|
|
|
|
|
"""
|
|
|
|
|
判断级数敛散性
|
|
|
|
|
:param series_term: 数列通式
|
|
|
|
|
:param n: 开始计算的n值
|
|
|
|
|
:param tolerance: 判断收敛的容差
|
|
|
|
|
:param max: 最大计算项数
|
|
|
|
|
:return: True/False
|
|
|
|
|
"""
|
|
|
|
|
#首先检查通项极限是否为0
|
|
|
|
|
def term_sequence(n):
|
|
|
|
|
return series_term(n)
|
|
|
|
|
|
|
|
|
|
limit, converged = calculate_sequence_limit(term_sequence, n, tolerance, max)
|
|
|
|
|
|
|
|
|
|
if converged and abs(limit) > tolerance:
|
|
|
|
|
return False # 通项极限不为0,级数发散
|
|
|
|
|
|
|
|
|
|
#判断部分和和数列是否收敛
|
|
|
|
|
def partial_sum_sequence(k):
|
|
|
|
|
return sum(sum_list)
|
|
|
|
|
|
|
|
|
|
limit,sum_converged = calculate_sequence_limit(partial_sum_sequence, n, tolerance, min(max_terms // 2, 100))
|
|
|
|
|
|
|
|
|
|
return sum_converged
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 测试函数
|
|
|
|
|
def run_tests():
|
|
|
|
|
print("===== 数列与级数计算库测试 =====")
|
|
|
|
|
|
|
|
|
|
# 测试数列极限计算
|
|
|
|
|
def test_sequence_limit():
|
|
|
|
|
# 收敛数列 1/n
|
|
|
|
|
def seq1(n):
|
|
|
|
|
return 1 / n
|
|
|
|
|
|
|
|
|
|
limit, converged = calculate_sequence_limit(seq1)
|
|
|
|
|
print(f"数列 1/n: 极限={limit}, 收敛={converged}")
|
|
|
|
|
|
|
|
|
|
# 发散数列 n
|
|
|
|
|
def seq2(n):
|
|
|
|
|
return n
|
|
|
|
|
|
|
|
|
|
limit, converged = calculate_sequence_limit(seq2)
|
|
|
|
|
print(f"数列 n: 极限={limit}, 收敛={converged}")
|
|
|
|
|
|
|
|
|
|
# 测试数列敛散性
|
|
|
|
|
def test_sequence_convergence():
|
|
|
|
|
# 收敛数列 (-1)^n/n
|
|
|
|
|
def seq1(n):
|
|
|
|
|
return (-1) ** n / n
|
|
|
|
|
|
|
|
|
|
converged = is_sequence_convergent(seq1)
|
|
|
|
|
print(f"数列 (-1)^n/n: 收敛={converged}")
|
|
|
|
|
|
|
|
|
|
# 发散数列 sin(n)
|
|
|
|
|
def seq2(n):
|
|
|
|
|
import math
|
|
|
|
|
return math.sin(n)
|
|
|
|
|
|
|
|
|
|
converged = is_sequence_convergent(seq2)
|
|
|
|
|
print(f"数列 sin(n): 收敛={converged}")
|
|
|
|
|
|
|
|
|
|
# 测试部分和计算
|
|
|
|
|
def test_partial_sums():
|
|
|
|
|
# 等比级数 1/2^n
|
|
|
|
|
def term(n):
|
|
|
|
|
return 1 / (2 ** n)
|
|
|
|
|
|
|
|
|
|
sums = calculate_partial_sums(term, 5)
|
|
|
|
|
print(f"级数 1/2^n 的前5项部分和: {sums}")
|
|
|
|
|
|
|
|
|
|
# 测试级数敛散性
|
|
|
|
|
def test_series_convergence():
|
|
|
|
|
# 收敛级数 1/n^2
|
|
|
|
|
def term1(n):
|
|
|
|
|
return 1 / (n ** 2)
|
|
|
|
|
|
|
|
|
|
converged = is_series_convergent(term1)
|
|
|
|
|
print(f"级数 1/n^2: 收敛={converged}")
|
|
|
|
|
|
|
|
|
|
# 发散级数 1/n
|
|
|
|
|
def term2(n):
|
|
|
|
|
return 1 / n
|
|
|
|
|
|
|
|
|
|
converged = is_series_convergent(term2)
|
|
|
|
|
print(f"级数 1/n: 收敛={converged}")
|
|
|
|
|
|
|
|
|
|
# 执行所有测试
|
|
|
|
|
test_sequence_limit()
|
|
|
|
|
print()
|
|
|
|
|
test_sequence_convergence()
|
|
|
|
|
print()
|
|
|
|
|
test_partial_sums()
|
|
|
|
|
print()
|
|
|
|
|
test_series_convergence()
|
|
|
|
|
def UI():
|
|
|
|
|
command=input('请选择功能:1.数列极限并判断敛散性 2.部分和数列求和 3.判断级数敛散性')
|
|
|
|
|
if command=="1":
|
|
|
|
|
sequence=input('请输入以n为变量的数列通式(如1/n):')
|
|
|
|
|
limitation,convergent=calculate_sequence_limit(sequence, n=1, tolerance=1e-10, max=1000)
|
|
|
|
|
if convergent:
|
|
|
|
|
print(f"数列收敛,极限值为{limitatoion}")
|
|
|
|
|
else:
|
|
|
|
|
print('极限不存在')
|
|
|
|
|
|
|
|
|
|
elif command=='2':
|
|
|
|
|
series_term = input('请输入以n为变量的数列通式(如1/n):')
|
|
|
|
|
n_term= input('请输入计算项数:')
|
|
|
|
|
result= calculate_partial_sums(series_term, n_terms)
|
|
|
|
|
print(f'通式为{series_term}前{n_term}项和为{result}')
|
|
|
|
|
elif command=='3':
|
|
|
|
|
series_term = input('请输入以n为变量的数列通式(如1/n):')
|
|
|
|
|
result=is_series_convergent(series_term, n=1, tolerance=1e-10, max=1000)
|
|
|
|
|
if result:
|
|
|
|
|
print('该级数收敛')
|
|
|
|
|
else:
|
|
|
|
|
print('该级数发散')
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
main_menu()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|