realroot_doc 0.1.1
/Users/mourrain/Devel/mmx/realroot/include/realroot/scalar_floating.hpp
Go to the documentation of this file.
00001 /*********************************************************************
00002 *      This file is part of the source code of ALP software.         *
00003 *      (C) B. Mourrain, GALAAD, INRIA                                *
00004 **********************************************************************
00005 History: 
00006 $Id: scalar_mpf.H,v 1.1.1.1 2006/10/06 08:01:40 trebuche Exp $
00007 **********************************************************************/
00008 #ifndef _SCL_MPF_H_
00009 #define _SCL_MPF_H_
00010 //----------------------------------------------------------------------
00011 #include <string>
00012 #include <iostream>
00013 #include <gmp.h>
00014 #include <realroot/scalar.hpp>
00015 //======================================================================
00016 namespace mmx {
00017 //----------------------------------------------------------------------
00018 typedef __mpf_struct  MPF;
00019   //unsigned long MPF_PRECISION;
00020 //----------------------------------------------------------------------
00021 //template<> inline shared_object<MPF>::rep::~rep() {mpf_clear(&obj);}
00022 //----------------------------------------------------------------------
00023 template<> inline
00024 void scalar<MPF>::init ( )                   {mpf_init(&rep());}
00025 //------------------- scalar<T> (...) -------------------------------------
00026 template<> inline
00027 scalar<MPF>::scalar ( )                    {
00028   //  cout<<"default construct "<<endl;
00029 mpf_init(&rep());}
00030 //----------------------------------------------------------------------
00031 template<> inline 
00032 scalar<MPF>::scalar(const scalar<MPF>& rhs) 
00033 {
00034   //cout<<"const SCl "<<endl;
00035   mpf_init_set(&rep(),&rhs.rep()); 
00036 }
00037 //----------------------------------------------------------------------
00038 template<> inline 
00039 scalar<MPF>& scalar<MPF>::operator =  (const scalar<MPF>& rhs)
00040 {
00041   if (this != &rhs) {
00042     mpf_set(&rep(), &rhs.rep());
00043   }
00044   return *this;
00045 }
00046 //----------------------------------------------------------------------
00047 template<> inline
00048 scalar<MPF>::scalar (signed long int sl)   {mpf_init_set_si(&rep(),sl);}
00049 //----------------------------------------------------------------------
00050 template<> inline
00051 scalar<MPF>::scalar (unsigned long int ul) {mpf_init_set_ui(&rep(),ul);}
00052 //----------------------------------------------------------------------
00053 template<> inline
00054 scalar<MPF>::scalar (int si)               {
00055   // cout<<"init set si "<<endl;
00056 mpf_init_set_si(&rep(), si);}
00057 //----------------------------------------------------------------------
00058 template<> inline
00059 scalar<MPF>::scalar (const char *string, unsigned int base)
00060 {
00061   if (mpf_init_set_str(&rep(), string, base))
00062     std::cerr << "scalar<MPF>: The string " << string 
00063               << " is not a valid number in base " << base << std::endl;
00064 }
00065 template<> inline
00066 scalar<MPF>::scalar (double d)
00067 {
00068   //cout<<"je passe dans construct double "<<endl;
00069   mpf_init_set_d(&rep(),d);
00070 }
00071 //----------------------------------------------------------------------
00072 template<> inline scalar<MPF>::~scalar (){mpf_clear(&rep());}
00073 //----------------------------------------------------------------------
00074 template<> inline 
00075 bool scalar<MPF>::operator == (const scalar<MPF>& rhs) const
00076 {
00077   return mpf_cmp(&rep(), &rhs.rep()) == 0;
00078 }
00079 
00080 template<> inline 
00081 bool scalar<MPF>::operator == (long sl) const {
00082   return mpf_cmp_si(&rep(), sl) == 0;
00083 }
00084 
00085 template<> inline 
00086 bool scalar<MPF>::operator == (int si) const {
00087   return mpf_cmp_si(&rep(), (long) si) == 0;
00088 }
00089 
00090 template<> inline 
00091 bool scalar<MPF>::operator == (unsigned long ul) const
00092 {
00093   return mpf_cmp_ui(&rep(), ul) == 0;
00094 }
00095 
00096 // != ------------------------------------------------------------------
00097 template<> inline 
00098 bool scalar<MPF>::operator != (const scalar<MPF>& rhs) const
00099 {
00100   return mpf_cmp(&rep(), &rhs.rep()) != 0;
00101 }
00102 
00103 template<> inline 
00104 bool scalar<MPF>::operator != (long sl) const {
00105   return mpf_cmp_si(&rep(), sl) != 0;
00106 }
00107 
00108 template<> inline
00109 bool scalar<MPF>::operator != (int si) const {
00110   return mpf_cmp_si(&rep(), (long) si) != 0;
00111 }
00112 
00113 template<> inline 
00114 bool scalar<MPF>::operator != (unsigned long ul) const
00115 {
00116   return mpf_cmp_ui(&rep(), ul) != 0;
00117 }
00118 // > -------------------------------------------------------------------
00119 template<> inline 
00120 bool scalar<MPF>::operator > (const scalar<MPF>& rhs) const
00121 {
00122   return mpf_cmp(&rep(), &rhs.rep()) > 0;
00123 }
00124 
00125 template<> inline 
00126 bool scalar<MPF>::operator > (long sl) const {
00127   return mpf_cmp_si(&rep(), sl) > 0;
00128 }
00129 
00130 template<> inline 
00131 bool scalar<MPF>::operator > (int si) const {
00132   return mpf_cmp_si(&rep(), (long) si) > 0;
00133 }
00134 
00135 template<> inline 
00136 bool scalar<MPF>::operator > (unsigned long ul) const
00137 {
00138   return mpf_cmp_ui(&rep(), ul) > 0;
00139 }
00140 // <= --------------------------------------------------------------------
00141 template<> inline 
00142 bool scalar<MPF>::operator >= (const scalar<MPF>& rhs) const
00143 {
00144   return mpf_cmp(&rep(), &rhs.rep()) >= 0;
00145 }
00146 
00147 template<> inline 
00148 bool scalar<MPF>::operator >= (long sl) const {
00149   return mpf_cmp_si(&rep(), sl) >= 0;
00150 }
00151 
00152 template<> inline 
00153 bool scalar<MPF>::operator >= (int si) const
00154 {
00155   return mpf_cmp_si(&rep(), (long) si) >= 0;
00156 }
00157 
00158 template<> inline 
00159 bool scalar<MPF>::operator >= (unsigned long ul) const
00160 {
00161   return mpf_cmp_ui(&rep(), ul) >= 0;
00162 }
00163 
00164 // < -------------------------------------------------------------------
00165 template<> inline 
00166 bool scalar<MPF>::operator < (const scalar<MPF>& rhs) const
00167 {
00168   return mpf_cmp(&rep(), &rhs.rep()) < 0;
00169 }
00170 
00171 template<> inline 
00172 bool scalar<MPF>::operator < (long sl) const {
00173   return mpf_cmp_si(&rep(), sl) < 0;
00174 }
00175 
00176 template<> inline 
00177 bool scalar<MPF>::operator < (int si) const
00178 {
00179   return mpf_cmp_si(&rep(), (long) si) < 0;
00180 }
00181 
00182 template<> inline 
00183 bool scalar<MPF>::operator < (unsigned long ul) const
00184 {
00185   return mpf_cmp_ui(&rep(), ul) < 0;
00186 }
00187 
00188 // <= ------------------------------------------------------------------
00189 
00190 template<> inline 
00191 bool scalar<MPF>::operator <= (const scalar<MPF>& rhs) const
00192 {
00193   return mpf_cmp(&rep(), &rhs.rep()) <= 0;
00194 }
00195 
00196 template<> inline 
00197 bool scalar<MPF>::operator <= (long sl) const {
00198   return mpf_cmp_si(&rep(), sl) <= 0;
00199 }
00200 
00201 template<> inline 
00202 bool scalar<MPF>::operator <= (int si) const
00203 {
00204   return mpf_cmp_si(&rep(), (long) si) <= 0;
00205 }
00206 
00207 template<> inline 
00208 bool scalar<MPF>::operator <= (unsigned long ul) const
00209 {
00210   return mpf_cmp_ui(&rep(), ul) <= 0;
00211 }
00212 
00213 // = -------------------------------------------------------------------
00214 template<> inline 
00215 scalar<MPF>&  scalar<MPF>::operator =  (unsigned long ul)
00216 {
00217   mpf_set_ui(&rep(), ul); return *this;
00218 }
00219 
00220 template<> inline 
00221 scalar<MPF>& scalar<MPF>::operator =  (long sl)
00222 {
00223   mpf_set_si(&rep(), sl);  return *this;
00224 }
00225 
00226 template<> inline 
00227 scalar<MPF>& scalar<MPF>::operator = (int ul)
00228 {
00229   mpf_set_si(&rep(), ul);  return *this;
00230 }
00231 // += -------------------------------------------------------------------
00232 template<> inline 
00233 scalar<MPF>& scalar<MPF>::operator += (const scalar<MPF>& rhs)
00234 {
00235   scalar<MPF> tmp;
00236   //mpf_init(tmp.&rep());
00237   mpf_add(&tmp.rep(), &rep(), &rhs.rep());  
00238   mpf_swap(&tmp.rep(),&rep());
00239   return *this;
00240 
00241 }
00242 
00243 template<> inline 
00244 scalar<MPF>& scalar<MPF>::operator += (unsigned long ul)
00245 {
00246   mpf_add_ui(&rep(), &rep(), ul); return *this;
00247 }
00248 
00249 template<> inline 
00250 scalar<MPF>& scalar<MPF>::operator += (long sl)
00251 {
00252   if (sl >= 0)
00253     mpf_add_ui(&rep(), &rep(), (unsigned long) sl);
00254   else
00255     mpf_sub_ui(&rep(), &rep(), ((unsigned long) -sl));
00256   return *this;
00257 }
00258 
00259 template<> inline 
00260 scalar<MPF>& scalar<MPF>::operator += (int ul)
00261 {
00262   *this += scalar<MPF>(ul);  return *this;
00263 }
00264 
00265 // -= -------------------------------------------------------------------
00266 template<> inline 
00267 scalar<MPF>& scalar<MPF>::operator -= (const scalar<MPF>& rhs)
00268 {
00269   mpf_sub(&rep(), &rep(), &rhs.rep()); return *this;
00270 }
00271 
00272 template<> inline 
00273 scalar<MPF>& scalar<MPF>::operator -= (unsigned long ul)
00274 {
00275   mpf_sub_ui(&rep(), &rep(), ul); return *this;
00276 }
00277 
00278 template<> inline 
00279 scalar<MPF>& scalar<MPF>::operator -= (long sl)
00280 {
00281   if (sl >= 0)
00282     mpf_sub_ui(&rep(), &rep(), (unsigned long) sl);
00283   else
00284     mpf_add_ui(&rep(), &rep(), (unsigned long) sl);
00285   return *this;
00286 }
00287 
00288 template<> inline 
00289 scalar<MPF>& scalar<MPF>::operator -= (int ul)
00290 {
00291   *this -= scalar<MPF>(ul); return *this;
00292 }
00293 // *= -----------------------------------------------------------------
00294 template<> inline 
00295 scalar<MPF>& scalar<MPF>::operator *= (const scalar<MPF>& rhs)
00296 {
00297   mpf_mul(&rep(), &rep(), &rhs.rep()); return *this;
00298 }
00299 template<> inline 
00300 scalar<MPF>& scalar<MPF>::operator *= (unsigned long ul)
00301 {
00302   mpf_mul_ui(&rep(), &rep(), ul); return *this;
00303 }
00304 template<> inline 
00305 scalar<MPF>& scalar<MPF>::operator *= (long sl)
00306 {
00307   if (sl >= 0)
00308     mpf_mul_ui(&rep(), &rep(), (unsigned long) sl);
00309   else
00310     {
00311       mpf_neg(&rep(),&rep());
00312       mpf_mul_ui(&rep(), &rep(),(unsigned long)(-sl));
00313     }
00314   return *this; 
00315 }
00316 template<> inline 
00317 scalar<MPF>& scalar<MPF>::operator *= (double d)
00318 {
00319   return *this*=(scalar<MPF>)d;
00320 }
00321 
00322 template<> inline 
00323 scalar<MPF>& scalar<MPF>::operator *= (int ul)
00324 {
00325    if (ul >= 0)
00326      mpf_mul_ui(&rep(), &rep(), (unsigned long) ul);
00327    else {
00328      mpf_mul_ui(&rep(), &rep(), (unsigned long) (-ul));
00329      mpf_neg(&rep(), &rep());
00330    }
00331    return *this;
00332 }
00333 // /= ------------------------------------------------------------------
00334 template<> inline 
00335 scalar<MPF>& scalar<MPF>::operator /= (const scalar<MPF>& rhs)
00336 {
00337   mpf_div(&rep(), &rep(), &rhs.rep());
00338   //mpf_divexact(&rep(), &rep(), &rhs.rep()); 
00339   return *this;
00340 }
00341 template<> inline 
00342 scalar<MPF>& scalar<MPF>::operator /= (unsigned long ul)
00343 {
00344   mpf_div_ui(&rep(), &rep(), ul); return *this;
00345 }
00346 
00347 template<> inline 
00348 scalar<MPF>& scalar<MPF>::operator /= (long sl)
00349 {
00350   if (sl >= 0)
00351     mpf_div_ui(&rep(), &rep(), (unsigned long) sl);
00352   else
00353     {
00354       mpf_neg(&rep(),&rep());
00355       mpf_div_ui(&rep(), &rep(), ((unsigned long) -sl));
00356     }
00357   return *this;
00358 }
00359 template<> inline 
00360 scalar<MPF>& scalar<MPF>::operator /= (int sl)
00361 {
00362   if (sl >= 0)
00363     mpf_div_ui(&rep(), &rep(), (unsigned long) sl);
00364   else
00365     {
00366       mpf_neg(&rep(),&rep());
00367       mpf_div_ui(&rep(), &rep(), ((unsigned long) -sl));
00368     }
00369   return *this;
00370 }
00371 
00372 //======================================================================
00373 // ARITHMETIC OPERATORS
00374 //======================================================================
00375 //  scalar<MPF> operator -(const scalar<MPF>& a1) 
00376 //   {
00377 //     scalar<MPF> result(a1);
00378 //     result*=(-1);
00379 //     return result;
00380 //   }
00381 inline
00382 scalar<MPF> operator +(const scalar<MPF>& a1, const scalar<MPF>& a2) 
00383 {
00384   scalar<MPF> result;
00385   mpf_add(&result.rep(), &a1.rep(), &a2.rep());
00386   return result;
00387 }
00388 //----------------------------------------------------------------------
00389 inline
00390 scalar<MPF> operator -(const scalar<MPF>& a1, const scalar<MPF>& a2) 
00391 {
00392   scalar<MPF> result;
00393   mpf_sub(&result.rep(),&a1.rep(), &a2.rep());
00394   return result;
00395 }
00396 
00397 inline
00398 scalar<MPF> operator -(const scalar<MPF>& a1) 
00399 {
00400   scalar<MPF> r;  mpf_neg(&r.rep(), &a1.rep()); return r;
00401 }
00402 //----------------------------------------------------------------------
00403 inline
00404 scalar<MPF> operator *(const scalar<MPF>& a1, const scalar<MPF>& a2) 
00405 {
00406   scalar<MPF> result;
00407   mpf_mul(&result.rep(), &a1.rep(), &a2.rep());
00408   return result;
00409 }
00410 //----------------------------------------------------------------------
00411 inline
00412 scalar<MPF> operator /(const scalar<MPF>& a1, const scalar<MPF>& a2) 
00413 {
00414   scalar<MPF> result;
00415   mpf_div(&result.rep(), &a1.rep(), &a2.rep());
00416   return result;
00417 }
00418 //======================================================================
00420 template<> inline 
00421 void scalar<MPF>::operator ++ ( )
00422 {
00423   mpf_add_ui(&rep(), &rep(), 1);
00424 }
00425 //----------------------------------------------------------------------
00427 template<> inline 
00428 void scalar<MPF>::operator -- ( )
00429 {
00430   mpf_sub_ui(&rep(), &rep(), 1);
00431 }
00432 //======================================================================
00433 inline void convert(scalar<MPF>& n, char *s) 
00434 {
00435   // printf("%s\n",s);
00436   mpf_init_set_str(&n.rep(), s, 10);
00437   //cout<<"apres convert "<<mpf_get_d(n.rep())<<endl;;
00438 }
00439 //======================================================================
00440 // INPUT OUTPUT
00441 //======================================================================
00442 inline
00443 std::ostream& operator << (std::ostream& os, const scalar<MPF>& b)
00444 {
00445   double temp;
00446   temp=mpf_get_d(&b.rep());
00447   os<<temp;
00448   //mpf_out_str(stdout, 10,16,&b.rep()); 
00449   return os;
00450 }
00451 //----------------------------------------------------------------------
00452 inline
00453 std::istream& operator >> (std::istream& is, scalar<MPF>& b)
00454 {
00455   std::string s;is >> s;
00456   mpf_init_set_str(&b.rep(),s.c_str(), 10);
00457   return is;
00458 }
00459 //======================================================================
00460 // SPECIAL FUNCTIONS
00461 //======================================================================
00465 inline void Precision(unsigned long l)
00466 {
00467    mpf_set_default_prec(l);
00468 }
00469 //----------------------------------------------------------------------
00473 inline void Precision( scalar<MPF>& b, unsigned long l)
00474 {
00475   mpf_set_prec(&b.rep(),l);
00476 }
00477 //======================================================================
00478 template<> inline 
00479 void scalar<MPF>::Div2Exp (unsigned long exponent_of_2)
00480 {
00481   mpf_div_2exp(&rep(), &rep(), exponent_of_2);
00482 }
00483 
00484 template<> inline void
00485 scalar<MPF>::abs ( )
00486 {
00487   if (mpf_sgn(&rep()) < 0)
00488     mpf_neg(&rep(),&rep());
00489 }
00490 
00491 template<> inline 
00492 scalar<MPF>& scalar<MPF>::negate ( )
00493 {
00494   mpf_neg(&rep(),&rep());
00495   //&rep()._mp_size = - &rep()._mp_size; 
00496   return *this;
00497 }
00498 
00499 template<> inline 
00500 void scalar<MPF>::sqrt ( )
00501 {
00502   mpf_sqrt(&rep(), &rep());
00503 }
00504 
00505 //-------------------------- log(...) -----------------------------------
00506 inline size_t log (const scalar<MPF>& b)
00507 {
00508   return mpf_size(&b.rep());
00509 }
00510 //-------------------------- sign ---------------------------------------------
00511 inline int sign (const scalar<MPF>& b)
00512 {
00513   return mpf_sgn(&b.rep());
00514 }
00515 //-------------------------- compare (...) ------------------------------------
00516 inline int compare (const scalar<MPF>& b1, const scalar<MPF>& b2)
00517 {
00518   return mpf_cmp(&b1.rep(), &b2.rep());
00519 }
00520 
00521 inline int compare (const scalar<MPF>& b, unsigned long ul)
00522 {
00523   return mpf_cmp_ui(&b.rep(), ul);
00524 }
00525 
00526 inline int compare (const scalar<MPF>& b, long sl)
00527 {
00528   return mpf_cmp_si(&b.rep(), sl);
00529 }
00530 //----------------------------------------------------------------------
00531 //  scalar<MPF> & operator =(scalar<MPF> &lhs,double &rhs)
00532 //  {
00533 //    mpf_set_d(lhs.rep(),rhs);
00534 //    return lhs;
00535 //  }
00536 //----------------------------------------------------------------------
00537 inline void assign(double& r, const scalar<MPF>& z ) { r = mpf_get_d(&z.rep()); }; 
00538 inline void assign(scalar<MPF>& r, double d )        { mpf_set_d(&r.rep(),d); }
00539 
00540 } //namespace mmx
00541 //======================================================================
00542 #endif // //SCL_MPF_H