1 #include <polylib/polylibgmp.h>
7 #ifndef HAVE_ENUMERATE4
8 #define Polyhedron_Enumerate(a,b,c,d) Polyhedron_Enumerate(a,b,c)
11 #define ALLOC(type) (type*)malloc(sizeof(type))
12 #define ALLOCN(type,n) (type*)malloc((n) * sizeof(type))
15 #define NALLOC(p,n) p = (typeof(p))malloc((n) * sizeof(*p))
17 #define NALLOC(p,n) p = (void *)malloc((n) * sizeof(*p))
20 #ifndef HAVE_ENUMERATION_FREE
21 #define Enumeration_Free(en) /* just leak some memory */
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
);
36 #ifndef HAVE_ENUMERATION_FREE
37 #undef Enumeration_Free
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 /* Inplace polarization
52 void Polyhedron_Polarize(Polyhedron
*P
)
54 unsigned NbRows
= P
->NbConstraints
+ P
->NbRays
;
58 q
= (Value
**)malloc(NbRows
* sizeof(Value
*));
60 for (i
= 0; i
< P
->NbRays
; ++i
)
62 for (; i
< NbRows
; ++i
)
63 q
[i
] = P
->Constraint
[i
-P
->NbRays
];
64 P
->NbConstraints
= NbRows
- P
->NbConstraints
;
65 P
->NbRays
= NbRows
- P
->NbRays
;
68 P
->Ray
= q
+ P
->NbConstraints
;
72 * Rather general polar
73 * We can optimize it significantly if we assume that
76 * Also, we calculate the polar as defined in Schrijver
77 * The opposite should probably work as well and would
78 * eliminate the need for multiplying by -1
80 Polyhedron
* Polyhedron_Polar(Polyhedron
*P
, unsigned NbMaxRays
)
84 unsigned dim
= P
->Dimension
+ 2;
85 Matrix
*M
= Matrix_Alloc(P
->NbRays
, dim
);
89 value_set_si(mone
, -1);
90 for (i
= 0; i
< P
->NbRays
; ++i
) {
91 Vector_Scale(P
->Ray
[i
], M
->p
[i
], mone
, dim
);
92 value_multiply(M
->p
[i
][0], M
->p
[i
][0], mone
);
93 value_multiply(M
->p
[i
][dim
-1], M
->p
[i
][dim
-1], mone
);
95 P
= Constraints2Polyhedron(M
, NbMaxRays
);
103 * Returns the supporting cone of P at the vertex with index v
105 Polyhedron
* supporting_cone(Polyhedron
*P
, int v
)
110 unsigned char *supporting
= (unsigned char *)malloc(P
->NbConstraints
);
111 unsigned dim
= P
->Dimension
+ 2;
113 assert(v
>=0 && v
< P
->NbRays
);
114 assert(value_pos_p(P
->Ray
[v
][dim
-1]));
118 for (i
= 0, n
= 0; i
< P
->NbConstraints
; ++i
) {
119 Inner_Product(P
->Constraint
[i
] + 1, P
->Ray
[v
] + 1, dim
- 1, &tmp
);
120 if ((supporting
[i
] = value_zero_p(tmp
)))
123 assert(n
>= dim
- 2);
125 M
= Matrix_Alloc(n
, dim
);
127 for (i
= 0, j
= 0; i
< P
->NbConstraints
; ++i
)
129 value_set_si(M
->p
[j
][dim
-1], 0);
130 Vector_Copy(P
->Constraint
[i
], M
->p
[j
++], dim
-1);
133 P
= Constraints2Polyhedron(M
, P
->NbRays
+1);
139 void value_lcm(Value i
, Value j
, Value
* lcm
)
143 value_multiply(aux
,i
,j
);
145 value_division(*lcm
,aux
,*lcm
);
149 Polyhedron
* supporting_cone_p(Polyhedron
*P
, Param_Vertices
*v
)
152 Value lcm
, tmp
, tmp2
;
153 unsigned char *supporting
= (unsigned char *)malloc(P
->NbConstraints
);
154 unsigned dim
= P
->Dimension
+ 2;
155 unsigned nparam
= v
->Vertex
->NbColumns
- 2;
156 unsigned nvar
= dim
- nparam
- 2;
161 row
= Vector_Alloc(nparam
+1);
166 value_set_si(lcm
, 1);
167 for (i
= 0, n
= 0; i
< P
->NbConstraints
; ++i
) {
168 Vector_Set(row
->p
, 0, nparam
+1);
169 for (j
= 0 ; j
< nvar
; ++j
) {
170 value_set_si(tmp
, 1);
171 value_assign(tmp2
, P
->Constraint
[i
][j
+1]);
172 if (value_ne(lcm
, v
->Vertex
->p
[j
][nparam
+1])) {
173 value_assign(tmp
, lcm
);
174 value_lcm(lcm
, v
->Vertex
->p
[j
][nparam
+1], &lcm
);
175 value_division(tmp
, lcm
, tmp
);
176 value_multiply(tmp2
, tmp2
, lcm
);
177 value_division(tmp2
, tmp2
, v
->Vertex
->p
[j
][nparam
+1]);
179 Vector_Combine(row
->p
, v
->Vertex
->p
[j
], row
->p
,
180 tmp
, tmp2
, nparam
+1);
182 value_set_si(tmp
, 1);
183 Vector_Combine(row
->p
, P
->Constraint
[i
]+1+nvar
, row
->p
, tmp
, lcm
, nparam
+1);
184 for (j
= 0; j
< nparam
+1; ++j
)
185 if (value_notzero_p(row
->p
[j
]))
187 if ((supporting
[i
] = (j
== nparam
+ 1)))
195 M
= Matrix_Alloc(n
, nvar
+2);
197 for (i
= 0, j
= 0; i
< P
->NbConstraints
; ++i
)
199 value_set_si(M
->p
[j
][nvar
+1], 0);
200 Vector_Copy(P
->Constraint
[i
], M
->p
[j
++], nvar
+1);
203 P
= Constraints2Polyhedron(M
, P
->NbRays
+1);
209 Polyhedron
* triangulate_cone(Polyhedron
*P
, unsigned NbMaxCons
)
211 const static int MAX_TRY
=10;
214 unsigned dim
= P
->Dimension
;
215 Matrix
*M
= Matrix_Alloc(P
->NbRays
+1, dim
+3);
217 Polyhedron
*L
, *R
, *T
;
218 assert(P
->NbEq
== 0);
223 Vector_Set(M
->p
[0]+1, 0, dim
+1);
224 value_set_si(M
->p
[0][0], 1);
225 value_set_si(M
->p
[0][dim
+2], 1);
226 Vector_Set(M
->p
[P
->NbRays
]+1, 0, dim
+2);
227 value_set_si(M
->p
[P
->NbRays
][0], 1);
228 value_set_si(M
->p
[P
->NbRays
][dim
+1], 1);
230 /* Delaunay triangulation */
231 for (i
= 0, r
= 1; i
< P
->NbRays
; ++i
) {
232 if (value_notzero_p(P
->Ray
[i
][dim
+1]))
234 Vector_Copy(P
->Ray
[i
], M
->p
[r
], dim
+1);
235 Inner_Product(M
->p
[r
]+1, M
->p
[r
]+1, dim
, &tmp
);
236 value_assign(M
->p
[r
][dim
+1], tmp
);
237 value_set_si(M
->p
[r
][dim
+2], 0);
242 L
= Rays2Polyhedron(M3
, NbMaxCons
);
245 M2
= Matrix_Alloc(dim
+1, dim
+2);
250 /* Usually R should still be 0 */
253 for (r
= 1; r
< P
->NbRays
; ++r
) {
254 value_set_si(M
->p
[r
][dim
+1], random_int((t
+1)*dim
)+1);
257 L
= Rays2Polyhedron(M3
, NbMaxCons
);
261 assert(t
<= MAX_TRY
);
266 for (i
= 0; i
< L
->NbConstraints
; ++i
) {
267 /* Ignore perpendicular facets, i.e., facets with 0 z-coordinate */
268 if (value_negz_p(L
->Constraint
[i
][dim
+1]))
270 if (value_notzero_p(L
->Constraint
[i
][dim
+2]))
272 for (j
= 1, r
= 1; j
< M
->NbRows
; ++j
) {
273 Inner_Product(M
->p
[j
]+1, L
->Constraint
[i
]+1, dim
+1, &tmp
);
274 if (value_notzero_p(tmp
))
278 Vector_Copy(M
->p
[j
]+1, M2
->p
[r
]+1, dim
);
279 value_set_si(M2
->p
[r
][0], 1);
280 value_set_si(M2
->p
[r
][dim
+1], 0);
284 Vector_Set(M2
->p
[0]+1, 0, dim
);
285 value_set_si(M2
->p
[0][0], 1);
286 value_set_si(M2
->p
[0][dim
+1], 1);
287 T
= Rays2Polyhedron(M2
, P
->NbConstraints
+1);
301 void check_triangulization(Polyhedron
*P
, Polyhedron
*T
)
303 Polyhedron
*C
, *D
, *E
, *F
, *G
, *U
;
304 for (C
= T
; C
; C
= C
->next
) {
308 U
= DomainConvex(DomainUnion(U
, C
, 100), 100);
309 for (D
= C
->next
; D
; D
= D
->next
) {
314 E
= DomainIntersection(C
, D
, 600);
315 assert(E
->NbRays
== 0 || E
->NbEq
>= 1);
321 assert(PolyhedronIncludes(U
, P
));
322 assert(PolyhedronIncludes(P
, U
));
325 /* Computes x, y and g such that g = gcd(a,b) and a*x+b*y = g */
326 void Extended_Euclid(Value a
, Value b
, Value
*x
, Value
*y
, Value
*g
)
328 Value c
, d
, e
, f
, tmp
;
335 value_absolute(c
, a
);
336 value_absolute(d
, b
);
339 while(value_pos_p(d
)) {
340 value_division(tmp
, c
, d
);
341 value_multiply(tmp
, tmp
, f
);
342 value_subtract(e
, e
, tmp
);
343 value_division(tmp
, c
, d
);
344 value_multiply(tmp
, tmp
, d
);
345 value_subtract(c
, c
, tmp
);
352 else if (value_pos_p(a
))
354 else value_oppose(*x
, e
);
358 value_multiply(tmp
, a
, *x
);
359 value_subtract(tmp
, c
, tmp
);
360 value_division(*y
, tmp
, b
);
369 Matrix
* unimodular_complete(Vector
*row
)
371 Value g
, b
, c
, old
, tmp
;
380 m
= Matrix_Alloc(row
->Size
, row
->Size
);
381 for (j
= 0; j
< row
->Size
; ++j
) {
382 value_assign(m
->p
[0][j
], row
->p
[j
]);
384 value_assign(g
, row
->p
[0]);
385 for (i
= 1; value_zero_p(g
) && i
< row
->Size
; ++i
) {
386 for (j
= 0; j
< row
->Size
; ++j
) {
388 value_set_si(m
->p
[i
][j
], 1);
390 value_set_si(m
->p
[i
][j
], 0);
392 value_assign(g
, row
->p
[i
]);
394 for (; i
< row
->Size
; ++i
) {
395 value_assign(old
, g
);
396 Extended_Euclid(old
, row
->p
[i
], &c
, &b
, &g
);
398 for (j
= 0; j
< row
->Size
; ++j
) {
400 value_multiply(tmp
, row
->p
[j
], b
);
401 value_division(m
->p
[i
][j
], tmp
, old
);
403 value_assign(m
->p
[i
][j
], c
);
405 value_set_si(m
->p
[i
][j
], 0);
417 * Returns a full-dimensional polyhedron with the same number
418 * of integer points as P
420 Polyhedron
*remove_equalities(Polyhedron
*P
)
424 Polyhedron
*p
= Polyhedron_Copy(P
), *q
;
425 unsigned dim
= p
->Dimension
;
430 while (!emptyQ2(p
) && p
->NbEq
> 0) {
432 Vector_Gcd(p
->Constraint
[0]+1, dim
+1, &g
);
433 Vector_AntiScale(p
->Constraint
[0]+1, p
->Constraint
[0]+1, g
, dim
+1);
434 Vector_Gcd(p
->Constraint
[0]+1, dim
, &g
);
435 if (value_notone_p(g
) && value_notmone_p(g
)) {
437 p
= Empty_Polyhedron(0);
440 v
= Vector_Alloc(dim
);
441 Vector_Copy(p
->Constraint
[0]+1, v
->p
, dim
);
442 m1
= unimodular_complete(v
);
443 m2
= Matrix_Alloc(dim
, dim
+1);
444 for (i
= 0; i
< dim
-1 ; ++i
) {
445 Vector_Copy(m1
->p
[i
+1], m2
->p
[i
], dim
);
446 value_set_si(m2
->p
[i
][dim
], 0);
448 Vector_Set(m2
->p
[dim
-1], 0, dim
);
449 value_set_si(m2
->p
[dim
-1][dim
], 1);
450 q
= Polyhedron_Image(p
, m2
, p
->NbConstraints
+1+p
->NbRays
);
463 * Returns a full-dimensional polyhedron with the same number
464 * of integer points as P
465 * nvar specifies the number of variables
466 * The remaining dimensions are assumed to be parameters
468 * factor is NbEq x (nparam+2) matrix, containing stride constraints
469 * on the parameters; column nparam is the constant;
470 * column nparam+1 is the stride
472 * if factor is NULL, only remove equalities that don't affect
473 * the number of points
475 Polyhedron
*remove_equalities_p(Polyhedron
*P
, unsigned nvar
, Matrix
**factor
)
479 Polyhedron
*p
= P
, *q
;
480 unsigned dim
= p
->Dimension
;
486 f
= Matrix_Alloc(p
->NbEq
, dim
-nvar
+2);
491 while (nvar
> 0 && p
->NbEq
- skip
> 0) {
494 while (skip
< p
->NbEq
&&
495 First_Non_Zero(p
->Constraint
[skip
]+1, nvar
) == -1)
500 Vector_Gcd(p
->Constraint
[skip
]+1, dim
+1, &g
);
501 Vector_AntiScale(p
->Constraint
[skip
]+1, p
->Constraint
[skip
]+1, g
, dim
+1);
502 Vector_Gcd(p
->Constraint
[skip
]+1, nvar
, &g
);
503 if (!factor
&& value_notone_p(g
) && value_notmone_p(g
)) {
508 Vector_Copy(p
->Constraint
[skip
]+1+nvar
, f
->p
[j
], dim
-nvar
+1);
509 value_assign(f
->p
[j
][dim
-nvar
+1], g
);
511 v
= Vector_Alloc(dim
);
512 Vector_AntiScale(p
->Constraint
[skip
]+1, v
->p
, g
, nvar
);
513 Vector_Set(v
->p
+nvar
, 0, dim
-nvar
);
514 m1
= unimodular_complete(v
);
515 m2
= Matrix_Alloc(dim
, dim
+1);
516 for (i
= 0; i
< dim
-1 ; ++i
) {
517 Vector_Copy(m1
->p
[i
+1], m2
->p
[i
], dim
);
518 value_set_si(m2
->p
[i
][dim
], 0);
520 Vector_Set(m2
->p
[dim
-1], 0, dim
);
521 value_set_si(m2
->p
[dim
-1][dim
], 1);
522 q
= Polyhedron_Image(p
, m2
, p
->NbConstraints
+1+p
->NbRays
);
536 void Line_Length(Polyhedron
*P
, Value
*len
)
542 assert(P
->Dimension
== 1);
548 for (i
= 0; i
< P
->NbConstraints
; ++i
) {
549 value_oppose(tmp
, P
->Constraint
[i
][2]);
550 if (value_pos_p(P
->Constraint
[i
][1])) {
551 mpz_cdiv_q(tmp
, tmp
, P
->Constraint
[i
][1]);
552 if (!p
|| value_gt(tmp
, pos
))
553 value_assign(pos
, tmp
);
556 mpz_fdiv_q(tmp
, tmp
, P
->Constraint
[i
][1]);
557 if (!n
|| value_lt(tmp
, neg
))
558 value_assign(neg
, tmp
);
562 value_subtract(tmp
, neg
, pos
);
563 value_increment(*len
, tmp
);
565 value_set_si(*len
, -1);
574 * Factors the polyhedron P into polyhedra Q_i such that
575 * the number of integer points in P is equal to the product
576 * of the number of integer points in the individual Q_i
578 * If no factors can be found, NULL is returned.
579 * Otherwise, a linked list of the factors is returned.
581 * The algorithm works by first computing the Hermite normal form
582 * and then grouping columns linked by one or more constraints together,
583 * where a constraints "links" two or more columns if the constraint
584 * has nonzero coefficients in the columns.
586 Polyhedron
* Polyhedron_Factor(Polyhedron
*P
, unsigned nparam
,
590 Matrix
*M
, *H
, *Q
, *U
;
591 int *pos
; /* for each column: row position of pivot */
592 int *group
; /* group to which a column belongs */
593 int *cnt
; /* number of columns in the group */
594 int *rowgroup
; /* group to which a constraint belongs */
595 int nvar
= P
->Dimension
- nparam
;
596 Polyhedron
*F
= NULL
;
604 NALLOC(rowgroup
, P
->NbConstraints
);
606 M
= Matrix_Alloc(P
->NbConstraints
, nvar
);
607 for (i
= 0; i
< P
->NbConstraints
; ++i
)
608 Vector_Copy(P
->Constraint
[i
]+1, M
->p
[i
], nvar
);
609 left_hermite(M
, &H
, &Q
, &U
);
614 for (i
= 0; i
< P
->NbConstraints
; ++i
)
616 for (i
= 0, j
= 0; i
< H
->NbColumns
; ++i
) {
617 for ( ; j
< H
->NbRows
; ++j
)
618 if (value_notzero_p(H
->p
[j
][i
]))
620 assert (j
< H
->NbRows
);
623 for (i
= 0; i
< nvar
; ++i
) {
627 for (i
= 0; i
< H
->NbColumns
&& cnt
[0] < nvar
; ++i
) {
628 if (rowgroup
[pos
[i
]] == -1)
629 rowgroup
[pos
[i
]] = i
;
630 for (j
= pos
[i
]+1; j
< H
->NbRows
; ++j
) {
631 if (value_zero_p(H
->p
[j
][i
]))
633 if (rowgroup
[j
] != -1)
635 rowgroup
[j
] = group
[i
];
636 for (k
= i
+1; k
< H
->NbColumns
&& j
>= pos
[k
]; ++k
) {
641 if (group
[k
] != group
[i
] && value_notzero_p(H
->p
[j
][k
])) {
642 assert(cnt
[group
[k
]] != 0);
643 assert(cnt
[group
[i
]] != 0);
644 if (group
[i
] < group
[k
]) {
645 cnt
[group
[i
]] += cnt
[group
[k
]];
649 cnt
[group
[k
]] += cnt
[group
[i
]];
658 if (cnt
[0] != nvar
) {
659 /* Extract out pure context constraints separately */
660 Polyhedron
**next
= &F
;
661 for (i
= nparam
? -1 : 0; i
< nvar
; ++i
) {
665 for (j
= 0, k
= 0; j
< P
->NbConstraints
; ++j
)
666 if (rowgroup
[j
] == -1) {
667 if (First_Non_Zero(P
->Constraint
[j
]+1+nvar
,
680 for (j
= 0, k
= 0; j
< P
->NbConstraints
; ++j
)
681 if (rowgroup
[j
] >= 0 && group
[rowgroup
[j
]] == i
) {
687 M
= Matrix_Alloc(k
, d
+nparam
+2);
688 for (j
= 0, k
= 0; j
< P
->NbConstraints
; ++j
) {
690 if (rowgroup
[j
] != i
)
692 value_assign(M
->p
[k
][0], P
->Constraint
[j
][0]);
693 for (l
= 0, m
= 0; m
< d
; ++l
) {
696 value_assign(M
->p
[k
][1+m
++], H
->p
[j
][l
]);
698 Vector_Copy(P
->Constraint
[j
]+1+nvar
, M
->p
[k
]+1+m
, nparam
+1);
701 *next
= Constraints2Polyhedron(M
, NbMaxRays
);
702 next
= &(*next
)->next
;
715 * Project on final dim dimensions
717 Polyhedron
* Polyhedron_Project(Polyhedron
*P
, int dim
)
720 int remove
= P
->Dimension
- dim
;
724 if (P
->Dimension
== dim
)
725 return Polyhedron_Copy(P
);
727 T
= Matrix_Alloc(dim
+1, P
->Dimension
+1);
728 for (i
= 0; i
< dim
+1; ++i
)
729 value_set_si(T
->p
[i
][i
+remove
], 1);
730 I
= Polyhedron_Image(P
, T
, P
->NbConstraints
);
735 /* Constructs a new constraint that ensures that
736 * the first constraint is (strictly) smaller than
739 static void smaller_constraint(Value
*a
, Value
*b
, Value
*c
, int pos
, int shift
,
740 int len
, int strict
, Value
*tmp
)
742 value_oppose(*tmp
, b
[pos
+1]);
743 value_set_si(c
[0], 1);
744 Vector_Combine(a
+1+shift
, b
+1+shift
, c
+1, *tmp
, a
[pos
+1], len
-shift
-1);
746 value_decrement(c
[len
-shift
-1], c
[len
-shift
-1]);
747 ConstraintSimplify(c
, c
, len
-shift
, tmp
);
750 struct section
{ Polyhedron
* D
; evalue E
; };
752 evalue
* ParamLine_Length_mod(Polyhedron
*P
, Polyhedron
*C
, int MaxRays
)
754 unsigned dim
= P
->Dimension
;
755 unsigned nvar
= dim
- C
->Dimension
;
770 NALLOC(pos
, P
->NbConstraints
);
773 evalue_set_si(&mone
, -1, 1);
775 for (i
= 0, z
= 0; i
< P
->NbConstraints
; ++i
)
776 if (value_zero_p(P
->Constraint
[i
][1]))
778 /* put those with positive coefficients first; number: p */
779 for (i
= 0, p
= 0, n
= P
->NbConstraints
-z
-1; i
< P
->NbConstraints
; ++i
)
780 if (value_pos_p(P
->Constraint
[i
][1]))
782 else if (value_neg_p(P
->Constraint
[i
][1]))
784 n
= P
->NbConstraints
-z
-p
;
785 assert (p
>= 1 && n
>= 1);
786 s
= (struct section
*) malloc(p
* n
* sizeof(struct section
));
787 M
= Matrix_Alloc((p
-1) + (n
-1), dim
-nvar
+2);
788 for (k
= 0; k
< p
; ++k
) {
789 for (k2
= 0; k2
< p
; ++k2
) {
794 P
->Constraint
[pos
[k
]],
795 P
->Constraint
[pos
[k2
]],
796 M
->p
[q
], 0, nvar
, dim
+2, k2
> k
, &g
);
798 for (l
= p
; l
< p
+n
; ++l
) {
799 for (l2
= p
; l2
< p
+n
; ++l2
) {
804 P
->Constraint
[pos
[l2
]],
805 P
->Constraint
[pos
[l
]],
806 M
->p
[q
], 0, nvar
, dim
+2, l2
> l
, &g
);
809 T
= Constraints2Polyhedron(M2
, P
->NbRays
);
811 s
[nd
].D
= DomainIntersection(T
, C
, MaxRays
);
813 POL_ENSURE_VERTICES(s
[nd
].D
);
814 if (emptyQ(s
[nd
].D
)) {
815 Polyhedron_Free(s
[nd
].D
);
818 L
= bv_ceil3(P
->Constraint
[pos
[k
]]+1+nvar
,
820 P
->Constraint
[pos
[k
]][0+1], s
[nd
].D
);
821 U
= bv_ceil3(P
->Constraint
[pos
[l
]]+1+nvar
,
823 P
->Constraint
[pos
[l
]][0+1], s
[nd
].D
);
839 value_set_si(F
->d
, 0);
840 F
->x
.p
= new_enode(partition
, 2*nd
, dim
-nvar
);
841 for (k
= 0; k
< nd
; ++k
) {
842 EVALUE_SET_DOMAIN(F
->x
.p
->arr
[2*k
], s
[k
].D
);
843 value_clear(F
->x
.p
->arr
[2*k
+1].d
);
844 F
->x
.p
->arr
[2*k
+1] = s
[k
].E
;
848 free_evalue_refs(&mone
);
856 evalue
* ParamLine_Length(Polyhedron
*P
, Polyhedron
*C
, unsigned MaxRays
)
858 return ParamLine_Length_mod(P
, C
, MaxRays
);
861 evalue
* ParamLine_Length(Polyhedron
*P
, Polyhedron
*C
, unsigned MaxRays
)
864 tmp
= ParamLine_Length_mod(P
, C
, MaxRays
);
865 evalue_mod2table(tmp
, C
->Dimension
);
871 Bool
isIdentity(Matrix
*M
)
874 if (M
->NbRows
!= M
->NbColumns
)
877 for (i
= 0;i
< M
->NbRows
; i
++)
878 for (j
= 0; j
< M
->NbColumns
; j
++)
880 if(value_notone_p(M
->p
[i
][j
]))
883 if(value_notzero_p(M
->p
[i
][j
]))
889 void Param_Polyhedron_Print(FILE* DST
, Param_Polyhedron
*PP
, char **param_names
)
894 for(P
=PP
->D
;P
;P
=P
->next
) {
896 /* prints current val. dom. */
897 printf( "---------------------------------------\n" );
898 printf( "Domain :\n");
899 Print_Domain( stdout
, P
->Domain
, param_names
);
901 /* scan the vertices */
902 printf( "Vertices :\n");
903 FORALL_PVertex_in_ParamPolyhedron(V
,P
,PP
) {
905 /* prints each vertex */
906 Print_Vertex( stdout
, V
->Vertex
, param_names
);
909 END_FORALL_PVertex_in_ParamPolyhedron
;
913 void Enumeration_Print(FILE *Dst
, Enumeration
*en
, char **params
)
915 for (; en
; en
= en
->next
) {
916 Print_Domain(Dst
, en
->ValidityDomain
, params
);
917 print_evalue(Dst
, &en
->EP
, params
);
921 void Enumeration_Free(Enumeration
*en
)
927 free_evalue_refs( &(en
->EP
) );
928 Domain_Free( en
->ValidityDomain
);
935 void Enumeration_mod2table(Enumeration
*en
, unsigned nparam
)
937 for (; en
; en
= en
->next
) {
938 evalue_mod2table(&en
->EP
, nparam
);
939 reduce_evalue(&en
->EP
);
943 size_t Enumeration_size(Enumeration
*en
)
947 for (; en
; en
= en
->next
) {
948 s
+= domain_size(en
->ValidityDomain
);
949 s
+= evalue_size(&en
->EP
);
954 void Free_ParamNames(char **params
, int m
)
961 int DomainIncludes(Polyhedron
*Pol1
, Polyhedron
*Pol2
)
964 for ( ; Pol1
; Pol1
= Pol1
->next
) {
965 for (P2
= Pol2
; P2
; P2
= P2
->next
)
966 if (!PolyhedronIncludes(Pol1
, P2
))
974 int line_minmax(Polyhedron
*I
, Value
*min
, Value
*max
)
979 value_oppose(I
->Constraint
[0][2], I
->Constraint
[0][2]);
980 /* There should never be a remainder here */
981 if (value_pos_p(I
->Constraint
[0][1]))
982 mpz_fdiv_q(*min
, I
->Constraint
[0][2], I
->Constraint
[0][1]);
984 mpz_fdiv_q(*min
, I
->Constraint
[0][2], I
->Constraint
[0][1]);
985 value_assign(*max
, *min
);
986 } else for (i
= 0; i
< I
->NbConstraints
; ++i
) {
987 if (value_zero_p(I
->Constraint
[i
][1])) {
992 value_oppose(I
->Constraint
[i
][2], I
->Constraint
[i
][2]);
993 if (value_pos_p(I
->Constraint
[i
][1]))
994 mpz_cdiv_q(*min
, I
->Constraint
[i
][2], I
->Constraint
[i
][1]);
996 mpz_fdiv_q(*max
, I
->Constraint
[i
][2], I
->Constraint
[i
][1]);
1004 PROCEDURES TO COMPUTE ENUMERATION. recursive procedure, recurse for
1007 @param pos index position of current loop index (1..hdim-1)
1008 @param P loop domain
1009 @param context context values for fixed indices
1010 @param exist number of existential variables
1011 @return the number of integer points in this
1015 void count_points_e (int pos
, Polyhedron
*P
, int exist
, int nparam
,
1016 Value
*context
, Value
*res
)
1021 value_set_si(*res
, 0);
1025 value_init(LB
); value_init(UB
); value_init(k
);
1029 if (lower_upper_bounds(pos
,P
,context
,&LB
,&UB
) !=0) {
1030 /* Problem if UB or LB is INFINITY */
1031 value_clear(LB
); value_clear(UB
); value_clear(k
);
1032 if (pos
> P
->Dimension
- nparam
- exist
)
1033 value_set_si(*res
, 1);
1035 value_set_si(*res
, -1);
1042 for (value_assign(k
,LB
); value_le(k
,UB
); value_increment(k
,k
)) {
1043 fprintf(stderr
, "(");
1044 for (i
=1; i
<pos
; i
++) {
1045 value_print(stderr
,P_VALUE_FMT
,context
[i
]);
1046 fprintf(stderr
,",");
1048 value_print(stderr
,P_VALUE_FMT
,k
);
1049 fprintf(stderr
,")\n");
1054 value_set_si(context
[pos
],0);
1055 if (value_lt(UB
,LB
)) {
1056 value_clear(LB
); value_clear(UB
); value_clear(k
);
1057 value_set_si(*res
, 0);
1062 value_set_si(*res
, 1);
1064 value_subtract(k
,UB
,LB
);
1065 value_add_int(k
,k
,1);
1066 value_assign(*res
, k
);
1068 value_clear(LB
); value_clear(UB
); value_clear(k
);
1072 /*-----------------------------------------------------------------*/
1073 /* Optimization idea */
1074 /* If inner loops are not a function of k (the current index) */
1075 /* i.e. if P->Constraint[i][pos]==0 for all P following this and */
1077 /* Then CNT = (UB-LB+1)*count_points(pos+1, P->next, context) */
1078 /* (skip the for loop) */
1079 /*-----------------------------------------------------------------*/
1082 value_set_si(*res
, 0);
1083 for (value_assign(k
,LB
);value_le(k
,UB
);value_increment(k
,k
)) {
1084 /* Insert k in context */
1085 value_assign(context
[pos
],k
);
1086 count_points_e(pos
+1, P
->next
, exist
, nparam
, context
, &c
);
1087 if(value_notmone_p(c
))
1088 value_addto(*res
, *res
, c
);
1090 value_set_si(*res
, -1);
1093 if (pos
> P
->Dimension
- nparam
- exist
&&
1100 fprintf(stderr
,"%d\n",CNT
);
1104 value_set_si(context
[pos
],0);
1105 value_clear(LB
); value_clear(UB
); value_clear(k
);
1107 } /* count_points_e */
1109 int DomainContains(Polyhedron
*P
, Value
*list_args
, int len
,
1110 unsigned MaxRays
, int set
)
1115 if (P
->Dimension
== len
)
1116 return in_domain(P
, list_args
);
1118 assert(set
); // assume list_args is large enough
1119 assert((P
->Dimension
- len
) % 2 == 0);
1121 for (i
= 0; i
< P
->Dimension
- len
; i
+= 2) {
1123 for (j
= 0 ; j
< P
->NbEq
; ++j
)
1124 if (value_notzero_p(P
->Constraint
[j
][1+len
+i
]))
1126 assert(j
< P
->NbEq
);
1127 value_absolute(m
, P
->Constraint
[j
][1+len
+i
]);
1128 k
= First_Non_Zero(P
->Constraint
[j
]+1, len
);
1130 assert(First_Non_Zero(P
->Constraint
[j
]+1+k
+1, len
- k
- 1) == -1);
1131 mpz_fdiv_q(list_args
[len
+i
], list_args
[k
], m
);
1132 mpz_fdiv_r(list_args
[len
+i
+1], list_args
[k
], m
);
1136 return in_domain(P
, list_args
);
1139 Polyhedron
*DomainConcat(Polyhedron
*head
, Polyhedron
*tail
)
1144 for (S
= head
; S
->next
; S
= S
->next
)
1150 #ifndef HAVE_LEXSMALLER
1152 evalue
*barvinok_lexsmaller_ev(Polyhedron
*P
, Polyhedron
*D
, unsigned dim
,
1153 Polyhedron
*C
, unsigned MaxRays
)
1159 #include <polylib/ranking.h>
1161 evalue
*barvinok_lexsmaller_ev(Polyhedron
*P
, Polyhedron
*D
, unsigned dim
,
1162 Polyhedron
*C
, unsigned MaxRays
)
1165 Polyhedron
*RC
, *RD
, *Q
;
1166 unsigned nparam
= dim
+ C
->Dimension
;
1170 RC
= LexSmaller(P
, D
, dim
, C
, MaxRays
);
1174 exist
= RD
->Dimension
- nparam
- dim
;
1175 CA
= align_context(RC
, RD
->Dimension
, MaxRays
);
1176 Q
= DomainIntersection(RD
, CA
, MaxRays
);
1177 Polyhedron_Free(CA
);
1179 Polyhedron_Free(RC
);
1182 for (Q
= RD
; Q
; Q
= Q
->next
) {
1184 Polyhedron
*next
= Q
->next
;
1187 t
= barvinok_enumerate_e(Q
, exist
, nparam
, MaxRays
);
1193 free_evalue_refs(t
);
1205 Enumeration
*barvinok_lexsmaller(Polyhedron
*P
, Polyhedron
*D
, unsigned dim
,
1206 Polyhedron
*C
, unsigned MaxRays
)
1208 evalue
*EP
= barvinok_lexsmaller_ev(P
, D
, dim
, C
, MaxRays
);
1210 return partition2enumeration(EP
);
1214 /* "align" matrix to have nrows by inserting
1215 * the necessary number of rows and an equal number of columns in front
1217 Matrix
*align_matrix(Matrix
*M
, int nrows
)
1220 int newrows
= nrows
- M
->NbRows
;
1221 Matrix
*M2
= Matrix_Alloc(nrows
, newrows
+ M
->NbColumns
);
1222 for (i
= 0; i
< newrows
; ++i
)
1223 value_set_si(M2
->p
[i
][i
], 1);
1224 for (i
= 0; i
< M
->NbRows
; ++i
)
1225 Vector_Copy(M
->p
[i
], M2
->p
[newrows
+i
]+newrows
, M
->NbColumns
);
1229 static void print_varlist(FILE *out
, int n
, char **names
)
1233 for (i
= 0; i
< n
; ++i
) {
1236 fprintf(out
, "%s", names
[i
]);
1241 static void print_term(FILE *out
, Value v
, int pos
, int dim
, int nparam
,
1242 char **iter_names
, char **param_names
, int *first
)
1244 if (value_zero_p(v
)) {
1245 if (first
&& *first
&& pos
>= dim
+ nparam
)
1251 if (!*first
&& value_pos_p(v
))
1255 if (pos
< dim
+ nparam
) {
1256 if (value_mone_p(v
))
1258 else if (!value_one_p(v
))
1259 value_print(out
, VALUE_FMT
, v
);
1261 fprintf(out
, "%s", iter_names
[pos
]);
1263 fprintf(out
, "%s", param_names
[pos
-dim
]);
1265 value_print(out
, VALUE_FMT
, v
);
1268 char **util_generate_names(int n
, char *prefix
)
1271 int len
= (prefix
? strlen(prefix
) : 0) + 10;
1272 char **names
= ALLOCN(char*, n
);
1274 fprintf(stderr
, "ERROR: memory overflow.\n");
1277 for (i
= 0; i
< n
; ++i
) {
1278 names
[i
] = ALLOCN(char, len
);
1280 fprintf(stderr
, "ERROR: memory overflow.\n");
1284 snprintf(names
[i
], len
, "%d", i
);
1286 snprintf(names
[i
], len
, "%s%d", prefix
, i
);
1292 void util_free_names(int n
, char **names
)
1295 for (i
= 0; i
< n
; ++i
)
1300 void Polyhedron_pprint(FILE *out
, Polyhedron
*P
, int dim
, int nparam
,
1301 char **iter_names
, char **param_names
)
1306 assert(dim
+ nparam
== P
->Dimension
);
1312 print_varlist(out
, nparam
, param_names
);
1313 fprintf(out
, " -> ");
1315 print_varlist(out
, dim
, iter_names
);
1316 fprintf(out
, " : ");
1319 fprintf(out
, "FALSE");
1320 else for (i
= 0; i
< P
->NbConstraints
; ++i
) {
1322 int v
= First_Non_Zero(P
->Constraint
[i
]+1, P
->Dimension
);
1323 if (v
== -1 && value_pos_p(P
->Constraint
[i
][0]))
1326 fprintf(out
, " && ");
1327 if (v
== -1 && value_notzero_p(P
->Constraint
[i
][1+P
->Dimension
]))
1328 fprintf(out
, "FALSE");
1329 else if (value_pos_p(P
->Constraint
[i
][v
+1])) {
1330 print_term(out
, P
->Constraint
[i
][v
+1], v
, dim
, nparam
,
1331 iter_names
, param_names
, NULL
);
1332 if (value_zero_p(P
->Constraint
[i
][0]))
1333 fprintf(out
, " = ");
1335 fprintf(out
, " >= ");
1336 for (j
= v
+1; j
<= dim
+nparam
; ++j
) {
1337 value_oppose(tmp
, P
->Constraint
[i
][1+j
]);
1338 print_term(out
, tmp
, j
, dim
, nparam
,
1339 iter_names
, param_names
, &first
);
1342 value_oppose(tmp
, P
->Constraint
[i
][1+v
]);
1343 print_term(out
, tmp
, v
, dim
, nparam
,
1344 iter_names
, param_names
, NULL
);
1345 fprintf(out
, " <= ");
1346 for (j
= v
+1; j
<= dim
+nparam
; ++j
)
1347 print_term(out
, P
->Constraint
[i
][1+j
], j
, dim
, nparam
,
1348 iter_names
, param_names
, &first
);
1352 fprintf(out
, " }\n");
1357 /* Construct a cone over P with P placed at x_d = 1, with
1358 * x_d the coordinate of an extra dimension
1360 * It's probably a mistake to depend so much on the internal
1361 * representation. We should probably simply compute the
1362 * vertices/facets first.
1364 Polyhedron
*Cone_over_Polyhedron(Polyhedron
*P
)
1366 unsigned NbConstraints
= 0;
1367 unsigned NbRays
= 0;
1371 if (POL_HAS(P
, POL_INEQUALITIES
))
1372 NbConstraints
= P
->NbConstraints
+ 1;
1373 if (POL_HAS(P
, POL_POINTS
))
1374 NbRays
= P
->NbRays
+ 1;
1376 C
= Polyhedron_Alloc(P
->Dimension
+1, NbConstraints
, NbRays
);
1377 if (POL_HAS(P
, POL_INEQUALITIES
)) {
1379 for (i
= 0; i
< P
->NbConstraints
; ++i
)
1380 Vector_Copy(P
->Constraint
[i
], C
->Constraint
[i
], P
->Dimension
+2);
1382 value_set_si(C
->Constraint
[P
->NbConstraints
][0], 1);
1383 value_set_si(C
->Constraint
[P
->NbConstraints
][1+P
->Dimension
], 1);
1385 if (POL_HAS(P
, POL_POINTS
)) {
1386 C
->NbBid
= P
->NbBid
;
1387 for (i
= 0; i
< P
->NbRays
; ++i
)
1388 Vector_Copy(P
->Ray
[i
], C
->Ray
[i
], P
->Dimension
+2);
1390 value_set_si(C
->Ray
[P
->NbRays
][0], 1);
1391 value_set_si(C
->Ray
[P
->NbRays
][1+C
->Dimension
], 1);
1393 POL_SET(C
, POL_VALID
);
1394 if (POL_HAS(P
, POL_INEQUALITIES
))
1395 POL_SET(C
, POL_INEQUALITIES
);
1396 if (POL_HAS(P
, POL_POINTS
))
1397 POL_SET(C
, POL_POINTS
);
1398 if (POL_HAS(P
, POL_VERTICES
))
1399 POL_SET(C
, POL_VERTICES
);
1403 Matrix
*compress_variables(Matrix
*Equalities
, unsigned nparam
)
1405 unsigned dim
= (Equalities
->NbColumns
-2) - nparam
;
1406 Matrix
*M
, *H
, *Q
, *U
, *C
, *ratH
, *invH
, *Ul
, *T1
, *T2
, *T
;
1410 for (n
= 0; n
< Equalities
->NbRows
; ++n
)
1411 if (First_Non_Zero(Equalities
->p
[n
]+1, dim
) == -1)
1414 return Identity(dim
+nparam
+1);
1416 value_set_si(mone
, -1);
1417 M
= Matrix_Alloc(n
, dim
);
1418 C
= Matrix_Alloc(n
+1, nparam
+1);
1419 for (i
= 0; i
< n
; ++i
) {
1420 Vector_Copy(Equalities
->p
[i
]+1, M
->p
[i
], dim
);
1421 Vector_Scale(Equalities
->p
[i
]+1+dim
, C
->p
[i
], mone
, nparam
+1);
1423 value_set_si(C
->p
[n
][nparam
], 1);
1424 left_hermite(M
, &H
, &Q
, &U
);
1429 /* we will need to treat scalings later */
1431 for (i
= 0; i
< n
; ++i
)
1432 assert(value_one_p(H
->p
[i
][i
]));
1434 ratH
= Matrix_Alloc(n
+1, n
+1);
1435 invH
= Matrix_Alloc(n
+1, n
+1);
1436 for (i
= 0; i
< n
; ++i
)
1437 Vector_Copy(H
->p
[i
], ratH
->p
[i
], n
);
1438 value_set_si(ratH
->p
[n
][n
], 1);
1439 int ok
= Matrix_Inverse(ratH
, invH
);
1443 T1
= Matrix_Alloc(n
+1, nparam
+1);
1444 Matrix_Product(invH
, C
, T1
);
1447 if (nparam
== 0 && value_notone_p(T1
->p
[n
][nparam
])) {
1448 for (i
= 0; i
< n
; ++i
) {
1449 if (!mpz_divisible_p(T1
->p
[i
][nparam
], T1
->p
[n
][nparam
])) {
1454 value_division(T1
->p
[i
][nparam
], T1
->p
[i
][nparam
], T1
->p
[n
][nparam
]);
1456 value_set_si(T1
->p
[n
][nparam
], 1);
1458 Ul
= Matrix_Alloc(dim
+1, n
+1);
1459 for (i
= 0; i
< dim
; ++i
)
1460 Vector_Copy(U
->p
[i
], Ul
->p
[i
], n
);
1461 value_set_si(Ul
->p
[dim
][n
], 1);
1462 T2
= Matrix_Alloc(dim
+1, nparam
+1);
1463 Matrix_Product(Ul
, T1
, T2
);
1467 T
= Matrix_Alloc(dim
+nparam
+1, (dim
-n
)+nparam
+1);
1468 for (i
= 0; i
< dim
; ++i
) {
1469 Vector_Copy(U
->p
[i
]+n
, T
->p
[i
], dim
-n
);
1470 Vector_Copy(T2
->p
[i
], T
->p
[i
]+dim
-n
, nparam
+1);
1472 for (i
= 0; i
< nparam
+1; ++i
)
1473 value_set_si(T
->p
[dim
+i
][(dim
-n
)+i
], 1);
1474 assert(value_one_p(T2
->p
[dim
][nparam
]));
1481 const char *barvinok_version(void)
1490 #ifdef USE_INCREMENTAL_BF
1492 #elif defined USE_INCREMENTAL_DF
1498 #ifdef HAVE_CORRECT_VERTICES
1499 " +CORRECT_VERTICES"
1501 " -CORRECT_VERTICES"