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