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 52 of file GMPrat.cc.

53 {
54  p = new rep;
55  mpq_init( p->rat );
56 }
mpq_t rat
Definition: GMPrat.h:18
rep * p
Definition: GMPrat.h:23

§ Rational() [2/5]

Rational::Rational ( int  a)

Definition at line 58 of file GMPrat.cc.

59 {
60  p = new rep;
61  mpq_init( p->rat );
62  mpq_set_si( p->rat,(long)a,1 );
63 }
const poly a
Definition: syzextra.cc:212
mpq_t rat
Definition: GMPrat.h:18
rep * p
Definition: GMPrat.h:23

§ Rational() [3/5]

Rational::Rational ( const Rational a)

Definition at line 65 of file GMPrat.cc.

66 {
67  a.p->n++;
68  p=a.p;
69 }
rep * p
Definition: GMPrat.h:23

§ Rational() [4/5]

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

Definition at line 75 of file GMPrat.cc.

76 {
77  p=new rep;
78  mpq_init(p->rat);
79  mpq_div(p->rat, a.p->rat, b.p->rat);
80 }
mpq_t rat
Definition: GMPrat.h:18
rep * p
Definition: GMPrat.h:23

§ Rational() [5/5]

Rational::Rational ( int  a,
int  b 
)

Definition at line 82 of file GMPrat.cc.

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 }
const poly a
Definition: syzextra.cc:212
mpq_t rat
Definition: GMPrat.h:18
rep * p
Definition: GMPrat.h:23
friend Rational abs(const Rational &)
Definition: GMPrat.cc:443
const poly b
Definition: syzextra.cc:213

§ ~Rational()

Rational::~Rational ( )

Definition at line 95 of file GMPrat.cc.

96 {
97  if (--(p->n)==0)
98  {
99  mpq_clear(p->rat);
100  delete p;
101  }
102 }
mpq_t rat
Definition: GMPrat.h:18
rep * p
Definition: GMPrat.h:23

Member Function Documentation

§ complexity()

double Rational::complexity ( ) const

Definition at line 533 of file GMPrat.cc.

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 }
CanonicalForm num(const CanonicalForm &f)
mpq_t rat
Definition: GMPrat.h:18
rep * p
Definition: GMPrat.h:23
CanonicalForm den(const CanonicalForm &f)

§ disconnect()

void Rational::disconnect ( )
private

Definition at line 36 of file GMPrat.cc.

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 }
mpq_t rat
Definition: GMPrat.h:18
rep * p
Definition: GMPrat.h:23

§ get_den()

Rational Rational::get_den ( )

Definition at line 150 of file GMPrat.cc.

151 {
152  Rational erg;
153 
154  mpq_set_num( erg.p->rat,mpq_denref( p->rat ) );
155 
156  return erg;
157 }
mpq_t rat
Definition: GMPrat.h:18
rep * p
Definition: GMPrat.h:23

§ get_den_si()

int Rational::get_den_si ( )

Definition at line 159 of file GMPrat.cc.

160 {
161  return mpz_get_si( mpq_denref( p->rat ) );
162 }
mpq_t rat
Definition: GMPrat.h:18
rep * p
Definition: GMPrat.h:23

§ get_num()

Rational Rational::get_num ( )

Definition at line 136 of file GMPrat.cc.

137 {
138  Rational erg;
139 
140  mpq_set_num( erg.p->rat,mpq_numref( p->rat ) );
141 
142  return erg;
143 }
mpq_t rat
Definition: GMPrat.h:18
rep * p
Definition: GMPrat.h:23

§ get_num_si()

int Rational::get_num_si ( )

Definition at line 145 of file GMPrat.cc.

146 {
147  return mpz_get_si( mpq_numref( p->rat ) );
148 }
mpq_t rat
Definition: GMPrat.h:18
rep * p
Definition: GMPrat.h:23

§ length()

unsigned int Rational::length ( ) const

Definition at line 369 of file GMPrat.cc.

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 }
mpq_t rat
Definition: GMPrat.h:18
unsigned int length() const
Definition: GMPrat.cc:369
#define free
Definition: omAllocFunc.c:12
rep * p
Definition: GMPrat.h:23
#define NULL
Definition: omList.c:10

