10#ifndef EIGEN_CHOLMODSUPPORT_H
11#define EIGEN_CHOLMODSUPPORT_H
17template<
typename Scalar,
typename CholmodType>
18void 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");
51template<
typename _Scalar,
int _Options,
typename _StorageIndex>
52cholmod_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);
98template<
typename _Scalar,
int _Options,
typename _Index>
99const cholmod_sparse viewAsCholmod(
const SparseMatrix<_Scalar,_Options,_Index>& mat)
101 cholmod_sparse res = viewAsCholmod(mat.const_cast_derived());
107template<
typename _Scalar,
int _Options,
typename _Index,
unsigned int UpLo>
108cholmod_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;
120template<
typename Derived>
121cholmod_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);
141template<
typename Scalar,
int Flags,
typename StorageIndex>
142MappedSparseMatrix<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
159template<
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);
188 : m_cholmodFactor(0), m_info(
Success)
190 m_shiftOffset[0] = m_shiftOffset[1] = RealScalar(0.0);
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.");
240 this->m_isInitialized =
true;
242 m_analysisIsOk =
true;
243 m_factorizationIsOk =
false;
254 eigen_assert(m_analysisIsOk &&
"You must first call analyzePattern()");
260 m_factorizationIsOk =
true;
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());
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);
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;
363template<
typename _MatrixType,
int _UpLo = Lower>
367 using Base::m_cholmod;
371 typedef _MatrixType MatrixType;
385 m_cholmod.final_asis = 0;
387 m_cholmod.final_ll = 1;
412template<
typename _MatrixType,
int _UpLo = Lower>
416 using Base::m_cholmod;
420 typedef _MatrixType MatrixType;
434 m_cholmod.final_asis = 1;
459template<
typename _MatrixType,
int _UpLo = Lower>
463 using Base::m_cholmod;
467 typedef _MatrixType MatrixType;
481 m_cholmod.final_asis = 1;
508template<
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;
536 case CholmodSimplicialLLt:
537 m_cholmod.final_asis = 0;
539 m_cholmod.final_ll = 1;
541 case CholmodSupernodalLLt:
542 m_cholmod.final_asis = 1;
546 m_cholmod.final_asis = 1;
556 m_cholmod.final_asis = 1;
The base class for the direct Cholesky factorization of Cholmod.
Definition CholmodSupport.h:161
cholmod_common & cholmod()
Returns a reference to the Cholmod's configuration structure to get a full control over the performed...
Definition CholmodSupport.h:265
Derived & compute(const MatrixType &matrix)
Computes the sparse Cholesky decomposition of matrix.
Definition CholmodSupport.h:217
void analyzePattern(const MatrixType &matrix)
Performs a symbolic decomposition on the sparsity pattern of matrix.
Definition CholmodSupport.h:230
void factorize(const MatrixType &matrix)
Performs a numeric decomposition of matrix.
Definition CholmodSupport.h:252
ComputationInfo info() const
Reports whether previous computation was successful.
Definition CholmodSupport.h:210
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:324
A general Cholesky factorization and solver based on Cholmod.
Definition CholmodSupport.h:510
A simplicial direct Cholesky (LDLT) factorization and solver based on Cholmod.
Definition CholmodSupport.h:414
A simplicial direct Cholesky (LLT) factorization and solver based on Cholmod.
Definition CholmodSupport.h:365
A supernodal Cholesky (LLT) factorization and solver based on Cholmod.
Definition CholmodSupport.h:461
Pseudo expression representing a solving operation.
Definition Solve.h:63
A base class for sparse solvers.
Definition SparseSolverBase.h:54
ComputationInfo
Enum for reporting the status of a computation.
Definition Constants.h:430
@ NumericalIssue
The provided data did not satisfy the prerequisites.
Definition Constants.h:434
@ Success
Computation was successful.
Definition Constants.h:432
Definition inference.c:32