See also Eigenvalues module.
#include <eigen3/Eigen/Eigenvalues> // header file
#include <iostream>
int main(){
Eigen::Matrix<double, 2, 2> A; // declare a real (double) 2x2 matrix
A << 0, 2, 1, 0; // defined the matrix A
Eigen::EigenSolver<Eigen::Matrix<double, 2,2> > s(A); // the instance s(A) includes the eigensystem
std::cout << A << std::endl;
std::cout << "eigenvalues:" << std::endl;
std::cout << s.eigenvalues() << std::endl;
std::cout << "eigenvectors=" << std::endl;
std::cout << s.eigenvectors() << std::endl;
return(0);
}
On Ubuntu 14.04, the header file should be included as <eigen3/Eigen/Eigenvalues>, but "eigen3" may be skipped in other platforms as <Eigen/Eigenvalues>.
The definition of the matrix A is also expressed as the normal matrix as
#include <eigen3/Eigen/Eigenvalues> // header file
#include <iostream>
int main(){
Eigen::Matrix<double, 2, 2> A; // declare a real (double) 2x2 matrix
A(0,0) = 0.0;
A(0,1) = 2.0;
A(1,0) = 1.0;
A(1,1) = 0.0;
Eigen::EigenSolver<Eigen::Matrix<double, 2,2> > s(A); // the instance s(A) includes the eigensystem
std::cout << A << std::endl;
std::cout << "eigenvalues:" << std::endl;
std::cout << s.eigenvalues() << std::endl;
std::cout << "eigenvectors=" << std::endl;
std::cout << s.eigenvectors() << std::endl;
l;
return(0);
}
Then, compile the source file and execute.
The 1st and 2nd arguments of each (.,.) are the real and imaginary parts.
Thus, the eigenvalues and the corresponding eigenvectors are:
| Eigenvalue | Eigenvector |
|---|---|
| 1.41421 | (0.816497,0.57735) |
| -1.41421 | (-0.816497,0.57735) |
Note: The eigenvectors are neither (0.816497,-0.816497) nor (0.57735,0.57735) !
| Eigenvalue | Eigenvector |
|---|---|
| i | (0.707107, i*0.707107) ~ (1,i) |
| -i | (0.707107, -i*0.707107) ~ (1,-i) |
#include <eigen3/Eigen/Eigenvalues> // header file
#include <complex>
#include <iostream>
int main(){
std::complex<double> I(0.0, 1.0); // imaginary unit
Eigen::Matrix<std::complex<double>, 2, 2> A; // declare a real (double) 2x2 matrix
A << 0.0, I, -I, 0.0; // defined the matrix A
Eigen::ComplexEigenSolver<Eigen::Matrix<std::complex<double>, 2,2> > s(A); // the instance s(A) includes the eigensystem
std::cout << A << std::endl;
std::cout << "eigenvalues:" << std::endl;
std::cout << s.eigenvalues() << std::endl;
std::cout << "eigenvectors=" << std::endl;
std::cout << s.eigenvectors() << std::endl;
return(0);
}
| Eigenvalue | Eigenvector |
|---|---|
| 1 | (0.707107,-i*0.707107) ~ (1,-i) |
| -1 | (0.707107,i*0.707107) ~ (1,i) |
#include <eigen3/Eigen/Eigenvalues> // header file
#include <complex>
#include <iostream>
int main(){
std::complex<double> I(0.0, 1.0); // imaginary unit
Eigen::Matrix<std::complex<double>, 2, 2> A; // declare a real (double) 2x2 matrix
A << 0.0, I, -I, 0.0; // defined the matrix A
Eigen::ComplexEigenSolver<Eigen::Matrix<std::complex<double>, 2,2> > s(A); // the instance s(A) includes the eigensystem
std::cout << "The 1st eigenvalue and eigenvector" << std::endl;
std::cout << real(s.eigenvalues()(0)) << " " << imag(s.eigenvalues()(0)) << std::endl;
std::cout << real(s.eigenvectors()(0,0)) << " " << imag(s.eigenvectors()(0,0)) << std::endl;
std::cout << real(s.eigenvectors()(1,0)) << " " << imag(s.eigenvectors()(1,0)) << std::endl;
std::cout << "The 2nd eigenvalue and eigenvector" << std::endl;
std::cout << real(s.eigenvalues()(1)) << " " << imag(s.eigenvalues()(1)) << std::endl;
std::cout << real(s.eigenvectors()(0,1)) << " " << imag(s.eigenvectors()(0,1)) << std::endl;
std::cout << real(s.eigenvectors()(1,1)) << " " << imag(s.eigenvectors()(1,1)) << std::endl;
return(0);
}
The result is as follows: