Loading...
Searching...
No Matches
InternalRational Class Reference

factory's class for rationals More...

`#include <int_rat.h>`

## Public Member Functions

| InternalRational ()
| InternalRational (const InternalCF &)
| InternalRational (const int i)
| InternalRational (const int n, const int d)
| InternalRational (const long i)
| InternalRational (const long n, const long d)
| InternalRational (const char *str)
| InternalRational (const mpz_ptr)
| InternalRational (const mpz_ptr, const mpz_ptr)
| ~InternalRational ()
InternalCF * | deepCopyObject () const
const char * | classname () const
void | print (OSTREAM &, char *)
InternalCF * | genZero ()
InternalCF * | genOne ()
bool | is_imm () const
int | levelcoeff () const
InternalCF * | num ()
InternalCF * | den ()
InternalCF * | neg ()
| InternalCF * InternalRational::neg ().
int | comparesame (InternalCF *)
| comparesame(), comparecoeff() - compare with an InternalRational.
InternalCF * | addsame (InternalCF *)
InternalCF * | subsame (InternalCF *)
InternalCF * | mulsame (InternalCF *)
InternalCF * | dividesame (InternalCF *)
InternalCF * | modulosame (InternalCF *)
InternalCF * | divsame (InternalCF *)
InternalCF * | modsame (InternalCF *)
void | divremsame (InternalCF *, InternalCF *&, InternalCF *&)
bool | divremsamet (InternalCF *, InternalCF *&, InternalCF *&)
int | comparecoeff (InternalCF *)
| comparecoeff() compares the CO=a/b and the integer c using the equivalence a/b < c iff a < c*b.
InternalCF * | addcoeff (InternalCF *)
InternalCF * | subcoeff (InternalCF *, bool)
InternalCF * | mulcoeff (InternalCF *)
InternalCF * | dividecoeff (InternalCF *, bool)
InternalCF * | modulocoeff (InternalCF *, bool)
InternalCF * | divcoeff (InternalCF *, bool)
InternalCF * | modcoeff (InternalCF *, bool)
void | divremcoeff (InternalCF *, InternalCF *&, InternalCF *&, bool)
bool | divremcoefft (InternalCF *, InternalCF *&, InternalCF *&, bool)
InternalCF * | bgcdsame (const InternalCF *const) const
InternalCF * | bgcdcoeff (const InternalCF *const)
InternalCF * | bextgcdsame (InternalCF *, CanonicalForm &, CanonicalForm &)
InternalCF * | bextgcdcoeff (InternalCF *, CanonicalForm &, CanonicalForm &)
long | intval () const
int | sign () const
InternalCF * | normalize_myself ()
| reduce InternalRational to lowest terms
Public Member Functions inherited from InternalCF
| InternalCF ()
| InternalCF (const InternalCF &)
virtual | ~InternalCF ()
int | deleteObject ()
InternalCF * | copyObject ()
virtual int | level () const
virtual int | type () const
virtual Variable | variable () const
virtual bool | inBaseDomain () const
virtual bool | inExtension () const
virtual bool | inCoeffDomain () const
virtual bool | inPolyDomain () const
virtual bool | inQuotDomain () const
virtual bool | isZero () const
virtual bool | isOne () const
| bool InternalCF::isOne, isZero () const
virtual bool | isUnivariate () const
virtual int | intmod (int) const
virtual InternalCF * | invert ()
virtual InternalCF * | tryInvert (const CanonicalForm &, bool &)
virtual InternalCF * | tryMulsame (InternalCF *, const CanonicalForm &)
virtual InternalCF * | tryDivsame (InternalCF *, const CanonicalForm &, bool &)
virtual bool | tryDivremsamet (InternalCF *, InternalCF *&, InternalCF *&, const CanonicalForm &, bool &)
virtual InternalCF * | tryDividecoeff (InternalCF *, bool, const CanonicalForm &, bool &)
virtual InternalCF * | tryDivcoeff (InternalCF *, bool, const CanonicalForm &, bool &)
virtual bool | tryDivremcoefft (InternalCF *, InternalCF *&, InternalCF *&, bool, const CanonicalForm &, bool &)
virtual InternalCF * | sqrt ()
| InternalCF * InternalCF::sqrt ().
virtual int | ilog2 ()
| int InternalCF::ilog2 ()
virtual CanonicalForm | lc ()
| CanonicalForm InternalCF::lc (), Lc (), LC ().
virtual CanonicalForm | Lc ()
virtual CanonicalForm | LC ()
virtual CanonicalForm | coeff (int i)
| CanonicalForm InternalCF::coeff ( int i ).
virtual int | degree ()
| int InternalCF::degree ()
virtual int | taildegree ()
virtual CanonicalForm | tailcoeff ()
| CanonicalForm InternalCF::tailcoeff (), int InternalCF::taildegree ().
Public Member Functions inherited from omallocClass
void * | operator new (size_t size) throw (std::bad_alloc)
void | operator delete (void *block) throw ()
void * | operator new[] (size_t size) throw (std::bad_alloc)
void | operator delete[] (void *block) throw ()
void * | operator new (size_t size, const std::nothrow_t &) throw ()
void * | operator new[] (size_t size, const std::nothrow_t &) throw ()

## Static Private Member Functions

static mpz_ptr | MPQNUM (const InternalCF *const c)
static mpz_ptr | MPQDEN (const InternalCF *const c)
static void | normalize (const mpz_ptr, const mpz_ptr, mpz_ptr, mpz_ptr)

## Private Attributes

mpz_t | _num
mpz_t | _den
STATIC_VAR int | initialized

## Friends

