isl_convex_hull: fix construction of initial facet
[platform/upstream/isl.git] / isl_convex_hull.c
1 #include "isl_lp.h"
2 #include "isl_map.h"
3 #include "isl_map_private.h"
4 #include "isl_mat.h"
5 #include "isl_set.h"
6 #include "isl_seq.h"
7 #include "isl_equalities.h"
8
9 static struct isl_basic_set *uset_convex_hull(struct isl_set *set);
10
11 static swap_ineq(struct isl_basic_map *bmap, unsigned i, unsigned j)
12 {
13         isl_int *t;
14
15         if (i != j) {
16                 t = bmap->ineq[i];
17                 bmap->ineq[i] = bmap->ineq[j];
18                 bmap->ineq[j] = t;
19         }
20 }
21
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.
25  *
26  * Alternatively, we could have intersected the basic map with the
27  * corresponding equality and the checked if the dimension was that
28  * of a facet.
29  */
30 struct isl_basic_map *isl_basic_map_convex_hull(struct isl_basic_map *bmap)
31 {
32         int i;
33         isl_int opt_n;
34         isl_int opt_d;
35         struct isl_ctx *ctx;
36
37         bmap = isl_basic_map_implicit_equalities(bmap);
38         if (!bmap)
39                 return NULL;
40
41         if (F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
42                 return bmap;
43         if (F_ISSET(bmap, ISL_BASIC_MAP_NO_REDUNDANT))
44                 return bmap;
45
46         ctx = bmap->ctx;
47         isl_int_init(opt_n);
48         isl_int_init(opt_d);
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);
52                 bmap->n_ineq--;
53                 res = isl_solve_lp(bmap, 0,
54                         bmap->ineq[bmap->n_ineq]+1, ctx->one, &opt_n, &opt_d);
55                 bmap->n_ineq++;
56                 swap_ineq(bmap, i, bmap->n_ineq-1);
57                 if (res == isl_lp_unbounded)
58                         continue;
59                 if (res == isl_lp_error)
60                         goto error;
61                 if (res == isl_lp_empty) {
62                         bmap = isl_basic_map_set_to_empty(bmap);
63                         break;
64                 }
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);
68         }
69         isl_int_clear(opt_n);
70         isl_int_clear(opt_d);
71
72         F_SET(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
73         return bmap;
74 error:
75         isl_int_clear(opt_n);
76         isl_int_clear(opt_d);
77         isl_basic_map_free(bmap);
78         return NULL;
79 }
80
81 struct isl_basic_set *isl_basic_set_convex_hull(struct isl_basic_set *bset)
82 {
83         return (struct isl_basic_set *)
84                 isl_basic_map_convex_hull((struct isl_basic_map *)bset);
85 }
86
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).
92  */
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)
96 {
97         int first;
98         int i = 0, j;
99         isl_int opt;
100         isl_int opt_denom;
101
102         isl_seq_cpy(dirs->row[n]+1, c+1, dirs->n_col-1);
103         if (n != 0) {
104                 int pos = isl_seq_first_non_zero(dirs->row[n]+1, dirs->n_col-1);
105                 if (pos < 0)
106                         return 0;
107                 for (i = 0; i < n; ++i) {
108                         int pos_i;
109                         pos_i = isl_seq_first_non_zero(dirs->row[i]+1, dirs->n_col-1);
110                         if (pos_i < pos)
111                                 continue;
112                         if (pos_i > pos)
113                                 break;
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);
117                         if (pos < 0)
118                                 return 0;
119                 }
120         }
121
122         isl_int_init(opt);
123         isl_int_init(opt_denom);
124         first = 1;
125         for (j = 0; j < set->n; ++j) {
126                 enum isl_lp_result res;
127
128                 if (F_ISSET(set->p[j], ISL_BASIC_MAP_EMPTY))
129                         continue;
130
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)
134                         break;
135                 if (res == isl_lp_error)
136                         goto error;
137                 if (res == isl_lp_empty) {
138                         set->p[j] = isl_basic_set_set_to_empty(set->p[j]);
139                         if (!set->p[j])
140                                 goto error;
141                         continue;
142                 }
143                 if (!isl_int_is_one(opt_denom))
144                         isl_seq_scale(dirs->row[n], dirs->row[n], opt_denom,
145                                         dirs->n_col);
146                 if (first || isl_int_lt(opt, dirs->row[n][0]))
147                         isl_int_set(dirs->row[n][0], opt);
148                 first = 0;
149         }
150         isl_int_clear(opt);
151         isl_int_clear(opt_denom);
152         if (j < set->n)
153                 return 0;
154         isl_int_neg(dirs->row[n][0], dirs->row[n][0]);
155         if (i < n) {
156                 int k;
157                 isl_int *t = dirs->row[n];
158                 for (k = n; k > i; --k)
159                         dirs->row[k] = dirs->row[k-1];
160                 dirs->row[i] = t;
161         }
162         return 1;
163 error:
164         isl_int_clear(opt);
165         isl_int_clear(opt_denom);
166         return -1;
167 }
168
169 /* Compute and return a maximal set of linearly independent bounds
170  * on the set "set", based on the constraints of the basic sets
171  * in "set".
172  */
173 static struct isl_mat *independent_bounds(struct isl_ctx *ctx,
174         struct isl_set *set)
175 {
176         int i, j, n;
177         struct isl_mat *dirs = NULL;
178
179         dirs = isl_mat_alloc(ctx, set->dim, 1+set->dim);
180         if (!dirs)
181                 goto error;
182
183         n = 0;
184         for (i = 0; n < set->dim && i < set->n; ++i) {
185                 int f;
186                 struct isl_basic_set *bset = set->p[i];
187
188                 for (j = 0; n < set->dim && j < bset->n_eq; ++j) {
189                         f = is_independent_bound(ctx, set, bset->eq[j],
190                                                 dirs, n);
191                         if (f < 0)
192                                 goto error;
193                         if (f) {
194                                 ++n;
195                                 continue;
196                         }
197                         isl_seq_neg(bset->eq[j], bset->eq[j], 1+set->dim);
198                         f = is_independent_bound(ctx, set, bset->eq[j],
199                                                 dirs, n);
200                         isl_seq_neg(bset->eq[j], bset->eq[j], 1+set->dim);
201                         if (f < 0)
202                                 goto error;
203                         if (f)
204                                 ++n;
205                 }
206                 for (j = 0; n < set->dim && j < bset->n_ineq; ++j) {
207                         f = is_independent_bound(ctx, set, bset->ineq[j],
208                                                 dirs, n);
209                         if (f < 0)
210                                 goto error;
211                         if (f)
212                                 ++n;
213                 }
214         }
215         dirs->n_row = n;
216         return dirs;
217 error:
218         isl_mat_free(ctx, dirs);
219         return NULL;
220 }
221
222 static struct isl_basic_set *isl_basic_set_set_rational(
223         struct isl_basic_set *bset)
224 {
225         if (!bset)
226                 return NULL;
227
228         if (F_ISSET(bset, ISL_BASIC_MAP_RATIONAL))
229                 return bset;
230
231         bset = isl_basic_set_cow(bset);
232         if (!bset)
233                 return NULL;
234
235         F_SET(bset, ISL_BASIC_MAP_RATIONAL);
236
237         return bset;
238 }
239
240 static struct isl_set *isl_set_set_rational(struct isl_set *set)
241 {
242         int i;
243
244         set = isl_set_cow(set);
245         if (!set)
246                 return NULL;
247         for (i = 0; i < set->n; ++i) {
248                 set->p[i] = isl_basic_set_set_rational(set->p[i]);
249                 if (!set->p[i])
250                         goto error;
251         }
252         return set;
253 error:
254         isl_set_free(set);
255         return NULL;
256 }
257
258 static struct isl_basic_set *isl_basic_set_add_equality(struct isl_ctx *ctx,
259         struct isl_basic_set *bset, isl_int *c)
260 {
261         int i;
262         unsigned total;
263
264         if (F_ISSET(bset, ISL_BASIC_SET_EMPTY))
265                 return bset;
266
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);
271         if (i < 0)
272                 goto error;
273         isl_seq_cpy(bset->eq[i], c, 1 + bset->dim);
274         return bset;
275 error:
276         isl_basic_set_free(bset);
277         return NULL;
278 }
279
280 static struct isl_set *isl_set_add_equality(struct isl_ctx *ctx,
281         struct isl_set *set, isl_int *c)
282 {
283         int i;
284
285         set = isl_set_cow(set);
286         if (!set)
287                 return NULL;
288         for (i = 0; i < set->n; ++i) {
289                 set->p[i] = isl_basic_set_add_equality(ctx, set->p[i], c);
290                 if (!set->p[i])
291                         goto error;
292         }
293         return set;
294 error:
295         isl_set_free(set);
296         return NULL;
297 }
298
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
304  *                                  [ 1 ]
305  *                              A_i [ x ]  >= 0
306  *
307  * then the resulting set is of dimension n*(1+d) and has as contraints
308  *
309  *                                  [ a_i ]
310  *                              A_i [ x_i ] >= 0
311  *
312  *                                    a_i   >= 0
313  *
314  *                      \sum_i x_{i,1} = 1
315  */
316 static struct isl_basic_set *wrap_constraints(struct isl_ctx *ctx,
317                                                         struct isl_set *set)
318 {
319         struct isl_basic_set *lp;
320         unsigned n_eq;
321         unsigned n_ineq;
322         int i, j, k;
323         unsigned dim;
324
325         if (!set)
326                 return NULL;
327
328         dim = 1 + set->dim;
329         n_eq = 1;
330         n_ineq = set->n;
331         for (i = 0; i < set->n; ++i) {
332                 n_eq += set->p[i]->n_eq;
333                 n_ineq += set->p[i]->n_ineq;
334         }
335         lp = isl_basic_set_alloc(ctx, 0, dim * set->n, 0, n_eq, n_ineq);
336         if (!lp)
337                 return NULL;
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);
344         }
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);
349
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));
355                 }
356
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));
362                 }
363         }
364         return lp;
365 }
366
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
369  * the ridge.
370  *
371  * We first transform the set such that the facet constraint becomes
372  *
373  *                      x_1 >= 0
374  *
375  * I.e., the facet is
376  *
377  *                      x_1 = 0
378  *
379  * and on that facet, the constraint that defines the ridge is
380  *
381  *                      x_2 >= 0
382  *
383  * (This transformation is not strictly needed, all that is needed is
384  * that the ridge contains the origin.)
385  *
386  * Since the ridge contains the origin, the cone of the convex hull
387  * will be of the form
388  *
389  *                      x_1 >= 0
390  *                      x_2 >= a x_1
391  *
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
399  *
400  *                      min \sum_i x_{i,2}
401  *                      st
402  *                              \sum_i x_{i,1} = 1
403  *                                  a_i   >= 0
404  *                                [ a_i ]
405  *                              A [ x_i ] >= 0
406  *
407  * with
408  *                                  [  1  ]
409  *                              A_i [ x_i ] >= 0
410  *
411  * the constraints of each (transformed) basic set.
412  * If a = n/d, then the consstraint defining the new facet (in the transformed
413  * space) is
414  *
415  *                      -n x_1 + d x_2 >= 0
416  *
417  * In the original space, we need to take the same combination of the
418  * corresponding constraints "facet" and "ridge".
419  */
420 static isl_int *wrap_facet(struct isl_ctx *ctx, struct isl_set *set,
421         isl_int *facet, isl_int *ridge)
422 {
423         int i;
424         struct isl_mat *T = NULL;
425         struct isl_basic_set *lp = NULL;
426         struct isl_vec *obj;
427         enum isl_lp_result res;
428         isl_int num, den;
429         unsigned dim;
430
431         set = isl_set_copy(set);
432
433         dim = 1 + set->dim;
434         T = isl_mat_alloc(ctx, 3, 1 + set->dim);
435         if (!T)
436                 goto error;
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);
443         T = NULL;
444         lp = wrap_constraints(ctx, set);
445         obj = isl_vec_alloc(ctx, dim*set->n);
446         if (!obj)
447                 goto error;
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);
452         }
453         isl_int_init(num);
454         isl_int_init(den);
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);
460         }
461         isl_int_clear(num);
462         isl_int_clear(den);
463         isl_vec_free(ctx, obj);
464         isl_basic_set_free(lp);
465         isl_set_free(set);
466         isl_assert(ctx, res == isl_lp_ok, return NULL);
467         return facet;
468 error:
469         isl_basic_set_free(lp);
470         isl_mat_free(ctx, T);
471         isl_set_free(set);
472         return NULL;
473 }
474
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".
477  *
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.
486  */
487 static struct isl_mat *initial_facet_constraint(struct isl_ctx *ctx,
488         struct isl_set *set, struct isl_mat *bounds)
489 {
490         struct isl_set *slice = NULL;
491         struct isl_basic_set *face = NULL;
492         struct isl_mat *m, *U, *Q;
493         int i;
494
495         isl_assert(ctx, set->n > 0, goto error);
496         isl_assert(ctx, bounds->n_row == set->dim, goto error);
497
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);
502                 if (!face)
503                         goto error;
504                 if (face->n_eq == 1) {
505                         isl_basic_set_free(face);
506                         break;
507                 }
508                 m = isl_mat_alloc(ctx, 1 + face->n_eq, 1 + face->dim);
509                 if (!m)
510                         goto error;
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) {
527                         bounds->n_row--;
528                         isl_assert(ctx, bounds->n_row > 1, goto error);
529                 }
530                 if (!wrap_facet(ctx, set, bounds->row[0],
531                                           bounds->row[bounds->n_row-1]))
532                         goto error;
533                 isl_basic_set_free(face);
534                 bounds->n_row--;
535         }
536         return bounds;
537 error:
538         isl_basic_set_free(face);
539         isl_mat_free(ctx, bounds);
540         return NULL;
541 }
542
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
545  * of the facet.
546  *
547  * We compute an affine transformation that transforms the constraint
548  *
549  *                        [ 1 ]
550  *                      c [ x ] = 0
551  *
552  * to the constraint
553  *
554  *                         z_1  = 0
555  *
556  * by computing the right inverse U of a matrix that starts with the rows
557  *
558  *                      [ 1 0 ]
559  *                      [  c  ]
560  *
561  * Then
562  *                      [ 1 ]     [ 1 ]
563  *                      [ x ] = U [ z ]
564  * and
565  *                      [ 1 ]     [ 1 ]
566  *                      [ z ] = Q [ x ]
567  *
568  * with Q = U^{-1}
569  * Since z_1 is zero, we can drop this variable as well as the corresponding
570  * column of U to obtain
571  *
572  *                      [ 1 ]      [ 1  ]
573  *                      [ x ] = U' [ z' ]
574  * and
575  *                      [ 1  ]      [ 1 ]
576  *                      [ z' ] = Q' [ x ]
577  *
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.
581  */
582 static struct isl_basic_set *compute_facet(struct isl_ctx *ctx,
583         struct isl_set *set, isl_int *c)
584 {
585         struct isl_mat *m, *U, *Q;
586         struct isl_basic_set *facet;
587
588         set = isl_set_copy(set);
589         m = isl_mat_alloc(ctx, 2, 1 + set->dim);
590         if (!m)
591                 goto error;
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);
602         return facet;
603 error:
604         isl_set_free(set);
605         return NULL;
606 }
607
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.
612  *
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
616  * facets
617  * and/or
618  * using the technique in section "3.1 Ridge Generation" of
619  * "Extended Convex Hull" by Fukuda et al.
620  */
621 static struct isl_basic_set *extend(struct isl_ctx *ctx, struct isl_set *set,
622         struct isl_mat *initial)
623 {
624         int i, j, f;
625         int k;
626         struct isl_basic_set *hull = NULL;
627         struct isl_basic_set *facet = NULL;
628         unsigned n_ineq;
629         unsigned total;
630
631         isl_assert(ctx, set->n > 0, goto error);
632
633         n_ineq = 1;
634         for (i = 0; i < set->n; ++i) {
635                 n_ineq += set->p[i]->n_eq;
636                 n_ineq += set->p[i]->n_ineq;
637         }
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);
641         if (!hull)
642                 goto error;
643         k = isl_basic_set_alloc_inequality(hull);
644         if (k < 0)
645                 goto error;
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]);
649                 if (!facet)
650                         goto error;
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;
655                 }
656                 for (j = 0; j < facet->n_ineq; ++j) {
657                         k = isl_basic_set_alloc_inequality(hull);
658                         if (k < 0)
659                                 goto error;
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]))
662                                 goto error;
663                         for (f = 0; f < k; ++f)
664                                 if (isl_seq_eq(hull->ineq[f], hull->ineq[k],
665                                                 1+hull->dim))
666                                         break;
667                         if (f < k)
668                                 isl_basic_set_free_inequality(hull, 1);
669                 }
670                 isl_basic_set_free(facet);
671         }
672         hull = isl_basic_set_simplify(hull);
673         hull = isl_basic_set_finalize(hull);
674         return hull;
675 error:
676         isl_basic_set_free(facet);
677         isl_basic_set_free(hull);
678         return NULL;
679 }
680
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.
684  */
685 static struct isl_basic_set *convex_hull_1d(struct isl_ctx *ctx,
686         struct isl_set *set)
687 {
688         struct isl_mat *c = NULL;
689         isl_int *lower = NULL;
690         isl_int *upper = NULL;
691         int i, j, k;
692         isl_int a, b;
693         struct isl_basic_set *hull;
694
695         for (i = 0; i < set->n; ++i) {
696                 set->p[i] = isl_basic_set_simplify(set->p[i]);
697                 if (!set->p[i])
698                         goto error;
699         }
700         set = isl_set_remove_empty_parts(set);
701         if (!set)
702                 goto error;
703         isl_assert(ctx, set->n > 0, goto error);
704         c = isl_mat_alloc(ctx, 2, 2);
705         if (!c)
706                 goto error;
707
708         if (set->p[0]->n_eq > 0) {
709                 isl_assert(ctx, set->p[0]->n_eq == 1, goto error);
710                 lower = c->row[0];
711                 upper = c->row[1];
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);
715                 } else {
716                         isl_seq_neg(lower, set->p[0]->eq[0], 2);
717                         isl_seq_cpy(upper, set->p[0]->eq[0], 2);
718                 }
719         } else {
720                 for (j = 0; j < set->p[0]->n_ineq; ++j) {
721                         if (isl_int_is_pos(set->p[0]->ineq[j][1])) {
722                                 lower = c->row[0];
723                                 isl_seq_cpy(lower, set->p[0]->ineq[j], 2);
724                         } else {
725                                 upper = c->row[1];
726                                 isl_seq_cpy(upper, set->p[0]->ineq[j], 2);
727                         }
728                 }
729         }
730
731         isl_int_init(a);
732         isl_int_init(b);
733         for (i = 0; i < set->n; ++i) {
734                 struct isl_basic_set *bset = set->p[i];
735                 int has_lower = 0;
736                 int has_upper = 0;
737
738                 for (j = 0; j < bset->n_eq; ++j) {
739                         has_lower = 1;
740                         has_upper = 1;
741                         if (lower) {
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);
748                         }
749                         if (upper) {
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);
756                         }
757                 }
758                 for (j = 0; j < bset->n_ineq; ++j) {
759                         if (isl_int_is_pos(bset->ineq[j][1]))
760                                 has_lower = 1;
761                         if (isl_int_is_neg(bset->ineq[j][1]))
762                                 has_upper = 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);
768                         }
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);
774                         }
775                 }
776                 if (!has_lower)
777                         lower = NULL;
778                 if (!has_upper)
779                         upper = NULL;
780         }
781         isl_int_clear(a);
782         isl_int_clear(b);
783
784         hull = isl_basic_set_alloc(ctx, 0, 1, 0, 0, 2);
785         hull = isl_basic_set_set_rational(hull);
786         if (!hull)
787                 goto error;
788         if (lower) {
789                 k = isl_basic_set_alloc_inequality(hull);
790                 isl_seq_cpy(hull->ineq[k], lower, 2);
791         }
792         if (upper) {
793                 k = isl_basic_set_alloc_inequality(hull);
794                 isl_seq_cpy(hull->ineq[k], upper, 2);
795         }
796         hull = isl_basic_set_finalize(hull);
797         isl_set_free(set);
798         isl_mat_free(ctx, c);
799         return hull;
800 error:
801         isl_set_free(set);
802         isl_mat_free(ctx, c);
803         return NULL;
804 }
805
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)
809 {
810         return isl_set_remove_dims(set, set->dim - n, n);
811 }
812
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.
819  */
820 static struct isl_basic_set *modulo_lineality(struct isl_ctx *ctx,
821         struct isl_set *set, struct isl_mat *bounds)
822 {
823         int i, j;
824         unsigned old_dim, new_dim;
825         struct isl_mat *H = NULL, *U = NULL, *Q = NULL;
826         struct isl_basic_set *hull;
827
828         old_dim = set->dim;
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);
832         if (!H)
833                 goto error;
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);
843         return hull;
844 error:
845         isl_mat_free(ctx, bounds);
846         isl_mat_free(ctx, Q);
847         isl_set_free(set);
848         return NULL;
849 }
850
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.
854  */
855 static struct isl_basic_set *uset_convex_hull(struct isl_set *set)
856 {
857         int i;
858         struct isl_basic_set *convex_hull = NULL;
859         struct isl_mat *bounds;
860
861         if (set->dim == 0) {
862                 convex_hull = isl_basic_set_universe(set->ctx, 0, 0);
863                 isl_set_free(set);
864                 convex_hull = isl_basic_set_set_rational(convex_hull);
865                 return convex_hull;
866         }
867
868         set = isl_set_set_rational(set);
869
870         if (!set)
871                 goto error;
872         for (i = 0; i < set->n; ++i) {
873                 set->p[i] = isl_basic_set_convex_hull(set->p[i]);
874                 if (!set->p[i])
875                         goto error;
876         }
877         set = isl_set_remove_empty_parts(set);
878         if (!set)
879                 goto error;
880         if (set->n == 1) {
881                 convex_hull = isl_basic_set_copy(set->p[0]);
882                 isl_set_free(set);
883                 return convex_hull;
884         }
885         if (set->dim == 1)
886                 return convex_hull_1d(set->ctx, set);
887
888         bounds = independent_bounds(set->ctx, set);
889         if (!bounds)
890                 goto error;
891         if (bounds->n_row < set->dim)
892                 return modulo_lineality(set->ctx, set, bounds);
893         bounds = initial_facet_constraint(set->ctx, set, bounds);
894         if (!bounds)
895                 goto error;
896         convex_hull = extend(set->ctx, set, bounds);
897         isl_mat_free(set->ctx, bounds);
898         isl_set_free(set);
899
900         return convex_hull;
901 error:
902         isl_set_free(set);
903         return NULL;
904 }
905
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.
910  */
911 static struct isl_basic_set *modulo_affine_hull(struct isl_ctx *ctx,
912         struct isl_set *set, struct isl_basic_set *affine_hull)
913 {
914         struct isl_mat *T;
915         struct isl_mat *T2;
916         struct isl_basic_set *dummy;
917         struct isl_basic_set *convex_hull;
918
919         dummy = isl_basic_set_remove_equalities(
920                         isl_basic_set_copy(affine_hull), &T, &T2);
921         if (!dummy)
922                 goto error;
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);
928         return convex_hull;
929 error:
930         isl_basic_set_free(affine_hull);
931         isl_set_free(set);
932         return NULL;
933 }
934
935 /* Compute the convex hull of a map.
936  *
937  * The implementation was inspired by "Extended Convex Hull" by Fukuda et al.,
938  * specifically, the wrapping of facets to obtain new facets.
939  */
940 struct isl_basic_map *isl_map_convex_hull(struct isl_map *map)
941 {
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;
946         struct isl_ctx *ctx;
947
948         if (!map)
949                 goto error;
950
951         ctx = map->ctx;
952         if (map->n == 0) {
953                 convex_hull = isl_basic_map_empty(ctx,
954                                             map->nparam, map->n_in, map->n_out);
955                 isl_map_free(map);
956                 return convex_hull;
957         }
958
959         set = isl_map_underlying_set(isl_map_copy(map));
960         if (!set)
961                 goto error;
962
963         affine_hull = isl_set_affine_hull(isl_set_copy(set));
964         if (!affine_hull)
965                 goto error;
966         if (affine_hull->n_eq != 0)
967                 bset = modulo_affine_hull(ctx, set, affine_hull);
968         else {
969                 isl_basic_set_free(affine_hull);
970                 bset = uset_convex_hull(set);
971         }
972
973         convex_hull = isl_basic_map_overlying_set(bset,
974                         isl_basic_map_copy(map->p[0]));
975
976         isl_map_free(map);
977         return convex_hull;
978 error:
979         isl_set_free(set);
980         isl_map_free(map);
981         return NULL;
982 }
983
984 struct isl_basic_set *isl_set_convex_hull(struct isl_set *set)
985 {
986         return (struct isl_basic_set *)
987                 isl_map_convex_hull((struct isl_map *)set);
988 }