Developer documentation

GMP.hpp
Go to the documentation of this file.
1 #ifndef mmx_gmp_kernel_hpp
2 #define mmx_gmp_kernel_hpp
9 #include <realroot/Interval.hpp>
10 
11 namespace mmx {
12 
25 struct GMP {
27  typedef double ieee;
35 };
36 
37  typedef GMP::rational QQ;
38  typedef GMP::integer ZZ;
39  typedef GMP::floating RR;
40 
43 
44  // pdobrowolski: should these go to a separate texp_* file?
45  template<class type> struct is_extended { typedef false_t T; };
46  template<> struct is_extended<EQQ> { typedef true_t T; };
47  template<> struct is_extended<EZZ> { typedef true_t T; };
48 
49  namespace texp {
50  template<class R> struct kernelof_;
51  template<> struct kernelof_<GMP::ieee> { typedef GMP T; };
52  template<> struct kernelof_<GMP::integer> { typedef GMP T; };
53  template<> struct kernelof_<GMP::rational> { typedef GMP T; };
54  template<> struct kernelof_<GMP::floating> { typedef GMP T; };
55 
56  template<> struct kernelof_<GMP::extended_integer> { typedef GMP T; };
57  template<> struct kernelof_<GMP::extended_rational> { typedef GMP T; };
58  }
59 
60  inline int sign(const QQ& a) { return mpq_sgn(&a.rep()); }
61 
62  inline bool with_plus_sign(const ZZ & c) { return (c>0);}
63  inline bool with_plus_sign(const QQ & c) { return (c>0);}
64  inline bool with_plus_sign(const RR & c) { return (c>0);}
65 
66  inline double to_double(const RR& a) { return mpf_get_d(&a.rep()); }
67  inline double as_double(const RR& a) { return mpf_get_d(&a.rep()); }
68 
69  inline double to_XT(const ZZ& a) { return as_double(a); }
70  inline double to_XT(const QQ& a) { return as_double(a); }
71  inline double to_XT(const RR& a) { return as_double(a); }
72 
73 //----------------------------------------------------------------------
77 inline void bit_precision(unsigned long l) {
78  mpf_set_default_prec(l);
79 }
80 
81 //----------------------------------------------------------------------
85 inline void
86 bit_precision( RR b, unsigned long l){
87  mpf_set_prec(&b.rep(),l);
88 }
89 
90 inline double gcd (const double&, const double&) { return 1; }
91 
92 
93 //======================================================================
94 namespace let
95 {
96  inline void assign(ZZ& x, const QQ& r) { x= numerator(r)/denominator(r);}
97  inline void assign(QQ& x, const ZZ& r) { mpq_set_z(&x.rep(), &r.rep());}
98  inline void assign(RR& r, const ZZ& x) { mpf_set_z(&r.rep(),&x.rep());}
99 
100  inline void assign(mpz_t r, const ZZ& x) { mpz_set(r, &x.rep());}
101  inline void assign(mpf_t r, const ZZ& x) { mpf_set_z(r,&x.rep());}
102  inline void assign(mpf_t r, const RR& x) { mpf_set(r,&x.rep());}
103  inline void assign(mpf_t r, const QQ& x)
104  {
105  // RR u=RR(numerator(x))/RR(denominator(x)); mpf_set(r,&u.rep());
106  mpf_t n, d; mpf_init (n); mpf_init (d);
107  assign (n, numerator (x));
108  assign (d, denominator (x));
109  mpf_div (r, n, d);
110  mpf_clear (n);
111  mpf_clear (d);
112  }
113 
114 }
115 
116 template<class T, class U> struct as_helper;
117 
118 template<> struct as_helper<QQ,ZZ> { static inline QQ cv(const ZZ& x)
119  { QQ r;mpq_set_z(&r.rep(),&x.rep()); return r; } };
120 
121 template<> struct as_helper<ZZ,QQ> { static inline ZZ cv(const QQ& r)
122  { return ( numerator(r)/denominator(r) ); } };
123 
124 template<> struct as_helper<QQ,RR> { static inline QQ cv(const RR& x)
125  { QQ r;mpq_set_f(&r.rep(),&x.rep()); return r; } };
126 
127 template<> struct as_helper<RR,QQ> { static inline RR cv(const QQ& x)
128  { RR r;mpf_set_q(&r.rep(),&x.rep()); return r; } };
129 
130 template<> struct as_helper<ZZ,unsigned> { static inline ZZ cv(const unsigned& x)
131  { ZZ r;mpz_set_ui(&r.rep(),x); return r; } };
132 
133 template<> struct as_helper<ZZ,double> { static inline ZZ cv(const double& x)
134  { ZZ r;mpz_set_d(&r.rep(),x); return r; } };
135 
136 template<> struct as_helper<RR,double> { static inline RR cv(const double& x)
137  { RR r;mpf_set_d(&r.rep(),x); return r; } };
138 
139 template<> struct as_helper<double,RR> { static inline double cv(const RR& x)
140  { return mpf_get_d(&x.rep()); } };
141 
142 template<> struct as_helper<double,mpf_t> { static inline double cv(const mpf_t& x)
143  { return mpf_get_d(x); } };
144 
145 
146 template<> struct as_helper<Interval<double>,QQ> {
147  static inline Interval<double> cv(const QQ& x) {
148  double l,u;
149  {
151  u=as<double>(x);
152  }
153  {
155  l=as<double>(x);
156  }
157 
158  return Interval<double>(l,u);
159  }
160 };
161 
162 // template<> struct as_helper<double,ZZ> { static inline double cv(const ZZ& x) { return x.get_d(); } };
163 // template<> struct as_helper<double,RR> { static inline double cv(const RR& x) { return x.get_d(); } };
164 
165 
166  inline QQ to_FT(const ZZ& a, const ZZ& b = 1) {
167  QQ r,d; let::assign(r,a); let::assign(d,b);
168  r /=d;
169  // r.canonicalize();
170  return r;
171  }
172  inline QQ to_FT(const QQ& a, const QQ& b = 1) { return a/b; }
173 //====================================================================
174 } //namespace mmx
175 #endif
static ZZ cv(const unsigned &x)
Definition: GMP.hpp:130
texp::rationalof< C >::T to_FT(const C &a, const C &b)
Definition: contfrac_intervaldata.hpp:93
true_t T
Definition: GMP.hpp:47
const C & b
Definition: Interval_glue.hpp:25
GMP::extended_rational EQQ
Definition: GMP.hpp:41
Definition: assign.hpp:48
bool with_plus_sign(const ZZ &c)
Definition: GMP.hpp:62
static ZZ cv(const QQ &r)
Definition: GMP.hpp:121
double gcd(const double &, const double &)
Definition: GMP.hpp:90
GMP::floating RR
Definition: GMP.hpp:39
T & rep()
Definition: scalar.hpp:30
Interval< extended_rational > interval_extended_rational
Definition: GMP.hpp:34
static Interval< double > cv(const QQ &x)
Definition: GMP.hpp:147
GMP T
Definition: GMP.hpp:54
double to_double(const RR &a)
Definition: GMP.hpp:66
static RR cv(const double &x)
Definition: GMP.hpp:136
scalar< extended< MPQ > > extended_rational
Definition: GMP.hpp:31
Definition: rounding_mode.hpp:71
GMP::rational QQ
Definition: GMP.hpp:37
double ieee
Definition: GMP.hpp:27
structure defining a positive answer
Definition: texp_bool.hpp:7
double to_XT(const ZZ &a)
Definition: GMP.hpp:69
static double cv(const RR &x)
Definition: GMP.hpp:139
Interval< rational > interval_rational
Definition: GMP.hpp:33
GMP T
Definition: GMP.hpp:52
ZZ numerator(const QQ &a)
Definition: GMPXX.hpp:95
scalar< MPZ > integer
Definition: GMP.hpp:28
false_t T
Definition: GMP.hpp:45
static QQ cv(const RR &x)
Definition: GMP.hpp:124
GMP T
Definition: GMP.hpp:53
scalar< MPF > floating
Definition: GMP.hpp:32
GMP::extended_integer EZZ
Definition: GMP.hpp:42
ZZ denominator(const QQ &a)
Definition: GMPXX.hpp:96
scalar< MPQ > rational
Definition: GMP.hpp:29
Generic class for intervals.
Definition: Interval.hpp:44
true_t T
Definition: GMP.hpp:46
const C & c
Definition: Interval_glue.hpp:45
static QQ cv(const ZZ &x)
Definition: GMP.hpp:118
Definition: GMP.hpp:45
structure defining a negative answer
Definition: texp_bool.hpp:9
int sign(const QQ &a)
Definition: GMP.hpp:60
void bit_precision(unsigned long l)
Definition: GMP.hpp:77
void assign(A &a, const B &b)
Generic definition of the assignement function.
Definition: assign.hpp:97
static ZZ cv(const double &x)
Definition: GMP.hpp:133
static double cv(const mpf_t &x)
Definition: GMP.hpp:142
Definition: array.hpp:12
GMP T
Definition: GMP.hpp:51
double as_double(const RR &a)
Definition: GMP.hpp:67
Definition: scalar.hpp:24
return the arithmetic kernel from which the unqualified type X comes from.
Definition: GMP.hpp:50
Numerical kernel based on gmp.
Definition: GMP.hpp:26
static RR cv(const QQ &x)
Definition: GMP.hpp:127
GMP::integer ZZ
Definition: GMP.hpp:38
scalar< extended< MPZ > > extended_integer
Definition: GMP.hpp:30
Home