26 #ifndef EIGEN_REAL_SCHUR_H
27 #define EIGEN_REAL_SCHUR_H
80 typedef typename MatrixType::Scalar
Scalar;
82 typedef typename MatrixType::Index
Index;
101 m_workspaceVector(size),
103 m_isInitialized(false),
104 m_matUisUptodate(false)
118 : m_matT(matrix.rows(),matrix.cols()),
119 m_matU(matrix.rows(),matrix.cols()),
120 m_workspaceVector(matrix.rows()),
121 m_hess(matrix.rows()),
122 m_isInitialized(false),
123 m_matUisUptodate(false)
141 eigen_assert(m_isInitialized &&
"RealSchur is not initialized.");
142 eigen_assert(m_matUisUptodate &&
"The matrix U has not been computed during the RealSchur decomposition.");
158 eigen_assert(m_isInitialized &&
"RealSchur is not initialized.");
187 eigen_assert(m_isInitialized &&
"RealSchur is not initialized.");
204 bool m_isInitialized;
205 bool m_matUisUptodate;
211 void splitOffTwoRows(
Index iu,
bool computeU,
Scalar exshift);
218 template<
typename MatrixType>
221 assert(matrix.cols() == matrix.rows());
225 m_matT = m_hess.matrixH();
227 m_matU = m_hess.matrixQ();
230 m_workspaceVector.resize(m_matT.cols());
231 Scalar* workspace = &m_workspaceVector.coeffRef(0);
237 Index iu = m_matT.cols() - 1;
240 Scalar norm = computeNormOfT();
244 Index il = findSmallSubdiagEntry(iu, norm);
249 m_matT.coeffRef(iu,iu) = m_matT.coeff(iu,iu) + exshift;
251 m_matT.coeffRef(iu, iu-1) =
Scalar(0);
257 splitOffTwoRows(iu, computeU, exshift);
264 Vector3s firstHouseholderVector(0,0,0), shiftInfo;
265 computeShift(iu, iter, exshift, shiftInfo);
267 if (iter > m_maxIterations)
break;
269 initFrancisQRStep(il, iu, shiftInfo, im, firstHouseholderVector);
270 performFrancisQRStep(il, im, iu, computeU, firstHouseholderVector, workspace);
274 if(iter <= m_maxIterations)
279 m_isInitialized =
true;
280 m_matUisUptodate = computeU;
285 template<
typename MatrixType>
288 const Index size = m_matT.cols();
293 for (Index j = 0; j < size; ++j)
294 norm += m_matT.row(j).segment((std::max)(j-1,Index(0)), size-(std::max)(j-1,Index(0))).cwiseAbs().sum();
299 template<
typename MatrixType>
300 inline typename MatrixType::Index RealSchur<MatrixType>::findSmallSubdiagEntry(Index iu, Scalar norm)
308 if (
internal::abs(m_matT.coeff(res,res-1)) < NumTraits<Scalar>::epsilon() * s)
316 template<
typename MatrixType>
317 inline void RealSchur<MatrixType>::splitOffTwoRows(Index iu,
bool computeU, Scalar exshift)
319 const Index size = m_matT.cols();
323 Scalar
p = Scalar(0.5) * (m_matT.coeff(iu-1,iu-1) - m_matT.coeff(iu,iu));
324 Scalar
q = p * p + m_matT.coeff(iu,iu-1) * m_matT.coeff(iu-1,iu);
325 m_matT.coeffRef(iu,iu) += exshift;
326 m_matT.coeffRef(iu-1,iu-1) += exshift;
331 JacobiRotation<Scalar> rot;
333 rot.makeGivens(p + z, m_matT.coeff(iu, iu-1));
335 rot.makeGivens(p - z, m_matT.coeff(iu, iu-1));
337 m_matT.rightCols(size-iu+1).applyOnTheLeft(iu-1, iu, rot.adjoint());
338 m_matT.topRows(iu+1).applyOnTheRight(iu-1, iu, rot);
339 m_matT.coeffRef(iu, iu-1) = Scalar(0);
341 m_matU.applyOnTheRight(iu-1, iu, rot);
345 m_matT.coeffRef(iu-1, iu-2) = Scalar(0);
349 template<
typename MatrixType>
350 inline void RealSchur<MatrixType>::computeShift(Index iu, Index iter, Scalar& exshift, Vector3s& shiftInfo)
352 shiftInfo.coeffRef(0) = m_matT.coeff(iu,iu);
353 shiftInfo.coeffRef(1) = m_matT.coeff(iu-1,iu-1);
354 shiftInfo.coeffRef(2) = m_matT.coeff(iu,iu-1) * m_matT.coeff(iu-1,iu);
359 exshift += shiftInfo.coeff(0);
360 for (Index i = 0; i <= iu; ++i)
361 m_matT.coeffRef(i,i) -= shiftInfo.coeff(0);
363 shiftInfo.coeffRef(0) = Scalar(0.75) * s;
364 shiftInfo.coeffRef(1) = Scalar(0.75) * s;
365 shiftInfo.coeffRef(2) = Scalar(-0.4375) * s * s;
371 Scalar s = (shiftInfo.coeff(1) - shiftInfo.coeff(0)) / Scalar(2.0);
372 s = s * s + shiftInfo.coeff(2);
376 if (shiftInfo.coeff(1) < shiftInfo.coeff(0))
378 s = s + (shiftInfo.coeff(1) - shiftInfo.coeff(0)) / Scalar(2.0);
379 s = shiftInfo.coeff(0) - shiftInfo.coeff(2) / s;
381 for (Index i = 0; i <= iu; ++i)
382 m_matT.coeffRef(i,i) -= s;
383 shiftInfo.setConstant(Scalar(0.964));
389 template<
typename MatrixType>
390 inline void RealSchur<MatrixType>::initFrancisQRStep(Index il, Index iu,
const Vector3s& shiftInfo, Index& im, Vector3s& firstHouseholderVector)
392 Vector3s& v = firstHouseholderVector;
394 for (im = iu-2; im >= il; --im)
396 const Scalar Tmm = m_matT.coeff(im,im);
397 const Scalar r = shiftInfo.coeff(0) - Tmm;
398 const Scalar s = shiftInfo.coeff(1) - Tmm;
399 v.coeffRef(0) = (r * s - shiftInfo.coeff(2)) / m_matT.coeff(im+1,im) + m_matT.coeff(im,im+1);
400 v.coeffRef(1) = m_matT.coeff(im+1,im+1) - Tmm - r - s;
401 v.coeffRef(2) = m_matT.coeff(im+2,im+1);
415 template<
typename MatrixType>
416 inline void RealSchur<MatrixType>::performFrancisQRStep(Index il, Index im, Index iu,
bool computeU,
const Vector3s& firstHouseholderVector, Scalar* workspace)
421 const Index size = m_matT.cols();
423 for (Index k = im; k <= iu-2; ++k)
425 bool firstIteration = (k == im);
429 v = firstHouseholderVector;
431 v = m_matT.template block<3,1>(k,k-1);
434 Matrix<Scalar, 2, 1> ess;
435 v.makeHouseholder(ess, tau, beta);
437 if (beta != Scalar(0))
439 if (firstIteration && k > il)
440 m_matT.coeffRef(k,k-1) = -m_matT.coeff(k,k-1);
441 else if (!firstIteration)
442 m_matT.coeffRef(k,k-1) = beta;
445 m_matT.block(k, k, 3, size-k).applyHouseholderOnTheLeft(ess, tau, workspace);
446 m_matT.block(0, k, (std::min)(iu,k+3) + 1, 3).applyHouseholderOnTheRight(ess, tau, workspace);
448 m_matU.block(0, k, size, 3).applyHouseholderOnTheRight(ess, tau, workspace);
452 Matrix<Scalar, 2, 1> v = m_matT.template block<2,1>(iu-1, iu-2);
454 Matrix<Scalar, 1, 1> ess;
455 v.makeHouseholder(ess, tau, beta);
457 if (beta != Scalar(0))
459 m_matT.coeffRef(iu-1, iu-2) = beta;
460 m_matT.block(iu-1, iu-1, 2, size-iu+1).applyHouseholderOnTheLeft(ess, tau, workspace);
461 m_matT.block(0, iu-1, iu+1, 2).applyHouseholderOnTheRight(ess, tau, workspace);
463 m_matU.block(0, iu-1, size, 2).applyHouseholderOnTheRight(ess, tau, workspace);
467 for (Index i = im+2; i <= iu; ++i)
469 m_matT.coeffRef(i,i-2) = Scalar(0);
471 m_matT.coeffRef(i,i-3) = Scalar(0);
477 #endif // EIGEN_REAL_SCHUR_H