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_project_out(bset
, isl_dim_set
, 0, 1);
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 return isl_pw_qpolynomial_zero(dim
);
217 if (value_notzero_p(e
->d
)) {
218 isl_set
*set
= isl_set_universe(isl_space_copy(dim
));
219 isl_qpolynomial
*qp
= isl_qpolynomial_rat_cst_on_domain(dim
, e
->x
.n
, e
->d
);
220 return isl_pw_qpolynomial_alloc(set
, qp
);
223 assert(!EVALUE_IS_NAN(*e
));
225 assert(e
->x
.p
->type
== partition
);
227 pw_space
= isl_space_copy(dim
);
228 pw_space
= isl_space_from_domain(pw_space
);
229 pw_space
= isl_space_add_dims(pw_space
, isl_dim_out
, 1);
230 pwqp
= isl_pw_qpolynomial_zero(pw_space
);
232 for (i
= 0; i
< e
->x
.p
->size
/2; ++i
) {
233 Polyhedron
*D
= EVALUE_DOMAIN(e
->x
.p
->arr
[2 * i
]);
234 isl_set
*set
= isl_set_new_from_polylib(D
, isl_space_copy(dim
));
235 isl_pw_qpolynomial
*pwqp_i
;
237 pwqp_i
= guarded_evalue2pwqp(set
, &e
->x
.p
->arr
[2 * i
+ 1]);
239 pwqp
= isl_pw_qpolynomial_add_disjoint(pwqp
, pwqp_i
);
250 static evalue
*evalue_pow(evalue
*e
, int exp
)
266 static evalue
*div2evalue(__isl_take isl_aff
*div
)
279 dim
= isl_aff_dim(div
, isl_dim_in
);
280 nparam
= isl_aff_dim(div
, isl_dim_param
);
282 ctx
= isl_aff_get_ctx(div
);
283 vec
= isl_vec_alloc(ctx
, 1 + dim
+ nparam
+ 1);
286 for (i
= 0; i
< dim
; ++i
)
287 isl_aff_get_coefficient(div
, isl_dim_in
, i
, &vec
->el
[1 + i
]);
288 for (i
= 0; i
< nparam
; ++i
)
289 isl_aff_get_coefficient(div
, isl_dim_param
, i
,
290 &vec
->el
[1 + dim
+ i
]);
291 isl_aff_get_denominator(div
, &vec
->el
[0]);
292 isl_aff_get_constant(div
, &vec
->el
[1 + dim
+ nparam
]);
294 e
= isl_alloc_type(ctx
, evalue
);
298 value_set_si(e
->d
, 0);
299 e
->x
.p
= new_enode(flooring
, 3, -1);
300 evalue_set_si(&e
->x
.p
->arr
[1], 0, 1);
301 evalue_set_si(&e
->x
.p
->arr
[2], 1, 1);
302 value_clear(e
->x
.p
->arr
[0].d
);
303 aff
= affine2evalue(vec
->el
+ 1, vec
->el
[0], dim
+ nparam
);
304 e
->x
.p
->arr
[0] = *aff
;
315 static int add_term(__isl_take isl_term
*term
, void *user
)
318 evalue
*sum
= (evalue
*)user
;
329 nparam
= isl_term_dim(term
, isl_dim_param
);
330 dim
= isl_term_dim(term
, isl_dim_set
);
331 n_div
= isl_term_dim(term
, isl_dim_div
);
333 ctx
= isl_term_get_ctx(term
);
334 e
= isl_alloc_type(ctx
, evalue
);
341 isl_term_get_num(term
, &n
);
342 isl_term_get_den(term
, &d
);
346 for (i
= 0; i
< dim
; ++i
) {
348 int exp
= isl_term_get_exp(term
, isl_dim_set
, i
);
353 pow
= evalue_pow(evalue_var(i
), exp
);
358 for (i
= 0; i
< nparam
; ++i
) {
360 int exp
= isl_term_get_exp(term
, isl_dim_param
, i
);
365 pow
= evalue_pow(evalue_var(dim
+ i
), exp
);
370 for (i
= 0; i
< n_div
; ++i
) {
374 int exp
= isl_term_get_exp(term
, isl_dim_div
, i
);
379 div
= isl_term_get_div(term
, i
);
380 floor
= div2evalue(div
);
381 pow
= evalue_pow(floor
, exp
);
400 evalue
*isl_qpolynomial_to_evalue(__isl_keep isl_qpolynomial
*qp
)
408 if (isl_qpolynomial_foreach_term(qp
, add_term
, e
) < 0)
417 static int add_guarded_qp(__isl_take isl_set
*set
, __isl_take isl_qpolynomial
*qp
,
423 evalue
*sum
= (evalue
*)user
;
426 e
= isl_alloc_type(isl_set_get_ctx(set
), evalue
);
430 D
= isl_set_to_polylib(set
);
434 f
= isl_qpolynomial_to_evalue(qp
);
440 dim
= isl_set_dim(set
, isl_dim_param
) + isl_set_dim(set
, isl_dim_set
);
442 e
->x
.p
= new_enode(partition
, 2, D
->Dimension
);
443 EVALUE_SET_DOMAIN(e
->x
.p
->arr
[0], D
);
445 value_clear(e
->x
.p
->arr
[1].d
);
453 isl_qpolynomial_free(qp
);
459 isl_qpolynomial_free(qp
);
463 evalue
*isl_pw_qpolynomial_to_evalue(__isl_keep isl_pw_qpolynomial
*pwqp
)
471 if (isl_pw_qpolynomial_foreach_piece(pwqp
, add_guarded_qp
, e
))