3 #include <barvinok/options.h>
4 #include <barvinok/util.h>
7 #include "lattice_width.h"
8 #include "param_util.h"
9 #include "reduce_domain.h"
11 #define ALLOC(type) (type*)malloc(sizeof(type))
12 #define ALLOCN(type,n) (type*)malloc((n) * sizeof(type))
14 static void clear_width_direction(struct width_direction
*wd
)
16 Vector_Free(wd
->width
);
19 Polyhedron_Free(wd
->domain
);
22 static struct width_direction_array
*new_width_direction_array(void)
24 struct width_direction_array
*dirs
= ALLOC(struct width_direction_array
);
28 dirs
->wd
= ALLOCN(struct width_direction
, dirs
->alloc
);
33 static void grow_width_direction_array(struct width_direction_array
*dirs
,
36 if (dirs
->n
+ extra
<= dirs
->alloc
)
38 dirs
->alloc
= (5*(dirs
->n
+extra
))/4;
39 dirs
->wd
= (struct width_direction
*)realloc(dirs
->wd
,
40 dirs
->alloc
* sizeof(struct width_direction
));
44 void free_width_direction_array(struct width_direction_array
*dirs
)
48 for (i
= 0; i
< dirs
->n
; ++i
)
49 clear_width_direction(&dirs
->wd
[i
]);
54 #define INT_BITS (sizeof(unsigned) * 8)
56 /* For each parametric vertex, compute cone of directions
57 * for which this vertex attains the minimal value.
59 static Matrix
**compute_vertex_dirs(Param_Polyhedron
*PP
)
62 unsigned nvar
= PP
->V
->Vertex
->NbRows
;
64 Matrix
**vertex_dirs
= ALLOCN(Matrix
*, PP
->nbV
);
66 for (i
= 0, V
= PP
->V
; V
; ++i
, V
= V
->next
) {
76 int len
= (PP
->Constraints
->NbRows
+INT_BITS
-1)/INT_BITS
;
78 n
= bit_vector_count(facets
, len
);
80 facets
= supporting_constraints(PP
->Constraints
, V
, &n
);
81 M
= Matrix_Alloc(n
, 1+nvar
+1);
82 for (k
= 0, j
= 0, kx
= 0, bx
= MSB
; j
< n
; ++k
) {
83 if (facets
[kx
] & bx
) {
84 value_set_si(M
->p
[j
][0], 1);
85 Vector_Copy(PP
->Constraints
->p
[k
]+1, M
->p
[j
++]+1, nvar
);
89 P
= Constraints2Polyhedron(M
, 0);
91 vertex_dirs
[i
] = Matrix_Alloc(P
->NbRays
-1, nvar
);
92 for (k
= 0, j
= 0; k
< P
->NbRays
; ++k
) {
93 if (value_notzero_p(P
->Ray
[k
][1+nvar
]))
95 Vector_Copy(P
->Ray
[k
]+1, vertex_dirs
[i
]->p
[j
++], nvar
);
110 static void Vector_Subtract(Value
*a
, Value a_d
,
112 Value
*c
, Value
*c_d
, int len
)
117 value_lcm(*c_d
, a_d
, b_d
);
118 value_divexact(ma
, *c_d
, a_d
);
119 value_divexact(mb
, *c_d
, b_d
);
120 value_oppose(mb
, mb
);
121 Vector_Combine(a
, b
, c
, ma
, mb
, len
);
126 /* Compute width for a given direction dir and initialize width_direction
129 static void compute_width_direction(Matrix
*V_min
, Matrix
*V_max
,
130 Value
*dir
, struct width_direction
*wd
)
132 Vector
*max
= Vector_Alloc(V_min
->NbColumns
);
133 unsigned nvar
= V_min
->NbRows
;
134 unsigned nparam
= V_min
->NbColumns
-2;
136 wd
->width
= Vector_Alloc(V_min
->NbColumns
);
137 wd
->dir
= Vector_Alloc(nvar
);
138 Vector_Copy(dir
, wd
->dir
->p
, nvar
);
144 Vector_Matrix_Product(dir
, V_max
, max
->p
);
145 Vector_Matrix_Product(dir
, V_min
, wd
->width
->p
);
146 Vector_Subtract(max
->p
, V_max
->p
[0][V_max
->NbColumns
],
147 wd
->width
->p
, V_min
->p
[0][V_min
->NbColumns
],
148 wd
->width
->p
, &wd
->width
->p
[nparam
+1],
154 Vector_Normalize(wd
->width
->p
, nparam
+2);
159 static int Vector_Compare(Value
*p1
, Value
*p2
, unsigned len
)
163 for (i
= 0; i
< len
; ++i
) {
164 int sign
= mpz_cmp(p1
[i
], p2
[i
]);
171 static int wd_width_lex_cmp(const void *va
, const void *vb
)
173 const struct width_direction
*a
= (const struct width_direction
*)va
;
174 const struct width_direction
*b
= (const struct width_direction
*)vb
;
176 return Vector_Compare(a
->width
->p
, b
->width
->p
, a
->width
->Size
);
179 static int wd_dir_lex_cmp(const void *va
, const void *vb
)
181 const struct width_direction
*a
= (const struct width_direction
*)va
;
182 const struct width_direction
*b
= (const struct width_direction
*)vb
;
184 return Vector_Compare(a
->dir
->p
, b
->dir
->p
, a
->dir
->Size
);
187 static int add_vertex(Matrix
*M
, int n
, Value
*v
)
190 Matrix_Extend(M
, 3*(M
->NbRows
+10)/2);
191 value_set_si(M
->p
[n
][0], 1);
192 Vector_Copy(v
, M
->p
[n
]+1, M
->NbColumns
-2);
193 value_set_si(M
->p
[n
][M
->NbColumns
-1], 1);
197 /* Puts the points in v that lie in P in front of the list
198 * and returns their number.
200 static int valid_vertices(Polyhedron
*P
, Matrix
*v
, int n_v
)
205 assert(v
->NbColumns
== P
->Dimension
+2);
208 for (j
= 0, k
= 0; j
< n_v
; ++j
) {
209 for (i
= 0; i
< P
->NbConstraints
; ++i
) {
210 Inner_Product(v
->p
[j
]+1, P
->Constraint
[i
]+1, P
->Dimension
+1, &tmp
);
211 if (value_neg_p(tmp
))
214 if (i
< P
->NbConstraints
)
217 Vector_Exchange(v
->p
[j
]+1, v
->p
[k
]+1, P
->Dimension
);
225 static struct width_direction_array
*
226 compute_width_directions(Param_Polyhedron
*PP
, struct barvinok_options
*options
)
228 Matrix
**vertex_dirs
;
229 Param_Vertices
*V_max
, *V_min
;
230 int i
, V_max_i
, V_min_i
;
231 unsigned nvar
= PP
->V
->Vertex
->NbRows
;
232 struct width_direction_array
*width_dirs
= new_width_direction_array();
233 Matrix
*all_vertices
= Matrix_Alloc(nvar
, 1+nvar
+1);
236 vertex_dirs
= compute_vertex_dirs(PP
);
238 for (V_max
= PP
->V
; V_max
; V_max
= V_max
->next
)
239 Param_Vertex_Common_Denominator(V_max
);
241 for (V_max
= PP
->V
, V_max_i
= 0; V_max
; V_max
= V_max
->next
, V_max_i
++) {
242 for (V_min
= V_max
->next
, V_min_i
= V_max_i
+1;
244 V_min
= V_min
->next
, V_min_i
++) {
248 unsigned V_max_n
= vertex_dirs
[V_max_i
]->NbRows
;
249 unsigned V_min_n
= vertex_dirs
[V_min_i
]->NbRows
;
253 if (options
->verbose
)
254 fprintf(stderr
, "%d/%d %d/%d %d \r",
259 M
= Matrix_Alloc(V_max_n
+V_min_n
, 1+nvar
+1);
260 for (i
= 0; i
< V_max_n
; ++i
) {
261 value_set_si(M
->p
[i
][0], 1);
262 Vector_Oppose(vertex_dirs
[V_max_i
]->p
[i
], M
->p
[i
]+1, nvar
);
264 for (i
= 0; i
< V_min_n
; ++i
) {
265 value_set_si(M
->p
[V_max_n
+i
][0], 1);
266 Vector_Copy(vertex_dirs
[V_min_i
]->p
[i
], M
->p
[V_max_n
+i
]+1, nvar
);
268 C
= Constraints2Polyhedron(M
, options
->MaxRays
);
270 n_valid
= valid_vertices(C
, all_vertices
, n_vertices
);
271 basis
= Cone_Integer_Hull(C
, all_vertices
, n_valid
, options
);
272 grow_width_direction_array(width_dirs
, basis
->NbRows
);
273 qsort(width_dirs
->wd
, width_dirs
->n
, sizeof(struct width_direction
),
275 sorted_n
= width_dirs
->n
;
276 for (i
= 0; i
< basis
->NbRows
; ++i
) {
278 struct width_direction wd
;
283 if (bsearch(&wd
, width_dirs
->wd
, sorted_n
,
284 sizeof(struct width_direction
),
288 n_vertices
= add_vertex(all_vertices
, n_vertices
, basis
->p
[i
]);
289 compute_width_direction(V_min
->Vertex
, V_max
->Vertex
,
291 &width_dirs
->wd
[width_dirs
->n
++]);
297 Matrix_Free(all_vertices
);
299 for (i
= 0; i
< PP
->nbV
; ++i
)
300 Matrix_Free(vertex_dirs
[i
]);
306 /* Computes the lattice width direction of a parametric polytope.
307 * The parameter space is allowed to be unbounded.
308 * Currently, the parametric polytope and the parameter space
309 * are assumed to be full-dimensional.
311 * First, we compute the parametric vertices.
312 * Then, for each pair of vertices, we construct a (rational) cone
313 * of directions for which one vertex attains the minimal value
314 * and the other vertex attians the maximal value.
315 * The candidate directions are the elements of the integer hulls
317 * The minimal direction is then obtained by computing the
318 * region in the parameter space where each direction yields
319 * a smaller (or equal) width than all the other directions.
321 * In principle, we can avoid computing candidate directions
322 * for vertices with no overlapping activity domains (possibly
323 * after opening some facets of the activity domains in the
326 * The output is a list of triples, consisting of a direction,
327 * the corresponding width and the chamber in the parameter
328 * space where this direction leads to the minimal width.
330 * The algorithm is described in "Integer points in a parameterised
331 * polyhedron" by Friedrich Eisenbrand and Gennady Shmonin.
333 struct width_direction_array
*
334 Polyhedron_Lattice_Width_Directions(Polyhedron
*P
, Polyhedron
*C
,
335 struct barvinok_options
*options
)
337 Param_Polyhedron
*PP
;
338 unsigned nparam
= C
->Dimension
;
340 struct width_direction_array
*width_dirs
;
344 assert(P
->NbEq
== 0);
345 assert(C
->NbEq
== 0);
347 /* Use true context since the algorithm assumes P is non-empty
348 * for every point in the context.
350 TC
= true_context(P
, C
, options
->MaxRays
);
351 inner
= inner_point(TC
);
353 /* This is overkill, as we discard the computed chambers. */
354 PP
= Polyhedron2Param_Polyhedron(P
, TC
, options
);
356 width_dirs
= compute_width_directions(PP
, options
);
357 Param_Polyhedron_Free(PP
);
359 qsort(width_dirs
->wd
, width_dirs
->n
, sizeof(struct width_direction
),
362 for (i
= 1, j
= 1; i
< width_dirs
->n
; ++i
) {
363 /* We could also weed out width_directions that differ by a
364 * (positive) constant from another width_direction, but then
365 * we'd have to put the two width_directions on a common
368 if (Vector_Equal(width_dirs
->wd
[j
-1].width
->p
,
369 width_dirs
->wd
[i
].width
->p
, nparam
+2))
370 clear_width_direction(&width_dirs
->wd
[i
]);
372 width_dirs
->wd
[j
++] = width_dirs
->wd
[i
];
376 for (i
= 0, k
= 0; i
< width_dirs
->n
; ++i
) {
377 Matrix
*M
= Matrix_Alloc(TC
->NbConstraints
+width_dirs
->n
-(i
-k
)-1, nparam
+2);
378 for (j
= 0; j
< TC
->NbConstraints
; ++j
)
379 Vector_Copy(TC
->Constraint
[j
], M
->p
[j
], nparam
+2);
380 for (j
= 0; j
< width_dirs
->n
; ++j
) {
382 if (k
<= j
&& j
<= i
)
385 pos
= TC
->NbConstraints
+ j
;
387 pos
= TC
->NbConstraints
+ j
- (i
-k
) - 1;
388 Vector_Subtract(width_dirs
->wd
[j
].width
->p
,
389 width_dirs
->wd
[j
].width
->p
[nparam
+1],
390 width_dirs
->wd
[i
].width
->p
,
391 width_dirs
->wd
[i
].width
->p
[nparam
+1],
392 M
->p
[pos
]+1, M
->p
[pos
], nparam
+1);
393 value_set_si(M
->p
[pos
][0], 1);
394 Vector_Normalize(M
->p
[pos
]+1, nparam
+1);
395 if (!is_internal(inner
, M
->p
[pos
]))
396 value_decrement(M
->p
[pos
][nparam
+1], M
->p
[pos
][nparam
+1]);
398 width_dirs
->wd
[i
].domain
= Constraints2Polyhedron(M
, options
->MaxRays
);
399 if (emptyQ(width_dirs
->wd
[i
].domain
))
400 clear_width_direction(&width_dirs
->wd
[i
]);
402 width_dirs
->wd
[k
++] = width_dirs
->wd
[i
];
412 /* Construct evalue of chambers with their associated widths */
413 evalue
*Polyhedron_Lattice_Width(Polyhedron
*P
, Polyhedron
*C
,
414 struct barvinok_options
*options
)
417 struct evalue_section
*s
;
418 struct width_direction_array
*width_dirs
;
420 unsigned nparam
= C
->Dimension
;
422 width_dirs
= Polyhedron_Lattice_Width_Directions(P
, C
, options
);
423 s
= ALLOCN(struct evalue_section
, width_dirs
->n
);
424 for (i
= 0; i
< width_dirs
->n
; ++i
) {
425 s
[i
].D
= width_dirs
->wd
[i
].domain
;
426 width_dirs
->wd
[i
].domain
= NULL
;
427 s
[i
].E
= affine2evalue(width_dirs
->wd
[i
].width
->p
,
428 width_dirs
->wd
[i
].width
->p
[nparam
+1],
431 free_width_direction_array(width_dirs
);
433 width
= evalue_from_section_array(s
, i
);