Gröbner basis project
Codebase for research into Gröbner basis computation
test_f4.cpp
1 /*****************************************************************************\
2 * This file is part of DynGB. *
3 * *
4 * DynGB is free software: you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation, either version 2 of the License, or *
7 * (at your option) any later version. *
8 * *
9 * Foobar is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU General Public License *
15 * along with DynGB. If not, see <http://www.gnu.org/licenses/>. *
16 \*****************************************************************************/
17 
18 #include <set>
19 #include <cstring>
20 #include <iostream>
21 
22 using std::set;
23 using std::cout; using std::endl;
24 
25 #include "system_constants.hpp"
26 
27 #include "goda.hpp"
28 #include "cyclic_n.hpp"
29 #include "polynomial.hpp"
30 #include "strategies.hpp"
31 #include "monomial_ordering.hpp"
32 #include "particular_orderings.hpp"
33 #include "polynomial_linked_list.hpp"
34 #include "f4_reduction.cpp"
35 
36 extern Monomial_Ordering * generic_grevlex_ptr;
41 
42 // Forward declarations
43 bool meaningful_arguments(int, char **, bool &, int &, int &);
44 
45 void give_help();
46 
47 int main(int argc, char *argv[]) {
48  bool homog;
49  int modulus, numvars;
50  if (not meaningful_arguments(argc, argv, homog, modulus, numvars)) {
51  give_help();
52  } else {
53  int true_numvars = (homog) ? numvars + 1 : numvars;
54  Prime_Field FF = Prime_Field(modulus);
55  // set up the basis
56  list<Abstract_Polynomial *> F = cyclic_n(numvars, FF, homog);
57  // message
58  cout << "Computing a Groebner basis for:\n";
59  for (Abstract_Polynomial * f : F)
60  cout << '\t' << *f << endl;
61  // compute basis
62  list<Constant_Polynomial *> G = f4_control(F);
63  // display basis
64  cout << G.size() << " polynomials in basis:\n";
65  /*for (list<Constant_Polynomial *>::const_iterator g = G.begin(); g != G.end(); ++g)
66  cout << '\t' << *(*g) << endl;*/
67  Polynomial_Ring * R = & (G.front()->base_ring());
68  auto mord = static_cast<const CachedWGrevlex_Ordering *>(
69  G.front()->monomial_ordering()
70  );
71  cout << G.size() << " leading monomials:\n";
72  for (Constant_Polynomial * g : G) {
73  cout << g->leading_monomial() << ", ";
74  //cout << *g << endl;
75  delete g;
76  }
77  cout << endl;
78  for (Abstract_Polynomial * f : F) delete f;
79  delete R;
80  if (mord != generic_grevlex_ptr) delete mord;
81  }
82  if (goda != nullptr) delete goda;
83  if (moda != nullptr) delete moda;
84  if (monoda != nullptr) delete monoda;
85  if (monododa != nullptr) delete monododa;
86  cout << "bye\n";
87 }
88 
89 enum order_flags { GENERIC_GREVLEX = 0, GREVLEX, LEX, WGREVLEX };
90 
91 bool meaningful_arguments(
92  int argc, char *argv[],
93  bool & homogeneous, int & modulus, int & numvars
94 ) {
95  modulus = 43;
96  homogeneous = false;
97  WT_TYPE * weights = nullptr;
98  unsigned int order_flag = 0;
99  bool good_args = (argc > 1);
100  if (good_args) {
101  for (int i = 1; good_args and i < argc; ++i) {
102  if (!strcmp(argv[i],"hom") or !strcmp(argv[i],"homog")
103  or !strcmp(argv[i],"homogeneous"))
104  homogeneous = true;
105  else {
106  int j = 0;
107  for (/* */; argv[i][j] != '=' and argv[i][j] != '\0'; ++j) { /* */ }
108  if (argv[i][j] != '=') {
109  good_args = false;
110  cout << "Options must have form <option>=<value>.\n";
111  }
112  else {
113  argv[i][j] = '\0';
114  if (!strcmp(argv[i],"n") or !strcmp(argv[i],"num")
115  or !strcmp(argv[i],"numvars")) {
116  numvars = atoi(&(argv[i][j+1]));
117  if (numvars < 3) {
118  good_args = false;
119  cout << "Invalid number of variables: must be at least 3.\n";
120  }
121  }
122  else if (!strcmp(argv[i],"m") or !strcmp(argv[i],"mod")
123  or !strcmp(argv[i],"modulus"))
124  {
125  modulus = atoi(&(argv[i][j+1]));
126  if (modulus < 2) {
127  good_args = false;
128  cout << "Invalid modulus; must be at least 2.\n";
129  }
130  }
131  else {
132  cout << "Unrecognized argument.\n"; good_args = false;
133  }
134  }
135  }
136  }
137  }
138  return good_args;
139 }
140 
141 void give_help() {
142  cout << "Call with options n=<num> m=<mod> [hom]\n";
143  cout << "You *must* specify <num> vars, an integer greater than 2.\n";
144  cout << "You can add optional <mod>ulus (please make it prime).\n";
145  cout << "The option <hom>ogenize will give you a homogenized ideal.\n";
146  cout << "So 'test_f4 n=6 m=43' would compute the Groebner basis\n";
147  cout << "of the Cyclic-n ideal in 6 variables, modulo 43.";
148 }
The general class of a polynomial.
Definition: polynomial.hpp:101
special memory pool allocator for Grevlex_Order_Data and WGrevlex_Order_Data
Definition: goda.hpp:67
Grading_Order_Data_Allocator< Monomial > * monoda
memory manager for monomials (not their exponents; see moda for that).
Definition: monomial.cpp:53
A Constant_Polynomial is a polynomial that should not change.
Grading_Order_Data_Allocator< EXP_TYPE > * moda
memory manager for monomial exponents
Definition: monomial.cpp:46
Information necessary for a field modulo a prime.
Definition: fields.hpp:49
interface to a monomial ordering
Grading_Order_Data_Allocator< WT_TYPE > * goda
Memory manager for graded ordering data.
Encapsulates information about a polynomial ring for easy access: ground field, number of indetermina...
list< Constant_Polynomial * > f4_control(const list< Abstract_Polynomial *> &F)
equivalent to buchberger(), but for Faugère’s F4 algorithm
the weighted grevlex ordering for a specified number of variables, with cached weights for each monom...
Grading_Order_Data_Allocator< Monomial_Node > * monododa
memory manager for Monomial_Node
list< Abstract_Polynomial * > cyclic_n(NVAR_TYPE n, Prime_Field &F, bool homog, Monomial_Ordering *mord=generic_grevlex_ptr)
generates the Cyclic- system
Definition: cyclic_n.hpp:57