Shapeworks Studio  2.1
Shape analysis software suite
List of all members | Public Types | Public Member Functions | Static Public Attributes | Protected Attributes | Friends
Eigen::SelfAdjointEigenSolver< _MatrixType > Class Template Reference

Computes eigenvalues and eigenvectors of selfadjoint matrices. More...

#include <SelfAdjointEigenSolver.h>

+ Inheritance diagram for Eigen::SelfAdjointEigenSolver< _MatrixType >:
+ Collaboration diagram for Eigen::SelfAdjointEigenSolver< _MatrixType >:

Public Types

enum  { Size = MatrixType::RowsAtCompileTime, ColsAtCompileTime = MatrixType::ColsAtCompileTime, Options = MatrixType::Options, MaxColsAtCompileTime = MatrixType::MaxColsAtCompileTime }
 
typedef _MatrixType MatrixType
 
typedef MatrixType::Scalar Scalar
 Scalar type for matrices of type _MatrixType.
 
typedef MatrixType::Index Index
 
typedef NumTraits< Scalar >::Real RealScalar
 Real scalar type for _MatrixType. More...
 
typedef internal::plain_col_type< MatrixType, RealScalar >::type RealVectorType
 Type for vector of eigenvalues as returned by eigenvalues(). More...
 
typedef Tridiagonalization< MatrixType > TridiagonalizationType
 

Public Member Functions

 SelfAdjointEigenSolver ()
 Default constructor for fixed-size matrices. More...
 
 SelfAdjointEigenSolver (Index size)
 Constructor, pre-allocates memory for dynamic-size matrices. More...
 
 SelfAdjointEigenSolver (const MatrixType &matrix, int options=ComputeEigenvectors)
 Constructor; computes eigendecomposition of given matrix. More...
 
SelfAdjointEigenSolvercompute (const MatrixType &matrix, int options=ComputeEigenvectors)
 Computes eigendecomposition of given matrix. More...
 
SelfAdjointEigenSolvercomputeDirect (const MatrixType &matrix, int options=ComputeEigenvectors)
 Computes eigendecomposition of given matrix using a direct algorithm. More...
 
const MatrixType & eigenvectors () const
 Returns the eigenvectors of given matrix. More...
 
const RealVectorTypeeigenvalues () const
 Returns the eigenvalues of given matrix. More...
 
MatrixType operatorSqrt () const
 Computes the positive-definite square root of the matrix. More...
 
MatrixType operatorInverseSqrt () const
 Computes the inverse square root of the matrix. More...
 
ComputationInfo info () const
 Reports whether previous computation was successful. More...
 

Static Public Attributes

static const int m_maxIterations = 30
 Maximum number of iterations. More...
 

Protected Attributes

MatrixType m_eivec
 
RealVectorType m_eivalues
 
TridiagonalizationType::SubDiagonalType m_subdiag
 
ComputationInfo m_info
 
bool m_isInitialized
 
bool m_eigenvectorsOk
 

Friends

struct internal::direct_selfadjoint_eigenvalues< SelfAdjointEigenSolver, Size, NumTraits< Scalar >::IsComplex >
 

Detailed Description

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

Computes eigenvalues and eigenvectors of selfadjoint matrices.

