add isl_morph_get_ctx
[isl.git] / isl_morph.c
blob43922c95f6c78b83c9008d510ca8f8dafc944a52
1 /*
2 * Copyright 2010 INRIA Saclay
4 * Use of this software is governed by the MIT license
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 */
11 #include <isl_map_private.h>
12 #include <isl_morph.h>
13 #include <isl_seq.h>
14 #include <isl_mat_private.h>
15 #include <isl_space_private.h>
16 #include <isl_equalities.h>
18 isl_ctx *isl_morph_get_ctx(__isl_keep isl_morph *morph)
20 if (!morph)
21 return NULL;
22 return isl_basic_set_get_ctx(morph->dom);
25 __isl_give isl_morph *isl_morph_alloc(
26 __isl_take isl_basic_set *dom, __isl_take isl_basic_set *ran,
27 __isl_take isl_mat *map, __isl_take isl_mat *inv)
29 isl_morph *morph;
31 if (!dom || !ran || !map || !inv)
32 goto error;
34 morph = isl_alloc_type(dom->ctx, struct isl_morph);
35 if (!morph)
36 goto error;
38 morph->ref = 1;
39 morph->dom = dom;
40 morph->ran = ran;
41 morph->map = map;
42 morph->inv = inv;
44 return morph;
45 error:
46 isl_basic_set_free(dom);
47 isl_basic_set_free(ran);
48 isl_mat_free(map);
49 isl_mat_free(inv);
50 return NULL;
53 __isl_give isl_morph *isl_morph_copy(__isl_keep isl_morph *morph)
55 if (!morph)
56 return NULL;
58 morph->ref++;
59 return morph;
62 __isl_give isl_morph *isl_morph_dup(__isl_keep isl_morph *morph)
64 if (!morph)
65 return NULL;
67 return isl_morph_alloc(isl_basic_set_copy(morph->dom),
68 isl_basic_set_copy(morph->ran),
69 isl_mat_copy(morph->map), isl_mat_copy(morph->inv));
72 __isl_give isl_morph *isl_morph_cow(__isl_take isl_morph *morph)
74 if (!morph)
75 return NULL;
77 if (morph->ref == 1)
78 return morph;
79 morph->ref--;
80 return isl_morph_dup(morph);
83 void isl_morph_free(__isl_take isl_morph *morph)
85 if (!morph)
86 return;
88 if (--morph->ref > 0)
89 return;
91 isl_basic_set_free(morph->dom);
92 isl_basic_set_free(morph->ran);
93 isl_mat_free(morph->map);
94 isl_mat_free(morph->inv);
95 free(morph);
98 __isl_give isl_space *isl_morph_get_ran_space(__isl_keep isl_morph *morph)
100 if (!morph)
101 return NULL;
103 return isl_space_copy(morph->ran->dim);
106 unsigned isl_morph_dom_dim(__isl_keep isl_morph *morph, enum isl_dim_type type)
108 if (!morph)
109 return 0;
111 return isl_basic_set_dim(morph->dom, type);
114 unsigned isl_morph_ran_dim(__isl_keep isl_morph *morph, enum isl_dim_type type)
116 if (!morph)
117 return 0;
119 return isl_basic_set_dim(morph->ran, type);
122 __isl_give isl_morph *isl_morph_remove_dom_dims(__isl_take isl_morph *morph,
123 enum isl_dim_type type, unsigned first, unsigned n)
125 unsigned dom_offset;
127 if (n == 0)
128 return morph;
130 morph = isl_morph_cow(morph);
131 if (!morph)
132 return NULL;
134 dom_offset = 1 + isl_space_offset(morph->dom->dim, type);
136 morph->dom = isl_basic_set_remove_dims(morph->dom, type, first, n);
138 morph->map = isl_mat_drop_cols(morph->map, dom_offset + first, n);
140 morph->inv = isl_mat_drop_rows(morph->inv, dom_offset + first, n);
142 if (morph->dom && morph->ran && morph->map && morph->inv)
143 return morph;
145 isl_morph_free(morph);
146 return NULL;
149 __isl_give isl_morph *isl_morph_remove_ran_dims(__isl_take isl_morph *morph,
150 enum isl_dim_type type, unsigned first, unsigned n)
152 unsigned ran_offset;
154 if (n == 0)
155 return morph;
157 morph = isl_morph_cow(morph);
158 if (!morph)
159 return NULL;
161 ran_offset = 1 + isl_space_offset(morph->ran->dim, type);
163 morph->ran = isl_basic_set_remove_dims(morph->ran, type, first, n);
165 morph->map = isl_mat_drop_rows(morph->map, ran_offset + first, n);
167 morph->inv = isl_mat_drop_cols(morph->inv, ran_offset + first, n);
169 if (morph->dom && morph->ran && morph->map && morph->inv)
170 return morph;
172 isl_morph_free(morph);
173 return NULL;
176 /* Project domain of morph onto its parameter domain.
178 __isl_give isl_morph *isl_morph_dom_params(__isl_take isl_morph *morph)
180 unsigned n;
182 morph = isl_morph_cow(morph);
183 if (!morph)
184 return NULL;
185 n = isl_basic_set_dim(morph->dom, isl_dim_set);
186 morph = isl_morph_remove_dom_dims(morph, isl_dim_set, 0, n);
187 if (!morph)
188 return NULL;
189 morph->dom = isl_basic_set_params(morph->dom);
190 if (morph->dom)
191 return morph;
193 isl_morph_free(morph);
194 return NULL;
197 /* Project range of morph onto its parameter domain.
199 __isl_give isl_morph *isl_morph_ran_params(__isl_take isl_morph *morph)
201 unsigned n;
203 morph = isl_morph_cow(morph);
204 if (!morph)
205 return NULL;
206 n = isl_basic_set_dim(morph->ran, isl_dim_set);
207 morph = isl_morph_remove_ran_dims(morph, isl_dim_set, 0, n);
208 if (!morph)
209 return NULL;
210 morph->ran = isl_basic_set_params(morph->ran);
211 if (morph->ran)
212 return morph;
214 isl_morph_free(morph);
215 return NULL;
218 void isl_morph_print_internal(__isl_take isl_morph *morph, FILE *out)
220 if (!morph)
221 return;
223 isl_basic_set_print(morph->dom, out, 0, "", "", ISL_FORMAT_ISL);
224 isl_basic_set_print(morph->ran, out, 0, "", "", ISL_FORMAT_ISL);
225 isl_mat_print_internal(morph->map, out, 4);
226 isl_mat_print_internal(morph->inv, out, 4);
229 void isl_morph_dump(__isl_take isl_morph *morph)
231 isl_morph_print_internal(morph, stderr);
234 __isl_give isl_morph *isl_morph_identity(__isl_keep isl_basic_set *bset)
236 isl_mat *id;
237 isl_basic_set *universe;
238 unsigned total;
240 if (!bset)
241 return NULL;
243 total = isl_basic_set_total_dim(bset);
244 id = isl_mat_identity(bset->ctx, 1 + total);
245 universe = isl_basic_set_universe(isl_space_copy(bset->dim));
247 return isl_morph_alloc(universe, isl_basic_set_copy(universe),
248 id, isl_mat_copy(id));
251 /* Create a(n identity) morphism between empty sets of the same dimension
252 * a "bset".
254 __isl_give isl_morph *isl_morph_empty(__isl_keep isl_basic_set *bset)
256 isl_mat *id;
257 isl_basic_set *empty;
258 unsigned total;
260 if (!bset)
261 return NULL;
263 total = isl_basic_set_total_dim(bset);
264 id = isl_mat_identity(bset->ctx, 1 + total);
265 empty = isl_basic_set_empty(isl_space_copy(bset->dim));
267 return isl_morph_alloc(empty, isl_basic_set_copy(empty),
268 id, isl_mat_copy(id));
271 /* Given a matrix that maps a (possibly) parametric domain to
272 * a parametric domain, add in rows that map the "nparam" parameters onto
273 * themselves.
275 static __isl_give isl_mat *insert_parameter_rows(__isl_take isl_mat *mat,
276 unsigned nparam)
278 int i;
280 if (nparam == 0)
281 return mat;
282 if (!mat)
283 return NULL;
285 mat = isl_mat_insert_rows(mat, 1, nparam);
286 if (!mat)
287 return NULL;
289 for (i = 0; i < nparam; ++i) {
290 isl_seq_clr(mat->row[1 + i], mat->n_col);
291 isl_int_set(mat->row[1 + i][1 + i], mat->row[0][0]);
294 return mat;
297 /* Construct a basic set described by the "n" equalities of "bset" starting
298 * at "first".
300 static __isl_give isl_basic_set *copy_equalities(__isl_keep isl_basic_set *bset,
301 unsigned first, unsigned n)
303 int i, k;
304 isl_basic_set *eq;
305 unsigned total;
307 isl_assert(bset->ctx, bset->n_div == 0, return NULL);
309 total = isl_basic_set_total_dim(bset);
310 eq = isl_basic_set_alloc_space(isl_space_copy(bset->dim), 0, n, 0);
311 if (!eq)
312 return NULL;
313 for (i = 0; i < n; ++i) {
314 k = isl_basic_set_alloc_equality(eq);
315 if (k < 0)
316 goto error;
317 isl_seq_cpy(eq->eq[k], bset->eq[first + k], 1 + total);
320 return eq;
321 error:
322 isl_basic_set_free(eq);
323 return NULL;
326 /* Given a basic set, exploit the equalties in the basic set to construct
327 * a morphishm that maps the basic set to a lower-dimensional space.
328 * Specifically, the morphism reduces the number of dimensions of type "type".
330 * This function is a slight generalization of isl_mat_variable_compression
331 * in that it allows the input to be parametric and that it allows for the
332 * compression of either parameters or set variables.
334 * We first select the equalities of interest, that is those that involve
335 * variables of type "type" and no later variables.
336 * Denote those equalities as
338 * -C(p) + M x = 0
340 * where C(p) depends on the parameters if type == isl_dim_set and
341 * is a constant if type == isl_dim_param.
343 * First compute the (left) Hermite normal form of M,
345 * M [U1 U2] = M U = H = [H1 0]
346 * or
347 * M = H Q = [H1 0] [Q1]
348 * [Q2]
350 * with U, Q unimodular, Q = U^{-1} (and H lower triangular).
351 * Define the transformed variables as
353 * x = [U1 U2] [ x1' ] = [U1 U2] [Q1] x
354 * [ x2' ] [Q2]
356 * The equalities then become
358 * -C(p) + H1 x1' = 0 or x1' = H1^{-1} C(p) = C'(p)
360 * If the denominator of the constant term does not divide the
361 * the common denominator of the parametric terms, then every
362 * integer point is mapped to a non-integer point and then the original set has no
363 * integer solutions (since the x' are a unimodular transformation
364 * of the x). In this case, an empty morphism is returned.
365 * Otherwise, the transformation is given by
367 * x = U1 H1^{-1} C(p) + U2 x2'
369 * The inverse transformation is simply
371 * x2' = Q2 x
373 * Both matrices are extended to map the full original space to the full
374 * compressed space.
376 __isl_give isl_morph *isl_basic_set_variable_compression(
377 __isl_keep isl_basic_set *bset, enum isl_dim_type type)
379 unsigned otype;
380 unsigned ntype;
381 unsigned orest;
382 unsigned nrest;
383 int f_eq, n_eq;
384 isl_space *dim;
385 isl_mat *H, *U, *Q, *C = NULL, *H1, *U1, *U2;
386 isl_basic_set *dom, *ran;
388 if (!bset)
389 return NULL;
391 if (isl_basic_set_plain_is_empty(bset))
392 return isl_morph_empty(bset);
394 isl_assert(bset->ctx, bset->n_div == 0, return NULL);
396 otype = 1 + isl_space_offset(bset->dim, type);
397 ntype = isl_basic_set_dim(bset, type);
398 orest = otype + ntype;
399 nrest = isl_basic_set_total_dim(bset) - (orest - 1);
401 for (f_eq = 0; f_eq < bset->n_eq; ++f_eq)
402 if (isl_seq_first_non_zero(bset->eq[f_eq] + orest, nrest) == -1)
403 break;
404 for (n_eq = 0; f_eq + n_eq < bset->n_eq; ++n_eq)
405 if (isl_seq_first_non_zero(bset->eq[f_eq + n_eq] + otype, ntype) == -1)
406 break;
407 if (n_eq == 0)
408 return isl_morph_identity(bset);
410 H = isl_mat_sub_alloc6(bset->ctx, bset->eq, f_eq, n_eq, otype, ntype);
411 H = isl_mat_left_hermite(H, 0, &U, &Q);
412 if (!H || !U || !Q)
413 goto error;
414 Q = isl_mat_drop_rows(Q, 0, n_eq);
415 Q = isl_mat_diagonal(isl_mat_identity(bset->ctx, otype), Q);
416 Q = isl_mat_diagonal(Q, isl_mat_identity(bset->ctx, nrest));
417 C = isl_mat_alloc(bset->ctx, 1 + n_eq, otype);
418 if (!C)
419 goto error;
420 isl_int_set_si(C->row[0][0], 1);
421 isl_seq_clr(C->row[0] + 1, otype - 1);
422 isl_mat_sub_neg(C->ctx, C->row + 1, bset->eq + f_eq, n_eq, 0, 0, otype);
423 H1 = isl_mat_sub_alloc(H, 0, H->n_row, 0, H->n_row);
424 H1 = isl_mat_lin_to_aff(H1);
425 C = isl_mat_inverse_product(H1, C);
426 if (!C)
427 goto error;
428 isl_mat_free(H);
430 if (!isl_int_is_one(C->row[0][0])) {
431 int i;
432 isl_int g;
434 isl_int_init(g);
435 for (i = 0; i < n_eq; ++i) {
436 isl_seq_gcd(C->row[1 + i] + 1, otype - 1, &g);
437 isl_int_gcd(g, g, C->row[0][0]);
438 if (!isl_int_is_divisible_by(C->row[1 + i][0], g))
439 break;
441 isl_int_clear(g);
443 if (i < n_eq) {
444 isl_mat_free(C);
445 isl_mat_free(U);
446 isl_mat_free(Q);
447 return isl_morph_empty(bset);
450 C = isl_mat_normalize(C);
453 U1 = isl_mat_sub_alloc(U, 0, U->n_row, 0, n_eq);
454 U1 = isl_mat_lin_to_aff(U1);
455 U2 = isl_mat_sub_alloc(U, 0, U->n_row, n_eq, U->n_row - n_eq);
456 U2 = isl_mat_lin_to_aff(U2);
457 isl_mat_free(U);
459 C = isl_mat_product(U1, C);
460 C = isl_mat_aff_direct_sum(C, U2);
461 C = insert_parameter_rows(C, otype - 1);
462 C = isl_mat_diagonal(C, isl_mat_identity(bset->ctx, nrest));
464 dim = isl_space_copy(bset->dim);
465 dim = isl_space_drop_dims(dim, type, 0, ntype);
466 dim = isl_space_add_dims(dim, type, ntype - n_eq);
467 ran = isl_basic_set_universe(dim);
468 dom = copy_equalities(bset, f_eq, n_eq);
470 return isl_morph_alloc(dom, ran, Q, C);
471 error:
472 isl_mat_free(C);
473 isl_mat_free(H);
474 isl_mat_free(U);
475 isl_mat_free(Q);
476 return NULL;
479 /* Construct a parameter compression for "bset".
480 * We basically just call isl_mat_parameter_compression with the right input
481 * and then extend the resulting matrix to include the variables.
483 * The implementation assumes that "bset" does not have any equalities
484 * that only involve the parameters and that isl_basic_set_gauss has
485 * been applied to "bset".
487 * Let the equalities be given as
489 * B(p) + A x = 0.
491 * We use isl_mat_parameter_compression_ext to compute the compression
493 * p = T p'.
495 __isl_give isl_morph *isl_basic_set_parameter_compression(
496 __isl_keep isl_basic_set *bset)
498 unsigned nparam;
499 unsigned nvar;
500 unsigned n_div;
501 int n_eq;
502 isl_mat *H, *B;
503 isl_mat *map, *inv;
504 isl_basic_set *dom, *ran;
506 if (!bset)
507 return NULL;
509 if (isl_basic_set_plain_is_empty(bset))
510 return isl_morph_empty(bset);
511 if (bset->n_eq == 0)
512 return isl_morph_identity(bset);
514 n_eq = bset->n_eq;
515 nparam = isl_basic_set_dim(bset, isl_dim_param);
516 nvar = isl_basic_set_dim(bset, isl_dim_set);
517 n_div = isl_basic_set_dim(bset, isl_dim_div);
519 if (isl_seq_first_non_zero(bset->eq[bset->n_eq - 1] + 1 + nparam,
520 nvar + n_div) == -1)
521 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
522 "input not allowed to have parameter equalities",
523 return NULL);
524 if (n_eq > nvar + n_div)
525 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
526 "input not gaussed", return NULL);
528 B = isl_mat_sub_alloc6(bset->ctx, bset->eq, 0, n_eq, 0, 1 + nparam);
529 H = isl_mat_sub_alloc6(bset->ctx, bset->eq,
530 0, n_eq, 1 + nparam, nvar + n_div);
531 inv = isl_mat_parameter_compression_ext(B, H);
532 inv = isl_mat_diagonal(inv, isl_mat_identity(bset->ctx, nvar));
533 map = isl_mat_right_inverse(isl_mat_copy(inv));
535 dom = isl_basic_set_universe(isl_space_copy(bset->dim));
536 ran = isl_basic_set_universe(isl_space_copy(bset->dim));
538 return isl_morph_alloc(dom, ran, map, inv);
541 /* Add stride constraints to "bset" based on the inverse mapping
542 * that was plugged in. In particular, if morph maps x' to x,
543 * the the constraints of the original input
545 * A x' + b >= 0
547 * have been rewritten to
549 * A inv x + b >= 0
551 * However, this substitution may loose information on the integrality of x',
552 * so we need to impose that
554 * inv x
556 * is integral. If inv = B/d, this means that we need to impose that
558 * B x = 0 mod d
560 * or
562 * exists alpha in Z^m: B x = d alpha
564 * This function is similar to add_strides in isl_affine_hull.c
566 static __isl_give isl_basic_set *add_strides(__isl_take isl_basic_set *bset,
567 __isl_keep isl_morph *morph)
569 int i, div, k;
570 isl_int gcd;
572 if (isl_int_is_one(morph->inv->row[0][0]))
573 return bset;
575 isl_int_init(gcd);
577 for (i = 0; 1 + i < morph->inv->n_row; ++i) {
578 isl_seq_gcd(morph->inv->row[1 + i], morph->inv->n_col, &gcd);
579 if (isl_int_is_divisible_by(gcd, morph->inv->row[0][0]))
580 continue;
581 div = isl_basic_set_alloc_div(bset);
582 if (div < 0)
583 goto error;
584 isl_int_set_si(bset->div[div][0], 0);
585 k = isl_basic_set_alloc_equality(bset);
586 if (k < 0)
587 goto error;
588 isl_seq_cpy(bset->eq[k], morph->inv->row[1 + i],
589 morph->inv->n_col);
590 isl_seq_clr(bset->eq[k] + morph->inv->n_col, bset->n_div);
591 isl_int_set(bset->eq[k][morph->inv->n_col + div],
592 morph->inv->row[0][0]);
595 isl_int_clear(gcd);
597 return bset;
598 error:
599 isl_int_clear(gcd);
600 isl_basic_set_free(bset);
601 return NULL;
604 /* Apply the morphism to the basic set.
605 * We basically just compute the preimage of "bset" under the inverse mapping
606 * in morph, add in stride constraints and intersect with the range
607 * of the morphism.
609 __isl_give isl_basic_set *isl_morph_basic_set(__isl_take isl_morph *morph,
610 __isl_take isl_basic_set *bset)
612 isl_basic_set *res = NULL;
613 isl_mat *mat = NULL;
614 int i, k;
615 int max_stride;
617 if (!morph || !bset)
618 goto error;
620 isl_assert(bset->ctx, isl_space_is_equal(bset->dim, morph->dom->dim),
621 goto error);
623 max_stride = morph->inv->n_row - 1;
624 if (isl_int_is_one(morph->inv->row[0][0]))
625 max_stride = 0;
626 res = isl_basic_set_alloc_space(isl_space_copy(morph->ran->dim),
627 bset->n_div + max_stride, bset->n_eq + max_stride, bset->n_ineq);
629 for (i = 0; i < bset->n_div; ++i)
630 if (isl_basic_set_alloc_div(res) < 0)
631 goto error;
633 mat = isl_mat_sub_alloc6(bset->ctx, bset->eq, 0, bset->n_eq,
634 0, morph->inv->n_row);
635 mat = isl_mat_product(mat, isl_mat_copy(morph->inv));
636 if (!mat)
637 goto error;
638 for (i = 0; i < bset->n_eq; ++i) {
639 k = isl_basic_set_alloc_equality(res);
640 if (k < 0)
641 goto error;
642 isl_seq_cpy(res->eq[k], mat->row[i], mat->n_col);
643 isl_seq_scale(res->eq[k] + mat->n_col, bset->eq[i] + mat->n_col,
644 morph->inv->row[0][0], bset->n_div);
646 isl_mat_free(mat);
648 mat = isl_mat_sub_alloc6(bset->ctx, bset->ineq, 0, bset->n_ineq,
649 0, morph->inv->n_row);
650 mat = isl_mat_product(mat, isl_mat_copy(morph->inv));
651 if (!mat)
652 goto error;
653 for (i = 0; i < bset->n_ineq; ++i) {
654 k = isl_basic_set_alloc_inequality(res);
655 if (k < 0)
656 goto error;
657 isl_seq_cpy(res->ineq[k], mat->row[i], mat->n_col);
658 isl_seq_scale(res->ineq[k] + mat->n_col,
659 bset->ineq[i] + mat->n_col,
660 morph->inv->row[0][0], bset->n_div);
662 isl_mat_free(mat);
664 mat = isl_mat_sub_alloc6(bset->ctx, bset->div, 0, bset->n_div,
665 1, morph->inv->n_row);
666 mat = isl_mat_product(mat, isl_mat_copy(morph->inv));
667 if (!mat)
668 goto error;
669 for (i = 0; i < bset->n_div; ++i) {
670 isl_int_mul(res->div[i][0],
671 morph->inv->row[0][0], bset->div[i][0]);
672 isl_seq_cpy(res->div[i] + 1, mat->row[i], mat->n_col);
673 isl_seq_scale(res->div[i] + 1 + mat->n_col,
674 bset->div[i] + 1 + mat->n_col,
675 morph->inv->row[0][0], bset->n_div);
677 isl_mat_free(mat);
679 res = add_strides(res, morph);
681 if (isl_basic_set_is_rational(bset))
682 res = isl_basic_set_set_rational(res);
684 res = isl_basic_set_simplify(res);
685 res = isl_basic_set_finalize(res);
687 res = isl_basic_set_intersect(res, isl_basic_set_copy(morph->ran));
689 isl_morph_free(morph);
690 isl_basic_set_free(bset);
691 return res;
692 error:
693 isl_mat_free(mat);
694 isl_morph_free(morph);
695 isl_basic_set_free(bset);
696 isl_basic_set_free(res);
697 return NULL;
700 /* Apply the morphism to the set.
702 __isl_give isl_set *isl_morph_set(__isl_take isl_morph *morph,
703 __isl_take isl_set *set)
705 int i;
707 if (!morph || !set)
708 goto error;
710 isl_assert(set->ctx, isl_space_is_equal(set->dim, morph->dom->dim), goto error);
712 set = isl_set_cow(set);
713 if (!set)
714 goto error;
716 isl_space_free(set->dim);
717 set->dim = isl_space_copy(morph->ran->dim);
718 if (!set->dim)
719 goto error;
721 for (i = 0; i < set->n; ++i) {
722 set->p[i] = isl_morph_basic_set(isl_morph_copy(morph), set->p[i]);
723 if (!set->p[i])
724 goto error;
727 isl_morph_free(morph);
729 ISL_F_CLR(set, ISL_SET_NORMALIZED);
731 return set;
732 error:
733 isl_set_free(set);
734 isl_morph_free(morph);
735 return NULL;
738 /* Construct a morphism that first does morph2 and then morph1.
740 __isl_give isl_morph *isl_morph_compose(__isl_take isl_morph *morph1,
741 __isl_take isl_morph *morph2)
743 isl_mat *map, *inv;
744 isl_basic_set *dom, *ran;
746 if (!morph1 || !morph2)
747 goto error;
749 map = isl_mat_product(isl_mat_copy(morph1->map), isl_mat_copy(morph2->map));
750 inv = isl_mat_product(isl_mat_copy(morph2->inv), isl_mat_copy(morph1->inv));
751 dom = isl_morph_basic_set(isl_morph_inverse(isl_morph_copy(morph2)),
752 isl_basic_set_copy(morph1->dom));
753 dom = isl_basic_set_intersect(dom, isl_basic_set_copy(morph2->dom));
754 ran = isl_morph_basic_set(isl_morph_copy(morph1),
755 isl_basic_set_copy(morph2->ran));
756 ran = isl_basic_set_intersect(ran, isl_basic_set_copy(morph1->ran));
758 isl_morph_free(morph1);
759 isl_morph_free(morph2);
761 return isl_morph_alloc(dom, ran, map, inv);
762 error:
763 isl_morph_free(morph1);
764 isl_morph_free(morph2);
765 return NULL;
768 __isl_give isl_morph *isl_morph_inverse(__isl_take isl_morph *morph)
770 isl_basic_set *bset;
771 isl_mat *mat;
773 morph = isl_morph_cow(morph);
774 if (!morph)
775 return NULL;
777 bset = morph->dom;
778 morph->dom = morph->ran;
779 morph->ran = bset;
781 mat = morph->map;
782 morph->map = morph->inv;
783 morph->inv = mat;
785 return morph;
788 /* We detect all the equalities first to avoid implicit equalties
789 * being discovered during the computations. In particular,
790 * the compression on the variables could expose additional stride
791 * constraints on the parameters. This would result in existentially
792 * quantified variables after applying the resulting morph, which
793 * in turn could break invariants of the calling functions.
795 __isl_give isl_morph *isl_basic_set_full_compression(
796 __isl_keep isl_basic_set *bset)
798 isl_morph *morph, *morph2;
800 bset = isl_basic_set_copy(bset);
801 bset = isl_basic_set_detect_equalities(bset);
803 morph = isl_basic_set_variable_compression(bset, isl_dim_param);
804 bset = isl_morph_basic_set(isl_morph_copy(morph), bset);
806 morph2 = isl_basic_set_parameter_compression(bset);
807 bset = isl_morph_basic_set(isl_morph_copy(morph2), bset);
809 morph = isl_morph_compose(morph2, morph);
811 morph2 = isl_basic_set_variable_compression(bset, isl_dim_set);
812 isl_basic_set_free(bset);
814 morph = isl_morph_compose(morph2, morph);
816 return morph;
819 __isl_give isl_vec *isl_morph_vec(__isl_take isl_morph *morph,
820 __isl_take isl_vec *vec)
822 if (!morph)
823 goto error;
825 vec = isl_mat_vec_product(isl_mat_copy(morph->map), vec);
827 isl_morph_free(morph);
828 return vec;
829 error:
830 isl_morph_free(morph);
831 isl_vec_free(vec);
832 return NULL;