1 /*
2 * Copyright 2006-2007 Universiteit Leiden
3 * Copyright 2008-2009 Katholieke Universiteit Leuven
4 * Copyright 2010 INRIA Saclay
5 *
6 * Use of this software is governed by the MIT license
7 *
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
14 */
15
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>
27
28 struct bernstein_data {
29 enum isl_fold type;
30 isl_qpolynomial *poly;
31 int check_tight;
32
33 isl_cell *cell;
34
35 isl_qpolynomial_fold *fold;
36 isl_qpolynomial_fold *fold_tight;
37 isl_pw_qpolynomial_fold *pwf;
38 isl_pw_qpolynomial_fold *pwf_tight;
39 };
40
vertex_is_integral(__isl_keep isl_basic_set * vertex)41 static isl_bool vertex_is_integral(__isl_keep isl_basic_set *vertex)
42 {
43 isl_size nvar;
44 isl_size nparam;
45 int i;
46
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;
56 }
57
58 return isl_bool_true;
59 }
60
vertex_coordinate(__isl_keep isl_basic_set * vertex,int i,__isl_take isl_space * space)61 static __isl_give isl_qpolynomial *vertex_coordinate(
62 __isl_keep isl_basic_set *vertex, int i, __isl_take isl_space *space)
63 {
64 isl_size nvar;
65 isl_size nparam;
66 isl_size total;
67 int r;
68 isl_int denom;
69 isl_qpolynomial *v;
70
71 isl_int_init(denom);
72
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;
79
80 isl_int_set(denom, vertex->eq[r][1 + nparam + i]);
81 isl_assert(vertex->ctx, !isl_int_is_zero(denom), goto error);
82
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);
87
88 v = isl_qpolynomial_from_affine(space, vertex->eq[r], denom);
89 isl_int_clear(denom);
90
91 return v;
92 error:
93 isl_space_free(space);
94 isl_int_clear(denom);
95 return NULL;
96 }
97
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.
102 */
is_tight(int * k,int n,int d,isl_cell * cell)103 static isl_bool is_tight(int *k, int n, int d, isl_cell *cell)
104 {
105 int i;
106
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);
115 }
116
117 return isl_bool_false;
118 }
119
add_fold(__isl_take isl_qpolynomial * b,__isl_keep isl_set * dom,int * k,int n,int d,struct bernstein_data * data)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)
122 {
123 isl_qpolynomial_fold *fold;
124 isl_bool tight;
125
126 fold = isl_qpolynomial_fold_alloc(data->type, b);
127
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;
140 }
141
142 /* Extract the coefficients of the Bernstein base polynomials and store
143 * them in data->fold and data->fold_tight.
144 *
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]!
148 *
149 * c[i] contains the coefficient of the selected powers of the first i+1 vars.
150 * multinom[i] contains the partial multinomial coefficient.
151 */
extract_coefficients(isl_qpolynomial * poly,__isl_keep isl_set * dom,struct bernstein_data * data)152 static isl_stat extract_coefficients(isl_qpolynomial *poly,
153 __isl_keep isl_set *dom, struct bernstein_data *data)
154 {
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;
163
164 n = isl_qpolynomial_dim(poly, isl_dim_in);
165 if (n < 0)
166 return isl_stat_error;
167
168 ctx = isl_qpolynomial_get_ctx(poly);
169 d = isl_qpolynomial_degree(poly);
170 isl_assert(ctx, n >= 2, return isl_stat_error);
171
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;
178
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 *space;
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 space = isl_qpolynomial_get_domain_space(b);
200 f = isl_qpolynomial_rat_cst_on_domain(space,
201 ctx->one, 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;
208 }
209 if (k[i] >= left[i - 1]) {
210 --i;
211 continue;
212 }
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;
224 }
225 isl_int_mul_ui(multinom->el[0], multinom->el[0], k[0]);
226 }
227
228 for (i = 0; i < n; ++i)
229 isl_qpolynomial_free(c[i]);
230
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;
245 }
246
247 /* Perform bernstein expansion on the parametric vertices that are active
248 * on "cell".
249 *
250 * data->poly has been homogenized in the calling function.
251 *
252 * We plug in the barycentric coordinates for the set variables
253 *
254 * \vec x = \sum_i \alpha_i v_i(\vec p)
255 *
256 * and the constant "1 = \sum_i \alpha_i" for the homogeneous dimension.
257 * Next, we extract the coefficients of the Bernstein base polynomials.
258 */
bernstein_coefficients_cell(__isl_take isl_cell * cell,void * user)259 static isl_stat bernstein_coefficients_cell(__isl_take isl_cell *cell,
260 void *user)
261 {
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;
274
275 n_in = isl_qpolynomial_dim(poly, isl_dim_in);
276 if (n_in < 0)
277 goto error;
278
279 nvar = n_in - 1;
280 n_vertices = cell->n_vertices;
281
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);
286
287 subs = isl_alloc_array(ctx, isl_qpolynomial *, 1 + nvar);
288 if (!subs)
289 goto error;
290
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);
294
295 for (i = 0; i < 1 + nvar; ++i)
296 subs[i] =
297 isl_qpolynomial_zero_on_domain(isl_space_copy(space_dst));
298
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);
312 }
313 subs[0] = isl_qpolynomial_add(subs[0], c);
314 }
315 isl_space_free(space_dst);
316
317 poly = isl_qpolynomial_copy(poly);
318
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);
322
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);
331 }
332
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);
338
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;
348 }
349
350 /* Base case of applying bernstein expansion.
351 *
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.
355 */
bernstein_coefficients_base(__isl_take isl_basic_set * bset,__isl_take isl_qpolynomial * poly,struct bernstein_data * data,isl_bool * tight)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,
359 isl_bool *tight)
360 {
361 isl_size nvar;
362 isl_space *space;
363 isl_pw_qpolynomial_fold *pwf;
364 isl_vertices *vertices;
365 isl_bool covers;
366
367 nvar = isl_basic_set_dim(bset, isl_dim_set);
368 if (nvar < 0)
369 bset = isl_basic_set_free(bset);
370 if (nvar == 0) {
371 isl_set *dom;
372 isl_qpolynomial_fold *fold;
373
374 fold = isl_qpolynomial_fold_alloc(data->type, poly);
375 dom = isl_set_from_basic_set(bset);
376 if (tight)
377 *tight = isl_bool_true;
378 pwf = isl_pw_qpolynomial_fold_alloc(data->type, dom, fold);
379 return isl_pw_qpolynomial_fold_project_domain_on_params(pwf);
380 }
381
382 if (isl_qpolynomial_is_zero(poly)) {
383 isl_set *dom;
384 isl_qpolynomial_fold *fold;
385 fold = isl_qpolynomial_fold_alloc(data->type, poly);
386 dom = isl_set_from_basic_set(bset);
387 pwf = isl_pw_qpolynomial_fold_alloc(data->type, dom, fold);
388 if (tight)
389 *tight = isl_bool_true;
390 return isl_pw_qpolynomial_fold_project_domain_on_params(pwf);
391 }
392
393 space = isl_basic_set_get_space(bset);
394 space = isl_space_params(space);
395 space = isl_space_from_domain(space);
396 space = isl_space_add_dims(space, isl_dim_set, 1);
397 data->pwf = isl_pw_qpolynomial_fold_zero(isl_space_copy(space),
398 data->type);
399 data->pwf_tight = isl_pw_qpolynomial_fold_zero(space, data->type);
400 data->poly = isl_qpolynomial_homogenize(isl_qpolynomial_copy(poly));
401 vertices = isl_basic_set_compute_vertices(bset);
402 if (isl_vertices_foreach_disjoint_cell(vertices,
403 &bernstein_coefficients_cell, data) < 0)
404 data->pwf = isl_pw_qpolynomial_fold_free(data->pwf);
405 isl_vertices_free(vertices);
406 isl_qpolynomial_free(data->poly);
407
408 isl_basic_set_free(bset);
409 isl_qpolynomial_free(poly);
410
411 covers = isl_pw_qpolynomial_fold_covers(data->pwf_tight, data->pwf);
412 if (covers < 0)
413 goto error;
414
415 if (tight)
416 *tight = covers;
417
418 if (covers) {
419 isl_pw_qpolynomial_fold_free(data->pwf);
420 return data->pwf_tight;
421 }
422
423 data->pwf = isl_pw_qpolynomial_fold_fold(data->pwf, data->pwf_tight);
424
425 return data->pwf;
426 error:
427 isl_pw_qpolynomial_fold_free(data->pwf_tight);
428 isl_pw_qpolynomial_fold_free(data->pwf);
429 return NULL;
430 }
431
432 /* Apply bernstein expansion recursively by working in on len[i]
433 * set variables at a time, with i ranging from n_group - 1 to 0.
434 */
bernstein_coefficients_recursive(__isl_take isl_pw_qpolynomial * pwqp,int n_group,int * len,struct bernstein_data * data,isl_bool * tight)435 static __isl_give isl_pw_qpolynomial_fold *bernstein_coefficients_recursive(
436 __isl_take isl_pw_qpolynomial *pwqp,
437 int n_group, int *len, struct bernstein_data *data, isl_bool *tight)
438 {
439 int i;
440 isl_size nparam;
441 isl_size nvar;
442 isl_pw_qpolynomial_fold *pwf;
443
444 nparam = isl_pw_qpolynomial_dim(pwqp, isl_dim_param);
445 nvar = isl_pw_qpolynomial_dim(pwqp, isl_dim_in);
446 if (nparam < 0 || nvar < 0)
447 goto error;
448
449 pwqp = isl_pw_qpolynomial_move_dims(pwqp, isl_dim_param, nparam,
450 isl_dim_in, 0, nvar - len[n_group - 1]);
451 pwf = isl_pw_qpolynomial_bound(pwqp, data->type, tight);
452
453 for (i = n_group - 2; i >= 0; --i) {
454 nparam = isl_pw_qpolynomial_fold_dim(pwf, isl_dim_param);
455 if (nparam < 0)
456 return isl_pw_qpolynomial_fold_free(pwf);
457 pwf = isl_pw_qpolynomial_fold_move_dims(pwf, isl_dim_in, 0,
458 isl_dim_param, nparam - len[i], len[i]);
459 if (tight && !*tight)
460 tight = NULL;
461 pwf = isl_pw_qpolynomial_fold_bound(pwf, tight);
462 }
463
464 return pwf;
465 error:
466 isl_pw_qpolynomial_free(pwqp);
467 return NULL;
468 }
469
bernstein_coefficients_factors(__isl_take isl_basic_set * bset,__isl_take isl_qpolynomial * poly,struct bernstein_data * data,isl_bool * tight)470 static __isl_give isl_pw_qpolynomial_fold *bernstein_coefficients_factors(
471 __isl_take isl_basic_set *bset,
472 __isl_take isl_qpolynomial *poly, struct bernstein_data *data,
473 isl_bool *tight)
474 {
475 isl_factorizer *f;
476 isl_set *set;
477 isl_pw_qpolynomial *pwqp;
478 isl_pw_qpolynomial_fold *pwf;
479
480 f = isl_basic_set_factorizer(bset);
481 if (!f)
482 goto error;
483 if (f->n_group == 0) {
484 isl_factorizer_free(f);
485 return bernstein_coefficients_base(bset, poly, data, tight);
486 }
487
488 set = isl_set_from_basic_set(bset);
489 pwqp = isl_pw_qpolynomial_alloc(set, poly);
490 pwqp = isl_pw_qpolynomial_morph_domain(pwqp, isl_morph_copy(f->morph));
491
492 pwf = bernstein_coefficients_recursive(pwqp, f->n_group, f->len, data,
493 tight);
494
495 isl_factorizer_free(f);
496
497 return pwf;
498 error:
499 isl_basic_set_free(bset);
500 isl_qpolynomial_free(poly);
501 return NULL;
502 }
503
bernstein_coefficients_full_recursive(__isl_take isl_basic_set * bset,__isl_take isl_qpolynomial * poly,struct bernstein_data * data,isl_bool * tight)504 static __isl_give isl_pw_qpolynomial_fold *bernstein_coefficients_full_recursive(
505 __isl_take isl_basic_set *bset,
506 __isl_take isl_qpolynomial *poly, struct bernstein_data *data,
507 isl_bool *tight)
508 {
509 int i;
510 int *len;
511 isl_size nvar;
512 isl_pw_qpolynomial_fold *pwf;
513 isl_set *set;
514 isl_pw_qpolynomial *pwqp;
515
516 nvar = isl_basic_set_dim(bset, isl_dim_set);
517 if (nvar < 0 || !poly)
518 goto error;
519
520 len = isl_alloc_array(bset->ctx, int, nvar);
521 if (nvar && !len)
522 goto error;
523
524 for (i = 0; i < nvar; ++i)
525 len[i] = 1;
526
527 set = isl_set_from_basic_set(bset);
528 pwqp = isl_pw_qpolynomial_alloc(set, poly);
529
530 pwf = bernstein_coefficients_recursive(pwqp, nvar, len, data, tight);
531
532 free(len);
533
534 return pwf;
535 error:
536 isl_basic_set_free(bset);
537 isl_qpolynomial_free(poly);
538 return NULL;
539 }
540
541 /* Compute a bound on the polynomial defined over the parametric polytope
542 * using bernstein expansion and store the result
543 * in bound->pwf and bound->pwf_tight.
544 *
545 * If bernstein_recurse is set to ISL_BERNSTEIN_FACTORS, we check if
546 * the polytope can be factorized and apply bernstein expansion recursively
547 * on the factors.
548 * If bernstein_recurse is set to ISL_BERNSTEIN_INTERVALS, we apply
549 * bernstein expansion recursively on each dimension.
550 * Otherwise, we apply bernstein expansion on the entire polytope.
551 */
isl_qpolynomial_bound_on_domain_bernstein(__isl_take isl_basic_set * bset,__isl_take isl_qpolynomial * poly,struct isl_bound * bound)552 isl_stat isl_qpolynomial_bound_on_domain_bernstein(
553 __isl_take isl_basic_set *bset, __isl_take isl_qpolynomial *poly,
554 struct isl_bound *bound)
555 {
556 struct bernstein_data data;
557 isl_pw_qpolynomial_fold *pwf;
558 isl_size nvar;
559 isl_bool tight = isl_bool_false;
560 isl_bool *tp = bound->check_tight ? &tight : NULL;
561
562 nvar = isl_basic_set_dim(bset, isl_dim_set);
563 if (nvar < 0 || !poly)
564 goto error;
565
566 data.type = bound->type;
567 data.check_tight = bound->check_tight;
568
569 if (bset->ctx->opt->bernstein_recurse & ISL_BERNSTEIN_FACTORS)
570 pwf = bernstein_coefficients_factors(bset, poly, &data, tp);
571 else if (nvar > 1 &&
572 (bset->ctx->opt->bernstein_recurse & ISL_BERNSTEIN_INTERVALS))
573 pwf = bernstein_coefficients_full_recursive(bset, poly, &data, tp);
574 else
575 pwf = bernstein_coefficients_base(bset, poly, &data, tp);
576
577 if (tight)
578 bound->pwf_tight = isl_pw_qpolynomial_fold_fold(bound->pwf_tight, pwf);
579 else
580 bound->pwf = isl_pw_qpolynomial_fold_fold(bound->pwf, pwf);
581
582 return isl_stat_ok;
583 error:
584 isl_basic_set_free(bset);
585 isl_qpolynomial_free(poly);
586 return isl_stat_error;
587 }
588