19 template<
typename MatrixType,
int UpLo>
struct LDLT_Traits;
45 template<
typename _MatrixType,
int _UpLo>
class LDLT
48 typedef _MatrixType MatrixType;
50 RowsAtCompileTime = MatrixType::RowsAtCompileTime,
51 ColsAtCompileTime = MatrixType::ColsAtCompileTime,
53 MaxRowsAtCompileTime = MatrixType::MaxRowsAtCompileTime,
54 MaxColsAtCompileTime = MatrixType::MaxColsAtCompileTime,
57 typedef typename MatrixType::Scalar Scalar;
59 typedef typename MatrixType::Index Index;
65 typedef internal::LDLT_Traits<MatrixType,UpLo> Traits;
72 LDLT() : m_matrix(), m_transpositions(), m_isInitialized(false) {}
81 : m_matrix(size, size),
82 m_transpositions(size),
84 m_isInitialized(false)
92 LDLT(
const MatrixType& matrix)
93 : m_matrix(matrix.rows(), matrix.cols()),
94 m_transpositions(matrix.rows()),
95 m_temporary(matrix.rows()),
96 m_isInitialized(false)
106 m_isInitialized =
false;
110 inline typename Traits::MatrixU
matrixU()
const
112 eigen_assert(m_isInitialized &&
"LDLT is not initialized.");
113 return Traits::getU(m_matrix);
117 inline typename Traits::MatrixL
matrixL()
const
119 eigen_assert(m_isInitialized &&
"LDLT is not initialized.");
120 return Traits::getL(m_matrix);
127 eigen_assert(m_isInitialized &&
"LDLT is not initialized.");
128 return m_transpositions;
134 eigen_assert(m_isInitialized &&
"LDLT is not initialized.");
135 return m_matrix.diagonal();
141 eigen_assert(m_isInitialized &&
"LDLT is not initialized.");
145 #ifdef EIGEN2_SUPPORT
146 inline bool isPositiveDefinite()
const
155 eigen_assert(m_isInitialized &&
"LDLT is not initialized.");
174 template<
typename Rhs>
175 inline const internal::solve_retval<LDLT, Rhs>
178 eigen_assert(m_isInitialized &&
"LDLT is not initialized.");
179 eigen_assert(m_matrix.rows()==b.rows()
180 &&
"LDLT::solve(): invalid number of rows of the right hand side matrix b");
181 return internal::solve_retval<LDLT, Rhs>(*
this, b.derived());
184 #ifdef EIGEN2_SUPPORT
185 template<
typename OtherDerived,
typename ResultType>
188 *result = this->
solve(b);
193 template<
typename Derived>
194 bool solveInPlace(MatrixBase<Derived> &bAndX)
const;
198 template <
typename Derived>
199 LDLT& rankUpdate(
const MatrixBase<Derived>& w,RealScalar alpha=1);
207 eigen_assert(m_isInitialized &&
"LDLT is not initialized.");
213 inline Index rows()
const {
return m_matrix.rows(); }
214 inline Index cols()
const {
return m_matrix.cols(); }
223 eigen_assert(m_isInitialized &&
"LDLT is not initialized.");
236 TranspositionType m_transpositions;
237 TmpMatrixType m_temporary;
239 bool m_isInitialized;
244 template<
int UpLo>
struct ldlt_inplace;
246 template<>
struct ldlt_inplace<
Lower>
248 template<
typename MatrixType,
typename TranspositionType,
typename Workspace>
249 static bool unblocked(MatrixType& mat, TranspositionType& transpositions, Workspace& temp,
int* sign=0)
251 typedef typename MatrixType::Scalar Scalar;
252 typedef typename MatrixType::RealScalar RealScalar;
253 typedef typename MatrixType::Index Index;
254 eigen_assert(mat.rows()==mat.cols());
255 const Index size = mat.rows();
259 transpositions.setIdentity();
261 *sign = real(mat.coeff(0,0))>0 ? 1:-1;
265 RealScalar cutoff(0), biggest_in_corner;
267 for (Index k = 0; k < size; ++k)
270 Index index_of_biggest_in_corner;
271 biggest_in_corner = mat.diagonal().tail(size-k).cwiseAbs().maxCoeff(&index_of_biggest_in_corner);
272 index_of_biggest_in_corner += k;
279 cutoff = abs(NumTraits<Scalar>::epsilon() * biggest_in_corner);
282 *sign = real(mat.diagonal().coeff(index_of_biggest_in_corner)) > 0 ? 1 : -1;
286 if(biggest_in_corner < cutoff)
288 for(Index i = k; i < size; i++) transpositions.coeffRef(i) = i;
292 transpositions.coeffRef(k) = index_of_biggest_in_corner;
293 if(k != index_of_biggest_in_corner)
297 Index s = size-index_of_biggest_in_corner-1;
298 mat.row(k).head(k).swap(mat.row(index_of_biggest_in_corner).head(k));
299 mat.col(k).tail(s).swap(mat.col(index_of_biggest_in_corner).tail(s));
300 std::swap(mat.coeffRef(k,k),mat.coeffRef(index_of_biggest_in_corner,index_of_biggest_in_corner));
301 for(
int i=k+1;i<index_of_biggest_in_corner;++i)
303 Scalar tmp = mat.coeffRef(i,k);
304 mat.coeffRef(i,k) = conj(mat.coeffRef(index_of_biggest_in_corner,i));
305 mat.coeffRef(index_of_biggest_in_corner,i) = conj(tmp);
307 if(NumTraits<Scalar>::IsComplex)
308 mat.coeffRef(index_of_biggest_in_corner,k) = conj(mat.coeff(index_of_biggest_in_corner,k));
315 Index rs = size - k - 1;
316 Block<MatrixType,Dynamic,1> A21(mat,k+1,k,rs,1);
317 Block<MatrixType,1,Dynamic> A10(mat,k,0,1,k);
318 Block<MatrixType,Dynamic,Dynamic> A20(mat,k+1,0,rs,k);
322 temp.head(k) = mat.diagonal().head(k).asDiagonal() * A10.adjoint();
323 mat.coeffRef(k,k) -= (A10 * temp.head(k)).value();
325 A21.noalias() -= A20 * temp.head(k);
327 if((rs>0) && (abs(mat.coeffRef(k,k)) > cutoff))
328 A21 /= mat.coeffRef(k,k);
341 template<
typename MatrixType,
typename WDerived>
342 static bool updateInPlace(MatrixType& mat, MatrixBase<WDerived>& w,
typename MatrixType::RealScalar sigma=1)
344 using internal::isfinite;
345 typedef typename MatrixType::Scalar Scalar;
346 typedef typename MatrixType::RealScalar RealScalar;
347 typedef typename MatrixType::Index Index;
349 const Index size = mat.rows();
350 eigen_assert(mat.cols() == size && w.size()==size);
352 RealScalar alpha = 1;
355 for (Index j = 0; j < size; j++)
358 if (!(isfinite)(alpha))
362 RealScalar dj = real(mat.coeff(j,j));
363 Scalar wj = w.coeff(j);
364 RealScalar swj2 = sigma*abs2(wj);
365 RealScalar gamma = dj*alpha + swj2;
367 mat.coeffRef(j,j) += swj2/alpha;
373 w.tail(rs) -= wj * mat.col(j).tail(rs);
375 mat.col(j).tail(rs) += (sigma*conj(wj)/gamma)*w.tail(rs);
380 template<
typename MatrixType,
typename TranspositionType,
typename Workspace,
typename WType>
381 static bool update(MatrixType& mat,
const TranspositionType& transpositions, Workspace& tmp,
const WType& w,
typename MatrixType::RealScalar sigma=1)
384 tmp = transpositions * w;
386 return ldlt_inplace<Lower>::updateInPlace(mat,tmp,sigma);
390 template<>
struct ldlt_inplace<
Upper>
392 template<
typename MatrixType,
typename TranspositionType,
typename Workspace>
393 static EIGEN_STRONG_INLINE
bool unblocked(MatrixType& mat, TranspositionType& transpositions, Workspace& temp,
int* sign=0)
395 Transpose<MatrixType> matt(mat);
396 return ldlt_inplace<Lower>::unblocked(matt, transpositions, temp, sign);
399 template<
typename MatrixType,
typename TranspositionType,
typename Workspace,
typename WType>
400 static EIGEN_STRONG_INLINE
bool update(MatrixType& mat, TranspositionType& transpositions, Workspace& tmp, WType& w,
typename MatrixType::RealScalar sigma=1)
402 Transpose<MatrixType> matt(mat);
403 return ldlt_inplace<Lower>::update(matt, transpositions, tmp, w.conjugate(), sigma);
407 template<
typename MatrixType>
struct LDLT_Traits<MatrixType,
Lower>
409 typedef const TriangularView<const MatrixType, UnitLower> MatrixL;
410 typedef const TriangularView<const typename MatrixType::AdjointReturnType, UnitUpper> MatrixU;
411 static inline MatrixL getL(
const MatrixType& m) {
return m; }
412 static inline MatrixU getU(
const MatrixType& m) {
return m.adjoint(); }
415 template<
typename MatrixType>
struct LDLT_Traits<MatrixType,
Upper>
417 typedef const TriangularView<const typename MatrixType::AdjointReturnType, UnitLower> MatrixL;
418 typedef const TriangularView<const MatrixType, UnitUpper> MatrixU;
419 static inline MatrixL getL(
const MatrixType& m) {
return m.adjoint(); }
420 static inline MatrixU getU(
const MatrixType& m) {
return m; }
427 template<
typename MatrixType,
int _UpLo>
430 eigen_assert(a.rows()==a.cols());
431 const Index size = a.rows();
435 m_transpositions.resize(size);
436 m_isInitialized =
false;
437 m_temporary.resize(size);
439 internal::ldlt_inplace<UpLo>::unblocked(m_matrix, m_transpositions, m_temporary, &m_sign);
441 m_isInitialized =
true;
450 template<
typename MatrixType,
int _UpLo>
451 template<
typename Derived>
454 const Index size = w.rows();
457 eigen_assert(m_matrix.rows()==size);
461 m_matrix.resize(size,size);
463 m_transpositions.resize(size);
464 for (Index i = 0; i < size; i++)
465 m_transpositions.coeffRef(i) = i;
466 m_temporary.resize(size);
467 m_sign = sigma>=0 ? 1 : -1;
468 m_isInitialized =
true;
471 internal::ldlt_inplace<UpLo>::update(m_matrix, m_transpositions, m_temporary, w, sigma);
477 template<
typename _MatrixType,
int _UpLo,
typename Rhs>
478 struct solve_retval<
LDLT<_MatrixType,_UpLo>, Rhs>
479 : solve_retval_base<LDLT<_MatrixType,_UpLo>, Rhs>
482 EIGEN_MAKE_SOLVE_HELPERS(LDLTType,Rhs)
484 template<typename Dest>
void evalTo(Dest& dst)
const
486 eigen_assert(rhs().rows() == dec().matrixLDLT().rows());
488 dst = dec().transpositionsP() * rhs();
491 dec().matrixL().solveInPlace(dst);
497 typedef typename LDLTType::MatrixType MatrixType;
498 typedef typename LDLTType::Scalar Scalar;
499 typedef typename LDLTType::RealScalar RealScalar;
503 for (Index i = 0; i < vectorD.size(); ++i) {
504 if(abs(vectorD(i)) > tolerance)
505 dst.row(i) /= vectorD(i);
507 dst.row(i).setZero();
511 dec().matrixU().solveInPlace(dst);
514 dst = dec().transpositionsP().transpose() * dst;
532 template<
typename MatrixType,
int _UpLo>
533 template<
typename Derived>
534 bool LDLT<MatrixType,_UpLo>::solveInPlace(MatrixBase<Derived> &bAndX)
const
536 eigen_assert(m_isInitialized &&
"LDLT is not initialized.");
537 const Index size = m_matrix.rows();
538 eigen_assert(size == bAndX.rows());
540 bAndX = this->solve(bAndX);
548 template<
typename MatrixType,
int _UpLo>
551 eigen_assert(m_isInitialized &&
"LDLT is not initialized.");
552 const Index size = m_matrix.rows();
553 MatrixType res(size,size);
557 res = transpositionsP() * res;
559 res = matrixU() * res;
561 res = vectorD().asDiagonal() * res;
563 res = matrixL() * res;
565 res = transpositionsP().transpose() * res;
573 template<
typename MatrixType,
unsigned int UpLo>
583 template<
typename Derived>
592 #endif // EIGEN_LDLT_H