My Project  UNKNOWN_GIT_VERSION
GMPrat.cc
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // GMPrat.cc
3 // begin of file
4 // originally written by Gerd Sussner, sussner@mi.uni-erlangen.de
5 // copied by Stephan Endrass, endrass@mathematik.uni-mainz.de
6 // 23.7.99
7 // ----------------------------------------------------------------------------
8 
9 #define GMPRAT_CC
10 
11 
12 
13 
14 #include "kernel/mod2.h"
15 
16 #ifdef HAVE_SPECTRUM
17 
18 #ifdef GMPRAT_PRINT
19 #include <iostream.h>
20 #ifndef GMPRAT_IOSTREAM
21 #include <stdio.h>
22 #endif
23 #endif
24 
25 #include "omalloc/omalloc.h"
26 #include "kernel/spectrum/GMPrat.h"
27 
28 // ----------------------------------------------------------------------------
29 // disconnect a rational from its reference
30 // ----------------------------------------------------------------------------
31 
33 {
34  if( p->n>1)
35  {
36  rep *old_p = p;
37  p->n--;
38  p = new rep;
39  mpq_init(p->rat);
40  mpq_set(p->rat, old_p->rat);
41  }
42 }
43 
44 // ----------------------------------------------------------------------------
45 // Constructors
46 // ----------------------------------------------------------------------------
47 
49 {
50  p = new rep;
51  mpq_init( p->rat );
52 }
53 
55 {
56  p = new rep;
57  mpq_init( p->rat );
58  mpq_set_si( p->rat,(long)a,1 );
59 }
60 
62 {
63  a.p->n++;
64  p=a.p;
65 }
66 
67 // ----------------------------------------------------------------------------
68 // Constructors with two arguments: numerator and denominator
69 // ----------------------------------------------------------------------------
70 
72 {
73  p=new rep;
74  mpq_init(p->rat);
75  mpq_div(p->rat, a.p->rat, b.p->rat);
76 }
77 
78 Rational::Rational(int a, int b)
79 {
80  if (b<0) a=-a;
81  p=new rep;
82  mpq_init(p->rat);
83  mpq_set_si(p->rat,(long) a,(unsigned long) abs(b));
84  mpq_canonicalize(p->rat);
85 }
86 
87 // ----------------------------------------------------------------------------
88 // Destructor
89 // ----------------------------------------------------------------------------
90 
92 {
93  if (--(p->n)==0)
94  {
95  mpq_clear(p->rat);
96  delete p;
97  }
98 }
99 
100 // ----------------------------------------------------------------------------
101 // Assignment operators
102 // ----------------------------------------------------------------------------
103 
105 {
106  if( p->n>1)
107  {
108  p->n--;
109  p = new rep;
110  mpq_init(p->rat);
111  }
112  mpq_set_si(p->rat,(long) a,1);
113  return *this;
114 }
115 
117 {
118  a.p->n++;
119  if (--(p->n)==0)
120  {
121  mpq_clear(p->rat);
122  delete p;
123  }
124  p=a.p;
125  return *this;
126 }
127 
128 // ----------------------------------------------------------------------------
129 // Numerator and denominator
130 // ----------------------------------------------------------------------------
131 
133 {
134  Rational erg;
135 
136  mpq_set_num( erg.p->rat,mpq_numref( p->rat ) );
137 
138  return erg;
139 }
140 
142 {
143  return mpz_get_si( mpq_numref( p->rat ) );
144 }
145 
147 {
148  Rational erg;
149 
150  mpq_set_num( erg.p->rat,mpq_denref( p->rat ) );
151 
152  return erg;
153 }
154 
156 {
157  return mpz_get_si( mpq_denref( p->rat ) );
158 }
159 
160 // ----------------------------------------------------------------------------
161 // Casting
162 // ----------------------------------------------------------------------------
163 
164 Rational::operator int()
165 {
166  mpz_t h;
167  long ret_val;
168 
169  mpz_init(h);
170  mpz_tdiv_q(h,mpq_numref(p->rat),mpq_denref(p->rat));
171  ret_val=mpz_get_si(h);
172  mpz_clear(h);
173 
174  return ret_val;
175 }
176 
177 // ----------------------------------------------------------------------------
178 // Unary minus
179 // ----------------------------------------------------------------------------
180 
181 Rational
183 {
184  Rational erg;
185 
186  mpq_neg(erg.p->rat,p->rat);
187  return erg;
188 }
189 
191 {
192  Rational erg;
193 
194  mpq_neg(erg.p->rat,r.p->rat);
195  return erg;
196 }
197 
198 // ----------------------------------------------------------------------------
199 // Inverse
200 // ----------------------------------------------------------------------------
201 
202 Rational
204 {
205  Rational erg;
206 
207  mpq_inv(erg.p->rat,p->rat);
208  return erg;
209 }
210 
211 // ----------------------------------------------------------------------------
212 // +=, -= ...
213 // ----------------------------------------------------------------------------
214 
215 Rational&
217 {
218  disconnect();
219  mpq_add(p->rat,p->rat,a.p->rat);
220  return *this;
221 }
222 
223 Rational&
225 {
226  disconnect();
227  mpq_sub(p->rat,p->rat,a.p->rat);
228  return *this;
229 }
230 
231 Rational&
233 {
234  disconnect();
235  mpq_mul(p->rat,p->rat,a.p->rat);
236  return *this;
237 }
238 
239 Rational&
241 {
242  disconnect();
243  mpq_div(p->rat,p->rat,a.p->rat);
244  return *this;
245 }
246 
247 // ----------------------------------------------------------------------------
248 // Increment and decrement
249 // ----------------------------------------------------------------------------
250 
251 Rational&
253 {
254  disconnect();
255  mpz_add(mpq_numref(p->rat), mpq_numref(p->rat), mpq_denref(p->rat));
256  return *this;
257 }
258 
259 Rational
261 {
262  Rational erg(*this);
263 
264  disconnect();
265  mpz_add(mpq_numref(p->rat), mpq_numref(p->rat), mpq_denref(p->rat));
266  return erg;
267 }
268 
269 Rational&
271 {
272  disconnect();
273  mpz_sub(mpq_numref(p->rat), mpq_numref(p->rat), mpq_denref(p->rat));
274  return *this;
275 }
276 
277 Rational
279 {
280  Rational erg(*this);
281 
282  disconnect();
283  mpz_sub(mpq_numref(p->rat), mpq_numref(p->rat), mpq_denref(p->rat));
284  return erg;
285 }
286 
287 // ----------------------------------------------------------------------------
288 // Relational operators
289 // ----------------------------------------------------------------------------
290 
291 bool operator<(const Rational& a,const Rational& b)
292 {
293  if (mpq_cmp(a.p->rat,b.p->rat)<0) return true;
294  return false;
295 }
296 
297 bool operator<=(const Rational& a,const Rational& b)
298 {
299  if (mpq_cmp(a.p->rat,b.p->rat)>0) return false;
300  return true;
301 }
302 
303 bool operator>(const Rational& a,const Rational& b)
304 {
305  if (mpq_cmp(a.p->rat,b.p->rat)>0) return true;
306  return false;
307 }
308 
309 bool operator>=(const Rational& a,const Rational& b)
310 {
311  if (mpq_cmp(a.p->rat,b.p->rat)<0) return false;
312  return true;
313 }
314 
315 bool operator==(const Rational& a,const Rational& b)
316 {
317  if (mpq_equal(a.p->rat,b.p->rat)) return true;
318  return false;
319 }
320 
321 bool operator!=(const Rational& a,const Rational& b)
322 {
323  if (mpq_equal(a.p->rat,b.p->rat)) return false;
324  return true;
325 }
326 
327 // ----------------------------------------------------------------------------
328 // Ostream
329 // ----------------------------------------------------------------------------
330 
331 #ifdef GMPRAT_PRINT
332 ostream& operator<< (ostream& s,const Rational& a)
333 {
334  char *snum,*sdenom;
335 
336  snum = mpz_get_str( NULL,10,mpq_numref(a.p->rat) );
337  sdenom = mpz_get_str( NULL,10,mpq_denref(a.p->rat) );
338 
339  if( sdenom[0] == '1' && sdenom[1] == '\0' )
340  {
341  #ifdef GMPRAT_IOSTREAM
342  s << snum;
343  #else
344  fprintf( stdout,snum );
345  #endif
346  }
347  else
348  {
349  #ifdef GMPRAT_IOSTREAM
350  s << snum << "/" << sdenom;
351  #else
352  fprintf( stdout,snum );
353  fprintf( stdout,"/" );
354  fprintf( stdout,sdenom );
355  #endif
356  }
357 
358  //free( snum );
359  //free( sdenom );
360 
361  return s;
362 }
363 #endif
364 
365 unsigned int Rational::length( ) const
366 {
367  char *snum = (char*)omAlloc(mpz_sizeinbase(mpq_numref(p->rat),10)+2);
368  char *sden = (char*)omAlloc(mpz_sizeinbase(mpq_denref(p->rat),10)+2);
369 
370  snum = mpz_get_str( snum,10,mpq_numref( p->rat ) );
371  sden = mpz_get_str( sden,10,mpq_denref( p->rat ) );
372 
373  int length = strlen( snum );
374 
375  if( sden[0] != '1' || sden[1] != '\0' ) length += strlen( sden ) + 1;
376 
377  omFree( snum );
378  omFree( sden );
379 
380  return length;
381 }
382 
383 // ----------------------------------------------------------------------------
384 // Operators
385 // ----------------------------------------------------------------------------
386 
387 Rational
388 operator+(const Rational& a,const Rational &b)
389 {
390  Rational
391  erg(a);
392 
393  return erg+=b;
394 }
395 
396 Rational
397 operator-(const Rational& a,const Rational &b)
398 {
399  Rational
400  erg(a);
401 
402  return erg-=b;
403 }
404 
405 Rational
406 operator*(const Rational& a,const Rational &b)
407 {
408  Rational
409  erg(a);
410 
411  return erg*=b;
412 }
413 
414 Rational pow( const Rational& a,int e )
415 {
416  Rational erg(1);
417 
418  for( int i=0; i<e; i++ )
419  {
420  erg *= a;
421  }
422  return erg;
423 }
424 
426 {
427  Rational
428  erg(a);
429 
430  return erg/=b;
431 }
432 
433 int sgn(const Rational& a)
434 {
435  return mpq_sgn(a.p->rat);
436 }
437 
438 Rational
439 abs(const Rational& a)
440 {
441  Rational
442  erg;
443 
444  if (mpq_sgn(a.p->rat)<0)
445  mpq_neg(erg.p->rat,a.p->rat);
446  else
447  mpq_set(erg.p->rat,a.p->rat);
448  return erg;
449 }
450 
451 Rational gcd( const Rational &a,const Rational &b )
452 {
453  if( a == 0 )
454  {
455  if( b == 0 )
456  {
457  return (Rational)1;
458  }
459  else
460  {
461  return abs( b );
462  }
463  }
464  else if( b == 0 )
465  {
466  return abs( a );
467  }
468 
469  Rational erg;
470 
471  mpz_gcd( mpq_numref( erg.p->rat ),
472  mpq_numref( a.p->rat ),mpq_numref( b.p->rat ) );
473  mpz_gcd( mpq_denref( erg.p->rat ),
474  mpq_denref( a.p->rat ),mpq_denref( b.p->rat ) );
475 
476  //mpq_canonicalize( erg.p->rat );
477 
478  return abs( erg );
479 }
480 
481 Rational gcd( Rational *a,int n )
482 {
483  if( n == 1 )
484  {
485  return a[0];
486  }
487 
488  Rational g = gcd( a[0],a[1] );
489 
490  for( int i=2; i<n; i++ )
491  {
492  g = gcd( g,a[i] );
493  }
494 
495  return g;
496 }
497 
498 Rational lcm( const Rational &a,const Rational &b )
499 {
500  if( a == 0 )
501  {
502  return b;
503  }
504  else if( b == 0 )
505  {
506  return a;
507  }
508 
509  return a*b/gcd(a,b);
510 }
511 
512 Rational lcm( Rational *a,int n )
513 {
514  if( n == 1 )
515  {
516  return a[0];
517  }
518 
519  Rational g = lcm( a[0],a[1] );
520 
521  for( int i=2; i<n; i++ )
522  {
523  g = lcm( g,a[i] );
524  }
525 
526  return g;
527 }
528 
529 double Rational::complexity( ) const
530 {
531  double num = mpz_get_d( mpq_numref( p->rat ) );
532  double den = mpz_get_d( mpq_denref( p->rat ) );
533 
534  if( num < 0 ) num = -num;
535  if( den < 0 ) den = -den;
536 
537  return ( num > den ? num : den );
538 }
539 
540 #endif /* HAVE_SPECTRUM */
541 // ----------------------------------------------------------------------------
542 // GMPrat.cc
543 // end of file
544 // ----------------------------------------------------------------------------
Rational::operator=
Rational & operator=(int)
Definition: GMPrat.cc:104
omalloc.h
operator/
Rational operator/(const Rational &a, const Rational &b)
Definition: GMPrat.cc:425
Rational::disconnect
void disconnect()
Definition: GMPrat.cc:32
omFree
#define omFree(addr)
Definition: omAllocDecl.h:261
operator<=
bool operator<=(const Rational &a, const Rational &b)
Definition: GMPrat.cc:297
operator+
Rational operator+(const Rational &a, const Rational &b)
Definition: GMPrat.cc:388
operator*
Rational operator*(const Rational &a, const Rational &b)
Definition: GMPrat.cc:406
num
CanonicalForm num(const CanonicalForm &f)
Definition: canonicalform.h:330
Rational::get_num
Rational get_num()
Definition: GMPrat.cc:132
Rational::operator--
Rational & operator--()
Definition: GMPrat.cc:270
g
g
Definition: cfModGcd.cc:4031
Rational::Rational
Rational()
Definition: GMPrat.cc:48
operator>=
bool operator>=(const Rational &a, const Rational &b)
Definition: GMPrat.cc:309
Rational::rep::n
int n
Definition: GMPrat.h:19
b
CanonicalForm b
Definition: cfModGcd.cc:4044
Rational::length
unsigned int length() const
Definition: GMPrat.cc:365
operator>
bool operator>(const Rational &a, const Rational &b)
Definition: GMPrat.cc:303
i
int i
Definition: cfEzgcd.cc:125
Rational::operator/=
Rational & operator/=(const Rational &)
Definition: GMPrat.cc:240
abs
Rational abs(const Rational &a)
Definition: GMPrat.cc:439
Rational::operator*=
Rational & operator*=(const Rational &)
Definition: GMPrat.cc:232
operator<<
ostream & operator<<(ostream &s, const spectrum &spec)
Definition: semic.cc:249
gcd
Rational gcd(const Rational &a, const Rational &b)
Definition: GMPrat.cc:451
Rational::abs
friend Rational abs(const Rational &)
Definition: GMPrat.cc:439
h
static Poly * h
Definition: janet.cc:972
mod2.h
Rational::operator+=
Rational & operator+=(const Rational &)
Definition: GMPrat.cc:216
omAlloc
#define omAlloc(size)
Definition: omAllocDecl.h:210
Rational::complexity
double complexity() const
Definition: GMPrat.cc:529
den
CanonicalForm den(const CanonicalForm &f)
Definition: canonicalform.h:333
Rational::operator-
Rational operator-()
Definition: GMPrat.cc:182
operator-
Rational operator-(const Rational &r)
Definition: GMPrat.cc:190
Rational::operator++
Rational & operator++()
Definition: GMPrat.cc:252
Rational::get_den_si
int get_den_si()
Definition: GMPrat.cc:155
Rational::get_num_si
int get_num_si()
Definition: GMPrat.cc:141
operator<
bool operator<(const Rational &a, const Rational &b)
Definition: GMPrat.cc:291
Rational::operator~
Rational operator~()
Definition: GMPrat.cc:203
Rational::rep::rat
mpq_t rat
Definition: GMPrat.h:18
pow
Rational pow(const Rational &a, int e)
Definition: GMPrat.cc:414
Rational::rep
Definition: GMPrat.h:16
Rational::p
rep * p
Definition: GMPrat.h:23
NULL
#define NULL
Definition: omList.c:10
Rational::operator-=
Rational & operator-=(const Rational &)
Definition: GMPrat.cc:224
operator!=
bool operator!=(const Rational &a, const Rational &b)
Definition: GMPrat.cc:321
lcm
Rational lcm(const Rational &a, const Rational &b)
Definition: GMPrat.cc:498
p
int p
Definition: cfModGcd.cc:4019
s
const CanonicalForm int s
Definition: facAbsFact.cc:55
Rational::get_den
Rational get_den()
Definition: GMPrat.cc:146
Rational::~Rational
~Rational()
Definition: GMPrat.cc:91
Rational
Definition: GMPrat.h:14
operator==
bool operator==(const Rational &a, const Rational &b)
Definition: GMPrat.cc:315
GMPrat.h
sgn
int sgn(const Rational &a)
Definition: GMPrat.cc:433