sparssym.hxx

Go to the documentation of this file.
00001 
00002 
00003 #ifndef CH_MATRIX_CLASSES__SPARSSYM_HXX
00004 #define CH_MATRIX_CLASSES__SPARSSYM_HXX
00005 
00014 #ifndef CH_MATRIX_CLASSES__SPARSMAT_HXX
00015 #include "sparsmat.hxx"
00016 #endif
00017 
00018 namespace CH_Matrix_Classes {
00019 
00020 // **************************************************************************
00021 //                            class definition
00022 // **************************************************************************
00023 
00027 
00089 class Sparsesym: protected Memarrayuser
00090 {
00091     friend class Indexmatrix;
00092     friend class Matrix;
00093     friend class Symmatrix;
00094     friend class Sparsemat;
00095     
00096 private:
00097 
00098     Mtype mtype;   
00099     Integer nr;    
00100 
00101     //column by column representation of lower triangle
00102     Indexmatrix colinfo;  
00103     Indexmatrix colindex; 
00104     Matrix colval;        
00105     Indexmatrix suppind;  
00106     Indexmatrix suppcol;  
00107         
00108     Real tol;          
00109 
00110     bool is_init;      
00111 
00113     inline void init_to_zero();            
00114 
00116     void update_support();     
00117 
00118 public:
00119 
00120   //----------------------------------------------------
00121   //----  constructors, destructor, and initialization
00122   //----------------------------------------------------
00123 
00127 
00129   inline Sparsesym(); 
00131   inline Sparsesym(const Sparsesym& A,Real d=1.); 
00133   inline Sparsesym(Integer nr);
00135   inline Sparsesym(Integer nr,Integer nz,
00136                    const Integer *ini,const Integer *inj,const Real* va);
00138   inline Sparsesym(Integer nr,Integer nz,
00139                    const Indexmatrix& ini,const Indexmatrix& inj, const Matrix& va);
00140 
00141 #if (CONICBUNDLE_DEBUG>=1)
00143  void set_init(bool i){is_init=i;}
00145   int get_init() const {return is_init;} 
00146 #else 
00148   void set_init(bool /* i */){}
00150   bool get_init() const {return true;}
00151 #endif 
00152 
00154   inline Sparsesym& init(const Sparsesym&,Real d=1.);   
00156   inline Sparsesym& init(const Matrix&,Real d=1.);       
00158   inline Sparsesym& init(const Indexmatrix&,Real d=1.);  
00160   inline Sparsesym& init(const Symmatrix&,Real d=1.); 
00162   inline Sparsesym& init(const Sparsemat&,Real d=1.);    
00164   inline Sparsesym& init(Integer nr);     
00166   Sparsesym& init(Integer nr,Integer nz,
00167                   const Integer *ini,const Integer *inj,const Real* va);
00169   Sparsesym& init(Integer nr,Integer nz,
00170                   const Indexmatrix& ini,const Indexmatrix& inj, const Matrix& va);
00171   
00172   //initialize to the same support as A but with constant value d; the same support will be generated even for d=0. 
00173   Sparsesym& init_support(const Sparsesym& A,Real d=0.);
00174 
00176   void set_tol(Real t){tol=t;}
00177 
00178 
00180 
00184 
00186   inline Sparsesym(const Matrix&,Real d=1.);         
00188   inline Sparsesym(const Indexmatrix&,Real d=1.);    
00190   inline Sparsesym(const Symmatrix&,Real d=1.);       
00192   inline Sparsesym(const Sparsemat&,Real d=1.);
00193 
00195 
00196 
00197   //----------------------------------------------------
00198   //----  size and type information
00199   //----------------------------------------------------
00200 
00204 
00206   void dim(Integer& r, Integer& c) const {r=nr; c=nr;}
00207 
00209   Integer dim() const {return nr*nr;}
00210 
00212   Integer rowdim() const {return nr;}
00213 
00215   Integer coldim() const {return nr;}
00216 
00218   Integer nonzeros() const {return colval.dim();}
00219   
00221   Mtype get_mtype() const {return mtype;}
00222   
00224 
00225     
00226   //--------------------------------
00227   //----  Indexing and Submatrices
00228   //--------------------------------
00229 
00233     
00235   Real operator()(Integer i,Integer j) const;
00237   Real operator()(Integer i) const;    //vector of stacked columns
00238 
00240    Real operator[](Integer i) const;    //vector of stacked columns
00241 
00243   const Indexmatrix& get_colinfo() const {return colinfo;}
00244 
00246   const Indexmatrix& get_colindex() const {return colindex;}
00248   const Matrix& get_colval() const {return colval;}
00250   const Indexmatrix& get_suppind() const {return suppind;}
00252   const Indexmatrix& get_suppcol() const {return suppcol;}
00253 
00255   void get_edge_rep(Indexmatrix& I, Indexmatrix& J, Matrix& val) const;
00256 
00258   int contains_support(const Sparsesym& A) const;
00259 
00261   int check_support(Integer i,Integer j) const;
00262       
00263 
00265 
00266 
00270 
00271     
00272 
00274   friend Matrix diag(const Sparsesym& A);      //=(A(1,1),A(2,2),...)^t
00276   friend Sparsesym sparseDiag(const Matrix& A,Real tol=SPARSE_ZERO_TOL);
00277 
00279   friend void swap(Sparsesym& A, Sparsesym& B);
00280 
00282   
00283 
00284   //------------------------------
00285   //----  BLAS-like Routines
00286   //------------------------------
00287 
00291 
00293   Sparsesym& xeya(const Sparsesym& A,Real d=1.);
00295   Sparsesym& xeya(const Matrix& A,Real d=1.);
00297   Sparsesym& xeya(const Indexmatrix& A,Real d=1.);
00298 
00300 
00304 
00306   inline friend Sparsesym& xbpeya(Sparsesym& x,const Sparsesym& y,Real alpha=1.,Real beta=0.);
00307   
00309   friend Sparsesym& xeyapzb(Sparsesym& x,const Sparsesym& y,const Sparsesym& z,Real alpha=1.,Real beta=1.);
00310   
00312   friend Sparsesym& support_rankadd(const Matrix& A,Sparsesym& C,
00313                                     Real alpha=1.,Real beta=0.,int trans=0);
00314 
00316   friend Matrix& genmult(const Sparsesym& A,const Matrix& B,Matrix &C,
00317                          Real alpha=1.,Real beta=0., int btrans=0);
00318 
00320   friend Matrix& genmult(const Matrix& A,const Sparsesym& B,Matrix &C,
00321                          Real alpha=1.,Real beta=0., int atrans=0);
00322 
00324   
00325 
00326   //------------------------------
00327   //----  usual operators
00328   //------------------------------
00329 
00333 
00335   inline Sparsesym& operator=(const Sparsesym &A);
00337   inline Sparsesym& operator+=(const Sparsesym &v);
00339   inline Sparsesym& operator-=(const Sparsesym &v);
00341   inline Sparsesym operator-() const;
00342  
00344   inline Sparsesym& operator*=(Real d);
00346   inline Sparsesym& operator/=(Real d);
00347   
00349   inline Sparsesym& operator=(const Matrix &A);
00351   inline Sparsesym& operator=(const Indexmatrix &A);
00352 
00354   Sparsesym& transpose(){return *this;}            
00355 
00356    
00358 
00362   
00364   friend inline Sparsesym operator+(const Sparsesym &A,const Sparsesym& B);
00366   friend inline Sparsesym operator-(const Sparsesym &A,const Sparsesym& B);
00368   friend inline Sparsesym operator*(const Sparsesym& A,Real d);
00370   friend inline Sparsesym operator*(Real d,const Sparsesym &A);
00372   friend inline Sparsesym operator/(const Sparsesym& A,Real d);
00373   
00374   
00376   friend inline Matrix operator*(const Matrix& A,const Sparsesym& B);
00378   friend inline Matrix operator*(const Sparsesym& A,const Matrix& B);
00380   friend inline Matrix operator+(const Matrix& A,const Sparsesym& B);
00382   friend inline Matrix operator+(const Sparsesym& A,const Matrix& B);
00384   friend inline Matrix operator-(const Matrix& A,const Sparsesym& B);
00386   friend inline Matrix operator-(const Sparsesym& A,const Matrix& B);
00387   
00388   
00390   friend inline Sparsesym transpose(const Sparsesym& A);
00391 
00393 
00394   //------------------------------------------
00395   //----  Connections to other Matrix Classes
00396   //------------------------------------------
00397   
00401 
00403   Sparsesym& xeya(const Symmatrix& A,Real d=1.);
00405   Sparsesym& xeya(const Sparsemat& A,Real d=1.);
00406 
00408   inline Sparsesym& operator=(const Symmatrix &A);
00410   inline Symmatrix operator+(const Symmatrix &A) const;
00412   inline Symmatrix operator-(const Symmatrix &A) const;   
00413 
00415   inline Sparsesym& operator=(const Sparsemat &A);
00417   Sparsemat sparsemult(const Matrix& A) const;
00418 
00420 
00424 
00426   friend inline Symmatrix operator+(const Sparsesym &A,const Symmatrix &B);
00428   friend inline Symmatrix operator+(const Symmatrix &A,const Sparsesym &B);
00430   friend inline Symmatrix operator-(const Sparsesym& A,const Symmatrix &B);
00432   friend inline Symmatrix operator-(const Symmatrix &A,const Sparsesym &B);
00434   friend Real ip(const Symmatrix& A, const Sparsesym& B); //=trace(B^t*A)
00436   friend inline Real ip(const Sparsesym& A, const Symmatrix& B);
00437 
00439   friend Matrix& genmult(const Sparsesym& A,const Sparsemat& B,Matrix &C,
00440                          Real alpha=1.,Real beta=0.,int btrans=0);
00442   friend Matrix& genmult(const Sparsemat& A,const Sparsesym& B,Matrix &C,
00443                          Real alpha=1.,Real beta=0.,int atrans=0);
00445   friend inline Matrix operator*(const Sparsesym& A,const Sparsemat& B);
00447   friend inline Matrix operator*(const Sparsemat& A,const Sparsesym& B);
00448 
00450 
00451   //------------------------------
00452   //----  Elementwise Operations
00453   //------------------------------
00454 
00458 
00460   friend Sparsesym abs(const Sparsesym& A);                 
00461 
00463 
00464   //----------------------------
00465   //----  Numerical Methods
00466   //----------------------------
00467 
00471 
00473   friend Real trace(const Sparsesym& A);               //=sum(diag(A))
00475   friend Real ip(const Sparsesym& A, const Sparsesym& B); //=trace(B^t*A)
00477   friend Real ip(const Matrix& A, const Sparsesym& B); //=trace(B^t*A)
00479   friend inline Real ip(const Sparsesym& A, const Matrix& B); 
00481   friend Real norm2(const Sparsesym& A); //=sqrt(ip(A,A))
00482 
00484   friend Matrix sumrows(const Sparsesym& A);   //=(1 1 1 ... 1)*A
00486   friend inline Matrix sumcols(const Sparsesym& A);    //=A*(1 1 1 ... 1)^t
00488   friend Real sum(const Sparsesym& A);         //=(1 1 ... 1)*A*(1 1 ... 1)^t
00489 
00491 
00492 
00493   //---------------------------------------------
00494   //----  Comparisons / Max / Min / sort / find
00495   //---------------------------------------------
00496 
00500 
00502    friend int equal(const Sparsesym& A, const Sparsesym& B,Real eqtol);
00503 
00505 
00506 
00507   //--------------------------------
00508   //----  Input / Output
00509   //--------------------------------
00510 
00514 
00517     void display(std::ostream& out, 
00518                  int precision=0,   
00519                  int width=0,       
00520                  int screenwidth=0  
00521                  ) const;
00522 
00524 
00528 
00530   friend std::ostream& operator<<(std::ostream& o,const Sparsesym &v);
00532   friend std::istream& operator>>(std::istream& i,Sparsesym &v);
00533 
00535 
00536 };
00537 
00539 
00540 // **************************************************************************
00541 //                make non inline friends available outside
00542 // **************************************************************************
00543 
00544   Matrix diag(const Sparsesym& A);      //=(A(1,1),A(2,2),...)^t
00545   Sparsesym sparseDiag(const Matrix& A,Real tol);
00546   void swap(Sparsesym& A, Sparsesym& B);
00547   Sparsesym& xeyapzb(Sparsesym& x,const Sparsesym& y,
00548                      const Sparsesym& z,Real alpha,Real beta);
00549   Sparsesym& support_rankadd(const Matrix& A,Sparsesym& C,
00550                              Real alpha,Real beta,int trans);
00551   Matrix& genmult(const Sparsesym& A,const Matrix& B,Matrix &C,
00552                   Real alpha,Real beta, int btrans);
00553   Matrix& genmult(const Matrix& A,const Sparsesym& B,Matrix &C,
00554                   Real alpha,Real beta, int atrans);
00555   Real ip(const Symmatrix& A, const Sparsesym& B); //=trace(B^t*A)
00556   Matrix& genmult(const Sparsesym& A,const Sparsemat& B,Matrix &C,
00557                   Real alpha,Real beta,int btrans);
00558   Matrix& genmult(const Sparsemat& A,const Sparsesym& B,Matrix &C,
00559                   Real alpha,Real beta,int atrans);
00560   Sparsesym abs(const Sparsesym& A);
00561   Real trace(const Sparsesym& A);               //=sum(diag(A))
00562   Real ip(const Sparsesym& A, const Sparsesym& B); //=trace(B^t*A)
00563   Real ip(const Matrix& A, const Sparsesym& B); //=trace(B^t*A)
00564   Real norm2(const Sparsesym& A); //=sqrt(ip(A,A))
00565   Matrix sumrows(const Sparsesym& A);   //=(1 1 1 ... 1)*A
00566   Real sum(const Sparsesym& A);         //=(1 1 ... 1)*A*(1 1 ... 1)^t
00567   std::ostream& operator<<(std::ostream& o,const Sparsesym &v);
00568   std::istream& operator>>(std::istream& i,Sparsesym &v);
00569 
00570 // **************************************************************************
00571 //                   implementation of inline functions
00572 // **************************************************************************
00573 
00574 inline void Sparsesym::init_to_zero()
00575 {
00576  mtype=MTsparse;
00577  nr=0;
00578  tol=SPARSE_ZERO_TOL;
00579 #if (CONICBUNDLE_DEBUG>=1)
00580  is_init=1;
00581 #endif
00582 }
00583 
00584 inline Sparsesym& Sparsesym::init(const Sparsesym& A,Real d)
00585 { return xeya(A,d); }
00586 
00587 inline Sparsesym& Sparsesym::init(const Matrix& A,Real d)
00588 { return xeya(A,d);}
00589 
00590 inline Sparsesym& Sparsesym::init(const Indexmatrix& A,Real d)
00591 { return xeya(A,d);}
00592 
00593 inline Sparsesym& Sparsesym::init(Integer r)
00594 {
00595 nr=r;
00596 colinfo.init(0,0,Integer(0)); colindex.init(0,0,Integer(0)); colval.init(0,0,0.);
00597 chk_set_init(*this,1);
00598 return *this;
00599 }
00600 
00601 inline Sparsesym::Sparsesym()
00602 { init_to_zero(); chk_set_init(*this,1);}
00603 inline Sparsesym::Sparsesym(Integer r)
00604 { init_to_zero(); init(r);}
00605 inline Sparsesym::Sparsesym(const Sparsesym& A,Real d):Memarrayuser()
00606 { init_to_zero(); xeya(A,d);}
00607 inline Sparsesym::Sparsesym(const Matrix& A,Real d)
00608 { init_to_zero(); xeya(A,d);}
00609 inline Sparsesym::Sparsesym(const Indexmatrix& A,Real d)
00610 { init_to_zero(); xeya(A,d);}
00611 
00612 inline Sparsesym::Sparsesym(Integer in_nr,Integer nz,
00613        const Integer *ini,const Integer *inj,const Real* va)
00614 { init_to_zero(); init(in_nr,nz,ini,inj,va);}
00615 
00616 inline Sparsesym::Sparsesym(Integer in_nr,Integer nz,
00617               const Indexmatrix& ini,const Indexmatrix& inj, const Matrix& va)
00618 { init_to_zero(); init(in_nr,nz,ini,inj,va);}
00619 
00620 inline Real Sparsesym::operator()(Integer i) const
00621 { return (*this)(i%nr,i/nr); }
00622 inline Real Sparsesym::operator[](Integer i) const
00623 { return (*this)(i%nr,i/nr); }
00624 
00625 inline Sparsesym& xbpeya(Sparsesym& x,const Sparsesym& y,Real alpha,Real beta)
00626 {
00627   if (beta==0.) return x.init(y,alpha); 
00628   Sparsesym B; xeyapzb(B,x,y,beta,alpha); swap(x,B); return x;
00629 }
00630 
00631 inline Sparsesym& Sparsesym::operator=(const Sparsesym &A)
00632 { return xeya(A);} 
00633 inline Sparsesym& Sparsesym::operator+=(const Sparsesym &A)
00634 { Sparsesym B; xeyapzb(B,*this,A); swap(*this,B); return *this;}
00635 inline Sparsesym& Sparsesym::operator-=(const Sparsesym &A)
00636 { Sparsesym B; xeyapzb(B,*this,A,1.,-1.); swap(*this,B); return *this;}
00637 inline Sparsesym& Sparsesym::operator*=(Real d)
00638 {chk_init(*this); colval*=d; return *this;}
00639 inline Sparsesym& Sparsesym::operator/=(Real d)
00640 {chk_init(*this);colval/=d;return *this;}
00641 inline Sparsesym Sparsesym::operator-() const
00642 { return Sparsesym(*this,-1.); }
00643 
00644     
00645 inline Sparsesym& Sparsesym::operator=(const Matrix &A)
00646 { return xeya(A);}
00647 inline Sparsesym& Sparsesym::operator=(const Indexmatrix &A)
00648 { return xeya(A);}
00649 
00650 inline Sparsesym operator+(const Sparsesym& A,const Sparsesym& B) 
00651 {Sparsesym C; return xeyapzb(C,A,B);}
00652 inline Sparsesym operator-(const Sparsesym& A,const Sparsesym& B) 
00653 {Sparsesym C; return xeyapzb(C,A,B,1.,-1.);}
00654 
00655 inline Sparsesym operator*(const Sparsesym &A,Real d) 
00656     { return Sparsesym(A,d);}
00657 inline Sparsesym operator*(Real d,const Sparsesym &A)
00658     { return Sparsesym(A,d);}
00659 inline Sparsesym operator/(const Sparsesym &A,Real d)
00660     { return Sparsesym(A,1/d);}
00661 
00662 inline Matrix operator*(const Sparsesym& A,const Matrix& B)
00663 { Matrix C; return genmult(A,B,C); }
00664 inline Matrix operator*(const Matrix& A,const Sparsesym& B)
00665 { Matrix C; return genmult(A,B,C); }
00666 inline Matrix operator+(const Matrix& A,const Sparsesym& B)
00667 { Matrix C(A); return C.xpeya(B);}
00668 inline Matrix operator+(const Sparsesym& A,const Matrix& B)
00669 { Matrix C(B); return C.xpeya(A);}
00670 inline Matrix operator-(const Matrix& A,const Sparsesym& B)
00671 { Matrix C(A); return C.xpeya(B,-1.);}
00672 inline Matrix operator-(const Sparsesym& A,const Matrix& B)
00673 { Matrix C(B,-1.); return C.xpeya(A);}
00674 
00675 inline Real ip(const Sparsesym& A, const Matrix& B)
00676 {return ip(B,A);}
00677 inline Sparsesym transpose(const Sparsesym& A)
00678 {return Sparsesym(A);}
00679 
00680 inline Matrix sumcols(const Sparsesym& A)    //=A*(1 1 1 ... 1)^t
00681 {Matrix s(sumrows(A)); return s.transpose();}
00682 
00683 
00684 inline Matrix::Matrix(const Sparsesym& A,Real d)
00685 
00686 {init_to_zero(); xeya(A,d); }
00687 
00688 inline Matrix& Matrix::init(const Sparsesym& A,Real d)
00689 
00690 {return xeya(A,d); }
00691 
00692 inline Matrix& Matrix::operator=(const Sparsesym& A)
00693 
00694 {return xeya(A); }
00695 
00696 inline Matrix& Matrix::operator*=(const Sparsesym& A)
00697 
00698 { Matrix C; return *this=genmult(*this,A,C);}
00699 
00700 inline Matrix& Matrix::operator+=(const Sparsesym& A)
00701 
00702 {return xpeya(A); }
00703 
00704 inline Matrix& Matrix::operator-=(const Sparsesym& A)
00705 
00706 {return xpeya(A,-1.); }
00707 
00708 
00709 inline Sparsesym::Sparsesym(const Symmatrix& A,Real d)
00710 { init_to_zero(); xeya(A,d);}
00711 inline Sparsesym& Sparsesym::init(const Symmatrix& A,Real d)
00712 { return xeya(A,d);}
00713 inline Sparsesym& Sparsesym::operator=(const Symmatrix& A)
00714 { return xeya(A);}
00715 inline Symmatrix Sparsesym::operator+(const Symmatrix &A) const
00716 {Symmatrix B(A); B+=*this; return B;}
00717 inline Symmatrix Sparsesym::operator-(const Symmatrix &A) const
00718 {Symmatrix B(A); B-=*this; return B;}
00719 inline Symmatrix operator+(const Sparsesym &A,const Symmatrix &B)
00720 {Symmatrix C(B);return C.xpeya(A);}
00721 inline Symmatrix operator+(const Symmatrix &A,const Sparsesym &B)
00722 {Symmatrix C(A);return C.xpeya(B);}
00723 inline Symmatrix operator-(const Sparsesym& A,const Symmatrix &B)
00724 {Symmatrix C(B,-1.);return C.xpeya(A);}
00725 inline Symmatrix operator-(const Symmatrix &A,const Sparsesym &B)
00726 {Symmatrix C(A);return C.xpeya(B,-1);}
00727 inline Real ip(const Sparsesym& A, const Symmatrix& B)
00728 { return ip(B,A);}    
00729 inline Sparsesym::Sparsesym(const Sparsemat& A,Real d)
00730     { init_to_zero(); xeya(A,d);}
00731 inline Sparsesym& Sparsesym::init(const Sparsemat& A,Real d)
00732     { return xeya(A,d);}
00733 inline Sparsesym& Sparsesym::operator=(const Sparsemat& A)
00734     { return xeya(A);}
00735 inline Matrix operator*(const Sparsesym& A,const Sparsemat& B)
00736     { Matrix C; return genmult(A,B,C);} 
00737 inline Matrix operator*(const Sparsemat& A,const Sparsesym& B)
00738     { Matrix C; return genmult(A,B,C);} 
00739 
00740 inline Sparsemat::Sparsemat(const Sparsesym& A,Real d)
00741 { init_to_zero(); xeya(A,d);}
00742 inline Sparsemat& Sparsemat::init(const Sparsesym& A,Real d) 
00743 { return xeya(A,d);}   
00744 inline Sparsemat& Sparsemat::operator=(const Sparsesym &A)
00745 { return xeya(A);}  
00746 inline Matrix operator*(const Symmatrix& A,const Sparsemat& B)
00747 { Matrix C; return genmult(A,B,C,1.,0.,0);} 
00748 inline Matrix operator*(const Sparsemat& A,const Symmatrix& B)
00749 { Matrix C; return genmult(A,B,C,1.,0.,0);}  
00750 
00751 inline Symmatrix::Symmatrix(const Sparsesym& A,Real d)
00752     { init_to_zero(); xeya(A,d);}
00753 inline Symmatrix& Symmatrix::init(const Sparsesym& A,Real d)          
00754     { return xeya(A,d);}
00755 inline Symmatrix& Symmatrix::operator=(const Sparsesym& A)                
00756     { return xeya(A); }
00757 inline Symmatrix& Symmatrix::operator+=(const Sparsesym& A)               
00758     { return xpeya(A);}
00759 inline Symmatrix& Symmatrix::operator-=(const Sparsesym& A)           
00760     { return xpeya(A,-1.);}
00761 
00765 
00767 int equal(const Sparsesym& A, const Sparsesym& B,Real eqtol=1e-10);
00768 
00769 
00771 
00772 }
00773 
00774 
00775 #endif
00776 

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