3 #include <bernstein/bernstein.h>
4 #include <bernstein/piecewise_lst.h>
5 #include <barvinok/barvinok.h>
6 #include <barvinok/util.h>
7 #include <barvinok/bernstein.h>
8 #include <barvinok/options.h>
10 using namespace GiNaC
;
11 using namespace bernstein
;
20 ex
evalue2ex(evalue
*e
, const exvector
& vars
)
22 if (value_notzero_p(e
->d
))
23 return value2numeric(e
->x
.n
)/value2numeric(e
->d
);
24 if (e
->x
.p
->type
!= polynomial
)
27 for (int i
= e
->x
.p
->size
-1; i
>= 0; --i
) {
28 poly
*= vars
[e
->x
.p
->pos
-1];
29 ex t
= evalue2ex(&e
->x
.p
->arr
[i
], vars
);
30 if (is_exactly_a
<fail
>(t
))
37 static int type_offset(enode
*p
)
39 return p
->type
== fractional
? 1 :
40 p
->type
== flooring
? 1 : 0;
43 typedef pair
<bool, const evalue
*> typed_evalue
;
45 static ex
evalue2ex_add_var(evalue
*e
, exvector
& extravar
,
46 vector
<typed_evalue
>& expr
, bool is_fract
)
50 for (int i
= 0; i
< expr
.size(); ++i
) {
51 if (is_fract
== expr
[i
].first
&& eequal(e
, expr
[i
].second
)) {
52 base_var
= extravar
[i
];
60 snprintf(name
, sizeof(name
), "f%c%d", is_fract
? 'r' : 'l', expr
.size());
61 extravar
.push_back(base_var
= symbol(name
));
62 expr
.push_back(typed_evalue(is_fract
, e
));
67 /* For the argument e=(f/d) of a fractional, return (d-1)/d times
68 * a variable in [0,1] (see setup_constraints).
70 static ex
evalue2ex_get_fract(evalue
*e
, exvector
& extravar
,
71 vector
<typed_evalue
>& expr
)
79 den
= value2numeric(d
);
83 ex base_var
= evalue2ex_add_var(e
, extravar
, expr
, true);
88 static ex
evalue2ex_r(const evalue
*e
, const exvector
& vars
,
89 exvector
& extravar
, vector
<typed_evalue
>& expr
,
92 if (value_notzero_p(e
->d
))
93 return value2numeric(e
->x
.n
)/value2numeric(e
->d
);
97 switch (e
->x
.p
->type
) {
99 base_var
= vars
[e
->x
.p
->pos
-1];
102 base_var
= evalue2ex_add_var(&e
->x
.p
->arr
[0], extravar
, expr
, false);
105 base_var
= evalue2ex_get_fract(&e
->x
.p
->arr
[0], extravar
, expr
);
109 return evalue2ex_r(&e
->x
.p
->arr
[VALUE_TO_INT(coset
->p
[e
->x
.p
->pos
-1])],
110 vars
, extravar
, expr
, coset
);
115 int offset
= type_offset(e
->x
.p
);
116 for (int i
= e
->x
.p
->size
-1; i
>= offset
; --i
) {
118 ex t
= evalue2ex_r(&e
->x
.p
->arr
[i
], vars
, extravar
, expr
, coset
);
119 if (is_exactly_a
<fail
>(t
))
126 /* For each t = floor(e/d), set up two constraints
129 * -e + d t + d-1 >= 0
131 * e is assumed to be an affine expression.
133 * For each t = fract(e/d), set up two constraints
138 static Matrix
*setup_constraints(const vector
<typed_evalue
> expr
, int nvar
)
140 int extra
= expr
.size();
143 Matrix
*M
= Matrix_Alloc(2*extra
, 1+extra
+nvar
+1);
144 for (int i
= 0; i
< extra
; ++i
) {
146 value_set_si(M
->p
[2*i
][0], 1);
147 value_set_si(M
->p
[2*i
][1+i
], -1);
148 value_set_si(M
->p
[2*i
][1+extra
+nvar
], 1);
149 value_set_si(M
->p
[2*i
+1][0], 1);
150 value_set_si(M
->p
[2*i
+1][1+i
], 1);
152 Value
*d
= &M
->p
[2*i
][1+i
];
153 evalue_extract_affine(expr
[i
].second
, M
->p
[2*i
]+1+extra
,
154 M
->p
[2*i
]+1+extra
+nvar
, d
);
155 value_oppose(*d
, *d
);
156 value_set_si(M
->p
[2*i
][0], -1);
157 Vector_Scale(M
->p
[2*i
], M
->p
[2*i
+1], M
->p
[2*i
][0], 1+extra
+nvar
+1);
158 value_set_si(M
->p
[2*i
][0], 1);
159 value_subtract(M
->p
[2*i
+1][1+extra
+nvar
], M
->p
[2*i
+1][1+extra
+nvar
], *d
);
160 value_decrement(M
->p
[2*i
+1][1+extra
+nvar
], M
->p
[2*i
+1][1+extra
+nvar
]);
166 static bool evalue_is_periodic(const evalue
*e
, Vector
*periods
)
169 bool is_periodic
= false;
171 if (value_notzero_p(e
->d
))
174 assert(e
->x
.p
->type
!= partition
);
175 if (e
->x
.p
->type
== periodic
) {
178 value_set_si(size
, e
->x
.p
->size
);
179 value_lcm(periods
->p
[e
->x
.p
->pos
-1], periods
->p
[e
->x
.p
->pos
-1], size
);
183 offset
= type_offset(e
->x
.p
);
184 for (i
= e
->x
.p
->size
-1; i
>= offset
; --i
)
185 is_periodic
= evalue_is_periodic(&e
->x
.p
->arr
[i
], periods
) || is_periodic
;
189 static ex
evalue2lst(const evalue
*e
, const exvector
& vars
,
190 exvector
& extravar
, vector
<typed_evalue
>& expr
,
193 Vector
*coset
= Vector_Alloc(periods
->Size
);
197 list
.append(evalue2ex_r(e
, vars
, extravar
, expr
, coset
));
198 for (i
= coset
->Size
-1; i
>= 0; --i
) {
199 value_increment(coset
->p
[i
], coset
->p
[i
]);
200 if (value_lt(coset
->p
[i
], periods
->p
[i
]))
202 value_set_si(coset
->p
[i
], 0);
211 ex
evalue2ex(const evalue
*e
, const exvector
& vars
, exvector
& floorvar
,
212 Matrix
**C
, Vector
**p
)
214 vector
<typed_evalue
> expr
;
215 Vector
*periods
= Vector_Alloc(vars
.size());
218 for (int i
= 0; i
< periods
->Size
; ++i
)
219 value_set_si(periods
->p
[i
], 1);
220 if (evalue_is_periodic(e
, periods
)) {
226 Vector_Free(periods
);
228 ex poly
= evalue2ex_r(e
, vars
, floorvar
, expr
, NULL
);
229 Matrix
*M
= setup_constraints(expr
, vars
.size());
235 /* if the evalue is a relation, we use the relation to cut off the
236 * the edges of the domain
238 static Polyhedron
*relation_domain(Polyhedron
*D
, evalue
*fr
, unsigned MaxRays
)
240 assert(value_zero_p(fr
->d
));
241 assert(fr
->x
.p
->type
== fractional
);
242 assert(fr
->x
.p
->size
== 3);
243 Matrix
*T
= Matrix_Alloc(2, D
->Dimension
+1);
244 value_set_si(T
->p
[1][D
->Dimension
], 1);
246 /* convert argument of fractional to polylib */
247 /* the argument is assumed to be linear */
248 evalue
*p
= &fr
->x
.p
->arr
[0];
249 evalue_denom(p
, &T
->p
[1][D
->Dimension
]);
250 for (;value_zero_p(p
->d
); p
= &p
->x
.p
->arr
[0]) {
251 assert(p
->x
.p
->type
== polynomial
);
252 assert(p
->x
.p
->size
== 2);
253 assert(value_notzero_p(p
->x
.p
->arr
[1].d
));
254 int pos
= p
->x
.p
->pos
- 1;
255 value_assign(T
->p
[0][pos
], p
->x
.p
->arr
[1].x
.n
);
256 value_multiply(T
->p
[0][pos
], T
->p
[0][pos
], T
->p
[1][D
->Dimension
]);
257 value_division(T
->p
[0][pos
], T
->p
[0][pos
], p
->x
.p
->arr
[1].d
);
259 int pos
= D
->Dimension
;
260 value_assign(T
->p
[0][pos
], p
->x
.n
);
261 value_multiply(T
->p
[0][pos
], T
->p
[0][pos
], T
->p
[1][D
->Dimension
]);
262 value_division(T
->p
[0][pos
], T
->p
[0][pos
], p
->d
);
264 Polyhedron
*E
= NULL
;
265 for (Polyhedron
*P
= D
; P
; P
= P
->next
) {
266 Polyhedron
*I
= Polyhedron_Image(P
, T
, MaxRays
);
267 I
= DomainConstraintSimplify(I
, MaxRays
);
268 Polyhedron
*R
= Polyhedron_Preimage(I
, T
, MaxRays
);
270 Polyhedron
*next
= P
->next
;
272 Polyhedron
*S
= DomainIntersection(P
, R
, MaxRays
);
278 E
= DomainConcat(S
, E
);
285 piecewise_lst
*evalue_bernstein_coefficients(piecewise_lst
*pl_all
, evalue
*e
,
286 Polyhedron
*ctx
, const exvector
& params
)
289 barvinok_options
*options
= barvinok_options_new_with_defaults();
290 pl
= evalue_bernstein_coefficients(pl_all
, e
, ctx
, params
, options
);
291 barvinok_options_free(options
);
295 static piecewise_lst
*bernstein_coefficients(piecewise_lst
*pl_all
,
296 Polyhedron
*D
, const ex
& poly
,
298 const exvector
& params
, const exvector
& floorvar
,
299 barvinok_options
*options
);
301 /* Recursively apply Bernstein expansion on P, optimizing over dims[i]
302 * variables in each level. The context ctx is assumed to have been adapted
303 * to the first level in the recursion.
305 static piecewise_lst
*bernstein_coefficients_recursive(piecewise_lst
*pl_all
,
306 Polyhedron
*P
, const vector
<int>& dims
, const ex
& poly
,
308 const exvector
& params
, const exvector
& vars
,
309 barvinok_options
*options
)
311 assert(dims
.size() > 0);
312 assert(ctx
->Dimension
== P
->Dimension
- dims
[0]);
315 for (int j
= 0; j
< dims
.size(); ++j
) {
317 pl_vars
.insert(pl_vars
.end(), vars
.begin()+done
, vars
.begin()+done
+dims
[j
]);
319 pl_params
.insert(pl_params
.end(), vars
.begin()+done
+dims
[j
], vars
.end());
320 pl_params
.insert(pl_params
.end(), params
.begin(), params
.end());
323 pl
= bernstein_coefficients(NULL
, P
, poly
, ctx
,
324 pl_params
, pl_vars
, options
);
326 piecewise_lst
*new_pl
= NULL
;
327 Polyhedron
*U
= Universe_Polyhedron(pl_params
.size());
329 for (int i
= 0; i
< pl
->list
.size(); ++i
) {
330 Polyhedron
*D
= pl
->list
[i
].first
;
331 lst polys
= pl
->list
[i
].second
;
332 new_pl
= bernstein_coefficients(new_pl
, D
, polys
, U
, pl_params
,
348 pl_all
->combine(*pl
);
355 static piecewise_lst
*bernstein_coefficients_full_recurse(piecewise_lst
*pl_all
,
356 Polyhedron
*P
, const ex
& poly
,
358 const exvector
& params
, const exvector
& vars
,
359 barvinok_options
*options
)
361 Polyhedron
*CR
= align_context(ctx
, P
->Dimension
-1, options
->MaxRays
);
362 vector
<int> dims(vars
.size());
363 for (int i
= 0; i
< dims
.size(); ++i
)
365 pl_all
= bernstein_coefficients_recursive(pl_all
, P
, dims
, poly
, CR
,
366 params
, vars
, options
);
372 static piecewise_lst
*bernstein_coefficients_product(piecewise_lst
*pl_all
,
373 Polyhedron
*F
, Matrix
*T
, const ex
& poly
,
375 const exvector
& params
, const exvector
& vars
,
376 barvinok_options
*options
)
380 for (Polyhedron
*G
= F
; G
; G
= G
->next
)
384 unsigned nparam
= params
.size();
385 unsigned nvar
= vars
.size();
386 unsigned constraints
;
388 Polyhedron
*C
= NULL
;
390 /* More context constraints */
391 if (F
->Dimension
== ctx
->Dimension
) {
401 M
= Matrix_Alloc(F
->NbConstraints
, 1+nvar
+nparam
+1);
402 for (int i
= 0; i
< F
->NbConstraints
; ++i
) {
403 Vector_Copy(F
->Constraint
[i
], M
->p
[i
], 1+F
->Dimension
-nparam
);
404 Vector_Copy(F
->Constraint
[i
]+1+F
->Dimension
-nparam
,
405 M
->p
[i
]+1+nvar
, nparam
+1);
407 P
= Constraints2Polyhedron(M
, options
->MaxRays
);
411 constraints
= C
? C
->NbConstraints
: 0;
412 constraints
+= ctx
->NbConstraints
;
413 for (Polyhedron
*G
= F
->next
; G
; G
= G
->next
) {
414 constraints
+= G
->NbConstraints
;
418 unsigned total_var
= nvar
-(F
->Dimension
-nparam
);
421 M
= Matrix_Alloc(constraints
, 1+total_var
+nparam
+1);
422 for (Polyhedron
*G
= F
->next
; G
; G
= G
->next
) {
423 unsigned this_var
= G
->Dimension
- nparam
;
424 for (int i
= 0; i
< G
->NbConstraints
; ++i
) {
425 value_assign(M
->p
[c
+i
][0], G
->Constraint
[i
][0]);
426 Vector_Copy(G
->Constraint
[i
]+1, M
->p
[c
+i
]+1+skip
, this_var
);
427 Vector_Copy(G
->Constraint
[i
]+1+this_var
, M
->p
[c
+i
]+1+total_var
,
430 c
+= G
->NbConstraints
;
433 assert(skip
== total_var
);
435 for (int i
= 0; i
< C
->NbConstraints
; ++i
) {
436 value_assign(M
->p
[c
+i
][0], C
->Constraint
[i
][0]);
437 Vector_Copy(C
->Constraint
[i
]+1, M
->p
[c
+i
]+1+total_var
,
440 c
+= C
->NbConstraints
;
442 for (int i
= 0; i
< ctx
->NbConstraints
; ++i
) {
443 value_assign(M
->p
[c
+i
][0], ctx
->Constraint
[i
][0]);
444 Vector_Copy(ctx
->Constraint
[i
]+1, M
->p
[c
+i
]+1+total_var
, nparam
+1);
446 PC
= Constraints2Polyhedron(M
, options
->MaxRays
);
449 exvector newvars
= constructVariableVector(nvar
, "t");
450 matrix
subs(1, nvar
);
451 for (int i
= 0; i
< nvar
; ++i
)
452 for (int j
= 0; j
< nvar
; ++j
)
453 subs(0,i
) += value2numeric(T
->p
[i
][j
]) * newvars
[j
];
455 ex newpoly
= replaceVariablesInPolynomial(poly
, vars
, subs
);
457 vector
<int> dims(factors
);
458 for (int i
= 0; F
; ++i
, F
= F
->next
)
459 dims
[i
] = F
->Dimension
-nparam
;
461 pl_all
= bernstein_coefficients_recursive(pl_all
, P
, dims
, newpoly
, PC
,
462 params
, newvars
, options
);
470 static piecewise_lst
*bernstein_coefficients_polyhedron(piecewise_lst
*pl_all
,
471 Polyhedron
*P
, const ex
& poly
,
473 const exvector
& params
, const exvector
& floorvar
,
474 barvinok_options
*options
)
476 if (Polyhedron_is_unbounded(P
, ctx
->Dimension
, options
->MaxRays
)) {
477 fprintf(stderr
, "warning: unbounded domain skipped\n");
478 Polyhedron_Print(stderr
, P_VALUE_FMT
, P
);
482 if (options
->bernstein_recurse
& BV_BERNSTEIN_FACTORS
) {
484 Polyhedron
*F
= Polyhedron_Factor(P
, ctx
->Dimension
, &T
, options
->MaxRays
);
486 pl_all
= bernstein_coefficients_product(pl_all
, F
, T
, poly
, ctx
, params
,
493 if (floorvar
.size() > 1 &&
494 options
->bernstein_recurse
& BV_BERNSTEIN_INTERVALS
)
495 return bernstein_coefficients_full_recurse(pl_all
, P
, poly
, ctx
,
496 params
, floorvar
, options
);
498 unsigned PP_MaxRays
= options
->MaxRays
;
499 if (PP_MaxRays
& POL_NO_DUAL
)
502 Param_Polyhedron
*PP
= Polyhedron2Param_Domain(P
, ctx
, PP_MaxRays
);
504 piecewise_lst
*pl
= new piecewise_lst(params
, options
->bernstein_optimize
);
505 for (Param_Domain
*Q
= PP
->D
; Q
; Q
= Q
->next
) {
506 matrix VM
= domainVertices(PP
, Q
, params
);
507 lst coeffs
= bernsteinExpansion(VM
, poly
, floorvar
, params
);
508 pl
->add_guarded_lst(Polyhedron_Copy(Q
->Domain
), coeffs
);
510 Param_Polyhedron_Free(PP
);
514 pl_all
->combine(*pl
);
521 static piecewise_lst
*bernstein_coefficients(piecewise_lst
*pl_all
,
522 Polyhedron
*D
, const ex
& poly
,
524 const exvector
& params
, const exvector
& floorvar
,
525 barvinok_options
*options
)
527 if (!D
->next
&& emptyQ2(D
))
530 for (Polyhedron
*P
= D
; P
; P
= P
->next
) {
531 /* This shouldn't happen */
534 Polyhedron
*next
= P
->next
;
536 pl_all
= bernstein_coefficients_polyhedron(pl_all
, P
, poly
, ctx
,
537 params
, floorvar
, options
);
543 /* Compute the coefficients of the polynomial corresponding to each coset
544 * on its own domain. This allows us to cut the domain on multiples of
546 * To perform the cutting for a coset "i mod n = c" we map the domain
547 * to the quotient space trough "i = i' n + c", simplify the constraints
548 * (implicitly) and then map back to the original space.
550 static piecewise_lst
*bernstein_coefficients_periodic(piecewise_lst
*pl_all
,
551 Polyhedron
*D
, const evalue
*e
,
552 Polyhedron
*ctx
, const exvector
& vars
,
553 const exvector
& params
, Vector
*periods
,
554 barvinok_options
*options
)
556 assert(D
->Dimension
== periods
->Size
);
557 Matrix
*T
= Matrix_Alloc(D
->Dimension
+1, D
->Dimension
+1);
558 Matrix
*T2
= Matrix_Alloc(D
->Dimension
+1, D
->Dimension
+1);
559 Vector
*coset
= Vector_Alloc(periods
->Size
);
561 vector
<typed_evalue
> expr
;
562 exvector allvars
= vars
;
563 allvars
.insert(allvars
.end(), params
.begin(), params
.end());
565 value_set_si(T2
->p
[D
->Dimension
][D
->Dimension
], 1);
566 for (int i
= 0; i
< D
->Dimension
; ++i
) {
567 value_assign(T
->p
[i
][i
], periods
->p
[i
]);
568 value_lcm(T2
->p
[D
->Dimension
][D
->Dimension
],
569 T2
->p
[D
->Dimension
][D
->Dimension
], periods
->p
[i
]);
571 value_set_si(T
->p
[D
->Dimension
][D
->Dimension
], 1);
572 for (int i
= 0; i
< D
->Dimension
; ++i
)
573 value_division(T2
->p
[i
][i
], T2
->p
[D
->Dimension
][D
->Dimension
],
577 ex poly
= evalue2ex_r(e
, allvars
, extravar
, expr
, coset
);
578 assert(extravar
.size() == 0);
579 assert(expr
.size() == 0);
580 Polyhedron
*E
= DomainPreimage(D
, T
, options
->MaxRays
);
581 Polyhedron
*F
= DomainPreimage(E
, T2
, options
->MaxRays
);
584 pl_all
= bernstein_coefficients(pl_all
, F
, poly
, ctx
, params
,
587 for (i
= D
->Dimension
-1; i
>= 0; --i
) {
588 value_increment(coset
->p
[i
], coset
->p
[i
]);
589 value_increment(T
->p
[i
][D
->Dimension
], T
->p
[i
][D
->Dimension
]);
590 value_subtract(T2
->p
[i
][D
->Dimension
], T2
->p
[i
][D
->Dimension
],
592 if (value_lt(coset
->p
[i
], periods
->p
[i
]))
594 value_set_si(coset
->p
[i
], 0);
595 value_set_si(T
->p
[i
][D
->Dimension
], 0);
596 value_set_si(T2
->p
[i
][D
->Dimension
], 0);
607 piecewise_lst
*bernstein_coefficients_relation(piecewise_lst
*pl_all
,
608 Polyhedron
*D
, evalue
*EP
, Polyhedron
*ctx
,
609 const exvector
& allvars
, const exvector
& vars
,
610 const exvector
& params
, barvinok_options
*options
)
612 if (value_zero_p(EP
->d
) && EP
->x
.p
->type
== relation
) {
613 Polyhedron
*E
= relation_domain(D
, &EP
->x
.p
->arr
[0], options
->MaxRays
);
615 pl_all
= bernstein_coefficients_relation(pl_all
, E
, &EP
->x
.p
->arr
[1],
616 ctx
, allvars
, vars
, params
,
620 /* In principle, we could cut off the edges of this domain too */
621 if (EP
->x
.p
->size
> 2)
622 pl_all
= bernstein_coefficients_relation(pl_all
, D
, &EP
->x
.p
->arr
[2],
623 ctx
, allvars
, vars
, params
,
631 ex poly
= evalue2ex(EP
, allvars
, floorvar
, &M
, &periods
);
632 floorvar
.insert(floorvar
.end(), vars
.begin(), vars
.end());
635 Polyhedron
*AE
= align_context(D
, M
->NbColumns
-2, options
->MaxRays
);
636 E
= DomainAddConstraints(AE
, M
, options
->MaxRays
);
640 if (is_exactly_a
<fail
>(poly
)) {
645 pl_all
= bernstein_coefficients_periodic(pl_all
, E
, EP
, ctx
, vars
,
646 params
, periods
, options
);
648 pl_all
= bernstein_coefficients(pl_all
, E
, poly
, ctx
, params
,
651 Vector_Free(periods
);
658 piecewise_lst
*evalue_bernstein_coefficients(piecewise_lst
*pl_all
, evalue
*e
,
659 Polyhedron
*ctx
, const exvector
& params
,
660 barvinok_options
*options
)
662 unsigned nparam
= ctx
->Dimension
;
663 if (EVALUE_IS_ZERO(*e
))
665 assert(value_zero_p(e
->d
));
666 assert(e
->x
.p
->type
== partition
);
667 assert(e
->x
.p
->size
>= 2);
668 unsigned nvars
= EVALUE_DOMAIN(e
->x
.p
->arr
[0])->Dimension
- nparam
;
670 exvector vars
= constructVariableVector(nvars
, "v");
671 exvector allvars
= vars
;
672 allvars
.insert(allvars
.end(), params
.begin(), params
.end());
674 for (int i
= 0; i
< e
->x
.p
->size
/2; ++i
) {
675 pl_all
= bernstein_coefficients_relation(pl_all
,
676 EVALUE_DOMAIN(e
->x
.p
->arr
[2*i
]), &e
->x
.p
->arr
[2*i
+1],
677 ctx
, allvars
, vars
, params
, options
);