Eigen  5.0.1
 
Loading...
Searching...
No Matches
Eigen::EigenSolver< MatrixType_ > Class Template Reference

#include <Eigen/src/Eigenvalues/EigenSolver.h>

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.
See also
MatrixBase::eigenvalues(), class ComplexEigenSolver, class SelfAdjointEigenSolver

Public Types

using ComplexScalar
 Complex scalar type for MatrixType.
 
using EigenvalueType
 Type for vector of eigenvalues as returned by eigenvalues().
 
using EigenvectorsType
 Type for matrix of eigenvectors as returned by eigenvectors().
 
using Index
 
using MatrixType
 Synonym for the template parameter MatrixType_.
 
using PlainMatrixType
 Plain matrix type with the shape and storage options of MatrixType; MatrixType itself unless that is a Ref<>. It is the type of the real matrices returned by pseudoEigenvectors() and pseudoEigenvalueMatrix().
 
using Scalar
 Scalar type for matrices of type MatrixType.
 

Public Member Functions

template<typename InputType>
EigenSolver & compute (const EigenBase< InputType > &matrix, bool computeEigenvectors=true)
 Computes eigendecomposition of given matrix.
 
 EigenSolver ()
 Default constructor.
 
template<typename InputType>
 EigenSolver (const EigenBase< InputType > &matrix, bool computeEigenvectors=true)
 Constructor; computes eigendecomposition of given matrix.
 
template<typename InputType>
 EigenSolver (EigenBase< InputType > &matrix, bool computeEigenvectors=true)
 Constructor for inplace decomposition .
 
 EigenSolver (Index size)
 Default constructor with memory preallocation.
 
const EigenvalueType & eigenvalues () const
 Returns the eigenvalues of given matrix.
 
EigenvectorsType eigenvectors () const
 Returns the eigenvectors of given matrix.
 
Index getMaxIterations () const
 Returns the maximum number of iterations.
 
ComputationInfo info () const
 
PlainMatrixType pseudoEigenvalueMatrix () const
 Returns the block-diagonal matrix in the pseudo-eigendecomposition.
 
const PlainMatrixType & pseudoEigenvectors () const
 Returns the pseudo-eigenvectors of given matrix.
 
EigenSolver & setMaxIterations (Index maxIters)
 Sets the maximum number of iterations allowed.
 

Member Typedef Documentation

◆ ComplexScalar

template<typename MatrixType_>
using 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_>
using 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_>
using 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_>
using Eigen::EigenSolver< MatrixType_ >::Index

Constructor & Destructor Documentation

◆ EigenSolver() [1/4]

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

See also
compute() for an example.

◆ EigenSolver() [2/4]

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.

See also
EigenSolver()

◆ EigenSolver() [3/4]

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]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 computes the eigenvalues and eigenvectors as compute() does.

Example:

// SPDX-FileCopyrightText: The Eigen Authors
// SPDX-License-Identifier: MPL-2.0
MatrixXd A = MatrixXd::Random(6, 6);
cout << "Here is a random 6x6 matrix, A:" << endl << A << endl << endl;
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;
EigenSolver()
Default constructor.
Definition EigenSolver.h:122
Matrix< std::complex< double >, Dynamic, Dynamic > MatrixXcd
Dynamic×Dynamic matrix of type std::complex<double>.
Definition Matrix.h:491
Matrix< double, Dynamic, Dynamic > MatrixXd
Dynamic×Dynamic matrix of type double.
Definition Matrix.h:489
Matrix< std::complex< double >, Dynamic, 1 > VectorXcd
Dynamic×1 vector of type std::complex<double>.
Definition Matrix.h:491

Output:

Here is a random 6x6 matrix, A:
-0.211 0.0268 -0.198  0.946  0.899  0.941
 0.597  0.832 -0.782  0.543 -0.828  0.702
-0.605  0.435 -0.563   0.54  0.326 0.0795
 0.536  0.214  0.678  0.783 -0.302   0.52
 0.108 -0.514 -0.408 -0.295  -0.96  0.335
 0.258  0.608 0.0486  0.838 -0.874 -0.921

The eigenvalues of A are:
      (1.98,0)
 (0.343,0.401)
(0.343,-0.401)
 (-1.28,0.633)
(-1.28,-0.633)
     (-1.15,0)
The matrix of eigenvectors, V, is:
      (-0.331,0)     (0.34,0.434)    (0.34,-0.434)  (-0.562,0.0977) (-0.562,-0.0977)        (0.255,0)
      (-0.653,0)  (0.0942,-0.613)   (0.0942,0.613)  (-0.163,-0.212)   (-0.163,0.212)       (0.0682,0)
      (-0.142,0)  (-0.287,-0.214)   (-0.287,0.214) (-0.476,-0.0536)  (-0.476,0.0536)        (0.659,0)
       (-0.54,0)  (-0.0893,0.303) (-0.0893,-0.303)    (0.196,0.059)   (0.196,-0.059)       (-0.468,0)
       (0.134,0)    (0.139,0.201)   (0.139,-0.201)   (-0.0938,-0.4)    (-0.0938,0.4)       (-0.157,0)
      (-0.366,0) (-0.0913,-0.124)  (-0.0913,0.124)   (0.366,-0.171)    (0.366,0.171)        (0.502,0)

Consider the first eigenvalue, lambda = (1.98,0)
If v is the corresponding eigenvector, then lambda * v = 
(-0.654,0)
 (-1.29,0)
