update isl to version 0.11
[barvinok.git] / util.c
blob14665609dde2d3e6550139afbed0a3bb552655d0
1 #include <stdlib.h>
2 #include <assert.h>
3 #include <isl_set_polylib.h>
4 #include <barvinok/util.h>
5 #include <barvinok/options.h>
6 #include <polylib/ranking.h>
7 #include "config.h"
8 #include "lattice_point.h"
10 #define ALLOC(type) (type*)malloc(sizeof(type))
11 #define ALLOCN(type,n) (type*)malloc((n) * sizeof(type))
13 #ifdef __GNUC__
14 #define NALLOC(p,n) p = (typeof(p))malloc((n) * sizeof(*p))
15 #else
16 #define NALLOC(p,n) p = (void *)malloc((n) * sizeof(*p))
17 #endif
19 void manual_count(Polyhedron *P, Value* result)
21 isl_ctx *ctx = isl_ctx_alloc();
22 isl_space *dim;
23 isl_set *set;
24 isl_int v;
25 int nvar = P->Dimension;
26 int r;
28 dim = isl_space_set_alloc(ctx, 0, nvar);
29 set = isl_set_new_from_polylib(P, dim);
31 isl_int_init(v);
32 r = isl_set_count(set, &v);
33 isl_int_get_gmp(v, *result);
34 isl_int_clear(v);
36 isl_set_free(set);
37 isl_ctx_free(ctx);
39 assert(r >= 0);
42 #include <barvinok/evalue.h>
43 #include <barvinok/util.h>
44 #include <barvinok/barvinok.h>
46 /* Return random value between 0 and max-1 inclusive
48 int random_int(int max) {
49 return (int) (((double)(max))*rand()/(RAND_MAX+1.0));
52 Polyhedron *Polyhedron_Read(unsigned MaxRays)
54 int vertices = 0;
55 unsigned NbRows, NbColumns;
56 Matrix *M;
57 Polyhedron *P;
58 char s[128];
60 while (fgets(s, sizeof(s), stdin)) {
61 if (*s == '#')
62 continue;
63 if (strncasecmp(s, "vertices", sizeof("vertices")-1) == 0)
64 vertices = 1;
65 if (sscanf(s, "%u %u", &NbRows, &NbColumns) == 2)
66 break;
68 if (feof(stdin))
69 return NULL;
70 M = Matrix_Alloc(NbRows,NbColumns);
71 Matrix_Read_Input(M);
72 if (vertices)
73 P = Rays2Polyhedron(M, MaxRays);
74 else
75 P = Constraints2Polyhedron(M, MaxRays);
76 Matrix_Free(M);
77 return P;
80 /* Inplace polarization
82 void Polyhedron_Polarize(Polyhedron *P)
84 unsigned NbRows = P->NbConstraints + P->NbRays;
85 int i;
86 Value **q;
88 q = (Value **)malloc(NbRows * sizeof(Value *));
89 assert(q);
90 for (i = 0; i < P->NbRays; ++i)
91 q[i] = P->Ray[i];
92 for (; i < NbRows; ++i)
93 q[i] = P->Constraint[i-P->NbRays];
94 P->NbConstraints = NbRows - P->NbConstraints;
95 P->NbRays = NbRows - P->NbRays;
96 free(P->Constraint);
97 P->Constraint = q;
98 P->Ray = q + P->NbConstraints;
102 * Rather general polar
103 * We can optimize it significantly if we assume that
104 * P includes zero
106 * Also, we calculate the polar as defined in Schrijver
107 * The opposite should probably work as well and would
108 * eliminate the need for multiplying by -1
110 Polyhedron* Polyhedron_Polar(Polyhedron *P, unsigned NbMaxRays)
112 int i;
113 Value mone;
114 unsigned dim = P->Dimension + 2;
115 Matrix *M = Matrix_Alloc(P->NbRays, dim);
117 assert(M);
118 value_init(mone);
119 value_set_si(mone, -1);
120 for (i = 0; i < P->NbRays; ++i) {
121 Vector_Scale(P->Ray[i], M->p[i], mone, dim);
122 value_multiply(M->p[i][0], M->p[i][0], mone);
123 value_multiply(M->p[i][dim-1], M->p[i][dim-1], mone);
125 P = Constraints2Polyhedron(M, NbMaxRays);
126 assert(P);
127 Matrix_Free(M);
128 value_clear(mone);
129 return P;
133 * Returns the supporting cone of P at the vertex with index v
135 Polyhedron* supporting_cone(Polyhedron *P, int v)
137 Matrix *M;
138 Value tmp;
139 int i, n, j;
140 unsigned char *supporting = (unsigned char *)malloc(P->NbConstraints);
141 unsigned dim = P->Dimension + 2;
143 assert(v >=0 && v < P->NbRays);
144 assert(value_pos_p(P->Ray[v][dim-1]));
145 assert(supporting);
147 value_init(tmp);
148 for (i = 0, n = 0; i < P->NbConstraints; ++i) {
149 Inner_Product(P->Constraint[i] + 1, P->Ray[v] + 1, dim - 1, &tmp);
150 if ((supporting[i] = value_zero_p(tmp)))
151 ++n;
153 assert(n >= dim - 2);
154 value_clear(tmp);
155 M = Matrix_Alloc(n, dim);
156 assert(M);
157 for (i = 0, j = 0; i < P->NbConstraints; ++i)
158 if (supporting[i]) {
159 value_set_si(M->p[j][dim-1], 0);
160 Vector_Copy(P->Constraint[i], M->p[j++], dim-1);
162 free(supporting);
163 P = Constraints2Polyhedron(M, P->NbRays+1);
164 assert(P);
165 Matrix_Free(M);
166 return P;
169 #define INT_BITS (sizeof(unsigned) * 8)
171 unsigned *supporting_constraints(Matrix *Constraints, Param_Vertices *v, int *n)
173 Value lcm, tmp, tmp2;
174 unsigned dim = Constraints->NbColumns;
175 unsigned nparam = v->Vertex->NbColumns - 2;
176 unsigned nvar = dim - nparam - 2;
177 int len = (Constraints->NbRows+INT_BITS-1)/INT_BITS;
178 unsigned *supporting = (unsigned *)calloc(len, sizeof(unsigned));
179 int i, j;
180 Vector *row;
181 int ix;
182 unsigned bx;
184 assert(supporting);
185 row = Vector_Alloc(nparam+1);
186 assert(row);
187 value_init(lcm);
188 value_init(tmp);
189 value_init(tmp2);
190 value_set_si(lcm, 1);
191 for (i = 0, *n = 0, ix = 0, bx = MSB; i < Constraints->NbRows; ++i) {
192 Vector_Set(row->p, 0, nparam+1);
193 for (j = 0 ; j < nvar; ++j) {
194 value_set_si(tmp, 1);
195 value_assign(tmp2, Constraints->p[i][j+1]);
196 if (value_ne(lcm, v->Vertex->p[j][nparam+1])) {
197 value_assign(tmp, lcm);
198 value_lcm(lcm, lcm, v->Vertex->p[j][nparam+1]);
199 value_division(tmp, lcm, tmp);
200 value_multiply(tmp2, tmp2, lcm);
201 value_division(tmp2, tmp2, v->Vertex->p[j][nparam+1]);
203 Vector_Combine(row->p, v->Vertex->p[j], row->p,
204 tmp, tmp2, nparam+1);
206 value_set_si(tmp, 1);
207 Vector_Combine(row->p, Constraints->p[i]+1+nvar, row->p, tmp, lcm, nparam+1);
208 for (j = 0; j < nparam+1; ++j)
209 if (value_notzero_p(row->p[j]))
210 break;
211 if (j == nparam + 1) {
212 supporting[ix] |= bx;
213 ++*n;
215 NEXT(ix, bx);
217 assert(*n >= nvar);
218 value_clear(tmp);
219 value_clear(tmp2);
220 value_clear(lcm);
221 Vector_Free(row);
223 return supporting;
226 Polyhedron* supporting_cone_p(Polyhedron *P, Param_Vertices *v)
228 Matrix *M;
229 unsigned dim = P->Dimension + 2;
230 unsigned nparam = v->Vertex->NbColumns - 2;
231 unsigned nvar = dim - nparam - 2;
232 int i, n, j;
233 int ix;
234 unsigned bx;
235 unsigned *supporting;
236 Matrix View;
238 Polyhedron_Matrix_View(P, &View, P->NbConstraints);
239 supporting = supporting_constraints(&View, v, &n);
240 M = Matrix_Alloc(n, nvar+2);
241 assert(M);
242 for (i = 0, j = 0, ix = 0, bx = MSB; i < P->NbConstraints; ++i) {
243 if (supporting[ix] & bx) {
244 value_set_si(M->p[j][nvar+1], 0);
245 Vector_Copy(P->Constraint[i], M->p[j++], nvar+1);
247 NEXT(ix, bx);
249 free(supporting);
250 P = Constraints2Polyhedron(M, P->NbRays+1);
251 assert(P);
252 Matrix_Free(M);
253 return P;
256 Polyhedron* triangulate_cone(Polyhedron *P, unsigned NbMaxCons)
258 struct barvinok_options *options = barvinok_options_new_with_defaults();
259 options->MaxRays = NbMaxCons;
260 P = triangulate_cone_with_options(P, options);
261 barvinok_options_free(options);
262 return P;
265 Polyhedron* triangulate_cone_with_options(Polyhedron *P,
266 struct barvinok_options *options)
268 const static int MAX_TRY=10;
269 int i, j, r, n, t;
270 Value tmp;
271 unsigned dim = P->Dimension;
272 Matrix *M = Matrix_Alloc(P->NbRays+1, dim+3);
273 Matrix *M2, *M3;
274 Polyhedron *L, *R, *T;
275 assert(P->NbEq == 0);
277 L = NULL;
278 R = NULL;
279 value_init(tmp);
281 Vector_Set(M->p[0]+1, 0, dim+1);
282 value_set_si(M->p[0][0], 1);
283 value_set_si(M->p[0][dim+2], 1);
284 Vector_Set(M->p[P->NbRays]+1, 0, dim+2);
285 value_set_si(M->p[P->NbRays][0], 1);
286 value_set_si(M->p[P->NbRays][dim+1], 1);
288 for (i = 0, r = 1; i < P->NbRays; ++i) {
289 if (value_notzero_p(P->Ray[i][dim+1]))
290 continue;
291 Vector_Copy(P->Ray[i], M->p[r], dim+1);
292 value_set_si(M->p[r][dim+2], 0);
293 ++r;
296 M2 = Matrix_Alloc(dim+1, dim+2);
298 t = 0;
299 if (options->try_Delaunay_triangulation) {
300 /* Delaunay triangulation */
301 for (r = 1; r < P->NbRays; ++r) {
302 Inner_Product(M->p[r]+1, M->p[r]+1, dim, &tmp);
303 value_assign(M->p[r][dim+1], tmp);
305 M3 = Matrix_Copy(M);
306 L = Rays2Polyhedron(M3, options->MaxRays);
307 Matrix_Free(M3);
308 ++t;
309 } else {
310 try_again:
311 /* Usually R should still be 0 */
312 Domain_Free(R);
313 Polyhedron_Free(L);
314 for (r = 1; r < P->NbRays; ++r) {
315 value_set_si(M->p[r][dim+1], random_int((t+1)*dim*P->NbRays)+1);
317 M3 = Matrix_Copy(M);
318 L = Rays2Polyhedron(M3, options->MaxRays);
319 Matrix_Free(M3);
320 ++t;
322 assert(t <= MAX_TRY);
324 R = NULL;
325 n = 0;
327 POL_ENSURE_FACETS(L);
328 for (i = 0; i < L->NbConstraints; ++i) {
329 /* Ignore perpendicular facets, i.e., facets with 0 z-coordinate */
330 if (value_negz_p(L->Constraint[i][dim+1]))
331 continue;
332 if (value_notzero_p(L->Constraint[i][dim+2]))
333 continue;
334 for (j = 1, r = 1; j < M->NbRows; ++j) {
335 Inner_Product(M->p[j]+1, L->Constraint[i]+1, dim+1, &tmp);
336 if (value_notzero_p(tmp))
337 continue;
338 if (r > dim)
339 goto try_again;
340 Vector_Copy(M->p[j]+1, M2->p[r]+1, dim);
341 value_set_si(M2->p[r][0], 1);
342 value_set_si(M2->p[r][dim+1], 0);
343 ++r;
345 assert(r == dim+1);
346 Vector_Set(M2->p[0]+1, 0, dim);
347 value_set_si(M2->p[0][0], 1);
348 value_set_si(M2->p[0][dim+1], 1);
349 T = Rays2Polyhedron(M2, P->NbConstraints+1);
350 T->next = R;
351 R = T;
352 ++n;
354 Matrix_Free(M2);
356 Polyhedron_Free(L);
357 value_clear(tmp);
358 Matrix_Free(M);
360 return R;
363 void check_triangulization(Polyhedron *P, Polyhedron *T)
365 Polyhedron *C, *D, *E, *F, *G, *U;
366 for (C = T; C; C = C->next) {
367 if (C == T)
368 U = C;
369 else
370 U = DomainConvex(DomainUnion(U, C, 100), 100);
371 for (D = C->next; D; D = D->next) {
372 F = C->next;
373 G = D->next;
374 C->next = NULL;
375 D->next = NULL;
376 E = DomainIntersection(C, D, 600);
377 assert(E->NbRays == 0 || E->NbEq >= 1);
378 Polyhedron_Free(E);
379 C->next = F;
380 D->next = G;
383 assert(PolyhedronIncludes(U, P));
384 assert(PolyhedronIncludes(P, U));
387 /* Computes x, y and g such that g = gcd(a,b) and a*x+b*y = g */
388 void Extended_Euclid(Value a, Value b, Value *x, Value *y, Value *g)
390 Value c, d, e, f, tmp;
392 value_init(c);
393 value_init(d);
394 value_init(e);
395 value_init(f);
396 value_init(tmp);
397 value_absolute(c, a);
398 value_absolute(d, b);
399 value_set_si(e, 1);
400 value_set_si(f, 0);
401 while(value_pos_p(d)) {
402 value_division(tmp, c, d);
403 value_multiply(tmp, tmp, f);
404 value_subtract(e, e, tmp);
405 value_division(tmp, c, d);
406 value_multiply(tmp, tmp, d);
407 value_subtract(c, c, tmp);
408 value_swap(c, d);
409 value_swap(e, f);
411 value_assign(*g, c);
412 if (value_zero_p(a))
413 value_set_si(*x, 0);
414 else if (value_pos_p(a))
415 value_assign(*x, e);
416 else value_oppose(*x, e);
417 if (value_zero_p(b))
418 value_set_si(*y, 0);
419 else {
420 value_multiply(tmp, a, *x);
421 value_subtract(tmp, c, tmp);
422 value_division(*y, tmp, b);
424 value_clear(c);
425 value_clear(d);
426 value_clear(e);
427 value_clear(f);
428 value_clear(tmp);
431 static int unimodular_complete_1(Matrix *m)
433 Value g, b, c, old, tmp;
434 unsigned i, j;
435 int ok;
437 value_init(b);
438 value_init(c);
439 value_init(g);
440 value_init(old);
441 value_init(tmp);
442 value_assign(g, m->p[0][0]);
443 for (i = 1; value_zero_p(g) && i < m->NbColumns; ++i) {
444 for (j = 0; j < m->NbColumns; ++j) {
445 if (j == i-1)
446 value_set_si(m->p[i][j], 1);
447 else
448 value_set_si(m->p[i][j], 0);
450 value_assign(g, m->p[0][i]);
452 for (; i < m->NbColumns; ++i) {
453 value_assign(old, g);
454 Extended_Euclid(old, m->p[0][i], &c, &b, &g);
455 value_oppose(b, b);
456 for (j = 0; j < m->NbColumns; ++j) {
457 if (j < i) {
458 value_multiply(tmp, m->p[0][j], b);
459 value_division(m->p[i][j], tmp, old);
460 } else if (j == i)
461 value_assign(m->p[i][j], c);
462 else
463 value_set_si(m->p[i][j], 0);
466 ok = value_one_p(g);
467 value_clear(b);
468 value_clear(c);
469 value_clear(g);
470 value_clear(old);
471 value_clear(tmp);
472 return ok;
475 int unimodular_complete(Matrix *M, int row)
477 int r;
478 int ok = 1;
479 Matrix *H, *Q, *U;
481 if (row == 1)
482 return unimodular_complete_1(M);
484 left_hermite(M, &H, &Q, &U);
485 Matrix_Free(U);
486 for (r = 0; ok && r < row; ++r)
487 if (value_notone_p(H->p[r][r]))
488 ok = 0;
489 Matrix_Free(H);
490 for (r = row; r < M->NbRows; ++r)
491 Vector_Copy(Q->p[r], M->p[r], M->NbColumns);
492 Matrix_Free(Q);
493 return ok;
497 * left_hermite may leave positive entries below the main diagonal in H.
498 * This function postprocesses the output of left_hermite to make
499 * the non-zero entries below the main diagonal negative.
501 void neg_left_hermite(Matrix *A, Matrix **H_p, Matrix **Q_p, Matrix **U_p)
503 int row, col, i, j;
504 Matrix *H, *U, *Q;
506 left_hermite(A, &H, &Q, &U);
507 *H_p = H;
508 *Q_p = Q;
509 *U_p = U;
511 for (row = 0, col = 0; col < H->NbColumns; ++col, ++row) {
512 while (value_zero_p(H->p[row][col]))
513 ++row;
514 for (i = 0; i < col; ++i) {
515 if (value_negz_p(H->p[row][i]))
516 continue;
518 /* subtract column col from column i in H and U */
519 for (j = 0; j < H->NbRows; ++j)
520 value_subtract(H->p[j][i], H->p[j][i], H->p[j][col]);
521 for (j = 0; j < U->NbRows; ++j)
522 value_subtract(U->p[j][i], U->p[j][i], U->p[j][col]);
524 /* add row i to row col in Q */
525 for (j = 0; j < Q->NbColumns; ++j)
526 value_addto(Q->p[col][j], Q->p[col][j], Q->p[i][j]);
532 * Returns a full-dimensional polyhedron with the same number
533 * of integer points as P
535 Polyhedron *remove_equalities(Polyhedron *P, unsigned MaxRays)
537 Matrix M;
538 Matrix *T;
539 Polyhedron *Q = Polyhedron_Copy(P);
540 unsigned dim = P->Dimension;
542 if (Q->NbEq == 0)
543 return Q;
545 Q = DomainConstraintSimplify(Q, MaxRays);
546 if (emptyQ2(Q))
547 return Q;
549 Polyhedron_Matrix_View(Q, &M, Q->NbEq);
550 T = compress_variables(&M, 0);
552 if (!T)
553 P = NULL;
554 else {
555 P = Polyhedron_Preimage(Q, T, MaxRays);
556 Matrix_Free(T);
559 Polyhedron_Free(Q);
561 return P;
565 * Returns a full-dimensional polyhedron with the same number
566 * of integer points as P
567 * nvar specifies the number of variables
568 * The remaining dimensions are assumed to be parameters
569 * Destroys P
570 * factor is NbEq x (nparam+2) matrix, containing stride constraints
571 * on the parameters; column nparam is the constant;
572 * column nparam+1 is the stride
574 * if factor is NULL, only remove equalities that don't affect
575 * the number of points
577 Polyhedron *remove_equalities_p(Polyhedron *P, unsigned nvar, Matrix **factor,
578 unsigned MaxRays)
580 Value g;
581 Polyhedron *Q;
582 unsigned dim = P->Dimension;
583 Matrix *m1, *m2, *f;
584 int i, j;
586 if (P->NbEq == 0)
587 return P;
589 m1 = Matrix_Alloc(nvar, nvar);
590 P = DomainConstraintSimplify(P, MaxRays);
591 if (factor) {
592 f = Matrix_Alloc(P->NbEq, dim-nvar+2);
593 *factor = f;
595 value_init(g);
596 for (i = 0, j = 0; i < P->NbEq; ++i) {
597 if (First_Non_Zero(P->Constraint[i]+1, nvar) == -1)
598 continue;
600 Vector_Gcd(P->Constraint[i]+1, nvar, &g);
601 if (!factor && value_notone_p(g))
602 continue;
604 if (factor) {
605 Vector_Copy(P->Constraint[i]+1+nvar, f->p[j], dim-nvar+1);
606 value_assign(f->p[j][dim-nvar+1], g);
609 Vector_Copy(P->Constraint[i]+1, m1->p[j], nvar);
611 ++j;
613 value_clear(g);
615 unimodular_complete(m1, j);
617 m2 = Matrix_Alloc(dim+1-j, dim+1);
618 for (i = 0; i < nvar-j ; ++i)
619 Vector_Copy(m1->p[i+j], m2->p[i], nvar);
620 Matrix_Free(m1);
621 for (i = nvar-j; i <= dim-j; ++i)
622 value_set_si(m2->p[i][i+j], 1);
624 Q = Polyhedron_Image(P, m2, MaxRays);
625 Matrix_Free(m2);
626 Polyhedron_Free(P);
628 return Q;
631 void Line_Length(Polyhedron *P, Value *len)
633 Value tmp, pos, neg;
634 int p = 0, n = 0;
635 int i;
637 assert(P->Dimension == 1);
639 if (P->NbEq > 0) {
640 if (mpz_divisible_p(P->Constraint[0][2], P->Constraint[0][1]))
641 value_set_si(*len, 1);
642 else
643 value_set_si(*len, 0);
644 return;
647 value_init(tmp);
648 value_init(pos);
649 value_init(neg);
651 for (i = 0; i < P->NbConstraints; ++i) {
652 value_oppose(tmp, P->Constraint[i][2]);
653 if (value_pos_p(P->Constraint[i][1])) {
654 mpz_cdiv_q(tmp, tmp, P->Constraint[i][1]);
655 if (!p || value_gt(tmp, pos))
656 value_assign(pos, tmp);
657 p = 1;
658 } else if (value_neg_p(P->Constraint[i][1])) {
659 mpz_fdiv_q(tmp, tmp, P->Constraint[i][1]);
660 if (!n || value_lt(tmp, neg))
661 value_assign(neg, tmp);
662 n = 1;
664 if (n && p) {
665 value_subtract(tmp, neg, pos);
666 value_increment(*len, tmp);
667 } else
668 value_set_si(*len, -1);
671 value_clear(tmp);
672 value_clear(pos);
673 value_clear(neg);
676 /* Update group[k] to the group column k belongs to.
677 * When merging two groups, only the group of the current
678 * group leader is changed. Here we change the group of
679 * the other members to also point to the group that the
680 * old group leader now points to.
682 static void update_group(int *group, int *cnt, int k)
684 int g = group[k];
685 while (cnt[g] == 0)
686 g = group[g];
687 group[k] = g;
691 * Factors the polyhedron P into polyhedra Q_i such that
692 * the number of integer points in P is equal to the product
693 * of the number of integer points in the individual Q_i
695 * If no factors can be found, NULL is returned.
696 * Otherwise, a linked list of the factors is returned.
698 * If there are factors and if T is not NULL, then a matrix will be
699 * returned through T expressing the old variables in terms of the
700 * new variables as they appear in the sequence of factors.
702 * The algorithm works by first computing the Hermite normal form
703 * and then grouping columns linked by one or more constraints together,
704 * where a constraints "links" two or more columns if the constraint
705 * has nonzero coefficients in the columns.
707 Polyhedron* Polyhedron_Factor(Polyhedron *P, unsigned nparam, Matrix **T,
708 unsigned NbMaxRays)
710 int i, j, k;
711 Matrix *M, *H, *Q, *U;
712 int *pos; /* for each column: row position of pivot */
713 int *group; /* group to which a column belongs */
714 int *cnt; /* number of columns in the group */
715 int *rowgroup; /* group to which a constraint belongs */
716 int nvar = P->Dimension - nparam;
717 Polyhedron *F = NULL;
719 if (nvar <= 1)
720 return NULL;
722 NALLOC(pos, nvar);
723 NALLOC(group, nvar);
724 NALLOC(cnt, nvar);
725 NALLOC(rowgroup, P->NbConstraints);
727 M = Matrix_Alloc(P->NbConstraints, nvar);
728 for (i = 0; i < P->NbConstraints; ++i)
729 Vector_Copy(P->Constraint[i]+1, M->p[i], nvar);
730 left_hermite(M, &H, &Q, &U);
731 Matrix_Free(M);
732 Matrix_Free(Q);
734 for (i = 0; i < P->NbConstraints; ++i)
735 rowgroup[i] = -1;
736 for (i = 0, j = 0; i < H->NbColumns; ++i) {
737 for ( ; j < H->NbRows; ++j)
738 if (value_notzero_p(H->p[j][i]))
739 break;
740 pos[i] = j;
742 for (i = 0; i < nvar; ++i) {
743 group[i] = i;
744 cnt[i] = 1;
746 for (i = 0; i < H->NbColumns && cnt[0] < nvar; ++i) {
747 if (pos[i] == H->NbRows)
748 continue; /* A line direction */
749 if (rowgroup[pos[i]] == -1)
750 rowgroup[pos[i]] = i;
751 for (j = pos[i]+1; j < H->NbRows; ++j) {
752 if (value_zero_p(H->p[j][i]))
753 continue;
754 if (rowgroup[j] != -1)
755 continue;
756 rowgroup[j] = group[i];
757 for (k = i+1; k < H->NbColumns && j >= pos[k]; ++k) {
758 update_group(group, cnt, k);
759 update_group(group, cnt, i);
760 if (group[k] != group[i] && value_notzero_p(H->p[j][k])) {
761 assert(cnt[group[k]] != 0);
762 assert(cnt[group[i]] != 0);
763 if (group[i] < group[k]) {
764 cnt[group[i]] += cnt[group[k]];
765 cnt[group[k]] = 0;
766 group[group[k]] = group[i];
767 } else {
768 cnt[group[k]] += cnt[group[i]];
769 cnt[group[i]] = 0;
770 group[group[i]] = group[k];
777 if (cnt[0] != nvar) {
778 /* Extract out pure context constraints separately */
779 Polyhedron **next = &F;
780 int tot_d = 0;
781 if (T)
782 *T = Matrix_Alloc(nvar, nvar);
783 for (i = nparam ? -1 : 0; i < nvar; ++i) {
784 int d;
786 if (i == -1) {
787 for (j = 0, k = 0; j < P->NbConstraints; ++j)
788 if (rowgroup[j] == -1) {
789 if (First_Non_Zero(P->Constraint[j]+1+nvar,
790 nparam) == -1)
791 rowgroup[j] = -2;
792 else
793 ++k;
795 if (k == 0)
796 continue;
797 d = 0;
798 } else {
799 if (cnt[i] == 0)
800 continue;
801 d = cnt[i];
802 for (j = 0, k = 0; j < P->NbConstraints; ++j)
803 if (rowgroup[j] >= 0 && group[rowgroup[j]] == i) {
804 rowgroup[j] = i;
805 ++k;
809 if (T)
810 for (j = 0; j < nvar; ++j) {
811 int l, m;
812 for (l = 0, m = 0; m < d; ++l) {
813 if (group[l] != i)
814 continue;
815 value_assign((*T)->p[j][tot_d+m++], U->p[j][l]);
819 M = Matrix_Alloc(k, d+nparam+2);
820 for (j = 0, k = 0; j < P->NbConstraints; ++j) {
821 int l, m;
822 if (rowgroup[j] != i)
823 continue;
824 value_assign(M->p[k][0], P->Constraint[j][0]);
825 for (l = 0, m = 0; m < d; ++l) {
826 if (group[l] != i)
827 continue;
828 value_assign(M->p[k][1+m++], H->p[j][l]);
830 Vector_Copy(P->Constraint[j]+1+nvar, M->p[k]+1+m, nparam+1);
831 ++k;
833 *next = Constraints2Polyhedron(M, NbMaxRays);
834 next = &(*next)->next;
835 Matrix_Free(M);
836 tot_d += d;
839 Matrix_Free(U);
840 Matrix_Free(H);
841 free(pos);
842 free(group);
843 free(cnt);
844 free(rowgroup);
845 return F;
848 /* Computes the intersection of the contexts of a list of factors */
849 Polyhedron *Factor_Context(Polyhedron *F, unsigned nparam, unsigned MaxRays)
851 Polyhedron *Q;
852 Polyhedron *C = NULL;
854 for (Q = F; Q; Q = Q->next) {
855 Polyhedron *QC = Q;
856 Polyhedron *next = Q->next;
857 Q->next = NULL;
859 if (Q->Dimension != nparam)
860 QC = Polyhedron_Project(Q, nparam);
862 if (!C)
863 C = Q == QC ? Polyhedron_Copy(QC) : QC;
864 else {
865 Polyhedron *C2 = C;
866 C = DomainIntersection(C, QC, MaxRays);
867 Polyhedron_Free(C2);
868 if (QC != Q)
869 Polyhedron_Free(QC);
871 Q->next = next;
873 return C;
877 * Project on final dim dimensions
879 Polyhedron* Polyhedron_Project(Polyhedron *P, int dim)
881 int i;
882 int remove = P->Dimension - dim;
883 Matrix *T;
884 Polyhedron *I;
886 if (P->Dimension == dim)
887 return Polyhedron_Copy(P);
889 T = Matrix_Alloc(dim+1, P->Dimension+1);
890 for (i = 0; i < dim+1; ++i)
891 value_set_si(T->p[i][i+remove], 1);
892 I = Polyhedron_Image(P, T, P->NbConstraints);
893 Matrix_Free(T);
894 return I;
897 /* Constructs a new constraint that ensures that
898 * the first constraint is (strictly) smaller than
899 * the second.
901 static void smaller_constraint(Value *a, Value *b, Value *c, int pos, int shift,
902 int len, int strict, Value *tmp)
904 value_oppose(*tmp, b[pos+1]);
905 value_set_si(c[0], 1);
906 Vector_Combine(a+1+shift, b+1+shift, c+1, *tmp, a[pos+1], len-shift-1);
907 if (strict)
908 value_decrement(c[len-shift-1], c[len-shift-1]);
909 ConstraintSimplify(c, c, len-shift, tmp);
913 /* For each pair of lower and upper bounds on the first variable,
914 * calls fn with the set of constraints on the remaining variables
915 * where these bounds are active, i.e., (stricly) larger/smaller than
916 * the other lower/upper bounds, the lower and upper bound and the
917 * call back data.
919 * If the first variable is equal to an affine combination of the
920 * other variables then fn is called with both lower and upper
921 * pointing to the corresponding equality.
923 * If there is no lower (or upper) bound, then NULL is passed
924 * as the corresponding bound.
926 void for_each_lower_upper_bound(Polyhedron *P,
927 for_each_lower_upper_bound_init init,
928 for_each_lower_upper_bound_fn fn,
929 void *cb_data)
931 unsigned dim = P->Dimension;
932 Matrix *M;
933 int *pos;
934 int i, j, p, n, z;
935 int k, l, k2, l2, q;
936 Value g;
938 if (value_zero_p(P->Constraint[0][0]) &&
939 value_notzero_p(P->Constraint[0][1])) {
940 M = Matrix_Alloc(P->NbConstraints-1, dim-1+2);
941 for (i = 1; i < P->NbConstraints; ++i) {
942 value_assign(M->p[i-1][0], P->Constraint[i][0]);
943 Vector_Copy(P->Constraint[i]+2, M->p[i-1]+1, dim);
945 if (init)
946 init(1, cb_data);
947 fn(M, P->Constraint[0], P->Constraint[0], cb_data);
948 Matrix_Free(M);
949 return;
952 value_init(g);
953 pos = ALLOCN(int, P->NbConstraints);
955 for (i = 0, z = 0; i < P->NbConstraints; ++i)
956 if (value_zero_p(P->Constraint[i][1]))
957 pos[P->NbConstraints-1 - z++] = i;
958 /* put those with positive coefficients first; number: p */
959 for (i = 0, p = 0, n = P->NbConstraints-z-1; i < P->NbConstraints; ++i)
960 if (value_pos_p(P->Constraint[i][1]))
961 pos[p++] = i;
962 else if (value_neg_p(P->Constraint[i][1]))
963 pos[n--] = i;
964 n = P->NbConstraints-z-p;
966 if (init)
967 init(p*n, cb_data);
969 M = Matrix_Alloc((p ? p-1 : 0) + (n ? n-1 : 0) + z + 1, dim-1+2);
970 for (i = 0; i < z; ++i) {
971 value_assign(M->p[i][0], P->Constraint[pos[P->NbConstraints-1 - i]][0]);
972 Vector_Copy(P->Constraint[pos[P->NbConstraints-1 - i]]+2,
973 M->p[i]+1, dim);
975 for (k = p ? 0 : -1; k < p; ++k) {
976 for (k2 = 0; k2 < p; ++k2) {
977 if (k2 == k)
978 continue;
979 q = 1 + z + k2 - (k2 > k);
980 smaller_constraint(
981 P->Constraint[pos[k]],
982 P->Constraint[pos[k2]],
983 M->p[q], 0, 1, dim+2, k2 > k, &g);
985 for (l = n ? p : p-1; l < p+n; ++l) {
986 Value *lower;
987 Value *upper;
988 for (l2 = p; l2 < p+n; ++l2) {
989 if (l2 == l)
990 continue;
991 q = 1 + z + l2-1 - (l2 > l);
992 smaller_constraint(
993 P->Constraint[pos[l2]],
994 P->Constraint[pos[l]],
995 M->p[q], 0, 1, dim+2, l2 > l, &g);
997 if (p && n)
998 smaller_constraint(P->Constraint[pos[k]],
999 P->Constraint[pos[l]],
1000 M->p[z], 0, 1, dim+2, 0, &g);
1001 lower = p ? P->Constraint[pos[k]] : NULL;
1002 upper = n ? P->Constraint[pos[l]] : NULL;
1003 fn(M, lower, upper, cb_data);
1006 Matrix_Free(M);
1008 free(pos);
1009 value_clear(g);
1012 struct section { Polyhedron * D; evalue E; };
1014 struct PLL_data {
1015 int nd;
1016 unsigned MaxRays;
1017 Polyhedron *C;
1018 evalue mone;
1019 struct section *s;
1022 static void PLL_init(unsigned n, void *cb_data)
1024 struct PLL_data *data = (struct PLL_data *)cb_data;
1026 data->s = ALLOCN(struct section, n);
1029 /* Computes ceil(-coef/abs(d)) */
1030 static evalue* bv_ceil3(Value *coef, int len, Value d, Polyhedron *P)
1032 Value t;
1033 evalue *EP, *f;
1034 Vector *val = Vector_Alloc(len);
1036 value_init(t);
1037 Vector_Oppose(coef, val->p, len);
1038 value_absolute(t, d);
1040 EP = ceiling(val->p, t, len-1, P);
1042 value_clear(t);
1043 Vector_Free(val);
1045 return EP;
1048 static void PLL_cb(Matrix *M, Value *lower, Value *upper, void *cb_data)
1050 struct PLL_data *data = (struct PLL_data *)cb_data;
1051 unsigned dim = M->NbColumns-1;
1052 Matrix *M2;
1053 Polyhedron *T;
1054 evalue *L, *U;
1056 assert(lower);
1057 assert(upper);
1059 M2 = Matrix_Copy(M);
1060 T = Constraints2Polyhedron(M2, data->MaxRays);
1061 Matrix_Free(M2);
1062 data->s[data->nd].D = DomainIntersection(T, data->C, data->MaxRays);
1063 Domain_Free(T);
1065 POL_ENSURE_VERTICES(data->s[data->nd].D);
1066 if (emptyQ(data->s[data->nd].D)) {
1067 Polyhedron_Free(data->s[data->nd].D);
1068 return;
1070 L = bv_ceil3(lower+1+1, dim-1+1, lower[0+1], data->s[data->nd].D);
1071 U = bv_ceil3(upper+1+1, dim-1+1, upper[0+1], data->s[data->nd].D);
1072 eadd(L, U);
1073 eadd(&data->mone, U);
1074 emul(&data->mone, U);
1075 data->s[data->nd].E = *U;
1076 evalue_free(L);
1077 free(U);
1078 ++data->nd;
1081 static evalue *ParamLine_Length_mod(Polyhedron *P, Polyhedron *C, unsigned MaxRays)
1083 unsigned dim = P->Dimension;
1084 unsigned nvar = dim - C->Dimension;
1085 struct PLL_data data;
1086 evalue *F;
1087 int k;
1089 assert(nvar == 1);
1091 value_init(data.mone.d);
1092 evalue_set_si(&data.mone, -1, 1);
1094 data.nd = 0;
1095 data.MaxRays = MaxRays;
1096 data.C = C;
1097 for_each_lower_upper_bound(P, PLL_init, PLL_cb, &data);
1099 free_evalue_refs(&data.mone);
1101 if (data.nd == 0) {
1102 free(data.s);
1103 return evalue_zero();
1106 F = ALLOC(evalue);
1107 value_init(F->d);
1108 value_set_si(F->d, 0);
1109 F->x.p = new_enode(partition, 2*data.nd, dim-nvar);
1110 for (k = 0; k < data.nd; ++k) {
1111 EVALUE_SET_DOMAIN(F->x.p->arr[2*k], data.s[k].D);
1112 value_clear(F->x.p->arr[2*k+1].d);
1113 F->x.p->arr[2*k+1] = data.s[k].E;
1115 free(data.s);
1117 return F;
1120 evalue* ParamLine_Length(Polyhedron *P, Polyhedron *C,
1121 struct barvinok_options *options)
1123 evalue* tmp;
1124 tmp = ParamLine_Length_mod(P, C, options->MaxRays);
1125 if (options->lookup_table) {
1126 evalue_mod2table(tmp, C->Dimension);
1127 reduce_evalue(tmp);
1129 return tmp;
1132 Bool isIdentity(Matrix *M)
1134 unsigned i, j;
1135 if (M->NbRows != M->NbColumns)
1136 return False;
1138 for (i = 0;i < M->NbRows; i ++)
1139 for (j = 0; j < M->NbColumns; j ++)
1140 if (i == j) {
1141 if(value_notone_p(M->p[i][j]))
1142 return False;
1143 } else {
1144 if(value_notzero_p(M->p[i][j]))
1145 return False;
1147 return True;
1150 void Param_Polyhedron_Print(FILE* DST, Param_Polyhedron *PP,
1151 const char **param_names)
1153 Param_Domain *P;
1154 Param_Vertices *V;
1156 for(P=PP->D;P;P=P->next) {
1158 /* prints current val. dom. */
1159 fprintf(DST, "---------------------------------------\n");
1160 fprintf(DST, "Domain :\n");
1161 Print_Domain(DST, P->Domain, param_names);
1163 /* scan the vertices */
1164 fprintf(DST, "Vertices :\n");
1165 FORALL_PVertex_in_ParamPolyhedron(V,P,PP) {
1167 /* prints each vertex */
1168 Print_Vertex(DST, V->Vertex, param_names);
1169 fprintf(DST, "\n");
1171 END_FORALL_PVertex_in_ParamPolyhedron;
1175 void Enumeration_Print(FILE *Dst, Enumeration *en, const char **params)
1177 for (; en; en = en->next) {
1178 Print_Domain(Dst, en->ValidityDomain, params);
1179 print_evalue(Dst, &en->EP, params);
1183 void Enumeration_Free(Enumeration *en)
1185 Enumeration *ee;
1187 while( en )
1189 free_evalue_refs( &(en->EP) );
1190 Domain_Free( en->ValidityDomain );
1191 ee = en ->next;
1192 free( en );
1193 en = ee;
1197 void Enumeration_mod2table(Enumeration *en, unsigned nparam)
1199 for (; en; en = en->next) {
1200 evalue_mod2table(&en->EP, nparam);
1201 reduce_evalue(&en->EP);
1205 size_t Enumeration_size(Enumeration *en)
1207 size_t s = 0;
1209 for (; en; en = en->next) {
1210 s += domain_size(en->ValidityDomain);
1211 s += evalue_size(&en->EP);
1213 return s;
1216 /* Check whether every set in D2 is included in some set of D1 */
1217 int DomainIncludes(Polyhedron *D1, Polyhedron *D2)
1219 for ( ; D2; D2 = D2->next) {
1220 Polyhedron *P1;
1221 for (P1 = D1; P1; P1 = P1->next)
1222 if (PolyhedronIncludes(P1, D2))
1223 break;
1224 if (!P1)
1225 return 0;
1227 return 1;
1230 int line_minmax(Polyhedron *I, Value *min, Value *max)
1232 int i;
1234 if (I->NbEq >= 1) {
1235 value_oppose(I->Constraint[0][2], I->Constraint[0][2]);
1236 /* There should never be a remainder here */
1237 if (value_pos_p(I->Constraint[0][1]))
1238 mpz_fdiv_q(*min, I->Constraint[0][2], I->Constraint[0][1]);
1239 else
1240 mpz_fdiv_q(*min, I->Constraint[0][2], I->Constraint[0][1]);
1241 value_assign(*max, *min);
1242 } else for (i = 0; i < I->NbConstraints; ++i) {
1243 if (value_zero_p(I->Constraint[i][1])) {
1244 Polyhedron_Free(I);
1245 return 0;
1248 value_oppose(I->Constraint[i][2], I->Constraint[i][2]);
1249 if (value_pos_p(I->Constraint[i][1]))
1250 mpz_cdiv_q(*min, I->Constraint[i][2], I->Constraint[i][1]);
1251 else
1252 mpz_fdiv_q(*max, I->Constraint[i][2], I->Constraint[i][1]);
1254 Polyhedron_Free(I);
1255 return 1;
1258 int DomainContains(Polyhedron *P, Value *list_args, int len,
1259 unsigned MaxRays, int set)
1261 int i;
1262 Value m;
1264 if (P->Dimension == len)
1265 return in_domain(P, list_args);
1267 assert(set); // assume list_args is large enough
1268 assert((P->Dimension - len) % 2 == 0);
1269 value_init(m);
1270 for (i = 0; i < P->Dimension - len; i += 2) {
1271 int j, k;
1272 for (j = 0 ; j < P->NbEq; ++j)
1273 if (value_notzero_p(P->Constraint[j][1+len+i]))
1274 break;
1275 assert(j < P->NbEq);
1276 value_absolute(m, P->Constraint[j][1+len+i]);
1277 k = First_Non_Zero(P->Constraint[j]+1, len);
1278 assert(k != -1);
1279 assert(First_Non_Zero(P->Constraint[j]+1+k+1, len - k - 1) == -1);
1280 mpz_fdiv_q(list_args[len+i], list_args[k], m);
1281 mpz_fdiv_r(list_args[len+i+1], list_args[k], m);
1283 value_clear(m);
1285 return in_domain(P, list_args);
1288 Polyhedron *DomainConcat(Polyhedron *head, Polyhedron *tail)
1290 Polyhedron *S;
1291 if (!head)
1292 return tail;
1293 for (S = head; S->next; S = S->next)
1295 S->next = tail;
1296 return head;
1299 evalue *barvinok_lexsmaller_ev(Polyhedron *P, Polyhedron *D, unsigned dim,
1300 Polyhedron *C, unsigned MaxRays)
1302 evalue *ranking;
1303 Polyhedron *RC, *RD, *Q;
1304 unsigned nparam = dim + C->Dimension;
1305 unsigned exist;
1306 Polyhedron *CA;
1308 RC = LexSmaller(P, D, dim, C, MaxRays);
1309 RD = RC->next;
1310 RC->next = NULL;
1312 exist = RD->Dimension - nparam - dim;
1313 CA = align_context(RC, RD->Dimension, MaxRays);
1314 Q = DomainIntersection(RD, CA, MaxRays);
1315 Polyhedron_Free(CA);
1316 Domain_Free(RD);
1317 Polyhedron_Free(RC);
1318 RD = Q;
1320 for (Q = RD; Q; Q = Q->next) {
1321 evalue *t;
1322 Polyhedron *next = Q->next;
1323 Q->next = 0;
1325 t = barvinok_enumerate_e(Q, exist, nparam, MaxRays);
1327 if (Q == RD)
1328 ranking = t;
1329 else {
1330 eadd(t, ranking);
1331 evalue_free(t);
1334 Q->next = next;
1337 Domain_Free(RD);
1339 return ranking;
1342 Enumeration *barvinok_lexsmaller(Polyhedron *P, Polyhedron *D, unsigned dim,
1343 Polyhedron *C, unsigned MaxRays)
1345 evalue *EP = barvinok_lexsmaller_ev(P, D, dim, C, MaxRays);
1347 return partition2enumeration(EP);
1350 /* "align" matrix to have nrows by inserting
1351 * the necessary number of rows and an equal number of columns in front
1353 Matrix *align_matrix(Matrix *M, int nrows)
1355 int i;
1356 int newrows = nrows - M->NbRows;
1357 Matrix *M2 = Matrix_Alloc(nrows, newrows + M->NbColumns);
1358 for (i = 0; i < newrows; ++i)
1359 value_set_si(M2->p[i][i], 1);
1360 for (i = 0; i < M->NbRows; ++i)
1361 Vector_Copy(M->p[i], M2->p[newrows+i]+newrows, M->NbColumns);
1362 return M2;
1365 static void print_varlist(FILE *out, int n, char **names)
1367 int i;
1368 fprintf(out, "[");
1369 for (i = 0; i < n; ++i) {
1370 if (i)
1371 fprintf(out, ",");
1372 fprintf(out, "%s", names[i]);
1374 fprintf(out, "]");
1377 static void print_term(FILE *out, Value v, int pos, int dim, int nparam,
1378 char **iter_names, char **param_names, int *first)
1380 if (value_zero_p(v)) {
1381 if (first && *first && pos >= dim + nparam)
1382 fprintf(out, "0");
1383 return;
1386 if (first) {
1387 if (!*first && value_pos_p(v))
1388 fprintf(out, "+");
1389 *first = 0;
1391 if (pos < dim + nparam) {
1392 if (value_mone_p(v))
1393 fprintf(out, "-");
1394 else if (!value_one_p(v))
1395 value_print(out, VALUE_FMT, v);
1396 if (pos < dim)
1397 fprintf(out, "%s", iter_names[pos]);
1398 else
1399 fprintf(out, "%s", param_names[pos-dim]);
1400 } else
1401 value_print(out, VALUE_FMT, v);
1404 char **util_generate_names(int n, const char *prefix)
1406 int i;
1407 int len = (prefix ? strlen(prefix) : 0) + 10;
1408 char **names = ALLOCN(char*, n);
1409 if (!names) {
1410 fprintf(stderr, "ERROR: memory overflow.\n");
1411 exit(1);
1413 for (i = 0; i < n; ++i) {
1414 names[i] = ALLOCN(char, len);
1415 if (!names[i]) {
1416 fprintf(stderr, "ERROR: memory overflow.\n");
1417 exit(1);
1419 if (!prefix)
1420 snprintf(names[i], len, "%d", i);
1421 else
1422 snprintf(names[i], len, "%s%d", prefix, i);
1425 return names;
1428 void util_free_names(int n, char **names)
1430 int i;
1431 for (i = 0; i < n; ++i)
1432 free(names[i]);
1433 free(names);
1436 void Polyhedron_pprint(FILE *out, Polyhedron *P, int dim, int nparam,
1437 char **iter_names, char **param_names)
1439 int i, j;
1440 Value tmp;
1442 assert(dim + nparam == P->Dimension);
1444 value_init(tmp);
1446 fprintf(out, "{ ");
1447 if (nparam) {
1448 print_varlist(out, nparam, param_names);
1449 fprintf(out, " -> ");
1451 print_varlist(out, dim, iter_names);
1452 fprintf(out, " : ");
1454 if (emptyQ2(P))
1455 fprintf(out, "FALSE");
1456 else for (i = 0; i < P->NbConstraints; ++i) {
1457 int first = 1;
1458 int v = First_Non_Zero(P->Constraint[i]+1, P->Dimension);
1459 if (v == -1 && value_pos_p(P->Constraint[i][0]))
1460 continue;
1461 if (i)
1462 fprintf(out, " && ");
1463 if (v == -1 && value_notzero_p(P->Constraint[i][1+P->Dimension]))
1464 fprintf(out, "FALSE");
1465 else if (value_pos_p(P->Constraint[i][v+1])) {
1466 print_term(out, P->Constraint[i][v+1], v, dim, nparam,
1467 iter_names, param_names, NULL);
1468 if (value_zero_p(P->Constraint[i][0]))
1469 fprintf(out, " = ");
1470 else
1471 fprintf(out, " >= ");
1472 for (j = v+1; j <= dim+nparam; ++j) {
1473 value_oppose(tmp, P->Constraint[i][1+j]);
1474 print_term(out, tmp, j, dim, nparam,
1475 iter_names, param_names, &first);
1477 } else {
1478 value_oppose(tmp, P->Constraint[i][1+v]);
1479 print_term(out, tmp, v, dim, nparam,
1480 iter_names, param_names, NULL);
1481 fprintf(out, " <= ");
1482 for (j = v+1; j <= dim+nparam; ++j)
1483 print_term(out, P->Constraint[i][1+j], j, dim, nparam,
1484 iter_names, param_names, &first);
1488 fprintf(out, " }\n");
1490 value_clear(tmp);
1493 /* Construct a cone over P with P placed at x_d = 1, with
1494 * x_d the coordinate of an extra dimension
1496 * It's probably a mistake to depend so much on the internal
1497 * representation. We should probably simply compute the
1498 * vertices/facets first.
1500 Polyhedron *Cone_over_Polyhedron(Polyhedron *P)
1502 unsigned NbConstraints = 0;
1503 unsigned NbRays = 0;
1504 Polyhedron *C;
1505 int i;
1507 if (POL_HAS(P, POL_INEQUALITIES))
1508 NbConstraints = P->NbConstraints + 1;
1509 if (POL_HAS(P, POL_POINTS))
1510 NbRays = P->NbRays + 1;
1512 C = Polyhedron_Alloc(P->Dimension+1, NbConstraints, NbRays);
1513 if (POL_HAS(P, POL_INEQUALITIES)) {
1514 C->NbEq = P->NbEq;
1515 for (i = 0; i < P->NbConstraints; ++i)
1516 Vector_Copy(P->Constraint[i], C->Constraint[i], P->Dimension+2);
1517 /* n >= 0 */
1518 value_set_si(C->Constraint[P->NbConstraints][0], 1);
1519 value_set_si(C->Constraint[P->NbConstraints][1+P->Dimension], 1);
1521 if (POL_HAS(P, POL_POINTS)) {
1522 C->NbBid = P->NbBid;
1523 for (i = 0; i < P->NbRays; ++i)
1524 Vector_Copy(P->Ray[i], C->Ray[i], P->Dimension+2);
1525 /* vertex 0 */
1526 value_set_si(C->Ray[P->NbRays][0], 1);
1527 value_set_si(C->Ray[P->NbRays][1+C->Dimension], 1);
1529 POL_SET(C, POL_VALID);
1530 if (POL_HAS(P, POL_INEQUALITIES))
1531 POL_SET(C, POL_INEQUALITIES);
1532 if (POL_HAS(P, POL_POINTS))
1533 POL_SET(C, POL_POINTS);
1534 if (POL_HAS(P, POL_VERTICES))
1535 POL_SET(C, POL_VERTICES);
1536 return C;
1539 /* Returns a (dim+nparam+1)x((dim-n)+nparam+1) matrix
1540 * mapping the transformed subspace back to the original space.
1541 * n is the number of equalities involving the variables
1542 * (i.e., not purely the parameters).
1543 * The remaining n coordinates in the transformed space would
1544 * have constant (parametric) values and are therefore not
1545 * included in the variables of the new space.
1547 Matrix *compress_variables(Matrix *Equalities, unsigned nparam)
1549 unsigned dim = (Equalities->NbColumns-2) - nparam;
1550 Matrix *M, *H, *Q, *U, *C, *ratH, *invH, *Ul, *T1, *T2, *T;
1551 Value mone;
1552 int n, i, j;
1553 int ok;
1555 for (n = 0; n < Equalities->NbRows; ++n)
1556 if (First_Non_Zero(Equalities->p[n]+1, dim) == -1)
1557 break;
1558 if (n == 0)
1559 return Identity(dim+nparam+1);
1560 value_init(mone);
1561 value_set_si(mone, -1);
1562 M = Matrix_Alloc(n, dim);
1563 C = Matrix_Alloc(n+1, nparam+1);
1564 for (i = 0; i < n; ++i) {
1565 Vector_Copy(Equalities->p[i]+1, M->p[i], dim);
1566 Vector_Scale(Equalities->p[i]+1+dim, C->p[i], mone, nparam+1);
1568 value_set_si(C->p[n][nparam], 1);
1569 left_hermite(M, &H, &Q, &U);
1570 Matrix_Free(M);
1571 Matrix_Free(Q);
1572 value_clear(mone);
1574 ratH = Matrix_Alloc(n+1, n+1);
1575 invH = Matrix_Alloc(n+1, n+1);
1576 for (i = 0; i < n; ++i)
1577 Vector_Copy(H->p[i], ratH->p[i], n);
1578 value_set_si(ratH->p[n][n], 1);
1579 ok = Matrix_Inverse(ratH, invH);
1580 assert(ok);
1581 Matrix_Free(H);
1582 Matrix_Free(ratH);
1583 T1 = Matrix_Alloc(n+1, nparam+1);
1584 Matrix_Product(invH, C, T1);
1585 Matrix_Free(C);
1586 Matrix_Free(invH);
1587 if (value_notone_p(T1->p[n][nparam])) {
1588 for (i = 0; i < n; ++i) {
1589 if (!mpz_divisible_p(T1->p[i][nparam], T1->p[n][nparam])) {
1590 Matrix_Free(T1);
1591 Matrix_Free(U);
1592 return NULL;
1594 /* compress_params should have taken care of this */
1595 for (j = 0; j < nparam; ++j)
1596 assert(mpz_divisible_p(T1->p[i][j], T1->p[n][nparam]));
1597 Vector_AntiScale(T1->p[i], T1->p[i], T1->p[n][nparam], nparam+1);
1599 value_set_si(T1->p[n][nparam], 1);
1601 Ul = Matrix_Alloc(dim+1, n+1);
1602 for (i = 0; i < dim; ++i)
1603 Vector_Copy(U->p[i], Ul->p[i], n);
1604 value_set_si(Ul->p[dim][n], 1);
1605 T2 = Matrix_Alloc(dim+1, nparam+1);
1606 Matrix_Product(Ul, T1, T2);
1607 Matrix_Free(Ul);
1608 Matrix_Free(T1);
1610 T = Matrix_Alloc(dim+nparam+1, (dim-n)+nparam+1);
1611 for (i = 0; i < dim; ++i) {
1612 Vector_Copy(U->p[i]+n, T->p[i], dim-n);
1613 Vector_Copy(T2->p[i], T->p[i]+dim-n, nparam+1);
1615 for (i = 0; i < nparam+1; ++i)
1616 value_set_si(T->p[dim+i][(dim-n)+i], 1);
1617 assert(value_one_p(T2->p[dim][nparam]));
1618 Matrix_Free(U);
1619 Matrix_Free(T2);
1621 return T;
1624 /* Computes the left inverse of an affine embedding M and, if Eq is not NULL,
1625 * the equalities that define the affine subspace onto which M maps
1626 * its argument.
1628 Matrix *left_inverse(Matrix *M, Matrix **Eq)
1630 int i, ok;
1631 Matrix *L, *H, *Q, *U, *ratH, *invH, *Ut, *inv;
1632 Vector *t;
1634 if (M->NbColumns == 1) {
1635 inv = Matrix_Alloc(1, M->NbRows);
1636 value_set_si(inv->p[0][M->NbRows-1], 1);
1637 if (Eq) {
1638 *Eq = Matrix_Alloc(M->NbRows-1, 1+(M->NbRows-1)+1);
1639 for (i = 0; i < M->NbRows-1; ++i) {
1640 value_oppose((*Eq)->p[i][1+i], M->p[M->NbRows-1][0]);
1641 value_assign((*Eq)->p[i][1+(M->NbRows-1)], M->p[i][0]);
1644 return inv;
1646 if (Eq)
1647 *Eq = NULL;
1648 L = Matrix_Alloc(M->NbRows-1, M->NbColumns-1);
1649 for (i = 0; i < L->NbRows; ++i)
1650 Vector_Copy(M->p[i], L->p[i], L->NbColumns);
1651 right_hermite(L, &H, &U, &Q);
1652 Matrix_Free(L);
1653 Matrix_Free(Q);
1654 t = Vector_Alloc(U->NbColumns);
1655 for (i = 0; i < U->NbColumns; ++i)
1656 value_oppose(t->p[i], M->p[i][M->NbColumns-1]);
1657 if (Eq) {
1658 *Eq = Matrix_Alloc(H->NbRows - H->NbColumns, 2 + U->NbColumns);
1659 for (i = 0; i < H->NbRows - H->NbColumns; ++i) {
1660 Vector_Copy(U->p[H->NbColumns+i], (*Eq)->p[i]+1, U->NbColumns);
1661 Inner_Product(U->p[H->NbColumns+i], t->p, U->NbColumns,
1662 (*Eq)->p[i]+1+U->NbColumns);
1665 ratH = Matrix_Alloc(H->NbColumns+1, H->NbColumns+1);
1666 invH = Matrix_Alloc(H->NbColumns+1, H->NbColumns+1);
1667 for (i = 0; i < H->NbColumns; ++i)
1668 Vector_Copy(H->p[i], ratH->p[i], H->NbColumns);
1669 value_set_si(ratH->p[ratH->NbRows-1][ratH->NbColumns-1], 1);
1670 Matrix_Free(H);
1671 ok = Matrix_Inverse(ratH, invH);
1672 assert(ok);
1673 Matrix_Free(ratH);
1674 Ut = Matrix_Alloc(invH->NbRows, U->NbColumns+1);
1675 for (i = 0; i < Ut->NbRows-1; ++i) {
1676 Vector_Copy(U->p[i], Ut->p[i], U->NbColumns);
1677 Inner_Product(U->p[i], t->p, U->NbColumns, &Ut->p[i][Ut->NbColumns-1]);
1679 Matrix_Free(U);
1680 Vector_Free(t);
1681 value_set_si(Ut->p[Ut->NbRows-1][Ut->NbColumns-1], 1);
1682 inv = Matrix_Alloc(invH->NbRows, Ut->NbColumns);
1683 Matrix_Product(invH, Ut, inv);
1684 Matrix_Free(Ut);
1685 Matrix_Free(invH);
1686 return inv;
1689 /* Check whether all rays are revlex positive in the parameters
1691 int Polyhedron_has_revlex_positive_rays(Polyhedron *P, unsigned nparam)
1693 int r;
1694 for (r = 0; r < P->NbRays; ++r) {
1695 int i;
1696 if (value_notzero_p(P->Ray[r][P->Dimension+1]))
1697 continue;
1698 for (i = P->Dimension-1; i >= P->Dimension-nparam; --i) {
1699 if (value_neg_p(P->Ray[r][i+1]))
1700 return 0;
1701 if (value_pos_p(P->Ray[r][i+1]))
1702 break;
1704 /* A ray independent of the parameters */
1705 if (i < P->Dimension-nparam)
1706 return 0;
1708 return 1;
1711 static Polyhedron *Recession_Cone(Polyhedron *P, unsigned nparam, unsigned MaxRays)
1713 int i;
1714 unsigned nvar = P->Dimension - nparam;
1715 Matrix *M = Matrix_Alloc(P->NbConstraints, 1 + nvar + 1);
1716 Polyhedron *R;
1717 for (i = 0; i < P->NbConstraints; ++i)
1718 Vector_Copy(P->Constraint[i], M->p[i], 1+nvar);
1719 R = Constraints2Polyhedron(M, MaxRays);
1720 Matrix_Free(M);
1721 return R;
1724 int Polyhedron_is_unbounded(Polyhedron *P, unsigned nparam, unsigned MaxRays)
1726 int i;
1727 int is_unbounded;
1728 Polyhedron *R = Recession_Cone(P, nparam, MaxRays);
1729 POL_ENSURE_VERTICES(R);
1730 if (R->NbBid == 0)
1731 for (i = 0; i < R->NbRays; ++i)
1732 if (value_zero_p(R->Ray[i][1+R->Dimension]))
1733 break;
1734 is_unbounded = R->NbBid > 0 || i < R->NbRays;
1735 Polyhedron_Free(R);
1736 return is_unbounded;
1739 static void SwapColumns(Value **V, int n, int i, int j)
1741 int r;
1743 for (r = 0; r < n; ++r)
1744 value_swap(V[r][i], V[r][j]);
1747 void Polyhedron_ExchangeColumns(Polyhedron *P, int Column1, int Column2)
1749 SwapColumns(P->Constraint, P->NbConstraints, Column1, Column2);
1750 SwapColumns(P->Ray, P->NbRays, Column1, Column2);
1751 if (P->NbEq) {
1752 Matrix M;
1753 Polyhedron_Matrix_View(P, &M, P->NbConstraints);
1754 Gauss(&M, P->NbEq, P->Dimension+1);
1758 /* perform transposition inline; assumes M is a square matrix */
1759 void Matrix_Transposition(Matrix *M)
1761 int i, j;
1763 assert(M->NbRows == M->NbColumns);
1764 for (i = 0; i < M->NbRows; ++i)
1765 for (j = i+1; j < M->NbColumns; ++j)
1766 value_swap(M->p[i][j], M->p[j][i]);
1769 /* Matrix "view" of first rows rows */
1770 void Polyhedron_Matrix_View(Polyhedron *P, Matrix *M, unsigned rows)
1772 M->NbRows = rows;
1773 M->NbColumns = P->Dimension+2;
1774 M->p_Init = P->p_Init;
1775 M->p = P->Constraint;
1778 int Last_Non_Zero(Value *p, unsigned len)
1780 int i;
1782 for (i = len - 1; i >= 0; --i)
1783 if (value_notzero_p(p[i]))
1784 return i;
1786 return -1;