class | InternalInteger
void | gmp_numerator (const CanonicalForm &f, mpz_ptr result)
void | gmp_denominator (const CanonicalForm &f, mpz_ptr result)
CanonicalForm | make_cf (const mpz_ptr n, const mpz_ptr d)
void | convertCF2Fmpq (fmpq_t result, const CanonicalForm &f)
| conversion of a factory rationals to fmpq_t

## Additional Inherited Members

Protected Member Functions inherited from InternalCF
int | getRefCount ()
void | incRefCount ()
int | decRefCount ()

## Detailed Description

factory's class for rationals

a rational is represented as two mpz_t's _num, _den

Note: If you want to compute over Q make sure that SW_RATIONAL is set to 1!

See also
  InternalInteger

Definition at line 54 of file int_rat.h.

## Constructor & Destructor Documentation

## InternalRational() [1/9]

InternalRational::InternalRational | ( |  | ) |

Definition at line 34 of file int_rat.cc.

{
mpz_init( _num );
mpz_init_set_si( _den, 1 );
}
InternalRational::_num
mpz_t _num
Definition int_rat.h:57
InternalRational::_den
mpz_t _den
Definition int_rat.h:58

## InternalRational() [2/9]

InternalRational::InternalRational | ( | const InternalCF & |  | ) |

inline

Definition at line 65 of file int_rat.h.

{
ASSERT( 0, "ups there is something wrong in your code" );
}
ASSERT
#define ASSERT(expression, message)
Definition cf_assert.h:99

## InternalRational() [3/9]

InternalRational::InternalRational | ( | const int | i | ) |

Definition at line 40 of file int_rat.cc.

{
mpz_init_set_si( _num, i );
mpz_init_set_si( _den, 1 );
}
i
int i
Definition cfEzgcd.cc:132

## InternalRational() [4/9]

InternalRational::InternalRational | ( | const int | n,
|  | const int | d )

Definition at line 46 of file int_rat.cc.

{
ASSERT( d != 0, "divide by zero" );
if ( n == 0 )
{
mpz_init_set_si( _num, 0 );
mpz_init_set_si( _den, 1 );
}
else
{
long g = intgcd( (long) n, (long) d );
if ( d < 0 )
{
mpz_init_set_si( _num, -((long)n) / g );
mpz_init_set_si( _den, -((long)d) / g );
}
else
{
mpz_init_set_si( _num, n / g );
mpz_init_set_si( _den, d / g );
}
}
}
g
g
Definition cfModGcd.cc:4098
intgcd
static long intgcd(long a, long b)
Definition int_rat.cc:17

## InternalRational() [5/9]

InternalRational::InternalRational | ( | const long | i | ) |

Definition at line 70 of file int_rat.cc.

{
mpz_init_set_si( _num, i );
mpz_init_set_si( _den, 1 );
}

## InternalRational() [6/9]

InternalRational::InternalRational | ( | const long | n,
|  | const long | d )

Definition at line 76 of file int_rat.cc.

{
ASSERT( d != 0, "divide by zero" );
if ( n == 0 )
{
mpz_init_set_si( _num, 0 );
mpz_init_set_si( _den, 1 );
}
else
{
long g = intgcd( n, d );
if ( d < 0 )
{
mpz_init_set_si( _num, -n / g );
mpz_init_set_si( _den, -d / g );
}
else
{
mpz_init_set_si( _num, n / g );
mpz_init_set_si( _den, d / g );
}
}
}

## InternalRational() [7/9]

InternalRational::InternalRational | ( | const char * | str | ) |

Definition at line 100 of file int_rat.cc.

{
// sollte nicht gebraucht werden !!!
ASSERT( 0, "fatal error" );
mpz_init( _num );
mpz_init( _den );
}

## InternalRational() [8/9]

InternalRational::InternalRational | ( | const mpz_ptr | n | ) |

Definition at line 113 of file int_rat.cc.

{
_num[0]=*n;
mpz_init_set_si( _den, 1 );
}

## InternalRational() [9/9]

InternalRational::InternalRational | ( | const mpz_ptr | n,
|  | const mpz_ptr | d )

Definition at line 119 of file int_rat.cc.

{
_num[0]=*n;
_den[0]=*d;
}

## ~InternalRational()

InternalRational::~InternalRational | ( |  | ) |

Definition at line 125 of file int_rat.cc.

{
mpz_clear( _num );
mpz_clear( _den );
}

## Member Function Documentation

## addcoeff()

InternalCF * InternalRational::addcoeff | ( | InternalCF * | c | ) |

virtual

Reimplemented from InternalCF.

Definition at line 560 of file int_rat.cc.

{
ASSERT( ::is_imm( c ) == INTMARK || ! ::is_imm( c ), "expected integer" );
mpz_t n, d;
if ( ::is_imm( c ) )
{
long cc = imm2int( c );
if ( cc == 0 )
return this;
else
{
mpz_init( n );
if ( cc < 0 )
{
mpz_mul_ui( n, _den, -cc );
mpz_sub( n, _num, n );
}
else
{
mpz_mul_ui( n, _den, cc );
mpz_add( n, _num, n );
}
}
}
else
{
ASSERT( c->levelcoeff() == IntegerDomain, "expected integer" );
mpz_init( n );
mpz_mul( n, _den, InternalInteger::MPI( c ) );
mpz_add( n, _num, n );
}
mpz_init_set( d, _den );
// at this point there is no way that the result is not a true rational
if ( deleteObject() ) delete this;
return new InternalRational( n, d );
}
IntegerDomain
#define IntegerDomain
Definition cf_defs.h:21
InternalCF::levelcoeff
virtual int levelcoeff() const
Definition int_cf.h:68
InternalCF::deleteObject
int deleteObject()
Definition int_cf.h:61
InternalInteger::MPI
static mpz_ptr MPI(const InternalCF *const c)
uiNormalizeMPI()' is the same as normalizeMPI()' except that aMpi' is expected to be non-begative.
Definition int_int.h:252
InternalRational::InternalRational
InternalRational()
Definition int_rat.cc:34
InternalRational::is_imm
bool is_imm() const
Definition int_rat.cc:154
imm2int
static long imm2int(const InternalCF *const imm)
Definition imm.h:70
INTMARK
const long INTMARK
Definition imm.h:37

