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