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     rtemp = std::abs(lu_col_ptr[isub]);
81     if (rtemp > pivmax) {
82       pivmax = rtemp;
83       pivptr = isub;
84     }
85     if (lsub_ptr[isub] == diagind) diag = isub;
86   }
87 
88   // Test for singularity
89   if ( pivmax == 0.0 ) {
90     pivrow = lsub_ptr[pivptr];
91     perm_r(pivrow) = jcol;
92     return (jcol+1);
93   }
94 
95   RealScalar thresh = diagpivotthresh * pivmax;
96 
97   // Choose appropriate pivotal element
98 
99   {
100     // Test if the diagonal element can be used as a pivot (given the threshold value)
101     if (diag >= 0 )
102     {
103       // Diagonal element exists
104       rtemp = std::abs(lu_col_ptr[diag]);
105       if (rtemp != 0.0 && rtemp >= thresh) pivptr = diag;
106     }
107     pivrow = lsub_ptr[pivptr];
108   }
109 
110   // Record pivot row
111   perm_r(pivrow) = jcol;
112   // Interchange row subscripts
113   if (pivptr != nsupc )
114   {
115     std::swap( lsub_ptr[pivptr], lsub_ptr[nsupc] );
116     // Interchange numerical values as well, for the two rows in the whole snode
117     // such that L is indexed the same way as A
118     for (icol = 0; icol <= nsupc; icol++)
119     {
120       itemp = pivptr + icol * lda;
121       std::swap(lu_sup_ptr[itemp], lu_sup_ptr[nsupc + icol * lda]);
122     }
123   }
124   // cdiv operations
125   Scalar temp = Scalar(1.0) / lu_col_ptr[nsupc];
126   for (k = nsupc+1; k < nsupr; k++)
127     lu_col_ptr[k] *= temp;
128   return 0;
129 }
130 
131 } // end namespace internal
132 } // end namespace Eigen
133 
134 #endif // SPARSELU_PIVOTL_H
135