Reputation: 47
As the title says, I am trying to convert an Eigen::VectorXd to an std::vector. The vector is obtained by diagonalizing a matrix and then taking the first eigenvector of the process.
#include <iostream>
#include <vector>
#include <cmath>
#include <iomanip>
#include <Eigen/Dense>
using namespace std;
using namespace Eigen;
using namespace std;
int main()
{
vector<double> vec;
MatrixXd ones = MatrixXd::Ones(3,3);
VectorXd firstEvector;
SelfAdjointEigenSolver<MatrixXd> es(ones);
firstEvector = es.eigenvectors().col(1);
cout << "The first eigenvector of the 3x3 matrix of ones is:" << endl << firstEvector << endl;
vec(&firstEvector[0], firstEvector.data() + firstEvector.cols()*firstEvector.rows());
return 0;
}
I thought this was the way to do it, however, it doesn't work. I get the following error.
C:\CBProjects\eigenVectors\main.cpp|20|error: no match for call to '(std::vector<double>) (Eigen::DenseCoeffsBase<Eigen::Matrix<double, -1, 1>, 1>::Scalar*, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 1> >::Scalar*)'|
I can't see what I am doing wrong. Any help would be greatly appreciated.
Upvotes: 3
Views: 4035
Reputation: 23788
You are using a constructor syntax for the std::vector
vec
although the vector was already declared. That doesn't work.
There are two efficient possibilities to copy the content of an Eigen::VectorXd
into a std::vector<double>
. You can either construct a new std::vector
vec2
, like this:
vector<double> vec2(firstEvector.data(), firstEvector.data() + firstEvector.size());
or use the previously declared vector vec
and transfer the data from the Eigen::VectorXd
by using Eigen::Map
. But in that case the std::vector
first needs to be resized:
vec.resize(firstEvector.size());
Map<VectorXd>(vec.data(), vec.size()) = firstEvector;
Upvotes: 6