fix typo in comment
[barvinok.git] / barvinok_enumerate_e.cc
blobf76a65519b9ba9cae53de8223827204dc61fecd1
1 #include <unistd.h>
2 #include <stdlib.h>
3 #include <assert.h>
4 #include <barvinok/util.h>
5 #include <barvinok/barvinok.h>
6 #include "argp.h"
7 #include "progname.h"
8 #include "error.h"
9 #include "config.h"
10 #ifdef HAVE_OMEGA
11 #include "omega/convert.h"
12 #include "omega/count.h"
13 #endif
14 #include "skewed_genfun.h"
15 #include "verify.h"
16 #include "verif_ehrhart.h"
17 #include "verify_series.h"
18 #include "evalue_convert.h"
20 /* The input of this example program is a polytope in combined
21 * data and parameter space followed by two lines indicating
22 * the number of existential variables and parameters respectively.
23 * The first lines starts with "E ", followed by a number.
24 * The second lines starts with "P ", followed by a number.
25 * These two lines are (optionally) followed by the names of the parameters.
26 * The polytope is in PolyLib notation.
29 struct argp_option argp_options[] = {
30 { "omega", 'o', 0, 0 },
31 { "parker", 'P', 0, 0 },
32 { "pip", 'p', 0, 0 },
33 { "series", 's', 0, 0 },
34 { "series", 's', 0, 0, "compute rational generating function" },
35 { "explicit", 'e', 0, 0, "convert rgf to psp" },
36 { "scarf", 'S', 0, 0 },
37 { 0 }
40 struct arguments {
41 struct verify_options verify;
42 struct convert_options convert;
43 int omega;
44 int parker;
45 int pip;
46 int scarf;
47 int series;
48 int function;
51 error_t parse_opt(int key, char *arg, struct argp_state *state)
53 struct arguments *arguments = (struct arguments *)(state->input);
55 switch (key) {
56 case ARGP_KEY_INIT:
57 state->child_inputs[0] = arguments->verify.barvinok;
58 state->child_inputs[1] = &arguments->verify;
59 state->child_inputs[2] = &arguments->convert;
60 break;
61 case 'e':
62 arguments->function = 1;
63 /* fall through */
64 case 's':
65 arguments->series = 1;
66 break;
67 case 'S':
68 arguments->scarf = 1;
69 break;
70 case 'o':
71 #ifdef HAVE_OMEGA
72 arguments->omega = 1;
73 #else
74 error(0, 0, "--omega option not supported");
75 #endif
76 break;
77 case 'P':
78 #ifdef USE_PARKER
79 arguments->parker = 1;
80 #else
81 error(0, 0, "--parker option not supported");
82 #endif
83 break;
84 case 'p':
85 arguments->pip = 1;
86 break;
87 default:
88 return ARGP_ERR_UNKNOWN;
90 return 0;
93 #ifdef HAVE_OMEGA
95 Polyhedron *Omega_simplify(Polyhedron *P,
96 unsigned exist, unsigned nparam, const char **parms,
97 unsigned MaxRays)
99 varvector varv;
100 varvector paramv;
101 Relation r = Polyhedron2relation(P, exist, nparam, parms);
102 Polyhedron_Free(P);
103 return relation2Domain(r, varv, paramv, MaxRays);
105 #else
106 Polyhedron *Omega_simplify(Polyhedron *P,
107 unsigned exist, unsigned nparam, const char **parms,
108 unsigned MaxRays)
110 return P;
113 evalue *barvinok_enumerate_parker(Polyhedron *P,
114 unsigned exist, unsigned nparam,
115 unsigned MaxRays)
117 assert(0);
119 #endif
121 static void verify_results(Polyhedron *P, evalue *EP, gen_fun *gf,
122 int exist, int nparam,
123 arguments *options);
125 int main(int argc, char **argv)
127 Polyhedron *A;
128 Matrix *MA;
129 const char **param_name;
130 int exist, nparam, nvar;
131 char s[128];
132 evalue *EP = NULL;
133 gen_fun *gf = NULL;
134 int print_solution = 1;
135 struct arguments arguments;
136 static struct argp_child argp_children[] = {
137 { &barvinok_argp, 0, 0, 0 },
138 { &verify_argp, 0, "verification", BV_GRP_LAST+1 },
139 { &convert_argp, 0, "output conversion", BV_GRP_LAST+2 },
140 { 0 }
142 static struct argp argp = { argp_options, parse_opt, 0, 0, argp_children };
143 struct barvinok_options *options = barvinok_options_new_with_defaults();
145 arguments.verify.barvinok = options;
146 arguments.omega = 0;
147 arguments.parker = 0;
148 arguments.pip = 0;
149 arguments.scarf = 0;
150 arguments.series = 0;
151 arguments.function = 0;
153 set_program_name(argv[0]);
154 argp_parse(&argp, argc, argv, 0, 0, &arguments);
156 MA = Matrix_Read();
157 A = Constraints2Polyhedron(MA, options->MaxRays);
158 Matrix_Free(MA);
160 fgets(s, 128, stdin);
161 while ((*s=='#') || (sscanf(s, "E %d", &exist)<1))
162 fgets(s, 128, stdin);
164 fgets(s, 128, stdin);
165 while ((*s=='#') || (sscanf(s, "P %d", &nparam)<1))
166 fgets(s, 128, stdin);
168 /******* Read the options: initialize Min and Max ********/
170 if (arguments.verify.verify) {
171 verify_options_set_range(&arguments.verify, A->Dimension);
172 if (!options->verbose)
173 print_solution = 0;
176 if (print_solution && options->verbose) {
177 Polyhedron_Print(stdout, P_VALUE_FMT, A);
178 printf("exist: %d, nparam: %d\n", exist, nparam);
180 param_name = Read_ParamNames(stdin, nparam);
181 nvar = A->Dimension - exist - nparam;
182 if (arguments.omega) {
183 A = Omega_simplify(A, exist, nparam, param_name, options->MaxRays);
184 assert(!A->next);
185 exist = A->Dimension - nvar - nparam;
187 if (arguments.series) {
188 if (arguments.scarf)
189 gf = barvinok_enumerate_scarf_series(A, exist, nparam, options);
190 else
191 gf = barvinok_enumerate_e_series(A, exist, nparam, options);
192 if (print_solution) {
193 gf->print(std::cout, nparam, param_name);
194 puts("");
196 if (arguments.function) {
197 EP = *gf;
198 if (print_solution)
199 print_evalue(stdout, EP, param_name);
201 } else {
202 if (arguments.parker)
203 EP = barvinok_enumerate_parker(A, A->Dimension-nparam-exist,
204 nparam, options->MaxRays);
205 else if (arguments.scarf)
206 EP = barvinok_enumerate_scarf(A, exist, nparam, options);
207 else if (arguments.pip && exist > 0)
208 EP = barvinok_enumerate_pip_with_options(A, exist, nparam, options);
209 else
210 EP = barvinok_enumerate_e_with_options(A, exist, nparam, options);
211 reduce_evalue(EP);
212 if (evalue_convert(EP, &arguments.convert, options->verbose, nparam,
213 param_name))
214 print_solution = 0;
215 if (print_solution)
216 print_evalue(stdout, EP, param_name);
218 if (arguments.verify.verify) {
219 arguments.verify.params = param_name;
220 verify_results(A, EP, gf, exist, nparam, &arguments);
222 if (gf)
223 delete gf;
224 if (EP)
225 evalue_free(EP);
227 if (options->print_stats)
228 barvinok_stats_print(options->stats, stdout);
230 Free_ParamNames(param_name, nparam);
231 Polyhedron_Free(A);
232 barvinok_options_free(options);
233 return 0;
236 void verify_results(Polyhedron *P, evalue *EP, gen_fun *gf,
237 int exist, int nparam,
238 arguments *options)
240 int i;
241 int res = 0;
242 Vector *p;
243 Value tmp;
244 Polyhedron *S, *CS;
245 unsigned MaxRays = options->verify.barvinok->MaxRays;
246 Polyhedron *C = NULL;
247 value_init(tmp);
249 p = Vector_Alloc(P->Dimension+2);
250 value_set_si(p->p[P->Dimension+1], 1);
252 CS = check_poly_context_scan(P, &C, nparam, &options->verify);
253 if (!C)
254 C = Universe_Polyhedron(nparam);
256 /* S = scanning list of polyhedra */
257 S = Polyhedron_Scan(P, C, MaxRays & POL_NO_DUAL ? 0 : MaxRays);
259 check_poly_init(C, &options->verify);
261 /******* CHECK NOW *********/
262 if (S) {
263 if (!options->series || options->function) {
264 if (!check_poly_EP(S, CS, EP, exist, nparam, 0, p->p,
265 &options->verify))
266 res = -1;
267 } else {
268 skewed_gen_fun *sgf = new skewed_gen_fun(new gen_fun(gf));
269 if (!check_poly_gf(S, CS, sgf, exist, nparam, 0, p->p,
270 &options->verify))
271 res = -1;
272 delete sgf;
276 if (!options->verify.print_all)
277 printf( "\n" );
279 Vector_Free(p);
280 value_clear(tmp);
281 Domain_Free(S);
282 Polyhedron_Free(C);
283 if (CS)
284 Domain_Free(CS);