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