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 template<
typename InputType>
104 : m_matrix(matrix.rows(), matrix.cols()),
105 m_transpositions(matrix.rows()),
106 m_temporary(matrix.rows()),
108 m_isInitialized(false)
118 m_isInitialized =
false;
122 inline typename Traits::MatrixU
matrixU()
const
124 eigen_assert(m_isInitialized &&
"LDLT is not initialized.");
125 return Traits::getU(m_matrix);
129 inline typename Traits::MatrixL
matrixL()
const
131 eigen_assert(m_isInitialized &&
"LDLT is not initialized.");
132 return Traits::getL(m_matrix);
139 eigen_assert(m_isInitialized &&
"LDLT is not initialized.");
140 return m_transpositions;
146 eigen_assert(m_isInitialized &&
"LDLT is not initialized.");
147 return m_matrix.diagonal();
153 eigen_assert(m_isInitialized &&
"LDLT is not initialized.");
154 return m_sign == internal::PositiveSemiDef || m_sign == internal::ZeroSign;
160 eigen_assert(m_isInitialized &&
"LDLT is not initialized.");
161 return m_sign == internal::NegativeSemiDef || m_sign == internal::ZeroSign;
179 template<
typename Rhs>
183 eigen_assert(m_isInitialized &&
"LDLT is not initialized.");
184 eigen_assert(m_matrix.rows()==b.rows()
185 &&
"LDLT::solve(): invalid number of rows of the right hand side matrix b");
189 template<
typename Derived>
192 template<
typename InputType>
195 template <
typename Derived>
204 eigen_assert(m_isInitialized &&
"LDLT is not initialized.");
210 inline Index rows()
const {
return m_matrix.rows(); }
211 inline Index cols()
const {
return m_matrix.cols(); }
220 eigen_assert(m_isInitialized &&
"LDLT is not initialized.");
224 #ifndef EIGEN_PARSED_BY_DOXYGEN
225 template<
typename RhsType,
typename DstType>
227 void _solve_impl(
const RhsType &rhs, DstType &dst)
const;
232 static void check_template_parameters()
234 EIGEN_STATIC_ASSERT_NON_INTEGER(Scalar);
244 TranspositionType m_transpositions;
245 TmpMatrixType m_temporary;
246 internal::SignMatrix m_sign;
247 bool m_isInitialized;
252 template<
int UpLo>
struct ldlt_inplace;
254 template<>
struct ldlt_inplace<
Lower>
256 template<
typename MatrixType,
typename TranspositionType,
typename Workspace>
257 static bool unblocked(MatrixType& mat, TranspositionType& transpositions, Workspace& temp, SignMatrix& sign)
260 typedef typename MatrixType::Scalar Scalar;
261 typedef typename MatrixType::RealScalar RealScalar;
262 typedef typename TranspositionType::StorageIndex IndexType;
263 eigen_assert(mat.rows()==mat.cols());
264 const Index size = mat.rows();
268 transpositions.setIdentity();
269 if (numext::real(mat.coeff(0,0)) > 0) sign = PositiveSemiDef;
270 else if (numext::real(mat.coeff(0,0)) < 0) sign = NegativeSemiDef;
271 else sign = ZeroSign;
275 for (Index k = 0; k < size; ++k)
278 Index index_of_biggest_in_corner;
279 mat.diagonal().tail(size-k).cwiseAbs().maxCoeff(&index_of_biggest_in_corner);
280 index_of_biggest_in_corner += k;
282 transpositions.coeffRef(k) = IndexType(index_of_biggest_in_corner);
283 if(k != index_of_biggest_in_corner)
287 Index s = size-index_of_biggest_in_corner-1;
288 mat.row(k).head(k).swap(mat.row(index_of_biggest_in_corner).head(k));
289 mat.col(k).tail(s).swap(mat.col(index_of_biggest_in_corner).tail(s));
290 std::swap(mat.coeffRef(k,k),mat.coeffRef(index_of_biggest_in_corner,index_of_biggest_in_corner));
291 for(Index i=k+1;i<index_of_biggest_in_corner;++i)
293 Scalar tmp = mat.coeffRef(i,k);
294 mat.coeffRef(i,k) = numext::conj(mat.coeffRef(index_of_biggest_in_corner,i));
295 mat.coeffRef(index_of_biggest_in_corner,i) = numext::conj(tmp);
297 if(NumTraits<Scalar>::IsComplex)
298 mat.coeffRef(index_of_biggest_in_corner,k) = numext::conj(mat.coeff(index_of_biggest_in_corner,k));
305 Index rs = size - k - 1;
306 Block<MatrixType,Dynamic,1> A21(mat,k+1,k,rs,1);
307 Block<MatrixType,1,Dynamic> A10(mat,k,0,1,k);
308 Block<MatrixType,Dynamic,Dynamic> A20(mat,k+1,0,rs,k);
312 temp.head(k) = mat.diagonal().real().head(k).asDiagonal() * A10.adjoint();
313 mat.coeffRef(k,k) -= (A10 * temp.head(k)).value();
315 A21.noalias() -= A20 * temp.head(k);
322 RealScalar realAkk = numext::real(mat.coeffRef(k,k));
323 if((rs>0) && (abs(realAkk) > RealScalar(0)))
326 if (sign == PositiveSemiDef) {
327 if (realAkk < 0) sign = Indefinite;
328 }
else if (sign == NegativeSemiDef) {
329 if (realAkk > 0) sign = Indefinite;
330 }
else if (sign == ZeroSign) {
331 if (realAkk > 0) sign = PositiveSemiDef;
332 else if (realAkk < 0) sign = NegativeSemiDef;
346 template<
typename MatrixType,
typename WDerived>
347 static bool updateInPlace(MatrixType& mat, MatrixBase<WDerived>& w,
const typename MatrixType::RealScalar& sigma=1)
349 using numext::isfinite;
350 typedef typename MatrixType::Scalar Scalar;
351 typedef typename MatrixType::RealScalar RealScalar;
353 const Index size = mat.rows();
354 eigen_assert(mat.cols() == size && w.size()==size);
356 RealScalar alpha = 1;
359 for (Index j = 0; j < size; j++)
362 if (!(isfinite)(alpha))
366 RealScalar dj = numext::real(mat.coeff(j,j));
367 Scalar wj = w.coeff(j);
368 RealScalar swj2 = sigma*numext::abs2(wj);
369 RealScalar gamma = dj*alpha + swj2;
371 mat.coeffRef(j,j) += swj2/alpha;
377 w.tail(rs) -= wj * mat.col(j).tail(rs);
379 mat.col(j).tail(rs) += (sigma*numext::conj(wj)/gamma)*w.tail(rs);
384 template<
typename MatrixType,
typename TranspositionType,
typename Workspace,
typename WType>
385 static bool update(MatrixType& mat,
const TranspositionType& transpositions, Workspace& tmp,
const WType& w,
const typename MatrixType::RealScalar& sigma=1)
388 tmp = transpositions * w;
390 return ldlt_inplace<Lower>::updateInPlace(mat,tmp,sigma);
394 template<>
struct ldlt_inplace<
Upper>
396 template<
typename MatrixType,
typename TranspositionType,
typename Workspace>
397 static EIGEN_STRONG_INLINE
bool unblocked(MatrixType& mat, TranspositionType& transpositions, Workspace& temp, SignMatrix& sign)
399 Transpose<MatrixType> matt(mat);
400 return ldlt_inplace<Lower>::unblocked(matt, transpositions, temp, sign);
403 template<
typename MatrixType,
typename TranspositionType,
typename Workspace,
typename WType>
404 static EIGEN_STRONG_INLINE
bool update(MatrixType& mat, TranspositionType& transpositions, Workspace& tmp, WType& w,
const typename MatrixType::RealScalar& sigma=1)
406 Transpose<MatrixType> matt(mat);
407 return ldlt_inplace<Lower>::update(matt, transpositions, tmp, w.conjugate(), sigma);
411 template<
typename MatrixType>
struct LDLT_Traits<MatrixType,
Lower>
413 typedef const TriangularView<const MatrixType, UnitLower> MatrixL;
414 typedef const TriangularView<const typename MatrixType::AdjointReturnType, UnitUpper> MatrixU;
415 static inline MatrixL getL(
const MatrixType& m) {
return MatrixL(m); }
416 static inline MatrixU getU(
const MatrixType& m) {
return MatrixU(m.adjoint()); }
419 template<
typename MatrixType>
struct LDLT_Traits<MatrixType,
Upper>
421 typedef const TriangularView<const typename MatrixType::AdjointReturnType, UnitLower> MatrixL;
422 typedef const TriangularView<const MatrixType, UnitUpper> MatrixU;
423 static inline MatrixL getL(
const MatrixType& m) {
return MatrixL(m.adjoint()); }
424 static inline MatrixU getU(
const MatrixType& m) {
return MatrixU(m); }
431 template<
typename MatrixType,
int _UpLo>
432 template<
typename InputType>
435 check_template_parameters();
438 const Index size = a.
rows();
442 m_transpositions.resize(size);
443 m_isInitialized =
false;
444 m_temporary.resize(size);
445 m_sign = internal::ZeroSign;
447 internal::ldlt_inplace<UpLo>::unblocked(m_matrix, m_transpositions, m_temporary, m_sign);
449 m_isInitialized =
true;
458 template<
typename MatrixType,
int _UpLo>
459 template<
typename Derived>
462 typedef typename TranspositionType::StorageIndex IndexType;
463 const Index size = w.rows();
466 eigen_assert(m_matrix.rows()==size);
470 m_matrix.resize(size,size);
472 m_transpositions.resize(size);
473 for (Index i = 0; i < size; i++)
474 m_transpositions.coeffRef(i) = IndexType(i);
475 m_temporary.resize(size);
476 m_sign = sigma>=0 ? internal::PositiveSemiDef : internal::NegativeSemiDef;
477 m_isInitialized =
true;
480 internal::ldlt_inplace<UpLo>::update(m_matrix, m_transpositions, m_temporary, w, sigma);
485 #ifndef EIGEN_PARSED_BY_DOXYGEN
486 template<
typename _MatrixType,
int _UpLo>
487 template<
typename RhsType,
typename DstType>
490 eigen_assert(rhs.rows() == rows());
492 dst = m_transpositions * rhs;
495 matrixL().solveInPlace(dst);
509 for (Index i = 0; i < vecD.size(); ++i)
511 if(abs(vecD(i)) > tolerance)
512 dst.row(i) /= vecD(i);
514 dst.row(i).setZero();
518 matrixU().solveInPlace(dst);
521 dst = m_transpositions.transpose() * dst;
538 template<
typename MatrixType,
int _UpLo>
539 template<
typename Derived>
540 bool LDLT<MatrixType,_UpLo>::solveInPlace(MatrixBase<Derived> &bAndX)
const
542 eigen_assert(m_isInitialized &&
"LDLT is not initialized.");
543 eigen_assert(m_matrix.rows() == bAndX.rows());
545 bAndX = this->solve(bAndX);
553 template<
typename MatrixType,
int _UpLo>
556 eigen_assert(m_isInitialized &&
"LDLT is not initialized.");
557 const Index size = m_matrix.rows();
558 MatrixType res(size,size);
562 res = transpositionsP() * res;
564 res = matrixU() * res;
566 res = vectorD().real().asDiagonal() * res;
568 res = matrixL() * res;
570 res = transpositionsP().transpose() * res;
580 template<
typename MatrixType,
unsigned int UpLo>
591 template<
typename Derived>
601 #endif // EIGEN_LDLT_H
Robust Cholesky decomposition of a matrix with pivoting.
Definition: LDLT.h:48
const LDLT< PlainObject > ldlt() const
Definition: LDLT.h:593
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
Derived & derived()
Definition: EigenBase.h:44
void setZero()
Definition: LDLT.h:116
const unsigned int RowMajorBit
Definition: Constants.h:61
Index rows() const
Definition: EigenBase.h:58
Definition: EigenBase.h:28
const MatrixType & matrixLDLT() const
Definition: LDLT.h:202
LDLT()
Default Constructor.
Definition: LDLT.h:76
Definition: Constants.h:204
LDLT(const EigenBase< InputType > &matrix)
Constructor with decomposition.
Definition: LDLT.h:103
bool isNegative(void) const
Definition: LDLT.h:158
MatrixType reconstructedMatrix() const
Definition: LDLT.h:554
const TranspositionType & transpositionsP() const
Definition: LDLT.h:137
Traits::MatrixU matrixU() const
Definition: LDLT.h:122
Definition: Constants.h:432
Eigen::Index Index
Definition: LDLT.h:62
const Solve< LDLT, Rhs > solve(const MatrixBase< Rhs > &b) const
Definition: LDLT.h:181
Definition: Eigen_Colamd.h:54
bool isPositive() const
Definition: LDLT.h:151
Index cols() const
Definition: EigenBase.h:61
ComputationInfo info() const
Reports whether previous computation was successful.
Definition: LDLT.h:218
Diagonal< const MatrixType > vectorD() const
Definition: LDLT.h:144
Traits::MatrixL matrixL() const
Definition: LDLT.h:129
Expression of a diagonal/subdiagonal/superdiagonal in a matrix.
Definition: Diagonal.h:63
Pseudo expression representing a solving operation.
Definition: Solve.h:62
Definition: Constants.h:206
ComputationInfo
Definition: Constants.h:430
Base class for all dense matrices, vectors, and expressions.
Definition: MatrixBase.h:48
const LDLT< PlainObject, UpLo > ldlt() const
Definition: LDLT.h:582