My Project  UNKNOWN_GIT_VERSION
Public Member Functions | Static Private Member Functions | Private Attributes | Static Private Attributes | Friends
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 ()
 
InternalCFdeepCopyObject () const
 
const char * classname () const
 
void print (OSTREAM &, char *)
 
InternalCFgenZero ()
 
InternalCFgenOne ()
 
bool is_imm () const
 
int levelcoeff () const
 
InternalCFnum ()
 
InternalCFden ()
 
InternalCFneg ()
 InternalCF * InternalRational::neg () More...
 
int comparesame (InternalCF *)
 comparesame(), comparecoeff() - compare with an InternalRational. More...
 
InternalCFaddsame (InternalCF *)
 
InternalCFsubsame (InternalCF *)
 
InternalCFmulsame (InternalCF *)
 
InternalCFdividesame (InternalCF *)
 
InternalCFmodulosame (InternalCF *)
 
InternalCFdivsame (InternalCF *)
 
InternalCFmodsame (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. More...
 
InternalCFaddcoeff (InternalCF *)
 
InternalCFsubcoeff (InternalCF *, bool)
 
InternalCFmulcoeff (InternalCF *)
 
InternalCFdividecoeff (InternalCF *, bool)
 
InternalCFmodulocoeff (InternalCF *, bool)
 
InternalCFdivcoeff (InternalCF *, bool)
 
InternalCFmodcoeff (InternalCF *, bool)
 
void divremcoeff (InternalCF *, InternalCF *&, InternalCF *&, bool)
 
bool divremcoefft (InternalCF *, InternalCF *&, InternalCF *&, bool)
 
InternalCFbgcdsame (const InternalCF *const) const
 
InternalCFbgcdcoeff (const InternalCF *const)
 
InternalCFbextgcdsame (InternalCF *, CanonicalForm &, CanonicalForm &)
 
InternalCFbextgcdcoeff (InternalCF *, CanonicalForm &, CanonicalForm &)
 
long intval () const
 
int sign () const
 
InternalCFnormalize_myself ()
 reduce InternalRational to lowest terms More...
 
- Public Member Functions inherited from InternalCF
 InternalCF ()
 
 InternalCF (const InternalCF &)
 
virtual ~InternalCF ()
 
int deleteObject ()
 
InternalCFcopyObject ()
 
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 More...
 
virtual bool isUnivariate () const
 
virtual int intmod (int) const
 
virtual InternalCFinvert ()
 
virtual InternalCFtryInvert (const CanonicalForm &, bool &)
 
virtual InternalCFtryMulsame (InternalCF *, const CanonicalForm &)
 
virtual InternalCFtryDivsame (InternalCF *, const CanonicalForm &, bool &)
 
virtual bool tryDivremsamet (InternalCF *, InternalCF *&, InternalCF *&, const CanonicalForm &, bool &)
 
virtual InternalCFtryDividecoeff (InternalCF *, bool, const CanonicalForm &, bool &)
 
virtual InternalCFtryDivcoeff (InternalCF *, bool, const CanonicalForm &, bool &)
 
virtual bool tryDivremcoefft (InternalCF *, InternalCF *&, InternalCF *&, bool, const CanonicalForm &, bool &)
 
virtual InternalCFsqrt ()
 InternalCF * InternalCF::sqrt () More...
 
virtual int ilog2 ()
 int InternalCF::ilog2 () More...
 
virtual CanonicalForm lc ()
 CanonicalForm InternalCF::lc (), Lc (), LC () More...
 
virtual CanonicalForm Lc ()
 
virtual CanonicalForm LC ()
 
virtual CanonicalForm coeff (int i)
 CanonicalForm InternalCF::coeff ( int i ) More...
 
virtual int degree ()
 int InternalCF::degree () More...
 
virtual int taildegree ()
 
virtual CanonicalForm tailcoeff ()
 CanonicalForm InternalCF::tailcoeff (), int InternalCF::taildegree () More...
 
- 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 Private Attributes

static 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)
 

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 38 of file int_rat.h.

Constructor & Destructor Documentation

◆ InternalRational() [1/9]

InternalRational::InternalRational ( )

Definition at line 34 of file int_rat.cc.

35 {
36  mpz_init( _num );
37  mpz_init_set_si( _den, 1 );
38 }

◆ InternalRational() [2/9]

InternalRational::InternalRational ( const InternalCF )
inline

Definition at line 49 of file int_rat.h.

50  {
51  ASSERT( 0, "ups there is something wrong in your code" );
52  }

◆ InternalRational() [3/9]

InternalRational::InternalRational ( const int  i)

Definition at line 40 of file int_rat.cc.

41 {
42  mpz_init_set_si( _num, i );
43  mpz_init_set_si( _den, 1 );
44 }

◆ InternalRational() [4/9]

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

Definition at line 46 of file int_rat.cc.

