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

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

Detailed Description

template<typename MatrixType_>
class Eigen::SelfAdjointEigenSolver< MatrixType_ >

Computes eigenvalues and eigenvectors of selfadjoint 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.

A matrix \( A \) 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 \( \lambda \) and vectors \( v \) such that \( Av = \lambda v \). The eigenvalues of a selfadjoint matrix are always real. 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 D V^{-1} \). This is called the eigendecomposition.

For a selfadjoint matrix, \( V \) is unitary, meaning its inverse is equal to its adjoint, \( V^{-1} = V^{\dagger} \). If \( A \) is real, then \( V \) is also real and therefore orthogonal, meaning its inverse is equal to its transpose, \( V^{-1} = V^T \).

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 \( Av = \lambda Bv \) and the likes, see the class GeneralizedSelfAdjointEigenSolver.

See also
MatrixBase::eigenvalues(), class EigenSolver, class ComplexEigenSolver
+ Inheritance diagram for Eigen::SelfAdjointEigenSolver< MatrixType_ >:

Public Types

using EigenvectorsType
 Type of the matrix returned by eigenvectors().
 
using Index
 
using PlainMatrixType
 Plain matrix type with the shape and storage options of MatrixType_; MatrixType_ itself unless that is a Ref<>.
 
using RealScalar
 Real scalar type for MatrixType_.
 
using Scalar
 Scalar type for matrices of type MatrixType_.
 
using VectorType
 Type for vector of eigenvalues as returned by eigenvalues().
 

Public Member Functions

template<typename InputType>
SelfAdjointEigenSolver & compute (const EigenBase< InputType > &matrix, int options=ComputeEigenvectors)
 Computes eigendecomposition of given matrix.
 
SelfAdjointEigenSolver & computeDirect (const MatrixType &matrix, int options=ComputeEigenvectors)
 Computes eigendecomposition of given matrix primarily using a closed-form algorithm.
 
SelfAdjointEigenSolver & computeFromTridiagonal (const RealVectorType &diag, const SubDiagonalType &subdiag, int options=ComputeEigenvectors)
 Computes the eigen decomposition from a tridiagonal symmetric matrix.
 
const RealVectorType & eigenvalues () const
 Returns the eigenvalues of given matrix.
 
const EigenvectorsType & eigenvectors () const
 Returns the eigenvectors of given matrix.
 
ComputationInfo info () const
 Reports whether previous computation was successful.
 
PlainMatrixType operatorExp () const
 Computes the matrix exponential of the matrix.
 
PlainMatrixType operatorInverseSqrt () const
 Computes the inverse square root of the matrix.
 
PlainMatrixType operatorSqrt () const
 Computes the positive-definite square root of the matrix.
 
 SelfAdjointEigenSolver ()
 Default constructor for fixed-size matrices.
 
template<typename InputType>
 SelfAdjointEigenSolver (const EigenBase< InputType > &matrix, int options=ComputeEigenvectors)
 Constructor; computes eigendecomposition of given matrix.
 
template<typename InputType, bool IsRef = internal::is_ref<MatrixType>::value, std::enable_if_t< IsRef, int > = 0>
 SelfAdjointEigenSolver (EigenBase< InputType > &matrix, int options=ComputeEigenvectors)
 Constructor for inplace decomposition .
 
 SelfAdjointEigenSolver (Index size)
 Constructor, pre-allocates memory for dynamic-size matrices.
 

Static Public Attributes

static const int m_maxIterations
 Maximum number of iterations.
 

Member Typedef Documentation

◆ EigenvectorsType

template<typename MatrixType_>
using Eigen::SelfAdjointEigenSolver< MatrixType_ >::EigenvectorsType

Type of the matrix returned by eigenvectors().

A column-major plain matrix, or MatrixType_ itself when that is a Ref<>: the inplace decomposition then stores the eigenvectors in the referenced matrix.

◆ Index

template<typename MatrixType_>
using Eigen::SelfAdjointEigenSolver< MatrixType_ >::Index

◆ RealScalar

template<typename MatrixType_>
using Eigen::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.

◆ VectorType

template<typename MatrixType_>
using Eigen::SelfAdjointEigenSolver< MatrixType_ >::VectorType

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

Constructor & Destructor Documentation

◆ SelfAdjointEigenSolver() [1/4]

template<typename MatrixType_>
Eigen::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:

