I want to add elements to the diagonal of a Eigen::MatrixXd object with the Eigen3 library (version 3.3.2).
Both for optimisation and being able to use constness, I want to do this by adding a diagonal matrix to the original, like this
const MatrixXd a(2,2); a << 1, 2, 3, 4;
const VectorXd v(2); v << 10, 20;
const MatrixXd b = a + v.asDiagonal();
But this doesn't work: I get a compiler error about there being no operator+
. Adding two MatrixXd
does work, so I would expect it to behave for the diagonal specialisation.
Removing the constness doesn't help. Using statically sized matrices makes no difference, so it's not a dynamic-sizing thing. And explicitly constructing a DiagonalMatrix
rather than using the DiagonalWrapper
returned by asDiagonal()
also gives the same error.
Multiplication is well-formed for these types: MatrixXd c = a * v.asDiagonal();
compiles and runs just fine. Am I doing something wrong, or is operator+(Matrix,DiagonalMatrix)
just missing from the library?