xref: /aosp_15_r20/external/eigen/Eigen/src/IterativeLinearSolvers/IncompleteLUT.h (revision bf2c37156dfe67e5dfebd6d394bad8b2ab5804d4)
1*bf2c3715SXin Li // This file is part of Eigen, a lightweight C++ template library
2*bf2c3715SXin Li // for linear algebra.
3*bf2c3715SXin Li //
4*bf2c3715SXin Li // Copyright (C) 2012 Désiré Nuentsa-Wakam <[email protected]>
5*bf2c3715SXin Li // Copyright (C) 2014 Gael Guennebaud <[email protected]>
6*bf2c3715SXin Li //
7*bf2c3715SXin Li // This Source Code Form is subject to the terms of the Mozilla
8*bf2c3715SXin Li // Public License v. 2.0. If a copy of the MPL was not distributed
9*bf2c3715SXin Li // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
10*bf2c3715SXin Li 
11*bf2c3715SXin Li #ifndef EIGEN_INCOMPLETE_LUT_H
12*bf2c3715SXin Li #define EIGEN_INCOMPLETE_LUT_H
13*bf2c3715SXin Li 
14*bf2c3715SXin Li 
15*bf2c3715SXin Li namespace Eigen {
16*bf2c3715SXin Li 
17*bf2c3715SXin Li namespace internal {
18*bf2c3715SXin Li 
19*bf2c3715SXin Li /** \internal
20*bf2c3715SXin Li   * Compute a quick-sort split of a vector
21*bf2c3715SXin Li   * On output, the vector row is permuted such that its elements satisfy
22*bf2c3715SXin Li   * abs(row(i)) >= abs(row(ncut)) if i<ncut
23*bf2c3715SXin Li   * abs(row(i)) <= abs(row(ncut)) if i>ncut
24*bf2c3715SXin Li   * \param row The vector of values
25*bf2c3715SXin Li   * \param ind The array of index for the elements in @p row
26*bf2c3715SXin Li   * \param ncut  The number of largest elements to keep
27*bf2c3715SXin Li   **/
28*bf2c3715SXin Li template <typename VectorV, typename VectorI>
QuickSplit(VectorV & row,VectorI & ind,Index ncut)29*bf2c3715SXin Li Index QuickSplit(VectorV &row, VectorI &ind, Index ncut)
30*bf2c3715SXin Li {
31*bf2c3715SXin Li   typedef typename VectorV::RealScalar RealScalar;
32*bf2c3715SXin Li   using std::swap;
33*bf2c3715SXin Li   using std::abs;
34*bf2c3715SXin Li   Index mid;
35*bf2c3715SXin Li   Index n = row.size(); /* length of the vector */
36*bf2c3715SXin Li   Index first, last ;
37*bf2c3715SXin Li 
38*bf2c3715SXin Li   ncut--; /* to fit the zero-based indices */
39*bf2c3715SXin Li   first = 0;
40*bf2c3715SXin Li   last = n-1;
41*bf2c3715SXin Li   if (ncut < first || ncut > last ) return 0;
42*bf2c3715SXin Li 
43*bf2c3715SXin Li   do {
44*bf2c3715SXin Li     mid = first;
45*bf2c3715SXin Li     RealScalar abskey = abs(row(mid));
46*bf2c3715SXin Li     for (Index j = first + 1; j <= last; j++) {
47*bf2c3715SXin Li       if ( abs(row(j)) > abskey) {
48*bf2c3715SXin Li         ++mid;
49*bf2c3715SXin Li         swap(row(mid), row(j));
50*bf2c3715SXin Li         swap(ind(mid), ind(j));
51*bf2c3715SXin Li       }
52*bf2c3715SXin Li     }
53*bf2c3715SXin Li     /* Interchange for the pivot element */
54*bf2c3715SXin Li     swap(row(mid), row(first));
55*bf2c3715SXin Li     swap(ind(mid), ind(first));
56*bf2c3715SXin Li 
57*bf2c3715SXin Li     if (mid > ncut) last = mid - 1;
58*bf2c3715SXin Li     else if (mid < ncut ) first = mid + 1;
59*bf2c3715SXin Li   } while (mid != ncut );
60*bf2c3715SXin Li 
61*bf2c3715SXin Li   return 0; /* mid is equal to ncut */
62*bf2c3715SXin Li }
63*bf2c3715SXin Li 
64*bf2c3715SXin Li }// end namespace internal
65*bf2c3715SXin Li 
66*bf2c3715SXin Li /** \ingroup IterativeLinearSolvers_Module
67*bf2c3715SXin Li   * \class IncompleteLUT
68*bf2c3715SXin Li   * \brief Incomplete LU factorization with dual-threshold strategy
69*bf2c3715SXin Li   *
70*bf2c3715SXin Li   * \implsparsesolverconcept
71*bf2c3715SXin Li   *
72*bf2c3715SXin Li   * During the numerical factorization, two dropping rules are used :
73*bf2c3715SXin Li   *  1) any element whose magnitude is less than some tolerance is dropped.
74*bf2c3715SXin Li   *    This tolerance is obtained by multiplying the input tolerance @p droptol
75*bf2c3715SXin Li   *    by the average magnitude of all the original elements in the current row.
76*bf2c3715SXin Li   *  2) After the elimination of the row, only the @p fill largest elements in
77*bf2c3715SXin Li   *    the L part and the @p fill largest elements in the U part are kept
78*bf2c3715SXin Li   *    (in addition to the diagonal element ). Note that @p fill is computed from
79*bf2c3715SXin Li   *    the input parameter @p fillfactor which is used the ratio to control the fill_in
80*bf2c3715SXin Li   *    relatively to the initial number of nonzero elements.
81*bf2c3715SXin Li   *
82*bf2c3715SXin Li   * The two extreme cases are when @p droptol=0 (to keep all the @p fill*2 largest elements)
83*bf2c3715SXin Li   * and when @p fill=n/2 with @p droptol being different to zero.
84*bf2c3715SXin Li   *
85*bf2c3715SXin Li   * References : Yousef Saad, ILUT: A dual threshold incomplete LU factorization,
86*bf2c3715SXin Li   *              Numerical Linear Algebra with Applications, 1(4), pp 387-402, 1994.
87*bf2c3715SXin Li   *
88*bf2c3715SXin Li   * NOTE : The following implementation is derived from the ILUT implementation
89*bf2c3715SXin Li   * in the SPARSKIT package, Copyright (C) 2005, the Regents of the University of Minnesota
90*bf2c3715SXin Li   *  released under the terms of the GNU LGPL:
91*bf2c3715SXin Li   *    http://www-users.cs.umn.edu/~saad/software/SPARSKIT/README
92*bf2c3715SXin Li   * However, Yousef Saad gave us permission to relicense his ILUT code to MPL2.
93*bf2c3715SXin Li   * See the Eigen mailing list archive, thread: ILUT, date: July 8, 2012:
94*bf2c3715SXin Li   *   http://listengine.tuxfamily.org/lists.tuxfamily.org/eigen/2012/07/msg00064.html
95*bf2c3715SXin Li   * alternatively, on GMANE:
96*bf2c3715SXin Li   *   http://comments.gmane.org/gmane.comp.lib.eigen/3302
97*bf2c3715SXin Li   */
98*bf2c3715SXin Li template <typename _Scalar, typename _StorageIndex = int>
99*bf2c3715SXin Li class IncompleteLUT : public SparseSolverBase<IncompleteLUT<_Scalar, _StorageIndex> >
100*bf2c3715SXin Li {
101*bf2c3715SXin Li   protected:
102*bf2c3715SXin Li     typedef SparseSolverBase<IncompleteLUT> Base;
103*bf2c3715SXin Li     using Base::m_isInitialized;
104*bf2c3715SXin Li   public:
105*bf2c3715SXin Li     typedef _Scalar Scalar;
106*bf2c3715SXin Li     typedef _StorageIndex StorageIndex;
107*bf2c3715SXin Li     typedef typename NumTraits<Scalar>::Real RealScalar;
108*bf2c3715SXin Li     typedef Matrix<Scalar,Dynamic,1> Vector;
109*bf2c3715SXin Li     typedef Matrix<StorageIndex,Dynamic,1> VectorI;
110*bf2c3715SXin Li     typedef SparseMatrix<Scalar,RowMajor,StorageIndex> FactorType;
111*bf2c3715SXin Li 
112*bf2c3715SXin Li     enum {
113*bf2c3715SXin Li       ColsAtCompileTime = Dynamic,
114*bf2c3715SXin Li       MaxColsAtCompileTime = Dynamic
115*bf2c3715SXin Li     };
116*bf2c3715SXin Li 
117*bf2c3715SXin Li   public:
118*bf2c3715SXin Li 
IncompleteLUT()119*bf2c3715SXin Li     IncompleteLUT()
120*bf2c3715SXin Li       : m_droptol(NumTraits<Scalar>::dummy_precision()), m_fillfactor(10),
121*bf2c3715SXin Li         m_analysisIsOk(false), m_factorizationIsOk(false)
122*bf2c3715SXin Li     {}
123*bf2c3715SXin Li 
124*bf2c3715SXin Li     template<typename MatrixType>
125*bf2c3715SXin Li     explicit IncompleteLUT(const MatrixType& mat, const RealScalar& droptol=NumTraits<Scalar>::dummy_precision(), int fillfactor = 10)
m_droptol(droptol)126*bf2c3715SXin Li       : m_droptol(droptol),m_fillfactor(fillfactor),
127*bf2c3715SXin Li         m_analysisIsOk(false),m_factorizationIsOk(false)
128*bf2c3715SXin Li     {
129*bf2c3715SXin Li       eigen_assert(fillfactor != 0);
130*bf2c3715SXin Li       compute(mat);
131*bf2c3715SXin Li     }
132*bf2c3715SXin Li 
rows()133*bf2c3715SXin Li     EIGEN_CONSTEXPR Index rows() const EIGEN_NOEXCEPT { return m_lu.rows(); }
134*bf2c3715SXin Li 
cols()135*bf2c3715SXin Li     EIGEN_CONSTEXPR Index cols() const EIGEN_NOEXCEPT { return m_lu.cols(); }
136*bf2c3715SXin Li 
137*bf2c3715SXin Li     /** \brief Reports whether previous computation was successful.
138*bf2c3715SXin Li       *
139*bf2c3715SXin Li       * \returns \c Success if computation was successful,
140*bf2c3715SXin Li       *          \c NumericalIssue if the matrix.appears to be negative.
141*bf2c3715SXin Li       */
info()142*bf2c3715SXin Li     ComputationInfo info() const
143*bf2c3715SXin Li     {
144*bf2c3715SXin Li       eigen_assert(m_isInitialized && "IncompleteLUT is not initialized.");
145*bf2c3715SXin Li       return m_info;
146*bf2c3715SXin Li     }
147*bf2c3715SXin Li 
148*bf2c3715SXin Li     template<typename MatrixType>
149*bf2c3715SXin Li     void analyzePattern(const MatrixType& amat);
150*bf2c3715SXin Li 
151*bf2c3715SXin Li     template<typename MatrixType>
152*bf2c3715SXin Li     void factorize(const MatrixType& amat);
153*bf2c3715SXin Li 
154*bf2c3715SXin Li     /**
155*bf2c3715SXin Li       * Compute an incomplete LU factorization with dual threshold on the matrix mat
156*bf2c3715SXin Li       * No pivoting is done in this version
157*bf2c3715SXin Li       *
158*bf2c3715SXin Li       **/
159*bf2c3715SXin Li     template<typename MatrixType>
compute(const MatrixType & amat)160*bf2c3715SXin Li     IncompleteLUT& compute(const MatrixType& amat)
161*bf2c3715SXin Li     {
162*bf2c3715SXin Li       analyzePattern(amat);
163*bf2c3715SXin Li       factorize(amat);
164*bf2c3715SXin Li       return *this;
165*bf2c3715SXin Li     }
166*bf2c3715SXin Li 
167*bf2c3715SXin Li     void setDroptol(const RealScalar& droptol);
168*bf2c3715SXin Li     void setFillfactor(int fillfactor);
169*bf2c3715SXin Li 
170*bf2c3715SXin Li     template<typename Rhs, typename Dest>
_solve_impl(const Rhs & b,Dest & x)171*bf2c3715SXin Li     void _solve_impl(const Rhs& b, Dest& x) const
172*bf2c3715SXin Li     {
173*bf2c3715SXin Li       x = m_Pinv * b;
174*bf2c3715SXin Li       x = m_lu.template triangularView<UnitLower>().solve(x);
175*bf2c3715SXin Li       x = m_lu.template triangularView<Upper>().solve(x);
176*bf2c3715SXin Li       x = m_P * x;
177*bf2c3715SXin Li     }
178*bf2c3715SXin Li 
179*bf2c3715SXin Li protected:
180*bf2c3715SXin Li 
181*bf2c3715SXin Li     /** keeps off-diagonal entries; drops diagonal entries */
182*bf2c3715SXin Li     struct keep_diag {
operatorkeep_diag183*bf2c3715SXin Li       inline bool operator() (const Index& row, const Index& col, const Scalar&) const
184*bf2c3715SXin Li       {
185*bf2c3715SXin Li         return row!=col;
186*bf2c3715SXin Li       }
187*bf2c3715SXin Li     };
188*bf2c3715SXin Li 
189*bf2c3715SXin Li protected:
190*bf2c3715SXin Li 
191*bf2c3715SXin Li     FactorType m_lu;
192*bf2c3715SXin Li     RealScalar m_droptol;
193*bf2c3715SXin Li     int m_fillfactor;
194*bf2c3715SXin Li     bool m_analysisIsOk;
195*bf2c3715SXin Li     bool m_factorizationIsOk;
196*bf2c3715SXin Li     ComputationInfo m_info;
197*bf2c3715SXin Li     PermutationMatrix<Dynamic,Dynamic,StorageIndex> m_P;     // Fill-reducing permutation
198*bf2c3715SXin Li     PermutationMatrix<Dynamic,Dynamic,StorageIndex> m_Pinv;  // Inverse permutation
199*bf2c3715SXin Li };
200*bf2c3715SXin Li 
201*bf2c3715SXin Li /**
202*bf2c3715SXin Li  * Set control parameter droptol
203*bf2c3715SXin Li  *  \param droptol   Drop any element whose magnitude is less than this tolerance
204*bf2c3715SXin Li  **/
205*bf2c3715SXin Li template<typename Scalar, typename StorageIndex>
setDroptol(const RealScalar & droptol)206*bf2c3715SXin Li void IncompleteLUT<Scalar,StorageIndex>::setDroptol(const RealScalar& droptol)
207*bf2c3715SXin Li {
208*bf2c3715SXin Li   this->m_droptol = droptol;
209*bf2c3715SXin Li }
210*bf2c3715SXin Li 
211*bf2c3715SXin Li /**
212*bf2c3715SXin Li  * Set control parameter fillfactor
213*bf2c3715SXin Li  * \param fillfactor  This is used to compute the  number @p fill_in of largest elements to keep on each row.
214*bf2c3715SXin Li  **/
215*bf2c3715SXin Li template<typename Scalar, typename StorageIndex>
setFillfactor(int fillfactor)216*bf2c3715SXin Li void IncompleteLUT<Scalar,StorageIndex>::setFillfactor(int fillfactor)
217*bf2c3715SXin Li {
218*bf2c3715SXin Li   this->m_fillfactor = fillfactor;
219*bf2c3715SXin Li }
220*bf2c3715SXin Li 
221*bf2c3715SXin Li template <typename Scalar, typename StorageIndex>
222*bf2c3715SXin Li template<typename _MatrixType>
analyzePattern(const _MatrixType & amat)223*bf2c3715SXin Li void IncompleteLUT<Scalar,StorageIndex>::analyzePattern(const _MatrixType& amat)
224*bf2c3715SXin Li {
225*bf2c3715SXin Li   // Compute the Fill-reducing permutation
226*bf2c3715SXin Li   // Since ILUT does not perform any numerical pivoting,
227*bf2c3715SXin Li   // it is highly preferable to keep the diagonal through symmetric permutations.
228*bf2c3715SXin Li   // To this end, let's symmetrize the pattern and perform AMD on it.
229*bf2c3715SXin Li   SparseMatrix<Scalar,ColMajor, StorageIndex> mat1 = amat;
230*bf2c3715SXin Li   SparseMatrix<Scalar,ColMajor, StorageIndex> mat2 = amat.transpose();
231*bf2c3715SXin Li   // FIXME for a matrix with nearly symmetric pattern, mat2+mat1 is the appropriate choice.
232*bf2c3715SXin Li   //       on the other hand for a really non-symmetric pattern, mat2*mat1 should be preferred...
233*bf2c3715SXin Li   SparseMatrix<Scalar,ColMajor, StorageIndex> AtA = mat2 + mat1;
234*bf2c3715SXin Li   AMDOrdering<StorageIndex> ordering;
235*bf2c3715SXin Li   ordering(AtA,m_P);
236*bf2c3715SXin Li   m_Pinv  = m_P.inverse(); // cache the inverse permutation
237*bf2c3715SXin Li   m_analysisIsOk = true;
238*bf2c3715SXin Li   m_factorizationIsOk = false;
239*bf2c3715SXin Li   m_isInitialized = true;
240*bf2c3715SXin Li }
241*bf2c3715SXin Li 
242*bf2c3715SXin Li template <typename Scalar, typename StorageIndex>
243*bf2c3715SXin Li template<typename _MatrixType>
factorize(const _MatrixType & amat)244*bf2c3715SXin Li void IncompleteLUT<Scalar,StorageIndex>::factorize(const _MatrixType& amat)
245*bf2c3715SXin Li {
246*bf2c3715SXin Li   using std::sqrt;
247*bf2c3715SXin Li   using std::swap;
248*bf2c3715SXin Li   using std::abs;
249*bf2c3715SXin Li   using internal::convert_index;
250*bf2c3715SXin Li 
251*bf2c3715SXin Li   eigen_assert((amat.rows() == amat.cols()) && "The factorization should be done on a square matrix");
252*bf2c3715SXin Li   Index n = amat.cols();  // Size of the matrix
253*bf2c3715SXin Li   m_lu.resize(n,n);
254*bf2c3715SXin Li   // Declare Working vectors and variables
255*bf2c3715SXin Li   Vector u(n) ;     // real values of the row -- maximum size is n --
256*bf2c3715SXin Li   VectorI ju(n);   // column position of the values in u -- maximum size  is n
257*bf2c3715SXin Li   VectorI jr(n);   // Indicate the position of the nonzero elements in the vector u -- A zero location is indicated by -1
258*bf2c3715SXin Li 
259*bf2c3715SXin Li   // Apply the fill-reducing permutation
260*bf2c3715SXin Li   eigen_assert(m_analysisIsOk && "You must first call analyzePattern()");
261*bf2c3715SXin Li   SparseMatrix<Scalar,RowMajor, StorageIndex> mat;
262*bf2c3715SXin Li   mat = amat.twistedBy(m_Pinv);
263*bf2c3715SXin Li 
264*bf2c3715SXin Li   // Initialization
265*bf2c3715SXin Li   jr.fill(-1);
266*bf2c3715SXin Li   ju.fill(0);
267*bf2c3715SXin Li   u.fill(0);
268*bf2c3715SXin Li 
269*bf2c3715SXin Li   // number of largest elements to keep in each row:
270*bf2c3715SXin Li   Index fill_in = (amat.nonZeros()*m_fillfactor)/n + 1;
271*bf2c3715SXin Li   if (fill_in > n) fill_in = n;
272*bf2c3715SXin Li 
273*bf2c3715SXin Li   // number of largest nonzero elements to keep in the L and the U part of the current row:
274*bf2c3715SXin Li   Index nnzL = fill_in/2;
275*bf2c3715SXin Li   Index nnzU = nnzL;
276*bf2c3715SXin Li   m_lu.reserve(n * (nnzL + nnzU + 1));
277*bf2c3715SXin Li 
278*bf2c3715SXin Li   // global loop over the rows of the sparse matrix
279*bf2c3715SXin Li   for (Index ii = 0; ii < n; ii++)
280*bf2c3715SXin Li   {
281*bf2c3715SXin Li     // 1 - copy the lower and the upper part of the row i of mat in the working vector u
282*bf2c3715SXin Li 
283*bf2c3715SXin Li     Index sizeu = 1; // number of nonzero elements in the upper part of the current row
284*bf2c3715SXin Li     Index sizel = 0; // number of nonzero elements in the lower part of the current row
285*bf2c3715SXin Li     ju(ii)    = convert_index<StorageIndex>(ii);
286*bf2c3715SXin Li     u(ii)     = 0;
287*bf2c3715SXin Li     jr(ii)    = convert_index<StorageIndex>(ii);
288*bf2c3715SXin Li     RealScalar rownorm = 0;
289*bf2c3715SXin Li 
290*bf2c3715SXin Li     typename FactorType::InnerIterator j_it(mat, ii); // Iterate through the current row ii
291*bf2c3715SXin Li     for (; j_it; ++j_it)
292*bf2c3715SXin Li     {
293*bf2c3715SXin Li       Index k = j_it.index();
294*bf2c3715SXin Li       if (k < ii)
295*bf2c3715SXin Li       {
296*bf2c3715SXin Li         // copy the lower part
297*bf2c3715SXin Li         ju(sizel) = convert_index<StorageIndex>(k);
298*bf2c3715SXin Li         u(sizel) = j_it.value();
299*bf2c3715SXin Li         jr(k) = convert_index<StorageIndex>(sizel);
300*bf2c3715SXin Li         ++sizel;
301*bf2c3715SXin Li       }
302*bf2c3715SXin Li       else if (k == ii)
303*bf2c3715SXin Li       {
304*bf2c3715SXin Li         u(ii) = j_it.value();
305*bf2c3715SXin Li       }
306*bf2c3715SXin Li       else
307*bf2c3715SXin Li       {
308*bf2c3715SXin Li         // copy the upper part
309*bf2c3715SXin Li         Index jpos = ii + sizeu;
310*bf2c3715SXin Li         ju(jpos) = convert_index<StorageIndex>(k);
311*bf2c3715SXin Li         u(jpos) = j_it.value();
312*bf2c3715SXin Li         jr(k) = convert_index<StorageIndex>(jpos);
313*bf2c3715SXin Li         ++sizeu;
314*bf2c3715SXin Li       }
315*bf2c3715SXin Li       rownorm += numext::abs2(j_it.value());
316*bf2c3715SXin Li     }
317*bf2c3715SXin Li 
318*bf2c3715SXin Li     // 2 - detect possible zero row
319*bf2c3715SXin Li     if(rownorm==0)
320*bf2c3715SXin Li     {
321*bf2c3715SXin Li       m_info = NumericalIssue;
322*bf2c3715SXin Li       return;
323*bf2c3715SXin Li     }
324*bf2c3715SXin Li     // Take the 2-norm of the current row as a relative tolerance
325*bf2c3715SXin Li     rownorm = sqrt(rownorm);
326*bf2c3715SXin Li 
327*bf2c3715SXin Li     // 3 - eliminate the previous nonzero rows
328*bf2c3715SXin Li     Index jj = 0;
329*bf2c3715SXin Li     Index len = 0;
330*bf2c3715SXin Li     while (jj < sizel)
331*bf2c3715SXin Li     {
332*bf2c3715SXin Li       // In order to eliminate in the correct order,
333*bf2c3715SXin Li       // we must select first the smallest column index among  ju(jj:sizel)
334*bf2c3715SXin Li       Index k;
335*bf2c3715SXin Li       Index minrow = ju.segment(jj,sizel-jj).minCoeff(&k); // k is relative to the segment
336*bf2c3715SXin Li       k += jj;
337*bf2c3715SXin Li       if (minrow != ju(jj))
338*bf2c3715SXin Li       {
339*bf2c3715SXin Li         // swap the two locations
340*bf2c3715SXin Li         Index j = ju(jj);
341*bf2c3715SXin Li         swap(ju(jj), ju(k));
342*bf2c3715SXin Li         jr(minrow) = convert_index<StorageIndex>(jj);
343*bf2c3715SXin Li         jr(j) = convert_index<StorageIndex>(k);
344*bf2c3715SXin Li         swap(u(jj), u(k));
345*bf2c3715SXin Li       }
346*bf2c3715SXin Li       // Reset this location
347*bf2c3715SXin Li       jr(minrow) = -1;
348*bf2c3715SXin Li 
349*bf2c3715SXin Li       // Start elimination
350*bf2c3715SXin Li       typename FactorType::InnerIterator ki_it(m_lu, minrow);
351*bf2c3715SXin Li       while (ki_it && ki_it.index() < minrow) ++ki_it;
352*bf2c3715SXin Li       eigen_internal_assert(ki_it && ki_it.col()==minrow);
353*bf2c3715SXin Li       Scalar fact = u(jj) / ki_it.value();
354*bf2c3715SXin Li 
355*bf2c3715SXin Li       // drop too small elements
356*bf2c3715SXin Li       if(abs(fact) <= m_droptol)
357*bf2c3715SXin Li       {
358*bf2c3715SXin Li         jj++;
359*bf2c3715SXin Li         continue;
360*bf2c3715SXin Li       }
361*bf2c3715SXin Li 
362*bf2c3715SXin Li       // linear combination of the current row ii and the row minrow
363*bf2c3715SXin Li       ++ki_it;
364*bf2c3715SXin Li       for (; ki_it; ++ki_it)
365*bf2c3715SXin Li       {
366*bf2c3715SXin Li         Scalar prod = fact * ki_it.value();
367*bf2c3715SXin Li         Index j     = ki_it.index();
368*bf2c3715SXin Li         Index jpos  = jr(j);
369*bf2c3715SXin Li         if (jpos == -1) // fill-in element
370*bf2c3715SXin Li         {
371*bf2c3715SXin Li           Index newpos;
372*bf2c3715SXin Li           if (j >= ii) // dealing with the upper part
373*bf2c3715SXin Li           {
374*bf2c3715SXin Li             newpos = ii + sizeu;
375*bf2c3715SXin Li             sizeu++;
376*bf2c3715SXin Li             eigen_internal_assert(sizeu<=n);
377*bf2c3715SXin Li           }
378*bf2c3715SXin Li           else // dealing with the lower part
379*bf2c3715SXin Li           {
380*bf2c3715SXin Li             newpos = sizel;
381*bf2c3715SXin Li             sizel++;
382*bf2c3715SXin Li             eigen_internal_assert(sizel<=ii);
383*bf2c3715SXin Li           }
384*bf2c3715SXin Li           ju(newpos) = convert_index<StorageIndex>(j);
385*bf2c3715SXin Li           u(newpos) = -prod;
386*bf2c3715SXin Li           jr(j) = convert_index<StorageIndex>(newpos);
387*bf2c3715SXin Li         }
388*bf2c3715SXin Li         else
389*bf2c3715SXin Li           u(jpos) -= prod;
390*bf2c3715SXin Li       }
391*bf2c3715SXin Li       // store the pivot element
392*bf2c3715SXin Li       u(len)  = fact;
393*bf2c3715SXin Li       ju(len) = convert_index<StorageIndex>(minrow);
394*bf2c3715SXin Li       ++len;
395*bf2c3715SXin Li 
396*bf2c3715SXin Li       jj++;
397*bf2c3715SXin Li     } // end of the elimination on the row ii
398*bf2c3715SXin Li 
399*bf2c3715SXin Li     // reset the upper part of the pointer jr to zero
400*bf2c3715SXin Li     for(Index k = 0; k <sizeu; k++) jr(ju(ii+k)) = -1;
401*bf2c3715SXin Li 
402*bf2c3715SXin Li     // 4 - partially sort and insert the elements in the m_lu matrix
403*bf2c3715SXin Li 
404*bf2c3715SXin Li     // sort the L-part of the row
405*bf2c3715SXin Li     sizel = len;
406*bf2c3715SXin Li     len = (std::min)(sizel, nnzL);
407*bf2c3715SXin Li     typename Vector::SegmentReturnType ul(u.segment(0, sizel));
408*bf2c3715SXin Li     typename VectorI::SegmentReturnType jul(ju.segment(0, sizel));
409*bf2c3715SXin Li     internal::QuickSplit(ul, jul, len);
410*bf2c3715SXin Li 
411*bf2c3715SXin Li     // store the largest m_fill elements of the L part
412*bf2c3715SXin Li     m_lu.startVec(ii);
413*bf2c3715SXin Li     for(Index k = 0; k < len; k++)
414*bf2c3715SXin Li       m_lu.insertBackByOuterInnerUnordered(ii,ju(k)) = u(k);
415*bf2c3715SXin Li 
416*bf2c3715SXin Li     // store the diagonal element
417*bf2c3715SXin Li     // apply a shifting rule to avoid zero pivots (we are doing an incomplete factorization)
418*bf2c3715SXin Li     if (u(ii) == Scalar(0))
419*bf2c3715SXin Li       u(ii) = sqrt(m_droptol) * rownorm;
420*bf2c3715SXin Li     m_lu.insertBackByOuterInnerUnordered(ii, ii) = u(ii);
421*bf2c3715SXin Li 
422*bf2c3715SXin Li     // sort the U-part of the row
423*bf2c3715SXin Li     // apply the dropping rule first
424*bf2c3715SXin Li     len = 0;
425*bf2c3715SXin Li     for(Index k = 1; k < sizeu; k++)
426*bf2c3715SXin Li     {
427*bf2c3715SXin Li       if(abs(u(ii+k)) > m_droptol * rownorm )
428*bf2c3715SXin Li       {
429*bf2c3715SXin Li         ++len;
430*bf2c3715SXin Li         u(ii + len)  = u(ii + k);
431*bf2c3715SXin Li         ju(ii + len) = ju(ii + k);
432*bf2c3715SXin Li       }
433*bf2c3715SXin Li     }
434*bf2c3715SXin Li     sizeu = len + 1; // +1 to take into account the diagonal element
435*bf2c3715SXin Li     len = (std::min)(sizeu, nnzU);
436*bf2c3715SXin Li     typename Vector::SegmentReturnType uu(u.segment(ii+1, sizeu-1));
437*bf2c3715SXin Li     typename VectorI::SegmentReturnType juu(ju.segment(ii+1, sizeu-1));
438*bf2c3715SXin Li     internal::QuickSplit(uu, juu, len);
439*bf2c3715SXin Li 
440*bf2c3715SXin Li     // store the largest elements of the U part
441*bf2c3715SXin Li     for(Index k = ii + 1; k < ii + len; k++)
442*bf2c3715SXin Li       m_lu.insertBackByOuterInnerUnordered(ii,ju(k)) = u(k);
443*bf2c3715SXin Li   }
444*bf2c3715SXin Li   m_lu.finalize();
445*bf2c3715SXin Li   m_lu.makeCompressed();
446*bf2c3715SXin Li 
447*bf2c3715SXin Li   m_factorizationIsOk = true;
448*bf2c3715SXin Li   m_info = Success;
449*bf2c3715SXin Li }
450*bf2c3715SXin Li 
451*bf2c3715SXin Li } // end namespace Eigen
452*bf2c3715SXin Li 
453*bf2c3715SXin Li #endif // EIGEN_INCOMPLETE_LUT_H
454