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  operator double() const;
135  operator long double() const;
136 
137 #ifdef SOPLEX_WITH_GMP
138  /// provides read-only access to underlying mpq_t
139  const mpq_t* getMpqPtr() const;
140 
141  /// provides read-only access to underlying mpq_t
142  const mpq_t& getMpqRef() const;
143 
144  /// provides write access to underlying mpq_t; use with care
145  mpq_t* getMpqPtr_w() const;
146 
147  /// provides write access to underlying mpq_t; use with care
148  mpq_t& getMpqRef_w() const;
149 #endif
150  //@}
151 
152 
153  //**@name Arithmetic operators */
154  //@{
155 
156  /// addition operator
157  Rational operator+(const Rational& r) const;
158 
159  /// addition assignment operator
160  Rational& operator+=(const Rational& r);
161 
162  /// addition operator for doubles
163  Rational operator+(const double& r) const;
164 
165  /// addition assignment operator for doubles
166  Rational& operator+=(const double& r);
167 
168  /// addition operator for ints
169  Rational operator+(const int& r) const;
170 
171  /// addition assignment operator for ints
172  Rational& operator+=(const int& r);
173 
174  /// subtraction operator
175  Rational operator-(const Rational& r) const;
176 
177  /// subtraction assignment operator
178  Rational& operator-=(const Rational& r);
179 
180  /// subtraction operator for doubles
181  Rational operator-(const double& r) const;
182 
183  /// subtraction assignment operator for doubles
184  Rational& operator-=(const double& r);
185 
186  /// subtraction operator for ints
187  Rational operator-(const int& r) const;
188 
189  /// subtraction assignment operator for ints
190  Rational& operator-=(const int& r);
191 
192  /// multiplication operator
193  Rational operator*(const Rational& r) const;
194 
195  /// multiplication assignment operator operator
196  Rational& operator*=(const Rational& r);
197 
198  /// multiplication operator for doubles
199  Rational operator*(const double& r) const;
200 
201  /// multiplication assignment operator for doubles
202  Rational& operator*=(const double& r);
203 
204  /// multiplication operator for ints
205  Rational operator*(const int& r) const;
206 
207  /// multiplication assignment operator for ints
208  Rational& operator*=(const int& r);
209 
210  /// division operator
211  Rational operator/(const Rational& r) const;
212 
213  /// division assignment operator
214  Rational& operator/=(const Rational& r);
215 
216  /// division operator for doubles
217  Rational operator/(const double& r) const;
218 
219  /// division assignment operator for doubles
220  Rational& operator/=(const double& r);
221 
222  /// division operator for ints
223  Rational operator/(const int& r) const;
224 
225  /// division assignment operator for ints
226  Rational& operator/=(const int& r);
227 
228  /// add product of two rationals
229  Rational& addProduct(const Rational& r, const Rational& s);
230 
231  /// subtract product of two rationals
232  Rational& subProduct(const Rational& r, const Rational& s);
233 
234  /// add quotient of two rationals, r divided by s
235  Rational& addQuotient(const Rational& r, const Rational& s);
236 
237  /// subtract quotient of two rationals, r divided by s
238  Rational& subQuotient(const Rational& r, const Rational& s);
239 
240  /// inversion
241  Rational& invert();
242 
243  /// round up to next power of two
244  Rational& powRound();
245 
246  //@}
247 
248 
249  //**@name Methods for checking exactness of doubles */
250  //@{
251 
252  /// checks if \p d is the closest number that can be represented by double
253  bool isNextTo(const double& d);
254 
255  /// checks if \p d is exactly equal to the Rational and if not, if it is one of the two adjacent doubles
256  bool isAdjacentTo(const double& d) const;
257 
258  //@}
259 
260 
261  //**@name Methods for querying size */
262  //@{
263 
264  /// Size in specified base (bit size for base 2)
265  int sizeInBase(const int base = 2) const;
266 
267  //@}
268 
269 
270  //**@name Static methods */
271  //@{
272 
273  /// returns precision of Rational implementation, i.e., number of bits used to store Rational numbers (INT_MAX if exact)
274  static int precision();
275 
276  //@}
277 
278 
279  //**@name Conversion from and to String */
280  //@{
281 
282  /// read Rational from string
283  bool readString(const char* s);
284 
285  friend std::string rationalToString(const Rational& r, const int precision);
286  friend bool readStringRational(const char* s, Rational& value);
287  friend std::ostream& operator<<(std::ostream& os, const Rational& q);
288 
289  //@}
290 
291 
292  //**@name Friends */
293  //@{
294 
295  friend int compareRational(const Rational& r, const Rational& s);
296  friend bool operator!=(const Rational& r, const Rational& s);
297  friend bool operator==(const Rational& r, const Rational& s);
298  friend bool operator<(const Rational& r, const Rational& s);
299  friend bool operator<=(const Rational& r, const Rational& s);
300  friend bool operator>(const Rational& r, const Rational& s);
301  friend bool operator>=(const Rational& r, const Rational& s);
302 
303  friend bool operator!=(const Rational& r, const double& s);
304  friend bool operator==(const Rational& r, const double& s);
305  friend bool operator<(const Rational& r, const double& s);
306  friend bool operator<=(const Rational& r, const double& s);
307  friend bool operator>(const Rational& r, const double& s);
308  friend bool operator>=(const Rational& r, const double& s);
309 
310  friend bool operator!=(const double& r, const Rational& s);
311  friend bool operator==(const double& r, const Rational& s);
312  friend bool operator<(const double& r, const Rational& s);
313  friend bool operator<=(const double& r, const Rational& s);
314  friend bool operator>(const double& r, const Rational& s);
315  friend bool operator>=(const double& r, const Rational& s);
316 
317  friend bool operator!=(const Rational& r, const long double& s);
318  friend bool operator==(const Rational& r, const long double& s);
319  friend bool operator<(const Rational& r, const long double& s);
320  friend bool operator<=(const Rational& r, const long double& s);
321  friend bool operator>(const Rational& r, const long double& s);
322  friend bool operator>=(const Rational& r, const long double& s);
323 
324  friend bool operator!=(const long double& r, const Rational& s);
325  friend bool operator==(const long double& r, const Rational& s);
326  friend bool operator<(const long double& r, const Rational& s);
327  friend bool operator<=(const long double& r, const Rational& s);
328  friend bool operator>(const long double& r, const Rational& s);
329  friend bool operator>=(const long double& r, const Rational& s);
330 
331  friend Rational operator+(const double& d, const Rational& r);
332  friend Rational operator-(const double& d, const Rational& r);
333  friend Rational operator*(const double& d, const Rational& r);
334  friend Rational operator/(const double& d, const Rational& r);
335 
336  friend bool operator!=(const Rational& r, const int& s);
337  friend bool operator==(const Rational& r, const int& s);
338  friend bool operator<(const Rational& r, const int& s);
339  friend bool operator<=(const Rational& r, const int& s);
340  friend bool operator>(const Rational& r, const int& s);
341  friend bool operator>=(const Rational& r, const int& s);
342 
343  friend bool operator!=(const int& r, const Rational& s);
344  friend bool operator==(const int& r, const Rational& s);
345  friend bool operator<(const int& r, const Rational& s);
346  friend bool operator<=(const int& r, const Rational& s);
347  friend bool operator>(const int& r, const Rational& s);
348  friend bool operator>=(const int& r, const Rational& s);
349 
350  friend Rational operator+(const int& d, const Rational& r);
351  friend Rational operator-(const int& d, const Rational& r);
352  friend Rational operator*(const int& d, const Rational& r);
353  friend Rational operator/(const int& d, const Rational& r);
354 
355  friend Rational spxAbs(const Rational& r);
356  friend int sign(const Rational& r);
357  friend Rational operator-(const Rational& q);
358 
359  //@}
360  };
361 
362 
363  //**@name Parsing and printing */
364  //@{
365 
366  /// convert rational number to string
367  std::string rationalToString(const Rational& r, const int precision = 32);
368 
369  /// read Rational from string
370  bool readStringRational(const char* s, Rational& value);
371 
372  /// print Rational
373  std::ostream& operator<<(std::ostream& os, const Rational& r);
374 
375  //@}
376 
377 
378  //**@name Relational operators */
379  //@{
380 
381  /// comparison operator returning a positive value if r > s, zero if r = s, and a negative value if r < s
382  int compareRational(const Rational& r, const Rational& s);
383 
384  /// equality operator
385  bool operator==(const Rational& r, const Rational& s);
386 
387  /// inequality operator
388  bool operator!=(const Rational& r, const Rational& s);
389 
390  /// less than operator
391  bool operator<(const Rational& r, const Rational& s);
392 
393  /// less than or equal to operator
394  bool operator<=(const Rational& r, const Rational& s);
395 
396  /// greater than operator
397  bool operator>(const Rational& r, const Rational& s);
398 
399  /// greater than or equal to operator
400  bool operator>=(const Rational& r, const Rational& s);
401 
402  /// equality operator for Rational and double
403  bool operator==(const Rational& r, const double& s);
404 
405  /// inequality operator for Rational and double
406  bool operator!=(const Rational& r, const double& s);
407 
408  /// less than operator for Rational and double
409  bool operator<(const Rational& r, const double& s);
410 
411  /// less than or equal to operator for Rational and double
412  bool operator<=(const Rational& r, const double& s);
413 
414  /// greater than operator for Rational and double
415  bool operator>(const Rational& r, const double& s);
416 
417  /// greater than or equal to operator for Rational and double
418  bool operator>=(const Rational& r, const double& s);
419 
420  /// equality operator for double and Rational
421  bool operator==(const double& r, const Rational& s);
422 
423  /// inequality operator for double and Rational
424  bool operator!=(const double& r, const Rational& s);
425 
426  /// less than operator for double and Rational
427  bool operator<(const double& r, const Rational& s);
428 
429  /// less than or equal to operator for double and Rational
430  bool operator<=(const double& r, const Rational& s);
431 
432  /// greater than operator for double and Rational
433  bool operator>(const double& r, const Rational& s);
434 
435  /// greater than or equal to operator for double and Rational
436  bool operator>=(const double& r, const Rational& s);
437 
438  /// equality operator for Rational and long double
439  bool operator==(const Rational& r, const long double& s);
440 
441  /// inequality operator for Rational and long double
442  bool operator!=(const Rational& r, const long double& s);
443 
444  /// less than operator for Rational and long double
445  bool operator<(const Rational& r, const long double& s);
446 
447  /// less than or equal to operator for Rational and long double
448  bool operator<=(const Rational& r, const long double& s);
449 
450  /// greater than operator for Rational and long double
451  bool operator>(const Rational& r, const long double& s);
452 
453  /// greater than or equal to operator for Rational and long double
454  bool operator>=(const Rational& r, const long double& s);
455 
456  /// equality operator for long double and Rational
457  bool operator==(const long double& r, const Rational& s);
458 
459  /// inequality operator for long double and Rational
460  bool operator!=(const long double& r, const Rational& s);
461 
462  /// less than operator for long double and Rational
463  bool operator<(const long double& r, const Rational& s);
464 
465  /// less than or equal to operator for long double and Rational
466  bool operator<=(const long double& r, const Rational& s);
467 
468  /// greater than operator for long double and Rational
469  bool operator>(const long double& r, const Rational& s);
470 
471  /// greater than or equal to operator for long double and Rational
472  bool operator>=(const long double& r, const Rational& s);
473 
474  /// equality operator for Rational and int
475  bool operator==(const Rational& r, const int& s);
476 
477  /// inequality operator for Rational and int
478  bool operator!=(const Rational& r, const int& s);
479 
480  /// less than operator for Rational and int
481  bool operator<(const Rational& r, const int& s);
482 
483  /// less than or equal to operator for Rational and int
484  bool operator<=(const Rational& r, const int& s);
485 
486  /// greater than operator for Rational and int
487  bool operator>(const Rational& r, const int& s);
488 
489  /// greater than or equal to operator for Rational and int
490  bool operator>=(const Rational& r, const int& s);
491 
492  /// equality operator for int and Rational
493  bool operator==(const int& r, const Rational& s);
494 
495  /// inequality operator for int and Rational
496  bool operator!=(const int& r, const Rational& s);
497 
498  /// less than operator for int and Rational
499  bool operator<(const int& r, const Rational& s);
500 
501  /// less than or equal to operator for int and Rational
502  bool operator<=(const int& r, const Rational& s);
503 
504  /// greater than operator for int and Rational
505  bool operator>(const int& r, const Rational& s);
506 
507  /// greater than or equal to operator for int and Rational
508  bool operator>=(const int& r, const Rational& s);
509 
510  //@}
511 
512 
513  //**@name Non-member arithmetic operators and functions */
514  //@{
515 
516  /// addition operator for double and Rational
517  Rational operator+(const double& d, const Rational& r);
518 
519  /// addition operator for double and Rational
520  Rational operator+(const double& d, const Rational& r);
521 
522  /// addition operator for double and Rational
523  Rational operator+(const double& d, const Rational& r);
524 
525  /// addition operator for double and Rational
526  Rational operator+(const double& d, const Rational& r);
527 
528  /// addition operator for int and Rational
529  Rational operator+(const int& d, const Rational& r);
530 
531  /// addition operator for int and Rational
532  Rational operator+(const int& d, const Rational& r);
533 
534  /// addition operator for int and Rational
535  Rational operator+(const int& d, const Rational& r);
536 
537  /// addition operator for int and Rational
538  Rational operator+(const int& d, const Rational& r);
539 
540  /// absolute function
541  Rational spxAbs(const Rational& r);
542 
543  /// Sign function; returns 1 if r > 0, 0 if r = 0, and -1 if r < 0.
544  int sign(const Rational& r);
545 
546  /// Negation.
547  Rational operator-(const Rational& r);
548 
549  /// Total size of rational vector.
550  int totalSizeRational(const Rational* vector, const int length, const int base = 2);
551 
552  /// Size of least common multiple of denominators in rational vector.
553  int dlcmSizeRational(const Rational* vector, const int length, const int base = 2);
554 
555  /// Size of largest denominator in rational vector.
556  int dmaxSizeRational(const Rational* vector, const int length, const int base = 2);
557 
558  //@}
559 
560 } // namespace soplex
561 #else
562 namespace soplex
563 {
564  typedef Real Rational;
565 }
566 #endif
567 #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:215
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:142
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