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