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_INTERNAL_ASSERT_SQUARE(matrix);
400 m_realSchur.compute(matrix.
derived(), computeEigenvectors);
401 return computeFromSchur(computeEigenvectors);
406template <
typename MatrixType>
407EigenSolver<MatrixType>& EigenSolver<MatrixType>::computeFromSchur(
bool computeEigenvectors) {
408 m_info = m_realSchur.info();
411 const MatrixType& matT = m_realSchur.m_matT;
412 const Index n = matT.cols();
415 m_eivalues.resize(n);
418 if (i == n - 1 || matT.coeff(i + 1, i) == Scalar(0)) {
419 m_eivalues.coeffRef(i) = matT.coeff(i, i);
420 if (!(numext::isfinite)(m_eivalues.coeffRef(i))) {
421 m_isInitialized =
true;
422 m_eigenvectorsOk =
false;
428 Scalar p = Scalar(0.5) * (matT.coeff(i, i) - matT.coeff(i + 1, i + 1));
433 Scalar t0 = matT.coeff(i + 1, i);
434 Scalar t1 = matT.coeff(i, i + 1);
435 Scalar maxval = numext::maxi<Scalar>(numext::abs(p), numext::maxi<Scalar>(numext::abs(t0), numext::abs(t1)));
438 Scalar p0 = p / maxval;
439 z = maxval * numext::sqrt(numext::abs(p0 * p0 + t0 * t1));
442 m_eivalues.coeffRef(i) = ComplexScalar(matT.coeff(i + 1, i + 1) + p, z);
443 m_eivalues.coeffRef(i + 1) = ComplexScalar(matT.coeff(i + 1, i + 1) + p, -z);
444 if (!((numext::isfinite)(m_eivalues.coeffRef(i)) && (numext::isfinite)(m_eivalues.coeffRef(i + 1)))) {
445 m_isInitialized =
true;
446 m_eigenvectorsOk =
false;
455 if (computeEigenvectors) doComputeEigenvectors();
456 }
else if (!m_realSchur.m_matT.allFinite()) {
461 m_isInitialized =
true;
462 m_eigenvectorsOk = computeEigenvectors;
467template <
typename MatrixType>
468void EigenSolver<MatrixType>::doComputeEigenvectors() {
471 MatrixType& matT = m_realSchur.m_matT;
472 PlainMatrixType& eivec = m_realSchur.m_matU;
473 const Index size = eivec.cols();
474 const Scalar eps = NumTraits<Scalar>::epsilon();
476 const Scalar norm = internal::hessenberg_abs_sum<Upper>(matT);
479 if (norm == Scalar(0)) {
483 for (Index n = size - 1; n >= 0; n--) {
484 Scalar p = m_eivalues.coeff(n).real();
485 Scalar q = m_eivalues.coeff(n).imag();
488 if (q == Scalar(0)) {
489 Scalar lastr(0), lastw(0);
492 matT.coeffRef(n, n) = Scalar(1);
493 for (Index i = n - 1; i >= 0; i--) {
494 Scalar w = matT.coeff(i, i) - p;
495 Scalar r = matT.row(i).segment(l, n - l + 1).dot(matT.col(n).segment(l, n - l + 1));
497 if (m_eivalues.coeff(i).imag() < Scalar(0)) {
502 if (m_eivalues.coeff(i).imag() == Scalar(0)) {
504 matT.coeffRef(i, n) = -r / w;
506 matT.coeffRef(i, n) = -r / (eps * norm);
509 Scalar x = matT.coeff(i, i + 1);
510 Scalar y = matT.coeff(i + 1, i);
511 Scalar denom = (m_eivalues.coeff(i).real() - p) * (m_eivalues.coeff(i).real() - p) +
512 m_eivalues.coeff(i).imag() * m_eivalues.coeff(i).imag();
513 Scalar t = (x * lastr - lastw * r) / denom;
514 matT.coeffRef(i, n) = t;
515 if (numext::abs(x) > numext::abs(lastw))
516 matT.coeffRef(i + 1, n) = (-r - w * t) / x;
518 matT.coeffRef(i + 1, n) = (-lastr - y * t) / lastw;
522 Scalar t = numext::abs(matT.coeff(i, n));
523 if ((eps * t) * t > Scalar(1)) matT.col(n).tail(size - i) /= t;
526 }
else if (q < Scalar(0) && n > 0)
528 Scalar lastra(0), lastsa(0), lastw(0);
532 if (numext::abs(matT.coeff(n, n - 1)) > numext::abs(matT.coeff(n - 1, n))) {
533 matT.coeffRef(n - 1, n - 1) = q / matT.coeff(n, n - 1);
534 matT.coeffRef(n - 1, n) = -(matT.coeff(n, n) - p) / matT.coeff(n, n - 1);
537 ComplexScalar(Scalar(0), -matT.coeff(n - 1, n)) / ComplexScalar(matT.coeff(n - 1, n - 1) - p, q);
538 matT.coeffRef(n - 1, n - 1) = numext::real(cc);
539 matT.coeffRef(n - 1, n) = numext::imag(cc);
541 matT.coeffRef(n, n - 1) = Scalar(0);
542 matT.coeffRef(n, n) = Scalar(1);
543 for (Index i = n - 2; i >= 0; i--) {
544 Scalar ra = matT.row(i).segment(l, n - l + 1).dot(matT.col(n - 1).segment(l, n - l + 1));
545 Scalar sa = matT.row(i).segment(l, n - l + 1).dot(matT.col(n).segment(l, n - l + 1));
546 Scalar w = matT.coeff(i, i) - p;
548 if (m_eivalues.coeff(i).imag() < Scalar(0)) {
554 if (m_eivalues.coeff(i).imag() == RealScalar(0)) {
555 ComplexScalar cc = ComplexScalar(-ra, -sa) / ComplexScalar(w, q);
556 matT.coeffRef(i, n - 1) = numext::real(cc);
557 matT.coeffRef(i, n) = numext::imag(cc);
560 Scalar x = matT.coeff(i, i + 1);
561 Scalar y = matT.coeff(i + 1, i);
562 Scalar vr = (m_eivalues.coeff(i).real() - p) * (m_eivalues.coeff(i).real() - p) +
563 m_eivalues.coeff(i).imag() * m_eivalues.coeff(i).imag() - q * q;
564 Scalar vi = (m_eivalues.coeff(i).real() - p) * Scalar(2) * q;
565 if ((vr == Scalar(0)) && (vi == Scalar(0)))
567 eps * norm * (numext::abs(w) + numext::abs(q) + numext::abs(x) + numext::abs(y) + numext::abs(lastw));
569 ComplexScalar cc = ComplexScalar(x * lastra - lastw * ra + q * sa, x * lastsa - lastw * sa - q * ra) /
570 ComplexScalar(vr, vi);
571 matT.coeffRef(i, n - 1) = numext::real(cc);
572 matT.coeffRef(i, n) = numext::imag(cc);
573 if (numext::abs(x) > (numext::abs(lastw) + numext::abs(q))) {
574 matT.coeffRef(i + 1, n - 1) = (-ra - w * matT.coeff(i, n - 1) + q * matT.coeff(i, n)) / x;
575 matT.coeffRef(i + 1, n) = (-sa - w * matT.coeff(i, n) - q * matT.coeff(i, n - 1)) / x;
577 cc = ComplexScalar(-lastra - y * matT.coeff(i, n - 1), -lastsa - y * matT.coeff(i, n)) /
578 ComplexScalar(lastw, q);
579 matT.coeffRef(i + 1, n - 1) = numext::real(cc);
580 matT.coeffRef(i + 1, n) = numext::imag(cc);
585 Scalar t = numext::maxi<Scalar>(numext::abs(matT.coeff(i, n - 1)), numext::abs(matT.coeff(i, n)));
586 if ((eps * t) * t > Scalar(1)) matT.block(i, n - 1, size - i, 2) /= t;
593 eigen_assert(0 &&
"Internal bug in EigenSolver (INF or NaN has not been detected)");
598 for (Index j = size - 1; j >= 0; j--) {
599 m_tmp.noalias() = eivec.leftCols(j + 1) * matT.col(j).segment(0, j + 1);
600 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 Derived & derived()
Definition EigenBase.h:50