__isl_give isl_point *isl_point_alloc(__isl_take isl_dim *dim, __isl_take isl_vec *vec) { struct isl_point *pnt; if (!dim || !vec) goto error; if (vec->size > 1 + isl_dim_total(dim)) { vec = isl_vec_cow(vec); if (!vec) goto error; vec->size = 1 + isl_dim_total(dim); } pnt = isl_alloc_type(dim->ctx, struct isl_point); if (!pnt) goto error; pnt->ref = 1; pnt->dim = dim; pnt->vec = vec; return pnt; error: isl_dim_free(dim); isl_vec_free(vec); return NULL; }
__isl_give isl_point *isl_point_sub_ui(__isl_take isl_point *pnt, enum isl_dim_type type, int pos, unsigned val) { if (!pnt || isl_point_is_void(pnt)) return pnt; pnt = isl_point_cow(pnt); if (!pnt) return NULL; pnt->vec = isl_vec_cow(pnt->vec); if (!pnt->vec) goto error; if (type == isl_dim_set) pos += isl_dim_size(pnt->dim, isl_dim_param); isl_int_sub_ui(pnt->vec->el[1 + pos], pnt->vec->el[1 + pos], val); return pnt; error: isl_point_free(pnt); 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; }