Eigen  3.2.9
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
EigenSolver< _MatrixType > Class Template Reference

Detailed Description

template<typename _MatrixType>
class Eigen::EigenSolver< _MatrixType >

Computes eigenvalues and eigenvectors of general matrices.

This is defined in the Eigenvalues module.

#include <Eigen/Eigenvalues>
Template Parameters
_MatrixTypethe type of the matrix of which we are computing the eigendecomposition; this is expected to be an instantiation of the Matrix class template. Currently, only real matrices are supported.

The eigenvalues and eigenvectors of a matrix $ A $ are scalars $ \lambda $ and vectors $ v $ such that $ Av = \lambda v $. If $ D $ is a diagonal matrix with the eigenvalues on the diagonal, and $ V $ is a matrix with the eigenvectors as its columns, then $ A V = V D $. The matrix $ V $ is almost always invertible, in which case we have $ A = V D V^{-1} $. This is called the eigendecomposition.

The eigenvalues and eigenvectors of a matrix may be complex, even when the matrix is real. However, we can choose real matrices $ V $ and $ D $ satisfying $ A V = V D $, just like the eigendecomposition, if the matrix $ D $ is not required to be diagonal, but if it is allowed to have blocks of the form

\[ \begin{bmatrix} u & v \\ -v & u \end{bmatrix} \]

(where $ u $ and $ v $ are real numbers) on the diagonal. These blocks correspond to complex eigenvalue pairs $ u \pm iv $. We call this variant of the eigendecomposition the pseudo-eigendecomposition.

Call the function compute() to compute the eigenvalues and eigenvectors of a given matrix. Alternatively, you can use the EigenSolver(const MatrixType&, bool) constructor which computes the eigenvalues and eigenvectors at construction time. Once the eigenvalue and eigenvectors are computed, they can be retrieved with the eigenvalues() and eigenvectors() functions. The pseudoEigenvalueMatrix() and pseudoEigenvectors() methods allow the construction of the pseudo-eigendecomposition.

The documentation for EigenSolver(const MatrixType&, bool) contains an example of the typical use of this class.

Note
The implementation is adapted from JAMA (public domain). Their code is based on EISPACK.
See Also
MatrixBase::eigenvalues(), class ComplexEigenSolver, class SelfAdjointEigenSolver

Public Types

typedef std::complex< RealScalar > ComplexScalar
 Complex scalar type for MatrixType. More...
 
typedef Matrix< ComplexScalar,
ColsAtCompileTime, 1, Options
&~RowMajor,
MaxColsAtCompileTime, 1 > 
EigenvalueType
 Type for vector of eigenvalues as returned by eigenvalues(). More...
 
typedef Matrix< ComplexScalar,
RowsAtCompileTime,
ColsAtCompileTime, Options,
MaxRowsAtCompileTime,
MaxColsAtCompileTime > 
EigenvectorsType
 Type for matrix of eigenvectors as returned by eigenvectors(). More...
 
typedef _MatrixType MatrixType
 Synonym for the template parameter _MatrixType.
 
typedef MatrixType::Scalar Scalar
 Scalar type for matrices of type MatrixType.
 

Public Member Functions

EigenSolvercompute (const MatrixType &matrix, bool computeEigenvectors=true)
 Computes eigendecomposition of given matrix. More...
 
 EigenSolver ()
 Default constructor. More...
 
 EigenSolver (Index size)
 Default constructor with memory preallocation. More...
 
 EigenSolver (const MatrixType &matrix, bool computeEigenvectors=true)
 Constructor; computes eigendecomposition of given matrix. More...
 
const EigenvalueTypeeigenvalues () const
 Returns the eigenvalues of given matrix. More...
 
EigenvectorsType eigenvectors () const
 Returns the eigenvectors of given matrix. More...
 
Index getMaxIterations ()
 Returns the maximum number of iterations.
 
MatrixType pseudoEigenvalueMatrix () const
 Returns the block-diagonal matrix in the pseudo-eigendecomposition. More...
 
const MatrixTypepseudoEigenvectors () const
 Returns the pseudo-eigenvectors of given matrix. More...
 
EigenSolversetMaxIterations (Index maxIters)
 Sets the maximum number of iterations allowed.
 

Member Typedef Documentation

typedef std::complex<RealScalar> ComplexScalar

Complex scalar type for MatrixType.

This is std::complex<Scalar> if Scalar is real (e.g., float or double) and just Scalar if Scalar is complex.

typedef Matrix<ComplexScalar, ColsAtCompileTime, 1, Options & ~RowMajor, MaxColsAtCompileTime, 1> EigenvalueType

