377template <
typename MatrixType>
378inline Index ldlt_blocksize(Index size) {
379 if (
int(MatrixType::MaxRowsAtCompileTime) != Dynamic ||
int(MatrixType::MaxColsAtCompileTime) != Dynamic)
return 0;
380#ifdef EIGEN_LDLT_BLOCKSIZE
381 const Index nb = Index(EIGEN_LDLT_BLOCKSIZE);
383 constexpr Index kMinBlockSize = 32;
385 if (size <= 3 * kMinBlockSize)
return 0;
386 const Index unclamped = 16 * Index(numext::round(numext::sqrt(3.0 *
double(size)) / 16));
387 const Index nb = numext::mini(numext::maxi(unclamped, kMinBlockSize), Index(128));
389 return nb > 0 && size > 3 * nb ? nb : 0;
396struct ldlt_inplace<
Lower> {
400 template <
typename MatrixType,
typename TranspositionType,
typename Workspace>
401 static bool factor_trivial(
const MatrixType& mat, TranspositionType& transpositions, Workspace& temp,
402 SignMatrix& sign,
bool& ret) {
403 using Scalar =
typename MatrixType::Scalar;
404 using RealScalar =
typename MatrixType::RealScalar;
405 const Index size = mat.rows();
408 transpositions.setIdentity();
411 else if (numext::real(mat.coeff(0, 0)) >
static_cast<RealScalar
>(0))
412 sign = PositiveSemiDef;
413 else if (numext::real(mat.coeff(0, 0)) <
static_cast<RealScalar
>(0))
414 sign = NegativeSemiDef;
417 ret = size == 0 || (numext::isfinite)(numext::real(mat.coeff(0, 0)));
421 temp = mat.diagonal().real().template cast<Scalar>();
422 if ((temp.array() == Scalar(0)).all()) {
423 transpositions.setIdentity();
426 for (Index j = 0; ret && j < size; ++j) ret = (mat.col(j).tail(size - j - 1).array() == Scalar(0)).all();
437 template <
bool PivotOnLargestValue = false,
typename MatrixType,
typename TranspositionType,
typename Workspace>
438 static bool factor_panel(MatrixType& mat, Index k0, Index kb, TranspositionType& transpositions, Workspace& temp,
440 using Scalar =
typename MatrixType::Scalar;
441 using RealScalar =
typename MatrixType::RealScalar;
442 using IndexType =
typename TranspositionType::StorageIndex;
443 const Index size = mat.rows();
446 for (Index k = k0; k < k0 + kb; ++k) {
447 Index index_of_biggest_in_corner;
448 EIGEN_IF_CONSTEXPR (PivotOnLargestValue) {
450 temp.tail(size - k).real().template maxCoeff<PropagateNumbers>(&index_of_biggest_in_corner);
452 temp.tail(size - k).real().cwiseAbs().maxCoeff(&index_of_biggest_in_corner);
454 index_of_biggest_in_corner += k;
456 transpositions.coeffRef(k) = IndexType(index_of_biggest_in_corner);
457 if (k != index_of_biggest_in_corner) {
458 auto trailing = mat.bottomRightCorner(size - k0, size - k0);
459 selfadjoint_swap_lower(trailing, k - k0, index_of_biggest_in_corner - k0);
460 numext::swap(temp.coeffRef(k), temp.coeffRef(index_of_biggest_in_corner));
469 Index rs = size - k - 1;
471 auto A21 = mat.block(k + 1, k, rs,
fix<1>);
473 if (done > 0 && rs > 0) {
474 auto A10 = mat.block(k, k0,
fix<1>, done);
475 auto A20 = mat.block(k + 1, k0, rs, done);
476 temp.segment(k0, done) = mat.diagonal().real().segment(k0, done).asDiagonal() * A10.adjoint();
477 A21.noalias() -= A20 * temp.segment(k0, done);
484 RealScalar realAkk = numext::real(temp.coeff(k));
485 mat.coeffRef(k, k) = Scalar(realAkk);
486 bool pivot_is_valid = (numext::abs(realAkk) > RealScalar(0));
487 if (!(numext::isfinite)(realAkk)) ret =
false;
490 if ((rs > 0) && pivot_is_valid) {
494 temp.tail(rs).real() -= A21.conjugate().cwiseProduct(realAkk * A21).real();
496 ret = ret && (A21.array() == Scalar(0)).all();
499 if (sign == PositiveSemiDef) {
500 if (realAkk <
static_cast<RealScalar
>(0)) sign = Indefinite;
501 }
else if (sign == NegativeSemiDef) {
502 if (realAkk >
static_cast<RealScalar
>(0)) sign = Indefinite;
503 }
else if (sign == ZeroSign) {
504 if (realAkk >
static_cast<RealScalar
>(0))
505 sign = PositiveSemiDef;
506 else if (realAkk <
static_cast<RealScalar
>(0))
507 sign = NegativeSemiDef;
514 template <
typename MatrixType,
typename TranspositionType,
typename Workspace>
515 static bool unblocked(MatrixType& mat, TranspositionType& transpositions, Workspace& temp, SignMatrix& sign) {
516 EIGEN_INTERNAL_ASSERT_SQUARE(mat);
518 if (factor_trivial(mat, transpositions, temp, sign, ret))
return ret;
519 return factor_panel(mat, 0, mat.rows(), transpositions, temp, sign);
524 template <
typename MatrixType,
typename TranspositionType,
typename Workspace,
typename BlockWorkspace>
525 static bool blocked(MatrixType& mat, TranspositionType& transpositions, Workspace& temp, BlockWorkspace& workspace,
527 EIGEN_INTERNAL_ASSERT_SQUARE(mat);
528 const Index size = mat.rows();
529 const Index nb = ldlt_blocksize<MatrixType>(size);
530 if (nb == 0)
return unblocked(mat, transpositions, temp, sign);
532 if (factor_trivial(mat, transpositions, temp, sign, ret))
return ret;
534 workspace.resize(size, nb);
535 for (Index k0 = 0; k0 < size; k0 += nb) {
536 const Index kb = numext::mini(nb, size - k0);
537 const Index k1 = k0 + kb;
538 if (!factor_panel(mat, k0, kb, transpositions, temp, sign)) ret =
false;
542 EIGEN_IF_CONSTEXPR (MatrixType::IsRowMajor) {
543 for (Index k = k0; k < k1; ++k) {
544 const Index p = transpositions.coeff(k);
545 if (p != k) mat.row(k).head(k0).swap(mat.row(p).head(k0));
548 for (Index c = 0; c < k0; ++c) {
549 for (Index k = k0; k < k1; ++k) {
550 const Index p = transpositions.coeff(k);
551 if (p != k) numext::swap(mat.coeffRef(k, c), mat.coeffRef(p, c));
556 const Index rs = size - k1;
558 auto L21 = mat.block(k1, k0, rs, kb);
559 auto W = workspace.topLeftCorner(rs, kb);
560 W.noalias() = L21 * mat.diagonal().real().segment(k0, kb).asDiagonal();
561 mat.block(k1, k1, rs, rs).template triangularView<Lower>() -= L21 * W.adjoint();
574 template <
typename MatrixType,
typename WDerived>
575 static bool updateInPlace(MatrixType& mat, MatrixBase<WDerived>& w,
576 const typename MatrixType::RealScalar& sigma = 1) {
577 using Scalar =
typename MatrixType::Scalar;
578 using RealScalar =
typename MatrixType::RealScalar;
580 const Index size = mat.rows();
581 EIGEN_INTERNAL_ASSERT_SQUARE(mat);
582 EIGEN_INTERNAL_ASSERT_SIZE_MATCHES_ROWS(w, mat);
584 RealScalar alpha = 1;
587 for (Index j = 0; j < size; j++) {
589 if (!(numext::isfinite)(alpha))
break;
592 RealScalar dj = numext::real(mat.coeff(j, j));
593 Scalar wj = w.coeff(j);
594 RealScalar swj2 = sigma * numext::abs2(wj);
595 RealScalar gamma = dj * alpha + swj2;
602 if (!numext::is_exactly_zero(swj2)) {
603 mat.coeffRef(j, j) += swj2 / alpha;
608 Index rs = size - j - 1;
609 w.tail(rs) -= wj * mat.col(j).tail(rs);
610 if (!numext::is_exactly_zero(gamma)) mat.col(j).tail(rs) += (sigma * numext::conj(wj) / gamma) * w.tail(rs);
615 template <
typename MatrixType,
typename TranspositionType,
typename Workspace,
typename WType>
616 static bool update(MatrixType& mat,
const TranspositionType& transpositions, Workspace& tmp,
const WType& w,
617 const typename MatrixType::RealScalar& sigma = 1) {
619 tmp = transpositions * w;
621 return ldlt_inplace<Lower>::updateInPlace(mat, tmp, sigma);
626struct ldlt_inplace<
Upper> {
627 template <
typename MatrixType,
typename TranspositionType,
typename Workspace>
628 static EIGEN_STRONG_INLINE
bool unblocked(MatrixType& mat, TranspositionType& transpositions, Workspace& temp,
630 Transpose<MatrixType> matt(mat);
631 return ldlt_inplace<Lower>::unblocked(matt, transpositions, temp, sign);
634 template <
typename MatrixType,
typename TranspositionType,
typename Workspace,
typename BlockWorkspace>
635 static EIGEN_STRONG_INLINE
bool blocked(MatrixType& mat, TranspositionType& transpositions, Workspace& temp,
636 BlockWorkspace& workspace, SignMatrix& sign) {
637 Transpose<MatrixType> matt(mat);
638 return ldlt_inplace<Lower>::blocked(matt, transpositions, temp, workspace, sign);
641 template <
typename MatrixType,
typename TranspositionType,
typename Workspace,
typename WType>
642 static EIGEN_STRONG_INLINE
bool update(MatrixType& mat, TranspositionType& transpositions, Workspace& tmp, WType& w,
643 const typename MatrixType::RealScalar& sigma = 1) {
644 Transpose<MatrixType> matt(mat);
645 return ldlt_inplace<Lower>::update(matt, transpositions, tmp, w.conjugate(), sigma);
649template <
typename MatrixType>
650struct LDLT_Traits<MatrixType,
Lower> {
651 using MatrixL =
const TriangularView<const MatrixType, UnitLower>;
652 using MatrixU =
const TriangularView<const typename MatrixType::AdjointReturnType, UnitUpper>;
653 static inline MatrixL getL(
const MatrixType& m) {
return MatrixL(m); }
654 static inline MatrixU getU(
const MatrixType& m) {
return MatrixU(m.adjoint()); }
657template <
typename MatrixType>
658struct LDLT_Traits<MatrixType,
Upper> {
659 using MatrixL =
const TriangularView<const typename MatrixType::AdjointReturnType, UnitLower>;
660 using MatrixU =
const TriangularView<const MatrixType, UnitUpper>;
661 static inline MatrixL getL(
const MatrixType& m) {
return MatrixL(m.adjoint()); }
662 static inline MatrixU getU(
const MatrixType& m) {
return MatrixU(m); }
669template <
typename MatrixType,
int UpLo_>
670template <
typename InputType>
672 EIGEN_INTERNAL_ASSERT_SQUARE(a);
673 const Index
size = a.rows();
675 m_matrix = a.derived();
678 m_l1_norm = m_matrix.template selfadjointView<UpLo_>().l1Norm();
680 m_transpositions.resize(
size);
681 m_isInitialized =
false;
682 m_temporary.resize(
size);
683 m_sign = internal::ZeroSign;
685 m_info = internal::ldlt_inplace<UpLo>::blocked(m_matrix, m_transpositions, m_temporary, m_workspace, m_sign)
689 m_isInitialized =
true;
712template <
typename MatrixType,
int UpLo_>
713template <
typename Derived>
716 using IndexType =
typename TranspositionType::StorageIndex;
717 const Index
size = w.rows();
718 if (m_isInitialized) {
719 EIGEN_INTERNAL_ASSERT_SAME_ROWS(m_matrix, w);
723 m_transpositions.resize(
size);
724 for (Index i = 0; i <
size; i++) m_transpositions.coeffRef(i) = IndexType(i);
725 m_temporary.resize(
size);
726 m_sign = sigma >= 0 ? internal::PositiveSemiDef : internal::NegativeSemiDef;
727 m_isInitialized =
true;
732 internal::ldlt_inplace<UpLo>::update(m_matrix, m_transpositions, m_temporary, w, sigma);
739template <
typename MatrixType_,
int UpLo_>
741 eigen_assert(m_isInitialized &&
"LDLT is not initialized.");
742 eigen_assert(m_info ==
Success &&
"LDLT failed because of a zero pivot.");
743 return Scalar(internal::scaled_prod(
vectorD().real()).value());
746template <
typename MatrixType_,
int UpLo_>
748 eigen_assert(m_isInitialized &&
"LDLT is not initialized.");
749 eigen_assert(m_info ==
Success &&
"LDLT failed because of a zero pivot.");
750 return internal::scaled_prod(
vectorD().real()).abs();
753template <
typename MatrixType_,
int UpLo_>
755 eigen_assert(m_isInitialized &&
"LDLT is not initialized.");
756 eigen_assert(m_info ==
Success &&
"LDLT failed because of a zero pivot.");
757 return internal::log_abs_prod(
vectorD().real());
760template <
typename MatrixType_,
int UpLo_>
762 eigen_assert(m_isInitialized &&
"LDLT is not initialized.");
763 eigen_assert(m_info ==
Success &&
"LDLT failed because of a zero pivot.");
764 return Scalar(
vectorD().real().array().sign().prod());
767#ifndef EIGEN_PARSED_BY_DOXYGEN
768template <
typename MatrixType_,
int UpLo_>
769template <
typename RhsType,
typename DstType>
771 _solve_impl_transposed<true>(rhs, dst);
774template <
typename MatrixType_,
int UpLo_>
775template <
bool Conjugate,
typename RhsType,
typename DstType>
776void LDLT<MatrixType_, UpLo_>::_solve_impl_transposed(
const RhsType& rhs, DstType& dst)
const {
778 dst = m_transpositions * rhs;
782 matrixL().template conjugateIf<!Conjugate>().solveInPlace(dst);
787 const typename Diagonal<const MatrixType>::RealReturnType vecD(vectorD());
794 RealScalar tolerance = (std::numeric_limits<RealScalar>::min)();
795 for (Index i = 0; i < vecD.size(); ++i) {
796 if (numext::abs(vecD(i)) > tolerance)
797 dst.row(i) /= vecD(i);
799 dst.row(i).setZero();
804 matrixL().transpose().template conjugateIf<Conjugate>().solveInPlace(dst);
808 dst = m_transpositions.transpose() * dst;
825template <
typename MatrixType,
int UpLo_>
826template <
typename Derived>
828 eigen_assert(m_isInitialized &&
"LDLT is not initialized.");
829 EIGEN_INTERNAL_ASSERT_SAME_ROWS(m_matrix, bAndX);
831 bAndX = this->solve(bAndX);
839template <
typename MatrixType,
int UpLo_>
841 eigen_assert(m_isInitialized &&
"LDLT is not initialized.");
843 res =
vectorD().real().asDiagonal() * res;
846 auto lower_triangle = res.template selfadjointView<Lower>();
847 for (Index k = m_matrix.rows() - 1; k >= 0; --k) lower_triangle.applyTransposition(k, m_transpositions.coeff(k));
848 res.template triangularView<StrictlyUpper>() = res.adjoint();
857template <
typename MatrixType,
unsigned int UpLo>
867template <
typename Derived>