user189035
user189035

Reputation: 5789

symmetric matrices in eigen

I have a lower triangular matrix M (strict, with 0 on the diagonal). I want to turn this unto a symmetric matrix, efficiently. (e.g. i want to do M<-M+M'). I'm using Eigen.

My problem, is i'm doing:

U=U+U.transpose();

but reading the docs i have the feeling that, perhaps, i should be taking advantage of some functions such as .noalias() and/or .transposeInPlace(), but the obvious candidate:

U+=U.transposeInPlace();

gives an error.

EDIT:

here is the error message:

.cpp:210:24: note: candidates are:
/eigen/Eigen/src/Core/MatrixBase.h:183:14: note: template<class OtherDerived> Derived& Eigen::MatrixBase::operator+=(const Eigen::MatrixBase<OtherDerived>&) [with OtherDerived = OtherDerived, Derived = Eigen::Matrix<float, -0x00000000000000001, -0x00000000000000001>]
/eigen/Eigen/src/Core/MatrixBase.h:517:46: note: template<class OtherDerived> Derived& Eigen::MatrixBase::operator+=(const Eigen::ArrayBase<OtherDerived>&) [with OtherDerived = OtherDerived, Derived = Eigen::Matrix<float, -0x00000000000000001, -0x00000000000000001>]
/eigen/Eigen/src/Core/DenseBase.h:266:14: note: template<class OtherDerived> Derived& Eigen::DenseBase::operator+=(const Eigen::EigenBase<OtherDerived>&) [with OtherDerived = OtherDerived, Derived = Eigen::Matrix<float, -0x00000000000000001, -0x00000000000000001>]

Upvotes: 1

Views: 2490

Answers (2)

user189035
user189035

Reputation: 5789

Actually,

U+=U.transpose().eval();
return(U);

does the trick

Upvotes: 2

stefan
stefan

Reputation: 10345

in Eigen, transposeInPlace() is declared as void. Thus, you can't use the result of that method in a sum of matrices, because the result simply isn't a matrix.

Do

V = U;
V.transposeInPlace();
U += V;

instead.

Upvotes: 3

Related Questions