Type for vector of eigenvalues as returned by eigenvalues().

This is a column vector with entries of type ComplexScalar. The length of the vector is the size of MatrixType.

typedef Matrix<ComplexScalar, RowsAtCompileTime, ColsAtCompileTime, Options, MaxRowsAtCompileTime, MaxColsAtCompileTime> EigenvectorsType

Type for matrix of eigenvectors as returned by eigenvectors().

This is a square matrix with entries of type ComplexScalar. The size is the same as the size of MatrixType.

Constructor & Destructor Documentation

EigenSolver ( )
inline

Default constructor.

The default constructor is useful in cases in which the user intends to perform decompositions via EigenSolver::compute(const MatrixType&, bool).

See Also
compute() for an example.
EigenSolver ( Index  size)
inline

Default constructor with memory preallocation.

Like the default constructor but with preallocation of the internal data according to the specified problem size.

See Also
EigenSolver()
EigenSolver ( const MatrixType matrix,
bool  computeEigenvectors = true 
)
inline

Constructor; computes eigendecomposition of given matrix.

Parameters
[in]matrixSquare matrix whose eigendecomposition is to be computed.
[in]computeEigenvectorsIf true, both the eigenvectors and the eigenvalues are computed; if false, only the eigenvalues are computed.

This constructor calls compute() to compute the eigenvalues and eigenvectors.

Example:

MatrixXd A = MatrixXd::Random(6,6);
cout << "Here is a random 6x6 matrix, A:" << endl << A << endl << endl;
EigenSolver<MatrixXd> es(A);
cout << "The eigenvalues of A are:" << endl << es.eigenvalues() << endl;
cout << "The matrix of eigenvectors, V, is:" << endl << es.eigenvectors() << endl << endl;
complex<double> lambda = es.eigenvalues()[0];
cout << "Consider the first eigenvalue, lambda = " << lambda << endl;
VectorXcd v = es.eigenvectors().col(0);
cout << "If v is the corresponding eigenvector, then lambda * v = " << endl << lambda * v << endl;
cout << "... and A * v = " << endl << A.cast<complex<double> >() * v << endl << endl;
MatrixXcd D = es.eigenvalues().asDiagonal();
MatrixXcd V = es.eigenvectors();
cout << "Finally, V * D * V^(-1) = " << endl << V * D * V.inverse() << endl;

Output:

Here is a random 6x6 matrix, A:
   0.68   -0.33   -0.27  -0.717  -0.687  0.0259
 -0.211   0.536  0.0268   0.214  -0.198   0.678
  0.566  -0.444   0.904  -0.967   -0.74   0.225
  0.597   0.108   0.832  -0.514  -0.782  -0.408
  0.823 -0.0452   0.271  -0.726   0.998   0.275
 -0.605   0.258   0.435   0.608  -0.563  0.0486

The eigenvalues of A are:
  (0.049,1.06)
 (0.049,-1.06)
     (0.967,0)
     (0.353,0)
 (0.618,0.129)
(0.618,-0.129)
The matrix of eigenvectors, V, is:
 (-0.292,-0.454)   (-0.292,0.454)      (-0.0607,0)       (-0.733,0)    (0.59,-0.122)     (0.59,0.122)
  (0.134,-0.104)    (0.134,0.104)       (-0.799,0)        (0.136,0)    (0.335,0.368)   (0.335,-0.368)
  (-0.422,-0.18)    (-0.422,0.18)        (0.192,0)       (0.0563,0)  (-0.335,-0.143)   (-0.335,0.143)
 (-0.589,0.0274) (-0.589,-0.0274)      (-0.0788,0)       (-0.627,0)   (0.322,-0.156)    (0.322,0.156)
  (-0.248,0.132)  (-0.248,-0.132)        (0.401,0)        (0.218,0)  (-0.335,-0.076)   (-0.335,0.076)
    (0.105,0.18)    (0.105,-0.18)       (-0.392,0)     (-0.00564,0)  (-0.0324,0.103) (-0.0324,-0.103)

Consider the first eigenvalue, lambda = (0.049,1.06)
If v is the corresponding eigenvector, then lambda * v = 
  (0.466,-0.331)
   (0.117,0.137)
   (0.17,-0.456)
(-0.0578,-0.622)
 (-0.152,-0.256)
   (-0.186,0.12)
... and A * v = 
  (0.466,-0.331)
   (0.117,0.137)
   (0.17,-0.456)
(-0.0578,-0.622)
 (-0.152,-0.256)
   (-0.186,0.12)

