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