How to understand the pivot matrix of scipy.linalg.lu_factor?
Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn
--
Music by Eric Matyas
https://www.soundimage.org
Track title: Riding Sky Waves v001
--
Chapters
00:00 Question
00:45 Accepted answer (Score 8)
01:41 Thank you
--
Full question
https://stackoverflow.com/questions/2592...
Question links:
[lu_factor]: http://docs.scipy.org/doc/scipy-0.13.0/r...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #numpy #scipy
#avk47
--
Music by Eric Matyas
https://www.soundimage.org
Track title: Riding Sky Waves v001
--
Chapters
00:00 Question
00:45 Accepted answer (Score 8)
01:41 Thank you
--
Full question
https://stackoverflow.com/questions/2592...
Question links:
[lu_factor]: http://docs.scipy.org/doc/scipy-0.13.0/r...
--
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):
- Row 0 changes with Row 1
- The new Row 1 changes with Row 2 and
- 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