12#ifndef EIGEN_COMPLEX_SCHUR_H
13#define EIGEN_COMPLEX_SCHUR_H
15#include "./HessenbergDecomposition.h"
20template<
typename MatrixType,
bool IsComplex>
struct complex_schur_reduce_to_hessenberg;
54 typedef _MatrixType MatrixType;
56 RowsAtCompileTime = MatrixType::RowsAtCompileTime,
57 ColsAtCompileTime = MatrixType::ColsAtCompileTime,
58 Options = MatrixType::Options,
59 MaxRowsAtCompileTime = MatrixType::MaxRowsAtCompileTime,
60 MaxColsAtCompileTime = MatrixType::MaxColsAtCompileTime
64 typedef typename MatrixType::Scalar
Scalar;
65 typedef typename NumTraits<Scalar>::Real RealScalar;
98 m_isInitialized(false),
99 m_matUisUptodate(false),
112 template<
typename InputType>
114 : m_matT(matrix.rows(),matrix.cols()),
115 m_matU(matrix.rows(),matrix.cols()),
116 m_hess(matrix.rows()),
117 m_isInitialized(false),
118 m_matUisUptodate(false),
140 eigen_assert(m_isInitialized &&
"ComplexSchur is not initialized.");
141 eigen_assert(m_matUisUptodate &&
"The matrix U has not been computed during the ComplexSchur decomposition.");
164 eigen_assert(m_isInitialized &&
"ComplexSchur is not initialized.");
190 template<
typename InputType>
210 template<
typename HessMatrixType,
typename OrthMatrixType>
219 eigen_assert(m_isInitialized &&
"ComplexSchur is not initialized.");
230 m_maxIters = maxIters;
251 bool m_isInitialized;
252 bool m_matUisUptodate;
256 bool subdiagonalEntryIsNeglegible(
Index i);
258 void reduceToTriangularForm(
bool computeU);
259 friend struct internal::complex_schur_reduce_to_hessenberg<MatrixType,
NumTraits<
Scalar>::IsComplex>;
265template<typename MatrixType>
266inline bool
ComplexSchur<MatrixType>::subdiagonalEntryIsNeglegible(Index i)
268 RealScalar d = numext::norm1(m_matT.
coeff(i,i)) + numext::norm1(m_matT.
coeff(i+1,i+1));
269 RealScalar sd = numext::norm1(m_matT.
coeff(i+1,i));
280template<
typename MatrixType>
281typename ComplexSchur<MatrixType>::ComplexScalar ComplexSchur<MatrixType>::computeShift(
Index iu,
Index iter)
284 if (iter == 10 || iter == 20)
287 return abs(numext::real(m_matT.
coeff(iu,iu-1))) + abs(numext::real(m_matT.
coeff(iu-1,iu-2)));
292 Matrix<ComplexScalar,2,2> t = m_matT.template block<2,2>(iu-1,iu-1);
293 RealScalar normt = t.cwiseAbs().sum();
303 RealScalar eival1_norm = numext::norm1(eival1);
304 RealScalar eival2_norm = numext::norm1(eival2);
307 if(eival1_norm > eival2_norm)
308 eival2 = det / eival1;
309 else if(eival2_norm!=RealScalar(0))
310 eival1 = det / eival2;
313 if(numext::norm1(eival1-t.coeff(1,1)) < numext::norm1(eival2-t.coeff(1,1)))
314 return normt * eival1;
316 return normt * eival2;
320template<
typename MatrixType>
321template<
typename InputType>
324 m_matUisUptodate =
false;
325 eigen_assert(matrix.cols() == matrix.rows());
327 if(matrix.cols() == 1)
329 m_matT = matrix.derived().template cast<ComplexScalar>();
330 if(computeU) m_matU = ComplexMatrixType::Identity(1,1);
332 m_isInitialized =
true;
333 m_matUisUptodate = computeU;
337 internal::complex_schur_reduce_to_hessenberg<MatrixType, NumTraits<Scalar>::IsComplex>::run(*
this, matrix.derived(), computeU);
342template<
typename MatrixType>
343template<
typename HessMatrixType,
typename OrthMatrixType>
349 reduceToTriangularForm(computeU);
355template<
typename MatrixType,
bool IsComplex>
356struct complex_schur_reduce_to_hessenberg
359 static void run(ComplexSchur<MatrixType>& _this,
const MatrixType& matrix,
bool computeU)
361 _this.m_hess.compute(matrix);
362 _this.m_matT = _this.m_hess.matrixH();
363 if(computeU) _this.m_matU = _this.m_hess.matrixQ();
367template<
typename MatrixType>
368struct complex_schur_reduce_to_hessenberg<MatrixType, false>
370 static void run(ComplexSchur<MatrixType>& _this,
const MatrixType& matrix,
bool computeU)
372 typedef typename ComplexSchur<MatrixType>::ComplexScalar ComplexScalar;
375 _this.m_hess.compute(matrix);
376 _this.m_matT = _this.m_hess.matrixH().template cast<ComplexScalar>();
380 MatrixType Q = _this.m_hess.matrixQ();
381 _this.m_matU = Q.template cast<ComplexScalar>();
389template<
typename MatrixType>
390void ComplexSchur<MatrixType>::reduceToTriangularForm(
bool computeU)
392 Index maxIters = m_maxIters;
400 Index iu = m_matT.cols() - 1;
410 if(!subdiagonalEntryIsNeglegible(iu-1))
break;
421 if(totalIter > maxIters)
break;
425 while(il > 0 && !subdiagonalEntryIsNeglegible(il-1))
435 JacobiRotation<ComplexScalar> rot;
436 rot.makeGivens(m_matT.
coeff(il,il) - shift, m_matT.
coeff(il+1,il));
437 m_matT.rightCols(m_matT.cols()-il).applyOnTheLeft(il, il+1, rot.adjoint());
438 m_matT.topRows((std::min)(il+2,iu)+1).applyOnTheRight(il, il+1, rot);
439 if(computeU) m_matU.applyOnTheRight(il, il+1, rot);
441 for(
Index i=il+1 ; i<iu ; i++)
445 m_matT.rightCols(m_matT.cols()-i).applyOnTheLeft(i, i+1, rot.adjoint());
446 m_matT.topRows((std::min)(i+2,iu)+1).applyOnTheRight(i, i+1, rot);
447 if(computeU) m_matU.applyOnTheRight(i, i+1, rot);
451 if(totalIter <= maxIters)
456 m_isInitialized =
true;
457 m_matUisUptodate = computeU;
Performs a complex Schur decomposition of a real or complex square matrix.
Definition: ComplexSchur.h:52
const ComplexMatrixType & matrixT() const
Returns the triangular matrix in the Schur decomposition.
Definition: ComplexSchur.h:162
Index getMaxIterations()
Returns the maximum number of iterations.
Definition: ComplexSchur.h:235
ComplexSchur & computeFromHessenberg(const HessMatrixType &matrixH, const OrthMatrixType &matrixQ, bool computeU=true)
Compute Schur decomposition from a given Hessenberg matrix.
Eigen::Index Index
Definition: ComplexSchur.h:66
const ComplexMatrixType & matrixU() const
Returns the unitary matrix in the Schur decomposition.
Definition: ComplexSchur.h:138
ComputationInfo info() const
Reports whether previous computation was successful.
Definition: ComplexSchur.h:217
MatrixType::Scalar Scalar
Scalar type for matrices of type _MatrixType.
Definition: ComplexSchur.h:64
ComplexSchur(const EigenBase< InputType > &matrix, bool computeU=true)
Constructor; computes Schur decomposition of given matrix.
Definition: ComplexSchur.h:113
ComplexSchur & setMaxIterations(Index maxIters)
Sets the maximum number of iterations allowed.
Definition: ComplexSchur.h:228
static const int m_maxIterationsPerRow
Maximum number of iterations per row.
Definition: ComplexSchur.h:245
ComplexSchur(Index size=RowsAtCompileTime==Dynamic ? 1 :RowsAtCompileTime)
Default constructor.
Definition: ComplexSchur.h:94
std::complex< RealScalar > ComplexScalar
Complex scalar type for _MatrixType.
Definition: ComplexSchur.h:74
ComplexSchur & compute(const EigenBase< InputType > &matrix, bool computeU=true)
Computes Schur decomposition of given matrix.
Matrix< ComplexScalar, RowsAtCompileTime, ColsAtCompileTime, Options, MaxRowsAtCompileTime, MaxColsAtCompileTime > ComplexMatrixType
Type for the matrices in the Schur decomposition.
Definition: ComplexSchur.h:81
Reduces a square matrix to Hessenberg form by an orthogonal similarity transformation.
Definition: HessenbergDecomposition.h:58
The matrix class, also used for vectors and row-vectors.
Definition: Matrix.h:180
Scalar & coeffRef(Index rowId, Index colId)
Definition: PlainObjectBase.h:175
const Scalar & coeff(Index rowId, Index colId) const
Definition: PlainObjectBase.h:152
ComputationInfo
Definition: Constants.h:440
@ Success
Definition: Constants.h:442
@ NoConvergence
Definition: Constants.h:446
Namespace containing all symbols from the Eigen library.
Definition: Core:141
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:74
const int Dynamic
Definition: Constants.h:22
Definition: EigenBase.h:30
Derived & derived()
Definition: EigenBase.h:46
Holds information about the various numeric (i.e. scalar) types allowed by Eigen.
Definition: NumTraits.h:233