isl_basic_map_drop_redundant_divs: coalesce divs if possible
[isl.git] / isl_equalities.c
blob0632ddd8a9ef41126522dc52435b8c5758962b57
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_ctx *ctx,
53 struct isl_mat *B, struct isl_vec *d)
55 int i, j;
56 struct isl_mat *M = NULL;
57 struct isl_mat *C = NULL;
58 struct isl_mat *U = NULL;
59 struct isl_mat *H = NULL;
60 struct isl_mat *cst = NULL;
61 struct isl_mat *T = NULL;
63 M = isl_mat_alloc(ctx, B->n_row, B->n_row + B->n_col - 1);
64 C = isl_mat_alloc(ctx, 1 + B->n_row, 1);
65 if (!M || !C)
66 goto error;
67 isl_int_set_si(C->row[0][0], 1);
68 for (i = 0; i < B->n_row; ++i) {
69 isl_seq_clr(M->row[i], B->n_row);
70 isl_int_set(M->row[i][i], d->block.data[i]);
71 isl_int_neg(C->row[1 + i][0], B->row[i][0]);
72 isl_int_fdiv_r(C->row[1+i][0], C->row[1+i][0], M->row[i][i]);
73 for (j = 0; j < B->n_col - 1; ++j)
74 isl_int_fdiv_r(M->row[i][B->n_row + j],
75 B->row[i][1 + j], M->row[i][i]);
77 M = isl_mat_left_hermite(ctx, M, 0, &U, NULL);
78 if (!M || !U)
79 goto error;
80 H = isl_mat_sub_alloc(ctx, M->row, 0, B->n_row, 0, B->n_row);
81 H = isl_mat_lin_to_aff(ctx, H);
82 C = isl_mat_inverse_product(ctx, H, C);
83 if (!C)
84 goto error;
85 for (i = 0; i < B->n_row; ++i) {
86 if (!isl_int_is_divisible_by(C->row[1+i][0], C->row[0][0]))
87 break;
88 isl_int_divexact(C->row[1+i][0], C->row[1+i][0], C->row[0][0]);
90 if (i < B->n_row)
91 cst = isl_mat_alloc(ctx, B->n_row, 0);
92 else
93 cst = isl_mat_sub_alloc(ctx, C->row, 1, B->n_row, 0, 1);
94 T = isl_mat_sub_alloc(ctx, U->row, B->n_row, B->n_col - 1, 0, B->n_row);
95 cst = isl_mat_product(ctx, T, cst);
96 isl_mat_free(ctx, M);
97 isl_mat_free(ctx, C);
98 isl_mat_free(ctx, U);
99 return cst;
100 error:
101 isl_mat_free(ctx, M);
102 isl_mat_free(ctx, C);
103 isl_mat_free(ctx, U);
104 return NULL;
107 /* Compute and return the matrix
109 * U_1^{-1} diag(d_1, 1, ..., 1)
111 * with U_1 the unimodular completion of the first (and only) row of B.
112 * The columns of this matrix generate the lattice that satisfies
113 * the single (linear) modulo constraint.
115 static struct isl_mat *parameter_compression_1(struct isl_ctx *ctx,
116 struct isl_mat *B, struct isl_vec *d)
118 struct isl_mat *U;
120 U = isl_mat_alloc(ctx, B->n_col - 1, B->n_col - 1);
121 if (!U)
122 return NULL;
123 isl_seq_cpy(U->row[0], B->row[0] + 1, B->n_col - 1);
124 U = isl_mat_unimodular_complete(ctx, U, 1);
125 U = isl_mat_right_inverse(ctx, U);
126 if (!U)
127 return NULL;
128 isl_mat_col_mul(U, 0, d->block.data[0], 0);
129 U = isl_mat_lin_to_aff(ctx, U);
130 return U;
131 error:
132 isl_mat_free(ctx, U);
133 return NULL;
136 /* Compute a common lattice of solutions to the linear modulo
137 * constraints specified by B and d.
138 * See also the documentation of isl_mat_parameter_compression.
139 * We put the matrix
141 * A = [ L_1^{-T} L_2^{-T} ... L_k^{-T} ]
143 * on a common denominator. This denominator D is the lcm of modulos d.
144 * Since L_i = U_i^{-1} diag(d_i, 1, ... 1), we have
145 * L_i^{-T} = U_i^T diag(d_i, 1, ... 1)^{-T} = U_i^T diag(1/d_i, 1, ..., 1).
146 * Putting this on the common denominator, we have
147 * D * L_i^{-T} = U_i^T diag(D/d_i, D, ..., D).
149 static struct isl_mat *parameter_compression_multi(struct isl_ctx *ctx,
150 struct isl_mat *B, struct isl_vec *d)
152 int i, j, k;
153 int ok;
154 isl_int D;
155 struct isl_mat *A = NULL, *U = NULL;
156 struct isl_mat *T;
157 unsigned size;
159 isl_int_init(D);
161 isl_vec_lcm(ctx, d, &D);
163 size = B->n_col - 1;
164 A = isl_mat_alloc(ctx, size, B->n_row * size);
165 U = isl_mat_alloc(ctx, size, size);
166 if (!U || !A)
167 goto error;
168 for (i = 0; i < B->n_row; ++i) {
169 isl_seq_cpy(U->row[0], B->row[i] + 1, size);
170 U = isl_mat_unimodular_complete(ctx, U, 1);
171 if (!U)
172 goto error;
173 isl_int_divexact(D, D, d->block.data[i]);
174 for (k = 0; k < U->n_col; ++k)
175 isl_int_mul(A->row[k][i*size+0], D, U->row[0][k]);
176 isl_int_mul(D, D, d->block.data[i]);
177 for (j = 1; j < U->n_row; ++j)
178 for (k = 0; k < U->n_col; ++k)
179 isl_int_mul(A->row[k][i*size+j],
180 D, U->row[j][k]);
182 A = isl_mat_left_hermite(ctx, A, 0, NULL, NULL);
183 T = isl_mat_sub_alloc(ctx, A->row, 0, A->n_row, 0, A->n_row);
184 T = isl_mat_lin_to_aff(ctx, T);
185 isl_int_set(T->row[0][0], D);
186 T = isl_mat_right_inverse(ctx, T);
187 isl_assert(ctx, isl_int_is_one(T->row[0][0]), goto error);
188 T = isl_mat_transpose(ctx, T);
189 isl_mat_free(ctx, A);
190 isl_mat_free(ctx, U);
192 isl_int_clear(D);
193 return T;
194 error:
195 isl_mat_free(ctx, A);
196 isl_mat_free(ctx, U);
197 isl_int_clear(D);
198 return NULL;
201 /* Given a set of modulo constraints
203 * c + A y = 0 mod d
205 * this function returns an affine transformation T,
207 * y = T y'
209 * that bijectively maps the integer vectors y' to integer
210 * vectors y that satisfy the modulo constraints.
212 * This function is inspired by Section 2.5.3
213 * of B. Meister, "Stating and Manipulating Periodicity in the Polytope
214 * Model. Applications to Program Analysis and Optimization".
215 * However, the implementation only follows the algorithm of that
216 * section for computing a particular solution and not for computing
217 * a general homogeneous solution. The latter is incomplete and
218 * may remove some valid solutions.
219 * Instead, we use an adaptation of the algorithm in Section 7 of
220 * B. Meister, S. Verdoolaege, "Polynomial Approximations in the Polytope
221 * Model: Bringing the Power of Quasi-Polynomials to the Masses".
223 * The input is given as a matrix B = [ c A ] and a vector d.
224 * Each element of the vector d corresponds to a row in B.
225 * The output is a lower triangular matrix.
226 * If no integer vector y satisfies the given constraints then
227 * a matrix with zero columns is returned.
229 * We first compute a particular solution y_0 to the given set of
230 * modulo constraints in particular_solution. If no such solution
231 * exists, then we return a zero-columned transformation matrix.
232 * Otherwise, we compute the generic solution to
234 * A y = 0 mod d
236 * That is we want to compute G such that
238 * y = G y''
240 * with y'' integer, describes the set of solutions.
242 * We first remove the common factors of each row.
243 * In particular if gcd(A_i,d_i) != 1, then we divide the whole
244 * row i (including d_i) by this common factor. If afterwards gcd(A_i) != 1,
245 * then we divide this row of A by the common factor, unless gcd(A_i) = 0.
246 * In the later case, we simply drop the row (in both A and d).
248 * If there are no rows left in A, the G is the identity matrix. Otherwise,
249 * for each row i, we now determine the lattice of integer vectors
250 * that satisfies this row. Let U_i be the unimodular extension of the
251 * row A_i. This unimodular extension exists because gcd(A_i) = 1.
252 * The first component of
254 * y' = U_i y
256 * needs to be a multiple of d_i. Let y' = diag(d_i, 1, ..., 1) y''.
257 * Then,
259 * y = U_i^{-1} diag(d_i, 1, ..., 1) y''
261 * for arbitrary integer vectors y''. That is, y belongs to the lattice
262 * generated by the columns of L_i = U_i^{-1} diag(d_i, 1, ..., 1).
263 * If there is only one row, then G = L_1.
265 * If there is more than one row left, we need to compute the intersection
266 * of the lattices. That is, we need to compute an L such that
268 * L = L_i L_i' for all i
270 * with L_i' some integer matrices. Let A be constructed as follows
272 * A = [ L_1^{-T} L_2^{-T} ... L_k^{-T} ]
274 * and computed the Hermite Normal Form of A = [ H 0 ] U
275 * Then,
277 * L_i^{-T} = H U_{1,i}
279 * or
281 * H^{-T} = L_i U_{1,i}^T
283 * In other words G = L = H^{-T}.
284 * To ensure that G is lower triangular, we compute and use its Hermite
285 * normal form.
287 * The affine transformation matrix returned is then
289 * [ 1 0 ]
290 * [ y_0 G ]
292 * as any y = y_0 + G y' with y' integer is a solution to the original
293 * modulo constraints.
295 struct isl_mat *isl_mat_parameter_compression(struct isl_ctx *ctx,
296 struct isl_mat *B, struct isl_vec *d)
298 int i;
299 struct isl_mat *cst = NULL;
300 struct isl_mat *T = NULL;
301 isl_int D;
303 if (!B || !d)
304 goto error;
305 isl_assert(ctx, B->n_row == d->size, goto error);
306 cst = particular_solution(ctx, B, d);
307 if (!cst)
308 goto error;
309 if (cst->n_col == 0) {
310 T = isl_mat_alloc(ctx, B->n_col, 0);
311 isl_mat_free(ctx, cst);
312 isl_mat_free(ctx, B);
313 isl_vec_free(ctx, d);
314 return T;
316 isl_int_init(D);
317 /* Replace a*g*row = 0 mod g*m by row = 0 mod m */
318 for (i = 0; i < B->n_row; ++i) {
319 isl_seq_gcd(B->row[i] + 1, B->n_col - 1, &D);
320 if (isl_int_is_one(D))
321 continue;
322 if (isl_int_is_zero(D)) {
323 B = isl_mat_drop_rows(ctx, B, i, 1);
324 d = isl_vec_cow(ctx, d);
325 if (!B || !d)
326 goto error2;
327 isl_seq_cpy(d->block.data+i, d->block.data+i+1,
328 d->size - (i+1));
329 d->size--;
330 i--;
331 continue;
333 B = isl_mat_cow(ctx, B);
334 if (!B)
335 goto error2;
336 isl_seq_scale_down(B->row[i] + 1, B->row[i] + 1, D, B->n_col-1);
337 isl_int_gcd(D, D, d->block.data[i]);
338 d = isl_vec_cow(ctx, d);
339 if (!d)
340 goto error2;
341 isl_int_divexact(d->block.data[i], d->block.data[i], D);
343 isl_int_clear(D);
344 if (B->n_row == 0)
345 T = isl_mat_identity(ctx, B->n_col);
346 else if (B->n_row == 1)
347 T = parameter_compression_1(ctx, B, d);
348 else
349 T = parameter_compression_multi(ctx, B, d);
350 T = isl_mat_left_hermite(ctx, T, 0, NULL, NULL);
351 if (!T)
352 goto error;
353 isl_mat_sub_copy(ctx, T->row + 1, cst->row, cst->n_row, 0, 0, 1);
354 isl_mat_free(ctx, cst);
355 isl_mat_free(ctx, B);
356 isl_vec_free(ctx, d);
357 return T;
358 error2:
359 isl_int_clear(D);
360 error:
361 isl_mat_free(ctx, cst);
362 isl_mat_free(ctx, B);
363 isl_vec_free(ctx, d);
364 return NULL;
367 /* Given a set of equalities
369 * M x - c = 0
371 * this function computes unimodular transformation from a lower-dimensional
372 * space to the original space that bijectively maps the integer points x'
373 * in the lower-dimensional space to the integer points x in the original
374 * space that satisfy the equalities.
376 * The input is given as a matrix B = [ -c M ] and the out is a
377 * matrix that maps [1 x'] to [1 x].
378 * If T2 is not NULL, then *T2 is set to a matrix mapping [1 x] to [1 x'].
380 * First compute the (left) Hermite normal form of M,
382 * M [U1 U2] = M U = H = [H1 0]
383 * or
384 * M = H Q = [H1 0] [Q1]
385 * [Q2]
387 * with U, Q unimodular, Q = U^{-1} (and H lower triangular).
388 * Define the transformed variables as
390 * x = [U1 U2] [ x1' ] = [U1 U2] [Q1] x
391 * [ x2' ] [Q2]
393 * The equalities then become
395 * H1 x1' - c = 0 or x1' = H1^{-1} c = c'
397 * If any of the c' is non-integer, then the original set has no
398 * integer solutions (since the x' are a unimodular transformation
399 * of the x).
400 * Otherwise, the transformation is given by
402 * x = U1 H1^{-1} c + U2 x2'
404 * The inverse transformation is simply
406 * x2' = Q2 x
408 struct isl_mat *isl_mat_variable_compression(struct isl_ctx *ctx,
409 struct isl_mat *B, struct isl_mat **T2)
411 int i;
412 struct isl_mat *H = NULL, *C = NULL, *H1, *U = NULL, *U1, *U2, *TC;
413 unsigned dim;
415 if (T2)
416 *T2 = NULL;
417 if (!B)
418 goto error;
420 dim = B->n_col - 1;
421 H = isl_mat_sub_alloc(ctx, B->row, 0, B->n_row, 1, dim);
422 H = isl_mat_left_hermite(ctx, H, 0, &U, T2);
423 if (!H || !U || (T2 && !*T2))
424 goto error;
425 if (T2) {
426 *T2 = isl_mat_drop_rows(ctx, *T2, 0, B->n_row);
427 *T2 = isl_mat_lin_to_aff(ctx, *T2);
428 if (!*T2)
429 goto error;
431 C = isl_mat_alloc(ctx, 1+B->n_row, 1);
432 if (!C)
433 goto error;
434 isl_int_set_si(C->row[0][0], 1);
435 isl_mat_sub_neg(ctx, C->row+1, B->row, B->n_row, 0, 0, 1);
436 H1 = isl_mat_sub_alloc(ctx, H->row, 0, H->n_row, 0, H->n_row);
437 H1 = isl_mat_lin_to_aff(ctx, H1);
438 TC = isl_mat_inverse_product(ctx, H1, C);
439 if (!TC)
440 goto error;
441 isl_mat_free(ctx, H);
442 if (!isl_int_is_one(TC->row[0][0])) {
443 for (i = 0; i < B->n_row; ++i) {
444 if (!isl_int_is_divisible_by(TC->row[1+i][0], TC->row[0][0])) {
445 isl_mat_free(ctx, B);
446 isl_mat_free(ctx, TC);
447 isl_mat_free(ctx, U);
448 if (T2) {
449 isl_mat_free(ctx, *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(ctx, U->row, 0, U->n_row, 0, B->n_row);
459 U1 = isl_mat_lin_to_aff(ctx, U1);
460 U2 = isl_mat_sub_alloc(ctx, U->row, 0, U->n_row,
461 B->n_row, U->n_row - B->n_row);
462 U2 = isl_mat_lin_to_aff(ctx, U2);
463 isl_mat_free(ctx, U);
464 TC = isl_mat_product(ctx, U1, TC);
465 TC = isl_mat_aff_direct_sum(ctx, TC, U2);
467 isl_mat_free(ctx, B);
469 return TC;
470 error:
471 isl_mat_free(ctx, B);
472 isl_mat_free(ctx, H);
473 isl_mat_free(ctx, U);
474 if (T2) {
475 isl_mat_free(ctx, *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(struct isl_ctx *ctx,
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(ctx, bset->eq, 0, bset->n_eq, 0, 1 + dim);
508 TC = isl_mat_variable_compression(ctx, B, T2);
509 if (!TC)
510 goto error;
511 if (TC->n_col == 0) {
512 isl_mat_free(ctx, TC);
513 if (T2) {
514 isl_mat_free(ctx, *T2);
515 *T2 = NULL;
517 return isl_basic_set_set_to_empty(bset);
520 bset = isl_basic_set_preimage(bset, T ? isl_mat_copy(ctx, 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->ctx, 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.
554 int isl_basic_set_dim_residue_class(struct isl_basic_set *bset,
555 int pos, isl_int *modulo, isl_int *residue)
557 struct isl_ctx *ctx;
558 struct isl_mat *H = NULL, *U = NULL, *C, *H1, *U1;
559 unsigned total;
560 unsigned nparam;
562 if (!bset || !modulo || !residue)
563 return -1;
565 ctx = bset->ctx;
566 total = isl_basic_set_total_dim(bset);
567 nparam = isl_basic_set_n_param(bset);
568 H = isl_mat_sub_alloc(ctx, bset->eq, 0, bset->n_eq, 1, total);
569 H = isl_mat_left_hermite(ctx, H, 0, &U, NULL);
570 if (!H)
571 return -1;
573 isl_seq_gcd(U->row[nparam + pos]+bset->n_eq,
574 total-bset->n_eq, modulo);
575 if (isl_int_is_zero(*modulo) || isl_int_is_one(*modulo)) {
576 isl_int_set_si(*residue, 0);
577 isl_mat_free(ctx, H);
578 isl_mat_free(ctx, U);
579 return 0;
582 C = isl_mat_alloc(ctx, 1+bset->n_eq, 1);
583 if (!C)
584 goto error;
585 isl_int_set_si(C->row[0][0], 1);
586 isl_mat_sub_neg(ctx, C->row+1, bset->eq, bset->n_eq, 0, 0, 1);
587 H1 = isl_mat_sub_alloc(ctx, H->row, 0, H->n_row, 0, H->n_row);
588 H1 = isl_mat_lin_to_aff(ctx, H1);
589 C = isl_mat_inverse_product(ctx, H1, C);
590 isl_mat_free(ctx, H);
591 U1 = isl_mat_sub_alloc(ctx, U->row, nparam+pos, 1, 0, bset->n_eq);
592 U1 = isl_mat_lin_to_aff(ctx, U1);
593 isl_mat_free(ctx, U);
594 C = isl_mat_product(ctx, U1, C);
595 if (!C)
596 goto error;
597 if (!isl_int_is_divisible_by(C->row[1][0], C->row[0][0])) {
598 bset = isl_basic_set_copy(bset);
599 bset = isl_basic_set_set_to_empty(bset);
600 isl_basic_set_free(bset);
601 isl_int_set_si(*modulo, 0);
602 isl_int_set_si(*residue, 0);
603 return 0;
605 isl_int_divexact(*residue, C->row[1][0], C->row[0][0]);
606 isl_int_fdiv_r(*residue, *residue, *modulo);
607 isl_mat_free(ctx, C);
608 return 0;
609 error:
610 isl_mat_free(ctx, H);
611 isl_mat_free(ctx, U);
612 return -1;