UmfPackSupport.h
Go to the documentation of this file.
00001 // This file is part of Eigen, a lightweight C++ template library
00002 // for linear algebra.
00003 //
00004 // Copyright (C) 2008-2011 Gael Guennebaud <gael.guennebaud@inria.fr>
00005 //
00006 // Eigen is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 3 of the License, or (at your option) any later version.
00010 //
00011 // Alternatively, you can redistribute it and/or
00012 // modify it under the terms of the GNU General Public License as
00013 // published by the Free Software Foundation; either version 2 of
00014 // the License, or (at your option) any later version.
00015 //
00016 // Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
00017 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00018 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
00019 // GNU General Public License for more details.
00020 //
00021 // You should have received a copy of the GNU Lesser General Public
00022 // License and a copy of the GNU General Public License along with
00023 // Eigen. If not, see <http://www.gnu.org/licenses/>.
00024 
00025 #ifndef EIGEN_UMFPACKSUPPORT_H
00026 #define EIGEN_UMFPACKSUPPORT_H
00027 
00028 namespace Eigen { 
00029 
00030 /* TODO extract L, extract U, compute det, etc... */
00031 
00032 // generic double/complex<double> wrapper functions:
00033 
00034 inline void umfpack_free_numeric(void **Numeric, double)
00035 { umfpack_di_free_numeric(Numeric); *Numeric = 0; }
00036 
00037 inline void umfpack_free_numeric(void **Numeric, std::complex<double>)
00038 { umfpack_zi_free_numeric(Numeric); *Numeric = 0; }
00039 
00040 inline void umfpack_free_symbolic(void **Symbolic, double)
00041 { umfpack_di_free_symbolic(Symbolic); *Symbolic = 0; }
00042 
00043 inline void umfpack_free_symbolic(void **Symbolic, std::complex<double>)
00044 { umfpack_zi_free_symbolic(Symbolic); *Symbolic = 0; }
00045 
00046 inline int umfpack_symbolic(int n_row,int n_col,
00047                             const int Ap[], const int Ai[], const double Ax[], void **Symbolic,
00048                             const double Control [UMFPACK_CONTROL], double Info [UMFPACK_INFO])
00049 {
00050   return umfpack_di_symbolic(n_row,n_col,Ap,Ai,Ax,Symbolic,Control,Info);
00051 }
00052 
00053 inline int umfpack_symbolic(int n_row,int n_col,
00054                             const int Ap[], const int Ai[], const std::complex<double> Ax[], void **Symbolic,
00055                             const double Control [UMFPACK_CONTROL], double Info [UMFPACK_INFO])
00056 {
00057   return umfpack_zi_symbolic(n_row,n_col,Ap,Ai,&internal::real_ref(Ax[0]),0,Symbolic,Control,Info);
00058 }
00059 
00060 inline int umfpack_numeric( const int Ap[], const int Ai[], const double Ax[],
00061                             void *Symbolic, void **Numeric,
00062                             const double Control[UMFPACK_CONTROL],double Info [UMFPACK_INFO])
00063 {
00064   return umfpack_di_numeric(Ap,Ai,Ax,Symbolic,Numeric,Control,Info);
00065 }
00066 
00067 inline int umfpack_numeric( const int Ap[], const int Ai[], const std::complex<double> Ax[],
00068                             void *Symbolic, void **Numeric,
00069                             const double Control[UMFPACK_CONTROL],double Info [UMFPACK_INFO])
00070 {
00071   return umfpack_zi_numeric(Ap,Ai,&internal::real_ref(Ax[0]),0,Symbolic,Numeric,Control,Info);
00072 }
00073 
00074 inline int umfpack_solve( int sys, const int Ap[], const int Ai[], const double Ax[],
00075                           double X[], const double B[], void *Numeric,
00076                           const double Control[UMFPACK_CONTROL], double Info[UMFPACK_INFO])
00077 {
00078   return umfpack_di_solve(sys,Ap,Ai,Ax,X,B,Numeric,Control,Info);
00079 }
00080 
00081 inline int umfpack_solve( int sys, const int Ap[], const int Ai[], const std::complex<double> Ax[],
00082                           std::complex<double> X[], const std::complex<double> B[], void *Numeric,
00083                           const double Control[UMFPACK_CONTROL], double Info[UMFPACK_INFO])
00084 {
00085   return umfpack_zi_solve(sys,Ap,Ai,&internal::real_ref(Ax[0]),0,&internal::real_ref(X[0]),0,&internal::real_ref(B[0]),0,Numeric,Control,Info);
00086 }
00087 
00088 inline int umfpack_get_lunz(int *lnz, int *unz, int *n_row, int *n_col, int *nz_udiag, void *Numeric, double)
00089 {
00090   return umfpack_di_get_lunz(lnz,unz,n_row,n_col,nz_udiag,Numeric);
00091 }
00092 
00093 inline int umfpack_get_lunz(int *lnz, int *unz, int *n_row, int *n_col, int *nz_udiag, void *Numeric, std::complex<double>)
00094 {
00095   return umfpack_zi_get_lunz(lnz,unz,n_row,n_col,nz_udiag,Numeric);
00096 }
00097 
00098 inline int umfpack_get_numeric(int Lp[], int Lj[], double Lx[], int Up[], int Ui[], double Ux[],
00099                                int P[], int Q[], double Dx[], int *do_recip, double Rs[], void *Numeric)
00100 {
00101   return umfpack_di_get_numeric(Lp,Lj,Lx,Up,Ui,Ux,P,Q,Dx,do_recip,Rs,Numeric);
00102 }
00103 
00104 inline int umfpack_get_numeric(int Lp[], int Lj[], std::complex<double> Lx[], int Up[], int Ui[], std::complex<double> Ux[],
00105                                int P[], int Q[], std::complex<double> Dx[], int *do_recip, double Rs[], void *Numeric)
00106 {
00107   double& lx0_real = internal::real_ref(Lx[0]);
00108   double& ux0_real = internal::real_ref(Ux[0]);
00109   double& dx0_real = internal::real_ref(Dx[0]);
00110   return umfpack_zi_get_numeric(Lp,Lj,Lx?&lx0_real:0,0,Up,Ui,Ux?&ux0_real:0,0,P,Q,
00111                                 Dx?&dx0_real:0,0,do_recip,Rs,Numeric);
00112 }
00113 
00114 inline int umfpack_get_determinant(double *Mx, double *Ex, void *NumericHandle, double User_Info [UMFPACK_INFO])
00115 {
00116   return umfpack_di_get_determinant(Mx,Ex,NumericHandle,User_Info);
00117 }
00118 
00119 inline int umfpack_get_determinant(std::complex<double> *Mx, double *Ex, void *NumericHandle, double User_Info [UMFPACK_INFO])
00120 {
00121   double& mx_real = internal::real_ref(*Mx);
00122   return umfpack_zi_get_determinant(&mx_real,0,Ex,NumericHandle,User_Info);
00123 }
00124 
00138 template<typename _MatrixType>
00139 class UmfPackLU : internal::noncopyable
00140 {
00141   public:
00142     typedef _MatrixType MatrixType;
00143     typedef typename MatrixType::Scalar Scalar;
00144     typedef typename MatrixType::RealScalar RealScalar;
00145     typedef typename MatrixType::Index Index;
00146     typedef Matrix<Scalar,Dynamic,1> Vector;
00147     typedef Matrix<int, 1, MatrixType::ColsAtCompileTime> IntRowVectorType;
00148     typedef Matrix<int, MatrixType::RowsAtCompileTime, 1> IntColVectorType;
00149     typedef SparseMatrix<Scalar> LUMatrixType;
00150     typedef SparseMatrix<Scalar,ColMajor,int> UmfpackMatrixType;
00151 
00152   public:
00153 
00154     UmfPackLU() { init(); }
00155 
00156     UmfPackLU(const MatrixType& matrix)
00157     {
00158       init();
00159       compute(matrix);
00160     }
00161 
00162     ~UmfPackLU()
00163     {
00164       if(m_symbolic) umfpack_free_symbolic(&m_symbolic,Scalar());
00165       if(m_numeric)  umfpack_free_numeric(&m_numeric,Scalar());
00166     }
00167 
00168     inline Index rows() const { return m_copyMatrix.rows(); }
00169     inline Index cols() const { return m_copyMatrix.cols(); }
00170 
00176     ComputationInfo info() const
00177     {
00178       eigen_assert(m_isInitialized && "Decomposition is not initialized.");
00179       return m_info;
00180     }
00181 
00182     inline const LUMatrixType& matrixL() const
00183     {
00184       if (m_extractedDataAreDirty) extractData();
00185       return m_l;
00186     }
00187 
00188     inline const LUMatrixType& matrixU() const
00189     {
00190       if (m_extractedDataAreDirty) extractData();
00191       return m_u;
00192     }
00193 
00194     inline const IntColVectorType& permutationP() const
00195     {
00196       if (m_extractedDataAreDirty) extractData();
00197       return m_p;
00198     }
00199 
00200     inline const IntRowVectorType& permutationQ() const
00201     {
00202       if (m_extractedDataAreDirty) extractData();
00203       return m_q;
00204     }
00205 
00210     void compute(const MatrixType& matrix)
00211     {
00212       analyzePattern(matrix);
00213       factorize(matrix);
00214     }
00215 
00220     template<typename Rhs>
00221     inline const internal::solve_retval<UmfPackLU, Rhs> solve(const MatrixBase<Rhs>& b) const
00222     {
00223       eigen_assert(m_isInitialized && "UmfPackLU is not initialized.");
00224       eigen_assert(rows()==b.rows()
00225                 && "UmfPackLU::solve(): invalid number of rows of the right hand side matrix b");
00226       return internal::solve_retval<UmfPackLU, Rhs>(*this, b.derived());
00227     }
00228 
00233 //     template<typename Rhs>
00234 //     inline const internal::sparse_solve_retval<UmfPAckLU, Rhs> solve(const SparseMatrixBase<Rhs>& b) const
00235 //     {
00236 //       eigen_assert(m_isInitialized && "UmfPAckLU is not initialized.");
00237 //       eigen_assert(rows()==b.rows()
00238 //                 && "UmfPAckLU::solve(): invalid number of rows of the right hand side matrix b");
00239 //       return internal::sparse_solve_retval<UmfPAckLU, Rhs>(*this, b.derived());
00240 //     }
00241 
00248     void analyzePattern(const MatrixType& matrix)
00249     {
00250       if(m_symbolic)
00251         umfpack_free_symbolic(&m_symbolic,Scalar());
00252       if(m_numeric)
00253         umfpack_free_numeric(&m_numeric,Scalar());
00254       
00255       grapInput(matrix);
00256 
00257       int errorCode = 0;
00258       errorCode = umfpack_symbolic(matrix.rows(), matrix.cols(), m_outerIndexPtr, m_innerIndexPtr, m_valuePtr,
00259                                    &m_symbolic, 0, 0);
00260 
00261       m_isInitialized = true;
00262       m_info = errorCode ? InvalidInput : Success;
00263       m_analysisIsOk = true;
00264       m_factorizationIsOk = false;
00265     }
00266 
00273     void factorize(const MatrixType& matrix)
00274     {
00275       eigen_assert(m_analysisIsOk && "UmfPackLU: you must first call analyzePattern()");
00276       if(m_numeric)
00277         umfpack_free_numeric(&m_numeric,Scalar());
00278 
00279       grapInput(matrix);
00280 
00281       int errorCode;
00282       errorCode = umfpack_numeric(m_outerIndexPtr, m_innerIndexPtr, m_valuePtr,
00283                                   m_symbolic, &m_numeric, 0, 0);
00284 
00285       m_info = errorCode ? NumericalIssue : Success;
00286       m_factorizationIsOk = true;
00287     }
00288 
00289     #ifndef EIGEN_PARSED_BY_DOXYGEN
00290 
00291     template<typename BDerived,typename XDerived>
00292     bool _solve(const MatrixBase<BDerived> &b, MatrixBase<XDerived> &x) const;
00293     #endif
00294 
00295     Scalar determinant() const;
00296 
00297     void extractData() const;
00298 
00299   protected:
00300 
00301 
00302     void init()
00303     {
00304       m_info = InvalidInput;
00305       m_isInitialized = false;
00306       m_numeric = 0;
00307       m_symbolic = 0;
00308       m_outerIndexPtr = 0;
00309       m_innerIndexPtr = 0;
00310       m_valuePtr      = 0;
00311     }
00312     
00313     void grapInput(const MatrixType& mat)
00314     {
00315       m_copyMatrix.resize(mat.rows(), mat.cols());
00316       if( ((MatrixType::Flags&RowMajorBit)==RowMajorBit) || sizeof(typename MatrixType::Index)!=sizeof(int) || !mat.isCompressed() )
00317       {
00318         // non supported input -> copy
00319         m_copyMatrix = mat;
00320         m_outerIndexPtr = m_copyMatrix.outerIndexPtr();
00321         m_innerIndexPtr = m_copyMatrix.innerIndexPtr();
00322         m_valuePtr      = m_copyMatrix.valuePtr();
00323       }
00324       else
00325       {
00326         m_outerIndexPtr = mat.outerIndexPtr();
00327         m_innerIndexPtr = mat.innerIndexPtr();
00328         m_valuePtr      = mat.valuePtr();
00329       }
00330     }
00331 
00332     // cached data to reduce reallocation, etc.
00333     mutable LUMatrixType m_l;
00334     mutable LUMatrixType m_u;
00335     mutable IntColVectorType m_p;
00336     mutable IntRowVectorType m_q;
00337 
00338     UmfpackMatrixType m_copyMatrix;
00339     const Scalar* m_valuePtr;
00340     const int* m_outerIndexPtr;
00341     const int* m_innerIndexPtr;
00342     void* m_numeric;
00343     void* m_symbolic;
00344 
00345     mutable ComputationInfo m_info;
00346     bool m_isInitialized;
00347     int m_factorizationIsOk;
00348     int m_analysisIsOk;
00349     mutable bool m_extractedDataAreDirty;
00350     
00351   private:
00352     UmfPackLU(UmfPackLU& ) { }
00353 };
00354 
00355 
00356 template<typename MatrixType>
00357 void UmfPackLU<MatrixType>::extractData() const
00358 {
00359   if (m_extractedDataAreDirty)
00360   {
00361     // get size of the data
00362     int lnz, unz, rows, cols, nz_udiag;
00363     umfpack_get_lunz(&lnz, &unz, &rows, &cols, &nz_udiag, m_numeric, Scalar());
00364 
00365     // allocate data
00366     m_l.resize(rows,(std::min)(rows,cols));
00367     m_l.resizeNonZeros(lnz);
00368 
00369     m_u.resize((std::min)(rows,cols),cols);
00370     m_u.resizeNonZeros(unz);
00371 
00372     m_p.resize(rows);
00373     m_q.resize(cols);
00374 
00375     // extract
00376     umfpack_get_numeric(m_l.outerIndexPtr(), m_l.innerIndexPtr(), m_l.valuePtr(),
00377                         m_u.outerIndexPtr(), m_u.innerIndexPtr(), m_u.valuePtr(),
00378                         m_p.data(), m_q.data(), 0, 0, 0, m_numeric);
00379 
00380     m_extractedDataAreDirty = false;
00381   }
00382 }
00383 
00384 template<typename MatrixType>
00385 typename UmfPackLU<MatrixType>::Scalar UmfPackLU<MatrixType>::determinant() const
00386 {
00387   Scalar det;
00388   umfpack_get_determinant(&det, 0, m_numeric, 0);
00389   return det;
00390 }
00391 
00392 template<typename MatrixType>
00393 template<typename BDerived,typename XDerived>
00394 bool UmfPackLU<MatrixType>::_solve(const MatrixBase<BDerived> &b, MatrixBase<XDerived> &x) const
00395 {
00396   const int rhsCols = b.cols();
00397   eigen_assert((BDerived::Flags&RowMajorBit)==0 && "UmfPackLU backend does not support non col-major rhs yet");
00398   eigen_assert((XDerived::Flags&RowMajorBit)==0 && "UmfPackLU backend does not support non col-major result yet");
00399 
00400   int errorCode;
00401   for (int j=0; j<rhsCols; ++j)
00402   {
00403     errorCode = umfpack_solve(UMFPACK_A,
00404         m_outerIndexPtr, m_innerIndexPtr, m_valuePtr,
00405         &x.col(j).coeffRef(0), &b.const_cast_derived().col(j).coeffRef(0), m_numeric, 0, 0);
00406     if (errorCode!=0)
00407       return false;
00408   }
00409 
00410   return true;
00411 }
00412 
00413 
00414 namespace internal {
00415 
00416 template<typename _MatrixType, typename Rhs>
00417 struct solve_retval<UmfPackLU<_MatrixType>, Rhs>
00418   : solve_retval_base<UmfPackLU<_MatrixType>, Rhs>
00419 {
00420   typedef UmfPackLU<_MatrixType> Dec;
00421   EIGEN_MAKE_SOLVE_HELPERS(Dec,Rhs)
00422 
00423   template<typename Dest> void evalTo(Dest& dst) const
00424   {
00425     dec()._solve(rhs(),dst);
00426   }
00427 };
00428 
00429 template<typename _MatrixType, typename Rhs>
00430 struct sparse_solve_retval<UmfPackLU<_MatrixType>, Rhs>
00431   : sparse_solve_retval_base<UmfPackLU<_MatrixType>, Rhs>
00432 {
00433   typedef UmfPackLU<_MatrixType> Dec;
00434   EIGEN_MAKE_SPARSE_SOLVE_HELPERS(Dec,Rhs)
00435 
00436   template<typename Dest> void evalTo(Dest& dst) const
00437   {
00438     dec()._solve(rhs(),dst);
00439   }
00440 };
00441 
00442 } // end namespace internal
00443 
00444 } // end namespace Eigen
00445 
00446 #endif // EIGEN_UMFPACKSUPPORT_H