add isl_id_to_pw_aff
[isl.git] / isl_affine_hull.c
blob6ca58bbd3a4379e1c763714f9ba8ec2833541d1c
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
4 * Copyright 2012 Ecole Normale Superieure
6 * Use of this software is governed by the MIT license
8 * Written by Sven Verdoolaege, K.U.Leuven, Departement
9 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
10 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
11 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
12 * and Ecole Normale Superieure, 45 rue d'Ulm, 75230 Paris, France
15 #include <isl_ctx_private.h>
16 #include <isl_map_private.h>
17 #include <isl_seq.h>
18 #include <isl/set.h>
19 #include <isl/lp.h>
20 #include <isl/map.h>
21 #include "isl_equalities.h"
22 #include "isl_sample.h"
23 #include "isl_tab.h"
24 #include <isl_mat_private.h>
25 #include <isl_vec_private.h>
27 struct isl_basic_map *isl_basic_map_implicit_equalities(
28 struct isl_basic_map *bmap)
30 struct isl_tab *tab;
32 if (!bmap)
33 return bmap;
35 bmap = isl_basic_map_gauss(bmap, NULL);
36 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
37 return bmap;
38 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NO_IMPLICIT))
39 return bmap;
40 if (bmap->n_ineq <= 1)
41 return bmap;
43 tab = isl_tab_from_basic_map(bmap, 0);
44 if (isl_tab_detect_implicit_equalities(tab) < 0)
45 goto error;
46 bmap = isl_basic_map_update_from_tab(bmap, tab);
47 isl_tab_free(tab);
48 bmap = isl_basic_map_gauss(bmap, NULL);
49 ISL_F_SET(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
50 return bmap;
51 error:
52 isl_tab_free(tab);
53 isl_basic_map_free(bmap);
54 return NULL;
57 struct isl_basic_set *isl_basic_set_implicit_equalities(
58 struct isl_basic_set *bset)
60 return (struct isl_basic_set *)
61 isl_basic_map_implicit_equalities((struct isl_basic_map*)bset);
64 struct isl_map *isl_map_implicit_equalities(struct isl_map *map)
66 int i;
68 if (!map)
69 return map;
71 for (i = 0; i < map->n; ++i) {
72 map->p[i] = isl_basic_map_implicit_equalities(map->p[i]);
73 if (!map->p[i])
74 goto error;
77 return map;
78 error:
79 isl_map_free(map);
80 return NULL;
83 /* Make eq[row][col] of both bmaps equal so we can add the row
84 * add the column to the common matrix.
85 * Note that because of the echelon form, the columns of row row
86 * after column col are zero.
88 static void set_common_multiple(
89 struct isl_basic_set *bset1, struct isl_basic_set *bset2,
90 unsigned row, unsigned col)
92 isl_int m, c;
94 if (isl_int_eq(bset1->eq[row][col], bset2->eq[row][col]))
95 return;
97 isl_int_init(c);
98 isl_int_init(m);
99 isl_int_lcm(m, bset1->eq[row][col], bset2->eq[row][col]);
100 isl_int_divexact(c, m, bset1->eq[row][col]);
101 isl_seq_scale(bset1->eq[row], bset1->eq[row], c, col+1);
102 isl_int_divexact(c, m, bset2->eq[row][col]);
103 isl_seq_scale(bset2->eq[row], bset2->eq[row], c, col+1);
104 isl_int_clear(c);
105 isl_int_clear(m);
108 /* Delete a given equality, moving all the following equalities one up.
110 static void delete_row(struct isl_basic_set *bset, unsigned row)
112 isl_int *t;
113 int r;
115 t = bset->eq[row];
116 bset->n_eq--;
117 for (r = row; r < bset->n_eq; ++r)
118 bset->eq[r] = bset->eq[r+1];
119 bset->eq[bset->n_eq] = t;
122 /* Make first row entries in column col of bset1 identical to
123 * those of bset2, using the fact that entry bset1->eq[row][col]=a
124 * is non-zero. Initially, these elements of bset1 are all zero.
125 * For each row i < row, we set
126 * A[i] = a * A[i] + B[i][col] * A[row]
127 * B[i] = a * B[i]
128 * so that
129 * A[i][col] = B[i][col] = a * old(B[i][col])
131 static void construct_column(
132 struct isl_basic_set *bset1, struct isl_basic_set *bset2,
133 unsigned row, unsigned col)
135 int r;
136 isl_int a;
137 isl_int b;
138 unsigned total;
140 isl_int_init(a);
141 isl_int_init(b);
142 total = 1 + isl_basic_set_n_dim(bset1);
143 for (r = 0; r < row; ++r) {
144 if (isl_int_is_zero(bset2->eq[r][col]))
145 continue;
146 isl_int_gcd(b, bset2->eq[r][col], bset1->eq[row][col]);
147 isl_int_divexact(a, bset1->eq[row][col], b);
148 isl_int_divexact(b, bset2->eq[r][col], b);
149 isl_seq_combine(bset1->eq[r], a, bset1->eq[r],
150 b, bset1->eq[row], total);
151 isl_seq_scale(bset2->eq[r], bset2->eq[r], a, total);
153 isl_int_clear(a);
154 isl_int_clear(b);
155 delete_row(bset1, row);
158 /* Make first row entries in column col of bset1 identical to
159 * those of bset2, using only these entries of the two matrices.
160 * Let t be the last row with different entries.
161 * For each row i < t, we set
162 * A[i] = (A[t][col]-B[t][col]) * A[i] + (B[i][col]-A[i][col) * A[t]
163 * B[i] = (A[t][col]-B[t][col]) * B[i] + (B[i][col]-A[i][col) * B[t]
164 * so that
165 * A[i][col] = B[i][col] = old(A[t][col]*B[i][col]-A[i][col]*B[t][col])
167 static int transform_column(
168 struct isl_basic_set *bset1, struct isl_basic_set *bset2,
169 unsigned row, unsigned col)
171 int i, t;
172 isl_int a, b, g;
173 unsigned total;
175 for (t = row-1; t >= 0; --t)
176 if (isl_int_ne(bset1->eq[t][col], bset2->eq[t][col]))
177 break;
178 if (t < 0)
179 return 0;
181 total = 1 + isl_basic_set_n_dim(bset1);
182 isl_int_init(a);
183 isl_int_init(b);
184 isl_int_init(g);
185 isl_int_sub(b, bset1->eq[t][col], bset2->eq[t][col]);
186 for (i = 0; i < t; ++i) {
187 isl_int_sub(a, bset2->eq[i][col], bset1->eq[i][col]);
188 isl_int_gcd(g, a, b);
189 isl_int_divexact(a, a, g);
190 isl_int_divexact(g, b, g);
191 isl_seq_combine(bset1->eq[i], g, bset1->eq[i], a, bset1->eq[t],
192 total);
193 isl_seq_combine(bset2->eq[i], g, bset2->eq[i], a, bset2->eq[t],
194 total);
196 isl_int_clear(a);
197 isl_int_clear(b);
198 isl_int_clear(g);
199 delete_row(bset1, t);
200 delete_row(bset2, t);
201 return 1;
204 /* The implementation is based on Section 5.2 of Michael Karr,
205 * "Affine Relationships Among Variables of a Program",
206 * except that the echelon form we use starts from the last column
207 * and that we are dealing with integer coefficients.
209 static struct isl_basic_set *affine_hull(
210 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
212 unsigned total;
213 int col;
214 int row;
216 if (!bset1 || !bset2)
217 goto error;
219 total = 1 + isl_basic_set_n_dim(bset1);
221 row = 0;
222 for (col = total-1; col >= 0; --col) {
223 int is_zero1 = row >= bset1->n_eq ||
224 isl_int_is_zero(bset1->eq[row][col]);
225 int is_zero2 = row >= bset2->n_eq ||
226 isl_int_is_zero(bset2->eq[row][col]);
227 if (!is_zero1 && !is_zero2) {
228 set_common_multiple(bset1, bset2, row, col);
229 ++row;
230 } else if (!is_zero1 && is_zero2) {
231 construct_column(bset1, bset2, row, col);
232 } else if (is_zero1 && !is_zero2) {
233 construct_column(bset2, bset1, row, col);
234 } else {
235 if (transform_column(bset1, bset2, row, col))
236 --row;
239 isl_assert(bset1->ctx, row == bset1->n_eq, goto error);
240 isl_basic_set_free(bset2);
241 bset1 = isl_basic_set_normalize_constraints(bset1);
242 return bset1;
243 error:
244 isl_basic_set_free(bset1);
245 isl_basic_set_free(bset2);
246 return NULL;
249 /* Find an integer point in the set represented by "tab"
250 * that lies outside of the equality "eq" e(x) = 0.
251 * If "up" is true, look for a point satisfying e(x) - 1 >= 0.
252 * Otherwise, look for a point satisfying -e(x) - 1 >= 0 (i.e., e(x) <= -1).
253 * The point, if found, is returned.
254 * If no point can be found, a zero-length vector is returned.
256 * Before solving an ILP problem, we first check if simply
257 * adding the normal of the constraint to one of the known
258 * integer points in the basic set represented by "tab"
259 * yields another point inside the basic set.
261 * The caller of this function ensures that the tableau is bounded or
262 * that tab->basis and tab->n_unbounded have been set appropriately.
264 static struct isl_vec *outside_point(struct isl_tab *tab, isl_int *eq, int up)
266 struct isl_ctx *ctx;
267 struct isl_vec *sample = NULL;
268 struct isl_tab_undo *snap;
269 unsigned dim;
271 if (!tab)
272 return NULL;
273 ctx = tab->mat->ctx;
275 dim = tab->n_var;
276 sample = isl_vec_alloc(ctx, 1 + dim);
277 if (!sample)
278 return NULL;
279 isl_int_set_si(sample->el[0], 1);
280 isl_seq_combine(sample->el + 1,
281 ctx->one, tab->bmap->sample->el + 1,
282 up ? ctx->one : ctx->negone, eq + 1, dim);
283 if (isl_basic_map_contains(tab->bmap, sample))
284 return sample;
285 isl_vec_free(sample);
286 sample = NULL;
288 snap = isl_tab_snap(tab);
290 if (!up)
291 isl_seq_neg(eq, eq, 1 + dim);
292 isl_int_sub_ui(eq[0], eq[0], 1);
294 if (isl_tab_extend_cons(tab, 1) < 0)
295 goto error;
296 if (isl_tab_add_ineq(tab, eq) < 0)
297 goto error;
299 sample = isl_tab_sample(tab);
301 isl_int_add_ui(eq[0], eq[0], 1);
302 if (!up)
303 isl_seq_neg(eq, eq, 1 + dim);
305 if (sample && isl_tab_rollback(tab, snap) < 0)
306 goto error;
308 return sample;
309 error:
310 isl_vec_free(sample);
311 return NULL;
314 struct isl_basic_set *isl_basic_set_recession_cone(struct isl_basic_set *bset)
316 int i;
318 bset = isl_basic_set_cow(bset);
319 if (!bset)
320 return NULL;
321 isl_assert(bset->ctx, bset->n_div == 0, goto error);
323 for (i = 0; i < bset->n_eq; ++i)
324 isl_int_set_si(bset->eq[i][0], 0);
326 for (i = 0; i < bset->n_ineq; ++i)
327 isl_int_set_si(bset->ineq[i][0], 0);
329 ISL_F_CLR(bset, ISL_BASIC_SET_NO_IMPLICIT);
330 return isl_basic_set_implicit_equalities(bset);
331 error:
332 isl_basic_set_free(bset);
333 return NULL;
336 __isl_give isl_set *isl_set_recession_cone(__isl_take isl_set *set)
338 int i;
340 if (!set)
341 return NULL;
342 if (set->n == 0)
343 return set;
345 set = isl_set_remove_divs(set);
346 set = isl_set_cow(set);
347 if (!set)
348 return NULL;
350 for (i = 0; i < set->n; ++i) {
351 set->p[i] = isl_basic_set_recession_cone(set->p[i]);
352 if (!set->p[i])
353 goto error;
356 return set;
357 error:
358 isl_set_free(set);
359 return NULL;
362 /* Move "sample" to a point that is one up (or down) from the original
363 * point in dimension "pos".
365 static void adjacent_point(__isl_keep isl_vec *sample, int pos, int up)
367 if (up)
368 isl_int_add_ui(sample->el[1 + pos], sample->el[1 + pos], 1);
369 else
370 isl_int_sub_ui(sample->el[1 + pos], sample->el[1 + pos], 1);
373 /* Check if any points that are adjacent to "sample" also belong to "bset".
374 * If so, add them to "hull" and return the updated hull.
376 * Before checking whether and adjacent point belongs to "bset", we first
377 * check whether it already belongs to "hull" as this test is typically
378 * much cheaper.
380 static __isl_give isl_basic_set *add_adjacent_points(
381 __isl_take isl_basic_set *hull, __isl_take isl_vec *sample,
382 __isl_keep isl_basic_set *bset)
384 int i, up;
385 int dim;
387 if (!sample)
388 goto error;
390 dim = isl_basic_set_dim(hull, isl_dim_set);
392 for (i = 0; i < dim; ++i) {
393 for (up = 0; up <= 1; ++up) {
394 int contains;
395 isl_basic_set *point;
397 adjacent_point(sample, i, up);
398 contains = isl_basic_set_contains(hull, sample);
399 if (contains < 0)
400 goto error;
401 if (contains) {
402 adjacent_point(sample, i, !up);
403 continue;
405 contains = isl_basic_set_contains(bset, sample);
406 if (contains < 0)
407 goto error;
408 if (contains) {
409 point = isl_basic_set_from_vec(
410 isl_vec_copy(sample));
411 hull = affine_hull(hull, point);
413 adjacent_point(sample, i, !up);
414 if (contains)
415 break;
419 isl_vec_free(sample);
421 return hull;
422 error:
423 isl_vec_free(sample);
424 isl_basic_set_free(hull);
425 return NULL;
428 /* Extend an initial (under-)approximation of the affine hull of basic
429 * set represented by the tableau "tab"
430 * by looking for points that do not satisfy one of the equalities
431 * in the current approximation and adding them to that approximation
432 * until no such points can be found any more.
434 * The caller of this function ensures that "tab" is bounded or
435 * that tab->basis and tab->n_unbounded have been set appropriately.
437 * "bset" may be either NULL or the basic set represented by "tab".
438 * If "bset" is not NULL, we check for any point we find if any
439 * of its adjacent points also belong to "bset".
441 static __isl_give isl_basic_set *extend_affine_hull(struct isl_tab *tab,
442 __isl_take isl_basic_set *hull, __isl_keep isl_basic_set *bset)
444 int i, j;
445 unsigned dim;
447 if (!tab || !hull)
448 goto error;
450 dim = tab->n_var;
452 if (isl_tab_extend_cons(tab, 2 * dim + 1) < 0)
453 goto error;
455 for (i = 0; i < dim; ++i) {
456 struct isl_vec *sample;
457 struct isl_basic_set *point;
458 for (j = 0; j < hull->n_eq; ++j) {
459 sample = outside_point(tab, hull->eq[j], 1);
460 if (!sample)
461 goto error;
462 if (sample->size > 0)
463 break;
464 isl_vec_free(sample);
465 sample = outside_point(tab, hull->eq[j], 0);
466 if (!sample)
467 goto error;
468 if (sample->size > 0)
469 break;
470 isl_vec_free(sample);
472 if (isl_tab_add_eq(tab, hull->eq[j]) < 0)
473 goto error;
475 if (j == hull->n_eq)
476 break;
477 if (tab->samples)
478 tab = isl_tab_add_sample(tab, isl_vec_copy(sample));
479 if (!tab)
480 goto error;
481 if (bset)
482 hull = add_adjacent_points(hull, isl_vec_copy(sample),
483 bset);
484 point = isl_basic_set_from_vec(sample);
485 hull = affine_hull(hull, point);
486 if (!hull)
487 return NULL;
490 return hull;
491 error:
492 isl_basic_set_free(hull);
493 return NULL;
496 /* Drop all constraints in bmap that involve any of the dimensions
497 * first to first+n-1.
499 static __isl_give isl_basic_map *isl_basic_map_drop_constraints_involving(
500 __isl_take isl_basic_map *bmap, unsigned first, unsigned n)
502 int i;
504 if (n == 0)
505 return bmap;
507 bmap = isl_basic_map_cow(bmap);
509 if (!bmap)
510 return NULL;
512 for (i = bmap->n_eq - 1; i >= 0; --i) {
513 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + first, n) == -1)
514 continue;
515 isl_basic_map_drop_equality(bmap, i);
518 for (i = bmap->n_ineq - 1; i >= 0; --i) {
519 if (isl_seq_first_non_zero(bmap->ineq[i] + 1 + first, n) == -1)
520 continue;
521 isl_basic_map_drop_inequality(bmap, i);
524 return bmap;
527 /* Drop all constraints in bset that involve any of the dimensions
528 * first to first+n-1.
530 __isl_give isl_basic_set *isl_basic_set_drop_constraints_involving(
531 __isl_take isl_basic_set *bset, unsigned first, unsigned n)
533 return isl_basic_map_drop_constraints_involving(bset, first, n);
536 /* Drop all constraints in bmap that do not involve any of the dimensions
537 * first to first + n - 1 of the given type.
539 __isl_give isl_basic_map *isl_basic_map_drop_constraints_not_involving_dims(
540 __isl_take isl_basic_map *bmap,
541 enum isl_dim_type type, unsigned first, unsigned n)
543 int i;
544 unsigned dim;
546 if (n == 0)
547 return isl_basic_map_set_to_empty(bmap);
548 bmap = isl_basic_map_cow(bmap);
549 if (!bmap)
550 return NULL;
552 dim = isl_basic_map_dim(bmap, type);
553 if (first + n > dim || first + n < first)
554 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
555 "index out of bounds", return isl_basic_map_free(bmap));
557 first += isl_basic_map_offset(bmap, type) - 1;
559 for (i = bmap->n_eq - 1; i >= 0; --i) {
560 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + first, n) != -1)
561 continue;
562 isl_basic_map_drop_equality(bmap, i);
565 for (i = bmap->n_ineq - 1; i >= 0; --i) {
566 if (isl_seq_first_non_zero(bmap->ineq[i] + 1 + first, n) != -1)
567 continue;
568 isl_basic_map_drop_inequality(bmap, i);
571 return bmap;
574 /* Drop all constraints in bset that do not involve any of the dimensions
575 * first to first + n - 1 of the given type.
577 __isl_give isl_basic_set *isl_basic_set_drop_constraints_not_involving_dims(
578 __isl_take isl_basic_set *bset,
579 enum isl_dim_type type, unsigned first, unsigned n)
581 return isl_basic_map_drop_constraints_not_involving_dims(bset,
582 type, first, n);
585 /* Drop all constraints in bmap that involve any of the dimensions
586 * first to first + n - 1 of the given type.
588 __isl_give isl_basic_map *isl_basic_map_drop_constraints_involving_dims(
589 __isl_take isl_basic_map *bmap,
590 enum isl_dim_type type, unsigned first, unsigned n)
592 unsigned dim;
594 if (!bmap)
595 return NULL;
596 if (n == 0)
597 return bmap;
599 dim = isl_basic_map_dim(bmap, type);
600 if (first + n > dim || first + n < first)
601 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
602 "index out of bounds", return isl_basic_map_free(bmap));
604 bmap = isl_basic_map_remove_divs_involving_dims(bmap, type, first, n);
605 first += isl_basic_map_offset(bmap, type) - 1;
606 return isl_basic_map_drop_constraints_involving(bmap, first, n);
609 /* Drop all constraints in bset that involve any of the dimensions
610 * first to first + n - 1 of the given type.
612 __isl_give isl_basic_set *isl_basic_set_drop_constraints_involving_dims(
613 __isl_take isl_basic_set *bset,
614 enum isl_dim_type type, unsigned first, unsigned n)
616 return isl_basic_map_drop_constraints_involving_dims(bset,
617 type, first, n);
620 /* Drop all constraints in map that involve any of the dimensions
621 * first to first + n - 1 of the given type.
623 __isl_give isl_map *isl_map_drop_constraints_involving_dims(
624 __isl_take isl_map *map,
625 enum isl_dim_type type, unsigned first, unsigned n)
627 int i;
628 unsigned dim;
630 if (!map)
631 return NULL;
632 if (n == 0)
633 return map;
635 dim = isl_map_dim(map, type);
636 if (first + n > dim || first + n < first)
637 isl_die(isl_map_get_ctx(map), isl_error_invalid,
638 "index out of bounds", return isl_map_free(map));
640 map = isl_map_cow(map);
641 if (!map)
642 return NULL;
644 for (i = 0; i < map->n; ++i) {
645 map->p[i] = isl_basic_map_drop_constraints_involving_dims(
646 map->p[i], type, first, n);
647 if (!map->p[i])
648 return isl_map_free(map);
651 return map;
654 /* Drop all constraints in set that involve any of the dimensions
655 * first to first + n - 1 of the given type.
657 __isl_give isl_set *isl_set_drop_constraints_involving_dims(
658 __isl_take isl_set *set,
659 enum isl_dim_type type, unsigned first, unsigned n)
661 return isl_map_drop_constraints_involving_dims(set, type, first, n);
664 /* Construct an initial underapproximatino of the hull of "bset"
665 * from "sample" and any of its adjacent points that also belong to "bset".
667 static __isl_give isl_basic_set *initialize_hull(__isl_keep isl_basic_set *bset,
668 __isl_take isl_vec *sample)
670 isl_basic_set *hull;
672 hull = isl_basic_set_from_vec(isl_vec_copy(sample));
673 hull = add_adjacent_points(hull, sample, bset);
675 return hull;
678 /* Look for all equalities satisfied by the integer points in bset,
679 * which is assumed to be bounded.
681 * The equalities are obtained by successively looking for
682 * a point that is affinely independent of the points found so far.
683 * In particular, for each equality satisfied by the points so far,
684 * we check if there is any point on a hyperplane parallel to the
685 * corresponding hyperplane shifted by at least one (in either direction).
687 static struct isl_basic_set *uset_affine_hull_bounded(struct isl_basic_set *bset)
689 struct isl_vec *sample = NULL;
690 struct isl_basic_set *hull;
691 struct isl_tab *tab = NULL;
692 unsigned dim;
694 if (isl_basic_set_plain_is_empty(bset))
695 return bset;
697 dim = isl_basic_set_n_dim(bset);
699 if (bset->sample && bset->sample->size == 1 + dim) {
700 int contains = isl_basic_set_contains(bset, bset->sample);
701 if (contains < 0)
702 goto error;
703 if (contains) {
704 if (dim == 0)
705 return bset;
706 sample = isl_vec_copy(bset->sample);
707 } else {
708 isl_vec_free(bset->sample);
709 bset->sample = NULL;
713 tab = isl_tab_from_basic_set(bset, 1);
714 if (!tab)
715 goto error;
716 if (tab->empty) {
717 isl_tab_free(tab);
718 isl_vec_free(sample);
719 return isl_basic_set_set_to_empty(bset);
722 if (!sample) {
723 struct isl_tab_undo *snap;
724 snap = isl_tab_snap(tab);
725 sample = isl_tab_sample(tab);
726 if (isl_tab_rollback(tab, snap) < 0)
727 goto error;
728 isl_vec_free(tab->bmap->sample);
729 tab->bmap->sample = isl_vec_copy(sample);
732 if (!sample)
733 goto error;
734 if (sample->size == 0) {
735 isl_tab_free(tab);
736 isl_vec_free(sample);
737 return isl_basic_set_set_to_empty(bset);
740 hull = initialize_hull(bset, sample);
742 hull = extend_affine_hull(tab, hull, bset);
743 isl_basic_set_free(bset);
744 isl_tab_free(tab);
746 return hull;
747 error:
748 isl_vec_free(sample);
749 isl_tab_free(tab);
750 isl_basic_set_free(bset);
751 return NULL;
754 /* Given an unbounded tableau and an integer point satisfying the tableau,
755 * construct an initial affine hull containing the recession cone
756 * shifted to the given point.
758 * The unbounded directions are taken from the last rows of the basis,
759 * which is assumed to have been initialized appropriately.
761 static __isl_give isl_basic_set *initial_hull(struct isl_tab *tab,
762 __isl_take isl_vec *vec)
764 int i;
765 int k;
766 struct isl_basic_set *bset = NULL;
767 struct isl_ctx *ctx;
768 unsigned dim;
770 if (!vec || !tab)
771 return NULL;
772 ctx = vec->ctx;
773 isl_assert(ctx, vec->size != 0, goto error);
775 bset = isl_basic_set_alloc(ctx, 0, vec->size - 1, 0, vec->size - 1, 0);
776 if (!bset)
777 goto error;
778 dim = isl_basic_set_n_dim(bset) - tab->n_unbounded;
779 for (i = 0; i < dim; ++i) {
780 k = isl_basic_set_alloc_equality(bset);
781 if (k < 0)
782 goto error;
783 isl_seq_cpy(bset->eq[k] + 1, tab->basis->row[1 + i] + 1,
784 vec->size - 1);
785 isl_seq_inner_product(bset->eq[k] + 1, vec->el +1,
786 vec->size - 1, &bset->eq[k][0]);
787 isl_int_neg(bset->eq[k][0], bset->eq[k][0]);
789 bset->sample = vec;
790 bset = isl_basic_set_gauss(bset, NULL);
792 return bset;
793 error:
794 isl_basic_set_free(bset);
795 isl_vec_free(vec);
796 return NULL;
799 /* Given a tableau of a set and a tableau of the corresponding
800 * recession cone, detect and add all equalities to the tableau.
801 * If the tableau is bounded, then we can simply keep the
802 * tableau in its state after the return from extend_affine_hull.
803 * However, if the tableau is unbounded, then
804 * isl_tab_set_initial_basis_with_cone will add some additional
805 * constraints to the tableau that have to be removed again.
806 * In this case, we therefore rollback to the state before
807 * any constraints were added and then add the equalities back in.
809 struct isl_tab *isl_tab_detect_equalities(struct isl_tab *tab,
810 struct isl_tab *tab_cone)
812 int j;
813 struct isl_vec *sample;
814 struct isl_basic_set *hull = NULL;
815 struct isl_tab_undo *snap;
817 if (!tab || !tab_cone)
818 goto error;
820 snap = isl_tab_snap(tab);
822 isl_mat_free(tab->basis);
823 tab->basis = NULL;
825 isl_assert(tab->mat->ctx, tab->bmap, goto error);
826 isl_assert(tab->mat->ctx, tab->samples, goto error);
827 isl_assert(tab->mat->ctx, tab->samples->n_col == 1 + tab->n_var, goto error);
828 isl_assert(tab->mat->ctx, tab->n_sample > tab->n_outside, goto error);
830 if (isl_tab_set_initial_basis_with_cone(tab, tab_cone) < 0)
831 goto error;
833 sample = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_var);
834 if (!sample)
835 goto error;
837 isl_seq_cpy(sample->el, tab->samples->row[tab->n_outside], sample->size);
839 isl_vec_free(tab->bmap->sample);
840 tab->bmap->sample = isl_vec_copy(sample);
842 if (tab->n_unbounded == 0)
843 hull = isl_basic_set_from_vec(isl_vec_copy(sample));
844 else
845 hull = initial_hull(tab, isl_vec_copy(sample));
847 for (j = tab->n_outside + 1; j < tab->n_sample; ++j) {
848 isl_seq_cpy(sample->el, tab->samples->row[j], sample->size);
849 hull = affine_hull(hull,
850 isl_basic_set_from_vec(isl_vec_copy(sample)));
853 isl_vec_free(sample);
855 hull = extend_affine_hull(tab, hull, NULL);
856 if (!hull)
857 goto error;
859 if (tab->n_unbounded == 0) {
860 isl_basic_set_free(hull);
861 return tab;
864 if (isl_tab_rollback(tab, snap) < 0)
865 goto error;
867 if (hull->n_eq > tab->n_zero) {
868 for (j = 0; j < hull->n_eq; ++j) {
869 isl_seq_normalize(tab->mat->ctx, hull->eq[j], 1 + tab->n_var);
870 if (isl_tab_add_eq(tab, hull->eq[j]) < 0)
871 goto error;
875 isl_basic_set_free(hull);
877 return tab;
878 error:
879 isl_basic_set_free(hull);
880 isl_tab_free(tab);
881 return NULL;
884 /* Compute the affine hull of "bset", where "cone" is the recession cone
885 * of "bset".
887 * We first compute a unimodular transformation that puts the unbounded
888 * directions in the last dimensions. In particular, we take a transformation
889 * that maps all equalities to equalities (in HNF) on the first dimensions.
890 * Let x be the original dimensions and y the transformed, with y_1 bounded
891 * and y_2 unbounded.
893 * [ y_1 ] [ y_1 ] [ Q_1 ]
894 * x = U [ y_2 ] [ y_2 ] = [ Q_2 ] x
896 * Let's call the input basic set S. We compute S' = preimage(S, U)
897 * and drop the final dimensions including any constraints involving them.
898 * This results in set S''.
899 * Then we compute the affine hull A'' of S''.
900 * Let F y_1 >= g be the constraint system of A''. In the transformed
901 * space the y_2 are unbounded, so we can add them back without any constraints,
902 * resulting in
904 * [ y_1 ]
905 * [ F 0 ] [ y_2 ] >= g
906 * or
907 * [ Q_1 ]
908 * [ F 0 ] [ Q_2 ] x >= g
909 * or
910 * F Q_1 x >= g
912 * The affine hull in the original space is then obtained as
913 * A = preimage(A'', Q_1).
915 static struct isl_basic_set *affine_hull_with_cone(struct isl_basic_set *bset,
916 struct isl_basic_set *cone)
918 unsigned total;
919 unsigned cone_dim;
920 struct isl_basic_set *hull;
921 struct isl_mat *M, *U, *Q;
923 if (!bset || !cone)
924 goto error;
926 total = isl_basic_set_total_dim(cone);
927 cone_dim = total - cone->n_eq;
929 M = isl_mat_sub_alloc6(bset->ctx, cone->eq, 0, cone->n_eq, 1, total);
930 M = isl_mat_left_hermite(M, 0, &U, &Q);
931 if (!M)
932 goto error;
933 isl_mat_free(M);
935 U = isl_mat_lin_to_aff(U);
936 bset = isl_basic_set_preimage(bset, isl_mat_copy(U));
938 bset = isl_basic_set_drop_constraints_involving(bset, total - cone_dim,
939 cone_dim);
940 bset = isl_basic_set_drop_dims(bset, total - cone_dim, cone_dim);
942 Q = isl_mat_lin_to_aff(Q);
943 Q = isl_mat_drop_rows(Q, 1 + total - cone_dim, cone_dim);
945 if (bset && bset->sample && bset->sample->size == 1 + total)
946 bset->sample = isl_mat_vec_product(isl_mat_copy(Q), bset->sample);
948 hull = uset_affine_hull_bounded(bset);
950 if (!hull) {
951 isl_mat_free(Q);
952 isl_mat_free(U);
953 } else {
954 struct isl_vec *sample = isl_vec_copy(hull->sample);
955 U = isl_mat_drop_cols(U, 1 + total - cone_dim, cone_dim);
956 if (sample && sample->size > 0)
957 sample = isl_mat_vec_product(U, sample);
958 else
959 isl_mat_free(U);
960 hull = isl_basic_set_preimage(hull, Q);
961 if (hull) {
962 isl_vec_free(hull->sample);
963 hull->sample = sample;
964 } else
965 isl_vec_free(sample);
968 isl_basic_set_free(cone);
970 return hull;
971 error:
972 isl_basic_set_free(bset);
973 isl_basic_set_free(cone);
974 return NULL;
977 /* Look for all equalities satisfied by the integer points in bset,
978 * which is assumed not to have any explicit equalities.
980 * The equalities are obtained by successively looking for
981 * a point that is affinely independent of the points found so far.
982 * In particular, for each equality satisfied by the points so far,
983 * we check if there is any point on a hyperplane parallel to the
984 * corresponding hyperplane shifted by at least one (in either direction).
986 * Before looking for any outside points, we first compute the recession
987 * cone. The directions of this recession cone will always be part
988 * of the affine hull, so there is no need for looking for any points
989 * in these directions.
990 * In particular, if the recession cone is full-dimensional, then
991 * the affine hull is simply the whole universe.
993 static struct isl_basic_set *uset_affine_hull(struct isl_basic_set *bset)
995 struct isl_basic_set *cone;
997 if (isl_basic_set_plain_is_empty(bset))
998 return bset;
1000 cone = isl_basic_set_recession_cone(isl_basic_set_copy(bset));
1001 if (!cone)
1002 goto error;
1003 if (cone->n_eq == 0) {
1004 struct isl_basic_set *hull;
1005 isl_basic_set_free(cone);
1006 hull = isl_basic_set_universe_like(bset);
1007 isl_basic_set_free(bset);
1008 return hull;
1011 if (cone->n_eq < isl_basic_set_total_dim(cone))
1012 return affine_hull_with_cone(bset, cone);
1014 isl_basic_set_free(cone);
1015 return uset_affine_hull_bounded(bset);
1016 error:
1017 isl_basic_set_free(bset);
1018 return NULL;
1021 /* Look for all equalities satisfied by the integer points in bmap
1022 * that are independent of the equalities already explicitly available
1023 * in bmap.
1025 * We first remove all equalities already explicitly available,
1026 * then look for additional equalities in the reduced space
1027 * and then transform the result to the original space.
1028 * The original equalities are _not_ added to this set. This is
1029 * the responsibility of the calling function.
1030 * The resulting basic set has all meaning about the dimensions removed.
1031 * In particular, dimensions that correspond to existential variables
1032 * in bmap and that are found to be fixed are not removed.
1034 static struct isl_basic_set *equalities_in_underlying_set(
1035 struct isl_basic_map *bmap)
1037 struct isl_mat *T1 = NULL;
1038 struct isl_mat *T2 = NULL;
1039 struct isl_basic_set *bset = NULL;
1040 struct isl_basic_set *hull = NULL;
1042 bset = isl_basic_map_underlying_set(bmap);
1043 if (!bset)
1044 return NULL;
1045 if (bset->n_eq)
1046 bset = isl_basic_set_remove_equalities(bset, &T1, &T2);
1047 if (!bset)
1048 goto error;
1050 hull = uset_affine_hull(bset);
1051 if (!T2)
1052 return hull;
1054 if (!hull) {
1055 isl_mat_free(T1);
1056 isl_mat_free(T2);
1057 } else {
1058 struct isl_vec *sample = isl_vec_copy(hull->sample);
1059 if (sample && sample->size > 0)
1060 sample = isl_mat_vec_product(T1, sample);
1061 else
1062 isl_mat_free(T1);
1063 hull = isl_basic_set_preimage(hull, T2);
1064 if (hull) {
1065 isl_vec_free(hull->sample);
1066 hull->sample = sample;
1067 } else
1068 isl_vec_free(sample);
1071 return hull;
1072 error:
1073 isl_mat_free(T1);
1074 isl_mat_free(T2);
1075 isl_basic_set_free(bset);
1076 isl_basic_set_free(hull);
1077 return NULL;
1080 /* Detect and make explicit all equalities satisfied by the (integer)
1081 * points in bmap.
1083 struct isl_basic_map *isl_basic_map_detect_equalities(
1084 struct isl_basic_map *bmap)
1086 int i, j;
1087 struct isl_basic_set *hull = NULL;
1089 if (!bmap)
1090 return NULL;
1091 if (bmap->n_ineq == 0)
1092 return bmap;
1093 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
1094 return bmap;
1095 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_ALL_EQUALITIES))
1096 return bmap;
1097 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
1098 return isl_basic_map_implicit_equalities(bmap);
1100 hull = equalities_in_underlying_set(isl_basic_map_copy(bmap));
1101 if (!hull)
1102 goto error;
1103 if (ISL_F_ISSET(hull, ISL_BASIC_SET_EMPTY)) {
1104 isl_basic_set_free(hull);
1105 return isl_basic_map_set_to_empty(bmap);
1107 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim), 0,
1108 hull->n_eq, 0);
1109 for (i = 0; i < hull->n_eq; ++i) {
1110 j = isl_basic_map_alloc_equality(bmap);
1111 if (j < 0)
1112 goto error;
1113 isl_seq_cpy(bmap->eq[j], hull->eq[i],
1114 1 + isl_basic_set_total_dim(hull));
1116 isl_vec_free(bmap->sample);
1117 bmap->sample = isl_vec_copy(hull->sample);
1118 isl_basic_set_free(hull);
1119 ISL_F_SET(bmap, ISL_BASIC_MAP_NO_IMPLICIT | ISL_BASIC_MAP_ALL_EQUALITIES);
1120 bmap = isl_basic_map_simplify(bmap);
1121 return isl_basic_map_finalize(bmap);
1122 error:
1123 isl_basic_set_free(hull);
1124 isl_basic_map_free(bmap);
1125 return NULL;
1128 __isl_give isl_basic_set *isl_basic_set_detect_equalities(
1129 __isl_take isl_basic_set *bset)
1131 return (isl_basic_set *)
1132 isl_basic_map_detect_equalities((isl_basic_map *)bset);
1135 __isl_give isl_map *isl_map_detect_equalities(__isl_take isl_map *map)
1137 return isl_map_inline_foreach_basic_map(map,
1138 &isl_basic_map_detect_equalities);
1141 __isl_give isl_set *isl_set_detect_equalities(__isl_take isl_set *set)
1143 return (isl_set *)isl_map_detect_equalities((isl_map *)set);
1146 /* After computing the rational affine hull (by detecting the implicit
1147 * equalities), we compute the additional equalities satisfied by
1148 * the integer points (if any) and add the original equalities back in.
1150 struct isl_basic_map *isl_basic_map_affine_hull(struct isl_basic_map *bmap)
1152 bmap = isl_basic_map_detect_equalities(bmap);
1153 bmap = isl_basic_map_cow(bmap);
1154 if (bmap)
1155 isl_basic_map_free_inequality(bmap, bmap->n_ineq);
1156 bmap = isl_basic_map_finalize(bmap);
1157 return bmap;
1160 struct isl_basic_set *isl_basic_set_affine_hull(struct isl_basic_set *bset)
1162 return (struct isl_basic_set *)
1163 isl_basic_map_affine_hull((struct isl_basic_map *)bset);
1166 /* Given a rational affine matrix "M", add stride constraints to "bmap"
1167 * that ensure that
1169 * M(x)
1171 * is an integer vector. The variables x include all the variables
1172 * of "bmap" except the unknown divs.
1174 * If d is the common denominator of M, then we need to impose that
1176 * d M(x) = 0 mod d
1178 * or
1180 * exists alpha : d M(x) = d alpha
1182 * This function is similar to add_strides in isl_morph.c
1184 static __isl_give isl_basic_map *add_strides(__isl_take isl_basic_map *bmap,
1185 __isl_keep isl_mat *M, int n_known)
1187 int i, div, k;
1188 isl_int gcd;
1190 if (isl_int_is_one(M->row[0][0]))
1191 return bmap;
1193 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
1194 M->n_row - 1, M->n_row - 1, 0);
1196 isl_int_init(gcd);
1197 for (i = 1; i < M->n_row; ++i) {
1198 isl_seq_gcd(M->row[i], M->n_col, &gcd);
1199 if (isl_int_is_divisible_by(gcd, M->row[0][0]))
1200 continue;
1201 div = isl_basic_map_alloc_div(bmap);
1202 if (div < 0)
1203 goto error;
1204 isl_int_set_si(bmap->div[div][0], 0);
1205 k = isl_basic_map_alloc_equality(bmap);
1206 if (k < 0)
1207 goto error;
1208 isl_seq_cpy(bmap->eq[k], M->row[i], M->n_col);
1209 isl_seq_clr(bmap->eq[k] + M->n_col, bmap->n_div - n_known);
1210 isl_int_set(bmap->eq[k][M->n_col - n_known + div],
1211 M->row[0][0]);
1213 isl_int_clear(gcd);
1215 return bmap;
1216 error:
1217 isl_int_clear(gcd);
1218 isl_basic_map_free(bmap);
1219 return NULL;
1222 /* If there are any equalities that involve (multiple) unknown divs,
1223 * then extract the stride information encoded by those equalities
1224 * and make it explicitly available in "bmap".
1226 * We first sort the divs so that the unknown divs appear last and
1227 * then we count how many equalities involve these divs.
1229 * Let these equalities be of the form
1231 * A(x) + B y = 0
1233 * where y represents the unknown divs and x the remaining variables.
1234 * Let [H 0] be the Hermite Normal Form of B, i.e.,
1236 * B = [H 0] Q
1238 * Then x is a solution of the equalities iff
1240 * H^-1 A(x) (= - [I 0] Q y)
1242 * is an integer vector. Let d be the common denominator of H^-1.
1243 * We impose
1245 * d H^-1 A(x) = d alpha
1247 * in add_strides, with alpha fresh existentially quantified variables.
1249 static __isl_give isl_basic_map *isl_basic_map_make_strides_explicit(
1250 __isl_take isl_basic_map *bmap)
1252 int known;
1253 int n_known;
1254 int n, n_col;
1255 int total;
1256 isl_ctx *ctx;
1257 isl_mat *A, *B, *M;
1259 known = isl_basic_map_divs_known(bmap);
1260 if (known < 0)
1261 return isl_basic_map_free(bmap);
1262 if (known)
1263 return bmap;
1264 bmap = isl_basic_map_sort_divs(bmap);
1265 bmap = isl_basic_map_gauss(bmap, NULL);
1266 if (!bmap)
1267 return NULL;
1269 for (n_known = 0; n_known < bmap->n_div; ++n_known)
1270 if (isl_int_is_zero(bmap->div[n_known][0]))
1271 break;
1272 ctx = isl_basic_map_get_ctx(bmap);
1273 total = isl_space_dim(bmap->dim, isl_dim_all);
1274 for (n = 0; n < bmap->n_eq; ++n)
1275 if (isl_seq_first_non_zero(bmap->eq[n] + 1 + total + n_known,
1276 bmap->n_div - n_known) == -1)
1277 break;
1278 if (n == 0)
1279 return bmap;
1280 B = isl_mat_sub_alloc6(ctx, bmap->eq, 0, n, 0, 1 + total + n_known);
1281 n_col = bmap->n_div - n_known;
1282 A = isl_mat_sub_alloc6(ctx, bmap->eq, 0, n, 1 + total + n_known, n_col);
1283 A = isl_mat_left_hermite(A, 0, NULL, NULL);
1284 A = isl_mat_drop_cols(A, n, n_col - n);
1285 A = isl_mat_lin_to_aff(A);
1286 A = isl_mat_right_inverse(A);
1287 B = isl_mat_insert_zero_rows(B, 0, 1);
1288 B = isl_mat_set_element_si(B, 0, 0, 1);
1289 M = isl_mat_product(A, B);
1290 if (!M)
1291 return isl_basic_map_free(bmap);
1292 bmap = add_strides(bmap, M, n_known);
1293 bmap = isl_basic_map_gauss(bmap, NULL);
1294 isl_mat_free(M);
1296 return bmap;
1299 /* Compute the affine hull of each basic map in "map" separately
1300 * and make all stride information explicit so that we can remove
1301 * all unknown divs without losing this information.
1302 * The result is also guaranteed to be gaussed.
1304 * In simple cases where a div is determined by an equality,
1305 * calling isl_basic_map_gauss is enough to make the stride information
1306 * explicit, as it will derive an explicit representation for the div
1307 * from the equality. If, however, the stride information
1308 * is encoded through multiple unknown divs then we need to make
1309 * some extra effort in isl_basic_map_make_strides_explicit.
1311 static __isl_give isl_map *isl_map_local_affine_hull(__isl_take isl_map *map)
1313 int i;
1315 map = isl_map_cow(map);
1316 if (!map)
1317 return NULL;
1319 for (i = 0; i < map->n; ++i) {
1320 map->p[i] = isl_basic_map_affine_hull(map->p[i]);
1321 map->p[i] = isl_basic_map_gauss(map->p[i], NULL);
1322 map->p[i] = isl_basic_map_make_strides_explicit(map->p[i]);
1323 if (!map->p[i])
1324 return isl_map_free(map);
1327 return map;
1330 static __isl_give isl_set *isl_set_local_affine_hull(__isl_take isl_set *set)
1332 return isl_map_local_affine_hull(set);
1335 /* Compute the affine hull of "map".
1337 * We first compute the affine hull of each basic map separately.
1338 * Then we align the divs and recompute the affine hulls of the basic
1339 * maps since some of them may now have extra divs.
1340 * In order to avoid performing parametric integer programming to
1341 * compute explicit expressions for the divs, possible leading to
1342 * an explosion in the number of basic maps, we first drop all unknown
1343 * divs before aligning the divs. Note that isl_map_local_affine_hull tries
1344 * to make sure that all stride information is explicitly available
1345 * in terms of known divs. This involves calling isl_basic_set_gauss,
1346 * which is also needed because affine_hull assumes its input has been gaussed,
1347 * while isl_map_affine_hull may be called on input that has not been gaussed,
1348 * in particular from initial_facet_constraint.
1349 * Similarly, align_divs may reorder some divs so that we need to
1350 * gauss the result again.
1351 * Finally, we combine the individual affine hulls into a single
1352 * affine hull.
1354 __isl_give isl_basic_map *isl_map_affine_hull(__isl_take isl_map *map)
1356 struct isl_basic_map *model = NULL;
1357 struct isl_basic_map *hull = NULL;
1358 struct isl_set *set;
1359 isl_basic_set *bset;
1361 map = isl_map_detect_equalities(map);
1362 map = isl_map_local_affine_hull(map);
1363 map = isl_map_remove_empty_parts(map);
1364 map = isl_map_remove_unknown_divs(map);
1365 map = isl_map_align_divs(map);
1367 if (!map)
1368 return NULL;
1370 if (map->n == 0) {
1371 hull = isl_basic_map_empty_like_map(map);
1372 isl_map_free(map);
1373 return hull;
1376 model = isl_basic_map_copy(map->p[0]);
1377 set = isl_map_underlying_set(map);
1378 set = isl_set_cow(set);
1379 set = isl_set_local_affine_hull(set);
1380 if (!set)
1381 goto error;
1383 while (set->n > 1)
1384 set->p[0] = affine_hull(set->p[0], set->p[--set->n]);
1386 bset = isl_basic_set_copy(set->p[0]);
1387 hull = isl_basic_map_overlying_set(bset, model);
1388 isl_set_free(set);
1389 hull = isl_basic_map_simplify(hull);
1390 return isl_basic_map_finalize(hull);
1391 error:
1392 isl_basic_map_free(model);
1393 isl_set_free(set);
1394 return NULL;
1397 struct isl_basic_set *isl_set_affine_hull(struct isl_set *set)
1399 return (struct isl_basic_set *)
1400 isl_map_affine_hull((struct isl_map *)set);