47 {
48  ASSERT( d != 0, "divide by zero" );
49  if ( n == 0 )
50  {
51  mpz_init_set_si( _num, 0 );
52  mpz_init_set_si( _den, 1 );
53  }
54  else
55  {
56  long g = intgcd( (long) n, (long) d );
57  if ( d < 0 )
58  {
59  mpz_init_set_si( _num, -((long)n) / g );
60  mpz_init_set_si( _den, -((long)d) / g );
61  }
62  else
63  {
64  mpz_init_set_si( _num, n / g );
65  mpz_init_set_si( _den, d / g );
66  }
67  }
68 }

◆ InternalRational() [5/9]

InternalRational::InternalRational ( const long  i)

Definition at line 70 of file int_rat.cc.

71 {
72  mpz_init_set_si( _num, i );
73  mpz_init_set_si( _den, 1 );
74 }

◆ InternalRational() [6/9]

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

Definition at line 76 of file int_rat.cc.

77 {
78  ASSERT( d != 0, "divide by zero" );
79  if ( n == 0 )
80  {
81  mpz_init_set_si( _num, 0 );
82  mpz_init_set_si( _den, 1 );
83  }
84  else
85  {
86  long g = intgcd( n, d );
87  if ( d < 0 )
88  {
89  mpz_init_set_si( _num, -n / g );
90  mpz_init_set_si( _den, -d / g );
91  }
92  else
93  {
94  mpz_init_set_si( _num, n / g );
95  mpz_init_set_si( _den, d / g );
96  }
97  }
98 }

◆ InternalRational() [7/9]

InternalRational::InternalRational ( const char *  str)

Definition at line 100 of file int_rat.cc.

101 {
102  // sollte nicht gebraucht werden !!!
103  ASSERT( 0, "fatal error" );
104  mpz_init( _num );
105  mpz_init( _den );
106 }

◆ InternalRational() [8/9]

InternalRational::InternalRational ( const mpz_ptr  n)

Definition at line 113 of file int_rat.cc.

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

◆ InternalRational() [9/9]

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

Definition at line 119 of file int_rat.cc.

120 {
121  _num[0]=*n;
122  _den[0]=*d;
123 }

◆ ~InternalRational()

InternalRational::~InternalRational ( )

Definition at line 125 of file int_rat.cc.

126 {
127  mpz_clear( _num );
128  mpz_clear( _den );
129 }

Member Function Documentation

◆ addcoeff()

InternalCF * InternalRational::addcoeff ( InternalCF c)
virtual

Reimplemented from InternalCF.

Definition at line 560 of file int_rat.cc.

561 {
562  ASSERT( ::is_imm( c ) == INTMARK || ! ::is_imm( c ), "expected integer" );
563  mpz_t n, d;
564  if ( ::is_imm( c ) )
565  {
566  long cc = imm2int( c );
567  if ( cc == 0 )
568  return this;
569  else
570  {
571  mpz_init( n );
572  if ( cc < 0 )
573  {
574  mpz_mul_ui( n, _den, -cc );
575  mpz_sub( n, _num, n );
576  }
577  else
578  {
579  mpz_mul_ui( n, _den, cc );
580  mpz_add( n, _num, n );
581  }
582  }
583  }
584  else
585  {
586  ASSERT( c->levelcoeff() == IntegerDomain, "expected integer" );
587  mpz_init( n );
588  mpz_mul( n, _den, InternalInteger::MPI( c ) );
589  mpz_add( n, _num, n );
590  }
591  mpz_init_set( d, _den );
592  // at this point there is no way that the result is not a true rational
593  if ( deleteObject() ) delete this;
594  return new InternalRational( n, d );
595 }

◆ addsame()

InternalCF * InternalRational::addsame ( InternalCF c)
virtual

Reimplemented from InternalCF.

Definition at line 234 of file int_rat.cc.

235 {
236  ASSERT( ! ::is_imm( c ) && c->levelcoeff() == RationalDomain, "illegal domain" );
237  mpz_t n, d, g;
238 
239  mpz_init( g ); mpz_init( n ); mpz_init( d );
240  mpz_gcd( g, _den, MPQDEN( c ) );
241 
242  if ( mpz_cmp_si( g, 1 ) == 0 )
243  {
244  mpz_mul( n, _den, MPQNUM( c ) );
245  mpz_mul( g, _num, MPQDEN( c ) );
246  mpz_add( n, n, g );
247  mpz_mul( d, _den, MPQDEN( c ) );
248  }
249  else
250  {
251  mpz_t tmp1;
252  mpz_t tmp2;
253  mpz_init( tmp1 );
254  mpz_divexact( tmp1, _den, g );
255  mpz_init( tmp2 );
256  mpz_divexact( tmp2, MPQDEN( c ), g );
257  mpz_mul( d, tmp2, _den );
258  mpz_mul( tmp2, tmp2, _num );
259  mpz_mul( tmp1, tmp1, MPQNUM( c ) );
260  mpz_add( n, tmp1, tmp2 );
261  mpz_gcd( g, n, d );
262  if ( mpz_cmp_si( g, 1 ) != 0 )
263  {
264  mpz_divexact( n, n, g );
265  mpz_divexact( d, d, g );
266  }
267  mpz_clear( tmp1 );
268  mpz_clear( tmp2 );
269  }
270  mpz_clear( g );
271  if ( deleteObject() ) delete this;
272  if ( mpz_cmp_si( d, 1 ) == 0 )
273  {
274  mpz_clear( d );
275  if ( mpz_is_imm( n ) )
276  {
277  InternalCF * res = int2imm( mpz_get_si( n ) );
278  mpz_clear( n );
279  return res;
280  }
281  else
282  {
283  return new InternalInteger( n );
284  }
285  }
286  else
287  {
288  return new InternalRational( n, d );
289  }
290 }

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

