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
51  THREADLOCAL static IdList< Private > unusedPrivateList;
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
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 #ifdef SOPLEX_WITH_GMP
123  /// assignment operator from mpq_t
124  Rational& operator=(const mpq_t& q);
125 #endif
126 
127  //@}
128 
129 
130  //**@name Typecasts */
131  //@{
132 
133  operator double() const;
134  operator long double() const;
135 
136 #ifdef SOPLEX_WITH_GMP
137  /// provides read-only access to underlying mpq_t
138  const mpq_t* getMpqPtr() const;
139 
140  /// provides read-only access to underlying mpq_t
141  const mpq_t& getMpqRef() const;
142 
143  /// provides write access to underlying mpq_t; use with care
144  mpq_t* getMpqPtr_w() const;
145 
146  /// provides write access to underlying mpq_t; use with care
147  mpq_t& getMpqRef_w() const;
148 #endif
149  //@}
150 
151 
152  //**@name Arithmetic operators */
153  //@{
154 
155  /// addition operator
156  Rational operator+(const Rational& r) const;
157 
158  /// addition assignment operator
159  Rational& operator+=(const Rational& r);
160 
161  /// addition operator for doubles
162  Rational operator+(const double& r) const;
163 
164  /// addition assignment operator for doubles
165  Rational& operator+=(const double& r);
166 
167  /// addition operator for ints
168  Rational operator+(const int& r) const;
169 
170  /// addition assignment operator for ints
171  Rational& operator+=(const int& r);
172 
173  /// subtraction operator
174  Rational operator-(const Rational& r) const;
175 
176  /// subtraction assignment operator
177  Rational& operator-=(const Rational& r);
178 
179  /// subtraction operator for doubles
180  Rational operator-(const double& r) const;
181 
182  /// subtraction assignment operator for doubles
183  Rational& operator-=(const double& r);
184 
185  /// subtraction operator for ints
186  Rational operator-(const int& r) const;
187 
188  /// subtraction assignment operator for ints
189  Rational& operator-=(const int& r);
190 
191  /// multiplication operator
192  Rational operator*(const Rational& r) const;
193 
194  /// multiplication assignment operator operator
195  Rational& operator*=(const Rational& r);
196 
197  /// multiplication operator for doubles
198  Rational operator*(const double& r) const;
199 
200  /// multiplication assignment operator for doubles
201  Rational& operator*=(const double& r);
202 
203  /// multiplication operator for ints
204  Rational operator*(const int& r) const;
205 
206  /// multiplication assignment operator for ints
207  Rational& operator*=(const int& r);
208 
209  /// division operator
210  Rational operator/(const Rational& r) const;
211 
212  /// division assignment operator
213  Rational& operator/=(const Rational& r);
214 
215  /// division operator for doubles
216  Rational operator/(const double& r) const;
217 
218  /// division assignment operator for doubles
219  Rational& operator/=(const double& r);
220 
221  /// division operator for ints
222  Rational operator/(const int& r) const;
223 
224  /// division assignment operator for ints
225  Rational& operator/=(const int& r);
226 
227  /// add product of two rationals
228  Rational& addProduct(const Rational& r, const Rational& s);
229 
230  /// subtract product of two rationals
231  Rational& subProduct(const Rational& r, const Rational& s);
232 
233  /// add quotient of two rationals, r divided by s
234  Rational& addQuotient(const Rational& r, const Rational& s);
235 
236  /// subtract quotient of two rationals, r divided by s
237  Rational& subQuotient(const Rational& r, const Rational& s);
238 
239  /// inversion
240  Rational& invert();
241 
242  /// round up to next power of two
243  Rational& powRound();
244 
245  //@}
246 
247 
248  //**@name Methods for checking exactness of doubles */
249  //@{
250 
251  /// checks if \p d is the closest number that can be represented by double
252  bool isNextTo(const double& d);
253 
254  /// checks if \p d is exactly equal to the Rational and if not, if it is one of the two adjacent doubles
255  bool isAdjacentTo(const double& d) const;
256 
257  //@}
258 
259 
260  //**@name Methods for querying size */
261  //@{
262 
263  /// Size in specified base (bit size for base 2)
264  int sizeInBase(const int base = 2) const;
265 
266  //@}
267 
268 
269  //**@name Static methods */
270  //@{
271 
272  /// returns precision of Rational implementation, i.e., number of bits used to store Rational numbers (INT_MAX if exact)
273  static int precision();
274 
275  //@}
276 
277 
278  //**@name Conversion from and to String */
279  //@{
280 
281  /// read Rational from string
282  bool readString(const char* s);
283 
284  friend std::string rationalToString(const Rational& r, const int precision);
285  friend bool readStringRational(const char* s, Rational& value);
286  friend std::ostream& operator<<(std::ostream& os, const Rational& q);
287 
288  //@}
289 
290 
291  //**@name Friends */
292  //@{
293 
294  friend int compareRational(const Rational& r, const Rational& s);
295  friend bool operator!=(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 
302  friend bool operator!=(const Rational& r, const double& s);
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 
309  friend bool operator!=(const double& r, const Rational& s);
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 
316  friend bool operator!=(const Rational& r, const long double& s);
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 
323  friend bool operator!=(const long double& r, const Rational& s);
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 
330  friend Rational operator+(const double& d, const Rational& r);
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 
335  friend bool operator!=(const Rational& r, const int& s);
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 
342  friend bool operator!=(const int& r, const Rational& s);
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 
349  friend Rational operator+(const int& d, const Rational& r);
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 
354  friend Rational spxAbs(const Rational& r);
355  friend int sign(const Rational& r);
356  friend Rational operator-(const Rational& q);
357 
358  //@}
359 };
360 
361 
362 //**@name Parsing and printing */
363 //@{
364 
365 /// convert rational number to string
366 std::string rationalToString(const Rational& r, const int precision = 32);
367 
368 /// read Rational from string
369 bool readStringRational(const char* s, Rational& value);
370 
371 /// print Rational
372 std::ostream& operator<<(std::ostream& os, const Rational& r);
373 
374 //@}
375 
376 
377 //**@name Relational operators */
378 //@{
379 
380 /// comparison operator returning a positive value if r > s, zero if r = s, and a negative value if r < s
381 int compareRational(const Rational& r, const Rational& s);
382 
383 /// equality operator
384 bool operator==(const Rational& r, const Rational& s);
385 
386 /// inequality operator
387 bool operator!=(const Rational& r, const Rational& s);
388 
389 /// less than operator
390 bool operator<(const Rational& r, const Rational& s);
391 
392 /// less than or equal to operator
393 bool operator<=(const Rational& r, const Rational& s);
394 
395 /// greater than operator
396 bool operator>(const Rational& r, const Rational& s);
397 
398 /// greater than or equal to operator
399 bool operator>=(const Rational& r, const Rational& s);
400 
401 /// equality operator for Rational and double
402 bool operator==(const Rational& r, const double& s);
403 
404 /// inequality operator for Rational and double
405 bool operator!=(const Rational& r, const double& s);
406 
407 /// less than operator for Rational and double
408 bool operator<(const Rational& r, const double& s);
409 
410 /// less than or equal to operator for Rational and double
411 bool operator<=(const Rational& r, const double& s);
412 
413 /// greater than operator for Rational and double
414 bool operator>(const Rational& r, const double& s);
415 
416 /// greater than or equal to operator for Rational and double
417 bool operator>=(const Rational& r, const double& s);
418 
419 /// equality operator for double and Rational
420 bool operator==(const double& r, const Rational& s);
421 
422 /// inequality operator for double and Rational
423 bool operator!=(const double& r, const Rational& s);
424 
425 /// less than operator for double and Rational
426 bool operator<(const double& r, const Rational& s);
427 
428 /// less than or equal to operator for double and Rational
429 bool operator<=(const double& r, const Rational& s);
430 
431 /// greater than operator for double and Rational
432 bool operator>(const double& r, const Rational& s);
433 
434 /// greater than or equal to operator for double and Rational
435 bool operator>=(const double& r, const Rational& s);
436 
437 /// equality operator for Rational and long double
438 bool operator==(const Rational& r, const long double& s);
439 
440 /// inequality operator for Rational and long double
441 bool operator!=(const Rational& r, const long double& s);
442 
443 /// less than operator for Rational and long double
444 bool operator<(const Rational& r, const long double& s);
445 
446 /// less than or equal to operator for Rational and long double
447 bool operator<=(const Rational& r, const long double& s);
448 
449 /// greater than operator for Rational and long double
450 bool operator>(const Rational& r, const long double& s);
451 
452 /// greater than or equal to operator for Rational and long double
453 bool operator>=(const Rational& r, const long double& s);
454 
455 /// equality operator for long double and Rational
456 bool operator==(const long double& r, const Rational& s);
457 
458 /// inequality operator for long double and Rational
459 bool operator!=(const long double& r, const Rational& s);
460 
461 /// less than operator for long double and Rational
462 bool operator<(const long double& r, const Rational& s);
463 
464 /// less than or equal to operator for long double and Rational
465 bool operator<=(const long double& r, const Rational& s);
466 
467 /// greater than operator for long double and Rational
468 bool operator>(const long double& r, const Rational& s);
469 
470 /// greater than or equal to operator for long double and Rational
471 bool operator>=(const long double& r, const Rational& s);
472 
473 /// equality operator for Rational and int
474 bool operator==(const Rational& r, const int& s);
475 
476 /// inequality operator for Rational and int
477 bool operator!=(const Rational& r, const int& s);
478 
479 /// less than operator for Rational and int
480 bool operator<(const Rational& r, const int& s);
481 
482 /// less than or equal to operator for Rational and int
483 bool operator<=(const Rational& r, const int& s);
484 
485 /// greater than operator for Rational and int
486 bool operator>(const Rational& r, const int& s);
487 
488 /// greater than or equal to operator for Rational and int
489 bool operator>=(const Rational& r, const int& s);
490 
491 /// equality operator for int and Rational
492 bool operator==(const int& r, const Rational& s);
493 
494 /// inequality operator for int and Rational
495 bool operator!=(const int& r, const Rational& s);
496 
497 /// less than operator for int and Rational
498 bool operator<(const int& r, const Rational& s);
499 
500 /// less than or equal to operator for int and Rational
501 bool operator<=(const int& r, const Rational& s);
502 
503 /// greater than operator for int and Rational
504 bool operator>(const int& r, const Rational& s);
505 
506 /// greater than or equal to operator for int and Rational
507 bool operator>=(const int& r, const Rational& s);
508 
509 //@}
510 
511 
512 //**@name Non-member arithmetic operators and functions */
513 //@{
514 
515 /// addition operator for double and Rational
516 Rational operator+(const double& d, const Rational& r);
517 
518 /// addition operator for double and Rational
519 Rational operator+(const double& d, const Rational& r);
520 
521 /// addition operator for double and Rational
522 Rational operator+(const double& d, const Rational& r);
523 
524 /// addition operator for double and Rational
525 Rational operator+(const double& d, const Rational& r);
526 
527 /// addition operator for int and Rational
528 Rational operator+(const int& d, const Rational& r);
529 
530 /// addition operator for int and Rational
531 Rational operator+(const int& d, const Rational& r);
532 
533 /// addition operator for int and Rational
534 Rational operator+(const int& d, const Rational& r);
535 
536 /// addition operator for int and Rational
537 Rational operator+(const int& d, const Rational& r);
538 
539 /// absolute function
540 Rational spxAbs(const Rational& r);
541 
542 /// Sign function; returns 1 if r > 0, 0 if r = 0, and -1 if r < 0.
543 int sign(const Rational& r);
544 
545 /// Negation.
546 Rational operator-(const Rational& r);
547 
548 /// Total size of rational vector.
549 int totalSizeRational(const Rational* vector, const int length, const int base = 2);
550 
551 /// Size of least common multiple of denominators in rational vector.
552 int dlcmSizeRational(const Rational* vector, const int length, const int base = 2);
553 
554 /// Size of largest denominator in rational vector.
555 int dmaxSizeRational(const Rational* vector, const int length, const int base = 2);
556 
557 //@}
558 
559 } // namespace soplex
560 
561 #endif // _RATIONAL_H_
Rational & addProduct(const Rational &r, const Rational &s)
add product of two rationals
Definition: rational.cpp:3549
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:3611
bool isNextTo(const double &d)
checks if d is the closest number that can be represented by double
Definition: rational.cpp:3603
Generic Real linked list.
friend Rational spxAbs(const Rational &r)
Absolute.
Definition: rational.cpp:4102
friend std::string rationalToString(const Rational &r, const int precision)
convert rational number to string
Definition: rational.cpp:3645
int dlcmSizeRational(const Rational *vector, const int length, const int base)
Size of least common multiple of denominators in rational vector.
Definition: rational.cpp:4150
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:3701
Rational operator*(const Rational &r) const
multiplication operator
Definition: rational.cpp:3435
~Rational()
destructor
Definition: rational.cpp:3232
int sizeInBase(const int base=2) const
Size in specified base (bit size for base 2)
Definition: rational.cpp:3619
friend bool operator<=(const Rational &r, const Rational &s)
less than or equal to operator
Definition: rational.cpp:3709
Rational operator-(const Rational &r) const
subtraction operator
Definition: rational.cpp:3378
friend bool operator!=(const Rational &r, const Rational &s)
inequality operator
Definition: rational.cpp:3693
Rational & subProduct(const Rational &r, const Rational &s)
subtract product of two rationals
Definition: rational.cpp:3558
Rational & invert()
inversion
Definition: rational.cpp:3585
friend bool operator>=(const Rational &r, const Rational &s)
greater than or equal to operator
Definition: rational.cpp:3725
int totalSizeRational(const Rational *vector, const int length, const int base)
Total size of rational vector.
Definition: rational.cpp:4133
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:3260
Rational & operator/=(const Rational &r)
division assignment operator
Definition: rational.cpp:3502
Rational & operator-=(const Rational &r)
subtraction assignment operator
Definition: rational.cpp:3388
static void freeListMem()
frees the unused rational elements in the memory list
Definition: rational.cpp:3252
int dmaxSizeRational(const Rational *vector, const int length, const int base)
Size of largest denominator in rational vector.
Definition: rational.cpp:4162
Rational & operator*=(const Rational &r)
multiplication assignment operator operator
Definition: rational.cpp:3445
bool readString(const char *s)
read Rational from string
Definition: rational.cpp:3636
static int precision()
returns precision of Rational implementation, i.e., number of bits used to store Rational numbers (IN...
Definition: rational.cpp:3628
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:4115
Defines the "Pimpl"-class Private.
Definition: rational.cpp:3112
friend std::ostream & operator<<(std::ostream &os, const Rational &q)
print Rational
Definition: rational.cpp:3663
static void enableListMem()
enables list memory
Definition: rational.cpp:3240
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:3685
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:3672
Rational operator+(const Rational &r) const
addition operator
Definition: rational.cpp:3321
Everything should be within this namespace.
Rational operator/(const Rational &r) const
division operator
Definition: rational.cpp:3492
friend bool readStringRational(const char *s, Rational &value)
read Rational from string
Definition: rational.cpp:3655
Rational & operator=(const Rational &)
assignment operator
Definition: rational.cpp:3268
Rational & subQuotient(const Rational &r, const Rational &s)
subtract quotient of two rationals, r divided by s
Definition: rational.cpp:3576
friend bool operator>(const Rational &r, const Rational &s)
greater than operator
Definition: rational.cpp:3717
Rational()
default constructor
Definition: rational.cpp:3182
#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:3567
Rational & operator+=(const Rational &r)
addition assignment operator
Definition: rational.cpp:3331
Rational & powRound()
round up to next power of two
Definition: rational.cpp:3594