Macaulay2 Engine
Loading...
Searching...
No Matches
Polynomial.hpp
Go to the documentation of this file.
1#ifndef _polynomial_hpp_
2#define _polynomial_hpp_
3
34
35#include "newdelete.hpp" // for our_new_delete
36#include "ringelem.hpp" // for ring_elem
37#include "style.hpp" // for GT, LT, EQ
38
39#include <cassert> // for assert
40#include <algorithm> // for copy
41#include <iostream> // for ostream
42#include <iterator> // for forward_iterator_tag
43#include <utility> // for pair, make_pair
44
60struct Monom
61// Format for monomials:
62 // A monomial is an array of ints, the first of which is the length of that array (including length field).
63 // e.g. [2 0] (is currently the empty monomial, but this class knows nothing about specific monomials.
64 // each monomial is of the form:
65 // <length: n+2> <degree: (currently) n> <var1> <var2> ... <varn>
66 // e.g. xyxy: 6 4 0 1 0 1
67 // xy23x: 27 25 0 1 1 ... 1 0
68 // 2 monomials: xzx, xy (dot is only there for readability)
69 // 5 3 0 2 0 . 4 2 0 1
70 // TODO: There are now weights in the monomial (which only the monoid knows about),
71 // so this needs to be updated.
72{
73 Monom() : mValue(nullptr) {}
74 Monom(const int* value) : mValue(value) {}
75 // const int* operator*() const { return mValue; }
76 const int* operator+(int i) const { return mValue+i; }
77
78 int operator[](int i) const { return mValue[i]; }
79
80 int size() const { return *mValue; }
81
82 const int* begin() const { return mValue; }
83 const int* end() const { return mValue + *mValue; }
84
85private:
86 const int* mValue; // We are visiting this monomial, we do not own it!
87};
88
89std::ostream& operator<<(std::ostream& o, const Monom& m);
90
108// Format for such a monomial:
109// [len value hashval comp deg v1 v2 ... vr]
110// where [len-3 deg v1 v2 ... vr] is a Monom.
111{
112
113public:
115
116 // const int* operator*() const { return mValue; }
117 const int* operator+(int i) const { return mValue+i; }
118 int operator[](int i) const { return mValue[i]; }
119 int* operator+(int i) { return mValue+i; }
120 int& operator[](int i) { return mValue[i]; }
121
122 int size() const { return *mValue; }
123
124 const int* begin() const { return mValue; }
125 const int* end() const { return mValue + *mValue; }
126 int* begin() { return mValue; }
127 int* end() { return mValue + *mValue; }
128
129 int component() const { return mValue[3]; }
130
132 {
133 return m.size() + 3;
134 }
135
136 void setIndex(int idx)
137 {
138 mValue[1] = idx;
139 }
140
141 int index() const { return mValue[1]; }
142
143 std::size_t hash() const
144 {
145 if (mValue[2] == 0) setHashValue();
146 return mValue[2];
147 }
148
149 static int compare(const ModuleMonom& m1, const ModuleMonom& m2)
150 {
151 if (m1[2] > m2[2]) return GT;
152 if (m1[2] < m2[2]) return LT;
153 if (m1[3] > m2[3]) return GT;
154 if (m1[3] < m2[3]) return LT;
155 // at this stage, they have the same degree, so use lex order
156 for (int j = 4; j < m1[0]; j++)
157 {
158 if (m1[j] > m2[j]) return LT;
159 if (m1[j] < m2[j]) return GT;
160 }
161 // if we are here, the monomials are the same.
162 return EQ;
163 }
164
165 bool operator==(const ModuleMonom &rhs) const
166 {
167 if (mValue[0] != rhs[0]) return false;
168 for (int i=2; i < mValue[0]; ++i)
169 if (mValue[i] != rhs[i]) return false;
170 return true;
171 }
172private:
173 void setHashValue() const
174 {
175 int result = 0;
176 int* end = mValue + *mValue;
177 for (auto i = mValue+3; i < end; ++i)
178 result = 17*result + *i;
179 const_cast<int*>(mValue)[2] = result;
180 }
181private:
182 int* mValue; // We are visiting this monomial, we do not own it!
183};
184
185std::ostream& operator<<(std::ostream& o, const ModuleMonom& m);
186
187inline ModuleMonom monomToModuleMonom(const Monom& a, int comp, std::pair<int*, int*> allocated_result)
188{
189 assert(allocated_result.second-allocated_result.first >= ModuleMonom::sizeOfCorrespondingModuleMonom(a));
190 auto begin = allocated_result.first;
192 begin[1] = 0; // index
193 begin[2] = 0; // hashval
194 begin[3] = comp;
195 std::copy(a.begin()+1, a.end(), begin+4);
196 return ModuleMonom(begin);
197}
198
199template<typename T>
200void appendModuleMonomToMonom(const ModuleMonom& a, int& comp, T& inserter)
201{
202 (void) comp;
203 inserter.push_back(a.size()-3);
204 for (int i=4; i<a.size(); ++i)
205 inserter.push_back(a[i]);
206}
207
211template<typename CoefficientRingType>
213{
214 friend class M2FreeAlgebra;
216 friend class FreeAlgebra;
217 friend class NCF4;
218
220public:
222 using monomVector = gc_vector<int>; // TODO: remove monomVector?
223
224 typedef typename coeffVector::iterator coeffIterator;
225 typedef monomVector::iterator monomIterator;
226
227 typedef typename coeffVector::const_iterator coeffConstIterator;
228 typedef monomVector::const_iterator monomConstIterator;
229
230 // this class is an non-const_iterator for traversing the terms in a polynomial.
232 {
233 public:
234 // useful typedefs
236 typedef std::forward_iterator_tag iterator_category;
237
238 // constructor
240
241 // iteration functions
243 {
244 // prefix ++ operator
246 return *this;
247 }
248
250 {
251 (void) junk;
252 // postfix ++ operator
253 self_type i = *this;
255 return i;
256 }
257
258 // accessor functions -- (unfortunately) replace the more convenient -> notation since
259 // we have two vector iterators.
260 const ring_elem coeff() const { return *(this->mCoeffIt); }
261 // for the record, we are using &*it here to get the pointer that records where an iterator currently is
262 // this seems like a bit of a hack, but it seems to be the way things are done.
263 // FRANK: Same as above, do we want to make a copy here?
264 Monom monom() const { return Monom((&*(this->mMonomIt))); }
265
266 std::pair<ring_elem, Monom> operator*() const { return std::make_pair(coeff(), monom()); }
267
268 // (in)equality checks
269 bool operator==(const self_type& rhs) const { return (this->mCoeffIt == rhs.mCoeffIt); }
270 bool operator!=(const self_type& rhs) const { return (this->mCoeffIt != rhs.mCoeffIt); }
271
274
275 private:
279 {
280 // this is the function that actually increments the various iterators
281 // increment the ring element first
282 mCoeffIt++;
283 // increment to the end of the monomial
284 mMonomIt += *mMonomIt; // move to next monomial
285 }
286 };
287
289 {
290 return const_iterator(mCoefficients.cbegin(), mMonomials.cbegin());
291 }
292
294 {
295 return const_iterator(mCoefficients.cend(), mMonomials.cend());
296 }
297
300
301 coeffConstIterator cbeginCoeff() const { return mCoefficients.cbegin(); }
302 monomConstIterator cbeginMonom() const { return mMonomials.cbegin(); }
303
304 coeffConstIterator cendCoeff() const { return mCoefficients.cend(); }
305 monomConstIterator cendMonom() const { return mMonomials.cend(); }
306
307 const coeffVector & getElementArray() const { return mCoefficients; }
308
309 const monomVector & getMonomVector() const { return mMonomials; }
311
312 size_t numTerms() const { return mCoefficients.size(); }
313
314private:
316
317private:
320};
321
337
341
342#endif
343
344// Local Variables:
345// compile-command: "make -C $M2BUILDDIR/Macaulay2/e "
346// indent-tabs-mode: nil
347// End:
gc_vector< const Poly * > ConstPolyList
Polynomial< CoefficientRingType > Poly
std::ostream & operator<<(std::ostream &o, const Monom &m)
Definition Polynomial.cpp:3
gc_vector< Poly * > PolyList
ModuleMonom monomToModuleMonom(const Monom &a, int comp, std::pair< int *, int * > allocated_result)
void appendModuleMonomToMonom(const ModuleMonom &a, int &comp, T &inserter)
const int * operator+(int i) const
static int sizeOfCorrespondingModuleMonom(const Monom &m)
int index() const
int size() const
int operator[](int i) const
void setIndex(int idx)
bool operator==(const ModuleMonom &rhs) const
int * begin()
const int * begin() const
std::size_t hash() const
static int compare(const ModuleMonom &m1, const ModuleMonom &m2)
int * operator+(int i)
int component() const
int & operator[](int i)
const int * end() const
ModuleMonom(int *begin)
void setHashValue() const
Monom extended with a module component, a stored index, and a memoised hash — the value type of IntsS...
coeffConstIterator mCoeffIt
coeffConstIterator cCoeffIterator() const
monomConstIterator mMonomIt
const_iterator(coeffConstIterator coeffIt, monomConstIterator monIt)
bool operator==(const self_type &rhs) const
std::pair< ring_elem, Monom > operator*() const
bool operator!=(const self_type &rhs) const
std::forward_iterator_tag iterator_category
self_type operator++(int junk)
const ring_elem coeff() const
monomConstIterator cMonomIterator() const
friend class M2FreeAlgebraOrQuotient
monomVector::iterator monomIterator
monomVector::const_iterator monomConstIterator
friend class FreeAlgebra
coeffVector & getCoeffInserter()
gc_vector< int > monomVector
friend class NCF4
coeffIterator endCoeff()
coeffConstIterator cendCoeff() const
const_iterator cbegin() const
friend class M2FreeAlgebra
size_t numTerms() const
coeffIterator beginCoeff()
CoefficientRingType::ElementType ElementType
coeffVector::const_iterator coeffConstIterator
coeffVector mCoefficients
const coeffVector & getElementArray() const
monomVector & getMonomInserter()
gc_vector< ElementType > coeffVector
monomConstIterator cbeginMonom() const
monomConstIterator cendMonom() const
coeffConstIterator cbeginCoeff() const
coeffVector::iterator coeffIterator
const_iterator cend() const
monomVector mMonomials
const monomVector & getMonomVector() const
VALGRIND_MAKE_MEM_DEFINED & result(result)
typename std::vector< T, gc_allocator< T > > gc_vector
a version of the STL vector, which allocates its backing memory with gc.
Definition newdelete.hpp:76
our_new_delete — per-class opt-in routing of new / delete through bdwgc.
TermIterator< Nterm > begin(Nterm *ptr)
Definition ringelem.cpp:4
ring_elem — the universal value type carried by every Ring* in the engine.
Default CoefficientRingType parameter for Polynomial<...>: a thin trait whose ElementType is just rin...
const int * end() const
Monom(const int *value)
const int * begin() const
int size() const
const int * mValue
const int * operator+(int i) const
int operator[](int i) const
Non-owning view onto a [length, degree, v1, v2, ..., vn] packed monomial in some externally managed b...
const int EQ
Definition style.hpp:40
const int GT
Definition style.hpp:41
const int LT
Definition style.hpp:39
Engine-wide stylistic constants: LT / EQ / GT codes, INTSIZE, GEOHEAP_SIZE.
#define T
Definition table.c:13