update isl for change in isl_vertex inspectors
[barvinok/uuh.git] / barvinok_summate.c
blob200dd49ecf4679331e97eb28a59e6106d969185e
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 ISL_ARGS_START(struct options, options_args)
13 ISL_ARG_CHILD(struct options, verify, NULL,
14 &verify_options_args, "verification")
15 ISL_ARGS_END
17 ISL_ARG_DEF(options, struct options, options_args)
19 struct verify_point_sum {
20 struct verify_point_data vpd;
21 isl_pw_qpolynomial *pwqp;
22 isl_pw_qpolynomial *sum;
24 isl_pw_qpolynomial *fixed;
25 isl_val *manual;
28 static int manual_sum(__isl_take isl_point *pnt, void *user)
30 struct verify_point_sum *vps = (struct verify_point_sum *) user;
31 isl_val *v;
33 v = isl_pw_qpolynomial_eval(isl_pw_qpolynomial_copy(vps->fixed), pnt);
34 vps->manual = isl_val_add(vps->manual, v);
36 return 0;
39 static int verify_point(__isl_take isl_point *pnt, void *user)
41 struct verify_point_sum *vps = (struct verify_point_sum *) user;
42 int i;
43 int ok;
44 unsigned nvar;
45 unsigned nparam;
46 isl_val *v;
47 isl_space *space;
48 isl_set *dom;
49 isl_val *eval;
50 int r;
51 FILE *out = vps->vpd.options->print_all ? stdout : stderr;
53 vps->vpd.n--;
55 vps->fixed = isl_pw_qpolynomial_copy(vps->pwqp);
56 nparam = isl_pw_qpolynomial_dim(vps->sum, isl_dim_param);
57 for (i = 0; i < nparam; ++i) {
58 v = isl_point_get_coordinate_val(pnt, isl_dim_param, i);
59 vps->fixed = isl_pw_qpolynomial_fix_val(vps->fixed,
60 isl_dim_param, i, v);
63 eval = isl_pw_qpolynomial_eval(isl_pw_qpolynomial_copy(vps->sum),
64 isl_point_copy(pnt));
66 vps->manual = isl_val_zero(isl_point_get_ctx(pnt));
67 dom = isl_pw_qpolynomial_domain(isl_pw_qpolynomial_copy(vps->fixed));
68 r = isl_set_foreach_point(dom, &manual_sum, user);
69 isl_set_free(dom);
70 if (r < 0)
71 goto error;
73 nvar = isl_set_dim(dom, isl_dim_set);
75 ok = isl_val_eq(eval, vps->manual);
77 if (vps->vpd.options->print_all || !ok) {
78 isl_ctx *ctx = isl_pw_qpolynomial_get_ctx(vps->pwqp);
79 isl_printer *p = isl_printer_to_file(ctx, out);
80 fprintf(out, "sum(");
81 for (i = 0; i < nparam; ++i) {
82 if (i)
83 fprintf(out, ", ");
84 v = isl_point_get_coordinate_val(pnt, isl_dim_param, i);
85 p = isl_printer_print_val(p, v);
86 isl_val_free(v);
88 fprintf(out, ") = ");
89 p = isl_printer_print_val(p, eval);
90 fprintf(out, ", sum(EP) = ");
91 p = isl_printer_print_val(p, vps->manual);
92 if (ok)
93 fprintf(out, ". OK\n");
94 else
95 fprintf(out, ". NOT OK\n");
96 isl_printer_free(p);
97 } else if ((vps->vpd.n % vps->vpd.s) == 0) {
98 printf("o");
99 fflush(stdout);
102 if (0) {
103 error:
104 ok = 0;
106 isl_val_free(vps->manual);
107 isl_pw_qpolynomial_free(vps->fixed);
108 isl_val_free(eval);
109 isl_point_free(pnt);
111 if (!ok)
112 vps->vpd.error = 1;
114 if (vps->vpd.options->continue_on_error)
115 ok = 1;
117 return (vps->vpd.n >= 1 && ok) ? 0 : -1;
120 static int verify(__isl_keep isl_pw_qpolynomial *pwqp,
121 __isl_take isl_pw_qpolynomial *sum, struct verify_options *options)
123 struct verify_point_sum vps = { { options } };
124 isl_set *context;
125 int r;
127 vps.pwqp = pwqp;
128 vps.sum = sum;
130 context = isl_pw_qpolynomial_domain(isl_pw_qpolynomial_copy(sum));
131 context = verify_context_set_bounds(context, options);
133 r = verify_point_data_init(&vps.vpd, context);
135 if (r == 0)
136 isl_set_foreach_point(context, verify_point, &vps);
137 if (vps.vpd.error)
138 r = -1;
140 isl_set_free(context);
142 verify_point_data_fini(&vps.vpd);
144 return r;
147 int main(int argc, char **argv)
149 int i;
150 int result = 0;
151 isl_ctx *ctx;
152 isl_space *dim;
153 isl_pw_qpolynomial *pwqp;
154 isl_pw_qpolynomial *sum;
155 struct isl_stream *s;
156 struct options *options = options_new_with_defaults();
158 argc = options_parse(options, argc, argv, ISL_ARG_ALL);
159 ctx = isl_ctx_alloc_with_options(&options_args, options);
161 s = isl_stream_new_file(ctx, stdin);
162 pwqp = isl_stream_read_pw_qpolynomial(s);
164 if (options->verify->verify) {
165 isl_space *dim = isl_pw_qpolynomial_get_space(pwqp);
166 unsigned total = isl_space_dim(dim, isl_dim_all);
167 isl_space_free(dim);
168 verify_options_set_range(options->verify, total);
171 sum = isl_pw_qpolynomial_sum(isl_pw_qpolynomial_copy(pwqp));
172 if (options->verify->verify)
173 result = verify(pwqp, sum, options->verify);
174 else {
175 isl_printer *p = isl_printer_to_file(ctx, stdout);
176 p = isl_printer_print_pw_qpolynomial(p, sum);
177 p = isl_printer_end_line(p);
178 isl_printer_free(p);
180 isl_pw_qpolynomial_free(sum);
181 isl_pw_qpolynomial_free(pwqp);
183 isl_stream_free(s);
184 isl_ctx_free(ctx);
185 return result;