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 static __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_drop_dims(__isl_take isl_morph
*morph
,
99 enum isl_dim_type type
, unsigned first
, unsigned n
)
107 morph
= isl_morph_cow(morph
);
111 dom_offset
= 1 + isl_dim_offset(morph
->dom
->dim
, type
);
112 ran_offset
= 1 + isl_dim_offset(morph
->ran
->dim
, type
);
114 morph
->dom
= isl_basic_set_drop(morph
->dom
, type
, first
, n
);
115 morph
->ran
= isl_basic_set_drop(morph
->ran
, type
, first
, n
);
117 morph
->map
= isl_mat_drop_cols(morph
->map
, dom_offset
+ first
, n
);
118 morph
->map
= isl_mat_drop_rows(morph
->map
, ran_offset
+ first
, n
);
120 morph
->inv
= isl_mat_drop_cols(morph
->inv
, ran_offset
+ first
, n
);
121 morph
->inv
= isl_mat_drop_rows(morph
->inv
, dom_offset
+ first
, n
);
123 if (morph
->dom
&& morph
->ran
&& morph
->map
&& morph
->inv
)
126 isl_morph_free(morph
);
130 void isl_morph_dump(__isl_take isl_morph
*morph
, FILE *out
)
135 isl_basic_set_print(morph
->dom
, out
, 0, "", "", ISL_FORMAT_ISL
);
136 isl_basic_set_print(morph
->ran
, out
, 0, "", "", ISL_FORMAT_ISL
);
137 isl_mat_dump(morph
->map
, out
, 4);
138 isl_mat_dump(morph
->inv
, out
, 4);
141 __isl_give isl_morph
*isl_morph_identity(__isl_keep isl_basic_set
*bset
)
144 isl_basic_set
*universe
;
150 total
= isl_basic_set_total_dim(bset
);
151 id
= isl_mat_identity(bset
->ctx
, 1 + total
);
152 universe
= isl_basic_set_universe(isl_dim_copy(bset
->dim
));
154 return isl_morph_alloc(universe
, isl_basic_set_copy(universe
),
155 id
, isl_mat_copy(id
));
158 /* Create a(n identity) morphism between empty sets of the same dimension
161 __isl_give isl_morph
*isl_morph_empty(__isl_keep isl_basic_set
*bset
)
164 isl_basic_set
*empty
;
170 total
= isl_basic_set_total_dim(bset
);
171 id
= isl_mat_identity(bset
->ctx
, 1 + total
);
172 empty
= isl_basic_set_empty(isl_dim_copy(bset
->dim
));
174 return isl_morph_alloc(empty
, isl_basic_set_copy(empty
),
175 id
, isl_mat_copy(id
));
178 /* Given a matrix that maps a (possibly) parametric domain to
179 * a parametric domain, add in rows that map the "nparam" parameters onto
182 static __isl_give isl_mat
*insert_parameter_rows(__isl_take isl_mat
*mat
,
192 mat
= isl_mat_insert_rows(mat
, 1, nparam
);
196 for (i
= 0; i
< nparam
; ++i
) {
197 isl_seq_clr(mat
->row
[1 + i
], mat
->n_col
);
198 isl_int_set(mat
->row
[1 + i
][1 + i
], mat
->row
[0][0]);
204 /* Construct a basic set described by the "n" equalities of "bset" starting
207 static __isl_give isl_basic_set
*copy_equalities(__isl_keep isl_basic_set
*bset
,
208 unsigned first
, unsigned n
)
214 isl_assert(bset
->ctx
, bset
->n_div
== 0, return NULL
);
216 total
= isl_basic_set_total_dim(bset
);
217 eq
= isl_basic_set_alloc_dim(isl_dim_copy(bset
->dim
), 0, n
, 0);
220 for (i
= 0; i
< n
; ++i
) {
221 k
= isl_basic_set_alloc_equality(eq
);
224 isl_seq_cpy(eq
->eq
[k
], bset
->eq
[first
+ k
], 1 + total
);
229 isl_basic_set_free(eq
);
233 /* Given a basic set, exploit the equalties in the a basic set to construct
234 * a morphishm that maps the basic set to a lower-dimensional space.
235 * Specifically, the morphism reduces the number of dimensions of type "type".
237 * This function is a slight generalization of isl_mat_variable_compression
238 * in that it allows the input to be parametric and that it allows for the
239 * compression of either parameters or set variables.
241 * We first select the equalities of interest, that is those that involve
242 * variables of type "type" and no later variables.
243 * Denote those equalities as
247 * where C(p) depends on the parameters if type == isl_dim_set and
248 * is a constant if type == isl_dim_param.
250 * First compute the (left) Hermite normal form of M,
252 * M [U1 U2] = M U = H = [H1 0]
254 * M = H Q = [H1 0] [Q1]
257 * with U, Q unimodular, Q = U^{-1} (and H lower triangular).
258 * Define the transformed variables as
260 * x = [U1 U2] [ x1' ] = [U1 U2] [Q1] x
263 * The equalities then become
265 * -C(p) + H1 x1' = 0 or x1' = H1^{-1} C(p) = C'(p)
267 * If the denominator of the constant term does not divide the
268 * the common denominator of the parametric terms, then every
269 * integer point is mapped to a non-integer point and then the original set has no
270 * integer solutions (since the x' are a unimodular transformation
271 * of the x). In this case, an empty morphism is returned.
272 * Otherwise, the transformation is given by
274 * x = U1 H1^{-1} C(p) + U2 x2'
276 * The inverse transformation is simply
280 * Both matrices are extended to map the full original space to the full
283 __isl_give isl_morph
*isl_basic_set_variable_compression(
284 __isl_keep isl_basic_set
*bset
, enum isl_dim_type type
)
293 isl_mat
*H
, *U
, *Q
, *C
= NULL
, *H1
, *U1
, *U2
;
294 isl_basic_set
*dom
, *ran
;
299 if (isl_basic_set_fast_is_empty(bset
))
300 return isl_morph_empty(bset
);
302 isl_assert(bset
->ctx
, bset
->n_div
== 0, return NULL
);
304 otype
= 1 + isl_dim_offset(bset
->dim
, type
);
305 ntype
= isl_basic_set_dim(bset
, type
);
306 orest
= otype
+ ntype
;
307 nrest
= isl_basic_set_total_dim(bset
) - (orest
- 1);
309 for (f_eq
= 0; f_eq
< bset
->n_eq
; ++f_eq
)
310 if (isl_seq_first_non_zero(bset
->eq
[f_eq
] + orest
, nrest
) == -1)
312 for (n_eq
= 0; f_eq
+ n_eq
< bset
->n_eq
; ++n_eq
)
313 if (isl_seq_first_non_zero(bset
->eq
[f_eq
+ n_eq
] + otype
, ntype
) == -1)
316 return isl_morph_identity(bset
);
318 H
= isl_mat_sub_alloc(bset
->ctx
, bset
->eq
, f_eq
, n_eq
, otype
, ntype
);
319 H
= isl_mat_left_hermite(H
, 0, &U
, &Q
);
322 Q
= isl_mat_drop_rows(Q
, 0, n_eq
);
323 Q
= isl_mat_diagonal(isl_mat_identity(bset
->ctx
, otype
), Q
);
324 Q
= isl_mat_diagonal(Q
, isl_mat_identity(bset
->ctx
, nrest
));
325 C
= isl_mat_alloc(bset
->ctx
, 1 + n_eq
, otype
);
328 isl_int_set_si(C
->row
[0][0], 1);
329 isl_seq_clr(C
->row
[0] + 1, otype
- 1);
330 isl_mat_sub_neg(C
->ctx
, C
->row
+ 1, bset
->eq
+ f_eq
, n_eq
, 0, 0, otype
);
331 H1
= isl_mat_sub_alloc(H
->ctx
, H
->row
, 0, H
->n_row
, 0, H
->n_row
);
332 H1
= isl_mat_lin_to_aff(H1
);
333 C
= isl_mat_inverse_product(H1
, C
);
338 if (!isl_int_is_one(C
->row
[0][0])) {
343 for (i
= 0; i
< n_eq
; ++i
) {
344 isl_seq_gcd(C
->row
[1 + i
] + 1, otype
- 1, &g
);
345 isl_int_gcd(g
, g
, C
->row
[0][0]);
346 if (!isl_int_is_divisible_by(C
->row
[1 + i
][0], g
))
355 return isl_morph_empty(bset
);
358 C
= isl_mat_normalize(C
);
361 U1
= isl_mat_sub_alloc(U
->ctx
, U
->row
, 0, U
->n_row
, 0, n_eq
);
362 U1
= isl_mat_lin_to_aff(U1
);
363 U2
= isl_mat_sub_alloc(U
->ctx
, U
->row
, 0, U
->n_row
, n_eq
, U
->n_row
- n_eq
);
364 U2
= isl_mat_lin_to_aff(U2
);
367 C
= isl_mat_product(U1
, C
);
368 C
= isl_mat_aff_direct_sum(C
, U2
);
369 C
= insert_parameter_rows(C
, otype
- 1);
370 C
= isl_mat_diagonal(C
, isl_mat_identity(bset
->ctx
, nrest
));
372 dim
= isl_dim_copy(bset
->dim
);
373 dim
= isl_dim_drop(dim
, type
, 0, ntype
);
374 dim
= isl_dim_add(dim
, type
, ntype
- n_eq
);
375 ran
= isl_basic_set_universe(dim
);
376 dom
= copy_equalities(bset
, f_eq
, n_eq
);
378 return isl_morph_alloc(dom
, ran
, Q
, C
);
387 /* Construct a parameter compression for "bset".
388 * We basically just call isl_mat_parameter_compression with the right input
389 * and then extend the resulting matrix to include the variables.
391 * Let the equalities be given as
395 * and let [H 0] be the Hermite Normal Form of A, then
399 * needs to be integer, so we impose that each row is divisible by
402 __isl_give isl_morph
*isl_basic_set_parameter_compression(
403 __isl_keep isl_basic_set
*bset
)
411 isl_basic_set
*dom
, *ran
;
416 if (isl_basic_set_fast_is_empty(bset
))
417 return isl_morph_empty(bset
);
419 return isl_morph_identity(bset
);
421 isl_assert(bset
->ctx
, bset
->n_div
== 0, return NULL
);
424 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
425 nvar
= isl_basic_set_dim(bset
, isl_dim_set
);
427 isl_assert(bset
->ctx
, n_eq
<= nvar
, return NULL
);
429 d
= isl_vec_alloc(bset
->ctx
, n_eq
);
430 B
= isl_mat_sub_alloc(bset
->ctx
, bset
->eq
, 0, n_eq
, 0, 1 + nparam
);
431 H
= isl_mat_sub_alloc(bset
->ctx
, bset
->eq
, 0, n_eq
, 1 + nparam
, nvar
);
432 H
= isl_mat_left_hermite(H
, 0, NULL
, NULL
);
433 H
= isl_mat_drop_cols(H
, n_eq
, nvar
- n_eq
);
434 H
= isl_mat_lin_to_aff(H
);
435 H
= isl_mat_right_inverse(H
);
438 isl_seq_set(d
->el
, H
->row
[0][0], d
->size
);
439 H
= isl_mat_drop_rows(H
, 0, 1);
440 H
= isl_mat_drop_cols(H
, 0, 1);
441 B
= isl_mat_product(H
, B
);
442 inv
= isl_mat_parameter_compression(B
, d
);
443 inv
= isl_mat_diagonal(inv
, isl_mat_identity(bset
->ctx
, nvar
));
444 map
= isl_mat_right_inverse(isl_mat_copy(inv
));
446 dom
= isl_basic_set_universe(isl_dim_copy(bset
->dim
));
447 ran
= isl_basic_set_universe(isl_dim_copy(bset
->dim
));
449 return isl_morph_alloc(dom
, ran
, map
, inv
);
457 /* Add stride constraints to "bset" based on the inverse mapping
458 * that was plugged in. In particular, if morph maps x' to x,
459 * the the constraints of the original input
463 * have been rewritten to
467 * However, this substitution may loose information on the integrality of x',
468 * so we need to impose that
472 * is integral. If inv = B/d, this means that we need to impose that
478 * exists alpha in Z^m: B x = d alpha
481 static __isl_give isl_basic_set
*add_strides(__isl_take isl_basic_set
*bset
,
482 __isl_keep isl_morph
*morph
)
487 if (isl_int_is_one(morph
->inv
->row
[0][0]))
492 for (i
= 0; 1 + i
< morph
->inv
->n_row
; ++i
) {
493 isl_seq_gcd(morph
->inv
->row
[1 + i
], morph
->inv
->n_col
, &gcd
);
494 if (isl_int_is_divisible_by(gcd
, morph
->inv
->row
[0][0]))
496 div
= isl_basic_set_alloc_div(bset
);
499 k
= isl_basic_set_alloc_equality(bset
);
502 isl_seq_cpy(bset
->eq
[k
], morph
->inv
->row
[1 + i
],
504 isl_seq_clr(bset
->eq
[k
] + morph
->inv
->n_col
, bset
->n_div
);
505 isl_int_set(bset
->eq
[k
][morph
->inv
->n_col
+ div
],
506 morph
->inv
->row
[0][0]);
514 isl_basic_set_free(bset
);
518 /* Apply the morphism to the basic set.
519 * We basically just compute the preimage of "bset" under the inverse mapping
520 * in morph, add in stride constraints and intersect with the range
523 __isl_give isl_basic_set
*isl_morph_basic_set(__isl_take isl_morph
*morph
,
524 __isl_take isl_basic_set
*bset
)
526 isl_basic_set
*res
= NULL
;
534 isl_assert(bset
->ctx
, isl_dim_equal(bset
->dim
, morph
->dom
->dim
),
537 max_stride
= morph
->inv
->n_row
- 1;
538 if (isl_int_is_one(morph
->inv
->row
[0][0]))
540 res
= isl_basic_set_alloc_dim(isl_dim_copy(morph
->ran
->dim
),
541 bset
->n_div
+ max_stride
, bset
->n_eq
+ max_stride
, bset
->n_ineq
);
543 for (i
= 0; i
< bset
->n_div
; ++i
)
544 if (isl_basic_set_alloc_div(res
) < 0)
547 mat
= isl_mat_sub_alloc(bset
->ctx
, bset
->eq
, 0, bset
->n_eq
,
548 0, morph
->inv
->n_row
);
549 mat
= isl_mat_product(mat
, isl_mat_copy(morph
->inv
));
552 for (i
= 0; i
< bset
->n_eq
; ++i
) {
553 k
= isl_basic_set_alloc_equality(res
);
556 isl_seq_cpy(res
->eq
[k
], mat
->row
[i
], mat
->n_col
);
557 isl_seq_scale(res
->eq
[k
] + mat
->n_col
, bset
->eq
[i
] + mat
->n_col
,
558 morph
->inv
->row
[0][0], bset
->n_div
);
562 mat
= isl_mat_sub_alloc(bset
->ctx
, bset
->ineq
, 0, bset
->n_ineq
,
563 0, morph
->inv
->n_row
);
564 mat
= isl_mat_product(mat
, isl_mat_copy(morph
->inv
));
567 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
568 k
= isl_basic_set_alloc_inequality(res
);
571 isl_seq_cpy(res
->ineq
[k
], mat
->row
[i
], mat
->n_col
);
572 isl_seq_scale(res
->ineq
[k
] + mat
->n_col
,
573 bset
->ineq
[i
] + mat
->n_col
,
574 morph
->inv
->row
[0][0], bset
->n_div
);
578 mat
= isl_mat_sub_alloc(bset
->ctx
, bset
->div
, 0, bset
->n_div
,
579 1, morph
->inv
->n_row
);
580 mat
= isl_mat_product(mat
, isl_mat_copy(morph
->inv
));
583 for (i
= 0; i
< bset
->n_div
; ++i
) {
584 isl_int_mul(res
->div
[i
][0],
585 morph
->inv
->row
[0][0], bset
->div
[i
][0]);
586 isl_seq_cpy(res
->div
[i
] + 1, mat
->row
[i
], mat
->n_col
);
587 isl_seq_scale(res
->div
[i
] + 1 + mat
->n_col
,
588 bset
->div
[i
] + 1 + mat
->n_col
,
589 morph
->inv
->row
[0][0], bset
->n_div
);
593 res
= add_strides(res
, morph
);
595 res
= isl_basic_set_simplify(res
);
596 res
= isl_basic_set_finalize(res
);
598 res
= isl_basic_set_intersect(res
, isl_basic_set_copy(morph
->ran
));
600 isl_morph_free(morph
);
601 isl_basic_set_free(bset
);
605 isl_morph_free(morph
);
606 isl_basic_set_free(bset
);
607 isl_basic_set_free(res
);
611 /* Apply the morphism to the set.
613 __isl_give isl_set
*isl_morph_set(__isl_take isl_morph
*morph
,
614 __isl_take isl_set
*set
)
621 isl_assert(set
->ctx
, isl_dim_equal(set
->dim
, morph
->dom
->dim
), goto error
);
623 set
= isl_set_cow(set
);
627 isl_dim_free(set
->dim
);
628 set
->dim
= isl_dim_copy(morph
->ran
->dim
);
632 for (i
= 0; i
< set
->n
; ++i
) {
633 set
->p
[i
] = isl_morph_basic_set(isl_morph_copy(morph
), set
->p
[i
]);
638 isl_morph_free(morph
);
640 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
645 isl_morph_free(morph
);
649 /* Construct a morphism that first does morph2 and then morph1.
651 __isl_give isl_morph
*isl_morph_compose(__isl_take isl_morph
*morph1
,
652 __isl_take isl_morph
*morph2
)
655 isl_basic_set
*dom
, *ran
;
657 if (!morph1
|| !morph2
)
660 map
= isl_mat_product(isl_mat_copy(morph1
->map
), isl_mat_copy(morph2
->map
));
661 inv
= isl_mat_product(isl_mat_copy(morph2
->inv
), isl_mat_copy(morph1
->inv
));
662 dom
= isl_morph_basic_set(isl_morph_inverse(isl_morph_copy(morph2
)),
663 isl_basic_set_copy(morph1
->dom
));
664 dom
= isl_basic_set_intersect(dom
, isl_basic_set_copy(morph2
->dom
));
665 ran
= isl_morph_basic_set(isl_morph_copy(morph1
),
666 isl_basic_set_copy(morph2
->ran
));
667 ran
= isl_basic_set_intersect(ran
, isl_basic_set_copy(morph1
->ran
));
669 isl_morph_free(morph1
);
670 isl_morph_free(morph2
);
672 return isl_morph_alloc(dom
, ran
, map
, inv
);
674 isl_morph_free(morph1
);
675 isl_morph_free(morph2
);
679 __isl_give isl_morph
*isl_morph_inverse(__isl_take isl_morph
*morph
)
684 morph
= isl_morph_cow(morph
);
689 morph
->dom
= morph
->ran
;
693 morph
->map
= morph
->inv
;