一,矩阵LU分解定理
设A为n阶矩阵,如果A的顺序主子式Di≠0(i=1,2,···,n-1),则A可以分解为一个单位下三角矩阵L和一个上三角矩阵U的乘积,且这种分解是唯一的,即A=LU
二,矩阵LU分解Python代码
# 自己原创
def lu_decomposition(coefficient_matrix: np.ndarray, right_hand_side_vector: np.ndarray):
"""
实现方程Ax=b系数矩阵A的LU decomposition
:param coefficient_matrix: 初始系数矩阵A
:param right_hand_side_vector: 初始常数列向量b
:return: 单位下三角矩阵L,上三角矩阵U,常数列向量b
"""
# first step: evaluate the lu decomposition condition
rows, columns = coefficient_matrix.shape
if rows == columns: # judge if it is a square matrix
for k in range(rows): # 判断各阶顺序主子式是否为0
if det(coefficient_matrix[:k + 1, :k + 1]) == 0:
raise Exception("cannot generate LU decomposition")
else:
# LU decomposition
lower_triangular_matrix = np.eye(rows)
for k in range(rows - 1):
for row in range(k + 1, rows):
multiplier = coefficient_matrix[row, k] / coefficient_matrix[k, k]
coefficient_matrix[row, k:] += -multiplier * (coefficient_matrix[k, k:])
right_hand_side_vector[row] += -multiplier * right_hand_side_vector[k]
lower_triangular_matrix[row, k] = multiplier
else:
raise Exception("ERROR:please pass a square matrix.")
return lower_triangular_matrix, coefficient_matrix, right_hand_side_vector