2 #include <bernstein/bernstein.h>
3 #include <bernstein/piecewise_lst.h>
4 #include <barvinok/barvinok.h>
5 #include <barvinok/util.h>
6 #include <barvinok/bernstein.h>
7 #include <barvinok/options.h>
10 using namespace bernstein
;
19 ex
evalue2ex(evalue
*e
, const exvector
& vars
)
21 if (value_notzero_p(e
->d
))
22 return value2numeric(e
->x
.n
)/value2numeric(e
->d
);
23 if (e
->x
.p
->type
!= polynomial
)
26 for (int i
= e
->x
.p
->size
-1; i
>= 0; --i
) {
27 poly
*= vars
[e
->x
.p
->pos
-1];
28 ex t
= evalue2ex(&e
->x
.p
->arr
[i
], vars
);
29 if (is_exactly_a
<fail
>(t
))
36 static int type_offset(enode
*p
)
38 return p
->type
== fractional
? 1 :
39 p
->type
== flooring
? 1 : 0;
42 typedef pair
<bool, const evalue
*> typed_evalue
;
44 static ex
evalue2ex_add_var(evalue
*e
, exvector
& extravar
,
45 vector
<typed_evalue
>& expr
, bool is_fract
)
49 for (int i
= 0; i
< expr
.size(); ++i
) {
50 if (is_fract
== expr
[i
].first
&& eequal(e
, expr
[i
].second
)) {
51 base_var
= extravar
[i
];
59 snprintf(name
, sizeof(name
), "f%c%d", is_fract
? 'r' : 'l', expr
.size());
60 extravar
.push_back(base_var
= symbol(name
));
61 expr
.push_back(typed_evalue(is_fract
, e
));
66 /* For the argument e=(f/d) of a fractional, return (d-1)/d times
67 * a variable in [0,1] (see setup_constraints).
69 static ex
evalue2ex_get_fract(evalue
*e
, exvector
& extravar
,
70 vector
<typed_evalue
>& expr
)
78 den
= value2numeric(d
);
82 ex base_var
= evalue2ex_add_var(e
, extravar
, expr
, true);
87 static ex
evalue2ex_r(const evalue
*e
, const exvector
& vars
,
88 exvector
& extravar
, vector
<typed_evalue
>& expr
,
91 if (value_notzero_p(e
->d
))
92 return value2numeric(e
->x
.n
)/value2numeric(e
->d
);
96 switch (e
->x
.p
->type
) {
98 base_var
= vars
[e
->x
.p
->pos
-1];
101 base_var
= evalue2ex_add_var(&e
->x
.p
->arr
[0], extravar
, expr
, false);
104 base_var
= evalue2ex_get_fract(&e
->x
.p
->arr
[0], extravar
, expr
);
108 return evalue2ex_r(&e
->x
.p
->arr
[VALUE_TO_INT(coset
->p
[e
->x
.p
->pos
-1])],
109 vars
, extravar
, expr
, coset
);
114 int offset
= type_offset(e
->x
.p
);
115 for (int i
= e
->x
.p
->size
-1; i
>= offset
; --i
) {
117 ex t
= evalue2ex_r(&e
->x
.p
->arr
[i
], vars
, extravar
, expr
, coset
);
118 if (is_exactly_a
<fail
>(t
))
125 /* For each t = floor(e/d), set up two constraints
128 * -e + d t + d-1 >= 0
130 * e is assumed to be an affine expression.
132 * For each t = fract(e/d), set up two constraints
137 static Matrix
*setup_constraints(const vector
<typed_evalue
> expr
, int nvar
)
139 int extra
= expr
.size();
142 Matrix
*M
= Matrix_Alloc(2*extra
, 1+extra
+nvar
+1);
143 for (int i
= 0; i
< extra
; ++i
) {
145 value_set_si(M
->p
[2*i
][0], 1);
146 value_set_si(M
->p
[2*i
][1+i
], -1);
147 value_set_si(M
->p
[2*i
][1+extra
+nvar
], 1);
148 value_set_si(M
->p
[2*i
+1][0], 1);
149 value_set_si(M
->p
[2*i
+1][1+i
], 1);
151 Value
*d
= &M
->p
[2*i
][1+i
];
152 evalue_extract_affine(expr
[i
].second
, M
->p
[2*i
]+1+extra
,
153 M
->p
[2*i
]+1+extra
+nvar
, d
);
154 value_oppose(*d
, *d
);
155 value_set_si(M
->p
[2*i
][0], -1);
156 Vector_Scale(M
->p
[2*i
], M
->p
[2*i
+1], M
->p
[2*i
][0], 1+extra
+nvar
+1);
157 value_set_si(M
->p
[2*i
][0], 1);
158 value_subtract(M
->p
[2*i
+1][1+extra
+nvar
], M
->p
[2*i
+1][1+extra
+nvar
], *d
);
159 value_decrement(M
->p
[2*i
+1][1+extra
+nvar
], M
->p
[2*i
+1][1+extra
+nvar
]);
165 static bool evalue_is_periodic(const evalue
*e
, Vector
*periods
)
168 bool is_periodic
= false;
170 if (value_notzero_p(e
->d
))
173 assert(e
->x
.p
->type
!= partition
);
174 if (e
->x
.p
->type
== periodic
) {
177 value_set_si(size
, e
->x
.p
->size
);
178 value_lcm(periods
->p
[e
->x
.p
->pos
-1], size
, &periods
->p
[e
->x
.p
->pos
-1]);
182 offset
= type_offset(e
->x
.p
);
183 for (i
= e
->x
.p
->size
-1; i
>= offset
; --i
)
184 is_periodic
= evalue_is_periodic(&e
->x
.p
->arr
[i
], periods
) || is_periodic
;
188 static ex
evalue2lst(const evalue
*e
, const exvector
& vars
,
189 exvector
& extravar
, vector
<typed_evalue
>& expr
,
192 Vector
*coset
= Vector_Alloc(periods
->Size
);
196 list
.append(evalue2ex_r(e
, vars
, extravar
, expr
, coset
));
197 for (i
= coset
->Size
-1; i
>= 0; --i
) {
198 value_increment(coset
->p
[i
], coset
->p
[i
]);
199 if (value_lt(coset
->p
[i
], periods
->p
[i
]))
201 value_set_si(coset
->p
[i
], 0);
210 ex
evalue2ex(const evalue
*e
, const exvector
& vars
, exvector
& floorvar
,
211 Matrix
**C
, Vector
**p
)
213 vector
<typed_evalue
> expr
;
214 Vector
*periods
= Vector_Alloc(vars
.size());
217 for (int i
= 0; i
< periods
->Size
; ++i
)
218 value_set_si(periods
->p
[i
], 1);
219 if (evalue_is_periodic(e
, periods
)) {
225 Vector_Free(periods
);
227 ex poly
= evalue2ex_r(e
, vars
, floorvar
, expr
, NULL
);
228 Matrix
*M
= setup_constraints(expr
, vars
.size());
234 /* if the evalue is a relation, we use the relation to cut off the
235 * the edges of the domain
237 static void evalue_extract_poly(evalue
*e
, int i
, Polyhedron
**D
, evalue
**poly
,
240 *D
= EVALUE_DOMAIN(e
->x
.p
->arr
[2*i
]);
241 *poly
= e
= &e
->x
.p
->arr
[2*i
+1];
242 if (value_notzero_p(e
->d
))
244 if (e
->x
.p
->type
!= relation
)
246 if (e
->x
.p
->size
> 2)
248 evalue
*fr
= &e
->x
.p
->arr
[0];
249 assert(value_zero_p(fr
->d
));
250 assert(fr
->x
.p
->type
== fractional
);
251 assert(fr
->x
.p
->size
== 3);
252 Matrix
*T
= Matrix_Alloc(2, (*D
)->Dimension
+1);
253 value_set_si(T
->p
[1][(*D
)->Dimension
], 1);
255 /* convert argument of fractional to polylib */
256 /* the argument is assumed to be linear */
257 evalue
*p
= &fr
->x
.p
->arr
[0];
258 evalue_denom(p
, &T
->p
[1][(*D
)->Dimension
]);
259 for (;value_zero_p(p
->d
); p
= &p
->x
.p
->arr
[0]) {
260 assert(p
->x
.p
->type
== polynomial
);
261 assert(p
->x
.p
->size
== 2);
262 assert(value_notzero_p(p
->x
.p
->arr
[1].d
));
263 int pos
= p
->x
.p
->pos
- 1;
264 value_assign(T
->p
[0][pos
], p
->x
.p
->arr
[1].x
.n
);
265 value_multiply(T
->p
[0][pos
], T
->p
[0][pos
], T
->p
[1][(*D
)->Dimension
]);
266 value_division(T
->p
[0][pos
], T
->p
[0][pos
], p
->x
.p
->arr
[1].d
);
268 int pos
= (*D
)->Dimension
;
269 value_assign(T
->p
[0][pos
], p
->x
.n
);
270 value_multiply(T
->p
[0][pos
], T
->p
[0][pos
], T
->p
[1][(*D
)->Dimension
]);
271 value_division(T
->p
[0][pos
], T
->p
[0][pos
], p
->d
);
273 Polyhedron
*E
= NULL
;
274 for (Polyhedron
*P
= *D
; P
; P
= P
->next
) {
275 Polyhedron
*I
= Polyhedron_Image(P
, T
, MaxRays
);
276 I
= DomainConstraintSimplify(I
, MaxRays
);
277 Polyhedron
*R
= Polyhedron_Preimage(I
, T
, MaxRays
);
279 Polyhedron
*next
= P
->next
;
281 Polyhedron
*S
= DomainIntersection(P
, R
, MaxRays
);
287 E
= DomainConcat(S
, E
);
292 *poly
= &e
->x
.p
->arr
[1];
295 piecewise_lst
*evalue_bernstein_coefficients(piecewise_lst
*pl_all
, evalue
*e
,
296 Polyhedron
*ctx
, const exvector
& params
)
299 barvinok_options
*options
= barvinok_options_new_with_defaults();
300 pl
= evalue_bernstein_coefficients(pl_all
, e
, ctx
, params
, options
);
301 barvinok_options_free(options
);
305 static piecewise_lst
*bernstein_coefficients(piecewise_lst
*pl_all
,
306 Polyhedron
*D
, const ex
& poly
,
308 const exvector
& params
, const exvector
& floorvar
,
309 barvinok_options
*options
);
311 static piecewise_lst
*bernstein_coefficients_product(piecewise_lst
*pl_all
,
312 Polyhedron
*F
, Matrix
*T
, const ex
& poly
,
314 const exvector
& params
, const exvector
& vars
,
315 barvinok_options
*options
)
319 for (Polyhedron
*G
= F
; G
; G
= G
->next
)
323 unsigned nparam
= params
.size();
324 unsigned nvar
= vars
.size();
325 unsigned constraints
;
326 Polyhedron
*C
= NULL
;
328 /* More context constraints */
329 if (F
->Dimension
== ctx
->Dimension
) {
339 M
= Matrix_Alloc(F
->NbConstraints
, 1+nvar
+nparam
+1);
340 for (int i
= 0; i
< F
->NbConstraints
; ++i
) {
341 Vector_Copy(F
->Constraint
[i
], M
->p
[i
], 1+F
->Dimension
-nparam
);
342 Vector_Copy(F
->Constraint
[i
]+1+F
->Dimension
-nparam
,
343 M
->p
[i
]+1+nvar
, nparam
+1);
345 P
= Constraints2Polyhedron(M
, options
->MaxRays
);
348 constraints
= C
? C
->NbConstraints
: 0;
349 constraints
+= ctx
->NbConstraints
;
350 for (Polyhedron
*G
= F
->next
; G
; G
= G
->next
)
351 constraints
+= G
->NbConstraints
;
353 unsigned total_var
= nvar
-(F
->Dimension
-nparam
);
356 M
= Matrix_Alloc(constraints
, 1+total_var
+nparam
+1);
357 for (Polyhedron
*G
= F
->next
; G
; G
= G
->next
) {
358 unsigned this_var
= G
->Dimension
- nparam
;
359 for (int i
= 0; i
< G
->NbConstraints
; ++i
) {
360 value_assign(M
->p
[c
+i
][0], G
->Constraint
[i
][0]);
361 Vector_Copy(G
->Constraint
[i
]+1, M
->p
[c
+i
]+1+skip
, this_var
);
362 Vector_Copy(G
->Constraint
[i
]+1+this_var
, M
->p
[c
+i
]+1+total_var
,
365 c
+= G
->NbConstraints
;
368 assert(skip
== total_var
);
370 for (int i
= 0; i
< C
->NbConstraints
; ++i
) {
371 value_assign(M
->p
[c
+i
][0], C
->Constraint
[i
][0]);
372 Vector_Copy(C
->Constraint
[i
]+1, M
->p
[c
+i
]+1+total_var
,
375 c
+= C
->NbConstraints
;
377 for (int i
= 0; i
< ctx
->NbConstraints
; ++i
) {
378 value_assign(M
->p
[c
+i
][0], ctx
->Constraint
[i
][0]);
379 Vector_Copy(ctx
->Constraint
[i
]+1, M
->p
[c
+i
]+1+total_var
, nparam
+1);
381 PC
= Constraints2Polyhedron(M
, options
->MaxRays
);
384 exvector newvars
= constructVariableVector(nvar
, "t");
385 matrix
subs(1, nvar
);
386 for (int i
= 0; i
< nvar
; ++i
)
387 for (int j
= 0; j
< nvar
; ++j
)
388 subs(0,i
) += value2numeric(T
->p
[i
][j
]) * newvars
[j
];
390 ex newpoly
= replaceVariablesInPolynomial(poly
, vars
, subs
);
393 P_vars
.insert(P_vars
.end(), newvars
.begin(),
394 newvars
.begin()+(F
->Dimension
-nparam
));
396 P_params
.insert(P_params
.end(), newvars
.begin()+(F
->Dimension
-nparam
),
398 P_params
.insert(P_params
.end(), params
.begin(), params
.end());
400 pl
= bernstein_coefficients(NULL
, P
, newpoly
, PC
, P_params
, P_vars
, options
);
404 if (options
->bernstein_optimize
== BV_BERNSTEIN_MIN
)
406 else if (options
->bernstein_optimize
== BV_BERNSTEIN_MAX
)
409 unsigned done
= F
->Dimension
-nparam
;
410 for (F
= F
->next
; F
; F
= F
->next
) {
412 pl_vars
.insert(pl_vars
.end(), newvars
.begin()+done
,
413 newvars
.begin()+done
+(F
->Dimension
-nparam
));
415 pl_params
.insert(pl_params
.end(), newvars
.begin()+done
+(F
->Dimension
-nparam
),
417 pl_params
.insert(pl_params
.end(), params
.begin(), params
.end());
418 piecewise_lst
*new_pl
= NULL
;
419 Polyhedron
*U
= Universe_Polyhedron(pl_params
.size());
421 for (int i
= 0; i
< pl
->list
.size(); ++i
) {
422 Polyhedron
*D
= pl
->list
[i
].first
;
423 lst polys
= pl
->list
[i
].second
;
424 new_pl
= bernstein_coefficients(new_pl
, D
, polys
, U
, pl_params
,
431 if (options
->bernstein_optimize
== BV_BERNSTEIN_MIN
)
433 else if (options
->bernstein_optimize
== BV_BERNSTEIN_MAX
)
437 done
+= F
->Dimension
-nparam
;
443 pl_all
->combine(*pl
);
450 static piecewise_lst
*bernstein_coefficients_polyhedron(piecewise_lst
*pl_all
,
451 Polyhedron
*P
, const ex
& poly
,
453 const exvector
& params
, const exvector
& floorvar
,
454 barvinok_options
*options
)
456 if (Polyhedron_is_unbounded(P
, ctx
->Dimension
, options
->MaxRays
)) {
457 fprintf(stderr
, "warning: unbounded domain skipped\n");
458 Polyhedron_Print(stderr
, P_VALUE_FMT
, P
);
463 Polyhedron
*F
= Polyhedron_Factor(P
, ctx
->Dimension
, &T
, options
->MaxRays
);
465 pl_all
= bernstein_coefficients_product(pl_all
, F
, T
, poly
, ctx
, params
,
472 unsigned PP_MaxRays
= options
->MaxRays
;
473 if (PP_MaxRays
& POL_NO_DUAL
)
476 Param_Polyhedron
*PP
= Polyhedron2Param_Domain(P
, ctx
, PP_MaxRays
);
478 piecewise_lst
*pl
= new piecewise_lst(params
);
479 for (Param_Domain
*Q
= PP
->D
; Q
; Q
= Q
->next
) {
480 matrix VM
= domainVertices(PP
, Q
, params
);
481 lst coeffs
= bernsteinExpansion(VM
, poly
, floorvar
, params
);
482 pl
->list
.push_back(guarded_lst(Polyhedron_Copy(Q
->Domain
), coeffs
));
484 Param_Polyhedron_Free(PP
);
488 pl_all
->combine(*pl
);
495 static piecewise_lst
*bernstein_coefficients(piecewise_lst
*pl_all
,
496 Polyhedron
*D
, const ex
& poly
,
498 const exvector
& params
, const exvector
& floorvar
,
499 barvinok_options
*options
)
501 if (!D
->next
&& emptyQ2(D
))
504 for (Polyhedron
*P
= D
; P
; P
= P
->next
) {
505 /* This shouldn't happen */
508 Polyhedron
*next
= P
->next
;
510 pl_all
= bernstein_coefficients_polyhedron(pl_all
, P
, poly
, ctx
,
511 params
, floorvar
, options
);
517 /* Compute the coefficients of the polynomial corresponding to each coset
518 * on its own domain. This allows us to cut the domain on multiples of
520 * To perform the cutting for a coset "i mod n = c" we map the domain
521 * to the quotient space trough "i = i' n + c", simplify the constraints
522 * (implicitly) and then map back to the original space.
524 static piecewise_lst
*bernstein_coefficients_periodic(piecewise_lst
*pl_all
,
525 Polyhedron
*D
, const evalue
*e
,
526 Polyhedron
*ctx
, const exvector
& vars
,
527 const exvector
& params
, Vector
*periods
,
528 barvinok_options
*options
)
530 assert(D
->Dimension
== periods
->Size
);
531 Matrix
*T
= Matrix_Alloc(D
->Dimension
+1, D
->Dimension
+1);
532 Matrix
*T2
= Matrix_Alloc(D
->Dimension
+1, D
->Dimension
+1);
533 Vector
*coset
= Vector_Alloc(periods
->Size
);
535 vector
<typed_evalue
> expr
;
536 exvector allvars
= vars
;
537 allvars
.insert(allvars
.end(), params
.begin(), params
.end());
539 value_set_si(T2
->p
[D
->Dimension
][D
->Dimension
], 1);
540 for (int i
= 0; i
< D
->Dimension
; ++i
) {
541 value_assign(T
->p
[i
][i
], periods
->p
[i
]);
542 value_lcm(T2
->p
[D
->Dimension
][D
->Dimension
], periods
->p
[i
],
543 &T2
->p
[D
->Dimension
][D
->Dimension
]);
545 value_set_si(T
->p
[D
->Dimension
][D
->Dimension
], 1);
546 for (int i
= 0; i
< D
->Dimension
; ++i
)
547 value_division(T2
->p
[i
][i
], T2
->p
[D
->Dimension
][D
->Dimension
],
551 ex poly
= evalue2ex_r(e
, allvars
, extravar
, expr
, coset
);
552 assert(extravar
.size() == 0);
553 assert(expr
.size() == 0);
554 Polyhedron
*E
= DomainPreimage(D
, T
, options
->MaxRays
);
555 Polyhedron
*F
= DomainPreimage(E
, T2
, options
->MaxRays
);
558 pl_all
= bernstein_coefficients(pl_all
, F
, poly
, ctx
, params
,
561 for (i
= D
->Dimension
-1; i
>= 0; --i
) {
562 value_increment(coset
->p
[i
], coset
->p
[i
]);
563 value_increment(T
->p
[i
][D
->Dimension
], T
->p
[i
][D
->Dimension
]);
564 value_subtract(T2
->p
[i
][D
->Dimension
], T2
->p
[i
][D
->Dimension
],
566 if (value_lt(coset
->p
[i
], periods
->p
[i
]))
568 value_set_si(coset
->p
[i
], 0);
569 value_set_si(T
->p
[i
][D
->Dimension
], 0);
570 value_set_si(T2
->p
[i
][D
->Dimension
], 0);
581 piecewise_lst
*evalue_bernstein_coefficients(piecewise_lst
*pl_all
, evalue
*e
,
582 Polyhedron
*ctx
, const exvector
& params
,
583 barvinok_options
*options
)
585 unsigned nparam
= ctx
->Dimension
;
586 if (EVALUE_IS_ZERO(*e
))
588 assert(value_zero_p(e
->d
));
589 assert(e
->x
.p
->type
== partition
);
590 assert(e
->x
.p
->size
>= 2);
591 unsigned nvars
= EVALUE_DOMAIN(e
->x
.p
->arr
[0])->Dimension
- nparam
;
593 exvector vars
= constructVariableVector(nvars
, "v");
594 exvector allvars
= vars
;
595 allvars
.insert(allvars
.end(), params
.begin(), params
.end());
597 for (int i
= 0; i
< e
->x
.p
->size
/2; ++i
) {
604 evalue_extract_poly(e
, i
, &E
, &EP
, options
->MaxRays
);
605 ex poly
= evalue2ex(EP
, allvars
, floorvar
, &M
, &periods
);
606 floorvar
.insert(floorvar
.end(), vars
.begin(), vars
.end());
608 Polyhedron
*AE
= align_context(E
, M
->NbColumns
-2, options
->MaxRays
);
609 if (E
!= EVALUE_DOMAIN(e
->x
.p
->arr
[2*i
]))
611 E
= DomainAddConstraints(AE
, M
, options
->MaxRays
);
615 if (is_exactly_a
<fail
>(poly
)) {
616 if (E
!= EVALUE_DOMAIN(e
->x
.p
->arr
[2*i
]))
622 pl_all
= bernstein_coefficients_periodic(pl_all
, E
, EP
, ctx
, vars
,
623 params
, periods
, options
);
625 pl_all
= bernstein_coefficients(pl_all
, E
, poly
, ctx
, params
,
628 Vector_Free(periods
);
629 if (E
!= EVALUE_DOMAIN(e
->x
.p
->arr
[2*i
]))