carl  24.04
Computer ARithmetic Library
GFNumber.tpp
Go to the documentation of this file.
1 /**
2  * @file: GFNumber.tpp
3  * @author: Sebastian Junges
4  *
5  * @since October 17, 2013
6  */
7 
8 #pragma once
9 
10 #include "GFNumber.h"
11 #include "adaption_native/EEA.h"
12 
13 namespace carl
14 {
15 
16 template<typename IntegerT>
17 GFNumber<IntegerT> GFNumber<IntegerT>::inverse() const
18 {
19  assert(mGf != nullptr);
20  return GFNumber<IntegerT>(EEA<IntegerT>::calculate(mGf->size(), mN).second, mGf);
21 
22 }
23 
24 
25 
26 template<typename IntegerT>
27 bool operator==(const GFNumber<IntegerT>& lhs, const GFNumber<IntegerT>& rhs)
28 {
29  if (lhs.is_zero() && rhs.is_zero()) return true;
30  if (lhs.is_unit() && rhs.is_unit()) return true;
31  if (lhs.mN == rhs.mN) return true;
32  if (!lhs.mGf || !rhs.mGf) return false;
33  assert(lhs.mGf != nullptr);
34  assert(rhs.mGf != nullptr);
35  return (lhs.mGf == rhs.mGf || *(lhs.mGf) == *(rhs.mGf)) && (lhs.mGf->modulo(lhs.mN - rhs.mN) == 0);
36 }
37 template<typename IntegerT>
38 bool operator==(const GFNumber<IntegerT>& lhs, const IntegerT& rhs)
39 {
40  assert(lhs.mGf != nullptr);
41  return lhs.mGf->modulo(lhs.mN) == rhs;
42 }
43 template<typename IntegerT>
44 bool operator==(const IntegerT& lhs, const GFNumber<IntegerT>& rhs)
45 {
46  return rhs == lhs;
47 }
48 template<typename IntegerT>
49 bool operator==(const GFNumber<IntegerT>& lhs, int rhs)
50 {
51  if(lhs.is_zero() && rhs == 0) return true;
52  if(lhs.is_unit() && rhs == 1) return true;
53  if(lhs.is_zero() && rhs != 0) return false;
54  if(lhs.is_unit() && rhs != 1) return false;
55  assert(lhs.mGf != nullptr);
56  return lhs.mGf->modulo(lhs.mN) == rhs;
57 }
58 template<typename IntegerT>
59 bool operator==(int lhs, const GFNumber<IntegerT>& rhs)
60 {
61  return rhs == lhs;
62 }
63 
64 
65 template<typename IntegerT>
66 bool operator!=(const GFNumber<IntegerT>& lhs, const GFNumber<IntegerT>& rhs)
67 {
68  return !(lhs == rhs);
69 }
70 template<typename IntegerT>
71 bool operator!=(const GFNumber<IntegerT>& lhs, const IntegerT& rhs)
72 {
73  return !(lhs == rhs);
74 }
75 template<typename IntegerT>
76 bool operator!=(const IntegerT& lhs, const GFNumber<IntegerT>& rhs)
77 {
78  return !(lhs == rhs);
79 }
80 
81 template<typename IntegerT>
82 bool operator!=(const GFNumber<IntegerT>& lhs, int rhs)
83 {
84  return !(lhs == rhs);
85 }
86 template<typename IntegerT>
87 bool operator!=(int lhs, const GFNumber<IntegerT>& rhs)
88 {
89  return !(lhs == rhs);
90 }
91 
92 template<typename IntegerT>
93 const GFNumber<IntegerT> GFNumber<IntegerT>::operator-() const
94 {
95  return GFNumber<IntegerT>(-mN, mGf);
96 }
97 
98 template<typename IntegerT>
99 GFNumber<IntegerT> operator+(const GFNumber<IntegerT>& lhs, const GFNumber<IntegerT>& rhs)
100 {
101  assert(lhs.mGf == nullptr || rhs.mGf == nullptr || *(lhs.mGf) == *(rhs.mGf));
102  return GFNumber<IntegerT>(lhs.mN + rhs.mN, lhs.mGf == nullptr ? rhs.mGf : lhs.mGf);
103 }
104 template<typename IntegerT>
105 GFNumber<IntegerT> operator+(const GFNumber<IntegerT>& lhs, const IntegerT& rhs)
106 {
107  return GFNumber<IntegerT>(lhs.mN + rhs, lhs.mGf);
108 }
109 template<typename IntegerT>
110 GFNumber<IntegerT> operator+(const IntegerT& lhs, const GFNumber<IntegerT>& rhs)
111 {
112  return rhs + lhs;
113 }
114 
115 template<typename IntegerT>
116 GFNumber<IntegerT>& GFNumber<IntegerT>::operator ++()
117 {
118  mN++;
119  return *this;
120 }
121 
122 template<typename IntegerType>
123 GFNumber<IntegerType>& GFNumber<IntegerType>::operator +=(const GFNumber& rhs)
124 {
125  if(mGf == nullptr)
126  {
127  mGf = rhs.mGf;
128  }
129  mN += rhs.mN;
130  return *this;
131 }
132 
133 template<typename IntegerType>
134 GFNumber<IntegerType>& GFNumber<IntegerType>::operator +=(const IntegerType& rhs)
135 {
136  mN += rhs;
137  return *this;
138 }
139 
140 
141 template<typename IntegerT>
142 GFNumber<IntegerT> operator-(const GFNumber<IntegerT>& lhs, const GFNumber<IntegerT>& rhs)
143 {
144  assert(lhs.mGf == nullptr || rhs.mGf == nullptr || *(lhs.mGf) == *(rhs.mGf));
145  return GFNumber<IntegerT>(lhs.mN - rhs.mN, lhs.mGf == nullptr ? rhs.mGf : lhs.mGf);
146 }
147 template<typename IntegerT>
148 GFNumber<IntegerT> operator-(const GFNumber<IntegerT>& lhs, const IntegerT& rhs)
149 {
150  return GFNumber<IntegerT>(lhs.mN - rhs, lhs.mGf);
151 }
152 template<typename IntegerT>
153 GFNumber<IntegerT> operator-(const IntegerT& lhs, const GFNumber<IntegerT>& rhs)
154 {
155  return GFNumber<IntegerT>(lhs - rhs.mN, rhs.mGf);
156 }
157 
158 template<typename IntegerT>
159 GFNumber<IntegerT>& GFNumber<IntegerT>::operator --()
160 {
161  mN--;
162  return *this;
163 }
164 
165 template<typename IntegerType>
166 GFNumber<IntegerType>& GFNumber<IntegerType>::operator -=(const GFNumber& rhs)
167 {
168  if (rhs.is_zero()) return *this;
169  if (mGf == nullptr) {
170  mGf = rhs.mGf;
171  }
172  mN -= rhs.mN;
173  return *this;
174 }
175 
176 template<typename IntegerType>
177 GFNumber<IntegerType>& GFNumber<IntegerType>::operator -=(const IntegerType& rhs)
178 {
179  mN -= rhs;
180  return *this;
181 }
182 
183 template<typename IntegerT>
184 GFNumber<IntegerT> operator*(const GFNumber<IntegerT>& lhs, const GFNumber<IntegerT>& rhs)
185 {
186  assert(lhs.mGf == nullptr || rhs.mGf == nullptr || *(lhs.mGf) == *(rhs.mGf));
187  return GFNumber<IntegerT>(lhs.mN * rhs.mN, lhs.mGf == nullptr ? rhs.mGf : lhs.mGf);
188 }
189 template<typename IntegerT>
190 GFNumber<IntegerT> operator*(const GFNumber<IntegerT>& lhs, const IntegerT& rhs)
191 {
192  return GFNumber<IntegerT>(lhs.mN * rhs, lhs.mGf);
193 }
194 template<typename IntegerT>
195 GFNumber<IntegerT> operator*(const IntegerT& lhs, const GFNumber<IntegerT>& rhs)
196 {
197  return rhs * lhs;
198 }
199 
200 
201 template<typename IntegerT>
202 GFNumber<IntegerT>& GFNumber<IntegerT>::operator *=(const GFNumber& rhs)
203 {
204  assert(rhs.mGf == nullptr || *mGf == *(rhs.mGf));
205  mN *= rhs.mN;
206  return *this;
207 }
208 
209 template<typename IntegerType>
210 GFNumber<IntegerType>& GFNumber<IntegerType>::operator *=(const IntegerType& rhs)
211 {
212  mN *= rhs;
213  return *this;
214 }
215 
216 
217 template<typename IntegerT>
218 GFNumber<IntegerT> operator/(const GFNumber<IntegerT>& lhs, const GFNumber<IntegerT>& rhs)
219 {
220  assert(!rhs.is_zero());
221  assert(rhs.mGf != nullptr);
222  return GFNumber<IntegerT>(lhs.mN * rhs.inverse().mN, rhs.mGf);
223 }
224 
225 
226 template<typename IntegerT>
227 GFNumber<IntegerT>& GFNumber<IntegerT>::operator /=(const GFNumber<IntegerT>& rhs)
228 {
229  assert(!rhs.is_zero());
230  mN * rhs.inverse().mN;
231  return *this;
232 }
233 
234 }