3 #include <isl_set_polylib.h>
4 #include <barvinok/util.h>
5 #include <barvinok/options.h>
6 #include <polylib/ranking.h>
8 #include "lattice_point.h"
10 #define ALLOC(type) (type*)malloc(sizeof(type))
11 #define ALLOCN(type,n) (type*)malloc((n) * sizeof(type))
14 #define NALLOC(p,n) p = (typeof(p))malloc((n) * sizeof(*p))
16 #define NALLOC(p,n) p = (void *)malloc((n) * sizeof(*p))
19 void manual_count(Polyhedron
*P
, Value
* result
)
21 isl_ctx
*ctx
= isl_ctx_alloc();
25 int nvar
= P
->Dimension
;
28 dim
= isl_space_set_alloc(ctx
, 0, nvar
);
29 set
= isl_set_new_from_polylib(P
, dim
);
32 r
= isl_set_count(set
, &v
);
33 isl_int_get_gmp(v
, *result
);
42 #include <barvinok/evalue.h>
43 #include <barvinok/util.h>
44 #include <barvinok/barvinok.h>
46 /* Return random value between 0 and max-1 inclusive
48 int random_int(int max
) {
49 return (int) (((double)(max
))*rand()/(RAND_MAX
+1.0));
52 Polyhedron
*Polyhedron_Read(unsigned MaxRays
)
55 unsigned NbRows
, NbColumns
;
60 while (fgets(s
, sizeof(s
), stdin
)) {
63 if (strncasecmp(s
, "vertices", sizeof("vertices")-1) == 0)
65 if (sscanf(s
, "%u %u", &NbRows
, &NbColumns
) == 2)
70 M
= Matrix_Alloc(NbRows
,NbColumns
);
73 P
= Rays2Polyhedron(M
, MaxRays
);
75 P
= Constraints2Polyhedron(M
, MaxRays
);
80 /* Inplace polarization
82 void Polyhedron_Polarize(Polyhedron
*P
)
84 unsigned NbRows
= P
->NbConstraints
+ P
->NbRays
;
88 q
= (Value
**)malloc(NbRows
* sizeof(Value
*));
90 for (i
= 0; i
< P
->NbRays
; ++i
)
92 for (; i
< NbRows
; ++i
)
93 q
[i
] = P
->Constraint
[i
-P
->NbRays
];
94 P
->NbConstraints
= NbRows
- P
->NbConstraints
;
95 P
->NbRays
= NbRows
- P
->NbRays
;
98 P
->Ray
= q
+ P
->NbConstraints
;
102 * Rather general polar
103 * We can optimize it significantly if we assume that
106 * Also, we calculate the polar as defined in Schrijver
107 * The opposite should probably work as well and would
108 * eliminate the need for multiplying by -1
110 Polyhedron
* Polyhedron_Polar(Polyhedron
*P
, unsigned NbMaxRays
)
114 unsigned dim
= P
->Dimension
+ 2;
115 Matrix
*M
= Matrix_Alloc(P
->NbRays
, dim
);
119 value_set_si(mone
, -1);
120 for (i
= 0; i
< P
->NbRays
; ++i
) {
121 Vector_Scale(P
->Ray
[i
], M
->p
[i
], mone
, dim
);
122 value_multiply(M
->p
[i
][0], M
->p
[i
][0], mone
);
123 value_multiply(M
->p
[i
][dim
-1], M
->p
[i
][dim
-1], mone
);
125 P
= Constraints2Polyhedron(M
, NbMaxRays
);
133 * Returns the supporting cone of P at the vertex with index v
135 Polyhedron
* supporting_cone(Polyhedron
*P
, int v
)
140 unsigned char *supporting
= (unsigned char *)malloc(P
->NbConstraints
);
141 unsigned dim
= P
->Dimension
+ 2;
143 assert(v
>=0 && v
< P
->NbRays
);
144 assert(value_pos_p(P
->Ray
[v
][dim
-1]));
148 for (i
= 0, n
= 0; i
< P
->NbConstraints
; ++i
) {
149 Inner_Product(P
->Constraint
[i
] + 1, P
->Ray
[v
] + 1, dim
- 1, &tmp
);
150 if ((supporting
[i
] = value_zero_p(tmp
)))
153 assert(n
>= dim
- 2);
155 M
= Matrix_Alloc(n
, dim
);
157 for (i
= 0, j
= 0; i
< P
->NbConstraints
; ++i
)
159 value_set_si(M
->p
[j
][dim
-1], 0);
160 Vector_Copy(P
->Constraint
[i
], M
->p
[j
++], dim
-1);
163 P
= Constraints2Polyhedron(M
, P
->NbRays
+1);
169 #define INT_BITS (sizeof(unsigned) * 8)
171 unsigned *supporting_constraints(Matrix
*Constraints
, Param_Vertices
*v
, int *n
)
173 Value lcm
, tmp
, tmp2
;
174 unsigned dim
= Constraints
->NbColumns
;
175 unsigned nparam
= v
->Vertex
->NbColumns
- 2;
176 unsigned nvar
= dim
- nparam
- 2;
177 int len
= (Constraints
->NbRows
+INT_BITS
-1)/INT_BITS
;
178 unsigned *supporting
= (unsigned *)calloc(len
, sizeof(unsigned));
185 row
= Vector_Alloc(nparam
+1);
190 value_set_si(lcm
, 1);
191 for (i
= 0, *n
= 0, ix
= 0, bx
= MSB
; i
< Constraints
->NbRows
; ++i
) {
192 Vector_Set(row
->p
, 0, nparam
+1);
193 for (j
= 0 ; j
< nvar
; ++j
) {
194 value_set_si(tmp
, 1);
195 value_assign(tmp2
, Constraints
->p
[i
][j
+1]);
196 if (value_ne(lcm
, v
->Vertex
->p
[j
][nparam
+1])) {
197 value_assign(tmp
, lcm
);
198 value_lcm(lcm
, lcm
, v
->Vertex
->p
[j
][nparam
+1]);
199 value_division(tmp
, lcm
, tmp
);
200 value_multiply(tmp2
, tmp2
, lcm
);
201 value_division(tmp2
, tmp2
, v
->Vertex
->p
[j
][nparam
+1]);
203 Vector_Combine(row
->p
, v
->Vertex
->p
[j
], row
->p
,
204 tmp
, tmp2
, nparam
+1);
206 value_set_si(tmp
, 1);
207 Vector_Combine(row
->p
, Constraints
->p
[i
]+1+nvar
, row
->p
, tmp
, lcm
, nparam
+1);
208 for (j
= 0; j
< nparam
+1; ++j
)
209 if (value_notzero_p(row
->p
[j
]))
211 if (j
== nparam
+ 1) {
212 supporting
[ix
] |= bx
;
226 Polyhedron
* supporting_cone_p(Polyhedron
*P
, Param_Vertices
*v
)
229 unsigned dim
= P
->Dimension
+ 2;
230 unsigned nparam
= v
->Vertex
->NbColumns
- 2;
231 unsigned nvar
= dim
- nparam
- 2;
235 unsigned *supporting
;
238 Polyhedron_Matrix_View(P
, &View
, P
->NbConstraints
);
239 supporting
= supporting_constraints(&View
, v
, &n
);
240 M
= Matrix_Alloc(n
, nvar
+2);
242 for (i
= 0, j
= 0, ix
= 0, bx
= MSB
; i
< P
->NbConstraints
; ++i
) {
243 if (supporting
[ix
] & bx
) {
244 value_set_si(M
->p
[j
][nvar
+1], 0);
245 Vector_Copy(P
->Constraint
[i
], M
->p
[j
++], nvar
+1);
250 P
= Constraints2Polyhedron(M
, P
->NbRays
+1);
256 Polyhedron
* triangulate_cone(Polyhedron
*P
, unsigned NbMaxCons
)
258 struct barvinok_options
*options
= barvinok_options_new_with_defaults();
259 options
->MaxRays
= NbMaxCons
;
260 P
= triangulate_cone_with_options(P
, options
);
261 barvinok_options_free(options
);
265 Polyhedron
* triangulate_cone_with_options(Polyhedron
*P
,
266 struct barvinok_options
*options
)
268 const static int MAX_TRY
=10;
271 unsigned dim
= P
->Dimension
;
272 Matrix
*M
= Matrix_Alloc(P
->NbRays
+1, dim
+3);
274 Polyhedron
*L
, *R
, *T
;
275 assert(P
->NbEq
== 0);
281 Vector_Set(M
->p
[0]+1, 0, dim
+1);
282 value_set_si(M
->p
[0][0], 1);
283 value_set_si(M
->p
[0][dim
+2], 1);
284 Vector_Set(M
->p
[P
->NbRays
]+1, 0, dim
+2);
285 value_set_si(M
->p
[P
->NbRays
][0], 1);
286 value_set_si(M
->p
[P
->NbRays
][dim
+1], 1);
288 for (i
= 0, r
= 1; i
< P
->NbRays
; ++i
) {
289 if (value_notzero_p(P
->Ray
[i
][dim
+1]))
291 Vector_Copy(P
->Ray
[i
], M
->p
[r
], dim
+1);
292 value_set_si(M
->p
[r
][dim
+2], 0);
296 M2
= Matrix_Alloc(dim
+1, dim
+2);
299 if (options
->try_Delaunay_triangulation
) {
300 /* Delaunay triangulation */
301 for (r
= 1; r
< P
->NbRays
; ++r
) {
302 Inner_Product(M
->p
[r
]+1, M
->p
[r
]+1, dim
, &tmp
);
303 value_assign(M
->p
[r
][dim
+1], tmp
);
306 L
= Rays2Polyhedron(M3
, options
->MaxRays
);
311 /* Usually R should still be 0 */
314 for (r
= 1; r
< P
->NbRays
; ++r
) {
315 value_set_si(M
->p
[r
][dim
+1], random_int((t
+1)*dim
*P
->NbRays
)+1);
318 L
= Rays2Polyhedron(M3
, options
->MaxRays
);
322 assert(t
<= MAX_TRY
);
327 POL_ENSURE_FACETS(L
);
328 for (i
= 0; i
< L
->NbConstraints
; ++i
) {
329 /* Ignore perpendicular facets, i.e., facets with 0 z-coordinate */
330 if (value_negz_p(L
->Constraint
[i
][dim
+1]))
332 if (value_notzero_p(L
->Constraint
[i
][dim
+2]))
334 for (j
= 1, r
= 1; j
< M
->NbRows
; ++j
) {
335 Inner_Product(M
->p
[j
]+1, L
->Constraint
[i
]+1, dim
+1, &tmp
);
336 if (value_notzero_p(tmp
))
340 Vector_Copy(M
->p
[j
]+1, M2
->p
[r
]+1, dim
);
341 value_set_si(M2
->p
[r
][0], 1);
342 value_set_si(M2
->p
[r
][dim
+1], 0);
346 Vector_Set(M2
->p
[0]+1, 0, dim
);
347 value_set_si(M2
->p
[0][0], 1);
348 value_set_si(M2
->p
[0][dim
+1], 1);
349 T
= Rays2Polyhedron(M2
, P
->NbConstraints
+1);
363 void check_triangulization(Polyhedron
*P
, Polyhedron
*T
)
365 Polyhedron
*C
, *D
, *E
, *F
, *G
, *U
;
366 for (C
= T
; C
; C
= C
->next
) {
370 U
= DomainConvex(DomainUnion(U
, C
, 100), 100);
371 for (D
= C
->next
; D
; D
= D
->next
) {
376 E
= DomainIntersection(C
, D
, 600);
377 assert(E
->NbRays
== 0 || E
->NbEq
>= 1);
383 assert(PolyhedronIncludes(U
, P
));
384 assert(PolyhedronIncludes(P
, U
));
387 /* Computes x, y and g such that g = gcd(a,b) and a*x+b*y = g */
388 void Extended_Euclid(Value a
, Value b
, Value
*x
, Value
*y
, Value
*g
)
390 Value c
, d
, e
, f
, tmp
;
397 value_absolute(c
, a
);
398 value_absolute(d
, b
);
401 while(value_pos_p(d
)) {
402 value_division(tmp
, c
, d
);
403 value_multiply(tmp
, tmp
, f
);
404 value_subtract(e
, e
, tmp
);
405 value_division(tmp
, c
, d
);
406 value_multiply(tmp
, tmp
, d
);
407 value_subtract(c
, c
, tmp
);
414 else if (value_pos_p(a
))
416 else value_oppose(*x
, e
);
420 value_multiply(tmp
, a
, *x
);
421 value_subtract(tmp
, c
, tmp
);
422 value_division(*y
, tmp
, b
);
431 static int unimodular_complete_1(Matrix
*m
)
433 Value g
, b
, c
, old
, tmp
;
442 value_assign(g
, m
->p
[0][0]);
443 for (i
= 1; value_zero_p(g
) && i
< m
->NbColumns
; ++i
) {
444 for (j
= 0; j
< m
->NbColumns
; ++j
) {
446 value_set_si(m
->p
[i
][j
], 1);
448 value_set_si(m
->p
[i
][j
], 0);
450 value_assign(g
, m
->p
[0][i
]);
452 for (; i
< m
->NbColumns
; ++i
) {
453 value_assign(old
, g
);
454 Extended_Euclid(old
, m
->p
[0][i
], &c
, &b
, &g
);
456 for (j
= 0; j
< m
->NbColumns
; ++j
) {
458 value_multiply(tmp
, m
->p
[0][j
], b
);
459 value_division(m
->p
[i
][j
], tmp
, old
);
461 value_assign(m
->p
[i
][j
], c
);
463 value_set_si(m
->p
[i
][j
], 0);
475 int unimodular_complete(Matrix
*M
, int row
)
482 return unimodular_complete_1(M
);
484 left_hermite(M
, &H
, &Q
, &U
);
486 for (r
= 0; ok
&& r
< row
; ++r
)
487 if (value_notone_p(H
->p
[r
][r
]))
490 for (r
= row
; r
< M
->NbRows
; ++r
)
491 Vector_Copy(Q
->p
[r
], M
->p
[r
], M
->NbColumns
);
497 * left_hermite may leave positive entries below the main diagonal in H.
498 * This function postprocesses the output of left_hermite to make
499 * the non-zero entries below the main diagonal negative.
501 void neg_left_hermite(Matrix
*A
, Matrix
**H_p
, Matrix
**Q_p
, Matrix
**U_p
)
506 left_hermite(A
, &H
, &Q
, &U
);
511 for (row
= 0, col
= 0; col
< H
->NbColumns
; ++col
, ++row
) {
512 while (value_zero_p(H
->p
[row
][col
]))
514 for (i
= 0; i
< col
; ++i
) {
515 if (value_negz_p(H
->p
[row
][i
]))
518 /* subtract column col from column i in H and U */
519 for (j
= 0; j
< H
->NbRows
; ++j
)
520 value_subtract(H
->p
[j
][i
], H
->p
[j
][i
], H
->p
[j
][col
]);
521 for (j
= 0; j
< U
->NbRows
; ++j
)
522 value_subtract(U
->p
[j
][i
], U
->p
[j
][i
], U
->p
[j
][col
]);
524 /* add row i to row col in Q */
525 for (j
= 0; j
< Q
->NbColumns
; ++j
)
526 value_addto(Q
->p
[col
][j
], Q
->p
[col
][j
], Q
->p
[i
][j
]);
532 * Returns a full-dimensional polyhedron with the same number
533 * of integer points as P
535 Polyhedron
*remove_equalities(Polyhedron
*P
, unsigned MaxRays
)
539 Polyhedron
*Q
= Polyhedron_Copy(P
);
540 unsigned dim
= P
->Dimension
;
545 Q
= DomainConstraintSimplify(Q
, MaxRays
);
549 Polyhedron_Matrix_View(Q
, &M
, Q
->NbEq
);
550 T
= compress_variables(&M
, 0);
555 P
= Polyhedron_Preimage(Q
, T
, MaxRays
);
565 * Returns a full-dimensional polyhedron with the same number
566 * of integer points as P
567 * nvar specifies the number of variables
568 * The remaining dimensions are assumed to be parameters
570 * factor is NbEq x (nparam+2) matrix, containing stride constraints
571 * on the parameters; column nparam is the constant;
572 * column nparam+1 is the stride
574 * if factor is NULL, only remove equalities that don't affect
575 * the number of points
577 Polyhedron
*remove_equalities_p(Polyhedron
*P
, unsigned nvar
, Matrix
**factor
,
582 unsigned dim
= P
->Dimension
;
589 m1
= Matrix_Alloc(nvar
, nvar
);
590 P
= DomainConstraintSimplify(P
, MaxRays
);
592 f
= Matrix_Alloc(P
->NbEq
, dim
-nvar
+2);
596 for (i
= 0, j
= 0; i
< P
->NbEq
; ++i
) {
597 if (First_Non_Zero(P
->Constraint
[i
]+1, nvar
) == -1)
600 Vector_Gcd(P
->Constraint
[i
]+1, nvar
, &g
);
601 if (!factor
&& value_notone_p(g
))
605 Vector_Copy(P
->Constraint
[i
]+1+nvar
, f
->p
[j
], dim
-nvar
+1);
606 value_assign(f
->p
[j
][dim
-nvar
+1], g
);
609 Vector_Copy(P
->Constraint
[i
]+1, m1
->p
[j
], nvar
);
615 unimodular_complete(m1
, j
);
617 m2
= Matrix_Alloc(dim
+1-j
, dim
+1);
618 for (i
= 0; i
< nvar
-j
; ++i
)
619 Vector_Copy(m1
->p
[i
+j
], m2
->p
[i
], nvar
);
621 for (i
= nvar
-j
; i
<= dim
-j
; ++i
)
622 value_set_si(m2
->p
[i
][i
+j
], 1);
624 Q
= Polyhedron_Image(P
, m2
, MaxRays
);
631 void Line_Length(Polyhedron
*P
, Value
*len
)
637 assert(P
->Dimension
== 1);
640 if (mpz_divisible_p(P
->Constraint
[0][2], P
->Constraint
[0][1]))
641 value_set_si(*len
, 1);
643 value_set_si(*len
, 0);
651 for (i
= 0; i
< P
->NbConstraints
; ++i
) {
652 value_oppose(tmp
, P
->Constraint
[i
][2]);
653 if (value_pos_p(P
->Constraint
[i
][1])) {
654 mpz_cdiv_q(tmp
, tmp
, P
->Constraint
[i
][1]);
655 if (!p
|| value_gt(tmp
, pos
))
656 value_assign(pos
, tmp
);
658 } else if (value_neg_p(P
->Constraint
[i
][1])) {
659 mpz_fdiv_q(tmp
, tmp
, P
->Constraint
[i
][1]);
660 if (!n
|| value_lt(tmp
, neg
))
661 value_assign(neg
, tmp
);
665 value_subtract(tmp
, neg
, pos
);
666 value_increment(*len
, tmp
);
668 value_set_si(*len
, -1);
676 /* Update group[k] to the group column k belongs to.
677 * When merging two groups, only the group of the current
678 * group leader is changed. Here we change the group of
679 * the other members to also point to the group that the
680 * old group leader now points to.
682 static void update_group(int *group
, int *cnt
, int k
)
691 * Factors the polyhedron P into polyhedra Q_i such that
692 * the number of integer points in P is equal to the product
693 * of the number of integer points in the individual Q_i
695 * If no factors can be found, NULL is returned.
696 * Otherwise, a linked list of the factors is returned.
698 * If there are factors and if T is not NULL, then a matrix will be
699 * returned through T expressing the old variables in terms of the
700 * new variables as they appear in the sequence of factors.
702 * The algorithm works by first computing the Hermite normal form
703 * and then grouping columns linked by one or more constraints together,
704 * where a constraints "links" two or more columns if the constraint
705 * has nonzero coefficients in the columns.
707 Polyhedron
* Polyhedron_Factor(Polyhedron
*P
, unsigned nparam
, Matrix
**T
,
711 Matrix
*M
, *H
, *Q
, *U
;
712 int *pos
; /* for each column: row position of pivot */
713 int *group
; /* group to which a column belongs */
714 int *cnt
; /* number of columns in the group */
715 int *rowgroup
; /* group to which a constraint belongs */
716 int nvar
= P
->Dimension
- nparam
;
717 Polyhedron
*F
= NULL
;
725 NALLOC(rowgroup
, P
->NbConstraints
);
727 M
= Matrix_Alloc(P
->NbConstraints
, nvar
);
728 for (i
= 0; i
< P
->NbConstraints
; ++i
)
729 Vector_Copy(P
->Constraint
[i
]+1, M
->p
[i
], nvar
);
730 left_hermite(M
, &H
, &Q
, &U
);
734 for (i
= 0; i
< P
->NbConstraints
; ++i
)
736 for (i
= 0, j
= 0; i
< H
->NbColumns
; ++i
) {
737 for ( ; j
< H
->NbRows
; ++j
)
738 if (value_notzero_p(H
->p
[j
][i
]))
742 for (i
= 0; i
< nvar
; ++i
) {
746 for (i
= 0; i
< H
->NbColumns
&& cnt
[0] < nvar
; ++i
) {
747 if (pos
[i
] == H
->NbRows
)
748 continue; /* A line direction */
749 if (rowgroup
[pos
[i
]] == -1)
750 rowgroup
[pos
[i
]] = i
;
751 for (j
= pos
[i
]+1; j
< H
->NbRows
; ++j
) {
752 if (value_zero_p(H
->p
[j
][i
]))
754 if (rowgroup
[j
] != -1)
756 rowgroup
[j
] = group
[i
];
757 for (k
= i
+1; k
< H
->NbColumns
&& j
>= pos
[k
]; ++k
) {
758 update_group(group
, cnt
, k
);
759 update_group(group
, cnt
, i
);
760 if (group
[k
] != group
[i
] && value_notzero_p(H
->p
[j
][k
])) {
761 assert(cnt
[group
[k
]] != 0);
762 assert(cnt
[group
[i
]] != 0);
763 if (group
[i
] < group
[k
]) {
764 cnt
[group
[i
]] += cnt
[group
[k
]];
766 group
[group
[k
]] = group
[i
];
768 cnt
[group
[k
]] += cnt
[group
[i
]];
770 group
[group
[i
]] = group
[k
];
777 if (cnt
[0] != nvar
) {
778 /* Extract out pure context constraints separately */
779 Polyhedron
**next
= &F
;
782 *T
= Matrix_Alloc(nvar
, nvar
);
783 for (i
= nparam
? -1 : 0; i
< nvar
; ++i
) {
787 for (j
= 0, k
= 0; j
< P
->NbConstraints
; ++j
)
788 if (rowgroup
[j
] == -1) {
789 if (First_Non_Zero(P
->Constraint
[j
]+1+nvar
,
802 for (j
= 0, k
= 0; j
< P
->NbConstraints
; ++j
)
803 if (rowgroup
[j
] >= 0 && group
[rowgroup
[j
]] == i
) {
810 for (j
= 0; j
< nvar
; ++j
) {
812 for (l
= 0, m
= 0; m
< d
; ++l
) {
815 value_assign((*T
)->p
[j
][tot_d
+m
++], U
->p
[j
][l
]);
819 M
= Matrix_Alloc(k
, d
+nparam
+2);
820 for (j
= 0, k
= 0; j
< P
->NbConstraints
; ++j
) {
822 if (rowgroup
[j
] != i
)
824 value_assign(M
->p
[k
][0], P
->Constraint
[j
][0]);
825 for (l
= 0, m
= 0; m
< d
; ++l
) {
828 value_assign(M
->p
[k
][1+m
++], H
->p
[j
][l
]);
830 Vector_Copy(P
->Constraint
[j
]+1+nvar
, M
->p
[k
]+1+m
, nparam
+1);
833 *next
= Constraints2Polyhedron(M
, NbMaxRays
);
834 next
= &(*next
)->next
;
848 /* Computes the intersection of the contexts of a list of factors */
849 Polyhedron
*Factor_Context(Polyhedron
*F
, unsigned nparam
, unsigned MaxRays
)
852 Polyhedron
*C
= NULL
;
854 for (Q
= F
; Q
; Q
= Q
->next
) {
856 Polyhedron
*next
= Q
->next
;
859 if (Q
->Dimension
!= nparam
)
860 QC
= Polyhedron_Project(Q
, nparam
);
863 C
= Q
== QC
? Polyhedron_Copy(QC
) : QC
;
866 C
= DomainIntersection(C
, QC
, MaxRays
);
877 * Project on final dim dimensions
879 Polyhedron
* Polyhedron_Project(Polyhedron
*P
, int dim
)
882 int remove
= P
->Dimension
- dim
;
886 if (P
->Dimension
== dim
)
887 return Polyhedron_Copy(P
);
889 T
= Matrix_Alloc(dim
+1, P
->Dimension
+1);
890 for (i
= 0; i
< dim
+1; ++i
)
891 value_set_si(T
->p
[i
][i
+remove
], 1);
892 I
= Polyhedron_Image(P
, T
, P
->NbConstraints
);
897 /* Constructs a new constraint that ensures that
898 * the first constraint is (strictly) smaller than
901 static void smaller_constraint(Value
*a
, Value
*b
, Value
*c
, int pos
, int shift
,
902 int len
, int strict
, Value
*tmp
)
904 value_oppose(*tmp
, b
[pos
+1]);
905 value_set_si(c
[0], 1);
906 Vector_Combine(a
+1+shift
, b
+1+shift
, c
+1, *tmp
, a
[pos
+1], len
-shift
-1);
908 value_decrement(c
[len
-shift
-1], c
[len
-shift
-1]);
909 ConstraintSimplify(c
, c
, len
-shift
, tmp
);
913 /* For each pair of lower and upper bounds on the first variable,
914 * calls fn with the set of constraints on the remaining variables
915 * where these bounds are active, i.e., (stricly) larger/smaller than
916 * the other lower/upper bounds, the lower and upper bound and the
919 * If the first variable is equal to an affine combination of the
920 * other variables then fn is called with both lower and upper
921 * pointing to the corresponding equality.
923 * If there is no lower (or upper) bound, then NULL is passed
924 * as the corresponding bound.
926 void for_each_lower_upper_bound(Polyhedron
*P
,
927 for_each_lower_upper_bound_init init
,
928 for_each_lower_upper_bound_fn fn
,
931 unsigned dim
= P
->Dimension
;
938 if (value_zero_p(P
->Constraint
[0][0]) &&
939 value_notzero_p(P
->Constraint
[0][1])) {
940 M
= Matrix_Alloc(P
->NbConstraints
-1, dim
-1+2);
941 for (i
= 1; i
< P
->NbConstraints
; ++i
) {
942 value_assign(M
->p
[i
-1][0], P
->Constraint
[i
][0]);
943 Vector_Copy(P
->Constraint
[i
]+2, M
->p
[i
-1]+1, dim
);
947 fn(M
, P
->Constraint
[0], P
->Constraint
[0], cb_data
);
953 pos
= ALLOCN(int, P
->NbConstraints
);
955 for (i
= 0, z
= 0; i
< P
->NbConstraints
; ++i
)
956 if (value_zero_p(P
->Constraint
[i
][1]))
957 pos
[P
->NbConstraints
-1 - z
++] = i
;
958 /* put those with positive coefficients first; number: p */
959 for (i
= 0, p
= 0, n
= P
->NbConstraints
-z
-1; i
< P
->NbConstraints
; ++i
)
960 if (value_pos_p(P
->Constraint
[i
][1]))
962 else if (value_neg_p(P
->Constraint
[i
][1]))
964 n
= P
->NbConstraints
-z
-p
;
969 M
= Matrix_Alloc((p
? p
-1 : 0) + (n
? n
-1 : 0) + z
+ 1, dim
-1+2);
970 for (i
= 0; i
< z
; ++i
) {
971 value_assign(M
->p
[i
][0], P
->Constraint
[pos
[P
->NbConstraints
-1 - i
]][0]);
972 Vector_Copy(P
->Constraint
[pos
[P
->NbConstraints
-1 - i
]]+2,
975 for (k
= p
? 0 : -1; k
< p
; ++k
) {
976 for (k2
= 0; k2
< p
; ++k2
) {
979 q
= 1 + z
+ k2
- (k2
> k
);
981 P
->Constraint
[pos
[k
]],
982 P
->Constraint
[pos
[k2
]],
983 M
->p
[q
], 0, 1, dim
+2, k2
> k
, &g
);
985 for (l
= n
? p
: p
-1; l
< p
+n
; ++l
) {
988 for (l2
= p
; l2
< p
+n
; ++l2
) {
991 q
= 1 + z
+ l2
-1 - (l2
> l
);
993 P
->Constraint
[pos
[l2
]],
994 P
->Constraint
[pos
[l
]],
995 M
->p
[q
], 0, 1, dim
+2, l2
> l
, &g
);
998 smaller_constraint(P
->Constraint
[pos
[k
]],
999 P
->Constraint
[pos
[l
]],
1000 M
->p
[z
], 0, 1, dim
+2, 0, &g
);
1001 lower
= p
? P
->Constraint
[pos
[k
]] : NULL
;
1002 upper
= n
? P
->Constraint
[pos
[l
]] : NULL
;
1003 fn(M
, lower
, upper
, cb_data
);
1012 struct section
{ Polyhedron
* D
; evalue E
; };
1022 static void PLL_init(unsigned n
, void *cb_data
)
1024 struct PLL_data
*data
= (struct PLL_data
*)cb_data
;
1026 data
->s
= ALLOCN(struct section
, n
);
1029 /* Computes ceil(-coef/abs(d)) */
1030 static evalue
* bv_ceil3(Value
*coef
, int len
, Value d
, Polyhedron
*P
)
1034 Vector
*val
= Vector_Alloc(len
);
1037 Vector_Oppose(coef
, val
->p
, len
);
1038 value_absolute(t
, d
);
1040 EP
= ceiling(val
->p
, t
, len
-1, P
);
1048 static void PLL_cb(Matrix
*M
, Value
*lower
, Value
*upper
, void *cb_data
)
1050 struct PLL_data
*data
= (struct PLL_data
*)cb_data
;
1051 unsigned dim
= M
->NbColumns
-1;
1059 M2
= Matrix_Copy(M
);
1060 T
= Constraints2Polyhedron(M2
, data
->MaxRays
);
1062 data
->s
[data
->nd
].D
= DomainIntersection(T
, data
->C
, data
->MaxRays
);
1065 POL_ENSURE_VERTICES(data
->s
[data
->nd
].D
);
1066 if (emptyQ(data
->s
[data
->nd
].D
)) {
1067 Polyhedron_Free(data
->s
[data
->nd
].D
);
1070 L
= bv_ceil3(lower
+1+1, dim
-1+1, lower
[0+1], data
->s
[data
->nd
].D
);
1071 U
= bv_ceil3(upper
+1+1, dim
-1+1, upper
[0+1], data
->s
[data
->nd
].D
);
1073 eadd(&data
->mone
, U
);
1074 emul(&data
->mone
, U
);
1075 data
->s
[data
->nd
].E
= *U
;
1081 static evalue
*ParamLine_Length_mod(Polyhedron
*P
, Polyhedron
*C
, unsigned MaxRays
)
1083 unsigned dim
= P
->Dimension
;
1084 unsigned nvar
= dim
- C
->Dimension
;
1085 struct PLL_data data
;
1091 value_init(data
.mone
.d
);
1092 evalue_set_si(&data
.mone
, -1, 1);
1095 data
.MaxRays
= MaxRays
;
1097 for_each_lower_upper_bound(P
, PLL_init
, PLL_cb
, &data
);
1099 free_evalue_refs(&data
.mone
);
1103 return evalue_zero();
1108 value_set_si(F
->d
, 0);
1109 F
->x
.p
= new_enode(partition
, 2*data
.nd
, dim
-nvar
);
1110 for (k
= 0; k
< data
.nd
; ++k
) {
1111 EVALUE_SET_DOMAIN(F
->x
.p
->arr
[2*k
], data
.s
[k
].D
);
1112 value_clear(F
->x
.p
->arr
[2*k
+1].d
);
1113 F
->x
.p
->arr
[2*k
+1] = data
.s
[k
].E
;
1120 evalue
* ParamLine_Length(Polyhedron
*P
, Polyhedron
*C
,
1121 struct barvinok_options
*options
)
1124 tmp
= ParamLine_Length_mod(P
, C
, options
->MaxRays
);
1125 if (options
->lookup_table
) {
1126 evalue_mod2table(tmp
, C
->Dimension
);
1132 Bool
isIdentity(Matrix
*M
)
1135 if (M
->NbRows
!= M
->NbColumns
)
1138 for (i
= 0;i
< M
->NbRows
; i
++)
1139 for (j
= 0; j
< M
->NbColumns
; j
++)
1141 if(value_notone_p(M
->p
[i
][j
]))
1144 if(value_notzero_p(M
->p
[i
][j
]))
1150 void Param_Polyhedron_Print(FILE* DST
, Param_Polyhedron
*PP
,
1151 const char **param_names
)
1156 for(P
=PP
->D
;P
;P
=P
->next
) {
1158 /* prints current val. dom. */
1159 fprintf(DST
, "---------------------------------------\n");
1160 fprintf(DST
, "Domain :\n");
1161 Print_Domain(DST
, P
->Domain
, param_names
);
1163 /* scan the vertices */
1164 fprintf(DST
, "Vertices :\n");
1165 FORALL_PVertex_in_ParamPolyhedron(V
,P
,PP
) {
1167 /* prints each vertex */
1168 Print_Vertex(DST
, V
->Vertex
, param_names
);
1171 END_FORALL_PVertex_in_ParamPolyhedron
;
1175 void Enumeration_Print(FILE *Dst
, Enumeration
*en
, const char **params
)
1177 for (; en
; en
= en
->next
) {
1178 Print_Domain(Dst
, en
->ValidityDomain
, params
);
1179 print_evalue(Dst
, &en
->EP
, params
);
1183 void Enumeration_Free(Enumeration
*en
)
1189 free_evalue_refs( &(en
->EP
) );
1190 Domain_Free( en
->ValidityDomain
);
1197 void Enumeration_mod2table(Enumeration
*en
, unsigned nparam
)
1199 for (; en
; en
= en
->next
) {
1200 evalue_mod2table(&en
->EP
, nparam
);
1201 reduce_evalue(&en
->EP
);
1205 size_t Enumeration_size(Enumeration
*en
)
1209 for (; en
; en
= en
->next
) {
1210 s
+= domain_size(en
->ValidityDomain
);
1211 s
+= evalue_size(&en
->EP
);
1216 /* Check whether every set in D2 is included in some set of D1 */
1217 int DomainIncludes(Polyhedron
*D1
, Polyhedron
*D2
)
1219 for ( ; D2
; D2
= D2
->next
) {
1221 for (P1
= D1
; P1
; P1
= P1
->next
)
1222 if (PolyhedronIncludes(P1
, D2
))
1230 int line_minmax(Polyhedron
*I
, Value
*min
, Value
*max
)
1235 value_oppose(I
->Constraint
[0][2], I
->Constraint
[0][2]);
1236 /* There should never be a remainder here */
1237 if (value_pos_p(I
->Constraint
[0][1]))
1238 mpz_fdiv_q(*min
, I
->Constraint
[0][2], I
->Constraint
[0][1]);
1240 mpz_fdiv_q(*min
, I
->Constraint
[0][2], I
->Constraint
[0][1]);
1241 value_assign(*max
, *min
);
1242 } else for (i
= 0; i
< I
->NbConstraints
; ++i
) {
1243 if (value_zero_p(I
->Constraint
[i
][1])) {
1248 value_oppose(I
->Constraint
[i
][2], I
->Constraint
[i
][2]);
1249 if (value_pos_p(I
->Constraint
[i
][1]))
1250 mpz_cdiv_q(*min
, I
->Constraint
[i
][2], I
->Constraint
[i
][1]);
1252 mpz_fdiv_q(*max
, I
->Constraint
[i
][2], I
->Constraint
[i
][1]);
1258 int DomainContains(Polyhedron
*P
, Value
*list_args
, int len
,
1259 unsigned MaxRays
, int set
)
1264 if (P
->Dimension
== len
)
1265 return in_domain(P
, list_args
);
1267 assert(set
); // assume list_args is large enough
1268 assert((P
->Dimension
- len
) % 2 == 0);
1270 for (i
= 0; i
< P
->Dimension
- len
; i
+= 2) {
1272 for (j
= 0 ; j
< P
->NbEq
; ++j
)
1273 if (value_notzero_p(P
->Constraint
[j
][1+len
+i
]))
1275 assert(j
< P
->NbEq
);
1276 value_absolute(m
, P
->Constraint
[j
][1+len
+i
]);
1277 k
= First_Non_Zero(P
->Constraint
[j
]+1, len
);
1279 assert(First_Non_Zero(P
->Constraint
[j
]+1+k
+1, len
- k
- 1) == -1);
1280 mpz_fdiv_q(list_args
[len
+i
], list_args
[k
], m
);
1281 mpz_fdiv_r(list_args
[len
+i
+1], list_args
[k
], m
);
1285 return in_domain(P
, list_args
);
1288 Polyhedron
*DomainConcat(Polyhedron
*head
, Polyhedron
*tail
)
1293 for (S
= head
; S
->next
; S
= S
->next
)
1299 evalue
*barvinok_lexsmaller_ev(Polyhedron
*P
, Polyhedron
*D
, unsigned dim
,
1300 Polyhedron
*C
, unsigned MaxRays
)
1303 Polyhedron
*RC
, *RD
, *Q
;
1304 unsigned nparam
= dim
+ C
->Dimension
;
1308 RC
= LexSmaller(P
, D
, dim
, C
, MaxRays
);
1312 exist
= RD
->Dimension
- nparam
- dim
;
1313 CA
= align_context(RC
, RD
->Dimension
, MaxRays
);
1314 Q
= DomainIntersection(RD
, CA
, MaxRays
);
1315 Polyhedron_Free(CA
);
1317 Polyhedron_Free(RC
);
1320 for (Q
= RD
; Q
; Q
= Q
->next
) {
1322 Polyhedron
*next
= Q
->next
;
1325 t
= barvinok_enumerate_e(Q
, exist
, nparam
, MaxRays
);
1342 Enumeration
*barvinok_lexsmaller(Polyhedron
*P
, Polyhedron
*D
, unsigned dim
,
1343 Polyhedron
*C
, unsigned MaxRays
)
1345 evalue
*EP
= barvinok_lexsmaller_ev(P
, D
, dim
, C
, MaxRays
);
1347 return partition2enumeration(EP
);
1350 /* "align" matrix to have nrows by inserting
1351 * the necessary number of rows and an equal number of columns in front
1353 Matrix
*align_matrix(Matrix
*M
, int nrows
)
1356 int newrows
= nrows
- M
->NbRows
;
1357 Matrix
*M2
= Matrix_Alloc(nrows
, newrows
+ M
->NbColumns
);
1358 for (i
= 0; i
< newrows
; ++i
)
1359 value_set_si(M2
->p
[i
][i
], 1);
1360 for (i
= 0; i
< M
->NbRows
; ++i
)
1361 Vector_Copy(M
->p
[i
], M2
->p
[newrows
+i
]+newrows
, M
->NbColumns
);
1365 static void print_varlist(FILE *out
, int n
, char **names
)
1369 for (i
= 0; i
< n
; ++i
) {
1372 fprintf(out
, "%s", names
[i
]);
1377 static void print_term(FILE *out
, Value v
, int pos
, int dim
, int nparam
,
1378 char **iter_names
, char **param_names
, int *first
)
1380 if (value_zero_p(v
)) {
1381 if (first
&& *first
&& pos
>= dim
+ nparam
)
1387 if (!*first
&& value_pos_p(v
))
1391 if (pos
< dim
+ nparam
) {
1392 if (value_mone_p(v
))
1394 else if (!value_one_p(v
))
1395 value_print(out
, VALUE_FMT
, v
);
1397 fprintf(out
, "%s", iter_names
[pos
]);
1399 fprintf(out
, "%s", param_names
[pos
-dim
]);
1401 value_print(out
, VALUE_FMT
, v
);
1404 char **util_generate_names(int n
, const char *prefix
)
1407 int len
= (prefix
? strlen(prefix
) : 0) + 10;
1408 char **names
= ALLOCN(char*, n
);
1410 fprintf(stderr
, "ERROR: memory overflow.\n");
1413 for (i
= 0; i
< n
; ++i
) {
1414 names
[i
] = ALLOCN(char, len
);
1416 fprintf(stderr
, "ERROR: memory overflow.\n");
1420 snprintf(names
[i
], len
, "%d", i
);
1422 snprintf(names
[i
], len
, "%s%d", prefix
, i
);
1428 void util_free_names(int n
, char **names
)
1431 for (i
= 0; i
< n
; ++i
)
1436 void Polyhedron_pprint(FILE *out
, Polyhedron
*P
, int dim
, int nparam
,
1437 char **iter_names
, char **param_names
)
1442 assert(dim
+ nparam
== P
->Dimension
);
1448 print_varlist(out
, nparam
, param_names
);
1449 fprintf(out
, " -> ");
1451 print_varlist(out
, dim
, iter_names
);
1452 fprintf(out
, " : ");
1455 fprintf(out
, "FALSE");
1456 else for (i
= 0; i
< P
->NbConstraints
; ++i
) {
1458 int v
= First_Non_Zero(P
->Constraint
[i
]+1, P
->Dimension
);
1459 if (v
== -1 && value_pos_p(P
->Constraint
[i
][0]))
1462 fprintf(out
, " && ");
1463 if (v
== -1 && value_notzero_p(P
->Constraint
[i
][1+P
->Dimension
]))
1464 fprintf(out
, "FALSE");
1465 else if (value_pos_p(P
->Constraint
[i
][v
+1])) {
1466 print_term(out
, P
->Constraint
[i
][v
+1], v
, dim
, nparam
,
1467 iter_names
, param_names
, NULL
);
1468 if (value_zero_p(P
->Constraint
[i
][0]))
1469 fprintf(out
, " = ");
1471 fprintf(out
, " >= ");
1472 for (j
= v
+1; j
<= dim
+nparam
; ++j
) {
1473 value_oppose(tmp
, P
->Constraint
[i
][1+j
]);
1474 print_term(out
, tmp
, j
, dim
, nparam
,
1475 iter_names
, param_names
, &first
);
1478 value_oppose(tmp
, P
->Constraint
[i
][1+v
]);
1479 print_term(out
, tmp
, v
, dim
, nparam
,
1480 iter_names
, param_names
, NULL
);
1481 fprintf(out
, " <= ");
1482 for (j
= v
+1; j
<= dim
+nparam
; ++j
)
1483 print_term(out
, P
->Constraint
[i
][1+j
], j
, dim
, nparam
,
1484 iter_names
, param_names
, &first
);
1488 fprintf(out
, " }\n");
1493 /* Construct a cone over P with P placed at x_d = 1, with
1494 * x_d the coordinate of an extra dimension
1496 * It's probably a mistake to depend so much on the internal
1497 * representation. We should probably simply compute the
1498 * vertices/facets first.
1500 Polyhedron
*Cone_over_Polyhedron(Polyhedron
*P
)
1502 unsigned NbConstraints
= 0;
1503 unsigned NbRays
= 0;
1507 if (POL_HAS(P
, POL_INEQUALITIES
))
1508 NbConstraints
= P
->NbConstraints
+ 1;
1509 if (POL_HAS(P
, POL_POINTS
))
1510 NbRays
= P
->NbRays
+ 1;
1512 C
= Polyhedron_Alloc(P
->Dimension
+1, NbConstraints
, NbRays
);
1513 if (POL_HAS(P
, POL_INEQUALITIES
)) {
1515 for (i
= 0; i
< P
->NbConstraints
; ++i
)
1516 Vector_Copy(P
->Constraint
[i
], C
->Constraint
[i
], P
->Dimension
+2);
1518 value_set_si(C
->Constraint
[P
->NbConstraints
][0], 1);
1519 value_set_si(C
->Constraint
[P
->NbConstraints
][1+P
->Dimension
], 1);
1521 if (POL_HAS(P
, POL_POINTS
)) {
1522 C
->NbBid
= P
->NbBid
;
1523 for (i
= 0; i
< P
->NbRays
; ++i
)
1524 Vector_Copy(P
->Ray
[i
], C
->Ray
[i
], P
->Dimension
+2);
1526 value_set_si(C
->Ray
[P
->NbRays
][0], 1);
1527 value_set_si(C
->Ray
[P
->NbRays
][1+C
->Dimension
], 1);
1529 POL_SET(C
, POL_VALID
);
1530 if (POL_HAS(P
, POL_INEQUALITIES
))
1531 POL_SET(C
, POL_INEQUALITIES
);
1532 if (POL_HAS(P
, POL_POINTS
))
1533 POL_SET(C
, POL_POINTS
);
1534 if (POL_HAS(P
, POL_VERTICES
))
1535 POL_SET(C
, POL_VERTICES
);
1539 /* Returns a (dim+nparam+1)x((dim-n)+nparam+1) matrix
1540 * mapping the transformed subspace back to the original space.
1541 * n is the number of equalities involving the variables
1542 * (i.e., not purely the parameters).
1543 * The remaining n coordinates in the transformed space would
1544 * have constant (parametric) values and are therefore not
1545 * included in the variables of the new space.
1547 Matrix
*compress_variables(Matrix
*Equalities
, unsigned nparam
)
1549 unsigned dim
= (Equalities
->NbColumns
-2) - nparam
;
1550 Matrix
*M
, *H
, *Q
, *U
, *C
, *ratH
, *invH
, *Ul
, *T1
, *T2
, *T
;
1555 for (n
= 0; n
< Equalities
->NbRows
; ++n
)
1556 if (First_Non_Zero(Equalities
->p
[n
]+1, dim
) == -1)
1559 return Identity(dim
+nparam
+1);
1561 value_set_si(mone
, -1);
1562 M
= Matrix_Alloc(n
, dim
);
1563 C
= Matrix_Alloc(n
+1, nparam
+1);
1564 for (i
= 0; i
< n
; ++i
) {
1565 Vector_Copy(Equalities
->p
[i
]+1, M
->p
[i
], dim
);
1566 Vector_Scale(Equalities
->p
[i
]+1+dim
, C
->p
[i
], mone
, nparam
+1);
1568 value_set_si(C
->p
[n
][nparam
], 1);
1569 left_hermite(M
, &H
, &Q
, &U
);
1574 ratH
= Matrix_Alloc(n
+1, n
+1);
1575 invH
= Matrix_Alloc(n
+1, n
+1);
1576 for (i
= 0; i
< n
; ++i
)
1577 Vector_Copy(H
->p
[i
], ratH
->p
[i
], n
);
1578 value_set_si(ratH
->p
[n
][n
], 1);
1579 ok
= Matrix_Inverse(ratH
, invH
);
1583 T1
= Matrix_Alloc(n
+1, nparam
+1);
1584 Matrix_Product(invH
, C
, T1
);
1587 if (value_notone_p(T1
->p
[n
][nparam
])) {
1588 for (i
= 0; i
< n
; ++i
) {
1589 if (!mpz_divisible_p(T1
->p
[i
][nparam
], T1
->p
[n
][nparam
])) {
1594 /* compress_params should have taken care of this */
1595 for (j
= 0; j
< nparam
; ++j
)
1596 assert(mpz_divisible_p(T1
->p
[i
][j
], T1
->p
[n
][nparam
]));
1597 Vector_AntiScale(T1
->p
[i
], T1
->p
[i
], T1
->p
[n
][nparam
], nparam
+1);
1599 value_set_si(T1
->p
[n
][nparam
], 1);
1601 Ul
= Matrix_Alloc(dim
+1, n
+1);
1602 for (i
= 0; i
< dim
; ++i
)
1603 Vector_Copy(U
->p
[i
], Ul
->p
[i
], n
);
1604 value_set_si(Ul
->p
[dim
][n
], 1);
1605 T2
= Matrix_Alloc(dim
+1, nparam
+1);
1606 Matrix_Product(Ul
, T1
, T2
);
1610 T
= Matrix_Alloc(dim
+nparam
+1, (dim
-n
)+nparam
+1);
1611 for (i
= 0; i
< dim
; ++i
) {
1612 Vector_Copy(U
->p
[i
]+n
, T
->p
[i
], dim
-n
);
1613 Vector_Copy(T2
->p
[i
], T
->p
[i
]+dim
-n
, nparam
+1);
1615 for (i
= 0; i
< nparam
+1; ++i
)
1616 value_set_si(T
->p
[dim
+i
][(dim
-n
)+i
], 1);
1617 assert(value_one_p(T2
->p
[dim
][nparam
]));
1624 /* Computes the left inverse of an affine embedding M and, if Eq is not NULL,
1625 * the equalities that define the affine subspace onto which M maps
1628 Matrix
*left_inverse(Matrix
*M
, Matrix
**Eq
)
1631 Matrix
*L
, *H
, *Q
, *U
, *ratH
, *invH
, *Ut
, *inv
;
1634 if (M
->NbColumns
== 1) {
1635 inv
= Matrix_Alloc(1, M
->NbRows
);
1636 value_set_si(inv
->p
[0][M
->NbRows
-1], 1);
1638 *Eq
= Matrix_Alloc(M
->NbRows
-1, 1+(M
->NbRows
-1)+1);
1639 for (i
= 0; i
< M
->NbRows
-1; ++i
) {
1640 value_oppose((*Eq
)->p
[i
][1+i
], M
->p
[M
->NbRows
-1][0]);
1641 value_assign((*Eq
)->p
[i
][1+(M
->NbRows
-1)], M
->p
[i
][0]);
1648 L
= Matrix_Alloc(M
->NbRows
-1, M
->NbColumns
-1);
1649 for (i
= 0; i
< L
->NbRows
; ++i
)
1650 Vector_Copy(M
->p
[i
], L
->p
[i
], L
->NbColumns
);
1651 right_hermite(L
, &H
, &U
, &Q
);
1654 t
= Vector_Alloc(U
->NbColumns
);
1655 for (i
= 0; i
< U
->NbColumns
; ++i
)
1656 value_oppose(t
->p
[i
], M
->p
[i
][M
->NbColumns
-1]);
1658 *Eq
= Matrix_Alloc(H
->NbRows
- H
->NbColumns
, 2 + U
->NbColumns
);
1659 for (i
= 0; i
< H
->NbRows
- H
->NbColumns
; ++i
) {
1660 Vector_Copy(U
->p
[H
->NbColumns
+i
], (*Eq
)->p
[i
]+1, U
->NbColumns
);
1661 Inner_Product(U
->p
[H
->NbColumns
+i
], t
->p
, U
->NbColumns
,
1662 (*Eq
)->p
[i
]+1+U
->NbColumns
);
1665 ratH
= Matrix_Alloc(H
->NbColumns
+1, H
->NbColumns
+1);
1666 invH
= Matrix_Alloc(H
->NbColumns
+1, H
->NbColumns
+1);
1667 for (i
= 0; i
< H
->NbColumns
; ++i
)
1668 Vector_Copy(H
->p
[i
], ratH
->p
[i
], H
->NbColumns
);
1669 value_set_si(ratH
->p
[ratH
->NbRows
-1][ratH
->NbColumns
-1], 1);
1671 ok
= Matrix_Inverse(ratH
, invH
);
1674 Ut
= Matrix_Alloc(invH
->NbRows
, U
->NbColumns
+1);
1675 for (i
= 0; i
< Ut
->NbRows
-1; ++i
) {
1676 Vector_Copy(U
->p
[i
], Ut
->p
[i
], U
->NbColumns
);
1677 Inner_Product(U
->p
[i
], t
->p
, U
->NbColumns
, &Ut
->p
[i
][Ut
->NbColumns
-1]);
1681 value_set_si(Ut
->p
[Ut
->NbRows
-1][Ut
->NbColumns
-1], 1);
1682 inv
= Matrix_Alloc(invH
->NbRows
, Ut
->NbColumns
);
1683 Matrix_Product(invH
, Ut
, inv
);
1689 /* Check whether all rays are revlex positive in the parameters
1691 int Polyhedron_has_revlex_positive_rays(Polyhedron
*P
, unsigned nparam
)
1694 for (r
= 0; r
< P
->NbRays
; ++r
) {
1696 if (value_notzero_p(P
->Ray
[r
][P
->Dimension
+1]))
1698 for (i
= P
->Dimension
-1; i
>= P
->Dimension
-nparam
; --i
) {
1699 if (value_neg_p(P
->Ray
[r
][i
+1]))
1701 if (value_pos_p(P
->Ray
[r
][i
+1]))
1704 /* A ray independent of the parameters */
1705 if (i
< P
->Dimension
-nparam
)
1711 static Polyhedron
*Recession_Cone(Polyhedron
*P
, unsigned nparam
, unsigned MaxRays
)
1714 unsigned nvar
= P
->Dimension
- nparam
;
1715 Matrix
*M
= Matrix_Alloc(P
->NbConstraints
, 1 + nvar
+ 1);
1717 for (i
= 0; i
< P
->NbConstraints
; ++i
)
1718 Vector_Copy(P
->Constraint
[i
], M
->p
[i
], 1+nvar
);
1719 R
= Constraints2Polyhedron(M
, MaxRays
);
1724 int Polyhedron_is_unbounded(Polyhedron
*P
, unsigned nparam
, unsigned MaxRays
)
1728 Polyhedron
*R
= Recession_Cone(P
, nparam
, MaxRays
);
1729 POL_ENSURE_VERTICES(R
);
1731 for (i
= 0; i
< R
->NbRays
; ++i
)
1732 if (value_zero_p(R
->Ray
[i
][1+R
->Dimension
]))
1734 is_unbounded
= R
->NbBid
> 0 || i
< R
->NbRays
;
1736 return is_unbounded
;
1739 static void SwapColumns(Value
**V
, int n
, int i
, int j
)
1743 for (r
= 0; r
< n
; ++r
)
1744 value_swap(V
[r
][i
], V
[r
][j
]);
1747 void Polyhedron_ExchangeColumns(Polyhedron
*P
, int Column1
, int Column2
)
1749 SwapColumns(P
->Constraint
, P
->NbConstraints
, Column1
, Column2
);
1750 SwapColumns(P
->Ray
, P
->NbRays
, Column1
, Column2
);
1753 Polyhedron_Matrix_View(P
, &M
, P
->NbConstraints
);
1754 Gauss(&M
, P
->NbEq
, P
->Dimension
+1);
1758 /* perform transposition inline; assumes M is a square matrix */
1759 void Matrix_Transposition(Matrix
*M
)
1763 assert(M
->NbRows
== M
->NbColumns
);
1764 for (i
= 0; i
< M
->NbRows
; ++i
)
1765 for (j
= i
+1; j
< M
->NbColumns
; ++j
)
1766 value_swap(M
->p
[i
][j
], M
->p
[j
][i
]);
1769 /* Matrix "view" of first rows rows */
1770 void Polyhedron_Matrix_View(Polyhedron
*P
, Matrix
*M
, unsigned rows
)
1773 M
->NbColumns
= P
->Dimension
+2;
1774 M
->p_Init
= P
->p_Init
;
1775 M
->p
= P
->Constraint
;
1778 int Last_Non_Zero(Value
*p
, unsigned len
)
1782 for (i
= len
- 1; i
>= 0; --i
)
1783 if (value_notzero_p(p
[i
]))