11#ifndef EIGEN_REAL_QZ_H
12#define EIGEN_REAL_QZ_H
15#include "./InternalHeaderCheck.h"
61template <
typename MatrixType_>
64 using MatrixType = MatrixType_;
66 RowsAtCompileTime = MatrixType::RowsAtCompileTime,
67 ColsAtCompileTime = MatrixType::ColsAtCompileTime,
68 Options = internal::plain_object_options<MatrixType>::value,
69 MaxRowsAtCompileTime = MatrixType::MaxRowsAtCompileTime,
70 MaxColsAtCompileTime = MatrixType::MaxColsAtCompileTime
72 using Scalar =
typename MatrixType::Scalar;
73 using ComplexScalar = internal::make_complex_t<Scalar>;
95 explicit RealQZ(
Index size = RowsAtCompileTime == Dynamic ? 1 : RowsAtCompileTime)
100 m_workspace(size * 2),
103 m_isInitialized(false),
114 template <
typename InputTypeA,
typename InputTypeB>
118 m_Q(A.rows(), A.cols()),
119 m_Z(A.rows(), A.cols()),
120 m_workspace(A.rows() * 2),
123 m_isInitialized(false),
125 computeInPlace(computeQZ);
138 template <
typename InputTypeA,
typename InputTypeB>
142 m_Q(A.rows(), A.cols()),
143 m_Z(A.rows(), A.cols()),
144 m_workspace(A.rows() * 2),
147 m_isInitialized(false),
149 computeInPlace(computeQZ);
157 eigen_assert(m_isInitialized &&
"RealQZ is not initialized.");
158 eigen_assert(m_computeQZ &&
"The matrices Q and Z have not been computed during the QZ decomposition.");
167 eigen_assert(m_isInitialized &&
"RealQZ is not initialized.");
168 eigen_assert(m_computeQZ &&
"The matrices Q and Z have not been computed during the QZ decomposition.");
177 eigen_assert(m_isInitialized &&
"RealQZ is not initialized.");
186 eigen_assert(m_isInitialized &&
"RealQZ is not initialized.");
197 template <
typename InputTypeA,
typename InputTypeB>
205 eigen_assert(m_isInitialized &&
"RealQZ is not initialized.");
212 eigen_assert(m_isInitialized &&
"RealQZ is not initialized.");
213 return m_global_iter;
220 m_maxIters = maxIters;
228 ColumnVectorType m_hCoeffs;
231 bool m_isInitialized;
233 Scalar m_normOfT, m_normOfS;
241 RealQZ& computeInPlace(
bool computeQZ);
242 void hessenbergTriangular();
244 Index findSmallSubdiagEntry(Index iu);
245 Index findSmallDiagEntry(Index f, Index l);
246 void splitOffTwoRows(Index i);
247 void pushDownZero(Index z, Index f, Index l);
248 void step(Index f, Index l, Index iter);
253template <
typename MatrixType>
254void RealQZ<MatrixType>::hessenbergTriangular() {
255 const Index dim = m_S.cols();
258 m_hCoeffs.resize(dim);
259 internal::householder_qr_inplace_blocked<MatrixType, ColumnVectorType>::run(m_T, m_hCoeffs, 48, m_workspace.data());
260 Map<ColumnVectorType> workspace(m_workspace.data(), dim);
262 m_T.template triangularView<StrictlyLower>().setZero();
264 m_Z.noalias() = m_Q.adjoint() * m_S;
267 if (m_computeQZ) m_Z = PlainMatrixType::Identity(dim, dim);
269 for (Index j = 0; j <= dim - 3; j++) {
270 for (Index i = dim - 1; i >= j + 2; i--) {
273 if (!numext::is_exactly_zero(m_S.coeff(i, j))) {
274 G.makeGivens(m_S.coeff(i - 1, j), m_S.coeff(i, j), &m_S.coeffRef(i - 1, j));
275 m_S.coeffRef(i, j) = Scalar(0.0);
276 m_S.rightCols(dim - j - 1).applyOnTheLeft(i - 1, i, G.adjoint());
277 m_T.rightCols(dim - i + 1).applyOnTheLeft(i - 1, i, G.adjoint());
279 if (m_computeQZ) m_Q.applyOnTheRight(i - 1, i, G);
282 if (!numext::is_exactly_zero(m_T.coeff(i, i - 1))) {
283 G.makeGivens(m_T.coeff(i, i), m_T.coeff(i, i - 1), &m_T.coeffRef(i, i));
284 m_T.coeffRef(i, i - 1) = Scalar(0.0);
285 m_S.applyOnTheRight(i, i - 1, G);
286 m_T.topRows(i).applyOnTheRight(i, i - 1, G);
288 if (m_computeQZ) m_Z.applyOnTheLeft(i, i - 1, G.adjoint());
295template <
typename MatrixType>
296inline void RealQZ<MatrixType>::computeNorms() {
297 m_normOfS = internal::hessenberg_abs_sum<Upper>(m_S);
298 m_normOfT = internal::triangular_abs_sum<Upper>(m_T);
302template <
typename MatrixType>
303inline Index RealQZ<MatrixType>::findSmallSubdiagEntry(Index iu) {
307 Scalar s = abs(m_S.coeff(res - 1, res - 1)) + abs(m_S.coeff(res, res));
308 if (numext::is_exactly_zero(s)) s = m_normOfS;
309 if (abs(m_S.coeff(res, res - 1)) < NumTraits<Scalar>::epsilon() * s)
break;
316template <
typename MatrixType>
317inline Index RealQZ<MatrixType>::findSmallDiagEntry(Index f, Index l) {
321 if (abs(m_T.coeff(res, res)) <= NumTraits<Scalar>::epsilon() * m_normOfT)
break;
328template <
typename MatrixType>
329inline void RealQZ<MatrixType>::splitOffTwoRows(Index i) {
332 const Index dim = m_S.cols();
333 if (numext::is_exactly_zero(abs(m_S.coeff(i + 1, i))))
return;
334 Index j = findSmallDiagEntry(i, i + 1);
337 Matrix2s STi = m_T.template block<2, 2>(i, i).template triangularView<Upper>().template solve<OnTheRight>(
338 m_S.template block<2, 2>(i, i));
339 Scalar p = Scalar(0.5) * (STi(0, 0) - STi(1, 1));
340 Scalar q = p * p + STi(1, 0) * STi(0, 1);
348 G.makeGivens(p + z, STi(1, 0));
350 G.makeGivens(p - z, STi(1, 0));
351 m_S.rightCols(dim - i).applyOnTheLeft(i, i + 1, G.adjoint());
352 m_T.rightCols(dim - i).applyOnTheLeft(i, i + 1, G.adjoint());
354 if (m_computeQZ) m_Q.applyOnTheRight(i, i + 1, G);
356 G.makeGivens(m_T.coeff(i + 1, i + 1), m_T.coeff(i + 1, i));
357 m_S.topRows(i + 2).applyOnTheRight(i + 1, i, G);
358 m_T.topRows(i + 2).applyOnTheRight(i + 1, i, G);
360 if (m_computeQZ) m_Z.applyOnTheLeft(i + 1, i, G.adjoint());
362 m_S.coeffRef(i + 1, i) = Scalar(0.0);
363 m_T.coeffRef(i + 1, i) = Scalar(0.0);
366 pushDownZero(j, i, i + 1);
371template <
typename MatrixType>
372inline void RealQZ<MatrixType>::pushDownZero(Index z, Index f, Index l) {
374 const Index dim = m_S.cols();
375 for (Index zz = z; zz < l; zz++) {
377 Index firstColS = zz > f ? (zz - 1) : zz;
378 G.makeGivens(m_T.coeff(zz, zz + 1), m_T.coeff(zz + 1, zz + 1));
379 m_S.rightCols(dim - firstColS).applyOnTheLeft(zz, zz + 1, G.adjoint());
380 m_T.rightCols(dim - zz).applyOnTheLeft(zz, zz + 1, G.adjoint());
381 m_T.coeffRef(zz + 1, zz + 1) = Scalar(0.0);
383 if (m_computeQZ) m_Q.applyOnTheRight(zz, zz + 1, G);
386 G.makeGivens(m_S.coeff(zz + 1, zz), m_S.coeff(zz + 1, zz - 1));
387 m_S.topRows(zz + 2).applyOnTheRight(zz, zz - 1, G);
388 m_T.topRows(zz + 1).applyOnTheRight(zz, zz - 1, G);
389 m_S.coeffRef(zz + 1, zz - 1) = Scalar(0.0);
391 if (m_computeQZ) m_Z.applyOnTheLeft(zz, zz - 1, G.adjoint());
395 G.makeGivens(m_S.coeff(l, l), m_S.coeff(l, l - 1));
396 m_S.applyOnTheRight(l, l - 1, G);
397 m_T.applyOnTheRight(l, l - 1, G);
398 m_S.coeffRef(l, l - 1) = Scalar(0.0);
400 if (m_computeQZ) m_Z.applyOnTheLeft(l, l - 1, G.adjoint());
404template <
typename MatrixType>
405inline void RealQZ<MatrixType>::step(Index f, Index l, Index iter) {
407 const Index dim = m_S.cols();
413 const Scalar a11 = m_S.coeff(f + 0, f + 0), a12 = m_S.coeff(f + 0, f + 1), a21 = m_S.coeff(f + 1, f + 0),
414 a22 = m_S.coeff(f + 1, f + 1), a32 = m_S.coeff(f + 2, f + 1), b12 = m_T.coeff(f + 0, f + 1),
415 b11i = Scalar(1.0) / m_T.coeff(f + 0, f + 0), b22i = Scalar(1.0) / m_T.coeff(f + 1, f + 1),
416 a87 = m_S.coeff(l - 1, l - 2), a98 = m_S.coeff(l - 0, l - 1),
417 b77i = Scalar(1.0) / m_T.coeff(l - 2, l - 2), b88i = Scalar(1.0) / m_T.coeff(l - 1, l - 1);
418 Scalar ss = abs(a87 * b77i) + abs(a98 * b88i), lpl = Scalar(1.5) * ss, ll = ss * ss;
419 x = ll + a11 * a11 * b11i * b11i - lpl * a11 * b11i + a12 * a21 * b11i * b22i -
420 a11 * a21 * b12 * b11i * b11i * b22i;
421 y = a11 * a21 * b11i * b11i - lpl * a21 * b11i + a21 * a22 * b11i * b22i - a21 * a21 * b12 * b11i * b11i * b22i;
422 z = a21 * a32 * b11i * b22i;
423 }
else if (iter == 16) {
425 x = m_S.coeff(f, f) / m_T.coeff(f, f) - m_S.coeff(l, l) / m_T.coeff(l, l) +
426 m_S.coeff(l, l - 1) * m_T.coeff(l - 1, l) / (m_T.coeff(l - 1, l - 1) * m_T.coeff(l, l));
427 y = m_S.coeff(f + 1, f) / m_T.coeff(f, f);
429 }
else if (iter > 23 && !(iter % 8)) {
431 x = internal::random<Scalar>(-1.0, 1.0);
432 y = internal::random<Scalar>(-1.0, 1.0);
433 z = internal::random<Scalar>(-1.0, 1.0);
441 const Scalar a11 = m_S.coeff(f, f), a12 = m_S.coeff(f, f + 1), a21 = m_S.coeff(f + 1, f),
442 a22 = m_S.coeff(f + 1, f + 1), a32 = m_S.coeff(f + 2, f + 1),
444 a88 = m_S.coeff(l - 1, l - 1), a89 = m_S.coeff(l - 1, l), a98 = m_S.coeff(l, l - 1),
445 a99 = m_S.coeff(l, l),
447 b11 = m_T.coeff(f, f), b12 = m_T.coeff(f, f + 1), b22 = m_T.coeff(f + 1, f + 1),
449 b88 = m_T.coeff(l - 1, l - 1), b89 = m_T.coeff(l - 1, l), b99 = m_T.coeff(l, l);
451 x = ((a88 / b88 - a11 / b11) * (a99 / b99 - a11 / b11) - (a89 / b99) * (a98 / b88) +
452 (a98 / b88) * (b89 / b99) * (a11 / b11)) *
454 a12 / b22 - (a11 / b11) * (b12 / b22);
455 y = (a22 / b22 - a11 / b11) - (a21 / b11) * (b12 / b22) - (a88 / b88 - a11 / b11) - (a99 / b99 - a11 / b11) +
456 (a98 / b88) * (b89 / b99);
462 for (Index k = f; k <= l - 2; k++) {
467 Vector3s hr(x, y, z);
470 hr.makeHouseholderInPlace(tau, beta);
471 essential2 = hr.template bottomRows<2>();
472 Index fc = (std::max)(k - 1, Index(0));
473 m_S.template middleRows<3>(k).rightCols(dim - fc).applyHouseholderOnTheLeft(essential2, tau, m_workspace.data());
474 m_T.template middleRows<3>(k).rightCols(dim - fc).applyHouseholderOnTheLeft(essential2, tau, m_workspace.data());
475 if (m_computeQZ) m_Q.template middleCols<3>(k).applyHouseholderOnTheRight(essential2, tau, m_workspace.data());
476 if (k > f) m_S.coeffRef(k + 2, k - 1) = m_S.coeffRef(k + 1, k - 1) = Scalar(0.0);
479 hr << m_T.coeff(k + 2, k + 2), m_T.coeff(k + 2, k), m_T.coeff(k + 2, k + 1);
480 hr.makeHouseholderInPlace(tau, beta);
481 essential2 = hr.template bottomRows<2>();
483 Index lr = (std::min)(k + 4, dim);
486 tmp.noalias() = m_S.template middleCols<2>(k).topRows(lr) * essential2;
487 tmp += m_S.col(k + 2).head(lr);
488 m_S.col(k + 2).head(lr) -= tau * tmp;
489 m_S.template middleCols<2>(k).topRows(lr).noalias() -= (tau * tmp) * essential2.adjoint();
491 tmp.noalias() = m_T.template middleCols<2>(k).topRows(lr) * essential2;
492 tmp += m_T.col(k + 2).head(lr);
493 m_T.col(k + 2).head(lr) -= tau * tmp;
494 m_T.template middleCols<2>(k).topRows(lr).noalias() -= (tau * tmp) * essential2.adjoint();
499 tmp.noalias() = essential2.adjoint() * (m_Z.template middleRows<2>(k));
500 tmp += m_Z.row(k + 2);
501 m_Z.row(k + 2) -= tau * tmp;
502 m_Z.template middleRows<2>(k).noalias() -= essential2 * (tau * tmp);
504 m_T.coeffRef(k + 2, k) = m_T.coeffRef(k + 2, k + 1) = Scalar(0.0);
507 G.makeGivens(m_T.coeff(k + 1, k + 1), m_T.coeff(k + 1, k));
508 m_S.applyOnTheRight(k + 1, k, G);
509 m_T.applyOnTheRight(k + 1, k, G);
511 if (m_computeQZ) m_Z.applyOnTheLeft(k + 1, k, G.adjoint());
512 m_T.coeffRef(k + 1, k) = Scalar(0.0);
515 x = m_S.coeff(k + 1, k);
516 y = m_S.coeff(k + 2, k);
517 if (k < l - 2) z = m_S.coeff(k + 3, k);
522 m_S.applyOnTheLeft(l - 1, l, G.adjoint());
523 m_T.applyOnTheLeft(l - 1, l, G.adjoint());
524 if (m_computeQZ) m_Q.applyOnTheRight(l - 1, l, G);
525 m_S.coeffRef(l, l - 2) = Scalar(0.0);
528 G.makeGivens(m_T.coeff(l, l), m_T.coeff(l, l - 1));
529 m_S.applyOnTheRight(l, l - 1, G);
530 m_T.applyOnTheRight(l, l - 1, G);
531 if (m_computeQZ) m_Z.applyOnTheLeft(l, l - 1, G.adjoint());
532 m_T.coeffRef(l, l - 1) = Scalar(0.0);
535template <
typename MatrixType>
536template <
typename InputTypeA,
typename InputTypeB>
539 eigen_assert(A_in.rows() == A_in.cols() && B_in.rows() == A_in.cols() && B_in.cols() == A_in.cols() &&
540 "Need square matrices of the same dimension");
541 m_S = A_in.derived();
542 m_T = B_in.derived();
543 return computeInPlace(computeQZ);
547template <
typename MatrixType>
549 const Index dim = m_S.cols();
551 eigen_assert(m_S.rows() == dim && m_T.rows() == dim && m_T.cols() == dim &&
552 "Need square matrices of the same dimension");
554 m_isInitialized =
true;
555 m_computeQZ = computeQZ;
556 m_workspace.resize(dim * 2);
560 hessenbergTriangular();
564 Index l = dim - 1, f, local_iter = 0;
566 while (l > 0 && local_iter < m_maxIters) {
567 f = findSmallSubdiagEntry(l);
569 if (f > 0) m_S.coeffRef(f, f - 1) = Scalar(0.0);
574 }
else if (f == l - 1)
582 Index z = findSmallDiagEntry(f, l);
585 pushDownZero(z, f, l);
590 step(f, l, local_iter);
606 for (Index i = 0; i < dim - 1; ++i) {
607 if (!numext::is_exactly_zero(m_S.coeff(i + 1, i))) {
609 internal::real_2x2_jacobi_svd(m_T, i, i + 1, &j_left, &j_right);
612 m_S.applyOnTheLeft(i, i + 1, j_left);
613 m_S.applyOnTheRight(i, i + 1, j_right);
614 m_T.applyOnTheLeft(i, i + 1, j_left);
615 m_T.applyOnTheRight(i, i + 1, j_right);
616 m_T(i + 1, i) = m_T(i, i + 1) = Scalar(0);
619 m_Q.applyOnTheRight(i, i + 1, j_left.transpose());
620 m_Z.applyOnTheLeft(i, i + 1, j_right.transpose());
631 m_T.template triangularView<StrictlyLower>().setZero();
Rotation given by a cosine-sine pair.
Definition Jacobi.h:39
A matrix or vector expression mapping an existing array of data.
Definition Map.h:97
The matrix class, also used for vectors and row-vectors.
Definition Matrix.h:188
Performs a real QZ decomposition of a pair of square matrices.
Definition RealQZ.h:62
RealQZ(EigenBase< InputTypeA > &A, EigenBase< InputTypeB > &B, bool computeQZ=true)
Constructor for inplace decomposition .
Definition RealQZ.h:139
const MatrixType & matrixT() const
Returns matrix S in the QZ decomposition.
Definition RealQZ.h:185
const MatrixType & matrixS() const
Returns matrix S in the QZ decomposition.
Definition RealQZ.h:176
Index iterations() const
Returns number of performed QR-like iterations.
Definition RealQZ.h:211
Eigen::Index Index
Definition RealQZ.h:74
const PlainMatrixType & matrixQ() const
Returns matrix Q in the QZ decomposition.
Definition RealQZ.h:156
ComputationInfo info() const
Reports whether previous computation was successful.
Definition RealQZ.h:204
Matrix< Scalar, RowsAtCompileTime, ColsAtCompileTime, Options, MaxRowsAtCompileTime, MaxColsAtCompileTime > PlainMatrixType
Type of the matrices returned by matrixQ() and matrixZ(): a plain matrix with the shape and storage o...
Definition RealQZ.h:81
const PlainMatrixType & matrixZ() const
Returns matrix Z in the QZ decomposition.
Definition RealQZ.h:166
RealQZ & compute(const EigenBase< InputTypeA > &A, const EigenBase< InputTypeB > &B, bool computeQZ=true)
Computes QZ decomposition of given matrix.
RealQZ(Index size=RowsAtCompileTime==Dynamic ? 1 :RowsAtCompileTime)
Default constructor.
Definition RealQZ.h:95
RealQZ(const EigenBase< InputTypeA > &A, const EigenBase< InputTypeB > &B, bool computeQZ=true)
Constructor; computes real QZ decomposition of given matrices.
Definition RealQZ.h:115
RealQZ & setMaxIterations(Index maxIters)
Definition RealQZ.h:219
HouseholderSequence< VectorsType, CoeffsType > householderSequence(const VectorsType &v, const CoeffsType &h)
Convenience function for constructing a Householder sequence.
Definition HouseholderSequence.h:673
ComputationInfo
Definition Constants.h:455
@ Success
Definition Constants.h:457
@ NoConvergence
Definition Constants.h:461
Definition EigenBase.h:34