As a newcomer to Eigen there is something I am battling to come to terms with.
With matrix multiplication Eigen creates a temporary by default to avoid aliasing issues:
matA = matA * matA; // works fine (Eigen creates a temporary before assigning)
If it is safe to assume no aliasing, we can use .noalias()
to allow for optimization:
matB = matA * matA; // sub-optimal (due to unnecessary temporary)
matB.noalias() = matA * matA; // more efficient
So Eigen here by default avoids making the unsafe assumption unless it is explicitly told that it is safe to assume no aliasing. So far so good.
However, for many other expressions, such as a = a.transpose()
Eigen makes the unsafe assumption (that there are no aliasing issues) by default, and needs explicit intervention to avoid that unsafe assumption:
a = a.transpose().eval(); // or alternatively: a.transposeInPlace()
So if we are concerned about efficiency, it is not sufficient to be careful when there is potential aliasing, and it is not sufficient to be careful when there is no potential aliasing. We have to be careful both when there is potential aliasing, and when there is no potential aliasing, and decide whether a special intervention is warranted, based on whether the expression involves matrix multiplication or not. Is there some design rationale in Eigen for this "mixture of default expectations" in Eigen's interface?