3 #include <isl/val_gmp.h>
4 #include <isl_set_polylib.h>
5 #include <barvinok/util.h>
6 #include <barvinok/options.h>
7 #include <polylib/ranking.h>
9 #include "lattice_point.h"
11 #define ALLOC(type) (type*)malloc(sizeof(type))
12 #define ALLOCN(type,n) (type*)malloc((n) * sizeof(type))
15 #define NALLOC(p,n) p = (typeof(p))malloc((n) * sizeof(*p))
17 #define NALLOC(p,n) p = (void *)malloc((n) * sizeof(*p))
20 void manual_count(Polyhedron
*P
, Value
* result
)
22 isl_ctx
*ctx
= isl_ctx_alloc();
26 int nvar
= P
->Dimension
;
28 dim
= isl_space_set_alloc(ctx
, 0, nvar
);
29 set
= isl_set_new_from_polylib(P
, dim
);
31 v
= isl_set_count_val(set
);
32 isl_val_get_num_gmp(v
, *result
);
41 #include <barvinok/evalue.h>
42 #include <barvinok/util.h>
43 #include <barvinok/barvinok.h>
45 /* Return random value between 0 and max-1 inclusive
47 int random_int(int max
) {
48 return (int) (((double)(max
))*rand()/(RAND_MAX
+1.0));
51 Polyhedron
*Polyhedron_Read(unsigned MaxRays
)
54 unsigned NbRows
, NbColumns
;
59 while (fgets(s
, sizeof(s
), stdin
)) {
62 if (strncasecmp(s
, "vertices", sizeof("vertices")-1) == 0)
64 if (sscanf(s
, "%u %u", &NbRows
, &NbColumns
) == 2)
69 M
= Matrix_Alloc(NbRows
,NbColumns
);
72 P
= Rays2Polyhedron(M
, MaxRays
);
74 P
= Constraints2Polyhedron(M
, MaxRays
);
79 /* Inplace polarization
81 void Polyhedron_Polarize(Polyhedron
*P
)
83 unsigned NbRows
= P
->NbConstraints
+ P
->NbRays
;
87 q
= (Value
**)malloc(NbRows
* sizeof(Value
*));
89 for (i
= 0; i
< P
->NbRays
; ++i
)
91 for (; i
< NbRows
; ++i
)
92 q
[i
] = P
->Constraint
[i
-P
->NbRays
];
93 P
->NbConstraints
= NbRows
- P
->NbConstraints
;
94 P
->NbRays
= NbRows
- P
->NbRays
;
97 P
->Ray
= q
+ P
->NbConstraints
;
101 * Rather general polar
102 * We can optimize it significantly if we assume that
105 * Also, we calculate the polar as defined in Schrijver
106 * The opposite should probably work as well and would
107 * eliminate the need for multiplying by -1
109 Polyhedron
* Polyhedron_Polar(Polyhedron
*P
, unsigned NbMaxRays
)
113 unsigned dim
= P
->Dimension
+ 2;
114 Matrix
*M
= Matrix_Alloc(P
->NbRays
, dim
);
118 value_set_si(mone
, -1);
119 for (i
= 0; i
< P
->NbRays
; ++i
) {
120 Vector_Scale(P
->Ray
[i
], M
->p
[i
], mone
, dim
);
121 value_multiply(M
->p
[i
][0], M
->p
[i
][0], mone
);
122 value_multiply(M
->p
[i
][dim
-1], M
->p
[i
][dim
-1], mone
);
124 P
= Constraints2Polyhedron(M
, NbMaxRays
);
132 * Returns the supporting cone of P at the vertex with index v
134 Polyhedron
* supporting_cone(Polyhedron
*P
, int v
)
139 unsigned char *supporting
= (unsigned char *)malloc(P
->NbConstraints
);
140 unsigned dim
= P
->Dimension
+ 2;
142 assert(v
>=0 && v
< P
->NbRays
);
143 assert(value_pos_p(P
->Ray
[v
][dim
-1]));
147 for (i
= 0, n
= 0; i
< P
->NbConstraints
; ++i
) {
148 Inner_Product(P
->Constraint
[i
] + 1, P
->Ray
[v
] + 1, dim
- 1, &tmp
);
149 if ((supporting
[i
] = value_zero_p(tmp
)))
152 assert(n
>= dim
- 2);
154 M
= Matrix_Alloc(n
, dim
);
156 for (i
= 0, j
= 0; i
< P
->NbConstraints
; ++i
)
158 value_set_si(M
->p
[j
][dim
-1], 0);
159 Vector_Copy(P
->Constraint
[i
], M
->p
[j
++], dim
-1);
162 P
= Constraints2Polyhedron(M
, P
->NbRays
+1);
168 #define INT_BITS (sizeof(unsigned) * 8)
170 unsigned *supporting_constraints(Matrix
*Constraints
, Param_Vertices
*v
, int *n
)
172 Value lcm
, tmp
, tmp2
;
173 unsigned dim
= Constraints
->NbColumns
;
174 unsigned nparam
= v
->Vertex
->NbColumns
- 2;
175 unsigned nvar
= dim
- nparam
- 2;
176 int len
= (Constraints
->NbRows
+INT_BITS
-1)/INT_BITS
;
177 unsigned *supporting
= (unsigned *)calloc(len
, sizeof(unsigned));
184 row
= Vector_Alloc(nparam
+1);
189 value_set_si(lcm
, 1);
190 for (i
= 0, *n
= 0, ix
= 0, bx
= MSB
; i
< Constraints
->NbRows
; ++i
) {
191 Vector_Set(row
->p
, 0, nparam
+1);
192 for (j
= 0 ; j
< nvar
; ++j
) {
193 value_set_si(tmp
, 1);
194 value_assign(tmp2
, Constraints
->p
[i
][j
+1]);
195 if (value_ne(lcm
, v
->Vertex
->p
[j
][nparam
+1])) {
196 value_assign(tmp
, lcm
);
197 value_lcm(lcm
, lcm
, v
->Vertex
->p
[j
][nparam
+1]);
198 value_division(tmp
, lcm
, tmp
);
199 value_multiply(tmp2
, tmp2
, lcm
);
200 value_division(tmp2
, tmp2
, v
->Vertex
->p
[j
][nparam
+1]);
202 Vector_Combine(row
->p
, v
->Vertex
->p
[j
], row
->p
,
203 tmp
, tmp2
, nparam
+1);
205 value_set_si(tmp
, 1);
206 Vector_Combine(row
->p
, Constraints
->p
[i
]+1+nvar
, row
->p
, tmp
, lcm
, nparam
+1);
207 for (j
= 0; j
< nparam
+1; ++j
)
208 if (value_notzero_p(row
->p
[j
]))
210 if (j
== nparam
+ 1) {
211 supporting
[ix
] |= bx
;
225 Polyhedron
* supporting_cone_p(Polyhedron
*P
, Param_Vertices
*v
)
228 unsigned dim
= P
->Dimension
+ 2;
229 unsigned nparam
= v
->Vertex
->NbColumns
- 2;
230 unsigned nvar
= dim
- nparam
- 2;
234 unsigned *supporting
;
237 Polyhedron_Matrix_View(P
, &View
, P
->NbConstraints
);
238 supporting
= supporting_constraints(&View
, v
, &n
);
239 M
= Matrix_Alloc(n
, nvar
+2);
241 for (i
= 0, j
= 0, ix
= 0, bx
= MSB
; i
< P
->NbConstraints
; ++i
) {
242 if (supporting
[ix
] & bx
) {
243 value_set_si(M
->p
[j
][nvar
+1], 0);
244 Vector_Copy(P
->Constraint
[i
], M
->p
[j
++], nvar
+1);
249 P
= Constraints2Polyhedron(M
, P
->NbRays
+1);
255 Polyhedron
* triangulate_cone(Polyhedron
*P
, unsigned NbMaxCons
)
257 struct barvinok_options
*options
= barvinok_options_new_with_defaults();
258 options
->MaxRays
= NbMaxCons
;
259 P
= triangulate_cone_with_options(P
, options
);
260 barvinok_options_free(options
);
264 Polyhedron
* triangulate_cone_with_options(Polyhedron
*P
,
265 struct barvinok_options
*options
)
267 const static int MAX_TRY
=10;
270 unsigned dim
= P
->Dimension
;
271 Matrix
*M
= Matrix_Alloc(P
->NbRays
+1, dim
+3);
273 Polyhedron
*L
, *R
, *T
;
274 assert(P
->NbEq
== 0);
280 Vector_Set(M
->p
[0]+1, 0, dim
+1);
281 value_set_si(M
->p
[0][0], 1);
282 value_set_si(M
->p
[0][dim
+2], 1);
283 Vector_Set(M
->p
[P
->NbRays
]+1, 0, dim
+2);
284 value_set_si(M
->p
[P
->NbRays
][0], 1);
285 value_set_si(M
->p
[P
->NbRays
][dim
+1], 1);
287 for (i
= 0, r
= 1; i
< P
->NbRays
; ++i
) {
288 if (value_notzero_p(P
->Ray
[i
][dim
+1]))
290 Vector_Copy(P
->Ray
[i
], M
->p
[r
], dim
+1);
291 value_set_si(M
->p
[r
][dim
+2], 0);
295 M2
= Matrix_Alloc(dim
+1, dim
+2);
298 if (options
->try_Delaunay_triangulation
) {
299 /* Delaunay triangulation */
300 for (r
= 1; r
< P
->NbRays
; ++r
) {
301 Inner_Product(M
->p
[r
]+1, M
->p
[r
]+1, dim
, &tmp
);
302 value_assign(M
->p
[r
][dim
+1], tmp
);
305 L
= Rays2Polyhedron(M3
, options
->MaxRays
);
310 /* Usually R should still be 0 */
313 for (r
= 1; r
< P
->NbRays
; ++r
) {
314 value_set_si(M
->p
[r
][dim
+1], random_int((t
+1)*dim
*P
->NbRays
)+1);
317 L
= Rays2Polyhedron(M3
, options
->MaxRays
);
321 assert(t
<= MAX_TRY
);
326 POL_ENSURE_FACETS(L
);
327 for (i
= 0; i
< L
->NbConstraints
; ++i
) {
328 /* Ignore perpendicular facets, i.e., facets with 0 z-coordinate */
329 if (value_negz_p(L
->Constraint
[i
][dim
+1]))
331 if (value_notzero_p(L
->Constraint
[i
][dim
+2]))
333 for (j
= 1, r
= 1; j
< M
->NbRows
; ++j
) {
334 Inner_Product(M
->p
[j
]+1, L
->Constraint
[i
]+1, dim
+1, &tmp
);
335 if (value_notzero_p(tmp
))
339 Vector_Copy(M
->p
[j
]+1, M2
->p
[r
]+1, dim
);
340 value_set_si(M2
->p
[r
][0], 1);
341 value_set_si(M2
->p
[r
][dim
+1], 0);
345 Vector_Set(M2
->p
[0]+1, 0, dim
);
346 value_set_si(M2
->p
[0][0], 1);
347 value_set_si(M2
->p
[0][dim
+1], 1);
348 T
= Rays2Polyhedron(M2
, P
->NbConstraints
+1);
362 void check_triangulization(Polyhedron
*P
, Polyhedron
*T
)
364 Polyhedron
*C
, *D
, *E
, *F
, *G
, *U
;
365 for (C
= T
; C
; C
= C
->next
) {
369 U
= DomainConvex(DomainUnion(U
, C
, 100), 100);
370 for (D
= C
->next
; D
; D
= D
->next
) {
375 E
= DomainIntersection(C
, D
, 600);
376 assert(E
->NbRays
== 0 || E
->NbEq
>= 1);
382 assert(PolyhedronIncludes(U
, P
));
383 assert(PolyhedronIncludes(P
, U
));
386 /* Computes x, y and g such that g = gcd(a,b) and a*x+b*y = g */
387 void Extended_Euclid(Value a
, Value b
, Value
*x
, Value
*y
, Value
*g
)
389 Value c
, d
, e
, f
, tmp
;
396 value_absolute(c
, a
);
397 value_absolute(d
, b
);
400 while(value_pos_p(d
)) {
401 value_division(tmp
, c
, d
);
402 value_multiply(tmp
, tmp
, f
);
403 value_subtract(e
, e
, tmp
);
404 value_division(tmp
, c
, d
);
405 value_multiply(tmp
, tmp
, d
);
406 value_subtract(c
, c
, tmp
);
413 else if (value_pos_p(a
))
415 else value_oppose(*x
, e
);
419 value_multiply(tmp
, a
, *x
);
420 value_subtract(tmp
, c
, tmp
);
421 value_division(*y
, tmp
, b
);
430 static int unimodular_complete_1(Matrix
*m
)
432 Value g
, b
, c
, old
, tmp
;
441 value_assign(g
, m
->p
[0][0]);
442 for (i
= 1; value_zero_p(g
) && i
< m
->NbColumns
; ++i
) {
443 for (j
= 0; j
< m
->NbColumns
; ++j
) {
445 value_set_si(m
->p
[i
][j
], 1);
447 value_set_si(m
->p
[i
][j
], 0);
449 value_assign(g
, m
->p
[0][i
]);
451 for (; i
< m
->NbColumns
; ++i
) {
452 value_assign(old
, g
);
453 Extended_Euclid(old
, m
->p
[0][i
], &c
, &b
, &g
);
455 for (j
= 0; j
< m
->NbColumns
; ++j
) {
457 value_multiply(tmp
, m
->p
[0][j
], b
);
458 value_division(m
->p
[i
][j
], tmp
, old
);
460 value_assign(m
->p
[i
][j
], c
);
462 value_set_si(m
->p
[i
][j
], 0);
474 int unimodular_complete(Matrix
*M
, int row
)
481 return unimodular_complete_1(M
);
483 left_hermite(M
, &H
, &Q
, &U
);
485 for (r
= 0; ok
&& r
< row
; ++r
)
486 if (value_notone_p(H
->p
[r
][r
]))
489 for (r
= row
; r
< M
->NbRows
; ++r
)
490 Vector_Copy(Q
->p
[r
], M
->p
[r
], M
->NbColumns
);
496 * left_hermite may leave positive entries below the main diagonal in H.
497 * This function postprocesses the output of left_hermite to make
498 * the non-zero entries below the main diagonal negative.
500 void neg_left_hermite(Matrix
*A
, Matrix
**H_p
, Matrix
**Q_p
, Matrix
**U_p
)
505 left_hermite(A
, &H
, &Q
, &U
);
510 for (row
= 0, col
= 0; col
< H
->NbColumns
; ++col
, ++row
) {
511 while (value_zero_p(H
->p
[row
][col
]))
513 for (i
= 0; i
< col
; ++i
) {
514 if (value_negz_p(H
->p
[row
][i
]))
517 /* subtract column col from column i in H and U */
518 for (j
= 0; j
< H
->NbRows
; ++j
)
519 value_subtract(H
->p
[j
][i
], H
->p
[j
][i
], H
->p
[j
][col
]);
520 for (j
= 0; j
< U
->NbRows
; ++j
)
521 value_subtract(U
->p
[j
][i
], U
->p
[j
][i
], U
->p
[j
][col
]);
523 /* add row i to row col in Q */
524 for (j
= 0; j
< Q
->NbColumns
; ++j
)
525 value_addto(Q
->p
[col
][j
], Q
->p
[col
][j
], Q
->p
[i
][j
]);
531 * Returns a full-dimensional polyhedron with the same number
532 * of integer points as P
534 Polyhedron
*remove_equalities(Polyhedron
*P
, unsigned MaxRays
)
538 Polyhedron
*Q
= Polyhedron_Copy(P
);
539 unsigned dim
= P
->Dimension
;
544 Q
= DomainConstraintSimplify(Q
, MaxRays
);
548 Polyhedron_Matrix_View(Q
, &M
, Q
->NbEq
);
549 T
= compress_variables(&M
, 0);
554 P
= Polyhedron_Preimage(Q
, T
, MaxRays
);
564 * Returns a full-dimensional polyhedron with the same number
565 * of integer points as P
566 * nvar specifies the number of variables
567 * The remaining dimensions are assumed to be parameters
569 * factor is NbEq x (nparam+2) matrix, containing stride constraints
570 * on the parameters; column nparam is the constant;
571 * column nparam+1 is the stride
573 * if factor is NULL, only remove equalities that don't affect
574 * the number of points
576 Polyhedron
*remove_equalities_p(Polyhedron
*P
, unsigned nvar
, Matrix
**factor
,
581 unsigned dim
= P
->Dimension
;
588 m1
= Matrix_Alloc(nvar
, nvar
);
589 P
= DomainConstraintSimplify(P
, MaxRays
);
591 f
= Matrix_Alloc(P
->NbEq
, dim
-nvar
+2);
595 for (i
= 0, j
= 0; i
< P
->NbEq
; ++i
) {
596 if (First_Non_Zero(P
->Constraint
[i
]+1, nvar
) == -1)
599 Vector_Gcd(P
->Constraint
[i
]+1, nvar
, &g
);
600 if (!factor
&& value_notone_p(g
))
604 Vector_Copy(P
->Constraint
[i
]+1+nvar
, f
->p
[j
], dim
-nvar
+1);
605 value_assign(f
->p
[j
][dim
-nvar
+1], g
);
608 Vector_Copy(P
->Constraint
[i
]+1, m1
->p
[j
], nvar
);
614 unimodular_complete(m1
, j
);
616 m2
= Matrix_Alloc(dim
+1-j
, dim
+1);
617 for (i
= 0; i
< nvar
-j
; ++i
)
618 Vector_Copy(m1
->p
[i
+j
], m2
->p
[i
], nvar
);
620 for (i
= nvar
-j
; i
<= dim
-j
; ++i
)
621 value_set_si(m2
->p
[i
][i
+j
], 1);
623 Q
= Polyhedron_Image(P
, m2
, MaxRays
);
630 void Line_Length(Polyhedron
*P
, Value
*len
)
636 assert(P
->Dimension
== 1);
639 if (mpz_divisible_p(P
->Constraint
[0][2], P
->Constraint
[0][1]))
640 value_set_si(*len
, 1);
642 value_set_si(*len
, 0);
650 for (i
= 0; i
< P
->NbConstraints
; ++i
) {
651 value_oppose(tmp
, P
->Constraint
[i
][2]);
652 if (value_pos_p(P
->Constraint
[i
][1])) {
653 mpz_cdiv_q(tmp
, tmp
, P
->Constraint
[i
][1]);
654 if (!p
|| value_gt(tmp
, pos
))
655 value_assign(pos
, tmp
);
657 } else if (value_neg_p(P
->Constraint
[i
][1])) {
658 mpz_fdiv_q(tmp
, tmp
, P
->Constraint
[i
][1]);
659 if (!n
|| value_lt(tmp
, neg
))
660 value_assign(neg
, tmp
);
664 value_subtract(tmp
, neg
, pos
);
665 value_increment(*len
, tmp
);
667 value_set_si(*len
, -1);
675 /* Update group[k] to the group column k belongs to.
676 * When merging two groups, only the group of the current
677 * group leader is changed. Here we change the group of
678 * the other members to also point to the group that the
679 * old group leader now points to.
681 static void update_group(int *group
, int *cnt
, int k
)
690 * Factors the polyhedron P into polyhedra Q_i such that
691 * the number of integer points in P is equal to the product
692 * of the number of integer points in the individual Q_i
694 * If no factors can be found, NULL is returned.
695 * Otherwise, a linked list of the factors is returned.
697 * If there are factors and if T is not NULL, then a matrix will be
698 * returned through T expressing the old variables in terms of the
699 * new variables as they appear in the sequence of factors.
701 * The algorithm works by first computing the Hermite normal form
702 * and then grouping columns linked by one or more constraints together,
703 * where a constraints "links" two or more columns if the constraint
704 * has nonzero coefficients in the columns.
706 Polyhedron
* Polyhedron_Factor(Polyhedron
*P
, unsigned nparam
, Matrix
**T
,
710 Matrix
*M
, *H
, *Q
, *U
;
711 int *pos
; /* for each column: row position of pivot */
712 int *group
; /* group to which a column belongs */
713 int *cnt
; /* number of columns in the group */
714 int *rowgroup
; /* group to which a constraint belongs */
715 int nvar
= P
->Dimension
- nparam
;
716 Polyhedron
*F
= NULL
;
724 NALLOC(rowgroup
, P
->NbConstraints
);
726 M
= Matrix_Alloc(P
->NbConstraints
, nvar
);
727 for (i
= 0; i
< P
->NbConstraints
; ++i
)
728 Vector_Copy(P
->Constraint
[i
]+1, M
->p
[i
], nvar
);
729 left_hermite(M
, &H
, &Q
, &U
);
733 for (i
= 0; i
< P
->NbConstraints
; ++i
)
735 for (i
= 0, j
= 0; i
< H
->NbColumns
; ++i
) {
736 for ( ; j
< H
->NbRows
; ++j
)
737 if (value_notzero_p(H
->p
[j
][i
]))
741 for (i
= 0; i
< nvar
; ++i
) {
745 for (i
= 0; i
< H
->NbColumns
&& cnt
[0] < nvar
; ++i
) {
746 if (pos
[i
] == H
->NbRows
)
747 continue; /* A line direction */
748 if (rowgroup
[pos
[i
]] == -1)
749 rowgroup
[pos
[i
]] = i
;
750 for (j
= pos
[i
]+1; j
< H
->NbRows
; ++j
) {
751 if (value_zero_p(H
->p
[j
][i
]))
753 if (rowgroup
[j
] != -1)
755 rowgroup
[j
] = group
[i
];
756 for (k
= i
+1; k
< H
->NbColumns
&& j
>= pos
[k
]; ++k
) {
757 update_group(group
, cnt
, k
);
758 update_group(group
, cnt
, i
);
759 if (group
[k
] != group
[i
] && value_notzero_p(H
->p
[j
][k
])) {
760 assert(cnt
[group
[k
]] != 0);
761 assert(cnt
[group
[i
]] != 0);
762 if (group
[i
] < group
[k
]) {
763 cnt
[group
[i
]] += cnt
[group
[k
]];
765 group
[group
[k
]] = group
[i
];
767 cnt
[group
[k
]] += cnt
[group
[i
]];
769 group
[group
[i
]] = group
[k
];
775 for (i
= 1; i
< nvar
; ++i
)
776 update_group(group
, cnt
, i
);
778 if (cnt
[0] != nvar
) {
779 /* Extract out pure context constraints separately */
780 Polyhedron
**next
= &F
;
783 *T
= Matrix_Alloc(nvar
, nvar
);
784 for (i
= nparam
? -1 : 0; i
< nvar
; ++i
) {
788 for (j
= 0, k
= 0; j
< P
->NbConstraints
; ++j
)
789 if (rowgroup
[j
] == -1) {
790 if (First_Non_Zero(P
->Constraint
[j
]+1+nvar
,
803 for (j
= 0, k
= 0; j
< P
->NbConstraints
; ++j
)
804 if (rowgroup
[j
] >= 0 && group
[rowgroup
[j
]] == i
) {
811 for (j
= 0; j
< nvar
; ++j
) {
813 for (l
= 0, m
= 0; m
< d
; ++l
) {
816 value_assign((*T
)->p
[j
][tot_d
+m
++], U
->p
[j
][l
]);
820 M
= Matrix_Alloc(k
, d
+nparam
+2);
821 for (j
= 0, k
= 0; j
< P
->NbConstraints
; ++j
) {
823 if (rowgroup
[j
] != i
)
825 value_assign(M
->p
[k
][0], P
->Constraint
[j
][0]);
826 for (l
= 0, m
= 0; m
< d
; ++l
) {
829 value_assign(M
->p
[k
][1+m
++], H
->p
[j
][l
]);
831 Vector_Copy(P
->Constraint
[j
]+1+nvar
, M
->p
[k
]+1+m
, nparam
+1);
834 *next
= Constraints2Polyhedron(M
, NbMaxRays
);
835 next
= &(*next
)->next
;
849 /* Computes the intersection of the contexts of a list of factors */
850 Polyhedron
*Factor_Context(Polyhedron
*F
, unsigned nparam
, unsigned MaxRays
)
853 Polyhedron
*C
= NULL
;
855 for (Q
= F
; Q
; Q
= Q
->next
) {
857 Polyhedron
*next
= Q
->next
;
860 if (Q
->Dimension
!= nparam
)
861 QC
= Polyhedron_Project(Q
, nparam
);
864 C
= Q
== QC
? Polyhedron_Copy(QC
) : QC
;
867 C
= DomainIntersection(C
, QC
, MaxRays
);
878 * Project on final dim dimensions
880 Polyhedron
* Polyhedron_Project(Polyhedron
*P
, int dim
)
883 int remove
= P
->Dimension
- dim
;
887 if (P
->Dimension
== dim
)
888 return Polyhedron_Copy(P
);
890 T
= Matrix_Alloc(dim
+1, P
->Dimension
+1);
891 for (i
= 0; i
< dim
+1; ++i
)
892 value_set_si(T
->p
[i
][i
+remove
], 1);
893 I
= Polyhedron_Image(P
, T
, P
->NbConstraints
);
898 /* Constructs a new constraint that ensures that
899 * the first constraint is (strictly) smaller than
902 static void smaller_constraint(Value
*a
, Value
*b
, Value
*c
, int pos
, int shift
,
903 int len
, int strict
, Value
*tmp
)
905 value_oppose(*tmp
, b
[pos
+1]);
906 value_set_si(c
[0], 1);
907 Vector_Combine(a
+1+shift
, b
+1+shift
, c
+1, *tmp
, a
[pos
+1], len
-shift
-1);
909 value_decrement(c
[len
-shift
-1], c
[len
-shift
-1]);
910 ConstraintSimplify(c
, c
, len
-shift
, tmp
);
914 /* For each pair of lower and upper bounds on the first variable,
915 * calls fn with the set of constraints on the remaining variables
916 * where these bounds are active, i.e., (stricly) larger/smaller than
917 * the other lower/upper bounds, the lower and upper bound and the
920 * If the first variable is equal to an affine combination of the
921 * other variables then fn is called with both lower and upper
922 * pointing to the corresponding equality.
924 * If there is no lower (or upper) bound, then NULL is passed
925 * as the corresponding bound.
927 void for_each_lower_upper_bound(Polyhedron
*P
,
928 for_each_lower_upper_bound_init init
,
929 for_each_lower_upper_bound_fn fn
,
932 unsigned dim
= P
->Dimension
;
939 if (value_zero_p(P
->Constraint
[0][0]) &&
940 value_notzero_p(P
->Constraint
[0][1])) {
941 M
= Matrix_Alloc(P
->NbConstraints
-1, dim
-1+2);
942 for (i
= 1; i
< P
->NbConstraints
; ++i
) {
943 value_assign(M
->p
[i
-1][0], P
->Constraint
[i
][0]);
944 Vector_Copy(P
->Constraint
[i
]+2, M
->p
[i
-1]+1, dim
);
948 fn(M
, P
->Constraint
[0], P
->Constraint
[0], cb_data
);
954 pos
= ALLOCN(int, P
->NbConstraints
);
956 for (i
= 0, z
= 0; i
< P
->NbConstraints
; ++i
)
957 if (value_zero_p(P
->Constraint
[i
][1]))
958 pos
[P
->NbConstraints
-1 - z
++] = i
;
959 /* put those with positive coefficients first; number: p */
960 for (i
= 0, p
= 0, n
= P
->NbConstraints
-z
-1; i
< P
->NbConstraints
; ++i
)
961 if (value_pos_p(P
->Constraint
[i
][1]))
963 else if (value_neg_p(P
->Constraint
[i
][1]))
965 n
= P
->NbConstraints
-z
-p
;
970 M
= Matrix_Alloc((p
? p
-1 : 0) + (n
? n
-1 : 0) + z
+ 1, dim
-1+2);
971 for (i
= 0; i
< z
; ++i
) {
972 value_assign(M
->p
[i
][0], P
->Constraint
[pos
[P
->NbConstraints
-1 - i
]][0]);
973 Vector_Copy(P
->Constraint
[pos
[P
->NbConstraints
-1 - i
]]+2,
976 for (k
= p
? 0 : -1; k
< p
; ++k
) {
977 for (k2
= 0; k2
< p
; ++k2
) {
980 q
= 1 + z
+ k2
- (k2
> k
);
982 P
->Constraint
[pos
[k
]],
983 P
->Constraint
[pos
[k2
]],
984 M
->p
[q
], 0, 1, dim
+2, k2
> k
, &g
);
986 for (l
= n
? p
: p
-1; l
< p
+n
; ++l
) {
989 for (l2
= p
; l2
< p
+n
; ++l2
) {
992 q
= 1 + z
+ l2
-1 - (l2
> l
);
994 P
->Constraint
[pos
[l2
]],
995 P
->Constraint
[pos
[l
]],
996 M
->p
[q
], 0, 1, dim
+2, l2
> l
, &g
);
999 smaller_constraint(P
->Constraint
[pos
[k
]],
1000 P
->Constraint
[pos
[l
]],
1001 M
->p
[z
], 0, 1, dim
+2, 0, &g
);
1002 lower
= p
? P
->Constraint
[pos
[k
]] : NULL
;
1003 upper
= n
? P
->Constraint
[pos
[l
]] : NULL
;
1004 fn(M
, lower
, upper
, cb_data
);
1013 struct section
{ Polyhedron
* D
; evalue E
; };
1023 static void PLL_init(unsigned n
, void *cb_data
)
1025 struct PLL_data
*data
= (struct PLL_data
*)cb_data
;
1027 data
->s
= ALLOCN(struct section
, n
);
1030 /* Computes ceil(-coef/abs(d)) */
1031 static evalue
* bv_ceil3(Value
*coef
, int len
, Value d
, Polyhedron
*P
)
1035 Vector
*val
= Vector_Alloc(len
);
1038 Vector_Oppose(coef
, val
->p
, len
);
1039 value_absolute(t
, d
);
1041 EP
= ceiling(val
->p
, t
, len
-1, P
);
1049 static void PLL_cb(Matrix
*M
, Value
*lower
, Value
*upper
, void *cb_data
)
1051 struct PLL_data
*data
= (struct PLL_data
*)cb_data
;
1052 unsigned dim
= M
->NbColumns
-1;
1060 M2
= Matrix_Copy(M
);
1061 T
= Constraints2Polyhedron(M2
, data
->MaxRays
);
1063 data
->s
[data
->nd
].D
= DomainIntersection(T
, data
->C
, data
->MaxRays
);
1066 POL_ENSURE_VERTICES(data
->s
[data
->nd
].D
);
1067 if (emptyQ(data
->s
[data
->nd
].D
)) {
1068 Polyhedron_Free(data
->s
[data
->nd
].D
);
1071 L
= bv_ceil3(lower
+1+1, dim
-1+1, lower
[0+1], data
->s
[data
->nd
].D
);
1072 U
= bv_ceil3(upper
+1+1, dim
-1+1, upper
[0+1], data
->s
[data
->nd
].D
);
1074 eadd(&data
->mone
, U
);
1075 emul(&data
->mone
, U
);
1076 data
->s
[data
->nd
].E
= *U
;
1082 static evalue
*ParamLine_Length_mod(Polyhedron
*P
, Polyhedron
*C
, unsigned MaxRays
)
1084 unsigned dim
= P
->Dimension
;
1085 unsigned nvar
= dim
- C
->Dimension
;
1086 struct PLL_data data
;
1092 value_init(data
.mone
.d
);
1093 evalue_set_si(&data
.mone
, -1, 1);
1096 data
.MaxRays
= MaxRays
;
1098 for_each_lower_upper_bound(P
, PLL_init
, PLL_cb
, &data
);
1100 free_evalue_refs(&data
.mone
);
1104 return evalue_zero();
1109 value_set_si(F
->d
, 0);
1110 F
->x
.p
= new_enode(partition
, 2*data
.nd
, dim
-nvar
);
1111 for (k
= 0; k
< data
.nd
; ++k
) {
1112 EVALUE_SET_DOMAIN(F
->x
.p
->arr
[2*k
], data
.s
[k
].D
);
1113 value_clear(F
->x
.p
->arr
[2*k
+1].d
);
1114 F
->x
.p
->arr
[2*k
+1] = data
.s
[k
].E
;
1121 evalue
* ParamLine_Length(Polyhedron
*P
, Polyhedron
*C
,
1122 struct barvinok_options
*options
)
1125 tmp
= ParamLine_Length_mod(P
, C
, options
->MaxRays
);
1126 if (options
->lookup_table
) {
1127 evalue_mod2table(tmp
, C
->Dimension
);
1133 Bool
isIdentity(Matrix
*M
)
1136 if (M
->NbRows
!= M
->NbColumns
)
1139 for (i
= 0;i
< M
->NbRows
; i
++)
1140 for (j
= 0; j
< M
->NbColumns
; j
++)
1142 if(value_notone_p(M
->p
[i
][j
]))
1145 if(value_notzero_p(M
->p
[i
][j
]))
1151 void Param_Polyhedron_Print(FILE* DST
, Param_Polyhedron
*PP
,
1152 const char **param_names
)
1157 for(P
=PP
->D
;P
;P
=P
->next
) {
1159 /* prints current val. dom. */
1160 fprintf(DST
, "---------------------------------------\n");
1161 fprintf(DST
, "Domain :\n");
1162 Print_Domain(DST
, P
->Domain
, param_names
);
1164 /* scan the vertices */
1165 fprintf(DST
, "Vertices :\n");
1166 FORALL_PVertex_in_ParamPolyhedron(V
,P
,PP
) {
1168 /* prints each vertex */
1169 Print_Vertex(DST
, V
->Vertex
, param_names
);
1172 END_FORALL_PVertex_in_ParamPolyhedron
;
1176 void Enumeration_Print(FILE *Dst
, Enumeration
*en
, const char **params
)
1178 for (; en
; en
= en
->next
) {
1179 Print_Domain(Dst
, en
->ValidityDomain
, params
);
1180 print_evalue(Dst
, &en
->EP
, params
);
1184 void Enumeration_Free(Enumeration
*en
)
1190 free_evalue_refs( &(en
->EP
) );
1191 Domain_Free( en
->ValidityDomain
);
1198 void Enumeration_mod2table(Enumeration
*en
, unsigned nparam
)
1200 for (; en
; en
= en
->next
) {
1201 evalue_mod2table(&en
->EP
, nparam
);
1202 reduce_evalue(&en
->EP
);
1206 size_t Enumeration_size(Enumeration
*en
)
1210 for (; en
; en
= en
->next
) {
1211 s
+= domain_size(en
->ValidityDomain
);
1212 s
+= evalue_size(&en
->EP
);
1217 /* Check whether every set in D2 is included in some set of D1 */
1218 int DomainIncludes(Polyhedron
*D1
, Polyhedron
*D2
)
1220 for ( ; D2
; D2
= D2
->next
) {
1222 for (P1
= D1
; P1
; P1
= P1
->next
)
1223 if (PolyhedronIncludes(P1
, D2
))
1231 int line_minmax(Polyhedron
*I
, Value
*min
, Value
*max
)
1236 value_oppose(I
->Constraint
[0][2], I
->Constraint
[0][2]);
1237 /* There should never be a remainder here */
1238 if (value_pos_p(I
->Constraint
[0][1]))
1239 mpz_fdiv_q(*min
, I
->Constraint
[0][2], I
->Constraint
[0][1]);
1241 mpz_fdiv_q(*min
, I
->Constraint
[0][2], I
->Constraint
[0][1]);
1242 value_assign(*max
, *min
);
1243 } else for (i
= 0; i
< I
->NbConstraints
; ++i
) {
1244 if (value_zero_p(I
->Constraint
[i
][1])) {
1249 value_oppose(I
->Constraint
[i
][2], I
->Constraint
[i
][2]);
1250 if (value_pos_p(I
->Constraint
[i
][1]))
1251 mpz_cdiv_q(*min
, I
->Constraint
[i
][2], I
->Constraint
[i
][1]);
1253 mpz_fdiv_q(*max
, I
->Constraint
[i
][2], I
->Constraint
[i
][1]);
1259 int DomainContains(Polyhedron
*P
, Value
*list_args
, int len
,
1260 unsigned MaxRays
, int set
)
1265 if (P
->Dimension
== len
)
1266 return in_domain(P
, list_args
);
1268 assert(set
); // assume list_args is large enough
1269 assert((P
->Dimension
- len
) % 2 == 0);
1271 for (i
= 0; i
< P
->Dimension
- len
; i
+= 2) {
1273 for (j
= 0 ; j
< P
->NbEq
; ++j
)
1274 if (value_notzero_p(P
->Constraint
[j
][1+len
+i
]))
1276 assert(j
< P
->NbEq
);
1277 value_absolute(m
, P
->Constraint
[j
][1+len
+i
]);
1278 k
= First_Non_Zero(P
->Constraint
[j
]+1, len
);
1280 assert(First_Non_Zero(P
->Constraint
[j
]+1+k
+1, len
- k
- 1) == -1);
1281 mpz_fdiv_q(list_args
[len
+i
], list_args
[k
], m
);
1282 mpz_fdiv_r(list_args
[len
+i
+1], list_args
[k
], m
);
1286 return in_domain(P
, list_args
);
1289 Polyhedron
*DomainConcat(Polyhedron
*head
, Polyhedron
*tail
)
1294 for (S
= head
; S
->next
; S
= S
->next
)
1300 evalue
*barvinok_lexsmaller_ev(Polyhedron
*P
, Polyhedron
*D
, unsigned dim
,
1301 Polyhedron
*C
, unsigned MaxRays
)
1304 Polyhedron
*RC
, *RD
, *Q
;
1305 unsigned nparam
= dim
+ C
->Dimension
;
1309 RC
= LexSmaller(P
, D
, dim
, C
, MaxRays
);
1313 exist
= RD
->Dimension
- nparam
- dim
;
1314 CA
= align_context(RC
, RD
->Dimension
, MaxRays
);
1315 Q
= DomainIntersection(RD
, CA
, MaxRays
);
1316 Polyhedron_Free(CA
);
1318 Polyhedron_Free(RC
);
1321 for (Q
= RD
; Q
; Q
= Q
->next
) {
1323 Polyhedron
*next
= Q
->next
;
1326 t
= barvinok_enumerate_e(Q
, exist
, nparam
, MaxRays
);
1343 Enumeration
*barvinok_lexsmaller(Polyhedron
*P
, Polyhedron
*D
, unsigned dim
,
1344 Polyhedron
*C
, unsigned MaxRays
)
1346 evalue
*EP
= barvinok_lexsmaller_ev(P
, D
, dim
, C
, MaxRays
);
1348 return partition2enumeration(EP
);
1351 /* "align" matrix to have nrows by inserting
1352 * the necessary number of rows and an equal number of columns in front
1354 Matrix
*align_matrix(Matrix
*M
, int nrows
)
1357 int newrows
= nrows
- M
->NbRows
;
1358 Matrix
*M2
= Matrix_Alloc(nrows
, newrows
+ M
->NbColumns
);
1359 for (i
= 0; i
< newrows
; ++i
)
1360 value_set_si(M2
->p
[i
][i
], 1);
1361 for (i
= 0; i
< M
->NbRows
; ++i
)
1362 Vector_Copy(M
->p
[i
], M2
->p
[newrows
+i
]+newrows
, M
->NbColumns
);
1366 static void print_varlist(FILE *out
, int n
, char **names
)
1370 for (i
= 0; i
< n
; ++i
) {
1373 fprintf(out
, "%s", names
[i
]);
1378 static void print_term(FILE *out
, Value v
, int pos
, int dim
, int nparam
,
1379 char **iter_names
, char **param_names
, int *first
)
1381 if (value_zero_p(v
)) {
1382 if (first
&& *first
&& pos
>= dim
+ nparam
)
1388 if (!*first
&& value_pos_p(v
))
1392 if (pos
< dim
+ nparam
) {
1393 if (value_mone_p(v
))
1395 else if (!value_one_p(v
))
1396 value_print(out
, VALUE_FMT
, v
);
1398 fprintf(out
, "%s", iter_names
[pos
]);
1400 fprintf(out
, "%s", param_names
[pos
-dim
]);
1402 value_print(out
, VALUE_FMT
, v
);
1405 char **util_generate_names(int n
, const char *prefix
)
1408 int len
= (prefix
? strlen(prefix
) : 0) + 10;
1409 char **names
= ALLOCN(char*, n
);
1411 fprintf(stderr
, "ERROR: memory overflow.\n");
1414 for (i
= 0; i
< n
; ++i
) {
1415 names
[i
] = ALLOCN(char, len
);
1417 fprintf(stderr
, "ERROR: memory overflow.\n");
1421 snprintf(names
[i
], len
, "%d", i
);
1423 snprintf(names
[i
], len
, "%s%d", prefix
, i
);
1429 void util_free_names(int n
, char **names
)
1432 for (i
= 0; i
< n
; ++i
)
1437 void Polyhedron_pprint(FILE *out
, Polyhedron
*P
, int dim
, int nparam
,
1438 char **iter_names
, char **param_names
)
1443 assert(dim
+ nparam
== P
->Dimension
);
1449 print_varlist(out
, nparam
, param_names
);
1450 fprintf(out
, " -> ");
1452 print_varlist(out
, dim
, iter_names
);
1453 fprintf(out
, " : ");
1456 fprintf(out
, "FALSE");
1457 else for (i
= 0; i
< P
->NbConstraints
; ++i
) {
1459 int v
= First_Non_Zero(P
->Constraint
[i
]+1, P
->Dimension
);
1460 if (v
== -1 && value_pos_p(P
->Constraint
[i
][0]))
1463 fprintf(out
, " && ");
1464 if (v
== -1 && value_notzero_p(P
->Constraint
[i
][1+P
->Dimension
]))
1465 fprintf(out
, "FALSE");
1466 else if (value_pos_p(P
->Constraint
[i
][v
+1])) {
1467 print_term(out
, P
->Constraint
[i
][v
+1], v
, dim
, nparam
,
1468 iter_names
, param_names
, NULL
);
1469 if (value_zero_p(P
->Constraint
[i
][0]))
1470 fprintf(out
, " = ");
1472 fprintf(out
, " >= ");
1473 for (j
= v
+1; j
<= dim
+nparam
; ++j
) {
1474 value_oppose(tmp
, P
->Constraint
[i
][1+j
]);
1475 print_term(out
, tmp
, j
, dim
, nparam
,
1476 iter_names
, param_names
, &first
);
1479 value_oppose(tmp
, P
->Constraint
[i
][1+v
]);
1480 print_term(out
, tmp
, v
, dim
, nparam
,
1481 iter_names
, param_names
, NULL
);
1482 fprintf(out
, " <= ");
1483 for (j
= v
+1; j
<= dim
+nparam
; ++j
)
1484 print_term(out
, P
->Constraint
[i
][1+j
], j
, dim
, nparam
,
1485 iter_names
, param_names
, &first
);
1489 fprintf(out
, " }\n");
1494 /* Construct a cone over P with P placed at x_d = 1, with
1495 * x_d the coordinate of an extra dimension
1497 * It's probably a mistake to depend so much on the internal
1498 * representation. We should probably simply compute the
1499 * vertices/facets first.
1501 Polyhedron
*Cone_over_Polyhedron(Polyhedron
*P
)
1503 unsigned NbConstraints
= 0;
1504 unsigned NbRays
= 0;
1508 if (POL_HAS(P
, POL_INEQUALITIES
))
1509 NbConstraints
= P
->NbConstraints
+ 1;
1510 if (POL_HAS(P
, POL_POINTS
))
1511 NbRays
= P
->NbRays
+ 1;
1513 C
= Polyhedron_Alloc(P
->Dimension
+1, NbConstraints
, NbRays
);
1514 if (POL_HAS(P
, POL_INEQUALITIES
)) {
1516 for (i
= 0; i
< P
->NbConstraints
; ++i
)
1517 Vector_Copy(P
->Constraint
[i
], C
->Constraint
[i
], P
->Dimension
+2);
1519 value_set_si(C
->Constraint
[P
->NbConstraints
][0], 1);
1520 value_set_si(C
->Constraint
[P
->NbConstraints
][1+P
->Dimension
], 1);
1522 if (POL_HAS(P
, POL_POINTS
)) {
1523 C
->NbBid
= P
->NbBid
;
1524 for (i
= 0; i
< P
->NbRays
; ++i
)
1525 Vector_Copy(P
->Ray
[i
], C
->Ray
[i
], P
->Dimension
+2);
1527 value_set_si(C
->Ray
[P
->NbRays
][0], 1);
1528 value_set_si(C
->Ray
[P
->NbRays
][1+C
->Dimension
], 1);
1530 POL_SET(C
, POL_VALID
);
1531 if (POL_HAS(P
, POL_INEQUALITIES
))
1532 POL_SET(C
, POL_INEQUALITIES
);
1533 if (POL_HAS(P
, POL_POINTS
))
1534 POL_SET(C
, POL_POINTS
);
1535 if (POL_HAS(P
, POL_VERTICES
))
1536 POL_SET(C
, POL_VERTICES
);
1540 /* Returns a (dim+nparam+1)x((dim-n)+nparam+1) matrix
1541 * mapping the transformed subspace back to the original space.
1542 * n is the number of equalities involving the variables
1543 * (i.e., not purely the parameters).
1544 * The remaining n coordinates in the transformed space would
1545 * have constant (parametric) values and are therefore not
1546 * included in the variables of the new space.
1548 Matrix
*compress_variables(Matrix
*Equalities
, unsigned nparam
)
1550 unsigned dim
= (Equalities
->NbColumns
-2) - nparam
;
1551 Matrix
*M
, *H
, *Q
, *U
, *C
, *ratH
, *invH
, *Ul
, *T1
, *T2
, *T
;
1556 for (n
= 0; n
< Equalities
->NbRows
; ++n
)
1557 if (First_Non_Zero(Equalities
->p
[n
]+1, dim
) == -1)
1560 return Identity(dim
+nparam
+1);
1562 value_set_si(mone
, -1);
1563 M
= Matrix_Alloc(n
, dim
);
1564 C
= Matrix_Alloc(n
+1, nparam
+1);
1565 for (i
= 0; i
< n
; ++i
) {
1566 Vector_Copy(Equalities
->p
[i
]+1, M
->p
[i
], dim
);
1567 Vector_Scale(Equalities
->p
[i
]+1+dim
, C
->p
[i
], mone
, nparam
+1);
1569 value_set_si(C
->p
[n
][nparam
], 1);
1570 left_hermite(M
, &H
, &Q
, &U
);
1575 ratH
= Matrix_Alloc(n
+1, n
+1);
1576 invH
= Matrix_Alloc(n
+1, n
+1);
1577 for (i
= 0; i
< n
; ++i
)
1578 Vector_Copy(H
->p
[i
], ratH
->p
[i
], n
);
1579 value_set_si(ratH
->p
[n
][n
], 1);
1580 ok
= Matrix_Inverse(ratH
, invH
);
1584 T1
= Matrix_Alloc(n
+1, nparam
+1);
1585 Matrix_Product(invH
, C
, T1
);
1588 if (value_notone_p(T1
->p
[n
][nparam
])) {
1589 for (i
= 0; i
< n
; ++i
) {
1590 if (!mpz_divisible_p(T1
->p
[i
][nparam
], T1
->p
[n
][nparam
])) {
1595 /* compress_params should have taken care of this */
1596 for (j
= 0; j
< nparam
; ++j
)
1597 assert(mpz_divisible_p(T1
->p
[i
][j
], T1
->p
[n
][nparam
]));
1598 Vector_AntiScale(T1
->p
[i
], T1
->p
[i
], T1
->p
[n
][nparam
], nparam
+1);
1600 value_set_si(T1
->p
[n
][nparam
], 1);
1602 Ul
= Matrix_Alloc(dim
+1, n
+1);
1603 for (i
= 0; i
< dim
; ++i
)
1604 Vector_Copy(U
->p
[i
], Ul
->p
[i
], n
);
1605 value_set_si(Ul
->p
[dim
][n
], 1);
1606 T2
= Matrix_Alloc(dim
+1, nparam
+1);
1607 Matrix_Product(Ul
, T1
, T2
);
1611 T
= Matrix_Alloc(dim
+nparam
+1, (dim
-n
)+nparam
+1);
1612 for (i
= 0; i
< dim
; ++i
) {
1613 Vector_Copy(U
->p
[i
]+n
, T
->p
[i
], dim
-n
);
1614 Vector_Copy(T2
->p
[i
], T
->p
[i
]+dim
-n
, nparam
+1);
1616 for (i
= 0; i
< nparam
+1; ++i
)
1617 value_set_si(T
->p
[dim
+i
][(dim
-n
)+i
], 1);
1618 assert(value_one_p(T2
->p
[dim
][nparam
]));
1625 /* Computes the left inverse of an affine embedding M and, if Eq is not NULL,
1626 * the equalities that define the affine subspace onto which M maps
1629 Matrix
*left_inverse(Matrix
*M
, Matrix
**Eq
)
1632 Matrix
*L
, *H
, *Q
, *U
, *ratH
, *invH
, *Ut
, *inv
;
1635 if (M
->NbColumns
== 1) {
1636 inv
= Matrix_Alloc(1, M
->NbRows
);
1637 value_set_si(inv
->p
[0][M
->NbRows
-1], 1);
1639 *Eq
= Matrix_Alloc(M
->NbRows
-1, 1+(M
->NbRows
-1)+1);
1640 for (i
= 0; i
< M
->NbRows
-1; ++i
) {
1641 value_oppose((*Eq
)->p
[i
][1+i
], M
->p
[M
->NbRows
-1][0]);
1642 value_assign((*Eq
)->p
[i
][1+(M
->NbRows
-1)], M
->p
[i
][0]);
1649 L
= Matrix_Alloc(M
->NbRows
-1, M
->NbColumns
-1);
1650 for (i
= 0; i
< L
->NbRows
; ++i
)
1651 Vector_Copy(M
->p
[i
], L
->p
[i
], L
->NbColumns
);
1652 right_hermite(L
, &H
, &U
, &Q
);
1655 t
= Vector_Alloc(U
->NbColumns
);
1656 for (i
= 0; i
< U
->NbColumns
; ++i
)
1657 value_oppose(t
->p
[i
], M
->p
[i
][M
->NbColumns
-1]);
1659 *Eq
= Matrix_Alloc(H
->NbRows
- H
->NbColumns
, 2 + U
->NbColumns
);
1660 for (i
= 0; i
< H
->NbRows
- H
->NbColumns
; ++i
) {
1661 Vector_Copy(U
->p
[H
->NbColumns
+i
], (*Eq
)->p
[i
]+1, U
->NbColumns
);
1662 Inner_Product(U
->p
[H
->NbColumns
+i
], t
->p
, U
->NbColumns
,
1663 (*Eq
)->p
[i
]+1+U
->NbColumns
);
1666 ratH
= Matrix_Alloc(H
->NbColumns
+1, H
->NbColumns
+1);
1667 invH
= Matrix_Alloc(H
->NbColumns
+1, H
->NbColumns
+1);
1668 for (i
= 0; i
< H
->NbColumns
; ++i
)
1669 Vector_Copy(H
->p
[i
], ratH
->p
[i
], H
->NbColumns
);
1670 value_set_si(ratH
->p
[ratH
->NbRows
-1][ratH
->NbColumns
-1], 1);
1672 ok
= Matrix_Inverse(ratH
, invH
);
1675 Ut
= Matrix_Alloc(invH
->NbRows
, U
->NbColumns
+1);
1676 for (i
= 0; i
< Ut
->NbRows
-1; ++i
) {
1677 Vector_Copy(U
->p
[i
], Ut
->p
[i
], U
->NbColumns
);
1678 Inner_Product(U
->p
[i
], t
->p
, U
->NbColumns
, &Ut
->p
[i
][Ut
->NbColumns
-1]);
1682 value_set_si(Ut
->p
[Ut
->NbRows
-1][Ut
->NbColumns
-1], 1);
1683 inv
= Matrix_Alloc(invH
->NbRows
, Ut
->NbColumns
);
1684 Matrix_Product(invH
, Ut
, inv
);
1690 /* Check whether all rays are revlex positive in the parameters
1692 int Polyhedron_has_revlex_positive_rays(Polyhedron
*P
, unsigned nparam
)
1695 for (r
= 0; r
< P
->NbRays
; ++r
) {
1697 if (value_notzero_p(P
->Ray
[r
][P
->Dimension
+1]))
1699 for (i
= P
->Dimension
-1; i
>= P
->Dimension
-nparam
; --i
) {
1700 if (value_neg_p(P
->Ray
[r
][i
+1]))
1702 if (value_pos_p(P
->Ray
[r
][i
+1]))
1705 /* A ray independent of the parameters */
1706 if (i
< P
->Dimension
-nparam
)
1712 static Polyhedron
*Recession_Cone(Polyhedron
*P
, unsigned nparam
, unsigned MaxRays
)
1715 unsigned nvar
= P
->Dimension
- nparam
;
1716 Matrix
*M
= Matrix_Alloc(P
->NbConstraints
, 1 + nvar
+ 1);
1718 for (i
= 0; i
< P
->NbConstraints
; ++i
)
1719 Vector_Copy(P
->Constraint
[i
], M
->p
[i
], 1+nvar
);
1720 R
= Constraints2Polyhedron(M
, MaxRays
);
1725 int Polyhedron_is_unbounded(Polyhedron
*P
, unsigned nparam
, unsigned MaxRays
)
1729 Polyhedron
*R
= Recession_Cone(P
, nparam
, MaxRays
);
1730 POL_ENSURE_VERTICES(R
);
1732 for (i
= 0; i
< R
->NbRays
; ++i
)
1733 if (value_zero_p(R
->Ray
[i
][1+R
->Dimension
]))
1735 is_unbounded
= R
->NbBid
> 0 || i
< R
->NbRays
;
1737 return is_unbounded
;
1740 static void SwapColumns(Value
**V
, int n
, int i
, int j
)
1744 for (r
= 0; r
< n
; ++r
)
1745 value_swap(V
[r
][i
], V
[r
][j
]);
1748 void Polyhedron_ExchangeColumns(Polyhedron
*P
, int Column1
, int Column2
)
1750 SwapColumns(P
->Constraint
, P
->NbConstraints
, Column1
, Column2
);
1751 SwapColumns(P
->Ray
, P
->NbRays
, Column1
, Column2
);
1754 Polyhedron_Matrix_View(P
, &M
, P
->NbConstraints
);
1755 Gauss(&M
, P
->NbEq
, P
->Dimension
+1);
1759 /* perform transposition inline; assumes M is a square matrix */
1760 void Matrix_Transposition(Matrix
*M
)
1764 assert(M
->NbRows
== M
->NbColumns
);
1765 for (i
= 0; i
< M
->NbRows
; ++i
)
1766 for (j
= i
+1; j
< M
->NbColumns
; ++j
)
1767 value_swap(M
->p
[i
][j
], M
->p
[j
][i
]);
1770 /* Matrix "view" of first rows rows */
1771 void Polyhedron_Matrix_View(Polyhedron
*P
, Matrix
*M
, unsigned rows
)
1774 M
->NbColumns
= P
->Dimension
+2;
1775 M
->p_Init
= P
->p_Init
;
1776 M
->p
= P
->Constraint
;
1779 int Last_Non_Zero(Value
*p
, unsigned len
)
1783 for (i
= len
- 1; i
>= 0; --i
)
1784 if (value_notzero_p(p
[i
]))