synaps/linalg/MatrDse.h

Go to the documentation of this file.
00001 /*********************************************************************
00002 *      This file is part of the source code of SYNAPS kernel.        *
00003 *   Author(s): B. Mourrain, GALAAD, INRIA                            *
00004 **********************************************************************
00005 History: 
00006 **********************************************************************/
00007 #ifndef synaps_linalg_MatrDse_H
00008 #define synaps_linalg_MatrDse_H
00009 //----------------------------------------------------------------------
00010 #include <sstream>
00011 #include <list>
00012 #include <synaps/init.h>
00013 #include <synaps/base/type.h>
00014 #include <synaps/base/Range.h>
00015 #include <synaps/linalg/texp.h>
00016 #include <synaps/arithm/let.h>
00017 #include <synaps/arithm/Cst.h>
00018 #include <synaps/linalg/MATRIX.m>
00019 #include <synaps/linalg/VectDse.h>
00020 #include <synaps/base/seq1d.h>
00021 #include <synaps/linalg/rep2d.h>
00022 
00023 //--------------------------------------------------------------------
00024 __BEGIN_NAMESPACE_SYNAPS
00025 //====================================================================
00029 //--------------------------------------------------------------------
00069 template<class C, class R=linalg::rep2d<C> > 
00070 struct MatrDse 
00071 {
00072   typedef VectDse<C> vector_type;
00073   typedef VectDse<C> row_type;
00074   typedef VectDse<C> col_type;
00075   shared_object<R>  data;
00076   R &       rep()       {return (*data);}
00077   const R & rep() const {return (*data);}
00078   
00079   typedef typename R::size_type     size_type;
00080   typedef C                         value_type;
00081   typedef C                         coeff_t;
00082   typedef typename R::row_iterator  row_iterator;
00083   typedef typename R::col_iterator  col_iterator;
00084   
00085   
00086   typedef R                         rep_type;
00087   typedef MatrDse<C, R>               self_t;
00088   
00089   MatrDse() {}
00090   MatrDse(const R & r): data(r) {}
00091   MatrDse(size_type m, size_type n) {
00092     rep().resize(m,n); 
00093   }
00095   MatrDse(size_type m, size_type n, const value_type & c) { 
00096     using MATRIX::init; rep().resize(m,n); init(rep(),c); 
00097   }
00103   MatrDse( size_type m, size_type n, const char* str, ByRow ){ 
00104     using MATRIX::init; std::istringstream ins(str); init(rep(),m,n,ins,ByRow()); 
00105   }
00109   MatrDse(size_type m, size_type n, const char * str, ByCol= ByCol()) { 
00110     using MATRIX::init; std::istringstream ins(str); init(rep(),m,n,ins,ByCol()); 
00111   };
00116   template<class T>
00117   MatrDse(size_type m, size_type n, const value_type * src, const T & t = ByCol()) { 
00118     using MATRIX::init; init( this->rep(), m, n, src, t ); 
00119   };
00120   // Copy constructors.
00121   MatrDse(const self_t & M): data(M.data) {
00122   } 
00123   // Assignement operators
00124   self_t & operator=(const self_t & M) {
00125     data = M.data; return *this;
00126   } 
00127   
00128     
00129   template<class X>
00130   self_t & operator=(const X & M);
00131   
00133   size_type nbrow() const {
00134     return rep().nbrow();
00135   } 
00136   
00138   size_type nbcol() const {
00139     return rep().nbcol();
00140   }
00141   
00143   value_type&       operator() (size_type i, size_type j) {return rep()(i,j);}
00144   
00146   const value_type& operator() (size_type i, size_type j) const {return rep()(i,j);} 
00147   
00148   typedef typename binary_operator_prototype< linalg::_submatrix_, self_t, Range2d >::F submatrix_t;
00150   submatrix_t  operator()(const Range & I, const Range & J) {  return submatrix(*this,Range2d(I,J)); };
00151   
00153   submatrix_t  operator()(const Range2d & rg ) {  return submatrix(*this,rg); };
00154   
00155   template<class X>
00156   self_t & operator+=(const X & x) { *this = *this + x; return *this; }
00157   
00158   template<class X>
00159   self_t & operator-=(const X & x) { *this = *this - x; return *this; };
00160   
00161   template<class X>
00162   self_t & operator*=(const X & x)  { *this = *this * x; return *this; };
00163   
00164   template<class X>
00165   self_t & operator/=(const X & x)  { *this = *this / x; return *this; };
00167   bool operator== (const self_t & v) { 
00168     using MATRIX::equal; return equal(*this,v); 
00169   };
00170   
00171   bool operator!= (const self_t & v) { return !(*this==v); }
00172   
00174   self_t & swaprow(size_type i, size_type j) {
00175     using MATRIX::swaprow; swaprow(rep(),i,j); return *this;
00176   };
00177   
00178   self_t & swapcol(size_type i, size_type j) { 
00179     using MATRIX::swapcol; swapcol(rep(),i,j); return *this;
00180   };
00181   
00182   self_t & addrow(size_type i,size_type j, const value_type & c = 1) {
00183     using MATRIX::addrow; addrow(rep(),i,j,c); return *this;
00184   };
00185   
00186   self_t & addcol(size_type i,size_type j, const value_type & c = 1) {
00187     using MATRIX::addcol; addcol(rep(),i,j,c); return *this;
00188   };
00189   
00191   self_t & multrow(size_type i, const value_type & c = 1) {
00192     using MATRIX::multrow; multrow(*this,i,c); return *this;
00193   };
00194   
00196   self_t & multcol(size_type i, const value_type & c = 1)
00197   {
00198     using MATRIX::multcol;
00199     multcol(*this,i,c);
00200     return *this;
00201   };
00202   
00204   self_t & transpose() {
00205     using MATRIX::transpose; transpose(rep()); return *this; 
00206   };
00207 
00209   self_t & resize(size_type i,size_type j) { 
00210     this->rep().resize(i,j); 
00211     return *this; 
00212   }
00213 };
00214 
00215 
00216 template<class C, class R>
00217 MatrDse<C,R>  Transpose(const MatrDse<C,R> & M)
00218 {
00219   MatrDse<C,R> TM(M); TM.transpose();
00220   return (TM);  
00221 }
00222 //======================================================================
00223 // INPUT OUTPUT 
00224 //======================================================================
00226 template<class C, class R>
00227 std::ostream & operator<<(std::ostream & os, const  MatrDse<C,R> & p)
00228 {
00229   using namespace MATRIX;
00230   return print(os,p.rep());
00231 }
00232 //----------------------------------------------------------------------
00238 template<class C, class R>  inline
00239 std::istream & operator>>(std::istream & is, MatrDse<C,R> & M)
00240 {
00241   using namespace MATRIX;
00242   return read(is,M);
00243 }
00244 //----------------------------------------------------------------------
00247 template<class C> MatrDse<C> diag(unsigned n, const C & c)
00248 {
00249   MatrDse<C> r(n,n);
00250   for(unsigned i=0;i<n;i++)
00251     for(unsigned j=0;j<n;j++)
00252       if(i==j) r(i,i)=c;
00253       else     r(i,j)=(C)0;
00254   return r;
00255 }
00256 
00257 __END_NAMESPACE_SYNAPS
00258 
00259 #include "matrdse/let.h"
00260 #include "matrdse/operations.h"
00261 #include "matrdse/arithm.h"
00262 
00263 
00264 __BEGIN_NAMESPACE_SYNAPS
00265 template<class C, class R>
00266 template<class X> inline
00267 MatrDse<C,R> & MatrDse<C,R>::operator=(const X & M)
00268 { 
00269   using let::assign; assign(*this,M); return *this;  
00270 } 
00271 
00272 namespace type
00273 {
00274   template<class C, class Y>
00275   struct instanceof< MatrDse<C>, Y> { typedef MatrDse<Y> T; };
00276   //  template<class C, class R, class Y>
00277   //    struct instanceof< MatrDse<C,R>, Y >{ typedef MatrDse<Y, typename instanceof<R,Y>::T > T; };
00278 };
00279 __END_NAMESPACE_SYNAPS
00280 #endif // synaps_linalg_matrdse_H
00281 
00282 

SYNAPS DOCUMENTATION
logo