Gröbner basis project
Codebase for research into Gröbner basis computation
reduction_support.hpp
1 #ifndef __REDUCTION_SUPPORT_HPP
2 #define __REDUCTION_SUPPORT_HPP
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 "system_constants.hpp"
22 #include "polynomial.hpp"
23 #include "monomial.hpp"
24 #include "fields.hpp"
25 
33 template <typename T>
35  Abstract_Polynomial * result = nullptr;
36  const Monomial & t = r->leading_monomial();
37  bool found = false;
38  // loop through G until we find a result, if one exists
39  for (Abstract_Polynomial * g : G)
40  {
41  if (
42  g->leading_monomial() | t
43  and (r->strategy() == nullptr or r->strategy()->valid_reduction(*r, *g)))
44  {
45  result = g;
46  found = true;
47  break;
48  }
49  }
50  return result;
51 }
52 
58 void top_reduce(Mutable_Polynomial *s, Abstract_Polynomial * g, int comm_id = 0);
59 
70 template <typename T>
71 void reduce_over_basis(Mutable_Polynomial **sp, const T & G, int comm_id=0) {
72  Abstract_Polynomial * g; // used to loop through G
73  Mutable_Polynomial * s = *sp; // s-poly
74  Mutable_Polynomial * r = s->zero_polynomial(); // remainder / residue
75  bool verbose = false;
76  bool very_verbose = false;
77  // continue reducing until s is zero
78  while (!s->is_zero()) {
79  if (verbose) cout << comm_id << " reducing " << s->leading_monomial() << "?\n";
80  if (very_verbose) s->println();
81  if ((g = find_reducer<T>(s, G))) {
82  if (verbose) cout << comm_id << " yes! by " << g->leading_monomial() << endl;
83  if (very_verbose) { cout << "yes! by "; g->println(); }
84  top_reduce(s, g);
85  if (very_verbose) {
86  cout << "\tresults in ";
87  if (s->is_zero())
88  cout << 0 << endl;
89  else
90  s->println();
91  if (not s->is_zero())
92  cout << "\tresults in " << s->leading_monomial() << endl;
93  }
94  } else {
95  if (verbose or very_verbose) cout << "no!\n";
96  // no reducer; move leading term to residue and continue
98  // t should already be smaller than what's already in r, so no danger
100  delete t;
101  }
102  }
103  //cout << comm_id << " wrapping up with " << *r << "\n";
104  // move the strategy from s to r
105  r->set_strategy(s->strategy());
106  s->set_strategy(nullptr);
107  delete s;
108  *sp = r;
109 }
110 
111 #endif
The general class of a polynomial.
Definition: polynomial.hpp:101
virtual bool is_zero() const =0
is this polynomial zero?
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()!
virtual Mutable_Polynomial * zero_polynomial() const =0
zero polynomial of this type
virtual bool valid_reduction(const Abstract_Polynomial &r, const Abstract_Polynomial &g) const
hook called while finding a reducer
Definition: strategies.cpp:43
void reduce_over_basis(Mutable_Polynomial **sp, const T &G, int comm_id=0)
Reduce the polynomial r over the basis G.
virtual void add_last(const Prime_Field_Element &, const Monomial &)=0
Attach a new monomial to the tail – check that it belongs at tail!
Polynomials that need arithmetic typically descend from this class.
Definition: polynomial.hpp:305
Implementation of monomials.
Definition: monomial.hpp:69
virtual Mutable_Polynomial * detach_head()=0
Remove and return the head.
void top_reduce(Mutable_Polynomial *s, Abstract_Polynomial *g, int comm_id)
reduce the polynomial **sp by *g
void set_strategy(Poly_Strategy_Data *psd)
sets the polynomial’s strategy to psd
Definition: polynomial.cpp:36
Abstract_Polynomial * find_reducer(Abstract_Polynomial *r, const T &G)
Find a polynomial in the basis G that can reduce r.
virtual Poly_Strategy_Data * strategy() const
strategy related information
Definition: polynomial.hpp:144