sparsmat.hxx

Go to the documentation of this file.
00001 
00002 #ifndef CH_MATRIX_CLASSES__SPARSMAT_HXX
00003 #define CH_MATRIX_CLASSES__SPARSMAT_HXX
00004 
00014 #ifndef CH_MATRIX_CLASSES__SYMMAT_HXX
00015 #include "symmat.hxx"
00016 #endif
00017 
00018 namespace CH_Matrix_Classes {
00019 
00020 //everything involving a "Sparsesym" is implemented in "sparssym.cxx/hxx"
00021 
00022 #define SPARSE_ZERO_TOL 1e-60
00023 
00026 
00072 class Sparsemat: protected Memarrayuser
00073 {
00074     friend class Indexmatrix;
00075     friend class Matrix;
00076     friend class Symmatrix;
00077     friend class Sparsesym;
00078     
00079 private:
00080 
00081     Mtype mtype;   
00082     Integer nr,    
00083             nc;    
00084 
00085     //column by column representation
00086     Indexmatrix colinfo;  
00087     Indexmatrix colindex; 
00088     Matrix colval;        
00089 
00090     //row by row representation (see column by column)
00091     Indexmatrix rowinfo; 
00092     Indexmatrix rowindex;
00093     Matrix rowval;       
00094 
00095         
00096     Real tol;          
00097 
00098     bool is_init;      
00099 
00101     void init_to_zero();          
00102 
00103 public:
00104 
00105   //----------------------------------------------------
00106   //----  constructors, destructor, and initialization
00107   //----------------------------------------------------
00108 
00112 
00114   inline Sparsemat();                      
00116   inline Sparsemat(const Sparsemat& A,Real d=1.);       
00118   inline Sparsemat(Integer nr,Integer nc);  
00120   inline Sparsemat(Integer nr,Integer nc,Integer nz,
00121                    const Integer *ini,const Integer *inj,const Real* va);
00123   inline Sparsemat(Integer nr,Integer nc,Integer nz,
00124                    const Indexmatrix& ini,const Indexmatrix& inj, const Matrix& va);
00125 
00126 #if (CONICBUNDLE_DEBUG>=1)
00128   void set_init(bool i){is_init=i;}
00130   bool get_init() const {return is_init;} 
00131 #else 
00133   void set_init(bool /* i */){}
00135   bool get_init() const {return true;}
00136 #endif 
00137 
00139   inline Sparsemat& init(const Sparsemat& A,Real d=1.);       
00141   inline Sparsemat& init(const Matrix& A,Real d=1.);          
00143   inline Sparsemat& init(const Indexmatrix& A,Real d=1.);     
00145   inline Sparsemat& init(const Symmatrix&,Real d=1.);     
00147   inline Sparsemat& init(const Sparsesym&,Real d=1.);    
00149   inline Sparsemat& init(Integer nr,Integer nc);  //zero-matrix of size nr*nc
00151   Sparsemat& init(Integer nr,Integer nc,Integer nz,
00152                   const Integer *ini,const Integer *inj,const Real* va);
00154   Sparsemat& init(Integer nr,Integer nc,Integer nz,
00155                   const Indexmatrix& ini,const Indexmatrix& inj, const Matrix& va);
00156 
00158   void set_tol(Real t){tol=t;}
00159 
00161 
00165 
00167   inline Sparsemat(const Matrix& A, Real d=1.);          
00169   inline Sparsemat(const Indexmatrix& A,Real d=1.);
00171   inline Sparsemat(const Symmatrix& A,Real d=1.); 
00173   inline Sparsemat(const Sparsesym& A,Real d=1.);
00174 
00176 
00177   //----------------------------------------------------
00178   //----  size and type information
00179   //----------------------------------------------------
00180 
00184 
00186   void dim(Integer& in_nr, Integer& in_nc) const {in_nr=nr; in_nc=nc;}
00187 
00189   Integer dim() const {return nr*nc;}
00190 
00192   Integer rowdim() const {return nr;}
00193 
00195   Integer coldim() const {return nc;}
00196 
00198   Integer nonzeros() const {return colval.dim();}
00199 
00201   Integer col_nonzeros(Integer i,Integer* startind=0) const;
00202 
00204   Integer row_nonzeros(Integer i,Integer* startind=0) const;
00205 
00207   Mtype get_mtype() const {return mtype;}
00208 
00209   
00211 
00212 
00213     
00214   //--------------------------------
00215   //----  Indexing and Submatrices
00216   //--------------------------------
00217 
00218 
00222     
00224   Real operator()(Integer i,Integer j) const;     
00225 
00227   inline Real operator()(Integer i) const;           
00228 
00230   inline Real operator[](Integer i) const; 
00231 
00233   Sparsemat col(Integer i) const;
00234 
00236   Sparsemat row(Integer i) const;
00238   Sparsemat cols(const Indexmatrix& ind) const;
00240   Sparsemat rows(const Indexmatrix& ind) const;
00241   
00243   Sparsemat& delete_rows(const Indexmatrix& ind);
00245   Sparsemat& delete_cols(const Indexmatrix& ind);
00246 
00248   Sparsemat& insert_row(Integer i,const Sparsemat& v); 
00250   Sparsemat& insert_col(Integer i,const Sparsemat& v); 
00251 
00253   Sparsemat& concat_right(const Sparsemat& A);
00255   Sparsemat& concat_below(const Sparsemat& A);
00256 
00258   const Indexmatrix& get_colinfo() const {return colinfo;}
00260   const Indexmatrix& get_colindex() const {return colindex;}
00262   const Matrix& get_colval() const {return colval;}
00263 
00265   const Indexmatrix& get_rowinfo() const {return rowinfo;}
00267   const Indexmatrix& get_rowindex() const {return rowindex;}
00269   const Matrix& get_rowval() const {return rowval;}
00270   
00272   void get_edge_rep(Indexmatrix& I, Indexmatrix& J, Matrix& val) const;
00274   int get_edge(Integer i,Integer& indi,Integer& indj, Real& val) const;
00276   int contains_support(const Sparsemat& A) const;
00277 
00278 
00280 
00281 
00285 
00287   friend inline Sparsemat concat_right(const Sparsemat& A,const Sparsemat& B);
00289   friend inline Sparsemat concat_below(const Sparsemat& A,const Sparsemat& B);
00290 
00292   friend void swap(Sparsemat& A, Sparsemat& B);
00293 
00295 
00296   //------------------------------
00297   //----  BLAS-like Routines
00298   //------------------------------
00299 
00303 
00305   Sparsemat& xeya(const Sparsemat& A,Real d=1.);
00307   Sparsemat& xeya(const Matrix& A,Real d=1.);
00309   Sparsemat& xeya(const Indexmatrix& A,Real d=1.);
00310 
00312 
00316 
00318   friend Sparsemat& xbpeya(Sparsemat& x,const Sparsemat& y,Real alpha=1.,Real beta=0.,int ytrans=0);
00319   
00321   friend Matrix& genmult(const Sparsemat& A,const Matrix& B,Matrix &C,
00322                          Real alpha=1.,Real beta=0., int atrans=0,int btrans=0);
00323       //C=beta*C+alpha A B
00324 
00326   friend Matrix& genmult(const Matrix& A,const Sparsemat& B,Matrix &C,
00327                          Real alpha=1.,Real beta=0., int atrans=0,int btrans=0);
00328       //C=beta*C+alpha A B
00329   
00331   friend Matrix& genmult(const Sparsemat& A,const Sparsemat& B,Matrix &C,
00332                          Real alpha=1.,Real beta=0., int atrans=0,int btrans=0);
00333       //C=beta*C+alpha A B
00334 
00336 
00337   //------------------------------
00338   //----  usual operators
00339   //------------------------------
00340 
00344 
00346   inline Sparsemat& operator=(const Sparsemat& A);
00348   inline Sparsemat& operator+=(const Sparsemat& A);
00350   inline Sparsemat& operator-=(const Sparsemat& A);
00352   inline Sparsemat operator-() const;
00353   
00355   inline Sparsemat& operator*=(Real d);
00357   inline Sparsemat& operator/=(Real d);
00358   
00360   Sparsemat& operator=(const Matrix& A);
00362   Sparsemat& operator=(const Indexmatrix& A);
00363 
00365   Sparsemat& transpose();          
00366    
00368 
00372   
00374   friend Sparsemat operator*(const Sparsemat& A,const Sparsemat& B);
00376   friend inline Sparsemat operator+(const Sparsemat &A,const Sparsemat& B);
00378   friend inline Sparsemat operator-(const Sparsemat &A,const Sparsemat& B);
00379 
00381   friend inline Sparsemat operator*(const Sparsemat& A,Real d);
00383   friend inline Sparsemat operator*(Real d,const Sparsemat& A);
00385   friend inline Sparsemat operator/(const Sparsemat& A,Real d);
00386   
00388   friend inline Matrix operator*(const Sparsemat& A,const Matrix& B);
00390   friend inline Matrix operator*(const Matrix& A,const Sparsemat& B);
00392   friend inline Matrix operator+(const Sparsemat& A,const Matrix& B);
00394   friend inline Matrix operator+(const Matrix& A,const Sparsemat& B);
00396   friend inline Matrix operator-(const Sparsemat& A,const Matrix& B);
00398   friend inline Matrix operator-(const Matrix& A,const Sparsemat& B);
00399 
00401   friend inline Sparsemat transpose(const Sparsemat& A);
00402 
00404 
00405   //------------------------------------------
00406   //----  Connections to other Matrix Classes
00407   //------------------------------------------
00408   
00412 
00414   Sparsemat& xeya(const Sparsesym& A,Real d=1.);
00416   inline Sparsemat& operator=(const Symmatrix& A);
00418   inline Sparsemat& operator=(const Sparsesym& A);
00419 
00421 
00425 
00427   friend Matrix& genmult(const Symmatrix& A,const Sparsemat& B,Matrix& C,
00428                          Real alpha,Real beta,int btrans);
00429       //returns C=beta*C+alpha*A*B, where A and B may be transposed
00430 
00432   friend Matrix& genmult(const Sparsemat& A,const Symmatrix& B,Matrix& C,
00433                          Real alpha,Real beta, int atrans);
00434 
00436   friend Matrix& genmult(const Sparsesym& A,const Sparsemat& B,Matrix &C,
00437                          Real alpha,Real beta, int btrans);
00438 
00440   friend Matrix& genmult(const Sparsemat& A,const Sparsesym& B,Matrix &C,
00441                          Real alpha,Real beta,int atrans);
00442 
00444   friend Symmatrix& rankadd(const Sparsemat& A,Symmatrix& C,
00445                             Real alpha,Real beta,int trans);
00446       // returns C=beta*C+alpha* A*A^T, where A may be transposed
00447     
00449   friend Symmatrix& rank2add(const Sparsemat& A,const Matrix& B,Symmatrix& C,
00450                              Real alpha,Real beta,int trans);
00451       // returns C=beta*C+alpha* sym(A*B^T) [or sym(A^T*B)]
00452 
00454   friend inline Matrix operator*(const Symmatrix& A,const Sparsemat& B);
00456   friend inline Matrix operator*(const Sparsemat& A,const Symmatrix& B);
00457 
00459 
00460   //------------------------------
00461   //----  Elementwise Operations
00462   //------------------------------
00463 
00467 
00469   friend Sparsemat abs(const Sparsemat& A);                 
00470 
00472 
00473   //----------------------------
00474   //----  Numerical Methods
00475   //----------------------------
00476 
00480 
00482   Sparsemat& scale_rows(const Matrix& vec);    //A=diag(vec)*A
00484   Sparsemat& scale_cols(const Matrix& vec);    //A=A*diag(vec)
00485 
00486 
00488 
00492 
00494   friend Real trace(const Sparsemat& A);               //=sum(diag(A))
00496   friend Real ip(const Sparsemat& A, const Sparsemat& B); //=trace(B^t*A)
00498   friend Real ip(const Sparsemat& A, const Matrix& B); //=trace(B^t*A)
00500   friend inline Real ip(const Matrix& A, const Sparsemat& B);
00501 
00503   friend Matrix sumrows(const Sparsemat& A);   //=(1 1 1 ... 1)*A
00505   friend Matrix sumcols(const Sparsemat& A);   //=A*(1 1 ... 1)^t
00507   friend inline Real sum(const Sparsemat& A);  //=(1 1 ... 1)*A*(1 1 ... 1)^t
00508 
00510 
00511   //---------------------------------------------
00512   //----  Comparisons / Max / Min / sort / find
00513   //---------------------------------------------
00514 
00518 
00520   friend int equal(const Sparsemat& A, const Sparsemat& B,Real eqtol);
00521 
00523 
00524 
00525   //--------------------------------
00526   //----  Input / Output
00527   //--------------------------------
00528 
00532 
00535     void display(std::ostream& out, 
00536                  int precision=0,   
00537                  int width=0,       
00538                  int screenwidth=0  
00539                  ) const;
00540 
00542 
00546 
00548     friend std::ostream& operator<<(std::ostream& o,const Sparsemat &v);
00550     friend std::istream& operator>>(std::istream& i,Sparsemat &v);
00551 
00553 
00554 };
00555 
00557 
00558 
00559 // **************************************************************************
00560 //                make non inline friends available outside
00561 // **************************************************************************
00562 
00563   Matrix& genmult(const Sparsemat& A,const Matrix& B,Matrix &C,
00564                          Real alpha,Real beta, int atrans,int btrans);
00565   Matrix& genmult(const Matrix& A,const Sparsemat& B,Matrix &C,
00566                          Real alpha,Real beta, int atrans,int btrans);
00567   Matrix& genmult(const Sparsemat& A,const Sparsemat& B,Matrix &C,
00568                          Real alpha,Real beta, int atrans,int btrans);
00569   Sparsemat operator*(const Sparsemat& A,const Sparsemat& B);
00570   Matrix& genmult(const Symmatrix& A,const Sparsemat& B,Matrix& C,
00571                          Real alpha,Real beta,int btrans);
00572   Matrix& genmult(const Sparsemat& A,const Symmatrix& B,Matrix& C,
00573                          Real alpha,Real beta, int atrans);
00574   Symmatrix& rankadd(const Sparsemat& A,Symmatrix& C,
00575                             Real alpha,Real beta,int trans);
00576   Symmatrix& rank2add(const Sparsemat& A,const Matrix& B,Symmatrix& C,
00577                              Real alpha,Real beta,int trans);
00578   Matrix& genmult(const Sparsesym& A,const Sparsemat& B,Matrix &C,
00579                          Real alpha,Real beta, int btrans);
00580   Matrix& genmult(const Sparsemat& A,const Sparsesym& B,Matrix &C,
00581                          Real alpha,Real beta,int atrans);
00582   Sparsemat abs(const Sparsemat& A);
00583   Real trace(const Sparsemat& A);               //=sum(diag(A))
00584   Real ip(const Sparsemat& A, const Sparsemat& B); //=trace(B^t*A)
00585   Real ip(const Sparsemat& A, const Matrix& B); //=trace(B^t*A)
00586   Matrix sumrows(const Sparsemat& A);   //=(1 1 1 ... 1)*A
00587   Matrix sumcols(const Sparsemat& A);   //=A*(1 1 ... 1)^t
00588   std::ostream& operator<<(std::ostream& o,const Sparsemat &v);
00589   std::istream& operator>>(std::istream& i,Sparsemat &v);
00590 
00591 // **************************************************************************
00592 //                   implementation of inline functions
00593 // **************************************************************************
00594 
00595 inline void Sparsemat::init_to_zero()
00596 {
00597  mtype=MTsparse;
00598  nr=nc=0;
00599  tol=SPARSE_ZERO_TOL;
00600 #if (CONICBUNDLE_DEBUG>=1)
00601  is_init=1;
00602 #endif
00603 }
00604 
00605 //initialize
00606 
00607 inline Sparsemat& Sparsemat::init(const Sparsemat& A,Real d)
00608 { return xeya(A,d);}
00609 inline Sparsemat& Sparsemat::init(const Matrix& A,Real d)
00610 { return xeya(A,d);}
00611 inline Sparsemat& Sparsemat::init(const Indexmatrix& A,Real d)
00612 { return xeya(A,d);}
00613 
00614 
00615 inline Sparsemat& Sparsemat::init(Integer r,Integer c)
00616 {
00617 chk_range(r,c,-1,-1); 
00618 nr=r; nc=c;
00619 colinfo.init(0,0,Integer(0)); colindex.init(0,0,Integer(0)); colval.init(0,0,0.);
00620 rowinfo.init(0,0,Integer(0)); rowindex.init(0,0,Integer(0)); rowval.init(0,0,0.);
00621 chk_set_init(*this,1);
00622 return *this;
00623 }
00624 
00625 inline Sparsemat::Sparsemat()
00626 { init_to_zero(); chk_set_init(*this,1);}
00627 
00628   inline Sparsemat::Sparsemat(const Sparsemat& A,double d):Memarrayuser()
00629 { init_to_zero(); xeya(A,d);}
00630 inline Sparsemat::Sparsemat(const Matrix& A,double d)
00631 { init_to_zero(); xeya(A,d);}
00632 inline Sparsemat::Sparsemat(const Indexmatrix& A,double d)
00633 { init_to_zero(); xeya(A,d);}
00634 
00635 inline Sparsemat::Sparsemat(Integer r,Integer c)
00636 { init_to_zero(); init(r,c); }
00637 
00638 inline Sparsemat::Sparsemat(Integer in_nr,Integer in_nc,Integer nz,
00639        const Integer *ini,const Integer *inj,const Real* va)
00640 { init_to_zero(); init(in_nr,in_nc,nz,ini,inj,va);}
00641 
00642 inline Sparsemat::Sparsemat(Integer in_nr,Integer in_nc,Integer nz,
00643               const Indexmatrix& ini,const Indexmatrix& inj, const Matrix& va)
00644 { init_to_zero(); init(in_nr,in_nc,nz,ini,inj,va);}
00645 
00646 inline Real Sparsemat::operator()(Integer i) const
00647 {return (*this)(i%nr,i/nr);}           
00648 inline Real Sparsemat::operator[](Integer i) const
00649 {return (*this)(i%nr,i/nr);}
00650     
00651 inline Sparsemat& Sparsemat::transpose()
00652 {chk_init(*this);Integer h=nr; nr=nc;nc=h;swap(colinfo,rowinfo);
00653  swap(colindex,rowindex);swap(colval,rowval);return *this;}          
00654     
00655 inline Sparsemat concat_right(const Sparsemat& A,const Sparsemat& B)
00656 {Sparsemat C(A);C.concat_right(B);return C;}
00657 inline Sparsemat concat_below(const Sparsemat& A,const Sparsemat& B)
00658 {Sparsemat C(A);C.concat_below(B);return C;}
00659 
00660 
00661 inline Sparsemat& Sparsemat::operator=(const Sparsemat &A)
00662 { return xeya(A); }
00663 inline Sparsemat& Sparsemat::operator+=(const Sparsemat &A)
00664 { return xbpeya(*this,A,1.,1.); }
00665 inline Sparsemat& Sparsemat::operator-=(const Sparsemat &A)
00666 { return xbpeya(*this,A,-1.,1.); }
00667 inline Sparsemat& Sparsemat::operator*=(Real d)
00668 {chk_init(*this); colval*=d; rowval*=d; return *this;}
00669 inline Sparsemat& Sparsemat::operator/=(Real d)
00670 {chk_init(*this);colval/=d;rowval/=d;return *this;}
00671 inline Sparsemat Sparsemat::operator-() const
00672 {return Sparsemat(*this,-1.); }
00673 inline Sparsemat operator+(const Sparsemat &A,const Sparsemat& B)
00674 { Sparsemat C(A); return xbpeya(C,B,1.,1.); }
00675 inline Sparsemat operator-(const Sparsemat &A,const Sparsemat& B)
00676 { Sparsemat C(A); return xbpeya(C,B,-1.,1.); }
00677 inline Sparsemat operator*(const Sparsemat& A,Real d)
00678 {return Sparsemat(A,d);}
00679 inline Sparsemat operator*(Real d,const Sparsemat& A) 
00680 {return Sparsemat(A,d);}
00681 inline Sparsemat operator/(const Sparsemat& A,Real d) 
00682 {return Sparsemat(A,1./d);}
00683 
00684 inline Sparsemat& Sparsemat::operator=(const Matrix &A)
00685 {return xeya(A);}
00686 inline Sparsemat& Sparsemat::operator=(const Indexmatrix &A)
00687 {return xeya(A);}
00688 
00689 
00690 inline Matrix operator*(const Sparsemat& A,const Matrix& B) 
00691     {Matrix C;return genmult(A,B,C);}
00692 inline Matrix operator*(const Matrix& A,const Sparsemat& B) 
00693     {Matrix C;return genmult(A,B,C);}
00694 
00695 inline Matrix operator+(const Sparsemat& A,const Matrix& B)
00696     {Matrix C(B);return C.xpeya(A);}
00697 inline Matrix operator+(const Matrix& A,const Sparsemat& B)
00698     {Matrix C(A);return C.xpeya(B);}
00699 inline Matrix operator-(const Sparsemat& A,const Matrix& B)
00700     {Matrix C(B,-1.);return C.xpeya(A);}
00701 inline Matrix operator-(const Matrix& A,const Sparsemat& B)
00702     {Matrix C(A);return C.xpeya(B,-1.);}
00703 
00704 
00705 inline Sparsemat::Sparsemat(const Symmatrix& A,Real d)
00706 { init_to_zero(); xeya(Matrix(A),d);}
00707 inline Sparsemat& Sparsemat::init(const Symmatrix &A,Real d)
00708 {return xeya(Matrix(A),d);}
00709 inline Sparsemat& Sparsemat::operator=(const Symmatrix &A)
00710 {return xeya(Matrix(A));}
00711 
00712 inline Real ip(const Matrix& A, const Sparsemat& B)
00713 {return ip(B,A);}
00714 
00715 inline Real sum(const Sparsemat& A)
00716 {return sum(A.colval);}
00717 
00718 inline Sparsemat transpose(const Sparsemat& A)
00719 {Sparsemat B(A); B.transpose(); return B;}
00720 
00721 
00722 
00723 inline Matrix::Matrix(const Sparsemat& A, Real d)        
00724 {init_to_zero(); xeya(A,d);}
00725 
00726 inline Matrix& Matrix::init(const Sparsemat& A, Real d)  
00727 {return xeya(A,d);}
00728 
00729 inline Matrix& Matrix::operator=(const Sparsemat & A)       
00730 {return xeya(A);}
00731 
00732 inline Matrix& Matrix::operator*=(const Sparsemat &A)
00733 { Matrix C; return *this=genmult(*this,A,C);}
00734 
00735 inline Matrix& Matrix::operator+=(const Sparsemat &A)       
00736 {return xpeya(A);}
00737 
00738 inline Matrix& Matrix::operator-=(const Sparsemat &A)       
00739 {return xpeya(A,-1.);}
00740 
00741 
00745 
00747   int equal(const Sparsemat& A, const Sparsemat& B,Real eqtol=1e-10);
00748 
00750 
00751 
00752 }
00753 
00754 //in order to define the inlines associated with the other matrix
00755 //classes, the hxx-files of these are included here, as well. 
00756 
00757 #ifndef CH_MATRIX_CLASSES__SPARSSYM_HXX
00758 #include "sparssym.hxx"
00759 #endif
00760 
00761 #endif
00762 

Generated on Tue May 3 16:52:53 2011 for ConicBundle by  doxygen 1.5.6