Loading...
Searching...
No Matches
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 ()
Rational & | operator= (int)
Rational & | operator= (char *s)
Rational & | operator= (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~ ()
Rational & | operator+= (const Rational &)
Rational & | operator-= (const Rational &)
Rational & | operator*= (const Rational &)
Rational & | operator/= (const Rational &)
Rational & | operator++ ()
Rational | operator++ (int)
Rational & | operator-- ()
Rational | operator-- (int)
double | complexity () const

## Private Member Functions

void | disconnect ()

## Private Attributes

rep * | p

## Friends

Rational | operator- (const Rational &r)
bool | operator< (const Rational &a, const Rational &b)
bool | operator<= (const Rational &a, const Rational &b)
bool | operator> (const Rational &a, const Rational &b)
bool | operator>= (const Rational &a, const Rational &b)
bool | operator== (const Rational &a, const Rational &b)
bool | operator!= (const Rational &a, const Rational &b)
int | sgn (const Rational &a)
Rational | abs (const Rational &a)
Rational | pow (const Rational &a, int e)
Rational | gcd (const Rational &a, const Rational &b)
Rational | lcm (const Rational &a, const Rational &b)
Rational | gcd (Rational *a, int n)
Rational | lcm (Rational *a, int n)

## 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.

{
p = new rep;
mpq_init( p->rat );
}
Rational::p
rep * p
Definition GMPrat.h:23
Rational::rep
Definition GMPrat.h:17

## Rational() [2/5]

Rational::Rational | ( | int | a | ) |

Definition at line 51 of file GMPrat.cc.

{
p = new rep;
mpq_init( p->rat );
mpq_set_si( p->rat,(long)a,1 );
}

## Rational() [3/5]

Rational::Rational | ( | const Rational & | a | ) |

Definition at line 58 of file GMPrat.cc.

{
a.p->n++;
p=a.p;
}
Rational::rep::n
int n
Definition GMPrat.h:19

## Rational() [4/5]

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

Definition at line 68 of file GMPrat.cc.

{
p=new rep;
mpq_init(p->rat);
mpq_div(p->rat, a.p->rat, b.p->rat);
}
b
CanonicalForm b
Definition cfModGcd.cc:4111
Rational::rep::rat
mpq_t rat
Definition GMPrat.h:18

## Rational() [5/5]

Rational::Rational | ( | int | a,
|  | int | b )

Definition at line 75 of file GMPrat.cc.

{
if (b<0) a=-a;
p=new rep;
mpq_init(p->rat);
mpq_set_si(p->rat,(long) a,(unsigned long) abs(b));
mpq_canonicalize(p->rat);
}
Rational::abs
friend Rational abs(const Rational &)
Definition GMPrat.cc:436

## ~Rational()

Rational::~Rational | ( |  | ) |

Definition at line 88 of file GMPrat.cc.

{
if (--(p->n)==0)
{
mpq_clear(p->rat);
delete p;
}
}

## Member Function Documentation

## complexity()

double Rational::complexity | ( |  | ) | const

Definition at line 526 of file GMPrat.cc.

{
double num = mpz_get_d( mpq_numref( p->rat ) );
double den = mpz_get_d( mpq_denref( p->rat ) );
if( num < 0 ) num = -num;
if( den < 0 ) den = -den;
return ( num > den ? num : den );
}
num
CanonicalForm num(const CanonicalForm &f)
Definition canonicalform.h:337
den
CanonicalForm den(const CanonicalForm &f)
Definition canonicalform.h:340

## disconnect()

void Rational::disconnect | ( |  | ) |

private

Definition at line 29 of file GMPrat.cc.

{
if( p->n>1)
{
rep *old_p = p;
p->n--;
p = new rep;
mpq_init(p->rat);
mpq_set(p->rat, old_p->rat);
}
}

## get_den()

Rational Rational::get_den | ( |  | ) |

Definition at line 143 of file GMPrat.cc.

{
Rational erg;
mpq_set_num( erg.p->rat,mpq_denref( p->rat ) );
return erg;
}
Rational::Rational
Rational()
Definition GMPrat.cc:45

## get_den_si()

int Rational::get_den_si | ( |  | ) |

Definition at line 152 of file GMPrat.cc.

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

## get_num()

Rational Rational::get_num | ( |  | ) |

Definition at line 129 of file GMPrat.cc.

{
Rational erg;
mpq_set_num( erg.p->rat,mpq_numref( p->rat ) );
return erg;
}

## get_num_si()

int Rational::get_num_si | ( |  | ) |

Definition at line 138 of file GMPrat.cc.

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

## length()

unsigned int Rational::length | ( |  | ) | const

Definition at line 362 of file GMPrat.cc.

{
char *snum = (char*)omAlloc(mpz_sizeinbase(mpq_numref(p->rat),10)+2);
char *sden = (char*)omAlloc(mpz_sizeinbase(mpq_denref(p->rat),10)+2);
snum = mpz_get_str( snum,10,mpq_numref( p->rat ) );
sden = mpz_get_str( sden,10,mpq_denref( p->rat ) );
int length = strlen( snum );
if( sden[0] != '1' || sden[1] != '\0' ) length += strlen( sden ) + 1;
omFree( snum );
omFree( sden );
return length;
}
Rational::length
unsigned int length() const
Definition GMPrat.cc:362
omAlloc
#define omAlloc(size)
Definition omAllocDecl.h:210
omFree
#define omFree(addr)
Definition omAllocDecl.h:261

## operator int()

Rational::operator int | ( |  | ) |

Definition at line 161 of file GMPrat.cc.

{
mpz_t h;
long ret_val;
mpz_init(h);
mpz_tdiv_q(h,mpq_numref(p->rat),mpq_denref(p->rat));
ret_val=mpz_get_si(h);
mpz_clear(h);
return ret_val;
}
h
STATIC_VAR Poly * h
Definition janet.cc:971

## operator*=()

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

Definition at line 229 of file GMPrat.cc.

{
disconnect();
mpq_mul(p->rat,p->rat,a.p->rat);
return *this;
}
Rational::disconnect
void disconnect()
Definition GMPrat.cc:29

## operator++() [1/2]

Rational & Rational::operator++ | ( |  | ) |

Definition at line 249 of file GMPrat.cc.

{
disconnect();
mpz_add(mpq_numref(p->rat), mpq_numref(p->rat), mpq_denref(p->rat));
return *this;
}

## operator++() [2/2]

Rational Rational::operator++ | ( | int |  | ) |

Definition at line 257 of file GMPrat.cc.

{
Rational erg(*this);
disconnect();
mpz_add(mpq_numref(p->rat), mpq_numref(p->rat), mpq_denref(p->rat));
return erg;
}

## operator+=()

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

Definition at line 213 of file GMPrat.cc.

{
disconnect();
mpq_add(p->rat,p->rat,a.p->rat);
return *this;
}

## operator-()

Rational Rational::operator- | ( |  | ) |

Definition at line 179 of file GMPrat.cc.

{
Rational erg;
mpq_neg(erg.p->rat,p->rat);
return erg;
}

## operator--() [1/2]

Rational & Rational::operator-- | ( |  | ) |

Definition at line 267 of file GMPrat.cc.

{
disconnect();
mpz_sub(mpq_numref(p->rat), mpq_numref(p->rat), mpq_denref(p->rat));
return *this;
}

## operator--() [2/2]

Rational Rational::operator-- | ( | int |  | ) |

Definition at line 275 of file GMPrat.cc.

{
Rational erg(*this);
disconnect();
mpz_sub(mpq_numref(p->rat), mpq_numref(p->rat), mpq_denref(p->rat));
return erg;
}

## operator-=()

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

Definition at line 221 of file GMPrat.cc.

{
disconnect();
mpq_sub(p->rat,p->rat,a.p->rat);
return *this;
}

## operator/=()

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

Definition at line 237 of file GMPrat.cc.

{
disconnect();
mpq_div(p->rat,p->rat,a.p->rat);
return *this;
}

## 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.

{
a.p->n++;
if (--(p->n)==0)
{
mpq_clear(p->rat);
delete p;
}
p=a.p;
return *this;
}

## operator=() [3/3]

Rational & Rational::operator= | ( | int | a | ) |

Definition at line 101 of file GMPrat.cc.

{
if( p->n>1)
{
p->n--;
p = new rep;
mpq_init(p->rat);
}
mpq_set_si(p->rat,(long) a,1);
return *this;
}

## operator~()

Rational Rational::operator~ | ( |  | ) |

Definition at line 200 of file GMPrat.cc.

{
Rational erg;
mpq_inv(erg.p->rat,p->rat);
return erg;
}

## Friends And Related Symbol Documentation

## abs

Rational abs | ( | const Rational & | a | ) |

friend

Definition at line 436 of file GMPrat.cc.

{
Rational
erg;
if (mpq_sgn(a.p->rat)<0)
mpq_neg(erg.p->rat,a.p->rat);
else
mpq_set(erg.p->rat,a.p->rat);
return erg;
}

## gcd [1/2]

Rational gcd | ( | const Rational & | a,
|  | const Rational & | b )

friend

Definition at line 448 of file GMPrat.cc.

{
if( a == 0 )
{
if( b == 0 )
{
return (Rational)1;
}
else
{
return abs( b );
}
}
else if( b == 0 )
{
return abs( a );
}
Rational erg;
mpz_gcd( mpq_numref( erg.p->rat ),
mpq_numref( a.p->rat ),mpq_numref( b.p->rat ) );
mpz_gcd( mpq_denref( erg.p->rat ),
mpq_denref( a.p->rat ),mpq_denref( b.p->rat ) );
//mpq_canonicalize( erg.p->rat );
return abs( erg );
}

## gcd [2/2]

Rational gcd | ( | Rational * | a,
|  | int | n )

friend

Definition at line 478 of file GMPrat.cc.

{
if( n == 1 )
{
return a[0];
}
Rational g = gcd( a[0],a[1] );
for( int i=2; i<n; i++ )
{
g = gcd( g,a[i] );
}
return g;
}
i
int i
Definition cfEzgcd.cc:132
g
g
Definition cfModGcd.cc:4098
Rational::gcd
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.

{
if( a == 0 )
{
return b;
}
else if( b == 0 )
{
return a;
}
return a*b/gcd(a,b);
}

## lcm [2/2]

Rational lcm | ( | Rational * | a,
|  | int | n )

friend

Definition at line 509 of file GMPrat.cc.

{
if( n == 1 )
{
return a[0];
}
Rational g = lcm( a[0],a[1] );
for( int i=2; i<n; i++ )
{
g = lcm( g,a[i] );
}
return g;
}
Rational::lcm
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.

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

## operator-

Rational operator- | ( | const Rational & | r | ) |

friend

Definition at line 187 of file GMPrat.cc.

{
Rational erg;
mpq_neg(erg.p->rat,r.p->rat);
return erg;
}

## operator<

bool operator< | ( | const Rational & | a,
|  | const Rational & | b )

friend

Definition at line 288 of file GMPrat.cc.

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

## operator<=

bool operator<= | ( | const Rational & | a,
|  | const Rational & | b )

friend

Definition at line 294 of file GMPrat.cc.

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

## operator==

bool operator== | ( | const Rational & | a,
|  | const Rational & | b )

friend

Definition at line 312 of file GMPrat.cc.

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

## operator>

bool operator> | ( | const Rational & | a,
|  | const Rational & | b )

friend

Definition at line 300 of file GMPrat.cc.

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

## operator>=

bool operator>= | ( | const Rational & | a,
|  | const Rational & | b )

friend

Definition at line 306 of file GMPrat.cc.

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

## pow

Rational pow | ( | const Rational & | a,
|  | int | e )

friend

Definition at line 411 of file GMPrat.cc.

{
Rational erg(1);
for( int i=0; i<e; i++ )
{
erg *= a;
}
return erg;
}

## sgn

int sgn | ( | const Rational & | a | ) |

friend

Definition at line 430 of file GMPrat.cc.

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

## 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:

- kernel/spectrum/GMPrat.h
- kernel/spectrum/GMPrat.cc