10 #ifndef EIGEN_SIMPLICIAL_CHOLESKY_H 11 #define EIGEN_SIMPLICIAL_CHOLESKY_H 15 enum SimplicialCholeskyMode {
16 SimplicialCholeskyLLT,
17 SimplicialCholeskyLDLT
35 template<
typename Derived>
43 typedef typename MatrixType::RealScalar RealScalar;
44 typedef typename MatrixType::Index Index;
52 : m_info(
Success), m_isInitialized(false), m_shiftOffset(0), m_shiftScale(1)
56 : m_info(
Success), m_isInitialized(
false), m_shiftOffset(0), m_shiftScale(1)
58 derived().compute(matrix);
65 Derived& derived() {
return *
static_cast<Derived*
>(
this); }
66 const Derived& derived()
const {
return *
static_cast<const Derived*
>(
this); }
68 inline Index cols()
const {
return m_matrix.
cols(); }
69 inline Index rows()
const {
return m_matrix.
rows(); }
78 eigen_assert(m_isInitialized &&
"Decomposition is not initialized.");
86 template<
typename Rhs>
90 eigen_assert(m_isInitialized &&
"Simplicial LLT or LDLT is not initialized.");
91 eigen_assert(rows()==b.rows()
92 &&
"SimplicialCholeskyBase::solve(): invalid number of rows of the right hand side matrix b");
100 template<
typename Rhs>
104 eigen_assert(m_isInitialized &&
"Simplicial LLT or LDLT is not initialized.");
105 eigen_assert(rows()==b.
rows()
106 &&
"SimplicialCholesky::solve(): invalid number of rows of the right hand side matrix b");
129 Derived&
setShift(
const RealScalar& offset,
const RealScalar& scale = 1)
131 m_shiftOffset = offset;
132 m_shiftScale = scale;
136 #ifndef EIGEN_PARSED_BY_DOXYGEN 138 template<
typename Stream>
139 void dumpMemory(Stream& s)
142 s <<
" L: " << ((total+=(m_matrix.
cols()+1) *
sizeof(
int) + m_matrix.
nonZeros()*(
sizeof(int)+
sizeof(Scalar))) >> 20) <<
"Mb" <<
"\n";
143 s <<
" diag: " << ((total+=m_diag.size() *
sizeof(Scalar)) >> 20) <<
"Mb" <<
"\n";
144 s <<
" tree: " << ((total+=m_parent.size() *
sizeof(int)) >> 20) <<
"Mb" <<
"\n";
145 s <<
" nonzeros: " << ((total+=m_nonZerosPerCol.size() *
sizeof(int)) >> 20) <<
"Mb" <<
"\n";
146 s <<
" perm: " << ((total+=m_P.
size() *
sizeof(int)) >> 20) <<
"Mb" <<
"\n";
147 s <<
" perm^-1: " << ((total+=m_Pinv.
size() *
sizeof(int)) >> 20) <<
"Mb" <<
"\n";
148 s <<
" TOTAL: " << (total>> 20) <<
"Mb" <<
"\n";
152 template<
typename Rhs,
typename Dest>
155 eigen_assert(m_factorizationIsOk &&
"The decomposition is not in a valid state for solving, you must first call either compute() or symbolic()/numeric()");
156 eigen_assert(m_matrix.
rows()==b.rows());
167 derived().matrixL().solveInPlace(dest);
170 dest = m_diag.asDiagonal().inverse() * dest;
173 derived().matrixU().solveInPlace(dest);
176 dest = m_Pinv * dest;
179 #endif // EIGEN_PARSED_BY_DOXYGEN 184 template<
bool DoLDLT>
187 eigen_assert(matrix.rows()==matrix.cols());
188 Index
size = matrix.cols();
189 CholMatrixType ap(size,size);
190 ordering(matrix, ap);
191 analyzePattern_preordered(ap, DoLDLT);
192 factorize_preordered<DoLDLT>(ap);
195 template<
bool DoLDLT>
196 void factorize(
const MatrixType& a)
198 eigen_assert(a.rows()==a.cols());
200 CholMatrixType ap(size,size);
201 ap.template selfadjointView<Upper>() = a.template selfadjointView<UpLo>().
twistedBy(m_P);
202 factorize_preordered<DoLDLT>(ap);
205 template<
bool DoLDLT>
206 void factorize_preordered(
const CholMatrixType& a);
208 void analyzePattern(
const MatrixType& a,
bool doLDLT)
210 eigen_assert(a.rows()==a.cols());
212 CholMatrixType ap(size,size);
214 analyzePattern_preordered(ap,doLDLT);
216 void analyzePattern_preordered(
const CholMatrixType& a,
bool doLDLT);
218 void ordering(
const MatrixType& a, CholMatrixType& ap);
222 inline bool operator() (
const Index& row,
const Index& col,
const Scalar&)
const 229 bool m_isInitialized;
230 bool m_factorizationIsOk;
233 CholMatrixType m_matrix;
236 VectorXi m_nonZerosPerCol;
240 RealScalar m_shiftOffset;
241 RealScalar m_shiftScale;
244 template<
typename _MatrixType,
int _UpLo = Lower,
typename _Ordering = AMDOrdering<
typename _MatrixType::Index> >
class SimplicialLLT;
245 template<
typename _MatrixType,
int _UpLo = Lower,
typename _Ordering = AMDOrdering<
typename _MatrixType::Index> >
class SimplicialLDLT;
246 template<
typename _MatrixType,
int _UpLo = Lower,
typename _Ordering = AMDOrdering<
typename _MatrixType::Index> >
class SimplicialCholesky;
250 template<
typename _MatrixType,
int _UpLo,
typename _Ordering>
struct traits<
SimplicialLLT<_MatrixType,_UpLo,_Ordering> >
252 typedef _MatrixType MatrixType;
253 typedef _Ordering OrderingType;
254 enum { UpLo = _UpLo };
256 typedef typename MatrixType::Index Index;
260 static inline MatrixL getL(
const MatrixType& m) {
return m; }
261 static inline MatrixU getU(
const MatrixType& m) {
return m.adjoint(); }
264 template<
typename _MatrixType,
int _UpLo,
typename _Ordering>
struct traits<
SimplicialLDLT<_MatrixType,_UpLo,_Ordering> >
266 typedef _MatrixType MatrixType;
267 typedef _Ordering OrderingType;
268 enum { UpLo = _UpLo };
270 typedef typename MatrixType::Index Index;
274 static inline MatrixL getL(
const MatrixType& m) {
return m; }
275 static inline MatrixU getU(
const MatrixType& m) {
return m.adjoint(); }
280 typedef _MatrixType MatrixType;
281 typedef _Ordering OrderingType;
282 enum { UpLo = _UpLo };
305 template<
typename _MatrixType,
int _UpLo,
typename _Ordering>
309 typedef _MatrixType MatrixType;
310 enum { UpLo = _UpLo };
313 typedef typename MatrixType::RealScalar RealScalar;
314 typedef typename MatrixType::Index Index;
318 typedef typename Traits::MatrixL MatrixL;
319 typedef typename Traits::MatrixU MatrixU;
329 eigen_assert(Base::m_factorizationIsOk &&
"Simplicial LLT not factorized");
330 return Traits::getL(Base::m_matrix);
335 eigen_assert(Base::m_factorizationIsOk &&
"Simplicial LLT not factorized");
336 return Traits::getU(Base::m_matrix);
342 Base::template compute<false>(matrix);
354 Base::analyzePattern(a,
false);
365 Base::template factorize<false>(a);
371 Scalar detL = Base::m_matrix.diagonal().prod();
372 return numext::abs2(detL);
394 template<
typename _MatrixType,
int _UpLo,
typename _Ordering>
398 typedef _MatrixType MatrixType;
399 enum { UpLo = _UpLo };
402 typedef typename MatrixType::RealScalar RealScalar;
403 typedef typename MatrixType::Index Index;
407 typedef typename Traits::MatrixL MatrixL;
408 typedef typename Traits::MatrixU MatrixU;
419 eigen_assert(Base::m_factorizationIsOk &&
"Simplicial LDLT not factorized");
424 eigen_assert(Base::m_factorizationIsOk &&
"Simplicial LDLT not factorized");
425 return Traits::getL(Base::m_matrix);
430 eigen_assert(Base::m_factorizationIsOk &&
"Simplicial LDLT not factorized");
431 return Traits::getU(Base::m_matrix);
437 Base::template compute<true>(matrix);
449 Base::analyzePattern(a,
true);
460 Base::template factorize<true>(a);
466 return Base::m_diag.prod();
476 template<
typename _MatrixType,
int _UpLo,
typename _Ordering>
480 typedef _MatrixType MatrixType;
481 enum { UpLo = _UpLo };
484 typedef typename MatrixType::RealScalar RealScalar;
485 typedef typename MatrixType::Index Index;
495 : Base(), m_LDLT(
true)
504 case SimplicialCholeskyLLT:
507 case SimplicialCholeskyLDLT:
517 inline const VectorType vectorD()
const {
518 eigen_assert(Base::m_factorizationIsOk &&
"Simplicial Cholesky not factorized");
521 inline const CholMatrixType rawMatrix()
const {
522 eigen_assert(Base::m_factorizationIsOk &&
"Simplicial Cholesky not factorized");
523 return Base::m_matrix;
530 Base::template compute<true>(matrix);
532 Base::template compute<false>(matrix);
544 Base::analyzePattern(a, m_LDLT);
556 Base::template factorize<true>(a);
558 Base::template factorize<false>(a);
562 template<
typename Rhs,
typename Dest>
565 eigen_assert(Base::m_factorizationIsOk &&
"The decomposition is not in a valid state for solving, you must first call either compute() or symbolic()/numeric()");
566 eigen_assert(Base::m_matrix.rows()==b.rows());
571 if(Base::m_P.
size()>0)
572 dest = Base::m_P * b;
576 if(Base::m_matrix.nonZeros()>0)
579 LDLTTraits::getL(Base::m_matrix).solveInPlace(dest);
581 LLTTraits::getL(Base::m_matrix).solveInPlace(dest);
584 if(Base::m_diag.
size()>0)
585 dest = Base::m_diag.
asDiagonal().inverse() * dest;
587 if (Base::m_matrix.nonZeros()>0)
590 LDLTTraits::getU(Base::m_matrix).solveInPlace(dest);
592 LLTTraits::getU(Base::m_matrix).solveInPlace(dest);
595 if(Base::m_P.
size()>0)
596 dest = Base::m_Pinv * dest;
599 Scalar determinant()
const 603 return Base::m_diag.prod();
608 return numext::abs2(detL);
616 template<
typename Derived>
619 eigen_assert(a.rows()==a.cols());
620 const Index
size = a.rows();
624 C = a.template selfadjointView<UpLo>();
626 OrderingType ordering;
631 m_P = m_Pinv.inverse();
636 ap.template selfadjointView<Upper>() = a.template selfadjointView<UpLo>().
twistedBy(m_P);
641 template<
typename Derived,
typename Rhs>
646 EIGEN_MAKE_SOLVE_HELPERS(Dec,Rhs)
648 template<
typename Dest>
void evalTo(Dest& dst)
const 650 dec().derived()._solve(rhs(),dst);
654 template<
typename Derived,
typename Rhs>
659 EIGEN_MAKE_SPARSE_SOLVE_HELPERS(Dec,Rhs)
661 template<
typename Dest>
void evalTo(Dest& dst)
const 663 this->defaultEvalTo(dst);
671 #endif // EIGEN_SIMPLICIAL_CHOLESKY_H SimplicialLDLT()
Default constructor.
Definition: SimplicialCholesky.h:411
Definition: gtest_unittest.cc:5031
Scalar determinant() const
Definition: SimplicialCholesky.h:464
const MatrixU matrixU() const
Definition: SimplicialCholesky.h:334
Definition: ForwardDeclarations.h:124
SimplicialCholeskyBase()
Default constructor.
Definition: SimplicialCholesky.h:51
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:129
SimplicialLLT & compute(const MatrixType &matrix)
Computes the sparse Cholesky decomposition of matrix.
Definition: SimplicialCholesky.h:340
const MatrixU matrixU() const
Definition: SimplicialCholesky.h:429
const PermutationMatrix< Dynamic, Dynamic, Index > & permutationPinv() const
Definition: SimplicialCholesky.h:117
iterative scaling algorithm to equilibrate rows and column norms in matrices
Definition: TestIMU_Common.h:87
Definition: SparseSolve.h:18
Index cols() const
Definition: SparseMatrix.h:121
A direct sparse LDLT Cholesky factorizations without square root.
Definition: SimplicialCholesky.h:245
SimplicialLLT()
Default constructor.
Definition: SimplicialCholesky.h:322
SimplicialLDLT & compute(const MatrixType &matrix)
Computes the sparse Cholesky decomposition of matrix.
Definition: SimplicialCholesky.h:435
Scalar determinant() const
Definition: SimplicialCholesky.h:369
Definition: SparseTriangularView.h:25
const MatrixL matrixL() const
Definition: SimplicialCholesky.h:423
void factorize(const MatrixType &a)
Performs a numeric decomposition of matrix.
Definition: SimplicialCholesky.h:363
Index size() const
Definition: PermutationMatrix.h:114
void analyzePattern(const MatrixType &a)
Performs a symbolic decomposition on the sparcity of matrix.
Definition: SimplicialCholesky.h:542
detail::size< coerce_list< Ts... >> size
Get the size of a list (number of elements.)
Definition: Size.h:56
Index nonZeros() const
Definition: SparseMatrix.h:246
Index rows() const
Definition: SparseMatrixBase.h:159
void compute(const MatrixType &matrix)
Computes the sparse Cholesky decomposition of matrix.
Definition: SimplicialCholesky.h:185
Base class of any sparse matrices or sparse expressions.
Definition: ForwardDeclarations.h:239
Definition: SimplicialCholesky.h:246
A direct sparse Cholesky factorizations.
Definition: SimplicialCholesky.h:36
Index rows() const
Definition: SparseMatrix.h:119
SimplicialLLT(const MatrixType &matrix)
Constructs and performs the LLT factorization of matrix.
Definition: SimplicialCholesky.h:324
void resize(Index rows, Index cols)
Resizes the matrix to a rows x cols matrix and initializes it to zero.
Definition: SparseMatrix.h:596
const DiagonalWrapper< const Derived > asDiagonal() const
Definition: DiagonalMatrix.h:278
void factorize(const MatrixType &a)
Performs a numeric decomposition of matrix.
Definition: SimplicialCholesky.h:553
const internal::sparse_solve_retval< SimplicialCholeskyBase, Rhs > solve(const SparseMatrixBase< Rhs > &b) const
Definition: SimplicialCholesky.h:102
Computation was successful.
Definition: Constants.h:376
Definition: BandTriangularSolver.h:13
ComputationInfo info() const
Reports whether previous computation was successful.
Definition: SimplicialCholesky.h:76
const PermutationMatrix< Dynamic, Dynamic, Index > & permutationP() const
Definition: SimplicialCholesky.h:112
Definition: SparseSolve.h:17
SimplicialLDLT(const MatrixType &matrix)
Constructs and performs the LLT factorization of matrix.
Definition: SimplicialCholesky.h:414
void factorize(const MatrixType &a)
Performs a numeric decomposition of matrix.
Definition: SimplicialCholesky.h:458
keeps off-diagonal entries; drops diagonal entries
Definition: SimplicialCholesky.h:221
const VectorType vectorD() const
Definition: SimplicialCholesky.h:418
Definition: ForwardDeclarations.h:125
const internal::solve_retval< SimplicialCholeskyBase, Rhs > solve(const MatrixBase< Rhs > &b) const
Definition: SimplicialCholesky.h:88
void analyzePattern(const MatrixType &a)
Performs a symbolic decomposition on the sparcity of matrix.
Definition: SimplicialCholesky.h:352
Expression of a diagonal/subdiagonal/superdiagonal in a matrix.
Definition: Diagonal.h:64
A direct sparse LLT Cholesky factorizations.
Definition: SimplicialCholesky.h:244
ComputationInfo
Enum for reporting the status of a computation.
Definition: Constants.h:374
SparseSymmetricPermutationProduct< Derived, Upper|Lower > twistedBy(const PermutationMatrix< Dynamic, Dynamic, Index > &perm) const
Definition: SparseMatrixBase.h:372
Base class for all dense matrices, vectors, and expressions.
Definition: MatrixBase.h:48
const MatrixL matrixL() const
Definition: SimplicialCholesky.h:328
Definition: ForwardDeclarations.h:17
double Scalar
Common scalar type.
Definition: FlexibleKalmanBase.h:48
SimplicialCholesky & compute(const MatrixType &matrix)
Computes the sparse Cholesky decomposition of matrix.
Definition: SimplicialCholesky.h:527
void analyzePattern(const MatrixType &a)
Performs a symbolic decomposition on the sparcity of matrix.
Definition: SimplicialCholesky.h:447