§ operator int()

Rational::operator int ( )

Definition at line 168 of file GMPrat.cc.

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 }
mpq_t rat
Definition: GMPrat.h:18
rep * p
Definition: GMPrat.h:23
static Poly * h
Definition: janet.cc:978

§ operator*=()

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

Definition at line 236 of file GMPrat.cc.

237 {
238  disconnect();
239  mpq_mul(p->rat,p->rat,a.p->rat);
240  return *this;
241 }
mpq_t rat
Definition: GMPrat.h:18
void disconnect()
Definition: GMPrat.cc:36
rep * p
Definition: GMPrat.h:23

§ operator++() [1/2]

Rational & Rational::operator++ ( )

Definition at line 256 of file GMPrat.cc.

257 {
258  disconnect();
259  mpz_add(mpq_numref(p->rat), mpq_numref(p->rat), mpq_denref(p->rat));
260  return *this;
261 }
mpq_t rat
Definition: GMPrat.h:18
void disconnect()
Definition: GMPrat.cc:36
rep * p
Definition: GMPrat.h:23

§ operator++() [2/2]

Rational Rational::operator++ ( int  )

Definition at line 264 of file GMPrat.cc.

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 }
mpq_t rat
Definition: GMPrat.h:18
void disconnect()
Definition: GMPrat.cc:36
rep * p
Definition: GMPrat.h:23

§ operator+=()

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

Definition at line 220 of file GMPrat.cc.

221 {
222  disconnect();
223  mpq_add(p->rat,p->rat,a.p->rat);
224  return *this;
225 }
mpq_t rat
Definition: GMPrat.h:18
void disconnect()
Definition: GMPrat.cc:36
rep * p
Definition: GMPrat.h:23

§ operator-()

Rational Rational::operator- ( )

Definition at line 186 of file GMPrat.cc.

187 {
188  Rational erg;
189 
190  mpq_neg(erg.p->rat,p->rat);
191  return erg;
192 }
mpq_t rat
Definition: GMPrat.h:18
rep * p
Definition: GMPrat.h:23

§ operator--() [1/2]

Rational & Rational::operator-- ( )

Definition at line 274 of file GMPrat.cc.

275 {
276  disconnect();
277  mpz_sub(mpq_numref(p->rat), mpq_numref(p->rat), mpq_denref(p->rat));
278  return *this;
279 }
mpq_t rat
Definition: GMPrat.h:18
void disconnect()
Definition: GMPrat.cc:36
rep * p
Definition: GMPrat.h:23

§ operator--() [2/2]

Rational Rational::operator-- ( int  )

Definition at line 282 of file GMPrat.cc.

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 }
mpq_t rat
Definition: GMPrat.h:18
void disconnect()
Definition: GMPrat.cc:36
rep * p
Definition: GMPrat.h:23

§ operator-=()

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

Definition at line 228 of file GMPrat.cc.

229 {
230  disconnect();
231  mpq_sub(p->rat,p->rat,a.p->rat);
232  return *this;
233 }
mpq_t rat
Definition: GMPrat.h:18
void disconnect()
Definition: GMPrat.cc:36
rep * p
Definition: GMPrat.h:23

§ operator/=()

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

Definition at line 244 of file GMPrat.cc.

245 {
246  disconnect();
247  mpq_div(p->rat,p->rat,a.p->rat);
248  return *this;
249 }
mpq_t rat
Definition: GMPrat.h:18
void disconnect()
Definition: GMPrat.cc:36
rep * p
Definition: GMPrat.h:23

§ operator=() [1/3]

Rational & Rational::operator= ( int  a)

Definition at line 108 of file GMPrat.cc.

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 }
const poly a
Definition: syzextra.cc:212
mpq_t rat
Definition: GMPrat.h:18
rep * p
Definition: GMPrat.h:23

§ operator=() [2/3]

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

§ operator=() [3/3]

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

Definition at line 120 of file GMPrat.cc.

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 }
mpq_t rat
Definition: GMPrat.h:18
rep * p
Definition: GMPrat.h:23

§ operator~()

Rational Rational::operator~ ( )

