My Project
Loading...
Searching...
No Matches
Data Structures | Public Member Functions | Private Member Functions | Private Attributes | Friends
Rational Class Reference

#include <GMPrat.h>

Data Structures

struct  rep
 

Public Member Functions

 Rational ()
 
 Rational (int)
 
 Rational (const Rational &)
 
 Rational (const Rational &, const Rational &)
 
 Rational (int, int)
 
 ~Rational ()
 
Rationaloperator= (int)
 
Rationaloperator= (char *s)
 
Rationaloperator= (const Rational &)
 
unsigned int length () const
 
Rational get_num ()
 
Rational get_den ()
 
int get_num_si ()
 
int get_den_si ()
 
 operator int ()
 
Rational operator- ()
 
Rational operator~ ()
 
Rationaloperator+= (const Rational &)
 
Rationaloperator-= (const Rational &)
 
Rationaloperator*= (const Rational &)
 
Rationaloperator/= (const Rational &)
 
Rationaloperator++ ()
 
Rational operator++ (int)
 
Rationaloperator-- ()
 
Rational operator-- (int)
 
double complexity () const
 

Private Member Functions

void disconnect ()
 

Private Attributes

repp
 

Friends

Rational operator- (const Rational &)
 
bool operator< (const Rational &, const Rational &)
 
bool operator<= (const Rational &, const Rational &)
 
bool operator> (const Rational &, const Rational &)
 
bool operator>= (const Rational &, const Rational &)
 
bool operator== (const Rational &, const Rational &)
 
bool operator!= (const Rational &, const Rational &)
 
int sgn (const Rational &)
 
Rational abs (const Rational &)
 
Rational pow (const Rational &, int)
 
Rational gcd (const Rational &, const Rational &)
 
Rational lcm (const Rational &, const Rational &)
 
Rational gcd (Rational *, int)
 
Rational lcm (Rational *, int)
 

Detailed Description

Definition at line 14 of file GMPrat.h.

Constructor & Destructor Documentation

◆ Rational() [1/5]

Rational::Rational ( )

Definition at line 45 of file GMPrat.cc.

46{
47 p = new rep;
48 mpq_init( p->rat );
49}
rep * p
Definition: GMPrat.h:23
mpq_t rat
Definition: GMPrat.h:18

◆ Rational() [2/5]

Rational::Rational ( int  a)

Definition at line 51 of file GMPrat.cc.

52{
53 p = new rep;
54 mpq_init( p->rat );
55 mpq_set_si( p->rat,(long)a,1 );
56}

◆ Rational() [3/5]

Rational::Rational ( const Rational a)

Definition at line 58 of file GMPrat.cc.

59{
60 a.p->n++;
61 p=a.p;
62}

◆ Rational() [4/5]

Rational::Rational ( const Rational a,
const Rational b 
)

Definition at line 68 of file GMPrat.cc.

69{
70 p=new rep;
71 mpq_init(p->rat);
72 mpq_div(p->rat, a.p->rat, b.p->rat);
73}
CanonicalForm b
Definition: cfModGcd.cc:4103

◆ Rational() [5/5]

Rational::Rational ( int  a,
int  b 
)

Definition at line 75 of file GMPrat.cc.

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}
friend Rational abs(const Rational &)
Definition: GMPrat.cc:436

◆ ~Rational()

Rational::~Rational ( )

Definition at line 88 of file GMPrat.cc.

89{
90 if (--(p->n)==0)
91 {
92 mpq_clear(p->rat);
93 delete p;
94 }
95}

Member Function Documentation

◆ complexity()

double Rational::complexity ( ) const

Definition at line 526 of file GMPrat.cc.

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}
CanonicalForm num(const CanonicalForm &f)
CanonicalForm den(const CanonicalForm &f)

◆ disconnect()

void Rational::disconnect ( )
private

Definition at line 29 of file GMPrat.cc.

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}

◆ get_den()

Rational Rational::get_den ( )

Definition at line 143 of file GMPrat.cc.

144{
145 Rational erg;
146
147 mpq_set_num( erg.p->rat,mpq_denref( p->rat ) );
148
149 return erg;
150}

◆ get_den_si()

int Rational::get_den_si ( )

Definition at line 152 of file GMPrat.cc.

153{
154 return mpz_get_si( mpq_denref( p->rat ) );
155}

◆ get_num()

Rational Rational::get_num ( )

Definition at line 129 of file GMPrat.cc.

130{
131 Rational erg;
132
133 mpq_set_num( erg.p->rat,mpq_numref( p->rat ) );
134
135 return erg;
136}

◆ get_num_si()

int Rational::get_num_si ( )

Definition at line 138 of file GMPrat.cc.

139{
140 return mpz_get_si( mpq_numref( p->rat ) );
141}

◆ length()

unsigned int Rational::length ( ) const

Definition at line 362 of file GMPrat.cc.

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}
unsigned int length() const
Definition: GMPrat.cc:362
#define omAlloc(size)
Definition: omAllocDecl.h:210
#define omFree(addr)
Definition: omAllocDecl.h:261

◆ operator int()

Rational::operator int ( )

Definition at line 161 of file GMPrat.cc.

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}
STATIC_VAR Poly * h
Definition: janet.cc:971

◆ operator*=()

Rational & Rational::operator*= ( const Rational a)

Definition at line 229 of file GMPrat.cc.

230{
231 disconnect();
232 mpq_mul(p->rat,p->rat,a.p->rat);
233 return *this;
234}
void disconnect()
Definition: GMPrat.cc:29

◆ operator++() [1/2]

Rational & Rational::operator++ ( )

Definition at line 249 of file GMPrat.cc.

