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