10 #ifndef EIGEN_SIMPLICIAL_CHOLESKY_H
11 #define EIGEN_SIMPLICIAL_CHOLESKY_H
15 enum SimplicialCholeskyMode {
16 SimplicialCholeskyLLT,
17 SimplicialCholeskyLDLT
21 template<
typename CholMatrixType,
typename InputMatrixType>
23 typedef CholMatrixType
const * ConstCholMatrixPtr;
24 static void run(
const InputMatrixType& input, ConstCholMatrixPtr &pmat, CholMatrixType &tmp)
31 template<
typename MatrixType>
33 typedef MatrixType
const * ConstMatrixPtr;
34 static void run(
const MatrixType& input, ConstMatrixPtr &pmat, MatrixType &)
54 template<
typename Derived>
58 using Base::m_isInitialized;
64 typedef typename MatrixType::Scalar Scalar;
65 typedef typename MatrixType::RealScalar RealScalar;
66 typedef typename MatrixType::StorageIndex StorageIndex;
73 ColsAtCompileTime = MatrixType::ColsAtCompileTime,
74 MaxColsAtCompileTime = MatrixType::MaxColsAtCompileTime
84 m_factorizationIsOk(false),
85 m_analysisIsOk(false),
92 m_factorizationIsOk(false),
93 m_analysisIsOk(false),
97 derived().compute(matrix);
104 Derived& derived() {
return *
static_cast<Derived*
>(
this); }
105 const Derived& derived()
const {
return *
static_cast<const Derived*
>(
this); }
107 inline Index cols()
const {
return m_matrix.
cols(); }
108 inline Index rows()
const {
return m_matrix.
rows(); }
117 eigen_assert(m_isInitialized &&
"Decomposition is not initialized.");
140 Derived&
setShift(
const RealScalar& offset,
const RealScalar& scale = 1)
142 m_shiftOffset = offset;
143 m_shiftScale = scale;
147 #ifndef EIGEN_PARSED_BY_DOXYGEN
149 template<
typename Stream>
150 void dumpMemory(Stream& s)
153 s <<
" L: " << ((total+=(m_matrix.
cols()+1) *
sizeof(
int) + m_matrix.
nonZeros()*(
sizeof(int)+
sizeof(Scalar))) >> 20) <<
"Mb" <<
"\n";
154 s <<
" diag: " << ((total+=m_diag.size() *
sizeof(Scalar)) >> 20) <<
"Mb" <<
"\n";
155 s <<
" tree: " << ((total+=m_parent.size() *
sizeof(int)) >> 20) <<
"Mb" <<
"\n";
156 s <<
" nonzeros: " << ((total+=m_nonZerosPerCol.size() *
sizeof(int)) >> 20) <<
"Mb" <<
"\n";
157 s <<
" perm: " << ((total+=m_P.
size() *
sizeof(int)) >> 20) <<
"Mb" <<
"\n";
158 s <<
" perm^-1: " << ((total+=m_Pinv.
size() *
sizeof(int)) >> 20) <<
"Mb" <<
"\n";
159 s <<
" TOTAL: " << (total>> 20) <<
"Mb" <<
"\n";
163 template<
typename Rhs,
typename Dest>
164 void _solve_impl(
const MatrixBase<Rhs> &b, MatrixBase<Dest> &dest)
const
166 eigen_assert(m_factorizationIsOk &&
"The decomposition is not in a valid state for solving, you must first call either compute() or symbolic()/numeric()");
167 eigen_assert(m_matrix.
rows()==b.rows());
178 derived().matrixL().solveInPlace(dest);
181 dest = m_diag.asDiagonal().inverse() * dest;
184 derived().matrixU().solveInPlace(dest);
187 dest = m_Pinv * dest;
190 template<
typename Rhs,
typename Dest>
191 void _solve_impl(
const SparseMatrixBase<Rhs> &b, SparseMatrixBase<Dest> &dest)
const
193 internal::solve_sparse_through_dense_panels(derived(), b, dest);
196 #endif // EIGEN_PARSED_BY_DOXYGEN
201 template<
bool DoLDLT>
204 eigen_assert(matrix.rows()==matrix.cols());
205 Index size = matrix.cols();
207 ConstCholMatrixPtr pmat;
208 ordering(matrix, pmat, tmp);
209 analyzePattern_preordered(*pmat, DoLDLT);
210 factorize_preordered<DoLDLT>(*pmat);
213 template<
bool DoLDLT>
214 void factorize(
const MatrixType& a)
216 eigen_assert(a.rows()==a.cols());
217 Index size = a.cols();
218 CholMatrixType tmp(size,size);
219 ConstCholMatrixPtr pmat;
228 tmp.template selfadjointView<Upper>() = a.template selfadjointView<UpLo>().twistedBy(m_P);
232 factorize_preordered<DoLDLT>(*pmat);
235 template<
bool DoLDLT>
236 void factorize_preordered(
const CholMatrixType& a);
238 void analyzePattern(
const MatrixType& a,
bool doLDLT)
240 eigen_assert(a.rows()==a.cols());
241 Index size = a.cols();
242 CholMatrixType tmp(size,size);
243 ConstCholMatrixPtr pmat;
244 ordering(a, pmat, tmp);
245 analyzePattern_preordered(*pmat,doLDLT);
247 void analyzePattern_preordered(
const CholMatrixType& a,
bool doLDLT);
249 void ordering(
const MatrixType& a, ConstCholMatrixPtr &pmat, CholMatrixType& ap);
253 inline bool operator() (
const Index& row,
const Index& col,
const Scalar&)
const
260 bool m_factorizationIsOk;
270 RealScalar m_shiftOffset;
271 RealScalar m_shiftScale;
274 template<
typename _MatrixType,
int _UpLo = Lower,
typename _Ordering = AMDOrdering<
typename _MatrixType::StorageIndex> >
class SimplicialLLT;
275 template<
typename _MatrixType,
int _UpLo = Lower,
typename _Ordering = AMDOrdering<
typename _MatrixType::StorageIndex> >
class SimplicialLDLT;
276 template<
typename _MatrixType,
int _UpLo = Lower,
typename _Ordering = AMDOrdering<
typename _MatrixType::StorageIndex> >
class SimplicialCholesky;
280 template<
typename _MatrixType,
int _UpLo,
typename _Ordering>
struct traits<
SimplicialLLT<_MatrixType,_UpLo,_Ordering> >
282 typedef _MatrixType MatrixType;
283 typedef _Ordering OrderingType;
284 enum { UpLo = _UpLo };
285 typedef typename MatrixType::Scalar Scalar;
286 typedef typename MatrixType::StorageIndex StorageIndex;
290 static inline MatrixL getL(
const MatrixType& m) {
return MatrixL(m); }
291 static inline MatrixU getU(
const MatrixType& m) {
return MatrixU(m.adjoint()); }
294 template<
typename _MatrixType,
int _UpLo,
typename _Ordering>
struct traits<
SimplicialLDLT<_MatrixType,_UpLo,_Ordering> >
296 typedef _MatrixType MatrixType;
297 typedef _Ordering OrderingType;
298 enum { UpLo = _UpLo };
299 typedef typename MatrixType::Scalar Scalar;
300 typedef typename MatrixType::StorageIndex StorageIndex;
304 static inline MatrixL getL(
const MatrixType& m) {
return MatrixL(m); }
305 static inline MatrixU getU(
const MatrixType& m) {
return MatrixU(m.adjoint()); }
310 typedef _MatrixType MatrixType;
311 typedef _Ordering OrderingType;
312 enum { UpLo = _UpLo };
337 template<
typename _MatrixType,
int _UpLo,
typename _Ordering>
341 typedef _MatrixType MatrixType;
342 enum { UpLo = _UpLo };
344 typedef typename MatrixType::Scalar Scalar;
345 typedef typename MatrixType::RealScalar RealScalar;
346 typedef typename MatrixType::StorageIndex StorageIndex;
350 typedef typename Traits::MatrixL MatrixL;
351 typedef typename Traits::MatrixU MatrixU;
361 eigen_assert(Base::m_factorizationIsOk &&
"Simplicial LLT not factorized");
362 return Traits::getL(Base::m_matrix);
367 eigen_assert(Base::m_factorizationIsOk &&
"Simplicial LLT not factorized");
368 return Traits::getU(Base::m_matrix);
374 Base::template compute<false>(matrix);
386 Base::analyzePattern(a,
false);
397 Base::template factorize<false>(a);
403 Scalar detL = Base::m_matrix.diagonal().prod();
404 return numext::abs2(detL);
428 template<
typename _MatrixType,
int _UpLo,
typename _Ordering>
432 typedef _MatrixType MatrixType;
433 enum { UpLo = _UpLo };
435 typedef typename MatrixType::Scalar Scalar;
436 typedef typename MatrixType::RealScalar RealScalar;
437 typedef typename MatrixType::StorageIndex StorageIndex;
441 typedef typename Traits::MatrixL MatrixL;
442 typedef typename Traits::MatrixU MatrixU;
453 eigen_assert(Base::m_factorizationIsOk &&
"Simplicial LDLT not factorized");
458 eigen_assert(Base::m_factorizationIsOk &&
"Simplicial LDLT not factorized");
459 return Traits::getL(Base::m_matrix);
464 eigen_assert(Base::m_factorizationIsOk &&
"Simplicial LDLT not factorized");
465 return Traits::getU(Base::m_matrix);
471 Base::template compute<true>(matrix);
483 Base::analyzePattern(a,
true);
494 Base::template factorize<true>(a);
500 return Base::m_diag.prod();
510 template<
typename _MatrixType,
int _UpLo,
typename _Ordering>
514 typedef _MatrixType MatrixType;
515 enum { UpLo = _UpLo };
517 typedef typename MatrixType::Scalar Scalar;
518 typedef typename MatrixType::RealScalar RealScalar;
519 typedef typename MatrixType::StorageIndex StorageIndex;
529 :
Base(), m_LDLT(
true)
538 case SimplicialCholeskyLLT:
541 case SimplicialCholeskyLDLT:
552 eigen_assert(Base::m_factorizationIsOk &&
"Simplicial Cholesky not factorized");
556 eigen_assert(Base::m_factorizationIsOk &&
"Simplicial Cholesky not factorized");
557 return Base::m_matrix;
564 Base::template compute<true>(matrix);
566 Base::template compute<false>(matrix);
578 Base::analyzePattern(a, m_LDLT);
590 Base::template factorize<true>(a);
592 Base::template factorize<false>(a);
596 template<
typename Rhs,
typename Dest>
599 eigen_assert(Base::m_factorizationIsOk &&
"The decomposition is not in a valid state for solving, you must first call either compute() or symbolic()/numeric()");
600 eigen_assert(Base::m_matrix.rows()==b.rows());
605 if(Base::m_P.size()>0)
606 dest = Base::m_P * b;
610 if(Base::m_matrix.nonZeros()>0)
613 LDLTTraits::getL(Base::m_matrix).solveInPlace(dest);
615 LLTTraits::getL(Base::m_matrix).solveInPlace(dest);
618 if(Base::m_diag.size()>0)
619 dest = Base::m_diag.real().
asDiagonal().inverse() * dest;
621 if (Base::m_matrix.nonZeros()>0)
624 LDLTTraits::getU(Base::m_matrix).solveInPlace(dest);
626 LLTTraits::getU(Base::m_matrix).solveInPlace(dest);
629 if(Base::m_P.size()>0)
630 dest = Base::m_Pinv * dest;
634 template<
typename Rhs,
typename Dest>
635 void _solve_impl(
const SparseMatrixBase<Rhs> &b, SparseMatrixBase<Dest> &dest)
const
637 internal::solve_sparse_through_dense_panels(*
this, b, dest);
640 Scalar determinant()
const
644 return Base::m_diag.prod();
648 Scalar detL = Diagonal<const CholMatrixType>(Base::m_matrix).prod();
649 return numext::abs2(detL);
657 template<
typename Derived>
658 void SimplicialCholeskyBase<Derived>::ordering(
const MatrixType& a, ConstCholMatrixPtr &pmat, CholMatrixType& ap)
660 eigen_assert(a.rows()==a.cols());
661 const Index size = a.rows();
664 if(!internal::is_same<OrderingType,NaturalOrdering<Index> >::value)
668 C = a.template selfadjointView<UpLo>();
670 OrderingType ordering;
674 if(m_Pinv.size()>0) m_P = m_Pinv.inverse();
677 ap.resize(size,size);
678 ap.template selfadjointView<Upper>() = a.template selfadjointView<UpLo>().twistedBy(m_P);
684 if(
int(UpLo)==
int(
Lower) || MatrixType::IsRowMajor)
687 ap.resize(size,size);
688 ap.template selfadjointView<Upper>() = a.template selfadjointView<UpLo>();
691 internal::simplicial_cholesky_grab_input<CholMatrixType,MatrixType>::run(a, pmat, ap);
697 #endif // EIGEN_SIMPLICIAL_CHOLESKY_H