Basic Polynomial Algebra Subprograms (BPAS)  v. 1.652
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  static mpz_class characteristic;
33  static RingProperties properties;
34 
35  // static bool isPrimeField;
36  // static bool isSmallPrimeField;
37  // static bool isComplexField;
38 
40 
41  ComplexRationalNumber (const mpq_class& _a, const mpq_class& _b = mpq_class(1));
42 
44 
45  ComplexRationalNumber(int _a, int _b = 1, int _c = 0, int _d = 1);
46 
47  explicit ComplexRationalNumber (const Integer& c);
48 
49  explicit ComplexRationalNumber (const RationalNumber& c);
50 
51  explicit ComplexRationalNumber (const SmallPrimeField& c);
52 
53  explicit ComplexRationalNumber (const BigPrimeField& c);
54 
56 
58 
60 
62 
64 
66 
67  template <class Ring>
69 
71 
72  ComplexRationalNumber& operator= (const mpq_class& k);
73 
75 
76  ComplexRationalNumber& setRealPart (const RationalNumber& r);
77 
78  ComplexRationalNumber& setRealPart (const mpq_class& k);
79 
80  ComplexRationalNumber& setRealPart (int k);
81 
82  ComplexRationalNumber& setImaginaryPart (const RationalNumber& r);
83 
84  ComplexRationalNumber& setImaginaryPart (const mpq_class& k);
85 
86  ComplexRationalNumber& setImaginaryPart (int k);
87 
88  ComplexRationalNumber& set (const RationalNumber& ka, const RationalNumber& kb);
89 
90  ComplexRationalNumber& set (const mpq_class& ka, const mpq_class& kb);
91 
92  ComplexRationalNumber& set (const mpq_class& ka, int kb);
93 
94  ComplexRationalNumber& set (int ka, const mpq_class& kb);
95 
96  ComplexRationalNumber& set (int ka, int kb);
97 
98  /**
99  * Is a zero
100  *
101  * @param
102  **/
103  inline bool isZero() const {
104  return (a == 0 && b == 0);
105  }
106 
107  /**
108  * Assign to zero
109  *
110  * @param
111  **/
112  inline void zero() {
113  a = 0;
114  b = 0;
115  }
116 
117  /**
118  * Is a 1
119  *
120  * @param
121  **/
122  inline bool isOne() const {
123  return (a == 1 && b == 0);
124  }
125 
126  /**
127  * Assign to one
128  *
129  * @param
130  **/
131  inline void one() {
132  a = 1;
133  b = 0;
134  }
135 
136  /**
137  * Is a -1
138  *
139  * @param
140  **/
141  inline bool isNegativeOne() const {
142  return (a == -1 && b == 0);
143  }
144 
145  /**
146  * Assign to negative one
147  *
148  * @param
149  **/
150  inline void negativeOne() {
151  a = -1;
152  b = 0;
153  }
154 
155  /**
156  * Is a constant
157  *
158  * @param
159  **/
160  inline int isConstant() const {
161  if (a >= 0)
162  return 1;
163  else { return -1; }
164  }
165 
167 
168  inline bool operator== (const ComplexRationalNumber& c) const {
169  if (a == c.a && b == c.b)
170  return 1;
171  else { return 0; }
172  }
173 
174  inline bool operator== (const mpq_class& k) const {
175  if (a == k && b == 0)
176  return 1;
177  else { return 0; }
178  }
179 
180  inline bool operator== (int k) const {
181  if (a == k && b == 0)
182  return 1;
183  else { return 0; }
184  }
185 
186  inline bool operator!= (const ComplexRationalNumber& c) const {
187  if (a == c.a && b == c.b)
188  return 0;
189  else { return 1; }
190  }
191 
192  inline bool operator!= (const mpq_class& k) const {
193  if (a == k && b == 0)
194  return 1;
195  else { return 0; }
196  }
197 
198  inline bool operator!= (int k) const {
199  if (a == k && b == 0)
200  return 1;
201  else { return 0; }
202  }
203 
205  ComplexRationalNumber r (*this);
206  return (r += c);
207  }
208 
210  a += c.a;
211  b += c.b;
212  return *this;
213  }
214 
216  ComplexRationalNumber r (*this);
217  return (r -= c);
218  }
219 
221  a -= c.a;
222  b -= c.b;
223  return *this;
224  }
225 
227  ComplexRationalNumber r (-a, -b);
228  return r;
229  }
230 
232  ComplexRationalNumber r (*this);
233  return (r *= c);
234  }
235 
237  mpq_class t = a*c.a - b*c.b;
238  mpq_class e = a*c.b + c.a*b;
239  a = t;
240  b = e;
241  return *this;
242  }
243 
244  inline ComplexRationalNumber& operator*= (const mpq_class& c) {
245  a *= c;
246  b *= c;
247  return *this;
248  }
249 
250  inline ComplexRationalNumber& operator*= (int c) {
251  a *= c;
252  b *= c;
253  return *this;
254  }
255 
256  /**
257  * Overload operator ^
258  * replace xor operation by exponentiation
259  *
260  * @param e: The exponentiation
261  **/
262  inline ComplexRationalNumber operator^ (long long int e) const {
264  if (isZero() || isOne() || e == 1)
265  r = *this;
266  else if (e == 2) {
267  r = *this * *this;
268  }
269  else if (e > 2) {
270  ComplexRationalNumber x (*this);
271  r.one();
272 
273  while (e != 0) {
274  if (e % 2)
275  r *= x;
276  x = x * x;
277  e >>= 1;
278  }
279  }
280  else if (e == 0) {
281  r.one();
282  }
283  else {
284  r = *this ^ (-e);
285  r.inverse();
286  }
287  return r;
288  }
289 
290  inline ComplexRationalNumber& operator^= (long long int e) {
291  *this = *this ^ e;
292  return *this;
293  }
294 
296  std::cerr << "ComplexRationalNumber::convertToExpressionTree NOT YET IMPLEMENTED" << std::endl;
297  exit(1);
298  return ExpressionTree();
299  }
300 
302  ComplexRationalNumber r (*this);
303  return (r /= c);
304  }
305 
307  if (c.isZero()) {
308  std::cout << "BPAS: error, dividend is zero from ComplexRationalNumber."<< std::endl;
309  exit(1);
310  }
311  mpq_class r = c.a*c.a + c.b*c.b;
312  mpq_class t = (a*c.a+b*c.b)/r;
313  mpq_class e = (b*c.a-c.b*a)/r;
314  a = t;
315  b = e;
316  return *this;
317  }
318 
320  return 0;
321  }
322 
324  *this = 0;
325  return *this;
326  }
327 
328  /**
329  * GCD(a, b)
330  *
331  * @param b: The other rational number
332  **/
335  if (isZero() && c.isZero())
336  e.zero();
337  else
338  e.one();
339  return e;
340  }
341 
342  /**
343  * Compute squarefree factorization of *this
344  */
346  std::vector<ComplexRationalNumber> ret;
347  ret.push_back(*this);
348  return ret;
349  }
350 
351  /**
352  * Get the euclidean size of *this.
353  */
355  //TODO
356  std::cerr << "ComplexRationalNumber::euclideanSize NOT YET IMPLEMENTED" << std::endl;
357  exit(1);
358  return ComplexRationalNumber();
359  }
360 
361  /**
362  * Perform the eucldiean division of *this and b. Returns the
363  * remainder. If q is not NULL, then returns the quotient in q.
364  */
366 
367  /**
368  * Perform the extended euclidean division on *this and b.
369  * Returns the GCD. If s and t are not NULL, returns the bezout coefficients in them.
370  */
372 
373  /**
374  * Get the quotient of *this and b.
375  */
377 
378  /**
379  * Get the remainder of *this and b.
380  */
382 
385  mpq_class e = a * a + b * b;
386  r.a = a/e;
387  r.b = -b/e;
388  return r;
389  }
390 
391  inline RationalNumber realPart() const {
392  return RationalNumber(a);
393  }
394 
395  inline RationalNumber imaginaryPart() const {
396  return RationalNumber(b);
397  }
398 
399  inline ComplexRationalNumber conjugate() const {
400  ComplexRationalNumber r(a, -b);
401  return r;
402  }
403 
404  void print(std::ostream& out) const;
405 
406 };
407 
408 
409 #endif
A univariate polynomial over an arbitrary BPASRing represented sparsely.
Definition: BigPrimeField.hpp:21
bool operator!=(const ComplexRationalNumber &c) const
Inequality test,.
Definition: ComplexRationalNumber.hpp:186
ComplexRationalNumber euclideanSize() const
Get the euclidean size of *this.
Definition: ComplexRationalNumber.hpp:354
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:209
ComplexRationalNumber operator/(const ComplexRationalNumber &c) const
Exact division.
Definition: ComplexRationalNumber.hpp:301
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:226
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:323
ComplexRationalNumber & operator-=(const ComplexRationalNumber &c)
Subtraction assignment.
Definition: ComplexRationalNumber.hpp:220
void zero()
Assign to zero.
Definition: ComplexRationalNumber.hpp:112
bool operator==(const ComplexRationalNumber &c) const
Equality test,.
Definition: ComplexRationalNumber.hpp:168
ComplexRationalNumber operator+(const ComplexRationalNumber &c) const
Addition.
Definition: ComplexRationalNumber.hpp:204
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
ComplexRationalNumber gcd(const ComplexRationalNumber &c) const
GCD(a, b)
Definition: ComplexRationalNumber.hpp:333
void negativeOne()
Assign to negative one.
Definition: ComplexRationalNumber.hpp:150
ComplexRationalNumber operator^(long long int e) const
Overload operator ^ replace xor operation by exponentiation.
Definition: ComplexRationalNumber.hpp:262
ComplexRationalNumber operator%(const ComplexRationalNumber &c) const
Get the remainder of *this and b;.
Definition: ComplexRationalNumber.hpp:319
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:15
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:306
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:295
int isConstant() const
Is a constant.
Definition: ComplexRationalNumber.hpp:160
ComplexRationalNumber inverse() const
Get the inverse of *this.
Definition: ComplexRationalNumber.hpp:383
ComplexRationalNumber remainder(const ComplexRationalNumber &b) const
Get the remainder of *this and b.
bool isOne() const
Is a 1.
Definition: ComplexRationalNumber.hpp:122
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
ComplexRationalNumber operator*(const ComplexRationalNumber &c) const
Multiplication.
Definition: ComplexRationalNumber.hpp:231
ComplexRationalNumber & operator*=(const ComplexRationalNumber &c)
Multiplication assignment.
Definition: ComplexRationalNumber.hpp:236
ComplexRationalNumber & operator^=(long long int e)
Exponentiation assignment.
Definition: ComplexRationalNumber.hpp:290
Factors< ComplexRationalNumber > squareFree() const
Compute squarefree factorization of *this.
Definition: ComplexRationalNumber.hpp:345
bool isZero() const
Is a zero.
Definition: ComplexRationalNumber.hpp:103
bool isNegativeOne() const
Is a -1.
Definition: ComplexRationalNumber.hpp:141
void one()
Assign to one.
Definition: ComplexRationalNumber.hpp:131