Computes eigenvalues and eigenvectors of general matrices. More...
#include <EigenSolver.h>
Public Types | |
enum | { RowsAtCompileTime = MatrixType::RowsAtCompileTime, ColsAtCompileTime = MatrixType::ColsAtCompileTime, Options = MatrixType::Options, MaxRowsAtCompileTime = MatrixType::MaxRowsAtCompileTime, MaxColsAtCompileTime = MatrixType::MaxColsAtCompileTime } |
typedef std::complex< RealScalar > | ComplexScalar |
Complex scalar type for MatrixType. | |
typedef Matrix< ComplexScalar, ColsAtCompileTime, 1, Options &~RowMajor, MaxColsAtCompileTime, 1 > | EigenvalueType |
Type for vector of eigenvalues as returned by eigenvalues(). | |
typedef Matrix< ComplexScalar, RowsAtCompileTime, ColsAtCompileTime, Options, MaxRowsAtCompileTime, MaxColsAtCompileTime > | EigenvectorsType |
Type for matrix of eigenvectors as returned by eigenvectors(). | |
typedef MatrixType::Index | Index |
typedef _MatrixType | MatrixType |
Synonym for the template parameter _MatrixType . | |
typedef NumTraits< Scalar >::Real | RealScalar |
typedef MatrixType::Scalar | Scalar |
Scalar type for matrices of type MatrixType. | |
Public Member Functions | |
EigenSolver & | compute (const MatrixType &matrix, bool computeEigenvectors=true) |
Computes eigendecomposition of given matrix. | |
EigenSolver () | |
Default constructor. | |
EigenSolver (Index size) | |
Default constructor with memory preallocation. | |
EigenSolver (const MatrixType &matrix, bool computeEigenvectors=true) | |
Constructor; computes eigendecomposition of given matrix. | |
const EigenvalueType & | eigenvalues () const |
Returns the eigenvalues of given matrix. | |
EigenvectorsType | eigenvectors () const |
Returns the eigenvectors of given matrix. | |
ComputationInfo | info () const |
MatrixType | pseudoEigenvalueMatrix () const |
Returns the block-diagonal matrix in the pseudo-eigendecomposition. | |
const MatrixType & | pseudoEigenvectors () const |
Returns the pseudo-eigenvectors of given matrix. | |
Protected Types | |
typedef Matrix< Scalar, ColsAtCompileTime, 1, Options &~RowMajor, MaxColsAtCompileTime, 1 > | ColumnVectorType |
Protected Attributes | |
bool | m_eigenvectorsOk |
EigenvalueType | m_eivalues |
MatrixType | m_eivec |
bool | m_isInitialized |
MatrixType | m_matT |
RealSchur< MatrixType > | m_realSchur |
ColumnVectorType | m_tmp |
Private Member Functions | |
void | doComputeEigenvectors () |
Computes eigenvalues and eigenvectors of general matrices.
_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 are scalars and vectors such that . If is a diagonal matrix with the eigenvalues on the diagonal, and is a matrix with the eigenvectors as its columns, then . The matrix is almost always invertible, in which case we have . 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 and satisfying , just like the eigendecomposition, if the matrix is not required to be diagonal, but if it is allowed to have blocks of the form
(where and are real numbers) on the diagonal. These blocks correspond to complex eigenvalue pairs . 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.
Definition at line 78 of file EigenSolver.h.
typedef Matrix<Scalar, ColsAtCompileTime, 1, Options & ~RowMajor, MaxColsAtCompileTime, 1> EigenSolver< _MatrixType >::ColumnVectorType [protected] |
Definition at line 309 of file EigenSolver.h.
typedef std::complex<RealScalar> 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.
Definition at line 104 of file EigenSolver.h.
typedef Matrix<ComplexScalar, ColsAtCompileTime, 1, Options & ~RowMajor, MaxColsAtCompileTime, 1> 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.
Definition at line 111 of file EigenSolver.h.
typedef Matrix<ComplexScalar, RowsAtCompileTime, ColsAtCompileTime, Options, MaxRowsAtCompileTime, MaxColsAtCompileTime> 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.
Definition at line 118 of file EigenSolver.h.
typedef MatrixType::Index EigenSolver< _MatrixType >::Index |
Definition at line 96 of file EigenSolver.h.
typedef _MatrixType EigenSolver< _MatrixType >::MatrixType |
Synonym for the template parameter _MatrixType
.
Definition at line 83 of file EigenSolver.h.
typedef NumTraits<Scalar>::Real EigenSolver< _MatrixType >::RealScalar |
Definition at line 95 of file EigenSolver.h.
typedef MatrixType::Scalar EigenSolver< _MatrixType >::Scalar |
Scalar type for matrices of type MatrixType.
Definition at line 94 of file EigenSolver.h.
anonymous enum |
Definition at line 85 of file EigenSolver.h.
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).
Definition at line 127 of file EigenSolver.h.
EigenSolver< _MatrixType >::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.
Definition at line 135 of file EigenSolver.h.
EigenSolver< _MatrixType >::EigenSolver | ( | const MatrixType & | matrix, |
bool | computeEigenvectors = true |
||
) | [inline] |
Constructor; computes eigendecomposition of given matrix.
[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:
Definition at line 160 of file EigenSolver.h.
EigenSolver< MatrixType > & EigenSolver< MatrixType >::compute | ( | const MatrixType & | matrix, |
bool | computeEigenvectors = true |
||
) |
Computes eigendecomposition of given matrix.
[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
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 (where is the size of the matrix) if computeEigenvectors
is true, and 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:
Definition at line 365 of file EigenSolver.h.
void EigenSolver< MatrixType >::doComputeEigenvectors | ( | ) | [private] |
Definition at line 429 of file EigenSolver.h.
const EigenvalueType& EigenSolver< _MatrixType >::eigenvalues | ( | ) | const [inline] |
Returns the eigenvalues of given matrix.
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:
Definition at line 257 of file EigenSolver.h.
EigenSolver< MatrixType >::EigenvectorsType EigenSolver< MatrixType >::eigenvectors | ( | ) | const |
Returns the eigenvectors of given matrix.
computeEigenvectors
was set to true (the default).Column of the returned matrix is an eigenvector corresponding to eigenvalue number as returned by eigenvalues(). The eigenvectors are normalized to have (Euclidean) norm equal to one. The matrix returned by this function is the matrix in the eigendecomposition , 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:
Definition at line 334 of file EigenSolver.h.
ComputationInfo EigenSolver< _MatrixType >::info | ( | ) | const [inline] |
Definition at line 292 of file EigenSolver.h.
MatrixType EigenSolver< MatrixType >::pseudoEigenvalueMatrix | ( | ) | const |
Returns the block-diagonal matrix in the pseudo-eigendecomposition.
The matrix 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 . These blocks are not sorted in any particular order. The matrix and the matrix returned by pseudoEigenvectors() satisfy .
Definition at line 314 of file EigenSolver.h.
const MatrixType& EigenSolver< _MatrixType >::pseudoEigenvectors | ( | ) | const [inline] |
Returns the pseudo-eigenvectors of given matrix.
computeEigenvectors
was set to true (the default).The real matrix returned by this function and the block-diagonal matrix returned by pseudoEigenvalueMatrix() satisfy .
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:
Definition at line 212 of file EigenSolver.h.
bool EigenSolver< _MatrixType >::m_eigenvectorsOk [protected] |
Definition at line 305 of file EigenSolver.h.
EigenvalueType EigenSolver< _MatrixType >::m_eivalues [protected] |
Definition at line 303 of file EigenSolver.h.
MatrixType EigenSolver< _MatrixType >::m_eivec [protected] |
Definition at line 302 of file EigenSolver.h.
bool EigenSolver< _MatrixType >::m_isInitialized [protected] |
Definition at line 304 of file EigenSolver.h.
MatrixType EigenSolver< _MatrixType >::m_matT [protected] |
Definition at line 307 of file EigenSolver.h.
RealSchur<MatrixType> EigenSolver< _MatrixType >::m_realSchur [protected] |
Definition at line 306 of file EigenSolver.h.
ColumnVectorType EigenSolver< _MatrixType >::m_tmp [protected] |
Definition at line 310 of file EigenSolver.h.