ComplexSchur.h
Go to the documentation of this file.
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_SCHUR_H
13 #define EIGEN_COMPLEX_SCHUR_H
14 
16 
17 #include "./InternalHeaderCheck.h"
18 
19 namespace Eigen {
20 
21 namespace internal {
22 template<typename MatrixType, bool IsComplex> struct complex_schur_reduce_to_hessenberg;
23 }
24 
53 template<typename MatrixType_> class ComplexSchur
54 {
55  public:
56  typedef MatrixType_ MatrixType;
57  enum {
58  RowsAtCompileTime = MatrixType::RowsAtCompileTime,
59  ColsAtCompileTime = MatrixType::ColsAtCompileTime,
60  Options = MatrixType::Options,
61  MaxRowsAtCompileTime = MatrixType::MaxRowsAtCompileTime,
62  MaxColsAtCompileTime = MatrixType::MaxColsAtCompileTime
63  };
64 
66  typedef typename MatrixType::Scalar Scalar;
68  typedef Eigen::Index Index;
69 
76  typedef std::complex<RealScalar> ComplexScalar;
77 
84 
97  : m_matT(size,size),
98  m_matU(size,size),
99  m_hess(size),
100  m_isInitialized(false),
101  m_matUisUptodate(false),
102  m_maxIters(-1)
103  {}
104 
114  template<typename InputType>
115  explicit ComplexSchur(const EigenBase<InputType>& matrix, bool computeU = true)
116  : m_matT(matrix.rows(),matrix.cols()),
117  m_matU(matrix.rows(),matrix.cols()),
118  m_hess(matrix.rows()),
119  m_isInitialized(false),
120  m_matUisUptodate(false),
121  m_maxIters(-1)
122  {
123  compute(matrix.derived(), computeU);
124  }
125 
140  const ComplexMatrixType& matrixU() const
141  {
142  eigen_assert(m_isInitialized && "ComplexSchur is not initialized.");
143  eigen_assert(m_matUisUptodate && "The matrix U has not been computed during the ComplexSchur decomposition.");
144  return m_matU;
145  }
146 
164  const ComplexMatrixType& matrixT() const
165  {
166  eigen_assert(m_isInitialized && "ComplexSchur is not initialized.");
167  return m_matT;
168  }
169 
192  template<typename InputType>
193  ComplexSchur& compute(const EigenBase<InputType>& matrix, bool computeU = true);
194 
212  template<typename HessMatrixType, typename OrthMatrixType>
213  ComplexSchur& computeFromHessenberg(const HessMatrixType& matrixH, const OrthMatrixType& matrixQ, bool computeU=true);
214 
220  {
221  eigen_assert(m_isInitialized && "ComplexSchur is not initialized.");
222  return m_info;
223  }
224 
231  {
232  m_maxIters = maxIters;
233  return *this;
234  }
235 
238  {
239  return m_maxIters;
240  }
241 
247  static const int m_maxIterationsPerRow = 30;
248 
249  protected:
256 
257  private:
260  void reduceToTriangularForm(bool computeU);
261  friend struct internal::complex_schur_reduce_to_hessenberg<MatrixType, NumTraits<Scalar>::IsComplex>;
262 };
263 
267 template<typename MatrixType>
269 {
270  RealScalar d = numext::norm1(m_matT.coeff(i,i)) + numext::norm1(m_matT.coeff(i+1,i+1));
271  RealScalar sd = numext::norm1(m_matT.coeff(i+1,i));
273  {
274  m_matT.coeffRef(i+1,i) = ComplexScalar(0);
275  return true;
276  }
277  return false;
278 }
279 
280 
282 template<typename MatrixType>
284 {
285  using std::abs;
286  if (iter == 10 || iter == 20)
287  {
288  // exceptional shift, taken from http://www.netlib.org/eispack/comqr.f
289  return abs(numext::real(m_matT.coeff(iu,iu-1))) + abs(numext::real(m_matT.coeff(iu-1,iu-2)));
290  }
291 
292  // compute the shift as one of the eigenvalues of t, the 2x2
293  // diagonal block on the bottom of the active submatrix
294  Matrix<ComplexScalar,2,2> t = m_matT.template block<2,2>(iu-1,iu-1);
295  RealScalar normt = t.cwiseAbs().sum();
296  t /= normt; // the normalization by sf is to avoid under/overflow
297 
298  ComplexScalar b = t.coeff(0,1) * t.coeff(1,0);
299  ComplexScalar c = t.coeff(0,0) - t.coeff(1,1);
300  ComplexScalar disc = sqrt(c*c + RealScalar(4)*b);
301  ComplexScalar det = t.coeff(0,0) * t.coeff(1,1) - b;
302  ComplexScalar trace = t.coeff(0,0) + t.coeff(1,1);
303  ComplexScalar eival1 = (trace + disc) / RealScalar(2);
304  ComplexScalar eival2 = (trace - disc) / RealScalar(2);
305  RealScalar eival1_norm = numext::norm1(eival1);
306  RealScalar eival2_norm = numext::norm1(eival2);
307  // A division by zero can only occur if eival1==eival2==0.
308  // In this case, det==0, and all we have to do is checking that eival2_norm!=0
309  if(eival1_norm > eival2_norm)
310  eival2 = det / eival1;
311  else if(!numext::is_exactly_zero(eival2_norm))
312  eival1 = det / eival2;
313 
314  // choose the eigenvalue closest to the bottom entry of the diagonal
315  if(numext::norm1(eival1-t.coeff(1,1)) < numext::norm1(eival2-t.coeff(1,1)))
316  return normt * eival1;
317  else
318  return normt * eival2;
319 }
320 
321 
322 template<typename MatrixType>
323 template<typename InputType>
325 {
326  m_matUisUptodate = false;
327  eigen_assert(matrix.cols() == matrix.rows());
328 
329  if(matrix.cols() == 1)
330  {
331  m_matT = matrix.derived().template cast<ComplexScalar>();
332  if(computeU) m_matU = ComplexMatrixType::Identity(1,1);
333  m_info = Success;
334  m_isInitialized = true;
335  m_matUisUptodate = computeU;
336  return *this;
337  }
338 
339  internal::complex_schur_reduce_to_hessenberg<MatrixType, NumTraits<Scalar>::IsComplex>::run(*this, matrix.derived(), computeU);
340  computeFromHessenberg(m_matT, m_matU, computeU);
341  return *this;
342 }
343 
344 template<typename MatrixType>
345 template<typename HessMatrixType, typename OrthMatrixType>
346 ComplexSchur<MatrixType>& ComplexSchur<MatrixType>::computeFromHessenberg(const HessMatrixType& matrixH, const OrthMatrixType& matrixQ, bool computeU)
347 {
348  m_matT = matrixH;
349  if(computeU)
350  m_matU = matrixQ;
351  reduceToTriangularForm(computeU);
352  return *this;
353 }
354 namespace internal {
355 
356 /* Reduce given matrix to Hessenberg form */
357 template<typename MatrixType, bool IsComplex>
358 struct complex_schur_reduce_to_hessenberg
359 {
360  // this is the implementation for the case IsComplex = true
361  static void run(ComplexSchur<MatrixType>& _this, const MatrixType& matrix, bool computeU)
362  {
363  _this.m_hess.compute(matrix);
364  _this.m_matT = _this.m_hess.matrixH();
365  if(computeU) _this.m_matU = _this.m_hess.matrixQ();
366  }
367 };
368 
369 template<typename MatrixType>
370 struct complex_schur_reduce_to_hessenberg<MatrixType, false>
371 {
372  static void run(ComplexSchur<MatrixType>& _this, const MatrixType& matrix, bool computeU)
373  {
375 
376  // Note: m_hess is over RealScalar; m_matT and m_matU is over ComplexScalar
377  _this.m_hess.compute(matrix);
378  _this.m_matT = _this.m_hess.matrixH().template cast<ComplexScalar>();
379  if(computeU)
380  {
381  // This may cause an allocation which seems to be avoidable
382  MatrixType Q = _this.m_hess.matrixQ();
383  _this.m_matU = Q.template cast<ComplexScalar>();
384  }
385  }
386 };
387 
388 } // end namespace internal
389 
390 // Reduce the Hessenberg matrix m_matT to triangular form by QR iteration.
391 template<typename MatrixType>
393 {
394  Index maxIters = m_maxIters;
395  if (maxIters == -1)
396  maxIters = m_maxIterationsPerRow * m_matT.rows();
397 
398  // The matrix m_matT is divided in three parts.
399  // Rows 0,...,il-1 are decoupled from the rest because m_matT(il,il-1) is zero.
400  // Rows il,...,iu is the part we are working on (the active submatrix).
401  // Rows iu+1,...,end are already brought in triangular form.
402  Index iu = m_matT.cols() - 1;
403  Index il;
404  Index iter = 0; // number of iterations we are working on the (iu,iu) element
405  Index totalIter = 0; // number of iterations for whole matrix
406 
407  while(true)
408  {
409  // find iu, the bottom row of the active submatrix
410  while(iu > 0)
411  {
412  if(!subdiagonalEntryIsNeglegible(iu-1)) break;
413  iter = 0;
414  --iu;
415  }
416 
417  // if iu is zero then we are done; the whole matrix is triangularized
418  if(iu==0) break;
419 
420  // if we spent too many iterations, we give up
421  iter++;
422  totalIter++;
423  if(totalIter > maxIters) break;
424 
425  // find il, the top row of the active submatrix
426  il = iu-1;
427  while(il > 0 && !subdiagonalEntryIsNeglegible(il-1))
428  {
429  --il;
430  }
431 
432  /* perform the QR step using Givens rotations. The first rotation
433  creates a bulge; the (il+2,il) element becomes nonzero. This
434  bulge is chased down to the bottom of the active submatrix. */
435 
436  ComplexScalar shift = computeShift(iu, iter);
438  rot.makeGivens(m_matT.coeff(il,il) - shift, m_matT.coeff(il+1,il));
439  m_matT.rightCols(m_matT.cols()-il).applyOnTheLeft(il, il+1, rot.adjoint());
440  m_matT.topRows((std::min)(il+2,iu)+1).applyOnTheRight(il, il+1, rot);
441  if(computeU) m_matU.applyOnTheRight(il, il+1, rot);
442 
443  for(Index i=il+1 ; i<iu ; i++)
444  {
445  rot.makeGivens(m_matT.coeffRef(i,i-1), m_matT.coeffRef(i+1,i-1), &m_matT.coeffRef(i,i-1));
446  m_matT.coeffRef(i+1,i-1) = ComplexScalar(0);
447  m_matT.rightCols(m_matT.cols()-i).applyOnTheLeft(i, i+1, rot.adjoint());
448  m_matT.topRows((std::min)(i+2,iu)+1).applyOnTheRight(i, i+1, rot);
449  if(computeU) m_matU.applyOnTheRight(i, i+1, rot);
450  }
451  }
452 
453  if(totalIter <= maxIters)
454  m_info = Success;
455  else
457 
458  m_isInitialized = true;
459  m_matUisUptodate = computeU;
460 }
461 
462 } // end namespace Eigen
463 
464 #endif // EIGEN_COMPLEX_SCHUR_H
const AbsReturnType abs() const
Array< int, 3, 1 > b
RealReturnType real() const
Array33i c
MatrixXf Q
#define eigen_assert(x)
Definition: Macros.h:902
Matrix< float, 1, Dynamic > MatrixType
Performs a complex Schur decomposition of a real or complex square matrix.
Definition: ComplexSchur.h:54
ComputationInfo info() const
Reports whether previous computation was successful.
Definition: ComplexSchur.h:219
const ComplexMatrixType & matrixU() const
Returns the unitary matrix in the Schur decomposition.
Definition: ComplexSchur.h:140
MatrixType::Scalar Scalar
Scalar type for matrices of type MatrixType_.
Definition: ComplexSchur.h:66
const ComplexMatrixType & matrixT() const
Returns the triangular matrix in the Schur decomposition.
Definition: ComplexSchur.h:164
HessenbergDecomposition< MatrixType > m_hess
Definition: ComplexSchur.h:251
NumTraits< Scalar >::Real RealScalar
Definition: ComplexSchur.h:67
ComplexSchur(Index size=RowsAtCompileTime==Dynamic ? 1 :RowsAtCompileTime)
Default constructor.
Definition: ComplexSchur.h:96
void reduceToTriangularForm(bool computeU)
Definition: ComplexSchur.h:392
MatrixType_ MatrixType
Definition: ComplexSchur.h:56
Index getMaxIterations()
Returns the maximum number of iterations.
Definition: ComplexSchur.h:237
ComplexMatrixType m_matU
Definition: ComplexSchur.h:250
static const int m_maxIterationsPerRow
Maximum number of iterations per row.
Definition: ComplexSchur.h:247
Eigen::Index Index
Definition: ComplexSchur.h:68
std::complex< RealScalar > ComplexScalar
Complex scalar type for MatrixType_.
Definition: ComplexSchur.h:76
ComplexSchur & computeFromHessenberg(const HessMatrixType &matrixH, const OrthMatrixType &matrixQ, bool computeU=true)
Compute Schur decomposition from a given Hessenberg matrix.
ComplexMatrixType m_matT
Definition: ComplexSchur.h:250
ComplexSchur & setMaxIterations(Index maxIters)
Sets the maximum number of iterations allowed.
Definition: ComplexSchur.h:230
ComplexSchur(const EigenBase< InputType > &matrix, bool computeU=true)
Constructor; computes Schur decomposition of given matrix.
Definition: ComplexSchur.h:115
bool subdiagonalEntryIsNeglegible(Index i)
Definition: ComplexSchur.h:268
Matrix< ComplexScalar, RowsAtCompileTime, ColsAtCompileTime, Options, MaxRowsAtCompileTime, MaxColsAtCompileTime > ComplexMatrixType
Type for the matrices in the Schur decomposition.
Definition: ComplexSchur.h:83
ComplexScalar computeShift(Index iu, Index iter)
Definition: ComplexSchur.h:283
ComputationInfo m_info
Definition: ComplexSchur.h:252
ComplexSchur & compute(const EigenBase< InputType > &matrix, bool computeU=true)
Computes Schur decomposition of given matrix.
internal::traits< Derived >::Scalar Scalar
Definition: DenseBase.h:61
Scalar sum() const
Definition: Redux.h:546
HessenbergDecomposition & compute(const EigenBase< InputType > &matrix)
Computes Hessenberg decomposition of given matrix.
MatrixHReturnType matrixH() const
Constructs the Hessenberg matrix H in the decomposition.
HouseholderSequenceType matrixQ() const
Reconstructs the orthogonal matrix Q in the decomposition.
Rotation given by a cosine-sine pair.
Definition: Jacobi.h:37
JacobiRotation adjoint() const
Definition: Jacobi.h:69
void makeGivens(const Scalar &p, const Scalar &q, Scalar *r=0)
Definition: Jacobi.h:164
EIGEN_CONSTEXPR Index cols() const EIGEN_NOEXCEPT
constexpr const Scalar & coeff(Index rowId, Index colId) const
EIGEN_CONSTEXPR Index rows() const EIGEN_NOEXCEPT
constexpr Scalar & coeffRef(Index rowId, Index colId)
ComputationInfo
Definition: Constants.h:444
@ Success
Definition: Constants.h:446
@ NoConvergence
Definition: Constants.h:450
bfloat16() min(const bfloat16 &a, const bfloat16 &b)
Definition: BFloat16.h:684
bool isMuchSmallerThan(const Scalar &x, const OtherScalar &y, const typename NumTraits< Scalar >::Real &precision=NumTraits< Scalar >::dummy_precision())
bool is_exactly_zero(const X &x)
Definition: Meta.h:475
: InteropHeaders
Definition: Core:139
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:82
const int Dynamic
Definition: Constants.h:24
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_abs_op< typename Derived::Scalar >, const Derived > abs(const Eigen::ArrayBase< Derived > &x)
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_sqrt_op< typename Derived::Scalar >, const Derived > sqrt(const Eigen::ArrayBase< Derived > &x)
Derived & derived()
Definition: EigenBase.h:48
EIGEN_CONSTEXPR Index cols() const EIGEN_NOEXCEPT
Definition: EigenBase.h:65
EIGEN_CONSTEXPR Index rows() const EIGEN_NOEXCEPT
Definition: EigenBase.h:62
Holds information about the various numeric (i.e. scalar) types allowed by Eigen.
Definition: NumTraits.h:231