11 #ifndef EIGEN_INCOMPLETE_CHOlESKY_H 12 #define EIGEN_INCOMPLETE_CHOlESKY_H 44 template <
typename Scalar,
int _UpLo =
Lower,
typename _OrderingType =
45 #ifndef EIGEN_MPL2_ONLY 55 using Base::m_isInitialized;
58 typedef _OrderingType OrderingType;
59 typedef typename OrderingType::PermutationType PermutationType;
60 typedef typename PermutationType::StorageIndex StorageIndex;
65 typedef std::vector<std::list<StorageIndex> > VectorList;
66 enum { UpLo = _UpLo };
83 template<
typename MatrixType>
106 eigen_assert(m_isInitialized &&
"IncompleteCholesky is not initialized.");
116 template<
typename MatrixType>
120 PermutationType pinv;
121 ord(mat.template selfadjointView<UpLo>(), pinv);
122 if(pinv.size()>0) m_perm = pinv.inverse();
123 else m_perm.resize(0);
124 m_L.
resize(mat.rows(), mat.cols());
125 m_analysisIsOk =
true;
126 m_isInitialized =
true;
137 template<
typename MatrixType>
146 template<
typename MatrixType>
154 template<
typename Rhs,
typename Dest>
155 void _solve_impl(
const Rhs& b, Dest& x)
const 157 eigen_assert(m_factorizationIsOk &&
"factorize() should be called first");
158 if (m_perm.rows() == b.rows()) x = m_perm * b;
160 x = m_scale.asDiagonal() * x;
161 x = m_L.template triangularView<Lower>().
solve(x);
162 x = m_L.adjoint().template triangularView<Upper>().
solve(x);
163 x = m_scale.asDiagonal() * x;
164 if (m_perm.rows() == b.rows())
165 x = m_perm.inverse() * x;
169 const FactorType&
matrixL()
const { eigen_assert(
"m_factorizationIsOk");
return m_L; }
172 const VectorRx&
scalingS()
const { eigen_assert(
"m_factorizationIsOk");
return m_scale; }
175 const PermutationType&
permutationP()
const { eigen_assert(
"m_analysisIsOk");
return m_perm; }
180 RealScalar m_initialShift;
182 bool m_factorizationIsOk;
184 PermutationType m_perm;
194 template<
typename Scalar,
int _UpLo,
typename OrderingType>
195 template<
typename _MatrixType>
199 eigen_assert(m_analysisIsOk &&
"analyzePattern() should be called first");
204 if (m_perm.rows() == mat.rows() )
208 tmp = mat.template selfadjointView<_UpLo>().twistedBy(m_perm);
209 m_L.template selfadjointView<Lower>() = tmp.template selfadjointView<Lower>();
213 m_L.template selfadjointView<Lower>() = mat.template selfadjointView<_UpLo>();
222 VectorList listCol(n);
226 col_pattern.fill(-1);
227 StorageIndex col_nnz;
233 for (
Index j = 0; j < n; j++)
234 for (
Index k = colPtr[j]; k < colPtr[j+1]; k++)
236 m_scale(j) += numext::abs2(vals(k));
238 m_scale(rowIdx[k]) += numext::abs2(vals(k));
241 m_scale = m_scale.cwiseSqrt().cwiseSqrt();
243 for (
Index j = 0; j < n; ++j)
244 if(m_scale(j)>(std::numeric_limits<RealScalar>::min)())
245 m_scale(j) = RealScalar(1)/m_scale(j);
253 for (
Index j = 0; j < n; j++)
255 for (
Index k = colPtr[j]; k < colPtr[j+1]; k++)
256 vals[k] *= (m_scale(j)*m_scale(rowIdx[k]));
257 eigen_internal_assert(rowIdx[colPtr[j]]==j &&
"IncompleteCholesky: only the lower triangular part must be stored");
258 mindiag = numext::mini(numext::real(vals[colPtr[j]]), mindiag);
263 RealScalar shift = 0;
264 if(mindiag <= RealScalar(0.))
265 shift = m_initialShift - mindiag;
274 for (
Index j = 0; j < n; j++)
275 vals[colPtr[j]] += shift;
283 Scalar diag = vals[colPtr[j]];
285 for (
Index i = colPtr[j] + 1; i < colPtr[j+1]; i++)
287 StorageIndex l = rowIdx[i];
288 col_vals(col_nnz) = vals[i];
289 col_irow(col_nnz) = l;
290 col_pattern(l) = col_nnz;
294 typename std::list<StorageIndex>::iterator k;
296 for(k = listCol[j].begin(); k != listCol[j].end(); k++)
298 Index jk = firstElt(*k);
299 eigen_internal_assert(rowIdx[jk]==j);
300 Scalar v_j_jk = numext::conj(vals[jk]);
303 for (
Index i = jk; i < colPtr[*k+1]; i++)
305 StorageIndex l = rowIdx[i];
308 col_vals(col_nnz) = vals[i] * v_j_jk;
309 col_irow[col_nnz] = l;
310 col_pattern(l) = col_nnz;
314 col_vals(col_pattern[l]) -= vals[i] * v_j_jk;
316 updateList(colPtr,rowIdx,vals, *k, jk, firstElt, listCol);
321 if(numext::real(diag) <= 0)
327 shift = numext::maxi(m_initialShift,RealScalar(2)*shift);
332 col_pattern.fill(-1);
333 for(
Index i=0; i<n; ++i)
339 RealScalar rdiag = sqrt(numext::real(diag));
340 vals[colPtr[j]] = rdiag;
341 for (
Index k = 0; k<col_nnz; ++k)
343 Index i = col_irow[k];
345 col_vals(k) /= rdiag;
347 vals[colPtr[i]] -= numext::abs2(col_vals(k));
351 Index p = colPtr[j+1] - colPtr[j] - 1 ;
354 internal::QuickSplit(cvals,cirow, p);
357 for (
Index i = colPtr[j]+1; i < colPtr[j+1]; i++)
359 vals[i] = col_vals(cpt);
360 rowIdx[i] = col_irow(cpt);
362 col_pattern(col_irow(cpt)) = -1;
366 Index jk = colPtr(j)+1;
367 updateList(colPtr,rowIdx,vals,j,jk,firstElt,listCol);
372 m_factorizationIsOk =
true;
378 template<
typename Scalar,
int _UpLo,
typename OrderingType>
381 if (jk < colPtr(col+1) )
383 Index p = colPtr(col+1) - jk;
385 rowIdx.segment(jk,p).minCoeff(&minpos);
387 if (rowIdx(minpos) != rowIdx(jk))
390 std::swap(rowIdx(jk),rowIdx(minpos));
391 std::swap(vals(jk),vals(minpos));
393 firstElt(col) = internal::convert_index<StorageIndex,Index>(jk);
394 listCol[rowIdx(jk)].push_back(internal::convert_index<StorageIndex,Index>(col));
ComputationInfo info() const
Reports whether previous computation was successful.
Definition: IncompleteCholesky.h:104
const FactorType & matrixL() const
Definition: IncompleteCholesky.h:169
void factorize(const MatrixType &mat)
Performs the numerical factorization of the input matrix mat.
const Scalar * valuePtr() const
Definition: SparseMatrix.h:144
A matrix or vector expression mapping an existing array of data.
Definition: Map.h:88
Modified Incomplete Cholesky with dual threshold.
Definition: IncompleteCholesky.h:51
A base class for sparse solvers.
Definition: SparseSolverBase.h:67
IncompleteCholesky(const MatrixType &matrix)
Constructor computing the incomplete factorization for the given matrix matrix.
Definition: IncompleteCholesky.h:84
const Solve< IncompleteCholesky< Scalar, _UpLo, _OrderingType >, Rhs > solve(const MatrixBase< Rhs > &b) const
Definition: SparseSolverBase.h:88
Namespace containing all symbols from the Eigen library.
Definition: bench_norm.cpp:85
Holds information about the various numeric (i.e.
Definition: NumTraits.h:150
Index cols() const
Definition: SparseMatrix.h:134
Index cols() const
Definition: IncompleteCholesky.h:93
Index nonZeros() const
Definition: SparseCompressedBase.h:56
IncompleteCholesky()
Default constructor leaving the object in a partly non-initialized stage.
Definition: IncompleteCholesky.h:79
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE void resize(Index rows, Index cols)
Resizes *this to a rows x cols matrix.
Definition: PlainObjectBase.h:273
View matrix as a lower triangular matrix.
Definition: Constants.h:204
Index rows() const
Definition: IncompleteCholesky.h:90
The provided data did not satisfy the prerequisites.
Definition: Constants.h:434
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:33
Index rows() const
Definition: SparseMatrix.h:132
void resize(Index rows, Index cols)
Resizes the matrix to a rows x cols matrix and initializes it to zero.
Definition: SparseMatrix.h:617
const VectorRx & scalingS() const
Definition: IncompleteCholesky.h:172
void setInitialShift(RealScalar shift)
Set the initial shift parameter .
Definition: IncompleteCholesky.h:112
const PermutationType & permutationP() const
Definition: IncompleteCholesky.h:175
EIGEN_DEVICE_FUNC Derived & setZero(Index size)
Resizes to the given size, and sets all coefficients in this expression to zero.
Definition: CwiseNullaryOp.h:515
Computation was successful.
Definition: Constants.h:432
A matrix or vector expression mapping an existing expression.
Definition: Ref.h:190
void analyzePattern(const MatrixType &mat)
Computes the fill reducing permutation vector using the sparsity pattern of mat.
Definition: IncompleteCholesky.h:117
const StorageIndex * innerIndexPtr() const
Definition: SparseMatrix.h:153
const StorageIndex * outerIndexPtr() const
Definition: SparseMatrix.h:162
const int Dynamic
This value means that a positive quantity (e.g., a size) is not known at compile-time, and that instead the value is stored in some runtime variable.
Definition: Constants.h:21
void compute(const MatrixType &mat)
Computes or re-computes the incomplete Cholesky factorization of the input matrix mat...
Definition: IncompleteCholesky.h:147
ComputationInfo
Enum for reporting the status of a computation.
Definition: Constants.h:430