carl  24.04
Computer ARithmetic Library
GaloisField.h
Go to the documentation of this file.
1 /**
2  * @file: GaloisField.h
3  * @author: Sebastian Junges
4  *
5  * @since October 17, 2013
6  */
7 
8 #pragma once
9 
12 
13 #include <map>
14 #include <memory>
15 #include <mutex>
16 #include <utility>
17 
18 namespace carl
19 {
20 
21 template<typename IntegerType>
23  bool operator()(const std::pair<IntegerType, IntegerType>& p1,const std::pair<IntegerType, IntegerType>& p2) const {
24  return (p1.first < p2.first) || (p1.first == p2.first && p1.second < p2.second);
25  }
26 };
27 
28 /**
29  * A finite field.
30  */
31 template<typename IntegerType>
32 class GaloisField {
33 public:
34  using BaseIntType = unsigned;
35 private:
36  const BaseIntType mP;
37  const BaseIntType mK;
38  const IntegerType mPK; // = mP ^ mK
39  const IntegerType mMaxValue; // = (mPK-1) / 2
40  const IntegerType mModulus; // = (mPK+1) / 2 = mMaxValue + 1
41 
42  public:
43  /**
44  * Creating the field Z_{p^k}
45  * @param p A prime number
46  * @param k A exponent
47  * @see GaloisFieldManager where the overhead of creating several GFs is prevented by storing them.
48  */
50  mP(p), mK(k),
51  mPK(pow(p,k)),
52  mMaxValue((mPK-1)/2),
54  {
55  }
56 
57  /**
58  * Returns the p from Z_{p^k}
59  * @return a prime
60  */
61  BaseIntType p() const noexcept {
62  return mP;
63  }
64 
65  /**
66  * Returns the k from Z_{p^k}
67  * @return A positive integer
68  */
69  BaseIntType k() const noexcept {
70  return mK;
71  }
72 
73  const IntegerType& size() const noexcept {
74  return mPK;
75  }
76 
77  IntegerType modulo(const IntegerType& n) const {
78  return symmetric_modulo(n);
79  }
80 
81  IntegerType symmetric_modulo(const IntegerType& n) const {
82  if (n > mMaxValue) {
83  return carl::mod(IntegerType(n-mModulus), mPK) - mMaxValue;
84  } else {
85  return carl::mod(n, mPK);
86  }
87  }
88 
89  friend bool operator==(const GaloisField& lhs, const GaloisField& rhs) {
90  return lhs.mPK == rhs.mPK;
91  }
92 
93  friend std::ostream& operator<<(std::ostream& os, const GaloisField& rhs) {
94  return os << "GF(" << rhs.mP << "^" << rhs.mK << ")";
95  }
96 };
97 
98 template<typename IntegerType>
99 class GaloisFieldManager: public Singleton<GaloisFieldManager<IntegerType>> {
100 public:
102 private:
103  std::map<std::pair<BaseIntType,BaseIntType>, std::unique_ptr<GaloisField<IntegerType>>, IntegerPairCompare<unsigned>> mGaloisFields;
104 public:
105 
107  {
108  auto it = mGaloisFields.find(std::make_pair(p,k));
109  if (it == mGaloisFields.end()) {
110  auto newit = mGaloisFields.emplace(
111  std::make_pair(p,k),
112  std::make_unique<GaloisField<IntegerType>>(p, k)
113  );
114  assert(newit.second);
115  return newit.first->second.get();
116  } else {
117  return it->second.get();
118  }
119  }
120 };
121 
122 }
carl is the main namespace for the library.
cln::cl_I mod(const cln::cl_I &a, const cln::cl_I &b)
Calculate the remainder of the integer division.
Definition: operations.h:445
Interval< Number > pow(const Interval< Number > &i, Integer exp)
Definition: Power.h:11
bool operator()(const std::pair< IntegerType, IntegerType > &p1, const std::pair< IntegerType, IntegerType > &p2) const
Definition: GaloisField.h:23
A finite field.
Definition: GaloisField.h:32
friend bool operator==(const GaloisField &lhs, const GaloisField &rhs)
Definition: GaloisField.h:89
IntegerType symmetric_modulo(const IntegerType &n) const
Definition: GaloisField.h:81
const BaseIntType mK
Definition: GaloisField.h:37
unsigned BaseIntType
Definition: GaloisField.h:34
const IntegerType mPK
Definition: GaloisField.h:38
BaseIntType p() const noexcept
Returns the p from Z_{p^k}.
Definition: GaloisField.h:61
const IntegerType mMaxValue
Definition: GaloisField.h:39
const IntegerType & size() const noexcept
Definition: GaloisField.h:73
friend std::ostream & operator<<(std::ostream &os, const GaloisField &rhs)
Definition: GaloisField.h:93
BaseIntType k() const noexcept
Returns the k from Z_{p^k}.
Definition: GaloisField.h:69
const BaseIntType mP
Definition: GaloisField.h:36
const IntegerType mModulus
Definition: GaloisField.h:40
GaloisField(BaseIntType p, BaseIntType k=1)
Creating the field Z_{p^k}.
Definition: GaloisField.h:49
IntegerType modulo(const IntegerType &n) const
Definition: GaloisField.h:77
const GaloisField< IntegerType > * field(BaseIntType p, BaseIntType k=1)
Definition: GaloisField.h:106
typename GaloisField< IntegerType >::BaseIntType BaseIntType
Definition: GaloisField.h:101
std::map< std::pair< BaseIntType, BaseIntType >, std::unique_ptr< GaloisField< IntegerType > >, IntegerPairCompare< unsigned > > mGaloisFields
Definition: GaloisField.h:103
Base class that implements a singleton.
Definition: Singleton.h:24