Search code examples
c++vectoreigeneigenvector

How to convert Eigen::eigenvector().col(0).real() to std vector?


I am working on a code in c++ that calulates the eigen vectors of a matrix using the Eigen library. I need only the first column and only the real values of the eigen vector. Is there a way to copy these values to the std::vector data type? Can someone help me with this?

I saw this Converting Eigen::MatrixXf to 2D std::vector post. But I need only the specific values. Moreover, I am not sure what is the type that eigenvector() function returns. In the documentation, it is said as complex Eigen::Matrix type.

This is an example code.

#include<iostream>
#include<Eigen/Eigenvalues>
#include<vector>
using namespace std;

struct eigen
{
    float a, b, c;
};

int main()
{
    vector<Eigen::Matrix3f> A(1);
    A[0] << 1, 2, 3, 2, 4, 5, 3, 5, 6;
    Eigen::EigenSolver<Eigen::Matrix3f> handle(A[0]);
    cout << "The matrix of eigenvectors, V, is: " << endl << handle.eigenvectors() << endl << endl;
    cout << "The real part of first column is : " << endl << 
    handle.eigenvectors().col(0).real() << endl << endl;
    return 0;
}

The output of the above code is

The matrix of eigenvectors, V, is:
 (0.327985,0) (-0.736977,0) (-0.591009,0)
 (0.591009,0) (-0.327985,0)  (0.736976,0)
 (0.736976,0)  (0.591009,0) (-0.327985,0)

The real part of the first column is :
0.327985
0.591009
0.736976

I need to copy the values of handle.eigenvectors().col(0).real() to std::vector<eigen>


Solution

  • Map is the answer:

     Vector3f::Map(&v[0].a) = handle.eigenvectors().col(0).real();