LAPACK: working with permutations of matrices

Introduction : 
There are plenty of functions in LAPACK where one of outputs is a permutation matrix. Such functions are all kinds of LU factorization, QR with pivoting, etc.. In all of the cases the permutation matrix is not stored in it's full form (with unique one's in each column and row filling all other places with zeroes) but as one dimensional array of permutation indexes. This storage form saves memory and operations when the permutation needs to be applied. 

Applying a permutation to a matrix
It could be done with help of ?LASWP, which will permute a supplied matrix according to the permutation.

Restoring full permutation matix
It could be done by applying a permutation to identity (one on diagonal, zeros in other places) matrix. The following sequence of operations could be used:
CALL ?LASET( 'Full', N, N, ZERO, ONE, P, LDP ) ! Init identity matrix in P - ONE on diagonal, ZERO in all other places
CALL ?LASWP( N, P, LDP, 1, N, IPIV, 1 ) ! Permute the identity matrix to obtain the permutation matrix in P
CALL ?GEMM( 'N', 'N', N, N, N, ONE, P, LDP, A, LDA, ZERO, AP, LDA ) !Multiply P*A=AP


For more complete information about compiler optimizations, see our Optimization Notice.