carl  24.04
Computer ARithmetic Library
GFNumber.h
Go to the documentation of this file.
1 /**
2  * @file GFNumber.h
3  * @author Sebastian Junges
4  *
5  * @since October 16, 2013
6  */
7 
8 #pragma once
9 
10 #include "GaloisField.h"
11 #include "numbers.h"
12 
13 namespace carl
14 {
15 
16 /**
17  * Galois Field numbers, i.e. numbers from fields with a finite characteristic.
18  */
19 template<typename IntegerType>
20 class GFNumber
21 {
23  const GaloisField<IntegerType>* mGf = nullptr;
24 
25  public:
26 
27  GFNumber() = default;
28  explicit GFNumber(IntegerType n, const GaloisField<IntegerType>* gf = nullptr):
29  mN(gf == nullptr ? n : gf->symmetric_modulo(n)),
30  mGf(gf)
31  {
32  }
33 
35  mN(n.mN),
36  mGf(gf)
37  {
38  }
39 
41  {
42  return mGf;
43  }
44 
46  {
47  return GFNumber(mN, newfield);
48  }
49 
50  void normalize()
51  {
52  if(is_zero() || is_unit()) return;
53  assert(mGf != nullptr);
54  mGf->modulo(mN);
55  }
56 
57  bool is_zero() const
58  {
59  return mN == 0;
60  }
61 
62  bool is_one() const
63  {
64  return is_unit();
65  }
66 
67  bool is_unit() const
68  {
69  return mN == 1;
70  }
71 
72  const IntegerType& representing_integer() const
73  {
74  return mN;
75  }
76 
77  GFNumber inverse() const;
78 
79  template<typename IntegerT>
80  friend bool operator==(const GFNumber<IntegerT>& lhs, const GFNumber<IntegerT>& rhs);
81  /**
82  * lhs == rhs, if rhs \\in [lhs].
83  * @return
84  */
85  template<typename IntegerT>
86  friend bool operator==(const GFNumber<IntegerT>& lhs, const IntegerT& rhs);
87  /**
88  * lhs == rhs, if lhs \\in [rhs].
89  * @return
90  */
91  template<typename IntegerT>
92  friend bool operator==(const IntegerT& lhs, const GFNumber<IntegerT>& rhs);
93  template<typename IntegerT>
94  friend bool operator==(const GFNumber<IntegerT>& lhs, const GFNumber<IntegerT>& rhs);
95  /**
96  * lhs == rhs, if rhs \\in [lhs].
97  * @return
98  */
99  template<typename IntegerT>
100  friend bool operator==(const GFNumber<IntegerT>& lhs, int rhs);
101  /**
102  * lhs == rhs, if lhs \\in [rhs].
103  * @return
104  */
105  template<typename IntegerT>
106  friend bool operator==(int lhs, const GFNumber<IntegerT>& rhs);
107  template<typename IntegerT>
108  friend bool operator!=(const GFNumber<IntegerT>& lhs, const GFNumber<IntegerT>& rhs);
109  template<typename IntegerT>
110  friend bool operator!=(const GFNumber<IntegerT>& lhs, const IntegerT& rhs);
111  template<typename IntegerT>
112  friend bool operator!=(const IntegerT& lhs, const GFNumber<IntegerT>& rhs);
113  template<typename IntegerT>
114  friend bool operator!=(const GFNumber<IntegerT>& lhs, int rhs);
115  template<typename IntegerT>
116  friend bool operator!=(int lhs, const GFNumber<IntegerT>& rhs);
117 
118 
119  const GFNumber operator-() const;
120 
121  template<typename IntegerT>
123  template<typename IntegerT>
124  friend GFNumber<IntegerT> operator+(const GFNumber<IntegerT>& lhs, const IntegerT& rhs);
125  template<typename IntegerT>
126  friend GFNumber<IntegerT> operator+(const IntegerT& lhs, const GFNumber<IntegerT>& rhs);
127 
130  GFNumber& operator+=(const IntegerType& rhs);
131 
132  template<typename IntegerT>
134  template<typename IntegerT>
135  friend GFNumber<IntegerT> operator-(const GFNumber<IntegerT>& lhs, const IntegerT& rhs);
136  template<typename IntegerT>
137  friend GFNumber<IntegerT> operator-(const IntegerT& lhs, const GFNumber<IntegerT>& rhs);
138 
141  GFNumber& operator-=(const IntegerType& rhs);
142 
143  template<typename IntegerT>
145  template<typename IntegerT>
146  friend GFNumber<IntegerT> operator*(const GFNumber<IntegerT>& lhs, const IntegerT& rhs);
147  template<typename IntegerT>
148  friend GFNumber<IntegerT> operator*(const IntegerT& lhs, const GFNumber<IntegerT>& rhs);
149 
151  GFNumber& operator*=(const IntegerType& rhs);
152 
153  template<typename IntegerT>
155 
157 
158  friend std::ostream& operator<<(std::ostream& os, const GFNumber& rhs)
159  {
160  os << "(" << rhs.mN << ") mod ";
161  if(rhs.mGf != nullptr)
162  {
163  os << rhs.mGf->size();
164  }
165  else
166  {
167  os << "?";
168  }
169  return os;
170  }
171 
172 };
173 
174 template<typename IntegerT>
175 bool is_zero(const GFNumber<IntegerT>& _in) {
176  return _in.representing_integer() == 0;
177 }
178 
179 template<typename IntegerT>
180 bool is_one(const GFNumber<IntegerT>& _in) {
181  return _in.representing_integer() == 1;
182 }
183 
184 template<typename IntegerT>
186  return lhs / rhs;
187 }
188 
189 template<typename IntegerT>
191  return n;
192 }
193 
194 /**
195  * @todo Implement this
196  * @param
197  * @return
198  */
199 template<typename IntegerT>
200 inline bool is_integer(const GFNumber<IntegerT>& /*unused*/) {
201  return false;
202 }
203 
204 /**
205  * Creates the string representation to the given galois field number.
206  * @param _number The galois field number to get its string representation for.
207  * @return The string representation to the given galois field number.
208  */
209 template<typename IntegerType>
210 std::string toString(const GFNumber<IntegerType>& _number, bool /*unused*/)
211 {
212  std::stringstream s;
213  s << _number;
214  return s.str();
215 }
216 
217 }
218 #include "GFNumber.tpp"
carl is the main namespace for the library.
std::string toString(Relation r)
Definition: Relation.h:73
Interval< Number > abs(const Interval< Number > &_in)
Method which returns the absolute value of the passed number.
Definition: Interval.h:1511
Interval< Number > quotient(const Interval< Number > &_lhs, const Interval< Number > &_rhs)
Implements the division with remainder.
Definition: Interval.h:1488
bool is_zero(const Interval< Number > &i)
Check if this interval is a point-interval containing 0.
Definition: Interval.h:1453
bool is_integer(const Interval< Number > &n)
Definition: Interval.h:1445
bool is_one(const Interval< Number > &i)
Check if this interval is a point-interval containing 1.
Definition: Interval.h:1462
static const T & get()
Definition: constants.h:42
A finite field.
Definition: GaloisField.h:32
Galois Field numbers, i.e.
Definition: GFNumber.h:21
IntegerType mN
Definition: GFNumber.h:22
friend bool operator==(const GFNumber< IntegerT > &lhs, const IntegerT &rhs)
lhs == rhs, if rhs \in [lhs].
GFNumber & operator++()
GFNumber(IntegerType n, const GaloisField< IntegerType > *gf=nullptr)
Definition: GFNumber.h:28
friend bool operator==(const GFNumber< IntegerT > &lhs, int rhs)
lhs == rhs, if rhs \in [lhs].
friend bool operator!=(int lhs, const GFNumber< IntegerT > &rhs)
GFNumber & operator*=(const IntegerType &rhs)
friend bool operator!=(const GFNumber< IntegerT > &lhs, const GFNumber< IntegerT > &rhs)
GFNumber & operator*=(const GFNumber &rhs)
GFNumber & operator-=(const GFNumber &rhs)
void normalize()
Definition: GFNumber.h:50
GFNumber & operator+=(const GFNumber &rhs)
friend bool operator==(const GFNumber< IntegerT > &lhs, const GFNumber< IntegerT > &rhs)
GFNumber< IntegerType > toGF(const GaloisField< IntegerType > *newfield) const
Definition: GFNumber.h:45
friend GFNumber< IntegerT > operator-(const GFNumber< IntegerT > &lhs, const IntegerT &rhs)
GFNumber()=default
friend GFNumber< IntegerT > operator*(const IntegerT &lhs, const GFNumber< IntegerT > &rhs)
friend bool operator!=(const GFNumber< IntegerT > &lhs, int rhs)
bool is_zero() const
Definition: GFNumber.h:57
const GaloisField< IntegerType > * gf() const
Definition: GFNumber.h:40
friend bool operator!=(const IntegerT &lhs, const GFNumber< IntegerT > &rhs)
friend GFNumber< IntegerT > operator+(const IntegerT &lhs, const GFNumber< IntegerT > &rhs)
GFNumber & operator/=(const GFNumber &rhs)
GFNumber inverse() const
friend GFNumber< IntegerT > operator/(const GFNumber< IntegerT > &lhs, const GFNumber< IntegerT > &rhs)
friend GFNumber< IntegerT > operator+(const GFNumber< IntegerT > &lhs, const IntegerT &rhs)
friend GFNumber< IntegerT > operator-(const IntegerT &lhs, const GFNumber< IntegerT > &rhs)
friend bool operator==(int lhs, const GFNumber< IntegerT > &rhs)
lhs == rhs, if lhs \in [rhs].
friend bool operator!=(const GFNumber< IntegerT > &lhs, const IntegerT &rhs)
GFNumber(const GFNumber &n, const GaloisField< IntegerType > *gf)
Definition: GFNumber.h:34
bool is_one() const
Definition: GFNumber.h:62
GFNumber & operator+=(const IntegerType &rhs)
const GaloisField< IntegerType > * mGf
Definition: GFNumber.h:23
bool is_unit() const
Definition: GFNumber.h:67
friend GFNumber< IntegerT > operator*(const GFNumber< IntegerT > &lhs, const IntegerT &rhs)
const IntegerType & representing_integer() const
Definition: GFNumber.h:72
friend GFNumber< IntegerT > operator+(const GFNumber< IntegerT > &lhs, const GFNumber< IntegerT > &rhs)
friend GFNumber< IntegerT > operator-(const GFNumber< IntegerT > &lhs, const GFNumber< IntegerT > &rhs)
friend GFNumber< IntegerT > operator*(const GFNumber< IntegerT > &lhs, const GFNumber< IntegerT > &rhs)
GFNumber & operator--()
GFNumber & operator-=(const IntegerType &rhs)
friend std::ostream & operator<<(std::ostream &os, const GFNumber &rhs)
Definition: GFNumber.h:158
const GFNumber operator-() const
friend bool operator==(const IntegerT &lhs, const GFNumber< IntegerT > &rhs)
lhs == rhs, if lhs \in [rhs].