19 template<
typename MatrixType,
int UpLo>
struct LDLT_Traits;
22 enum SignMatrix { PositiveSemiDef, NegativeSemiDef, ZeroSign, Indefinite };
50 template<
typename _MatrixType,
int _UpLo>
class LDLT 53 typedef _MatrixType MatrixType;
55 RowsAtCompileTime = MatrixType::RowsAtCompileTime,
56 ColsAtCompileTime = MatrixType::ColsAtCompileTime,
57 MaxRowsAtCompileTime = MatrixType::MaxRowsAtCompileTime,
58 MaxColsAtCompileTime = MatrixType::MaxColsAtCompileTime,
61 typedef typename MatrixType::Scalar Scalar;
64 typedef typename MatrixType::StorageIndex StorageIndex;
70 typedef internal::LDLT_Traits<MatrixType,UpLo> Traits;
81 m_isInitialized(false)
91 : m_matrix(size, size),
92 m_transpositions(size),
95 m_isInitialized(false)
104 template<
typename InputType>
106 : m_matrix(matrix.rows(), matrix.cols()),
107 m_transpositions(matrix.rows()),
108 m_temporary(matrix.rows()),
110 m_isInitialized(false)
121 template<
typename InputType>
123 : m_matrix(matrix.derived()),
124 m_transpositions(matrix.rows()),
125 m_temporary(matrix.rows()),
127 m_isInitialized(false)
137 m_isInitialized =
false;
141 inline typename Traits::MatrixU
matrixU()
const 143 eigen_assert(m_isInitialized &&
"LDLT is not initialized.");
144 return Traits::getU(m_matrix);
148 inline typename Traits::MatrixL
matrixL()
const 150 eigen_assert(m_isInitialized &&
"LDLT is not initialized.");
151 return Traits::getL(m_matrix);
158 eigen_assert(m_isInitialized &&
"LDLT is not initialized.");
159 return m_transpositions;
165 eigen_assert(m_isInitialized &&
"LDLT is not initialized.");
166 return m_matrix.diagonal();
172 eigen_assert(m_isInitialized &&
"LDLT is not initialized.");
173 return m_sign == internal::PositiveSemiDef || m_sign == internal::ZeroSign;
179 eigen_assert(m_isInitialized &&
"LDLT is not initialized.");
180 return m_sign == internal::NegativeSemiDef || m_sign == internal::ZeroSign;
198 template<
typename Rhs>
202 eigen_assert(m_isInitialized &&
"LDLT is not initialized.");
203 eigen_assert(m_matrix.rows()==b.
rows()
204 &&
"LDLT::solve(): invalid number of rows of the right hand side matrix b");
208 template<
typename Derived>
211 template<
typename InputType>
219 eigen_assert(m_isInitialized &&
"LDLT is not initialized.");
220 return internal::rcond_estimate_helper(m_l1_norm, *
this);
223 template <
typename Derived>
232 eigen_assert(m_isInitialized &&
"LDLT is not initialized.");
236 MatrixType reconstructedMatrix()
const;
245 inline Index rows()
const {
return m_matrix.rows(); }
246 inline Index cols()
const {
return m_matrix.cols(); }
255 eigen_assert(m_isInitialized &&
"LDLT is not initialized.");
259 #ifndef EIGEN_PARSED_BY_DOXYGEN 260 template<
typename RhsType,
typename DstType>
262 void _solve_impl(
const RhsType &rhs, DstType &dst)
const;
267 static void check_template_parameters()
269 EIGEN_STATIC_ASSERT_NON_INTEGER(Scalar);
279 RealScalar m_l1_norm;
280 TranspositionType m_transpositions;
281 TmpMatrixType m_temporary;
282 internal::SignMatrix m_sign;
283 bool m_isInitialized;
288 template<
int UpLo>
struct ldlt_inplace;
290 template<>
struct ldlt_inplace<Lower>
292 template<
typename MatrixType,
typename TranspositionType,
typename Workspace>
293 static bool unblocked(MatrixType& mat,
TranspositionType& transpositions, Workspace& temp, SignMatrix&
sign)
296 typedef typename MatrixType::Scalar Scalar;
297 typedef typename MatrixType::RealScalar RealScalar;
298 typedef typename TranspositionType::StorageIndex IndexType;
299 eigen_assert(mat.rows()==mat.cols());
300 const Index size = mat.rows();
304 transpositions.setIdentity();
305 if (numext::real(mat.coeff(0,0)) > static_cast<RealScalar>(0) ) sign = PositiveSemiDef;
306 else if (numext::real(mat.coeff(0,0)) < static_cast<RealScalar>(0)) sign = NegativeSemiDef;
307 else sign = ZeroSign;
311 for (
Index k = 0; k < size; ++k)
314 Index index_of_biggest_in_corner;
315 mat.diagonal().tail(size-k).cwiseAbs().maxCoeff(&index_of_biggest_in_corner);
316 index_of_biggest_in_corner += k;
318 transpositions.coeffRef(k) = IndexType(index_of_biggest_in_corner);
319 if(k != index_of_biggest_in_corner)
323 Index s = size-index_of_biggest_in_corner-1;
324 mat.row(k).head(k).swap(mat.row(index_of_biggest_in_corner).head(k));
325 mat.col(k).tail(s).swap(mat.col(index_of_biggest_in_corner).tail(s));
326 std::swap(mat.coeffRef(k,k),mat.coeffRef(index_of_biggest_in_corner,index_of_biggest_in_corner));
327 for(
Index i=k+1;i<index_of_biggest_in_corner;++i)
329 Scalar tmp = mat.coeffRef(i,k);
330 mat.coeffRef(i,k) = numext::conj(mat.coeffRef(index_of_biggest_in_corner,i));
331 mat.coeffRef(index_of_biggest_in_corner,i) = numext::conj(tmp);
334 mat.coeffRef(index_of_biggest_in_corner,k) = numext::conj(mat.coeff(index_of_biggest_in_corner,k));
341 Index rs = size - k - 1;
348 temp.head(k) = mat.diagonal().real().head(k).asDiagonal() * A10.adjoint();
349 mat.coeffRef(k,k) -= (A10 * temp.head(k)).value();
351 A21.noalias() -= A20 * temp.head(k);
358 RealScalar realAkk = numext::real(mat.coeffRef(k,k));
359 if((rs>0) && (
abs(realAkk) > RealScalar(0)))
362 if (sign == PositiveSemiDef) {
363 if (realAkk < static_cast<RealScalar>(0)) sign = Indefinite;
364 }
else if (sign == NegativeSemiDef) {
365 if (realAkk > static_cast<RealScalar>(0)) sign = Indefinite;
366 }
else if (sign == ZeroSign) {
367 if (realAkk > static_cast<RealScalar>(0)) sign = PositiveSemiDef;
368 else if (realAkk < static_cast<RealScalar>(0)) sign = NegativeSemiDef;
382 template<
typename MatrixType,
typename WDerived>
383 static bool updateInPlace(MatrixType& mat,
MatrixBase<WDerived>& w,
const typename MatrixType::RealScalar& sigma=1)
385 using numext::isfinite;
386 typedef typename MatrixType::Scalar Scalar;
387 typedef typename MatrixType::RealScalar RealScalar;
389 const Index size = mat.rows();
390 eigen_assert(mat.cols() == size && w.
size()==size);
392 RealScalar alpha = 1;
395 for (
Index j = 0; j < size; j++)
402 RealScalar dj = numext::real(mat.coeff(j,j));
403 Scalar wj = w.
coeff(j);
404 RealScalar swj2 = sigma*numext::abs2(wj);
405 RealScalar gamma = dj*alpha + swj2;
407 mat.coeffRef(j,j) += swj2/alpha;
413 w.
tail(rs) -= wj * mat.col(j).tail(rs);
415 mat.col(j).tail(rs) += (sigma*numext::conj(wj)/gamma)*w.
tail(rs);
420 template<
typename MatrixType,
typename TranspositionType,
typename Workspace,
typename WType>
421 static bool update(MatrixType& mat,
const TranspositionType& transpositions, Workspace& tmp,
const WType& w,
const typename MatrixType::RealScalar& sigma=1)
424 tmp = transpositions * w;
426 return ldlt_inplace<Lower>::updateInPlace(mat,tmp,sigma);
430 template<>
struct ldlt_inplace<Upper>
432 template<
typename MatrixType,
typename TranspositionType,
typename Workspace>
433 static EIGEN_STRONG_INLINE
bool unblocked(MatrixType& mat,
TranspositionType& transpositions, Workspace& temp, SignMatrix&
sign)
436 return ldlt_inplace<Lower>::unblocked(matt, transpositions, temp, sign);
439 template<
typename MatrixType,
typename TranspositionType,
typename Workspace,
typename WType>
440 static EIGEN_STRONG_INLINE
bool update(MatrixType& mat,
TranspositionType& transpositions, Workspace& tmp, WType& w,
const typename MatrixType::RealScalar& sigma=1)
443 return ldlt_inplace<Lower>::update(matt, transpositions, tmp, w.conjugate(), sigma);
447 template<
typename MatrixType>
struct LDLT_Traits<MatrixType,Lower>
451 static inline MatrixL getL(
const MatrixType& m) {
return MatrixL(m); }
452 static inline MatrixU getU(
const MatrixType& m) {
return MatrixU(m.adjoint()); }
455 template<
typename MatrixType>
struct LDLT_Traits<MatrixType,Upper>
459 static inline MatrixL getL(
const MatrixType& m) {
return MatrixL(m.adjoint()); }
460 static inline MatrixU getU(
const MatrixType& m) {
return MatrixU(m); }
467 template<
typename MatrixType,
int _UpLo>
468 template<
typename InputType>
471 check_template_parameters();
479 m_l1_norm = RealScalar(0);
481 for (
Index col = 0; col < size; ++col) {
482 RealScalar abs_col_sum;
484 abs_col_sum = m_matrix.col(col).tail(size - col).template lpNorm<1>() + m_matrix.row(col).head(col).template lpNorm<1>();
486 abs_col_sum = m_matrix.col(col).head(col).template lpNorm<1>() + m_matrix.row(col).tail(size - col).template lpNorm<1>();
487 if (abs_col_sum > m_l1_norm)
488 m_l1_norm = abs_col_sum;
491 m_transpositions.resize(size);
492 m_isInitialized =
false;
493 m_temporary.resize(size);
494 m_sign = internal::ZeroSign;
496 internal::ldlt_inplace<UpLo>::unblocked(m_matrix, m_transpositions, m_temporary, m_sign);
498 m_isInitialized =
true;
507 template<
typename MatrixType,
int _UpLo>
508 template<
typename Derived>
511 typedef typename TranspositionType::StorageIndex IndexType;
515 eigen_assert(m_matrix.rows()==size);
519 m_matrix.resize(size,size);
521 m_transpositions.resize(size);
522 for (
Index i = 0; i < size; i++)
523 m_transpositions.coeffRef(i) = IndexType(i);
524 m_temporary.resize(size);
525 m_sign = sigma>=0 ? internal::PositiveSemiDef : internal::NegativeSemiDef;
526 m_isInitialized =
true;
529 internal::ldlt_inplace<UpLo>::update(m_matrix, m_transpositions, m_temporary, w, sigma);
534 #ifndef EIGEN_PARSED_BY_DOXYGEN 535 template<
typename _MatrixType,
int _UpLo>
536 template<
typename RhsType,
typename DstType>
539 eigen_assert(rhs.rows() == rows());
541 dst = m_transpositions * rhs;
544 matrixL().solveInPlace(dst);
558 for (
Index i = 0; i < vecD.size(); ++i)
560 if(
abs(vecD(i)) > tolerance)
561 dst.row(i) /= vecD(i);
563 dst.row(i).setZero();
567 matrixU().solveInPlace(dst);
570 dst = m_transpositions.transpose() * dst;
587 template<
typename MatrixType,
int _UpLo>
588 template<
typename Derived>
591 eigen_assert(m_isInitialized &&
"LDLT is not initialized.");
592 eigen_assert(m_matrix.rows() == bAndX.
rows());
594 bAndX = this->solve(bAndX);
602 template<
typename MatrixType,
int _UpLo>
605 eigen_assert(m_isInitialized &&
"LDLT is not initialized.");
606 const Index size = m_matrix.rows();
607 MatrixType res(size,size);
611 res = transpositionsP() * res;
613 res = matrixU() * res;
615 res = vectorD().real().asDiagonal() * res;
617 res = matrixL() * res;
619 res = transpositionsP().transpose() * res;
629 template<
typename MatrixType,
unsigned int UpLo>
640 template<
typename Derived>
650 #endif // EIGEN_LDLT_H Robust Cholesky decomposition of a matrix with pivoting.
Definition: LDLT.h:50
CoeffReturnType coeff(Index row, Index col) const
Definition: DenseCoeffsBase.h:96
const LDLT< PlainObject > ldlt() const
Definition: LDLT.h:642
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_isfinite_op< typename Derived::Scalar >, const Derived > isfinite(const Eigen::ArrayBase< Derived > &x)
Expression of the transpose of a matrix.
Definition: Transpose.h:52
LDLT(Index size)
Default Constructor with memory preallocation.
Definition: LDLT.h:90
Namespace containing all symbols from the Eigen library.
Definition: Core:271
Holds information about the various numeric (i.e. scalar) types allowed by Eigen. ...
Definition: NumTraits.h:167
Derived & derived()
Definition: EigenBase.h:44
void setZero()
Definition: LDLT.h:135
Index rows() const
Definition: EigenBase.h:58
Definition: EigenBase.h:28
const MatrixType & matrixLDLT() const
Definition: LDLT.h:230
LDLT()
Default Constructor.
Definition: LDLT.h:77
Definition: Constants.h:204
LDLT(const EigenBase< InputType > &matrix)
Constructor with decomposition.
Definition: LDLT.h:105
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: XprHelper.h:35
bool isNegative(void) const
Definition: LDLT.h:177
RealScalar rcond() const
Definition: LDLT.h:217
MatrixType reconstructedMatrix() const
Definition: LDLT.h:603
const LDLT & adjoint() const
Definition: LDLT.h:243
const TranspositionType & transpositionsP() const
Definition: LDLT.h:156
Traits::MatrixU matrixU() const
Definition: LDLT.h:141
Definition: Constants.h:432
Eigen::Index Index
Definition: LDLT.h:63
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_abs_op< typename Derived::Scalar >, const Derived > abs(const Eigen::ArrayBase< Derived > &x)
const Solve< LDLT, Rhs > solve(const MatrixBase< Rhs > &b) const
Definition: LDLT.h:200
Definition: Eigen_Colamd.h:50
bool isPositive() const
Definition: LDLT.h:170
Index cols() const
Definition: EigenBase.h:61
ComputationInfo info() const
Reports whether previous computation was successful.
Definition: LDLT.h:253
Expression of a fixed-size or dynamic-size block.
Definition: Block.h:103
Diagonal< const MatrixType > vectorD() const
Definition: LDLT.h:163
Traits::MatrixL matrixL() const
Definition: LDLT.h:148
Expression of a triangular part in a matrix.
Definition: TriangularMatrix.h:186
Expression of a diagonal/subdiagonal/superdiagonal in a matrix.
Definition: Diagonal.h:63
Pseudo expression representing a solving operation.
Definition: Solve.h:62
SegmentReturnType tail(Index n)
Definition: DenseBase.h:892
LDLT(EigenBase< InputType > &matrix)
Constructs a LDLT factorization from a given matrix.
Definition: LDLT.h:122
ComputationInfo
Definition: Constants.h:430
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_sign_op< typename Derived::Scalar >, const Derived > sign(const Eigen::ArrayBase< Derived > &x)
Base class for all dense matrices, vectors, and expressions.
Definition: MatrixBase.h:48
const LDLT< PlainObject, UpLo > ldlt() const
Definition: LDLT.h:631
Index size() const
Definition: EigenBase.h:65