isl_bernstein.c: add_fold: return isl_stat
[isl.git] / isl_bernstein.c
blob9c6e0506fc4cb3100c7d0726993e07bd9a3b514e
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 int vertex_is_integral(__isl_keep isl_basic_set *vertex)
43 unsigned nvar;
44 unsigned 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 for (i = 0; i < nvar; ++i) {
50 int r = nvar - 1 - i;
51 if (!isl_int_is_one(vertex->eq[r][1 + nparam + i]) &&
52 !isl_int_is_negone(vertex->eq[r][1 + nparam + i]))
53 return 0;
56 return 1;
59 static __isl_give isl_qpolynomial *vertex_coordinate(
60 __isl_keep isl_basic_set *vertex, int i, __isl_take isl_space *space)
62 unsigned nvar;
63 unsigned nparam;
64 int r;
65 isl_int denom;
66 isl_qpolynomial *v;
68 nvar = isl_basic_set_dim(vertex, isl_dim_set);
69 nparam = isl_basic_set_dim(vertex, isl_dim_param);
70 r = nvar - 1 - i;
72 isl_int_init(denom);
73 isl_int_set(denom, vertex->eq[r][1 + nparam + i]);
74 isl_assert(vertex->ctx, !isl_int_is_zero(denom), goto error);
76 if (isl_int_is_pos(denom))
77 isl_seq_neg(vertex->eq[r], vertex->eq[r],
78 1 + isl_basic_set_total_dim(vertex));
79 else
80 isl_int_neg(denom, denom);
82 v = isl_qpolynomial_from_affine(space, vertex->eq[r], denom);
83 isl_int_clear(denom);
85 return v;
86 error:
87 isl_space_free(space);
88 isl_int_clear(denom);
89 return NULL;
92 /* Check whether the bound associated to the selection "k" is tight,
93 * which is the case if we select exactly one vertex and if that vertex
94 * is integral for all values of the parameters.
96 static int is_tight(int *k, int n, int d, isl_cell *cell)
98 int i;
100 for (i = 0; i < n; ++i) {
101 int v;
102 if (k[i] != d) {
103 if (k[i])
104 return 0;
105 continue;
107 v = cell->ids[n - 1 - i];
108 return vertex_is_integral(cell->vertices->v[v].vertex);
111 return 0;
114 static isl_stat add_fold(__isl_take isl_qpolynomial *b, __isl_keep isl_set *dom,
115 int *k, int n, int d, struct bernstein_data *data)
117 isl_qpolynomial_fold *fold;
119 fold = isl_qpolynomial_fold_alloc(data->type, b);
121 if (data->check_tight && is_tight(k, n, d, data->cell))
122 data->fold_tight = isl_qpolynomial_fold_fold_on_domain(dom,
123 data->fold_tight, fold);
124 else
125 data->fold = isl_qpolynomial_fold_fold_on_domain(dom,
126 data->fold, fold);
127 return isl_stat_ok;
130 /* Extract the coefficients of the Bernstein base polynomials and store
131 * them in data->fold and data->fold_tight.
133 * In particular, the coefficient of each monomial
134 * of multi-degree (k[0], k[1], ..., k[n-1]) is divided by the corresponding
135 * multinomial coefficient d!/k[0]! k[1]! ... k[n-1]!
137 * c[i] contains the coefficient of the selected powers of the first i+1 vars.
138 * multinom[i] contains the partial multinomial coefficient.
140 static isl_stat extract_coefficients(isl_qpolynomial *poly,
141 __isl_keep isl_set *dom, struct bernstein_data *data)
143 int i;
144 int d;
145 int n;
146 isl_ctx *ctx;
147 isl_qpolynomial **c = NULL;
148 int *k = NULL;
149 int *left = NULL;
150 isl_vec *multinom = NULL;
152 if (!poly)
153 return isl_stat_error;
155 ctx = isl_qpolynomial_get_ctx(poly);
156 n = isl_qpolynomial_dim(poly, isl_dim_in);
157 d = isl_qpolynomial_degree(poly);
158 isl_assert(ctx, n >= 2, return isl_stat_error);
160 c = isl_calloc_array(ctx, isl_qpolynomial *, n);
161 k = isl_alloc_array(ctx, int, n);
162 left = isl_alloc_array(ctx, int, n);
163 multinom = isl_vec_alloc(ctx, n);
164 if (!c || !k || !left || !multinom)
165 goto error;
167 isl_int_set_si(multinom->el[0], 1);
168 for (k[0] = d; k[0] >= 0; --k[0]) {
169 int i = 1;
170 isl_qpolynomial_free(c[0]);
171 c[0] = isl_qpolynomial_coeff(poly, isl_dim_in, n - 1, k[0]);
172 left[0] = d - k[0];
173 k[1] = -1;
174 isl_int_set(multinom->el[1], multinom->el[0]);
175 while (i > 0) {
176 if (i == n - 1) {
177 int j;
178 isl_space *dim;
179 isl_qpolynomial *b;
180 isl_qpolynomial *f;
181 for (j = 2; j <= left[i - 1]; ++j)
182 isl_int_divexact_ui(multinom->el[i],
183 multinom->el[i], j);
184 b = isl_qpolynomial_coeff(c[i - 1], isl_dim_in,
185 n - 1 - i, left[i - 1]);
186 b = isl_qpolynomial_project_domain_on_params(b);
187 dim = isl_qpolynomial_get_domain_space(b);
188 f = isl_qpolynomial_rat_cst_on_domain(dim, ctx->one,
189 multinom->el[i]);
190 b = isl_qpolynomial_mul(b, f);
191 k[n - 1] = left[n - 2];
192 if (add_fold(b, dom, k, n, d, data) < 0)
193 goto error;
194 --i;
195 continue;
197 if (k[i] >= left[i - 1]) {
198 --i;
199 continue;
201 ++k[i];
202 if (k[i])
203 isl_int_divexact_ui(multinom->el[i],
204 multinom->el[i], k[i]);
205 isl_qpolynomial_free(c[i]);
206 c[i] = isl_qpolynomial_coeff(c[i - 1], isl_dim_in,
207 n - 1 - i, k[i]);
208 left[i] = left[i - 1] - k[i];
209 k[i + 1] = -1;
210 isl_int_set(multinom->el[i + 1], multinom->el[i]);
211 ++i;
213 isl_int_mul_ui(multinom->el[0], multinom->el[0], k[0]);
216 for (i = 0; i < n; ++i)
217 isl_qpolynomial_free(c[i]);
219 isl_vec_free(multinom);
220 free(left);
221 free(k);
222 free(c);
223 return isl_stat_ok;
224 error:
225 isl_vec_free(multinom);
226 free(left);
227 free(k);
228 if (c)
229 for (i = 0; i < n; ++i)
230 isl_qpolynomial_free(c[i]);
231 free(c);
232 return isl_stat_error;
235 /* Perform bernstein expansion on the parametric vertices that are active
236 * on "cell".
238 * data->poly has been homogenized in the calling function.
240 * We plug in the barycentric coordinates for the set variables
242 * \vec x = \sum_i \alpha_i v_i(\vec p)
244 * and the constant "1 = \sum_i \alpha_i" for the homogeneous dimension.
245 * Next, we extract the coefficients of the Bernstein base polynomials.
247 static isl_stat bernstein_coefficients_cell(__isl_take isl_cell *cell,
248 void *user)
250 int i, j;
251 struct bernstein_data *data = (struct bernstein_data *)user;
252 isl_space *space_param;
253 isl_space *space_dst;
254 isl_qpolynomial *poly = data->poly;
255 unsigned nvar;
256 int n_vertices;
257 isl_qpolynomial **subs;
258 isl_pw_qpolynomial_fold *pwf;
259 isl_set *dom;
260 isl_ctx *ctx;
262 if (!poly)
263 goto error;
265 nvar = isl_qpolynomial_dim(poly, isl_dim_in) - 1;
266 n_vertices = cell->n_vertices;
268 ctx = isl_qpolynomial_get_ctx(poly);
269 if (n_vertices > nvar + 1 && ctx->opt->bernstein_triangulate)
270 return isl_cell_foreach_simplex(cell,
271 &bernstein_coefficients_cell, user);
273 subs = isl_alloc_array(ctx, isl_qpolynomial *, 1 + nvar);
274 if (!subs)
275 goto error;
277 space_param = isl_basic_set_get_space(cell->dom);
278 space_dst = isl_qpolynomial_get_domain_space(poly);
279 space_dst = isl_space_add_dims(space_dst, isl_dim_set, n_vertices);
281 for (i = 0; i < 1 + nvar; ++i)
282 subs[i] =
283 isl_qpolynomial_zero_on_domain(isl_space_copy(space_dst));
285 for (i = 0; i < n_vertices; ++i) {
286 isl_qpolynomial *c;
287 c = isl_qpolynomial_var_on_domain(isl_space_copy(space_dst),
288 isl_dim_set, 1 + nvar + i);
289 for (j = 0; j < nvar; ++j) {
290 int k = cell->ids[i];
291 isl_qpolynomial *v;
292 v = vertex_coordinate(cell->vertices->v[k].vertex, j,
293 isl_space_copy(space_param));
294 v = isl_qpolynomial_add_dims(v, isl_dim_in,
295 1 + nvar + n_vertices);
296 v = isl_qpolynomial_mul(v, isl_qpolynomial_copy(c));
297 subs[1 + j] = isl_qpolynomial_add(subs[1 + j], v);
299 subs[0] = isl_qpolynomial_add(subs[0], c);
301 isl_space_free(space_dst);
303 poly = isl_qpolynomial_copy(poly);
305 poly = isl_qpolynomial_add_dims(poly, isl_dim_in, n_vertices);
306 poly = isl_qpolynomial_substitute(poly, isl_dim_in, 0, 1 + nvar, subs);
307 poly = isl_qpolynomial_drop_dims(poly, isl_dim_in, 0, 1 + nvar);
309 data->cell = cell;
310 dom = isl_set_from_basic_set(isl_basic_set_copy(cell->dom));
311 data->fold = isl_qpolynomial_fold_empty(data->type,
312 isl_space_copy(space_param));
313 data->fold_tight = isl_qpolynomial_fold_empty(data->type, space_param);
314 if (extract_coefficients(poly, dom, data) < 0) {
315 data->fold = isl_qpolynomial_fold_free(data->fold);
316 data->fold_tight = isl_qpolynomial_fold_free(data->fold_tight);
319 pwf = isl_pw_qpolynomial_fold_alloc(data->type, isl_set_copy(dom),
320 data->fold);
321 data->pwf = isl_pw_qpolynomial_fold_fold(data->pwf, pwf);
322 pwf = isl_pw_qpolynomial_fold_alloc(data->type, dom, data->fold_tight);
323 data->pwf_tight = isl_pw_qpolynomial_fold_fold(data->pwf_tight, pwf);
325 isl_qpolynomial_free(poly);
326 isl_cell_free(cell);
327 for (i = 0; i < 1 + nvar; ++i)
328 isl_qpolynomial_free(subs[i]);
329 free(subs);
330 return isl_stat_ok;
331 error:
332 isl_cell_free(cell);
333 return isl_stat_error;
336 /* Base case of applying bernstein expansion.
338 * We compute the chamber decomposition of the parametric polytope "bset"
339 * and then perform bernstein expansion on the parametric vertices
340 * that are active on each chamber.
342 static __isl_give isl_pw_qpolynomial_fold *bernstein_coefficients_base(
343 __isl_take isl_basic_set *bset,
344 __isl_take isl_qpolynomial *poly, struct bernstein_data *data, int *tight)
346 unsigned nvar;
347 isl_space *space;
348 isl_pw_qpolynomial_fold *pwf;
349 isl_vertices *vertices;
350 isl_bool covers;
352 nvar = isl_basic_set_dim(bset, isl_dim_set);
353 if (nvar == 0) {
354 isl_set *dom;
355 isl_qpolynomial_fold *fold;
357 fold = isl_qpolynomial_fold_alloc(data->type, poly);
358 dom = isl_set_from_basic_set(bset);
359 if (tight)
360 *tight = 1;
361 pwf = isl_pw_qpolynomial_fold_alloc(data->type, dom, fold);
362 return isl_pw_qpolynomial_fold_project_domain_on_params(pwf);
365 if (isl_qpolynomial_is_zero(poly)) {
366 isl_set *dom;
367 isl_qpolynomial_fold *fold;
368 fold = isl_qpolynomial_fold_alloc(data->type, poly);
369 dom = isl_set_from_basic_set(bset);
370 pwf = isl_pw_qpolynomial_fold_alloc(data->type, dom, fold);
371 if (tight)
372 *tight = 1;
373 return isl_pw_qpolynomial_fold_project_domain_on_params(pwf);
376 space = isl_basic_set_get_space(bset);
377 space = isl_space_params(space);
378 space = isl_space_from_domain(space);
379 space = isl_space_add_dims(space, isl_dim_set, 1);
380 data->pwf = isl_pw_qpolynomial_fold_zero(isl_space_copy(space),
381 data->type);
382 data->pwf_tight = isl_pw_qpolynomial_fold_zero(space, data->type);
383 data->poly = isl_qpolynomial_homogenize(isl_qpolynomial_copy(poly));
384 vertices = isl_basic_set_compute_vertices(bset);
385 if (isl_vertices_foreach_disjoint_cell(vertices,
386 &bernstein_coefficients_cell, data) < 0)
387 data->pwf = isl_pw_qpolynomial_fold_free(data->pwf);
388 isl_vertices_free(vertices);
389 isl_qpolynomial_free(data->poly);
391 isl_basic_set_free(bset);
392 isl_qpolynomial_free(poly);
394 covers = isl_pw_qpolynomial_fold_covers(data->pwf_tight, data->pwf);
395 if (covers < 0)
396 goto error;
398 if (tight)
399 *tight = covers;
401 if (covers) {
402 isl_pw_qpolynomial_fold_free(data->pwf);
403 return data->pwf_tight;
406 data->pwf = isl_pw_qpolynomial_fold_fold(data->pwf, data->pwf_tight);
408 return data->pwf;
409 error:
410 isl_pw_qpolynomial_fold_free(data->pwf_tight);
411 isl_pw_qpolynomial_fold_free(data->pwf);
412 return NULL;
415 /* Apply bernstein expansion recursively by working in on len[i]
416 * set variables at a time, with i ranging from n_group - 1 to 0.
418 static __isl_give isl_pw_qpolynomial_fold *bernstein_coefficients_recursive(
419 __isl_take isl_pw_qpolynomial *pwqp,
420 int n_group, int *len, struct bernstein_data *data, int *tight)
422 int i;
423 unsigned nparam;
424 unsigned nvar;
425 isl_pw_qpolynomial_fold *pwf;
427 if (!pwqp)
428 return NULL;
430 nparam = isl_pw_qpolynomial_dim(pwqp, isl_dim_param);
431 nvar = isl_pw_qpolynomial_dim(pwqp, isl_dim_in);
433 pwqp = isl_pw_qpolynomial_move_dims(pwqp, isl_dim_param, nparam,
434 isl_dim_in, 0, nvar - len[n_group - 1]);
435 pwf = isl_pw_qpolynomial_bound(pwqp, data->type, tight);
437 for (i = n_group - 2; i >= 0; --i) {
438 nparam = isl_pw_qpolynomial_fold_dim(pwf, isl_dim_param);
439 pwf = isl_pw_qpolynomial_fold_move_dims(pwf, isl_dim_in, 0,
440 isl_dim_param, nparam - len[i], len[i]);
441 if (tight && !*tight)
442 tight = NULL;
443 pwf = isl_pw_qpolynomial_fold_bound(pwf, tight);
446 return pwf;
449 static __isl_give isl_pw_qpolynomial_fold *bernstein_coefficients_factors(
450 __isl_take isl_basic_set *bset,
451 __isl_take isl_qpolynomial *poly, struct bernstein_data *data, int *tight)
453 isl_factorizer *f;
454 isl_set *set;
455 isl_pw_qpolynomial *pwqp;
456 isl_pw_qpolynomial_fold *pwf;
458 f = isl_basic_set_factorizer(bset);
459 if (!f)
460 goto error;
461 if (f->n_group == 0) {
462 isl_factorizer_free(f);
463 return bernstein_coefficients_base(bset, poly, data, tight);
466 set = isl_set_from_basic_set(bset);
467 pwqp = isl_pw_qpolynomial_alloc(set, poly);
468 pwqp = isl_pw_qpolynomial_morph_domain(pwqp, isl_morph_copy(f->morph));
470 pwf = bernstein_coefficients_recursive(pwqp, f->n_group, f->len, data,
471 tight);
473 isl_factorizer_free(f);
475 return pwf;
476 error:
477 isl_basic_set_free(bset);
478 isl_qpolynomial_free(poly);
479 return NULL;
482 static __isl_give isl_pw_qpolynomial_fold *bernstein_coefficients_full_recursive(
483 __isl_take isl_basic_set *bset,
484 __isl_take isl_qpolynomial *poly, struct bernstein_data *data, int *tight)
486 int i;
487 int *len;
488 unsigned nvar;
489 isl_pw_qpolynomial_fold *pwf;
490 isl_set *set;
491 isl_pw_qpolynomial *pwqp;
493 if (!bset || !poly)
494 goto error;
496 nvar = isl_basic_set_dim(bset, isl_dim_set);
498 len = isl_alloc_array(bset->ctx, int, nvar);
499 if (nvar && !len)
500 goto error;
502 for (i = 0; i < nvar; ++i)
503 len[i] = 1;
505 set = isl_set_from_basic_set(bset);
506 pwqp = isl_pw_qpolynomial_alloc(set, poly);
508 pwf = bernstein_coefficients_recursive(pwqp, nvar, len, data, tight);
510 free(len);
512 return pwf;
513 error:
514 isl_basic_set_free(bset);
515 isl_qpolynomial_free(poly);
516 return NULL;
519 /* Compute a bound on the polynomial defined over the parametric polytope
520 * using bernstein expansion and store the result
521 * in bound->pwf and bound->pwf_tight.
523 * If bernstein_recurse is set to ISL_BERNSTEIN_FACTORS, we check if
524 * the polytope can be factorized and apply bernstein expansion recursively
525 * on the factors.
526 * If bernstein_recurse is set to ISL_BERNSTEIN_INTERVALS, we apply
527 * bernstein expansion recursively on each dimension.
528 * Otherwise, we apply bernstein expansion on the entire polytope.
530 isl_stat isl_qpolynomial_bound_on_domain_bernstein(
531 __isl_take isl_basic_set *bset, __isl_take isl_qpolynomial *poly,
532 struct isl_bound *bound)
534 struct bernstein_data data;
535 isl_pw_qpolynomial_fold *pwf;
536 unsigned nvar;
537 int tight = 0;
538 int *tp = bound->check_tight ? &tight : NULL;
540 if (!bset || !poly)
541 goto error;
543 data.type = bound->type;
544 data.check_tight = bound->check_tight;
546 nvar = isl_basic_set_dim(bset, isl_dim_set);
548 if (bset->ctx->opt->bernstein_recurse & ISL_BERNSTEIN_FACTORS)
549 pwf = bernstein_coefficients_factors(bset, poly, &data, tp);
550 else if (nvar > 1 &&
551 (bset->ctx->opt->bernstein_recurse & ISL_BERNSTEIN_INTERVALS))
552 pwf = bernstein_coefficients_full_recursive(bset, poly, &data, tp);
553 else
554 pwf = bernstein_coefficients_base(bset, poly, &data, tp);
556 if (tight)
557 bound->pwf_tight = isl_pw_qpolynomial_fold_fold(bound->pwf_tight, pwf);
558 else
559 bound->pwf = isl_pw_qpolynomial_fold_fold(bound->pwf, pwf);
561 return isl_stat_ok;
562 error:
563 isl_basic_set_free(bset);
564 isl_qpolynomial_free(poly);
565 return isl_stat_error;