Gröbner basis project
Codebase for research into Gröbner basis computation
polynomial.cpp
1 #ifndef __POLYNOMIAL_CPP_
2 #define __POLYNOMIAL_CPP_
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 "polynomial.hpp"
22 
24 
26 
27 Prime_Field & Abstract_Polynomial::ground_field() { return R.ground_field(); }
28 
30  return R.number_of_variables();
31 }
32 
34 { return leading_monomial() | other.leading_monomial(); }
35 
37 
38 void Abstract_Polynomial::print(ostream & os) const { os << *this; }
39 
40 void Abstract_Polynomial::println(ostream & os) const {
41  print(); os << endl;
42 }
43 
44 ostream & operator << (ostream & os, const Abstract_Polynomial & p) {
45  if (p.is_zero())
46  os << "0 ";
47  else {
49  while (!pi->fellOff()) {
50  if (pi->currMonomial().is_one() or not pi->currCoeff().is_one())
51  os << pi->currCoeff() << ' ';
52  //os << pi->currMonomial();
53  if (not pi->currMonomial().is_one())
54  pi->currMonomial().print(false, os, p.base_ring().name_list());
55  pi->moveRight();
56  if (!pi->fellOff()) os << " + ";
57  }
58  delete pi;
59  }
60  return os;
61 }
62 
64 
66  Mutable_Polynomial_Iterator * pi = new_mutable_iterator();
67  while (!pi->fellOff()) {
68  Prime_Field_Element b = pi->currCoeff() * a;
69  pi->set_currCoeff(b);
70  pi->moveRight();
71  }
72  delete pi;
73 }
74 
76  Mutable_Polynomial_Iterator * pi = new_mutable_iterator();
77  while (!pi->fellOff()) {
78  Monomial b = pi->currMonomial();
79  b *= t;
80  pi->set_currMonomial(b);
81  pi->moveRight();
82  }
83  delete pi;
84 }
85 
87  while (p.can_reduce(*this)) {
88  Monomial u = leading_monomial();
89  u /= p.leading_monomial();
90  Prime_Field_Element a = leading_coefficient();
91  a *= p.leading_coefficient().inverse();
92  add_polynomial_multiple(a, u, p, true);
93  }
94 }
95 
97  DEG_TYPE d = 0;
98  Polynomial_Iterator * pi = new_iterator();
99  while (not pi->fellOff()) {
100  if (pi->currMonomial().total_degree() > d)
101  d = pi->currMonomial().total_degree();
102  pi->moveRight();
103  }
104  delete pi;
105  return d;
106 }
107 
108 DEG_TYPE Abstract_Polynomial::weighted_degree(const WT_TYPE * w) const {
109  if (w == nullptr) return standard_degree();
110  else {
111  DEG_TYPE d = 0;
112  Polynomial_Iterator * pi = new_iterator();
113  while (not pi->fellOff()) {
114  if (pi->currMonomial().weighted_degree(w) > d)
115  d = pi->currMonomial().weighted_degree(w);
116  pi->moveRight();
117  }
118  delete pi;
119  return d;
120  }
121 }
122 
123 #endif
DEG_TYPE total_degree(NVAR_TYPE m=0) const
Sum of exponents of the first m variables.
Definition: monomial.cpp:190
The general class of a polynomial.
Definition: polynomial.hpp:101
virtual bool is_zero() const =0
is this polynomial zero?
DEG_TYPE weighted_degree(const WT_TYPE *weights, NVAR_TYPE m=0) const
Definition: monomial.cpp:199
virtual bool can_reduce(Abstract_Polynomial &other) const
can this reduce other?
Definition: polynomial.cpp:33
polynomial-related strategy data
Definition: strategies.hpp:49
virtual Polynomial_Iterator * new_iterator() const =0
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 DEG_TYPE standard_degree() const
maximum sum of exponents for any monomial
Definition: polynomial.cpp:96
Information necessary for a field modulo a prime.
Definition: fields.hpp:49
virtual Monomial & leading_monomial() const =0
leading monomial – call after sort_by_order()!
virtual Prime_Field_Element leading_coefficient() const =0
leading coefficient – call after sort_by_order()!
const Abstract_Polynomial * p
the polynomial this points to
Definition: polynomial.hpp:272
Prime_Field & ground_field()
ground field – all coefficients should be in this field
Definition: polynomial.cpp:27
void print(bool=true, ostream &=cout, const string *names=nullptr) const
prints the monomial to the give stream with the given names; adds a newline if the boolean is true ...
Definition: monomial.cpp:439
bool is_one() const
all exponents 0?
Definition: monomial.cpp:176
virtual DEG_TYPE weighted_degree(const WT_TYPE *w=nullptr) const
Definition: polynomial.cpp:108
virtual void reduce_by(const Abstract_Polynomial &p)
reduce by until no further reduction possible
Definition: polynomial.cpp:86
virtual void set_currCoeff(const Prime_Field_Element &)=0
change coefficient in current position
virtual void multiply_by_monomial(const Monomial &t)
multiply by monomial
Definition: polynomial.cpp:75
virtual bool fellOff() const =0
virtual ~Mutable_Polynomial()=0
destructor
Definition: polynomial.cpp:63
Implementation of monomials.
Definition: monomial.hpp:69
Element of a field of prime characteristic.
Definition: fields.hpp:137
bool is_one() const
Is this the multiplicative identity?
Definition: fields.cpp:88
virtual void set_currMonomial(const Monomial &)=0
change monomial in current position
unsigned number_of_variables() const
number of variables – all monomials should agree with this (though it is never tested by the class) ...
Definition: polynomial.cpp:29
Encapsulates information about a polynomial ring for easy access: ground field, number of indetermina...
virtual void multiply_by_scalar(const Prime_Field_Element &a)
multiply by scalar
Definition: polynomial.cpp:65
A Mutable_Polynomial_Iterator allows one to modify the terms of a polynomial.
Definition: polynomial.hpp:283
virtual ~Polynomial_Iterator()=0
needed to avoid undefined behavior when disposing
Definition: polynomial.cpp:23
void set_strategy(Poly_Strategy_Data *psd)
sets the polynomial’s strategy to psd
Definition: polynomial.cpp:36
Used to iterate through a polynomial.
Definition: polynomial.hpp:224
COEF_TYPE inverse() const
Returns the multiplicative inverse of this.
Definition: fields.cpp:84
virtual const Monomial & currMonomial() const =0
Reports the monomial at the current position.
virtual const string * name_list() const
names of all the variabiles
Polynomial_Ring & base_ring() const
ring in which this polynomial resides
Definition: polynomial.cpp:25