Please, help us to better know about our user community by answering the following short survey: https://forms.gle/wpyrxWi18ox9Z5ae9
 Eigen  3.3.9
Eigen::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
 _MatrixType the 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.

## 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 Eigen::Index Index

typedef _MatrixType MatrixType
Synonym for the template parameter _MatrixType.

typedef MatrixType::Scalar Scalar
Scalar type for matrices of type MatrixType.

## Public Member Functions

template<typename InputType >
EigenSolvercompute (const EigenBase< InputType > &matrix, bool computeEigenvectors=true)
Computes eigendecomposition of given matrix. More...

EigenSolver ()
Default constructor. More...

template<typename InputType >
EigenSolver (const EigenBase< InputType > &matrix, bool computeEigenvectors=true)
Constructor; computes eigendecomposition of given matrix. More...

EigenSolver (Index size)
Default constructor with memory preallocation. 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.

ComputationInfo info () const

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.

## ◆ ComplexScalar

template<typename _MatrixType >
 typedef std::complex Eigen::EigenSolver< _MatrixType >::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.

## ◆ EigenvalueType

template<typename _MatrixType >
 typedef Matrix Eigen::EigenSolver< _MatrixType >::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.

## ◆ EigenvectorsType

template<typename _MatrixType >
 typedef Matrix Eigen::EigenSolver< _MatrixType >::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.

## ◆ Index

template<typename _MatrixType >
 typedef Eigen::Index Eigen::EigenSolver< _MatrixType >::Index
Deprecated:
since Eigen 3.3

## ◆ EigenSolver() [1/3]

template<typename _MatrixType >
 Eigen::EigenSolver< _MatrixType >::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).

compute() for an example.

## ◆ EigenSolver() [2/3]

template<typename _MatrixType >
 Eigen::EigenSolver< _MatrixType >::EigenSolver ( Index size )
inlineexplicit

Default constructor with memory preallocation.

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

EigenSolver()

## ◆ EigenSolver() [3/3]

template<typename _MatrixType >
template<typename InputType >
 Eigen::EigenSolver< _MatrixType >::EigenSolver ( const EigenBase< InputType > & matrix, bool computeEigenvectors = true )
inlineexplicit

Constructor; computes eigendecomposition of given matrix.

Parameters
 [in] matrix Square matrix whose eigendecomposition is to be computed. [in] computeEigenvectors If 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:
-1  -0.906   0.662  -0.233   0.692   0.821
-0.737   0.358  -0.931  -0.866  0.0539   0.524
0.511   0.359  -0.893  -0.165  -0.816  -0.475
-0.0827   0.869  0.0594   0.374   0.308  -0.905
0.0655  -0.233   0.342   0.178  -0.168   0.472
-0.562  0.0388  -0.985   0.861   0.402  -0.344

The eigenvalues of A are:
(-1.49,0)
(0.0766,1.16)
(0.0766,-1.16)
(0.733,0)
(-0.534,0.22)
(-0.534,-0.22)
The matrix of eigenvectors, V, is:
(0.786,0)    (-0.263,0.313)   (-0.263,-0.313)         (0.152,0)    (0.325,-0.367)     (0.325,0.367)
(0.0307,0)     (0.357,0.149)    (0.357,-0.149)        (-0.697,0)    (0.0187,0.158)   (0.0187,-0.158)
(-0.577,0)   (0.247,-0.0954)    (0.247,0.0954)         (0.157,0)   (-0.0561,0.315)  (-0.0561,-0.315)
(-0.0523,0) (-0.00741,-0.598)  (-0.00741,0.598)        (-0.351,0)    (-0.0994,0.31)   (-0.0994,-0.31)
(0.169,0)    (-0.176,0.159)   (-0.176,-0.159)        (-0.119,0)    (0.305,-0.552)     (0.305,0.552)
(-0.13,0)  (-0.446,-0.0248)   (-0.446,0.0248)        (-0.574,0)     (0.064,0.353)    (0.064,-0.353)

Consider the first eigenvalue, lambda = (-1.49,0)
If v is the corresponding eigenvector, then lambda * v =
(-1.17,0)
(-0.0458,0)
(0.861,-0)
(0.078,-0)
(-0.252,0)
(0.195,-0)
... and A * v =
(-1.17,0)
(-0.0458,0)
(0.861,0)
(0.078,0)
(-0.252,0)
(0.195,0)

