11#ifndef EIGEN_REAL_SCHUR_H
12#define EIGEN_REAL_SCHUR_H
14#include "./HessenbergDecomposition.h"
17#include "./InternalHeaderCheck.h"
57template <
typename MatrixType_>
60 typedef MatrixType_ MatrixType;
62 RowsAtCompileTime = MatrixType::RowsAtCompileTime,
63 ColsAtCompileTime = MatrixType::ColsAtCompileTime,
64 Options = internal::traits<MatrixType>::Options,
65 MaxRowsAtCompileTime = MatrixType::MaxRowsAtCompileTime,
66 MaxColsAtCompileTime = MatrixType::MaxColsAtCompileTime
68 typedef typename MatrixType::Scalar Scalar;
69 typedef std::complex<typename NumTraits<Scalar>::Real> ComplexScalar;
89 m_workspaceVector(size),
91 m_isInitialized(false),
92 m_matUisUptodate(false),
105 template <
typename InputType>
107 : m_matT(matrix.rows(), matrix.cols()),
108 m_matU(matrix.rows(), matrix.cols()),
109 m_workspaceVector(matrix.rows()),
110 m_hess(matrix.rows()),
111 m_isInitialized(false),
112 m_matUisUptodate(false),
129 eigen_assert(m_isInitialized &&
"RealSchur is not initialized.");
130 eigen_assert(m_matUisUptodate &&
"The matrix U has not been computed during the RealSchur decomposition.");
145 eigen_assert(m_isInitialized &&
"RealSchur is not initialized.");
168 template <
typename InputType>
188 template <
typename HessMatrixType,
typename OrthMatrixType>
195 eigen_assert(m_isInitialized &&
"RealSchur is not initialized.");
205 m_maxIters = maxIters;
225 bool m_isInitialized;
226 bool m_matUisUptodate;
231 Scalar computeNormOfT();
232 Index findSmallSubdiagEntry(
Index iu,
const Scalar& considerAsZero);
233 void splitOffTwoRows(
Index iu,
bool computeU,
const Scalar& exshift);
240template <
typename MatrixType>
241template <
typename InputType>
243 const Scalar considerAsZero = (std::numeric_limits<Scalar>::min)();
245 eigen_assert(matrix.
cols() == matrix.
rows());
246 Index maxIters = m_maxIters;
247 if (maxIters == -1) maxIters = m_maxIterationsPerRow * matrix.
rows();
249 Scalar scale = matrix.
derived().cwiseAbs().maxCoeff();
250 if (scale < considerAsZero) {
251 m_matT.setZero(matrix.
rows(), matrix.
cols());
252 if (computeU) m_matU.setIdentity(matrix.
rows(), matrix.
cols());
254 m_isInitialized =
true;
255 m_matUisUptodate = computeU;
260 m_hess.compute(matrix.
derived() / scale);
265 m_workspaceVector.resize(matrix.
cols());
266 if (computeU) m_hess.matrixQ().evalTo(m_matU, m_workspaceVector);
267 computeFromHessenberg(m_hess.matrixH(), m_matU, computeU);
273template <
typename MatrixType>
274template <
typename HessMatrixType,
typename OrthMatrixType>
275RealSchur<MatrixType>& RealSchur<MatrixType>::computeFromHessenberg(
const HessMatrixType& matrixH,
276 const OrthMatrixType& matrixQ,
bool computeU) {
280 m_workspaceVector.resize(m_matT.cols());
281 if (computeU && !internal::is_same_dense(m_matU, matrixQ)) m_matU = matrixQ;
283 Index maxIters = m_maxIters;
284 if (maxIters == -1) maxIters = m_maxIterationsPerRow * matrixH.rows();
285 Scalar* workspace = &m_workspaceVector.coeffRef(0);
291 Index iu = m_matT.cols() - 1;
295 Scalar norm = computeNormOfT();
298 Scalar considerAsZero =
299 numext::maxi<Scalar>(norm * numext::abs2(NumTraits<Scalar>::epsilon()), (std::numeric_limits<Scalar>::min)());
301 if (!numext::is_exactly_zero(norm)) {
303 Index il = findSmallSubdiagEntry(iu, considerAsZero);
308 m_matT.coeffRef(iu, iu) = m_matT.coeff(iu, iu) + exshift;
309 if (iu > 0) m_matT.coeffRef(iu, iu - 1) = Scalar(0);
312 }
else if (il == iu - 1)
314 splitOffTwoRows(iu, computeU, exshift);
321 Vector3s firstHouseholderVector = Vector3s::Zero(), shiftInfo;
322 computeShift(iu, iter, exshift, shiftInfo);
324 totalIter = totalIter + 1;
325 if (totalIter > maxIters)
break;
327 initFrancisQRStep(il, iu, shiftInfo, im, firstHouseholderVector);
328 performFrancisQRStep(il, im, iu, computeU, firstHouseholderVector, workspace);
332 if (totalIter <= maxIters)
337 m_isInitialized =
true;
338 m_matUisUptodate = computeU;
343template <
typename MatrixType>
344inline typename MatrixType::Scalar RealSchur<MatrixType>::computeNormOfT() {
345 const Index size = m_matT.cols();
350 for (Index j = 0; j < size; ++j) norm += m_matT.col(j).segment(0, (std::min)(size, j + 2)).cwiseAbs().sum();
355template <
typename MatrixType>
356inline Index RealSchur<MatrixType>::findSmallSubdiagEntry(Index iu,
const Scalar& considerAsZero) {
360 Scalar s = abs(m_matT.coeff(res - 1, res - 1)) + abs(m_matT.coeff(res, res));
362 s = numext::maxi<Scalar>(s * NumTraits<Scalar>::epsilon(), considerAsZero);
364 if (abs(m_matT.coeff(res, res - 1)) <= s)
break;
371template <
typename MatrixType>
372inline void RealSchur<MatrixType>::splitOffTwoRows(Index iu,
bool computeU,
const Scalar& exshift) {
375 const Index size = m_matT.cols();
379 Scalar p = Scalar(0.5) * (m_matT.coeff(iu - 1, iu - 1) - m_matT.coeff(iu, iu));
380 Scalar q = p * p + m_matT.coeff(iu, iu - 1) * m_matT.coeff(iu - 1, iu);
381 m_matT.coeffRef(iu, iu) += exshift;
382 m_matT.coeffRef(iu - 1, iu - 1) += exshift;
386 Scalar z = sqrt(abs(q));
387 JacobiRotation<Scalar> rot;
389 rot.makeGivens(p + z, m_matT.coeff(iu, iu - 1));
391 rot.makeGivens(p - z, m_matT.coeff(iu, iu - 1));
393 m_matT.rightCols(size - iu + 1).applyOnTheLeft(iu - 1, iu, rot.adjoint());
394 m_matT.topRows(iu + 1).applyOnTheRight(iu - 1, iu, rot);
395 m_matT.coeffRef(iu, iu - 1) = Scalar(0);
396 if (computeU) m_matU.applyOnTheRight(iu - 1, iu, rot);
399 if (iu > 1) m_matT.coeffRef(iu - 1, iu - 2) = Scalar(0);
403template <
typename MatrixType>
404inline void RealSchur<MatrixType>::computeShift(Index iu, Index iter, Scalar& exshift, Vector3s& shiftInfo) {
407 shiftInfo.coeffRef(0) = m_matT.coeff(iu, iu);
408 shiftInfo.coeffRef(1) = m_matT.coeff(iu - 1, iu - 1);
409 shiftInfo.coeffRef(2) = m_matT.coeff(iu, iu - 1) * m_matT.coeff(iu - 1, iu);
412 if (iter % 16 == 0) {
414 if (iter % 32 != 0) {
415 exshift += shiftInfo.coeff(0);
416 for (Index i = 0; i <= iu; ++i) m_matT.coeffRef(i, i) -= shiftInfo.coeff(0);
417 Scalar s = abs(m_matT.coeff(iu, iu - 1)) + abs(m_matT.coeff(iu - 1, iu - 2));
418 shiftInfo.coeffRef(0) = Scalar(0.75) * s;
419 shiftInfo.coeffRef(1) = Scalar(0.75) * s;
420 shiftInfo.coeffRef(2) = Scalar(-0.4375) * s * s;
423 Scalar s = (shiftInfo.coeff(1) - shiftInfo.coeff(0)) / Scalar(2.0);
424 s = s * s + shiftInfo.coeff(2);
427 if (shiftInfo.coeff(1) < shiftInfo.coeff(0)) s = -s;
428 s = s + (shiftInfo.coeff(1) - shiftInfo.coeff(0)) / Scalar(2.0);
429 s = shiftInfo.coeff(0) - shiftInfo.coeff(2) / s;
431 for (Index i = 0; i <= iu; ++i) m_matT.coeffRef(i, i) -= s;
432 shiftInfo.setConstant(Scalar(0.964));
439template <
typename MatrixType>
440inline void RealSchur<MatrixType>::initFrancisQRStep(Index il, Index iu,
const Vector3s& shiftInfo, Index& im,
441 Vector3s& firstHouseholderVector) {
443 Vector3s& v = firstHouseholderVector;
445 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)
break;
462template <
typename MatrixType>
463inline void RealSchur<MatrixType>::performFrancisQRStep(Index il, Index im, Index iu,
bool computeU,
464 const Vector3s& firstHouseholderVector, Scalar* workspace) {
465 eigen_assert(im >= il);
466 eigen_assert(im <= iu - 2);
468 const Index size = m_matT.cols();
470 for (Index k = im; k <= iu - 2; ++k) {
471 bool firstIteration = (k == im);
475 v = firstHouseholderVector;
477 v = m_matT.template block<3, 1>(k, k - 1);
480 Matrix<Scalar, 2, 1> ess;
481 v.makeHouseholder(ess, tau, beta);
483 if (!numext::is_exactly_zero(beta))
485 if (firstIteration && k > il)
486 m_matT.coeffRef(k, k - 1) = -m_matT.coeff(k, k - 1);
487 else if (!firstIteration)
488 m_matT.coeffRef(k, k - 1) = beta;
491 m_matT.block(k, k, 3, size - k).applyHouseholderOnTheLeft(ess, tau, workspace);
492 m_matT.block(0, k, (std::min)(iu, k + 3) + 1, 3).applyHouseholderOnTheRight(ess, tau, workspace);
493 if (computeU) m_matU.block(0, k, size, 3).applyHouseholderOnTheRight(ess, tau, workspace);
497 Matrix<Scalar, 2, 1> v = m_matT.template block<2, 1>(iu - 1, iu - 2);
499 Matrix<Scalar, 1, 1> ess;
500 v.makeHouseholder(ess, tau, beta);
502 if (!numext::is_exactly_zero(beta))
504 m_matT.coeffRef(iu - 1, iu - 2) = beta;
505 m_matT.block(iu - 1, iu - 1, 2, size - iu + 1).applyHouseholderOnTheLeft(ess, tau, workspace);
506 m_matT.block(0, iu - 1, iu + 1, 2).applyHouseholderOnTheRight(ess, tau, workspace);
507 if (computeU) m_matU.block(0, iu - 1, size, 2).applyHouseholderOnTheRight(ess, tau, workspace);
511 for (Index i = im + 2; i <= iu; ++i) {
512 m_matT.coeffRef(i, i - 2) = Scalar(0);
513 if (i > im + 2) m_matT.coeffRef(i, i - 3) = Scalar(0);
Reduces a square matrix to Hessenberg form by an orthogonal similarity transformation.
Definition HessenbergDecomposition.h:61
The matrix class, also used for vectors and row-vectors.
Definition Matrix.h:186
Performs a real Schur decomposition of a square matrix.
Definition RealSchur.h:58
const MatrixType & matrixU() const
Returns the orthogonal matrix in the Schur decomposition.
Definition RealSchur.h:128
ComputationInfo info() const
Reports whether previous computation was successful.
Definition RealSchur.h:194
Index getMaxIterations()
Returns the maximum number of iterations.
Definition RealSchur.h:210
RealSchur(Index size=RowsAtCompileTime==Dynamic ? 1 :RowsAtCompileTime)
Default constructor.
Definition RealSchur.h:86
RealSchur(const EigenBase< InputType > &matrix, bool computeU=true)
Constructor; computes real Schur decomposition of given matrix.
Definition RealSchur.h:106
static const int m_maxIterationsPerRow
Maximum number of iterations per row.
Definition RealSchur.h:217
RealSchur & compute(const EigenBase< InputType > &matrix, bool computeU=true)
Computes Schur decomposition of given matrix.
RealSchur & setMaxIterations(Index maxIters)
Sets the maximum number of iterations allowed.
Definition RealSchur.h:204
RealSchur & computeFromHessenberg(const HessMatrixType &matrixH, const OrthMatrixType &matrixQ, bool computeU)
Computes Schur decomposition of a Hessenberg matrix H = Z T Z^T.
Eigen::Index Index
Definition RealSchur.h:70
const MatrixType & matrixT() const
Returns the quasi-triangular matrix in the Schur decomposition.
Definition RealSchur.h:144
ComputationInfo
Definition Constants.h:438
@ Success
Definition Constants.h:440
@ NoConvergence
Definition Constants.h:444
Namespace containing all symbols from the Eigen library.
Definition Core:137
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
Definition EigenBase.h:33
EIGEN_CONSTEXPR Index cols() const EIGEN_NOEXCEPT
Definition EigenBase.h:61
Derived & derived()
Definition EigenBase.h:49
EIGEN_CONSTEXPR Index rows() const EIGEN_NOEXCEPT
Definition EigenBase.h:59