Generated on Sat Apr 10 2021 00:00:00 for Gecode by doxygen 1.9.1
val.hpp
Go to the documentation of this file.
1 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2 /*
3  * Main authors:
4  * Vincent Barichard <Vincent.Barichard@univ-angers.fr>
5  *
6  * Contributing authors:
7  * Christian Schulte <schulte@gecode.org>
8  *
9  * Copyright:
10  * Christian Schulte, 2012
11  * Vincent Barichard, 2012
12  *
13  * This file is part of Gecode, the generic constraint
14  * development environment:
15  * http://www.gecode.org
16  *
17  * Permission is hereby granted, free of charge, to any person obtaining
18  * a copy of this software and associated documentation files (the
19  * "Software"), to deal in the Software without restriction, including
20  * without limitation the rights to use, copy, modify, merge, publish,
21  * distribute, sublicense, and/or sell copies of the Software, and to
22  * permit persons to whom the Software is furnished to do so, subject to
23  * the following conditions:
24  *
25  * The above copyright notice and this permission notice shall be
26  * included in all copies or substantial portions of the Software.
27  *
28  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
29  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
30  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
31  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
32  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
33  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
34  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35  *
36  */
37 
38 namespace Gecode {
39 
40  /*
41  * Floating point value: member functions
42  *
43  */
49  FloatVal::FloatVal(const FloatNum& l, const FloatNum& u) : x(l,u) {}
53  FloatVal::FloatVal(const FloatVal& v) : x(v.x) {}
54 
57  x = n; return *this;
58  }
61  x = v.x; return *this;
62  }
63 
64  forceinline void
65  FloatVal::assign(FloatNum const &l, FloatNum const &u) {
66  x.assign(l,u);
67  }
68 
70  FloatVal::min(void) const {
71  return x.lower();
72  }
74  FloatVal::max(void) const {
75  return x.upper();
76  }
78  FloatVal::size(void) const {
79  return boost::numeric::width(x);
80  }
82  FloatVal::med(void) const {
83  return boost::numeric::median(x);
84  }
85 
86  forceinline bool
87  FloatVal::tight(void) const {
88  return (boost::numeric::singleton(x) ||
89  (nextafter(x.lower(),x.upper()) == x.upper()));
90  }
91  forceinline bool
92  FloatVal::singleton(void) const {
93  return boost::numeric::singleton(x);
94  }
95  forceinline bool
97  return boost::numeric::in(n,x);
98  }
99  forceinline bool
100  FloatVal::zero_in(void) const {
101  return boost::numeric::zero_in(x);
102  }
103 
106  FloatVal h(x,y); return h;
107  }
110  FloatVal p(boost::numeric::interval_lib::pi_half<FloatValImpType>());
111  return p;
112  }
114  FloatVal::pi(void) {
115  FloatVal p(boost::numeric::interval_lib::pi<FloatValImpType>());
116  return p;
117  }
120  FloatVal p(boost::numeric::interval_lib::pi_twice<FloatValImpType>());
121  return p;
122  }
123 
126  x += n; return *this;
127  }
130  x -= n; return *this;
131  }
134  x *= n; return *this;
135  }
138  x /= n; return *this;
139  }
140 
143  x += v.x; return *this;
144  }
147  x -= v.x; return *this;
148  }
151  x *= v.x; return *this;
152  }
155  x /= v.x; return *this;
156  }
157 
158  /*
159  * Operators and functions on float values
160  *
161  */
162 
164  operator +(const FloatVal& x) {
165  return FloatVal(+x.x);
166  }
168  operator -(const FloatVal& x) {
169  FloatNum mmi = (x.min() == 0.0) ? 0.0 : -x.min();
170  FloatNum mma = (x.max() == 0.0) ? 0.0 :-x.max();
171  return FloatVal(mma,mmi);
172  }
174  operator +(const FloatVal& x, const FloatVal& y) {
175  return FloatVal(x.x+y.x);
176  }
178  operator +(const FloatVal& x, const FloatNum& y) {
179  return FloatVal(x.x+y);
180  }
182  operator +(const FloatNum& x, const FloatVal& y) {
183  return FloatVal(x+y.x);
184  }
185 
187  operator -(const FloatVal& x, const FloatVal& y) {
188  return FloatVal(x.x-y.x);
189  }
191  operator -(const FloatVal& x, const FloatNum& y) {
192  return FloatVal(x.x-y);
193  }
195  operator -(const FloatNum& x, const FloatVal& y) {
196  return FloatVal(x-y.x);
197  }
198 
200  operator *(const FloatVal& x, const FloatVal& y) {
201  return FloatVal(x.x*y.x);
202  }
204  operator *(const FloatVal& x, const FloatNum& y) {
205  return FloatVal(x.x*y);
206  }
208  operator *(const FloatNum& x, const FloatVal& y) {
209  return FloatVal(x*y.x);
210  }
211 
213  operator /(const FloatVal& x, const FloatVal& y) {
214  return FloatVal(x.x/y.x);
215  }
217  operator /(const FloatVal& x, const FloatNum& y) {
218  return FloatVal(x.x/y);
219  }
221  operator /(const FloatNum& x, const FloatVal& y) {
222  return FloatVal(x/y.x);
223  }
224 
225  inline bool
226  operator <(const FloatVal& x, const FloatVal& y) {
227  try {
228  return x.x < y.x;
229  } catch (boost::numeric::interval_lib::comparison_error&) {
230  return false;
231  }
232  }
233  inline bool
234  operator <(const FloatVal& x, const FloatNum& y) {
235  try {
236  return x.x < y;
237  } catch (boost::numeric::interval_lib::comparison_error&) {
238  return false;
239  }
240  }
241 
242  inline bool
243  operator <=(const FloatVal& x, const FloatVal& y) {
244  try {
245  return x.x <= y.x;
246  } catch (boost::numeric::interval_lib::comparison_error&) {
247  return false;
248  }
249  }
250  inline bool
251  operator <=(const FloatVal& x, const FloatNum& y) {
252  try {
253  return x.x <= y;
254  } catch (boost::numeric::interval_lib::comparison_error&) {
255  return false;
256  }
257  }
258 
259  inline bool
260  operator >(const FloatVal& x, const FloatVal& y) {
261  try {
262  return x.x > y.x;
263  } catch (boost::numeric::interval_lib::comparison_error&) {
264  return false;
265  }
266  }
267  inline bool
268  operator >(const FloatVal& x, const FloatNum& y) {
269  try {
270  return x.x > y;
271  } catch (boost::numeric::interval_lib::comparison_error&) {
272  return false;
273  }
274  }
275 
276  inline bool
277  operator >=(const FloatVal& x, const FloatVal& y) {
278  try {
279  return x.x >= y.x;
280  } catch (boost::numeric::interval_lib::comparison_error&) {
281  return false;
282  }
283  }
284  inline bool
285  operator >=(const FloatVal& x, const FloatNum& y) {
286  try {
287  return x.x >= y;
288  } catch (boost::numeric::interval_lib::comparison_error&) {
289  return false;
290  }
291  }
292 
293  inline bool
294  operator ==(const FloatVal& x, const FloatVal& y) {
295  try {
296  return x.x == y.x;
297  } catch (boost::numeric::interval_lib::comparison_error&) {
298  return false;
299  }
300  }
301  inline bool
302  operator ==(const FloatVal& x, const FloatNum& y) {
303  if (!boost::numeric::interval_lib::checking_strict<FloatNum>
304  ::is_empty(x.x.lower(), x.x.upper())) {
305  if ((x.x.lower() == y) && (x.x.upper() == y))
306  return true;
307  }
308  if (((x.x.lower() == y) &&
309  (nextafter(x.x.lower(),x.x.upper()) == x.x.upper())) ||
310  ((x.x.upper() == y) &&
311  (nextafter(x.x.upper(),x.x.lower()) == x.x.lower())))
312  return true;
313  return false;
314  }
315 
316  inline bool
317  operator !=(const FloatVal& x, const FloatVal& y) {
318  try {
319  return x.x != y.x;
320  } catch (boost::numeric::interval_lib::comparison_error&) {
321  return false;
322  }
323  }
324  inline bool
325  operator !=(const FloatVal& x, const FloatNum& y) {
326  try {
327  return x.x != y;
328  } catch (boost::numeric::interval_lib::comparison_error&) {
329  return false;
330  }
331  }
332 
334  operator <(const FloatNum& x, const FloatVal& y) {
335  return y > x;
336  }
338  operator <=(const FloatNum& x, const FloatVal& y) {
339  return y >= x;
340  }
341  forceinline bool
342  operator >(const FloatNum& x, const FloatVal& y) {
343  return y < x;
344  }
345  forceinline bool
346  operator >=(const FloatNum& x, const FloatVal& y) {
347  return y <= x;
348  }
349  forceinline bool
350  operator ==(const FloatNum& x, const FloatVal& y) {
351  return y == x;
352  }
353  forceinline bool
354  operator !=(const FloatNum& x, const FloatVal& y) {
355  return y != x;
356  }
357 
358  template<class Char, class Traits>
359  std::basic_ostream<Char,Traits>&
360  operator <<(std::basic_ostream<Char,Traits>& os, const FloatVal& x) {
361  return os << '[' << x.min() << ".." << x.max() << ']';
362  }
363 
364  forceinline FloatVal
365  abs(const FloatVal& x) {
366  return FloatVal(abs(x.x));
367  }
369  sqrt(const FloatVal& x) {
370  return FloatVal(sqrt(x.x));
371  }
373  sqr(const FloatVal& x) {
374  return FloatVal(square(x.x));
375  }
377  pow(const FloatVal& x, int n) {
378  return FloatVal(pow(x.x,n));
379  }
381  nroot(const FloatVal& x, int n) {
382  return FloatVal(nth_root(x.x,n));
383  }
384 
386  max(const FloatVal& x, const FloatVal& y) {
387  return FloatVal(max(x.x,y.x));
388  }
390  max(const FloatVal& x, const FloatNum& y) {
391  return FloatVal(max(x.x,y));
392  }
394  max(const FloatNum& x, const FloatVal& y) {
395  return FloatVal(max(x,y.x));
396  }
398  min(const FloatVal& x, const FloatVal& y) {
399  return FloatVal(min(x.x,y.x));
400  }
402  min(const FloatVal& x, const FloatNum& y) {
403  return FloatVal(min(x.x,y));
404  }
406  min(const FloatNum& x, const FloatVal& y) {
407  return FloatVal(min(x,y.x));
408  }
409 
410 #ifdef GECODE_HAS_MPFR
411 
413  exp(const FloatVal& x) {
414  return FloatVal(exp(x.x));
415  }
417  log(const FloatVal& x) {
418  return FloatVal(log(x.x));
419  }
420 
422  fmod(const FloatVal& x, const FloatVal& y) {
423  return FloatVal(fmod(x.x,y.x));
424  }
426  fmod(const FloatVal& x, const FloatNum& y) {
427  return FloatVal(fmod(x.x,y));
428  }
430  fmod(const FloatNum& x, const FloatVal& y) {
431  return FloatVal(fmod(x,y.x));
432  }
433 
435  sin(const FloatVal& x) {
436  return FloatVal(sin(x.x));
437  }
439  cos(const FloatVal& x) {
440  return FloatVal(cos(x.x));
441  }
443  tan(const FloatVal& x) {
444  return FloatVal(tan(x.x));
445  }
447  asin(const FloatVal& x) {
448  return FloatVal(asin(x.x));
449  }
451  acos(const FloatVal& x) {
452  return FloatVal(acos(x.x));
453  }
455  atan(const FloatVal& x) {
456  return FloatVal(atan(x.x));
457  }
458 
460  sinh(const FloatVal& x) {
461  return FloatVal(sinh(x.x));
462  }
464  cosh(const FloatVal& x) {
465  return FloatVal(cosh(x.x));
466  }
468  tanh(const FloatVal& x) {
469  return FloatVal(tanh(x.x));
470  }
472  asinh(const FloatVal& x) {
473  return FloatVal(asinh(x.x));
474  }
476  acosh(const FloatVal& x) {
477  return FloatVal(acosh(x.x));
478  }
480  atanh(const FloatVal& x) {
481  return FloatVal(atanh(x.x));
482  }
483 
484 #endif
485 }
486 
487 namespace Gecode { namespace Float {
488 
489  forceinline bool
490  subset(const FloatVal& x, const FloatVal& y) {
491  return subset(x.x,y.x);
492  }
493  forceinline bool
494  proper_subset(const FloatVal& x, const FloatVal& y) {
495  return proper_subset(x.x,y.x);
496  }
497  forceinline bool
498  overlap(const FloatVal& x, const FloatVal& y) {
499  return overlap(x.x,y.x);
500  }
501 
503  intersect(const FloatVal& x, const FloatVal& y) {
504  return FloatVal(intersect(x.x,y.x));
505  }
507  hull(const FloatVal& x, const FloatVal& y) {
508  return FloatVal(hull(x.x,y.x));
509  }
511  hull(const FloatVal& x, const FloatNum& y) {
512  return FloatVal(hull(x.x,y));
513  }
515  hull(const FloatNum& x, const FloatVal& y) {
516  return FloatVal(hull(x,y.x));
517  }
519  hull(const FloatNum& x, const FloatNum& y) {
520  return FloatVal(x,y);
521  }
522 
523 }}
524 
525 // STATISTICS: float-var
526 
NNF * l
Left subtree.
Definition: bool-expr.cpp:240
int p
Number of positive literals for node type.
Definition: bool-expr.cpp:232
union Gecode::@602::NNF::@65 u
Union depending on nodetype t.
int n
Number of negative literals for node type.
Definition: bool-expr.cpp:234
Node * x
Pointer to corresponding Boolean expression node.
Definition: bool-expr.cpp:249
Float value type.
Definition: float.hh:334
bool singleton(void) const
Test whether float is a singleton.
Definition: val.hpp:92
FloatVal & operator/=(const FloatNum &n)
Divide by n.
Definition: val.hpp:137
static FloatVal pi(void)
Return lower bound of .
Definition: val.hpp:114
boost::numeric::interval< FloatNum, boost::numeric::interval_lib::policies< R, P > > FloatValImpType
Implementation type for float value.
Definition: float.hh:423
static FloatVal hull(FloatNum x, FloatNum y)
Return hull of x and y.
Definition: val.hpp:105
static FloatVal pi_twice(void)
Return .
Definition: val.hpp:119
FloatValImpType x
Implementation of float value.
Definition: float.hh:425
bool zero_in(void) const
Test whether zero is included.
Definition: val.hpp:100
FloatVal & operator-=(const FloatNum &n)
Subtract by n.
Definition: val.hpp:129
bool in(FloatNum n) const
Test whether n is included.
Definition: val.hpp:96
FloatVal & operator*=(const FloatNum &n)
Multiply by n.
Definition: val.hpp:133
FloatNum max(void) const
Return upper bound.
Definition: val.hpp:74
void assign(FloatNum const &l, FloatNum const &u)
Assign lower bound l and upper bound u.
Definition: val.hpp:65
FloatVal & operator+=(const FloatNum &n)
Increment by n.
Definition: val.hpp:125
FloatNum med(void) const
Return median of float value.
Definition: val.hpp:82
FloatVal & operator=(const FloatNum &n)
Assignment operator.
Definition: val.hpp:56
bool tight(void) const
Test whether float is tight.
Definition: val.hpp:87
FloatNum size(void) const
Return size of float value (distance between maximum and minimum)
Definition: val.hpp:78
static FloatVal pi_half(void)
Return .
Definition: val.hpp:109
FloatVal(void)
Default constructor.
Definition: val.hpp:45
FloatNum min(void) const
Return lower bound.
Definition: val.hpp:70
VarImp * x
Pointer to variable implementation.
Definition: var.hpp:50
FloatVal asinh(const FloatVal &x)
Definition: val.hpp:472
FloatVal fmod(const FloatVal &x, const FloatVal &y)
Definition: val.hpp:422
FloatVal sinh(const FloatVal &x)
Definition: val.hpp:460
FloatVal acosh(const FloatVal &x)
Definition: val.hpp:476
FloatVal tanh(const FloatVal &x)
Definition: val.hpp:468
FloatVal atanh(const FloatVal &x)
Definition: val.hpp:480
FloatVal cosh(const FloatVal &x)
Definition: val.hpp:464
void sqr(Home home, FloatVar x0, FloatVar x1)
Post propagator for .
Definition: arithmetic.cpp:95
void abs(Home home, FloatVar x0, FloatVar x1)
Post propagator for .
Definition: arithmetic.cpp:41
void sqrt(Home home, FloatVar x0, FloatVar x1)
Post propagator for .
Definition: arithmetic.cpp:102
void pow(Home home, FloatVar x0, int n, FloatVar x1)
Post propagator for for $n\geq 0$.
Definition: arithmetic.cpp:109
void nroot(Home home, FloatVar x0, int n, FloatVar x1)
Post propagator for for $n\geq 0$.
Definition: arithmetic.cpp:118
void log(Home home, FloatVar x0, FloatVar x1)
Post propagator for .
void exp(Home home, FloatVar x0, FloatVar x1)
Post propagator for .
void sin(Home home, FloatVar x0, FloatVar x1)
Post propagator for .
void cos(Home home, FloatVar x0, FloatVar x1)
Post propagator for .
void acos(Home home, FloatVar x0, FloatVar x1)
Post propagator for .
void atan(Home home, FloatVar x0, FloatVar x1)
Post propagator for .
void asin(Home home, FloatVar x0, FloatVar x1)
Post propagator for .
void tan(Home home, FloatVar x0, FloatVar x1)
Post propagator for .
Post propagator for SetVar SetOpType SetVar y
Definition: set.hh:767
double FloatNum
Floating point number base type.
Definition: float.hh:106
bool subset(const FloatVal &x, const FloatVal &y)
Definition: val.hpp:490
bool proper_subset(const FloatVal &x, const FloatVal &y)
Definition: val.hpp:494
FloatVal hull(const FloatVal &x, const FloatVal &y)
Definition: val.hpp:507
bool overlap(const FloatVal &x, const FloatVal &y)
Definition: val.hpp:498
FloatVal intersect(const FloatVal &x, const FloatVal &y)
Definition: val.hpp:503
Gecode::IntArgs i({1, 2, 3, 4})
const int v[7]
Definition: distinct.cpp:259
#define forceinline
Definition: config.hpp:192