isl_coalesce.c: add missing include
[isl.git] / isl_equalities.c
blobcb16d172d12a2d1c0e1f039d8c9925621b9d8ddd
1 #include "isl_mat.h"
2 #include "isl_seq.h"
3 #include "isl_map_private.h"
4 #include "isl_equalities.h"
6 /* Given a set of modulo constraints
8 * c + A y = 0 mod d
10 * this function computes a particular solution y_0
12 * The input is given as a matrix B = [ c A ] and a vector d.
14 * The output is matrix containing the solution y_0 or
15 * a zero-column matrix if the constraints admit no integer solution.
17 * The given set of constrains is equivalent to
19 * c + A y = -D x
21 * with D = diag d and x a fresh set of variables.
22 * Reducing both c and A modulo d does not change the
23 * value of y in the solution and may lead to smaller coefficients.
24 * Let M = [ D A ] and [ H 0 ] = M U, the Hermite normal form of M.
25 * Then
26 * [ x ]
27 * M [ y ] = - c
28 * and so
29 * [ x ]
30 * [ H 0 ] U^{-1} [ y ] = - c
31 * Let
32 * [ A ] [ x ]
33 * [ B ] = U^{-1} [ y ]
34 * then
35 * H A + 0 B = -c
37 * so B may be chosen arbitrarily, e.g., B = 0, and then
39 * [ x ] = [ -c ]
40 * U^{-1} [ y ] = [ 0 ]
41 * or
42 * [ x ] [ -c ]
43 * [ y ] = U [ 0 ]
44 * specifically,
46 * y = U_{2,1} (-c)
48 * If any of the coordinates of this y are non-integer
49 * then the constraints admit no integer solution and
50 * a zero-column matrix is returned.
52 static struct isl_mat *particular_solution(struct isl_mat *B, struct isl_vec *d)
54 int i, j;
55 struct isl_mat *M = NULL;
56 struct isl_mat *C = NULL;
57 struct isl_mat *U = NULL;
58 struct isl_mat *H = NULL;
59 struct isl_mat *cst = NULL;
60 struct isl_mat *T = NULL;
62 M = isl_mat_alloc(B->ctx, B->n_row, B->n_row + B->n_col - 1);
63 C = isl_mat_alloc(B->ctx, 1 + B->n_row, 1);
64 if (!M || !C)
65 goto error;
66 isl_int_set_si(C->row[0][0], 1);
67 for (i = 0; i < B->n_row; ++i) {
68 isl_seq_clr(M->row[i], B->n_row);
69 isl_int_set(M->row[i][i], d->block.data[i]);
70 isl_int_neg(C->row[1 + i][0], B->row[i][0]);
71 isl_int_fdiv_r(C->row[1+i][0], C->row[1+i][0], M->row[i][i]);
72 for (j = 0; j < B->n_col - 1; ++j)
73 isl_int_fdiv_r(M->row[i][B->n_row + j],
74 B->row[i][1 + j], M->row[i][i]);
76 M = isl_mat_left_hermite(M, 0, &U, NULL);
77 if (!M || !U)
78 goto error;
79 H = isl_mat_sub_alloc(B->ctx, M->row, 0, B->n_row, 0, B->n_row);
80 H = isl_mat_lin_to_aff(H);
81 C = isl_mat_inverse_product(H, C);
82 if (!C)
83 goto error;
84 for (i = 0; i < B->n_row; ++i) {
85 if (!isl_int_is_divisible_by(C->row[1+i][0], C->row[0][0]))
86 break;
87 isl_int_divexact(C->row[1+i][0], C->row[1+i][0], C->row[0][0]);
89 if (i < B->n_row)
90 cst = isl_mat_alloc(B->ctx, B->n_row, 0);
91 else
92 cst = isl_mat_sub_alloc(C->ctx, C->row, 1, B->n_row, 0, 1);
93 T = isl_mat_sub_alloc(U->ctx, U->row, B->n_row, B->n_col - 1, 0, B->n_row);
94 cst = isl_mat_product(T, cst);
95 isl_mat_free(M);
96 isl_mat_free(C);
97 isl_mat_free(U);
98 return cst;
99 error:
100 isl_mat_free(M);
101 isl_mat_free(C);
102 isl_mat_free(U);
103 return NULL;
106 /* Compute and return the matrix
108 * U_1^{-1} diag(d_1, 1, ..., 1)
110 * with U_1 the unimodular completion of the first (and only) row of B.
111 * The columns of this matrix generate the lattice that satisfies
112 * the single (linear) modulo constraint.
114 static struct isl_mat *parameter_compression_1(
115 struct isl_mat *B, struct isl_vec *d)
117 struct isl_mat *U;
119 U = isl_mat_alloc(B->ctx, B->n_col - 1, B->n_col - 1);
120 if (!U)
121 return NULL;
122 isl_seq_cpy(U->row[0], B->row[0] + 1, B->n_col - 1);
123 U = isl_mat_unimodular_complete(U, 1);
124 U = isl_mat_right_inverse(U);
125 if (!U)
126 return NULL;
127 isl_mat_col_mul(U, 0, d->block.data[0], 0);
128 U = isl_mat_lin_to_aff(U);
129 return U;
130 error:
131 isl_mat_free(U);
132 return NULL;
135 /* Compute a common lattice of solutions to the linear modulo
136 * constraints specified by B and d.
137 * See also the documentation of isl_mat_parameter_compression.
138 * We put the matrix
140 * A = [ L_1^{-T} L_2^{-T} ... L_k^{-T} ]
142 * on a common denominator. This denominator D is the lcm of modulos d.
143 * Since L_i = U_i^{-1} diag(d_i, 1, ... 1), we have
144 * L_i^{-T} = U_i^T diag(d_i, 1, ... 1)^{-T} = U_i^T diag(1/d_i, 1, ..., 1).
145 * Putting this on the common denominator, we have
146 * D * L_i^{-T} = U_i^T diag(D/d_i, D, ..., D).
148 static struct isl_mat *parameter_compression_multi(
149 struct isl_mat *B, struct isl_vec *d)
151 int i, j, k;
152 int ok;
153 isl_int D;
154 struct isl_mat *A = NULL, *U = NULL;
155 struct isl_mat *T;
156 unsigned size;
158 isl_int_init(D);
160 isl_vec_lcm(d, &D);
162 size = B->n_col - 1;
163 A = isl_mat_alloc(B->ctx, size, B->n_row * size);
164 U = isl_mat_alloc(B->ctx, size, size);
165 if (!U || !A)
166 goto error;
167 for (i = 0; i < B->n_row; ++i) {
168 isl_seq_cpy(U->row[0], B->row[i] + 1, size);
169 U = isl_mat_unimodular_complete(U, 1);
170 if (!U)
171 goto error;
172 isl_int_divexact(D, D, d->block.data[i]);
173 for (k = 0; k < U->n_col; ++k)
174 isl_int_mul(A->row[k][i*size+0], D, U->row[0][k]);
175 isl_int_mul(D, D, d->block.data[i]);
176 for (j = 1; j < U->n_row; ++j)
177 for (k = 0; k < U->n_col; ++k)
178 isl_int_mul(A->row[k][i*size+j],
179 D, U->row[j][k]);
181 A = isl_mat_left_hermite(A, 0, NULL, NULL);
182 T = isl_mat_sub_alloc(A->ctx, A->row, 0, A->n_row, 0, A->n_row);
183 T = isl_mat_lin_to_aff(T);
184 isl_int_set(T->row[0][0], D);
185 T = isl_mat_right_inverse(T);
186 isl_assert(ctx, isl_int_is_one(T->row[0][0]), goto error);
187 T = isl_mat_transpose(T);
188 isl_mat_free(A);
189 isl_mat_free(U);
191 isl_int_clear(D);
192 return T;
193 error:
194 isl_mat_free(A);
195 isl_mat_free(U);
196 isl_int_clear(D);
197 return NULL;
200 /* Given a set of modulo constraints
202 * c + A y = 0 mod d
204 * this function returns an affine transformation T,
206 * y = T y'
208 * that bijectively maps the integer vectors y' to integer
209 * vectors y that satisfy the modulo constraints.
211 * This function is inspired by Section 2.5.3
212 * of B. Meister, "Stating and Manipulating Periodicity in the Polytope
213 * Model. Applications to Program Analysis and Optimization".
214 * However, the implementation only follows the algorithm of that
215 * section for computing a particular solution and not for computing
216 * a general homogeneous solution. The latter is incomplete and
217 * may remove some valid solutions.
218 * Instead, we use an adaptation of the algorithm in Section 7 of
219 * B. Meister, S. Verdoolaege, "Polynomial Approximations in the Polytope
220 * Model: Bringing the Power of Quasi-Polynomials to the Masses".
222 * The input is given as a matrix B = [ c A ] and a vector d.
223 * Each element of the vector d corresponds to a row in B.
224 * The output is a lower triangular matrix.
225 * If no integer vector y satisfies the given constraints then
226 * a matrix with zero columns is returned.
228 * We first compute a particular solution y_0 to the given set of
229 * modulo constraints in particular_solution. If no such solution
230 * exists, then we return a zero-columned transformation matrix.
231 * Otherwise, we compute the generic solution to
233 * A y = 0 mod d
235 * That is we want to compute G such that
237 * y = G y''
239 * with y'' integer, describes the set of solutions.
241 * We first remove the common factors of each row.
242 * In particular if gcd(A_i,d_i) != 1, then we divide the whole
243 * row i (including d_i) by this common factor. If afterwards gcd(A_i) != 1,
244 * then we divide this row of A by the common factor, unless gcd(A_i) = 0.
245 * In the later case, we simply drop the row (in both A and d).
247 * If there are no rows left in A, the G is the identity matrix. Otherwise,
248 * for each row i, we now determine the lattice of integer vectors
249 * that satisfies this row. Let U_i be the unimodular extension of the
250 * row A_i. This unimodular extension exists because gcd(A_i) = 1.
251 * The first component of
253 * y' = U_i y
255 * needs to be a multiple of d_i. Let y' = diag(d_i, 1, ..., 1) y''.
256 * Then,
258 * y = U_i^{-1} diag(d_i, 1, ..., 1) y''
260 * for arbitrary integer vectors y''. That is, y belongs to the lattice
261 * generated by the columns of L_i = U_i^{-1} diag(d_i, 1, ..., 1).
262 * If there is only one row, then G = L_1.
264 * If there is more than one row left, we need to compute the intersection
265 * of the lattices. That is, we need to compute an L such that
267 * L = L_i L_i' for all i
269 * with L_i' some integer matrices. Let A be constructed as follows
271 * A = [ L_1^{-T} L_2^{-T} ... L_k^{-T} ]
273 * and computed the Hermite Normal Form of A = [ H 0 ] U
274 * Then,
276 * L_i^{-T} = H U_{1,i}
278 * or
280 * H^{-T} = L_i U_{1,i}^T
282 * In other words G = L = H^{-T}.
283 * To ensure that G is lower triangular, we compute and use its Hermite
284 * normal form.
286 * The affine transformation matrix returned is then
288 * [ 1 0 ]
289 * [ y_0 G ]
291 * as any y = y_0 + G y' with y' integer is a solution to the original
292 * modulo constraints.
294 struct isl_mat *isl_mat_parameter_compression(
295 struct isl_mat *B, struct isl_vec *d)
297 int i;
298 struct isl_mat *cst = NULL;
299 struct isl_mat *T = NULL;
300 isl_int D;
302 if (!B || !d)
303 goto error;
304 isl_assert(ctx, B->n_row == d->size, goto error);
305 cst = particular_solution(B, d);
306 if (!cst)
307 goto error;
308 if (cst->n_col == 0) {
309 T = isl_mat_alloc(B->ctx, B->n_col, 0);
310 isl_mat_free(cst);
311 isl_mat_free(B);
312 isl_vec_free(d);
313 return T;
315 isl_int_init(D);
316 /* Replace a*g*row = 0 mod g*m by row = 0 mod m */
317 for (i = 0; i < B->n_row; ++i) {
318 isl_seq_gcd(B->row[i] + 1, B->n_col - 1, &D);
319 if (isl_int_is_one(D))
320 continue;
321 if (isl_int_is_zero(D)) {
322 B = isl_mat_drop_rows(B, i, 1);
323 d = isl_vec_cow(d);
324 if (!B || !d)
325 goto error2;
326 isl_seq_cpy(d->block.data+i, d->block.data+i+1,
327 d->size - (i+1));
328 d->size--;
329 i--;
330 continue;
332 B = isl_mat_cow(B);
333 if (!B)
334 goto error2;
335 isl_seq_scale_down(B->row[i] + 1, B->row[i] + 1, D, B->n_col-1);
336 isl_int_gcd(D, D, d->block.data[i]);
337 d = isl_vec_cow(d);
338 if (!d)
339 goto error2;
340 isl_int_divexact(d->block.data[i], d->block.data[i], D);
342 isl_int_clear(D);
343 if (B->n_row == 0)
344 T = isl_mat_identity(B->ctx, B->n_col);
345 else if (B->n_row == 1)
346 T = parameter_compression_1(B, d);
347 else
348 T = parameter_compression_multi(B, d);
349 T = isl_mat_left_hermite(T, 0, NULL, NULL);
350 if (!T)
351 goto error;
352 isl_mat_sub_copy(T->ctx, T->row + 1, cst->row, cst->n_row, 0, 0, 1);
353 isl_mat_free(cst);
354 isl_mat_free(B);
355 isl_vec_free(d);
356 return T;
357 error2:
358 isl_int_clear(D);
359 error:
360 isl_mat_free(cst);
361 isl_mat_free(B);
362 isl_vec_free(d);
363 return NULL;
366 /* Given a set of equalities
368 * M x - c = 0
370 * this function computes unimodular transformation from a lower-dimensional
371 * space to the original space that bijectively maps the integer points x'
372 * in the lower-dimensional space to the integer points x in the original
373 * space that satisfy the equalities.
375 * The input is given as a matrix B = [ -c M ] and the out is a
376 * matrix that maps [1 x'] to [1 x].
377 * If T2 is not NULL, then *T2 is set to a matrix mapping [1 x] to [1 x'].
379 * First compute the (left) Hermite normal form of M,
381 * M [U1 U2] = M U = H = [H1 0]
382 * or
383 * M = H Q = [H1 0] [Q1]
384 * [Q2]
386 * with U, Q unimodular, Q = U^{-1} (and H lower triangular).
387 * Define the transformed variables as
389 * x = [U1 U2] [ x1' ] = [U1 U2] [Q1] x
390 * [ x2' ] [Q2]
392 * The equalities then become
394 * H1 x1' - c = 0 or x1' = H1^{-1} c = c'
396 * If any of the c' is non-integer, then the original set has no
397 * integer solutions (since the x' are a unimodular transformation
398 * of the x).
399 * Otherwise, the transformation is given by
401 * x = U1 H1^{-1} c + U2 x2'
403 * The inverse transformation is simply
405 * x2' = Q2 x
407 struct isl_mat *isl_mat_variable_compression(struct isl_mat *B,
408 struct isl_mat **T2)
410 int i;
411 struct isl_mat *H = NULL, *C = NULL, *H1, *U = NULL, *U1, *U2, *TC;
412 unsigned dim;
414 if (T2)
415 *T2 = NULL;
416 if (!B)
417 goto error;
419 dim = B->n_col - 1;
420 H = isl_mat_sub_alloc(B->ctx, B->row, 0, B->n_row, 1, dim);
421 H = isl_mat_left_hermite(H, 0, &U, T2);
422 if (!H || !U || (T2 && !*T2))
423 goto error;
424 if (T2) {
425 *T2 = isl_mat_drop_rows(*T2, 0, B->n_row);
426 *T2 = isl_mat_lin_to_aff(*T2);
427 if (!*T2)
428 goto error;
430 C = isl_mat_alloc(B->ctx, 1+B->n_row, 1);
431 if (!C)
432 goto error;
433 isl_int_set_si(C->row[0][0], 1);
434 isl_mat_sub_neg(C->ctx, C->row+1, B->row, B->n_row, 0, 0, 1);
435 H1 = isl_mat_sub_alloc(H->ctx, H->row, 0, H->n_row, 0, H->n_row);
436 H1 = isl_mat_lin_to_aff(H1);
437 TC = isl_mat_inverse_product(H1, C);
438 if (!TC)
439 goto error;
440 isl_mat_free(H);
441 if (!isl_int_is_one(TC->row[0][0])) {
442 for (i = 0; i < B->n_row; ++i) {
443 if (!isl_int_is_divisible_by(TC->row[1+i][0], TC->row[0][0])) {
444 struct isl_ctx *ctx = B->ctx;
445 isl_mat_free(B);
446 isl_mat_free(TC);
447 isl_mat_free(U);
448 if (T2) {
449 isl_mat_free(*T2);
450 *T2 = NULL;
452 return isl_mat_alloc(ctx, 1 + dim, 0);
454 isl_seq_scale_down(TC->row[1+i], TC->row[1+i], TC->row[0][0], 1);
456 isl_int_set_si(TC->row[0][0], 1);
458 U1 = isl_mat_sub_alloc(U->ctx, U->row, 0, U->n_row, 0, B->n_row);
459 U1 = isl_mat_lin_to_aff(U1);
460 U2 = isl_mat_sub_alloc(U->ctx, U->row, 0, U->n_row,
461 B->n_row, U->n_row - B->n_row);
462 U2 = isl_mat_lin_to_aff(U2);
463 isl_mat_free(U);
464 TC = isl_mat_product(U1, TC);
465 TC = isl_mat_aff_direct_sum(TC, U2);
467 isl_mat_free(B);
469 return TC;
470 error:
471 isl_mat_free(B);
472 isl_mat_free(H);
473 isl_mat_free(U);
474 if (T2) {
475 isl_mat_free(*T2);
476 *T2 = NULL;
478 return NULL;
481 /* Use the n equalities of bset to unimodularly transform the
482 * variables x such that n transformed variables x1' have a constant value
483 * and rewrite the constraints of bset in terms of the remaining
484 * transformed variables x2'. The matrix pointed to by T maps
485 * the new variables x2' back to the original variables x, while T2
486 * maps the original variables to the new variables.
488 static struct isl_basic_set *compress_variables(
489 struct isl_basic_set *bset, struct isl_mat **T, struct isl_mat **T2)
491 struct isl_mat *B, *TC;
492 unsigned dim;
494 if (T)
495 *T = NULL;
496 if (T2)
497 *T2 = NULL;
498 if (!bset)
499 goto error;
500 isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error);
501 isl_assert(ctx, bset->n_div == 0, goto error);
502 dim = isl_basic_set_n_dim(bset);
503 isl_assert(ctx, bset->n_eq <= dim, goto error);
504 if (bset->n_eq == 0)
505 return bset;
507 B = isl_mat_sub_alloc(bset->ctx, bset->eq, 0, bset->n_eq, 0, 1 + dim);
508 TC = isl_mat_variable_compression(B, T2);
509 if (!TC)
510 goto error;
511 if (TC->n_col == 0) {
512 isl_mat_free(TC);
513 if (T2) {
514 isl_mat_free(*T2);
515 *T2 = NULL;
517 return isl_basic_set_set_to_empty(bset);
520 bset = isl_basic_set_preimage(bset, T ? isl_mat_copy(TC) : TC);
521 if (T)
522 *T = TC;
523 return bset;
524 error:
525 isl_basic_set_free(bset);
526 return NULL;
529 struct isl_basic_set *isl_basic_set_remove_equalities(
530 struct isl_basic_set *bset, struct isl_mat **T, struct isl_mat **T2)
532 if (T)
533 *T = NULL;
534 if (T2)
535 *T2 = NULL;
536 if (!bset)
537 return NULL;
538 isl_assert(bset->ctx, isl_basic_set_n_param(bset) == 0, goto error);
539 bset = isl_basic_set_gauss(bset, NULL);
540 if (ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY))
541 return bset;
542 bset = compress_variables(bset, T, T2);
543 return bset;
544 error:
545 isl_basic_set_free(bset);
546 *T = NULL;
547 return NULL;
550 /* Check if dimension dim belongs to a residue class
551 * i_dim \equiv r mod m
552 * with m != 1 and if so return m in *modulo and r in *residue.
553 * As a special case, when i_dim has a fixed value v, then
554 * *modulo is set to 0 and *residue to v.
556 * If i_dim does not belong to such a residue class, then *modulo
557 * is set to 1 and *residue is set to 0.
559 int isl_basic_set_dim_residue_class(struct isl_basic_set *bset,
560 int pos, isl_int *modulo, isl_int *residue)
562 struct isl_ctx *ctx;
563 struct isl_mat *H = NULL, *U = NULL, *C, *H1, *U1;
564 unsigned total;
565 unsigned nparam;
567 if (!bset || !modulo || !residue)
568 return -1;
570 if (isl_basic_set_fast_dim_is_fixed(bset, pos, residue)) {
571 isl_int_set_si(*modulo, 0);
572 return 0;
575 ctx = bset->ctx;
576 total = isl_basic_set_total_dim(bset);
577 nparam = isl_basic_set_n_param(bset);
578 H = isl_mat_sub_alloc(bset->ctx, bset->eq, 0, bset->n_eq, 1, total);
579 H = isl_mat_left_hermite(H, 0, &U, NULL);
580 if (!H)
581 return -1;
583 isl_seq_gcd(U->row[nparam + pos]+bset->n_eq,
584 total-bset->n_eq, modulo);
585 if (isl_int_is_zero(*modulo))
586 isl_int_set_si(*modulo, 1);
587 if (isl_int_is_one(*modulo)) {
588 isl_int_set_si(*residue, 0);
589 isl_mat_free(H);
590 isl_mat_free(U);
591 return 0;
594 C = isl_mat_alloc(bset->ctx, 1+bset->n_eq, 1);
595 if (!C)
596 goto error;
597 isl_int_set_si(C->row[0][0], 1);
598 isl_mat_sub_neg(C->ctx, C->row+1, bset->eq, bset->n_eq, 0, 0, 1);
599 H1 = isl_mat_sub_alloc(H->ctx, H->row, 0, H->n_row, 0, H->n_row);
600 H1 = isl_mat_lin_to_aff(H1);
601 C = isl_mat_inverse_product(H1, C);
602 isl_mat_free(H);
603 U1 = isl_mat_sub_alloc(U->ctx, U->row, nparam+pos, 1, 0, bset->n_eq);
604 U1 = isl_mat_lin_to_aff(U1);
605 isl_mat_free(U);
606 C = isl_mat_product(U1, C);
607 if (!C)
608 goto error;
609 if (!isl_int_is_divisible_by(C->row[1][0], C->row[0][0])) {
610 bset = isl_basic_set_copy(bset);
611 bset = isl_basic_set_set_to_empty(bset);
612 isl_basic_set_free(bset);
613 isl_int_set_si(*modulo, 1);
614 isl_int_set_si(*residue, 0);
615 return 0;
617 isl_int_divexact(*residue, C->row[1][0], C->row[0][0]);
618 isl_int_fdiv_r(*residue, *residue, *modulo);
619 isl_mat_free(C);
620 return 0;
621 error:
622 isl_mat_free(H);
623 isl_mat_free(U);
624 return -1;