Basic Polynomial Algebra Subprograms (BPAS)  v. 1.791
ComplexRationalNumber.hpp
1 
2 #ifndef _COMPLEX_RATIONAL_NUMBER_H_
3 #define _COMPLEX_RATIONAL_NUMBER_H_
4 
5 #include "BPASField.hpp"
6 #include <iostream>
7 
8 //forward declarations
9 class Integer;
10 class RationalNumber;
11 class SmallPrimeField;
12 class BigPrimeField;
16 template <class Ring>
18 
19 
20 /**
21  * An arbitrary-precision complex rational number.
22  */
23 class ComplexRationalNumber : public BPASField<ComplexRationalNumber> {
24 
25 private:
26  // a + i * b
27  mpq_class a;
28  mpq_class b;
29 
30 public:
31 
32 
33 
34  // static bool isPrimeField;
35  // static bool isSmallPrimeField;
36  // static bool isComplexField;
37 
39 
40  ComplexRationalNumber (const mpq_class& _a, const mpq_class& _b = mpq_class(1));
41 
43 
44  ComplexRationalNumber(int _a, int _b = 1, int _c = 0, int _d = 1);
45 
46  explicit ComplexRationalNumber (const Integer& c);
47 
48  explicit ComplexRationalNumber (const RationalNumber& c);
49 
50  explicit ComplexRationalNumber (const SmallPrimeField& c);
51 
52  explicit ComplexRationalNumber (const BigPrimeField& c);
53 
55 
57 
59 
61 
63 
65 
66  template <class Ring>
68 
70 
71  ComplexRationalNumber& operator= (const mpq_class& k);
72 
74 
75  ComplexRationalNumber& setRealPart (const RationalNumber& r);
76 
77  ComplexRationalNumber& setRealPart (const mpq_class& k);
78 
79  ComplexRationalNumber& setRealPart (int k);
80 
81  ComplexRationalNumber& setImaginaryPart (const RationalNumber& r);
82 
83  ComplexRationalNumber& setImaginaryPart (const mpq_class& k);
84 
85  ComplexRationalNumber& setImaginaryPart (int k);
86 
87  ComplexRationalNumber& set (const RationalNumber& ka, const RationalNumber& kb);
88 
89  ComplexRationalNumber& set (const mpq_class& ka, const mpq_class& kb);
90 
91  ComplexRationalNumber& set (const mpq_class& ka, int kb);
92 
93  ComplexRationalNumber& set (int ka, const mpq_class& kb);
94 
95  ComplexRationalNumber& set (int ka, int kb);
96 
97  /**
98  * Is a zero
99  *
100  * @param
101  **/
102  inline bool isZero() const {
103  return (a == 0 && b == 0);
104  }
105 
106  /**
107  * Assign to zero
108  *
109  * @param
110  **/
111  inline void zero() {
112  a = 0;
113  b = 0;
114  }
115 
116  /**
117  * Is a 1
118  *
119  * @param
120  **/
121  inline bool isOne() const {
122  return (a == 1 && b == 0);
123  }
124 
125  /**
126  * Assign to one
127  *
128  * @param
129  **/
130  inline void one() {
131  a = 1;
132  b = 0;
133  }
134 
135  /**
136  * Is a -1
137  *
138  * @param
139  **/
140  inline bool isNegativeOne() const {
141  return (a == -1 && b == 0);
142  }
143 
144  /**
145  * Assign to negative one
146  *
147  * @param
148  **/
149  inline void negativeOne() {
150  a = -1;
151  b = 0;
152  }
153 
154  /**
155  * Is a constant
156  *
157  * @param
158  **/
159  inline int isConstant() const {
160  if (a >= 0)
161  return 1;
162  else { return -1; }
163  }
164 
166 
167  inline bool operator== (const ComplexRationalNumber& c) const {
168  if (a == c.a && b == c.b)
169  return 1;
170  else { return 0; }
171  }
172 
173  inline bool operator== (const mpq_class& k) const {
174  if (a == k && b == 0)
175  return 1;
176  else { return 0; }
177  }
178 
179  inline bool operator== (int k) const {
180  if (a == k && b == 0)
181  return 1;
182  else { return 0; }
183  }
184 
185  inline bool operator!= (const ComplexRationalNumber& c) const {
186  if (a == c.a && b == c.b)
187  return 0;
188  else { return 1; }
189  }
190 
191  inline bool operator!= (const mpq_class& k) const {
192  if (a == k && b == 0)
193  return 1;
194  else { return 0; }
195  }
196 
197  inline bool operator!= (int k) const {
198  if (a == k && b == 0)
199  return 1;
200  else { return 0; }
201  }
202 
204  ComplexRationalNumber r (*this);
205  return (r += c);
206  }
207 
209  a += c.a;
210  b += c.b;
211  return *this;
212  }
213 
215  ComplexRationalNumber r (*this);
216  return (r -= c);
217  }
218 
220  a -= c.a;
221  b -= c.b;
222  return *this;
223  }
224 
226  ComplexRationalNumber r (-a, -b);
227  return r;
228  }
229 
231  ComplexRationalNumber r (*this);
232  return (r *= c);
233  }
234 
236  mpq_class t = a*c.a - b*c.b;
237  mpq_class e = a*c.b + c.a*b;
238  a = t;
239  b = e;
240  return *this;
241  }
242 
243  inline ComplexRationalNumber& operator*= (const mpq_class& c) {
244  a *= c;
245  b *= c;
246  return *this;
247  }
248 
249  inline ComplexRationalNumber& operator*= (int c) {
250  a *= c;
251  b *= c;
252  return *this;
253  }
254 
255  /**
256  * Overload operator ^
257  * replace xor operation by exponentiation
258  *
259  * @param e: The exponentiation
260  **/
261  inline ComplexRationalNumber operator^ (long long int e) const {
263  if (isZero() || isOne() || e == 1)
264  r = *this;
265  else if (e == 2) {
266  r = *this * *this;
267  }
268  else if (e > 2) {
269  ComplexRationalNumber x (*this);
270  r.one();
271 
272  while (e != 0) {
273  if (e % 2)
274  r *= x;
275  x = x * x;
276  e >>= 1;
277  }
278  }
279  else if (e == 0) {
280  r.one();
281  }
282  else {
283  r = *this ^ (-e);
284  r.inverse();
285  }
286  return r;
287  }
288 
289  inline ComplexRationalNumber& operator^= (long long int e) {
290  *this = *this ^ e;
291  return *this;
292  }
293 
295  std::cerr << "ComplexRationalNumber::convertToExpressionTree NOT YET IMPLEMENTED" << std::endl;
296  exit(1);
297  return ExpressionTree();
298  }
299 
301  ComplexRationalNumber r (*this);
302  return (r /= c);
303  }
304 
306  if (c.isZero()) {
307  std::cout << "BPAS: error, dividend is zero from ComplexRationalNumber."<< std::endl;
308  exit(1);
309  }
310  mpq_class r = c.a*c.a + c.b*c.b;
311  mpq_class t = (a*c.a+b*c.b)/r;
312  mpq_class e = (b*c.a-c.b*a)/r;
313  a = t;
314  b = e;
315  return *this;
316  }
317 
319  return 0;
320  }
321 
323  *this = 0;
324  return *this;
325  }
326 
327  /**
328  * GCD(a, b)
329  *
330  * @param b: The other rational number
331  **/
334  if (isZero() && c.isZero())
335  e.zero();
336  else
337  e.one();
338  return e;
339  }
340 
341  /**
342  * Compute squarefree factorization of *this
343  */
345  std::vector<ComplexRationalNumber> ret;
346  ret.push_back(*this);
347  return ret;
348  }
349 
350  /**
351  * Get the euclidean size of *this.
352  */
353  inline Integer euclideanSize() const {
354  return Integer(1);
355  }
356 
357  /**
358  * Perform the eucldiean division of *this and b. Returns the
359  * remainder. If q is not NULL, then returns the quotient in q.
360  */
362 
363  /**
364  * Perform the extended euclidean division on *this and b.
365  * Returns the GCD. If s and t are not NULL, returns the bezout coefficients in them.
366  */
368 
369  /**
370  * Get the quotient of *this and b.
371  */
373 
374  /**
375  * Get the remainder of *this and b.
376  */
378 
381  mpq_class e = a * a + b * b;
382  r.a = a/e;
383  r.b = -b/e;
384  return r;
385  }
386 
387  inline RationalNumber realPart() const {
388  return RationalNumber(a);
389  }
390 
391  inline RationalNumber imaginaryPart() const {
392  return RationalNumber(b);
393  }
394 
395  inline ComplexRationalNumber conjugate() const {
396  ComplexRationalNumber r(a, -b);
397  return r;
398  }
399 
400  void print(std::ostream& out) const;
401 
402 };
403 
404 
405 #endif
A sparsely represented univariate polynomial over an arbitrary ring.
Definition: BigPrimeField.hpp:21
bool operator!=(const ComplexRationalNumber &c) const
Inequality test,.
Definition: ComplexRationalNumber.hpp:185
An arbitrary-precision complex rational number.
Definition: ComplexRationalNumber.hpp:23
ComplexRationalNumber quotient(const ComplexRationalNumber &b) const
Get the quotient of *this and b.
ComplexRationalNumber & operator+=(const ComplexRationalNumber &c)
Addition assignment.
Definition: ComplexRationalNumber.hpp:208
ComplexRationalNumber operator/(const ComplexRationalNumber &c) const
Exact division.
Definition: ComplexRationalNumber.hpp:300
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
ComplexRationalNumber operator-() const
Negation.
Definition: ComplexRationalNumber.hpp:225
ComplexRationalNumber & operator=(const ComplexRationalNumber &c)
Copy assignment.
ComplexRationalNumber & operator%=(const ComplexRationalNumber &c)
Assign *this to be the remainder of *this and b.
Definition: ComplexRationalNumber.hpp:322
ComplexRationalNumber & operator-=(const ComplexRationalNumber &c)
Subtraction assignment.
Definition: ComplexRationalNumber.hpp:219
void zero()
Assign to zero.
Definition: ComplexRationalNumber.hpp:111
bool operator==(const ComplexRationalNumber &c) const
Equality test,.
Definition: ComplexRationalNumber.hpp:167
ComplexRationalNumber operator+(const ComplexRationalNumber &c) const
Addition.
Definition: ComplexRationalNumber.hpp:203
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
ComplexRationalNumber gcd(const ComplexRationalNumber &c) const
GCD(a, b)
Definition: ComplexRationalNumber.hpp:332
void negativeOne()
Assign to negative one.
Definition: ComplexRationalNumber.hpp:149
ComplexRationalNumber operator^(long long int e) const
Overload operator ^ replace xor operation by exponentiation.
Definition: ComplexRationalNumber.hpp:261
ComplexRationalNumber operator%(const ComplexRationalNumber &c) const
Get the remainder of *this and b;.
Definition: ComplexRationalNumber.hpp:318
ComplexRationalNumber extendedEuclidean(const ComplexRationalNumber &b, ComplexRationalNumber *s=NULL, ComplexRationalNumber *t=NULL) const
Perform the extended euclidean division on *this and b.
A univariate polynomial with RationalNumber coefficients represented densely.
Definition: urpolynomial.h:16
ComplexRationalNumber unitCanonical(ComplexRationalNumber *u=NULL, ComplexRationalNumber *v=NULL) const
Obtain the unit normal (a.k.a canonical associate) of an element.
A prime field whose prime can be arbitrarily large.
Definition: BigPrimeField.hpp:27
ComplexRationalNumber & operator/=(const ComplexRationalNumber &c)
Exact division assignment.
Definition: ComplexRationalNumber.hpp:305
A simple data structure for encapsulating a collection of Factor elements.
Definition: Factors.hpp:95
void print(std::ostream &out) const
Print the Ring element.
ComplexRationalNumber euclideanDivision(const ComplexRationalNumber &b, ComplexRationalNumber *q=NULL) const
Perform the eucldiean division of *this and b.
An arbitrary-precision Integer.
Definition: Integer.hpp:22
ExpressionTree convertToExpressionTree() const
Convert this to an expression tree.
Definition: ComplexRationalNumber.hpp:294
int isConstant() const
Is a constant.
Definition: ComplexRationalNumber.hpp:159
ComplexRationalNumber inverse() const
Get the inverse of *this.
Definition: ComplexRationalNumber.hpp:379
ComplexRationalNumber remainder(const ComplexRationalNumber &b) const
Get the remainder of *this and b.
bool isOne() const
Is a 1.
Definition: ComplexRationalNumber.hpp:121
An arbitrary-precision rational number.
Definition: RationalNumber.hpp:24
An abstract class defining the interface of a field.
Definition: BPASField.hpp:11
ComplexRationalNumber operator*(const ComplexRationalNumber &c) const
Multiplication.
Definition: ComplexRationalNumber.hpp:230
ComplexRationalNumber & operator*=(const ComplexRationalNumber &c)
Multiplication assignment.
Definition: ComplexRationalNumber.hpp:235
ComplexRationalNumber & operator^=(long long int e)
Exponentiation assignment.
Definition: ComplexRationalNumber.hpp:289
Factors< ComplexRationalNumber > squareFree() const
Compute squarefree factorization of *this.
Definition: ComplexRationalNumber.hpp:344
bool isZero() const
Is a zero.
Definition: ComplexRationalNumber.hpp:102
bool isNegativeOne() const
Is a -1.
Definition: ComplexRationalNumber.hpp:140
Integer euclideanSize() const
Get the euclidean size of *this.
Definition: ComplexRationalNumber.hpp:353
void one()
Assign to one.
Definition: ComplexRationalNumber.hpp:130