synaps/arithm/CC.h

00001 /*********************************************************************
00002 *      This file is part of the source code of SYNAPS kernel.        *
00003 *   Author(s): B. Mourrain, GALAAD, INRIA                            *
00004 **********************************************************************
00005 History: 
00006 $Id: CC.h,v 1.4 2005/09/28 06:40:42 mourrain Exp $
00007 **********************************************************************/
00008 #ifndef synaps_arithm_CC_H
00009 #define synaps_arithm_CC_H
00010 /********************************************************************/
00011 #include <assert.h>
00012 #include <synaps/init.h>
00013 #include <synaps/arithm/Scl.h>
00014 #include <mpsolve/mpc.h>
00015 #include <iostream>
00016 /********************************************************************/
00017 __BEGIN_NAMESPACE_SYNAPS
00018 //--------------------------------------------------------------------
00019 typedef  __mpc_struct MPC;
00020 //typedef  mpc_t MPC;
00021 typedef Scl<MPC> CC;
00022 //--------------------------------------------------------------------
00023 template<> inline 
00024 void CC::init() {mpc_init(&rep());}
00025 //-------------------------- Scl<T> (...) ----------------------------
00026 template<> inline
00027 Scl<MPC>::Scl(void)
00028 {
00029   mpc_init(&rep());
00030 }
00031 
00032 template<> inline
00033 Scl<MPC>::Scl(const Scl<MPC>& b)
00034 {
00035   mpc_init_set(&rep(), &b.rep());
00036 }
00037 
00038 template<> inline
00039 Scl<MPC>::Scl(const MPC & b)
00040 {
00041   mpc_init_set(&rep(), &b);
00042 }
00043 
00044 template<> inline
00045 Scl<MPC>::Scl(double d)
00046 {
00047   mpc_init_set_d(&rep(), d, 0);
00048 }
00049 
00050 template<> inline
00051 Scl<MPC>::Scl(signed long int sl)
00052 {
00053   mpc_init_set_si(&rep(), sl, 0);
00054 }
00055 
00056 template<> inline
00057 Scl<MPC>::Scl(unsigned long int ul)
00058 {
00059   mpc_init_set_ui(&rep(), ul, 0);
00060 }
00061 
00062 template<> inline
00063 Scl<MPC>::Scl(int si)
00064 {
00065   mpc_init_set_si(&rep(), (long) si,0);
00066 }
00067 
00068 template<> inline
00069 Scl<MPC>::Scl(const char *string,unsigned int base)
00070 {
00071   if (mpc_init_set_str(&rep(), string,"0", base))
00072     std::cerr << "Scl<MPC>::Scl<MPC>: The string " << string 
00073               << " is not a valid number in base " << base << std::endl;
00074 }
00075 
00076 //-------------------------- Scl<MPC> =, +=, -=, *=, /=, %= ---------------------
00077 
00078 template<> inline 
00079 Scl<MPC>& Scl<MPC>::operator =  (const Scl<MPC>& rhs)
00080 {
00081   if (this != &rhs)
00082     mpc_set(&rep(), &rhs.rep()); return *this;
00083 }
00084 
00085 template<> inline 
00086 Scl<MPC>& Scl<MPC>::operator += (const Scl<MPC>& rhs)
00087 {
00088   mpc_add(&rep(), &rep(), &rhs.rep());  return *this;
00089 }
00090 
00091 template<> inline 
00092 Scl<MPC>& Scl<MPC>::operator -= (const Scl<MPC>& rhs)
00093 {
00094   mpc_sub(&rep(), &rep(), &rhs.rep()); return *this;
00095 }
00096 
00097 template<> inline 
00098 Scl<MPC>& Scl<MPC>::operator *= (const Scl<MPC>& rhs)
00099 {
00100   mpc_mul(&rep(), &rep(), &rhs.rep()); return *this;
00101 }
00102 
00103 template<> inline 
00104 Scl<MPC>& Scl<MPC>::operator /= (const Scl<MPC>& rhs)
00105 {
00106   mpc_div(&rep(), &rep(), &rhs.rep()); return *this;
00107 }
00108 
00109 //-------------------------- unsigned long =, +=, -=, *=, /=, %= --------------
00110 
00111 template<> inline 
00112 Scl<MPC>&  Scl<MPC>::operator =  (unsigned long ul)
00113 {
00114   mpc_set_ui(&rep(), ul,0); return *this;
00115 }
00116 
00117 template<> inline Scl<MPC>& 
00118 Scl<MPC>::operator += (unsigned long ul)
00119 {
00120   mpc_add_ui(&rep(), &rep(), ul,0);return *this;
00121 }
00122 
00123 template<> inline Scl<MPC>& 
00124 Scl<MPC>::operator -= (unsigned long ul)
00125 {
00126   mpc_sub_ui(&rep(), &rep(), ul,0);return *this;
00127 }
00128 
00129 template<> inline Scl<MPC>& 
00130 Scl<MPC>::operator *= (int ul)
00131 {
00132    if (ul >= 0)
00133      mpc_mul_ui(&rep(), &rep(), (unsigned long) ul);
00134    else {
00135      mpc_mul_ui(&rep(), &rep(), (unsigned long) (-ul));
00136      mpc_neg(&rep(), &rep());
00137    }
00138    return *this;
00139 }
00140 
00141 template<> inline Scl<MPC>& 
00142 Scl<MPC>::operator *= (unsigned long ul)
00143 {
00144   mpc_mul_ui(&rep(), &rep(), ul);return *this;
00145 }
00146 
00147 template<> inline Scl<MPC>& 
00148 Scl<MPC>::operator /= (unsigned long ul)
00149 {
00150   mpc_div_ui(&rep(), &rep(), ul);return *this;
00151 }
00152 
00153 //-------------------------- long =, +=, -=, *=, /=, %= -----------------------
00154 template<> inline Scl<MPC>& 
00155 Scl<MPC>::operator =  (long sl)
00156 {
00157   mpc_set_si(&rep(), sl,0);return *this;
00158 }
00159 
00160 template<> inline Scl<MPC>& 
00161 Scl<MPC>::operator += (long sl)
00162 {
00163   if (sl >= 0)
00164     mpc_add_ui(&rep(), &rep(), (unsigned long) sl,0);
00165   else
00166     mpc_sub_ui(&rep(), &rep(), ((unsigned long) -sl),0);
00167   return *this;
00168 }
00169 
00170 template<> inline Scl<MPC>& 
00171 Scl<MPC>::operator -= (long sl)
00172 {
00173   if (sl >= 0)
00174     mpc_sub_ui(&rep(), &rep(), (unsigned long) sl,0);
00175   else
00176     mpc_add_ui(&rep(), &rep(), (unsigned long) sl,0);
00177   return *this;
00178 }
00179 
00180 template<> inline Scl<MPC>& 
00181 Scl<MPC>::operator *= (long sl)
00182 {
00183   if (sl >= 0)
00184     mpc_mul_ui(&rep(), &rep(), (unsigned long)sl);
00185   else
00186     {
00187       mpc_mul_ui(&rep(), &rep(), ((unsigned long) -sl));
00188       mpc_neg(&rep(), &rep());
00189     }
00190   return *this;
00191 }
00192 
00193 template<> inline Scl<MPC>& 
00194 Scl<MPC>::operator /= (long sl)
00195 {
00196   if (sl >= 0)
00197     mpc_div_ui(&rep(), &rep(), (unsigned long) sl);
00198   else
00199     {
00200       mpc_div_ui(&rep(), &rep(), ((unsigned long) -sl));
00201       mpc_neg(&rep(), &rep());
00202     }
00203   return *this;
00204 }
00205 
00206 //----------------------------------------------------------------------
00207 template<> inline Scl<MPC>& Scl<MPC>::operator = (int si)
00208 {
00209   mpc_init_set_si(&rep(), (long) si,0); return *this;
00210 }
00211 //----------------------------------------------------------------------
00212 template<> inline Scl<MPC>& 
00213 Scl<MPC>::operator += (int i)
00214 {
00215   *this+= long(i);
00216   return *this;
00217 }
00218 
00219 template<> inline 
00220 Scl<MPC>& Scl<MPC>::pow (unsigned long exp)
00221 {
00222   //  double r(mpc_get_d(&rep())),x(r); 
00223   Scl<MPC> r(*this);
00224   for (unsigned long i = 2; i <= exp; i++) mpc_mul(&rep(),&rep(),&r.rep()); // x *=r;
00225   // mpc_set_d(&rep(),x);
00226   
00227   return *this;
00228 }
00229 
00230 inline Scl<MPC> operator * (double d, const Scl<MPC>& b)
00231 {
00232   /*Scl<MPC> r(b);
00233     mpc_set_d(&r.rep(),mpc_get_d(&b.rep()) * d);*/
00234   Scl<MPC> r(d);
00235   mpc_mul(&r.rep(),&r.rep(),&b.rep());
00236   return Scl<MPC>(r);
00237 }
00238 //----------------------------------------------------------------------
00239 template<> inline bool
00240 Scl<MPC>::operator == (const Scl<MPC>& x) const
00241 {
00242   return mpc_eq(&rep(), &x.rep(), 0);
00243 }
00244 //----------------------------------------------------------------------
00245 template<> inline bool
00246 Scl<MPC>::operator != (int si) const
00247 {
00248   if(si==0)
00249     return mpc_eq_zero(& this->rep());
00250   else if(si==1)
00251     return mpc_eq_one(& this->rep());
00252   else
00253     {
00254       Scl<MPC> tmp(si);
00255       return mpc_eq(&rep(), &tmp.rep(), 0); //xxx
00256     }
00257 }
00258 //----------------------------------------------------------------------
00259 inline Scl<MPC>
00260 Norm(const Scl<MPC>& x)
00261 {
00262  if(x>0)
00263   return Scl<MPC>(x);
00264  else
00265   return Scl<MPC>(-x);
00266 }
00267 
00268 //======================================================================
00269 // ASSIGNEMENTS  
00270 //======================================================================
00271 
00272 inline void add(MPC& r, const MPC& a, const MPC& b) {mpc_add(&r,&a,&b);}
00273 inline void sub(MPC& r, const MPC& a, const MPC& b) {mpc_sub(&r,&a,&b);}
00274 inline void mul(MPC& r, const MPC& a, const MPC& b) {mpc_mul(&r,&a,&b);}
00275 inline void div(MPC& r, const MPC& a, const MPC& b) {mpc_div(&r,&a,&b);}
00276 
00277 
00278 // namespace let {
00279 
00280 //   inline void assign(Scl<MPC> & p, 
00281 //                   const OP<'+',Scl<MPC>, Scl<MPC> >& M) 
00282 //   {
00283 //     mpc_add(&p.rep(),&M.op1.rep(),&M.op2.rep());
00284 //   }
00285 //   //----------------------------------------------------------------------
00286 //   inline
00287 //   void assign(Scl<MPC> & p, 
00288 //            const OP<'-',Scl<MPC>, Scl<MPC> >& M) 
00289 //   { 
00290 //     mpc_sub(&p.rep(),&M.op1.rep(),&M.op2.rep());
00291 //   }
00292 //   //----------------------------------------------------------------------
00293 //   inline void assign(Scl<MPC> & p, 
00294 //            const OP<'*',Scl<MPC>, Scl<MPC> >& M) 
00295 //   { 
00296 //     mpc_mul(&p.rep(),&M.op1.rep(),&M.op2.rep());
00297 //   }
00298 //   //----------------------------------------------------------------------
00299 //   inline void assign(Scl<MPC> & p, 
00300 //            const OP<'/',Scl<MPC>,Scl<MPC> >& M) 
00301 //   { 
00302 //     assert(M.op2!=0);
00303 //     mpc_div(&p.rep(),&M.op1.rep(),&M.op2.rep());
00304 //   }
00305 // }
00306 //======================================================================
00307 // INPUT OUTPUT
00308 //======================================================================
00309 inline std::ostream&  operator << (std::ostream& os, const Scl<MPC>& b)
00310 {
00311   mpc_out_str(stdout, 10, mpc_get_prec(&b.rep()), &b.rep()); //mpc_get_prec(&b.rep())
00312   return os;
00313 }
00314 //----------------------------------------------------------------------
00315 inline std::istream&  operator >> (std::istream& is, Scl<MPC>& x)
00316 {
00317   std::string a,b; is >> a>> b;
00318   mpc_init_set_str(&x.rep(),a.c_str(),b.c_str(),10);
00319   return is;
00320 }
00321 //--------------------------------------------------------------------
00322 template<class T>
00323 CC Complex(const T& a, const T& b)
00324 {
00325   RR r,i;
00326   let::assign(r,a);
00327   let::assign(i,b);
00328   CC z;
00329   mpc_set_f(&z.rep(),r.get_mpf_t(),i.get_mpf_t());
00330   return z; 
00331 }
00332 
00333 inline RR real(const CC& z)
00334 {
00335   return RR(z.rep().r); 
00336 } 
00337 
00338 inline RR img(const CC& z)
00339 {
00340   return RR(z.rep().i); 
00341 } 
00342 /********************************************************************/
00343 __END_NAMESPACE_SYNAPS
00344 #endif //synaps_arithm_CC_H

SYNAPS DOCUMENTATION
logo