繁体   English   中英

为什么scipy.linalg.lu()在此代码中返回错误的方阵B分解矩阵?

[英]Why does scipy.linalg.lu() return the wrong decomposition matrices of square matrix B in this code?

我有以下代码,其结果非常令人困惑:

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(matrix)仅计算您放入的任何矩阵的LU分解矩阵。但是,在这种情况下, L矩阵不正确。 这里发生了什么? 任何帮助表示赞赏。

我认为您可能会误解分解该做什么。 “因为它对我来说看起来是正确的...让我们来看一下示例的结果,并附带一些其他详细信息和评论:

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.]]

# :-)  

我希望这可以澄清一些事情。 如果仍然不确定,则可以重新阅读有关置换矩阵三角矩阵lu- depositionscipy.linalg.lu和紧密相关的主题。
祝好运!


似乎有一个澄清:
在一般情况下,LU分解不一定是唯一的。
如果您想获取详细信息,然后在上面的Wikipedia链接中保留相关子章节的内容 ,那么我建议堆栈交换问题的第一个和第三个答案。
因此,如果您碰巧从不同的实现或方法中获得了两个不同的答案,那并不意味着一个或另一个是错误的。 如果您有一个置换矩阵P(即使它是平凡的单位矩阵),一个低矩阵L,一个高矩阵U,并且它们分解了矩阵,那么您自己就是一个分解。 希望这能说明问题!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM