3 * @author: Sebastian Junges
5 * @since October 17, 2013
11 #include "adaption_native/EEA.h"
16 template<typename IntegerT>
17 GFNumber<IntegerT> GFNumber<IntegerT>::inverse() const
19 assert(mGf != nullptr);
20 return GFNumber<IntegerT>(EEA<IntegerT>::calculate(mGf->size(), mN).second, mGf);
26 template<typename IntegerT>
27 bool operator==(const GFNumber<IntegerT>& lhs, const GFNumber<IntegerT>& rhs)
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);
37 template<typename IntegerT>
38 bool operator==(const GFNumber<IntegerT>& lhs, const IntegerT& rhs)
40 assert(lhs.mGf != nullptr);
41 return lhs.mGf->modulo(lhs.mN) == rhs;
43 template<typename IntegerT>
44 bool operator==(const IntegerT& lhs, const GFNumber<IntegerT>& rhs)
48 template<typename IntegerT>
49 bool operator==(const GFNumber<IntegerT>& lhs, int rhs)
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;
58 template<typename IntegerT>
59 bool operator==(int lhs, const GFNumber<IntegerT>& rhs)
65 template<typename IntegerT>
66 bool operator!=(const GFNumber<IntegerT>& lhs, const GFNumber<IntegerT>& rhs)
70 template<typename IntegerT>
71 bool operator!=(const GFNumber<IntegerT>& lhs, const IntegerT& rhs)
75 template<typename IntegerT>
76 bool operator!=(const IntegerT& lhs, const GFNumber<IntegerT>& rhs)
81 template<typename IntegerT>
82 bool operator!=(const GFNumber<IntegerT>& lhs, int rhs)
86 template<typename IntegerT>
87 bool operator!=(int lhs, const GFNumber<IntegerT>& rhs)
92 template<typename IntegerT>
93 const GFNumber<IntegerT> GFNumber<IntegerT>::operator-() const
95 return GFNumber<IntegerT>(-mN, mGf);
98 template<typename IntegerT>
99 GFNumber<IntegerT> operator+(const GFNumber<IntegerT>& lhs, const GFNumber<IntegerT>& rhs)
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);
104 template<typename IntegerT>
105 GFNumber<IntegerT> operator+(const GFNumber<IntegerT>& lhs, const IntegerT& rhs)
107 return GFNumber<IntegerT>(lhs.mN + rhs, lhs.mGf);
109 template<typename IntegerT>
110 GFNumber<IntegerT> operator+(const IntegerT& lhs, const GFNumber<IntegerT>& rhs)
115 template<typename IntegerT>
116 GFNumber<IntegerT>& GFNumber<IntegerT>::operator ++()
122 template<typename IntegerType>
123 GFNumber<IntegerType>& GFNumber<IntegerType>::operator +=(const GFNumber& rhs)
133 template<typename IntegerType>
134 GFNumber<IntegerType>& GFNumber<IntegerType>::operator +=(const IntegerType& rhs)
141 template<typename IntegerT>
142 GFNumber<IntegerT> operator-(const GFNumber<IntegerT>& lhs, const GFNumber<IntegerT>& rhs)
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);
147 template<typename IntegerT>
148 GFNumber<IntegerT> operator-(const GFNumber<IntegerT>& lhs, const IntegerT& rhs)
150 return GFNumber<IntegerT>(lhs.mN - rhs, lhs.mGf);
152 template<typename IntegerT>
153 GFNumber<IntegerT> operator-(const IntegerT& lhs, const GFNumber<IntegerT>& rhs)
155 return GFNumber<IntegerT>(lhs - rhs.mN, rhs.mGf);
158 template<typename IntegerT>
159 GFNumber<IntegerT>& GFNumber<IntegerT>::operator --()
165 template<typename IntegerType>
166 GFNumber<IntegerType>& GFNumber<IntegerType>::operator -=(const GFNumber& rhs)
168 if (rhs.is_zero()) return *this;
169 if (mGf == nullptr) {
176 template<typename IntegerType>
177 GFNumber<IntegerType>& GFNumber<IntegerType>::operator -=(const IntegerType& rhs)
183 template<typename IntegerT>
184 GFNumber<IntegerT> operator*(const GFNumber<IntegerT>& lhs, const GFNumber<IntegerT>& rhs)
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);
189 template<typename IntegerT>
190 GFNumber<IntegerT> operator*(const GFNumber<IntegerT>& lhs, const IntegerT& rhs)
192 return GFNumber<IntegerT>(lhs.mN * rhs, lhs.mGf);
194 template<typename IntegerT>
195 GFNumber<IntegerT> operator*(const IntegerT& lhs, const GFNumber<IntegerT>& rhs)
201 template<typename IntegerT>
202 GFNumber<IntegerT>& GFNumber<IntegerT>::operator *=(const GFNumber& rhs)
204 assert(rhs.mGf == nullptr || *mGf == *(rhs.mGf));
209 template<typename IntegerType>
210 GFNumber<IntegerType>& GFNumber<IntegerType>::operator *=(const IntegerType& rhs)
217 template<typename IntegerT>
218 GFNumber<IntegerT> operator/(const GFNumber<IntegerT>& lhs, const GFNumber<IntegerT>& rhs)
220 assert(!rhs.is_zero());
221 assert(rhs.mGf != nullptr);
222 return GFNumber<IntegerT>(lhs.mN * rhs.inverse().mN, rhs.mGf);
226 template<typename IntegerT>
227 GFNumber<IntegerT>& GFNumber<IntegerT>::operator /=(const GFNumber<IntegerT>& rhs)
229 assert(!rhs.is_zero());
230 mN * rhs.inverse().mN;