4 #include <barvinok/util.h>
5 #include <barvinok/barvinok.h>
9 #include "omega/convert.h"
12 /* The input of this example program is a polytope in combined
13 * data and parameter space followed by two lines indicating
14 * the number of existential variables and parameters respectively.
15 * The first lines starts with "E ", followed by a number.
16 * The second lines starts with "P ", followed by a number.
17 * These two lines are (optionally) followed by the names of the parameters.
18 * The polytope is in PolyLib notation.
21 struct argp_option argp_options
[] = {
23 { "omega", 'o', 0, 0 },
28 { "series", 's', 0, 0 },
29 { "scarf", 'S', 0, 0 },
30 { "convert", 'c', 0, 0 },
31 { "floor", 'f', 0, 0 },
32 { "range-reduction", 'R', 0, 0 },
33 { "verify", 'T', 0, 0 },
34 { "print-all", 'A', 0, 0 },
35 { "min", 'm', "int", 0 },
36 { "max", 'M', "int", 0 },
37 { "range", 'r', "int", 0 },
42 struct barvinok_options
*options
;
56 error_t
parse_opt(int key
, char *arg
, struct argp_state
*state
)
58 struct arguments
*arguments
= (struct arguments
*)(state
->input
);
62 arguments
->series
= 1;
77 arguments
->convert
= 1;
83 arguments
->verify
= 1;
86 arguments
->print_all
= 1;
89 arguments
->m
= atoi(optarg
);
90 arguments
->verify
= 1;
93 arguments
->M
= atoi(optarg
);
94 arguments
->verify
= 1;
97 arguments
->M
= atoi(optarg
);
98 arguments
->m
= -arguments
->M
;
99 arguments
->verify
= 1;
102 return ARGP_ERR_UNKNOWN
;
109 Polyhedron
*Omega_simplify(Polyhedron
*P
,
110 unsigned exist
, unsigned nparam
, char **parms
)
114 Relation r
= Polyhedron2relation(P
, exist
, nparam
, parms
);
116 return relation2Domain(r
, varv
, paramv
);
119 Polyhedron
*Omega_simplify(Polyhedron
*P
,
120 unsigned exist
, unsigned nparam
, char **parms
)
126 /* define this to continue the test after first error found */
127 /* #define DONT_BREAK_ON_ERROR */
129 /* RANGE : normal range for evalutations (-RANGE -> RANGE) */
132 /* SRANGE : small range for evalutations */
135 /* if dimension >= BIDDIM, use SRANGE */
138 /* VSRANGE : very small range for evalutations */
141 /* if dimension >= VBIDDIM, use VSRANGE */
144 static Value min_val
, max_val
;
146 static char **params
;
150 static int check_poly(Polyhedron
*S
, Polyhedron
*C
, evalue
*EP
,
151 int exist
, int nparam
, int pos
, Value
*z
, int print_all
);
152 static void verify_results(Polyhedron
*P
, evalue
*EP
, int exist
, int nparam
,
153 int m
, int M
, int print_all
, unsigned MaxRays
);
155 int main(int argc
, char **argv
)
160 int exist
, nparam
, nvar
;
163 int print_solution
= 1;
165 struct arguments arguments
;
166 static struct argp_child argp_children
[] = {
167 { &barvinok_argp
, 0, 0, 0 },
170 static struct argp argp
= { argp_options
, parse_opt
, 0, 0, argp_children
};
171 struct barvinok_options
*options
= barvinok_options_new_with_defaults();
173 arguments
.options
= options
;
175 arguments
.convert
= 0;
179 arguments
.series
= 0;
181 arguments
.verify
= 0;
182 arguments
.print_all
= 0;
183 arguments
.m
= INT_MAX
;
184 arguments
.M
= INT_MIN
;
186 argp_parse(&argp
, argc
, argv
, ARGP_NO_EXIT
, 0, &arguments
);
188 if (arguments
.series
&& !arguments
.scarf
) {
190 "--series currently only available if --scarf is specified\n");
195 A
= Constraints2Polyhedron(MA
, options
->MaxRays
);
198 fgets(s
, 128, stdin
);
199 while ((*s
=='#') || (sscanf(s
, "E %d", &exist
)<1))
200 fgets(s
, 128, stdin
);
202 fgets(s
, 128, stdin
);
203 while ((*s
=='#') || (sscanf(s
, "P %d", &nparam
)<1))
204 fgets(s
, 128, stdin
);
206 /******* Read the options: initialize Min and Max ********/
207 if (A
->Dimension
>= VBIGDIM
)
209 else if (A
->Dimension
>= BIGDIM
)
213 if (arguments
.M
== INT_MIN
)
215 if (arguments
.m
== INT_MAX
)
218 if (arguments
.verify
&& arguments
.m
> arguments
.M
) {
219 fprintf(stderr
,"Nothing to do: min > max !\n");
222 if (arguments
.verify
)
225 if (print_solution
) {
226 Polyhedron_Print(stdout
, P_VALUE_FMT
, A
);
227 printf("exist: %d, nparam: %d\n", exist
, nparam
);
229 param_name
= Read_ParamNames(stdin
, nparam
);
230 nvar
= A
->Dimension
- exist
- nparam
;
231 if (arguments
.omega
) {
232 A
= Omega_simplify(A
, exist
, nparam
, param_name
);
234 exist
= A
->Dimension
- nvar
- nparam
;
236 if (arguments
.series
) {
238 barvinok_options
*options
= barvinok_options_new_with_defaults();
239 assert(arguments
.scarf
);
240 gf
= barvinok_enumerate_scarf_series(A
, exist
, nparam
, options
);
241 if (print_solution
) {
242 gf
->print(std::cout
, nparam
, param_name
);
248 if (arguments
.scarf
) {
249 barvinok_options
*options
= barvinok_options_new_with_defaults();
250 EP
= barvinok_enumerate_scarf(A
, exist
, nparam
, options
);
252 } else if (arguments
.pip
&& exist
> 0)
253 EP
= barvinok_enumerate_pip(A
, exist
, nparam
, options
->MaxRays
);
255 EP
= barvinok_enumerate_e(A
, exist
, nparam
, options
->MaxRays
);
259 evalue_range_reduction(EP
);
261 print_evalue(stdout
, EP
, param_name
);
262 if (arguments
.floor
) {
263 fprintf(stderr
, "WARNING: floor conversion not supported\n");
264 evalue_frac2floor2(EP
, 0);
266 print_evalue(stdout
, EP
, param_name
);
267 } else if (arguments
.convert
) {
268 evalue_mod2table(EP
, nparam
);
270 print_evalue(stdout
, EP
, param_name
);
272 if (arguments
.verify
)
273 verify_results(A
, EP
, exist
, nparam
, arguments
.m
, arguments
.M
,
274 arguments
.print_all
, options
->MaxRays
);
275 free_evalue_refs(EP
);
278 Free_ParamNames(param_name
, nparam
);
283 void verify_results(Polyhedron
*P
, evalue
*EP
, int exist
, int nparam
, int m
, int M
,
284 int print_all
, unsigned MaxRays
)
290 Polyhedron
*C
= Polyhedron_Project(P
, nparam
);
293 value_set_si(min_val
,m
);
294 value_set_si(max_val
,M
);
297 p
= (Value
*)malloc(sizeof(Value
) * (P
->Dimension
+2));
298 for(i
=0;i
<=P
->Dimension
;i
++) {
300 value_set_si(p
[i
],0);
303 value_set_si(p
[i
],1);
305 /* S = scanning list of polyhedra */
306 S
= Polyhedron_Scan(P
, C
, MaxRays
& POL_NO_DUAL
? 0 : MaxRays
);
309 if (C
->Dimension
> 0) {
310 value_subtract(tmp
,max_val
,min_val
);
311 if (VALUE_TO_INT(tmp
) > 80)
312 st
= 1+(VALUE_TO_INT(tmp
))/80;
315 for(i
=VALUE_TO_INT(min_val
);i
<=VALUE_TO_INT(max_val
);i
+=st
)
322 /******* CHECK NOW *********/
324 if(S
&& !check_poly(S
, C
, EP
, exist
, nparam
, 0, p
, print_all
)) {
325 fprintf(stderr
,"Check failed !\n");
332 for(i
=0;i
<=(P
->Dimension
+1);i
++)
340 /****************************************************/
341 /* function check_poly : */
342 /* scans the parameter space from min to max (all */
343 /* directions). Computes the number of points in */
344 /* the polytope using both methods, and compare them*/
345 /* returns 1 on success */
346 /****************************************************/
348 int check_poly(Polyhedron
*S
, Polyhedron
*C
, evalue
*EP
,
349 int exist
, int nparam
, int pos
, Value
*z
, int print_all
)
354 value_init(c
); value_init(tmp
);
358 /* Computes the ehrhart polynomial */
359 value_set_double(c
, compute_evalue(EP
,&z
[S
->Dimension
-nparam
+1])+.25);
360 /* if c=0 we may be out of context. */
361 /* scanning is useless in this case*/
362 if(!in_domain(C
,&z
[S
->Dimension
-nparam
+1])) {
370 value_print(stdout
,VALUE_FMT
,z
[S
->Dimension
-nparam
+1]);
371 for(k
=S
->Dimension
-nparam
+2;k
<=S
->Dimension
;++k
) {
373 value_print(stdout
,VALUE_FMT
,z
[k
]);
376 value_print(stdout
,VALUE_FMT
,c
);
380 /* Manually count the number of points */
381 count_points_e(1, S
, exist
, nparam
, z
, &tmp
);
383 printf(", count = ");
384 value_print(stdout
, P_VALUE_FMT
, tmp
);
388 if(value_ne(tmp
,c
)) {
391 fprintf(stderr
,"Error !\n");
392 fprintf(stderr
,"EP( ");
393 value_print(stderr
,VALUE_FMT
,z
[S
->Dimension
-nparam
+1]);
394 for(k
=S
->Dimension
-nparam
+2;k
<=S
->Dimension
;++k
) {
395 fprintf(stderr
,", ");
396 value_print(stderr
,VALUE_FMT
,z
[k
]);
398 fprintf(stderr
," ) should be ");
399 value_print(stderr
,VALUE_FMT
,tmp
);
400 fprintf(stderr
,", while EP eval gives ");
401 value_print(stderr
,VALUE_FMT
,c
);
402 fprintf(stderr
,".\n");
403 print_evalue(stderr
, EP
, params
);
404 #ifndef DONT_BREAK_ON_ERROR
405 value_clear(c
); value_clear(tmp
);
414 for(value_assign(tmp
,min_val
); value_le(tmp
,max_val
); value_increment(tmp
,tmp
)) {
416 k
= VALUE_TO_INT(tmp
);
417 if(!pos
&& !(k
%st
)) {
423 value_assign(z
[pos
+S
->Dimension
-nparam
+1],tmp
);
424 if(!check_poly(S
, C
, EP
, exist
, nparam
, pos
+1, z
, print_all
)) {
425 value_clear(c
); value_clear(tmp
);
429 value_clear(c
); value_clear(tmp
);