## addsame()

InternalCF * InternalRational::addsame | ( | InternalCF * | c | ) |

virtual

Reimplemented from InternalCF.

Definition at line 234 of file int_rat.cc.

{
ASSERT( ! ::is_imm( c ) && c->levelcoeff() == RationalDomain, "illegal domain" );
mpz_t n, d, g;
mpz_init( g ); mpz_init( n ); mpz_init( d );
mpz_gcd( g, _den, MPQDEN( c ) );
if ( mpz_cmp_si( g, 1 ) == 0 )
{
mpz_mul( n, _den, MPQNUM( c ) );
mpz_mul( g, _num, MPQDEN( c ) );
mpz_add( n, n, g );
mpz_mul( d, _den, MPQDEN( c ) );
}
else
{
mpz_t tmp1;
mpz_t tmp2;
mpz_init( tmp1 );
mpz_divexact( tmp1, _den, g );
mpz_init( tmp2 );
mpz_divexact( tmp2, MPQDEN( c ), g );
mpz_mul( d, tmp2, _den );
mpz_mul( tmp2, tmp2, _num );
mpz_mul( tmp1, tmp1, MPQNUM( c ) );
mpz_add( n, tmp1, tmp2 );
mpz_gcd( g, n, d );
if ( mpz_cmp_si( g, 1 ) != 0 )
{
mpz_divexact( n, n, g );
mpz_divexact( d, d, g );
}
mpz_clear( tmp1 );
mpz_clear( tmp2 );
}
mpz_clear( g );
if ( deleteObject() ) delete this;
if ( mpz_cmp_si( d, 1 ) == 0 )
{
mpz_clear( d );
if ( mpz_is_imm( n ) )
{
InternalCF * res = int2imm( mpz_get_si( n ) );
mpz_clear( n );
return res;
}
else
{
return new InternalInteger( n );
}
}
else
{
return new InternalRational( n, d );
}
}
RationalDomain
#define RationalDomain
Definition cf_defs.h:20
InternalCF::InternalCF
InternalCF()
Definition int_cf.h:55
InternalRational::InternalInteger
friend class InternalInteger
Definition int_rat.h:129
InternalRational::MPQDEN
static mpz_ptr MPQDEN(const InternalCF *const c)
Definition int_rat.h:143
InternalRational::MPQNUM
static mpz_ptr MPQNUM(const InternalCF *const c)
Definition int_rat.h:138
res
CanonicalForm res
Definition facAbsFact.cc:60
tmp1
CFList tmp1
Definition facFqBivar.cc:75
tmp2
CFList tmp2
Definition facFqBivar.cc:75
mpz_is_imm
bool mpz_is_imm(const mpz_t mpi)
Definition gmpext.h:19
int2imm
static InternalCF * int2imm(long i)
Definition imm.h:75

## bextgcdcoeff()

InternalCF * InternalRational::bextgcdcoeff | ( | InternalCF * | ,
|  | CanonicalForm & | a,
|  | CanonicalForm & | b )

virtual

See also
  CanonicalForm::bextgcd(), InternalRational::bextgcdsame()

Reimplemented from InternalCF.

Definition at line 850 of file int_rat.cc.

{
a = 1/CanonicalForm( copyObject() ); b = 0;
return int2imm( 1 );
}
b
CanonicalForm b
Definition cfModGcd.cc:4111
InternalCF::copyObject
InternalCF * copyObject()
Definition int_cf.h:62

## bextgcdsame()

InternalCF * InternalRational::bextgcdsame | ( | InternalCF * | ,
|  | CanonicalForm & | a,
|  | CanonicalForm & | b )

virtual

See also
  CanonicalForm::bextgcd(), InternalRational::bextgcdcoeff()

Reimplemented from InternalCF.

Definition at line 840 of file int_rat.cc.

{
a = 1/CanonicalForm( copyObject() ); b = 0;
return int2imm( 1 );
}

## bgcdcoeff()

InternalCF * InternalRational::bgcdcoeff | ( | const InternalCF * const |  | ) |

virtual

See also
  CanonicalForm::bgcd(), InternalRational::bgcdsame()

Reimplemented from InternalCF.

Definition at line 831 of file int_rat.cc.

{
return int2imm( 1 );
}

## bgcdsame()

InternalCF * InternalRational::bgcdsame | ( | const InternalCF * const |  | ) | const

virtual

See also
  CanonicalForm::bgcd(), InternalRational::bgcdcoeff()

Reimplemented from InternalCF.

Definition at line 822 of file int_rat.cc.

{
return int2imm( 1 );
}

## classname()

const char * InternalRational::classname | ( |  | ) | const

inline virtual

Reimplemented from InternalCF.

Definition at line 78 of file int_rat.h.

{ return "InternalRational"; }

## comparecoeff()

int InternalRational::comparecoeff | ( | InternalCF * | c | ) |

virtual

comparecoeff() compares the CO=a/b and the integer c using the equivalence a/b < c iff a < c*b.

Note: May be relatively expensive due to the multiplications.

Reimplemented from InternalCF.

Definition at line 536 of file int_rat.cc.

{
if ( ::is_imm( c ) )
{
ASSERT( ::is_imm( c ) == INTMARK, "incompatible base coefficients" );
mpz_t dummy;
mpz_init_set_si( dummy, imm2int( c ) );
mpz_mul( dummy, dummy, _den );
int result = mpz_cmp( _num, dummy );
mpz_clear( dummy );
return result;
}
else
{
ASSERT( c->levelcoeff() == IntegerDomain, "incompatible base coefficients" );
mpz_t dummy;
mpz_init( dummy );
mpz_mul( dummy, _den, InternalInteger::MPI( c ) );
int result = mpz_cmp( _num, dummy );
mpz_clear( dummy );
return result;
}
}
result
return result
Definition facAbsBiFact.cc:76

## comparesame()

int InternalRational::comparesame | ( | InternalCF * | c | ) |

virtual

comparesame(), comparecoeff() - compare with an InternalRational.

comparesame() compares the CO=a/b and c=p/q using the equivalence a/b < p/q iff a*q < p*b.

Note: May be relatively expensive due to the multiplications.

See also: CanonicalForm::operator <(), CanonicalForm::operator ==()

Reimplemented from InternalCF.

Definition at line 516 of file int_rat.cc.

{
ASSERT( ! ::is_imm( c ) && c->levelcoeff() == RationalDomain, "incompatible base coefficients" );
mpz_t dummy1, dummy2;
mpz_init( dummy1 ); mpz_init( dummy2 );
mpz_mul( dummy1, _num, MPQDEN( c ) );
mpz_mul( dummy2, _den, MPQNUM( c ) );
int result = mpz_cmp( dummy1, dummy2 );
mpz_clear( dummy1 ); mpz_clear( dummy2 );
return result;
}

## deepCopyObject()

InternalCF * InternalRational::deepCopyObject | ( |  | ) | const

virtual

Reimplemented from InternalCF.

Definition at line 131 of file int_rat.cc.

{
mpz_t dummy_num;
mpz_t dummy_den;
mpz_init_set( dummy_num, _num );
mpz_init_set( dummy_den, _den );
return new InternalRational( dummy_num, dummy_den );
}

## den()

InternalCF * InternalRational::den | ( |  | ) |

virtual

See also
  CanonicalForm::num(), CanonicalForm::den(), InternalRational::num()

Reimplemented from InternalCF.

Definition at line 196 of file int_rat.cc.

{
if ( mpz_is_imm( _den ) )
{
InternalCF * res = int2imm( mpz_get_si( _den ) );
return res;
}
else
{
mpz_t dummy;
mpz_init_set( dummy, _den );
return new InternalInteger( dummy );
}
}

## divcoeff()

InternalCF * InternalRational::divcoeff | ( | InternalCF * | c,
|  | bool | invert )

virtual

Reimplemented from InternalCF.

Definition at line 787 of file int_rat.cc.

{
return dividecoeff( c, invert );
}
InternalCF::invert
virtual InternalCF * invert()
Definition int_cf.cc:172
InternalRational::dividecoeff
InternalCF * dividecoeff(InternalCF *, bool)
Definition int_rat.cc:707

## dividecoeff()

InternalCF * InternalRational::dividecoeff | ( | InternalCF * | c,
|  | bool | invert )

virtual

Reimplemented from InternalCF.

Definition at line 707 of file int_rat.cc.

{
ASSERT( ::is_imm( c ) == INTMARK || ! ::is_imm( c ), "expected integer" );
mpz_t n, d, g;
if ( ::is_imm( c ) )
{
long cc = imm2int( c );
ASSERT( c != 0 || invert, "divide by zero" );
if ( cc == 0 )
{
// => invert
if ( deleteObject() ) delete this;
return CFFactory::basic( 0 );
}
if ( invert )
{
mpz_init_set_si( n, cc );
mpz_mul( n, n, _den );
mpz_init_set( d, _num );
}
else
{
mpz_init_set_si( d, cc );
mpz_mul( d, d, _den );
mpz_init_set( n, _num );
}
}
else
{
ASSERT( c->levelcoeff() == IntegerDomain, "expected integer" );
if ( invert )
{
mpz_init_set( n, InternalInteger::MPI( c ) );
mpz_mul( n, n, _den );
mpz_init_set( d, _num );
}
else
{
mpz_init_set( d, InternalInteger::MPI( c ) );
mpz_mul( d, d, _den );
mpz_init_set( n, _num );
}
}
if ( mpz_cmp_si( d, 0 ) < 0 )
{
mpz_neg( d, d );
mpz_neg( n, n );
}
mpz_init( g );
mpz_gcd( g, n, d );
if ( mpz_cmp_si( g, 1 ) != 0 )
{
mpz_divexact( d, d, g );
mpz_divexact( n, n, g );
}
mpz_clear( g );
if ( deleteObject() ) delete this;
if ( ! invert )
{
// then there was no way for the result to become an integer
return new InternalRational( n, d );
}
if ( mpz_cmp_si( d, 1 ) == 0 )
{
mpz_clear( d );
if ( mpz_is_imm( n ) )
{
InternalCF * res = int2imm( mpz_get_si( n ) );
mpz_clear( n );
return res;
}
else
{
return new InternalInteger( n );
}
}
else
return new InternalRational( n, d );
}
CFFactory::basic
static InternalCF * basic(int value)
Definition cf_factory.cc:61

## dividesame()

