update AUTHORS
[barvinok.git] / barvinok_union.cc
blobe80760479d2823b8bc1d4e29e4e2200294ede8c9
1 #include <stdlib.h>
2 #include <isl/ctx.h>
3 #include <isl/space.h>
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)
27 int i;
28 isl_space *space;
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)
42 isl_ctx *ctx;
43 isl_bool equal;
44 isl_pw_qpolynomial *exp;
46 if (!pwqp)
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);
55 if (equal < 0)
56 return isl_stat_error;
57 if (!equal)
58 isl_die(ctx, isl_error_unknown,
59 "unexpected result", return isl_stat_error);
61 return isl_stat_ok;
64 int main(int argc, char **argv)
66 isl_ctx *ctx;
67 Matrix *M;
68 Polyhedron *C, *D = NULL;
69 int i, npol;
70 const char **param_name;
71 char s[128];
72 int check;
73 int r = EXIT_SUCCESS;
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;
81 fgets(s, 128, stdin);
82 while ((*s=='#') || (sscanf(s, "%d", &npol)<1))
83 fgets(s, 128, stdin);
85 for (i = 0; i < npol; ++i) {
86 Polyhedron *P;
87 M = Matrix_Read();
88 P = Constraints2Polyhedron(M, options->barvinok->MaxRays);
89 Matrix_Free(M);
90 D = DomainConcat(P, D);
92 M = Matrix_Read();
93 C = Constraints2Polyhedron(M, options->barvinok->MaxRays);
94 Matrix_Free(M);
95 if (!check) {
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) {
101 gen_fun *gf;
102 gf = barvinok_enumerate_union_series(D, C, options->barvinok->MaxRays);
103 gf->print(std::cout, C->Dimension, param_name);
104 puts("");
105 delete gf;
106 } else {
107 evalue *EP;
108 EP = barvinok_enumerate_union(D, C, options->barvinok->MaxRays);
109 if (check) {
110 isl_pw_qpolynomial *pwqp;
111 pwqp = evalue2pwqp(ctx, EP, C->Dimension, param_name);
112 if (check_result(pwqp) < 0)
113 r = EXIT_FAILURE;
114 isl_pw_qpolynomial_free(pwqp);
115 } else {
116 print_evalue(stdout, EP, param_name);
118 evalue_free(EP);
120 Free_ParamNames(param_name, C->Dimension);
121 Domain_Free(D);
122 Polyhedron_Free(C);
123 isl_ctx_free(ctx);
124 return r;