/* 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); }
/* Use the n equalities of bset to unimodularly transform the * variables x such that n transformed variables x1' have a constant value * and rewrite the constraints of bset in terms of the remaining * transformed variables x2'. The matrix pointed to by T maps * the new variables x2' back to the original variables x, while T2 * maps the original variables to the new variables. */ static struct isl_basic_set *compress_variables( struct isl_basic_set *bset, struct isl_mat **T, struct isl_mat **T2) { struct isl_mat *B, *TC; unsigned dim; if (T) *T = NULL; if (T2) *T2 = NULL; if (!bset) goto error; isl_assert(bset->ctx, isl_basic_set_n_param(bset) == 0, goto error); isl_assert(bset->ctx, bset->n_div == 0, goto error); dim = isl_basic_set_n_dim(bset); isl_assert(bset->ctx, bset->n_eq <= dim, goto error); if (bset->n_eq == 0) return bset; B = isl_mat_sub_alloc6(bset->ctx, bset->eq, 0, bset->n_eq, 0, 1 + dim); TC = isl_mat_variable_compression(B, T2); if (!TC) goto error; if (TC->n_col == 0) { isl_mat_free(TC); if (T2) { isl_mat_free(*T2); *T2 = NULL; } return isl_basic_set_set_to_empty(bset); } bset = isl_basic_set_preimage(bset, T ? isl_mat_copy(TC) : TC); if (T) *T = TC; return bset; error: isl_basic_set_free(bset); return NULL; }
/* The implementation is based on Section 5.2 of Michael Karr, * "Affine Relationships Among Variables of a Program", * except that the echelon form we use starts from the last column * and that we are dealing with integer coefficients. */ static struct isl_basic_set *affine_hull( struct isl_basic_set *bset1, struct isl_basic_set *bset2) { unsigned total; int col; int row; if (!bset1 || !bset2) goto error; total = 1 + isl_basic_set_n_dim(bset1); row = 0; for (col = total-1; col >= 0; --col) { int is_zero1 = row >= bset1->n_eq || isl_int_is_zero(bset1->eq[row][col]); int is_zero2 = row >= bset2->n_eq || isl_int_is_zero(bset2->eq[row][col]); if (!is_zero1 && !is_zero2) { set_common_multiple(bset1, bset2, row, col); ++row; } else if (!is_zero1 && is_zero2) { construct_column(bset1, bset2, row, col); } else if (is_zero1 && !is_zero2) { construct_column(bset2, bset1, row, col); } else { if (transform_column(bset1, bset2, row, col)) --row; } } isl_assert(bset1->ctx, row == bset1->n_eq, goto error); isl_basic_set_free(bset2); bset1 = isl_basic_set_normalize_constraints(bset1); return bset1; error: isl_basic_set_free(bset1); isl_basic_set_free(bset2); return NULL; }
/* Given an unbounded tableau and an integer point satisfying the tableau, * construct an initial affine hull containing the recession cone * shifted to the given point. * * The unbounded directions are taken from the last rows of the basis, * which is assumed to have been initialized appropriately. */ static __isl_give isl_basic_set *initial_hull(struct isl_tab *tab, __isl_take isl_vec *vec) { int i; int k; struct isl_basic_set *bset = NULL; struct isl_ctx *ctx; unsigned dim; if (!vec || !tab) return NULL; ctx = vec->ctx; isl_assert(ctx, vec->size != 0, goto error); bset = isl_basic_set_alloc(ctx, 0, vec->size - 1, 0, vec->size - 1, 0); if (!bset) goto error; dim = isl_basic_set_n_dim(bset) - tab->n_unbounded; for (i = 0; i < dim; ++i) { k = isl_basic_set_alloc_equality(bset); if (k < 0) goto error; isl_seq_cpy(bset->eq[k] + 1, tab->basis->row[1 + i] + 1, vec->size - 1); isl_seq_inner_product(bset->eq[k] + 1, vec->el +1, vec->size - 1, &bset->eq[k][0]); isl_int_neg(bset->eq[k][0], bset->eq[k][0]); } bset->sample = vec; bset = isl_basic_set_gauss(bset, NULL); return bset; error: isl_basic_set_free(bset); isl_vec_free(vec); return NULL; }
/* 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; }
/* Look for all equalities satisfied by the integer points in bset, * which is assumed to be bounded. * * The equalities are obtained by successively looking for * a point that is affinely independent of the points found so far. * In particular, for each equality satisfied by the points so far, * we check if there is any point on a hyperplane parallel to the * corresponding hyperplane shifted by at least one (in either direction). */ static struct isl_basic_set *uset_affine_hull_bounded(struct isl_basic_set *bset) { struct isl_vec *sample = NULL; struct isl_basic_set *hull; struct isl_tab *tab = NULL; unsigned dim; if (isl_basic_set_plain_is_empty(bset)) return bset; dim = isl_basic_set_n_dim(bset); if (bset->sample && bset->sample->size == 1 + dim) { int contains = isl_basic_set_contains(bset, bset->sample); if (contains < 0) goto error; if (contains) { if (dim == 0) return bset; sample = isl_vec_copy(bset->sample); } else { isl_vec_free(bset->sample); bset->sample = NULL; } } tab = isl_tab_from_basic_set(bset); if (!tab) goto error; if (tab->empty) { isl_tab_free(tab); isl_vec_free(sample); return isl_basic_set_set_to_empty(bset); } if (isl_tab_track_bset(tab, isl_basic_set_copy(bset)) < 0) goto error; if (!sample) { struct isl_tab_undo *snap; snap = isl_tab_snap(tab); sample = isl_tab_sample(tab); if (isl_tab_rollback(tab, snap) < 0) goto error; isl_vec_free(tab->bmap->sample); tab->bmap->sample = isl_vec_copy(sample); } if (!sample) goto error; if (sample->size == 0) { isl_tab_free(tab); isl_vec_free(sample); return isl_basic_set_set_to_empty(bset); } hull = isl_basic_set_from_vec(sample); isl_basic_set_free(bset); hull = extend_affine_hull(tab, hull); isl_tab_free(tab); return hull; error: isl_vec_free(sample); isl_tab_free(tab); isl_basic_set_free(bset); return NULL; }