851 {
852  a = 1/CanonicalForm( copyObject() ); b = 0;
853  return int2imm( 1 );
854 }

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

841 {
842  a = 1/CanonicalForm( copyObject() ); b = 0;
843  return int2imm( 1 );
844 }

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

832 {
833  return int2imm( 1 );
834 }

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

823 {
824  return int2imm( 1 );
825 }

◆ classname()

const char* InternalRational::classname ( ) const
inlinevirtual

Reimplemented from InternalCF.

Definition at line 62 of file int_rat.h.

62 { 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.

537 {
538  if ( ::is_imm( c ) )
539  {
540  ASSERT( ::is_imm( c ) == INTMARK, "incompatible base coefficients" );
541  mpz_t dummy;
542  mpz_init_set_si( dummy, imm2int( c ) );
543  mpz_mul( dummy, dummy, _den );
544  int result = mpz_cmp( _num, dummy );
545  mpz_clear( dummy );
546  return result;
547  }
548  else
549  {
550  ASSERT( c->levelcoeff() == IntegerDomain, "incompatible base coefficients" );
551  mpz_t dummy;
552  mpz_init( dummy );
553  mpz_mul( dummy, _den, InternalInteger::MPI( c ) );
554  int result = mpz_cmp( _num, dummy );
555  mpz_clear( dummy );
556  return result;
557  }
558 }

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

517 {
518  ASSERT( ! ::is_imm( c ) && c->levelcoeff() == RationalDomain, "incompatible base coefficients" );
519  mpz_t dummy1, dummy2;
520  mpz_init( dummy1 ); mpz_init( dummy2 );
521  mpz_mul( dummy1, _num, MPQDEN( c ) );
522  mpz_mul( dummy2, _den, MPQNUM( c ) );
523  int result = mpz_cmp( dummy1, dummy2 );
524  mpz_clear( dummy1 ); mpz_clear( dummy2 );
525  return result;
526 }

◆ deepCopyObject()

InternalCF * InternalRational::deepCopyObject ( ) const
virtual

Reimplemented from InternalCF.

Definition at line 131 of file int_rat.cc.

132 {
133  mpz_t dummy_num;
134  mpz_t dummy_den;
135  mpz_init_set( dummy_num, _num );
136  mpz_init_set( dummy_den, _den );
137  return new InternalRational( dummy_num, dummy_den );
138 }

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

197 {
198  if ( mpz_is_imm( _den ) )
199  {
200  InternalCF * res = int2imm( mpz_get_si( _den ) );
201  return res;
202  }
203  else
204  {
205  mpz_t dummy;
206  mpz_init_set( dummy, _den );
207  return new InternalInteger( dummy );
208  }
209 }

◆ divcoeff()

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

Reimplemented from InternalCF.

Definition at line 787 of file int_rat.cc.

788 {
789  return dividecoeff( c, invert );
790 }

◆ dividecoeff()

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

Reimplemented from InternalCF.

Definition at line 707 of file int_rat.cc.

708 {
709  ASSERT( ::is_imm( c ) == INTMARK || ! ::is_imm( c ), "expected integer" );
710  mpz_t n, d, g;
711  if ( ::is_imm( c ) )
712  {
713  long cc = imm2int( c );
714  ASSERT( c != 0 || invert, "divide by zero" );
715  if ( cc == 0 )
716  {
717  // => invert
718  if ( deleteObject() ) delete this;
719  return CFFactory::basic( 0L );
720  }
721  if ( invert )
722  {
723  mpz_init_set_si( n, cc );
724  mpz_mul( n, n, _den );
725  mpz_init_set( d, _num );
726  }
727  else
728  {
729  mpz_init_set_si( d, cc );
730  mpz_mul( d, d, _den );
731  mpz_init_set( n, _num );
732  }
733  }
734  else
735  {
736  ASSERT( c->levelcoeff() == IntegerDomain, "expected integer" );
737  if ( invert )
738  {
739  mpz_init_set( n, InternalInteger::MPI( c ) );
740  mpz_mul( n, n, _den );
741  mpz_init_set( d, _num );
742  }
743  else
744  {
745  mpz_init_set( d, InternalInteger::MPI( c ) );
746  mpz_mul( d, d, _den );
747  mpz_init_set( n, _num );
748  }
749  }
750  if ( mpz_cmp_si( d, 0 ) < 0 )
751  {
752  mpz_neg( d, d );
753  mpz_neg( n, n );
754  }
755  mpz_init( g );
756  mpz_gcd( g, n, d );
757  if ( mpz_cmp_si( g, 1 ) != 0 )
758  {
759  mpz_divexact( d, d, g );
760  mpz_divexact( n, n, g );
761  }
762  mpz_clear( g );
763  if ( deleteObject() ) delete this;
764  if ( ! invert )
765  {
766  // then there was no way for the result to become an integer
767  return new InternalRational( n, d );
768  }
769  if ( mpz_cmp_si( d, 1 ) == 0 )
770  {
771  mpz_clear( d );
772  if ( mpz_is_imm( n ) )
773  {
774  InternalCF * res = int2imm( mpz_get_si( n ) );
775  mpz_clear( n );
776  return res;
777  }
778  else
779  {
780  return new InternalInteger( n );
781  }
782  }
783  else
784  return new InternalRational( n, d );
785 }

◆ dividesame()

InternalCF * InternalRational::dividesame ( InternalCF c)
virtual

Reimplemented from InternalCF.

Definition at line 408 of file int_rat.cc.

409 {
410  ASSERT( ! ::is_imm( c ) && c->levelcoeff() == RationalDomain, "illegal domain" );
411 
412  if ( this == c )
413  {
414  if ( deleteObject() ) delete this;
415  return CFFactory::basic( 1L );
416  }
417  else
418  {
419  mpz_t n, d;
420  mpz_t g1, g2, tmp1, tmp2;
421  mpz_init( n ); mpz_init( d );
422  mpz_init( g1 ); mpz_init( g2 );
423  mpz_gcd( g1, _num, MPQNUM( c ) );
424  mpz_gcd( g2, _den, MPQDEN( c ) );
425  bool g1is1 = mpz_cmp_si( g1, 1 ) == 0;
426  bool g2is1 = mpz_cmp_si( g2, 1 ) == 0;
427  mpz_init( tmp1 ); mpz_init( tmp2 );
428  if ( ! g1is1 )
429  mpz_divexact( tmp1, _num, g1 );
430  else
431  mpz_set( tmp1, _num );
432  if ( ! g2is1 )
433  mpz_divexact( tmp2, MPQDEN( c ), g2 );
434  else
435  mpz_set( tmp2, MPQDEN( c ) );
436  mpz_mul( n, tmp1, tmp2 );
437  if ( ! g1is1 )
438  mpz_divexact( tmp1, MPQNUM( c ), g1 );
439  else
440  mpz_set( tmp1, MPQNUM( c ) );
441  if ( ! g2is1 )
442  mpz_divexact( tmp2, _den, g2 );
443  else
444  mpz_set( tmp2, _den );
445  mpz_mul( d, tmp1, tmp2 );
446  mpz_clear( tmp1 ); mpz_clear( tmp2 );
447  mpz_clear( g1 ); mpz_clear( g2 );
448  if ( deleteObject() ) delete this;
449  if ( mpz_cmp_si( d, 0 ) < 0 )
450  {
451  mpz_neg( d, d );
452  mpz_neg( n, n );
453  }
454  if ( mpz_cmp_si( d, 1 ) == 0 )
455  {
456  mpz_clear( d );
457  if ( mpz_is_imm( n ) )
458  {
459  InternalCF * res = int2imm( mpz_get_si( n ) );
460  mpz_clear( n );
461  return res;
462  }
463  else
464  {
465  return new InternalInteger( n );
466  }
467  }
468  else
469  return new InternalRational( n, d );
470  }
471 }

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

806 {
807  quot = copyObject();
808  quot = quot->dividecoeff( c, invert );
809  rem = CFFactory::basic( 0L );
810 }

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

813 {
814  divremcoeff( c, quot, rem, invert );
815  return true;
816 }

◆ divremsame()

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

Reimplemented from InternalCF.

Definition at line 489 of file int_rat.cc.

490 {
491  quot = copyObject();
492  quot = quot->dividesame( c );
493  rem = CFFactory::basic( 0L );
494 }

◆ divremsamet()

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

Reimplemented from InternalCF.

Definition at line 496 of file int_rat.cc.

497 {
498  divremsame( c, quot, rem );
499  return true;
500 }

◆ divsame()

InternalCF * InternalRational::divsame ( InternalCF c)
virtual

Reimplemented from InternalCF.

Definition at line 473 of file int_rat.cc.

474 {
475  return dividesame( c );
476 }

◆ genOne()

InternalCF * InternalRational::genOne ( )
virtual

Reimplemented from InternalCF.

Definition at line 167 of file int_rat.cc.

168 {
169  if ( isOne() )
170  return copyObject();
171  else
172  return new InternalRational( 1 );
173 }

◆ genZero()

InternalCF * InternalRational::genZero ( )
virtual

Reimplemented from InternalCF.

Definition at line 159 of file int_rat.cc.

160 {
161  if ( isZero() )
162  return copyObject();
163  else
164  return new InternalRational();
165 }

◆ intval()

long InternalRational::intval ( ) const
virtual

Reimplemented from InternalCF.

Definition at line 897 of file int_rat.cc.

898 {
899 
900  ASSERT( mpz_cmp_si( _den, 1 ) == 0, "illegal operation" );
901  return mpz_get_si( _num );
902 
903 }

◆ is_imm()

bool InternalRational::is_imm ( ) const

Definition at line 154 of file int_rat.cc.

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

◆ levelcoeff()

int InternalRational::levelcoeff ( ) const
inlinevirtual

Reimplemented from InternalCF.

Definition at line 70 of file int_rat.h.

70 { return RationalDomain; }

◆ modcoeff()

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

Reimplemented from InternalCF.

Definition at line 797 of file int_rat.cc.

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

◆ modsame()

InternalCF * InternalRational::modsame ( InternalCF )
virtual

Reimplemented from InternalCF.

Definition at line 483 of file int_rat.cc.

484 {
485  if ( deleteObject() ) delete this;
486  return CFFactory::basic( 0L );
487 }

◆ modulocoeff()

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

Reimplemented from InternalCF.

Definition at line 792 of file int_rat.cc.

793 {
794  return modcoeff( c, invert );
795 }

◆ modulosame()

InternalCF * InternalRational::modulosame ( InternalCF c)
virtual

Reimplemented from InternalCF.

Definition at line 478 of file int_rat.cc.

479 {
480  return modsame( c );
481 }

◆ MPQDEN()

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

Definition at line 124 of file int_rat.h.

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

◆ MPQNUM()

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

Definition at line 119 of file int_rat.h.

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

◆ mulcoeff()

InternalCF * InternalRational::mulcoeff ( InternalCF c)
virtual

Reimplemented from InternalCF.

Definition at line 654 of file int_rat.cc.

655 {
656  ASSERT( ::is_imm( c ) == INTMARK || ! ::is_imm( c ), "expected integer" );
657  mpz_t n, d, g;
658  if ( ::is_imm( c ) )
659  {
660  long cc = imm2int( c );
661  if ( cc == 0 )
662  {
663  if ( deleteObject() ) delete this;
664  return CFFactory::basic( 0L );
665  }
666  mpz_init_set_si( n, cc );
667  }
668  else
669  {
670  ASSERT( c->levelcoeff() == IntegerDomain, "expected integer" );
671  mpz_init_set( n, InternalInteger::MPI( c ) );
672  }
673  mpz_init( g );
674  mpz_gcd( g, n, _den );
675  if ( mpz_cmp_si( g, 1 ) == 0 )
676  {
677  mpz_mul( n, n, _num );
678  mpz_init_set( d, _den );
679  }
680  else
681  {
682  mpz_divexact( n, n, g );
683  mpz_mul( n, n, _num );
684  mpz_init( d );
685  mpz_divexact( d, _den, g );
686  }
687  mpz_clear( g );
688  if ( deleteObject() ) delete this;
689  if ( mpz_cmp_si( d, 1 ) == 0 )
690  {
691  mpz_clear( d );
692  if ( mpz_is_imm( n ) )
693  {
694  InternalCF * res = int2imm( mpz_get_si( n ) );
695  mpz_clear( n );
696  return res;
697  }
698  else
699  {
700  return new InternalInteger( n );
701  }
702  }
703  else
704  return new InternalRational( n, d );
705 }

◆ mulsame()

InternalCF * InternalRational::mulsame ( InternalCF c)
virtual

Reimplemented from InternalCF.

Definition at line 348 of file int_rat.cc.

349 {
350  ASSERT( ! ::is_imm( c ) && c->levelcoeff() == RationalDomain, "illegal domain" );
351  mpz_t n, d;
352  mpz_init( n ); mpz_init( d );
353 
354  if ( this == c )
355  {
356  mpz_mul( n, _num, _num );
357  mpz_mul( d, _den, _den );
358  }
359  else
360  {
361  mpz_t g1, g2, tmp1, tmp2;
362  mpz_init( g1 ); mpz_init( g2 );
363  mpz_gcd( g1, _num, MPQDEN( c ) );
364  mpz_gcd( g2, _den, MPQNUM( c ) );
365  bool g1is1 = mpz_cmp_si( g1, 1 ) == 0;
366  bool g2is1 = mpz_cmp_si( g2, 1 ) == 0;
367  mpz_init( tmp1 ); mpz_init( tmp2 );
368  if ( ! g1is1 )
369  mpz_divexact( tmp1, _num, g1 );
370  else
371  mpz_set( tmp1, _num );
372  if ( ! g2is1 )
373  mpz_divexact( tmp2, MPQNUM( c ), g2 );
374  else
375  mpz_set( tmp2, MPQNUM( c ) );
376  mpz_mul( n, tmp1, tmp2 );
377  if ( ! g1is1 )
378  mpz_divexact( tmp1, MPQDEN( c ), g1 );
379  else
380  mpz_set( tmp1, MPQDEN( c ) );
381  if ( ! g2is1 )
382  mpz_divexact( tmp2, _den, g2 );
383  else
384  mpz_set( tmp2, _den );
385  mpz_mul( d, tmp1, tmp2 );
386  mpz_clear( tmp1 ); mpz_clear( tmp2 );
387  mpz_clear( g1 ); mpz_clear( g2 );
388  }
389  if ( deleteObject() ) delete this;
390  if ( mpz_cmp_si( d, 1 ) == 0 )
391  {
392  mpz_clear( d );
393  if ( mpz_is_imm( n ) )
394  {
395  InternalCF * res = int2imm( mpz_get_si( n ) );
396  mpz_clear( n );
397  return res;
398  }
399  else
400  {
401  return new InternalInteger( n );
402  }
403  }
404  else
405  return new InternalRational( n, d );
406 }

◆ neg()

InternalCF * InternalRational::neg ( )
virtual

InternalCF * InternalRational::neg ()

See also
CanonicalForm::operator -()

Reimplemented from InternalCF.

Definition at line 215 of file int_rat.cc.

216 {
217  if ( getRefCount() > 1 )
218  {
219  decRefCount();
220  mpz_t dummy_num;
221  mpz_t dummy_den;
222  mpz_init_set( dummy_num, _num );
223  mpz_init_set( dummy_den, _den );
224  mpz_neg( dummy_num, dummy_num );
225  return new InternalRational( dummy_num, dummy_den );
226  }
227  else
228  {
229  mpz_neg( _num, _num );
230  return this;
231  }
232 }

◆ normalize()

static void InternalRational::normalize ( const  mpz_ptr,
const  mpz_ptr,
mpz_ptr  ,
mpz_ptr   
)
staticprivate

◆ normalize_myself()

InternalCF * InternalRational::normalize_myself ( )

reduce InternalRational to lowest terms

Definition at line 859 of file int_rat.cc.

860 {
861  ASSERT( getRefCount() == 1, "illegal operation" );
862  mpz_t g;
863  mpz_init( g );
864  mpz_gcd( g, _num, _den );
865  if ( mpz_cmp_si( g, 1 ) != 0 )
866  {
867  mpz_divexact( _num, _num, g );
868  mpz_divexact( _den, _den, g );
869  }
870  mpz_clear( g );
871  if ( mpz_cmp_si( _den, 0 ) < 0 )
872  {
873  mpz_neg( _num, _num );
874  mpz_neg( _den, _den );
875  }
876  if ( mpz_cmp_si( _den, 1 ) == 0 )
877  {
878  if ( mpz_is_imm( _num ) )
879  {
880  InternalCF * res = int2imm( mpz_get_si( _num ) );
881  delete this;
882  return res;
883  }
884  else
885  {
886  mpz_t res;
887  mpz_init_set( res, _num );
888  delete this;
889  return new InternalInteger( res );
890  }
891  }
892  else
893  return this;
894 }

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

179 {
180  if ( mpz_is_imm( _num ) )
181  {
182  InternalCF * res = int2imm( mpz_get_si( _num ) );
183  return res;
184  }
185  else
186  {
187  mpz_t dummy;
188  mpz_init_set( dummy, _num );
189  return new InternalInteger( dummy );
190  }
191 }

◆ print()

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

Reimplemented from InternalCF.

Definition at line 141 of file int_rat.cc.

142 {
143  char * str = new char[mpz_sizeinbase( _num, 10 ) + 2];
144  str = mpz_get_str( str, 10, _num );
145  os << str << '/';
146  delete [] str;
147  str = new char[mpz_sizeinbase( _den, 10 ) + 2];
148  str = mpz_get_str( str, 10, _den );
149  os << str << c;
150  delete [] str;
151 }

◆ sign()

int InternalRational::sign ( ) const
virtual
See also
CanonicalForm::sign()

Reimplemented from InternalCF.

Definition at line 909 of file int_rat.cc.

910 {
911  return mpz_sgn( _num );
912 }

◆ subcoeff()

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

Reimplemented from InternalCF.

Definition at line 597 of file int_rat.cc.

598 {
599  ASSERT( ::is_imm( c ) == INTMARK || ! ::is_imm( c ), "expected integer" );
600  mpz_t n, d;
601  if ( ::is_imm( c ) )
602  {
603  long cc = imm2int( c );
604  if ( cc == 0 )
605  {
606  if ( negate )
607  {
608  if ( getRefCount() == 1 )
609  {
610  mpz_neg( _num, _num );
611  return this;
612  }
613  else
614  {
615  decRefCount();
616  mpz_init_set( d, _den );
617  mpz_init_set( n, _num );
618  mpz_neg( n, n );
619  return new InternalRational( n, d );
620  }
621  }
622  else
623  return this;
624  }
625  mpz_init( n );
626  if ( cc < 0 )
627  {
628  mpz_mul_ui( n, _den, -cc );
629  mpz_neg( n, n );
630  }
631  else
632  mpz_mul_ui( n, _den, cc );
633  if ( negate )
634  mpz_sub( n, n, _num );
635  else
636  mpz_sub( n, _num, n );
637  }
638  else
639  {
640  ASSERT( c->levelcoeff() == IntegerDomain, "expected integer" );
641  mpz_init( n );
642  mpz_mul( n, _den, InternalInteger::MPI( c ) );
643  if ( negate )
644  mpz_sub( n, n, _num );
645  else
646  mpz_sub( n, _num, n );
647  }
648  mpz_init_set( d, _den );
649  // at this point there is no way that the result is not a true rational
650  if ( deleteObject() ) delete this;
651  return new InternalRational( n, d );
652 }

◆ subsame()

InternalCF * InternalRational::subsame ( InternalCF c)
virtual

Reimplemented from InternalCF.

Definition at line 292 of file int_rat.cc.

293 {
294  ASSERT( ! ::is_imm( c ) && c->levelcoeff() == RationalDomain, "illegal domain" );
295  mpz_t n, d, g;
296 
297  mpz_init( g ); mpz_init( n ); mpz_init( d );
298  mpz_gcd( g, _den, MPQDEN( c ) );
299 
300  if ( mpz_cmp_si( g, 1 ) == 0 )
301  {
302  mpz_mul( n, _den, MPQNUM( c ) );
303  mpz_mul( g, _num, MPQDEN( c ) );
304  mpz_sub( n, g, n );
305  mpz_mul( d, _den, MPQDEN( c ) );
306  }
307  else
308  {
309  mpz_t tmp1;
310  mpz_t tmp2;
311  mpz_init( tmp1 );
312  mpz_divexact( tmp1, _den, g );
313  mpz_init( tmp2 );
314  mpz_divexact( tmp2, MPQDEN( c ), g );
315  mpz_mul( d, tmp2, _den );
316  mpz_mul( tmp2, tmp2, _num );
317  mpz_mul( tmp1, tmp1, MPQNUM( c ) );
318  mpz_sub( n, tmp2, tmp1 );
319  mpz_gcd( g, n, d );
320  if ( mpz_cmp_si( g, 1 ) != 0 )
321  {
322  mpz_divexact( n, n, g );
323  mpz_divexact( d, d, g );
324  }
325  mpz_clear( tmp1 );
326  mpz_clear( tmp2 );
327  }
328  mpz_clear( g );
329  if ( deleteObject() ) delete this;
330  if ( mpz_cmp_si( d, 1 ) == 0 )
331  {
332  mpz_clear( d );
333  if ( mpz_is_imm( n ) )
334  {
335  InternalCF * res = int2imm( mpz_get_si( n ) );
336  mpz_clear( n );
337  return res;
338  }
339  else
340  {
341  return new InternalInteger( n );
342  }
343  }
344  else
345  return new InternalRational( n, d );
346 }

Friends And Related Function Documentation

◆ gmp_denominator

void gmp_denominator ( const CanonicalForm f,
mpz_ptr  result 
)
friend

Definition at line 40 of file singext.cc.

41 {
42  InternalCF * ff = f.getval();
43  ASSERT( ! is_imm( ff ), "illegal type" );
44  if ( ff->levelcoeff() == IntegerDomain )
45  {
46  mpz_init_set_si( result, 1 );
47  ff->deleteObject();
48  }
49  else if ( ff->levelcoeff() == RationalDomain )
50  {
51  mpz_init_set( result, (InternalRational::MPQDEN( ff )) );
52  ff->deleteObject();
53  }
54  else
55  {
56  ASSERT( 0, "illegal type" );
57  }
58 }

◆ gmp_numerator

void gmp_numerator ( const CanonicalForm f,
mpz_ptr  result 
)
friend

Definition at line 20 of file singext.cc.

21 {
22  InternalCF * ff = f.getval();
23  ASSERT( ! is_imm( ff ), "illegal type" );
24  if ( ff->levelcoeff() == IntegerDomain )
25  {
26  mpz_init_set( result, (InternalInteger::MPI( ff )) );
27  ff->deleteObject();
28  }
29  else if ( ff->levelcoeff() == RationalDomain )
30  {
31  mpz_init_set( result, (InternalRational::MPQNUM( ff )) );
32  ff->deleteObject();
33  }
34  else
35  {
36  ASSERT( 0, "illegal type" );
37  }
38 }

◆ InternalInteger

friend class InternalInteger
friend

Definition at line 113 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 42 of file int_rat.h.

◆ _num

mpz_t InternalRational::_num
private

Definition at line 41 of file int_rat.h.

◆ initialized

int InternalRational::initialized
staticprivate

Definition at line 43 of file int_rat.h.


The documentation for this class was generated from the following files:
InternalRational::is_imm
bool is_imm() const
Definition: int_rat.cc:154
InternalCF::invert
virtual InternalCF * invert()
Definition: int_cf.cc:172
f
FILE * f
Definition: checklibs.c:9
InternalRational::InternalRational
InternalRational()
Definition: int_rat.cc:34
result
return result
Definition: facAbsBiFact.cc:76
RationalDomain
#define RationalDomain
Definition: cf_defs.h:24
InternalRational::dividesame
InternalCF * dividesame(InternalCF *)
Definition: int_rat.cc:408
InternalCF::isZero
virtual bool isZero() const
Definition: int_cf.cc:24
g
g
Definition: cfModGcd.cc:4031
InternalCF
virtual class for internal CanonicalForm's
Definition: int_cf.h:47
b
CanonicalForm b
Definition: cfModGcd.cc:4044
InternalCF::isOne
virtual bool isOne() const
bool InternalCF::isOne, isZero () const
Definition: int_cf.cc:18
InternalRational::MPQNUM
static mpz_ptr MPQNUM(const InternalCF *const c)
Definition: int_rat.h:119
CanonicalForm
factory's main class
Definition: canonicalform.h:83
InternalCF::copyObject
InternalCF * copyObject()
Definition: int_cf.h:62
i
int i
Definition: cfEzgcd.cc:125
InternalCF::levelcoeff
virtual int levelcoeff() const
Definition: int_cf.h:68
res
CanonicalForm res
Definition: facAbsFact.cc:64
ASSERT
#define ASSERT(expression, message)
Definition: cf_assert.h:99
INTMARK
const long INTMARK
Definition: imm.h:37
InternalRational::_num
mpz_t _num
Definition: int_rat.h:41
int2imm
static InternalCF * int2imm(long i)
Definition: imm.h:75
CFFactory::basic
static InternalCF * basic(long value)
Definition: cf_factory.cc:30
InternalRational::modsame
InternalCF * modsame(InternalCF *)
Definition: int_rat.cc:483
intgcd
static long intgcd(long a, long b)
Definition: int_rat.cc:17
tmp1
CFList tmp1
Definition: facFqBivar.cc:70
InternalCF::dividecoeff
virtual InternalCF * dividecoeff(InternalCF *, bool) PVIRT_INTCF("dividecoeff")
InternalRational::modcoeff
InternalCF * modcoeff(InternalCF *, bool)
Definition: int_rat.cc:797
InternalRational
factory's class for rationals
Definition: int_rat.h:39
InternalCF::deleteObject
int deleteObject()
Definition: int_cf.h:61
InternalRational::_den
mpz_t _den
Definition: int_rat.h:42
InternalCF::dividesame
virtual InternalCF * dividesame(InternalCF *) PVIRT_INTCF("dividesame")
InternalCF::decRefCount
int decRefCount()
Definition: int_cf.h:53
InternalInteger::MPI
static mpz_ptr MPI(const InternalCF *const c)
MPI() - return underlying mpz_t of ā€˜c’.
Definition: int_int.h:232
imm2int
static long imm2int(const InternalCF *const imm)
Definition: imm.h:70
InternalRational::InternalInteger
friend class InternalInteger
Definition: int_rat.h:113
InternalRational::divremcoeff
void divremcoeff(InternalCF *, InternalCF *&, InternalCF *&, bool)
Definition: int_rat.cc:805
tmp2
CFList tmp2
Definition: facFqBivar.cc:70
InternalRational::divremsame
void divremsame(InternalCF *, InternalCF *&, InternalCF *&)
Definition: int_rat.cc:489
IntegerDomain
#define IntegerDomain
Definition: cf_defs.h:25
mpz_is_imm
bool mpz_is_imm(const mpz_t mpi)
Definition: gmpext.h:19
InternalRational::dividecoeff
InternalCF * dividecoeff(InternalCF *, bool)
Definition: int_rat.cc:707
InternalRational::MPQDEN
static mpz_ptr MPQDEN(const InternalCF *const c)
Definition: int_rat.h:124
rem
void rem(unsigned long *a, unsigned long *q, unsigned long p, int &dega, int degq)
Definition: minpoly.cc:572
InternalCF::getRefCount
int getRefCount()
Definition: int_cf.h:51