README: explain how to use a pre-installed version of isl
[barvinok.git] / util.c
blobd217db1f4911d1d75d17e887d79fd566e5e21b81
1 #include <stdlib.h>
2 #include <assert.h>
3 #include <isl/val_gmp.h>
4 #include <isl_set_polylib.h>
5 #include <barvinok/util.h>
6 #include <barvinok/options.h>
7 #include <polylib/ranking.h>
8 #include "config.h"
9 #include "lattice_point.h"
11 #define ALLOC(type) (type*)malloc(sizeof(type))
12 #define ALLOCN(type,n) (type*)malloc((n) * sizeof(type))
14 #ifdef __GNUC__
15 #define NALLOC(p,n) p = (typeof(p))malloc((n) * sizeof(*p))
16 #else
17 #define NALLOC(p,n) p = (void *)malloc((n) * sizeof(*p))
18 #endif
20 void manual_count(Polyhedron *P, Value* result)
22 isl_ctx *ctx = isl_ctx_alloc();
23 isl_space *dim;
24 isl_set *set;
25 isl_val *v;
26 int nvar = P->Dimension;
28 dim = isl_space_set_alloc(ctx, 0, nvar);
29 set = isl_set_new_from_polylib(P, dim);
31 v = isl_set_count_val(set);
32 isl_val_get_num_gmp(v, *result);
33 isl_val_free(v);
35 isl_set_free(set);
36 isl_ctx_free(ctx);
38 assert(v);
41 #include <barvinok/evalue.h>
42 #include <barvinok/util.h>
43 #include <barvinok/barvinok.h>
45 /* Return random value between 0 and max-1 inclusive
47 int random_int(int max) {
48 return (int) (((double)(max))*rand()/(RAND_MAX+1.0));
51 Polyhedron *Polyhedron_Read(unsigned MaxRays)
53 int vertices = 0;
54 unsigned NbRows, NbColumns;
55 Matrix *M;
56 Polyhedron *P;
57 char s[128];
59 while (fgets(s, sizeof(s), stdin)) {
60 if (*s == '#')
61 continue;
62 if (strncasecmp(s, "vertices", sizeof("vertices")-1) == 0)
63 vertices = 1;
64 if (sscanf(s, "%u %u", &NbRows, &NbColumns) == 2)
65 break;
67 if (feof(stdin))
68 return NULL;
69 M = Matrix_Alloc(NbRows,NbColumns);
70 Matrix_Read_Input(M);
71 if (vertices)
72 P = Rays2Polyhedron(M, MaxRays);
73 else
74 P = Constraints2Polyhedron(M, MaxRays);
75 Matrix_Free(M);
76 return P;
79 /* Inplace polarization
81 void Polyhedron_Polarize(Polyhedron *P)
83 unsigned NbRows = P->NbConstraints + P->NbRays;
84 int i;
85 Value **q;
87 q = (Value **)malloc(NbRows * sizeof(Value *));
88 assert(q);
89 for (i = 0; i < P->NbRays; ++i)
90 q[i] = P->Ray[i];
91 for (; i < NbRows; ++i)
92 q[i] = P->Constraint[i-P->NbRays];
93 P->NbConstraints = NbRows - P->NbConstraints;
94 P->NbRays = NbRows - P->NbRays;
95 free(P->Constraint);
96 P->Constraint = q;
97 P->Ray = q + P->NbConstraints;
101 * Rather general polar
102 * We can optimize it significantly if we assume that
103 * P includes zero
105 * Also, we calculate the polar as defined in Schrijver
106 * The opposite should probably work as well and would
107 * eliminate the need for multiplying by -1
109 Polyhedron* Polyhedron_Polar(Polyhedron *P, unsigned NbMaxRays)
111 int i;
112 Value mone;
113 unsigned dim = P->Dimension + 2;
114 Matrix *M = Matrix_Alloc(P->NbRays, dim);
116 assert(M);
117 value_init(mone);
118 value_set_si(mone, -1);
119 for (i = 0; i < P->NbRays; ++i) {
120 Vector_Scale(P->Ray[i], M->p[i], mone, dim);
121 value_multiply(M->p[i][0], M->p[i][0], mone);
122 value_multiply(M->p[i][dim-1], M->p[i][dim-1], mone);
124 P = Constraints2Polyhedron(M, NbMaxRays);
125 assert(P);
126 Matrix_Free(M);
127 value_clear(mone);
128 return P;
132 * Returns the supporting cone of P at the vertex with index v
134 Polyhedron* supporting_cone(Polyhedron *P, int v)
136 Matrix *M;
137 Value tmp;
138 int i, n, j;
139 unsigned char *supporting = (unsigned char *)malloc(P->NbConstraints);
140 unsigned dim = P->Dimension + 2;
142 assert(v >=0 && v < P->NbRays);
143 assert(value_pos_p(P->Ray[v][dim-1]));
144 assert(supporting);
146 value_init(tmp);
147 for (i = 0, n = 0; i < P->NbConstraints; ++i) {
148 Inner_Product(P->Constraint[i] + 1, P->Ray[v] + 1, dim - 1, &tmp);
149 if ((supporting[i] = value_zero_p(tmp)))
150 ++n;
152 assert(n >= dim - 2);
153 value_clear(tmp);
154 M = Matrix_Alloc(n, dim);
155 assert(M);
156 for (i = 0, j = 0; i < P->NbConstraints; ++i)
157 if (supporting[i]) {
158 value_set_si(M->p[j][dim-1], 0);
159 Vector_Copy(P->Constraint[i], M->p[j++], dim-1);
161 free(supporting);
162 P = Constraints2Polyhedron(M, P->NbRays+1);
163 assert(P);
164 Matrix_Free(M);
165 return P;
168 #define INT_BITS (sizeof(unsigned) * 8)
170 unsigned *supporting_constraints(Matrix *Constraints, Param_Vertices *v, int *n)
172 Value lcm, tmp, tmp2;
173 unsigned dim = Constraints->NbColumns;
174 unsigned nparam = v->Vertex->NbColumns - 2;
175 unsigned nvar = dim - nparam - 2;
176 int len = (Constraints->NbRows+INT_BITS-1)/INT_BITS;
177 unsigned *supporting = (unsigned *)calloc(len, sizeof(unsigned));
178 int i, j;
179 Vector *row;
180 int ix;
181 unsigned bx;
183 assert(supporting);
184 row = Vector_Alloc(nparam+1);
185 assert(row);
186 value_init(lcm);
187 value_init(tmp);
188 value_init(tmp2);
189 value_set_si(lcm, 1);
190 for (i = 0, *n = 0, ix = 0, bx = MSB; i < Constraints->NbRows; ++i) {
191 Vector_Set(row->p, 0, nparam+1);
192 for (j = 0 ; j < nvar; ++j) {
193 value_set_si(tmp, 1);
194 value_assign(tmp2, Constraints->p[i][j+1]);
195 if (value_ne(lcm, v->Vertex->p[j][nparam+1])) {
196 value_assign(tmp, lcm);
197 value_lcm(lcm, lcm, v->Vertex->p[j][nparam+1]);
198 value_division(tmp, lcm, tmp);
199 value_multiply(tmp2, tmp2, lcm);
200 value_division(tmp2, tmp2, v->Vertex->p[j][nparam+1]);
202 Vector_Combine(row->p, v->Vertex->p[j], row->p,
203 tmp, tmp2, nparam+1);
205 value_set_si(tmp, 1);
206 Vector_Combine(row->p, Constraints->p[i]+1+nvar, row->p, tmp, lcm, nparam+1);
207 for (j = 0; j < nparam+1; ++j)
208 if (value_notzero_p(row->p[j]))
209 break;
210 if (j == nparam + 1) {
211 supporting[ix] |= bx;
212 ++*n;
214 NEXT(ix, bx);
216 assert(*n >= nvar);
217 value_clear(tmp);
218 value_clear(tmp2);
219 value_clear(lcm);
220 Vector_Free(row);
222 return supporting;
225 Polyhedron* supporting_cone_p(Polyhedron *P, Param_Vertices *v)
227 Matrix *M;
228 unsigned dim = P->Dimension + 2;
229 unsigned nparam = v->Vertex->NbColumns - 2;
230 unsigned nvar = dim - nparam - 2;
231 int i, n, j;
232 int ix;
233 unsigned bx;
234 unsigned *supporting;
235 Matrix View;
237 Polyhedron_Matrix_View(P, &View, P->NbConstraints);
238 supporting = supporting_constraints(&View, v, &n);
239 M = Matrix_Alloc(n, nvar+2);
240 assert(M);
241 for (i = 0, j = 0, ix = 0, bx = MSB; i < P->NbConstraints; ++i) {
242 if (supporting[ix] & bx) {
243 value_set_si(M->p[j][nvar+1], 0);
244 Vector_Copy(P->Constraint[i], M->p[j++], nvar+1);
246 NEXT(ix, bx);
248 free(supporting);
249 P = Constraints2Polyhedron(M, P->NbRays+1);
250 assert(P);
251 Matrix_Free(M);
252 return P;
255 Polyhedron* triangulate_cone(Polyhedron *P, unsigned NbMaxCons)
257 struct barvinok_options *options = barvinok_options_new_with_defaults();
258 options->MaxRays = NbMaxCons;
259 P = triangulate_cone_with_options(P, options);
260 barvinok_options_free(options);
261 return P;
264 Polyhedron* triangulate_cone_with_options(Polyhedron *P,
265 struct barvinok_options *options)
267 const static int MAX_TRY=10;
268 int i, j, r, n, t;
269 Value tmp;
270 unsigned dim = P->Dimension;
271 Matrix *M = Matrix_Alloc(P->NbRays+1, dim+3);
272 Matrix *M2, *M3;
273 Polyhedron *L, *R, *T;
274 assert(P->NbEq == 0);
276 L = NULL;
277 R = NULL;
278 value_init(tmp);
280 Vector_Set(M->p[0]+1, 0, dim+1);
281 value_set_si(M->p[0][0], 1);
282 value_set_si(M->p[0][dim+2], 1);
283 Vector_Set(M->p[P->NbRays]+1, 0, dim+2);
284 value_set_si(M->p[P->NbRays][0], 1);
285 value_set_si(M->p[P->NbRays][dim+1], 1);
287 for (i = 0, r = 1; i < P->NbRays; ++i) {
288 if (value_notzero_p(P->Ray[i][dim+1]))
289 continue;
290 Vector_Copy(P->Ray[i], M->p[r], dim+1);
291 value_set_si(M->p[r][dim+2], 0);
292 ++r;
295 M2 = Matrix_Alloc(dim+1, dim+2);
297 t = 0;
298 if (options->try_Delaunay_triangulation) {
299 /* Delaunay triangulation */
300 for (r = 1; r < P->NbRays; ++r) {
301 Inner_Product(M->p[r]+1, M->p[r]+1, dim, &tmp);
302 value_assign(M->p[r][dim+1], tmp);
304 M3 = Matrix_Copy(M);
305 L = Rays2Polyhedron(M3, options->MaxRays);
306 Matrix_Free(M3);
307 ++t;
308 } else {
309 try_again:
310 /* Usually R should still be 0 */
311 Domain_Free(R);
312 Polyhedron_Free(L);
313 for (r = 1; r < P->NbRays; ++r) {
314 value_set_si(M->p[r][dim+1], random_int((t+1)*dim*P->NbRays)+1);
316 M3 = Matrix_Copy(M);
317 L = Rays2Polyhedron(M3, options->MaxRays);
318 Matrix_Free(M3);
319 ++t;
321 assert(t <= MAX_TRY);
323 R = NULL;
324 n = 0;
326 POL_ENSURE_FACETS(L);
327 for (i = 0; i < L->NbConstraints; ++i) {
328 /* Ignore perpendicular facets, i.e., facets with 0 z-coordinate */
329 if (value_negz_p(L->Constraint[i][dim+1]))
330 continue;
331 if (value_notzero_p(L->Constraint[i][dim+2]))
332 continue;
333 for (j = 1, r = 1; j < M->NbRows; ++j) {
334 Inner_Product(M->p[j]+1, L->Constraint[i]+1, dim+1, &tmp);
335 if (value_notzero_p(tmp))
336 continue;
337 if (r > dim)
338 goto try_again;
339 Vector_Copy(M->p[j]+1, M2->p[r]+1, dim);
340 value_set_si(M2->p[r][0], 1);
341 value_set_si(M2->p[r][dim+1], 0);
342 ++r;
344 assert(r == dim+1);
345 Vector_Set(M2->p[0]+1, 0, dim);
346 value_set_si(M2->p[0][0], 1);
347 value_set_si(M2->p[0][dim+1], 1);
348 T = Rays2Polyhedron(M2, P->NbConstraints+1);
349 T->next = R;
350 R = T;
351 ++n;
353 Matrix_Free(M2);
355 Polyhedron_Free(L);
356 value_clear(tmp);
357 Matrix_Free(M);
359 return R;
362 void check_triangulization(Polyhedron *P, Polyhedron *T)
364 Polyhedron *C, *D, *E, *F, *G, *U;
365 for (C = T; C; C = C->next) {
366 if (C == T)
367 U = C;
368 else
369 U = DomainConvex(DomainUnion(U, C, 100), 100);
370 for (D = C->next; D; D = D->next) {
371 F = C->next;
372 G = D->next;
373 C->next = NULL;
374 D->next = NULL;
375 E = DomainIntersection(C, D, 600);
376 assert(E->NbRays == 0 || E->NbEq >= 1);
377 Polyhedron_Free(E);
378 C->next = F;
379 D->next = G;
382 assert(PolyhedronIncludes(U, P));
383 assert(PolyhedronIncludes(P, U));
386 /* Computes x, y and g such that g = gcd(a,b) and a*x+b*y = g */
387 void Extended_Euclid(Value a, Value b, Value *x, Value *y, Value *g)
389 Value c, d, e, f, tmp;
391 value_init(c);
392 value_init(d);
393 value_init(e);
394 value_init(f);
395 value_init(tmp);
396 value_absolute(c, a);
397 value_absolute(d, b);
398 value_set_si(e, 1);
399 value_set_si(f, 0);
400 while(value_pos_p(d)) {
401 value_division(tmp, c, d);
402 value_multiply(tmp, tmp, f);
403 value_subtract(e, e, tmp);
404 value_division(tmp, c, d);
405 value_multiply(tmp, tmp, d);
406 value_subtract(c, c, tmp);
407 value_swap(c, d);
408 value_swap(e, f);
410 value_assign(*g, c);
411 if (value_zero_p(a))
412 value_set_si(*x, 0);
413 else if (value_pos_p(a))
414 value_assign(*x, e);
415 else value_oppose(*x, e);
416 if (value_zero_p(b))
417 value_set_si(*y, 0);
418 else {
419 value_multiply(tmp, a, *x);
420 value_subtract(tmp, c, tmp);
421 value_division(*y, tmp, b);
423 value_clear(c);
424 value_clear(d);
425 value_clear(e);
426 value_clear(f);
427 value_clear(tmp);
430 static int unimodular_complete_1(Matrix *m)
432 Value g, b, c, old, tmp;
433 unsigned i, j;
434 int ok;
436 value_init(b);
437 value_init(c);
438 value_init(g);
439 value_init(old);
440 value_init(tmp);
441 value_assign(g, m->p[0][0]);
442 for (i = 1; value_zero_p(g) && i < m->NbColumns; ++i) {
443 for (j = 0; j < m->NbColumns; ++j) {
444 if (j == i-1)
445 value_set_si(m->p[i][j], 1);
446 else
447 value_set_si(m->p[i][j], 0);
449 value_assign(g, m->p[0][i]);
451 for (; i < m->NbColumns; ++i) {
452 value_assign(old, g);
453 Extended_Euclid(old, m->p[0][i], &c, &b, &g);
454 value_oppose(b, b);
455 for (j = 0; j < m->NbColumns; ++j) {
456 if (j < i) {
457 value_multiply(tmp, m->p[0][j], b);
458 value_division(m->p[i][j], tmp, old);
459 } else if (j == i)
460 value_assign(m->p[i][j], c);
461 else
462 value_set_si(m->p[i][j], 0);
465 ok = value_one_p(g);
466 value_clear(b);
467 value_clear(c);
468 value_clear(g);
469 value_clear(old);
470 value_clear(tmp);
471 return ok;
474 int unimodular_complete(Matrix *M, int row)
476 int r;
477 int ok = 1;
478 Matrix *H, *Q, *U;
480 if (row == 1)
481 return unimodular_complete_1(M);
483 left_hermite(M, &H, &Q, &U);
484 Matrix_Free(U);
485 for (r = 0; ok && r < row; ++r)
486 if (value_notone_p(H->p[r][r]))
487 ok = 0;
488 Matrix_Free(H);
489 for (r = row; r < M->NbRows; ++r)
490 Vector_Copy(Q->p[r], M->p[r], M->NbColumns);
491 Matrix_Free(Q);
492 return ok;
496 * left_hermite may leave positive entries below the main diagonal in H.
497 * This function postprocesses the output of left_hermite to make
498 * the non-zero entries below the main diagonal negative.
500 void neg_left_hermite(Matrix *A, Matrix **H_p, Matrix **Q_p, Matrix **U_p)
502 int row, col, i, j;
503 Matrix *H, *U, *Q;
505 left_hermite(A, &H, &Q, &U);
506 *H_p = H;
507 *Q_p = Q;
508 *U_p = U;
510 for (row = 0, col = 0; col < H->NbColumns; ++col, ++row) {
511 while (value_zero_p(H->p[row][col]))
512 ++row;
513 for (i = 0; i < col; ++i) {
514 if (value_negz_p(H->p[row][i]))
515 continue;
517 /* subtract column col from column i in H and U */
518 for (j = 0; j < H->NbRows; ++j)
519 value_subtract(H->p[j][i], H->p[j][i], H->p[j][col]);
520 for (j = 0; j < U->NbRows; ++j)
521 value_subtract(U->p[j][i], U->p[j][i], U->p[j][col]);
523 /* add row i to row col in Q */
524 for (j = 0; j < Q->NbColumns; ++j)
525 value_addto(Q->p[col][j], Q->p[col][j], Q->p[i][j]);
531 * Returns a full-dimensional polyhedron with the same number
532 * of integer points as P
534 Polyhedron *remove_equalities(Polyhedron *P, unsigned MaxRays)
536 Matrix M;
537 Matrix *T;
538 Polyhedron *Q = Polyhedron_Copy(P);
539 unsigned dim = P->Dimension;
541 if (Q->NbEq == 0)
542 return Q;
544 Q = DomainConstraintSimplify(Q, MaxRays);
545 if (emptyQ2(Q))
546 return Q;
548 Polyhedron_Matrix_View(Q, &M, Q->NbEq);
549 T = compress_variables(&M, 0);
551 if (!T)
552 P = NULL;
553 else {
554 P = Polyhedron_Preimage(Q, T, MaxRays);
555 Matrix_Free(T);
558 Polyhedron_Free(Q);
560 return P;
564 * Returns a full-dimensional polyhedron with the same number
565 * of integer points as P
566 * nvar specifies the number of variables
567 * The remaining dimensions are assumed to be parameters
568 * Destroys P
569 * factor is NbEq x (nparam+2) matrix, containing stride constraints
570 * on the parameters; column nparam is the constant;
571 * column nparam+1 is the stride
573 * if factor is NULL, only remove equalities that don't affect
574 * the number of points
576 Polyhedron *remove_equalities_p(Polyhedron *P, unsigned nvar, Matrix **factor,
577 unsigned MaxRays)
579 Value g;
580 Polyhedron *Q;
581 unsigned dim = P->Dimension;
582 Matrix *m1, *m2, *f;
583 int i, j;
585 if (P->NbEq == 0)
586 return P;
588 m1 = Matrix_Alloc(nvar, nvar);
589 P = DomainConstraintSimplify(P, MaxRays);
590 if (factor) {
591 f = Matrix_Alloc(P->NbEq, dim-nvar+2);
592 *factor = f;
594 value_init(g);
595 for (i = 0, j = 0; i < P->NbEq; ++i) {
596 if (First_Non_Zero(P->Constraint[i]+1, nvar) == -1)
597 continue;
599 Vector_Gcd(P->Constraint[i]+1, nvar, &g);
600 if (!factor && value_notone_p(g))
601 continue;
603 if (factor) {
604 Vector_Copy(P->Constraint[i]+1+nvar, f->p[j], dim-nvar+1);
605 value_assign(f->p[j][dim-nvar+1], g);
608 Vector_Copy(P->Constraint[i]+1, m1->p[j], nvar);
610 ++j;
612 value_clear(g);
614 unimodular_complete(m1, j);
616 m2 = Matrix_Alloc(dim+1-j, dim+1);
617 for (i = 0; i < nvar-j ; ++i)
618 Vector_Copy(m1->p[i+j], m2->p[i], nvar);
619 Matrix_Free(m1);
620 for (i = nvar-j; i <= dim-j; ++i)
621 value_set_si(m2->p[i][i+j], 1);
623 Q = Polyhedron_Image(P, m2, MaxRays);
624 Matrix_Free(m2);
625 Polyhedron_Free(P);
627 return Q;
630 void Line_Length(Polyhedron *P, Value *len)
632 Value tmp, pos, neg;
633 int p = 0, n = 0;
634 int i;
636 assert(P->Dimension == 1);
638 if (P->NbEq > 0) {
639 if (mpz_divisible_p(P->Constraint[0][2], P->Constraint[0][1]))
640 value_set_si(*len, 1);
641 else
642 value_set_si(*len, 0);
643 return;
646 value_init(tmp);
647 value_init(pos);
648 value_init(neg);
650 for (i = 0; i < P->NbConstraints; ++i) {
651 value_oppose(tmp, P->Constraint[i][2]);
652 if (value_pos_p(P->Constraint[i][1])) {
653 mpz_cdiv_q(tmp, tmp, P->Constraint[i][1]);
654 if (!p || value_gt(tmp, pos))
655 value_assign(pos, tmp);
656 p = 1;
657 } else if (value_neg_p(P->Constraint[i][1])) {
658 mpz_fdiv_q(tmp, tmp, P->Constraint[i][1]);
659 if (!n || value_lt(tmp, neg))
660 value_assign(neg, tmp);
661 n = 1;
663 if (n && p) {
664 value_subtract(tmp, neg, pos);
665 value_increment(*len, tmp);
666 } else
667 value_set_si(*len, -1);
670 value_clear(tmp);
671 value_clear(pos);
672 value_clear(neg);
675 /* Update group[k] to the group column k belongs to.
676 * When merging two groups, only the group of the current
677 * group leader is changed. Here we change the group of
678 * the other members to also point to the group that the
679 * old group leader now points to.
681 static void update_group(int *group, int *cnt, int k)
683 int g = group[k];
684 while (cnt[g] == 0)
685 g = group[g];
686 group[k] = g;
690 * Factors the polyhedron P into polyhedra Q_i such that
691 * the number of integer points in P is equal to the product
692 * of the number of integer points in the individual Q_i
694 * If no factors can be found, NULL is returned.
695 * Otherwise, a linked list of the factors is returned.
697 * If there are factors and if T is not NULL, then a matrix will be
698 * returned through T expressing the old variables in terms of the
699 * new variables as they appear in the sequence of factors.
701 * The algorithm works by first computing the Hermite normal form
702 * and then grouping columns linked by one or more constraints together,
703 * where a constraints "links" two or more columns if the constraint
704 * has nonzero coefficients in the columns.
706 Polyhedron* Polyhedron_Factor(Polyhedron *P, unsigned nparam, Matrix **T,
707 unsigned NbMaxRays)
709 int i, j, k;
710 Matrix *M, *H, *Q, *U;
711 int *pos; /* for each column: row position of pivot */
712 int *group; /* group to which a column belongs */
713 int *cnt; /* number of columns in the group */
714 int *rowgroup; /* group to which a constraint belongs */
715 int nvar = P->Dimension - nparam;
716 Polyhedron *F = NULL;
718 if (nvar <= 1)
719 return NULL;
721 NALLOC(pos, nvar);
722 NALLOC(group, nvar);
723 NALLOC(cnt, nvar);
724 NALLOC(rowgroup, P->NbConstraints);
726 M = Matrix_Alloc(P->NbConstraints, nvar);
727 for (i = 0; i < P->NbConstraints; ++i)
728 Vector_Copy(P->Constraint[i]+1, M->p[i], nvar);
729 left_hermite(M, &H, &Q, &U);
730 Matrix_Free(M);
731 Matrix_Free(Q);
733 for (i = 0; i < P->NbConstraints; ++i)
734 rowgroup[i] = -1;
735 for (i = 0, j = 0; i < H->NbColumns; ++i) {
736 for ( ; j < H->NbRows; ++j)
737 if (value_notzero_p(H->p[j][i]))
738 break;
739 pos[i] = j;
741 for (i = 0; i < nvar; ++i) {
742 group[i] = i;
743 cnt[i] = 1;
745 for (i = 0; i < H->NbColumns && cnt[0] < nvar; ++i) {
746 if (pos[i] == H->NbRows)
747 continue; /* A line direction */
748 if (rowgroup[pos[i]] == -1)
749 rowgroup[pos[i]] = i;
750 for (j = pos[i]+1; j < H->NbRows; ++j) {
751 if (value_zero_p(H->p[j][i]))
752 continue;
753 if (rowgroup[j] != -1)
754 continue;
755 rowgroup[j] = group[i];
756 for (k = i+1; k < H->NbColumns && j >= pos[k]; ++k) {
757 update_group(group, cnt, k);
758 update_group(group, cnt, i);
759 if (group[k] != group[i] && value_notzero_p(H->p[j][k])) {
760 assert(cnt[group[k]] != 0);
761 assert(cnt[group[i]] != 0);
762 if (group[i] < group[k]) {
763 cnt[group[i]] += cnt[group[k]];
764 cnt[group[k]] = 0;
765 group[group[k]] = group[i];
766 } else {
767 cnt[group[k]] += cnt[group[i]];
768 cnt[group[i]] = 0;
769 group[group[i]] = group[k];
775 for (i = 1; i < nvar; ++i)
776 update_group(group, cnt, i);
778 if (cnt[0] != nvar) {
779 /* Extract out pure context constraints separately */
780 Polyhedron **next = &F;
781 int tot_d = 0;
782 if (T)
783 *T = Matrix_Alloc(nvar, nvar);
784 for (i = nparam ? -1 : 0; i < nvar; ++i) {
785 int d;
787 if (i == -1) {
788 for (j = 0, k = 0; j < P->NbConstraints; ++j)
789 if (rowgroup[j] == -1) {
790 if (First_Non_Zero(P->Constraint[j]+1+nvar,
791 nparam) == -1)
792 rowgroup[j] = -2;
793 else
794 ++k;
796 if (k == 0)
797 continue;
798 d = 0;
799 } else {
800 if (cnt[i] == 0)
801 continue;
802 d = cnt[i];
803 for (j = 0, k = 0; j < P->NbConstraints; ++j)
804 if (rowgroup[j] >= 0 && group[rowgroup[j]] == i) {
805 rowgroup[j] = i;
806 ++k;
810 if (T)
811 for (j = 0; j < nvar; ++j) {
812 int l, m;
813 for (l = 0, m = 0; m < d; ++l) {
814 if (group[l] != i)
815 continue;
816 value_assign((*T)->p[j][tot_d+m++], U->p[j][l]);
820 M = Matrix_Alloc(k, d+nparam+2);
821 for (j = 0, k = 0; j < P->NbConstraints; ++j) {
822 int l, m;
823 if (rowgroup[j] != i)
824 continue;
825 value_assign(M->p[k][0], P->Constraint[j][0]);
826 for (l = 0, m = 0; m < d; ++l) {
827 if (group[l] != i)
828 continue;
829 value_assign(M->p[k][1+m++], H->p[j][l]);
831 Vector_Copy(P->Constraint[j]+1+nvar, M->p[k]+1+m, nparam+1);
832 ++k;
834 *next = Constraints2Polyhedron(M, NbMaxRays);
835 next = &(*next)->next;
836 Matrix_Free(M);
837 tot_d += d;
840 Matrix_Free(U);
841 Matrix_Free(H);
842 free(pos);
843 free(group);
844 free(cnt);
845 free(rowgroup);
846 return F;
849 /* Computes the intersection of the contexts of a list of factors */
850 Polyhedron *Factor_Context(Polyhedron *F, unsigned nparam, unsigned MaxRays)
852 Polyhedron *Q;
853 Polyhedron *C = NULL;
855 for (Q = F; Q; Q = Q->next) {
856 Polyhedron *QC = Q;
857 Polyhedron *next = Q->next;
858 Q->next = NULL;
860 if (Q->Dimension != nparam)
861 QC = Polyhedron_Project(Q, nparam);
863 if (!C)
864 C = Q == QC ? Polyhedron_Copy(QC) : QC;
865 else {
866 Polyhedron *C2 = C;
867 C = DomainIntersection(C, QC, MaxRays);
868 Polyhedron_Free(C2);
869 if (QC != Q)
870 Polyhedron_Free(QC);
872 Q->next = next;
874 return C;
878 * Project on final dim dimensions
880 Polyhedron* Polyhedron_Project(Polyhedron *P, int dim)
882 int i;
883 int remove = P->Dimension - dim;
884 Matrix *T;
885 Polyhedron *I;
887 if (P->Dimension == dim)
888 return Polyhedron_Copy(P);
890 T = Matrix_Alloc(dim+1, P->Dimension+1);
891 for (i = 0; i < dim+1; ++i)
892 value_set_si(T->p[i][i+remove], 1);
893 I = Polyhedron_Image(P, T, P->NbConstraints);
894 Matrix_Free(T);
895 return I;
898 /* Constructs a new constraint that ensures that
899 * the first constraint is (strictly) smaller than
900 * the second.
902 static void smaller_constraint(Value *a, Value *b, Value *c, int pos, int shift,
903 int len, int strict, Value *tmp)
905 value_oppose(*tmp, b[pos+1]);
906 value_set_si(c[0], 1);
907 Vector_Combine(a+1+shift, b+1+shift, c+1, *tmp, a[pos+1], len-shift-1);
908 if (strict)
909 value_decrement(c[len-shift-1], c[len-shift-1]);
910 ConstraintSimplify(c, c, len-shift, tmp);
914 /* For each pair of lower and upper bounds on the first variable,
915 * calls fn with the set of constraints on the remaining variables
916 * where these bounds are active, i.e., (stricly) larger/smaller than
917 * the other lower/upper bounds, the lower and upper bound and the
918 * call back data.
920 * If the first variable is equal to an affine combination of the
921 * other variables then fn is called with both lower and upper
922 * pointing to the corresponding equality.
924 * If there is no lower (or upper) bound, then NULL is passed
925 * as the corresponding bound.
927 void for_each_lower_upper_bound(Polyhedron *P,
928 for_each_lower_upper_bound_init init,
929 for_each_lower_upper_bound_fn fn,
930 void *cb_data)
932 unsigned dim = P->Dimension;
933 Matrix *M;
934 int *pos;
935 int i, j, p, n, z;
936 int k, l, k2, l2, q;
937 Value g;
939 if (value_zero_p(P->Constraint[0][0]) &&
940 value_notzero_p(P->Constraint[0][1])) {
941 M = Matrix_Alloc(P->NbConstraints-1, dim-1+2);
942 for (i = 1; i < P->NbConstraints; ++i) {
943 value_assign(M->p[i-1][0], P->Constraint[i][0]);
944 Vector_Copy(P->Constraint[i]+2, M->p[i-1]+1, dim);
946 if (init)
947 init(1, cb_data);
948 fn(M, P->Constraint[0], P->Constraint[0], cb_data);
949 Matrix_Free(M);
950 return;
953 value_init(g);
954 pos = ALLOCN(int, P->NbConstraints);
956 for (i = 0, z = 0; i < P->NbConstraints; ++i)
957 if (value_zero_p(P->Constraint[i][1]))
958 pos[P->NbConstraints-1 - z++] = i;
959 /* put those with positive coefficients first; number: p */
960 for (i = 0, p = 0, n = P->NbConstraints-z-1; i < P->NbConstraints; ++i)
961 if (value_pos_p(P->Constraint[i][1]))
962 pos[p++] = i;
963 else if (value_neg_p(P->Constraint[i][1]))
964 pos[n--] = i;
965 n = P->NbConstraints-z-p;
967 if (init)
968 init(p*n, cb_data);
970 M = Matrix_Alloc((p ? p-1 : 0) + (n ? n-1 : 0) + z + 1, dim-1+2);
971 for (i = 0; i < z; ++i) {
972 value_assign(M->p[i][0], P->Constraint[pos[P->NbConstraints-1 - i]][0]);
973 Vector_Copy(P->Constraint[pos[P->NbConstraints-1 - i]]+2,
974 M->p[i]+1, dim);
976 for (k = p ? 0 : -1; k < p; ++k) {
977 for (k2 = 0; k2 < p; ++k2) {
978 if (k2 == k)
979 continue;
980 q = 1 + z + k2 - (k2 > k);
981 smaller_constraint(
982 P->Constraint[pos[k]],
983 P->Constraint[pos[k2]],
984 M->p[q], 0, 1, dim+2, k2 > k, &g);
986 for (l = n ? p : p-1; l < p+n; ++l) {
987 Value *lower;
988 Value *upper;
989 for (l2 = p; l2 < p+n; ++l2) {
990 if (l2 == l)
991 continue;
992 q = 1 + z + l2-1 - (l2 > l);
993 smaller_constraint(
994 P->Constraint[pos[l2]],
995 P->Constraint[pos[l]],
996 M->p[q], 0, 1, dim+2, l2 > l, &g);
998 if (p && n)
999 smaller_constraint(P->Constraint[pos[k]],
1000 P->Constraint[pos[l]],
1001 M->p[z], 0, 1, dim+2, 0, &g);
1002 lower = p ? P->Constraint[pos[k]] : NULL;
1003 upper = n ? P->Constraint[pos[l]] : NULL;
1004 fn(M, lower, upper, cb_data);
1007 Matrix_Free(M);
1009 free(pos);
1010 value_clear(g);
1013 struct section { Polyhedron * D; evalue E; };
1015 struct PLL_data {
1016 int nd;
1017 unsigned MaxRays;
1018 Polyhedron *C;
1019 evalue mone;
1020 struct section *s;
1023 static void PLL_init(unsigned n, void *cb_data)
1025 struct PLL_data *data = (struct PLL_data *)cb_data;
1027 data->s = ALLOCN(struct section, n);
1030 /* Computes ceil(-coef/abs(d)) */
1031 static evalue* bv_ceil3(Value *coef, int len, Value d, Polyhedron *P)
1033 Value t;
1034 evalue *EP, *f;
1035 Vector *val = Vector_Alloc(len);
1037 value_init(t);
1038 Vector_Oppose(coef, val->p, len);
1039 value_absolute(t, d);
1041 EP = ceiling(val->p, t, len-1, P);
1043 value_clear(t);
1044 Vector_Free(val);
1046 return EP;
1049 static void PLL_cb(Matrix *M, Value *lower, Value *upper, void *cb_data)
1051 struct PLL_data *data = (struct PLL_data *)cb_data;
1052 unsigned dim = M->NbColumns-1;
1053 Matrix *M2;
1054 Polyhedron *T;
1055 evalue *L, *U;
1057 assert(lower);
1058 assert(upper);
1060 M2 = Matrix_Copy(M);
1061 T = Constraints2Polyhedron(M2, data->MaxRays);
1062 Matrix_Free(M2);
1063 data->s[data->nd].D = DomainIntersection(T, data->C, data->MaxRays);
1064 Domain_Free(T);
1066 POL_ENSURE_VERTICES(data->s[data->nd].D);
1067 if (emptyQ(data->s[data->nd].D)) {
1068 Polyhedron_Free(data->s[data->nd].D);
1069 return;
1071 L = bv_ceil3(lower+1+1, dim-1+1, lower[0+1], data->s[data->nd].D);
1072 U = bv_ceil3(upper+1+1, dim-1+1, upper[0+1], data->s[data->nd].D);
1073 eadd(L, U);
1074 eadd(&data->mone, U);
1075 emul(&data->mone, U);
1076 data->s[data->nd].E = *U;
1077 evalue_free(L);
1078 free(U);
1079 ++data->nd;
1082 static evalue *ParamLine_Length_mod(Polyhedron *P, Polyhedron *C, unsigned MaxRays)
1084 unsigned dim = P->Dimension;
1085 unsigned nvar = dim - C->Dimension;
1086 struct PLL_data data;
1087 evalue *F;
1088 int k;
1090 assert(nvar == 1);
1092 value_init(data.mone.d);
1093 evalue_set_si(&data.mone, -1, 1);
1095 data.nd = 0;
1096 data.MaxRays = MaxRays;
1097 data.C = C;
1098 for_each_lower_upper_bound(P, PLL_init, PLL_cb, &data);
1100 free_evalue_refs(&data.mone);
1102 if (data.nd == 0) {
1103 free(data.s);
1104 return evalue_zero();
1107 F = ALLOC(evalue);
1108 value_init(F->d);
1109 value_set_si(F->d, 0);
1110 F->x.p = new_enode(partition, 2*data.nd, dim-nvar);
1111 for (k = 0; k < data.nd; ++k) {
1112 EVALUE_SET_DOMAIN(F->x.p->arr[2*k], data.s[k].D);
1113 value_clear(F->x.p->arr[2*k+1].d);
1114 F->x.p->arr[2*k+1] = data.s[k].E;
1116 free(data.s);
1118 return F;
1121 evalue* ParamLine_Length(Polyhedron *P, Polyhedron *C,
1122 struct barvinok_options *options)
1124 evalue* tmp;
1125 tmp = ParamLine_Length_mod(P, C, options->MaxRays);
1126 if (options->lookup_table) {
1127 evalue_mod2table(tmp, C->Dimension);
1128 reduce_evalue(tmp);
1130 return tmp;
1133 Bool isIdentity(Matrix *M)
1135 unsigned i, j;
1136 if (M->NbRows != M->NbColumns)
1137 return False;
1139 for (i = 0;i < M->NbRows; i ++)
1140 for (j = 0; j < M->NbColumns; j ++)
1141 if (i == j) {
1142 if(value_notone_p(M->p[i][j]))
1143 return False;
1144 } else {
1145 if(value_notzero_p(M->p[i][j]))
1146 return False;
1148 return True;
1151 void Param_Polyhedron_Print(FILE* DST, Param_Polyhedron *PP,
1152 const char **param_names)
1154 Param_Domain *P;
1155 Param_Vertices *V;
1157 for(P=PP->D;P;P=P->next) {
1159 /* prints current val. dom. */
1160 fprintf(DST, "---------------------------------------\n");
1161 fprintf(DST, "Domain :\n");
1162 Print_Domain(DST, P->Domain, param_names);
1164 /* scan the vertices */
1165 fprintf(DST, "Vertices :\n");
1166 FORALL_PVertex_in_ParamPolyhedron(V,P,PP) {
1168 /* prints each vertex */
1169 Print_Vertex(DST, V->Vertex, param_names);
1170 fprintf(DST, "\n");
1172 END_FORALL_PVertex_in_ParamPolyhedron;
1176 void Enumeration_Print(FILE *Dst, Enumeration *en, const char **params)
1178 for (; en; en = en->next) {
1179 Print_Domain(Dst, en->ValidityDomain, params);
1180 print_evalue(Dst, &en->EP, params);
1184 void Enumeration_Free(Enumeration *en)
1186 Enumeration *ee;
1188 while( en )
1190 free_evalue_refs( &(en->EP) );
1191 Domain_Free( en->ValidityDomain );
1192 ee = en ->next;
1193 free( en );
1194 en = ee;
1198 void Enumeration_mod2table(Enumeration *en, unsigned nparam)
1200 for (; en; en = en->next) {
1201 evalue_mod2table(&en->EP, nparam);
1202 reduce_evalue(&en->EP);
1206 size_t Enumeration_size(Enumeration *en)
1208 size_t s = 0;
1210 for (; en; en = en->next) {
1211 s += domain_size(en->ValidityDomain);
1212 s += evalue_size(&en->EP);
1214 return s;
1217 /* Check whether every set in D2 is included in some set of D1 */
1218 int DomainIncludes(Polyhedron *D1, Polyhedron *D2)
1220 for ( ; D2; D2 = D2->next) {
1221 Polyhedron *P1;
1222 for (P1 = D1; P1; P1 = P1->next)
1223 if (PolyhedronIncludes(P1, D2))
1224 break;
1225 if (!P1)
1226 return 0;
1228 return 1;
1231 int line_minmax(Polyhedron *I, Value *min, Value *max)
1233 int i;
1235 if (I->NbEq >= 1) {
1236 value_oppose(I->Constraint[0][2], I->Constraint[0][2]);
1237 /* There should never be a remainder here */
1238 if (value_pos_p(I->Constraint[0][1]))
1239 mpz_fdiv_q(*min, I->Constraint[0][2], I->Constraint[0][1]);
1240 else
1241 mpz_fdiv_q(*min, I->Constraint[0][2], I->Constraint[0][1]);
1242 value_assign(*max, *min);
1243 } else for (i = 0; i < I->NbConstraints; ++i) {
1244 if (value_zero_p(I->Constraint[i][1])) {
1245 Polyhedron_Free(I);
1246 return 0;
1249 value_oppose(I->Constraint[i][2], I->Constraint[i][2]);
1250 if (value_pos_p(I->Constraint[i][1]))
1251 mpz_cdiv_q(*min, I->Constraint[i][2], I->Constraint[i][1]);
1252 else
1253 mpz_fdiv_q(*max, I->Constraint[i][2], I->Constraint[i][1]);
1255 Polyhedron_Free(I);
1256 return 1;
1259 int DomainContains(Polyhedron *P, Value *list_args, int len,
1260 unsigned MaxRays, int set)
1262 int i;
1263 Value m;
1265 if (P->Dimension == len)
1266 return in_domain(P, list_args);
1268 assert(set); // assume list_args is large enough
1269 assert((P->Dimension - len) % 2 == 0);
1270 value_init(m);
1271 for (i = 0; i < P->Dimension - len; i += 2) {
1272 int j, k;
1273 for (j = 0 ; j < P->NbEq; ++j)
1274 if (value_notzero_p(P->Constraint[j][1+len+i]))
1275 break;
1276 assert(j < P->NbEq);
1277 value_absolute(m, P->Constraint[j][1+len+i]);
1278 k = First_Non_Zero(P->Constraint[j]+1, len);
1279 assert(k != -1);
1280 assert(First_Non_Zero(P->Constraint[j]+1+k+1, len - k - 1) == -1);
1281 mpz_fdiv_q(list_args[len+i], list_args[k], m);
1282 mpz_fdiv_r(list_args[len+i+1], list_args[k], m);
1284 value_clear(m);
1286 return in_domain(P, list_args);
1289 Polyhedron *DomainConcat(Polyhedron *head, Polyhedron *tail)
1291 Polyhedron *S;
1292 if (!head)
1293 return tail;
1294 for (S = head; S->next; S = S->next)
1296 S->next = tail;
1297 return head;
1300 evalue *barvinok_lexsmaller_ev(Polyhedron *P, Polyhedron *D, unsigned dim,
1301 Polyhedron *C, unsigned MaxRays)
1303 evalue *ranking;
1304 Polyhedron *RC, *RD, *Q;
1305 unsigned nparam = dim + C->Dimension;
1306 unsigned exist;
1307 Polyhedron *CA;
1309 RC = LexSmaller(P, D, dim, C, MaxRays);
1310 RD = RC->next;
1311 RC->next = NULL;
1313 exist = RD->Dimension - nparam - dim;
1314 CA = align_context(RC, RD->Dimension, MaxRays);
1315 Q = DomainIntersection(RD, CA, MaxRays);
1316 Polyhedron_Free(CA);
1317 Domain_Free(RD);
1318 Polyhedron_Free(RC);
1319 RD = Q;
1321 for (Q = RD; Q; Q = Q->next) {
1322 evalue *t;
1323 Polyhedron *next = Q->next;
1324 Q->next = 0;
1326 t = barvinok_enumerate_e(Q, exist, nparam, MaxRays);
1328 if (Q == RD)
1329 ranking = t;
1330 else {
1331 eadd(t, ranking);
1332 evalue_free(t);
1335 Q->next = next;
1338 Domain_Free(RD);
1340 return ranking;
1343 Enumeration *barvinok_lexsmaller(Polyhedron *P, Polyhedron *D, unsigned dim,
1344 Polyhedron *C, unsigned MaxRays)
1346 evalue *EP = barvinok_lexsmaller_ev(P, D, dim, C, MaxRays);
1348 return partition2enumeration(EP);
1351 /* "align" matrix to have nrows by inserting
1352 * the necessary number of rows and an equal number of columns in front
1354 Matrix *align_matrix(Matrix *M, int nrows)
1356 int i;
1357 int newrows = nrows - M->NbRows;
1358 Matrix *M2 = Matrix_Alloc(nrows, newrows + M->NbColumns);
1359 for (i = 0; i < newrows; ++i)
1360 value_set_si(M2->p[i][i], 1);
1361 for (i = 0; i < M->NbRows; ++i)
1362 Vector_Copy(M->p[i], M2->p[newrows+i]+newrows, M->NbColumns);
1363 return M2;
1366 static void print_varlist(FILE *out, int n, char **names)
1368 int i;
1369 fprintf(out, "[");
1370 for (i = 0; i < n; ++i) {
1371 if (i)
1372 fprintf(out, ",");
1373 fprintf(out, "%s", names[i]);
1375 fprintf(out, "]");
1378 static void print_term(FILE *out, Value v, int pos, int dim, int nparam,
1379 char **iter_names, char **param_names, int *first)
1381 if (value_zero_p(v)) {
1382 if (first && *first && pos >= dim + nparam)
1383 fprintf(out, "0");
1384 return;
1387 if (first) {
1388 if (!*first && value_pos_p(v))
1389 fprintf(out, "+");
1390 *first = 0;
1392 if (pos < dim + nparam) {
1393 if (value_mone_p(v))
1394 fprintf(out, "-");
1395 else if (!value_one_p(v))
1396 value_print(out, VALUE_FMT, v);
1397 if (pos < dim)
1398 fprintf(out, "%s", iter_names[pos]);
1399 else
1400 fprintf(out, "%s", param_names[pos-dim]);
1401 } else
1402 value_print(out, VALUE_FMT, v);
1405 char **util_generate_names(int n, const char *prefix)
1407 int i;
1408 int len = (prefix ? strlen(prefix) : 0) + 10;
1409 char **names = ALLOCN(char*, n);
1410 if (!names) {
1411 fprintf(stderr, "ERROR: memory overflow.\n");
1412 exit(1);
1414 for (i = 0; i < n; ++i) {
1415 names[i] = ALLOCN(char, len);
1416 if (!names[i]) {
1417 fprintf(stderr, "ERROR: memory overflow.\n");
1418 exit(1);
1420 if (!prefix)
1421 snprintf(names[i], len, "%d", i);
1422 else
1423 snprintf(names[i], len, "%s%d", prefix, i);
1426 return names;
1429 void util_free_names(int n, char **names)
1431 int i;
1432 for (i = 0; i < n; ++i)
1433 free(names[i]);
1434 free(names);
1437 void Polyhedron_pprint(FILE *out, Polyhedron *P, int dim, int nparam,
1438 char **iter_names, char **param_names)
1440 int i, j;
1441 Value tmp;
1443 assert(dim + nparam == P->Dimension);
1445 value_init(tmp);
1447 fprintf(out, "{ ");
1448 if (nparam) {
1449 print_varlist(out, nparam, param_names);
1450 fprintf(out, " -> ");
1452 print_varlist(out, dim, iter_names);
1453 fprintf(out, " : ");
1455 if (emptyQ2(P))
1456 fprintf(out, "FALSE");
1457 else for (i = 0; i < P->NbConstraints; ++i) {
1458 int first = 1;
1459 int v = First_Non_Zero(P->Constraint[i]+1, P->Dimension);
1460 if (v == -1 && value_pos_p(P->Constraint[i][0]))
1461 continue;
1462 if (i)
1463 fprintf(out, " && ");
1464 if (v == -1 && value_notzero_p(P->Constraint[i][1+P->Dimension]))
1465 fprintf(out, "FALSE");
1466 else if (value_pos_p(P->Constraint[i][v+1])) {
1467 print_term(out, P->Constraint[i][v+1], v, dim, nparam,
1468 iter_names, param_names, NULL);
1469 if (value_zero_p(P->Constraint[i][0]))
1470 fprintf(out, " = ");
1471 else
1472 fprintf(out, " >= ");
1473 for (j = v+1; j <= dim+nparam; ++j) {
1474 value_oppose(tmp, P->Constraint[i][1+j]);
1475 print_term(out, tmp, j, dim, nparam,
1476 iter_names, param_names, &first);
1478 } else {
1479 value_oppose(tmp, P->Constraint[i][1+v]);
1480 print_term(out, tmp, v, dim, nparam,
1481 iter_names, param_names, NULL);
1482 fprintf(out, " <= ");
1483 for (j = v+1; j <= dim+nparam; ++j)
1484 print_term(out, P->Constraint[i][1+j], j, dim, nparam,
1485 iter_names, param_names, &first);
1489 fprintf(out, " }\n");
1491 value_clear(tmp);
1494 /* Construct a cone over P with P placed at x_d = 1, with
1495 * x_d the coordinate of an extra dimension
1497 * It's probably a mistake to depend so much on the internal
1498 * representation. We should probably simply compute the
1499 * vertices/facets first.
1501 Polyhedron *Cone_over_Polyhedron(Polyhedron *P)
1503 unsigned NbConstraints = 0;
1504 unsigned NbRays = 0;
1505 Polyhedron *C;
1506 int i;
1508 if (POL_HAS(P, POL_INEQUALITIES))
1509 NbConstraints = P->NbConstraints + 1;
1510 if (POL_HAS(P, POL_POINTS))
1511 NbRays = P->NbRays + 1;
1513 C = Polyhedron_Alloc(P->Dimension+1, NbConstraints, NbRays);
1514 if (POL_HAS(P, POL_INEQUALITIES)) {
1515 C->NbEq = P->NbEq;
1516 for (i = 0; i < P->NbConstraints; ++i)
1517 Vector_Copy(P->Constraint[i], C->Constraint[i], P->Dimension+2);
1518 /* n >= 0 */
1519 value_set_si(C->Constraint[P->NbConstraints][0], 1);
1520 value_set_si(C->Constraint[P->NbConstraints][1+P->Dimension], 1);
1522 if (POL_HAS(P, POL_POINTS)) {
1523 C->NbBid = P->NbBid;
1524 for (i = 0; i < P->NbRays; ++i)
1525 Vector_Copy(P->Ray[i], C->Ray[i], P->Dimension+2);
1526 /* vertex 0 */
1527 value_set_si(C->Ray[P->NbRays][0], 1);
1528 value_set_si(C->Ray[P->NbRays][1+C->Dimension], 1);
1530 POL_SET(C, POL_VALID);
1531 if (POL_HAS(P, POL_INEQUALITIES))
1532 POL_SET(C, POL_INEQUALITIES);
1533 if (POL_HAS(P, POL_POINTS))
1534 POL_SET(C, POL_POINTS);
1535 if (POL_HAS(P, POL_VERTICES))
1536 POL_SET(C, POL_VERTICES);
1537 return C;
1540 /* Returns a (dim+nparam+1)x((dim-n)+nparam+1) matrix
1541 * mapping the transformed subspace back to the original space.
1542 * n is the number of equalities involving the variables
1543 * (i.e., not purely the parameters).
1544 * The remaining n coordinates in the transformed space would
1545 * have constant (parametric) values and are therefore not
1546 * included in the variables of the new space.
1548 Matrix *compress_variables(Matrix *Equalities, unsigned nparam)
1550 unsigned dim = (Equalities->NbColumns-2) - nparam;
1551 Matrix *M, *H, *Q, *U, *C, *ratH, *invH, *Ul, *T1, *T2, *T;
1552 Value mone;
1553 int n, i, j;
1554 int ok;
1556 for (n = 0; n < Equalities->NbRows; ++n)
1557 if (First_Non_Zero(Equalities->p[n]+1, dim) == -1)
1558 break;
1559 if (n == 0)
1560 return Identity(dim+nparam+1);
1561 value_init(mone);
1562 value_set_si(mone, -1);
1563 M = Matrix_Alloc(n, dim);
1564 C = Matrix_Alloc(n+1, nparam+1);
1565 for (i = 0; i < n; ++i) {
1566 Vector_Copy(Equalities->p[i]+1, M->p[i], dim);
1567 Vector_Scale(Equalities->p[i]+1+dim, C->p[i], mone, nparam+1);
1569 value_set_si(C->p[n][nparam], 1);
1570 left_hermite(M, &H, &Q, &U);
1571 Matrix_Free(M);
1572 Matrix_Free(Q);
1573 value_clear(mone);
1575 ratH = Matrix_Alloc(n+1, n+1);
1576 invH = Matrix_Alloc(n+1, n+1);
1577 for (i = 0; i < n; ++i)
1578 Vector_Copy(H->p[i], ratH->p[i], n);
1579 value_set_si(ratH->p[n][n], 1);
1580 ok = Matrix_Inverse(ratH, invH);
1581 assert(ok);
1582 Matrix_Free(H);
1583 Matrix_Free(ratH);
1584 T1 = Matrix_Alloc(n+1, nparam+1);
1585 Matrix_Product(invH, C, T1);
1586 Matrix_Free(C);
1587 Matrix_Free(invH);
1588 if (value_notone_p(T1->p[n][nparam])) {
1589 for (i = 0; i < n; ++i) {
1590 if (!mpz_divisible_p(T1->p[i][nparam], T1->p[n][nparam])) {
1591 Matrix_Free(T1);
1592 Matrix_Free(U);
1593 return NULL;
1595 /* compress_params should have taken care of this */
1596 for (j = 0; j < nparam; ++j)
1597 assert(mpz_divisible_p(T1->p[i][j], T1->p[n][nparam]));
1598 Vector_AntiScale(T1->p[i], T1->p[i], T1->p[n][nparam], nparam+1);
1600 value_set_si(T1->p[n][nparam], 1);
1602 Ul = Matrix_Alloc(dim+1, n+1);
1603 for (i = 0; i < dim; ++i)
1604 Vector_Copy(U->p[i], Ul->p[i], n);
1605 value_set_si(Ul->p[dim][n], 1);
1606 T2 = Matrix_Alloc(dim+1, nparam+1);
1607 Matrix_Product(Ul, T1, T2);
1608 Matrix_Free(Ul);
1609 Matrix_Free(T1);
1611 T = Matrix_Alloc(dim+nparam+1, (dim-n)+nparam+1);
1612 for (i = 0; i < dim; ++i) {
1613 Vector_Copy(U->p[i]+n, T->p[i], dim-n);
1614 Vector_Copy(T2->p[i], T->p[i]+dim-n, nparam+1);
1616 for (i = 0; i < nparam+1; ++i)
1617 value_set_si(T->p[dim+i][(dim-n)+i], 1);
1618 assert(value_one_p(T2->p[dim][nparam]));
1619 Matrix_Free(U);
1620 Matrix_Free(T2);
1622 return T;
1625 /* Computes the left inverse of an affine embedding M and, if Eq is not NULL,
1626 * the equalities that define the affine subspace onto which M maps
1627 * its argument.
1629 Matrix *left_inverse(Matrix *M, Matrix **Eq)
1631 int i, ok;
1632 Matrix *L, *H, *Q, *U, *ratH, *invH, *Ut, *inv;
1633 Vector *t;
1635 if (M->NbColumns == 1) {
1636 inv = Matrix_Alloc(1, M->NbRows);
1637 value_set_si(inv->p[0][M->NbRows-1], 1);
1638 if (Eq) {
1639 *Eq = Matrix_Alloc(M->NbRows-1, 1+(M->NbRows-1)+1);
1640 for (i = 0; i < M->NbRows-1; ++i) {
1641 value_oppose((*Eq)->p[i][1+i], M->p[M->NbRows-1][0]);
1642 value_assign((*Eq)->p[i][1+(M->NbRows-1)], M->p[i][0]);
1645 return inv;
1647 if (Eq)
1648 *Eq = NULL;
1649 L = Matrix_Alloc(M->NbRows-1, M->NbColumns-1);
1650 for (i = 0; i < L->NbRows; ++i)
1651 Vector_Copy(M->p[i], L->p[i], L->NbColumns);
1652 right_hermite(L, &H, &U, &Q);
1653 Matrix_Free(L);
1654 Matrix_Free(Q);
1655 t = Vector_Alloc(U->NbColumns);
1656 for (i = 0; i < U->NbColumns; ++i)
1657 value_oppose(t->p[i], M->p[i][M->NbColumns-1]);
1658 if (Eq) {
1659 *Eq = Matrix_Alloc(H->NbRows - H->NbColumns, 2 + U->NbColumns);
1660 for (i = 0; i < H->NbRows - H->NbColumns; ++i) {
1661 Vector_Copy(U->p[H->NbColumns+i], (*Eq)->p[i]+1, U->NbColumns);
1662 Inner_Product(U->p[H->NbColumns+i], t->p, U->NbColumns,
1663 (*Eq)->p[i]+1+U->NbColumns);
1666 ratH = Matrix_Alloc(H->NbColumns+1, H->NbColumns+1);
1667 invH = Matrix_Alloc(H->NbColumns+1, H->NbColumns+1);
1668 for (i = 0; i < H->NbColumns; ++i)
1669 Vector_Copy(H->p[i], ratH->p[i], H->NbColumns);
1670 value_set_si(ratH->p[ratH->NbRows-1][ratH->NbColumns-1], 1);
1671 Matrix_Free(H);
1672 ok = Matrix_Inverse(ratH, invH);
1673 assert(ok);
1674 Matrix_Free(ratH);
1675 Ut = Matrix_Alloc(invH->NbRows, U->NbColumns+1);
1676 for (i = 0; i < Ut->NbRows-1; ++i) {
1677 Vector_Copy(U->p[i], Ut->p[i], U->NbColumns);
1678 Inner_Product(U->p[i], t->p, U->NbColumns, &Ut->p[i][Ut->NbColumns-1]);
1680 Matrix_Free(U);
1681 Vector_Free(t);
1682 value_set_si(Ut->p[Ut->NbRows-1][Ut->NbColumns-1], 1);
1683 inv = Matrix_Alloc(invH->NbRows, Ut->NbColumns);
1684 Matrix_Product(invH, Ut, inv);
1685 Matrix_Free(Ut);
1686 Matrix_Free(invH);
1687 return inv;
1690 /* Check whether all rays are revlex positive in the parameters
1692 int Polyhedron_has_revlex_positive_rays(Polyhedron *P, unsigned nparam)
1694 int r;
1695 for (r = 0; r < P->NbRays; ++r) {
1696 int i;
1697 if (value_notzero_p(P->Ray[r][P->Dimension+1]))
1698 continue;
1699 for (i = P->Dimension-1; i >= P->Dimension-nparam; --i) {
1700 if (value_neg_p(P->Ray[r][i+1]))
1701 return 0;
1702 if (value_pos_p(P->Ray[r][i+1]))
1703 break;
1705 /* A ray independent of the parameters */
1706 if (i < P->Dimension-nparam)
1707 return 0;
1709 return 1;
1712 static Polyhedron *Recession_Cone(Polyhedron *P, unsigned nparam, unsigned MaxRays)
1714 int i;
1715 unsigned nvar = P->Dimension - nparam;
1716 Matrix *M = Matrix_Alloc(P->NbConstraints, 1 + nvar + 1);
1717 Polyhedron *R;
1718 for (i = 0; i < P->NbConstraints; ++i)
1719 Vector_Copy(P->Constraint[i], M->p[i], 1+nvar);
1720 R = Constraints2Polyhedron(M, MaxRays);
1721 Matrix_Free(M);
1722 return R;
1725 int Polyhedron_is_unbounded(Polyhedron *P, unsigned nparam, unsigned MaxRays)
1727 int i;
1728 int is_unbounded;
1729 Polyhedron *R = Recession_Cone(P, nparam, MaxRays);
1730 POL_ENSURE_VERTICES(R);
1731 if (R->NbBid == 0)
1732 for (i = 0; i < R->NbRays; ++i)
1733 if (value_zero_p(R->Ray[i][1+R->Dimension]))
1734 break;
1735 is_unbounded = R->NbBid > 0 || i < R->NbRays;
1736 Polyhedron_Free(R);
1737 return is_unbounded;
1740 static void SwapColumns(Value **V, int n, int i, int j)
1742 int r;
1744 for (r = 0; r < n; ++r)
1745 value_swap(V[r][i], V[r][j]);
1748 void Polyhedron_ExchangeColumns(Polyhedron *P, int Column1, int Column2)
1750 SwapColumns(P->Constraint, P->NbConstraints, Column1, Column2);
1751 SwapColumns(P->Ray, P->NbRays, Column1, Column2);
1752 if (P->NbEq) {
1753 Matrix M;
1754 Polyhedron_Matrix_View(P, &M, P->NbConstraints);
1755 Gauss(&M, P->NbEq, P->Dimension+1);
1759 /* perform transposition inline; assumes M is a square matrix */
1760 void Matrix_Transposition(Matrix *M)
1762 int i, j;
1764 assert(M->NbRows == M->NbColumns);
1765 for (i = 0; i < M->NbRows; ++i)
1766 for (j = i+1; j < M->NbColumns; ++j)
1767 value_swap(M->p[i][j], M->p[j][i]);
1770 /* Matrix "view" of first rows rows */
1771 void Polyhedron_Matrix_View(Polyhedron *P, Matrix *M, unsigned rows)
1773 M->NbRows = rows;
1774 M->NbColumns = P->Dimension+2;
1775 M->p_Init = P->p_Init;
1776 M->p = P->Constraint;
1779 int Last_Non_Zero(Value *p, unsigned len)
1781 int i;
1783 for (i = len - 1; i >= 0; --i)
1784 if (value_notzero_p(p[i]))
1785 return i;
1787 return -1;