250{
251 disconnect();
252 mpz_add(mpq_numref(p->rat), mpq_numref(p->rat), mpq_denref(p->rat));
253 return *this;
254}

◆ operator++() [2/2]

Rational Rational::operator++ ( int  )

Definition at line 257 of file GMPrat.cc.

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}

◆ operator+=()

Rational & Rational::operator+= ( const Rational a)

Definition at line 213 of file GMPrat.cc.

214{
215 disconnect();
216 mpq_add(p->rat,p->rat,a.p->rat);
217 return *this;
218}

◆ operator-()

Rational Rational::operator- ( )

Definition at line 179 of file GMPrat.cc.

180{
181 Rational erg;
182
183 mpq_neg(erg.p->rat,p->rat);
184 return erg;
185}

◆ operator--() [1/2]

Rational & Rational::operator-- ( )

Definition at line 267 of file GMPrat.cc.

268{
269 disconnect();
270 mpz_sub(mpq_numref(p->rat), mpq_numref(p->rat), mpq_denref(p->rat));
271 return *this;
272}

◆ operator--() [2/2]

Rational Rational::operator-- ( int  )

Definition at line 275 of file GMPrat.cc.

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}

◆ operator-=()

Rational & Rational::operator-= ( const Rational a)

Definition at line 221 of file GMPrat.cc.

222{
223 disconnect();
224 mpq_sub(p->rat,p->rat,a.p->rat);
225 return *this;
226}

◆ operator/=()

Rational & Rational::operator/= ( const Rational a)

Definition at line 237 of file GMPrat.cc.

238{
239 disconnect();
240 mpq_div(p->rat,p->rat,a.p->rat);
241 return *this;
242}

◆ operator=() [1/3]

Rational & Rational::operator= ( char *  s)

◆ operator=() [2/3]

Rational & Rational::operator= ( const Rational a)

Definition at line 113 of file GMPrat.cc.

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}

◆ operator=() [3/3]

Rational & Rational::operator= ( int  a)

Definition at line 101 of file GMPrat.cc.

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}

◆ operator~()

Rational Rational::operator~ ( )

Definition at line 200 of file GMPrat.cc.

201{
202 Rational erg;
203
204 mpq_inv(erg.p->rat,p->rat);
205 return erg;
206}

Friends And Related Function Documentation

◆ abs

Rational abs ( const Rational a)
friend

Definition at line 436 of file GMPrat.cc.

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}

◆ gcd [1/2]

Rational gcd ( const Rational a,
const Rational b 
)
friend

Definition at line 448 of file GMPrat.cc.

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}

◆ gcd [2/2]

Rational gcd ( Rational a,
int  n 
)
friend

Definition at line 478 of file GMPrat.cc.

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}
int i
Definition: cfEzgcd.cc:132
g
Definition: cfModGcd.cc:4090
friend Rational gcd(const Rational &, const Rational &)
Definition: GMPrat.cc:448

◆ lcm [1/2]

Rational lcm ( const Rational a,
const Rational b 
)
friend

Definition at line 495 of file GMPrat.cc.

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}

◆ lcm [2/2]

Rational lcm ( Rational a,
int  n 
)
friend

Definition at line 509 of file GMPrat.cc.

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}
friend Rational lcm(const Rational &, const Rational &)
Definition: GMPrat.cc:495

◆ operator!=

bool operator!= ( const Rational a,
const Rational b 
)
friend

Definition at line 318 of file GMPrat.cc.

319{
320 if (mpq_equal(a.p->rat,b.p->rat)) return false;
321 return true;
322}

◆ operator-

Rational operator- ( const Rational r)
friend

Definition at line 187 of file GMPrat.cc.

188{
189 Rational erg;
190
191 mpq_neg(erg.p->rat,r.p->rat);
192 return erg;
193}

◆ operator<

bool operator< ( const Rational a,
const Rational b 
)
friend

Definition at line 288 of file GMPrat.cc.

289{
290 if (mpq_cmp(a.p->rat,b.p->rat)<0) return true;
291 return false;
292}

◆ operator<=

bool operator<= ( const Rational a,
const Rational b 
)
friend

Definition at line 294 of file GMPrat.cc.

295{
296 if (mpq_cmp(a.p->rat,b.p->rat)>0) return false;
297 return true;
298}

◆ operator==

bool operator== ( const Rational a,
const Rational b 
)
friend

Definition at line 312 of file GMPrat.cc.

313{
314 if (mpq_equal(a.p->rat,b.p->rat)) return true;
315 return false;
316}

◆ operator>

bool operator> ( const Rational a,
const Rational b 
)
friend

Definition at line 300 of file GMPrat.cc.

301{
302 if (mpq_cmp(a.p->rat,b.p->rat)>0) return true;
303 return false;
304}

◆ operator>=

bool operator>= ( const Rational a,
const Rational b 
)
friend

Definition at line 306 of file GMPrat.cc.

307{
308 if (mpq_cmp(a.p->rat,b.p->rat)<0) return false;
309 return true;
310}

◆ pow

Rational pow ( const Rational a,
int  e 
)
friend

Definition at line 411 of file GMPrat.cc.

412{
413 Rational erg(1);
414
415 for( int i=0; i<e; i++ )
416 {
417 erg *= a;
418 }
419 return erg;
420}

◆ sgn

int sgn ( const Rational a)
friend

Definition at line 430 of file GMPrat.cc.

431{
432 return mpq_sgn(a.p->rat);
433}

Field Documentation

◆ p

rep* Rational::p
private

Definition at line 23 of file GMPrat.h.


The documentation for this class was generated from the following files: