10 #ifndef EIGEN_CHOLMODSUPPORT_H
11 #define EIGEN_CHOLMODSUPPORT_H
17 template<
typename Scalar,
typename CholmodType>
18 void cholmod_configure_matrix(CholmodType& mat)
20 if (internal::is_same<Scalar,float>::value)
22 mat.xtype = CHOLMOD_REAL;
23 mat.dtype = CHOLMOD_SINGLE;
25 else if (internal::is_same<Scalar,double>::value)
27 mat.xtype = CHOLMOD_REAL;
28 mat.dtype = CHOLMOD_DOUBLE;
30 else if (internal::is_same<Scalar,std::complex<float> >::value)
32 mat.xtype = CHOLMOD_COMPLEX;
33 mat.dtype = CHOLMOD_SINGLE;
35 else if (internal::is_same<Scalar,std::complex<double> >::value)
37 mat.xtype = CHOLMOD_COMPLEX;
38 mat.dtype = CHOLMOD_DOUBLE;
42 eigen_assert(
false &&
"Scalar type not supported by CHOLMOD");
51 template<
typename _Scalar,
int _Options,
typename _StorageIndex>
52 cholmod_sparse viewAsCholmod(SparseMatrix<_Scalar,_Options,_StorageIndex>& mat)
55 res.nzmax = mat.nonZeros();
56 res.nrow = mat.rows();;
57 res.ncol = mat.cols();
58 res.p = mat.outerIndexPtr();
59 res.i = mat.innerIndexPtr();
60 res.x = mat.valuePtr();
63 if(mat.isCompressed())
71 res.nz = mat.innerNonZeroPtr();
77 if (internal::is_same<_StorageIndex,int>::value)
79 res.itype = CHOLMOD_INT;
81 else if (internal::is_same<_StorageIndex,SuiteSparse_long>::value)
83 res.itype = CHOLMOD_LONG;
87 eigen_assert(
false &&
"Index type not supported yet");
91 internal::cholmod_configure_matrix<_Scalar>(res);
98 template<
typename _Scalar,
int _Options,
typename _Index>
99 const cholmod_sparse viewAsCholmod(
const SparseMatrix<_Scalar,_Options,_Index>& mat)
101 cholmod_sparse res = viewAsCholmod(mat.const_cast_derived());
107 template<
typename _Scalar,
int _Options,
typename _Index,
unsigned int UpLo>
108 cholmod_sparse viewAsCholmod(
const SparseSelfAdjointView<
const SparseMatrix<_Scalar,_Options,_Index>, UpLo>& mat)
110 cholmod_sparse res = viewAsCholmod(mat.matrix().const_cast_derived());
112 if(UpLo==
Upper) res.stype = 1;
113 if(UpLo==
Lower) res.stype = -1;
120 template<
typename Derived>
121 cholmod_dense viewAsCholmod(MatrixBase<Derived>& mat)
123 EIGEN_STATIC_ASSERT((internal::traits<Derived>::Flags&
RowMajorBit)==0,THIS_METHOD_IS_ONLY_FOR_COLUMN_MAJOR_MATRICES);
124 typedef typename Derived::Scalar Scalar;
127 res.nrow = mat.rows();
128 res.ncol = mat.cols();
129 res.nzmax = res.nrow * res.ncol;
130 res.d = Derived::IsVectorAtCompileTime ? mat.derived().size() : mat.derived().outerStride();
131 res.x = (
void*)(mat.derived().data());
134 internal::cholmod_configure_matrix<Scalar>(res);
141 template<
typename Scalar,
int Flags,
typename StorageIndex>
142 MappedSparseMatrix<Scalar,Flags,StorageIndex> viewAsEigen(cholmod_sparse& cm)
144 return MappedSparseMatrix<Scalar,Flags,StorageIndex>
145 (cm.nrow, cm.ncol,
static_cast<StorageIndex*
>(cm.p)[cm.ncol],
146 static_cast<StorageIndex*>(cm.p),
static_cast<StorageIndex*
>(cm.i),static_cast<Scalar*>(cm.x) );
150 CholmodAuto, CholmodSimplicialLLt, CholmodSupernodalLLt, CholmodLDLt
159 template<
typename _MatrixType,
int _UpLo,
typename Derived>
165 using Base::m_isInitialized;
167 typedef _MatrixType MatrixType;
168 enum { UpLo = _UpLo };
169 typedef typename MatrixType::Scalar Scalar;
170 typedef typename MatrixType::RealScalar RealScalar;
171 typedef MatrixType CholMatrixType;
172 typedef typename MatrixType::StorageIndex StorageIndex;
174 ColsAtCompileTime = MatrixType::ColsAtCompileTime,
175 MaxColsAtCompileTime = MatrixType::MaxColsAtCompileTime
181 : m_cholmodFactor(0), m_info(
Success)
183 m_shiftOffset[0] = m_shiftOffset[1] = RealScalar(0.0);
184 cholmod_start(&m_cholmod);
188 : m_cholmodFactor(0), m_info(
Success)
190 m_shiftOffset[0] = m_shiftOffset[1] = RealScalar(0.0);
191 cholmod_start(&m_cholmod);
198 cholmod_free_factor(&m_cholmodFactor, &m_cholmod);
199 cholmod_finish(&m_cholmod);
202 inline StorageIndex cols()
const {
return internal::convert_index<StorageIndex, Index>(m_cholmodFactor->n); }
203 inline StorageIndex rows()
const {
return internal::convert_index<StorageIndex, Index>(m_cholmodFactor->n); }
212 eigen_assert(m_isInitialized &&
"Decomposition is not initialized.");
234 cholmod_free_factor(&m_cholmodFactor, &m_cholmod);
237 cholmod_sparse A = viewAsCholmod(matrix.template selfadjointView<UpLo>());
238 m_cholmodFactor = cholmod_analyze(&A, &m_cholmod);
240 this->m_isInitialized =
true;
242 m_analysisIsOk =
true;
243 m_factorizationIsOk =
false;
254 eigen_assert(m_analysisIsOk &&
"You must first call analyzePattern()");
255 cholmod_sparse A = viewAsCholmod(matrix.template selfadjointView<UpLo>());
256 cholmod_factorize_p(&A, m_shiftOffset, 0, 0, m_cholmodFactor, &m_cholmod);
260 m_factorizationIsOk =
true;
265 cholmod_common&
cholmod() {
return m_cholmod; }
267 #ifndef EIGEN_PARSED_BY_DOXYGEN
269 template<
typename Rhs,
typename Dest>
272 eigen_assert(m_factorizationIsOk &&
"The decomposition is not in a valid state for solving, you must first call either compute() or symbolic()/numeric()");
273 const Index size = m_cholmodFactor->n;
274 EIGEN_UNUSED_VARIABLE(size);
275 eigen_assert(size==b.rows());
278 Rhs& b_ref(b.const_cast_derived());
279 cholmod_dense b_cd = viewAsCholmod(b_ref);
280 cholmod_dense* x_cd = cholmod_solve(CHOLMOD_A, m_cholmodFactor, &b_cd, &m_cholmod);
287 dest = Matrix<Scalar,Dest::RowsAtCompileTime,Dest::ColsAtCompileTime>::Map(reinterpret_cast<Scalar*>(x_cd->x),b.rows(),b.cols());
288 cholmod_free_dense(&x_cd, &m_cholmod);
292 template<
typename RhsScalar,
int RhsOptions,
typename RhsIndex,
typename DestScalar,
int DestOptions,
typename DestIndex>
293 void _solve_impl(
const SparseMatrix<RhsScalar,RhsOptions,RhsIndex> &b, SparseMatrix<DestScalar,DestOptions,DestIndex> &dest)
const
295 eigen_assert(m_factorizationIsOk &&
"The decomposition is not in a valid state for solving, you must first call either compute() or symbolic()/numeric()");
296 const Index size = m_cholmodFactor->n;
297 EIGEN_UNUSED_VARIABLE(size);
298 eigen_assert(size==b.rows());
301 cholmod_sparse b_cs = viewAsCholmod(b);
302 cholmod_sparse* x_cs = cholmod_spsolve(CHOLMOD_A, m_cholmodFactor, &b_cs, &m_cholmod);
309 dest = viewAsEigen<DestScalar,DestOptions,DestIndex>(*x_cs);
310 cholmod_free_sparse(&x_cs, &m_cholmod);
312 #endif // EIGEN_PARSED_BY_DOXYGEN
326 m_shiftOffset[0] = offset;
330 template<
typename Stream>
331 void dumpMemory(Stream& )
335 mutable cholmod_common m_cholmod;
336 cholmod_factor* m_cholmodFactor;
337 RealScalar m_shiftOffset[2];
339 int m_factorizationIsOk;
363 template<
typename _MatrixType,
int _UpLo = Lower>
367 using Base::m_cholmod;
371 typedef _MatrixType MatrixType;
385 m_cholmod.final_asis = 0;
386 m_cholmod.supernodal = CHOLMOD_SIMPLICIAL;
387 m_cholmod.final_ll = 1;
412 template<
typename _MatrixType,
int _UpLo = Lower>
416 using Base::m_cholmod;
420 typedef _MatrixType MatrixType;
434 m_cholmod.final_asis = 1;
435 m_cholmod.supernodal = CHOLMOD_SIMPLICIAL;
459 template<
typename _MatrixType,
int _UpLo = Lower>
463 using Base::m_cholmod;
467 typedef _MatrixType MatrixType;
481 m_cholmod.final_asis = 1;
482 m_cholmod.supernodal = CHOLMOD_SUPERNODAL;
508 template<
typename _MatrixType,
int _UpLo = Lower>
512 using Base::m_cholmod;
516 typedef _MatrixType MatrixType;
528 void setMode(CholmodMode mode)
533 m_cholmod.final_asis = 1;
534 m_cholmod.supernodal = CHOLMOD_AUTO;
536 case CholmodSimplicialLLt:
537 m_cholmod.final_asis = 0;
538 m_cholmod.supernodal = CHOLMOD_SIMPLICIAL;
539 m_cholmod.final_ll = 1;
541 case CholmodSupernodalLLt:
542 m_cholmod.final_asis = 1;
543 m_cholmod.supernodal = CHOLMOD_SUPERNODAL;
546 m_cholmod.final_asis = 1;
547 m_cholmod.supernodal = CHOLMOD_SIMPLICIAL;
556 m_cholmod.final_asis = 1;
557 m_cholmod.supernodal = CHOLMOD_AUTO;
563 #endif // EIGEN_CHOLMODSUPPORT_H
void factorize(const MatrixType &matrix)
Definition: CholmodSupport.h:252
A base class for sparse solvers.
Definition: SparseSolverBase.h:53
A supernodal Cholesky (LLT) factorization and solver based on Cholmod.
Definition: CholmodSupport.h:460
const unsigned int RowMajorBit
Definition: Constants.h:61
cholmod_common & cholmod()
Definition: CholmodSupport.h:265
Definition: Constants.h:204
Definition: Constants.h:434
void analyzePattern(const MatrixType &matrix)
Definition: CholmodSupport.h:230
A general Cholesky factorization and solver based on Cholmod.
Definition: CholmodSupport.h:509
The base class for the direct Cholesky factorization of Cholmod.
Definition: CholmodSupport.h:160
Definition: Constants.h:432
ComputationInfo info() const
Reports whether previous computation was successful.
Definition: CholmodSupport.h:210
A simplicial direct Cholesky (LLT) factorization and solver based on Cholmod.
Definition: CholmodSupport.h:364
Definition: Eigen_Colamd.h:54
A simplicial direct Cholesky (LDLT) factorization and solver based on Cholmod.
Definition: CholmodSupport.h:413
Derived & compute(const MatrixType &matrix)
Definition: CholmodSupport.h:217
Derived & setShift(const RealScalar &offset)
Definition: CholmodSupport.h:324
Definition: Constants.h:206
ComputationInfo
Definition: Constants.h:430
Base class for all dense matrices, vectors, and expressions.
Definition: MatrixBase.h:48