// SPDX-FileCopyrightText: The Eigen Authors
// SPDX-License-Identifier: MPL-2.0
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)); // reuse es to compute eigenvalues of A+I
cout << "The eigenvalues of A+I are: " << es.eigenvalues().transpose() << endl;
SelfAdjointEigenSolver & compute(const EigenBase< InputType > &matrix, int options=ComputeEigenvectors)
Computes eigendecomposition of given matrix.
SelfAdjointEigenSolver()
Default constructor for fixed-size matrices.
Definition SelfAdjointEigenSolver.h:140
const RealVectorType & eigenvalues() const
Returns the eigenvalues of given matrix.
Definition SelfAdjointEigenSolver.h:338
Matrix< float, 4, 4 > Matrix4f
4×4 matrix of type float.
Definition Matrix.h:488

Output:

The eigenvalues of A are:  -1.58 -0.473   1.32   2.46
The eigenvalues of A+I are: -0.581  0.527   2.32   3.46

◆ SelfAdjointEigenSolver() [2/4]

template<typename MatrixType_>
Eigen::SelfAdjointEigenSolver< MatrixType_ >::SelfAdjointEigenSolver ( Index size)
inlineexplicit

Constructor, pre-allocates memory for dynamic-size matrices.

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

See also
compute() for an example

◆ SelfAdjointEigenSolver() [3/4]

template<typename MatrixType_>
template<typename InputType>
Eigen::SelfAdjointEigenSolver< MatrixType_ >::SelfAdjointEigenSolver ( const EigenBase< InputType > & matrix,
int options = ComputeEigenvectors )
inlineexplicit

Constructor; computes eigendecomposition of given matrix.

Parameters
[in]matrixSelfadjoint matrix whose eigendecomposition is to be computed. Only the lower triangular part of the matrix is referenced.
[in]optionsCan 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:

// SPDX-FileCopyrightText: The Eigen Authors
// SPDX-License-Identifier: MPL-2.0
MatrixXd X = MatrixXd::Random(5, 5);
MatrixXd A = X + X.transpose();
cout << "Here is a random symmetric 5x5 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;
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;
Matrix< double, Dynamic, Dynamic > MatrixXd
Dynamic×Dynamic matrix of type double.
Definition Matrix.h:489
Matrix< double, Dynamic, 1 > VectorXd
Dynamic×1 vector of type double.
Definition Matrix.h:489

Output:

Here is a random symmetric 5x5 matrix, A:
-0.422  0.855  -1.12   1.21  0.648
 0.855 0.0536   1.44 0.0267  0.997
 -1.12   1.44 -0.396 -0.734 -0.859
  1.21 0.0267 -0.734   1.89   1.38
 0.648  0.997 -0.859   1.38    1.8

The eigenvalues of A are:
 -2.72
-0.409
 0.368
  1.51
  4.18
The matrix of eigenvectors, V, is:
 -0.503  -0.772   0.175 -0.0251   0.345
  0.552  -0.262   0.271   0.732   0.134
 -0.629   0.362   0.376   0.513  -0.262
  0.088   0.376   0.603  -0.272   0.643
 -0.196   0.251  -0.625   0.356   0.618

Consider the first eigenvalue, lambda = -2.72
If v is the corresponding eigenvector, then lambda * v = 
  1.37
  -1.5
  1.71
-0.239
 0.533
... and A * v = 
  1.37
  -1.5
  1.71
-0.239
 0.533

Finally, V * D * V^(-1) = 
-0.422  0.855  -1.12   1.21  0.648
 0.855 0.0536   1.44 0.0267  0.997
 -1.12   1.44 -0.396 -0.734 -0.859
  1.21 0.0267 -0.734   1.89   1.38
 0.648  0.997 -0.859   1.38    1.8
See also
compute(const MatrixType&, int)

◆ SelfAdjointEigenSolver() [4/4]

template<typename MatrixType_>
template<typename InputType, bool IsRef = internal::is_ref<MatrixType>::value, std::enable_if_t< IsRef, int > = 0>
Eigen::SelfAdjointEigenSolver< MatrixType_ >::SelfAdjointEigenSolver ( EigenBase< InputType > & matrix,
int options = ComputeEigenvectors )
inlineexplicit

Constructor for inplace decomposition .

Parameters
[in,out]matrixSelfadjoint matrix whose eigendecomposition is to be computed. Only the lower triangular part of the matrix is referenced.
[in]optionsCan be ComputeEigenvectors (default) or EigenvaluesOnly.

When MatrixType is a Ref<>, the decomposition is computed within the memory of matrix, whose content is destroyed: with ComputeEigenvectors it holds the eigenvectors afterwards and eigenvectors() refers to it, with EigenvaluesOnly its content is unspecified. This overload is only available for Ref<>; owning matrix types use the constructor taking a const input.

Member Function Documentation

◆ compute()

template<typename MatrixType_>
template<typename InputType>
SelfAdjointEigenSolver & Eigen::SelfAdjointEigenSolver< MatrixType_ >::compute ( const EigenBase< InputType > & matrix,
int options = ComputeEigenvectors )

