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

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

#include <EigenSolver.h>

+ Collaboration diagram for Eigen::EigenSolver< _MatrixType >:

Public Types

enum  {
  RowsAtCompileTime = MatrixType::RowsAtCompileTime, ColsAtCompileTime = MatrixType::ColsAtCompileTime, Options = MatrixType::Options, MaxRowsAtCompileTime = MatrixType::MaxRowsAtCompileTime,
  MaxColsAtCompileTime = MatrixType::MaxColsAtCompileTime
}
 
typedef _MatrixType MatrixType
 Synonym for the template parameter _MatrixType.
 
typedef MatrixType::Scalar Scalar
 Scalar type for matrices of type MatrixType.
 
typedef NumTraits< Scalar >::Real RealScalar
 
typedef MatrixType::Index Index
 
typedef std::complex< RealScalar > ComplexScalar
 Complex scalar type for MatrixType. More...
 
typedef Matrix< ComplexScalar, ColsAtCompileTime, 1, Options &~RowMajor, MaxColsAtCompileTime, 1 > EigenvalueType
 Type for vector of eigenvalues as returned by eigenvalues(). More...
 
typedef Matrix< ComplexScalar, RowsAtCompileTime, ColsAtCompileTime, Options, MaxRowsAtCompileTime, MaxColsAtCompileTime > EigenvectorsType
 Type for matrix of eigenvectors as returned by eigenvectors(). More...
 

Public Member Functions

 EigenSolver ()
 Default constructor. More...
 
 EigenSolver (Index size)
 Default constructor with memory preallocation. More...
 
 EigenSolver (const MatrixType &matrix, bool computeEigenvectors=true)
 Constructor; computes eigendecomposition of given matrix. More...
 
EigenvectorsType eigenvectors () const
 Returns the eigenvectors of given matrix. More...
 
const MatrixTypepseudoEigenvectors () const
 Returns the pseudo-eigenvectors of given matrix. More...
 
MatrixType pseudoEigenvalueMatrix () const
 Returns the block-diagonal matrix in the pseudo-eigendecomposition. More...
 
const EigenvalueTypeeigenvalues () const
 Returns the eigenvalues of given matrix. More...
 
EigenSolvercompute (const MatrixType &matrix, bool computeEigenvectors=true)
 Computes eigendecomposition of given matrix. More...
 
ComputationInfo info () const
 
EigenSolversetMaxIterations (Index maxIters)
 Sets the maximum number of iterations allowed.
 
Index getMaxIterations ()
 Returns the maximum number of iterations.
 

Protected Types

typedef Matrix< Scalar, ColsAtCompileTime, 1, Options &~RowMajor, MaxColsAtCompileTime, 1 > ColumnVectorType
 

Protected Attributes

MatrixType m_eivec
 
EigenvalueType m_eivalues
 
bool m_isInitialized
 
bool m_eigenvectorsOk
 
RealSchur< MatrixTypem_realSchur
 
MatrixType m_matT
 
ColumnVectorType m_tmp
 

Detailed Description

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

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

Definition at line 64 of file EigenSolver.h.

Member Typedef Documentation

template<typename _MatrixType>
typedef std::complex<RealScalar> 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.

Definition at line 90 of file EigenSolver.h.

template<typename _MatrixType>
typedef Matrix<ComplexScalar, ColsAtCompileTime, 1, Options & ~RowMajor, MaxColsAtCompileTime, 1> 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.

Definition at line 97 of file EigenSolver.h.

template<typename _MatrixType>
typedef Matrix<ComplexScalar, RowsAtCompileTime, ColsAtCompileTime, Options, MaxRowsAtCompileTime, MaxColsAtCompileTime> 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.

Definition at line 104 of file EigenSolver.h.

Constructor & Destructor Documentation

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.

Definition at line 113 of file EigenSolver.h.

113 : m_eivec(), m_eivalues(), m_isInitialized(false), m_realSchur(), m_matT(), m_tmp() {}
template<typename _MatrixType>
Eigen::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.

See also
EigenSolver()

Definition at line 121 of file EigenSolver.h.

122  : m_eivec(size, size),
123  m_eivalues(size),
124  m_isInitialized(false),
125  m_eigenvectorsOk(false),
126  m_realSchur(size),
127  m_matT(size, size),
128  m_tmp(size)
129  {}
template<typename _MatrixType>
Eigen::EigenSolver< _MatrixType >::EigenSolver ( const MatrixType matrix,
bool  computeEigenvectors = true 
)
inline

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 calls compute() to compute the eigenvalues and eigenvectors.

Example:

Output:

See also
compute()

Definition at line 146 of file EigenSolver.h.

147  : m_eivec(matrix.rows(), matrix.cols()),
148  m_eivalues(matrix.cols()),
149  m_isInitialized(false),
150  m_eigenvectorsOk(false),
151  m_realSchur(matrix.cols()),
152  m_matT(matrix.rows(), matrix.cols()),
153  m_tmp(matrix.cols())
154  {
155  compute(matrix, computeEigenvectors);
156  }
Definition: math3d.h:219
EigenSolver & compute(const MatrixType &matrix, bool computeEigenvectors=true)
Computes eigendecomposition of given matrix.
Definition: EigenSolver.h:365

Member Function Documentation

template<typename MatrixType >
EigenSolver< MatrixType > & Eigen::EigenSolver< MatrixType >::compute ( const MatrixType 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 of the allocated data in the EigenSolver object.

Example:

Output:

 

Definition at line 365 of file EigenSolver.h.

366 {
367  using std::sqrt;
368  using std::abs;
369  eigen_assert(matrix.cols() == matrix.rows());
370 
371  // Reduce to real Schur form.
372  m_realSchur.compute(matrix, computeEigenvectors);
373 
374  if (m_realSchur.info() == Success)
375  {
376  m_matT = m_realSchur.matrixT();
377  if (computeEigenvectors)
378  m_eivec = m_realSchur.matrixU();
379 
380  // Compute eigenvalues from matT
381  m_eivalues.resize(matrix.cols());
382  Index i = 0;
383  while (i < matrix.cols())
384  {
385  if (i == matrix.cols() - 1 || m_matT.coeff(i+1, i) == Scalar(0))
386  {
387  m_eivalues.coeffRef(i) = m_matT.coeff(i, i);
388  ++i;
389  }
390  else
391  {
392  Scalar p = Scalar(0.5) * (m_matT.coeff(i, i) - m_matT.coeff(i+1, i+1));
393  Scalar z = sqrt(abs(p * p + m_matT.coeff(i+1, i) * m_matT.coeff(i, i+1)));
394  m_eivalues.coeffRef(i) = ComplexScalar(m_matT.coeff(i+1, i+1) + p, z);
395  m_eivalues.coeffRef(i+1) = ComplexScalar(m_matT.coeff(i+1, i+1) + p, -z);
396  i += 2;
397  }
398  }
399 
400  // Compute eigenvectors.
401  if (computeEigenvectors)
402  doComputeEigenvectors();
403  }
404 
405  m_isInitialized = true;
406  m_eigenvectorsOk = computeEigenvectors;
407 
408  return *this;
409 }
std::complex< RealScalar > ComplexScalar
Complex scalar type for MatrixType.
Definition: EigenSolver.h:90
Definition: math3d.h:219
const MatrixType & matrixU() const
Returns the orthogonal matrix in the Schur decomposition.
Definition: RealSchur.h:126
ComputationInfo info() const
Reports whether previous computation was successful.
Definition: RealSchur.h:193
RealSchur & compute(const MatrixType &matrix, bool computeU=true)
Computes Schur decomposition of given matrix.
Definition: RealSchur.h:246
EIGEN_STRONG_INLINE void resize(Index nbRows, Index nbCols)
MatrixType::Scalar Scalar
Scalar type for matrices of type MatrixType.
Definition: EigenSolver.h:80
const MatrixType & matrixT() const
Returns the quasi-triangular matrix in the Schur decomposition.
Definition: RealSchur.h:143
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:

Output:

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

Definition at line 243 of file EigenSolver.h.

244  {
245  eigen_assert(m_isInitialized && "EigenSolver is not initialized.");
246  return m_eivalues;
247  }
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:

Output:

See also
eigenvalues(), pseudoEigenvectors()

Definition at line 333 of file EigenSolver.h.

334 {
335  eigen_assert(m_isInitialized && "EigenSolver is not initialized.");
336  eigen_assert(m_eigenvectorsOk && "The eigenvectors have not been computed together with the eigenvalues.");
337  Index n = m_eivec.cols();
338  EigenvectorsType matV(n,n);
339  for (Index j=0; j<n; ++j)
340  {
341  if (internal::isMuchSmallerThan(numext::imag(m_eivalues.coeff(j)), numext::real(m_eivalues.coeff(j))) || j+1==n)
342  {
343  // we have a real eigen value
344  matV.col(j) = m_eivec.col(j).template cast<ComplexScalar>();
345  matV.col(j).normalize();
346  }
347  else
348  {
349  // we have a pair of complex eigen values
350  for (Index i=0; i<n; ++i)
351  {
352  matV.coeffRef(i,j) = ComplexScalar(m_eivec.coeff(i,j), m_eivec.coeff(i,j+1));
353  matV.coeffRef(i,j+1) = ComplexScalar(m_eivec.coeff(i,j), -m_eivec.coeff(i,j+1));
354  }
355  matV.col(j).normalize();
356  matV.col(j+1).normalize();
357  ++j;
358  }
359  }
360  return matV;
361 }
std::complex< RealScalar > ComplexScalar
Complex scalar type for MatrixType.
Definition: EigenSolver.h:90
Matrix< ComplexScalar, RowsAtCompileTime, ColsAtCompileTime, Options, MaxRowsAtCompileTime, MaxColsAtCompileTime > EigenvectorsType
Type for matrix of eigenvectors as returned by eigenvectors().
Definition: EigenSolver.h:104
template<typename MatrixType >
MatrixType 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()

Definition at line 313 of file EigenSolver.h.

314 {
315  eigen_assert(m_isInitialized && "EigenSolver is not initialized.");
316  Index n = m_eivalues.rows();
317  MatrixType matD = MatrixType::Zero(n,n);
318  for (Index i=0; i<n; ++i)
319  {
320  if (internal::isMuchSmallerThan(numext::imag(m_eivalues.coeff(i)), numext::real(m_eivalues.coeff(i))))
321  matD.coeffRef(i,i) = numext::real(m_eivalues.coeff(i));
322  else
323  {
324  matD.template block<2,2>(i,i) << numext::real(m_eivalues.coeff(i)), numext::imag(m_eivalues.coeff(i)),
325  -numext::imag(m_eivalues.coeff(i)), numext::real(m_eivalues.coeff(i));
326  ++i;
327  }
328  }
329  return matD;
330 }
_MatrixType MatrixType
Synonym for the template parameter _MatrixType.
Definition: EigenSolver.h:69
template<typename _MatrixType>
const MatrixType& 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:

Output:

See also
pseudoEigenvalueMatrix(), eigenvectors()

Definition at line 198 of file EigenSolver.h.

199  {
200  eigen_assert(m_isInitialized && "EigenSolver is not initialized.");
201  eigen_assert(m_eigenvectorsOk && "The eigenvectors have not been computed together with the eigenvalues.");
202  return m_eivec;
203  }

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