12#ifndef EIGEN_SELFADJOINTEIGENSOLVER_H
13#define EIGEN_SELFADJOINTEIGENSOLVER_H
15#include "./Tridiagonalization.h"
18#include "./InternalHeaderCheck.h"
22template <
typename MatrixType_>
26template <
typename SolverType,
int Size,
bool IsComplex,
bool IsDirect = !IsComplex && (Size == 2 || Size == 3)>
27struct direct_selfadjoint_eigenvalues;
29template <
bool PerBlockScaling,
typename MatrixType,
typename DiagType,
typename SubDiagType>
30EIGEN_DEVICE_FUNC
ComputationInfo computeFromTridiagonal_impl(DiagType& diag, SubDiagType& subdiag,
31 const Index maxIterations,
bool computeEigenvectors,
83template <
typename MatrixType_>
86 using MatrixType = MatrixType_;
88 Size = MatrixType::RowsAtCompileTime,
89 ColsAtCompileTime = MatrixType::ColsAtCompileTime,
90 Options = internal::plain_object_options<MatrixType>::value,
91 MaxColsAtCompileTime = MatrixType::MaxColsAtCompileTime
95 using Scalar =
typename MatrixType::Scalar;
108 std::conditional_t<internal::is_ref<MatrixType>::value, MatrixType,
126 using
VectorType = typename internal::plain_col_type<MatrixType, Scalar>::type;
127 using RealVectorType = typename internal::plain_col_type<MatrixType, RealScalar>::type;
129 using SubDiagonalType = typename TridiagonalizationType::SubDiagonalType;
147 m_info(InvalidInput),
148 m_isInitialized(false),
149 m_eigenvectorsOk(false) {}
164 : m_eivec(size, size),
167 m_subdiag(size > 1 ? size - 1 : 1),
168 m_hcoeffs(size > 1 ? size - 1 : 1),
169 m_isInitialized(false),
170 m_eigenvectorsOk(false) {}
188 template <
typename InputType>
191 : m_eivec(matrix.rows(), matrix.cols()),
192 m_workspace(matrix.cols()),
193 m_eivalues(matrix.cols()),
194 m_subdiag(matrix.rows() > 1 ? matrix.rows() - 1 : 1),
195 m_hcoeffs(matrix.cols() > 1 ? matrix.cols() - 1 : 1),
196 m_isInitialized(false),
197 m_eigenvectorsOk(false) {
213 template <typename InputType, bool IsRef = internal::is_ref<MatrixType>::value, std::enable_if_t<IsRef, int> = 0>
216 m_eivec.template triangularView<StrictlyUpper>().setZero();
217 computeInPlace(options);
251 template <
typename InputType>
322 eigen_assert(m_isInitialized &&
"SelfAdjointEigenSolver is not initialized.");
323 eigen_assert(m_eigenvectorsOk &&
"The eigenvectors have not been computed together with the eigenvalues.");
343 eigen_assert(m_isInitialized &&
"SelfAdjointEigenSolver is not initialized.");
365 eigen_assert(m_isInitialized &&
"SelfAdjointEigenSolver is not initialized.");
366 eigen_assert(m_eigenvectorsOk &&
"The eigenvectors have not been computed together with the eigenvalues.");
367 return m_eivec * m_eivalues.cwiseSqrt().asDiagonal() * m_eivec.adjoint();
381 eigen_assert(m_isInitialized &&
"SelfAdjointEigenSolver is not initialized.");
382 eigen_assert(m_eigenvectorsOk &&
"The eigenvectors have not been computed together with the eigenvalues.");
383 return m_eivec * m_eivalues.array().exp().matrix().asDiagonal() * m_eivec.adjoint();
405 eigen_assert(m_isInitialized &&
"SelfAdjointEigenSolver is not initialized.");
406 eigen_assert(m_eigenvectorsOk &&
"The eigenvectors have not been computed together with the eigenvalues.");
407 return m_eivec * m_eivalues.cwiseInverse().cwiseSqrt().asDiagonal() * m_eivec.adjoint();
415 eigen_assert(m_isInitialized &&
"SelfAdjointEigenSolver is not initialized.");
427 EIGEN_STATIC_ASSERT_NON_INTEGER(
Scalar)
430 struct BindStorageTag {};
434 template <
typename InputType>
436 : m_eivec(matrix.derived()),
437 m_workspace(matrix.cols()),
438 m_eivalues(matrix.cols()),
439 m_subdiag(matrix.rows() > 1 ? matrix.rows() - 1 : 1),
440 m_hcoeffs(matrix.cols() > 1 ? matrix.cols() - 1 : 1),
441 m_isInitialized(false),
442 m_eigenvectorsOk(false) {}
450 RealVectorType m_eivalues;
451 typename TridiagonalizationType::SubDiagonalType m_subdiag;
452 typename TridiagonalizationType::CoeffVectorType m_hcoeffs;
453 ComputationInfo m_info;
454 bool m_isInitialized;
455 bool m_eigenvectorsOk;
477template <
typename RealScalar,
typename Index,
typename MatrixQType>
478EIGEN_DEVICE_FUNC
static void tridiagonal_qr_step(RealScalar* diag, RealScalar* subdiag, Index start, Index end,
479 MatrixQType* matrixQ);
482template <
typename MatrixType>
483template <
typename InputType>
486 const InputType& matrix(a_matrix.derived());
487 EIGEN_INTERNAL_ASSERT_SQUARE(matrix);
488 m_eivec = matrix.template triangularView<Lower>();
489 return computeInPlace(options);
492template <
typename MatrixType>
494 EIGEN_INTERNAL_ASSERT_SQUARE(m_eivec);
495 eigen_assert((options & ~(EigVecMask | GenEigMask)) == 0 && (options & EigVecMask) != EigVecMask &&
496 "invalid option parameter");
498 Index n = m_eivec.cols();
499 m_eivalues.resize(n, 1);
502 if (n == 1) m_eivalues.coeffRef(0, 0) = numext::real(m_eivec.coeff(0, 0));
503 if (computeEigenvectors) m_eivec.setOnes();
505 m_isInitialized =
true;
506 m_eigenvectorsOk = computeEigenvectors;
511 RealVectorType& diag = m_eivalues;
512 EigenvectorsType& mat = m_eivec;
516 EIGEN_IF_CONSTEXPR (NumTraits<Scalar>::IsComplex) {
517 mat.diagonal() = mat.diagonal().real();
525 const RealScalar maxCoeff = internal::safe_scaling<RealScalar>::max_abs_component(mat);
526 if (!(numext::isfinite)(maxCoeff)) {
529 m_isInitialized =
true;
530 m_eigenvectorsOk =
false;
533 const auto factors = internal::safe_scaling<RealScalar>::with_scaled(mat, maxCoeff, [&](
const auto& scaled) {
534 mat.template triangularView<Lower>() = scaled.template triangularView<Lower>();
536 m_subdiag.resize(n - 1);
537 m_hcoeffs.resize(n - 1);
538 internal::tridiagonalization_inplace(mat, diag, m_subdiag, m_hcoeffs, m_workspace, computeEigenvectors);
540 m_info = internal::computeFromTridiagonal_impl<false>(diag, m_subdiag, m_maxIterations, computeEigenvectors, m_eivec);
543 internal::safe_scaling<RealScalar>::unscale_in_place(m_eivalues, maxCoeff, factors);
545 m_isInitialized =
true;
546 m_eigenvectorsOk = computeEigenvectors;
550template <
typename MatrixType>
552 const RealVectorType& diag,
const SubDiagonalType& subdiag,
int options) {
561 if (m_eivalues.size() > 0) scale = m_eivalues.cwiseAbs().maxCoeff();
562 if (m_subdiag.size() > 0) scale = numext::maxi(scale, m_subdiag.cwiseAbs().maxCoeff());
563 if (!(numext::isfinite)(scale)) {
565 m_isInitialized =
true;
566 m_eigenvectorsOk =
false;
571 if (computeEigenvectors) {
572 m_eivec.setIdentity(diag.size(), diag.size());
577 internal::computeFromTridiagonal_impl<true>(m_eivalues, m_subdiag,
m_maxIterations, computeEigenvectors, m_eivec);
579 m_isInitialized =
true;
580 m_eigenvectorsOk = computeEigenvectors;
600template <
bool PerBlockScaling,
typename MatrixType,
typename DiagType,
typename SubDiagType>
601EIGEN_DEVICE_FUNC
ComputationInfo computeFromTridiagonal_impl(DiagType& diag, SubDiagType& subdiag,
602 const Index maxIterations,
bool computeEigenvectors,
606 Index n = diag.size();
611 using RealScalar =
typename DiagType::RealScalar;
612 const RealScalar considerAsZero = (std::numeric_limits<RealScalar>::min)();
613 const RealScalar precision = NumTraits<RealScalar>::epsilon();
614 const RealScalar precision_inv = RealScalar(1) / precision;
617 auto deflate = [&](Index lo, Index hi) {
618 for (Index i = lo; i < hi; ++i) {
619 const RealScalar absSubdiag = numext::abs(subdiag[i]);
620 if (absSubdiag < considerAsZero) {
621 subdiag[i] = RealScalar(0);
622 }
else if (!PerBlockScaling) {
625 if (absSubdiag <= precision * numext::maxi(numext::abs(diag[i]), numext::abs(diag[i + 1]))) {
626 subdiag[i] = RealScalar(0);
629 const RealScalar scaled_subdiag = precision_inv * subdiag[i];
630 if (scaled_subdiag * scaled_subdiag <= (numext::abs(diag[i]) + numext::abs(diag[i + 1]))) {
631 subdiag[i] = RealScalar(0);
641 Index scaled_start = -1, scaled_end = -1;
642 RealScalar block_norm = RealScalar(0);
643 safe_scaling_factors<RealScalar> blockFactors;
644 const auto restore_block = [&]() {
645 if (scaled_start < 0)
return;
646 auto diagonal = diag.segment(scaled_start, scaled_end - scaled_start + 1);
647 auto offDiagonal = subdiag.segment(scaled_start, scaled_end - scaled_start);
648 safe_scaling<RealScalar>::unscale_in_place(diagonal, block_norm, blockFactors);
649 safe_scaling<RealScalar>::unscale_in_place(offDiagonal, block_norm, blockFactors);
656 while (end > 0 && numext::is_exactly_zero(subdiag[end - 1])) {
663 if (iter > maxIterations * n)
break;
666 while (start > 0 && !numext::is_exactly_zero(subdiag[start - 1])) start--;
668 if (PerBlockScaling) {
670 if (start != scaled_start || end != scaled_end) {
673 block_norm = RealScalar(0);
674 for (Index i = start; i <= end; ++i) block_norm = numext::maxi(block_norm, numext::abs(diag[i]));
675 for (Index i = start; i < end; ++i) block_norm = numext::maxi(block_norm, numext::abs(subdiag[i]));
676 auto diagonal = diag.segment(start, end - start + 1);
677 auto offDiagonal = subdiag.segment(start, end - start);
678 blockFactors = safe_scaling<RealScalar>::scale_to(diagonal, diagonal, block_norm);
679 safe_scaling<RealScalar>::scale_to(offDiagonal, offDiagonal, block_norm, blockFactors);
680 scaled_start = start;
685 internal::tridiagonal_qr_step(diag.data(), subdiag.data(), start, end,
686 computeEigenvectors ? &eivec :
static_cast<MatrixType*
>(
nullptr));
690 if (PerBlockScaling) restore_block();
691 if (iter <= maxIterations * n)
699 for (Index i = 0; i < n - 1; ++i) {
705 RealScalar min_val = diag[i];
706 for (Index j = i + 1; j < n; ++j) {
707 if (diag[j] < min_val) {
713 numext::swap(diag[i], diag[k]);
714 if (computeEigenvectors) eivec.col(i).swap(eivec.col(k));
721template <
typename SolverType,
int Size,
bool IsComplex,
bool IsDirect>
722struct direct_selfadjoint_eigenvalues {
723 EIGEN_DEVICE_FUNC
static inline void run(SolverType& eig,
const typename SolverType::MatrixType& A,
int options) {
724 eig.compute(A, options);
728template <
typename SolverType,
int Size>
729struct direct_selfadjoint_eigensolver_kernel;
731template <
typename SolverType>
732struct direct_selfadjoint_eigensolver_kernel<SolverType, 3> {
733 using MatrixType =
typename SolverType::MatrixType;
734 using VectorType =
typename SolverType::RealVectorType;
735 using Scalar =
typename SolverType::Scalar;
736 using EigenvectorsType =
typename SolverType::EigenvectorsType;
737 using PlainMatrixType =
typename SolverType::PlainMatrixType;
743 EIGEN_DEVICE_FUNC
static inline void computeRoots(
const MatrixType& m, VectorType& roots) {
744 const Scalar s_inv3 = Scalar(1) / Scalar(3);
745 const Scalar s_sqrt3 = numext::sqrt(Scalar(3));
750 Scalar c0 = m(0, 0) * m(1, 1) * m(2, 2) + Scalar(2) * m(1, 0) * m(2, 0) * m(2, 1) - m(0, 0) * m(2, 1) * m(2, 1) -
751 m(1, 1) * m(2, 0) * m(2, 0) - m(2, 2) * m(1, 0) * m(1, 0);
752 Scalar c1 = m(0, 0) * m(1, 1) - m(1, 0) * m(1, 0) + m(0, 0) * m(2, 2) - m(2, 0) * m(2, 0) + m(1, 1) * m(2, 2) -
754 Scalar c2 = m(0, 0) + m(1, 1) + m(2, 2);
758 Scalar c2_over_3 = c2 * s_inv3;
759 Scalar a_over_3 = (c2 * c2_over_3 - c1) * s_inv3;
760 a_over_3 = numext::maxi(a_over_3, Scalar(0));
762 Scalar half_b = Scalar(0.5) * (c0 + c2_over_3 * (Scalar(2) * c2_over_3 * c2_over_3 - c1));
764 Scalar q = a_over_3 * a_over_3 * a_over_3 - half_b * half_b;
765 q = numext::maxi(q, Scalar(0));
768 Scalar rho = numext::sqrt(a_over_3);
769 Scalar theta = numext::atan2(numext::sqrt(q), half_b) *
771 Scalar cos_theta = numext::cos(theta);
772 Scalar sin_theta = numext::sin(theta);
774 roots(0) = c2_over_3 - rho * (cos_theta + s_sqrt3 * sin_theta);
775 roots(1) = c2_over_3 - rho * (cos_theta - s_sqrt3 * sin_theta);
776 roots(2) = c2_over_3 + Scalar(2) * rho * cos_theta;
779 EIGEN_DEVICE_FUNC
static inline bool extract_kernel(PlainMatrixType& mat, Ref<VectorType> res,
780 Ref<VectorType> representative) {
783 mat.diagonal().cwiseAbs().maxCoeff(&i0);
786 representative = mat.col(i0);
789 n0 = (c0 = representative.cross(mat.col((i0 + 1) % 3))).squaredNorm();
790 n1 = (c1 = representative.cross(mat.col((i0 + 2) % 3))).squaredNorm();
792 res = c0 / numext::sqrt(n0);
794 res = c1 / numext::sqrt(n1);
799 EIGEN_DEVICE_FUNC
static void run(PlainMatrixType& scaledMat, VectorType& eivals, EigenvectorsType& eivecs,
800 bool computeEigenvectors,
const Scalar& maxCoeff) {
802 computeRoots(scaledMat, eivals);
807 if (eivals(0) > eivals(1)) numext::swap(eivals(0), eivals(1));
808 if (eivals(1) > eivals(2)) numext::swap(eivals(1), eivals(2));
809 if (eivals(0) > eivals(1)) numext::swap(eivals(0), eivals(1));
812 if (computeEigenvectors) {
813 if ((eivals(2) - eivals(0)) <= Eigen::NumTraits<Scalar>::epsilon() * maxCoeff) {
815 eivecs.setIdentity();
821 Scalar d0 = eivals(2) - eivals(1);
822 Scalar d1 = eivals(1) - eivals(0);
831 tmp.diagonal().array() -= eivals(k);
833 extract_kernel(tmp, eivecs.col(k), eivecs.col(l));
837 if (d0 <= 2 * Eigen::NumTraits<Scalar>::epsilon() * d1) {
840 eivecs.col(l) -= eivecs.col(k).dot(eivecs.col(l)) * eivecs.col(k);
841 eivecs.col(l).normalize();
844 tmp.diagonal().array() -= eivals(l);
847 extract_kernel(tmp, eivecs.col(l), dummy);
851 eivecs.col(1) = eivecs.col(2).cross(eivecs.col(0)).normalized();
858template <
typename SolverType>
859struct direct_selfadjoint_eigensolver_kernel<SolverType, 2> {
860 using MatrixType =
typename SolverType::MatrixType;
861 using VectorType =
typename SolverType::RealVectorType;
862 using Scalar =
typename SolverType::Scalar;
863 using EigenvectorsType =
typename SolverType::EigenvectorsType;
864 using PlainMatrixType =
typename SolverType::PlainMatrixType;
866 EIGEN_DEVICE_FUNC
static inline void computeRoots(
const MatrixType& m, VectorType& roots) {
868 Scalar(0.5) * numext::sqrt<Scalar>(numext::abs2(m(0, 0) - m(1, 1)) + Scalar(4) * numext::abs2(m(1, 0)));
869 const Scalar t1 = Scalar(0.5) * (m(0, 0) + m(1, 1));
874 EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
static void run(PlainMatrixType& scaledMat, VectorType& eivals,
875 EigenvectorsType& eivecs,
bool computeEigenvectors,
878 computeRoots(scaledMat, eivals);
881 if (computeEigenvectors) {
882 if ((eivals(1) - eivals(0)) <= numext::abs(eivals(1)) * Eigen::NumTraits<Scalar>::epsilon()) {
883 eivecs.setIdentity();
885 scaledMat.diagonal().array() -= eivals(1);
886 Scalar a2 = numext::abs2(scaledMat(0, 0));
887 Scalar c2 = numext::abs2(scaledMat(1, 1));
888 Scalar b2 = numext::abs2(scaledMat(1, 0));
890 eivecs.col(1) << -scaledMat(1, 0), scaledMat(0, 0);
891 eivecs.col(1) /= numext::sqrt<Scalar>(a2 + b2);
893 eivecs.col(1) << -scaledMat(1, 1), scaledMat(1, 0);
894 eivecs.col(1) /= numext::sqrt<Scalar>(c2 + b2);
898 eivecs.col(0) << -eivecs(1, 1), eivecs(0, 1);
904template <
typename SolverType,
int Size>
905struct direct_selfadjoint_eigenvalues<SolverType, Size, false, true> {
906 using MatrixType =
typename SolverType::MatrixType;
907 using PlainMatrixType =
typename SolverType::PlainMatrixType;
908 using Scalar =
typename SolverType::Scalar;
911 using Limit = std::conditional_t<std::is_same<Scalar, long double>::value,
long double,
double>;
913 EIGEN_DEVICE_FUNC
static constexpr Limit power_of_two(
int exponent) {
915 for (; exponent > 0; --exponent) result *= 2;
916 for (; exponent < 0; ++exponent) result /= 2;
920 EIGEN_DEVICE_FUNC
static bool safe_without_scaling(
const Scalar& magnitude, true_type) {
923 constexpr int degree = Size == 3 ? 6 : 2;
924 constexpr int lower = std::numeric_limits<Scalar>::min_exponent - 1 + std::numeric_limits<Scalar>::digits - 1 + 12;
925 constexpr int upper = std::numeric_limits<Scalar>::max_exponent - 1 - 12;
926 constexpr int minExponent = lower >= 0 ? (lower + degree - 1) / degree : lower / degree;
927 constexpr int maxExponent = upper >= 0 ? upper / degree : (upper - degree + 1) / degree;
928 constexpr Limit minimum = power_of_two(minExponent);
929 constexpr Limit maximum = power_of_two(maxExponent);
930 return magnitude >= Scalar(minimum) && magnitude <= Scalar(maximum);
933 EIGEN_DEVICE_FUNC
static bool safe_without_scaling(
const Scalar& magnitude, false_type) {
934 return magnitude >= Scalar(0.25) && magnitude <= Scalar(2) &&
935 NumTraits<Scalar>::epsilon() / Scalar(4096) >= (std::numeric_limits<Scalar>::min)();
938 EIGEN_DEVICE_FUNC EIGEN_DONT_INLINE
static void run_scaled(SolverType& solver,
const MatrixType& mat,
939 const Scalar& shift, Scalar centeredMax,
int options) {
940 PlainMatrixType scaledMat = mat.template selfadjointView<Lower>();
941 scaledMat.diagonal().array() -= shift;
942 centeredMax = safe_scaling<Scalar>::recover_flushed_max_coeff(scaledMat, centeredMax);
943 const auto factors = safe_scaling<Scalar>::scale_to(scaledMat, scaledMat, centeredMax);
944 direct_selfadjoint_eigensolver_kernel<SolverType, Size>::run(scaledMat, solver.m_eivalues, solver.m_eivec,
946 scaledMat.cwiseAbs().maxCoeff());
947 safe_scaling<Scalar>::unscale_in_place(solver.m_eivalues, centeredMax, factors);
950 EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
static void run_centered(SolverType& solver,
const MatrixType& mat,
953 PlainMatrixType scaledMat = mat.template selfadjointView<Lower>();
954 const Scalar first = scaledMat(0, 0);
955 Scalar shift = first;
956 for (Index i = 1; i < Size; ++i) shift += (scaledMat(i, i) - first) / Scalar(Size);
957 scaledMat.diagonal().array() -= shift;
959 const Scalar centeredMax = scaledMat.cwiseAbs().maxCoeff();
960 if (safe_without_scaling(centeredMax, supports_power_of_two_scaling<Scalar>())) {
961 direct_selfadjoint_eigensolver_kernel<SolverType, Size>::run(scaledMat, solver.m_eivalues, solver.m_eivec,
962 computeEigenvectors, centeredMax);
964 run_scaled(solver, mat, shift, centeredMax, options);
966 solver.m_eivalues.array() += shift;
968 solver.m_isInitialized =
true;
969 solver.m_eigenvectorsOk = computeEigenvectors;
972 EIGEN_DEVICE_FUNC
static void run_large(SolverType& solver, PlainMatrixType& scaledMat,
int options, false_type) {
973 run_centered(solver, scaledMat, options);
976 EIGEN_DEVICE_FUNC
static void run_large(SolverType& solver, PlainMatrixType& scaledMat,
int options, true_type) {
978 solver.compute(scaledMat, options);
981 EIGEN_DEVICE_FUNC EIGEN_DONT_INLINE
static void run_prescaled(SolverType& solver,
const MatrixType& mat,
982 const Scalar& maxCoeff,
int options) {
983 PlainMatrixType scaledMat = mat.template selfadjointView<Lower>();
984 const Scalar recoveredMax = safe_scaling<Scalar>::recover_flushed_max_coeff(scaledMat, maxCoeff);
985 const auto inputFactors = safe_scaling<Scalar>::scale_to(scaledMat, scaledMat, recoveredMax);
986 if (maxCoeff > (NumTraits<Scalar>::highest() * Scalar(0.5)) / Scalar(Size)) {
987 run_large(solver, scaledMat, options,
988 bool_constant<(Size == 3 && supports_power_of_two_scaling<Scalar>::value)>());
990 run_centered(solver, scaledMat, options);
992 safe_scaling<Scalar>::unscale_in_place(solver.m_eivalues, recoveredMax, inputFactors);
995 EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
static void run(SolverType& solver,
const MatrixType& mat,
int options) {
996 EIGEN_INTERNAL_CHECK_SHAPE(mat.rows() == Size && mat.cols() == Size, YOU_MIXED_MATRICES_OF_DIFFERENT_SIZES,
997 internal::fixed_dimensions<MatrixType, Size, Size>);
998 eigen_assert((options & ~(EigVecMask | GenEigMask)) == 0 && (options & EigVecMask) != EigVecMask &&
999 "invalid option parameter");
1000 PlainMatrixType scaledMat = mat.template selfadjointView<Lower>();
1001 const Scalar maxCoeff = scaledMat.cwiseAbs().maxCoeff();
1005 if (EIGEN_PREDICT_FALSE(maxCoeff < (std::numeric_limits<Scalar>::min)() / NumTraits<Scalar>::epsilon() ||
1006 maxCoeff > (NumTraits<Scalar>::highest() * Scalar(0.5)) / Scalar(Size))) {
1007 run_prescaled(solver, mat, maxCoeff, options);
1009 run_centered(solver, mat, options);
1016template <
typename MatrixType>
1018 const MatrixType& matrix,
int options) {
1019 internal::direct_selfadjoint_eigenvalues<SelfAdjointEigenSolver, Size, NumTraits<Scalar>::IsComplex>::run(
1020 *
this, matrix, options);
1027template <
typename RealScalar,
typename Index,
typename MatrixQType>
1028EIGEN_DEVICE_FUNC
static void tridiagonal_qr_step(RealScalar* diag, RealScalar* subdiag, Index start, Index end,
1029 MatrixQType* matrixQ) {
1031 RealScalar td = (diag[end - 1] - diag[end]) * RealScalar(0.5);
1032 RealScalar e = subdiag[end - 1];
1033 RealScalar mu = diag[end];
1034 if (numext::is_exactly_zero(td)) {
1035 mu -= numext::abs(e);
1036 }
else if (!numext::is_exactly_zero(e)) {
1037 const RealScalar e2 = numext::abs2(e);
1038 if (numext::is_exactly_zero(e2)) {
1043 const RealScalar ratio = td / e;
1044 const RealScalar h = numext::hypot(ratio, RealScalar(1));
1045 mu -= e / (ratio + (ratio > RealScalar(0) ? h : -h));
1047 const RealScalar h = numext::hypot(td, e);
1048 mu -= e2 / (td + (td > RealScalar(0) ? h : -h));
1052 RealScalar x = diag[start] - mu;
1053 RealScalar z = subdiag[start];
1056 for (Index k = start; k < end && !numext::is_exactly_zero(z); ++k) {
1057 JacobiRotation<RealScalar> rot;
1058 rot.makeGivens(x, z);
1063 const RealScalar diff = diag[k] - diag[k + 1];
1072 const RealScalar delta = rot.s() * (rot.s() * diff + RealScalar(2) * rot.c() * subdiag[k]);
1075 subdiag[k] = rot.c() * rot.s() * diff + (rot.c() * rot.c() - rot.s() * rot.s()) * subdiag[k];
1077 diag[k + 1] += delta;
1079 if (k > start) subdiag[k - 1] = rot.c() * subdiag[k - 1] - rot.s() * z;
1084 z = -rot.s() * subdiag[k + 1];
1085 subdiag[k + 1] = rot.c() * subdiag[k + 1];
1093 EIGEN_IF_CONSTEXPR (!MatrixQType::IsRowMajor &&
int(MatrixQType::InnerStrideAtCompileTime) == 1) {
1094 using Scalar =
typename MatrixQType::Scalar;
1095 Map<Matrix<Scalar, Dynamic, Dynamic, ColMajor>,
Unaligned, OuterStride<>> q(
1096 matrixQ->data(), matrixQ->rows(), matrixQ->cols(), OuterStride<>(matrixQ->outerStride()));
1097 q.applyOnTheRight(k, k + 1, rot);
1099 matrixQ->applyOnTheRight(k, k + 1, rot);
Computes eigenvalues and eigenvectors of the generalized selfadjoint eigen problem.
Definition GeneralizedSelfAdjointEigenSolver.h:53
The matrix class, also used for vectors and row-vectors.
Definition Matrix.h:188
Computes eigenvalues and eigenvectors of selfadjoint matrices.
Definition SelfAdjointEigenSolver.h:84
PlainMatrixType operatorExp() const
Computes the matrix exponential of the matrix.
Definition SelfAdjointEigenSolver.h:380
SelfAdjointEigenSolver & compute(const EigenBase< InputType > &matrix, int options=ComputeEigenvectors)
Computes eigendecomposition of given matrix.
SelfAdjointEigenSolver & computeFromTridiagonal(const RealVectorType &diag, const SubDiagonalType &subdiag, int options=ComputeEigenvectors)
Computes the eigen decomposition from a tridiagonal symmetric matrix.
Definition SelfAdjointEigenSolver.h:551
SelfAdjointEigenSolver(EigenBase< InputType > &matrix, int options=ComputeEigenvectors)
Constructor for inplace decomposition .
Definition SelfAdjointEigenSolver.h:214
typename MatrixType::Scalar Scalar
Scalar type for matrices of type MatrixType_.
Definition SelfAdjointEigenSolver.h:95
typename internal::plain_col_type< MatrixType, Scalar >::type VectorType
Type for vector of eigenvalues as returned by eigenvalues().
Definition SelfAdjointEigenSolver.h:126
SelfAdjointEigenSolver()
Default constructor for fixed-size matrices.
Definition SelfAdjointEigenSolver.h:141
ComputationInfo info() const
Reports whether previous computation was successful.
Definition SelfAdjointEigenSolver.h:414
Matrix< Scalar, Size, ColsAtCompileTime, Options, MatrixType::MaxRowsAtCompileTime, MaxColsAtCompileTime > PlainMatrixType
Plain matrix type with the shape and storage options of MatrixType_; MatrixType_ itself unless that i...
Definition SelfAdjointEigenSolver.h:100
PlainMatrixType operatorInverseSqrt() const
Computes the inverse square root of the matrix.
Definition SelfAdjointEigenSolver.h:404
std::conditional_t< internal::is_ref< MatrixType >::value, MatrixType, Matrix< Scalar, Size, Size, ColMajor, MaxColsAtCompileTime, MaxColsAtCompileTime > > EigenvectorsType
Type of the matrix returned by eigenvectors().
Definition SelfAdjointEigenSolver.h:107
typename NumTraits< Scalar >::Real RealScalar
Real scalar type for MatrixType_.
Definition SelfAdjointEigenSolver.h:117
PlainMatrixType operatorSqrt() const
Computes the positive-definite square root of the matrix.
Definition SelfAdjointEigenSolver.h:364
Eigen::Index Index
Definition SelfAdjointEigenSolver.h:96
SelfAdjointEigenSolver(Index size)
Constructor, pre-allocates memory for dynamic-size matrices.
Definition SelfAdjointEigenSolver.h:163
const RealVectorType & eigenvalues() const
Returns the eigenvalues of given matrix.
Definition SelfAdjointEigenSolver.h:342
static const int m_maxIterations
Maximum number of iterations.
Definition SelfAdjointEigenSolver.h:424
const EigenvectorsType & eigenvectors() const
Returns the eigenvectors of given matrix.
Definition SelfAdjointEigenSolver.h:321
SelfAdjointEigenSolver(const EigenBase< InputType > &matrix, int options=ComputeEigenvectors)
Constructor; computes eigendecomposition of given matrix.
Definition SelfAdjointEigenSolver.h:189
SelfAdjointEigenSolver & computeDirect(const MatrixType &matrix, int options=ComputeEigenvectors)
Computes eigendecomposition of given matrix primarily using a closed-form algorithm.
Definition SelfAdjointEigenSolver.h:1017
Tridiagonal decomposition of a selfadjoint matrix.
Definition Tridiagonalization.h:71
ComputationInfo
Definition Constants.h:455
@ Unaligned
Definition Constants.h:236
@ Success
Definition Constants.h:457
@ NoConvergence
Definition Constants.h:461
@ ComputeEigenvectors
Definition Constants.h:406
Definition EigenBase.h:34
constexpr Derived & derived()
Definition EigenBase.h:50
Holds information about the various numeric (i.e. scalar) types allowed by Eigen.
Definition NumTraits.h:267