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;
62 typedef typename MatrixType::Index Index;
68 typedef internal::LDLT_Traits<MatrixType,UpLo> Traits;
79 m_isInitialized(false)
89 : m_matrix(size, size),
90 m_transpositions(size),
93 m_isInitialized(false)
102 : m_matrix(matrix.rows(), matrix.cols()),
103 m_transpositions(matrix.rows()),
104 m_temporary(matrix.rows()),
106 m_isInitialized(false)
116 m_isInitialized =
false;
120 inline typename Traits::MatrixU
matrixU()
const
122 eigen_assert(m_isInitialized &&
"LDLT is not initialized.");
123 return Traits::getU(m_matrix);
127 inline typename Traits::MatrixL
matrixL()
const
129 eigen_assert(m_isInitialized &&
"LDLT is not initialized.");
130 return Traits::getL(m_matrix);
137 eigen_assert(m_isInitialized &&
"LDLT is not initialized.");
138 return m_transpositions;
144 eigen_assert(m_isInitialized &&
"LDLT is not initialized.");
145 return m_matrix.diagonal();
151 eigen_assert(m_isInitialized &&
"LDLT is not initialized.");
152 return m_sign == internal::PositiveSemiDef || m_sign == internal::ZeroSign;
155 #ifdef EIGEN2_SUPPORT
156 inline bool isPositiveDefinite()
const
165 eigen_assert(m_isInitialized &&
"LDLT is not initialized.");
166 return m_sign == internal::NegativeSemiDef || m_sign == internal::ZeroSign;
184 template<
typename Rhs>
185 inline const internal::solve_retval<LDLT, Rhs>
188 eigen_assert(m_isInitialized &&
"LDLT is not initialized.");
189 eigen_assert(m_matrix.rows()==b.rows()
190 &&
"LDLT::solve(): invalid number of rows of the right hand side matrix b");
191 return internal::solve_retval<LDLT, Rhs>(*
this, b.derived());
194 #ifdef EIGEN2_SUPPORT
195 template<
typename OtherDerived,
typename ResultType>
198 *result = this->
solve(b);
203 template<
typename Derived>
204 bool solveInPlace(MatrixBase<Derived> &bAndX)
const;
208 template <
typename Derived>
209 LDLT& rankUpdate(
const MatrixBase<Derived>& w,
const RealScalar& alpha=1);
217 eigen_assert(m_isInitialized &&
"LDLT is not initialized.");
223 inline Index rows()
const {
return m_matrix.rows(); }
224 inline Index cols()
const {
return m_matrix.cols(); }
233 eigen_assert(m_isInitialized &&
"LDLT is not initialized.");
246 TranspositionType m_transpositions;
247 TmpMatrixType m_temporary;
248 internal::SignMatrix m_sign;
249 bool m_isInitialized;
254 template<
int UpLo>
struct ldlt_inplace;
256 template<>
struct ldlt_inplace<
Lower>
258 template<
typename MatrixType,
typename TranspositionType,
typename Workspace>
259 static bool unblocked(MatrixType& mat, TranspositionType& transpositions, Workspace& temp, SignMatrix& sign)
262 typedef typename MatrixType::Scalar Scalar;
263 typedef typename MatrixType::RealScalar RealScalar;
264 typedef typename MatrixType::Index Index;
265 eigen_assert(mat.rows()==mat.cols());
266 const Index size = mat.rows();
270 transpositions.setIdentity();
271 if (numext::real(mat.coeff(0,0)) > 0) sign = PositiveSemiDef;
272 else if (numext::real(mat.coeff(0,0)) < 0) sign = NegativeSemiDef;
273 else sign = ZeroSign;
277 for (Index k = 0; k < size; ++k)
280 Index index_of_biggest_in_corner;
281 mat.diagonal().tail(size-k).cwiseAbs().maxCoeff(&index_of_biggest_in_corner);
282 index_of_biggest_in_corner += k;
284 transpositions.coeffRef(k) = index_of_biggest_in_corner;
285 if(k != index_of_biggest_in_corner)
289 Index s = size-index_of_biggest_in_corner-1;
290 mat.row(k).head(k).swap(mat.row(index_of_biggest_in_corner).head(k));
291 mat.col(k).tail(s).swap(mat.col(index_of_biggest_in_corner).tail(s));
292 std::swap(mat.coeffRef(k,k),mat.coeffRef(index_of_biggest_in_corner,index_of_biggest_in_corner));
293 for(
int i=k+1;i<index_of_biggest_in_corner;++i)
295 Scalar tmp = mat.coeffRef(i,k);
296 mat.coeffRef(i,k) = numext::conj(mat.coeffRef(index_of_biggest_in_corner,i));
297 mat.coeffRef(index_of_biggest_in_corner,i) = numext::conj(tmp);
299 if(NumTraits<Scalar>::IsComplex)
300 mat.coeffRef(index_of_biggest_in_corner,k) = numext::conj(mat.coeff(index_of_biggest_in_corner,k));
307 Index rs = size - k - 1;
308 Block<MatrixType,Dynamic,1> A21(mat,k+1,k,rs,1);
309 Block<MatrixType,1,Dynamic> A10(mat,k,0,1,k);
310 Block<MatrixType,Dynamic,Dynamic> A20(mat,k+1,0,rs,k);
314 temp.head(k) = mat.diagonal().real().head(k).asDiagonal() * A10.adjoint();
315 mat.coeffRef(k,k) -= (A10 * temp.head(k)).value();
317 A21.noalias() -= A20 * temp.head(k);
324 RealScalar realAkk = numext::real(mat.coeffRef(k,k));
325 if((rs>0) && (abs(realAkk) > RealScalar(0)))
328 if (sign == PositiveSemiDef) {
329 if (realAkk < 0) sign = Indefinite;
330 }
else if (sign == NegativeSemiDef) {
331 if (realAkk > 0) sign = Indefinite;
332 }
else if (sign == ZeroSign) {
333 if (realAkk > 0) sign = PositiveSemiDef;
334 else if (realAkk < 0) sign = NegativeSemiDef;
348 template<
typename MatrixType,
typename WDerived>
349 static bool updateInPlace(MatrixType& mat, MatrixBase<WDerived>& w,
const typename MatrixType::RealScalar& sigma=1)
351 using numext::isfinite;
352 typedef typename MatrixType::Scalar Scalar;
353 typedef typename MatrixType::RealScalar RealScalar;
354 typedef typename MatrixType::Index Index;
356 const Index size = mat.rows();
357 eigen_assert(mat.cols() == size && w.size()==size);
359 RealScalar alpha = 1;
362 for (Index j = 0; j < size; j++)
365 if (!(isfinite)(alpha))
369 RealScalar dj = numext::real(mat.coeff(j,j));
370 Scalar wj = w.coeff(j);
371 RealScalar swj2 = sigma*numext::abs2(wj);
372 RealScalar gamma = dj*alpha + swj2;
374 mat.coeffRef(j,j) += swj2/alpha;
380 w.tail(rs) -= wj * mat.col(j).tail(rs);
382 mat.col(j).tail(rs) += (sigma*numext::conj(wj)/gamma)*w.tail(rs);
387 template<
typename MatrixType,
typename TranspositionType,
typename Workspace,
typename WType>
388 static bool update(MatrixType& mat,
const TranspositionType& transpositions, Workspace& tmp,
const WType& w,
const typename MatrixType::RealScalar& sigma=1)
391 tmp = transpositions * w;
393 return ldlt_inplace<Lower>::updateInPlace(mat,tmp,sigma);
397 template<>
struct ldlt_inplace<
Upper>
399 template<
typename MatrixType,
typename TranspositionType,
typename Workspace>
400 static EIGEN_STRONG_INLINE
bool unblocked(MatrixType& mat, TranspositionType& transpositions, Workspace& temp, SignMatrix& sign)
402 Transpose<MatrixType> matt(mat);
403 return ldlt_inplace<Lower>::unblocked(matt, transpositions, temp, sign);
406 template<
typename MatrixType,
typename TranspositionType,
typename Workspace,
typename WType>
407 static EIGEN_STRONG_INLINE
bool update(MatrixType& mat, TranspositionType& transpositions, Workspace& tmp, WType& w,
const typename MatrixType::RealScalar& sigma=1)
409 Transpose<MatrixType> matt(mat);
410 return ldlt_inplace<Lower>::update(matt, transpositions, tmp, w.conjugate(), sigma);
414 template<
typename MatrixType>
struct LDLT_Traits<MatrixType,
Lower>
416 typedef const TriangularView<const MatrixType, UnitLower> MatrixL;
417 typedef const TriangularView<const typename MatrixType::AdjointReturnType, UnitUpper> MatrixU;
418 static inline MatrixL getL(
const MatrixType& m) {
return m; }
419 static inline MatrixU getU(
const MatrixType& m) {
return m.adjoint(); }
422 template<
typename MatrixType>
struct LDLT_Traits<MatrixType,
Upper>
424 typedef const TriangularView<const typename MatrixType::AdjointReturnType, UnitLower> MatrixL;
425 typedef const TriangularView<const MatrixType, UnitUpper> MatrixU;
426 static inline MatrixL getL(
const MatrixType& m) {
return m.adjoint(); }
427 static inline MatrixU getU(
const MatrixType& m) {
return m; }
434 template<
typename MatrixType,
int _UpLo>
437 eigen_assert(a.rows()==a.cols());
438 const Index size = a.rows();
442 m_transpositions.resize(size);
443 m_isInitialized =
false;
444 m_temporary.resize(size);
446 internal::ldlt_inplace<UpLo>::unblocked(m_matrix, m_transpositions, m_temporary, m_sign);
448 m_isInitialized =
true;
457 template<
typename MatrixType,
int _UpLo>
458 template<
typename Derived>
461 const Index size = w.rows();
464 eigen_assert(m_matrix.rows()==size);
468 m_matrix.resize(size,size);
470 m_transpositions.resize(size);
471 for (Index i = 0; i < size; i++)
472 m_transpositions.coeffRef(i) = i;
473 m_temporary.resize(size);
474 m_sign = sigma>=0 ? internal::PositiveSemiDef : internal::NegativeSemiDef;
475 m_isInitialized =
true;
478 internal::ldlt_inplace<UpLo>::update(m_matrix, m_transpositions, m_temporary, w, sigma);
484 template<
typename _MatrixType,
int _UpLo,
typename Rhs>
485 struct solve_retval<
LDLT<_MatrixType,_UpLo>, Rhs>
486 : solve_retval_base<LDLT<_MatrixType,_UpLo>, Rhs>
489 EIGEN_MAKE_SOLVE_HELPERS(LDLTType,Rhs)
491 template<typename Dest>
void evalTo(Dest& dst)
const
493 eigen_assert(rhs().rows() == dec().matrixLDLT().rows());
495 dst = dec().transpositionsP() * rhs();
498 dec().matrixL().solveInPlace(dst);
504 typedef typename LDLTType::MatrixType MatrixType;
505 typedef typename LDLTType::Scalar Scalar;
506 typedef typename LDLTType::RealScalar RealScalar;
516 for (Index i = 0; i < vectorD.size(); ++i) {
517 if(abs(vectorD(i)) > tolerance)
518 dst.row(i) /= vectorD(i);
520 dst.row(i).setZero();
524 dec().matrixU().solveInPlace(dst);
527 dst = dec().transpositionsP().transpose() * dst;
545 template<
typename MatrixType,
int _UpLo>
546 template<
typename Derived>
547 bool LDLT<MatrixType,_UpLo>::solveInPlace(MatrixBase<Derived> &bAndX)
const
549 eigen_assert(m_isInitialized &&
"LDLT is not initialized.");
550 eigen_assert(m_matrix.rows() == bAndX.rows());
552 bAndX = this->solve(bAndX);
560 template<
typename MatrixType,
int _UpLo>
563 eigen_assert(m_isInitialized &&
"LDLT is not initialized.");
564 const Index size = m_matrix.rows();
565 MatrixType res(size,size);
569 res = transpositionsP() * res;
571 res = matrixU() * res;
573 res = vectorD().real().asDiagonal() * res;
575 res = matrixL() * res;
577 res = transpositionsP().transpose() * res;
585 template<
typename MatrixType,
unsigned int UpLo>
595 template<
typename Derived>
604 #endif // EIGEN_LDLT_H
Robust Cholesky decomposition of a matrix with pivoting.
Definition: LDLT.h:48
ComputationInfo info() const
Reports whether previous computation was successful.
Definition: LDLT.h:231
LDLT(const MatrixType &matrix)
Constructor with decomposition.
Definition: LDLT.h:101
MatrixType reconstructedMatrix() const
Definition: LDLT.h:561
const TranspositionType & transpositionsP() const
Definition: LDLT.h:135
Holds information about the various numeric (i.e. scalar) types allowed by Eigen. ...
Definition: NumTraits.h:88
Traits::MatrixL matrixL() const
Definition: LDLT.h:127
const internal::solve_retval< LDLT, Rhs > solve(const MatrixBase< Rhs > &b) const
Definition: LDLT.h:186
bool isPositive() const
Definition: LDLT.h:149
const LDLT< PlainObject, UpLo > ldlt() const
Definition: LDLT.h:587
Definition: Constants.h:169
Definition: Constants.h:167
LDLT & compute(const MatrixType &matrix)
Definition: LDLT.h:435
Definition: Eigen_Colamd.h:54
LDLT(Index size)
Default Constructor with memory preallocation.
Definition: LDLT.h:88
void setZero()
Definition: LDLT.h:114
Diagonal< const MatrixType > vectorD() const
Definition: LDLT.h:142
LDLT()
Default Constructor.
Definition: LDLT.h:75
Definition: Constants.h:376
bool isNegative(void) const
Definition: LDLT.h:163
const unsigned int RowMajorBit
Definition: Constants.h:53
Expression of a diagonal/subdiagonal/superdiagonal in a matrix.
Definition: Diagonal.h:64
ComputationInfo
Definition: Constants.h:374
Base class for all dense matrices, vectors, and expressions.
Definition: MatrixBase.h:48
const LDLT< PlainObject > ldlt() const
Definition: LDLT.h:597
Traits::MatrixU matrixU() const
Definition: LDLT.h:120
const MatrixType & matrixLDLT() const
Definition: LDLT.h:215