我有以下代码,其结果非常令人困惑:
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)
print(L)
返回以下内容:
[[ 1. 0. 0. ] [ 1. 1. 0. ] [-0.5 -0.25 1. ]]
但这不是B的LU分解中正确的L矩阵,据我所知,命令scipy.linalg.lu(矩阵)只计算你输入的任何矩阵的LU分解矩阵。然而,在这种情况下,L矩阵是不正确的。这里发生什么事情?任何帮助都是非常感谢的。
发布于 2018-12-06 23:01:29
我想你可能误解了分解应该做什么。因为这在我看来是正确的.让我们看看你的例子的结果,还有一些额外的细节和评论:
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.]]
# :-)
我希望这能澄清一点。如果您仍然不确定,那么可能会重读有关置换矩阵、三角矩阵、鲁分解、scipy.linalg.lu和密切相关的主题的文章。
祝好运!
似乎已经作出了澄清:
鲁分解在一般情况下并不一定是唯一的。
如果您想知道上面维基百科链接中的有关分章的详细信息,我推荐关于这堆栈交换问题的第一个和第三个答案。
因此,如果您碰巧从不同的实现或方法中得到了两个不同的答案,这并不意味着其中一个是错误的。如果你有一个置换矩阵P(即使它是平凡的恒等矩阵),一个下矩阵L,一个上矩阵U,它们分解你的矩阵,那么你就得到了一个分解。希望这能把事情弄清楚!
https://stackoverflow.com/questions/53658376
复制相似问题