clean up isl_basic_set_variable_compression_with_id
[isl.git] / isl_bernstein.c
blob5759ddc8a82c5c53d36863a8002736be18f78d54
1 /*
2 * Copyright 2006-2007 Universiteit Leiden
3 * Copyright 2008-2009 Katholieke Universiteit Leuven
4 * Copyright 2010 INRIA Saclay
6 * Use of this software is governed by the MIT license
8 * Written by Sven Verdoolaege, Leiden Institute of Advanced Computer Science,
9 * Universiteit Leiden, Niels Bohrweg 1, 2333 CA Leiden, The Netherlands
10 * and K.U.Leuven, Departement Computerwetenschappen, Celestijnenlaan 200A,
11 * B-3001 Leuven, Belgium
12 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
13 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
16 #include <isl_ctx_private.h>
17 #include <isl_map_private.h>
18 #include <isl/set.h>
19 #include <isl_seq.h>
20 #include <isl_morph.h>
21 #include <isl_factorization.h>
22 #include <isl_vertices_private.h>
23 #include <isl_polynomial_private.h>
24 #include <isl_options_private.h>
25 #include <isl_vec_private.h>
26 #include <isl_bernstein.h>
28 struct bernstein_data {
29 enum isl_fold type;
30 isl_qpolynomial *poly;
31 int check_tight;
33 isl_cell *cell;
35 isl_qpolynomial_fold *fold;
36 isl_qpolynomial_fold *fold_tight;
37 isl_pw_qpolynomial_fold *pwf;
38 isl_pw_qpolynomial_fold *pwf_tight;
41 static isl_bool vertex_is_integral(__isl_keep isl_basic_set *vertex)
43 isl_size nvar;
44 isl_size nparam;
45 int i;
47 nvar = isl_basic_set_dim(vertex, isl_dim_set);
48 nparam = isl_basic_set_dim(vertex, isl_dim_param);
49 if (nvar < 0 || nparam < 0)
50 return isl_bool_error;
51 for (i = 0; i < nvar; ++i) {
52 int r = nvar - 1 - i;
53 if (!isl_int_is_one(vertex->eq[r][1 + nparam + i]) &&
54 !isl_int_is_negone(vertex->eq[r][1 + nparam + i]))
55 return isl_bool_false;
58 return isl_bool_true;
61 static __isl_give isl_qpolynomial *vertex_coordinate(
62 __isl_keep isl_basic_set *vertex, int i, __isl_take isl_space *space)
64 isl_size nvar;
65 isl_size nparam;
66 isl_size total;
67 int r;
68 isl_int denom;
69 isl_qpolynomial *v;
71 isl_int_init(denom);
73 nvar = isl_basic_set_dim(vertex, isl_dim_set);
74 nparam = isl_basic_set_dim(vertex, isl_dim_param);
75 total = isl_basic_set_dim(vertex, isl_dim_all);
76 if (nvar < 0 || nparam < 0 || total < 0)
77 goto error;
78 r = nvar - 1 - i;
80 isl_int_set(denom, vertex->eq[r][1 + nparam + i]);
81 isl_assert(vertex->ctx, !isl_int_is_zero(denom), goto error);
83 if (isl_int_is_pos(denom))
84 isl_seq_neg(vertex->eq[r], vertex->eq[r], 1 + total);
85 else
86 isl_int_neg(denom, denom);
88 v = isl_qpolynomial_from_affine(space, vertex->eq[r], denom);
89 isl_int_clear(denom);
91 return v;
92 error:
93 isl_space_free(space);
94 isl_int_clear(denom);
95 return NULL;
98 /* Check whether the bound associated to the selection "k" is tight,
99 * which is the case if we select exactly one vertex (i.e., one of the
100 * exponents in "k" is exactly "d") and if that vertex
101 * is integral for all values of the parameters.
103 static isl_bool is_tight(int *k, int n, int d, isl_cell *cell)
105 int i;
107 for (i = 0; i < n; ++i) {
108 int v;
109 if (!k[i])
110 continue;
111 if (k[i] != d)
112 return isl_bool_false;
113 v = cell->ids[n - 1 - i];
114 return vertex_is_integral(cell->vertices->v[v].vertex);
117 return isl_bool_false;
120 static isl_stat add_fold(__isl_take isl_qpolynomial *b, __isl_keep isl_set *dom,
121 int *k, int n, int d, struct bernstein_data *data)
123 isl_qpolynomial_fold *fold;
124 isl_bool tight;
126 fold = isl_qpolynomial_fold_alloc(data->type, b);
128 tight = isl_bool_false;
129 if (data->check_tight)
130 tight = is_tight(k, n, d, data->cell);
131 if (tight < 0)
132 return isl_stat_error;
133 if (tight)
134 data->fold_tight = isl_qpolynomial_fold_fold_on_domain(dom,
135 data->fold_tight, fold);
136 else
137 data->fold = isl_qpolynomial_fold_fold_on_domain(dom,
138 data->fold, fold);
139 return isl_stat_ok;
142 /* Extract the coefficients of the Bernstein base polynomials and store
143 * them in data->fold and data->fold_tight.
145 * In particular, the coefficient of each monomial
146 * of multi-degree (k[0], k[1], ..., k[n-1]) is divided by the corresponding
147 * multinomial coefficient d!/k[0]! k[1]! ... k[n-1]!
149 * c[i] contains the coefficient of the selected powers of the first i+1 vars.
150 * multinom[i] contains the partial multinomial coefficient.
152 static isl_stat extract_coefficients(isl_qpolynomial *poly,
153 __isl_keep isl_set *dom, struct bernstein_data *data)
155 int i;
156 int d;
157 isl_size n;
158 isl_ctx *ctx;
159 isl_qpolynomial **c = NULL;
160 int *k = NULL;
161 int *left = NULL;
162 isl_vec *multinom = NULL;
164 n = isl_qpolynomial_dim(poly, isl_dim_in);
165 if (n < 0)
166 return isl_stat_error;
168 ctx = isl_qpolynomial_get_ctx(poly);
169 d = isl_qpolynomial_degree(poly);
170 isl_assert(ctx, n >= 2, return isl_stat_error);
172 c = isl_calloc_array(ctx, isl_qpolynomial *, n);
173 k = isl_alloc_array(ctx, int, n);
174 left = isl_alloc_array(ctx, int, n);
175 multinom = isl_vec_alloc(ctx, n);
176 if (!c || !k || !left || !multinom)
177 goto error;
179 isl_int_set_si(multinom->el[0], 1);
180 for (k[0] = d; k[0] >= 0; --k[0]) {
181 int i = 1;
182 isl_qpolynomial_free(c[0]);
183 c[0] = isl_qpolynomial_coeff(poly, isl_dim_in, n - 1, k[0]);
184 left[0] = d - k[0];
185 k[1] = -1;
186 isl_int_set(multinom->el[1], multinom->el[0]);
187 while (i > 0) {
188 if (i == n - 1) {
189 int j;
190 isl_space *dim;
191 isl_qpolynomial *b;
192 isl_qpolynomial *f;
193 for (j = 2; j <= left[i - 1]; ++j)
194 isl_int_divexact_ui(multinom->el[i],
195 multinom->el[i], j);
196 b = isl_qpolynomial_coeff(c[i - 1], isl_dim_in,
197 n - 1 - i, left[i - 1]);
198 b = isl_qpolynomial_project_domain_on_params(b);
199 dim = isl_qpolynomial_get_domain_space(b);
200 f = isl_qpolynomial_rat_cst_on_domain(dim, ctx->one,
201 multinom->el[i]);
202 b = isl_qpolynomial_mul(b, f);
203 k[n - 1] = left[n - 2];
204 if (add_fold(b, dom, k, n, d, data) < 0)
205 goto error;
206 --i;
207 continue;
209 if (k[i] >= left[i - 1]) {
210 --i;
211 continue;
213 ++k[i];
214 if (k[i])
215 isl_int_divexact_ui(multinom->el[i],
216 multinom->el[i], k[i]);
217 isl_qpolynomial_free(c[i]);
218 c[i] = isl_qpolynomial_coeff(c[i - 1], isl_dim_in,
219 n - 1 - i, k[i]);
220 left[i] = left[i - 1] - k[i];
221 k[i + 1] = -1;
222 isl_int_set(multinom->el[i + 1], multinom->el[i]);
223 ++i;
225 isl_int_mul_ui(multinom->el[0], multinom->el[0], k[0]);
228 for (i = 0; i < n; ++i)
229 isl_qpolynomial_free(c[i]);
231 isl_vec_free(multinom);
232 free(left);
233 free(k);
234 free(c);
235 return isl_stat_ok;
236 error:
237 isl_vec_free(multinom);
238 free(left);
239 free(k);
240 if (c)
241 for (i = 0; i < n; ++i)
242 isl_qpolynomial_free(c[i]);
243 free(c);
244 return isl_stat_error;
247 /* Perform bernstein expansion on the parametric vertices that are active
248 * on "cell".
250 * data->poly has been homogenized in the calling function.
252 * We plug in the barycentric coordinates for the set variables
254 * \vec x = \sum_i \alpha_i v_i(\vec p)
256 * and the constant "1 = \sum_i \alpha_i" for the homogeneous dimension.
257 * Next, we extract the coefficients of the Bernstein base polynomials.
259 static isl_stat bernstein_coefficients_cell(__isl_take isl_cell *cell,
260 void *user)
262 int i, j;
263 struct bernstein_data *data = (struct bernstein_data *)user;
264 isl_space *space_param;
265 isl_space *space_dst;
266 isl_qpolynomial *poly = data->poly;
267 isl_size n_in;
268 unsigned nvar;
269 int n_vertices;
270 isl_qpolynomial **subs;
271 isl_pw_qpolynomial_fold *pwf;
272 isl_set *dom;
273 isl_ctx *ctx;
275 n_in = isl_qpolynomial_dim(poly, isl_dim_in);
276 if (n_in < 0)
277 goto error;
279 nvar = n_in - 1;
280 n_vertices = cell->n_vertices;
282 ctx = isl_qpolynomial_get_ctx(poly);
283 if (n_vertices > nvar + 1 && ctx->opt->bernstein_triangulate)
284 return isl_cell_foreach_simplex(cell,
285 &bernstein_coefficients_cell, user);
287 subs = isl_alloc_array(ctx, isl_qpolynomial *, 1 + nvar);
288 if (!subs)
289 goto error;
291 space_param = isl_basic_set_get_space(cell->dom);
292 space_dst = isl_qpolynomial_get_domain_space(poly);
293 space_dst = isl_space_add_dims(space_dst, isl_dim_set, n_vertices);
295 for (i = 0; i < 1 + nvar; ++i)
296 subs[i] =
297 isl_qpolynomial_zero_on_domain(isl_space_copy(space_dst));
299 for (i = 0; i < n_vertices; ++i) {
300 isl_qpolynomial *c;
301 c = isl_qpolynomial_var_on_domain(isl_space_copy(space_dst),
302 isl_dim_set, 1 + nvar + i);
303 for (j = 0; j < nvar; ++j) {
304 int k = cell->ids[i];
305 isl_qpolynomial *v;
306 v = vertex_coordinate(cell->vertices->v[k].vertex, j,
307 isl_space_copy(space_param));
308 v = isl_qpolynomial_add_dims(v, isl_dim_in,
309 1 + nvar + n_vertices);
310 v = isl_qpolynomial_mul(v, isl_qpolynomial_copy(c));
311 subs[1 + j] = isl_qpolynomial_add(subs[1 + j], v);
313 subs[0] = isl_qpolynomial_add(subs[0], c);
315 isl_space_free(space_dst);
317 poly = isl_qpolynomial_copy(poly);
319 poly = isl_qpolynomial_add_dims(poly, isl_dim_in, n_vertices);
320 poly = isl_qpolynomial_substitute(poly, isl_dim_in, 0, 1 + nvar, subs);
321 poly = isl_qpolynomial_drop_dims(poly, isl_dim_in, 0, 1 + nvar);
323 data->cell = cell;
324 dom = isl_set_from_basic_set(isl_basic_set_copy(cell->dom));
325 data->fold = isl_qpolynomial_fold_empty(data->type,
326 isl_space_copy(space_param));
327 data->fold_tight = isl_qpolynomial_fold_empty(data->type, space_param);
328 if (extract_coefficients(poly, dom, data) < 0) {
329 data->fold = isl_qpolynomial_fold_free(data->fold);
330 data->fold_tight = isl_qpolynomial_fold_free(data->fold_tight);
333 pwf = isl_pw_qpolynomial_fold_alloc(data->type, isl_set_copy(dom),
334 data->fold);
335 data->pwf = isl_pw_qpolynomial_fold_fold(data->pwf, pwf);
336 pwf = isl_pw_qpolynomial_fold_alloc(data->type, dom, data->fold_tight);
337 data->pwf_tight = isl_pw_qpolynomial_fold_fold(data->pwf_tight, pwf);
339 isl_qpolynomial_free(poly);
340 isl_cell_free(cell);
341 for (i = 0; i < 1 + nvar; ++i)
342 isl_qpolynomial_free(subs[i]);
343 free(subs);
344 return isl_stat_ok;
345 error:
346 isl_cell_free(cell);
347 return isl_stat_error;
350 /* Base case of applying bernstein expansion.
352 * We compute the chamber decomposition of the parametric polytope "bset"
353 * and then perform bernstein expansion on the parametric vertices
354 * that are active on each chamber.
356 static __isl_give isl_pw_qpolynomial_fold *bernstein_coefficients_base(
357 __isl_take isl_basic_set *bset,
358 __isl_take isl_qpolynomial *poly, struct bernstein_data *data, int *tight)
360 isl_size nvar;
361 isl_space *space;
362 isl_pw_qpolynomial_fold *pwf;
363 isl_vertices *vertices;
364 isl_bool covers;
366 nvar = isl_basic_set_dim(bset, isl_dim_set);
367 if (nvar < 0)
368 bset = isl_basic_set_free(bset);
369 if (nvar == 0) {
370 isl_set *dom;
371 isl_qpolynomial_fold *fold;
373 fold = isl_qpolynomial_fold_alloc(data->type, poly);
374 dom = isl_set_from_basic_set(bset);
375 if (tight)
376 *tight = 1;
377 pwf = isl_pw_qpolynomial_fold_alloc(data->type, dom, fold);
378 return isl_pw_qpolynomial_fold_project_domain_on_params(pwf);
381 if (isl_qpolynomial_is_zero(poly)) {
382 isl_set *dom;
383 isl_qpolynomial_fold *fold;
384 fold = isl_qpolynomial_fold_alloc(data->type, poly);
385 dom = isl_set_from_basic_set(bset);
386 pwf = isl_pw_qpolynomial_fold_alloc(data->type, dom, fold);
387 if (tight)
388 *tight = 1;
389 return isl_pw_qpolynomial_fold_project_domain_on_params(pwf);
392 space = isl_basic_set_get_space(bset);
393 space = isl_space_params(space);
394 space = isl_space_from_domain(space);
395 space = isl_space_add_dims(space, isl_dim_set, 1);
396 data->pwf = isl_pw_qpolynomial_fold_zero(isl_space_copy(space),
397 data->type);
398 data->pwf_tight = isl_pw_qpolynomial_fold_zero(space, data->type);
399 data->poly = isl_qpolynomial_homogenize(isl_qpolynomial_copy(poly));
400 vertices = isl_basic_set_compute_vertices(bset);
401 if (isl_vertices_foreach_disjoint_cell(vertices,
402 &bernstein_coefficients_cell, data) < 0)
403 data->pwf = isl_pw_qpolynomial_fold_free(data->pwf);
404 isl_vertices_free(vertices);
405 isl_qpolynomial_free(data->poly);
407 isl_basic_set_free(bset);
408 isl_qpolynomial_free(poly);
410 covers = isl_pw_qpolynomial_fold_covers(data->pwf_tight, data->pwf);
411 if (covers < 0)
412 goto error;
414 if (tight)
415 *tight = covers;
417 if (covers) {
418 isl_pw_qpolynomial_fold_free(data->pwf);
419 return data->pwf_tight;
422 data->pwf = isl_pw_qpolynomial_fold_fold(data->pwf, data->pwf_tight);
424 return data->pwf;
425 error:
426 isl_pw_qpolynomial_fold_free(data->pwf_tight);
427 isl_pw_qpolynomial_fold_free(data->pwf);
428 return NULL;
431 /* Apply bernstein expansion recursively by working in on len[i]
432 * set variables at a time, with i ranging from n_group - 1 to 0.
434 static __isl_give isl_pw_qpolynomial_fold *bernstein_coefficients_recursive(
435 __isl_take isl_pw_qpolynomial *pwqp,
436 int n_group, int *len, struct bernstein_data *data, int *tight)
438 int i;
439 isl_size nparam;
440 isl_size nvar;
441 isl_pw_qpolynomial_fold *pwf;
443 nparam = isl_pw_qpolynomial_dim(pwqp, isl_dim_param);
444 nvar = isl_pw_qpolynomial_dim(pwqp, isl_dim_in);
445 if (nparam < 0 || nvar < 0)
446 goto error;
448 pwqp = isl_pw_qpolynomial_move_dims(pwqp, isl_dim_param, nparam,
449 isl_dim_in, 0, nvar - len[n_group - 1]);
450 pwf = isl_pw_qpolynomial_bound(pwqp, data->type, tight);
452 for (i = n_group - 2; i >= 0; --i) {
453 nparam = isl_pw_qpolynomial_fold_dim(pwf, isl_dim_param);
454 if (nparam < 0)
455 return isl_pw_qpolynomial_fold_free(pwf);
456 pwf = isl_pw_qpolynomial_fold_move_dims(pwf, isl_dim_in, 0,
457 isl_dim_param, nparam - len[i], len[i]);
458 if (tight && !*tight)
459 tight = NULL;
460 pwf = isl_pw_qpolynomial_fold_bound(pwf, tight);
463 return pwf;
464 error:
465 isl_pw_qpolynomial_free(pwqp);
466 return NULL;
469 static __isl_give isl_pw_qpolynomial_fold *bernstein_coefficients_factors(
470 __isl_take isl_basic_set *bset,
471 __isl_take isl_qpolynomial *poly, struct bernstein_data *data, int *tight)
473 isl_factorizer *f;
474 isl_set *set;
475 isl_pw_qpolynomial *pwqp;
476 isl_pw_qpolynomial_fold *pwf;
478 f = isl_basic_set_factorizer(bset);
479 if (!f)
480 goto error;
481 if (f->n_group == 0) {
482 isl_factorizer_free(f);
483 return bernstein_coefficients_base(bset, poly, data, tight);
486 set = isl_set_from_basic_set(bset);
487 pwqp = isl_pw_qpolynomial_alloc(set, poly);
488 pwqp = isl_pw_qpolynomial_morph_domain(pwqp, isl_morph_copy(f->morph));
490 pwf = bernstein_coefficients_recursive(pwqp, f->n_group, f->len, data,
491 tight);
493 isl_factorizer_free(f);
495 return pwf;
496 error:
497 isl_basic_set_free(bset);
498 isl_qpolynomial_free(poly);
499 return NULL;
502 static __isl_give isl_pw_qpolynomial_fold *bernstein_coefficients_full_recursive(
503 __isl_take isl_basic_set *bset,
504 __isl_take isl_qpolynomial *poly, struct bernstein_data *data, int *tight)
506 int i;
507 int *len;
508 isl_size nvar;
509 isl_pw_qpolynomial_fold *pwf;
510 isl_set *set;
511 isl_pw_qpolynomial *pwqp;
513 nvar = isl_basic_set_dim(bset, isl_dim_set);
514 if (nvar < 0 || !poly)
515 goto error;
517 len = isl_alloc_array(bset->ctx, int, nvar);
518 if (nvar && !len)
519 goto error;
521 for (i = 0; i < nvar; ++i)
522 len[i] = 1;
524 set = isl_set_from_basic_set(bset);
525 pwqp = isl_pw_qpolynomial_alloc(set, poly);
527 pwf = bernstein_coefficients_recursive(pwqp, nvar, len, data, tight);
529 free(len);
531 return pwf;
532 error:
533 isl_basic_set_free(bset);
534 isl_qpolynomial_free(poly);
535 return NULL;
538 /* Compute a bound on the polynomial defined over the parametric polytope
539 * using bernstein expansion and store the result
540 * in bound->pwf and bound->pwf_tight.
542 * If bernstein_recurse is set to ISL_BERNSTEIN_FACTORS, we check if
543 * the polytope can be factorized and apply bernstein expansion recursively
544 * on the factors.
545 * If bernstein_recurse is set to ISL_BERNSTEIN_INTERVALS, we apply
546 * bernstein expansion recursively on each dimension.
547 * Otherwise, we apply bernstein expansion on the entire polytope.
549 isl_stat isl_qpolynomial_bound_on_domain_bernstein(
550 __isl_take isl_basic_set *bset, __isl_take isl_qpolynomial *poly,
551 struct isl_bound *bound)
553 struct bernstein_data data;
554 isl_pw_qpolynomial_fold *pwf;
555 isl_size nvar;
556 int tight = 0;
557 int *tp = bound->check_tight ? &tight : NULL;
559 nvar = isl_basic_set_dim(bset, isl_dim_set);
560 if (nvar < 0 || !poly)
561 goto error;
563 data.type = bound->type;
564 data.check_tight = bound->check_tight;
566 if (bset->ctx->opt->bernstein_recurse & ISL_BERNSTEIN_FACTORS)
567 pwf = bernstein_coefficients_factors(bset, poly, &data, tp);
568 else if (nvar > 1 &&
569 (bset->ctx->opt->bernstein_recurse & ISL_BERNSTEIN_INTERVALS))
570 pwf = bernstein_coefficients_full_recursive(bset, poly, &data, tp);
571 else
572 pwf = bernstein_coefficients_base(bset, poly, &data, tp);
574 if (tight)
575 bound->pwf_tight = isl_pw_qpolynomial_fold_fold(bound->pwf_tight, pwf);
576 else
577 bound->pwf = isl_pw_qpolynomial_fold_fold(bound->pwf, pwf);
579 return isl_stat_ok;
580 error:
581 isl_basic_set_free(bset);
582 isl_qpolynomial_free(poly);
583 return isl_stat_error;