# How can I find the pivot matrix from LU-factorization?

I trying to solve LU-factrization with pivoting:

$$PA=LU$$

By using the subroutine sgeft2 from Lapack. It's a Fortran 90 library for numerical linear algebra.

I have found the $$L$$ and $$U$$ matrix, but not found the $$P$$ matrix, and it's because of the IPIV argument.

IPIV is INTEGER array, dimension (min(M,N))
The pivot indices; for 1 <= i <= min(M,N), row i of the
matrix was interchanged with row IPIV(i).


I interprent this text as I should have a identity matrix $$P \in \Re^{nxn}$$. Then I should move the rows of $$P$$ depening on what the vector $$IPIV$$ says. Is this correct? I have tried different ways to model some MATLAB code, but not succeed to find the $$P$$ matrix.

Example. If I got the matrix $$A$$

0.300000 0.815502 0.632273 0.217150
0.041052 0.756497 0.383632 0.963453
0.840969 0.653643 0.988885 0.256717
0.464724 0.148658 0.405135 0.055045
0.306172 0.033121 0.250060 0.465494


Then I get the $$U$$ matrix

0.840969 0.653643 0.988885 0.256717
0.000000 0.724589 0.335359 0.950921
0.000000 0.000000 -0.042955 0.192122
0.000000 0.000000 0.000000 -0.593969


And $$L$$ matrix

1.000000 0.000000 0.000000 0.000000
0.048815 1.000000 0.000000 0.000000
0.552605 -0.293337 1.000000 0.000000
0.356731 0.803665 -0.232571 1.000000
0.364071 -0.282713 0.352770 -0.964855


I should also get the $$P$$ matrix

   0   0   1   0   0
0   1   0   0   0
0   0   0   1   0
1   0   0   0   0
0   0   0   0   1


But instead, due to $$IPIV$$

3 2 4 4 4


I got my $$P$$ matrix

0.000000 0.000000 0.000000 0.000000 0.000000
0.000000 1.000000 0.000000 0.000000 0.000000
1.000000 0.000000 0.000000 0.000000 0.000000
0.000000 0.000000 1.000000 1.000000 1.000000
0.000000 0.000000 0.000000 0.000000 0.000000


I must have interprented this text above wrong?

• Your text interpretation is correct. IPIV should be used element by element to change the identity into the final permutation. – VorKir Feb 13 '19 at 15:28
• @VorKir But it won't give the correct result. – Daniel Mårtensson Feb 13 '19 at 15:36

IPIV should be processed in the following “recursive manner”. Start with identity permutation 1 2 3 4 5.

1) IPIV(1)=3 means that you interchange first and third elements. Your permutation now is 3 2 1 4 5.

2) IPIV(2)=2 means you interchange the current second element with itself. So, the result is again 3 2 1 4 5.

3) Result is 3 2 4 1 5

4) Result is 3 2 4 1 5

5) Result is 3 2 4 5 1

Looks that it doesn’t agree with the correct answer. Are you sure your IPIV doesn’t have 5 as the last element?

• Yes. Very sure. Can you try the library with C code? Else, i can upload some C code, but you need Eclipse compiler for that. – Daniel Mårtensson Feb 14 '19 at 9:32
• Do you want to have a source code example? – Daniel Mårtensson Feb 14 '19 at 15:50