Definition at line 207 of file GMPrat.cc.

208 {
209  Rational erg;
210 
211  mpq_inv(erg.p->rat,p->rat);
212  return erg;
213 }
mpq_t rat
Definition: GMPrat.h:18
rep * p
Definition: GMPrat.h:23

Friends And Related Function Documentation

§ abs

Rational abs ( const Rational a)
friend

Definition at line 443 of file GMPrat.cc.

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 }
mpq_t rat
Definition: GMPrat.h:18
rep * p
Definition: GMPrat.h:23

§ gcd [1/2]

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

Definition at line 455 of file GMPrat.cc.

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 }
mpq_t rat
Definition: GMPrat.h:18
rep * p
Definition: GMPrat.h:23
friend Rational abs(const Rational &)
Definition: GMPrat.cc:443

§ gcd [2/2]

Rational gcd ( Rational a,
int  n 
)
friend

Definition at line 485 of file GMPrat.cc.

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

§ lcm [1/2]

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

Definition at line 502 of file GMPrat.cc.

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 }
const poly a
Definition: syzextra.cc:212
friend Rational gcd(const Rational &, const Rational &)
Definition: GMPrat.cc:455
const poly b
Definition: syzextra.cc:213

§ lcm [2/2]

Rational lcm ( Rational a,
int  n 
)
friend

Definition at line 516 of file GMPrat.cc.

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

§ operator!=

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

Definition at line 325 of file GMPrat.cc.

326 {
327  if (mpq_equal(a.p->rat,b.p->rat)) return false;
328  return true;
329 }
mpq_t rat
Definition: GMPrat.h:18
rep * p
Definition: GMPrat.h:23

§ operator-

Rational operator- ( const Rational r)
friend

Definition at line 194 of file GMPrat.cc.

195 {
196  Rational erg;
197 
198  mpq_neg(erg.p->rat,r.p->rat);
199  return erg;
200 }
mpq_t rat
Definition: GMPrat.h:18
rep * p
Definition: GMPrat.h:23

§ operator<

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

Definition at line 295 of file GMPrat.cc.

296 {
297  if (mpq_cmp(a.p->rat,b.p->rat)<0) return true;
298  return false;
299 }
mpq_t rat
Definition: GMPrat.h:18
rep * p
Definition: GMPrat.h:23

§ operator<=

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

Definition at line 301 of file GMPrat.cc.

302 {
303  if (mpq_cmp(a.p->rat,b.p->rat)>0) return false;
304  return true;
305 }
mpq_t rat
Definition: GMPrat.h:18
rep * p
Definition: GMPrat.h:23

§ operator==

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

Definition at line 319 of file GMPrat.cc.

320 {
321  if (mpq_equal(a.p->rat,b.p->rat)) return true;
322  return false;
323 }
mpq_t rat
Definition: GMPrat.h:18
rep * p
Definition: GMPrat.h:23

§ operator>

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

Definition at line 307 of file GMPrat.cc.

308 {
309  if (mpq_cmp(a.p->rat,b.p->rat)>0) return true;
310  return false;
311 }
mpq_t rat
Definition: GMPrat.h:18
rep * p
Definition: GMPrat.h:23

§ operator>=

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

Definition at line 313 of file GMPrat.cc.

314 {
315  if (mpq_cmp(a.p->rat,b.p->rat)<0) return false;
316  return true;
317 }
mpq_t rat
Definition: GMPrat.h:18
rep * p
Definition: GMPrat.h:23

§ pow

Rational pow ( const Rational a,
int  e 
)
friend

Definition at line 418 of file GMPrat.cc.

419 {
420  Rational erg(1);
421 
422  for( int i=0; i<e; i++ )
423  {
424  erg *= a;
425  }
426  return erg;
427 }
const poly a
Definition: syzextra.cc:212
int i
Definition: cfEzgcd.cc:123

§ sgn

int sgn ( const Rational a)
friend

Definition at line 437 of file GMPrat.cc.

438 {
439  return mpq_sgn(a.p->rat);
440 }
mpq_t rat
Definition: GMPrat.h:18
rep * p
Definition: GMPrat.h:23

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: