10#ifndef EIGEN_SIMPLICIAL_CHOLESKY_H
11#define EIGEN_SIMPLICIAL_CHOLESKY_H
14#include "./InternalHeaderCheck.h"
18enum SimplicialCholeskyMode { SimplicialCholeskyLLT, SimplicialCholeskyLDLT };
21template <
typename CholMatrixType,
typename InputMatrixType>
22struct simplicial_cholesky_grab_input {
23 typedef CholMatrixType
const* ConstCholMatrixPtr;
24 static void run(
const InputMatrixType& input, ConstCholMatrixPtr& pmat, CholMatrixType& tmp) {
30template <
typename MatrixType>
31struct simplicial_cholesky_grab_input<MatrixType, MatrixType> {
32 typedef MatrixType
const* ConstMatrixPtr;
33 static void run(
const MatrixType& input, ConstMatrixPtr& pmat, MatrixType& ) { pmat = &input; }
50template <
typename Derived>
53 using Base::m_isInitialized;
56 typedef typename internal::traits<Derived>::MatrixType MatrixType;
57 typedef typename internal::traits<Derived>::OrderingType OrderingType;
58 enum { UpLo = internal::traits<Derived>::UpLo };
59 typedef typename MatrixType::Scalar Scalar;
60 typedef typename MatrixType::RealScalar RealScalar;
61 typedef typename internal::traits<Derived>::DiagonalScalar DiagonalScalar;
62 typedef typename MatrixType::StorageIndex StorageIndex;
68 enum { ColsAtCompileTime = MatrixType::ColsAtCompileTime, MaxColsAtCompileTime = MatrixType::MaxColsAtCompileTime };
75 : m_info(
Success), m_factorizationIsOk(false), m_analysisIsOk(false), m_shiftOffset(0), m_shiftScale(1) {}
78 : m_info(
Success), m_factorizationIsOk(false), m_analysisIsOk(false), m_shiftOffset(0), m_shiftScale(1) {
79 derived().compute(matrix);
82 ~SimplicialCholeskyBase() {}
84 Derived& derived() {
return *
static_cast<Derived*
>(
this); }
85 const Derived& derived()
const {
return *
static_cast<const Derived*
>(
this); }
87 inline Index cols()
const {
return m_matrix.
cols(); }
88 inline Index rows()
const {
return m_matrix.
rows(); }
96 eigen_assert(m_isInitialized &&
"Decomposition is not initialized.");
118 Derived&
setShift(
const DiagonalScalar& offset,
const DiagonalScalar& scale = 1) {
119 m_shiftOffset = offset;
120 m_shiftScale = scale;
124#ifndef EIGEN_PARSED_BY_DOXYGEN
126 template <
typename Stream>
127 void dumpMemory(Stream& s) {
130 << ((total += (m_matrix.
cols() + 1) *
sizeof(
int) + m_matrix.
nonZeros() * (
sizeof(int) +
sizeof(Scalar))) >> 20)
133 s <<
" diag: " << ((total += m_diag.size() *
sizeof(Scalar)) >> 20) <<
"Mb"
135 s <<
" tree: " << ((total += m_parent.size() *
sizeof(int)) >> 20) <<
"Mb"
137 s <<
" nonzeros: " << ((total += m_nonZerosPerCol.size() *
sizeof(int)) >> 20) <<
"Mb"
139 s <<
" perm: " << ((total += m_P.
size() *
sizeof(int)) >> 20) <<
"Mb"
141 s <<
" perm^-1: " << ((total += m_Pinv.
size() *
sizeof(int)) >> 20) <<
"Mb"
143 s <<
" TOTAL: " << (total >> 20) <<
"Mb"
148 template <
typename Rhs,
typename Dest>
149 void _solve_impl(
const MatrixBase<Rhs>& b, MatrixBase<Dest>& dest)
const {
150 eigen_assert(m_factorizationIsOk &&
151 "The decomposition is not in a valid state for solving, you must first call either compute() or "
152 "symbolic()/numeric()");
153 eigen_assert(m_matrix.
rows() == b.rows());
163 derived().matrixL().solveInPlace(dest);
165 if (m_diag.size() > 0) dest = m_diag.asDiagonal().inverse() * dest;
168 derived().matrixU().solveInPlace(dest);
170 if (m_P.
size() > 0) dest = m_Pinv * dest;
173 template <
typename Rhs,
typename Dest>
174 void _solve_impl(
const SparseMatrixBase<Rhs>& b, SparseMatrixBase<Dest>& dest)
const {
175 internal::solve_sparse_through_dense_panels(derived(), b, dest);
182 template <
bool DoLDLT,
bool NonHermitian>
184 eigen_assert(matrix.rows() == matrix.cols());
185 Index size = matrix.cols();
187 ConstCholMatrixPtr pmat;
188 ordering<NonHermitian>(matrix, pmat, tmp);
189 analyzePattern_preordered(*pmat, DoLDLT);
190 factorize_preordered<DoLDLT, NonHermitian>(*pmat);
193 template <
bool DoLDLT,
bool NonHermitian>
194 void factorize(
const MatrixType& a) {
195 eigen_assert(a.rows() == a.cols());
196 Index size = a.cols();
197 CholMatrixType tmp(size, size);
198 ConstCholMatrixPtr pmat;
202 internal::simplicial_cholesky_grab_input<CholMatrixType, MatrixType>::run(a, pmat, tmp);
204 internal::permute_symm_to_symm<UpLo, Upper, NonHermitian>(a, tmp, m_P.
indices().data());
208 factorize_preordered<DoLDLT, NonHermitian>(*pmat);
211 template <
bool DoLDLT,
bool NonHermitian>
212 void factorize_preordered(
const CholMatrixType& a);
214 template <
bool DoLDLT,
bool NonHermitian>
215 void analyzePattern(
const MatrixType& a) {
216 eigen_assert(a.rows() == a.cols());
217 Index size = a.cols();
218 CholMatrixType tmp(size, size);
219 ConstCholMatrixPtr pmat;
220 ordering<NonHermitian>(a, pmat, tmp);
221 analyzePattern_preordered(*pmat, DoLDLT);
223 void analyzePattern_preordered(
const CholMatrixType& a,
bool doLDLT);
225 template <
bool NonHermitian>
226 void ordering(
const MatrixType& a, ConstCholMatrixPtr& pmat, CholMatrixType& ap);
228 inline DiagonalScalar getDiag(Scalar x) {
return internal::traits<Derived>::getDiag(x); }
229 inline Scalar getSymm(Scalar x) {
return internal::traits<Derived>::getSymm(x); }
233 inline bool operator()(
const Index& row,
const Index& col,
const Scalar&)
const {
return row != col; }
237 bool m_factorizationIsOk;
247 DiagonalScalar m_shiftOffset;
248 DiagonalScalar m_shiftScale;
251template <
typename MatrixType_,
int UpLo_ =
Lower,
254template <
typename MatrixType_,
int UpLo_ =
Lower,
257template <
typename MatrixType_,
int UpLo_ =
Lower,
260template <
typename MatrixType_,
int UpLo_ =
Lower,
263template <
typename MatrixType_,
int UpLo_ =
Lower,
269template <
typename MatrixType_,
int UpLo_,
typename Ordering_>
270struct traits<
SimplicialLLT<MatrixType_, UpLo_, Ordering_> > {
271 typedef MatrixType_ MatrixType;
272 typedef Ordering_ OrderingType;
273 enum { UpLo = UpLo_ };
274 typedef typename MatrixType::Scalar Scalar;
275 typedef typename MatrixType::RealScalar DiagonalScalar;
276 typedef typename MatrixType::StorageIndex StorageIndex;
277 typedef SparseMatrix<Scalar, ColMajor, StorageIndex> CholMatrixType;
278 typedef TriangularView<const CholMatrixType, Eigen::Lower> MatrixL;
279 typedef TriangularView<const typename CholMatrixType::AdjointReturnType, Eigen::Upper> MatrixU;
280 static inline MatrixL getL(
const CholMatrixType& m) {
return MatrixL(m); }
281 static inline MatrixU getU(
const CholMatrixType& m) {
return MatrixU(m.adjoint()); }
282 static inline DiagonalScalar getDiag(Scalar x) {
return numext::real(x); }
283 static inline Scalar getSymm(Scalar x) {
return numext::conj(x); }
286template <
typename MatrixType_,
int UpLo_,
typename Ordering_>
287struct traits<SimplicialLDLT<MatrixType_, UpLo_, Ordering_> > {
288 typedef MatrixType_ MatrixType;
289 typedef Ordering_ OrderingType;
290 enum { UpLo = UpLo_ };
291 typedef typename MatrixType::Scalar Scalar;
292 typedef typename MatrixType::RealScalar DiagonalScalar;
293 typedef typename MatrixType::StorageIndex StorageIndex;
294 typedef SparseMatrix<Scalar, ColMajor, StorageIndex> CholMatrixType;
295 typedef TriangularView<const CholMatrixType, Eigen::UnitLower> MatrixL;
296 typedef TriangularView<const typename CholMatrixType::AdjointReturnType, Eigen::UnitUpper> MatrixU;
297 static inline MatrixL getL(
const CholMatrixType& m) {
return MatrixL(m); }
298 static inline MatrixU getU(
const CholMatrixType& m) {
return MatrixU(m.adjoint()); }
299 static inline DiagonalScalar getDiag(Scalar x) {
return numext::real(x); }
300 static inline Scalar getSymm(Scalar x) {
return numext::conj(x); }
303template <
typename MatrixType_,
int UpLo_,
typename Ordering_>
304struct traits<SimplicialNonHermitianLLT<MatrixType_, UpLo_, Ordering_> > {
305 typedef MatrixType_ MatrixType;
306 typedef Ordering_ OrderingType;
307 enum { UpLo = UpLo_ };
308 typedef typename MatrixType::Scalar Scalar;
309 typedef typename MatrixType::Scalar DiagonalScalar;
310 typedef typename MatrixType::StorageIndex StorageIndex;
311 typedef SparseMatrix<Scalar, ColMajor, StorageIndex> CholMatrixType;
312 typedef TriangularView<const CholMatrixType, Eigen::Lower> MatrixL;
313 typedef TriangularView<const typename CholMatrixType::ConstTransposeReturnType, Eigen::Upper> MatrixU;
314 static inline MatrixL getL(
const CholMatrixType& m) {
return MatrixL(m); }
315 static inline MatrixU getU(
const CholMatrixType& m) {
return MatrixU(m.transpose()); }
316 static inline DiagonalScalar getDiag(Scalar x) {
return x; }
317 static inline Scalar getSymm(Scalar x) {
return x; }
320template <
typename MatrixType_,
int UpLo_,
typename Ordering_>
321struct traits<SimplicialNonHermitianLDLT<MatrixType_, UpLo_, Ordering_> > {
322 typedef MatrixType_ MatrixType;
323 typedef Ordering_ OrderingType;
324 enum { UpLo = UpLo_ };
325 typedef typename MatrixType::Scalar Scalar;
326 typedef typename MatrixType::Scalar DiagonalScalar;
327 typedef typename MatrixType::StorageIndex StorageIndex;
328 typedef SparseMatrix<Scalar, ColMajor, StorageIndex> CholMatrixType;
329 typedef TriangularView<const CholMatrixType, Eigen::UnitLower> MatrixL;
330 typedef TriangularView<const typename CholMatrixType::ConstTransposeReturnType, Eigen::UnitUpper> MatrixU;
331 static inline MatrixL getL(
const CholMatrixType& m) {
return MatrixL(m); }
332 static inline MatrixU getU(
const CholMatrixType& m) {
return MatrixU(m.transpose()); }
333 static inline DiagonalScalar getDiag(Scalar x) {
return x; }
334 static inline Scalar getSymm(Scalar x) {
return x; }
337template <
typename MatrixType_,
int UpLo_,
typename Ordering_>
338struct traits<SimplicialCholesky<MatrixType_, UpLo_, Ordering_> > {
339 typedef MatrixType_ MatrixType;
340 typedef Ordering_ OrderingType;
341 enum { UpLo = UpLo_ };
342 typedef typename MatrixType::Scalar Scalar;
343 typedef typename MatrixType::RealScalar DiagonalScalar;
344 static inline DiagonalScalar getDiag(Scalar x) {
return numext::real(x); }
345 static inline Scalar getSymm(Scalar x) {
return numext::conj(x); }
370template <
typename MatrixType_,
int UpLo_,
typename Ordering_>
373 typedef MatrixType_ MatrixType;
374 enum { UpLo = UpLo_ };
376 typedef typename MatrixType::Scalar Scalar;
377 typedef typename MatrixType::RealScalar RealScalar;
378 typedef typename MatrixType::StorageIndex StorageIndex;
381 typedef internal::traits<SimplicialLLT> Traits;
382 typedef typename Traits::MatrixL MatrixL;
383 typedef typename Traits::MatrixU MatrixU;
393 eigen_assert(Base::m_factorizationIsOk &&
"Simplicial LLT not factorized");
394 return Traits::getL(Base::m_matrix);
399 eigen_assert(Base::m_factorizationIsOk &&
"Simplicial LLT not factorized");
400 return Traits::getU(Base::m_matrix);
427 Scalar detL = Base::m_matrix.diagonal().prod();
428 return numext::abs2(detL);
452template <
typename MatrixType_,
int UpLo_,
typename Ordering_>
455 typedef MatrixType_ MatrixType;
456 enum { UpLo = UpLo_ };
458 typedef typename MatrixType::Scalar Scalar;
459 typedef typename MatrixType::RealScalar RealScalar;
460 typedef typename MatrixType::StorageIndex StorageIndex;
463 typedef internal::traits<SimplicialLDLT> Traits;
464 typedef typename Traits::MatrixL MatrixL;
465 typedef typename Traits::MatrixU MatrixU;
476 eigen_assert(Base::m_factorizationIsOk &&
"Simplicial LDLT not factorized");
481 eigen_assert(Base::m_factorizationIsOk &&
"Simplicial LDLT not factorized");
482 return Traits::getL(Base::m_matrix);
487 eigen_assert(Base::m_factorizationIsOk &&
"Simplicial LDLT not factorized");
488 return Traits::getU(Base::m_matrix);
537template <
typename MatrixType_,
int UpLo_,
typename Ordering_>
541 typedef MatrixType_ MatrixType;
542 enum { UpLo = UpLo_ };
544 typedef typename MatrixType::Scalar Scalar;
545 typedef typename MatrixType::RealScalar RealScalar;
546 typedef typename MatrixType::StorageIndex StorageIndex;
549 typedef internal::traits<SimplicialNonHermitianLLT> Traits;
550 typedef typename Traits::MatrixL MatrixL;
551 typedef typename Traits::MatrixU MatrixU;
562 eigen_assert(Base::m_factorizationIsOk &&
"Simplicial LLT not factorized");
563 return Traits::getL(Base::m_matrix);
568 eigen_assert(Base::m_factorizationIsOk &&
"Simplicial LLT not factorized");
569 return Traits::getU(Base::m_matrix);
596 Scalar detL = Base::m_matrix.diagonal().prod();
621template <
typename MatrixType_,
int UpLo_,
typename Ordering_>
625 typedef MatrixType_ MatrixType;
626 enum { UpLo = UpLo_ };
628 typedef typename MatrixType::Scalar Scalar;
629 typedef typename MatrixType::RealScalar RealScalar;
630 typedef typename MatrixType::StorageIndex StorageIndex;
633 typedef internal::traits<SimplicialNonHermitianLDLT> Traits;
634 typedef typename Traits::MatrixL MatrixL;
635 typedef typename Traits::MatrixU MatrixU;
646 eigen_assert(Base::m_factorizationIsOk &&
"Simplicial LDLT not factorized");
651 eigen_assert(Base::m_factorizationIsOk &&
"Simplicial LDLT not factorized");
652 return Traits::getL(Base::m_matrix);
657 eigen_assert(Base::m_factorizationIsOk &&
"Simplicial LDLT not factorized");
658 return Traits::getU(Base::m_matrix);
693template <
typename MatrixType_,
int UpLo_,
typename Ordering_>
696 typedef MatrixType_ MatrixType;
697 enum { UpLo = UpLo_ };
699 typedef typename MatrixType::Scalar Scalar;
700 typedef typename MatrixType::RealScalar RealScalar;
701 typedef typename MatrixType::StorageIndex StorageIndex;
704 typedef internal::traits<SimplicialLDLT<MatrixType, UpLo> > LDLTTraits;
705 typedef internal::traits<SimplicialLLT<MatrixType, UpLo> > LLTTraits;
714 case SimplicialCholeskyLLT:
717 case SimplicialCholeskyLDLT:
728 eigen_assert(Base::m_factorizationIsOk &&
"Simplicial Cholesky not factorized");
732 eigen_assert(Base::m_factorizationIsOk &&
"Simplicial Cholesky not factorized");
733 return Base::m_matrix;
772 template <
typename Rhs,
typename Dest>
774 eigen_assert(Base::m_factorizationIsOk &&
775 "The decomposition is not in a valid state for solving, you must first call either compute() or "
776 "symbolic()/numeric()");
777 eigen_assert(Base::m_matrix.rows() == b.
rows());
779 if (Base::m_info !=
Success)
return;
781 if (Base::m_P.size() > 0)
782 dest = Base::m_P * b;
786 if (Base::m_matrix.nonZeros() > 0)
789 LDLTTraits::getL(Base::m_matrix).solveInPlace(dest);
791 LLTTraits::getL(Base::m_matrix).solveInPlace(dest);
794 if (Base::m_diag.size() > 0) dest = Base::m_diag.real().
asDiagonal().inverse() * dest;
796 if (Base::m_matrix.nonZeros() > 0)
799 LDLTTraits::getU(Base::m_matrix).solveInPlace(dest);
801 LLTTraits::getU(Base::m_matrix).solveInPlace(dest);
804 if (Base::m_P.size() > 0) dest = Base::m_Pinv * dest;
808 template <
typename Rhs,
typename Dest>
809 void _solve_impl(
const SparseMatrixBase<Rhs>& b, SparseMatrixBase<Dest>& dest)
const {
810 internal::solve_sparse_through_dense_panels(*
this, b, dest);
813 Scalar determinant()
const {
815 return Base::m_diag.prod();
817 Scalar detL = Diagonal<const CholMatrixType>(Base::m_matrix).prod();
818 return numext::abs2(detL);
826template <
typename Derived>
827template <
bool NonHermitian>
828void SimplicialCholeskyBase<Derived>::ordering(
const MatrixType& a, ConstCholMatrixPtr& pmat, CholMatrixType& ap) {
829 eigen_assert(a.rows() == a.cols());
830 const Index size = a.rows();
833 if (!internal::is_same<OrderingType, NaturalOrdering<Index> >::value) {
836 internal::permute_symm_to_fullsymm<UpLo, NonHermitian>(a, C, NULL);
838 OrderingType ordering;
842 if (m_Pinv.size() > 0)
843 m_P = m_Pinv.inverse();
847 ap.resize(size, size);
848 internal::permute_symm_to_symm<UpLo, Upper, NonHermitian>(a, ap, m_P.indices().data());
852 if (
int(UpLo) ==
int(Lower) || MatrixType::IsRowMajor) {
854 ap.resize(size, size);
855 internal::permute_symm_to_symm<UpLo, Upper, NonHermitian>(a, ap, NULL);
857 internal::simplicial_cholesky_grab_input<CholMatrixType, MatrixType>::run(a, pmat, ap);
EIGEN_CONSTEXPR Index rows() const EIGEN_NOEXCEPT
Definition EigenBase.h:59
Base class for all dense matrices, vectors, and expressions.
Definition MatrixBase.h:52
const DiagonalWrapper< const Derived > asDiagonal() const
Definition DiagonalMatrix.h:344
The matrix class, also used for vectors and row-vectors.
Definition Matrix.h:186
Index size() const
Definition PermutationMatrix.h:96
Permutation matrix.
Definition PermutationMatrix.h:280
const IndicesType & indices() const
Definition PermutationMatrix.h:334
A base class for direct sparse Cholesky factorizations.
Definition SimplicialCholesky.h:51
SimplicialCholeskyBase()
Definition SimplicialCholesky.h:74
void compute(const MatrixType &matrix)
Definition SimplicialCholesky.h:183
ComputationInfo info() const
Reports whether previous computation was successful.
Definition SimplicialCholesky.h:95
const PermutationMatrix< Dynamic, Dynamic, StorageIndex > & permutationP() const
Definition SimplicialCholesky.h:102
Derived & setShift(const DiagonalScalar &offset, const DiagonalScalar &scale=1)
Definition SimplicialCholesky.h:118
const PermutationMatrix< Dynamic, Dynamic, StorageIndex > & permutationPinv() const
Definition SimplicialCholesky.h:106
Definition SimplicialCholesky.h:694
void factorize(const MatrixType &a)
Definition SimplicialCholesky.h:764
void analyzePattern(const MatrixType &a)
Definition SimplicialCholesky.h:751
SimplicialCholesky & compute(const MatrixType &matrix)
Definition SimplicialCholesky.h:737
A direct sparse LDLT Cholesky factorizations without square root.
Definition SimplicialCholesky.h:453
Scalar determinant() const
Definition SimplicialCholesky.h:514
const MatrixL matrixL() const
Definition SimplicialCholesky.h:480
SimplicialLDLT(const MatrixType &matrix)
Definition SimplicialCholesky.h:472
const MatrixU matrixU() const
Definition SimplicialCholesky.h:486
void analyzePattern(const MatrixType &a)
Definition SimplicialCholesky.h:503
void factorize(const MatrixType &a)
Definition SimplicialCholesky.h:511
const VectorType vectorD() const
Definition SimplicialCholesky.h:475
SimplicialLDLT & compute(const MatrixType &matrix)
Definition SimplicialCholesky.h:492
SimplicialLDLT()
Definition SimplicialCholesky.h:469
A direct sparse LLT Cholesky factorizations.
Definition SimplicialCholesky.h:371
const MatrixL matrixL() const
Definition SimplicialCholesky.h:392
SimplicialLLT & compute(const MatrixType &matrix)
Definition SimplicialCholesky.h:404
Scalar determinant() const
Definition SimplicialCholesky.h:426
const MatrixU matrixU() const
Definition SimplicialCholesky.h:398
SimplicialLLT()
Definition SimplicialCholesky.h:387
void analyzePattern(const MatrixType &a)
Definition SimplicialCholesky.h:415
void factorize(const MatrixType &a)
Definition SimplicialCholesky.h:423
SimplicialLLT(const MatrixType &matrix)
Definition SimplicialCholesky.h:389
A direct sparse LDLT Cholesky factorizations without square root, for symmetric non-hermitian matrice...
Definition SimplicialCholesky.h:623
SimplicialNonHermitianLDLT()
Definition SimplicialCholesky.h:639
Scalar determinant() const
Definition SimplicialCholesky.h:684
SimplicialNonHermitianLDLT & compute(const MatrixType &matrix)
Definition SimplicialCholesky.h:662
const MatrixU matrixU() const
Definition SimplicialCholesky.h:656
const MatrixL matrixL() const
Definition SimplicialCholesky.h:650
const VectorType vectorD() const
Definition SimplicialCholesky.h:645
SimplicialNonHermitianLDLT(const MatrixType &matrix)
Definition SimplicialCholesky.h:642
void factorize(const MatrixType &a)
Definition SimplicialCholesky.h:681
void analyzePattern(const MatrixType &a)
Definition SimplicialCholesky.h:673
A direct sparse LLT Cholesky factorizations, for symmetric non-hermitian matrices.
Definition SimplicialCholesky.h:539
void factorize(const MatrixType &a)
Definition SimplicialCholesky.h:592
const MatrixU matrixU() const
Definition SimplicialCholesky.h:567
SimplicialNonHermitianLLT()
Definition SimplicialCholesky.h:555
SimplicialNonHermitianLLT & compute(const MatrixType &matrix)
Definition SimplicialCholesky.h:573
Scalar determinant() const
Definition SimplicialCholesky.h:595
const MatrixL matrixL() const
Definition SimplicialCholesky.h:561
void analyzePattern(const MatrixType &a)
Definition SimplicialCholesky.h:584
SimplicialNonHermitianLLT(const MatrixType &matrix)
Definition SimplicialCholesky.h:558
A versatible sparse matrix representation.
Definition SparseUtil.h:47
Index nonZeros() const
Definition SparseCompressedBase.h:64
Index cols() const
Definition SparseMatrix.h:161
Index rows() const
Definition SparseMatrix.h:159
A base class for sparse solvers.
Definition SparseSolverBase.h:67
ComputationInfo
Definition Constants.h:438
@ Lower
Definition Constants.h:211
@ Upper
Definition Constants.h:213
@ Success
Definition Constants.h:440
Namespace containing all symbols from the Eigen library.
Definition Core:137
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition Meta.h:83
Definition SimplicialCholesky.h:232