1 #ifndef __POLYNOMIAL_DOUBLE_BUFFERED_CPP_ 2 #define __POLYNOMIAL_DOUBLE_BUFFERED_CPP_ 21 #include "polynomial_double_buffered.hpp" 23 DB_Polynomial_Iterator::~DB_Polynomial_Iterator() { }
32 return current_position < head or current_position >=
tail;
59 DB_Polynomial_Iterator::DB_Polynomial_Iterator(
63 A = f->coeffs[f->active_buffer];
64 T = f->mons[f->active_buffer];
71 Double_Buffered_Polynomial::Double_Buffered_Polynomial(
Polynomial_Ring & R,
74 mons[0] = mons[1] =
nullptr;
75 coeffs[0] = coeffs[1] =
nullptr;
76 sizes[0] = sizes[1] = 0;
82 Double_Buffered_Polynomial::Double_Buffered_Polynomial(
85 mons[0] = mons[1] =
nullptr;
86 coeffs[0] = coeffs[1] =
nullptr;
87 sizes[0] = sizes[1] = 0;
91 expand_buffer(0,
tail);
103 Double_Buffered_Polynomial::~Double_Buffered_Polynomial() {
104 if (mons[0] !=
nullptr) {
105 for (
unsigned i = 0; i < sizes[0]; ++i)
106 mons[0][i].deinitialize();
110 if (mons[1] !=
nullptr) {
111 for (
unsigned i = 0; i < sizes[1]; ++i)
112 mons[1][i].deinitialize();
119 return mons[active_buffer][
head];
124 return coeffs[active_buffer][
head];
148 for (
unsigned i =
head; i <
tail; ++i)
149 mons[active_buffer][i].set_monomial_ordering(order);
158 unsigned i = p->active_buffer;
160 for (
unsigned j =
head; j <
tail; ++j)
169 unsigned i = p->active_buffer;
171 for (
unsigned j =
head; j <
tail; ++j)
180 unsigned i = active_buffer;
181 unsigned j = 1 - active_buffer;
183 unsigned m = length() + p.
length();
184 if (not test_buffer(j, m))
191 mons[j][k] = mons[i][l];
192 coeffs[j][k] = coeffs[i][l] + pi->
currCoeff();
200 mons[j][k] = mons[i][l];
201 coeffs[j][k] = coeffs[i][l];
204 if (not coeffs[j][k].is_zero())
214 mons[j][k] = mons[i][l];
215 coeffs[j][k] = coeffs[i][l];
231 unsigned i = active_buffer;
232 unsigned j = 1 - active_buffer;
234 unsigned m = length() + p.
length();
235 if (not test_buffer(j, m))
242 mons[j][k] = mons[i][l];
243 coeffs[j][k] = coeffs[i][l] - pi->
currCoeff();
251 mons[j][k] = mons[i][l];
252 coeffs[j][k] = coeffs[i][l];
255 if (not coeffs[j][k].is_zero())
265 mons[j][k] = mons[i][l];
266 coeffs[j][k] = coeffs[i][l];
286 bool true_multiple = not u.
is_one();
287 unsigned i = active_buffer;
288 unsigned j = 1 - active_buffer;
290 unsigned m = length() + p.
length();
291 if (not test_buffer(j, m))
298 mons[j][k] = mons[i][l];
303 coeffs[j][k] += coeffs[i][l];
306 }
else if (mons[i][l].larger_than_multiple(pi->
currMonomial(), u)) {
307 mons[j][k] = mons[i][l];
308 coeffs[j][k] = coeffs[i][l];
320 if (not coeffs[j][k].is_zero())
334 mons[j][k] = mons[i][l];
335 coeffs[j][k] = coeffs[i][l];
353 for (
int i = 0; i <
tail; ++i) {
357 for (; j > 0 and M[i] > M[j-1]; --j) {
392 unsigned i = active_buffer;
394 if (
tail + 1 >= sizes[i]) {
397 if (not test_buffer(j, length() + 1))
398 expand_buffer(j, length() + 1);
404 unsigned k =
head = 0;
405 for (
unsigned l =
head; l <
tail; ++l, ++k) {
410 active_buffer = i = j;
420 base_ring(), coeffs[active_buffer][
head], mons[active_buffer][head]);
The general class of a polynomial.
virtual bool can_reduce(Abstract_Polynomial &other) const override
can this reduce other?
virtual Double_Buffered_Polynomial * zero_polynomial() const override
zero polynomial of this type
virtual Double_Buffered_Polynomial * scalar_multiple(const Prime_Field_Element &c) const override
multiple of this and c
const Monomial_Ordering * monomial_ordering() const
reports leading monomial’s monomial ordering
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 Polynomial_Iterator * new_iterator() const =0
An iterator that poses no risk of modifying the polynomial.
virtual DB_Polynomial_Iterator * new_iterator() const override
An iterator that poses no risk of modifying the polynomial.
virtual void moveRight()=0
Moves right in the polynomial, to the next smaller monomial.
virtual const Prime_Field_Element & currCoeff() const =0
Reports the coefficient at the current position.
virtual void restart_iteration() override
This should move the iterator to the leading term.
virtual Monomial & leading_monomial() const =0
leading monomial – call after sort_by_order()!
virtual Prime_Field_Element leading_coefficient() const override
leading coefficient – call after sort_by_order()!
const Abstract_Polynomial * p
the polynomial this points to
void negate()
“Negates” this.
virtual Double_Buffered_Polynomial * monomial_multiple(const Monomial &t) const override
multiple of this and u
virtual Polynomial_Linked_List * detach_head() override
Remove and return the head.
Iterator over double-buffered polynomials.
bool is_one() const
all exponents 0?
unsigned tail
position of last monomial
virtual void set_currMonomial(const Monomial &t) override
change monomial in current position
virtual unsigned length() const override
number of monomials
virtual void sort_by_order() override
sort according to the leading monomial’s ordering
virtual DB_Polynomial_Iterator * new_mutable_iterator() override
An iterator that may modify the current position.
virtual bool is_zero() const override
is this polynomial zero?
virtual void set_currCoeff(const Prime_Field_Element &a) override
change coefficient in current position
virtual bool fellOff() const =0
Polynomials that need arithmetic typically descend from this class.
virtual void moveRight() override
Moves right in the polynomial, to the next smaller monomial.
Implementation of monomials.
Element of a field of prime characteristic.
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 void add_last(const Prime_Field_Element &a, const Monomial &t) override
Attach a new monomial to the tail – check that it belongs at tail!
virtual bool fellOff() const override
virtual Monomial & leading_monomial() const override
leading monomial – call after sort_by_order()!
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.
virtual void add_polynomial_multiple(const Prime_Field_Element &a, const Monomial &u, const Abstract_Polynomial &p, bool subtract) override
add monomial multiple of other
Used to iterate through a polynomial.
virtual const Monomial & currMonomial() const =0
Reports the monomial at the current position.
Prime_Field_Element * A
pointer to coefficients
virtual const Monomial & currMonomial() const override
Reports the monomial at the current position.
virtual Mutable_Polynomial & operator-=(const Abstract_Polynomial &p) override
subtract another polynomial
Polynomials implemented using double buffers.A double-buffered polynomial maintains at all times two ...
virtual void set_monomial_ordering(const Monomial_Ordering *order, bool sort_anew=true) override
set the monomial ordering and sort the polynomials (optionally, but by default)
virtual unsigned length() const =0
number of monomials
Polynomial_Ring & base_ring() const
ring in which this polynomial resides
virtual Mutable_Polynomial & operator+=(const Abstract_Polynomial &p) override
add another polynomial
unsigned head
position of first monomial