I keep getting this error every time I compute this line
double k = b.transpose()*Z.inverse()*b;
where Eigen::MatrixXd Z(3,3), b(3,1);
. I've tried casting but no luck. Any suggestions?
The result is by default an Eigen expression (think of it as a matrix, technically is a template type called Eigen::GeneralProduct<...>
), so even if the matrix is 1 x 1
, it is not implicitly convertible to double
. What you have to do is to access its (0)
element (or (0,0)
, it makes no difference), see below
Eigen::MatrixXd Z(3,3), b(3,1);
double k = (b.transpose()*Z.inverse()*b)(0);
PS: As mentioned by @ggael, you should use an Eigen::VectorXd
as the type of b
, in which case the result is implicitly convertible to a double
.
This works for me, so make sure that b is declared as a VectorXd
such that Eigen can know at compile-time that result is a 1x1
matrix and so it can be safely converted to a scalar value. Here is a self-contained example:
#include <Eigen/Dense>
using namespace Eigen;
int main() {
int n = 10;
VectorXd b(n);
MatrixXd Z(n,n);
double k = b.transpose() * Z.inverse() * b;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With