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>
15 #include "isl_map_private.h"
16 #include "isl_equalities.h"
17 #include <isl_val_private.h>
19 /* Given a set of modulo constraints
23 * this function computes a particular solution y_0
25 * The input is given as a matrix B = [ c A ] and a vector d.
27 * The output is matrix containing the solution y_0 or
28 * a zero-column matrix if the constraints admit no integer solution.
30 * The given set of constrains is equivalent to
34 * with D = diag d and x a fresh set of variables.
35 * Reducing both c and A modulo d does not change the
36 * value of y in the solution and may lead to smaller coefficients.
37 * Let M = [ D A ] and [ H 0 ] = M U, the Hermite normal form of M.
43 * [ H 0 ] U^{-1} [ y ] = - c
46 * [ B ] = U^{-1} [ y ]
50 * so B may be chosen arbitrarily, e.g., B = 0, and then
53 * U^{-1} [ y ] = [ 0 ]
61 * If any of the coordinates of this y are non-integer
62 * then the constraints admit no integer solution and
63 * a zero-column matrix is returned.
65 static struct isl_mat *particular_solution(struct isl_mat *B, struct isl_vec *d)
68 struct isl_mat *M = NULL;
69 struct isl_mat *C = NULL;
70 struct isl_mat *U = NULL;
71 struct isl_mat *H = NULL;
72 struct isl_mat *cst = NULL;
73 struct isl_mat *T = NULL;
75 M = isl_mat_alloc(B->ctx, B->n_row, B->n_row + B->n_col - 1);
76 C = isl_mat_alloc(B->ctx, 1 + B->n_row, 1);
79 isl_int_set_si(C->row[0][0], 1);
80 for (i = 0; i < B->n_row; ++i) {
81 isl_seq_clr(M->row[i], B->n_row);
82 isl_int_set(M->row[i][i], d->block.data[i]);
83 isl_int_neg(C->row[1 + i][0], B->row[i][0]);
84 isl_int_fdiv_r(C->row[1+i][0], C->row[1+i][0], M->row[i][i]);
85 for (j = 0; j < B->n_col - 1; ++j)
86 isl_int_fdiv_r(M->row[i][B->n_row + j],
87 B->row[i][1 + j], M->row[i][i]);
89 M = isl_mat_left_hermite(M, 0, &U, NULL);
92 H = isl_mat_sub_alloc(M, 0, B->n_row, 0, B->n_row);
93 H = isl_mat_lin_to_aff(H);
94 C = isl_mat_inverse_product(H, C);
97 for (i = 0; i < B->n_row; ++i) {
98 if (!isl_int_is_divisible_by(C->row[1+i][0], C->row[0][0]))
100 isl_int_divexact(C->row[1+i][0], C->row[1+i][0], C->row[0][0]);
103 cst = isl_mat_alloc(B->ctx, B->n_row, 0);
105 cst = isl_mat_sub_alloc(C, 1, B->n_row, 0, 1);
106 T = isl_mat_sub_alloc(U, B->n_row, B->n_col - 1, 0, B->n_row);
107 cst = isl_mat_product(T, cst);
119 /* Compute and return the matrix
121 * U_1^{-1} diag(d_1, 1, ..., 1)
123 * with U_1 the unimodular completion of the first (and only) row of B.
124 * The columns of this matrix generate the lattice that satisfies
125 * the single (linear) modulo constraint.
127 static struct isl_mat *parameter_compression_1(
128 struct isl_mat *B, struct isl_vec *d)
132 U = isl_mat_alloc(B->ctx, B->n_col - 1, B->n_col - 1);
135 isl_seq_cpy(U->row[0], B->row[0] + 1, B->n_col - 1);
136 U = isl_mat_unimodular_complete(U, 1);
137 U = isl_mat_right_inverse(U);
140 isl_mat_col_mul(U, 0, d->block.data[0], 0);
141 U = isl_mat_lin_to_aff(U);
145 /* Compute a common lattice of solutions to the linear modulo
146 * constraints specified by B and d.
147 * See also the documentation of isl_mat_parameter_compression.
150 * A = [ L_1^{-T} L_2^{-T} ... L_k^{-T} ]
152 * on a common denominator. This denominator D is the lcm of modulos d.
153 * Since L_i = U_i^{-1} diag(d_i, 1, ... 1), we have
154 * L_i^{-T} = U_i^T diag(d_i, 1, ... 1)^{-T} = U_i^T diag(1/d_i, 1, ..., 1).
155 * Putting this on the common denominator, we have
156 * D * L_i^{-T} = U_i^T diag(D/d_i, D, ..., D).
158 static struct isl_mat *parameter_compression_multi(
159 struct isl_mat *B, struct isl_vec *d)
163 struct isl_mat *A = NULL, *U = NULL;
172 A = isl_mat_alloc(B->ctx, size, B->n_row * size);
173 U = isl_mat_alloc(B->ctx, size, size);
176 for (i = 0; i < B->n_row; ++i) {
177 isl_seq_cpy(U->row[0], B->row[i] + 1, size);
178 U = isl_mat_unimodular_complete(U, 1);
181 isl_int_divexact(D, D, d->block.data[i]);
182 for (k = 0; k < U->n_col; ++k)
183 isl_int_mul(A->row[k][i*size+0], D, U->row[0][k]);
184 isl_int_mul(D, D, d->block.data[i]);
185 for (j = 1; j < U->n_row; ++j)
186 for (k = 0; k < U->n_col; ++k)
187 isl_int_mul(A->row[k][i*size+j],
190 A = isl_mat_left_hermite(A, 0, NULL, NULL);
191 T = isl_mat_sub_alloc(A, 0, A->n_row, 0, A->n_row);
192 T = isl_mat_lin_to_aff(T);
195 isl_int_set(T->row[0][0], D);
196 T = isl_mat_right_inverse(T);
199 isl_assert(T->ctx, isl_int_is_one(T->row[0][0]), goto error);
200 T = isl_mat_transpose(T);
213 /* Given a set of modulo constraints
217 * this function returns an affine transformation T,
221 * that bijectively maps the integer vectors y' to integer
222 * vectors y that satisfy the modulo constraints.
224 * This function is inspired by Section 2.5.3
225 * of B. Meister, "Stating and Manipulating Periodicity in the Polytope
226 * Model. Applications to Program Analysis and Optimization".
227 * However, the implementation only follows the algorithm of that
228 * section for computing a particular solution and not for computing
229 * a general homogeneous solution. The latter is incomplete and
230 * may remove some valid solutions.
231 * Instead, we use an adaptation of the algorithm in Section 7 of
232 * B. Meister, S. Verdoolaege, "Polynomial Approximations in the Polytope
233 * Model: Bringing the Power of Quasi-Polynomials to the Masses".
235 * The input is given as a matrix B = [ c A ] and a vector d.
236 * Each element of the vector d corresponds to a row in B.
237 * The output is a lower triangular matrix.
238 * If no integer vector y satisfies the given constraints then
239 * a matrix with zero columns is returned.
241 * We first compute a particular solution y_0 to the given set of
242 * modulo constraints in particular_solution. If no such solution
243 * exists, then we return a zero-columned transformation matrix.
244 * Otherwise, we compute the generic solution to
248 * That is we want to compute G such that
252 * with y'' integer, describes the set of solutions.
254 * We first remove the common factors of each row.
255 * In particular if gcd(A_i,d_i) != 1, then we divide the whole
256 * row i (including d_i) by this common factor. If afterwards gcd(A_i) != 1,
257 * then we divide this row of A by the common factor, unless gcd(A_i) = 0.
258 * In the later case, we simply drop the row (in both A and d).
260 * If there are no rows left in A, then G is the identity matrix. Otherwise,
261 * for each row i, we now determine the lattice of integer vectors
262 * that satisfies this row. Let U_i be the unimodular extension of the
263 * row A_i. This unimodular extension exists because gcd(A_i) = 1.
264 * The first component of
268 * needs to be a multiple of d_i. Let y' = diag(d_i, 1, ..., 1) y''.
271 * y = U_i^{-1} diag(d_i, 1, ..., 1) y''
273 * for arbitrary integer vectors y''. That is, y belongs to the lattice
274 * generated by the columns of L_i = U_i^{-1} diag(d_i, 1, ..., 1).
275 * If there is only one row, then G = L_1.
277 * If there is more than one row left, we need to compute the intersection
278 * of the lattices. That is, we need to compute an L such that
280 * L = L_i L_i' for all i
282 * with L_i' some integer matrices. Let A be constructed as follows
284 * A = [ L_1^{-T} L_2^{-T} ... L_k^{-T} ]
286 * and computed the Hermite Normal Form of A = [ H 0 ] U
289 * L_i^{-T} = H U_{1,i}
293 * H^{-T} = L_i U_{1,i}^T
295 * In other words G = L = H^{-T}.
296 * To ensure that G is lower triangular, we compute and use its Hermite
299 * The affine transformation matrix returned is then
304 * as any y = y_0 + G y' with y' integer is a solution to the original
305 * modulo constraints.
307 struct isl_mat *isl_mat_parameter_compression(
308 struct isl_mat *B, struct isl_vec *d)
311 struct isl_mat *cst = NULL;
312 struct isl_mat *T = NULL;
317 isl_assert(B->ctx, B->n_row == d->size, goto error);
318 cst = particular_solution(B, d);
321 if (cst->n_col == 0) {
322 T = isl_mat_alloc(B->ctx, B->n_col, 0);
329 /* Replace a*g*row = 0 mod g*m by row = 0 mod m */
330 for (i = 0; i < B->n_row; ++i) {
331 isl_seq_gcd(B->row[i] + 1, B->n_col - 1, &D);
332 if (isl_int_is_one(D))
334 if (isl_int_is_zero(D)) {
335 B = isl_mat_drop_rows(B, i, 1);
339 isl_seq_cpy(d->block.data+i, d->block.data+i+1,
348 isl_seq_scale_down(B->row[i] + 1, B->row[i] + 1, D, B->n_col-1);
349 isl_int_gcd(D, D, d->block.data[i]);
353 isl_int_divexact(d->block.data[i], d->block.data[i], D);
357 T = isl_mat_identity(B->ctx, B->n_col);
358 else if (B->n_row == 1)
359 T = parameter_compression_1(B, d);
361 T = parameter_compression_multi(B, d);
362 T = isl_mat_left_hermite(T, 0, NULL, NULL);
365 isl_mat_sub_copy(T->ctx, T->row + 1, cst->row, cst->n_row, 0, 0, 1);
379 /* Given a set of equalities
383 * compute and return an affine transformation T,
387 * that bijectively maps the integer vectors y' to integer
388 * vectors y that satisfy the modulo constraints for some value of x.
390 * Let [H 0] be the Hermite Normal Form of A, i.e.,
394 * Then y is a solution of (*) iff
396 * H^-1 B(y) (= - [I 0] Q x)
398 * is an integer vector. Let d be the common denominator of H^-1.
401 * d H^-1 B(y) = 0 mod d
403 * and compute the solution using isl_mat_parameter_compression.
405 __isl_give isl_mat *isl_mat_parameter_compression_ext(__isl_take isl_mat *B,
406 __isl_take isl_mat *A)
413 return isl_mat_free(B);
415 ctx = isl_mat_get_ctx(A);
418 A = isl_mat_left_hermite(A, 0, NULL, NULL);
419 A = isl_mat_drop_cols(A, n_row, n_col - n_row);
420 A = isl_mat_lin_to_aff(A);
421 A = isl_mat_right_inverse(A);
422 d = isl_vec_alloc(ctx, n_row);
424 d = isl_vec_set(d, A->row[0][0]);
425 A = isl_mat_drop_rows(A, 0, 1);
426 A = isl_mat_drop_cols(A, 0, 1);
427 B = isl_mat_product(A, B);
429 return isl_mat_parameter_compression(B, d);
432 /* Given a set of equalities
436 * this function computes a unimodular transformation from a lower-dimensional
437 * space to the original space that bijectively maps the integer points x'
438 * in the lower-dimensional space to the integer points x in the original
439 * space that satisfy the equalities.
441 * The input is given as a matrix B = [ -c M ] and the output is a
442 * matrix that maps [1 x'] to [1 x].
443 * If T2 is not NULL, then *T2 is set to a matrix mapping [1 x] to [1 x'].
445 * First compute the (left) Hermite normal form of M,
447 * M [U1 U2] = M U = H = [H1 0]
449 * M = H Q = [H1 0] [Q1]
452 * with U, Q unimodular, Q = U^{-1} (and H lower triangular).
453 * Define the transformed variables as
455 * x = [U1 U2] [ x1' ] = [U1 U2] [Q1] x
458 * The equalities then become
460 * H1 x1' - c = 0 or x1' = H1^{-1} c = c'
462 * If any of the c' is non-integer, then the original set has no
463 * integer solutions (since the x' are a unimodular transformation
464 * of the x) and a zero-column matrix is returned.
465 * Otherwise, the transformation is given by
467 * x = U1 H1^{-1} c + U2 x2'
469 * The inverse transformation is simply
473 __isl_give isl_mat *isl_mat_variable_compression(__isl_take isl_mat *B,
474 __isl_give isl_mat **T2)
477 struct isl_mat *H = NULL, *C = NULL, *H1, *U = NULL, *U1, *U2, *TC;
486 H = isl_mat_sub_alloc(B, 0, B->n_row, 1, dim);
487 H = isl_mat_left_hermite(H, 0, &U, T2);
488 if (!H || !U || (T2 && !*T2))
491 *T2 = isl_mat_drop_rows(*T2, 0, B->n_row);
492 *T2 = isl_mat_lin_to_aff(*T2);
496 C = isl_mat_alloc(B->ctx, 1+B->n_row, 1);
499 isl_int_set_si(C->row[0][0], 1);
500 isl_mat_sub_neg(C->ctx, C->row+1, B->row, B->n_row, 0, 0, 1);
501 H1 = isl_mat_sub_alloc(H, 0, H->n_row, 0, H->n_row);
502 H1 = isl_mat_lin_to_aff(H1);
503 TC = isl_mat_inverse_product(H1, C);
507 if (!isl_int_is_one(TC->row[0][0])) {
508 for (i = 0; i < B->n_row; ++i) {
509 if (!isl_int_is_divisible_by(TC->row[1+i][0], TC->row[0][0])) {
510 struct isl_ctx *ctx = B->ctx;
518 return isl_mat_alloc(ctx, 1 + dim, 0);
520 isl_seq_scale_down(TC->row[1+i], TC->row[1+i], TC->row[0][0], 1);
522 isl_int_set_si(TC->row[0][0], 1);
524 U1 = isl_mat_sub_alloc(U, 0, U->n_row, 0, B->n_row);
525 U1 = isl_mat_lin_to_aff(U1);
526 U2 = isl_mat_sub_alloc(U, 0, U->n_row, B->n_row, U->n_row - B->n_row);
527 U2 = isl_mat_lin_to_aff(U2);
529 TC = isl_mat_product(U1, TC);
530 TC = isl_mat_aff_direct_sum(TC, U2);
546 /* Use the n equalities of bset to unimodularly transform the
547 * variables x such that n transformed variables x1' have a constant value
548 * and rewrite the constraints of bset in terms of the remaining
549 * transformed variables x2'. The matrix pointed to by T maps
550 * the new variables x2' back to the original variables x, while T2
551 * maps the original variables to the new variables.
553 static struct isl_basic_set *compress_variables(
554 struct isl_basic_set *bset, struct isl_mat **T, struct isl_mat **T2)
556 struct isl_mat *B, *TC;
565 isl_assert(bset->ctx, isl_basic_set_n_param(bset) == 0, goto error);
566 isl_assert(bset->ctx, bset->n_div == 0, goto error);
567 dim = isl_basic_set_n_dim(bset);
568 isl_assert(bset->ctx, bset->n_eq <= dim, goto error);
572 B = isl_mat_sub_alloc6(bset->ctx, bset->eq, 0, bset->n_eq, 0, 1 + dim);
573 TC = isl_mat_variable_compression(B, T2);
576 if (TC->n_col == 0) {
582 return isl_basic_set_set_to_empty(bset);
585 bset = isl_basic_set_preimage(bset, T ? isl_mat_copy(TC) : TC);
590 isl_basic_set_free(bset);
594 struct isl_basic_set *isl_basic_set_remove_equalities(
595 struct isl_basic_set *bset, struct isl_mat **T, struct isl_mat **T2)
603 isl_assert(bset->ctx, isl_basic_set_n_param(bset) == 0, goto error);
604 bset = isl_basic_set_gauss(bset, NULL);
605 if (ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY))
607 bset = compress_variables(bset, T, T2);
610 isl_basic_set_free(bset);
615 /* Check if dimension dim belongs to a residue class
616 * i_dim \equiv r mod m
617 * with m != 1 and if so return m in *modulo and r in *residue.
618 * As a special case, when i_dim has a fixed value v, then
619 * *modulo is set to 0 and *residue to v.
621 * If i_dim does not belong to such a residue class, then *modulo
622 * is set to 1 and *residue is set to 0.
624 int isl_basic_set_dim_residue_class(struct isl_basic_set *bset,
625 int pos, isl_int *modulo, isl_int *residue)
628 struct isl_mat *H = NULL, *U = NULL, *C, *H1, *U1;
632 if (!bset || !modulo || !residue)
635 if (isl_basic_set_plain_dim_is_fixed(bset, pos, residue)) {
636 isl_int_set_si(*modulo, 0);
641 total = isl_basic_set_total_dim(bset);
642 nparam = isl_basic_set_n_param(bset);
643 H = isl_mat_sub_alloc6(bset->ctx, bset->eq, 0, bset->n_eq, 1, total);
644 H = isl_mat_left_hermite(H, 0, &U, NULL);
648 isl_seq_gcd(U->row[nparam + pos]+bset->n_eq,
649 total-bset->n_eq, modulo);
650 if (isl_int_is_zero(*modulo))
651 isl_int_set_si(*modulo, 1);
652 if (isl_int_is_one(*modulo)) {
653 isl_int_set_si(*residue, 0);
659 C = isl_mat_alloc(bset->ctx, 1+bset->n_eq, 1);
662 isl_int_set_si(C->row[0][0], 1);
663 isl_mat_sub_neg(C->ctx, C->row+1, bset->eq, bset->n_eq, 0, 0, 1);
664 H1 = isl_mat_sub_alloc(H, 0, H->n_row, 0, H->n_row);
665 H1 = isl_mat_lin_to_aff(H1);
666 C = isl_mat_inverse_product(H1, C);
668 U1 = isl_mat_sub_alloc(U, nparam+pos, 1, 0, bset->n_eq);
669 U1 = isl_mat_lin_to_aff(U1);
671 C = isl_mat_product(U1, C);
674 if (!isl_int_is_divisible_by(C->row[1][0], C->row[0][0])) {
675 bset = isl_basic_set_copy(bset);
676 bset = isl_basic_set_set_to_empty(bset);
677 isl_basic_set_free(bset);
678 isl_int_set_si(*modulo, 1);
679 isl_int_set_si(*residue, 0);
682 isl_int_divexact(*residue, C->row[1][0], C->row[0][0]);
683 isl_int_fdiv_r(*residue, *residue, *modulo);
692 /* Check if dimension dim belongs to a residue class
693 * i_dim \equiv r mod m
694 * with m != 1 and if so return m in *modulo and r in *residue.
695 * As a special case, when i_dim has a fixed value v, then
696 * *modulo is set to 0 and *residue to v.
698 * If i_dim does not belong to such a residue class, then *modulo
699 * is set to 1 and *residue is set to 0.
701 int isl_set_dim_residue_class(struct isl_set *set,
702 int pos, isl_int *modulo, isl_int *residue)
708 if (!set || !modulo || !residue)
712 isl_int_set_si(*modulo, 0);
713 isl_int_set_si(*residue, 0);
717 if (isl_basic_set_dim_residue_class(set->p[0], pos, modulo, residue)<0)
723 if (isl_int_is_one(*modulo))
729 for (i = 1; i < set->n; ++i) {
730 if (isl_basic_set_dim_residue_class(set->p[i], pos, &m, &r) < 0)
732 isl_int_gcd(*modulo, *modulo, m);
733 isl_int_sub(m, *residue, r);
734 isl_int_gcd(*modulo, *modulo, m);
735 if (!isl_int_is_zero(*modulo))
736 isl_int_fdiv_r(*residue, *residue, *modulo);
737 if (isl_int_is_one(*modulo))
751 /* Check if dimension "dim" belongs to a residue class
752 * i_dim \equiv r mod m
753 * with m != 1 and if so return m in *modulo and r in *residue.
754 * As a special case, when i_dim has a fixed value v, then
755 * *modulo is set to 0 and *residue to v.
757 * If i_dim does not belong to such a residue class, then *modulo
758 * is set to 1 and *residue is set to 0.
760 int isl_set_dim_residue_class_val(__isl_keep isl_set *set,
761 int pos, __isl_give isl_val **modulo, __isl_give isl_val **residue)
767 *modulo = isl_val_alloc(isl_set_get_ctx(set));
768 *residue = isl_val_alloc(isl_set_get_ctx(set));
769 if (!*modulo || !*residue)
771 if (isl_set_dim_residue_class(set, pos,
772 &(*modulo)->n, &(*residue)->n) < 0)
774 isl_int_set_si((*modulo)->d, 1);
775 isl_int_set_si((*residue)->d, 1);
778 isl_val_free(*modulo);
779 isl_val_free(*residue);