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 RealScalar precision = RealScalar(2) * NumTraits<RealScalar>::epsilon();
343 const Index n = m_eivalues.rows();
346 for (; i < n - 1; ++i) {
347 RealScalar real = numext::real(m_eivalues.coeff(i));
348 RealScalar imag = numext::imag(m_eivalues.coeff(i));
350 if (!internal::isMuchSmallerThan(imag, real, precision)) {
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.");
368 const RealScalar precision = RealScalar(2) * NumTraits<RealScalar>::epsilon();
370 Index n = eivec.cols();
372 for (
Index j = 0; j < n; ++j) {
373 if (internal::isMuchSmallerThan(numext::imag(m_eivalues.coeff(j)), numext::real(m_eivalues.coeff(j)), precision) ||
376 matV.col(j) = eivec.col(j).template cast<ComplexScalar>();
377 matV.col(j).normalize();
380 for (
Index i = 0; i < n; ++i) {
384 matV.col(j).normalize();
385 matV.col(j + 1).normalize();
392template <
typename MatrixType>
393template <
typename InputType>
395 bool computeEigenvectors) {
396 check_template_parameters();
397 eigen_assert(matrix.
cols() == matrix.
rows());
400 m_realSchur.compute(matrix.
derived(), computeEigenvectors);
401 return computeFromSchur(computeEigenvectors);
406template <
typename MatrixType>
407EigenSolver<MatrixType>& EigenSolver<MatrixType>::computeFromSchur(
bool computeEigenvectors) {
408 using numext::isfinite;
410 m_info = m_realSchur.info();
413 const MatrixType& matT = m_realSchur.m_matT;
414 const Index n = matT.cols();
417 m_eivalues.resize(n);
420 if (i == n - 1 || matT.coeff(i + 1, i) == Scalar(0)) {
421 m_eivalues.coeffRef(i) = matT.coeff(i, i);
422 if (!(isfinite)(m_eivalues.coeffRef(i))) {
423 m_isInitialized =
true;
424 m_eigenvectorsOk =
false;
430 Scalar p = Scalar(0.5) * (matT.coeff(i, i) - matT.coeff(i + 1, i + 1));
435 Scalar t0 = matT.coeff(i + 1, i);
436 Scalar t1 = matT.coeff(i, i + 1);
437 Scalar maxval = numext::maxi<Scalar>(numext::abs(p), numext::maxi<Scalar>(numext::abs(t0), numext::abs(t1)));
440 Scalar p0 = p / maxval;
441 z = maxval * numext::sqrt(numext::abs(p0 * p0 + t0 * t1));
444 m_eivalues.coeffRef(i) = ComplexScalar(matT.coeff(i + 1, i + 1) + p, z);
445 m_eivalues.coeffRef(i + 1) = ComplexScalar(matT.coeff(i + 1, i + 1) + p, -z);
446 if (!((isfinite)(m_eivalues.coeffRef(i)) && (isfinite)(m_eivalues.coeffRef(i + 1)))) {
447 m_isInitialized =
true;
448 m_eigenvectorsOk =
false;
457 if (computeEigenvectors) doComputeEigenvectors();
458 }
else if (!m_realSchur.m_matT.allFinite()) {
463 m_isInitialized =
true;
464 m_eigenvectorsOk = computeEigenvectors;
469template <
typename MatrixType>
470void EigenSolver<MatrixType>::doComputeEigenvectors() {
473 MatrixType& matT = m_realSchur.m_matT;
474 PlainMatrixType& eivec = m_realSchur.m_matU;
475 const Index size = eivec.cols();
476 const Scalar eps = NumTraits<Scalar>::epsilon();
478 const Scalar norm = internal::hessenberg_abs_sum<Upper>(matT);
481 if (norm == Scalar(0)) {
485 for (Index n = size - 1; n >= 0; n--) {
486 Scalar p = m_eivalues.coeff(n).real();
487 Scalar q = m_eivalues.coeff(n).imag();
490 if (q == Scalar(0)) {
491 Scalar lastr(0), lastw(0);
494 matT.coeffRef(n, n) = Scalar(1);
495 for (Index i = n - 1; i >= 0; i--) {
496 Scalar w = matT.coeff(i, i) - p;
497 Scalar r = matT.row(i).segment(l, n - l + 1).dot(matT.col(n).segment(l, n - l + 1));
499 if (m_eivalues.coeff(i).imag() < Scalar(0)) {
504 if (m_eivalues.coeff(i).imag() == Scalar(0)) {
506 matT.coeffRef(i, n) = -r / w;
508 matT.coeffRef(i, n) = -r / (eps * norm);
511 Scalar x = matT.coeff(i, i + 1);
512 Scalar y = matT.coeff(i + 1, i);
513 Scalar denom = (m_eivalues.coeff(i).real() - p) * (m_eivalues.coeff(i).real() - p) +
514 m_eivalues.coeff(i).imag() * m_eivalues.coeff(i).imag();
515 Scalar t = (x * lastr - lastw * r) / denom;
516 matT.coeffRef(i, n) = t;
517 if (numext::abs(x) > numext::abs(lastw))
518 matT.coeffRef(i + 1, n) = (-r - w * t) / x;
520 matT.coeffRef(i + 1, n) = (-lastr - y * t) / lastw;
524 Scalar t = numext::abs(matT.coeff(i, n));
525 if ((eps * t) * t > Scalar(1)) matT.col(n).tail(size - i) /= t;
528 }
else if (q < Scalar(0) && n > 0)
530 Scalar lastra(0), lastsa(0), lastw(0);
534 if (numext::abs(matT.coeff(n, n - 1)) > numext::abs(matT.coeff(n - 1, n))) {
535 matT.coeffRef(n - 1, n - 1) = q / matT.coeff(n, n - 1);
536 matT.coeffRef(n - 1, n) = -(matT.coeff(n, n) - p) / matT.coeff(n, n - 1);
539 ComplexScalar(Scalar(0), -matT.coeff(n - 1, n)) / ComplexScalar(matT.coeff(n - 1, n - 1) - p, q);
540 matT.coeffRef(n - 1, n - 1) = numext::real(cc);
541 matT.coeffRef(n - 1, n) = numext::imag(cc);
543 matT.coeffRef(n, n - 1) = Scalar(0);
544 matT.coeffRef(n, n) = Scalar(1);
545 for (Index i = n - 2; i >= 0; i--) {
546 Scalar ra = matT.row(i).segment(l, n - l + 1).dot(matT.col(n - 1).segment(l, n - l + 1));
547 Scalar sa = matT.row(i).segment(l, n - l + 1).dot(matT.col(n).segment(l, n - l + 1));
548 Scalar w = matT.coeff(i, i) - p;
550 if (m_eivalues.coeff(i).imag() < Scalar(0)) {
556 if (m_eivalues.coeff(i).imag() == RealScalar(0)) {
557 ComplexScalar cc = ComplexScalar(-ra, -sa) / ComplexScalar(w, q);
558 matT.coeffRef(i, n - 1) = numext::real(cc);
559 matT.coeffRef(i, n) = numext::imag(cc);
562 Scalar x = matT.coeff(i, i + 1);
563 Scalar y = matT.coeff(i + 1, i);
564 Scalar vr = (m_eivalues.coeff(i).real() - p) * (m_eivalues.coeff(i).real() - p) +
565 m_eivalues.coeff(i).imag() * m_eivalues.coeff(i).imag() - q * q;
566 Scalar vi = (m_eivalues.coeff(i).real() - p) * Scalar(2) * q;
567 if ((vr == Scalar(0)) && (vi == Scalar(0)))
569 eps * norm * (numext::abs(w) + numext::abs(q) + numext::abs(x) + numext::abs(y) + numext::abs(lastw));
571 ComplexScalar cc = ComplexScalar(x * lastra - lastw * ra + q * sa, x * lastsa - lastw * sa - q * ra) /
572 ComplexScalar(vr, vi);
573 matT.coeffRef(i, n - 1) = numext::real(cc);
574 matT.coeffRef(i, n) = numext::imag(cc);
575 if (numext::abs(x) > (numext::abs(lastw) + numext::abs(q))) {
576 matT.coeffRef(i + 1, n - 1) = (-ra - w * matT.coeff(i, n - 1) + q * matT.coeff(i, n)) / x;
577 matT.coeffRef(i + 1, n) = (-sa - w * matT.coeff(i, n) - q * matT.coeff(i, n - 1)) / x;
579 cc = ComplexScalar(-lastra - y * matT.coeff(i, n - 1), -lastsa - y * matT.coeff(i, n)) /
580 ComplexScalar(lastw, q);
581 matT.coeffRef(i + 1, n - 1) = numext::real(cc);
582 matT.coeffRef(i + 1, n) = numext::imag(cc);
587 Scalar t = numext::maxi<Scalar>(numext::abs(matT.coeff(i, n - 1)), numext::abs(matT.coeff(i, n)));
588 if ((eps * t) * t > Scalar(1)) matT.block(i, n - 1, size - i, 2) /= t;
595 eigen_assert(0 &&
"Internal bug in EigenSolver (INF or NaN has not been detected)");
600 for (Index j = size - 1; j >= 0; j--) {
601 m_tmp.noalias() = eivec.leftCols(j + 1) * matT.col(j).segment(0, j + 1);
602 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:205
constexpr const Scalar & coeff(Index rowId, Index colId) const
Definition PlainObjectBase.h:187
ComputationInfo
Definition Constants.h:455
@ NumericalIssue
Definition Constants.h:459
@ Success
Definition Constants.h:457
Definition EigenBase.h:34
constexpr Index cols() const noexcept
Definition EigenBase.h:62
constexpr Derived & derived()
Definition EigenBase.h:50
constexpr Index rows() const noexcept
Definition EigenBase.h:60