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,
11 #include <isl_morph.h>
13 #include <isl_map_private.h>
14 #include <isl_dim_private.h>
15 #include <isl_equalities.h>
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
)
23 if (!dom
|| !ran
|| !map
|| !inv
)
26 morph
= isl_alloc_type(in_dim
->ctx
, struct isl_morph
);
38 isl_basic_set_free(dom
);
39 isl_basic_set_free(ran
);
45 __isl_give isl_morph
*isl_morph_copy(__isl_keep isl_morph
*morph
)
54 __isl_give isl_morph
*isl_morph_dup(__isl_keep isl_morph
*morph
)
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
));
64 __isl_give isl_morph
*isl_morph_cow(__isl_take isl_morph
*morph
)
72 return isl_morph_dup(morph
);
75 void isl_morph_free(__isl_take isl_morph
*morph
)
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
);
90 __isl_give isl_dim
*isl_morph_get_ran_dim(__isl_keep isl_morph
*morph
)
95 return isl_dim_copy(morph
->ran
->dim
);
98 __isl_give isl_morph
*isl_morph_remove_dom_dims(__isl_take isl_morph
*morph
,
99 enum isl_dim_type type
, unsigned first
, unsigned n
)
106 morph
= isl_morph_cow(morph
);
110 dom_offset
= 1 + isl_dim_offset(morph
->dom
->dim
, type
);
112 morph
->dom
= isl_basic_set_remove(morph
->dom
, type
, first
, n
);
114 morph
->map
= isl_mat_drop_cols(morph
->map
, dom_offset
+ first
, n
);
116 morph
->inv
= isl_mat_drop_rows(morph
->inv
, dom_offset
+ first
, n
);
118 if (morph
->dom
&& morph
->ran
&& morph
->map
&& morph
->inv
)
121 isl_morph_free(morph
);
125 __isl_give isl_morph
*isl_morph_remove_ran_dims(__isl_take isl_morph
*morph
,
126 enum isl_dim_type type
, unsigned first
, unsigned n
)
133 morph
= isl_morph_cow(morph
);
137 ran_offset
= 1 + isl_dim_offset(morph
->ran
->dim
, type
);
139 morph
->ran
= isl_basic_set_remove(morph
->ran
, type
, first
, n
);
141 morph
->map
= isl_mat_drop_rows(morph
->map
, ran_offset
+ first
, n
);
143 morph
->inv
= isl_mat_drop_cols(morph
->inv
, ran_offset
+ first
, n
);
145 if (morph
->dom
&& morph
->ran
&& morph
->map
&& morph
->inv
)
148 isl_morph_free(morph
);
152 void isl_morph_dump(__isl_take isl_morph
*morph
, FILE *out
)
157 isl_basic_set_print(morph
->dom
, out
, 0, "", "", ISL_FORMAT_ISL
);
158 isl_basic_set_print(morph
->ran
, out
, 0, "", "", ISL_FORMAT_ISL
);
159 isl_mat_dump(morph
->map
, out
, 4);
160 isl_mat_dump(morph
->inv
, out
, 4);
163 __isl_give isl_morph
*isl_morph_identity(__isl_keep isl_basic_set
*bset
)
166 isl_basic_set
*universe
;
172 total
= isl_basic_set_total_dim(bset
);
173 id
= isl_mat_identity(bset
->ctx
, 1 + total
);
174 universe
= isl_basic_set_universe(isl_dim_copy(bset
->dim
));
176 return isl_morph_alloc(universe
, isl_basic_set_copy(universe
),
177 id
, isl_mat_copy(id
));
180 /* Create a(n identity) morphism between empty sets of the same dimension
183 __isl_give isl_morph
*isl_morph_empty(__isl_keep isl_basic_set
*bset
)
186 isl_basic_set
*empty
;
192 total
= isl_basic_set_total_dim(bset
);
193 id
= isl_mat_identity(bset
->ctx
, 1 + total
);
194 empty
= isl_basic_set_empty(isl_dim_copy(bset
->dim
));
196 return isl_morph_alloc(empty
, isl_basic_set_copy(empty
),
197 id
, isl_mat_copy(id
));
200 /* Given a matrix that maps a (possibly) parametric domain to
201 * a parametric domain, add in rows that map the "nparam" parameters onto
204 static __isl_give isl_mat
*insert_parameter_rows(__isl_take isl_mat
*mat
,
214 mat
= isl_mat_insert_rows(mat
, 1, nparam
);
218 for (i
= 0; i
< nparam
; ++i
) {
219 isl_seq_clr(mat
->row
[1 + i
], mat
->n_col
);
220 isl_int_set(mat
->row
[1 + i
][1 + i
], mat
->row
[0][0]);
226 /* Construct a basic set described by the "n" equalities of "bset" starting
229 static __isl_give isl_basic_set
*copy_equalities(__isl_keep isl_basic_set
*bset
,
230 unsigned first
, unsigned n
)
236 isl_assert(bset
->ctx
, bset
->n_div
== 0, return NULL
);
238 total
= isl_basic_set_total_dim(bset
);
239 eq
= isl_basic_set_alloc_dim(isl_dim_copy(bset
->dim
), 0, n
, 0);
242 for (i
= 0; i
< n
; ++i
) {
243 k
= isl_basic_set_alloc_equality(eq
);
246 isl_seq_cpy(eq
->eq
[k
], bset
->eq
[first
+ k
], 1 + total
);
251 isl_basic_set_free(eq
);
255 /* Given a basic set, exploit the equalties in the a basic set to construct
256 * a morphishm that maps the basic set to a lower-dimensional space.
257 * Specifically, the morphism reduces the number of dimensions of type "type".
259 * This function is a slight generalization of isl_mat_variable_compression
260 * in that it allows the input to be parametric and that it allows for the
261 * compression of either parameters or set variables.
263 * We first select the equalities of interest, that is those that involve
264 * variables of type "type" and no later variables.
265 * Denote those equalities as
269 * where C(p) depends on the parameters if type == isl_dim_set and
270 * is a constant if type == isl_dim_param.
272 * First compute the (left) Hermite normal form of M,
274 * M [U1 U2] = M U = H = [H1 0]
276 * M = H Q = [H1 0] [Q1]
279 * with U, Q unimodular, Q = U^{-1} (and H lower triangular).
280 * Define the transformed variables as
282 * x = [U1 U2] [ x1' ] = [U1 U2] [Q1] x
285 * The equalities then become
287 * -C(p) + H1 x1' = 0 or x1' = H1^{-1} C(p) = C'(p)
289 * If the denominator of the constant term does not divide the
290 * the common denominator of the parametric terms, then every
291 * integer point is mapped to a non-integer point and then the original set has no
292 * integer solutions (since the x' are a unimodular transformation
293 * of the x). In this case, an empty morphism is returned.
294 * Otherwise, the transformation is given by
296 * x = U1 H1^{-1} C(p) + U2 x2'
298 * The inverse transformation is simply
302 * Both matrices are extended to map the full original space to the full
305 __isl_give isl_morph
*isl_basic_set_variable_compression(
306 __isl_keep isl_basic_set
*bset
, enum isl_dim_type type
)
315 isl_mat
*H
, *U
, *Q
, *C
= NULL
, *H1
, *U1
, *U2
;
316 isl_basic_set
*dom
, *ran
;
321 if (isl_basic_set_fast_is_empty(bset
))
322 return isl_morph_empty(bset
);
324 isl_assert(bset
->ctx
, bset
->n_div
== 0, return NULL
);
326 otype
= 1 + isl_dim_offset(bset
->dim
, type
);
327 ntype
= isl_basic_set_dim(bset
, type
);
328 orest
= otype
+ ntype
;
329 nrest
= isl_basic_set_total_dim(bset
) - (orest
- 1);
331 for (f_eq
= 0; f_eq
< bset
->n_eq
; ++f_eq
)
332 if (isl_seq_first_non_zero(bset
->eq
[f_eq
] + orest
, nrest
) == -1)
334 for (n_eq
= 0; f_eq
+ n_eq
< bset
->n_eq
; ++n_eq
)
335 if (isl_seq_first_non_zero(bset
->eq
[f_eq
+ n_eq
] + otype
, ntype
) == -1)
338 return isl_morph_identity(bset
);
340 H
= isl_mat_sub_alloc(bset
->ctx
, bset
->eq
, f_eq
, n_eq
, otype
, ntype
);
341 H
= isl_mat_left_hermite(H
, 0, &U
, &Q
);
344 Q
= isl_mat_drop_rows(Q
, 0, n_eq
);
345 Q
= isl_mat_diagonal(isl_mat_identity(bset
->ctx
, otype
), Q
);
346 Q
= isl_mat_diagonal(Q
, isl_mat_identity(bset
->ctx
, nrest
));
347 C
= isl_mat_alloc(bset
->ctx
, 1 + n_eq
, otype
);
350 isl_int_set_si(C
->row
[0][0], 1);
351 isl_seq_clr(C
->row
[0] + 1, otype
- 1);
352 isl_mat_sub_neg(C
->ctx
, C
->row
+ 1, bset
->eq
+ f_eq
, n_eq
, 0, 0, otype
);
353 H1
= isl_mat_sub_alloc(H
->ctx
, H
->row
, 0, H
->n_row
, 0, H
->n_row
);
354 H1
= isl_mat_lin_to_aff(H1
);
355 C
= isl_mat_inverse_product(H1
, C
);
360 if (!isl_int_is_one(C
->row
[0][0])) {
365 for (i
= 0; i
< n_eq
; ++i
) {
366 isl_seq_gcd(C
->row
[1 + i
] + 1, otype
- 1, &g
);
367 isl_int_gcd(g
, g
, C
->row
[0][0]);
368 if (!isl_int_is_divisible_by(C
->row
[1 + i
][0], g
))
377 return isl_morph_empty(bset
);
380 C
= isl_mat_normalize(C
);
383 U1
= isl_mat_sub_alloc(U
->ctx
, U
->row
, 0, U
->n_row
, 0, n_eq
);
384 U1
= isl_mat_lin_to_aff(U1
);
385 U2
= isl_mat_sub_alloc(U
->ctx
, U
->row
, 0, U
->n_row
, n_eq
, U
->n_row
- n_eq
);
386 U2
= isl_mat_lin_to_aff(U2
);
389 C
= isl_mat_product(U1
, C
);
390 C
= isl_mat_aff_direct_sum(C
, U2
);
391 C
= insert_parameter_rows(C
, otype
- 1);
392 C
= isl_mat_diagonal(C
, isl_mat_identity(bset
->ctx
, nrest
));
394 dim
= isl_dim_copy(bset
->dim
);
395 dim
= isl_dim_drop(dim
, type
, 0, ntype
);
396 dim
= isl_dim_add(dim
, type
, ntype
- n_eq
);
397 ran
= isl_basic_set_universe(dim
);
398 dom
= copy_equalities(bset
, f_eq
, n_eq
);
400 return isl_morph_alloc(dom
, ran
, Q
, C
);
409 /* Construct a parameter compression for "bset".
410 * We basically just call isl_mat_parameter_compression with the right input
411 * and then extend the resulting matrix to include the variables.
413 * Let the equalities be given as
417 * and let [H 0] be the Hermite Normal Form of A, then
421 * needs to be integer, so we impose that each row is divisible by
424 __isl_give isl_morph
*isl_basic_set_parameter_compression(
425 __isl_keep isl_basic_set
*bset
)
433 isl_basic_set
*dom
, *ran
;
438 if (isl_basic_set_fast_is_empty(bset
))
439 return isl_morph_empty(bset
);
441 return isl_morph_identity(bset
);
443 isl_assert(bset
->ctx
, bset
->n_div
== 0, return NULL
);
446 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
447 nvar
= isl_basic_set_dim(bset
, isl_dim_set
);
449 isl_assert(bset
->ctx
, n_eq
<= nvar
, return NULL
);
451 d
= isl_vec_alloc(bset
->ctx
, n_eq
);
452 B
= isl_mat_sub_alloc(bset
->ctx
, bset
->eq
, 0, n_eq
, 0, 1 + nparam
);
453 H
= isl_mat_sub_alloc(bset
->ctx
, bset
->eq
, 0, n_eq
, 1 + nparam
, nvar
);
454 H
= isl_mat_left_hermite(H
, 0, NULL
, NULL
);
455 H
= isl_mat_drop_cols(H
, n_eq
, nvar
- n_eq
);
456 H
= isl_mat_lin_to_aff(H
);
457 H
= isl_mat_right_inverse(H
);
460 isl_seq_set(d
->el
, H
->row
[0][0], d
->size
);
461 H
= isl_mat_drop_rows(H
, 0, 1);
462 H
= isl_mat_drop_cols(H
, 0, 1);
463 B
= isl_mat_product(H
, B
);
464 inv
= isl_mat_parameter_compression(B
, d
);
465 inv
= isl_mat_diagonal(inv
, isl_mat_identity(bset
->ctx
, nvar
));
466 map
= isl_mat_right_inverse(isl_mat_copy(inv
));
468 dom
= isl_basic_set_universe(isl_dim_copy(bset
->dim
));
469 ran
= isl_basic_set_universe(isl_dim_copy(bset
->dim
));
471 return isl_morph_alloc(dom
, ran
, map
, inv
);
479 /* Add stride constraints to "bset" based on the inverse mapping
480 * that was plugged in. In particular, if morph maps x' to x,
481 * the the constraints of the original input
485 * have been rewritten to
489 * However, this substitution may loose information on the integrality of x',
490 * so we need to impose that
494 * is integral. If inv = B/d, this means that we need to impose that
500 * exists alpha in Z^m: B x = d alpha
503 static __isl_give isl_basic_set
*add_strides(__isl_take isl_basic_set
*bset
,
504 __isl_keep isl_morph
*morph
)
509 if (isl_int_is_one(morph
->inv
->row
[0][0]))
514 for (i
= 0; 1 + i
< morph
->inv
->n_row
; ++i
) {
515 isl_seq_gcd(morph
->inv
->row
[1 + i
], morph
->inv
->n_col
, &gcd
);
516 if (isl_int_is_divisible_by(gcd
, morph
->inv
->row
[0][0]))
518 div
= isl_basic_set_alloc_div(bset
);
521 k
= isl_basic_set_alloc_equality(bset
);
524 isl_seq_cpy(bset
->eq
[k
], morph
->inv
->row
[1 + i
],
526 isl_seq_clr(bset
->eq
[k
] + morph
->inv
->n_col
, bset
->n_div
);
527 isl_int_set(bset
->eq
[k
][morph
->inv
->n_col
+ div
],
528 morph
->inv
->row
[0][0]);
536 isl_basic_set_free(bset
);
540 /* Apply the morphism to the basic set.
541 * We basically just compute the preimage of "bset" under the inverse mapping
542 * in morph, add in stride constraints and intersect with the range
545 __isl_give isl_basic_set
*isl_morph_basic_set(__isl_take isl_morph
*morph
,
546 __isl_take isl_basic_set
*bset
)
548 isl_basic_set
*res
= NULL
;
556 isl_assert(bset
->ctx
, isl_dim_equal(bset
->dim
, morph
->dom
->dim
),
559 max_stride
= morph
->inv
->n_row
- 1;
560 if (isl_int_is_one(morph
->inv
->row
[0][0]))
562 res
= isl_basic_set_alloc_dim(isl_dim_copy(morph
->ran
->dim
),
563 bset
->n_div
+ max_stride
, bset
->n_eq
+ max_stride
, bset
->n_ineq
);
565 for (i
= 0; i
< bset
->n_div
; ++i
)
566 if (isl_basic_set_alloc_div(res
) < 0)
569 mat
= isl_mat_sub_alloc(bset
->ctx
, bset
->eq
, 0, bset
->n_eq
,
570 0, morph
->inv
->n_row
);
571 mat
= isl_mat_product(mat
, isl_mat_copy(morph
->inv
));
574 for (i
= 0; i
< bset
->n_eq
; ++i
) {
575 k
= isl_basic_set_alloc_equality(res
);
578 isl_seq_cpy(res
->eq
[k
], mat
->row
[i
], mat
->n_col
);
579 isl_seq_scale(res
->eq
[k
] + mat
->n_col
, bset
->eq
[i
] + mat
->n_col
,
580 morph
->inv
->row
[0][0], bset
->n_div
);
584 mat
= isl_mat_sub_alloc(bset
->ctx
, bset
->ineq
, 0, bset
->n_ineq
,
585 0, morph
->inv
->n_row
);
586 mat
= isl_mat_product(mat
, isl_mat_copy(morph
->inv
));
589 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
590 k
= isl_basic_set_alloc_inequality(res
);
593 isl_seq_cpy(res
->ineq
[k
], mat
->row
[i
], mat
->n_col
);
594 isl_seq_scale(res
->ineq
[k
] + mat
->n_col
,
595 bset
->ineq
[i
] + mat
->n_col
,
596 morph
->inv
->row
[0][0], bset
->n_div
);
600 mat
= isl_mat_sub_alloc(bset
->ctx
, bset
->div
, 0, bset
->n_div
,
601 1, morph
->inv
->n_row
);
602 mat
= isl_mat_product(mat
, isl_mat_copy(morph
->inv
));
605 for (i
= 0; i
< bset
->n_div
; ++i
) {
606 isl_int_mul(res
->div
[i
][0],
607 morph
->inv
->row
[0][0], bset
->div
[i
][0]);
608 isl_seq_cpy(res
->div
[i
] + 1, mat
->row
[i
], mat
->n_col
);
609 isl_seq_scale(res
->div
[i
] + 1 + mat
->n_col
,
610 bset
->div
[i
] + 1 + mat
->n_col
,
611 morph
->inv
->row
[0][0], bset
->n_div
);
615 res
= add_strides(res
, morph
);
617 res
= isl_basic_set_simplify(res
);
618 res
= isl_basic_set_finalize(res
);
620 res
= isl_basic_set_intersect(res
, isl_basic_set_copy(morph
->ran
));
622 isl_morph_free(morph
);
623 isl_basic_set_free(bset
);
627 isl_morph_free(morph
);
628 isl_basic_set_free(bset
);
629 isl_basic_set_free(res
);
633 /* Apply the morphism to the set.
635 __isl_give isl_set
*isl_morph_set(__isl_take isl_morph
*morph
,
636 __isl_take isl_set
*set
)
643 isl_assert(set
->ctx
, isl_dim_equal(set
->dim
, morph
->dom
->dim
), goto error
);
645 set
= isl_set_cow(set
);
649 isl_dim_free(set
->dim
);
650 set
->dim
= isl_dim_copy(morph
->ran
->dim
);
654 for (i
= 0; i
< set
->n
; ++i
) {
655 set
->p
[i
] = isl_morph_basic_set(isl_morph_copy(morph
), set
->p
[i
]);
660 isl_morph_free(morph
);
662 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
667 isl_morph_free(morph
);
671 /* Construct a morphism that first does morph2 and then morph1.
673 __isl_give isl_morph
*isl_morph_compose(__isl_take isl_morph
*morph1
,
674 __isl_take isl_morph
*morph2
)
677 isl_basic_set
*dom
, *ran
;
679 if (!morph1
|| !morph2
)
682 map
= isl_mat_product(isl_mat_copy(morph1
->map
), isl_mat_copy(morph2
->map
));
683 inv
= isl_mat_product(isl_mat_copy(morph2
->inv
), isl_mat_copy(morph1
->inv
));
684 dom
= isl_morph_basic_set(isl_morph_inverse(isl_morph_copy(morph2
)),
685 isl_basic_set_copy(morph1
->dom
));
686 dom
= isl_basic_set_intersect(dom
, isl_basic_set_copy(morph2
->dom
));
687 ran
= isl_morph_basic_set(isl_morph_copy(morph1
),
688 isl_basic_set_copy(morph2
->ran
));
689 ran
= isl_basic_set_intersect(ran
, isl_basic_set_copy(morph1
->ran
));
691 isl_morph_free(morph1
);
692 isl_morph_free(morph2
);
694 return isl_morph_alloc(dom
, ran
, map
, inv
);
696 isl_morph_free(morph1
);
697 isl_morph_free(morph2
);
701 __isl_give isl_morph
*isl_morph_inverse(__isl_take isl_morph
*morph
)
706 morph
= isl_morph_cow(morph
);
711 morph
->dom
= morph
->ran
;
715 morph
->map
= morph
->inv
;