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,
const 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)
252 typedef typename MatrixType::Scalar Scalar;
253 typedef typename MatrixType::RealScalar RealScalar;
254 typedef typename MatrixType::Index Index;
255 eigen_assert(mat.rows()==mat.cols());
256 const Index size = mat.rows();
260 transpositions.setIdentity();
262 *sign = numext::real(mat.coeff(0,0))>0 ? 1:-1;
266 RealScalar cutoff(0), biggest_in_corner;
268 for (Index k = 0; k < size; ++k)
271 Index index_of_biggest_in_corner;
272 biggest_in_corner = mat.diagonal().tail(size-k).cwiseAbs().maxCoeff(&index_of_biggest_in_corner);
273 index_of_biggest_in_corner += k;
280 cutoff = abs(NumTraits<Scalar>::epsilon() * biggest_in_corner);
284 if(biggest_in_corner < cutoff)
286 for(Index i = k; i < size; i++) transpositions.coeffRef(i) = i;
291 transpositions.coeffRef(k) = index_of_biggest_in_corner;
292 if(k != index_of_biggest_in_corner)
296 Index s = size-index_of_biggest_in_corner-1;
297 mat.row(k).head(k).swap(mat.row(index_of_biggest_in_corner).head(k));
298 mat.col(k).tail(s).swap(mat.col(index_of_biggest_in_corner).tail(s));
299 std::swap(mat.coeffRef(k,k),mat.coeffRef(index_of_biggest_in_corner,index_of_biggest_in_corner));
300 for(
int i=k+1;i<index_of_biggest_in_corner;++i)
302 Scalar tmp = mat.coeffRef(i,k);
303 mat.coeffRef(i,k) = numext::conj(mat.coeffRef(index_of_biggest_in_corner,i));
304 mat.coeffRef(index_of_biggest_in_corner,i) = numext::conj(tmp);
306 if(NumTraits<Scalar>::IsComplex)
307 mat.coeffRef(index_of_biggest_in_corner,k) = numext::conj(mat.coeff(index_of_biggest_in_corner,k));
314 Index rs = size - k - 1;
315 Block<MatrixType,Dynamic,1> A21(mat,k+1,k,rs,1);
316 Block<MatrixType,1,Dynamic> A10(mat,k,0,1,k);
317 Block<MatrixType,Dynamic,Dynamic> A20(mat,k+1,0,rs,k);
321 temp.head(k) = mat.diagonal().head(k).asDiagonal() * A10.adjoint();
322 mat.coeffRef(k,k) -= (A10 * temp.head(k)).value();
324 A21.noalias() -= A20 * temp.head(k);
326 if((rs>0) && (abs(mat.coeffRef(k,k)) > cutoff))
327 A21 /= mat.coeffRef(k,k);
332 int newSign = numext::real(mat.diagonal().coeff(index_of_biggest_in_corner)) > 0;
335 else if(*sign != newSign)
350 template<
typename MatrixType,
typename WDerived>
351 static bool updateInPlace(MatrixType& mat, MatrixBase<WDerived>& w,
const typename MatrixType::RealScalar& sigma=1)
353 using numext::isfinite;
354 typedef typename MatrixType::Scalar Scalar;
355 typedef typename MatrixType::RealScalar RealScalar;
356 typedef typename MatrixType::Index Index;
358 const Index size = mat.rows();
359 eigen_assert(mat.cols() == size && w.size()==size);
361 RealScalar alpha = 1;
364 for (Index j = 0; j < size; j++)
367 if (!(isfinite)(alpha))
371 RealScalar dj = numext::real(mat.coeff(j,j));
372 Scalar wj = w.coeff(j);
373 RealScalar swj2 = sigma*numext::abs2(wj);
374 RealScalar gamma = dj*alpha + swj2;
376 mat.coeffRef(j,j) += swj2/alpha;
382 w.tail(rs) -= wj * mat.col(j).tail(rs);
384 mat.col(j).tail(rs) += (sigma*numext::conj(wj)/gamma)*w.tail(rs);
389 template<
typename MatrixType,
typename TranspositionType,
typename Workspace,
typename WType>
390 static bool update(MatrixType& mat,
const TranspositionType& transpositions, Workspace& tmp,
const WType& w,
const typename MatrixType::RealScalar& sigma=1)
393 tmp = transpositions * w;
395 return ldlt_inplace<Lower>::updateInPlace(mat,tmp,sigma);
399 template<>
struct ldlt_inplace<
Upper>
401 template<
typename MatrixType,
typename TranspositionType,
typename Workspace>
402 static EIGEN_STRONG_INLINE
bool unblocked(MatrixType& mat, TranspositionType& transpositions, Workspace& temp,
int* sign=0)
404 Transpose<MatrixType> matt(mat);
405 return ldlt_inplace<Lower>::unblocked(matt, transpositions, temp, sign);
408 template<
typename MatrixType,
typename TranspositionType,
typename Workspace,
typename WType>
409 static EIGEN_STRONG_INLINE
bool update(MatrixType& mat, TranspositionType& transpositions, Workspace& tmp, WType& w,
const typename MatrixType::RealScalar& sigma=1)
411 Transpose<MatrixType> matt(mat);
412 return ldlt_inplace<Lower>::update(matt, transpositions, tmp, w.conjugate(), sigma);
416 template<
typename MatrixType>
struct LDLT_Traits<MatrixType,
Lower>
418 typedef const TriangularView<const MatrixType, UnitLower> MatrixL;
419 typedef const TriangularView<const typename MatrixType::AdjointReturnType, UnitUpper> MatrixU;
420 static inline MatrixL getL(
const MatrixType& m) {
return m; }
421 static inline MatrixU getU(
const MatrixType& m) {
return m.adjoint(); }
424 template<
typename MatrixType>
struct LDLT_Traits<MatrixType,
Upper>
426 typedef const TriangularView<const typename MatrixType::AdjointReturnType, UnitLower> MatrixL;
427 typedef const TriangularView<const MatrixType, UnitUpper> MatrixU;
428 static inline MatrixL getL(
const MatrixType& m) {
return m.adjoint(); }
429 static inline MatrixU getU(
const MatrixType& m) {
return m; }
436 template<
typename MatrixType,
int _UpLo>
439 eigen_assert(a.rows()==a.cols());
440 const Index size = a.rows();
444 m_transpositions.resize(size);
445 m_isInitialized =
false;
446 m_temporary.resize(size);
448 internal::ldlt_inplace<UpLo>::unblocked(m_matrix, m_transpositions, m_temporary, &m_sign);
450 m_isInitialized =
true;
459 template<
typename MatrixType,
int _UpLo>
460 template<
typename Derived>
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) = i;
475 m_temporary.resize(size);
476 m_sign = sigma>=0 ? 1 : -1;
477 m_isInitialized =
true;
480 internal::ldlt_inplace<UpLo>::update(m_matrix, m_transpositions, m_temporary, w, sigma);
486 template<
typename _MatrixType,
int _UpLo,
typename Rhs>
487 struct solve_retval<
LDLT<_MatrixType,_UpLo>, Rhs>
488 : solve_retval_base<LDLT<_MatrixType,_UpLo>, Rhs>
491 EIGEN_MAKE_SOLVE_HELPERS(LDLTType,Rhs)
493 template<typename Dest>
void evalTo(Dest& dst)
const
495 eigen_assert(rhs().rows() == dec().matrixLDLT().rows());
497 dst = dec().transpositionsP() * rhs();
500 dec().matrixL().solveInPlace(dst);
506 typedef typename LDLTType::MatrixType MatrixType;
507 typedef typename LDLTType::Scalar Scalar;
508 typedef typename LDLTType::RealScalar RealScalar;
512 for (Index i = 0; i < vectorD.size(); ++i) {
513 if(abs(vectorD(i)) > tolerance)
514 dst.row(i) /= vectorD(i);
516 dst.row(i).setZero();
520 dec().matrixU().solveInPlace(dst);
523 dst = dec().transpositionsP().transpose() * dst;
541 template<
typename MatrixType,
int _UpLo>
542 template<
typename Derived>
543 bool LDLT<MatrixType,_UpLo>::solveInPlace(MatrixBase<Derived> &bAndX)
const
545 eigen_assert(m_isInitialized &&
"LDLT is not initialized.");
546 eigen_assert(m_matrix.rows() == bAndX.rows());
548 bAndX = this->solve(bAndX);
556 template<
typename MatrixType,
int _UpLo>
559 eigen_assert(m_isInitialized &&
"LDLT is not initialized.");
560 const Index size = m_matrix.rows();
561 MatrixType res(size,size);
565 res = transpositionsP() * res;
567 res = matrixU() * res;
569 res = vectorD().asDiagonal() * res;
571 res = matrixL() * res;
573 res = transpositionsP().transpose() * res;
581 template<
typename MatrixType,
unsigned int UpLo>
591 template<
typename Derived>
600 #endif // EIGEN_LDLT_H