Scippy

SCIP

Solving Constraint Integer Programs

intervalarithext.h
Go to the documentation of this file.
1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2 /* */
3 /* This file is part of the program and library */
4 /* SCIP --- Solving Constraint Integer Programs */
5 /* */
6 /* Copyright (C) 2002-2017 Konrad-Zuse-Zentrum */
7 /* fuer Informationstechnik Berlin */
8 /* */
9 /* SCIP is distributed under the terms of the ZIB Academic License. */
10 /* */
11 /* You should have received a copy of the ZIB Academic License */
12 /* along with SCIP; see the file COPYING. If not email to scip@zib.de. */
13 /* */
14 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
15 
16 /**@file intervalarithext.h
17  * @brief C++ extensions to interval arithmetics for provable bounds
18  * @author Stefan Vigerske
19  */
20 
21 /*---+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+----1----+----2*/
22 
23 #ifndef __SCIP_INTERVALARITHEXT_HPP__
24 #define __SCIP_INTERVALARITHEXT_HPP__
25 
26 #include "scip/intervalarith.h"
27 #include "scip/pub_message.h"
28 
29 /* In some cases, a user may need the SCIPInterval class to be defined in some namespace.
30  * To allow this, the symbol SCIPInterval_NAMESPACE should be defined to the name of that namespace
31  * before inclusion of this header file
32  * It is in the users responsibility to implement the corresponding symbols from nlpi/intervalarith.c.
33  */
34 #ifdef SCIPInterval_NAMESPACE
35 namespace SCIPInterval_NAMESPACE {
36 #endif
37 
38 /** an interval that extends the SCIP_INTERVAL struct
39  *
40  * Used by various methods to allow calculating with intervals as with ordinary numbers.
41  */
42 class SCIPInterval : public SCIP_INTERVAL
43 {
44 public:
45  /** value to use for infinity
46  *
47  * Currently a global variable, thus use with care!
48  */
49  static SCIP_Real infinity;
50 
51  /** default constructor -> gives entire interval */
52  SCIPInterval()
53  {
55  }
56 
57  /** constructor for an SCIP_INTERVAL struct */
58  SCIPInterval(
59  const SCIP_INTERVAL& x /**< interval to copy */
60  )
61  {
62  SCIPintervalSetBounds(this, x.inf, x.sup);
63  }
64 
65  /** constructor for an interval giving pointwise bounds */
66  SCIPInterval(
67  SCIP_Real infinum, /**< lower bound of interval */
68  SCIP_Real supremum /**< upper bound of interval */
69  )
70  {
71  SCIPintervalSetBounds(this, infinum, supremum);
72  }
73 
74  /** constructor for a singleton */
75  SCIPInterval(
76  SCIP_Real number /**< number to be represented by interval */
77  )
78  {
79  SCIPintervalSet(this, number);
80  }
81 
82  /** sets interval bounds */
83  void setBounds(
84  SCIP_Real newinf, /**< lower bound of interval */
85  SCIP_Real newsup /**< upper bound of interval */
86  )
87  {
88  SCIPintervalSetBounds(this, newinf, newsup);
89  }
90 
91  /** returns whether this interval is equal to another one */
92  bool operator==(
93  const SCIP_INTERVAL& y /**< interval to compare with */
94  ) const
95  {
97  return false;
98  if( this->inf <= -SCIPInterval::infinity && y.inf > -SCIPInterval::infinity )
99  return false;
100  if( this->sup >= SCIPInterval::infinity && y.sup < SCIPInterval::infinity )
101  return false;
102  return (this->inf == y.inf) && (this->sup == y.sup);
103  }
104 
105  /** returns whether this interval is not equal to another one */
106  bool operator!=(
107  const SCIP_INTERVAL& y /**< interval to compare with */
108  ) const
109  {
110  return !operator==(y);
111  }
112 
113  /** returns whether this interval is equal to a given number */
114  bool operator==(
115  const SCIP_Real& y /**< number to compare with */
116  ) const
117  {
118  return ( (inf == y) && (sup == y) ) ||
119  ( sup <= -SCIPInterval::infinity && y <= -SCIPInterval::infinity ) ||
121  }
122 
123  /** adds another interval to this one */
124  SCIPInterval& operator+=(
125  const SCIPInterval& y /**< interval to add in */
126  )
127  {
128  SCIPintervalAdd(SCIPInterval::infinity, this, *this, y);
129  return *this;
130  }
131 
132  /** subtracts an interval from this one */
133  SCIPInterval& operator-=(
134  const SCIPInterval& y /**< interval to substract */
135  )
136  {
137  SCIPintervalSub(SCIPInterval::infinity, this, *this, y);
138  return *this;
139  }
140 
141  /** multiplies an interval with this one */
142  SCIPInterval& operator*=(
143  const SCIPInterval& y /**< interval to multiply with */
144  )
145  {
146  SCIPintervalMul(SCIPInterval::infinity, this, *this, y);
147  return *this;
148  }
149 
150  /** divides this interval by another one */
151  SCIPInterval& operator/=(
152  const SCIPInterval& y /**< interval to divide by */
153  )
154  {
155  SCIPintervalDiv(SCIPInterval::infinity, this, *this, y);
156  return *this;
157  }
158 
159  /** assigns a number to this interval */
160  SCIPInterval& operator=(
161  const SCIP_Real& y /**< new value for both interval bounds */
162  )
163  {
164  SCIPintervalSet(this, y);
165  return *this;
166  }
167 
168  /** assign an interval to this interval */
169  SCIPInterval& operator=(
170  const SCIP_INTERVAL& y /**< new value for this interval */
171  )
172  {
173  SCIPintervalSetBounds(this, y.inf, y.sup);
174  return *this;
175  }
176 
177 };
178 
179 /** addition of two intervals */
180 inline
181 SCIPInterval operator+(
182  const SCIPInterval& x, /**< first operand */
183  const SCIPInterval& y /**< second operand */
184  )
185 {
186  SCIPInterval resultant;
187 
188  SCIPintervalAdd(SCIPInterval::infinity, &resultant, x, y);
189 
190  return resultant;
191 }
192 
193 /** substraction for two intervals */
194 inline
195 SCIPInterval operator-(
196  const SCIPInterval& x, /**< first operand */
197  const SCIPInterval& y /**< second operand */
198  )
199 {
200  SCIPInterval resultant;
201 
202  SCIPintervalSub(SCIPInterval::infinity, &resultant, x, y);
203 
204  return resultant;
205 }
206 
207 /** negation of an interval */
208 inline
209 SCIPInterval operator-(
210  const SCIPInterval& y /**< operand */
211  )
212 {
213  SCIPInterval resultant;
214 
215  SCIPintervalSetBounds(&resultant, -y.sup, -y.inf);
216 
217  return resultant;
218 }
219 
220 /** multiplication of two intervals */
221 inline
222 SCIPInterval operator*(
223  const SCIPInterval& x, /**< first operand */
224  const SCIPInterval& y /**< second operand */
225  )
226 {
227  SCIPInterval resultant;
228 
229  SCIPintervalMul(SCIPInterval::infinity, &resultant, x, y);
230 
231  return resultant;
232 }
233 
234 /** division for two intervals */
235 inline
236 SCIPInterval operator/(
237  const SCIPInterval& x, /**< first operand */
238  const SCIPInterval& y /**< second operand */
239  )
240 {
241  SCIPInterval resultant;
242 
243  SCIPintervalDiv(SCIPInterval::infinity, &resultant, x, y);
244 
245  return resultant;
246 }
247 
248 /** cosine of an interval */
249 inline
250 SCIPInterval cos(
251  const SCIPInterval& x /**< operand */
252  )
253 {
254  SCIPInterval resultant;
255 
256  SCIPintervalCos(SCIPInterval::infinity, &resultant, x);
257 
258  return resultant;
259 }
260 
261 /** exponential of an interval */
262 inline
263 SCIPInterval exp(
264  const SCIPInterval& x /**< operand */
265  )
266 {
267  SCIPInterval resultant;
268 
269  SCIPintervalExp(SCIPInterval::infinity, &resultant, x);
270 
271  return resultant;
272 }
273 
274 /** natural logarithm of an interval */
275 inline
276 SCIPInterval log(
277  const SCIPInterval& x /**< operand */
278  )
279 {
280  SCIPInterval resultant;
281 
282  SCIPintervalLog(SCIPInterval::infinity, &resultant, x);
283 
284  return resultant;
285 }
286 
287 /** power of an interval to another interval */
288 inline
289 SCIPInterval pow(
290  const SCIPInterval& x, /**< first operand */
291  const SCIPInterval& y /**< second operand */
292  )
293 {
294  SCIPInterval resultant;
295 
296  SCIPintervalPower(SCIPInterval::infinity, &resultant, x, y);
297 
298  return resultant;
299 }
300 
301 /** power of an interval to a scalar */
302 inline
303 SCIPInterval pow(
304  const SCIPInterval& x, /**< first operand */
305  const SCIP_Real& y /**< exponent */
306  )
307 {
308  SCIPInterval resultant;
309 
311 
312  return resultant;
313 }
314 
315 /** signpower of an interval to a scalar */
316 inline
317 SCIPInterval signpow(
318  const SCIPInterval& x, /**< first operand */
319  const SCIP_Real p /**< exponent */
320  )
321 {
322  SCIPInterval resultant;
323 
325 
326  return resultant;
327 }
328 
329 /** sine of an interval */
330 inline
331 SCIPInterval sin(
332  const SCIPInterval& x /**< operand */
333  )
334 {
335  SCIPInterval resultant;
336 
337  SCIPintervalSin(SCIPInterval::infinity, &resultant, x);
338 
339  return resultant;
340 }
341 
342 /** square an interval */
343 inline
344 SCIPInterval square(
345  const SCIPInterval& x /**< operand */
346  )
347 {
348  SCIPInterval resultant;
349 
351 
352  return resultant;
353 }
354 
355 /** square root of an interval */
356 inline
357 SCIPInterval sqrt(
358  const SCIPInterval& x /**< operand */
359  )
360 {
361  SCIPInterval resultant;
362 
364 
365  return resultant;
366 }
367 
368 /** absolute value of an interval */
369 inline
370 SCIPInterval abs(
371  const SCIPInterval& x /**< operand */
372  )
373 {
374  SCIPInterval resultant;
375 
376  SCIPintervalAbs(SCIPInterval::infinity, &resultant, x);
377 
378  return resultant;
379 }
380 
381 /** sign of an interval */
382 inline
383 SCIPInterval sign(
384  const SCIPInterval& x /**< operand */
385  )
386 {
387  SCIPInterval resultant;
388 
390 
391  return resultant;
392 }
393 
394 /** macro for easy definition of not implemented interval functions */
395 #define SCIP_INTERVALARITH_UNDEFFUNC(function) \
396 inline \
397 SCIPInterval function( \
398  const SCIPInterval& x /**< operand */ \
399  ) \
400 { \
401  SCIPerrorMessage("Error: " #function " not implemented for intervals.\n"); \
402  return SCIPInterval(); \
403 }
404 
418 #undef SCIP_INTERVALARITH_UNDEFFUNC
419 
420 #ifdef SCIPInterval_NAMESPACE
421 } /* namespace SCIPInterval_NAMESPACE */
422 #endif
423 
424 #endif
void SCIPintervalSignPowerScalar(SCIP_Real infinity, SCIP_INTERVAL *resultant, SCIP_INTERVAL operand1, SCIP_Real operand2)
SCIPInterval square(const SCIPInterval &x)
SCIP_Bool SCIPintervalIsEmpty(SCIP_Real infinity, SCIP_INTERVAL operand)
void SCIPintervalSign(SCIP_Real infinity, SCIP_INTERVAL *resultant, SCIP_INTERVAL operand)
#define infinity
Definition: gastrans.c:71
SCIPInterval pow(const SCIPInterval &x, const SCIPInterval &y)
SCIPInterval cos(const SCIPInterval &x)
void SCIPintervalMul(SCIP_Real infinity, SCIP_INTERVAL *resultant, SCIP_INTERVAL operand1, SCIP_INTERVAL operand2)
SCIPInterval operator-(const SCIPInterval &x, const SCIPInterval &y)
SCIPInterval exp(const SCIPInterval &x)
void SCIPintervalSetBounds(SCIP_INTERVAL *resultant, SCIP_Real inf, SCIP_Real sup)
void SCIPintervalPowerScalar(SCIP_Real infinity, SCIP_INTERVAL *resultant, SCIP_INTERVAL operand1, SCIP_Real operand2)
void SCIPintervalDiv(SCIP_Real infinity, SCIP_INTERVAL *resultant, SCIP_INTERVAL operand1, SCIP_INTERVAL operand2)
SCIPInterval abs(const SCIPInterval &x)
void SCIPintervalSin(SCIP_Real infinity, SCIP_INTERVAL *resultant, SCIP_INTERVAL operand)
SCIPInterval signpow(const SCIPInterval &x, const SCIP_Real p)
SCIP_Real inf
Definition: intervalarith.h:39
#define SCIP_INTERVALARITH_UNDEFFUNC(function)
interval arithmetics for provable bounds
void SCIPintervalLog(SCIP_Real infinity, SCIP_INTERVAL *resultant, SCIP_INTERVAL operand)
SCIPInterval sqrt(const SCIPInterval &x)
SCIPInterval sign(const SCIPInterval &x)
#define SCIPInterval_NAMESPACE
SCIPInterval operator+(const SCIPInterval &x, const SCIPInterval &y)
SCIP_Real sup
Definition: intervalarith.h:40
void SCIPintervalSet(SCIP_INTERVAL *resultant, SCIP_Real value)
void SCIPintervalCos(SCIP_Real infinity, SCIP_INTERVAL *resultant, SCIP_INTERVAL operand)
SCIPInterval sin(const SCIPInterval &x)
void SCIPintervalSquareRoot(SCIP_Real infinity, SCIP_INTERVAL *resultant, SCIP_INTERVAL operand)
void SCIPintervalSquare(SCIP_Real infinity, SCIP_INTERVAL *resultant, SCIP_INTERVAL operand)
void SCIPintervalAdd(SCIP_Real infinity, SCIP_INTERVAL *resultant, SCIP_INTERVAL operand1, SCIP_INTERVAL operand2)
SCIPInterval log(const SCIPInterval &x)
static long * number
void SCIPintervalAbs(SCIP_Real infinity, SCIP_INTERVAL *resultant, SCIP_INTERVAL operand)
void SCIPintervalExp(SCIP_Real infinity, SCIP_INTERVAL *resultant, SCIP_INTERVAL operand)
public methods for message output
#define SCIP_Real
Definition: def.h:135
SCIPInterval operator*(const SCIPInterval &x, const SCIPInterval &y)
void SCIPintervalSub(SCIP_Real infinity, SCIP_INTERVAL *resultant, SCIP_INTERVAL operand1, SCIP_INTERVAL operand2)
SCIPInterval operator/(const SCIPInterval &x, const SCIPInterval &y)
void SCIPintervalPower(SCIP_Real infinity, SCIP_INTERVAL *resultant, SCIP_INTERVAL operand1, SCIP_INTERVAL operand2)