Borderbasix

Scl_mpq.hpp
Go to the documentation of this file.
1 /*********************************************************************
2 * This file is part of the source code of BORDERBASIX software. *
3 * (C) B. Mourrain, GALAAD, INRIA *
4 **********************************************************************
5 History:
6 $Id: Scl_mpq.hpp,v 1.1.1.1 2006/10/06 08:01:40 trebuche Exp $
7 **********************************************************************/
8 #ifndef _SCL_MPQ_H_
9 #define _SCL_MPQ_H_
10 //----------------------------------------------------------------------
11 #include <limits.h>
12 #include <string.h>
13 #include <stdlib.h>
14 #include <stdio.h>
15 //using namespace std;
16 #include "Scl.hpp"
17 //----------------------------------------------------------------------
18 typedef MP_RAT MPQ;
19 #define _MPQ_
20 std::ostream& operator << (std::ostream& os, const Scl<MPQ>& b);
21 // //----------------------------------------------------------------------
22 // template<> inline
23 // shared_object<MPQ>::rep::~data {mpq_clear(&obj);}
24 //----------------------------------------------------------------------
25 //----------------------------------------------------------------------
26 template<> inline Scl<MPQ>::~Scl ( ) {mpq_clear(&data);}
27 //----------------------------------------------------------------------
28 
29 template<> inline
30 void Scl<MPQ>::init ( ) {mpq_init(&data);}
31 //------------------- Scl<T> (...) -------------------------------------
32 template<> inline
33 Scl<MPQ>::Scl ( ) {mpq_init(&data);}
34 //----------------------------------------------------------------------
35 template<> inline
36 Scl<MPQ>::Scl (signed long int sl)
37 {mpq_init(&data);mpq_set_si(&data,sl,1);}
38 //----------------------------------------------------------------------
39 template<> inline
40 Scl<MPQ>::Scl (unsigned long int ul)
41 {mpq_init(&data);mpq_set_ui(&data,ul,1);}
42 //----------------------------------------------------------------------
43 template<> inline
44 Scl<MPQ>::Scl (int si)
45 {mpq_init(&data);mpq_set_si(&data,si,1);}
46 //----------------------------------------------------------------------
47 template<> inline
48 Scl<MPQ>::Scl (const char *string, unsigned int base)
49 {
50  MP_INT tmp;
51  mpz_init_set_str(&tmp, string, base);
52  mpq_init(&data);
53  mpq_set_z(&data,&tmp);
54 }
55 //----------------------------------------------------------------------
56 template<> inline
57 Scl<MPQ>::Scl(const Scl<MPQ>& rhs)
58 {
59  // COUNT<MPQ>('c');
60  mpz_init_set(&data._mp_num, &rhs.data._mp_num);
61  mpz_init_set(&data._mp_den, &rhs.data._mp_den);
62 }
63 //----------------------------------------------------------------------
64 template<> inline
65 Scl<MPQ>::Scl(double rhs)
66 {
67  // COUNT<MPQ>('c');
68  mpq_init(&data);
69  mpq_set_d(&data, rhs);
70 }
71 //----------------------------------------------------------------------
72 template<> inline
74 {
75  // COUNT<MPQ>('=');
76  //if (this != &rhs)
77  //mpq_clear(&data);
78  //mpq_init(&data);
79  mpq_set(&data, &rhs.data);
80  return *this;
81 }
82 // == ------------------------------------------------------------------
83 template<> inline
84 bool Scl<MPQ>::operator == (const Scl<MPQ>& rhs) const
85 {
86  return mpq_cmp(&data, &rhs.data) == 0;
87 }
88 
89 template<> inline
90 bool Scl<MPQ>::operator == (long sl) const {
91  assert(sl>0);
92  return mpq_cmp_ui(&data, sl, 1) == 0;
93 
94 }
95 
96 template<> inline
97 bool Scl<MPQ>::operator == (int si) const {
98  return mpq_cmp_ui(&data, (long) si, 1) == 0;
99 }
100 
101 template<> inline
102 bool Scl<MPQ>::operator == (unsigned long ul) const
103 {
104  return mpq_cmp_ui(&data, ul, 1) == 0;
105 }
106 
107 // != ------------------------------------------------------------------
108 template<> inline
109 bool Scl<MPQ>::operator != (const Scl<MPQ>& rhs) const
110 {
111  return mpq_cmp(&data, &rhs.data) != 0;
112 }
113 
114 template<> inline
115 bool Scl<MPQ>::operator != (long sl) const {
116  assert(sl>=0);
117  return mpq_cmp_ui(&data, sl,1) != 0;
118 }
119 
120 template<> inline
121 bool Scl<MPQ>::operator != (int si) const {
122  assert(si>=0);
123  return mpq_cmp_ui(&data, (long) si,1) != 0;
124 }
125 
126 template<> inline
127 bool Scl<MPQ>::operator != (unsigned long ul) const
128 {
129  return mpq_cmp_ui(&data, ul,1) != 0;
130 }
131 // > -------------------------------------------------------------------
132 template<> inline
133 bool Scl<MPQ>::operator > (const Scl<MPQ>& rhs) const
134 {
135  return mpq_cmp(&data, &rhs.data) > 0;
136 }
137 
138 template<> inline
139 bool Scl<MPQ>::operator > (long sl) const {
140  assert(sl>=0);
141  return mpq_cmp_ui(&data, sl, 1) > 0;
142 }
143 
144 template<> inline
145 bool Scl<MPQ>::operator > (int si) const {
146  assert(si>=0);
147  return mpq_cmp_ui(&data, (long) si, 1) > 0;
148 }
149 
150 template<> inline
151 bool Scl<MPQ>::operator > (unsigned long ul) const
152 {
153  return mpq_cmp_ui(&data, ul,1) > 0;
154 }
155 // <= --------------------------------------------------------------------
156 template<> inline
157 bool Scl<MPQ>::operator >= (const Scl<MPQ>& rhs) const
158 {
159  return mpq_cmp(&data, &rhs.data) >= 0;
160 }
161 
162 template<> inline
163 bool Scl<MPQ>::operator >= (long sl) const {
164  assert(sl>=0);
165  return mpq_cmp_ui(&data, sl, 1) >= 0;
166 }
167 
168 template<> inline
169 bool Scl<MPQ>::operator >= (int si) const
170 {
171  assert(si>=0);
172  return mpq_cmp_ui(&data, (long) si, 1) >= 0;
173 }
174 
175 template<> inline
176 bool Scl<MPQ>::operator >= (unsigned long ul) const
177 {
178  return mpq_cmp_ui(&data, ul, 1) >= 0;
179 }
180 
181 // < -------------------------------------------------------------------
182 template<> inline
183 bool Scl<MPQ>::operator < (const Scl<MPQ>& rhs) const
184 {
185  return mpq_cmp(&data, &rhs.data) < 0;
186 }
187 
188 template<> inline
189 bool Scl<MPQ>::operator < (long sl) const {
190  return mpq_cmp_ui(&data, sl, 1) < 0;
191 }
192 
193 template<> inline
194 bool Scl<MPQ>::operator < (int si) const
195 {
196  assert(si>=0);
197  return mpq_cmp_ui(&data, (long) si, 1) < 0;
198 }
199 
200 template<> inline
201 bool Scl<MPQ>::operator < (unsigned long ul) const
202 {
203  assert(ul>=0);
204  return mpq_cmp_ui(&data, ul, 1) < 0;
205 }
206 
207 // <= ------------------------------------------------------------------
208 
209 template<> inline
210 bool Scl<MPQ>::operator <= (const Scl<MPQ>& rhs) const
211 {
212  return mpq_cmp(&data, &rhs.data) <= 0;
213 }
214 
215 template<> inline
216 bool Scl<MPQ>::operator <= (long sl) const {
217  assert(sl>=0);
218  return mpq_cmp_ui(&data, sl,1) <= 0;
219 }
220 
221 template<> inline
222 bool Scl<MPQ>::operator <= (int si) const
223 {
224  assert(si>=0);
225  return mpq_cmp_ui(&data, (long) si, 1) <= 0;
226 }
227 
228 template<> inline
229 bool Scl<MPQ>::operator <= (unsigned long ul) const
230 {
231  return mpq_cmp_ui(&data, ul, 1) <= 0;
232 }
233 
234 // = -------------------------------------------------------------------
235 template<> inline
237 {
238  mpq_set_ui(&data, ul, 1); return *this;
239 }
240 
241 template<> inline
243 {
244  mpq_set_si(&data, sl, 1); return *this;
245 }
246 
247 template<> inline
249 {
250  mpq_set_si(&data, ul, 1); return *this;
251 }
252 // += -------------------------------------------------------------------
253 template<> inline
255 {
256  Scl<MPQ> tmp;
257  mpq_add(&tmp.data, &data, &rhs.data);
258  mpq_swap(&tmp.data,&data);
259  // mpq_canonicalize(&data);
260  return *this;
261 }
262 
263 template<> inline
265 {
266  Scl<MPQ> tmp(ul);
267  mpq_add(&data, &data, &tmp.data);
268  // mpq_canonicalize(&data);
269  return *this;
270 }
271 
272 template<> inline
274 {
275  Scl<MPQ> tmp(sl);
276  mpq_add(&data, &data, &tmp.data);
277  //mpq_canonicalize(&data);
278  return *this;
279 }
280 
281 template<> inline
283 {
284  Scl<MPQ> tmp(ul);
285  *this += tmp;
286  //mpq_canonicalize(&data);
287  return *this;
288 }
289 
290 // -= -------------------------------------------------------------------
291 template<> inline
293 {
294  mpq_sub(&data, &data, &rhs.data);
295  //mpq_canonicalize(&data);
296  return *this;
297 }
298 
299 template<> inline
301 {
302  Scl<MPQ> tmp(ul);
303  mpq_sub(&data, &data, &tmp.data);
304  //mpq_canonicalize(&data);
305  return *this;
306 }
307 
308 template<> inline
310 {
311  Scl<MPQ> tmp(sl);
312  mpq_sub(&data, &data, &tmp.data);
313  //mpq_canonicalize(&data);
314  return *this;
315 }
316 
317 template<> inline
319 {
320  *this -= Scl<MPQ>(ul);
321  //mpq_canonicalize(&data);
322  return *this;
323 }
324 // *= -----------------------------------------------------------------
325 template<> inline
327 {
328  //cout<<"operand *= "<<*this<<" "<<rhs<<endl;
329  //if(!(mpq_cmp_ui(&data,0ul,1ul) && mpq_cmp_ui(&rhs.data,0ul,1ul)))
330  //{
331  // mpq_set_ui(&data,0ul,1ul);
332  // }
333  //else
334  // {
335  mpq_mul(&data, &data, &rhs.data);
336  // }
337  // mpq_canonicalize(&data);
338  return *this;
339 }
340 template<> inline
342 {
343  Scl<MPQ> tmp(ul);
344  mpq_mul(&data, &data, &tmp.data);
345  //mpq_canonicalize(&data);
346  return *this;
347 }
348 template<> inline
350 {
351  Scl<MPQ> tmp(sl);
352  mpq_mul(&data, &data, &tmp.data);
353  //mpq_canonicalize(&data);
354  return *this;
355 }
356 
357 template<> inline
359 {
360  Scl<MPQ> tmp(ul);
361  mpq_mul(&data, &data, &tmp.data);
362  //mpq_canonicalize(&data);
363  return *this;
364 }
365 // /= ------------------------------------------------------------------
366 template<> inline
368 {
369  //cout<<"operator /= "<<*this<<" "<<rhs<<endl;
370  mpq_div(&data, &data, &rhs.data);
371  //mpq_divexact(&data, &data, &rhs.data);
372  //mpq_canonicalize(&data);
373  return *this;
374 }
375 template<> inline
377 {
378  Scl<MPQ> tmp(ul);
379  mpq_div(&data, &data, &tmp.data);
380  //mpq_canonicalize(&data);
381  return *this;
382 }
383 
384 template<> inline
386 {
387  Scl<MPQ> tmp(sl);
388  mpq_div(&data, &data, &tmp.data);
389  //mpq_canonicalize(&data);
390  return *this;
391 }
392 
393 //======================================================================
394 // ARITHMETIC OPERATORS
395 //======================================================================
396 Scl<MPQ> operator +(const Scl<MPQ>& a1, const Scl<MPQ>& a2)
397 {
398  Scl<MPQ> result;
399  mpq_add(&result.data, &a1.data, &a2.data);
400  //mpq_canonicalize(&result.data);
401  return result;
402 }
403 //----------------------------------------------------------------------
404 Scl<MPQ> operator -(const Scl<MPQ>& a1, const Scl<MPQ>& a2)
405 {
406  Scl<MPQ> result;
407  mpq_sub(&result.data, &a1.data, &a2.data);
408  //mpq_canonicalize(&result.data);
409  return result;
410 }
411 //----------------------------------------------------------------------
412 Scl<MPQ> operator *(const Scl<MPQ>& a1, const Scl<MPQ>& a2)
413 {
414  static Scl<MPQ> result;
415  // if(mpq_cmp_ui(&a1.data,0ul,1ul) && mpq_cmp_ui(&a2.data,0ul,1ul))
416  mpq_mul(&result.data, &a1.data, &a2.data);
417  //mpq_canonicalize(&result.data);
418  return result;
419 }
420 Scl<MPQ> operator /(const Scl<MPQ>& a1, const Scl<MPQ>& a2)
421 {
422  Scl<MPQ> result;
423  mpq_div(&result.data, &a1.data, &a2.data);
424  //mpq_canonicalize(&result.data);
425  return result;
426 }
427 //======================================================================
428 // ASSIGNEMENTS
429 //======================================================================
430 // inline void assign(Scl<MPQ> & p,
431 // const OP<'+',Scl<MPQ>, Scl<MPQ> >& M)
432 // {
433 // mpq_add(&p.data,&M.op1.data,&M.op2.data);
434 // }
435 // //----------------------------------------------------------------------
436 // inline
437 // void assign(Scl<MPQ> & p,
438 // const OP<'-',Scl<MPQ>, Scl<MPQ> >& M)
439 // {
440 // mpq_sub(&p.data,&M.op1.data,&M.op2.data);
441 // }
442 // //----------------------------------------------------------------------
443 // inline void assign(Scl<MPQ> & p,
444 // const OP<'*',Scl<MPQ>, Scl<MPQ> >& M)
445 // {
446 // mpq_mul(&p.data,&M.op1.data,&M.op2.data);
447 // }
448 // //----------------------------------------------------------------------
449 // void assign(Scl<MPQ> & p,
450 // const OP<'/',Scl<MPQ>,Scl<MPQ> >& M)
451 // {
452 // assert(M.op2!=0);
453 // mpq_div(&p.data,&M.op1.data,&M.op2.data);
454 // }
455 //======================================================================
457 template<> inline
459 {
460  Scl<MPQ> tmp(1);
461  mpq_add(&data, &data, &tmp.data);
462  //mpq_canonicalize(&data);
463 }
464 //----------------------------------------------------------------------
466 template<> inline
468 {
469  Scl<MPQ> tmp(1);
470  mpq_sub(&data, &data, &tmp.data);
471  //mpq_canonicalize(&data);
472 }
473 //======================================================================
474 // INPUT OUTPUT
475 //======================================================================
476 std::ostream& operator << (std::ostream& os, const Scl<MPQ>& b)
477 {
478  size_t len_num= mpz_sizeinbase (&b.data._mp_num, 10) + 2;
479  size_t len_den= mpz_sizeinbase (&b.data._mp_den, 10) + 2;
480  size_t tmp_len= len_num > len_den ? len_num : len_den;
481  char* tmp= new char[tmp_len];
482  tmp= mpz_get_str(tmp,10,&b.data._mp_num);
483  os << tmp;
484  if(mpz_cmp_ui(&b.data._mp_den,1)){
485  os <<"/";
486  tmp=mpz_get_str(tmp, 10, &b.data._mp_den);
487  os<<tmp;
488  }
489  delete[] tmp;
490  return os;
491 }
492 //----------------------------------------------------------------------
493 std::istream& operator >> (std::istream& is, Scl<MPQ>& b)
494 {
495  std::string s;is >> s;
496  MP_INT tmp;
497  mpz_init_set_str(&tmp,s.c_str(), 10);
498  mpq_init(&b.data);
499  mpq_set_z(&b.data,&tmp);
500  return is;
501 }
502 //======================================================================
503 // SPECIAL FUNCTIONS
504 //======================================================================
505 
506 template<> inline void
508 {
509  if (data._mp_num._mp_size < 0) data._mp_num._mp_size = - data._mp_num._mp_size;
510 }
511 
512 template<> inline
514 {
515  data._mp_num._mp_size = - data._mp_num._mp_size; return *this;
516 }
517 
518 //-------------------------- compare (...) ------------------------------------
519 inline int compare (const Scl<MPQ>& b1, const Scl<MPQ>& b2)
520 {
521  return mpq_cmp(&b1.data, &b2.data);
522 }
523 
524 inline int compare (const Scl<MPQ>& b, unsigned long ul)
525 {
526  return mpq_cmp_ui(&b.data, ul, 1);
527 }
528 
529 inline int compare (const Scl<MPQ>& b, long sl)
530 {
531  assert(sl>=0);
532  return mpq_cmp_ui(&b.data, sl, 1);
533 }
534 //----------------------------------------------------------------------
535 //inline void convert (Scl<MPQ>& dd, char *s)
536 //{
537 // //exit(-10);
538 // //double tmp=atof(s);
539 // //mpq_clear(&dd.data);
540 // //mpq_init(&dd.data);
541 // //mpz_init_set_str(mpq_numref(&dd.data),s,10);
542 // //mpq_set_d(&dd.data,tmp);
543 // //mpq_canonicalize(&dd.data);
544 
545 // mpq_clear(&dd.data);
546 // mpq_init(&dd.data);
547 // mpz_set_str(mpq_numref(&dd.data),s,10);
548 //}
549 
551  return (x>0);
552 }
553 #endif // //SCL_MPQ_H
bool has_positive_sign(const Scl< MPQ > &x)
Definition: Scl_mpq.hpp:550
Scl< MPQ > operator+(const Scl< MPQ > &a1, const Scl< MPQ > &a2)
Definition: Scl_mpq.hpp:396
Scl< T > & negate()
Scl< MPQ > operator-(const Scl< MPQ > &a1, const Scl< MPQ > &a2)
Definition: Scl_mpq.hpp:404
T data
Definition: Scl.hpp:34
Scl< MPQ > operator/(const Scl< MPQ > &a1, const Scl< MPQ > &a2)
Definition: Scl_mpq.hpp:420
Scl< MPQ > operator*(const Scl< MPQ > &a1, const Scl< MPQ > &a2)
Definition: Scl_mpq.hpp:412
Scl< T > operator-=(const Scl< T > &rhs)
MSKaccmodee MSKint32t MSKsoltypee MSKstakeye MSKrealt MSKrealt * sl
Definition: mosek.h:3209
Scl< T > operator+=(const Scl< T > &rhs)
bool operator>=(const Scl< T > &rhs) const
void operator--()
MSKaccmodee MSKint32t MSKsoltypee MSKstakeye MSKrealt * x
Definition: mosek.h:3209
Scl< T > operator*=(const Scl< T > &rhs)
void operator++()
bool operator==(const Scl< T > &rhs) const
Scl()
Definition: Scl.hpp:38
std::istream & operator>>(std::istream &is, Scl< MPQ > &b)
Definition: Scl_mpq.hpp:493
bool operator<=(const Scl< T > &rhs) const
Scl< T > operator/=(const Scl< T > &rhs)
Definition: Scl.hpp:26
Scl< T > operator=(const Scl< T > &rhs)
bool operator>(const Scl< T > &rhs) const
MP_RAT MPQ
Definition: Scl_mpq.hpp:18
void abs()
~Scl()
Definition: Scl.hpp:49
int compare(const Scl< MPQ > &b1, const Scl< MPQ > &b2)
Definition: Scl_mpq.hpp:519
bool operator!=(const Scl< T > &rhs) const
bool operator<(const Scl< T > &rhs) const
Home  |  Download & InstallContributions