realroot_doc 0.1.1
/Users/mourrain/Devel/mmx/realroot/include/realroot/polynomial_mthd.hpp
Go to the documentation of this file.
00001 #ifndef realroot_polynomial_mthd_hpp
00002 #define realroot_polynomial_mthd_hpp
00003 //======================================================================
00004 # include <realroot/parser.hpp>
00005 # include <realroot/assign.hpp>
00006 
00007 # define TMPL    template <class C, class Rep, class Ord>
00008 # define VARIANT with<Rep,Ord>
00009 # define SELF    polynomial<C, VARIANT>
00010 //======================================================================
00011 namespace mmx {
00012 //----------------------------------------------------------------------
00013 // template<class R>
00014 // template<class X> inline
00015 // polynomial<R>::polynomial( const X& x )
00016 // { 
00017 //   let::assign(*this,x); 
00018 // }
00019 //----------------------------------------------------------------------
00020 extern "C" char *synaps_inputptr;
00021 TMPL inline
00022 SELF::polynomial( const char *   s, variables&  vars ): data() { 
00023   typedef SELF polynomial_t;
00024   //  *this = polynomial_t(0);
00025   //  std::cout <<"Init "<<*this<<std::endl ;
00026   int n = strlen(s);
00027   if (s[n-1]=='\n') {n--;}
00028   if(s[n-1]==';')
00029     {
00030       synaps_input = new char[n+1];
00031       memcpy(synaps_input,s,n); synaps_input[n]='\0';
00032       synaps_inputlim = synaps_input + n;
00033     }
00034   else
00035     {
00036       synaps_input = new char[n+2];
00037       memcpy(synaps_input,s,n); 
00038       synaps_input[n]=';'; synaps_input[n+1]='\0';
00039       synaps_inputlim = synaps_input + n+1;
00040     }
00041   synaps_inputptr=synaps_input;
00042   polynomial_t m(1);
00043 
00044 
00045   bool sign = true;
00046   bool first = true;
00047   int Ask;
00048 
00049   for (;;) {
00050     Ask = yylex();
00051     //    std::cout<<"yylval "<<yylval<<std::endl;
00052     if (Ask == COEF) { /* monomial constant */
00053       coeff_t c;
00054       let::assign(c,yylval);
00055       free(yylval);
00056       m *= c;
00057       //      std::cout<<"coeff "<<m<<std::endl;
00058     }
00059     else if (Ask == XID) {
00060       char* p = std::strchr(yylval,'^');
00061       //      std::cout<<yylval<<" exp "<<p<<std::endl;
00062       if (p == NULL)
00063         {
00064           m *= polynomial_t(1,1,vars[std::string(yylval)]);
00065           //      std::cout<<"var "<<m<<std::endl;
00066         }
00067       else {
00068         p++;
00069         m *= polynomial_t(1,atoi(p),vars[std::string(yylval,p-1)]);
00070         //      std::cout<<"var "<<m<<std::endl;
00071       }
00072       free(yylval);
00073     }
00074     else if (Ask == MULT);
00075     else if (Ask == SUM) {
00076       if (sign) {
00077         //      std::cout<<"add " <<m<<std::endl;
00078         if(m!=0) *this += m;
00079         m = polynomial_t(1);
00080       } else {
00081         //      std::cout<<"sub " <<m<<std::endl;
00082         if(m!=0) *this -= m;
00083         sign = true;
00084         m = polynomial_t(1);
00085       }
00086     }
00087     else if (Ask == MINUS) {
00088       if (first)
00089         sign = false;
00090       else if (sign) {
00091         //      std::cout<<"add " <<m<<std::endl;
00092         if(m!=0) *this += m;
00093         sign = false;
00094         m = polynomial_t(1);
00095       }
00096       else {
00097         //      std::cout<<"sub " <<m<<std::endl;
00098         
00099         if(m!=0) *this -= m;
00100         m = polynomial_t(1);
00101       }
00102     }
00103     else if (Ask == TERMINATE) {
00104       if (sign){
00105         //      std::cout<<"add " <<m<<std::endl;
00106         if(m!=0) *this += m;
00107       }else{
00108         //      std::cout<<"sub " <<m<<std::endl;
00109         if(m!=0) *this -= m;
00110       }
00111       break;
00112     }
00113     first = false;
00114   }
00115   delete[] synaps_input;
00116 }
00117 //----------------------------------------------------------------------
00118 // TMPL inline
00119 // SELF & SELF::operator= ( const char *   name ) { 
00120 //   std::cout<<"= "<<std::endl;
00121 //   *this=SELF(name);
00122 //   return *this;
00123 // }
00124 //----------------------------------------------------------------------
00125 TMPL inline
00126 SELF & SELF::operator= ( const C& c) { 
00127   *this=SELF(c);
00128   return *this;
00129 }
00130 //----------------------------------------------------------------------
00131 TMPL bool 
00132 SELF::operator== (const typename SELF::value_type & c) const {
00133   return rep () == c; 
00134 }
00135 TMPL bool 
00136 SELF::operator!= (const typename SELF::value_type & c) const {
00137   return ! (rep () == c); 
00138 }
00139 //----------------------------------------------------------------------
00140 //TMPL
00141 //template <class S> inline 
00142 //SELF::polynomial (const polynomial<S> &mpol) : data (mpol) {};
00143 //----------------------------------------------------------------------
00144 TMPL template <class S, class UECT> inline 
00145 SELF::polynomial (const char* s, const UECT& bx) 
00146   : data () {
00147   *this = self_t(s);
00148   for(unsigned i=0;i<bx.size();i++) restrict(rep(),i,bx[i].lower(),bx[i].upper());
00149 };
00150 //----------------------------------------------------------------------
00151   TMPL template <class S, class W, class VECT> inline 
00152 SELF::polynomial (const polynomial<S,W> &pol, const VECT& bx) 
00153   : data () {
00154   let::assign(rep(),pol.rep());
00155   for(int i=0;i<(int)bx.size()-1;i+=2) restrict(rep(),i,bx[i],bx[i+1]);
00156 };
00157 //----------------------------------------------------------------------
00158 //TMPL inline
00159 //SELF::polynomial (const char *s) : data(mpoldst<C>(s)) {};
00160 //----------------------------------------------------------------------
00161 TMPL inline const typename SELF::value_type
00162 SELF::operator[]( int i ) const { 
00163   return rep()[i]; };
00164 //----------------------------------------------------------------------
00165 TMPL inline  typename SELF::value_type & 
00166 SELF::operator[]( int i ) { 
00167   return rep()[i]; }
00168 //----------------------------------------------------------------------
00169 // TMPL inline SELF &
00170 // SELF::operator+= (const SELF &mp) {
00171 //   add(rep(),mp.rep());  return *this; }
00172 //----------------------------------------------------------------------
00173 // TMPL inline SELF & 
00174 // SELF::operator+= (const C & x) {
00175 //   add (rep (), x);  return *this; }
00176 //----------------------------------------------------------------------
00177 TMPL inline bool 
00178 SELF::operator== (const polynomial & mp) const {
00179   return rep () == mp.rep (); }
00180 //----------------------------------------------------------------------
00181 TMPL template <class S> inline SELF &
00182 SELF::operator= (const polynomial<S, VARIANT> & pol) {
00183   this->~SELF();  new (this) SELF(pol);
00184   return *this;
00185 };
00186 //----------------------------------------------------------------------
00187 } //namespace mmx
00188 //======================================================================
00189 # undef TMPL
00190 # undef SELF
00191 # undef VARIANT
00192 //======================================================================
00193 # endif //realroot_polynomial_mthd_hpp