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>
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
{
30 isl_qpolynomial
*poly
;
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
)
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
) {
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
;
61 static __isl_give isl_qpolynomial
*vertex_coordinate(
62 __isl_keep isl_basic_set
*vertex
, int i
, __isl_take isl_space
*space
)
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)
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
);
86 isl_int_neg(denom
, denom
);
88 v
= isl_qpolynomial_from_affine(space
, vertex
->eq
[r
], denom
);
93 isl_space_free(space
);
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
)
107 for (i
= 0; i
< n
; ++i
) {
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
;
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
);
132 return isl_stat_error
;
134 data
->fold_tight
= isl_qpolynomial_fold_fold_on_domain(dom
,
135 data
->fold_tight
, fold
);
137 data
->fold
= isl_qpolynomial_fold_fold_on_domain(dom
,
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
)
159 isl_qpolynomial
**c
= NULL
;
162 isl_vec
*multinom
= NULL
;
164 n
= isl_qpolynomial_dim(poly
, isl_dim_in
);
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
)
179 isl_int_set_si(multinom
->el
[0], 1);
180 for (k
[0] = d
; k
[0] >= 0; --k
[0]) {
182 isl_qpolynomial_free(c
[0]);
183 c
[0] = isl_qpolynomial_coeff(poly
, isl_dim_in
, n
- 1, k
[0]);
186 isl_int_set(multinom
->el
[1], multinom
->el
[0]);
193 for (j
= 2; j
<= left
[i
- 1]; ++j
)
194 isl_int_divexact_ui(multinom
->el
[i
],
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)
209 if (k
[i
] >= left
[i
- 1]) {
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
,
220 left
[i
] = left
[i
- 1] - k
[i
];
222 isl_int_set(multinom
->el
[i
+ 1], multinom
->el
[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
);
237 isl_vec_free(multinom
);
241 for (i
= 0; i
< n
; ++i
)
242 isl_qpolynomial_free(c
[i
]);
244 return isl_stat_error
;
247 /* Perform bernstein expansion on the parametric vertices that are active
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
,
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
;
270 isl_qpolynomial
**subs
;
271 isl_pw_qpolynomial_fold
*pwf
;
275 n_in
= isl_qpolynomial_dim(poly
, isl_dim_in
);
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
);
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
)
297 isl_qpolynomial_zero_on_domain(isl_space_copy(space_dst
));
299 for (i
= 0; i
< n_vertices
; ++i
) {
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
];
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
);
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
),
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
);
341 for (i
= 0; i
< 1 + nvar
; ++i
)
342 isl_qpolynomial_free(subs
[i
]);
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
,
363 isl_pw_qpolynomial_fold
*pwf
;
364 isl_vertices
*vertices
;
367 nvar
= isl_basic_set_dim(bset
, isl_dim_set
);
369 bset
= isl_basic_set_free(bset
);
372 isl_qpolynomial_fold
*fold
;
374 fold
= isl_qpolynomial_fold_alloc(data
->type
, poly
);
375 dom
= isl_set_from_basic_set(bset
);
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
);
382 if (isl_qpolynomial_is_zero(poly
)) {
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
);
389 *tight
= isl_bool_true
;
390 return isl_pw_qpolynomial_fold_project_domain_on_params(pwf
);
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
),
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
);
408 isl_basic_set_free(bset
);
409 isl_qpolynomial_free(poly
);
411 covers
= isl_pw_qpolynomial_fold_covers(data
->pwf_tight
, data
->pwf
);
419 isl_pw_qpolynomial_fold_free(data
->pwf
);
420 return data
->pwf_tight
;
423 data
->pwf
= isl_pw_qpolynomial_fold_fold(data
->pwf
, data
->pwf_tight
);
427 isl_pw_qpolynomial_fold_free(data
->pwf_tight
);
428 isl_pw_qpolynomial_fold_free(data
->pwf
);
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.
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
)
442 isl_pw_qpolynomial_fold
*pwf
;
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)
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
);
453 for (i
= n_group
- 2; i
>= 0; --i
) {
454 nparam
= isl_pw_qpolynomial_fold_dim(pwf
, isl_dim_param
);
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
)
461 pwf
= isl_pw_qpolynomial_fold_bound(pwf
, tight
);
466 isl_pw_qpolynomial_free(pwqp
);
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
,
477 isl_pw_qpolynomial
*pwqp
;
478 isl_pw_qpolynomial_fold
*pwf
;
480 f
= isl_basic_set_factorizer(bset
);
483 if (f
->n_group
== 0) {
484 isl_factorizer_free(f
);
485 return bernstein_coefficients_base(bset
, poly
, data
, tight
);
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
));
492 pwf
= bernstein_coefficients_recursive(pwqp
, f
->n_group
, f
->len
, data
,
495 isl_factorizer_free(f
);
499 isl_basic_set_free(bset
);
500 isl_qpolynomial_free(poly
);
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
,
512 isl_pw_qpolynomial_fold
*pwf
;
514 isl_pw_qpolynomial
*pwqp
;
516 nvar
= isl_basic_set_dim(bset
, isl_dim_set
);
517 if (nvar
< 0 || !poly
)
520 len
= isl_alloc_array(bset
->ctx
, int, nvar
);
524 for (i
= 0; i
< nvar
; ++i
)
527 set
= isl_set_from_basic_set(bset
);
528 pwqp
= isl_pw_qpolynomial_alloc(set
, poly
);
530 pwf
= bernstein_coefficients_recursive(pwqp
, nvar
, len
, data
, tight
);
536 isl_basic_set_free(bset
);
537 isl_qpolynomial_free(poly
);
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.
545 * If bernstein_recurse is set to ISL_BERNSTEIN_FACTORS, we check if
546 * the polytope can be factorized and apply bernstein expansion recursively
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.
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
)
556 struct bernstein_data data
;
557 isl_pw_qpolynomial_fold
*pwf
;
559 isl_bool tight
= isl_bool_false
;
560 isl_bool
*tp
= bound
->check_tight
? &tight
: NULL
;
562 nvar
= isl_basic_set_dim(bset
, isl_dim_set
);
563 if (nvar
< 0 || !poly
)
566 data
.type
= bound
->type
;
567 data
.check_tight
= bound
->check_tight
;
569 if (bset
->ctx
->opt
->bernstein_recurse
& ISL_BERNSTEIN_FACTORS
)
570 pwf
= bernstein_coefficients_factors(bset
, poly
, &data
, tp
);
572 (bset
->ctx
->opt
->bernstein_recurse
& ISL_BERNSTEIN_INTERVALS
))
573 pwf
= bernstein_coefficients_full_recursive(bset
, poly
, &data
, tp
);
575 pwf
= bernstein_coefficients_base(bset
, poly
, &data
, tp
);
578 bound
->pwf_tight
= isl_pw_qpolynomial_fold_fold(bound
->pwf_tight
, pwf
);
580 bound
->pwf
= isl_pw_qpolynomial_fold_fold(bound
->pwf
, pwf
);
584 isl_basic_set_free(bset
);
585 isl_qpolynomial_free(poly
);
586 return isl_stat_error
;