Finally, V * D * V^(-1) =
(-1,-1.11e-16) (-0.906,-3.47e-17)   (0.662,1.11e-16)  (-0.233,5.55e-17)   (0.692,1.11e-16)  (0.821,-1.39e-17)
(-0.737,1.53e-16)  (0.358,-6.94e-18)  (-0.931,1.39e-16)  (-0.866,6.25e-17)  (0.0539,4.16e-17)   (0.524,9.54e-17)
(0.511,0)   (0.359,2.08e-17) (-0.893,-5.55e-17) (-0.165,-6.94e-17) (-0.816,-5.55e-17)  (-0.475,3.64e-17)
(-0.0827,1.11e-16)   (0.869,2.08e-17)         (0.0594,0)   (0.374,5.55e-17)  (0.308,-1.11e-16)  (-0.905,8.67e-17)
(0.0655,-1.11e-16)         (-0.233,0)          (0.342,0)  (0.178,-5.55e-17)  (-0.168,2.78e-17)  (0.472,-6.94e-17)
(-0.562,-1.94e-16) (0.0388,-1.04e-17)         (-0.985,0)   (0.861,4.16e-17)  (0.402,-2.78e-17)  (-0.344,-5.2e-18)

compute()

## ◆ compute()

template<typename _MatrixType >
template<typename InputType >
 EigenSolver& Eigen::EigenSolver< _MatrixType >::compute ( const EigenBase< InputType > & matrix, bool computeEigenvectors = true )

Computes eigendecomposition of given matrix.

Parameters
 [in] matrix Square matrix whose eigendecomposition is to be computed. [in] computeEigenvectors If 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;
MatrixXf A = MatrixXf::Random(4,4);
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.289,1.01) (-0.289,-1.01)      (-1.04,0)     (-0.115,0)
The eigenvalues of A+I are:  (0.711,1.01) (0.711,-1.01)   (-0.0419,0)     (0.885,0)


## ◆ eigenvalues()

template<typename _MatrixType >
 const EigenvalueType& Eigen::EigenSolver< _MatrixType >::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)

eigenvectors(), pseudoEigenvalueMatrix(), MatrixBase::eigenvalues()

## ◆ eigenvectors()

template<typename MatrixType >
 EigenSolver< MatrixType >::EigenvectorsType Eigen::EigenSolver< MatrixType >::eigenvectors

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(0) << endl;

Output:

The first eigenvector of the 3x3 matrix of ones is:
(-0.816,0)
(0.408,0)
(0.408,0)

eigenvalues(), pseudoEigenvectors()

## ◆ info()

template<typename _MatrixType >
 ComputationInfo Eigen::EigenSolver< _MatrixType >::info ( ) const
inline
Returns
NumericalIssue if the input contains INF or NaN values or overflow occured. Returns Success otherwise.

## ◆ pseudoEigenvalueMatrix()

template<typename MatrixType >
 MatrixType Eigen::EigenSolver< MatrixType >::pseudoEigenvalueMatrix

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$$.

pseudoEigenvectors() for an example, eigenvalues()

## ◆ pseudoEigenvectors()

template<typename _MatrixType >
 const MatrixType& Eigen::EigenSolver< _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:
-1  -0.906   0.662  -0.233   0.692   0.821
-0.737   0.358  -0.931  -0.866  0.0539   0.524
0.511   0.359  -0.893  -0.165  -0.816  -0.475
-0.0827   0.869  0.0594   0.374   0.308  -0.905
0.0655  -0.233   0.342   0.178  -0.168   0.472
-0.562  0.0388  -0.985   0.861   0.402  -0.344

The pseudo-eigenvalue matrix D is:
-1.49      0      0      0      0      0
0 0.0766   1.16      0      0      0
0  -1.16 0.0766      0      0      0
0      0      0  0.733      0      0
0      0      0      0 -0.534   0.22
0      0      0      0  -0.22 -0.534
The pseudo-eigenvector matrix V is:
0.786  -0.431   0.512   0.155   0.828  -0.933
0.0307   0.585   0.243  -0.711  0.0475   0.401
-0.577   0.405  -0.156    0.16  -0.143   0.802
-0.0523 -0.0121   -0.98  -0.358  -0.253    0.79
0.169  -0.288   0.261  -0.122   0.775   -1.41
-0.13   -0.73 -0.0406  -0.586   0.163   0.898
Finally, V * D * V^(-1) =
-1  -0.906   0.662  -0.233   0.692   0.821
-0.737   0.358  -0.931  -0.866  0.0539   0.524
0.511   0.359  -0.893  -0.165  -0.816  -0.475
-0.0827   0.869  0.0594   0.374   0.308  -0.905
0.0655  -0.233   0.342   0.178  -0.168   0.472
-0.562  0.0388  -0.985   0.861   0.402  -0.344