Developer documentation

scalar_rational.hpp
Go to the documentation of this file.
1 /*********************************************************************
2 * This file is part of the source code of realroot software. *
3 * (C) B. Mourrain, GALAAD, INRIA *
4 **********************************************************************
5 History:
6 **********************************************************************/
7 #ifndef _SCL_MPQ_H_
8 #define _SCL_MPQ_H_
9 //----------------------------------------------------------------------
10 #include <string>
11 #include <iostream>
12 #include <realroot/scalar.hpp>
13 //======================================================================
14 namespace mmx {
15 //----------------------------------------------------------------------
16 typedef MP_INT MPZ;
17 typedef MP_RAT MPQ;
18 //----------------------------------------------------------------------
19 std::ostream& operator << (std::ostream& os, const scalar<MPQ>& b);
20 //----------------------------------------------------------------------
21 template<> inline
22 void scalar<MPQ>::init ( ) {mpq_init(&rep());}
23 //------------------- scalar<T> (...) -------------------------------------
24 template<> inline
25 scalar<MPQ>::scalar ( ) {mpq_init(&rep());}
26 //----------------------------------------------------------------------
27 template<> inline scalar<MPQ>::~scalar ( ) {mpq_clear(&rep());}
28 //----------------------------------------------------------------------
29 template<> inline
31  {mpq_init(&rep());mpq_set_si(&rep(),si,1);}
32 //----------------------------------------------------------------------
33 template<> inline
34 scalar<MPQ>::scalar (unsigned si)
35  {mpq_init(&rep());mpq_set_ui(&rep(),si,1);}
36 //----------------------------------------------------------------------
37 template<> inline
38 scalar<MPQ>::scalar (const char *string, unsigned int base)
39 {
40  MP_INT tmp;
41  mpz_init_set_str(&tmp, string, base);
42  mpq_init(&rep());
43  mpq_set_z(&rep(),&tmp);
44 }
45 //----------------------------------------------------------------------
46 template<> inline
47 scalar<MPQ>::scalar (signed long int sl)
48  {mpq_init(&rep());mpq_set_si(&rep(),sl,1);}
49 //----------------------------------------------------------------------
50 template<> inline
51 scalar<MPQ>::scalar (unsigned long int ul)
52  {mpq_init(&rep());mpq_set_ui(&rep(),ul,1);}
53 //----------------------------------------------------------------------
54 template<> inline
56 {
57  mpz_init_set(&rep()._mp_num, &rhs.rep()._mp_num);
58  mpz_init_set(&rep()._mp_den, &rhs.rep()._mp_den);
59 }
60 //----------------------------------------------------------------------
61 template<> inline
62 scalar<MPQ>::scalar(double rhs)
63 {
64  mpq_init(&rep());
65  mpq_set_d(&rep(), rhs);
66 }
67 //----------------------------------------------------------------------
68 template<> inline
70 {
71  // COUNT<MPQ>('=');
72  //if (this != &rhs)
73  //mpq_clear(&rep());
74  //mpq_init(&rep());
75  mpq_set(&rep(), &rhs.rep());
76  return *this;
77 }
78 //----------------------------------------------------------------------
79 template<> inline
80 bool scalar<MPQ>::operator == (const scalar<MPQ>& rhs) const
81 {
82  return mpq_cmp(&rep(), &rhs.rep()) == 0;
83 }
84 
85 template<> inline
86 bool scalar<MPQ>::operator == (long sl) const {
87  assert(sl>0);
88  return mpq_cmp_ui(&rep(), sl, 1) == 0;
89 
90 }
91 
92 template<> inline
93 bool scalar<MPQ>::operator == (int si) const {
94  return mpq_cmp_ui(&rep(), (long) si, 1) == 0;
95 }
96 
97 template<> inline
98 bool scalar<MPQ>::operator == (unsigned long ul) const
99 {
100  return mpq_cmp_ui(&rep(), ul, 1) == 0;
101 }
102 
103 // != ------------------------------------------------------------------
104 template<> inline
106 {
107  return mpq_cmp(&rep(), &rhs.rep()) != 0;
108 }
109 
110 template<> inline
111 bool scalar<MPQ>::operator != (long sl) const {
112  assert(sl>=0);
113  return mpq_cmp_ui(&rep(), sl,1) != 0;
114 }
115 
116 template<> inline
117 bool scalar<MPQ>::operator != (int si) const {
118  assert(si>=0);
119  return mpq_cmp_ui(&rep(), (long) si,1) != 0;
120 }
121 
122 template<> inline
123 bool scalar<MPQ>::operator != (unsigned long ul) const
124 {
125  return mpq_cmp_ui(&rep(), ul,1) != 0;
126 }
127 // > -------------------------------------------------------------------
128 template<> inline
129 bool scalar<MPQ>::operator > (const scalar<MPQ>& rhs) const
130 {
131  return mpq_cmp(&rep(), &rhs.rep()) > 0;
132 }
133 
134 template<> inline
135 bool scalar<MPQ>::operator > (long sl) const {
136  assert(sl>=0);
137  return mpq_cmp_ui(&rep(), sl, 1) > 0;
138 }
139 
140 template<> inline
141 bool scalar<MPQ>::operator > (int si) const {
142  assert(si>=0);
143  return mpq_cmp_ui(&rep(), (long) si, 1) > 0;
144 }
145 
146 template<> inline
147 bool scalar<MPQ>::operator > (unsigned long ul) const
148 {
149  return mpq_cmp_ui(&rep(), ul,1) > 0;
150 }
151 // <= --------------------------------------------------------------------
152 template<> inline
154 {
155  return mpq_cmp(&rep(), &rhs.rep()) >= 0;
156 }
157 
158 template<> inline
159 bool scalar<MPQ>::operator >= (long sl) const {
160  assert(sl>=0);
161  return mpq_cmp_ui(&rep(), sl, 1) >= 0;
162 }
163 
164 template<> inline
165 bool scalar<MPQ>::operator >= (int si) const
166 {
167  assert(si>=0);
168  return mpq_cmp_ui(&rep(), (long) si, 1) >= 0;
169 }
170 
171 template<> inline
172 bool scalar<MPQ>::operator >= (unsigned long ul) const
173 {
174  return mpq_cmp_ui(&rep(), ul, 1) >= 0;
175 }
176 
177 // < -------------------------------------------------------------------
178 template<> inline
179 bool scalar<MPQ>::operator < (const scalar<MPQ>& rhs) const
180 {
181  return mpq_cmp(&rep(), &rhs.rep()) < 0;
182 }
183 
184 template<> inline
185 bool scalar<MPQ>::operator < (long sl) const {
186  return mpq_cmp_ui(&rep(), sl, 1) < 0;
187 }
188 
189 template<> inline
190 bool scalar<MPQ>::operator < (int si) const
191 {
192  assert(si>=0);
193  return mpq_cmp_ui(&rep(), (long) si, 1) < 0;
194 }
195 
196 template<> inline
197 bool scalar<MPQ>::operator < (unsigned long ul) const
198 {
199  return mpq_cmp_ui(&rep(), ul, 1) < 0;
200 }
201 
202 // <= ------------------------------------------------------------------
203 
204 template<> inline
206 {
207  return mpq_cmp(&rep(), &rhs.rep()) <= 0;
208 }
209 
210 template<> inline
211 bool scalar<MPQ>::operator <= (long sl) const {
212  assert(sl>=0);
213  return mpq_cmp_ui(&rep(), sl,1) <= 0;
214 }
215 
216 template<> inline
217 bool scalar<MPQ>::operator <= (int si) const
218 {
219  assert(si>=0);
220  return mpq_cmp_ui(&rep(), (long) si, 1) <= 0;
221 }
222 
223 template<> inline
224 bool scalar<MPQ>::operator <= (unsigned long ul) const
225 {
226  return mpq_cmp_ui(&rep(), ul, 1) <= 0;
227 }
228 
229 // = -------------------------------------------------------------------
230 template<> inline
232 {
233  mpq_set_ui(&rep(), ul, 1); return *this;
234 }
235 
236 template<> inline
238 {
239  mpq_set_si(&rep(), sl, 1); return *this;
240 }
241 
242 template<> inline
244 {
245  mpq_set_si(&rep(), ul, 1); return *this;
246 }
247 // += -------------------------------------------------------------------
248 template<> inline
250 {
251  scalar<MPQ> tmp;
252  mpq_add(&tmp.rep(), &rep(), &rhs.rep());
253  mpq_swap(&tmp.rep(),&rep());
254  // mpq_canonicalize(&rep());
255  return *this;
256 }
257 
258 template<> inline
260 {
261  mpq_add(&rep(), &rep(), &scalar<MPQ>(ul).rep());
262  // mpq_canonicalize(&rep());
263  return *this;
264 }
265 
266 template<> inline
268 {
269  mpq_add(&rep(), &rep(), &scalar<MPQ>(sl).rep());
270  //mpq_canonicalize(&rep());
271  return *this;
272 }
273 
274 template<> inline
276 {
277  scalar<MPQ> tmp(ul);
278  *this += tmp;
279  //mpq_canonicalize(&rep());
280  return *this;
281 }
282 
283 // -= -------------------------------------------------------------------
284 template<> inline
286 {
287  mpq_sub(&rep(), &rep(), &rhs.rep());
288  //mpq_canonicalize(&rep());
289  return *this;
290 }
291 
292 template<> inline
294 {
295  mpq_sub(&rep(), &rep(), &scalar<MPQ>(ul).rep());
296  //mpq_canonicalize(&rep());
297  return *this;
298 }
299 
300 template<> inline
302 {
303  mpq_sub(&rep(), &rep(), &scalar<MPQ>(sl).rep());
304  //mpq_canonicalize(&rep());
305  return *this;
306 }
307 
308 template<> inline
310 {
311  *this -= scalar<MPQ>(ul);
312  //mpq_canonicalize(&rep());
313  return *this;
314 }
315 // *= -----------------------------------------------------------------
316 template<> inline
318 {
319  mpq_mul(&rep(), &rep(), &rhs.rep());
320  return *this;
321 }
322 
323 template<> inline
325 {
326  mpq_mul(&rep(), &rep(), &scalar<MPQ>(ul).rep());
327  return *this;
328 }
329 template<> inline
331 {
332  mpq_mul(&rep(), &rep(), &scalar<MPQ>(ul).rep());
333  return *this;
334 }
335 template<> inline
337 {
338  mpq_mul(&rep(), &rep(), &scalar<MPQ>(sl).rep());
339  //mpq_canonicalize(&rep());
340  return *this;
341 }
342 
343 // /= ------------------------------------------------------------------
344 template<> inline
346 {
347  mpq_div(&rep(), &rep(), &rhs.rep());
348  return *this;
349 }
350 
351 template<> inline
353 {
354  mpq_div(&rep(), &rep(), &scalar<MPQ>(sl).rep());
355  return *this;
356 }
357 
358 template<> inline
360 {
361  mpq_div(&rep(), &rep(), &scalar<MPQ>(sl).rep());
362  return *this;
363 }
364 
365 template<> inline
367 {
368  mpq_div(&rep(), &rep(), &scalar<MPQ>(ul).rep());
369  return *this;
370 }
371 
372 template<> inline
374 {
375 
376  mpq_div(&rep(), &rep(), &scalar<MPQ>(sl).rep());
377  //mpq_canonicalize(&rep());
378  return *this;
379 }
380 
381 //======================================================================
382 // ARITHMETIC OPERATORS
383 //======================================================================
384 inline
386 {
387  scalar<MPQ> result;
388  mpq_add(&result.rep(), &a1.rep(), &a2.rep());
389  //mpq_canonicalize(&result.rep());
390  return result;
391 }
392 //----------------------------------------------------------------------
393 inline
395 {
396  scalar<MPQ> result;
397  mpq_sub(&result.rep(), &a1.rep(), &a2.rep());
398  //mpq_canonicalize(&result.rep());
399  return result;
400 }
401 inline
403 {
404  scalar<MPQ> r; mpq_neg(&r.rep(), &a1.rep()); return r;
405 }
406 //----------------------------------------------------------------------
407 inline
409 {
410  // static
411  scalar<MPQ> result;
412  // if(mpq_cmp_ui(&a1.rep(),0ul,1ul) && mpq_cmp_ui(&a2.rep(),0ul,1ul))
413  mpq_mul(&result.rep(), &a1.rep(), &a2.rep());
414  return result;
415 }
416 
417 inline
419 {
420  return a1*scalar<MPQ>(a2);
421 }
422 
423 inline scalar<MPQ> operator *(const scalar<MPZ>& a1, const scalar<MPQ>& a2)
424 {
425  return a2*scalar<MPQ>(a1);
426 }
427 //--------------------------------------------------------------------
428 inline
429 scalar<MPQ> operator *(const scalar<MPQ>& a2, int a1)
430 {
431  return a2 * scalar<MPQ>(a1);
432 }
433 
434 inline
435 scalar<MPQ> operator *(int a1, const scalar<MPQ>& a2)
436 {
437  return a2 * scalar<MPQ>(a1);
438 }
439 
440 inline
442 {
443  scalar<MPQ> result;
444  mpq_div(&result.rep(), &a1.rep(), &a2.rep());
445  return result;
446 }
447 
448 inline
450 {
451  return a1/scalar<MPQ>(a2);
452 }
453 //======================================================================
455 template<> inline
457 {
458  mpq_add(&rep(), &rep(), &scalar<MPQ>(1).rep());
459  //mpq_canonicalize(&rep());
460 }
461 //----------------------------------------------------------------------
463 template<> inline
465 {
466  mpq_sub(&rep(), &rep(), &scalar<MPQ>(1).rep());
467  //mpq_canonicalize(&rep());
468 }
469 //======================================================================
470 // INPUT OUTPUT
471 //======================================================================
472 inline
473 std::ostream& operator << (std::ostream& os, const scalar<MPQ>& b)
474 {
475  mpq_out_str(stdout, 10, &b.rep()); //MP_INT tmp; //mpz_init(&tmp);
476  return os;
477 }
478 
479 inline char* as_charp( const scalar<MPQ>& b) {
480  return mpq_get_str(NULL,10,&b.rep());
481 }
482 
483 inline std::string to_string( const scalar<MPQ>& b) {
484  char * str= mpq_get_str(NULL,10,&b.rep());
485  return std::string(str);
486 }
487 
488 template<class OSTREAM> inline void
489 print(OSTREAM& os, const scalar<MPQ>& b) {
490  os<<as_charp(b);
491 }
492 //----------------------------------------------------------------------
493 inline
494 std::istream& operator >> (std::istream& is, scalar<MPQ>& b)
495 {
496  std::string s;is >> s;
497  MP_INT tmp;
498  mpz_init_set_str(&tmp,s.c_str(), 10);
499  mpq_init(&b.rep());
500  mpq_set_z(&b.rep(),&tmp);
501  return is;
502 }
503 //======================================================================
504 // SPECIAL FUNCTIONS
505 //======================================================================
506 template<> inline void
508 {
509  if (rep()._mp_num._mp_size < 0) rep()._mp_num._mp_size = - rep()._mp_num._mp_size;
510 }
511 
512 template<> inline
514 {
515  rep()._mp_num._mp_size = - rep()._mp_num._mp_size; return *this;
516 }
517 
518 inline scalar<MPZ> numerator(const scalar<MPQ>& q) { scalar<MPZ> r; mpq_get_num(&r.rep(),&q.rep()); return r;}
519 inline scalar<MPZ> denominator(const scalar<MPQ>& q) { scalar<MPZ> r; mpq_get_den(&r.rep(),&q.rep()); return r;}
520 inline scalar<MPZ> rfloor (const scalar<MPQ>& q) { scalar<MPZ> r=numerator(q); r/=denominator(q); return r; }
521 inline scalar<MPZ> rceil (const scalar<MPQ>& q) { return rfloor(q)+1; }
522 //-------------------------- compare (...) ------------------------------------
523 inline int compare (const scalar<MPQ>& b1, const scalar<MPQ>& b2)
524 {
525  return mpq_cmp(&b1.rep(), &b2.rep());
526 }
527 
528 inline int compare (const scalar<MPQ>& b, unsigned long ul)
529 {
530  return mpq_cmp_ui(&b.rep(), ul, 1);
531 }
532 
533 inline int compare (const scalar<MPQ>& b, long sl)
534 {
535  assert(sl>=0);
536  return mpq_cmp_ui(&b.rep(), sl, 1);
537 }
538 
539 inline scalar<MPQ> min(const scalar<MPQ>& a1, const scalar<MPQ>& a2) {return (a1<a2?a1:a2);}
540 inline scalar<MPQ> max(const scalar<MPQ>& a1, const scalar<MPQ>& a2) {return (a1>a2?a1:a2);}
541 
542 //----------------------------------------------------------------------
543 inline void convert (scalar<MPQ>& dd, char *s)
544 {
545  mpq_clear(&dd.rep());
546  mpq_init(&dd.rep());
547  mpz_set_str(mpq_numref(&dd.rep()),s,10);
548 }
549 //----------------------------------------------------------------------
550 inline scalar<MPQ> Size(const scalar<MPQ> & r) { return abs(r); };
551 
552 namespace let
553 {
554  inline void assign(scalar<MPQ> & x, char *s) { mpq_set_str(&x.rep(), s, 10);}
555  inline void assign(scalar<MPQ> & q, double d ) { mpq_set_d(&q.rep(),d); }
556  inline void assign(double & d, const scalar<MPQ>& x )
557  {
558  d = mpq_get_d(&x.rep());
559  }
560 }
561 //--------------------------------------------------------------------
562 
563 inline double to_double(const scalar<MPQ>& a) { return mpq_get_d(&a.rep()); }
564 inline double as_double(const scalar<MPQ>& a) { return mpq_get_d(&a.rep()); }
565 
566 template<typename T,typename F> struct as_helper;
567 
568 template<> struct as_helper<double,scalar<MPQ> > {
569  static inline double cv(const scalar<MPQ>& x) {double r; let::assign(r,x); return r;}
570 };
571 
572 } //namespace mmx
573 //======================================================================
574 #endif // //SCL_MPQ_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
scalar< T > & operator=(const scalar< T > &rhs)
bool operator>=(const scalar< T > &rhs) const
QQ Size(const QQ &r)
Definition: GMPXX.hpp:64
Definition: assign.hpp:48
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)
double to_double(const RR &a)
Definition: GMP.hpp:66
int compare(const QQ &a, const QQ &b)
Definition: GMPXX.hpp:71
std::string to_string(int x)
to_string convert int to std::string In C++11, it should use std::to_string
Definition: assign.hpp:24
extended< NT > operator+(const extended< NT > &lhs, const extended< NT > &rhs)
Definition: extended.hpp:122
MP_RAT MPQ
Definition: scalar_rational.hpp:17
bool operator<(const scalar< T > &rhs) const
bool operator<=(const scalar< T > &rhs) const
bool operator!=(const scalar< T > &rhs) const
Interval< T, r > min(const Interval< T, r > &a, const Interval< T, r > &b)
Definition: Interval_fcts.hpp:130
scalar< T > & operator*=(const scalar< T > &rhs)
ZZ numerator(const QQ &a)
Definition: GMPXX.hpp:95
scalar< T > & negate()
void operator++()
double rceil(double x)
Definition: scalar.hpp:536
scalar< T > & operator/=(const scalar< T > &rhs)
char * as_charp(const scalar< MPZ > &b)
Definition: scalar_integer.hpp:444
void abs(Interval< C, r > &x, const Interval< C, r > &a)
Definition: Interval_fcts.hpp:185
void print(OSTREAM &os, const Interval< T, r > &a)
Definition: Interval.hpp:135
bool operator==(const scalar< T > &rhs) const
MP_INT MPZ
Definition: scalar_integer.hpp:18
static double cv(const scalar< MPQ > &x)
Definition: scalar_rational.hpp:569
ZZ denominator(const QQ &a)
Definition: GMPXX.hpp:96
std::istream & operator>>(std::istream &is, scalar< MPF > &b)
Definition: scalar_floating.hpp:453
void operator--()
void convert(scalar< MPF > &n, char *s)
Definition: scalar_floating.hpp:433
void assign(A &a, const B &b)
Generic definition of the assignement function.
Definition: assign.hpp:97
Interval< T, r > max(const Interval< T, r > &a, const Interval< T, r > &b)
Definition: Interval_fcts.hpp:135
Definition: array.hpp:12
double rfloor(double x)
Definition: scalar.hpp:535
extended< NT > operator*(const extended< NT > &lhs, const extended< NT > &rhs)
Definition: extended.hpp:101
double as_double(const RR &a)
Definition: GMP.hpp:67
Definition: scalar.hpp:24
~scalar()
Definition: scalar.hpp:55
#define assert(expr, msg)
Definition: shared_object.hpp:57
Home