11 #ifndef EIGEN_REAL_SCHUR_H
12 #define EIGEN_REAL_SCHUR_H
14 #include "./HessenbergDecomposition.h"
57 typedef _MatrixType MatrixType;
59 RowsAtCompileTime = MatrixType::RowsAtCompileTime,
60 ColsAtCompileTime = MatrixType::ColsAtCompileTime,
61 Options = MatrixType::Options,
62 MaxRowsAtCompileTime = MatrixType::MaxRowsAtCompileTime,
63 MaxColsAtCompileTime = MatrixType::MaxColsAtCompileTime
65 typedef typename MatrixType::Scalar Scalar;
66 typedef std::complex<typename NumTraits<Scalar>::Real> ComplexScalar;
67 typedef typename MatrixType::Index Index;
86 m_workspaceVector(size),
88 m_isInitialized(false),
89 m_matUisUptodate(false),
103 RealSchur(
const MatrixType& matrix,
bool computeU =
true)
104 : m_matT(matrix.rows(),matrix.cols()),
105 m_matU(matrix.rows(),matrix.cols()),
106 m_workspaceVector(matrix.rows()),
107 m_hess(matrix.rows()),
108 m_isInitialized(false),
109 m_matUisUptodate(false),
128 eigen_assert(m_isInitialized &&
"RealSchur is not initialized.");
129 eigen_assert(m_matUisUptodate &&
"The matrix U has not been computed during the RealSchur decomposition.");
145 eigen_assert(m_isInitialized &&
"RealSchur is not initialized.");
187 template<
typename HessMatrixType,
typename OrthMatrixType>
195 eigen_assert(m_isInitialized &&
"RealSchur is not initialized.");
206 m_maxIters = maxIters;
230 bool m_isInitialized;
231 bool m_matUisUptodate;
236 Scalar computeNormOfT();
237 Index findSmallSubdiagEntry(Index iu);
238 void splitOffTwoRows(Index iu,
bool computeU,
const Scalar& exshift);
239 void computeShift(Index iu, Index iter, Scalar& exshift,
Vector3s& shiftInfo);
240 void initFrancisQRStep(Index il, Index iu,
const Vector3s& shiftInfo, Index& im,
Vector3s& firstHouseholderVector);
241 void performFrancisQRStep(Index il, Index im, Index iu,
bool computeU,
const Vector3s& firstHouseholderVector, Scalar* workspace);
245 template<
typename MatrixType>
248 eigen_assert(matrix.cols() == matrix.rows());
249 Index maxIters = m_maxIters;
251 maxIters = m_maxIterationsPerRow * matrix.rows();
254 m_hess.compute(matrix);
257 computeFromHessenberg(m_hess.matrixH(), m_hess.matrixQ(), computeU);
261 template<
typename MatrixType>
262 template<
typename HessMatrixType,
typename OrthMatrixType>
269 Index maxIters = m_maxIters;
271 maxIters = m_maxIterationsPerRow * matrixH.rows();
272 m_workspaceVector.resize(m_matT.cols());
273 Scalar* workspace = &m_workspaceVector.coeffRef(0);
279 Index iu = m_matT.cols() - 1;
283 Scalar norm = computeNormOfT();
289 Index il = findSmallSubdiagEntry(iu);
294 m_matT.coeffRef(iu,iu) = m_matT.coeff(iu,iu) + exshift;
296 m_matT.coeffRef(iu, iu-1) = Scalar(0);
302 splitOffTwoRows(iu, computeU, exshift);
309 Vector3s firstHouseholderVector(0,0,0), shiftInfo;
310 computeShift(iu, iter, exshift, shiftInfo);
312 totalIter = totalIter + 1;
313 if (totalIter > maxIters)
break;
315 initFrancisQRStep(il, iu, shiftInfo, im, firstHouseholderVector);
316 performFrancisQRStep(il, im, iu, computeU, firstHouseholderVector, workspace);
320 if(totalIter <= maxIters)
325 m_isInitialized =
true;
326 m_matUisUptodate = computeU;
331 template<
typename MatrixType>
332 inline typename MatrixType::Scalar RealSchur<MatrixType>::computeNormOfT()
334 const Index size = m_matT.cols();
339 for (Index j = 0; j < size; ++j)
340 norm += m_matT.col(j).segment(0, (std::min)(size,j+2)).cwiseAbs().sum();
345 template<
typename MatrixType>
346 inline typename MatrixType::Index RealSchur<MatrixType>::findSmallSubdiagEntry(Index iu)
352 Scalar s = abs(m_matT.coeff(res-1,res-1)) + abs(m_matT.coeff(res,res));
353 if (abs(m_matT.coeff(res,res-1)) <= NumTraits<Scalar>::epsilon() * s)
361 template<
typename MatrixType>
362 inline void RealSchur<MatrixType>::splitOffTwoRows(Index iu,
bool computeU,
const Scalar& exshift)
366 const Index size = m_matT.cols();
370 Scalar p = Scalar(0.5) * (m_matT.coeff(iu-1,iu-1) - m_matT.coeff(iu,iu));
371 Scalar q = p * p + m_matT.coeff(iu,iu-1) * m_matT.coeff(iu-1,iu);
372 m_matT.coeffRef(iu,iu) += exshift;
373 m_matT.coeffRef(iu-1,iu-1) += exshift;
377 Scalar z = sqrt(abs(q));
378 JacobiRotation<Scalar> rot;
380 rot.makeGivens(p + z, m_matT.coeff(iu, iu-1));
382 rot.makeGivens(p - z, m_matT.coeff(iu, iu-1));
384 m_matT.rightCols(size-iu+1).applyOnTheLeft(iu-1, iu, rot.adjoint());
385 m_matT.topRows(iu+1).applyOnTheRight(iu-1, iu, rot);
386 m_matT.coeffRef(iu, iu-1) = Scalar(0);
388 m_matU.applyOnTheRight(iu-1, iu, rot);
392 m_matT.coeffRef(iu-1, iu-2) = Scalar(0);
396 template<
typename MatrixType>
397 inline void RealSchur<MatrixType>::computeShift(Index iu, Index iter, Scalar& exshift, Vector3s& shiftInfo)
401 shiftInfo.coeffRef(0) = m_matT.coeff(iu,iu);
402 shiftInfo.coeffRef(1) = m_matT.coeff(iu-1,iu-1);
403 shiftInfo.coeffRef(2) = m_matT.coeff(iu,iu-1) * m_matT.coeff(iu-1,iu);
408 exshift += shiftInfo.coeff(0);
409 for (Index i = 0; i <= iu; ++i)
410 m_matT.coeffRef(i,i) -= shiftInfo.coeff(0);
411 Scalar s = abs(m_matT.coeff(iu,iu-1)) + abs(m_matT.coeff(iu-1,iu-2));
412 shiftInfo.coeffRef(0) = Scalar(0.75) * s;
413 shiftInfo.coeffRef(1) = Scalar(0.75) * s;
414 shiftInfo.coeffRef(2) = Scalar(-0.4375) * s * s;
420 Scalar s = (shiftInfo.coeff(1) - shiftInfo.coeff(0)) / Scalar(2.0);
421 s = s * s + shiftInfo.coeff(2);
425 if (shiftInfo.coeff(1) < shiftInfo.coeff(0))
427 s = s + (shiftInfo.coeff(1) - shiftInfo.coeff(0)) / Scalar(2.0);
428 s = shiftInfo.coeff(0) - shiftInfo.coeff(2) / s;
430 for (Index i = 0; i <= iu; ++i)
431 m_matT.coeffRef(i,i) -= s;
432 shiftInfo.setConstant(Scalar(0.964));
438 template<
typename MatrixType>
439 inline void RealSchur<MatrixType>::initFrancisQRStep(Index il, Index iu,
const Vector3s& shiftInfo, Index& im, Vector3s& firstHouseholderVector)
442 Vector3s& v = firstHouseholderVector;
444 for (im = iu-2; im >= il; --im)
446 const Scalar Tmm = m_matT.coeff(im,im);
447 const Scalar r = shiftInfo.coeff(0) - Tmm;
448 const Scalar s = shiftInfo.coeff(1) - Tmm;
449 v.coeffRef(0) = (r * s - shiftInfo.coeff(2)) / m_matT.coeff(im+1,im) + m_matT.coeff(im,im+1);
450 v.coeffRef(1) = m_matT.coeff(im+1,im+1) - Tmm - r - s;
451 v.coeffRef(2) = m_matT.coeff(im+2,im+1);
455 const Scalar lhs = m_matT.coeff(im,im-1) * (abs(v.coeff(1)) + abs(v.coeff(2)));
456 const Scalar rhs = v.coeff(0) * (abs(m_matT.coeff(im-1,im-1)) + abs(Tmm) + abs(m_matT.coeff(im+1,im+1)));
457 if (abs(lhs) < NumTraits<Scalar>::epsilon() * rhs)
463 template<
typename MatrixType>
464 inline void RealSchur<MatrixType>::performFrancisQRStep(Index il, Index im, Index iu,
bool computeU,
const Vector3s& firstHouseholderVector, Scalar* workspace)
466 eigen_assert(im >= il);
467 eigen_assert(im <= iu-2);
469 const Index size = m_matT.cols();
471 for (Index k = im; k <= iu-2; ++k)
473 bool firstIteration = (k == im);
477 v = firstHouseholderVector;
479 v = m_matT.template block<3,1>(k,k-1);
482 Matrix<Scalar, 2, 1> ess;
483 v.makeHouseholder(ess, tau, beta);
485 if (beta != Scalar(0))
487 if (firstIteration && k > il)
488 m_matT.coeffRef(k,k-1) = -m_matT.coeff(k,k-1);
489 else if (!firstIteration)
490 m_matT.coeffRef(k,k-1) = beta;
493 m_matT.block(k, k, 3, size-k).applyHouseholderOnTheLeft(ess, tau, workspace);
494 m_matT.block(0, k, (std::min)(iu,k+3) + 1, 3).applyHouseholderOnTheRight(ess, tau, workspace);
496 m_matU.block(0, k, size, 3).applyHouseholderOnTheRight(ess, tau, workspace);
500 Matrix<Scalar, 2, 1> v = m_matT.template block<2,1>(iu-1, iu-2);
502 Matrix<Scalar, 1, 1> ess;
503 v.makeHouseholder(ess, tau, beta);
505 if (beta != Scalar(0))
507 m_matT.coeffRef(iu-1, iu-2) = beta;
508 m_matT.block(iu-1, iu-1, 2, size-iu+1).applyHouseholderOnTheLeft(ess, tau, workspace);
509 m_matT.block(0, iu-1, iu+1, 2).applyHouseholderOnTheRight(ess, tau, workspace);
511 m_matU.block(0, iu-1, size, 2).applyHouseholderOnTheRight(ess, tau, workspace);
515 for (Index i = im+2; i <= iu; ++i)
517 m_matT.coeffRef(i,i-2) = Scalar(0);
519 m_matT.coeffRef(i,i-3) = Scalar(0);
525 #endif // EIGEN_REAL_SCHUR_H
RealSchur & compute(const MatrixType &matrix, bool computeU=true)
Computes Schur decomposition of given matrix.
Definition: RealSchur.h:246
ComputationInfo info() const
Reports whether previous computation was successful.
Definition: RealSchur.h:193
Performs a real Schur decomposition of a square matrix.
Definition: RealSchur.h:54
RealSchur(const MatrixType &matrix, bool computeU=true)
Constructor; computes real Schur decomposition of given matrix.
Definition: RealSchur.h:103
static const int m_maxIterationsPerRow
Maximum number of iterations per row.
Definition: RealSchur.h:221
const int Dynamic
Definition: Constants.h:21
Index getMaxIterations()
Returns the maximum number of iterations.
Definition: RealSchur.h:211
RealSchur & setMaxIterations(Index maxIters)
Sets the maximum number of iterations allowed.
Definition: RealSchur.h:204
Definition: Constants.h:380
RealSchur & computeFromHessenberg(const HessMatrixType &matrixH, const OrthMatrixType &matrixQ, bool computeU)
Computes Schur decomposition of a Hessenberg matrix H = Z T Z^T.
Definition: Constants.h:376
RealSchur(Index size=RowsAtCompileTime==Dynamic?1:RowsAtCompileTime)
Default constructor.
Definition: RealSchur.h:83
const MatrixType & matrixT() const
Returns the quasi-triangular matrix in the Schur decomposition.
Definition: RealSchur.h:143
ComputationInfo
Definition: Constants.h:374
const MatrixType & matrixU() const
Returns the orthogonal matrix in the Schur decomposition.
Definition: RealSchur.h:126