/* Check if dimension dim belongs to a residue class * i_dim \equiv r mod m * with m != 1 and if so return m in *modulo and r in *residue. * As a special case, when i_dim has a fixed value v, then * *modulo is set to 0 and *residue to v. * * If i_dim does not belong to such a residue class, then *modulo * is set to 1 and *residue is set to 0. */ int isl_set_dim_residue_class(struct isl_set *set, int pos, isl_int *modulo, isl_int *residue) { isl_int m; isl_int r; int i; if (!set || !modulo || !residue) return -1; if (set->n == 0) { isl_int_set_si(*modulo, 0); isl_int_set_si(*residue, 0); return 0; } if (isl_basic_set_dim_residue_class(set->p[0], pos, modulo, residue)<0) return -1; if (set->n == 1) return 0; if (isl_int_is_one(*modulo)) return 0; isl_int_init(m); isl_int_init(r); for (i = 1; i < set->n; ++i) { if (isl_basic_set_dim_residue_class(set->p[i], pos, &m, &r) < 0) goto error; isl_int_gcd(*modulo, *modulo, m); isl_int_sub(m, *residue, r); isl_int_gcd(*modulo, *modulo, m); if (!isl_int_is_zero(*modulo)) isl_int_fdiv_r(*residue, *residue, *modulo); if (isl_int_is_one(*modulo)) break; } isl_int_clear(m); isl_int_clear(r); return 0; error: isl_int_clear(m); isl_int_clear(r); return -1; }
/* Make first row entries in column col of bset1 identical to * those of bset2, using the fact that entry bset1->eq[row][col]=a * is non-zero. Initially, these elements of bset1 are all zero. * For each row i < row, we set * A[i] = a * A[i] + B[i][col] * A[row] * B[i] = a * B[i] * so that * A[i][col] = B[i][col] = a * old(B[i][col]) */ static void construct_column( struct isl_basic_set *bset1, struct isl_basic_set *bset2, unsigned row, unsigned col) { int r; isl_int a; isl_int b; unsigned total; isl_int_init(a); isl_int_init(b); total = 1 + isl_basic_set_n_dim(bset1); for (r = 0; r < row; ++r) { if (isl_int_is_zero(bset2->eq[r][col])) continue; isl_int_gcd(b, bset2->eq[r][col], bset1->eq[row][col]); isl_int_divexact(a, bset1->eq[row][col], b); isl_int_divexact(b, bset2->eq[r][col], b); isl_seq_combine(bset1->eq[r], a, bset1->eq[r], b, bset1->eq[row], total); isl_seq_scale(bset2->eq[r], bset2->eq[r], a, total); } isl_int_clear(a); isl_int_clear(b); delete_row(bset1, row); }
/* Make first row entries in column col of bset1 identical to * those of bset2, using only these entries of the two matrices. * Let t be the last row with different entries. * For each row i < t, we set * A[i] = (A[t][col]-B[t][col]) * A[i] + (B[i][col]-A[i][col) * A[t] * B[i] = (A[t][col]-B[t][col]) * B[i] + (B[i][col]-A[i][col) * B[t] * so that * A[i][col] = B[i][col] = old(A[t][col]*B[i][col]-A[i][col]*B[t][col]) */ static int transform_column( struct isl_basic_set *bset1, struct isl_basic_set *bset2, unsigned row, unsigned col) { int i, t; isl_int a, b, g; unsigned total; for (t = row-1; t >= 0; --t) if (isl_int_ne(bset1->eq[t][col], bset2->eq[t][col])) break; if (t < 0) return 0; total = 1 + isl_basic_set_n_dim(bset1); isl_int_init(a); isl_int_init(b); isl_int_init(g); isl_int_sub(b, bset1->eq[t][col], bset2->eq[t][col]); for (i = 0; i < t; ++i) { isl_int_sub(a, bset2->eq[i][col], bset1->eq[i][col]); isl_int_gcd(g, a, b); isl_int_divexact(a, a, g); isl_int_divexact(g, b, g); isl_seq_combine(bset1->eq[i], g, bset1->eq[i], a, bset1->eq[t], total); isl_seq_combine(bset2->eq[i], g, bset2->eq[i], a, bset2->eq[t], total); } isl_int_clear(a); isl_int_clear(b); isl_int_clear(g); delete_row(bset1, t); delete_row(bset2, t); return 1; }
/* Given a basic set, exploit the equalties in the a basic set to construct * a morphishm that maps the basic set to a lower-dimensional space. * Specifically, the morphism reduces the number of dimensions of type "type". * * This function is a slight generalization of isl_mat_variable_compression * in that it allows the input to be parametric and that it allows for the * compression of either parameters or set variables. * * We first select the equalities of interest, that is those that involve * variables of type "type" and no later variables. * Denote those equalities as * * -C(p) + M x = 0 * * where C(p) depends on the parameters if type == isl_dim_set and * is a constant if type == isl_dim_param. * * First compute the (left) Hermite normal form of M, * * M [U1 U2] = M U = H = [H1 0] * or * M = H Q = [H1 0] [Q1] * [Q2] * * with U, Q unimodular, Q = U^{-1} (and H lower triangular). * Define the transformed variables as * * x = [U1 U2] [ x1' ] = [U1 U2] [Q1] x * [ x2' ] [Q2] * * The equalities then become * * -C(p) + H1 x1' = 0 or x1' = H1^{-1} C(p) = C'(p) * * If the denominator of the constant term does not divide the * the common denominator of the parametric terms, then every * integer point is mapped to a non-integer point and then the original set has no * integer solutions (since the x' are a unimodular transformation * of the x). In this case, an empty morphism is returned. * Otherwise, the transformation is given by * * x = U1 H1^{-1} C(p) + U2 x2' * * The inverse transformation is simply * * x2' = Q2 x * * Both matrices are extended to map the full original space to the full * compressed space. */ __isl_give isl_morph *isl_basic_set_variable_compression( __isl_keep isl_basic_set *bset, enum isl_dim_type type) { unsigned otype; unsigned ntype; unsigned orest; unsigned nrest; int f_eq, n_eq; isl_space *dim; isl_mat *H, *U, *Q, *C = NULL, *H1, *U1, *U2; isl_basic_set *dom, *ran; if (!bset) return NULL; if (isl_basic_set_plain_is_empty(bset)) return isl_morph_empty(bset); isl_assert(bset->ctx, bset->n_div == 0, return NULL); otype = 1 + isl_space_offset(bset->dim, type); ntype = isl_basic_set_dim(bset, type); orest = otype + ntype; nrest = isl_basic_set_total_dim(bset) - (orest - 1); for (f_eq = 0; f_eq < bset->n_eq; ++f_eq) if (isl_seq_first_non_zero(bset->eq[f_eq] + orest, nrest) == -1) break; for (n_eq = 0; f_eq + n_eq < bset->n_eq; ++n_eq) if (isl_seq_first_non_zero(bset->eq[f_eq + n_eq] + otype, ntype) == -1) break; if (n_eq == 0) return isl_morph_identity(bset); H = isl_mat_sub_alloc6(bset->ctx, bset->eq, f_eq, n_eq, otype, ntype); H = isl_mat_left_hermite(H, 0, &U, &Q); if (!H || !U || !Q) goto error; Q = isl_mat_drop_rows(Q, 0, n_eq); Q = isl_mat_diagonal(isl_mat_identity(bset->ctx, otype), Q); Q = isl_mat_diagonal(Q, isl_mat_identity(bset->ctx, nrest)); C = isl_mat_alloc(bset->ctx, 1 + n_eq, otype); if (!C) goto error; isl_int_set_si(C->row[0][0], 1); isl_seq_clr(C->row[0] + 1, otype - 1); isl_mat_sub_neg(C->ctx, C->row + 1, bset->eq + f_eq, n_eq, 0, 0, otype); H1 = isl_mat_sub_alloc(H, 0, H->n_row, 0, H->n_row); H1 = isl_mat_lin_to_aff(H1); C = isl_mat_inverse_product(H1, C); if (!C) goto error; isl_mat_free(H); if (!isl_int_is_one(C->row[0][0])) { int i; isl_int g; isl_int_init(g); for (i = 0; i < n_eq; ++i) { isl_seq_gcd(C->row[1 + i] + 1, otype - 1, &g); isl_int_gcd(g, g, C->row[0][0]); if (!isl_int_is_divisible_by(C->row[1 + i][0], g)) break; } isl_int_clear(g); if (i < n_eq) { isl_mat_free(C); isl_mat_free(U); isl_mat_free(Q); return isl_morph_empty(bset); } C = isl_mat_normalize(C); } U1 = isl_mat_sub_alloc(U, 0, U->n_row, 0, n_eq); U1 = isl_mat_lin_to_aff(U1); U2 = isl_mat_sub_alloc(U, 0, U->n_row, n_eq, U->n_row - n_eq); U2 = isl_mat_lin_to_aff(U2); isl_mat_free(U); C = isl_mat_product(U1, C); C = isl_mat_aff_direct_sum(C, U2); C = insert_parameter_rows(C, otype - 1); C = isl_mat_diagonal(C, isl_mat_identity(bset->ctx, nrest)); dim = isl_space_copy(bset->dim); dim = isl_space_drop_dims(dim, type, 0, ntype); dim = isl_space_add_dims(dim, type, ntype - n_eq); ran = isl_basic_set_universe(dim); dom = copy_equalities(bset, f_eq, n_eq); return isl_morph_alloc(dom, ran, Q, C); error: isl_mat_free(C); isl_mat_free(H); isl_mat_free(U); isl_mat_free(Q); return NULL; }
/* Given a set of modulo constraints * * c + A y = 0 mod d * * this function returns an affine transformation T, * * y = T y' * * that bijectively maps the integer vectors y' to integer * vectors y that satisfy the modulo constraints. * * This function is inspired by Section 2.5.3 * of B. Meister, "Stating and Manipulating Periodicity in the Polytope * Model. Applications to Program Analysis and Optimization". * However, the implementation only follows the algorithm of that * section for computing a particular solution and not for computing * a general homogeneous solution. The latter is incomplete and * may remove some valid solutions. * Instead, we use an adaptation of the algorithm in Section 7 of * B. Meister, S. Verdoolaege, "Polynomial Approximations in the Polytope * Model: Bringing the Power of Quasi-Polynomials to the Masses". * * The input is given as a matrix B = [ c A ] and a vector d. * Each element of the vector d corresponds to a row in B. * The output is a lower triangular matrix. * If no integer vector y satisfies the given constraints then * a matrix with zero columns is returned. * * We first compute a particular solution y_0 to the given set of * modulo constraints in particular_solution. If no such solution * exists, then we return a zero-columned transformation matrix. * Otherwise, we compute the generic solution to * * A y = 0 mod d * * That is we want to compute G such that * * y = G y'' * * with y'' integer, describes the set of solutions. * * We first remove the common factors of each row. * In particular if gcd(A_i,d_i) != 1, then we divide the whole * row i (including d_i) by this common factor. If afterwards gcd(A_i) != 1, * then we divide this row of A by the common factor, unless gcd(A_i) = 0. * In the later case, we simply drop the row (in both A and d). * * If there are no rows left in A, then G is the identity matrix. Otherwise, * for each row i, we now determine the lattice of integer vectors * that satisfies this row. Let U_i be the unimodular extension of the * row A_i. This unimodular extension exists because gcd(A_i) = 1. * The first component of * * y' = U_i y * * needs to be a multiple of d_i. Let y' = diag(d_i, 1, ..., 1) y''. * Then, * * y = U_i^{-1} diag(d_i, 1, ..., 1) y'' * * for arbitrary integer vectors y''. That is, y belongs to the lattice * generated by the columns of L_i = U_i^{-1} diag(d_i, 1, ..., 1). * If there is only one row, then G = L_1. * * If there is more than one row left, we need to compute the intersection * of the lattices. That is, we need to compute an L such that * * L = L_i L_i' for all i * * with L_i' some integer matrices. Let A be constructed as follows * * A = [ L_1^{-T} L_2^{-T} ... L_k^{-T} ] * * and computed the Hermite Normal Form of A = [ H 0 ] U * Then, * * L_i^{-T} = H U_{1,i} * * or * * H^{-T} = L_i U_{1,i}^T * * In other words G = L = H^{-T}. * To ensure that G is lower triangular, we compute and use its Hermite * normal form. * * The affine transformation matrix returned is then * * [ 1 0 ] * [ y_0 G ] * * as any y = y_0 + G y' with y' integer is a solution to the original * modulo constraints. */ struct isl_mat *isl_mat_parameter_compression( struct isl_mat *B, struct isl_vec *d) { int i; struct isl_mat *cst = NULL; struct isl_mat *T = NULL; isl_int D; if (!B || !d) goto error; isl_assert(B->ctx, B->n_row == d->size, goto error); cst = particular_solution(B, d); if (!cst) goto error; if (cst->n_col == 0) { T = isl_mat_alloc(B->ctx, B->n_col, 0); isl_mat_free(cst); isl_mat_free(B); isl_vec_free(d); return T; } isl_int_init(D); /* Replace a*g*row = 0 mod g*m by row = 0 mod m */ for (i = 0; i < B->n_row; ++i) { isl_seq_gcd(B->row[i] + 1, B->n_col - 1, &D); if (isl_int_is_one(D)) continue; if (isl_int_is_zero(D)) { B = isl_mat_drop_rows(B, i, 1); d = isl_vec_cow(d); if (!B || !d) goto error2; isl_seq_cpy(d->block.data+i, d->block.data+i+1, d->size - (i+1)); d->size--; i--; continue; } B = isl_mat_cow(B); if (!B) goto error2; isl_seq_scale_down(B->row[i] + 1, B->row[i] + 1, D, B->n_col-1); isl_int_gcd(D, D, d->block.data[i]); d = isl_vec_cow(d); if (!d) goto error2; isl_int_divexact(d->block.data[i], d->block.data[i], D); } isl_int_clear(D); if (B->n_row == 0) T = isl_mat_identity(B->ctx, B->n_col); else if (B->n_row == 1) T = parameter_compression_1(B, d); else T = parameter_compression_multi(B, d); T = isl_mat_left_hermite(T, 0, NULL, NULL); if (!T) goto error; isl_mat_sub_copy(T->ctx, T->row + 1, cst->row, cst->n_row, 0, 0, 1); isl_mat_free(cst); isl_mat_free(B); isl_vec_free(d); return T; error2: isl_int_clear(D); error: isl_mat_free(cst); isl_mat_free(B); isl_vec_free(d); return NULL; }