2 #include <barvinok/options.h>
3 #include <barvinok/util.h>
5 #include "lattice_width.h"
6 #include "param_util.h"
7 #include "reduce_domain.h"
9 #define ALLOC(type) (type*)malloc(sizeof(type))
10 #define ALLOCN(type,n) (type*)malloc((n) * sizeof(type))
12 static void clear_width_direction(struct width_direction
*wd
)
14 Vector_Free(wd
->width
);
17 Polyhedron_Free(wd
->domain
);
20 static struct width_direction_array
*new_width_direction_array(void)
22 struct width_direction_array
*dirs
= ALLOC(struct width_direction_array
);
26 dirs
->wd
= ALLOCN(struct width_direction
, dirs
->alloc
);
31 static void grow_width_direction_array(struct width_direction_array
*dirs
,
34 if (dirs
->n
+ extra
<= dirs
->alloc
)
36 dirs
->alloc
= (5*(dirs
->n
+extra
))/4;
37 dirs
->wd
= (struct width_direction
*)realloc(dirs
->wd
,
38 dirs
->alloc
* sizeof(struct width_direction
));
42 void free_width_direction_array(struct width_direction_array
*dirs
)
46 for (i
= 0; i
< dirs
->n
; ++i
)
47 clear_width_direction(&dirs
->wd
[i
]);
52 #define INT_BITS (sizeof(unsigned) * 8)
54 /* For each parametric vertex, compute cone of directions
55 * for which this vertex attains the minimal value.
57 static Matrix
**compute_vertex_dirs(Param_Polyhedron
*PP
)
60 unsigned nvar
= PP
->V
->Vertex
->NbRows
;
62 Matrix
**vertex_dirs
= ALLOCN(Matrix
*, PP
->nbV
);
64 for (i
= 0, V
= PP
->V
; V
; ++i
, V
= V
->next
) {
74 int len
= (PP
->Constraints
->NbRows
+INT_BITS
-1)/INT_BITS
;
76 n
= bit_vector_count(facets
, len
);
78 facets
= supporting_constraints(PP
->Constraints
, V
, &n
);
79 M
= Matrix_Alloc(n
, 1+nvar
+1);
80 for (k
= 0, j
= 0, kx
= 0, bx
= MSB
; j
< n
; ++k
) {
81 if (facets
[kx
] & bx
) {
82 value_set_si(M
->p
[j
][0], 1);
83 Vector_Copy(PP
->Constraints
->p
[k
]+1, M
->p
[j
++]+1, nvar
);
87 P
= Constraints2Polyhedron(M
, 0);
89 vertex_dirs
[i
] = Matrix_Alloc(P
->NbRays
-1, nvar
);
90 for (k
= 0, j
= 0; k
< P
->NbRays
; ++k
) {
91 if (value_notzero_p(P
->Ray
[k
][1+nvar
]))
93 Vector_Copy(P
->Ray
[k
]+1, vertex_dirs
[i
]->p
[j
++], nvar
);
108 static void Vector_Subtract(Value
*a
, Value a_d
,
110 Value
*c
, Value
*c_d
, int len
)
115 value_lcm(*c_d
, a_d
, b_d
);
116 value_divexact(ma
, *c_d
, a_d
);
117 value_divexact(mb
, *c_d
, b_d
);
118 value_oppose(mb
, mb
);
119 Vector_Combine(a
, b
, c
, ma
, mb
, len
);
124 /* Compute width for a given direction dir and initialize width_direction
127 static void compute_width_direction(Matrix
*V_min
, Matrix
*V_max
,
128 Value
*dir
, struct width_direction
*wd
)
130 Vector
*max
= Vector_Alloc(V_min
->NbColumns
);
131 unsigned nvar
= V_min
->NbRows
;
132 unsigned nparam
= V_min
->NbColumns
-2;
134 wd
->width
= Vector_Alloc(V_min
->NbColumns
);
135 wd
->dir
= Vector_Alloc(nvar
);
136 Vector_Copy(dir
, wd
->dir
->p
, nvar
);
142 Vector_Matrix_Product(dir
, V_max
, max
->p
);
143 Vector_Matrix_Product(dir
, V_min
, wd
->width
->p
);
144 Vector_Subtract(max
->p
, V_max
->p
[0][V_max
->NbColumns
],
145 wd
->width
->p
, V_min
->p
[0][V_min
->NbColumns
],
146 wd
->width
->p
, &wd
->width
->p
[nparam
+1],
152 Vector_Normalize(wd
->width
->p
, nparam
+2);
157 static int Vector_Compare(Value
*p1
, Value
*p2
, unsigned len
)
161 for (i
= 0; i
< len
; ++i
) {
162 int sign
= mpz_cmp(p1
[i
], p2
[i
]);
169 static int wd_width_lex_cmp(const void *va
, const void *vb
)
171 const struct width_direction
*a
= (const struct width_direction
*)va
;
172 const struct width_direction
*b
= (const struct width_direction
*)vb
;
174 return Vector_Compare(a
->width
->p
, b
->width
->p
, a
->width
->Size
);
177 static int wd_dir_lex_cmp(const void *va
, const void *vb
)
179 const struct width_direction
*a
= (const struct width_direction
*)va
;
180 const struct width_direction
*b
= (const struct width_direction
*)vb
;
182 return Vector_Compare(a
->dir
->p
, b
->dir
->p
, a
->dir
->Size
);
185 static struct width_direction_array
*
186 compute_width_directions(Param_Polyhedron
*PP
, struct barvinok_options
*options
)
188 Matrix
**vertex_dirs
;
189 Param_Vertices
*V_max
, *V_min
;
190 int i
, V_max_i
, V_min_i
;
191 unsigned nvar
= PP
->V
->Vertex
->NbRows
;
192 struct width_direction_array
*width_dirs
= new_width_direction_array();
194 vertex_dirs
= compute_vertex_dirs(PP
);
196 for (V_max
= PP
->V
; V_max
; V_max
= V_max
->next
)
197 Param_Vertex_Common_Denominator(V_max
);
199 for (V_max
= PP
->V
, V_max_i
= 0; V_max
; V_max
= V_max
->next
, V_max_i
++) {
200 for (V_min
= V_max
->next
, V_min_i
= V_max_i
+1;
202 V_min
= V_min
->next
, V_min_i
++) {
206 unsigned V_max_n
= vertex_dirs
[V_max_i
]->NbRows
;
207 unsigned V_min_n
= vertex_dirs
[V_min_i
]->NbRows
;
210 if (options
->verbose
)
211 fprintf(stderr
, "%d/%d %d/%d %d \r",
216 M
= Matrix_Alloc(V_max_n
+V_min_n
, 1+nvar
+1);
217 for (i
= 0; i
< V_max_n
; ++i
) {
218 value_set_si(M
->p
[i
][0], 1);
219 Vector_Oppose(vertex_dirs
[V_max_i
]->p
[i
], M
->p
[i
]+1, nvar
);
221 for (i
= 0; i
< V_min_n
; ++i
) {
222 value_set_si(M
->p
[V_max_n
+i
][0], 1);
223 Vector_Copy(vertex_dirs
[V_min_i
]->p
[i
], M
->p
[V_max_n
+i
]+1, nvar
);
225 C
= Constraints2Polyhedron(M
, options
->MaxRays
);
227 basis
= Cone_Integer_Hull(C
, options
);
228 grow_width_direction_array(width_dirs
, basis
->NbRows
);
229 qsort(width_dirs
->wd
, width_dirs
->n
, sizeof(struct width_direction
),
231 sorted_n
= width_dirs
->n
;
232 for (i
= 0; i
< basis
->NbRows
; ++i
) {
234 struct width_direction wd
;
239 if (bsearch(&wd
, width_dirs
->wd
, sorted_n
,
240 sizeof(struct width_direction
),
244 compute_width_direction(V_min
->Vertex
, V_max
->Vertex
,
246 &width_dirs
->wd
[width_dirs
->n
++]);
253 for (i
= 0; i
< PP
->nbV
; ++i
)
254 Matrix_Free(vertex_dirs
[i
]);
260 /* Computes the lattice width direction of a parametric polytope.
261 * The parameter space is allowed to be unbounded.
262 * Currently, the parametric polytope and the parameter space
263 * are assumed to be full-dimensional.
265 * First, we compute the parametric vertices.
266 * Then, for each pair of vertices, we construct a (rational) cone
267 * of directions for which one vertex attains the minimal value
268 * and the other vertex attians the maximal value.
269 * The candidate directions are the elements of the integer hulls
271 * The minimal direction is then obtained by computing the
272 * region in the parameter space where each direction yields
273 * a smaller (or equal) width than all the other directions.
275 * In principle, we can avoid computing candidate directions
276 * for vertices with no overlapping activity domains (possibly
277 * after opening some facets of the activity domains in the
280 * The output is a list of triples, consisting of a direction,
281 * the corresponding width and the chamber in the parameter
282 * space where this direction leads to the minimal width.
284 * The algorithm is described in "Integer points in a parameterised
285 * polyhedron" by Friedrich Eisenbrand and Gennady Shmonin.
287 struct width_direction_array
*
288 Polyhedron_Lattice_Width_Directions(Polyhedron
*P
, Polyhedron
*C
,
289 struct barvinok_options
*options
)
291 Param_Polyhedron
*PP
;
292 unsigned nparam
= C
->Dimension
;
294 struct width_direction_array
*width_dirs
;
298 assert(P
->NbEq
== 0);
299 assert(C
->NbEq
== 0);
301 /* Use true context since the algorithm assumes P is non-empty
302 * for every point in the context.
304 TC
= true_context(P
, C
, options
->MaxRays
);
305 inner
= inner_point(TC
);
307 /* This is overkill, as we discard the computed chambers. */
308 PP
= Polyhedron2Param_Polyhedron(P
, TC
, options
);
310 width_dirs
= compute_width_directions(PP
, options
);
311 Param_Polyhedron_Free(PP
);
313 qsort(width_dirs
->wd
, width_dirs
->n
, sizeof(struct width_direction
),
316 for (i
= 1, j
= 1; i
< width_dirs
->n
; ++i
) {
317 /* We could also weed out width_directions that differ by a
318 * (positive) constant from another width_direction, but then
319 * we'd have to put the two width_directions on a common
322 if (Vector_Equal(width_dirs
->wd
[j
-1].width
->p
,
323 width_dirs
->wd
[i
].width
->p
, nparam
+2))
324 clear_width_direction(&width_dirs
->wd
[i
]);
326 width_dirs
->wd
[j
++] = width_dirs
->wd
[i
];
330 for (i
= 0, k
= 0; i
< width_dirs
->n
; ++i
) {
331 Matrix
*M
= Matrix_Alloc(TC
->NbConstraints
+width_dirs
->n
-(i
-k
)-1, nparam
+2);
332 for (j
= 0; j
< TC
->NbConstraints
; ++j
)
333 Vector_Copy(TC
->Constraint
[j
], M
->p
[j
], nparam
+2);
334 for (j
= 0; j
< width_dirs
->n
; ++j
) {
336 if (k
<= j
&& j
<= i
)
339 pos
= TC
->NbConstraints
+ j
;
341 pos
= TC
->NbConstraints
+ j
- (i
-k
) - 1;
342 Vector_Subtract(width_dirs
->wd
[j
].width
->p
,
343 width_dirs
->wd
[j
].width
->p
[nparam
+1],
344 width_dirs
->wd
[i
].width
->p
,
345 width_dirs
->wd
[i
].width
->p
[nparam
+1],
346 M
->p
[pos
]+1, M
->p
[pos
], nparam
+1);
347 value_set_si(M
->p
[pos
][0], 1);
348 Vector_Normalize(M
->p
[pos
]+1, nparam
+1);
349 if (!is_internal(inner
, M
->p
[pos
]))
350 value_decrement(M
->p
[pos
][nparam
+1], M
->p
[pos
][nparam
+1]);
352 width_dirs
->wd
[i
].domain
= Constraints2Polyhedron(M
, options
->MaxRays
);
353 if (emptyQ(width_dirs
->wd
[i
].domain
))
354 clear_width_direction(&width_dirs
->wd
[i
]);
356 width_dirs
->wd
[k
++] = width_dirs
->wd
[i
];
366 /* Construct evalue of chambers with their associated widths */
367 evalue
*Polyhedron_Lattice_Width(Polyhedron
*P
, Polyhedron
*C
,
368 struct barvinok_options
*options
)
371 struct evalue_section
*s
;
372 struct width_direction_array
*width_dirs
;
374 unsigned nparam
= C
->Dimension
;
376 width_dirs
= Polyhedron_Lattice_Width_Directions(P
, C
, options
);
377 s
= ALLOCN(struct evalue_section
, width_dirs
->n
);
378 for (i
= 0; i
< width_dirs
->n
; ++i
) {
379 s
[i
].D
= width_dirs
->wd
[i
].domain
;
380 width_dirs
->wd
[i
].domain
= NULL
;
381 s
[i
].E
= affine2evalue(width_dirs
->wd
[i
].width
->p
,
382 width_dirs
->wd
[i
].width
->p
[nparam
+1],
385 free_width_direction_array(width_dirs
);
387 width
= evalue_from_section_array(s
, i
);