3 #include <bernstein/bernstein.h>
4 #include <bernstein/piecewise_lst.h>
5 #include <isl_set_polylib.h>
6 #include <barvinok/barvinok.h>
7 #include <barvinok/util.h>
8 #include <barvinok/bernstein.h>
9 #include <barvinok/options.h>
10 #include "reduce_domain.h"
12 using namespace GiNaC
;
13 using namespace bernstein
;
14 using namespace barvinok
;
23 ex
evalue2ex(evalue
*e
, const exvector
& vars
)
25 if (value_pos_p(e
->d
))
26 return value2numeric(e
->x
.n
)/value2numeric(e
->d
);
27 if (EVALUE_IS_NAN(*e
))
29 if (e
->x
.p
->type
!= polynomial
)
32 for (int i
= e
->x
.p
->size
-1; i
>= 0; --i
) {
33 poly
*= vars
[e
->x
.p
->pos
-1];
34 ex t
= evalue2ex(&e
->x
.p
->arr
[i
], vars
);
35 if (is_exactly_a
<fail
>(t
))
42 static int type_offset(enode
*p
)
44 return p
->type
== fractional
? 1 :
45 p
->type
== flooring
? 1 : 0;
48 typedef pair
<bool, const evalue
*> typed_evalue
;
50 static ex
evalue2ex_add_var(evalue
*e
, exvector
& extravar
,
51 vector
<typed_evalue
>& expr
, bool is_fract
)
55 for (int i
= 0; i
< expr
.size(); ++i
) {
56 if (is_fract
== expr
[i
].first
&& eequal(e
, expr
[i
].second
)) {
57 base_var
= extravar
[i
];
65 snprintf(name
, sizeof(name
), "f%c%zd", is_fract
? 'r' : 'l', expr
.size());
66 extravar
.push_back(base_var
= symbol(name
));
67 expr
.push_back(typed_evalue(is_fract
, e
));
72 /* For the argument e=(f/d) of a fractional, return (d-1)/d times
73 * a variable in [0,1] (see setup_constraints).
75 static ex
evalue2ex_get_fract(evalue
*e
, exvector
& extravar
,
76 vector
<typed_evalue
>& expr
)
84 den
= value2numeric(d
);
88 ex base_var
= evalue2ex_add_var(e
, extravar
, expr
, true);
93 static ex
evalue2ex_r(const evalue
*e
, const exvector
& vars
,
94 exvector
& extravar
, vector
<typed_evalue
>& expr
,
97 if (value_notzero_p(e
->d
))
98 return value2numeric(e
->x
.n
)/value2numeric(e
->d
);
103 switch (e
->x
.p
->type
) {
105 base_var
= vars
[e
->x
.p
->pos
-1];
108 base_var
= evalue2ex_add_var(&e
->x
.p
->arr
[0], extravar
, expr
, false);
111 base_var
= evalue2ex_get_fract(&e
->x
.p
->arr
[0], extravar
, expr
);
115 rem
= VALUE_TO_INT(coset
->p
[e
->x
.p
->pos
-1]) % e
->x
.p
->size
;
116 return evalue2ex_r(&e
->x
.p
->arr
[rem
], vars
, extravar
, expr
, coset
);
121 int offset
= type_offset(e
->x
.p
);
122 for (int i
= e
->x
.p
->size
-1; i
>= offset
; --i
) {
124 ex t
= evalue2ex_r(&e
->x
.p
->arr
[i
], vars
, extravar
, expr
, coset
);
125 if (is_exactly_a
<fail
>(t
))
132 /* For each t = floor(e/d), set up two constraints
135 * -e + d t + d-1 >= 0
137 * e is assumed to be an affine expression.
139 * For each t = fract(e/d), set up two constraints
144 static Matrix
*setup_constraints(const vector
<typed_evalue
> expr
, int nvar
)
146 int extra
= expr
.size();
149 Matrix
*M
= Matrix_Alloc(2*extra
, 1+extra
+nvar
+1);
150 for (int i
= 0; i
< extra
; ++i
) {
152 value_set_si(M
->p
[2*i
][0], 1);
153 value_set_si(M
->p
[2*i
][1+i
], -1);
154 value_set_si(M
->p
[2*i
][1+extra
+nvar
], 1);
155 value_set_si(M
->p
[2*i
+1][0], 1);
156 value_set_si(M
->p
[2*i
+1][1+i
], 1);
158 Value
*d
= &M
->p
[2*i
][1+i
];
159 evalue_extract_affine(expr
[i
].second
, M
->p
[2*i
]+1+extra
,
160 M
->p
[2*i
]+1+extra
+nvar
, d
);
161 value_oppose(*d
, *d
);
162 value_set_si(M
->p
[2*i
][0], -1);
163 Vector_Scale(M
->p
[2*i
], M
->p
[2*i
+1], M
->p
[2*i
][0], 1+extra
+nvar
+1);
164 value_set_si(M
->p
[2*i
][0], 1);
165 value_subtract(M
->p
[2*i
+1][1+extra
+nvar
], M
->p
[2*i
+1][1+extra
+nvar
], *d
);
166 value_decrement(M
->p
[2*i
+1][1+extra
+nvar
], M
->p
[2*i
+1][1+extra
+nvar
]);
172 static bool evalue_is_periodic(const evalue
*e
, Vector
*periods
)
175 bool is_periodic
= false;
177 if (value_notzero_p(e
->d
))
180 assert(e
->x
.p
->type
!= partition
);
181 if (e
->x
.p
->type
== periodic
) {
184 value_set_si(size
, e
->x
.p
->size
);
185 value_lcm(periods
->p
[e
->x
.p
->pos
-1], periods
->p
[e
->x
.p
->pos
-1], size
);
189 offset
= type_offset(e
->x
.p
);
190 for (i
= e
->x
.p
->size
-1; i
>= offset
; --i
)
191 is_periodic
= evalue_is_periodic(&e
->x
.p
->arr
[i
], periods
) || is_periodic
;
195 static ex
evalue2lst(const evalue
*e
, const exvector
& vars
,
196 exvector
& extravar
, vector
<typed_evalue
>& expr
,
199 Vector
*coset
= Vector_Alloc(periods
->Size
);
203 list
.append(evalue2ex_r(e
, vars
, extravar
, expr
, coset
));
204 for (i
= coset
->Size
-1; i
>= 0; --i
) {
205 value_increment(coset
->p
[i
], coset
->p
[i
]);
206 if (value_lt(coset
->p
[i
], periods
->p
[i
]))
208 value_set_si(coset
->p
[i
], 0);
217 ex
evalue2ex(const evalue
*e
, const exvector
& vars
, exvector
& floorvar
,
218 Matrix
**C
, Vector
**p
)
220 vector
<typed_evalue
> expr
;
221 Vector
*periods
= Vector_Alloc(vars
.size());
224 for (int i
= 0; i
< periods
->Size
; ++i
)
225 value_set_si(periods
->p
[i
], 1);
226 if (evalue_is_periodic(e
, periods
)) {
232 Vector_Free(periods
);
234 ex poly
= evalue2ex_r(e
, vars
, floorvar
, expr
, NULL
);
235 Matrix
*M
= setup_constraints(expr
, vars
.size());
241 /* if the evalue is a relation, we use the relation to cut off the
242 * the edges of the domain
244 static Polyhedron
*relation_domain(Polyhedron
*D
, evalue
*fr
, unsigned MaxRays
)
246 assert(value_zero_p(fr
->d
));
247 assert(fr
->x
.p
->type
== fractional
);
248 assert(fr
->x
.p
->size
== 3);
249 Matrix
*T
= Matrix_Alloc(2, D
->Dimension
+1);
250 value_set_si(T
->p
[1][D
->Dimension
], 1);
252 /* convert argument of fractional to polylib */
253 /* the argument is assumed to be linear */
254 evalue
*p
= &fr
->x
.p
->arr
[0];
255 evalue_denom(p
, &T
->p
[1][D
->Dimension
]);
256 for (;value_zero_p(p
->d
); p
= &p
->x
.p
->arr
[0]) {
257 assert(p
->x
.p
->type
== polynomial
);
258 assert(p
->x
.p
->size
== 2);
259 assert(value_notzero_p(p
->x
.p
->arr
[1].d
));
260 int pos
= p
->x
.p
->pos
- 1;
261 value_assign(T
->p
[0][pos
], p
->x
.p
->arr
[1].x
.n
);
262 value_multiply(T
->p
[0][pos
], T
->p
[0][pos
], T
->p
[1][D
->Dimension
]);
263 value_division(T
->p
[0][pos
], T
->p
[0][pos
], p
->x
.p
->arr
[1].d
);
265 int pos
= D
->Dimension
;
266 value_assign(T
->p
[0][pos
], p
->x
.n
);
267 value_multiply(T
->p
[0][pos
], T
->p
[0][pos
], T
->p
[1][D
->Dimension
]);
268 value_division(T
->p
[0][pos
], T
->p
[0][pos
], p
->d
);
270 Polyhedron
*E
= NULL
;
271 for (Polyhedron
*P
= D
; P
; P
= P
->next
) {
272 Polyhedron
*I
= Polyhedron_Image(P
, T
, MaxRays
);
273 I
= DomainConstraintSimplify(I
, MaxRays
);
274 Polyhedron
*R
= Polyhedron_Preimage(I
, T
, MaxRays
);
276 Polyhedron
*next
= P
->next
;
278 Polyhedron
*S
= DomainIntersection(P
, R
, MaxRays
);
284 E
= DomainConcat(S
, E
);
291 piecewise_lst
*evalue_bernstein_coefficients(piecewise_lst
*pl_all
, evalue
*e
,
292 Polyhedron
*ctx
, const exvector
& params
)
295 barvinok_options
*options
= barvinok_options_new_with_defaults();
296 pl
= evalue_bernstein_coefficients(pl_all
, e
, ctx
, params
, options
);
297 barvinok_options_free(options
);
301 static piecewise_lst
*bernstein_coefficients(piecewise_lst
*pl_all
,
302 Polyhedron
*D
, const ex
& poly
,
304 const exvector
& params
, const exvector
& floorvar
,
305 barvinok_options
*options
);
307 /* Recursively apply Bernstein expansion on P, optimizing over dims[i]
308 * variables in each level. The context ctx is assumed to have been adapted
309 * to the first level in the recursion.
311 static piecewise_lst
*bernstein_coefficients_recursive(piecewise_lst
*pl_all
,
312 Polyhedron
*P
, const vector
<int>& dims
, const ex
& poly
,
314 const exvector
& params
, const exvector
& vars
,
315 barvinok_options
*options
)
317 assert(dims
.size() > 0);
318 assert(ctx
->Dimension
== P
->Dimension
- dims
[0]);
321 for (int j
= 0; j
< dims
.size(); ++j
) {
323 pl_vars
.insert(pl_vars
.end(), vars
.begin()+done
, vars
.begin()+done
+dims
[j
]);
325 pl_params
.insert(pl_params
.end(), vars
.begin()+done
+dims
[j
], vars
.end());
326 pl_params
.insert(pl_params
.end(), params
.begin(), params
.end());
329 pl
= bernstein_coefficients(NULL
, P
, poly
, ctx
,
330 pl_params
, pl_vars
, options
);
332 piecewise_lst
*new_pl
= NULL
;
333 Polyhedron
*U
= Universe_Polyhedron(pl_params
.size());
335 for (int i
= 0; i
< pl
->list
.size(); ++i
) {
336 Polyhedron
*D
= pl
->list
[i
].first
;
337 lst polys
= pl
->list
[i
].second
;
338 new_pl
= bernstein_coefficients(new_pl
, D
, polys
, U
, pl_params
,
357 pl_all
->combine(*pl
);
364 static piecewise_lst
*bernstein_coefficients_full_recurse(piecewise_lst
*pl_all
,
365 Polyhedron
*P
, const ex
& poly
,
367 const exvector
& params
, const exvector
& vars
,
368 barvinok_options
*options
)
370 Polyhedron
*CR
= align_context(ctx
, P
->Dimension
-1, options
->MaxRays
);
371 vector
<int> dims(vars
.size());
372 for (int i
= 0; i
< dims
.size(); ++i
)
374 pl_all
= bernstein_coefficients_recursive(pl_all
, P
, dims
, poly
, CR
,
375 params
, vars
, options
);
381 static piecewise_lst
*bernstein_coefficients_product(piecewise_lst
*pl_all
,
382 Polyhedron
*F
, Matrix
*T
, const ex
& poly
,
384 const exvector
& params
, const exvector
& vars
,
385 barvinok_options
*options
)
389 for (Polyhedron
*G
= F
; G
; G
= G
->next
)
393 unsigned nparam
= params
.size();
394 unsigned nvar
= vars
.size();
395 unsigned constraints
;
397 Polyhedron
*C
= NULL
;
399 /* More context constraints */
400 if (F
->Dimension
== ctx
->Dimension
) {
410 M
= Matrix_Alloc(F
->NbConstraints
, 1+nvar
+nparam
+1);
411 for (int i
= 0; i
< F
->NbConstraints
; ++i
) {
412 Vector_Copy(F
->Constraint
[i
], M
->p
[i
], 1+F
->Dimension
-nparam
);
413 Vector_Copy(F
->Constraint
[i
]+1+F
->Dimension
-nparam
,
414 M
->p
[i
]+1+nvar
, nparam
+1);
416 P
= Constraints2Polyhedron(M
, options
->MaxRays
);
420 constraints
= C
? C
->NbConstraints
: 0;
421 constraints
+= ctx
->NbConstraints
;
422 for (Polyhedron
*G
= F
->next
; G
; G
= G
->next
) {
423 constraints
+= G
->NbConstraints
;
427 unsigned total_var
= nvar
-(F
->Dimension
-nparam
);
430 M
= Matrix_Alloc(constraints
, 1+total_var
+nparam
+1);
431 for (Polyhedron
*G
= F
->next
; G
; G
= G
->next
) {
432 unsigned this_var
= G
->Dimension
- nparam
;
433 for (int i
= 0; i
< G
->NbConstraints
; ++i
) {
434 value_assign(M
->p
[c
+i
][0], G
->Constraint
[i
][0]);
435 Vector_Copy(G
->Constraint
[i
]+1, M
->p
[c
+i
]+1+skip
, this_var
);
436 Vector_Copy(G
->Constraint
[i
]+1+this_var
, M
->p
[c
+i
]+1+total_var
,
439 c
+= G
->NbConstraints
;
442 assert(skip
== total_var
);
444 for (int i
= 0; i
< C
->NbConstraints
; ++i
) {
445 value_assign(M
->p
[c
+i
][0], C
->Constraint
[i
][0]);
446 Vector_Copy(C
->Constraint
[i
]+1, M
->p
[c
+i
]+1+total_var
,
449 c
+= C
->NbConstraints
;
451 for (int i
= 0; i
< ctx
->NbConstraints
; ++i
) {
452 value_assign(M
->p
[c
+i
][0], ctx
->Constraint
[i
][0]);
453 Vector_Copy(ctx
->Constraint
[i
]+1, M
->p
[c
+i
]+1+total_var
, nparam
+1);
455 PC
= Constraints2Polyhedron(M
, options
->MaxRays
);
458 exvector newvars
= constructVariableVector(nvar
, "t");
459 matrix
subs(1, nvar
);
460 for (int i
= 0; i
< nvar
; ++i
)
461 for (int j
= 0; j
< nvar
; ++j
)
462 subs(0,i
) += value2numeric(T
->p
[i
][j
]) * newvars
[j
];
464 ex newpoly
= replaceVariablesInPolynomial(poly
, vars
, subs
);
466 vector
<int> dims(factors
);
467 for (int i
= 0; F
; ++i
, F
= F
->next
)
468 dims
[i
] = F
->Dimension
-nparam
;
470 pl_all
= bernstein_coefficients_recursive(pl_all
, P
, dims
, newpoly
, PC
,
471 params
, newvars
, options
);
479 static piecewise_lst
*bernstein_coefficients_polyhedron(piecewise_lst
*pl_all
,
480 Polyhedron
*P
, const ex
& poly
,
482 const exvector
& params
, const exvector
& floorvar
,
483 barvinok_options
*options
)
485 if (Polyhedron_is_unbounded(P
, ctx
->Dimension
, options
->MaxRays
)) {
486 fprintf(stderr
, "warning: unbounded domain skipped\n");
487 Polyhedron_Print(stderr
, P_VALUE_FMT
, P
);
491 if (options
->bernstein_recurse
& BV_BERNSTEIN_FACTORS
) {
493 Polyhedron
*F
= Polyhedron_Factor(P
, ctx
->Dimension
, &T
, options
->MaxRays
);
495 pl_all
= bernstein_coefficients_product(pl_all
, F
, T
, poly
, ctx
, params
,
502 if (floorvar
.size() > 1 &&
503 options
->bernstein_recurse
& BV_BERNSTEIN_INTERVALS
)
504 return bernstein_coefficients_full_recurse(pl_all
, P
, poly
, ctx
,
505 params
, floorvar
, options
);
507 unsigned PP_MaxRays
= options
->MaxRays
;
508 if (PP_MaxRays
& POL_NO_DUAL
)
511 Param_Polyhedron
*PP
= Polyhedron2Param_Domain(P
, ctx
, PP_MaxRays
);
514 piecewise_lst
*pl
= new piecewise_lst(params
, options
->bernstein_optimize
);
517 Polyhedron
*TC
= true_context(P
, ctx
, options
->MaxRays
);
518 FORALL_REDUCED_DOMAIN(PP
, TC
, nd
, options
, i
, PD
, rVD
)
519 matrix VM
= domainVertices(PP
, PD
, params
);
520 lst coeffs
= bernsteinExpansion(VM
, poly
, floorvar
, params
);
521 pl
->add_guarded_lst(rVD
, coeffs
);
522 END_FORALL_REDUCED_DOMAIN
525 Param_Polyhedron_Free(PP
);
529 pl_all
->combine(*pl
);
536 static piecewise_lst
*bernstein_coefficients(piecewise_lst
*pl_all
,
537 Polyhedron
*D
, const ex
& poly
,
539 const exvector
& params
, const exvector
& floorvar
,
540 barvinok_options
*options
)
542 if (!D
->next
&& emptyQ2(D
))
545 for (Polyhedron
*P
= D
; P
; P
= P
->next
) {
546 /* This shouldn't happen */
549 Polyhedron
*next
= P
->next
;
551 pl_all
= bernstein_coefficients_polyhedron(pl_all
, P
, poly
, ctx
,
552 params
, floorvar
, options
);
558 /* Compute the coefficients of the polynomial corresponding to each coset
559 * on its own domain. This allows us to cut the domain on multiples of
561 * To perform the cutting for a coset "i mod n = c" we map the domain
562 * to the quotient space trough "i = i' n + c", simplify the constraints
563 * (implicitly) and then map back to the original space.
565 static piecewise_lst
*bernstein_coefficients_periodic(piecewise_lst
*pl_all
,
566 Polyhedron
*D
, const evalue
*e
,
567 Polyhedron
*ctx
, const exvector
& vars
,
568 const exvector
& params
, Vector
*periods
,
569 barvinok_options
*options
)
571 assert(D
->Dimension
== periods
->Size
);
572 Matrix
*T
= Matrix_Alloc(D
->Dimension
+1, D
->Dimension
+1);
573 Matrix
*T2
= Matrix_Alloc(D
->Dimension
+1, D
->Dimension
+1);
574 Vector
*coset
= Vector_Alloc(periods
->Size
);
576 vector
<typed_evalue
> expr
;
577 exvector allvars
= vars
;
578 allvars
.insert(allvars
.end(), params
.begin(), params
.end());
580 value_set_si(T2
->p
[D
->Dimension
][D
->Dimension
], 1);
581 for (int i
= 0; i
< D
->Dimension
; ++i
) {
582 value_assign(T
->p
[i
][i
], periods
->p
[i
]);
583 value_lcm(T2
->p
[D
->Dimension
][D
->Dimension
],
584 T2
->p
[D
->Dimension
][D
->Dimension
], periods
->p
[i
]);
586 value_set_si(T
->p
[D
->Dimension
][D
->Dimension
], 1);
587 for (int i
= 0; i
< D
->Dimension
; ++i
)
588 value_division(T2
->p
[i
][i
], T2
->p
[D
->Dimension
][D
->Dimension
],
592 ex poly
= evalue2ex_r(e
, allvars
, extravar
, expr
, coset
);
593 assert(extravar
.size() == 0);
594 assert(expr
.size() == 0);
595 Polyhedron
*E
= DomainPreimage(D
, T
, options
->MaxRays
);
596 Polyhedron
*F
= DomainPreimage(E
, T2
, options
->MaxRays
);
599 pl_all
= bernstein_coefficients(pl_all
, F
, poly
, ctx
, params
,
602 for (i
= D
->Dimension
-1; i
>= 0; --i
) {
603 value_increment(coset
->p
[i
], coset
->p
[i
]);
604 value_increment(T
->p
[i
][D
->Dimension
], T
->p
[i
][D
->Dimension
]);
605 value_subtract(T2
->p
[i
][D
->Dimension
], T2
->p
[i
][D
->Dimension
],
607 if (value_lt(coset
->p
[i
], periods
->p
[i
]))
609 value_set_si(coset
->p
[i
], 0);
610 value_set_si(T
->p
[i
][D
->Dimension
], 0);
611 value_set_si(T2
->p
[i
][D
->Dimension
], 0);
622 piecewise_lst
*bernstein_coefficients_relation(piecewise_lst
*pl_all
,
623 Polyhedron
*D
, evalue
*EP
, Polyhedron
*ctx
,
624 const exvector
& allvars
, const exvector
& vars
,
625 const exvector
& params
, barvinok_options
*options
)
627 if (value_zero_p(EP
->d
) && EP
->x
.p
->type
== relation
) {
628 Polyhedron
*E
= relation_domain(D
, &EP
->x
.p
->arr
[0], options
->MaxRays
);
630 pl_all
= bernstein_coefficients_relation(pl_all
, E
, &EP
->x
.p
->arr
[1],
631 ctx
, allvars
, vars
, params
,
635 /* In principle, we could cut off the edges of this domain too */
636 if (EP
->x
.p
->size
> 2)
637 pl_all
= bernstein_coefficients_relation(pl_all
, D
, &EP
->x
.p
->arr
[2],
638 ctx
, allvars
, vars
, params
,
646 ex poly
= evalue2ex(EP
, allvars
, floorvar
, &M
, &periods
);
647 floorvar
.insert(floorvar
.end(), vars
.begin(), vars
.end());
650 Polyhedron
*AE
= align_context(D
, M
->NbColumns
-2, options
->MaxRays
);
651 E
= DomainAddConstraints(AE
, M
, options
->MaxRays
);
655 if (is_exactly_a
<fail
>(poly
)) {
660 pl_all
= bernstein_coefficients_periodic(pl_all
, E
, EP
, ctx
, vars
,
661 params
, periods
, options
);
663 pl_all
= bernstein_coefficients(pl_all
, E
, poly
, ctx
, params
,
666 Vector_Free(periods
);
673 piecewise_lst
*evalue_bernstein_coefficients(piecewise_lst
*pl_all
, evalue
*e
,
674 Polyhedron
*ctx
, const exvector
& params
,
675 barvinok_options
*options
)
677 unsigned nparam
= ctx
->Dimension
;
678 if (EVALUE_IS_ZERO(*e
))
680 assert(value_zero_p(e
->d
));
681 assert(e
->x
.p
->type
== partition
);
682 assert(e
->x
.p
->size
>= 2);
683 unsigned nvars
= EVALUE_DOMAIN(e
->x
.p
->arr
[0])->Dimension
- nparam
;
685 exvector vars
= constructVariableVector(nvars
, "v");
686 exvector allvars
= vars
;
687 allvars
.insert(allvars
.end(), params
.begin(), params
.end());
689 for (int i
= 0; i
< e
->x
.p
->size
/2; ++i
) {
690 pl_all
= bernstein_coefficients_relation(pl_all
,
691 EVALUE_DOMAIN(e
->x
.p
->arr
[2*i
]), &e
->x
.p
->arr
[2*i
+1],
692 ctx
, allvars
, vars
, params
, options
);
697 static __isl_give isl_qpolynomial
*qp_from_ex(__isl_take isl_dim
*dim
,
698 const GiNaC::ex ex
, const GiNaC::exvector
¶ms
, int i
)
701 isl_qpolynomial
*base
;
706 return isl_qpolynomial_nan(dim
);
708 if (is_a
<numeric
>(ex
)) {
709 numeric r
= ex_to
<numeric
>(ex
);
714 numeric2value(r
.numer(), n
);
715 numeric2value(r
.denom(), d
);
716 qp
= isl_qpolynomial_rat_cst(dim
, n
, d
);
722 deg
= ex
.degree(params
[i
]);
724 return qp_from_ex(dim
, ex
, params
, i
+ 1);
726 base
= isl_qpolynomial_var(isl_dim_copy(dim
), isl_dim_param
, i
);
727 qp
= qp_from_ex(isl_dim_copy(dim
), ex
.coeff(params
[i
], deg
),
730 for (j
= deg
- 1; j
>= 0; --j
) {
731 qp
= isl_qpolynomial_mul(qp
, isl_qpolynomial_copy(base
));
732 qp
= isl_qpolynomial_add(qp
,
733 qp_from_ex(isl_dim_copy(dim
), ex
.coeff(params
[i
], j
),
737 isl_qpolynomial_free(base
);
743 __isl_give isl_qpolynomial
*isl_qpolynomial_from_ginac(__isl_take isl_dim
*dim
,
744 const GiNaC::ex
&ex
, const GiNaC::exvector
¶ms
)
752 return qp_from_ex(dim
, exp
, params
, 0);
758 __isl_give isl_qpolynomial_fold
*isl_qpolynomial_fold_from_ginac(
759 __isl_take isl_dim
*dim
, enum isl_fold type
, const GiNaC::lst
&lst
,
760 const GiNaC::exvector
¶ms
)
762 isl_qpolynomial_fold
*fold
;
763 lst::const_iterator j
;
765 fold
= isl_qpolynomial_fold_empty(type
, isl_dim_copy(dim
));
766 for (j
= lst
.begin(); j
!= lst
.end(); ++j
) {
768 isl_qpolynomial_fold
*fold_i
;
770 qp
= isl_qpolynomial_from_ginac(isl_dim_copy(dim
), *j
, params
);
771 fold_i
= isl_qpolynomial_fold_alloc(type
, qp
);
772 fold
= isl_qpolynomial_fold_fold(fold
, fold_i
);
778 __isl_give isl_pw_qpolynomial_fold
*isl_pw_qpolynomial_fold_from_ginac(
779 __isl_take isl_dim
*dim
, bernstein::piecewise_lst
*pl
,
780 const GiNaC::exvector
¶ms
)
783 isl_pw_qpolynomial_fold
*pwf
;
785 pwf
= isl_pw_qpolynomial_fold_zero(isl_dim_copy(dim
));
787 type
= pl
->sign
> 0 ? isl_fold_max
788 : pl
->sign
< 0 ? isl_fold_min
: isl_fold_list
;
790 for (int i
= 0; i
< pl
->list
.size(); ++i
) {
791 isl_pw_qpolynomial_fold
*pwf_i
;
792 isl_qpolynomial_fold
*fold
;
795 set
= isl_set_new_from_polylib(pl
->list
[i
].first
,
797 fold
= isl_qpolynomial_fold_from_ginac(isl_dim_copy(dim
),
798 type
, pl
->list
[i
].second
, params
);
799 pwf_i
= isl_pw_qpolynomial_fold_alloc(set
, fold
);
800 pwf
= isl_pw_qpolynomial_fold_add_disjoint(pwf
, pwf_i
);
818 static int guarded_qp_bernstein_coefficients(__isl_take isl_set
*set
,
819 __isl_take isl_qpolynomial
*qp
, void *user
)
821 struct isl_bound
*bound
= (struct isl_bound
*)user
;
824 struct barvinok_options
*options
;
827 options
= barvinok_options_new_with_defaults();
832 nvar
= isl_set_dim(set
, isl_dim_set
);
834 e
= isl_qpolynomial_to_evalue(qp
);
838 set
= isl_set_make_disjoint(set
);
839 D
= isl_set_to_polylib(set
);
841 bound
->vars
= constructVariableVector(nvar
, "v");
842 bound
->allvars
= bound
->vars
;
843 bound
->allvars
.insert(bound
->allvars
.end(),
844 bound
->params
.begin(), bound
->params
.end());
846 bound
->pl
= bernstein_coefficients_relation(bound
->pl
, D
, e
, bound
->U
,
847 bound
->allvars
, bound
->vars
, bound
->params
, options
);
852 isl_qpolynomial_free(qp
);
853 barvinok_options_free(options
);
858 isl_qpolynomial_free(qp
);
859 barvinok_options_free(options
);
863 __isl_give isl_pw_qpolynomial_fold
*isl_pw_qpolynomial_upper_bound(
864 __isl_take isl_pw_qpolynomial
*pwqp
)
869 struct isl_bound bound
;
870 struct isl_pw_qpolynomial_fold
*pwf
;
875 dim
= isl_pw_qpolynomial_get_dim(pwqp
);
876 nvar
= isl_dim_size(dim
, isl_dim_set
);
879 return isl_pw_qpolynomial_fold_from_pw_qpolynomial(isl_fold_max
,
883 nparam
= isl_dim_size(dim
, isl_dim_param
);
885 bound
.U
= Universe_Polyhedron(nparam
);
886 bound
.params
= constructVariableVector(nparam
, "p");
888 if (isl_pw_qpolynomial_foreach_lifted_piece(pwqp
,
889 guarded_qp_bernstein_coefficients
, &bound
))
892 bound
.pl
->maximize();
894 dim
= isl_dim_drop(dim
, isl_dim_set
, 0, nvar
);
897 pwf
= isl_pw_qpolynomial_fold_from_ginac(dim
, bound
.pl
, bound
.params
);
899 Polyhedron_Free(bound
.U
);
902 isl_pw_qpolynomial_free(pwqp
);
906 isl_pw_qpolynomial_free(pwqp
);