13#ifndef EIGEN_GENERALIZEDEIGENSOLVER_H
14#define EIGEN_GENERALIZEDEIGENSOLVER_H
19#include "./InternalHeaderCheck.h"
28template <
typename RealScalar>
29RealScalar generalized_eigen_split_pair_diagonal(
const RealScalar& a,
const RealScalar& b, RealScalar& aScaled,
30 RealScalar& bScaled, true_type) {
32 const RealScalar maxT = max_preserving_subnormals(abs_preserving_subnormals(a), abs_preserving_subnormals(b));
33 if (numext::equal_strict(frexp_preserving_subnormals(maxT, e), RealScalar(0.5))) --e;
34 aScaled = ldexp_preserving_subnormals(a, -e);
35 bScaled = ldexp_preserving_subnormals(b, -e);
36 return ldexp_preserving_subnormals(aScaled, e) * bScaled;
38template <
typename RealScalar>
39RealScalar generalized_eigen_split_pair_diagonal(
const RealScalar& a,
const RealScalar& b, RealScalar& aScaled,
40 RealScalar& bScaled, false_type) {
41 const RealScalar maxT = numext::maxi(numext::abs(a), numext::abs(b));
44 if (!numext::is_exactly_zero(maxT)) {
92template <
typename MatrixType_>
99 RowsAtCompileTime = MatrixType::RowsAtCompileTime,
100 ColsAtCompileTime = MatrixType::ColsAtCompileTime,
101 Options = internal::plain_object_options<MatrixType>::value,
102 MaxRowsAtCompileTime = MatrixType::MaxRowsAtCompileTime,
103 MaxColsAtCompileTime = MatrixType::MaxColsAtCompileTime
107 using Scalar =
typename MatrixType::Scalar;
108 using RealScalar =
typename NumTraits<Scalar>::Real;
154 : m_eivec(), m_alphas(), m_betas(), m_computeEigenvectors(false), m_isInitialized(false), m_realQZ() {}
163 : m_eivec(size, size),
166 m_computeEigenvectors(false),
167 m_isInitialized(false),
183 template <
typename InputTypeA,
typename InputTypeB>
185 bool computeEigenvectors =
true)
186 : m_eivec(A.rows(), A.cols()),
189 m_computeEigenvectors(false),
190 m_isInitialized(false),
191 m_realQZ(A.derived(), B.derived(), computeEigenvectors),
193 computeFromQZ(computeEigenvectors);
207 template <
typename InputTypeA,
typename InputTypeB>
209 : m_eivec(A.rows(), A.cols()),
212 m_computeEigenvectors(false),
213 m_isInitialized(false),
214 m_realQZ(A.derived(), B.derived(), computeEigenvectors),
216 computeFromQZ(computeEigenvectors);
232 eigen_assert(info() ==
Success &&
"GeneralizedEigenSolver failed to compute eigenvectors");
233 eigen_assert(m_computeEigenvectors &&
"Eigenvectors for GeneralizedEigenSolver were not calculated");
256 eigen_assert(info() ==
Success &&
"GeneralizedEigenSolver failed to compute eigenvalues.");
266 eigen_assert(info() ==
Success &&
"GeneralizedEigenSolver failed to compute alphas.");
276 eigen_assert(info() ==
Success &&
"GeneralizedEigenSolver failed to compute betas.");
303 template <
typename InputTypeA,
typename InputTypeB>
305 bool computeEigenvectors =
true);
308 eigen_assert(m_isInitialized &&
"EigenSolver is not initialized.");
309 return m_realQZ.info();
320 EIGEN_STATIC_ASSERT_NON_INTEGER(Scalar)
321 EIGEN_STATIC_ASSERT(!NumTraits<Scalar>::IsComplex, NUMERIC_TYPE_MUST_BE_REAL)
326 bool m_computeEigenvectors;
327 bool m_isInitialized;
335 template <
typename T>
337 const RealScalar& smin) {
339 m.cwiseAbs().maxCoeff(&row, &col);
340 const Index otherRow = 1 - row, otherCol = 1 - col;
341 T pivot = m.
coeff(row, col);
342 if (numext::abs(pivot) < smin) pivot = T(smin);
343 const T multiplier = m.
coeff(otherRow, col) / pivot;
344 T otherPivot = m.
coeff(otherRow, otherCol) - multiplier * m.
coeff(row, otherCol);
345 if (numext::abs(otherPivot) < smin) otherPivot = T(smin);
347 x.coeffRef(otherCol) = (r.
coeff(otherRow) - multiplier * r.
coeff(row)) / otherPivot;
348 x.coeffRef(col) = (r.
coeff(row) - m.
coeff(row, otherCol) * x.coeff(otherCol)) / pivot;
353template <
typename MatrixType>
354template <
typename InputTypeA,
typename InputTypeB>
357 bool computeEigenvectors) {
358 EIGEN_INTERNAL_ASSERT_SQUARE(A);
359 EIGEN_INTERNAL_ASSERT_SAME_SHAPE(A, B);
362 m_realQZ.
compute(A.derived(), B.derived(), computeEigenvectors);
363 return computeFromQZ(computeEigenvectors);
368template <
typename MatrixType>
370 const Index size = m_realQZ.
matrixS().cols();
373 m_alphas.resize(size);
374 m_betas.resize(size);
375 if (computeEigenvectors) {
376 m_eivec.resize(size, size);
382 ComplexVectorType& cv = m_tmp;
383 const MatrixType& mS = m_realQZ.
matrixS();
384 const MatrixType& mT = m_realQZ.
matrixT();
399 const RealScalar eps = NumTraits<RealScalar>::epsilon();
400 const RealScalar big = RealScalar(1) / eps;
401 const RealScalar smallestNormal = RealScalar((std::numeric_limits<RealScalar>::min)());
402 const RealScalar maxS = computeEigenvectors && size > 0
403 ? numext::maxi(RealScalar(mS.cwiseAbs().maxCoeff()), smallestNormal)
405 const RealScalar maxT = computeEigenvectors && size > 0
406 ? numext::maxi(RealScalar(mT.cwiseAbs().maxCoeff()), smallestNormal)
409 const RealScalar wanted = numext::maxi(maxS, maxT) * (smallestNormal / eps);
410 const RealScalar halfHighest = NumTraits<RealScalar>::highest() / RealScalar(2);
411 const RealScalar smaller = numext::mini(maxS, maxT);
412 const RealScalar bound =
413 numext::maxi(RealScalar(1), smaller < wanted / halfHighest ? RealScalar(smaller * halfHighest) : wanted);
414 const RealScalar scaleS = bound / maxS, scaleT = bound / maxT;
415 const auto scaleDivisor = [&](
const RealScalar& absAlpha,
const RealScalar& absBeta) {
416 return numext::maxi(numext::maxi(RealScalar(absBeta / maxT), RealScalar(absAlpha / maxS)), smallestNormal);
418 const auto pivotFloor = [&](Index row, Index first, Index last,
const RealScalar& absAlpha,
419 const RealScalar& absBeta) {
420 const Index count = last - first + 1;
421 return numext::maxi(RealScalar(eps * (absBeta * mS.row(row).segment(first, count).cwiseAbs() +
422 absAlpha * mT.row(row).segment(first, count).cwiseAbs())
429 if (i == size - 1 || mS.coeff(i + 1, i) == Scalar(0)) {
431 m_alphas.coeffRef(i) = mS.diagonal().coeff(i);
432 m_betas.coeffRef(i) = mT.diagonal().coeff(i);
433 if (computeEigenvectors) {
434 v.setConstant(Scalar(0.0));
435 v.coeffRef(i) = Scalar(1.0);
436 const Scalar alpha = numext::real(m_alphas.coeffRef(i)), beta = m_betas.coeffRef(i);
437 const RealScalar divisor = scaleDivisor(numext::abs(alpha), numext::abs(beta));
438 const Scalar scaledAlpha = alpha / divisor / maxS * scaleT, scaledBeta = beta / divisor / maxT * scaleS;
439 const RealScalar absAlpha = numext::abs(scaledAlpha), absBeta = numext::abs(scaledBeta);
440 for (Index j = i - 1; j >= 0; j--) {
441 const Index st = j + 1;
442 const Index sz = i - j;
444 if (j > 0 && mS.coeff(j, j - 1) != Scalar(0)) {
447 scaledBeta * mS.template block<2, Dynamic>(j - 1, st, 2, sz))
448 .lazyProduct(v.segment(st, sz));
450 scaledAlpha * mT.template block<2, 2>(j - 1, j - 1);
451 const RealScalar smin = numext::maxi(pivotFloor(j - 1, j - 1, i, absAlpha, absBeta),
452 pivotFloor(j, j - 1, i, absAlpha, absBeta));
453 v.template segment<2>(j - 1) = solveWithPerturbedPivots(lhs, rhs, smin);
454 largest = v.template segment<2>(j - 1).cwiseAbs().maxCoeff();
457 Scalar pivot = scaledBeta * mS.coeff(j, j) - scaledAlpha * mT.coeff(j, j);
458 const RealScalar smin = pivotFloor(j, j, i, absAlpha, absBeta);
459 if (numext::abs(pivot) < smin) pivot = Scalar(smin);
463 .cwiseProduct(scaledBeta * mS.block(j, st, 1, sz) - scaledAlpha * mT.block(j, st, 1, sz))
466 largest = numext::abs(v.coeff(j));
468 if (largest > big) v /= largest;
470 m_eivec.col(i).real().noalias() = m_realQZ.
matrixZ().transpose() * v;
471 m_eivec.col(i).real().normalize();
484 const RealScalar a = mT.diagonal().coeff(i), b = mT.diagonal().coeff(i + 1);
485 RealScalar aScaled, bScaled;
486 const RealScalar beta = m_betas.coeffRef(i) = m_betas.coeffRef(i + 1) =
487 internal::generalized_eigen_split_pair_diagonal(
488 a, b, aScaled, bScaled,
489 internal::bool_constant<internal::use_subnormal_preserving_scaling<RealScalar, RealScalar>::value>());
493 const RealScalar p = RealScalar(0.5) * M.
coeff(0, 0) - RealScalar(0.5) * M.coeff(1, 1);
494 const RealScalar m01 = M.coeff(0, 1), m10 = M.coeff(1, 0);
495 const RealScalar bcMax = numext::maxi(numext::abs(m01), numext::abs(m10));
496 const RealScalar bcMin = numext::mini(numext::abs(m01), numext::abs(m10));
497 const RealScalar scale = numext::maxi(numext::abs(p), bcMax);
499 if (!numext::is_exactly_zero(scale)) {
500 const RealScalar bcMinSigned = ((m01 < RealScalar(0)) == (m10 < RealScalar(0))) ? bcMin : -bcMin;
501 z = numext::sqrt(scale) * numext::sqrt(numext::abs((p / scale) * p + (bcMax / scale) * bcMinSigned));
503 const ComplexScalar alpha = ComplexScalar(M.coeff(1, 1) + p, (beta > 0) ? z : -z);
504 m_alphas.coeffRef(i) = numext::conj(alpha);
505 m_alphas.coeffRef(i + 1) = alpha;
507 if (computeEigenvectors) {
510 cv.coeffRef(i + 1) = Scalar(1.0);
511 const RealScalar divisor = scaleDivisor(numext::abs(alpha), numext::abs(beta));
512 const ComplexScalar scaledAlpha = alpha / divisor / maxS * scaleT;
513 const RealScalar scaledBeta = beta / divisor / maxT * scaleS;
514 const RealScalar absAlpha = numext::abs(scaledAlpha), absBeta = numext::abs(scaledBeta);
517 -(
static_cast<Scalar
>(scaledBeta * mS.coeffRef(i, i + 1)) - scaledAlpha * mT.coeffRef(i, i + 1)) /
518 (
static_cast<Scalar
>(scaledBeta * mS.coeffRef(i, i)) - scaledAlpha * mT.coeffRef(i, i));
519 if (numext::abs(cv.coeff(i)) > big) cv /= numext::abs(cv.coeff(i));
520 for (Index j = i - 1; j >= 0; j--) {
521 const Index st = j + 1;
522 const Index sz = i + 1 - j;
524 if (j > 0 && mS.coeff(j, j - 1) != Scalar(0)) {
527 scaledBeta * mS.template block<2, Dynamic>(j - 1, st, 2, sz))
528 .lazyProduct(cv.segment(st, sz));
530 scaledAlpha * mT.template block<2, 2>(j - 1, j - 1);
531 const RealScalar smin = numext::maxi(pivotFloor(j - 1, j - 1, i + 1, absAlpha, absBeta),
532 pivotFloor(j, j - 1, i + 1, absAlpha, absBeta));
533 cv.template segment<2>(j - 1) = solveWithPerturbedPivots(lhs, rhs, smin);
534 largest = cv.template segment<2>(j - 1).cwiseAbs().maxCoeff();
537 ComplexScalar pivot = scaledAlpha * mT.coeff(j, j) -
static_cast<Scalar
>(scaledBeta * mS.coeff(j, j));
538 const RealScalar smin = pivotFloor(j, j, i + 1, absAlpha, absBeta);
539 if (numext::abs(pivot) < smin) pivot = ComplexScalar(smin);
543 .cwiseProduct(scaledBeta * mS.block(j, st, 1, sz) - scaledAlpha * mT.block(j, st, 1, sz))
546 largest = numext::abs(cv.coeff(j));
548 if (largest > big) cv /= largest;
550 m_eivec.col(i + 1).noalias() = m_realQZ.
matrixZ().transpose() * cv;
551 m_eivec.col(i + 1).normalize();
552 m_eivec.col(i) = m_eivec.col(i + 1).conjugate();
558 m_computeEigenvectors = computeEigenvectors;
559 m_isInitialized =
true;
Generic expression where a coefficient-wise binary operator is applied to two expressions.
Definition CwiseBinaryOp.h:91
Computes the generalized eigenvalues and eigenvectors of a pair of general matrices.
Definition GeneralizedEigenSolver.h:93
GeneralizedEigenSolver(EigenBase< InputTypeA > &A, EigenBase< InputTypeB > &B, bool computeEigenvectors=true)
Constructor for inplace decomposition .
Definition GeneralizedEigenSolver.h:208
internal::make_complex_t< Scalar > ComplexScalar
Complex scalar type for MatrixType.
Definition GeneralizedEigenSolver.h:117
Eigen::Index Index
Definition GeneralizedEigenSolver.h:109
EigenvectorsType eigenvectors() const
Returns the computed generalized eigenvectors.
Definition GeneralizedEigenSolver.h:231
Matrix< Scalar, ColsAtCompileTime, 1, Options &~RowMajor, MaxColsAtCompileTime, 1 > VectorType
Type for vector of real scalar values eigenvalues as returned by betas().
Definition GeneralizedEigenSolver.h:124
typename MatrixType::Scalar Scalar
Scalar type for matrices of type MatrixType.
Definition GeneralizedEigenSolver.h:107
MatrixType_ MatrixType
Synonym for the template parameter MatrixType_.
Definition GeneralizedEigenSolver.h:96
const VectorType & betas() const
Definition GeneralizedEigenSolver.h:275
CwiseBinaryOp< internal::scalar_quotient_op< ComplexScalar, Scalar >, ComplexVectorType, VectorType > EigenvalueType
Expression type for the eigenvalues as returned by eigenvalues().
Definition GeneralizedEigenSolver.h:135
GeneralizedEigenSolver()
Default constructor.
Definition GeneralizedEigenSolver.h:153
const ComplexVectorType & alphas() const
Definition GeneralizedEigenSolver.h:265
Matrix< ComplexScalar, ColsAtCompileTime, 1, Options &~RowMajor, MaxColsAtCompileTime, 1 > ComplexVectorType
Type for vector of complex scalar values eigenvalues as returned by alphas().
Definition GeneralizedEigenSolver.h:131
GeneralizedEigenSolver(const EigenBase< InputTypeA > &A, const EigenBase< InputTypeB > &B, bool computeEigenvectors=true)
Constructor; computes the generalized eigendecomposition of given matrix pair.
Definition GeneralizedEigenSolver.h:184
Matrix< ComplexScalar, RowsAtCompileTime, ColsAtCompileTime, Options, MaxRowsAtCompileTime, MaxColsAtCompileTime > EigenvectorsType
Type for matrix of eigenvectors as returned by eigenvectors().
Definition GeneralizedEigenSolver.h:143
EigenvalueType eigenvalues() const
Returns an expression of the computed generalized eigenvalues.
Definition GeneralizedEigenSolver.h:255
GeneralizedEigenSolver(Index size)
Default constructor with memory preallocation.
Definition GeneralizedEigenSolver.h:162
GeneralizedEigenSolver & setMaxIterations(Index maxIters)
Definition GeneralizedEigenSolver.h:314
GeneralizedEigenSolver & compute(const EigenBase< InputTypeA > &A, const EigenBase< InputTypeB > &B, bool computeEigenvectors=true)
Computes generalized eigendecomposition of given matrix.
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
constexpr const Scalar & coeff(Index rowId, Index colId) const
Definition PlainObjectBase.h:187
Derived & setConstant(Index size, const Scalar &val)
Definition CwiseNullaryOp.h:349
Performs a real QZ decomposition of a pair of square matrices.
Definition RealQZ.h:62
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
ComputationInfo info() const
Reports whether previous computation was successful.
Definition RealQZ.h:206
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.
ComputationInfo
Definition Constants.h:455
@ Success
Definition Constants.h:457
Definition EigenBase.h:34