export isl_multi_aff_drop_dims
[isl.git] / isl_affine_hull.c
blob96af3134ab251e292551404947ba80f0a740537f
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
4 * Use of this software is governed by the GNU LGPLv2.1 license
6 * Written by Sven Verdoolaege, K.U.Leuven, Departement
7 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
8 */
10 #include <isl_ctx_private.h>
11 #include <isl_map_private.h>
12 #include <isl/seq.h>
13 #include <isl/set.h>
14 #include <isl/lp.h>
15 #include <isl/map.h>
16 #include "isl_equalities.h"
17 #include "isl_sample.h"
18 #include "isl_tab.h"
19 #include <isl_mat_private.h>
21 struct isl_basic_map *isl_basic_map_implicit_equalities(
22 struct isl_basic_map *bmap)
24 struct isl_tab *tab;
26 if (!bmap)
27 return bmap;
29 bmap = isl_basic_map_gauss(bmap, NULL);
30 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
31 return bmap;
32 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NO_IMPLICIT))
33 return bmap;
34 if (bmap->n_ineq <= 1)
35 return bmap;
37 tab = isl_tab_from_basic_map(bmap);
38 if (isl_tab_detect_implicit_equalities(tab) < 0)
39 goto error;
40 bmap = isl_basic_map_update_from_tab(bmap, tab);
41 isl_tab_free(tab);
42 bmap = isl_basic_map_gauss(bmap, NULL);
43 ISL_F_SET(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
44 return bmap;
45 error:
46 isl_tab_free(tab);
47 isl_basic_map_free(bmap);
48 return NULL;
51 struct isl_basic_set *isl_basic_set_implicit_equalities(
52 struct isl_basic_set *bset)
54 return (struct isl_basic_set *)
55 isl_basic_map_implicit_equalities((struct isl_basic_map*)bset);
58 struct isl_map *isl_map_implicit_equalities(struct isl_map *map)
60 int i;
62 if (!map)
63 return map;
65 for (i = 0; i < map->n; ++i) {
66 map->p[i] = isl_basic_map_implicit_equalities(map->p[i]);
67 if (!map->p[i])
68 goto error;
71 return map;
72 error:
73 isl_map_free(map);
74 return NULL;
77 /* Make eq[row][col] of both bmaps equal so we can add the row
78 * add the column to the common matrix.
79 * Note that because of the echelon form, the columns of row row
80 * after column col are zero.
82 static void set_common_multiple(
83 struct isl_basic_set *bset1, struct isl_basic_set *bset2,
84 unsigned row, unsigned col)
86 isl_int m, c;
88 if (isl_int_eq(bset1->eq[row][col], bset2->eq[row][col]))
89 return;
91 isl_int_init(c);
92 isl_int_init(m);
93 isl_int_lcm(m, bset1->eq[row][col], bset2->eq[row][col]);
94 isl_int_divexact(c, m, bset1->eq[row][col]);
95 isl_seq_scale(bset1->eq[row], bset1->eq[row], c, col+1);
96 isl_int_divexact(c, m, bset2->eq[row][col]);
97 isl_seq_scale(bset2->eq[row], bset2->eq[row], c, col+1);
98 isl_int_clear(c);
99 isl_int_clear(m);
102 /* Delete a given equality, moving all the following equalities one up.
104 static void delete_row(struct isl_basic_set *bset, unsigned row)
106 isl_int *t;
107 int r;
109 t = bset->eq[row];
110 bset->n_eq--;
111 for (r = row; r < bset->n_eq; ++r)
112 bset->eq[r] = bset->eq[r+1];
113 bset->eq[bset->n_eq] = t;
116 /* Make first row entries in column col of bset1 identical to
117 * those of bset2, using the fact that entry bset1->eq[row][col]=a
118 * is non-zero. Initially, these elements of bset1 are all zero.
119 * For each row i < row, we set
120 * A[i] = a * A[i] + B[i][col] * A[row]
121 * B[i] = a * B[i]
122 * so that
123 * A[i][col] = B[i][col] = a * old(B[i][col])
125 static void construct_column(
126 struct isl_basic_set *bset1, struct isl_basic_set *bset2,
127 unsigned row, unsigned col)
129 int r;
130 isl_int a;
131 isl_int b;
132 unsigned total;
134 isl_int_init(a);
135 isl_int_init(b);
136 total = 1 + isl_basic_set_n_dim(bset1);
137 for (r = 0; r < row; ++r) {
138 if (isl_int_is_zero(bset2->eq[r][col]))
139 continue;
140 isl_int_gcd(b, bset2->eq[r][col], bset1->eq[row][col]);
141 isl_int_divexact(a, bset1->eq[row][col], b);
142 isl_int_divexact(b, bset2->eq[r][col], b);
143 isl_seq_combine(bset1->eq[r], a, bset1->eq[r],
144 b, bset1->eq[row], total);
145 isl_seq_scale(bset2->eq[r], bset2->eq[r], a, total);
147 isl_int_clear(a);
148 isl_int_clear(b);
149 delete_row(bset1, row);
152 /* Make first row entries in column col of bset1 identical to
153 * those of bset2, using only these entries of the two matrices.
154 * Let t be the last row with different entries.
155 * For each row i < t, we set
156 * A[i] = (A[t][col]-B[t][col]) * A[i] + (B[i][col]-A[i][col) * A[t]
157 * B[i] = (A[t][col]-B[t][col]) * B[i] + (B[i][col]-A[i][col) * B[t]
158 * so that
159 * A[i][col] = B[i][col] = old(A[t][col]*B[i][col]-A[i][col]*B[t][col])
161 static int transform_column(
162 struct isl_basic_set *bset1, struct isl_basic_set *bset2,
163 unsigned row, unsigned col)
165 int i, t;
166 isl_int a, b, g;
167 unsigned total;
169 for (t = row-1; t >= 0; --t)
170 if (isl_int_ne(bset1->eq[t][col], bset2->eq[t][col]))
171 break;
172 if (t < 0)
173 return 0;
175 total = 1 + isl_basic_set_n_dim(bset1);
176 isl_int_init(a);
177 isl_int_init(b);
178 isl_int_init(g);
179 isl_int_sub(b, bset1->eq[t][col], bset2->eq[t][col]);
180 for (i = 0; i < t; ++i) {
181 isl_int_sub(a, bset2->eq[i][col], bset1->eq[i][col]);
182 isl_int_gcd(g, a, b);
183 isl_int_divexact(a, a, g);
184 isl_int_divexact(g, b, g);
185 isl_seq_combine(bset1->eq[i], g, bset1->eq[i], a, bset1->eq[t],
186 total);
187 isl_seq_combine(bset2->eq[i], g, bset2->eq[i], a, bset2->eq[t],
188 total);
190 isl_int_clear(a);
191 isl_int_clear(b);
192 isl_int_clear(g);
193 delete_row(bset1, t);
194 delete_row(bset2, t);
195 return 1;
198 /* The implementation is based on Section 5.2 of Michael Karr,
199 * "Affine Relationships Among Variables of a Program",
200 * except that the echelon form we use starts from the last column
201 * and that we are dealing with integer coefficients.
203 static struct isl_basic_set *affine_hull(
204 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
206 unsigned total;
207 int col;
208 int row;
210 if (!bset1 || !bset2)
211 goto error;
213 total = 1 + isl_basic_set_n_dim(bset1);
215 row = 0;
216 for (col = total-1; col >= 0; --col) {
217 int is_zero1 = row >= bset1->n_eq ||
218 isl_int_is_zero(bset1->eq[row][col]);
219 int is_zero2 = row >= bset2->n_eq ||
220 isl_int_is_zero(bset2->eq[row][col]);
221 if (!is_zero1 && !is_zero2) {
222 set_common_multiple(bset1, bset2, row, col);
223 ++row;
224 } else if (!is_zero1 && is_zero2) {
225 construct_column(bset1, bset2, row, col);
226 } else if (is_zero1 && !is_zero2) {
227 construct_column(bset2, bset1, row, col);
228 } else {
229 if (transform_column(bset1, bset2, row, col))
230 --row;
233 isl_assert(bset1->ctx, row == bset1->n_eq, goto error);
234 isl_basic_set_free(bset2);
235 bset1 = isl_basic_set_normalize_constraints(bset1);
236 return bset1;
237 error:
238 isl_basic_set_free(bset1);
239 isl_basic_set_free(bset2);
240 return NULL;
243 /* Find an integer point in the set represented by "tab"
244 * that lies outside of the equality "eq" e(x) = 0.
245 * If "up" is true, look for a point satisfying e(x) - 1 >= 0.
246 * Otherwise, look for a point satisfying -e(x) - 1 >= 0 (i.e., e(x) <= -1).
247 * The point, if found, is returned.
248 * If no point can be found, a zero-length vector is returned.
250 * Before solving an ILP problem, we first check if simply
251 * adding the normal of the constraint to one of the known
252 * integer points in the basic set represented by "tab"
253 * yields another point inside the basic set.
255 * The caller of this function ensures that the tableau is bounded or
256 * that tab->basis and tab->n_unbounded have been set appropriately.
258 static struct isl_vec *outside_point(struct isl_tab *tab, isl_int *eq, int up)
260 struct isl_ctx *ctx;
261 struct isl_vec *sample = NULL;
262 struct isl_tab_undo *snap;
263 unsigned dim;
265 if (!tab)
266 return NULL;
267 ctx = tab->mat->ctx;
269 dim = tab->n_var;
270 sample = isl_vec_alloc(ctx, 1 + dim);
271 if (!sample)
272 return NULL;
273 isl_int_set_si(sample->el[0], 1);
274 isl_seq_combine(sample->el + 1,
275 ctx->one, tab->bmap->sample->el + 1,
276 up ? ctx->one : ctx->negone, eq + 1, dim);
277 if (isl_basic_map_contains(tab->bmap, sample))
278 return sample;
279 isl_vec_free(sample);
280 sample = NULL;
282 snap = isl_tab_snap(tab);
284 if (!up)
285 isl_seq_neg(eq, eq, 1 + dim);
286 isl_int_sub_ui(eq[0], eq[0], 1);
288 if (isl_tab_extend_cons(tab, 1) < 0)
289 goto error;
290 if (isl_tab_add_ineq(tab, eq) < 0)
291 goto error;
293 sample = isl_tab_sample(tab);
295 isl_int_add_ui(eq[0], eq[0], 1);
296 if (!up)
297 isl_seq_neg(eq, eq, 1 + dim);
299 if (sample && isl_tab_rollback(tab, snap) < 0)
300 goto error;
302 return sample;
303 error:
304 isl_vec_free(sample);
305 return NULL;
308 struct isl_basic_set *isl_basic_set_recession_cone(struct isl_basic_set *bset)
310 int i;
312 bset = isl_basic_set_cow(bset);
313 if (!bset)
314 return NULL;
315 isl_assert(bset->ctx, bset->n_div == 0, goto error);
317 for (i = 0; i < bset->n_eq; ++i)
318 isl_int_set_si(bset->eq[i][0], 0);
320 for (i = 0; i < bset->n_ineq; ++i)
321 isl_int_set_si(bset->ineq[i][0], 0);
323 ISL_F_CLR(bset, ISL_BASIC_SET_NO_IMPLICIT);
324 return isl_basic_set_implicit_equalities(bset);
325 error:
326 isl_basic_set_free(bset);
327 return NULL;
330 __isl_give isl_set *isl_set_recession_cone(__isl_take isl_set *set)
332 int i;
334 if (!set)
335 return NULL;
336 if (set->n == 0)
337 return set;
339 set = isl_set_remove_divs(set);
340 set = isl_set_cow(set);
341 if (!set)
342 return NULL;
344 for (i = 0; i < set->n; ++i) {
345 set->p[i] = isl_basic_set_recession_cone(set->p[i]);
346 if (!set->p[i])
347 goto error;
350 return set;
351 error:
352 isl_set_free(set);
353 return NULL;
356 /* Extend an initial (under-)approximation of the affine hull of basic
357 * set represented by the tableau "tab"
358 * by looking for points that do not satisfy one of the equalities
359 * in the current approximation and adding them to that approximation
360 * until no such points can be found any more.
362 * The caller of this function ensures that "tab" is bounded or
363 * that tab->basis and tab->n_unbounded have been set appropriately.
365 static struct isl_basic_set *extend_affine_hull(struct isl_tab *tab,
366 struct isl_basic_set *hull)
368 int i, j;
369 unsigned dim;
371 if (!tab || !hull)
372 goto error;
374 dim = tab->n_var;
376 if (isl_tab_extend_cons(tab, 2 * dim + 1) < 0)
377 goto error;
379 for (i = 0; i < dim; ++i) {
380 struct isl_vec *sample;
381 struct isl_basic_set *point;
382 for (j = 0; j < hull->n_eq; ++j) {
383 sample = outside_point(tab, hull->eq[j], 1);
384 if (!sample)
385 goto error;
386 if (sample->size > 0)
387 break;
388 isl_vec_free(sample);
389 sample = outside_point(tab, hull->eq[j], 0);
390 if (!sample)
391 goto error;
392 if (sample->size > 0)
393 break;
394 isl_vec_free(sample);
396 if (isl_tab_add_eq(tab, hull->eq[j]) < 0)
397 goto error;
399 if (j == hull->n_eq)
400 break;
401 if (tab->samples)
402 tab = isl_tab_add_sample(tab, isl_vec_copy(sample));
403 if (!tab)
404 goto error;
405 point = isl_basic_set_from_vec(sample);
406 hull = affine_hull(hull, point);
407 if (!hull)
408 return NULL;
411 return hull;
412 error:
413 isl_basic_set_free(hull);
414 return NULL;
417 /* Drop all constraints in bset that involve any of the dimensions
418 * first to first+n-1.
420 __isl_give isl_basic_set *isl_basic_set_drop_constraints_involving(
421 __isl_take isl_basic_set *bset, unsigned first, unsigned n)
423 int i;
425 if (n == 0)
426 return bset;
428 bset = isl_basic_set_cow(bset);
430 if (!bset)
431 return NULL;
433 for (i = bset->n_eq - 1; i >= 0; --i) {
434 if (isl_seq_first_non_zero(bset->eq[i] + 1 + first, n) == -1)
435 continue;
436 isl_basic_set_drop_equality(bset, i);
439 for (i = bset->n_ineq - 1; i >= 0; --i) {
440 if (isl_seq_first_non_zero(bset->ineq[i] + 1 + first, n) == -1)
441 continue;
442 isl_basic_set_drop_inequality(bset, i);
445 return bset;
448 /* Look for all equalities satisfied by the integer points in bset,
449 * which is assumed to be bounded.
451 * The equalities are obtained by successively looking for
452 * a point that is affinely independent of the points found so far.
453 * In particular, for each equality satisfied by the points so far,
454 * we check if there is any point on a hyperplane parallel to the
455 * corresponding hyperplane shifted by at least one (in either direction).
457 static struct isl_basic_set *uset_affine_hull_bounded(struct isl_basic_set *bset)
459 struct isl_vec *sample = NULL;
460 struct isl_basic_set *hull;
461 struct isl_tab *tab = NULL;
462 unsigned dim;
464 if (isl_basic_set_plain_is_empty(bset))
465 return bset;
467 dim = isl_basic_set_n_dim(bset);
469 if (bset->sample && bset->sample->size == 1 + dim) {
470 int contains = isl_basic_set_contains(bset, bset->sample);
471 if (contains < 0)
472 goto error;
473 if (contains) {
474 if (dim == 0)
475 return bset;
476 sample = isl_vec_copy(bset->sample);
477 } else {
478 isl_vec_free(bset->sample);
479 bset->sample = NULL;
483 tab = isl_tab_from_basic_set(bset);
484 if (!tab)
485 goto error;
486 if (tab->empty) {
487 isl_tab_free(tab);
488 isl_vec_free(sample);
489 return isl_basic_set_set_to_empty(bset);
491 if (isl_tab_track_bset(tab, isl_basic_set_copy(bset)) < 0)
492 goto error;
494 if (!sample) {
495 struct isl_tab_undo *snap;
496 snap = isl_tab_snap(tab);
497 sample = isl_tab_sample(tab);
498 if (isl_tab_rollback(tab, snap) < 0)
499 goto error;
500 isl_vec_free(tab->bmap->sample);
501 tab->bmap->sample = isl_vec_copy(sample);
504 if (!sample)
505 goto error;
506 if (sample->size == 0) {
507 isl_tab_free(tab);
508 isl_vec_free(sample);
509 return isl_basic_set_set_to_empty(bset);
512 hull = isl_basic_set_from_vec(sample);
514 isl_basic_set_free(bset);
515 hull = extend_affine_hull(tab, hull);
516 isl_tab_free(tab);
518 return hull;
519 error:
520 isl_vec_free(sample);
521 isl_tab_free(tab);
522 isl_basic_set_free(bset);
523 return NULL;
526 /* Given an unbounded tableau and an integer point satisfying the tableau,
527 * construct an initial affine hull containing the recession cone
528 * shifted to the given point.
530 * The unbounded directions are taken from the last rows of the basis,
531 * which is assumed to have been initialized appropriately.
533 static __isl_give isl_basic_set *initial_hull(struct isl_tab *tab,
534 __isl_take isl_vec *vec)
536 int i;
537 int k;
538 struct isl_basic_set *bset = NULL;
539 struct isl_ctx *ctx;
540 unsigned dim;
542 if (!vec || !tab)
543 return NULL;
544 ctx = vec->ctx;
545 isl_assert(ctx, vec->size != 0, goto error);
547 bset = isl_basic_set_alloc(ctx, 0, vec->size - 1, 0, vec->size - 1, 0);
548 if (!bset)
549 goto error;
550 dim = isl_basic_set_n_dim(bset) - tab->n_unbounded;
551 for (i = 0; i < dim; ++i) {
552 k = isl_basic_set_alloc_equality(bset);
553 if (k < 0)
554 goto error;
555 isl_seq_cpy(bset->eq[k] + 1, tab->basis->row[1 + i] + 1,
556 vec->size - 1);
557 isl_seq_inner_product(bset->eq[k] + 1, vec->el +1,
558 vec->size - 1, &bset->eq[k][0]);
559 isl_int_neg(bset->eq[k][0], bset->eq[k][0]);
561 bset->sample = vec;
562 bset = isl_basic_set_gauss(bset, NULL);
564 return bset;
565 error:
566 isl_basic_set_free(bset);
567 isl_vec_free(vec);
568 return NULL;
571 /* Given a tableau of a set and a tableau of the corresponding
572 * recession cone, detect and add all equalities to the tableau.
573 * If the tableau is bounded, then we can simply keep the
574 * tableau in its state after the return from extend_affine_hull.
575 * However, if the tableau is unbounded, then
576 * isl_tab_set_initial_basis_with_cone will add some additional
577 * constraints to the tableau that have to be removed again.
578 * In this case, we therefore rollback to the state before
579 * any constraints were added and then add the equalities back in.
581 struct isl_tab *isl_tab_detect_equalities(struct isl_tab *tab,
582 struct isl_tab *tab_cone)
584 int j;
585 struct isl_vec *sample;
586 struct isl_basic_set *hull;
587 struct isl_tab_undo *snap;
589 if (!tab || !tab_cone)
590 goto error;
592 snap = isl_tab_snap(tab);
594 isl_mat_free(tab->basis);
595 tab->basis = NULL;
597 isl_assert(tab->mat->ctx, tab->bmap, goto error);
598 isl_assert(tab->mat->ctx, tab->samples, goto error);
599 isl_assert(tab->mat->ctx, tab->samples->n_col == 1 + tab->n_var, goto error);
600 isl_assert(tab->mat->ctx, tab->n_sample > tab->n_outside, goto error);
602 if (isl_tab_set_initial_basis_with_cone(tab, tab_cone) < 0)
603 goto error;
605 sample = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_var);
606 if (!sample)
607 goto error;
609 isl_seq_cpy(sample->el, tab->samples->row[tab->n_outside], sample->size);
611 isl_vec_free(tab->bmap->sample);
612 tab->bmap->sample = isl_vec_copy(sample);
614 if (tab->n_unbounded == 0)
615 hull = isl_basic_set_from_vec(isl_vec_copy(sample));
616 else
617 hull = initial_hull(tab, isl_vec_copy(sample));
619 for (j = tab->n_outside + 1; j < tab->n_sample; ++j) {
620 isl_seq_cpy(sample->el, tab->samples->row[j], sample->size);
621 hull = affine_hull(hull,
622 isl_basic_set_from_vec(isl_vec_copy(sample)));
625 isl_vec_free(sample);
627 hull = extend_affine_hull(tab, hull);
628 if (!hull)
629 goto error;
631 if (tab->n_unbounded == 0) {
632 isl_basic_set_free(hull);
633 return tab;
636 if (isl_tab_rollback(tab, snap) < 0)
637 goto error;
639 if (hull->n_eq > tab->n_zero) {
640 for (j = 0; j < hull->n_eq; ++j) {
641 isl_seq_normalize(tab->mat->ctx, hull->eq[j], 1 + tab->n_var);
642 if (isl_tab_add_eq(tab, hull->eq[j]) < 0)
643 goto error;
647 isl_basic_set_free(hull);
649 return tab;
650 error:
651 isl_tab_free(tab);
652 return NULL;
655 /* Compute the affine hull of "bset", where "cone" is the recession cone
656 * of "bset".
658 * We first compute a unimodular transformation that puts the unbounded
659 * directions in the last dimensions. In particular, we take a transformation
660 * that maps all equalities to equalities (in HNF) on the first dimensions.
661 * Let x be the original dimensions and y the transformed, with y_1 bounded
662 * and y_2 unbounded.
664 * [ y_1 ] [ y_1 ] [ Q_1 ]
665 * x = U [ y_2 ] [ y_2 ] = [ Q_2 ] x
667 * Let's call the input basic set S. We compute S' = preimage(S, U)
668 * and drop the final dimensions including any constraints involving them.
669 * This results in set S''.
670 * Then we compute the affine hull A'' of S''.
671 * Let F y_1 >= g be the constraint system of A''. In the transformed
672 * space the y_2 are unbounded, so we can add them back without any constraints,
673 * resulting in
675 * [ y_1 ]
676 * [ F 0 ] [ y_2 ] >= g
677 * or
678 * [ Q_1 ]
679 * [ F 0 ] [ Q_2 ] x >= g
680 * or
681 * F Q_1 x >= g
683 * The affine hull in the original space is then obtained as
684 * A = preimage(A'', Q_1).
686 static struct isl_basic_set *affine_hull_with_cone(struct isl_basic_set *bset,
687 struct isl_basic_set *cone)
689 unsigned total;
690 unsigned cone_dim;
691 struct isl_basic_set *hull;
692 struct isl_mat *M, *U, *Q;
694 if (!bset || !cone)
695 goto error;
697 total = isl_basic_set_total_dim(cone);
698 cone_dim = total - cone->n_eq;
700 M = isl_mat_sub_alloc6(bset->ctx, cone->eq, 0, cone->n_eq, 1, total);
701 M = isl_mat_left_hermite(M, 0, &U, &Q);
702 if (!M)
703 goto error;
704 isl_mat_free(M);
706 U = isl_mat_lin_to_aff(U);
707 bset = isl_basic_set_preimage(bset, isl_mat_copy(U));
709 bset = isl_basic_set_drop_constraints_involving(bset, total - cone_dim,
710 cone_dim);
711 bset = isl_basic_set_drop_dims(bset, total - cone_dim, cone_dim);
713 Q = isl_mat_lin_to_aff(Q);
714 Q = isl_mat_drop_rows(Q, 1 + total - cone_dim, cone_dim);
716 if (bset && bset->sample && bset->sample->size == 1 + total)
717 bset->sample = isl_mat_vec_product(isl_mat_copy(Q), bset->sample);
719 hull = uset_affine_hull_bounded(bset);
721 if (!hull)
722 isl_mat_free(U);
723 else {
724 struct isl_vec *sample = isl_vec_copy(hull->sample);
725 U = isl_mat_drop_cols(U, 1 + total - cone_dim, cone_dim);
726 if (sample && sample->size > 0)
727 sample = isl_mat_vec_product(U, sample);
728 else
729 isl_mat_free(U);
730 hull = isl_basic_set_preimage(hull, Q);
731 if (hull) {
732 isl_vec_free(hull->sample);
733 hull->sample = sample;
734 } else
735 isl_vec_free(sample);
738 isl_basic_set_free(cone);
740 return hull;
741 error:
742 isl_basic_set_free(bset);
743 isl_basic_set_free(cone);
744 return NULL;
747 /* Look for all equalities satisfied by the integer points in bset,
748 * which is assumed not to have any explicit equalities.
750 * The equalities are obtained by successively looking for
751 * a point that is affinely independent of the points found so far.
752 * In particular, for each equality satisfied by the points so far,
753 * we check if there is any point on a hyperplane parallel to the
754 * corresponding hyperplane shifted by at least one (in either direction).
756 * Before looking for any outside points, we first compute the recession
757 * cone. The directions of this recession cone will always be part
758 * of the affine hull, so there is no need for looking for any points
759 * in these directions.
760 * In particular, if the recession cone is full-dimensional, then
761 * the affine hull is simply the whole universe.
763 static struct isl_basic_set *uset_affine_hull(struct isl_basic_set *bset)
765 struct isl_basic_set *cone;
767 if (isl_basic_set_plain_is_empty(bset))
768 return bset;
770 cone = isl_basic_set_recession_cone(isl_basic_set_copy(bset));
771 if (!cone)
772 goto error;
773 if (cone->n_eq == 0) {
774 struct isl_basic_set *hull;
775 isl_basic_set_free(cone);
776 hull = isl_basic_set_universe_like(bset);
777 isl_basic_set_free(bset);
778 return hull;
781 if (cone->n_eq < isl_basic_set_total_dim(cone))
782 return affine_hull_with_cone(bset, cone);
784 isl_basic_set_free(cone);
785 return uset_affine_hull_bounded(bset);
786 error:
787 isl_basic_set_free(bset);
788 return NULL;
791 /* Look for all equalities satisfied by the integer points in bmap
792 * that are independent of the equalities already explicitly available
793 * in bmap.
795 * We first remove all equalities already explicitly available,
796 * then look for additional equalities in the reduced space
797 * and then transform the result to the original space.
798 * The original equalities are _not_ added to this set. This is
799 * the responsibility of the calling function.
800 * The resulting basic set has all meaning about the dimensions removed.
801 * In particular, dimensions that correspond to existential variables
802 * in bmap and that are found to be fixed are not removed.
804 static struct isl_basic_set *equalities_in_underlying_set(
805 struct isl_basic_map *bmap)
807 struct isl_mat *T1 = NULL;
808 struct isl_mat *T2 = NULL;
809 struct isl_basic_set *bset = NULL;
810 struct isl_basic_set *hull = NULL;
812 bset = isl_basic_map_underlying_set(bmap);
813 if (!bset)
814 return NULL;
815 if (bset->n_eq)
816 bset = isl_basic_set_remove_equalities(bset, &T1, &T2);
817 if (!bset)
818 goto error;
820 hull = uset_affine_hull(bset);
821 if (!T2)
822 return hull;
824 if (!hull) {
825 isl_mat_free(T1);
826 isl_mat_free(T2);
827 } else {
828 struct isl_vec *sample = isl_vec_copy(hull->sample);
829 if (sample && sample->size > 0)
830 sample = isl_mat_vec_product(T1, sample);
831 else
832 isl_mat_free(T1);
833 hull = isl_basic_set_preimage(hull, T2);
834 if (hull) {
835 isl_vec_free(hull->sample);
836 hull->sample = sample;
837 } else
838 isl_vec_free(sample);
841 return hull;
842 error:
843 isl_mat_free(T2);
844 isl_basic_set_free(bset);
845 isl_basic_set_free(hull);
846 return NULL;
849 /* Detect and make explicit all equalities satisfied by the (integer)
850 * points in bmap.
852 struct isl_basic_map *isl_basic_map_detect_equalities(
853 struct isl_basic_map *bmap)
855 int i, j;
856 struct isl_basic_set *hull = NULL;
858 if (!bmap)
859 return NULL;
860 if (bmap->n_ineq == 0)
861 return bmap;
862 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
863 return bmap;
864 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_ALL_EQUALITIES))
865 return bmap;
866 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
867 return isl_basic_map_implicit_equalities(bmap);
869 hull = equalities_in_underlying_set(isl_basic_map_copy(bmap));
870 if (!hull)
871 goto error;
872 if (ISL_F_ISSET(hull, ISL_BASIC_SET_EMPTY)) {
873 isl_basic_set_free(hull);
874 return isl_basic_map_set_to_empty(bmap);
876 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim), 0,
877 hull->n_eq, 0);
878 for (i = 0; i < hull->n_eq; ++i) {
879 j = isl_basic_map_alloc_equality(bmap);
880 if (j < 0)
881 goto error;
882 isl_seq_cpy(bmap->eq[j], hull->eq[i],
883 1 + isl_basic_set_total_dim(hull));
885 isl_vec_free(bmap->sample);
886 bmap->sample = isl_vec_copy(hull->sample);
887 isl_basic_set_free(hull);
888 ISL_F_SET(bmap, ISL_BASIC_MAP_NO_IMPLICIT | ISL_BASIC_MAP_ALL_EQUALITIES);
889 bmap = isl_basic_map_simplify(bmap);
890 return isl_basic_map_finalize(bmap);
891 error:
892 isl_basic_set_free(hull);
893 isl_basic_map_free(bmap);
894 return NULL;
897 __isl_give isl_basic_set *isl_basic_set_detect_equalities(
898 __isl_take isl_basic_set *bset)
900 return (isl_basic_set *)
901 isl_basic_map_detect_equalities((isl_basic_map *)bset);
904 __isl_give isl_map *isl_map_inline_foreach_basic_map(__isl_take isl_map *map,
905 __isl_give isl_basic_map *(*fn)(__isl_take isl_basic_map *bmap))
907 struct isl_basic_map *bmap;
908 int i;
910 if (!map)
911 return NULL;
913 for (i = 0; i < map->n; ++i) {
914 bmap = isl_basic_map_copy(map->p[i]);
915 bmap = fn(bmap);
916 if (!bmap)
917 goto error;
918 isl_basic_map_free(map->p[i]);
919 map->p[i] = bmap;
922 return map;
923 error:
924 isl_map_free(map);
925 return NULL;
928 __isl_give isl_map *isl_map_detect_equalities(__isl_take isl_map *map)
930 return isl_map_inline_foreach_basic_map(map,
931 &isl_basic_map_detect_equalities);
934 __isl_give isl_set *isl_set_detect_equalities(__isl_take isl_set *set)
936 return (isl_set *)isl_map_detect_equalities((isl_map *)set);
939 /* After computing the rational affine hull (by detecting the implicit
940 * equalities), we compute the additional equalities satisfied by
941 * the integer points (if any) and add the original equalities back in.
943 struct isl_basic_map *isl_basic_map_affine_hull(struct isl_basic_map *bmap)
945 bmap = isl_basic_map_detect_equalities(bmap);
946 bmap = isl_basic_map_cow(bmap);
947 if (bmap)
948 isl_basic_map_free_inequality(bmap, bmap->n_ineq);
949 bmap = isl_basic_map_finalize(bmap);
950 return bmap;
953 struct isl_basic_set *isl_basic_set_affine_hull(struct isl_basic_set *bset)
955 return (struct isl_basic_set *)
956 isl_basic_map_affine_hull((struct isl_basic_map *)bset);
959 struct isl_basic_map *isl_map_affine_hull(struct isl_map *map)
961 int i;
962 struct isl_basic_map *model = NULL;
963 struct isl_basic_map *hull = NULL;
964 struct isl_set *set;
966 map = isl_map_detect_equalities(map);
967 map = isl_map_align_divs(map);
969 if (!map)
970 return NULL;
972 if (map->n == 0) {
973 hull = isl_basic_map_empty_like_map(map);
974 isl_map_free(map);
975 return hull;
978 model = isl_basic_map_copy(map->p[0]);
979 set = isl_map_underlying_set(map);
980 set = isl_set_cow(set);
981 if (!set)
982 goto error;
984 for (i = 0; i < set->n; ++i) {
985 set->p[i] = isl_basic_set_cow(set->p[i]);
986 set->p[i] = isl_basic_set_affine_hull(set->p[i]);
987 set->p[i] = isl_basic_set_gauss(set->p[i], NULL);
988 if (!set->p[i])
989 goto error;
991 set = isl_set_remove_empty_parts(set);
992 if (set->n == 0) {
993 hull = isl_basic_map_empty_like(model);
994 isl_basic_map_free(model);
995 } else {
996 struct isl_basic_set *bset;
997 while (set->n > 1) {
998 set->p[0] = affine_hull(set->p[0], set->p[--set->n]);
999 if (!set->p[0])
1000 goto error;
1002 bset = isl_basic_set_copy(set->p[0]);
1003 hull = isl_basic_map_overlying_set(bset, model);
1005 isl_set_free(set);
1006 hull = isl_basic_map_simplify(hull);
1007 return isl_basic_map_finalize(hull);
1008 error:
1009 isl_basic_map_free(model);
1010 isl_set_free(set);
1011 return NULL;
1014 struct isl_basic_set *isl_set_affine_hull(struct isl_set *set)
1016 return (struct isl_basic_set *)
1017 isl_map_affine_hull((struct isl_map *)set);