4 #include <barvinok/util.h>
5 #include <barvinok/barvinok.h>
10 #include "omega/convert.h"
14 /* The input of this example program is a polytope in combined
15 * data and parameter space followed by two lines indicating
16 * the number of existential variables and parameters respectively.
17 * The first lines starts with "E ", followed by a number.
18 * The second lines starts with "P ", followed by a number.
19 * These two lines are (optionally) followed by the names of the parameters.
20 * The polytope is in PolyLib notation.
23 struct argp_option argp_options
[] = {
24 { "omega", 'o', 0, 0 },
26 { "series", 's', 0, 0 },
27 { "scarf", 'S', 0, 0 },
28 { "convert", 'c', 0, 0 },
29 { "floor", 'f', 0, 0 },
30 { "range-reduction", 'R', 0, 0 },
35 struct barvinok_options
*options
;
36 struct verify_options verify
;
46 error_t
parse_opt(int key
, char *arg
, struct argp_state
*state
)
48 struct arguments
*arguments
= (struct arguments
*)(state
->input
);
52 state
->child_inputs
[0] = arguments
->options
;
53 state
->child_inputs
[1] = &arguments
->verify
;
56 arguments
->series
= 1;
65 error(0, 0, "--omega option not supported");
72 error(0, 0, "--pip option not supported");
79 arguments
->convert
= 1;
85 return ARGP_ERR_UNKNOWN
;
92 Polyhedron
*Omega_simplify(Polyhedron
*P
,
93 unsigned exist
, unsigned nparam
, char **parms
)
97 Relation r
= Polyhedron2relation(P
, exist
, nparam
, parms
);
99 return relation2Domain(r
, varv
, paramv
);
102 Polyhedron
*Omega_simplify(Polyhedron
*P
,
103 unsigned exist
, unsigned nparam
, char **parms
)
109 /* define this to continue the test after first error found */
110 /* #define DONT_BREAK_ON_ERROR */
112 static Value min_val
, max_val
;
114 static char **params
;
118 static int check_poly(Polyhedron
*S
, Polyhedron
*C
, evalue
*EP
,
119 int exist
, int nparam
, int pos
, Value
*z
, int print_all
);
120 static void verify_results(Polyhedron
*P
, evalue
*EP
, int exist
, int nparam
,
121 int m
, int M
, int print_all
, unsigned MaxRays
);
123 int main(int argc
, char **argv
)
128 int exist
, nparam
, nvar
;
131 int print_solution
= 1;
132 struct arguments arguments
;
133 static struct argp_child argp_children
[] = {
134 { &barvinok_argp
, 0, 0, 0 },
135 { &verify_argp
, 0, "verification", 1 },
138 static struct argp argp
= { argp_options
, parse_opt
, 0, 0, argp_children
};
139 struct barvinok_options
*options
= barvinok_options_new_with_defaults();
141 arguments
.options
= options
;
143 arguments
.convert
= 0;
147 arguments
.series
= 0;
150 argp_parse(&argp
, argc
, argv
, 0, 0, &arguments
);
152 if (arguments
.series
&& !arguments
.scarf
) {
154 "--series currently only available if --scarf is specified\n");
159 A
= Constraints2Polyhedron(MA
, options
->MaxRays
);
162 fgets(s
, 128, stdin
);
163 while ((*s
=='#') || (sscanf(s
, "E %d", &exist
)<1))
164 fgets(s
, 128, stdin
);
166 fgets(s
, 128, stdin
);
167 while ((*s
=='#') || (sscanf(s
, "P %d", &nparam
)<1))
168 fgets(s
, 128, stdin
);
170 /******* Read the options: initialize Min and Max ********/
171 verify_options_set_range(&arguments
.verify
, A
);
173 if (arguments
.verify
.verify
)
176 if (print_solution
) {
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
);
185 exist
= A
->Dimension
- nvar
- nparam
;
187 if (arguments
.series
) {
189 barvinok_options
*options
= barvinok_options_new_with_defaults();
190 assert(arguments
.scarf
);
191 gf
= barvinok_enumerate_scarf_series(A
, exist
, nparam
, options
);
192 if (print_solution
) {
193 gf
->print(std::cout
, nparam
, param_name
);
199 if (arguments
.scarf
) {
200 barvinok_options
*options
= barvinok_options_new_with_defaults();
201 EP
= barvinok_enumerate_scarf(A
, exist
, nparam
, options
);
203 } else if (arguments
.pip
&& exist
> 0)
204 EP
= barvinok_enumerate_pip_with_options(A
, exist
, nparam
, options
);
206 EP
= barvinok_enumerate_e_with_options(A
, exist
, nparam
, options
);
210 evalue_range_reduction(EP
);
212 print_evalue(stdout
, EP
, param_name
);
213 if (arguments
.floor
) {
214 fprintf(stderr
, "WARNING: floor conversion not supported\n");
215 evalue_frac2floor2(EP
, 0);
217 print_evalue(stdout
, EP
, param_name
);
218 } else if (arguments
.convert
) {
219 evalue_mod2table(EP
, nparam
);
221 print_evalue(stdout
, EP
, param_name
);
223 if (arguments
.verify
.verify
)
224 verify_results(A
, EP
, exist
, nparam
, arguments
.verify
.m
,
225 arguments
.verify
.M
, arguments
.verify
.print_all
,
227 free_evalue_refs(EP
);
230 Free_ParamNames(param_name
, nparam
);
235 void verify_results(Polyhedron
*P
, evalue
*EP
, int exist
, int nparam
, int m
, int M
,
236 int print_all
, unsigned MaxRays
)
242 Polyhedron
*C
= Polyhedron_Project(P
, nparam
);
245 value_set_si(min_val
,m
);
246 value_set_si(max_val
,M
);
249 p
= (Value
*)malloc(sizeof(Value
) * (P
->Dimension
+2));
250 for(i
=0;i
<=P
->Dimension
;i
++) {
252 value_set_si(p
[i
],0);
255 value_set_si(p
[i
],1);
257 /* S = scanning list of polyhedra */
258 S
= Polyhedron_Scan(P
, C
, MaxRays
& POL_NO_DUAL
? 0 : MaxRays
);
261 if (C
->Dimension
> 0) {
262 value_subtract(tmp
,max_val
,min_val
);
263 if (VALUE_TO_INT(tmp
) > 80)
264 st
= 1+(VALUE_TO_INT(tmp
))/80;
267 for(i
=VALUE_TO_INT(min_val
);i
<=VALUE_TO_INT(max_val
);i
+=st
)
274 /******* CHECK NOW *********/
276 if(S
&& !check_poly(S
, C
, EP
, exist
, nparam
, 0, p
, print_all
)) {
277 fprintf(stderr
,"Check failed !\n");
284 for(i
=0;i
<=(P
->Dimension
+1);i
++)
292 /****************************************************/
293 /* function check_poly : */
294 /* scans the parameter space from min to max (all */
295 /* directions). Computes the number of points in */
296 /* the polytope using both methods, and compare them*/
297 /* returns 1 on success */
298 /****************************************************/
300 int check_poly(Polyhedron
*S
, Polyhedron
*C
, evalue
*EP
,
301 int exist
, int nparam
, int pos
, Value
*z
, int print_all
)
306 value_init(c
); value_init(tmp
);
310 /* Computes the ehrhart polynomial */
311 value_set_double(c
, compute_evalue(EP
,&z
[S
->Dimension
-nparam
+1])+.25);
312 /* if c=0 we may be out of context. */
313 /* scanning is useless in this case*/
314 if(!in_domain(C
,&z
[S
->Dimension
-nparam
+1])) {
322 value_print(stdout
,VALUE_FMT
,z
[S
->Dimension
-nparam
+1]);
323 for(k
=S
->Dimension
-nparam
+2;k
<=S
->Dimension
;++k
) {
325 value_print(stdout
,VALUE_FMT
,z
[k
]);
328 value_print(stdout
,VALUE_FMT
,c
);
332 /* Manually count the number of points */
333 count_points_e(1, S
, exist
, nparam
, z
, &tmp
);
335 printf(", count = ");
336 value_print(stdout
, P_VALUE_FMT
, tmp
);
340 if(value_ne(tmp
,c
)) {
343 fprintf(stderr
,"Error !\n");
344 fprintf(stderr
,"EP( ");
345 value_print(stderr
,VALUE_FMT
,z
[S
->Dimension
-nparam
+1]);
346 for(k
=S
->Dimension
-nparam
+2;k
<=S
->Dimension
;++k
) {
347 fprintf(stderr
,", ");
348 value_print(stderr
,VALUE_FMT
,z
[k
]);
350 fprintf(stderr
," ) should be ");
351 value_print(stderr
,VALUE_FMT
,tmp
);
352 fprintf(stderr
,", while EP eval gives ");
353 value_print(stderr
,VALUE_FMT
,c
);
354 fprintf(stderr
,".\n");
355 print_evalue(stderr
, EP
, params
);
356 #ifndef DONT_BREAK_ON_ERROR
357 value_clear(c
); value_clear(tmp
);
366 for(value_assign(tmp
,min_val
); value_le(tmp
,max_val
); value_increment(tmp
,tmp
)) {
368 k
= VALUE_TO_INT(tmp
);
369 if(!pos
&& !(k
%st
)) {
375 value_assign(z
[pos
+S
->Dimension
-nparam
+1],tmp
);
376 if(!check_poly(S
, C
, EP
, exist
, nparam
, pos
+1, z
, print_all
)) {
377 value_clear(c
); value_clear(tmp
);
381 value_clear(c
); value_clear(tmp
);