10 #ifndef EIGEN_CHOLMODSUPPORT_H 11 #define EIGEN_CHOLMODSUPPORT_H 20 template<
typename CholmodType>
21 static void run(CholmodType& mat) {
22 mat.xtype = CHOLMOD_REAL;
23 mat.dtype = CHOLMOD_DOUBLE;
28 template<
typename CholmodType>
29 static void run(CholmodType& mat) {
30 mat.xtype = CHOLMOD_COMPLEX;
31 mat.dtype = CHOLMOD_DOUBLE;
57 template<
typename _Scalar,
int _Options,
typename _StorageIndex>
61 res.nzmax = mat.nonZeros();
62 res.nrow = mat.rows();
63 res.ncol = mat.cols();
64 res.p = mat.outerIndexPtr();
65 res.i = mat.innerIndexPtr();
66 res.x = mat.valuePtr();
69 if(mat.isCompressed())
77 res.nz = mat.innerNonZeroPtr();
85 res.itype = CHOLMOD_INT;
89 res.itype = CHOLMOD_LONG;
93 eigen_assert(
false &&
"Index type not supported yet");
104 template<
typename _Scalar,
int _Options,
typename _Index>
111 template<
typename _Scalar,
int _Options,
typename _Index>
120 template<
typename _Scalar,
int _Options,
typename _Index,
unsigned int UpLo>
125 if(UpLo==
Upper) res.stype = 1;
126 if(UpLo==
Lower) res.stype = -1;
133 template<
typename Derived>
137 typedef typename Derived::Scalar Scalar;
140 res.nrow = mat.rows();
141 res.ncol = mat.cols();
142 res.nzmax = res.nrow * res.ncol;
143 res.d = Derived::IsVectorAtCompileTime ? mat.derived().size() : mat.derived().outerStride();
144 res.x = (
void*)(mat.derived().data());
154 template<
typename Scalar,
int Flags,
typename StorageIndex>
158 (cm.nrow, cm.ncol,
static_cast<StorageIndex*
>(cm.p)[cm.ncol],
159 static_cast<StorageIndex*>(cm.p),
static_cast<StorageIndex*
>(cm.i),static_cast<Scalar*>(cm.x) );
163 CholmodAuto, CholmodSimplicialLLt, CholmodSupernodalLLt, CholmodLDLt
172 template<
typename _MatrixType,
int _UpLo,
typename Derived>
178 using Base::m_isInitialized;
180 typedef _MatrixType MatrixType;
181 enum { UpLo = _UpLo };
182 typedef typename MatrixType::Scalar Scalar;
183 typedef typename MatrixType::RealScalar RealScalar;
184 typedef MatrixType CholMatrixType;
185 typedef typename MatrixType::StorageIndex StorageIndex;
187 ColsAtCompileTime = MatrixType::ColsAtCompileTime,
188 MaxColsAtCompileTime = MatrixType::MaxColsAtCompileTime
194 : m_cholmodFactor(0), m_info(
Success), m_factorizationIsOk(
false), m_analysisIsOk(
false)
197 m_shiftOffset[0] = m_shiftOffset[1] = 0.0;
198 cholmod_start(&m_cholmod);
202 : m_cholmodFactor(0), m_info(
Success), m_factorizationIsOk(
false), m_analysisIsOk(
false)
205 m_shiftOffset[0] = m_shiftOffset[1] = 0.0;
206 cholmod_start(&m_cholmod);
213 cholmod_free_factor(&m_cholmodFactor, &m_cholmod);
214 cholmod_finish(&m_cholmod);
217 inline StorageIndex cols()
const {
return internal::convert_index<StorageIndex, Index>(m_cholmodFactor->n); }
218 inline StorageIndex rows()
const {
return internal::convert_index<StorageIndex, Index>(m_cholmodFactor->n); }
227 eigen_assert(m_isInitialized &&
"Decomposition is not initialized.");
234 analyzePattern(matrix);
249 cholmod_free_factor(&m_cholmodFactor, &m_cholmod);
252 cholmod_sparse
A =
viewAsCholmod(matrix.template selfadjointView<UpLo>());
253 m_cholmodFactor = cholmod_analyze(&A, &m_cholmod);
255 this->m_isInitialized =
true;
257 m_analysisIsOk =
true;
258 m_factorizationIsOk =
false;
269 eigen_assert(m_analysisIsOk &&
"You must first call analyzePattern()");
270 cholmod_sparse
A =
viewAsCholmod(matrix.template selfadjointView<UpLo>());
271 cholmod_factorize_p(&A, m_shiftOffset, 0, 0, m_cholmodFactor, &m_cholmod);
275 m_factorizationIsOk =
true;
280 cholmod_common&
cholmod() {
return m_cholmod; }
282 #ifndef EIGEN_PARSED_BY_DOXYGEN 284 template<
typename Rhs,
typename Dest>
287 eigen_assert(m_factorizationIsOk &&
"The decomposition is not in a valid state for solving, you must first call either compute() or symbolic()/numeric()");
288 const Index size = m_cholmodFactor->n;
289 EIGEN_UNUSED_VARIABLE(size);
290 eigen_assert(size==b.rows());
296 cholmod_dense* x_cd = cholmod_solve(CHOLMOD_A, m_cholmodFactor, &b_cd, &m_cholmod);
304 cholmod_free_dense(&x_cd, &m_cholmod);
308 template<
typename RhsDerived,
typename DestDerived>
311 eigen_assert(m_factorizationIsOk &&
"The decomposition is not in a valid state for solving, you must first call either compute() or symbolic()/numeric()");
312 const Index size = m_cholmodFactor->n;
313 EIGEN_UNUSED_VARIABLE(size);
314 eigen_assert(size==b.
rows());
319 cholmod_sparse* x_cs = cholmod_spsolve(CHOLMOD_A, m_cholmodFactor, &b_cs, &m_cholmod);
326 dest.derived() = viewAsEigen<typename DestDerived::Scalar,ColMajor,typename DestDerived::StorageIndex>(*x_cs);
327 cholmod_free_sparse(&x_cs, &m_cholmod);
329 #endif // EIGEN_PARSED_BY_DOXYGEN 343 m_shiftOffset[0] = double(offset);
351 return exp(logDeterminant());
359 eigen_assert(m_factorizationIsOk &&
"The decomposition is not in a valid state for solving, you must first call either compute() or symbolic()/numeric()");
361 RealScalar logDet = 0;
362 Scalar *x =
static_cast<Scalar*
>(m_cholmodFactor->x);
363 if (m_cholmodFactor->is_super)
369 StorageIndex *super =
static_cast<StorageIndex*
>(m_cholmodFactor->super);
371 StorageIndex *pi =
static_cast<StorageIndex*
>(m_cholmodFactor->pi);
373 StorageIndex *px =
static_cast<StorageIndex*
>(m_cholmodFactor->px);
375 Index nb_super_nodes = m_cholmodFactor->nsuper;
376 for (
Index k=0; k < nb_super_nodes; ++k)
378 StorageIndex ncols = super[k + 1] - super[k];
379 StorageIndex nrows = pi[k + 1] - pi[k];
382 logDet += sk.real().log().sum();
388 StorageIndex *p =
static_cast<StorageIndex*
>(m_cholmodFactor->p);
389 Index size = m_cholmodFactor->n;
390 for (
Index k=0; k<size; ++k)
391 logDet += log(real( x[p[k]] ));
393 if (m_cholmodFactor->is_ll)
398 template<
typename Stream>
399 void dumpMemory(Stream& )
403 mutable cholmod_common m_cholmod;
404 cholmod_factor* m_cholmodFactor;
405 double m_shiftOffset[2];
407 int m_factorizationIsOk;
433 template<
typename _MatrixType,
int _UpLo = Lower>
437 using Base::m_cholmod;
441 typedef _MatrixType MatrixType;
448 this->compute(matrix);
455 m_cholmod.final_asis = 0;
456 m_cholmod.supernodal = CHOLMOD_SIMPLICIAL;
457 m_cholmod.final_ll = 1;
484 template<
typename _MatrixType,
int _UpLo = Lower>
488 using Base::m_cholmod;
492 typedef _MatrixType MatrixType;
499 this->compute(matrix);
506 m_cholmod.final_asis = 1;
507 m_cholmod.supernodal = CHOLMOD_SIMPLICIAL;
533 template<
typename _MatrixType,
int _UpLo = Lower>
537 using Base::m_cholmod;
541 typedef _MatrixType MatrixType;
548 this->compute(matrix);
555 m_cholmod.final_asis = 1;
556 m_cholmod.supernodal = CHOLMOD_SUPERNODAL;
584 template<
typename _MatrixType,
int _UpLo = Lower>
588 using Base::m_cholmod;
592 typedef _MatrixType MatrixType;
599 this->compute(matrix);
604 void setMode(CholmodMode mode)
609 m_cholmod.final_asis = 1;
610 m_cholmod.supernodal = CHOLMOD_AUTO;
612 case CholmodSimplicialLLt:
613 m_cholmod.final_asis = 0;
614 m_cholmod.supernodal = CHOLMOD_SIMPLICIAL;
615 m_cholmod.final_ll = 1;
617 case CholmodSupernodalLLt:
618 m_cholmod.final_asis = 1;
619 m_cholmod.supernodal = CHOLMOD_SUPERNODAL;
622 m_cholmod.final_asis = 1;
623 m_cholmod.supernodal = CHOLMOD_SIMPLICIAL;
632 m_cholmod.final_asis = 1;
633 m_cholmod.supernodal = CHOLMOD_AUTO;
639 #endif // EIGEN_CHOLMODSUPPORT_H Scalar logDeterminant() const
Definition: CholmodSupport.h:355
A versatible sparse matrix representation.
Definition: SparseMatrix.h:92
A matrix or vector expression mapping an existing array of data.
Definition: Map.h:88
void factorize(const MatrixType &matrix)
Performs a numeric decomposition of matrix.
Definition: CholmodSupport.h:267
A base class for sparse solvers.
Definition: SparseSolverBase.h:67
MappedSparseMatrix< Scalar, Flags, StorageIndex > viewAsEigen(cholmod_sparse &cm)
Returns a view of the Cholmod sparse matrix cm as an Eigen sparse matrix.
Definition: CholmodSupport.h:155
Namespace containing all symbols from the Eigen library.
Definition: bench_norm.cpp:85
Pseudo expression to manipulate a triangular sparse matrix as a selfadjoint matrix.
Definition: SparseSelfAdjointView.h:43
A supernodal Cholesky (LLT) factorization and solver based on Cholmod.
Definition: CholmodSupport.h:534
const unsigned int RowMajorBit
for a matrix, this means that the storage order is row-major.
Definition: Constants.h:61
cholmod_common & cholmod()
Returns a reference to the Cholmod's configuration structure to get a full control over the performed...
Definition: CholmodSupport.h:280
ComputationInfo info() const
Reports whether previous computation was successful.
Definition: CholmodSupport.h:225
Scalar determinant() const
Definition: CholmodSupport.h:348
Index rows() const
Definition: SparseMatrixBase.h:167
Base class of any sparse matrices or sparse expressions.
Definition: ForwardDeclarations.h:281
View matrix as a lower triangular matrix.
Definition: Constants.h:204
The provided data did not satisfy the prerequisites.
Definition: Constants.h:434
a sparse vector class
Definition: SparseUtil.h:54
Convenience specialization of Stride to specify only an inner stride See class Map for some examples...
Definition: Stride.h:90
void analyzePattern(const MatrixType &matrix)
Performs a symbolic decomposition on the sparsity pattern of matrix.
Definition: CholmodSupport.h:245
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:33
A general Cholesky factorization and solver based on Cholmod.
Definition: CholmodSupport.h:585
cholmod_sparse viewAsCholmod(Ref< SparseMatrix< _Scalar, _Options, _StorageIndex > > mat)
Wraps the Eigen sparse matrix mat into a Cholmod sparse matrix object.
Definition: CholmodSupport.h:58
View matrix as an upper triangular matrix.
Definition: Constants.h:206
The base class for the direct Cholesky factorization of Cholmod.
Definition: CholmodSupport.h:173
Computation was successful.
Definition: Constants.h:432
A matrix or vector expression mapping an existing expression.
Definition: Ref.h:190
A simplicial direct Cholesky (LLT) factorization and solver based on Cholmod.
Definition: CholmodSupport.h:434
Definition: BandTriangularSolver.h:13
Definition: TutorialInplaceLU.cpp:2
A simplicial direct Cholesky (LDLT) factorization and solver based on Cholmod.
Definition: CholmodSupport.h:485
Derived & compute(const MatrixType &matrix)
Computes the sparse Cholesky decomposition of matrix.
Definition: CholmodSupport.h:232
Derived & setShift(const RealScalar &offset)
Sets the shift parameter that will be used to adjust the diagonal coefficients during the numerical f...
Definition: CholmodSupport.h:341
Definition: datatypes.h:12
The matrix class, also used for vectors and row-vectors.
Definition: Matrix.h:178
ComputationInfo
Enum for reporting the status of a computation.
Definition: Constants.h:430
Sparse matrix.
Definition: MappedSparseMatrix.h:32
Base class for all dense matrices, vectors, and expressions.
Definition: MatrixBase.h:48
Definition: ForwardDeclarations.h:17