10#ifndef EIGEN_SIMPLICIAL_CHOLESKY_H
11#define EIGEN_SIMPLICIAL_CHOLESKY_H
15enum SimplicialCholeskyMode {
16 SimplicialCholeskyLLT,
17 SimplicialCholeskyLDLT
21 template<
typename CholMatrixType,
typename InputMatrixType>
31 template<
typename MatrixType>
56template<
typename Derived>
60 using Base::m_isInitialized;
66 typedef typename MatrixType::Scalar Scalar;
67 typedef typename MatrixType::RealScalar RealScalar;
68 typedef typename MatrixType::StorageIndex StorageIndex;
75 ColsAtCompileTime = MatrixType::ColsAtCompileTime,
76 MaxColsAtCompileTime = MatrixType::MaxColsAtCompileTime
85 : m_info(
Success), m_shiftOffset(0), m_shiftScale(1)
89 : m_info(
Success), m_shiftOffset(0), m_shiftScale(1)
91 derived().compute(matrix);
94 ~SimplicialCholeskyBase()
98 Derived& derived() {
return *
static_cast<Derived*
>(
this); }
99 const Derived& derived()
const {
return *
static_cast<const Derived*
>(
this); }
101 inline Index cols()
const {
return m_matrix.
cols(); }
102 inline Index rows()
const {
return m_matrix.
rows(); }
111 eigen_assert(m_isInitialized &&
"Decomposition is not initialized.");
134 Derived&
setShift(
const RealScalar& offset,
const RealScalar& scale = 1)
136 m_shiftOffset = offset;
137 m_shiftScale = scale;
141#ifndef EIGEN_PARSED_BY_DOXYGEN
143 template<
typename Stream>
144 void dumpMemory(Stream& s)
147 s <<
" L: " << ((
total+=(m_matrix.
cols()+1) *
sizeof(
int) + m_matrix.
nonZeros()*(
sizeof(int)+
sizeof(Scalar))) >> 20) <<
"Mb" <<
"\n";
148 s <<
" diag: " << ((
total+=m_diag.size() *
sizeof(Scalar)) >> 20) <<
"Mb" <<
"\n";
149 s <<
" tree: " << ((
total+=m_parent.size() *
sizeof(int)) >> 20) <<
"Mb" <<
"\n";
150 s <<
" nonzeros: " << ((
total+=m_nonZerosPerCol.size() *
sizeof(int)) >> 20) <<
"Mb" <<
"\n";
151 s <<
" perm: " << ((
total+=m_P.
size() *
sizeof(int)) >> 20) <<
"Mb" <<
"\n";
152 s <<
" perm^-1: " << ((
total+=m_Pinv.
size() *
sizeof(int)) >> 20) <<
"Mb" <<
"\n";
153 s <<
" TOTAL: " << (
total>> 20) <<
"Mb" <<
"\n";
157 template<
typename Rhs,
typename Dest>
158 void _solve_impl(
const MatrixBase<Rhs> &b, MatrixBase<Dest> &dest)
const
160 eigen_assert(m_factorizationIsOk &&
"The decomposition is not in a valid state for solving, you must first call either compute() or symbolic()/numeric()");
161 eigen_assert(m_matrix.
rows()==b.rows());
172 derived().matrixL().solveInPlace(dest);
175 dest = m_diag.asDiagonal().inverse() * dest;
178 derived().matrixU().solveInPlace(dest);
181 dest = m_Pinv * dest;
184 template<
typename Rhs,
typename Dest>
185 void _solve_impl(
const SparseMatrixBase<Rhs> &b, SparseMatrixBase<Dest> &dest)
const
187 internal::solve_sparse_through_dense_panels(derived(), b, dest);
195 template<
bool DoLDLT>
198 eigen_assert(matrix.rows()==matrix.cols());
199 Index size = matrix.cols();
201 ConstCholMatrixPtr
pmat;
202 ordering(matrix,
pmat, tmp);
207 template<
bool DoLDLT>
208 void factorize(
const MatrixType& a)
210 eigen_assert(a.rows()==a.cols());
211 Index size = a.cols();
212 CholMatrixType tmp(size,size);
213 ConstCholMatrixPtr
pmat;
222 tmp.template selfadjointView<Upper>() = a.template selfadjointView<UpLo>().twistedBy(m_P);
226 factorize_preordered<DoLDLT>(*pmat);
229 template<
bool DoLDLT>
230 void factorize_preordered(
const CholMatrixType& a);
232 void analyzePattern(
const MatrixType& a,
bool doLDLT)
234 eigen_assert(a.rows()==a.cols());
235 Index size = a.cols();
236 CholMatrixType tmp(size,size);
237 ConstCholMatrixPtr pmat;
238 ordering(a, pmat, tmp);
239 analyzePattern_preordered(*pmat,doLDLT);
241 void analyzePattern_preordered(
const CholMatrixType& a,
bool doLDLT);
243 void ordering(
const MatrixType& a, ConstCholMatrixPtr &pmat, CholMatrixType& ap);
247 inline bool operator() (
const Index& row,
const Index& col,
const Scalar&)
const
254 bool m_factorizationIsOk;
264 RealScalar m_shiftOffset;
265 RealScalar m_shiftScale;
268template<
typename _MatrixType,
int _UpLo = Lower,
typename _Ordering = AMDOrdering<
typename _MatrixType::StorageIndex> >
class SimplicialLLT;
269template<
typename _MatrixType,
int _UpLo = Lower,
typename _Ordering = AMDOrdering<
typename _MatrixType::StorageIndex> >
class SimplicialLDLT;
270template<
typename _MatrixType,
int _UpLo = Lower,
typename _Ordering = AMDOrdering<
typename _MatrixType::StorageIndex> >
class SimplicialCholesky;
276 typedef _MatrixType MatrixType;
278 enum { UpLo =
_UpLo };
279 typedef typename MatrixType::Scalar Scalar;
280 typedef typename MatrixType::StorageIndex StorageIndex;
284 static inline MatrixL getL(
const MatrixType& m) {
return MatrixL(m); }
285 static inline MatrixU getU(
const MatrixType& m) {
return MatrixU(m.adjoint()); }
290 typedef _MatrixType MatrixType;
292 enum { UpLo =
_UpLo };
293 typedef typename MatrixType::Scalar Scalar;
294 typedef typename MatrixType::StorageIndex StorageIndex;
298 static inline MatrixL getL(
const MatrixType& m) {
return MatrixL(m); }
299 static inline MatrixU getU(
const MatrixType& m) {
return MatrixU(m.adjoint()); }
304 typedef _MatrixType MatrixType;
306 enum { UpLo =
_UpLo };
331template<
typename _MatrixType,
int _UpLo,
typename _Ordering>
335 typedef _MatrixType MatrixType;
336 enum { UpLo =
_UpLo };
338 typedef typename MatrixType::Scalar Scalar;
339 typedef typename MatrixType::RealScalar RealScalar;
340 typedef typename MatrixType::StorageIndex StorageIndex;
344 typedef typename Traits::MatrixL MatrixL;
345 typedef typename Traits::MatrixU MatrixU;
355 eigen_assert(Base::m_factorizationIsOk &&
"Simplicial LLT not factorized");
356 return Traits::getL(Base::m_matrix);
361 eigen_assert(Base::m_factorizationIsOk &&
"Simplicial LLT not factorized");
362 return Traits::getU(Base::m_matrix);
380 Base::analyzePattern(a,
false);
397 Scalar
detL = Base::m_matrix.diagonal().prod();
398 return numext::abs2(
detL);
422template<
typename _MatrixType,
int _UpLo,
typename _Ordering>
426 typedef _MatrixType MatrixType;
427 enum { UpLo =
_UpLo };
429 typedef typename MatrixType::Scalar Scalar;
430 typedef typename MatrixType::RealScalar RealScalar;
431 typedef typename MatrixType::StorageIndex StorageIndex;
435 typedef typename Traits::MatrixL MatrixL;
436 typedef typename Traits::MatrixU MatrixU;
447 eigen_assert(Base::m_factorizationIsOk &&
"Simplicial LDLT not factorized");
452 eigen_assert(Base::m_factorizationIsOk &&
"Simplicial LDLT not factorized");
453 return Traits::getL(Base::m_matrix);
458 eigen_assert(Base::m_factorizationIsOk &&
"Simplicial LDLT not factorized");
459 return Traits::getU(Base::m_matrix);
477 Base::analyzePattern(a,
true);
494 return Base::m_diag.prod();
504template<
typename _MatrixType,
int _UpLo,
typename _Ordering>
508 typedef _MatrixType MatrixType;
509 enum { UpLo =
_UpLo };
511 typedef typename MatrixType::Scalar Scalar;
512 typedef typename MatrixType::RealScalar RealScalar;
513 typedef typename MatrixType::StorageIndex StorageIndex;
523 :
Base(), m_LDLT(
true)
532 case SimplicialCholeskyLLT:
535 case SimplicialCholeskyLDLT:
546 eigen_assert(Base::m_factorizationIsOk &&
"Simplicial Cholesky not factorized");
550 eigen_assert(Base::m_factorizationIsOk &&
"Simplicial Cholesky not factorized");
551 return Base::m_matrix;
572 Base::analyzePattern(a, m_LDLT);
590 template<
typename Rhs,
typename Dest>
593 eigen_assert(Base::m_factorizationIsOk &&
"The decomposition is not in a valid state for solving, you must first call either compute() or symbolic()/numeric()");
594 eigen_assert(Base::m_matrix.rows()==b.rows());
599 if(Base::m_P.size()>0)
600 dest = Base::m_P * b;
604 if(Base::m_matrix.nonZeros()>0)
607 LDLTTraits::getL(Base::m_matrix).solveInPlace(dest);
609 LLTTraits::getL(Base::m_matrix).solveInPlace(dest);
612 if(Base::m_diag.size()>0)
613 dest = Base::m_diag.asDiagonal().inverse() * dest;
615 if (Base::m_matrix.nonZeros()>0)
618 LDLTTraits::getU(Base::m_matrix).solveInPlace(dest);
620 LLTTraits::getU(Base::m_matrix).solveInPlace(dest);
623 if(Base::m_P.size()>0)
624 dest = Base::m_Pinv * dest;
628 template<
typename Rhs,
typename Dest>
629 void _solve_impl(
const SparseMatrixBase<Rhs> &b, SparseMatrixBase<Dest> &dest)
const
631 internal::solve_sparse_through_dense_panels(*
this, b, dest);
634 Scalar determinant()
const
638 return Base::m_diag.prod();
642 Scalar detL = Diagonal<const CholMatrixType>(Base::m_matrix).prod();
643 return numext::abs2(detL);
651template<
typename Derived>
652void SimplicialCholeskyBase<Derived>::ordering(
const MatrixType& a, ConstCholMatrixPtr &pmat, CholMatrixType& ap)
654 eigen_assert(a.rows()==a.cols());
655 const Index size = a.rows();
658 if(!internal::is_same<OrderingType,NaturalOrdering<Index> >::value)
662 C = a.template selfadjointView<UpLo>();
664 OrderingType ordering;
668 if(m_Pinv.size()>0) m_P = m_Pinv.inverse();
671 ap.resize(size,size);
672 ap.template selfadjointView<Upper>() = a.template selfadjointView<UpLo>().twistedBy(m_P);
678 if(
int(UpLo)==
int(
Lower) || MatrixType::IsRowMajor)
681 ap.resize(size,size);
682 ap.template selfadjointView<Upper>() = a.template selfadjointView<UpLo>();
685 internal::simplicial_cholesky_grab_input<CholMatrixType,MatrixType>::run(a, pmat, ap);
Index size() const
Definition PermutationMatrix.h:109
A direct sparse Cholesky factorizations.
Definition SimplicialCholesky.h:58
SimplicialCholeskyBase()
Default constructor.
Definition SimplicialCholesky.h:84
ComputationInfo info() const
Reports whether previous computation was successful.
Definition SimplicialCholesky.h:109
const PermutationMatrix< Dynamic, Dynamic, StorageIndex > & permutationP() const
Definition SimplicialCholesky.h:117
Derived & setShift(const RealScalar &offset, const RealScalar &scale=1)
Sets the shift parameters that will be used to adjust the diagonal coefficients during the numerical ...
Definition SimplicialCholesky.h:134
void compute(const MatrixType &matrix)
Computes the sparse Cholesky decomposition of matrix.
Definition SimplicialCholesky.h:196
const PermutationMatrix< Dynamic, Dynamic, StorageIndex > & permutationPinv() const
Definition SimplicialCholesky.h:122
Definition SimplicialCholesky.h:506
SimplicialCholesky & compute(const MatrixType &matrix)
Computes the sparse Cholesky decomposition of matrix.
Definition SimplicialCholesky.h:555
void analyzePattern(const MatrixType &a)
Performs a symbolic decomposition on the sparcity of matrix.
Definition SimplicialCholesky.h:570
void factorize(const MatrixType &a)
Performs a numeric decomposition of matrix.
Definition SimplicialCholesky.h:581
A direct sparse LDLT Cholesky factorizations without square root.
Definition SimplicialCholesky.h:424
SimplicialLDLT(const MatrixType &matrix)
Constructs and performs the LLT factorization of matrix.
Definition SimplicialCholesky.h:442
SimplicialLDLT()
Default constructor.
Definition SimplicialCholesky.h:439
SimplicialLDLT & compute(const MatrixType &matrix)
Computes the sparse Cholesky decomposition of matrix.
Definition SimplicialCholesky.h:463
void factorize(const MatrixType &a)
Performs a numeric decomposition of matrix.
Definition SimplicialCholesky.h:486
Scalar determinant() const
Definition SimplicialCholesky.h:492
void analyzePattern(const MatrixType &a)
Performs a symbolic decomposition on the sparcity of matrix.
Definition SimplicialCholesky.h:475
const VectorType vectorD() const
Definition SimplicialCholesky.h:446
const MatrixL matrixL() const
Definition SimplicialCholesky.h:451
const MatrixU matrixU() const
Definition SimplicialCholesky.h:457
A direct sparse LLT Cholesky factorizations.
Definition SimplicialCholesky.h:333
const MatrixU matrixU() const
Definition SimplicialCholesky.h:360
SimplicialLLT(const MatrixType &matrix)
Constructs and performs the LLT factorization of matrix.
Definition SimplicialCholesky.h:350
SimplicialLLT & compute(const MatrixType &matrix)
Computes the sparse Cholesky decomposition of matrix.
Definition SimplicialCholesky.h:366
void factorize(const MatrixType &a)
Performs a numeric decomposition of matrix.
Definition SimplicialCholesky.h:389
Scalar determinant() const
Definition SimplicialCholesky.h:395
SimplicialLLT()
Default constructor.
Definition SimplicialCholesky.h:348
void analyzePattern(const MatrixType &a)
Performs a symbolic decomposition on the sparcity of matrix.
Definition SimplicialCholesky.h:378
const MatrixL matrixL() const
Definition SimplicialCholesky.h:354
Pseudo expression representing a solving operation.
Definition Solve.h:63
Index nonZeros() const
Definition SparseCompressedBase.h:46
Index rows() const
Definition SparseMatrix.h:131
Index cols() const
Definition SparseMatrix.h:133
A base class for sparse solvers.
Definition SparseSolverBase.h:54
ComputationInfo
Enum for reporting the status of a computation.
Definition Constants.h:430
@ Lower
View matrix as a lower triangular matrix.
Definition Constants.h:204
@ Upper
View matrix as an upper triangular matrix.
Definition Constants.h:206
@ Success
Computation was successful.
Definition Constants.h:432
keeps off-diagonal entries; drops diagonal entries
Definition SimplicialCholesky.h:246
Definition ForwardDeclarations.h:17