This forum has been archived. All content is frozen. Please use KDE Discuss instead.

Best Way to Permute Sparse Matrix?

Tags: None
(comma "," separated)
jbauer
Registered Member
Posts
25
Karma
0

Best Way to Permute Sparse Matrix?

Wed Oct 14, 2015 8:45 pm
I'm trying to figure out the 'best' (most efficient) way to permute a sparse matrix in Eigen given a permutation stored as a vector. For example, I currently have:

Code: Select all
// Permutation comes from somewhere (such as Cuthill-McKee reordering)
Eigen::VectorXi permutation = ...;
// Some matrix that we want to permute
Eigen::SparseMatrix<double> matrix_to_permute = ...;
// The result of the permutation
Eigen::SparseMatrix<double> output_matrix;
// Do the permutation
( matrix_to_permute.twistedBy( Eigen::PermutationMatrix<Eigen::Dynamic,Eigen::Dynamic,int>{ permutation }.inverse() ) ).evalTo( output_matrix );


Is this the proper 'Eigen' way to permute a sparse matrix? Will PermutationMatrix respect the sparsity (that is, it won't evaluate to a dense matrix here, will it)?

Thank you for your advices and tips.
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
You can avoid the copy using the asPermutation() method returning a permutation expression, and please do not use evalTo() which is rather an internal thing:

Code: Select all
out_mat = mat.twistedBy( perm_vec.asPermutation().inverse() );


And yes, of course this will both exploit and preserve the sparsity.
jbauer
Registered Member
Posts
25
Karma
0
Many thanks, the code you have suggested works perfectly.


Bookmarks



Who is online

Registered users: Bing [Bot], Google [Bot], q.ignora, watchstar