3 #include "isl_piplib.h"
4 #include "isl_sample_piplib.h"
6 static void swap_inequality(struct isl_basic_set *bset, int a, int b)
8 isl_int *t = bset->ineq[a];
9 bset->ineq[a] = bset->ineq[b];
13 static struct isl_mat *independent_bounds(struct isl_ctx *ctx,
14 struct isl_basic_set *bset)
17 struct isl_mat *dirs = NULL;
18 struct isl_mat *bounds = NULL;
23 bounds = isl_mat_alloc(ctx, 1+bset->dim, 1+bset->dim);
27 isl_int_set_si(bounds->row[0][0], 1);
28 isl_seq_clr(bounds->row[0]+1, bset->dim);
31 if (bset->n_ineq == 0)
34 dirs = isl_mat_alloc(ctx, bset->dim, bset->dim);
36 isl_mat_free(ctx, bounds);
39 isl_seq_cpy(dirs->row[0], bset->ineq[0]+1, dirs->n_col);
40 isl_seq_cpy(bounds->row[1], bset->ineq[0], bounds->n_col);
41 for (j = 1, n = 1; n < bset->dim && j < bset->n_ineq; ++j) {
44 isl_seq_cpy(dirs->row[n], bset->ineq[j]+1, dirs->n_col);
46 pos = isl_seq_first_non_zero(dirs->row[n], dirs->n_col);
49 for (i = 0; i < n; ++i) {
51 pos_i = isl_seq_first_non_zero(dirs->row[i], dirs->n_col);
56 isl_seq_elim(dirs->row[n], dirs->row[i], pos,
58 pos = isl_seq_first_non_zero(dirs->row[n], dirs->n_col);
66 isl_int *t = dirs->row[n];
67 for (k = n; k > i; --k)
68 dirs->row[k] = dirs->row[k-1];
72 isl_seq_cpy(bounds->row[n], bset->ineq[j], bounds->n_col);
74 isl_mat_free(ctx, dirs);
79 /* Skew into positive orthant and project out lineality space */
80 static struct isl_basic_set *isl_basic_set_skew_to_positive_orthant(
81 struct isl_basic_set *bset, struct isl_mat **T)
83 struct isl_mat *U = NULL;
84 struct isl_mat *bounds = NULL;
86 unsigned old_dim, new_dim;
94 isl_assert(ctx, bset->nparam == 0, goto error);
95 isl_assert(ctx, bset->n_div == 0, goto error);
96 isl_assert(ctx, bset->n_eq == 0, goto error);
98 /* Try to move (multiples of) unit rows up. */
99 for (i = 0, j = 0; i < bset->n_ineq; ++i) {
100 int pos = isl_seq_first_non_zero(bset->ineq[i]+1,
104 if (isl_seq_first_non_zero(bset->ineq[i]+1+pos+1,
105 bset->dim-pos-1) >= 0)
108 swap_inequality(bset, i, j);
111 bounds = independent_bounds(ctx, bset);
115 new_dim = bounds->n_row - 1;
116 bounds = isl_mat_left_hermite(ctx, bounds, 1, &U, NULL);
119 U = isl_mat_drop_cols(ctx, U, 1 + new_dim, old_dim - new_dim);
120 bset = isl_basic_set_preimage(ctx, bset, isl_mat_copy(ctx, U));
124 isl_mat_free(ctx, bounds);
127 isl_mat_free(ctx, bounds);
128 isl_mat_free(ctx, U);
129 isl_basic_set_free(bset);
133 struct isl_vec *isl_pip_basic_set_sample(struct isl_basic_set *bset)
135 PipOptions *options = NULL;
136 PipMatrix *domain = NULL;
137 PipQuast *sol = NULL;
138 struct isl_vec *vec = NULL;
146 isl_assert(ctx, bset->nparam == 0, goto error);
147 isl_assert(ctx, bset->n_div == 0, goto error);
148 bset = isl_basic_set_skew_to_positive_orthant(bset, &T);
152 domain = isl_basic_map_to_pip((struct isl_basic_map *)bset, 0, 0, 0);
156 options = pip_options_init();
159 sol = pip_solve(domain, NULL, -1, options);
163 vec = isl_vec_alloc(ctx, 0);
164 isl_mat_free(ctx, T);
168 vec = isl_vec_alloc(ctx, 1 + dim);
171 isl_int_set_si(vec->block.data[0], 1);
172 for (i = 0, l = sol->list; l && i < dim; ++i, l = l->next) {
173 isl_seq_cpy_from_pip(&vec->block.data[1+i],
174 &l->vector->the_vector[0], 1);
175 isl_assert(ctx, !entier_zero_p(l->vector->the_deno[0]),
178 isl_assert(ctx, i == dim, goto error);
179 vec = isl_mat_vec_product(ctx, T, vec);
183 pip_options_free(options);
184 pip_matrix_free(domain);
186 isl_basic_set_free(bset);
189 isl_vec_free(ctx, vec);
190 isl_basic_set_free(bset);
194 pip_matrix_free(domain);