SparseLU_pivotL.h 4.9 KB

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