00001 
00002 
00003 
00004 
00005 
00006 
00007 #ifndef synaps_mpol_dynamicexp_h_
00008 #define synaps_mpol_dynamicexp_h_
00009 
00010 #include <stdlib.h>
00011 #include <iostream>
00012 #include <string>
00013 #include <iterator>
00014 #include <cassert>
00015 #include <synaps/init.h>
00016 #include <synaps/arithm/REF.h>
00017 #include <synaps/base/COUNT.h>
00018 #include <synaps/linalg/VECTOR.m>
00019 
00020 
00021 __BEGIN_NAMESPACE_SYNAPS
00022 
00024 template<class E=unsigned>
00025 struct dynamicexp {
00026 
00027   typedef E            value_type;
00028   typedef unsigned int size_type;
00029   typedef E*           iterator;
00030   typedef iterator     const_iterator;
00031   typedef std::reverse_iterator<const_iterator>  const_reverse_iterator;
00032   typedef std::reverse_iterator<iterator>        reverse_iterator;
00033 
00034   typedef E   exponent_t;
00035   typedef int degree_t;
00036   typedef dynamicexp<E> self_t;
00037 
00038   
00039   dynamicexp():_size(0),_tab(NULL){}
00040   dynamicexp(const dynamicexp<E> & d):_size(d._size){
00041         
00042         if (_size) {
00043           _tab = dynamicexp<E>::alloc(_size);
00044           memcpy(_tab,d._tab,sizeof(E)*_size);
00045         }
00046         else
00047           _tab = NULL;
00048         
00049   }
00050 
00051   dynamicexp(int s): _size(s){
00052         
00053         _tab = dynamicexp<E>::alloc(s);
00054         for(int i=0;i<s;i++) _tab[i]=(exponent_t)0;
00055         
00056   }
00057 
00058   dynamicexp(int s, E *t): _size(s){
00059         assert(s <= sizeof(t)/sizeof(E));
00060         _tab = dynamicexp<E>::alloc(_size);
00061         for(int i=0;i<s;i++) _tab[i]=t[i];
00062   }
00063 
00064   template<class S>
00065         self_t & operator =(const VAL<S> & M)
00066         {
00067           if (this == &(M.rep())) { return *this; }
00068           assign(*this,M.rep());
00069           return *this;
00070         }
00071 
00072   
00073   ~dynamicexp(){if (_tab) delete [] _tab;}
00074 
00075   size_type size() const {return _size;}
00076 
00077   iterator        begin()       {return iterator(_tab); }
00078   const_iterator  begin() const {return const_iterator(_tab); }
00079   iterator        end()         {return iterator(_tab+_size); }
00080   const_iterator  end()   const {return const_iterator(_tab+_size); }
00081 
00082   reverse_iterator       rbegin()       {return reverse_iterator(_tab+_size); }
00083   const_reverse_iterator rbegin() const {return reverse_iterator(_tab+_size); }
00084   reverse_iterator       rend()         {return reverse_iterator(_tab); }
00085   const_reverse_iterator rend()   const {return reverse_iterator(_tab); }
00086 
00087   E   operator[] ( size_type i) const {
00088         
00089     return (i<_size?_tab[i]:0); 
00090   }
00091   E & operator[] (size_type i) { assert(i<_size);return _tab[i];}
00092 
00093   
00094   self_t& operator = (const self_t & A) {
00095         if (&A == this) { return *this; }
00096         if (_tab != NULL) {
00097           
00098           
00099           
00100           
00101           delete [] _tab;
00102         }
00103         _size = A._size;
00104         if (_size) {
00105           _tab = dynamicexp<E>::alloc(_size);
00106           memcpy(_tab,A._tab,sizeof(E)*_size);
00107         }
00108         else
00109           _tab = NULL;
00110         return *this;
00111   }
00112 
00113   
00114 
00115   void reserve(size_type s)
00116   {
00117         if (!s) {
00118           if (_size) { delete [] _tab; }
00119           _size = 0;
00120           _tab = NULL;
00121         }
00122         else if(_tab==NULL) {
00123           _size = s;_tab = dynamicexp<E>::alloc(s);
00124           
00125         }
00126         else {
00127           delete [] _tab;
00128           _tab = dynamicexp<E>::alloc(s);
00129           _size=s;
00130         }
00131   }
00132   void resize(size_type s)
00133   {
00134         if(s<=_size)
00135           _size = s;
00136         else{
00137           E* _tmp =_tab;
00138           _tab = dynamicexp<E>::alloc(s);
00139           for(size_type i =0;i<_size;i++) _tab[i]=_tmp[i];
00140           for(size_type i =_size;i<s;i++) _tab[i]=0;
00141           delete [] _tmp;
00142           _size=s;
00143         }
00144   }
00145 
00146 
00147   void init(int s)
00148   {
00149         if (_size) { delete [] _tab; }
00150         _size= s;
00151         _tab = dynamicexp<E>::alloc(s);
00152   }
00153 
00154   self_t& setExponent(size_type i, value_type d);
00155 
00156   friend bool operator == (const dynamicexp & A, const dynamicexp & B) {
00157         int i,minsize=std::min(A._size,B._size);
00158         for(i=0;i<minsize;i++)
00159           if(A._tab[i]!=B._tab[i]) return 0;
00160         if(A._size>B._size)
00161         {
00162           for(;i<(int)A._size;i++)
00163                 if(A._tab[i]) return 0;
00164         }
00165         else
00166         {
00167           for(;i<(int)B._size;i++)
00168                 if(B._tab[i]) return 0;
00169 
00170         }
00171         
00172         
00173         return 1;
00174   }
00175   friend bool operator != (const dynamicexp & A, const dynamicexp & B) {
00176         return !(A==B);
00177   }
00178   
00179   size_type _size;
00180   E*        _tab;
00181 
00182   static E* alloc(unsigned int N)
00183   {return (new E[N]);}
00184   
00185   void clear() {delete[] _tab;}
00186   
00187 
00188 };
00189 
00190   template<class E> inline
00191 typename dynamicexp<E>::degree_t degree(const dynamicexp<E> & t)
00192 {
00193   typename dynamicexp<E>::degree_t d(0);
00194   for (typename dynamicexp<E>::size_type i = 0; i < t._size; ++i) d+=t[i];
00195   return(d);
00196 }
00197 
00198 template<class E>
00199 std::ostream & operator << (std::ostream & os, const dynamicexp<E> & t) {
00200   bool first=true;
00201   if(t._size)
00202         for (typename dynamicexp<E>::size_type i = 0; i < t._size; ++i)
00203         {
00204           if (t[i] == 1){
00205                 if(first) first=false; else os <<"*";
00206                 os <<'X'<< i;
00207           }else if (t[i] == 0);
00208           else {
00209                 if(first) first=false; else os <<"*";
00210                 os <<"X"<< i << '^' <<(int)(t[i]);
00211           }
00212         }
00213   return os;
00214 }
00215 
00216   template<class E>
00217 dynamicexp<E>& dynamicexp<E>::setExponent(size_type i, value_type d)
00218 {
00219   assert(i<_size);
00220   (*this)[i]=d;
00221 
00222   
00223 
00224   
00225   
00226   
00227   
00228   
00229   
00230   
00231   
00232   
00233   
00234   
00235   
00236   
00237   
00238   
00239   
00240   
00241   
00242   
00243   
00244   
00245   
00246   
00247   
00248   
00249   
00250   
00251   
00252   
00253   
00254   
00255   
00256   
00257   
00258   return *this;
00259 }
00260 
00261 template<class E>
00262 int lvar (const dynamicexp<E> & A) {
00263   int r=A._size-1;
00264   while(r>=0 && (A[r]==0)) r--;
00265   return r;
00266 }
00267 
00268 template<class E>
00269 void erase (dynamicexp<E> & A) {
00270   A.clear();
00271   A._tab = NULL;
00272   A._size = 0;
00273 }
00274 
00275 template <class E>
00276   inline VAL<OP<'+',dynamicexp<E>,dynamicexp<E> > >
00277 operator+(const dynamicexp<E> & a, const dynamicexp<E> & b)
00278 {
00279   return VAL<OP<'+',dynamicexp<E>,dynamicexp<E> > >
00280         (OP<'+',dynamicexp<E>,dynamicexp<E> >(a,b));
00281 }
00282 
00283 template <class E> inline
00284 void assign(dynamicexp<E> & a,
00285         const OP<'+',dynamicexp<E>,dynamicexp<E> > & M)
00286 {
00287   add(a,M.op1,M.op2);
00288 }
00289 
00290 template<class E>
00291 void add (dynamicexp<E> & r,
00292         const dynamicexp<E> & A, const dynamicexp<E> & B);
00293 
00294 template<class E>
00295 void add (dynamicexp<E> & r, 
00296           const dynamicexp<E> & A, 
00297           const dynamicexp<E> & B)
00298 {
00299   r.init(std::max(A.size(),B.size()) );
00300   VECTOR::add(r,A,B);
00301 }
00302 
00303 __END_NAMESPACE_SYNAPS
00304 
00305 #endif // synaps_mpol_dynamicexp_h_
00306