10 #ifndef EIGEN_INCOMPLETE_CHOlESKY_H
11 #define EIGEN_INCOMPLETE_CHOlESKY_H
12 #include "Eigen/src/IterativeLinearSolvers/IncompleteLUT.h"
13 #include <Eigen/OrderingMethods>
29 template <
typename Scalar,
int _UpLo = Lower,
typename _OrderingType = NaturalOrdering<
int> >
33 typedef SparseMatrix<Scalar,ColMajor> MatrixType;
34 typedef _OrderingType OrderingType;
35 typedef typename MatrixType::RealScalar RealScalar;
36 typedef typename MatrixType::Index Index;
37 typedef PermutationMatrix<Dynamic, Dynamic, Index> PermutationType;
38 typedef Matrix<Scalar,Dynamic,1> ScalarType;
39 typedef Matrix<Index,Dynamic, 1> IndexType;
40 typedef std::vector<std::list<Index> > VectorList;
41 enum { UpLo = _UpLo };
49 Index rows()
const {
return m_L.rows(); }
51 Index cols()
const {
return m_L.cols(); }
59 ComputationInfo
info()
const
61 eigen_assert(m_isInitialized &&
"IncompleteLLT is not initialized.");
68 void setShift( Scalar shift) { m_shift = shift; }
73 template<
typename MatrixType>
77 ord(mat.template selfadjointView<UpLo>(), m_perm);
78 m_analysisIsOk =
true;
81 template<
typename MatrixType>
82 void factorize(
const MatrixType& amat);
84 template<
typename MatrixType>
85 void compute (
const MatrixType& matrix)
91 template<
typename Rhs,
typename Dest>
92 void _solve(
const Rhs& b, Dest& x)
const
94 eigen_assert(m_factorizationIsOk &&
"factorize() should be called first");
95 if (m_perm.rows() == b.rows())
96 x = m_perm.inverse() * b;
99 x = m_scal.asDiagonal() * x;
100 x = m_L.template triangularView<UnitLower>().solve(x);
101 x = m_L.adjoint().template triangularView<Upper>().solve(x);
102 if (m_perm.rows() == b.rows())
104 x = m_scal.asDiagonal() * x;
106 template<
typename Rhs>
inline const internal::solve_retval<IncompleteCholesky, Rhs>
107 solve(
const MatrixBase<Rhs>& b)
const
109 eigen_assert(m_factorizationIsOk &&
"IncompleteLLT did not succeed");
110 eigen_assert(m_isInitialized &&
"IncompleteLLT is not initialized.");
111 eigen_assert(cols()==b.rows()
112 &&
"IncompleteLLT::solve(): invalid number of rows of the right hand side matrix b");
113 return internal::solve_retval<IncompleteCholesky, Rhs>(*
this, b.derived());
116 SparseMatrix<Scalar,ColMajor> m_L;
120 bool m_factorizationIsOk;
121 bool m_isInitialized;
122 ComputationInfo m_info;
123 PermutationType m_perm;
126 template <
typename IdxType,
typename SclType>
127 inline void updateList(
const IdxType& colPtr, IdxType& rowIdx, SclType& vals,
const Index& col,
const Index& jk, IndexType& firstElt, VectorList& listCol);
130 template<
typename Scalar,
int _UpLo,
typename OrderingType>
131 template<
typename _MatrixType>
132 void IncompleteCholesky<Scalar,_UpLo, OrderingType>::factorize(
const _MatrixType& mat)
136 eigen_assert(m_analysisIsOk &&
"analyzePattern() should be called first");
141 if (m_perm.rows() == mat.rows() )
142 m_L.template selfadjointView<Lower>() = mat.template selfadjointView<_UpLo>().twistedBy(m_perm);
144 m_L.template selfadjointView<Lower>() = mat.template selfadjointView<_UpLo>();
146 Index n = m_L.cols();
147 Index nnz = m_L.nonZeros();
148 Map<ScalarType> vals(m_L.valuePtr(), nnz);
149 Map<IndexType> rowIdx(m_L.innerIndexPtr(), nnz);
150 Map<IndexType> colPtr( m_L.outerIndexPtr(), n+1);
151 IndexType firstElt(n-1);
152 VectorList listCol(n);
153 ScalarType curCol(n);
159 for (
int j = 0; j < n; j++)
161 m_scal(j) = m_L.col(j).norm();
162 m_scal(j) = sqrt(m_scal(j));
165 Scalar mindiag = vals[0];
166 for (
int j = 0; j < n; j++){
167 for (
int k = colPtr[j]; k < colPtr[j+1]; k++)
168 vals[k] /= (m_scal(j) * m_scal(rowIdx[k]));
169 mindiag = (min)(vals[colPtr[j]], mindiag);
172 if(mindiag < Scalar(0.)) m_shift = m_shift - mindiag;
174 for (
int j = 0; j < n; j++)
175 vals[colPtr[j]] += m_shift;
177 for (
int j=0; j < n; ++j)
181 Scalar diag = vals[colPtr[j]];
183 irow.setLinSpaced(n,0,n-1);
184 for (
int i = colPtr[j] + 1; i < colPtr[j+1]; i++)
186 curCol(rowIdx[i]) = vals[i];
187 irow(rowIdx[i]) = rowIdx[i];
189 std::list<int>::iterator k;
191 for(k = listCol[j].begin(); k != listCol[j].end(); k++)
193 int jk = firstElt(*k);
195 for (
int i = jk; i < colPtr[*k+1]; i++)
197 curCol(rowIdx[i]) -= vals[i] * vals[jk] ;
199 updateList(colPtr,rowIdx,vals, *k, jk, firstElt, listCol);
203 if(RealScalar(diag) <= 0)
205 std::cerr <<
"\nNegative diagonal during Incomplete factorization... "<< j <<
"\n";
206 m_info = NumericalIssue;
209 RealScalar rdiag = sqrt(RealScalar(diag));
210 vals[colPtr[j]] = rdiag;
211 for (
int i = j+1; i < n; i++)
216 vals[colPtr[i]] -= curCol(i) * curCol(i);
220 int p = colPtr[j+1] - colPtr[j] - 1 ;
221 internal::QuickSplit(curCol, irow, p);
224 for (
int i = colPtr[j]+1; i < colPtr[j+1]; i++)
226 vals[i] = curCol(cpt);
227 rowIdx[i] = irow(cpt);
231 Index jk = colPtr(j)+1;
232 updateList(colPtr,rowIdx,vals,j,jk,firstElt,listCol);
234 m_factorizationIsOk =
true;
235 m_isInitialized =
true;
239 template<
typename Scalar,
int _UpLo,
typename OrderingType>
240 template <
typename IdxType,
typename SclType>
241 inline void IncompleteCholesky<Scalar,_UpLo, OrderingType>::updateList(
const IdxType& colPtr, IdxType& rowIdx, SclType& vals,
const Index& col,
const Index& jk, IndexType& firstElt, VectorList& listCol)
243 if (jk < colPtr(col+1) )
245 Index p = colPtr(col+1) - jk;
247 rowIdx.segment(jk,p).minCoeff(&minpos);
249 if (rowIdx(minpos) != rowIdx(jk))
252 std::swap(rowIdx(jk),rowIdx(minpos));
253 std::swap(vals(jk),vals(minpos));
256 listCol[rowIdx(jk)].push_back(col);
261 template<
typename _Scalar,
int _UpLo,
typename OrderingType,
typename Rhs>
262 struct solve_retval<IncompleteCholesky<_Scalar, _UpLo, OrderingType>, Rhs>
263 : solve_retval_base<IncompleteCholesky<_Scalar, _UpLo, OrderingType>, Rhs>
265 typedef IncompleteCholesky<_Scalar, _UpLo, OrderingType> Dec;
266 EIGEN_MAKE_SOLVE_HELPERS(Dec,Rhs)
268 template<typename Dest>
void evalTo(Dest& dst)
const
270 dec()._solve(rhs(),dst);
Modified Incomplete Cholesky with dual threshold.
Definition: IncompleteCholesky.h:30
ComputationInfo info() const
Reports whether previous computation was successful.
Definition: IncompleteCholesky.h:59
void setShift(Scalar shift)
Set the initial shift parameter.
Definition: IncompleteCholesky.h:68
void analyzePattern(const MatrixType &mat)
Computes the fill reducing permutation vector.
Definition: IncompleteCholesky.h:74