laurent_old.cc: laurent_summator_old: member initializer list: base classes first
[barvinok.git] / barvinok_union.cc
blob9ff8cc32666357dada701fd0e889002f2e0372fa
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 check;
71 int r = EXIT_SUCCESS;
72 struct union_options *options = union_options_new_with_defaults();
74 argc = union_options_parse(options, argc, argv, ISL_ARG_ALL);
75 ctx = isl_ctx_alloc_with_options(&union_options_args, options);
77 check = options->check && !options->series;
79 fgets(s, 128, stdin);
80 while ((*s=='#') || (sscanf(s, "%d", &npol)<1))
81 fgets(s, 128, stdin);
83 for (i = 0; i < npol; ++i) {
84 Polyhedron *P;
85 M = Matrix_Read();
86 P = Constraints2Polyhedron(M, options->barvinok->MaxRays);
87 Matrix_Free(M);
88 D = DomainConcat(P, D);
90 M = Matrix_Read();
91 C = Constraints2Polyhedron(M, options->barvinok->MaxRays);
92 Matrix_Free(M);
93 if (!check) {
94 Polyhedron_Print(stdout, P_VALUE_FMT, D);
95 Polyhedron_Print(stdout, P_VALUE_FMT, C);
97 param_name = Read_ParamNames(stdin, C->Dimension);
98 if (options->series) {
99 gen_fun *gf;
100 gf = barvinok_enumerate_union_series(D, C, options->barvinok->MaxRays);
101 gf->print(std::cout, C->Dimension, param_name);
102 puts("");
103 delete gf;
104 } else {
105 evalue *EP;
106 EP = barvinok_enumerate_union(D, C, options->barvinok->MaxRays);
107 if (check) {
108 isl_pw_qpolynomial *pwqp;
109 pwqp = evalue2pwqp(ctx, EP, C->Dimension, param_name);
110 if (check_result(pwqp) < 0)
111 r = EXIT_FAILURE;
112 isl_pw_qpolynomial_free(pwqp);
113 } else {
114 print_evalue(stdout, EP, param_name);
116 evalue_free(EP);
118 Free_ParamNames(param_name, C->Dimension);
119 Domain_Free(D);
120 Polyhedron_Free(C);
121 isl_ctx_free(ctx);
122 return r;