(-0.281,0)
 (-1.07,0)
 (0.266,0)
(-0.723,0)
... and A * v = 
(-0.654,0)
 (-1.29,0)
(-0.281,0)
 (-1.07,0)
 (0.266,0)
(-0.723,0)

Finally, V * D * V^(-1) = 
 (-0.211,6.58e-18)  (0.0268,5.86e-17) (-0.198,-1.64e-16)  (0.946,-1.11e-17)   (0.899,1.29e-16)  (0.941,-1.19e-16)
 (0.597,-1.58e-17)   (0.832,1.43e-16) (-0.782,-2.21e-16)  (0.543,-1.93e-16)  (-0.828,4.83e-18)    (0.702,2.2e-16)
(-0.605,-4.66e-17)  (0.435,-5.78e-18)  (-0.563,3.48e-17)    (0.54,2.24e-17)   (0.326,1.58e-16) (0.0795,-1.98e-17)
  (0.536,4.12e-17)   (0.214,6.36e-17)  (0.678,-1.72e-16)  (0.783,-8.62e-17) (-0.302,-3.11e-16)    (0.52,7.65e-17)
  (0.108,4.49e-18) (-0.514,-3.32e-17)     (-0.408,4e-17) (-0.295,-2.76e-17)   (-0.96,9.99e-17)   (0.335,1.16e-16)
 (0.258,-5.08e-19)   (0.608,3.39e-17) (0.0486,-1.06e-16)  (0.838,-4.34e-17) (-0.874,-1.31e-16)  (-0.921,1.51e-16)
See also
compute()

◆ EigenSolver() [4/4]

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

Constructor for inplace decomposition .

Parameters
[in,out]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.

When MatrixType is a Ref<>, the decomposition is computed within the memory of matrix, whose content is destroyed; the results are stored in the decomposition object. Otherwise this constructor behaves like EigenSolver(const EigenBase<InputType>&, bool).

Member Function Documentation

◆ 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]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 the allocated data in the EigenSolver object.

Example:

// SPDX-FileCopyrightText: The Eigen Authors
// SPDX-License-Identifier: MPL-2.0
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); // reuse es to compute eigenvalues of A+I
cout << "The eigenvalues of A+I are: " << es.eigenvalues().transpose() << endl;
EigenSolver & compute(const EigenBase< InputType > &matrix, bool computeEigenvectors=true)
Computes eigendecomposition of given matrix.
const EigenvalueType & eigenvalues() const
Returns the eigenvalues of given matrix.
Definition EigenSolver.h:265
Matrix< float, Dynamic, Dynamic > MatrixXf
Dynamic×Dynamic matrix of type float.
Definition Matrix.h:488

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)

◆ 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:

// SPDX-FileCopyrightText: The Eigen Authors
// SPDX-License-Identifier: MPL-2.0
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()

◆ eigenvectors()

template<typename MatrixType>
EigenSolver< MatrixType >::EigenvectorsType Eigen::EigenSolver< MatrixType >::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:

// SPDX-FileCopyrightText: The Eigen Authors
// SPDX-License-Identifier: MPL-2.0
MatrixXd ones = MatrixXd::Ones(3, 3);
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)
See also
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 occurred, NoConvergence if the Schur decomposition did not converge within the maximum number of iterations, and Success otherwise.

◆ pseudoEigenvalueMatrix()

template<typename MatrixType>
EigenSolver< MatrixType >::PlainMatrixType Eigen::EigenSolver< 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()

◆ pseudoEigenvectors()

template<typename MatrixType_>
const PlainMatrixType & 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:

// SPDX-FileCopyrightText: The Eigen Authors
// SPDX-License-Identifier: MPL-2.0
MatrixXd A = MatrixXd::Random(6, 6);
cout << "Here is a random 6x6 matrix, A:" << endl << A << endl << endl;
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.211 0.0268 -0.198  0.946  0.899  0.941
 0.597  0.832 -0.782  0.543 -0.828  0.702
-0.605  0.435 -0.563   0.54  0.326 0.0795
 0.536  0.214  0.678  0.783 -0.302   0.52
 0.108 -0.514 -0.408 -0.295  -0.96  0.335
 0.258  0.608 0.0486  0.838 -0.874 -0.921

The pseudo-eigenvalue matrix D is:
  1.98      0      0      0      0      0
     0  0.343  0.401      0      0      0
     0 -0.401  0.343      0      0      0
     0      0      0  -1.28  0.633      0
     0      0      0 -0.633  -1.28      0
     0      0      0      0      0  -1.15
The pseudo-eigenvector matrix V is:
-0.331  0.439  0.561  -1.23  0.214  0.331
-0.653  0.122 -0.791 -0.357 -0.463 0.0887
-0.142 -0.371 -0.277  -1.04 -0.117  0.857
 -0.54 -0.115  0.391  0.428  0.129 -0.609
 0.134   0.18   0.26 -0.205 -0.874 -0.204
-0.366 -0.118  -0.16    0.8 -0.374  0.653
Finally, V * D * V^(-1) = 
-0.211 0.0268 -0.198  0.946  0.899  0.941
 0.597  0.832 -0.782  0.543 -0.828  0.702
-0.605  0.435 -0.563   0.54  0.326 0.0795
 0.536  0.214  0.678  0.783 -0.302   0.52
 0.108 -0.514 -0.408 -0.295  -0.96  0.335
 0.258  0.608 0.0486  0.838 -0.874 -0.921
See also
pseudoEigenvalueMatrix(), eigenvectors()

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