2 * Copyright 2008-2009 Katholieke Universiteit Leuven
4 * Use of this software is governed by the GNU LGPLv2.1 license
6 * Written by Sven Verdoolaege, K.U.Leuven, Departement
7 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
12 #include "isl_map_private.h"
13 #include "isl_equalities.h"
15 /* Given a set of modulo constraints
19 * this function computes a particular solution y_0
21 * The input is given as a matrix B = [ c A ] and a vector d.
23 * The output is matrix containing the solution y_0 or
24 * a zero-column matrix if the constraints admit no integer solution.
26 * The given set of constrains is equivalent to
30 * with D = diag d and x a fresh set of variables.
31 * Reducing both c and A modulo d does not change the
32 * value of y in the solution and may lead to smaller coefficients.
33 * Let M = [ D A ] and [ H 0 ] = M U, the Hermite normal form of M.
39 * [ H 0 ] U^{-1} [ y ] = - c
42 * [ B ] = U^{-1} [ y ]
46 * so B may be chosen arbitrarily, e.g., B = 0, and then
49 * U^{-1} [ y ] = [ 0 ]
57 * If any of the coordinates of this y are non-integer
58 * then the constraints admit no integer solution and
59 * a zero-column matrix is returned.
61 static struct isl_mat
*particular_solution(struct isl_mat
*B
, struct isl_vec
*d
)
64 struct isl_mat
*M
= NULL
;
65 struct isl_mat
*C
= NULL
;
66 struct isl_mat
*U
= NULL
;
67 struct isl_mat
*H
= NULL
;
68 struct isl_mat
*cst
= NULL
;
69 struct isl_mat
*T
= NULL
;
71 M
= isl_mat_alloc(B
->ctx
, B
->n_row
, B
->n_row
+ B
->n_col
- 1);
72 C
= isl_mat_alloc(B
->ctx
, 1 + B
->n_row
, 1);
75 isl_int_set_si(C
->row
[0][0], 1);
76 for (i
= 0; i
< B
->n_row
; ++i
) {
77 isl_seq_clr(M
->row
[i
], B
->n_row
);
78 isl_int_set(M
->row
[i
][i
], d
->block
.data
[i
]);
79 isl_int_neg(C
->row
[1 + i
][0], B
->row
[i
][0]);
80 isl_int_fdiv_r(C
->row
[1+i
][0], C
->row
[1+i
][0], M
->row
[i
][i
]);
81 for (j
= 0; j
< B
->n_col
- 1; ++j
)
82 isl_int_fdiv_r(M
->row
[i
][B
->n_row
+ j
],
83 B
->row
[i
][1 + j
], M
->row
[i
][i
]);
85 M
= isl_mat_left_hermite(M
, 0, &U
, NULL
);
88 H
= isl_mat_sub_alloc(B
->ctx
, M
->row
, 0, B
->n_row
, 0, B
->n_row
);
89 H
= isl_mat_lin_to_aff(H
);
90 C
= isl_mat_inverse_product(H
, C
);
93 for (i
= 0; i
< B
->n_row
; ++i
) {
94 if (!isl_int_is_divisible_by(C
->row
[1+i
][0], C
->row
[0][0]))
96 isl_int_divexact(C
->row
[1+i
][0], C
->row
[1+i
][0], C
->row
[0][0]);
99 cst
= isl_mat_alloc(B
->ctx
, B
->n_row
, 0);
101 cst
= isl_mat_sub_alloc(C
->ctx
, C
->row
, 1, B
->n_row
, 0, 1);
102 T
= isl_mat_sub_alloc(U
->ctx
, U
->row
, B
->n_row
, B
->n_col
- 1, 0, B
->n_row
);
103 cst
= isl_mat_product(T
, cst
);
115 /* Compute and return the matrix
117 * U_1^{-1} diag(d_1, 1, ..., 1)
119 * with U_1 the unimodular completion of the first (and only) row of B.
120 * The columns of this matrix generate the lattice that satisfies
121 * the single (linear) modulo constraint.
123 static struct isl_mat
*parameter_compression_1(
124 struct isl_mat
*B
, struct isl_vec
*d
)
128 U
= isl_mat_alloc(B
->ctx
, B
->n_col
- 1, B
->n_col
- 1);
131 isl_seq_cpy(U
->row
[0], B
->row
[0] + 1, B
->n_col
- 1);
132 U
= isl_mat_unimodular_complete(U
, 1);
133 U
= isl_mat_right_inverse(U
);
136 isl_mat_col_mul(U
, 0, d
->block
.data
[0], 0);
137 U
= isl_mat_lin_to_aff(U
);
141 /* Compute a common lattice of solutions to the linear modulo
142 * constraints specified by B and d.
143 * See also the documentation of isl_mat_parameter_compression.
146 * A = [ L_1^{-T} L_2^{-T} ... L_k^{-T} ]
148 * on a common denominator. This denominator D is the lcm of modulos d.
149 * Since L_i = U_i^{-1} diag(d_i, 1, ... 1), we have
150 * L_i^{-T} = U_i^T diag(d_i, 1, ... 1)^{-T} = U_i^T diag(1/d_i, 1, ..., 1).
151 * Putting this on the common denominator, we have
152 * D * L_i^{-T} = U_i^T diag(D/d_i, D, ..., D).
154 static struct isl_mat
*parameter_compression_multi(
155 struct isl_mat
*B
, struct isl_vec
*d
)
159 struct isl_mat
*A
= NULL
, *U
= NULL
;
168 A
= isl_mat_alloc(B
->ctx
, size
, B
->n_row
* size
);
169 U
= isl_mat_alloc(B
->ctx
, size
, size
);
172 for (i
= 0; i
< B
->n_row
; ++i
) {
173 isl_seq_cpy(U
->row
[0], B
->row
[i
] + 1, size
);
174 U
= isl_mat_unimodular_complete(U
, 1);
177 isl_int_divexact(D
, D
, d
->block
.data
[i
]);
178 for (k
= 0; k
< U
->n_col
; ++k
)
179 isl_int_mul(A
->row
[k
][i
*size
+0], D
, U
->row
[0][k
]);
180 isl_int_mul(D
, D
, d
->block
.data
[i
]);
181 for (j
= 1; j
< U
->n_row
; ++j
)
182 for (k
= 0; k
< U
->n_col
; ++k
)
183 isl_int_mul(A
->row
[k
][i
*size
+j
],
186 A
= isl_mat_left_hermite(A
, 0, NULL
, NULL
);
187 T
= isl_mat_sub_alloc(A
->ctx
, A
->row
, 0, A
->n_row
, 0, A
->n_row
);
188 T
= isl_mat_lin_to_aff(T
);
189 isl_int_set(T
->row
[0][0], D
);
190 T
= isl_mat_right_inverse(T
);
191 isl_assert(T
->ctx
, isl_int_is_one(T
->row
[0][0]), goto error
);
192 T
= isl_mat_transpose(T
);
205 /* Given a set of modulo constraints
209 * this function returns an affine transformation T,
213 * that bijectively maps the integer vectors y' to integer
214 * vectors y that satisfy the modulo constraints.
216 * This function is inspired by Section 2.5.3
217 * of B. Meister, "Stating and Manipulating Periodicity in the Polytope
218 * Model. Applications to Program Analysis and Optimization".
219 * However, the implementation only follows the algorithm of that
220 * section for computing a particular solution and not for computing
221 * a general homogeneous solution. The latter is incomplete and
222 * may remove some valid solutions.
223 * Instead, we use an adaptation of the algorithm in Section 7 of
224 * B. Meister, S. Verdoolaege, "Polynomial Approximations in the Polytope
225 * Model: Bringing the Power of Quasi-Polynomials to the Masses".
227 * The input is given as a matrix B = [ c A ] and a vector d.
228 * Each element of the vector d corresponds to a row in B.
229 * The output is a lower triangular matrix.
230 * If no integer vector y satisfies the given constraints then
231 * a matrix with zero columns is returned.
233 * We first compute a particular solution y_0 to the given set of
234 * modulo constraints in particular_solution. If no such solution
235 * exists, then we return a zero-columned transformation matrix.
236 * Otherwise, we compute the generic solution to
240 * That is we want to compute G such that
244 * with y'' integer, describes the set of solutions.
246 * We first remove the common factors of each row.
247 * In particular if gcd(A_i,d_i) != 1, then we divide the whole
248 * row i (including d_i) by this common factor. If afterwards gcd(A_i) != 1,
249 * then we divide this row of A by the common factor, unless gcd(A_i) = 0.
250 * In the later case, we simply drop the row (in both A and d).
252 * If there are no rows left in A, the G is the identity matrix. Otherwise,
253 * for each row i, we now determine the lattice of integer vectors
254 * that satisfies this row. Let U_i be the unimodular extension of the
255 * row A_i. This unimodular extension exists because gcd(A_i) = 1.
256 * The first component of
260 * needs to be a multiple of d_i. Let y' = diag(d_i, 1, ..., 1) y''.
263 * y = U_i^{-1} diag(d_i, 1, ..., 1) y''
265 * for arbitrary integer vectors y''. That is, y belongs to the lattice
266 * generated by the columns of L_i = U_i^{-1} diag(d_i, 1, ..., 1).
267 * If there is only one row, then G = L_1.
269 * If there is more than one row left, we need to compute the intersection
270 * of the lattices. That is, we need to compute an L such that
272 * L = L_i L_i' for all i
274 * with L_i' some integer matrices. Let A be constructed as follows
276 * A = [ L_1^{-T} L_2^{-T} ... L_k^{-T} ]
278 * and computed the Hermite Normal Form of A = [ H 0 ] U
281 * L_i^{-T} = H U_{1,i}
285 * H^{-T} = L_i U_{1,i}^T
287 * In other words G = L = H^{-T}.
288 * To ensure that G is lower triangular, we compute and use its Hermite
291 * The affine transformation matrix returned is then
296 * as any y = y_0 + G y' with y' integer is a solution to the original
297 * modulo constraints.
299 struct isl_mat
*isl_mat_parameter_compression(
300 struct isl_mat
*B
, struct isl_vec
*d
)
303 struct isl_mat
*cst
= NULL
;
304 struct isl_mat
*T
= NULL
;
309 isl_assert(B
->ctx
, B
->n_row
== d
->size
, goto error
);
310 cst
= particular_solution(B
, d
);
313 if (cst
->n_col
== 0) {
314 T
= isl_mat_alloc(B
->ctx
, B
->n_col
, 0);
321 /* Replace a*g*row = 0 mod g*m by row = 0 mod m */
322 for (i
= 0; i
< B
->n_row
; ++i
) {
323 isl_seq_gcd(B
->row
[i
] + 1, B
->n_col
- 1, &D
);
324 if (isl_int_is_one(D
))
326 if (isl_int_is_zero(D
)) {
327 B
= isl_mat_drop_rows(B
, i
, 1);
331 isl_seq_cpy(d
->block
.data
+i
, d
->block
.data
+i
+1,
340 isl_seq_scale_down(B
->row
[i
] + 1, B
->row
[i
] + 1, D
, B
->n_col
-1);
341 isl_int_gcd(D
, D
, d
->block
.data
[i
]);
345 isl_int_divexact(d
->block
.data
[i
], d
->block
.data
[i
], D
);
349 T
= isl_mat_identity(B
->ctx
, B
->n_col
);
350 else if (B
->n_row
== 1)
351 T
= parameter_compression_1(B
, d
);
353 T
= parameter_compression_multi(B
, d
);
354 T
= isl_mat_left_hermite(T
, 0, NULL
, NULL
);
357 isl_mat_sub_copy(T
->ctx
, T
->row
+ 1, cst
->row
, cst
->n_row
, 0, 0, 1);
371 /* Given a set of equalities
375 * this function computes unimodular transformation from a lower-dimensional
376 * space to the original space that bijectively maps the integer points x'
377 * in the lower-dimensional space to the integer points x in the original
378 * space that satisfy the equalities.
380 * The input is given as a matrix B = [ -c M ] and the out is a
381 * matrix that maps [1 x'] to [1 x].
382 * If T2 is not NULL, then *T2 is set to a matrix mapping [1 x] to [1 x'].
384 * First compute the (left) Hermite normal form of M,
386 * M [U1 U2] = M U = H = [H1 0]
388 * M = H Q = [H1 0] [Q1]
391 * with U, Q unimodular, Q = U^{-1} (and H lower triangular).
392 * Define the transformed variables as
394 * x = [U1 U2] [ x1' ] = [U1 U2] [Q1] x
397 * The equalities then become
399 * H1 x1' - c = 0 or x1' = H1^{-1} c = c'
401 * If any of the c' is non-integer, then the original set has no
402 * integer solutions (since the x' are a unimodular transformation
404 * Otherwise, the transformation is given by
406 * x = U1 H1^{-1} c + U2 x2'
408 * The inverse transformation is simply
412 struct isl_mat
*isl_mat_variable_compression(struct isl_mat
*B
,
416 struct isl_mat
*H
= NULL
, *C
= NULL
, *H1
, *U
= NULL
, *U1
, *U2
, *TC
;
425 H
= isl_mat_sub_alloc(B
->ctx
, B
->row
, 0, B
->n_row
, 1, dim
);
426 H
= isl_mat_left_hermite(H
, 0, &U
, T2
);
427 if (!H
|| !U
|| (T2
&& !*T2
))
430 *T2
= isl_mat_drop_rows(*T2
, 0, B
->n_row
);
431 *T2
= isl_mat_lin_to_aff(*T2
);
435 C
= isl_mat_alloc(B
->ctx
, 1+B
->n_row
, 1);
438 isl_int_set_si(C
->row
[0][0], 1);
439 isl_mat_sub_neg(C
->ctx
, C
->row
+1, B
->row
, B
->n_row
, 0, 0, 1);
440 H1
= isl_mat_sub_alloc(H
->ctx
, H
->row
, 0, H
->n_row
, 0, H
->n_row
);
441 H1
= isl_mat_lin_to_aff(H1
);
442 TC
= isl_mat_inverse_product(H1
, C
);
446 if (!isl_int_is_one(TC
->row
[0][0])) {
447 for (i
= 0; i
< B
->n_row
; ++i
) {
448 if (!isl_int_is_divisible_by(TC
->row
[1+i
][0], TC
->row
[0][0])) {
449 struct isl_ctx
*ctx
= B
->ctx
;
457 return isl_mat_alloc(ctx
, 1 + dim
, 0);
459 isl_seq_scale_down(TC
->row
[1+i
], TC
->row
[1+i
], TC
->row
[0][0], 1);
461 isl_int_set_si(TC
->row
[0][0], 1);
463 U1
= isl_mat_sub_alloc(U
->ctx
, U
->row
, 0, U
->n_row
, 0, B
->n_row
);
464 U1
= isl_mat_lin_to_aff(U1
);
465 U2
= isl_mat_sub_alloc(U
->ctx
, U
->row
, 0, U
->n_row
,
466 B
->n_row
, U
->n_row
- B
->n_row
);
467 U2
= isl_mat_lin_to_aff(U2
);
469 TC
= isl_mat_product(U1
, TC
);
470 TC
= isl_mat_aff_direct_sum(TC
, U2
);
486 /* Use the n equalities of bset to unimodularly transform the
487 * variables x such that n transformed variables x1' have a constant value
488 * and rewrite the constraints of bset in terms of the remaining
489 * transformed variables x2'. The matrix pointed to by T maps
490 * the new variables x2' back to the original variables x, while T2
491 * maps the original variables to the new variables.
493 static struct isl_basic_set
*compress_variables(
494 struct isl_basic_set
*bset
, struct isl_mat
**T
, struct isl_mat
**T2
)
496 struct isl_mat
*B
, *TC
;
505 isl_assert(bset
->ctx
, isl_basic_set_n_param(bset
) == 0, goto error
);
506 isl_assert(bset
->ctx
, bset
->n_div
== 0, goto error
);
507 dim
= isl_basic_set_n_dim(bset
);
508 isl_assert(bset
->ctx
, bset
->n_eq
<= dim
, goto error
);
512 B
= isl_mat_sub_alloc(bset
->ctx
, bset
->eq
, 0, bset
->n_eq
, 0, 1 + dim
);
513 TC
= isl_mat_variable_compression(B
, T2
);
516 if (TC
->n_col
== 0) {
522 return isl_basic_set_set_to_empty(bset
);
525 bset
= isl_basic_set_preimage(bset
, T
? isl_mat_copy(TC
) : TC
);
530 isl_basic_set_free(bset
);
534 struct isl_basic_set
*isl_basic_set_remove_equalities(
535 struct isl_basic_set
*bset
, struct isl_mat
**T
, struct isl_mat
**T2
)
543 isl_assert(bset
->ctx
, isl_basic_set_n_param(bset
) == 0, goto error
);
544 bset
= isl_basic_set_gauss(bset
, NULL
);
545 if (ISL_F_ISSET(bset
, ISL_BASIC_SET_EMPTY
))
547 bset
= compress_variables(bset
, T
, T2
);
550 isl_basic_set_free(bset
);
555 /* Check if dimension dim belongs to a residue class
556 * i_dim \equiv r mod m
557 * with m != 1 and if so return m in *modulo and r in *residue.
558 * As a special case, when i_dim has a fixed value v, then
559 * *modulo is set to 0 and *residue to v.
561 * If i_dim does not belong to such a residue class, then *modulo
562 * is set to 1 and *residue is set to 0.
564 int isl_basic_set_dim_residue_class(struct isl_basic_set
*bset
,
565 int pos
, isl_int
*modulo
, isl_int
*residue
)
568 struct isl_mat
*H
= NULL
, *U
= NULL
, *C
, *H1
, *U1
;
572 if (!bset
|| !modulo
|| !residue
)
575 if (isl_basic_set_fast_dim_is_fixed(bset
, pos
, residue
)) {
576 isl_int_set_si(*modulo
, 0);
581 total
= isl_basic_set_total_dim(bset
);
582 nparam
= isl_basic_set_n_param(bset
);
583 H
= isl_mat_sub_alloc(bset
->ctx
, bset
->eq
, 0, bset
->n_eq
, 1, total
);
584 H
= isl_mat_left_hermite(H
, 0, &U
, NULL
);
588 isl_seq_gcd(U
->row
[nparam
+ pos
]+bset
->n_eq
,
589 total
-bset
->n_eq
, modulo
);
590 if (isl_int_is_zero(*modulo
))
591 isl_int_set_si(*modulo
, 1);
592 if (isl_int_is_one(*modulo
)) {
593 isl_int_set_si(*residue
, 0);
599 C
= isl_mat_alloc(bset
->ctx
, 1+bset
->n_eq
, 1);
602 isl_int_set_si(C
->row
[0][0], 1);
603 isl_mat_sub_neg(C
->ctx
, C
->row
+1, bset
->eq
, bset
->n_eq
, 0, 0, 1);
604 H1
= isl_mat_sub_alloc(H
->ctx
, H
->row
, 0, H
->n_row
, 0, H
->n_row
);
605 H1
= isl_mat_lin_to_aff(H1
);
606 C
= isl_mat_inverse_product(H1
, C
);
608 U1
= isl_mat_sub_alloc(U
->ctx
, U
->row
, nparam
+pos
, 1, 0, bset
->n_eq
);
609 U1
= isl_mat_lin_to_aff(U1
);
611 C
= isl_mat_product(U1
, C
);
614 if (!isl_int_is_divisible_by(C
->row
[1][0], C
->row
[0][0])) {
615 bset
= isl_basic_set_copy(bset
);
616 bset
= isl_basic_set_set_to_empty(bset
);
617 isl_basic_set_free(bset
);
618 isl_int_set_si(*modulo
, 1);
619 isl_int_set_si(*residue
, 0);
622 isl_int_divexact(*residue
, C
->row
[1][0], C
->row
[0][0]);
623 isl_int_fdiv_r(*residue
, *residue
, *modulo
);
632 /* Check if dimension dim belongs to a residue class
633 * i_dim \equiv r mod m
634 * with m != 1 and if so return m in *modulo and r in *residue.
635 * As a special case, when i_dim has a fixed value v, then
636 * *modulo is set to 0 and *residue to v.
638 * If i_dim does not belong to such a residue class, then *modulo
639 * is set to 1 and *residue is set to 0.
641 int isl_set_dim_residue_class(struct isl_set
*set
,
642 int pos
, isl_int
*modulo
, isl_int
*residue
)
648 if (!set
|| !modulo
|| !residue
)
652 isl_int_set_si(*modulo
, 0);
653 isl_int_set_si(*residue
, 0);
657 if (isl_basic_set_dim_residue_class(set
->p
[0], pos
, modulo
, residue
)<0)
663 if (isl_int_is_one(*modulo
))
669 for (i
= 1; i
< set
->n
; ++i
) {
670 if (isl_basic_set_dim_residue_class(set
->p
[0], pos
, &m
, &r
) < 0)
672 isl_int_gcd(*modulo
, *modulo
, m
);
673 if (!isl_int_is_zero(*modulo
))
674 isl_int_fdiv_r(*residue
, *residue
, *modulo
);
675 if (isl_int_is_one(*modulo
))
677 if (!isl_int_is_zero(*modulo
))
678 isl_int_fdiv_r(r
, r
, *modulo
);
679 if (isl_int_ne(*residue
, r
)) {
680 isl_int_set_si(*modulo
, 1);
681 isl_int_set_si(*residue
, 0);