3 #include "isl_map_private.h"
7 #include "isl_equalities.h"
9 static struct isl_basic_set *uset_convex_hull(struct isl_set *set);
11 static swap_ineq(struct isl_basic_map *bmap, unsigned i, unsigned j)
17 bmap->ineq[i] = bmap->ineq[j];
22 /* Compute the convex hull of a basic map, by removing the redundant
23 * constraints. If the minimal value along the normal of a constraint
24 * is the same if the constraint is removed, then the constraint is redundant.
26 * Alternatively, we could have intersected the basic map with the
27 * corresponding equality and the checked if the dimension was that
30 struct isl_basic_map *isl_basic_map_convex_hull(struct isl_basic_map *bmap)
37 bmap = isl_basic_map_implicit_equalities(bmap);
41 if (F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
43 if (F_ISSET(bmap, ISL_BASIC_MAP_NO_REDUNDANT))
49 for (i = bmap->n_ineq-1; i >= 0; --i) {
50 enum isl_lp_result res;
51 swap_ineq(bmap, i, bmap->n_ineq-1);
53 res = isl_solve_lp(bmap, 0,
54 bmap->ineq[bmap->n_ineq]+1, ctx->one, &opt_n, &opt_d);
56 swap_ineq(bmap, i, bmap->n_ineq-1);
57 if (res == isl_lp_unbounded)
59 if (res == isl_lp_error)
61 if (res == isl_lp_empty) {
62 bmap = isl_basic_map_set_to_empty(bmap);
65 isl_int_addmul(opt_n, opt_d, bmap->ineq[i][0]);
66 if (!isl_int_is_neg(opt_n))
67 isl_basic_map_drop_inequality(bmap, i);
72 F_SET(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
77 isl_basic_map_free(bmap);
81 struct isl_basic_set *isl_basic_set_convex_hull(struct isl_basic_set *bset)
83 return (struct isl_basic_set *)
84 isl_basic_map_convex_hull((struct isl_basic_map *)bset);
87 /* Check if "c" is a direction with a lower bound in "set" that is independent
88 * of the previously found "n" bounds in "dirs".
89 * If so, add it to the list, with the negative of the lower bound
90 * in the constant position, i.e., such that c correspond to a bounding
91 * hyperplane (but not necessarily a facet).
93 static int is_independent_bound(struct isl_ctx *ctx,
94 struct isl_set *set, isl_int *c,
95 struct isl_mat *dirs, int n)
102 isl_seq_cpy(dirs->row[n]+1, c+1, dirs->n_col-1);
104 int pos = isl_seq_first_non_zero(dirs->row[n]+1, dirs->n_col-1);
107 for (i = 0; i < n; ++i) {
109 pos_i = isl_seq_first_non_zero(dirs->row[i]+1, dirs->n_col-1);
114 isl_seq_elim(dirs->row[n]+1, dirs->row[i]+1, pos,
115 dirs->n_col-1, NULL);
116 pos = isl_seq_first_non_zero(dirs->row[n]+1, dirs->n_col-1);
123 isl_int_init(opt_denom);
125 for (j = 0; j < set->n; ++j) {
126 enum isl_lp_result res;
128 if (F_ISSET(set->p[j], ISL_BASIC_MAP_EMPTY))
131 res = isl_solve_lp((struct isl_basic_map*)set->p[j],
132 0, dirs->row[n]+1, ctx->one, &opt, &opt_denom);
133 if (res == isl_lp_unbounded)
135 if (res == isl_lp_error)
137 if (res == isl_lp_empty) {
138 set->p[j] = isl_basic_set_set_to_empty(set->p[j]);
143 if (!isl_int_is_one(opt_denom))
144 isl_seq_scale(dirs->row[n], dirs->row[n], opt_denom,
146 if (first || isl_int_lt(opt, dirs->row[n][0]))
147 isl_int_set(dirs->row[n][0], opt);
151 isl_int_clear(opt_denom);
154 isl_int_neg(dirs->row[n][0], dirs->row[n][0]);
157 isl_int *t = dirs->row[n];
158 for (k = n; k > i; --k)
159 dirs->row[k] = dirs->row[k-1];
165 isl_int_clear(opt_denom);
169 /* Compute and return a maximal set of linearly independent bounds
170 * on the set "set", based on the constraints of the basic sets
173 static struct isl_mat *independent_bounds(struct isl_ctx *ctx,
177 struct isl_mat *dirs = NULL;
179 dirs = isl_mat_alloc(ctx, set->dim, 1+set->dim);
184 for (i = 0; n < set->dim && i < set->n; ++i) {
186 struct isl_basic_set *bset = set->p[i];
188 for (j = 0; n < set->dim && j < bset->n_eq; ++j) {
189 f = is_independent_bound(ctx, set, bset->eq[j],
197 isl_seq_neg(bset->eq[j], bset->eq[j], 1+set->dim);
198 f = is_independent_bound(ctx, set, bset->eq[j],
200 isl_seq_neg(bset->eq[j], bset->eq[j], 1+set->dim);
206 for (j = 0; n < set->dim && j < bset->n_ineq; ++j) {
207 f = is_independent_bound(ctx, set, bset->ineq[j],
218 isl_mat_free(ctx, dirs);
222 static struct isl_basic_set *isl_basic_set_set_rational(
223 struct isl_basic_set *bset)
228 if (F_ISSET(bset, ISL_BASIC_MAP_RATIONAL))
231 bset = isl_basic_set_cow(bset);
235 F_SET(bset, ISL_BASIC_MAP_RATIONAL);
240 static struct isl_set *isl_set_set_rational(struct isl_set *set)
244 set = isl_set_cow(set);
247 for (i = 0; i < set->n; ++i) {
248 set->p[i] = isl_basic_set_set_rational(set->p[i]);
258 static struct isl_basic_set *isl_basic_set_add_equality(struct isl_ctx *ctx,
259 struct isl_basic_set *bset, isl_int *c)
264 if (F_ISSET(bset, ISL_BASIC_SET_EMPTY))
267 isl_assert(ctx, bset->nparam == 0, goto error);
268 isl_assert(ctx, bset->n_div == 0, goto error);
269 bset = isl_basic_set_extend(bset, 0, bset->dim, 0, 1, 0);
270 i = isl_basic_set_alloc_equality(bset);
273 isl_seq_cpy(bset->eq[i], c, 1 + bset->dim);
276 isl_basic_set_free(bset);
280 static struct isl_set *isl_set_add_equality(struct isl_ctx *ctx,
281 struct isl_set *set, isl_int *c)
285 set = isl_set_cow(set);
288 for (i = 0; i < set->n; ++i) {
289 set->p[i] = isl_basic_set_add_equality(ctx, set->p[i], c);
299 /* Given a union of basic sets, construct the constraints for wrapping
300 * a facet around one of its ridges.
301 * In particular, if each of n the d-dimensional basic sets i in "set"
302 * contains the origin, satisfies the constraints x_1 >= 0 and x_2 >= 0
303 * and is defined by the constraints
307 * then the resulting set is of dimension n*(1+d) and has as contraints
316 static struct isl_basic_set *wrap_constraints(struct isl_ctx *ctx,
319 struct isl_basic_set *lp;
331 for (i = 0; i < set->n; ++i) {
332 n_eq += set->p[i]->n_eq;
333 n_ineq += set->p[i]->n_ineq;
335 lp = isl_basic_set_alloc(ctx, 0, dim * set->n, 0, n_eq, n_ineq);
338 k = isl_basic_set_alloc_equality(lp);
339 isl_int_set_si(lp->eq[k][0], -1);
340 for (i = 0; i < set->n; ++i) {
341 isl_int_set_si(lp->eq[k][1+dim*i], 0);
342 isl_int_set_si(lp->eq[k][1+dim*i+1], 1);
343 isl_seq_clr(lp->eq[k]+1+dim*i+2, dim-2);
345 for (i = 0; i < set->n; ++i) {
346 k = isl_basic_set_alloc_inequality(lp);
347 isl_seq_clr(lp->ineq[k], 1+lp->dim);
348 isl_int_set_si(lp->ineq[k][1+dim*i], 1);
350 for (j = 0; j < set->p[i]->n_eq; ++j) {
351 k = isl_basic_set_alloc_equality(lp);
352 isl_seq_clr(lp->eq[k], 1+dim*i);
353 isl_seq_cpy(lp->eq[k]+1+dim*i, set->p[i]->eq[j], dim);
354 isl_seq_clr(lp->eq[k]+1+dim*(i+1), dim*(set->n-i-1));
357 for (j = 0; j < set->p[i]->n_ineq; ++j) {
358 k = isl_basic_set_alloc_inequality(lp);
359 isl_seq_clr(lp->ineq[k], 1+dim*i);
360 isl_seq_cpy(lp->ineq[k]+1+dim*i, set->p[i]->ineq[j], dim);
361 isl_seq_clr(lp->ineq[k]+1+dim*(i+1), dim*(set->n-i-1));
367 /* Given a facet "facet" of the convex hull of "set" and a facet "ridge"
368 * of that facet, compute the other facet of the convex hull that contains
371 * We first transform the set such that the facet constraint becomes
379 * and on that facet, the constraint that defines the ridge is
383 * (This transformation is not strictly needed, all that is needed is
384 * that the ridge contains the origin.)
386 * Since the ridge contains the origin, the cone of the convex hull
387 * will be of the form
392 * with this second constraint defining the new facet.
393 * The constant a is obtained by settting x_1 in the cone of the
394 * convex hull to 1 and minimizing x_2.
395 * Now, each element in the cone of the convex hull is the sum
396 * of elements in the cones of the basic sets.
397 * If a_i is the dilation factor of basic set i, then the problem
398 * we need to solve is
411 * the constraints of each (transformed) basic set.
412 * If a = n/d, then the consstraint defining the new facet (in the transformed
415 * -n x_1 + d x_2 >= 0
417 * In the original space, we need to take the same combination of the
418 * corresponding constraints "facet" and "ridge".
420 static isl_int *wrap_facet(struct isl_ctx *ctx, struct isl_set *set,
421 isl_int *facet, isl_int *ridge)
424 struct isl_mat *T = NULL;
425 struct isl_basic_set *lp = NULL;
427 enum isl_lp_result res;
431 set = isl_set_copy(set);
434 T = isl_mat_alloc(ctx, 3, 1 + set->dim);
437 isl_int_set_si(T->row[0][0], 1);
438 isl_seq_clr(T->row[0]+1, set->dim);
439 isl_seq_cpy(T->row[1], facet, 1+set->dim);
440 isl_seq_cpy(T->row[2], ridge, 1+set->dim);
441 T = isl_mat_right_inverse(ctx, T);
442 set = isl_set_preimage(ctx, set, T);
444 lp = wrap_constraints(ctx, set);
445 obj = isl_vec_alloc(ctx, dim*set->n);
448 for (i = 0; i < set->n; ++i) {
449 isl_seq_clr(obj->block.data+dim*i, 2);
450 isl_int_set_si(obj->block.data[dim*i+2], 1);
451 isl_seq_clr(obj->block.data+dim*i+3, dim-3);
455 res = isl_solve_lp((struct isl_basic_map *)lp, 0,
456 obj->block.data, ctx->one, &num, &den);
457 if (res == isl_lp_ok) {
458 isl_int_neg(num, num);
459 isl_seq_combine(facet, num, facet, den, ridge, dim);
463 isl_vec_free(ctx, obj);
464 isl_basic_set_free(lp);
466 isl_assert(ctx, res == isl_lp_ok, return NULL);
469 isl_basic_set_free(lp);
470 isl_mat_free(ctx, T);
475 /* Given a set of d linearly independent bounding constraints of the
476 * convex hull of "set", compute the constraint of a facet of "set".
478 * We first compute the intersection with the first bounding hyperplane
479 * and remove the component corresponding to this hyperplane from
480 * other bounds (in homogeneous space).
481 * We then wrap around one of the remaining bounding constraints
482 * and continue the process until all bounding constraints have been
483 * taken into account.
484 * The resulting linear combination of the bounding constraints will
485 * correspond to a facet of the convex hull.
487 static struct isl_mat *initial_facet_constraint(struct isl_ctx *ctx,
488 struct isl_set *set, struct isl_mat *bounds)
490 struct isl_set *slice = NULL;
491 struct isl_basic_set *face = NULL;
492 struct isl_mat *m, *U, *Q;
495 isl_assert(ctx, set->n > 0, goto error);
496 isl_assert(ctx, bounds->n_row == set->dim, goto error);
498 while (bounds->n_row > 1) {
499 slice = isl_set_copy(set);
500 slice = isl_set_add_equality(ctx, slice, bounds->row[0]);
501 face = isl_set_affine_hull(slice);
504 if (face->n_eq == 1) {
505 isl_basic_set_free(face);
508 m = isl_mat_alloc(ctx, 1 + face->n_eq, 1 + face->dim);
511 isl_int_set_si(m->row[0][0], 1);
512 isl_seq_clr(m->row[0]+1, face->dim);
513 for (i = 0; i < face->n_eq; ++i)
514 isl_seq_cpy(m->row[1 + i], face->eq[i], 1 + face->dim);
515 U = isl_mat_right_inverse(ctx, m);
516 Q = isl_mat_right_inverse(ctx, isl_mat_copy(ctx, U));
517 U = isl_mat_drop_cols(ctx, U, 1 + face->n_eq,
518 face->dim - face->n_eq);
519 Q = isl_mat_drop_rows(ctx, Q, 1 + face->n_eq,
520 face->dim - face->n_eq);
521 U = isl_mat_drop_cols(ctx, U, 0, 1);
522 Q = isl_mat_drop_rows(ctx, Q, 0, 1);
523 bounds = isl_mat_product(ctx, bounds, U);
524 bounds = isl_mat_product(ctx, bounds, Q);
525 while (isl_seq_first_non_zero(bounds->row[bounds->n_row-1],
526 bounds->n_col) == -1) {
528 isl_assert(ctx, bounds->n_row > 1, goto error);
530 if (!wrap_facet(ctx, set, bounds->row[0],
531 bounds->row[bounds->n_row-1]))
533 isl_basic_set_free(face);
538 isl_basic_set_free(face);
539 isl_mat_free(ctx, bounds);
543 /* Given the bounding constraint "c" of a facet of the convex hull of "set",
544 * compute a hyperplane description of the facet, i.e., compute the facets
547 * We compute an affine transformation that transforms the constraint
556 * by computing the right inverse U of a matrix that starts with the rows
569 * Since z_1 is zero, we can drop this variable as well as the corresponding
570 * column of U to obtain
578 * with Q' equal to Q, but without the corresponding row.
579 * After computing the facets of the facet in the z' space,
580 * we convert them back to the x space through Q.
582 static struct isl_basic_set *compute_facet(struct isl_ctx *ctx,
583 struct isl_set *set, isl_int *c)
585 struct isl_mat *m, *U, *Q;
586 struct isl_basic_set *facet;
588 set = isl_set_copy(set);
589 m = isl_mat_alloc(ctx, 2, 1 + set->dim);
592 isl_int_set_si(m->row[0][0], 1);
593 isl_seq_clr(m->row[0]+1, set->dim);
594 isl_seq_cpy(m->row[1], c, 1+set->dim);
595 U = isl_mat_right_inverse(ctx, m);
596 Q = isl_mat_right_inverse(ctx, isl_mat_copy(ctx, U));
597 U = isl_mat_drop_cols(ctx, U, 1, 1);
598 Q = isl_mat_drop_rows(ctx, Q, 1, 1);
599 set = isl_set_preimage(ctx, set, U);
600 facet = uset_convex_hull(set);
601 facet = isl_basic_set_preimage(ctx, facet, Q);
608 /* Given an initial facet constraint, compute the remaining facets.
609 * We do this by running through all facets found so far and computing
610 * the adjacent facets through wrapping, adding those facets that we
611 * hadn't already found before.
613 * This function can still be significantly optimized by checking which of
614 * the facets of the basic sets are also facets of the convex hull and
615 * using all the facets so far to help in constructing the facets of the
618 * using the technique in section "3.1 Ridge Generation" of
619 * "Extended Convex Hull" by Fukuda et al.
621 static struct isl_basic_set *extend(struct isl_ctx *ctx, struct isl_set *set,
622 struct isl_mat *initial)
626 struct isl_basic_set *hull = NULL;
627 struct isl_basic_set *facet = NULL;
631 isl_assert(ctx, set->n > 0, goto error);
634 for (i = 0; i < set->n; ++i) {
635 n_ineq += set->p[i]->n_eq;
636 n_ineq += set->p[i]->n_ineq;
638 isl_assert(ctx, 1 + set->dim == initial->n_col, goto error);
639 hull = isl_basic_set_alloc(ctx, 0, set->dim, 0, 0, n_ineq);
640 hull = isl_basic_set_set_rational(hull);
643 k = isl_basic_set_alloc_inequality(hull);
646 isl_seq_cpy(hull->ineq[k], initial->row[0], initial->n_col);
647 for (i = 0; i < hull->n_ineq; ++i) {
648 facet = compute_facet(ctx, set, hull->ineq[i]);
651 if (facet->n_ineq + hull->n_ineq > n_ineq) {
652 hull = isl_basic_set_extend(hull,
653 hull->nparam, hull->dim, 0, 0, facet->n_ineq);
654 n_ineq = hull->n_ineq + facet->n_ineq;
656 for (j = 0; j < facet->n_ineq; ++j) {
657 k = isl_basic_set_alloc_inequality(hull);
660 isl_seq_cpy(hull->ineq[k], hull->ineq[i], 1+hull->dim);
661 if (!wrap_facet(ctx, set, hull->ineq[k], facet->ineq[j]))
663 for (f = 0; f < k; ++f)
664 if (isl_seq_eq(hull->ineq[f], hull->ineq[k],
668 isl_basic_set_free_inequality(hull, 1);
670 isl_basic_set_free(facet);
672 hull = isl_basic_set_simplify(hull);
673 hull = isl_basic_set_finalize(hull);
676 isl_basic_set_free(facet);
677 isl_basic_set_free(hull);
681 /* Special case for computing the convex hull of a one dimensional set.
682 * We simply collect the lower and upper bounds of each basic set
683 * and the biggest of those.
685 static struct isl_basic_set *convex_hull_1d(struct isl_ctx *ctx,
688 struct isl_mat *c = NULL;
689 isl_int *lower = NULL;
690 isl_int *upper = NULL;
693 struct isl_basic_set *hull;
695 for (i = 0; i < set->n; ++i) {
696 set->p[i] = isl_basic_set_simplify(set->p[i]);
700 set = isl_set_remove_empty_parts(set);
703 isl_assert(ctx, set->n > 0, goto error);
704 c = isl_mat_alloc(ctx, 2, 2);
708 if (set->p[0]->n_eq > 0) {
709 isl_assert(ctx, set->p[0]->n_eq == 1, goto error);
712 if (isl_int_is_pos(set->p[0]->eq[0][1])) {
713 isl_seq_cpy(lower, set->p[0]->eq[0], 2);
714 isl_seq_neg(upper, set->p[0]->eq[0], 2);
716 isl_seq_neg(lower, set->p[0]->eq[0], 2);
717 isl_seq_cpy(upper, set->p[0]->eq[0], 2);
720 for (j = 0; j < set->p[0]->n_ineq; ++j) {
721 if (isl_int_is_pos(set->p[0]->ineq[j][1])) {
723 isl_seq_cpy(lower, set->p[0]->ineq[j], 2);
726 isl_seq_cpy(upper, set->p[0]->ineq[j], 2);
733 for (i = 0; i < set->n; ++i) {
734 struct isl_basic_set *bset = set->p[i];
738 for (j = 0; j < bset->n_eq; ++j) {
742 isl_int_mul(a, lower[0], bset->eq[j][1]);
743 isl_int_mul(b, lower[1], bset->eq[j][0]);
744 if (isl_int_lt(a, b) && isl_int_is_pos(bset->eq[j][1]))
745 isl_seq_cpy(lower, bset->eq[j], 2);
746 if (isl_int_gt(a, b) && isl_int_is_neg(bset->eq[j][1]))
747 isl_seq_neg(lower, bset->eq[j], 2);
750 isl_int_mul(a, upper[0], bset->eq[j][1]);
751 isl_int_mul(b, upper[1], bset->eq[j][0]);
752 if (isl_int_lt(a, b) && isl_int_is_pos(bset->eq[j][1]))
753 isl_seq_neg(upper, bset->eq[j], 2);
754 if (isl_int_gt(a, b) && isl_int_is_neg(bset->eq[j][1]))
755 isl_seq_cpy(upper, bset->eq[j], 2);
758 for (j = 0; j < bset->n_ineq; ++j) {
759 if (isl_int_is_pos(bset->ineq[j][1]))
761 if (isl_int_is_neg(bset->ineq[j][1]))
763 if (lower && isl_int_is_pos(bset->ineq[j][1])) {
764 isl_int_mul(a, lower[0], bset->ineq[j][1]);
765 isl_int_mul(b, lower[1], bset->ineq[j][0]);
766 if (isl_int_lt(a, b))
767 isl_seq_cpy(lower, bset->ineq[j], 2);
769 if (upper && isl_int_is_neg(bset->ineq[j][1])) {
770 isl_int_mul(a, upper[0], bset->ineq[j][1]);
771 isl_int_mul(b, upper[1], bset->ineq[j][0]);
772 if (isl_int_gt(a, b))
773 isl_seq_cpy(upper, bset->ineq[j], 2);
784 hull = isl_basic_set_alloc(ctx, 0, 1, 0, 0, 2);
785 hull = isl_basic_set_set_rational(hull);
789 k = isl_basic_set_alloc_inequality(hull);
790 isl_seq_cpy(hull->ineq[k], lower, 2);
793 k = isl_basic_set_alloc_inequality(hull);
794 isl_seq_cpy(hull->ineq[k], upper, 2);
796 hull = isl_basic_set_finalize(hull);
798 isl_mat_free(ctx, c);
802 isl_mat_free(ctx, c);
806 /* Project out final n dimensions using Fourier-Motzkin */
807 static struct isl_set *set_project_out(struct isl_ctx *ctx,
808 struct isl_set *set, unsigned n)
810 return isl_set_remove_dims(set, set->dim - n, n);
813 /* If the number of linearly independent bounds we found is smaller
814 * than the dimension, then the convex hull will have a lineality space,
815 * so we may as well project out this lineality space.
816 * We first transform the set such that the first variables correspond
817 * to the directions of the linearly independent bounds and then
818 * project out the remaining variables.
820 static struct isl_basic_set *modulo_lineality(struct isl_ctx *ctx,
821 struct isl_set *set, struct isl_mat *bounds)
824 unsigned old_dim, new_dim;
825 struct isl_mat *H = NULL, *U = NULL, *Q = NULL;
826 struct isl_basic_set *hull;
829 new_dim = bounds->n_row;
830 H = isl_mat_sub_alloc(ctx, bounds->row, 0, bounds->n_row, 1, set->dim);
831 H = isl_mat_left_hermite(ctx, H, 0, &U, &Q);
834 U = isl_mat_lin_to_aff(ctx, U);
835 Q = isl_mat_lin_to_aff(ctx, Q);
836 Q->n_row = 1 + new_dim;
837 isl_mat_free(ctx, H);
838 set = isl_set_preimage(ctx, set, U);
839 set = set_project_out(ctx, set, old_dim - new_dim);
840 hull = uset_convex_hull(set);
841 hull = isl_basic_set_preimage(ctx, hull, Q);
842 isl_mat_free(ctx, bounds);
845 isl_mat_free(ctx, bounds);
846 isl_mat_free(ctx, Q);
851 /* This is the core procedure, where "set" is a "pure" set, i.e.,
852 * without parameters or divs and where the convex hull of set is
853 * known to be full-dimensional.
855 static struct isl_basic_set *uset_convex_hull(struct isl_set *set)
858 struct isl_basic_set *convex_hull = NULL;
859 struct isl_mat *bounds;
862 convex_hull = isl_basic_set_universe(set->ctx, 0, 0);
864 convex_hull = isl_basic_set_set_rational(convex_hull);
868 set = isl_set_set_rational(set);
872 for (i = 0; i < set->n; ++i) {
873 set->p[i] = isl_basic_set_convex_hull(set->p[i]);
877 set = isl_set_remove_empty_parts(set);
881 convex_hull = isl_basic_set_copy(set->p[0]);
886 return convex_hull_1d(set->ctx, set);
888 bounds = independent_bounds(set->ctx, set);
891 if (bounds->n_row < set->dim)
892 return modulo_lineality(set->ctx, set, bounds);
893 bounds = initial_facet_constraint(set->ctx, set, bounds);
896 convex_hull = extend(set->ctx, set, bounds);
897 isl_mat_free(set->ctx, bounds);
906 /* Compute the convex hull of set "set" with affine hull "affine_hull",
907 * We first remove the equalities (transforming the set), compute the
908 * convex hull of the transformed set and then add the equalities back
909 * (after performing the inverse transformation.
911 static struct isl_basic_set *modulo_affine_hull(struct isl_ctx *ctx,
912 struct isl_set *set, struct isl_basic_set *affine_hull)
916 struct isl_basic_set *dummy;
917 struct isl_basic_set *convex_hull;
919 dummy = isl_basic_set_remove_equalities(
920 isl_basic_set_copy(affine_hull), &T, &T2);
923 isl_basic_set_free(dummy);
924 set = isl_set_preimage(ctx, set, T);
925 convex_hull = uset_convex_hull(set);
926 convex_hull = isl_basic_set_preimage(ctx, convex_hull, T2);
927 convex_hull = isl_basic_set_intersect(convex_hull, affine_hull);
930 isl_basic_set_free(affine_hull);
935 /* Compute the convex hull of a map.
937 * The implementation was inspired by "Extended Convex Hull" by Fukuda et al.,
938 * specifically, the wrapping of facets to obtain new facets.
940 struct isl_basic_map *isl_map_convex_hull(struct isl_map *map)
942 struct isl_basic_set *bset;
943 struct isl_basic_set *affine_hull = NULL;
944 struct isl_basic_map *convex_hull = NULL;
945 struct isl_set *set = NULL;
953 convex_hull = isl_basic_map_empty(ctx,
954 map->nparam, map->n_in, map->n_out);
959 set = isl_map_underlying_set(isl_map_copy(map));
963 affine_hull = isl_set_affine_hull(isl_set_copy(set));
966 if (affine_hull->n_eq != 0)
967 bset = modulo_affine_hull(ctx, set, affine_hull);
969 isl_basic_set_free(affine_hull);
970 bset = uset_convex_hull(set);
973 convex_hull = isl_basic_map_overlying_set(bset,
974 isl_basic_map_copy(map->p[0]));
984 struct isl_basic_set *isl_set_convex_hull(struct isl_set *set)
986 return (struct isl_basic_set *)
987 isl_map_convex_hull((struct isl_map *)set);