10#ifndef EIGEN_REAL_QZ_H
11#define EIGEN_REAL_QZ_H
57 template<
typename _MatrixType>
class RealQZ
68 typedef typename MatrixType::Scalar
Scalar;
93 m_isInitialized(false),
106 m_S(A.rows(),A.cols()),
107 m_T(A.rows(),A.cols()),
108 m_Q(A.rows(),A.cols()),
109 m_Z(A.rows(),A.cols()),
110 m_workspace(A.rows()*2),
112 m_isInitialized(false),
123 eigen_assert(m_isInitialized &&
"RealQZ is not initialized.");
124 eigen_assert(m_computeQZ &&
"The matrices Q and Z have not been computed during the QZ decomposition.");
133 eigen_assert(m_isInitialized &&
"RealQZ is not initialized.");
134 eigen_assert(m_computeQZ &&
"The matrices Q and Z have not been computed during the QZ decomposition.");
143 eigen_assert(m_isInitialized &&
"RealQZ is not initialized.");
152 eigen_assert(m_isInitialized &&
"RealQZ is not initialized.");
171 eigen_assert(m_isInitialized &&
"RealQZ is not initialized.");
179 eigen_assert(m_isInitialized &&
"RealQZ is not initialized.");
180 return m_global_iter;
188 m_maxIters = maxIters;
198 bool m_isInitialized;
200 Scalar m_normOfT, m_normOfS;
208 void hessenbergTriangular();
212 void splitOffTwoRows(
Index i);
219 template<
typename MatrixType>
220 void RealQZ<MatrixType>::hessenbergTriangular()
223 const Index dim = m_S.cols();
226 HouseholderQR<MatrixType> qrT(m_T);
227 m_T = qrT.matrixQR();
228 m_T.template triangularView<StrictlyLower>().setZero();
229 m_Q = qrT.householderQ();
231 m_S.applyOnTheLeft(m_Q.adjoint());
234 m_Z = MatrixType::Identity(dim,dim);
236 for (
Index j=0; j<=dim-3; j++) {
237 for (
Index i=dim-1; i>=j+2; i--) {
240 if(m_S.coeff(i,j) != 0)
242 G.makeGivens(m_S.coeff(i-1,j), m_S.coeff(i,j), &m_S.coeffRef(i-1, j));
243 m_S.coeffRef(i,j) = Scalar(0.0);
244 m_S.rightCols(dim-j-1).applyOnTheLeft(i-1,i,
G.adjoint());
245 m_T.rightCols(dim-i+1).applyOnTheLeft(i-1,i,
G.adjoint());
248 m_Q.applyOnTheRight(i-1,i,
G);
251 if(m_T.coeff(i,i-1)!=Scalar(0))
253 G.makeGivens(m_T.coeff(i,i), m_T.coeff(i,i-1), &m_T.coeffRef(i,i));
254 m_T.coeffRef(i,i-1) = Scalar(0.0);
255 m_S.applyOnTheRight(i,i-1,
G);
256 m_T.topRows(i).applyOnTheRight(i,i-1,
G);
259 m_Z.applyOnTheLeft(i,i-1,
G.adjoint());
266 template<
typename MatrixType>
267 inline void RealQZ<MatrixType>::computeNorms()
270 m_normOfS = Scalar(0.0);
271 m_normOfT = Scalar(0.0);
274 m_normOfS += m_S.col(j).segment(0, (
std::min)(
size,j+2)).cwiseAbs().sum();
275 m_normOfT += m_T.row(j).segment(j,
size - j).cwiseAbs().sum();
281 template<
typename MatrixType>
282 inline Index RealQZ<MatrixType>::findSmallSubdiagEntry(
Index iu)
288 Scalar s =
abs(m_S.coeff(res-1,res-1)) +
abs(m_S.coeff(res,res));
289 if (s == Scalar(0.0))
291 if (
abs(m_S.coeff(res,res-1)) < NumTraits<Scalar>::epsilon() * s)
299 template<
typename MatrixType>
305 if (
abs(m_T.coeff(res,res)) <= NumTraits<Scalar>::epsilon() * m_normOfT)
313 template<
typename MatrixType>
314 inline void RealQZ<MatrixType>::splitOffTwoRows(
Index i)
318 const Index dim=m_S.cols();
319 if (
abs(m_S.coeff(i+1,i))==Scalar(0))
321 Index j = findSmallDiagEntry(i,i+1);
325 Matrix2s STi = m_T.template block<2,2>(i,i).template triangularView<Upper>().
326 template solve<OnTheRight>(m_S.template block<2,2>(i,i));
327 Scalar p = Scalar(0.5)*(STi(0,0)-STi(1,1));
328 Scalar q = p*p + STi(1,0)*STi(0,1);
336 G.makeGivens(p + z, STi(1,0));
338 G.makeGivens(p - z, STi(1,0));
339 m_S.rightCols(dim-i).applyOnTheLeft(i,i+1,
G.adjoint());
340 m_T.rightCols(dim-i).applyOnTheLeft(i,i+1,
G.adjoint());
343 m_Q.applyOnTheRight(i,i+1,
G);
345 G.makeGivens(m_T.coeff(i+1,i+1), m_T.coeff(i+1,i));
346 m_S.topRows(i+2).applyOnTheRight(i+1,i,
G);
347 m_T.topRows(i+2).applyOnTheRight(i+1,i,
G);
350 m_Z.applyOnTheLeft(i+1,i,
G.adjoint());
352 m_S.coeffRef(i+1,i) = Scalar(0.0);
353 m_T.coeffRef(i+1,i) = Scalar(0.0);
358 pushDownZero(j,i,i+1);
363 template<
typename MatrixType>
364 inline void RealQZ<MatrixType>::pushDownZero(
Index z,
Index f,
Index l)
367 const Index dim = m_S.cols();
368 for (
Index zz=z; zz<l; zz++)
371 Index firstColS = zz>f ? (zz-1) : zz;
372 G.makeGivens(m_T.coeff(zz, zz+1), m_T.coeff(zz+1, zz+1));
373 m_S.rightCols(dim-firstColS).applyOnTheLeft(zz,zz+1,
G.adjoint());
374 m_T.rightCols(dim-zz).applyOnTheLeft(zz,zz+1,
G.adjoint());
375 m_T.coeffRef(zz+1,zz+1) = Scalar(0.0);
378 m_Q.applyOnTheRight(zz,zz+1,
G);
382 G.makeGivens(m_S.coeff(zz+1, zz), m_S.coeff(zz+1,zz-1));
383 m_S.topRows(zz+2).applyOnTheRight(zz, zz-1,
G);
384 m_T.topRows(zz+1).applyOnTheRight(zz, zz-1,
G);
385 m_S.coeffRef(zz+1,zz-1) = Scalar(0.0);
388 m_Z.applyOnTheLeft(zz,zz-1,
G.adjoint());
392 G.makeGivens(m_S.coeff(l,l), m_S.coeff(l,l-1));
393 m_S.applyOnTheRight(l,l-1,
G);
394 m_T.applyOnTheRight(l,l-1,
G);
395 m_S.coeffRef(l,l-1)=Scalar(0.0);
398 m_Z.applyOnTheLeft(l,l-1,
G.adjoint());
402 template<
typename MatrixType>
406 const Index dim = m_S.cols();
414 a11=m_S.coeff(f+0,f+0), a12=m_S.coeff(f+0,f+1),
415 a21=m_S.coeff(f+1,f+0), a22=m_S.coeff(f+1,f+1), a32=m_S.coeff(f+2,f+1),
416 b12=m_T.coeff(f+0,f+1),
417 b11i=Scalar(1.0)/m_T.coeff(f+0,f+0),
418 b22i=Scalar(1.0)/m_T.coeff(f+1,f+1),
419 a87=m_S.coeff(l-1,l-2),
420 a98=m_S.coeff(l-0,l-1),
421 b77i=Scalar(1.0)/m_T.coeff(l-2,l-2),
422 b88i=Scalar(1.0)/m_T.coeff(l-1,l-1);
423 Scalar ss =
abs(a87*b77i) +
abs(a98*b88i),
424 lpl = Scalar(1.5)*ss,
426 x = ll + a11*a11*b11i*b11i - lpl*a11*b11i + a12*a21*b11i*b22i
427 - a11*a21*b12*b11i*b11i*b22i;
428 y = a11*a21*b11i*b11i - lpl*a21*b11i + a21*a22*b11i*b22i
429 - a21*a21*b12*b11i*b11i*b22i;
430 z = a21*a32*b11i*b22i;
435 x = m_S.coeff(f,f)/m_T.coeff(f,f)-m_S.coeff(l,l)/m_T.coeff(l,l) + m_S.coeff(l,l-1)*m_T.coeff(l-1,l) /
436 (m_T.coeff(l-1,l-1)*m_T.coeff(l,l));
437 y = m_S.coeff(f+1,f)/m_T.coeff(f,f);
440 else if (iter>23 && !(iter%8))
443 x = internal::random<Scalar>(-1.0,1.0);
444 y = internal::random<Scalar>(-1.0,1.0);
445 z = internal::random<Scalar>(-1.0,1.0);
456 a11 = m_S.coeff(f,f), a12 = m_S.coeff(f,f+1),
457 a21 = m_S.coeff(f+1,f), a22 = m_S.coeff(f+1,f+1),
458 a32 = m_S.coeff(f+2,f+1),
460 a88 = m_S.coeff(l-1,l-1), a89 = m_S.coeff(l-1,l),
461 a98 = m_S.coeff(l,l-1), a99 = m_S.coeff(l,l),
463 b11 = m_T.coeff(f,f), b12 = m_T.coeff(f,f+1),
464 b22 = m_T.coeff(f+1,f+1),
466 b88 = m_T.coeff(l-1,l-1), b89 = m_T.coeff(l-1,l),
467 b99 = m_T.coeff(l,l);
469 x = ( (a88/b88 - a11/b11)*(a99/b99 - a11/b11) - (a89/b99)*(a98/b88) + (a98/b88)*(b89/b99)*(a11/b11) ) * (b11/a21)
470 + a12/b22 - (a11/b11)*(b12/b22);
471 y = (a22/b22-a11/b11) - (a21/b11)*(b12/b22) - (a88/b88-a11/b11) - (a99/b99-a11/b11) + (a98/b88)*(b89/b99);
477 for (
Index k=f; k<=l-2; k++)
486 hr.makeHouseholderInPlace(tau,
beta);
487 essential2 = hr.template bottomRows<2>();
489 m_S.template middleRows<3>(k).rightCols(dim-
fc).applyHouseholderOnTheLeft(essential2, tau, m_workspace.data());
490 m_T.template middleRows<3>(k).rightCols(dim-
fc).applyHouseholderOnTheLeft(essential2, tau, m_workspace.data());
492 m_Q.template middleCols<3>(k).applyHouseholderOnTheRight(essential2, tau, m_workspace.data());
494 m_S.coeffRef(k+2,k-1) = m_S.coeffRef(k+1,k-1) = Scalar(0.0);
497 hr << m_T.coeff(k+2,k+2),m_T.coeff(k+2,k),m_T.coeff(k+2,k+1);
498 hr.makeHouseholderInPlace(tau,
beta);
499 essential2 = hr.template bottomRows<2>();
502 Map<Matrix<Scalar,Dynamic,1> > tmp(m_workspace.data(),lr);
504 tmp = m_S.template middleCols<2>(k).topRows(lr) * essential2;
505 tmp += m_S.col(k+2).head(lr);
506 m_S.col(k+2).head(lr) -= tau*tmp;
507 m_S.template middleCols<2>(k).topRows(lr) -= (tau*tmp) * essential2.adjoint();
509 tmp = m_T.template middleCols<2>(k).topRows(lr) * essential2;
510 tmp += m_T.col(k+2).head(lr);
511 m_T.col(k+2).head(lr) -= tau*tmp;
512 m_T.template middleCols<2>(k).topRows(lr) -= (tau*tmp) * essential2.adjoint();
517 Map<Matrix<Scalar,1,Dynamic> > tmp(m_workspace.data(),dim);
518 tmp = essential2.adjoint()*(m_Z.template middleRows<2>(k));
520 m_Z.row(k+2) -= tau*tmp;
521 m_Z.template middleRows<2>(k) -= essential2 * (tau*tmp);
523 m_T.coeffRef(k+2,k) = m_T.coeffRef(k+2,k+1) = Scalar(0.0);
526 G.makeGivens(m_T.coeff(k+1,k+1), m_T.coeff(k+1,k));
527 m_S.applyOnTheRight(k+1,k,
G);
528 m_T.applyOnTheRight(k+1,k,
G);
531 m_Z.applyOnTheLeft(k+1,k,
G.adjoint());
532 m_T.coeffRef(k+1,k) = Scalar(0.0);
535 x = m_S.coeff(k+1,k);
536 y = m_S.coeff(k+2,k);
538 z = m_S.coeff(k+3,k);
543 m_S.applyOnTheLeft(l-1,l,
G.adjoint());
544 m_T.applyOnTheLeft(l-1,l,
G.adjoint());
546 m_Q.applyOnTheRight(l-1,l,
G);
547 m_S.coeffRef(l,l-2) = Scalar(0.0);
550 G.makeGivens(m_T.coeff(l,l),m_T.coeff(l,l-1));
551 m_S.applyOnTheRight(l,l-1,
G);
552 m_T.applyOnTheRight(l,l-1,
G);
554 m_Z.applyOnTheLeft(l,l-1,
G.adjoint());
555 m_T.coeffRef(l,l-1) = Scalar(0.0);
558 template<
typename MatrixType>
562 const Index dim = A_in.cols();
565 && B_in.rows()==dim && B_in.cols()==dim
566 &&
"Need square matrices of the same dimension");
568 m_isInitialized =
true;
569 m_computeQZ = computeQZ;
570 m_S = A_in; m_T = B_in;
571 m_workspace.resize(dim*2);
575 hessenbergTriangular();
583 while (l>0 && local_iter<m_maxIters)
585 f = findSmallSubdiagEntry(l);
587 if (f>0) m_S.coeffRef(f,f-1) =
Scalar(0.0);
602 Index z = findSmallDiagEntry(f,l);
613 step(f,l, local_iter);
628 for(
Index i=0; i<dim-1; ++i)
630 if(m_S.coeff(i+1, i) !=
Scalar(0))
636 m_S.applyOnTheLeft(i,i+1,j_left);
637 m_S.applyOnTheRight(i,i+1,j_right);
638 m_T.applyOnTheLeft(i,i+1,j_left);
639 m_T.applyOnTheRight(i,i+1,j_right);
640 m_T(i+1,i) = m_T(i,i+1) =
Scalar(0);
643 m_Q.applyOnTheRight(i,i+1,j_left.
transpose());
644 m_Z.applyOnTheLeft(i,i+1,j_right.
transpose());
#define eigen_assert(x)
Definition: Macros.h:1047
constexpr common_return_t< T1, T2 > beta(const T1 a, const T2 b) noexcept
Compile-time beta function.
Definition: beta.hpp:36
\jacobi_module
Definition: Jacobi.h:35
EIGEN_DEVICE_FUNC JacobiRotation transpose() const
Returns the transposed transformation.
Definition: Jacobi.h:63
\eigenvalues_module
Definition: RealQZ.h:58
MatrixType::Scalar Scalar
Definition: RealQZ.h:68
const MatrixType & matrixQ() const
Returns matrix Q in the QZ decomposition.
Definition: RealQZ.h:122
std::complex< typename NumTraits< Scalar >::Real > ComplexScalar
Definition: RealQZ.h:69
RealQZ & compute(const MatrixType &A, const MatrixType &B, bool computeQZ=true)
Computes QZ decomposition of given matrix.
Definition: RealQZ.h:559
ComputationInfo info() const
Reports whether previous computation was successful.
Definition: RealQZ.h:169
@ Options
Definition: RealQZ.h:64
@ MaxColsAtCompileTime
Definition: RealQZ.h:66
@ ColsAtCompileTime
Definition: RealQZ.h:63
@ MaxRowsAtCompileTime
Definition: RealQZ.h:65
@ RowsAtCompileTime
Definition: RealQZ.h:62
Eigen::Index Index
Definition: RealQZ.h:70
_MatrixType MatrixType
Definition: RealQZ.h:60
const MatrixType & matrixT() const
Returns matrix S in the QZ decomposition.
Definition: RealQZ.h:151
Matrix< Scalar, ColsAtCompileTime, 1, Options &~RowMajor, MaxColsAtCompileTime, 1 > ColumnVectorType
Definition: RealQZ.h:73
RealQZ & setMaxIterations(Index maxIters)
Sets the maximal number of iterations allowed to converge to one eigenvalue or decouple the problem.
Definition: RealQZ.h:186
RealQZ(const MatrixType &A, const MatrixType &B, bool computeQZ=true)
Constructor; computes real QZ decomposition of given matrices.
Definition: RealQZ.h:105
RealQZ(Index size=RowsAtCompileTime==Dynamic ? 1 :RowsAtCompileTime)
Default constructor.
Definition: RealQZ.h:86
const MatrixType & matrixS() const
Returns matrix S in the QZ decomposition.
Definition: RealQZ.h:142
const MatrixType & matrixZ() const
Returns matrix Z in the QZ decomposition.
Definition: RealQZ.h:132
Matrix< ComplexScalar, ColsAtCompileTime, 1, Options &~RowMajor, MaxColsAtCompileTime, 1 > EigenvalueType
Definition: RealQZ.h:72
Index iterations() const
Returns number of performed QR-like iterations.
Definition: RealQZ.h:177
UnitType abs(const UnitType x) noexcept
Compute absolute value.
Definition: math.h:721
auto sqrt(const UnitType &value) noexcept -> unit_t< square_root< typename units::traits::unit_t_traits< UnitType >::unit_type >, typename units::traits::unit_t_traits< UnitType >::underlying_type, linear_scale >
computes the square root of value
Definition: math.h:483
ComputationInfo
Enum for reporting the status of a computation.
Definition: Constants.h:440
@ Success
Computation was successful.
Definition: Constants.h:442
@ NoConvergence
Iterative procedure did not converge.
Definition: Constants.h:446
constexpr common_t< T1, T2 > max(const T1 x, const T2 y) noexcept
Compile-time pairwise maximum function.
Definition: max.hpp:35
constexpr common_t< T1, T2 > min(const T1 x, const T2 y) noexcept
Compile-time pairwise minimum function.
Definition: min.hpp:35
const Scalar & y
Definition: MathFunctions.h:821
void real_2x2_jacobi_svd(const MatrixType &matrix, Index p, Index q, JacobiRotation< RealScalar > *j_left, JacobiRotation< RealScalar > *j_right)
Definition: RealSvd2x2.h:19
EIGEN_CONSTEXPR Index size(const T &x)
Definition: Meta.h:479
Namespace containing all symbols from the Eigen library.
Definition: MatrixExponential.h:16
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:74
const int Dynamic
This value means that a positive quantity (e.g., a size) is not known at compile-time,...
Definition: Constants.h:22
fc
Definition: illuminance.h:48
G
Definition: magnetic_field_strength.h:52