user62039
user62039

Reputation: 371

How to symmetrize a sparse matrix in Eigen C++?

I have a sparse matrix A in Eigen C++. Now I want to symmetrize it to another sparse matrix Asym:

I was hoping that it would be as simple as:

Eigen::SparseMatrix<FLOATDATA> A;
...
Eigen::SparseMatrix<FLOATDATA> Asym = 0.5*(A+A.transpose()); // error here

But due to obvious reasons, it gives the following assert failure error:

error: static assertion failed: THE_STORAGE_ORDER_OF_BOTH_SIDES_MUST_MATCH

My question is how to neatly do the above operation in Eigen C++?

Upvotes: 2

Views: 1419

Answers (1)

chtz
chtz

Reputation: 18809

The easiest way to make your code compile is to evaluate the transposed matrix into a temporary of the correct storage order:

Eigen::SparseMatrix<FLOATDATA> Asym = 0.5*(A+Eigen::SparseMatrix<FLOATDATA>(A.transpose())));

Upvotes: 4

Related Questions