Borderbasix

MPoly.hpp
Go to the documentation of this file.
1 /*********************************************************************
2 * This file is part of the source code of borderbasix *
3 * (C) B. Mourrain, Ph. Trebuchet *
4 **********************************************************************/
5 #ifndef _MPoly_hpp_
6 #define _MPoly_hpp_
7 
8 #include "MPOLYNOMIAL.hpp"
9 #include <stdlib.h>
10 #include <string.h>
11 #include <string>
12 #include <cassert>
13 #include "parser.hpp"
15 
28 template <class R, class O> class MPoly {
29 public:
30  // Types
31  typedef typename R::value_type monom_t;
32  typedef typename monom_t::coeff_t coeff_t;
33  typedef O order_t;
34  typedef typename R::iterator iterator;
35  typedef typename R::const_iterator const_iterator;
36  typedef typename R::reverse_iterator reverse_iterator;
37  typedef typename R::const_reverse_iterator const_reverse_iterator;
38 
39  typedef MPoly<R,O> self_t;
40 
41  // Constructors
42  MPoly():rep(){}
43  MPoly(const monom_t & m):rep() { rep.push_back( m); }
44  MPoly(const coeff_t & c):rep() { rep.push_back(monom_t(c));}
45  MPoly(int, const monom_t *);
46  MPoly(const self_t & P) {
48  }
49  MPoly(const int & i): rep(){ rep.push_back(monom_t(i));}
50  MPoly(const char *);
51  MPoly(char *);
52 
53  // void make();
54  // Destructor
55  // no dynamic allocation at this level.
56 
57  // Assignment operators
58  self_t & operator=(const self_t &);
59  self_t & operator+=(const monom_t &);
60  self_t & operator+=(const self_t &);
61  self_t & operator-=(const monom_t &);
62  self_t & operator-=(const self_t &);
63  self_t & operator*=(const coeff_t &);
64  self_t & operator*=(const monom_t &);
65  self_t & operator*=(const self_t &);
66  self_t & operator/=(const monom_t &);
67  self_t & operator/=(const self_t & Q);
68 
69  // Boolean operators and functions
70  bool operator==(int n) const;
71  bool operator!=(int n) const {return !(*this==n);}
72 
73  // friend bool operator == <>(const self_t &, const self_t &);
74  // friend bool operator != <>(const self_t &, const self_t &);
75  // friend bool operator < <>(const self_t &, const self_t &);
76  // friend bool operator <= <> (const self_t &, const self_t &);
77  // friend bool operator > <>(const self_t &, const self_t &);
78  // friend bool operator >= <> (const self_t &, const self_t &);
79 
80  // Comput operators and functions
81  self_t operator-() const;
82  // friend self_t operator + <>(const self_t &, const self_t &);
83  // friend self_t & operator - <>(const self_t &, const self_t &);
84  // friend self_t & operator * <>(const self_t &, const monom_t &);
85  // friend self_t & operator * <>(const self_t &, const self_t &);
86  // friend self_t & operator / <>(const self_t &, const self_t &);
87 
88  // Input/Output operator
89  //friend template <class R, class O>
90  //istream & operator >> (istream & is, MPoly<R,O> & P);
91  //istream & operator >> <>(istream &, self_t &);
92  //friend template <class R, class O>
93  //ostream & operator << (ostream & is, MPoly<R,O> & P);
94  //ostream & operator << <>(ostream &, const self_t &);
95 
96  // Iterator functions
97  iterator begin () { return rep.begin(); }
98  const_iterator begin () const { return rep.begin(); }
99  reverse_iterator rbegin () { return rep.rbegin(); }
100  const_reverse_iterator rbegin () const { return rep.rbegin(); }
101 
102  iterator end () { return rep.end(); }
103  const_iterator end () const { return rep.end(); }
104  reverse_iterator rend () { return rep.rend(); }
105  const_reverse_iterator rend () const { return rep.rend(); }
106 
107  int size() const {return rep.size();}
108 
109  // Data
110  R rep;
111 
112  // private:
113  // // private member functions
114  // iterator add (const self_t &, iterator);
115  // iterator sub (const self_t &, iterator);
116 };
117 
118 //----------------------------------------------------------------------
119 //char *myinput;
120 //char *myinputptr;
121 //char *myinputlim;
122 //char *yylval;
123 
124 //extern "C" int yywrap (void);
125 //extern "C" int yylex(void);
126 
127 //----------------------------------------------------------------------
128 // Constructors
129 //----------------------------------------------------------------------
130 template <class R, class O>
131 inline MPoly<R,O>::MPoly (int s, const monom_t *t) {
132  for (int i = 0; i<s; i++) *this += t[i];
133 }
134 
135 template<class R, class O>
136 MPoly<R,O>::MPoly(const char* s) {
137  int n = strlen(s);
138  if(s[n-1]!=';'){
139  char* str = new char[n+2];
140  memcpy(str,s,n);
141  str[n]=';'; str[n+1]='\0';
142  yy_scan_string(str);
143  } else {
144  yy_scan_string(s);
145  }
147  p.yyparse();
148  if(p.yypol!=0)
149  MPOLYNOMIAL__copy(rep,p.yypol->rep);
150 }
151 
152 template<class R, class O>
154  yy_scan_string(const_cast<const char*>(s));
156  p.yyparse();
157  if(p.yypol!=0)
158  MPOLYNOMIAL__copy(rep,p.yypol->rep);
159 }
160 
161 //----------------------------------------------------------------------
162 // Assignment operators
163 //----------------------------------------------------------------------
164 template <class R, class O>
166 {
167  // cout <<" =[p] "<<P<<endl;
168  MPOLYNOMIAL__copy(rep,P.rep);
169  return *this;
170 }
171 
172 template <class R, class O>
173 inline MPoly<R,O>& MPoly<R,O>::operator += (const typename MPoly<R,O>::monom_t & m){
174  typedef typename MPoly<R,O>::monom_t mon;
175  typedef typename mon::coeff_t C;
176  if (m.GetCoeff()!=(C)0)
177  MPOLYNOMIAL__insert<R,O>(rep, rep.begin(), m);
178  return *this;
179 }
180 
181 template <class R, class O>
183 {
184  if (P==0) return *this;
185  if (*this==0) {*this = P; return *this;}
186 
187  MPOLYNOMIAL__plus<R,O>(rep,P.rep,begin());
188  return *this;
189 }
190 
191 template <class R, class O>
192 inline MPoly<R,O>& MPoly<R,O>::operator -= (const typename MPoly<R,O>::monom_t & m){
193  if (m.GetCoeff()!=(typename R::value_type::coeff_t)0){
194  MPOLYNOMIAL__insert<R,O>(rep,rep.begin(),-m);
195  }
196  return *this;
197 }
198 
199 template <class R, class O>
201  //cout<<"*this"<<*this<<endl;
202  //cout<<"P "<<P<<endl;
203  if (P==0) return *this;
204  if (*this==0) {*this = -P; return *this;}
205  MPOLYNOMIAL__sub<R,O>(rep,P.rep,begin());
206  return *this;
207 }
208 
209 template <class R, class O>
210 inline MPoly<R,O>& MPoly<R,O>::operator /= (const typename MPoly<R,O>::monom_t & m){
211  if ( (*this!=0) && (m.GetCoeff() !=(typename R::value_type::coeff_t)0) ) {
212  MPoly<R,O> P(*this);
213  for (iterator i = P.begin(); i != P.end(); i++) {
214  int n=m.Lvar();
215  if (n<=(*i).Lvar())
216  while (((*i).GetDegree(n) >= m.GetDegree(n) ) && ( n >= 0)) n--;
217  if (n<0)
218  *i=div(*i,m);
219  else return *this;
220  }
221  MPOLYNOMIAL__copy(this->rep,P.rep);
222  }
223  return *this;
224 }
225 template <class R, class O>
226 inline MPoly<R,O>& MPoly<R,O>::operator *= (const typename MPoly<R,O>::coeff_t & m){
227  //xxx IF m<> 0
228  MPoly<R,O> P;
229  for (iterator i = begin(); i != end(); i++) {
230  (*i)*= m;
231  //monom_t mon((*i) * m);
232  //MPOLYNOMIAL__insert<monom_t,O>(P.rep,P.begin(),mon);
233  }
234  //xxx Polypack::sort(this->rep,O,IFN<O,MONOMIALORDER>::VAL());
235  return *this;
236 }
237 template <class R, class O>
238 inline MPoly<R,O>& MPoly<R,O>::operator *= (const typename MPoly<R,O>::monom_t & m){
239  //xxx IF m<> 0
240  MPoly<R,O> P;
241  for (iterator i = begin(); i != end(); i++) {
242  (*i)*= m;
243  //monom_t mon((*i) * m);
244  //MPOLYNOMIAL__insert<monom_t,O>(P.rep,P.begin(),mon);
245  }
246  //xxx Polypack::sort(this->rep,O,IFN<O,MONOMIALORDER>::VAL());
247  return *this;
248 }
249 
250 template <class R, class O>
252  // cout << "*="<<endl;
253  if ( (*this !=0) && (Q !=0) ) {
254  bool copy;
255  const MPoly<R,O> *NQ;
256  copy = (&rep == &Q.rep);
257  if (copy)
258  NQ = new MPoly<R,O>(Q);
259  else
260  NQ = &Q;
261 
262  iterator i, j;
263  const_iterator iP,iQ = NQ->begin();
264  monom_t *m = new monom_t;
265 
266  MPoly<R,O> P(*this);
267  // swap(P.rep,this->rep);
268  // cout <<P<<" "<<*this<<endl;
269  for(i=begin();i != end();++i) (*i)*=(*iQ);
270  //xxx should not imply a copy of m;
271  // this->rep.push_back(m); //xxx use output iterator
272 
273  (iQ++);
274  i=begin();
275  for (; iQ != NQ->end();iQ++) {
276  j = begin();
277  for (iP = P.begin(); iP != P.end(); iP++) {
278  *m = (*iP) * (*iQ);
279  j = MPOLYNOMIAL__insert<R,O>(this->rep,j,*m);
280  }
281  }
282  delete m;
283  if (copy) delete(NQ);
284  }
285  else *this=MPoly<R,O>();
286  return *this;
287 }
288 
289 template <class R, class O>
291  if (*this!=0 && Q!=0){
292  bool copy;
293  if (Degree(Q) > 0) {
294  const MPoly<R,O> *NQ;
295  copy = (&rep == &Q.rep);
296  if (copy)
297  NQ = new MPoly<R,O>(Q);
298  else
299  NQ = &Q;
300  MPoly<R,O> Quo;// XP((*this));
301  while (*this!=0) {
302  int n=Q.begin()->Lvar();
303  if (n<=(*this).begin()->Lvar())
304  while (((*this).begin()->GetDegree(n) >= Q.begin()->GetDegree(n) ) && ( n >= 0)) n--;
305  if (n<0) {
306  typename MPoly<R,O>::monom_t m=(div(*((*this).begin()),*(Q.begin())));
307  Quo+=m;
308  (*this)-=Q*m;
309  }
310  else
311  (*this)-=(*((*this).begin()));
312  }
313  //Rem = XP-Q*Quo;
314  (*this)=Quo;
315  }
316  else (*this)/= *(Q.begin());
317  }
318  else
319  (*this)=MPoly<R,O>(0);
320  return *this;
321 }
322 
323 //----------------------------------------------------------------------
324 // Boolean operators and functions
325 //----------------------------------------------------------------------
326 template <class R, class O> inline
327 bool MPoly<R,O>::operator==(int n) const
328 {
329  if(n==0)
330  if (this->rep.empty())
331  return true;
332  else
333  return false;
334  else
335  if (this->rep.empty())
336  return false;
337  else
338  return( this->rep.size()==1 && (*this - MPoly<R,O>(n)) ==0);
339 }
340 template <class R, class O> inline
341 bool operator == (const MPoly<R,O> & P, const MPoly<R,O> & Q)
342 {
343  return P.rep == Q.rep;
344 }
345 
346 template <class R, class O> inline
347 bool operator != (const MPoly<R,O> & P, const MPoly<R,O> & Q)
348 {
349  return !(P.rep == Q.rep);
350 }
351 
352 template <class R, class O> inline
353 bool operator < (const MPoly<R,O> & P, const MPoly<R,O> & Q) {
354  typename MPoly<R,O>::const_iterator iP = P.begin();
355  typename MPoly<R,O>::const_iterator iQ = Q.begin();
356  while (iP != P.end() && iQ != Q.end() && IsComparable(*iP,*iQ)) {
357  ++iP;
358  ++iQ;
359  }
360  if (iQ == Q.end())
361  return false;
362  if (iP == P.end())
363  return true;
364  return O::less(*iP,*iQ);
365 }
366 
367 template <class R, class O> inline
368 bool operator > (const MPoly<R,O> & P, const MPoly<R,O> & Q){
369  return Q < P;
370 }
371 
372 template <class R, class O> inline
373 bool operator >= (const MPoly<R,O> & P, const MPoly<R,O> & Q){
374  return !(P < Q);
375 }
376 
377 template <class R, class O> inline
378 bool operator <= (const MPoly<R,O> & P, const MPoly<R,O> & Q){
379  return !(Q < P);
380 }
381 
382 //----------------------------------------------------------------------
383 // Comput operators and functions
384 //----------------------------------------------------------------------
385 template <class R, class O> inline
387  MPoly<R,O> r(*this);
388  for (iterator i = r.begin(); i != r.end(); i++) *i = -(*i);
389  return (r);
390 }
391 template <class R, class O> inline
393 {
394  return (MPoly<R,O>(P)/= Q);
395 }
396 
397 template <class R, class O> inline
399 {
400  return (MPoly<R,O>(P)+= Q);
401 }
402 
403 template <class R, class O> inline
405 {
406  return (MPoly<R,O>(P)-= Q);
407 }
408 
409 template <class R, class O> inline
411  const typename MPoly<R,O>::monom_t & m)
412 {
413  return (MPoly<R,O>(P) *= m);
414 }
415 
416 template <class R, class O> inline
418 {
419  return (MPoly<R,O>(P)*= Q);
420 }
421 //----------------------------------------------------------------------
422 template <class R, class O> inline
423 int leading_var (const MPoly<R,O> & P) {
424  return MPOLYNOMIAL__lvar(P.rep);
425 }
426 
427 template <class R, class O> inline
428 int Degree (const MPoly<R,O> & P) {
429  return MPOLYNOMIAL__degree(P.rep);
430 }
431 
432 //----------------------------------------------------------------------
433 // Input/putput operators
434 //----------------------------------------------------------------------
435 template <class R, class O> inline
436 std::istream & operator >> (std::istream & is, MPoly<R,O> & P) {
437  std::string s,ss = "";
438  int p;
439  char *buf;
440 
441  for (;;) {
442  is >> s;
443  p = s.find(';');
444  if (p == -1)
445  ss += s;
446  else {
447  ss += s.substr(0,p+1);
448  break;
449  }
450  }
451  buf = (char *)malloc(sizeof(char)*ss.length() +1);
452  strcpy(buf,ss.c_str());
453  P = MPoly<R,O>(buf);
454  free(buf);
455  return is;
456 }
457 
458 template <class R, class O> inline
459 std::ostream & operator << (std::ostream & os, const MPoly<R,O> & P){
460  typename MPoly<R,O>::const_iterator i = P.begin();
461  if ( i == P.end() )
462  os << '0';
463  while ( i != P.end() )
464  {
465  os << (*i++);
466  if ( i != P.end() && has_positive_sign(i->GetCoeff()) )
467  os << '+';
468  }
469  return os;
470 }
471 
472 template <class R, class O, class T>
473 MPoly<R,O> pow(const MPoly<R,O> & P, T n)
474 {
475  MPoly<R,O> result(typename MPoly<R,O>::coeff_t(1));
476  MPoly<R,O> temp(P);
477  assert(n>=0);
478  while(n > 0 )
479  {
480  if (n & 1) result *= temp;
481  temp *= temp;
482  n /= 2;
483  }
484  return result;
485 }
486 
487 
488 // Substitute the i-th variable of the polynomial P by the polynomial Q
489 template <class R, class O>
491 {
492  typename MPoly<R,O>::iterator it;
493  MPoly<R,O> result;
494  for(it=P.begin(); it!=P.end(); it++) {
495  MPoly<R,O> temp_pol;
496  typename MPoly<R,O>::monom_t temp(*it);
498  t = temp[i];
499  if(t > 0) {
500  temp.SetDegree(i,0);
501  temp_pol= pow(Q,t) * temp;
502  } else {
503  temp_pol = MPoly<R,O>(temp);
504  }
505  result += temp_pol;
506  }
507 
508  return result;
509 }
510 
511 #endif //
bool operator!=(int n) const
Definition: MPoly.hpp:71
R::value_type monom_t
Definition: MPoly.hpp:31
int size() const
Definition: MPoly.hpp:107
const_iterator end() const
Definition: MPoly.hpp:103
MPOLY * yypol
Definition: parser.hpp:644
MPoly()
Definition: MPoly.hpp:42
R rep
Definition: MPoly.hpp:110
Multivariate polynomials.
Definition: MPoly.hpp:28
void free(void *)
M div(const M &m1, const M &m2)
Definition: Monom.hpp:217
iterator end()
Definition: MPoly.hpp:102
MPoly(const self_t &P)
Definition: MPoly.hpp:46
self_t & operator/=(const monom_t &)
reverse_iterator rbegin()
Definition: MPoly.hpp:99
int yyparse(void)
Definition: parser.hpp:701
MPoly< R, O > pow(const MPoly< R, O > &P, T n)
Definition: MPoly.hpp:473
const_reverse_iterator rbegin() const
Definition: MPoly.hpp:100
self_t & operator=(const self_t &)
Definition: MPoly.hpp:165
YY_BUFFER_STATE yy_scan_string(yyconst char *yy_str)
Definition: lex.yy.c:1277
R::iterator iterator
Definition: MPoly.hpp:34
MPoly< R, O > self_t
Definition: MPoly.hpp:39
reverse_iterator rend()
Definition: MPoly.hpp:104
const_reverse_iterator rend() const
Definition: MPoly.hpp:105
R::const_reverse_iterator const_reverse_iterator
Definition: MPoly.hpp:37
void * malloc(YYSIZE_T)
MPoly(const int &i)
Definition: MPoly.hpp:49
self_t & operator-=(const monom_t &)
bool operator!=(const MPoly< R, O > &P, const MPoly< R, O > &Q)
Definition: MPoly.hpp:347
bool operator==(int n) const
Definition: MPoly.hpp:327
MPoly< R, O > operator/(const MPoly< R, O > &P, const MPoly< R, O > &Q)
Definition: MPoly.hpp:392
MSKconetypee MSKrealt MSKint32t MSKint32t j
Definition: mosek.h:2421
monom_t::coeff_t coeff_t
Definition: MPoly.hpp:32
MPoly(const monom_t &m)
Definition: MPoly.hpp:43
MSKCONST char * str
Definition: mosek.h:2317
self_t operator-() const
Definition: MPoly.hpp:386
C coeff_t
Definition: Monom.hpp:26
MPoly< R, O > operator-(const MPoly< R, O > &P, const MPoly< R, O > &Q)
Definition: MPoly.hpp:404
self_t & operator*=(const coeff_t &)
bool operator==(const MPoly< R, O > &P, const MPoly< R, O > &Q)
Definition: MPoly.hpp:341
int leading_var(const MPoly< R, O > &P)
Definition: MPoly.hpp:423
R::reverse_iterator reverse_iterator
Definition: MPoly.hpp:36
int MPOLYNOMIAL__degree(const R &p)
Definition: MPOLYNOMIAL.hpp:99
Mon mon
Definition: solver_bb_floating.cpp:136
MSKrealt * c
Definition: mosek.h:2678
MPoly< R, O > operator*(const MPoly< R, O > &P, const typename MPoly< R, O >::monom_t &m)
Definition: MPoly.hpp:410
MSKint32t MSKint32t MSKint32t i
Definition: mosek.h:2278
bool operator>(const MPoly< R, O > &P, const MPoly< R, O > &Q)
Definition: MPoly.hpp:368
int MPOLYNOMIAL__lvar(const R &p)
Definition: MPOLYNOMIAL.hpp:89
MSKrescodee r
Definition: mosek.h:2321
R::const_iterator const_iterator
Definition: MPoly.hpp:35
bool operator>=(const MPoly< R, O > &P, const MPoly< R, O > &Q)
Definition: MPoly.hpp:373
void MPOLYNOMIAL__copy(T &p, const T &q)
Definition: MPOLYNOMIAL.hpp:108
Parser for polynomials.
Definition: parser.hpp:642
MPoly< R, O > operator+(const MPoly< R, O > &P, const MPoly< R, O > &Q)
Definition: MPoly.hpp:398
int Degree(const MPoly< R, O > &P)
Definition: MPoly.hpp:428
self_t & operator+=(const monom_t &)
bool has_positive_sign(const X &x)
Definition: has_sign.hpp:5
MPoly< R, O > subs(MPoly< R, O > P, int i, MPoly< R, O > Q)
Definition: MPoly.hpp:490
MPoly(const coeff_t &c)
Definition: MPoly.hpp:44
iterator begin()
Definition: MPoly.hpp:97
const_iterator begin() const
Definition: MPoly.hpp:98
std::istream & operator>>(std::istream &is, MPoly< R, O > &P)
Definition: MPoly.hpp:436
O order_t
Definition: MPoly.hpp:33
Home  |  Download & InstallContributions