evalue.c: reorder_terms: fix typo
[barvinok.git] / barvinok_enumerate_e.cc
blob368f1c229d656bb8d5bc4ab0cbcf4d7d2c1a2577
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 "verify.h"
14 #include "verif_ehrhart.h"
15 #include "evalue_convert.h"
17 /* The input of this example program is a polytope in combined
18 * data and parameter space followed by two lines indicating
19 * the number of existential variables and parameters respectively.
20 * The first lines starts with "E ", followed by a number.
21 * The second lines starts with "P ", followed by a number.
22 * These two lines are (optionally) followed by the names of the parameters.
23 * The polytope is in PolyLib notation.
26 struct argp_option argp_options[] = {
27 { "omega", 'o', 0, 0 },
28 { "pip", 'p', 0, 0 },
29 { "series", 's', 0, 0 },
30 { "scarf", 'S', 0, 0 },
31 { "verbose", 'v' },
32 { 0 }
35 struct arguments {
36 struct verify_options verify;
37 struct convert_options convert;
38 int omega;
39 int pip;
40 int scarf;
41 int series;
42 int verbose;
45 error_t parse_opt(int key, char *arg, struct argp_state *state)
47 struct arguments *arguments = (struct arguments *)(state->input);
49 switch (key) {
50 case ARGP_KEY_INIT:
51 state->child_inputs[0] = arguments->verify.barvinok;
52 state->child_inputs[1] = &arguments->verify;
53 state->child_inputs[2] = &arguments->convert;
54 break;
55 case 's':
56 arguments->series = 1;
57 break;
58 case 'S':
59 arguments->scarf = 1;
60 break;
61 case 'o':
62 #ifdef HAVE_OMEGA
63 arguments->omega = 1;
64 #else
65 error(0, 0, "--omega option not supported");
66 #endif
67 break;
68 case 'p':
69 #ifdef HAVE_PIPLIB
70 arguments->pip = 1;
71 #else
72 error(0, 0, "--pip option not supported");
73 #endif
74 break;
75 case 'v':
76 arguments->verbose = 1;
77 break;
78 default:
79 return ARGP_ERR_UNKNOWN;
81 return 0;
84 #ifdef HAVE_OMEGA
86 Polyhedron *Omega_simplify(Polyhedron *P,
87 unsigned exist, unsigned nparam, char **parms)
89 varvector varv;
90 varvector paramv;
91 Relation r = Polyhedron2relation(P, exist, nparam, parms);
92 Polyhedron_Free(P);
93 return relation2Domain(r, varv, paramv);
95 #else
96 Polyhedron *Omega_simplify(Polyhedron *P,
97 unsigned exist, unsigned nparam, char **parms)
99 return P;
101 #endif
103 static void verify_results(Polyhedron *P, evalue *EP, int exist, int nparam,
104 verify_options *options);
106 int main(int argc, char **argv)
108 Polyhedron *A;
109 Matrix *MA;
110 char **param_name;
111 int exist, nparam, nvar;
112 char s[128];
113 evalue *EP;
114 int print_solution = 1;
115 struct arguments arguments;
116 static struct argp_child argp_children[] = {
117 { &barvinok_argp, 0, 0, 0 },
118 { &verify_argp, 0, "verification", BV_GRP_LAST+1 },
119 { &convert_argp, 0, "output conversion", BV_GRP_LAST+2 },
120 { 0 }
122 static struct argp argp = { argp_options, parse_opt, 0, 0, argp_children };
123 struct barvinok_options *options = barvinok_options_new_with_defaults();
125 arguments.verify.barvinok = options;
126 arguments.omega = 0;
127 arguments.pip = 0;
128 arguments.scarf = 0;
129 arguments.series = 0;
130 arguments.verbose = 0;
132 set_program_name(argv[0]);
133 argp_parse(&argp, argc, argv, 0, 0, &arguments);
135 if (arguments.series && !arguments.scarf) {
136 fprintf(stderr,
137 "--series currently only available if --scarf is specified\n");
138 exit(1);
141 MA = Matrix_Read();
142 A = Constraints2Polyhedron(MA, options->MaxRays);
143 Matrix_Free(MA);
145 fgets(s, 128, stdin);
146 while ((*s=='#') || (sscanf(s, "E %d", &exist)<1))
147 fgets(s, 128, stdin);
149 fgets(s, 128, stdin);
150 while ((*s=='#') || (sscanf(s, "P %d", &nparam)<1))
151 fgets(s, 128, stdin);
153 /******* Read the options: initialize Min and Max ********/
155 if (arguments.verify.verify) {
156 verify_options_set_range(&arguments.verify, A->Dimension);
157 if (!arguments.verbose)
158 print_solution = 0;
161 if (print_solution && arguments.verbose) {
162 Polyhedron_Print(stdout, P_VALUE_FMT, A);
163 printf("exist: %d, nparam: %d\n", exist, nparam);
165 param_name = Read_ParamNames(stdin, nparam);
166 nvar = A->Dimension - exist - nparam;
167 if (arguments.omega) {
168 A = Omega_simplify(A, exist, nparam, param_name);
169 assert(!A->next);
170 exist = A->Dimension - nvar - nparam;
172 if (arguments.series) {
173 gen_fun *gf;
174 assert(arguments.scarf);
175 gf = barvinok_enumerate_scarf_series(A, exist, nparam, options);
176 if (print_solution) {
177 gf->print(std::cout, nparam, param_name);
178 puts("");
180 delete gf;
181 } else {
182 if (arguments.scarf)
183 EP = barvinok_enumerate_scarf(A, exist, nparam, options);
184 else if (arguments.pip && exist > 0)
185 EP = barvinok_enumerate_pip_with_options(A, exist, nparam, options);
186 else
187 EP = barvinok_enumerate_e_with_options(A, exist, nparam, options);
188 reduce_evalue(EP);
189 if (evalue_convert(EP, &arguments.convert, arguments.verbose, nparam,
190 param_name))
191 print_solution = 0;
192 if (print_solution)
193 print_evalue(stdout, EP, param_name);
194 if (arguments.verify.verify) {
195 arguments.verify.params = param_name;
196 verify_results(A, EP, exist, nparam, &arguments.verify);
198 free_evalue_refs(EP);
199 free(EP);
201 Free_ParamNames(param_name, nparam);
202 Polyhedron_Free(A);
203 barvinok_options_free(options);
204 return 0;
207 void verify_results(Polyhedron *P, evalue *EP, int exist, int nparam,
208 verify_options *options)
210 int i;
211 int res;
212 Value *p, tmp;
213 Polyhedron *S, *CS;
214 unsigned MaxRays = options->barvinok->MaxRays;
215 Polyhedron *C = NULL;
216 value_init(tmp);
218 p = (Value *)malloc(sizeof(Value) * (P->Dimension+2));
219 for(i=0;i<=P->Dimension;i++) {
220 value_init(p[i]);
221 value_set_si(p[i],0);
223 value_init(p[i]);
224 value_set_si(p[i],1);
226 CS = check_poly_context_scan(P, &C, nparam, options);
228 /* S = scanning list of polyhedra */
229 S = Polyhedron_Scan(P, C, MaxRays & POL_NO_DUAL ? 0 : MaxRays);
231 check_poly_init(C, options);
233 /******* CHECK NOW *********/
234 res = 0;
235 if (S && !check_poly_EP(S, CS, EP, exist, nparam, 0, p, options)) {
236 fprintf(stderr,"Check failed !\n");
237 res = -1;
240 if (!options->print_all)
241 printf( "\n" );
243 for(i=0;i<=(P->Dimension+1);i++)
244 value_clear(p[i]);
245 free(p);
246 value_clear(tmp);
247 Domain_Free(S);
248 Polyhedron_Free(C);
249 if (CS)
250 Domain_Free(CS);