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 //
6 // This Source Code Form is subject to the terms of the Mozilla
7 // Public License v. 2.0. If a copy of the MPL was not distributed
8 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 
10 /*
11 
12  * NOTE: This file is the modified version of xpivotL.c file in SuperLU
13 
14  * -- SuperLU routine (version 3.0) --
15  * Univ. of California Berkeley, Xerox Palo Alto Research Center,
16  * and Lawrence Berkeley National Lab.
17  * October 15, 2003
18  *
19  * Copyright (c) 1994 by Xerox Corporation.  All rights reserved.
20  *
21  * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY
22  * EXPRESSED OR IMPLIED.  ANY USE IS AT YOUR OWN RISK.
23  *
24  * Permission is hereby granted to use or copy this program for any
25  * purpose, provided the above notices are retained on all copies.
26  * Permission to modify the code and to distribute modified code is
27  * granted, provided the above notices are retained, and a notice that
28  * the code was modified is included with the above copyright notice.
29  */
30 #ifndef SPARSELU_PIVOTL_H
31 #define SPARSELU_PIVOTL_H
32 
33 namespace Eigen {
34 namespace internal {
35 
36 /**
37  * \brief Performs the numerical pivotin on the current column of L, and the CDIV operation.
38  *
39  * Pivot policy :
40  * (1) Compute thresh = u * max_(i>=j) abs(A_ij);
41  * (2) IF user specifies pivot row k and abs(A_kj) >= thresh THEN
42  *           pivot row = k;
43  *       ELSE IF abs(A_jj) >= thresh THEN
44  *           pivot row = j;
45  *       ELSE
46  *           pivot row = m;
47  *
48  *   Note: If you absolutely want to use a given pivot order, then set u=0.0.
49  *
50  * \param jcol The current column of L
51  * \param diagpivotthresh diagonal pivoting threshold
52  * \param[in,out] perm_r Row permutation (threshold pivoting)
53  * \param[in] iperm_c column permutation - used to finf diagonal of Pc*A*Pc'
54  * \param[out] pivrow  The pivot row
55  * \param glu Global LU data
56  * \return 0 if success, i > 0 if U(i,i) is exactly zero
57  *
58  */
59 template <typename Scalar, typename Index>
pivotL(const Index jcol,const RealScalar & diagpivotthresh,IndexVector & perm_r,IndexVector & iperm_c,Index & pivrow,GlobalLU_t & glu)60 Index SparseLUImpl<Scalar,Index>::pivotL(const Index jcol, const RealScalar& diagpivotthresh, IndexVector& perm_r, IndexVector& iperm_c, Index& pivrow, GlobalLU_t& glu)
61 {
62 
63   Index fsupc = (glu.xsup)((glu.supno)(jcol)); // First column in the supernode containing the column jcol
64   Index nsupc = jcol - fsupc; // Number of columns in the supernode portion, excluding jcol; nsupc >=0
65   Index lptr = glu.xlsub(fsupc); // pointer to the starting location of the row subscripts for this supernode portion
66   Index nsupr = glu.xlsub(fsupc+1) - lptr; // Number of rows in the supernode
67   Index lda = glu.xlusup(fsupc+1) - glu.xlusup(fsupc); // leading dimension
68   Scalar* lu_sup_ptr = &(glu.lusup.data()[glu.xlusup(fsupc)]); // Start of the current supernode
69   Scalar* lu_col_ptr = &(glu.lusup.data()[glu.xlusup(jcol)]); // Start of jcol in the supernode
70   Index* lsub_ptr = &(glu.lsub.data()[lptr]); // Start of row indices of the supernode
71 
72   // Determine the largest abs numerical value for partial pivoting
73   Index diagind = iperm_c(jcol); // diagonal index
74   RealScalar pivmax = 0.0;
75   Index pivptr = nsupc;
76   Index diag = emptyIdxLU;
77   RealScalar rtemp;
78   Index isub, icol, itemp, k;
79   for (isub = nsupc; isub < nsupr; ++isub) {
80     using std::abs;
81     rtemp = abs(lu_col_ptr[isub]);
82     if (rtemp > pivmax) {
83       pivmax = rtemp;
84       pivptr = isub;
85     }
86     if (lsub_ptr[isub] == diagind) diag = isub;
87   }
88 
89   // Test for singularity
90   if ( pivmax == 0.0 ) {
91     pivrow = lsub_ptr[pivptr];
92     perm_r(pivrow) = jcol;
93     return (jcol+1);
94   }
95 
96   RealScalar thresh = diagpivotthresh * pivmax;
97 
98   // Choose appropriate pivotal element
99 
100   {
101     // Test if the diagonal element can be used as a pivot (given the threshold value)
102     if (diag >= 0 )
103     {
104       // Diagonal element exists
105       using std::abs;
106       rtemp = abs(lu_col_ptr[diag]);
107       if (rtemp != 0.0 && rtemp >= thresh) pivptr = diag;
108     }
109     pivrow = lsub_ptr[pivptr];
110   }
111 
112   // Record pivot row
113   perm_r(pivrow) = jcol;
114   // Interchange row subscripts
115   if (pivptr != nsupc )
116   {
117     std::swap( lsub_ptr[pivptr], lsub_ptr[nsupc] );
118     // Interchange numerical values as well, for the two rows in the whole snode
119     // such that L is indexed the same way as A
120     for (icol = 0; icol <= nsupc; icol++)
121     {
122       itemp = pivptr + icol * lda;
123       std::swap(lu_sup_ptr[itemp], lu_sup_ptr[nsupc + icol * lda]);
124     }
125   }
126   // cdiv operations
127   Scalar temp = Scalar(1.0) / lu_col_ptr[nsupc];
128   for (k = nsupc+1; k < nsupr; k++)
129     lu_col_ptr[k] *= temp;
130   return 0;
131 }
132 
133 } // end namespace internal
134 } // end namespace Eigen
135 
136 #endif // SPARSELU_PIVOTL_H
137