Developer documentation

solver_binary.hpp
Go to the documentation of this file.
1 /*******************************************************************
2  * This file is part of the source code of the realroot kernel.
3  * Author(s): J.P. Pavone GALAAD, INRIA
4  * B. Mourrain GALAAD, INRIA
5  ********************************************************************/
6 #ifndef realroot_solver_binary_hpp
7 #define realroot_solver_binary_hpp
8 /********************************************************************/
9 #include <realroot/Seq.hpp>
10 #include <realroot/binomials.hpp>
15 #include <realroot/solver.hpp>
16 #include <realroot/homography.hpp>
18 #include <assert.h>
19 #include <vector>
20 #include <algorithm>
21 //====================================================================
22 namespace mmx {
23 
24 #ifndef WITH_AS
25 #define WITH_AS
26 template<typename T,typename F>
27 struct cast_helper {
28  static inline T cv (const F& x) { return x; }
29 };
30 
31 template<typename T,typename F> inline
32 T as (const F& x) { return cast_helper<T,F>::cv (x); }
33 #endif
34 
35 // template<class C> inline void operator<<(std::vector<C>& v, const C& x) { v.push_back(x) ; }
36 
37 template<class C, int N> struct Interval;
38 // template<class C> struct with_rounding { typedef meta::false_t T;};
39 // template<class C, int N> struct with_rounding<Interval<C,N> > { typedef typename with_rounding<C>::T T;};
40 // template<> struct with_rounding<double> { typedef meta::true_t T;};
41 
42 // template<class real> struct Homography
43 // {
44 // real a,b,c,d;
45 // Homography(): a(1),b(0),c(0),d(1) {}
46 // Homography(const Homography& H): a(H.a),b(H.b),c(H.c),d(H.d) {}
47 // Homography(const real& A, const real& B, const real& C, const real&D): a(A),b(B),c(C),d(D) {}
48 // };
49 
50 //====================================================================
51 struct bound
52 {
53  unsigned e;
54  unsigned m;
55 
56  // bound() :e(0), m(0){}
57  // bound(const bound& b) :e(b.e), m(b_m.hpp){}
58  // bound & operator=(const bound& b) {e=b.e; m=b_m.hpp;return *this;}
59 
60  bool operator==( const bound& b )
61  {
62  if ( e < b.e )
63  {
64  return (m << (b.e-e)) == b.m;
65  };
66  return (b.m << (e-b.e)) == m;
67  };
68 };
69 //--------------------------------------------------------------------
70 // struct dmn_t
71 // {
72 // unsigned h;
73 // unsigned_t v;
74 // };
75 //====================================================================
76 struct res_t {
79  bool t;
80  int sv;
81 };
82 //====================================================================
83 struct data_t {
84  typedef double creal_t;
85  typedef unsigned sz_t;
86  typedef unsigned unsigned_t;
87 
88  creal_t *m_data;
90  sz_t m_limit;
91  sz_t m_s;
92  sz_t m_cup;
93  sz_t m_cdw;
94  creal_t eps, root_bound;
95  bool isole;
96  std::vector<res_t> m_res;
97 
98  data_t(): eps(1e-6) { m_data = 0; };
99  data_t(bool isl): eps(1e-6), isole(isl) { m_data = 0; };
100  data_t(const creal_t& e): eps(e) { m_data = 0; };
101  ~data_t() { Free(); };
102 
103  inline
104  void Free()
105  {
106  if ( m_data )
107  {
108  delete[] m_data;
109  delete[] m_dmn;
110  m_res.resize(0);
111  };
112  }
113 
114  inline
115  void alloc( sz_t s, sz_t cs, sz_t deep )
116  {
117  Free();
118  m_s = s;
119  m_limit = std::min((sz_t)numerics::hdwi<sz_t>::nbit,deep);
120  m_data = new creal_t [ cs*m_limit ];
121  m_dmn = new bound [ m_limit ];
122  m_res.resize(0);
123  // std::cout<<"Alloc "<<m_limit<<std::endl;
124  }
125 
126  inline const bound& bcka() const { return m_res.back().a; };
127  inline bound& bcka() { return m_res.back().a; };
128  inline const bound& bckb() const { return m_res.back().b; };
129  inline bound& bckb() { return m_res.back().b; };
130 
131  inline bool& bckt() { return m_res.back().t; };
132  inline bool bckt() const { return m_res.back().t; };
133 
134  inline int & bcks() { return m_res.back().sv; };
135  inline int bcks() const { return m_res.back().sv; };
136 
137  inline void set_back_a(const bound& a) { m_res.back().a = a;};
138  inline void set_back_b(const bound& b) { m_res.back().b = b;};
139 
140  inline void setbck( const bound& a, const bound& b, bool type )
141  {
142  bcka() = a;
143  bckb() = b;
144  bckt() = type;
145  };
146 
147  inline
148  void pshbck()
149  {
150  m_res.push_back(res_t());
151  };
152 
153  inline
154  void sstore( int d, int t = 1 )
155  {
156  // m_data + d*m_s;
157  pshbck();
158  bcka() = m_dmn[d];
159  bckb() = bcka();
160  bckb().m += t;
161  bckt() = true;
162  bcks() = 1;
163  };
164 
165  inline
166  void bstore( int d, int t = 1 )
167  {
168  // m_data + d*m_s;
169  pshbck();
170  bcka() = m_dmn[d];
171  bcka().m += t;
172  bckb() = bcka();
173  bckt() = true;
174  bcks() = 1;
175  };
176 
177  inline
178  void estore( int d, int t = 1 )
179  {
180  // m_data + d*m_s;
181  pshbck();
182  bckb() = m_dmn[d];
183  bckb().m += t;
184  bcka() = bckb();
185  bckt() = true;
186  bcks() = 1;
187  };
188 
189  inline
190  void mstore( int d , int sv)
191  {
192  sstore(d);
193  bckt() = false;
194  bcks() = sv;
195  };
196 
197  sz_t size() const { return m_res.size(); };
198  sz_t nb_sol() { return m_res.size();}
199 
200 
201  void writedomain( int d )
202  {
203  std::cout << "Domaine-" << d << std::endl;
204  std::cout << "\thauteur = " << m_dmn[d].e << std::endl;
205  std::cout << "\tvaleur = " << m_dmn[d].m << std::endl;
206  };
207 
208  void writebck()
209  {
210  std::cout << "Back " << std::endl;
211  std::cout << "\tA = " << bcka().e << " , " << bcka().m << std::endl;
212  std::cout << "\tB = " << bckb().e << " , " << bckb().m << std::endl;
213  };
214 
215  inline creal_t get_root_bound() { return root_bound;}
216 
217  template<class real> static
218  void convert( real& a, const bound& b,
219  const real & first = 0, const real & scale = 1 )
220  {
221  unsigned m(b.m);
223  a = first;
224  real x = scale;
225  for ( unsigned i = 0; i < b.e + 1; x /= 2, i ++ )
226  {
227  if ( m & 1 ) a += x;
228  m >>= 1;
229  };
230  }
231 
232  template<class real, class coeff>
233  void convert( real& a, const bound& b, const homography<coeff>& H)
234  {
235  unsigned_t m(b.m);
237  a = H.b;
238  real d = -H.d;
239  real x = H.a, y = -H.c;
240  for ( unsigned i = 0; i < b.e + 1; x /= 2, y/=2, i ++ )
241  {
242  if ( m & 1 ) {a += x; d+=y;}
243  m >>= 1;
244  };
245  // std::cout <<"\n\t d= "<<d<<" a= "<<a<< std::endl;
246  if(d==0 )
247  {
248  // std::cout <<"\n\t rb= "<<this->root_bound<< std::endl;
249  a = real(root_bound);
250  }
251  else
252  {
253  a /= real(-d);
254  //std::cout <<"\t d!=0 a= "<<a<< std::endl;
255  }
256  };
257 
258 
259 };
260 
261 //====================================================================
262 template<class K >
263 struct binary_subdivision : public K {
264 
265  typedef typename K::integer integer;
266  typedef typename K::rational rational;
267  typedef typename K::floating floating;
268  typedef typename K::ieee C;
269 
270  typedef unsigned sz_t;
271  // typedef unsigned unsigned_t;
274 
276  typedef C creal_t;
277 
278  static data_t data;
279 
280  inline
281  static void split( creal_t * r, creal_t * l, sz_t sz )
282  {
283  creal_t * er, * p;
284  er = r + (sz-1);
285  for ( er = r + (sz-1); er != r; er--, l++ )
286  {
287  *l = *r;
288  for ( p = r; p != er; p ++ )
289  *p = (*p+*(p+1))/2;
290  };
291  *l = *r;
292  };
293 
294  static sz_t sgncnt ( creal_t const * b , sz_t sz )
295  {
296  creal_t const * last = b + sz;
297  sz_t c;
298  int prv = (((*b>0)<<1)|(*b<0));
299  int crr;
300  for ( c = 0; b != last && c<2; crr = (((*b>0)<<1)|(*b<0)), c += prv != crr, prv = crr, b ++ ) ;
301  // std::cout<<"sgncnt "<< c<<std::endl;
302  return c;
303  };
304 
305  static inline void split( bound& r, bound& l )
306  {
307  r.m <<= 1;
308  l.m = r.m;
309  r.m |= 1;
310  r.e ++;
311  l.e = r.e;
312  };
313 
314  static inline
315  void print( creal_t * p, sz_t n )
316  {
317  std::cout << "[";
318  for ( sz_t i = 0; i < n-1; i ++ )
319  std::cout << p[i] << ",";
320  std::cout << p[n-1] << " ]";
321  };
322 
323  void Loop( bool isole = true )
324  {
325  // std::cout <<"Starting simple solver loop"<<std::endl;
326  creal_t * pup, * pdw;
327  int d;
328  sz_t a,s,cup;
329  unsigned_t v;
330  s = data.m_s;
331  pup = data.m_data;
332  data.m_dmn[0].m = 0;
333  data.m_dmn[0].e = 0;
334  for( a = 0, d = 0; d >= 0; d--, a -= s )
335  {
336  cup = sgncnt(pup+a,s);
337  if ( cup )
338  {
339  if ( isole && cup == 1 ) { data.sstore(d); continue; };
340  if ( data.m_dmn[d].e == data.m_limit-1 )
341  {
342  if ( cup == 1 )
343  {
344  if ( *(pup+a) != 0 )
345  {
346  if ( *(pup+a+s-1) == 0 )
347  data.sstore(d,0);
348  else
349  data.sstore(d);
350  };
351  }
352  else
353  {
354  data.mstore(d,cup);
355  //std::cout<<"s "<<cup<<" "<<d<<" "<<data.m_limit<<std::endl;
356  }
357  continue;
358  };
359  split(pup+a,pup+a+s,s);
360  split(data.m_dmn[d],data.m_dmn[d+1]);
361  d += 2;
362  a += 2*s;
363  };
364  };
365  //std::cout <<"End of simple solver loop"<<std::endl;
366  };
367 
368  //--------------------------------------------------------------------
369  template<class output>
370  void get_flags( output& o )
371  {
372  unsigned s = o.size();
373  o.resize( s + o.size() );
374  for ( sz_t i = 0; i < data.m_res.size(); o[s+i] = data.m_res[i++].t )
375  ;
376  };
377  //--------------------------------------------------------------------
378  template<class input>
379  void run_loop( const input& in, const creal_t& eps, bool isole, texp::false_t)
380  {
381  sz_t prec = numerics::bitprec(eps);
382  data.alloc(in.size(),prec);
383  std::copy(in.begin(),in.end(),data.m_data);
384  Loop(isole);
385  };
386 
387 };
388 
389 template <class K> data_t binary_subdivision<K>::data;
390 //====================================================================
391 } //namespace mmx
392 #endif //realroot_solver_binary_hpp
static void convert(real &a, const bound &b, const real &first=0, const real &scale=1)
Definition: solver_binary.hpp:218
void mstore(int d, int sv)
Definition: solver_binary.hpp:190
const C & b
Definition: Interval_glue.hpp:25
static hdw_int reverse(hdw_int a)
Definition: numerics_hdwi.hpp:54
const bound & bckb() const
Definition: solver_binary.hpp:128
int sv
Definition: solver_binary.hpp:80
unsigned e
Definition: solver_binary.hpp:53
void run_loop(const input &in, const creal_t &eps, bool isole, texp::false_t)
Definition: solver_binary.hpp:379
Definition: solver_binary.hpp:51
void scale(IntervalData< RT, Poly > &ID, const RT &a)
Definition: contfrac_intervaldata.hpp:221
void sstore(int d, int t=1)
Definition: solver_binary.hpp:154
void estore(int d, int t=1)
Definition: solver_binary.hpp:178
real a
Definition: homography.hpp:17
bigunsigned< bitquo+(sz_t)(bitrem!=0)> unsigned_t
Definition: solver_binary.hpp:275
K::integer integer
Definition: solver_binary.hpp:265
void pshbck()
Definition: solver_binary.hpp:148
int bcks() const
Definition: solver_binary.hpp:135
bool operator==(const bound &b)
Definition: solver_binary.hpp:60
bound a
Definition: solver_binary.hpp:77
static void print(creal_t *p, sz_t n)
Definition: solver_binary.hpp:315
bound & bcka()
Definition: solver_binary.hpp:127
bound * m_dmn
Definition: solver_binary.hpp:89
creal_t root_bound
Definition: solver_binary.hpp:94
void bstore(int d, int t=1)
Definition: solver_binary.hpp:166
K::rational rational
Definition: solver_binary.hpp:266
static T cv(const F &x)
Definition: solver_binary.hpp:28
C creal_t
Definition: solver_binary.hpp:276
void setbck(const bound &a, const bound &b, bool type)
Definition: solver_binary.hpp:140
void writebck()
Definition: solver_binary.hpp:208
double creal_t
Definition: solver_binary.hpp:84
sz_t m_cdw
Definition: solver_binary.hpp:93
real c
Definition: homography.hpp:17
K::floating floating
Definition: solver_binary.hpp:267
T as(const F &x)
Definition: assign.hpp:51
Definition: numerics_hdwi.hpp:20
void writedomain(int d)
Definition: solver_binary.hpp:201
TMPL void copy(Polynomial &r, const Polynomial &a)
Copy of a in r.
Definition: sparse_monomials.hpp:613
void get_flags(output &o)
Definition: solver_binary.hpp:370
bool in(const T &x, const Interval< T, r > &y)
Definition: Interval_fcts.hpp:100
creal_t eps
Definition: solver_binary.hpp:94
static const sz_t bitrem
Definition: solver_binary.hpp:273
data_t(const creal_t &e)
Definition: solver_binary.hpp:100
#define min(a, b)
Definition: parser_def.c:475
data_t()
Definition: solver_binary.hpp:98
#define Interval
Definition: Interval_glue.hpp:8
unsigned m
Definition: solver_binary.hpp:54
void set_back_a(const bound &a)
Definition: solver_binary.hpp:137
unsigned unsigned_t
Definition: solver_binary.hpp:86
void set_back_b(const bound &b)
Definition: solver_binary.hpp:138
sz_t m_limit
Definition: solver_binary.hpp:90
unsigned sz_t
Definition: solver_binary.hpp:270
sz_t m_s
Definition: solver_binary.hpp:91
bool t
Definition: solver_binary.hpp:79
Definition: homography.hpp:15
static void split(bound &r, bound &l)
Definition: solver_binary.hpp:305
~data_t()
Definition: solver_binary.hpp:101
sz_t size() const
Definition: solver_binary.hpp:197
Definition: numerics_hdwi.hpp:47
Definition: solver_binary.hpp:76
bound b
Definition: solver_binary.hpp:78
unsigned sz_t
Definition: solver_binary.hpp:85
real b
Definition: homography.hpp:17
Definition: solver_binary.hpp:83
sz_t nb_sol()
Definition: solver_binary.hpp:198
unsigned bitprec(const T &e, const T &l=T(1.0))
Definition: numerics_hdwi.hpp:29
creal_t get_root_bound()
Definition: solver_binary.hpp:215
static const sz_t bitquo
Definition: solver_binary.hpp:272
Definition: solver_binary.hpp:263
const C & c
Definition: Interval_glue.hpp:45
bool & bckt()
Definition: solver_binary.hpp:131
bool bckt() const
Definition: solver_binary.hpp:132
structure defining a negative answer
Definition: texp_bool.hpp:9
sz_t m_cup
Definition: solver_binary.hpp:92
bool isole
Definition: solver_binary.hpp:95
static data_t data
Definition: solver_binary.hpp:278
static sz_t sgncnt(creal_t const *b, sz_t sz)
Definition: solver_binary.hpp:294
Definition: scalar_bigunsigned.hpp:11
bound & bckb()
Definition: solver_binary.hpp:129
std::vector< res_t > m_res
Definition: solver_binary.hpp:96
void Free()
Definition: solver_binary.hpp:104
int & bcks()
Definition: solver_binary.hpp:134
void Loop(bool isole=true)
Definition: solver_binary.hpp:323
void convert(real &a, const bound &b, const homography< coeff > &H)
Definition: solver_binary.hpp:233
K::ieee C
Definition: solver_binary.hpp:268
void alloc(sz_t s, sz_t cs, sz_t deep)
Definition: solver_binary.hpp:115
Definition: array.hpp:12
const bound & bcka() const
Definition: solver_binary.hpp:126
real d
Definition: homography.hpp:17
creal_t * m_data
Definition: solver_binary.hpp:88
static void split(creal_t *r, creal_t *l, sz_t sz)
Definition: solver_binary.hpp:281
data_t(bool isl)
Definition: solver_binary.hpp:99
Home