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 isl_assert(ctx, bset->nparam == 0, goto error);
265 isl_assert(ctx, bset->n_div == 0, goto error);
266 bset = isl_basic_set_extend(bset, 0, bset->dim, 0, 1, 0);
267 i = isl_basic_set_alloc_equality(bset);
270 isl_seq_cpy(bset->eq[i], c, 1 + bset->dim);
273 isl_basic_set_free(bset);
277 static struct isl_set *isl_set_add_equality(struct isl_ctx *ctx,
278 struct isl_set *set, isl_int *c)
282 set = isl_set_cow(set);
285 for (i = 0; i < set->n; ++i) {
286 set->p[i] = isl_basic_set_add_equality(ctx, set->p[i], c);
296 /* Given a union of basic sets, construct the constraints for wrapping
297 * a facet around one of its ridges.
298 * In particular, if each of n the d-dimensional basic sets i in "set"
299 * contains the origin, satisfies the constraints x_1 >= 0 and x_2 >= 0
300 * and is defined by the constraints
304 * then the resulting set is of dimension n*(1+d) and has as contraints
313 static struct isl_basic_set *wrap_constraints(struct isl_ctx *ctx,
316 struct isl_basic_set *lp;
328 for (i = 0; i < set->n; ++i) {
329 n_eq += set->p[i]->n_eq;
330 n_ineq += set->p[i]->n_ineq;
332 lp = isl_basic_set_alloc(ctx, 0, dim * set->n, 0, n_eq, n_ineq);
335 k = isl_basic_set_alloc_equality(lp);
336 isl_int_set_si(lp->eq[k][0], -1);
337 for (i = 0; i < set->n; ++i) {
338 isl_int_set_si(lp->eq[k][1+dim*i], 0);
339 isl_int_set_si(lp->eq[k][1+dim*i+1], 1);
340 isl_seq_clr(lp->eq[k]+1+dim*i+2, dim-2);
342 for (i = 0; i < set->n; ++i) {
343 k = isl_basic_set_alloc_inequality(lp);
344 isl_seq_clr(lp->ineq[k], 1+lp->dim);
345 isl_int_set_si(lp->ineq[k][1+dim*i], 1);
347 for (j = 0; j < set->p[i]->n_eq; ++j) {
348 k = isl_basic_set_alloc_equality(lp);
349 isl_seq_clr(lp->eq[k], 1+dim*i);
350 isl_seq_cpy(lp->eq[k]+1+dim*i, set->p[i]->eq[j], dim);
351 isl_seq_clr(lp->eq[k]+1+dim*(i+1), dim*(set->n-i-1));
354 for (j = 0; j < set->p[i]->n_ineq; ++j) {
355 k = isl_basic_set_alloc_inequality(lp);
356 isl_seq_clr(lp->ineq[k], 1+dim*i);
357 isl_seq_cpy(lp->ineq[k]+1+dim*i, set->p[i]->ineq[j], dim);
358 isl_seq_clr(lp->ineq[k]+1+dim*(i+1), dim*(set->n-i-1));
364 /* Given a facet "facet" of the convex hull of "set" and a facet "ridge"
365 * of that facet, compute the other facet of the convex hull that contains
368 * We first transform the set such that the facet constraint becomes
376 * and on that facet, the constraint that defines the ridge is
380 * (This transformation is not strictly needed, all that is needed is
381 * that the ridge contains the origin.)
383 * Since the ridge contains the origin, the cone of the convex hull
384 * will be of the form
389 * with this second constraint defining the new facet.
390 * The constant a is obtained by settting x_1 in the cone of the
391 * convex hull to 1 and minimizing x_2.
392 * Now, each element in the cone of the convex hull is the sum
393 * of elements in the cones of the basic sets.
394 * If a_i is the dilation factor of basic set i, then the problem
395 * we need to solve is
408 * the constraints of each (transformed) basic set.
409 * If a = n/d, then the consstraint defining the new facet (in the transformed
412 * -n x_1 + d x_2 >= 0
414 * In the original space, we need to take the same combination of the
415 * corresponding constraints "facet" and "ridge".
417 static isl_int *wrap_facet(struct isl_ctx *ctx, struct isl_set *set,
418 isl_int *facet, isl_int *ridge)
421 struct isl_mat *T = NULL;
422 struct isl_basic_set *lp = NULL;
424 enum isl_lp_result res;
428 set = isl_set_copy(set);
431 T = isl_mat_alloc(ctx, 3, 1 + set->dim);
434 isl_int_set_si(T->row[0][0], 1);
435 isl_seq_clr(T->row[0]+1, set->dim);
436 isl_seq_cpy(T->row[1], facet, 1+set->dim);
437 isl_seq_cpy(T->row[2], ridge, 1+set->dim);
438 T = isl_mat_right_inverse(ctx, T);
439 set = isl_set_preimage(ctx, set, T);
441 lp = wrap_constraints(ctx, set);
442 obj = isl_vec_alloc(ctx, dim*set->n);
445 for (i = 0; i < set->n; ++i) {
446 isl_seq_clr(obj->block.data+dim*i, 2);
447 isl_int_set_si(obj->block.data[dim*i+2], 1);
448 isl_seq_clr(obj->block.data+dim*i+3, dim-3);
452 res = isl_solve_lp((struct isl_basic_map *)lp, 0,
453 obj->block.data, ctx->one, &num, &den);
454 if (res == isl_lp_ok) {
455 isl_int_neg(num, num);
456 isl_seq_combine(facet, num, facet, den, ridge, dim);
460 isl_vec_free(ctx, obj);
461 isl_basic_set_free(lp);
463 return (res == isl_lp_ok) ? facet : NULL;
465 isl_basic_set_free(lp);
466 isl_mat_free(ctx, T);
471 /* Given a direction of a constraint, compute the constant term
472 * such that the resulting constraint is a bounding constraint
473 * of the set "set" (which just happens to be a face of the
476 static int compute_bound_on_face(struct isl_ctx *ctx,
477 struct isl_set *set, isl_int *c)
485 isl_int_init(opt_denom);
486 for (j = 0; j < set->n; ++j) {
487 enum isl_lp_result res;
489 if (F_ISSET(set->p[j], ISL_BASIC_MAP_EMPTY))
492 res = isl_solve_lp((struct isl_basic_map*)set->p[j],
493 0, c+1, ctx->one, &opt, &opt_denom);
494 if (res == isl_lp_unbounded)
496 if (res == isl_lp_error)
498 if (res == isl_lp_empty) {
499 set->p[j] = isl_basic_set_set_to_empty(set->p[j]);
504 if (!isl_int_is_one(opt_denom))
505 isl_seq_scale(c, c, opt_denom, 1+set->dim);
506 if (first || isl_int_lt(opt, c[0]))
507 isl_int_set(c[0], opt);
510 isl_assert(ctx, !first, goto error);
512 isl_int_clear(opt_denom);
513 isl_int_neg(c[0], c[0]);
517 isl_int_clear(opt_denom);
521 /* Given a set of d linearly independent bounding constraints of the
522 * convex hull of "set", compute the constraint of a facet of "set".
524 * We first compute the intersection with the first bounding hyperplane
525 * and shift the second bounding constraint to be a bounding constraint
526 * of the resulting face. We then wrap around the next bounding constraint
527 * and continue the process until all bounding constraints have been
528 * taken into account.
529 * The resulting linear combination of the bounding constraints will
530 * correspond to a facet of the convex hull.
532 static struct isl_mat *initial_facet_constraint(struct isl_ctx *ctx,
533 struct isl_set *set, struct isl_mat *bounds)
535 struct isl_set *face = NULL;
538 isl_assert(ctx, set->n > 0, goto error);
539 isl_assert(ctx, bounds->n_row == set->dim, goto error);
541 face = isl_set_copy(set);
544 for (i = 1; i < set->dim; ++i) {
545 face = isl_set_add_equality(ctx, face, bounds->row[i-1]);
546 if (compute_bound_on_face(ctx, face, bounds->row[i]) < 0)
548 if (!wrap_facet(ctx, set, bounds->row[0], bounds->row[i]))
555 isl_mat_free(ctx, bounds);
559 /* Given the bounding constraint "c" of a facet of the convex hull of "set",
560 * compute a hyperplane description of the facet, i.e., compute the facets
563 * We compute an affine transformation that transforms the constraint
572 * by computing the right inverse U of a matrix that starts with the rows
585 * Since z_1 is zero, we can drop this variable as well as the corresponding
586 * column of U to obtain
594 * with Q' equal to Q, but without the corresponding row.
595 * After computing the facets of the facet in the z' space,
596 * we convert them back to the x space through Q.
598 static struct isl_basic_set *compute_facet(struct isl_ctx *ctx,
599 struct isl_set *set, isl_int *c)
601 struct isl_mat *m, *U, *Q;
602 struct isl_basic_set *facet;
604 set = isl_set_copy(set);
605 m = isl_mat_alloc(ctx, 2, 1 + set->dim);
608 isl_int_set_si(m->row[0][0], 1);
609 isl_seq_clr(m->row[0]+1, set->dim);
610 isl_seq_cpy(m->row[1], c, 1+set->dim);
611 m = isl_mat_left_hermite(ctx, m, 0, &U, &Q);
614 U = isl_mat_drop_cols(ctx, U, 1, 1);
615 Q = isl_mat_drop_rows(ctx, Q, 1, 1);
616 set = isl_set_preimage(ctx, set, U);
617 facet = uset_convex_hull(set);
618 facet = isl_basic_set_preimage(ctx, facet, Q);
619 isl_mat_free(ctx, m);
626 /* Given an initial facet constraint, compute the remaining facets.
627 * We do this by running through all facets found so far and computing
628 * the adjacent facets through wrapping, adding those facets that we
629 * hadn't already found before.
631 * This function can still be significantly optimized by checking which of
632 * the facets of the basic sets are also facets of the convex hull and
633 * using all the facets so far to help in constructing the facets of the
636 * using the technique in section "3.1 Ridge Generation" of
637 * "Extended Convex Hull" by Fukuda et al.
639 static struct isl_basic_set *extend(struct isl_ctx *ctx, struct isl_set *set,
640 struct isl_mat *initial)
644 struct isl_basic_set *hull = NULL;
645 struct isl_basic_set *facet = NULL;
649 isl_assert(ctx, set->n > 0, goto error);
652 for (i = 0; i < set->n; ++i) {
653 n_ineq += set->p[i]->n_eq;
654 n_ineq += set->p[i]->n_ineq;
656 isl_assert(ctx, 1 + set->dim == initial->n_col, goto error);
657 hull = isl_basic_set_alloc(ctx, 0, set->dim, 0,
658 0, n_ineq + 2 * set->p[0]->n_div);
661 k = isl_basic_set_alloc_inequality(hull);
664 isl_seq_cpy(hull->ineq[k], initial->row[0], initial->n_col);
665 for (i = 0; i < hull->n_ineq; ++i) {
666 facet = compute_facet(ctx, set, hull->ineq[i]);
669 for (j = 0; j < facet->n_ineq; ++j) {
670 k = isl_basic_set_alloc_inequality(hull);
673 isl_seq_cpy(hull->ineq[k], hull->ineq[i], 1+hull->dim);
674 if (!wrap_facet(ctx, set, hull->ineq[k], facet->ineq[j]))
676 for (f = 0; f < k; ++f)
677 if (isl_seq_eq(hull->ineq[f], hull->ineq[k],
681 isl_basic_set_free_inequality(hull, 1);
683 isl_basic_set_free(facet);
685 hull = isl_basic_set_simplify(hull);
686 hull = isl_basic_set_finalize(hull);
689 isl_basic_set_free(facet);
690 isl_basic_set_free(hull);
694 /* Special case for computing the convex hull of a one dimensional set.
695 * We simply collect the lower and upper bounds of each basic set
696 * and the biggest of those.
698 static struct isl_basic_set *convex_hull_1d(struct isl_ctx *ctx,
701 struct isl_mat *c = NULL;
702 isl_int *lower = NULL;
703 isl_int *upper = NULL;
706 struct isl_basic_set *hull;
708 for (i = 0; i < set->n; ++i) {
709 set->p[i] = isl_basic_set_simplify(set->p[i]);
713 set = isl_set_remove_empty_parts(set);
716 isl_assert(ctx, set->n > 0, goto error);
717 c = isl_mat_alloc(ctx, 2, 2);
721 if (set->p[0]->n_eq > 0) {
722 isl_assert(ctx, set->p[0]->n_eq == 1, goto error);
725 if (isl_int_is_pos(set->p[0]->eq[0][1])) {
726 isl_seq_cpy(lower, set->p[0]->eq[0], 2);
727 isl_seq_neg(upper, set->p[0]->eq[0], 2);
729 isl_seq_neg(lower, set->p[0]->eq[0], 2);
730 isl_seq_cpy(upper, set->p[0]->eq[0], 2);
733 for (j = 0; j < set->p[0]->n_ineq; ++j) {
734 if (isl_int_is_pos(set->p[0]->ineq[j][1])) {
736 isl_seq_cpy(lower, set->p[0]->ineq[j], 2);
739 isl_seq_cpy(upper, set->p[0]->ineq[j], 2);
746 for (i = 0; i < set->n; ++i) {
747 struct isl_basic_set *bset = set->p[i];
751 for (j = 0; j < bset->n_eq; ++j) {
755 isl_int_mul(a, lower[0], bset->eq[j][1]);
756 isl_int_mul(b, lower[1], bset->eq[j][0]);
757 if (isl_int_lt(a, b) && isl_int_is_pos(bset->eq[j][1]))
758 isl_seq_cpy(lower, bset->eq[j], 2);
759 if (isl_int_gt(a, b) && isl_int_is_neg(bset->eq[j][1]))
760 isl_seq_neg(lower, bset->eq[j], 2);
763 isl_int_mul(a, upper[0], bset->eq[j][1]);
764 isl_int_mul(b, upper[1], bset->eq[j][0]);
765 if (isl_int_lt(a, b) && isl_int_is_pos(bset->eq[j][1]))
766 isl_seq_neg(upper, bset->eq[j], 2);
767 if (isl_int_gt(a, b) && isl_int_is_neg(bset->eq[j][1]))
768 isl_seq_cpy(upper, bset->eq[j], 2);
771 for (j = 0; j < bset->n_ineq; ++j) {
772 if (isl_int_is_pos(bset->ineq[j][1]))
774 if (isl_int_is_neg(bset->ineq[j][1]))
776 if (lower && isl_int_is_pos(bset->ineq[j][1])) {
777 isl_int_mul(a, lower[0], bset->ineq[j][1]);
778 isl_int_mul(b, lower[1], bset->ineq[j][0]);
779 if (isl_int_lt(a, b))
780 isl_seq_cpy(lower, bset->ineq[j], 2);
782 if (upper && isl_int_is_neg(bset->ineq[j][1])) {
783 isl_int_mul(a, upper[0], bset->ineq[j][1]);
784 isl_int_mul(b, upper[1], bset->ineq[j][0]);
785 if (isl_int_gt(a, b))
786 isl_seq_cpy(upper, bset->ineq[j], 2);
797 hull = isl_basic_set_alloc(ctx, 0, 1, 0, 0, 2);
801 k = isl_basic_set_alloc_inequality(hull);
802 isl_seq_cpy(hull->ineq[k], lower, 2);
805 k = isl_basic_set_alloc_inequality(hull);
806 isl_seq_cpy(hull->ineq[k], upper, 2);
808 hull = isl_basic_set_finalize(hull);
810 isl_mat_free(ctx, c);
814 isl_mat_free(ctx, c);
818 /* Project out final n dimensions using Fourier-Motzkin */
819 static struct isl_set *set_project_out(struct isl_ctx *ctx,
820 struct isl_set *set, unsigned n)
822 return isl_set_remove_dims(set, set->dim - n, n);
825 /* If the number of linearly independent bounds we found is smaller
826 * than the dimension, then the convex hull will have a lineality space,
827 * so we may as well project out this lineality space.
828 * We first transform the set such that the first variables correspond
829 * to the directions of the linearly independent bounds and then
830 * project out the remaining variables.
832 static struct isl_basic_set *modulo_lineality(struct isl_ctx *ctx,
833 struct isl_set *set, struct isl_mat *bounds)
836 unsigned old_dim, new_dim;
837 struct isl_mat *H = NULL, *U = NULL, *Q = NULL;
838 struct isl_basic_set *hull;
841 new_dim = bounds->n_row;
842 H = isl_mat_sub_alloc(ctx, bounds->row, 0, bounds->n_row, 1, set->dim);
843 H = isl_mat_left_hermite(ctx, H, 0, &U, &Q);
846 U = isl_mat_lin_to_aff(ctx, U);
847 Q = isl_mat_lin_to_aff(ctx, Q);
848 Q->n_row = 1 + new_dim;
849 isl_mat_free(ctx, H);
850 set = isl_set_preimage(ctx, set, U);
851 set = set_project_out(ctx, set, old_dim - new_dim);
852 hull = uset_convex_hull(set);
853 hull = isl_basic_set_preimage(ctx, hull, Q);
854 isl_mat_free(ctx, bounds);
857 isl_mat_free(ctx, bounds);
858 isl_mat_free(ctx, Q);
863 /* This is the core procedure, where "set" is a "pure" set, i.e.,
864 * without parameters or divs and where the convex hull of set is
865 * known to be full-dimensional.
867 static struct isl_basic_set *uset_convex_hull(struct isl_set *set)
870 struct isl_basic_set *convex_hull = NULL;
871 struct isl_mat *bounds;
874 convex_hull = isl_basic_set_universe(set->ctx, 0, 0);
879 set = isl_set_set_rational(set);
883 for (i = 0; i < set->n; ++i) {
884 set->p[i] = isl_basic_set_convex_hull(set->p[i]);
888 set = isl_set_remove_empty_parts(set);
892 convex_hull = isl_basic_set_copy(set->p[0]);
897 return convex_hull_1d(set->ctx, set);
899 bounds = independent_bounds(set->ctx, set);
902 if (bounds->n_row < set->dim)
903 return modulo_lineality(set->ctx, set, bounds);
904 bounds = initial_facet_constraint(set->ctx, set, bounds);
907 convex_hull = extend(set->ctx, set, bounds);
908 isl_mat_free(set->ctx, bounds);
917 /* Compute the convex hull of set "set" with affine hull "affine_hull",
918 * We first remove the equalities (transforming the set), compute the
919 * convex hull of the transformed set and then add the equalities back
920 * (after performing the inverse transformation.
922 static struct isl_basic_set *modulo_affine_hull(struct isl_ctx *ctx,
923 struct isl_set *set, struct isl_basic_set *affine_hull)
927 struct isl_basic_set *dummy;
928 struct isl_basic_set *convex_hull;
930 dummy = isl_basic_set_remove_equalities(
931 isl_basic_set_copy(affine_hull), &T, &T2);
934 isl_basic_set_free(dummy);
935 set = isl_set_preimage(ctx, set, T);
936 convex_hull = uset_convex_hull(set);
937 convex_hull = isl_basic_set_preimage(ctx, convex_hull, T2);
938 convex_hull = isl_basic_set_intersect(convex_hull, affine_hull);
941 isl_basic_set_free(affine_hull);
946 /* Compute the convex hull of a map.
948 * The implementation was inspired by "Extended Convex Hull" by Fukuda et al.,
949 * specifically, the wrapping of facets to obtain new facets.
951 struct isl_basic_map *isl_map_convex_hull(struct isl_map *map)
953 struct isl_basic_set *bset;
954 struct isl_basic_set *affine_hull = NULL;
955 struct isl_basic_map *convex_hull = NULL;
956 struct isl_set *set = NULL;
964 convex_hull = isl_basic_map_empty(ctx,
965 map->nparam, map->n_in, map->n_out);
970 set = isl_map_underlying_set(isl_map_copy(map));
974 affine_hull = isl_set_affine_hull(isl_set_copy(set));
977 if (affine_hull->n_eq != 0)
978 bset = modulo_affine_hull(ctx, set, affine_hull);
980 isl_basic_set_free(affine_hull);
981 bset = uset_convex_hull(set);
984 convex_hull = isl_basic_map_overlying_set(bset,
985 isl_basic_map_copy(map->p[0]));
995 struct isl_basic_set *isl_set_convex_hull(struct isl_set *set)
997 return (struct isl_basic_set *)
998 isl_map_convex_hull((struct isl_map *)set);