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
15 #include "isl_map_private.h"
16 #include "isl_equalities.h"
17 #include "isl_sample.h"
20 struct isl_basic_map *isl_basic_map_implicit_equalities(
21 struct isl_basic_map *bmap)
28 bmap = isl_basic_map_gauss(bmap, NULL);
29 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
31 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NO_IMPLICIT))
33 if (bmap->n_ineq <= 1)
36 tab = isl_tab_from_basic_map(bmap);
37 if (isl_tab_detect_implicit_equalities(tab) < 0)
39 bmap = isl_basic_map_update_from_tab(bmap, tab);
41 bmap = isl_basic_map_gauss(bmap, NULL);
42 ISL_F_SET(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
46 isl_basic_map_free(bmap);
50 struct isl_basic_set *isl_basic_set_implicit_equalities(
51 struct isl_basic_set *bset)
53 return (struct isl_basic_set *)
54 isl_basic_map_implicit_equalities((struct isl_basic_map*)bset);
57 struct isl_map *isl_map_implicit_equalities(struct isl_map *map)
64 for (i = 0; i < map->n; ++i) {
65 map->p[i] = isl_basic_map_implicit_equalities(map->p[i]);
76 /* Make eq[row][col] of both bmaps equal so we can add the row
77 * add the column to the common matrix.
78 * Note that because of the echelon form, the columns of row row
79 * after column col are zero.
81 static void set_common_multiple(
82 struct isl_basic_set *bset1, struct isl_basic_set *bset2,
83 unsigned row, unsigned col)
87 if (isl_int_eq(bset1->eq[row][col], bset2->eq[row][col]))
92 isl_int_lcm(m, bset1->eq[row][col], bset2->eq[row][col]);
93 isl_int_divexact(c, m, bset1->eq[row][col]);
94 isl_seq_scale(bset1->eq[row], bset1->eq[row], c, col+1);
95 isl_int_divexact(c, m, bset2->eq[row][col]);
96 isl_seq_scale(bset2->eq[row], bset2->eq[row], c, col+1);
101 /* Delete a given equality, moving all the following equalities one up.
103 static void delete_row(struct isl_basic_set *bset, unsigned row)
110 for (r = row; r < bset->n_eq; ++r)
111 bset->eq[r] = bset->eq[r+1];
112 bset->eq[bset->n_eq] = t;
115 /* Make first row entries in column col of bset1 identical to
116 * those of bset2, using the fact that entry bset1->eq[row][col]=a
117 * is non-zero. Initially, these elements of bset1 are all zero.
118 * For each row i < row, we set
119 * A[i] = a * A[i] + B[i][col] * A[row]
122 * A[i][col] = B[i][col] = a * old(B[i][col])
124 static void construct_column(
125 struct isl_basic_set *bset1, struct isl_basic_set *bset2,
126 unsigned row, unsigned col)
135 total = 1 + isl_basic_set_n_dim(bset1);
136 for (r = 0; r < row; ++r) {
137 if (isl_int_is_zero(bset2->eq[r][col]))
139 isl_int_gcd(b, bset2->eq[r][col], bset1->eq[row][col]);
140 isl_int_divexact(a, bset1->eq[row][col], b);
141 isl_int_divexact(b, bset2->eq[r][col], b);
142 isl_seq_combine(bset1->eq[r], a, bset1->eq[r],
143 b, bset1->eq[row], total);
144 isl_seq_scale(bset2->eq[r], bset2->eq[r], a, total);
148 delete_row(bset1, row);
151 /* Make first row entries in column col of bset1 identical to
152 * those of bset2, using only these entries of the two matrices.
153 * Let t be the last row with different entries.
154 * For each row i < t, we set
155 * A[i] = (A[t][col]-B[t][col]) * A[i] + (B[i][col]-A[i][col) * A[t]
156 * B[i] = (A[t][col]-B[t][col]) * B[i] + (B[i][col]-A[i][col) * B[t]
158 * A[i][col] = B[i][col] = old(A[t][col]*B[i][col]-A[i][col]*B[t][col])
160 static int transform_column(
161 struct isl_basic_set *bset1, struct isl_basic_set *bset2,
162 unsigned row, unsigned col)
168 for (t = row-1; t >= 0; --t)
169 if (isl_int_ne(bset1->eq[t][col], bset2->eq[t][col]))
174 total = 1 + isl_basic_set_n_dim(bset1);
178 isl_int_sub(b, bset1->eq[t][col], bset2->eq[t][col]);
179 for (i = 0; i < t; ++i) {
180 isl_int_sub(a, bset2->eq[i][col], bset1->eq[i][col]);
181 isl_int_gcd(g, a, b);
182 isl_int_divexact(a, a, g);
183 isl_int_divexact(g, b, g);
184 isl_seq_combine(bset1->eq[i], g, bset1->eq[i], a, bset1->eq[t],
186 isl_seq_combine(bset2->eq[i], g, bset2->eq[i], a, bset2->eq[t],
192 delete_row(bset1, t);
193 delete_row(bset2, t);
197 /* The implementation is based on Section 5.2 of Michael Karr,
198 * "Affine Relationships Among Variables of a Program",
199 * except that the echelon form we use starts from the last column
200 * and that we are dealing with integer coefficients.
202 static struct isl_basic_set *affine_hull(
203 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
209 if (!bset1 || !bset2)
212 total = 1 + isl_basic_set_n_dim(bset1);
215 for (col = total-1; col >= 0; --col) {
216 int is_zero1 = row >= bset1->n_eq ||
217 isl_int_is_zero(bset1->eq[row][col]);
218 int is_zero2 = row >= bset2->n_eq ||
219 isl_int_is_zero(bset2->eq[row][col]);
220 if (!is_zero1 && !is_zero2) {
221 set_common_multiple(bset1, bset2, row, col);
223 } else if (!is_zero1 && is_zero2) {
224 construct_column(bset1, bset2, row, col);
225 } else if (is_zero1 && !is_zero2) {
226 construct_column(bset2, bset1, row, col);
228 if (transform_column(bset1, bset2, row, col))
232 isl_assert(bset1->ctx, row == bset1->n_eq, goto error);
233 isl_basic_set_free(bset2);
234 bset1 = isl_basic_set_normalize_constraints(bset1);
237 isl_basic_set_free(bset1);
238 isl_basic_set_free(bset2);
242 /* Find an integer point in the set represented by "tab"
243 * that lies outside of the equality "eq" e(x) = 0.
244 * If "up" is true, look for a point satisfying e(x) - 1 >= 0.
245 * Otherwise, look for a point satisfying -e(x) - 1 >= 0 (i.e., e(x) <= -1).
246 * The point, if found, is returned.
247 * If no point can be found, a zero-length vector is returned.
249 * Before solving an ILP problem, we first check if simply
250 * adding the normal of the constraint to one of the known
251 * integer points in the basic set represented by "tab"
252 * yields another point inside the basic set.
254 * The caller of this function ensures that the tableau is bounded or
255 * that tab->basis and tab->n_unbounded have been set appropriately.
257 static struct isl_vec *outside_point(struct isl_tab *tab, isl_int *eq, int up)
260 struct isl_vec *sample = NULL;
261 struct isl_tab_undo *snap;
269 sample = isl_vec_alloc(ctx, 1 + dim);
272 isl_int_set_si(sample->el[0], 1);
273 isl_seq_combine(sample->el + 1,
274 ctx->one, tab->bmap->sample->el + 1,
275 up ? ctx->one : ctx->negone, eq + 1, dim);
276 if (isl_basic_map_contains(tab->bmap, sample))
278 isl_vec_free(sample);
281 snap = isl_tab_snap(tab);
284 isl_seq_neg(eq, eq, 1 + dim);
285 isl_int_sub_ui(eq[0], eq[0], 1);
287 if (isl_tab_extend_cons(tab, 1) < 0)
289 if (isl_tab_add_ineq(tab, eq) < 0)
292 sample = isl_tab_sample(tab);
294 isl_int_add_ui(eq[0], eq[0], 1);
296 isl_seq_neg(eq, eq, 1 + dim);
298 if (isl_tab_rollback(tab, snap) < 0)
303 isl_vec_free(sample);
307 struct isl_basic_set *isl_basic_set_recession_cone(struct isl_basic_set *bset)
311 bset = isl_basic_set_cow(bset);
314 isl_assert(bset->ctx, bset->n_div == 0, goto error);
316 for (i = 0; i < bset->n_eq; ++i)
317 isl_int_set_si(bset->eq[i][0], 0);
319 for (i = 0; i < bset->n_ineq; ++i)
320 isl_int_set_si(bset->ineq[i][0], 0);
322 ISL_F_CLR(bset, ISL_BASIC_SET_NO_IMPLICIT);
323 return isl_basic_set_implicit_equalities(bset);
325 isl_basic_set_free(bset);
329 __isl_give isl_set *isl_set_recession_cone(__isl_take isl_set *set)
338 set = isl_set_remove_divs(set);
339 set = isl_set_cow(set);
343 for (i = 0; i < set->n; ++i) {
344 set->p[i] = isl_basic_set_recession_cone(set->p[i]);
355 /* Extend an initial (under-)approximation of the affine hull of basic
356 * set represented by the tableau "tab"
357 * by looking for points that do not satisfy one of the equalities
358 * in the current approximation and adding them to that approximation
359 * until no such points can be found any more.
361 * The caller of this function ensures that "tab" is bounded or
362 * that tab->basis and tab->n_unbounded have been set appropriately.
364 static struct isl_basic_set *extend_affine_hull(struct isl_tab *tab,
365 struct isl_basic_set *hull)
375 if (isl_tab_extend_cons(tab, 2 * dim + 1) < 0)
378 for (i = 0; i < dim; ++i) {
379 struct isl_vec *sample;
380 struct isl_basic_set *point;
381 for (j = 0; j < hull->n_eq; ++j) {
382 sample = outside_point(tab, hull->eq[j], 1);
385 if (sample->size > 0)
387 isl_vec_free(sample);
388 sample = outside_point(tab, hull->eq[j], 0);
391 if (sample->size > 0)
393 isl_vec_free(sample);
395 if (isl_tab_add_eq(tab, hull->eq[j]) < 0)
401 tab = isl_tab_add_sample(tab, isl_vec_copy(sample));
404 point = isl_basic_set_from_vec(sample);
405 hull = affine_hull(hull, point);
410 isl_basic_set_free(hull);
414 /* Drop all constraints in bset that involve any of the dimensions
415 * first to first+n-1.
417 static struct isl_basic_set *drop_constraints_involving
418 (struct isl_basic_set *bset, unsigned first, unsigned n)
425 bset = isl_basic_set_cow(bset);
427 for (i = bset->n_eq - 1; i >= 0; --i) {
428 if (isl_seq_first_non_zero(bset->eq[i] + 1 + first, n) == -1)
430 isl_basic_set_drop_equality(bset, i);
433 for (i = bset->n_ineq - 1; i >= 0; --i) {
434 if (isl_seq_first_non_zero(bset->ineq[i] + 1 + first, n) == -1)
436 isl_basic_set_drop_inequality(bset, i);
442 /* Look for all equalities satisfied by the integer points in bset,
443 * which is assumed to be bounded.
445 * The equalities are obtained by successively looking for
446 * a point that is affinely independent of the points found so far.
447 * In particular, for each equality satisfied by the points so far,
448 * we check if there is any point on a hyperplane parallel to the
449 * corresponding hyperplane shifted by at least one (in either direction).
451 static struct isl_basic_set *uset_affine_hull_bounded(struct isl_basic_set *bset)
453 struct isl_vec *sample = NULL;
454 struct isl_basic_set *hull;
455 struct isl_tab *tab = NULL;
458 if (isl_basic_set_fast_is_empty(bset))
461 dim = isl_basic_set_n_dim(bset);
463 if (bset->sample && bset->sample->size == 1 + dim) {
464 int contains = isl_basic_set_contains(bset, bset->sample);
470 sample = isl_vec_copy(bset->sample);
472 isl_vec_free(bset->sample);
477 tab = isl_tab_from_basic_set(bset);
482 isl_vec_free(sample);
483 return isl_basic_set_set_to_empty(bset);
485 if (isl_tab_track_bset(tab, isl_basic_set_copy(bset)) < 0)
489 struct isl_tab_undo *snap;
490 snap = isl_tab_snap(tab);
491 sample = isl_tab_sample(tab);
492 if (isl_tab_rollback(tab, snap) < 0)
494 isl_vec_free(tab->bmap->sample);
495 tab->bmap->sample = isl_vec_copy(sample);
500 if (sample->size == 0) {
502 isl_vec_free(sample);
503 return isl_basic_set_set_to_empty(bset);
506 hull = isl_basic_set_from_vec(sample);
508 isl_basic_set_free(bset);
509 hull = extend_affine_hull(tab, hull);
514 isl_vec_free(sample);
516 isl_basic_set_free(bset);
520 /* Given an unbounded tableau and an integer point satisfying the tableau,
521 * construct an intial affine hull containing the recession cone
522 * shifted to the given point.
524 * The unbounded directions are taken from the last rows of the basis,
525 * which is assumed to have been initialized appropriately.
527 static __isl_give isl_basic_set *initial_hull(struct isl_tab *tab,
528 __isl_take isl_vec *vec)
532 struct isl_basic_set *bset = NULL;
539 isl_assert(ctx, vec->size != 0, goto error);
541 bset = isl_basic_set_alloc(ctx, 0, vec->size - 1, 0, vec->size - 1, 0);
544 dim = isl_basic_set_n_dim(bset) - tab->n_unbounded;
545 for (i = 0; i < dim; ++i) {
546 k = isl_basic_set_alloc_equality(bset);
549 isl_seq_cpy(bset->eq[k] + 1, tab->basis->row[1 + i] + 1,
551 isl_seq_inner_product(bset->eq[k] + 1, vec->el +1,
552 vec->size - 1, &bset->eq[k][0]);
553 isl_int_neg(bset->eq[k][0], bset->eq[k][0]);
556 bset = isl_basic_set_gauss(bset, NULL);
560 isl_basic_set_free(bset);
565 /* Given a tableau of a set and a tableau of the corresponding
566 * recession cone, detect and add all equalities to the tableau.
567 * If the tableau is bounded, then we can simply keep the
568 * tableau in its state after the return from extend_affine_hull.
569 * However, if the tableau is unbounded, then
570 * isl_tab_set_initial_basis_with_cone will add some additional
571 * constraints to the tableau that have to be removed again.
572 * In this case, we therefore rollback to the state before
573 * any constraints were added and then add the eqaulities back in.
575 struct isl_tab *isl_tab_detect_equalities(struct isl_tab *tab,
576 struct isl_tab *tab_cone)
579 struct isl_vec *sample;
580 struct isl_basic_set *hull;
581 struct isl_tab_undo *snap;
583 if (!tab || !tab_cone)
586 snap = isl_tab_snap(tab);
588 isl_mat_free(tab->basis);
591 isl_assert(tab->mat->ctx, tab->bmap, goto error);
592 isl_assert(tab->mat->ctx, tab->samples, goto error);
593 isl_assert(tab->mat->ctx, tab->samples->n_col == 1 + tab->n_var, goto error);
594 isl_assert(tab->mat->ctx, tab->n_sample > tab->n_outside, goto error);
596 if (isl_tab_set_initial_basis_with_cone(tab, tab_cone) < 0)
599 sample = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_var);
603 isl_seq_cpy(sample->el, tab->samples->row[tab->n_outside], sample->size);
605 isl_vec_free(tab->bmap->sample);
606 tab->bmap->sample = isl_vec_copy(sample);
608 if (tab->n_unbounded == 0)
609 hull = isl_basic_set_from_vec(isl_vec_copy(sample));
611 hull = initial_hull(tab, isl_vec_copy(sample));
613 for (j = tab->n_outside + 1; j < tab->n_sample; ++j) {
614 isl_seq_cpy(sample->el, tab->samples->row[j], sample->size);
615 hull = affine_hull(hull,
616 isl_basic_set_from_vec(isl_vec_copy(sample)));
619 isl_vec_free(sample);
621 hull = extend_affine_hull(tab, hull);
625 if (tab->n_unbounded == 0) {
626 isl_basic_set_free(hull);
630 if (isl_tab_rollback(tab, snap) < 0)
633 if (hull->n_eq > tab->n_zero) {
634 for (j = 0; j < hull->n_eq; ++j) {
635 isl_seq_normalize(tab->mat->ctx, hull->eq[j], 1 + tab->n_var);
636 if (isl_tab_add_eq(tab, hull->eq[j]) < 0)
641 isl_basic_set_free(hull);
649 /* Compute the affine hull of "bset", where "cone" is the recession cone
652 * We first compute a unimodular transformation that puts the unbounded
653 * directions in the last dimensions. In particular, we take a transformation
654 * that maps all equalities to equalities (in HNF) on the first dimensions.
655 * Let x be the original dimensions and y the transformed, with y_1 bounded
658 * [ y_1 ] [ y_1 ] [ Q_1 ]
659 * x = U [ y_2 ] [ y_2 ] = [ Q_2 ] x
661 * Let's call the input basic set S. We compute S' = preimage(S, U)
662 * and drop the final dimensions including any constraints involving them.
663 * This results in set S''.
664 * Then we compute the affine hull A'' of S''.
665 * Let F y_1 >= g be the constraint system of A''. In the transformed
666 * space the y_2 are unbounded, so we can add them back without any constraints,
670 * [ F 0 ] [ y_2 ] >= g
673 * [ F 0 ] [ Q_2 ] x >= g
677 * The affine hull in the original space is then obtained as
678 * A = preimage(A'', Q_1).
680 static struct isl_basic_set *affine_hull_with_cone(struct isl_basic_set *bset,
681 struct isl_basic_set *cone)
685 struct isl_basic_set *hull;
686 struct isl_mat *M, *U, *Q;
691 total = isl_basic_set_total_dim(cone);
692 cone_dim = total - cone->n_eq;
694 M = isl_mat_sub_alloc(bset->ctx, cone->eq, 0, cone->n_eq, 1, total);
695 M = isl_mat_left_hermite(M, 0, &U, &Q);
700 U = isl_mat_lin_to_aff(U);
701 bset = isl_basic_set_preimage(bset, isl_mat_copy(U));
703 bset = drop_constraints_involving(bset, total - cone_dim, cone_dim);
704 bset = isl_basic_set_drop_dims(bset, total - cone_dim, cone_dim);
706 Q = isl_mat_lin_to_aff(Q);
707 Q = isl_mat_drop_rows(Q, 1 + total - cone_dim, cone_dim);
709 if (bset && bset->sample && bset->sample->size == 1 + total)
710 bset->sample = isl_mat_vec_product(isl_mat_copy(Q), bset->sample);
712 hull = uset_affine_hull_bounded(bset);
717 struct isl_vec *sample = isl_vec_copy(hull->sample);
718 U = isl_mat_drop_cols(U, 1 + total - cone_dim, cone_dim);
719 if (sample && sample->size > 0)
720 sample = isl_mat_vec_product(U, sample);
723 hull = isl_basic_set_preimage(hull, Q);
724 isl_vec_free(hull->sample);
725 hull->sample = sample;
728 isl_basic_set_free(cone);
732 isl_basic_set_free(bset);
733 isl_basic_set_free(cone);
737 /* Look for all equalities satisfied by the integer points in bset,
738 * which is assumed not to have any explicit equalities.
740 * The equalities are obtained by successively looking for
741 * a point that is affinely independent of the points found so far.
742 * In particular, for each equality satisfied by the points so far,
743 * we check if there is any point on a hyperplane parallel to the
744 * corresponding hyperplane shifted by at least one (in either direction).
746 * Before looking for any outside points, we first compute the recession
747 * cone. The directions of this recession cone will always be part
748 * of the affine hull, so there is no need for looking for any points
749 * in these directions.
750 * In particular, if the recession cone is full-dimensional, then
751 * the affine hull is simply the whole universe.
753 static struct isl_basic_set *uset_affine_hull(struct isl_basic_set *bset)
755 struct isl_basic_set *cone;
757 if (isl_basic_set_fast_is_empty(bset))
760 cone = isl_basic_set_recession_cone(isl_basic_set_copy(bset));
763 if (cone->n_eq == 0) {
764 struct isl_basic_set *hull;
765 isl_basic_set_free(cone);
766 hull = isl_basic_set_universe_like(bset);
767 isl_basic_set_free(bset);
771 if (cone->n_eq < isl_basic_set_total_dim(cone))
772 return affine_hull_with_cone(bset, cone);
774 isl_basic_set_free(cone);
775 return uset_affine_hull_bounded(bset);
777 isl_basic_set_free(bset);
781 /* Look for all equalities satisfied by the integer points in bmap
782 * that are independent of the equalities already explicitly available
785 * We first remove all equalities already explicitly available,
786 * then look for additional equalities in the reduced space
787 * and then transform the result to the original space.
788 * The original equalities are _not_ added to this set. This is
789 * the responsibility of the calling function.
790 * The resulting basic set has all meaning about the dimensions removed.
791 * In particular, dimensions that correspond to existential variables
792 * in bmap and that are found to be fixed are not removed.
794 static struct isl_basic_set *equalities_in_underlying_set(
795 struct isl_basic_map *bmap)
797 struct isl_mat *T1 = NULL;
798 struct isl_mat *T2 = NULL;
799 struct isl_basic_set *bset = NULL;
800 struct isl_basic_set *hull = NULL;
802 bset = isl_basic_map_underlying_set(bmap);
806 bset = isl_basic_set_remove_equalities(bset, &T1, &T2);
810 hull = uset_affine_hull(bset);
818 struct isl_vec *sample = isl_vec_copy(hull->sample);
819 if (sample && sample->size > 0)
820 sample = isl_mat_vec_product(T1, sample);
823 hull = isl_basic_set_preimage(hull, T2);
825 isl_vec_free(hull->sample);
826 hull->sample = sample;
828 isl_vec_free(sample);
834 isl_basic_set_free(bset);
835 isl_basic_set_free(hull);
839 /* Detect and make explicit all equalities satisfied by the (integer)
842 struct isl_basic_map *isl_basic_map_detect_equalities(
843 struct isl_basic_map *bmap)
846 struct isl_basic_set *hull = NULL;
850 if (bmap->n_ineq == 0)
852 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
854 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_ALL_EQUALITIES))
856 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
857 return isl_basic_map_implicit_equalities(bmap);
859 hull = equalities_in_underlying_set(isl_basic_map_copy(bmap));
862 if (ISL_F_ISSET(hull, ISL_BASIC_SET_EMPTY)) {
863 isl_basic_set_free(hull);
864 return isl_basic_map_set_to_empty(bmap);
866 bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim), 0,
868 for (i = 0; i < hull->n_eq; ++i) {
869 j = isl_basic_map_alloc_equality(bmap);
872 isl_seq_cpy(bmap->eq[j], hull->eq[i],
873 1 + isl_basic_set_total_dim(hull));
875 isl_vec_free(bmap->sample);
876 bmap->sample = isl_vec_copy(hull->sample);
877 isl_basic_set_free(hull);
878 ISL_F_SET(bmap, ISL_BASIC_MAP_NO_IMPLICIT | ISL_BASIC_MAP_ALL_EQUALITIES);
879 bmap = isl_basic_map_simplify(bmap);
880 return isl_basic_map_finalize(bmap);
882 isl_basic_set_free(hull);
883 isl_basic_map_free(bmap);
887 __isl_give isl_basic_set *isl_basic_set_detect_equalities(
888 __isl_take isl_basic_set *bset)
890 return (isl_basic_set *)
891 isl_basic_map_detect_equalities((isl_basic_map *)bset);
894 struct isl_map *isl_map_detect_equalities(struct isl_map *map)
896 struct isl_basic_map *bmap;
902 for (i = 0; i < map->n; ++i) {
903 bmap = isl_basic_map_copy(map->p[i]);
904 bmap = isl_basic_map_detect_equalities(bmap);
907 isl_basic_map_free(map->p[i]);
917 __isl_give isl_set *isl_set_detect_equalities(__isl_take isl_set *set)
919 return (isl_set *)isl_map_detect_equalities((isl_map *)set);
922 /* After computing the rational affine hull (by detecting the implicit
923 * equalities), we compute the additional equalities satisfied by
924 * the integer points (if any) and add the original equalities back in.
926 struct isl_basic_map *isl_basic_map_affine_hull(struct isl_basic_map *bmap)
928 bmap = isl_basic_map_detect_equalities(bmap);
929 bmap = isl_basic_map_cow(bmap);
931 isl_basic_map_free_inequality(bmap, bmap->n_ineq);
935 struct isl_basic_set *isl_basic_set_affine_hull(struct isl_basic_set *bset)
937 return (struct isl_basic_set *)
938 isl_basic_map_affine_hull((struct isl_basic_map *)bset);
941 struct isl_basic_map *isl_map_affine_hull(struct isl_map *map)
944 struct isl_basic_map *model = NULL;
945 struct isl_basic_map *hull = NULL;
948 map = isl_map_detect_equalities(map);
949 map = isl_map_align_divs(map);
955 hull = isl_basic_map_empty_like_map(map);
960 model = isl_basic_map_copy(map->p[0]);
961 set = isl_map_underlying_set(map);
962 set = isl_set_cow(set);
966 for (i = 0; i < set->n; ++i) {
967 set->p[i] = isl_basic_set_cow(set->p[i]);
968 set->p[i] = isl_basic_set_affine_hull(set->p[i]);
969 set->p[i] = isl_basic_set_gauss(set->p[i], NULL);
973 set = isl_set_remove_empty_parts(set);
975 hull = isl_basic_map_empty_like(model);
976 isl_basic_map_free(model);
978 struct isl_basic_set *bset;
980 set->p[0] = affine_hull(set->p[0], set->p[--set->n]);
984 bset = isl_basic_set_copy(set->p[0]);
985 hull = isl_basic_map_overlying_set(bset, model);
988 hull = isl_basic_map_simplify(hull);
989 return isl_basic_map_finalize(hull);
991 isl_basic_map_free(model);
996 struct isl_basic_set *isl_set_affine_hull(struct isl_set *set)
998 return (struct isl_basic_set *)
999 isl_map_affine_hull((struct isl_map *)set);