update isl-polylib for include path ordering issue
[barvinok.git] / barvinok_summate.c
blobb8430f23338216b11e2baec5e792b0b8f6572670
1 #include <assert.h>
2 #include <isl/stream.h>
3 #include <barvinok/barvinok.h>
4 #include <barvinok/options.h>
5 #include <barvinok/util.h>
6 #include "verify.h"
8 struct options {
9 struct verify_options *verify;
12 struct isl_arg options_arg[] = {
13 ISL_ARG_CHILD(struct options, verify, NULL,
14 verify_options_arg, "verification")
15 ISL_ARG_END
18 ISL_ARG_DEF(options, struct options, options_arg)
20 struct verify_point_sum {
21 struct verify_point_data vpd;
22 isl_pw_qpolynomial *pwqp;
23 isl_pw_qpolynomial *sum;
25 isl_pw_qpolynomial *fixed;
26 isl_qpolynomial *manual;
29 static int manual_sum(__isl_take isl_point *pnt, void *user)
31 struct verify_point_sum *vps = (struct verify_point_sum *) user;
32 isl_qpolynomial *qp;
34 qp = isl_pw_qpolynomial_eval(isl_pw_qpolynomial_copy(vps->fixed), pnt);
35 vps->manual = isl_qpolynomial_add(vps->manual, qp);
37 return 0;
40 static int verify_point(__isl_take isl_point *pnt, void *user)
42 struct verify_point_sum *vps = (struct verify_point_sum *) user;
43 int i;
44 int ok;
45 unsigned nvar;
46 unsigned nparam;
47 isl_int v;
48 isl_space *space;
49 isl_set *dom;
50 isl_qpolynomial *eval;
51 int r;
52 FILE *out = vps->vpd.options->print_all ? stdout : stderr;
54 vps->vpd.n--;
56 isl_int_init(v);
57 vps->fixed = isl_pw_qpolynomial_copy(vps->pwqp);
58 nparam = isl_pw_qpolynomial_dim(vps->sum, isl_dim_param);
59 for (i = 0; i < nparam; ++i) {
60 isl_point_get_coordinate(pnt, isl_dim_param, i, &v);
61 vps->fixed = isl_pw_qpolynomial_fix_dim(vps->fixed,
62 isl_dim_param, i, v);
65 eval = isl_pw_qpolynomial_eval(isl_pw_qpolynomial_copy(vps->sum),
66 isl_point_copy(pnt));
68 space = isl_pw_qpolynomial_get_domain_space(vps->pwqp);
69 vps->manual = isl_qpolynomial_zero_on_domain(space);
70 dom = isl_pw_qpolynomial_domain(isl_pw_qpolynomial_copy(vps->fixed));
71 r = isl_set_foreach_point(dom, &manual_sum, user);
72 isl_set_free(dom);
73 if (r < 0)
74 goto error;
76 nvar = isl_set_dim(dom, isl_dim_set);
77 vps->manual = isl_qpolynomial_project_domain_on_params(vps->manual);
79 ok = isl_qpolynomial_plain_is_equal(eval, vps->manual);
81 if (vps->vpd.options->print_all || !ok) {
82 isl_ctx *ctx = isl_pw_qpolynomial_get_ctx(vps->pwqp);
83 isl_printer *p = isl_printer_to_file(ctx, out);
84 fprintf(out, "sum(");
85 for (i = 0; i < nparam; ++i) {
86 if (i)
87 fprintf(out, ", ");
88 isl_point_get_coordinate(pnt, isl_dim_param, i, &v);
89 isl_int_print(out, v, 0);
91 fprintf(out, ") = ");
92 p = isl_printer_print_qpolynomial(p, eval);
93 fprintf(out, ", sum(EP) = ");
94 p = isl_printer_print_qpolynomial(p, vps->manual);
95 if (ok)
96 fprintf(out, ". OK\n");
97 else
98 fprintf(out, ". NOT OK\n");
99 isl_printer_free(p);
100 } else if ((vps->vpd.n % vps->vpd.s) == 0) {
101 printf("o");
102 fflush(stdout);
105 if (0) {
106 error:
107 ok = 0;
109 isl_qpolynomial_free(vps->manual);
110 isl_pw_qpolynomial_free(vps->fixed);
111 isl_qpolynomial_free(eval);
112 isl_int_clear(v);
113 isl_point_free(pnt);
115 if (!ok)
116 vps->vpd.error = 1;
118 if (vps->vpd.options->continue_on_error)
119 ok = 1;
121 return (vps->vpd.n >= 1 && ok) ? 0 : -1;
124 static int verify(__isl_keep isl_pw_qpolynomial *pwqp,
125 __isl_take isl_pw_qpolynomial *sum, struct verify_options *options)
127 struct verify_point_sum vps = { { options } };
128 isl_set *context;
129 int r;
131 vps.pwqp = pwqp;
132 vps.sum = sum;
134 context = isl_pw_qpolynomial_domain(isl_pw_qpolynomial_copy(sum));
135 context = verify_context_set_bounds(context, options);
137 r = verify_point_data_init(&vps.vpd, context);
139 if (r == 0)
140 isl_set_foreach_point(context, verify_point, &vps);
141 if (vps.vpd.error)
142 r = -1;
144 isl_set_free(context);
146 verify_point_data_fini(&vps.vpd);
148 return r;
151 int main(int argc, char **argv)
153 int i;
154 int result = 0;
155 isl_ctx *ctx;
156 isl_space *dim;
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_arg, 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;