isl_qpolynomial_get_den: improve error handling
[isl.git] / isl_morph.c
blob35c614a628e0113d3775471cfdfb1bbdad1b6105
1 /*
2 * Copyright 2010-2011 INRIA Saclay
3 * Copyright 2014 Ecole Normale Superieure
5 * Use of this software is governed by the MIT license
7 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
8 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
9 * 91893 Orsay, France
10 * and Ecole Normale Superieure, 45 rue d'Ulm, 75230 Paris, France
13 #include <isl_map_private.h>
14 #include <isl_aff_private.h>
15 #include <isl_morph.h>
16 #include <isl_seq.h>
17 #include <isl_mat_private.h>
18 #include <isl_space_private.h>
19 #include <isl_equalities.h>
20 #include <isl_id_private.h>
22 isl_ctx *isl_morph_get_ctx(__isl_keep isl_morph *morph)
24 if (!morph)
25 return NULL;
26 return isl_basic_set_get_ctx(morph->dom);
29 __isl_give isl_morph *isl_morph_alloc(
30 __isl_take isl_basic_set *dom, __isl_take isl_basic_set *ran,
31 __isl_take isl_mat *map, __isl_take isl_mat *inv)
33 isl_morph *morph;
35 if (!dom || !ran || !map || !inv)
36 goto error;
38 morph = isl_alloc_type(dom->ctx, struct isl_morph);
39 if (!morph)
40 goto error;
42 morph->ref = 1;
43 morph->dom = dom;
44 morph->ran = ran;
45 morph->map = map;
46 morph->inv = inv;
48 return morph;
49 error:
50 isl_basic_set_free(dom);
51 isl_basic_set_free(ran);
52 isl_mat_free(map);
53 isl_mat_free(inv);
54 return NULL;
57 __isl_give isl_morph *isl_morph_copy(__isl_keep isl_morph *morph)
59 if (!morph)
60 return NULL;
62 morph->ref++;
63 return morph;
66 __isl_give isl_morph *isl_morph_dup(__isl_keep isl_morph *morph)
68 if (!morph)
69 return NULL;
71 return isl_morph_alloc(isl_basic_set_copy(morph->dom),
72 isl_basic_set_copy(morph->ran),
73 isl_mat_copy(morph->map), isl_mat_copy(morph->inv));
76 __isl_give isl_morph *isl_morph_cow(__isl_take isl_morph *morph)
78 if (!morph)
79 return NULL;
81 if (morph->ref == 1)
82 return morph;
83 morph->ref--;
84 return isl_morph_dup(morph);
87 __isl_null isl_morph *isl_morph_free(__isl_take isl_morph *morph)
89 if (!morph)
90 return NULL;
92 if (--morph->ref > 0)
93 return NULL;
95 isl_basic_set_free(morph->dom);
96 isl_basic_set_free(morph->ran);
97 isl_mat_free(morph->map);
98 isl_mat_free(morph->inv);
99 free(morph);
101 return NULL;
104 /* Is "morph" an identity on the parameters?
106 static int identity_on_parameters(__isl_keep isl_morph *morph)
108 int is_identity;
109 unsigned nparam;
110 isl_mat *sub;
112 nparam = isl_morph_dom_dim(morph, isl_dim_param);
113 if (nparam != isl_morph_ran_dim(morph, isl_dim_param))
114 return 0;
115 if (nparam == 0)
116 return 1;
117 sub = isl_mat_sub_alloc(morph->map, 0, 1 + nparam, 0, 1 + nparam);
118 is_identity = isl_mat_is_scaled_identity(sub);
119 isl_mat_free(sub);
121 return is_identity;
124 /* Return an affine expression of the variables of the range of "morph"
125 * in terms of the parameters and the variables of the domain on "morph".
127 * In order for the space manipulations to make sense, we require
128 * that the parameters are not modified by "morph".
130 __isl_give isl_multi_aff *isl_morph_get_var_multi_aff(
131 __isl_keep isl_morph *morph)
133 isl_space *dom, *ran, *space;
134 isl_local_space *ls;
135 isl_multi_aff *ma;
136 unsigned nparam, nvar;
137 int i;
138 int is_identity;
140 if (!morph)
141 return NULL;
143 is_identity = identity_on_parameters(morph);
144 if (is_identity < 0)
145 return NULL;
146 if (!is_identity)
147 isl_die(isl_morph_get_ctx(morph), isl_error_invalid,
148 "cannot handle parameter compression", return NULL);
150 dom = isl_morph_get_dom_space(morph);
151 ls = isl_local_space_from_space(isl_space_copy(dom));
152 ran = isl_morph_get_ran_space(morph);
153 space = isl_space_map_from_domain_and_range(dom, ran);
154 ma = isl_multi_aff_zero(space);
156 nparam = isl_multi_aff_dim(ma, isl_dim_param);
157 nvar = isl_multi_aff_dim(ma, isl_dim_out);
158 for (i = 0; i < nvar; ++i) {
159 isl_val *val;
160 isl_vec *v;
161 isl_aff *aff;
163 v = isl_mat_get_row(morph->map, 1 + nparam + i);
164 v = isl_vec_insert_els(v, 0, 1);
165 val = isl_mat_get_element_val(morph->map, 0, 0);
166 v = isl_vec_set_element_val(v, 0, val);
167 aff = isl_aff_alloc_vec(isl_local_space_copy(ls), v);
168 ma = isl_multi_aff_set_aff(ma, i, aff);
171 isl_local_space_free(ls);
172 return ma;
175 /* Return the domain space of "morph".
177 __isl_give isl_space *isl_morph_get_dom_space(__isl_keep isl_morph *morph)
179 if (!morph)
180 return NULL;
182 return isl_basic_set_get_space(morph->dom);
185 __isl_give isl_space *isl_morph_get_ran_space(__isl_keep isl_morph *morph)
187 if (!morph)
188 return NULL;
190 return isl_space_copy(morph->ran->dim);
193 unsigned isl_morph_dom_dim(__isl_keep isl_morph *morph, enum isl_dim_type type)
195 if (!morph)
196 return 0;
198 return isl_basic_set_dim(morph->dom, type);
201 unsigned isl_morph_ran_dim(__isl_keep isl_morph *morph, enum isl_dim_type type)
203 if (!morph)
204 return 0;
206 return isl_basic_set_dim(morph->ran, type);
209 __isl_give isl_morph *isl_morph_remove_dom_dims(__isl_take isl_morph *morph,
210 enum isl_dim_type type, unsigned first, unsigned n)
212 unsigned dom_offset;
214 if (n == 0)
215 return morph;
217 morph = isl_morph_cow(morph);
218 if (!morph)
219 return NULL;
221 dom_offset = 1 + isl_space_offset(morph->dom->dim, type);
223 morph->dom = isl_basic_set_remove_dims(morph->dom, type, first, n);
225 morph->map = isl_mat_drop_cols(morph->map, dom_offset + first, n);
227 morph->inv = isl_mat_drop_rows(morph->inv, dom_offset + first, n);
229 if (morph->dom && morph->ran && morph->map && morph->inv)
230 return morph;
232 isl_morph_free(morph);
233 return NULL;
236 __isl_give isl_morph *isl_morph_remove_ran_dims(__isl_take isl_morph *morph,
237 enum isl_dim_type type, unsigned first, unsigned n)
239 unsigned ran_offset;
241 if (n == 0)
242 return morph;
244 morph = isl_morph_cow(morph);
245 if (!morph)
246 return NULL;
248 ran_offset = 1 + isl_space_offset(morph->ran->dim, type);
250 morph->ran = isl_basic_set_remove_dims(morph->ran, type, first, n);
252 morph->map = isl_mat_drop_rows(morph->map, ran_offset + first, n);
254 morph->inv = isl_mat_drop_cols(morph->inv, ran_offset + first, n);
256 if (morph->dom && morph->ran && morph->map && morph->inv)
257 return morph;
259 isl_morph_free(morph);
260 return NULL;
263 /* Project domain of morph onto its parameter domain.
265 __isl_give isl_morph *isl_morph_dom_params(__isl_take isl_morph *morph)
267 unsigned n;
269 morph = isl_morph_cow(morph);
270 if (!morph)
271 return NULL;
272 n = isl_basic_set_dim(morph->dom, isl_dim_set);
273 morph = isl_morph_remove_dom_dims(morph, isl_dim_set, 0, n);
274 if (!morph)
275 return NULL;
276 morph->dom = isl_basic_set_params(morph->dom);
277 if (morph->dom)
278 return morph;
280 isl_morph_free(morph);
281 return NULL;
284 /* Project range of morph onto its parameter domain.
286 __isl_give isl_morph *isl_morph_ran_params(__isl_take isl_morph *morph)
288 unsigned n;
290 morph = isl_morph_cow(morph);
291 if (!morph)
292 return NULL;
293 n = isl_basic_set_dim(morph->ran, isl_dim_set);
294 morph = isl_morph_remove_ran_dims(morph, isl_dim_set, 0, n);
295 if (!morph)
296 return NULL;
297 morph->ran = isl_basic_set_params(morph->ran);
298 if (morph->ran)
299 return morph;
301 isl_morph_free(morph);
302 return NULL;
305 void isl_morph_print_internal(__isl_take isl_morph *morph, FILE *out)
307 if (!morph)
308 return;
310 isl_basic_set_dump(morph->dom);
311 isl_basic_set_dump(morph->ran);
312 isl_mat_print_internal(morph->map, out, 4);
313 isl_mat_print_internal(morph->inv, out, 4);
316 void isl_morph_dump(__isl_take isl_morph *morph)
318 isl_morph_print_internal(morph, stderr);
321 __isl_give isl_morph *isl_morph_identity(__isl_keep isl_basic_set *bset)
323 isl_mat *id;
324 isl_basic_set *universe;
325 unsigned total;
327 if (!bset)
328 return NULL;
330 total = isl_basic_set_total_dim(bset);
331 id = isl_mat_identity(bset->ctx, 1 + total);
332 universe = isl_basic_set_universe(isl_space_copy(bset->dim));
334 return isl_morph_alloc(universe, isl_basic_set_copy(universe),
335 id, isl_mat_copy(id));
338 /* Create a(n identity) morphism between empty sets of the same dimension
339 * a "bset".
341 __isl_give isl_morph *isl_morph_empty(__isl_keep isl_basic_set *bset)
343 isl_mat *id;
344 isl_basic_set *empty;
345 unsigned total;
347 if (!bset)
348 return NULL;
350 total = isl_basic_set_total_dim(bset);
351 id = isl_mat_identity(bset->ctx, 1 + total);
352 empty = isl_basic_set_empty(isl_space_copy(bset->dim));
354 return isl_morph_alloc(empty, isl_basic_set_copy(empty),
355 id, isl_mat_copy(id));
358 /* Construct a basic set described by the "n" equalities of "bset" starting
359 * at "first".
361 static __isl_give isl_basic_set *copy_equalities(__isl_keep isl_basic_set *bset,
362 unsigned first, unsigned n)
364 int i, k;
365 isl_basic_set *eq;
366 unsigned total;
368 if (isl_basic_set_check_no_locals(bset) < 0)
369 return NULL;
371 total = isl_basic_set_total_dim(bset);
372 eq = isl_basic_set_alloc_space(isl_space_copy(bset->dim), 0, n, 0);
373 if (!eq)
374 return NULL;
375 for (i = 0; i < n; ++i) {
376 k = isl_basic_set_alloc_equality(eq);
377 if (k < 0)
378 goto error;
379 isl_seq_cpy(eq->eq[k], bset->eq[first + i], 1 + total);
382 return eq;
383 error:
384 isl_basic_set_free(eq);
385 return NULL;
388 /* Given a basic set, exploit the equalities in the basic set to construct
389 * a morphism that maps the basic set to a lower-dimensional space
390 * with identifier "id".
391 * Specifically, the morphism reduces the number of dimensions of type "type".
393 * We first select the equalities of interest, that is those that involve
394 * variables of type "type" and no later variables.
395 * Denote those equalities as
397 * -C(p) + M x = 0
399 * where C(p) depends on the parameters if type == isl_dim_set and
400 * is a constant if type == isl_dim_param.
402 * Use isl_mat_final_variable_compression to construct a compression
404 * x = T x'
406 * x' = Q x
408 * If T is a zero-column matrix, then the set of equality constraints
409 * do not admit a solution. In this case, an empty morphism is returned.
411 * Both matrices are extended to map the full original space to the full
412 * compressed space.
414 __isl_give isl_morph *isl_basic_set_variable_compression_with_id(
415 __isl_keep isl_basic_set *bset, enum isl_dim_type type,
416 __isl_keep isl_id *id)
418 unsigned otype;
419 unsigned ntype;
420 unsigned orest;
421 unsigned nrest;
422 int f_eq, n_eq;
423 isl_space *space;
424 isl_mat *E, *Q, *C;
425 isl_basic_set *dom, *ran;
427 if (!bset)
428 return NULL;
430 if (isl_basic_set_plain_is_empty(bset))
431 return isl_morph_empty(bset);
433 if (isl_basic_set_check_no_locals(bset) < 0)
434 return NULL;
436 otype = 1 + isl_space_offset(bset->dim, type);
437 ntype = isl_basic_set_dim(bset, type);
438 orest = otype + ntype;
439 nrest = isl_basic_set_total_dim(bset) - (orest - 1);
441 for (f_eq = 0; f_eq < bset->n_eq; ++f_eq)
442 if (isl_seq_first_non_zero(bset->eq[f_eq] + orest, nrest) == -1)
443 break;
444 for (n_eq = 0; f_eq + n_eq < bset->n_eq; ++n_eq)
445 if (isl_seq_first_non_zero(bset->eq[f_eq + n_eq] + otype, ntype) == -1)
446 break;
447 if (n_eq == 0)
448 return isl_morph_identity(bset);
450 E = isl_mat_sub_alloc6(bset->ctx, bset->eq, f_eq, n_eq, 0, orest);
451 C = isl_mat_final_variable_compression(E, otype - 1, &Q);
452 if (!Q)
453 C = isl_mat_free(C);
454 if (C && C->n_col == 0) {
455 isl_mat_free(C);
456 isl_mat_free(Q);
457 return isl_morph_empty(bset);
460 Q = isl_mat_diagonal(Q, isl_mat_identity(bset->ctx, nrest));
461 C = isl_mat_diagonal(C, isl_mat_identity(bset->ctx, nrest));
463 space = isl_space_copy(bset->dim);
464 space = isl_space_drop_dims(space, type, 0, ntype);
465 space = isl_space_add_dims(space, type, ntype - n_eq);
466 space = isl_space_set_tuple_id(space, isl_dim_set, isl_id_copy(id));
467 ran = isl_basic_set_universe(space);
468 dom = copy_equalities(bset, f_eq, n_eq);
470 return isl_morph_alloc(dom, ran, Q, C);
473 /* Given a basic set, exploit the equalities in the basic set to construct
474 * a morphism that maps the basic set to a lower-dimensional space.
475 * Specifically, the morphism reduces the number of dimensions of type "type".
477 __isl_give isl_morph *isl_basic_set_variable_compression(
478 __isl_keep isl_basic_set *bset, enum isl_dim_type type)
480 return isl_basic_set_variable_compression_with_id(bset, type,
481 &isl_id_none);
484 /* Construct a parameter compression for "bset".
485 * We basically just call isl_mat_parameter_compression with the right input
486 * and then extend the resulting matrix to include the variables.
488 * The implementation assumes that "bset" does not have any equalities
489 * that only involve the parameters and that isl_basic_set_gauss has
490 * been applied to "bset".
492 * Let the equalities be given as
494 * B(p) + A x = 0.
496 * We use isl_mat_parameter_compression_ext to compute the compression
498 * p = T p'.
500 __isl_give isl_morph *isl_basic_set_parameter_compression(
501 __isl_keep isl_basic_set *bset)
503 unsigned nparam;
504 unsigned nvar;
505 unsigned n_div;
506 int n_eq;
507 isl_mat *H, *B;
508 isl_mat *map, *inv;
509 isl_basic_set *dom, *ran;
511 if (!bset)
512 return NULL;
514 if (isl_basic_set_plain_is_empty(bset))
515 return isl_morph_empty(bset);
516 if (bset->n_eq == 0)
517 return isl_morph_identity(bset);
519 n_eq = bset->n_eq;
520 nparam = isl_basic_set_dim(bset, isl_dim_param);
521 nvar = isl_basic_set_dim(bset, isl_dim_set);
522 n_div = isl_basic_set_dim(bset, isl_dim_div);
524 if (isl_seq_first_non_zero(bset->eq[bset->n_eq - 1] + 1 + nparam,
525 nvar + n_div) == -1)
526 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
527 "input not allowed to have parameter equalities",
528 return NULL);
529 if (n_eq > nvar + n_div)
530 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
531 "input not gaussed", return NULL);
533 B = isl_mat_sub_alloc6(bset->ctx, bset->eq, 0, n_eq, 0, 1 + nparam);
534 H = isl_mat_sub_alloc6(bset->ctx, bset->eq,
535 0, n_eq, 1 + nparam, nvar + n_div);
536 inv = isl_mat_parameter_compression_ext(B, H);
537 inv = isl_mat_diagonal(inv, isl_mat_identity(bset->ctx, nvar));
538 map = isl_mat_right_inverse(isl_mat_copy(inv));
540 dom = isl_basic_set_universe(isl_space_copy(bset->dim));
541 ran = isl_basic_set_universe(isl_space_copy(bset->dim));
543 return isl_morph_alloc(dom, ran, map, inv);
546 /* Add stride constraints to "bset" based on the inverse mapping
547 * that was plugged in. In particular, if morph maps x' to x,
548 * the constraints of the original input
550 * A x' + b >= 0
552 * have been rewritten to
554 * A inv x + b >= 0
556 * However, this substitution may loose information on the integrality of x',
557 * so we need to impose that
559 * inv x
561 * is integral. If inv = B/d, this means that we need to impose that
563 * B x = 0 mod d
565 * or
567 * exists alpha in Z^m: B x = d alpha
569 * This function is similar to add_strides in isl_affine_hull.c
571 static __isl_give isl_basic_set *add_strides(__isl_take isl_basic_set *bset,
572 __isl_keep isl_morph *morph)
574 int i, div, k;
575 isl_int gcd;
577 if (isl_int_is_one(morph->inv->row[0][0]))
578 return bset;
580 isl_int_init(gcd);
582 for (i = 0; 1 + i < morph->inv->n_row; ++i) {
583 isl_seq_gcd(morph->inv->row[1 + i], morph->inv->n_col, &gcd);
584 if (isl_int_is_divisible_by(gcd, morph->inv->row[0][0]))
585 continue;
586 div = isl_basic_set_alloc_div(bset);
587 if (div < 0)
588 goto error;
589 isl_int_set_si(bset->div[div][0], 0);
590 k = isl_basic_set_alloc_equality(bset);
591 if (k < 0)
592 goto error;
593 isl_seq_cpy(bset->eq[k], morph->inv->row[1 + i],
594 morph->inv->n_col);
595 isl_seq_clr(bset->eq[k] + morph->inv->n_col, bset->n_div);
596 isl_int_set(bset->eq[k][morph->inv->n_col + div],
597 morph->inv->row[0][0]);
600 isl_int_clear(gcd);
602 return bset;
603 error:
604 isl_int_clear(gcd);
605 isl_basic_set_free(bset);
606 return NULL;
609 /* Apply the morphism to the basic set.
610 * We basically just compute the preimage of "bset" under the inverse mapping
611 * in morph, add in stride constraints and intersect with the range
612 * of the morphism.
614 __isl_give isl_basic_set *isl_morph_basic_set(__isl_take isl_morph *morph,
615 __isl_take isl_basic_set *bset)
617 isl_basic_set *res = NULL;
618 isl_mat *mat = NULL;
619 int i, k;
620 int max_stride;
622 if (!morph || !bset)
623 goto error;
625 isl_assert(bset->ctx, isl_space_is_equal(bset->dim, morph->dom->dim),
626 goto error);
628 max_stride = morph->inv->n_row - 1;
629 if (isl_int_is_one(morph->inv->row[0][0]))
630 max_stride = 0;
631 res = isl_basic_set_alloc_space(isl_space_copy(morph->ran->dim),
632 bset->n_div + max_stride, bset->n_eq + max_stride, bset->n_ineq);
634 for (i = 0; i < bset->n_div; ++i)
635 if (isl_basic_set_alloc_div(res) < 0)
636 goto error;
638 mat = isl_mat_sub_alloc6(bset->ctx, bset->eq, 0, bset->n_eq,
639 0, morph->inv->n_row);
640 mat = isl_mat_product(mat, isl_mat_copy(morph->inv));
641 if (!mat)
642 goto error;
643 for (i = 0; i < bset->n_eq; ++i) {
644 k = isl_basic_set_alloc_equality(res);
645 if (k < 0)
646 goto error;
647 isl_seq_cpy(res->eq[k], mat->row[i], mat->n_col);
648 isl_seq_scale(res->eq[k] + mat->n_col, bset->eq[i] + mat->n_col,
649 morph->inv->row[0][0], bset->n_div);
651 isl_mat_free(mat);
653 mat = isl_mat_sub_alloc6(bset->ctx, bset->ineq, 0, bset->n_ineq,
654 0, morph->inv->n_row);
655 mat = isl_mat_product(mat, isl_mat_copy(morph->inv));
656 if (!mat)
657 goto error;
658 for (i = 0; i < bset->n_ineq; ++i) {
659 k = isl_basic_set_alloc_inequality(res);
660 if (k < 0)
661 goto error;
662 isl_seq_cpy(res->ineq[k], mat->row[i], mat->n_col);
663 isl_seq_scale(res->ineq[k] + mat->n_col,
664 bset->ineq[i] + mat->n_col,
665 morph->inv->row[0][0], bset->n_div);
667 isl_mat_free(mat);
669 mat = isl_mat_sub_alloc6(bset->ctx, bset->div, 0, bset->n_div,
670 1, morph->inv->n_row);
671 mat = isl_mat_product(mat, isl_mat_copy(morph->inv));
672 if (!mat)
673 goto error;
674 for (i = 0; i < bset->n_div; ++i) {
675 isl_int_mul(res->div[i][0],
676 morph->inv->row[0][0], bset->div[i][0]);
677 isl_seq_cpy(res->div[i] + 1, mat->row[i], mat->n_col);
678 isl_seq_scale(res->div[i] + 1 + mat->n_col,
679 bset->div[i] + 1 + mat->n_col,
680 morph->inv->row[0][0], bset->n_div);
682 isl_mat_free(mat);
684 res = add_strides(res, morph);
686 if (isl_basic_set_is_rational(bset))
687 res = isl_basic_set_set_rational(res);
689 res = isl_basic_set_simplify(res);
690 res = isl_basic_set_finalize(res);
692 res = isl_basic_set_intersect(res, isl_basic_set_copy(morph->ran));
694 isl_morph_free(morph);
695 isl_basic_set_free(bset);
696 return res;
697 error:
698 isl_mat_free(mat);
699 isl_morph_free(morph);
700 isl_basic_set_free(bset);
701 isl_basic_set_free(res);
702 return NULL;
705 /* Apply the morphism to the set.
707 __isl_give isl_set *isl_morph_set(__isl_take isl_morph *morph,
708 __isl_take isl_set *set)
710 int i;
712 if (!morph || !set)
713 goto error;
715 isl_assert(set->ctx, isl_space_is_equal(set->dim, morph->dom->dim), goto error);
717 set = isl_set_cow(set);
718 if (!set)
719 goto error;
721 isl_space_free(set->dim);
722 set->dim = isl_space_copy(morph->ran->dim);
723 if (!set->dim)
724 goto error;
726 for (i = 0; i < set->n; ++i) {
727 set->p[i] = isl_morph_basic_set(isl_morph_copy(morph), set->p[i]);
728 if (!set->p[i])
729 goto error;
732 isl_morph_free(morph);
734 ISL_F_CLR(set, ISL_SET_NORMALIZED);
736 return set;
737 error:
738 isl_set_free(set);
739 isl_morph_free(morph);
740 return NULL;
743 /* Construct a morphism that first does morph2 and then morph1.
745 __isl_give isl_morph *isl_morph_compose(__isl_take isl_morph *morph1,
746 __isl_take isl_morph *morph2)
748 isl_mat *map, *inv;
749 isl_basic_set *dom, *ran;
751 if (!morph1 || !morph2)
752 goto error;
754 map = isl_mat_product(isl_mat_copy(morph1->map), isl_mat_copy(morph2->map));
755 inv = isl_mat_product(isl_mat_copy(morph2->inv), isl_mat_copy(morph1->inv));
756 dom = isl_morph_basic_set(isl_morph_inverse(isl_morph_copy(morph2)),
757 isl_basic_set_copy(morph1->dom));
758 dom = isl_basic_set_intersect(dom, isl_basic_set_copy(morph2->dom));
759 ran = isl_morph_basic_set(isl_morph_copy(morph1),
760 isl_basic_set_copy(morph2->ran));
761 ran = isl_basic_set_intersect(ran, isl_basic_set_copy(morph1->ran));
763 isl_morph_free(morph1);
764 isl_morph_free(morph2);
766 return isl_morph_alloc(dom, ran, map, inv);
767 error:
768 isl_morph_free(morph1);
769 isl_morph_free(morph2);
770 return NULL;
773 __isl_give isl_morph *isl_morph_inverse(__isl_take isl_morph *morph)
775 isl_basic_set *bset;
776 isl_mat *mat;
778 morph = isl_morph_cow(morph);
779 if (!morph)
780 return NULL;
782 bset = morph->dom;
783 morph->dom = morph->ran;
784 morph->ran = bset;
786 mat = morph->map;
787 morph->map = morph->inv;
788 morph->inv = mat;
790 return morph;
793 /* We detect all the equalities first to avoid implicit equalities
794 * being discovered during the computations. In particular,
795 * the compression on the variables could expose additional stride
796 * constraints on the parameters. This would result in existentially
797 * quantified variables after applying the resulting morph, which
798 * in turn could break invariants of the calling functions.
800 __isl_give isl_morph *isl_basic_set_full_compression(
801 __isl_keep isl_basic_set *bset)
803 isl_morph *morph, *morph2;
805 bset = isl_basic_set_copy(bset);
806 bset = isl_basic_set_detect_equalities(bset);
808 morph = isl_basic_set_variable_compression(bset, isl_dim_param);
809 bset = isl_morph_basic_set(isl_morph_copy(morph), bset);
811 morph2 = isl_basic_set_parameter_compression(bset);
812 bset = isl_morph_basic_set(isl_morph_copy(morph2), bset);
814 morph = isl_morph_compose(morph2, morph);
816 morph2 = isl_basic_set_variable_compression(bset, isl_dim_set);
817 isl_basic_set_free(bset);
819 morph = isl_morph_compose(morph2, morph);
821 return morph;
824 __isl_give isl_vec *isl_morph_vec(__isl_take isl_morph *morph,
825 __isl_take isl_vec *vec)
827 if (!morph)
828 goto error;
830 vec = isl_mat_vec_product(isl_mat_copy(morph->map), vec);
832 isl_morph_free(morph);
833 return vec;
834 error:
835 isl_morph_free(morph);
836 isl_vec_free(vec);
837 return NULL;