Eigen  3.4.90 (git rev 67eeba6e720c5745abc77ae6c92ce0a44aa7b7ae)
SparseLU.h
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) 2012-2014 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 
11 
12 #ifndef EIGEN_SPARSE_LU_H
13 #define EIGEN_SPARSE_LU_H
14 
15 #include "./InternalHeaderCheck.h"
16 
17 namespace Eigen {
18 
19 template <typename MatrixType_, typename OrderingType_ = COLAMDOrdering<typename MatrixType_::StorageIndex> > class SparseLU;
20 template <typename MappedSparseMatrixType> struct SparseLUMatrixLReturnType;
21 template <typename MatrixLType, typename MatrixUType> struct SparseLUMatrixUReturnType;
22 
23 template <bool Conjugate,class SparseLUType>
24 class SparseLUTransposeView : public SparseSolverBase<SparseLUTransposeView<Conjugate,SparseLUType> >
25 {
26 protected:
27  typedef SparseSolverBase<SparseLUTransposeView<Conjugate,SparseLUType> > APIBase;
28  using APIBase::m_isInitialized;
29 public:
30  typedef typename SparseLUType::Scalar Scalar;
31  typedef typename SparseLUType::StorageIndex StorageIndex;
32  typedef typename SparseLUType::MatrixType MatrixType;
33  typedef typename SparseLUType::OrderingType OrderingType;
34 
35  enum {
36  ColsAtCompileTime = MatrixType::ColsAtCompileTime,
37  MaxColsAtCompileTime = MatrixType::MaxColsAtCompileTime
38  };
39 
40  SparseLUTransposeView() : m_sparseLU(NULL) {}
41  SparseLUTransposeView(const SparseLUTransposeView& view) {
42  this->m_sparseLU = view.m_sparseLU;
43  }
44  void setIsInitialized(const bool isInitialized) {this->m_isInitialized = isInitialized;}
45  void setSparseLU(SparseLUType* sparseLU) {m_sparseLU = sparseLU;}
46  using APIBase::_solve_impl;
47  template<typename Rhs, typename Dest>
48  bool _solve_impl(const MatrixBase<Rhs> &B, MatrixBase<Dest> &X_base) const
49  {
50  Dest& X(X_base.derived());
51  eigen_assert(m_sparseLU->info() == Success && "The matrix should be factorized first");
52  EIGEN_STATIC_ASSERT((Dest::Flags&RowMajorBit)==0,
53  THIS_METHOD_IS_ONLY_FOR_COLUMN_MAJOR_MATRICES);
54 
55 
56  // this ugly const_cast_derived() helps to detect aliasing when applying the permutations
57  for(Index j = 0; j < B.cols(); ++j){
58  X.col(j) = m_sparseLU->colsPermutation() * B.const_cast_derived().col(j);
59  }
60  //Forward substitution with transposed or adjoint of U
61  m_sparseLU->matrixU().template solveTransposedInPlace<Conjugate>(X);
62 
63  //Backward substitution with transposed or adjoint of L
64  m_sparseLU->matrixL().template solveTransposedInPlace<Conjugate>(X);
65 
66  // Permute back the solution
67  for (Index j = 0; j < B.cols(); ++j)
68  X.col(j) = m_sparseLU->rowsPermutation().transpose() * X.col(j);
69  return true;
70  }
71  inline Index rows() const { return m_sparseLU->rows(); }
72  inline Index cols() const { return m_sparseLU->cols(); }
73 
74 private:
75  SparseLUType *m_sparseLU;
76  SparseLUTransposeView& operator=(const SparseLUTransposeView&);
77 };
78 
79 
132 template <typename MatrixType_, typename OrderingType_>
133 class SparseLU : public SparseSolverBase<SparseLU<MatrixType_,OrderingType_> >, public internal::SparseLUImpl<typename MatrixType_::Scalar, typename MatrixType_::StorageIndex>
134 {
135  protected:
137  using APIBase::m_isInitialized;
138  public:
139  using APIBase::_solve_impl;
140 
141  typedef MatrixType_ MatrixType;
142  typedef OrderingType_ OrderingType;
143  typedef typename MatrixType::Scalar Scalar;
144  typedef typename MatrixType::RealScalar RealScalar;
145  typedef typename MatrixType::StorageIndex StorageIndex;
147  typedef internal::MappedSuperNodalMatrix<Scalar, StorageIndex> SCMatrix;
151  typedef internal::SparseLUImpl<Scalar, StorageIndex> Base;
152 
153  enum {
154  ColsAtCompileTime = MatrixType::ColsAtCompileTime,
155  MaxColsAtCompileTime = MatrixType::MaxColsAtCompileTime
156  };
157 
158  public:
159 
160  SparseLU():m_lastError(""),m_Ustore(0,0,0,0,0,0),m_symmetricmode(false),m_diagpivotthresh(1.0),m_detPermR(1)
161  {
162  initperfvalues();
163  }
164  explicit SparseLU(const MatrixType& matrix)
165  : m_lastError(""),m_Ustore(0,0,0,0,0,0),m_symmetricmode(false),m_diagpivotthresh(1.0),m_detPermR(1)
166  {
167  initperfvalues();
168  compute(matrix);
169  }
170 
171  ~SparseLU()
172  {
173  // Free all explicit dynamic pointers
174  }
175 
176  void analyzePattern (const MatrixType& matrix);
177  void factorize (const MatrixType& matrix);
178  void simplicialfactorize(const MatrixType& matrix);
179 
184  void compute (const MatrixType& matrix)
185  {
186  // Analyze
187  analyzePattern(matrix);
188  //Factorize
189  factorize(matrix);
190  }
191 
202  const SparseLUTransposeView<false,SparseLU<MatrixType_,OrderingType_> > transpose()
203  {
204  SparseLUTransposeView<false, SparseLU<MatrixType_,OrderingType_> > transposeView;
205  transposeView.setSparseLU(this);
206  transposeView.setIsInitialized(this->m_isInitialized);
207  return transposeView;
208  }
209 
210 
223  const SparseLUTransposeView<true, SparseLU<MatrixType_,OrderingType_> > adjoint()
224  {
225  SparseLUTransposeView<true, SparseLU<MatrixType_,OrderingType_> > adjointView;
226  adjointView.setSparseLU(this);
227  adjointView.setIsInitialized(this->m_isInitialized);
228  return adjointView;
229  }
230 
231  inline Index rows() const { return m_mat.rows(); }
232  inline Index cols() const { return m_mat.cols(); }
234  void isSymmetric(bool sym)
235  {
236  m_symmetricmode = sym;
237  }
238 
245  SparseLUMatrixLReturnType<SCMatrix> matrixL() const
246  {
247  return SparseLUMatrixLReturnType<SCMatrix>(m_Lstore);
248  }
255  SparseLUMatrixUReturnType<SCMatrix,Map<SparseMatrix<Scalar,ColMajor,StorageIndex> > > matrixU() const
256  {
257  return SparseLUMatrixUReturnType<SCMatrix, Map<SparseMatrix<Scalar,ColMajor,StorageIndex> > >(m_Lstore, m_Ustore);
258  }
259 
264  inline const PermutationType& rowsPermutation() const
265  {
266  return m_perm_r;
267  }
272  inline const PermutationType& colsPermutation() const
273  {
274  return m_perm_c;
275  }
277  void setPivotThreshold(const RealScalar& thresh)
278  {
279  m_diagpivotthresh = thresh;
280  }
281 
282 #ifdef EIGEN_PARSED_BY_DOXYGEN
289  template<typename Rhs>
290  inline const Solve<SparseLU, Rhs> solve(const MatrixBase<Rhs>& B) const;
291 #endif // EIGEN_PARSED_BY_DOXYGEN
292 
302  {
303  eigen_assert(m_isInitialized && "Decomposition is not initialized.");
304  return m_info;
305  }
306 
310  std::string lastErrorMessage() const
311  {
312  return m_lastError;
313  }
314 
315  template<typename Rhs, typename Dest>
316  bool _solve_impl(const MatrixBase<Rhs> &B, MatrixBase<Dest> &X_base) const
317  {
318  Dest& X(X_base.derived());
319  eigen_assert(m_factorizationIsOk && "The matrix should be factorized first");
320  EIGEN_STATIC_ASSERT((Dest::Flags&RowMajorBit)==0,
321  THIS_METHOD_IS_ONLY_FOR_COLUMN_MAJOR_MATRICES);
322 
323  // Permute the right hand side to form X = Pr*B
324  // on return, X is overwritten by the computed solution
325  X.resize(B.rows(),B.cols());
326 
327  // this ugly const_cast_derived() helps to detect aliasing when applying the permutations
328  for(Index j = 0; j < B.cols(); ++j)
329  X.col(j) = rowsPermutation() * B.const_cast_derived().col(j);
330 
331  //Forward substitution with L
332  this->matrixL().solveInPlace(X);
333  this->matrixU().solveInPlace(X);
334 
335  // Permute back the solution
336  for (Index j = 0; j < B.cols(); ++j)
337  X.col(j) = colsPermutation().inverse() * X.col(j);
338 
339  return true;
340  }
341 
352  Scalar absDeterminant()
353  {
354  using std::abs;
355  eigen_assert(m_factorizationIsOk && "The matrix should be factorized first.");
356  // Initialize with the determinant of the row matrix
357  Scalar det = Scalar(1.);
358  // Note that the diagonal blocks of U are stored in supernodes,
359  // which are available in the L part :)
360  for (Index j = 0; j < this->cols(); ++j)
361  {
362  for (typename SCMatrix::InnerIterator it(m_Lstore, j); it; ++it)
363  {
364  if(it.index() == j)
365  {
366  det *= abs(it.value());
367  break;
368  }
369  }
370  }
371  return det;
372  }
373 
382  Scalar logAbsDeterminant() const
383  {
384  using std::log;
385  using std::abs;
386 
387  eigen_assert(m_factorizationIsOk && "The matrix should be factorized first.");
388  Scalar det = Scalar(0.);
389  for (Index j = 0; j < this->cols(); ++j)
390  {
391  for (typename SCMatrix::InnerIterator it(m_Lstore, j); it; ++it)
392  {
393  if(it.row() < j) continue;
394  if(it.row() == j)
395  {
396  det += log(abs(it.value()));
397  break;
398  }
399  }
400  }
401  return det;
402  }
403 
409  {
410  eigen_assert(m_factorizationIsOk && "The matrix should be factorized first.");
411  // Initialize with the determinant of the row matrix
412  Index det = 1;
413  // Note that the diagonal blocks of U are stored in supernodes,
414  // which are available in the L part :)
415  for (Index j = 0; j < this->cols(); ++j)
416  {
417  for (typename SCMatrix::InnerIterator it(m_Lstore, j); it; ++it)
418  {
419  if(it.index() == j)
420  {
421  if(it.value()<0)
422  det = -det;
423  else if(it.value()==0)
424  return 0;
425  break;
426  }
427  }
428  }
429  return det * m_detPermR * m_detPermC;
430  }
431 
436  Scalar determinant()
437  {
438  eigen_assert(m_factorizationIsOk && "The matrix should be factorized first.");
439  // Initialize with the determinant of the row matrix
440  Scalar det = Scalar(1.);
441  // Note that the diagonal blocks of U are stored in supernodes,
442  // which are available in the L part :)
443  for (Index j = 0; j < this->cols(); ++j)
444  {
445  for (typename SCMatrix::InnerIterator it(m_Lstore, j); it; ++it)
446  {
447  if(it.index() == j)
448  {
449  det *= it.value();
450  break;
451  }
452  }
453  }
454  return (m_detPermR * m_detPermC) > 0 ? det : -det;
455  }
456 
457  Index nnzL() const { return m_nnzL; }
458  Index nnzU() const { return m_nnzU; }
459 
460  protected:
461  // Functions
462  void initperfvalues()
463  {
464  m_perfv.panel_size = 16;
465  m_perfv.relax = 1;
466  m_perfv.maxsuper = 128;
467  m_perfv.rowblk = 16;
468  m_perfv.colblk = 8;
469  m_perfv.fillfactor = 20;
470  }
471 
472  // Variables
473  mutable ComputationInfo m_info;
474  bool m_factorizationIsOk;
475  bool m_analysisIsOk;
476  std::string m_lastError;
477  NCMatrix m_mat; // The input (permuted ) matrix
478  SCMatrix m_Lstore; // The lower triangular matrix (supernodal)
479  Map<SparseMatrix<Scalar,ColMajor,StorageIndex>> m_Ustore; // The upper triangular matrix
480  PermutationType m_perm_c; // Column permutation
481  PermutationType m_perm_r ; // Row permutation
482  IndexVector m_etree; // Column elimination tree
483 
484  typename Base::GlobalLU_t m_glu;
485 
486  // SparseLU options
487  bool m_symmetricmode;
488  // values for performance
489  internal::perfvalues m_perfv;
490  RealScalar m_diagpivotthresh; // Specifies the threshold used for a diagonal entry to be an acceptable pivot
491  Index m_nnzL, m_nnzU; // Nonzeros in L and U factors
492  Index m_detPermR, m_detPermC; // Determinants of the permutation matrices
493  private:
494  // Disable copy constructor
495  SparseLU (const SparseLU& );
496 }; // End class SparseLU
497 
498 
499 
500 // Functions needed by the anaysis phase
511 template <typename MatrixType, typename OrderingType>
513 {
514 
515  //TODO It is possible as in SuperLU to compute row and columns scaling vectors to equilibrate the matrix mat.
516 
517  // Firstly, copy the whole input matrix.
518  m_mat = mat;
519 
520  // Compute fill-in ordering
521  OrderingType ord;
522  ord(m_mat,m_perm_c);
523 
524  // Apply the permutation to the column of the input matrix
525  if (m_perm_c.size())
526  {
527  m_mat.uncompress(); //NOTE: The effect of this command is only to create the InnerNonzeros pointers. FIXME : This vector is filled but not subsequently used.
528  // Then, permute only the column pointers
529  ei_declare_aligned_stack_constructed_variable(StorageIndex,outerIndexPtr,mat.cols()+1,mat.isCompressed()?const_cast<StorageIndex*>(mat.outerIndexPtr()):0);
530 
531  // If the input matrix 'mat' is uncompressed, then the outer-indices do not match the ones of m_mat, and a copy is thus needed.
532  if(!mat.isCompressed())
533  IndexVector::Map(outerIndexPtr, mat.cols()+1) = IndexVector::Map(m_mat.outerIndexPtr(),mat.cols()+1);
534 
535  // Apply the permutation and compute the nnz per column.
536  for (Index i = 0; i < mat.cols(); i++)
537  {
538  m_mat.outerIndexPtr()[m_perm_c.indices()(i)] = outerIndexPtr[i];
539  m_mat.innerNonZeroPtr()[m_perm_c.indices()(i)] = outerIndexPtr[i+1] - outerIndexPtr[i];
540  }
541  }
542 
543  // Compute the column elimination tree of the permuted matrix
544  IndexVector firstRowElt;
545  internal::coletree(m_mat, m_etree,firstRowElt);
546 
547  // In symmetric mode, do not do postorder here
548  if (!m_symmetricmode) {
549  IndexVector post, iwork;
550  // Post order etree
551  internal::treePostorder(StorageIndex(m_mat.cols()), m_etree, post);
552 
553 
554  // Renumber etree in postorder
555  Index m = m_mat.cols();
556  iwork.resize(m+1);
557  for (Index i = 0; i < m; ++i) iwork(post(i)) = post(m_etree(i));
558  m_etree = iwork;
559 
560  // Postmultiply A*Pc by post, i.e reorder the matrix according to the postorder of the etree
561  PermutationType post_perm(m);
562  for (Index i = 0; i < m; i++)
563  post_perm.indices()(i) = post(i);
564 
565  // Combine the two permutations : postorder the permutation for future use
566  if(m_perm_c.size()) {
567  m_perm_c = post_perm * m_perm_c;
568  }
569 
570  } // end postordering
571 
572  m_analysisIsOk = true;
573 }
574 
575 // Functions needed by the numerical factorization phase
576 
577 
596 template <typename MatrixType, typename OrderingType>
597 void SparseLU<MatrixType, OrderingType>::factorize(const MatrixType& matrix)
598 {
599  using internal::emptyIdxLU;
600  eigen_assert(m_analysisIsOk && "analyzePattern() should be called first");
601  eigen_assert((matrix.rows() == matrix.cols()) && "Only for squared matrices");
602 
603  m_isInitialized = true;
604 
605  // Apply the column permutation computed in analyzepattern()
606  // m_mat = matrix * m_perm_c.inverse();
607  m_mat = matrix;
608  if (m_perm_c.size())
609  {
610  m_mat.uncompress(); //NOTE: The effect of this command is only to create the InnerNonzeros pointers.
611  //Then, permute only the column pointers
612  const StorageIndex * outerIndexPtr;
613  if (matrix.isCompressed()) outerIndexPtr = matrix.outerIndexPtr();
614  else
615  {
616  StorageIndex* outerIndexPtr_t = new StorageIndex[matrix.cols()+1];
617  for(Index i = 0; i <= matrix.cols(); i++) outerIndexPtr_t[i] = m_mat.outerIndexPtr()[i];
618  outerIndexPtr = outerIndexPtr_t;
619  }
620  for (Index i = 0; i < matrix.cols(); i++)
621  {
622  m_mat.outerIndexPtr()[m_perm_c.indices()(i)] = outerIndexPtr[i];
623  m_mat.innerNonZeroPtr()[m_perm_c.indices()(i)] = outerIndexPtr[i+1] - outerIndexPtr[i];
624  }
625  if(!matrix.isCompressed()) delete[] outerIndexPtr;
626  }
627  else
628  { //FIXME This should not be needed if the empty permutation is handled transparently
629  m_perm_c.resize(matrix.cols());
630  for(StorageIndex i = 0; i < matrix.cols(); ++i) m_perm_c.indices()(i) = i;
631  }
632 
633  Index m = m_mat.rows();
634  Index n = m_mat.cols();
635  Index nnz = m_mat.nonZeros();
636  Index maxpanel = m_perfv.panel_size * m;
637  // Allocate working storage common to the factor routines
638  Index lwork = 0;
639  Index info = Base::memInit(m, n, nnz, lwork, m_perfv.fillfactor, m_perfv.panel_size, m_glu);
640  if (info)
641  {
642  m_lastError = "UNABLE TO ALLOCATE WORKING MEMORY\n\n" ;
643  m_factorizationIsOk = false;
644  return ;
645  }
646 
647  // Set up pointers for integer working arrays
648  IndexVector segrep(m); segrep.setZero();
649  IndexVector parent(m); parent.setZero();
650  IndexVector xplore(m); xplore.setZero();
651  IndexVector repfnz(maxpanel);
652  IndexVector panel_lsub(maxpanel);
653  IndexVector xprune(n); xprune.setZero();
654  IndexVector marker(m*internal::LUNoMarker); marker.setZero();
655 
656  repfnz.setConstant(-1);
657  panel_lsub.setConstant(-1);
658 
659  // Set up pointers for scalar working arrays
660  ScalarVector dense;
661  dense.setZero(maxpanel);
662  ScalarVector tempv;
663  tempv.setZero(internal::LUnumTempV(m, m_perfv.panel_size, m_perfv.maxsuper, /*m_perfv.rowblk*/m) );
664 
665  // Compute the inverse of perm_c
666  PermutationType iperm_c(m_perm_c.inverse());
667 
668  // Identify initial relaxed snodes
669  IndexVector relax_end(n);
670  if ( m_symmetricmode == true )
671  Base::heap_relax_snode(n, m_etree, m_perfv.relax, marker, relax_end);
672  else
673  Base::relax_snode(n, m_etree, m_perfv.relax, marker, relax_end);
674 
675 
676  m_perm_r.resize(m);
677  m_perm_r.indices().setConstant(-1);
678  marker.setConstant(-1);
679  m_detPermR = 1; // Record the determinant of the row permutation
680 
681  m_glu.supno(0) = emptyIdxLU; m_glu.xsup.setConstant(0);
682  m_glu.xsup(0) = m_glu.xlsub(0) = m_glu.xusub(0) = m_glu.xlusup(0) = Index(0);
683 
684  // Work on one 'panel' at a time. A panel is one of the following :
685  // (a) a relaxed supernode at the bottom of the etree, or
686  // (b) panel_size contiguous columns, <panel_size> defined by the user
687  Index jcol;
688  Index pivrow; // Pivotal row number in the original row matrix
689  Index nseg1; // Number of segments in U-column above panel row jcol
690  Index nseg; // Number of segments in each U-column
691  Index irep;
692  Index i, k, jj;
693  for (jcol = 0; jcol < n; )
694  {
695  // Adjust panel size so that a panel won't overlap with the next relaxed snode.
696  Index panel_size = m_perfv.panel_size; // upper bound on panel width
697  for (k = jcol + 1; k < (std::min)(jcol+panel_size, n); k++)
698  {
699  if (relax_end(k) != emptyIdxLU)
700  {
701  panel_size = k - jcol;
702  break;
703  }
704  }
705  if (k == n)
706  panel_size = n - jcol;
707 
708  // Symbolic outer factorization on a panel of columns
709  Base::panel_dfs(m, panel_size, jcol, m_mat, m_perm_r.indices(), nseg1, dense, panel_lsub, segrep, repfnz, xprune, marker, parent, xplore, m_glu);
710 
711  // Numeric sup-panel updates in topological order
712  Base::panel_bmod(m, panel_size, jcol, nseg1, dense, tempv, segrep, repfnz, m_glu);
713 
714  // Sparse LU within the panel, and below the panel diagonal
715  for ( jj = jcol; jj< jcol + panel_size; jj++)
716  {
717  k = (jj - jcol) * m; // Column index for w-wide arrays
718 
719  nseg = nseg1; // begin after all the panel segments
720  //Depth-first-search for the current column
721  VectorBlock<IndexVector> panel_lsubk(panel_lsub, k, m);
722  VectorBlock<IndexVector> repfnz_k(repfnz, k, m);
723  info = Base::column_dfs(m, jj, m_perm_r.indices(), m_perfv.maxsuper, nseg, panel_lsubk, segrep, repfnz_k, xprune, marker, parent, xplore, m_glu);
724  if ( info )
725  {
726  m_lastError = "UNABLE TO EXPAND MEMORY IN COLUMN_DFS() ";
727  m_info = NumericalIssue;
728  m_factorizationIsOk = false;
729  return;
730  }
731  // Numeric updates to this column
732  VectorBlock<ScalarVector> dense_k(dense, k, m);
733  VectorBlock<IndexVector> segrep_k(segrep, nseg1, m-nseg1);
734  info = Base::column_bmod(jj, (nseg - nseg1), dense_k, tempv, segrep_k, repfnz_k, jcol, m_glu);
735  if ( info )
736  {
737  m_lastError = "UNABLE TO EXPAND MEMORY IN COLUMN_BMOD() ";
738  m_info = NumericalIssue;
739  m_factorizationIsOk = false;
740  return;
741  }
742 
743  // Copy the U-segments to ucol(*)
744  info = Base::copy_to_ucol(jj, nseg, segrep, repfnz_k ,m_perm_r.indices(), dense_k, m_glu);
745  if ( info )
746  {
747  m_lastError = "UNABLE TO EXPAND MEMORY IN COPY_TO_UCOL() ";
748  m_info = NumericalIssue;
749  m_factorizationIsOk = false;
750  return;
751  }
752 
753  // Form the L-segment
754  info = Base::pivotL(jj, m_diagpivotthresh, m_perm_r.indices(), iperm_c.indices(), pivrow, m_glu);
755  if ( info )
756  {
757  m_lastError = "THE MATRIX IS STRUCTURALLY SINGULAR ... ZERO COLUMN AT ";
758  std::ostringstream returnInfo;
759  returnInfo << info;
760  m_lastError += returnInfo.str();
761  m_info = NumericalIssue;
762  m_factorizationIsOk = false;
763  return;
764  }
765 
766  // Update the determinant of the row permutation matrix
767  // FIXME: the following test is not correct, we should probably take iperm_c into account and pivrow is not directly the row pivot.
768  if (pivrow != jj) m_detPermR = -m_detPermR;
769 
770  // Prune columns (0:jj-1) using column jj
771  Base::pruneL(jj, m_perm_r.indices(), pivrow, nseg, segrep, repfnz_k, xprune, m_glu);
772 
773  // Reset repfnz for this column
774  for (i = 0; i < nseg; i++)
775  {
776  irep = segrep(i);
777  repfnz_k(irep) = emptyIdxLU;
778  }
779  } // end SparseLU within the panel
780  jcol += panel_size; // Move to the next panel
781  } // end for -- end elimination
782 
783  m_detPermR = m_perm_r.determinant();
784  m_detPermC = m_perm_c.determinant();
785 
786  // Count the number of nonzeros in factors
787  Base::countnz(n, m_nnzL, m_nnzU, m_glu);
788  // Apply permutation to the L subscripts
789  Base::fixupL(n, m_perm_r.indices(), m_glu);
790 
791  // Create supernode matrix L
792  m_Lstore.setInfos(m, n, m_glu.lusup, m_glu.xlusup, m_glu.lsub, m_glu.xlsub, m_glu.supno, m_glu.xsup);
793  // Create the column major upper sparse matrix U;
794  new (&m_Ustore) Map<SparseMatrix<Scalar, ColMajor, StorageIndex>> ( m, n, m_nnzU, m_glu.xusub.data(), m_glu.usub.data(), m_glu.ucol.data() );
795 
796  m_info = Success;
797  m_factorizationIsOk = true;
798 }
799 
800 template<typename MappedSupernodalType>
801 struct SparseLUMatrixLReturnType : internal::no_assignment_operator
802 {
803  typedef typename MappedSupernodalType::Scalar Scalar;
804  explicit SparseLUMatrixLReturnType(const MappedSupernodalType& mapL) : m_mapL(mapL)
805  { }
806  Index rows() const { return m_mapL.rows(); }
807  Index cols() const { return m_mapL.cols(); }
808  template<typename Dest>
809  void solveInPlace( MatrixBase<Dest> &X) const
810  {
811  m_mapL.solveInPlace(X);
812  }
813  template<bool Conjugate, typename Dest>
814  void solveTransposedInPlace( MatrixBase<Dest> &X) const
815  {
816  m_mapL.template solveTransposedInPlace<Conjugate>(X);
817  }
818 
819  const MappedSupernodalType& m_mapL;
820 };
821 
822 template<typename MatrixLType, typename MatrixUType>
823 struct SparseLUMatrixUReturnType : internal::no_assignment_operator
824 {
825  typedef typename MatrixLType::Scalar Scalar;
826  SparseLUMatrixUReturnType(const MatrixLType& mapL, const MatrixUType& mapU)
827  : m_mapL(mapL),m_mapU(mapU)
828  { }
829  Index rows() const { return m_mapL.rows(); }
830  Index cols() const { return m_mapL.cols(); }
831 
832  template<typename Dest> void solveInPlace(MatrixBase<Dest> &X) const
833  {
834  Index nrhs = X.cols();
835  Index n = X.rows();
836  // Backward solve with U
837  for (Index k = m_mapL.nsuper(); k >= 0; k--)
838  {
839  Index fsupc = m_mapL.supToCol()[k];
840  Index lda = m_mapL.colIndexPtr()[fsupc+1] - m_mapL.colIndexPtr()[fsupc]; // leading dimension
841  Index nsupc = m_mapL.supToCol()[k+1] - fsupc;
842  Index luptr = m_mapL.colIndexPtr()[fsupc];
843 
844  if (nsupc == 1)
845  {
846  for (Index j = 0; j < nrhs; j++)
847  {
848  X(fsupc, j) /= m_mapL.valuePtr()[luptr];
849  }
850  }
851  else
852  {
853  // FIXME: the following lines should use Block expressions and not Map!
854  Map<const Matrix<Scalar,Dynamic,Dynamic, ColMajor>, 0, OuterStride<> > A( &(m_mapL.valuePtr()[luptr]), nsupc, nsupc, OuterStride<>(lda) );
855  Map< Matrix<Scalar,Dynamic,Dest::ColsAtCompileTime, ColMajor>, 0, OuterStride<> > U (&(X.coeffRef(fsupc,0)), nsupc, nrhs, OuterStride<>(n) );
856  U = A.template triangularView<Upper>().solve(U);
857  }
858 
859  for (Index j = 0; j < nrhs; ++j)
860  {
861  for (Index jcol = fsupc; jcol < fsupc + nsupc; jcol++)
862  {
863  typename MatrixUType::InnerIterator it(m_mapU, jcol);
864  for ( ; it; ++it)
865  {
866  Index irow = it.index();
867  X(irow, j) -= X(jcol, j) * it.value();
868  }
869  }
870  }
871  } // End For U-solve
872  }
873 
874  template<bool Conjugate, typename Dest> void solveTransposedInPlace(MatrixBase<Dest> &X) const
875  {
876  using numext::conj;
877  Index nrhs = X.cols();
878  Index n = X.rows();
879  // Forward solve with U
880  for (Index k = 0; k <= m_mapL.nsuper(); k++)
881  {
882  Index fsupc = m_mapL.supToCol()[k];
883  Index lda = m_mapL.colIndexPtr()[fsupc+1] - m_mapL.colIndexPtr()[fsupc]; // leading dimension
884  Index nsupc = m_mapL.supToCol()[k+1] - fsupc;
885  Index luptr = m_mapL.colIndexPtr()[fsupc];
886 
887  for (Index j = 0; j < nrhs; ++j)
888  {
889  for (Index jcol = fsupc; jcol < fsupc + nsupc; jcol++)
890  {
891  typename MatrixUType::InnerIterator it(m_mapU, jcol);
892  for ( ; it; ++it)
893  {
894  Index irow = it.index();
895  X(jcol, j) -= X(irow, j) * (Conjugate? conj(it.value()): it.value());
896  }
897  }
898  }
899  if (nsupc == 1)
900  {
901  for (Index j = 0; j < nrhs; j++)
902  {
903  X(fsupc, j) /= (Conjugate? conj(m_mapL.valuePtr()[luptr]) : m_mapL.valuePtr()[luptr]);
904  }
905  }
906  else
907  {
908  Map<const Matrix<Scalar,Dynamic,Dynamic, ColMajor>, 0, OuterStride<> > A( &(m_mapL.valuePtr()[luptr]), nsupc, nsupc, OuterStride<>(lda) );
909  Map< Matrix<Scalar,Dynamic,Dest::ColsAtCompileTime, ColMajor>, 0, OuterStride<> > U (&(X(fsupc,0)), nsupc, nrhs, OuterStride<>(n) );
910  if(Conjugate)
911  U = A.adjoint().template triangularView<Lower>().solve(U);
912  else
913  U = A.transpose().template triangularView<Lower>().solve(U);
914  }
915  }// End For U-solve
916  }
917 
918 
919  const MatrixLType& m_mapL;
920  const MatrixUType& m_mapU;
921 };
922 
923 } // End namespace Eigen
924 
925 #endif
Derived & derived()
Definition: EigenBase.h:48
EIGEN_CONSTEXPR Index cols() const EIGEN_NOEXCEPT
Definition: EigenBase.h:65
EIGEN_CONSTEXPR Index rows() const EIGEN_NOEXCEPT
Definition: EigenBase.h:62
A matrix or vector expression mapping an existing array of data.
Definition: Map.h:98
Base class for all dense matrices, vectors, and expressions.
Definition: MatrixBase.h:52
InverseReturnType inverse() const
Definition: PermutationMatrix.h:187
const IndicesType & indices() const
Definition: PermutationMatrix.h:362
void resize(Index rows, Index cols)
Definition: PlainObjectBase.h:283
Derived & setConstant(Index size, const Scalar &val)
Definition: CwiseNullaryOp.h:363
Derived & setZero(Index size)
Definition: CwiseNullaryOp.h:564
Pseudo expression representing a solving operation.
Definition: Solve.h:65
Sparse supernodal LU factorization for general matrices.
Definition: SparseLU.h:134
void setPivotThreshold(const RealScalar &thresh)
Definition: SparseLU.h:277
Scalar logAbsDeterminant() const
Definition: SparseLU.h:382
SparseLUMatrixUReturnType< SCMatrix, Map< SparseMatrix< Scalar, ColMajor, StorageIndex > > > matrixU() const
Definition: SparseLU.h:255
void factorize(const MatrixType &matrix)
Definition: SparseLU.h:597
const PermutationType & rowsPermutation() const
Definition: SparseLU.h:264
std::string lastErrorMessage() const
Definition: SparseLU.h:310
const SparseLUTransposeView< true, SparseLU< MatrixType_, OrderingType_ > > adjoint()
Definition: SparseLU.h:223
SparseLUMatrixLReturnType< SCMatrix > matrixL() const
Definition: SparseLU.h:245
void compute(const MatrixType &matrix)
Definition: SparseLU.h:184
ComputationInfo info() const
Reports whether previous computation was successful.
Definition: SparseLU.h:301
Scalar signDeterminant()
Definition: SparseLU.h:408
Scalar absDeterminant()
Definition: SparseLU.h:352
void analyzePattern(const MatrixType &matrix)
Definition: SparseLU.h:512
const Solve< SparseLU, Rhs > solve(const MatrixBase< Rhs > &B) const
const SparseLUTransposeView< false, SparseLU< MatrixType_, OrderingType_ > > transpose()
Definition: SparseLU.h:202
void isSymmetric(bool sym)
Definition: SparseLU.h:234
const PermutationType & colsPermutation() const
Definition: SparseLU.h:272
Scalar determinant()
Definition: SparseLU.h:436
Index cols() const
Definition: SparseMatrix.h:142
Index rows() const
Definition: SparseMatrix.h:140
A base class for sparse solvers.
Definition: SparseSolverBase.h:70
Expression of a fixed-size or dynamic-size sub-vector.
Definition: VectorBlock.h:62
ComputationInfo
Definition: Constants.h:442
@ NumericalIssue
Definition: Constants.h:446
@ Success
Definition: Constants.h:444
const unsigned int RowMajorBit
Definition: Constants.h:68
Namespace containing all symbols from the Eigen library.
Definition: Core:139
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:59
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_conjugate_op< typename Derived::Scalar >, const Derived > conj(const Eigen::ArrayBase< Derived > &x)
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_abs_op< typename Derived::Scalar >, const Derived > abs(const Eigen::ArrayBase< Derived > &x)
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_log_op< typename Derived::Scalar >, const Derived > log(const Eigen::ArrayBase< Derived > &x)