3 #include <barvinok/util.h>
4 #include <barvinok/options.h>
5 #include <polylib/ranking.h>
7 #include "lattice_point.h"
9 #define ALLOC(type) (type*)malloc(sizeof(type))
10 #define ALLOCN(type,n) (type*)malloc((n) * sizeof(type))
13 #define NALLOC(p,n) p = (typeof(p))malloc((n) * sizeof(*p))
15 #define NALLOC(p,n) p = (void *)malloc((n) * sizeof(*p))
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
);
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
)
43 unsigned NbRows
, NbColumns
;
48 while (fgets(s
, sizeof(s
), stdin
)) {
51 if (strncasecmp(s
, "vertices", sizeof("vertices")-1) == 0)
53 if (sscanf(s
, "%u %u", &NbRows
, &NbColumns
) == 2)
58 M
= Matrix_Alloc(NbRows
,NbColumns
);
61 P
= Rays2Polyhedron(M
, MaxRays
);
63 P
= Constraints2Polyhedron(M
, MaxRays
);
68 /* Inplace polarization
70 void Polyhedron_Polarize(Polyhedron
*P
)
72 unsigned NbRows
= P
->NbConstraints
+ P
->NbRays
;
76 q
= (Value
**)malloc(NbRows
* sizeof(Value
*));
78 for (i
= 0; i
< P
->NbRays
; ++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
;
86 P
->Ray
= q
+ P
->NbConstraints
;
90 * Rather general polar
91 * We can optimize it significantly if we assume that
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
)
102 unsigned dim
= P
->Dimension
+ 2;
103 Matrix
*M
= Matrix_Alloc(P
->NbRays
, dim
);
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
);
121 * Returns the supporting cone of P at the vertex with index v
123 Polyhedron
* supporting_cone(Polyhedron
*P
, int v
)
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]));
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
)))
141 assert(n
>= dim
- 2);
143 M
= Matrix_Alloc(n
, dim
);
145 for (i
= 0, j
= 0; i
< P
->NbConstraints
; ++i
)
147 value_set_si(M
->p
[j
][dim
-1], 0);
148 Vector_Copy(P
->Constraint
[i
], M
->p
[j
++], dim
-1);
151 P
= Constraints2Polyhedron(M
, P
->NbRays
+1);
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));
173 row
= Vector_Alloc(nparam
+1);
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
]))
199 if (j
== nparam
+ 1) {
200 supporting
[ix
] |= bx
;
214 Polyhedron
* supporting_cone_p(Polyhedron
*P
, Param_Vertices
*v
)
217 unsigned dim
= P
->Dimension
+ 2;
218 unsigned nparam
= v
->Vertex
->NbColumns
- 2;
219 unsigned nvar
= dim
- nparam
- 2;
223 unsigned *supporting
;
226 Polyhedron_Matrix_View(P
, &View
, P
->NbConstraints
);
227 supporting
= supporting_constraints(&View
, v
, &n
);
228 M
= Matrix_Alloc(n
, nvar
+2);
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);
238 P
= Constraints2Polyhedron(M
, P
->NbRays
+1);
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
);
253 Polyhedron
* triangulate_cone_with_options(Polyhedron
*P
,
254 struct barvinok_options
*options
)
256 const static int MAX_TRY
=10;
259 unsigned dim
= P
->Dimension
;
260 Matrix
*M
= Matrix_Alloc(P
->NbRays
+1, dim
+3);
262 Polyhedron
*L
, *R
, *T
;
263 assert(P
->NbEq
== 0);
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]))
279 Vector_Copy(P
->Ray
[i
], M
->p
[r
], dim
+1);
280 value_set_si(M
->p
[r
][dim
+2], 0);
284 M2
= Matrix_Alloc(dim
+1, dim
+2);
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
);
294 L
= Rays2Polyhedron(M3
, options
->MaxRays
);
299 /* Usually R should still be 0 */
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);
306 L
= Rays2Polyhedron(M3
, options
->MaxRays
);
310 assert(t
<= MAX_TRY
);
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]))
320 if (value_notzero_p(L
->Constraint
[i
][dim
+2]))
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
))
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);
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);
351 void check_triangulization(Polyhedron
*P
, Polyhedron
*T
)
353 Polyhedron
*C
, *D
, *E
, *F
, *G
, *U
;
354 for (C
= T
; C
; C
= C
->next
) {
358 U
= DomainConvex(DomainUnion(U
, C
, 100), 100);
359 for (D
= C
->next
; D
; D
= D
->next
) {
364 E
= DomainIntersection(C
, D
, 600);
365 assert(E
->NbRays
== 0 || E
->NbEq
>= 1);
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
;
385 value_absolute(c
, a
);
386 value_absolute(d
, b
);
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
);
402 else if (value_pos_p(a
))
404 else value_oppose(*x
, e
);
408 value_multiply(tmp
, a
, *x
);
409 value_subtract(tmp
, c
, tmp
);
410 value_division(*y
, tmp
, b
);
419 static int unimodular_complete_1(Matrix
*m
)
421 Value g
, b
, c
, old
, 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
) {
434 value_set_si(m
->p
[i
][j
], 1);
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
);
444 for (j
= 0; j
< m
->NbColumns
; ++j
) {
446 value_multiply(tmp
, m
->p
[0][j
], b
);
447 value_division(m
->p
[i
][j
], tmp
, old
);
449 value_assign(m
->p
[i
][j
], c
);
451 value_set_si(m
->p
[i
][j
], 0);
463 int unimodular_complete(Matrix
*M
, int row
)
470 return unimodular_complete_1(M
);
472 left_hermite(M
, &H
, &Q
, &U
);
474 for (r
= 0; ok
&& r
< row
; ++r
)
475 if (value_notone_p(H
->p
[r
][r
]))
478 for (r
= row
; r
< M
->NbRows
; ++r
)
479 Vector_Copy(Q
->p
[r
], M
->p
[r
], M
->NbColumns
);
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
)
494 left_hermite(A
, &H
, &Q
, &U
);
499 for (row
= 0, col
= 0; col
< H
->NbColumns
; ++col
, ++row
) {
500 while (value_zero_p(H
->p
[row
][col
]))
502 for (i
= 0; i
< col
; ++i
) {
503 if (value_negz_p(H
->p
[row
][i
]))
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
;
533 Q
= DomainConstraintSimplify(Q
, MaxRays
);
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);
550 P
= Polyhedron_Image(Q
, m2
, MaxRays
);
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
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
,
575 unsigned dim
= P
->Dimension
;
582 m1
= Matrix_Alloc(nvar
, nvar
);
583 P
= DomainConstraintSimplify(P
, MaxRays
);
585 f
= Matrix_Alloc(P
->NbEq
, dim
-nvar
+2);
589 for (i
= 0, j
= 0; i
< P
->NbEq
; ++i
) {
590 if (First_Non_Zero(P
->Constraint
[i
]+1, nvar
) == -1)
593 Vector_Gcd(P
->Constraint
[i
]+1, nvar
, &g
);
594 if (!factor
&& value_notone_p(g
))
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
);
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
);
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
);
624 void Line_Length(Polyhedron
*P
, Value
*len
)
630 assert(P
->Dimension
== 1);
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
);
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
);
650 value_subtract(tmp
, neg
, pos
);
651 value_increment(*len
, tmp
);
653 value_set_si(*len
, -1);
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
,
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
;
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
);
705 for (i
= 0; i
< P
->NbConstraints
; ++i
)
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
]))
713 for (i
= 0; i
< nvar
; ++i
) {
717 for (i
= 0; i
< H
->NbColumns
&& cnt
[0] < nvar
; ++i
) {
718 if (pos
[i
] == H
->NbRows
)
719 continue; /* A line direction */
720 if (rowgroup
[pos
[i
]] == -1)
721 rowgroup
[pos
[i
]] = i
;
722 for (j
= pos
[i
]+1; j
< H
->NbRows
; ++j
) {
723 if (value_zero_p(H
->p
[j
][i
]))
725 if (rowgroup
[j
] != -1)
727 rowgroup
[j
] = group
[i
];
728 for (k
= i
+1; k
< H
->NbColumns
&& j
>= pos
[k
]; ++k
) {
733 if (group
[k
] != group
[i
] && value_notzero_p(H
->p
[j
][k
])) {
734 assert(cnt
[group
[k
]] != 0);
735 assert(cnt
[group
[i
]] != 0);
736 if (group
[i
] < group
[k
]) {
737 cnt
[group
[i
]] += cnt
[group
[k
]];
741 cnt
[group
[k
]] += cnt
[group
[i
]];
750 if (cnt
[0] != nvar
) {
751 /* Extract out pure context constraints separately */
752 Polyhedron
**next
= &F
;
755 *T
= Matrix_Alloc(nvar
, nvar
);
756 for (i
= nparam
? -1 : 0; i
< nvar
; ++i
) {
760 for (j
= 0, k
= 0; j
< P
->NbConstraints
; ++j
)
761 if (rowgroup
[j
] == -1) {
762 if (First_Non_Zero(P
->Constraint
[j
]+1+nvar
,
775 for (j
= 0, k
= 0; j
< P
->NbConstraints
; ++j
)
776 if (rowgroup
[j
] >= 0 && group
[rowgroup
[j
]] == i
) {
783 for (j
= 0; j
< nvar
; ++j
) {
785 for (l
= 0, m
= 0; m
< d
; ++l
) {
788 value_assign((*T
)->p
[j
][tot_d
+m
++], U
->p
[j
][l
]);
792 M
= Matrix_Alloc(k
, d
+nparam
+2);
793 for (j
= 0, k
= 0; j
< P
->NbConstraints
; ++j
) {
795 if (rowgroup
[j
] != i
)
797 value_assign(M
->p
[k
][0], P
->Constraint
[j
][0]);
798 for (l
= 0, m
= 0; m
< d
; ++l
) {
801 value_assign(M
->p
[k
][1+m
++], H
->p
[j
][l
]);
803 Vector_Copy(P
->Constraint
[j
]+1+nvar
, M
->p
[k
]+1+m
, nparam
+1);
806 *next
= Constraints2Polyhedron(M
, NbMaxRays
);
807 next
= &(*next
)->next
;
821 /* Computes the intersection of the contexts of a list of factors */
822 Polyhedron
*Factor_Context(Polyhedron
*F
, unsigned nparam
, unsigned MaxRays
)
825 Polyhedron
*C
= NULL
;
827 for (Q
= F
; Q
; Q
= Q
->next
) {
829 Polyhedron
*next
= Q
->next
;
832 if (Q
->Dimension
!= nparam
)
833 QC
= Polyhedron_Project(Q
, nparam
);
836 C
= Q
== QC
? Polyhedron_Copy(QC
) : QC
;
839 C
= DomainIntersection(C
, QC
, MaxRays
);
850 * Project on final dim dimensions
852 Polyhedron
* Polyhedron_Project(Polyhedron
*P
, int dim
)
855 int remove
= P
->Dimension
- dim
;
859 if (P
->Dimension
== dim
)
860 return Polyhedron_Copy(P
);
862 T
= Matrix_Alloc(dim
+1, P
->Dimension
+1);
863 for (i
= 0; i
< dim
+1; ++i
)
864 value_set_si(T
->p
[i
][i
+remove
], 1);
865 I
= Polyhedron_Image(P
, T
, P
->NbConstraints
);
870 /* Constructs a new constraint that ensures that
871 * the first constraint is (strictly) smaller than
874 static void smaller_constraint(Value
*a
, Value
*b
, Value
*c
, int pos
, int shift
,
875 int len
, int strict
, Value
*tmp
)
877 value_oppose(*tmp
, b
[pos
+1]);
878 value_set_si(c
[0], 1);
879 Vector_Combine(a
+1+shift
, b
+1+shift
, c
+1, *tmp
, a
[pos
+1], len
-shift
-1);
881 value_decrement(c
[len
-shift
-1], c
[len
-shift
-1]);
882 ConstraintSimplify(c
, c
, len
-shift
, tmp
);
886 /* For each pair of lower and upper bounds on the first variable,
887 * calls fn with the set of constraints on the remaining variables
888 * where these bounds are active, i.e., (stricly) larger/smaller than
889 * the other lower/upper bounds, the lower and upper bound and the
892 * If the first variable is equal to an affine combination of the
893 * other variables then fn is called with both lower and upper
894 * pointing to the corresponding equality.
896 * If there is no lower (or upper) bound, then NULL is passed
897 * as the corresponding bound.
899 void for_each_lower_upper_bound(Polyhedron
*P
,
900 for_each_lower_upper_bound_init init
,
901 for_each_lower_upper_bound_fn fn
,
904 unsigned dim
= P
->Dimension
;
911 if (value_zero_p(P
->Constraint
[0][0]) &&
912 value_notzero_p(P
->Constraint
[0][1])) {
913 M
= Matrix_Alloc(P
->NbConstraints
-1, dim
-1+2);
914 for (i
= 1; i
< P
->NbConstraints
; ++i
) {
915 value_assign(M
->p
[i
-1][0], P
->Constraint
[i
][0]);
916 Vector_Copy(P
->Constraint
[i
]+2, M
->p
[i
-1]+1, dim
);
920 fn(M
, P
->Constraint
[0], P
->Constraint
[0], cb_data
);
926 pos
= ALLOCN(int, P
->NbConstraints
);
928 for (i
= 0, z
= 0; i
< P
->NbConstraints
; ++i
)
929 if (value_zero_p(P
->Constraint
[i
][1]))
930 pos
[P
->NbConstraints
-1 - z
++] = i
;
931 /* put those with positive coefficients first; number: p */
932 for (i
= 0, p
= 0, n
= P
->NbConstraints
-z
-1; i
< P
->NbConstraints
; ++i
)
933 if (value_pos_p(P
->Constraint
[i
][1]))
935 else if (value_neg_p(P
->Constraint
[i
][1]))
937 n
= P
->NbConstraints
-z
-p
;
942 M
= Matrix_Alloc((p
? p
-1 : 0) + (n
? n
-1 : 0) + z
+ 1, dim
-1+2);
943 for (i
= 0; i
< z
; ++i
) {
944 value_assign(M
->p
[i
][0], P
->Constraint
[pos
[P
->NbConstraints
-1 - i
]][0]);
945 Vector_Copy(P
->Constraint
[pos
[P
->NbConstraints
-1 - i
]]+2,
948 for (k
= p
? 0 : -1; k
< p
; ++k
) {
949 for (k2
= 0; k2
< p
; ++k2
) {
952 q
= 1 + z
+ k2
- (k2
> k
);
954 P
->Constraint
[pos
[k
]],
955 P
->Constraint
[pos
[k2
]],
956 M
->p
[q
], 0, 1, dim
+2, k2
> k
, &g
);
958 for (l
= n
? p
: p
-1; l
< p
+n
; ++l
) {
961 for (l2
= p
; l2
< p
+n
; ++l2
) {
964 q
= 1 + z
+ l2
-1 - (l2
> l
);
966 P
->Constraint
[pos
[l2
]],
967 P
->Constraint
[pos
[l
]],
968 M
->p
[q
], 0, 1, dim
+2, l2
> l
, &g
);
971 smaller_constraint(P
->Constraint
[pos
[k
]],
972 P
->Constraint
[pos
[l
]],
973 M
->p
[z
], 0, 1, dim
+2, 0, &g
);
974 lower
= p
? P
->Constraint
[pos
[k
]] : NULL
;
975 upper
= n
? P
->Constraint
[pos
[l
]] : NULL
;
976 fn(M
, lower
, upper
, cb_data
);
985 struct section
{ Polyhedron
* D
; evalue E
; };
995 static void PLL_init(unsigned n
, void *cb_data
)
997 struct PLL_data
*data
= (struct PLL_data
*)cb_data
;
999 data
->s
= ALLOCN(struct section
, n
);
1002 /* Computes ceil(-coef/abs(d)) */
1003 static evalue
* bv_ceil3(Value
*coef
, int len
, Value d
, Polyhedron
*P
)
1007 Vector
*val
= Vector_Alloc(len
);
1010 Vector_Oppose(coef
, val
->p
, len
);
1011 value_absolute(t
, d
);
1013 EP
= ceiling(val
->p
, t
, len
-1, P
);
1021 static void PLL_cb(Matrix
*M
, Value
*lower
, Value
*upper
, void *cb_data
)
1023 struct PLL_data
*data
= (struct PLL_data
*)cb_data
;
1024 unsigned dim
= M
->NbColumns
-1;
1032 M2
= Matrix_Copy(M
);
1033 T
= Constraints2Polyhedron(M2
, data
->MaxRays
);
1035 data
->s
[data
->nd
].D
= DomainIntersection(T
, data
->C
, data
->MaxRays
);
1038 POL_ENSURE_VERTICES(data
->s
[data
->nd
].D
);
1039 if (emptyQ(data
->s
[data
->nd
].D
)) {
1040 Polyhedron_Free(data
->s
[data
->nd
].D
);
1043 L
= bv_ceil3(lower
+1+1, dim
-1+1, lower
[0+1], data
->s
[data
->nd
].D
);
1044 U
= bv_ceil3(upper
+1+1, dim
-1+1, upper
[0+1], data
->s
[data
->nd
].D
);
1046 eadd(&data
->mone
, U
);
1047 emul(&data
->mone
, U
);
1048 data
->s
[data
->nd
].E
= *U
;
1054 static evalue
*ParamLine_Length_mod(Polyhedron
*P
, Polyhedron
*C
, unsigned MaxRays
)
1056 unsigned dim
= P
->Dimension
;
1057 unsigned nvar
= dim
- C
->Dimension
;
1058 struct PLL_data data
;
1064 value_init(data
.mone
.d
);
1065 evalue_set_si(&data
.mone
, -1, 1);
1068 data
.MaxRays
= MaxRays
;
1070 for_each_lower_upper_bound(P
, PLL_init
, PLL_cb
, &data
);
1074 value_set_si(F
->d
, 0);
1075 F
->x
.p
= new_enode(partition
, 2*data
.nd
, dim
-nvar
);
1076 for (k
= 0; k
< data
.nd
; ++k
) {
1077 EVALUE_SET_DOMAIN(F
->x
.p
->arr
[2*k
], data
.s
[k
].D
);
1078 value_clear(F
->x
.p
->arr
[2*k
+1].d
);
1079 F
->x
.p
->arr
[2*k
+1] = data
.s
[k
].E
;
1083 free_evalue_refs(&data
.mone
);
1088 evalue
* ParamLine_Length(Polyhedron
*P
, Polyhedron
*C
,
1089 struct barvinok_options
*options
)
1092 tmp
= ParamLine_Length_mod(P
, C
, options
->MaxRays
);
1093 if (options
->lookup_table
) {
1094 evalue_mod2table(tmp
, C
->Dimension
);
1100 Bool
isIdentity(Matrix
*M
)
1103 if (M
->NbRows
!= M
->NbColumns
)
1106 for (i
= 0;i
< M
->NbRows
; i
++)
1107 for (j
= 0; j
< M
->NbColumns
; j
++)
1109 if(value_notone_p(M
->p
[i
][j
]))
1112 if(value_notzero_p(M
->p
[i
][j
]))
1118 void Param_Polyhedron_Print(FILE* DST
, Param_Polyhedron
*PP
,
1119 const char **param_names
)
1124 for(P
=PP
->D
;P
;P
=P
->next
) {
1126 /* prints current val. dom. */
1127 fprintf(DST
, "---------------------------------------\n");
1128 fprintf(DST
, "Domain :\n");
1129 Print_Domain(DST
, P
->Domain
, param_names
);
1131 /* scan the vertices */
1132 fprintf(DST
, "Vertices :\n");
1133 FORALL_PVertex_in_ParamPolyhedron(V
,P
,PP
) {
1135 /* prints each vertex */
1136 Print_Vertex(DST
, V
->Vertex
, param_names
);
1139 END_FORALL_PVertex_in_ParamPolyhedron
;
1143 void Enumeration_Print(FILE *Dst
, Enumeration
*en
, const char **params
)
1145 for (; en
; en
= en
->next
) {
1146 Print_Domain(Dst
, en
->ValidityDomain
, params
);
1147 print_evalue(Dst
, &en
->EP
, params
);
1151 void Enumeration_Free(Enumeration
*en
)
1157 free_evalue_refs( &(en
->EP
) );
1158 Domain_Free( en
->ValidityDomain
);
1165 void Enumeration_mod2table(Enumeration
*en
, unsigned nparam
)
1167 for (; en
; en
= en
->next
) {
1168 evalue_mod2table(&en
->EP
, nparam
);
1169 reduce_evalue(&en
->EP
);
1173 size_t Enumeration_size(Enumeration
*en
)
1177 for (; en
; en
= en
->next
) {
1178 s
+= domain_size(en
->ValidityDomain
);
1179 s
+= evalue_size(&en
->EP
);
1184 /* Check whether every set in D2 is included in some set of D1 */
1185 int DomainIncludes(Polyhedron
*D1
, Polyhedron
*D2
)
1187 for ( ; D2
; D2
= D2
->next
) {
1189 for (P1
= D1
; P1
; P1
= P1
->next
)
1190 if (PolyhedronIncludes(P1
, D2
))
1198 int line_minmax(Polyhedron
*I
, Value
*min
, Value
*max
)
1203 value_oppose(I
->Constraint
[0][2], I
->Constraint
[0][2]);
1204 /* There should never be a remainder here */
1205 if (value_pos_p(I
->Constraint
[0][1]))
1206 mpz_fdiv_q(*min
, I
->Constraint
[0][2], I
->Constraint
[0][1]);
1208 mpz_fdiv_q(*min
, I
->Constraint
[0][2], I
->Constraint
[0][1]);
1209 value_assign(*max
, *min
);
1210 } else for (i
= 0; i
< I
->NbConstraints
; ++i
) {
1211 if (value_zero_p(I
->Constraint
[i
][1])) {
1216 value_oppose(I
->Constraint
[i
][2], I
->Constraint
[i
][2]);
1217 if (value_pos_p(I
->Constraint
[i
][1]))
1218 mpz_cdiv_q(*min
, I
->Constraint
[i
][2], I
->Constraint
[i
][1]);
1220 mpz_fdiv_q(*max
, I
->Constraint
[i
][2], I
->Constraint
[i
][1]);
1228 PROCEDURES TO COMPUTE ENUMERATION. recursive procedure, recurse for
1231 @param pos index position of current loop index (1..hdim-1)
1232 @param P loop domain
1233 @param context context values for fixed indices
1234 @param exist number of existential variables
1235 @return the number of integer points in this
1239 void count_points_e (int pos
, Polyhedron
*P
, int exist
, int nparam
,
1240 Value
*context
, Value
*res
)
1245 value_set_si(*res
, 0);
1250 count_points(pos
, P
, context
, res
);
1254 value_init(LB
); value_init(UB
); value_init(k
);
1258 if (lower_upper_bounds(pos
,P
,context
,&LB
,&UB
) !=0) {
1259 /* Problem if UB or LB is INFINITY */
1260 value_clear(LB
); value_clear(UB
); value_clear(k
);
1261 if (pos
> P
->Dimension
- nparam
- exist
)
1262 value_set_si(*res
, 1);
1264 value_set_si(*res
, -1);
1271 for (value_assign(k
,LB
); value_le(k
,UB
); value_increment(k
,k
)) {
1272 fprintf(stderr
, "(");
1273 for (i
=1; i
<pos
; i
++) {
1274 value_print(stderr
,P_VALUE_FMT
,context
[i
]);
1275 fprintf(stderr
,",");
1277 value_print(stderr
,P_VALUE_FMT
,k
);
1278 fprintf(stderr
,")\n");
1283 value_set_si(context
[pos
],0);
1284 if (value_lt(UB
,LB
)) {
1285 value_clear(LB
); value_clear(UB
); value_clear(k
);
1286 value_set_si(*res
, 0);
1291 value_set_si(*res
, 1);
1293 value_subtract(k
,UB
,LB
);
1294 value_add_int(k
,k
,1);
1295 value_assign(*res
, k
);
1297 value_clear(LB
); value_clear(UB
); value_clear(k
);
1301 /*-----------------------------------------------------------------*/
1302 /* Optimization idea */
1303 /* If inner loops are not a function of k (the current index) */
1304 /* i.e. if P->Constraint[i][pos]==0 for all P following this and */
1306 /* Then CNT = (UB-LB+1)*count_points(pos+1, P->next, context) */
1307 /* (skip the for loop) */
1308 /*-----------------------------------------------------------------*/
1311 value_set_si(*res
, 0);
1312 for (value_assign(k
,LB
);value_le(k
,UB
);value_increment(k
,k
)) {
1313 /* Insert k in context */
1314 value_assign(context
[pos
],k
);
1315 count_points_e(pos
+1, P
->next
, exist
, nparam
, context
, &c
);
1316 if(value_notmone_p(c
))
1317 value_addto(*res
, *res
, c
);
1319 value_set_si(*res
, -1);
1322 if (pos
> P
->Dimension
- nparam
- exist
&&
1329 fprintf(stderr
,"%d\n",CNT
);
1333 value_set_si(context
[pos
],0);
1334 value_clear(LB
); value_clear(UB
); value_clear(k
);
1336 } /* count_points_e */
1338 int DomainContains(Polyhedron
*P
, Value
*list_args
, int len
,
1339 unsigned MaxRays
, int set
)
1344 if (P
->Dimension
== len
)
1345 return in_domain(P
, list_args
);
1347 assert(set
); // assume list_args is large enough
1348 assert((P
->Dimension
- len
) % 2 == 0);
1350 for (i
= 0; i
< P
->Dimension
- len
; i
+= 2) {
1352 for (j
= 0 ; j
< P
->NbEq
; ++j
)
1353 if (value_notzero_p(P
->Constraint
[j
][1+len
+i
]))
1355 assert(j
< P
->NbEq
);
1356 value_absolute(m
, P
->Constraint
[j
][1+len
+i
]);
1357 k
= First_Non_Zero(P
->Constraint
[j
]+1, len
);
1359 assert(First_Non_Zero(P
->Constraint
[j
]+1+k
+1, len
- k
- 1) == -1);
1360 mpz_fdiv_q(list_args
[len
+i
], list_args
[k
], m
);
1361 mpz_fdiv_r(list_args
[len
+i
+1], list_args
[k
], m
);
1365 return in_domain(P
, list_args
);
1368 Polyhedron
*DomainConcat(Polyhedron
*head
, Polyhedron
*tail
)
1373 for (S
= head
; S
->next
; S
= S
->next
)
1379 evalue
*barvinok_lexsmaller_ev(Polyhedron
*P
, Polyhedron
*D
, unsigned dim
,
1380 Polyhedron
*C
, unsigned MaxRays
)
1383 Polyhedron
*RC
, *RD
, *Q
;
1384 unsigned nparam
= dim
+ C
->Dimension
;
1388 RC
= LexSmaller(P
, D
, dim
, C
, MaxRays
);
1392 exist
= RD
->Dimension
- nparam
- dim
;
1393 CA
= align_context(RC
, RD
->Dimension
, MaxRays
);
1394 Q
= DomainIntersection(RD
, CA
, MaxRays
);
1395 Polyhedron_Free(CA
);
1397 Polyhedron_Free(RC
);
1400 for (Q
= RD
; Q
; Q
= Q
->next
) {
1402 Polyhedron
*next
= Q
->next
;
1405 t
= barvinok_enumerate_e(Q
, exist
, nparam
, MaxRays
);
1422 Enumeration
*barvinok_lexsmaller(Polyhedron
*P
, Polyhedron
*D
, unsigned dim
,
1423 Polyhedron
*C
, unsigned MaxRays
)
1425 evalue
*EP
= barvinok_lexsmaller_ev(P
, D
, dim
, C
, MaxRays
);
1427 return partition2enumeration(EP
);
1430 /* "align" matrix to have nrows by inserting
1431 * the necessary number of rows and an equal number of columns in front
1433 Matrix
*align_matrix(Matrix
*M
, int nrows
)
1436 int newrows
= nrows
- M
->NbRows
;
1437 Matrix
*M2
= Matrix_Alloc(nrows
, newrows
+ M
->NbColumns
);
1438 for (i
= 0; i
< newrows
; ++i
)
1439 value_set_si(M2
->p
[i
][i
], 1);
1440 for (i
= 0; i
< M
->NbRows
; ++i
)
1441 Vector_Copy(M
->p
[i
], M2
->p
[newrows
+i
]+newrows
, M
->NbColumns
);
1445 static void print_varlist(FILE *out
, int n
, char **names
)
1449 for (i
= 0; i
< n
; ++i
) {
1452 fprintf(out
, "%s", names
[i
]);
1457 static void print_term(FILE *out
, Value v
, int pos
, int dim
, int nparam
,
1458 char **iter_names
, char **param_names
, int *first
)
1460 if (value_zero_p(v
)) {
1461 if (first
&& *first
&& pos
>= dim
+ nparam
)
1467 if (!*first
&& value_pos_p(v
))
1471 if (pos
< dim
+ nparam
) {
1472 if (value_mone_p(v
))
1474 else if (!value_one_p(v
))
1475 value_print(out
, VALUE_FMT
, v
);
1477 fprintf(out
, "%s", iter_names
[pos
]);
1479 fprintf(out
, "%s", param_names
[pos
-dim
]);
1481 value_print(out
, VALUE_FMT
, v
);
1484 char **util_generate_names(int n
, const char *prefix
)
1487 int len
= (prefix
? strlen(prefix
) : 0) + 10;
1488 char **names
= ALLOCN(char*, n
);
1490 fprintf(stderr
, "ERROR: memory overflow.\n");
1493 for (i
= 0; i
< n
; ++i
) {
1494 names
[i
] = ALLOCN(char, len
);
1496 fprintf(stderr
, "ERROR: memory overflow.\n");
1500 snprintf(names
[i
], len
, "%d", i
);
1502 snprintf(names
[i
], len
, "%s%d", prefix
, i
);
1508 void util_free_names(int n
, char **names
)
1511 for (i
= 0; i
< n
; ++i
)
1516 void Polyhedron_pprint(FILE *out
, Polyhedron
*P
, int dim
, int nparam
,
1517 char **iter_names
, char **param_names
)
1522 assert(dim
+ nparam
== P
->Dimension
);
1528 print_varlist(out
, nparam
, param_names
);
1529 fprintf(out
, " -> ");
1531 print_varlist(out
, dim
, iter_names
);
1532 fprintf(out
, " : ");
1535 fprintf(out
, "FALSE");
1536 else for (i
= 0; i
< P
->NbConstraints
; ++i
) {
1538 int v
= First_Non_Zero(P
->Constraint
[i
]+1, P
->Dimension
);
1539 if (v
== -1 && value_pos_p(P
->Constraint
[i
][0]))
1542 fprintf(out
, " && ");
1543 if (v
== -1 && value_notzero_p(P
->Constraint
[i
][1+P
->Dimension
]))
1544 fprintf(out
, "FALSE");
1545 else if (value_pos_p(P
->Constraint
[i
][v
+1])) {
1546 print_term(out
, P
->Constraint
[i
][v
+1], v
, dim
, nparam
,
1547 iter_names
, param_names
, NULL
);
1548 if (value_zero_p(P
->Constraint
[i
][0]))
1549 fprintf(out
, " = ");
1551 fprintf(out
, " >= ");
1552 for (j
= v
+1; j
<= dim
+nparam
; ++j
) {
1553 value_oppose(tmp
, P
->Constraint
[i
][1+j
]);
1554 print_term(out
, tmp
, j
, dim
, nparam
,
1555 iter_names
, param_names
, &first
);
1558 value_oppose(tmp
, P
->Constraint
[i
][1+v
]);
1559 print_term(out
, tmp
, v
, dim
, nparam
,
1560 iter_names
, param_names
, NULL
);
1561 fprintf(out
, " <= ");
1562 for (j
= v
+1; j
<= dim
+nparam
; ++j
)
1563 print_term(out
, P
->Constraint
[i
][1+j
], j
, dim
, nparam
,
1564 iter_names
, param_names
, &first
);
1568 fprintf(out
, " }\n");
1573 /* Construct a cone over P with P placed at x_d = 1, with
1574 * x_d the coordinate of an extra dimension
1576 * It's probably a mistake to depend so much on the internal
1577 * representation. We should probably simply compute the
1578 * vertices/facets first.
1580 Polyhedron
*Cone_over_Polyhedron(Polyhedron
*P
)
1582 unsigned NbConstraints
= 0;
1583 unsigned NbRays
= 0;
1587 if (POL_HAS(P
, POL_INEQUALITIES
))
1588 NbConstraints
= P
->NbConstraints
+ 1;
1589 if (POL_HAS(P
, POL_POINTS
))
1590 NbRays
= P
->NbRays
+ 1;
1592 C
= Polyhedron_Alloc(P
->Dimension
+1, NbConstraints
, NbRays
);
1593 if (POL_HAS(P
, POL_INEQUALITIES
)) {
1595 for (i
= 0; i
< P
->NbConstraints
; ++i
)
1596 Vector_Copy(P
->Constraint
[i
], C
->Constraint
[i
], P
->Dimension
+2);
1598 value_set_si(C
->Constraint
[P
->NbConstraints
][0], 1);
1599 value_set_si(C
->Constraint
[P
->NbConstraints
][1+P
->Dimension
], 1);
1601 if (POL_HAS(P
, POL_POINTS
)) {
1602 C
->NbBid
= P
->NbBid
;
1603 for (i
= 0; i
< P
->NbRays
; ++i
)
1604 Vector_Copy(P
->Ray
[i
], C
->Ray
[i
], P
->Dimension
+2);
1606 value_set_si(C
->Ray
[P
->NbRays
][0], 1);
1607 value_set_si(C
->Ray
[P
->NbRays
][1+C
->Dimension
], 1);
1609 POL_SET(C
, POL_VALID
);
1610 if (POL_HAS(P
, POL_INEQUALITIES
))
1611 POL_SET(C
, POL_INEQUALITIES
);
1612 if (POL_HAS(P
, POL_POINTS
))
1613 POL_SET(C
, POL_POINTS
);
1614 if (POL_HAS(P
, POL_VERTICES
))
1615 POL_SET(C
, POL_VERTICES
);
1619 /* Returns a (dim+nparam+1)x((dim-n)+nparam+1) matrix
1620 * mapping the transformed subspace back to the original space.
1621 * n is the number of equalities involving the variables
1622 * (i.e., not purely the parameters).
1623 * The remaining n coordinates in the transformed space would
1624 * have constant (parametric) values and are therefore not
1625 * included in the variables of the new space.
1627 Matrix
*compress_variables(Matrix
*Equalities
, unsigned nparam
)
1629 unsigned dim
= (Equalities
->NbColumns
-2) - nparam
;
1630 Matrix
*M
, *H
, *Q
, *U
, *C
, *ratH
, *invH
, *Ul
, *T1
, *T2
, *T
;
1635 for (n
= 0; n
< Equalities
->NbRows
; ++n
)
1636 if (First_Non_Zero(Equalities
->p
[n
]+1, dim
) == -1)
1639 return Identity(dim
+nparam
+1);
1641 value_set_si(mone
, -1);
1642 M
= Matrix_Alloc(n
, dim
);
1643 C
= Matrix_Alloc(n
+1, nparam
+1);
1644 for (i
= 0; i
< n
; ++i
) {
1645 Vector_Copy(Equalities
->p
[i
]+1, M
->p
[i
], dim
);
1646 Vector_Scale(Equalities
->p
[i
]+1+dim
, C
->p
[i
], mone
, nparam
+1);
1648 value_set_si(C
->p
[n
][nparam
], 1);
1649 left_hermite(M
, &H
, &Q
, &U
);
1654 ratH
= Matrix_Alloc(n
+1, n
+1);
1655 invH
= Matrix_Alloc(n
+1, n
+1);
1656 for (i
= 0; i
< n
; ++i
)
1657 Vector_Copy(H
->p
[i
], ratH
->p
[i
], n
);
1658 value_set_si(ratH
->p
[n
][n
], 1);
1659 ok
= Matrix_Inverse(ratH
, invH
);
1663 T1
= Matrix_Alloc(n
+1, nparam
+1);
1664 Matrix_Product(invH
, C
, T1
);
1667 if (value_notone_p(T1
->p
[n
][nparam
])) {
1668 for (i
= 0; i
< n
; ++i
) {
1669 if (!mpz_divisible_p(T1
->p
[i
][nparam
], T1
->p
[n
][nparam
])) {
1674 /* compress_params should have taken care of this */
1675 for (j
= 0; j
< nparam
; ++j
)
1676 assert(mpz_divisible_p(T1
->p
[i
][j
], T1
->p
[n
][nparam
]));
1677 Vector_AntiScale(T1
->p
[i
], T1
->p
[i
], T1
->p
[n
][nparam
], nparam
+1);
1679 value_set_si(T1
->p
[n
][nparam
], 1);
1681 Ul
= Matrix_Alloc(dim
+1, n
+1);
1682 for (i
= 0; i
< dim
; ++i
)
1683 Vector_Copy(U
->p
[i
], Ul
->p
[i
], n
);
1684 value_set_si(Ul
->p
[dim
][n
], 1);
1685 T2
= Matrix_Alloc(dim
+1, nparam
+1);
1686 Matrix_Product(Ul
, T1
, T2
);
1690 T
= Matrix_Alloc(dim
+nparam
+1, (dim
-n
)+nparam
+1);
1691 for (i
= 0; i
< dim
; ++i
) {
1692 Vector_Copy(U
->p
[i
]+n
, T
->p
[i
], dim
-n
);
1693 Vector_Copy(T2
->p
[i
], T
->p
[i
]+dim
-n
, nparam
+1);
1695 for (i
= 0; i
< nparam
+1; ++i
)
1696 value_set_si(T
->p
[dim
+i
][(dim
-n
)+i
], 1);
1697 assert(value_one_p(T2
->p
[dim
][nparam
]));
1704 /* Computes the left inverse of an affine embedding M and, if Eq is not NULL,
1705 * the equalities that define the affine subspace onto which M maps
1708 Matrix
*left_inverse(Matrix
*M
, Matrix
**Eq
)
1711 Matrix
*L
, *H
, *Q
, *U
, *ratH
, *invH
, *Ut
, *inv
;
1714 if (M
->NbColumns
== 1) {
1715 inv
= Matrix_Alloc(1, M
->NbRows
);
1716 value_set_si(inv
->p
[0][M
->NbRows
-1], 1);
1718 *Eq
= Matrix_Alloc(M
->NbRows
-1, 1+(M
->NbRows
-1)+1);
1719 for (i
= 0; i
< M
->NbRows
-1; ++i
) {
1720 value_oppose((*Eq
)->p
[i
][1+i
], M
->p
[M
->NbRows
-1][0]);
1721 value_assign((*Eq
)->p
[i
][1+(M
->NbRows
-1)], M
->p
[i
][0]);
1728 L
= Matrix_Alloc(M
->NbRows
-1, M
->NbColumns
-1);
1729 for (i
= 0; i
< L
->NbRows
; ++i
)
1730 Vector_Copy(M
->p
[i
], L
->p
[i
], L
->NbColumns
);
1731 right_hermite(L
, &H
, &U
, &Q
);
1734 t
= Vector_Alloc(U
->NbColumns
);
1735 for (i
= 0; i
< U
->NbColumns
; ++i
)
1736 value_oppose(t
->p
[i
], M
->p
[i
][M
->NbColumns
-1]);
1738 *Eq
= Matrix_Alloc(H
->NbRows
- H
->NbColumns
, 2 + U
->NbColumns
);
1739 for (i
= 0; i
< H
->NbRows
- H
->NbColumns
; ++i
) {
1740 Vector_Copy(U
->p
[H
->NbColumns
+i
], (*Eq
)->p
[i
]+1, U
->NbColumns
);
1741 Inner_Product(U
->p
[H
->NbColumns
+i
], t
->p
, U
->NbColumns
,
1742 (*Eq
)->p
[i
]+1+U
->NbColumns
);
1745 ratH
= Matrix_Alloc(H
->NbColumns
+1, H
->NbColumns
+1);
1746 invH
= Matrix_Alloc(H
->NbColumns
+1, H
->NbColumns
+1);
1747 for (i
= 0; i
< H
->NbColumns
; ++i
)
1748 Vector_Copy(H
->p
[i
], ratH
->p
[i
], H
->NbColumns
);
1749 value_set_si(ratH
->p
[ratH
->NbRows
-1][ratH
->NbColumns
-1], 1);
1751 ok
= Matrix_Inverse(ratH
, invH
);
1754 Ut
= Matrix_Alloc(invH
->NbRows
, U
->NbColumns
+1);
1755 for (i
= 0; i
< Ut
->NbRows
-1; ++i
) {
1756 Vector_Copy(U
->p
[i
], Ut
->p
[i
], U
->NbColumns
);
1757 Inner_Product(U
->p
[i
], t
->p
, U
->NbColumns
, &Ut
->p
[i
][Ut
->NbColumns
-1]);
1761 value_set_si(Ut
->p
[Ut
->NbRows
-1][Ut
->NbColumns
-1], 1);
1762 inv
= Matrix_Alloc(invH
->NbRows
, Ut
->NbColumns
);
1763 Matrix_Product(invH
, Ut
, inv
);
1769 /* Check whether all rays are revlex positive in the parameters
1771 int Polyhedron_has_revlex_positive_rays(Polyhedron
*P
, unsigned nparam
)
1774 for (r
= 0; r
< P
->NbRays
; ++r
) {
1776 if (value_notzero_p(P
->Ray
[r
][P
->Dimension
+1]))
1778 for (i
= P
->Dimension
-1; i
>= P
->Dimension
-nparam
; --i
) {
1779 if (value_neg_p(P
->Ray
[r
][i
+1]))
1781 if (value_pos_p(P
->Ray
[r
][i
+1]))
1784 /* A ray independent of the parameters */
1785 if (i
< P
->Dimension
-nparam
)
1791 static Polyhedron
*Recession_Cone(Polyhedron
*P
, unsigned nparam
, unsigned MaxRays
)
1794 unsigned nvar
= P
->Dimension
- nparam
;
1795 Matrix
*M
= Matrix_Alloc(P
->NbConstraints
, 1 + nvar
+ 1);
1797 for (i
= 0; i
< P
->NbConstraints
; ++i
)
1798 Vector_Copy(P
->Constraint
[i
], M
->p
[i
], 1+nvar
);
1799 R
= Constraints2Polyhedron(M
, MaxRays
);
1804 int Polyhedron_is_unbounded(Polyhedron
*P
, unsigned nparam
, unsigned MaxRays
)
1808 Polyhedron
*R
= Recession_Cone(P
, nparam
, MaxRays
);
1809 POL_ENSURE_VERTICES(R
);
1811 for (i
= 0; i
< R
->NbRays
; ++i
)
1812 if (value_zero_p(R
->Ray
[i
][1+R
->Dimension
]))
1814 is_unbounded
= R
->NbBid
> 0 || i
< R
->NbRays
;
1816 return is_unbounded
;
1819 static void SwapColumns(Value
**V
, int n
, int i
, int j
)
1823 for (r
= 0; r
< n
; ++r
)
1824 value_swap(V
[r
][i
], V
[r
][j
]);
1827 void Polyhedron_ExchangeColumns(Polyhedron
*P
, int Column1
, int Column2
)
1829 SwapColumns(P
->Constraint
, P
->NbConstraints
, Column1
, Column2
);
1830 SwapColumns(P
->Ray
, P
->NbRays
, Column1
, Column2
);
1833 Polyhedron_Matrix_View(P
, &M
, P
->NbConstraints
);
1834 Gauss(&M
, P
->NbEq
, P
->Dimension
+1);
1838 /* perform transposition inline; assumes M is a square matrix */
1839 void Matrix_Transposition(Matrix
*M
)
1843 assert(M
->NbRows
== M
->NbColumns
);
1844 for (i
= 0; i
< M
->NbRows
; ++i
)
1845 for (j
= i
+1; j
< M
->NbColumns
; ++j
)
1846 value_swap(M
->p
[i
][j
], M
->p
[j
][i
]);
1849 /* Matrix "view" of first rows rows */
1850 void Polyhedron_Matrix_View(Polyhedron
*P
, Matrix
*M
, unsigned rows
)
1853 M
->NbColumns
= P
->Dimension
+2;
1854 M
->p_Init
= P
->p_Init
;
1855 M
->p
= P
->Constraint
;