Makefile.am: check-evalue: print name of each test file
[barvinok.git] / util.c
blob082d5db13f110e6dcd992753c4efe8c98d7f977f
1 #include <stdlib.h>
2 #include <assert.h>
3 #include <barvinok/util.h>
4 #include <barvinok/options.h>
5 #include <polylib/ranking.h>
6 #include "config.h"
8 #define ALLOC(type) (type*)malloc(sizeof(type))
9 #define ALLOCN(type,n) (type*)malloc((n) * sizeof(type))
11 #ifdef __GNUC__
12 #define NALLOC(p,n) p = (typeof(p))malloc((n) * sizeof(*p))
13 #else
14 #define NALLOC(p,n) p = (void *)malloc((n) * sizeof(*p))
15 #endif
17 void manual_count(Polyhedron *P, Value* result)
19 Polyhedron *U = Universe_Polyhedron(0);
20 Enumeration *en = Polyhedron_Enumerate(P,U,1024,NULL);
21 Value *v = compute_poly(en,NULL);
22 value_assign(*result, *v);
23 value_clear(*v);
24 free(v);
25 Enumeration_Free(en);
26 Polyhedron_Free(U);
29 #include <barvinok/evalue.h>
30 #include <barvinok/util.h>
31 #include <barvinok/barvinok.h>
33 /* Return random value between 0 and max-1 inclusive
35 int random_int(int max) {
36 return (int) (((double)(max))*rand()/(RAND_MAX+1.0));
39 Polyhedron *Polyhedron_Read(unsigned MaxRays)
41 int vertices = 0;
42 unsigned NbRows, NbColumns;
43 Matrix *M;
44 Polyhedron *P;
45 char s[128];
47 while (fgets(s, sizeof(s), stdin)) {
48 if (*s == '#')
49 continue;
50 if (strncasecmp(s, "vertices", sizeof("vertices")-1) == 0)
51 vertices = 1;
52 if (sscanf(s, "%u %u", &NbRows, &NbColumns) == 2)
53 break;
55 if (feof(stdin))
56 return NULL;
57 M = Matrix_Alloc(NbRows,NbColumns);
58 Matrix_Read_Input(M);
59 if (vertices)
60 P = Rays2Polyhedron(M, MaxRays);
61 else
62 P = Constraints2Polyhedron(M, MaxRays);
63 Matrix_Free(M);
64 return P;
67 /* Inplace polarization
69 void Polyhedron_Polarize(Polyhedron *P)
71 unsigned NbRows = P->NbConstraints + P->NbRays;
72 int i;
73 Value **q;
75 q = (Value **)malloc(NbRows * sizeof(Value *));
76 assert(q);
77 for (i = 0; i < P->NbRays; ++i)
78 q[i] = P->Ray[i];
79 for (; i < NbRows; ++i)
80 q[i] = P->Constraint[i-P->NbRays];
81 P->NbConstraints = NbRows - P->NbConstraints;
82 P->NbRays = NbRows - P->NbRays;
83 free(P->Constraint);
84 P->Constraint = q;
85 P->Ray = q + P->NbConstraints;
89 * Rather general polar
90 * We can optimize it significantly if we assume that
91 * P includes zero
93 * Also, we calculate the polar as defined in Schrijver
94 * The opposite should probably work as well and would
95 * eliminate the need for multiplying by -1
97 Polyhedron* Polyhedron_Polar(Polyhedron *P, unsigned NbMaxRays)
99 int i;
100 Value mone;
101 unsigned dim = P->Dimension + 2;
102 Matrix *M = Matrix_Alloc(P->NbRays, dim);
104 assert(M);
105 value_init(mone);
106 value_set_si(mone, -1);
107 for (i = 0; i < P->NbRays; ++i) {
108 Vector_Scale(P->Ray[i], M->p[i], mone, dim);
109 value_multiply(M->p[i][0], M->p[i][0], mone);
110 value_multiply(M->p[i][dim-1], M->p[i][dim-1], mone);
112 P = Constraints2Polyhedron(M, NbMaxRays);
113 assert(P);
114 Matrix_Free(M);
115 value_clear(mone);
116 return P;
120 * Returns the supporting cone of P at the vertex with index v
122 Polyhedron* supporting_cone(Polyhedron *P, int v)
124 Matrix *M;
125 Value tmp;
126 int i, n, j;
127 unsigned char *supporting = (unsigned char *)malloc(P->NbConstraints);
128 unsigned dim = P->Dimension + 2;
130 assert(v >=0 && v < P->NbRays);
131 assert(value_pos_p(P->Ray[v][dim-1]));
132 assert(supporting);
134 value_init(tmp);
135 for (i = 0, n = 0; i < P->NbConstraints; ++i) {
136 Inner_Product(P->Constraint[i] + 1, P->Ray[v] + 1, dim - 1, &tmp);
137 if ((supporting[i] = value_zero_p(tmp)))
138 ++n;
140 assert(n >= dim - 2);
141 value_clear(tmp);
142 M = Matrix_Alloc(n, dim);
143 assert(M);
144 for (i = 0, j = 0; i < P->NbConstraints; ++i)
145 if (supporting[i]) {
146 value_set_si(M->p[j][dim-1], 0);
147 Vector_Copy(P->Constraint[i], M->p[j++], dim-1);
149 free(supporting);
150 P = Constraints2Polyhedron(M, P->NbRays+1);
151 assert(P);
152 Matrix_Free(M);
153 return P;
156 #define INT_BITS (sizeof(unsigned) * 8)
158 unsigned *supporting_constraints(Matrix *Constraints, Param_Vertices *v, int *n)
160 Value lcm, tmp, tmp2;
161 unsigned dim = Constraints->NbColumns;
162 unsigned nparam = v->Vertex->NbColumns - 2;
163 unsigned nvar = dim - nparam - 2;
164 int len = (Constraints->NbRows+INT_BITS-1)/INT_BITS;
165 unsigned *supporting = (unsigned *)calloc(len, sizeof(unsigned));
166 int i, j;
167 Vector *row;
168 int ix;
169 unsigned bx;
171 assert(supporting);
172 row = Vector_Alloc(nparam+1);
173 assert(row);
174 value_init(lcm);
175 value_init(tmp);
176 value_init(tmp2);
177 value_set_si(lcm, 1);
178 for (i = 0, *n = 0, ix = 0, bx = MSB; i < Constraints->NbRows; ++i) {
179 Vector_Set(row->p, 0, nparam+1);
180 for (j = 0 ; j < nvar; ++j) {
181 value_set_si(tmp, 1);
182 value_assign(tmp2, Constraints->p[i][j+1]);
183 if (value_ne(lcm, v->Vertex->p[j][nparam+1])) {
184 value_assign(tmp, lcm);
185 value_lcm(lcm, lcm, v->Vertex->p[j][nparam+1]);
186 value_division(tmp, lcm, tmp);
187 value_multiply(tmp2, tmp2, lcm);
188 value_division(tmp2, tmp2, v->Vertex->p[j][nparam+1]);
190 Vector_Combine(row->p, v->Vertex->p[j], row->p,
191 tmp, tmp2, nparam+1);
193 value_set_si(tmp, 1);
194 Vector_Combine(row->p, Constraints->p[i]+1+nvar, row->p, tmp, lcm, nparam+1);
195 for (j = 0; j < nparam+1; ++j)
196 if (value_notzero_p(row->p[j]))
197 break;
198 if (j == nparam + 1) {
199 supporting[ix] |= bx;
200 ++*n;
202 NEXT(ix, bx);
204 assert(*n >= nvar);
205 value_clear(tmp);
206 value_clear(tmp2);
207 value_clear(lcm);
208 Vector_Free(row);
210 return supporting;
213 Polyhedron* supporting_cone_p(Polyhedron *P, Param_Vertices *v)
215 Matrix *M;
216 unsigned dim = P->Dimension + 2;
217 unsigned nparam = v->Vertex->NbColumns - 2;
218 unsigned nvar = dim - nparam - 2;
219 int i, n, j;
220 int ix;
221 unsigned bx;
222 unsigned *supporting;
223 Matrix View;
225 Polyhedron_Matrix_View(P, &View, P->NbConstraints);
226 supporting = supporting_constraints(&View, v, &n);
227 M = Matrix_Alloc(n, nvar+2);
228 assert(M);
229 for (i = 0, j = 0, ix = 0, bx = MSB; i < P->NbConstraints; ++i) {
230 if (supporting[ix] & bx) {
231 value_set_si(M->p[j][nvar+1], 0);
232 Vector_Copy(P->Constraint[i], M->p[j++], nvar+1);
234 NEXT(ix, bx);
236 free(supporting);
237 P = Constraints2Polyhedron(M, P->NbRays+1);
238 assert(P);
239 Matrix_Free(M);
240 return P;
243 Polyhedron* triangulate_cone(Polyhedron *P, unsigned NbMaxCons)
245 struct barvinok_options *options = barvinok_options_new_with_defaults();
246 options->MaxRays = NbMaxCons;
247 P = triangulate_cone_with_options(P, options);
248 barvinok_options_free(options);
249 return P;
252 Polyhedron* triangulate_cone_with_options(Polyhedron *P,
253 struct barvinok_options *options)
255 const static int MAX_TRY=10;
256 int i, j, r, n, t;
257 Value tmp;
258 unsigned dim = P->Dimension;
259 Matrix *M = Matrix_Alloc(P->NbRays+1, dim+3);
260 Matrix *M2, *M3;
261 Polyhedron *L, *R, *T;
262 assert(P->NbEq == 0);
264 L = NULL;
265 R = NULL;
266 value_init(tmp);
268 Vector_Set(M->p[0]+1, 0, dim+1);
269 value_set_si(M->p[0][0], 1);
270 value_set_si(M->p[0][dim+2], 1);
271 Vector_Set(M->p[P->NbRays]+1, 0, dim+2);
272 value_set_si(M->p[P->NbRays][0], 1);
273 value_set_si(M->p[P->NbRays][dim+1], 1);
275 for (i = 0, r = 1; i < P->NbRays; ++i) {
276 if (value_notzero_p(P->Ray[i][dim+1]))
277 continue;
278 Vector_Copy(P->Ray[i], M->p[r], dim+1);
279 value_set_si(M->p[r][dim+2], 0);
280 ++r;
283 M2 = Matrix_Alloc(dim+1, dim+2);
285 t = 0;
286 if (options->try_Delaunay_triangulation) {
287 /* Delaunay triangulation */
288 for (r = 1; r < P->NbRays; ++r) {
289 Inner_Product(M->p[r]+1, M->p[r]+1, dim, &tmp);
290 value_assign(M->p[r][dim+1], tmp);
292 M3 = Matrix_Copy(M);
293 L = Rays2Polyhedron(M3, options->MaxRays);
294 Matrix_Free(M3);
295 ++t;
296 } else {
297 try_again:
298 /* Usually R should still be 0 */
299 Domain_Free(R);
300 Polyhedron_Free(L);
301 for (r = 1; r < P->NbRays; ++r) {
302 value_set_si(M->p[r][dim+1], random_int((t+1)*dim*P->NbRays)+1);
304 M3 = Matrix_Copy(M);
305 L = Rays2Polyhedron(M3, options->MaxRays);
306 Matrix_Free(M3);
307 ++t;
309 assert(t <= MAX_TRY);
311 R = NULL;
312 n = 0;
314 POL_ENSURE_FACETS(L);
315 for (i = 0; i < L->NbConstraints; ++i) {
316 /* Ignore perpendicular facets, i.e., facets with 0 z-coordinate */
317 if (value_negz_p(L->Constraint[i][dim+1]))
318 continue;
319 if (value_notzero_p(L->Constraint[i][dim+2]))
320 continue;
321 for (j = 1, r = 1; j < M->NbRows; ++j) {
322 Inner_Product(M->p[j]+1, L->Constraint[i]+1, dim+1, &tmp);
323 if (value_notzero_p(tmp))
324 continue;
325 if (r > dim)
326 goto try_again;
327 Vector_Copy(M->p[j]+1, M2->p[r]+1, dim);
328 value_set_si(M2->p[r][0], 1);
329 value_set_si(M2->p[r][dim+1], 0);
330 ++r;
332 assert(r == dim+1);
333 Vector_Set(M2->p[0]+1, 0, dim);
334 value_set_si(M2->p[0][0], 1);
335 value_set_si(M2->p[0][dim+1], 1);
336 T = Rays2Polyhedron(M2, P->NbConstraints+1);
337 T->next = R;
338 R = T;
339 ++n;
341 Matrix_Free(M2);
343 Polyhedron_Free(L);
344 value_clear(tmp);
345 Matrix_Free(M);
347 return R;
350 void check_triangulization(Polyhedron *P, Polyhedron *T)
352 Polyhedron *C, *D, *E, *F, *G, *U;
353 for (C = T; C; C = C->next) {
354 if (C == T)
355 U = C;
356 else
357 U = DomainConvex(DomainUnion(U, C, 100), 100);
358 for (D = C->next; D; D = D->next) {
359 F = C->next;
360 G = D->next;
361 C->next = NULL;
362 D->next = NULL;
363 E = DomainIntersection(C, D, 600);
364 assert(E->NbRays == 0 || E->NbEq >= 1);
365 Polyhedron_Free(E);
366 C->next = F;
367 D->next = G;
370 assert(PolyhedronIncludes(U, P));
371 assert(PolyhedronIncludes(P, U));
374 /* Computes x, y and g such that g = gcd(a,b) and a*x+b*y = g */
375 void Extended_Euclid(Value a, Value b, Value *x, Value *y, Value *g)
377 Value c, d, e, f, tmp;
379 value_init(c);
380 value_init(d);
381 value_init(e);
382 value_init(f);
383 value_init(tmp);
384 value_absolute(c, a);
385 value_absolute(d, b);
386 value_set_si(e, 1);
387 value_set_si(f, 0);
388 while(value_pos_p(d)) {
389 value_division(tmp, c, d);
390 value_multiply(tmp, tmp, f);
391 value_subtract(e, e, tmp);
392 value_division(tmp, c, d);
393 value_multiply(tmp, tmp, d);
394 value_subtract(c, c, tmp);
395 value_swap(c, d);
396 value_swap(e, f);
398 value_assign(*g, c);
399 if (value_zero_p(a))
400 value_set_si(*x, 0);
401 else if (value_pos_p(a))
402 value_assign(*x, e);
403 else value_oppose(*x, e);
404 if (value_zero_p(b))
405 value_set_si(*y, 0);
406 else {
407 value_multiply(tmp, a, *x);
408 value_subtract(tmp, c, tmp);
409 value_division(*y, tmp, b);
411 value_clear(c);
412 value_clear(d);
413 value_clear(e);
414 value_clear(f);
415 value_clear(tmp);
418 static int unimodular_complete_1(Matrix *m)
420 Value g, b, c, old, tmp;
421 unsigned i, j;
422 int ok;
424 value_init(b);
425 value_init(c);
426 value_init(g);
427 value_init(old);
428 value_init(tmp);
429 value_assign(g, m->p[0][0]);
430 for (i = 1; value_zero_p(g) && i < m->NbColumns; ++i) {
431 for (j = 0; j < m->NbColumns; ++j) {
432 if (j == i-1)
433 value_set_si(m->p[i][j], 1);
434 else
435 value_set_si(m->p[i][j], 0);
437 value_assign(g, m->p[0][i]);
439 for (; i < m->NbColumns; ++i) {
440 value_assign(old, g);
441 Extended_Euclid(old, m->p[0][i], &c, &b, &g);
442 value_oppose(b, b);
443 for (j = 0; j < m->NbColumns; ++j) {
444 if (j < i) {
445 value_multiply(tmp, m->p[0][j], b);
446 value_division(m->p[i][j], tmp, old);
447 } else if (j == i)
448 value_assign(m->p[i][j], c);
449 else
450 value_set_si(m->p[i][j], 0);
453 ok = value_one_p(g);
454 value_clear(b);
455 value_clear(c);
456 value_clear(g);
457 value_clear(old);
458 value_clear(tmp);
459 return ok;
462 int unimodular_complete(Matrix *M, int row)
464 int r;
465 int ok = 1;
466 Matrix *H, *Q, *U;
468 if (row == 1)
469 return unimodular_complete_1(M);
471 left_hermite(M, &H, &Q, &U);
472 Matrix_Free(U);
473 for (r = 0; ok && r < row; ++r)
474 if (value_notone_p(H->p[r][r]))
475 ok = 0;
476 Matrix_Free(H);
477 for (r = row; r < M->NbRows; ++r)
478 Vector_Copy(Q->p[r], M->p[r], M->NbColumns);
479 Matrix_Free(Q);
480 return ok;
484 * Returns a full-dimensional polyhedron with the same number
485 * of integer points as P
487 Polyhedron *remove_equalities(Polyhedron *P, unsigned MaxRays)
489 Polyhedron *Q = Polyhedron_Copy(P);
490 unsigned dim = P->Dimension;
491 Matrix *m1, *m2;
492 int i;
494 if (Q->NbEq == 0)
495 return Q;
497 Q = DomainConstraintSimplify(Q, MaxRays);
498 if (emptyQ2(Q))
499 return Q;
501 m1 = Matrix_Alloc(dim, dim);
502 for (i = 0; i < Q->NbEq; ++i)
503 Vector_Copy(Q->Constraint[i]+1, m1->p[i], dim);
505 /* m1 may not be unimodular, but we won't be throwing anything away */
506 unimodular_complete(m1, Q->NbEq);
508 m2 = Matrix_Alloc(dim+1-Q->NbEq, dim+1);
509 for (i = Q->NbEq; i < dim; ++i)
510 Vector_Copy(m1->p[i], m2->p[i-Q->NbEq], dim);
511 value_set_si(m2->p[dim-Q->NbEq][dim], 1);
512 Matrix_Free(m1);
514 P = Polyhedron_Image(Q, m2, MaxRays);
515 Matrix_Free(m2);
516 Polyhedron_Free(Q);
518 return P;
522 * Returns a full-dimensional polyhedron with the same number
523 * of integer points as P
524 * nvar specifies the number of variables
525 * The remaining dimensions are assumed to be parameters
526 * Destroys P
527 * factor is NbEq x (nparam+2) matrix, containing stride constraints
528 * on the parameters; column nparam is the constant;
529 * column nparam+1 is the stride
531 * if factor is NULL, only remove equalities that don't affect
532 * the number of points
534 Polyhedron *remove_equalities_p(Polyhedron *P, unsigned nvar, Matrix **factor,
535 unsigned MaxRays)
537 Value g;
538 Polyhedron *Q;
539 unsigned dim = P->Dimension;
540 Matrix *m1, *m2, *f;
541 int i, j;
543 if (P->NbEq == 0)
544 return P;
546 m1 = Matrix_Alloc(nvar, nvar);
547 P = DomainConstraintSimplify(P, MaxRays);
548 if (factor) {
549 f = Matrix_Alloc(P->NbEq, dim-nvar+2);
550 *factor = f;
552 value_init(g);
553 for (i = 0, j = 0; i < P->NbEq; ++i) {
554 if (First_Non_Zero(P->Constraint[i]+1, nvar) == -1)
555 continue;
557 Vector_Gcd(P->Constraint[i]+1, nvar, &g);
558 if (!factor && value_notone_p(g))
559 continue;
561 if (factor) {
562 Vector_Copy(P->Constraint[i]+1+nvar, f->p[j], dim-nvar+1);
563 value_assign(f->p[j][dim-nvar+1], g);
566 Vector_Copy(P->Constraint[i]+1, m1->p[j], nvar);
568 ++j;
570 value_clear(g);
572 unimodular_complete(m1, j);
574 m2 = Matrix_Alloc(dim+1-j, dim+1);
575 for (i = 0; i < nvar-j ; ++i)
576 Vector_Copy(m1->p[i+j], m2->p[i], nvar);
577 Matrix_Free(m1);
578 for (i = nvar-j; i <= dim-j; ++i)
579 value_set_si(m2->p[i][i+j], 1);
581 Q = Polyhedron_Image(P, m2, MaxRays);
582 Matrix_Free(m2);
583 Polyhedron_Free(P);
585 return Q;
588 void Line_Length(Polyhedron *P, Value *len)
590 Value tmp, pos, neg;
591 int p = 0, n = 0;
592 int i;
594 assert(P->Dimension == 1);
596 value_init(tmp);
597 value_init(pos);
598 value_init(neg);
600 for (i = 0; i < P->NbConstraints; ++i) {
601 value_oppose(tmp, P->Constraint[i][2]);
602 if (value_pos_p(P->Constraint[i][1])) {
603 mpz_cdiv_q(tmp, tmp, P->Constraint[i][1]);
604 if (!p || value_gt(tmp, pos))
605 value_assign(pos, tmp);
606 p = 1;
607 } else if (value_neg_p(P->Constraint[i][1])) {
608 mpz_fdiv_q(tmp, tmp, P->Constraint[i][1]);
609 if (!n || value_lt(tmp, neg))
610 value_assign(neg, tmp);
611 n = 1;
613 if (n && p) {
614 value_subtract(tmp, neg, pos);
615 value_increment(*len, tmp);
616 } else
617 value_set_si(*len, -1);
620 value_clear(tmp);
621 value_clear(pos);
622 value_clear(neg);
626 * Factors the polyhedron P into polyhedra Q_i such that
627 * the number of integer points in P is equal to the product
628 * of the number of integer points in the individual Q_i
630 * If no factors can be found, NULL is returned.
631 * Otherwise, a linked list of the factors is returned.
633 * If there are factors and if T is not NULL, then a matrix will be
634 * returned through T expressing the old variables in terms of the
635 * new variables as they appear in the sequence of factors.
637 * The algorithm works by first computing the Hermite normal form
638 * and then grouping columns linked by one or more constraints together,
639 * where a constraints "links" two or more columns if the constraint
640 * has nonzero coefficients in the columns.
642 Polyhedron* Polyhedron_Factor(Polyhedron *P, unsigned nparam, Matrix **T,
643 unsigned NbMaxRays)
645 int i, j, k;
646 Matrix *M, *H, *Q, *U;
647 int *pos; /* for each column: row position of pivot */
648 int *group; /* group to which a column belongs */
649 int *cnt; /* number of columns in the group */
650 int *rowgroup; /* group to which a constraint belongs */
651 int nvar = P->Dimension - nparam;
652 Polyhedron *F = NULL;
654 if (nvar <= 1)
655 return NULL;
657 NALLOC(pos, nvar);
658 NALLOC(group, nvar);
659 NALLOC(cnt, nvar);
660 NALLOC(rowgroup, P->NbConstraints);
662 M = Matrix_Alloc(P->NbConstraints, nvar);
663 for (i = 0; i < P->NbConstraints; ++i)
664 Vector_Copy(P->Constraint[i]+1, M->p[i], nvar);
665 left_hermite(M, &H, &Q, &U);
666 Matrix_Free(M);
667 Matrix_Free(Q);
669 for (i = 0; i < P->NbConstraints; ++i)
670 rowgroup[i] = -1;
671 for (i = 0, j = 0; i < H->NbColumns; ++i) {
672 for ( ; j < H->NbRows; ++j)
673 if (value_notzero_p(H->p[j][i]))
674 break;
675 assert (j < H->NbRows);
676 pos[i] = j;
678 for (i = 0; i < nvar; ++i) {
679 group[i] = i;
680 cnt[i] = 1;
682 for (i = 0; i < H->NbColumns && cnt[0] < nvar; ++i) {
683 if (rowgroup[pos[i]] == -1)
684 rowgroup[pos[i]] = i;
685 for (j = pos[i]+1; j < H->NbRows; ++j) {
686 if (value_zero_p(H->p[j][i]))
687 continue;
688 if (rowgroup[j] != -1)
689 continue;
690 rowgroup[j] = group[i];
691 for (k = i+1; k < H->NbColumns && j >= pos[k]; ++k) {
692 int g = group[k];
693 while (cnt[g] == 0)
694 g = group[g];
695 group[k] = g;
696 if (group[k] != group[i] && value_notzero_p(H->p[j][k])) {
697 assert(cnt[group[k]] != 0);
698 assert(cnt[group[i]] != 0);
699 if (group[i] < group[k]) {
700 cnt[group[i]] += cnt[group[k]];
701 cnt[group[k]] = 0;
702 group[k] = group[i];
703 } else {
704 cnt[group[k]] += cnt[group[i]];
705 cnt[group[i]] = 0;
706 group[i] = group[k];
713 if (cnt[0] != nvar) {
714 /* Extract out pure context constraints separately */
715 Polyhedron **next = &F;
716 int tot_d = 0;
717 if (T)
718 *T = Matrix_Alloc(nvar, nvar);
719 for (i = nparam ? -1 : 0; i < nvar; ++i) {
720 int d;
722 if (i == -1) {
723 for (j = 0, k = 0; j < P->NbConstraints; ++j)
724 if (rowgroup[j] == -1) {
725 if (First_Non_Zero(P->Constraint[j]+1+nvar,
726 nparam) == -1)
727 rowgroup[j] = -2;
728 else
729 ++k;
731 if (k == 0)
732 continue;
733 d = 0;
734 } else {
735 if (cnt[i] == 0)
736 continue;
737 d = cnt[i];
738 for (j = 0, k = 0; j < P->NbConstraints; ++j)
739 if (rowgroup[j] >= 0 && group[rowgroup[j]] == i) {
740 rowgroup[j] = i;
741 ++k;
745 if (T)
746 for (j = 0; j < nvar; ++j) {
747 int l, m;
748 for (l = 0, m = 0; m < d; ++l) {
749 if (group[l] != i)
750 continue;
751 value_assign((*T)->p[j][tot_d+m++], U->p[j][l]);
755 M = Matrix_Alloc(k, d+nparam+2);
756 for (j = 0, k = 0; j < P->NbConstraints; ++j) {
757 int l, m;
758 if (rowgroup[j] != i)
759 continue;
760 value_assign(M->p[k][0], P->Constraint[j][0]);
761 for (l = 0, m = 0; m < d; ++l) {
762 if (group[l] != i)
763 continue;
764 value_assign(M->p[k][1+m++], H->p[j][l]);
766 Vector_Copy(P->Constraint[j]+1+nvar, M->p[k]+1+m, nparam+1);
767 ++k;
769 *next = Constraints2Polyhedron(M, NbMaxRays);
770 next = &(*next)->next;
771 Matrix_Free(M);
772 tot_d += d;
775 Matrix_Free(U);
776 Matrix_Free(H);
777 free(pos);
778 free(group);
779 free(cnt);
780 free(rowgroup);
781 return F;
785 * Project on final dim dimensions
787 Polyhedron* Polyhedron_Project(Polyhedron *P, int dim)
789 int i;
790 int remove = P->Dimension - dim;
791 Matrix *T;
792 Polyhedron *I;
794 if (P->Dimension == dim)
795 return Polyhedron_Copy(P);
797 T = Matrix_Alloc(dim+1, P->Dimension+1);
798 for (i = 0; i < dim+1; ++i)
799 value_set_si(T->p[i][i+remove], 1);
800 I = Polyhedron_Image(P, T, P->NbConstraints);
801 Matrix_Free(T);
802 return I;
805 /* Constructs a new constraint that ensures that
806 * the first constraint is (strictly) smaller than
807 * the second.
809 static void smaller_constraint(Value *a, Value *b, Value *c, int pos, int shift,
810 int len, int strict, Value *tmp)
812 value_oppose(*tmp, b[pos+1]);
813 value_set_si(c[0], 1);
814 Vector_Combine(a+1+shift, b+1+shift, c+1, *tmp, a[pos+1], len-shift-1);
815 if (strict)
816 value_decrement(c[len-shift-1], c[len-shift-1]);
817 ConstraintSimplify(c, c, len-shift, tmp);
821 /* For each pair of lower and upper bounds on the first variable,
822 * calls fn with the set of constraints on the remaining variables
823 * where these bounds are active, i.e., (stricly) larger/smaller than
824 * the other lower/upper bounds, the lower and upper bound and the
825 * call back data.
827 * If the first variable is equal to an affine combination of the
828 * other variables then fn is called with both lower and upper
829 * pointing to the corresponding equality.
831 void for_each_lower_upper_bound(Polyhedron *P, for_each_lower_upper_bound_fn fn,
832 void *cb_data)
834 unsigned dim = P->Dimension;
835 Matrix *M;
836 int *pos;
837 int i, j, p, n, z;
838 int k, l, k2, l2, q;
839 Value g;
841 if (value_zero_p(P->Constraint[0][0]) &&
842 value_notzero_p(P->Constraint[0][1])) {
843 M = Matrix_Alloc(P->NbConstraints-1, dim-1+2);
844 for (i = 1; i < P->NbConstraints; ++i) {
845 value_assign(M->p[i-1][0], P->Constraint[i][0]);
846 Vector_Copy(P->Constraint[i]+2, M->p[i-1]+1, dim);
848 fn(M, P->Constraint[0], P->Constraint[0], cb_data);
849 Matrix_Free(M);
850 return;
853 value_init(g);
854 pos = ALLOCN(int, P->NbConstraints);
856 for (i = 0, z = 0; i < P->NbConstraints; ++i)
857 if (value_zero_p(P->Constraint[i][1]))
858 pos[P->NbConstraints-1 - z++] = i;
859 /* put those with positive coefficients first; number: p */
860 for (i = 0, p = 0, n = P->NbConstraints-z-1; i < P->NbConstraints; ++i)
861 if (value_pos_p(P->Constraint[i][1]))
862 pos[p++] = i;
863 else if (value_neg_p(P->Constraint[i][1]))
864 pos[n--] = i;
865 n = P->NbConstraints-z-p;
866 assert (p >= 1 && n >= 1);
868 M = Matrix_Alloc((p-1) + (n-1) + z + 1, dim-1+2);
869 for (i = 0; i < z; ++i) {
870 value_assign(M->p[i][0], P->Constraint[pos[P->NbConstraints-1 - i]][0]);
871 Vector_Copy(P->Constraint[pos[P->NbConstraints-1 - i]]+2,
872 M->p[i]+1, dim);
874 for (k = 0; k < p; ++k) {
875 for (k2 = 0; k2 < p; ++k2) {
876 if (k2 == k)
877 continue;
878 q = 1 + z + k2 - (k2 > k);
879 smaller_constraint(
880 P->Constraint[pos[k]],
881 P->Constraint[pos[k2]],
882 M->p[q], 0, 1, dim+2, k2 > k, &g);
884 for (l = p; l < p+n; ++l) {
885 for (l2 = p; l2 < p+n; ++l2) {
886 if (l2 == l)
887 continue;
888 q = 1 + z + l2-1 - (l2 > l);
889 smaller_constraint(
890 P->Constraint[pos[l2]],
891 P->Constraint[pos[l]],
892 M->p[q], 0, 1, dim+2, l2 > l, &g);
894 smaller_constraint(P->Constraint[pos[k]],
895 P->Constraint[pos[l]],
896 M->p[z], 0, 1, dim+2, 0, &g);
897 fn(M, P->Constraint[pos[k]], P->Constraint[pos[l]], cb_data);
900 Matrix_Free(M);
902 free(pos);
903 value_clear(g);
906 struct section { Polyhedron * D; evalue E; };
908 struct PLL_data {
909 int nd;
910 unsigned MaxRays;
911 Polyhedron *C;
912 evalue mone;
913 struct section *s;
916 static void PLL_cb(Matrix *M, Value *lower, Value *upper, void *cb_data)
918 struct PLL_data *data = (struct PLL_data *)cb_data;
919 unsigned dim = M->NbColumns-1;
920 Matrix *M2;
921 Polyhedron *T;
922 evalue *L, *U;
924 M2 = Matrix_Copy(M);
925 T = Constraints2Polyhedron(M2, data->MaxRays);
926 Matrix_Free(M2);
927 data->s[data->nd].D = DomainIntersection(T, data->C, data->MaxRays);
928 Domain_Free(T);
930 POL_ENSURE_VERTICES(data->s[data->nd].D);
931 if (emptyQ(data->s[data->nd].D)) {
932 Polyhedron_Free(data->s[data->nd].D);
933 return;
935 L = bv_ceil3(lower+1+1, dim-1+1, lower[0+1], data->s[data->nd].D);
936 U = bv_ceil3(upper+1+1, dim-1+1, upper[0+1], data->s[data->nd].D);
937 eadd(L, U);
938 eadd(&data->mone, U);
939 emul(&data->mone, U);
940 data->s[data->nd].E = *U;
941 evalue_free(L);
942 free(U);
943 ++data->nd;
946 static evalue *ParamLine_Length_mod(Polyhedron *P, Polyhedron *C, unsigned MaxRays)
948 unsigned dim = P->Dimension;
949 unsigned nvar = dim - C->Dimension;
950 int ssize = (P->NbConstraints+1) * (P->NbConstraints+1) / 4;
951 struct PLL_data data;
952 evalue *F;
953 int k;
955 assert(nvar == 1);
957 value_init(data.mone.d);
958 evalue_set_si(&data.mone, -1, 1);
960 data.s = ALLOCN(struct section, ssize);
961 data.nd = 0;
962 data.MaxRays = MaxRays;
963 data.C = C;
964 for_each_lower_upper_bound(P, PLL_cb, &data);
966 F = ALLOC(evalue);
967 value_init(F->d);
968 value_set_si(F->d, 0);
969 F->x.p = new_enode(partition, 2*data.nd, dim-nvar);
970 for (k = 0; k < data.nd; ++k) {
971 EVALUE_SET_DOMAIN(F->x.p->arr[2*k], data.s[k].D);
972 value_clear(F->x.p->arr[2*k+1].d);
973 F->x.p->arr[2*k+1] = data.s[k].E;
975 free(data.s);
977 free_evalue_refs(&data.mone);
979 return F;
982 evalue* ParamLine_Length(Polyhedron *P, Polyhedron *C,
983 struct barvinok_options *options)
985 evalue* tmp;
986 tmp = ParamLine_Length_mod(P, C, options->MaxRays);
987 if (options->lookup_table) {
988 evalue_mod2table(tmp, C->Dimension);
989 reduce_evalue(tmp);
991 return tmp;
994 Bool isIdentity(Matrix *M)
996 unsigned i, j;
997 if (M->NbRows != M->NbColumns)
998 return False;
1000 for (i = 0;i < M->NbRows; i ++)
1001 for (j = 0; j < M->NbColumns; j ++)
1002 if (i == j) {
1003 if(value_notone_p(M->p[i][j]))
1004 return False;
1005 } else {
1006 if(value_notzero_p(M->p[i][j]))
1007 return False;
1009 return True;
1012 void Param_Polyhedron_Print(FILE* DST, Param_Polyhedron *PP, char **param_names)
1014 Param_Domain *P;
1015 Param_Vertices *V;
1017 for(P=PP->D;P;P=P->next) {
1019 /* prints current val. dom. */
1020 fprintf(DST, "---------------------------------------\n");
1021 fprintf(DST, "Domain :\n");
1022 Print_Domain(DST, P->Domain, param_names);
1024 /* scan the vertices */
1025 fprintf(DST, "Vertices :\n");
1026 FORALL_PVertex_in_ParamPolyhedron(V,P,PP) {
1028 /* prints each vertex */
1029 Print_Vertex(DST, V->Vertex, param_names);
1030 fprintf(DST, "\n");
1032 END_FORALL_PVertex_in_ParamPolyhedron;
1036 void Enumeration_Print(FILE *Dst, Enumeration *en, const char * const *params)
1038 for (; en; en = en->next) {
1039 Print_Domain(Dst, en->ValidityDomain, params);
1040 print_evalue(Dst, &en->EP, params);
1044 void Enumeration_Free(Enumeration *en)
1046 Enumeration *ee;
1048 while( en )
1050 free_evalue_refs( &(en->EP) );
1051 Domain_Free( en->ValidityDomain );
1052 ee = en ->next;
1053 free( en );
1054 en = ee;
1058 void Enumeration_mod2table(Enumeration *en, unsigned nparam)
1060 for (; en; en = en->next) {
1061 evalue_mod2table(&en->EP, nparam);
1062 reduce_evalue(&en->EP);
1066 size_t Enumeration_size(Enumeration *en)
1068 size_t s = 0;
1070 for (; en; en = en->next) {
1071 s += domain_size(en->ValidityDomain);
1072 s += evalue_size(&en->EP);
1074 return s;
1077 void Free_ParamNames(char **params, int m)
1079 while (--m >= 0)
1080 free(params[m]);
1081 free(params);
1084 /* Check whether every set in D2 is included in some set of D1 */
1085 int DomainIncludes(Polyhedron *D1, Polyhedron *D2)
1087 for ( ; D2; D2 = D2->next) {
1088 Polyhedron *P1;
1089 for (P1 = D1; P1; P1 = P1->next)
1090 if (PolyhedronIncludes(P1, D2))
1091 break;
1092 if (!P1)
1093 return 0;
1095 return 1;
1098 int line_minmax(Polyhedron *I, Value *min, Value *max)
1100 int i;
1102 if (I->NbEq >= 1) {
1103 value_oppose(I->Constraint[0][2], I->Constraint[0][2]);
1104 /* There should never be a remainder here */
1105 if (value_pos_p(I->Constraint[0][1]))
1106 mpz_fdiv_q(*min, I->Constraint[0][2], I->Constraint[0][1]);
1107 else
1108 mpz_fdiv_q(*min, I->Constraint[0][2], I->Constraint[0][1]);
1109 value_assign(*max, *min);
1110 } else for (i = 0; i < I->NbConstraints; ++i) {
1111 if (value_zero_p(I->Constraint[i][1])) {
1112 Polyhedron_Free(I);
1113 return 0;
1116 value_oppose(I->Constraint[i][2], I->Constraint[i][2]);
1117 if (value_pos_p(I->Constraint[i][1]))
1118 mpz_cdiv_q(*min, I->Constraint[i][2], I->Constraint[i][1]);
1119 else
1120 mpz_fdiv_q(*max, I->Constraint[i][2], I->Constraint[i][1]);
1122 Polyhedron_Free(I);
1123 return 1;
1126 /**
1128 PROCEDURES TO COMPUTE ENUMERATION. recursive procedure, recurse for
1129 each imbriquation
1131 @param pos index position of current loop index (1..hdim-1)
1132 @param P loop domain
1133 @param context context values for fixed indices
1134 @param exist number of existential variables
1135 @return the number of integer points in this
1136 polyhedron
1139 void count_points_e (int pos, Polyhedron *P, int exist, int nparam,
1140 Value *context, Value *res)
1142 Value LB, UB, k, c;
1144 if (emptyQ(P)) {
1145 value_set_si(*res, 0);
1146 return;
1149 value_init(LB); value_init(UB); value_init(k);
1150 value_set_si(LB,0);
1151 value_set_si(UB,0);
1153 if (lower_upper_bounds(pos,P,context,&LB,&UB) !=0) {
1154 /* Problem if UB or LB is INFINITY */
1155 value_clear(LB); value_clear(UB); value_clear(k);
1156 if (pos > P->Dimension - nparam - exist)
1157 value_set_si(*res, 1);
1158 else
1159 value_set_si(*res, -1);
1160 return;
1163 #ifdef EDEBUG1
1164 if (!P->next) {
1165 int i;
1166 for (value_assign(k,LB); value_le(k,UB); value_increment(k,k)) {
1167 fprintf(stderr, "(");
1168 for (i=1; i<pos; i++) {
1169 value_print(stderr,P_VALUE_FMT,context[i]);
1170 fprintf(stderr,",");
1172 value_print(stderr,P_VALUE_FMT,k);
1173 fprintf(stderr,")\n");
1176 #endif
1178 value_set_si(context[pos],0);
1179 if (value_lt(UB,LB)) {
1180 value_clear(LB); value_clear(UB); value_clear(k);
1181 value_set_si(*res, 0);
1182 return;
1184 if (!P->next) {
1185 if (exist)
1186 value_set_si(*res, 1);
1187 else {
1188 value_subtract(k,UB,LB);
1189 value_add_int(k,k,1);
1190 value_assign(*res, k);
1192 value_clear(LB); value_clear(UB); value_clear(k);
1193 return;
1196 /*-----------------------------------------------------------------*/
1197 /* Optimization idea */
1198 /* If inner loops are not a function of k (the current index) */
1199 /* i.e. if P->Constraint[i][pos]==0 for all P following this and */
1200 /* for all i, */
1201 /* Then CNT = (UB-LB+1)*count_points(pos+1, P->next, context) */
1202 /* (skip the for loop) */
1203 /*-----------------------------------------------------------------*/
1205 value_init(c);
1206 value_set_si(*res, 0);
1207 for (value_assign(k,LB);value_le(k,UB);value_increment(k,k)) {
1208 /* Insert k in context */
1209 value_assign(context[pos],k);
1210 count_points_e(pos+1, P->next, exist, nparam, context, &c);
1211 if(value_notmone_p(c))
1212 value_addto(*res, *res, c);
1213 else {
1214 value_set_si(*res, -1);
1215 break;
1217 if (pos > P->Dimension - nparam - exist &&
1218 value_pos_p(*res))
1219 break;
1221 value_clear(c);
1223 #ifdef EDEBUG11
1224 fprintf(stderr,"%d\n",CNT);
1225 #endif
1227 /* Reset context */
1228 value_set_si(context[pos],0);
1229 value_clear(LB); value_clear(UB); value_clear(k);
1230 return;
1231 } /* count_points_e */
1233 int DomainContains(Polyhedron *P, Value *list_args, int len,
1234 unsigned MaxRays, int set)
1236 int i;
1237 Value m;
1239 if (P->Dimension == len)
1240 return in_domain(P, list_args);
1242 assert(set); // assume list_args is large enough
1243 assert((P->Dimension - len) % 2 == 0);
1244 value_init(m);
1245 for (i = 0; i < P->Dimension - len; i += 2) {
1246 int j, k;
1247 for (j = 0 ; j < P->NbEq; ++j)
1248 if (value_notzero_p(P->Constraint[j][1+len+i]))
1249 break;
1250 assert(j < P->NbEq);
1251 value_absolute(m, P->Constraint[j][1+len+i]);
1252 k = First_Non_Zero(P->Constraint[j]+1, len);
1253 assert(k != -1);
1254 assert(First_Non_Zero(P->Constraint[j]+1+k+1, len - k - 1) == -1);
1255 mpz_fdiv_q(list_args[len+i], list_args[k], m);
1256 mpz_fdiv_r(list_args[len+i+1], list_args[k], m);
1258 value_clear(m);
1260 return in_domain(P, list_args);
1263 Polyhedron *DomainConcat(Polyhedron *head, Polyhedron *tail)
1265 Polyhedron *S;
1266 if (!head)
1267 return tail;
1268 for (S = head; S->next; S = S->next)
1270 S->next = tail;
1271 return head;
1274 evalue *barvinok_lexsmaller_ev(Polyhedron *P, Polyhedron *D, unsigned dim,
1275 Polyhedron *C, unsigned MaxRays)
1277 evalue *ranking;
1278 Polyhedron *RC, *RD, *Q;
1279 unsigned nparam = dim + C->Dimension;
1280 unsigned exist;
1281 Polyhedron *CA;
1283 RC = LexSmaller(P, D, dim, C, MaxRays);
1284 RD = RC->next;
1285 RC->next = NULL;
1287 exist = RD->Dimension - nparam - dim;
1288 CA = align_context(RC, RD->Dimension, MaxRays);
1289 Q = DomainIntersection(RD, CA, MaxRays);
1290 Polyhedron_Free(CA);
1291 Domain_Free(RD);
1292 Polyhedron_Free(RC);
1293 RD = Q;
1295 for (Q = RD; Q; Q = Q->next) {
1296 evalue *t;
1297 Polyhedron *next = Q->next;
1298 Q->next = 0;
1300 t = barvinok_enumerate_e(Q, exist, nparam, MaxRays);
1302 if (Q == RD)
1303 ranking = t;
1304 else {
1305 eadd(t, ranking);
1306 evalue_free(t);
1309 Q->next = next;
1312 Domain_Free(RD);
1314 return ranking;
1317 Enumeration *barvinok_lexsmaller(Polyhedron *P, Polyhedron *D, unsigned dim,
1318 Polyhedron *C, unsigned MaxRays)
1320 evalue *EP = barvinok_lexsmaller_ev(P, D, dim, C, MaxRays);
1322 return partition2enumeration(EP);
1325 /* "align" matrix to have nrows by inserting
1326 * the necessary number of rows and an equal number of columns in front
1328 Matrix *align_matrix(Matrix *M, int nrows)
1330 int i;
1331 int newrows = nrows - M->NbRows;
1332 Matrix *M2 = Matrix_Alloc(nrows, newrows + M->NbColumns);
1333 for (i = 0; i < newrows; ++i)
1334 value_set_si(M2->p[i][i], 1);
1335 for (i = 0; i < M->NbRows; ++i)
1336 Vector_Copy(M->p[i], M2->p[newrows+i]+newrows, M->NbColumns);
1337 return M2;
1340 static void print_varlist(FILE *out, int n, char **names)
1342 int i;
1343 fprintf(out, "[");
1344 for (i = 0; i < n; ++i) {
1345 if (i)
1346 fprintf(out, ",");
1347 fprintf(out, "%s", names[i]);
1349 fprintf(out, "]");
1352 static void print_term(FILE *out, Value v, int pos, int dim, int nparam,
1353 char **iter_names, char **param_names, int *first)
1355 if (value_zero_p(v)) {
1356 if (first && *first && pos >= dim + nparam)
1357 fprintf(out, "0");
1358 return;
1361 if (first) {
1362 if (!*first && value_pos_p(v))
1363 fprintf(out, "+");
1364 *first = 0;
1366 if (pos < dim + nparam) {
1367 if (value_mone_p(v))
1368 fprintf(out, "-");
1369 else if (!value_one_p(v))
1370 value_print(out, VALUE_FMT, v);
1371 if (pos < dim)
1372 fprintf(out, "%s", iter_names[pos]);
1373 else
1374 fprintf(out, "%s", param_names[pos-dim]);
1375 } else
1376 value_print(out, VALUE_FMT, v);
1379 char **util_generate_names(int n, const char *prefix)
1381 int i;
1382 int len = (prefix ? strlen(prefix) : 0) + 10;
1383 char **names = ALLOCN(char*, n);
1384 if (!names) {
1385 fprintf(stderr, "ERROR: memory overflow.\n");
1386 exit(1);
1388 for (i = 0; i < n; ++i) {
1389 names[i] = ALLOCN(char, len);
1390 if (!names[i]) {
1391 fprintf(stderr, "ERROR: memory overflow.\n");
1392 exit(1);
1394 if (!prefix)
1395 snprintf(names[i], len, "%d", i);
1396 else
1397 snprintf(names[i], len, "%s%d", prefix, i);
1400 return names;
1403 void util_free_names(int n, char **names)
1405 int i;
1406 for (i = 0; i < n; ++i)
1407 free(names[i]);
1408 free(names);
1411 void Polyhedron_pprint(FILE *out, Polyhedron *P, int dim, int nparam,
1412 char **iter_names, char **param_names)
1414 int i, j;
1415 Value tmp;
1417 assert(dim + nparam == P->Dimension);
1419 value_init(tmp);
1421 fprintf(out, "{ ");
1422 if (nparam) {
1423 print_varlist(out, nparam, param_names);
1424 fprintf(out, " -> ");
1426 print_varlist(out, dim, iter_names);
1427 fprintf(out, " : ");
1429 if (emptyQ2(P))
1430 fprintf(out, "FALSE");
1431 else for (i = 0; i < P->NbConstraints; ++i) {
1432 int first = 1;
1433 int v = First_Non_Zero(P->Constraint[i]+1, P->Dimension);
1434 if (v == -1 && value_pos_p(P->Constraint[i][0]))
1435 continue;
1436 if (i)
1437 fprintf(out, " && ");
1438 if (v == -1 && value_notzero_p(P->Constraint[i][1+P->Dimension]))
1439 fprintf(out, "FALSE");
1440 else if (value_pos_p(P->Constraint[i][v+1])) {
1441 print_term(out, P->Constraint[i][v+1], v, dim, nparam,
1442 iter_names, param_names, NULL);
1443 if (value_zero_p(P->Constraint[i][0]))
1444 fprintf(out, " = ");
1445 else
1446 fprintf(out, " >= ");
1447 for (j = v+1; j <= dim+nparam; ++j) {
1448 value_oppose(tmp, P->Constraint[i][1+j]);
1449 print_term(out, tmp, j, dim, nparam,
1450 iter_names, param_names, &first);
1452 } else {
1453 value_oppose(tmp, P->Constraint[i][1+v]);
1454 print_term(out, tmp, v, dim, nparam,
1455 iter_names, param_names, NULL);
1456 fprintf(out, " <= ");
1457 for (j = v+1; j <= dim+nparam; ++j)
1458 print_term(out, P->Constraint[i][1+j], j, dim, nparam,
1459 iter_names, param_names, &first);
1463 fprintf(out, " }\n");
1465 value_clear(tmp);
1468 /* Construct a cone over P with P placed at x_d = 1, with
1469 * x_d the coordinate of an extra dimension
1471 * It's probably a mistake to depend so much on the internal
1472 * representation. We should probably simply compute the
1473 * vertices/facets first.
1475 Polyhedron *Cone_over_Polyhedron(Polyhedron *P)
1477 unsigned NbConstraints = 0;
1478 unsigned NbRays = 0;
1479 Polyhedron *C;
1480 int i;
1482 if (POL_HAS(P, POL_INEQUALITIES))
1483 NbConstraints = P->NbConstraints + 1;
1484 if (POL_HAS(P, POL_POINTS))
1485 NbRays = P->NbRays + 1;
1487 C = Polyhedron_Alloc(P->Dimension+1, NbConstraints, NbRays);
1488 if (POL_HAS(P, POL_INEQUALITIES)) {
1489 C->NbEq = P->NbEq;
1490 for (i = 0; i < P->NbConstraints; ++i)
1491 Vector_Copy(P->Constraint[i], C->Constraint[i], P->Dimension+2);
1492 /* n >= 0 */
1493 value_set_si(C->Constraint[P->NbConstraints][0], 1);
1494 value_set_si(C->Constraint[P->NbConstraints][1+P->Dimension], 1);
1496 if (POL_HAS(P, POL_POINTS)) {
1497 C->NbBid = P->NbBid;
1498 for (i = 0; i < P->NbRays; ++i)
1499 Vector_Copy(P->Ray[i], C->Ray[i], P->Dimension+2);
1500 /* vertex 0 */
1501 value_set_si(C->Ray[P->NbRays][0], 1);
1502 value_set_si(C->Ray[P->NbRays][1+C->Dimension], 1);
1504 POL_SET(C, POL_VALID);
1505 if (POL_HAS(P, POL_INEQUALITIES))
1506 POL_SET(C, POL_INEQUALITIES);
1507 if (POL_HAS(P, POL_POINTS))
1508 POL_SET(C, POL_POINTS);
1509 if (POL_HAS(P, POL_VERTICES))
1510 POL_SET(C, POL_VERTICES);
1511 return C;
1514 /* Returns a (dim+nparam+1)x((dim-n)+nparam+1) matrix
1515 * mapping the transformed subspace back to the original space.
1516 * n is the number of equalities involving the variables
1517 * (i.e., not purely the parameters).
1518 * The remaining n coordinates in the transformed space would
1519 * have constant (parametric) values and are therefore not
1520 * included in the variables of the new space.
1522 Matrix *compress_variables(Matrix *Equalities, unsigned nparam)
1524 unsigned dim = (Equalities->NbColumns-2) - nparam;
1525 Matrix *M, *H, *Q, *U, *C, *ratH, *invH, *Ul, *T1, *T2, *T;
1526 Value mone;
1527 int n, i, j;
1528 int ok;
1530 for (n = 0; n < Equalities->NbRows; ++n)
1531 if (First_Non_Zero(Equalities->p[n]+1, dim) == -1)
1532 break;
1533 if (n == 0)
1534 return Identity(dim+nparam+1);
1535 value_init(mone);
1536 value_set_si(mone, -1);
1537 M = Matrix_Alloc(n, dim);
1538 C = Matrix_Alloc(n+1, nparam+1);
1539 for (i = 0; i < n; ++i) {
1540 Vector_Copy(Equalities->p[i]+1, M->p[i], dim);
1541 Vector_Scale(Equalities->p[i]+1+dim, C->p[i], mone, nparam+1);
1543 value_set_si(C->p[n][nparam], 1);
1544 left_hermite(M, &H, &Q, &U);
1545 Matrix_Free(M);
1546 Matrix_Free(Q);
1547 value_clear(mone);
1549 ratH = Matrix_Alloc(n+1, n+1);
1550 invH = Matrix_Alloc(n+1, n+1);
1551 for (i = 0; i < n; ++i)
1552 Vector_Copy(H->p[i], ratH->p[i], n);
1553 value_set_si(ratH->p[n][n], 1);
1554 ok = Matrix_Inverse(ratH, invH);
1555 assert(ok);
1556 Matrix_Free(H);
1557 Matrix_Free(ratH);
1558 T1 = Matrix_Alloc(n+1, nparam+1);
1559 Matrix_Product(invH, C, T1);
1560 Matrix_Free(C);
1561 Matrix_Free(invH);
1562 if (value_notone_p(T1->p[n][nparam])) {
1563 for (i = 0; i < n; ++i) {
1564 if (!mpz_divisible_p(T1->p[i][nparam], T1->p[n][nparam])) {
1565 Matrix_Free(T1);
1566 Matrix_Free(U);
1567 return NULL;
1569 /* compress_params should have taken care of this */
1570 for (j = 0; j < nparam; ++j)
1571 assert(mpz_divisible_p(T1->p[i][j], T1->p[n][nparam]));
1572 Vector_AntiScale(T1->p[i], T1->p[i], T1->p[n][nparam], nparam+1);
1574 value_set_si(T1->p[n][nparam], 1);
1576 Ul = Matrix_Alloc(dim+1, n+1);
1577 for (i = 0; i < dim; ++i)
1578 Vector_Copy(U->p[i], Ul->p[i], n);
1579 value_set_si(Ul->p[dim][n], 1);
1580 T2 = Matrix_Alloc(dim+1, nparam+1);
1581 Matrix_Product(Ul, T1, T2);
1582 Matrix_Free(Ul);
1583 Matrix_Free(T1);
1585 T = Matrix_Alloc(dim+nparam+1, (dim-n)+nparam+1);
1586 for (i = 0; i < dim; ++i) {
1587 Vector_Copy(U->p[i]+n, T->p[i], dim-n);
1588 Vector_Copy(T2->p[i], T->p[i]+dim-n, nparam+1);
1590 for (i = 0; i < nparam+1; ++i)
1591 value_set_si(T->p[dim+i][(dim-n)+i], 1);
1592 assert(value_one_p(T2->p[dim][nparam]));
1593 Matrix_Free(U);
1594 Matrix_Free(T2);
1596 return T;
1599 /* Computes the left inverse of an affine embedding M and, if Eq is not NULL,
1600 * the equalities that define the affine subspace onto which M maps
1601 * its argument.
1603 Matrix *left_inverse(Matrix *M, Matrix **Eq)
1605 int i, ok;
1606 Matrix *L, *H, *Q, *U, *ratH, *invH, *Ut, *inv;
1607 Vector *t;
1609 if (M->NbColumns == 1) {
1610 inv = Matrix_Alloc(1, M->NbRows);
1611 value_set_si(inv->p[0][M->NbRows-1], 1);
1612 if (Eq) {
1613 *Eq = Matrix_Alloc(M->NbRows-1, 1+(M->NbRows-1)+1);
1614 for (i = 0; i < M->NbRows-1; ++i) {
1615 value_oppose((*Eq)->p[i][1+i], M->p[M->NbRows-1][0]);
1616 value_assign((*Eq)->p[i][1+(M->NbRows-1)], M->p[i][0]);
1619 return inv;
1621 if (Eq)
1622 *Eq = NULL;
1623 L = Matrix_Alloc(M->NbRows-1, M->NbColumns-1);
1624 for (i = 0; i < L->NbRows; ++i)
1625 Vector_Copy(M->p[i], L->p[i], L->NbColumns);
1626 right_hermite(L, &H, &U, &Q);
1627 Matrix_Free(L);
1628 Matrix_Free(Q);
1629 t = Vector_Alloc(U->NbColumns);
1630 for (i = 0; i < U->NbColumns; ++i)
1631 value_oppose(t->p[i], M->p[i][M->NbColumns-1]);
1632 if (Eq) {
1633 *Eq = Matrix_Alloc(H->NbRows - H->NbColumns, 2 + U->NbColumns);
1634 for (i = 0; i < H->NbRows - H->NbColumns; ++i) {
1635 Vector_Copy(U->p[H->NbColumns+i], (*Eq)->p[i]+1, U->NbColumns);
1636 Inner_Product(U->p[H->NbColumns+i], t->p, U->NbColumns,
1637 (*Eq)->p[i]+1+U->NbColumns);
1640 ratH = Matrix_Alloc(H->NbColumns+1, H->NbColumns+1);
1641 invH = Matrix_Alloc(H->NbColumns+1, H->NbColumns+1);
1642 for (i = 0; i < H->NbColumns; ++i)
1643 Vector_Copy(H->p[i], ratH->p[i], H->NbColumns);
1644 value_set_si(ratH->p[ratH->NbRows-1][ratH->NbColumns-1], 1);
1645 Matrix_Free(H);
1646 ok = Matrix_Inverse(ratH, invH);
1647 assert(ok);
1648 Matrix_Free(ratH);
1649 Ut = Matrix_Alloc(invH->NbRows, U->NbColumns+1);
1650 for (i = 0; i < Ut->NbRows-1; ++i) {
1651 Vector_Copy(U->p[i], Ut->p[i], U->NbColumns);
1652 Inner_Product(U->p[i], t->p, U->NbColumns, &Ut->p[i][Ut->NbColumns-1]);
1654 Matrix_Free(U);
1655 Vector_Free(t);
1656 value_set_si(Ut->p[Ut->NbRows-1][Ut->NbColumns-1], 1);
1657 inv = Matrix_Alloc(invH->NbRows, Ut->NbColumns);
1658 Matrix_Product(invH, Ut, inv);
1659 Matrix_Free(Ut);
1660 Matrix_Free(invH);
1661 return inv;
1664 /* Check whether all rays are revlex positive in the parameters
1666 int Polyhedron_has_revlex_positive_rays(Polyhedron *P, unsigned nparam)
1668 int r;
1669 for (r = 0; r < P->NbRays; ++r) {
1670 int i;
1671 if (value_notzero_p(P->Ray[r][P->Dimension+1]))
1672 continue;
1673 for (i = P->Dimension-1; i >= P->Dimension-nparam; --i) {
1674 if (value_neg_p(P->Ray[r][i+1]))
1675 return 0;
1676 if (value_pos_p(P->Ray[r][i+1]))
1677 break;
1679 /* A ray independent of the parameters */
1680 if (i < P->Dimension-nparam)
1681 return 0;
1683 return 1;
1686 static Polyhedron *Recession_Cone(Polyhedron *P, unsigned nparam, unsigned MaxRays)
1688 int i;
1689 unsigned nvar = P->Dimension - nparam;
1690 Matrix *M = Matrix_Alloc(P->NbConstraints, 1 + nvar + 1);
1691 Polyhedron *R;
1692 for (i = 0; i < P->NbConstraints; ++i)
1693 Vector_Copy(P->Constraint[i], M->p[i], 1+nvar);
1694 R = Constraints2Polyhedron(M, MaxRays);
1695 Matrix_Free(M);
1696 return R;
1699 int Polyhedron_is_unbounded(Polyhedron *P, unsigned nparam, unsigned MaxRays)
1701 int i;
1702 int is_unbounded;
1703 Polyhedron *R = Recession_Cone(P, nparam, MaxRays);
1704 POL_ENSURE_VERTICES(R);
1705 if (R->NbBid == 0)
1706 for (i = 0; i < R->NbRays; ++i)
1707 if (value_zero_p(R->Ray[i][1+R->Dimension]))
1708 break;
1709 is_unbounded = R->NbBid > 0 || i < R->NbRays;
1710 Polyhedron_Free(R);
1711 return is_unbounded;
1714 static void SwapColumns(Value **V, int n, int i, int j)
1716 int r;
1718 for (r = 0; r < n; ++r)
1719 value_swap(V[r][i], V[r][j]);
1722 void Polyhedron_ExchangeColumns(Polyhedron *P, int Column1, int Column2)
1724 SwapColumns(P->Constraint, P->NbConstraints, Column1, Column2);
1725 SwapColumns(P->Ray, P->NbRays, Column1, Column2);
1726 if (P->NbEq) {
1727 Matrix M;
1728 Polyhedron_Matrix_View(P, &M, P->NbConstraints);
1729 Gauss(&M, P->NbEq, P->Dimension+1);
1733 /* perform transposition inline; assumes M is a square matrix */
1734 void Matrix_Transposition(Matrix *M)
1736 int i, j;
1738 assert(M->NbRows == M->NbColumns);
1739 for (i = 0; i < M->NbRows; ++i)
1740 for (j = i+1; j < M->NbColumns; ++j)
1741 value_swap(M->p[i][j], M->p[j][i]);
1744 /* Matrix "view" of first rows rows */
1745 void Polyhedron_Matrix_View(Polyhedron *P, Matrix *M, unsigned rows)
1747 M->NbRows = rows;
1748 M->NbColumns = P->Dimension+2;
1749 M->p_Init = P->p_Init;
1750 M->p = P->Constraint;