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);
283 if(biggest_in_corner < cutoff)
285 for(Index i = k; i < size; i++) transpositions.coeffRef(i) = i;
290 transpositions.coeffRef(k) = index_of_biggest_in_corner;
291 if(k != index_of_biggest_in_corner)
295 Index s = size-index_of_biggest_in_corner-1;
296 mat.row(k).head(k).swap(mat.row(index_of_biggest_in_corner).head(k));
297 mat.col(k).tail(s).swap(mat.col(index_of_biggest_in_corner).tail(s));
298 std::swap(mat.coeffRef(k,k),mat.coeffRef(index_of_biggest_in_corner,index_of_biggest_in_corner));
299 for(
int i=k+1;i<index_of_biggest_in_corner;++i)
301 Scalar tmp = mat.coeffRef(i,k);
302 mat.coeffRef(i,k) = conj(mat.coeffRef(index_of_biggest_in_corner,i));
303 mat.coeffRef(index_of_biggest_in_corner,i) = conj(tmp);
305 if(NumTraits<Scalar>::IsComplex)
306 mat.coeffRef(index_of_biggest_in_corner,k) = conj(mat.coeff(index_of_biggest_in_corner,k));
313 Index rs = size - k - 1;
314 Block<MatrixType,Dynamic,1> A21(mat,k+1,k,rs,1);
315 Block<MatrixType,1,Dynamic> A10(mat,k,0,1,k);
316 Block<MatrixType,Dynamic,Dynamic> A20(mat,k+1,0,rs,k);
320 temp.head(k) = mat.diagonal().head(k).asDiagonal() * A10.adjoint();
321 mat.coeffRef(k,k) -= (A10 * temp.head(k)).value();
323 A21.noalias() -= A20 * temp.head(k);
325 if((rs>0) && (abs(mat.coeffRef(k,k)) > cutoff))
326 A21 /= mat.coeffRef(k,k);
331 int newSign = real(mat.diagonal().coeff(index_of_biggest_in_corner)) > 0;
334 else if(*sign != newSign)
349 template<
typename MatrixType,
typename WDerived>
350 static bool updateInPlace(MatrixType& mat, MatrixBase<WDerived>& w,
typename MatrixType::RealScalar sigma=1)
352 using internal::isfinite;
353 typedef typename MatrixType::Scalar Scalar;
354 typedef typename MatrixType::RealScalar RealScalar;
355 typedef typename MatrixType::Index Index;
357 const Index size = mat.rows();
358 eigen_assert(mat.cols() == size && w.size()==size);
360 RealScalar alpha = 1;
363 for (Index j = 0; j < size; j++)
366 if (!(isfinite)(alpha))
370 RealScalar dj = real(mat.coeff(j,j));
371 Scalar wj = w.coeff(j);
372 RealScalar swj2 = sigma*abs2(wj);
373 RealScalar gamma = dj*alpha + swj2;
375 mat.coeffRef(j,j) += swj2/alpha;
381 w.tail(rs) -= wj * mat.col(j).tail(rs);
383 mat.col(j).tail(rs) += (sigma*conj(wj)/gamma)*w.tail(rs);
388 template<
typename MatrixType,
typename TranspositionType,
typename Workspace,
typename WType>
389 static bool update(MatrixType& mat,
const TranspositionType& transpositions, Workspace& tmp,
const WType& w,
typename MatrixType::RealScalar sigma=1)
392 tmp = transpositions * w;
394 return ldlt_inplace<Lower>::updateInPlace(mat,tmp,sigma);
398 template<>
struct ldlt_inplace<
Upper>
400 template<
typename MatrixType,
typename TranspositionType,
typename Workspace>
401 static EIGEN_STRONG_INLINE
bool unblocked(MatrixType& mat, TranspositionType& transpositions, Workspace& temp,
int* sign=0)
403 Transpose<MatrixType> matt(mat);
404 return ldlt_inplace<Lower>::unblocked(matt, transpositions, temp, sign);
407 template<
typename MatrixType,
typename TranspositionType,
typename Workspace,
typename WType>
408 static EIGEN_STRONG_INLINE
bool update(MatrixType& mat, TranspositionType& transpositions, Workspace& tmp, WType& w,
typename MatrixType::RealScalar sigma=1)
410 Transpose<MatrixType> matt(mat);
411 return ldlt_inplace<Lower>::update(matt, transpositions, tmp, w.conjugate(), sigma);
415 template<
typename MatrixType>
struct LDLT_Traits<MatrixType,
Lower>
417 typedef const TriangularView<const MatrixType, UnitLower> MatrixL;
418 typedef const TriangularView<const typename MatrixType::AdjointReturnType, UnitUpper> MatrixU;
419 static inline MatrixL getL(
const MatrixType& m) {
return m; }
420 static inline MatrixU getU(
const MatrixType& m) {
return m.adjoint(); }
423 template<
typename MatrixType>
struct LDLT_Traits<MatrixType,
Upper>
425 typedef const TriangularView<const typename MatrixType::AdjointReturnType, UnitLower> MatrixL;
426 typedef const TriangularView<const MatrixType, UnitUpper> MatrixU;
427 static inline MatrixL getL(
const MatrixType& m) {
return m.adjoint(); }
428 static inline MatrixU getU(
const MatrixType& m) {
return m; }
435 template<
typename MatrixType,
int _UpLo>
438 eigen_assert(a.rows()==a.cols());
439 const Index size = a.rows();
443 m_transpositions.resize(size);
444 m_isInitialized =
false;
445 m_temporary.resize(size);
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 const Index size = w.rows();
465 eigen_assert(m_matrix.rows()==size);
469 m_matrix.resize(size,size);
471 m_transpositions.resize(size);
472 for (Index i = 0; i < size; i++)
473 m_transpositions.coeffRef(i) = i;
474 m_temporary.resize(size);
475 m_sign = sigma>=0 ? 1 : -1;
476 m_isInitialized =
true;
479 internal::ldlt_inplace<UpLo>::update(m_matrix, m_transpositions, m_temporary, w, sigma);
485 template<
typename _MatrixType,
int _UpLo,
typename Rhs>
486 struct solve_retval<
LDLT<_MatrixType,_UpLo>, Rhs>
487 : solve_retval_base<LDLT<_MatrixType,_UpLo>, Rhs>
490 EIGEN_MAKE_SOLVE_HELPERS(LDLTType,Rhs)
492 template<typename Dest>
void evalTo(Dest& dst)
const
494 eigen_assert(rhs().rows() == dec().matrixLDLT().rows());
496 dst = dec().transpositionsP() * rhs();
499 dec().matrixL().solveInPlace(dst);
505 typedef typename LDLTType::MatrixType MatrixType;
506 typedef typename LDLTType::Scalar Scalar;
507 typedef typename LDLTType::RealScalar RealScalar;
511 for (Index i = 0; i < vectorD.size(); ++i) {
512 if(abs(vectorD(i)) > tolerance)
513 dst.row(i) /= vectorD(i);
515 dst.row(i).setZero();
519 dec().matrixU().solveInPlace(dst);
522 dst = dec().transpositionsP().transpose() * dst;
540 template<
typename MatrixType,
int _UpLo>
541 template<
typename Derived>
542 bool LDLT<MatrixType,_UpLo>::solveInPlace(MatrixBase<Derived> &bAndX)
const
544 eigen_assert(m_isInitialized &&
"LDLT is not initialized.");
545 eigen_assert(m_matrix.rows() == bAndX.rows());
547 bAndX = this->solve(bAndX);
555 template<
typename MatrixType,
int _UpLo>
558 eigen_assert(m_isInitialized &&
"LDLT is not initialized.");
559 const Index size = m_matrix.rows();
560 MatrixType res(size,size);
564 res = transpositionsP() * res;
566 res = matrixU() * res;
568 res = vectorD().asDiagonal() * res;
570 res = matrixL() * res;
572 res = transpositionsP().transpose() * res;
580 template<
typename MatrixType,
unsigned int UpLo>
590 template<
typename Derived>
599 #endif // EIGEN_LDLT_H