-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalgorithm_buchberger_basic.hpp
More file actions
231 lines (211 loc) · 7.58 KB
/
Copy pathalgorithm_buchberger_basic.hpp
File metadata and controls
231 lines (211 loc) · 7.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#ifndef __ALGORITHM_BUCHBERGER_BASIC_HPP_
#define __ALGORITHM_BUCHBERGER_BASIC_HPP_
/*****************************************************************************\
* This file is part of DynGB. *
* *
* DynGB is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 2 of the License, or *
* (at your option) any later version. *
* *
* DynGB is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with DynGB. If not, see <http://www.gnu.org/licenses/>. *
\*****************************************************************************/
#include <set>
#include <list>
#include <iostream>
#include <iterator>
using std::list;
using std::cout; using std::endl;
using std::next;
using std::set;
using std::iterator; using std::forward_iterator_tag;
#include "system_constants.hpp"
#include "fields.hpp"
#include "monomial.hpp"
#include "polynomial.hpp"
#include "critical_pair.hpp"
#include "normal_strategy.hpp"
#include "polynomial_array.hpp"
#include "polynomial_geobucket.hpp"
#include "polynomial_linked_list.hpp"
#include "polynomial_double_buffered.hpp"
#include "sugar_strategy.hpp"
#include "weighted_sugar_strategy.hpp"
/**
@defgroup GBComputation Gröbner basis computation
@brief classes related directly to Gröbner basis computation
*/
/**
@brief Checks whether @p p is in danger of forming a Buchberger triple
with some pair listed in @p C. We need this to avoid deleting useful new pairs.
@ingroup GBComputation
@param p a critical pair of some sort
@param C a list of critical pairs
@return @c true iff @p p does not form a Buchberger triple with some pair in @p C
*/
template <typename T>
bool no_triplet(const T *p, const list<T *>C) {
bool result = true;
for (T * c : C) {
const Monomial & u = c->lcm();
bool degrees_smaller = true;
for (NVAR_TYPE i = 0; degrees_smaller and i < u.num_vars(); ++i)
degrees_smaller = degrees_smaller and u.degree(i) <= p->lcm_degree(i);
result = result and !degrees_smaller;
if (not result) break;
}
return result;
}
/**
@brief Checks if the lcm of @p t and @p u is like the lcm stored in @p p.
@param t a monomial
@param u a monomial
@param p a critical pair
@ingroup GBComputation
@return @c true iff \f$ lcm(t,u) \f$ is @p p’s lcm
*/
bool lcm_alike(
const Monomial & t,
const Monomial & u,
const Critical_Pair_Basic * p
);
/**
@brief Remove redundant polynomials from <c>G</c>.
@ingroup GBComputation
@details (A polynomial <i>g</i> in <i>G</i> is redundant when we can find <i>h</i>
in <i>G</i> whose leading monomial divides <i>g</i>’s leading monomial.)
@param G list of generators of a polynomial ideal, of which some may be redundant
@return a list of polynomials without the redundant elements
*/
list<Abstract_Polynomial *> reduce_basis(list<Abstract_Polynomial *>G);
/**
@brief Implementation of Gebauer-Moeller algorithm.
Based on description in Becker and Weispfenning (1993).
@ingroup GBComputation
@param P list of critical pairs
@param G current basis
@param r polynomial to add to basis (and to generate new pairs)
@param strategy how to sort pairs
*/
void gm_update(
list<Critical_Pair_Basic *> & P,
list<Abstract_Polynomial *> & G,
Abstract_Polynomial * r,
StrategyFlags strategy
);
/**
@brief A brief report on the number of critical pairs. If <c>verbose</c> is true,
also lists them.
@ingroup GBComputation
@param P a list of critical pairs
@param verbose whether to list the pairs as well as report their number
*/
template <typename T>
void report_critical_pairs(const list<T *>P, bool verbose = false) {
cout << P.size() << " critical pairs remaining\n";
if (verbose)
for (T * p : P)
cout << '\t' << *p << endl;
}
/**
@brief used to sort polynomials by leading monomial
@ingroup GBComputation
*/
struct smaller_lm {
/**
@brief returns @c true iff @p f’s leading monomial is
smaller than @c g’s
@param f a polynomial of some sort
@param g a polynomial of some sort
@return @c true iff @p f’s leading monomial is
smaller than @c g’s
*/
bool operator()(Abstract_Polynomial *f, Abstract_Polynomial *g) {
return f->leading_monomial() < g->leading_monomial();
}
};
/**
@brief checks that @p G is a Gröbner basis by verifying each s-polynomial
reduces to zero
@ingroup GBComputation
@param G list of generators of an ideal
@param strategy how to select critical pairs
@param max_degree check only pairs of this degree or less;
a value of 0 means to check every degree
*/
void check_correctness(
list<Abstract_Polynomial *>G,
StrategyFlags strategy = StrategyFlags::NORMAL_STRATEGY,
EXP_TYPE max_degree = 0
);
/**
@brief prints the number of polynomials in the basis
@param verbose print each polynomial’s leading term
@param very_verbose print each polynomial
*/
void report_basis(
list<Abstract_Polynomial *> G,
bool verbose=false,
bool very_verbose=false
);
/**
@brief gives a summary of information in @c p, with additional information
depending on @c strategy
*/
void report_front_pair(Critical_Pair_Basic *p, StrategyFlags strategy);
/**
@brief Implementation of Buchberger’s algorithm.
@ingroup GBComputation
@param F generators of the ideal whose Gröbner basis you’d like to
compute
@param rep which polynomial representation to use for the s-polynomials
(default is GEOBUCKETS)
@param strategy which strategy to use when selecting a critical pair
(default is SUGAR_STRATEGY)
@param strategy_weights if using a weighted sugar strategy, place an array
of weights here
@return list of polynomials in a Gröbner basis of @c F
*/
list<Abstract_Polynomial *> buchberger(
const list<Abstract_Polynomial *> &F,
SPolyCreationFlags rep = SPolyCreationFlags::GEOBUCKETS,
StrategyFlags strategy = StrategyFlags::SUGAR_STRATEGY,
WT_TYPE * strategy_weights = nullptr
);
/**
@brief Applies the strategy to find the “smallest” critical pair.
@ingroup GBComputation
@details Rather than sort each time, which typically shuffles
about a lot of pairs that will later be deleted,
we simply pass through the list once, find the smallest one,
and move it to the front.
@param P a list of critical pairs
*/
template <typename T>
void sort_pairs_by_strategy(list<T *> & P) {
// identify lowest lcm
typename list<T *>::iterator minkey_i = P.begin();
const Pair_Strategy_Data * minkey_data = (*minkey_i)->pair_key();
for (typename list<T *>::iterator pi = next(minkey_i);
pi != P.end();
++pi
)
{
const Pair_Strategy_Data * pairkey = (*pi)->pair_key();
if (*pairkey < *minkey_data) {
minkey_i = pi;
minkey_data = pairkey;
}
}
// move it to front
T * minkey = *minkey_i;
P.erase(minkey_i);
P.push_front(minkey);
}
#endif