bernstein: add piecewise_lst::is_equal
[barvinok.git] / barvinok_enumerate_e.cc
blob28e856b9ed94b0f89827173824023cfd3684c705
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 #endif
13 #include "skewed_genfun.h"
14 #include "verify.h"
15 #include "verif_ehrhart.h"
16 #include "verify_series.h"
17 #include "evalue_convert.h"
19 /* The input of this example program is a polytope in combined
20 * data and parameter space followed by two lines indicating
21 * the number of existential variables and parameters respectively.
22 * The first lines starts with "E ", followed by a number.
23 * The second lines starts with "P ", followed by a number.
24 * These two lines are (optionally) followed by the names of the parameters.
25 * The polytope is in PolyLib notation.
28 #define PRINT_STATS (BV_OPT_LAST+1)
30 struct argp_option argp_options[] = {
31 { "omega", 'o', 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 { "print-stats", PRINT_STATS, 0, 0 },
38 { 0 }
41 struct arguments {
42 struct verify_options verify;
43 struct convert_options convert;
44 int omega;
45 int pip;
46 int scarf;
47 int series;
48 int function;
49 int print_stats;
52 error_t parse_opt(int key, char *arg, struct argp_state *state)
54 struct arguments *arguments = (struct arguments *)(state->input);
56 switch (key) {
57 case ARGP_KEY_INIT:
58 state->child_inputs[0] = arguments->verify.barvinok;
59 state->child_inputs[1] = &arguments->verify;
60 state->child_inputs[2] = &arguments->convert;
61 break;
62 case PRINT_STATS:
63 arguments->print_stats = 1;
64 break;
65 case 'e':
66 arguments->function = 1;
67 /* fall through */
68 case 's':
69 arguments->series = 1;
70 break;
71 case 'S':
72 arguments->scarf = 1;
73 break;
74 case 'o':
75 #ifdef HAVE_OMEGA
76 arguments->omega = 1;
77 #else
78 error(0, 0, "--omega option not supported");
79 #endif
80 break;
81 case 'p':
82 arguments->pip = 1;
83 break;
84 default:
85 return ARGP_ERR_UNKNOWN;
87 return 0;
90 #ifdef HAVE_OMEGA
92 Polyhedron *Omega_simplify(Polyhedron *P,
93 unsigned exist, unsigned nparam, char **parms)
95 varvector varv;
96 varvector paramv;
97 Relation r = Polyhedron2relation(P, exist, nparam, parms);
98 Polyhedron_Free(P);
99 return relation2Domain(r, varv, paramv);
101 #else
102 Polyhedron *Omega_simplify(Polyhedron *P,
103 unsigned exist, unsigned nparam, char **parms)
105 return P;
107 #endif
109 static void verify_results(Polyhedron *P, evalue *EP, gen_fun *gf,
110 int exist, int nparam,
111 arguments *options);
113 int main(int argc, char **argv)
115 Polyhedron *A;
116 Matrix *MA;
117 char **param_name;
118 int exist, nparam, nvar;
119 char s[128];
120 evalue *EP = NULL;
121 gen_fun *gf = NULL;
122 int print_solution = 1;
123 struct arguments arguments;
124 static struct argp_child argp_children[] = {
125 { &barvinok_argp, 0, 0, 0 },
126 { &verify_argp, 0, "verification", BV_GRP_LAST+1 },
127 { &convert_argp, 0, "output conversion", BV_GRP_LAST+2 },
128 { 0 }
130 static struct argp argp = { argp_options, parse_opt, 0, 0, argp_children };
131 struct barvinok_options *options = barvinok_options_new_with_defaults();
133 arguments.verify.barvinok = options;
134 arguments.omega = 0;
135 arguments.pip = 0;
136 arguments.scarf = 0;
137 arguments.series = 0;
138 arguments.function = 0;
139 arguments.print_stats = 0;
141 set_program_name(argv[0]);
142 argp_parse(&argp, argc, argv, 0, 0, &arguments);
144 if (arguments.series && !arguments.scarf) {
145 fprintf(stderr,
146 "--series currently only available if --scarf is specified\n");
147 exit(1);
150 MA = Matrix_Read();
151 A = Constraints2Polyhedron(MA, options->MaxRays);
152 Matrix_Free(MA);
154 fgets(s, 128, stdin);
155 while ((*s=='#') || (sscanf(s, "E %d", &exist)<1))
156 fgets(s, 128, stdin);
158 fgets(s, 128, stdin);
159 while ((*s=='#') || (sscanf(s, "P %d", &nparam)<1))
160 fgets(s, 128, stdin);
162 /******* Read the options: initialize Min and Max ********/
164 if (arguments.verify.verify) {
165 verify_options_set_range(&arguments.verify, A->Dimension);
166 if (!options->verbose)
167 print_solution = 0;
170 if (print_solution && options->verbose) {
171 Polyhedron_Print(stdout, P_VALUE_FMT, A);
172 printf("exist: %d, nparam: %d\n", exist, nparam);
174 param_name = Read_ParamNames(stdin, nparam);
175 nvar = A->Dimension - exist - nparam;
176 if (arguments.omega) {
177 A = Omega_simplify(A, exist, nparam, param_name);
178 assert(!A->next);
179 exist = A->Dimension - nvar - nparam;
181 if (arguments.series) {
182 assert(arguments.scarf);
183 gf = barvinok_enumerate_scarf_series(A, exist, nparam, options);
184 if (print_solution) {
185 gf->print(std::cout, nparam, param_name);
186 puts("");
188 if (arguments.function) {
189 EP = *gf;
190 if (print_solution)
191 print_evalue(stdout, EP, param_name);
193 } else {
194 if (arguments.scarf)
195 EP = barvinok_enumerate_scarf(A, exist, nparam, options);
196 else if (arguments.pip && exist > 0)
197 EP = barvinok_enumerate_pip_with_options(A, exist, nparam, options);
198 else
199 EP = barvinok_enumerate_e_with_options(A, exist, nparam, options);
200 reduce_evalue(EP);
201 if (evalue_convert(EP, &arguments.convert, options->verbose, nparam,
202 param_name))
203 print_solution = 0;
204 if (print_solution)
205 print_evalue(stdout, EP, param_name);
207 if (arguments.verify.verify) {
208 arguments.verify.params = param_name;
209 verify_results(A, EP, gf, exist, nparam, &arguments);
211 if (gf)
212 delete gf;
213 if (EP)
214 evalue_free(EP);
216 if (arguments.print_stats)
217 barvinok_stats_print(options->stats, stdout);
219 Free_ParamNames(param_name, nparam);
220 Polyhedron_Free(A);
221 barvinok_options_free(options);
222 return 0;
225 void verify_results(Polyhedron *P, evalue *EP, gen_fun *gf,
226 int exist, int nparam,
227 arguments *options)
229 int i;
230 int res = 0;
231 Vector *p;
232 Value tmp;
233 Polyhedron *S, *CS;
234 unsigned MaxRays = options->verify.barvinok->MaxRays;
235 Polyhedron *C = NULL;
236 value_init(tmp);
238 p = Vector_Alloc(P->Dimension+2);
239 value_set_si(p->p[P->Dimension+1], 1);
241 CS = check_poly_context_scan(P, &C, nparam, &options->verify);
242 if (!C)
243 C = Universe_Polyhedron(nparam);
245 /* S = scanning list of polyhedra */
246 S = Polyhedron_Scan(P, C, MaxRays & POL_NO_DUAL ? 0 : MaxRays);
248 check_poly_init(C, &options->verify);
250 /******* CHECK NOW *********/
251 if (S) {
252 if (!options->series || options->function) {
253 if (!check_poly_EP(S, CS, EP, exist, nparam, 0, p->p,
254 &options->verify))
255 res = -1;
256 } else {
257 skewed_gen_fun *sgf = new skewed_gen_fun(new gen_fun(gf));
258 if (!check_poly_gf(S, CS, sgf, exist, nparam, 0, p->p,
259 &options->verify))
260 res = -1;
261 delete sgf;
265 if (!options->verify.print_all)
266 printf( "\n" );
268 Vector_Free(p);
269 value_clear(tmp);
270 Domain_Free(S);
271 Polyhedron_Free(C);
272 if (CS)
273 Domain_Free(CS);