Scippy

SoPlex

Sequential object-oriented simPlex

rational.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-2017 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 rational.h
17  * @brief Wrapper for GMP types.
18  */
19 #ifndef _RATIONAL_H_
20 #define _RATIONAL_H_
21 
22 #ifndef SOPLEX_LEGACY
23 #include <math.h>
24 #include <assert.h>
25 #include <string.h>
26 #include <iostream>
27 
28 #include "spxdefines.h"
29 #include "idlist.h"
30 
31 #ifdef SOPLEX_WITH_GMP
32 #include "gmp.h"
33 #endif
34 
35 
36 namespace soplex
37 {
38 /**@brief Wrapper for GMP type mpq_class.
39  * @ingroup Algebra
40  *
41  * We wrap mpq_class so that we can replace it by a double type if GMP is not available.
42  */
43 
44 /// If compiled with GMP support, Rational is defined as mpq_class.
45  class Rational
46  {
47  private:
48  class Private;
50 
51 #ifdef SOPLEX_WITH_GMP
52  THREADLOCAL static IdList< Private > unusedPrivateList;
53  THREADLOCAL static bool useListMem;
54 
55 
56  /// special constructor only for initializing static rational variables; this is necessary since we need a
57  /// constructor for Rational::{ZERO, POSONE, NEGONE} that does not use these numbers
58  Rational(const int& i, const bool& dummy);
59 
60  //**@name Static variables for special rational values */
61  //@{
62 
63  static const Rational ZERO;
64  static const Rational POSONE;
65  static const Rational NEGONE;
66 
67  //@}
68 #endif
69 
70  public:
71 
72  //**@name Construction and destruction */
73  //@{
74 
75  /// default constructor
76  Rational();
77 
78  /// copy constructor
79  Rational(const Rational& r);
80 
81  /// constructor from long double
82  Rational(const long double& r);
83 
84  /// constructor from double
85  Rational(const double& r);
86 
87  ///constructor from int
88  Rational(const int& i);
89 
90 #ifdef SOPLEX_WITH_GMP
91  /// constructor from mpq_t
92  Rational(const mpq_t& q);
93 #endif
94 
95  /// destructor
96  ~Rational();
97 
98  /// enables list memory
99  static void enableListMem();
100 
101  /// frees the unused rational elements in the memory list
102  /** this can be useful when you want to save memory or needed when working with a GMP memory manager like the one
103  * in EGlib that frees GMP memory before the destructor of the static memory list is called; in most cases this
104  * method is optional; note that this does not free the Rational elements that are currently in use
105  */
106  static void freeListMem();
107 
108  /// disables list memory
109  static void disableListMem();
110 
111  /// assignment operator
112  Rational& operator=(const Rational&);
113 
114  /// assignment operator from long double
115  Rational& operator=(const long double &r);
116 
117  /// assignment operator from double
118  Rational& operator=(const double &r);
119 
120  /// assignment operator from int
121  Rational& operator=(const int &i);
122 
123 #ifdef SOPLEX_WITH_GMP
124  /// assignment operator from mpq_t
125  Rational& operator=(const mpq_t &q);
126 #endif
127 
128  //@}
129 
130 
131  //**@name Typecasts */
132  //@{
133 
134  // Visual Studio <= 2010 does not fully support C++11, this makes
135  // it compiling (noone checked whether it also works properly)
136 #if defined(_MSC_VER) && _MSC_VER <= 1600
137  /// typecasts Rational to double (only allows explicit typecasting)
138  explicit operator double() const;
139 
140  /// typecasts Rational to long double (only allows explicit typecasting)
141  explicit operator long double() const;
142 #else
143  operator double() const;
144  operator long double() const;
145 #endif
146 
147 #ifdef SOPLEX_WITH_GMP
148  /// provides read-only access to underlying mpq_t
149  const mpq_t* getMpqPtr() const;
150 
151  /// provides read-only access to underlying mpq_t
152  const mpq_t& getMpqRef() const;
153 
154  /// provides write access to underlying mpq_t; use with care
155  mpq_t* getMpqPtr_w() const;
156 
157  /// provides write access to underlying mpq_t; use with care
158  mpq_t& getMpqRef_w() const;
159 #endif
160  //@}
161 
162 
163  //**@name Arithmetic operators */
164  //@{
165 
166  /// addition operator
167  Rational operator+(const Rational& r) const;
168 
169  /// addition assignment operator
170  Rational& operator+=(const Rational& r);
171 
172  /// addition operator for doubles
173  Rational operator+(const double& r) const;
174 
175  /// addition assignment operator for doubles
176  Rational& operator+=(const double& r);
177 
178  /// addition operator for ints
179  Rational operator+(const int& r) const;
180 
181  /// addition assignment operator for ints
182  Rational& operator+=(const int& r);
183 
184  /// subtraction operator
185  Rational operator-(const Rational& r) const;
186 
187  /// subtraction assignment operator
188  Rational& operator-=(const Rational& r);
189 
190  /// subtraction operator for doubles
191  Rational operator-(const double& r) const;
192 
193  /// subtraction assignment operator for doubles
194  Rational& operator-=(const double& r);
195 
196  /// subtraction operator for ints
197  Rational operator-(const int& r) const;
198 
199  /// subtraction assignment operator for ints
200  Rational& operator-=(const int& r);
201 
202  /// multiplication operator
203  Rational operator*(const Rational& r) const;
204 
205  /// multiplication assignment operator operator
206  Rational& operator*=(const Rational& r);
207 
208  /// multiplication operator for doubles
209  Rational operator*(const double& r) const;
210 
211  /// multiplication assignment operator for doubles
212  Rational& operator*=(const double& r);
213 
214  /// multiplication operator for ints
215  Rational operator*(const int& r) const;
216 
217  /// multiplication assignment operator for ints
218  Rational& operator*=(const int& r);
219 
220  /// division operator
221  Rational operator/(const Rational& r) const;
222 
223  /// division assignment operator
224  Rational& operator/=(const Rational& r);
225 
226  /// division operator for doubles
227  Rational operator/(const double& r) const;
228 
229  /// division assignment operator for doubles
230  Rational& operator/=(const double& r);
231 
232  /// division operator for ints
233  Rational operator/(const int& r) const;
234 
235  /// division assignment operator for ints
236  Rational& operator/=(const int& r);
237 
238  /// add product of two rationals
239  Rational& addProduct(const Rational& r, const Rational& s);
240 
241  /// subtract product of two rationals
242  Rational& subProduct(const Rational& r, const Rational& s);
243 
244  /// add quotient of two rationals, r divided by s
245  Rational& addQuotient(const Rational& r, const Rational& s);
246 
247  /// subtract quotient of two rationals, r divided by s
248  Rational& subQuotient(const Rational& r, const Rational& s);
249 
250  /// inversion
251  Rational& invert();
252 
253  /// round up to next power of two
254  Rational& powRound();
255 
256  //@}
257 
258 
259  //**@name Methods for checking exactness of doubles */
260  //@{
261 
262  /// checks if \p d is the closest number that can be represented by double
263  bool isNextTo(const double& d);
264 
265  /// checks if \p d is exactly equal to the Rational and if not, if it is one of the two adjacent doubles
266  bool isAdjacentTo(const double& d) const;
267 
268  //@}
269 
270 
271  //**@name Methods for querying size */
272  //@{
273 
274  /// Size in specified base (bit size for base 2)
275  int sizeInBase(const int base = 2) const;
276 
277  //@}
278 
279 
280  //**@name Static methods */
281  //@{
282 
283  /// returns precision of Rational implementation, i.e., number of bits used to store Rational numbers (INT_MAX if exact)
284  static int precision();
285 
286  //@}
287 
288 
289  //**@name Conversion from and to String */
290  //@{
291 
292  /// read Rational from string
293  bool readString(const char* s);
294 
295  friend std::string rationalToString(const Rational& r, const int precision);
296  friend bool readStringRational(const char* s, Rational& value);
297  friend std::ostream& operator<<(std::ostream& os, const Rational& q);
298 
299  //@}
300 
301 
302  //**@name Friends */
303  //@{
304 
305  friend int compareRational(const Rational& r, const Rational& s);
306  friend bool operator!=(const Rational& r, const Rational& s);
307  friend bool operator==(const Rational& r, const Rational& s);
308  friend bool operator<(const Rational& r, const Rational& s);
309  friend bool operator<=(const Rational& r, const Rational& s);
310  friend bool operator>(const Rational& r, const Rational& s);
311  friend bool operator>=(const Rational& r, const Rational& s);
312 
313  friend bool operator!=(const Rational& r, const double& s);
314  friend bool operator==(const Rational& r, const double& s);
315  friend bool operator<(const Rational& r, const double& s);
316  friend bool operator<=(const Rational& r, const double& s);
317  friend bool operator>(const Rational& r, const double& s);
318  friend bool operator>=(const Rational& r, const double& s);
319 
320  friend bool operator!=(const double& r, const Rational& s);
321  friend bool operator==(const double& r, const Rational& s);
322  friend bool operator<(const double& r, const Rational& s);
323  friend bool operator<=(const double& r, const Rational& s);
324  friend bool operator>(const double& r, const Rational& s);
325  friend bool operator>=(const double& r, const Rational& s);
326 
327  friend bool operator!=(const Rational& r, const long double& s);
328  friend bool operator==(const Rational& r, const long double& s);
329  friend bool operator<(const Rational& r, const long double& s);
330  friend bool operator<=(const Rational& r, const long double& s);
331  friend bool operator>(const Rational& r, const long double& s);
332  friend bool operator>=(const Rational& r, const long double& s);
333 
334  friend bool operator!=(const long double& r, const Rational& s);
335  friend bool operator==(const long double& r, const Rational& s);
336  friend bool operator<(const long double& r, const Rational& s);
337  friend bool operator<=(const long double& r, const Rational& s);
338  friend bool operator>(const long double& r, const Rational& s);
339  friend bool operator>=(const long double& r, const Rational& s);
340 
341  friend Rational operator+(const double& d, const Rational& r);
342  friend Rational operator-(const double& d, const Rational& r);
343  friend Rational operator*(const double& d, const Rational& r);
344  friend Rational operator/(const double& d, const Rational& r);
345 
346  friend bool operator!=(const Rational& r, const int& s);
347  friend bool operator==(const Rational& r, const int& s);
348  friend bool operator<(const Rational& r, const int& s);
349  friend bool operator<=(const Rational& r, const int& s);
350  friend bool operator>(const Rational& r, const int& s);
351  friend bool operator>=(const Rational& r, const int& s);
352 
353  friend bool operator!=(const int& r, const Rational& s);
354  friend bool operator==(const int& r, const Rational& s);
355  friend bool operator<(const int& r, const Rational& s);
356  friend bool operator<=(const int& r, const Rational& s);
357  friend bool operator>(const int& r, const Rational& s);
358  friend bool operator>=(const int& r, const Rational& s);
359 
360  friend Rational operator+(const int& d, const Rational& r);
361  friend Rational operator-(const int& d, const Rational& r);
362  friend Rational operator*(const int& d, const Rational& r);
363  friend Rational operator/(const int& d, const Rational& r);
364 
365  friend Rational spxAbs(const Rational& r);
366  friend int sign(const Rational& r);
367  friend Rational operator-(const Rational& q);
368 
369  //@}
370  };
371 
372 
373  //**@name Parsing and printing */
374  //@{
375 
376  /// convert rational number to string
377  std::string rationalToString(const Rational& r, const int precision = 32);
378 
379  /// read Rational from string
380  bool readStringRational(const char* s, Rational& value);
381 
382  /// print Rational
383  std::ostream& operator<<(std::ostream& os, const Rational& r);
384 
385  //@}
386 
387 
388  //**@name Relational operators */
389  //@{
390 
391  /// comparison operator returning a positive value if r > s, zero if r = s, and a negative value if r < s
392  int compareRational(const Rational& r, const Rational& s);
393 
394  /// equality operator
395  bool operator==(const Rational& r, const Rational& s);
396 
397  /// inequality operator
398  bool operator!=(const Rational& r, const Rational& s);
399 
400  /// less than operator
401  bool operator<(const Rational& r, const Rational& s);
402 
403  /// less than or equal to operator
404  bool operator<=(const Rational& r, const Rational& s);
405 
406  /// greater than operator
407  bool operator>(const Rational& r, const Rational& s);
408 
409  /// greater than or equal to operator
410  bool operator>=(const Rational& r, const Rational& s);
411 
412  /// equality operator for Rational and double
413  bool operator==(const Rational& r, const double& s);
414 
415  /// inequality operator for Rational and double
416  bool operator!=(const Rational& r, const double& s);
417 
418  /// less than operator for Rational and double
419  bool operator<(const Rational& r, const double& s);
420 
421  /// less than or equal to operator for Rational and double
422  bool operator<=(const Rational& r, const double& s);
423 
424  /// greater than operator for Rational and double
425  bool operator>(const Rational& r, const double& s);
426 
427  /// greater than or equal to operator for Rational and double
428  bool operator>=(const Rational& r, const double& s);
429 
430  /// equality operator for double and Rational
431  bool operator==(const double& r, const Rational& s);
432 
433  /// inequality operator for double and Rational
434  bool operator!=(const double& r, const Rational& s);
435 
436  /// less than operator for double and Rational
437  bool operator<(const double& r, const Rational& s);
438 
439  /// less than or equal to operator for double and Rational
440  bool operator<=(const double& r, const Rational& s);
441 
442  /// greater than operator for double and Rational
443  bool operator>(const double& r, const Rational& s);
444 
445  /// greater than or equal to operator for double and Rational
446  bool operator>=(const double& r, const Rational& s);
447 
448  /// equality operator for Rational and long double
449  bool operator==(const Rational& r, const long double& s);
450 
451  /// inequality operator for Rational and long double
452  bool operator!=(const Rational& r, const long double& s);
453 
454  /// less than operator for Rational and long double
455  bool operator<(const Rational& r, const long double& s);
456 
457  /// less than or equal to operator for Rational and long double
458  bool operator<=(const Rational& r, const long double& s);
459 
460  /// greater than operator for Rational and long double
461  bool operator>(const Rational& r, const long double& s);
462 
463  /// greater than or equal to operator for Rational and long double
464  bool operator>=(const Rational& r, const long double& s);
465 
466  /// equality operator for long double and Rational
467  bool operator==(const long double& r, const Rational& s);
468 
469  /// inequality operator for long double and Rational
470  bool operator!=(const long double& r, const Rational& s);
471 
472  /// less than operator for long double and Rational
473  bool operator<(const long double& r, const Rational& s);
474 
475  /// less than or equal to operator for long double and Rational
476  bool operator<=(const long double& r, const Rational& s);
477 
478  /// greater than operator for long double and Rational
479  bool operator>(const long double& r, const Rational& s);
480 
481  /// greater than or equal to operator for long double and Rational
482  bool operator>=(const long double& r, const Rational& s);
483 
484  /// equality operator for Rational and int
485  bool operator==(const Rational& r, const int& s);
486 
487  /// inequality operator for Rational and int
488  bool operator!=(const Rational& r, const int& s);
489 
490  /// less than operator for Rational and int
491  bool operator<(const Rational& r, const int& s);
492 
493  /// less than or equal to operator for Rational and int
494  bool operator<=(const Rational& r, const int& s);
495 
496  /// greater than operator for Rational and int
497  bool operator>(const Rational& r, const int& s);
498 
499  /// greater than or equal to operator for Rational and int
500  bool operator>=(const Rational& r, const int& s);
501 
502  /// equality operator for int and Rational
503  bool operator==(const int& r, const Rational& s);
504 
505  /// inequality operator for int and Rational
506  bool operator!=(const int& r, const Rational& s);
507 
508  /// less than operator for int and Rational
509  bool operator<(const int& r, const Rational& s);
510 
511  /// less than or equal to operator for int and Rational
512  bool operator<=(const int& r, const Rational& s);
513 
514  /// greater than operator for int and Rational
515  bool operator>(const int& r, const Rational& s);
516 
517  /// greater than or equal to operator for int and Rational
518  bool operator>=(const int& r, const Rational& s);
519 
520  //@}
521 
522 
523  //**@name Non-member arithmetic operators and functions */
524  //@{
525 
526  /// addition operator for double and Rational
527  Rational operator+(const double& d, const Rational& r);
528 
529  /// addition operator for double and Rational
530  Rational operator+(const double& d, const Rational& r);
531 
532  /// addition operator for double and Rational
533  Rational operator+(const double& d, const Rational& r);
534 
535  /// addition operator for double and Rational
536  Rational operator+(const double& d, const Rational& r);
537 
538  /// addition operator for int and Rational
539  Rational operator+(const int& d, const Rational& r);
540 
541  /// addition operator for int and Rational
542  Rational operator+(const int& d, const Rational& r);
543 
544  /// addition operator for int and Rational
545  Rational operator+(const int& d, const Rational& r);
546 
547  /// addition operator for int and Rational
548  Rational operator+(const int& d, const Rational& r);
549 
550  /// absolute function
551  Rational spxAbs(const Rational& r);
552 
553  /// Sign function; returns 1 if r > 0, 0 if r = 0, and -1 if r < 0.
554  int sign(const Rational& r);
555 
556  /// Negation.
557  Rational operator-(const Rational& r);
558 
559  /// Total size of rational vector.
560  int totalSizeRational(const Rational* vector, const int length, const int base = 2);
561 
562  /// Size of least common multiple of denominators in rational vector.
563  int dlcmSizeRational(const Rational* vector, const int length, const int base = 2);
564 
565  /// Size of largest denominator in rational vector.
566  int dmaxSizeRational(const Rational* vector, const int length, const int base = 2);
567 
568  //@}
569 
570 } // namespace soplex
571 #else
572 namespace soplex
573 {
574  typedef Real Rational;
575 }
576 #endif
577 #endif // _RATIONAL_H_
Rational & addProduct(const Rational &r, const Rational &s)
add product of two rationals
Definition: rational.cpp:3356
bool isAdjacentTo(const double &d) const
checks if d is exactly equal to the Rational and if not, if it is one of the two adjacent doubles ...
Definition: rational.cpp:3418
bool isNextTo(const double &d)
checks if d is the closest number that can be represented by double
Definition: rational.cpp:3410
Generic Real linked list.
friend Rational spxAbs(const Rational &r)
Absolute.
Definition: rational.cpp:3909
friend std::string rationalToString(const Rational &r, const int precision)
convert rational number to string
Definition: rational.cpp:3452
int dlcmSizeRational(const Rational *vector, const int length, const int base)
Size of least common multiple of denominators in rational vector.
Definition: rational.cpp:3957
Generic Real linked list.Class IdList implements an intrusive Real linked list as a template class...
Definition: idlist.h:123
friend bool operator<(const Rational &r, const Rational &s)
less than operator
Definition: rational.cpp:3508
Rational operator*(const Rational &r) const
multiplication operator
Definition: rational.cpp:3242
~Rational()
destructor
Definition: rational.cpp:3039
int sizeInBase(const int base=2) const
Size in specified base (bit size for base 2)
Definition: rational.cpp:3426
friend bool operator<=(const Rational &r, const Rational &s)
less than or equal to operator
Definition: rational.cpp:3516
Rational operator-(const Rational &r) const
subtraction operator
Definition: rational.cpp:3185
friend bool operator!=(const Rational &r, const Rational &s)
inequality operator
Definition: rational.cpp:3500
Rational & subProduct(const Rational &r, const Rational &s)
subtract product of two rationals
Definition: rational.cpp:3365
Rational & invert()
inversion
Definition: rational.cpp:3392
friend bool operator>=(const Rational &r, const Rational &s)
greater than or equal to operator
Definition: rational.cpp:3532
int totalSizeRational(const Rational *vector, const int length, const int base)
Total size of rational vector.
Definition: rational.cpp:3940
Wrapper for GMP type mpq_class.We wrap mpq_class so that we can replace it by a double type if GMP is...
Definition: rational.h:45
static void disableListMem()
disables list memory
Definition: rational.cpp:3067
Rational & operator/=(const Rational &r)
division assignment operator
Definition: rational.cpp:3309
Rational & operator-=(const Rational &r)
subtraction assignment operator
Definition: rational.cpp:3195
static void freeListMem()
frees the unused rational elements in the memory list
Definition: rational.cpp:3059
int dmaxSizeRational(const Rational *vector, const int length, const int base)
Size of largest denominator in rational vector.
Definition: rational.cpp:3969
double Real
Definition: spxdefines.h:214
Rational & operator*=(const Rational &r)
multiplication assignment operator operator
Definition: rational.cpp:3252
bool readString(const char *s)
read Rational from string
Definition: rational.cpp:3443
static int precision()
returns precision of Rational implementation, i.e., number of bits used to store Rational numbers (IN...
Definition: rational.cpp:3435
friend int sign(const Rational &r)
Sign function; returns 1 if r > 0, 0 if r = 0, and -1 if r < 0.
Definition: rational.cpp:3922
Defines the "Pimpl"-class Private.
Definition: rational.cpp:2919
friend std::ostream & operator<<(std::ostream &os, const Rational &q)
print Rational
Definition: rational.cpp:3470
static void enableListMem()
enables list memory
Definition: rational.cpp:3047
Debugging, floating point type and parameter definitions.
Private * dpointer
Definition: rational.h:48
friend bool operator==(const Rational &r, const Rational &s)
equality operator
Definition: rational.cpp:3492
friend int compareRational(const Rational &r, const Rational &s)
comparison operator returning a positive value if r > s, zero if r = s, and a negative value if r < s...
Definition: rational.cpp:3479
Rational operator+(const Rational &r) const
addition operator
Definition: rational.cpp:3128
Everything should be within this namespace.
Rational operator/(const Rational &r) const
division operator
Definition: rational.cpp:3299
friend bool readStringRational(const char *s, Rational &value)
read Rational from string
Definition: rational.cpp:3462
Rational & operator=(const Rational &)
assignment operator
Definition: rational.cpp:3075
Rational & subQuotient(const Rational &r, const Rational &s)
subtract quotient of two rationals, r divided by s
Definition: rational.cpp:3383
friend bool operator>(const Rational &r, const Rational &s)
greater than operator
Definition: rational.cpp:3524
Rational()
default constructor
Definition: rational.cpp:2989
#define THREADLOCAL
SOPLEX_DEBUG.
Definition: spxdefines.h:141
Rational & addQuotient(const Rational &r, const Rational &s)
add quotient of two rationals, r divided by s
Definition: rational.cpp:3374
Rational & operator+=(const Rational &r)
addition assignment operator
Definition: rational.cpp:3138
Rational & powRound()
round up to next power of two
Definition: rational.cpp:3401