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

1.3k views Asked by At

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++?

1

There are 1 answers

2
chtz On

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())));