realroot_doc 0.1.1
/Users/mourrain/Devel/mmx/realroot/include/realroot/polynomial.hpp
Go to the documentation of this file.
00001 #ifndef realroot_polynomial_hpp
00002 #define realroot_polynomial_hpp
00003 
00019 //======================================================================
00020 #include <realroot/shared_object.hpp>
00021 #include <realroot/texp_sup.hpp>
00022 #include <realroot/variables.hpp>
00023 #include <realroot/monomial.hpp>
00024 #include <realroot/ring.hpp>
00025 
00026 #define TMPL    template <class C, class Rep, class Ord>
00027 #define VARIANT with<Rep,Ord>
00028 #define SELF    polynomial<C, VARIANT>
00029 
00030 //======================================================================
00031 namespace mmx {
00032 
00033 template<class A, class B> struct use;
00034 struct operators_of;
00035 
00036   template<class C, class V>   struct polynomial;
00037 #ifndef DECL_WITH
00038 #define DECL_WITH
00039   template<class V, class W=V> struct with;
00040 #endif //DECL_WITH
00041 
00042 struct polynomial_of {};
00046 TMPL
00047 struct polynomial<C,VARIANT> 
00048 {
00049   typedef polynomial<C,VARIANT>                   self_t;
00050   typedef use<polynomial_of,self_t>               Def;
00051   typedef typename Def::Ring                      Ring;
00052   typedef Ring                                    V;  
00053   typedef typename Def::rep_t                     R;
00054   typedef typename Def::rep_t                     rep_t;
00055   typedef use<operators_of,R>                     Op;
00056   typedef C                                       scalar_t;
00057   typedef C                                       Scalar;
00058   typedef C                                       value_type;
00059   typedef C                                       coeff_t;
00060   typedef typename R::size_type                   size_type;
00061   typedef R                                       rep_type;
00062   typedef Ord                                     order_t;
00063   typedef typename R::iterator                    iterator;
00064   typedef typename R::const_iterator              const_iterator;
00065   typedef typename R::reverse_iterator            reverse_iterator;
00066   typedef typename R::const_reverse_iterator      const_reverse_iterator;
00067 
00068   typedef typename Def::Monomial                  Monomial;
00069 
00070 
00071   shared_object<R> data;
00072   R &       rep()       { return (*data);}
00073   const R & rep() const { return (*data);}
00074 
00075 //----------------------------------------------------------------------
00076   polynomial(): data() {};
00077 
00078   polynomial( int c) : data() { rep()= R((Scalar)c); }
00079   polynomial( const C& c): data() { rep() = R(c); }
00080   polynomial( const C& c, int d, unsigned v=0) : data() { rep()=R(c,d,v); }
00081   polynomial( const Monomial & m): data() { rep()=R(m);}
00082   polynomial( const self_t & P): data(P.data) {}
00083  ~polynomial() {}
00084 
00085   template<class S, class VECT> 
00086   polynomial( const char * s, const VECT& bx);
00087 
00088   template<class S, class W, class VECT> 
00089   polynomial( const polynomial<S,W>& pol, const VECT& bx);
00090 
00091   polynomial( const char * s, variables& vars = Ring::vars());
00092   polynomial( const char * s, const variables& vars) {
00093     variables v(vars);
00094     *this= self_t(s,v);
00095   }
00096 //----------------------------------------------------------------------
00097 
00098   bool operator==( const C& c ) const;
00099   bool operator!=( const C& c ) const;
00100   bool operator==( const self_t& mp ) const;
00101   
00102   //  polynomial & operator= ( const char* );
00103   self_t & operator = ( const C& );
00104   self_t & operator = ( const self_t & x ) { data=x.data; return *this;}
00105 
00106   template<class S>
00107   polynomial& operator = ( const polynomial<S,VARIANT>& pol );
00108 
00109   template<class X>
00110   polynomial& operator+= ( const X & x )      { *this = *this + x; return *this; };
00111   polynomial& operator+= ( const self_t& p )  { Op::add(rep(),p.rep()); return *this;}
00112   polynomial& operator+= ( const C& c )       { Op::add(rep(),c); return *this; }
00113 
00114   template<class X>
00115   polynomial& operator-= ( const X & x )      { *this = *this - x; return *this; };
00116   polynomial& operator-= ( const self_t& p )  { Op::sub(rep(),p.rep()); return *this;}
00117   polynomial& operator-= ( const C& c )       { Op::sub(rep(),c); return *this; }
00118 
00119   template<class X>
00120   polynomial& operator*= ( const X & x )      { *this = *this * x; return *this; };
00121   polynomial& operator*= ( const C& c)        { Op::mul(rep(),c); return *this; }
00122   polynomial& operator*= ( const Monomial& m) { Op::mul(rep(),m); return *this; }
00123   polynomial& operator*= ( const self_t& p)   { Op::mul(rep(),p.rep()); return *this; }
00124 
00125 
00126   polynomial& operator/= ( const C& c)        { Op::div(rep(),c); return *this; }
00127   polynomial& operator/= ( const self_t& p)   { Op::div(rep(),p.rep()); return *this; }
00128 
00129   // Iterator functions
00130   iterator               begin()        {return rep().begin();}
00131   const_iterator         begin()  const {return rep().begin();}
00132   iterator               end()          {return rep().end();}
00133   const_iterator         end()    const {return rep().end();}
00134   
00135   reverse_iterator       rbegin()       {return rep().rbegin();}
00136   const_reverse_iterator rbegin() const {return rep().rbegin();}
00137   reverse_iterator       rend()         {return rep().rend();}
00138   const_reverse_iterator rend()   const {return rep().rend();}
00139 
00141   unsigned size()  const {return rep().size();}
00142 
00144   int nbvar() const {return rep().nbvar();}
00145 
00147   const C coefof(int * deg) {return rep().entry(deg);}
00148 
00149   const C  operator[]( int i ) const;
00150   C& operator[]( int i ) ;
00151   //  std::string varname( int i ) const ;
00152   //  void lexsort();
00153 
00154   C operator()(const C& x) const {
00155     assert(rep().nbvar()<=1);
00156     C r; eval(r,rep(),x); return r;
00157   }
00158 
00159   template<class T>
00160   T operator()(const T& x) const {
00161     //    assert(rep().nbvar()==1);
00162     T r; eval(r,rep(),x); return r;
00163   }
00164 
00165   template<class T>
00166   T operator()(const T& x, const T& y) const {
00167     T p[2]={x,y};
00168     T r; eval(r,rep(),p,2); return r;
00169     // delete p;
00170   } 
00171 
00172   template<class T>
00173   T operator()(const T& x, const T& y, const T& z) const {
00174     T p[3]={x,y,z};
00175     T r; eval(r,rep(),p,3); return r;
00176     // delete p;
00177   }
00178 
00179   template<class T>
00180   T operator()(const T& x, unsigned n) const {
00181     T r; eval(r,rep(), x, n); return r;
00182   }
00183 
00185   template<class T>
00186   typename T::value_type at(const T& x) const {
00187     typename T::value_type r; eval_at(r,rep(), x); return r;
00188   }
00189 };
00190 } // namespace mmx
00191 //====================================================================
00192 # undef TMPL
00193 # undef SELF
00194 # undef VARIANT
00195 //======================================================================
00196 # include <realroot/polynomial_mthd.hpp>
00197 # include <realroot/polynomial_fcts.hpp>
00198 # include <realroot/polynomial_operators.hpp>
00199 //====================================================================
00200 #endif //realroot_polynomial_hpp