The Python Oracle

How to understand the pivot matrix of scipy.linalg.lu_factor?

--------------------------------------------------
Hire the world's top talent on demand or became one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Sunrise at the Stream

--

Chapters
00:00 How To Understand The Pivot Matrix Of Scipy.Linalg.Lu_factor?
00:34 Accepted Answer Score 8
01:16 Answer 2 Score 0
01:27 Thank you

--

Full question
https://stackoverflow.com/questions/2592...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #numpy #scipy

#avk47



ACCEPTED ANSWER

Score 8


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):

  1. Row 0 changes with Row 1
  2. The new Row 1 changes with Row 2 and
  3. The new Row 2 stays 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 is

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

This is probably not a very fast way of computing P but it does the trick and answers the question.




ANSWER 2

Score 0


Or with slight refactoring -

def constructPermutationMatrixFromPermutationVector(piv):
        n = len(piv)
        P = np.eye(n)
        for kk, ll in enumerate(piv):
            P[:, kk], P[:, ll] = P[:, ll].copy(), P[:, kk].copy()
        return P