Finally, V * D * V^(-1) = 
    (0.68,1.53e-16)   (-0.33,-1.84e-18)   (-0.27,-2.88e-16)  (-0.717,-1.94e-16)  (-0.687,-3.92e-16)  (0.0259,-5.53e-16)
 (-0.211,-1.11e-16)    (0.536,9.71e-17)   (0.0268,6.94e-17)           (0.214,0)   (-0.198,5.55e-16)    (0.678,2.22e-16)
  (0.566,-3.89e-16)  (-0.444,-1.11e-16)     (0.904,5.2e-17)  (-0.967,-1.11e-16)   (-0.74,-6.66e-16)   (0.225,-8.88e-16)
  (0.597,-3.33e-16)   (0.108,-1.42e-16)   (0.832,-5.72e-17)   (-0.514,5.55e-17)  (-0.782,-1.39e-15)  (-0.408,-1.11e-15)
   (0.823,-2.5e-16) (-0.0452,-1.73e-16)     (0.271,1.3e-16)   (-0.726,2.78e-17)   (0.998,-1.03e-15)   (0.275,-5.55e-16)
 (-0.605,-8.33e-17)    (0.258,8.67e-17)    (0.435,4.16e-17)    (0.608,1.39e-17)   (-0.563,4.44e-16)   (0.0486,5.55e-17)
See Also
compute()

References EigenSolver< _MatrixType >::compute().

Member Function Documentation

EigenSolver< MatrixType > & compute ( const MatrixType matrix,
bool  computeEigenvectors = true 
)

Computes eigendecomposition of given matrix.

Parameters
[in]matrixSquare matrix whose eigendecomposition is to be computed.
[in]computeEigenvectorsIf true, both the eigenvectors and the eigenvalues are computed; if false, only the eigenvalues are computed.
Returns
Reference to *this

This function computes the eigenvalues of the real matrix matrix. The eigenvalues() function can be used to retrieve them. If computeEigenvectors is true, then the eigenvectors are also computed and can be retrieved by calling eigenvectors().

The matrix is first reduced to real Schur form using the RealSchur class. The Schur decomposition is then used to compute the eigenvalues and eigenvectors.

The cost of the computation is dominated by the cost of the Schur decomposition, which is very approximately $ 25n^3 $ (where $ n $ is the size of the matrix) if computeEigenvectors is true, and $ 10n^3 $ if computeEigenvectors is false.

This method reuses of the allocated data in the EigenSolver object.

Example:

EigenSolver<MatrixXf> es;
es.compute(A, /* computeEigenvectors = */ false);
cout << "The eigenvalues of A are: " << es.eigenvalues().transpose() << endl;
es.compute(A + MatrixXf::Identity(4,4), false); // re-use es to compute eigenvalues of A+I
cout << "The eigenvalues of A+I are: " << es.eigenvalues().transpose() << endl;

Output:

The eigenvalues of A are:    (0.755,0.528)   (0.755,-0.528)  (-0.323,0.0965) (-0.323,-0.0965)
The eigenvalues of A+I are:    (1.75,0.528)   (1.75,-0.528)  (0.677,0.0965) (0.677,-0.0965)

References Eigen::Success.

Referenced by EigenSolver< _MatrixType >::EigenSolver().

const EigenvalueType& eigenvalues ( ) const
inline

Returns the eigenvalues of given matrix.

Returns
A const reference to the column vector containing the eigenvalues.
Precondition
Either the constructor EigenSolver(const MatrixType&,bool) or the member function compute(const MatrixType&, bool) has been called before.

The eigenvalues are repeated according to their algebraic multiplicity, so there are as many eigenvalues as rows in the matrix. The eigenvalues are not sorted in any particular order.

Example:

MatrixXd ones = MatrixXd::Ones(3,3);
EigenSolver<MatrixXd> es(ones, false);
cout << "The eigenvalues of the 3x3 matrix of ones are:"
<< endl << es.eigenvalues() << endl;

Output:

The eigenvalues of the 3x3 matrix of ones are:
(-5.31e-17,0)
        (3,0)
        (0,0)
See Also
eigenvectors(), pseudoEigenvalueMatrix(), MatrixBase::eigenvalues()
EigenSolver< MatrixType >::EigenvectorsType eigenvectors ( ) const

Returns the eigenvectors of given matrix.

Returns
Matrix whose columns are the (possibly complex) eigenvectors.
Precondition
Either the constructor EigenSolver(const MatrixType&,bool) or the member function compute(const MatrixType&, bool) has been called before, and computeEigenvectors was set to true (the default).

