realroot_doc 0.1.1
|
00001 #ifndef mmx_gmp_kernel_hpp 00002 #define mmx_gmp_kernel_hpp 00003 #include <realroot/texp_kernelof.hpp> 00004 #include <realroot/scalar_integer.hpp> 00005 #include <realroot/scalar_rational.hpp> 00006 #include <realroot/scalar_floating.hpp> 00007 #include <realroot/Interval.hpp> 00008 00009 namespace mmx { 00010 00023 00024 struct GMP { 00025 typedef double ieee; 00026 typedef scalar<MPZ> integer; 00027 typedef scalar<MPQ> rational; 00028 typedef scalar<MPF> floating; 00029 typedef Interval<rational> interval_rational ; 00030 }; 00031 00032 typedef GMP::rational QQ; 00033 typedef GMP::integer ZZ; 00034 typedef GMP::floating RR; 00035 00036 namespace texp { 00037 template<class R> struct kernelof_; 00038 template<> struct kernelof_<GMP::ieee> { typedef GMP T; }; 00039 template<> struct kernelof_<GMP::integer> { typedef GMP T; }; 00040 template<> struct kernelof_<GMP::rational> { typedef GMP T; }; 00041 template<> struct kernelof_<GMP::floating> { typedef GMP T; }; 00042 } 00043 00044 inline int sign(const QQ& a) { return mpq_sgn(&a.rep()); } 00045 inline bool with_plus_sign(const ZZ & c) { return (c>0);} 00046 inline bool with_plus_sign(const QQ & c) { return (c>0);} 00047 inline bool with_plus_sign(const RR & c) { return (c>0);} 00048 00049 00050 inline double to_double(const RR& a) { return mpf_get_d(&a.rep()); } 00051 inline double as_double(const RR& a) { return mpf_get_d(&a.rep()); } 00052 inline double to_XT(const ZZ& a) { return to_double(a); } 00053 inline double to_XT(const QQ& a) { return to_double(a); } 00054 inline double to_XT(const RR& a) { return to_double(a); } 00055 00056 00057 00058 //---------------------------------------------------------------------- 00062 inline void bit_precision(unsigned long l) { 00063 mpf_set_default_prec(l); 00064 } 00065 00066 //---------------------------------------------------------------------- 00070 inline void 00071 bit_precision( RR b, unsigned long l){ 00072 mpf_set_prec(&b.rep(),l); 00073 } 00074 00075 inline double gcd (const double&, const double&) { return 1; } 00076 00077 00078 //====================================================================== 00079 namespace let 00080 { 00081 inline void assign(ZZ& x, const QQ& r) { x= numerator(r)/denominator(r);} 00082 inline void assign(QQ& x, const ZZ& r) { mpq_set_z(&x.rep(), &r.rep());} 00083 00084 inline void assign(mpz_t r, const ZZ& x) { mpz_set(r, &x.rep());} 00085 inline void assign(mpf_t r, const ZZ& x) { mpf_set_z(r,&x.rep());} 00086 inline void assign(mpf_t r, const RR& x) { mpf_set(r,&x.rep());} 00087 inline void assign(mpf_t r, const QQ& x) 00088 { 00089 RR u=RR(numerator(x))/RR(denominator(x)); mpf_set(r,&u.rep()); 00090 } 00091 00092 } 00093 00094 template<class T, class U> struct as_helper; 00095 00096 template<> struct as_helper<QQ,ZZ> { static inline QQ cv(const ZZ& x) 00097 { QQ r;mpq_set_z(&r.rep(),&x.rep()); return r; } }; 00098 00099 template<> struct as_helper<ZZ,QQ> { static inline ZZ cv(const QQ& r) 00100 { return ( numerator(r)/denominator(r) ); } }; 00101 00102 template<> struct as_helper<QQ,RR> { static inline QQ cv(const RR& x) 00103 { QQ r;mpq_set_f(&r.rep(),&x.rep()); return r; } }; 00104 00105 template<> struct as_helper<RR,QQ> { static inline RR cv(const QQ& x) 00106 { RR r;mpf_set_q(&r.rep(),&x.rep()); return r; } }; 00107 00108 template<> struct as_helper<ZZ,unsigned> { static inline ZZ cv(const unsigned& x) 00109 { ZZ r;mpz_set_ui(&r.rep(),x); return r; } }; 00110 00111 template<> struct as_helper<ZZ,double> { static inline ZZ cv(const double& x) 00112 { ZZ r;mpz_set_d(&r.rep(),x); return r; } }; 00113 00114 template<> struct as_helper<RR,double> { static inline RR cv(const double& x) 00115 { RR r;mpf_set_d(&r.rep(),x); return r; } }; 00116 00117 template<> struct as_helper<double,RR> { static inline double cv(const RR& x) 00118 { return mpf_get_d(&x.rep()); } }; 00119 00120 template<> struct as_helper<double,mpf_t> { static inline double cv(const mpf_t& x) 00121 { return mpf_get_d(x); } }; 00122 00123 00124 template<> struct as_helper<Interval<double>,QQ> { 00125 static inline Interval<double> cv(const QQ& x) { 00126 double l,u; 00127 { 00128 numerics::rounding<double> rnd( numerics::rounding<double>::rnd_up() ); 00129 u=as<double>(x); 00130 } 00131 { 00132 numerics::rounding<double> rnd( numerics::rounding<double>::rnd_dw() ); 00133 l=as<double>(x); 00134 } 00135 00136 return Interval<double>(l,u); 00137 } 00138 }; 00139 00140 // template<> struct as_helper<double,ZZ> { static inline double cv(const ZZ& x) { return x.get_d(); } }; 00141 // template<> struct as_helper<double,RR> { static inline double cv(const RR& x) { return x.get_d(); } }; 00142 00143 00144 inline QQ to_FT(const ZZ& a, const ZZ& b = 1) { 00145 QQ r,d; let::assign(r,a); let::assign(d,b); 00146 r /=d; 00147 // r.canonicalize(); 00148 return r; 00149 } 00150 inline QQ to_FT(const QQ& a, const QQ& b = 1) { return a/b; } 00151 //==================================================================== 00152 } //namespace mmx 00153 #endif