Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-04-19 09:06:42

0001 // This file is part of Eigen, a lightweight C++ template library
0002 // for linear algebra.
0003 //
0004 // Copyright (C) 2012 Désiré Nuentsa-Wakam <desire.nuentsa_wakam@inria.fr>
0005 // Copyright (C) 2012-2014 Gael Guennebaud <gael.guennebaud@inria.fr>
0006 //
0007 // This Source Code Form is subject to the terms of the Mozilla
0008 // Public License v. 2.0. If a copy of the MPL was not distributed
0009 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
0010 
0011 
0012 #ifndef EIGEN_SPARSE_LU_H
0013 #define EIGEN_SPARSE_LU_H
0014 
0015 namespace RivetEigen {
0016 
0017 template <typename _MatrixType, typename _OrderingType = COLAMDOrdering<typename _MatrixType::StorageIndex> > class SparseLU;
0018 template <typename MappedSparseMatrixType> struct SparseLUMatrixLReturnType;
0019 template <typename MatrixLType, typename MatrixUType> struct SparseLUMatrixUReturnType;
0020 
0021 template <bool Conjugate,class SparseLUType>
0022 class SparseLUTransposeView : public SparseSolverBase<SparseLUTransposeView<Conjugate,SparseLUType> >
0023 {
0024 protected:
0025   typedef SparseSolverBase<SparseLUTransposeView<Conjugate,SparseLUType> > APIBase;
0026   using APIBase::m_isInitialized;
0027 public:
0028   typedef typename SparseLUType::Scalar Scalar;
0029   typedef typename SparseLUType::StorageIndex StorageIndex;
0030   typedef typename SparseLUType::MatrixType MatrixType;
0031   typedef typename SparseLUType::OrderingType OrderingType;
0032 
0033   enum {
0034     ColsAtCompileTime = MatrixType::ColsAtCompileTime,
0035     MaxColsAtCompileTime = MatrixType::MaxColsAtCompileTime
0036   };
0037 
0038   SparseLUTransposeView() : m_sparseLU(NULL) {}
0039   SparseLUTransposeView(const SparseLUTransposeView& view) {
0040     this->m_sparseLU = view.m_sparseLU;
0041   }
0042   void setIsInitialized(const bool isInitialized) {this->m_isInitialized = isInitialized;}
0043   void setSparseLU(SparseLUType* sparseLU) {m_sparseLU = sparseLU;}
0044   using APIBase::_solve_impl;
0045   template<typename Rhs, typename Dest>
0046   bool _solve_impl(const MatrixBase<Rhs> &B, MatrixBase<Dest> &X_base) const
0047   {
0048     Dest& X(X_base.derived());
0049     eigen_assert(m_sparseLU->info() == Success && "The matrix should be factorized first");
0050     EIGEN_STATIC_ASSERT((Dest::Flags&RowMajorBit)==0,
0051                         THIS_METHOD_IS_ONLY_FOR_COLUMN_MAJOR_MATRICES);
0052 
0053 
0054     // this ugly const_cast_derived() helps to detect aliasing when applying the permutations
0055     for(Index j = 0; j < B.cols(); ++j){
0056       X.col(j) = m_sparseLU->colsPermutation() * B.const_cast_derived().col(j);
0057     }
0058     //Forward substitution with transposed or adjoint of U
0059     m_sparseLU->matrixU().template solveTransposedInPlace<Conjugate>(X);
0060 
0061     //Backward substitution with transposed or adjoint of L
0062     m_sparseLU->matrixL().template solveTransposedInPlace<Conjugate>(X);
0063 
0064     // Permute back the solution
0065     for (Index j = 0; j < B.cols(); ++j)
0066       X.col(j) = m_sparseLU->rowsPermutation().transpose() * X.col(j);
0067     return true;
0068   }
0069   inline Index rows() const { return m_sparseLU->rows(); }
0070   inline Index cols() const { return m_sparseLU->cols(); }
0071 
0072 private:
0073   SparseLUType *m_sparseLU;
0074   SparseLUTransposeView& operator=(const SparseLUTransposeView&);
0075 };
0076 
0077 
0078 /** \ingroup SparseLU_Module
0079   * \class SparseLU
0080   * 
0081   * \brief Sparse supernodal LU factorization for general matrices
0082   * 
0083   * This class implements the supernodal LU factorization for general matrices.
0084   * It uses the main techniques from the sequential SuperLU package 
0085   * (http://crd-legacy.lbl.gov/~xiaoye/SuperLU/). It handles transparently real 
0086   * and complex arithmetic with single and double precision, depending on the 
0087   * scalar type of your input matrix. 
0088   * The code has been optimized to provide BLAS-3 operations during supernode-panel updates. 
0089   * It benefits directly from the built-in high-performant Eigen BLAS routines. 
0090   * Moreover, when the size of a supernode is very small, the BLAS calls are avoided to 
0091   * enable a better optimization from the compiler. For best performance, 
0092   * you should compile it with NDEBUG flag to avoid the numerous bounds checking on vectors. 
0093   * 
0094   * An important parameter of this class is the ordering method. It is used to reorder the columns 
0095   * (and eventually the rows) of the matrix to reduce the number of new elements that are created during 
0096   * numerical factorization. The cheapest method available is COLAMD. 
0097   * See  \link OrderingMethods_Module the OrderingMethods module \endlink for the list of 
0098   * built-in and external ordering methods. 
0099   *
0100   * Simple example with key steps 
0101   * \code
0102   * VectorXd x(n), b(n);
0103   * SparseMatrix<double> A;
0104   * SparseLU<SparseMatrix<double>, COLAMDOrdering<int> >   solver;
0105   * // fill A and b;
0106   * // Compute the ordering permutation vector from the structural pattern of A
0107   * solver.analyzePattern(A); 
0108   * // Compute the numerical factorization 
0109   * solver.factorize(A); 
0110   * //Use the factors to solve the linear system 
0111   * x = solver.solve(b); 
0112   * \endcode
0113   * 
0114   * \warning The input matrix A should be in a \b compressed and \b column-major form.
0115   * Otherwise an expensive copy will be made. You can call the inexpensive makeCompressed() to get a compressed matrix.
0116   * 
0117   * \note Unlike the initial SuperLU implementation, there is no step to equilibrate the matrix. 
0118   * For badly scaled matrices, this step can be useful to reduce the pivoting during factorization. 
0119   * If this is the case for your matrices, you can try the basic scaling method at
0120   *  "unsupported/Eigen/src/IterativeSolvers/Scaling.h"
0121   * 
0122   * \tparam _MatrixType The type of the sparse matrix. It must be a column-major SparseMatrix<>
0123   * \tparam _OrderingType The ordering method to use, either AMD, COLAMD or METIS. Default is COLMAD
0124   *
0125   * \implsparsesolverconcept
0126   * 
0127   * \sa \ref TutorialSparseSolverConcept
0128   * \sa \ref OrderingMethods_Module
0129   */
0130 template <typename _MatrixType, typename _OrderingType>
0131 class SparseLU : public SparseSolverBase<SparseLU<_MatrixType,_OrderingType> >, public internal::SparseLUImpl<typename _MatrixType::Scalar, typename _MatrixType::StorageIndex>
0132 {
0133   protected:
0134     typedef SparseSolverBase<SparseLU<_MatrixType,_OrderingType> > APIBase;
0135     using APIBase::m_isInitialized;
0136   public:
0137     using APIBase::_solve_impl;
0138     
0139     typedef _MatrixType MatrixType; 
0140     typedef _OrderingType OrderingType;
0141     typedef typename MatrixType::Scalar Scalar; 
0142     typedef typename MatrixType::RealScalar RealScalar; 
0143     typedef typename MatrixType::StorageIndex StorageIndex;
0144     typedef SparseMatrix<Scalar,ColMajor,StorageIndex> NCMatrix;
0145     typedef internal::MappedSuperNodalMatrix<Scalar, StorageIndex> SCMatrix;
0146     typedef Matrix<Scalar,Dynamic,1> ScalarVector;
0147     typedef Matrix<StorageIndex,Dynamic,1> IndexVector;
0148     typedef PermutationMatrix<Dynamic, Dynamic, StorageIndex> PermutationType;
0149     typedef internal::SparseLUImpl<Scalar, StorageIndex> Base;
0150 
0151     enum {
0152       ColsAtCompileTime = MatrixType::ColsAtCompileTime,
0153       MaxColsAtCompileTime = MatrixType::MaxColsAtCompileTime
0154     };
0155     
0156   public:
0157 
0158     SparseLU():m_lastError(""),m_Ustore(0,0,0,0,0,0),m_symmetricmode(false),m_diagpivotthresh(1.0),m_detPermR(1)
0159     {
0160       initperfvalues(); 
0161     }
0162     explicit SparseLU(const MatrixType& matrix)
0163       : m_lastError(""),m_Ustore(0,0,0,0,0,0),m_symmetricmode(false),m_diagpivotthresh(1.0),m_detPermR(1)
0164     {
0165       initperfvalues(); 
0166       compute(matrix);
0167     }
0168     
0169     ~SparseLU()
0170     {
0171       // Free all explicit dynamic pointers 
0172     }
0173     
0174     void analyzePattern (const MatrixType& matrix);
0175     void factorize (const MatrixType& matrix);
0176     void simplicialfactorize(const MatrixType& matrix);
0177     
0178     /**
0179       * Compute the symbolic and numeric factorization of the input sparse matrix.
0180       * The input matrix should be in column-major storage. 
0181       */
0182     void compute (const MatrixType& matrix)
0183     {
0184       // Analyze 
0185       analyzePattern(matrix); 
0186       //Factorize
0187       factorize(matrix);
0188     } 
0189 
0190     /** \returns an expression of the transposed of the factored matrix.
0191       *
0192       * A typical usage is to solve for the transposed problem A^T x = b:
0193       * \code
0194       * solver.compute(A);
0195       * x = solver.transpose().solve(b);
0196       * \endcode
0197       *
0198       * \sa adjoint(), solve()
0199       */
0200     const SparseLUTransposeView<false,SparseLU<_MatrixType,_OrderingType> > transpose()
0201     {
0202       SparseLUTransposeView<false,  SparseLU<_MatrixType,_OrderingType> > transposeView;
0203       transposeView.setSparseLU(this);
0204       transposeView.setIsInitialized(this->m_isInitialized);
0205       return transposeView;
0206     }
0207 
0208 
0209     /** \returns an expression of the adjoint of the factored matrix
0210       *
0211       * A typical usage is to solve for the adjoint problem A' x = b:
0212       * \code
0213       * solver.compute(A);
0214       * x = solver.adjoint().solve(b);
0215       * \endcode
0216       *
0217       * For real scalar types, this function is equivalent to transpose().
0218       *
0219       * \sa transpose(), solve()
0220       */
0221     const SparseLUTransposeView<true, SparseLU<_MatrixType,_OrderingType> > adjoint()
0222     {
0223       SparseLUTransposeView<true,  SparseLU<_MatrixType,_OrderingType> > adjointView;
0224       adjointView.setSparseLU(this);
0225       adjointView.setIsInitialized(this->m_isInitialized);
0226       return adjointView;
0227     }
0228     
0229     inline Index rows() const { return m_mat.rows(); }
0230     inline Index cols() const { return m_mat.cols(); }
0231     /** Indicate that the pattern of the input matrix is symmetric */
0232     void isSymmetric(bool sym)
0233     {
0234       m_symmetricmode = sym;
0235     }
0236     
0237     /** \returns an expression of the matrix L, internally stored as supernodes
0238       * The only operation available with this expression is the triangular solve
0239       * \code
0240       * y = b; matrixL().solveInPlace(y);
0241       * \endcode
0242       */
0243     SparseLUMatrixLReturnType<SCMatrix> matrixL() const
0244     {
0245       return SparseLUMatrixLReturnType<SCMatrix>(m_Lstore);
0246     }
0247     /** \returns an expression of the matrix U,
0248       * The only operation available with this expression is the triangular solve
0249       * \code
0250       * y = b; matrixU().solveInPlace(y);
0251       * \endcode
0252       */
0253     SparseLUMatrixUReturnType<SCMatrix,MappedSparseMatrix<Scalar,ColMajor,StorageIndex> > matrixU() const
0254     {
0255       return SparseLUMatrixUReturnType<SCMatrix, MappedSparseMatrix<Scalar,ColMajor,StorageIndex> >(m_Lstore, m_Ustore);
0256     }
0257 
0258     /**
0259       * \returns a reference to the row matrix permutation \f$ P_r \f$ such that \f$P_r A P_c^T = L U\f$
0260       * \sa colsPermutation()
0261       */
0262     inline const PermutationType& rowsPermutation() const
0263     {
0264       return m_perm_r;
0265     }
0266     /**
0267       * \returns a reference to the column matrix permutation\f$ P_c^T \f$ such that \f$P_r A P_c^T = L U\f$
0268       * \sa rowsPermutation()
0269       */
0270     inline const PermutationType& colsPermutation() const
0271     {
0272       return m_perm_c;
0273     }
0274     /** Set the threshold used for a diagonal entry to be an acceptable pivot. */
0275     void setPivotThreshold(const RealScalar& thresh)
0276     {
0277       m_diagpivotthresh = thresh; 
0278     }
0279 
0280 #ifdef EIGEN_PARSED_BY_DOXYGEN
0281     /** \returns the solution X of \f$ A X = B \f$ using the current decomposition of A.
0282       *
0283       * \warning the destination matrix X in X = this->solve(B) must be colmun-major.
0284       *
0285       * \sa compute()
0286       */
0287     template<typename Rhs>
0288     inline const Solve<SparseLU, Rhs> solve(const MatrixBase<Rhs>& B) const;
0289 #endif // EIGEN_PARSED_BY_DOXYGEN
0290     
0291     /** \brief Reports whether previous computation was successful.
0292       *
0293       * \returns \c Success if computation was successful,
0294       *          \c NumericalIssue if the LU factorization reports a problem, zero diagonal for instance
0295       *          \c InvalidInput if the input matrix is invalid
0296       *
0297       * \sa iparm()          
0298       */
0299     ComputationInfo info() const
0300     {
0301       eigen_assert(m_isInitialized && "Decomposition is not initialized.");
0302       return m_info;
0303     }
0304     
0305     /**
0306       * \returns A string describing the type of error
0307       */
0308     std::string lastErrorMessage() const
0309     {
0310       return m_lastError; 
0311     }
0312 
0313     template<typename Rhs, typename Dest>
0314     bool _solve_impl(const MatrixBase<Rhs> &B, MatrixBase<Dest> &X_base) const
0315     {
0316       Dest& X(X_base.derived());
0317       eigen_assert(m_factorizationIsOk && "The matrix should be factorized first");
0318       EIGEN_STATIC_ASSERT((Dest::Flags&RowMajorBit)==0,
0319                         THIS_METHOD_IS_ONLY_FOR_COLUMN_MAJOR_MATRICES);
0320       
0321       // Permute the right hand side to form X = Pr*B
0322       // on return, X is overwritten by the computed solution
0323       X.resize(B.rows(),B.cols());
0324 
0325       // this ugly const_cast_derived() helps to detect aliasing when applying the permutations
0326       for(Index j = 0; j < B.cols(); ++j)
0327         X.col(j) = rowsPermutation() * B.const_cast_derived().col(j);
0328       
0329       //Forward substitution with L
0330       this->matrixL().solveInPlace(X);
0331       this->matrixU().solveInPlace(X);
0332       
0333       // Permute back the solution 
0334       for (Index j = 0; j < B.cols(); ++j)
0335         X.col(j) = colsPermutation().inverse() * X.col(j);
0336       
0337       return true; 
0338     }
0339     
0340     /**
0341       * \returns the absolute value of the determinant of the matrix of which
0342       * *this is the QR decomposition.
0343       *
0344       * \warning a determinant can be very big or small, so for matrices
0345       * of large enough dimension, there is a risk of overflow/underflow.
0346       * One way to work around that is to use logAbsDeterminant() instead.
0347       *
0348       * \sa logAbsDeterminant(), signDeterminant()
0349       */
0350     Scalar absDeterminant()
0351     {
0352       using std::abs;
0353       eigen_assert(m_factorizationIsOk && "The matrix should be factorized first.");
0354       // Initialize with the determinant of the row matrix
0355       Scalar det = Scalar(1.);
0356       // Note that the diagonal blocks of U are stored in supernodes,
0357       // which are available in the  L part :)
0358       for (Index j = 0; j < this->cols(); ++j)
0359       {
0360         for (typename SCMatrix::InnerIterator it(m_Lstore, j); it; ++it)
0361         {
0362           if(it.index() == j)
0363           {
0364             det *= abs(it.value());
0365             break;
0366           }
0367         }
0368       }
0369       return det;
0370     }
0371 
0372     /** \returns the natural log of the absolute value of the determinant of the matrix
0373       * of which **this is the QR decomposition
0374       *
0375       * \note This method is useful to work around the risk of overflow/underflow that's
0376       * inherent to the determinant computation.
0377       *
0378       * \sa absDeterminant(), signDeterminant()
0379       */
0380     Scalar logAbsDeterminant() const
0381     {
0382       using std::log;
0383       using std::abs;
0384 
0385       eigen_assert(m_factorizationIsOk && "The matrix should be factorized first.");
0386       Scalar det = Scalar(0.);
0387       for (Index j = 0; j < this->cols(); ++j)
0388       {
0389         for (typename SCMatrix::InnerIterator it(m_Lstore, j); it; ++it)
0390         {
0391           if(it.row() < j) continue;
0392           if(it.row() == j)
0393           {
0394             det += log(abs(it.value()));
0395             break;
0396           }
0397         }
0398       }
0399       return det;
0400     }
0401 
0402     /** \returns A number representing the sign of the determinant
0403       *
0404       * \sa absDeterminant(), logAbsDeterminant()
0405       */
0406     Scalar signDeterminant()
0407     {
0408       eigen_assert(m_factorizationIsOk && "The matrix should be factorized first.");
0409       // Initialize with the determinant of the row matrix
0410       Index det = 1;
0411       // Note that the diagonal blocks of U are stored in supernodes,
0412       // which are available in the  L part :)
0413       for (Index j = 0; j < this->cols(); ++j)
0414       {
0415         for (typename SCMatrix::InnerIterator it(m_Lstore, j); it; ++it)
0416         {
0417           if(it.index() == j)
0418           {
0419             if(it.value()<0)
0420               det = -det;
0421             else if(it.value()==0)
0422               return 0;
0423             break;
0424           }
0425         }
0426       }
0427       return det * m_detPermR * m_detPermC;
0428     }
0429     
0430     /** \returns The determinant of the matrix.
0431       *
0432       * \sa absDeterminant(), logAbsDeterminant()
0433       */
0434     Scalar determinant()
0435     {
0436       eigen_assert(m_factorizationIsOk && "The matrix should be factorized first.");
0437       // Initialize with the determinant of the row matrix
0438       Scalar det = Scalar(1.);
0439       // Note that the diagonal blocks of U are stored in supernodes,
0440       // which are available in the  L part :)
0441       for (Index j = 0; j < this->cols(); ++j)
0442       {
0443         for (typename SCMatrix::InnerIterator it(m_Lstore, j); it; ++it)
0444         {
0445           if(it.index() == j)
0446           {
0447             det *= it.value();
0448             break;
0449           }
0450         }
0451       }
0452       return (m_detPermR * m_detPermC) > 0 ? det : -det;
0453     }
0454 
0455     Index nnzL() const { return m_nnzL; };
0456     Index nnzU() const { return m_nnzU; };
0457 
0458   protected:
0459     // Functions 
0460     void initperfvalues()
0461     {
0462       m_perfv.panel_size = 16;
0463       m_perfv.relax = 1; 
0464       m_perfv.maxsuper = 128; 
0465       m_perfv.rowblk = 16; 
0466       m_perfv.colblk = 8; 
0467       m_perfv.fillfactor = 20;  
0468     }
0469       
0470     // Variables 
0471     mutable ComputationInfo m_info;
0472     bool m_factorizationIsOk;
0473     bool m_analysisIsOk;
0474     std::string m_lastError;
0475     NCMatrix m_mat; // The input (permuted ) matrix 
0476     SCMatrix m_Lstore; // The lower triangular matrix (supernodal)
0477     MappedSparseMatrix<Scalar,ColMajor,StorageIndex> m_Ustore; // The upper triangular matrix
0478     PermutationType m_perm_c; // Column permutation 
0479     PermutationType m_perm_r ; // Row permutation
0480     IndexVector m_etree; // Column elimination tree 
0481     
0482     typename Base::GlobalLU_t m_glu; 
0483                                
0484     // SparseLU options 
0485     bool m_symmetricmode;
0486     // values for performance 
0487     internal::perfvalues m_perfv;
0488     RealScalar m_diagpivotthresh; // Specifies the threshold used for a diagonal entry to be an acceptable pivot
0489     Index m_nnzL, m_nnzU; // Nonzeros in L and U factors
0490     Index m_detPermR, m_detPermC; // Determinants of the permutation matrices
0491   private:
0492     // Disable copy constructor 
0493     SparseLU (const SparseLU& );
0494 }; // End class SparseLU
0495 
0496 
0497 
0498 // Functions needed by the anaysis phase
0499 /** 
0500   * Compute the column permutation to minimize the fill-in
0501   * 
0502   *  - Apply this permutation to the input matrix - 
0503   * 
0504   *  - Compute the column elimination tree on the permuted matrix 
0505   * 
0506   *  - Postorder the elimination tree and the column permutation
0507   * 
0508   */
0509 template <typename MatrixType, typename OrderingType>
0510 void SparseLU<MatrixType, OrderingType>::analyzePattern(const MatrixType& mat)
0511 {
0512   
0513   //TODO  It is possible as in SuperLU to compute row and columns scaling vectors to equilibrate the matrix mat.
0514   
0515   // Firstly, copy the whole input matrix. 
0516   m_mat = mat;
0517   
0518   // Compute fill-in ordering
0519   OrderingType ord; 
0520   ord(m_mat,m_perm_c);
0521   
0522   // Apply the permutation to the column of the input  matrix
0523   if (m_perm_c.size())
0524   {
0525     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.  
0526     // Then, permute only the column pointers
0527     ei_declare_aligned_stack_constructed_variable(StorageIndex,outerIndexPtr,mat.cols()+1,mat.isCompressed()?const_cast<StorageIndex*>(mat.outerIndexPtr()):0);
0528     
0529     // 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.
0530     if(!mat.isCompressed()) 
0531       IndexVector::Map(outerIndexPtr, mat.cols()+1) = IndexVector::Map(m_mat.outerIndexPtr(),mat.cols()+1);
0532     
0533     // Apply the permutation and compute the nnz per column.
0534     for (Index i = 0; i < mat.cols(); i++)
0535     {
0536       m_mat.outerIndexPtr()[m_perm_c.indices()(i)] = outerIndexPtr[i];
0537       m_mat.innerNonZeroPtr()[m_perm_c.indices()(i)] = outerIndexPtr[i+1] - outerIndexPtr[i];
0538     }
0539   }
0540   
0541   // Compute the column elimination tree of the permuted matrix 
0542   IndexVector firstRowElt;
0543   internal::coletree(m_mat, m_etree,firstRowElt); 
0544      
0545   // In symmetric mode, do not do postorder here
0546   if (!m_symmetricmode) {
0547     IndexVector post, iwork; 
0548     // Post order etree
0549     internal::treePostorder(StorageIndex(m_mat.cols()), m_etree, post); 
0550       
0551    
0552     // Renumber etree in postorder 
0553     Index m = m_mat.cols(); 
0554     iwork.resize(m+1);
0555     for (Index i = 0; i < m; ++i) iwork(post(i)) = post(m_etree(i));
0556     m_etree = iwork;
0557     
0558     // Postmultiply A*Pc by post, i.e reorder the matrix according to the postorder of the etree
0559     PermutationType post_perm(m); 
0560     for (Index i = 0; i < m; i++) 
0561       post_perm.indices()(i) = post(i); 
0562         
0563     // Combine the two permutations : postorder the permutation for future use
0564     if(m_perm_c.size()) {
0565       m_perm_c = post_perm * m_perm_c;
0566     }
0567     
0568   } // end postordering 
0569   
0570   m_analysisIsOk = true; 
0571 }
0572 
0573 // Functions needed by the numerical factorization phase
0574 
0575 
0576 /** 
0577   *  - Numerical factorization 
0578   *  - Interleaved with the symbolic factorization 
0579   * On exit,  info is 
0580   * 
0581   *    = 0: successful factorization
0582   * 
0583   *    > 0: if info = i, and i is
0584   * 
0585   *       <= A->ncol: U(i,i) is exactly zero. The factorization has
0586   *          been completed, but the factor U is exactly singular,
0587   *          and division by zero will occur if it is used to solve a
0588   *          system of equations.
0589   * 
0590   *       > A->ncol: number of bytes allocated when memory allocation
0591   *         failure occurred, plus A->ncol. If lwork = -1, it is
0592   *         the estimated amount of space needed, plus A->ncol.  
0593   */
0594 template <typename MatrixType, typename OrderingType>
0595 void SparseLU<MatrixType, OrderingType>::factorize(const MatrixType& matrix)
0596 {
0597   using internal::emptyIdxLU;
0598   eigen_assert(m_analysisIsOk && "analyzePattern() should be called first"); 
0599   eigen_assert((matrix.rows() == matrix.cols()) && "Only for squared matrices");
0600   
0601   m_isInitialized = true;
0602   
0603   // Apply the column permutation computed in analyzepattern()
0604   //   m_mat = matrix * m_perm_c.inverse(); 
0605   m_mat = matrix;
0606   if (m_perm_c.size()) 
0607   {
0608     m_mat.uncompress(); //NOTE: The effect of this command is only to create the InnerNonzeros pointers.
0609     //Then, permute only the column pointers
0610     const StorageIndex * outerIndexPtr;
0611     if (matrix.isCompressed()) outerIndexPtr = matrix.outerIndexPtr();
0612     else
0613     {
0614       StorageIndex* outerIndexPtr_t = new StorageIndex[matrix.cols()+1];
0615       for(Index i = 0; i <= matrix.cols(); i++) outerIndexPtr_t[i] = m_mat.outerIndexPtr()[i];
0616       outerIndexPtr = outerIndexPtr_t;
0617     }
0618     for (Index i = 0; i < matrix.cols(); i++)
0619     {
0620       m_mat.outerIndexPtr()[m_perm_c.indices()(i)] = outerIndexPtr[i];
0621       m_mat.innerNonZeroPtr()[m_perm_c.indices()(i)] = outerIndexPtr[i+1] - outerIndexPtr[i];
0622     }
0623     if(!matrix.isCompressed()) delete[] outerIndexPtr;
0624   } 
0625   else 
0626   { //FIXME This should not be needed if the empty permutation is handled transparently
0627     m_perm_c.resize(matrix.cols());
0628     for(StorageIndex i = 0; i < matrix.cols(); ++i) m_perm_c.indices()(i) = i;
0629   }
0630   
0631   Index m = m_mat.rows();
0632   Index n = m_mat.cols();
0633   Index nnz = m_mat.nonZeros();
0634   Index maxpanel = m_perfv.panel_size * m;
0635   // Allocate working storage common to the factor routines
0636   Index lwork = 0;
0637   Index info = Base::memInit(m, n, nnz, lwork, m_perfv.fillfactor, m_perfv.panel_size, m_glu); 
0638   if (info) 
0639   {
0640     m_lastError = "UNABLE TO ALLOCATE WORKING MEMORY\n\n" ;
0641     m_factorizationIsOk = false;
0642     return ; 
0643   }
0644   
0645   // Set up pointers for integer working arrays 
0646   IndexVector segrep(m); segrep.setZero();
0647   IndexVector parent(m); parent.setZero();
0648   IndexVector xplore(m); xplore.setZero();
0649   IndexVector repfnz(maxpanel);
0650   IndexVector panel_lsub(maxpanel);
0651   IndexVector xprune(n); xprune.setZero();
0652   IndexVector marker(m*internal::LUNoMarker); marker.setZero();
0653   
0654   repfnz.setConstant(-1); 
0655   panel_lsub.setConstant(-1);
0656   
0657   // Set up pointers for scalar working arrays 
0658   ScalarVector dense; 
0659   dense.setZero(maxpanel);
0660   ScalarVector tempv; 
0661   tempv.setZero(internal::LUnumTempV(m, m_perfv.panel_size, m_perfv.maxsuper, /*m_perfv.rowblk*/m) );
0662   
0663   // Compute the inverse of perm_c
0664   PermutationType iperm_c(m_perm_c.inverse()); 
0665   
0666   // Identify initial relaxed snodes
0667   IndexVector relax_end(n);
0668   if ( m_symmetricmode == true ) 
0669     Base::heap_relax_snode(n, m_etree, m_perfv.relax, marker, relax_end);
0670   else
0671     Base::relax_snode(n, m_etree, m_perfv.relax, marker, relax_end);
0672   
0673   
0674   m_perm_r.resize(m); 
0675   m_perm_r.indices().setConstant(-1);
0676   marker.setConstant(-1);
0677   m_detPermR = 1; // Record the determinant of the row permutation
0678   
0679   m_glu.supno(0) = emptyIdxLU; m_glu.xsup.setConstant(0);
0680   m_glu.xsup(0) = m_glu.xlsub(0) = m_glu.xusub(0) = m_glu.xlusup(0) = Index(0);
0681   
0682   // Work on one 'panel' at a time. A panel is one of the following :
0683   //  (a) a relaxed supernode at the bottom of the etree, or
0684   //  (b) panel_size contiguous columns, <panel_size> defined by the user
0685   Index jcol; 
0686   Index pivrow; // Pivotal row number in the original row matrix
0687   Index nseg1; // Number of segments in U-column above panel row jcol
0688   Index nseg; // Number of segments in each U-column 
0689   Index irep; 
0690   Index i, k, jj; 
0691   for (jcol = 0; jcol < n; )
0692   {
0693     // Adjust panel size so that a panel won't overlap with the next relaxed snode. 
0694     Index panel_size = m_perfv.panel_size; // upper bound on panel width
0695     for (k = jcol + 1; k < (std::min)(jcol+panel_size, n); k++)
0696     {
0697       if (relax_end(k) != emptyIdxLU) 
0698       {
0699         panel_size = k - jcol; 
0700         break; 
0701       }
0702     }
0703     if (k == n) 
0704       panel_size = n - jcol; 
0705       
0706     // Symbolic outer factorization on a panel of columns 
0707     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); 
0708     
0709     // Numeric sup-panel updates in topological order 
0710     Base::panel_bmod(m, panel_size, jcol, nseg1, dense, tempv, segrep, repfnz, m_glu); 
0711     
0712     // Sparse LU within the panel, and below the panel diagonal 
0713     for ( jj = jcol; jj< jcol + panel_size; jj++) 
0714     {
0715       k = (jj - jcol) * m; // Column index for w-wide arrays 
0716       
0717       nseg = nseg1; // begin after all the panel segments
0718       //Depth-first-search for the current column
0719       VectorBlock<IndexVector> panel_lsubk(panel_lsub, k, m);
0720       VectorBlock<IndexVector> repfnz_k(repfnz, k, m); 
0721       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); 
0722       if ( info ) 
0723       {
0724         m_lastError =  "UNABLE TO EXPAND MEMORY IN COLUMN_DFS() ";
0725         m_info = NumericalIssue; 
0726         m_factorizationIsOk = false; 
0727         return; 
0728       }
0729       // Numeric updates to this column 
0730       VectorBlock<ScalarVector> dense_k(dense, k, m); 
0731       VectorBlock<IndexVector> segrep_k(segrep, nseg1, m-nseg1); 
0732       info = Base::column_bmod(jj, (nseg - nseg1), dense_k, tempv, segrep_k, repfnz_k, jcol, m_glu); 
0733       if ( info ) 
0734       {
0735         m_lastError = "UNABLE TO EXPAND MEMORY IN COLUMN_BMOD() ";
0736         m_info = NumericalIssue; 
0737         m_factorizationIsOk = false; 
0738         return; 
0739       }
0740       
0741       // Copy the U-segments to ucol(*)
0742       info = Base::copy_to_ucol(jj, nseg, segrep, repfnz_k ,m_perm_r.indices(), dense_k, m_glu); 
0743       if ( info ) 
0744       {
0745         m_lastError = "UNABLE TO EXPAND MEMORY IN COPY_TO_UCOL() ";
0746         m_info = NumericalIssue; 
0747         m_factorizationIsOk = false; 
0748         return; 
0749       }
0750       
0751       // Form the L-segment 
0752       info = Base::pivotL(jj, m_diagpivotthresh, m_perm_r.indices(), iperm_c.indices(), pivrow, m_glu);
0753       if ( info ) 
0754       {
0755         m_lastError = "THE MATRIX IS STRUCTURALLY SINGULAR ... ZERO COLUMN AT ";
0756         std::ostringstream returnInfo;
0757         returnInfo << info; 
0758         m_lastError += returnInfo.str();
0759         m_info = NumericalIssue; 
0760         m_factorizationIsOk = false; 
0761         return; 
0762       }
0763       
0764       // Update the determinant of the row permutation matrix
0765       // FIXME: the following test is not correct, we should probably take iperm_c into account and pivrow is not directly the row pivot.
0766       if (pivrow != jj) m_detPermR = -m_detPermR;
0767 
0768       // Prune columns (0:jj-1) using column jj
0769       Base::pruneL(jj, m_perm_r.indices(), pivrow, nseg, segrep, repfnz_k, xprune, m_glu); 
0770       
0771       // Reset repfnz for this column 
0772       for (i = 0; i < nseg; i++)
0773       {
0774         irep = segrep(i); 
0775         repfnz_k(irep) = emptyIdxLU; 
0776       }
0777     } // end SparseLU within the panel  
0778     jcol += panel_size;  // Move to the next panel
0779   } // end for -- end elimination 
0780   
0781   m_detPermR = m_perm_r.determinant();
0782   m_detPermC = m_perm_c.determinant();
0783   
0784   // Count the number of nonzeros in factors 
0785   Base::countnz(n, m_nnzL, m_nnzU, m_glu); 
0786   // Apply permutation  to the L subscripts 
0787   Base::fixupL(n, m_perm_r.indices(), m_glu);
0788   
0789   // Create supernode matrix L 
0790   m_Lstore.setInfos(m, n, m_glu.lusup, m_glu.xlusup, m_glu.lsub, m_glu.xlsub, m_glu.supno, m_glu.xsup); 
0791   // Create the column major upper sparse matrix  U; 
0792   new (&m_Ustore) MappedSparseMatrix<Scalar, ColMajor, StorageIndex> ( m, n, m_nnzU, m_glu.xusub.data(), m_glu.usub.data(), m_glu.ucol.data() );
0793   
0794   m_info = Success;
0795   m_factorizationIsOk = true;
0796 }
0797 
0798 template<typename MappedSupernodalType>
0799 struct SparseLUMatrixLReturnType : internal::no_assignment_operator
0800 {
0801   typedef typename MappedSupernodalType::Scalar Scalar;
0802   explicit SparseLUMatrixLReturnType(const MappedSupernodalType& mapL) : m_mapL(mapL)
0803   { }
0804   Index rows() const { return m_mapL.rows(); }
0805   Index cols() const { return m_mapL.cols(); }
0806   template<typename Dest>
0807   void solveInPlace( MatrixBase<Dest> &X) const
0808   {
0809     m_mapL.solveInPlace(X);
0810   }
0811   template<bool Conjugate, typename Dest>
0812   void solveTransposedInPlace( MatrixBase<Dest> &X) const
0813   {
0814     m_mapL.template solveTransposedInPlace<Conjugate>(X);
0815   }
0816 
0817   const MappedSupernodalType& m_mapL;
0818 };
0819 
0820 template<typename MatrixLType, typename MatrixUType>
0821 struct SparseLUMatrixUReturnType : internal::no_assignment_operator
0822 {
0823   typedef typename MatrixLType::Scalar Scalar;
0824   SparseLUMatrixUReturnType(const MatrixLType& mapL, const MatrixUType& mapU)
0825   : m_mapL(mapL),m_mapU(mapU)
0826   { }
0827   Index rows() const { return m_mapL.rows(); }
0828   Index cols() const { return m_mapL.cols(); }
0829 
0830   template<typename Dest>   void solveInPlace(MatrixBase<Dest> &X) const
0831   {
0832     Index nrhs = X.cols();
0833     Index n    = X.rows();
0834     // Backward solve with U
0835     for (Index k = m_mapL.nsuper(); k >= 0; k--)
0836     {
0837       Index fsupc = m_mapL.supToCol()[k];
0838       Index lda = m_mapL.colIndexPtr()[fsupc+1] - m_mapL.colIndexPtr()[fsupc]; // leading dimension
0839       Index nsupc = m_mapL.supToCol()[k+1] - fsupc;
0840       Index luptr = m_mapL.colIndexPtr()[fsupc];
0841 
0842       if (nsupc == 1)
0843       {
0844         for (Index j = 0; j < nrhs; j++)
0845         {
0846           X(fsupc, j) /= m_mapL.valuePtr()[luptr];
0847         }
0848       }
0849       else
0850       {
0851         // FIXME: the following lines should use Block expressions and not Map!
0852         Map<const Matrix<Scalar,Dynamic,Dynamic, ColMajor>, 0, OuterStride<> > A( &(m_mapL.valuePtr()[luptr]), nsupc, nsupc, OuterStride<>(lda) );
0853         Map< Matrix<Scalar,Dynamic,Dest::ColsAtCompileTime, ColMajor>, 0, OuterStride<> > U (&(X.coeffRef(fsupc,0)), nsupc, nrhs, OuterStride<>(n) );
0854         U = A.template triangularView<Upper>().solve(U);
0855       }
0856 
0857       for (Index j = 0; j < nrhs; ++j)
0858       {
0859         for (Index jcol = fsupc; jcol < fsupc + nsupc; jcol++)
0860         {
0861           typename MatrixUType::InnerIterator it(m_mapU, jcol);
0862           for ( ; it; ++it)
0863           {
0864             Index irow = it.index();
0865             X(irow, j) -= X(jcol, j) * it.value();
0866           }
0867         }
0868       }
0869     } // End For U-solve
0870   }
0871 
0872   template<bool Conjugate, typename Dest>   void solveTransposedInPlace(MatrixBase<Dest> &X) const
0873   {
0874     using numext::conj;
0875     Index nrhs = X.cols();
0876     Index n    = X.rows();
0877     // Forward solve with U
0878     for (Index k = 0; k <=  m_mapL.nsuper(); k++)
0879     {
0880       Index fsupc = m_mapL.supToCol()[k];
0881       Index lda = m_mapL.colIndexPtr()[fsupc+1] - m_mapL.colIndexPtr()[fsupc]; // leading dimension
0882       Index nsupc = m_mapL.supToCol()[k+1] - fsupc;
0883       Index luptr = m_mapL.colIndexPtr()[fsupc];
0884 
0885       for (Index j = 0; j < nrhs; ++j)
0886       {
0887         for (Index jcol = fsupc; jcol < fsupc + nsupc; jcol++)
0888         {
0889           typename MatrixUType::InnerIterator it(m_mapU, jcol);
0890           for ( ; it; ++it)
0891           {
0892             Index irow = it.index();
0893             X(jcol, j) -= X(irow, j) * (Conjugate? conj(it.value()): it.value());
0894           }
0895         }
0896       }
0897       if (nsupc == 1)
0898       {
0899         for (Index j = 0; j < nrhs; j++)
0900         {
0901           X(fsupc, j) /= (Conjugate? conj(m_mapL.valuePtr()[luptr]) : m_mapL.valuePtr()[luptr]);
0902         }
0903       }
0904       else
0905       {
0906         Map<const Matrix<Scalar,Dynamic,Dynamic, ColMajor>, 0, OuterStride<> > A( &(m_mapL.valuePtr()[luptr]), nsupc, nsupc, OuterStride<>(lda) );
0907         Map< Matrix<Scalar,Dynamic,Dest::ColsAtCompileTime, ColMajor>, 0, OuterStride<> > U (&(X(fsupc,0)), nsupc, nrhs, OuterStride<>(n) );
0908         if(Conjugate)
0909           U = A.adjoint().template triangularView<Lower>().solve(U);
0910         else
0911           U = A.transpose().template triangularView<Lower>().solve(U);
0912       }
0913     }// End For U-solve
0914   }
0915 
0916 
0917   const MatrixLType& m_mapL;
0918   const MatrixUType& m_mapU;
0919 };
0920 
0921 } // End namespace RivetEigen 
0922 
0923 #endif