synaps/linalg/VectDse.h

Go to the documentation of this file.
00001 /*********************************************************************
00002 *   This file is part of the source code of the SYNAPS environment.  *
00003 *   Author(s): B. Mourrain, GALAAD, INRIA                            *
00004 **********************************************************************/
00005 #ifndef synaps_linalg_VectDse_H
00006 #define synaps_linalg_VectDse_H
00007 //----------------------------------------------------------------------
00008 #include <sstream>
00009 #include <vector>
00010 #include <synaps/init.h>
00011 #include <synaps/base/shared_object.h>
00012 #include <synaps/arithm/texp.h>
00013 #include <synaps/base/Range.h>
00014 #include <synaps/base/seq1d.h>
00015 #include <synaps/arithm/let.h>
00016 #include <synaps/arithm/REF.h>
00017 #include <synaps/linalg/rep1d.h>
00018 #include <synaps/linalg/VECTOR.m>
00019 #include <synaps/linalg/MATRIX.m>
00020 //----------------------------------------------------------------------
00021 __BEGIN_NAMESPACE_SYNAPS
00022 //----------------------------------------------------------------------
00060 template <class C, class R = linalg::rep1d<C> >
00061 struct VectDse   {
00062 
00063   shared_object<R>  data;
00064   R &       rep()       {return (*data);}
00065   const R & rep() const {return (*data);}
00066 
00067   typedef typename R::size_type          size_type;
00068   typedef C                              value_type;
00069   typedef typename R::iterator           iterator;
00070   typedef typename R::const_iterator     const_iterator;
00071   typedef typename R::reverse_iterator   reverse_iterator;
00072   typedef typename R::const_reverse_iterator const_reverse_iterator;
00073   typedef R                              rep_type;
00074   typedef VectDse<C,R>                   self_t;
00075   
00076   operator arithm::template_expression<self_t>(){ return arithm::template_expression<self_t>(*this); };
00077   
00078   VectDse(): data() {}
00079   VectDse(const R & r):data(r){}
00080   VectDse(R* r):data(r){}
00081   VectDse(const shared_object<R> & r):data(r){}
00082   VectDse(size_type s) {rep().resize(s);}
00083   VectDse(size_type s,const type::AsSize & mth) {rep().resize(s);}
00084   VectDse(size_type s, value_type* t) {rep()=R(t,t+s);}
00085   VectDse(iterator b,iterator e) {rep()=R(b,e);}
00086   VectDse(size_type m, const char * str);
00087 
00088   //  inline operator generic () { return generic(data.body); }
00089 
00090   // Copy constructor
00091   VectDse(const self_t & r): data(r.data) {}
00092   template<class X>   VectDse( const arithm::template_expression<X> & x );
00093   template<class C1,class R1> VectDse( const VectDse<C1,R1> & v );
00094   template<class X>   self_t & operator=(const X & exp);
00095   
00096   iterator               begin()        {return rep().begin();}
00097   iterator               end()          {return rep().end();}
00098   const_iterator         begin()  const {return rep().begin();}
00099   const_iterator         end()    const {return rep().end();}
00100   reverse_iterator       rbegin()       {return rep().rbegin();}
00101   reverse_iterator       rend()         {return rep().rend();}
00102   const_reverse_iterator rbegin() const {return rep().rbegin();}
00103   const_reverse_iterator rend  () const {return rep().rbegin();}
00104 
00105   self_t & operator =(const self_t & V) { using let::assign; assign(data,V.data); return *this; }
00106   self_t & operator+=(const self_t & v)     { add(*this,v); return *this; };
00107   self_t & operator-=(const self_t & v)     { sub(*this,v); return *this; };
00108   self_t & operator*=(const value_type & c) { mul(*this,c); return *this; };
00109   self_t & operator/=(const value_type & c) { div(*this,c); return *this; };
00110   template<class X> 
00111   self_t & operator+=(const X & x) { *this = *this + x; return *this; };
00112   template<class X>
00113   self_t & operator-=(const X & x) { *this = *this - x; return *this; };
00114   template<class X>
00115   self_t & operator*=(const X & x) { *this = *this * x; return *this; };
00116   template<class X>
00117   self_t & operator/=(const X & x) { *this = *this / x; return *this; };
00118   
00119   value_type & operator[] (size_type i)       {return rep()[i];}
00120   const value_type & operator[] (size_type i) const {return rep()[i];}
00121 
00122   bool operator== (const self_t & v) 
00123   { 
00124     using VECTOR::equal;
00125     return equal(this->rep(),v.rep());
00126   };
00127   
00128   template<class S>
00129   self_t & operator=(const VAL<S> & V) 
00130   { 
00131     using let::assign;
00132     //using let::copy; 
00133     assign(*this,V.rep()); return *this;
00134   }
00135   
00136   bool operator!= (const self_t & v)  { return !(*this==v); }
00137 
00138   VectDse<C,seq1d<value_type,iterator> > operator[](const Range & I);
00139   
00140   size_type size() const {return rep().size();}
00141   self_t resize(int n)  {rep().resize(n); return *this;}
00142 
00143 };
00144 
00145 //======================================================================
00146 /*{
00147  The @value_type@ is the type of the coefficients. The @size_type@ is the
00148  type of the indices used to access the elements of the vectors.  The
00149  @iterator@ is the type of the forward iterators on the vectors.  The
00150  internal representation type @R@ is @rep_type@.
00151 }*/
00152 
00153 namespace let
00154 {
00155   template<class Ca,class Ra,class Cb, class Rb> inline
00156   void assign( VectDse<Ca,Ra> & a, const VectDse<Cb,Rb> & b )
00157   {
00158     using VECTOR::assign;
00159     assign(a.rep(),b.rep());
00160   };
00161   //----------------------------------------------------------------------
00162   template<class Ca, class Ra,class Cb, class  Rb> inline
00163   void copy(VectDse<Ca,Ra> & v, VectDse<Cb,Rb>* t)
00164   {
00165     using VECTOR::assign;
00166     assign(v.rep(), (*t).rep());
00167   }
00168   
00169   template<class Ca, class Ra, class Cb, class Rb> inline
00170   void copy( VectDse<Ca,Ra> & a, const VectDse<Cb,Rb> & b )
00171   {
00172     using VECTOR::assign; assign(a.rep(),b.rep());
00173   };
00174 
00175   template<class Ca, class Ra, class TEXP> inline
00176   void copy( VectDse<Ca,Ra> & a, const TEXP & b )
00177   {
00178     using let::assign; assign(a,b);
00179   }
00180 }
00181 
00182 
00183 //======================================================================
00184 // INPUT OUTPUT
00185 //======================================================================
00187 template<class C, class R>
00188 std::ostream & operator<<(std::ostream & os, const VectDse<C,R> & V)
00189 {
00190   using namespace VECTOR;
00191   return print(os,V.rep());
00192 }
00193 //----------------------------------------------------------------------
00198 template<class C, class R>  inline
00199 std::istream & operator>>(std::istream & is, VectDse<C,R> & V)
00200 {
00201   using namespace VECTOR;
00202   return read(is,V);
00203 }
00204 
00205 //--------------------------------------------------------------------
00210 template <class C, class R> 
00211 VectDse<C,R>::VectDse(size_type m, const char * str)
00212 {
00213   rep()= R(m);
00214   std::istringstream ins(str);
00215   for(size_type i=0; i< m && ins.good(); i++)   ins >> rep()[i];
00216 }
00217 
00218 //----------------------------------------------------------------------
00219 template <class C, class R> 
00220 VectDse<C,seq1d<typename VectDse<C,R>::value_type,typename VectDse<C,R>::iterator> > 
00221    VectDse<C,R>::operator[](const Range & I) 
00222 {
00223   using namespace VECTOR;
00224   typedef typename VectDse<C,R>::value_type value_type;
00225   typedef typename VectDse<C,R>::iterator   iterator;
00226   typedef seq1d<value_type,iterator>     rep_type;
00227       
00228   typename VectDse<C,R>::size_type n=I.i2-I.i1+1;
00229   return VectDse<C,rep_type>(rep_type(n,create(iterator(),rep(),I.i1),
00230                                       create(iterator(),rep(),I.i2+1)));
00231 }
00232 //----------------------------------------------------------------------
00233 
00234 template<class C, class R> 
00235 void add( VectDse<C,R> & r, const VectDse<C,R> & a )
00236 {
00237   using VECTOR::add;
00238   add(r.rep(),a.rep());
00239 };
00240 
00241 template<class C, class R>
00242 void add( VectDse<C,R> & r, const VectDse<C,R> & a, const VectDse<C,R> & b )
00243 {
00244   using VECTOR::add;
00245   add(r.rep(),a.rep(),b.rep());
00246 };
00247 
00248 template<class C, class R>
00249 void sub( VectDse<C,R> & r, const VectDse<C,R> & a )
00250 {
00251   using VECTOR::sub;
00252   sub(r.rep(),a.rep());
00253 };
00254 
00255 template<class C, class R> 
00256 void sub( VectDse<C,R> & r, const VectDse<C,R> & a, const VectDse<C,R> & b )
00257 {
00258   using VECTOR::sub;
00259   sub(r.rep(),a.rep(),b.rep());
00260 };
00261 
00262 template<class C, class R> 
00263 void mul( C & r, const VectDse<C,R> & a, const VectDse<C,R> & b )
00264 {
00265   using VECTOR::innerprod;
00266   r = innerprod(a.rep(),b.rep());
00267 };
00268 
00269 template<class C, class R> 
00270 void mul( VectDse<C,R> & r, const VectDse<C,R> & a, const C & b )
00271 {
00272   using VECTOR::mul_ext;
00273   mul_ext(r.rep(),a.rep(),b);
00274 };
00275 
00276 template<class C, class R>
00277 void mul( VectDse<C,R> & r, const C & b, const VectDse<C,R> & a )
00278 {
00279   using VECTOR::mul_ext;
00280   mul_ext(r.rep(),a.rep(),b);
00281 };
00282 
00283 template<class C, class R>
00284 void mul( VectDse<C,R> & r, const C & c )
00285 {
00286   using VECTOR::mul_ext;
00287   mul_ext(r.rep(),c);
00288 };
00289 
00290 template<class C, class R>
00291 void div( VectDse<C,R> & r, const C & c )
00292 {
00293   using VECTOR::div_ext;
00294   div_ext(r.rep(),c);
00295 };
00296 
00297 template<class C, class R>
00298 void div( VectDse<C,R> & r, const VectDse<C,R> & a, const C & c )
00299 {
00300   using VECTOR::div_ext;
00301   div_ext(r.rep(),a.rep(),c);
00302 };
00303 
00304 template<class C, class R> 
00305 void neg( VectDse<C,R> & r, const VectDse<C,R> & a )
00306 {
00307   using VECTOR::neg;
00308   neg( r.rep(), a.rep()) ;
00309 };
00310 
00311 namespace arithm 
00312 {
00313   template<class C, class R> struct structureof< VectDse<C,R> > { typedef structure::vector T; };
00314   
00315 #define template_arg_list template<class Ca, class Ra, class Cb, class Rb>
00316 #define parm0 VectDse<Ca,Ra>
00317 #define parm1 VectDse<Cb,Rb>
00318   declare_binary_operator(template_arg_list,parm0,parm1,_add_,operator+);
00319   declare_binary_operator(template_arg_list,parm0,parm1,_sub_,operator-);
00320   declare_binary_operator(template_arg_list,parm0,parm1,_mul_,operator*);
00321 #undef  template_arg_list
00322 #define template_arg_list template<class C, class R>
00323 #undef  parm0
00324 #define parm0 VectDse<C,R>
00325 #undef  parm1
00326 #define parm1 typename VectDse<C,R>::value_type
00327   declare_binary_operator(template_arg_list,parm0,parm1,_mul_,operator*);
00328   declare_binary_operator(template_arg_list,parm1,parm0,_mul_,operator*);
00329   declare_binary_operator(template_arg_list,parm0,parm1,_div_,operator/);
00330 #undef  parm1
00331 #undef  template_arg_list
00332 #define template_arg_list template<class C, class R, class K>
00333 #define parm1 typename K::integer
00334   declare_binary_operator(template_arg_list,parm0,parm1,_mul_,operator*);
00335   declare_binary_operator(template_arg_list,parm1,parm0,_mul_,operator*);
00336   declare_binary_operator(template_arg_list,parm0,parm1,_div_,operator/);
00337 #undef  parm1
00338 #define parm1 typename K::rational
00339   declare_binary_operator(template_arg_list,parm0,parm1,_mul_,operator*);
00340   declare_binary_operator(template_arg_list,parm1,parm0,_mul_,operator*);
00341   declare_binary_operator(template_arg_list,parm0,parm1,_div_,operator/);
00342 #undef parm1
00343 #define parm1 typename K::floating
00344   declare_binary_operator(template_arg_list,parm0,parm1,_mul_,operator*);
00345   declare_binary_operator(template_arg_list,parm1,parm0,_mul_,operator*);
00346   declare_binary_operator(template_arg_list,parm0,parm1,_div_,operator/);
00347 #undef template_arg_list
00348 #undef parm0
00349 #undef parm1
00350 #define template_arg_list template<class C, class R>
00351 #define parm0 VectDse<C,R>
00352   declare_unary_operator(template_arg_list,parm0,_neg_,operator-);
00353 #undef template_arg_list
00354 #undef parm0
00355 };
00356 
00357 
00358 template<class C, class R>
00359 template<class X> inline
00360 VectDse<C,R>::VectDse( const arithm::template_expression<X> & x )
00361 {
00362   using let::assign;
00363   assign(*this,x);
00364 };
00365 
00366 template<class C, class R>
00367 template<class X> inline
00368 VectDse<C,R> & VectDse<C,R>::operator= ( const X & x )
00369 {
00370   using let::assign;  assign(*this,x);
00371 };
00372 
00373 template<class C, class R> 
00374 template<class C1,class R1>  VectDse<C,R>::VectDse( const VectDse<C1,R1> & v )
00375 {
00376   let::assign(*this,v);
00377 };
00378 
00379 
00380 namespace type
00381 {
00382   template<class Y, class C> struct instanceof< VectDse<C>, Y  >  { typedef VectDse<Y> T; };
00383 };
00384 __END_NAMESPACE_SYNAPS
00385 //====================================================================
00386 #endif //synaps_linalg_VectDse_H
00387 

SYNAPS DOCUMENTATION
logo