1 #include <isl_set_polylib.h>
2 #include <isl_constraint.h>
3 #include <barvinok/evalue.h>
5 static __isl_give isl_qpolynomial
*extract_base(__isl_take isl_dim
*dim
,
12 isl_qpolynomial
*base
, *c
;
18 if (e
->x
.p
->type
== polynomial
)
19 return isl_qpolynomial_var(dim
, isl_dim_param
, e
->x
.p
->pos
- 1);
21 ctx
= isl_dim_get_ctx(dim
);
22 nparam
= isl_dim_size(dim
, isl_dim_param
);
23 v
= isl_vec_alloc(ctx
, 2 + nparam
);
27 isl_seq_clr(v
->el
+ 2, nparam
);
28 evalue_extract_affine(&e
->x
.p
->arr
[0], v
->el
+ 2, &v
->el
[1], &v
->el
[0]);
30 div
= isl_div_alloc(isl_dim_copy(dim
));
31 isl_div_set_constant(div
, v
->el
[1]);
32 isl_div_set_denominator(div
, v
->el
[0]);
34 for (i
= 0; i
< nparam
; ++i
)
35 isl_div_set_coefficient(div
, isl_dim_param
, i
, v
->el
[2 + i
]);
37 base
= isl_qpolynomial_div(div
);
39 if (e
->x
.p
->type
== fractional
) {
40 base
= isl_qpolynomial_neg(base
);
42 c
= isl_qpolynomial_rat_cst(isl_dim_copy(dim
), v
->el
[1], v
->el
[0]);
43 base
= isl_qpolynomial_add(base
, c
);
45 for (i
= 0; i
< nparam
; ++i
) {
47 c
= isl_qpolynomial_rat_cst(isl_dim_copy(dim
),
48 v
->el
[2 + i
], v
->el
[0]);
49 t
= isl_qpolynomial_var(isl_dim_copy(dim
),
51 t
= isl_qpolynomial_mul(c
, t
);
52 base
= isl_qpolynomial_add(base
, t
);
65 static int type_offset(enode
*p
)
67 return p
->type
== fractional
? 1 :
68 p
->type
== flooring
? 1 : 0;
71 __isl_give isl_qpolynomial
*isl_qpolynomial_from_evalue(__isl_take isl_dim
*dim
,
76 isl_qpolynomial
*base
;
79 if (EVALUE_IS_NAN(*e
))
80 return isl_qpolynomial_infty(dim
);
81 if (value_notzero_p(e
->d
))
82 return isl_qpolynomial_rat_cst(dim
, e
->x
.n
, e
->d
);
84 offset
= type_offset(e
->x
.p
);
86 assert(e
->x
.p
->type
== polynomial
||
87 e
->x
.p
->type
== flooring
||
88 e
->x
.p
->type
== fractional
);
89 assert(e
->x
.p
->size
>= 1 + offset
);
91 base
= extract_base(isl_dim_copy(dim
), e
);
92 qp
= isl_qpolynomial_from_evalue(isl_dim_copy(dim
),
93 &e
->x
.p
->arr
[e
->x
.p
->size
- 1]);
95 for (i
= e
->x
.p
->size
- 2; i
>= offset
; --i
) {
96 qp
= isl_qpolynomial_mul(qp
, isl_qpolynomial_copy(base
));
97 qp
= isl_qpolynomial_add(qp
,
98 isl_qpolynomial_from_evalue(isl_dim_copy(dim
),
102 isl_qpolynomial_free(base
);
108 static __isl_give isl_pw_qpolynomial
*guarded_evalue2pwqp(__isl_take isl_set
*set
,
111 static __isl_give isl_pw_qpolynomial
*relation2pwqp(__isl_take isl_set
*set
,
118 isl_pw_qpolynomial
*pwqp
;
119 struct isl_constraint
*c
;
120 struct isl_basic_set
*bset
;
121 struct isl_set
*guard
;
127 if (e
->x
.p
->size
== 1) {
128 dim
= isl_set_get_dim(set
);
130 return isl_pw_qpolynomial_zero(dim
);
133 isl_assert(set
->ctx
, e
->x
.p
->size
> 0, goto error
);
134 isl_assert(set
->ctx
, e
->x
.p
->size
<= 3, goto error
);
135 isl_assert(set
->ctx
, value_zero_p(e
->x
.p
->arr
[0].d
), goto error
);
136 isl_assert(set
->ctx
, e
->x
.p
->arr
[0].x
.p
->type
== fractional
, goto error
);
137 fract
= &e
->x
.p
->arr
[0];
139 nparam
= isl_set_dim(set
, isl_dim_param
);
140 vec
= isl_vec_alloc(set
->ctx
, 2 + nparam
+ 1);
144 isl_seq_clr(vec
->el
+ 2, nparam
);
145 evalue_extract_affine(&fract
->x
.p
->arr
[0],
146 vec
->el
+ 2, &vec
->el
[1], &vec
->el
[0]);
148 dim
= isl_set_get_dim(set
);
149 dim
= isl_dim_add(dim
, isl_dim_set
, 1);
151 bset
= isl_basic_set_universe(dim
);
152 c
= isl_equality_alloc(isl_dim_copy(dim
));
153 isl_int_neg(vec
->el
[0], vec
->el
[0]);
154 isl_constraint_set_coefficient(c
, isl_dim_set
, 0, vec
->el
[0]);
155 isl_constraint_set_constant(c
, vec
->el
[1]);
156 for (i
= 0; i
< nparam
; ++i
)
157 isl_constraint_set_coefficient(c
, isl_dim_param
, i
, vec
->el
[2+i
]);
158 bset
= isl_basic_set_add_constraint(bset
, c
);
159 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 0, 1);
160 guard
= isl_set_from_basic_set(bset
);
163 pwqp
= guarded_evalue2pwqp(isl_set_intersect(isl_set_copy(set
),
164 isl_set_copy(guard
)),
167 if (e
->x
.p
->size
== 3) {
168 isl_pw_qpolynomial
*pwqpc
;
169 guard
= isl_set_complement(guard
);
170 pwqpc
= guarded_evalue2pwqp(isl_set_intersect(isl_set_copy(set
),
171 isl_set_copy(guard
)),
173 pwqp
= isl_pw_qpolynomial_add_disjoint(pwqp
, pwqpc
);
185 static __isl_give isl_pw_qpolynomial
*guarded_evalue2pwqp(__isl_take isl_set
*set
,
190 if (value_zero_p(e
->d
) && e
->x
.p
->type
== relation
)
191 return relation2pwqp(set
, e
);
193 qp
= isl_qpolynomial_from_evalue(isl_set_get_dim(set
), e
);
195 return isl_pw_qpolynomial_alloc(set
, qp
);
198 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_from_evalue(__isl_take isl_dim
*dim
, const evalue
*e
)
201 isl_pw_qpolynomial
*pwqp
;
205 if (EVALUE_IS_ZERO(*e
))
206 return isl_pw_qpolynomial_zero(dim
);
208 if (value_notzero_p(e
->d
)) {
209 isl_set
*set
= isl_set_universe(isl_dim_copy(dim
));
210 isl_qpolynomial
*qp
= isl_qpolynomial_rat_cst(dim
, e
->x
.n
, e
->d
);
211 return isl_pw_qpolynomial_alloc(set
, qp
);
214 assert(!EVALUE_IS_NAN(*e
));
216 assert(e
->x
.p
->type
== partition
);
218 pwqp
= isl_pw_qpolynomial_zero(isl_dim_copy(dim
));
220 for (i
= 0; i
< e
->x
.p
->size
/2; ++i
) {
221 Polyhedron
*D
= EVALUE_DOMAIN(e
->x
.p
->arr
[2 * i
]);
222 isl_set
*set
= isl_set_new_from_polylib(D
, isl_dim_copy(dim
));
223 isl_pw_qpolynomial
*pwqp_i
;
225 pwqp_i
= guarded_evalue2pwqp(set
, &e
->x
.p
->arr
[2 * i
+ 1]);
227 pwqp
= isl_pw_qpolynomial_add_disjoint(pwqp
, pwqp_i
);
238 static evalue
*evalue_pow(evalue
*e
, int exp
)
254 static evalue
*div2evalue(__isl_take isl_div
*div
)
266 dim
= isl_div_dim(div
, isl_dim_set
);
267 nparam
= isl_div_dim(div
, isl_dim_param
);
269 vec
= isl_vec_alloc(div
->ctx
, 1 + dim
+ nparam
+ 1);
272 for (i
= 0; i
< dim
; ++i
)
273 isl_div_get_coefficient(div
, isl_dim_set
, i
, &vec
->el
[1 + i
]);
274 for (i
= 0; i
< nparam
; ++i
)
275 isl_div_get_coefficient(div
, isl_dim_param
, i
,
276 &vec
->el
[1 + dim
+ i
]);
277 isl_div_get_denominator(div
, &vec
->el
[0]);
278 isl_div_get_constant(div
, &vec
->el
[1 + dim
+ nparam
]);
280 e
= isl_alloc_type(div
->ctx
, evalue
);
284 value_set_si(e
->d
, 0);
285 e
->x
.p
= new_enode(flooring
, 3, -1);
286 evalue_set_si(&e
->x
.p
->arr
[1], 0, 1);
287 evalue_set_si(&e
->x
.p
->arr
[2], 1, 1);
288 value_clear(e
->x
.p
->arr
[0].d
);
289 aff
= affine2evalue(vec
->el
+ 1, vec
->el
[0], dim
+ nparam
);
290 e
->x
.p
->arr
[0] = *aff
;
301 static int add_term(__isl_take isl_term
*term
, void *user
)
304 evalue
*sum
= (evalue
*)user
;
315 nparam
= isl_term_dim(term
, isl_dim_param
);
316 dim
= isl_term_dim(term
, isl_dim_set
);
317 n_div
= isl_term_dim(term
, isl_dim_div
);
319 ctx
= isl_term_get_ctx(term
);
320 e
= isl_alloc_type(ctx
, evalue
);
327 isl_term_get_num(term
, &n
);
328 isl_term_get_den(term
, &d
);
332 for (i
= 0; i
< dim
; ++i
) {
334 int exp
= isl_term_get_exp(term
, isl_dim_set
, i
);
339 pow
= evalue_pow(evalue_var(i
), exp
);
344 for (i
= 0; i
< nparam
; ++i
) {
346 int exp
= isl_term_get_exp(term
, isl_dim_param
, i
);
351 pow
= evalue_pow(evalue_var(dim
+ i
), exp
);
356 for (i
= 0; i
< n_div
; ++i
) {
360 int exp
= isl_term_get_exp(term
, isl_dim_div
, i
);
365 div
= isl_term_get_div(term
, i
);
366 floor
= div2evalue(div
);
367 pow
= evalue_pow(floor
, exp
);
386 evalue
*isl_qpolynomial_to_evalue(__isl_keep isl_qpolynomial
*qp
)
394 if (isl_qpolynomial_foreach_term(qp
, add_term
, e
) < 0)
403 static int add_guarded_qp(__isl_take isl_set
*set
, __isl_take isl_qpolynomial
*qp
,
409 evalue
*sum
= (evalue
*)user
;
412 e
= isl_alloc_type(set
->ctx
, evalue
);
416 D
= isl_set_to_polylib(set
);
420 f
= isl_qpolynomial_to_evalue(qp
);
426 dim
= isl_set_dim(set
, isl_dim_param
) + isl_set_dim(set
, isl_dim_set
);
428 e
->x
.p
= new_enode(partition
, 2, D
->Dimension
);
429 EVALUE_SET_DOMAIN(e
->x
.p
->arr
[0], D
);
431 value_clear(e
->x
.p
->arr
[1].d
);
439 isl_qpolynomial_free(qp
);
445 isl_qpolynomial_free(qp
);
449 evalue
*isl_pw_qpolynomial_to_evalue(__isl_keep isl_pw_qpolynomial
*pwqp
)
457 if (isl_pw_qpolynomial_foreach_piece(pwqp
, add_guarded_qp
, e
))