我认为您可能误解了分解应该做什么。因为它看起来对我来说是正确的......让我们来看看你的例子的结果,还有一些额外的细节和评论:
import numpy as np
import scipy.linalg
B = np.array([[2,-1,-2],[-4,6,3],[-4,-2,8]])
P,L,U = scipy.linalg.lu(B)
# Now let's see if P is a permutation matrix (a single 1 in each row and column, all the rest should be zero):
print(P)
>> [[ 0. 0. 1.]
[ 1. 0. 0.]
[ 0. 1. 0.]]
# Yup! That's a successful permutation matrix
# Now let's see if L is a lower triangular matrix (anything above the main diagonal should be zero):
print(L)
>> [[ 1. 0. 0. ]
[ 1. 1. 0. ]
[-0.5 -0.25 1. ]]
# Yup! still doing good.
# Now let's see if U is a upper triangular matrix (anything below the main diagonal should be zero):
print(U)
>> [[-4. 6. 3. ]
[ 0. -8. 5. ]
[ 0. 0. 0.75]]
# And last but not least, let's see if this works as a decomposition of B (i.e. PLU==B):
print(np.matmul(P, np.matmul(L, U)))
>> [[ 2. -1. -2.]
[-4. 6. 3.]
[-4. -2. 8.]]
# :-)
我希望这能澄清一点。如果您仍然不确定,那么可以重新阅读permutation matrices、triangular matrices、lu-decomposition、scipy.linalg.lu和密切相关的主题。
祝你好运!
似乎有一个澄清:
在一般情况下,LU 分解不一定是唯一的。
如果您想了解详细信息,那么除了上述维基百科链接中的相关子章节之外,我推荐关于这个堆栈交换问题的第一个和第三个答案。
因此,如果您碰巧从不同的实现或方法中得到两个不同的答案,那并不意味着其中一个是错误的。如果你有一个置换矩阵 P(即使它是微不足道的单位矩阵)、一个下矩阵 L、一个上矩阵 U,并且它们分解了你的矩阵,那么你就得到了一个分解。希望这能说明问题!