3 #include <polylib/polylibgmp.h>
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 #ifdef HAVE_GROWING_CHERNIKOVA
22 #define MAXRAYS POL_NO_DUAL
28 #define getopt_long(a,b,c,d,e) getopt(a,b,c)
31 struct option options
[] = {
33 { "omega", no_argument
, 0, 'o' },
36 { "pip", no_argument
, 0, 'p' },
38 { "series", no_argument
, 0, 's' },
39 { "scarf", no_argument
, 0, 'S' },
40 { "convert", no_argument
, 0, 'c' },
41 { "floor", no_argument
, 0, 'f' },
42 { "range-reduction", no_argument
, 0, 'R' },
43 { "verify", no_argument
, 0, 'T' },
44 { "print-all", no_argument
, 0, 'A' },
45 { "min", required_argument
, 0, 'm' },
46 { "max", required_argument
, 0, 'M' },
47 { "range", required_argument
, 0, 'r' },
48 { "version", no_argument
, 0, 'V' },
54 #define PIPLIB_OPT "p"
62 Polyhedron
*Omega_simplify(Polyhedron
*P
,
63 unsigned exist
, unsigned nparam
, char **parms
)
67 Relation r
= Polyhedron2relation(P
, exist
, nparam
, parms
);
69 return relation2Domain(r
, varv
, paramv
);
73 Polyhedron
*Omega_simplify(Polyhedron
*P
,
74 unsigned exist
, unsigned nparam
, char **parms
)
80 /* define this to continue the test after first error found */
81 /* #define DONT_BREAK_ON_ERROR */
83 /* RANGE : normal range for evalutations (-RANGE -> RANGE) */
86 /* SRANGE : small range for evalutations */
89 /* if dimension >= BIDDIM, use SRANGE */
92 /* VSRANGE : very small range for evalutations */
95 /* if dimension >= VBIDDIM, use VSRANGE */
98 static Value min_val
, max_val
;
100 static char **params
;
104 static int check_poly(Polyhedron
*S
, Polyhedron
*C
, evalue
*EP
,
105 int exist
, int nparam
, int pos
, Value
*z
, int print_all
);
106 static void verify_results(Polyhedron
*P
, evalue
*EP
, int exist
, int nparam
,
107 int m
, int M
, int print_all
);
109 int main(int argc
, char **argv
)
114 int exist
, nparam
, nvar
;
127 int m
= INT_MAX
, M
= INT_MIN
, r
;
128 int print_solution
= 1;
130 while ((c
= getopt_long(argc
, argv
,
131 OMEGA_OPT PIPLIB_OPT
"sSfcRTAm:M:r:V", options
, &ind
)) != -1) {
174 printf(barvinok_version());
180 if (series
&& !scarf
) {
182 "--series currently only available if --scarf is specified\n");
187 A
= Constraints2Polyhedron(MA
, MAXRAYS
);
190 fgets(s
, 128, stdin
);
191 while ((*s
=='#') || (sscanf(s
, "E %d", &exist
)<1))
192 fgets(s
, 128, stdin
);
194 fgets(s
, 128, stdin
);
195 while ((*s
=='#') || (sscanf(s
, "P %d", &nparam
)<1))
196 fgets(s
, 128, stdin
);
198 /******* Read the options: initialize Min and Max ********/
199 if (A
->Dimension
>= VBIGDIM
)
201 else if (A
->Dimension
>= BIGDIM
)
210 if (verify
&& m
> M
) {
211 fprintf(stderr
,"Nothing to do: min > max !\n");
217 if (print_solution
) {
218 Polyhedron_Print(stdout
, P_VALUE_FMT
, A
);
219 printf("exist: %d, nparam: %d\n", exist
, nparam
);
221 param_name
= Read_ParamNames(stdin
, nparam
);
222 nvar
= A
->Dimension
- exist
- nparam
;
224 A
= Omega_simplify(A
, exist
, nparam
, param_name
);
226 exist
= A
->Dimension
- nvar
- nparam
;
231 gf
= barvinok_enumerate_scarf_series(A
, exist
, nparam
, MAXRAYS
);
232 if (print_solution
) {
233 gf
->print(std::cout
, nparam
, param_name
);
239 EP
= barvinok_enumerate_scarf(A
, exist
, nparam
, MAXRAYS
);
240 } else if (pip
&& exist
> 0)
241 EP
= barvinok_enumerate_pip(A
, exist
, nparam
, MAXRAYS
);
243 EP
= barvinok_enumerate_e(A
, exist
, nparam
, MAXRAYS
);
247 evalue_range_reduction(EP
);
249 print_evalue(stdout
, EP
, param_name
);
251 fprintf(stderr
, "WARNING: floor conversion not supported\n");
252 evalue_frac2floor(EP
);
254 print_evalue(stdout
, EP
, param_name
);
255 } else if (convert
) {
256 evalue_mod2table(EP
, nparam
);
258 print_evalue(stdout
, EP
, param_name
);
261 verify_results(A
, EP
, exist
, nparam
, m
, M
, print_all
);
262 free_evalue_refs(EP
);
265 Free_ParamNames(param_name
, nparam
);
270 void verify_results(Polyhedron
*P
, evalue
*EP
, int exist
, int nparam
, int m
, int M
,
277 Polyhedron
*C
= Polyhedron_Project(P
, nparam
);
280 value_set_si(min_val
,m
);
281 value_set_si(max_val
,M
);
284 p
= (Value
*)malloc(sizeof(Value
) * (P
->Dimension
+2));
285 for(i
=0;i
<=P
->Dimension
;i
++) {
287 value_set_si(p
[i
],0);
290 value_set_si(p
[i
],1);
292 /* S = scanning list of polyhedra */
293 S
= Polyhedron_Scan(P
, C
, MAXRAYS
& POL_NO_DUAL
? 0 : MAXRAYS
);
296 if (C
->Dimension
> 0) {
297 value_subtract(tmp
,max_val
,min_val
);
298 if (VALUE_TO_INT(tmp
) > 80)
299 st
= 1+(VALUE_TO_INT(tmp
))/80;
302 for(i
=VALUE_TO_INT(min_val
);i
<=VALUE_TO_INT(max_val
);i
+=st
)
309 /******* CHECK NOW *********/
311 if(S
&& !check_poly(S
, C
, EP
, exist
, nparam
, 0, p
, print_all
)) {
312 fprintf(stderr
,"Check failed !\n");
319 for(i
=0;i
<=(P
->Dimension
+1);i
++)
327 /****************************************************/
328 /* function check_poly : */
329 /* scans the parameter space from min to max (all */
330 /* directions). Computes the number of points in */
331 /* the polytope using both methods, and compare them*/
332 /* returns 1 on success */
333 /****************************************************/
335 int check_poly(Polyhedron
*S
, Polyhedron
*C
, evalue
*EP
,
336 int exist
, int nparam
, int pos
, Value
*z
, int print_all
)
341 value_init(c
); value_init(tmp
);
345 /* Computes the ehrhart polynomial */
346 value_set_double(c
, compute_evalue(EP
,&z
[S
->Dimension
-nparam
+1])+.25);
347 /* if c=0 we may be out of context. */
348 /* scanning is useless in this case*/
349 if(!in_domain(C
,&z
[S
->Dimension
-nparam
+1])) {
357 value_print(stdout
,VALUE_FMT
,z
[S
->Dimension
-nparam
+1]);
358 for(k
=S
->Dimension
-nparam
+2;k
<=S
->Dimension
;++k
) {
360 value_print(stdout
,VALUE_FMT
,z
[k
]);
363 value_print(stdout
,VALUE_FMT
,c
);
367 /* Manually count the number of points */
368 count_points_e(1, S
, exist
, nparam
, z
, &tmp
);
370 printf(", count = ");
371 value_print(stdout
, P_VALUE_FMT
, tmp
);
375 if(value_ne(tmp
,c
)) {
378 fprintf(stderr
,"Error !\n");
379 fprintf(stderr
,"EP( ");
380 value_print(stderr
,VALUE_FMT
,z
[S
->Dimension
-nparam
+1]);
381 for(k
=S
->Dimension
-nparam
+2;k
<=S
->Dimension
;++k
) {
382 fprintf(stderr
,", ");
383 value_print(stderr
,VALUE_FMT
,z
[k
]);
385 fprintf(stderr
," ) should be ");
386 value_print(stderr
,VALUE_FMT
,tmp
);
387 fprintf(stderr
,", while EP eval gives ");
388 value_print(stderr
,VALUE_FMT
,c
);
389 fprintf(stderr
,".\n");
390 print_evalue(stderr
, EP
, params
);
391 #ifndef DONT_BREAK_ON_ERROR
392 value_clear(c
); value_clear(tmp
);
401 for(value_assign(tmp
,min_val
); value_le(tmp
,max_val
); value_increment(tmp
,tmp
)) {
403 k
= VALUE_TO_INT(tmp
);
404 if(!pos
&& !(k
%st
)) {
410 value_assign(z
[pos
+S
->Dimension
-nparam
+1],tmp
);
411 if(!check_poly(S
, C
, EP
, exist
, nparam
, pos
+1, z
, print_all
)) {
412 value_clear(c
); value_clear(tmp
);
416 value_clear(c
); value_clear(tmp
);