doc: drop description of BV_GBR_NONE
[barvinok.git] / barvinok_union.cc
blob76a598121ac795646e5849854583c0ae066c5611
1 #include <stdlib.h>
2 #include <isl/ctx.h>
3 #include <isl/space.h>
4 #include <isl/polynomial.h>
5 #include <barvinok/barvinok.h>
6 #include <barvinok/util.h>
7 #include "barvinok_union_options.h"
9 /* The input of this example program is similar to that of ehrhart_union
10 * in the PolyLib distribution, the difference being that the number of
11 * polytopes in the union needs to be specified explicitly.
12 * The input starts with this number, followed by this number of
13 * polytopes in combined data and parameter space, a context polytope
14 * in parameter space and (optionally) the names of the parameters.
15 * All polytopes are in PolyLib notation.
19 /* Convert "EP" into an isl_pw_qpolynomial with "nparam" parameter names
20 * specified by "names".
22 static __isl_give isl_pw_qpolynomial *evalue2pwqp(isl_ctx *ctx, evalue *EP,
23 unsigned nparam, const char **names)
25 int i;
26 isl_space *space;
28 space = isl_space_params_alloc(ctx, nparam);
29 for (i = 0; i < nparam; ++i)
30 space = isl_space_set_dim_name(space,
31 isl_dim_param, i, names[i]);
32 return isl_pw_qpolynomial_from_evalue(space, EP);
35 /* Check that "pwqp" is equal to the piecewise quasi-polynomial
36 * that appears next on stdin.
38 static isl_stat check_result(__isl_keep isl_pw_qpolynomial *pwqp)
40 isl_ctx *ctx;
41 isl_bool equal;
42 isl_pw_qpolynomial *exp;
44 if (!pwqp)
45 return isl_stat_error;
46 ctx = isl_pw_qpolynomial_get_ctx(pwqp);
48 exp = isl_pw_qpolynomial_read_from_file(ctx, stdin);
49 exp = isl_pw_qpolynomial_sub(exp, isl_pw_qpolynomial_copy(pwqp));
50 equal = isl_pw_qpolynomial_is_zero(exp);
51 isl_pw_qpolynomial_free(exp);
53 if (equal < 0)
54 return isl_stat_error;
55 if (!equal)
56 isl_die(ctx, isl_error_unknown,
57 "unexpected result", return isl_stat_error);
59 return isl_stat_ok;
62 int main(int argc, char **argv)
64 isl_ctx *ctx;
65 Matrix *M;
66 Polyhedron *C, *D = NULL;
67 int i, npol;
68 const char **param_name;
69 char s[128];
70 int c, ind = 0;
71 int check;
72 int r = EXIT_SUCCESS;
73 struct union_options *options = union_options_new_with_defaults();
75 argc = union_options_parse(options, argc, argv, ISL_ARG_ALL);
76 ctx = isl_ctx_alloc_with_options(&union_options_args, options);
78 check = options->check && !options->series;
80 fgets(s, 128, stdin);
81 while ((*s=='#') || (sscanf(s, "%d", &npol)<1))
82 fgets(s, 128, stdin);
84 for (i = 0; i < npol; ++i) {
85 Polyhedron *P;
86 M = Matrix_Read();
87 P = Constraints2Polyhedron(M, options->barvinok->MaxRays);
88 Matrix_Free(M);
89 D = DomainConcat(P, D);
91 M = Matrix_Read();
92 C = Constraints2Polyhedron(M, options->barvinok->MaxRays);
93 Matrix_Free(M);
94 if (!check) {
95 Polyhedron_Print(stdout, P_VALUE_FMT, D);
96 Polyhedron_Print(stdout, P_VALUE_FMT, C);
98 param_name = Read_ParamNames(stdin, C->Dimension);
99 if (options->series) {
100 gen_fun *gf;
101 gf = barvinok_enumerate_union_series(D, C, options->barvinok->MaxRays);
102 gf->print(std::cout, C->Dimension, param_name);
103 puts("");
104 delete gf;
105 } else {
106 evalue *EP;
107 EP = barvinok_enumerate_union(D, C, options->barvinok->MaxRays);
108 if (check) {
109 isl_pw_qpolynomial *pwqp;
110 pwqp = evalue2pwqp(ctx, EP, C->Dimension, param_name);
111 if (check_result(pwqp) < 0)
112 r = EXIT_FAILURE;
113 isl_pw_qpolynomial_free(pwqp);
114 } else {
115 print_evalue(stdout, EP, param_name);
117 evalue_free(EP);
119 Free_ParamNames(param_name, C->Dimension);
120 Domain_Free(D);
121 Polyhedron_Free(C);
122 isl_ctx_free(ctx);
123 return r;