11 #ifndef EIGEN_EIGENSOLVER_H 12 #define EIGEN_EIGENSOLVER_H 14 #include "./RealSchur.h" 72 RowsAtCompileTime = MatrixType::RowsAtCompileTime,
73 ColsAtCompileTime = MatrixType::ColsAtCompileTime,
74 Options = MatrixType::Options,
75 MaxRowsAtCompileTime = MatrixType::MaxRowsAtCompileTime,
76 MaxColsAtCompileTime = MatrixType::MaxColsAtCompileTime
80 typedef typename MatrixType::Scalar
Scalar;
113 EigenSolver() : m_eivec(), m_eivalues(), m_isInitialized(false), m_realSchur(), m_matT(), m_tmp() {}
122 : m_eivec(size, size),
124 m_isInitialized(false),
125 m_eigenvectorsOk(false),
146 template<
typename InputType>
148 : m_eivec(matrix.rows(), matrix.cols()),
149 m_eivalues(matrix.cols()),
150 m_isInitialized(false),
151 m_eigenvectorsOk(false),
152 m_realSchur(matrix.cols()),
153 m_matT(matrix.rows(), matrix.cols()),
201 eigen_assert(m_isInitialized &&
"EigenSolver is not initialized.");
202 eigen_assert(m_eigenvectorsOk &&
"The eigenvectors have not been computed together with the eigenvalues.");
246 eigen_assert(m_isInitialized &&
"EigenSolver is not initialized.");
277 template<
typename InputType>
283 eigen_assert(m_isInitialized &&
"EigenSolver is not initialized.");
301 void doComputeEigenvectors();
305 static void check_template_parameters()
307 EIGEN_STATIC_ASSERT_NON_INTEGER(Scalar);
312 EigenvalueType m_eivalues;
313 bool m_isInitialized;
314 bool m_eigenvectorsOk;
320 ColumnVectorType m_tmp;
323 template<
typename MatrixType>
326 eigen_assert(m_isInitialized &&
"EigenSolver is not initialized.");
328 Index n = m_eivalues.rows();
330 for (
Index i=0; i<n; ++i)
332 if (internal::isMuchSmallerThan(numext::imag(m_eivalues.
coeff(i)), numext::real(m_eivalues.
coeff(i)), precision))
333 matD.coeffRef(i,i) = numext::real(m_eivalues.
coeff(i));
336 matD.template block<2,2>(i,i) << numext::real(m_eivalues.
coeff(i)), numext::imag(m_eivalues.
coeff(i)),
337 -numext::imag(m_eivalues.
coeff(i)), numext::real(m_eivalues.
coeff(i));
344 template<
typename MatrixType>
347 eigen_assert(m_isInitialized &&
"EigenSolver is not initialized.");
348 eigen_assert(m_eigenvectorsOk &&
"The eigenvectors have not been computed together with the eigenvalues.");
350 Index n = m_eivec.cols();
352 for (
Index j=0; j<n; ++j)
354 if (internal::isMuchSmallerThan(numext::imag(m_eivalues.
coeff(j)), numext::real(m_eivalues.
coeff(j)), precision) || j+1==n)
357 matV.col(j) = m_eivec.col(j).template cast<ComplexScalar>();
358 matV.col(j).normalize();
363 for (
Index i=0; i<n; ++i)
368 matV.col(j).normalize();
369 matV.col(j+1).normalize();
376 template<
typename MatrixType>
377 template<
typename InputType>
381 check_template_parameters();
385 using numext::isfinite;
386 eigen_assert(matrix.
cols() == matrix.
rows());
391 m_info = m_realSchur.
info();
395 m_matT = m_realSchur.
matrixT();
396 if (computeEigenvectors)
397 m_eivec = m_realSchur.
matrixU();
402 while (i < matrix.
cols())
404 if (i == matrix.
cols() - 1 || m_matT.coeff(i+1, i) ==
Scalar(0))
406 m_eivalues.
coeffRef(i) = m_matT.coeff(i, i);
407 if(!(isfinite)(m_eivalues.
coeffRef(i)))
409 m_isInitialized =
true;
410 m_eigenvectorsOk =
false;
418 Scalar p =
Scalar(0.5) * (m_matT.coeff(i, i) - m_matT.coeff(i+1, i+1));
423 Scalar t0 = m_matT.coeff(i+1, i);
424 Scalar t1 = m_matT.coeff(i, i+1);
425 Scalar maxval = numext::maxi<Scalar>(abs(p),numext::maxi<Scalar>(abs(t0),abs(t1)));
429 z = maxval * sqrt(abs(p0 * p0 + t0 * t1));
434 if(!((isfinite)(m_eivalues.
coeffRef(i)) && (isfinite)(m_eivalues.
coeffRef(i+1))))
436 m_isInitialized =
true;
437 m_eigenvectorsOk =
false;
446 if (computeEigenvectors)
447 doComputeEigenvectors();
450 m_isInitialized =
true;
451 m_eigenvectorsOk = computeEigenvectors;
457 template<
typename MatrixType>
461 const Index size = m_eivec.cols();
466 for (
Index j = 0; j < size; ++j)
468 norm += m_matT.row(j).segment((std::max)(j-1,
Index(0)), size-(std::max)(j-1,
Index(0))).cwiseAbs().sum();
477 for (
Index n = size-1; n >= 0; n--)
485 Scalar lastr(0), lastw(0);
488 m_matT.coeffRef(n,n) =
Scalar(1);
489 for (
Index i = n-1; i >= 0; i--)
491 Scalar w = m_matT.coeff(i,i) - p;
492 Scalar r = m_matT.row(i).segment(l,n-l+1).dot(m_matT.col(n).segment(l, n-l+1));
505 m_matT.coeffRef(i,n) = -r / w;
507 m_matT.coeffRef(i,n) = -r / (eps * norm);
511 Scalar x = m_matT.coeff(i,i+1);
512 Scalar y = m_matT.coeff(i+1,i);
513 Scalar denom = (m_eivalues.
coeff(i).real() - p) * (m_eivalues.
coeff(i).real() - p) + m_eivalues.
coeff(i).imag() * m_eivalues.
coeff(i).imag();
514 Scalar t = (x * lastr - lastw * r) / denom;
515 m_matT.coeffRef(i,n) = t;
516 if (abs(x) > abs(lastw))
517 m_matT.coeffRef(i+1,n) = (-r - w * t) / x;
519 m_matT.coeffRef(i+1,n) = (-lastr - y * t) / lastw;
523 Scalar t = abs(m_matT.coeff(i,n));
524 if ((eps * t) * t >
Scalar(1))
525 m_matT.col(n).tail(size-i) /= t;
529 else if (q <
Scalar(0) && n > 0)
531 Scalar lastra(0), lastsa(0), lastw(0);
535 if (abs(m_matT.coeff(n,n-1)) > abs(m_matT.coeff(n-1,n)))
537 m_matT.coeffRef(n-1,n-1) = q / m_matT.coeff(n,n-1);
538 m_matT.coeffRef(n-1,n) = -(m_matT.coeff(n,n) - p) / m_matT.coeff(n,n-1);
543 m_matT.coeffRef(n-1,n-1) = numext::real(cc);
544 m_matT.coeffRef(n-1,n) = numext::imag(cc);
546 m_matT.coeffRef(n,n-1) =
Scalar(0);
547 m_matT.coeffRef(n,n) =
Scalar(1);
548 for (
Index i = n-2; i >= 0; i--)
550 Scalar ra = m_matT.row(i).segment(l, n-l+1).dot(m_matT.col(n-1).segment(l, n-l+1));
551 Scalar sa = m_matT.row(i).segment(l, n-l+1).dot(m_matT.col(n).segment(l, n-l+1));
552 Scalar w = m_matT.coeff(i,i) - p;
563 if (m_eivalues.
coeff(i).imag() == RealScalar(0))
566 m_matT.coeffRef(i,n-1) = numext::real(cc);
567 m_matT.coeffRef(i,n) = numext::imag(cc);
572 Scalar x = m_matT.coeff(i,i+1);
573 Scalar y = m_matT.coeff(i+1,i);
574 Scalar vr = (m_eivalues.
coeff(i).real() - p) * (m_eivalues.
coeff(i).real() - p) + m_eivalues.
coeff(i).imag() * m_eivalues.
coeff(i).imag() - q * q;
577 vr = eps * norm * (abs(w) + abs(q) + abs(x) + abs(y) + abs(lastw));
580 m_matT.coeffRef(i,n-1) = numext::real(cc);
581 m_matT.coeffRef(i,n) = numext::imag(cc);
582 if (abs(x) > (abs(lastw) + abs(q)))
584 m_matT.coeffRef(i+1,n-1) = (-ra - w * m_matT.coeff(i,n-1) + q * m_matT.coeff(i,n)) / x;
585 m_matT.coeffRef(i+1,n) = (-sa - w * m_matT.coeff(i,n) - q * m_matT.coeff(i,n-1)) / x;
590 m_matT.coeffRef(i+1,n-1) = numext::real(cc);
591 m_matT.coeffRef(i+1,n) = numext::imag(cc);
596 Scalar t = numext::maxi<Scalar>(abs(m_matT.coeff(i,n-1)),abs(m_matT.coeff(i,n)));
597 if ((eps * t) * t >
Scalar(1))
598 m_matT.block(i, n-1, size-i, 2) /= t;
608 eigen_assert(0 &&
"Internal bug in EigenSolver (INF or NaN has not been detected)");
613 for (
Index j = size-1; j >= 0; j--)
615 m_tmp.noalias() = m_eivec.leftCols(j+1) * m_matT.col(j).segment(0, j+1);
616 m_eivec.col(j) = m_tmp;
622 #endif // EIGEN_EIGENSOLVER_H MatrixType pseudoEigenvalueMatrix() const
Returns the block-diagonal matrix in the pseudo-eigendecomposition.
Definition: EigenSolver.h:324
const MatrixType & pseudoEigenvectors() const
Returns the pseudo-eigenvectors of given matrix.
Definition: EigenSolver.h:199
Matrix< ComplexScalar, RowsAtCompileTime, ColsAtCompileTime, Options, MaxRowsAtCompileTime, MaxColsAtCompileTime > EigenvectorsType
Type for matrix of eigenvectors as returned by eigenvectors().
Definition: EigenSolver.h:104
EigenSolver & compute(const EigenBase< InputType > &matrix, bool computeEigenvectors=true)
Computes eigendecomposition of given matrix.
EigenSolver(Index size)
Default constructor with memory preallocation.
Definition: EigenSolver.h:121
EIGEN_DEVICE_FUNC Index rows() const
Definition: EigenBase.h:58
Namespace containing all symbols from the Eigen library.
Definition: bench_norm.cpp:85
Holds information about the various numeric (i.e.
Definition: NumTraits.h:150
Index getMaxIterations()
Returns the maximum number of iterations.
Definition: EigenSolver.h:295
Eigen::Index Index
Definition: EigenSolver.h:82
Index getMaxIterations()
Returns the maximum number of iterations.
Definition: RealSchur.h:213
Common base class for all classes T such that MatrixBase has an operator=(T) and a constructor Matrix...
Definition: EigenBase.h:28
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar & coeffRef(Index rowId, Index colId)
This is an overloaded version of DenseCoeffsBase<Derived,WriteAccessors>::coeffRef(Index,Index) const provided to by-pass the creation of an evaluator of the expression, thus saving compilation efforts.
Definition: PlainObjectBase.h:177
EigenSolver(const EigenBase< InputType > &matrix, bool computeEigenvectors=true)
Constructor; computes eigendecomposition of given matrix.
Definition: EigenSolver.h:147
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void resize(Index rows, Index cols)
Resizes *this to a rows x cols matrix.
Definition: PlainObjectBase.h:273
ComputationInfo info() const
Definition: EigenSolver.h:281
The provided data did not satisfy the prerequisites.
Definition: Constants.h:434
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar & coeff(Index rowId, Index colId) const
This is an overloaded version of DenseCoeffsBase<Derived,ReadOnlyAccessors>::coeff(Index,Index) const provided to by-pass the creation of an evaluator of the expression, thus saving compilation efforts.
Definition: PlainObjectBase.h:154
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:33
RealSchur & compute(const EigenBase< InputType > &matrix, bool computeU=true)
Computes Schur decomposition of given matrix.
EigenSolver & setMaxIterations(Index maxIters)
Sets the maximum number of iterations allowed.
Definition: EigenSolver.h:288
std::complex< RealScalar > ComplexScalar
Complex scalar type for MatrixType.
Definition: EigenSolver.h:90
const MatrixType & matrixT() const
Returns the quasi-triangular matrix in the Schur decomposition.
Definition: RealSchur.h:144
Computation was successful.
Definition: Constants.h:432
_MatrixType MatrixType
Synonym for the template parameter _MatrixType.
Definition: EigenSolver.h:69
EigenvectorsType eigenvectors() const
Returns the eigenvectors of given matrix.
Definition: EigenSolver.h:345
EigenSolver()
Default constructor.
Definition: EigenSolver.h:113
const MatrixType & matrixU() const
Returns the orthogonal matrix in the Schur decomposition.
Definition: RealSchur.h:127
RealSchur & setMaxIterations(Index maxIters)
Sets the maximum number of iterations allowed.
Definition: RealSchur.h:206
EIGEN_DEVICE_FUNC Index cols() const
Definition: EigenBase.h:61
MatrixType::Scalar Scalar
Scalar type for matrices of type MatrixType.
Definition: EigenSolver.h:80
const EigenvalueType & eigenvalues() const
Returns the eigenvalues of given matrix.
Definition: EigenSolver.h:244
Definition: EigenSolver.h:64
ComputationInfo info() const
Reports whether previous computation was successful.
Definition: RealSchur.h:195
ComputationInfo
Enum for reporting the status of a computation.
Definition: Constants.h:430
EIGEN_DEVICE_FUNC Derived & derived()
Definition: EigenBase.h:44
Matrix< ComplexScalar, ColsAtCompileTime, 1, Options &~RowMajor, MaxColsAtCompileTime, 1 > EigenvalueType
Type for vector of eigenvalues as returned by eigenvalues().
Definition: EigenSolver.h:97