Scippy

SoPlex

Sequential object-oriented simPlex

spxdefines.h
Go to the documentation of this file.
1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2 /* */
3 /* This file is part of the class library */
4 /* SoPlex --- the Sequential object-oriented simPlex. */
5 /* */
6 /* Copyright (C) 1996-2019 Konrad-Zuse-Zentrum */
7 /* fuer Informationstechnik Berlin */
8 /* */
9 /* SoPlex is distributed under the terms of the ZIB Academic Licence. */
10 /* */
11 /* You should have received a copy of the ZIB Academic License */
12 /* along with SoPlex; see the file COPYING. If not email to soplex@zib.de. */
13 /* */
14 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
15 
16 /**@file spxdefines.h
17  * @brief Debugging, floating point type and parameter definitions.
18  *
19  * In optimized code with \c NDEBUG defined, only
20  * \ref soplex::SPxOut::INFO1 "INFO1",
21  * \ref soplex::SPxOut::INFO2 "INFO2", and
22  * \ref soplex::SPxOut::INFO3 "INFO3" are set.
23  * If \c NDEBUG is not defined, the code within \#TRACE is used.
24  * If \c SOPLEX_DEBUG is defined, the code within
25  * \ref soplex::SPxOut::DEBUG "DEBUG" is also used.
26  *
27  * If \c WITH_LONG_DOUBLE is defined, all Real numbers are of type
28  * long double instead of just double.
29  */
30 #ifndef _SPXDEFINES_H_
31 #define _SPXDEFINES_H_
32 
33 #include <math.h>
34 #ifdef _MSC_VER
35 #include <float.h>
36 #endif
37 
38 #include <assert.h>
39 #include <stdarg.h>
40 #include <stdio.h>
41 #include <iostream>
42 
43 namespace soplex
44 {
45 #define SOPLEX_VERSION 401
46 #define SOPLEX_SUBVERSION 0
47 #define SOPLEX_APIVERSION 6
48 #define SOPLEX_COPYRIGHT "Copyright (c) 1996-2019 Konrad-Zuse-Zentrum fuer Informationstechnik Berlin (ZIB)"
49 
50 /*-----------------------------------------------------------------------------
51  * Assertion Macros etc.
52  *-----------------------------------------------------------------------------
53  */
54 
55 /**
56  \brief Macro to turn some assertions into warnings.
57 
58  If both \c NDEBUG and \c WITH_WARNINGS are defined then the failed
59  assertion is converted to a warning. In all other cases this macro is
60  equivalent to assert().
61 
62  @param prefix Short string for grepping in source code.
63  @param expr Expression that must be satisfied.
64 */
65 #if defined (NDEBUG) && defined (WITH_WARNINGS)
66 #define ASSERT_WARN( prefix, expr ) \
67  if ( !( expr ) ) \
68  { \
69  std::cerr \
70  << prefix \
71  << " failed assertion on line " << __LINE__ \
72  << " in file " << __FILE__ << ": " \
73  << #expr \
74  << std::endl; \
75  }
76 #else // just a normal assert
77 #define ASSERT_WARN( prefix, expr ) ( assert( expr ) )
78 #endif
79 
80 
81 
82 /*-----------------------------------------------------------------------------
83  * Debugging Macros etc.
84  *-----------------------------------------------------------------------------
85  */
86 
87 /**
88  Prints/Executes \p stream with verbosity level \p verbosity, resetting
89  the old verbosity level afterwards.
90  Usually the parameter \p stream prints something out.
91  This is an internal define used by MSG_ERROR, MSG_WARNING, etc.
92 */
93 #ifdef DISABLE_VERBOSITY
94 #define DO_WITH_TMP_VERBOSITY( verbosity, spxout, do_something ) {}
95 #define DO_WITH_ERR_VERBOSITY( do_something ) {}
96 #else
97 #define DO_WITH_TMP_VERBOSITY( verbosity, spxout, do_something ) \
98  { \
99  if( &spxout != NULL ) \
100  { \
101  if( verbosity <= spxout.getVerbosity() ) \
102  { \
103  const SPxOut::Verbosity old_verbosity = spxout.getVerbosity(); \
104  spxout.setVerbosity( verbosity ); \
105  do_something; \
106  spxout.setVerbosity( old_verbosity ); \
107  } \
108  } \
109  }
110 #define DO_WITH_ERR_VERBOSITY( do_something ) { do_something; }
111 #endif
112 
113 /// Prints out message \p x if the verbosity level is at least SPxOut::ERROR.
114 #define MSG_ERROR(x) { DO_WITH_ERR_VERBOSITY( x ) }
115 /// Prints out message \p x if the verbosity level is at least SPxOut::WARNING.
116 #define MSG_WARNING(spxout, x) { DO_WITH_TMP_VERBOSITY( SPxOut::WARNING, spxout, x ) }
117 /// Prints out message \p x if the verbosity level is at least SPxOut::INFO1.
118 #define MSG_INFO1(spxout, x) { DO_WITH_TMP_VERBOSITY( SPxOut::INFO1, spxout, x ) }
119 /// Prints out message \p x if the verbosity level is at least SPxOut::INFO2.
120 #define MSG_INFO2(spxout, x) { DO_WITH_TMP_VERBOSITY( SPxOut::INFO2, spxout, x ) }
121 /// Prints out message \p x if the verbosity level is at least SPxOut::INFO3.
122 #define MSG_INFO3(spxout, x) { DO_WITH_TMP_VERBOSITY( SPxOut::INFO3, spxout, x ) }
123 
124 extern bool msginconsistent(const char* name, const char* file, int line);
125 
126 #define MSGinconsistent(name) msginconsistent(name, __FILE__, __LINE__)
127 
128 #if defined(SOPLEX_DEBUG)
129 // print output in any case, regardless of Param::verbose():
130 #define MSG_DEBUG(x) { x; }
131 #else
132 #define MSG_DEBUG(x) /**/
133 #endif //!SOPLEX_DEBUG
134 
135 
136 /*-----------------------------------------------------------------------------
137  * multi-thread support
138  *-----------------------------------------------------------------------------
139  */
140 // enable the user to compile without thread_local by setting USRCXXFLAGS=-DTHREADLOCAL=""
141 #if !defined(THREADLOCAL)
142 #if defined(_MSC_VER) && _MSC_VER < 1900
143 #define THREADLOCAL
144 #else
145 #define THREADLOCAL thread_local
146 #endif
147 #endif
148 
149 /*-----------------------------------------------------------------------------
150  * Long double support, Parameters and Epsilons
151  *-----------------------------------------------------------------------------
152  */
153 
154 #ifdef WITH_LONG_DOUBLE
155 
156 
157 typedef long double Real;
158 
159 #ifndef REAL
160 #define REAL(x) x##L
161 #define REAL_FORMAT "Lf"
162 #endif
163 /// default allowed bound violation
164 #ifndef DEFAULT_BND_VIOL
165 #define DEFAULT_BND_VIOL 1e-12L
166 #endif
167 /// default allowed additive zero: 1.0 + EPS_ZERO == 1.0
168 #ifndef DEFAULT_EPS_ZERO
169 #define DEFAULT_EPS_ZERO 1e-28L
170 #endif
171 /// epsilon for factorization
172 #ifndef DEFAULT_EPS_FACTOR
173 #define DEFAULT_EPS_FACTOR 1e-30L
174 #endif
175 /// epsilon for factorization update
176 #ifndef DEFAULT_EPS_UPDATE
177 #define DEFAULT_EPS_UPDATE 1e-26L
178 #endif
179 #ifndef DEFAULT_EPS_PIVOT
180 #define DEFAULT_EPS_PIVOT 1e-20L
181 #endif
182 ///
183 #define DEFAULT_INFINITY 1e100L
184 
185 
186 #else
187 
188 #ifdef WITH_FLOAT
189 
190 typedef float Real;
191 
192 #ifndef REAL
193 #define REAL(x) x
194 #define REAL_FORMAT "f"
195 #endif
196 /// default allowed bound violation
197 #ifndef DEFAULT_BND_VIOL
198 #define DEFAULT_BND_VIOL 1e-1
199 #endif
200 /// default allowed additive zero: 1.0 + EPS_ZERO == 1.0
201 #ifndef DEFAULT_EPS_ZERO
202 #define DEFAULT_EPS_ZERO 1e-7
203 #endif
204 #ifndef DEFAULT_EPS_FACTOR
205 #define DEFAULT_EPS_FACTOR 1e-7
206 #endif
207 #ifndef DEFAULT_EPS_UPDATE
208 #define DEFAULT_EPS_UPDATE 1e-6
209 #endif
210 #ifndef DEFAULT_EPS_PIVOT
211 #define DEFAULT_EPS_PIVOT 1e-6
212 #endif
213 #define DEFAULT_INFINITY 1e100
214 
215 
216 #else
217 
218 typedef double Real;
219 
220 #ifndef REAL
221 #define REAL(x) x
222 #define REAL_FORMAT "lf"
223 #endif
224 /// default allowed bound violation
225 #ifndef DEFAULT_BND_VIOL
226 #define DEFAULT_BND_VIOL 1e-6
227 #endif
228 /// default allowed additive zero: 1.0 + EPS_ZERO == 1.0
229 #ifndef DEFAULT_EPS_ZERO
230 #define DEFAULT_EPS_ZERO 1e-16
231 #endif
232 #ifndef DEFAULT_EPS_FACTOR
233 #define DEFAULT_EPS_FACTOR 1e-20
234 #endif
235 #ifndef DEFAULT_EPS_UPDATE
236 #define DEFAULT_EPS_UPDATE 1e-16
237 #endif
238 #ifndef DEFAULT_EPS_PIVOT
239 #define DEFAULT_EPS_PIVOT 1e-10
240 #endif
241 #define DEFAULT_INFINITY 1e100
242 
243 #endif // !WITH_FLOAT
244 #endif // !WITH_LONG_DOUBLE
245 
246 #define MAXIMUM(x,y) ((x)>(y) ? (x) : (y))
247 #define MINIMUM(x,y) ((x)<(y) ? (x) : (y))
248 
249 #define SPX_MAXSTRLEN 1024 /**< maximum string length in SoPlex */
250 
251 THREADLOCAL extern const Real infinity;
252 
253 class Param
254 {
255 private:
256 
257  //------------------------------------
258  /**@name Data */
259  //@{
260  /// default allowed additive zero: 1.0 + EPS_ZERO == 1.0
261  THREADLOCAL static Real s_epsilon;
262  /// epsilon for factorization
264  /// epsilon for factorization update
266  /// epsilon for pivot zero tolerance in factorization
268  //@}
269 
270 public:
271 
272  //------------------------------------
273  /**@name Access / modification */
274  //@{
275  ///
276  static Real epsilon();
277  ///
278  static void setEpsilon(Real eps);
279  ///
280  static Real epsilonFactorization();
281  ///
282  static void setEpsilonFactorization(Real eps);
283  ///
284  static Real epsilonUpdate();
285  ///
286  static void setEpsilonUpdate(Real eps);
287  ///
288  static Real epsilonPivot();
289  ///
290  static void setEpsilonPivot(Real eps);
291  //@}
292 };
293 
294 #ifdef WITH_LONG_DOUBLE
295 /// returns |a|
296 inline Real spxAbs(Real a)
297 {
298  return fabsl(a);
299 }
300 
301 /// returns square root
302 inline Real spxSqrt(Real a)
303 {
304  return sqrtl(a);
305 }
306 
307 // returns the next representable value after x in the direction of y
308 inline Real spxNextafter(Real x, Real y)
309 {
310  return nextafterl(x, y);
311 }
312 
313 /// returns x * 2^exp
314 inline Real spxLdexp(Real x, int exp)
315 {
316  return ldexpl(x, exp);
317 }
318 
319 // returns x and exp such that y = x * 2^exp
320 inline Real spxFrexp(Real y, int* exp)
321 {
322  return frexpl(y, exp);
323 }
324 #else
325 /// returns |a|
326 inline Real spxAbs(Real a)
327 {
328  return fabs(a);
329 }
330 
331 /// returns square root
332 inline Real spxSqrt(Real a)
333 {
334  return sqrt(a);
335 }
336 
337 // returns the next representable value after x in the direction of y
338 inline Real spxNextafter(Real x, Real y)
339 {
340 #ifndef _MSC_VER
341  return nextafter(x, y);
342 #else
343  return _nextafter(x, y);
344 #endif
345 }
346 
347 /// returns x * 2^exp
348 inline Real spxLdexp(Real x, int exp)
349 {
350  return ldexp(x, exp);
351 }
352 
353 // returns x and exp such that y = x * 2^exp
354 inline Real spxFrexp(Real y, int* exp)
355 {
356  return frexp(y, exp);
357 }
358 #endif
359 
360 /// returns max(|a|,|b|)
361 inline Real maxAbs(Real a, Real b)
362 {
363  const Real absa = spxAbs(a);
364  const Real absb = spxAbs(b);
365 
366  return absa > absb ? absa : absb;
367 }
368 
369 /// returns (a-b) / max(|a|,|b|,1.0)
370 inline Real relDiff(Real a, Real b)
371 {
372  return (a - b) / (maxAbs(a, b) > 1.0 ? maxAbs(a, b) : 1.0);
373 }
374 
375 /// returns \c true iff |a-b| <= eps
376 inline bool EQ(Real a, Real b, Real eps = Param::epsilon())
377 {
378  return spxAbs(a - b) <= eps;
379 }
380 
381 /// returns \c true iff |a-b| > eps
382 inline bool NE(Real a, Real b, Real eps = Param::epsilon())
383 {
384  return spxAbs(a - b) > eps;
385 }
386 
387 /// returns \c true iff a < b + eps
388 inline bool LT(Real a, Real b, Real eps = Param::epsilon())
389 {
390  return (a - b) < -eps;
391 }
392 
393 /// returns \c true iff a <= b + eps
394 inline bool LE(Real a, Real b, Real eps = Param::epsilon())
395 {
396  return (a - b) < eps;
397 }
398 
399 /// returns \c true iff a > b + eps
400 inline bool GT(Real a, Real b, Real eps = Param::epsilon())
401 {
402  return (a - b) > eps;
403 }
404 
405 /// returns \c true iff a >= b + eps
406 inline bool GE(Real a, Real b, Real eps = Param::epsilon())
407 {
408  return (a - b) > -eps;
409 }
410 
411 /// returns \c true iff |a| <= eps
412 inline bool isZero(Real a, Real eps = Param::epsilon())
413 {
414  return spxAbs(a) <= eps;
415 }
416 
417 /// returns \c true iff |a| > eps
418 inline bool isNotZero(Real a, Real eps = Param::epsilon())
419 {
420  return spxAbs(a) > eps;
421 }
422 
423 /// returns \c true iff |relDiff(a,b)| <= eps
424 inline bool EQrel(Real a, Real b, Real eps = Param::epsilon())
425 {
426  return spxAbs(relDiff(a, b)) <= eps;
427 }
428 
429 /// returns \c true iff |relDiff(a,b)| > eps
430 inline bool NErel(Real a, Real b, Real eps = Param::epsilon())
431 {
432  return spxAbs(relDiff(a, b)) > eps;
433 }
434 
435 /// returns \c true iff relDiff(a,b) <= -eps
436 inline bool LTrel(Real a, Real b, Real eps = Param::epsilon())
437 {
438  return relDiff(a, b) <= -eps;
439 }
440 
441 /// returns \c true iff relDiff(a,b) <= eps
442 inline bool LErel(Real a, Real b, Real eps = Param::epsilon())
443 {
444  return relDiff(a, b) <= eps;
445 }
446 
447 /// returns \c true iff relDiff(a,b) > eps
448 inline bool GTrel(Real a, Real b, Real eps = Param::epsilon())
449 {
450  return relDiff(a, b) > eps;
451 }
452 
453 /// returns \c true iff relDiff(a,b) > -eps
454 inline bool GErel(Real a, Real b, Real eps = Param::epsilon())
455 {
456  return relDiff(a, b) > -eps;
457 }
458 
459 /// safe version of snprintf
460 inline int spxSnprintf(
461  char* t, /**< target string */
462  size_t len, /**< length of the string to copy */
463  const char* s, /**< source string */
464  ... /**< further parameters */
465 )
466 {
467  va_list ap;
468  int n;
469 
470  assert(t != NULL);
471  assert(len > 0);
472 
473  va_start(ap, s); /*lint !e826*/
474 
475 #if defined(_WIN32) || defined(_WIN64)
476  n = _vsnprintf(t, len, s, ap);
477 #else
478  n = vsnprintf(t, len, s, ap); /*lint !e571*/
479 #endif
480  va_end(ap);
481 
482  if(n < 0 || (size_t) n >= len)
483  {
484 #ifndef NDEBUG
485 
486  if(n < 0)
487  {
488  MSG_ERROR(std::cerr << "vsnprintf returned " << n << " while reading: " << s << std::endl;)
489  }
490 
491 #endif
492  t[len - 1] = '\0';
493  n = (int) len - 1;
494  }
495 
496  return n;
497 }
498 
499 } // namespace soplex
500 #endif // _SPXDEFINES_H_
Rational spxAbs(const Rational &r)
Absolute.
Definition: rational.cpp:4102
bool isNotZero(Real a, Real eps=Param::epsilon())
returns true iff |a| > eps
Definition: spxdefines.h:418
bool LTrel(Real a, Real b, Real eps=Param::epsilon())
returns true iff relDiff(a,b) <= -eps
Definition: spxdefines.h:436
bool GE(Real a, Real b, Real eps=Param::epsilon())
returns true iff a >= b + eps
Definition: spxdefines.h:406
static void setEpsilon(Real eps)
Definition: spxdefines.cpp:50
THREADLOCAL const Real infinity
Definition: spxdefines.cpp:26
Real maxAbs(Real a, Real b)
returns max(|a|,|b|)
Definition: spxdefines.h:361
bool msginconsistent(const char *name, const char *file, int line)
Definition: spxdefines.cpp:33
bool LE(Real a, Real b, Real eps=Param::epsilon())
returns true iff a <= b + eps
Definition: spxdefines.h:394
bool LT(Real a, Real b, Real eps=Param::epsilon())
returns true iff a < b + eps
Definition: spxdefines.h:388
bool NE(Real a, Real b, Real eps=Param::epsilon())
returns true iff |a-b| > eps
Definition: spxdefines.h:382
Real spxFrexp(Real y, int *exp)
Definition: spxdefines.h:354
Real spxLdexp(Real x, int exp)
returns x * 2^exp
Definition: spxdefines.h:348
double Real
Definition: spxdefines.h:218
bool GT(Real a, Real b, Real eps=Param::epsilon())
returns true iff a > b + eps
Definition: spxdefines.h:400
static void setEpsilonPivot(Real eps)
Definition: spxdefines.cpp:80
#define MSG_ERROR(x)
Prints out message x if the verbosity level is at least SPxOut::ERROR.
Definition: spxdefines.h:114
int spxSnprintf(char *t, size_t len, const char *s,...)
safe version of snprintf
Definition: spxdefines.h:460
static THREADLOCAL Real s_epsilon_pivot
epsilon for pivot zero tolerance in factorization
Definition: spxdefines.h:267
static Real epsilonUpdate()
Definition: spxdefines.cpp:65
Real spxSqrt(Real a)
returns square root
Definition: spxdefines.h:332
bool GTrel(Real a, Real b, Real eps=Param::epsilon())
returns true iff relDiff(a,b) > eps
Definition: spxdefines.h:448
bool GErel(Real a, Real b, Real eps=Param::epsilon())
returns true iff relDiff(a,b) > -eps
Definition: spxdefines.h:454
static Real epsilon()
Definition: spxdefines.cpp:45
bool EQ(Real a, Real b, Real eps=Param::epsilon())
returns true iff |a-b| <= eps
Definition: spxdefines.h:376
static THREADLOCAL Real s_epsilon
default allowed additive zero: 1.0 + EPS_ZERO == 1.0
Definition: spxdefines.h:261
Everything should be within this namespace.
Real relDiff(Real a, Real b)
returns (a-b) / max(|a|,|b|,1.0)
Definition: spxdefines.h:370
bool EQrel(Real a, Real b, Real eps=Param::epsilon())
returns true iff |relDiff(a,b)| <= eps
Definition: spxdefines.h:424
Real spxNextafter(Real x, Real y)
Definition: spxdefines.h:338
static void setEpsilonUpdate(Real eps)
Definition: spxdefines.cpp:70
bool NErel(Real a, Real b, Real eps=Param::epsilon())
returns true iff |relDiff(a,b)| > eps
Definition: spxdefines.h:430
static THREADLOCAL Real s_epsilon_update
epsilon for factorization update
Definition: spxdefines.h:265
static Real epsilonPivot()
Definition: spxdefines.cpp:75
bool LErel(Real a, Real b, Real eps=Param::epsilon())
returns true iff relDiff(a,b) <= eps
Definition: spxdefines.h:442
static void setEpsilonFactorization(Real eps)
Definition: spxdefines.cpp:60
bool isZero(Real a, Real eps=Param::epsilon())
returns true iff |a| <= eps
Definition: spxdefines.h:412
static Real epsilonFactorization()
Definition: spxdefines.cpp:55
static THREADLOCAL Real s_epsilon_factorization
epsilon for factorization
Definition: spxdefines.h:263
#define THREADLOCAL
SOPLEX_DEBUG.
Definition: spxdefines.h:145