Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cannot convert from 'const Eigen::GeneralProduct<Lhs,Rhs,ProductType>' to 'double'

Tags:

c++

eigen3

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?

like image 599
CroCo Avatar asked Aug 03 '14 17:08

CroCo


2 Answers

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.

like image 149
vsoftco Avatar answered Sep 22 '22 10:09

vsoftco


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;
}
like image 3
ggael Avatar answered Sep 23 '22 10:09

ggael