2 #include <isl_set_polylib.h>
3 #include <isl/constraint.h>
5 #include <barvinok/evalue.h>
7 static __isl_give isl_qpolynomial
*extract_base(__isl_take isl_space
*dim
,
15 isl_qpolynomial
*base
, *c
;
21 if (e
->x
.p
->type
== polynomial
)
22 return isl_qpolynomial_var_on_domain(dim
, isl_dim_param
, e
->x
.p
->pos
- 1);
24 ctx
= isl_space_get_ctx(dim
);
25 nparam
= isl_space_dim(dim
, isl_dim_param
);
26 v
= isl_vec_alloc(ctx
, 2 + nparam
);
30 isl_seq_clr(v
->el
+ 2, nparam
);
31 evalue_extract_affine(&e
->x
.p
->arr
[0], v
->el
+ 2, &v
->el
[1], &v
->el
[0]);
33 ls
= isl_local_space_from_space(isl_space_copy(dim
));
34 aff
= isl_aff_zero_on_domain(ls
);
35 aff
= isl_aff_set_constant(aff
, v
->el
[1]);
36 aff
= isl_aff_set_denominator(aff
, v
->el
[0]);
38 for (i
= 0; i
< nparam
; ++i
)
39 aff
= isl_aff_set_coefficient(aff
, isl_dim_param
, i
,
42 aff
= isl_aff_floor(aff
);
43 base
= isl_qpolynomial_from_aff(aff
);
45 if (e
->x
.p
->type
== fractional
) {
46 base
= isl_qpolynomial_neg(base
);
48 c
= isl_qpolynomial_rat_cst_on_domain(isl_space_copy(dim
), v
->el
[1], v
->el
[0]);
49 base
= isl_qpolynomial_add(base
, c
);
51 for (i
= 0; i
< nparam
; ++i
) {
53 c
= isl_qpolynomial_rat_cst_on_domain(isl_space_copy(dim
),
54 v
->el
[2 + i
], v
->el
[0]);
55 t
= isl_qpolynomial_var_on_domain(isl_space_copy(dim
),
57 t
= isl_qpolynomial_mul(c
, t
);
58 base
= isl_qpolynomial_add(base
, t
);
71 static int type_offset(enode
*p
)
73 return p
->type
== fractional
? 1 :
74 p
->type
== flooring
? 1 : 0;
77 __isl_give isl_qpolynomial
*isl_qpolynomial_from_evalue(__isl_take isl_space
*dim
,
82 isl_qpolynomial
*base
;
85 if (EVALUE_IS_NAN(*e
))
86 return isl_qpolynomial_infty_on_domain(dim
);
87 if (value_notzero_p(e
->d
))
88 return isl_qpolynomial_rat_cst_on_domain(dim
, e
->x
.n
, e
->d
);
90 offset
= type_offset(e
->x
.p
);
92 assert(e
->x
.p
->type
== polynomial
||
93 e
->x
.p
->type
== flooring
||
94 e
->x
.p
->type
== fractional
);
95 assert(e
->x
.p
->size
>= 1 + offset
);
97 base
= extract_base(isl_space_copy(dim
), e
);
98 qp
= isl_qpolynomial_from_evalue(isl_space_copy(dim
),
99 &e
->x
.p
->arr
[e
->x
.p
->size
- 1]);
101 for (i
= e
->x
.p
->size
- 2; i
>= offset
; --i
) {
102 qp
= isl_qpolynomial_mul(qp
, isl_qpolynomial_copy(base
));
103 qp
= isl_qpolynomial_add(qp
,
104 isl_qpolynomial_from_evalue(isl_space_copy(dim
),
108 isl_qpolynomial_free(base
);
114 static __isl_give isl_pw_qpolynomial
*guarded_evalue2pwqp(__isl_take isl_set
*set
,
117 static __isl_give isl_pw_qpolynomial
*relation2pwqp(__isl_take isl_set
*set
,
125 isl_pw_qpolynomial
*pwqp
;
126 struct isl_constraint
*c
;
127 struct isl_basic_set
*bset
;
128 struct isl_set
*guard
;
134 if (e
->x
.p
->size
== 1) {
135 dim
= isl_set_get_space(set
);
137 return isl_pw_qpolynomial_zero(dim
);
140 ctx
= isl_set_get_ctx(set
);
141 isl_assert(ctx
, e
->x
.p
->size
> 0, goto error
);
142 isl_assert(ctx
, e
->x
.p
->size
<= 3, goto error
);
143 isl_assert(ctx
, value_zero_p(e
->x
.p
->arr
[0].d
), goto error
);
144 isl_assert(ctx
, e
->x
.p
->arr
[0].x
.p
->type
== fractional
, goto error
);
145 fract
= &e
->x
.p
->arr
[0];
147 nparam
= isl_set_dim(set
, isl_dim_param
);
148 vec
= isl_vec_alloc(ctx
, 2 + nparam
+ 1);
152 isl_seq_clr(vec
->el
+ 2, nparam
);
153 evalue_extract_affine(&fract
->x
.p
->arr
[0],
154 vec
->el
+ 2, &vec
->el
[1], &vec
->el
[0]);
156 dim
= isl_set_get_space(set
);
157 dim
= isl_space_add_dims(dim
, isl_dim_set
, 1);
159 bset
= isl_basic_set_universe(isl_space_copy(dim
));
160 c
= isl_equality_alloc(isl_local_space_from_space(dim
));
161 isl_int_neg(vec
->el
[0], vec
->el
[0]);
162 isl_constraint_set_coefficient(c
, isl_dim_set
, 0, vec
->el
[0]);
163 isl_constraint_set_constant(c
, vec
->el
[1]);
164 for (i
= 0; i
< nparam
; ++i
)
165 isl_constraint_set_coefficient(c
, isl_dim_param
, i
, vec
->el
[2+i
]);
166 bset
= isl_basic_set_add_constraint(bset
, c
);
167 bset
= isl_basic_set_params(bset
);
168 guard
= isl_set_from_basic_set(bset
);
171 pwqp
= guarded_evalue2pwqp(isl_set_intersect(isl_set_copy(set
),
172 isl_set_copy(guard
)),
175 if (e
->x
.p
->size
== 3) {
176 isl_pw_qpolynomial
*pwqpc
;
177 guard
= isl_set_complement(guard
);
178 pwqpc
= guarded_evalue2pwqp(isl_set_intersect(isl_set_copy(set
),
179 isl_set_copy(guard
)),
181 pwqp
= isl_pw_qpolynomial_add_disjoint(pwqp
, pwqpc
);
193 static __isl_give isl_pw_qpolynomial
*guarded_evalue2pwqp(__isl_take isl_set
*set
,
198 if (value_zero_p(e
->d
) && e
->x
.p
->type
== relation
)
199 return relation2pwqp(set
, e
);
201 qp
= isl_qpolynomial_from_evalue(isl_set_get_space(set
), e
);
203 return isl_pw_qpolynomial_alloc(set
, qp
);
206 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_from_evalue(__isl_take isl_space
*dim
, const evalue
*e
)
210 isl_pw_qpolynomial
*pwqp
;
214 if (EVALUE_IS_ZERO(*e
)) {
215 dim
= isl_space_from_domain(dim
);
216 dim
= isl_space_add_dims(dim
, isl_dim_out
, 1);
217 return isl_pw_qpolynomial_zero(dim
);
220 if (value_notzero_p(e
->d
)) {
221 isl_set
*set
= isl_set_universe(isl_space_copy(dim
));
222 isl_qpolynomial
*qp
= isl_qpolynomial_rat_cst_on_domain(dim
, e
->x
.n
, e
->d
);
223 return isl_pw_qpolynomial_alloc(set
, qp
);
226 assert(!EVALUE_IS_NAN(*e
));
228 assert(e
->x
.p
->type
== partition
);
230 pw_space
= isl_space_copy(dim
);
231 pw_space
= isl_space_from_domain(pw_space
);
232 pw_space
= isl_space_add_dims(pw_space
, isl_dim_out
, 1);
233 pwqp
= isl_pw_qpolynomial_zero(pw_space
);
235 for (i
= 0; i
< e
->x
.p
->size
/2; ++i
) {
236 Polyhedron
*D
= EVALUE_DOMAIN(e
->x
.p
->arr
[2 * i
]);
237 isl_set
*set
= isl_set_new_from_polylib(D
, isl_space_copy(dim
));
238 isl_pw_qpolynomial
*pwqp_i
;
240 pwqp_i
= guarded_evalue2pwqp(set
, &e
->x
.p
->arr
[2 * i
+ 1]);
242 pwqp
= isl_pw_qpolynomial_add_disjoint(pwqp
, pwqp_i
);
253 static evalue
*evalue_pow(evalue
*e
, int exp
)
269 static evalue
*div2evalue(__isl_take isl_aff
*div
)
282 dim
= isl_aff_dim(div
, isl_dim_in
);
283 nparam
= isl_aff_dim(div
, isl_dim_param
);
285 ctx
= isl_aff_get_ctx(div
);
286 vec
= isl_vec_alloc(ctx
, 1 + dim
+ nparam
+ 1);
289 for (i
= 0; i
< dim
; ++i
)
290 isl_aff_get_coefficient(div
, isl_dim_in
, i
, &vec
->el
[1 + i
]);
291 for (i
= 0; i
< nparam
; ++i
)
292 isl_aff_get_coefficient(div
, isl_dim_param
, i
,
293 &vec
->el
[1 + dim
+ i
]);
294 isl_aff_get_denominator(div
, &vec
->el
[0]);
295 isl_aff_get_constant(div
, &vec
->el
[1 + dim
+ nparam
]);
297 e
= isl_alloc_type(ctx
, evalue
);
301 value_set_si(e
->d
, 0);
302 e
->x
.p
= new_enode(flooring
, 3, -1);
303 evalue_set_si(&e
->x
.p
->arr
[1], 0, 1);
304 evalue_set_si(&e
->x
.p
->arr
[2], 1, 1);
305 value_clear(e
->x
.p
->arr
[0].d
);
306 aff
= affine2evalue(vec
->el
+ 1, vec
->el
[0], dim
+ nparam
);
307 e
->x
.p
->arr
[0] = *aff
;
318 static int add_term(__isl_take isl_term
*term
, void *user
)
321 evalue
*sum
= (evalue
*)user
;
332 nparam
= isl_term_dim(term
, isl_dim_param
);
333 dim
= isl_term_dim(term
, isl_dim_set
);
334 n_div
= isl_term_dim(term
, isl_dim_div
);
336 ctx
= isl_term_get_ctx(term
);
337 e
= isl_alloc_type(ctx
, evalue
);
344 isl_term_get_num(term
, &n
);
345 isl_term_get_den(term
, &d
);
349 for (i
= 0; i
< dim
; ++i
) {
351 int exp
= isl_term_get_exp(term
, isl_dim_set
, i
);
356 pow
= evalue_pow(evalue_var(i
), exp
);
361 for (i
= 0; i
< nparam
; ++i
) {
363 int exp
= isl_term_get_exp(term
, isl_dim_param
, i
);
368 pow
= evalue_pow(evalue_var(dim
+ i
), exp
);
373 for (i
= 0; i
< n_div
; ++i
) {
377 int exp
= isl_term_get_exp(term
, isl_dim_div
, i
);
382 div
= isl_term_get_div(term
, i
);
383 floor
= div2evalue(div
);
384 pow
= evalue_pow(floor
, exp
);
403 evalue
*isl_qpolynomial_to_evalue(__isl_keep isl_qpolynomial
*qp
)
411 if (isl_qpolynomial_foreach_term(qp
, add_term
, e
) < 0)
420 static int add_guarded_qp(__isl_take isl_set
*set
, __isl_take isl_qpolynomial
*qp
,
426 evalue
*sum
= (evalue
*)user
;
429 e
= isl_alloc_type(isl_set_get_ctx(set
), evalue
);
433 D
= isl_set_to_polylib(set
);
437 f
= isl_qpolynomial_to_evalue(qp
);
443 dim
= isl_set_dim(set
, isl_dim_param
) + isl_set_dim(set
, isl_dim_set
);
445 e
->x
.p
= new_enode(partition
, 2, D
->Dimension
);
446 EVALUE_SET_DOMAIN(e
->x
.p
->arr
[0], D
);
448 value_clear(e
->x
.p
->arr
[1].d
);
456 isl_qpolynomial_free(qp
);
462 isl_qpolynomial_free(qp
);
466 evalue
*isl_pw_qpolynomial_to_evalue(__isl_keep isl_pw_qpolynomial
*pwqp
)
474 if (isl_pw_qpolynomial_foreach_piece(pwqp
, add_guarded_qp
, e
))