Basic Polynomial Algebra Subprograms (BPAS)  v. 1.652
RationalNumber.hpp
1
2 #ifndef _BPAS_RATIONAL_NUMBER_H_
3 #define _BPAS_RATIONAL_NUMBER_H_
4
5 #include "BPASField.hpp"
6 #include <gmpxx.h>
7 #include <iostream>
8
9 //forward declarations
10 class Integer;
12 class SmallPrimeField;
13 class BigPrimeField;
17 template <class Ring>
20
21 /**
22  * An arbitrary-precision rational number.
23  */
24 class RationalNumber : public BPASField<RationalNumber> {
25  private:
26  mpq_class _m;
27
28  public:
29  static mpz_class characteristic;
30  static RingProperties properties;
31
32  // static bool isPrimeField;
33  // static bool isSmallPrimeField;
34  // static bool isComplexField;
35
36  RationalNumber ();
37
38  RationalNumber (int a, int b = 1);
39
40  RationalNumber (const std::string& digits, int base = 10);
41
42  RationalNumber (const mpq_t& q);
43
44  RationalNumber (const mpq_class& a);
45
46  RationalNumber (const mpz_class& a, const mpz_class& b = mpz_class(1));
47
48  RationalNumber (const RationalNumber& a);
49
50  explicit RationalNumber (const Integer& a);
51
52  explicit RationalNumber (const ComplexRationalNumber& a);
53
54  explicit RationalNumber (const SmallPrimeField& a);
55
56  explicit RationalNumber (const BigPrimeField& a);
57
58  explicit RationalNumber (const GeneralizedFermatPrimeField& a);
59
61
63
65
67
69
71
72  template <class Ring>
74
75  RationalNumber* RNpointer(RationalNumber* a);
76
77  RationalNumber* RNpointer(SmallPrimeField* a);
78
79  RationalNumber* RNpointer(BigPrimeField* a);
80
82
83  RationalNumber& set (int a, int b);
84
85  inline mpq_class get_mpq() const {
86  return _m;
87  }
88
89  inline mpq_class& get_mpq_ref() {
90  return _m;
91  }
92
93  inline const mpq_class& get_mpq_ref() const {
94  return _m;
95  }
96
97  inline mpq_ptr get_mpq_t() {
98  return _m.get_mpq_t();
99  }
100
101  inline mpq_srcptr get_mpq_t() const {
102  return _m.get_mpq_t();
103  }
104
105  inline Integer get_num() const {
106  return Integer(_m.get_num());
107  }
108
109  double get_d() const {
110  return _m.get_d();
111  }
112
113  inline Integer get_den() const {
114  return Integer(_m.get_den());
115  }
116
117  /**
118  * Is a zero
119  *
120  * @param
121  **/
122  inline bool isZero() const {
123  return (_m == 0);
124  }
125
126  /**
127  * Assign to zero
128  *
129  * @param
130  **/
131  inline void zero() {
132  _m = 0;
133  }
134
135  /**
136  * Is a 1
137  *
138  * @param
139  **/
140  inline bool isOne() const {
141  return (_m == 1);
142  }
143
144  /**
145  * Assign to one
146  *
147  * @param
148  **/
149  inline void one() {
150  _m = 1;
151  }
152
153  /**
154  * Is a -1
155  *
156  * @param
157  **/
158  inline bool isNegativeOne() const {
159  return (_m == -1);
160  }
161
162  /**
163  * Assign to negative one
164  *
165  * @param
166  **/
167  inline void negativeOne() {
168  _m = -1;
169  }
170
171  /**
172  * Is a constant
173  *
174  * @param
175  **/
176  inline int isConstant() const {
177  if (_m >= 0)
178  return 1;
179  else { return -1; }
180  }
181
182  /**
183  * Obtain the unit normal (a.k.a canonical associate) of an element.
184  * If either parameters u, v, are non-NULL then the units are returned such that
185  * b = ua, v = u^-1. Where b is the unit normal of a, and is the returned value.
186  */
187  RationalNumber unitCanonical(RationalNumber* u = NULL, RationalNumber* v = NULL) const;
188
189  /**
190  * Copy assignment.
191  */
193
194  /**
196  */
197  inline RationalNumber operator+ (const RationalNumber& i) const {
198  RationalNumber ret = *this;
199  ret += i;
200  return ret;
201  }
202
203  /**
205  */
207  _m += i._m;
208  return *this;
209  }
210
211  /**
212  * Subtraction.
213  */
214  inline RationalNumber operator- (const RationalNumber& i) const {
215  RationalNumber ret = *this;
216  ret -= i;
217  return ret;
218  }
219
220  /**
221  * Subtraction assignment.
222  */
224  _m -= i._m;
225  return *this;
226  }
227
228  /**
229  * Negation.
230  */
231  inline RationalNumber operator- () const {
232  RationalNumber ret;
233  mpq_neg(ret._m.get_mpq_t(), (*this)._m.get_mpq_t());
234  return ret;
235  }
236
237  /**
238  * Multiplication.
239  */
240  inline RationalNumber operator* (const RationalNumber& i) const {
241  RationalNumber ret = *this;
242  ret *= i;
243  return ret;
244  }
245
246  /**
247  * Multiplication assignment.
248  */
250  _m *= i._m;
251  return *this;
252  }
253
254  /**
255  * Equality test,
256  *
257  * returns true iff equal
258  */
259  inline bool operator== (const RationalNumber& i) const {
260  return (_m == i._m);
261  }
262
263  /**
264  * Inequality test,
265  *
266  * returns true iff not equal.
267  */
268  inline bool operator!= (const RationalNumber& i) const {
269  return (_m != i._m);
270  }
271
273  return ExpressionTree(new ExprTreeNode(_m));
274  }
275
276  /**
277  * Exact division.
278  */
279  inline RationalNumber operator/ (const RationalNumber& i) const {
280  //TODO ensure this is exact and not rounded
281  RationalNumber ret = *this;
282  ret /= i;
283  return ret;
284  }
285
286  /**
287  * Exact division assignment.
288  */
290  _m /= i._m;
291  return *this;
292  }
293
294  inline bool operator< (const RationalNumber& r) const {
295  return _m < r._m;
296  }
297
298  inline bool operator<= (const RationalNumber& r) const {
299  return _m <= r._m;
300  }
301
302  inline bool operator> (const RationalNumber& r) const {
303  return _m > r._m;
304  }
305
306  inline bool operator>= (const RationalNumber& r) const {
307  return _m >= r._m;
308  }
309
310  /**
311  * GCD(a, b)
312  *
313  * @param b: The other rational number
314  **/
315  inline RationalNumber gcd (const RationalNumber& b) const {
316  RationalNumber c;
317  if (this->isZero() && b.isZero())
318  c = 0;
319  else
320  c = 1;
321  return c;
322  }
323
324  /**
325  * Compute squarefree factorization of *this
326  */
328  std::vector<RationalNumber> ret;
329  ret.push_back(*this);
330  return ret;
331  }
332
333  /**
334  * Get the euclidean size of *this.
335  */
336  inline RationalNumber euclideanSize() const {
337  std::cerr << "RationalNumber::euclideanSize NOT YET IMPLMENETED" << std::endl;
338  exit(1);
339  return *this;
340  }
341
342  /**
343  * Perform the eucldiean division of *this and b. Returns the
344  * remainder. If q is not NULL, then returns the quotient in q.
345  */
347
348  /**
349  * Perform the extended euclidean division on *this and b.
350  * Returns the GCD. If s and t are not NULL, returns the bezout coefficients in them.
351  */
352  RationalNumber extendedEuclidean(const RationalNumber& b, RationalNumber* s = NULL, RationalNumber* t = NULL) const;
353
354  /**
355  * Get the quotient of *this and b.
356  */
357  RationalNumber quotient(const RationalNumber& b) const;
358
359  /**
360  * Get the remainder of *this and b.
361  */
362  RationalNumber remainder(const RationalNumber& b) const;
363
364
365  /**
367  * replace xor operation by exponentiation
368  *
369  * @param e: The exponentiation
370  **/
371  inline RationalNumber operator^ (long long int e) const {
372  RationalNumber r;
373  mpz_pow_ui(r._m.get_num_mpz_t(), _m.get_num_mpz_t(), (unsigned long int) e);
374  mpz_pow_ui(r._m.get_den_mpz_t(), _m.get_den_mpz_t(), (unsigned long int) e);
375  return r;
376  }
377
378  inline RationalNumber& operator^= (long long int e) {
379  *this = *this ^ e;
380  return *this;
381  }
382
383  inline RationalNumber operator% (const RationalNumber& r) const {
384  return 0;
385  }
386
388  *this = 0;
389  return *this;
390  }
391
392  inline RationalNumber inverse() const {
393  RationalNumber ret;
394  mpz_set(ret._m.get_den_mpz_t(), _m.get_num_mpz_t());
395  mpz_set(ret._m.get_num_mpz_t(), _m.get_den_mpz_t());
396  ret._m.canonicalize();
397  return ret;
398  }
399
400
401
402  //Friend functions
403
404  inline friend RationalNumber operator+(int a, const RationalNumber& r) {
405  return RationalNumber(mpq_class(a) + r._m);
406  }
407
408  inline friend RationalNumber operator-(int a, const RationalNumber& r) {
409  return RationalNumber(mpq_class(a) - r._m);
410  }
411
412  inline friend RationalNumber operator*(int a, const RationalNumber& r) {
413  return RationalNumber(mpq_class(a) * r._m);
414  }
415
416  inline friend RationalNumber operator/(int a, const RationalNumber& r) {
417  return RationalNumber(mpq_class(a) / r._m);
418  }
419
420  inline friend RationalNumber operator+(long int a, const RationalNumber& r) {
421  return RationalNumber(mpq_class(a) + r._m);
422  }
423
424  inline friend RationalNumber operator-(long int a, const RationalNumber& r) {
425  return RationalNumber(mpq_class(a) - r._m);
426  }
427
428  inline friend RationalNumber operator*(long int a, const RationalNumber& r) {
429  return RationalNumber(mpq_class(a) * r._m);
430  }
431
432  inline friend RationalNumber operator/(long int a, const RationalNumber& r) {
433  return RationalNumber(mpq_class(a) / r._m);
434  }
435
436  inline friend RationalNumber abs(const RationalNumber& i) {
437  return RationalNumber(abs(i._m));
438  }
439
440
441
442
443
444 };
445
446 #endif
RationalNumber & operator+=(const RationalNumber &i)
Definition: RationalNumber.hpp:206
RationalNumber operator^(long long int e) const
Overload operator ^ replace xor operation by exponentiation.
Definition: RationalNumber.hpp:371
A univariate polynomial over an arbitrary BPASRing represented sparsely.
Definition: BigPrimeField.hpp:21
void negativeOne()
Assign to negative one.
Definition: RationalNumber.hpp:167
void zero()
Assign to zero.
Definition: RationalNumber.hpp:131
A multivariate polynomial with RationalNumber coefficients represented sparely.
Definition: mrpolynomial.h:70
An arbitrary-precision complex rational number.
Definition: ComplexRationalNumber.hpp:23
bool isZero() const
Is a zero.
Definition: RationalNumber.hpp:122
Factors< RationalNumber > squareFree() const
Compute squarefree factorization of *this.
Definition: RationalNumber.hpp:327
void one()
Assign to one.
Definition: RationalNumber.hpp:149
An ExpressionTree encompasses various forms of data that can be expressed generically as a binary tre...
Definition: ExpressionTree.hpp:17
A finite field whose prime should be a generalized fermat number.
Definition: GeneralizedFermatPrimeField.hpp:36
RationalNumber & operator-=(const RationalNumber &i)
Subtraction assignment.
Definition: RationalNumber.hpp:223
bool isOne() const
Is a 1.
Definition: RationalNumber.hpp:140
RationalNumber & operator^=(long long int e)
Exponentiation assignment.
Definition: RationalNumber.hpp:378
RationalNumber gcd(const RationalNumber &b) const
GCD(a, b)
Definition: RationalNumber.hpp:315
A prime field whose prime is 32 bits or less.
Definition: SmallPrimeField.hpp:449
A univariate polynomial with Integer coefficients using a dense representation.
Definition: uzpolynomial.h:13
RationalNumber remainder(const RationalNumber &b) const
Get the remainder of *this and b.
RationalNumber operator*(const RationalNumber &i) const
Multiplication.
Definition: RationalNumber.hpp:240
RationalNumber & operator/=(const RationalNumber &i)
Exact division assignment.
Definition: RationalNumber.hpp:289
RationalNumber & operator*=(const RationalNumber &i)
Multiplication assignment.
Definition: RationalNumber.hpp:249
A univariate polynomial with RationalNumber coefficients represented densely.
Definition: urpolynomial.h:15
A prime field whose prime can be arbitrarily large.
Definition: BigPrimeField.hpp:27
A simple data structure for encapsulating a collection of Factor elements.
Definition: Factors.hpp:95
int isConstant() const
Is a constant.
Definition: RationalNumber.hpp:176
RationalNumber extendedEuclidean(const RationalNumber &b, RationalNumber *s=NULL, RationalNumber *t=NULL) const
Perform the extended euclidean division on *this and b.
An arbitrary-precision Integer.
Definition: Integer.hpp:22
bool operator==(const RationalNumber &i) const
Equality test,.
Definition: RationalNumber.hpp:259
RationalNumber unitCanonical(RationalNumber *u=NULL, RationalNumber *v=NULL) const
Obtain the unit normal (a.k.a canonical associate) of an element.
RationalNumber & operator=(const RationalNumber &a)
Copy assignment.
RationalNumber euclideanDivision(const RationalNumber &b, RationalNumber *q=NULL) const
Perform the eucldiean division of *this and b.
RationalNumber operator-() const
Negation.
Definition: RationalNumber.hpp:231
RationalNumber quotient(const RationalNumber &b) const
Get the quotient of *this and b.
An arbitrary-precision rational number.
Definition: RationalNumber.hpp:24
virtual mpz_class characteristic()
The characteristic of this ring class.
Definition: BPASRing.hpp:87
An abstract class defining the interface of a field.
Definition: BPASField.hpp:11
RationalNumber inverse() const
Get the inverse of *this.
Definition: RationalNumber.hpp:392
RationalNumber operator/(const RationalNumber &i) const
Exact division.
Definition: RationalNumber.hpp:279
RationalNumber & operator%=(const RationalNumber &r)
Assign *this to be the remainder of *this and b.
Definition: RationalNumber.hpp:387
ExprTreeNode is a single node in the bianry tree of an ExpressionTree.
Definition: ExprTreeNode.hpp:76
bool operator!=(const RationalNumber &i) const
Inequality test,.
Definition: RationalNumber.hpp:268
RationalNumber euclideanSize() const
Get the euclidean size of *this.
Definition: RationalNumber.hpp:336
RationalNumber operator%(const RationalNumber &r) const
Get the remainder of *this and b;.
Definition: RationalNumber.hpp:383
ExpressionTree convertToExpressionTree() const
Convert this to an expression tree.
Definition: RationalNumber.hpp:272
RationalNumber operator+(const RationalNumber &i) const