13#ifndef EIGEN_COMPLEX_SCHUR_H
14#define EIGEN_COMPLEX_SCHUR_H
16#include "./HessenbergDecomposition.h"
19#include "./InternalHeaderCheck.h"
24template <
typename MatrixType,
bool IsComplex>
25struct complex_schur_reduce_to_hessenberg;
28template <
typename MatrixType_>
59template <
typename MatrixType_>
62 using MatrixType = MatrixType_;
64 RowsAtCompileTime = MatrixType::RowsAtCompileTime,
65 ColsAtCompileTime = MatrixType::ColsAtCompileTime,
66 Options = internal::plain_object_options<MatrixType>::value,
67 MaxRowsAtCompileTime = MatrixType::MaxRowsAtCompileTime,
68 MaxColsAtCompileTime = MatrixType::MaxColsAtCompileTime
72 using Scalar =
typename MatrixType::Scalar;
73 using RealScalar =
typename NumTraits<Scalar>::Real;
110 : m_matT(size, size),
113 m_isInitialized(false),
114 m_matUisUptodate(false),
126 template <
typename InputType>
128 : m_matT(matrix.rows(), matrix.cols()),
129 m_matU(matrix.rows(), matrix.cols()),
130 m_hess(matrix.rows()),
131 m_isInitialized(false),
132 m_matUisUptodate(false),
146 template <typename InputType, bool IsRef = internal::is_ref<MatrixType>::value, std::enable_if_t<IsRef, int> = 0>
148 : m_matT(matrix.derived()),
149 m_matU(matrix.rows(), matrix.cols()),
150 m_hess(matrix.rows()),
151 m_isInitialized(false),
152 m_matUisUptodate(false),
154 computeInPlace(computeU);
172 eigen_assert(m_isInitialized &&
"ComplexSchur is not initialized.");
173 eigen_assert(m_matUisUptodate &&
"The matrix U has not been computed during the ComplexSchur decomposition.");
195 eigen_assert(m_isInitialized &&
"ComplexSchur is not initialized.");
220 template <
typename InputType>
240 template <
typename HessMatrixType,
typename OrthMatrixType>
242 bool computeU =
true);
249 eigen_assert(m_isInitialized &&
"ComplexSchur is not initialized.");
259 m_maxIters = maxIters;
274 EIGEN_STATIC_ASSERT(NumTraits<Scalar>::IsComplex || !internal::is_ref<MatrixType>::value,
275 INPLACE_COMPLEXSCHUR_REQUIRES_A_COMPLEX_MATRIX_TYPE)
279 internal::complex_schur_reduce_to_hessenberg<MatrixType, NumTraits<Scalar>::IsComplex> m_hess;
281 bool m_isInitialized;
282 bool m_matUisUptodate;
290 bool subdiagonalEntryIsNeglegible(
Index i);
292 void reduceToTriangularForm(
bool computeU);
293 friend struct internal::complex_schur_reduce_to_hessenberg<MatrixType,
NumTraits<
Scalar>::IsComplex>;
299template <typename MatrixType>
300inline bool
ComplexSchur<MatrixType>::subdiagonalEntryIsNeglegible(Index i) {
301 RealScalar d = numext::norm1(m_matT.coeff(i, i)) + numext::norm1(m_matT.coeff(i + 1, i + 1));
302 RealScalar sd = numext::norm1(m_matT.coeff(i + 1, i));
303 if (internal::isMuchSmallerThan(sd, d, NumTraits<RealScalar>::epsilon())) {
311template <
typename MatrixType>
313 if ((iter == 10 || iter == 20) && iu > 1) {
316 numext::abs(numext::real(m_matT.coeff(iu - 1, iu - 2))));
321 Matrix<ComplexScalar, 2, 2> t = m_matT.template block<2, 2>(iu - 1, iu - 1);
322 const RealScalar normt = t.cwiseAbs().sum();
323 const auto factors = internal::safe_scaling<RealScalar>::scale_to(t, t, normt);
327 ComplexScalar disc = numext::sqrt(c * c + RealScalar(4) * b);
332 RealScalar eival1_norm = numext::norm1(eival1);
333 RealScalar eival2_norm = numext::norm1(eival2);
336 if (eival1_norm > eival2_norm)
337 eival2 = det / eival1;
338 else if (!numext::is_exactly_zero(eival2_norm))
339 eival1 = det / eival2;
342 ComplexScalar shift = numext::norm1(eival1 - t.coeff(1, 1)) < numext::norm1(eival2 - t.coeff(1, 1)) ? eival1 : eival2;
343 internal::safe_scaling<RealScalar>::unscale_in_place(shift, factors);
347template <
typename MatrixType>
348template <
typename InputType>
350 m_matUisUptodate =
false;
351 EIGEN_INTERNAL_ASSERT_SQUARE(matrix);
353 if (matrix.cols() <= 1) {
354 m_matT = matrix.derived().template cast<ComplexScalar>();
355 if (computeU) m_matU = ComplexMatrixType::Identity(matrix.rows(), matrix.cols());
357 m_isInitialized =
true;
358 m_matUisUptodate = computeU;
370 const RealScalar maxCoeff = internal::safe_scaling<RealScalar>::max_abs_component(matrix.derived());
371 const internal::safe_scaling_factors<RealScalar> factors = internal::safe_scaling<RealScalar>::with_scaled(
372 matrix.derived(), maxCoeff, [&](
const auto& scaled) { m_hess.run(*this, scaled, computeU); });
375 internal::safe_scaling<RealScalar>::unscale_in_place(m_matT, maxCoeff, factors);
380template <
typename MatrixType>
382 m_matUisUptodate =
false;
383 EIGEN_INTERNAL_ASSERT_SQUARE(m_matT);
385 if (m_matT.cols() <= 1) {
386 if (computeU) m_matU = ComplexMatrixType::Identity(m_matT.rows(), m_matT.cols());
388 m_isInitialized =
true;
389 m_matUisUptodate = computeU;
393 const RealScalar maxCoeff = internal::safe_scaling<RealScalar>::max_abs_component(m_matT);
394 const internal::safe_scaling_factors<RealScalar> factors =
395 internal::safe_scaling<RealScalar>::scale_in_place(m_matT, maxCoeff);
396 m_hess.runInPlace(*
this, computeU);
398 internal::safe_scaling<RealScalar>::unscale_in_place(m_matT, maxCoeff, factors);
402template <
typename MatrixType>
403template <
typename HessMatrixType,
typename OrthMatrixType>
405 const OrthMatrixType& matrixQ,
407 if (!internal::is_same_dense(m_matT, matrixH)) m_matT = matrixH;
408 if (computeU && !internal::is_same_dense(m_matU, matrixQ)) m_matU = matrixQ;
409 reduceToTriangularForm(computeU);
415template <
typename MatrixType,
bool IsComplex>
416struct complex_schur_reduce_to_hessenberg {
418 using Schur = ComplexSchur<MatrixType>;
419 using Scalar =
typename Schur::ComplexScalar;
420 using CoeffVectorType = Matrix<Scalar, Schur::RowsAtCompileTime == Dynamic ? Dynamic : Schur::RowsAtCompileTime - 1,
421 1, Schur::Options & ~RowMajor,
422 Schur::MaxRowsAtCompileTime == Dynamic ? Dynamic : Schur::MaxRowsAtCompileTime - 1, 1>;
423 using WorkspaceType =
424 Matrix<Scalar, Schur::ColsAtCompileTime, 1, Schur::Options & ~RowMajor, Schur::MaxColsAtCompileTime, 1>;
426 explicit complex_schur_reduce_to_hessenberg(Index size) : m_workspace(size) {
427 if (size > 1) m_hCoeffs.resize(size - 1);
430 template <
typename InputType>
431 void run(Schur& _this,
const InputType& matrix,
bool computeU) {
432 _this.m_matT = matrix;
433 runInPlace(_this, computeU);
436 void runInPlace(Schur& _this,
bool computeU) {
437 hessenberg_decomposition_inplace(_this.m_matT, m_hCoeffs, m_workspace, _this.m_matU, computeU);
440 CoeffVectorType m_hCoeffs;
441 WorkspaceType m_workspace;
444template <
typename MatrixType>
445struct complex_schur_reduce_to_hessenberg<MatrixType, false> {
446 using Schur = ComplexSchur<MatrixType>;
448 explicit complex_schur_reduce_to_hessenberg(Index size) : m_hess(size) {}
450 template <
typename InputType>
451 void run(Schur& _this,
const InputType& matrix,
bool computeU) {
452 using ComplexScalar =
typename Schur::ComplexScalar;
455 m_hess.compute(matrix);
456 _this.m_matT = m_hess.matrixH().template cast<ComplexScalar>();
459 MatrixType Q = m_hess.matrixQ();
460 _this.m_matU = Q.template cast<ComplexScalar>();
464 HessenbergDecomposition<MatrixType> m_hess;
470template <
typename MatrixType>
472 Index maxIters = m_maxIters;
479 Index iu = m_matT.cols() - 1;
487 if (!subdiagonalEntryIsNeglegible(iu - 1))
break;
498 if (totalIter > maxIters)
break;
502 while (il > 0 && !subdiagonalEntryIsNeglegible(il - 1)) {
511 JacobiRotation<ComplexScalar> rot;
512 rot.makeGivens(m_matT.coeff(il, il) - shift, m_matT.coeff(il + 1, il));
513 m_matT.rightCols(m_matT.cols() - il).applyOnTheLeft(il, il + 1, rot.adjoint());
514 m_matT.topRows((std::min)(il + 2, iu) + 1).applyOnTheRight(il, il + 1, rot);
515 if (computeU) m_matU.applyOnTheRight(il, il + 1, rot);
517 for (
Index i = il + 1; i < iu; i++) {
518 rot.makeGivens(m_matT.coeffRef(i, i - 1), m_matT.coeffRef(i + 1, i - 1), &m_matT.coeffRef(i, i - 1));
520 m_matT.rightCols(m_matT.cols() - i).applyOnTheLeft(i, i + 1, rot.adjoint());
521 m_matT.topRows((std::min)(i + 2, iu) + 1).applyOnTheRight(i, i + 1, rot);
522 if (computeU) m_matU.applyOnTheRight(i, i + 1, rot);
526 if (totalIter <= maxIters)
531 m_isInitialized =
true;
532 m_matUisUptodate = computeU;
Computes eigenvalues and eigenvectors of general complex matrices.
Definition ComplexEigenSolver.h:50
ComputationInfo info() const
Reports whether previous computation was successful.
Definition ComplexSchur.h:248
Eigen::Index Index
Definition ComplexSchur.h:74
std::conditional_t< internal::is_ref< MatrixType >::value, MatrixType, ComplexMatrixType > MatrixTType
Type of the matrix returned by matrixT().
Definition ComplexSchur.h:96
ComplexSchur(Index size=RowsAtCompileTime==Dynamic ? 1 :RowsAtCompileTime)
Default constructor.
Definition ComplexSchur.h:109
const MatrixTType & matrixT() const
Returns the triangular matrix in the Schur decomposition.
Definition ComplexSchur.h:194
Matrix< ComplexScalar, RowsAtCompileTime, ColsAtCompileTime, Options, MaxRowsAtCompileTime, MaxColsAtCompileTime > ComplexMatrixType
Type for the matrices in the Schur decomposition.
Definition ComplexSchur.h:89
typename MatrixType::Scalar Scalar
Scalar type for matrices of type MatrixType_.
Definition ComplexSchur.h:72
ComplexSchur & computeFromHessenberg(const HessMatrixType &matrixH, const OrthMatrixType &matrixQ, bool computeU=true)
Compute Schur decomposition from a given Hessenberg matrix.
Index getMaxIterations() const
Returns the maximum number of iterations.
Definition ComplexSchur.h:264
static const int m_maxIterationsPerRow
Definition ComplexSchur.h:271
internal::make_complex_t< Scalar > ComplexScalar
Complex scalar type for MatrixType_.
Definition ComplexSchur.h:82
ComplexSchur(const EigenBase< InputType > &matrix, bool computeU=true)
Constructor; computes Schur decomposition of given matrix.
Definition ComplexSchur.h:127
ComplexSchur(EigenBase< InputType > &matrix, bool computeU=true)
Constructor for inplace decomposition .
Definition ComplexSchur.h:147
ComplexSchur & compute(const EigenBase< InputType > &matrix, bool computeU=true)
Computes Schur decomposition of given matrix.
ComplexSchur & setMaxIterations(Index maxIters)
Sets the maximum number of iterations allowed.
Definition ComplexSchur.h:258
const ComplexMatrixType & matrixU() const
Returns the unitary matrix in the Schur decomposition.
Definition ComplexSchur.h:171
The matrix class, also used for vectors and row-vectors.
Definition Matrix.h:188
ComputationInfo
Definition Constants.h:455
@ Success
Definition Constants.h:457
@ NoConvergence
Definition Constants.h:461
Definition EigenBase.h:34
constexpr Derived & derived()
Definition EigenBase.h:50
Holds information about the various numeric (i.e. scalar) types allowed by Eigen.
Definition NumTraits.h:267