This forum has been archived. All content is frozen. Please use KDE Discuss instead.

[EigenSolver] cast result of eigenvalues/vectors to real

Tags: None
(comma "," separated)
rtrepos
Registered Member
Posts
11
Karma
0
Hello,

Is it possible to cast the result of eigen solvers to real (double or float) ?
For example, in the above example, m_solved_val if of type EigenvalueType
which is a matrix of complex numbers. But it appears that the matrix contains only real numbers.

For your information, this is the example of function "eigen" in R.

Best Regards,
R. Trépos


Code: Select all
      Matrix3d m;
      m << 1, 3, 1,
            1, 2, 2,
            1, 1, 3;

      EigenSolver<Matrix3d> m_solve(m);

      EigenSolver<Matrix3d>::EigenvalueType m_solved_val(m_solve.eigenvalues());
      EigenSolver<Matrix3d>::EigenvectorType m_solved_vec = m_solve.eigenvectors();

User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
you can simply do:

VectorXd m_solved_val = m_solve.eigenvalues().cast<double>();

also note that the eigen solver m_solve already stores the eigenvalues/vectors for you, so you can directly use them in expressions without overhead, e.g.:

smallest_abs_eigenvalue = m_solve.eigenvalues().cwise().abs().minCoeff();
rtrepos
Registered Member
Posts
11
Karma
0
Thanks you for your answer
However, with this line :

VectorXd m_solved_val_d = m_solve.eigenvalues().cast<double>();


I have the compile error :

...
/home/rtrepos/usr/src/vle_dev/eigen_utils/src/test_eigen_utils.cpp:77: instantiated from here
/usr/include/eigen2/Eigen/src/Core/Functors.h:225: error: invalid static_cast from type ‘const std::complex<double>’ to type ‘double’
make[2]: *** [src/CMakeFiles/test_eigen_utils.dir/test_eigen_utils.cpp.o] Erreur 1
make[1]: *** [src/CMakeFiles/test_eigen_utils.dir/all] Erreur 2

Could this be a problem of a "const" ?
Hauke
Registered Member
Posts
109
Karma
3
OS
For the moment I think you can try
Code: Select all
m_solve.pseudoEigenvalueMatrix().diagonal()

In the future it would be great if we were able to cast from complex<double> to double by stripping the imaginary part.

- Hauke
rtrepos
Registered Member
Posts
11
Karma
0
Ok it works,
thank you for your time..
I have other questions but I have to create new topics for that :-)
R. Trépos
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
sorry I'm stupid ! Actually you can already strip the imaginary part:

m_solve.eigenvalues().real();

that's faster than constructing the "pseudoEigenvalueMatrix".


Bookmarks



Who is online

Registered users: Bing [Bot], daret, Google [Bot], sandyvee, Sogou [Bot]