laurent.cc: laurent_summator: member initializer list: put base classes first
[barvinok.git] / barvinok_enumerate.cc
blobf336a0fa7fbafa202a1bd4e9a7476e0179448999
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 isl_set_free(set);
105 isl_val_free(v);
106 isl_val_free(n);
107 isl_point_free(pnt);
109 if (!ok)
110 vpe->vpd.error = 1;
112 if (vpe->vpd.options->continue_on_error)
113 ok = 1;
115 return (vpe->vpd.n >= 1 && ok) ? isl_stat_ok : isl_stat_error;
118 static int verify_isl(Polyhedron *P, Polyhedron *C,
119 evalue *EP, const struct verify_options *options)
121 struct verify_point_enum vpe = { { options } };
122 int i;
123 isl_ctx *ctx = isl_ctx_alloc();
124 isl_space *dim;
125 isl_set *set;
126 isl_set *set_C;
127 int r;
129 dim = isl_space_set_alloc(ctx, C->Dimension, P->Dimension - C->Dimension);
130 for (i = 0; i < C->Dimension; ++i)
131 dim = isl_space_set_dim_name(dim, isl_dim_param, i, options->params[i]);
132 set = isl_set_new_from_polylib(P, isl_space_copy(dim));
133 dim = isl_space_params(dim);
134 set_C = isl_set_new_from_polylib(C, dim);
135 set_C = isl_set_intersect_params(isl_set_copy(set), set_C);
136 set_C = isl_set_params(set_C);
138 set_C = verify_context_set_bounds(set_C, options);
140 r = verify_point_data_init(&vpe.vpd, set_C);
142 vpe.set = set;
143 vpe.pwqp = isl_pw_qpolynomial_from_evalue(isl_set_get_space(set_C), EP);
144 if (r == 0)
145 isl_set_foreach_point(set_C, verify_point, &vpe);
146 if (vpe.vpd.error)
147 r = -1;
149 isl_pw_qpolynomial_free(vpe.pwqp);
150 isl_set_free(set);
151 isl_set_free(set_C);
153 isl_ctx_free(ctx);
155 verify_point_data_fini(&vpe.vpd);
157 return r;
160 static int verify(Polyhedron *P, Polyhedron *C, evalue *EP, skewed_gen_fun *gf,
161 struct enumerate_options *options)
163 Polyhedron *CS, *S;
164 Vector *p;
165 int result = 0;
167 if (!options->series || options->function)
168 return verify_isl(P, C, EP, options->verify);
170 CS = check_poly_context_scan(P, &C, C->Dimension, options->verify);
172 p = Vector_Alloc(P->Dimension+2);
173 value_set_si(p->p[P->Dimension+1], 1);
175 /* S = scanning list of polyhedra */
176 S = Polyhedron_Scan(P, C, options->verify->barvinok->MaxRays);
178 check_poly_init(C, options->verify);
180 /******* CHECK NOW *********/
181 if (S) {
182 if (!check_poly_gf(S, CS, gf, 0, C->Dimension, 0, p->p,
183 options->verify))
184 result = -1;
185 Domain_Free(S);
188 if (result == -1)
189 fprintf(stderr,"Check failed !\n");
191 if (!options->verify->print_all)
192 printf( "\n" );
194 Vector_Free(p);
195 if (CS) {
196 Domain_Free(CS);
197 Domain_Free(C);
200 return result;
203 /* frees M and Minv */
204 static void apply_transformation(Polyhedron **P, Polyhedron **C,
205 bool free_P, bool free_C,
206 Matrix *M, Matrix *Minv, Matrix **inv,
207 barvinok_options *options)
209 Polyhedron *T;
210 Matrix *M2;
212 M2 = align_matrix(M, (*P)->Dimension + 1);
213 T = *P;
214 *P = Polyhedron_Preimage(*P, M2, options->MaxRays);
215 if (free_P)
216 Polyhedron_Free(T);
217 Matrix_Free(M2);
219 T = *C;
220 *C = Polyhedron_Preimage(*C, M, options->MaxRays);
221 if (free_C)
222 Polyhedron_Free(T);
224 Matrix_Free(M);
226 if (*inv) {
227 Matrix *T = *inv;
228 *inv = Matrix_Alloc(Minv->NbRows, T->NbColumns);
229 Matrix_Product(Minv, T, *inv);
230 Matrix_Free(T);
231 Matrix_Free(Minv);
232 } else
233 *inv = Minv;
236 /* Since we have "compressed" the parameters (in case there were
237 * any equalities), the result is independent of the coordinates in the
238 * coordinate subspace spanned by the lines. We can therefore assume
239 * these coordinates are zero and compute the inverse image of the map
240 * from a lower dimensional space that adds zeros in the appropriate
241 * places.
243 static void remove_lines(Polyhedron *C, Matrix **M, Matrix **Minv)
245 Matrix *L = Matrix_Alloc(C->Dimension+1, C->Dimension+1);
246 for (int r = 0; r < C->NbBid; ++r)
247 Vector_Copy(C->Ray[r]+1, L->p[r], C->Dimension);
248 unimodular_complete(L, C->NbBid);
249 assert(value_one_p(L->p[C->Dimension][C->Dimension]));
250 assert(First_Non_Zero(L->p[C->Dimension], C->Dimension) == -1);
251 Matrix_Transposition(L);
252 assert(First_Non_Zero(L->p[C->Dimension], C->Dimension) == -1);
254 *M = Matrix_Alloc(C->Dimension+1, C->Dimension-C->NbBid+1);
255 for (int i = 0; i < C->Dimension+1; ++i)
256 Vector_Copy(L->p[i]+C->NbBid, (*M)->p[i], C->Dimension-C->NbBid+1);
258 Matrix *Linv = Matrix_Alloc(C->Dimension+1, C->Dimension+1);
259 int ok = Matrix_Inverse(L, Linv);
260 assert(ok);
261 Matrix_Free(L);
263 *Minv = Matrix_Alloc(C->Dimension-C->NbBid+1, C->Dimension+1);
264 for (int i = C->NbBid; i < C->Dimension+1; ++i)
265 Vector_AntiScale(Linv->p[i], (*Minv)->p[i-C->NbBid],
266 Linv->p[C->Dimension][C->Dimension], C->Dimension+1);
267 Matrix_Free(Linv);
270 static skewed_gen_fun *series(Polyhedron *P, Polyhedron* C,
271 barvinok_options *options)
273 Polyhedron *C1, *C2;
274 gen_fun *gf;
275 Matrix *inv = NULL;
276 Matrix *eq = NULL;
277 Matrix *div = NULL;
278 Polyhedron *PT = P;
280 /* Compute true context */
281 C1 = Polyhedron_Project(P, C->Dimension);
282 C2 = DomainIntersection(C, C1, options->MaxRays);
283 Polyhedron_Free(C1);
285 POL_ENSURE_VERTICES(C2);
286 if (C2->NbBid != 0) {
287 Matrix *CP;
288 if (C2->NbEq || P->NbEq) {
289 /* We remove all equalities to be sure all lines are unit vectors */
290 Polyhedron *CT = C2;
291 remove_all_equalities(&PT, &CT, &CP, NULL, C2->Dimension,
292 options->MaxRays);
293 if (CT != C2) {
294 Polyhedron_Free(C2);
295 C2 = CT;
297 if (CP) {
298 inv = left_inverse(CP, &eq);
299 Matrix_Free(CP);
301 int d = 0;
302 Value tmp;
303 value_init(tmp);
304 div = Matrix_Alloc(inv->NbRows-1, inv->NbColumns+1);
305 for (int i = 0; i < inv->NbRows-1; ++i) {
306 Vector_Gcd(inv->p[i], inv->NbColumns, &tmp);
307 if (mpz_divisible_p(tmp,
308 inv->p[inv->NbRows-1][inv->NbColumns-1]))
309 continue;
310 Vector_Copy(inv->p[i], div->p[d], inv->NbColumns);
311 value_assign(div->p[d][inv->NbColumns],
312 inv->p[inv->NbRows-1][inv->NbColumns-1]);
313 ++d;
315 value_clear(tmp);
317 if (!d) {
318 Matrix_Free(div);
319 div = NULL;
320 } else
321 div->NbRows = d;
324 POL_ENSURE_VERTICES(C2);
326 if (C2->NbBid) {
327 Matrix *M, *Minv;
328 remove_lines(C2, &M, &Minv);
329 apply_transformation(&PT, &C2, PT != P, C2 != C, M, Minv, &inv,
330 options);
333 POL_ENSURE_VERTICES(C2);
334 if (!Polyhedron_has_revlex_positive_rays(C2, C2->Dimension)) {
335 Matrix *Constraints;
336 Matrix *H, *Q, *U;
337 Constraints = Matrix_Alloc(C2->NbConstraints, C2->Dimension+1);
338 for (int i = 0; i < C2->NbConstraints; ++i)
339 Vector_Copy(C2->Constraint[i]+1, Constraints->p[i], C2->Dimension);
340 left_hermite(Constraints, &H, &Q, &U);
341 Matrix_Free(Constraints);
342 /* flip rows of Q */
343 for (int i = 0; i < C2->Dimension/2; ++i)
344 Vector_Exchange(Q->p[i], Q->p[C2->Dimension-1-i], C2->Dimension);
345 Matrix_Free(H);
346 Matrix_Free(U);
347 Matrix *M = Matrix_Alloc(C2->Dimension+1, C2->Dimension+1);
348 U = Matrix_Copy(Q);
349 int ok = Matrix_Inverse(U, M);
350 assert(ok);
351 Matrix_Free(U);
353 apply_transformation(&PT, &C2, PT != P, C2 != C, M, Q, &inv, options);
355 gf = barvinok_series_with_options(PT, C2, options);
356 Polyhedron_Free(C2);
357 if (PT != P)
358 Polyhedron_Free(PT);
359 return new skewed_gen_fun(gf, inv, eq, div);
362 int main(int argc, char **argv)
364 Polyhedron *A, *C;
365 Matrix *M;
366 evalue *EP = NULL;
367 skewed_gen_fun *gf = NULL;
368 const char **param_name;
369 int print_solution = 1;
370 int result = 0;
371 struct enumerate_options *options = enumerate_options_new_with_defaults();
373 argc = enumerate_options_parse(options, argc, argv, ISL_ARG_ALL);
375 M = Matrix_Read();
376 assert(M);
377 A = Constraints2Polyhedron(M, options->verify->barvinok->MaxRays);
378 Matrix_Free(M);
379 M = Matrix_Read();
380 assert(M);
381 C = Constraints2Polyhedron(M, options->verify->barvinok->MaxRays);
382 Matrix_Free(M);
383 assert(A->Dimension >= C->Dimension);
384 param_name = Read_ParamNames(stdin, C->Dimension);
386 if (options->verify->verify) {
387 verify_options_set_range(options->verify, A->Dimension);
388 if (!options->verify->barvinok->verbose)
389 print_solution = 0;
392 if (print_solution && options->verify->barvinok->verbose) {
393 Polyhedron_Print(stdout, P_VALUE_FMT, A);
394 Polyhedron_Print(stdout, P_VALUE_FMT, C);
397 if (options->series) {
398 gf = series(A, C, options->verify->barvinok);
399 if (print_solution) {
400 gf->print(cout, C->Dimension, param_name);
401 puts("");
403 if (options->function) {
404 EP = *gf;
405 if (print_solution)
406 print_evalue(stdout, EP, param_name);
408 } else {
409 EP = barvinok_enumerate_with_options(A, C, options->verify->barvinok);
410 assert(EP);
411 if (evalue_convert(EP, options->convert, options->verify->barvinok->verbose,
412 C->Dimension, param_name))
413 print_solution = 0;
414 if (options->size)
415 printf("\nSize: %zd\n", evalue_size(EP));
416 if (print_solution)
417 print_evalue(stdout, EP, param_name);
420 if (options->verify->verify) {
421 options->verify->params = param_name;
422 result = verify(A, C, EP, gf, options);
425 if (gf)
426 delete gf;
427 if (EP)
428 evalue_free(EP);
430 if (options->verify->barvinok->print_stats)
431 barvinok_stats_print(options->verify->barvinok->stats, stdout);
433 Free_ParamNames(param_name, C->Dimension);
434 Polyhedron_Free(A);
435 Polyhedron_Free(C);
436 enumerate_options_free(options);
437 return result;