How to understand the scipy.linalg.lu_factor summary matrix?

How can I manually restore matrix A factorized by lu_factor ? (A = PLU)

My current attempts failed due to the installation of the matrix P. Here is what I still have:

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

I am looking for a matrix P that outputs this line True:

print np.allclose(A, np.dot(P, np.dot(L, U)))

Any hint / link / suggestion appreciated!

+4
source share
1 answer

The permutation vector must be interpreted sequentially. If piv=[1,2,2], then you must do the following (with zero indexing):

  • Line 0 is changed using line 1
  • New line 1 is changed using line 2 and
  • The new Row 2 remains the same.

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)

For piv=[1,2,2]P there is

[[ 0.  0.  1.]
 [ 1.  0.  0.]
 [ 0.  1.  0.]]

, , P, .

+4

All Articles