Gröbner basis project
Codebase for research into Gröbner basis computation
polynomial_double_buffered.hpp
1 #ifndef __DBL_BUF_POLYNOMIAL_H_
2 #define __DBL_BUF_POLYNOMIAL_H_
3 
4 /*****************************************************************************\
5 * This file is part of DynGB. *
6 * *
7 * DynGB is free software: you can redistribute it and/or modify *
8 * it under the terms of the GNU General Public License as published by *
9 * the Free Software Foundation, either version 2 of the License, or *
10 * (at your option) any later version. *
11 * *
12 * Foobar is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU General Public License *
18 * along with DynGB. If not, see <http://www.gnu.org/licenses/>. *
19 \*****************************************************************************/
20 
21 #include <iostream>
22 using std::cout; using std::endl;
23 
24 #include "polynomial.hpp"
25 #include "polynomial_linked_list.hpp"
26 
27 extern Monomial_Ordering * generic_grevlex_ptr;
28 
30 
39 
40 public:
41 
43  DB_Polynomial_Iterator(const Double_Buffered_Polynomial * f, bool at_end=false);
46 
51 
53  virtual void restart_iteration() override;
55 
56  virtual void moveRight() override;
57 
58  virtual void moveLeft() override;
59 
60  virtual bool fellOff() const override;
61  virtual bool canMoveLeft() const override;
62  virtual bool canMoveRight() const override;
64 
66  virtual const Monomial & currMonomial() const override;
68 
69  virtual const Prime_Field_Element & currCoeff() const override;
71 
73  virtual void set_currCoeff(const Prime_Field_Element & a) override;
75 
76  virtual void set_currMonomial(const Monomial & t) override;
78 
79 protected:
80 
86  unsigned head;
88  unsigned tail;
90  long long current_position;
91 
92 };
93 
111 
112 public:
113 
115 
118  Polynomial_Ring & R,
119  Monomial_Ordering * order = generic_grevlex_ptr
120  );
121 
123 
125 
126 
129 
131 
133 
135  virtual Monomial & leading_monomial() const override;
136 
137  virtual Prime_Field_Element leading_coefficient() const override;
138 
139  virtual unsigned length() const override;
140 
141  virtual bool is_zero() const override;
142 
143  virtual bool can_reduce(Abstract_Polynomial &other) const override;
144 
145  virtual Double_Buffered_Polynomial * zero_polynomial() const override;
146 
147  virtual void set_monomial_ordering(
148  const Monomial_Ordering * order, bool sort_anew=true
149  ) override;
150 
152 
153 
155  virtual Double_Buffered_Polynomial * monomial_multiple(
156  const Monomial & t
157  ) const override;
158 
159  virtual Double_Buffered_Polynomial * scalar_multiple(
160  const Prime_Field_Element & c)
161  const override;
162 
163  virtual Mutable_Polynomial & operator += (const Abstract_Polynomial & p) override;
164 
165  virtual Mutable_Polynomial & operator -= (const Abstract_Polynomial & p) override;
166 
167  virtual void add_polynomial_multiple(
168  const Prime_Field_Element & a,
169  const Monomial & u,
170  const Abstract_Polynomial & p,
171  bool subtract
172  ) override;
173 
174  virtual void sort_by_order() override;
175 
177 
178 
180  virtual DB_Polynomial_Iterator * new_iterator() const override;
181 
182  virtual DB_Polynomial_Iterator * new_mutable_iterator() override;
183 
184  virtual Polynomial_Iterator * begin() const override;
185  virtual Polynomial_Iterator * end() const override;
186 
188 
189 
191  virtual void add_last(const Prime_Field_Element & a, const Monomial & t) override;
192 
193  virtual Polynomial_Linked_List * detach_head() override;
194 
196 
197 protected:
198 
200 
206  inline bool test_buffer(unsigned b, unsigned n) { return sizes[b] >= n; }
207 
214  void expand_buffer(unsigned b, unsigned n) {
215  if (mons[b] != nullptr) {
216  for (unsigned k = 0; k < sizes[b]; ++k)
217  mons[b][k].deinitialize();
218  free(mons[b]);
219  free(coeffs[b]);
220  }
221  unsigned new_size = 2*n;
222  sizes[b] = new_size;
223  mons[b] = (Monomial *)malloc(new_size*sizeof(Monomial));
224  for (unsigned k = 0; k < new_size; ++k)
225  mons[b][k].initialize_exponents(n);
226  coeffs[b] = (Prime_Field_Element *)malloc(new_size*sizeof(Prime_Field_Element));
227  }
228 
230 
233 
234 private:
235 
236  Monomial * mons[2];
237  Prime_Field_Element * coeffs[2];
238  unsigned sizes[2];
239  unsigned active_buffer;
240  unsigned head;
241  unsigned tail;
242 
243 };
244 
245 #endif
The general class of a polynomial.
Definition: polynomial.hpp:101
long long current_position
current position in the list
virtual bool canMoveRight() const override
Can this iterator move right, or would it fall off?
virtual bool canMoveLeft() const override
virtual void restart_iteration() override
This should move the iterator to the leading term.
const Abstract_Polynomial * p
the polynomial this points to
Definition: polynomial.hpp:272
Iterator over double-buffered polynomials.
unsigned tail
position of last monomial
virtual void set_currMonomial(const Monomial &t) override
change monomial in current position
virtual void set_currCoeff(const Prime_Field_Element &a) override
change coefficient in current position
Polynomials that need arithmetic typically descend from this class.
Definition: polynomial.hpp:305
virtual void moveRight() override
Moves right in the polynomial, to the next smaller monomial.
bool test_buffer(unsigned b, unsigned n)
true iff buffer b has space to hold n elements; expand other buffer if not.
Implementation of monomials.
Definition: monomial.hpp:69
Element of a field of prime characteristic.
Definition: fields.hpp:137
interface to a monomial ordering
Encapsulates information about a polynomial ring for easy access: ground field, number of indetermina...
virtual void moveLeft() override
Moves left in the polynomial, to the next larger monomial.
virtual bool fellOff() const override
A Mutable_Polynomial_Iterator allows one to modify the terms of a polynomial.
Definition: polynomial.hpp:283
void expand_buffer(unsigned b, unsigned n)
Expand buffer b to hold elements.
virtual const Prime_Field_Element & currCoeff() const override
Reports the coefficient at the current position.
Monomial * T
pointer to monomials
Polynomials represented as a doubly linked list.
Used to iterate through a polynomial.
Definition: polynomial.hpp:224
Prime_Field_Element * A
pointer to coefficients
virtual const Monomial & currMonomial() const override
Reports the monomial at the current position.
Polynomials implemented using double buffers.A double-buffered polynomial maintains at all times two ...
unsigned head
position of first monomial