barvinok_e.cc: directly include required headers
[barvinok.git] / barvinok_summate.c
blob30b757044dd8dad6d4a1515a30b850d781c969a7
1 #include <assert.h>
2 #include <isl/ctx.h>
3 #include <isl/val.h>
4 #include <isl/space.h>
5 #include <isl/point.h>
6 #include <isl/set.h>
7 #include <isl/polynomial.h>
8 #include <isl/stream.h>
9 #include <isl/printer.h>
10 #include <barvinok/isl.h>
11 #include <barvinok/options.h>
12 #include <barvinok/util.h>
13 #include "verify.h"
15 struct options {
16 struct verify_options *verify;
19 ISL_ARGS_START(struct options, options_args)
20 ISL_ARG_CHILD(struct options, verify, NULL,
21 &verify_options_args, "verification")
22 ISL_ARGS_END
24 ISL_ARG_DEF(options, struct options, options_args)
26 struct verify_point_sum {
27 struct verify_point_data vpd;
28 isl_pw_qpolynomial *pwqp;
29 isl_pw_qpolynomial *sum;
31 isl_pw_qpolynomial *fixed;
32 isl_val *manual;
35 static isl_stat manual_sum(__isl_take isl_point *pnt, void *user)
37 struct verify_point_sum *vps = (struct verify_point_sum *) user;
38 isl_val *v;
40 v = isl_pw_qpolynomial_eval(isl_pw_qpolynomial_copy(vps->fixed), pnt);
41 vps->manual = isl_val_add(vps->manual, v);
43 return isl_stat_ok;
46 static isl_stat verify_point(__isl_take isl_point *pnt, void *user)
48 struct verify_point_sum *vps = (struct verify_point_sum *) user;
49 int i;
50 int ok;
51 unsigned nvar;
52 unsigned nparam;
53 isl_val *v;
54 isl_set *dom;
55 isl_val *eval;
56 int r;
57 FILE *out = vps->vpd.options->print_all ? stdout : stderr;
59 vps->vpd.n--;
61 vps->fixed = isl_pw_qpolynomial_copy(vps->pwqp);
62 nparam = isl_pw_qpolynomial_dim(vps->sum, isl_dim_param);
63 for (i = 0; i < nparam; ++i) {
64 v = isl_point_get_coordinate_val(pnt, isl_dim_param, i);
65 vps->fixed = isl_pw_qpolynomial_fix_val(vps->fixed,
66 isl_dim_param, i, v);
69 eval = isl_pw_qpolynomial_eval(isl_pw_qpolynomial_copy(vps->sum),
70 isl_point_copy(pnt));
72 vps->manual = isl_val_zero(isl_point_get_ctx(pnt));
73 dom = isl_pw_qpolynomial_domain(isl_pw_qpolynomial_copy(vps->fixed));
74 r = isl_set_foreach_point(dom, &manual_sum, user);
75 isl_set_free(dom);
76 if (r < 0)
77 goto error;
79 nvar = isl_set_dim(dom, isl_dim_set);
81 ok = isl_val_eq(eval, vps->manual);
83 if (vps->vpd.options->print_all || !ok) {
84 isl_ctx *ctx = isl_pw_qpolynomial_get_ctx(vps->pwqp);
85 isl_printer *p = isl_printer_to_file(ctx, out);
86 fprintf(out, "sum(");
87 for (i = 0; i < nparam; ++i) {
88 if (i)
89 fprintf(out, ", ");
90 v = isl_point_get_coordinate_val(pnt, isl_dim_param, i);
91 p = isl_printer_print_val(p, v);
92 isl_val_free(v);
94 fprintf(out, ") = ");
95 p = isl_printer_print_val(p, eval);
96 fprintf(out, ", sum(EP) = ");
97 p = isl_printer_print_val(p, vps->manual);
98 if (ok)
99 fprintf(out, ". OK\n");
100 else
101 fprintf(out, ". NOT OK\n");
102 isl_printer_free(p);
103 } else if ((vps->vpd.n % vps->vpd.s) == 0) {
104 printf("o");
105 fflush(stdout);
108 if (0) {
109 error:
110 ok = 0;
112 isl_val_free(vps->manual);
113 isl_pw_qpolynomial_free(vps->fixed);
114 isl_val_free(eval);
115 isl_point_free(pnt);
117 if (!ok)
118 vps->vpd.error = 1;
120 if (vps->vpd.options->continue_on_error)
121 ok = 1;
123 return (vps->vpd.n >= 1 && ok) ? isl_stat_ok : isl_stat_error;
126 static int verify(__isl_keep isl_pw_qpolynomial *pwqp,
127 __isl_take isl_pw_qpolynomial *sum, struct verify_options *options)
129 struct verify_point_sum vps = { { options } };
130 isl_set *context;
131 int r;
133 vps.pwqp = pwqp;
134 vps.sum = sum;
136 context = isl_pw_qpolynomial_domain(isl_pw_qpolynomial_copy(sum));
137 context = verify_context_set_bounds(context, options);
139 r = verify_point_data_init(&vps.vpd, context);
141 if (r == 0)
142 isl_set_foreach_point(context, verify_point, &vps);
143 if (vps.vpd.error)
144 r = -1;
146 isl_set_free(context);
148 verify_point_data_fini(&vps.vpd);
150 return r;
153 int main(int argc, char **argv)
155 int result = 0;
156 isl_ctx *ctx;
157 isl_pw_qpolynomial *pwqp;
158 isl_pw_qpolynomial *sum;
159 struct isl_stream *s;
160 struct options *options = options_new_with_defaults();
162 argc = options_parse(options, argc, argv, ISL_ARG_ALL);
163 ctx = isl_ctx_alloc_with_options(&options_args, options);
165 s = isl_stream_new_file(ctx, stdin);
166 pwqp = isl_stream_read_pw_qpolynomial(s);
168 if (options->verify->verify) {
169 isl_space *dim = isl_pw_qpolynomial_get_space(pwqp);
170 unsigned total = isl_space_dim(dim, isl_dim_all);
171 isl_space_free(dim);
172 verify_options_set_range(options->verify, total);
175 sum = isl_pw_qpolynomial_sum(isl_pw_qpolynomial_copy(pwqp));
176 if (options->verify->verify)
177 result = verify(pwqp, sum, options->verify);
178 else {
179 isl_printer *p = isl_printer_to_file(ctx, stdout);
180 p = isl_printer_print_pw_qpolynomial(p, sum);
181 p = isl_printer_end_line(p);
182 isl_printer_free(p);
184 isl_pw_qpolynomial_free(sum);
185 isl_pw_qpolynomial_free(pwqp);
187 isl_stream_free(s);
188 isl_ctx_free(ctx);
189 return result;