Scippy

SoPlex

Sequential object-oriented simPlex

rational.h
Go to the documentation of this file.
1 
2 /* ----------------------------------------------------------------------
3  * This file is autogenerated from the file multiprecision.hpp.in during
4  * the cmake configuration of your project. If you need to make changes
5  * edit the original file.
6  * ----------------------------------------------------------------------
7  */
8 #ifndef __SOPLEX_MULTIPRECISION_HPP_
9 #define __SOPLEX_MULTIPRECISION_HPP_
10 #include <numeric>
11 #include <vector>
12 #include <string>
13 #include "soplex/spxdefines.h"
14 
15 #ifdef SOPLEX_WITH_GMP
16 #include <gmp.h>
17 #endif
18 
19 #ifdef SOPLEX_WITH_BOOST
20 #include <boost/multiprecision/number.hpp>
21 
22 #ifdef SOPLEX_WITH_GMP
23 #include <boost/multiprecision/gmp.hpp>
24 
25 namespace soplex
26 {
27 
28 using namespace boost::multiprecision;
29 using Rational = number<gmp_rational, et_off>;
30 using Integer = number<gmp_int, et_off>;
31 inline void SpxLcm(Integer& result, Integer a, Integer b)
32 {
33  mpz_lcm(result.backend().data(), a.backend().data(), b.backend().data());
34 }
35 inline void SpxGcd(Integer& result, Integer a, Integer b)
36 {
37  mpz_gcd(result.backend().data(), a.backend().data(), b.backend().data());
38 }
39 
40 } // namespace soplex
41 #else
42 #include <boost/multiprecision/cpp_int.hpp>
43 #include <boost/multiprecision/detail/default_ops.hpp>
44 
45 namespace soplex
46 {
47 
48 using namespace boost::multiprecision;
49 using Rational = cpp_rational;
50 using Integer = cpp_int;
51 inline void SpxLcm(Integer& result, Integer a, Integer b)
52 {
53  result = boost::multiprecision::lcm(a, b);
54 }
55 inline void SpxGcd(Integer& result, Integer a, Integer b)
56 {
57  result = boost::multiprecision::gcd(a, b);
58 }
59 
60 } // namespace soplex
61 #endif
62 
63 namespace soplex
64 {
65 
66 inline void printRational(Rational r)
67 {
68  std::cout << r << std::endl;
69 }
70 
71 inline void printInteger(Integer r)
72 {
73  std::cout << r << std::endl;
74 }
75 inline bool isAdjacentTo(const Rational& r, const double& d)
76 {
77  double x = (double) r;
78  double a;
79  double b;
80  Rational tmp = x;
81 
82  // the rational value is representable in double precision
83  if(tmp == r)
84  return true;
85  // the rounded value is smaller than the rational value
86  else if(tmp < r)
87  {
88  a = x;
89  b = (double)nextafter(a, 1e100);
90  }
91  // the rounded value is larger than the rational value
92  else
93  {
94  b = x;
95  a = (double)nextafter(b, -1e100);
96  }
97 
98  return ((a == d) || (b == d));
99 }
100 
101 inline void invert(Rational& r)
102 {
103  r = Rational(denominator(r), numerator(r));
104 }
105 
106 /// round up to next power of two
107 inline void powRound(Rational& r)
108 {
109  Integer roundval;
110  Integer den;
111  Integer num;
112 
113  num = numerator(r);
114  den = denominator(r);
115  roundval = num / den;
116 
117  size_t binlog = roundval == 0 ? 1 : msb(roundval) + 1;
118  Integer base = 2;
119 
120  roundval = boost::multiprecision::pow(base, (unsigned int)binlog);
121 
122  r = roundval;
123 }
124 
125 /// returns the order of magnitude of the given rational
127 {
128  if(numerator(r) == 0 || (int) log10((double)numerator(r)) == log10((double)denominator(r)))
129  return 0;
130  else
131  return (int) log10((double)numerator(r)) - (int) log10((double)denominator(r));
132 }
133 
134 /* find substring, ignore case */
135 static
136 std::string::const_iterator findSubStringIC(const std::string& substr, const std::string& str)
137 {
138  auto it = std::search(
139  str.begin(), str.end(),
140  substr.begin(), substr.end(),
141  [](char ch1, char ch2)
142  {
143  return std::toupper(ch1) == std::toupper(ch2);
144  }
145  );
146  return it;
147 }
148 
149 inline Rational ratFromString(const char* desc)
150 {
151  Rational res;
152 
153  if(0 == strcmp(desc, "inf"))
154  {
155  res = 1e100;
156  }
157  else if(0 == strcmp(desc, "-inf"))
158  {
159  res = -1e100;
160  }
161  else
162  {
163  std::string s(desc);
164 
165  /* case 1: string is given in nom/den format */
166  if(s.find_first_of(".Ee") == std::string::npos)
167  {
168  if(s[0] == '+')
169  res = Rational(desc + 1);
170  else
171  res = Rational(desc);
172  }
173  /* case 2: string is given as base-10 decimal number */
174  else
175  {
176  std::string::const_iterator it = findSubStringIC("e", s);
177  int mult = 0;
178 
179  if(it != s.end())
180  {
181  int exponentidx = int(it - s.begin());
182  mult = std::stoi(s.substr(exponentidx + 1, s.length()));
183  s = s.substr(0, exponentidx);
184  }
185 
186  // std::cout << s << std::endl;
187  if(s[0] == '.')
188  s.insert(0, "0");
189 
190  size_t pos = s.find('.');
191 
192  // if s contains a ., convert it to a rational
193  if(pos != std::string::npos)
194  {
195  size_t exp = s.length() - 1 - pos;
196  std::string den("1");
197 
198  for(size_t i = 0; i < exp; ++i)
199  den.append("0");
200 
201  s.erase(pos, 1);
202  assert(std::all_of(s.begin() + 1, s.end(), ::isdigit));
203 
204  // remove padding 0s
205  if(s[0] == '-')
206  s.erase(1, SOPLEX_MIN(s.substr(1).find_first_not_of('0'), s.size() - 1));
207  else
208  s.erase(0, SOPLEX_MIN(s.find_first_not_of('0'), s.size() - 1));
209 
210  s.append("/");
211  s.append(den);
212  }
213 
214  if(s[0] == '+')
215  res = Rational(s.substr(1));
216  else
217  res = Rational(s);
218 
219  res *= pow(10, mult);
220  }
221  }
222 
223  return res;
224 }
225 
226 } // namespace soplex
227 #else
228 
229 #ifndef SOPLEX_WITH_GMP
230 using mpq_t = char;
231 #endif
232 
233 using Integer = int;
234 // this is a placeholder class to ensure compilation when boost ist not linked. Rationals need BOOST in order to function.
235 // coverity[missing_move_assign]
236 class Rational
237 {
238 
239 public:
240 
241  ///@name Construction and destruction
242  ///@{
243 
244  inline void rationalErrorMessage() const
245  {
246  SPX_MSG_ERROR(std::cerr << "Using rational methods without linking boost is not supported" <<
247  std::endl;)
248  };
249 
250  /// default constructor
251  inline Rational()
252  {
253  };
254  /// copy constructor
255  inline Rational(const Rational& r)
256  {
257  };
258  /// constructor from long double
259  inline Rational(const long double& r)
260  {
261  };
262  /// constructor from double
263  inline Rational(const double& r)
264  {
265  };
266  ///constructor from int
267  inline Rational(const int& i)
268  {
269  };
270  /// constructor from Integer
271  inline Rational(const Integer& num, const Integer& den)
272  {
273  };
274  /// constructor from mpq_t (GMP only)
275  inline Rational(const mpq_t& q)
276  {
277  };
278 #ifdef SOPLEX_WITH_BOOST
279  // constructor from boost number
280  inline template <typename T, boost::multiprecision::expression_template_option eto>
281  Rational(const boost::multiprecision::number<T, eto>& q)
282  {
283  };
284 #endif
285  /// destructor
286  inline ~Rational()
287  {
288  };
289 
290  /// assignment operator
291  inline Rational& operator=(const Rational&)
292  {
293  return *this;
294  };
295  /// assignment operator from long double
296  inline Rational& operator=(const long double& r)
297  {
298  return *this;
299  };
300  /// assignment operator from double
301  inline Rational& operator=(const double& r)
302  {
303  return *this;
304  };
305  /// assignment operator from int
306  inline Rational& operator=(const int& i)
307  {
308  return *this;
309  };
310  /// assignment operator from mpq_t
311  inline Rational& operator=(const mpq_t& q)
312  {
313  return *this;
314  };
315 
316  inline void assign(const Rational&)
317  {
318  rationalErrorMessage();
319  };
320  inline void assign(const long double& r)
321  {
322  rationalErrorMessage();
323  };
324  inline void assign(const double& r)
325  {
326  rationalErrorMessage();
327  };
328  inline void assign(const int& i)
329  {
330  rationalErrorMessage();
331  };
332 
333  ///@name Typecasts
334  ///@{
335 
336  inline operator double() const
337  {
338  return 0;
339  };
340  inline operator long double() const
341  {
342  return 0;
343  };
344  inline operator float() const
345  {
346  return 0;
347  };
348 #ifdef SOPLEX_WITH_BOOST
349 #ifndef SOPLEX_WITH_CPPMPF
350  // Operator to typecast Rational to one of the Boost Number types
351  inline template <typename T, boost::multiprecision::expression_template_option eto>
352  operator boost::multiprecision::number<T, eto>() const
353  {
354  rationalErrorMessage();
355  return 0;
356  };
357 #else
358  // Operator to typecast Rational to one of the Boost Number types
359  inline template <unsigned bits, boost::multiprecision::expression_template_option eto>
360  operator boost::multiprecision::number<boost::multiprecision::backends::cpp_dec_float<bits>, eto>()
361  const
362  {
363  rationalErrorMessage();
364  return 0;
365  };
366 #endif
367 #endif
368 
369  ///@name Typecasts
370  ///@{
371 
372  ///@}
373 
374 
375  ///@name Arithmetic operators
376  ///@{
377 
378  /// addition operator
379  inline Rational operator+(const Rational& r) const
380  {
381  rationalErrorMessage();
382  return *this;
383  }
384  /// addition assignment operator
385  inline Rational operator+=(const Rational& r)
386  {
387  rationalErrorMessage();
388  return *this;
389  }
390  /// addition operator for doubles
391  inline Rational operator+(const double& r) const
392  {
393  rationalErrorMessage();
394  return *this;
395  }
396  /// addition assignment operator for doubles
397  inline Rational operator+=(const double& r)
398  {
399  rationalErrorMessage();
400  return *this;
401  }
402  /// addition operator for ints
403  inline Rational operator+(const int& r) const
404  {
405  rationalErrorMessage();
406  return *this;
407  }
408  /// addition assignment operator for ints
409  inline Rational operator+=(const int& r)
410  {
411  rationalErrorMessage();
412  return *this;
413  }
414  /// subtraction operator
415  inline Rational operator-(const Rational& r) const
416  {
417  rationalErrorMessage();
418  return *this;
419  }
420  /// subtraction assignment operator
421  inline Rational operator-=(const Rational& r)
422  {
423  rationalErrorMessage();
424  return *this;
425  }
426  /// subtraction operator for doubles
427  inline Rational operator-(const double& r) const
428  {
429  rationalErrorMessage();
430  return *this;
431  }
432  /// subtraction assignment operator for doubles
433  inline Rational operator-=(const double& r)
434  {
435  rationalErrorMessage();
436  return *this;
437  }
438  /// subtraction operator for ints
439  inline Rational operator-(const int& r) const
440  {
441  rationalErrorMessage();
442  return *this;
443  }
444  /// subtraction assignment operator for ints
445  inline Rational operator-=(const int& r)
446  {
447  rationalErrorMessage();
448  return *this;
449  }
450  /// multiplication operator
451  inline Rational operator*(const Rational& r) const
452  {
453  rationalErrorMessage();
454  return *this;
455  }
456  /// multiplication assignment operator operator
457  inline Rational operator*=(const Rational& r)
458  {
459  rationalErrorMessage();
460  return *this;
461  }
462  /// multiplication operator for doubles
463  inline Rational operator*(const double& r) const
464  {
465  rationalErrorMessage();
466  return *this;
467  }
468  /// multiplication assignment operator for doubles
469  inline Rational operator*=(const double& r)
470  {
471  rationalErrorMessage();
472  return *this;
473  }
474  /// multiplication operator for ints
475  inline Rational operator*(const int& r) const
476  {
477  rationalErrorMessage();
478  return *this;
479  }
480  /// multiplication assignment operator for ints
481  inline Rational operator*=(const int& r)
482  {
483  rationalErrorMessage();
484  return *this;
485  }
486  /// division operator
487  inline Rational operator/(const Rational& r) const
488  {
489  rationalErrorMessage();
490  return *this;
491  }
492  /// division assignment operator
493  inline Rational operator/=(const Rational& r)
494  {
495  rationalErrorMessage();
496  return *this;
497  }
498  /// division operator for doubles
499  inline Rational operator/(const double& r) const
500  {
501  rationalErrorMessage();
502  return *this;
503  }
504  /// division assignment operator for doubles
505  inline Rational operator/=(const double& r)
506  {
507  rationalErrorMessage();
508  return *this;
509  }
510  /// division operator for ints
511  inline Rational operator/(const int& r) const
512  {
513  rationalErrorMessage();
514  return *this;
515  }
516  /// division assignment operator for ints
517  inline Rational operator/=(const int& r)
518  {
519  rationalErrorMessage();
520  return *this;
521  }
522  /// add product of two rationals
523  Rational& addProduct(const Rational& r, const Rational& s)
524  {
525  rationalErrorMessage();
526  return *this;
527  }
528 
529  /// subtract product of two rationals
530  Rational& subProduct(const Rational& r, const Rational& s)
531  {
532  rationalErrorMessage();
533  return *this;
534  }
535 
536  /// add quotient of two rationals, r divided by s
537  Rational& addQuotient(const Rational& r, const Rational& s)
538  {
539  rationalErrorMessage();
540  return *this;
541  }
542 
543  /// subtract quotient of two rationals, r divided by s
544  Rational& subQuotient(const Rational& r, const Rational& s)
545  {
546  rationalErrorMessage();
547  return *this;
548  }
549 
550  ///@}
551 
552 
553  ///@name Methods for checking exactness of doubles
554  ///@{
555 
556  /// checks if \p d is exactly equal to the Rational and if not, if it is one of the two adjacent doubles
557  inline bool isAdjacentTo(const double& d) const
558  {
559  rationalErrorMessage();
560  return false;
561  };
562 
563  ///@}
564 
565 
566  ///@name Methods for querying size
567  ///@{
568 
569  /// Size in specified base (bit size for base 2)
570  int sizeInBase(const int base = 2) const
571  {
572  rationalErrorMessage();
573  return 0;
574  };
575 
576  ///@}
577 
578 
579  ///@name Conversion from and to String
580  ///@{
581  inline friend std::ostream& operator<<(std::ostream& os, const Rational& r)
582  {
583  r.rationalErrorMessage();
584  return os;
585  };
586  inline std::string str() const
587  {
588  this->rationalErrorMessage();
589  return std::string("");
590  };
591  ///@}
592 
593  ///@name Friends
594  ///@{
595 
596  inline friend int compareRational(const Rational& r, const Rational& s)
597  {
598  r.rationalErrorMessage();
599  return 0;
600  };
601  inline friend bool operator!=(const Rational& r, const Rational& s)
602  {
603  r.rationalErrorMessage();
604  return false;
605  };
606  inline friend bool operator==(const Rational& r, const Rational& s)
607  {
608  r.rationalErrorMessage();
609  return false;
610  };
611  inline friend bool operator<(const Rational& r, const Rational& s)
612  {
613  r.rationalErrorMessage();
614  return false;
615  };
616  inline friend bool operator<=(const Rational& r, const Rational& s)
617  {
618  r.rationalErrorMessage();
619  return false;
620  };
621  inline friend bool operator>(const Rational& r, const Rational& s)
622  {
623  r.rationalErrorMessage();
624  return false;
625  };
626  inline friend bool operator>=(const Rational& r, const Rational& s)
627  {
628  r.rationalErrorMessage();
629  return false;
630  };
631 
632  inline friend bool operator!=(const Rational& r, const double& s)
633  {
634  r.rationalErrorMessage();
635  return false;
636  };
637  inline friend bool operator==(const Rational& r, const double& s)
638  {
639  r.rationalErrorMessage();
640  return false;
641  };
642  inline friend bool operator<(const Rational& r, const double& s)
643  {
644  r.rationalErrorMessage();
645  return false;
646  };
647  inline friend bool operator<=(const Rational& r, const double& s)
648  {
649  r.rationalErrorMessage();
650  return false;
651  };
652  inline friend bool operator>(const Rational& r, const double& s)
653  {
654  r.rationalErrorMessage();
655  return false;
656  };
657  inline friend bool operator>=(const Rational& r, const double& s)
658  {
659  r.rationalErrorMessage();
660  return false;
661  };
662 
663  inline friend bool operator!=(const double& r, const Rational& s)
664  {
665  s.rationalErrorMessage();
666  return false;
667  };
668  inline friend bool operator==(const double& r, const Rational& s)
669  {
670  s.rationalErrorMessage();
671  return false;
672  };
673  inline friend bool operator<(const double& r, const Rational& s)
674  {
675  s.rationalErrorMessage();
676  return false;
677  };
678  inline friend bool operator<=(const double& r, const Rational& s)
679  {
680  s.rationalErrorMessage();
681  return false;
682  };
683  inline friend bool operator>(const double& r, const Rational& s)
684  {
685  s.rationalErrorMessage();
686  return false;
687  };
688  inline friend bool operator>=(const double& r, const Rational& s)
689  {
690  s.rationalErrorMessage();
691  return false;
692  };
693 
694  inline friend bool operator!=(const Rational& r, const long double& s)
695  {
696  r.rationalErrorMessage();
697  return false;
698  };
699  inline friend bool operator==(const Rational& r, const long double& s)
700  {
701  r.rationalErrorMessage();
702  return false;
703  };
704  inline friend bool operator<(const Rational& r, const long double& s)
705  {
706  r.rationalErrorMessage();
707  return false;
708  };
709  inline friend bool operator<=(const Rational& r, const long double& s)
710  {
711  r.rationalErrorMessage();
712  return false;
713  };
714  inline friend bool operator>(const Rational& r, const long double& s)
715  {
716  r.rationalErrorMessage();
717  return false;
718  };
719  inline friend bool operator>=(const Rational& r, const long double& s)
720  {
721  r.rationalErrorMessage();
722  return false;
723  };
724 
725  inline friend bool operator!=(const long double& r, const Rational& s)
726  {
727  s.rationalErrorMessage();
728  return false;
729  };
730  inline friend bool operator==(const long double& r, const Rational& s)
731  {
732  s.rationalErrorMessage();
733  return false;
734  };
735  inline friend bool operator<(const long double& r, const Rational& s)
736  {
737  s.rationalErrorMessage();
738  return false;
739  };
740  inline friend bool operator<=(const long double& r, const Rational& s)
741  {
742  s.rationalErrorMessage();
743  return false;
744  };
745  inline friend bool operator>(const long double& r, const Rational& s)
746  {
747  s.rationalErrorMessage();
748  return false;
749  };
750  inline friend bool operator>=(const long double& r, const Rational& s)
751  {
752  s.rationalErrorMessage();
753  return false;
754  };
755 
756  inline friend bool operator!=(const Rational& r, const float& s)
757  {
758  r.rationalErrorMessage();
759  return false;
760  };
761  inline friend bool operator==(const Rational& r, const float& s)
762  {
763  r.rationalErrorMessage();
764  return false;
765  };
766  inline friend bool operator<(const Rational& r, const float& s)
767  {
768  r.rationalErrorMessage();
769  return false;
770  };
771  inline friend bool operator<=(const Rational& r, const float& s)
772  {
773  r.rationalErrorMessage();
774  return false;
775  };
776  inline friend bool operator>(const Rational& r, const float& s)
777  {
778  r.rationalErrorMessage();
779  return false;
780  };
781  inline friend bool operator>=(const Rational& r, const float& s)
782  {
783  r.rationalErrorMessage();
784  return false;
785  };
786 
787  inline friend bool operator!=(const float& r, const Rational& s)
788  {
789  s.rationalErrorMessage();
790  return false;
791  };
792  inline friend bool operator==(const float& r, const Rational& s)
793  {
794  s.rationalErrorMessage();
795  return false;
796  };
797  inline friend bool operator<(const float& r, const Rational& s)
798  {
799  s.rationalErrorMessage();
800  return false;
801  };
802  inline friend bool operator<=(const float& r, const Rational& s)
803  {
804  s.rationalErrorMessage();
805  return false;
806  };
807  inline friend bool operator>(const float& r, const Rational& s)
808  {
809  s.rationalErrorMessage();
810  return false;
811  };
812  inline friend bool operator>=(const float& r, const Rational& s)
813  {
814  s.rationalErrorMessage();
815  return false;
816  };
817 
818  inline friend Rational operator+(const double& d, const Rational& r)
819  {
820  r.rationalErrorMessage();
821  return r;
822  };
823  inline friend Rational operator-(const double& d, const Rational& r)
824  {
825  r.rationalErrorMessage();
826  return r;
827  };
828  inline friend Rational operator*(const double& d, const Rational& r)
829  {
830  r.rationalErrorMessage();
831  return r;
832  };
833  inline friend Rational operator/(const double& d, const Rational& r)
834  {
835  r.rationalErrorMessage();
836  return r;
837  };
838 
839  inline friend bool operator!=(const Rational& r, const int& s)
840  {
841  r.rationalErrorMessage();
842  return false;
843  };
844  inline friend bool operator==(const Rational& r, const int& s)
845  {
846  r.rationalErrorMessage();
847  return false;
848  };
849  inline friend bool operator<(const Rational& r, const int& s)
850  {
851  r.rationalErrorMessage();
852  return false;
853  };
854  inline friend bool operator<=(const Rational& r, const int& s)
855  {
856  r.rationalErrorMessage();
857  return false;
858  };
859  inline friend bool operator>(const Rational& r, const int& s)
860  {
861  r.rationalErrorMessage();
862  return false;
863  };
864  inline friend bool operator>=(const Rational& r, const int& s)
865  {
866  r.rationalErrorMessage();
867  return false;
868  };
869 
870  inline friend bool operator!=(const int& r, const Rational& s)
871  {
872  s.rationalErrorMessage();
873  return false;
874  };
875  inline friend bool operator==(const int& r, const Rational& s)
876  {
877  s.rationalErrorMessage();
878  return false;
879  };
880  inline friend bool operator<(const int& r, const Rational& s)
881  {
882  s.rationalErrorMessage();
883  return false;
884  };
885  inline friend bool operator<=(const int& r, const Rational& s)
886  {
887  s.rationalErrorMessage();
888  return false;
889  };
890  inline friend bool operator>(const int& r, const Rational& s)
891  {
892  s.rationalErrorMessage();
893  return false;
894  };
895  inline friend bool operator>=(const int& r, const Rational& s)
896  {
897  s.rationalErrorMessage();
898  return false;
899  };
900 
901  inline friend Rational operator+(const int& d, const Rational& r)
902  {
903  r.rationalErrorMessage();
904  return r;
905  };
906  inline friend Rational operator-(const int& d, const Rational& r)
907  {
908  r.rationalErrorMessage();
909  return r;
910  };
911  inline friend Rational operator*(const int& d, const Rational& r)
912  {
913  r.rationalErrorMessage();
914  return r;
915  };
916  inline friend Rational operator/(const int& d, const Rational& r)
917  {
918  r.rationalErrorMessage();
919  return r;
920  };
921 
922  inline friend Rational spxAbs(const Rational& r)
923  {
924  r.rationalErrorMessage();
925  return r;
926  };
927  inline friend int sign(const Rational& r)
928  {
929  r.rationalErrorMessage();
930  return 0;
931  };
932  inline friend Rational operator-(const Rational& q)
933  {
934  q.rationalErrorMessage();
935  return q;
936  };///@name Construction and destruction
937  ///@{
938 };
939 
940 inline Integer numerator(const Rational& r)
941 {
942  r.rationalErrorMessage();
943  return 0;
944 }
945 inline Integer denominator(const Rational& r)
946 {
947  r.rationalErrorMessage();
948  return 0;
949 }
950 inline Rational ratFromString(const char* desc)
951 {
952  return Rational();
953 }
954 inline int orderOfMagnitude(Rational& r)
955 {
956  r.rationalErrorMessage();
957  return 0;
958 }
959 inline void SpxLcm(Integer& result, Integer a, Integer b) {}
960 inline void SpxGcd(Integer& result, Integer a, Integer b) {}
961 inline void divide_qr(Integer& result, Integer& result2, Integer a, Integer b) {}
962 inline void invert(Rational& r)
963 {
964  r.rationalErrorMessage();
965 }
966 inline void powRound(Rational& r)
967 {
968  r.rationalErrorMessage();
969 }
970 #endif
971 
972 namespace soplex
973 {
974 
975 /// Size in specified base (bit size for base 2)
976 inline int sizeInBase(const Rational R, const int base)
977 {
978 #ifndef SOPLEX_WITH_BOOST
979  SPX_MSG_ERROR(std::cerr << "ERROR: rational solve without Boost not defined!" << std::endl;)
980  return 0;
981 #else
982 
983  if(R == Rational(0))
984  return 3;
985 
986  Integer num = numerator(R);
987  Integer den = denominator(R);
988  size_t numsize, densize;
989 
990 #ifdef SOPLEX_WITH_GMP
991  densize = mpz_sizeinbase(den.backend().data(), base);
992  numsize = mpz_sizeinbase(num.backend().data(), base);
993 #else
994 
995  if(base != 2)
996  {
997  densize = (size_t)(log2(den.convert_to<double>()) / log2(double(base))) + 1;
998  numsize = (size_t)(log2(num.convert_to<double>()) / log2(double(base))) + 1;
999  }
1000  else
1001  {
1002  densize = msb(den) + 1;
1003  numsize = msb(num) + 1;
1004  }
1005 
1006 #endif
1007 
1008  return (int)(densize + numsize);
1009 #endif
1010 }
1011 /// Total size of rational vector.
1012 inline int totalSizeRational(const Rational* vector, const int length, const int base)
1013 {
1014  assert(vector != 0);
1015  assert(length >= 0);
1016  assert(base >= 0);
1017 
1018  int size = 0;
1019 
1020  for(int i = 0; i < length; i++)
1021  size += sizeInBase(vector[i], base);
1022 
1023  return size;
1024 }
1025 
1026 /// Size of least common multiple of denominators in rational vector.
1027 inline int dlcmSizeRational(const Rational* vector, const int length, const int base)
1028 {
1029  assert(vector != 0);
1030  assert(length >= 0);
1031 
1032 #ifndef SOPLEX_WITH_BOOST
1033  SPX_MSG_ERROR(std::cerr << "ERROR: rational solve without Boost not defined!" << std::endl;)
1034  return 0;
1035 #else
1036 
1037  Integer lcm = 1;
1038 
1039  for(int i = 0; i < length; i++)
1040  SpxLcm(lcm, lcm, denominator(vector[i]));
1041 
1042  int size = sizeInBase(Rational(lcm), base) + 1;
1043 
1044  return size;
1045 #endif
1046 }
1047 
1048 /// Size of largest denominator in rational vector.
1049 inline int dmaxSizeRational(const Rational* vector, const int length, const int base)
1050 {
1051  assert(vector != 0);
1052  assert(length >= 0);
1053 #ifndef SOPLEX_WITH_BOOST
1054  SPX_MSG_ERROR(std::cerr << "ERROR: rational solve without Boost not defined!" << std::endl;)
1055  return 0;
1056 #else
1057 
1058  size_t dmax = 0;
1059 
1060  for(int i = 0; i < length; i++)
1061  {
1062  size_t dsize = sizeInBase(Rational(denominator(vector[i])), base) + 1;
1063 
1064  if(dsize > dmax)
1065  dmax = dsize;
1066  }
1067 
1068  return (int)dmax;
1069 #endif
1070 }
1071 
1072 } // namespace soplex
1073 #endif
1074 //}
bool operator==(fp x, fp y)
Definition: format-inl.h:456
void printInteger(Integer r)
Definition: rational.h:71
number< gmp_rational, et_off > Rational
Definition: rational.h:29
void powRound(Rational &r)
round up to next power of two
Definition: rational.h:107
bool isAdjacentTo(const Rational &r, const double &d)
Definition: rational.h:75
#define SOPLEX_MIN(x, y)
Definition: spxdefines.h:298
VectorBase< R > operator-(const SVectorBase< R > &v, const VectorBase< R > &w)
Subtraction.
Definition: basevectors.h:1162
#define SPX_MSG_ERROR(x)
Prints out message x if the verbosity level is at least SPxOut::ERROR.
Definition: spxdefines.h:163
Rational ratFromString(const char *desc)
Definition: rational.h:149
DSVectorBase< R > operator*(const SVectorBase< R > &v, R x)
Scaling.
Definition: basevectors.h:1180
number< gmp_int, et_off > Integer
Definition: rational.h:30
std::ostream & operator<<(std::ostream &s, const VectorBase< R > &vec)
Output operator.
Definition: basevectors.h:1143
void SpxLcm(Integer &result, Integer a, Integer b)
Definition: rational.h:31
static std::string::const_iterator findSubStringIC(const std::string &substr, const std::string &str)
Definition: rational.h:136
int orderOfMagnitude(Rational &r)
returns the order of magnitude of the given rational
Definition: rational.h:126
Definition: format.h:1009
void invert(Rational &r)
Definition: rational.h:101
Debugging, floating point type and parameter definitions.
Everything should be within this namespace.
int totalSizeRational(const Rational *vector, const int length, const int base)
Total size of rational vector.
Definition: rational.h:1012
int sizeInBase(const Rational R, const int base)
Size in specified base (bit size for base 2)
Definition: rational.h:976
R spxAbs(R a)
Definition: spxdefines.h:393
void printRational(Rational r)
Definition: rational.h:66
int dmaxSizeRational(const Rational *vector, const int length, const int base)
Size of largest denominator in rational vector.
Definition: rational.h:1049
int dlcmSizeRational(const Rational *vector, const int length, const int base)
Size of least common multiple of denominators in rational vector.
Definition: rational.h:1027
void SpxGcd(Integer &result, Integer a, Integer b)
Definition: rational.h:35