Computes eigendecomposition of given matrix.

Parameters
[in]matrixSelfadjoint matrix whose eigendecomposition is to be computed. Only the lower triangular part of the matrix is referenced.
[in]optionsCan be ComputeEigenvectors (default) or EigenvaluesOnly.
Returns
Reference to *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 \( 9n^3 \) if the eigenvectors are required and \( 4n^3/3 \) 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:

// SPDX-FileCopyrightText: The Eigen Authors
// SPDX-License-Identifier: MPL-2.0
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)); // reuse es to compute eigenvalues of A+I
cout << "The eigenvalues of A+I are: " << es.eigenvalues().transpose() << endl;
Matrix< float, Dynamic, Dynamic > MatrixXf
Dynamic×Dynamic matrix of type float.
Definition Matrix.h:488

Output:

The eigenvalues of A are:  -1.58 -0.473   1.32   2.46
The eigenvalues of A+I are: -0.581  0.527   2.32   3.46
See also
SelfAdjointEigenSolver(const MatrixType&, int)

◆ computeDirect()

template<typename MatrixType>
SelfAdjointEigenSolver< MatrixType > & Eigen::SelfAdjointEigenSolver< MatrixType >::computeDirect ( const MatrixType & matrix,
int options = ComputeEigenvectors )

Computes eigendecomposition of given matrix primarily using a closed-form algorithm.

This is a variant of compute(const MatrixType&, int options) which normally solves the underlying polynomial equation directly. For supported real binary floating-point 3x3 matrices with coefficients near the overflow limit, it instead uses the iterative solver in scaled coordinates to avoid overflow from roundoff near repeated eigenvalues. This fallback has the runtime characteristics of compute() and may fail to converge. Check info() for Success before using the results; the fallback can report NoConvergence. Non-finite inputs have no guaranteed decomposition. In particular, infinite coefficients can also enter the 3x3 fallback and report NoConvergence; a Success status alone does not validate non-finite input.

Currently only 2x2 and 3x3 matrices for which the sizes are known at compile time are supported (e.g., Matrix3d).

This method is usually significantly faster than the QR iterative algorithm but it might also be less accurate. It is also worth noting that for 3x3 matrices it involves trigonometric operations which are not necessarily available for all scalar types.

For the 3x3 case, we observed the following worst case relative error regarding the eigenvalues:

  • double: 1e-8
  • float: 1e-3
See also
compute(const MatrixType&, int options)

◆ computeFromTridiagonal()

template<typename MatrixType>
SelfAdjointEigenSolver< MatrixType > & Eigen::SelfAdjointEigenSolver< MatrixType >::computeFromTridiagonal ( const RealVectorType & diag,
const SubDiagonalType & subdiag,
int options = ComputeEigenvectors )

Computes the eigen decomposition from a tridiagonal symmetric matrix.

Parameters
[in]diagThe vector containing the diagonal of the matrix.
[in]subdiagThe subdiagonal of the matrix.
[in]optionsCan be ComputeEigenvectors (default) or EigenvaluesOnly.
Returns
Reference to *this

This function assumes that the matrix has been reduced to tridiagonal form.

See also
compute(const MatrixType&, int) for more information

◆ eigenvalues()

template<typename MatrixType_>
const RealVectorType & Eigen::SelfAdjointEigenSolver< MatrixType_ >::eigenvalues ( ) const
inline

Returns the eigenvalues of given matrix.

Returns
A const reference to the column vector containing the eigenvalues.
Precondition
The eigenvalues have been computed before.

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:

// SPDX-FileCopyrightText: The Eigen Authors
// SPDX-License-Identifier: MPL-2.0
MatrixXd ones = MatrixXd::Ones(3, 3);
cout << "The eigenvalues of the 3x3 matrix of ones are:" << endl << es.eigenvalues() << endl;

Output:

The eigenvalues of the 3x3 matrix of ones are:
-2.22e-16
        0
        3
See also
eigenvectors(), MatrixBase::eigenvalues()

◆ eigenvectors()

template<typename MatrixType_>
const EigenvectorsType & Eigen::SelfAdjointEigenSolver< MatrixType_ >::eigenvectors ( ) const
inline

Returns the eigenvectors of given matrix.

Returns
A const reference to the matrix whose columns are the eigenvectors.
Precondition
The eigenvectors have been computed before.

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. If this object was used to solve the eigenproblem for the selfadjoint matrix \( A \), then the matrix returned by this function is the matrix \( V \) in the eigendecomposition \( A = V D V^{-1} \).

For a selfadjoint matrix, \( V \) is unitary, meaning its inverse is equal to its adjoint, \( V^{-1} = V^{\dagger} \). If \( A \) is real, then \( V \) is also real and therefore orthogonal, meaning its inverse is equal to its transpose, \( V^{-1} = V^T \).

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.408
 0.408
