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