My Project  debian-1:4.1.1-p2+ds-4build4
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 48 of file GMPrat.cc.

49 {
50  p = new rep;
51  mpq_init( p->rat );
52 }
rep * p
Definition: GMPrat.h:23
mpq_t rat
Definition: GMPrat.h:18

◆ Rational() [2/5]

Rational::Rational ( int  a)

Definition at line 54 of file GMPrat.cc.

55 {
56  p = new rep;
57  mpq_init( p->rat );
58  mpq_set_si( p->rat,(long)a,1 );
59 }

◆ Rational() [3/5]

Rational::Rational ( const Rational a)

Definition at line 61 of file GMPrat.cc.

62 {
63  a.p->n++;
64  p=a.p;
65 }

◆ Rational() [4/5]

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

Definition at line 71 of file GMPrat.cc.

72 {
73  p=new rep;
74  mpq_init(p->rat);
75  mpq_div(p->rat, a.p->rat, b.p->rat);
76 }
CanonicalForm b
Definition: cfModGcd.cc:4044

◆ Rational() [5/5]

Rational::Rational ( int  a,
int  b 
)

Definition at line 78 of file GMPrat.cc.

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

◆ ~Rational()

Rational::~Rational ( )

Definition at line 91 of file GMPrat.cc.

92 {
93  if (--(p->n)==0)
94  {
95  mpq_clear(p->rat);
96  delete p;
97  }
98 }

Member Function Documentation

◆ complexity()

double Rational::complexity ( ) const

Definition at line 529 of file GMPrat.cc.

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

◆ disconnect()

void Rational::disconnect ( )
private

Definition at line 32 of file GMPrat.cc.

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 }

◆ get_den()

Rational Rational::get_den ( )

Definition at line 146 of file GMPrat.cc.

147 {
148  Rational erg;
149 
150  mpq_set_num( erg.p->rat,mpq_denref( p->rat ) );
151 
152  return erg;
153 }

◆ get_den_si()

int Rational::get_den_si ( )

Definition at line 155 of file GMPrat.cc.

156 {
157  return mpz_get_si( mpq_denref( p->rat ) );
158 }

◆ get_num()

Rational Rational::get_num ( )

Definition at line 132 of file GMPrat.cc.

133 {
134  Rational erg;
135 
136  mpq_set_num( erg.p->rat,mpq_numref( p->rat ) );
137 
138  return erg;
139 }

◆ get_num_si()

int Rational::get_num_si ( )

Definition at line 141 of file GMPrat.cc.

142 {
143  return mpz_get_si( mpq_numref( p->rat ) );
144 }

◆ length()

unsigned int Rational::length ( ) const

Definition at line 365 of file GMPrat.cc.

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

◆ operator int()

Rational::operator int ( )

Definition at line 164 of file GMPrat.cc.

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

◆ operator*=()

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

Definition at line 232 of file GMPrat.cc.

233 {
234  disconnect();
235  mpq_mul(p->rat,p->rat,a.p->rat);
236  return *this;
237 }
void disconnect()
Definition: GMPrat.cc:32

◆ operator++() [1/2]

Rational & Rational::operator++ ( )

Definition at line 252 of file GMPrat.cc.

253 {
254  disconnect();
255  mpz_add(mpq_numref(p->rat), mpq_numref(p->rat), mpq_denref(p->rat));
256  return *this;
257 }

◆ operator++() [2/2]

Rational Rational::operator++ ( int  )

Definition at line 260 of file GMPrat.cc.

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 }

◆ operator+=()

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

Definition at line 216 of file GMPrat.cc.

217 {
218  disconnect();
219  mpq_add(p->rat,p->rat,a.p->rat);
220  return *this;
221 }

◆ operator-()

Rational Rational::operator- ( )

Definition at line 182 of file GMPrat.cc.

183 {
184  Rational erg;
185 
186  mpq_neg(erg.p->rat,p->rat);
187  return erg;
188 }

◆ operator--() [1/2]

Rational & Rational::operator-- ( )

Definition at line 270 of file GMPrat.cc.

271 {
272  disconnect();
273  mpz_sub(mpq_numref(p->rat), mpq_numref(p->rat), mpq_denref(p->rat));
274  return *this;
275 }

◆ operator--() [2/2]

Rational Rational::operator-- ( int  )

Definition at line 278 of file GMPrat.cc.

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 }

◆ operator-=()

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

Definition at line 224 of file GMPrat.cc.

225 {
226  disconnect();
227  mpq_sub(p->rat,p->rat,a.p->rat);
228  return *this;
229 }

◆ operator/=()

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

Definition at line 240 of file GMPrat.cc.

241 {
242  disconnect();
243  mpq_div(p->rat,p->rat,a.p->rat);
244  return *this;
245 }

◆ operator=() [1/3]

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

◆ operator=() [2/3]

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

Definition at line 116 of file GMPrat.cc.

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 }

◆ operator=() [3/3]

Rational & Rational::operator= ( int  a)

Definition at line 104 of file GMPrat.cc.

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 }

◆ operator~()

Rational Rational::operator~ ( )

Definition at line 203 of file GMPrat.cc.

204 {
205  Rational erg;
206 
207  mpq_inv(erg.p->rat,p->rat);
208  return erg;
209 }

Friends And Related Function Documentation

◆ abs

Rational abs ( const Rational a)
friend

Definition at line 439 of file GMPrat.cc.

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 }

◆ gcd [1/2]

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

Definition at line 451 of file GMPrat.cc.

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 }

◆ gcd [2/2]

Rational gcd ( Rational a,
int  n 
)
friend

Definition at line 481 of file GMPrat.cc.

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

◆ lcm [1/2]

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

Definition at line 498 of file GMPrat.cc.

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 }

◆ lcm [2/2]

Rational lcm ( Rational a,
int  n 
)
friend

Definition at line 512 of file GMPrat.cc.

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

◆ operator!=

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

Definition at line 321 of file GMPrat.cc.

322 {
323  if (mpq_equal(a.p->rat,b.p->rat)) return false;
324  return true;
325 }

◆ operator-

Rational operator- ( const Rational r)
friend

Definition at line 190 of file GMPrat.cc.

191 {
192  Rational erg;
193 
194  mpq_neg(erg.p->rat,r.p->rat);
195  return erg;
196 }

◆ operator<

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

Definition at line 291 of file GMPrat.cc.

292 {
293  if (mpq_cmp(a.p->rat,b.p->rat)<0) return true;
294  return false;
295 }

◆ operator<=

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

Definition at line 297 of file GMPrat.cc.

298 {
299  if (mpq_cmp(a.p->rat,b.p->rat)>0) return false;
300  return true;
301 }

◆ operator==

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

Definition at line 315 of file GMPrat.cc.

316 {
317  if (mpq_equal(a.p->rat,b.p->rat)) return true;
318  return false;
319 }

◆ operator>

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

Definition at line 303 of file GMPrat.cc.

304 {
305  if (mpq_cmp(a.p->rat,b.p->rat)>0) return true;
306  return false;
307 }

◆ operator>=

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

Definition at line 309 of file GMPrat.cc.

310 {
311  if (mpq_cmp(a.p->rat,b.p->rat)<0) return false;
312  return true;
313 }

◆ pow

Rational pow ( const Rational a,
int  e 
)
friend

Definition at line 414 of file GMPrat.cc.

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

◆ sgn

int sgn ( const Rational a)
friend

Definition at line 433 of file GMPrat.cc.

434 {
435  return mpq_sgn(a.p->rat);
436 }

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: