c557718ed4a44296f7c9f3d3375feec9db662f87
[platform/upstream/isl.git] / isl_morph.c
1 /*
2  * Copyright 2010      INRIA Saclay
3  *
4  * Use of this software is governed by the GNU LGPLv2.1 license
5  *
6  * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
7  * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
8  * 91893 Orsay, France 
9  */
10
11 #include <isl_morph.h>
12 #include <isl_seq.h>
13 #include <isl_map_private.h>
14 #include <isl_dim_private.h>
15 #include <isl_equalities.h>
16
17 __isl_give isl_morph *isl_morph_alloc(
18         __isl_take isl_basic_set *dom, __isl_take isl_basic_set *ran,
19         __isl_take isl_mat *map, __isl_take isl_mat *inv)
20 {
21         isl_morph *morph;
22
23         if (!dom || !ran || !map || !inv)
24                 goto error;
25
26         morph = isl_alloc_type(in_dim->ctx, struct isl_morph);
27         if (!morph)
28                 goto error;
29
30         morph->ref = 1;
31         morph->dom = dom;
32         morph->ran = ran;
33         morph->map = map;
34         morph->inv = inv;
35
36         return morph;
37 error:
38         isl_basic_set_free(dom);
39         isl_basic_set_free(ran);
40         isl_mat_free(map);
41         isl_mat_free(inv);
42         return NULL;
43 }
44
45 __isl_give isl_morph *isl_morph_copy(__isl_keep isl_morph *morph)
46 {
47         if (!morph)
48                 return NULL;
49
50         morph->ref++;
51         return morph;
52 }
53
54 __isl_give isl_morph *isl_morph_dup(__isl_keep isl_morph *morph)
55 {
56         if (!morph)
57                 return NULL;
58
59         return isl_morph_alloc(isl_basic_set_copy(morph->dom),
60                 isl_basic_set_copy(morph->ran),
61                 isl_mat_copy(morph->map), isl_mat_copy(morph->inv));
62 }
63
64 __isl_give isl_morph *isl_morph_cow(__isl_take isl_morph *morph)
65 {
66         if (!morph)
67                 return NULL;
68
69         if (morph->ref == 1)
70                 return morph;
71         morph->ref--;
72         return isl_morph_dup(morph);
73 }
74
75 void isl_morph_free(__isl_take isl_morph *morph)
76 {
77         if (!morph)
78                 return;
79
80         if (--morph->ref > 0)
81                 return;
82
83         isl_basic_set_free(morph->dom);
84         isl_basic_set_free(morph->ran);
85         isl_mat_free(morph->map);
86         isl_mat_free(morph->inv);
87         free(morph);
88 }
89
90 __isl_give isl_dim *isl_morph_get_ran_dim(__isl_keep isl_morph *morph)
91 {
92         if (!morph)
93                 return NULL;
94         
95         return isl_dim_copy(morph->ran->dim);
96 }
97
98 unsigned isl_morph_dom_dim(__isl_keep isl_morph *morph, enum isl_dim_type type)
99 {
100         if (!morph)
101                 return 0;
102
103         return isl_basic_set_dim(morph->dom, type);
104 }
105
106 unsigned isl_morph_ran_dim(__isl_keep isl_morph *morph, enum isl_dim_type type)
107 {
108         if (!morph)
109                 return 0;
110
111         return isl_basic_set_dim(morph->ran, type);
112 }
113
114 __isl_give isl_morph *isl_morph_remove_dom_dims(__isl_take isl_morph *morph,
115         enum isl_dim_type type, unsigned first, unsigned n)
116 {
117         unsigned dom_offset;
118
119         if (n == 0)
120                 return morph;
121
122         morph = isl_morph_cow(morph);
123         if (!morph)
124                 return NULL;
125
126         dom_offset = 1 + isl_dim_offset(morph->dom->dim, type);
127
128         morph->dom = isl_basic_set_remove(morph->dom, type, first, n);
129
130         morph->map = isl_mat_drop_cols(morph->map, dom_offset + first, n);
131
132         morph->inv = isl_mat_drop_rows(morph->inv, dom_offset + first, n);
133
134         if (morph->dom && morph->ran && morph->map && morph->inv)
135                 return morph;
136
137         isl_morph_free(morph);
138         return NULL;
139 }
140
141 __isl_give isl_morph *isl_morph_remove_ran_dims(__isl_take isl_morph *morph,
142         enum isl_dim_type type, unsigned first, unsigned n)
143 {
144         unsigned ran_offset;
145
146         if (n == 0)
147                 return morph;
148
149         morph = isl_morph_cow(morph);
150         if (!morph)
151                 return NULL;
152
153         ran_offset = 1 + isl_dim_offset(morph->ran->dim, type);
154
155         morph->ran = isl_basic_set_remove(morph->ran, type, first, n);
156
157         morph->map = isl_mat_drop_rows(morph->map, ran_offset + first, n);
158
159         morph->inv = isl_mat_drop_cols(morph->inv, ran_offset + first, n);
160
161         if (morph->dom && morph->ran && morph->map && morph->inv)
162                 return morph;
163
164         isl_morph_free(morph);
165         return NULL;
166 }
167
168 void isl_morph_dump(__isl_take isl_morph *morph, FILE *out)
169 {
170         if (!morph)
171                 return;
172
173         isl_basic_set_print(morph->dom, out, 0, "", "", ISL_FORMAT_ISL);
174         isl_basic_set_print(morph->ran, out, 0, "", "", ISL_FORMAT_ISL);
175         isl_mat_dump(morph->map, out, 4);
176         isl_mat_dump(morph->inv, out, 4);
177 }
178
179 __isl_give isl_morph *isl_morph_identity(__isl_keep isl_basic_set *bset)
180 {
181         isl_mat *id;
182         isl_basic_set *universe;
183         unsigned total;
184
185         if (!bset)
186                 return NULL;
187
188         total = isl_basic_set_total_dim(bset);
189         id = isl_mat_identity(bset->ctx, 1 + total);
190         universe = isl_basic_set_universe(isl_dim_copy(bset->dim));
191
192         return isl_morph_alloc(universe, isl_basic_set_copy(universe),
193                 id, isl_mat_copy(id));
194 }
195
196 /* Create a(n identity) morphism between empty sets of the same dimension
197  * a "bset".
198  */
199 __isl_give isl_morph *isl_morph_empty(__isl_keep isl_basic_set *bset)
200 {
201         isl_mat *id;
202         isl_basic_set *empty;
203         unsigned total;
204
205         if (!bset)
206                 return NULL;
207
208         total = isl_basic_set_total_dim(bset);
209         id = isl_mat_identity(bset->ctx, 1 + total);
210         empty = isl_basic_set_empty(isl_dim_copy(bset->dim));
211
212         return isl_morph_alloc(empty, isl_basic_set_copy(empty),
213                 id, isl_mat_copy(id));
214 }
215
216 /* Given a matrix that maps a (possibly) parametric domain to
217  * a parametric domain, add in rows that map the "nparam" parameters onto
218  * themselves.
219  */
220 static __isl_give isl_mat *insert_parameter_rows(__isl_take isl_mat *mat,
221         unsigned nparam)
222 {
223         int i;
224
225         if (nparam == 0)
226                 return mat;
227         if (!mat)
228                 return NULL;
229
230         mat = isl_mat_insert_rows(mat, 1, nparam);
231         if (!mat)
232                 return NULL;
233
234         for (i = 0; i < nparam; ++i) {
235                 isl_seq_clr(mat->row[1 + i], mat->n_col);
236                 isl_int_set(mat->row[1 + i][1 + i], mat->row[0][0]);
237         }
238
239         return mat;
240 }
241
242 /* Construct a basic set described by the "n" equalities of "bset" starting
243  * at "first".
244  */
245 static __isl_give isl_basic_set *copy_equalities(__isl_keep isl_basic_set *bset,
246         unsigned first, unsigned n)
247 {
248         int i, k;
249         isl_basic_set *eq;
250         unsigned total;
251
252         isl_assert(bset->ctx, bset->n_div == 0, return NULL);
253
254         total = isl_basic_set_total_dim(bset);
255         eq = isl_basic_set_alloc_dim(isl_dim_copy(bset->dim), 0, n, 0);
256         if (!eq)
257                 return NULL;
258         for (i = 0; i < n; ++i) {
259                 k = isl_basic_set_alloc_equality(eq);
260                 if (k < 0)
261                         goto error;
262                 isl_seq_cpy(eq->eq[k], bset->eq[first + k], 1 + total);
263         }
264
265         return eq;
266 error:
267         isl_basic_set_free(eq);
268         return NULL;
269 }
270
271 /* Given a basic set, exploit the equalties in the a basic set to construct
272  * a morphishm that maps the basic set to a lower-dimensional space.
273  * Specifically, the morphism reduces the number of dimensions of type "type".
274  *
275  * This function is a slight generalization of isl_mat_variable_compression
276  * in that it allows the input to be parametric and that it allows for the
277  * compression of either parameters or set variables.
278  *
279  * We first select the equalities of interest, that is those that involve
280  * variables of type "type" and no later variables.
281  * Denote those equalities as
282  *
283  *              -C(p) + M x = 0
284  *
285  * where C(p) depends on the parameters if type == isl_dim_set and
286  * is a constant if type == isl_dim_param.
287  *
288  * First compute the (left) Hermite normal form of M,
289  *
290  *              M [U1 U2] = M U = H = [H1 0]
291  * or
292  *                            M = H Q = [H1 0] [Q1]
293  *                                             [Q2]
294  *
295  * with U, Q unimodular, Q = U^{-1} (and H lower triangular).
296  * Define the transformed variables as
297  *
298  *              x = [U1 U2] [ x1' ] = [U1 U2] [Q1] x
299  *                          [ x2' ]           [Q2]
300  *
301  * The equalities then become
302  *
303  *              -C(p) + H1 x1' = 0   or   x1' = H1^{-1} C(p) = C'(p)
304  *
305  * If the denominator of the constant term does not divide the
306  * the common denominator of the parametric terms, then every
307  * integer point is mapped to a non-integer point and then the original set has no
308  * integer solutions (since the x' are a unimodular transformation
309  * of the x).  In this case, an empty morphism is returned.
310  * Otherwise, the transformation is given by
311  *
312  *              x = U1 H1^{-1} C(p) + U2 x2'
313  *
314  * The inverse transformation is simply
315  *
316  *              x2' = Q2 x
317  *
318  * Both matrices are extended to map the full original space to the full
319  * compressed space.
320  */
321 __isl_give isl_morph *isl_basic_set_variable_compression(
322         __isl_keep isl_basic_set *bset, enum isl_dim_type type)
323 {
324         unsigned otype;
325         unsigned ntype;
326         unsigned orest;
327         unsigned nrest;
328         unsigned total;
329         int f_eq, n_eq;
330         isl_dim *dim;
331         isl_mat *H, *U, *Q, *C = NULL, *H1, *U1, *U2;
332         isl_basic_set *dom, *ran;
333
334         if (!bset)
335                 return NULL;
336
337         if (isl_basic_set_fast_is_empty(bset))
338                 return isl_morph_empty(bset);
339
340         isl_assert(bset->ctx, bset->n_div == 0, return NULL);
341
342         otype = 1 + isl_dim_offset(bset->dim, type);
343         ntype = isl_basic_set_dim(bset, type);
344         orest = otype + ntype;
345         nrest = isl_basic_set_total_dim(bset) - (orest - 1);
346
347         for (f_eq = 0; f_eq < bset->n_eq; ++f_eq)
348                 if (isl_seq_first_non_zero(bset->eq[f_eq] + orest, nrest) == -1)
349                         break;
350         for (n_eq = 0; f_eq + n_eq < bset->n_eq; ++n_eq)
351                 if (isl_seq_first_non_zero(bset->eq[f_eq + n_eq] + otype, ntype) == -1)
352                         break;
353         if (n_eq == 0)
354                 return isl_morph_identity(bset);
355
356         H = isl_mat_sub_alloc(bset->ctx, bset->eq, f_eq, n_eq, otype, ntype);
357         H = isl_mat_left_hermite(H, 0, &U, &Q);
358         if (!H || !U || !Q)
359                 goto error;
360         Q = isl_mat_drop_rows(Q, 0, n_eq);
361         Q = isl_mat_diagonal(isl_mat_identity(bset->ctx, otype), Q);
362         Q = isl_mat_diagonal(Q, isl_mat_identity(bset->ctx, nrest));
363         C = isl_mat_alloc(bset->ctx, 1 + n_eq, otype);
364         if (!C)
365                 goto error;
366         isl_int_set_si(C->row[0][0], 1);
367         isl_seq_clr(C->row[0] + 1, otype - 1);
368         isl_mat_sub_neg(C->ctx, C->row + 1, bset->eq + f_eq, n_eq, 0, 0, otype);
369         H1 = isl_mat_sub_alloc(H->ctx, H->row, 0, H->n_row, 0, H->n_row);
370         H1 = isl_mat_lin_to_aff(H1);
371         C = isl_mat_inverse_product(H1, C);
372         if (!C)
373                 goto error;
374         isl_mat_free(H);
375
376         if (!isl_int_is_one(C->row[0][0])) {
377                 int i;
378                 isl_int g;
379
380                 isl_int_init(g);
381                 for (i = 0; i < n_eq; ++i) {
382                         isl_seq_gcd(C->row[1 + i] + 1, otype - 1, &g);
383                         isl_int_gcd(g, g, C->row[0][0]);
384                         if (!isl_int_is_divisible_by(C->row[1 + i][0], g))
385                                 break;
386                 }
387                 isl_int_clear(g);
388
389                 if (i < n_eq) {
390                         isl_mat_free(C);
391                         isl_mat_free(U);
392                         isl_mat_free(Q);
393                         return isl_morph_empty(bset);
394                 }
395
396                 C = isl_mat_normalize(C);
397         }
398
399         U1 = isl_mat_sub_alloc(U->ctx, U->row, 0, U->n_row, 0, n_eq);
400         U1 = isl_mat_lin_to_aff(U1);
401         U2 = isl_mat_sub_alloc(U->ctx, U->row, 0, U->n_row, n_eq, U->n_row - n_eq);
402         U2 = isl_mat_lin_to_aff(U2);
403         isl_mat_free(U);
404
405         C = isl_mat_product(U1, C);
406         C = isl_mat_aff_direct_sum(C, U2);
407         C = insert_parameter_rows(C, otype - 1);
408         C = isl_mat_diagonal(C, isl_mat_identity(bset->ctx, nrest));
409
410         dim = isl_dim_copy(bset->dim);
411         dim = isl_dim_drop(dim, type, 0, ntype);
412         dim = isl_dim_add(dim, type, ntype - n_eq);
413         ran = isl_basic_set_universe(dim);
414         dom = copy_equalities(bset, f_eq, n_eq);
415
416         return isl_morph_alloc(dom, ran, Q, C);
417 error:
418         isl_mat_free(C);
419         isl_mat_free(H);
420         isl_mat_free(U);
421         isl_mat_free(Q);
422         return NULL;
423 }
424
425 /* Construct a parameter compression for "bset".
426  * We basically just call isl_mat_parameter_compression with the right input
427  * and then extend the resulting matrix to include the variables.
428  *
429  * Let the equalities be given as
430  *
431  *      B(p) + A x = 0
432  *
433  * and let [H 0] be the Hermite Normal Form of A, then
434  *
435  *      H^-1 B(p)
436  *
437  * needs to be integer, so we impose that each row is divisible by
438  * the denominator.
439  */
440 __isl_give isl_morph *isl_basic_set_parameter_compression(
441         __isl_keep isl_basic_set *bset)
442 {
443         unsigned nparam;
444         unsigned nvar;
445         int n_eq;
446         isl_mat *H, *B;
447         isl_vec *d;
448         isl_mat *map, *inv;
449         isl_basic_set *dom, *ran;
450
451         if (!bset)
452                 return NULL;
453
454         if (isl_basic_set_fast_is_empty(bset))
455                 return isl_morph_empty(bset);
456         if (bset->n_eq == 0)
457                 return isl_morph_identity(bset);
458
459         isl_assert(bset->ctx, bset->n_div == 0, return NULL);
460
461         n_eq = bset->n_eq;
462         nparam = isl_basic_set_dim(bset, isl_dim_param);
463         nvar = isl_basic_set_dim(bset, isl_dim_set);
464
465         isl_assert(bset->ctx, n_eq <= nvar, return NULL);
466
467         d = isl_vec_alloc(bset->ctx, n_eq);
468         B = isl_mat_sub_alloc(bset->ctx, bset->eq, 0, n_eq, 0, 1 + nparam);
469         H = isl_mat_sub_alloc(bset->ctx, bset->eq, 0, n_eq, 1 + nparam, nvar);
470         H = isl_mat_left_hermite(H, 0, NULL, NULL);
471         H = isl_mat_drop_cols(H, n_eq, nvar - n_eq);
472         H = isl_mat_lin_to_aff(H);
473         H = isl_mat_right_inverse(H);
474         if (!H || !d)
475                 goto error;
476         isl_seq_set(d->el, H->row[0][0], d->size);
477         H = isl_mat_drop_rows(H, 0, 1);
478         H = isl_mat_drop_cols(H, 0, 1);
479         B = isl_mat_product(H, B);
480         inv = isl_mat_parameter_compression(B, d);
481         inv = isl_mat_diagonal(inv, isl_mat_identity(bset->ctx, nvar));
482         map = isl_mat_right_inverse(isl_mat_copy(inv));
483
484         dom = isl_basic_set_universe(isl_dim_copy(bset->dim));
485         ran = isl_basic_set_universe(isl_dim_copy(bset->dim));
486
487         return isl_morph_alloc(dom, ran, map, inv);
488 error:
489         isl_mat_free(H);
490         isl_mat_free(B);
491         isl_vec_free(d);
492         return NULL;
493 }
494
495 /* Add stride constraints to "bset" based on the inverse mapping
496  * that was plugged in.  In particular, if morph maps x' to x,
497  * the the constraints of the original input
498  *
499  *      A x' + b >= 0
500  *
501  * have been rewritten to
502  *
503  *      A inv x + b >= 0
504  *
505  * However, this substitution may loose information on the integrality of x',
506  * so we need to impose that
507  *
508  *      inv x
509  *
510  * is integral.  If inv = B/d, this means that we need to impose that
511  *
512  *      B x = 0         mod d
513  *
514  * or
515  *
516  *      exists alpha in Z^m: B x = d alpha
517  *
518  */
519 static __isl_give isl_basic_set *add_strides(__isl_take isl_basic_set *bset,
520         __isl_keep isl_morph *morph)
521 {
522         int i, div, k;
523         isl_int gcd;
524
525         if (isl_int_is_one(morph->inv->row[0][0]))
526                 return bset;
527
528         isl_int_init(gcd);
529
530         for (i = 0; 1 + i < morph->inv->n_row; ++i) {
531                 isl_seq_gcd(morph->inv->row[1 + i], morph->inv->n_col, &gcd);
532                 if (isl_int_is_divisible_by(gcd, morph->inv->row[0][0]))
533                         continue;
534                 div = isl_basic_set_alloc_div(bset);
535                 if (div < 0)
536                         goto error;
537                 k = isl_basic_set_alloc_equality(bset);
538                 if (k < 0)
539                         goto error;
540                 isl_seq_cpy(bset->eq[k], morph->inv->row[1 + i],
541                             morph->inv->n_col);
542                 isl_seq_clr(bset->eq[k] + morph->inv->n_col, bset->n_div);
543                 isl_int_set(bset->eq[k][morph->inv->n_col + div],
544                             morph->inv->row[0][0]);
545         }
546
547         isl_int_clear(gcd);
548
549         return bset;
550 error:
551         isl_int_clear(gcd);
552         isl_basic_set_free(bset);
553         return NULL;
554 }
555
556 /* Apply the morphism to the basic set.
557  * We basically just compute the preimage of "bset" under the inverse mapping
558  * in morph, add in stride constraints and intersect with the range
559  * of the morphism.
560  */
561 __isl_give isl_basic_set *isl_morph_basic_set(__isl_take isl_morph *morph,
562         __isl_take isl_basic_set *bset)
563 {
564         isl_basic_set *res = NULL;
565         isl_mat *mat = NULL;
566         int i, k;
567         int max_stride;
568
569         if (!morph || !bset)
570                 goto error;
571
572         isl_assert(bset->ctx, isl_dim_equal(bset->dim, morph->dom->dim),
573                     goto error);
574
575         max_stride = morph->inv->n_row - 1;
576         if (isl_int_is_one(morph->inv->row[0][0]))
577                 max_stride = 0;
578         res = isl_basic_set_alloc_dim(isl_dim_copy(morph->ran->dim),
579                 bset->n_div + max_stride, bset->n_eq + max_stride, bset->n_ineq);
580
581         for (i = 0; i < bset->n_div; ++i)
582                 if (isl_basic_set_alloc_div(res) < 0)
583                         goto error;
584
585         mat = isl_mat_sub_alloc(bset->ctx, bset->eq, 0, bset->n_eq,
586                                         0, morph->inv->n_row);
587         mat = isl_mat_product(mat, isl_mat_copy(morph->inv));
588         if (!mat)
589                 goto error;
590         for (i = 0; i < bset->n_eq; ++i) {
591                 k = isl_basic_set_alloc_equality(res);
592                 if (k < 0)
593                         goto error;
594                 isl_seq_cpy(res->eq[k], mat->row[i], mat->n_col);
595                 isl_seq_scale(res->eq[k] + mat->n_col, bset->eq[i] + mat->n_col,
596                                 morph->inv->row[0][0], bset->n_div);
597         }
598         isl_mat_free(mat);
599
600         mat = isl_mat_sub_alloc(bset->ctx, bset->ineq, 0, bset->n_ineq,
601                                         0, morph->inv->n_row);
602         mat = isl_mat_product(mat, isl_mat_copy(morph->inv));
603         if (!mat)
604                 goto error;
605         for (i = 0; i < bset->n_ineq; ++i) {
606                 k = isl_basic_set_alloc_inequality(res);
607                 if (k < 0)
608                         goto error;
609                 isl_seq_cpy(res->ineq[k], mat->row[i], mat->n_col);
610                 isl_seq_scale(res->ineq[k] + mat->n_col,
611                                 bset->ineq[i] + mat->n_col,
612                                 morph->inv->row[0][0], bset->n_div);
613         }
614         isl_mat_free(mat);
615
616         mat = isl_mat_sub_alloc(bset->ctx, bset->div, 0, bset->n_div,
617                                         1, morph->inv->n_row);
618         mat = isl_mat_product(mat, isl_mat_copy(morph->inv));
619         if (!mat)
620                 goto error;
621         for (i = 0; i < bset->n_div; ++i) {
622                 isl_int_mul(res->div[i][0],
623                                 morph->inv->row[0][0], bset->div[i][0]);
624                 isl_seq_cpy(res->div[i] + 1, mat->row[i], mat->n_col);
625                 isl_seq_scale(res->div[i] + 1 + mat->n_col,
626                                 bset->div[i] + 1 + mat->n_col,
627                                 morph->inv->row[0][0], bset->n_div);
628         }
629         isl_mat_free(mat);
630
631         res = add_strides(res, morph);
632
633         res = isl_basic_set_simplify(res);
634         res = isl_basic_set_finalize(res);
635
636         res = isl_basic_set_intersect(res, isl_basic_set_copy(morph->ran));
637
638         isl_morph_free(morph);
639         isl_basic_set_free(bset);
640         return res;
641 error:
642         isl_mat_free(mat);
643         isl_morph_free(morph);
644         isl_basic_set_free(bset);
645         isl_basic_set_free(res);
646         return NULL;
647 }
648
649 /* Apply the morphism to the set.
650  */
651 __isl_give isl_set *isl_morph_set(__isl_take isl_morph *morph,
652         __isl_take isl_set *set)
653 {
654         int i;
655
656         if (!morph || !set)
657                 goto error;
658
659         isl_assert(set->ctx, isl_dim_equal(set->dim, morph->dom->dim), goto error);
660
661         set = isl_set_cow(set);
662         if (!set)
663                 goto error;
664
665         isl_dim_free(set->dim);
666         set->dim = isl_dim_copy(morph->ran->dim);
667         if (!set->dim)
668                 goto error;
669
670         for (i = 0; i < set->n; ++i) {
671                 set->p[i] = isl_morph_basic_set(isl_morph_copy(morph), set->p[i]);
672                 if (!set->p[i])
673                         goto error;
674         }
675
676         isl_morph_free(morph);
677
678         ISL_F_CLR(set, ISL_SET_NORMALIZED);
679
680         return set;
681 error:
682         isl_set_free(set);
683         isl_morph_free(morph);
684         return NULL;
685 }
686
687 /* Construct a morphism that first does morph2 and then morph1.
688  */
689 __isl_give isl_morph *isl_morph_compose(__isl_take isl_morph *morph1,
690         __isl_take isl_morph *morph2)
691 {
692         isl_mat *map, *inv;
693         isl_basic_set *dom, *ran;
694
695         if (!morph1 || !morph2)
696                 goto error;
697
698         map = isl_mat_product(isl_mat_copy(morph1->map), isl_mat_copy(morph2->map));
699         inv = isl_mat_product(isl_mat_copy(morph2->inv), isl_mat_copy(morph1->inv));
700         dom = isl_morph_basic_set(isl_morph_inverse(isl_morph_copy(morph2)),
701                                   isl_basic_set_copy(morph1->dom));
702         dom = isl_basic_set_intersect(dom, isl_basic_set_copy(morph2->dom));
703         ran = isl_morph_basic_set(isl_morph_copy(morph1),
704                                   isl_basic_set_copy(morph2->ran));
705         ran = isl_basic_set_intersect(ran, isl_basic_set_copy(morph1->ran));
706
707         isl_morph_free(morph1);
708         isl_morph_free(morph2);
709
710         return isl_morph_alloc(dom, ran, map, inv);
711 error:
712         isl_morph_free(morph1);
713         isl_morph_free(morph2);
714         return NULL;
715 }
716
717 __isl_give isl_morph *isl_morph_inverse(__isl_take isl_morph *morph)
718 {
719         isl_basic_set *bset;
720         isl_mat *mat;
721
722         morph = isl_morph_cow(morph);
723         if (!morph)
724                 return NULL;
725
726         bset = morph->dom;
727         morph->dom = morph->ran;
728         morph->ran = bset;
729
730         mat = morph->map;
731         morph->map = morph->inv;
732         morph->inv = mat;
733
734         return morph;
735 }
736
737 __isl_give isl_morph *isl_basic_set_full_compression(
738         __isl_keep isl_basic_set *bset)
739 {
740         isl_morph *morph, *morph2;
741
742         bset = isl_basic_set_copy(bset);
743
744         morph = isl_basic_set_variable_compression(bset, isl_dim_param);
745         bset = isl_morph_basic_set(isl_morph_copy(morph), bset);
746
747         morph2 = isl_basic_set_parameter_compression(bset);
748         bset = isl_morph_basic_set(isl_morph_copy(morph2), bset);
749
750         morph = isl_morph_compose(morph2, morph);
751
752         morph2 = isl_basic_set_variable_compression(bset, isl_dim_set);
753         isl_basic_set_free(bset);
754
755         morph = isl_morph_compose(morph2, morph);
756
757         return morph;
758 }