isl_equalities.c: remove unused variable
[isl.git] / isl_equalities.c
blob5b6cb61a9e1a3a3f0cefd8d3021babc81d496ade
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 isl_int D;
153 struct isl_mat *A = NULL, *U = NULL;
154 struct isl_mat *T;
155 unsigned size;
157 isl_int_init(D);
159 isl_vec_lcm(d, &D);
161 size = B->n_col - 1;
162 A = isl_mat_alloc(B->ctx, size, B->n_row * size);
163 U = isl_mat_alloc(B->ctx, size, size);
164 if (!U || !A)
165 goto error;
166 for (i = 0; i < B->n_row; ++i) {
167 isl_seq_cpy(U->row[0], B->row[i] + 1, size);
168 U = isl_mat_unimodular_complete(U, 1);
169 if (!U)
170 goto error;
171 isl_int_divexact(D, D, d->block.data[i]);
172 for (k = 0; k < U->n_col; ++k)
173 isl_int_mul(A->row[k][i*size+0], D, U->row[0][k]);
174 isl_int_mul(D, D, d->block.data[i]);
175 for (j = 1; j < U->n_row; ++j)
176 for (k = 0; k < U->n_col; ++k)
177 isl_int_mul(A->row[k][i*size+j],
178 D, U->row[j][k]);
180 A = isl_mat_left_hermite(A, 0, NULL, NULL);
181 T = isl_mat_sub_alloc(A->ctx, A->row, 0, A->n_row, 0, A->n_row);
182 T = isl_mat_lin_to_aff(T);
183 isl_int_set(T->row[0][0], D);
184 T = isl_mat_right_inverse(T);
185 isl_assert(T->ctx, isl_int_is_one(T->row[0][0]), goto error);
186 T = isl_mat_transpose(T);
187 isl_mat_free(A);
188 isl_mat_free(U);
190 isl_int_clear(D);
191 return T;
192 error:
193 isl_mat_free(A);
194 isl_mat_free(U);
195 isl_int_clear(D);
196 return NULL;
199 /* Given a set of modulo constraints
201 * c + A y = 0 mod d
203 * this function returns an affine transformation T,
205 * y = T y'
207 * that bijectively maps the integer vectors y' to integer
208 * vectors y that satisfy the modulo constraints.
210 * This function is inspired by Section 2.5.3
211 * of B. Meister, "Stating and Manipulating Periodicity in the Polytope
212 * Model. Applications to Program Analysis and Optimization".
213 * However, the implementation only follows the algorithm of that
214 * section for computing a particular solution and not for computing
215 * a general homogeneous solution. The latter is incomplete and
216 * may remove some valid solutions.
217 * Instead, we use an adaptation of the algorithm in Section 7 of
218 * B. Meister, S. Verdoolaege, "Polynomial Approximations in the Polytope
219 * Model: Bringing the Power of Quasi-Polynomials to the Masses".
221 * The input is given as a matrix B = [ c A ] and a vector d.
222 * Each element of the vector d corresponds to a row in B.
223 * The output is a lower triangular matrix.
224 * If no integer vector y satisfies the given constraints then
225 * a matrix with zero columns is returned.
227 * We first compute a particular solution y_0 to the given set of
228 * modulo constraints in particular_solution. If no such solution
229 * exists, then we return a zero-columned transformation matrix.
230 * Otherwise, we compute the generic solution to
232 * A y = 0 mod d
234 * That is we want to compute G such that
236 * y = G y''
238 * with y'' integer, describes the set of solutions.
240 * We first remove the common factors of each row.
241 * In particular if gcd(A_i,d_i) != 1, then we divide the whole
242 * row i (including d_i) by this common factor. If afterwards gcd(A_i) != 1,
243 * then we divide this row of A by the common factor, unless gcd(A_i) = 0.
244 * In the later case, we simply drop the row (in both A and d).
246 * If there are no rows left in A, the G is the identity matrix. Otherwise,
247 * for each row i, we now determine the lattice of integer vectors
248 * that satisfies this row. Let U_i be the unimodular extension of the
249 * row A_i. This unimodular extension exists because gcd(A_i) = 1.
250 * The first component of
252 * y' = U_i y
254 * needs to be a multiple of d_i. Let y' = diag(d_i, 1, ..., 1) y''.
255 * Then,
257 * y = U_i^{-1} diag(d_i, 1, ..., 1) y''
259 * for arbitrary integer vectors y''. That is, y belongs to the lattice
260 * generated by the columns of L_i = U_i^{-1} diag(d_i, 1, ..., 1).
261 * If there is only one row, then G = L_1.
263 * If there is more than one row left, we need to compute the intersection
264 * of the lattices. That is, we need to compute an L such that
266 * L = L_i L_i' for all i
268 * with L_i' some integer matrices. Let A be constructed as follows
270 * A = [ L_1^{-T} L_2^{-T} ... L_k^{-T} ]
272 * and computed the Hermite Normal Form of A = [ H 0 ] U
273 * Then,
275 * L_i^{-T} = H U_{1,i}
277 * or
279 * H^{-T} = L_i U_{1,i}^T
281 * In other words G = L = H^{-T}.
282 * To ensure that G is lower triangular, we compute and use its Hermite
283 * normal form.
285 * The affine transformation matrix returned is then
287 * [ 1 0 ]
288 * [ y_0 G ]
290 * as any y = y_0 + G y' with y' integer is a solution to the original
291 * modulo constraints.
293 struct isl_mat *isl_mat_parameter_compression(
294 struct isl_mat *B, struct isl_vec *d)
296 int i;
297 struct isl_mat *cst = NULL;
298 struct isl_mat *T = NULL;
299 isl_int D;
301 if (!B || !d)
302 goto error;
303 isl_assert(B->ctx, B->n_row == d->size, goto error);
304 cst = particular_solution(B, d);
305 if (!cst)
306 goto error;
307 if (cst->n_col == 0) {
308 T = isl_mat_alloc(B->ctx, B->n_col, 0);
309 isl_mat_free(cst);
310 isl_mat_free(B);
311 isl_vec_free(d);
312 return T;
314 isl_int_init(D);
315 /* Replace a*g*row = 0 mod g*m by row = 0 mod m */
316 for (i = 0; i < B->n_row; ++i) {
317 isl_seq_gcd(B->row[i] + 1, B->n_col - 1, &D);
318 if (isl_int_is_one(D))
319 continue;
320 if (isl_int_is_zero(D)) {
321 B = isl_mat_drop_rows(B, i, 1);
322 d = isl_vec_cow(d);
323 if (!B || !d)
324 goto error2;
325 isl_seq_cpy(d->block.data+i, d->block.data+i+1,
326 d->size - (i+1));
327 d->size--;
328 i--;
329 continue;
331 B = isl_mat_cow(B);
332 if (!B)
333 goto error2;
334 isl_seq_scale_down(B->row[i] + 1, B->row[i] + 1, D, B->n_col-1);
335 isl_int_gcd(D, D, d->block.data[i]);
336 d = isl_vec_cow(d);
337 if (!d)
338 goto error2;
339 isl_int_divexact(d->block.data[i], d->block.data[i], D);
341 isl_int_clear(D);
342 if (B->n_row == 0)
343 T = isl_mat_identity(B->ctx, B->n_col);
344 else if (B->n_row == 1)
345 T = parameter_compression_1(B, d);
346 else
347 T = parameter_compression_multi(B, d);
348 T = isl_mat_left_hermite(T, 0, NULL, NULL);
349 if (!T)
350 goto error;
351 isl_mat_sub_copy(T->ctx, T->row + 1, cst->row, cst->n_row, 0, 0, 1);
352 isl_mat_free(cst);
353 isl_mat_free(B);
354 isl_vec_free(d);
355 return T;
356 error2:
357 isl_int_clear(D);
358 error:
359 isl_mat_free(cst);
360 isl_mat_free(B);
361 isl_vec_free(d);
362 return NULL;
365 /* Given a set of equalities
367 * M x - c = 0
369 * this function computes unimodular transformation from a lower-dimensional
370 * space to the original space that bijectively maps the integer points x'
371 * in the lower-dimensional space to the integer points x in the original
372 * space that satisfy the equalities.
374 * The input is given as a matrix B = [ -c M ] and the out is a
375 * matrix that maps [1 x'] to [1 x].
376 * If T2 is not NULL, then *T2 is set to a matrix mapping [1 x] to [1 x'].
378 * First compute the (left) Hermite normal form of M,
380 * M [U1 U2] = M U = H = [H1 0]
381 * or
382 * M = H Q = [H1 0] [Q1]
383 * [Q2]
385 * with U, Q unimodular, Q = U^{-1} (and H lower triangular).
386 * Define the transformed variables as
388 * x = [U1 U2] [ x1' ] = [U1 U2] [Q1] x
389 * [ x2' ] [Q2]
391 * The equalities then become
393 * H1 x1' - c = 0 or x1' = H1^{-1} c = c'
395 * If any of the c' is non-integer, then the original set has no
396 * integer solutions (since the x' are a unimodular transformation
397 * of the x).
398 * Otherwise, the transformation is given by
400 * x = U1 H1^{-1} c + U2 x2'
402 * The inverse transformation is simply
404 * x2' = Q2 x
406 struct isl_mat *isl_mat_variable_compression(struct isl_mat *B,
407 struct isl_mat **T2)
409 int i;
410 struct isl_mat *H = NULL, *C = NULL, *H1, *U = NULL, *U1, *U2, *TC;
411 unsigned dim;
413 if (T2)
414 *T2 = NULL;
415 if (!B)
416 goto error;
418 dim = B->n_col - 1;
419 H = isl_mat_sub_alloc(B->ctx, B->row, 0, B->n_row, 1, dim);
420 H = isl_mat_left_hermite(H, 0, &U, T2);
421 if (!H || !U || (T2 && !*T2))
422 goto error;
423 if (T2) {
424 *T2 = isl_mat_drop_rows(*T2, 0, B->n_row);
425 *T2 = isl_mat_lin_to_aff(*T2);
426 if (!*T2)
427 goto error;
429 C = isl_mat_alloc(B->ctx, 1+B->n_row, 1);
430 if (!C)
431 goto error;
432 isl_int_set_si(C->row[0][0], 1);
433 isl_mat_sub_neg(C->ctx, C->row+1, B->row, B->n_row, 0, 0, 1);
434 H1 = isl_mat_sub_alloc(H->ctx, H->row, 0, H->n_row, 0, H->n_row);
435 H1 = isl_mat_lin_to_aff(H1);
436 TC = isl_mat_inverse_product(H1, C);
437 if (!TC)
438 goto error;
439 isl_mat_free(H);
440 if (!isl_int_is_one(TC->row[0][0])) {
441 for (i = 0; i < B->n_row; ++i) {
442 if (!isl_int_is_divisible_by(TC->row[1+i][0], TC->row[0][0])) {
443 struct isl_ctx *ctx = B->ctx;
444 isl_mat_free(B);
445 isl_mat_free(TC);
446 isl_mat_free(U);
447 if (T2) {
448 isl_mat_free(*T2);
449 *T2 = NULL;
451 return isl_mat_alloc(ctx, 1 + dim, 0);
453 isl_seq_scale_down(TC->row[1+i], TC->row[1+i], TC->row[0][0], 1);
455 isl_int_set_si(TC->row[0][0], 1);
457 U1 = isl_mat_sub_alloc(U->ctx, U->row, 0, U->n_row, 0, B->n_row);
458 U1 = isl_mat_lin_to_aff(U1);
459 U2 = isl_mat_sub_alloc(U->ctx, U->row, 0, U->n_row,
460 B->n_row, U->n_row - B->n_row);
461 U2 = isl_mat_lin_to_aff(U2);
462 isl_mat_free(U);
463 TC = isl_mat_product(U1, TC);
464 TC = isl_mat_aff_direct_sum(TC, U2);
466 isl_mat_free(B);
468 return TC;
469 error:
470 isl_mat_free(B);
471 isl_mat_free(H);
472 isl_mat_free(U);
473 if (T2) {
474 isl_mat_free(*T2);
475 *T2 = NULL;
477 return NULL;
480 /* Use the n equalities of bset to unimodularly transform the
481 * variables x such that n transformed variables x1' have a constant value
482 * and rewrite the constraints of bset in terms of the remaining
483 * transformed variables x2'. The matrix pointed to by T maps
484 * the new variables x2' back to the original variables x, while T2
485 * maps the original variables to the new variables.
487 static struct isl_basic_set *compress_variables(
488 struct isl_basic_set *bset, struct isl_mat **T, struct isl_mat **T2)
490 struct isl_mat *B, *TC;
491 unsigned dim;
493 if (T)
494 *T = NULL;
495 if (T2)
496 *T2 = NULL;
497 if (!bset)
498 goto error;
499 isl_assert(bset->ctx, isl_basic_set_n_param(bset) == 0, goto error);
500 isl_assert(bset->ctx, bset->n_div == 0, goto error);
501 dim = isl_basic_set_n_dim(bset);
502 isl_assert(bset->ctx, bset->n_eq <= dim, goto error);
503 if (bset->n_eq == 0)
504 return bset;
506 B = isl_mat_sub_alloc(bset->ctx, bset->eq, 0, bset->n_eq, 0, 1 + dim);
507 TC = isl_mat_variable_compression(B, T2);
508 if (!TC)
509 goto error;
510 if (TC->n_col == 0) {
511 isl_mat_free(TC);
512 if (T2) {
513 isl_mat_free(*T2);
514 *T2 = NULL;
516 return isl_basic_set_set_to_empty(bset);
519 bset = isl_basic_set_preimage(bset, T ? isl_mat_copy(TC) : TC);
520 if (T)
521 *T = TC;
522 return bset;
523 error:
524 isl_basic_set_free(bset);
525 return NULL;
528 struct isl_basic_set *isl_basic_set_remove_equalities(
529 struct isl_basic_set *bset, struct isl_mat **T, struct isl_mat **T2)
531 if (T)
532 *T = NULL;
533 if (T2)
534 *T2 = NULL;
535 if (!bset)
536 return NULL;
537 isl_assert(bset->ctx, isl_basic_set_n_param(bset) == 0, goto error);
538 bset = isl_basic_set_gauss(bset, NULL);
539 if (ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY))
540 return bset;
541 bset = compress_variables(bset, T, T2);
542 return bset;
543 error:
544 isl_basic_set_free(bset);
545 *T = NULL;
546 return NULL;
549 /* Check if dimension dim belongs to a residue class
550 * i_dim \equiv r mod m
551 * with m != 1 and if so return m in *modulo and r in *residue.
552 * As a special case, when i_dim has a fixed value v, then
553 * *modulo is set to 0 and *residue to v.
555 * If i_dim does not belong to such a residue class, then *modulo
556 * is set to 1 and *residue is set to 0.
558 int isl_basic_set_dim_residue_class(struct isl_basic_set *bset,
559 int pos, isl_int *modulo, isl_int *residue)
561 struct isl_ctx *ctx;
562 struct isl_mat *H = NULL, *U = NULL, *C, *H1, *U1;
563 unsigned total;
564 unsigned nparam;
566 if (!bset || !modulo || !residue)
567 return -1;
569 if (isl_basic_set_fast_dim_is_fixed(bset, pos, residue)) {
570 isl_int_set_si(*modulo, 0);
571 return 0;
574 ctx = bset->ctx;
575 total = isl_basic_set_total_dim(bset);
576 nparam = isl_basic_set_n_param(bset);
577 H = isl_mat_sub_alloc(bset->ctx, bset->eq, 0, bset->n_eq, 1, total);
578 H = isl_mat_left_hermite(H, 0, &U, NULL);
579 if (!H)
580 return -1;
582 isl_seq_gcd(U->row[nparam + pos]+bset->n_eq,
583 total-bset->n_eq, modulo);
584 if (isl_int_is_zero(*modulo))
585 isl_int_set_si(*modulo, 1);
586 if (isl_int_is_one(*modulo)) {
587 isl_int_set_si(*residue, 0);
588 isl_mat_free(H);
589 isl_mat_free(U);
590 return 0;
593 C = isl_mat_alloc(bset->ctx, 1+bset->n_eq, 1);
594 if (!C)
595 goto error;
596 isl_int_set_si(C->row[0][0], 1);
597 isl_mat_sub_neg(C->ctx, C->row+1, bset->eq, bset->n_eq, 0, 0, 1);
598 H1 = isl_mat_sub_alloc(H->ctx, H->row, 0, H->n_row, 0, H->n_row);
599 H1 = isl_mat_lin_to_aff(H1);
600 C = isl_mat_inverse_product(H1, C);
601 isl_mat_free(H);
602 U1 = isl_mat_sub_alloc(U->ctx, U->row, nparam+pos, 1, 0, bset->n_eq);
603 U1 = isl_mat_lin_to_aff(U1);
604 isl_mat_free(U);
605 C = isl_mat_product(U1, C);
606 if (!C)
607 goto error;
608 if (!isl_int_is_divisible_by(C->row[1][0], C->row[0][0])) {
609 bset = isl_basic_set_copy(bset);
610 bset = isl_basic_set_set_to_empty(bset);
611 isl_basic_set_free(bset);
612 isl_int_set_si(*modulo, 1);
613 isl_int_set_si(*residue, 0);
614 return 0;
616 isl_int_divexact(*residue, C->row[1][0], C->row[0][0]);
617 isl_int_fdiv_r(*residue, *residue, *modulo);
618 isl_mat_free(C);
619 return 0;
620 error:
621 isl_mat_free(H);
622 isl_mat_free(U);
623 return -1;