2

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

user62039
  • 371
  • 2
  • 15
  • Is A symmetric? If not, you may not add A and its transpose. In other words you may add two matrices, mxn and nxm if and only if m==n. I've also never seen a single formula where A + A^T is performed. Are you trying A^T x A, which makes a lot of sense at times. – Kaveh Vahedipour Sep 03 '17 at 18:13

1 Answers1

4

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())));
chtz
  • 17,329
  • 4
  • 26
  • 56
  • Thanks! Do you think doing this way will optimise away the temporary transpose? – user62039 Sep 08 '17 at 06:42
  • It does not get rid of the temporary -- and I don't think there is an efficient algorithm to add sparse matrices with different storage order (maybe unless you can exploit some special structure of the matrix). – chtz Sep 18 '17 at 20:13