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();
246 Index il = findSmallSubdiagEntry(iu, norm);
251 m_matT.coeffRef(iu,iu) = m_matT.coeff(iu,iu) + exshift;
253 m_matT.coeffRef(iu, iu-1) =
Scalar(0);
259 splitOffTwoRows(iu, computeU, exshift);
266 Vector3s firstHouseholderVector(0,0,0), shiftInfo;
267 computeShift(iu, iter, exshift, shiftInfo);
269 if (iter > m_maxIterations)
break;
271 initFrancisQRStep(il, iu, shiftInfo, im, firstHouseholderVector);
272 performFrancisQRStep(il, im, iu, computeU, firstHouseholderVector, workspace);
276 if(iter <= m_maxIterations)
281 m_isInitialized =
true;
282 m_matUisUptodate = computeU;
287 template<
typename MatrixType>
290 const Index size = m_matT.cols();
295 for (Index j = 0; j < size; ++j)
296 norm += m_matT.row(j).segment((std::max)(j-1,Index(0)), size-(std::max)(j-1,Index(0))).cwiseAbs().sum();
301 template<
typename MatrixType>
302 inline typename MatrixType::Index RealSchur<MatrixType>::findSmallSubdiagEntry(Index iu, Scalar norm)
310 if (
internal::abs(m_matT.coeff(res,res-1)) < NumTraits<Scalar>::epsilon() * s)
318 template<
typename MatrixType>
319 inline void RealSchur<MatrixType>::splitOffTwoRows(Index iu,
bool computeU, Scalar exshift)
321 const Index size = m_matT.cols();
325 Scalar
p = Scalar(0.5) * (m_matT.coeff(iu-1,iu-1) - m_matT.coeff(iu,iu));
326 Scalar
q = p * p + m_matT.coeff(iu,iu-1) * m_matT.coeff(iu-1,iu);
327 m_matT.coeffRef(iu,iu) += exshift;
328 m_matT.coeffRef(iu-1,iu-1) += exshift;
333 JacobiRotation<Scalar> rot;
335 rot.makeGivens(p + z, m_matT.coeff(iu, iu-1));
337 rot.makeGivens(p - z, m_matT.coeff(iu, iu-1));
339 m_matT.rightCols(size-iu+1).applyOnTheLeft(iu-1, iu, rot.adjoint());
340 m_matT.topRows(iu+1).applyOnTheRight(iu-1, iu, rot);
341 m_matT.coeffRef(iu, iu-1) = Scalar(0);
343 m_matU.applyOnTheRight(iu-1, iu, rot);
347 m_matT.coeffRef(iu-1, iu-2) = Scalar(0);
351 template<
typename MatrixType>
352 inline void RealSchur<MatrixType>::computeShift(Index iu, Index iter, Scalar& exshift, Vector3s& shiftInfo)
354 shiftInfo.coeffRef(0) = m_matT.coeff(iu,iu);
355 shiftInfo.coeffRef(1) = m_matT.coeff(iu-1,iu-1);
356 shiftInfo.coeffRef(2) = m_matT.coeff(iu,iu-1) * m_matT.coeff(iu-1,iu);
361 exshift += shiftInfo.coeff(0);
362 for (Index i = 0; i <= iu; ++i)
363 m_matT.coeffRef(i,i) -= shiftInfo.coeff(0);
365 shiftInfo.coeffRef(0) = Scalar(0.75) * s;
366 shiftInfo.coeffRef(1) = Scalar(0.75) * s;
367 shiftInfo.coeffRef(2) = Scalar(-0.4375) * s * s;
373 Scalar s = (shiftInfo.coeff(1) - shiftInfo.coeff(0)) / Scalar(2.0);
374 s = s * s + shiftInfo.coeff(2);
378 if (shiftInfo.coeff(1) < shiftInfo.coeff(0))
380 s = s + (shiftInfo.coeff(1) - shiftInfo.coeff(0)) / Scalar(2.0);
381 s = shiftInfo.coeff(0) - shiftInfo.coeff(2) / s;
383 for (Index i = 0; i <= iu; ++i)
384 m_matT.coeffRef(i,i) -= s;
385 shiftInfo.setConstant(Scalar(0.964));
391 template<
typename MatrixType>
392 inline void RealSchur<MatrixType>::initFrancisQRStep(Index il, Index iu,
const Vector3s& shiftInfo, Index& im, Vector3s& firstHouseholderVector)
394 Vector3s& v = firstHouseholderVector;
396 for (im = iu-2; im >= il; --im)
398 const Scalar Tmm = m_matT.coeff(im,im);
399 const Scalar r = shiftInfo.coeff(0) - Tmm;
400 const Scalar s = shiftInfo.coeff(1) - Tmm;
401 v.coeffRef(0) = (r * s - shiftInfo.coeff(2)) / m_matT.coeff(im+1,im) + m_matT.coeff(im,im+1);
402 v.coeffRef(1) = m_matT.coeff(im+1,im+1) - Tmm - r - s;
403 v.coeffRef(2) = m_matT.coeff(im+2,im+1);
417 template<
typename MatrixType>
418 inline void RealSchur<MatrixType>::performFrancisQRStep(Index il, Index im, Index iu,
bool computeU,
const Vector3s& firstHouseholderVector, Scalar* workspace)
423 const Index size = m_matT.cols();
425 for (Index k = im; k <= iu-2; ++k)
427 bool firstIteration = (k == im);
431 v = firstHouseholderVector;
433 v = m_matT.template block<3,1>(k,k-1);
436 Matrix<Scalar, 2, 1> ess;
437 v.makeHouseholder(ess, tau, beta);
439 if (beta != Scalar(0))
441 if (firstIteration && k > il)
442 m_matT.coeffRef(k,k-1) = -m_matT.coeff(k,k-1);
443 else if (!firstIteration)
444 m_matT.coeffRef(k,k-1) = beta;
447 m_matT.block(k, k, 3, size-k).applyHouseholderOnTheLeft(ess, tau, workspace);
448 m_matT.block(0, k, (std::min)(iu,k+3) + 1, 3).applyHouseholderOnTheRight(ess, tau, workspace);
450 m_matU.block(0, k, size, 3).applyHouseholderOnTheRight(ess, tau, workspace);
454 Matrix<Scalar, 2, 1> v = m_matT.template block<2,1>(iu-1, iu-2);
456 Matrix<Scalar, 1, 1> ess;
457 v.makeHouseholder(ess, tau, beta);
459 if (beta != Scalar(0))
461 m_matT.coeffRef(iu-1, iu-2) = beta;
462 m_matT.block(iu-1, iu-1, 2, size-iu+1).applyHouseholderOnTheLeft(ess, tau, workspace);
463 m_matT.block(0, iu-1, iu+1, 2).applyHouseholderOnTheRight(ess, tau, workspace);
465 m_matU.block(0, iu-1, size, 2).applyHouseholderOnTheRight(ess, tau, workspace);
469 for (Index i = im+2; i <= iu; ++i)
471 m_matT.coeffRef(i,i-2) = Scalar(0);
473 m_matT.coeffRef(i,i-3) = Scalar(0);
479 #endif // EIGEN_REAL_SCHUR_H