Developer documentation

tensor_eenv_fcts.hpp
Go to the documentation of this file.
1 #ifndef realroot_SBDSLV_EENV_C
2 #define realroot_SBDSLV_EENV_C
4 namespace mmx {
5 namespace tensor
6 {
7  inline void eenv::swap (eenv & e) { std::swap (data, e.data); };
8 
9  inline int eenv::nvr () const { return data[1]; };
10 
11  inline const int *eenv::szs () const { return data + 2; };
12  inline int *eenv::szs () { return cdup ().data + 2; };
13  inline int *eenv::szs_data () { return data + 2; };
14 
15  inline int eenv::sz (int v) const { return szs ()[v]; };
16  inline int eenv::sz () const { return str ()[-1]; };
17 
18  inline int *eenv::vrs () { return szs () + nvr (); };
19  inline const int *eenv::vrs () const { return szs () + nvr (); };
20 
21  inline int *eenv::str () { return vrs () + nvr () + 1; };
22  inline const int *eenv::str () const { return vrs () + nvr () + 1; };
23 
24  inline int eenv::st (int v) const { return str ()[v]; };
25 
26  inline int eenv::vr (int v) const { return vrs ()[v]; };
27 
28  inline int eenv::msz (int nvr_) { return 1 + 1 + nvr_ + nvr_ + nvr_ + 1 + 2 * nvr_; };
29  inline int eenv::msz () const { return eenv::msz (nvr ()); };
30 
31  inline bool eenv::operator< (const eenv & b) const { return sz () < b.sz (); };
32 
33  inline int *eenv::alloc (int nvr_) {
34  // return this->data = new int[msz (nvr_)];
35  // data = new int[eenv::msz (nvr_)];
36  new (data)int[eenv::msz (nvr_)];
37  return data;
38  }
39 
40  inline void eenv::construct (int nvr_, int const *szs_, int const *vrs_,
41  int *mem)
42  {
43 #ifdef EENV_DEBUG
44  std::cout << "eenv::construct begin\n";
45  std::cout <<nvr_<<" "<<mem<<" "<<msz (nvr_)<<std::endl;
46 #endif
47  data = (mem==0)?alloc(nvr_):mem;
48  data[0] = 1;
49  data[1] = nvr_;
50  if (vrs_)
51  {
52  std::pair<int,int> *tmp = new std::pair<int,int>[ nvr_ ];
53  for ( int i = 0; i < nvr_; i ++ ) {
54  tmp[i].first = vrs_[i];
55  tmp[i].second = szs_[i];
56  }
57  std::sort(tmp,tmp+nvr_);
58  for ( int i = 0; i < nvr(); i ++ ) {
59  data[2+nvr()+i] = tmp[i].first; // vrs()[i] = tmp[i].first;
60  data[2+i] = tmp[i].second; // szs()[i] = tmp[i].second;
61  };
62  cstr ();
63  }
64  else
65  if (szs_)
66  {
67  std::copy (szs_, szs_ + nvr_, data+2); //szs();
68  cstr ();
69  /*gcc-bug for (int v = 0; v < nvr (); vrs()[v] = v ++ ); */
70  for (int v = 0; v < nvr () ; data[2+nvr()+v] = v, v++ ) ;
71  };
72  };
73 
74  inline bool eenv::equal (const eenv & a, const eenv & b)
75  {
76  if (a.data == b.data) return true;
77 
78  for (int i = 1; i < 2 + 2 * a.nvr (); i++)
79  if (a.data[i] != b.data[i]) return false;
80  return true;
81  };
82 
83  inline void eenv::dealloc () { if (data && (--data[0] == 0) ) delete[] data; };
84 
85  inline eenv & eenv::copy (eenv & e, const eenv & b)
86  {
87  // std::cout<<"copy"<<std::endl;
88  e.dealloc ();
89  e.data = b.data;
90  if (e.data)
91  e.data[0]++;
92  return e;
93  };
94 
95  inline eenv & eenv::cdup ()
96  {
97  if (data[0] == 1)
98  return *this;
99  // std::cout<<"cdup"<<std::endl;
100  data[0]--;
101  int *sv = data;
102  int _sz = msz();
103  data = alloc (nvr ());
104  std::copy (sv, sv + _sz, data);
105  return *this;
106  };
107 
108  inline void eenv::new_copy (eenv & e, const eenv & b)
109  {
110  e.dealloc ();
111  unsigned sz = b.msz();
112  e.data = new int[sz];
113  for(unsigned i=0;i<sz;i++) e.data[i]=b.data[i];
114  e.data[0]=1;
115  }
116 
117  inline void eenv::cstr ()
118  {
119 #ifdef EENV_DEBUG
120  std::cout << "eenv::cstr()" << std::endl;
121 #endif
122  int i, s = 2+2*nvr()+1;//* _st(str());
123  const int *_sz(szs_data()); //data+2
124  for ( i = nvr()-2, data[s+nvr()-1] = 1; i >= -1; data[s+i]=_sz[i+1]*data[s+i+1], i-- ) ;
125 #ifdef EENV_DEBUG
126  std::cout << (*this) << std::endl;
127 #endif
128  // std::cout << "esz= " << _st[-1] << std::endl;
129  };
130 
131  inline bool eenv::hasvar (int &lv, int gv) const
132  {
133  int g, d, m;
134  const int * _vr = vrs();
135  if ((gv < _vr[0]) || (gv > _vr[(d=nvr())-1])) return false;
136  if ( gv == _vr[g = 0] ) { lv = 0; return true; };
137  if ( gv == _vr[d=nvr()-1]) { lv = nvr()-1; return true;};
138  do
139  {
140  m = (g + d) / 2;
141  if ( _vr[m] == gv ) { lv = m; return true; };
142  if ( _vr[m] < gv ) g = m; else d = m;
143  }
144  while (d - g > 1);
145  return false;
146  };
147 
148 
149  inline eenv::~eenv () { dealloc (); };
150 
151  inline eenv::eenv (int nvr_, int const *szs_, int const *vrs_, int *mem)
152  :data(new int[msz(nvr_)])
153  {
154  construct (nvr_, szs_, vrs_, mem);
155  };
156 
157  inline eenv::eenv (const eenv & e ):data(e.data) {
158  // data = e.data;
159  if ( data ) data[0]++;
160  };
161  inline eenv & eenv::operator= (const eenv & e) { return copy (*this, e); };
162  inline eenv eenv::mrgvrs (const eenv & a, const eenv & b)
163  {
164 
165  unsigned c = 0;
166  int va, vb;
167  int prv = (int) (-1);
168  int anvr = a.nvr ();
169  int bnvr = b.nvr ();
170  const int * avr = a.vrs();
171  const int * bvr = b.vrs() ;
172  //const eenv::vr_t * agvr = a.gvr ();
173  // const eenv::vr_t * bgvr = b.gvr ();
174 
175  int tmp[anvr + bnvr];
176 
177  c = va = vb = 0;
178 
179  do
180  {
181  if ( avr[va] <= bvr[vb] )
182  {
183  if ( avr[va] != prv ) tmp[c++] = prv = avr[va];
184  va++;
185  continue;
186  };
187 
188  if ( bvr[vb] < avr[va] )
189  {
190  if ( bvr[vb] != prv ) tmp[c++] = prv = bvr[vb];
191  vb++;
192  continue;
193  };
194  }
195  while (va < anvr && vb < bnvr);
196 
197  if (va < anvr) for (; va < anvr; va++)
198  if ( avr[va] != prv ) tmp[c++] = avr[va];
199 
200  if (vb < bnvr) for (; vb < bnvr; vb++)
201  if ( bvr[vb] != prv ) tmp[c++] = bvr[vb];
202 
203  eenv oenv (c);
204 
205 
206  std::copy(tmp,tmp+oenv.nvr(),oenv.vrs());
207  return oenv;
208  };
209 
210 
211  inline eenv eenv::common (const eenv & a, const eenv & b)
212  {
213  if ( a == b ) return eenv(a);
214  int v, lva, lvb;
215  eenv oenv (mrgvrs (a, b));
216 
217  const int *ovr = oenv.vrs ();
218  const int *aszs = a.szs ();
219  const int *bszs = b.szs ();
220  int *oszs = oenv.szs ();
221 
222  for (v = 0; v < oenv.nvr (); v++)
223  {
224  if (a.hasvar (lva, ovr[v]))
225  {
226  if (b.hasvar (lvb, ovr[v]))
227  {
228  oszs[v] = std::max(aszs[lva], bszs[lvb]);
229  }
230  else
231  oszs[v] = aszs[lva];
232  }
233  else
234  {
235  if (b.hasvar (lvb, ovr[v]))
236  oszs[v] = bszs[lvb];
237  else
238  std::cout << "erreur dans common\n";
239  };
240  };
241 
242  oenv.cstr ();
243  return oenv;
244  };
245 
246  inline eenv eenv::mul (const eenv & a, const eenv & b)
247  {
248 
249  int v, lva, lvb;
250  eenv oenv (mrgvrs (a, b));
251  // std::cout << " mrgvrs = " << oenv << std::endl;
252  int *oszs = oenv.szs ();
253  const int *ovrs = oenv.vrs ();
254  const int *aszs = a.szs ();
255  const int *bszs = b.szs ();
256 
257  for (v = 0; v < oenv.nvr (); v++)
258  {
259  oszs[v] = 0;
260  if (a.hasvar (lva, ovrs[v]))
261  oszs[v] += aszs[lva] - 1;
262  if (b.hasvar (lvb, ovrs[v]))
263  oszs[v] += bszs[lvb] - 1;
264  oszs[v]++;
265  };
266  oenv.cstr ();
267  return oenv;
268  };
269 
270  inline eenv eenv::diff (const eenv & b, int lv)
271  {
272  // const int * bszs = b.szs();
273  eenv oenv; eenv::new_copy(oenv,b);
274  int *oszs = oenv.szs();
275  oszs[lv] = std::max (oszs[lv] - 1, 1);
276  oenv.cstr ();
277  return oenv;
278  };
279 
280  inline bool eenv::vmap (int *vmap, const eenv & o, const eenv & i)
281  {
282  for (int v = 0; v < i.nvr (); v++)
283  {
284  int lv;
285  if (!o.hasvar (lv, i.vr (v)))
286  {
287  return false;
288  };
289  vmap[v] = lv;
290  };
291  return true;
292  };
293 
294 
295 inline bool eenv::oaddress (const eenv & oenv, unsigned *osupp,
296  const eenv & ienv, unsigned *isupp, unsigned nsp)
297  {
298  // std::cout << oenv << std::endl;
299  // std::cout << ienv << std::endl;
300  int v;
301  unsigned c;
302  int vmap_[ienv.nvr ()];
303  unsigned addi, addo;
304  if (!vmap (vmap_, oenv, ienv))
305  return false;
306  if (isupp)
307  for (c = 0; c < nsp; osupp[c] = addo, c++)
308  for (addi = isupp[c], addo = 0, v = ienv.nvr () - 1; addi;
309  addi /= ienv.sz (v), v--)
310  addo += (addi % ienv.sz (v)) * oenv.st (vmap_[v]);
311  else
312  {
313  const int *istr = ienv.str ();
314  const int *ostr = oenv.str ();
315  osupp[0] = 0;
316  for (v = 0; v < ienv.nvr (); v++)
317  for (int j = 0; j < istr[-1]; j += istr[v - 1])
318  for (int i = j + istr[v]; i < j + istr[v - 1]; i += istr[v])
319  osupp[i] = osupp[i - istr[v]] + ostr[vmap_[v]];
320  };
321  return true;
322  };
323 
324  inline
325  std::ostream & operator<< (std::ostream & out, const eenv & env)
326  {
327  out << "*eenv*\n";
328  out << "\tpos: " << env.data << "\n";
329  out << "\tref: " << env.data[0] << "\n";
330  out << "\tnvr: " << env.nvr() << "\n";
331  out << "\tesz: " << env.sz () << "\n";
332  out << "\tszs: ";
333  vct::print (env.szs (), env.nvr (), 1, out);
334  out << "\n";
335  out << "\tvrs: ";
336  vct::print (env.vrs (), env.nvr (), 1, out);
337  out << "\n";
338  out << "\tstr: ";
339  vct::print (env.str (), env.nvr (), 1, out);
340  return out;
341  };
342 
343  inline
344  bool eenv::subset (const eenv & a, const eenv & b)
345  {
346  const int *avrs = a.vrs ();
347  const int *aszs = a.szs ();
348  const int *bszs = b.szs ();
349  int lv;
350  for (int i = 0; i < a.nvr (); i++)
351  if ((!b.hasvar (lv, avrs[i])) || aszs[i] > bszs[lv])
352  return false;
353  return true;
354  };
355 
356  inline bool eenv::operator== (const eenv & e) const
357  {
358  return eenv::equal (*this, e);
359  };
360 
361  inline bool eenv::operator!= (const eenv & e) const
362  {
363  return !eenv::equal (*this, e);
364  };
365 
366  inline eenv eenv::elevation (const eenv & o, const eenv & i)
367  {
368  int nszs[o.nvr ()];
369  const int * ovrs = o.vrs();
370  const int * oszs = o.szs ();
371  const int * iszs = i.szs ();
372  int lv;
373  for (int v = 0; v < o.nvr (); v++)
374  if (i.hasvar (lv, ovrs[v]))
375  nszs[v] = oszs[v] - iszs[lv] + 1;
376  else
377  nszs[v] = oszs[v];
378  eenv tmp(o.nvr (), nszs, o.vrs ());
379  // std::cout << " tmp = " << tmp << std::endl;
380  return tmp;
381  };
382 
383 
384 // mpoldst Connection
385 
386  struct vd
387  {
388  int n, d;
389  vd ():n (-1), d (0)
390  {
391  };
392  };
393 
394  inline std::ostream & operator<< (std::ostream & o, const vd & vd_)
395  {
396  o << "vd(" << vd_.n << ", " << vd_.d << ")";
397  return o;
398  };
399 
400  template < class MONOM > void mescan (int &last, vd * vdeg, const MONOM & m)
401  {
402  for (unsigned i = 0; i < m.size (); i++)
403  {
404  if (m[i] != 0)
405  {
406  if (vdeg[i].n != -1)
407  vdeg[i].d = std::max ((int) m[i], vdeg[i].d);
408  else
409  {
410  if (last != -1)
411  {
412  vdeg[i].n = vdeg[last].n;
413  vdeg[i].d = m[i];
414  vdeg[last].n = i;
415  last = i;
416  }
417  else
418  {
419  last = i;
420  vdeg[i].n = i;
421  vdeg[i].d = m[i];
422  };
423  };
424  };
425  };
426  }
427 
428 
429  struct oulala {
430  int initial; int name;
431  bool operator<( const oulala& b ) const { return name < b.name; };
432  };
433 
434  template < class C, class O >
435  inline eenv::eenv (const sparse::monomial_seq< C, O > &mpol)
436  {
437  int nvr = nbvar (mpol);
438  vd *lenv = new vd[nvr];
439  int last = -1;
440  typename sparse::monomial_seq < C, O >::const_iterator it;
441  for (it = mpol.begin (); it != mpol.end (); mescan (last, lenv, *it++)) ;
442  if ( last == -1 )
443  {
444  nvr = 1; int szs = 1;
445  new (this) eenv(1,&szs,0);
446  return;
447  };
448  int c = last;
449  nvr = 0;
450  do
451  {
452  c = lenv[c].n;
453  nvr++;
454  }
455  while (c != last);
456  int vrs[nvr];
457  int szs[nvr];
458  c = last;
459 
460  int k = 0;
461  do
462  {
463  vrs[k] = c;
464  szs[k] = lenv[c].d + 1;
465  c = lenv[c].n;
466  lenv[vrs[k]].n = -1;
467  lenv[vrs[k]].d = 0;
468  k++;
469  }
470  while (c != last);
471  new (this) eenv (nvr, szs, vrs );
472  };
473 
474  inline bool eenv::equiv (const eenv & a, const eenv & b)
475  {
476  return equal(a,b);
477  };
478 
479 
480  inline eenv eenv::rewrite( const eenv& o, const eenv& i )
481  {
482  int vmap_[ o.nvr() ];
483  if ( ! vmap( vmap_, o, i ) ) { std::cerr << "vmap error(rewrite)\n"; exit(1); };
484  eenv tmp(o);
485  for ( int v = 0; v < o.nvr(); v ++ ) tmp.szs()[v] = 1;
486  for ( int v = 0; v < i.nvr(); v ++ ) tmp.szs()[vmap_[v]] = i.szs()[v];
487  tmp.cstr();
488  return tmp;
489  };
490 
491 }
492 
493 } //namespace mmx
494 #endif
static eenv mul(const eenv &a, const eenv &b)
Definition: tensor_eenv_fcts.hpp:246
static bool oaddress(const eenv &oenv, unsigned *osupp, const eenv &ienv, unsigned *isupp=0, unsigned nsp=0)
addresses translation from environment i to environment o
Definition: tensor_eenv_fcts.hpp:295
Definition: tensor_eenv_fcts.hpp:429
const C & b
Definition: Interval_glue.hpp:25
int d
Definition: tensor_eenv_fcts.hpp:388
bool operator==(const eenv &a) const
Definition: tensor_eenv_fcts.hpp:356
int * szs()
Definition: tensor_eenv_fcts.hpp:12
Definition: tensor_eenv_fcts.hpp:386
static bool equal(const eenv &a, const eenv &b)
Definition: tensor_eenv_fcts.hpp:74
MP swap(const MP &P, int var_i, int var_j)
Definition: sparse_monomials.hpp:988
static eenv elevation(const eenv &a, const eenv &b)
Definition: tensor_eenv_fcts.hpp:366
static bool equiv(const eenv &a, const eenv &b)
Definition: tensor_eenv_fcts.hpp:474
void construct(int nvr_, int const *szs_, int const *vrs_, int *mem)
Definition: tensor_eenv_fcts.hpp:40
eenv()
constructors
Definition: tensor_eenv.hpp:72
static bool subset(const eenv &a, const eenv &b)
Definition: tensor_eenv_fcts.hpp:344
void dealloc()
Definition: tensor_eenv_fcts.hpp:83
static eenv common(const eenv &a, const eenv &b)
Definition: tensor_eenv_fcts.hpp:211
int * vrs()
Definition: tensor_eenv_fcts.hpp:18
std::ostream & operator<<(std::ostream &o, const bernstein< C > &mpl)
Definition: tensor_bernstein_fcts.hpp:253
int * str()
Definition: tensor_eenv_fcts.hpp:21
Definition: tensor_eenv.hpp:9
void swap(eenv &e)
Definition: tensor_eenv_fcts.hpp:7
void mescan(int &last, vd *vdeg, const MONOM &m)
Definition: tensor_eenv_fcts.hpp:400
static eenv rewrite(const eenv &o, const eenv &i)
Definition: tensor_eenv_fcts.hpp:480
int vr(int lv) const
return the global id for variable lv
Definition: tensor_eenv_fcts.hpp:26
int * szs_data()
Definition: tensor_eenv_fcts.hpp:13
int nvr() const
number of variables
Definition: tensor_eenv_fcts.hpp:9
int * alloc(int nvr_)
Definition: tensor_eenv_fcts.hpp:33
TMPL void copy(Polynomial &r, const Polynomial &a)
Copy of a in r.
Definition: sparse_monomials.hpp:613
eenv & cdup()
Definition: tensor_eenv_fcts.hpp:95
vd()
Definition: tensor_eenv_fcts.hpp:389
static eenv diff(const eenv &b, int lv)
Definition: tensor_eenv_fcts.hpp:270
void cstr()
Definition: tensor_eenv_fcts.hpp:117
int sz() const
return the total number of coefficients of the dense representation
Definition: tensor_eenv_fcts.hpp:16
void print(const A *p, unsigned n, int st=1, std::ostream &o=std::cout)
Definition: tensor_vctops.hpp:206
int n
Definition: tensor_eenv_fcts.hpp:388
static bool vmap(int *vmap, const eenv &o, const eenv &i)
variable names translation from environment i to environment o
Definition: tensor_eenv_fcts.hpp:280
bool operator!=(const eenv &a) const
Definition: tensor_eenv_fcts.hpp:361
int initial
Definition: tensor_eenv_fcts.hpp:430
int name
Definition: tensor_eenv_fcts.hpp:430
static eenv & copy(eenv &e, const eenv &b)
Definition: tensor_eenv_fcts.hpp:85
static eenv mrgvrs(const eenv &a, const eenv &b)
Definition: tensor_eenv_fcts.hpp:162
int * data
Definition: tensor_eenv.hpp:37
const C & c
Definition: Interval_glue.hpp:45
eenv & operator=(const eenv &e)
Definition: tensor_eenv_fcts.hpp:161
int st(int v) const
return the stride for variable v
Definition: tensor_eenv_fcts.hpp:24
bool operator<(const eenv &b) const
Definition: tensor_eenv_fcts.hpp:31
static void new_copy(eenv &e, const eenv &b)
Definition: tensor_eenv_fcts.hpp:108
TMPL int nbvar(const Polynomial &mp)
Definition: polynomial_fcts.hpp:43
int msz() const
memory size used by the eenv representation in bytes.
Definition: tensor_eenv_fcts.hpp:29
Interval< T, r > max(const Interval< T, r > &a, const Interval< T, r > &b)
Definition: Interval_fcts.hpp:135
bool hasvar(int &lv, int gv) const
Definition: tensor_eenv_fcts.hpp:131
Definition: array.hpp:12
static int msz(int nvr_)
Definition: tensor_eenv_fcts.hpp:28
bool operator<(const oulala &b) const
Definition: tensor_eenv_fcts.hpp:431
~eenv()
Definition: tensor_eenv_fcts.hpp:149
Home