InternalCF * InternalRational::dividesame | ( | InternalCF * | c | ) |

virtual

Reimplemented from InternalCF.

Definition at line 408 of file int_rat.cc.

{
ASSERT( ! ::is_imm( c ) && c->levelcoeff() == RationalDomain, "illegal domain" );
if ( this == c )
{
if ( deleteObject() ) delete this;
return CFFactory::basic( 1 );
}
else
{
mpz_t n, d;
mpz_t g1, g2, tmp1, tmp2;
mpz_init( n ); mpz_init( d );
mpz_init( g1 ); mpz_init( g2 );
mpz_gcd( g1, _num, MPQNUM( c ) );
mpz_gcd( g2, _den, MPQDEN( c ) );
bool g1is1 = mpz_cmp_si( g1, 1 ) == 0;
bool g2is1 = mpz_cmp_si( g2, 1 ) == 0;
mpz_init( tmp1 ); mpz_init( tmp2 );
if ( ! g1is1 )
mpz_divexact( tmp1, _num, g1 );
else
mpz_set( tmp1, _num );
if ( ! g2is1 )
mpz_divexact( tmp2, MPQDEN( c ), g2 );
else
mpz_set( tmp2, MPQDEN( c ) );
mpz_mul( n, tmp1, tmp2 );
if ( ! g1is1 )
mpz_divexact( tmp1, MPQNUM( c ), g1 );
else
mpz_set( tmp1, MPQNUM( c ) );
if ( ! g2is1 )
mpz_divexact( tmp2, _den, g2 );
else
mpz_set( tmp2, _den );
mpz_mul( d, tmp1, tmp2 );
mpz_clear( tmp1 ); mpz_clear( tmp2 );
mpz_clear( g1 ); mpz_clear( g2 );
if ( deleteObject() ) delete this;
if ( mpz_cmp_si( d, 0 ) < 0 )
{
mpz_neg( d, d );
mpz_neg( n, n );
}
if ( mpz_cmp_si( d, 1 ) == 0 )
{
mpz_clear( d );
if ( mpz_is_imm( n ) )
{
InternalCF * res = int2imm( mpz_get_si( n ) );
mpz_clear( n );
return res;
}
else
{
return new InternalInteger( n );
}
}
else
return new InternalRational( n, d );
}
}

## divremcoeff()

void InternalRational::divremcoeff | ( | InternalCF * | c,
|  | InternalCF *& | quot,
|  | InternalCF *& | rem,
|  | bool | invert )

virtual

Reimplemented from InternalCF.

Definition at line 805 of file int_rat.cc.

{
quot = copyObject();
quot = quot->dividecoeff( c, invert );
rem = CFFactory::basic( 0 );
}
InternalCF::dividecoeff
virtual InternalCF * dividecoeff(InternalCF *, bool) PVIRT_INTCF("dividecoeff")
rem
void rem(unsigned long *a, unsigned long *q, unsigned long p, int &dega, int degq)
Definition minpoly.cc:572

## divremcoefft()

bool InternalRational::divremcoefft | ( | InternalCF * | c,
|  | InternalCF *& | quot,
|  | InternalCF *& | rem,
|  | bool | invert )

virtual

Reimplemented from InternalCF.

Definition at line 812 of file int_rat.cc.

{
divremcoeff( c, quot, rem, invert );
return true;
}
InternalRational::divremcoeff
void divremcoeff(InternalCF *, InternalCF *&, InternalCF *&, bool)
Definition int_rat.cc:805

## divremsame()

void InternalRational::divremsame | ( | InternalCF * | c,
|  | InternalCF *& | quot,
|  | InternalCF *& | rem )

virtual

Reimplemented from InternalCF.

Definition at line 489 of file int_rat.cc.

{
quot = copyObject();
quot = quot->dividesame( c );
rem = CFFactory::basic( 0 );
}
InternalCF::dividesame
virtual InternalCF * dividesame(InternalCF *) PVIRT_INTCF("dividesame")

## divremsamet()

bool InternalRational::divremsamet | ( | InternalCF * | c,
|  | InternalCF *& | quot,
|  | InternalCF *& | rem )

virtual

Reimplemented from InternalCF.

Definition at line 496 of file int_rat.cc.

{
divremsame( c, quot, rem );
return true;
}
InternalRational::divremsame
void divremsame(InternalCF *, InternalCF *&, InternalCF *&)
Definition int_rat.cc:489

## divsame()

InternalCF * InternalRational::divsame | ( | InternalCF * | c | ) |

virtual

Reimplemented from InternalCF.

Definition at line 473 of file int_rat.cc.

{
return dividesame( c );
}
InternalRational::dividesame
InternalCF * dividesame(InternalCF *)
Definition int_rat.cc:408

## genOne()

InternalCF * InternalRational::genOne | ( |  | ) |

virtual

Reimplemented from InternalCF.

Definition at line 167 of file int_rat.cc.

{
if ( isOne() )
return copyObject();
else
return new InternalRational( 1 );
}
InternalCF::isOne
virtual bool isOne() const
bool InternalCF::isOne, isZero () const
Definition int_cf.cc:18

## genZero()

InternalCF * InternalRational::genZero | ( |  | ) |

virtual

Reimplemented from InternalCF.

Definition at line 159 of file int_rat.cc.

{
if ( isZero() )
return copyObject();
else
return new InternalRational();
}
InternalCF::isZero
virtual bool isZero() const
Definition int_cf.cc:24

## intval()

long InternalRational::intval | ( |  | ) | const

virtual

Reimplemented from InternalCF.

Definition at line 897 of file int_rat.cc.

