2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
5 * Use of this software is governed by the MIT license
7 * Written by Sven Verdoolaege, K.U.Leuven, Departement
8 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
9 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
10 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
13 #include <isl_mat_private.h>
14 #include <isl_vec_private.h>
16 #include "isl_map_private.h"
17 #include "isl_equalities.h"
18 #include <isl_val_private.h>
20 /* Given a set of modulo constraints
24 * this function computes a particular solution y_0
26 * The input is given as a matrix B = [ c A ] and a vector d.
28 * The output is matrix containing the solution y_0 or
29 * a zero-column matrix if the constraints admit no integer solution.
31 * The given set of constrains is equivalent to
35 * with D = diag d and x a fresh set of variables.
36 * Reducing both c and A modulo d does not change the
37 * value of y in the solution and may lead to smaller coefficients.
38 * Let M = [ D A ] and [ H 0 ] = M U, the Hermite normal form of M.
44 * [ H 0 ] U^{-1} [ y ] = - c
47 * [ B ] = U^{-1} [ y ]
51 * so B may be chosen arbitrarily, e.g., B = 0, and then
54 * U^{-1} [ y ] = [ 0 ]
62 * If any of the coordinates of this y are non-integer
63 * then the constraints admit no integer solution and
64 * a zero-column matrix is returned.
66 static struct isl_mat
*particular_solution(struct isl_mat
*B
, struct isl_vec
*d
)
69 struct isl_mat
*M
= NULL
;
70 struct isl_mat
*C
= NULL
;
71 struct isl_mat
*U
= NULL
;
72 struct isl_mat
*H
= NULL
;
73 struct isl_mat
*cst
= NULL
;
74 struct isl_mat
*T
= NULL
;
76 M
= isl_mat_alloc(B
->ctx
, B
->n_row
, B
->n_row
+ B
->n_col
- 1);
77 C
= isl_mat_alloc(B
->ctx
, 1 + B
->n_row
, 1);
80 isl_int_set_si(C
->row
[0][0], 1);
81 for (i
= 0; i
< B
->n_row
; ++i
) {
82 isl_seq_clr(M
->row
[i
], B
->n_row
);
83 isl_int_set(M
->row
[i
][i
], d
->block
.data
[i
]);
84 isl_int_neg(C
->row
[1 + i
][0], B
->row
[i
][0]);
85 isl_int_fdiv_r(C
->row
[1+i
][0], C
->row
[1+i
][0], M
->row
[i
][i
]);
86 for (j
= 0; j
< B
->n_col
- 1; ++j
)
87 isl_int_fdiv_r(M
->row
[i
][B
->n_row
+ j
],
88 B
->row
[i
][1 + j
], M
->row
[i
][i
]);
90 M
= isl_mat_left_hermite(M
, 0, &U
, NULL
);
93 H
= isl_mat_sub_alloc(M
, 0, B
->n_row
, 0, B
->n_row
);
94 H
= isl_mat_lin_to_aff(H
);
95 C
= isl_mat_inverse_product(H
, C
);
98 for (i
= 0; i
< B
->n_row
; ++i
) {
99 if (!isl_int_is_divisible_by(C
->row
[1+i
][0], C
->row
[0][0]))
101 isl_int_divexact(C
->row
[1+i
][0], C
->row
[1+i
][0], C
->row
[0][0]);
104 cst
= isl_mat_alloc(B
->ctx
, B
->n_row
, 0);
106 cst
= isl_mat_sub_alloc(C
, 1, B
->n_row
, 0, 1);
107 T
= isl_mat_sub_alloc(U
, B
->n_row
, B
->n_col
- 1, 0, B
->n_row
);
108 cst
= isl_mat_product(T
, cst
);
120 /* Compute and return the matrix
122 * U_1^{-1} diag(d_1, 1, ..., 1)
124 * with U_1 the unimodular completion of the first (and only) row of B.
125 * The columns of this matrix generate the lattice that satisfies
126 * the single (linear) modulo constraint.
128 static struct isl_mat
*parameter_compression_1(
129 struct isl_mat
*B
, struct isl_vec
*d
)
133 U
= isl_mat_alloc(B
->ctx
, B
->n_col
- 1, B
->n_col
- 1);
136 isl_seq_cpy(U
->row
[0], B
->row
[0] + 1, B
->n_col
- 1);
137 U
= isl_mat_unimodular_complete(U
, 1);
138 U
= isl_mat_right_inverse(U
);
141 isl_mat_col_mul(U
, 0, d
->block
.data
[0], 0);
142 U
= isl_mat_lin_to_aff(U
);
146 /* Compute a common lattice of solutions to the linear modulo
147 * constraints specified by B and d.
148 * See also the documentation of isl_mat_parameter_compression.
151 * A = [ L_1^{-T} L_2^{-T} ... L_k^{-T} ]
153 * on a common denominator. This denominator D is the lcm of modulos d.
154 * Since L_i = U_i^{-1} diag(d_i, 1, ... 1), we have
155 * L_i^{-T} = U_i^T diag(d_i, 1, ... 1)^{-T} = U_i^T diag(1/d_i, 1, ..., 1).
156 * Putting this on the common denominator, we have
157 * D * L_i^{-T} = U_i^T diag(D/d_i, D, ..., D).
159 static struct isl_mat
*parameter_compression_multi(
160 struct isl_mat
*B
, struct isl_vec
*d
)
164 struct isl_mat
*A
= NULL
, *U
= NULL
;
173 A
= isl_mat_alloc(B
->ctx
, size
, B
->n_row
* size
);
174 U
= isl_mat_alloc(B
->ctx
, size
, size
);
177 for (i
= 0; i
< B
->n_row
; ++i
) {
178 isl_seq_cpy(U
->row
[0], B
->row
[i
] + 1, size
);
179 U
= isl_mat_unimodular_complete(U
, 1);
182 isl_int_divexact(D
, D
, d
->block
.data
[i
]);
183 for (k
= 0; k
< U
->n_col
; ++k
)
184 isl_int_mul(A
->row
[k
][i
*size
+0], D
, U
->row
[0][k
]);
185 isl_int_mul(D
, D
, d
->block
.data
[i
]);
186 for (j
= 1; j
< U
->n_row
; ++j
)
187 for (k
= 0; k
< U
->n_col
; ++k
)
188 isl_int_mul(A
->row
[k
][i
*size
+j
],
191 A
= isl_mat_left_hermite(A
, 0, NULL
, NULL
);
192 T
= isl_mat_sub_alloc(A
, 0, A
->n_row
, 0, A
->n_row
);
193 T
= isl_mat_lin_to_aff(T
);
196 isl_int_set(T
->row
[0][0], D
);
197 T
= isl_mat_right_inverse(T
);
200 isl_assert(T
->ctx
, isl_int_is_one(T
->row
[0][0]), goto error
);
201 T
= isl_mat_transpose(T
);
214 /* Given a set of modulo constraints
218 * this function returns an affine transformation T,
222 * that bijectively maps the integer vectors y' to integer
223 * vectors y that satisfy the modulo constraints.
225 * This function is inspired by Section 2.5.3
226 * of B. Meister, "Stating and Manipulating Periodicity in the Polytope
227 * Model. Applications to Program Analysis and Optimization".
228 * However, the implementation only follows the algorithm of that
229 * section for computing a particular solution and not for computing
230 * a general homogeneous solution. The latter is incomplete and
231 * may remove some valid solutions.
232 * Instead, we use an adaptation of the algorithm in Section 7 of
233 * B. Meister, S. Verdoolaege, "Polynomial Approximations in the Polytope
234 * Model: Bringing the Power of Quasi-Polynomials to the Masses".
236 * The input is given as a matrix B = [ c A ] and a vector d.
237 * Each element of the vector d corresponds to a row in B.
238 * The output is a lower triangular matrix.
239 * If no integer vector y satisfies the given constraints then
240 * a matrix with zero columns is returned.
242 * We first compute a particular solution y_0 to the given set of
243 * modulo constraints in particular_solution. If no such solution
244 * exists, then we return a zero-columned transformation matrix.
245 * Otherwise, we compute the generic solution to
249 * That is we want to compute G such that
253 * with y'' integer, describes the set of solutions.
255 * We first remove the common factors of each row.
256 * In particular if gcd(A_i,d_i) != 1, then we divide the whole
257 * row i (including d_i) by this common factor. If afterwards gcd(A_i) != 1,
258 * then we divide this row of A by the common factor, unless gcd(A_i) = 0.
259 * In the later case, we simply drop the row (in both A and d).
261 * If there are no rows left in A, then G is the identity matrix. Otherwise,
262 * for each row i, we now determine the lattice of integer vectors
263 * that satisfies this row. Let U_i be the unimodular extension of the
264 * row A_i. This unimodular extension exists because gcd(A_i) = 1.
265 * The first component of
269 * needs to be a multiple of d_i. Let y' = diag(d_i, 1, ..., 1) y''.
272 * y = U_i^{-1} diag(d_i, 1, ..., 1) y''
274 * for arbitrary integer vectors y''. That is, y belongs to the lattice
275 * generated by the columns of L_i = U_i^{-1} diag(d_i, 1, ..., 1).
276 * If there is only one row, then G = L_1.
278 * If there is more than one row left, we need to compute the intersection
279 * of the lattices. That is, we need to compute an L such that
281 * L = L_i L_i' for all i
283 * with L_i' some integer matrices. Let A be constructed as follows
285 * A = [ L_1^{-T} L_2^{-T} ... L_k^{-T} ]
287 * and computed the Hermite Normal Form of A = [ H 0 ] U
290 * L_i^{-T} = H U_{1,i}
294 * H^{-T} = L_i U_{1,i}^T
296 * In other words G = L = H^{-T}.
297 * To ensure that G is lower triangular, we compute and use its Hermite
300 * The affine transformation matrix returned is then
305 * as any y = y_0 + G y' with y' integer is a solution to the original
306 * modulo constraints.
308 struct isl_mat
*isl_mat_parameter_compression(
309 struct isl_mat
*B
, struct isl_vec
*d
)
312 struct isl_mat
*cst
= NULL
;
313 struct isl_mat
*T
= NULL
;
318 isl_assert(B
->ctx
, B
->n_row
== d
->size
, goto error
);
319 cst
= particular_solution(B
, d
);
322 if (cst
->n_col
== 0) {
323 T
= isl_mat_alloc(B
->ctx
, B
->n_col
, 0);
330 /* Replace a*g*row = 0 mod g*m by row = 0 mod m */
331 for (i
= 0; i
< B
->n_row
; ++i
) {
332 isl_seq_gcd(B
->row
[i
] + 1, B
->n_col
- 1, &D
);
333 if (isl_int_is_one(D
))
335 if (isl_int_is_zero(D
)) {
336 B
= isl_mat_drop_rows(B
, i
, 1);
340 isl_seq_cpy(d
->block
.data
+i
, d
->block
.data
+i
+1,
349 isl_seq_scale_down(B
->row
[i
] + 1, B
->row
[i
] + 1, D
, B
->n_col
-1);
350 isl_int_gcd(D
, D
, d
->block
.data
[i
]);
354 isl_int_divexact(d
->block
.data
[i
], d
->block
.data
[i
], D
);
358 T
= isl_mat_identity(B
->ctx
, B
->n_col
);
359 else if (B
->n_row
== 1)
360 T
= parameter_compression_1(B
, d
);
362 T
= parameter_compression_multi(B
, d
);
363 T
= isl_mat_left_hermite(T
, 0, NULL
, NULL
);
366 isl_mat_sub_copy(T
->ctx
, T
->row
+ 1, cst
->row
, cst
->n_row
, 0, 0, 1);
380 /* Given a set of equalities
384 * compute and return an affine transformation T,
388 * that bijectively maps the integer vectors y' to integer
389 * vectors y that satisfy the modulo constraints for some value of x.
391 * Let [H 0] be the Hermite Normal Form of A, i.e.,
395 * Then y is a solution of (*) iff
397 * H^-1 B(y) (= - [I 0] Q x)
399 * is an integer vector. Let d be the common denominator of H^-1.
402 * d H^-1 B(y) = 0 mod d
404 * and compute the solution using isl_mat_parameter_compression.
406 __isl_give isl_mat
*isl_mat_parameter_compression_ext(__isl_take isl_mat
*B
,
407 __isl_take isl_mat
*A
)
414 return isl_mat_free(B
);
416 ctx
= isl_mat_get_ctx(A
);
419 A
= isl_mat_left_hermite(A
, 0, NULL
, NULL
);
420 A
= isl_mat_drop_cols(A
, n_row
, n_col
- n_row
);
421 A
= isl_mat_lin_to_aff(A
);
422 A
= isl_mat_right_inverse(A
);
423 d
= isl_vec_alloc(ctx
, n_row
);
425 d
= isl_vec_set(d
, A
->row
[0][0]);
426 A
= isl_mat_drop_rows(A
, 0, 1);
427 A
= isl_mat_drop_cols(A
, 0, 1);
428 B
= isl_mat_product(A
, B
);
430 return isl_mat_parameter_compression(B
, d
);
433 /* Given a set of equalities
437 * this function computes a unimodular transformation from a lower-dimensional
438 * space to the original space that bijectively maps the integer points x'
439 * in the lower-dimensional space to the integer points x in the original
440 * space that satisfy the equalities.
442 * The input is given as a matrix B = [ -c M ] and the output is a
443 * matrix that maps [1 x'] to [1 x].
444 * If T2 is not NULL, then *T2 is set to a matrix mapping [1 x] to [1 x'].
446 * First compute the (left) Hermite normal form of M,
448 * M [U1 U2] = M U = H = [H1 0]
450 * M = H Q = [H1 0] [Q1]
453 * with U, Q unimodular, Q = U^{-1} (and H lower triangular).
454 * Define the transformed variables as
456 * x = [U1 U2] [ x1' ] = [U1 U2] [Q1] x
459 * The equalities then become
461 * H1 x1' - c = 0 or x1' = H1^{-1} c = c'
463 * If any of the c' is non-integer, then the original set has no
464 * integer solutions (since the x' are a unimodular transformation
465 * of the x) and a zero-column matrix is returned.
466 * Otherwise, the transformation is given by
468 * x = U1 H1^{-1} c + U2 x2'
470 * The inverse transformation is simply
474 __isl_give isl_mat
*isl_mat_variable_compression(__isl_take isl_mat
*B
,
475 __isl_give isl_mat
**T2
)
478 struct isl_mat
*H
= NULL
, *C
= NULL
, *H1
, *U
= NULL
, *U1
, *U2
, *TC
;
487 H
= isl_mat_sub_alloc(B
, 0, B
->n_row
, 1, dim
);
488 H
= isl_mat_left_hermite(H
, 0, &U
, T2
);
489 if (!H
|| !U
|| (T2
&& !*T2
))
492 *T2
= isl_mat_drop_rows(*T2
, 0, B
->n_row
);
493 *T2
= isl_mat_lin_to_aff(*T2
);
497 C
= isl_mat_alloc(B
->ctx
, 1+B
->n_row
, 1);
500 isl_int_set_si(C
->row
[0][0], 1);
501 isl_mat_sub_neg(C
->ctx
, C
->row
+1, B
->row
, B
->n_row
, 0, 0, 1);
502 H1
= isl_mat_sub_alloc(H
, 0, H
->n_row
, 0, H
->n_row
);
503 H1
= isl_mat_lin_to_aff(H1
);
504 TC
= isl_mat_inverse_product(H1
, C
);
508 if (!isl_int_is_one(TC
->row
[0][0])) {
509 for (i
= 0; i
< B
->n_row
; ++i
) {
510 if (!isl_int_is_divisible_by(TC
->row
[1+i
][0], TC
->row
[0][0])) {
511 struct isl_ctx
*ctx
= B
->ctx
;
519 return isl_mat_alloc(ctx
, 1 + dim
, 0);
521 isl_seq_scale_down(TC
->row
[1+i
], TC
->row
[1+i
], TC
->row
[0][0], 1);
523 isl_int_set_si(TC
->row
[0][0], 1);
525 U1
= isl_mat_sub_alloc(U
, 0, U
->n_row
, 0, B
->n_row
);
526 U1
= isl_mat_lin_to_aff(U1
);
527 U2
= isl_mat_sub_alloc(U
, 0, U
->n_row
, B
->n_row
, U
->n_row
- B
->n_row
);
528 U2
= isl_mat_lin_to_aff(U2
);
530 TC
= isl_mat_product(U1
, TC
);
531 TC
= isl_mat_aff_direct_sum(TC
, U2
);
547 /* Use the n equalities of bset to unimodularly transform the
548 * variables x such that n transformed variables x1' have a constant value
549 * and rewrite the constraints of bset in terms of the remaining
550 * transformed variables x2'. The matrix pointed to by T maps
551 * the new variables x2' back to the original variables x, while T2
552 * maps the original variables to the new variables.
554 static struct isl_basic_set
*compress_variables(
555 struct isl_basic_set
*bset
, struct isl_mat
**T
, struct isl_mat
**T2
)
557 struct isl_mat
*B
, *TC
;
566 isl_assert(bset
->ctx
, isl_basic_set_n_param(bset
) == 0, goto error
);
567 isl_assert(bset
->ctx
, bset
->n_div
== 0, goto error
);
568 dim
= isl_basic_set_n_dim(bset
);
569 isl_assert(bset
->ctx
, bset
->n_eq
<= dim
, goto error
);
573 B
= isl_mat_sub_alloc6(bset
->ctx
, bset
->eq
, 0, bset
->n_eq
, 0, 1 + dim
);
574 TC
= isl_mat_variable_compression(B
, T2
);
577 if (TC
->n_col
== 0) {
583 return isl_basic_set_set_to_empty(bset
);
586 bset
= isl_basic_set_preimage(bset
, T
? isl_mat_copy(TC
) : TC
);
591 isl_basic_set_free(bset
);
595 struct isl_basic_set
*isl_basic_set_remove_equalities(
596 struct isl_basic_set
*bset
, struct isl_mat
**T
, struct isl_mat
**T2
)
604 isl_assert(bset
->ctx
, isl_basic_set_n_param(bset
) == 0, goto error
);
605 bset
= isl_basic_set_gauss(bset
, NULL
);
606 if (ISL_F_ISSET(bset
, ISL_BASIC_SET_EMPTY
))
608 bset
= compress_variables(bset
, T
, T2
);
611 isl_basic_set_free(bset
);
616 /* Check if dimension dim belongs to a residue class
617 * i_dim \equiv r mod m
618 * with m != 1 and if so return m in *modulo and r in *residue.
619 * As a special case, when i_dim has a fixed value v, then
620 * *modulo is set to 0 and *residue to v.
622 * If i_dim does not belong to such a residue class, then *modulo
623 * is set to 1 and *residue is set to 0.
625 int isl_basic_set_dim_residue_class(struct isl_basic_set
*bset
,
626 int pos
, isl_int
*modulo
, isl_int
*residue
)
629 struct isl_mat
*H
= NULL
, *U
= NULL
, *C
, *H1
, *U1
;
633 if (!bset
|| !modulo
|| !residue
)
636 if (isl_basic_set_plain_dim_is_fixed(bset
, pos
, residue
)) {
637 isl_int_set_si(*modulo
, 0);
642 total
= isl_basic_set_total_dim(bset
);
643 nparam
= isl_basic_set_n_param(bset
);
644 H
= isl_mat_sub_alloc6(bset
->ctx
, bset
->eq
, 0, bset
->n_eq
, 1, total
);
645 H
= isl_mat_left_hermite(H
, 0, &U
, NULL
);
649 isl_seq_gcd(U
->row
[nparam
+ pos
]+bset
->n_eq
,
650 total
-bset
->n_eq
, modulo
);
651 if (isl_int_is_zero(*modulo
))
652 isl_int_set_si(*modulo
, 1);
653 if (isl_int_is_one(*modulo
)) {
654 isl_int_set_si(*residue
, 0);
660 C
= isl_mat_alloc(bset
->ctx
, 1+bset
->n_eq
, 1);
663 isl_int_set_si(C
->row
[0][0], 1);
664 isl_mat_sub_neg(C
->ctx
, C
->row
+1, bset
->eq
, bset
->n_eq
, 0, 0, 1);
665 H1
= isl_mat_sub_alloc(H
, 0, H
->n_row
, 0, H
->n_row
);
666 H1
= isl_mat_lin_to_aff(H1
);
667 C
= isl_mat_inverse_product(H1
, C
);
669 U1
= isl_mat_sub_alloc(U
, nparam
+pos
, 1, 0, bset
->n_eq
);
670 U1
= isl_mat_lin_to_aff(U1
);
672 C
= isl_mat_product(U1
, C
);
675 if (!isl_int_is_divisible_by(C
->row
[1][0], C
->row
[0][0])) {
676 bset
= isl_basic_set_copy(bset
);
677 bset
= isl_basic_set_set_to_empty(bset
);
678 isl_basic_set_free(bset
);
679 isl_int_set_si(*modulo
, 1);
680 isl_int_set_si(*residue
, 0);
683 isl_int_divexact(*residue
, C
->row
[1][0], C
->row
[0][0]);
684 isl_int_fdiv_r(*residue
, *residue
, *modulo
);
693 /* Check if dimension dim belongs to a residue class
694 * i_dim \equiv r mod m
695 * with m != 1 and if so return m in *modulo and r in *residue.
696 * As a special case, when i_dim has a fixed value v, then
697 * *modulo is set to 0 and *residue to v.
699 * If i_dim does not belong to such a residue class, then *modulo
700 * is set to 1 and *residue is set to 0.
702 int isl_set_dim_residue_class(struct isl_set
*set
,
703 int pos
, isl_int
*modulo
, isl_int
*residue
)
709 if (!set
|| !modulo
|| !residue
)
713 isl_int_set_si(*modulo
, 0);
714 isl_int_set_si(*residue
, 0);
718 if (isl_basic_set_dim_residue_class(set
->p
[0], pos
, modulo
, residue
)<0)
724 if (isl_int_is_one(*modulo
))
730 for (i
= 1; i
< set
->n
; ++i
) {
731 if (isl_basic_set_dim_residue_class(set
->p
[i
], pos
, &m
, &r
) < 0)
733 isl_int_gcd(*modulo
, *modulo
, m
);
734 isl_int_sub(m
, *residue
, r
);
735 isl_int_gcd(*modulo
, *modulo
, m
);
736 if (!isl_int_is_zero(*modulo
))
737 isl_int_fdiv_r(*residue
, *residue
, *modulo
);
738 if (isl_int_is_one(*modulo
))
752 /* Check if dimension "dim" belongs to a residue class
753 * i_dim \equiv r mod m
754 * with m != 1 and if so return m in *modulo and r in *residue.
755 * As a special case, when i_dim has a fixed value v, then
756 * *modulo is set to 0 and *residue to v.
758 * If i_dim does not belong to such a residue class, then *modulo
759 * is set to 1 and *residue is set to 0.
761 int isl_set_dim_residue_class_val(__isl_keep isl_set
*set
,
762 int pos
, __isl_give isl_val
**modulo
, __isl_give isl_val
**residue
)
768 *modulo
= isl_val_alloc(isl_set_get_ctx(set
));
769 *residue
= isl_val_alloc(isl_set_get_ctx(set
));
770 if (!*modulo
|| !*residue
)
772 if (isl_set_dim_residue_class(set
, pos
,
773 &(*modulo
)->n
, &(*residue
)->n
) < 0)
775 isl_int_set_si((*modulo
)->d
, 1);
776 isl_int_set_si((*residue
)->d
, 1);
779 isl_val_free(*modulo
);
780 isl_val_free(*residue
);