12 #ifndef EIGEN_COMPLEX_SCHUR_H
13 #define EIGEN_COMPLEX_SCHUR_H
15 #include "./HessenbergDecomposition.h"
17 #include "./InternalHeaderCheck.h"
22 template<
typename MatrixType,
bool IsComplex>
struct complex_schur_reduce_to_hessenberg;
56 typedef MatrixType_ MatrixType;
58 RowsAtCompileTime = MatrixType::RowsAtCompileTime,
59 ColsAtCompileTime = MatrixType::ColsAtCompileTime,
60 Options = MatrixType::Options,
61 MaxRowsAtCompileTime = MatrixType::MaxRowsAtCompileTime,
62 MaxColsAtCompileTime = MatrixType::MaxColsAtCompileTime
66 typedef typename MatrixType::Scalar
Scalar;
100 m_isInitialized(false),
101 m_matUisUptodate(false),
114 template<
typename InputType>
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),
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.");
166 eigen_assert(m_isInitialized &&
"ComplexSchur is not initialized.");
192 template<
typename InputType>
212 template<
typename HessMatrixType,
typename OrthMatrixType>
221 eigen_assert(m_isInitialized &&
"ComplexSchur is not initialized.");
232 m_maxIters = maxIters;
253 bool m_isInitialized;
254 bool m_matUisUptodate;
258 bool subdiagonalEntryIsNeglegible(
Index i);
260 void reduceToTriangularForm(
bool computeU);
261 friend struct internal::complex_schur_reduce_to_hessenberg<MatrixType,
NumTraits<
Scalar>::IsComplex>;
267 template<typename MatrixType>
268 inline bool
ComplexSchur<MatrixType>::subdiagonalEntryIsNeglegible(Index i)
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));
282 template<
typename MatrixType>
286 if (iter == 10 || iter == 20)
289 return abs(numext::real(m_matT.
coeff(iu,iu-1))) +
abs(numext::real(m_matT.
coeff(iu-1,iu-2)));
294 Matrix<ComplexScalar,2,2> t = m_matT.template block<2,2>(iu-1,iu-1);
295 RealScalar normt = t.cwiseAbs().sum();
305 RealScalar eival1_norm = numext::norm1(eival1);
306 RealScalar eival2_norm = numext::norm1(eival2);
309 if(eival1_norm > eival2_norm)
310 eival2 = det / eival1;
311 else if(!numext::is_exactly_zero(eival2_norm))
312 eival1 = det / eival2;
315 if(numext::norm1(eival1-t.coeff(1,1)) < numext::norm1(eival2-t.coeff(1,1)))
316 return normt * eival1;
318 return normt * eival2;
322 template<
typename MatrixType>
323 template<
typename InputType>
326 m_matUisUptodate =
false;
327 eigen_assert(matrix.cols() == matrix.rows());
329 if(matrix.cols() == 1)
331 m_matT = matrix.derived().template cast<ComplexScalar>();
332 if(computeU) m_matU = ComplexMatrixType::Identity(1,1);
334 m_isInitialized =
true;
335 m_matUisUptodate = computeU;
339 internal::complex_schur_reduce_to_hessenberg<MatrixType, NumTraits<Scalar>::IsComplex>::run(*
this, matrix.derived(), computeU);
344 template<
typename MatrixType>
345 template<
typename HessMatrixType,
typename OrthMatrixType>
351 reduceToTriangularForm(computeU);
357 template<
typename MatrixType,
bool IsComplex>
358 struct complex_schur_reduce_to_hessenberg
361 static void run(ComplexSchur<MatrixType>& _this,
const MatrixType& matrix,
bool computeU)
363 _this.m_hess.compute(matrix);
364 _this.m_matT = _this.m_hess.matrixH();
365 if(computeU) _this.m_matU = _this.m_hess.matrixQ();
369 template<
typename MatrixType>
370 struct complex_schur_reduce_to_hessenberg<MatrixType, false>
372 static void run(ComplexSchur<MatrixType>& _this,
const MatrixType& matrix,
bool computeU)
377 _this.m_hess.compute(matrix);
378 _this.m_matT = _this.m_hess.matrixH().template cast<ComplexScalar>();
382 MatrixType Q = _this.m_hess.matrixQ();
383 _this.m_matU = Q.template cast<ComplexScalar>();
391 template<
typename MatrixType>
392 void ComplexSchur<MatrixType>::reduceToTriangularForm(
bool computeU)
394 Index maxIters = m_maxIters;
402 Index iu = m_matT.cols() - 1;
412 if(!subdiagonalEntryIsNeglegible(iu-1))
break;
423 if(totalIter > maxIters)
break;
427 while(il > 0 && !subdiagonalEntryIsNeglegible(il-1))
437 JacobiRotation<ComplexScalar> rot;
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);
443 for(
Index i=il+1 ; i<iu ; i++)
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);
453 if(totalIter <= maxIters)
458 m_isInitialized =
true;
459 m_matUisUptodate = computeU;
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
ComplexSchur(Index size=RowsAtCompileTime==Dynamic ? 1 :RowsAtCompileTime)
Default constructor.
Definition: ComplexSchur.h:96
Index getMaxIterations()
Returns the maximum number of iterations.
Definition: ComplexSchur.h:237
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.
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
Matrix< ComplexScalar, RowsAtCompileTime, ColsAtCompileTime, Options, MaxRowsAtCompileTime, MaxColsAtCompileTime > ComplexMatrixType
Type for the matrices in the Schur decomposition.
Definition: ComplexSchur.h:83
ComplexSchur & compute(const EigenBase< InputType > &matrix, bool computeU=true)
Computes Schur decomposition of given matrix.
const Scalar & coeff(Index rowId, Index colId) const
Definition: PlainObjectBase.h:164
Scalar & coeffRef(Index rowId, Index colId)
Definition: PlainObjectBase.h:187
ComputationInfo
Definition: Constants.h:442
@ Success
Definition: Constants.h:444
@ NoConvergence
Definition: Constants.h:448
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
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)
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