{
ASSERT( mpz_cmp_si( _den, 1 ) == 0, "illegal operation" );
return mpz_get_si( _num );
}

## is_imm()

bool InternalRational::is_imm | ( |  | ) | const

Definition at line 154 of file int_rat.cc.

{
return mpz_cmp_si( _den, 1 ) == 0 && mpz_is_imm( _num );
}

## levelcoeff()

int InternalRational::levelcoeff | ( |  | ) | const

inline virtual

Reimplemented from InternalCF.

Definition at line 86 of file int_rat.h.

{ return RationalDomain; }

## modcoeff()

InternalCF * InternalRational::modcoeff | ( | InternalCF * | c,
|  | bool | invert )

virtual

Reimplemented from InternalCF.

Definition at line 797 of file int_rat.cc.

{
ASSERT( ::is_imm( c ) == INTMARK || ! ::is_imm( c ), "integer expected" );
ASSERT( invert || ! ::is_imm( c ) || imm2int( c ) != 0, "divide by zero" );
if ( deleteObject() ) delete this;
return CFFactory::basic( 0 );
}

## modsame()

InternalCF * InternalRational::modsame | ( | InternalCF * |  | ) |

virtual

Reimplemented from InternalCF.

Definition at line 483 of file int_rat.cc.

{
if ( deleteObject() ) delete this;
return CFFactory::basic( 0 );
}

## modulocoeff()

InternalCF * InternalRational::modulocoeff | ( | InternalCF * | c,
|  | bool | invert )

virtual

Reimplemented from InternalCF.

Definition at line 792 of file int_rat.cc.

{
return modcoeff( c, invert );
}
InternalRational::modcoeff
InternalCF * modcoeff(InternalCF *, bool)
Definition int_rat.cc:797

## modulosame()

InternalCF * InternalRational::modulosame | ( | InternalCF * | c | ) |

virtual

Reimplemented from InternalCF.

Definition at line 478 of file int_rat.cc.

{
return modsame( c );
}
InternalRational::modsame
InternalCF * modsame(InternalCF *)
Definition int_rat.cc:483

## MPQDEN()

mpz_ptr InternalRational::MPQDEN | ( | const InternalCF *const | c | ) |

inline static private

Definition at line 143 of file int_rat.h.

{
return (((InternalRational*)c)->_den);
}

## MPQNUM()

mpz_ptr InternalRational::MPQNUM | ( | const InternalCF *const | c | ) |

inline static private

Definition at line 138 of file int_rat.h.

{
return (((InternalRational*)c)->_num);
}

## mulcoeff()

InternalCF * InternalRational::mulcoeff | ( | InternalCF * | c | ) |

virtual

Reimplemented from InternalCF.

Definition at line 654 of file int_rat.cc.

{
ASSERT( ::is_imm( c ) == INTMARK || ! ::is_imm( c ), "expected integer" );
mpz_t n, d, g;
if ( ::is_imm( c ) )
{
long cc = imm2int( c );
if ( cc == 0 )
{
if ( deleteObject() ) delete this;
return CFFactory::basic( 0 );
}
mpz_init_set_si( n, cc );
}
else
{
ASSERT( c->levelcoeff() == IntegerDomain, "expected integer" );
mpz_init_set( n, InternalInteger::MPI( c ) );
}
mpz_init( g );
mpz_gcd( g, n, _den );
if ( mpz_cmp_si( g, 1 ) == 0 )
{
mpz_mul( n, n, _num );
mpz_init_set( d, _den );
}
else
{
mpz_divexact( n, n, g );
mpz_mul( n, n, _num );
mpz_init( d );
mpz_divexact( d, _den, g );
}
mpz_clear( g );
if ( deleteObject() ) delete this;
if ( mpz_cmp_si( d, 1 ) == 0 )
{
mpz_clear( d );
if ( mpz_is_imm( n ) )
{
InternalCF * res = int2imm( mpz_get_si( n ) );
mpz_clear( n );
return res;
}
else
{
return new InternalInteger( n );
}
}
else
return new InternalRational( n, d );
}

## mulsame()

InternalCF * InternalRational::mulsame | ( | InternalCF * | c | ) |

virtual

Reimplemented from InternalCF.

Definition at line 348 of file int_rat.cc.

{
ASSERT( ! ::is_imm( c ) && c->levelcoeff() == RationalDomain, "illegal domain" );
mpz_t n, d;
mpz_init( n ); mpz_init( d );
if ( this == c )
{
mpz_mul( n, _num, _num );
mpz_mul( d, _den, _den );
}
else
{
mpz_t g1, g2, tmp1, tmp2;
mpz_init( g1 ); mpz_init( g2 );
mpz_gcd( g1, _num, MPQDEN( c ) );
mpz_gcd( g2, _den, MPQNUM( c ) );
bool g1is1 = mpz_cmp_si( g1, 1 ) == 0;
bool g2is1 = mpz_cmp_si( g2, 1 ) == 0;
mpz_init( tmp1 ); mpz_init( tmp2 );
if ( ! g1is1 )
mpz_divexact( tmp1, _num, g1 );
else
mpz_set( tmp1, _num );
if ( ! g2is1 )
mpz_divexact( tmp2, MPQNUM( c ), g2 );
else
mpz_set( tmp2, MPQNUM( c ) );
mpz_mul( n, tmp1, tmp2 );
if ( ! g1is1 )
mpz_divexact( tmp1, MPQDEN( c ), g1 );
else
mpz_set( tmp1, MPQDEN( c ) );
if ( ! g2is1 )
mpz_divexact( tmp2, _den, g2 );
else
mpz_set( tmp2, _den );
mpz_mul( d, tmp1, tmp2 );
mpz_clear( tmp1 ); mpz_clear( tmp2 );
mpz_clear( g1 ); mpz_clear( g2 );
}
if ( deleteObject() ) delete this;
if ( mpz_cmp_si( d, 1 ) == 0 )
{
mpz_clear( d );
if ( mpz_is_imm( n ) )
{
InternalCF * res = int2imm( mpz_get_si( n ) );
mpz_clear( n );
return res;
}
else
{
return new InternalInteger( n );
}
}
else
return new InternalRational( n, d );
}