Column $ k $ of the returned matrix is an eigenvector corresponding to eigenvalue number $ k $ as returned by eigenvalues(). The eigenvectors are normalized to have (Euclidean) norm equal to one. The matrix returned by this function is the matrix $ V $ in the eigendecomposition $ A = V D V^{-1} $, if it exists.

Example:

MatrixXd ones = MatrixXd::Ones(3,3);
EigenSolver<MatrixXd> es(ones);
cout << "The first eigenvector of the 3x3 matrix of ones is:"
<< endl << es.eigenvectors().col(1) << endl;

Output:

The first eigenvector of the 3x3 matrix of ones is:
(0.577,0)
(0.577,0)
(0.577,0)
See Also
eigenvalues(), pseudoEigenvectors()
MatrixType pseudoEigenvalueMatrix ( ) const

Returns the block-diagonal matrix in the pseudo-eigendecomposition.

Returns
A block-diagonal matrix.
Precondition
Either the constructor EigenSolver(const MatrixType&,bool) or the member function compute(const MatrixType&, bool) has been called before.

The matrix $ D $ returned by this function is real and block-diagonal. The blocks on the diagonal are either 1-by-1 or 2-by-2 blocks of the form $ \begin{bmatrix} u & v \\ -v & u \end{bmatrix} $. These blocks are not sorted in any particular order. The matrix $ D $ and the matrix $ V $ returned by pseudoEigenvectors() satisfy $ AV = VD $.

See Also
pseudoEigenvectors() for an example, eigenvalues()
const MatrixType& pseudoEigenvectors ( ) const
inline

Returns the pseudo-eigenvectors of given matrix.

Returns
Const reference to matrix whose columns are the pseudo-eigenvectors.
Precondition
Either the constructor EigenSolver(const MatrixType&,bool) or the member function compute(const MatrixType&, bool) has been called before, and computeEigenvectors was set to true (the default).

The real matrix $ V $ returned by this function and the block-diagonal matrix $ D $ returned by pseudoEigenvalueMatrix() satisfy $ AV = VD $.

Example:

MatrixXd A = MatrixXd::Random(6,6);
cout << "Here is a random 6x6 matrix, A:" << endl << A << endl << endl;
EigenSolver<MatrixXd> es(A);
MatrixXd D = es.pseudoEigenvalueMatrix();
MatrixXd V = es.pseudoEigenvectors();
cout << "The pseudo-eigenvalue matrix D is:" << endl << D << endl;
cout << "The pseudo-eigenvector matrix V is:" << endl << V << endl;
cout << "Finally, V * D * V^(-1) = " << endl << V * D * V.inverse() << endl;

Output:

Here is a random 6x6 matrix, A:
   0.68   -0.33   -0.27  -0.717  -0.687  0.0259
 -0.211   0.536  0.0268   0.214  -0.198   0.678
  0.566  -0.444   0.904  -0.967   -0.74   0.225
  0.597   0.108   0.832  -0.514  -0.782  -0.408
  0.823 -0.0452   0.271  -0.726   0.998   0.275
 -0.605   0.258   0.435   0.608  -0.563  0.0486

The pseudo-eigenvalue matrix D is:
 0.049   1.06      0      0      0      0
 -1.06  0.049      0      0      0      0
     0      0  0.967      0      0      0
     0      0      0  0.353      0      0
     0      0      0      0  0.618  0.129
     0      0      0      0 -0.129  0.618
The pseudo-eigenvector matrix V is:
  -0.571   -0.888   -0.066    -1.13     17.2    -3.54
   0.263   -0.204   -0.869     0.21     9.73     10.7
  -0.827   -0.352    0.209   0.0871    -9.75    -4.17
   -1.15   0.0535  -0.0857   -0.971     9.36    -4.53
  -0.485    0.258    0.436    0.337    -9.74    -2.21
   0.206    0.353   -0.426 -0.00873   -0.942     2.98
Finally, V * D * V^(-1) = 
   0.68   -0.33   -0.27  -0.717  -0.687  0.0259
 -0.211   0.536  0.0268   0.214  -0.198   0.678
  0.566  -0.444   0.904  -0.967   -0.74   0.225
  0.597   0.108   0.832  -0.514  -0.782  -0.408
  0.823 -0.0452   0.271  -0.726   0.998   0.275
 -0.605   0.258   0.435   0.608  -0.563  0.0486
See Also
pseudoEigenvalueMatrix(), eigenvectors()

The documentation for this class was generated from the following file: