Eigen  3.4.90 (git rev 67eeba6e720c5745abc77ae6c92ce0a44aa7b7ae)
ComplexEigenSolver.h
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2009 Claire Maurice
5 // Copyright (C) 2009 Gael Guennebaud <gael.guennebaud@inria.fr>
6 // Copyright (C) 2010,2012 Jitse Niesen <jitse@maths.leeds.ac.uk>
7 //
8 // This Source Code Form is subject to the terms of the Mozilla
9 // Public License v. 2.0. If a copy of the MPL was not distributed
10 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
11 
12 #ifndef EIGEN_COMPLEX_EIGEN_SOLVER_H
13 #define EIGEN_COMPLEX_EIGEN_SOLVER_H
14 
15 #include "./ComplexSchur.h"
16 
17 #include "./InternalHeaderCheck.h"
18 
19 namespace Eigen {
20 
47 template<typename MatrixType_> class ComplexEigenSolver
48 {
49  public:
50 
52  typedef MatrixType_ MatrixType;
53 
54  enum {
55  RowsAtCompileTime = MatrixType::RowsAtCompileTime,
56  ColsAtCompileTime = MatrixType::ColsAtCompileTime,
57  Options = MatrixType::Options,
58  MaxRowsAtCompileTime = MatrixType::MaxRowsAtCompileTime,
59  MaxColsAtCompileTime = MatrixType::MaxColsAtCompileTime
60  };
61 
63  typedef typename MatrixType::Scalar Scalar;
64  typedef typename NumTraits<Scalar>::Real RealScalar;
65  typedef Eigen::Index Index;
66 
73  typedef std::complex<RealScalar> ComplexScalar;
74 
80  typedef Matrix<ComplexScalar, ColsAtCompileTime, 1, Options&(~RowMajor), MaxColsAtCompileTime, 1> EigenvalueType;
81 
88 
95  : m_eivec(),
96  m_eivalues(),
97  m_schur(),
98  m_isInitialized(false),
99  m_eigenvectorsOk(false),
100  m_matX()
101  {}
102 
109  explicit ComplexEigenSolver(Index size)
110  : m_eivec(size, size),
111  m_eivalues(size),
112  m_schur(size),
113  m_isInitialized(false),
114  m_eigenvectorsOk(false),
115  m_matX(size, size)
116  {}
117 
127  template<typename InputType>
128  explicit ComplexEigenSolver(const EigenBase<InputType>& matrix, bool computeEigenvectors = true)
129  : m_eivec(matrix.rows(),matrix.cols()),
130  m_eivalues(matrix.cols()),
131  m_schur(matrix.rows()),
132  m_isInitialized(false),
133  m_eigenvectorsOk(false),
134  m_matX(matrix.rows(),matrix.cols())
135  {
136  compute(matrix.derived(), computeEigenvectors);
137  }
138 
160  {
161  eigen_assert(m_isInitialized && "ComplexEigenSolver is not initialized.");
162  eigen_assert(m_eigenvectorsOk && "The eigenvectors have not been computed together with the eigenvalues.");
163  return m_eivec;
164  }
165 
185  {
186  eigen_assert(m_isInitialized && "ComplexEigenSolver is not initialized.");
187  return m_eivalues;
188  }
189 
214  template<typename InputType>
215  ComplexEigenSolver& compute(const EigenBase<InputType>& matrix, bool computeEigenvectors = true);
216 
222  {
223  eigen_assert(m_isInitialized && "ComplexEigenSolver is not initialized.");
224  return m_schur.info();
225  }
226 
229  {
230  m_schur.setMaxIterations(maxIters);
231  return *this;
232  }
233 
236  {
237  return m_schur.getMaxIterations();
238  }
239 
240  protected:
241 
242  EIGEN_STATIC_ASSERT_NON_INTEGER(Scalar)
243 
244  EigenvectorType m_eivec;
245  EigenvalueType m_eivalues;
246  ComplexSchur<MatrixType> m_schur;
247  bool m_isInitialized;
248  bool m_eigenvectorsOk;
249  EigenvectorType m_matX;
250 
251  private:
252  void doComputeEigenvectors(RealScalar matrixnorm);
253  void sortEigenvalues(bool computeEigenvectors);
254 };
255 
256 
257 template<typename MatrixType>
258 template<typename InputType>
259 ComplexEigenSolver<MatrixType>&
260 ComplexEigenSolver<MatrixType>::compute(const EigenBase<InputType>& matrix, bool computeEigenvectors)
261 {
262  // this code is inspired from Jampack
263  eigen_assert(matrix.cols() == matrix.rows());
264 
265  // Do a complex Schur decomposition, A = U T U^*
266  // The eigenvalues are on the diagonal of T.
267  m_schur.compute(matrix.derived(), computeEigenvectors);
268 
269  if(m_schur.info() == Success)
270  {
271  m_eivalues = m_schur.matrixT().diagonal();
272  if(computeEigenvectors)
273  doComputeEigenvectors(m_schur.matrixT().norm());
274  sortEigenvalues(computeEigenvectors);
275  }
276 
277  m_isInitialized = true;
278  m_eigenvectorsOk = computeEigenvectors;
279  return *this;
280 }
281 
282 
283 template<typename MatrixType>
284 void ComplexEigenSolver<MatrixType>::doComputeEigenvectors(RealScalar matrixnorm)
285 {
286  const Index n = m_eivalues.size();
287 
288  matrixnorm = numext::maxi(matrixnorm,(std::numeric_limits<RealScalar>::min)());
289 
290  // Compute X such that T = X D X^(-1), where D is the diagonal of T.
291  // The matrix X is unit triangular.
292  m_matX = EigenvectorType::Zero(n, n);
293  for(Index k=n-1 ; k>=0 ; k--)
294  {
295  m_matX.coeffRef(k,k) = ComplexScalar(1.0,0.0);
296  // Compute X(i,k) using the (i,k) entry of the equation X T = D X
297  for(Index i=k-1 ; i>=0 ; i--)
298  {
299  m_matX.coeffRef(i,k) = -m_schur.matrixT().coeff(i,k);
300  if(k-i-1>0)
301  m_matX.coeffRef(i,k) -= (m_schur.matrixT().row(i).segment(i+1,k-i-1) * m_matX.col(k).segment(i+1,k-i-1)).value();
302  ComplexScalar z = m_schur.matrixT().coeff(i,i) - m_schur.matrixT().coeff(k,k);
303  if(z==ComplexScalar(0))
304  {
305  // If the i-th and k-th eigenvalue are equal, then z equals 0.
306  // Use a small value instead, to prevent division by zero.
307  numext::real_ref(z) = NumTraits<RealScalar>::epsilon() * matrixnorm;
308  }
309  m_matX.coeffRef(i,k) = m_matX.coeff(i,k) / z;
310  }
311  }
312 
313  // Compute V as V = U X; now A = U T U^* = U X D X^(-1) U^* = V D V^(-1)
314  m_eivec.noalias() = m_schur.matrixU() * m_matX;
315  // .. and normalize the eigenvectors
316  for(Index k=0 ; k<n ; k++)
317  {
318  m_eivec.col(k).normalize();
319  }
320 }
321 
322 
323 template<typename MatrixType>
324 void ComplexEigenSolver<MatrixType>::sortEigenvalues(bool computeEigenvectors)
325 {
326  const Index n = m_eivalues.size();
327  for (Index i=0; i<n; i++)
328  {
329  Index k;
330  m_eivalues.cwiseAbs().tail(n-i).minCoeff(&k);
331  if (k != 0)
332  {
333  k += i;
334  std::swap(m_eivalues[k],m_eivalues[i]);
335  if(computeEigenvectors)
336  m_eivec.col(i).swap(m_eivec.col(k));
337  }
338  }
339 }
340 
341 } // end namespace Eigen
342 
343 #endif // EIGEN_COMPLEX_EIGEN_SOLVER_H
Computes eigenvalues and eigenvectors of general complex matrices.
Definition: ComplexEigenSolver.h:48
std::complex< RealScalar > ComplexScalar
Complex scalar type for MatrixType.
Definition: ComplexEigenSolver.h:73
ComplexEigenSolver(Index size)
Default Constructor with memory preallocation.
Definition: ComplexEigenSolver.h:109
Index getMaxIterations()
Returns the maximum number of iterations.
Definition: ComplexEigenSolver.h:235
ComplexEigenSolver()
Default constructor.
Definition: ComplexEigenSolver.h:94
MatrixType::Scalar Scalar
Scalar type for matrices of type MatrixType.
Definition: ComplexEigenSolver.h:63
ComplexEigenSolver(const EigenBase< InputType > &matrix, bool computeEigenvectors=true)
Constructor; computes eigendecomposition of given matrix.
Definition: ComplexEigenSolver.h:128
ComplexEigenSolver & compute(const EigenBase< InputType > &matrix, bool computeEigenvectors=true)
Computes eigendecomposition of given matrix.
Eigen::Index Index
Definition: ComplexEigenSolver.h:65
Matrix< ComplexScalar, RowsAtCompileTime, ColsAtCompileTime, Options, MaxRowsAtCompileTime, MaxColsAtCompileTime > EigenvectorType
Type for matrix of eigenvectors as returned by eigenvectors().
Definition: ComplexEigenSolver.h:87
ComplexEigenSolver & setMaxIterations(Index maxIters)
Sets the maximum number of iterations allowed.
Definition: ComplexEigenSolver.h:228
const EigenvalueType & eigenvalues() const
Returns the eigenvalues of given matrix.
Definition: ComplexEigenSolver.h:184
MatrixType_ MatrixType
Synonym for the template parameter MatrixType_.
Definition: ComplexEigenSolver.h:52
ComputationInfo info() const
Reports whether previous computation was successful.
Definition: ComplexEigenSolver.h:221
Matrix< ComplexScalar, ColsAtCompileTime, 1, Options &(~RowMajor), MaxColsAtCompileTime, 1 > EigenvalueType
Type for vector of eigenvalues as returned by eigenvalues().
Definition: ComplexEigenSolver.h:80
const EigenvectorType & eigenvectors() const
Returns the eigenvectors of given matrix.
Definition: ComplexEigenSolver.h:159
ComputationInfo info() const
Reports whether previous computation was successful.
Definition: ComplexSchur.h:219
Index getMaxIterations()
Returns the maximum number of iterations.
Definition: ComplexSchur.h:237
ComplexSchur & setMaxIterations(Index maxIters)
Sets the maximum number of iterations allowed.
Definition: ComplexSchur.h:230
The matrix class, also used for vectors and row-vectors.
Definition: Matrix.h:182
ComputationInfo
Definition: Constants.h:442
@ Success
Definition: Constants.h:444
@ RowMajor
Definition: Constants.h:323
Namespace containing all symbols from the Eigen library.
Definition: Core:139
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:59
Definition: EigenBase.h:32
Derived & derived()
Definition: EigenBase.h:48
Holds information about the various numeric (i.e. scalar) types allowed by Eigen.
Definition: NumTraits.h:231