## neg()

InternalCF * InternalRational::neg | ( |  | ) |

virtual

InternalCF * InternalRational::neg ().

See also
  CanonicalForm::operator -()

Reimplemented from InternalCF.

Definition at line 215 of file int_rat.cc.

{
if ( getRefCount() > 1 )
{
decRefCount();
mpz_t dummy_num;
mpz_t dummy_den;
mpz_init_set( dummy_num, _num );
mpz_init_set( dummy_den, _den );
mpz_neg( dummy_num, dummy_num );
return new InternalRational( dummy_num, dummy_den );
}
else
{
mpz_neg( _num, _num );
return this;
}
}
InternalCF::getRefCount
int getRefCount()
Definition int_cf.h:51
InternalCF::decRefCount
int decRefCount()
Definition int_cf.h:53

## normalize()

void InternalRational::normalize | ( | const mpz_ptr | ,
|  | const mpz_ptr | ,
|  | mpz_ptr | ,
|  | mpz_ptr | )

static private

## normalize_myself()

InternalCF * InternalRational::normalize_myself | ( |  | ) |

reduce InternalRational to lowest terms

Definition at line 859 of file int_rat.cc.

{
ASSERT( getRefCount() == 1, "illegal operation" );
mpz_t g;
mpz_init( g );
mpz_gcd( g, _num, _den );
if ( mpz_cmp_si( g, 1 ) != 0 )
{
mpz_divexact( _num, _num, g );
mpz_divexact( _den, _den, g );
}
mpz_clear( g );
if ( mpz_cmp_si( _den, 0 ) < 0 )
{
mpz_neg( _num, _num );
mpz_neg( _den, _den );
}
if ( mpz_cmp_si( _den, 1 ) == 0 )
{
if ( mpz_is_imm( _num ) )
{
InternalCF * res = int2imm( mpz_get_si( _num ) );
delete this;
return res;
}
else
{
mpz_t res;
mpz_init_set( res, _num );
delete this;
return new InternalInteger( res );
}
}
else
return this;
}

## num()

InternalCF * InternalRational::num | ( |  | ) |

virtual

See also
  CanonicalForm::num(), CanonicalForm::den(), InternalRational::den()

Reimplemented from InternalCF.

Definition at line 178 of file int_rat.cc.

{
if ( mpz_is_imm( _num ) )
{
InternalCF * res = int2imm( mpz_get_si( _num ) );
return res;
}
else
{
mpz_t dummy;
mpz_init_set( dummy, _num );
return new InternalInteger( dummy );
}
}

## print()

void InternalRational::print | ( | OSTREAM & | os,
|  | char * | c )

virtual

Reimplemented from InternalCF.

Definition at line 141 of file int_rat.cc.

{
char * str = new char[mpz_sizeinbase( _num, 10 ) + 2];
str = mpz_get_str( str, 10, _num );
os << str << '/';
delete [] str;
str = new char[mpz_sizeinbase( _den, 10 ) + 2];
str = mpz_get_str( str, 10, _den );
os << str << c;
delete [] str;
}
LibThread::str
char * str(leftv arg)
Definition shared.cc:699

## sign()

int InternalRational::sign | ( |  | ) | const

virtual

See also
  CanonicalForm::sign()

Reimplemented from InternalCF.

Definition at line 909 of file int_rat.cc.

{
return mpz_sgn( _num );
}

## subcoeff()

InternalCF * InternalRational::subcoeff | ( | InternalCF * | c,
|  | bool | negate )

virtual

Reimplemented from InternalCF.

Definition at line 597 of file int_rat.cc.

{
ASSERT( ::is_imm( c ) == INTMARK || ! ::is_imm( c ), "expected integer" );
mpz_t n, d;
if ( ::is_imm( c ) )
{
long cc = imm2int( c );
if ( cc == 0 )
{
if ( negate )
{
if ( getRefCount() == 1 )
{
mpz_neg( _num, _num );
return this;
}
else
{
decRefCount();
mpz_init_set( d, _den );
mpz_init_set( n, _num );
mpz_neg( n, n );
return new InternalRational( n, d );
}
}
else
return this;
}
mpz_init( n );
if ( cc < 0 )
{
mpz_mul_ui( n, _den, -cc );
mpz_neg( n, n );
}
else
mpz_mul_ui( n, _den, cc );
if ( negate )
mpz_sub( n, n, _num );
else
mpz_sub( n, _num, n );
}
else
{
ASSERT( c->levelcoeff() == IntegerDomain, "expected integer" );
mpz_init( n );
mpz_mul( n, _den, InternalInteger::MPI( c ) );
if ( negate )
mpz_sub( n, n, _num );
else
mpz_sub( n, _num, n );
}
mpz_init_set( d, _den );
// at this point there is no way that the result is not a true rational
if ( deleteObject() ) delete this;
return new InternalRational( n, d );
}

## subsame()

InternalCF * InternalRational::subsame | ( | InternalCF * | c | ) |

virtual

Reimplemented from InternalCF.

Definition at line 292 of file int_rat.cc.

