isl_obj_str.c: isl_str_free: drop unused variable
[barvinok.git] / barvinok_enumerate.cc
blobe23f231338ea3756ccd7a6c658174a540de33014
1 #include <assert.h>
2 #include <unistd.h>
3 #include <stdlib.h>
4 #include <gmp.h>
5 #include <isl_set_polylib.h>
6 #include <barvinok/evalue.h>
7 #include <barvinok/util.h>
8 #include <barvinok/barvinok.h>
9 #include "barvinok_enumerate_options.h"
10 #include "verify.h"
11 #include "verify_series.h"
12 #include "remove_equalities.h"
13 #include "evalue_convert.h"
14 #include "conversion.h"
15 #include "skewed_genfun.h"
17 #undef CS /* for Solaris 10 */
19 using std::cout;
20 using std::endl;
22 /* The input of this example program is the same as that of testehrhart
23 * in the PolyLib distribution, i.e., a polytope in combined
24 * data and parameter space, a context polytope in parameter space
25 * and (optionally) the names of the parameters.
26 * Both polytopes are in PolyLib notation.
29 struct verify_point_enum {
30 struct verify_point_data vpd;
31 isl_set *set;
32 isl_pw_qpolynomial *pwqp;
35 static isl_stat verify_point(__isl_take isl_point *pnt, void *user)
37 struct verify_point_enum *vpe = (struct verify_point_enum *) user;
38 isl_set *set;
39 int i;
40 unsigned nparam;
41 isl_val *v, *n, *t;
42 int pa = vpe->vpd.options->barvinok->approx->approximation;
43 int ok;
44 FILE *out = vpe->vpd.options->print_all ? stdout : stderr;
46 vpe->vpd.n--;
48 set = isl_set_copy(vpe->set);
49 nparam = isl_set_dim(set, isl_dim_param);
50 for (i = 0; i < nparam; ++i) {
51 v = isl_point_get_coordinate_val(pnt, isl_dim_param, i);
52 set = isl_set_fix_val(set, isl_dim_param, i, v);
55 v = isl_set_count_val(set);
57 n = isl_pw_qpolynomial_eval(isl_pw_qpolynomial_copy(vpe->pwqp),
58 isl_point_copy(pnt));
60 if (pa == BV_APPROX_SIGN_LOWER)
61 n = isl_val_ceil(n);
62 else if (pa == BV_APPROX_SIGN_UPPER)
63 n = isl_val_floor(n);
64 else
65 n = isl_val_trunc(n);
67 if (pa == BV_APPROX_SIGN_APPROX)
68 /* just accept everything */
69 ok = 1;
70 else if (pa == BV_APPROX_SIGN_LOWER)
71 ok = isl_val_le(n, v);
72 else if (pa == BV_APPROX_SIGN_UPPER)
73 ok = isl_val_ge(n, v);
74 else
75 ok = isl_val_eq(n, v);
77 if (vpe->vpd.options->print_all || !ok) {
78 isl_ctx *ctx = isl_point_get_ctx(pnt);
79 isl_printer *p;
80 p = isl_printer_to_file(ctx, out);
81 p = isl_printer_print_str(p, "EP(");
82 for (i = 0; i < nparam; ++i) {
83 if (i)
84 p = isl_printer_print_str(p, ", ");
85 t = isl_point_get_coordinate_val(pnt, isl_dim_param, i);
86 p = isl_printer_print_val(p, t);
87 isl_val_free(t);
89 p = isl_printer_print_str(p, ") = ");
90 p = isl_printer_print_val(p, n);
91 p = isl_printer_print_str(p, ", count = ");
92 p = isl_printer_print_val(p, v);
93 if (ok)
94 p = isl_printer_print_str(p, ". OK");
95 else
96 p = isl_printer_print_str(p, ". NOT OK");
97 p = isl_printer_end_line(p);
98 isl_printer_free(p);
99 } else if ((vpe->vpd.n % vpe->vpd.s) == 0) {
100 printf("o");
101 fflush(stdout);
104 if (0) {
105 error:
106 ok = 0;
108 isl_set_free(set);
109 isl_val_free(v);
110 isl_val_free(n);
111 isl_point_free(pnt);
113 if (!ok)
114 vpe->vpd.error = 1;
116 if (vpe->vpd.options->continue_on_error)
117 ok = 1;
119 return (vpe->vpd.n >= 1 && ok) ? isl_stat_ok : isl_stat_error;
122 static int verify_isl(Polyhedron *P, Polyhedron *C,
123 evalue *EP, const struct verify_options *options)
125 struct verify_point_enum vpe = { { options } };
126 int i;
127 isl_ctx *ctx = isl_ctx_alloc();
128 isl_space *dim;
129 isl_set *set;
130 isl_set *set_C;
131 int r;
133 dim = isl_space_set_alloc(ctx, C->Dimension, P->Dimension - C->Dimension);
134 for (i = 0; i < C->Dimension; ++i)
135 dim = isl_space_set_dim_name(dim, isl_dim_param, i, options->params[i]);
136 set = isl_set_new_from_polylib(P, isl_space_copy(dim));
137 dim = isl_space_params(dim);
138 set_C = isl_set_new_from_polylib(C, dim);
139 set_C = isl_set_intersect_params(isl_set_copy(set), set_C);
140 set_C = isl_set_params(set_C);
142 set_C = verify_context_set_bounds(set_C, options);
144 r = verify_point_data_init(&vpe.vpd, set_C);
146 vpe.set = set;
147 vpe.pwqp = isl_pw_qpolynomial_from_evalue(isl_set_get_space(set_C), EP);
148 if (r == 0)
149 isl_set_foreach_point(set_C, verify_point, &vpe);
150 if (vpe.vpd.error)
151 r = -1;
153 isl_pw_qpolynomial_free(vpe.pwqp);
154 isl_set_free(set);
155 isl_set_free(set_C);
157 isl_ctx_free(ctx);
159 verify_point_data_fini(&vpe.vpd);
161 return r;
164 static int verify(Polyhedron *P, Polyhedron *C, evalue *EP, skewed_gen_fun *gf,
165 struct enumerate_options *options)
167 Polyhedron *CS, *S;
168 Vector *p;
169 int result = 0;
171 if (!options->series || options->function)
172 return verify_isl(P, C, EP, options->verify);
174 CS = check_poly_context_scan(P, &C, C->Dimension, options->verify);
176 p = Vector_Alloc(P->Dimension+2);
177 value_set_si(p->p[P->Dimension+1], 1);
179 /* S = scanning list of polyhedra */
180 S = Polyhedron_Scan(P, C, options->verify->barvinok->MaxRays);
182 check_poly_init(C, options->verify);
184 /******* CHECK NOW *********/
185 if (S) {
186 if (!check_poly_gf(S, CS, gf, 0, C->Dimension, 0, p->p,
187 options->verify))
188 result = -1;
189 Domain_Free(S);
192 if (result == -1)
193 fprintf(stderr,"Check failed !\n");
195 if (!options->verify->print_all)
196 printf( "\n" );
198 Vector_Free(p);
199 if (CS) {
200 Domain_Free(CS);
201 Domain_Free(C);
204 return result;
207 /* frees M and Minv */
208 static void apply_transformation(Polyhedron **P, Polyhedron **C,
209 bool free_P, bool free_C,
210 Matrix *M, Matrix *Minv, Matrix **inv,
211 barvinok_options *options)
213 Polyhedron *T;
214 Matrix *M2;
216 M2 = align_matrix(M, (*P)->Dimension + 1);
217 T = *P;
218 *P = Polyhedron_Preimage(*P, M2, options->MaxRays);
219 if (free_P)
220 Polyhedron_Free(T);
221 Matrix_Free(M2);
223 T = *C;
224 *C = Polyhedron_Preimage(*C, M, options->MaxRays);
225 if (free_C)
226 Polyhedron_Free(T);
228 Matrix_Free(M);
230 if (*inv) {
231 Matrix *T = *inv;
232 *inv = Matrix_Alloc(Minv->NbRows, T->NbColumns);
233 Matrix_Product(Minv, T, *inv);
234 Matrix_Free(T);
235 Matrix_Free(Minv);
236 } else
237 *inv = Minv;
240 /* Since we have "compressed" the parameters (in case there were
241 * any equalities), the result is independent of the coordinates in the
242 * coordinate subspace spanned by the lines. We can therefore assume
243 * these coordinates are zero and compute the inverse image of the map
244 * from a lower dimensional space that adds zeros in the appropriate
245 * places.
247 static void remove_lines(Polyhedron *C, Matrix **M, Matrix **Minv)
249 Matrix *L = Matrix_Alloc(C->Dimension+1, C->Dimension+1);
250 for (int r = 0; r < C->NbBid; ++r)
251 Vector_Copy(C->Ray[r]+1, L->p[r], C->Dimension);
252 unimodular_complete(L, C->NbBid);
253 assert(value_one_p(L->p[C->Dimension][C->Dimension]));
254 assert(First_Non_Zero(L->p[C->Dimension], C->Dimension) == -1);
255 Matrix_Transposition(L);
256 assert(First_Non_Zero(L->p[C->Dimension], C->Dimension) == -1);
258 *M = Matrix_Alloc(C->Dimension+1, C->Dimension-C->NbBid+1);
259 for (int i = 0; i < C->Dimension+1; ++i)
260 Vector_Copy(L->p[i]+C->NbBid, (*M)->p[i], C->Dimension-C->NbBid+1);
262 Matrix *Linv = Matrix_Alloc(C->Dimension+1, C->Dimension+1);
263 int ok = Matrix_Inverse(L, Linv);
264 assert(ok);
265 Matrix_Free(L);
267 *Minv = Matrix_Alloc(C->Dimension-C->NbBid+1, C->Dimension+1);
268 for (int i = C->NbBid; i < C->Dimension+1; ++i)
269 Vector_AntiScale(Linv->p[i], (*Minv)->p[i-C->NbBid],
270 Linv->p[C->Dimension][C->Dimension], C->Dimension+1);
271 Matrix_Free(Linv);
274 static skewed_gen_fun *series(Polyhedron *P, Polyhedron* C,
275 barvinok_options *options)
277 Polyhedron *C1, *C2;
278 gen_fun *gf;
279 Matrix *inv = NULL;
280 Matrix *eq = NULL;
281 Matrix *div = NULL;
282 Polyhedron *PT = P;
284 /* Compute true context */
285 C1 = Polyhedron_Project(P, C->Dimension);
286 C2 = DomainIntersection(C, C1, options->MaxRays);
287 Polyhedron_Free(C1);
289 POL_ENSURE_VERTICES(C2);
290 if (C2->NbBid != 0) {
291 Matrix *CP;
292 if (C2->NbEq || P->NbEq) {
293 /* We remove all equalities to be sure all lines are unit vectors */
294 Polyhedron *CT = C2;
295 remove_all_equalities(&PT, &CT, &CP, NULL, C2->Dimension,
296 options->MaxRays);
297 if (CT != C2) {
298 Polyhedron_Free(C2);
299 C2 = CT;
301 if (CP) {
302 inv = left_inverse(CP, &eq);
303 Matrix_Free(CP);
305 int d = 0;
306 Value tmp;
307 value_init(tmp);
308 div = Matrix_Alloc(inv->NbRows-1, inv->NbColumns+1);
309 for (int i = 0; i < inv->NbRows-1; ++i) {
310 Vector_Gcd(inv->p[i], inv->NbColumns, &tmp);
311 if (mpz_divisible_p(tmp,
312 inv->p[inv->NbRows-1][inv->NbColumns-1]))
313 continue;
314 Vector_Copy(inv->p[i], div->p[d], inv->NbColumns);
315 value_assign(div->p[d][inv->NbColumns],
316 inv->p[inv->NbRows-1][inv->NbColumns-1]);
317 ++d;
319 value_clear(tmp);
321 if (!d) {
322 Matrix_Free(div);
323 div = NULL;
324 } else
325 div->NbRows = d;
328 POL_ENSURE_VERTICES(C2);
330 if (C2->NbBid) {
331 Matrix *M, *Minv;
332 remove_lines(C2, &M, &Minv);
333 apply_transformation(&PT, &C2, PT != P, C2 != C, M, Minv, &inv,
334 options);
337 POL_ENSURE_VERTICES(C2);
338 if (!Polyhedron_has_revlex_positive_rays(C2, C2->Dimension)) {
339 Matrix *Constraints;
340 Matrix *H, *Q, *U;
341 Constraints = Matrix_Alloc(C2->NbConstraints, C2->Dimension+1);
342 for (int i = 0; i < C2->NbConstraints; ++i)
343 Vector_Copy(C2->Constraint[i]+1, Constraints->p[i], C2->Dimension);
344 left_hermite(Constraints, &H, &Q, &U);
345 Matrix_Free(Constraints);
346 /* flip rows of Q */
347 for (int i = 0; i < C2->Dimension/2; ++i)
348 Vector_Exchange(Q->p[i], Q->p[C2->Dimension-1-i], C2->Dimension);
349 Matrix_Free(H);
350 Matrix_Free(U);
351 Matrix *M = Matrix_Alloc(C2->Dimension+1, C2->Dimension+1);
352 U = Matrix_Copy(Q);
353 int ok = Matrix_Inverse(U, M);
354 assert(ok);
355 Matrix_Free(U);
357 apply_transformation(&PT, &C2, PT != P, C2 != C, M, Q, &inv, options);
359 gf = barvinok_series_with_options(PT, C2, options);
360 Polyhedron_Free(C2);
361 if (PT != P)
362 Polyhedron_Free(PT);
363 return new skewed_gen_fun(gf, inv, eq, div);
366 int main(int argc, char **argv)
368 Polyhedron *A, *C;
369 Matrix *M;
370 evalue *EP = NULL;
371 skewed_gen_fun *gf = NULL;
372 const char **param_name;
373 int print_solution = 1;
374 int result = 0;
375 struct enumerate_options *options = enumerate_options_new_with_defaults();
377 argc = enumerate_options_parse(options, argc, argv, ISL_ARG_ALL);
379 M = Matrix_Read();
380 assert(M);
381 A = Constraints2Polyhedron(M, options->verify->barvinok->MaxRays);
382 Matrix_Free(M);
383 M = Matrix_Read();
384 assert(M);
385 C = Constraints2Polyhedron(M, options->verify->barvinok->MaxRays);
386 Matrix_Free(M);
387 assert(A->Dimension >= C->Dimension);
388 param_name = Read_ParamNames(stdin, C->Dimension);
390 if (options->verify->verify) {
391 verify_options_set_range(options->verify, A->Dimension);
392 if (!options->verify->barvinok->verbose)
393 print_solution = 0;
396 if (print_solution && options->verify->barvinok->verbose) {
397 Polyhedron_Print(stdout, P_VALUE_FMT, A);
398 Polyhedron_Print(stdout, P_VALUE_FMT, C);
401 if (options->series) {
402 gf = series(A, C, options->verify->barvinok);
403 if (print_solution) {
404 gf->print(cout, C->Dimension, param_name);
405 puts("");
407 if (options->function) {
408 EP = *gf;
409 if (print_solution)
410 print_evalue(stdout, EP, param_name);
412 } else {
413 EP = barvinok_enumerate_with_options(A, C, options->verify->barvinok);
414 assert(EP);
415 if (evalue_convert(EP, options->convert, options->verify->barvinok->verbose,
416 C->Dimension, param_name))
417 print_solution = 0;
418 if (options->size)
419 printf("\nSize: %zd\n", evalue_size(EP));
420 if (print_solution)
421 print_evalue(stdout, EP, param_name);
424 if (options->verify->verify) {
425 options->verify->params = param_name;
426 result = verify(A, C, EP, gf, options);
429 if (gf)
430 delete gf;
431 if (EP)
432 evalue_free(EP);
434 if (options->verify->barvinok->print_stats)
435 barvinok_stats_print(options->verify->barvinok->stats, stdout);
437 Free_ParamNames(param_name, C->Dimension);
438 Polyhedron_Free(A);
439 Polyhedron_Free(C);
440 enumerate_options_free(options);
441 return result;