Developer documentation

scalar_floating.hpp
Go to the documentation of this file.
1 /*********************************************************************
2 * This file is part of the source code of ALP software. *
3 * (C) B. Mourrain, GALAAD, INRIA *
4 **********************************************************************
5 History:
6 $Id: scalar_mpf.H,v 1.1.1.1 2006/10/06 08:01:40 trebuche Exp $
7 **********************************************************************/
8 #ifndef _SCL_MPF_H_
9 #define _SCL_MPF_H_
10 //----------------------------------------------------------------------
11 #include <string>
12 #include <iostream>
13 #include <gmp.h>
14 #include <realroot/scalar.hpp>
15 //======================================================================
16 namespace mmx {
17 //----------------------------------------------------------------------
18 typedef __mpf_struct MPF;
19  //unsigned long MPF_PRECISION;
20 //----------------------------------------------------------------------
21 //template<> inline shared_object<MPF>::rep::~rep() {mpf_clear(&obj);}
22 //----------------------------------------------------------------------
23 template<> inline
24 void scalar<MPF>::init ( ) {mpf_init(&rep());}
25 //------------------- scalar<T> (...) -------------------------------------
26 template<> inline
28  // cout<<"default construct "<<endl;
29 mpf_init(&rep());}
30 //----------------------------------------------------------------------
31 template<> inline
33 {
34  //cout<<"const SCl "<<endl;
35  mpf_init_set(&rep(),&rhs.rep());
36 }
37 //----------------------------------------------------------------------
38 template<> inline
40 {
41  if (this != &rhs) {
42  mpf_set(&rep(), &rhs.rep());
43  }
44  return *this;
45 }
46 //----------------------------------------------------------------------
47 template<> inline
48 scalar<MPF>::scalar (signed long int sl) {mpf_init_set_si(&rep(),sl);}
49 //----------------------------------------------------------------------
50 template<> inline
51 scalar<MPF>::scalar (unsigned long int ul) {mpf_init_set_ui(&rep(),ul);}
52 //----------------------------------------------------------------------
53 template<> inline
55  // cout<<"init set si "<<endl;
56 mpf_init_set_si(&rep(), si);}
57 //----------------------------------------------------------------------
58 template<> inline
59 scalar<MPF>::scalar (const char *string, unsigned int base)
60 {
61  if (mpf_init_set_str(&rep(), string, base))
62  std::cerr << "scalar<MPF>: The string " << string
63  << " is not a valid number in base " << base << std::endl;
64 }
65 template<> inline
67 {
68  //cout<<"je passe dans construct double "<<endl;
69  mpf_init_set_d(&rep(),d);
70 }
71 //----------------------------------------------------------------------
72 template<> inline scalar<MPF>::~scalar (){mpf_clear(&rep());}
73 //----------------------------------------------------------------------
74 template<> inline
75 bool scalar<MPF>::operator == (const scalar<MPF>& rhs) const
76 {
77  return mpf_cmp(&rep(), &rhs.rep()) == 0;
78 }
79 
80 template<> inline
81 bool scalar<MPF>::operator == (long sl) const {
82  return mpf_cmp_si(&rep(), sl) == 0;
83 }
84 
85 template<> inline
86 bool scalar<MPF>::operator == (int si) const {
87  return mpf_cmp_si(&rep(), (long) si) == 0;
88 }
89 
90 template<> inline
91 bool scalar<MPF>::operator == (unsigned long ul) const
92 {
93  return mpf_cmp_ui(&rep(), ul) == 0;
94 }
95 
96 // != ------------------------------------------------------------------
97 template<> inline
98 bool scalar<MPF>::operator != (const scalar<MPF>& rhs) const
99 {
100  return mpf_cmp(&rep(), &rhs.rep()) != 0;
101 }
102 
103 template<> inline
104 bool scalar<MPF>::operator != (long sl) const {
105  return mpf_cmp_si(&rep(), sl) != 0;
106 }
107 
108 template<> inline
109 bool scalar<MPF>::operator != (int si) const {
110  return mpf_cmp_si(&rep(), (long) si) != 0;
111 }
112 
113 template<> inline
114 bool scalar<MPF>::operator != (unsigned long ul) const
115 {
116  return mpf_cmp_ui(&rep(), ul) != 0;
117 }
118 // > -------------------------------------------------------------------
119 template<> inline
120 bool scalar<MPF>::operator > (const scalar<MPF>& rhs) const
121 {
122  return mpf_cmp(&rep(), &rhs.rep()) > 0;
123 }
124 
125 template<> inline
126 bool scalar<MPF>::operator > (long sl) const {
127  return mpf_cmp_si(&rep(), sl) > 0;
128 }
129 
130 template<> inline
131 bool scalar<MPF>::operator > (int si) const {
132  return mpf_cmp_si(&rep(), (long) si) > 0;
133 }
134 
135 template<> inline
136 bool scalar<MPF>::operator > (unsigned long ul) const
137 {
138  return mpf_cmp_ui(&rep(), ul) > 0;
139 }
140 // <= --------------------------------------------------------------------
141 template<> inline
143 {
144  return mpf_cmp(&rep(), &rhs.rep()) >= 0;
145 }
146 
147 template<> inline
148 bool scalar<MPF>::operator >= (long sl) const {
149  return mpf_cmp_si(&rep(), sl) >= 0;
150 }
151 
152 template<> inline
153 bool scalar<MPF>::operator >= (int si) const
154 {
155  return mpf_cmp_si(&rep(), (long) si) >= 0;
156 }
157 
158 template<> inline
159 bool scalar<MPF>::operator >= (unsigned long ul) const
160 {
161  return mpf_cmp_ui(&rep(), ul) >= 0;
162 }
163 
164 // < -------------------------------------------------------------------
165 template<> inline
166 bool scalar<MPF>::operator < (const scalar<MPF>& rhs) const
167 {
168  return mpf_cmp(&rep(), &rhs.rep()) < 0;
169 }
170 
171 template<> inline
172 bool scalar<MPF>::operator < (long sl) const {
173  return mpf_cmp_si(&rep(), sl) < 0;
174 }
175 
176 template<> inline
177 bool scalar<MPF>::operator < (int si) const
178 {
179  return mpf_cmp_si(&rep(), (long) si) < 0;
180 }
181 
182 template<> inline
183 bool scalar<MPF>::operator < (unsigned long ul) const
184 {
185  return mpf_cmp_ui(&rep(), ul) < 0;
186 }
187 
188 // <= ------------------------------------------------------------------
189 
190 template<> inline
192 {
193  return mpf_cmp(&rep(), &rhs.rep()) <= 0;
194 }
195 
196 template<> inline
197 bool scalar<MPF>::operator <= (long sl) const {
198  return mpf_cmp_si(&rep(), sl) <= 0;
199 }
200 
201 template<> inline
202 bool scalar<MPF>::operator <= (int si) const
203 {
204  return mpf_cmp_si(&rep(), (long) si) <= 0;
205 }
206 
207 template<> inline
208 bool scalar<MPF>::operator <= (unsigned long ul) const
209 {
210  return mpf_cmp_ui(&rep(), ul) <= 0;
211 }
212 
213 // = -------------------------------------------------------------------
214 template<> inline
216 {
217  mpf_set_ui(&rep(), ul); return *this;
218 }
219 
220 template<> inline
222 {
223  mpf_set_si(&rep(), sl); return *this;
224 }
225 
226 template<> inline
228 {
229  mpf_set_si(&rep(), ul); return *this;
230 }
231 // += -------------------------------------------------------------------
232 template<> inline
234 {
235  scalar<MPF> tmp;
236  //mpf_init(tmp.&rep());
237  mpf_add(&tmp.rep(), &rep(), &rhs.rep());
238  mpf_swap(&tmp.rep(),&rep());
239  return *this;
240 
241 }
242 
243 template<> inline
245 {
246  mpf_add_ui(&rep(), &rep(), ul); return *this;
247 }
248 
249 template<> inline
251 {
252  if (sl >= 0)
253  mpf_add_ui(&rep(), &rep(), (unsigned long) sl);
254  else
255  mpf_sub_ui(&rep(), &rep(), ((unsigned long) -sl));
256  return *this;
257 }
258 
259 template<> inline
261 {
262  *this += scalar<MPF>(ul); return *this;
263 }
264 
265 // -= -------------------------------------------------------------------
266 template<> inline
268 {
269  mpf_sub(&rep(), &rep(), &rhs.rep()); return *this;
270 }
271 
272 template<> inline
274 {
275  mpf_sub_ui(&rep(), &rep(), ul); return *this;
276 }
277 
278 template<> inline
280 {
281  if (sl >= 0)
282  mpf_sub_ui(&rep(), &rep(), (unsigned long) sl);
283  else
284  mpf_add_ui(&rep(), &rep(), (unsigned long) sl);
285  return *this;
286 }
287 
288 template<> inline
290 {
291  *this -= scalar<MPF>(ul); return *this;
292 }
293 // *= -----------------------------------------------------------------
294 template<> inline
296 {
297  mpf_mul(&rep(), &rep(), &rhs.rep()); return *this;
298 }
299 template<> inline
301 {
302  mpf_mul_ui(&rep(), &rep(), ul); return *this;
303 }
304 template<> inline
306 {
307  if (sl >= 0)
308  mpf_mul_ui(&rep(), &rep(), (unsigned long) sl);
309  else
310  {
311  mpf_neg(&rep(),&rep());
312  mpf_mul_ui(&rep(), &rep(),(unsigned long)(-sl));
313  }
314  return *this;
315 }
316 template<> inline
318 {
319  return *this*=(scalar<MPF>)d;
320 }
321 
322 template<> inline
324 {
325  if (ul >= 0)
326  mpf_mul_ui(&rep(), &rep(), (unsigned long) ul);
327  else {
328  mpf_mul_ui(&rep(), &rep(), (unsigned long) (-ul));
329  mpf_neg(&rep(), &rep());
330  }
331  return *this;
332 }
333 // /= ------------------------------------------------------------------
334 template<> inline
336 {
337  mpf_div(&rep(), &rep(), &rhs.rep());
338  //mpf_divexact(&rep(), &rep(), &rhs.rep());
339  return *this;
340 }
341 template<> inline
343 {
344  mpf_div_ui(&rep(), &rep(), ul); return *this;
345 }
346 
347 template<> inline
349 {
350  if (sl >= 0)
351  mpf_div_ui(&rep(), &rep(), (unsigned long) sl);
352  else
353  {
354  mpf_neg(&rep(),&rep());
355  mpf_div_ui(&rep(), &rep(), ((unsigned long) -sl));
356  }
357  return *this;
358 }
359 template<> inline
361 {
362  if (sl >= 0)
363  mpf_div_ui(&rep(), &rep(), (unsigned long) sl);
364  else
365  {
366  mpf_neg(&rep(),&rep());
367  mpf_div_ui(&rep(), &rep(), ((unsigned long) -sl));
368  }
369  return *this;
370 }
371 
372 //======================================================================
373 // ARITHMETIC OPERATORS
374 //======================================================================
375 // scalar<MPF> operator -(const scalar<MPF>& a1)
376 // {
377 // scalar<MPF> result(a1);
378 // result*=(-1);
379 // return result;
380 // }
381 inline
383 {
384  scalar<MPF> result;
385  mpf_add(&result.rep(), &a1.rep(), &a2.rep());
386  return result;
387 }
388 //----------------------------------------------------------------------
389 inline
391 {
392  scalar<MPF> result;
393  mpf_sub(&result.rep(),&a1.rep(), &a2.rep());
394  return result;
395 }
396 
397 inline
399 {
400  scalar<MPF> r; mpf_neg(&r.rep(), &a1.rep()); return r;
401 }
402 //----------------------------------------------------------------------
403 inline
405 {
406  scalar<MPF> result;
407  mpf_mul(&result.rep(), &a1.rep(), &a2.rep());
408  return result;
409 }
410 //----------------------------------------------------------------------
411 inline
413 {
414  scalar<MPF> result;
415  mpf_div(&result.rep(), &a1.rep(), &a2.rep());
416  return result;
417 }
418 //======================================================================
420 template<> inline
422 {
423  mpf_add_ui(&rep(), &rep(), 1);
424 }
425 //----------------------------------------------------------------------
427 template<> inline
429 {
430  mpf_sub_ui(&rep(), &rep(), 1);
431 }
432 //======================================================================
433 inline void convert(scalar<MPF>& n, char *s)
434 {
435  // printf("%s\n",s);
436  mpf_init_set_str(&n.rep(), s, 10);
437  //cout<<"apres convert "<<mpf_get_d(n.rep())<<endl;;
438 }
439 //======================================================================
440 // INPUT OUTPUT
441 //======================================================================
442 inline
443 std::ostream& operator << (std::ostream& os, const scalar<MPF>& b)
444 {
445  double temp;
446  temp=mpf_get_d(&b.rep());
447  os<<temp;
448  //mpf_out_str(stdout, 10,16,&b.rep());
449  return os;
450 }
451 //----------------------------------------------------------------------
452 inline
453 std::istream& operator >> (std::istream& is, scalar<MPF>& b)
454 {
455  std::string s;is >> s;
456  mpf_init_set_str(&b.rep(),s.c_str(), 10);
457  return is;
458 }
459 //======================================================================
460 // SPECIAL FUNCTIONS
461 //======================================================================
465 inline void Precision(unsigned long l)
466 {
467  mpf_set_default_prec(l);
468 }
469 //----------------------------------------------------------------------
473 inline void Precision( scalar<MPF>& b, unsigned long l)
474 {
475  mpf_set_prec(&b.rep(),l);
476 }
477 //======================================================================
478 template<> inline
479 void scalar<MPF>::Div2Exp (unsigned long exponent_of_2)
480 {
481  mpf_div_2exp(&rep(), &rep(), exponent_of_2);
482 }
483 
484 template<> inline void
486 {
487  if (mpf_sgn(&rep()) < 0)
488  mpf_neg(&rep(),&rep());
489 }
490 
491 template<> inline
493 {
494  mpf_neg(&rep(),&rep());
495  //&rep()._mp_size = - &rep()._mp_size;
496  return *this;
497 }
498 
499 template<> inline
501 {
502  mpf_sqrt(&rep(), &rep());
503 }
504 
505 //-------------------------- log(...) -----------------------------------
506 inline size_t log (const scalar<MPF>& b)
507 {
508  return mpf_size(&b.rep());
509 }
510 //-------------------------- sign ---------------------------------------------
511 inline int sign (const scalar<MPF>& b)
512 {
513  return mpf_sgn(&b.rep());
514 }
515 //-------------------------- compare (...) ------------------------------------
516 inline int compare (const scalar<MPF>& b1, const scalar<MPF>& b2)
517 {
518  return mpf_cmp(&b1.rep(), &b2.rep());
519 }
520 
521 inline int compare (const scalar<MPF>& b, unsigned long ul)
522 {
523  return mpf_cmp_ui(&b.rep(), ul);
524 }
525 
526 inline int compare (const scalar<MPF>& b, long sl)
527 {
528  return mpf_cmp_si(&b.rep(), sl);
529 }
530 //----------------------------------------------------------------------
531 // scalar<MPF> & operator =(scalar<MPF> &lhs,double &rhs)
532 // {
533 // mpf_set_d(lhs.rep(),rhs);
534 // return lhs;
535 // }
536 //----------------------------------------------------------------------
537 inline void assign(double& r, const scalar<MPF>& z ) { r = mpf_get_d(&z.rep()); };
538 inline void assign(scalar<MPF>& r, double d ) { mpf_set_d(&r.rep(),d); }
539 
540 } //namespace mmx
541 //======================================================================
542 #endif // //SCL_MPF_H
bool operator>(const scalar< T > &rhs) const
scalar< T > & operator+=(const scalar< T > &rhs)
scalar()
Definition: scalar.hpp:37
const C & b
Definition: Interval_glue.hpp:25
void Precision(unsigned long l)
Definition: GMPXX.hpp:112
scalar< T > & operator=(const scalar< T > &rhs)
bool operator>=(const scalar< T > &rhs) const
T & rep()
Definition: scalar.hpp:30
R & rep(R &r)
Definition: shared_object.hpp:180
extended< NT > operator-(const extended< NT > &lhs, const extended< NT > &rhs)
Definition: extended.hpp:132
extended< NT > operator/(const extended< NT > &lhs, const extended< NT > &rhs)
Definition: extended.hpp:111
scalar< T > & operator-=(const scalar< T > &rhs)
int compare(const QQ &a, const QQ &b)
Definition: GMPXX.hpp:71
extended< NT > operator+(const extended< NT > &lhs, const extended< NT > &rhs)
Definition: extended.hpp:122
__mpf_struct MPF
Definition: scalar_floating.hpp:18
bool operator<(const scalar< T > &rhs) const
bool operator<=(const scalar< T > &rhs) const
bool operator!=(const scalar< T > &rhs) const
void assign(double &r, const scalar< MPF > &z)
Definition: scalar_floating.hpp:537
scalar< T > & operator*=(const scalar< T > &rhs)
scalar< T > & negate()
void operator++()
scalar< T > & operator/=(const scalar< T > &rhs)
bool operator==(const scalar< T > &rhs) const
std::istream & operator>>(std::istream &is, scalar< MPF > &b)
Definition: scalar_floating.hpp:453
void operator--()
int sign(const QQ &a)
Definition: GMP.hpp:60
void convert(scalar< MPF > &n, char *s)
Definition: scalar_floating.hpp:433
Definition: array.hpp:12
extended< NT > operator*(const extended< NT > &lhs, const extended< NT > &rhs)
Definition: extended.hpp:101
Definition: scalar.hpp:24
size_t log(const scalar< MPF > &b)
Definition: scalar_floating.hpp:506
void Div2Exp(unsigned long exponent_of_2)
~scalar()
Definition: scalar.hpp:55
Home