Eigen  5.0.1-dev+master
 
Loading...
Searching...
No Matches
ComplexQZ.h
1// This file is part of Eigen, a lightweight C++ template library
2// for linear algebra.
3//
4// Copyright (C) 2012 Alexey Korepanov
5// Copyright (C) 2025 Ludwig Striet <ludwig.striet@mathematik.uni-freiburg.de>
6//
7// This Source Code Form is subject to the terms of the
8// Mozilla Public License v. 2.0. If a copy of the MPL
9// was not distributed with this file, You can obtain one at
10// https://mozilla.org/MPL/2.0/.
11//
12// Derived from: Eigen/src/Eigenvalues/RealQZ.h
13// SPDX-License-Identifier: MPL-2.0
14
15#ifndef EIGEN_COMPLEX_QZ_H_
16#define EIGEN_COMPLEX_QZ_H_
17
18// IWYU pragma: private
19#include "./InternalHeaderCheck.h"
20
48
49namespace Eigen {
50
51template <typename MatrixType_>
52class ComplexQZ {
53 public:
54 using MatrixType = MatrixType_;
55 using Scalar = typename MatrixType_::Scalar;
56 using RealScalar = typename MatrixType_::RealScalar;
57
58 enum {
59 RowsAtCompileTime = MatrixType::RowsAtCompileTime,
60 ColsAtCompileTime = MatrixType::ColsAtCompileTime,
61 Options = internal::plain_object_options<MatrixType>::value,
62 MaxRowsAtCompileTime = MatrixType::MaxRowsAtCompileTime,
63 MaxColsAtCompileTime = MatrixType::MaxColsAtCompileTime
64 };
65
68 using PlainMatrixType =
69 Matrix<Scalar, RowsAtCompileTime, ColsAtCompileTime, Options, MaxRowsAtCompileTime, MaxColsAtCompileTime>;
70
71 using Vec = Matrix<Scalar, Dynamic, 1>;
72 using Vec2 = Matrix<Scalar, 2, 1>;
73 using Vec3 = Matrix<Scalar, 3, 1>;
74 using Row2 = Matrix<Scalar, 1, 2>;
75 using Mat2 = Matrix<Scalar, 2, 2>;
76
81 const PlainMatrixType& matrixQ() const {
82 eigen_assert(m_isInitialized && "ComplexQZ is not initialized.");
83 eigen_assert(m_computeQZ && "The matrices Q and Z have not been computed during the QZ decomposition.");
84 return m_Q;
85 }
86
91 const PlainMatrixType& matrixZ() const {
92 eigen_assert(m_isInitialized && "ComplexQZ is not initialized.");
93 eigen_assert(m_computeQZ && "The matrices Q and Z have not been computed during the QZ decomposition.");
94 return m_Z;
95 }
96
101 const MatrixType& matrixS() const {
102 eigen_assert(m_isInitialized && "ComplexQZ is not initialized.");
103 return m_S;
104 }
105
110 const MatrixType& matrixT() const {
111 eigen_assert(m_isInitialized && "ComplexQZ is not initialized.");
112 return m_T;
113 }
114
123 ComplexQZ(Index n, bool computeQZ = true, unsigned int maxIters = 400)
124 : m_n(n),
125 m_maxIters(maxIters),
126 m_computeQZ(computeQZ),
127 m_S(n, n),
128 m_T(n, n),
129 m_Q(computeQZ ? n : (MatrixType::RowsAtCompileTime == Eigen::Dynamic ? 0 : MatrixType::RowsAtCompileTime),
130 computeQZ ? n : (MatrixType::ColsAtCompileTime == Eigen::Dynamic ? 0 : MatrixType::ColsAtCompileTime)),
131 m_Z(computeQZ ? n : (MatrixType::RowsAtCompileTime == Eigen::Dynamic ? 0 : MatrixType::RowsAtCompileTime),
132 computeQZ ? n : (MatrixType::ColsAtCompileTime == Eigen::Dynamic ? 0 : MatrixType::ColsAtCompileTime)),
133 m_ws(2 * n),
134 m_hCoeffs(n) {}
135
147 template <typename InputTypeA, typename InputTypeB>
148 ComplexQZ(const EigenBase<InputTypeA>& A, const EigenBase<InputTypeB>& B, bool computeQZ = true,
149 unsigned int maxIters = 400)
150 : m_n(A.rows()),
151 m_maxIters(maxIters),
152 m_computeQZ(computeQZ),
153 m_S(A.derived()),
154 m_T(B.derived()),
155 m_Q(computeQZ ? m_n : (MatrixType::RowsAtCompileTime == Eigen::Dynamic ? 0 : MatrixType::RowsAtCompileTime),
156 computeQZ ? m_n : (MatrixType::ColsAtCompileTime == Eigen::Dynamic ? 0 : MatrixType::ColsAtCompileTime)),
157 m_Z(computeQZ ? m_n : (MatrixType::RowsAtCompileTime == Eigen::Dynamic ? 0 : MatrixType::RowsAtCompileTime),
158 computeQZ ? m_n : (MatrixType::ColsAtCompileTime == Eigen::Dynamic ? 0 : MatrixType::ColsAtCompileTime)),
159 m_ws(2 * m_n),
160 m_hCoeffs(m_n) {
161 computeInPlace(computeQZ);
162 }
163
175 template <typename InputTypeA, typename InputTypeB>
176 ComplexQZ(EigenBase<InputTypeA>& A, EigenBase<InputTypeB>& B, bool computeQZ = true, unsigned int maxIters = 400)
177 : m_n(A.rows()),
178 m_maxIters(maxIters),
179 m_computeQZ(computeQZ),
180 m_S(A.derived()),
181 m_T(B.derived()),
182 m_Q(computeQZ ? m_n : (MatrixType::RowsAtCompileTime == Eigen::Dynamic ? 0 : MatrixType::RowsAtCompileTime),
183 computeQZ ? m_n : (MatrixType::ColsAtCompileTime == Eigen::Dynamic ? 0 : MatrixType::ColsAtCompileTime)),
184 m_Z(computeQZ ? m_n : (MatrixType::RowsAtCompileTime == Eigen::Dynamic ? 0 : MatrixType::RowsAtCompileTime),
185 computeQZ ? m_n : (MatrixType::ColsAtCompileTime == Eigen::Dynamic ? 0 : MatrixType::ColsAtCompileTime)),
186 m_ws(2 * m_n),
187 m_hCoeffs(m_n) {
188 computeInPlace(computeQZ);
189 }
190
197 template <typename InputTypeA, typename InputTypeB>
198 void compute(const EigenBase<InputTypeA>& A, const EigenBase<InputTypeB>& B, bool computeQZ = true);
199
206 template <typename SparseMatrixType_>
207 void computeSparse(const SparseMatrixType_& A, const SparseMatrixType_& B, bool computeQZ = true);
208
217 ComputationInfo info() const {
218 eigen_assert(m_isInitialized && "ComplexQZ is not initialized.");
219 return m_info;
220 }
221
224 unsigned int iterations() const {
225 eigen_assert(m_isInitialized && "ComplexQZ is not initialized.");
226 return m_global_iter;
227 }
228
229 private:
230 Index m_n;
231 const unsigned int m_maxIters;
232 unsigned int m_global_iter = 0;
233 bool m_isInitialized = false;
234 bool m_computeQZ;
235 ComputationInfo m_info = InvalidInput;
236 MatrixType m_S, m_T;
237 PlainMatrixType m_Q, m_Z;
238 RealScalar m_normOfT, m_normOfS;
239 Vec m_ws;
240 Vec m_hCoeffs;
241
242 // Test if a Scalar is 0 up to a certain tolerance
243 static bool is_negligible(const Scalar x, const RealScalar tol = NumTraits<RealScalar>::epsilon()) {
244 return numext::abs(x) <= tol;
245 }
246
247 void do_QZ_step(Index f, Index l, unsigned int iter);
248
249 Vec3 shiftColumn(Index p, Index l, unsigned int iter) const;
250
251 JacobiRotation<Scalar> computeZk2(const Row2& b);
252
253 void computeInPlace(bool computeQZ);
254
255 // This is basically taken from Eigen3::RealQZ
256 void hessenbergTriangular();
257
258 // This function can be called when m_Q and m_Z are initialized and m_S, m_T
259 // are in hessenberg-triangular form
260 void reduceHessenbergTriangular();
261
262 void computeNorms();
263
264 Index findSmallSubdiagEntry(Index l);
265 Index findSmallDiagEntry(Index f, Index l);
266
267 void push_down_zero_ST(Index k, Index l);
268
269 void reduceDiagonal2x2block(Index i);
270};
271
272template <typename MatrixType_>
273template <typename InputTypeA, typename InputTypeB>
274void ComplexQZ<MatrixType_>::compute(const EigenBase<InputTypeA>& A, const EigenBase<InputTypeB>& B, bool computeQZ) {
275 EIGEN_INTERNAL_ASSERT_SQUARE_MSG(A, "A is not a square matrix");
276 EIGEN_INTERNAL_ASSERT_SAME_SHAPE_MSG(A, B, "B is not a square matrix or B is not of the same size as A");
277 // Copy A and B, these will be the matrices on which we operate later
278 m_S = A.derived();
279 m_T = B.derived();
280 computeInPlace(computeQZ);
281}
282
284template <typename MatrixType_>
285void ComplexQZ<MatrixType_>::computeInPlace(bool computeQZ) {
286 m_computeQZ = computeQZ;
287 m_n = m_S.rows();
288
289 EIGEN_INTERNAL_ASSERT_SQUARE_MSG(m_S, "A is not a square matrix");
290 EIGEN_INTERNAL_ASSERT_SAME_SHAPE_MSG(m_S, m_T, "B is not a square matrix or B is not of the same size as A");
291
292 m_isInitialized = true;
293 m_global_iter = 0;
294 m_info = Success;
295
296 // Scale S and T separately by powers of two near their largest coefficients, so that the iteration neither overflows
297 // nor underflows. The scaling is exact: it leaves Q and Z unchanged and is undone on S and T. The coefficients are
298 // measured by their largest real or imaginary part, propagating NaN: the modulus of a finite coefficient can
299 // overflow, which would leave the pencil unscaled.
300 using Scaling = internal::safe_scaling<RealScalar>;
301 const RealScalar maxS = Scaling::max_abs_component(m_S);
302 const RealScalar maxT = Scaling::max_abs_component(m_T);
303 const internal::safe_scaling_factors<RealScalar> factorsS = Scaling::scale_in_place(m_S, maxS);
304 const internal::safe_scaling_factors<RealScalar> factorsT = Scaling::scale_in_place(m_T, maxT);
305
306 // This will initialize m_Q and m_Z and bring m_S, m_T to hessenberg-triangular form
307 hessenbergTriangular();
308
309 // We assume that we already have that S is upper-Hessenberg and T is
310 // upper-triangular. This is what the hessenbergTriangular(...) method does
311 reduceHessenbergTriangular();
312
313 // Entries the iteration annihilated or declared negligible keep rounding residues, measured at <= 2 eps max|S| and
314 // 2 eps max|T| (the reflectors and rotations are applied to whole rows and columns). When it converged, drop them, so
315 // that S and T are exactly upper triangular as documented; Q S Z changes by the rounding error of the product. When
316 // it did not, the subdiagonal of S is significant.
317 if (m_info == Success) {
318 m_S.template triangularView<StrictlyLower>().setZero();
319 m_T.template triangularView<StrictlyLower>().setZero();
320 }
321
322 Scaling::unscale_in_place(m_S, maxS, factorsS);
323 Scaling::unscale_in_place(m_T, maxT, factorsT);
324 // A non-finite A or B is left unscaled and can still leave S and T finite: an infinite S(k, k-1) passes
325 // |S(k, k-1)| <= eps (|S(k-1, k-1)| + |S(k, k)|) where that sum is infinite, or eps m_normOfS = inf where it is 0,
326 // and is set to 0.
327 const bool finiteInput = (numext::isfinite)(maxS) && (numext::isfinite)(maxT);
328 // Where Q^* A Z or Q^* B Z is not representable, unscaling overflows; Success has to mean that S and T are usable.
329 if (m_info == Success && !(finiteInput && m_S.allFinite() && m_T.allFinite())) m_info = NumericalIssue;
330}
331
332// This is basically taken from Eigen3::RealQZ
333template <typename MatrixType_>
335 // Perform the QR decomposition of T in place: T holds R above the Householder vectors Q is formed from
336 m_ws.resize(2 * m_n);
337 m_hCoeffs.resize(m_n);
338 internal::householder_qr_inplace_blocked<MatrixType, Vec>::run(m_T, m_hCoeffs, 48, m_ws.data());
339 Map<Vec> workspace(m_ws.data(), m_n);
340 const auto householderQ = householderSequence(m_T, m_hCoeffs.conjugate());
341 if (m_computeQZ) householderQ.evalTo(m_Q, workspace);
342
343 // overwrite S with Q* x S
344 householderQ.adjoint().applyThisOnTheLeft(m_S, workspace);
345
346 m_T.template triangularView<StrictlyLower>().setZero();
347
348 if (m_computeQZ) m_Z = PlainMatrixType::Identity(m_n, m_n);
349
350 // reduce S to upper Hessenberg with Givens rotations
351 for (Index j = 0; j <= m_n - 3; j++) {
352 for (Index i = m_n - 1; i >= j + 2; i--) {
353 JacobiRotation<Scalar> G;
354 // delete S(i,j)
355 if (!numext::is_exactly_zero(m_S.coeff(i, j))) {
356 G.makeGivens(m_S.coeff(i - 1, j), m_S.coeff(i, j), &m_S.coeffRef(i - 1, j));
357 m_S.coeffRef(i, j) = Scalar(0);
358 m_T.rightCols(m_n - i + 1).applyOnTheLeft(i - 1, i, G.adjoint());
359 m_S.rightCols(m_n - j - 1).applyOnTheLeft(i - 1, i, G.adjoint());
360 // This is what we want to achieve
361 if (!is_negligible(m_S(i, j)))
363 else
364 m_S(i, j) = Scalar(0);
365 // update Q
366 if (m_computeQZ) m_Q.applyOnTheRight(i - 1, i, G);
367 }
368
369 if (!numext::is_exactly_zero(m_T.coeff(i, i - 1))) {
370 // Compute rotation and update matrix T
371 G.makeGivens(m_T.coeff(i, i), m_T.coeff(i, i - 1), &m_T.coeffRef(i, i));
372 m_T.topRows(i).applyOnTheRight(i - 1, i, G.adjoint());
373 m_T.coeffRef(i, i - 1) = Scalar(0);
374 // Update matrix S
375 m_S.applyOnTheRight(i - 1, i, G.adjoint());
376 // update Z
377 if (m_computeQZ) m_Z.applyOnTheLeft(i - 1, i, G);
378 }
379 }
380 }
381}
382
383template <typename MatrixType>
384template <typename SparseMatrixType_>
385void ComplexQZ<MatrixType>::computeSparse(const SparseMatrixType_& A, const SparseMatrixType_& B, bool computeQZ) {
386 EIGEN_INTERNAL_ASSERT_SQUARE_MSG(A, "A is not a square matrix");
387 EIGEN_INTERNAL_ASSERT_SAME_SHAPE_MSG(A, B, "B is not a square matrix or B is not of the same size as A");
388 // S, T, Q and Z are dense and the iteration costs O(n^3), so a sparse QR of B would save no order of work. The dense
389 // reduction is also backward stable where SparseQR's reflectors are not (they form sums of squares unscaled), and it
390 // does not drop a column of B that holds a NaN.
391 m_S = A.toDense();
392 m_T = B.toDense();
393 computeInPlace(computeQZ);
394}
395
396template <typename MatrixType_>
398 m_ws.resize(2 * m_n);
399 Index l = m_n - 1, f;
400 unsigned int local_iter = 0;
401 computeNorms();
402
403 while (l > 0) {
404 f = findSmallSubdiagEntry(l);
405
406 // Subdiag entry is small -> can be safely set to 0
407 if (f > 0) {
408 m_S.coeffRef(f, f - 1) = Scalar(0);
409 }
410 if (f == l) { // One root found
411 l--;
412 local_iter = 0;
413 } else if (f == l - 1) { // Two roots found
414 // We found an undesired non-zero at (f+1,f) in S and eliminate it immediately
415 reduceDiagonal2x2block(f);
416 l -= 2;
417 local_iter = 0;
418 } else {
419 // The limit is checked before a step rather than before the deflation test, so a deflation the last allowed
420 // step produces is still found. Every pass deflates or takes a step, so the loop ends after at most
421 // n (maxIters + 1) passes even if a chase makes no progress.
422 if (local_iter >= m_maxIters) break;
423 Index z = findSmallDiagEntry(f, l);
424 if (z >= f) {
425 push_down_zero_ST(z, l);
426 } else {
427 do_QZ_step(f, l, local_iter);
428 m_global_iter++;
429 }
430 local_iter++;
431 }
432 }
433
434 // Part of the pencil is not reduced: that takes precedence over a NumericalIssue reported before.
435 if (l > 0) m_info = NoConvergence;
436}
437
438template <typename MatrixType_>
439JacobiRotation<typename ComplexQZ<MatrixType_>::Scalar> ComplexQZ<MatrixType_>::computeZk2(const Row2& b) {
440 JacobiRotation<Scalar> J;
441 J.makeGivens(numext::conj(b(1)), numext::conj(b(0)));
442 // S J S = J.transpose() for the exchange matrix S and a real Givens cosine.
443 return J.transpose();
444}
445
447template <typename MatrixType_>
448typename ComplexQZ<MatrixType_>::Vec3 ComplexQZ<MatrixType_>::shiftColumn(Index p, Index l, unsigned int iter) const {
449 const auto a = [p, this](Index i, Index j) { return m_S(p + i - 1, p + j - 1); };
450 const auto b = [p, this](Index i, Index j) { return m_T(p + i - 1, p + j - 1); };
451 const Index m = l - p + 1; // Size of the inner block
452 if (iter > 0 && iter % 10 == 0) {
453 // Break stalled double-shift iterations with a single shift displaced by the trailing subdiagonals.
454 const RealScalar displacement =
455 numext::abs(a(m, m - 1) / b(m - 1, m - 1)) + numext::abs(a(m - 1, m - 2) / b(m - 2, m - 2));
456 const Scalar shift = a(m, m) / b(m, m) + displacement;
457 // (S*T^-1 - shift*I)*e1 has only two nonzero entries.
458 return Vec3(a(1, 1) / b(1, 1) - shift, a(2, 1) / b(1, 1), Scalar(0));
459 }
460 const Scalar W1 = a(m - 1, m - 1) / b(m - 1, m - 1) - a(1, 1) / b(1, 1), W2 = a(m, m) / b(m, m) - a(1, 1) / b(1, 1),
461 W3 = a(m, m - 1) / b(m - 1, m - 1);
462 return Vec3(
463 (W1 * W2 - a(m - 1, m) / b(m, m) * W3 + W3 * b(m - 1, m) / b(m, m) * a(1, 1) / b(1, 1)) * b(1, 1) / a(2, 1) +
464 a(1, 2) / b(2, 2) - a(1, 1) / b(1, 1) * b(1, 2) / b(2, 2),
465 (a(2, 2) / b(2, 2) - a(1, 1) / b(1, 1)) - a(2, 1) / b(1, 1) * b(1, 2) / b(2, 2) - W1 - W2 +
466 W3 * (b(m - 1, m) / b(m, m)),
467 a(3, 2) / b(2, 2));
468}
469
471template <typename MatrixType_>
472void ComplexQZ<MatrixType_>::do_QZ_step(Index f, Index l, unsigned int iter) {
473 // Start at the lowest row p > f where the bulge v spreads S(p, p-1) only into negligible entries (the test of
474 // EISPACK's hqr and LAPACK's xLAHQR, applied to S, with v scaled to |v(0)| + |v(1)| + |v(2)| = 1; rows where that
475 // sum is 0 or not finite are skipped):
476 // |S(p, p-1)| (|v(1)| + |v(2)|) <= eps |v(0)| (|S(p-1, p-1)| + |S(p, p)| + |S(p+1, p+1)|).
477 // From row f the bulge is proportional to S(f+1, f), and a tiny but not negligible entry, e.g. next to zero
478 // eigenvalues, can stall the iteration. A block of three rows has no such p, as in xLAHQR.
479 Index p = f;
480 Vec3 X;
481 for (Index j = l - 2; j > f; --j) {
482 X = shiftColumn(j, l, iter);
483 const RealScalar scale = numext::abs(X(0)) + numext::abs(X(1)) + numext::abs(X(2));
484 if (!(numext::isfinite)(scale) || scale == RealScalar(0)) continue;
485 X /= scale;
486 const RealScalar diagonal =
487 numext::abs(m_S(j - 1, j - 1)) + numext::abs(m_S(j, j)) + numext::abs(m_S(j + 1, j + 1));
488 if (numext::abs(m_S(j, j - 1)) * (numext::abs(X(1)) + numext::abs(X(2))) <=
489 NumTraits<RealScalar>::epsilon() * numext::abs(X(0)) * diagonal) {
490 p = j;
491 break;
492 }
493 }
494 if (p == f) X = shiftColumn(f, l, iter);
495 const Index m = l - p + 1; // Size of the inner block
496 Scalar x = X(0), y = X(1), z = X(2);
497 const PermutationMatrix<3, 3, int> S3(Vector3i(2, 0, 1));
498 for (Index k = p; k < p + m - 2; k++) {
499 X << x, y, z;
500 Vec2 ess;
501 Scalar tau;
502 RealScalar beta;
503 X.makeHouseholder(ess, tau, beta);
504 // The permutations are needed because the makeHouseHolder-method computes
505 // the householder transformation in a way that the vector is reflected to
506 // (1 0 ... 0) instead of (0 ... 0 1)
507 m_S.template middleRows<3>(k)
508 .rightCols((std::min)(m_n, m_n - k + 1))
509 .applyHouseholderOnTheLeft(ess, tau, m_ws.data());
510 if (k > f && k == p) {
511 // The reflector spread the negligible S(p, p-1) into the entries below it, which are dropped.
512 m_S.coeffRef(k + 1, k - 1) = Scalar(0);
513 m_S.coeffRef(k + 2, k - 1) = Scalar(0);
514 }
515 m_T.template middleRows<3>(k).rightCols(m_n - k).applyHouseholderOnTheLeft(ess, tau, m_ws.data());
516 if (m_computeQZ) m_Q.template middleCols<3>(k).applyHouseholderOnTheRight(ess, numext::conj(tau), m_ws.data());
517
518 // Compute Matrix Zk1 s.t. (b(k+2,k) ... b(k+2, k+2)) Zk1 = (0,0,*)
519 Vec3 bprime = (m_T.template block<1, 3>(k + 2, k) * S3).adjoint();
520 bprime.makeHouseholder(ess, tau, beta);
521 auto Sk = m_S.template middleCols<3>(k).topRows((std::min)(k + 4, m_n));
522 auto Tk = m_T.template middleCols<3>(k).topRows((std::min)(k + 3, m_n));
523 // Right multiplication by S3 permutes columns as (2, 0, 1); reverse the swaps for S3^T.
524 Sk.col(0).swap(Sk.col(2));
525 Sk.col(1).swap(Sk.col(2));
526 Sk.applyHouseholderOnTheRight(ess, numext::conj(tau), m_ws.data());
527 Sk.col(1).swap(Sk.col(2));
528 Sk.col(0).swap(Sk.col(2));
529 Tk.col(0).swap(Tk.col(2));
530 Tk.col(1).swap(Tk.col(2));
531 Tk.applyHouseholderOnTheRight(ess, numext::conj(tau), m_ws.data());
532 Tk.col(1).swap(Tk.col(2));
533 Tk.col(0).swap(Tk.col(2));
534 if (m_computeQZ) {
535 auto Zk = m_Z.template middleRows<3>(k);
536 Zk.row(0).swap(Zk.row(2));
537 Zk.row(1).swap(Zk.row(2));
538 Zk.applyHouseholderOnTheLeft(ess, tau, m_ws.data());
539 Zk.row(1).swap(Zk.row(2));
540 Zk.row(0).swap(Zk.row(2));
541 }
542 const JacobiRotation<Scalar> Zk2 = computeZk2(m_T.template block<1, 2>(k + 1, k));
543 m_S.template middleCols<2>(k).topRows((std::min)(k + 4, m_n)).applyOnTheRight(0, 1, Zk2);
544 m_T.template middleCols<2>(k).topRows((std::min)(k + 3, m_n)).applyOnTheRight(0, 1, Zk2);
545
546 if (m_computeQZ) m_Z.template middleRows<2>(k).applyOnTheLeft(0, 1, Zk2.adjoint());
547
548 x = m_S(k + 1, k);
549 y = m_S(k + 2, k);
550 if (k < p + m - 3) {
551 z = m_S(k + 3, k);
552 }
553 }
554
555 // Find a Householder matrix Qn1 s.t. Qn1 (x y)^T = (* 0)
556 JacobiRotation<Scalar> J;
557 J.makeGivens(x, y);
558 m_S.template middleRows<2>(p + m - 2).applyOnTheLeft(0, 1, J.adjoint());
559 m_T.template middleRows<2>(p + m - 2).applyOnTheLeft(0, 1, J.adjoint());
560
561 if (m_computeQZ) m_Q.template middleCols<2>(p + m - 2).applyOnTheRight(0, 1, J);
562
563 // Find a rotation Zn1 s.t. (b(n,n-1) b(n,n)) * Zn1 = (0 *)
564 const JacobiRotation<Scalar> Zn1 = computeZk2(m_T.template block<1, 2>(p + m - 1, p + m - 2));
565 m_S.template middleCols<2>(p + m - 2).applyOnTheRight(0, 1, Zn1);
566 m_T.template middleCols<2>(p + m - 2).applyOnTheRight(0, 1, Zn1);
567
568 if (m_computeQZ) m_Z.template middleRows<2>(p + m - 2).applyOnTheLeft(0, 1, Zn1.adjoint());
569}
570
572template <typename MatrixType_>
574 // We have found a non-zero on the subdiagonal and want to eliminate it
575 Mat2 Si = m_S.template block<2, 2>(i, i), Ti = m_T.template block<2, 2>(i, i);
576 const RealScalar tolT = m_normOfT * NumTraits<RealScalar>::epsilon();
577 if (is_negligible(Ti(0, 0), tolT)) {
579 G.makeGivens(m_S(i, i), m_S(i + 1, i));
580 m_S.applyOnTheLeft(i, i + 1, G.adjoint());
581 m_T.applyOnTheLeft(i, i + 1, G.adjoint());
582
583 if (m_computeQZ) m_Q.applyOnTheRight(i, i + 1, G);
584
585 } else if (is_negligible(Ti(1, 1), tolT)) {
587 G.makeGivens(m_S(i + 1, i + 1), m_S(i + 1, i));
588 m_S.applyOnTheRight(i, i + 1, G.adjoint());
589 m_T.applyOnTheRight(i, i + 1, G.adjoint());
590 if (m_computeQZ) m_Z.applyOnTheLeft(i, i + 1, G);
591 } else {
592 Scalar mu = Si(0, 0) / Ti(0, 0);
593 Scalar a12_bar = Si(0, 1) - mu * Ti(0, 1);
594 Scalar a22_bar = Si(1, 1) - mu * Ti(1, 1);
595 Scalar p = Scalar(0.5) * (a22_bar / Ti(1, 1) - Ti(0, 1) * Si(1, 0) / (Ti(0, 0) * Ti(1, 1)));
596 RealScalar sgn_p = p.real() >= RealScalar(0) ? RealScalar(1) : RealScalar(-1);
597 Scalar q = Si(1, 0) * a12_bar / (Ti(0, 0) * Ti(1, 1));
598 Scalar r = p * p + q;
599 Scalar lambda = mu + p + sgn_p * numext::sqrt(r);
600 Mat2 E = Si - lambda * Ti;
601 Index l;
602 E.rowwise().norm().maxCoeff(&l);
603 JacobiRotation<Scalar> G;
604 G.makeGivens(E(l, 1), E(l, 0));
605 m_S.applyOnTheRight(i, i + 1, G.adjoint());
606 m_T.applyOnTheRight(i, i + 1, G.adjoint());
607
608 if (m_computeQZ) m_Z.applyOnTheLeft(i, i + 1, G);
609
610 // The left rotation from T's first column keeps T triangular; the residual ~eps (|S| + |lambda| |T|) of the null
611 // vector lands in S(i+1, i), where the refinement below removes it.
612 if (numext::abs(m_T(i, i)) + numext::abs(m_T(i + 1, i)) > tolT)
613 G.makeGivens(m_T(i, i), m_T(i + 1, i));
614 else
615 G.makeGivens(m_S(i, i), m_S(i + 1, i));
616 m_S.applyOnTheLeft(i, i + 1, G.adjoint());
617 m_T.applyOnTheLeft(i, i + 1, G.adjoint());
618
619 if (m_computeQZ) m_Q.applyOnTheRight(i, i + 1, G);
620 }
621
622 // Rotations formed from lambda can leave S(i+1, i) well above eps (|S(i, i)| + |S(i+1, i+1)|), e.g. for graded T.
623 // Refine with single-shift QZ steps. With K = T^-1 S = [a b; c d], the Rayleigh quotient shift d = S(i+1, i+1) /
624 // T(i+1, i+1) maps |c| to |c|^2 |b| / (|a - d|^2 + |c|^2): quadratic for separated eigenvalues, but not monotone. Up
625 // to four steps run on a copy of the block. Of the prefixes that pass the final test |S(i+1, i)| <= eps m_normOfS, or
626 // of all if none does, the one with the smallest |S(i+1, i)| / (|S(i, i)| + |S(i+1, i+1)|) is applied.
627 const auto relativeSubdiagonal = [](const Mat2& s) {
628 const RealScalar subdiagonal = numext::abs(s(1, 0));
629 const RealScalar diagonal = numext::abs(s(0, 0)) + numext::abs(s(1, 1));
630 return subdiagonal <= NumTraits<RealScalar>::epsilon() * diagonal ? RealScalar(0) : subdiagonal / diagonal;
631 };
632 JacobiRotation<Scalar> left[4], right[4];
633 Mat2 Sstep = m_S.template block<2, 2>(i, i), Tstep = m_T.template block<2, 2>(i, i);
634 const RealScalar tolS = m_normOfS * NumTraits<RealScalar>::epsilon();
635 RealScalar current = relativeSubdiagonal(Sstep), best = current;
636 bool bestPasses = is_negligible(Sstep(1, 0), tolS);
637 int bestSteps = 0;
638 for (int k = 0; k < 4 && current > RealScalar(0); ++k) {
639 if (is_negligible(Tstep(1, 1), tolT)) {
640 // An infinite eigenvalue: deflate it with a rotation of the columns, as in the branch above.
641 left[k] = JacobiRotation<Scalar>(Scalar(1), Scalar(0));
642 right[k].makeGivens(Sstep(1, 1), Sstep(1, 0));
643 right[k] = right[k].adjoint();
644 } else {
645 const Scalar shift = Sstep(1, 1) / Tstep(1, 1);
646 left[k].makeGivens(Sstep(0, 0) - shift * Tstep(0, 0), Sstep(1, 0));
647 Sstep.applyOnTheLeft(0, 1, left[k].adjoint());
648 Tstep.applyOnTheLeft(0, 1, left[k].adjoint());
649 right[k] = computeZk2(Tstep.row(1));
650 }
651 Sstep.applyOnTheRight(0, 1, right[k]);
652 Tstep.applyOnTheRight(0, 1, right[k]);
653 Tstep(1, 0) = Scalar(0);
654 current = relativeSubdiagonal(Sstep);
655 const bool passes = is_negligible(Sstep(1, 0), tolS);
656 if ((passes && !bestPasses) || (passes == bestPasses && current < best)) {
657 best = current;
658 bestPasses = passes;
659 bestSteps = k + 1;
660 }
661 }
662 for (int k = 0; k < bestSteps; ++k) {
663 m_S.rightCols(m_n - i).applyOnTheLeft(i, i + 1, left[k].adjoint());
664 m_T.rightCols(m_n - i).applyOnTheLeft(i, i + 1, left[k].adjoint());
665 if (m_computeQZ) m_Q.applyOnTheRight(i, i + 1, left[k]);
666 m_S.topRows(i + 2).applyOnTheRight(i, i + 1, right[k]);
667 m_T.topRows(i + 2).applyOnTheRight(i, i + 1, right[k]);
668 m_T(i + 1, i) = Scalar(0);
669 if (m_computeQZ) m_Z.applyOnTheLeft(i, i + 1, right[k].adjoint());
670 }
671
672 if (!is_negligible(m_S(i + 1, i), tolS)) {
674 } else {
675 m_S(i + 1, i) = Scalar(0);
676 m_T(i + 1, i) = Scalar(0);
677 }
678}
679
681template <typename MatrixType_>
682void ComplexQZ<MatrixType_>::push_down_zero_ST(Index k, Index l) {
683 JacobiRotation<Scalar> J;
684 for (Index j = k + 1; j <= l; j++) {
685 // Create a 0 at _T(j, j)
686 J.makeGivens(m_T(j - 1, j), m_T(j, j), &m_T.coeffRef(j - 1, j));
687 if (m_n - j - 1 > 0) {
688 m_T.rightCols(m_n - j - 1).applyOnTheLeft(j - 1, j, J.adjoint());
689 }
690 m_T.coeffRef(j, j) = Scalar(0);
691
692 m_S.applyOnTheLeft(j - 1, j, J.adjoint());
693
694 if (m_computeQZ) m_Q.applyOnTheRight(j - 1, j, J);
695
696 // Delete the non-desired non-zero at _S(j, j-2)
697 if (j > 1) {
698 J.makeGivens(numext::conj(m_S(j, j - 1)), numext::conj(m_S(j, j - 2)));
699 m_S.applyOnTheRight(j - 1, j - 2, J);
700 m_S(j, j - 2) = Scalar(0);
701 m_T.applyOnTheRight(j - 1, j - 2, J);
702 if (m_computeQZ) m_Z.applyOnTheLeft(j - 1, j - 2, J.adjoint());
703 }
704 }
705
706 // Assume we have the desired structure now, up to the non-zero entry at
707 // _S(l, l-1) which we will delete through a last right-jacobi-rotation
708 J.makeGivens(numext::conj(m_S(l, l)), numext::conj(m_S(l, l - 1)));
709 m_S.topRows(l + 1).applyOnTheRight(l, l - 1, J);
710
711 if (!is_negligible(m_S(l, l - 1), m_normOfS * NumTraits<Scalar>::epsilon())) {
713 } else {
714 m_S(l, l - 1) = Scalar(0);
715 }
716 m_T.topRows(l + 1).applyOnTheRight(l, l - 1, J);
717
718 if (m_computeQZ) m_Z.applyOnTheLeft(l, l - 1, J.adjoint());
719
720 // Ensure postconditions, with the same thresholds relative to the norms as the deflation tests.
721 if (!is_negligible(m_T(l, l), m_normOfT * NumTraits<RealScalar>::epsilon()) ||
722 !is_negligible(m_S(l, l - 1), m_normOfS * NumTraits<RealScalar>::epsilon())) {
724 } else {
725 m_T(l, l) = Scalar(0);
726 m_S(l, l - 1) = Scalar(0);
727 }
728}
729
731template <typename MatrixType_>
733 m_normOfS = internal::hessenberg_abs_sum<Upper>(m_S);
734 m_normOfT = internal::triangular_abs_sum<Upper>(m_T);
735}
736
739template <typename MatrixType_>
741 Index res = iu;
742 while (res > 0) {
743 RealScalar s = numext::abs(m_S.coeff(res - 1, res - 1)) + numext::abs(m_S.coeff(res, res));
744 if (s == Scalar(0)) s = m_normOfS;
745 // <=, so that an exact zero deflates also where s is 0, e.g. for S = 0.
746 if (numext::abs(m_S.coeff(res, res - 1)) <= NumTraits<RealScalar>::epsilon() * s) break;
747 res--;
748 }
749 return res;
750}
751
754template <typename MatrixType_>
755inline Index ComplexQZ<MatrixType_>::findSmallDiagEntry(Index f, Index l) {
756 Index res = l;
757 while (res >= f) {
758 if (numext::abs(m_T.coeff(res, res)) <= NumTraits<RealScalar>::epsilon() * m_normOfT) break;
759 res--;
760 }
761 return res;
762}
763
764} // namespace Eigen
765
766#endif // EIGEN_COMPLEX_QZ_H_
Performs a QZ decomposition of a pair of matrices A, B.
Rotation given by a cosine-sine pair.
Definition Jacobi.h:39
JacobiRotation adjoint() const
Definition Jacobi.h:64
void makeGivens(const Scalar &p, const Scalar &q, Scalar *r=0)
Definition Jacobi.h:157
HouseholderSequence< VectorsType, CoeffsType > householderSequence(const VectorsType &v, const CoeffsType &h)
Convenience function for constructing a Householder sequence.
Definition HouseholderSequence.h:739
@ NumericalIssue
Definition Constants.h:459
@ InvalidInput
Definition Constants.h:464
@ Success
Definition Constants.h:457
@ NoConvergence
Definition Constants.h:461
Matrix< int, 3, 1 > Vector3i
3×1 vector of type int.
Definition Matrix.h:487