carl  24.04
Computer ARithmetic Library
Ideal.h
Go to the documentation of this file.
1 /**
2  * @file Ideal.h
3  * @ingroup gb
4  * @author Sebastian Junges
5  */
6 
7 #pragma once
8 
11 
14 #include <unordered_set>
15 
16 namespace carl
17 {
18 
19 /**
20  * @ingroup gb
21  */
22 template <class Polynomial, template<class> class Datastructure = IdealDatastructureVector, int CacheSize = 0>
23 class Ideal
24 {
25 private:
26  std::vector<Polynomial> mGenerators;
27 
29 
30  std::unordered_set<size_t> mEliminated;
31  Datastructure<Polynomial> mDivisorLookup = Datastructure<Polynomial>(mGenerators, mEliminated, mTermOrder);
32 public:
33 
34  Ideal() = default;
35 
36  Ideal(const Polynomial& p1, const Polynomial& p2):
39  {
40  addGenerator(p1);
41  addGenerator(p2);
42  }
43 
44  virtual ~Ideal() = default;
45 
46  Ideal(const Ideal& rhs):
51  {
53  mDivisorLookup.reset();
54  }
55 
56  Ideal& operator=(const Ideal& rhs)
57  {
58  if(this == &rhs) return *this;
59  this->mGenerators.assign(rhs.mGenerators.begin(), rhs.mGenerators.end());
60  this->mEliminated = rhs.mEliminated;
61  this->mDivisorLookup = Datastructure<Polynomial>(mGenerators, mEliminated, mTermOrder);
63  mDivisorLookup.reset();
64  return *this;
65  }
66 
67  size_t addGenerator(const Polynomial& f)
68  {
69  size_t lastIndex = mGenerators.size();
70  mGenerators.push_back(f);
71  mDivisorLookup.addGenerator(lastIndex);
72  return lastIndex;
73  }
74 
76  {
77  return mDivisorLookup.getDivisor(t);
78  }
79 
80 
81 
83  {
84  return mDivisorLookup.isDividable(m);
85  }
86 
87  size_t nrGenerators() const
88  {
89  return mGenerators.size();
90  }
91 
92  std::vector<Polynomial>& getGenerators()
93  {
94  return mGenerators;
95  }
96 
97  const std::vector<Polynomial>& getGenerators() const
98  {
99  return mGenerators;
100  }
101 
102 
103  const Polynomial& getGenerator(size_t index) const
104  {
105  return mGenerators[index];
106  }
107 
108  std::vector<size_t> getOrderedIndices()
109  {
110  std::vector<size_t> orderedIndices;
111  for(size_t i = 0; i < mGenerators.size(); ++i)
112  {
113  orderedIndices.push_back(i);
114  }
115 
116  std::sort(orderedIndices.begin(), orderedIndices.end(), mTermOrder);
117  return orderedIndices;
118 
119  }
120 
121  void eliminateGenerator(size_t index)
122  {
123  mEliminated.insert(index);
124  }
125 
126  /**
127  * Invalidates indices
128  * @return a vector with the new indices
129  */
131  {
132  std::vector<Polynomial> tempGen;
133  for(size_t it = 0; it != mGenerators.size(); ++it)
134  {
135  if(mEliminated.count(it) == 0)
136  {
137  tempGen.push_back(mGenerators[it]);
138  }
139  }
140  tempGen.swap(mGenerators);
141  mEliminated.clear();
142 
143  }
144 
145  void clear()
146  {
147  mGenerators.clear();
148  mEliminated.clear();
149  mDivisorLookup.reset();
150  }
151 
152 
153  bool is_constant() const
154  {
155  return mGenerators.size() == 1 && mGenerators.front().is_constant();
156  }
157 
158  /**
159  * Checks whether all polynomials occurring in this ideal are linear.
160  * @return
161  */
162  bool is_linear() const
163  {
164  for(auto it = mGenerators.begin(); it != mGenerators.end(); ++it)
165  {
166  if(!it->is_linear()) return false;
167  }
168  return true;
169  }
170 
171  /**
172  * Gather all variables occurring in this ideal.
173  * @return
174  */
175  std::set<unsigned> gatherVariables() const
176  {
177  std::set<unsigned> vars;
178  for(auto it = mGenerators.begin(); it != mGenerators.end(); ++it)
179  {
180  it->gatherVariables(vars);
181  }
182  return vars;
183  }
184 
185 // std::set<unsigned> getSuperfluousVariables() const
186 // {
187 // std::set<unsigned> superfluous;
188 // for(auto it = mGenerators.begin(); it != mGenerators.end(); ++it)
189 // {
190 // //Variables occurring in polynomials x + y.
191 // if(it->nrOfTerms() == 2 && it->lterm().tdeg() == 1)
192 // {
193 // superfluous.insert(it->lterm().getSingleVariableNr());
194 // }
195 // }
196 // return superfluous;
197 // }
198 
199  friend std::ostream& operator<<(std::ostream& os, const Ideal& rhs )
200  {
201  os << "{";
202  for(Polynomial p : rhs.mGenerators)
203  {
204  os << p << std::endl;
205  }
206  return os << "}";
207  }
208 
209  void print(bool printOrigins=true, std::ostream& os = std::cout) const
210  {
211  for(typename std::vector<Polynomial>::const_iterator it = mGenerators.begin(); it != mGenerators.end(); ++it)
212  {
213  os << *it;
214  if(printOrigins)
215  {
216  os << " [";
217  it->getReasons().print();
218  os << "]";
219  }
220  os << ";\n";
221  }
222  }
223 };
224 
225 
226 }
carl is the main namespace for the library.
Sorts generators of an ideal by their leading terms.
void eliminateGenerator(size_t index)
Definition: Ideal.h:121
size_t addGenerator(const Polynomial &f)
Definition: Ideal.h:67
void removeEliminated()
Invalidates indices.
Definition: Ideal.h:130
std::vector< Polynomial > mGenerators
Definition: Ideal.h:26
const Polynomial & getGenerator(size_t index) const
Definition: Ideal.h:103
void clear()
Definition: Ideal.h:145
const std::vector< Polynomial > & getGenerators() const
Definition: Ideal.h:97
bool is_linear() const
Checks whether all polynomials occurring in this ideal are linear.
Definition: Ideal.h:162
std::unordered_set< size_t > mEliminated
Definition: Ideal.h:30
friend std::ostream & operator<<(std::ostream &os, const Ideal &rhs)
Definition: Ideal.h:199
sortByLeadingTerm< Polynomial > mTermOrder
Definition: Ideal.h:28
bool is_constant() const
Definition: Ideal.h:153
void print(bool printOrigins=true, std::ostream &os=std::cout) const
Definition: Ideal.h:209
std::vector< size_t > getOrderedIndices()
Definition: Ideal.h:108
std::vector< Polynomial > & getGenerators()
Definition: Ideal.h:92
bool isDividable(const Term< typename Polynomial::CoeffType > &m)
Definition: Ideal.h:82
Ideal(const Ideal &rhs)
Definition: Ideal.h:46
size_t nrGenerators() const
Definition: Ideal.h:87
std::set< unsigned > gatherVariables() const
Gather all variables occurring in this ideal.
Definition: Ideal.h:175
DivisionLookupResult< Polynomial > getDivisor(const Term< typename Polynomial::CoeffType > &t) const
Definition: Ideal.h:75
virtual ~Ideal()=default
Datastructure< Polynomial > mDivisorLookup
Definition: Ideal.h:31
Ideal(const Polynomial &p1, const Polynomial &p2)
Definition: Ideal.h:36
Ideal & operator=(const Ideal &rhs)
Definition: Ideal.h:56
Ideal()=default