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