19 template<
typename MatrixType,
int UpLo>
struct LDLT_Traits;
22 enum SignMatrix { PositiveSemiDef, NegativeSemiDef, ZeroSign, Indefinite };
48 template<
typename _MatrixType,
int _UpLo>
class LDLT
51 typedef _MatrixType MatrixType;
53 RowsAtCompileTime = MatrixType::RowsAtCompileTime,
54 ColsAtCompileTime = MatrixType::ColsAtCompileTime,
56 MaxRowsAtCompileTime = MatrixType::MaxRowsAtCompileTime,
57 MaxColsAtCompileTime = MatrixType::MaxColsAtCompileTime,
60 typedef typename MatrixType::Scalar Scalar;
63 typedef typename MatrixType::StorageIndex StorageIndex;
69 typedef internal::LDLT_Traits<MatrixType,UpLo> Traits;
80 m_isInitialized(false)
90 : m_matrix(size, size),
91 m_transpositions(size),
94 m_isInitialized(false)
102 explicit LDLT(
const MatrixType& matrix)
103 : m_matrix(matrix.rows(), matrix.cols()),
104 m_transpositions(matrix.rows()),
105 m_temporary(matrix.rows()),
107 m_isInitialized(false)
117 m_isInitialized =
false;
121 inline typename Traits::MatrixU
matrixU()
const
123 eigen_assert(m_isInitialized &&
"LDLT is not initialized.");
124 return Traits::getU(m_matrix);
128 inline typename Traits::MatrixL
matrixL()
const
130 eigen_assert(m_isInitialized &&
"LDLT is not initialized.");
131 return Traits::getL(m_matrix);
138 eigen_assert(m_isInitialized &&
"LDLT is not initialized.");
139 return m_transpositions;
145 eigen_assert(m_isInitialized &&
"LDLT is not initialized.");
146 return m_matrix.diagonal();
152 eigen_assert(m_isInitialized &&
"LDLT is not initialized.");
153 return m_sign == internal::PositiveSemiDef || m_sign == internal::ZeroSign;
159 eigen_assert(m_isInitialized &&
"LDLT is not initialized.");
160 return m_sign == internal::NegativeSemiDef || m_sign == internal::ZeroSign;
178 template<
typename Rhs>
182 eigen_assert(m_isInitialized &&
"LDLT is not initialized.");
183 eigen_assert(m_matrix.rows()==b.rows()
184 &&
"LDLT::solve(): invalid number of rows of the right hand side matrix b");
188 template<
typename Derived>
193 template <
typename Derived>
202 eigen_assert(m_isInitialized &&
"LDLT is not initialized.");
208 inline Index rows()
const {
return m_matrix.rows(); }
209 inline Index cols()
const {
return m_matrix.cols(); }
218 eigen_assert(m_isInitialized &&
"LDLT is not initialized.");
222 #ifndef EIGEN_PARSED_BY_DOXYGEN
223 template<
typename RhsType,
typename DstType>
225 void _solve_impl(
const RhsType &rhs, DstType &dst)
const;
230 static void check_template_parameters()
232 EIGEN_STATIC_ASSERT_NON_INTEGER(Scalar);
242 TranspositionType m_transpositions;
243 TmpMatrixType m_temporary;
244 internal::SignMatrix m_sign;
245 bool m_isInitialized;
250 template<
int UpLo>
struct ldlt_inplace;
252 template<>
struct ldlt_inplace<
Lower>
254 template<
typename MatrixType,
typename TranspositionType,
typename Workspace>
255 static bool unblocked(MatrixType& mat, TranspositionType& transpositions, Workspace& temp, SignMatrix& sign)
258 typedef typename MatrixType::Scalar Scalar;
259 typedef typename MatrixType::RealScalar RealScalar;
260 typedef typename TranspositionType::StorageIndex IndexType;
261 eigen_assert(mat.rows()==mat.cols());
262 const Index size = mat.rows();
266 transpositions.setIdentity();
267 if (numext::real(mat.coeff(0,0)) > 0) sign = PositiveSemiDef;
268 else if (numext::real(mat.coeff(0,0)) < 0) sign = NegativeSemiDef;
269 else sign = ZeroSign;
273 for (Index k = 0; k < size; ++k)
276 Index index_of_biggest_in_corner;
277 mat.diagonal().tail(size-k).cwiseAbs().maxCoeff(&index_of_biggest_in_corner);
278 index_of_biggest_in_corner += k;
280 transpositions.coeffRef(k) = IndexType(index_of_biggest_in_corner);
281 if(k != index_of_biggest_in_corner)
285 Index s = size-index_of_biggest_in_corner-1;
286 mat.row(k).head(k).swap(mat.row(index_of_biggest_in_corner).head(k));
287 mat.col(k).tail(s).swap(mat.col(index_of_biggest_in_corner).tail(s));
288 std::swap(mat.coeffRef(k,k),mat.coeffRef(index_of_biggest_in_corner,index_of_biggest_in_corner));
289 for(Index i=k+1;i<index_of_biggest_in_corner;++i)
291 Scalar tmp = mat.coeffRef(i,k);
292 mat.coeffRef(i,k) = numext::conj(mat.coeffRef(index_of_biggest_in_corner,i));
293 mat.coeffRef(index_of_biggest_in_corner,i) = numext::conj(tmp);
295 if(NumTraits<Scalar>::IsComplex)
296 mat.coeffRef(index_of_biggest_in_corner,k) = numext::conj(mat.coeff(index_of_biggest_in_corner,k));
303 Index rs = size - k - 1;
304 Block<MatrixType,Dynamic,1> A21(mat,k+1,k,rs,1);
305 Block<MatrixType,1,Dynamic> A10(mat,k,0,1,k);
306 Block<MatrixType,Dynamic,Dynamic> A20(mat,k+1,0,rs,k);
310 temp.head(k) = mat.diagonal().real().head(k).asDiagonal() * A10.adjoint();
311 mat.coeffRef(k,k) -= (A10 * temp.head(k)).value();
313 A21.noalias() -= A20 * temp.head(k);
320 RealScalar realAkk = numext::real(mat.coeffRef(k,k));
321 if((rs>0) && (abs(realAkk) > RealScalar(0)))
324 if (sign == PositiveSemiDef) {
325 if (realAkk < 0) sign = Indefinite;
326 }
else if (sign == NegativeSemiDef) {
327 if (realAkk > 0) sign = Indefinite;
328 }
else if (sign == ZeroSign) {
329 if (realAkk > 0) sign = PositiveSemiDef;
330 else if (realAkk < 0) sign = NegativeSemiDef;
344 template<
typename MatrixType,
typename WDerived>
345 static bool updateInPlace(MatrixType& mat, MatrixBase<WDerived>& w,
const typename MatrixType::RealScalar& sigma=1)
347 using numext::isfinite;
348 typedef typename MatrixType::Scalar Scalar;
349 typedef typename MatrixType::RealScalar RealScalar;
351 const Index size = mat.rows();
352 eigen_assert(mat.cols() == size && w.size()==size);
354 RealScalar alpha = 1;
357 for (Index j = 0; j < size; j++)
360 if (!(isfinite)(alpha))
364 RealScalar dj = numext::real(mat.coeff(j,j));
365 Scalar wj = w.coeff(j);
366 RealScalar swj2 = sigma*numext::abs2(wj);
367 RealScalar gamma = dj*alpha + swj2;
369 mat.coeffRef(j,j) += swj2/alpha;
375 w.tail(rs) -= wj * mat.col(j).tail(rs);
377 mat.col(j).tail(rs) += (sigma*numext::conj(wj)/gamma)*w.tail(rs);
382 template<
typename MatrixType,
typename TranspositionType,
typename Workspace,
typename WType>
383 static bool update(MatrixType& mat,
const TranspositionType& transpositions, Workspace& tmp,
const WType& w,
const typename MatrixType::RealScalar& sigma=1)
386 tmp = transpositions * w;
388 return ldlt_inplace<Lower>::updateInPlace(mat,tmp,sigma);
392 template<>
struct ldlt_inplace<
Upper>
394 template<
typename MatrixType,
typename TranspositionType,
typename Workspace>
395 static EIGEN_STRONG_INLINE
bool unblocked(MatrixType& mat, TranspositionType& transpositions, Workspace& temp, SignMatrix& sign)
397 Transpose<MatrixType> matt(mat);
398 return ldlt_inplace<Lower>::unblocked(matt, transpositions, temp, sign);
401 template<
typename MatrixType,
typename TranspositionType,
typename Workspace,
typename WType>
402 static EIGEN_STRONG_INLINE
bool update(MatrixType& mat, TranspositionType& transpositions, Workspace& tmp, WType& w,
const typename MatrixType::RealScalar& sigma=1)
404 Transpose<MatrixType> matt(mat);
405 return ldlt_inplace<Lower>::update(matt, transpositions, tmp, w.conjugate(), sigma);
409 template<
typename MatrixType>
struct LDLT_Traits<MatrixType,
Lower>
411 typedef const TriangularView<const MatrixType, UnitLower> MatrixL;
412 typedef const TriangularView<const typename MatrixType::AdjointReturnType, UnitUpper> MatrixU;
413 static inline MatrixL getL(
const MatrixType& m) {
return MatrixL(m); }
414 static inline MatrixU getU(
const MatrixType& m) {
return MatrixU(m.adjoint()); }
417 template<
typename MatrixType>
struct LDLT_Traits<MatrixType,
Upper>
419 typedef const TriangularView<const typename MatrixType::AdjointReturnType, UnitLower> MatrixL;
420 typedef const TriangularView<const MatrixType, UnitUpper> MatrixU;
421 static inline MatrixL getL(
const MatrixType& m) {
return MatrixL(m.adjoint()); }
422 static inline MatrixU getU(
const MatrixType& m) {
return MatrixU(m); }
429 template<
typename MatrixType,
int _UpLo>
432 check_template_parameters();
434 eigen_assert(a.rows()==a.cols());
435 const Index size = a.rows();
439 m_transpositions.resize(size);
440 m_isInitialized =
false;
441 m_temporary.resize(size);
442 m_sign = internal::ZeroSign;
444 internal::ldlt_inplace<UpLo>::unblocked(m_matrix, m_transpositions, m_temporary, m_sign);
446 m_isInitialized =
true;
455 template<
typename MatrixType,
int _UpLo>
456 template<
typename Derived>
459 typedef typename TranspositionType::StorageIndex IndexType;
460 const Index size = w.rows();
463 eigen_assert(m_matrix.rows()==size);
467 m_matrix.resize(size,size);
469 m_transpositions.resize(size);
470 for (Index i = 0; i < size; i++)
471 m_transpositions.coeffRef(i) = IndexType(i);
472 m_temporary.resize(size);
473 m_sign = sigma>=0 ? internal::PositiveSemiDef : internal::NegativeSemiDef;
474 m_isInitialized =
true;
477 internal::ldlt_inplace<UpLo>::update(m_matrix, m_transpositions, m_temporary, w, sigma);
482 #ifndef EIGEN_PARSED_BY_DOXYGEN
483 template<
typename _MatrixType,
int _UpLo>
484 template<
typename RhsType,
typename DstType>
487 eigen_assert(rhs.rows() == rows());
489 dst = m_transpositions * rhs;
492 matrixL().solveInPlace(dst);
506 for (Index i = 0; i < vecD.size(); ++i)
508 if(abs(vecD(i)) > tolerance)
509 dst.row(i) /= vecD(i);
511 dst.row(i).setZero();
515 matrixU().solveInPlace(dst);
518 dst = m_transpositions.transpose() * dst;
535 template<
typename MatrixType,
int _UpLo>
536 template<
typename Derived>
537 bool LDLT<MatrixType,_UpLo>::solveInPlace(MatrixBase<Derived> &bAndX)
const
539 eigen_assert(m_isInitialized &&
"LDLT is not initialized.");
540 eigen_assert(m_matrix.rows() == bAndX.rows());
542 bAndX = this->solve(bAndX);
550 template<
typename MatrixType,
int _UpLo>
553 eigen_assert(m_isInitialized &&
"LDLT is not initialized.");
554 const Index size = m_matrix.rows();
555 MatrixType res(size,size);
559 res = transpositionsP() * res;
561 res = matrixU() * res;
563 res = vectorD().real().asDiagonal() * res;
565 res = matrixL() * res;
567 res = transpositionsP().transpose() * res;
577 template<
typename MatrixType,
unsigned int UpLo>
588 template<
typename Derived>
598 #endif // EIGEN_LDLT_H
Robust Cholesky decomposition of a matrix with pivoting.
Definition: LDLT.h:48
const LDLT< PlainObject > ldlt() const
Definition: LDLT.h:590
Definition: Constants.h:196
LDLT(Index size)
Default Constructor with memory preallocation.
Definition: LDLT.h:89
Holds information about the various numeric (i.e. scalar) types allowed by Eigen. ...
Definition: NumTraits.h:107
void setZero()
Definition: LDLT.h:115
const unsigned int RowMajorBit
Definition: Constants.h:53
const MatrixType & matrixLDLT() const
Definition: LDLT.h:200
Definition: Constants.h:198
LDLT()
Default Constructor.
Definition: LDLT.h:76
bool isNegative(void) const
Definition: LDLT.h:157
MatrixType reconstructedMatrix() const
Definition: LDLT.h:551
const TranspositionType & transpositionsP() const
Definition: LDLT.h:136
Traits::MatrixU matrixU() const
Definition: LDLT.h:121
Definition: Constants.h:424
Eigen::Index Index
Definition: LDLT.h:62
const Solve< LDLT, Rhs > solve(const MatrixBase< Rhs > &b) const
Definition: LDLT.h:180
Definition: Eigen_Colamd.h:54
bool isPositive() const
Definition: LDLT.h:150
ComputationInfo info() const
Reports whether previous computation was successful.
Definition: LDLT.h:216
Diagonal< const MatrixType > vectorD() const
Definition: LDLT.h:143
Traits::MatrixL matrixL() const
Definition: LDLT.h:128
Expression of a diagonal/subdiagonal/superdiagonal in a matrix.
Definition: Diagonal.h:63
Pseudo expression representing a solving operation.
Definition: Solve.h:63
ComputationInfo
Definition: Constants.h:422
Base class for all dense matrices, vectors, and expressions.
Definition: MatrixBase.h:48
LDLT & compute(const MatrixType &matrix)
Definition: LDLT.h:430
LDLT(const MatrixType &matrix)
Constructor with decomposition.
Definition: LDLT.h:102
const LDLT< PlainObject, UpLo > ldlt() const
Definition: LDLT.h:579