6 #include <isl/val_gmp.h>
9 #include <isl_set_polylib.h>
10 #include <barvinok/polylib.h>
11 #include <barvinok/options.h>
12 #include <polylib/ranking.h>
13 #include "lattice_point.h"
15 #define ALLOC(type) (type*)malloc(sizeof(type))
16 #define ALLOCN(type,n) (type*)malloc((n) * sizeof(type))
19 #define NALLOC(p,n) p = (typeof(p))malloc((n) * sizeof(*p))
21 #define NALLOC(p,n) p = (void *)malloc((n) * sizeof(*p))
24 void manual_count(Polyhedron
*P
, Value
* result
)
26 isl_ctx
*ctx
= isl_ctx_alloc();
30 int nvar
= P
->Dimension
;
32 dim
= isl_space_set_alloc(ctx
, 0, nvar
);
33 set
= isl_set_new_from_polylib(P
, dim
);
35 v
= isl_set_count_val(set
);
36 isl_val_get_num_gmp(v
, *result
);
45 #include <barvinok/evalue.h>
46 #include <barvinok/util.h>
47 #include <barvinok/barvinok.h>
49 /* Return random value between 0 and max-1 inclusive
51 int random_int(int max
) {
52 return (int) (((double)(max
))*rand()/(RAND_MAX
+1.0));
55 Polyhedron
*Polyhedron_Read(unsigned MaxRays
)
58 unsigned NbRows
, NbColumns
;
63 while (fgets(s
, sizeof(s
), stdin
)) {
66 if (strncasecmp(s
, "vertices", sizeof("vertices")-1) == 0)
68 if (sscanf(s
, "%u %u", &NbRows
, &NbColumns
) == 2)
73 M
= Matrix_Alloc(NbRows
,NbColumns
);
76 P
= Rays2Polyhedron(M
, MaxRays
);
78 P
= Constraints2Polyhedron(M
, MaxRays
);
83 /* Inplace polarization
85 void Polyhedron_Polarize(Polyhedron
*P
)
92 POL_ENSURE_VERTICES(P
);
93 NbRows
= P
->NbConstraints
+ P
->NbRays
;
94 q
= (Value
**)malloc(NbRows
* sizeof(Value
*));
96 for (i
= 0; i
< P
->NbRays
; ++i
)
98 for (; i
< NbRows
; ++i
)
99 q
[i
] = P
->Constraint
[i
-P
->NbRays
];
100 P
->NbConstraints
= NbRows
- P
->NbConstraints
;
101 P
->NbRays
= NbRows
- P
->NbRays
;
104 P
->Ray
= q
+ P
->NbConstraints
;
108 * Rather general polar
109 * We can optimize it significantly if we assume that
112 * Also, we calculate the polar as defined in Schrijver
113 * The opposite should probably work as well and would
114 * eliminate the need for multiplying by -1
116 Polyhedron
* Polyhedron_Polar(Polyhedron
*P
, unsigned NbMaxRays
)
120 unsigned dim
= P
->Dimension
+ 2;
121 Matrix
*M
= Matrix_Alloc(P
->NbRays
, dim
);
125 value_set_si(mone
, -1);
126 for (i
= 0; i
< P
->NbRays
; ++i
) {
127 Vector_Scale(P
->Ray
[i
], M
->p
[i
], mone
, dim
);
128 value_multiply(M
->p
[i
][0], M
->p
[i
][0], mone
);
129 value_multiply(M
->p
[i
][dim
-1], M
->p
[i
][dim
-1], mone
);
131 P
= Constraints2Polyhedron(M
, NbMaxRays
);
139 * Returns the supporting cone of P at the vertex with index v
141 Polyhedron
* supporting_cone(Polyhedron
*P
, int v
)
146 unsigned char *supporting
= (unsigned char *)malloc(P
->NbConstraints
);
147 unsigned dim
= P
->Dimension
+ 2;
149 assert(v
>=0 && v
< P
->NbRays
);
150 assert(value_pos_p(P
->Ray
[v
][dim
-1]));
154 for (i
= 0, n
= 0; i
< P
->NbConstraints
; ++i
) {
155 Inner_Product(P
->Constraint
[i
] + 1, P
->Ray
[v
] + 1, dim
- 1, &tmp
);
156 if ((supporting
[i
] = value_zero_p(tmp
)))
159 assert(n
>= dim
- 2);
161 M
= Matrix_Alloc(n
, dim
);
163 for (i
= 0, j
= 0; i
< P
->NbConstraints
; ++i
)
165 value_set_si(M
->p
[j
][dim
-1], 0);
166 Vector_Copy(P
->Constraint
[i
], M
->p
[j
++], dim
-1);
169 P
= Constraints2Polyhedron(M
, P
->NbRays
+1);
175 #define INT_BITS (sizeof(unsigned) * 8)
177 unsigned *supporting_constraints(Matrix
*Constraints
, Param_Vertices
*v
, int *n
)
179 Value lcm
, tmp
, tmp2
;
180 unsigned dim
= Constraints
->NbColumns
;
181 unsigned nparam
= v
->Vertex
->NbColumns
- 2;
182 unsigned nvar
= dim
- nparam
- 2;
183 int len
= (Constraints
->NbRows
+INT_BITS
-1)/INT_BITS
;
184 unsigned *supporting
= (unsigned *)calloc(len
, sizeof(unsigned));
191 row
= Vector_Alloc(nparam
+1);
196 value_set_si(lcm
, 1);
197 for (i
= 0, *n
= 0, ix
= 0, bx
= MSB
; i
< Constraints
->NbRows
; ++i
) {
198 Vector_Set(row
->p
, 0, nparam
+1);
199 for (j
= 0 ; j
< nvar
; ++j
) {
200 value_set_si(tmp
, 1);
201 value_assign(tmp2
, Constraints
->p
[i
][j
+1]);
202 if (value_ne(lcm
, v
->Vertex
->p
[j
][nparam
+1])) {
203 value_assign(tmp
, lcm
);
204 value_lcm(lcm
, lcm
, v
->Vertex
->p
[j
][nparam
+1]);
205 value_division(tmp
, lcm
, tmp
);
206 value_multiply(tmp2
, tmp2
, lcm
);
207 value_division(tmp2
, tmp2
, v
->Vertex
->p
[j
][nparam
+1]);
209 Vector_Combine(row
->p
, v
->Vertex
->p
[j
], row
->p
,
210 tmp
, tmp2
, nparam
+1);
212 value_set_si(tmp
, 1);
213 Vector_Combine(row
->p
, Constraints
->p
[i
]+1+nvar
, row
->p
, tmp
, lcm
, nparam
+1);
214 for (j
= 0; j
< nparam
+1; ++j
)
215 if (value_notzero_p(row
->p
[j
]))
217 if (j
== nparam
+ 1) {
218 supporting
[ix
] |= bx
;
232 Polyhedron
* supporting_cone_p(Polyhedron
*P
, Param_Vertices
*v
)
235 unsigned dim
= P
->Dimension
+ 2;
236 unsigned nparam
= v
->Vertex
->NbColumns
- 2;
237 unsigned nvar
= dim
- nparam
- 2;
241 unsigned *supporting
;
244 Polyhedron_Matrix_View(P
, &View
, P
->NbConstraints
);
245 supporting
= supporting_constraints(&View
, v
, &n
);
246 M
= Matrix_Alloc(n
, nvar
+2);
248 for (i
= 0, j
= 0, ix
= 0, bx
= MSB
; i
< P
->NbConstraints
; ++i
) {
249 if (supporting
[ix
] & bx
) {
250 value_set_si(M
->p
[j
][nvar
+1], 0);
251 Vector_Copy(P
->Constraint
[i
], M
->p
[j
++], nvar
+1);
256 P
= Constraints2Polyhedron(M
, P
->NbRays
+1);
262 Polyhedron
* triangulate_cone(Polyhedron
*P
, unsigned NbMaxCons
)
264 struct barvinok_options
*options
= barvinok_options_new_with_defaults();
265 options
->MaxRays
= NbMaxCons
;
266 P
= triangulate_cone_with_options(P
, options
);
267 barvinok_options_free(options
);
271 Polyhedron
* triangulate_cone_with_options(Polyhedron
*P
,
272 struct barvinok_options
*options
)
274 const static int MAX_TRY
=10;
277 unsigned dim
= P
->Dimension
;
278 Matrix
*M
= Matrix_Alloc(P
->NbRays
+1, dim
+3);
280 Polyhedron
*L
, *R
, *T
;
281 assert(P
->NbEq
== 0);
287 Vector_Set(M
->p
[0]+1, 0, dim
+1);
288 value_set_si(M
->p
[0][0], 1);
289 value_set_si(M
->p
[0][dim
+2], 1);
290 Vector_Set(M
->p
[P
->NbRays
]+1, 0, dim
+2);
291 value_set_si(M
->p
[P
->NbRays
][0], 1);
292 value_set_si(M
->p
[P
->NbRays
][dim
+1], 1);
294 for (i
= 0, r
= 1; i
< P
->NbRays
; ++i
) {
295 if (value_notzero_p(P
->Ray
[i
][dim
+1]))
297 Vector_Copy(P
->Ray
[i
], M
->p
[r
], dim
+1);
298 value_set_si(M
->p
[r
][dim
+2], 0);
302 M2
= Matrix_Alloc(dim
+1, dim
+2);
305 if (options
->try_Delaunay_triangulation
) {
306 /* Delaunay triangulation */
307 for (r
= 1; r
< P
->NbRays
; ++r
) {
308 Inner_Product(M
->p
[r
]+1, M
->p
[r
]+1, dim
, &tmp
);
309 value_assign(M
->p
[r
][dim
+1], tmp
);
312 L
= Rays2Polyhedron(M3
, options
->MaxRays
);
317 /* Usually R should still be 0 */
320 for (r
= 1; r
< P
->NbRays
; ++r
) {
321 value_set_si(M
->p
[r
][dim
+1], random_int((t
+1)*dim
*P
->NbRays
)+1);
324 L
= Rays2Polyhedron(M3
, options
->MaxRays
);
328 assert(t
<= MAX_TRY
);
333 POL_ENSURE_FACETS(L
);
334 for (i
= 0; i
< L
->NbConstraints
; ++i
) {
335 /* Ignore perpendicular facets, i.e., facets with 0 z-coordinate */
336 if (value_negz_p(L
->Constraint
[i
][dim
+1]))
338 if (value_notzero_p(L
->Constraint
[i
][dim
+2]))
340 for (j
= 1, r
= 1; j
< M
->NbRows
; ++j
) {
341 Inner_Product(M
->p
[j
]+1, L
->Constraint
[i
]+1, dim
+1, &tmp
);
342 if (value_notzero_p(tmp
))
346 Vector_Copy(M
->p
[j
]+1, M2
->p
[r
]+1, dim
);
347 value_set_si(M2
->p
[r
][0], 1);
348 value_set_si(M2
->p
[r
][dim
+1], 0);
352 Vector_Set(M2
->p
[0]+1, 0, dim
);
353 value_set_si(M2
->p
[0][0], 1);
354 value_set_si(M2
->p
[0][dim
+1], 1);
355 T
= Rays2Polyhedron(M2
, P
->NbConstraints
+1);
369 void check_triangulization(Polyhedron
*P
, Polyhedron
*T
)
371 Polyhedron
*C
, *D
, *E
, *F
, *G
, *U
;
372 for (C
= T
; C
; C
= C
->next
) {
376 U
= DomainConvex(DomainUnion(U
, C
, 100), 100);
377 for (D
= C
->next
; D
; D
= D
->next
) {
382 E
= DomainIntersection(C
, D
, 600);
383 assert(E
->NbRays
== 0 || E
->NbEq
>= 1);
389 assert(PolyhedronIncludes(U
, P
));
390 assert(PolyhedronIncludes(P
, U
));
393 /* Computes x, y and g such that g = gcd(a,b) and a*x+b*y = g */
394 void Extended_Euclid(Value a
, Value b
, Value
*x
, Value
*y
, Value
*g
)
396 Value c
, d
, e
, f
, tmp
;
403 value_absolute(c
, a
);
404 value_absolute(d
, b
);
407 while(value_pos_p(d
)) {
408 value_division(tmp
, c
, d
);
409 value_multiply(tmp
, tmp
, f
);
410 value_subtract(e
, e
, tmp
);
411 value_division(tmp
, c
, d
);
412 value_multiply(tmp
, tmp
, d
);
413 value_subtract(c
, c
, tmp
);
420 else if (value_pos_p(a
))
422 else value_oppose(*x
, e
);
426 value_multiply(tmp
, a
, *x
);
427 value_subtract(tmp
, c
, tmp
);
428 value_division(*y
, tmp
, b
);
437 static int unimodular_complete_1(Matrix
*m
)
439 Value g
, b
, c
, old
, tmp
;
448 value_assign(g
, m
->p
[0][0]);
449 for (i
= 1; value_zero_p(g
) && i
< m
->NbColumns
; ++i
) {
450 for (j
= 0; j
< m
->NbColumns
; ++j
) {
452 value_set_si(m
->p
[i
][j
], 1);
454 value_set_si(m
->p
[i
][j
], 0);
456 value_assign(g
, m
->p
[0][i
]);
458 for (; i
< m
->NbColumns
; ++i
) {
459 value_assign(old
, g
);
460 Extended_Euclid(old
, m
->p
[0][i
], &c
, &b
, &g
);
462 for (j
= 0; j
< m
->NbColumns
; ++j
) {
464 value_multiply(tmp
, m
->p
[0][j
], b
);
465 value_division(m
->p
[i
][j
], tmp
, old
);
467 value_assign(m
->p
[i
][j
], c
);
469 value_set_si(m
->p
[i
][j
], 0);
481 int unimodular_complete(Matrix
*M
, int row
)
488 return unimodular_complete_1(M
);
490 left_hermite(M
, &H
, &Q
, &U
);
492 for (r
= 0; ok
&& r
< row
; ++r
)
493 if (value_notone_p(H
->p
[r
][r
]))
496 for (r
= row
; r
< M
->NbRows
; ++r
)
497 Vector_Copy(Q
->p
[r
], M
->p
[r
], M
->NbColumns
);
503 * left_hermite may leave positive entries below the main diagonal in H.
504 * This function postprocesses the output of left_hermite to make
505 * the non-zero entries below the main diagonal negative.
507 void neg_left_hermite(Matrix
*A
, Matrix
**H_p
, Matrix
**Q_p
, Matrix
**U_p
)
512 left_hermite(A
, &H
, &Q
, &U
);
517 for (row
= 0, col
= 0; col
< H
->NbColumns
; ++col
, ++row
) {
518 while (value_zero_p(H
->p
[row
][col
]))
520 for (i
= 0; i
< col
; ++i
) {
521 if (value_negz_p(H
->p
[row
][i
]))
524 /* subtract column col from column i in H and U */
525 for (j
= 0; j
< H
->NbRows
; ++j
)
526 value_subtract(H
->p
[j
][i
], H
->p
[j
][i
], H
->p
[j
][col
]);
527 for (j
= 0; j
< U
->NbRows
; ++j
)
528 value_subtract(U
->p
[j
][i
], U
->p
[j
][i
], U
->p
[j
][col
]);
530 /* add row i to row col in Q */
531 for (j
= 0; j
< Q
->NbColumns
; ++j
)
532 value_addto(Q
->p
[col
][j
], Q
->p
[col
][j
], Q
->p
[i
][j
]);
538 * Returns a full-dimensional polyhedron with the same number
539 * of integer points as P
541 Polyhedron
*remove_equalities(Polyhedron
*P
, unsigned MaxRays
)
545 Polyhedron
*Q
= Polyhedron_Copy(P
);
550 Q
= DomainConstraintSimplify(Q
, MaxRays
);
554 Polyhedron_Matrix_View(Q
, &M
, Q
->NbEq
);
555 T
= compress_variables(&M
, 0);
560 P
= Polyhedron_Preimage(Q
, T
, MaxRays
);
570 * Returns a full-dimensional polyhedron with the same number
571 * of integer points as P
572 * nvar specifies the number of variables
573 * The remaining dimensions are assumed to be parameters
575 * factor is NbEq x (nparam+2) matrix, containing stride constraints
576 * on the parameters; column nparam is the constant;
577 * column nparam+1 is the stride
579 * if factor is NULL, only remove equalities that don't affect
580 * the number of points
582 Polyhedron
*remove_equalities_p(Polyhedron
*P
, unsigned nvar
, Matrix
**factor
,
587 unsigned dim
= P
->Dimension
;
594 m1
= Matrix_Alloc(nvar
, nvar
);
595 P
= DomainConstraintSimplify(P
, MaxRays
);
597 f
= Matrix_Alloc(P
->NbEq
, dim
-nvar
+2);
601 for (i
= 0, j
= 0; i
< P
->NbEq
; ++i
) {
602 if (First_Non_Zero(P
->Constraint
[i
]+1, nvar
) == -1)
605 Vector_Gcd(P
->Constraint
[i
]+1, nvar
, &g
);
606 if (!factor
&& value_notone_p(g
))
610 Vector_Copy(P
->Constraint
[i
]+1+nvar
, f
->p
[j
], dim
-nvar
+1);
611 value_assign(f
->p
[j
][dim
-nvar
+1], g
);
614 Vector_Copy(P
->Constraint
[i
]+1, m1
->p
[j
], nvar
);
620 unimodular_complete(m1
, j
);
622 m2
= Matrix_Alloc(dim
+1-j
, dim
+1);
623 for (i
= 0; i
< nvar
-j
; ++i
)
624 Vector_Copy(m1
->p
[i
+j
], m2
->p
[i
], nvar
);
626 for (i
= nvar
-j
; i
<= dim
-j
; ++i
)
627 value_set_si(m2
->p
[i
][i
+j
], 1);
629 Q
= Polyhedron_Image(P
, m2
, MaxRays
);
636 void Line_Length(Polyhedron
*P
, Value
*len
)
642 assert(P
->Dimension
== 1);
645 if (mpz_divisible_p(P
->Constraint
[0][2], P
->Constraint
[0][1]))
646 value_set_si(*len
, 1);
648 value_set_si(*len
, 0);
656 for (i
= 0; i
< P
->NbConstraints
; ++i
) {
657 value_oppose(tmp
, P
->Constraint
[i
][2]);
658 if (value_pos_p(P
->Constraint
[i
][1])) {
659 mpz_cdiv_q(tmp
, tmp
, P
->Constraint
[i
][1]);
660 if (!p
|| value_gt(tmp
, pos
))
661 value_assign(pos
, tmp
);
663 } else if (value_neg_p(P
->Constraint
[i
][1])) {
664 mpz_fdiv_q(tmp
, tmp
, P
->Constraint
[i
][1]);
665 if (!n
|| value_lt(tmp
, neg
))
666 value_assign(neg
, tmp
);
670 value_subtract(tmp
, neg
, pos
);
671 value_increment(*len
, tmp
);
673 value_set_si(*len
, -1);
681 /* Update group[k] to the group column k belongs to.
682 * When merging two groups, only the group of the current
683 * group leader is changed. Here we change the group of
684 * the other members to also point to the group that the
685 * old group leader now points to.
687 static void update_group(int *group
, int *cnt
, int k
)
696 * Factors the polyhedron P into polyhedra Q_i such that
697 * the number of integer points in P is equal to the product
698 * of the number of integer points in the individual Q_i
700 * If no factors can be found, NULL is returned.
701 * Otherwise, a linked list of the factors is returned.
703 * If there are factors and if T is not NULL, then a matrix will be
704 * returned through T expressing the old variables in terms of the
705 * new variables as they appear in the sequence of factors.
707 * The algorithm works by first computing the Hermite normal form
708 * and then grouping columns linked by one or more constraints together,
709 * where a constraints "links" two or more columns if the constraint
710 * has nonzero coefficients in the columns.
712 Polyhedron
* Polyhedron_Factor(Polyhedron
*P
, unsigned nparam
, Matrix
**T
,
716 Matrix
*M
, *H
, *Q
, *U
;
717 int *pos
; /* for each column: row position of pivot */
718 int *group
; /* group to which a column belongs */
719 int *cnt
; /* number of columns in the group */
720 int *rowgroup
; /* group to which a constraint belongs */
721 int nvar
= P
->Dimension
- nparam
;
722 Polyhedron
*F
= NULL
;
730 NALLOC(rowgroup
, P
->NbConstraints
);
732 M
= Matrix_Alloc(P
->NbConstraints
, nvar
);
733 for (i
= 0; i
< P
->NbConstraints
; ++i
)
734 Vector_Copy(P
->Constraint
[i
]+1, M
->p
[i
], nvar
);
735 left_hermite(M
, &H
, &Q
, &U
);
739 for (i
= 0; i
< P
->NbConstraints
; ++i
)
741 for (i
= 0, j
= 0; i
< H
->NbColumns
; ++i
) {
742 for ( ; j
< H
->NbRows
; ++j
)
743 if (value_notzero_p(H
->p
[j
][i
]))
747 for (i
= 0; i
< nvar
; ++i
) {
751 for (i
= 0; i
< H
->NbColumns
&& cnt
[0] < nvar
; ++i
) {
752 if (pos
[i
] == H
->NbRows
)
753 continue; /* A line direction */
754 if (rowgroup
[pos
[i
]] == -1)
755 rowgroup
[pos
[i
]] = i
;
756 for (j
= pos
[i
]+1; j
< H
->NbRows
; ++j
) {
757 if (value_zero_p(H
->p
[j
][i
]))
759 if (rowgroup
[j
] != -1)
761 rowgroup
[j
] = group
[i
];
762 for (k
= i
+1; k
< H
->NbColumns
&& j
>= pos
[k
]; ++k
) {
763 update_group(group
, cnt
, k
);
764 update_group(group
, cnt
, i
);
765 if (group
[k
] != group
[i
] && value_notzero_p(H
->p
[j
][k
])) {
766 assert(cnt
[group
[k
]] != 0);
767 assert(cnt
[group
[i
]] != 0);
768 if (group
[i
] < group
[k
]) {
769 cnt
[group
[i
]] += cnt
[group
[k
]];
771 group
[group
[k
]] = group
[i
];
773 cnt
[group
[k
]] += cnt
[group
[i
]];
775 group
[group
[i
]] = group
[k
];
781 for (i
= 1; i
< nvar
; ++i
)
782 update_group(group
, cnt
, i
);
784 if (cnt
[0] != nvar
) {
785 /* Extract out pure context constraints separately */
786 Polyhedron
**next
= &F
;
789 *T
= Matrix_Alloc(nvar
, nvar
);
790 for (i
= nparam
? -1 : 0; i
< nvar
; ++i
) {
794 for (j
= 0, k
= 0; j
< P
->NbConstraints
; ++j
)
795 if (rowgroup
[j
] == -1) {
796 if (First_Non_Zero(P
->Constraint
[j
]+1+nvar
,
809 for (j
= 0, k
= 0; j
< P
->NbConstraints
; ++j
)
810 if (rowgroup
[j
] >= 0 && group
[rowgroup
[j
]] == i
) {
817 for (j
= 0; j
< nvar
; ++j
) {
819 for (l
= 0, m
= 0; m
< d
; ++l
) {
822 value_assign((*T
)->p
[j
][tot_d
+m
++], U
->p
[j
][l
]);
826 M
= Matrix_Alloc(k
, d
+nparam
+2);
827 for (j
= 0, k
= 0; j
< P
->NbConstraints
; ++j
) {
829 if (rowgroup
[j
] != i
)
831 value_assign(M
->p
[k
][0], P
->Constraint
[j
][0]);
832 for (l
= 0, m
= 0; m
< d
; ++l
) {
835 value_assign(M
->p
[k
][1+m
++], H
->p
[j
][l
]);
837 Vector_Copy(P
->Constraint
[j
]+1+nvar
, M
->p
[k
]+1+m
, nparam
+1);
840 *next
= Constraints2Polyhedron(M
, NbMaxRays
);
841 next
= &(*next
)->next
;
855 /* Computes the intersection of the contexts of a list of factors */
856 Polyhedron
*Factor_Context(Polyhedron
*F
, unsigned nparam
, unsigned MaxRays
)
859 Polyhedron
*C
= NULL
;
861 for (Q
= F
; Q
; Q
= Q
->next
) {
863 Polyhedron
*next
= Q
->next
;
866 if (Q
->Dimension
!= nparam
)
867 QC
= Polyhedron_Project(Q
, nparam
);
870 C
= Q
== QC
? Polyhedron_Copy(QC
) : QC
;
873 C
= DomainIntersection(C
, QC
, MaxRays
);
884 * Project on final dim dimensions
886 Polyhedron
* Polyhedron_Project(Polyhedron
*P
, int dim
)
889 int remove
= P
->Dimension
- dim
;
893 if (P
->Dimension
== dim
)
894 return Polyhedron_Copy(P
);
896 T
= Matrix_Alloc(dim
+1, P
->Dimension
+1);
897 for (i
= 0; i
< dim
+1; ++i
)
898 value_set_si(T
->p
[i
][i
+remove
], 1);
899 I
= Polyhedron_Image(P
, T
, P
->NbConstraints
);
904 /* Constructs a new constraint that ensures that
905 * the first constraint is (strictly) smaller than
908 static void smaller_constraint(Value
*a
, Value
*b
, Value
*c
, int pos
, int shift
,
909 int len
, int strict
, Value
*tmp
)
911 value_oppose(*tmp
, b
[pos
+1]);
912 value_set_si(c
[0], 1);
913 Vector_Combine(a
+1+shift
, b
+1+shift
, c
+1, *tmp
, a
[pos
+1], len
-shift
-1);
915 value_decrement(c
[len
-shift
-1], c
[len
-shift
-1]);
916 ConstraintSimplify(c
, c
, len
-shift
, tmp
);
920 /* For each pair of lower and upper bounds on the first variable,
921 * calls fn with the set of constraints on the remaining variables
922 * where these bounds are active, i.e., (stricly) larger/smaller than
923 * the other lower/upper bounds, the lower and upper bound and the
926 * If the first variable is equal to an affine combination of the
927 * other variables then fn is called with both lower and upper
928 * pointing to the corresponding equality.
930 * If there is no lower (or upper) bound, then NULL is passed
931 * as the corresponding bound.
933 void for_each_lower_upper_bound(Polyhedron
*P
,
934 for_each_lower_upper_bound_init init
,
935 for_each_lower_upper_bound_fn fn
,
938 unsigned dim
= P
->Dimension
;
945 if (value_zero_p(P
->Constraint
[0][0]) &&
946 value_notzero_p(P
->Constraint
[0][1])) {
947 M
= Matrix_Alloc(P
->NbConstraints
-1, dim
-1+2);
948 for (i
= 1; i
< P
->NbConstraints
; ++i
) {
949 value_assign(M
->p
[i
-1][0], P
->Constraint
[i
][0]);
950 Vector_Copy(P
->Constraint
[i
]+2, M
->p
[i
-1]+1, dim
);
954 fn(M
, P
->Constraint
[0], P
->Constraint
[0], cb_data
);
960 pos
= ALLOCN(int, P
->NbConstraints
);
962 for (i
= 0, z
= 0; i
< P
->NbConstraints
; ++i
)
963 if (value_zero_p(P
->Constraint
[i
][1]))
964 pos
[P
->NbConstraints
-1 - z
++] = i
;
965 /* put those with positive coefficients first; number: p */
966 for (i
= 0, p
= 0, n
= P
->NbConstraints
-z
-1; i
< P
->NbConstraints
; ++i
)
967 if (value_pos_p(P
->Constraint
[i
][1]))
969 else if (value_neg_p(P
->Constraint
[i
][1]))
971 n
= P
->NbConstraints
-z
-p
;
976 M
= Matrix_Alloc((p
? p
-1 : 0) + (n
? n
-1 : 0) + z
+ 1, dim
-1+2);
977 for (i
= 0; i
< z
; ++i
) {
978 value_assign(M
->p
[i
][0], P
->Constraint
[pos
[P
->NbConstraints
-1 - i
]][0]);
979 Vector_Copy(P
->Constraint
[pos
[P
->NbConstraints
-1 - i
]]+2,
982 for (k
= p
? 0 : -1; k
< p
; ++k
) {
983 for (k2
= 0; k2
< p
; ++k2
) {
986 q
= 1 + z
+ k2
- (k2
> k
);
988 P
->Constraint
[pos
[k
]],
989 P
->Constraint
[pos
[k2
]],
990 M
->p
[q
], 0, 1, dim
+2, k2
> k
, &g
);
992 for (l
= n
? p
: p
-1; l
< p
+n
; ++l
) {
995 for (l2
= p
; l2
< p
+n
; ++l2
) {
998 q
= 1 + z
+ l2
-1 - (l2
> l
);
1000 P
->Constraint
[pos
[l2
]],
1001 P
->Constraint
[pos
[l
]],
1002 M
->p
[q
], 0, 1, dim
+2, l2
> l
, &g
);
1005 smaller_constraint(P
->Constraint
[pos
[k
]],
1006 P
->Constraint
[pos
[l
]],
1007 M
->p
[z
], 0, 1, dim
+2, 0, &g
);
1008 lower
= p
? P
->Constraint
[pos
[k
]] : NULL
;
1009 upper
= n
? P
->Constraint
[pos
[l
]] : NULL
;
1010 fn(M
, lower
, upper
, cb_data
);
1019 struct section
{ Polyhedron
* D
; evalue E
; };
1029 static void PLL_init(unsigned n
, void *cb_data
)
1031 struct PLL_data
*data
= (struct PLL_data
*)cb_data
;
1033 data
->s
= ALLOCN(struct section
, n
);
1036 /* Computes ceil(-coef/abs(d)) */
1037 static evalue
* bv_ceil3(Value
*coef
, int len
, Value d
, Polyhedron
*P
)
1041 Vector
*val
= Vector_Alloc(len
);
1044 Vector_Oppose(coef
, val
->p
, len
);
1045 value_absolute(t
, d
);
1047 EP
= ceiling(val
->p
, t
, len
-1, P
);
1055 static void PLL_cb(Matrix
*M
, Value
*lower
, Value
*upper
, void *cb_data
)
1057 struct PLL_data
*data
= (struct PLL_data
*)cb_data
;
1058 unsigned dim
= M
->NbColumns
-1;
1066 M2
= Matrix_Copy(M
);
1067 T
= Constraints2Polyhedron(M2
, data
->MaxRays
);
1069 data
->s
[data
->nd
].D
= DomainIntersection(T
, data
->C
, data
->MaxRays
);
1072 POL_ENSURE_VERTICES(data
->s
[data
->nd
].D
);
1073 if (emptyQ(data
->s
[data
->nd
].D
)) {
1074 Polyhedron_Free(data
->s
[data
->nd
].D
);
1077 L
= bv_ceil3(lower
+1+1, dim
-1+1, lower
[0+1], data
->s
[data
->nd
].D
);
1078 U
= bv_ceil3(upper
+1+1, dim
-1+1, upper
[0+1], data
->s
[data
->nd
].D
);
1080 eadd(&data
->mone
, U
);
1081 emul(&data
->mone
, U
);
1082 data
->s
[data
->nd
].E
= *U
;
1088 static evalue
*ParamLine_Length_mod(Polyhedron
*P
, Polyhedron
*C
, unsigned MaxRays
)
1090 unsigned dim
= P
->Dimension
;
1091 unsigned nvar
= dim
- C
->Dimension
;
1092 struct PLL_data data
;
1098 value_init(data
.mone
.d
);
1099 evalue_set_si(&data
.mone
, -1, 1);
1102 data
.MaxRays
= MaxRays
;
1104 for_each_lower_upper_bound(P
, PLL_init
, PLL_cb
, &data
);
1106 free_evalue_refs(&data
.mone
);
1110 return evalue_zero();
1115 value_set_si(F
->d
, 0);
1116 F
->x
.p
= new_enode(partition
, 2*data
.nd
, dim
-nvar
);
1117 for (k
= 0; k
< data
.nd
; ++k
) {
1118 EVALUE_SET_DOMAIN(F
->x
.p
->arr
[2*k
], data
.s
[k
].D
);
1119 value_clear(F
->x
.p
->arr
[2*k
+1].d
);
1120 F
->x
.p
->arr
[2*k
+1] = data
.s
[k
].E
;
1127 evalue
* ParamLine_Length(Polyhedron
*P
, Polyhedron
*C
,
1128 struct barvinok_options
*options
)
1131 tmp
= ParamLine_Length_mod(P
, C
, options
->MaxRays
);
1132 if (options
->lookup_table
) {
1133 evalue_mod2table(tmp
, C
->Dimension
);
1139 Bool
isIdentity(Matrix
*M
)
1142 if (M
->NbRows
!= M
->NbColumns
)
1145 for (i
= 0;i
< M
->NbRows
; i
++)
1146 for (j
= 0; j
< M
->NbColumns
; j
++)
1148 if(value_notone_p(M
->p
[i
][j
]))
1151 if(value_notzero_p(M
->p
[i
][j
]))
1157 void Param_Polyhedron_Print(FILE* DST
, Param_Polyhedron
*PP
,
1158 const char **param_names
)
1163 for(P
=PP
->D
;P
;P
=P
->next
) {
1165 /* prints current val. dom. */
1166 fprintf(DST
, "---------------------------------------\n");
1167 fprintf(DST
, "Domain :\n");
1168 Print_Domain(DST
, P
->Domain
, param_names
);
1170 /* scan the vertices */
1171 fprintf(DST
, "Vertices :\n");
1172 FORALL_PVertex_in_ParamPolyhedron(V
,P
,PP
) {
1174 /* prints each vertex */
1175 Print_Vertex(DST
, V
->Vertex
, param_names
);
1178 END_FORALL_PVertex_in_ParamPolyhedron
;
1182 void Enumeration_Print(FILE *Dst
, Enumeration
*en
, const char **params
)
1184 for (; en
; en
= en
->next
) {
1185 Print_Domain(Dst
, en
->ValidityDomain
, params
);
1186 print_evalue(Dst
, &en
->EP
, params
);
1190 void Enumeration_Free(Enumeration
*en
)
1196 free_evalue_refs( &(en
->EP
) );
1197 Domain_Free( en
->ValidityDomain
);
1204 void Enumeration_mod2table(Enumeration
*en
, unsigned nparam
)
1206 for (; en
; en
= en
->next
) {
1207 evalue_mod2table(&en
->EP
, nparam
);
1208 reduce_evalue(&en
->EP
);
1212 size_t Enumeration_size(Enumeration
*en
)
1216 for (; en
; en
= en
->next
) {
1217 s
+= domain_size(en
->ValidityDomain
);
1218 s
+= evalue_size(&en
->EP
);
1223 /* Check whether every set in D2 is included in some set of D1 */
1224 int DomainIncludes(Polyhedron
*D1
, Polyhedron
*D2
)
1226 for ( ; D2
; D2
= D2
->next
) {
1228 for (P1
= D1
; P1
; P1
= P1
->next
)
1229 if (PolyhedronIncludes(P1
, D2
))
1237 int line_minmax(Polyhedron
*I
, Value
*min
, Value
*max
)
1242 value_oppose(I
->Constraint
[0][2], I
->Constraint
[0][2]);
1243 /* There should never be a remainder here */
1244 if (value_pos_p(I
->Constraint
[0][1]))
1245 mpz_fdiv_q(*min
, I
->Constraint
[0][2], I
->Constraint
[0][1]);
1247 mpz_fdiv_q(*min
, I
->Constraint
[0][2], I
->Constraint
[0][1]);
1248 value_assign(*max
, *min
);
1249 } else for (i
= 0; i
< I
->NbConstraints
; ++i
) {
1250 if (value_zero_p(I
->Constraint
[i
][1])) {
1255 value_oppose(I
->Constraint
[i
][2], I
->Constraint
[i
][2]);
1256 if (value_pos_p(I
->Constraint
[i
][1]))
1257 mpz_cdiv_q(*min
, I
->Constraint
[i
][2], I
->Constraint
[i
][1]);
1259 mpz_fdiv_q(*max
, I
->Constraint
[i
][2], I
->Constraint
[i
][1]);
1265 int DomainContains(Polyhedron
*P
, Value
*list_args
, int len
,
1266 unsigned MaxRays
, int set
)
1271 if (P
->Dimension
== len
)
1272 return in_domain(P
, list_args
);
1274 assert(set
); // assume list_args is large enough
1275 assert((P
->Dimension
- len
) % 2 == 0);
1277 for (i
= 0; i
< P
->Dimension
- len
; i
+= 2) {
1279 for (j
= 0 ; j
< P
->NbEq
; ++j
)
1280 if (value_notzero_p(P
->Constraint
[j
][1+len
+i
]))
1282 assert(j
< P
->NbEq
);
1283 value_absolute(m
, P
->Constraint
[j
][1+len
+i
]);
1284 k
= First_Non_Zero(P
->Constraint
[j
]+1, len
);
1286 assert(First_Non_Zero(P
->Constraint
[j
]+1+k
+1, len
- k
- 1) == -1);
1287 mpz_fdiv_q(list_args
[len
+i
], list_args
[k
], m
);
1288 mpz_fdiv_r(list_args
[len
+i
+1], list_args
[k
], m
);
1292 return in_domain(P
, list_args
);
1295 Polyhedron
*DomainConcat(Polyhedron
*head
, Polyhedron
*tail
)
1300 for (S
= head
; S
->next
; S
= S
->next
)
1306 evalue
*barvinok_lexsmaller_ev(Polyhedron
*P
, Polyhedron
*D
, unsigned dim
,
1307 Polyhedron
*C
, unsigned MaxRays
)
1310 Polyhedron
*RC
, *RD
, *Q
;
1311 unsigned nparam
= dim
+ C
->Dimension
;
1315 RC
= LexSmaller(P
, D
, dim
, C
, MaxRays
);
1319 exist
= RD
->Dimension
- nparam
- dim
;
1320 CA
= align_context(RC
, RD
->Dimension
, MaxRays
);
1321 Q
= DomainIntersection(RD
, CA
, MaxRays
);
1322 Polyhedron_Free(CA
);
1324 Polyhedron_Free(RC
);
1327 for (Q
= RD
; Q
; Q
= Q
->next
) {
1329 Polyhedron
*next
= Q
->next
;
1332 t
= barvinok_enumerate_e(Q
, exist
, nparam
, MaxRays
);
1349 Enumeration
*barvinok_lexsmaller(Polyhedron
*P
, Polyhedron
*D
, unsigned dim
,
1350 Polyhedron
*C
, unsigned MaxRays
)
1352 evalue
*EP
= barvinok_lexsmaller_ev(P
, D
, dim
, C
, MaxRays
);
1354 return partition2enumeration(EP
);
1357 /* "align" matrix to have nrows by inserting
1358 * the necessary number of rows and an equal number of columns in front
1360 Matrix
*align_matrix(Matrix
*M
, int nrows
)
1363 int newrows
= nrows
- M
->NbRows
;
1364 Matrix
*M2
= Matrix_Alloc(nrows
, newrows
+ M
->NbColumns
);
1365 for (i
= 0; i
< newrows
; ++i
)
1366 value_set_si(M2
->p
[i
][i
], 1);
1367 for (i
= 0; i
< M
->NbRows
; ++i
)
1368 Vector_Copy(M
->p
[i
], M2
->p
[newrows
+i
]+newrows
, M
->NbColumns
);
1372 static void print_varlist(FILE *out
, int n
, char **names
)
1376 for (i
= 0; i
< n
; ++i
) {
1379 fprintf(out
, "%s", names
[i
]);
1384 static void print_term(FILE *out
, Value v
, int pos
, int dim
, int nparam
,
1385 char **iter_names
, char **param_names
, int *first
)
1387 if (value_zero_p(v
)) {
1388 if (first
&& *first
&& pos
>= dim
+ nparam
)
1394 if (!*first
&& value_pos_p(v
))
1398 if (pos
< dim
+ nparam
) {
1399 if (value_mone_p(v
))
1401 else if (!value_one_p(v
))
1402 value_print(out
, VALUE_FMT
, v
);
1404 fprintf(out
, "%s", iter_names
[pos
]);
1406 fprintf(out
, "%s", param_names
[pos
-dim
]);
1408 value_print(out
, VALUE_FMT
, v
);
1411 char **util_generate_names(int n
, const char *prefix
)
1414 int len
= (prefix
? strlen(prefix
) : 0) + 10;
1415 char **names
= ALLOCN(char*, n
);
1417 fprintf(stderr
, "ERROR: memory overflow.\n");
1420 for (i
= 0; i
< n
; ++i
) {
1421 names
[i
] = ALLOCN(char, len
);
1423 fprintf(stderr
, "ERROR: memory overflow.\n");
1427 snprintf(names
[i
], len
, "%d", i
);
1429 snprintf(names
[i
], len
, "%s%d", prefix
, i
);
1435 void util_free_names(int n
, char **names
)
1438 for (i
= 0; i
< n
; ++i
)
1443 void Polyhedron_pprint(FILE *out
, Polyhedron
*P
, int dim
, int nparam
,
1444 char **iter_names
, char **param_names
)
1449 assert(dim
+ nparam
== P
->Dimension
);
1455 print_varlist(out
, nparam
, param_names
);
1456 fprintf(out
, " -> ");
1458 print_varlist(out
, dim
, iter_names
);
1459 fprintf(out
, " : ");
1462 fprintf(out
, "FALSE");
1463 else for (i
= 0; i
< P
->NbConstraints
; ++i
) {
1465 int v
= First_Non_Zero(P
->Constraint
[i
]+1, P
->Dimension
);
1466 if (v
== -1 && value_pos_p(P
->Constraint
[i
][0]))
1469 fprintf(out
, " && ");
1470 if (v
== -1 && value_notzero_p(P
->Constraint
[i
][1+P
->Dimension
]))
1471 fprintf(out
, "FALSE");
1472 else if (value_pos_p(P
->Constraint
[i
][v
+1])) {
1473 print_term(out
, P
->Constraint
[i
][v
+1], v
, dim
, nparam
,
1474 iter_names
, param_names
, NULL
);
1475 if (value_zero_p(P
->Constraint
[i
][0]))
1476 fprintf(out
, " = ");
1478 fprintf(out
, " >= ");
1479 for (j
= v
+1; j
<= dim
+nparam
; ++j
) {
1480 value_oppose(tmp
, P
->Constraint
[i
][1+j
]);
1481 print_term(out
, tmp
, j
, dim
, nparam
,
1482 iter_names
, param_names
, &first
);
1485 value_oppose(tmp
, P
->Constraint
[i
][1+v
]);
1486 print_term(out
, tmp
, v
, dim
, nparam
,
1487 iter_names
, param_names
, NULL
);
1488 fprintf(out
, " <= ");
1489 for (j
= v
+1; j
<= dim
+nparam
; ++j
)
1490 print_term(out
, P
->Constraint
[i
][1+j
], j
, dim
, nparam
,
1491 iter_names
, param_names
, &first
);
1495 fprintf(out
, " }\n");
1500 /* Construct a cone over P with P placed at x_d = 1, with
1501 * x_d the coordinate of an extra dimension
1503 * It's probably a mistake to depend so much on the internal
1504 * representation. We should probably simply compute the
1505 * vertices/facets first.
1507 Polyhedron
*Cone_over_Polyhedron(Polyhedron
*P
)
1509 unsigned NbConstraints
= 0;
1510 unsigned NbRays
= 0;
1514 if (POL_HAS(P
, POL_INEQUALITIES
))
1515 NbConstraints
= P
->NbConstraints
+ 1;
1516 if (POL_HAS(P
, POL_POINTS
))
1517 NbRays
= P
->NbRays
+ 1;
1519 C
= Polyhedron_Alloc(P
->Dimension
+1, NbConstraints
, NbRays
);
1520 if (POL_HAS(P
, POL_INEQUALITIES
)) {
1522 for (i
= 0; i
< P
->NbConstraints
; ++i
)
1523 Vector_Copy(P
->Constraint
[i
], C
->Constraint
[i
], P
->Dimension
+2);
1525 value_set_si(C
->Constraint
[P
->NbConstraints
][0], 1);
1526 value_set_si(C
->Constraint
[P
->NbConstraints
][1+P
->Dimension
], 1);
1528 if (POL_HAS(P
, POL_POINTS
)) {
1529 C
->NbBid
= P
->NbBid
;
1530 for (i
= 0; i
< P
->NbRays
; ++i
)
1531 Vector_Copy(P
->Ray
[i
], C
->Ray
[i
], P
->Dimension
+2);
1533 value_set_si(C
->Ray
[P
->NbRays
][0], 1);
1534 value_set_si(C
->Ray
[P
->NbRays
][1+C
->Dimension
], 1);
1536 POL_SET(C
, POL_VALID
);
1537 if (POL_HAS(P
, POL_INEQUALITIES
))
1538 POL_SET(C
, POL_INEQUALITIES
);
1539 if (POL_HAS(P
, POL_POINTS
))
1540 POL_SET(C
, POL_POINTS
);
1541 if (POL_HAS(P
, POL_VERTICES
))
1542 POL_SET(C
, POL_VERTICES
);
1546 /* Returns a (dim+nparam+1)x((dim-n)+nparam+1) matrix
1547 * mapping the transformed subspace back to the original space.
1548 * n is the number of equalities involving the variables
1549 * (i.e., not purely the parameters).
1550 * The remaining n coordinates in the transformed space would
1551 * have constant (parametric) values and are therefore not
1552 * included in the variables of the new space.
1554 Matrix
*compress_variables(Matrix
*Equalities
, unsigned nparam
)
1556 unsigned dim
= (Equalities
->NbColumns
-2) - nparam
;
1557 Matrix
*M
, *H
, *Q
, *U
, *C
, *ratH
, *invH
, *Ul
, *T1
, *T2
, *T
;
1562 for (n
= 0; n
< Equalities
->NbRows
; ++n
)
1563 if (First_Non_Zero(Equalities
->p
[n
]+1, dim
) == -1)
1566 return Identity(dim
+nparam
+1);
1568 value_set_si(mone
, -1);
1569 M
= Matrix_Alloc(n
, dim
);
1570 C
= Matrix_Alloc(n
+1, nparam
+1);
1571 for (i
= 0; i
< n
; ++i
) {
1572 Vector_Copy(Equalities
->p
[i
]+1, M
->p
[i
], dim
);
1573 Vector_Scale(Equalities
->p
[i
]+1+dim
, C
->p
[i
], mone
, nparam
+1);
1575 value_set_si(C
->p
[n
][nparam
], 1);
1576 left_hermite(M
, &H
, &Q
, &U
);
1581 ratH
= Matrix_Alloc(n
+1, n
+1);
1582 invH
= Matrix_Alloc(n
+1, n
+1);
1583 for (i
= 0; i
< n
; ++i
)
1584 Vector_Copy(H
->p
[i
], ratH
->p
[i
], n
);
1585 value_set_si(ratH
->p
[n
][n
], 1);
1586 ok
= Matrix_Inverse(ratH
, invH
);
1590 T1
= Matrix_Alloc(n
+1, nparam
+1);
1591 Matrix_Product(invH
, C
, T1
);
1594 if (value_notone_p(T1
->p
[n
][nparam
])) {
1595 for (i
= 0; i
< n
; ++i
) {
1596 if (!mpz_divisible_p(T1
->p
[i
][nparam
], T1
->p
[n
][nparam
])) {
1601 /* compress_params should have taken care of this */
1602 for (j
= 0; j
< nparam
; ++j
)
1603 assert(mpz_divisible_p(T1
->p
[i
][j
], T1
->p
[n
][nparam
]));
1604 Vector_AntiScale(T1
->p
[i
], T1
->p
[i
], T1
->p
[n
][nparam
], nparam
+1);
1606 value_set_si(T1
->p
[n
][nparam
], 1);
1608 Ul
= Matrix_Alloc(dim
+1, n
+1);
1609 for (i
= 0; i
< dim
; ++i
)
1610 Vector_Copy(U
->p
[i
], Ul
->p
[i
], n
);
1611 value_set_si(Ul
->p
[dim
][n
], 1);
1612 T2
= Matrix_Alloc(dim
+1, nparam
+1);
1613 Matrix_Product(Ul
, T1
, T2
);
1617 T
= Matrix_Alloc(dim
+nparam
+1, (dim
-n
)+nparam
+1);
1618 for (i
= 0; i
< dim
; ++i
) {
1619 Vector_Copy(U
->p
[i
]+n
, T
->p
[i
], dim
-n
);
1620 Vector_Copy(T2
->p
[i
], T
->p
[i
]+dim
-n
, nparam
+1);
1622 for (i
= 0; i
< nparam
+1; ++i
)
1623 value_set_si(T
->p
[dim
+i
][(dim
-n
)+i
], 1);
1624 assert(value_one_p(T2
->p
[dim
][nparam
]));
1631 /* Computes the left inverse of an affine embedding M and, if Eq is not NULL,
1632 * the equalities that define the affine subspace onto which M maps
1635 Matrix
*left_inverse(Matrix
*M
, Matrix
**Eq
)
1638 Matrix
*L
, *H
, *Q
, *U
, *ratH
, *invH
, *Ut
, *inv
;
1641 if (M
->NbColumns
== 1) {
1642 inv
= Matrix_Alloc(1, M
->NbRows
);
1643 value_set_si(inv
->p
[0][M
->NbRows
-1], 1);
1645 *Eq
= Matrix_Alloc(M
->NbRows
-1, 1+(M
->NbRows
-1)+1);
1646 for (i
= 0; i
< M
->NbRows
-1; ++i
) {
1647 value_oppose((*Eq
)->p
[i
][1+i
], M
->p
[M
->NbRows
-1][0]);
1648 value_assign((*Eq
)->p
[i
][1+(M
->NbRows
-1)], M
->p
[i
][0]);
1655 L
= Matrix_Alloc(M
->NbRows
-1, M
->NbColumns
-1);
1656 for (i
= 0; i
< L
->NbRows
; ++i
)
1657 Vector_Copy(M
->p
[i
], L
->p
[i
], L
->NbColumns
);
1658 right_hermite(L
, &H
, &U
, &Q
);
1661 t
= Vector_Alloc(U
->NbColumns
);
1662 for (i
= 0; i
< U
->NbColumns
; ++i
)
1663 value_oppose(t
->p
[i
], M
->p
[i
][M
->NbColumns
-1]);
1665 *Eq
= Matrix_Alloc(H
->NbRows
- H
->NbColumns
, 2 + U
->NbColumns
);
1666 for (i
= 0; i
< H
->NbRows
- H
->NbColumns
; ++i
) {
1667 Vector_Copy(U
->p
[H
->NbColumns
+i
], (*Eq
)->p
[i
]+1, U
->NbColumns
);
1668 Inner_Product(U
->p
[H
->NbColumns
+i
], t
->p
, U
->NbColumns
,
1669 (*Eq
)->p
[i
]+1+U
->NbColumns
);
1672 ratH
= Matrix_Alloc(H
->NbColumns
+1, H
->NbColumns
+1);
1673 invH
= Matrix_Alloc(H
->NbColumns
+1, H
->NbColumns
+1);
1674 for (i
= 0; i
< H
->NbColumns
; ++i
)
1675 Vector_Copy(H
->p
[i
], ratH
->p
[i
], H
->NbColumns
);
1676 value_set_si(ratH
->p
[ratH
->NbRows
-1][ratH
->NbColumns
-1], 1);
1678 ok
= Matrix_Inverse(ratH
, invH
);
1681 Ut
= Matrix_Alloc(invH
->NbRows
, U
->NbColumns
+1);
1682 for (i
= 0; i
< Ut
->NbRows
-1; ++i
) {
1683 Vector_Copy(U
->p
[i
], Ut
->p
[i
], U
->NbColumns
);
1684 Inner_Product(U
->p
[i
], t
->p
, U
->NbColumns
, &Ut
->p
[i
][Ut
->NbColumns
-1]);
1688 value_set_si(Ut
->p
[Ut
->NbRows
-1][Ut
->NbColumns
-1], 1);
1689 inv
= Matrix_Alloc(invH
->NbRows
, Ut
->NbColumns
);
1690 Matrix_Product(invH
, Ut
, inv
);
1696 /* Check whether all rays are revlex positive in the parameters
1698 int Polyhedron_has_revlex_positive_rays(Polyhedron
*P
, unsigned nparam
)
1701 for (r
= 0; r
< P
->NbRays
; ++r
) {
1703 if (value_notzero_p(P
->Ray
[r
][P
->Dimension
+1]))
1705 for (i
= P
->Dimension
-1; i
>= P
->Dimension
-nparam
; --i
) {
1706 if (value_neg_p(P
->Ray
[r
][i
+1]))
1708 if (value_pos_p(P
->Ray
[r
][i
+1]))
1711 /* A ray independent of the parameters */
1712 if (i
< P
->Dimension
-nparam
)
1718 static Polyhedron
*Recession_Cone(Polyhedron
*P
, unsigned nparam
, unsigned MaxRays
)
1721 unsigned nvar
= P
->Dimension
- nparam
;
1722 Matrix
*M
= Matrix_Alloc(P
->NbConstraints
, 1 + nvar
+ 1);
1724 for (i
= 0; i
< P
->NbConstraints
; ++i
)
1725 Vector_Copy(P
->Constraint
[i
], M
->p
[i
], 1+nvar
);
1726 R
= Constraints2Polyhedron(M
, MaxRays
);
1731 int Polyhedron_is_unbounded(Polyhedron
*P
, unsigned nparam
, unsigned MaxRays
)
1735 Polyhedron
*R
= Recession_Cone(P
, nparam
, MaxRays
);
1736 POL_ENSURE_VERTICES(R
);
1738 for (i
= 0; i
< R
->NbRays
; ++i
)
1739 if (value_zero_p(R
->Ray
[i
][1+R
->Dimension
]))
1741 is_unbounded
= R
->NbBid
> 0 || i
< R
->NbRays
;
1743 return is_unbounded
;
1746 static void SwapColumns(Value
**V
, int n
, int i
, int j
)
1750 for (r
= 0; r
< n
; ++r
)
1751 value_swap(V
[r
][i
], V
[r
][j
]);
1754 void Polyhedron_ExchangeColumns(Polyhedron
*P
, int Column1
, int Column2
)
1756 SwapColumns(P
->Constraint
, P
->NbConstraints
, Column1
, Column2
);
1757 SwapColumns(P
->Ray
, P
->NbRays
, Column1
, Column2
);
1760 Polyhedron_Matrix_View(P
, &M
, P
->NbConstraints
);
1761 Gauss(&M
, P
->NbEq
, P
->Dimension
+1);
1765 /* perform transposition inline; assumes M is a square matrix */
1766 void Matrix_Transposition(Matrix
*M
)
1770 assert(M
->NbRows
== M
->NbColumns
);
1771 for (i
= 0; i
< M
->NbRows
; ++i
)
1772 for (j
= i
+1; j
< M
->NbColumns
; ++j
)
1773 value_swap(M
->p
[i
][j
], M
->p
[j
][i
]);
1776 /* Matrix "view" of first rows rows */
1777 void Polyhedron_Matrix_View(Polyhedron
*P
, Matrix
*M
, unsigned rows
)
1780 M
->NbColumns
= P
->Dimension
+2;
1781 M
->p_Init
= P
->p_Init
;
1782 M
->p
= P
->Constraint
;
1785 int Last_Non_Zero(Value
*p
, unsigned len
)
1789 for (i
= len
- 1; i
>= 0; --i
)
1790 if (value_notzero_p(p
[i
]))