Developer documentation

polynomial_mthd.hpp
Go to the documentation of this file.
1 #ifndef realroot_polynomial_mthd_hpp
2 #define realroot_polynomial_mthd_hpp
3 //======================================================================
4 # include <realroot/parser.hpp>
5 # include <realroot/assign.hpp>
6 
7 # define TMPL template <class C, class Rep, class Ord>
8 # define VARIANT with<Rep,Ord>
9 # define SELF polynomial<C, VARIANT>
10 //======================================================================
11 namespace mmx {
12 //----------------------------------------------------------------------
13 // template<class R>
14 // template<class X> inline
15 // polynomial<R>::polynomial( const X& x )
16 // {
17 // let::assign(*this,x);
18 // }
19 //----------------------------------------------------------------------
20 extern "C" char *synaps_inputptr;
21 
22 TMPL inline
23 SELF::polynomial( const char * s, variables& vars ): data() {
24  typedef SELF polynomial_t;
25  // std::cout<<SELF(std::vector<int>());
26  // *this = polynomial_t(0);
27  //std::cout <<"Eq: >> "<<s<<std::endl ;
28  int n = strlen(s);
29  if (s[n-1]=='\n') {n--;}
30  if(s[n-1]==';')
31  {
32  synaps_input = new char[n+1];
33  memcpy(synaps_input,s,n); synaps_input[n]='\0';
35  }
36  else
37  {
38  synaps_input = new char[n+2];
39  memcpy(synaps_input,s,n);
40  synaps_input[n]=';'; synaps_input[n+1]='\0';
42  }
43  synaps_inputptr=synaps_input;
44  polynomial_t m(1);
45 
46 
47  bool sign = true;
48  bool first = true;
49  int Ask;
50 
51  for (;;) {
52  Ask = yylex();
53  //std::cout<<"yylval "<<yylval<<std::endl;
54  if (Ask == COEF) { /* monomial constant */
55  coeff_t c;
57  //std::cout<<"coeff "<<c << " "<< yylval;
58  free(yylval);
59  m *= c;
60  //std::cout<< " " << m<<std::endl;
61  }
62  else if (Ask == XID) {
63  char* p = std::strchr(yylval,'^');
64  // std::cout<<yylval<<" exp "<<p<<std::endl;
65  if (p == NULL)
66  {
67  m *= polynomial_t(1,1,vars[std::string(yylval)]);
68  // std::cout<<"var "<<m<<std::endl;
69  }
70  else {
71  p++;
72  m *= polynomial_t(1,atoi(p),vars[std::string(yylval,p-1)]);
73  // std::cout<<"var "<<m<<std::endl;
74  }
75  free(yylval);
76  }
77  else if (Ask == MULT);
78  else if (Ask == SUM) {
79  if (sign) {
80  // std::cout<<"add " <<m<<std::endl;
81  if(m!=(C)0) *this += m;
82  m = polynomial_t(1);
83  } else {
84  // std::cout<<"sub " <<m<<std::endl;
85  if(m!=(C)0) *this -= m;
86  sign = true;
87  m = polynomial_t(1);
88  }
89  }
90  else if (Ask == MINUS) {
91  if (first)
92  sign = false;
93  else if (sign) {
94  // std::cout<<"add " <<m<<std::endl;
95  if(m!=(C)0) *this += m;
96  sign = false;
97  m = polynomial_t(1);
98  }
99  else {
100  // std::cout<<"sub " <<m<<std::endl;
101 
102  if(m!=(C)0) *this -= m;
103  m = polynomial_t(1);
104  }
105  }
106  else if (Ask == TERMINATE) {
107  if (sign){
108  // std::cout<<"add " <<m<<std::endl;
109  if(m!=(C)0) *this += m;
110  }else{
111  // std::cout<<"sub " <<m<<std::endl;
112  if(m!=(C)0) *this -= m;
113  }
114  break;
115  }
116  first = false;
117  }
118  delete[] synaps_input;
119 }
120 //----------------------------------------------------------------------
121 // TMPL inline
122 // SELF & SELF::operator= ( const char * name ) {
123 // std::cout<<"= "<<std::endl;
124 // *this=SELF(name);
125 // return *this;
126 // }
127 //----------------------------------------------------------------------
128 TMPL inline
129 SELF & SELF::operator= ( const C& c) {
130  *this=SELF(c);
131  return *this;
132 }
133 //----------------------------------------------------------------------
134 TMPL bool
135 SELF::operator== (const typename SELF::value_type & c) const {
136  return rep () == c;
137 }
138 TMPL bool
139 SELF::operator!= (const typename SELF::value_type & c) const {
140  return ! (rep () == c);
141 }
142 //----------------------------------------------------------------------
143 //TMPL
144 //template <class S> inline
145 //SELF::polynomial (const polynomial<S> &mpol) : data (mpol) {};
146 //----------------------------------------------------------------------
147 TMPL template <class S, class UECT> inline
148 SELF::polynomial (const char* s, const UECT& bx)
149  : data () {
150  *this = self_t(s);
151  for(unsigned i=0;i<bx.size();i++) restrict(rep(),i,bx[i].lower(),bx[i].upper());
152 };
153 //----------------------------------------------------------------------
154  TMPL template <class S, class W, class VECT> inline
155 SELF::polynomial (const polynomial<S,W> &pol, const VECT& bx)
156  : data () {
157  let::assign(rep(),pol.rep());
158  for(int i=0;i<(int)bx.size()-1;i+=2) restrict(rep(),i,bx[i],bx[i+1]);
159 };
160 //----------------------------------------------------------------------
161 //TMPL inline
162 //SELF::polynomial (const char *s) : data(mpoldst<C>(s)) {};
163 //----------------------------------------------------------------------
164 TMPL inline const typename SELF::value_type
165 SELF::operator[]( int i ) const {
166  return rep()[i]; };
167 //----------------------------------------------------------------------
168 TMPL inline typename SELF::value_type &
169 SELF::operator[]( int i ) {
170  return rep()[i]; }
171 //----------------------------------------------------------------------
172 // TMPL inline SELF &
173 // SELF::operator+= (const SELF &mp) {
174 // add(rep(),mp.rep()); return *this; }
175 //----------------------------------------------------------------------
176 // TMPL inline SELF &
177 // SELF::operator+= (const C & x) {
178 // add (rep (), x); return *this; }
179 //----------------------------------------------------------------------
180 TMPL inline bool
181 SELF::operator== (const polynomial & mp) const {
182  return rep () == mp.rep (); }
183 //----------------------------------------------------------------------
184 TMPL template <class S> inline SELF &
185 SELF::operator= (const polynomial<S, VARIANT> & pol) {
186  this->~SELF(); new (this) SELF(pol);
187  return *this;
188 };
189 //----------------------------------------------------------------------
190 } //namespace mmx
191 //======================================================================
192 # undef TMPL
193 # undef SELF
194 # undef VARIANT
195 //======================================================================
196 # endif //realroot_polynomial_mthd_hpp
bool operator==(const extended< NT > &lhs, const extended< NT > &rhs)
Definition: extended.hpp:88
#define TMPL
Definition: polynomial_mthd.hpp:7
int yylex(void)
char * yylval
Definition: parser.hpp:31
#define MINUS
Definition: parser.hpp:19
R & rep(R &r)
Definition: shared_object.hpp:180
void restrict(bernstein< C > &rst, int v, const T &a, const T &b)
Definition: tensor_bernstein_fcts.hpp:347
#define COEF
Definition: parser.hpp:16
char * synaps_inputlim
Definition: parser.hpp:37
char * synaps_input
Definition: parser.hpp:34
#define SUM
Definition: parser.hpp:18
#define MULT
Definition: parser.hpp:20
#define SELF
Definition: polynomial_mthd.hpp:9
char * synaps_inputptr
Definition: monomial.hpp:310
#define XID
Definition: parser.hpp:17
const C & c
Definition: Interval_glue.hpp:45
double C
Definition: solver_mv_fatarcs.cpp:16
int sign(const QQ &a)
Definition: GMP.hpp:60
bool operator!=(const extended< NT > &lhs, const extended< NT > &rhs)
Definition: extended.hpp:95
void assign(A &a, const B &b)
Generic definition of the assignement function.
Definition: assign.hpp:97
Definition: array.hpp:12
#define TERMINATE
Definition: parser.hpp:21
Home