$treeview $search $mathjax
Eigen  3.2.3
$projectbrief
$projectbrief
$searchbox

SuiteSparseQRSupport.h

00001 // This file is part of Eigen, a lightweight C++ template library
00002 // for linear algebra.
00003 //
00004 // Copyright (C) 2012 Desire Nuentsa <desire.nuentsa_wakam@inria.fr>
00005 //
00006 // This Source Code Form is subject to the terms of the Mozilla
00007 // Public License v. 2.0. If a copy of the MPL was not distributed
00008 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
00009 
00010 #ifndef EIGEN_SUITESPARSEQRSUPPORT_H
00011 #define EIGEN_SUITESPARSEQRSUPPORT_H
00012 
00013 namespace Eigen {
00014   
00015   template<typename MatrixType> class SPQR; 
00016   template<typename SPQRType> struct SPQRMatrixQReturnType; 
00017   template<typename SPQRType> struct SPQRMatrixQTransposeReturnType; 
00018   template <typename SPQRType, typename Derived> struct SPQR_QProduct;
00019   namespace internal {
00020     template <typename SPQRType> struct traits<SPQRMatrixQReturnType<SPQRType> >
00021     {
00022       typedef typename SPQRType::MatrixType ReturnType;
00023     };
00024     template <typename SPQRType> struct traits<SPQRMatrixQTransposeReturnType<SPQRType> >
00025     {
00026       typedef typename SPQRType::MatrixType ReturnType;
00027     };
00028     template <typename SPQRType, typename Derived> struct traits<SPQR_QProduct<SPQRType, Derived> >
00029     {
00030       typedef typename Derived::PlainObject ReturnType;
00031     };
00032   } // End namespace internal
00033   
00056 template<typename _MatrixType>
00057 class SPQR
00058 {
00059   public:
00060     typedef typename _MatrixType::Scalar Scalar;
00061     typedef typename _MatrixType::RealScalar RealScalar;
00062     typedef UF_long Index ; 
00063     typedef SparseMatrix<Scalar, ColMajor, Index> MatrixType;
00064     typedef PermutationMatrix<Dynamic, Dynamic> PermutationType;
00065   public:
00066     SPQR() 
00067       : m_isInitialized(false),
00068       m_ordering(SPQR_ORDERING_DEFAULT),
00069       m_allow_tol(SPQR_DEFAULT_TOL),
00070       m_tolerance (NumTraits<Scalar>::epsilon())
00071     { 
00072       cholmod_l_start(&m_cc);
00073     }
00074     
00075     SPQR(const _MatrixType& matrix) 
00076     : m_isInitialized(false),
00077       m_ordering(SPQR_ORDERING_DEFAULT),
00078       m_allow_tol(SPQR_DEFAULT_TOL),
00079       m_tolerance (NumTraits<Scalar>::epsilon())
00080     {
00081       cholmod_l_start(&m_cc);
00082       compute(matrix);
00083     }
00084     
00085     ~SPQR()
00086     {
00087       SPQR_free();
00088       cholmod_l_finish(&m_cc);
00089     }
00090     void SPQR_free()
00091     {
00092       cholmod_l_free_sparse(&m_H, &m_cc);
00093       cholmod_l_free_sparse(&m_cR, &m_cc);
00094       cholmod_l_free_dense(&m_HTau, &m_cc);
00095       std::free(m_E);
00096       std::free(m_HPinv);
00097     }
00098 
00099     void compute(const _MatrixType& matrix)
00100     {
00101       if(m_isInitialized) SPQR_free();
00102 
00103       MatrixType mat(matrix);
00104       cholmod_sparse A; 
00105       A = viewAsCholmod(mat);
00106       Index col = matrix.cols();
00107       m_rank = SuiteSparseQR<Scalar>(m_ordering, m_tolerance, col, &A, 
00108                              &m_cR, &m_E, &m_H, &m_HPinv, &m_HTau, &m_cc);
00109 
00110       if (!m_cR)
00111       {
00112         m_info = NumericalIssue; 
00113         m_isInitialized = false;
00114         return;
00115       }
00116       m_info = Success;
00117       m_isInitialized = true;
00118       m_isRUpToDate = false;
00119     }
00123     inline Index rows() const {return m_H->nrow; }
00124     
00128     inline Index cols() const { return m_cR->ncol; }
00129    
00134     template<typename Rhs>
00135     inline const internal::solve_retval<SPQR, Rhs> solve(const MatrixBase<Rhs>& B) const 
00136     {
00137       eigen_assert(m_isInitialized && " The QR factorization should be computed first, call compute()");
00138       eigen_assert(this->rows()==B.rows()
00139                     && "SPQR::solve(): invalid number of rows of the right hand side matrix B");
00140           return internal::solve_retval<SPQR, Rhs>(*this, B.derived());
00141     }
00142     
00143     template<typename Rhs, typename Dest>
00144     void _solve(const MatrixBase<Rhs> &b, MatrixBase<Dest> &dest) const
00145     {
00146       eigen_assert(m_isInitialized && " The QR factorization should be computed first, call compute()");
00147       eigen_assert(b.cols()==1 && "This method is for vectors only");
00148       
00149       //Compute Q^T * b
00150       typename Dest::PlainObject y;
00151       y = matrixQ().transpose() * b;
00152         // Solves with the triangular matrix R
00153       Index rk = this->rank();
00154       y.topRows(rk) = this->matrixR().topLeftCorner(rk, rk).template triangularView<Upper>().solve(y.topRows(rk));
00155       y.bottomRows(cols()-rk).setZero();
00156       // Apply the column permutation 
00157       dest.topRows(cols()) = colsPermutation() * y.topRows(cols());
00158       
00159       m_info = Success;
00160     }
00161     
00164     const MatrixType matrixR() const
00165     {
00166       eigen_assert(m_isInitialized && " The QR factorization should be computed first, call compute()");
00167       if(!m_isRUpToDate) {
00168         m_R = viewAsEigen<Scalar,ColMajor, typename MatrixType::Index>(*m_cR);
00169         m_isRUpToDate = true;
00170       }
00171       return m_R;
00172     }
00174     SPQRMatrixQReturnType<SPQR> matrixQ() const
00175     {
00176       return SPQRMatrixQReturnType<SPQR>(*this);
00177     }
00179     PermutationType colsPermutation() const
00180     { 
00181       eigen_assert(m_isInitialized && "Decomposition is not initialized.");
00182       Index n = m_cR->ncol;
00183       PermutationType colsPerm(n);
00184       for(Index j = 0; j <n; j++) colsPerm.indices()(j) = m_E[j];
00185       return colsPerm; 
00186       
00187     }
00192     Index rank() const
00193     {
00194       eigen_assert(m_isInitialized && "Decomposition is not initialized.");
00195       return m_cc.SPQR_istat[4];
00196     }
00198     void setSPQROrdering(int ord) { m_ordering = ord;}
00200     void setPivotThreshold(const RealScalar& tol) { m_tolerance = tol; }
00201     
00203     cholmod_common *cholmodCommon() const { return &m_cc; }
00204     
00205     
00211     ComputationInfo info() const
00212     {
00213       eigen_assert(m_isInitialized && "Decomposition is not initialized.");
00214       return m_info;
00215     }
00216   protected:
00217     bool m_isInitialized;
00218     bool m_analysisIsOk;
00219     bool m_factorizationIsOk;
00220     mutable bool m_isRUpToDate;
00221     mutable ComputationInfo m_info;
00222     int m_ordering; // Ordering method to use, see SPQR's manual
00223     int m_allow_tol; // Allow to use some tolerance during numerical factorization.
00224     RealScalar m_tolerance; // treat columns with 2-norm below this tolerance as zero
00225     mutable cholmod_sparse *m_cR; // The sparse R factor in cholmod format
00226     mutable MatrixType m_R; // The sparse matrix R in Eigen format
00227     mutable Index *m_E; // The permutation applied to columns
00228     mutable cholmod_sparse *m_H;  //The householder vectors
00229     mutable Index *m_HPinv; // The row permutation of H
00230     mutable cholmod_dense *m_HTau; // The Householder coefficients
00231     mutable Index m_rank; // The rank of the matrix
00232     mutable cholmod_common m_cc; // Workspace and parameters
00233     template<typename ,typename > friend struct SPQR_QProduct;
00234 };
00235 
00236 template <typename SPQRType, typename Derived>
00237 struct SPQR_QProduct : ReturnByValue<SPQR_QProduct<SPQRType,Derived> >
00238 {
00239   typedef typename SPQRType::Scalar Scalar;
00240   typedef typename SPQRType::Index Index;
00241   //Define the constructor to get reference to argument types
00242   SPQR_QProduct(const SPQRType& spqr, const Derived& other, bool transpose) : m_spqr(spqr),m_other(other),m_transpose(transpose) {}
00243   
00244   inline Index rows() const { return m_transpose ? m_spqr.rows() : m_spqr.cols(); }
00245   inline Index cols() const { return m_other.cols(); }
00246   // Assign to a vector
00247   template<typename ResType>
00248   void evalTo(ResType& res) const
00249   {
00250     cholmod_dense y_cd;
00251     cholmod_dense *x_cd; 
00252     int method = m_transpose ? SPQR_QTX : SPQR_QX; 
00253     cholmod_common *cc = m_spqr.cholmodCommon();
00254     y_cd = viewAsCholmod(m_other.const_cast_derived());
00255     x_cd = SuiteSparseQR_qmult<Scalar>(method, m_spqr.m_H, m_spqr.m_HTau, m_spqr.m_HPinv, &y_cd, cc);
00256     res = Matrix<Scalar,ResType::RowsAtCompileTime,ResType::ColsAtCompileTime>::Map(reinterpret_cast<Scalar*>(x_cd->x), x_cd->nrow, x_cd->ncol);
00257     cholmod_l_free_dense(&x_cd, cc);
00258   }
00259   const SPQRType& m_spqr; 
00260   const Derived& m_other; 
00261   bool m_transpose; 
00262   
00263 };
00264 template<typename SPQRType>
00265 struct SPQRMatrixQReturnType{
00266   
00267   SPQRMatrixQReturnType(const SPQRType& spqr) : m_spqr(spqr) {}
00268   template<typename Derived>
00269   SPQR_QProduct<SPQRType, Derived> operator*(const MatrixBase<Derived>& other)
00270   {
00271     return SPQR_QProduct<SPQRType,Derived>(m_spqr,other.derived(),false);
00272   }
00273   SPQRMatrixQTransposeReturnType<SPQRType> adjoint() const
00274   {
00275     return SPQRMatrixQTransposeReturnType<SPQRType>(m_spqr);
00276   }
00277   // To use for operations with the transpose of Q
00278   SPQRMatrixQTransposeReturnType<SPQRType> transpose() const
00279   {
00280     return SPQRMatrixQTransposeReturnType<SPQRType>(m_spqr);
00281   }
00282   const SPQRType& m_spqr;
00283 };
00284 
00285 template<typename SPQRType>
00286 struct SPQRMatrixQTransposeReturnType{
00287   SPQRMatrixQTransposeReturnType(const SPQRType& spqr) : m_spqr(spqr) {}
00288   template<typename Derived>
00289   SPQR_QProduct<SPQRType,Derived> operator*(const MatrixBase<Derived>& other)
00290   {
00291     return SPQR_QProduct<SPQRType,Derived>(m_spqr,other.derived(), true);
00292   }
00293   const SPQRType& m_spqr;
00294 };
00295 
00296 namespace internal {
00297   
00298 template<typename _MatrixType, typename Rhs>
00299 struct solve_retval<SPQR<_MatrixType>, Rhs>
00300   : solve_retval_base<SPQR<_MatrixType>, Rhs>
00301 {
00302   typedef SPQR<_MatrixType> Dec;
00303   EIGEN_MAKE_SOLVE_HELPERS(Dec,Rhs)
00304 
00305   template<typename Dest> void evalTo(Dest& dst) const
00306   {
00307     dec()._solve(rhs(),dst);
00308   }
00309 };
00310 
00311 } // end namespace internal
00312 
00313 }// End namespace Eigen
00314 #endif