add isl_union_map_params
[isl.git] / isl_bernstein.c
blob43045fac31eeea0a2f551079b4a7e8f30299e578
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 GNU LGPLv2.1 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_bernstein.h>
26 struct bernstein_data {
27 enum isl_fold type;
28 isl_qpolynomial *poly;
29 int check_tight;
31 isl_cell *cell;
33 isl_qpolynomial_fold *fold;
34 isl_qpolynomial_fold *fold_tight;
35 isl_pw_qpolynomial_fold *pwf;
36 isl_pw_qpolynomial_fold *pwf_tight;
39 static int vertex_is_integral(__isl_keep isl_basic_set *vertex)
41 unsigned nvar;
42 unsigned nparam;
43 int i;
45 nvar = isl_basic_set_dim(vertex, isl_dim_set);
46 nparam = isl_basic_set_dim(vertex, isl_dim_param);
47 for (i = 0; i < nvar; ++i) {
48 int r = nvar - 1 - i;
49 if (!isl_int_is_one(vertex->eq[r][1 + nparam + i]) &&
50 !isl_int_is_negone(vertex->eq[r][1 + nparam + i]))
51 return 0;
54 return 1;
57 static __isl_give isl_qpolynomial *vertex_coordinate(
58 __isl_keep isl_basic_set *vertex, int i, __isl_take isl_space *dim)
60 unsigned nvar;
61 unsigned nparam;
62 int r;
63 isl_int denom;
64 isl_qpolynomial *v;
66 nvar = isl_basic_set_dim(vertex, isl_dim_set);
67 nparam = isl_basic_set_dim(vertex, isl_dim_param);
68 r = nvar - 1 - i;
70 isl_int_init(denom);
71 isl_int_set(denom, vertex->eq[r][1 + nparam + i]);
72 isl_assert(vertex->ctx, !isl_int_is_zero(denom), goto error);
74 if (isl_int_is_pos(denom))
75 isl_seq_neg(vertex->eq[r], vertex->eq[r],
76 1 + isl_basic_set_total_dim(vertex));
77 else
78 isl_int_neg(denom, denom);
80 v = isl_qpolynomial_from_affine(dim, vertex->eq[r], denom);
81 isl_int_clear(denom);
83 return v;
84 error:
85 isl_space_free(dim);
86 isl_int_clear(denom);
87 return NULL;
90 /* Check whether the bound associated to the selection "k" is tight,
91 * which is the case if we select exactly one vertex and if that vertex
92 * is integral for all values of the parameters.
94 static int is_tight(int *k, int n, int d, isl_cell *cell)
96 int i;
98 for (i = 0; i < n; ++i) {
99 int v;
100 if (k[i] != d) {
101 if (k[i])
102 return 0;
103 continue;
105 v = cell->ids[n - 1 - i];
106 return vertex_is_integral(cell->vertices->v[v].vertex);
109 return 0;
112 static void add_fold(__isl_take isl_qpolynomial *b, __isl_keep isl_set *dom,
113 int *k, int n, int d, struct bernstein_data *data)
115 isl_qpolynomial_fold *fold;
117 fold = isl_qpolynomial_fold_alloc(data->type, b);
119 if (data->check_tight && is_tight(k, n, d, data->cell))
120 data->fold_tight = isl_qpolynomial_fold_fold_on_domain(dom,
121 data->fold_tight, fold);
122 else
123 data->fold = isl_qpolynomial_fold_fold_on_domain(dom,
124 data->fold, fold);
127 /* Extract the coefficients of the Bernstein base polynomials and store
128 * them in data->fold and data->fold_tight.
130 * In particular, the coefficient of each monomial
131 * of multi-degree (k[0], k[1], ..., k[n-1]) is divided by the corresponding
132 * multinomial coefficient d!/k[0]! k[1]! ... k[n-1]!
134 * c[i] contains the coefficient of the selected powers of the first i+1 vars.
135 * multinom[i] contains the partial multinomial coefficient.
137 static void extract_coefficients(isl_qpolynomial *poly,
138 __isl_keep isl_set *dom, struct bernstein_data *data)
140 int i;
141 int d;
142 int n;
143 isl_ctx *ctx;
144 isl_qpolynomial **c = NULL;
145 int *k = NULL;
146 int *left = NULL;
147 isl_vec *multinom = NULL;
149 if (!poly)
150 return;
152 ctx = isl_qpolynomial_get_ctx(poly);
153 n = isl_qpolynomial_dim(poly, isl_dim_in);
154 d = isl_qpolynomial_degree(poly);
155 isl_assert(ctx, n >= 2, return);
157 c = isl_calloc_array(ctx, isl_qpolynomial *, n);
158 k = isl_alloc_array(ctx, int, n);
159 left = isl_alloc_array(ctx, int, n);
160 multinom = isl_vec_alloc(ctx, n);
161 if (!c || !k || !left || !multinom)
162 goto error;
164 isl_int_set_si(multinom->el[0], 1);
165 for (k[0] = d; k[0] >= 0; --k[0]) {
166 int i = 1;
167 isl_qpolynomial_free(c[0]);
168 c[0] = isl_qpolynomial_coeff(poly, isl_dim_in, n - 1, k[0]);
169 left[0] = d - k[0];
170 k[1] = -1;
171 isl_int_set(multinom->el[1], multinom->el[0]);
172 while (i > 0) {
173 if (i == n - 1) {
174 int j;
175 isl_space *dim;
176 isl_qpolynomial *b;
177 isl_qpolynomial *f;
178 for (j = 2; j <= left[i - 1]; ++j)
179 isl_int_divexact_ui(multinom->el[i],
180 multinom->el[i], j);
181 b = isl_qpolynomial_coeff(c[i - 1], isl_dim_in,
182 n - 1 - i, left[i - 1]);
183 b = isl_qpolynomial_project_domain_on_params(b);
184 dim = isl_qpolynomial_get_domain_space(b);
185 f = isl_qpolynomial_rat_cst_on_domain(dim, ctx->one,
186 multinom->el[i]);
187 b = isl_qpolynomial_mul(b, f);
188 k[n - 1] = left[n - 2];
189 add_fold(b, dom, k, n, d, data);
190 --i;
191 continue;
193 if (k[i] >= left[i - 1]) {
194 --i;
195 continue;
197 ++k[i];
198 if (k[i])
199 isl_int_divexact_ui(multinom->el[i],
200 multinom->el[i], k[i]);
201 isl_qpolynomial_free(c[i]);
202 c[i] = isl_qpolynomial_coeff(c[i - 1], isl_dim_in,
203 n - 1 - i, k[i]);
204 left[i] = left[i - 1] - k[i];
205 k[i + 1] = -1;
206 isl_int_set(multinom->el[i + 1], multinom->el[i]);
207 ++i;
209 isl_int_mul_ui(multinom->el[0], multinom->el[0], k[0]);
212 for (i = 0; i < n; ++i)
213 isl_qpolynomial_free(c[i]);
215 isl_vec_free(multinom);
216 free(left);
217 free(k);
218 free(c);
219 return;
220 error:
221 isl_vec_free(multinom);
222 free(left);
223 free(k);
224 if (c)
225 for (i = 0; i < n; ++i)
226 isl_qpolynomial_free(c[i]);
227 free(c);
228 return;
231 /* Perform bernstein expansion on the parametric vertices that are active
232 * on "cell".
234 * data->poly has been homogenized in the calling function.
236 * We plug in the barycentric coordinates for the set variables
238 * \vec x = \sum_i \alpha_i v_i(\vec p)
240 * and the constant "1 = \sum_i \alpha_i" for the homogeneous dimension.
241 * Next, we extract the coefficients of the Bernstein base polynomials.
243 static int bernstein_coefficients_cell(__isl_take isl_cell *cell, void *user)
245 int i, j;
246 struct bernstein_data *data = (struct bernstein_data *)user;
247 isl_space *dim_param;
248 isl_space *dim_dst;
249 isl_qpolynomial *poly = data->poly;
250 unsigned nvar;
251 int n_vertices;
252 isl_qpolynomial **subs;
253 isl_pw_qpolynomial_fold *pwf;
254 isl_set *dom;
255 isl_ctx *ctx;
257 if (!poly)
258 goto error;
260 nvar = isl_qpolynomial_dim(poly, isl_dim_in) - 1;
261 n_vertices = cell->n_vertices;
263 ctx = isl_qpolynomial_get_ctx(poly);
264 if (n_vertices > nvar + 1 && ctx->opt->bernstein_triangulate)
265 return isl_cell_foreach_simplex(cell,
266 &bernstein_coefficients_cell, user);
268 subs = isl_alloc_array(ctx, isl_qpolynomial *, 1 + nvar);
269 if (!subs)
270 goto error;
272 dim_param = isl_basic_set_get_space(cell->dom);
273 dim_dst = isl_qpolynomial_get_domain_space(poly);
274 dim_dst = isl_space_add_dims(dim_dst, isl_dim_set, n_vertices);
276 for (i = 0; i < 1 + nvar; ++i)
277 subs[i] = isl_qpolynomial_zero_on_domain(isl_space_copy(dim_dst));
279 for (i = 0; i < n_vertices; ++i) {
280 isl_qpolynomial *c;
281 c = isl_qpolynomial_var_on_domain(isl_space_copy(dim_dst), isl_dim_set,
282 1 + nvar + i);
283 for (j = 0; j < nvar; ++j) {
284 int k = cell->ids[i];
285 isl_qpolynomial *v;
286 v = vertex_coordinate(cell->vertices->v[k].vertex, j,
287 isl_space_copy(dim_param));
288 v = isl_qpolynomial_add_dims(v, isl_dim_in,
289 1 + nvar + n_vertices);
290 v = isl_qpolynomial_mul(v, isl_qpolynomial_copy(c));
291 subs[1 + j] = isl_qpolynomial_add(subs[1 + j], v);
293 subs[0] = isl_qpolynomial_add(subs[0], c);
295 isl_space_free(dim_dst);
297 poly = isl_qpolynomial_copy(poly);
299 poly = isl_qpolynomial_add_dims(poly, isl_dim_in, n_vertices);
300 poly = isl_qpolynomial_substitute(poly, isl_dim_in, 0, 1 + nvar, subs);
301 poly = isl_qpolynomial_drop_dims(poly, isl_dim_in, 0, 1 + nvar);
303 data->cell = cell;
304 dom = isl_set_from_basic_set(isl_basic_set_copy(cell->dom));
305 data->fold = isl_qpolynomial_fold_empty(data->type, isl_space_copy(dim_param));
306 data->fold_tight = isl_qpolynomial_fold_empty(data->type, dim_param);
307 extract_coefficients(poly, dom, data);
309 pwf = isl_pw_qpolynomial_fold_alloc(data->type, isl_set_copy(dom),
310 data->fold);
311 data->pwf = isl_pw_qpolynomial_fold_fold(data->pwf, pwf);
312 pwf = isl_pw_qpolynomial_fold_alloc(data->type, dom, data->fold_tight);
313 data->pwf_tight = isl_pw_qpolynomial_fold_fold(data->pwf_tight, pwf);
315 isl_qpolynomial_free(poly);
316 isl_cell_free(cell);
317 for (i = 0; i < 1 + nvar; ++i)
318 isl_qpolynomial_free(subs[i]);
319 free(subs);
320 return 0;
321 error:
322 isl_cell_free(cell);
323 return -1;
326 /* Base case of applying bernstein expansion.
328 * We compute the chamber decomposition of the parametric polytope "bset"
329 * and then perform bernstein expansion on the parametric vertices
330 * that are active on each chamber.
332 static __isl_give isl_pw_qpolynomial_fold *bernstein_coefficients_base(
333 __isl_take isl_basic_set *bset,
334 __isl_take isl_qpolynomial *poly, struct bernstein_data *data, int *tight)
336 unsigned nvar;
337 isl_space *dim;
338 isl_pw_qpolynomial_fold *pwf;
339 isl_vertices *vertices;
340 int covers;
342 nvar = isl_basic_set_dim(bset, isl_dim_set);
343 if (nvar == 0) {
344 isl_set *dom;
345 isl_qpolynomial_fold *fold;
347 fold = isl_qpolynomial_fold_alloc(data->type, poly);
348 dom = isl_set_from_basic_set(bset);
349 if (tight)
350 *tight = 1;
351 pwf = isl_pw_qpolynomial_fold_alloc(data->type, dom, fold);
352 return isl_pw_qpolynomial_fold_project_domain_on_params(pwf);
355 if (isl_qpolynomial_is_zero(poly)) {
356 isl_set *dom;
357 isl_qpolynomial_fold *fold;
358 fold = isl_qpolynomial_fold_alloc(data->type, poly);
359 dom = isl_set_from_basic_set(bset);
360 pwf = isl_pw_qpolynomial_fold_alloc(data->type, dom, fold);
361 if (tight)
362 *tight = 1;
363 return isl_pw_qpolynomial_fold_project_domain_on_params(pwf);
366 dim = isl_basic_set_get_space(bset);
367 dim = isl_space_params(dim);
368 dim = isl_space_from_domain(dim);
369 dim = isl_space_add_dims(dim, isl_dim_set, 1);
370 data->pwf = isl_pw_qpolynomial_fold_zero(isl_space_copy(dim), data->type);
371 data->pwf_tight = isl_pw_qpolynomial_fold_zero(dim, data->type);
372 data->poly = isl_qpolynomial_homogenize(isl_qpolynomial_copy(poly));
373 vertices = isl_basic_set_compute_vertices(bset);
374 isl_vertices_foreach_disjoint_cell(vertices,
375 &bernstein_coefficients_cell, data);
376 isl_vertices_free(vertices);
377 isl_qpolynomial_free(data->poly);
379 isl_basic_set_free(bset);
380 isl_qpolynomial_free(poly);
382 covers = isl_pw_qpolynomial_fold_covers(data->pwf_tight, data->pwf);
383 if (covers < 0)
384 goto error;
386 if (tight)
387 *tight = covers;
389 if (covers) {
390 isl_pw_qpolynomial_fold_free(data->pwf);
391 return data->pwf_tight;
394 data->pwf = isl_pw_qpolynomial_fold_fold(data->pwf, data->pwf_tight);
396 return data->pwf;
397 error:
398 isl_pw_qpolynomial_fold_free(data->pwf_tight);
399 isl_pw_qpolynomial_fold_free(data->pwf);
400 return NULL;
403 /* Apply bernstein expansion recursively by working in on len[i]
404 * set variables at a time, with i ranging from n_group - 1 to 0.
406 static __isl_give isl_pw_qpolynomial_fold *bernstein_coefficients_recursive(
407 __isl_take isl_pw_qpolynomial *pwqp,
408 int n_group, int *len, struct bernstein_data *data, int *tight)
410 int i;
411 unsigned nparam;
412 unsigned nvar;
413 isl_pw_qpolynomial_fold *pwf;
415 if (!pwqp)
416 return NULL;
418 nparam = isl_pw_qpolynomial_dim(pwqp, isl_dim_param);
419 nvar = isl_pw_qpolynomial_dim(pwqp, isl_dim_in);
421 pwqp = isl_pw_qpolynomial_move_dims(pwqp, isl_dim_param, nparam,
422 isl_dim_in, 0, nvar - len[n_group - 1]);
423 pwf = isl_pw_qpolynomial_bound(pwqp, data->type, tight);
425 for (i = n_group - 2; i >= 0; --i) {
426 nparam = isl_pw_qpolynomial_fold_dim(pwf, isl_dim_param);
427 pwf = isl_pw_qpolynomial_fold_move_dims(pwf, isl_dim_in, 0,
428 isl_dim_param, nparam - len[i], len[i]);
429 if (tight && !*tight)
430 tight = NULL;
431 pwf = isl_pw_qpolynomial_fold_bound(pwf, tight);
434 return pwf;
437 static __isl_give isl_pw_qpolynomial_fold *bernstein_coefficients_factors(
438 __isl_take isl_basic_set *bset,
439 __isl_take isl_qpolynomial *poly, struct bernstein_data *data, int *tight)
441 isl_factorizer *f;
442 isl_set *set;
443 isl_pw_qpolynomial *pwqp;
444 isl_pw_qpolynomial_fold *pwf;
446 f = isl_basic_set_factorizer(bset);
447 if (!f)
448 goto error;
449 if (f->n_group == 0) {
450 isl_factorizer_free(f);
451 return bernstein_coefficients_base(bset, poly, data, tight);
454 set = isl_set_from_basic_set(bset);
455 pwqp = isl_pw_qpolynomial_alloc(set, poly);
456 pwqp = isl_pw_qpolynomial_morph_domain(pwqp, isl_morph_copy(f->morph));
458 pwf = bernstein_coefficients_recursive(pwqp, f->n_group, f->len, data,
459 tight);
461 isl_factorizer_free(f);
463 return pwf;
464 error:
465 isl_basic_set_free(bset);
466 isl_qpolynomial_free(poly);
467 return NULL;
470 static __isl_give isl_pw_qpolynomial_fold *bernstein_coefficients_full_recursive(
471 __isl_take isl_basic_set *bset,
472 __isl_take isl_qpolynomial *poly, struct bernstein_data *data, int *tight)
474 int i;
475 int *len;
476 unsigned nvar;
477 isl_pw_qpolynomial_fold *pwf;
478 isl_set *set;
479 isl_pw_qpolynomial *pwqp;
481 if (!bset || !poly)
482 goto error;
484 nvar = isl_basic_set_dim(bset, isl_dim_set);
486 len = isl_alloc_array(bset->ctx, int, nvar);
487 if (!len)
488 goto error;
490 for (i = 0; i < nvar; ++i)
491 len[i] = 1;
493 set = isl_set_from_basic_set(bset);
494 pwqp = isl_pw_qpolynomial_alloc(set, poly);
496 pwf = bernstein_coefficients_recursive(pwqp, nvar, len, data, tight);
498 free(len);
500 return pwf;
501 error:
502 isl_basic_set_free(bset);
503 isl_qpolynomial_free(poly);
504 return NULL;
507 /* Compute a bound on the polynomial defined over the parametric polytope
508 * using bernstein expansion and store the result
509 * in bound->pwf and bound->pwf_tight.
511 * If bernstein_recurse is set to ISL_BERNSTEIN_FACTORS, we check if
512 * the polytope can be factorized and apply bernstein expansion recursively
513 * on the factors.
514 * If bernstein_recurse is set to ISL_BERNSTEIN_INTERVALS, we apply
515 * bernstein expansion recursively on each dimension.
516 * Otherwise, we apply bernstein expansion on the entire polytope.
518 int isl_qpolynomial_bound_on_domain_bernstein(__isl_take isl_basic_set *bset,
519 __isl_take isl_qpolynomial *poly, struct isl_bound *bound)
521 struct bernstein_data data;
522 isl_pw_qpolynomial_fold *pwf;
523 unsigned nvar;
524 int tight = 0;
525 int *tp = bound->check_tight ? &tight : NULL;
527 if (!bset || !poly)
528 goto error;
530 data.type = bound->type;
531 data.check_tight = bound->check_tight;
533 nvar = isl_basic_set_dim(bset, isl_dim_set);
535 if (bset->ctx->opt->bernstein_recurse & ISL_BERNSTEIN_FACTORS)
536 pwf = bernstein_coefficients_factors(bset, poly, &data, tp);
537 else if (nvar > 1 &&
538 (bset->ctx->opt->bernstein_recurse & ISL_BERNSTEIN_INTERVALS))
539 pwf = bernstein_coefficients_full_recursive(bset, poly, &data, tp);
540 else
541 pwf = bernstein_coefficients_base(bset, poly, &data, tp);
543 if (tight)
544 bound->pwf_tight = isl_pw_qpolynomial_fold_fold(bound->pwf_tight, pwf);
545 else
546 bound->pwf = isl_pw_qpolynomial_fold_fold(bound->pwf, pwf);
548 return 0;
549 error:
550 isl_basic_set_free(bset);
551 isl_qpolynomial_free(poly);
552 return -1;