Template Parameters
_MatrixTypethe 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} $ (for selfadjoint matrices, the matrix $ V $ 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 $ Av = \lambda Bv $ and the likes, see the class GeneralizedSelfAdjointEigenSolver.

See also
MatrixBase::eigenvalues(), class EigenSolver, class ComplexEigenSolver

Definition at line 68 of file SelfAdjointEigenSolver.h.

Member Typedef Documentation

template<typename _MatrixType>
typedef NumTraits<Scalar>::Real 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.

Definition at line 90 of file SelfAdjointEigenSolver.h.

template<typename _MatrixType>
typedef internal::plain_col_type<MatrixType, RealScalar>::type Eigen::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 99 of file SelfAdjointEigenSolver.h.

Constructor & Destructor Documentation

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:

Output:

 

Definition at line 112 of file SelfAdjointEigenSolver.h.

113  : m_eivec(),
114  m_eivalues(),
115  m_subdiag(),
116  m_isInitialized(false)
117  { }
template<typename _MatrixType>
Eigen::SelfAdjointEigenSolver< _MatrixType >::SelfAdjointEigenSolver ( Index  size)
inline

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

Definition at line 131 of file SelfAdjointEigenSolver.h.

132  : m_eivec(size, size),
133  m_eivalues(size),
134  m_subdiag(size > 1 ? size - 1 : 1),
135  m_isInitialized(false)
136  {}
template<typename _MatrixType>
Eigen::SelfAdjointEigenSolver< _MatrixType >::SelfAdjointEigenSolver ( const MatrixType &  matrix,
int  options = ComputeEigenvectors 
)
inline

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:

Output:

See also
compute(const MatrixType&, int)

Definition at line 153 of file SelfAdjointEigenSolver.h.

154  : m_eivec(matrix.rows(), matrix.cols()),
155  m_eivalues(matrix.cols()),
156  m_subdiag(matrix.rows() > 1 ? matrix.rows() - 1 : 1),
157  m_isInitialized(false)
158  {
159  compute(matrix, options);
160  }
Definition: math3d.h:219
SelfAdjointEigenSolver & compute(const MatrixType &matrix, int options=ComputeEigenvectors)
Computes eigendecomposition of given matrix.

Member Function Documentation

template<typename MatrixType >
SelfAdjointEigenSolver< MatrixType > & Eigen::SelfAdjointEigenSolver< MatrixType >::compute ( const MatrixType &  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:

Output:

See also
SelfAdjointEigenSolver(const MatrixType&, int)

Definition at line 385 of file SelfAdjointEigenSolver.h.

386 {
387  using std::abs;
388  eigen_assert(matrix.cols() == matrix.rows());
389  eigen_assert((options&~(EigVecMask|GenEigMask))==0
390  && (options&EigVecMask)!=EigVecMask
391  && "invalid option parameter");
392  bool computeEigenvectors = (options&ComputeEigenvectors)==ComputeEigenvectors;
393  Index n = matrix.cols();
394  m_eivalues.resize(n,1);
395 
396  if(n==1)
397  {
398  m_eivalues.coeffRef(0,0) = numext::real(matrix.coeff(0,0));
399  if(computeEigenvectors)
400  m_eivec.setOnes(n,n);
401  m_info = Success;
402  m_isInitialized = true;
403  m_eigenvectorsOk = computeEigenvectors;
404  return *this;
405  }
406 
407  // declare some aliases
408  RealVectorType& diag = m_eivalues;
409  MatrixType& mat = m_eivec;
410 
411  // map the matrix coefficients to [-1:1] to avoid over- and underflow.
412  mat = matrix.template triangularView<Lower>();
413  RealScalar scale = mat.cwiseAbs().maxCoeff();
414  if(scale==RealScalar(0)) scale = RealScalar(1);
415  mat.template triangularView<Lower>() /= scale;
416  m_subdiag.resize(n-1);
417  internal::tridiagonalization_inplace(mat, diag, m_subdiag, computeEigenvectors);
418 
419  Index end = n-1;
420  Index start = 0;
421  Index iter = 0; // total number of iterations
422 
423  while (end>0)
424  {
425  for (Index i = start; i<end; ++i)
426  if (internal::isMuchSmallerThan(abs(m_subdiag[i]),(abs(diag[i])+abs(diag[i+1]))))
427  m_subdiag[i] = 0;
428 
429  // find the largest unreduced block
430  while (end>0 && m_subdiag[end-1]==0)
431  {
432  end--;
433  }
434  if (end<=0)
435  break;
436 
437  // if we spent too many iterations, we give up
438  iter++;
439  if(iter > m_maxIterations * n) break;
440 
441  start = end - 1;
442  while (start>0 && m_subdiag[start-1]!=0)
443  start--;
444 
445  internal::tridiagonal_qr_step<MatrixType::Flags&RowMajorBit ? RowMajor : ColMajor>(diag.data(), m_subdiag.data(), start, end, computeEigenvectors ? m_eivec.data() : (Scalar*)0, n);
446  }
447 
448  if (iter <= m_maxIterations * n)
449  m_info = Success;
450  else
451  m_info = NoConvergence;
452 
453  // Sort eigenvalues and corresponding vectors.
454  // TODO make the sort optional ?
455  // TODO use a better sort algorithm !!
456  if (m_info == Success)
457  {
458  for (Index i = 0; i < n-1; ++i)
459  {
460  Index k;
461  m_eivalues.segment(i,n-i).minCoeff(&k);
462  if (k > 0)
463  {
464  std::swap(m_eivalues[i], m_eivalues[k+i]);
465  if(computeEigenvectors)
466  m_eivec.col(i).swap(m_eivec.col(k+i));
467  }
468  }
469  }
470 
471  // scale back the eigen values
472  m_eivalues *= scale;
473 
474  m_isInitialized = true;
475  m_eigenvectorsOk = computeEigenvectors;
476  return *this;
477 }
static const int m_maxIterations
Maximum number of iterations.
MatrixType::Scalar Scalar
Scalar type for matrices of type _MatrixType.
Definition: math3d.h:219
NumTraits< Scalar >::Real RealScalar
Real scalar type for _MatrixType.
EIGEN_STRONG_INLINE void resize(Index nbRows, Index nbCols)
EIGEN_STRONG_INLINE const Scalar * data() const
internal::plain_col_type< MatrixType, RealScalar >::type RealVectorType
Type for vector of eigenvalues as returned by eigenvalues().
template<typename MatrixType >
SelfAdjointEigenSolver< MatrixType > & Eigen::SelfAdjointEigenSolver< MatrixType >::computeDirect ( const MatrixType &  matrix,
int  options = ComputeEigenvectors 
)

Computes eigendecomposition of given matrix using a direct algorithm.

This is a variant of compute(const MatrixType&, int options) which directly solves the underlying polynomial equation.

Currently only 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 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.

See also
compute(const MatrixType&, int options)

Definition at line 732 of file SelfAdjointEigenSolver.h.

733 {
734  internal::direct_selfadjoint_eigenvalues<SelfAdjointEigenSolver,Size,NumTraits<Scalar>::IsComplex>::run(*this,matrix,options);
735  return *this;
736 }
Definition: math3d.h:219
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:

Output:

See also
eigenvectors(), MatrixBase::eigenvalues()

Definition at line 250 of file SelfAdjointEigenSolver.h.

251  {
252  eigen_assert(m_isInitialized && "SelfAdjointEigenSolver is not initialized.");
253  return m_eivalues;
254  }
template<typename _MatrixType>
const MatrixType& 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} $.

Example:

Output:

See also
eigenvalues()

Definition at line 228 of file SelfAdjointEigenSolver.h.

229  {
230  eigen_assert(m_isInitialized && "SelfAdjointEigenSolver is not initialized.");
231  eigen_assert(m_eigenvectorsOk && "The eigenvectors have not been computed together with the eigenvalues.");
232  return m_eivec;
233  }
template<typename _MatrixType>
ComputationInfo Eigen::SelfAdjointEigenSolver< _MatrixType >::info ( ) const
inline

Reports whether previous computation was successful.

Returns
Success if computation was succesful, NoConvergence otherwise.

Definition at line 310 of file SelfAdjointEigenSolver.h.

311  {
312  eigen_assert(m_isInitialized && "SelfAdjointEigenSolver is not initialized.");
313  return m_info;
314  }
template<typename _MatrixType>
MatrixType 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:

Output:

See also
operatorSqrt(), MatrixBase::inverse(), MatrixFunctions Module

Definition at line 299 of file SelfAdjointEigenSolver.h.

300  {
301  eigen_assert(m_isInitialized && "SelfAdjointEigenSolver is not initialized.");
302  eigen_assert(m_eigenvectorsOk && "The eigenvectors have not been computed together with the eigenvalues.");
303  return m_eivec * m_eivalues.cwiseInverse().cwiseSqrt().asDiagonal() * m_eivec.adjoint();
304  }
template<typename _MatrixType>
MatrixType 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:

Output:

See also
operatorInverseSqrt(), MatrixFunctions Module

Definition at line 274 of file SelfAdjointEigenSolver.h.

275  {
276  eigen_assert(m_isInitialized && "SelfAdjointEigenSolver is not initialized.");
277  eigen_assert(m_eigenvectorsOk && "The eigenvectors have not been computed together with the eigenvalues.");
278  return m_eivec * m_eivalues.cwiseSqrt().asDiagonal() * m_eivec.adjoint();
279  }

Member Data Documentation

template<typename _MatrixType>
const int Eigen::SelfAdjointEigenSolver< _MatrixType >::m_maxIterations = 30
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).

Definition at line 321 of file SelfAdjointEigenSolver.h.


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