avoid "errno" argument name
[isl.git] / isl_morph.c
blob0d007e108723cc78e8f902d4a2b457c5003168c0
1 /*
2 * Copyright 2010 INRIA Saclay
4 * Use of this software is governed by the GNU LGPLv2.1 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_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)
22 isl_morph *morph;
24 if (!dom || !ran || !map || !inv)
25 goto error;
27 morph = isl_alloc_type(dom->ctx, struct isl_morph);
28 if (!morph)
29 goto error;
31 morph->ref = 1;
32 morph->dom = dom;
33 morph->ran = ran;
34 morph->map = map;
35 morph->inv = inv;
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;
46 __isl_give isl_morph *isl_morph_copy(__isl_keep isl_morph *morph)
48 if (!morph)
49 return NULL;
51 morph->ref++;
52 return morph;
55 __isl_give isl_morph *isl_morph_dup(__isl_keep isl_morph *morph)
57 if (!morph)
58 return NULL;
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));
65 __isl_give isl_morph *isl_morph_cow(__isl_take isl_morph *morph)
67 if (!morph)
68 return NULL;
70 if (morph->ref == 1)
71 return morph;
72 morph->ref--;
73 return isl_morph_dup(morph);
76 void isl_morph_free(__isl_take isl_morph *morph)
78 if (!morph)
79 return;
81 if (--morph->ref > 0)
82 return;
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);
91 __isl_give isl_space *isl_morph_get_ran_space(__isl_keep isl_morph *morph)
93 if (!morph)
94 return NULL;
96 return isl_space_copy(morph->ran->dim);
99 unsigned isl_morph_dom_dim(__isl_keep isl_morph *morph, enum isl_dim_type type)
101 if (!morph)
102 return 0;
104 return isl_basic_set_dim(morph->dom, type);
107 unsigned isl_morph_ran_dim(__isl_keep isl_morph *morph, enum isl_dim_type type)
109 if (!morph)
110 return 0;
112 return isl_basic_set_dim(morph->ran, type);
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)
118 unsigned dom_offset;
120 if (n == 0)
121 return morph;
123 morph = isl_morph_cow(morph);
124 if (!morph)
125 return NULL;
127 dom_offset = 1 + isl_space_offset(morph->dom->dim, type);
129 morph->dom = isl_basic_set_remove_dims(morph->dom, type, first, n);
131 morph->map = isl_mat_drop_cols(morph->map, dom_offset + first, n);
133 morph->inv = isl_mat_drop_rows(morph->inv, dom_offset + first, n);
135 if (morph->dom && morph->ran && morph->map && morph->inv)
136 return morph;
138 isl_morph_free(morph);
139 return NULL;
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)
145 unsigned ran_offset;
147 if (n == 0)
148 return morph;
150 morph = isl_morph_cow(morph);
151 if (!morph)
152 return NULL;
154 ran_offset = 1 + isl_space_offset(morph->ran->dim, type);
156 morph->ran = isl_basic_set_remove_dims(morph->ran, type, first, n);
158 morph->map = isl_mat_drop_rows(morph->map, ran_offset + first, n);
160 morph->inv = isl_mat_drop_cols(morph->inv, ran_offset + first, n);
162 if (morph->dom && morph->ran && morph->map && morph->inv)
163 return morph;
165 isl_morph_free(morph);
166 return NULL;
169 /* Project domain of morph onto its parameter domain.
171 __isl_give isl_morph *isl_morph_dom_params(__isl_take isl_morph *morph)
173 unsigned n;
175 if (!morph)
176 return NULL;
177 n = isl_basic_set_dim(morph->dom, isl_dim_set);
178 morph = isl_morph_remove_dom_dims(morph, isl_dim_set, 0, n);
179 if (!morph)
180 return NULL;
181 morph->dom = isl_basic_set_params(morph->dom);
182 if (morph->dom)
183 return morph;
185 isl_morph_free(morph);
186 return NULL;
189 /* Project range of morph onto its parameter domain.
191 __isl_give isl_morph *isl_morph_ran_params(__isl_take isl_morph *morph)
193 unsigned n;
195 if (!morph)
196 return NULL;
197 n = isl_basic_set_dim(morph->ran, isl_dim_set);
198 morph = isl_morph_remove_ran_dims(morph, isl_dim_set, 0, n);
199 if (!morph)
200 return NULL;
201 morph->ran = isl_basic_set_params(morph->ran);
202 if (morph->ran)
203 return morph;
205 isl_morph_free(morph);
206 return NULL;
209 void isl_morph_print_internal(__isl_take isl_morph *morph, FILE *out)
211 if (!morph)
212 return;
214 isl_basic_set_print(morph->dom, out, 0, "", "", ISL_FORMAT_ISL);
215 isl_basic_set_print(morph->ran, out, 0, "", "", ISL_FORMAT_ISL);
216 isl_mat_print_internal(morph->map, out, 4);
217 isl_mat_print_internal(morph->inv, out, 4);
220 void isl_morph_dump(__isl_take isl_morph *morph)
222 isl_morph_print_internal(morph, stderr);
225 __isl_give isl_morph *isl_morph_identity(__isl_keep isl_basic_set *bset)
227 isl_mat *id;
228 isl_basic_set *universe;
229 unsigned total;
231 if (!bset)
232 return NULL;
234 total = isl_basic_set_total_dim(bset);
235 id = isl_mat_identity(bset->ctx, 1 + total);
236 universe = isl_basic_set_universe(isl_space_copy(bset->dim));
238 return isl_morph_alloc(universe, isl_basic_set_copy(universe),
239 id, isl_mat_copy(id));
242 /* Create a(n identity) morphism between empty sets of the same dimension
243 * a "bset".
245 __isl_give isl_morph *isl_morph_empty(__isl_keep isl_basic_set *bset)
247 isl_mat *id;
248 isl_basic_set *empty;
249 unsigned total;
251 if (!bset)
252 return NULL;
254 total = isl_basic_set_total_dim(bset);
255 id = isl_mat_identity(bset->ctx, 1 + total);
256 empty = isl_basic_set_empty(isl_space_copy(bset->dim));
258 return isl_morph_alloc(empty, isl_basic_set_copy(empty),
259 id, isl_mat_copy(id));
262 /* Given a matrix that maps a (possibly) parametric domain to
263 * a parametric domain, add in rows that map the "nparam" parameters onto
264 * themselves.
266 static __isl_give isl_mat *insert_parameter_rows(__isl_take isl_mat *mat,
267 unsigned nparam)
269 int i;
271 if (nparam == 0)
272 return mat;
273 if (!mat)
274 return NULL;
276 mat = isl_mat_insert_rows(mat, 1, nparam);
277 if (!mat)
278 return NULL;
280 for (i = 0; i < nparam; ++i) {
281 isl_seq_clr(mat->row[1 + i], mat->n_col);
282 isl_int_set(mat->row[1 + i][1 + i], mat->row[0][0]);
285 return mat;
288 /* Construct a basic set described by the "n" equalities of "bset" starting
289 * at "first".
291 static __isl_give isl_basic_set *copy_equalities(__isl_keep isl_basic_set *bset,
292 unsigned first, unsigned n)
294 int i, k;
295 isl_basic_set *eq;
296 unsigned total;
298 isl_assert(bset->ctx, bset->n_div == 0, return NULL);
300 total = isl_basic_set_total_dim(bset);
301 eq = isl_basic_set_alloc_space(isl_space_copy(bset->dim), 0, n, 0);
302 if (!eq)
303 return NULL;
304 for (i = 0; i < n; ++i) {
305 k = isl_basic_set_alloc_equality(eq);
306 if (k < 0)
307 goto error;
308 isl_seq_cpy(eq->eq[k], bset->eq[first + k], 1 + total);
311 return eq;
312 error:
313 isl_basic_set_free(eq);
314 return NULL;
317 /* Given a basic set, exploit the equalties in the a basic set to construct
318 * a morphishm that maps the basic set to a lower-dimensional space.
319 * Specifically, the morphism reduces the number of dimensions of type "type".
321 * This function is a slight generalization of isl_mat_variable_compression
322 * in that it allows the input to be parametric and that it allows for the
323 * compression of either parameters or set variables.
325 * We first select the equalities of interest, that is those that involve
326 * variables of type "type" and no later variables.
327 * Denote those equalities as
329 * -C(p) + M x = 0
331 * where C(p) depends on the parameters if type == isl_dim_set and
332 * is a constant if type == isl_dim_param.
334 * First compute the (left) Hermite normal form of M,
336 * M [U1 U2] = M U = H = [H1 0]
337 * or
338 * M = H Q = [H1 0] [Q1]
339 * [Q2]
341 * with U, Q unimodular, Q = U^{-1} (and H lower triangular).
342 * Define the transformed variables as
344 * x = [U1 U2] [ x1' ] = [U1 U2] [Q1] x
345 * [ x2' ] [Q2]
347 * The equalities then become
349 * -C(p) + H1 x1' = 0 or x1' = H1^{-1} C(p) = C'(p)
351 * If the denominator of the constant term does not divide the
352 * the common denominator of the parametric terms, then every
353 * integer point is mapped to a non-integer point and then the original set has no
354 * integer solutions (since the x' are a unimodular transformation
355 * of the x). In this case, an empty morphism is returned.
356 * Otherwise, the transformation is given by
358 * x = U1 H1^{-1} C(p) + U2 x2'
360 * The inverse transformation is simply
362 * x2' = Q2 x
364 * Both matrices are extended to map the full original space to the full
365 * compressed space.
367 __isl_give isl_morph *isl_basic_set_variable_compression(
368 __isl_keep isl_basic_set *bset, enum isl_dim_type type)
370 unsigned otype;
371 unsigned ntype;
372 unsigned orest;
373 unsigned nrest;
374 int f_eq, n_eq;
375 isl_space *dim;
376 isl_mat *H, *U, *Q, *C = NULL, *H1, *U1, *U2;
377 isl_basic_set *dom, *ran;
379 if (!bset)
380 return NULL;
382 if (isl_basic_set_plain_is_empty(bset))
383 return isl_morph_empty(bset);
385 isl_assert(bset->ctx, bset->n_div == 0, return NULL);
387 otype = 1 + isl_space_offset(bset->dim, type);
388 ntype = isl_basic_set_dim(bset, type);
389 orest = otype + ntype;
390 nrest = isl_basic_set_total_dim(bset) - (orest - 1);
392 for (f_eq = 0; f_eq < bset->n_eq; ++f_eq)
393 if (isl_seq_first_non_zero(bset->eq[f_eq] + orest, nrest) == -1)
394 break;
395 for (n_eq = 0; f_eq + n_eq < bset->n_eq; ++n_eq)
396 if (isl_seq_first_non_zero(bset->eq[f_eq + n_eq] + otype, ntype) == -1)
397 break;
398 if (n_eq == 0)
399 return isl_morph_identity(bset);
401 H = isl_mat_sub_alloc6(bset->ctx, bset->eq, f_eq, n_eq, otype, ntype);
402 H = isl_mat_left_hermite(H, 0, &U, &Q);
403 if (!H || !U || !Q)
404 goto error;
405 Q = isl_mat_drop_rows(Q, 0, n_eq);
406 Q = isl_mat_diagonal(isl_mat_identity(bset->ctx, otype), Q);
407 Q = isl_mat_diagonal(Q, isl_mat_identity(bset->ctx, nrest));
408 C = isl_mat_alloc(bset->ctx, 1 + n_eq, otype);
409 if (!C)
410 goto error;
411 isl_int_set_si(C->row[0][0], 1);
412 isl_seq_clr(C->row[0] + 1, otype - 1);
413 isl_mat_sub_neg(C->ctx, C->row + 1, bset->eq + f_eq, n_eq, 0, 0, otype);
414 H1 = isl_mat_sub_alloc(H, 0, H->n_row, 0, H->n_row);
415 H1 = isl_mat_lin_to_aff(H1);
416 C = isl_mat_inverse_product(H1, C);
417 if (!C)
418 goto error;
419 isl_mat_free(H);
421 if (!isl_int_is_one(C->row[0][0])) {
422 int i;
423 isl_int g;
425 isl_int_init(g);
426 for (i = 0; i < n_eq; ++i) {
427 isl_seq_gcd(C->row[1 + i] + 1, otype - 1, &g);
428 isl_int_gcd(g, g, C->row[0][0]);
429 if (!isl_int_is_divisible_by(C->row[1 + i][0], g))
430 break;
432 isl_int_clear(g);
434 if (i < n_eq) {
435 isl_mat_free(C);
436 isl_mat_free(U);
437 isl_mat_free(Q);
438 return isl_morph_empty(bset);
441 C = isl_mat_normalize(C);
444 U1 = isl_mat_sub_alloc(U, 0, U->n_row, 0, n_eq);
445 U1 = isl_mat_lin_to_aff(U1);
446 U2 = isl_mat_sub_alloc(U, 0, U->n_row, n_eq, U->n_row - n_eq);
447 U2 = isl_mat_lin_to_aff(U2);
448 isl_mat_free(U);
450 C = isl_mat_product(U1, C);
451 C = isl_mat_aff_direct_sum(C, U2);
452 C = insert_parameter_rows(C, otype - 1);
453 C = isl_mat_diagonal(C, isl_mat_identity(bset->ctx, nrest));
455 dim = isl_space_copy(bset->dim);
456 dim = isl_space_drop_dims(dim, type, 0, ntype);
457 dim = isl_space_add_dims(dim, type, ntype - n_eq);
458 ran = isl_basic_set_universe(dim);
459 dom = copy_equalities(bset, f_eq, n_eq);
461 return isl_morph_alloc(dom, ran, Q, C);
462 error:
463 isl_mat_free(C);
464 isl_mat_free(H);
465 isl_mat_free(U);
466 isl_mat_free(Q);
467 return NULL;
470 /* Construct a parameter compression for "bset".
471 * We basically just call isl_mat_parameter_compression with the right input
472 * and then extend the resulting matrix to include the variables.
474 * Let the equalities be given as
476 * B(p) + A x = 0
478 * and let [H 0] be the Hermite Normal Form of A, then
480 * H^-1 B(p)
482 * needs to be integer, so we impose that each row is divisible by
483 * the denominator.
485 __isl_give isl_morph *isl_basic_set_parameter_compression(
486 __isl_keep isl_basic_set *bset)
488 unsigned nparam;
489 unsigned nvar;
490 int n_eq;
491 isl_mat *H, *B;
492 isl_vec *d;
493 isl_mat *map, *inv;
494 isl_basic_set *dom, *ran;
496 if (!bset)
497 return NULL;
499 if (isl_basic_set_plain_is_empty(bset))
500 return isl_morph_empty(bset);
501 if (bset->n_eq == 0)
502 return isl_morph_identity(bset);
504 isl_assert(bset->ctx, bset->n_div == 0, return NULL);
506 n_eq = bset->n_eq;
507 nparam = isl_basic_set_dim(bset, isl_dim_param);
508 nvar = isl_basic_set_dim(bset, isl_dim_set);
510 isl_assert(bset->ctx, n_eq <= nvar, return NULL);
512 d = isl_vec_alloc(bset->ctx, n_eq);
513 B = isl_mat_sub_alloc6(bset->ctx, bset->eq, 0, n_eq, 0, 1 + nparam);
514 H = isl_mat_sub_alloc6(bset->ctx, bset->eq, 0, n_eq, 1 + nparam, nvar);
515 H = isl_mat_left_hermite(H, 0, NULL, NULL);
516 H = isl_mat_drop_cols(H, n_eq, nvar - n_eq);
517 H = isl_mat_lin_to_aff(H);
518 H = isl_mat_right_inverse(H);
519 if (!H || !d)
520 goto error;
521 isl_seq_set(d->el, H->row[0][0], d->size);
522 H = isl_mat_drop_rows(H, 0, 1);
523 H = isl_mat_drop_cols(H, 0, 1);
524 B = isl_mat_product(H, B);
525 inv = isl_mat_parameter_compression(B, d);
526 inv = isl_mat_diagonal(inv, isl_mat_identity(bset->ctx, nvar));
527 map = isl_mat_right_inverse(isl_mat_copy(inv));
529 dom = isl_basic_set_universe(isl_space_copy(bset->dim));
530 ran = isl_basic_set_universe(isl_space_copy(bset->dim));
532 return isl_morph_alloc(dom, ran, map, inv);
533 error:
534 isl_mat_free(H);
535 isl_mat_free(B);
536 isl_vec_free(d);
537 return NULL;
540 /* Add stride constraints to "bset" based on the inverse mapping
541 * that was plugged in. In particular, if morph maps x' to x,
542 * the the constraints of the original input
544 * A x' + b >= 0
546 * have been rewritten to
548 * A inv x + b >= 0
550 * However, this substitution may loose information on the integrality of x',
551 * so we need to impose that
553 * inv x
555 * is integral. If inv = B/d, this means that we need to impose that
557 * B x = 0 mod d
559 * or
561 * exists alpha in Z^m: B x = d alpha
564 static __isl_give isl_basic_set *add_strides(__isl_take isl_basic_set *bset,
565 __isl_keep isl_morph *morph)
567 int i, div, k;
568 isl_int gcd;
570 if (isl_int_is_one(morph->inv->row[0][0]))
571 return bset;
573 isl_int_init(gcd);
575 for (i = 0; 1 + i < morph->inv->n_row; ++i) {
576 isl_seq_gcd(morph->inv->row[1 + i], morph->inv->n_col, &gcd);
577 if (isl_int_is_divisible_by(gcd, morph->inv->row[0][0]))
578 continue;
579 div = isl_basic_set_alloc_div(bset);
580 if (div < 0)
581 goto error;
582 k = isl_basic_set_alloc_equality(bset);
583 if (k < 0)
584 goto error;
585 isl_seq_cpy(bset->eq[k], morph->inv->row[1 + i],
586 morph->inv->n_col);
587 isl_seq_clr(bset->eq[k] + morph->inv->n_col, bset->n_div);
588 isl_int_set(bset->eq[k][morph->inv->n_col + div],
589 morph->inv->row[0][0]);
592 isl_int_clear(gcd);
594 return bset;
595 error:
596 isl_int_clear(gcd);
597 isl_basic_set_free(bset);
598 return NULL;
601 /* Apply the morphism to the basic set.
602 * We basically just compute the preimage of "bset" under the inverse mapping
603 * in morph, add in stride constraints and intersect with the range
604 * of the morphism.
606 __isl_give isl_basic_set *isl_morph_basic_set(__isl_take isl_morph *morph,
607 __isl_take isl_basic_set *bset)
609 isl_basic_set *res = NULL;
610 isl_mat *mat = NULL;
611 int i, k;
612 int max_stride;
614 if (!morph || !bset)
615 goto error;
617 isl_assert(bset->ctx, isl_space_is_equal(bset->dim, morph->dom->dim),
618 goto error);
620 max_stride = morph->inv->n_row - 1;
621 if (isl_int_is_one(morph->inv->row[0][0]))
622 max_stride = 0;
623 res = isl_basic_set_alloc_space(isl_space_copy(morph->ran->dim),
624 bset->n_div + max_stride, bset->n_eq + max_stride, bset->n_ineq);
626 for (i = 0; i < bset->n_div; ++i)
627 if (isl_basic_set_alloc_div(res) < 0)
628 goto error;
630 mat = isl_mat_sub_alloc6(bset->ctx, bset->eq, 0, bset->n_eq,
631 0, morph->inv->n_row);
632 mat = isl_mat_product(mat, isl_mat_copy(morph->inv));
633 if (!mat)
634 goto error;
635 for (i = 0; i < bset->n_eq; ++i) {
636 k = isl_basic_set_alloc_equality(res);
637 if (k < 0)
638 goto error;
639 isl_seq_cpy(res->eq[k], mat->row[i], mat->n_col);
640 isl_seq_scale(res->eq[k] + mat->n_col, bset->eq[i] + mat->n_col,
641 morph->inv->row[0][0], bset->n_div);
643 isl_mat_free(mat);
645 mat = isl_mat_sub_alloc6(bset->ctx, bset->ineq, 0, bset->n_ineq,
646 0, morph->inv->n_row);
647 mat = isl_mat_product(mat, isl_mat_copy(morph->inv));
648 if (!mat)
649 goto error;
650 for (i = 0; i < bset->n_ineq; ++i) {
651 k = isl_basic_set_alloc_inequality(res);
652 if (k < 0)
653 goto error;
654 isl_seq_cpy(res->ineq[k], mat->row[i], mat->n_col);
655 isl_seq_scale(res->ineq[k] + mat->n_col,
656 bset->ineq[i] + mat->n_col,
657 morph->inv->row[0][0], bset->n_div);
659 isl_mat_free(mat);
661 mat = isl_mat_sub_alloc6(bset->ctx, bset->div, 0, bset->n_div,
662 1, morph->inv->n_row);
663 mat = isl_mat_product(mat, isl_mat_copy(morph->inv));
664 if (!mat)
665 goto error;
666 for (i = 0; i < bset->n_div; ++i) {
667 isl_int_mul(res->div[i][0],
668 morph->inv->row[0][0], bset->div[i][0]);
669 isl_seq_cpy(res->div[i] + 1, mat->row[i], mat->n_col);
670 isl_seq_scale(res->div[i] + 1 + mat->n_col,
671 bset->div[i] + 1 + mat->n_col,
672 morph->inv->row[0][0], bset->n_div);
674 isl_mat_free(mat);
676 res = add_strides(res, morph);
678 if (isl_basic_set_is_rational(bset))
679 res = isl_basic_set_set_rational(res);
681 res = isl_basic_set_simplify(res);
682 res = isl_basic_set_finalize(res);
684 res = isl_basic_set_intersect(res, isl_basic_set_copy(morph->ran));
686 isl_morph_free(morph);
687 isl_basic_set_free(bset);
688 return res;
689 error:
690 isl_mat_free(mat);
691 isl_morph_free(morph);
692 isl_basic_set_free(bset);
693 isl_basic_set_free(res);
694 return NULL;
697 /* Apply the morphism to the set.
699 __isl_give isl_set *isl_morph_set(__isl_take isl_morph *morph,
700 __isl_take isl_set *set)
702 int i;
704 if (!morph || !set)
705 goto error;
707 isl_assert(set->ctx, isl_space_is_equal(set->dim, morph->dom->dim), goto error);
709 set = isl_set_cow(set);
710 if (!set)
711 goto error;
713 isl_space_free(set->dim);
714 set->dim = isl_space_copy(morph->ran->dim);
715 if (!set->dim)
716 goto error;
718 for (i = 0; i < set->n; ++i) {
719 set->p[i] = isl_morph_basic_set(isl_morph_copy(morph), set->p[i]);
720 if (!set->p[i])
721 goto error;
724 isl_morph_free(morph);
726 ISL_F_CLR(set, ISL_SET_NORMALIZED);
728 return set;
729 error:
730 isl_set_free(set);
731 isl_morph_free(morph);
732 return NULL;
735 /* Construct a morphism that first does morph2 and then morph1.
737 __isl_give isl_morph *isl_morph_compose(__isl_take isl_morph *morph1,
738 __isl_take isl_morph *morph2)
740 isl_mat *map, *inv;
741 isl_basic_set *dom, *ran;
743 if (!morph1 || !morph2)
744 goto error;
746 map = isl_mat_product(isl_mat_copy(morph1->map), isl_mat_copy(morph2->map));
747 inv = isl_mat_product(isl_mat_copy(morph2->inv), isl_mat_copy(morph1->inv));
748 dom = isl_morph_basic_set(isl_morph_inverse(isl_morph_copy(morph2)),
749 isl_basic_set_copy(morph1->dom));
750 dom = isl_basic_set_intersect(dom, isl_basic_set_copy(morph2->dom));
751 ran = isl_morph_basic_set(isl_morph_copy(morph1),
752 isl_basic_set_copy(morph2->ran));
753 ran = isl_basic_set_intersect(ran, isl_basic_set_copy(morph1->ran));
755 isl_morph_free(morph1);
756 isl_morph_free(morph2);
758 return isl_morph_alloc(dom, ran, map, inv);
759 error:
760 isl_morph_free(morph1);
761 isl_morph_free(morph2);
762 return NULL;
765 __isl_give isl_morph *isl_morph_inverse(__isl_take isl_morph *morph)
767 isl_basic_set *bset;
768 isl_mat *mat;
770 morph = isl_morph_cow(morph);
771 if (!morph)
772 return NULL;
774 bset = morph->dom;
775 morph->dom = morph->ran;
776 morph->ran = bset;
778 mat = morph->map;
779 morph->map = morph->inv;
780 morph->inv = mat;
782 return morph;
785 __isl_give isl_morph *isl_basic_set_full_compression(
786 __isl_keep isl_basic_set *bset)
788 isl_morph *morph, *morph2;
790 bset = isl_basic_set_copy(bset);
792 morph = isl_basic_set_variable_compression(bset, isl_dim_param);
793 bset = isl_morph_basic_set(isl_morph_copy(morph), bset);
795 morph2 = isl_basic_set_parameter_compression(bset);
796 bset = isl_morph_basic_set(isl_morph_copy(morph2), bset);
798 morph = isl_morph_compose(morph2, morph);
800 morph2 = isl_basic_set_variable_compression(bset, isl_dim_set);
801 isl_basic_set_free(bset);
803 morph = isl_morph_compose(morph2, morph);
805 return morph;
808 __isl_give isl_vec *isl_morph_vec(__isl_take isl_morph *morph,
809 __isl_take isl_vec *vec)
811 if (!morph)
812 goto error;
814 vec = isl_mat_vec_product(isl_mat_copy(morph->map), vec);
816 isl_morph_free(morph);
817 return vec;
818 error:
819 isl_morph_free(morph);
820 isl_vec_free(vec);
821 return NULL;