rename summate.cc to barvinok_summate.cc
[barvinok.git] / util.c
blob4a1ce065b020a4fbdef66974e99913155e0123e3
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"
7 #include "lattice_point.h"
9 #define ALLOC(type) (type*)malloc(sizeof(type))
10 #define ALLOCN(type,n) (type*)malloc((n) * sizeof(type))
12 #ifdef __GNUC__
13 #define NALLOC(p,n) p = (typeof(p))malloc((n) * sizeof(*p))
14 #else
15 #define NALLOC(p,n) p = (void *)malloc((n) * sizeof(*p))
16 #endif
18 void manual_count(Polyhedron *P, Value* result)
20 Polyhedron *U = Universe_Polyhedron(0);
21 Enumeration *en = Polyhedron_Enumerate(P,U,1024,NULL);
22 Value *v = compute_poly(en,NULL);
23 value_assign(*result, *v);
24 value_clear(*v);
25 free(v);
26 Enumeration_Free(en);
27 Polyhedron_Free(U);
30 #include <barvinok/evalue.h>
31 #include <barvinok/util.h>
32 #include <barvinok/barvinok.h>
34 /* Return random value between 0 and max-1 inclusive
36 int random_int(int max) {
37 return (int) (((double)(max))*rand()/(RAND_MAX+1.0));
40 Polyhedron *Polyhedron_Read(unsigned MaxRays)
42 int vertices = 0;
43 unsigned NbRows, NbColumns;
44 Matrix *M;
45 Polyhedron *P;
46 char s[128];
48 while (fgets(s, sizeof(s), stdin)) {
49 if (*s == '#')
50 continue;
51 if (strncasecmp(s, "vertices", sizeof("vertices")-1) == 0)
52 vertices = 1;
53 if (sscanf(s, "%u %u", &NbRows, &NbColumns) == 2)
54 break;
56 if (feof(stdin))
57 return NULL;
58 M = Matrix_Alloc(NbRows,NbColumns);
59 Matrix_Read_Input(M);
60 if (vertices)
61 P = Rays2Polyhedron(M, MaxRays);
62 else
63 P = Constraints2Polyhedron(M, MaxRays);
64 Matrix_Free(M);
65 return P;
68 /* Inplace polarization
70 void Polyhedron_Polarize(Polyhedron *P)
72 unsigned NbRows = P->NbConstraints + P->NbRays;
73 int i;
74 Value **q;
76 q = (Value **)malloc(NbRows * sizeof(Value *));
77 assert(q);
78 for (i = 0; i < P->NbRays; ++i)
79 q[i] = P->Ray[i];
80 for (; i < NbRows; ++i)
81 q[i] = P->Constraint[i-P->NbRays];
82 P->NbConstraints = NbRows - P->NbConstraints;
83 P->NbRays = NbRows - P->NbRays;
84 free(P->Constraint);
85 P->Constraint = q;
86 P->Ray = q + P->NbConstraints;
90 * Rather general polar
91 * We can optimize it significantly if we assume that
92 * P includes zero
94 * Also, we calculate the polar as defined in Schrijver
95 * The opposite should probably work as well and would
96 * eliminate the need for multiplying by -1
98 Polyhedron* Polyhedron_Polar(Polyhedron *P, unsigned NbMaxRays)
100 int i;
101 Value mone;
102 unsigned dim = P->Dimension + 2;
103 Matrix *M = Matrix_Alloc(P->NbRays, dim);
105 assert(M);
106 value_init(mone);
107 value_set_si(mone, -1);
108 for (i = 0; i < P->NbRays; ++i) {
109 Vector_Scale(P->Ray[i], M->p[i], mone, dim);
110 value_multiply(M->p[i][0], M->p[i][0], mone);
111 value_multiply(M->p[i][dim-1], M->p[i][dim-1], mone);
113 P = Constraints2Polyhedron(M, NbMaxRays);
114 assert(P);
115 Matrix_Free(M);
116 value_clear(mone);
117 return P;
121 * Returns the supporting cone of P at the vertex with index v
123 Polyhedron* supporting_cone(Polyhedron *P, int v)
125 Matrix *M;
126 Value tmp;
127 int i, n, j;
128 unsigned char *supporting = (unsigned char *)malloc(P->NbConstraints);
129 unsigned dim = P->Dimension + 2;
131 assert(v >=0 && v < P->NbRays);
132 assert(value_pos_p(P->Ray[v][dim-1]));
133 assert(supporting);
135 value_init(tmp);
136 for (i = 0, n = 0; i < P->NbConstraints; ++i) {
137 Inner_Product(P->Constraint[i] + 1, P->Ray[v] + 1, dim - 1, &tmp);
138 if ((supporting[i] = value_zero_p(tmp)))
139 ++n;
141 assert(n >= dim - 2);
142 value_clear(tmp);
143 M = Matrix_Alloc(n, dim);
144 assert(M);
145 for (i = 0, j = 0; i < P->NbConstraints; ++i)
146 if (supporting[i]) {
147 value_set_si(M->p[j][dim-1], 0);
148 Vector_Copy(P->Constraint[i], M->p[j++], dim-1);
150 free(supporting);
151 P = Constraints2Polyhedron(M, P->NbRays+1);
152 assert(P);
153 Matrix_Free(M);
154 return P;
157 #define INT_BITS (sizeof(unsigned) * 8)
159 unsigned *supporting_constraints(Matrix *Constraints, Param_Vertices *v, int *n)
161 Value lcm, tmp, tmp2;
162 unsigned dim = Constraints->NbColumns;
163 unsigned nparam = v->Vertex->NbColumns - 2;
164 unsigned nvar = dim - nparam - 2;
165 int len = (Constraints->NbRows+INT_BITS-1)/INT_BITS;
166 unsigned *supporting = (unsigned *)calloc(len, sizeof(unsigned));
167 int i, j;
168 Vector *row;
169 int ix;
170 unsigned bx;
172 assert(supporting);
173 row = Vector_Alloc(nparam+1);
174 assert(row);
175 value_init(lcm);
176 value_init(tmp);
177 value_init(tmp2);
178 value_set_si(lcm, 1);
179 for (i = 0, *n = 0, ix = 0, bx = MSB; i < Constraints->NbRows; ++i) {
180 Vector_Set(row->p, 0, nparam+1);
181 for (j = 0 ; j < nvar; ++j) {
182 value_set_si(tmp, 1);
183 value_assign(tmp2, Constraints->p[i][j+1]);
184 if (value_ne(lcm, v->Vertex->p[j][nparam+1])) {
185 value_assign(tmp, lcm);
186 value_lcm(lcm, lcm, v->Vertex->p[j][nparam+1]);
187 value_division(tmp, lcm, tmp);
188 value_multiply(tmp2, tmp2, lcm);
189 value_division(tmp2, tmp2, v->Vertex->p[j][nparam+1]);
191 Vector_Combine(row->p, v->Vertex->p[j], row->p,
192 tmp, tmp2, nparam+1);
194 value_set_si(tmp, 1);
195 Vector_Combine(row->p, Constraints->p[i]+1+nvar, row->p, tmp, lcm, nparam+1);
196 for (j = 0; j < nparam+1; ++j)
197 if (value_notzero_p(row->p[j]))
198 break;
199 if (j == nparam + 1) {
200 supporting[ix] |= bx;
201 ++*n;
203 NEXT(ix, bx);
205 assert(*n >= nvar);
206 value_clear(tmp);
207 value_clear(tmp2);
208 value_clear(lcm);
209 Vector_Free(row);
211 return supporting;
214 Polyhedron* supporting_cone_p(Polyhedron *P, Param_Vertices *v)
216 Matrix *M;
217 unsigned dim = P->Dimension + 2;
218 unsigned nparam = v->Vertex->NbColumns - 2;
219 unsigned nvar = dim - nparam - 2;
220 int i, n, j;
221 int ix;
222 unsigned bx;
223 unsigned *supporting;
224 Matrix View;
226 Polyhedron_Matrix_View(P, &View, P->NbConstraints);
227 supporting = supporting_constraints(&View, v, &n);
228 M = Matrix_Alloc(n, nvar+2);
229 assert(M);
230 for (i = 0, j = 0, ix = 0, bx = MSB; i < P->NbConstraints; ++i) {
231 if (supporting[ix] & bx) {
232 value_set_si(M->p[j][nvar+1], 0);
233 Vector_Copy(P->Constraint[i], M->p[j++], nvar+1);
235 NEXT(ix, bx);
237 free(supporting);
238 P = Constraints2Polyhedron(M, P->NbRays+1);
239 assert(P);
240 Matrix_Free(M);
241 return P;
244 Polyhedron* triangulate_cone(Polyhedron *P, unsigned NbMaxCons)
246 struct barvinok_options *options = barvinok_options_new_with_defaults();
247 options->MaxRays = NbMaxCons;
248 P = triangulate_cone_with_options(P, options);
249 barvinok_options_free(options);
250 return P;
253 Polyhedron* triangulate_cone_with_options(Polyhedron *P,
254 struct barvinok_options *options)
256 const static int MAX_TRY=10;
257 int i, j, r, n, t;
258 Value tmp;
259 unsigned dim = P->Dimension;
260 Matrix *M = Matrix_Alloc(P->NbRays+1, dim+3);
261 Matrix *M2, *M3;
262 Polyhedron *L, *R, *T;
263 assert(P->NbEq == 0);
265 L = NULL;
266 R = NULL;
267 value_init(tmp);
269 Vector_Set(M->p[0]+1, 0, dim+1);
270 value_set_si(M->p[0][0], 1);
271 value_set_si(M->p[0][dim+2], 1);
272 Vector_Set(M->p[P->NbRays]+1, 0, dim+2);
273 value_set_si(M->p[P->NbRays][0], 1);
274 value_set_si(M->p[P->NbRays][dim+1], 1);
276 for (i = 0, r = 1; i < P->NbRays; ++i) {
277 if (value_notzero_p(P->Ray[i][dim+1]))
278 continue;
279 Vector_Copy(P->Ray[i], M->p[r], dim+1);
280 value_set_si(M->p[r][dim+2], 0);
281 ++r;
284 M2 = Matrix_Alloc(dim+1, dim+2);
286 t = 0;
287 if (options->try_Delaunay_triangulation) {
288 /* Delaunay triangulation */
289 for (r = 1; r < P->NbRays; ++r) {
290 Inner_Product(M->p[r]+1, M->p[r]+1, dim, &tmp);
291 value_assign(M->p[r][dim+1], tmp);
293 M3 = Matrix_Copy(M);
294 L = Rays2Polyhedron(M3, options->MaxRays);
295 Matrix_Free(M3);
296 ++t;
297 } else {
298 try_again:
299 /* Usually R should still be 0 */
300 Domain_Free(R);
301 Polyhedron_Free(L);
302 for (r = 1; r < P->NbRays; ++r) {
303 value_set_si(M->p[r][dim+1], random_int((t+1)*dim*P->NbRays)+1);
305 M3 = Matrix_Copy(M);
306 L = Rays2Polyhedron(M3, options->MaxRays);
307 Matrix_Free(M3);
308 ++t;
310 assert(t <= MAX_TRY);
312 R = NULL;
313 n = 0;
315 POL_ENSURE_FACETS(L);
316 for (i = 0; i < L->NbConstraints; ++i) {
317 /* Ignore perpendicular facets, i.e., facets with 0 z-coordinate */
318 if (value_negz_p(L->Constraint[i][dim+1]))
319 continue;
320 if (value_notzero_p(L->Constraint[i][dim+2]))
321 continue;
322 for (j = 1, r = 1; j < M->NbRows; ++j) {
323 Inner_Product(M->p[j]+1, L->Constraint[i]+1, dim+1, &tmp);
324 if (value_notzero_p(tmp))
325 continue;
326 if (r > dim)
327 goto try_again;
328 Vector_Copy(M->p[j]+1, M2->p[r]+1, dim);
329 value_set_si(M2->p[r][0], 1);
330 value_set_si(M2->p[r][dim+1], 0);
331 ++r;
333 assert(r == dim+1);
334 Vector_Set(M2->p[0]+1, 0, dim);
335 value_set_si(M2->p[0][0], 1);
336 value_set_si(M2->p[0][dim+1], 1);
337 T = Rays2Polyhedron(M2, P->NbConstraints+1);
338 T->next = R;
339 R = T;
340 ++n;
342 Matrix_Free(M2);
344 Polyhedron_Free(L);
345 value_clear(tmp);
346 Matrix_Free(M);
348 return R;
351 void check_triangulization(Polyhedron *P, Polyhedron *T)
353 Polyhedron *C, *D, *E, *F, *G, *U;
354 for (C = T; C; C = C->next) {
355 if (C == T)
356 U = C;
357 else
358 U = DomainConvex(DomainUnion(U, C, 100), 100);
359 for (D = C->next; D; D = D->next) {
360 F = C->next;
361 G = D->next;
362 C->next = NULL;
363 D->next = NULL;
364 E = DomainIntersection(C, D, 600);
365 assert(E->NbRays == 0 || E->NbEq >= 1);
366 Polyhedron_Free(E);
367 C->next = F;
368 D->next = G;
371 assert(PolyhedronIncludes(U, P));
372 assert(PolyhedronIncludes(P, U));
375 /* Computes x, y and g such that g = gcd(a,b) and a*x+b*y = g */
376 void Extended_Euclid(Value a, Value b, Value *x, Value *y, Value *g)
378 Value c, d, e, f, tmp;
380 value_init(c);
381 value_init(d);
382 value_init(e);
383 value_init(f);
384 value_init(tmp);
385 value_absolute(c, a);
386 value_absolute(d, b);
387 value_set_si(e, 1);
388 value_set_si(f, 0);
389 while(value_pos_p(d)) {
390 value_division(tmp, c, d);
391 value_multiply(tmp, tmp, f);
392 value_subtract(e, e, tmp);
393 value_division(tmp, c, d);
394 value_multiply(tmp, tmp, d);
395 value_subtract(c, c, tmp);
396 value_swap(c, d);
397 value_swap(e, f);
399 value_assign(*g, c);
400 if (value_zero_p(a))
401 value_set_si(*x, 0);
402 else if (value_pos_p(a))
403 value_assign(*x, e);
404 else value_oppose(*x, e);
405 if (value_zero_p(b))
406 value_set_si(*y, 0);
407 else {
408 value_multiply(tmp, a, *x);
409 value_subtract(tmp, c, tmp);
410 value_division(*y, tmp, b);
412 value_clear(c);
413 value_clear(d);
414 value_clear(e);
415 value_clear(f);
416 value_clear(tmp);
419 static int unimodular_complete_1(Matrix *m)
421 Value g, b, c, old, tmp;
422 unsigned i, j;
423 int ok;
425 value_init(b);
426 value_init(c);
427 value_init(g);
428 value_init(old);
429 value_init(tmp);
430 value_assign(g, m->p[0][0]);
431 for (i = 1; value_zero_p(g) && i < m->NbColumns; ++i) {
432 for (j = 0; j < m->NbColumns; ++j) {
433 if (j == i-1)
434 value_set_si(m->p[i][j], 1);
435 else
436 value_set_si(m->p[i][j], 0);
438 value_assign(g, m->p[0][i]);
440 for (; i < m->NbColumns; ++i) {
441 value_assign(old, g);
442 Extended_Euclid(old, m->p[0][i], &c, &b, &g);
443 value_oppose(b, b);
444 for (j = 0; j < m->NbColumns; ++j) {
445 if (j < i) {
446 value_multiply(tmp, m->p[0][j], b);
447 value_division(m->p[i][j], tmp, old);
448 } else if (j == i)
449 value_assign(m->p[i][j], c);
450 else
451 value_set_si(m->p[i][j], 0);
454 ok = value_one_p(g);
455 value_clear(b);
456 value_clear(c);
457 value_clear(g);
458 value_clear(old);
459 value_clear(tmp);
460 return ok;
463 int unimodular_complete(Matrix *M, int row)
465 int r;
466 int ok = 1;
467 Matrix *H, *Q, *U;
469 if (row == 1)
470 return unimodular_complete_1(M);
472 left_hermite(M, &H, &Q, &U);
473 Matrix_Free(U);
474 for (r = 0; ok && r < row; ++r)
475 if (value_notone_p(H->p[r][r]))
476 ok = 0;
477 Matrix_Free(H);
478 for (r = row; r < M->NbRows; ++r)
479 Vector_Copy(Q->p[r], M->p[r], M->NbColumns);
480 Matrix_Free(Q);
481 return ok;
485 * left_hermite may leave positive entries below the main diagonal in H.
486 * This function postprocesses the output of left_hermite to make
487 * the non-zero entries below the main diagonal negative.
489 void neg_left_hermite(Matrix *A, Matrix **H_p, Matrix **Q_p, Matrix **U_p)
491 int row, col, i, j;
492 Matrix *H, *U, *Q;
494 left_hermite(A, &H, &Q, &U);
495 *H_p = H;
496 *Q_p = Q;
497 *U_p = U;
499 for (row = 0, col = 0; col < H->NbColumns; ++col, ++row) {
500 while (value_zero_p(H->p[row][col]))
501 ++row;
502 for (i = 0; i < col; ++i) {
503 if (value_negz_p(H->p[row][i]))
504 continue;
506 /* subtract column col from column i in H and U */
507 for (j = 0; j < H->NbRows; ++j)
508 value_subtract(H->p[j][i], H->p[j][i], H->p[j][col]);
509 for (j = 0; j < U->NbRows; ++j)
510 value_subtract(U->p[j][i], U->p[j][i], U->p[j][col]);
512 /* add row i to row col in Q */
513 for (j = 0; j < Q->NbColumns; ++j)
514 value_addto(Q->p[col][j], Q->p[col][j], Q->p[i][j]);
520 * Returns a full-dimensional polyhedron with the same number
521 * of integer points as P
523 Polyhedron *remove_equalities(Polyhedron *P, unsigned MaxRays)
525 Polyhedron *Q = Polyhedron_Copy(P);
526 unsigned dim = P->Dimension;
527 Matrix *m1, *m2;
528 int i;
530 if (Q->NbEq == 0)
531 return Q;
533 Q = DomainConstraintSimplify(Q, MaxRays);
534 if (emptyQ2(Q))
535 return Q;
537 m1 = Matrix_Alloc(dim, dim);
538 for (i = 0; i < Q->NbEq; ++i)
539 Vector_Copy(Q->Constraint[i]+1, m1->p[i], dim);
541 /* m1 may not be unimodular, but we won't be throwing anything away */
542 unimodular_complete(m1, Q->NbEq);
544 m2 = Matrix_Alloc(dim+1-Q->NbEq, dim+1);
545 for (i = Q->NbEq; i < dim; ++i)
546 Vector_Copy(m1->p[i], m2->p[i-Q->NbEq], dim);
547 value_set_si(m2->p[dim-Q->NbEq][dim], 1);
548 Matrix_Free(m1);
550 P = Polyhedron_Image(Q, m2, MaxRays);
551 Matrix_Free(m2);
552 Polyhedron_Free(Q);
554 return P;
558 * Returns a full-dimensional polyhedron with the same number
559 * of integer points as P
560 * nvar specifies the number of variables
561 * The remaining dimensions are assumed to be parameters
562 * Destroys P
563 * factor is NbEq x (nparam+2) matrix, containing stride constraints
564 * on the parameters; column nparam is the constant;
565 * column nparam+1 is the stride
567 * if factor is NULL, only remove equalities that don't affect
568 * the number of points
570 Polyhedron *remove_equalities_p(Polyhedron *P, unsigned nvar, Matrix **factor,
571 unsigned MaxRays)
573 Value g;
574 Polyhedron *Q;
575 unsigned dim = P->Dimension;
576 Matrix *m1, *m2, *f;
577 int i, j;
579 if (P->NbEq == 0)
580 return P;
582 m1 = Matrix_Alloc(nvar, nvar);
583 P = DomainConstraintSimplify(P, MaxRays);
584 if (factor) {
585 f = Matrix_Alloc(P->NbEq, dim-nvar+2);
586 *factor = f;
588 value_init(g);
589 for (i = 0, j = 0; i < P->NbEq; ++i) {
590 if (First_Non_Zero(P->Constraint[i]+1, nvar) == -1)
591 continue;
593 Vector_Gcd(P->Constraint[i]+1, nvar, &g);
594 if (!factor && value_notone_p(g))
595 continue;
597 if (factor) {
598 Vector_Copy(P->Constraint[i]+1+nvar, f->p[j], dim-nvar+1);
599 value_assign(f->p[j][dim-nvar+1], g);
602 Vector_Copy(P->Constraint[i]+1, m1->p[j], nvar);
604 ++j;
606 value_clear(g);
608 unimodular_complete(m1, j);
610 m2 = Matrix_Alloc(dim+1-j, dim+1);
611 for (i = 0; i < nvar-j ; ++i)
612 Vector_Copy(m1->p[i+j], m2->p[i], nvar);
613 Matrix_Free(m1);
614 for (i = nvar-j; i <= dim-j; ++i)
615 value_set_si(m2->p[i][i+j], 1);
617 Q = Polyhedron_Image(P, m2, MaxRays);
618 Matrix_Free(m2);
619 Polyhedron_Free(P);
621 return Q;
624 void Line_Length(Polyhedron *P, Value *len)
626 Value tmp, pos, neg;
627 int p = 0, n = 0;
628 int i;
630 assert(P->Dimension == 1);
632 value_init(tmp);
633 value_init(pos);
634 value_init(neg);
636 for (i = 0; i < P->NbConstraints; ++i) {
637 value_oppose(tmp, P->Constraint[i][2]);
638 if (value_pos_p(P->Constraint[i][1])) {
639 mpz_cdiv_q(tmp, tmp, P->Constraint[i][1]);
640 if (!p || value_gt(tmp, pos))
641 value_assign(pos, tmp);
642 p = 1;
643 } else if (value_neg_p(P->Constraint[i][1])) {
644 mpz_fdiv_q(tmp, tmp, P->Constraint[i][1]);
645 if (!n || value_lt(tmp, neg))
646 value_assign(neg, tmp);
647 n = 1;
649 if (n && p) {
650 value_subtract(tmp, neg, pos);
651 value_increment(*len, tmp);
652 } else
653 value_set_si(*len, -1);
656 value_clear(tmp);
657 value_clear(pos);
658 value_clear(neg);
662 * Factors the polyhedron P into polyhedra Q_i such that
663 * the number of integer points in P is equal to the product
664 * of the number of integer points in the individual Q_i
666 * If no factors can be found, NULL is returned.
667 * Otherwise, a linked list of the factors is returned.
669 * If there are factors and if T is not NULL, then a matrix will be
670 * returned through T expressing the old variables in terms of the
671 * new variables as they appear in the sequence of factors.
673 * The algorithm works by first computing the Hermite normal form
674 * and then grouping columns linked by one or more constraints together,
675 * where a constraints "links" two or more columns if the constraint
676 * has nonzero coefficients in the columns.
678 Polyhedron* Polyhedron_Factor(Polyhedron *P, unsigned nparam, Matrix **T,
679 unsigned NbMaxRays)
681 int i, j, k;
682 Matrix *M, *H, *Q, *U;
683 int *pos; /* for each column: row position of pivot */
684 int *group; /* group to which a column belongs */
685 int *cnt; /* number of columns in the group */
686 int *rowgroup; /* group to which a constraint belongs */
687 int nvar = P->Dimension - nparam;
688 Polyhedron *F = NULL;
690 if (nvar <= 1)
691 return NULL;
693 NALLOC(pos, nvar);
694 NALLOC(group, nvar);
695 NALLOC(cnt, nvar);
696 NALLOC(rowgroup, P->NbConstraints);
698 M = Matrix_Alloc(P->NbConstraints, nvar);
699 for (i = 0; i < P->NbConstraints; ++i)
700 Vector_Copy(P->Constraint[i]+1, M->p[i], nvar);
701 left_hermite(M, &H, &Q, &U);
702 Matrix_Free(M);
703 Matrix_Free(Q);
705 for (i = 0; i < P->NbConstraints; ++i)
706 rowgroup[i] = -1;
707 for (i = 0, j = 0; i < H->NbColumns; ++i) {
708 for ( ; j < H->NbRows; ++j)
709 if (value_notzero_p(H->p[j][i]))
710 break;
711 assert (j < H->NbRows);
712 pos[i] = j;
714 for (i = 0; i < nvar; ++i) {
715 group[i] = i;
716 cnt[i] = 1;
718 for (i = 0; i < H->NbColumns && cnt[0] < nvar; ++i) {
719 if (rowgroup[pos[i]] == -1)
720 rowgroup[pos[i]] = i;
721 for (j = pos[i]+1; j < H->NbRows; ++j) {
722 if (value_zero_p(H->p[j][i]))
723 continue;
724 if (rowgroup[j] != -1)
725 continue;
726 rowgroup[j] = group[i];
727 for (k = i+1; k < H->NbColumns && j >= pos[k]; ++k) {
728 int g = group[k];
729 while (cnt[g] == 0)
730 g = group[g];
731 group[k] = g;
732 if (group[k] != group[i] && value_notzero_p(H->p[j][k])) {
733 assert(cnt[group[k]] != 0);
734 assert(cnt[group[i]] != 0);
735 if (group[i] < group[k]) {
736 cnt[group[i]] += cnt[group[k]];
737 cnt[group[k]] = 0;
738 group[k] = group[i];
739 } else {
740 cnt[group[k]] += cnt[group[i]];
741 cnt[group[i]] = 0;
742 group[i] = group[k];
749 if (cnt[0] != nvar) {
750 /* Extract out pure context constraints separately */
751 Polyhedron **next = &F;
752 int tot_d = 0;
753 if (T)
754 *T = Matrix_Alloc(nvar, nvar);
755 for (i = nparam ? -1 : 0; i < nvar; ++i) {
756 int d;
758 if (i == -1) {
759 for (j = 0, k = 0; j < P->NbConstraints; ++j)
760 if (rowgroup[j] == -1) {
761 if (First_Non_Zero(P->Constraint[j]+1+nvar,
762 nparam) == -1)
763 rowgroup[j] = -2;
764 else
765 ++k;
767 if (k == 0)
768 continue;
769 d = 0;
770 } else {
771 if (cnt[i] == 0)
772 continue;
773 d = cnt[i];
774 for (j = 0, k = 0; j < P->NbConstraints; ++j)
775 if (rowgroup[j] >= 0 && group[rowgroup[j]] == i) {
776 rowgroup[j] = i;
777 ++k;
781 if (T)
782 for (j = 0; j < nvar; ++j) {
783 int l, m;
784 for (l = 0, m = 0; m < d; ++l) {
785 if (group[l] != i)
786 continue;
787 value_assign((*T)->p[j][tot_d+m++], U->p[j][l]);
791 M = Matrix_Alloc(k, d+nparam+2);
792 for (j = 0, k = 0; j < P->NbConstraints; ++j) {
793 int l, m;
794 if (rowgroup[j] != i)
795 continue;
796 value_assign(M->p[k][0], P->Constraint[j][0]);
797 for (l = 0, m = 0; m < d; ++l) {
798 if (group[l] != i)
799 continue;
800 value_assign(M->p[k][1+m++], H->p[j][l]);
802 Vector_Copy(P->Constraint[j]+1+nvar, M->p[k]+1+m, nparam+1);
803 ++k;
805 *next = Constraints2Polyhedron(M, NbMaxRays);
806 next = &(*next)->next;
807 Matrix_Free(M);
808 tot_d += d;
811 Matrix_Free(U);
812 Matrix_Free(H);
813 free(pos);
814 free(group);
815 free(cnt);
816 free(rowgroup);
817 return F;
821 * Project on final dim dimensions
823 Polyhedron* Polyhedron_Project(Polyhedron *P, int dim)
825 int i;
826 int remove = P->Dimension - dim;
827 Matrix *T;
828 Polyhedron *I;
830 if (P->Dimension == dim)
831 return Polyhedron_Copy(P);
833 T = Matrix_Alloc(dim+1, P->Dimension+1);
834 for (i = 0; i < dim+1; ++i)
835 value_set_si(T->p[i][i+remove], 1);
836 I = Polyhedron_Image(P, T, P->NbConstraints);
837 Matrix_Free(T);
838 return I;
841 /* Constructs a new constraint that ensures that
842 * the first constraint is (strictly) smaller than
843 * the second.
845 static void smaller_constraint(Value *a, Value *b, Value *c, int pos, int shift,
846 int len, int strict, Value *tmp)
848 value_oppose(*tmp, b[pos+1]);
849 value_set_si(c[0], 1);
850 Vector_Combine(a+1+shift, b+1+shift, c+1, *tmp, a[pos+1], len-shift-1);
851 if (strict)
852 value_decrement(c[len-shift-1], c[len-shift-1]);
853 ConstraintSimplify(c, c, len-shift, tmp);
857 /* For each pair of lower and upper bounds on the first variable,
858 * calls fn with the set of constraints on the remaining variables
859 * where these bounds are active, i.e., (stricly) larger/smaller than
860 * the other lower/upper bounds, the lower and upper bound and the
861 * call back data.
863 * If the first variable is equal to an affine combination of the
864 * other variables then fn is called with both lower and upper
865 * pointing to the corresponding equality.
867 * If there is no lower (or upper) bound, then NULL is passed
868 * as the corresponding bound.
870 void for_each_lower_upper_bound(Polyhedron *P,
871 for_each_lower_upper_bound_init init,
872 for_each_lower_upper_bound_fn fn,
873 void *cb_data)
875 unsigned dim = P->Dimension;
876 Matrix *M;
877 int *pos;
878 int i, j, p, n, z;
879 int k, l, k2, l2, q;
880 Value g;
882 if (value_zero_p(P->Constraint[0][0]) &&
883 value_notzero_p(P->Constraint[0][1])) {
884 M = Matrix_Alloc(P->NbConstraints-1, dim-1+2);
885 for (i = 1; i < P->NbConstraints; ++i) {
886 value_assign(M->p[i-1][0], P->Constraint[i][0]);
887 Vector_Copy(P->Constraint[i]+2, M->p[i-1]+1, dim);
889 if (init)
890 init(1, cb_data);
891 fn(M, P->Constraint[0], P->Constraint[0], cb_data);
892 Matrix_Free(M);
893 return;
896 value_init(g);
897 pos = ALLOCN(int, P->NbConstraints);
899 for (i = 0, z = 0; i < P->NbConstraints; ++i)
900 if (value_zero_p(P->Constraint[i][1]))
901 pos[P->NbConstraints-1 - z++] = i;
902 /* put those with positive coefficients first; number: p */
903 for (i = 0, p = 0, n = P->NbConstraints-z-1; i < P->NbConstraints; ++i)
904 if (value_pos_p(P->Constraint[i][1]))
905 pos[p++] = i;
906 else if (value_neg_p(P->Constraint[i][1]))
907 pos[n--] = i;
908 n = P->NbConstraints-z-p;
910 if (init)
911 init(p*n, cb_data);
913 M = Matrix_Alloc((p ? p-1 : 0) + (n ? n-1 : 0) + z + 1, dim-1+2);
914 for (i = 0; i < z; ++i) {
915 value_assign(M->p[i][0], P->Constraint[pos[P->NbConstraints-1 - i]][0]);
916 Vector_Copy(P->Constraint[pos[P->NbConstraints-1 - i]]+2,
917 M->p[i]+1, dim);
919 for (k = p ? 0 : -1; k < p; ++k) {
920 for (k2 = 0; k2 < p; ++k2) {
921 if (k2 == k)
922 continue;
923 q = 1 + z + k2 - (k2 > k);
924 smaller_constraint(
925 P->Constraint[pos[k]],
926 P->Constraint[pos[k2]],
927 M->p[q], 0, 1, dim+2, k2 > k, &g);
929 for (l = n ? p : p-1; l < p+n; ++l) {
930 Value *lower;
931 Value *upper;
932 for (l2 = p; l2 < p+n; ++l2) {
933 if (l2 == l)
934 continue;
935 q = 1 + z + l2-1 - (l2 > l);
936 smaller_constraint(
937 P->Constraint[pos[l2]],
938 P->Constraint[pos[l]],
939 M->p[q], 0, 1, dim+2, l2 > l, &g);
941 if (p && n)
942 smaller_constraint(P->Constraint[pos[k]],
943 P->Constraint[pos[l]],
944 M->p[z], 0, 1, dim+2, 0, &g);
945 lower = p ? P->Constraint[pos[k]] : NULL;
946 upper = n ? P->Constraint[pos[l]] : NULL;
947 fn(M, lower, upper, cb_data);
950 Matrix_Free(M);
952 free(pos);
953 value_clear(g);
956 struct section { Polyhedron * D; evalue E; };
958 struct PLL_data {
959 int nd;
960 unsigned MaxRays;
961 Polyhedron *C;
962 evalue mone;
963 struct section *s;
966 static void PLL_init(unsigned n, void *cb_data)
968 struct PLL_data *data = (struct PLL_data *)cb_data;
970 data->s = ALLOCN(struct section, n);
973 /* Computes ceil(-coef/abs(d)) */
974 static evalue* bv_ceil3(Value *coef, int len, Value d, Polyhedron *P)
976 Value t;
977 evalue *EP, *f;
978 Vector *val = Vector_Alloc(len);
980 value_init(t);
981 Vector_Oppose(coef, val->p, len);
982 value_absolute(t, d);
984 EP = ceiling(val->p, t, len-1, P);
986 value_clear(t);
987 Vector_Free(val);
989 return EP;
992 static void PLL_cb(Matrix *M, Value *lower, Value *upper, void *cb_data)
994 struct PLL_data *data = (struct PLL_data *)cb_data;
995 unsigned dim = M->NbColumns-1;
996 Matrix *M2;
997 Polyhedron *T;
998 evalue *L, *U;
1000 assert(lower);
1001 assert(upper);
1003 M2 = Matrix_Copy(M);
1004 T = Constraints2Polyhedron(M2, data->MaxRays);
1005 Matrix_Free(M2);
1006 data->s[data->nd].D = DomainIntersection(T, data->C, data->MaxRays);
1007 Domain_Free(T);
1009 POL_ENSURE_VERTICES(data->s[data->nd].D);
1010 if (emptyQ(data->s[data->nd].D)) {
1011 Polyhedron_Free(data->s[data->nd].D);
1012 return;
1014 L = bv_ceil3(lower+1+1, dim-1+1, lower[0+1], data->s[data->nd].D);
1015 U = bv_ceil3(upper+1+1, dim-1+1, upper[0+1], data->s[data->nd].D);
1016 eadd(L, U);
1017 eadd(&data->mone, U);
1018 emul(&data->mone, U);
1019 data->s[data->nd].E = *U;
1020 evalue_free(L);
1021 free(U);
1022 ++data->nd;
1025 static evalue *ParamLine_Length_mod(Polyhedron *P, Polyhedron *C, unsigned MaxRays)
1027 unsigned dim = P->Dimension;
1028 unsigned nvar = dim - C->Dimension;
1029 struct PLL_data data;
1030 evalue *F;
1031 int k;
1033 assert(nvar == 1);
1035 value_init(data.mone.d);
1036 evalue_set_si(&data.mone, -1, 1);
1038 data.nd = 0;
1039 data.MaxRays = MaxRays;
1040 data.C = C;
1041 for_each_lower_upper_bound(P, PLL_init, PLL_cb, &data);
1043 F = ALLOC(evalue);
1044 value_init(F->d);
1045 value_set_si(F->d, 0);
1046 F->x.p = new_enode(partition, 2*data.nd, dim-nvar);
1047 for (k = 0; k < data.nd; ++k) {
1048 EVALUE_SET_DOMAIN(F->x.p->arr[2*k], data.s[k].D);
1049 value_clear(F->x.p->arr[2*k+1].d);
1050 F->x.p->arr[2*k+1] = data.s[k].E;
1052 free(data.s);
1054 free_evalue_refs(&data.mone);
1056 return F;
1059 evalue* ParamLine_Length(Polyhedron *P, Polyhedron *C,
1060 struct barvinok_options *options)
1062 evalue* tmp;
1063 tmp = ParamLine_Length_mod(P, C, options->MaxRays);
1064 if (options->lookup_table) {
1065 evalue_mod2table(tmp, C->Dimension);
1066 reduce_evalue(tmp);
1068 return tmp;
1071 Bool isIdentity(Matrix *M)
1073 unsigned i, j;
1074 if (M->NbRows != M->NbColumns)
1075 return False;
1077 for (i = 0;i < M->NbRows; i ++)
1078 for (j = 0; j < M->NbColumns; j ++)
1079 if (i == j) {
1080 if(value_notone_p(M->p[i][j]))
1081 return False;
1082 } else {
1083 if(value_notzero_p(M->p[i][j]))
1084 return False;
1086 return True;
1089 void Param_Polyhedron_Print(FILE* DST, Param_Polyhedron *PP, char **param_names)
1091 Param_Domain *P;
1092 Param_Vertices *V;
1094 for(P=PP->D;P;P=P->next) {
1096 /* prints current val. dom. */
1097 fprintf(DST, "---------------------------------------\n");
1098 fprintf(DST, "Domain :\n");
1099 Print_Domain(DST, P->Domain, param_names);
1101 /* scan the vertices */
1102 fprintf(DST, "Vertices :\n");
1103 FORALL_PVertex_in_ParamPolyhedron(V,P,PP) {
1105 /* prints each vertex */
1106 Print_Vertex(DST, V->Vertex, param_names);
1107 fprintf(DST, "\n");
1109 END_FORALL_PVertex_in_ParamPolyhedron;
1113 void Enumeration_Print(FILE *Dst, Enumeration *en, const char * const *params)
1115 for (; en; en = en->next) {
1116 Print_Domain(Dst, en->ValidityDomain, params);
1117 print_evalue(Dst, &en->EP, params);
1121 void Enumeration_Free(Enumeration *en)
1123 Enumeration *ee;
1125 while( en )
1127 free_evalue_refs( &(en->EP) );
1128 Domain_Free( en->ValidityDomain );
1129 ee = en ->next;
1130 free( en );
1131 en = ee;
1135 void Enumeration_mod2table(Enumeration *en, unsigned nparam)
1137 for (; en; en = en->next) {
1138 evalue_mod2table(&en->EP, nparam);
1139 reduce_evalue(&en->EP);
1143 size_t Enumeration_size(Enumeration *en)
1145 size_t s = 0;
1147 for (; en; en = en->next) {
1148 s += domain_size(en->ValidityDomain);
1149 s += evalue_size(&en->EP);
1151 return s;
1154 void Free_ParamNames(char **params, int m)
1156 while (--m >= 0)
1157 free(params[m]);
1158 free(params);
1161 /* Check whether every set in D2 is included in some set of D1 */
1162 int DomainIncludes(Polyhedron *D1, Polyhedron *D2)
1164 for ( ; D2; D2 = D2->next) {
1165 Polyhedron *P1;
1166 for (P1 = D1; P1; P1 = P1->next)
1167 if (PolyhedronIncludes(P1, D2))
1168 break;
1169 if (!P1)
1170 return 0;
1172 return 1;
1175 int line_minmax(Polyhedron *I, Value *min, Value *max)
1177 int i;
1179 if (I->NbEq >= 1) {
1180 value_oppose(I->Constraint[0][2], I->Constraint[0][2]);
1181 /* There should never be a remainder here */
1182 if (value_pos_p(I->Constraint[0][1]))
1183 mpz_fdiv_q(*min, I->Constraint[0][2], I->Constraint[0][1]);
1184 else
1185 mpz_fdiv_q(*min, I->Constraint[0][2], I->Constraint[0][1]);
1186 value_assign(*max, *min);
1187 } else for (i = 0; i < I->NbConstraints; ++i) {
1188 if (value_zero_p(I->Constraint[i][1])) {
1189 Polyhedron_Free(I);
1190 return 0;
1193 value_oppose(I->Constraint[i][2], I->Constraint[i][2]);
1194 if (value_pos_p(I->Constraint[i][1]))
1195 mpz_cdiv_q(*min, I->Constraint[i][2], I->Constraint[i][1]);
1196 else
1197 mpz_fdiv_q(*max, I->Constraint[i][2], I->Constraint[i][1]);
1199 Polyhedron_Free(I);
1200 return 1;
1203 /**
1205 PROCEDURES TO COMPUTE ENUMERATION. recursive procedure, recurse for
1206 each imbriquation
1208 @param pos index position of current loop index (1..hdim-1)
1209 @param P loop domain
1210 @param context context values for fixed indices
1211 @param exist number of existential variables
1212 @return the number of integer points in this
1213 polyhedron
1216 void count_points_e (int pos, Polyhedron *P, int exist, int nparam,
1217 Value *context, Value *res)
1219 Value LB, UB, k, c;
1221 if (emptyQ(P)) {
1222 value_set_si(*res, 0);
1223 return;
1226 if (!exist) {
1227 count_points(pos, P, context, res);
1228 return;
1231 value_init(LB); value_init(UB); value_init(k);
1232 value_set_si(LB,0);
1233 value_set_si(UB,0);
1235 if (lower_upper_bounds(pos,P,context,&LB,&UB) !=0) {
1236 /* Problem if UB or LB is INFINITY */
1237 value_clear(LB); value_clear(UB); value_clear(k);
1238 if (pos > P->Dimension - nparam - exist)
1239 value_set_si(*res, 1);
1240 else
1241 value_set_si(*res, -1);
1242 return;
1245 #ifdef EDEBUG1
1246 if (!P->next) {
1247 int i;
1248 for (value_assign(k,LB); value_le(k,UB); value_increment(k,k)) {
1249 fprintf(stderr, "(");
1250 for (i=1; i<pos; i++) {
1251 value_print(stderr,P_VALUE_FMT,context[i]);
1252 fprintf(stderr,",");
1254 value_print(stderr,P_VALUE_FMT,k);
1255 fprintf(stderr,")\n");
1258 #endif
1260 value_set_si(context[pos],0);
1261 if (value_lt(UB,LB)) {
1262 value_clear(LB); value_clear(UB); value_clear(k);
1263 value_set_si(*res, 0);
1264 return;
1266 if (!P->next) {
1267 if (exist)
1268 value_set_si(*res, 1);
1269 else {
1270 value_subtract(k,UB,LB);
1271 value_add_int(k,k,1);
1272 value_assign(*res, k);
1274 value_clear(LB); value_clear(UB); value_clear(k);
1275 return;
1278 /*-----------------------------------------------------------------*/
1279 /* Optimization idea */
1280 /* If inner loops are not a function of k (the current index) */
1281 /* i.e. if P->Constraint[i][pos]==0 for all P following this and */
1282 /* for all i, */
1283 /* Then CNT = (UB-LB+1)*count_points(pos+1, P->next, context) */
1284 /* (skip the for loop) */
1285 /*-----------------------------------------------------------------*/
1287 value_init(c);
1288 value_set_si(*res, 0);
1289 for (value_assign(k,LB);value_le(k,UB);value_increment(k,k)) {
1290 /* Insert k in context */
1291 value_assign(context[pos],k);
1292 count_points_e(pos+1, P->next, exist, nparam, context, &c);
1293 if(value_notmone_p(c))
1294 value_addto(*res, *res, c);
1295 else {
1296 value_set_si(*res, -1);
1297 break;
1299 if (pos > P->Dimension - nparam - exist &&
1300 value_pos_p(*res))
1301 break;
1303 value_clear(c);
1305 #ifdef EDEBUG11
1306 fprintf(stderr,"%d\n",CNT);
1307 #endif
1309 /* Reset context */
1310 value_set_si(context[pos],0);
1311 value_clear(LB); value_clear(UB); value_clear(k);
1312 return;
1313 } /* count_points_e */
1315 int DomainContains(Polyhedron *P, Value *list_args, int len,
1316 unsigned MaxRays, int set)
1318 int i;
1319 Value m;
1321 if (P->Dimension == len)
1322 return in_domain(P, list_args);
1324 assert(set); // assume list_args is large enough
1325 assert((P->Dimension - len) % 2 == 0);
1326 value_init(m);
1327 for (i = 0; i < P->Dimension - len; i += 2) {
1328 int j, k;
1329 for (j = 0 ; j < P->NbEq; ++j)
1330 if (value_notzero_p(P->Constraint[j][1+len+i]))
1331 break;
1332 assert(j < P->NbEq);
1333 value_absolute(m, P->Constraint[j][1+len+i]);
1334 k = First_Non_Zero(P->Constraint[j]+1, len);
1335 assert(k != -1);
1336 assert(First_Non_Zero(P->Constraint[j]+1+k+1, len - k - 1) == -1);
1337 mpz_fdiv_q(list_args[len+i], list_args[k], m);
1338 mpz_fdiv_r(list_args[len+i+1], list_args[k], m);
1340 value_clear(m);
1342 return in_domain(P, list_args);
1345 Polyhedron *DomainConcat(Polyhedron *head, Polyhedron *tail)
1347 Polyhedron *S;
1348 if (!head)
1349 return tail;
1350 for (S = head; S->next; S = S->next)
1352 S->next = tail;
1353 return head;
1356 evalue *barvinok_lexsmaller_ev(Polyhedron *P, Polyhedron *D, unsigned dim,
1357 Polyhedron *C, unsigned MaxRays)
1359 evalue *ranking;
1360 Polyhedron *RC, *RD, *Q;
1361 unsigned nparam = dim + C->Dimension;
1362 unsigned exist;
1363 Polyhedron *CA;
1365 RC = LexSmaller(P, D, dim, C, MaxRays);
1366 RD = RC->next;
1367 RC->next = NULL;
1369 exist = RD->Dimension - nparam - dim;
1370 CA = align_context(RC, RD->Dimension, MaxRays);
1371 Q = DomainIntersection(RD, CA, MaxRays);
1372 Polyhedron_Free(CA);
1373 Domain_Free(RD);
1374 Polyhedron_Free(RC);
1375 RD = Q;
1377 for (Q = RD; Q; Q = Q->next) {
1378 evalue *t;
1379 Polyhedron *next = Q->next;
1380 Q->next = 0;
1382 t = barvinok_enumerate_e(Q, exist, nparam, MaxRays);
1384 if (Q == RD)
1385 ranking = t;
1386 else {
1387 eadd(t, ranking);
1388 evalue_free(t);
1391 Q->next = next;
1394 Domain_Free(RD);
1396 return ranking;
1399 Enumeration *barvinok_lexsmaller(Polyhedron *P, Polyhedron *D, unsigned dim,
1400 Polyhedron *C, unsigned MaxRays)
1402 evalue *EP = barvinok_lexsmaller_ev(P, D, dim, C, MaxRays);
1404 return partition2enumeration(EP);
1407 /* "align" matrix to have nrows by inserting
1408 * the necessary number of rows and an equal number of columns in front
1410 Matrix *align_matrix(Matrix *M, int nrows)
1412 int i;
1413 int newrows = nrows - M->NbRows;
1414 Matrix *M2 = Matrix_Alloc(nrows, newrows + M->NbColumns);
1415 for (i = 0; i < newrows; ++i)
1416 value_set_si(M2->p[i][i], 1);
1417 for (i = 0; i < M->NbRows; ++i)
1418 Vector_Copy(M->p[i], M2->p[newrows+i]+newrows, M->NbColumns);
1419 return M2;
1422 static void print_varlist(FILE *out, int n, char **names)
1424 int i;
1425 fprintf(out, "[");
1426 for (i = 0; i < n; ++i) {
1427 if (i)
1428 fprintf(out, ",");
1429 fprintf(out, "%s", names[i]);
1431 fprintf(out, "]");
1434 static void print_term(FILE *out, Value v, int pos, int dim, int nparam,
1435 char **iter_names, char **param_names, int *first)
1437 if (value_zero_p(v)) {
1438 if (first && *first && pos >= dim + nparam)
1439 fprintf(out, "0");
1440 return;
1443 if (first) {
1444 if (!*first && value_pos_p(v))
1445 fprintf(out, "+");
1446 *first = 0;
1448 if (pos < dim + nparam) {
1449 if (value_mone_p(v))
1450 fprintf(out, "-");
1451 else if (!value_one_p(v))
1452 value_print(out, VALUE_FMT, v);
1453 if (pos < dim)
1454 fprintf(out, "%s", iter_names[pos]);
1455 else
1456 fprintf(out, "%s", param_names[pos-dim]);
1457 } else
1458 value_print(out, VALUE_FMT, v);
1461 char **util_generate_names(int n, const char *prefix)
1463 int i;
1464 int len = (prefix ? strlen(prefix) : 0) + 10;
1465 char **names = ALLOCN(char*, n);
1466 if (!names) {
1467 fprintf(stderr, "ERROR: memory overflow.\n");
1468 exit(1);
1470 for (i = 0; i < n; ++i) {
1471 names[i] = ALLOCN(char, len);
1472 if (!names[i]) {
1473 fprintf(stderr, "ERROR: memory overflow.\n");
1474 exit(1);
1476 if (!prefix)
1477 snprintf(names[i], len, "%d", i);
1478 else
1479 snprintf(names[i], len, "%s%d", prefix, i);
1482 return names;
1485 void util_free_names(int n, char **names)
1487 int i;
1488 for (i = 0; i < n; ++i)
1489 free(names[i]);
1490 free(names);
1493 void Polyhedron_pprint(FILE *out, Polyhedron *P, int dim, int nparam,
1494 char **iter_names, char **param_names)
1496 int i, j;
1497 Value tmp;
1499 assert(dim + nparam == P->Dimension);
1501 value_init(tmp);
1503 fprintf(out, "{ ");
1504 if (nparam) {
1505 print_varlist(out, nparam, param_names);
1506 fprintf(out, " -> ");
1508 print_varlist(out, dim, iter_names);
1509 fprintf(out, " : ");
1511 if (emptyQ2(P))
1512 fprintf(out, "FALSE");
1513 else for (i = 0; i < P->NbConstraints; ++i) {
1514 int first = 1;
1515 int v = First_Non_Zero(P->Constraint[i]+1, P->Dimension);
1516 if (v == -1 && value_pos_p(P->Constraint[i][0]))
1517 continue;
1518 if (i)
1519 fprintf(out, " && ");
1520 if (v == -1 && value_notzero_p(P->Constraint[i][1+P->Dimension]))
1521 fprintf(out, "FALSE");
1522 else if (value_pos_p(P->Constraint[i][v+1])) {
1523 print_term(out, P->Constraint[i][v+1], v, dim, nparam,
1524 iter_names, param_names, NULL);
1525 if (value_zero_p(P->Constraint[i][0]))
1526 fprintf(out, " = ");
1527 else
1528 fprintf(out, " >= ");
1529 for (j = v+1; j <= dim+nparam; ++j) {
1530 value_oppose(tmp, P->Constraint[i][1+j]);
1531 print_term(out, tmp, j, dim, nparam,
1532 iter_names, param_names, &first);
1534 } else {
1535 value_oppose(tmp, P->Constraint[i][1+v]);
1536 print_term(out, tmp, v, dim, nparam,
1537 iter_names, param_names, NULL);
1538 fprintf(out, " <= ");
1539 for (j = v+1; j <= dim+nparam; ++j)
1540 print_term(out, P->Constraint[i][1+j], j, dim, nparam,
1541 iter_names, param_names, &first);
1545 fprintf(out, " }\n");
1547 value_clear(tmp);
1550 /* Construct a cone over P with P placed at x_d = 1, with
1551 * x_d the coordinate of an extra dimension
1553 * It's probably a mistake to depend so much on the internal
1554 * representation. We should probably simply compute the
1555 * vertices/facets first.
1557 Polyhedron *Cone_over_Polyhedron(Polyhedron *P)
1559 unsigned NbConstraints = 0;
1560 unsigned NbRays = 0;
1561 Polyhedron *C;
1562 int i;
1564 if (POL_HAS(P, POL_INEQUALITIES))
1565 NbConstraints = P->NbConstraints + 1;
1566 if (POL_HAS(P, POL_POINTS))
1567 NbRays = P->NbRays + 1;
1569 C = Polyhedron_Alloc(P->Dimension+1, NbConstraints, NbRays);
1570 if (POL_HAS(P, POL_INEQUALITIES)) {
1571 C->NbEq = P->NbEq;
1572 for (i = 0; i < P->NbConstraints; ++i)
1573 Vector_Copy(P->Constraint[i], C->Constraint[i], P->Dimension+2);
1574 /* n >= 0 */
1575 value_set_si(C->Constraint[P->NbConstraints][0], 1);
1576 value_set_si(C->Constraint[P->NbConstraints][1+P->Dimension], 1);
1578 if (POL_HAS(P, POL_POINTS)) {
1579 C->NbBid = P->NbBid;
1580 for (i = 0; i < P->NbRays; ++i)
1581 Vector_Copy(P->Ray[i], C->Ray[i], P->Dimension+2);
1582 /* vertex 0 */
1583 value_set_si(C->Ray[P->NbRays][0], 1);
1584 value_set_si(C->Ray[P->NbRays][1+C->Dimension], 1);
1586 POL_SET(C, POL_VALID);
1587 if (POL_HAS(P, POL_INEQUALITIES))
1588 POL_SET(C, POL_INEQUALITIES);
1589 if (POL_HAS(P, POL_POINTS))
1590 POL_SET(C, POL_POINTS);
1591 if (POL_HAS(P, POL_VERTICES))
1592 POL_SET(C, POL_VERTICES);
1593 return C;
1596 /* Returns a (dim+nparam+1)x((dim-n)+nparam+1) matrix
1597 * mapping the transformed subspace back to the original space.
1598 * n is the number of equalities involving the variables
1599 * (i.e., not purely the parameters).
1600 * The remaining n coordinates in the transformed space would
1601 * have constant (parametric) values and are therefore not
1602 * included in the variables of the new space.
1604 Matrix *compress_variables(Matrix *Equalities, unsigned nparam)
1606 unsigned dim = (Equalities->NbColumns-2) - nparam;
1607 Matrix *M, *H, *Q, *U, *C, *ratH, *invH, *Ul, *T1, *T2, *T;
1608 Value mone;
1609 int n, i, j;
1610 int ok;
1612 for (n = 0; n < Equalities->NbRows; ++n)
1613 if (First_Non_Zero(Equalities->p[n]+1, dim) == -1)
1614 break;
1615 if (n == 0)
1616 return Identity(dim+nparam+1);
1617 value_init(mone);
1618 value_set_si(mone, -1);
1619 M = Matrix_Alloc(n, dim);
1620 C = Matrix_Alloc(n+1, nparam+1);
1621 for (i = 0; i < n; ++i) {
1622 Vector_Copy(Equalities->p[i]+1, M->p[i], dim);
1623 Vector_Scale(Equalities->p[i]+1+dim, C->p[i], mone, nparam+1);
1625 value_set_si(C->p[n][nparam], 1);
1626 left_hermite(M, &H, &Q, &U);
1627 Matrix_Free(M);
1628 Matrix_Free(Q);
1629 value_clear(mone);
1631 ratH = Matrix_Alloc(n+1, n+1);
1632 invH = Matrix_Alloc(n+1, n+1);
1633 for (i = 0; i < n; ++i)
1634 Vector_Copy(H->p[i], ratH->p[i], n);
1635 value_set_si(ratH->p[n][n], 1);
1636 ok = Matrix_Inverse(ratH, invH);
1637 assert(ok);
1638 Matrix_Free(H);
1639 Matrix_Free(ratH);
1640 T1 = Matrix_Alloc(n+1, nparam+1);
1641 Matrix_Product(invH, C, T1);
1642 Matrix_Free(C);
1643 Matrix_Free(invH);
1644 if (value_notone_p(T1->p[n][nparam])) {
1645 for (i = 0; i < n; ++i) {
1646 if (!mpz_divisible_p(T1->p[i][nparam], T1->p[n][nparam])) {
1647 Matrix_Free(T1);
1648 Matrix_Free(U);
1649 return NULL;
1651 /* compress_params should have taken care of this */
1652 for (j = 0; j < nparam; ++j)
1653 assert(mpz_divisible_p(T1->p[i][j], T1->p[n][nparam]));
1654 Vector_AntiScale(T1->p[i], T1->p[i], T1->p[n][nparam], nparam+1);
1656 value_set_si(T1->p[n][nparam], 1);
1658 Ul = Matrix_Alloc(dim+1, n+1);
1659 for (i = 0; i < dim; ++i)
1660 Vector_Copy(U->p[i], Ul->p[i], n);
1661 value_set_si(Ul->p[dim][n], 1);
1662 T2 = Matrix_Alloc(dim+1, nparam+1);
1663 Matrix_Product(Ul, T1, T2);
1664 Matrix_Free(Ul);
1665 Matrix_Free(T1);
1667 T = Matrix_Alloc(dim+nparam+1, (dim-n)+nparam+1);
1668 for (i = 0; i < dim; ++i) {
1669 Vector_Copy(U->p[i]+n, T->p[i], dim-n);
1670 Vector_Copy(T2->p[i], T->p[i]+dim-n, nparam+1);
1672 for (i = 0; i < nparam+1; ++i)
1673 value_set_si(T->p[dim+i][(dim-n)+i], 1);
1674 assert(value_one_p(T2->p[dim][nparam]));
1675 Matrix_Free(U);
1676 Matrix_Free(T2);
1678 return T;
1681 /* Computes the left inverse of an affine embedding M and, if Eq is not NULL,
1682 * the equalities that define the affine subspace onto which M maps
1683 * its argument.
1685 Matrix *left_inverse(Matrix *M, Matrix **Eq)
1687 int i, ok;
1688 Matrix *L, *H, *Q, *U, *ratH, *invH, *Ut, *inv;
1689 Vector *t;
1691 if (M->NbColumns == 1) {
1692 inv = Matrix_Alloc(1, M->NbRows);
1693 value_set_si(inv->p[0][M->NbRows-1], 1);
1694 if (Eq) {
1695 *Eq = Matrix_Alloc(M->NbRows-1, 1+(M->NbRows-1)+1);
1696 for (i = 0; i < M->NbRows-1; ++i) {
1697 value_oppose((*Eq)->p[i][1+i], M->p[M->NbRows-1][0]);
1698 value_assign((*Eq)->p[i][1+(M->NbRows-1)], M->p[i][0]);
1701 return inv;
1703 if (Eq)
1704 *Eq = NULL;
1705 L = Matrix_Alloc(M->NbRows-1, M->NbColumns-1);
1706 for (i = 0; i < L->NbRows; ++i)
1707 Vector_Copy(M->p[i], L->p[i], L->NbColumns);
1708 right_hermite(L, &H, &U, &Q);
1709 Matrix_Free(L);
1710 Matrix_Free(Q);
1711 t = Vector_Alloc(U->NbColumns);
1712 for (i = 0; i < U->NbColumns; ++i)
1713 value_oppose(t->p[i], M->p[i][M->NbColumns-1]);
1714 if (Eq) {
1715 *Eq = Matrix_Alloc(H->NbRows - H->NbColumns, 2 + U->NbColumns);
1716 for (i = 0; i < H->NbRows - H->NbColumns; ++i) {
1717 Vector_Copy(U->p[H->NbColumns+i], (*Eq)->p[i]+1, U->NbColumns);
1718 Inner_Product(U->p[H->NbColumns+i], t->p, U->NbColumns,
1719 (*Eq)->p[i]+1+U->NbColumns);
1722 ratH = Matrix_Alloc(H->NbColumns+1, H->NbColumns+1);
1723 invH = Matrix_Alloc(H->NbColumns+1, H->NbColumns+1);
1724 for (i = 0; i < H->NbColumns; ++i)
1725 Vector_Copy(H->p[i], ratH->p[i], H->NbColumns);
1726 value_set_si(ratH->p[ratH->NbRows-1][ratH->NbColumns-1], 1);
1727 Matrix_Free(H);
1728 ok = Matrix_Inverse(ratH, invH);
1729 assert(ok);
1730 Matrix_Free(ratH);
1731 Ut = Matrix_Alloc(invH->NbRows, U->NbColumns+1);
1732 for (i = 0; i < Ut->NbRows-1; ++i) {
1733 Vector_Copy(U->p[i], Ut->p[i], U->NbColumns);
1734 Inner_Product(U->p[i], t->p, U->NbColumns, &Ut->p[i][Ut->NbColumns-1]);
1736 Matrix_Free(U);
1737 Vector_Free(t);
1738 value_set_si(Ut->p[Ut->NbRows-1][Ut->NbColumns-1], 1);
1739 inv = Matrix_Alloc(invH->NbRows, Ut->NbColumns);
1740 Matrix_Product(invH, Ut, inv);
1741 Matrix_Free(Ut);
1742 Matrix_Free(invH);
1743 return inv;
1746 /* Check whether all rays are revlex positive in the parameters
1748 int Polyhedron_has_revlex_positive_rays(Polyhedron *P, unsigned nparam)
1750 int r;
1751 for (r = 0; r < P->NbRays; ++r) {
1752 int i;
1753 if (value_notzero_p(P->Ray[r][P->Dimension+1]))
1754 continue;
1755 for (i = P->Dimension-1; i >= P->Dimension-nparam; --i) {
1756 if (value_neg_p(P->Ray[r][i+1]))
1757 return 0;
1758 if (value_pos_p(P->Ray[r][i+1]))
1759 break;
1761 /* A ray independent of the parameters */
1762 if (i < P->Dimension-nparam)
1763 return 0;
1765 return 1;
1768 static Polyhedron *Recession_Cone(Polyhedron *P, unsigned nparam, unsigned MaxRays)
1770 int i;
1771 unsigned nvar = P->Dimension - nparam;
1772 Matrix *M = Matrix_Alloc(P->NbConstraints, 1 + nvar + 1);
1773 Polyhedron *R;
1774 for (i = 0; i < P->NbConstraints; ++i)
1775 Vector_Copy(P->Constraint[i], M->p[i], 1+nvar);
1776 R = Constraints2Polyhedron(M, MaxRays);
1777 Matrix_Free(M);
1778 return R;
1781 int Polyhedron_is_unbounded(Polyhedron *P, unsigned nparam, unsigned MaxRays)
1783 int i;
1784 int is_unbounded;
1785 Polyhedron *R = Recession_Cone(P, nparam, MaxRays);
1786 POL_ENSURE_VERTICES(R);
1787 if (R->NbBid == 0)
1788 for (i = 0; i < R->NbRays; ++i)
1789 if (value_zero_p(R->Ray[i][1+R->Dimension]))
1790 break;
1791 is_unbounded = R->NbBid > 0 || i < R->NbRays;
1792 Polyhedron_Free(R);
1793 return is_unbounded;
1796 static void SwapColumns(Value **V, int n, int i, int j)
1798 int r;
1800 for (r = 0; r < n; ++r)
1801 value_swap(V[r][i], V[r][j]);
1804 void Polyhedron_ExchangeColumns(Polyhedron *P, int Column1, int Column2)
1806 SwapColumns(P->Constraint, P->NbConstraints, Column1, Column2);
1807 SwapColumns(P->Ray, P->NbRays, Column1, Column2);
1808 if (P->NbEq) {
1809 Matrix M;
1810 Polyhedron_Matrix_View(P, &M, P->NbConstraints);
1811 Gauss(&M, P->NbEq, P->Dimension+1);
1815 /* perform transposition inline; assumes M is a square matrix */
1816 void Matrix_Transposition(Matrix *M)
1818 int i, j;
1820 assert(M->NbRows == M->NbColumns);
1821 for (i = 0; i < M->NbRows; ++i)
1822 for (j = i+1; j < M->NbColumns; ++j)
1823 value_swap(M->p[i][j], M->p[j][i]);
1826 /* Matrix "view" of first rows rows */
1827 void Polyhedron_Matrix_View(Polyhedron *P, Matrix *M, unsigned rows)
1829 M->NbRows = rows;
1830 M->NbColumns = P->Dimension+2;
1831 M->p_Init = P->p_Init;
1832 M->p = P->Constraint;