Developer documentation

sparse_dual.hpp
Go to the documentation of this file.
1 /*********************************************************************
2 * This file is part of the source code of the realroot library.
3 * Author(s): B. Mourrain, GALAAD, INRIA
4 **********************************************************************/
5 
6 #ifndef realroot_sparse_dual_hpp
7 #define realroot_sparse_dual_hpp
8 
10 
11 //--------------------------------------------------------------------
12 namespace mmx {
13  namespace sparse {
14 //----------------------------------------------------------------------
15 template <class C,
16  class O=DegRevLex>
17 struct dual: public monomial_seq<C,O> {
18 
19  // Types
20 
21  typedef monomial_seq<C,O> R;
22  typedef monomial_seq<C,O> base_t;
23 
24  typedef typename R::monom_t monom_t;
25  typedef C coeff_t;
26  typedef O order_t;
27  typedef typename R::iterator iterator;
28  typedef typename R::const_iterator const_iterator;
29  typedef typename R::reverse_iterator reverse_iterator;
30  typedef typename R::const_reverse_iterator const_reverse_iterator;
31  typedef monomial_seq<C,O> poly_t;
32  typedef dual<C,O> self_t;
33 
34  // Constructors
35  dual(): poly_t() {}
36  dual(const coeff_t & c): poly_t(c) {}
37  dual(const coeff_t & c, int d, int v): poly_t(c,-d,v) {}
38  dual(const monom_t & m): poly_t(m) {neg_exponent(*this);}
39  // dual(int n, const monom_t * t): poly_t(n,t) {neg_exponent(*this);}
40 
41  dual(const self_t & P): poly_t(P){}
42  dual(const char * s, variables& vars):
43  poly_t(s,vars) {neg_exponent(*this);}
44  dual(const char * s, const variables& vars):
45  poly_t(s,vars) {neg_exponent(*this);}
46 
47  template<class Q>
48  dual(const dual<C,Q>& p) : poly_t(p) {}
49 
50  // Destructor
51  // no dynamic allocation at this level.
52 
53  // static Variable var;
54 
55  // Assignment operators
56  self_t & operator= (const self_t & x)
57  {
58  this->base_t::operator=(x); return *this;
59  }
60  self_t & operator= (const coeff_t & c) {*this=self_t(c); return *this;}
61  self_t & operator= (int n) {*this=self_t(n); return *this;}
62  self_t & operator= (const monom_t & m) {*this=self_t(m); return *this;}
63 
64  C operator()(const poly_t& p);
65 
66 };
67 
68 
69 template<class C, class O>
71 {
72  typedef typename dual<C,O>::iterator iterator;
73  for(iterator it=p.begin();it != p.end();it++)
74  {
75  for(int j=0;j<(it->nvars()+1);j++)
76  {
77  it->set_expt(j,-(*it)[j]);
78  }
79  }
80 }
81 //--------------------------------------------------------------------
86 template<class C, class O>
87 C dual<C,O>::operator()(const monomial_seq<C,O>& p)
88 {
89  monomial_seq<C,O> t= ((monomial_seq<C,O>)(*this))*p;
90  typedef typename monomial_seq<C,O>::reverse_iterator iterator;
91  iterator it;
92  for(it=t.rbegin(); it != t.rend();it++)
93  {
94  bool nul=true;
95  for(unsigned j=0;j<(it->nvars()+1) && nul;j++)
96  {
97  if((*it)[j]!=0) nul=false;
98  }
99  if(nul) return it->coeff();
100  }
101  return C(0);
102 }
103 //--------------------------------------------------------------------
104 template<class C, class O> inline void
105 add(dual<C,O>& res, const dual<C,O>& a, const dual<C,O>& b) {
106  add((monomial_seq<C,O>&)res,(const monomial_seq<C,O>&)a,(const monomial_seq<C,O>&)b);
107 }
108 //--------------------------------------------------------------------
109 template<class C, class O> inline void
110 add(dual<C,O>& res, const dual<C,O>& a, const C& b) {
111  add((monomial_seq<C,O>&)res,(const monomial_seq<C,O>&)a,b);
112 }
113 //--------------------------------------------------------------------
114 template<class C, class O> inline void
115 add(dual<C,O>& res, const C& b , const dual<C,O>& a) {
116  add((monomial_seq<C,O>&)res,(const monomial_seq<C,O>&)a,b);
117 }
118 //--------------------------------------------------------------------
119 template<class C, class O> inline void
120 sub(dual<C,O>& res, const dual<C,O>& a, const dual<C,O>& b) {
121  sub((monomial_seq<C,O>&)res,(const monomial_seq<C,O>&)a,(const monomial_seq<C,O>&)b);
122 }
123 //--------------------------------------------------------------------
124 template<class C, class O> inline void
125 sub(dual<C,O>& res, const dual<C,O>& a, const C& b) {
126  sub((monomial_seq<C,O>&)res,(const monomial_seq<C,O>&)a,b);
127 }
128 //--------------------------------------------------------------------
129 template<class C, class O> inline void
130 sub(dual<C,O>& res, const C& a, const dual<C,O>& b) {
131  sub((monomial_seq<C,O>&)res,a,(const monomial_seq<C,O>&)b);
132 }
133 //--------------------------------------------------------------------
134 template<class C, class O> inline void
135 mul(dual<C,O>& res, const dual<C,O>& a, const dual<C,O>& b) {
136  mul((monomial_seq<C,O>&)res,(const monomial_seq<C,O>&)a,(const monomial_seq<C,O>&)b);
137 }
138 //--------------------------------------------------------------------
139 template<class C, class O> inline void
140 mul(dual<C,O>& res, const dual<C,O>& a, const C& b) {
141  mul((monomial_seq<C,O>&)res,(const monomial_seq<C,O>&)a,b);
142 }
143 //--------------------------------------------------------------------
144 template<class C, class O> inline void
145 mul(dual<C,O>& res, const C& b, const dual<C,O>& a) {
146  mul((monomial_seq<C,O>&)res,(const monomial_seq<C,O>&)a,b);
147 }
148 //--------------------------------------------------------------------
154 template<class C, class O> void
155 mul(dual<C,O>& res, const monomial_seq<C,O>& p, const dual<C,O>& l) {
156  monomial_seq<C,O> t;
157  mul(t,p,(monomial_seq<C,O>)l);
158  typedef typename dual<C,O>::const_iterator iterator;
159  for(iterator it=t.begin();it != t.end();it++)
160  {
161  bool neg=true;
162  for(unsigned j=0;j<it->size() && neg;j++) {
163  if((*it)[j]>0) neg=false;
164  }
165  if(neg) res.push_back(*it);
166  }
167 }
168 
169 // template<class C, class O> Seq<polynom< monomial_seq<C,O> >
170 // coefficients(const Polynomial & f, const int & v ) {
171 // unsigned N = degree(f,v)+1;
172 // Seq<Polynomial> r(N);
173 // typename Polynomial::Monomial m;
174 // for(typename Polynomial::const_iterator it=f.begin(); it != f.end(); it++) {
175 // m=*it;
176 // if (v<(int)m.size()) m.set_expt(v,0);
177 // r[(*it)[v]]+= Polynomial(m);
178 // }
179 // return r;
180 // }
181 
182 
183 //--------------------------------------------------------------------
188 template<class C, class O> void
189 mul(monomial_seq<C,O>& res, const dual<C,O>& l, const monomial_seq<C,O>& p) {
190  monomial_seq<C,O> t;
191  mul(t,(monomial_seq<C,O>)l,p);
192  typedef typename dual<C,O>::const_iterator iterator;
193  for(iterator it=t.begin();it != t.end();it++)
194  {
195  bool pos=true;
196  for(unsigned j=0;j<it->size() && pos;j++) {
197  if((*it)[j]<0) pos=false;
198  }
199  if(pos) res.push_back(*it);
200  }
201 }
202 
203 //----------------------------------------------------------------------
204 template<class C, class O> inline void
205 div(dual<C,O> &f, const C& c) {
206  for(typename dual<C,O>::iterator it=f.begin(); it != f.end(); it++)
207  it->coeff()/=c;
208 }
209 
210 //----------------------------------------------------------------------
211 template<class C, class O> void
212 div(dual<C,O> &r, const dual<C,O>& p, const C & c) {
213  r = p; div(r,c);
214 }
215 
216 //--------------------------------------------------------------------
217 #ifndef MMX_WITH_PLUS_SIGN
218 #define MMX_WITH_PLUS_SIGN
219  template<class X> bool with_plus_sign(const X& x) {return x>0;}
220 #endif //MMX_WITH_PLUS_SIGN
221 
222 template <class OSTREAM, class C> inline OSTREAM &
223 print_dual (OSTREAM & os, const monom<C>& m, const variables& V) {
224  if (m.coeff() ==0 ) {
225  os<<"0"; return os;
226  }
227  int i=m.size()-1;
228  while(i> -1 && m[i]==0) i--;
229  if(i<0)
230  os <<m.coeff();
231  else {
232  if(m.coeff() != 1) {
233  if(m.coeff()==-1)
234  os << "-";
235  else {
236  os << m.coeff(); os <<"*";
237  }
238  }
239  }
240  bool first=true;
241  for (unsigned i = 0; i < m.size(); ++i) {
242  if (m[i] !=0) {
243  if(first)
244  first =false;
245  else
246  os<<'*';
247  os << 'd'<<V[i];
248  if (m[i] != -1)
249  os << '^' <<(-m[i]);
250  }
251  }
252  return os;
253 }
254 //--------------------------------------------------------------------
256 
259 template <class OSTREAM, class C, class O> OSTREAM &
260 print(OSTREAM & os, const dual<C,O> & P, const variables& V ) {
261  if ( P.begin() == P.end() ) {
262  os << '0';
263  return os;
264  }
265  typename dual<C,O>::const_iterator i = P.begin();
266  while ( i != P.end() ) {
267  if ( with_plus_sign(i->coeff()) && i !=P.begin())
268  os <<'+';
269  print_dual(os,*i++,V);
270  }
271  return os;
272 }
273 //--------------------------------------------------------------------__
274 } //namespace sparse
275 
276 //======================================================================
277 # define Polynomial sparse::dual<C,O>
278 # define TMPL template<class C, class O>
279 
280 template<class A, class B> struct use;
281 struct operators_of;
282 
284 
285  static inline void
286  add (Polynomial &r, const Polynomial &a ) {
287  sparse::add (r, a);
288  }
289  static inline void
290  add (Polynomial &r, const Polynomial &a, const Polynomial &b) {
291  sparse::add (r, a, b);
292  }
293  static inline void
294  add (Polynomial &r, const Polynomial &a, const C & b) {
295  sparse::add (r, a, b);
296  }
297  static inline void
298  add (Polynomial &r, const C & a, const Polynomial &b) {
299  sparse::add (r, b, a);
300  }
301  static inline void
302  sub (Polynomial &r, const Polynomial &a ) {
303  sparse::sub (r, a );
304  }
305  static inline void
306  sub (Polynomial &r, const Polynomial &a, const Polynomial &b) {
307  sparse::sub (r, a, b);
308  }
309  static inline void
310  sub (Polynomial &r, const C & a, const Polynomial &b) {
311  sparse::sub (r, a, b);
312  }
313  static inline void
314  sub (Polynomial &r, const Polynomial &a, const C & b) {
315  sparse::sub (r, a, b);
316  }
317  static inline void
318  mul (Polynomial &r, const Polynomial &a ) {
319  sparse::mul (r, a );
320  }
321  static inline void
322  mul (Polynomial &r, const Polynomial &a, const Polynomial &b) {
323  sparse::mul (r, a, b);
324 }
325  static inline void
326  mul (Polynomial &r, const Polynomial &a, const C & b) {
327  sparse::mul (r, a, b); }
328  static inline void
329  mul (Polynomial &r, const C & a, const Polynomial &b) {
330  sparse::mul (r, b, a);
331  }
332  static inline void
333  div (Polynomial &r, const Polynomial &a, const Polynomial &b) {
334  sparse::div (r, a, b);
335  }
336  static inline void
337  div (Polynomial &r, const Polynomial &a, const C & b) {
338  sparse::div (r, a, b);
339  }
340  static inline void
341  rem (Polynomial &r, const Polynomial &a, const Polynomial &b) {
342  sparse::rem (r, b, a);
343  }
344 };
345 # undef Polynomial
346 # undef TMPL
347 //====================================================================
348 } //namespace mmx
349 #endif // realroot_sparse_dual_hpp
350 
void sub(dual< C, O > &res, const dual< C, O > &a, const dual< C, O > &b)
Definition: sparse_dual.hpp:120
void mul(dual< C, O > &res, const dual< C, O > &a, const dual< C, O > &b)
Definition: sparse_dual.hpp:135
void neg(Interval< C, r > &a)
Definition: Interval_fcts.hpp:200
const C & b
Definition: Interval_glue.hpp:25
dual(const char *s, const variables &vars)
Definition: sparse_dual.hpp:44
TMPL X
Definition: polynomial_operators.hpp:148
R::iterator iterator
Definition: sparse_dual.hpp:27
C coeff_t
Definition: sparse_dual.hpp:25
dual(const char *s, variables &vars)
Definition: sparse_dual.hpp:42
unsigned size() const
Definition: monomial.hpp:135
Definition: sparse_dual.hpp:17
self_t & operator=(const self_t &x)
Definition: sparse_dual.hpp:56
Degree Reverse Lexicographic monomial ordering.
Definition: monomial_ordering.hpp:38
static void sub(Polynomial &r, const Polynomial &a, const C &b)
Definition: sparse_dual.hpp:314
R::reverse_iterator reverse_iterator
Definition: sparse_dual.hpp:29
bool with_plus_sign(const X &x)
Definition: sparse_dual.hpp:219
static void add(Polynomial &r, const C &a, const Polynomial &b)
Definition: sparse_dual.hpp:298
monomial_seq< C, O > R
Definition: sparse_dual.hpp:21
dual(const monom_t &m)
Definition: sparse_dual.hpp:38
static void mul(Polynomial &r, const C &a, const Polynomial &b)
Definition: sparse_dual.hpp:329
void add(dual< C, O > &res, const dual< C, O > &a, const dual< C, O > &b)
Definition: sparse_dual.hpp:105
static void sub(Polynomial &r, const Polynomial &a, const Polynomial &b)
Definition: sparse_dual.hpp:306
static void sub(Polynomial &r, const Polynomial &a)
Definition: sparse_dual.hpp:302
dual(const coeff_t &c)
Definition: sparse_dual.hpp:36
O order_t
Definition: sparse_dual.hpp:26
dual()
Definition: sparse_dual.hpp:35
dual(const coeff_t &c, int d, int v)
Definition: sparse_dual.hpp:37
static void sub(Polynomial &r, const C &a, const Polynomial &b)
Definition: sparse_dual.hpp:310
static void div(Polynomial &r, const Polynomial &a, const C &b)
Definition: sparse_dual.hpp:337
void neg_exponent(dual< C, O > &p)
Definition: sparse_dual.hpp:70
TMPL void rem(Polynomial &r, const Polynomial &a, const Polynomial &b)
Definition: sparse_monomials.hpp:446
static void add(Polynomial &r, const Polynomial &a, const C &b)
Definition: sparse_dual.hpp:294
Definition: polynomial.hpp:34
OSTREAM & print_dual(OSTREAM &os, const monom< C > &m, const variables &V)
Definition: sparse_dual.hpp:223
Definition: polynomial.hpp:37
static void mul(Polynomial &r, const Polynomial &a, const C &b)
Definition: sparse_dual.hpp:326
static void mul(Polynomial &r, const Polynomial &a, const Polynomial &b)
Definition: sparse_dual.hpp:322
R::const_reverse_iterator const_reverse_iterator
Definition: sparse_dual.hpp:30
#define TMPL
Definition: sparse_dual.hpp:278
monomial_seq< C, O > poly_t
Definition: sparse_dual.hpp:31
const C & coeff() const
Definition: monomial.hpp:128
void div(dual< C, O > &f, const C &c)
Definition: sparse_dual.hpp:205
static void div(Polynomial &r, const Polynomial &a, const Polynomial &b)
Definition: sparse_dual.hpp:333
const C & c
Definition: Interval_glue.hpp:45
dual(const dual< C, Q > &p)
Definition: sparse_dual.hpp:48
static void mul(Polynomial &r, const Polynomial &a)
Definition: sparse_dual.hpp:318
static void rem(Polynomial &r, const Polynomial &a, const Polynomial &b)
Definition: sparse_dual.hpp:341
Definition: polynomial_operators.hpp:77
double C
Definition: solver_mv_fatarcs.cpp:16
monomial_seq< C, O > base_t
Definition: sparse_dual.hpp:22
OSTREAM & print(OSTREAM &os, const dual< C, O > &P, const variables &V)
Output operator.
Definition: sparse_dual.hpp:260
C operator()(const poly_t &p)
Definition: sparse_dual.hpp:87
Monomial class.
Definition: monomial.hpp:62
static void add(Polynomial &r, const Polynomial &a, const Polynomial &b)
Definition: sparse_dual.hpp:290
Definition: array.hpp:12
Definition: variables.hpp:65
R::const_iterator const_iterator
Definition: sparse_dual.hpp:28
dual< C, O > self_t
Definition: sparse_dual.hpp:32
R::monom_t monom_t
Definition: sparse_dual.hpp:24
static void add(Polynomial &r, const Polynomial &a)
Definition: sparse_dual.hpp:286
dual(const self_t &P)
Definition: sparse_dual.hpp:41
Home