Loading...
Searching...
No Matches
GMPrat.cc
Go to the documentation of this file.
// ----------------------------------------------------------------------------
// GMPrat.cc
// begin of file
// originally written by Gerd Sussner, sussner@mi.uni-erlangen.de
// copied by Stephan Endrass, endrass@mathematik.uni-mainz.de
// 23.7.99
// ----------------------------------------------------------------------------
#define GMPRAT_CC
#include "kernel/mod2.h"
#ifdef HAVE_SPECTRUM
#ifdef GMPRAT_PRINT
#include <iostream.h>
#ifndef GMPRAT_IOSTREAM
#include <stdio.h>
#endif
#endif
#include "omalloc/omalloc.h"
#include "kernel/spectrum/GMPrat.h"
// ----------------------------------------------------------------------------
// disconnect a rational from its reference
// ----------------------------------------------------------------------------
void Rational::disconnect()
{
if( p->n>1)
{
rep *old_p = p;
p->n--;
p = new rep;
mpq_init(p->rat);
mpq_set(p->rat, old_p->rat);
}
}
// ----------------------------------------------------------------------------
// Constructors
// ----------------------------------------------------------------------------
Rational::Rational( )
{
p = new rep;
mpq_init( p->rat );
}
Rational::Rational( int a )
{
p = new rep;
mpq_init( p->rat );
mpq_set_si( p->rat,(long)a,1 );
}
Rational::Rational( const Rational& a )
{
a.p->n++;
p=a.p;
}
// ----------------------------------------------------------------------------
// Constructors with two arguments: numerator and denominator
// ----------------------------------------------------------------------------
Rational::Rational(const Rational& a, const Rational& b)
{
p=new rep;
mpq_init(p->rat);
mpq_div(p->rat, a.p->rat, b.p->rat);
}
Rational::Rational(int a, int b)
{
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);
}
// ----------------------------------------------------------------------------
// Destructor
// ----------------------------------------------------------------------------
Rational::~Rational()
{
if (--(p->n)==0)
{
mpq_clear(p->rat);
delete p;
}
}
// ----------------------------------------------------------------------------
// Assignment operators
// ----------------------------------------------------------------------------
Rational& Rational::operator=(int a)
{
if( p->n>1)
{
p->n--;
p = new rep;
mpq_init(p->rat);
}
mpq_set_si(p->rat,(long) a,1);
return *this;
}
Rational& Rational::operator=(const Rational& a)
{
a.p->n++;
if (--(p->n)==0)
{
mpq_clear(p->rat);
delete p;
}
p=a.p;
return *this;
}
// ----------------------------------------------------------------------------
// Numerator and denominator
// ----------------------------------------------------------------------------
Rational Rational::get_num( )
{
Rational erg;
mpq_set_num( erg.p->rat,mpq_numref( p->rat ) );
return erg;
}
int Rational::get_num_si( )
{
return mpz_get_si( mpq_numref( p->rat ) );
}
Rational Rational::get_den( )
{
Rational erg;
mpq_set_num( erg.p->rat,mpq_denref( p->rat ) );
return erg;
}
int Rational::get_den_si( )
{
return mpz_get_si( mpq_denref( p->rat ) );
}
// ----------------------------------------------------------------------------
// Casting
// ----------------------------------------------------------------------------
Rational::operator int()
{
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;
}
// ----------------------------------------------------------------------------
// Unary minus
// ----------------------------------------------------------------------------
Rational
Rational::operator-()
{
Rational erg;
mpq_neg(erg.p->rat,p->rat);
return erg;
}
Rational operator - ( const Rational &r )
{
Rational erg;
mpq_neg(erg.p->rat,r.p->rat);
return erg;
}
// ----------------------------------------------------------------------------
// Inverse
// ----------------------------------------------------------------------------
Rational
Rational::operator~()
{
Rational erg;
mpq_inv(erg.p->rat,p->rat);
return erg;
}
// ----------------------------------------------------------------------------
// +=, -= ...
// ----------------------------------------------------------------------------
Rational&
Rational::operator+=(const Rational &a)
{
disconnect();
mpq_add(p->rat,p->rat,a.p->rat);
return *this;
}
Rational&
Rational::operator-=(const Rational &a)
{
disconnect();
mpq_sub(p->rat,p->rat,a.p->rat);
return *this;
}
Rational&
Rational::operator*=(const Rational &a)
{
disconnect();
mpq_mul(p->rat,p->rat,a.p->rat);
return *this;
}
Rational&
Rational::operator/=(const Rational &a)
{
disconnect();
mpq_div(p->rat,p->rat,a.p->rat);
return *this;
}
// ----------------------------------------------------------------------------
// Increment and decrement
// ----------------------------------------------------------------------------
Rational&
Rational::operator++()
{
disconnect();
mpz_add(mpq_numref(p->rat), mpq_numref(p->rat), mpq_denref(p->rat));
return *this;
}
Rational
Rational::operator++(int)
{
Rational erg(*this);
disconnect();
mpz_add(mpq_numref(p->rat), mpq_numref(p->rat), mpq_denref(p->rat));
return erg;
}
Rational&
Rational::operator--()
{
disconnect();
mpz_sub(mpq_numref(p->rat), mpq_numref(p->rat), mpq_denref(p->rat));
return *this;
}
Rational
Rational::operator--(int)
{
Rational erg(*this);
disconnect();
mpz_sub(mpq_numref(p->rat), mpq_numref(p->rat), mpq_denref(p->rat));
return erg;
}
// ----------------------------------------------------------------------------
// Relational operators
// ----------------------------------------------------------------------------
bool operator<(const Rational& a,const Rational& b)
{
if (mpq_cmp(a.p->rat,b.p->rat)<0) return true;
return false;
}
bool operator<=(const Rational& a,const Rational& b)
{
if (mpq_cmp(a.p->rat,b.p->rat)>0) return false;
return true;
}
bool operator>(const Rational& a,const Rational& b)
{
if (mpq_cmp(a.p->rat,b.p->rat)>0) return true;
return false;
}
bool operator>=(const Rational& a,const Rational& b)
{
if (mpq_cmp(a.p->rat,b.p->rat)<0) return false;
return true;
}
bool operator==(const Rational& a,const Rational& b)
{
if (mpq_equal(a.p->rat,b.p->rat)) return true;
return false;
}
bool operator!=(const Rational& a,const Rational& b)
{
if (mpq_equal(a.p->rat,b.p->rat)) return false;
return true;
}
// ----------------------------------------------------------------------------
// Ostream
// ----------------------------------------------------------------------------
#ifdef GMPRAT_PRINT
ostream& operator<< (ostream& s,const Rational& a)
{
char *snum,*sdenom;
snum = mpz_get_str( NULL,10,mpq_numref(a.p->rat) );
sdenom = mpz_get_str( NULL,10,mpq_denref(a.p->rat) );
if( sdenom[0] == '1' && sdenom[1] == '\0' )
{
#ifdef GMPRAT_IOSTREAM
s << snum;
#else
fprintf( stdout,snum );
#endif
}
else
{
#ifdef GMPRAT_IOSTREAM
s << snum << "/" << sdenom;
#else
fprintf( stdout,snum );
fprintf( stdout,"/" );
fprintf( stdout,sdenom );
#endif
}
//free( snum );
//free( sdenom );
return s;
}
#endif
unsigned int Rational::length( ) const
{
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;
}
// ----------------------------------------------------------------------------
// Operators
// ----------------------------------------------------------------------------
Rational
operator+(const Rational& a,const Rational &b)
{
Rational
erg(a);
return erg+=b;
}
Rational
operator-(const Rational& a,const Rational &b)
{
Rational
erg(a);
return erg-=b;
}
Rational
operator*(const Rational& a,const Rational &b)
{
Rational
erg(a);
return erg*=b;
}
Rational pow( const Rational& a,int e )
{
Rational erg(1);
for( int i=0; i<e; i++ )
{
erg *= a;
}
return erg;
}
Rational operator/(const Rational& a,const Rational &b)
{
Rational
erg(a);
return erg/=b;
}
int sgn(const Rational& a)
{
return mpq_sgn(a.p->rat);
}
Rational
abs(const Rational& a)
{
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;
}
Rational gcd( const Rational &a,const Rational &b )
{
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 );
}
Rational gcd( Rational *a,int n )
{
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;
}
Rational lcm( const Rational &a,const Rational &b )
{
if( a == 0 )
{
return b;
}
else if( b == 0 )
{
return a;
}
return a*b/gcd(a,b);
}
Rational lcm( Rational *a,int n )
{
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;
}
double Rational::complexity( ) const
{
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 );
}
#endif /* HAVE_SPECTRUM */
// ----------------------------------------------------------------------------
// GMPrat.cc
// end of file
// ----------------------------------------------------------------------------
operator!=
bool operator!=(const Rational &a, const Rational &b)
Definition GMPrat.cc:318
lcm
Rational lcm(const Rational &a, const Rational &b)
Definition GMPrat.cc:495
operator<
bool operator<(const Rational &a, const Rational &b)
Definition GMPrat.cc:288
operator+
Rational operator+(const Rational &a, const Rational &b)
Definition GMPrat.cc:385
operator*
Rational operator*(const Rational &a, const Rational &b)
Definition GMPrat.cc:403
pow
Rational pow(const Rational &a, int e)
Definition GMPrat.cc:411
operator==
bool operator==(const Rational &a, const Rational &b)
Definition GMPrat.cc:312
operator/
Rational operator/(const Rational &a, const Rational &b)
Definition GMPrat.cc:422
sgn
int sgn(const Rational &a)
Definition GMPrat.cc:430
abs
Rational abs(const Rational &a)
Definition GMPrat.cc:436
operator-
Rational operator-(const Rational &r)
Definition GMPrat.cc:187
operator>
bool operator>(const Rational &a, const Rational &b)
Definition GMPrat.cc:300
operator<=
bool operator<=(const Rational &a, const Rational &b)
Definition GMPrat.cc:294
operator>=
bool operator>=(const Rational &a, const Rational &b)
Definition GMPrat.cc:306
gcd
Rational gcd(const Rational &a, const Rational &b)
Definition GMPrat.cc:448
GMPrat.h
num
CanonicalForm num(const CanonicalForm &f)
Definition canonicalform.h:337
den
CanonicalForm den(const CanonicalForm &f)
Definition canonicalform.h:340
i
int i
Definition cfEzgcd.cc:132
g
g
Definition cfModGcd.cc:4098
b
CanonicalForm b
Definition cfModGcd.cc:4111
Rational
Definition GMPrat.h:15
Rational::operator*=
Rational & operator*=(const Rational &)
Definition GMPrat.cc:229
Rational::get_num_si
int get_num_si()
Definition GMPrat.cc:138
Rational::p
rep * p
Definition GMPrat.h:23
Rational::Rational
Rational()
Definition GMPrat.cc:45
Rational::operator--
Rational & operator--()
Definition GMPrat.cc:267
Rational::operator-=
Rational & operator-=(const Rational &)
Definition GMPrat.cc:221
Rational::~Rational
~Rational()
Definition GMPrat.cc:88
Rational::operator~
Rational operator~()
Definition GMPrat.cc:200
Rational::complexity
double complexity() const
Definition GMPrat.cc:526
Rational::get_den_si
int get_den_si()
Definition GMPrat.cc:152
Rational::operator++
Rational & operator++()
Definition GMPrat.cc:249
Rational::get_num
Rational get_num()
Definition GMPrat.cc:129
Rational::abs
friend Rational abs(const Rational &)
Definition GMPrat.cc:436
Rational::operator-
Rational operator-()
Definition GMPrat.cc:179
Rational::length
unsigned int length() const
Definition GMPrat.cc:362
Rational::operator+=
Rational & operator+=(const Rational &)
Definition GMPrat.cc:213
Rational::operator=
Rational & operator=(int)
Definition GMPrat.cc:101
Rational::disconnect
void disconnect()
Definition GMPrat.cc:29
Rational::get_den
Rational get_den()
Definition GMPrat.cc:143
Rational::operator/=
Rational & operator/=(const Rational &)
Definition GMPrat.cc:237
s
const CanonicalForm int s
Definition facAbsFact.cc:51
h
STATIC_VAR Poly * h
Definition janet.cc:971
mod2.h
omAlloc
#define omAlloc(size)
Definition omAllocDecl.h:210
omFree
#define omFree(addr)
Definition omAllocDecl.h:261
NULL
#define NULL
Definition omList.c:12
omalloc.h
operator<<
ostream & operator<<(ostream &s, const spectrum &spec)
Definition semic.cc:249
Rational::rep
Definition GMPrat.h:17
Rational::rep::n
int n
Definition GMPrat.h:19
Rational::rep::rat
mpq_t rat
Definition GMPrat.h:18