2 * Copyright 2008-2009 Katholieke Universiteit Leuven
4 * Use of this software is governed by the MIT license
6 * Written by Sven Verdoolaege, K.U.Leuven, Departement
7 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
10 #include <isl_mat_private.h>
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(M, 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, 1, B->n_row, 0, 1);
102 T = isl_mat_sub_alloc(U, 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, 0, A->n_row, 0, A->n_row);
188 T = isl_mat_lin_to_aff(T);
191 isl_int_set(T->row[0][0], D);
192 T = isl_mat_right_inverse(T);
195 isl_assert(T->ctx, isl_int_is_one(T->row[0][0]), goto error);
196 T = isl_mat_transpose(T);
209 /* Given a set of modulo constraints
213 * this function returns an affine transformation T,
217 * that bijectively maps the integer vectors y' to integer
218 * vectors y that satisfy the modulo constraints.
220 * This function is inspired by Section 2.5.3
221 * of B. Meister, "Stating and Manipulating Periodicity in the Polytope
222 * Model. Applications to Program Analysis and Optimization".
223 * However, the implementation only follows the algorithm of that
224 * section for computing a particular solution and not for computing
225 * a general homogeneous solution. The latter is incomplete and
226 * may remove some valid solutions.
227 * Instead, we use an adaptation of the algorithm in Section 7 of
228 * B. Meister, S. Verdoolaege, "Polynomial Approximations in the Polytope
229 * Model: Bringing the Power of Quasi-Polynomials to the Masses".
231 * The input is given as a matrix B = [ c A ] and a vector d.
232 * Each element of the vector d corresponds to a row in B.
233 * The output is a lower triangular matrix.
234 * If no integer vector y satisfies the given constraints then
235 * a matrix with zero columns is returned.
237 * We first compute a particular solution y_0 to the given set of
238 * modulo constraints in particular_solution. If no such solution
239 * exists, then we return a zero-columned transformation matrix.
240 * Otherwise, we compute the generic solution to
244 * That is we want to compute G such that
248 * with y'' integer, describes the set of solutions.
250 * We first remove the common factors of each row.
251 * In particular if gcd(A_i,d_i) != 1, then we divide the whole
252 * row i (including d_i) by this common factor. If afterwards gcd(A_i) != 1,
253 * then we divide this row of A by the common factor, unless gcd(A_i) = 0.
254 * In the later case, we simply drop the row (in both A and d).
256 * If there are no rows left in A, then G is the identity matrix. Otherwise,
257 * for each row i, we now determine the lattice of integer vectors
258 * that satisfies this row. Let U_i be the unimodular extension of the
259 * row A_i. This unimodular extension exists because gcd(A_i) = 1.
260 * The first component of
264 * needs to be a multiple of d_i. Let y' = diag(d_i, 1, ..., 1) y''.
267 * y = U_i^{-1} diag(d_i, 1, ..., 1) y''
269 * for arbitrary integer vectors y''. That is, y belongs to the lattice
270 * generated by the columns of L_i = U_i^{-1} diag(d_i, 1, ..., 1).
271 * If there is only one row, then G = L_1.
273 * If there is more than one row left, we need to compute the intersection
274 * of the lattices. That is, we need to compute an L such that
276 * L = L_i L_i' for all i
278 * with L_i' some integer matrices. Let A be constructed as follows
280 * A = [ L_1^{-T} L_2^{-T} ... L_k^{-T} ]
282 * and computed the Hermite Normal Form of A = [ H 0 ] U
285 * L_i^{-T} = H U_{1,i}
289 * H^{-T} = L_i U_{1,i}^T
291 * In other words G = L = H^{-T}.
292 * To ensure that G is lower triangular, we compute and use its Hermite
295 * The affine transformation matrix returned is then
300 * as any y = y_0 + G y' with y' integer is a solution to the original
301 * modulo constraints.
303 struct isl_mat *isl_mat_parameter_compression(
304 struct isl_mat *B, struct isl_vec *d)
307 struct isl_mat *cst = NULL;
308 struct isl_mat *T = NULL;
313 isl_assert(B->ctx, B->n_row == d->size, goto error);
314 cst = particular_solution(B, d);
317 if (cst->n_col == 0) {
318 T = isl_mat_alloc(B->ctx, B->n_col, 0);
325 /* Replace a*g*row = 0 mod g*m by row = 0 mod m */
326 for (i = 0; i < B->n_row; ++i) {
327 isl_seq_gcd(B->row[i] + 1, B->n_col - 1, &D);
328 if (isl_int_is_one(D))
330 if (isl_int_is_zero(D)) {
331 B = isl_mat_drop_rows(B, i, 1);
335 isl_seq_cpy(d->block.data+i, d->block.data+i+1,
344 isl_seq_scale_down(B->row[i] + 1, B->row[i] + 1, D, B->n_col-1);
345 isl_int_gcd(D, D, d->block.data[i]);
349 isl_int_divexact(d->block.data[i], d->block.data[i], D);
353 T = isl_mat_identity(B->ctx, B->n_col);
354 else if (B->n_row == 1)
355 T = parameter_compression_1(B, d);
357 T = parameter_compression_multi(B, d);
358 T = isl_mat_left_hermite(T, 0, NULL, NULL);
361 isl_mat_sub_copy(T->ctx, T->row + 1, cst->row, cst->n_row, 0, 0, 1);
375 /* Given a set of equalities
379 * this function computes a unimodular transformation from a lower-dimensional
380 * space to the original space that bijectively maps the integer points x'
381 * in the lower-dimensional space to the integer points x in the original
382 * space that satisfy the equalities.
384 * The input is given as a matrix B = [ -c M ] and the output is a
385 * matrix that maps [1 x'] to [1 x].
386 * If T2 is not NULL, then *T2 is set to a matrix mapping [1 x] to [1 x'].
388 * First compute the (left) Hermite normal form of M,
390 * M [U1 U2] = M U = H = [H1 0]
392 * M = H Q = [H1 0] [Q1]
395 * with U, Q unimodular, Q = U^{-1} (and H lower triangular).
396 * Define the transformed variables as
398 * x = [U1 U2] [ x1' ] = [U1 U2] [Q1] x
401 * The equalities then become
403 * H1 x1' - c = 0 or x1' = H1^{-1} c = c'
405 * If any of the c' is non-integer, then the original set has no
406 * integer solutions (since the x' are a unimodular transformation
407 * of the x) and a zero-column matrix is returned.
408 * Otherwise, the transformation is given by
410 * x = U1 H1^{-1} c + U2 x2'
412 * The inverse transformation is simply
416 __isl_give isl_mat *isl_mat_variable_compression(__isl_take isl_mat *B,
417 __isl_give isl_mat **T2)
420 struct isl_mat *H = NULL, *C = NULL, *H1, *U = NULL, *U1, *U2, *TC;
429 H = isl_mat_sub_alloc(B, 0, B->n_row, 1, dim);
430 H = isl_mat_left_hermite(H, 0, &U, T2);
431 if (!H || !U || (T2 && !*T2))
434 *T2 = isl_mat_drop_rows(*T2, 0, B->n_row);
435 *T2 = isl_mat_lin_to_aff(*T2);
439 C = isl_mat_alloc(B->ctx, 1+B->n_row, 1);
442 isl_int_set_si(C->row[0][0], 1);
443 isl_mat_sub_neg(C->ctx, C->row+1, B->row, B->n_row, 0, 0, 1);
444 H1 = isl_mat_sub_alloc(H, 0, H->n_row, 0, H->n_row);
445 H1 = isl_mat_lin_to_aff(H1);
446 TC = isl_mat_inverse_product(H1, C);
450 if (!isl_int_is_one(TC->row[0][0])) {
451 for (i = 0; i < B->n_row; ++i) {
452 if (!isl_int_is_divisible_by(TC->row[1+i][0], TC->row[0][0])) {
453 struct isl_ctx *ctx = B->ctx;
461 return isl_mat_alloc(ctx, 1 + dim, 0);
463 isl_seq_scale_down(TC->row[1+i], TC->row[1+i], TC->row[0][0], 1);
465 isl_int_set_si(TC->row[0][0], 1);
467 U1 = isl_mat_sub_alloc(U, 0, U->n_row, 0, B->n_row);
468 U1 = isl_mat_lin_to_aff(U1);
469 U2 = isl_mat_sub_alloc(U, 0, U->n_row, B->n_row, U->n_row - B->n_row);
470 U2 = isl_mat_lin_to_aff(U2);
472 TC = isl_mat_product(U1, TC);
473 TC = isl_mat_aff_direct_sum(TC, U2);
489 /* Use the n equalities of bset to unimodularly transform the
490 * variables x such that n transformed variables x1' have a constant value
491 * and rewrite the constraints of bset in terms of the remaining
492 * transformed variables x2'. The matrix pointed to by T maps
493 * the new variables x2' back to the original variables x, while T2
494 * maps the original variables to the new variables.
496 static struct isl_basic_set *compress_variables(
497 struct isl_basic_set *bset, struct isl_mat **T, struct isl_mat **T2)
499 struct isl_mat *B, *TC;
508 isl_assert(bset->ctx, isl_basic_set_n_param(bset) == 0, goto error);
509 isl_assert(bset->ctx, bset->n_div == 0, goto error);
510 dim = isl_basic_set_n_dim(bset);
511 isl_assert(bset->ctx, bset->n_eq <= dim, goto error);
515 B = isl_mat_sub_alloc6(bset->ctx, bset->eq, 0, bset->n_eq, 0, 1 + dim);
516 TC = isl_mat_variable_compression(B, T2);
519 if (TC->n_col == 0) {
525 return isl_basic_set_set_to_empty(bset);
528 bset = isl_basic_set_preimage(bset, T ? isl_mat_copy(TC) : TC);
533 isl_basic_set_free(bset);
537 struct isl_basic_set *isl_basic_set_remove_equalities(
538 struct isl_basic_set *bset, struct isl_mat **T, struct isl_mat **T2)
546 isl_assert(bset->ctx, isl_basic_set_n_param(bset) == 0, goto error);
547 bset = isl_basic_set_gauss(bset, NULL);
548 if (ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY))
550 bset = compress_variables(bset, T, T2);
553 isl_basic_set_free(bset);
558 /* Check if dimension dim belongs to a residue class
559 * i_dim \equiv r mod m
560 * with m != 1 and if so return m in *modulo and r in *residue.
561 * As a special case, when i_dim has a fixed value v, then
562 * *modulo is set to 0 and *residue to v.
564 * If i_dim does not belong to such a residue class, then *modulo
565 * is set to 1 and *residue is set to 0.
567 int isl_basic_set_dim_residue_class(struct isl_basic_set *bset,
568 int pos, isl_int *modulo, isl_int *residue)
571 struct isl_mat *H = NULL, *U = NULL, *C, *H1, *U1;
575 if (!bset || !modulo || !residue)
578 if (isl_basic_set_plain_dim_is_fixed(bset, pos, residue)) {
579 isl_int_set_si(*modulo, 0);
584 total = isl_basic_set_total_dim(bset);
585 nparam = isl_basic_set_n_param(bset);
586 H = isl_mat_sub_alloc6(bset->ctx, bset->eq, 0, bset->n_eq, 1, total);
587 H = isl_mat_left_hermite(H, 0, &U, NULL);
591 isl_seq_gcd(U->row[nparam + pos]+bset->n_eq,
592 total-bset->n_eq, modulo);
593 if (isl_int_is_zero(*modulo))
594 isl_int_set_si(*modulo, 1);
595 if (isl_int_is_one(*modulo)) {
596 isl_int_set_si(*residue, 0);
602 C = isl_mat_alloc(bset->ctx, 1+bset->n_eq, 1);
605 isl_int_set_si(C->row[0][0], 1);
606 isl_mat_sub_neg(C->ctx, C->row+1, bset->eq, bset->n_eq, 0, 0, 1);
607 H1 = isl_mat_sub_alloc(H, 0, H->n_row, 0, H->n_row);
608 H1 = isl_mat_lin_to_aff(H1);
609 C = isl_mat_inverse_product(H1, C);
611 U1 = isl_mat_sub_alloc(U, nparam+pos, 1, 0, bset->n_eq);
612 U1 = isl_mat_lin_to_aff(U1);
614 C = isl_mat_product(U1, C);
617 if (!isl_int_is_divisible_by(C->row[1][0], C->row[0][0])) {
618 bset = isl_basic_set_copy(bset);
619 bset = isl_basic_set_set_to_empty(bset);
620 isl_basic_set_free(bset);
621 isl_int_set_si(*modulo, 1);
622 isl_int_set_si(*residue, 0);
625 isl_int_divexact(*residue, C->row[1][0], C->row[0][0]);
626 isl_int_fdiv_r(*residue, *residue, *modulo);
635 /* Check if dimension dim belongs to a residue class
636 * i_dim \equiv r mod m
637 * with m != 1 and if so return m in *modulo and r in *residue.
638 * As a special case, when i_dim has a fixed value v, then
639 * *modulo is set to 0 and *residue to v.
641 * If i_dim does not belong to such a residue class, then *modulo
642 * is set to 1 and *residue is set to 0.
644 int isl_set_dim_residue_class(struct isl_set *set,
645 int pos, isl_int *modulo, isl_int *residue)
651 if (!set || !modulo || !residue)
655 isl_int_set_si(*modulo, 0);
656 isl_int_set_si(*residue, 0);
660 if (isl_basic_set_dim_residue_class(set->p[0], pos, modulo, residue)<0)
666 if (isl_int_is_one(*modulo))
672 for (i = 1; i < set->n; ++i) {
673 if (isl_basic_set_dim_residue_class(set->p[i], pos, &m, &r) < 0)
675 isl_int_gcd(*modulo, *modulo, m);
676 isl_int_sub(m, *residue, r);
677 isl_int_gcd(*modulo, *modulo, m);
678 if (!isl_int_is_zero(*modulo))
679 isl_int_fdiv_r(*residue, *residue, *modulo);
680 if (isl_int_is_one(*modulo))