isl_space.c: isl_space_has_equal_tuples: return isl_bool
[isl.git] / isl_bernstein.c
bloba34a0d5f94711e8ddeecb456b486168ac5092f47
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 *dim)
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(dim, vertex->eq[r], denom);
83 isl_int_clear(denom);
85 return v;
86 error:
87 isl_space_free(dim);
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 void 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);
129 /* Extract the coefficients of the Bernstein base polynomials and store
130 * them in data->fold and data->fold_tight.
132 * In particular, the coefficient of each monomial
133 * of multi-degree (k[0], k[1], ..., k[n-1]) is divided by the corresponding
134 * multinomial coefficient d!/k[0]! k[1]! ... k[n-1]!
136 * c[i] contains the coefficient of the selected powers of the first i+1 vars.
137 * multinom[i] contains the partial multinomial coefficient.
139 static void extract_coefficients(isl_qpolynomial *poly,
140 __isl_keep isl_set *dom, struct bernstein_data *data)
142 int i;
143 int d;
144 int n;
145 isl_ctx *ctx;
146 isl_qpolynomial **c = NULL;
147 int *k = NULL;
148 int *left = NULL;
149 isl_vec *multinom = NULL;
151 if (!poly)
152 return;
154 ctx = isl_qpolynomial_get_ctx(poly);
155 n = isl_qpolynomial_dim(poly, isl_dim_in);
156 d = isl_qpolynomial_degree(poly);
157 isl_assert(ctx, n >= 2, return);
159 c = isl_calloc_array(ctx, isl_qpolynomial *, n);
160 k = isl_alloc_array(ctx, int, n);
161 left = isl_alloc_array(ctx, int, n);
162 multinom = isl_vec_alloc(ctx, n);
163 if (!c || !k || !left || !multinom)
164 goto error;
166 isl_int_set_si(multinom->el[0], 1);
167 for (k[0] = d; k[0] >= 0; --k[0]) {
168 int i = 1;
169 isl_qpolynomial_free(c[0]);
170 c[0] = isl_qpolynomial_coeff(poly, isl_dim_in, n - 1, k[0]);
171 left[0] = d - k[0];
172 k[1] = -1;
173 isl_int_set(multinom->el[1], multinom->el[0]);
174 while (i > 0) {
175 if (i == n - 1) {
176 int j;
177 isl_space *dim;
178 isl_qpolynomial *b;
179 isl_qpolynomial *f;
180 for (j = 2; j <= left[i - 1]; ++j)
181 isl_int_divexact_ui(multinom->el[i],
182 multinom->el[i], j);
183 b = isl_qpolynomial_coeff(c[i - 1], isl_dim_in,
184 n - 1 - i, left[i - 1]);
185 b = isl_qpolynomial_project_domain_on_params(b);
186 dim = isl_qpolynomial_get_domain_space(b);
187 f = isl_qpolynomial_rat_cst_on_domain(dim, ctx->one,
188 multinom->el[i]);
189 b = isl_qpolynomial_mul(b, f);
190 k[n - 1] = left[n - 2];
191 add_fold(b, dom, k, n, d, data);
192 --i;
193 continue;
195 if (k[i] >= left[i - 1]) {
196 --i;
197 continue;
199 ++k[i];
200 if (k[i])
201 isl_int_divexact_ui(multinom->el[i],
202 multinom->el[i], k[i]);
203 isl_qpolynomial_free(c[i]);
204 c[i] = isl_qpolynomial_coeff(c[i - 1], isl_dim_in,
205 n - 1 - i, k[i]);
206 left[i] = left[i - 1] - k[i];
207 k[i + 1] = -1;
208 isl_int_set(multinom->el[i + 1], multinom->el[i]);
209 ++i;
211 isl_int_mul_ui(multinom->el[0], multinom->el[0], k[0]);
214 for (i = 0; i < n; ++i)
215 isl_qpolynomial_free(c[i]);
217 isl_vec_free(multinom);
218 free(left);
219 free(k);
220 free(c);
221 return;
222 error:
223 isl_vec_free(multinom);
224 free(left);
225 free(k);
226 if (c)
227 for (i = 0; i < n; ++i)
228 isl_qpolynomial_free(c[i]);
229 free(c);
230 return;
233 /* Perform bernstein expansion on the parametric vertices that are active
234 * on "cell".
236 * data->poly has been homogenized in the calling function.
238 * We plug in the barycentric coordinates for the set variables
240 * \vec x = \sum_i \alpha_i v_i(\vec p)
242 * and the constant "1 = \sum_i \alpha_i" for the homogeneous dimension.
243 * Next, we extract the coefficients of the Bernstein base polynomials.
245 static int bernstein_coefficients_cell(__isl_take isl_cell *cell, void *user)
247 int i, j;
248 struct bernstein_data *data = (struct bernstein_data *)user;
249 isl_space *dim_param;
250 isl_space *dim_dst;
251 isl_qpolynomial *poly = data->poly;
252 unsigned nvar;
253 int n_vertices;
254 isl_qpolynomial **subs;
255 isl_pw_qpolynomial_fold *pwf;
256 isl_set *dom;
257 isl_ctx *ctx;
259 if (!poly)
260 goto error;
262 nvar = isl_qpolynomial_dim(poly, isl_dim_in) - 1;
263 n_vertices = cell->n_vertices;
265 ctx = isl_qpolynomial_get_ctx(poly);
266 if (n_vertices > nvar + 1 && ctx->opt->bernstein_triangulate)
267 return isl_cell_foreach_simplex(cell,
268 &bernstein_coefficients_cell, user);
270 subs = isl_alloc_array(ctx, isl_qpolynomial *, 1 + nvar);
271 if (!subs)
272 goto error;
274 dim_param = isl_basic_set_get_space(cell->dom);
275 dim_dst = isl_qpolynomial_get_domain_space(poly);
276 dim_dst = isl_space_add_dims(dim_dst, isl_dim_set, n_vertices);
278 for (i = 0; i < 1 + nvar; ++i)
279 subs[i] = isl_qpolynomial_zero_on_domain(isl_space_copy(dim_dst));
281 for (i = 0; i < n_vertices; ++i) {
282 isl_qpolynomial *c;
283 c = isl_qpolynomial_var_on_domain(isl_space_copy(dim_dst), isl_dim_set,
284 1 + nvar + i);
285 for (j = 0; j < nvar; ++j) {
286 int k = cell->ids[i];
287 isl_qpolynomial *v;
288 v = vertex_coordinate(cell->vertices->v[k].vertex, j,
289 isl_space_copy(dim_param));
290 v = isl_qpolynomial_add_dims(v, isl_dim_in,
291 1 + nvar + n_vertices);
292 v = isl_qpolynomial_mul(v, isl_qpolynomial_copy(c));
293 subs[1 + j] = isl_qpolynomial_add(subs[1 + j], v);
295 subs[0] = isl_qpolynomial_add(subs[0], c);
297 isl_space_free(dim_dst);
299 poly = isl_qpolynomial_copy(poly);
301 poly = isl_qpolynomial_add_dims(poly, isl_dim_in, n_vertices);
302 poly = isl_qpolynomial_substitute(poly, isl_dim_in, 0, 1 + nvar, subs);
303 poly = isl_qpolynomial_drop_dims(poly, isl_dim_in, 0, 1 + nvar);
305 data->cell = cell;
306 dom = isl_set_from_basic_set(isl_basic_set_copy(cell->dom));
307 data->fold = isl_qpolynomial_fold_empty(data->type, isl_space_copy(dim_param));
308 data->fold_tight = isl_qpolynomial_fold_empty(data->type, dim_param);
309 extract_coefficients(poly, dom, data);
311 pwf = isl_pw_qpolynomial_fold_alloc(data->type, isl_set_copy(dom),
312 data->fold);
313 data->pwf = isl_pw_qpolynomial_fold_fold(data->pwf, pwf);
314 pwf = isl_pw_qpolynomial_fold_alloc(data->type, dom, data->fold_tight);
315 data->pwf_tight = isl_pw_qpolynomial_fold_fold(data->pwf_tight, pwf);
317 isl_qpolynomial_free(poly);
318 isl_cell_free(cell);
319 for (i = 0; i < 1 + nvar; ++i)
320 isl_qpolynomial_free(subs[i]);
321 free(subs);
322 return 0;
323 error:
324 isl_cell_free(cell);
325 return -1;
328 /* Base case of applying bernstein expansion.
330 * We compute the chamber decomposition of the parametric polytope "bset"
331 * and then perform bernstein expansion on the parametric vertices
332 * that are active on each chamber.
334 static __isl_give isl_pw_qpolynomial_fold *bernstein_coefficients_base(
335 __isl_take isl_basic_set *bset,
336 __isl_take isl_qpolynomial *poly, struct bernstein_data *data, int *tight)
338 unsigned nvar;
339 isl_space *dim;
340 isl_pw_qpolynomial_fold *pwf;
341 isl_vertices *vertices;
342 int covers;
344 nvar = isl_basic_set_dim(bset, isl_dim_set);
345 if (nvar == 0) {
346 isl_set *dom;
347 isl_qpolynomial_fold *fold;
349 fold = isl_qpolynomial_fold_alloc(data->type, poly);
350 dom = isl_set_from_basic_set(bset);
351 if (tight)
352 *tight = 1;
353 pwf = isl_pw_qpolynomial_fold_alloc(data->type, dom, fold);
354 return isl_pw_qpolynomial_fold_project_domain_on_params(pwf);
357 if (isl_qpolynomial_is_zero(poly)) {
358 isl_set *dom;
359 isl_qpolynomial_fold *fold;
360 fold = isl_qpolynomial_fold_alloc(data->type, poly);
361 dom = isl_set_from_basic_set(bset);
362 pwf = isl_pw_qpolynomial_fold_alloc(data->type, dom, fold);
363 if (tight)
364 *tight = 1;
365 return isl_pw_qpolynomial_fold_project_domain_on_params(pwf);
368 dim = isl_basic_set_get_space(bset);
369 dim = isl_space_params(dim);
370 dim = isl_space_from_domain(dim);
371 dim = isl_space_add_dims(dim, isl_dim_set, 1);
372 data->pwf = isl_pw_qpolynomial_fold_zero(isl_space_copy(dim), data->type);
373 data->pwf_tight = isl_pw_qpolynomial_fold_zero(dim, data->type);
374 data->poly = isl_qpolynomial_homogenize(isl_qpolynomial_copy(poly));
375 vertices = isl_basic_set_compute_vertices(bset);
376 isl_vertices_foreach_disjoint_cell(vertices,
377 &bernstein_coefficients_cell, data);
378 isl_vertices_free(vertices);
379 isl_qpolynomial_free(data->poly);
381 isl_basic_set_free(bset);
382 isl_qpolynomial_free(poly);
384 covers = isl_pw_qpolynomial_fold_covers(data->pwf_tight, data->pwf);
385 if (covers < 0)
386 goto error;
388 if (tight)
389 *tight = covers;
391 if (covers) {
392 isl_pw_qpolynomial_fold_free(data->pwf);
393 return data->pwf_tight;
396 data->pwf = isl_pw_qpolynomial_fold_fold(data->pwf, data->pwf_tight);
398 return data->pwf;
399 error:
400 isl_pw_qpolynomial_fold_free(data->pwf_tight);
401 isl_pw_qpolynomial_fold_free(data->pwf);
402 return NULL;
405 /* Apply bernstein expansion recursively by working in on len[i]
406 * set variables at a time, with i ranging from n_group - 1 to 0.
408 static __isl_give isl_pw_qpolynomial_fold *bernstein_coefficients_recursive(
409 __isl_take isl_pw_qpolynomial *pwqp,
410 int n_group, int *len, struct bernstein_data *data, int *tight)
412 int i;
413 unsigned nparam;
414 unsigned nvar;
415 isl_pw_qpolynomial_fold *pwf;
417 if (!pwqp)
418 return NULL;
420 nparam = isl_pw_qpolynomial_dim(pwqp, isl_dim_param);
421 nvar = isl_pw_qpolynomial_dim(pwqp, isl_dim_in);
423 pwqp = isl_pw_qpolynomial_move_dims(pwqp, isl_dim_param, nparam,
424 isl_dim_in, 0, nvar - len[n_group - 1]);
425 pwf = isl_pw_qpolynomial_bound(pwqp, data->type, tight);
427 for (i = n_group - 2; i >= 0; --i) {
428 nparam = isl_pw_qpolynomial_fold_dim(pwf, isl_dim_param);
429 pwf = isl_pw_qpolynomial_fold_move_dims(pwf, isl_dim_in, 0,
430 isl_dim_param, nparam - len[i], len[i]);
431 if (tight && !*tight)
432 tight = NULL;
433 pwf = isl_pw_qpolynomial_fold_bound(pwf, tight);
436 return pwf;
439 static __isl_give isl_pw_qpolynomial_fold *bernstein_coefficients_factors(
440 __isl_take isl_basic_set *bset,
441 __isl_take isl_qpolynomial *poly, struct bernstein_data *data, int *tight)
443 isl_factorizer *f;
444 isl_set *set;
445 isl_pw_qpolynomial *pwqp;
446 isl_pw_qpolynomial_fold *pwf;
448 f = isl_basic_set_factorizer(bset);
449 if (!f)
450 goto error;
451 if (f->n_group == 0) {
452 isl_factorizer_free(f);
453 return bernstein_coefficients_base(bset, poly, data, tight);
456 set = isl_set_from_basic_set(bset);
457 pwqp = isl_pw_qpolynomial_alloc(set, poly);
458 pwqp = isl_pw_qpolynomial_morph_domain(pwqp, isl_morph_copy(f->morph));
460 pwf = bernstein_coefficients_recursive(pwqp, f->n_group, f->len, data,
461 tight);
463 isl_factorizer_free(f);
465 return pwf;
466 error:
467 isl_basic_set_free(bset);
468 isl_qpolynomial_free(poly);
469 return NULL;
472 static __isl_give isl_pw_qpolynomial_fold *bernstein_coefficients_full_recursive(
473 __isl_take isl_basic_set *bset,
474 __isl_take isl_qpolynomial *poly, struct bernstein_data *data, int *tight)
476 int i;
477 int *len;
478 unsigned nvar;
479 isl_pw_qpolynomial_fold *pwf;
480 isl_set *set;
481 isl_pw_qpolynomial *pwqp;
483 if (!bset || !poly)
484 goto error;
486 nvar = isl_basic_set_dim(bset, isl_dim_set);
488 len = isl_alloc_array(bset->ctx, int, nvar);
489 if (nvar && !len)
490 goto error;
492 for (i = 0; i < nvar; ++i)
493 len[i] = 1;
495 set = isl_set_from_basic_set(bset);
496 pwqp = isl_pw_qpolynomial_alloc(set, poly);
498 pwf = bernstein_coefficients_recursive(pwqp, nvar, len, data, tight);
500 free(len);
502 return pwf;
503 error:
504 isl_basic_set_free(bset);
505 isl_qpolynomial_free(poly);
506 return NULL;
509 /* Compute a bound on the polynomial defined over the parametric polytope
510 * using bernstein expansion and store the result
511 * in bound->pwf and bound->pwf_tight.
513 * If bernstein_recurse is set to ISL_BERNSTEIN_FACTORS, we check if
514 * the polytope can be factorized and apply bernstein expansion recursively
515 * on the factors.
516 * If bernstein_recurse is set to ISL_BERNSTEIN_INTERVALS, we apply
517 * bernstein expansion recursively on each dimension.
518 * Otherwise, we apply bernstein expansion on the entire polytope.
520 int isl_qpolynomial_bound_on_domain_bernstein(__isl_take isl_basic_set *bset,
521 __isl_take isl_qpolynomial *poly, struct isl_bound *bound)
523 struct bernstein_data data;
524 isl_pw_qpolynomial_fold *pwf;
525 unsigned nvar;
526 int tight = 0;
527 int *tp = bound->check_tight ? &tight : NULL;
529 if (!bset || !poly)
530 goto error;
532 data.type = bound->type;
533 data.check_tight = bound->check_tight;
535 nvar = isl_basic_set_dim(bset, isl_dim_set);
537 if (bset->ctx->opt->bernstein_recurse & ISL_BERNSTEIN_FACTORS)
538 pwf = bernstein_coefficients_factors(bset, poly, &data, tp);
539 else if (nvar > 1 &&
540 (bset->ctx->opt->bernstein_recurse & ISL_BERNSTEIN_INTERVALS))
541 pwf = bernstein_coefficients_full_recursive(bset, poly, &data, tp);
542 else
543 pwf = bernstein_coefficients_base(bset, poly, &data, tp);
545 if (tight)
546 bound->pwf_tight = isl_pw_qpolynomial_fold_fold(bound->pwf_tight, pwf);
547 else
548 bound->pwf = isl_pw_qpolynomial_fold_fold(bound->pwf, pwf);
550 return 0;
551 error:
552 isl_basic_set_free(bset);
553 isl_qpolynomial_free(poly);
554 return -1;