volume.c: drop redundant arguments to volume_simplex
[barvinok.git] / util.c
blob7eea4ca192158dbbce93a68b9e073178b3bebe8e
1 #include <stdlib.h>
2 #include <assert.h>
3 #include <barvinok/util.h>
4 #include <barvinok/options.h>
5 #include "config.h"
7 #ifndef HAVE_ENUMERATE4
8 #define Polyhedron_Enumerate(a,b,c,d) Polyhedron_Enumerate(a,b,c)
9 #endif
11 #define ALLOC(type) (type*)malloc(sizeof(type))
12 #define ALLOCN(type,n) (type*)malloc((n) * sizeof(type))
14 #ifdef __GNUC__
15 #define NALLOC(p,n) p = (typeof(p))malloc((n) * sizeof(*p))
16 #else
17 #define NALLOC(p,n) p = (void *)malloc((n) * sizeof(*p))
18 #endif
20 #ifndef HAVE_ENUMERATION_FREE
21 #define Enumeration_Free(en) /* just leak some memory */
22 #endif
24 void manual_count(Polyhedron *P, Value* result)
26 Polyhedron *U = Universe_Polyhedron(0);
27 Enumeration *en = Polyhedron_Enumerate(P,U,1024,NULL);
28 Value *v = compute_poly(en,NULL);
29 value_assign(*result, *v);
30 value_clear(*v);
31 free(v);
32 Enumeration_Free(en);
33 Polyhedron_Free(U);
36 #ifndef HAVE_ENUMERATION_FREE
37 #undef Enumeration_Free
38 #endif
40 #include <barvinok/evalue.h>
41 #include <barvinok/util.h>
42 #include <barvinok/barvinok.h>
44 /* Return random value between 0 and max-1 inclusive
46 int random_int(int max) {
47 return (int) (((double)(max))*rand()/(RAND_MAX+1.0));
50 Polyhedron *Polyhedron_Read(unsigned MaxRays)
52 int vertices = 0;
53 unsigned NbRows, NbColumns;
54 Matrix *M;
55 Polyhedron *P;
56 char s[128];
58 while (fgets(s, sizeof(s), stdin)) {
59 if (*s == '#')
60 continue;
61 if (strncasecmp(s, "vertices", sizeof("vertices")-1) == 0)
62 vertices = 1;
63 if (sscanf(s, "%u %u", &NbRows, &NbColumns) == 2)
64 break;
66 if (feof(stdin))
67 return NULL;
68 M = Matrix_Alloc(NbRows,NbColumns);
69 Matrix_Read_Input(M);
70 if (vertices)
71 P = Rays2Polyhedron(M, MaxRays);
72 else
73 P = Constraints2Polyhedron(M, MaxRays);
74 Matrix_Free(M);
75 return P;
78 /* Inplace polarization
80 void Polyhedron_Polarize(Polyhedron *P)
82 unsigned NbRows = P->NbConstraints + P->NbRays;
83 int i;
84 Value **q;
86 q = (Value **)malloc(NbRows * sizeof(Value *));
87 assert(q);
88 for (i = 0; i < P->NbRays; ++i)
89 q[i] = P->Ray[i];
90 for (; i < NbRows; ++i)
91 q[i] = P->Constraint[i-P->NbRays];
92 P->NbConstraints = NbRows - P->NbConstraints;
93 P->NbRays = NbRows - P->NbRays;
94 free(P->Constraint);
95 P->Constraint = q;
96 P->Ray = q + P->NbConstraints;
100 * Rather general polar
101 * We can optimize it significantly if we assume that
102 * P includes zero
104 * Also, we calculate the polar as defined in Schrijver
105 * The opposite should probably work as well and would
106 * eliminate the need for multiplying by -1
108 Polyhedron* Polyhedron_Polar(Polyhedron *P, unsigned NbMaxRays)
110 int i;
111 Value mone;
112 unsigned dim = P->Dimension + 2;
113 Matrix *M = Matrix_Alloc(P->NbRays, dim);
115 assert(M);
116 value_init(mone);
117 value_set_si(mone, -1);
118 for (i = 0; i < P->NbRays; ++i) {
119 Vector_Scale(P->Ray[i], M->p[i], mone, dim);
120 value_multiply(M->p[i][0], M->p[i][0], mone);
121 value_multiply(M->p[i][dim-1], M->p[i][dim-1], mone);
123 P = Constraints2Polyhedron(M, NbMaxRays);
124 assert(P);
125 Matrix_Free(M);
126 value_clear(mone);
127 return P;
131 * Returns the supporting cone of P at the vertex with index v
133 Polyhedron* supporting_cone(Polyhedron *P, int v)
135 Matrix *M;
136 Value tmp;
137 int i, n, j;
138 unsigned char *supporting = (unsigned char *)malloc(P->NbConstraints);
139 unsigned dim = P->Dimension + 2;
141 assert(v >=0 && v < P->NbRays);
142 assert(value_pos_p(P->Ray[v][dim-1]));
143 assert(supporting);
145 value_init(tmp);
146 for (i = 0, n = 0; i < P->NbConstraints; ++i) {
147 Inner_Product(P->Constraint[i] + 1, P->Ray[v] + 1, dim - 1, &tmp);
148 if ((supporting[i] = value_zero_p(tmp)))
149 ++n;
151 assert(n >= dim - 2);
152 value_clear(tmp);
153 M = Matrix_Alloc(n, dim);
154 assert(M);
155 for (i = 0, j = 0; i < P->NbConstraints; ++i)
156 if (supporting[i]) {
157 value_set_si(M->p[j][dim-1], 0);
158 Vector_Copy(P->Constraint[i], M->p[j++], dim-1);
160 free(supporting);
161 P = Constraints2Polyhedron(M, P->NbRays+1);
162 assert(P);
163 Matrix_Free(M);
164 return P;
167 void value_lcm(const Value i, const Value j, Value* lcm)
169 Value aux;
170 value_init(aux);
171 value_multiply(aux,i,j);
172 Gcd(i,j,lcm);
173 value_division(*lcm,aux,*lcm);
174 value_clear(aux);
177 unsigned char *supporting_constraints(Polyhedron *P, Param_Vertices *v, int *n)
179 Value lcm, tmp, tmp2;
180 unsigned dim = P->Dimension + 2;
181 unsigned nparam = v->Vertex->NbColumns - 2;
182 unsigned nvar = dim - nparam - 2;
183 unsigned char *supporting = (unsigned char *)malloc(P->NbConstraints);
184 int i, j;
185 Vector *row;
187 assert(supporting);
188 row = Vector_Alloc(nparam+1);
189 assert(row);
190 value_init(lcm);
191 value_init(tmp);
192 value_init(tmp2);
193 value_set_si(lcm, 1);
194 for (i = 0, *n = 0; i < P->NbConstraints; ++i) {
195 Vector_Set(row->p, 0, nparam+1);
196 for (j = 0 ; j < nvar; ++j) {
197 value_set_si(tmp, 1);
198 value_assign(tmp2, P->Constraint[i][j+1]);
199 if (value_ne(lcm, v->Vertex->p[j][nparam+1])) {
200 value_assign(tmp, lcm);
201 value_lcm(lcm, v->Vertex->p[j][nparam+1], &lcm);
202 value_division(tmp, lcm, tmp);
203 value_multiply(tmp2, tmp2, lcm);
204 value_division(tmp2, tmp2, v->Vertex->p[j][nparam+1]);
206 Vector_Combine(row->p, v->Vertex->p[j], row->p,
207 tmp, tmp2, nparam+1);
209 value_set_si(tmp, 1);
210 Vector_Combine(row->p, P->Constraint[i]+1+nvar, row->p, tmp, lcm, nparam+1);
211 for (j = 0; j < nparam+1; ++j)
212 if (value_notzero_p(row->p[j]))
213 break;
214 if ((supporting[i] = (j == nparam + 1)))
215 ++*n;
217 assert(*n >= nvar);
218 value_clear(tmp);
219 value_clear(tmp2);
220 value_clear(lcm);
221 Vector_Free(row);
223 return supporting;
226 Polyhedron* supporting_cone_p(Polyhedron *P, Param_Vertices *v)
228 Matrix *M;
229 unsigned dim = P->Dimension + 2;
230 unsigned nparam = v->Vertex->NbColumns - 2;
231 unsigned nvar = dim - nparam - 2;
232 int i, n, j;
233 unsigned char *supporting;
235 supporting = supporting_constraints(P, v, &n);
236 M = Matrix_Alloc(n, nvar+2);
237 assert(M);
238 for (i = 0, j = 0; i < P->NbConstraints; ++i)
239 if (supporting[i]) {
240 value_set_si(M->p[j][nvar+1], 0);
241 Vector_Copy(P->Constraint[i], M->p[j++], nvar+1);
243 free(supporting);
244 P = Constraints2Polyhedron(M, P->NbRays+1);
245 assert(P);
246 Matrix_Free(M);
247 return P;
250 Polyhedron* triangulate_cone(Polyhedron *P, unsigned NbMaxCons)
252 struct barvinok_options *options = barvinok_options_new_with_defaults();
253 options->MaxRays = NbMaxCons;
254 P = triangulate_cone_with_options(P, options);
255 barvinok_options_free(options);
256 return P;
259 Polyhedron* triangulate_cone_with_options(Polyhedron *P,
260 struct barvinok_options *options)
262 const static int MAX_TRY=10;
263 int i, j, r, n, t;
264 Value tmp;
265 unsigned dim = P->Dimension;
266 Matrix *M = Matrix_Alloc(P->NbRays+1, dim+3);
267 Matrix *M2, *M3;
268 Polyhedron *L, *R, *T;
269 assert(P->NbEq == 0);
271 L = NULL;
272 R = NULL;
273 value_init(tmp);
275 Vector_Set(M->p[0]+1, 0, dim+1);
276 value_set_si(M->p[0][0], 1);
277 value_set_si(M->p[0][dim+2], 1);
278 Vector_Set(M->p[P->NbRays]+1, 0, dim+2);
279 value_set_si(M->p[P->NbRays][0], 1);
280 value_set_si(M->p[P->NbRays][dim+1], 1);
282 for (i = 0, r = 1; i < P->NbRays; ++i) {
283 if (value_notzero_p(P->Ray[i][dim+1]))
284 continue;
285 Vector_Copy(P->Ray[i], M->p[r], dim+1);
286 value_set_si(M->p[r][dim+2], 0);
287 ++r;
290 M2 = Matrix_Alloc(dim+1, dim+2);
292 t = 0;
293 if (options->try_Delaunay_triangulation) {
294 /* Delaunay triangulation */
295 for (r = 1; r < P->NbRays; ++r) {
296 Inner_Product(M->p[r]+1, M->p[r]+1, dim, &tmp);
297 value_assign(M->p[r][dim+1], tmp);
299 M3 = Matrix_Copy(M);
300 L = Rays2Polyhedron(M3, options->MaxRays);
301 Matrix_Free(M3);
302 ++t;
303 } else {
304 try_again:
305 /* Usually R should still be 0 */
306 Domain_Free(R);
307 Polyhedron_Free(L);
308 for (r = 1; r < P->NbRays; ++r) {
309 value_set_si(M->p[r][dim+1], random_int((t+1)*dim*P->NbRays)+1);
311 M3 = Matrix_Copy(M);
312 L = Rays2Polyhedron(M3, options->MaxRays);
313 Matrix_Free(M3);
314 ++t;
316 assert(t <= MAX_TRY);
318 R = NULL;
319 n = 0;
321 POL_ENSURE_FACETS(L);
322 for (i = 0; i < L->NbConstraints; ++i) {
323 /* Ignore perpendicular facets, i.e., facets with 0 z-coordinate */
324 if (value_negz_p(L->Constraint[i][dim+1]))
325 continue;
326 if (value_notzero_p(L->Constraint[i][dim+2]))
327 continue;
328 for (j = 1, r = 1; j < M->NbRows; ++j) {
329 Inner_Product(M->p[j]+1, L->Constraint[i]+1, dim+1, &tmp);
330 if (value_notzero_p(tmp))
331 continue;
332 if (r > dim)
333 goto try_again;
334 Vector_Copy(M->p[j]+1, M2->p[r]+1, dim);
335 value_set_si(M2->p[r][0], 1);
336 value_set_si(M2->p[r][dim+1], 0);
337 ++r;
339 assert(r == dim+1);
340 Vector_Set(M2->p[0]+1, 0, dim);
341 value_set_si(M2->p[0][0], 1);
342 value_set_si(M2->p[0][dim+1], 1);
343 T = Rays2Polyhedron(M2, P->NbConstraints+1);
344 T->next = R;
345 R = T;
346 ++n;
348 Matrix_Free(M2);
350 Polyhedron_Free(L);
351 value_clear(tmp);
352 Matrix_Free(M);
354 return R;
357 void check_triangulization(Polyhedron *P, Polyhedron *T)
359 Polyhedron *C, *D, *E, *F, *G, *U;
360 for (C = T; C; C = C->next) {
361 if (C == T)
362 U = C;
363 else
364 U = DomainConvex(DomainUnion(U, C, 100), 100);
365 for (D = C->next; D; D = D->next) {
366 F = C->next;
367 G = D->next;
368 C->next = NULL;
369 D->next = NULL;
370 E = DomainIntersection(C, D, 600);
371 assert(E->NbRays == 0 || E->NbEq >= 1);
372 Polyhedron_Free(E);
373 C->next = F;
374 D->next = G;
377 assert(PolyhedronIncludes(U, P));
378 assert(PolyhedronIncludes(P, U));
381 /* Computes x, y and g such that g = gcd(a,b) and a*x+b*y = g */
382 void Extended_Euclid(Value a, Value b, Value *x, Value *y, Value *g)
384 Value c, d, e, f, tmp;
386 value_init(c);
387 value_init(d);
388 value_init(e);
389 value_init(f);
390 value_init(tmp);
391 value_absolute(c, a);
392 value_absolute(d, b);
393 value_set_si(e, 1);
394 value_set_si(f, 0);
395 while(value_pos_p(d)) {
396 value_division(tmp, c, d);
397 value_multiply(tmp, tmp, f);
398 value_subtract(e, e, tmp);
399 value_division(tmp, c, d);
400 value_multiply(tmp, tmp, d);
401 value_subtract(c, c, tmp);
402 value_swap(c, d);
403 value_swap(e, f);
405 value_assign(*g, c);
406 if (value_zero_p(a))
407 value_set_si(*x, 0);
408 else if (value_pos_p(a))
409 value_assign(*x, e);
410 else value_oppose(*x, e);
411 if (value_zero_p(b))
412 value_set_si(*y, 0);
413 else {
414 value_multiply(tmp, a, *x);
415 value_subtract(tmp, c, tmp);
416 value_division(*y, tmp, b);
418 value_clear(c);
419 value_clear(d);
420 value_clear(e);
421 value_clear(f);
422 value_clear(tmp);
425 Matrix * unimodular_complete(Vector *row)
427 Value g, b, c, old, tmp;
428 Matrix *m;
429 unsigned i, j;
431 value_init(b);
432 value_init(c);
433 value_init(g);
434 value_init(old);
435 value_init(tmp);
436 m = Matrix_Alloc(row->Size, row->Size);
437 for (j = 0; j < row->Size; ++j) {
438 value_assign(m->p[0][j], row->p[j]);
440 value_assign(g, row->p[0]);
441 for (i = 1; value_zero_p(g) && i < row->Size; ++i) {
442 for (j = 0; j < row->Size; ++j) {
443 if (j == i-1)
444 value_set_si(m->p[i][j], 1);
445 else
446 value_set_si(m->p[i][j], 0);
448 value_assign(g, row->p[i]);
450 for (; i < row->Size; ++i) {
451 value_assign(old, g);
452 Extended_Euclid(old, row->p[i], &c, &b, &g);
453 value_oppose(b, b);
454 for (j = 0; j < row->Size; ++j) {
455 if (j < i) {
456 value_multiply(tmp, row->p[j], b);
457 value_division(m->p[i][j], tmp, old);
458 } else if (j == i)
459 value_assign(m->p[i][j], c);
460 else
461 value_set_si(m->p[i][j], 0);
464 value_clear(b);
465 value_clear(c);
466 value_clear(g);
467 value_clear(old);
468 value_clear(tmp);
469 return m;
473 * Returns a full-dimensional polyhedron with the same number
474 * of integer points as P
476 Polyhedron *remove_equalities(Polyhedron *P)
478 Value g;
479 Vector *v;
480 Polyhedron *p = Polyhedron_Copy(P), *q;
481 unsigned dim = p->Dimension;
482 Matrix *m1, *m2;
483 int i;
485 value_init(g);
486 while (!emptyQ2(p) && p->NbEq > 0) {
487 assert(dim > 0);
488 Vector_Gcd(p->Constraint[0]+1, dim+1, &g);
489 Vector_AntiScale(p->Constraint[0]+1, p->Constraint[0]+1, g, dim+1);
490 Vector_Gcd(p->Constraint[0]+1, dim, &g);
491 if (value_notone_p(g) && value_notmone_p(g)) {
492 Polyhedron_Free(p);
493 p = Empty_Polyhedron(0);
494 break;
496 v = Vector_Alloc(dim);
497 Vector_Copy(p->Constraint[0]+1, v->p, dim);
498 m1 = unimodular_complete(v);
499 m2 = Matrix_Alloc(dim, dim+1);
500 for (i = 0; i < dim-1 ; ++i) {
501 Vector_Copy(m1->p[i+1], m2->p[i], dim);
502 value_set_si(m2->p[i][dim], 0);
504 Vector_Set(m2->p[dim-1], 0, dim);
505 value_set_si(m2->p[dim-1][dim], 1);
506 q = Polyhedron_Image(p, m2, p->NbConstraints+1+p->NbRays);
507 Vector_Free(v);
508 Matrix_Free(m1);
509 Matrix_Free(m2);
510 Polyhedron_Free(p);
511 p = q;
512 --dim;
514 value_clear(g);
515 return p;
519 * Returns a full-dimensional polyhedron with the same number
520 * of integer points as P
521 * nvar specifies the number of variables
522 * The remaining dimensions are assumed to be parameters
523 * Destroys P
524 * factor is NbEq x (nparam+2) matrix, containing stride constraints
525 * on the parameters; column nparam is the constant;
526 * column nparam+1 is the stride
528 * if factor is NULL, only remove equalities that don't affect
529 * the number of points
531 Polyhedron *remove_equalities_p(Polyhedron *P, unsigned nvar, Matrix **factor)
533 Value g;
534 Vector *v;
535 Polyhedron *p = P, *q;
536 unsigned dim = p->Dimension;
537 Matrix *m1, *m2, *f;
538 int i, j, skip;
540 value_init(g);
541 if (factor) {
542 f = Matrix_Alloc(p->NbEq, dim-nvar+2);
543 *factor = f;
545 j = 0;
546 skip = 0;
547 while (nvar > 0 && p->NbEq - skip > 0) {
548 assert(dim > 0);
550 while (skip < p->NbEq &&
551 First_Non_Zero(p->Constraint[skip]+1, nvar) == -1)
552 ++skip;
553 if (p->NbEq == skip)
554 break;
556 Vector_Gcd(p->Constraint[skip]+1, dim+1, &g);
557 Vector_AntiScale(p->Constraint[skip]+1, p->Constraint[skip]+1, g, dim+1);
558 Vector_Gcd(p->Constraint[skip]+1, nvar, &g);
559 if (!factor && value_notone_p(g) && value_notmone_p(g)) {
560 ++skip;
561 continue;
563 if (factor) {
564 Vector_Copy(p->Constraint[skip]+1+nvar, f->p[j], dim-nvar+1);
565 value_assign(f->p[j][dim-nvar+1], g);
567 v = Vector_Alloc(dim);
568 Vector_AntiScale(p->Constraint[skip]+1, v->p, g, nvar);
569 Vector_Set(v->p+nvar, 0, dim-nvar);
570 m1 = unimodular_complete(v);
571 m2 = Matrix_Alloc(dim, dim+1);
572 for (i = 0; i < dim-1 ; ++i) {
573 Vector_Copy(m1->p[i+1], m2->p[i], dim);
574 value_set_si(m2->p[i][dim], 0);
576 Vector_Set(m2->p[dim-1], 0, dim);
577 value_set_si(m2->p[dim-1][dim], 1);
578 q = Polyhedron_Image(p, m2, p->NbConstraints+1+p->NbRays);
579 Vector_Free(v);
580 Matrix_Free(m1);
581 Matrix_Free(m2);
582 Polyhedron_Free(p);
583 p = q;
584 --dim;
585 --nvar;
586 ++j;
588 value_clear(g);
589 return p;
592 void Line_Length(Polyhedron *P, Value *len)
594 Value tmp, pos, neg;
595 int p = 0, n = 0;
596 int i;
598 assert(P->Dimension == 1);
600 value_init(tmp);
601 value_init(pos);
602 value_init(neg);
604 for (i = 0; i < P->NbConstraints; ++i) {
605 value_oppose(tmp, P->Constraint[i][2]);
606 if (value_pos_p(P->Constraint[i][1])) {
607 mpz_cdiv_q(tmp, tmp, P->Constraint[i][1]);
608 if (!p || value_gt(tmp, pos))
609 value_assign(pos, tmp);
610 p = 1;
611 } else {
612 mpz_fdiv_q(tmp, tmp, P->Constraint[i][1]);
613 if (!n || value_lt(tmp, neg))
614 value_assign(neg, tmp);
615 n = 1;
617 if (n && p) {
618 value_subtract(tmp, neg, pos);
619 value_increment(*len, tmp);
620 } else
621 value_set_si(*len, -1);
624 value_clear(tmp);
625 value_clear(pos);
626 value_clear(neg);
630 * Factors the polyhedron P into polyhedra Q_i such that
631 * the number of integer points in P is equal to the product
632 * of the number of integer points in the individual Q_i
634 * If no factors can be found, NULL is returned.
635 * Otherwise, a linked list of the factors is returned.
637 * If there are factors and if T is not NULL, then a matrix will be
638 * returned through T expressing the old variables in terms of the
639 * new variables as they appear in the sequence of factors.
641 * The algorithm works by first computing the Hermite normal form
642 * and then grouping columns linked by one or more constraints together,
643 * where a constraints "links" two or more columns if the constraint
644 * has nonzero coefficients in the columns.
646 Polyhedron* Polyhedron_Factor(Polyhedron *P, unsigned nparam, Matrix **T,
647 unsigned NbMaxRays)
649 int i, j, k;
650 Matrix *M, *H, *Q, *U;
651 int *pos; /* for each column: row position of pivot */
652 int *group; /* group to which a column belongs */
653 int *cnt; /* number of columns in the group */
654 int *rowgroup; /* group to which a constraint belongs */
655 int nvar = P->Dimension - nparam;
656 Polyhedron *F = NULL;
658 if (nvar <= 1)
659 return NULL;
661 NALLOC(pos, nvar);
662 NALLOC(group, nvar);
663 NALLOC(cnt, nvar);
664 NALLOC(rowgroup, P->NbConstraints);
666 M = Matrix_Alloc(P->NbConstraints, nvar);
667 for (i = 0; i < P->NbConstraints; ++i)
668 Vector_Copy(P->Constraint[i]+1, M->p[i], nvar);
669 left_hermite(M, &H, &Q, &U);
670 Matrix_Free(M);
671 Matrix_Free(Q);
673 for (i = 0; i < P->NbConstraints; ++i)
674 rowgroup[i] = -1;
675 for (i = 0, j = 0; i < H->NbColumns; ++i) {
676 for ( ; j < H->NbRows; ++j)
677 if (value_notzero_p(H->p[j][i]))
678 break;
679 assert (j < H->NbRows);
680 pos[i] = j;
682 for (i = 0; i < nvar; ++i) {
683 group[i] = i;
684 cnt[i] = 1;
686 for (i = 0; i < H->NbColumns && cnt[0] < nvar; ++i) {
687 if (rowgroup[pos[i]] == -1)
688 rowgroup[pos[i]] = i;
689 for (j = pos[i]+1; j < H->NbRows; ++j) {
690 if (value_zero_p(H->p[j][i]))
691 continue;
692 if (rowgroup[j] != -1)
693 continue;
694 rowgroup[j] = group[i];
695 for (k = i+1; k < H->NbColumns && j >= pos[k]; ++k) {
696 int g = group[k];
697 while (cnt[g] == 0)
698 g = group[g];
699 group[k] = g;
700 if (group[k] != group[i] && value_notzero_p(H->p[j][k])) {
701 assert(cnt[group[k]] != 0);
702 assert(cnt[group[i]] != 0);
703 if (group[i] < group[k]) {
704 cnt[group[i]] += cnt[group[k]];
705 cnt[group[k]] = 0;
706 group[k] = group[i];
707 } else {
708 cnt[group[k]] += cnt[group[i]];
709 cnt[group[i]] = 0;
710 group[i] = group[k];
717 if (cnt[0] != nvar) {
718 /* Extract out pure context constraints separately */
719 Polyhedron **next = &F;
720 int tot_d = 0;
721 if (T)
722 *T = Matrix_Alloc(nvar, nvar);
723 for (i = nparam ? -1 : 0; i < nvar; ++i) {
724 int d;
726 if (i == -1) {
727 for (j = 0, k = 0; j < P->NbConstraints; ++j)
728 if (rowgroup[j] == -1) {
729 if (First_Non_Zero(P->Constraint[j]+1+nvar,
730 nparam) == -1)
731 rowgroup[j] = -2;
732 else
733 ++k;
735 if (k == 0)
736 continue;
737 d = 0;
738 } else {
739 if (cnt[i] == 0)
740 continue;
741 d = cnt[i];
742 for (j = 0, k = 0; j < P->NbConstraints; ++j)
743 if (rowgroup[j] >= 0 && group[rowgroup[j]] == i) {
744 rowgroup[j] = i;
745 ++k;
749 if (T)
750 for (j = 0; j < nvar; ++j) {
751 int l, m;
752 for (l = 0, m = 0; m < d; ++l) {
753 if (group[l] != i)
754 continue;
755 value_assign((*T)->p[j][tot_d+m++], U->p[j][l]);
759 M = Matrix_Alloc(k, d+nparam+2);
760 for (j = 0, k = 0; j < P->NbConstraints; ++j) {
761 int l, m;
762 if (rowgroup[j] != i)
763 continue;
764 value_assign(M->p[k][0], P->Constraint[j][0]);
765 for (l = 0, m = 0; m < d; ++l) {
766 if (group[l] != i)
767 continue;
768 value_assign(M->p[k][1+m++], H->p[j][l]);
770 Vector_Copy(P->Constraint[j]+1+nvar, M->p[k]+1+m, nparam+1);
771 ++k;
773 *next = Constraints2Polyhedron(M, NbMaxRays);
774 next = &(*next)->next;
775 Matrix_Free(M);
776 tot_d += d;
779 Matrix_Free(U);
780 Matrix_Free(H);
781 free(pos);
782 free(group);
783 free(cnt);
784 free(rowgroup);
785 return F;
789 * Project on final dim dimensions
791 Polyhedron* Polyhedron_Project(Polyhedron *P, int dim)
793 int i;
794 int remove = P->Dimension - dim;
795 Matrix *T;
796 Polyhedron *I;
798 if (P->Dimension == dim)
799 return Polyhedron_Copy(P);
801 T = Matrix_Alloc(dim+1, P->Dimension+1);
802 for (i = 0; i < dim+1; ++i)
803 value_set_si(T->p[i][i+remove], 1);
804 I = Polyhedron_Image(P, T, P->NbConstraints);
805 Matrix_Free(T);
806 return I;
809 /* Constructs a new constraint that ensures that
810 * the first constraint is (strictly) smaller than
811 * the second.
813 static void smaller_constraint(Value *a, Value *b, Value *c, int pos, int shift,
814 int len, int strict, Value *tmp)
816 value_oppose(*tmp, b[pos+1]);
817 value_set_si(c[0], 1);
818 Vector_Combine(a+1+shift, b+1+shift, c+1, *tmp, a[pos+1], len-shift-1);
819 if (strict)
820 value_decrement(c[len-shift-1], c[len-shift-1]);
821 ConstraintSimplify(c, c, len-shift, tmp);
824 struct section { Polyhedron * D; evalue E; };
826 evalue * ParamLine_Length_mod(Polyhedron *P, Polyhedron *C, int MaxRays)
828 unsigned dim = P->Dimension;
829 unsigned nvar = dim - C->Dimension;
830 int *pos;
831 int i, j, p, n, z;
832 struct section *s;
833 Matrix *M, *M2;
834 int nd = 0;
835 int k, l, k2, l2, q;
836 evalue *L, *U;
837 evalue *F;
838 Value g;
839 Polyhedron *T;
840 evalue mone;
842 assert(nvar == 1);
844 NALLOC(pos, P->NbConstraints);
845 value_init(g);
846 value_init(mone.d);
847 evalue_set_si(&mone, -1, 1);
849 for (i = 0, z = 0; i < P->NbConstraints; ++i)
850 if (value_zero_p(P->Constraint[i][1]))
851 ++z;
852 /* put those with positive coefficients first; number: p */
853 for (i = 0, p = 0, n = P->NbConstraints-z-1; i < P->NbConstraints; ++i)
854 if (value_pos_p(P->Constraint[i][1]))
855 pos[p++] = i;
856 else if (value_neg_p(P->Constraint[i][1]))
857 pos[n--] = i;
858 n = P->NbConstraints-z-p;
859 assert (p >= 1 && n >= 1);
860 s = (struct section *) malloc(p * n * sizeof(struct section));
861 M = Matrix_Alloc((p-1) + (n-1), dim-nvar+2);
862 for (k = 0; k < p; ++k) {
863 for (k2 = 0; k2 < p; ++k2) {
864 if (k2 == k)
865 continue;
866 q = k2 - (k2 > k);
867 smaller_constraint(
868 P->Constraint[pos[k]],
869 P->Constraint[pos[k2]],
870 M->p[q], 0, nvar, dim+2, k2 > k, &g);
872 for (l = p; l < p+n; ++l) {
873 for (l2 = p; l2 < p+n; ++l2) {
874 if (l2 == l)
875 continue;
876 q = l2-1 - (l2 > l);
877 smaller_constraint(
878 P->Constraint[pos[l2]],
879 P->Constraint[pos[l]],
880 M->p[q], 0, nvar, dim+2, l2 > l, &g);
882 M2 = Matrix_Copy(M);
883 T = Constraints2Polyhedron(M2, P->NbRays);
884 Matrix_Free(M2);
885 s[nd].D = DomainIntersection(T, C, MaxRays);
886 Domain_Free(T);
887 POL_ENSURE_VERTICES(s[nd].D);
888 if (emptyQ(s[nd].D)) {
889 Polyhedron_Free(s[nd].D);
890 continue;
892 L = bv_ceil3(P->Constraint[pos[k]]+1+nvar,
893 dim-nvar+1,
894 P->Constraint[pos[k]][0+1], s[nd].D);
895 U = bv_ceil3(P->Constraint[pos[l]]+1+nvar,
896 dim-nvar+1,
897 P->Constraint[pos[l]][0+1], s[nd].D);
898 eadd(L, U);
899 eadd(&mone, U);
900 emul(&mone, U);
901 s[nd].E = *U;
902 free_evalue_refs(L);
903 free(L);
904 free(U);
905 ++nd;
909 Matrix_Free(M);
911 F = ALLOC(evalue);
912 value_init(F->d);
913 value_set_si(F->d, 0);
914 F->x.p = new_enode(partition, 2*nd, dim-nvar);
915 for (k = 0; k < nd; ++k) {
916 EVALUE_SET_DOMAIN(F->x.p->arr[2*k], s[k].D);
917 value_clear(F->x.p->arr[2*k+1].d);
918 F->x.p->arr[2*k+1] = s[k].E;
920 free(s);
922 free_evalue_refs(&mone);
923 value_clear(g);
924 free(pos);
926 return F;
929 evalue* ParamLine_Length(Polyhedron *P, Polyhedron *C,
930 struct barvinok_options *options)
932 evalue* tmp;
933 tmp = ParamLine_Length_mod(P, C, options->MaxRays);
934 if (options->lookup_table) {
935 evalue_mod2table(tmp, C->Dimension);
936 reduce_evalue(tmp);
938 return tmp;
941 Bool isIdentity(Matrix *M)
943 unsigned i, j;
944 if (M->NbRows != M->NbColumns)
945 return False;
947 for (i = 0;i < M->NbRows; i ++)
948 for (j = 0; j < M->NbColumns; j ++)
949 if (i == j) {
950 if(value_notone_p(M->p[i][j]))
951 return False;
952 } else {
953 if(value_notzero_p(M->p[i][j]))
954 return False;
956 return True;
959 void Param_Polyhedron_Print(FILE* DST, Param_Polyhedron *PP, char **param_names)
961 Param_Domain *P;
962 Param_Vertices *V;
964 for(P=PP->D;P;P=P->next) {
966 /* prints current val. dom. */
967 fprintf(DST, "---------------------------------------\n");
968 fprintf(DST, "Domain :\n");
969 Print_Domain(DST, P->Domain, param_names);
971 /* scan the vertices */
972 fprintf(DST, "Vertices :\n");
973 FORALL_PVertex_in_ParamPolyhedron(V,P,PP) {
975 /* prints each vertex */
976 Print_Vertex(DST, V->Vertex, param_names);
977 printf( "\n" );
979 END_FORALL_PVertex_in_ParamPolyhedron;
983 void Enumeration_Print(FILE *Dst, Enumeration *en, char **params)
985 for (; en; en = en->next) {
986 Print_Domain(Dst, en->ValidityDomain, params);
987 print_evalue(Dst, &en->EP, params);
991 void Enumeration_Free(Enumeration *en)
993 Enumeration *ee;
995 while( en )
997 free_evalue_refs( &(en->EP) );
998 Domain_Free( en->ValidityDomain );
999 ee = en ->next;
1000 free( en );
1001 en = ee;
1005 void Enumeration_mod2table(Enumeration *en, unsigned nparam)
1007 for (; en; en = en->next) {
1008 evalue_mod2table(&en->EP, nparam);
1009 reduce_evalue(&en->EP);
1013 size_t Enumeration_size(Enumeration *en)
1015 size_t s = 0;
1017 for (; en; en = en->next) {
1018 s += domain_size(en->ValidityDomain);
1019 s += evalue_size(&en->EP);
1021 return s;
1024 void Free_ParamNames(char **params, int m)
1026 while (--m >= 0)
1027 free(params[m]);
1028 free(params);
1031 /* Check whether every set in D2 is included in some set of D1 */
1032 int DomainIncludes(Polyhedron *D1, Polyhedron *D2)
1034 for ( ; D2; D2 = D2->next) {
1035 Polyhedron *P1;
1036 for (P1 = D1; P1; P1 = P1->next)
1037 if (PolyhedronIncludes(P1, D2))
1038 break;
1039 if (!P1)
1040 return 0;
1042 return 1;
1045 int line_minmax(Polyhedron *I, Value *min, Value *max)
1047 int i;
1049 if (I->NbEq >= 1) {
1050 value_oppose(I->Constraint[0][2], I->Constraint[0][2]);
1051 /* There should never be a remainder here */
1052 if (value_pos_p(I->Constraint[0][1]))
1053 mpz_fdiv_q(*min, I->Constraint[0][2], I->Constraint[0][1]);
1054 else
1055 mpz_fdiv_q(*min, I->Constraint[0][2], I->Constraint[0][1]);
1056 value_assign(*max, *min);
1057 } else for (i = 0; i < I->NbConstraints; ++i) {
1058 if (value_zero_p(I->Constraint[i][1])) {
1059 Polyhedron_Free(I);
1060 return 0;
1063 value_oppose(I->Constraint[i][2], I->Constraint[i][2]);
1064 if (value_pos_p(I->Constraint[i][1]))
1065 mpz_cdiv_q(*min, I->Constraint[i][2], I->Constraint[i][1]);
1066 else
1067 mpz_fdiv_q(*max, I->Constraint[i][2], I->Constraint[i][1]);
1069 Polyhedron_Free(I);
1070 return 1;
1073 /**
1075 PROCEDURES TO COMPUTE ENUMERATION. recursive procedure, recurse for
1076 each imbriquation
1078 @param pos index position of current loop index (1..hdim-1)
1079 @param P loop domain
1080 @param context context values for fixed indices
1081 @param exist number of existential variables
1082 @return the number of integer points in this
1083 polyhedron
1086 void count_points_e (int pos, Polyhedron *P, int exist, int nparam,
1087 Value *context, Value *res)
1089 Value LB, UB, k, c;
1091 if (emptyQ(P)) {
1092 value_set_si(*res, 0);
1093 return;
1096 value_init(LB); value_init(UB); value_init(k);
1097 value_set_si(LB,0);
1098 value_set_si(UB,0);
1100 if (lower_upper_bounds(pos,P,context,&LB,&UB) !=0) {
1101 /* Problem if UB or LB is INFINITY */
1102 value_clear(LB); value_clear(UB); value_clear(k);
1103 if (pos > P->Dimension - nparam - exist)
1104 value_set_si(*res, 1);
1105 else
1106 value_set_si(*res, -1);
1107 return;
1110 #ifdef EDEBUG1
1111 if (!P->next) {
1112 int i;
1113 for (value_assign(k,LB); value_le(k,UB); value_increment(k,k)) {
1114 fprintf(stderr, "(");
1115 for (i=1; i<pos; i++) {
1116 value_print(stderr,P_VALUE_FMT,context[i]);
1117 fprintf(stderr,",");
1119 value_print(stderr,P_VALUE_FMT,k);
1120 fprintf(stderr,")\n");
1123 #endif
1125 value_set_si(context[pos],0);
1126 if (value_lt(UB,LB)) {
1127 value_clear(LB); value_clear(UB); value_clear(k);
1128 value_set_si(*res, 0);
1129 return;
1131 if (!P->next) {
1132 if (exist)
1133 value_set_si(*res, 1);
1134 else {
1135 value_subtract(k,UB,LB);
1136 value_add_int(k,k,1);
1137 value_assign(*res, k);
1139 value_clear(LB); value_clear(UB); value_clear(k);
1140 return;
1143 /*-----------------------------------------------------------------*/
1144 /* Optimization idea */
1145 /* If inner loops are not a function of k (the current index) */
1146 /* i.e. if P->Constraint[i][pos]==0 for all P following this and */
1147 /* for all i, */
1148 /* Then CNT = (UB-LB+1)*count_points(pos+1, P->next, context) */
1149 /* (skip the for loop) */
1150 /*-----------------------------------------------------------------*/
1152 value_init(c);
1153 value_set_si(*res, 0);
1154 for (value_assign(k,LB);value_le(k,UB);value_increment(k,k)) {
1155 /* Insert k in context */
1156 value_assign(context[pos],k);
1157 count_points_e(pos+1, P->next, exist, nparam, context, &c);
1158 if(value_notmone_p(c))
1159 value_addto(*res, *res, c);
1160 else {
1161 value_set_si(*res, -1);
1162 break;
1164 if (pos > P->Dimension - nparam - exist &&
1165 value_pos_p(*res))
1166 break;
1168 value_clear(c);
1170 #ifdef EDEBUG11
1171 fprintf(stderr,"%d\n",CNT);
1172 #endif
1174 /* Reset context */
1175 value_set_si(context[pos],0);
1176 value_clear(LB); value_clear(UB); value_clear(k);
1177 return;
1178 } /* count_points_e */
1180 int DomainContains(Polyhedron *P, Value *list_args, int len,
1181 unsigned MaxRays, int set)
1183 int i;
1184 Value m;
1186 if (P->Dimension == len)
1187 return in_domain(P, list_args);
1189 assert(set); // assume list_args is large enough
1190 assert((P->Dimension - len) % 2 == 0);
1191 value_init(m);
1192 for (i = 0; i < P->Dimension - len; i += 2) {
1193 int j, k;
1194 for (j = 0 ; j < P->NbEq; ++j)
1195 if (value_notzero_p(P->Constraint[j][1+len+i]))
1196 break;
1197 assert(j < P->NbEq);
1198 value_absolute(m, P->Constraint[j][1+len+i]);
1199 k = First_Non_Zero(P->Constraint[j]+1, len);
1200 assert(k != -1);
1201 assert(First_Non_Zero(P->Constraint[j]+1+k+1, len - k - 1) == -1);
1202 mpz_fdiv_q(list_args[len+i], list_args[k], m);
1203 mpz_fdiv_r(list_args[len+i+1], list_args[k], m);
1205 value_clear(m);
1207 return in_domain(P, list_args);
1210 Polyhedron *DomainConcat(Polyhedron *head, Polyhedron *tail)
1212 Polyhedron *S;
1213 if (!head)
1214 return tail;
1215 for (S = head; S->next; S = S->next)
1217 S->next = tail;
1218 return head;
1221 #ifndef HAVE_LEXSMALLER
1223 evalue *barvinok_lexsmaller_ev(Polyhedron *P, Polyhedron *D, unsigned dim,
1224 Polyhedron *C, unsigned MaxRays)
1226 assert(0);
1229 #else
1230 #include <polylib/ranking.h>
1232 evalue *barvinok_lexsmaller_ev(Polyhedron *P, Polyhedron *D, unsigned dim,
1233 Polyhedron *C, unsigned MaxRays)
1235 evalue *ranking;
1236 Polyhedron *RC, *RD, *Q;
1237 unsigned nparam = dim + C->Dimension;
1238 unsigned exist;
1239 Polyhedron *CA;
1241 RC = LexSmaller(P, D, dim, C, MaxRays);
1242 RD = RC->next;
1243 RC->next = NULL;
1245 exist = RD->Dimension - nparam - dim;
1246 CA = align_context(RC, RD->Dimension, MaxRays);
1247 Q = DomainIntersection(RD, CA, MaxRays);
1248 Polyhedron_Free(CA);
1249 Domain_Free(RD);
1250 Polyhedron_Free(RC);
1251 RD = Q;
1253 for (Q = RD; Q; Q = Q->next) {
1254 evalue *t;
1255 Polyhedron *next = Q->next;
1256 Q->next = 0;
1258 t = barvinok_enumerate_e(Q, exist, nparam, MaxRays);
1260 if (Q == RD)
1261 ranking = t;
1262 else {
1263 eadd(t, ranking);
1264 free_evalue_refs(t);
1265 free(t);
1268 Q->next = next;
1271 Domain_Free(RD);
1273 return ranking;
1276 Enumeration *barvinok_lexsmaller(Polyhedron *P, Polyhedron *D, unsigned dim,
1277 Polyhedron *C, unsigned MaxRays)
1279 evalue *EP = barvinok_lexsmaller_ev(P, D, dim, C, MaxRays);
1281 return partition2enumeration(EP);
1283 #endif
1285 /* "align" matrix to have nrows by inserting
1286 * the necessary number of rows and an equal number of columns in front
1288 Matrix *align_matrix(Matrix *M, int nrows)
1290 int i;
1291 int newrows = nrows - M->NbRows;
1292 Matrix *M2 = Matrix_Alloc(nrows, newrows + M->NbColumns);
1293 for (i = 0; i < newrows; ++i)
1294 value_set_si(M2->p[i][i], 1);
1295 for (i = 0; i < M->NbRows; ++i)
1296 Vector_Copy(M->p[i], M2->p[newrows+i]+newrows, M->NbColumns);
1297 return M2;
1300 static void print_varlist(FILE *out, int n, char **names)
1302 int i;
1303 fprintf(out, "[");
1304 for (i = 0; i < n; ++i) {
1305 if (i)
1306 fprintf(out, ",");
1307 fprintf(out, "%s", names[i]);
1309 fprintf(out, "]");
1312 static void print_term(FILE *out, Value v, int pos, int dim, int nparam,
1313 char **iter_names, char **param_names, int *first)
1315 if (value_zero_p(v)) {
1316 if (first && *first && pos >= dim + nparam)
1317 fprintf(out, "0");
1318 return;
1321 if (first) {
1322 if (!*first && value_pos_p(v))
1323 fprintf(out, "+");
1324 *first = 0;
1326 if (pos < dim + nparam) {
1327 if (value_mone_p(v))
1328 fprintf(out, "-");
1329 else if (!value_one_p(v))
1330 value_print(out, VALUE_FMT, v);
1331 if (pos < dim)
1332 fprintf(out, "%s", iter_names[pos]);
1333 else
1334 fprintf(out, "%s", param_names[pos-dim]);
1335 } else
1336 value_print(out, VALUE_FMT, v);
1339 char **util_generate_names(int n, char *prefix)
1341 int i;
1342 int len = (prefix ? strlen(prefix) : 0) + 10;
1343 char **names = ALLOCN(char*, n);
1344 if (!names) {
1345 fprintf(stderr, "ERROR: memory overflow.\n");
1346 exit(1);
1348 for (i = 0; i < n; ++i) {
1349 names[i] = ALLOCN(char, len);
1350 if (!names[i]) {
1351 fprintf(stderr, "ERROR: memory overflow.\n");
1352 exit(1);
1354 if (!prefix)
1355 snprintf(names[i], len, "%d", i);
1356 else
1357 snprintf(names[i], len, "%s%d", prefix, i);
1360 return names;
1363 void util_free_names(int n, char **names)
1365 int i;
1366 for (i = 0; i < n; ++i)
1367 free(names[i]);
1368 free(names);
1371 void Polyhedron_pprint(FILE *out, Polyhedron *P, int dim, int nparam,
1372 char **iter_names, char **param_names)
1374 int i, j;
1375 Value tmp;
1377 assert(dim + nparam == P->Dimension);
1379 value_init(tmp);
1381 fprintf(out, "{ ");
1382 if (nparam) {
1383 print_varlist(out, nparam, param_names);
1384 fprintf(out, " -> ");
1386 print_varlist(out, dim, iter_names);
1387 fprintf(out, " : ");
1389 if (emptyQ2(P))
1390 fprintf(out, "FALSE");
1391 else for (i = 0; i < P->NbConstraints; ++i) {
1392 int first = 1;
1393 int v = First_Non_Zero(P->Constraint[i]+1, P->Dimension);
1394 if (v == -1 && value_pos_p(P->Constraint[i][0]))
1395 continue;
1396 if (i)
1397 fprintf(out, " && ");
1398 if (v == -1 && value_notzero_p(P->Constraint[i][1+P->Dimension]))
1399 fprintf(out, "FALSE");
1400 else if (value_pos_p(P->Constraint[i][v+1])) {
1401 print_term(out, P->Constraint[i][v+1], v, dim, nparam,
1402 iter_names, param_names, NULL);
1403 if (value_zero_p(P->Constraint[i][0]))
1404 fprintf(out, " = ");
1405 else
1406 fprintf(out, " >= ");
1407 for (j = v+1; j <= dim+nparam; ++j) {
1408 value_oppose(tmp, P->Constraint[i][1+j]);
1409 print_term(out, tmp, j, dim, nparam,
1410 iter_names, param_names, &first);
1412 } else {
1413 value_oppose(tmp, P->Constraint[i][1+v]);
1414 print_term(out, tmp, v, dim, nparam,
1415 iter_names, param_names, NULL);
1416 fprintf(out, " <= ");
1417 for (j = v+1; j <= dim+nparam; ++j)
1418 print_term(out, P->Constraint[i][1+j], j, dim, nparam,
1419 iter_names, param_names, &first);
1423 fprintf(out, " }\n");
1425 value_clear(tmp);
1428 /* Construct a cone over P with P placed at x_d = 1, with
1429 * x_d the coordinate of an extra dimension
1431 * It's probably a mistake to depend so much on the internal
1432 * representation. We should probably simply compute the
1433 * vertices/facets first.
1435 Polyhedron *Cone_over_Polyhedron(Polyhedron *P)
1437 unsigned NbConstraints = 0;
1438 unsigned NbRays = 0;
1439 Polyhedron *C;
1440 int i;
1442 if (POL_HAS(P, POL_INEQUALITIES))
1443 NbConstraints = P->NbConstraints + 1;
1444 if (POL_HAS(P, POL_POINTS))
1445 NbRays = P->NbRays + 1;
1447 C = Polyhedron_Alloc(P->Dimension+1, NbConstraints, NbRays);
1448 if (POL_HAS(P, POL_INEQUALITIES)) {
1449 C->NbEq = P->NbEq;
1450 for (i = 0; i < P->NbConstraints; ++i)
1451 Vector_Copy(P->Constraint[i], C->Constraint[i], P->Dimension+2);
1452 /* n >= 0 */
1453 value_set_si(C->Constraint[P->NbConstraints][0], 1);
1454 value_set_si(C->Constraint[P->NbConstraints][1+P->Dimension], 1);
1456 if (POL_HAS(P, POL_POINTS)) {
1457 C->NbBid = P->NbBid;
1458 for (i = 0; i < P->NbRays; ++i)
1459 Vector_Copy(P->Ray[i], C->Ray[i], P->Dimension+2);
1460 /* vertex 0 */
1461 value_set_si(C->Ray[P->NbRays][0], 1);
1462 value_set_si(C->Ray[P->NbRays][1+C->Dimension], 1);
1464 POL_SET(C, POL_VALID);
1465 if (POL_HAS(P, POL_INEQUALITIES))
1466 POL_SET(C, POL_INEQUALITIES);
1467 if (POL_HAS(P, POL_POINTS))
1468 POL_SET(C, POL_POINTS);
1469 if (POL_HAS(P, POL_VERTICES))
1470 POL_SET(C, POL_VERTICES);
1471 return C;
1474 /* Returns a (dim+nparam+1)x((dim-n)+nparam+1) matrix
1475 * mapping the transformed subspace back to the original space.
1476 * n is the number of equalities involving the variables
1477 * (i.e., not purely the parameters).
1478 * The remaining n coordinates in the transformed space would
1479 * have constant (parametric) values and are therefore not
1480 * included in the variables of the new space.
1482 Matrix *compress_variables(Matrix *Equalities, unsigned nparam)
1484 unsigned dim = (Equalities->NbColumns-2) - nparam;
1485 Matrix *M, *H, *Q, *U, *C, *ratH, *invH, *Ul, *T1, *T2, *T;
1486 Value mone;
1487 int n, i, j;
1488 int ok;
1490 for (n = 0; n < Equalities->NbRows; ++n)
1491 if (First_Non_Zero(Equalities->p[n]+1, dim) == -1)
1492 break;
1493 if (n == 0)
1494 return Identity(dim+nparam+1);
1495 value_init(mone);
1496 value_set_si(mone, -1);
1497 M = Matrix_Alloc(n, dim);
1498 C = Matrix_Alloc(n+1, nparam+1);
1499 for (i = 0; i < n; ++i) {
1500 Vector_Copy(Equalities->p[i]+1, M->p[i], dim);
1501 Vector_Scale(Equalities->p[i]+1+dim, C->p[i], mone, nparam+1);
1503 value_set_si(C->p[n][nparam], 1);
1504 left_hermite(M, &H, &Q, &U);
1505 Matrix_Free(M);
1506 Matrix_Free(Q);
1507 value_clear(mone);
1509 ratH = Matrix_Alloc(n+1, n+1);
1510 invH = Matrix_Alloc(n+1, n+1);
1511 for (i = 0; i < n; ++i)
1512 Vector_Copy(H->p[i], ratH->p[i], n);
1513 value_set_si(ratH->p[n][n], 1);
1514 ok = Matrix_Inverse(ratH, invH);
1515 assert(ok);
1516 Matrix_Free(H);
1517 Matrix_Free(ratH);
1518 T1 = Matrix_Alloc(n+1, nparam+1);
1519 Matrix_Product(invH, C, T1);
1520 Matrix_Free(C);
1521 Matrix_Free(invH);
1522 if (value_notone_p(T1->p[n][nparam])) {
1523 for (i = 0; i < n; ++i) {
1524 if (!mpz_divisible_p(T1->p[i][nparam], T1->p[n][nparam])) {
1525 Matrix_Free(T1);
1526 Matrix_Free(U);
1527 return NULL;
1529 /* compress_params should have taken care of this */
1530 for (j = 0; j < nparam; ++j)
1531 assert(mpz_divisible_p(T1->p[i][j], T1->p[n][nparam]));
1532 Vector_AntiScale(T1->p[i], T1->p[i], T1->p[n][nparam], nparam+1);
1534 value_set_si(T1->p[n][nparam], 1);
1536 Ul = Matrix_Alloc(dim+1, n+1);
1537 for (i = 0; i < dim; ++i)
1538 Vector_Copy(U->p[i], Ul->p[i], n);
1539 value_set_si(Ul->p[dim][n], 1);
1540 T2 = Matrix_Alloc(dim+1, nparam+1);
1541 Matrix_Product(Ul, T1, T2);
1542 Matrix_Free(Ul);
1543 Matrix_Free(T1);
1545 T = Matrix_Alloc(dim+nparam+1, (dim-n)+nparam+1);
1546 for (i = 0; i < dim; ++i) {
1547 Vector_Copy(U->p[i]+n, T->p[i], dim-n);
1548 Vector_Copy(T2->p[i], T->p[i]+dim-n, nparam+1);
1550 for (i = 0; i < nparam+1; ++i)
1551 value_set_si(T->p[dim+i][(dim-n)+i], 1);
1552 assert(value_one_p(T2->p[dim][nparam]));
1553 Matrix_Free(U);
1554 Matrix_Free(T2);
1556 return T;
1559 Matrix *left_inverse(Matrix *M, Matrix **Eq)
1561 int i, ok;
1562 Matrix *L, *H, *Q, *U, *ratH, *invH, *Ut, *inv;
1563 Vector *t;
1565 if (Eq)
1566 *Eq = NULL;
1567 L = Matrix_Alloc(M->NbRows-1, M->NbColumns-1);
1568 for (i = 0; i < L->NbRows; ++i)
1569 Vector_Copy(M->p[i], L->p[i], L->NbColumns);
1570 right_hermite(L, &H, &U, &Q);
1571 Matrix_Free(L);
1572 Matrix_Free(Q);
1573 t = Vector_Alloc(U->NbColumns);
1574 for (i = 0; i < U->NbColumns; ++i)
1575 value_oppose(t->p[i], M->p[i][M->NbColumns-1]);
1576 if (Eq) {
1577 *Eq = Matrix_Alloc(H->NbRows - H->NbColumns, 2 + U->NbColumns);
1578 for (i = 0; i < H->NbRows - H->NbColumns; ++i) {
1579 Vector_Copy(U->p[H->NbColumns+i], (*Eq)->p[i]+1, U->NbColumns);
1580 Inner_Product(U->p[H->NbColumns+i], t->p, U->NbColumns,
1581 (*Eq)->p[i]+1+U->NbColumns);
1584 ratH = Matrix_Alloc(H->NbColumns+1, H->NbColumns+1);
1585 invH = Matrix_Alloc(H->NbColumns+1, H->NbColumns+1);
1586 for (i = 0; i < H->NbColumns; ++i)
1587 Vector_Copy(H->p[i], ratH->p[i], H->NbColumns);
1588 value_set_si(ratH->p[ratH->NbRows-1][ratH->NbColumns-1], 1);
1589 Matrix_Free(H);
1590 ok = Matrix_Inverse(ratH, invH);
1591 assert(ok);
1592 Matrix_Free(ratH);
1593 Ut = Matrix_Alloc(invH->NbRows, U->NbColumns+1);
1594 for (i = 0; i < Ut->NbRows-1; ++i) {
1595 Vector_Copy(U->p[i], Ut->p[i], U->NbColumns);
1596 Inner_Product(U->p[i], t->p, U->NbColumns, &Ut->p[i][Ut->NbColumns-1]);
1598 Matrix_Free(U);
1599 Vector_Free(t);
1600 value_set_si(Ut->p[Ut->NbRows-1][Ut->NbColumns-1], 1);
1601 inv = Matrix_Alloc(invH->NbRows, Ut->NbColumns);
1602 Matrix_Product(invH, Ut, inv);
1603 Matrix_Free(Ut);
1604 Matrix_Free(invH);
1605 return inv;
1608 /* Check whether all rays are revlex positive in the parameters
1610 int Polyhedron_has_revlex_positive_rays(Polyhedron *P, unsigned nparam)
1612 int r;
1613 for (r = 0; r < P->NbRays; ++r) {
1614 if (value_notzero_p(P->Ray[r][P->Dimension+1]))
1615 continue;
1616 int i;
1617 for (i = P->Dimension-1; i >= P->Dimension-nparam; --i) {
1618 if (value_neg_p(P->Ray[r][i+1]))
1619 return 0;
1620 if (value_pos_p(P->Ray[r][i+1]))
1621 break;
1623 /* A ray independent of the parameters */
1624 if (i < P->Dimension-nparam)
1625 return 0;
1627 return 1;
1630 static Polyhedron *Recession_Cone(Polyhedron *P, unsigned nparam, unsigned MaxRays)
1632 int i;
1633 unsigned nvar = P->Dimension - nparam;
1634 Matrix *M = Matrix_Alloc(P->NbConstraints, 1 + nvar + 1);
1635 for (i = 0; i < P->NbConstraints; ++i)
1636 Vector_Copy(P->Constraint[i], M->p[i], 1+nvar);
1637 Polyhedron *R = Constraints2Polyhedron(M, MaxRays);
1638 Matrix_Free(M);
1639 return R;
1642 int Polyhedron_is_unbounded(Polyhedron *P, unsigned nparam, unsigned MaxRays)
1644 int i;
1645 int is_unbounded;
1646 Polyhedron *R = Recession_Cone(P, nparam, MaxRays);
1647 POL_ENSURE_VERTICES(R);
1648 if (R->NbBid == 0)
1649 for (i = 0; i < R->NbRays; ++i)
1650 if (value_zero_p(R->Ray[i][1+R->Dimension]))
1651 break;
1652 is_unbounded = R->NbBid > 0 || i < R->NbRays;
1653 Polyhedron_Free(R);
1654 return is_unbounded;