IncompleteCholesky.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. // This file is part of Eigen, a lightweight C++ template library
  2. // for linear algebra.
  3. //
  4. // Copyright (C) 2012 Désiré Nuentsa-Wakam <desire.nuentsa_wakam@inria.fr>
  5. // Copyright (C) 2015 Gael Guennebaud <gael.guennebaud@inria.fr>
  6. //
  7. // This Source Code Form is subject to the terms of the Mozilla
  8. // Public License v. 2.0. If a copy of the MPL was not distributed
  9. // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
  10. #ifndef EIGEN_INCOMPLETE_CHOlESKY_H
  11. #define EIGEN_INCOMPLETE_CHOlESKY_H
  12. #include <vector>
  13. #include <list>
  14. namespace Eigen {
  15. /**
  16. * \brief Modified Incomplete Cholesky with dual threshold
  17. *
  18. * References : C-J. Lin and J. J. Moré, Incomplete Cholesky Factorizations with
  19. * Limited memory, SIAM J. Sci. Comput. 21(1), pp. 24-45, 1999
  20. *
  21. * \tparam Scalar the scalar type of the input matrices
  22. * \tparam _UpLo The triangular part that will be used for the computations. It can be Lower
  23. * or Upper. Default is Lower.
  24. * \tparam _OrderingType The ordering method to use, either AMDOrdering<> or NaturalOrdering<>. Default is AMDOrdering<int>,
  25. * unless EIGEN_MPL2_ONLY is defined, in which case the default is NaturalOrdering<int>.
  26. *
  27. * \implsparsesolverconcept
  28. *
  29. * It performs the following incomplete factorization: \f$ S P A P' S \approx L L' \f$
  30. * where L is a lower triangular factor, S is a diagonal scaling matrix, and P is a
  31. * fill-in reducing permutation as computed by the ordering method.
  32. *
  33. * \b Shifting \b strategy: Let \f$ B = S P A P' S \f$ be the scaled matrix on which the factorization is carried out,
  34. * and \f$ \beta \f$ be the minimum value of the diagonal. If \f$ \beta > 0 \f$ then, the factorization is directly performed
  35. * on the matrix B. Otherwise, the factorization is performed on the shifted matrix \f$ B + (\sigma+|\beta| I \f$ where
  36. * \f$ \sigma \f$ is the initial shift value as returned and set by setInitialShift() method. The default value is \f$ \sigma = 10^{-3} \f$.
  37. * If the factorization fails, then the shift in doubled until it succeed or a maximum of ten attempts. If it still fails, as returned by
  38. * the info() method, then you can either increase the initial shift, or better use another preconditioning technique.
  39. *
  40. */
  41. template <typename Scalar, int _UpLo = Lower, typename _OrderingType =
  42. #ifndef EIGEN_MPL2_ONLY
  43. AMDOrdering<int>
  44. #else
  45. NaturalOrdering<int>
  46. #endif
  47. >
  48. class IncompleteCholesky : public SparseSolverBase<IncompleteCholesky<Scalar,_UpLo,_OrderingType> >
  49. {
  50. protected:
  51. typedef SparseSolverBase<IncompleteCholesky<Scalar,_UpLo,_OrderingType> > Base;
  52. using Base::m_isInitialized;
  53. public:
  54. typedef typename NumTraits<Scalar>::Real RealScalar;
  55. typedef _OrderingType OrderingType;
  56. typedef typename OrderingType::PermutationType PermutationType;
  57. typedef typename PermutationType::StorageIndex StorageIndex;
  58. typedef SparseMatrix<Scalar,ColMajor,StorageIndex> FactorType;
  59. typedef Matrix<Scalar,Dynamic,1> VectorSx;
  60. typedef Matrix<RealScalar,Dynamic,1> VectorRx;
  61. typedef Matrix<StorageIndex,Dynamic, 1> VectorIx;
  62. typedef std::vector<std::list<StorageIndex> > VectorList;
  63. enum { UpLo = _UpLo };
  64. enum {
  65. ColsAtCompileTime = Dynamic,
  66. MaxColsAtCompileTime = Dynamic
  67. };
  68. public:
  69. /** Default constructor leaving the object in a partly non-initialized stage.
  70. *
  71. * You must call compute() or the pair analyzePattern()/factorize() to make it valid.
  72. *
  73. * \sa IncompleteCholesky(const MatrixType&)
  74. */
  75. IncompleteCholesky() : m_initialShift(1e-3),m_factorizationIsOk(false) {}
  76. /** Constructor computing the incomplete factorization for the given matrix \a matrix.
  77. */
  78. template<typename MatrixType>
  79. IncompleteCholesky(const MatrixType& matrix) : m_initialShift(1e-3),m_factorizationIsOk(false)
  80. {
  81. compute(matrix);
  82. }
  83. /** \returns number of rows of the factored matrix */
  84. Index rows() const { return m_L.rows(); }
  85. /** \returns number of columns of the factored matrix */
  86. Index cols() const { return m_L.cols(); }
  87. /** \brief Reports whether previous computation was successful.
  88. *
  89. * It triggers an assertion if \c *this has not been initialized through the respective constructor,
  90. * or a call to compute() or analyzePattern().
  91. *
  92. * \returns \c Success if computation was successful,
  93. * \c NumericalIssue if the matrix appears to be negative.
  94. */
  95. ComputationInfo info() const
  96. {
  97. eigen_assert(m_isInitialized && "IncompleteCholesky is not initialized.");
  98. return m_info;
  99. }
  100. /** \brief Set the initial shift parameter \f$ \sigma \f$.
  101. */
  102. void setInitialShift(RealScalar shift) { m_initialShift = shift; }
  103. /** \brief Computes the fill reducing permutation vector using the sparsity pattern of \a mat
  104. */
  105. template<typename MatrixType>
  106. void analyzePattern(const MatrixType& mat)
  107. {
  108. OrderingType ord;
  109. PermutationType pinv;
  110. ord(mat.template selfadjointView<UpLo>(), pinv);
  111. if(pinv.size()>0) m_perm = pinv.inverse();
  112. else m_perm.resize(0);
  113. m_L.resize(mat.rows(), mat.cols());
  114. m_analysisIsOk = true;
  115. m_isInitialized = true;
  116. m_info = Success;
  117. }
  118. /** \brief Performs the numerical factorization of the input matrix \a mat
  119. *
  120. * The method analyzePattern() or compute() must have been called beforehand
  121. * with a matrix having the same pattern.
  122. *
  123. * \sa compute(), analyzePattern()
  124. */
  125. template<typename MatrixType>
  126. void factorize(const MatrixType& mat);
  127. /** Computes or re-computes the incomplete Cholesky factorization of the input matrix \a mat
  128. *
  129. * It is a shortcut for a sequential call to the analyzePattern() and factorize() methods.
  130. *
  131. * \sa analyzePattern(), factorize()
  132. */
  133. template<typename MatrixType>
  134. void compute(const MatrixType& mat)
  135. {
  136. analyzePattern(mat);
  137. factorize(mat);
  138. }
  139. // internal
  140. template<typename Rhs, typename Dest>
  141. void _solve_impl(const Rhs& b, Dest& x) const
  142. {
  143. eigen_assert(m_factorizationIsOk && "factorize() should be called first");
  144. if (m_perm.rows() == b.rows()) x = m_perm * b;
  145. else x = b;
  146. x = m_scale.asDiagonal() * x;
  147. x = m_L.template triangularView<Lower>().solve(x);
  148. x = m_L.adjoint().template triangularView<Upper>().solve(x);
  149. x = m_scale.asDiagonal() * x;
  150. if (m_perm.rows() == b.rows())
  151. x = m_perm.inverse() * x;
  152. }
  153. /** \returns the sparse lower triangular factor L */
  154. const FactorType& matrixL() const { eigen_assert("m_factorizationIsOk"); return m_L; }
  155. /** \returns a vector representing the scaling factor S */
  156. const VectorRx& scalingS() const { eigen_assert("m_factorizationIsOk"); return m_scale; }
  157. /** \returns the fill-in reducing permutation P (can be empty for a natural ordering) */
  158. const PermutationType& permutationP() const { eigen_assert("m_analysisIsOk"); return m_perm; }
  159. protected:
  160. FactorType m_L; // The lower part stored in CSC
  161. VectorRx m_scale; // The vector for scaling the matrix
  162. RealScalar m_initialShift; // The initial shift parameter
  163. bool m_analysisIsOk;
  164. bool m_factorizationIsOk;
  165. ComputationInfo m_info;
  166. PermutationType m_perm;
  167. private:
  168. inline void updateList(Ref<const VectorIx> colPtr, Ref<VectorIx> rowIdx, Ref<VectorSx> vals, const Index& col, const Index& jk, VectorIx& firstElt, VectorList& listCol);
  169. };
  170. // Based on the following paper:
  171. // C-J. Lin and J. J. Moré, Incomplete Cholesky Factorizations with
  172. // Limited memory, SIAM J. Sci. Comput. 21(1), pp. 24-45, 1999
  173. // http://ftp.mcs.anl.gov/pub/tech_reports/reports/P682.pdf
  174. template<typename Scalar, int _UpLo, typename OrderingType>
  175. template<typename _MatrixType>
  176. void IncompleteCholesky<Scalar,_UpLo, OrderingType>::factorize(const _MatrixType& mat)
  177. {
  178. using std::sqrt;
  179. eigen_assert(m_analysisIsOk && "analyzePattern() should be called first");
  180. // Dropping strategy : Keep only the p largest elements per column, where p is the number of elements in the column of the original matrix. Other strategies will be added
  181. // Apply the fill-reducing permutation computed in analyzePattern()
  182. if (m_perm.rows() == mat.rows() ) // To detect the null permutation
  183. {
  184. // The temporary is needed to make sure that the diagonal entry is properly sorted
  185. FactorType tmp(mat.rows(), mat.cols());
  186. tmp = mat.template selfadjointView<_UpLo>().twistedBy(m_perm);
  187. m_L.template selfadjointView<Lower>() = tmp.template selfadjointView<Lower>();
  188. }
  189. else
  190. {
  191. m_L.template selfadjointView<Lower>() = mat.template selfadjointView<_UpLo>();
  192. }
  193. Index n = m_L.cols();
  194. Index nnz = m_L.nonZeros();
  195. Map<VectorSx> vals(m_L.valuePtr(), nnz); //values
  196. Map<VectorIx> rowIdx(m_L.innerIndexPtr(), nnz); //Row indices
  197. Map<VectorIx> colPtr( m_L.outerIndexPtr(), n+1); // Pointer to the beginning of each row
  198. VectorIx firstElt(n-1); // for each j, points to the next entry in vals that will be used in the factorization
  199. VectorList listCol(n); // listCol(j) is a linked list of columns to update column j
  200. VectorSx col_vals(n); // Store a nonzero values in each column
  201. VectorIx col_irow(n); // Row indices of nonzero elements in each column
  202. VectorIx col_pattern(n);
  203. col_pattern.fill(-1);
  204. StorageIndex col_nnz;
  205. // Computes the scaling factors
  206. m_scale.resize(n);
  207. m_scale.setZero();
  208. for (Index j = 0; j < n; j++)
  209. for (Index k = colPtr[j]; k < colPtr[j+1]; k++)
  210. {
  211. m_scale(j) += numext::abs2(vals(k));
  212. if(rowIdx[k]!=j)
  213. m_scale(rowIdx[k]) += numext::abs2(vals(k));
  214. }
  215. m_scale = m_scale.cwiseSqrt().cwiseSqrt();
  216. for (Index j = 0; j < n; ++j)
  217. if(m_scale(j)>(std::numeric_limits<RealScalar>::min)())
  218. m_scale(j) = RealScalar(1)/m_scale(j);
  219. else
  220. m_scale(j) = 1;
  221. // TODO disable scaling if not needed, i.e., if it is roughly uniform? (this will make solve() faster)
  222. // Scale and compute the shift for the matrix
  223. RealScalar mindiag = NumTraits<RealScalar>::highest();
  224. for (Index j = 0; j < n; j++)
  225. {
  226. for (Index k = colPtr[j]; k < colPtr[j+1]; k++)
  227. vals[k] *= (m_scale(j)*m_scale(rowIdx[k]));
  228. eigen_internal_assert(rowIdx[colPtr[j]]==j && "IncompleteCholesky: only the lower triangular part must be stored");
  229. mindiag = numext::mini(numext::real(vals[colPtr[j]]), mindiag);
  230. }
  231. FactorType L_save = m_L;
  232. RealScalar shift = 0;
  233. if(mindiag <= RealScalar(0.))
  234. shift = m_initialShift - mindiag;
  235. m_info = NumericalIssue;
  236. // Try to perform the incomplete factorization using the current shift
  237. int iter = 0;
  238. do
  239. {
  240. // Apply the shift to the diagonal elements of the matrix
  241. for (Index j = 0; j < n; j++)
  242. vals[colPtr[j]] += shift;
  243. // jki version of the Cholesky factorization
  244. Index j=0;
  245. for (; j < n; ++j)
  246. {
  247. // Left-looking factorization of the j-th column
  248. // First, load the j-th column into col_vals
  249. Scalar diag = vals[colPtr[j]]; // It is assumed that only the lower part is stored
  250. col_nnz = 0;
  251. for (Index i = colPtr[j] + 1; i < colPtr[j+1]; i++)
  252. {
  253. StorageIndex l = rowIdx[i];
  254. col_vals(col_nnz) = vals[i];
  255. col_irow(col_nnz) = l;
  256. col_pattern(l) = col_nnz;
  257. col_nnz++;
  258. }
  259. {
  260. typename std::list<StorageIndex>::iterator k;
  261. // Browse all previous columns that will update column j
  262. for(k = listCol[j].begin(); k != listCol[j].end(); k++)
  263. {
  264. Index jk = firstElt(*k); // First element to use in the column
  265. eigen_internal_assert(rowIdx[jk]==j);
  266. Scalar v_j_jk = numext::conj(vals[jk]);
  267. jk += 1;
  268. for (Index i = jk; i < colPtr[*k+1]; i++)
  269. {
  270. StorageIndex l = rowIdx[i];
  271. if(col_pattern[l]<0)
  272. {
  273. col_vals(col_nnz) = vals[i] * v_j_jk;
  274. col_irow[col_nnz] = l;
  275. col_pattern(l) = col_nnz;
  276. col_nnz++;
  277. }
  278. else
  279. col_vals(col_pattern[l]) -= vals[i] * v_j_jk;
  280. }
  281. updateList(colPtr,rowIdx,vals, *k, jk, firstElt, listCol);
  282. }
  283. }
  284. // Scale the current column
  285. if(numext::real(diag) <= 0)
  286. {
  287. if(++iter>=10)
  288. return;
  289. // increase shift
  290. shift = numext::maxi(m_initialShift,RealScalar(2)*shift);
  291. // restore m_L, col_pattern, and listCol
  292. vals = Map<const VectorSx>(L_save.valuePtr(), nnz);
  293. rowIdx = Map<const VectorIx>(L_save.innerIndexPtr(), nnz);
  294. colPtr = Map<const VectorIx>(L_save.outerIndexPtr(), n+1);
  295. col_pattern.fill(-1);
  296. for(Index i=0; i<n; ++i)
  297. listCol[i].clear();
  298. break;
  299. }
  300. RealScalar rdiag = sqrt(numext::real(diag));
  301. vals[colPtr[j]] = rdiag;
  302. for (Index k = 0; k<col_nnz; ++k)
  303. {
  304. Index i = col_irow[k];
  305. //Scale
  306. col_vals(k) /= rdiag;
  307. //Update the remaining diagonals with col_vals
  308. vals[colPtr[i]] -= numext::abs2(col_vals(k));
  309. }
  310. // Select the largest p elements
  311. // p is the original number of elements in the column (without the diagonal)
  312. Index p = colPtr[j+1] - colPtr[j] - 1 ;
  313. Ref<VectorSx> cvals = col_vals.head(col_nnz);
  314. Ref<VectorIx> cirow = col_irow.head(col_nnz);
  315. internal::QuickSplit(cvals,cirow, p);
  316. // Insert the largest p elements in the matrix
  317. Index cpt = 0;
  318. for (Index i = colPtr[j]+1; i < colPtr[j+1]; i++)
  319. {
  320. vals[i] = col_vals(cpt);
  321. rowIdx[i] = col_irow(cpt);
  322. // restore col_pattern:
  323. col_pattern(col_irow(cpt)) = -1;
  324. cpt++;
  325. }
  326. // Get the first smallest row index and put it after the diagonal element
  327. Index jk = colPtr(j)+1;
  328. updateList(colPtr,rowIdx,vals,j,jk,firstElt,listCol);
  329. }
  330. if(j==n)
  331. {
  332. m_factorizationIsOk = true;
  333. m_info = Success;
  334. }
  335. } while(m_info!=Success);
  336. }
  337. template<typename Scalar, int _UpLo, typename OrderingType>
  338. inline void IncompleteCholesky<Scalar,_UpLo, OrderingType>::updateList(Ref<const VectorIx> colPtr, Ref<VectorIx> rowIdx, Ref<VectorSx> vals, const Index& col, const Index& jk, VectorIx& firstElt, VectorList& listCol)
  339. {
  340. if (jk < colPtr(col+1) )
  341. {
  342. Index p = colPtr(col+1) - jk;
  343. Index minpos;
  344. rowIdx.segment(jk,p).minCoeff(&minpos);
  345. minpos += jk;
  346. if (rowIdx(minpos) != rowIdx(jk))
  347. {
  348. //Swap
  349. std::swap(rowIdx(jk),rowIdx(minpos));
  350. std::swap(vals(jk),vals(minpos));
  351. }
  352. firstElt(col) = internal::convert_index<StorageIndex,Index>(jk);
  353. listCol[rowIdx(jk)].push_back(internal::convert_index<StorageIndex,Index>(col));
  354. }
  355. }
  356. } // end namespace Eigen
  357. #endif