See also
eigenvalues()

◆ info()

template<typename MatrixType_>
ComputationInfo Eigen::SelfAdjointEigenSolver< MatrixType_ >::info ( ) const
inline

Reports whether previous computation was successful.

Returns
Success if computation was successful, NoConvergence otherwise.

◆ operatorExp()

template<typename MatrixType_>
PlainMatrixType Eigen::SelfAdjointEigenSolver< MatrixType_ >::operatorExp ( ) const
inline

Computes the matrix exponential of the matrix.

Returns
the matrix exponential of the matrix.
Precondition
The eigenvalues and eigenvectors of a positive-definite matrix have been computed before.
See also
operatorInverseSqrt(), operatorSqrt(), MatrixFunctions Module

◆ operatorInverseSqrt()

template<typename MatrixType_>
PlainMatrixType Eigen::SelfAdjointEigenSolver< MatrixType_ >::operatorInverseSqrt ( ) const
inline

Computes the inverse square root of the matrix.

Returns
the inverse positive-definite square root of the matrix
Precondition
The eigenvalues and eigenvectors of a positive-definite matrix have been computed before.

This function uses the eigendecomposition \( A = V D V^{-1} \) to compute the inverse square root as \( V D^{-1/2} V^{-1} \). This is cheaper than first computing the square root with operatorSqrt() and then its inverse with MatrixBase::inverse().

Example:

// SPDX-FileCopyrightText: The Eigen Authors
// SPDX-License-Identifier: MPL-2.0
MatrixXd X = MatrixXd::Random(4, 4);
MatrixXd A = X * X.transpose();
cout << "Here is a random positive-definite matrix, A:" << endl << A << endl << endl;
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:

Here is a random positive-definite matrix, A:
  0.284    0.15  0.0188   0.107
   0.15    1.08 -0.0233   0.134
 0.0188 -0.0233   0.949  -0.997
  0.107   0.134  -0.997    1.81

The inverse square root of A is: 
   1.99  -0.172   -0.18  -0.166
 -0.172   0.985 -0.0149 -0.0403
  -0.18 -0.0149    1.51   0.581
 -0.166 -0.0403   0.581    1.03
We can also compute it with operatorSqrt() and inverse(). That yields: 
   1.99  -0.172   -0.18  -0.166
 -0.172   0.985 -0.0149 -0.0403
  -0.18 -0.0149    1.51   0.581
 -0.166 -0.0403   0.581    1.03
See also
operatorSqrt(), MatrixBase::inverse(), MatrixFunctions Module

◆ operatorSqrt()

template<typename MatrixType_>
PlainMatrixType Eigen::SelfAdjointEigenSolver< MatrixType_ >::operatorSqrt ( ) const
inline

Computes the positive-definite square root of the matrix.

Returns
the positive-definite square root of the matrix
Precondition
The eigenvalues and eigenvectors of a positive-definite matrix have been computed before.

The square root of a positive-definite matrix \( A \) is the positive-definite matrix whose square equals \( A \). This function uses the eigendecomposition \( A = V D V^{-1} \) to compute the square root as \( A^{1/2} = V D^{1/2} V^{-1} \).

Example:

// SPDX-FileCopyrightText: The Eigen Authors
// SPDX-License-Identifier: MPL-2.0
MatrixXd X = MatrixXd::Random(4, 4);
MatrixXd A = X * X.transpose();
cout << "Here is a random positive-definite matrix, A:" << endl << A << endl << endl;
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:

Here is a random positive-definite matrix, A:
  0.284    0.15  0.0188   0.107
   0.15    1.08 -0.0233   0.134
 0.0188 -0.0233   0.949  -0.997
  0.107   0.134  -0.997    1.81

The square root of A is: 
    0.519     0.094     0.037    0.0669
    0.094      1.03 -0.000133    0.0559
    0.037 -0.000133      0.85    -0.475
   0.0669    0.0559    -0.475      1.26
If we square this, we get: 
  0.284    0.15  0.0188   0.107
   0.15    1.08 -0.0233   0.134
 0.0188 -0.0233   0.949  -0.997
  0.107   0.134  -0.997    1.81
See also
operatorInverseSqrt(), MatrixFunctions Module

Member Data Documentation

◆ m_maxIterations

template<typename MatrixType_>
const int Eigen::SelfAdjointEigenSolver< MatrixType_ >::m_maxIterations
static

Maximum number of iterations.

The algorithm terminates if it does not converge within m_maxIterations * n iterations, where n denotes the size of the matrix. This value is currently set to 30 (copied from LAPACK).


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