10 #ifndef EIGEN_REAL_QZ_H
11 #define EIGEN_REAL_QZ_H
59 template<
typename MatrixType_>
class RealQZ
221 template<
typename MatrixType>
225 const Index dim = m_S.cols();
230 m_T.template triangularView<StrictlyLower>().setZero();
233 m_S.applyOnTheLeft(m_Q.adjoint());
236 m_Z = MatrixType::Identity(dim,dim);
244 G.makeGivens(m_S.coeff(
i-1,
j), m_S.coeff(
i,
j), &m_S.coeffRef(
i-1,
j));
246 m_S.rightCols(dim-
j-1).applyOnTheLeft(
i-1,
i,
G.adjoint());
247 m_T.rightCols(dim-
i+1).applyOnTheLeft(
i-1,
i,
G.adjoint());
250 m_Q.applyOnTheRight(
i-1,
i,
G);
255 G.makeGivens(m_T.coeff(
i,
i), m_T.coeff(
i,
i-1), &m_T.coeffRef(
i,
i));
257 m_S.applyOnTheRight(
i,
i-1,
G);
258 m_T.topRows(
i).applyOnTheRight(
i,
i-1,
G);
261 m_Z.applyOnTheLeft(
i,
i-1,
G.adjoint());
268 template<
typename MatrixType>
276 m_normOfS += m_S.col(
j).segment(0, (
std::min)(
size,
j+2)).cwiseAbs().sum();
277 m_normOfT += m_T.row(
j).segment(
j,
size -
j).cwiseAbs().sum();
283 template<
typename MatrixType>
301 template<
typename MatrixType>
315 template<
typename MatrixType>
320 const Index dim=m_S.cols();
323 Index j = findSmallDiagEntry(
i,
i+1);
327 Matrix2s STi = m_T.template block<2,2>(
i,
i).template triangularView<Upper>().
328 template solve<OnTheRight>(m_S.template block<2,2>(
i,
i));
338 G.makeGivens(
p + z, STi(1,0));
340 G.makeGivens(
p - z, STi(1,0));
341 m_S.rightCols(dim-
i).applyOnTheLeft(
i,
i+1,
G.adjoint());
342 m_T.rightCols(dim-
i).applyOnTheLeft(
i,
i+1,
G.adjoint());
345 m_Q.applyOnTheRight(
i,
i+1,
G);
347 G.makeGivens(m_T.coeff(
i+1,
i+1), m_T.coeff(
i+1,
i));
348 m_S.topRows(
i+2).applyOnTheRight(
i+1,
i,
G);
349 m_T.topRows(
i+2).applyOnTheRight(
i+1,
i,
G);
352 m_Z.applyOnTheLeft(
i+1,
i,
G.adjoint());
360 pushDownZero(
j,
i,
i+1);
365 template<
typename MatrixType>
369 const Index dim = m_S.cols();
370 for (
Index zz=z; zz<l; zz++)
373 Index firstColS = zz>f ? (zz-1) : zz;
374 G.makeGivens(m_T.coeff(zz, zz+1), m_T.coeff(zz+1, zz+1));
375 m_S.rightCols(dim-firstColS).applyOnTheLeft(zz,zz+1,
G.adjoint());
376 m_T.rightCols(dim-zz).applyOnTheLeft(zz,zz+1,
G.adjoint());
377 m_T.coeffRef(zz+1,zz+1) =
Scalar(0.0);
380 m_Q.applyOnTheRight(zz,zz+1,
G);
384 G.makeGivens(m_S.coeff(zz+1, zz), m_S.coeff(zz+1,zz-1));
385 m_S.topRows(zz+2).applyOnTheRight(zz, zz-1,
G);
386 m_T.topRows(zz+1).applyOnTheRight(zz, zz-1,
G);
387 m_S.coeffRef(zz+1,zz-1) =
Scalar(0.0);
390 m_Z.applyOnTheLeft(zz,zz-1,
G.adjoint());
394 G.makeGivens(m_S.coeff(l,l), m_S.coeff(l,l-1));
395 m_S.applyOnTheRight(l,l-1,
G);
396 m_T.applyOnTheRight(l,l-1,
G);
397 m_S.coeffRef(l,l-1)=
Scalar(0.0);
400 m_Z.applyOnTheLeft(l,l-1,
G.adjoint());
404 template<
typename MatrixType>
408 const Index dim = m_S.cols();
416 a11=m_S.coeff(f+0,f+0), a12=m_S.coeff(f+0,f+1),
417 a21=m_S.coeff(f+1,f+0), a22=m_S.coeff(f+1,f+1), a32=m_S.coeff(f+2,f+1),
418 b12=m_T.coeff(f+0,f+1),
419 b11i=
Scalar(1.0)/m_T.coeff(f+0,f+0),
420 b22i=
Scalar(1.0)/m_T.coeff(f+1,f+1),
421 a87=m_S.coeff(l-1,l-2),
422 a98=m_S.coeff(l-0,l-1),
423 b77i=
Scalar(1.0)/m_T.coeff(l-2,l-2),
424 b88i=
Scalar(1.0)/m_T.coeff(l-1,l-1);
428 x = ll + a11*a11*b11i*b11i - lpl*a11*b11i + a12*a21*b11i*b22i
429 - a11*a21*b12*b11i*b11i*b22i;
430 y = a11*a21*b11i*b11i - lpl*a21*b11i + a21*a22*b11i*b22i
431 - a21*a21*b12*b11i*b11i*b22i;
432 z = a21*a32*b11i*b22i;
437 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) /
438 (m_T.coeff(l-1,l-1)*m_T.coeff(l,l));
439 y = m_S.coeff(f+1,f)/m_T.coeff(f,f);
442 else if (iter>23 && !(iter%8))
445 x = internal::random<Scalar>(-1.0,1.0);
446 y = internal::random<Scalar>(-1.0,1.0);
447 z = internal::random<Scalar>(-1.0,1.0);
458 a11 = m_S.coeff(f,f), a12 = m_S.coeff(f,f+1),
459 a21 = m_S.coeff(f+1,f), a22 = m_S.coeff(f+1,f+1),
460 a32 = m_S.coeff(f+2,f+1),
462 a88 = m_S.coeff(l-1,l-1), a89 = m_S.coeff(l-1,l),
463 a98 = m_S.coeff(l,l-1), a99 = m_S.coeff(l,l),
465 b11 = m_T.coeff(f,f), b12 = m_T.coeff(f,f+1),
466 b22 = m_T.coeff(f+1,f+1),
468 b88 = m_T.coeff(l-1,l-1), b89 = m_T.coeff(l-1,l),
469 b99 = m_T.coeff(l,l);
471 x = ( (a88/b88 - a11/b11)*(a99/b99 - a11/b11) - (a89/b99)*(a98/b88) + (a98/b88)*(b89/b99)*(a11/b11) ) * (b11/a21)
472 + a12/b22 - (a11/b11)*(b12/b22);
473 y = (a22/b22-a11/b11) - (a21/b11)*(b12/b22) - (a88/b88-a11/b11) - (a99/b99-a11/b11) + (a98/b88)*(b89/b99);
479 for (
Index k=f; k<=l-2; k++)
488 hr.makeHouseholderInPlace(tau, beta);
489 essential2 = hr.template bottomRows<2>();
491 m_S.template middleRows<3>(k).rightCols(dim-fc).applyHouseholderOnTheLeft(essential2, tau, m_workspace.
data());
492 m_T.template middleRows<3>(k).rightCols(dim-fc).applyHouseholderOnTheLeft(essential2, tau, m_workspace.
data());
494 m_Q.template middleCols<3>(k).applyHouseholderOnTheRight(essential2, tau, m_workspace.
data());
496 m_S.coeffRef(k+2,k-1) = m_S.coeffRef(k+1,k-1) =
Scalar(0.0);
499 hr << m_T.
coeff(k+2,k+2),m_T.coeff(k+2,k),m_T.coeff(k+2,k+1);
500 hr.makeHouseholderInPlace(tau, beta);
501 essential2 = hr.template bottomRows<2>();
506 tmp = m_S.template middleCols<2>(k).topRows(lr) * essential2;
507 tmp += m_S.col(k+2).head(lr);
508 m_S.col(k+2).head(lr) -= tau*tmp;
509 m_S.template middleCols<2>(k).topRows(lr) -= (tau*tmp) * essential2.
adjoint();
511 tmp = m_T.template middleCols<2>(k).topRows(lr) * essential2;
512 tmp += m_T.col(k+2).head(lr);
513 m_T.col(k+2).head(lr) -= tau*tmp;
514 m_T.template middleCols<2>(k).topRows(lr) -= (tau*tmp) * essential2.
adjoint();
520 tmp = essential2.
adjoint()*(m_Z.template middleRows<2>(k));
522 m_Z.row(k+2) -= tau*tmp;
523 m_Z.template middleRows<2>(k) -= essential2 * (tau*tmp);
528 G.makeGivens(m_T.coeff(k+1,k+1), m_T.coeff(k+1,k));
529 m_S.applyOnTheRight(k+1,k,
G);
530 m_T.applyOnTheRight(k+1,k,
G);
533 m_Z.applyOnTheLeft(k+1,k,
G.adjoint());
534 m_T.coeffRef(k+1,k) =
Scalar(0.0);
537 x = m_S.coeff(k+1,k);
538 y = m_S.coeff(k+2,k);
540 z = m_S.coeff(k+3,k);
545 m_S.applyOnTheLeft(l-1,l,
G.adjoint());
546 m_T.applyOnTheLeft(l-1,l,
G.adjoint());
548 m_Q.applyOnTheRight(l-1,l,
G);
549 m_S.coeffRef(l,l-2) =
Scalar(0.0);
552 G.makeGivens(m_T.coeff(l,l),m_T.coeff(l,l-1));
553 m_S.applyOnTheRight(l,l-1,
G);
554 m_T.applyOnTheRight(l,l-1,
G);
556 m_Z.applyOnTheLeft(l,l-1,
G.adjoint());
557 m_T.coeffRef(l,l-1) =
Scalar(0.0);
560 template<
typename MatrixType>
564 const Index dim = A_in.cols();
567 && B_in.rows()==dim && B_in.cols()==dim
568 &&
"Need square matrices of the same dimension");
570 m_isInitialized =
true;
571 m_computeQZ = computeQZ;
572 m_S = A_in; m_T = B_in;
573 m_workspace.resize(dim*2);
577 hessenbergTriangular();
585 while (l>0 && local_iter<m_maxIters)
587 f = findSmallSubdiagEntry(l);
589 if (f>0) m_S.coeffRef(f,f-1) =
Scalar(0.0);
604 Index z = findSmallDiagEntry(f,l);
615 step(f,l, local_iter);
638 m_S.applyOnTheLeft(
i,
i+1,j_left);
639 m_S.applyOnTheRight(
i,
i+1,j_right);
640 m_T.applyOnTheLeft(
i,
i+1,j_left);
641 m_T.applyOnTheRight(
i,
i+1,j_right);
const AbsReturnType abs() const
const SqrtReturnType sqrt() const
JacobiRotation< float > G
cout<< "Here is the matrix m:"<< endl<< m<< endl;Matrix< ptrdiff_t, 3, 1 > res
internal::traits< Derived >::Scalar Scalar
Householder QR decomposition of a matrix.
HouseholderSequenceType householderQ() const
const MatrixType & matrixQR() const
Rotation given by a cosine-sine pair.
JacobiRotation transpose() const
A matrix or vector expression mapping an existing array of data.
const AdjointReturnType adjoint() const
constexpr const Scalar & coeff(Index rowId, Index colId) const
const Scalar * data() const
constexpr Scalar & coeffRef(Index rowId, Index colId)
Performs a real QZ decomposition of a pair of square matrices.
RealQZ & compute(const MatrixType &A, const MatrixType &B, bool computeQZ=true)
Computes QZ decomposition of given matrix.
Index iterations() const
Returns number of performed QR-like iterations.
Matrix< Scalar, Dynamic, 1 > m_workspace
Index findSmallSubdiagEntry(Index iu)
const MatrixType & matrixQ() const
Returns matrix Q in the QZ decomposition.
Matrix< Scalar, 2, 1 > Vector2s
RealQZ & setMaxIterations(Index maxIters)
RealQZ(const MatrixType &A, const MatrixType &B, bool computeQZ=true)
Constructor; computes real QZ decomposition of given matrices.
ComputationInfo info() const
Reports whether previous computation was successful.
void splitOffTwoRows(Index i)
const MatrixType & matrixZ() const
Returns matrix Z in the QZ decomposition.
Matrix< Scalar, 3, 1 > Vector3s
const MatrixType & matrixT() const
Returns matrix S in the QZ decomposition.
MatrixType::Scalar Scalar
void step(Index f, Index l, Index iter)
Matrix< Scalar, ColsAtCompileTime, 1, Options &~RowMajor, MaxColsAtCompileTime, 1 > ColumnVectorType
RealQZ(Index size=RowsAtCompileTime==Dynamic ? 1 :RowsAtCompileTime)
Default constructor.
void hessenbergTriangular()
Matrix< ComplexScalar, ColsAtCompileTime, 1, Options &~RowMajor, MaxColsAtCompileTime, 1 > EigenvalueType
const MatrixType & matrixS() const
Returns matrix S in the QZ decomposition.
JacobiRotation< Scalar > JRs
Matrix< Scalar, 2, 2 > Matrix2s
std::complex< typename NumTraits< Scalar >::Real > ComplexScalar
Index findSmallDiagEntry(Index f, Index l)
void pushDownZero(Index z, Index f, Index l)
bfloat16() max(const bfloat16 &a, const bfloat16 &b)
bfloat16() min(const bfloat16 &a, const bfloat16 &b)
void real_2x2_jacobi_svd(const MatrixType &matrix, Index p, Index q, JacobiRotation< RealScalar > *j_left, JacobiRotation< RealScalar > *j_right)
bool is_exactly_zero(const X &x)
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Holds information about the various numeric (i.e. scalar) types allowed by Eigen.