Developer documentation

system_dstack.h
Go to the documentation of this file.
1 /********************************************************************
2  * This file is part of the source code of the realroot library.
3  * Author(s): J.P. Pavone, GALAAD, INRIA
4  * $Id: dstack.h,v 1.1 2005/07/11 10:03:57 jppavone Exp $
5  ********************************************************************/
6 #ifndef realroot_SOLVE_SBDSLV_SYSTEM_DOMAIN_HPP
7 #define realroot_SOLVE_SBDSLV_SYSTEM_DOMAIN_HPP
8 /********************************************************************/
9 #include <stdlib.h>
12 #include <realroot/system_nstack.h>
13 #include <realroot/Interval.hpp>
14 //--------------------------------------------------------------------
15 namespace mmx {
16 //--------------------------------------------------------------------
17 namespace realroot
18 {
19  template< class creal_t >
20  struct dstack : memory::nstack< typename Intervals<creal_t>::rdw_t >
21  {
22  typedef unsigned sz_t;
25  void receiver( method_base * mb ) { m_error = mb; };
26  void error( const char * msg ) { m_error->error( msg ); };
27 
28  /* domaine prĂ©cedent */
29 
30  inline interval_t * previous() { return this->top()+this->nvr(); };
31  /* domaine courant */
32  inline interval_t * current () { return this->top(); };
33  inline const interval_t * current() const { return this->top(); };
34  sz_t m_nvr; /* nombre de variables */
35  sz_t nvr() const { return m_nvr; };
36  creal_t m_peps ;/* prĂ©cision Ă atteindre */
37  const creal_t& peps() const { return m_peps; };
38  creal_t m_spleps;/* seuil d'isolation */
39  creal_t m_rdprm; /* parametre de rĂ©duction */
40  const creal_t& rdscr_prm() const { return m_rdprm; };
41  sz_t m_large; /* variable correspondant au plus grand intervalle */
42  sz_t m_small; /* variable correspondant au plus petit intervalle */
43 
44  inline sz_t largest() { return m_large; };
45  inline sz_t smallest() { return m_small; };
46  inline static creal_t size( const interval_t& i ) { return i.upper()-i.lower(); };
47  inline creal_t prc() { return size(this->top()[ largest() ]); };
48 
49  /* initialisation de la pile avec le domaine de dĂ©part donnĂ©s par inits (2*nvr() valeurs) */
50  void init( creal_t * inits )
51  {
52  this->popall(), this->push();
53  if ( inits )
54  for ( sz_t v = 0; v < this->nvr(); v ++ )
55  new (this->top()+v) interval_t(inits[2*v],inits[2*v+1]);
56  else
57  std::fill( this->top(), this->top() + nvr(), interval_t((creal_t)0.0,(creal_t)1.0) );
58  };
59 
60  void reset()
61  {
62  const interval_t * curr(current());
63  m_small = m_large = nvr()-1;
64  creal_t mn(curr[nvr()-1].size());
65  creal_t mx(curr[nvr()-1].size());
66  for ( sz_t v = 0; v < nvr(); v += 2 )
67  {
68  creal_t a = curr[v].size();
69  creal_t b = curr[v+1].size();
70  if ( a > b )
71  {
72  if ( b < mn ) { m_small = v + 1; mn = b; };
73  if ( mx < a ) { m_large = v; mx = a; };
74  }
75  else
76  {
77  if ( a < mn ) { m_small = v; mn = a; };
78  if ( mx < b ) { m_large = v + 1; mx = b; };
79  };
80  };
81  };
82 
83  /* fixe la prĂ©cision que l'on souhaite atteindre et la prĂ©cision de l'isolation */
84  void set_precision( const creal_t& peps, const creal_t& spleps = 0 )
85  {
86  m_peps = peps;
87  m_spleps = std::max(std::max(peps,spleps),(creal_t)1e-6);
88  };
89 
90  dstack( sz_t _nvr_, const creal_t& peps, const creal_t& spleps ):
91  memory::nstack< interval_t >(_nvr_*2),
92  m_nvr(_nvr_)
93  {
94  assert(_nvr_);
95  m_nvr = _nvr_;
96  // m_rdprm = 0.9;//
97  m_rdprm = (nvr()-(creal_t)0.5)/(nvr());
98  /* m_rdprm = 0.99; */
99  // std::cout << m_rdprm << std::endl;
100  set_precision(peps,spleps);};
101 
102  /* garder en mĂ©moire le domaine */
103  inline void setmem() { std::copy(this->top(),this->top()+nvr(),this->top()+nvr()); };
104  inline void mpush() {
105  interval_t * prv = previous();
106  this->push();
107  std::copy(prv,prv+nvr(),this->top()+nvr());
108  };
109  inline void cpush() {
110  interval_t * prv = current();
111  this->push();
112  std::copy(prv,prv+2*nvr(),this->top());
113  };
114 
115  /* Ă©chantillonage alĂ©atoire du domaine */
116  void rndsupport( creal_t * dst, int nsmp = 1 )
117  {
118  interval_t * dm = this->top();
119  creal_t * ptr;
120  for ( unsigned v = 0; v < nvr(); v ++ )
121  {
122  creal_t a = dm[v].upper();
123  creal_t b = dm[v].lower()-a;
124  for ( ptr = dst + v; ptr < dst + nsmp * nvr(); ptr += nvr() )
125  *ptr = a + ((creal_t)rand())/((creal_t)RAND_MAX)*b;
126  };
127  };
128 
129 
130  static inline
131  creal_t _linterp_dw( const interval_t& intvl, const creal_t& t )
132  {
133  using namespace numerics::rdw;
134  return dwadd(dwmul(upsub(creal_t(1),t),intvl.lower()),
135  dwmul(t,intvl.upper())) ;
136  };
137 
138  static inline
139  creal_t _linterp_up( const interval_t& intvl, const creal_t& t )
140  {
141  using namespace numerics::rdw;
142  return upadd(upmul(dwsub(creal_t(1),t),intvl.lower()),
143  upmul(t,intvl.upper()));
144  };
145 
146  /* mise a jour domaine local (src) vers global (dst) */
147  void update( interval_t * dst, interval_t * src, interval_t * locals)
148  // ,bool debug = false )
149  {
150  numerics::rounding< creal_t > rnd( numerics::rnd_dw() );
151  for ( sz_t v = 0; v < nvr(); v ++ )
152  new(dst+v) interval_t( std::max(_linterp_dw(src[v],locals[v].lower()),
153  src[v].lower()),
154  std::min(_linterp_up(src[v],locals[v].upper()),
155  src[v].upper()) );
156  };
157 
158  creal_t rdscr( interval_t const * const , interval_t const * const locals )
159  {
160  sz_t v;
161  creal_t d0;
162  for ( v = 0, d0=0.0 ; v < nvr(); d0 += size(locals[v]), v ++ ) ;
163  d0/=nvr();
164  // std::cout << d0 << "/" << rdscr_prm() << std::endl;
165  return d0;
166  };
167 
168 
169  /* empilage des rĂ©ductions */
170  bool reduce( std::vector<interval_t> * ri, unsigned ) {
171  unsigned nvrs = nvr();
172  unsigned nsdm = 1, rs[nvrs];
173  /* le nombre de sous-domaines (nsdm) prĂ©sents dans la rĂ©duction est */
174  /* Ă©gal au produit du nombre d'intervalles trouvĂ©s pour chaque variable */
175  for ( sz_t v = 0; v < nvrs; nsdm *= rs[v], v++ )
176  {
177  if (!ri[v].size()) ri[v].push_back(interval_t(0.0,1.0));
178  rs[v] = ri[v].size();
179  };
180  /* allocation du nombre d'intervalles nĂ©cessaires reprĂ©senter les sous-domaines */
181  /* sdm[i] pointe sur sys->nvr() (nvrs) intervalles, pour i = 0..nsdm-1 */
182  unsigned s,a,v; /* sous-domaine, addressage, variable */
183  interval_t *_tmp= new interval_t[ nsdm * nvrs ];
184  interval_t** sdm = new interval_t*[ nsdm ];
185  for ( sdm[0] = _tmp, s = 1; s < nsdm; sdm[s] = sdm[s-1] + nvrs, s ++) ;
186 
187  /* traduction entre l'addresse ou numĂ©ro (a) du sous-domaine et sa dĂ©finition */
188  /* sdm[a] = [ r[0][x[0]], r[1][x[1]], r[2][x[2]], ..., r[nvrs][x[3]] ] */
189  /* a = x[0]+rs[0]*(x[1]+rs[1]*( ... */
190  for ( s = 0; s < nsdm; s ++ )
191  for ( a = s, v = 0; v < nvrs; sdm[s][v] = ri[v][a%rs[v]], a/=rs[v], v++ ) ;
192  /* reduction simple insuffisante */
193  if ( nsdm == 1 && rdscr(current(),sdm[0]) > rdscr_prm() ) return false;
194  setmem(); /* mĂ©morisation */
195  for ( s = 0; s < nsdm-1; s ++ )
196  {
197  update( this->top(), previous(), sdm[s] );
198  mpush();
199  };
200  update( this->top(), previous(), sdm[s] );
201  return true;
202  };
203 
204  bool subdivide( sz_t v = (sz_t)(-1), creal_t m = (creal_t)0 )
205  {
206  creal_t prec(prc());
207  if ( prec < m_spleps ) return false;
208  /* split(-1,m) = split(largest(),m) */
209  v = ((v==((sz_t)(-1)))?largest():v);
210  /* si la subdivision est trop proche des bornes de l'intervalle */
211  if ( m*(1.0-m) < 0.01 ) m = 0.5;
212  /* taille de l'intervalle a subdiviser trop faible */
213  creal_t d(size(current()[v]));
214  if ( d < m_spleps )
215  {
216  v = largest();
217  d = prec;
218  };
219  /* calcul de la position en absolu */
220  creal_t t(current()[v].lower() + m * d);
221  /* mĂ©morisation du domaine courant */
222  interval_t * sv = current();
223  cpush ();
224  new (sv+v) interval_t(sv[v].lower(),t);
225  new (current()+v) interval_t(t,current()[v].upper());
226 
227  return true;
228 
229  };
230 
231  };
232 }
233 //--------------------------------------------------------------------
234 } //namespace mmx
235 /********************************************************************/
236 #endif //
void popall()
Definition: system_nstack.h:54
void push()
Definition: system_nstack.h:35
T dwmul(const T &a, const T &b)
Definition: rounding_mode.hpp:96
void mpush()
Definition: system_dstack.h:104
T upadd(const T &a, const T &b)
Definition: rounding_mode.hpp:86
void fill(C *a, unsigned n, int s, const C &x)
Definition: tensor_vctops.hpp:152
const C & b
Definition: Interval_glue.hpp:25
unsigned sz_t
Definition: system_dstack.h:22
T upper(const Interval< T, r > &x)
Definition: Interval_fcts.hpp:89
void rndsupport(creal_t *dst, int nsmp=1)
Definition: system_dstack.h:116
bool reduce(std::vector< interval_t > *ri, unsigned)
Definition: system_dstack.h:170
sz_t m_large
Definition: system_dstack.h:40
Intervals< creal_t >::rdw_t * top()
Definition: system_nstack.h:34
bool subdivide(sz_t v=(sz_t)(-1), creal_t m=(creal_t) 0)
Definition: system_dstack.h:204
void init(creal_t *inits)
Definition: system_dstack.h:50
Intervals< creal_t >::rdw_t interval_t
Definition: system_dstack.h:23
const creal_t & peps() const
Definition: system_dstack.h:37
T dwadd(const T &a, const T &b)
Definition: rounding_mode.hpp:88
T upmul(const T &a, const T &b)
Definition: rounding_mode.hpp:94
dstack(sz_t _nvr_, const creal_t &peps, const creal_t &spleps)
Definition: system_dstack.h:90
void cpush()
Definition: system_dstack.h:109
Definition: system_nstack.h:18
nstack(unsigned n=1)
Definition: system_nstack.h:37
creal_t rdscr(interval_t const *const , interval_t const *const locals)
Definition: system_dstack.h:158
void receiver(method_base *mb)
Definition: system_dstack.h:25
creal_t prc()
Definition: system_dstack.h:47
Definition: rounding_mode.hpp:71
void reset()
Definition: system_dstack.h:60
method_base * m_error
Definition: system_dstack.h:24
TMPL void copy(Polynomial &r, const Polynomial &a)
Copy of a in r.
Definition: sparse_monomials.hpp:613
void setmem()
Definition: system_dstack.h:103
return T = Y * from X = Y or X = Y *
Definition: texp_strip_qualifiers.hpp:9
#define min(a, b)
Definition: parser_def.c:475
void error(const char *msg)
Definition: system_dstack.h:26
static creal_t _linterp_dw(const interval_t &intvl, const creal_t &t)
Definition: system_dstack.h:131
static creal_t _linterp_up(const interval_t &intvl, const creal_t &t)
Definition: system_dstack.h:139
Definition: Interval_fcts.hpp:604
creal_t m_spleps
Definition: system_dstack.h:37
T lower(const Interval< T, r > &x)
Definition: Interval_fcts.hpp:87
T dwsub(const T &a, const T &b)
Definition: rounding_mode.hpp:92
const creal_t & rdscr_prm() const
Definition: system_dstack.h:40
T upsub(const T &a, const T &b)
Definition: rounding_mode.hpp:90
interval_t * current()
Definition: system_dstack.h:32
void update(interval_t *dst, interval_t *src, interval_t *locals)
Definition: system_dstack.h:147
sz_t nvr() const
Definition: system_dstack.h:35
sz_t largest()
Definition: system_dstack.h:44
creal_t m_peps
Definition: system_dstack.h:35
sz_t smallest()
Definition: system_dstack.h:45
interval_t * previous()
Definition: system_dstack.h:30
sz_t m_nvr
Definition: system_dstack.h:33
Interval< T, r > max(const Interval< T, r > &a, const Interval< T, r > &b)
Definition: Interval_fcts.hpp:135
Definition: array.hpp:12
Definition: system_dstack.h:20
sz_t m_small
Definition: system_dstack.h:42
Definition: method_base.hpp:14
static creal_t size(const interval_t &i)
Definition: system_dstack.h:46
virtual void error(const char *)=0
creal_t m_rdprm
Definition: system_dstack.h:39
#define assert(expr, msg)
Definition: shared_object.hpp:57
void set_precision(const creal_t &peps, const creal_t &spleps=0)
Definition: system_dstack.h:84
Home