Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Eigen: why can one add-assign but not add for the method asDiagonal()

Tags:

c++

eigen

I've noticed some unexpected behaviour in Eigen:

int n=10;  //value is irrelevant 
Eigen::MatrixXd A(n);
Eigen::VectorXd v(n);

//works:
Eigen::MatrixXd B = A;
B += v.asDiagonal();

//error:
Eigen::MatrixXd C = A + v.asDiagonal();

In the second case, the compiler complains that there is no suitable operator+ availaible taking a MatrixXd and a DiagonalWrapper<...>. (The same holds for other operators as well).

Is this intended? And if yes, is there a neat way around the two-line alternative (first assign, then subtract)?

like image 213
davidhigh Avatar asked Oct 25 '25 04:10

davidhigh


1 Answers

Unfortunately that seems to be the expected behaviour (probably due to optimisation reasons, but it's just a guess). You can, however, do the following:

Eigen::MatrixXd C = A + v.asDiagonal().toDenseMatrix();
like image 197
cbuchart Avatar answered Oct 26 '25 19:10

cbuchart



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!