2 * Copyright 2010 INRIA Saclay
4 * Use of this software is governed by the MIT license
6 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
7 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
11 #include <isl_map_private.h>
13 #include <isl_space_private.h>
17 * Let C be a cone and define
19 * C' := { y | forall x in C : y x >= 0 }
21 * C' contains the coefficients of all linear constraints
22 * that are valid for C.
23 * Furthermore, C'' = C.
25 * If C is defined as { x | A x >= 0 }
26 * then any element in C' must be a non-negative combination
27 * of the rows of A, i.e., y = t A with t >= 0. That is,
29 * C' = { y | exists t >= 0 : y = t A }
31 * If any of the rows in A actually represents an equality, then
32 * also negative combinations of this row are allowed and so the
33 * non-negativity constraint on the corresponding element of t
36 * A polyhedron P = { x | b + A x >= 0 } can be represented
37 * in homogeneous coordinates by the cone
38 * C = { [z,x] | b z + A x >= and z >= 0 }
39 * The valid linear constraints on C correspond to the valid affine
41 * This is essentially Farkas' lemma.
45 * [ w y ] = [t_0 t] [ b A ]
49 * C' = { w, y | exists t_0, t >= 0 : y = t A and w = t_0 + t b }
52 * C' = { w, y | exists t >= 0 : y = t A and w - t b >= 0 }
54 * In practice, we introduce an extra variable (w), shifting all
55 * other variables to the right, and an extra inequality
56 * (w - t b >= 0) corresponding to the positivity constraint on
57 * the homogeneous coordinate.
59 * When going back from coefficients to solutions, we immediately
60 * plug in 1 for z, which corresponds to shifting all variables
61 * to the left, with the leftmost ending up in the constant position.
64 /* Add the given prefix to all named isl_dim_set dimensions in "space".
66 static __isl_give isl_space
*isl_space_prefix(__isl_take isl_space
*space
,
72 size_t prefix_len
= strlen(prefix
);
77 ctx
= isl_space_get_ctx(space
);
78 nvar
= isl_space_dim(space
, isl_dim_set
);
80 for (i
= 0; i
< nvar
; ++i
) {
84 name
= isl_space_get_dim_name(space
, isl_dim_set
, i
);
88 prefix_name
= isl_alloc_array(ctx
, char,
89 prefix_len
+ strlen(name
) + 1);
92 memcpy(prefix_name
, prefix
, prefix_len
);
93 strcpy(prefix_name
+ prefix_len
, name
);
95 space
= isl_space_set_dim_name(space
,
96 isl_dim_set
, i
, prefix_name
);
102 isl_space_free(space
);
106 /* Given a dimension specification of the solutions space, construct
107 * a dimension specification for the space of coefficients.
109 * In particular transform
115 * { coefficients[[cst, params] -> S] }
117 * and prefix each dimension name with "c_".
119 static __isl_give isl_space
*isl_space_coefficients(__isl_take isl_space
*space
)
121 isl_space
*dim_param
;
125 nvar
= isl_space_dim(space
, isl_dim_set
);
126 nparam
= isl_space_dim(space
, isl_dim_param
);
127 dim_param
= isl_space_copy(space
);
128 dim_param
= isl_space_drop_dims(dim_param
, isl_dim_set
, 0, nvar
);
129 dim_param
= isl_space_move_dims(dim_param
, isl_dim_set
, 0,
130 isl_dim_param
, 0, nparam
);
131 dim_param
= isl_space_prefix(dim_param
, "c_");
132 dim_param
= isl_space_insert_dims(dim_param
, isl_dim_set
, 0, 1);
133 dim_param
= isl_space_set_dim_name(dim_param
, isl_dim_set
, 0, "c_cst");
134 space
= isl_space_drop_dims(space
, isl_dim_param
, 0, nparam
);
135 space
= isl_space_prefix(space
, "c_");
136 space
= isl_space_join(isl_space_from_domain(dim_param
),
137 isl_space_from_range(space
));
138 space
= isl_space_wrap(space
);
139 space
= isl_space_set_tuple_name(space
, isl_dim_set
, "coefficients");
144 /* Drop the given prefix from all named dimensions of type "type" in "space".
146 static __isl_give isl_space
*isl_space_unprefix(__isl_take isl_space
*space
,
147 enum isl_dim_type type
, const char *prefix
)
151 size_t prefix_len
= strlen(prefix
);
153 n
= isl_space_dim(space
, type
);
155 for (i
= 0; i
< n
; ++i
) {
158 name
= isl_space_get_dim_name(space
, type
, i
);
161 if (strncmp(name
, prefix
, prefix_len
))
164 space
= isl_space_set_dim_name(space
,
165 type
, i
, name
+ prefix_len
);
171 /* Given a dimension specification of the space of coefficients, construct
172 * a dimension specification for the space of solutions.
174 * In particular transform
176 * { coefficients[[cst, params] -> S] }
182 * and drop the "c_" prefix from the dimension names.
184 static __isl_give isl_space
*isl_space_solutions(__isl_take isl_space
*space
)
188 space
= isl_space_unwrap(space
);
189 space
= isl_space_drop_dims(space
, isl_dim_in
, 0, 1);
190 space
= isl_space_unprefix(space
, isl_dim_in
, "c_");
191 space
= isl_space_unprefix(space
, isl_dim_out
, "c_");
192 nparam
= isl_space_dim(space
, isl_dim_in
);
193 space
= isl_space_move_dims(space
,
194 isl_dim_param
, 0, isl_dim_in
, 0, nparam
);
195 space
= isl_space_range(space
);
200 /* Return the rational universe basic set in the given space.
202 static __isl_give isl_basic_set
*rational_universe(__isl_take isl_space
*space
)
206 bset
= isl_basic_set_universe(space
);
207 bset
= isl_basic_set_set_rational(bset
);
212 /* Compute the dual of "bset" by applying Farkas' lemma.
213 * As explained above, we add an extra dimension to represent
214 * the coefficient of the constant term when going from solutions
215 * to coefficients (shift == 1) and we drop the extra dimension when going
216 * in the opposite direction (shift == -1). "dim" is the space in which
217 * the dual should be created.
219 * If "bset" is (obviously) empty, then the way this emptiness
220 * is represented by the constraints does not allow for the application
221 * of the standard farkas algorithm. We therefore handle this case
222 * specifically and return the universe basic set.
224 static __isl_give isl_basic_set
*farkas(__isl_take isl_space
*space
,
225 __isl_take isl_basic_set
*bset
, int shift
)
228 isl_basic_set
*dual
= NULL
;
231 if (isl_basic_set_plain_is_empty(bset
)) {
232 isl_basic_set_free(bset
);
233 return rational_universe(space
);
236 total
= isl_basic_set_total_dim(bset
);
238 dual
= isl_basic_set_alloc_space(space
, bset
->n_eq
+ bset
->n_ineq
,
239 total
, bset
->n_ineq
+ (shift
> 0));
240 dual
= isl_basic_set_set_rational(dual
);
242 for (i
= 0; i
< bset
->n_eq
+ bset
->n_ineq
; ++i
) {
243 k
= isl_basic_set_alloc_div(dual
);
246 isl_int_set_si(dual
->div
[k
][0], 0);
249 for (i
= 0; i
< total
; ++i
) {
250 k
= isl_basic_set_alloc_equality(dual
);
253 isl_seq_clr(dual
->eq
[k
], 1 + shift
+ total
);
254 isl_int_set_si(dual
->eq
[k
][1 + shift
+ i
], -1);
255 for (j
= 0; j
< bset
->n_eq
; ++j
)
256 isl_int_set(dual
->eq
[k
][1 + shift
+ total
+ j
],
258 for (j
= 0; j
< bset
->n_ineq
; ++j
)
259 isl_int_set(dual
->eq
[k
][1 + shift
+ total
+ bset
->n_eq
+ j
],
260 bset
->ineq
[j
][1 + i
]);
263 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
264 k
= isl_basic_set_alloc_inequality(dual
);
267 isl_seq_clr(dual
->ineq
[k
],
268 1 + shift
+ total
+ bset
->n_eq
+ bset
->n_ineq
);
269 isl_int_set_si(dual
->ineq
[k
][1 + shift
+ total
+ bset
->n_eq
+ i
], 1);
273 k
= isl_basic_set_alloc_inequality(dual
);
276 isl_seq_clr(dual
->ineq
[k
], 2 + total
);
277 isl_int_set_si(dual
->ineq
[k
][1], 1);
278 for (j
= 0; j
< bset
->n_eq
; ++j
)
279 isl_int_neg(dual
->ineq
[k
][2 + total
+ j
],
281 for (j
= 0; j
< bset
->n_ineq
; ++j
)
282 isl_int_neg(dual
->ineq
[k
][2 + total
+ bset
->n_eq
+ j
],
286 dual
= isl_basic_set_remove_divs(dual
);
287 dual
= isl_basic_set_simplify(dual
);
288 dual
= isl_basic_set_finalize(dual
);
290 isl_basic_set_free(bset
);
293 isl_basic_set_free(bset
);
294 isl_basic_set_free(dual
);
298 /* Construct a basic set containing the tuples of coefficients of all
299 * valid affine constraints on the given basic set.
301 __isl_give isl_basic_set
*isl_basic_set_coefficients(
302 __isl_take isl_basic_set
*bset
)
309 isl_die(bset
->ctx
, isl_error_invalid
,
310 "input set not allowed to have local variables",
313 dim
= isl_basic_set_get_space(bset
);
314 dim
= isl_space_coefficients(dim
);
316 return farkas(dim
, bset
, 1);
318 isl_basic_set_free(bset
);
322 /* Construct a basic set containing the elements that satisfy all
323 * affine constraints whose coefficient tuples are
324 * contained in the given basic set.
326 __isl_give isl_basic_set
*isl_basic_set_solutions(
327 __isl_take isl_basic_set
*bset
)
334 isl_die(bset
->ctx
, isl_error_invalid
,
335 "input set not allowed to have local variables",
338 dim
= isl_basic_set_get_space(bset
);
339 dim
= isl_space_solutions(dim
);
341 return farkas(dim
, bset
, -1);
343 isl_basic_set_free(bset
);
347 /* Construct a basic set containing the tuples of coefficients of all
348 * valid affine constraints on the given set.
350 __isl_give isl_basic_set
*isl_set_coefficients(__isl_take isl_set
*set
)
353 isl_basic_set
*coeff
;
358 isl_space
*space
= isl_set_get_space(set
);
359 space
= isl_space_coefficients(space
);
361 return rational_universe(space
);
364 coeff
= isl_basic_set_coefficients(isl_basic_set_copy(set
->p
[0]));
366 for (i
= 1; i
< set
->n
; ++i
) {
367 isl_basic_set
*bset
, *coeff_i
;
368 bset
= isl_basic_set_copy(set
->p
[i
]);
369 coeff_i
= isl_basic_set_coefficients(bset
);
370 coeff
= isl_basic_set_intersect(coeff
, coeff_i
);
377 /* Wrapper around isl_basic_set_coefficients for use
378 * as a isl_basic_set_list_map callback.
380 static __isl_give isl_basic_set
*coefficients_wrap(
381 __isl_take isl_basic_set
*bset
, void *user
)
383 return isl_basic_set_coefficients(bset
);
386 /* Replace the elements of "list" by the result of applying
387 * isl_basic_set_coefficients to them.
389 __isl_give isl_basic_set_list
*isl_basic_set_list_coefficients(
390 __isl_take isl_basic_set_list
*list
)
392 return isl_basic_set_list_map(list
, &coefficients_wrap
, NULL
);
395 /* Construct a basic set containing the elements that satisfy all
396 * affine constraints whose coefficient tuples are
397 * contained in the given set.
399 __isl_give isl_basic_set
*isl_set_solutions(__isl_take isl_set
*set
)
407 isl_space
*space
= isl_set_get_space(set
);
408 space
= isl_space_solutions(space
);
410 return rational_universe(space
);
413 sol
= isl_basic_set_solutions(isl_basic_set_copy(set
->p
[0]));
415 for (i
= 1; i
< set
->n
; ++i
) {
416 isl_basic_set
*bset
, *sol_i
;
417 bset
= isl_basic_set_copy(set
->p
[i
]);
418 sol_i
= isl_basic_set_solutions(bset
);
419 sol
= isl_basic_set_intersect(sol
, sol_i
);