12#ifndef EIGEN_EIGENSOLVER_H
13#define EIGEN_EIGENSOLVER_H
15#include "./RealSchur.h"
18#include "./InternalHeaderCheck.h"
68template <
typename MatrixType_>
75 RowsAtCompileTime = MatrixType::RowsAtCompileTime,
76 ColsAtCompileTime = MatrixType::ColsAtCompileTime,
77 Options = internal::plain_object_options<MatrixType>::value,
78 MaxRowsAtCompileTime = MatrixType::MaxRowsAtCompileTime,
79 MaxColsAtCompileTime = MatrixType::MaxColsAtCompileTime
83 using Scalar =
typename MatrixType::Scalar;
84 using RealScalar =
typename NumTraits<Scalar>::Real;
122 EigenSolver() : m_eivalues(), m_isInitialized(false), m_eigenvectorsOk(false), m_realSchur(), m_tmp() {}
131 : m_eivalues(size), m_isInitialized(false), m_eigenvectorsOk(false), m_realSchur(size), m_tmp(size) {}
148 template <
typename InputType>
150 : m_eivalues(matrix.cols()),
151 m_isInitialized(false),
152 m_eigenvectorsOk(false),
153 m_realSchur(matrix.derived(), computeEigenvectors),
154 m_tmp(matrix.cols()) {
155 check_template_parameters();
156 computeFromSchur(computeEigenvectors);
170 template <
typename InputType>
172 : m_eivalues(matrix.cols()),
173 m_isInitialized(false),
174 m_eigenvectorsOk(false),
175 m_realSchur(matrix.derived(), computeEigenvectors),
176 m_tmp(matrix.cols()) {
177 check_template_parameters();
178 computeFromSchur(computeEigenvectors);
222 eigen_assert(m_isInitialized &&
"EigenSolver is not initialized.");
223 eigen_assert(m_eigenvectorsOk &&
"The eigenvectors have not been computed together with the eigenvalues.");
224 return m_realSchur.m_matU;
266 eigen_assert(m_isInitialized &&
"EigenSolver is not initialized.");
297 template <
typename InputType>
304 eigen_assert(m_isInitialized &&
"EigenSolver is not initialized.");
318 EigenSolver& computeFromSchur(
bool computeEigenvectors);
319 void doComputeEigenvectors();
322 static void check_template_parameters() {
323 EIGEN_STATIC_ASSERT_NON_INTEGER(Scalar);
324 EIGEN_STATIC_ASSERT(!NumTraits<Scalar>::IsComplex, NUMERIC_TYPE_MUST_BE_REAL);
328 bool m_isInitialized;
329 bool m_eigenvectorsOk;
330 ComputationInfo m_info;
333 RealSchur<MatrixType> m_realSchur;
335 using ColumnVectorType = Matrix<Scalar, ColsAtCompileTime, 1, Options & ~RowMajor, MaxColsAtCompileTime, 1>;
336 ColumnVectorType m_tmp;
339template <
typename MatrixType>
341 eigen_assert(m_isInitialized &&
"EigenSolver is not initialized.");
342 const Index n = m_eivalues.rows();
345 for (; i < n - 1; ++i) {
346 RealScalar real = numext::real(m_eivalues.coeff(i));
347 RealScalar imag = numext::imag(m_eivalues.coeff(i));
350 if (!numext::is_exactly_zero(imag)) {
358 matD.
coeffRef(i, i) = numext::real(m_eivalues.coeff(i));
364template <
typename MatrixType>
366 eigen_assert(m_isInitialized &&
"EigenSolver is not initialized.");
367 eigen_assert(m_eigenvectorsOk &&
"The eigenvectors have not been computed together with the eigenvalues.");
369 Index n = eivec.cols();
371 for (
Index j = 0; j < n; ++j) {
372 if (numext::is_exactly_zero(numext::imag(m_eivalues.coeff(j))) || j + 1 == n) {
374 matV.col(j) = eivec.col(j).template cast<ComplexScalar>();
375 matV.col(j).normalize();
378 for (
Index i = 0; i < n; ++i) {
382 matV.col(j).normalize();
383 matV.col(j + 1).normalize();
390template <
typename MatrixType>
391template <
typename InputType>
393 bool computeEigenvectors) {
394 check_template_parameters();
395 EIGEN_INTERNAL_ASSERT_SQUARE(matrix);
398 m_realSchur.compute(matrix.
derived(), computeEigenvectors);
399 return computeFromSchur(computeEigenvectors);
404template <
typename MatrixType>
405EigenSolver<MatrixType>& EigenSolver<MatrixType>::computeFromSchur(
bool computeEigenvectors) {
406 m_info = m_realSchur.info();
409 const MatrixType& matT = m_realSchur.m_matT;
410 const Index n = matT.cols();
413 m_eivalues.resize(n);
416 if (i == n - 1 || matT.coeff(i + 1, i) == Scalar(0)) {
417 m_eivalues.coeffRef(i) = matT.coeff(i, i);
418 if (!(numext::isfinite)(m_eivalues.coeffRef(i))) {
419 m_isInitialized =
true;
420 m_eigenvectorsOk =
false;
426 Scalar p = Scalar(0.5) * (matT.coeff(i, i) - matT.coeff(i + 1, i + 1));
431 Scalar t0 = matT.coeff(i + 1, i);
432 Scalar t1 = matT.coeff(i, i + 1);
433 Scalar maxval = numext::maxi<Scalar>(numext::abs(p), numext::maxi<Scalar>(numext::abs(t0), numext::abs(t1)));
436 Scalar p0 = p / maxval;
437 z = maxval * numext::sqrt(numext::abs(p0 * p0 + t0 * t1));
440 m_eivalues.coeffRef(i) = ComplexScalar(matT.coeff(i + 1, i + 1) + p, z);
441 m_eivalues.coeffRef(i + 1) = ComplexScalar(matT.coeff(i + 1, i + 1) + p, -z);
442 if (!((numext::isfinite)(m_eivalues.coeffRef(i)) && (numext::isfinite)(m_eivalues.coeffRef(i + 1)))) {
443 m_isInitialized =
true;
444 m_eigenvectorsOk =
false;
453 if (computeEigenvectors) doComputeEigenvectors();
454 }
else if (!m_realSchur.m_matT.allFinite()) {
459 m_isInitialized =
true;
460 m_eigenvectorsOk = computeEigenvectors;
465template <
typename MatrixType>
466void EigenSolver<MatrixType>::doComputeEigenvectors() {
469 MatrixType& matT = m_realSchur.m_matT;
470 PlainMatrixType& eivec = m_realSchur.m_matU;
471 const Index size = eivec.cols();
472 const Scalar eps = NumTraits<Scalar>::epsilon();
474 Scalar norm = internal::hessenberg_abs_sum<Upper>(matT);
477 if (norm == Scalar(0)) {
485 const Scalar safeMin = numext::sqrt((std::numeric_limits<Scalar>::min)() / eps);
486 const Scalar safeMax = numext::sqrt(NumTraits<Scalar>::highest() / Scalar(8 * size));
487 EigenvalueType scaledEivalues;
489 if (!(norm <= safeMax && norm >= Scalar(size) * Scalar(size) * safeMin)) {
491 for (Index j = 0; j < size; ++j)
492 maxCoeff = numext::maxi(maxCoeff, matT.col(j).head(numext::mini(j + 2, size)).cwiseAbs().maxCoeff());
493 const auto factors = internal::safe_scaling<Scalar>::scale_in_place(matT, maxCoeff);
494 scaledEivalues = m_eivalues;
495 internal::safe_scaling<Scalar>::scale_in_place(scaledEivalues, maxCoeff, factors);
496 norm = internal::hessenberg_abs_sum<Upper>(matT);
499 const EigenvalueType& eivalues = scaled ? scaledEivalues : m_eivalues;
501 for (Index n = size - 1; n >= 0; n--) {
502 Scalar p = eivalues.coeff(n).real();
503 Scalar q = eivalues.coeff(n).imag();
506 if (q == Scalar(0)) {
507 Scalar lastr(0), lastw(0);
510 matT.coeffRef(n, n) = Scalar(1);
511 for (Index i = n - 1; i >= 0; i--) {
512 Scalar w = matT.coeff(i, i) - p;
513 Scalar r = matT.row(i).segment(l, n - l + 1).dot(matT.col(n).segment(l, n - l + 1));
515 if (eivalues.coeff(i).imag() < Scalar(0)) {
520 if (eivalues.coeff(i).imag() == Scalar(0)) {
521 const Scalar divisor = w != Scalar(0) ? w : eps * norm;
526 if (numext::abs(r) > numext::abs(divisor) * NumTraits<Scalar>::highest() && (numext::isfinite)(r)) {
527 const Scalar f = numext::abs(divisor) / numext::abs(r);
528 matT.col(n).tail(size - i - 1) *= f;
529 r = r < Scalar(0) ? -numext::abs(divisor) : numext::abs(divisor);
531 matT.coeffRef(i, n) = -r / divisor;
534 Scalar x = matT.coeff(i, i + 1);
535 Scalar y = matT.coeff(i + 1, i);
536 Scalar denom = (eivalues.coeff(i).real() - p) * (eivalues.coeff(i).real() - p) +
537 eivalues.coeff(i).imag() * eivalues.coeff(i).imag();
538 Scalar t = (x * lastr - lastw * r) / denom;
539 matT.coeffRef(i, n) = t;
540 if (numext::abs(x) > numext::abs(lastw))
541 matT.coeffRef(i + 1, n) = (-r - w * t) / x;
543 matT.coeffRef(i + 1, n) = (-lastr - y * t) / lastw;
547 Scalar t = numext::abs(matT.coeff(i, n));
548 if ((eps * t) * t > Scalar(1)) matT.col(n).tail(size - i) /= t;
551 }
else if (q < Scalar(0) && n > 0)
553 Scalar lastra(0), lastsa(0), lastw(0);
557 if (numext::abs(matT.coeff(n, n - 1)) > numext::abs(matT.coeff(n - 1, n))) {
558 matT.coeffRef(n - 1, n - 1) = q / matT.coeff(n, n - 1);
559 matT.coeffRef(n - 1, n) = -(matT.coeff(n, n) - p) / matT.coeff(n, n - 1);
562 ComplexScalar(Scalar(0), -matT.coeff(n - 1, n)) / ComplexScalar(matT.coeff(n - 1, n - 1) - p, q);
563 matT.coeffRef(n - 1, n - 1) = numext::real(cc);
564 matT.coeffRef(n - 1, n) = numext::imag(cc);
566 matT.coeffRef(n, n - 1) = Scalar(0);
567 matT.coeffRef(n, n) = Scalar(1);
568 for (Index i = n - 2; i >= 0; i--) {
569 Scalar ra = matT.row(i).segment(l, n - l + 1).dot(matT.col(n - 1).segment(l, n - l + 1));
570 Scalar sa = matT.row(i).segment(l, n - l + 1).dot(matT.col(n).segment(l, n - l + 1));
571 Scalar w = matT.coeff(i, i) - p;
573 if (eivalues.coeff(i).imag() < Scalar(0)) {
579 if (eivalues.coeff(i).imag() == RealScalar(0)) {
580 ComplexScalar cc = ComplexScalar(-ra, -sa) / ComplexScalar(w, q);
581 matT.coeffRef(i, n - 1) = numext::real(cc);
582 matT.coeffRef(i, n) = numext::imag(cc);
585 Scalar x = matT.coeff(i, i + 1);
586 Scalar y = matT.coeff(i + 1, i);
587 Scalar vr = (eivalues.coeff(i).real() - p) * (eivalues.coeff(i).real() - p) +
588 eivalues.coeff(i).imag() * eivalues.coeff(i).imag() - q * q;
589 Scalar vi = (eivalues.coeff(i).real() - p) * Scalar(2) * q;
590 if ((vr == Scalar(0)) && (vi == Scalar(0)))
592 eps * norm * (numext::abs(w) + numext::abs(q) + numext::abs(x) + numext::abs(y) + numext::abs(lastw));
594 ComplexScalar cc = ComplexScalar(x * lastra - lastw * ra + q * sa, x * lastsa - lastw * sa - q * ra) /
595 ComplexScalar(vr, vi);
596 matT.coeffRef(i, n - 1) = numext::real(cc);
597 matT.coeffRef(i, n) = numext::imag(cc);
598 if (numext::abs(x) > (numext::abs(lastw) + numext::abs(q))) {
599 matT.coeffRef(i + 1, n - 1) = (-ra - w * matT.coeff(i, n - 1) + q * matT.coeff(i, n)) / x;
600 matT.coeffRef(i + 1, n) = (-sa - w * matT.coeff(i, n) - q * matT.coeff(i, n - 1)) / x;
602 cc = ComplexScalar(-lastra - y * matT.coeff(i, n - 1), -lastsa - y * matT.coeff(i, n)) /
603 ComplexScalar(lastw, q);
604 matT.coeffRef(i + 1, n - 1) = numext::real(cc);
605 matT.coeffRef(i + 1, n) = numext::imag(cc);
610 Scalar t = numext::maxi<Scalar>(numext::abs(matT.coeff(i, n - 1)), numext::abs(matT.coeff(i, n)));
611 if ((eps * t) * t > Scalar(1)) matT.block(i, n - 1, size - i, 2) /= t;
618 eigen_assert(0 &&
"Internal bug in EigenSolver (INF or NaN has not been detected)");
623 for (Index j = size - 1; j >= 0; j--) {
624 m_tmp.noalias() = eivec.leftCols(j + 1) * matT.col(j).segment(0, j + 1);
625 eivec.col(j) = m_tmp;
Computes eigenvalues and eigenvectors of general matrices.
Definition EigenSolver.h:69
EigenSolver(const EigenBase< InputType > &matrix, bool computeEigenvectors=true)
Constructor; computes eigendecomposition of given matrix.
Definition EigenSolver.h:149
PlainMatrixType pseudoEigenvalueMatrix() const
Returns the block-diagonal matrix in the pseudo-eigendecomposition.
Definition EigenSolver.h:340
EigenSolver(EigenBase< InputType > &matrix, bool computeEigenvectors=true)
Constructor for inplace decomposition .
Definition EigenSolver.h:171
typename MatrixType::Scalar Scalar
Scalar type for matrices of type MatrixType.
Definition EigenSolver.h:83
EigenSolver(Index size)
Default constructor with memory preallocation.
Definition EigenSolver.h:130
Matrix< ComplexScalar, ColsAtCompileTime, 1, Options &~RowMajor, MaxColsAtCompileTime, 1 > EigenvalueType
Type for vector of eigenvalues as returned by eigenvalues().
Definition EigenSolver.h:105
ComputationInfo info() const
Definition EigenSolver.h:303
EigenvectorsType eigenvectors() const
Returns the eigenvectors of given matrix.
Definition EigenSolver.h:365
EigenSolver()
Default constructor.
Definition EigenSolver.h:122
EigenSolver & setMaxIterations(Index maxIters)
Sets the maximum number of iterations allowed.
Definition EigenSolver.h:309
const PlainMatrixType & pseudoEigenvectors() const
Returns the pseudo-eigenvectors of given matrix.
Definition EigenSolver.h:221
Index getMaxIterations() const
Returns the maximum number of iterations.
Definition EigenSolver.h:315
Matrix< ComplexScalar, RowsAtCompileTime, ColsAtCompileTime, Options, MaxRowsAtCompileTime, MaxColsAtCompileTime > EigenvectorsType
Type for matrix of eigenvectors as returned by eigenvectors().
Definition EigenSolver.h:112
EigenSolver & compute(const EigenBase< InputType > &matrix, bool computeEigenvectors=true)
Computes eigendecomposition of given matrix.
MatrixType_ MatrixType
Synonym for the template parameter MatrixType_.
Definition EigenSolver.h:72
internal::make_complex_t< Scalar > ComplexScalar
Complex scalar type for MatrixType.
Definition EigenSolver.h:98
const EigenvalueType & eigenvalues() const
Returns the eigenvalues of given matrix.
Definition EigenSolver.h:265
Eigen::Index Index
Definition EigenSolver.h:85
Matrix< Scalar, RowsAtCompileTime, ColsAtCompileTime, Options, MaxRowsAtCompileTime, MaxColsAtCompileTime > PlainMatrixType
Plain matrix type with the shape and storage options of MatrixType; MatrixType itself unless that is ...
Definition EigenSolver.h:89
The matrix class, also used for vectors and row-vectors.
Definition Matrix.h:188
constexpr Scalar & coeffRef(Index rowId, Index colId)
Definition PlainObjectBase.h:218
constexpr const Scalar & coeff(Index rowId, Index colId) const
Definition PlainObjectBase.h:200
ComputationInfo
Definition Constants.h:455
@ NumericalIssue
Definition Constants.h:459
@ Success
Definition Constants.h:457
Definition EigenBase.h:34
constexpr Derived & derived()
Definition EigenBase.h:50