Obtain the same eigenvectors as matlab ones

Viewed 120

I am working on an application which is a translation from Matlab to C/C++ and so I need the same outputs. The problem is that I tried to use the Eigen library to replace the Matlab eig command. I obtain different eigenvectors but the same eigenvalues.

C/C++

#include <iostream>
#include <Eigen/Eigenvalues> 
 
using namespace Eigen;
 
int main(){  
  MatrixXd A(4,4);
  A << 0.680375, 0.823295, -0.444451, -0.270431, 
    -0.211234, -0.604897, 0.10794, 0.0268018,
    0.566198, -0.329554, -0.0452059, 0.904459,
    0.59688, 0.536459, 0.257742, 0.83239;
  EigenSolver<MatrixXd> es(A);
  
  std::cout << "The matrix A is:\n" << A << "\n\n";
  std::cout << "Eigenvectors:\n" << es.eigenvectors() << "\n";
    std::cout << "Eigenvalues:\n" << es.eigenvalues() << "\n";
}

Output

The matrix A is:
  0.680375   0.823295  -0.444451  -0.270431
 -0.211234  -0.604897    0.10794  0.0268018
  0.566198  -0.329554 -0.0452059   0.904459
   0.59688   0.536459   0.257742    0.83239

Eigenvectors:
     (0.349378,0.540657)     (0.349378,-0.540657)   (-0.0377612,-0.222364)    (-0.0377612,0.222364)
 (-0.0630065,-0.0993635)   (-0.0630065,0.0993635)  (-0.179376,0.000710941) (-0.179376,-0.000710941)
    (0.313002,-0.372126)      (0.313002,0.372126)    (-0.594826,-0.663137)     (-0.594826,0.663137)
     (0.25223,-0.521263)       (0.25223,0.521263)      (0.212016,0.280058)     (0.212016,-0.280058)
Eigenvalues:
   (0.754819,0.527518)
  (0.754819,-0.527518)
 (-0.323488,0.0964573)
(-0.323488,-0.0964573)

Matlab

A=[0.680375 0.823295 -0.444451 -0.270431; -0.211234 -0.604897 0.10794 0.0268018; 0.566198 -0.329554 -0.0452059 0.904459; ...
0.59688 0.536459 0.257742 0.83239];
[eig_vectors, eig_value] = eig(A);

Output

eig_vectors =

   0.6437 + 0.0000i   0.6437 + 0.0000i   0.1907 + 0.1204i   0.1907 - 0.1204i
  -0.1177 - 0.0010i  -0.1177 + 0.0010i   0.1192 - 0.1340i   0.1192 + 0.1340i
  -0.1427 - 0.4649i  -0.1427 + 0.4649i   0.8908 + 0.0000i   0.8908 + 0.0000i
  -0.3009 - 0.4948i  -0.3009 + 0.4948i  -0.3500 - 0.0292i  -0.3500 + 0.0292i


eig_value =

   0.7548 + 0.5275i   0.0000 + 0.0000i   0.0000 + 0.0000i   0.0000 + 0.0000i
   0.0000 + 0.0000i   0.7548 - 0.5275i   0.0000 + 0.0000i   0.0000 + 0.0000i
   0.0000 + 0.0000i   0.0000 + 0.0000i  -0.3235 + 0.0965i   0.0000 + 0.0000i
   0.0000 + 0.0000i   0.0000 + 0.0000i   0.0000 + 0.0000i  -0.3235 - 0.0965i

Now, I know that eigenvectors are not unique, but I need to have the same output. Could it be possible ?

0 Answers
Related