Computes eigenvalues and eigenvectors of selfadjoint matrices. More...
#include <SelfAdjointEigenSolver.h>
Public Types | |
enum | { Size = MatrixType::RowsAtCompileTime, ColsAtCompileTime = MatrixType::ColsAtCompileTime, Options = MatrixType::Options, MaxColsAtCompileTime = MatrixType::MaxColsAtCompileTime } |
typedef MatrixType::Index | Index |
typedef _MatrixType | MatrixType |
typedef NumTraits< Scalar >::Real | RealScalar |
Real scalar type for _MatrixType . | |
typedef internal::plain_col_type < MatrixType, RealScalar > ::type | RealVectorType |
Type for vector of eigenvalues as returned by eigenvalues(). | |
typedef MatrixType::Scalar | Scalar |
Scalar type for matrices of type _MatrixType . | |
typedef Tridiagonalization < MatrixType > | TridiagonalizationType |
Public Member Functions | |
SelfAdjointEigenSolver & | compute (const MatrixType &matrix, int options=ComputeEigenvectors) |
Computes eigendecomposition of given matrix. | |
const RealVectorType & | eigenvalues () const |
Returns the eigenvalues of given matrix. | |
const MatrixType & | eigenvectors () const |
Returns the eigenvectors of given matrix. | |
ComputationInfo | info () const |
Reports whether previous computation was successful. | |
MatrixType | operatorInverseSqrt () const |
Computes the inverse square root of the matrix. | |
MatrixType | operatorSqrt () const |
Computes the positive-definite square root of the matrix. | |
SelfAdjointEigenSolver () | |
Default constructor for fixed-size matrices. | |
SelfAdjointEigenSolver (Index size) | |
Constructor, pre-allocates memory for dynamic-size matrices. | |
SelfAdjointEigenSolver (const MatrixType &matrix, int options=ComputeEigenvectors) | |
Constructor; computes eigendecomposition of given matrix. | |
Static Public Attributes | |
static const int | m_maxIterations = 30 |
Maximum number of iterations. | |
Protected Attributes | |
bool | m_eigenvectorsOk |
RealVectorType | m_eivalues |
MatrixType | m_eivec |
ComputationInfo | m_info |
bool | m_isInitialized |
TridiagonalizationType::SubDiagonalType | m_subdiag |
Computes eigenvalues and eigenvectors of selfadjoint 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. |
A matrix is selfadjoint if it equals its adjoint. For real matrices, this means that the matrix is symmetric: it equals its transpose. This class computes the eigenvalues and eigenvectors of a selfadjoint matrix. These are the scalars and vectors such that . The eigenvalues of a selfadjoint matrix are always real. If is a diagonal matrix with the eigenvalues on the diagonal, and is a matrix with the eigenvectors as its columns, then (for selfadjoint matrices, the matrix is always invertible). This is called the eigendecomposition.
The algorithm exploits the fact that the matrix is selfadjoint, making it faster and more accurate than the general purpose eigenvalue algorithms implemented in EigenSolver and ComplexEigenSolver.
Only the lower triangular part of the input matrix is referenced.
Call the function compute() to compute the eigenvalues and eigenvectors of a given matrix. Alternatively, you can use the SelfAdjointEigenSolver(const MatrixType&, int) 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 documentation for SelfAdjointEigenSolver(const MatrixType&, int) contains an example of the typical use of this class.
To solve the generalized eigenvalue problem and the likes, see the class GeneralizedSelfAdjointEigenSolver.
Definition at line 78 of file SelfAdjointEigenSolver.h.
typedef MatrixType::Index SelfAdjointEigenSolver< _MatrixType >::Index |
Reimplemented in GeneralizedSelfAdjointEigenSolver< _MatrixType >.
Definition at line 92 of file SelfAdjointEigenSolver.h.
typedef _MatrixType SelfAdjointEigenSolver< _MatrixType >::MatrixType |
Reimplemented in GeneralizedSelfAdjointEigenSolver< _MatrixType >.
Definition at line 82 of file SelfAdjointEigenSolver.h.
typedef NumTraits<Scalar>::Real SelfAdjointEigenSolver< _MatrixType >::RealScalar |
Real scalar type for _MatrixType
.
This is just Scalar
if Scalar is real (e.g., float
or double
), and the type of the real part of Scalar
if Scalar is complex.
Definition at line 100 of file SelfAdjointEigenSolver.h.
typedef internal::plain_col_type<MatrixType, RealScalar>::type SelfAdjointEigenSolver< _MatrixType >::RealVectorType |
Type for vector of eigenvalues as returned by eigenvalues().
This is a column vector with entries of type RealScalar. The length of the vector is the size of _MatrixType
.
Definition at line 107 of file SelfAdjointEigenSolver.h.
typedef MatrixType::Scalar SelfAdjointEigenSolver< _MatrixType >::Scalar |
Scalar type for matrices of type _MatrixType
.
Definition at line 91 of file SelfAdjointEigenSolver.h.
typedef Tridiagonalization<MatrixType> SelfAdjointEigenSolver< _MatrixType >::TridiagonalizationType |
Definition at line 108 of file SelfAdjointEigenSolver.h.
anonymous enum |
Definition at line 83 of file SelfAdjointEigenSolver.h.
SelfAdjointEigenSolver< _MatrixType >::SelfAdjointEigenSolver | ( | ) | [inline] |
Default constructor for fixed-size matrices.
The default constructor is useful in cases in which the user intends to perform decompositions via compute(). This constructor can only be used if _MatrixType
is a fixed-size matrix; use SelfAdjointEigenSolver(Index) for dynamic-size matrices.
Example:
SelfAdjointEigenSolver<Matrix4f> es; Matrix4f X = Matrix4f::Random(4,4); Matrix4f A = X + X.transpose(); es.compute(A); cout << "The eigenvalues of A are: " << es.eigenvalues().transpose() << endl; es.compute(A + Matrix4f::Identity(4,4)); // re-use es to compute eigenvalues of A+I cout << "The eigenvalues of A+I are: " << es.eigenvalues().transpose() << endl;
Output:
Definition at line 120 of file SelfAdjointEigenSolver.h.
SelfAdjointEigenSolver< _MatrixType >::SelfAdjointEigenSolver | ( | Index | size | ) | [inline] |
Constructor, pre-allocates memory for dynamic-size matrices.
[in] | size | Positive integer, size of the matrix whose eigenvalues and eigenvectors will be computed. |
This constructor is useful for dynamic-size matrices, when the user intends to perform decompositions via compute(). The size
parameter is only used as a hint. It is not an error to give a wrong size
, but it may impair performance.
Definition at line 139 of file SelfAdjointEigenSolver.h.
SelfAdjointEigenSolver< _MatrixType >::SelfAdjointEigenSolver | ( | const MatrixType & | matrix, |
int | options = ComputeEigenvectors |
||
) | [inline] |
Constructor; computes eigendecomposition of given matrix.
[in] | matrix | Selfadjoint matrix whose eigendecomposition is to be computed. Only the lower triangular part of the matrix is referenced. |
[in] | options | Can be ComputeEigenvectors (default) or EigenvaluesOnly. |
This constructor calls compute(const MatrixType&, int) to compute the eigenvalues of the matrix matrix
. The eigenvectors are computed if options
equals ComputeEigenvectors.
Example:
MatrixXd X = MatrixXd::Random(5,5); MatrixXd A = X + X.transpose(); cout << "Here is a random symmetric 5x5 matrix, A:" << endl << A << endl << endl; SelfAdjointEigenSolver<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; double lambda = es.eigenvalues()[0]; cout << "Consider the first eigenvalue, lambda = " << lambda << endl; VectorXd 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 * v << endl << endl; MatrixXd D = es.eigenvalues().asDiagonal(); MatrixXd V = es.eigenvectors(); cout << "Finally, V * D * V^(-1) = " << endl << V * D * V.inverse() << endl;
Output:
Definition at line 161 of file SelfAdjointEigenSolver.h.
SelfAdjointEigenSolver< MatrixType > & SelfAdjointEigenSolver< MatrixType >::compute | ( | const MatrixType & | matrix, |
int | options = ComputeEigenvectors |
||
) |
Computes eigendecomposition of given matrix.
[in] | matrix | Selfadjoint matrix whose eigendecomposition is to be computed. Only the lower triangular part of the matrix is referenced. |
[in] | options | Can be ComputeEigenvectors (default) or EigenvaluesOnly. |
*this
This function computes the eigenvalues of matrix
. The eigenvalues() function can be used to retrieve them. If options
equals ComputeEigenvectors, then the eigenvectors are also computed and can be retrieved by calling eigenvectors().
This implementation uses a symmetric QR algorithm. The matrix is first reduced to tridiagonal form using the Tridiagonalization class. The tridiagonal matrix is then brought to diagonal form with implicit symmetric QR steps with Wilkinson shift. Details can be found in Section 8.3 of Golub & Van Loan, Matrix Computations.
The cost of the computation is about if the eigenvectors are required and if they are not required.
This method reuses the memory in the SelfAdjointEigenSolver object that was allocated when the object was constructed, if the size of the matrix does not change.
Example:
SelfAdjointEigenSolver<MatrixXf> es(4); MatrixXf X = MatrixXf::Random(4,4); MatrixXf A = X + X.transpose(); es.compute(A); cout << "The eigenvalues of A are: " << es.eigenvalues().transpose() << endl; es.compute(A + MatrixXf::Identity(4,4)); // re-use es to compute eigenvalues of A+I cout << "The eigenvalues of A+I are: " << es.eigenvalues().transpose() << endl;
Output:
Definition at line 376 of file SelfAdjointEigenSolver.h.
const RealVectorType& SelfAdjointEigenSolver< _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 sorted in increasing order.
Example:
MatrixXd ones = MatrixXd::Ones(3,3); SelfAdjointEigenSolver<MatrixXd> es(ones); cout << "The eigenvalues of the 3x3 matrix of ones are:" << endl << es.eigenvalues() << endl;
Output:
Definition at line 242 of file SelfAdjointEigenSolver.h.
const MatrixType& SelfAdjointEigenSolver< _MatrixType >::eigenvectors | ( | ) | const [inline] |
Returns the eigenvectors of given matrix.
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. If this object was used to solve the eigenproblem for the selfadjoint matrix , then the matrix returned by this function is the matrix in the eigendecomposition .
Example:
MatrixXd ones = MatrixXd::Ones(3,3); SelfAdjointEigenSolver<MatrixXd> es(ones); cout << "The first eigenvector of the 3x3 matrix of ones is:" << endl << es.eigenvectors().col(1) << endl;
Output:
Definition at line 220 of file SelfAdjointEigenSolver.h.
ComputationInfo SelfAdjointEigenSolver< _MatrixType >::info | ( | ) | const [inline] |
Reports whether previous computation was successful.
Success
if computation was succesful, NoConvergence
otherwise. Definition at line 302 of file SelfAdjointEigenSolver.h.
MatrixType SelfAdjointEigenSolver< _MatrixType >::operatorInverseSqrt | ( | ) | const [inline] |
Computes the inverse square root of the matrix.
This function uses the eigendecomposition to compute the inverse square root as . This is cheaper than first computing the square root with operatorSqrt() and then its inverse with MatrixBase::inverse().
Example:
MatrixXd X = MatrixXd::Random(4,4); MatrixXd A = X * X.transpose(); cout << "Here is a random positive-definite matrix, A:" << endl << A << endl << endl; SelfAdjointEigenSolver<MatrixXd> es(A); cout << "The inverse square root of A is: " << endl; cout << es.operatorInverseSqrt() << endl; cout << "We can also compute it with operatorSqrt() and inverse(). That yields: " << endl; cout << es.operatorSqrt().inverse() << endl;
Output:
Definition at line 291 of file SelfAdjointEigenSolver.h.
MatrixType SelfAdjointEigenSolver< _MatrixType >::operatorSqrt | ( | ) | const [inline] |
Computes the positive-definite square root of the matrix.
The square root of a positive-definite matrix is the positive-definite matrix whose square equals . This function uses the eigendecomposition to compute the square root as .
Example:
MatrixXd X = MatrixXd::Random(4,4); MatrixXd A = X * X.transpose(); cout << "Here is a random positive-definite matrix, A:" << endl << A << endl << endl; SelfAdjointEigenSolver<MatrixXd> es(A); MatrixXd sqrtA = es.operatorSqrt(); cout << "The square root of A is: " << endl << sqrtA << endl; cout << "If we square this, we get: " << endl << sqrtA*sqrtA << endl;
Output:
Definition at line 266 of file SelfAdjointEigenSolver.h.
bool SelfAdjointEigenSolver< _MatrixType >::m_eigenvectorsOk [protected] |
Definition at line 350 of file SelfAdjointEigenSolver.h.
RealVectorType SelfAdjointEigenSolver< _MatrixType >::m_eivalues [protected] |
Definition at line 346 of file SelfAdjointEigenSolver.h.
MatrixType SelfAdjointEigenSolver< _MatrixType >::m_eivec [protected] |
Definition at line 345 of file SelfAdjointEigenSolver.h.
ComputationInfo SelfAdjointEigenSolver< _MatrixType >::m_info [protected] |
Definition at line 348 of file SelfAdjointEigenSolver.h.
bool SelfAdjointEigenSolver< _MatrixType >::m_isInitialized [protected] |
Definition at line 349 of file SelfAdjointEigenSolver.h.
const int SelfAdjointEigenSolver< _MatrixType >::m_maxIterations = 30 [static] |
Maximum number of iterations.
Maximum number of iterations allowed for an eigenvalue to converge.
Definition at line 312 of file SelfAdjointEigenSolver.h.
TridiagonalizationType::SubDiagonalType SelfAdjointEigenSolver< _MatrixType >::m_subdiag [protected] |
Definition at line 347 of file SelfAdjointEigenSolver.h.