10 #ifndef EIGEN_REAL_QZ_H 11 #define EIGEN_REAL_QZ_H 57 template<
typename _MatrixType>
class RealQZ 60 typedef _MatrixType MatrixType;
62 RowsAtCompileTime = MatrixType::RowsAtCompileTime,
63 ColsAtCompileTime = MatrixType::ColsAtCompileTime,
64 Options = 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;
86 explicit RealQZ(Index size = RowsAtCompileTime==
Dynamic ? 1 : RowsAtCompileTime) :
93 m_isInitialized(false)
104 RealQZ(
const MatrixType&
A,
const MatrixType&
B,
bool computeQZ =
true) :
105 m_S(A.rows(),A.cols()),
106 m_T(A.rows(),A.cols()),
107 m_Q(A.rows(),A.cols()),
108 m_Z(A.rows(),A.cols()),
109 m_workspace(A.rows()*2),
111 m_isInitialized(false) {
120 eigen_assert(m_isInitialized &&
"RealQZ is not initialized.");
121 eigen_assert(m_computeQZ &&
"The matrices Q and Z have not been computed during the QZ decomposition.");
130 eigen_assert(m_isInitialized &&
"RealQZ is not initialized.");
131 eigen_assert(m_computeQZ &&
"The matrices Q and Z have not been computed during the QZ decomposition.");
140 eigen_assert(m_isInitialized &&
"RealQZ is not initialized.");
149 eigen_assert(m_isInitialized &&
"RealQZ is not initialized.");
160 RealQZ&
compute(
const MatrixType&
A,
const MatrixType&
B,
bool computeQZ =
true);
168 eigen_assert(m_isInitialized &&
"RealQZ is not initialized.");
176 eigen_assert(m_isInitialized &&
"RealQZ is not initialized.");
177 return m_global_iter;
185 m_maxIters = maxIters;
191 MatrixType m_S, m_T, m_Q, m_Z;
195 bool m_isInitialized;
197 Scalar m_normOfT, m_normOfS;
205 void hessenbergTriangular();
207 Index findSmallSubdiagEntry(Index iu);
208 Index findSmallDiagEntry(Index f, Index l);
209 void splitOffTwoRows(Index i);
210 void pushDownZero(Index z, Index f, Index l);
211 void step(Index f, Index l, Index iter);
216 template<
typename MatrixType>
220 const Index dim = m_S.cols();
225 m_T.template triangularView<StrictlyLower>().setZero();
228 m_S.applyOnTheLeft(m_Q.adjoint());
231 m_Z = MatrixType::Identity(dim,dim);
233 for (
Index j=0; j<=dim-3; j++) {
234 for (
Index i=dim-1; i>=j+2; i--) {
237 if(m_S.coeff(i,j) != 0)
239 G.
makeGivens(m_S.coeff(i-1,j), m_S.coeff(i,j), &m_S.coeffRef(i-1, j));
240 m_S.coeffRef(i,j) = Scalar(0.0);
241 m_S.rightCols(dim-j-1).applyOnTheLeft(i-1,i,G.
adjoint());
242 m_T.rightCols(dim-i+1).applyOnTheLeft(i-1,i,G.
adjoint());
245 m_Q.applyOnTheRight(i-1,i,G);
248 if(m_T.coeff(i,i-1)!=Scalar(0))
250 G.
makeGivens(m_T.coeff(i,i), m_T.coeff(i,i-1), &m_T.coeffRef(i,i));
251 m_T.coeffRef(i,i-1) = Scalar(0.0);
252 m_S.applyOnTheRight(i,i-1,G);
253 m_T.topRows(i).applyOnTheRight(i,i-1,G);
256 m_Z.applyOnTheLeft(i,i-1,G.
adjoint());
263 template<
typename MatrixType>
266 const Index size = m_S.cols();
267 m_normOfS = Scalar(0.0);
268 m_normOfT = Scalar(0.0);
269 for (
Index j = 0; j < size; ++j)
271 m_normOfS += m_S.col(j).segment(0, (std::min)(size,j+2)).cwiseAbs().sum();
272 m_normOfT += m_T.row(j).segment(j, size - j).cwiseAbs().sum();
278 template<
typename MatrixType>
285 Scalar s = abs(m_S.coeff(res-1,res-1)) + abs(m_S.coeff(res,res));
286 if (s == Scalar(0.0))
296 template<
typename MatrixType>
310 template<
typename MatrixType>
315 const Index dim=m_S.cols();
316 if (abs(m_S.coeff(i+1,i))==Scalar(0))
318 Index j = findSmallDiagEntry(i,i+1);
322 Matrix2s STi = m_T.template block<2,2>(i,i).
template triangularView<Upper>().
323 template solve<OnTheRight>(m_S.template block<2,2>(i,i));
324 Scalar p = Scalar(0.5)*(STi(0,0)-STi(1,1));
325 Scalar q = p*p + STi(1,0)*STi(0,1);
336 m_S.rightCols(dim-i).applyOnTheLeft(i,i+1,G.
adjoint());
337 m_T.rightCols(dim-i).applyOnTheLeft(i,i+1,G.
adjoint());
340 m_Q.applyOnTheRight(i,i+1,G);
342 G.
makeGivens(m_T.coeff(i+1,i+1), m_T.coeff(i+1,i));
343 m_S.topRows(i+2).applyOnTheRight(i+1,i,G);
344 m_T.topRows(i+2).applyOnTheRight(i+1,i,G);
347 m_Z.applyOnTheLeft(i+1,i,G.
adjoint());
349 m_S.coeffRef(i+1,i) = Scalar(0.0);
350 m_T.coeffRef(i+1,i) = Scalar(0.0);
355 pushDownZero(j,i,i+1);
360 template<
typename MatrixType>
364 const Index dim = m_S.cols();
365 for (
Index zz=z; zz<l; zz++)
368 Index firstColS = zz>f ? (zz-1) : zz;
369 G.
makeGivens(m_T.coeff(zz, zz+1), m_T.coeff(zz+1, zz+1));
370 m_S.rightCols(dim-firstColS).applyOnTheLeft(zz,zz+1,G.
adjoint());
371 m_T.rightCols(dim-zz).applyOnTheLeft(zz,zz+1,G.
adjoint());
372 m_T.coeffRef(zz+1,zz+1) = Scalar(0.0);
375 m_Q.applyOnTheRight(zz,zz+1,G);
379 G.
makeGivens(m_S.coeff(zz+1, zz), m_S.coeff(zz+1,zz-1));
380 m_S.topRows(zz+2).applyOnTheRight(zz, zz-1,G);
381 m_T.topRows(zz+1).applyOnTheRight(zz, zz-1,G);
382 m_S.coeffRef(zz+1,zz-1) = Scalar(0.0);
385 m_Z.applyOnTheLeft(zz,zz-1,G.
adjoint());
389 G.
makeGivens(m_S.coeff(l,l), m_S.coeff(l,l-1));
390 m_S.applyOnTheRight(l,l-1,G);
391 m_T.applyOnTheRight(l,l-1,G);
392 m_S.coeffRef(l,l-1)=Scalar(0.0);
395 m_Z.applyOnTheLeft(l,l-1,G.
adjoint());
399 template<
typename MatrixType>
403 const Index dim = m_S.cols();
411 a11=m_S.coeff(f+0,f+0), a12=m_S.coeff(f+0,f+1),
412 a21=m_S.coeff(f+1,f+0), a22=m_S.coeff(f+1,f+1), a32=m_S.coeff(f+2,f+1),
413 b12=m_T.coeff(f+0,f+1),
414 b11i=Scalar(1.0)/m_T.coeff(f+0,f+0),
415 b22i=Scalar(1.0)/m_T.coeff(f+1,f+1),
416 a87=m_S.coeff(l-1,l-2),
417 a98=m_S.coeff(l-0,l-1),
418 b77i=Scalar(1.0)/m_T.coeff(l-2,l-2),
419 b88i=Scalar(1.0)/m_T.coeff(l-1,l-1);
420 Scalar ss = abs(a87*b77i) + abs(a98*b88i),
421 lpl = Scalar(1.5)*ss,
423 x = ll + a11*a11*b11i*b11i - lpl*a11*b11i + a12*a21*b11i*b22i
424 - a11*a21*b12*b11i*b11i*b22i;
425 y = a11*a21*b11i*b11i - lpl*a21*b11i + a21*a22*b11i*b22i
426 - a21*a21*b12*b11i*b11i*b22i;
427 z = a21*a32*b11i*b22i;
432 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) /
433 (m_T.coeff(l-1,l-1)*m_T.coeff(l,l));
434 y = m_S.coeff(f+1,f)/m_T.coeff(f,f);
437 else if (iter>23 && !(iter%8))
440 x = internal::random<Scalar>(-1.0,1.0);
441 y = internal::random<Scalar>(-1.0,1.0);
442 z = internal::random<Scalar>(-1.0,1.0);
453 a11 = m_S.coeff(f,f), a12 = m_S.coeff(f,f+1),
454 a21 = m_S.coeff(f+1,f), a22 = m_S.coeff(f+1,f+1),
455 a32 = m_S.coeff(f+2,f+1),
457 a88 = m_S.coeff(l-1,l-1), a89 = m_S.coeff(l-1,l),
458 a98 = m_S.coeff(l,l-1), a99 = m_S.coeff(l,l),
460 b11 = m_T.coeff(f,f), b12 = m_T.coeff(f,f+1),
461 b22 = m_T.coeff(f+1,f+1),
463 b88 = m_T.coeff(l-1,l-1), b89 = m_T.coeff(l-1,l),
464 b99 = m_T.coeff(l,l);
466 x = ( (a88/b88 - a11/b11)*(a99/b99 - a11/b11) - (a89/b99)*(a98/b88) + (a98/b88)*(b89/b99)*(a11/b11) ) * (b11/a21)
467 + a12/b22 - (a11/b11)*(b12/b22);
468 y = (a22/b22-a11/b11) - (a21/b11)*(b12/b22) - (a88/b88-a11/b11) - (a99/b99-a11/b11) + (a98/b88)*(b89/b99);
474 for (
Index k=f; k<=l-2; k++)
483 hr.makeHouseholderInPlace(tau, beta);
484 essential2 = hr.template bottomRows<2>();
486 m_S.template middleRows<3>(k).rightCols(dim-fc).applyHouseholderOnTheLeft(essential2, tau, m_workspace.
data());
487 m_T.template middleRows<3>(k).rightCols(dim-fc).applyHouseholderOnTheLeft(essential2, tau, m_workspace.
data());
489 m_Q.template middleCols<3>(k).applyHouseholderOnTheRight(essential2, tau, m_workspace.
data());
491 m_S.coeffRef(k+2,k-1) = m_S.coeffRef(k+1,k-1) = Scalar(0.0);
494 hr << m_T.coeff(k+2,k+2),m_T.coeff(k+2,k),m_T.coeff(k+2,k+1);
495 hr.makeHouseholderInPlace(tau, beta);
496 essential2 = hr.template bottomRows<2>();
498 Index lr = (std::min)(k+4,dim);
501 tmp = m_S.template middleCols<2>(k).topRows(lr) * essential2;
502 tmp += m_S.col(k+2).head(lr);
503 m_S.col(k+2).head(lr) -= tau*tmp;
504 m_S.template middleCols<2>(k).topRows(lr) -= (tau*tmp) * essential2.adjoint();
506 tmp = m_T.template middleCols<2>(k).topRows(lr) * essential2;
507 tmp += m_T.col(k+2).head(lr);
508 m_T.col(k+2).head(lr) -= tau*tmp;
509 m_T.template middleCols<2>(k).topRows(lr) -= (tau*tmp) * essential2.adjoint();
515 tmp = essential2.adjoint()*(m_Z.template middleRows<2>(k));
517 m_Z.row(k+2) -= tau*tmp;
518 m_Z.template middleRows<2>(k) -= essential2 * (tau*tmp);
520 m_T.coeffRef(k+2,k) = m_T.coeffRef(k+2,k+1) = Scalar(0.0);
523 G.
makeGivens(m_T.coeff(k+1,k+1), m_T.coeff(k+1,k));
524 m_S.applyOnTheRight(k+1,k,G);
525 m_T.applyOnTheRight(k+1,k,G);
528 m_Z.applyOnTheLeft(k+1,k,G.
adjoint());
529 m_T.coeffRef(k+1,k) = Scalar(0.0);
532 x = m_S.coeff(k+1,k);
533 y = m_S.coeff(k+2,k);
535 z = m_S.coeff(k+3,k);
540 m_S.applyOnTheLeft(l-1,l,G.
adjoint());
541 m_T.applyOnTheLeft(l-1,l,G.
adjoint());
543 m_Q.applyOnTheRight(l-1,l,G);
544 m_S.coeffRef(l,l-2) = Scalar(0.0);
547 G.
makeGivens(m_T.coeff(l,l),m_T.coeff(l,l-1));
548 m_S.applyOnTheRight(l,l-1,G);
549 m_T.applyOnTheRight(l,l-1,G);
551 m_Z.applyOnTheLeft(l,l-1,G.
adjoint());
552 m_T.coeffRef(l,l-1) = Scalar(0.0);
555 template<
typename MatrixType>
559 const Index dim = A_in.cols();
561 eigen_assert (A_in.rows()==dim && A_in.cols()==dim
562 && B_in.rows()==dim && B_in.cols()==dim
563 &&
"Need square matrices of the same dimension");
565 m_isInitialized =
true;
566 m_computeQZ = computeQZ;
567 m_S = A_in; m_T = B_in;
568 m_workspace.
resize(dim*2);
572 hessenbergTriangular();
580 while (l>0 && local_iter<m_maxIters)
582 f = findSmallSubdiagEntry(l);
584 if (f>0) m_S.coeffRef(f,f-1) = Scalar(0.0);
599 Index z = findSmallDiagEntry(f,l);
610 step(f,l, local_iter);
625 for(
Index i=0; i<dim-1; ++i)
627 if(m_S.coeff(i+1, i) != Scalar(0))
630 internal::real_2x2_jacobi_svd(m_T, i, i+1, &j_left, &j_right);
633 m_S.applyOnTheLeft(i,i+1,j_left);
634 m_S.applyOnTheRight(i,i+1,j_right);
635 m_T.applyOnTheLeft(i,i+1,j_left);
636 m_T.applyOnTheRight(i,i+1,j_right);
637 m_T(i+1,i) = m_T(i,i+1) = Scalar(0);
640 m_Q.applyOnTheRight(i,i+1,j_left.
transpose());
641 m_Z.applyOnTheLeft(i,i+1,j_right.
transpose());
654 #endif //EIGEN_REAL_QZ const MatrixType & matrixS() const
Returns matrix S in the QZ decomposition.
Definition: RealQZ.h:139
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar * data() const
Definition: PlainObjectBase.h:249
Eigen::Index Index
Definition: RealQZ.h:70
void makeGivens(const Scalar &p, const Scalar &q, Scalar *z=0)
Makes *this as a Givens rotation G such that applying to the left of the vector yields: ...
Definition: Jacobi.h:149
Index iterations() const
Returns number of performed QR-like iterations.
Definition: RealQZ.h:174
A matrix or vector expression mapping an existing array of data.
Definition: Map.h:88
const MatrixType & matrixQR() const
Definition: HouseholderQR.h:163
RealQZ(Index size=RowsAtCompileTime==Dynamic ? 1 :RowsAtCompileTime)
Default constructor.
Definition: RealQZ.h:86
Namespace containing all symbols from the Eigen library.
Definition: bench_norm.cpp:85
Definition: ForwardDeclarations.h:263
Holds information about the various numeric (i.e.
Definition: NumTraits.h:150
RealQZ & setMaxIterations(Index maxIters)
Sets the maximal number of iterations allowed to converge to one eigenvalue or decouple the problem...
Definition: RealQZ.h:183
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void resize(Index rows, Index cols)
Resizes *this to a rows x cols matrix.
Definition: PlainObjectBase.h:273
RealQZ(const MatrixType &A, const MatrixType &B, bool computeQZ=true)
Constructor; computes real QZ decomposition of given matrices.
Definition: RealQZ.h:104
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:33
HouseholderSequenceType householderQ() const
This method returns an expression of the unitary matrix Q as a sequence of Householder transformation...
Definition: HouseholderQR.h:154
const MatrixType & matrixZ() const
Returns matrix Z in the QZ decomposition.
Definition: RealQZ.h:129
ComputationInfo info() const
Reports whether previous computation was successful.
Definition: RealQZ.h:166
Computation was successful.
Definition: Constants.h:432
const MatrixType & matrixT() const
Returns matrix S in the QZ decomposition.
Definition: RealQZ.h:148
JacobiRotation transpose() const
Returns the transposed transformation.
Definition: Jacobi.h:59
RealQZ & compute(const MatrixType &A, const MatrixType &B, bool computeQZ=true)
Computes QZ decomposition of given matrix.
Definition: RealQZ.h:556
const int Dynamic
This value means that a positive quantity (e.g., a size) is not known at compile-time, and that instead the value is stored in some runtime variable.
Definition: Constants.h:21
ComputationInfo
Enum for reporting the status of a computation.
Definition: Constants.h:430
const MatrixType & matrixQ() const
Returns matrix Q in the QZ decomposition.
Definition: RealQZ.h:119
Iterative procedure did not converge.
Definition: Constants.h:436
JacobiRotation adjoint() const
Returns the adjoint transformation.
Definition: Jacobi.h:62