11 #ifndef EIGEN_EIGENSOLVER_H
12 #define EIGEN_EIGENSOLVER_H
148 template<
typename InputType>
279 template<
typename InputType>
325 template<
typename MatrixType>
328 eigen_assert(m_isInitialized &&
"EigenSolver is not initialized.");
330 Index n = m_eivalues.rows();
346 template<
typename MatrixType>
349 eigen_assert(m_isInitialized &&
"EigenSolver is not initialized.");
350 eigen_assert(m_eigenvectorsOk &&
"The eigenvectors have not been computed together with the eigenvalues.");
359 matV.col(
j) = m_eivec.col(
j).template cast<ComplexScalar>();
360 matV.col(
j).normalize();
370 matV.col(
j).normalize();
371 matV.col(
j+1).normalize();
378 template<
typename MatrixType>
379 template<
typename InputType>
383 check_template_parameters();
391 m_realSchur.compute(matrix.
derived(), computeEigenvectors);
393 m_info = m_realSchur.info();
397 m_matT = m_realSchur.matrixT();
398 if (computeEigenvectors)
399 m_eivec = m_realSchur.matrixU();
402 m_eivalues.resize(matrix.
cols());
404 while (
i < matrix.
cols())
406 if (
i == matrix.
cols() - 1 || m_matT.coeff(
i+1,
i) ==
Scalar(0))
408 m_eivalues.coeffRef(
i) = m_matT.coeff(
i,
i);
411 m_isInitialized =
true;
412 m_eigenvectorsOk =
false;
427 Scalar maxval = numext::maxi<Scalar>(
abs(
p),numext::maxi<Scalar>(
abs(t0),
abs(t1)));
438 m_isInitialized =
true;
439 m_eigenvectorsOk =
false;
448 if (computeEigenvectors)
449 doComputeEigenvectors();
452 m_isInitialized =
true;
453 m_eigenvectorsOk = computeEigenvectors;
459 template<
typename MatrixType>
481 Scalar p = m_eivalues.coeff(
n).real();
482 Scalar q = m_eivalues.coeff(
n).imag();
487 Scalar lastr(0), lastw(0);
494 Scalar r = m_matT.row(
i).segment(l,
n-l+1).dot(m_matT.col(
n).segment(l,
n-l+1));
496 if (m_eivalues.coeff(
i).imag() <
Scalar(0))
504 if (m_eivalues.coeff(
i).imag() ==
Scalar(0))
507 m_matT.coeffRef(
i,
n) = -r /
w;
515 Scalar denom = (m_eivalues.coeff(
i).real() -
p) * (m_eivalues.coeff(
i).real() -
p) + m_eivalues.coeff(
i).imag() * m_eivalues.coeff(
i).imag();
516 Scalar t = (
x * lastr - lastw * r) / denom;
517 m_matT.coeffRef(
i,
n) = t;
519 m_matT.coeffRef(
i+1,
n) = (-r -
w * t) /
x;
521 m_matT.coeffRef(
i+1,
n) = (-lastr -
y * t) / lastw;
526 if ((eps * t) * t >
Scalar(1))
527 m_matT.col(
n).tail(
size-
i) /= t;
531 else if (q <
Scalar(0) &&
n > 0)
533 Scalar lastra(0), lastsa(0), lastw(0);
537 if (
abs(m_matT.coeff(
n,
n-1)) >
abs(m_matT.coeff(
n-1,
n)))
539 m_matT.coeffRef(
n-1,
n-1) = q / m_matT.coeff(
n,
n-1);
540 m_matT.coeffRef(
n-1,
n) = -(m_matT.coeff(
n,
n) -
p) / m_matT.coeff(
n,
n-1);
552 Scalar ra = m_matT.row(
i).segment(l,
n-l+1).dot(m_matT.col(
n-1).segment(l,
n-l+1));
553 Scalar sa = m_matT.row(
i).segment(l,
n-l+1).dot(m_matT.col(
n).segment(l,
n-l+1));
556 if (m_eivalues.coeff(
i).imag() <
Scalar(0))
576 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;
586 m_matT.coeffRef(
i+1,
n-1) = (-ra -
w * m_matT.
coeff(
i,
n-1) + q * m_matT.coeff(
i,
n)) /
x;
587 m_matT.coeffRef(
i+1,
n) = (-sa -
w * m_matT.
coeff(
i,
n) - q * m_matT.coeff(
i,
n-1)) /
x;
598 Scalar t = numext::maxi<Scalar>(
abs(m_matT.coeff(
i,
n-1)),
abs(m_matT.coeff(
i,
n)));
599 if ((eps * t) * t >
Scalar(1))
600 m_matT.block(
i,
n-1,
size-
i, 2) /= t;
610 eigen_assert(0 &&
"Internal bug in EigenSolver (INF or NaN has not been detected)");
617 m_tmp.noalias() = m_eivec.leftCols(
j+1) * m_matT.col(
j).segment(0,
j+1);
618 m_eivec.col(
j) = m_tmp;
const AbsReturnType abs() const
const SqrtReturnType sqrt() const
const ImagReturnType imag() const
RealReturnType real() const
#define EIGEN_STATIC_ASSERT(X, MSG)
#define EIGEN_STATIC_ASSERT_NON_INTEGER(TYPE)
Matrix< float, 1, Dynamic > MatrixType
internal::traits< Derived >::Scalar Scalar
Computes eigenvalues and eigenvectors of general matrices.
MatrixType::Scalar Scalar
Scalar type for matrices of type MatrixType.
EigenSolver(const EigenBase< InputType > &matrix, bool computeEigenvectors=true)
Constructor; computes eigendecomposition of given matrix.
const EigenvalueType & eigenvalues() const
Returns the eigenvalues of given matrix.
void doComputeEigenvectors()
EigenSolver(Index size)
Default constructor with memory preallocation.
Matrix< Scalar, ColsAtCompileTime, 1, Options &~RowMajor, MaxColsAtCompileTime, 1 > ColumnVectorType
MatrixType pseudoEigenvalueMatrix() const
Returns the block-diagonal matrix in the pseudo-eigendecomposition.
RealSchur< MatrixType > m_realSchur
ComputationInfo info() const
EigenSolver & setMaxIterations(Index maxIters)
Sets the maximum number of iterations allowed.
EigenvectorsType eigenvectors() const
Returns the eigenvectors of given matrix.
std::complex< RealScalar > ComplexScalar
Complex scalar type for MatrixType.
EigenSolver()
Default constructor.
Matrix< ComplexScalar, ColsAtCompileTime, 1, Options &~RowMajor, MaxColsAtCompileTime, 1 > EigenvalueType
Type for vector of eigenvalues as returned by eigenvalues().
NumTraits< Scalar >::Real RealScalar
static void check_template_parameters()
EigenSolver & compute(const EigenBase< InputType > &matrix, bool computeEigenvectors=true)
Computes eigendecomposition of given matrix.
const MatrixType & pseudoEigenvectors() const
Returns the pseudo-eigenvectors of given matrix.
Index getMaxIterations()
Returns the maximum number of iterations.
Matrix< ComplexScalar, RowsAtCompileTime, ColsAtCompileTime, Options, MaxRowsAtCompileTime, MaxColsAtCompileTime > EigenvectorsType
Type for matrix of eigenvectors as returned by eigenvectors().
MatrixType_ MatrixType
Synonym for the template parameter MatrixType_.
EigenvalueType m_eivalues
constexpr const Scalar & coeff(Index rowId, Index colId) const
constexpr Scalar & coeffRef(Index rowId, Index colId)
RealSchur & setMaxIterations(Index maxIters)
Sets the maximum number of iterations allowed.
Index getMaxIterations()
Returns the maximum number of iterations.
bfloat16() max(const bfloat16 &a, const bfloat16 &b)
bool isMuchSmallerThan(const Scalar &x, const OtherScalar &y, const typename NumTraits< Scalar >::Real &precision=NumTraits< Scalar >::dummy_precision())
EIGEN_ALWAYS_INLINE bool() isfinite(const Eigen::bfloat16 &h)
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_isfinite_op< typename Derived::Scalar >, const Derived > isfinite(const Eigen::ArrayBase< Derived > &x)
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_abs_op< typename Derived::Scalar >, const Derived > abs(const Eigen::ArrayBase< Derived > &x)
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_sqrt_op< typename Derived::Scalar >, const Derived > sqrt(const Eigen::ArrayBase< Derived > &x)
EIGEN_CONSTEXPR Index cols() const EIGEN_NOEXCEPT
EIGEN_CONSTEXPR Index rows() const EIGEN_NOEXCEPT
Holds information about the various numeric (i.e. scalar) types allowed by Eigen.