如何理解 scipy.linalg.lu_factor 的主元矩阵? [英] How to understand the pivot matrix of scipy.linalg.lu_factor?
问题描述
如何手动重建由 lu_factor?(A = PLU)
How can I manually reconstruct a matrix A that was factorized by lu_factor? (A = PLU)
由于矩阵P的设置,我目前的尝试都失败了.这是我目前所拥有的:
My current attempts all failed due to the setup of matrix P. Here is what I have so far:
A = np.random.rand(3,3)
lu, piv = lu_factor(A)
U = np.triu(lu)
L = np.tril(lu, -1)
L[np.diag_indices_from(L)] = 1.0
我正在寻找使这条线打印True的矩阵P:
I am looking for the matrix P that makes this line print True:
print np.allclose(A, np.dot(P, np.dot(L, U)))
感谢任何提示/链接/建议!
Any hint/link/suggestion is appreciated!
推荐答案
置换向量需要依次解释.如果 piv=[1,2,2]
则需要按顺序执行以下操作(使用从零开始的索引):
The permutation vector needs to be interpreted in sequence. If piv=[1,2,2]
then the following needs to be done in sequence (with zero-based indexing):
- 第 0 行随第 1 行变化
- 新的第 1 行随着第 2 行的变化而变化
- 新的第 2 行保持不变.
在代码中,这可以解决问题:
In code this would do the trick:
P = np.eye(3)
for i, p in enumerate(piv):
Q = np.eye(3,3)
q = Q[i,:].copy()
Q[i,:] = Q[p,:]
Q[p,:] = q
P = np.dot(P, Q)
对于 piv=[1,2,2]
P 是
[[ 0. 0. 1.]
[ 1. 0. 0.]
[ 0. 1. 0.]]
这可能不是一种计算 P 的非常快的方法,但它可以解决问题并回答问题.
This is probably not a very fast way of computing P but it does the trick and answers the question.
这篇关于如何理解 scipy.linalg.lu_factor 的主元矩阵?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!