I am trying to get a random symmetric matrix with a library of linear algebras C ++ Eigen3. I do it like this:
Eigen::MatrixXd m(3, 3);
m.setRandom();
m = 0.5 * (m + m.transpose());
But the result is completely wrong. But if I do not rewrite the variable m and just print it to the console as follows:
Eigen::MatrixXd m(3, 3);
m.setRandom();
cout << 0.5 * (m + m.transpose()) << endl;
Everything seems to be working fine. I do not understand what the problem is. Is this because methods such as transpose and operations of type * and + do not create a new matrix, but instead do it in a lazy way and keep a reference to the matrix m? But how then can I find out from official documentation? And isnβt behavior like this in most cases prone to errors?
UPDATE: Yes, I think my guess about lazy computing is correct. It is mentioned in the documentation of the method transpose:
, , , ? , .eval()? , .