{
ASSERT( ! ::is_imm( c ) && c->levelcoeff() == RationalDomain, "illegal domain" );
mpz_t n, d, g;
mpz_init( g ); mpz_init( n ); mpz_init( d );
mpz_gcd( g, _den, MPQDEN( c ) );
if ( mpz_cmp_si( g, 1 ) == 0 )
{
mpz_mul( n, _den, MPQNUM( c ) );
mpz_mul( g, _num, MPQDEN( c ) );
mpz_sub( n, g, n );
mpz_mul( d, _den, MPQDEN( c ) );
}
else
{
mpz_t tmp1;
mpz_t tmp2;
mpz_init( tmp1 );
mpz_divexact( tmp1, _den, g );
mpz_init( tmp2 );
mpz_divexact( tmp2, MPQDEN( c ), g );
mpz_mul( d, tmp2, _den );
mpz_mul( tmp2, tmp2, _num );
mpz_mul( tmp1, tmp1, MPQNUM( c ) );
mpz_sub( n, tmp2, tmp1 );
mpz_gcd( g, n, d );
if ( mpz_cmp_si( g, 1 ) != 0 )
{
mpz_divexact( n, n, g );
mpz_divexact( d, d, g );
}
mpz_clear( tmp1 );
mpz_clear( tmp2 );
}
mpz_clear( g );
if ( deleteObject() ) delete this;
if ( mpz_cmp_si( d, 1 ) == 0 )
{
mpz_clear( d );
if ( mpz_is_imm( n ) )
{
InternalCF * res = int2imm( mpz_get_si( n ) );
mpz_clear( n );
return res;
}
else
{
return new InternalInteger( n );
}
}
else
return new InternalRational( n, d );
}

## Friends And Related Symbol Documentation

## convertCF2Fmpq

void convertCF2Fmpq | ( | fmpq_t | result,
|  | const CanonicalForm & | f )

friend

conversion of a factory rationals to fmpq_t

Parameters

  [in,out] | result | an fmpq_t
  [in] | f | a CanonicalForm wrapping a rational

Definition at line 220 of file FLINTconvert.cc.

{
//ASSERT (isOn (SW_RATIONAL), "expected rational");
if (f.isImm ())
{
fmpq_set_si(result, f.intval(), 1);
}
else if(f.inQ())
{
InternalCF *fi=f.getval();
if (fi->levelcoeff() == RationalDomain)
{
fmpz_set_mpz(fmpq_numref(result), InternalRational::MPQNUM( fi ));
fmpz_set_mpz(fmpq_denref(result), InternalRational::MPQDEN( fi ));
}
else
{
mpz_t gmp_val;
gmp_numerator (f, gmp_val);
fmpz_set_mpz (fmpq_numref (result), gmp_val);
mpz_clear (gmp_val);
gmp_denominator (f, gmp_val);
fmpz_set_mpz (fmpq_denref (result), gmp_val);
mpz_clear (gmp_val);
}
fi->deleteObject();
}
else if(f.inZ()) /* and not isImm() */
{
InternalInteger *fi=(InternalInteger*)f.getval();
fmpz_set_mpz (fmpq_numref (result), fi->thempi);
fmpz_one(fmpq_denref(result));
fi->deleteObject();
}
else
{
printf("wrong type\n");
}
}
f
FILE * f
Definition checklibs.c:9
InternalInteger::thempi
mpz_t thempi
Definition int_int.h:58
InternalRational::gmp_denominator
friend void gmp_denominator(const CanonicalForm &f, mpz_ptr result)
Definition singext.cc:40
InternalRational::gmp_numerator
friend void gmp_numerator(const CanonicalForm &f, mpz_ptr result)
Definition singext.cc:20

## gmp_denominator

void gmp_denominator | ( | const CanonicalForm & | f,
|  | mpz_ptr | result )

friend

Definition at line 40 of file singext.cc.

{
InternalCF * ff = f.getval();
ASSERT( ! is_imm( ff ), "illegal type" );
if ( ff->levelcoeff() == IntegerDomain )
{
mpz_init_set_si( result, 1 );
ff->deleteObject();
}
else if ( ff->levelcoeff() == RationalDomain )
{
mpz_init_set( result, (InternalRational::MPQDEN( ff )) );
ff->deleteObject();
}
else
{
ASSERT( 0, "illegal type" );
}
}

## gmp_numerator

void gmp_numerator | ( | const CanonicalForm & | f,
|  | mpz_ptr | result )

friend

Definition at line 20 of file singext.cc.

{
InternalCF * ff = f.getval();
ASSERT( ! is_imm( ff ), "illegal type" );
if ( ff->levelcoeff() == IntegerDomain )
{
mpz_init_set( result, (InternalInteger::MPI( ff )) );
ff->deleteObject();
}
else if ( ff->levelcoeff() == RationalDomain )
{
mpz_init_set( result, (InternalRational::MPQNUM( ff )) );
ff->deleteObject();
}
else
{
ASSERT( 0, "illegal type" );
}
}

## InternalInteger

friend class InternalInteger

friend

Definition at line 129 of file int_rat.h.

## make_cf

CanonicalForm make_cf | ( | const mpz_ptr | n,
|  | const mpz_ptr | d )

friend

## Field Documentation

## _den

mpz_t InternalRational::_den

private

Definition at line 58 of file int_rat.h.

## _num

mpz_t InternalRational::_num

private

Definition at line 57 of file int_rat.h.

## initialized

STATIC_VAR int InternalRational::initialized

private

Definition at line 59 of file int_rat.h.

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

- factory/int_rat.h
- factory/int_rat.cc