I've run into trouble with a matrix-by-vector multiplication. Namely an innocous-looking expression always return a vector close to [1 0] while a similar-looking expression returns the correct result:
// version WITH temp var, correct
Eigen::Vector3d lcoord_eig(lcoord[0], lcoord[1], lcoord[2]);
auto lcoord2d = P3to2 * lcoord_eig;
std::cout << std::endl << lcoord2d << std::endl;
// version WITHOUT temp var, always [1 0]
auto lcoord2d_2 = P3to2 * Eigen::Vector3d(lcoord[0], lcoord[1], lcoord[2]);
std::cout << std::endl << lcoord2d_2 << std::endl;
where P3to2
is a 2-by-3 matrix (Eigen::MatrixXd
) and lcoord
is the 3d vector type of some other library, with above code contained in a for-loop.
Some of the output (annotations by me):
-0.0036135
2.1684e-18 // correct
1
0 // [1 0], wrong
0.00209583
0.000388139 // correct
1
5.55112e-17 // [1 0], wrong
0.00148429
-0.000435008 // correct
1
5.55112e-17 // [1 0], wrong
This error took me a long time to spot, and I still don't understand what is going on that would cause the second version to behave like this. What mistake did I make?
Edit: This also happens for constant vectors, like Eigen::Vector3d(.5,.5,.5)