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 // IWYU pragma: private
18 #include "./InternalHeaderCheck.h"
19 
20 namespace Eigen {
21 
22 namespace internal {
23 template <typename MatrixType, bool IsComplex>
24 struct complex_schur_reduce_to_hessenberg;
25 }
26 
55 template <typename MatrixType_>
56 class ComplexSchur {
57  public:
58  typedef MatrixType_ MatrixType;
59  enum {
60  RowsAtCompileTime = MatrixType::RowsAtCompileTime,
61  ColsAtCompileTime = MatrixType::ColsAtCompileTime,
63  MaxRowsAtCompileTime = MatrixType::MaxRowsAtCompileTime,
64  MaxColsAtCompileTime = MatrixType::MaxColsAtCompileTime
65  };
66 
68  typedef typename MatrixType::Scalar Scalar;
70  typedef Eigen::Index Index;
71 
78  typedef std::complex<RealScalar> ComplexScalar;
79 
88 
101  : m_matT(size, size),
102  m_matU(size, size),
103  m_hess(size),
104  m_isInitialized(false),
105  m_matUisUptodate(false),
106  m_maxIters(-1) {}
107 
117  template <typename InputType>
118  explicit ComplexSchur(const EigenBase<InputType>& matrix, bool computeU = true)
119  : m_matT(matrix.rows(), matrix.cols()),
120  m_matU(matrix.rows(), matrix.cols()),
121  m_hess(matrix.rows()),
122  m_isInitialized(false),
123  m_matUisUptodate(false),
124  m_maxIters(-1) {
125  compute(matrix.derived(), computeU);
126  }
127 
142  const ComplexMatrixType& matrixU() const {
143  eigen_assert(m_isInitialized && "ComplexSchur is not initialized.");
144  eigen_assert(m_matUisUptodate && "The matrix U has not been computed during the ComplexSchur decomposition.");
145  return m_matU;
146  }
147 
165  const ComplexMatrixType& matrixT() const {
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,
214  bool computeU = true);
215 
221  eigen_assert(m_isInitialized && "ComplexSchur is not initialized.");
222  return m_info;
223  }
224 
231  m_maxIters = maxIters;
232  return *this;
233  }
234 
237 
243  static const int m_maxIterationsPerRow = 30;
244 
245  protected:
252 
253  private:
256  void reduceToTriangularForm(bool computeU);
258 };
259 
263 template <typename MatrixType>
265  RealScalar d = numext::norm1(m_matT.coeff(i, i)) + numext::norm1(m_matT.coeff(i + 1, i + 1));
266  RealScalar sd = numext::norm1(m_matT.coeff(i + 1, i));
268  m_matT.coeffRef(i + 1, i) = ComplexScalar(0);
269  return true;
270  }
271  return false;
272 }
273 
275 template <typename MatrixType>
277  using std::abs;
278  if ((iter == 10 || iter == 20) && iu > 1) {
279  // exceptional shift, taken from http://www.netlib.org/eispack/comqr.f
280  return abs(numext::real(m_matT.coeff(iu, iu - 1))) + abs(numext::real(m_matT.coeff(iu - 1, iu - 2)));
281  }
282 
283  // compute the shift as one of the eigenvalues of t, the 2x2
284  // diagonal block on the bottom of the active submatrix
285  Matrix<ComplexScalar, 2, 2> t = m_matT.template block<2, 2>(iu - 1, iu - 1);
286  RealScalar normt = t.cwiseAbs().sum();
287  t /= normt; // the normalization by sf is to avoid under/overflow
288 
289  ComplexScalar b = t.coeff(0, 1) * t.coeff(1, 0);
290  ComplexScalar c = t.coeff(0, 0) - t.coeff(1, 1);
291  ComplexScalar disc = sqrt(c * c + RealScalar(4) * b);
292  ComplexScalar det = t.coeff(0, 0) * t.coeff(1, 1) - b;
293  ComplexScalar trace = t.coeff(0, 0) + t.coeff(1, 1);
294  ComplexScalar eival1 = (trace + disc) / RealScalar(2);
295  ComplexScalar eival2 = (trace - disc) / RealScalar(2);
296  RealScalar eival1_norm = numext::norm1(eival1);
297  RealScalar eival2_norm = numext::norm1(eival2);
298  // A division by zero can only occur if eival1==eival2==0.
299  // In this case, det==0, and all we have to do is checking that eival2_norm!=0
300  if (eival1_norm > eival2_norm)
301  eival2 = det / eival1;
302  else if (!numext::is_exactly_zero(eival2_norm))
303  eival1 = det / eival2;
304 
305  // choose the eigenvalue closest to the bottom entry of the diagonal
306  if (numext::norm1(eival1 - t.coeff(1, 1)) < numext::norm1(eival2 - t.coeff(1, 1)))
307  return normt * eival1;
308  else
309  return normt * eival2;
310 }
311 
312 template <typename MatrixType>
313 template <typename InputType>
315  m_matUisUptodate = false;
316  eigen_assert(matrix.cols() == matrix.rows());
317 
318  if (matrix.cols() == 1) {
319  m_matT = matrix.derived().template cast<ComplexScalar>();
320  if (computeU) m_matU = ComplexMatrixType::Identity(1, 1);
321  m_info = Success;
322  m_isInitialized = true;
323  m_matUisUptodate = computeU;
324  return *this;
325  }
326 
328  computeU);
329  computeFromHessenberg(m_matT, m_matU, computeU);
330  return *this;
331 }
332 
333 template <typename MatrixType>
334 template <typename HessMatrixType, typename OrthMatrixType>
336  const OrthMatrixType& matrixQ,
337  bool computeU) {
338  m_matT = matrixH;
339  if (computeU) m_matU = matrixQ;
340  reduceToTriangularForm(computeU);
341  return *this;
342 }
343 namespace internal {
344 
345 /* Reduce given matrix to Hessenberg form */
346 template <typename MatrixType, bool IsComplex>
348  // this is the implementation for the case IsComplex = true
349  static void run(ComplexSchur<MatrixType>& _this, const MatrixType& matrix, bool computeU) {
350  _this.m_hess.compute(matrix);
351  _this.m_matT = _this.m_hess.matrixH();
352  if (computeU) _this.m_matU = _this.m_hess.matrixQ();
353  }
354 };
355 
356 template <typename MatrixType>
358  static void run(ComplexSchur<MatrixType>& _this, const MatrixType& matrix, bool computeU) {
360 
361  // Note: m_hess is over RealScalar; m_matT and m_matU is over ComplexScalar
362  _this.m_hess.compute(matrix);
363  _this.m_matT = _this.m_hess.matrixH().template cast<ComplexScalar>();
364  if (computeU) {
365  // This may cause an allocation which seems to be avoidable
366  MatrixType Q = _this.m_hess.matrixQ();
367  _this.m_matU = Q.template cast<ComplexScalar>();
368  }
369  }
370 };
371 
372 } // end namespace internal
373 
374 // Reduce the Hessenberg matrix m_matT to triangular form by QR iteration.
375 template <typename MatrixType>
377  Index maxIters = m_maxIters;
378  if (maxIters == -1) maxIters = m_maxIterationsPerRow * m_matT.rows();
379 
380  // The matrix m_matT is divided in three parts.
381  // Rows 0,...,il-1 are decoupled from the rest because m_matT(il,il-1) is zero.
382  // Rows il,...,iu is the part we are working on (the active submatrix).
383  // Rows iu+1,...,end are already brought in triangular form.
384  Index iu = m_matT.cols() - 1;
385  Index il;
386  Index iter = 0; // number of iterations we are working on the (iu,iu) element
387  Index totalIter = 0; // number of iterations for whole matrix
388 
389  while (true) {
390  // find iu, the bottom row of the active submatrix
391  while (iu > 0) {
392  if (!subdiagonalEntryIsNeglegible(iu - 1)) break;
393  iter = 0;
394  --iu;
395  }
396 
397  // if iu is zero then we are done; the whole matrix is triangularized
398  if (iu == 0) break;
399 
400  // if we spent too many iterations, we give up
401  iter++;
402  totalIter++;
403  if (totalIter > maxIters) break;
404 
405  // find il, the top row of the active submatrix
406  il = iu - 1;
407  while (il > 0 && !subdiagonalEntryIsNeglegible(il - 1)) {
408  --il;
409  }
410 
411  /* perform the QR step using Givens rotations. The first rotation
412  creates a bulge; the (il+2,il) element becomes nonzero. This
413  bulge is chased down to the bottom of the active submatrix. */
414 
415  ComplexScalar shift = computeShift(iu, iter);
417  rot.makeGivens(m_matT.coeff(il, il) - shift, m_matT.coeff(il + 1, il));
418  m_matT.rightCols(m_matT.cols() - il).applyOnTheLeft(il, il + 1, rot.adjoint());
419  m_matT.topRows((std::min)(il + 2, iu) + 1).applyOnTheRight(il, il + 1, rot);
420  if (computeU) m_matU.applyOnTheRight(il, il + 1, rot);
421 
422  for (Index i = il + 1; i < iu; i++) {
423  rot.makeGivens(m_matT.coeffRef(i, i - 1), m_matT.coeffRef(i + 1, i - 1), &m_matT.coeffRef(i, i - 1));
424  m_matT.coeffRef(i + 1, i - 1) = ComplexScalar(0);
425  m_matT.rightCols(m_matT.cols() - i).applyOnTheLeft(i, i + 1, rot.adjoint());
426  m_matT.topRows((std::min)(i + 2, iu) + 1).applyOnTheRight(i, i + 1, rot);
427  if (computeU) m_matU.applyOnTheRight(i, i + 1, rot);
428  }
429  }
430 
431  if (totalIter <= maxIters)
432  m_info = Success;
433  else
435 
436  m_isInitialized = true;
437  m_matUisUptodate = computeU;
438 }
439 
440 } // end namespace Eigen
441 
442 #endif // EIGEN_COMPLEX_SCHUR_H
AnnoyingScalar abs(const AnnoyingScalar &x)
Definition: AnnoyingScalar.h:135
AnnoyingScalar sqrt(const AnnoyingScalar &x)
Definition: AnnoyingScalar.h:134
int i
Definition: BiCGSTAB_step_by_step.cpp:9
MatrixXf Q
Definition: HouseholderQR_householderQ.cpp:1
#define eigen_assert(x)
Definition: Macros.h:910
int rows
Definition: Tutorial_commainit_02.cpp:1
int cols
Definition: Tutorial_commainit_02.cpp:1
Scalar Scalar int size
Definition: benchVecAdd.cpp:17
Scalar * b
Definition: benchVecAdd.cpp:17
SCALAR Scalar
Definition: bench_gemm.cpp:45
MatrixXf MatrixType
Definition: benchmark-blocking-sizes.cpp:52
Performs a complex Schur decomposition of a real or complex square matrix.
Definition: ComplexSchur.h:56
ComputationInfo info() const
Reports whether previous computation was successful.
Definition: ComplexSchur.h:220
bool m_matUisUptodate
Definition: ComplexSchur.h:250
const ComplexMatrixType & matrixU() const
Returns the unitary matrix in the Schur decomposition.
Definition: ComplexSchur.h:142
MatrixType::Scalar Scalar
Scalar type for matrices of type MatrixType_.
Definition: ComplexSchur.h:68
const ComplexMatrixType & matrixT() const
Returns the triangular matrix in the Schur decomposition.
Definition: ComplexSchur.h:165
bool m_isInitialized
Definition: ComplexSchur.h:249
HessenbergDecomposition< MatrixType > m_hess
Definition: ComplexSchur.h:247
NumTraits< Scalar >::Real RealScalar
Definition: ComplexSchur.h:69
ComplexSchur(Index size=RowsAtCompileTime==Dynamic ? 1 :RowsAtCompileTime)
Default constructor.
Definition: ComplexSchur.h:100
void reduceToTriangularForm(bool computeU)
Definition: ComplexSchur.h:376
MatrixType_ MatrixType
Definition: ComplexSchur.h:58
Index getMaxIterations()
Returns the maximum number of iterations.
Definition: ComplexSchur.h:236
ComplexMatrixType m_matU
Definition: ComplexSchur.h:246
static const int m_maxIterationsPerRow
Maximum number of iterations per row.
Definition: ComplexSchur.h:243
Eigen::Index Index
Definition: ComplexSchur.h:70
std::complex< RealScalar > ComplexScalar
Complex scalar type for MatrixType_.
Definition: ComplexSchur.h:78
ComplexSchur & computeFromHessenberg(const HessMatrixType &matrixH, const OrthMatrixType &matrixQ, bool computeU=true)
Compute Schur decomposition from a given Hessenberg matrix.
@ MaxRowsAtCompileTime
Definition: ComplexSchur.h:63
@ Options
Definition: ComplexSchur.h:62
@ ColsAtCompileTime
Definition: ComplexSchur.h:61
@ MaxColsAtCompileTime
Definition: ComplexSchur.h:64
@ RowsAtCompileTime
Definition: ComplexSchur.h:60
ComplexMatrixType m_matT
Definition: ComplexSchur.h:246
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:118
bool subdiagonalEntryIsNeglegible(Index i)
Definition: ComplexSchur.h:264
Matrix< ComplexScalar, RowsAtCompileTime, ColsAtCompileTime, Options, MaxRowsAtCompileTime, MaxColsAtCompileTime > ComplexMatrixType
Type for the matrices in the Schur decomposition.
Definition: ComplexSchur.h:87
ComplexScalar computeShift(Index iu, Index iter)
Definition: ComplexSchur.h:276
ComputationInfo m_info
Definition: ComplexSchur.h:248
Index m_maxIters
Definition: ComplexSchur.h:251
ComplexSchur & compute(const EigenBase< InputType > &matrix, bool computeU=true)
Computes Schur decomposition of given matrix.
HessenbergDecomposition & compute(const EigenBase< InputType > &matrix)
Computes Hessenberg decomposition of given matrix.
Definition: HessenbergDecomposition.h:147
MatrixHReturnType matrixH() const
Constructs the Hessenberg matrix H in the decomposition.
Definition: HessenbergDecomposition.h:250
HouseholderSequenceType matrixQ() const
Reconstructs the orthogonal matrix Q in the decomposition.
Definition: HessenbergDecomposition.h:225
Rotation given by a cosine-sine pair.
Definition: Jacobi.h:38
The matrix class, also used for vectors and row-vectors.
Definition: Eigen/Eigen/src/Core/Matrix.h:186
EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE Scalar & coeffRef(Index rowId, Index colId)
Definition: PlainObjectBase.h:217
EIGEN_DEVICE_FUNC constexpr EIGEN_STRONG_INLINE const Scalar & coeff(Index rowId, Index colId) const
Definition: PlainObjectBase.h:198
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE EIGEN_CONSTEXPR Index cols() const EIGEN_NOEXCEPT
Definition: PlainObjectBase.h:192
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE EIGEN_CONSTEXPR Index rows() const EIGEN_NOEXCEPT
Definition: PlainObjectBase.h:191
@ IsComplex
Definition: common.h:73
Eigen::Map< Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor >, 0, Eigen::OuterStride<> > matrix(T *data, int rows, int cols, int stride)
Definition: common.h:85
float real
Definition: datatypes.h:10
#define min(a, b)
Definition: datatypes.h:22
ComputationInfo
Definition: Constants.h:438
@ Success
Definition: Constants.h:440
@ NoConvergence
Definition: Constants.h:444
EIGEN_BLAS_FUNC() rot(int *n, Scalar *px, int *incx, Scalar *py, int *incy, Scalar *pc, Scalar *ps)
Definition: level1_real_impl.h:88
EIGEN_DEVICE_FUNC bool isMuchSmallerThan(const Scalar &x, const OtherScalar &y, const typename NumTraits< Scalar >::Real &precision=NumTraits< Scalar >::dummy_precision())
Definition: MathFunctions.h:1916
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bool is_exactly_zero(const X &x)
Definition: Meta.h:592
Namespace containing all symbols from the Eigen library.
Definition: bench_norm.cpp:70
auto run(Kernel kernel, Args &&... args) -> decltype(kernel(args...))
Definition: gpu_test_helper.h:414
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:83
const int Dynamic
Definition: Constants.h:25
int c
Definition: calibrate.py:100
Definition: Eigen_Colamd.h:49
t
Definition: plotPSD.py:36
Definition: EigenBase.h:33
Holds information about the various numeric (i.e. scalar) types allowed by Eigen.
Definition: NumTraits.h:217
static void run(ComplexSchur< MatrixType > &_this, const MatrixType &matrix, bool computeU)
Definition: ComplexSchur.h:358
static void run(ComplexSchur< MatrixType > &_this, const MatrixType &matrix, bool computeU)
Definition: ComplexSchur.h:349
Definition: ForwardDeclarations.h:21