Developer documentation

solver_mv_monomial.hpp
Go to the documentation of this file.
1 /********************************************************************
2  * Author(s): A. Mantzaflaris, GALAAD, INRIA
3  ********************************************************************/
4 #ifndef _realroot_SOLVE_MV_CONFRAC_H
5 #define _realroot_SOLVE_MV_CONFRAC_H
6 /********************************************************************/
12 # include <realroot/Interval.hpp>
14 #include <ctime>
15 
16 //# include <realroot/IEEE754.hpp>
17 //# include <realroot/polynom.hpp>
18 //# include <realroot/ring_tens.hpp>
19 
22 
23 # include <realroot/homography.hpp>
26 
27 # include <stack>
28 //# include <realroot/solver.hpp>
29 
30 //====================================================================
31 # define TMPL template<class POL>
32 # define TMPLF template<class FT, class POL>
33 # define SOLVER solver<C,MCFisolate >
34 
35 # define BOX box_rep<POL>
36 # define N_ITER 50000
37 
38 
39 # define ALLBOXES 0
40 # define VERBOSE 0
41 
42 # define DPOL polynomial<double,with<MonomialTensor> >
43 
45 
46 # define C_INCLUDE include1(b,J) // Miranda test. (degens?)
47 //# define C_INCLUDE include3(b,J,S0) // Rump's Test
49 
50 //# define C_EXCLUDE exclude1(b,S0) // Interval arithmetic
52 # define C_EXCLUDE exclude3(b) //Sign Inspection
53 //# define C_EXCLUDE exclude1(b,S0) || exclude3(b) //both
54 
55 
56 //====================================================================
57 namespace mmx {
58 namespace realroot
59 {
60 
61 //====================================================================
62 // ---------------------- Solver class ------------------------------
63 //====================================================================
64 
65 TMPLF
67 {
68  typedef typename POL::scalar_t C;
69  typedef std::stack< BOX * > STACK;
70 
71  double eps;// precision
72  //DPOL * jac;// jacobian matrix with double coefficients
73  DPOL * J;// jacobian matrix with double coefficients
74  Seq<POL *> S0;// initial polynomials
75 public:
76 
78  solver_mv_monomial(double e=1e-7)
79  {
80  J=NULL;
81  eps=e;
82  };
83 
86  {
87  unsigned c=0,cand=0, i, it=0, subs=0, ver=0;
88  BOX * b = NULL;
89  BOX * r = NULL;
90  Seq<BOX> sol;
91  Seq<BOX> psol;
92  bool red, out;
93  STACK boxes;
94 
95  FT v(0), bx;
96  bx= sys.template volume <FT>();
97  if (VERBOSE) sys.print();
98 
99  boxes.push( new BOX(sys) );
100 
101  while ( !boxes.empty() )
102  {
103  it++;
104  b = boxes.top() ;
105  boxes.pop();
106 
107  /*reduce the domain */
108  out= false;
109  red= true;
110  while ( !out && red )
111  {
112  if ( C_EXCLUDE )
113  {
114  out=true;
115  if (VERBOSE) {
116  //std::cout<<"- Excluded:"<<std::endl;
117  //b->print();
118  }
119  if (ALLBOXES) //FOR AXEL
120  {
121  v+= b->template volume<FT>();
122  r = new BOX( *b ) ;
123  sol << (*r);
124  free(r);
125  }
126  c++;
127  }
128  else
129  {
130  red= b->reduce_domain();
131  if (VERBOSE && red) {
132  //std::cout<<"- Reduced:"<<std::endl;
133  //b->print();
134  }
135  //red= false;
136  }
137  }
138 
139  if ( out )
140  {
141  free(b);
142  continue;
143  }
144  /*check for root */
145  if ( C_INCLUDE )
146  {
147  if (VERBOSE) {
148  std::cout<<"- Solution found:"<<std::endl;
149  b->print();
150  }
151 
152  ver++ ;
153  sol << (*b);
154  free(b);
155  continue;
156  }
157 
158  /*Subdivide*/
159  if ( it > N_ITER )
160  {
161  cand++;
162  std::cout<<"MAX iters"<<" ("<<N_ITER<<") "
163  <<"reached!" << std::endl;
164  b->print();
165  sol << (*b);
166  free(b);
167  }
168  else
169  {
170  if ( b->template width<double>(i) > eps )
171  {
172  subs++;
173 
174  if (check_root( b->subdiv_point(i),eps) )
175  {
176  psol <<BOX( *b, i);
177  //free(b);
178  //continue;
179  //sol[sol.size()-1].print();
180  ver++;
181  }
182 
183  b->subdivide (i,boxes, b->middle(i) );
184  //b->subdivide (i,boxes);
185  //b->subdivide (i,boxes, 1 );
186  //b->subdivide (boxes);
187  free(b);
188  }
189  else
190  {
191  if ( C_EXCLUDE ){
192  if (ALLBOXES) sol << (*b); //FOR AXEL
193  }else
194  {
195  cand++;
196  sol << (*b);
197  if (VERBOSE) {
198  std::cout<<"- EPS reached:"<<std::endl;
199  //b->print();
200  }
201  }
202  free(b);
203  }
204  }
205  }/*while*/
206 
207  unsigned j=0;
208  //if (0)
209  if ( !ALLBOXES && S0.size()==2)
210  while (j<sol.size())
211  {
212  if ( exclude2( &sol[j],J) )
213  {
214  sol.erase(j);
215  cand--;
216  }
217  else
218  { //std::cout <<"td="<<sol[j].td<<std::endl;
219  ++j;
220  }
221  }
222 
223  if (VERBOSE) {
224  std::cout<< "Iterations= "<< it <<std::endl;
225  std::cout<< "Excluded = "<< c <<std::endl;
226  std::cout<< "Verified = "<< ver <<std::endl;
227  std::cout<< "Subdivs = "<< subs <<std::endl;
228  if (ALLBOXES)
229  std::cout<< "Reduced volume= "<<
230  as<double>( 100*(bx-v)/bx )<< "\%" <<std::endl;
231  std::cout<< "#stack= "<< cand <<std::endl;
232  }
233  sol<< psol;
234 
235  return sol;
236  }
237 
239  {
240  homography_mv<C> h(d);
241  BOX sys= BOX(p,h);
242 
243  /*Global data*/
244  free(J);
245  for (unsigned i=0;i<S0.size();++i)
246  free(S0[i]);
247  S0=Seq<POL *>();
248  for (unsigned i=0;i<sys.nbpol();++i)
249  S0 << new POL( sys.system(i) );
250  J= jacobian(S0);
251  /*end Global data*/
252 
253  Seq< BOX> s(solve_system(sys) );
254 
256 
257  for (unsigned i=0; i<s.size(); ++i)
258  a << s[i].template domain<FT>().rep();
259 
260 // free(J);
261 // for (unsigned i=0;i<S0.size();++i)
262 // free(S0[i]);
263  return a;
264  }
265 
267  {
268  BOX * sys= new BOX(p, dom.size() );
269 
270  /*Global data*/
271  free(J);
272  for (unsigned i=0;i<S0.size();++i)
273  free(S0[i]);
274  S0=Seq<POL *>();
275  for (unsigned i=0;i<sys->nbpol();++i)
276  S0 << new POL( sys->system(i) );
277  J= jacobian(S0);
278  /*end Global data*/
279 
280  sys->restrict(dom);
281  Seq< BOX> s(solve_system( *sys) );
282 
284 
285  for (unsigned i=0; i<s.size(); ++i)
286  a << s[i].template domain<FT>().rep();
287 
288 // free(J);
289 // for (unsigned i=0;i<S0.size();++i)
290 // free(S0[i]);
291  return a;
292  }
293 
294 
296  {
297  homography_mv<C> h(d);
298  BOX sys= BOX(p,h);
299 
300  /*Global data*/
301  free(J);
302  for (unsigned i=0;i<S0.size();++i)
303  free(S0[i]);
304  S0=Seq<POL *>();
305  for (unsigned i=0;i<sys->nbpol();++i)
306  S0 << new POL( sys->system(i) );
307  J= jacobian(S0);
308  /*end Global data*/
309 
310  Seq<FT> t;
311  Seq< BOX> s(solve_system(sys) );
312 
314 
315  // ... inf ?
316 
317 // free(J);
318 // for (unsigned i=0;i<S0.size();++i)
319 // free(S0[i]);
320  return a;
321  }
322 
324  {
325  BOX * sys= new BOX(p, dom.size() );
326 
327  /*Global data*/
328  // free(J); //crash
329  for (unsigned i=0;i<S0.size();++i)
330  free(S0[i]);
331  S0=Seq<POL *>();
332  for (unsigned i=0;i<sys->nbpol();++i)
333  S0 << new POL( sys->system(i) );
334 
335 
336 
337 
338 
339  J= jacobian(S0);
340  /*end Global data*/
341 
342  sys->restrict(dom);
343  unsigned v; //, dim(dom.size() );
344  Seq<FT> d;
345  BOX * l, * b;
346  Seq<C> t;
347  //FT ev;
348  Seq<BOX> s(solve_system(*sys) );
349 
351 
352  for (unsigned i=0;i<s.size(); ++i )
353  {
354  b= new BOX(s[i]);
355 
356  while ( b->template width<FT>(v) > eps )
357  {
358  t= b->middle();
359  if ( b->is_root(t) )
360  {
361  d= b->template point<FT>(t);
362  a << d.rep();
363  }
364 
365  l= new BOX( *b ) ;
366  l->shift_box( t[v] , v );
367 
368  if ( C_INCLUDE )
369  {
370  free(b);
371  b=l;
372  continue;
373  }
374  else
375  {
376  free(l);
377  b->contract_box(t[i],v);
378  b->reverse_and_shift_box(v);
379  b->reverse_box(v);
380  }
381  }
382  d= b->template domain<FT>();
383  a << d.rep();
384  free(b);
385  }
386  return a;
387  }
388 
389 
390  bool check_root(const Seq<double> t, const double eps)
391  {
392  DPOL p(0);
393  double ev;
394  for (unsigned j=0; j!=S0.size(); ++j)
395  {
396 
397  let::assign(p, *S0[j]);
398  tensor::eval( ev , p.rep() ,
399  t , t.size() );
400 
401  //std::cout<<"check: "<< ev<<std::endl;
402  if ( abs(ev) > 1e-10 )
403  return false;
404  }
405  std::cout<<"Root on split: "<< t <<std::endl;
406  return true;
407 
408  };
409 
410 
411 };// solver_mv_monomial
412 
413 } //namespace realroot
414 
415 //====================================================================
416 // --------------------------- INTERFACE -----------------------------
417 //====================================================================
418 
419  struct MCFisolate{};
420  struct MCFapproximate{};
421 
422 //--------------------------------------------------------------------
423 
424  template<class C>
425  struct solver<C, MCFisolate > {
426 
427  typedef C base_t;
428 
429  template<class FT, class POL>
431  solve( Seq<POL>& p, Seq< Interval<base_t> > & dom);
432 
433  template<class FT, class POL>
435  solve( Seq<POL>& p);
436 };
437 
441 template<class C>
442 template<class FT, class POL>
445 {
446 
448  return slv.isolate(p, dom);
449 }
450 
454 template<class C>
455 template<class FT,class POL>
456 Seq<std::vector<Interval<FT> > >
457 SOLVER::solve(Seq<POL>& p) {
458 
459  unsigned d(p[0].nbvar() );
460  realroot::solver_mv_monomial<FT,POL> slv(1e-3);
461  return slv.isolate(p,d);
462 }
463 
464 
465 //--------------------------------------------------------------------
466 template<class C>
468 
469  typedef C base_t;
470  template<class FT, class POL> static Seq< std::vector<FT> >
471  solve( Seq<POL>& p, Seq<Interval<base_t> > & dom);
472 
473  template<class FT, class POL> static Seq< std::vector<FT> >
474  solve( Seq<POL>& p);
475 };
476 
481 template<class Ring>
482 template<class FT, class POL>
485 {
487  return slv.approximate(p, dom );
488 }
489 
493 template<class C>
494 template<class FT, class POL>
497 {
498 
499  unsigned d( p[0].nbvar() );
501 
502  return slv.approximate(p,d);
503 }
504 
505 
506 } //namespace mmx
507 //====================================================================
508 # undef TMPL
509 # undef SOLVER
510 # undef DPOL
511 //====================================================================
512 #endif
const C & b
Definition: Interval_glue.hpp:25
#define VERBOSE
Definition: solver_mv_monomial.hpp:40
#define BOX
Definition: solver_mv_monomial.hpp:35
static Solutions solve(const POL &p)
Definition: solver.hpp:75
#define DPOL
Definition: solver_mv_monomial.hpp:42
bool check_root(const Seq< double > t, const double eps)
Definition: solver_mv_monomial.hpp:390
MP subs(unsigned var, const X &val, const MP &P)
Definition: sparse_monomials.hpp:906
self_t & erase(size_type i)
Definition: Seq.hpp:112
Definition: solver_mv_monomial.hpp:419
C base_t
Definition: solver_mv_monomial.hpp:427
#define C_EXCLUDE
Definition: solver_mv_monomial.hpp:52
Definition: fatarcs.hpp:23
solver_mv_monomial(double e=1e-7)
Constructor.
Definition: solver_mv_monomial.hpp:78
#define ALLBOXES
Definition: solver_mv_monomial.hpp:39
Seq< typename ContFrac< NT, LB >::root_t > solve(const typename ContFrac< NT >::Poly &f, ContFrac< NT, LB >)
Definition: contfrac.hpp:164
TMPL DPOL * jacobian(Seq< POL * > &S0)
Definition: solver_mv_monomial_tests.hpp:49
void eval(Result &result, const bernstein< Coeff > &controls, const Parameters &parameters)
Definition: tensor_bernstein_fcts.hpp:195
size_type size() const
Definition: Seq.hpp:166
Seq< BOX > solve_system(BOX &sys)
Solve routine.
Definition: solver_mv_monomial.hpp:85
#define C_INCLUDE
Definition: solver_mv_monomial.hpp:46
Definition: solver.hpp:68
void abs(Interval< C, r > &x, const Interval< C, r > &a)
Definition: Interval_fcts.hpp:185
TMPL POL
Definition: polynomial_dual.hpp:74
Seq< std::vector< Interval< FT > > > isolate(Seq< POL > &p, Seq< Interval< C > > &dom)
Definition: solver_mv_monomial.hpp:266
R & rep()
Definition: Seq.hpp:62
Generic class for intervals.
Definition: Interval.hpp:44
Seq< std::vector< FT > > approximate(Seq< POL > &p, unsigned &d)
Definition: solver_mv_monomial.hpp:295
const C & c
Definition: Interval_glue.hpp:45
C base_t
Definition: solver_mv_monomial.hpp:469
double C
Definition: solver_mv_fatarcs.cpp:16
#define TMPLF
Definition: solver_mv_monomial.hpp:32
#define N_ITER
Definition: solver_mv_monomial.hpp:36
TMPL int nbvar(const Polynomial &mp)
Definition: polynomial_fcts.hpp:43
void assign(A &a, const B &b)
Generic definition of the assignement function.
Definition: assign.hpp:97
Definition: solver_mv_monomial.hpp:66
Seq< std::vector< FT > > approximate(Seq< POL > &p, Seq< Interval< C > > &dom)
Definition: solver_mv_monomial.hpp:323
Definition: array.hpp:12
Definition: solver_mv_monomial.hpp:420
Seq< std::vector< Interval< FT > > > isolate(Seq< POL > &p, unsigned &d)
Definition: solver_mv_monomial.hpp:238
TMPL bool exclude2(BOX *b, DPOL *J)
Exclusion criteria (topological degree)
Definition: solver_mv_monomial_tests.hpp:380
Home