Files
CalWay_Python/按方法整理/矩阵-雅可比得J.py
2025-06-19 09:34:40 +08:00

57 lines
1.7 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#模 范数
def Norm(x,v):
if len(x[0]) == 1:
if v == 1:
return sum([abs(i[0]) for i in x])
elif v == 2:
return (sum([i[0]**2 for i in x]))**0.5
elif v == float("inf"):
return max([abs(i[0]) for i in x])
else:
if v == 1:
return max([sum([abs(x[i][j]) for i in range(len(x))]) for j in range(len(x[0]))])
elif v == float("inf"):
return max([sum([abs(i) for i in x[j]]) for j in range(len(x))])
return None
# 计算矩阵的点积
def Dot(A,B):
if len(A[0]) != len(B):
return None
return [[sum([A[i][j] * B[j][k] for j in range(len(A[0]))]) for k in range(len(B[0]))] for i in range(len(A))]
# 计算矩阵的行列式
def Det(A):
if len(A) == 2:
return A[0][0] * A[1][1] - A[0][1] * A[1][0]
det = 0
for c in range(len(A)):
sub_matrix = [row[:c] + row[c+1:] for row in A[1:]]
det += ((-1) ** c) * A[0][c] * Det(sub_matrix)
return det
if __name__ == "__main__":
##########################################################################
# 把矩阵换成题干的矩阵 #########################
A = [
[1,2,-2],
[1,1,1],
[2,2,1]
]
# 把范数的种类数换成题干的要求inf是无穷范数 #########################
LU = A.copy()
ID = [[0 for _ in range(len(A))] for __ in range(len(A))]
for i in range(len(A)):
for j in range(len(A[0])):
if i == j:
ID[i][j] = 1/A[i][j]
LU[i][j] = 0
else:
LU[i][j] = -LU[i][j]
print(f"LU分解的LU矩阵为: {LU}")
print(f"LU分解的D矩阵的逆矩阵为: {ID}")
J = Dot(ID, LU)
print(f"LU分解的J矩阵为: {J}")