4 #include <isl/polynomial.h>
5 #include <barvinok/polylib.h>
6 #include <barvinok/evalue.h>
7 #include <barvinok/barvinok.h>
8 #include <barvinok/util.h>
9 #include "barvinok_union_options.h"
11 /* The input of this example program is similar to that of ehrhart_union
12 * in the PolyLib distribution, the difference being that the number of
13 * polytopes in the union needs to be specified explicitly.
14 * The input starts with this number, followed by this number of
15 * polytopes in combined data and parameter space, a context polytope
16 * in parameter space and (optionally) the names of the parameters.
17 * All polytopes are in PolyLib notation.
21 /* Convert "EP" into an isl_pw_qpolynomial with "nparam" parameter names
22 * specified by "names".
24 static __isl_give isl_pw_qpolynomial
*evalue2pwqp(isl_ctx
*ctx
, evalue
*EP
,
25 unsigned nparam
, const char **names
)
30 space
= isl_space_params_alloc(ctx
, nparam
);
31 for (i
= 0; i
< nparam
; ++i
)
32 space
= isl_space_set_dim_name(space
,
33 isl_dim_param
, i
, names
[i
]);
34 return isl_pw_qpolynomial_from_evalue(space
, EP
);
37 /* Check that "pwqp" is equal to the piecewise quasi-polynomial
38 * that appears next on stdin.
40 static isl_stat
check_result(__isl_keep isl_pw_qpolynomial
*pwqp
)
44 isl_pw_qpolynomial
*exp
;
47 return isl_stat_error
;
48 ctx
= isl_pw_qpolynomial_get_ctx(pwqp
);
50 exp
= isl_pw_qpolynomial_read_from_file(ctx
, stdin
);
51 exp
= isl_pw_qpolynomial_sub(exp
, isl_pw_qpolynomial_copy(pwqp
));
52 equal
= isl_pw_qpolynomial_is_zero(exp
);
53 isl_pw_qpolynomial_free(exp
);
56 return isl_stat_error
;
58 isl_die(ctx
, isl_error_unknown
,
59 "unexpected result", return isl_stat_error
);
64 int main(int argc
, char **argv
)
68 Polyhedron
*C
, *D
= NULL
;
70 const char **param_name
;
74 struct union_options
*options
= union_options_new_with_defaults();
76 argc
= union_options_parse(options
, argc
, argv
, ISL_ARG_ALL
);
77 ctx
= isl_ctx_alloc_with_options(&union_options_args
, options
);
79 check
= options
->check
&& !options
->series
;
82 while ((*s
=='#') || (sscanf(s
, "%d", &npol
)<1))
85 for (i
= 0; i
< npol
; ++i
) {
88 P
= Constraints2Polyhedron(M
, options
->barvinok
->MaxRays
);
90 D
= DomainConcat(P
, D
);
93 C
= Constraints2Polyhedron(M
, options
->barvinok
->MaxRays
);
96 Polyhedron_Print(stdout
, P_VALUE_FMT
, D
);
97 Polyhedron_Print(stdout
, P_VALUE_FMT
, C
);
99 param_name
= Read_ParamNames(stdin
, C
->Dimension
);
100 if (options
->series
) {
102 gf
= barvinok_enumerate_union_series(D
, C
, options
->barvinok
->MaxRays
);
103 gf
->print(std::cout
, C
->Dimension
, param_name
);
108 EP
= barvinok_enumerate_union(D
, C
, options
->barvinok
->MaxRays
);
110 isl_pw_qpolynomial
*pwqp
;
111 pwqp
= evalue2pwqp(ctx
, EP
, C
->Dimension
, param_name
);
112 if (check_result(pwqp
) < 0)
114 isl_pw_qpolynomial_free(pwqp
);
116 print_evalue(stdout
, EP
, param_name
);
120 Free_ParamNames(param_name
, C
->Dimension
);