2 #include <isl_set_polylib.h>
3 #include <barvinok/options.h>
4 #include <barvinok/util.h>
8 #include "laurent_old.h"
10 #include "section_array.h"
11 #include "remove_equalities.h"
13 extern evalue
*evalue_outer_floor(evalue
*e
);
14 extern int evalue_replace_floor(evalue
*e
, const evalue
*floor
, int var
);
15 extern void evalue_drop_floor(evalue
*e
, const evalue
*floor
);
17 #define ALLOC(type) (type*)malloc(sizeof(type))
18 #define ALLOCN(type,n) (type*)malloc((n) * sizeof(type))
20 /* Apply the variable transformation specified by T and CP on
21 * the polynomial e. T expresses the old variables in terms
22 * of the new variables (and optionally also the new parameters),
23 * while CP expresses the old parameters in terms of the new
26 static void transform_polynomial(evalue
*E
, Matrix
*T
, Matrix
*CP
,
27 unsigned nvar
, unsigned nparam
,
28 unsigned new_nvar
, unsigned new_nparam
)
33 subs
= ALLOCN(evalue
*, nvar
+nparam
);
35 for (j
= 0; j
< nvar
; ++j
) {
37 subs
[j
] = affine2evalue(T
->p
[j
], T
->p
[T
->NbRows
-1][T
->NbColumns
-1],
40 subs
[j
] = evalue_var(j
);
42 for (j
= 0; j
< nparam
; ++j
) {
44 subs
[nvar
+j
] = affine2evalue(CP
->p
[j
], CP
->p
[nparam
][new_nparam
],
47 subs
[nvar
+j
] = evalue_var(j
);
48 evalue_shift_variables(subs
[nvar
+j
], 0, new_nvar
);
51 evalue_substitute(E
, subs
);
54 for (j
= 0; j
< nvar
+nparam
; ++j
)
59 static evalue
*sum_with_equalities(Polyhedron
*P
, evalue
*E
,
60 unsigned nvar
, struct evalue_section_array
*sections
,
61 struct barvinok_options
*options
,
62 evalue
*(*base
)(Polyhedron
*P
, evalue
*E
, unsigned nvar
,
63 struct evalue_section_array
*sections
,
64 struct barvinok_options
*options
))
66 unsigned dim
= P
->Dimension
;
67 unsigned new_dim
, new_nparam
;
68 Matrix
*T
= NULL
, *CP
= NULL
;
76 remove_all_equalities(&P
, NULL
, &CP
, &T
, dim
-nvar
, options
->MaxRays
);
83 new_nparam
= CP
? CP
->NbColumns
-1 : dim
- nvar
;
84 new_dim
= T
? T
->NbColumns
-1 : nvar
+ new_nparam
;
86 /* We can avoid these substitutions if E is a constant */
88 transform_polynomial(E
, T
, CP
, nvar
, dim
-nvar
,
89 new_dim
-new_nparam
, new_nparam
);
91 if (new_dim
-new_nparam
> 0) {
92 sum
= base(P
, E
, new_dim
-new_nparam
, sections
, options
);
98 sum
->x
.p
= new_enode(partition
, 2, new_dim
);
99 EVALUE_SET_DOMAIN(sum
->x
.p
->arr
[0], P
);
100 value_clear(sum
->x
.p
->arr
[1].d
);
101 sum
->x
.p
->arr
[1] = *E
;
106 evalue_backsubstitute(sum
, CP
, options
->MaxRays
);
116 static evalue
*sum_over_polytope_with_equalities(Polyhedron
*P
, evalue
*E
,
117 unsigned nvar
, struct evalue_section_array
*sections
,
118 struct barvinok_options
*options
)
120 return sum_with_equalities(P
, E
, nvar
, sections
, options
,
121 &barvinok_sum_over_polytope
);
124 static evalue
*sum_base(Polyhedron
*P
, evalue
*E
, unsigned nvar
,
125 struct barvinok_options
*options
);
127 static evalue
*sum_base_wrap(Polyhedron
*P
, evalue
*E
, unsigned nvar
,
128 struct evalue_section_array
*sections
, struct barvinok_options
*options
)
130 return sum_base(P
, E
, nvar
, options
);
133 static evalue
*sum_base_with_equalities(Polyhedron
*P
, evalue
*E
,
134 unsigned nvar
, struct barvinok_options
*options
)
136 return sum_with_equalities(P
, E
, nvar
, NULL
, options
, &sum_base_wrap
);
139 /* The substitutions in sum_step_polynomial may have reintroduced equalities
140 * (in particular, one of the floor expressions may be equal to one of
141 * the variables), so we need to check for them again.
143 static evalue
*sum_base(Polyhedron
*P
, evalue
*E
, unsigned nvar
,
144 struct barvinok_options
*options
)
147 return sum_base_with_equalities(P
, E
, nvar
, options
);
148 if (options
->summation
== BV_SUM_EULER
)
149 return euler_summate(P
, E
, nvar
, options
);
150 else if (options
->summation
== BV_SUM_LAURENT
)
151 return laurent_summate(P
, E
, nvar
, options
);
152 else if (options
->summation
== BV_SUM_LAURENT_OLD
)
153 return laurent_summate_old(P
, E
, nvar
, options
);
157 /* Count the number of non-zero terms in e when viewed as a polynomial
158 * in only the first nvar variables. "count" is the number counted
161 static int evalue_count_terms(const evalue
*e
, unsigned nvar
, int count
)
165 if (EVALUE_IS_ZERO(*e
))
168 if (value_zero_p(e
->d
))
169 assert(e
->x
.p
->type
== polynomial
);
170 if (value_notzero_p(e
->d
) || e
->x
.p
->pos
>= nvar
+1)
173 for (i
= 0; i
< e
->x
.p
->size
; ++i
)
174 count
= evalue_count_terms(&e
->x
.p
->arr
[i
], nvar
, count
);
179 /* Create placeholder structure for unzipping.
180 * A "polynomial" is created with size terms in variable pos,
181 * with each term having itself as coefficient.
183 static evalue
*create_placeholder(int size
, int pos
)
186 evalue
*E
= ALLOC(evalue
);
188 E
->x
.p
= new_enode(polynomial
, size
, pos
+1);
189 for (i
= 0; i
< size
; ++i
) {
190 E
->x
.p
->arr
[i
].x
.p
= new_enode(polynomial
, i
+1, pos
+1);
191 for (j
= 0; j
< i
; ++j
)
192 evalue_set_si(&E
->x
.p
->arr
[i
].x
.p
->arr
[j
], 0, 1);
193 evalue_set_si(&E
->x
.p
->arr
[i
].x
.p
->arr
[i
], 1, 1);
198 /* Interchange each non-zero term in e (when viewed as a polynomial
199 * in only the first nvar variables) with a placeholder in ph (created
200 * by create_placeholder), resulting in two polynomials in the
201 * placeholder variable such that for each non-zero term in e
202 * there is a power of the placeholder variable such that the factors
203 * in the first nvar variables form the coefficient of that power in
204 * the first polynomial (e) and the factors in the remaining variables
205 * form the coefficient of that power in the second polynomial (ph).
207 static int evalue_unzip_terms(evalue
*e
, evalue
*ph
, unsigned nvar
, int count
)
211 if (EVALUE_IS_ZERO(*e
))
214 if (value_zero_p(e
->d
))
215 assert(e
->x
.p
->type
== polynomial
);
216 if (value_notzero_p(e
->d
) || e
->x
.p
->pos
>= nvar
+1) {
218 *e
= ph
->x
.p
->arr
[count
];
219 ph
->x
.p
->arr
[count
] = t
;
223 for (i
= 0; i
< e
->x
.p
->size
; ++i
)
224 count
= evalue_unzip_terms(&e
->x
.p
->arr
[i
], ph
, nvar
, count
);
229 /* Remove n variables at pos (0-based) from the polyhedron P.
230 * Each of these variables is assumed to be completely free,
231 * i.e., there is a line in the polyhedron corresponding to
232 * each of these variables.
234 static Polyhedron
*Polyhedron_Remove_Columns(Polyhedron
*P
, unsigned pos
,
238 unsigned NbConstraints
= 0;
245 assert(pos
<= P
->Dimension
);
247 if (POL_HAS(P
, POL_INEQUALITIES
))
248 NbConstraints
= P
->NbConstraints
;
249 if (POL_HAS(P
, POL_POINTS
))
250 NbRays
= P
->NbRays
- n
;
252 Q
= Polyhedron_Alloc(P
->Dimension
- n
, NbConstraints
, NbRays
);
253 if (POL_HAS(P
, POL_INEQUALITIES
)) {
255 for (i
= 0; i
< P
->NbConstraints
; ++i
) {
256 Vector_Copy(P
->Constraint
[i
], Q
->Constraint
[i
], 1+pos
);
257 Vector_Copy(P
->Constraint
[i
]+1+pos
+n
, Q
->Constraint
[i
]+1+pos
,
261 if (POL_HAS(P
, POL_POINTS
)) {
262 Q
->NbBid
= P
->NbBid
- n
;
263 for (i
= 0; i
< n
; ++i
)
264 value_set_si(Q
->Ray
[i
][1+pos
+i
], 1);
265 for (i
= 0, j
= 0; i
< P
->NbRays
; ++i
) {
266 int line
= First_Non_Zero(P
->Ray
[i
], 1+P
->Dimension
+1);
268 if (line
-1 >= pos
&& line
-1 < pos
+n
) {
273 assert(i
-j
< Q
->NbRays
);
274 Vector_Copy(P
->Ray
[i
], Q
->Ray
[i
-j
], 1+pos
);
275 Vector_Copy(P
->Ray
[i
]+1+pos
+n
, Q
->Ray
[i
-j
]+1+pos
,
279 POL_SET(Q
, POL_VALID
);
280 if (POL_HAS(P
, POL_INEQUALITIES
))
281 POL_SET(Q
, POL_INEQUALITIES
);
282 if (POL_HAS(P
, POL_POINTS
))
283 POL_SET(Q
, POL_POINTS
);
284 if (POL_HAS(P
, POL_VERTICES
))
285 POL_SET(Q
, POL_VERTICES
);
289 /* Remove n variables at pos (0-based) from the union of polyhedra P.
290 * Each of these variables is assumed to be completely free,
291 * i.e., there is a line in the polyhedron corresponding to
292 * each of these variables.
294 static Polyhedron
*Domain_Remove_Columns(Polyhedron
*P
, unsigned pos
,
298 Polyhedron
**next
= &R
;
300 for (; P
; P
= P
->next
) {
301 *next
= Polyhedron_Remove_Columns(P
, pos
, n
);
302 next
= &(*next
)->next
;
307 /* Drop n parameters starting at first from partition evalue e */
308 static void drop_parameters(evalue
*e
, int first
, int n
)
312 if (EVALUE_IS_ZERO(*e
))
315 assert(value_zero_p(e
->d
) && e
->x
.p
->type
== partition
);
316 for (i
= 0; i
< e
->x
.p
->size
/2; ++i
) {
317 Polyhedron
*P
= EVALUE_DOMAIN(e
->x
.p
->arr
[2*i
]);
318 Polyhedron
*Q
= Domain_Remove_Columns(P
, first
, n
);
319 EVALUE_SET_DOMAIN(e
->x
.p
->arr
[2*i
], Q
);
321 evalue_shift_variables(&e
->x
.p
->arr
[2*i
+1], first
, -n
);
326 static void extract_term_into(const evalue
*src
, int var
, int exp
, evalue
*dst
)
330 if (value_notzero_p(src
->d
) ||
331 src
->x
.p
->type
!= polynomial
||
332 src
->x
.p
->pos
> var
+1) {
334 evalue_copy(dst
, src
);
336 evalue_set_si(dst
, 0, 1);
340 if (src
->x
.p
->pos
== var
+1) {
341 if (src
->x
.p
->size
> exp
)
342 evalue_copy(dst
, &src
->x
.p
->arr
[exp
]);
344 evalue_set_si(dst
, 0, 1);
348 dst
->x
.p
= new_enode(polynomial
, src
->x
.p
->size
, src
->x
.p
->pos
);
349 for (i
= 0; i
< src
->x
.p
->size
; ++i
)
350 extract_term_into(&src
->x
.p
->arr
[i
], var
, exp
,
354 /* Extract the coefficient of var^exp.
356 static evalue
*extract_term(const evalue
*e
, int var
, int exp
)
361 if (EVALUE_IS_ZERO(*e
))
362 return evalue_zero();
364 assert(value_zero_p(e
->d
) && e
->x
.p
->type
== partition
);
367 res
->x
.p
= new_enode(partition
, e
->x
.p
->size
, e
->x
.p
->pos
);
368 for (i
= 0; i
< e
->x
.p
->size
/2; ++i
) {
369 EVALUE_SET_DOMAIN(res
->x
.p
->arr
[2*i
],
370 Domain_Copy(EVALUE_DOMAIN(e
->x
.p
->arr
[2*i
])));
371 extract_term_into(&e
->x
.p
->arr
[2*i
+1], var
, exp
,
372 &res
->x
.p
->arr
[2*i
+1]);
373 reduce_evalue(&res
->x
.p
->arr
[2*i
+1]);
378 /* Insert n free variables at pos (0-based) in the polyhedron P.
380 static Polyhedron
*Polyhedron_Insert_Columns(Polyhedron
*P
, unsigned pos
,
384 unsigned NbConstraints
= 0;
393 assert(pos
<= P
->Dimension
);
395 if (POL_HAS(P
, POL_INEQUALITIES
))
396 NbConstraints
= P
->NbConstraints
;
397 if (POL_HAS(P
, POL_POINTS
))
398 NbRays
= P
->NbRays
+ n
;
400 Q
= Polyhedron_Alloc(P
->Dimension
+n
, NbConstraints
, NbRays
);
401 if (POL_HAS(P
, POL_INEQUALITIES
)) {
403 for (i
= 0; i
< P
->NbConstraints
; ++i
) {
404 Vector_Copy(P
->Constraint
[i
], Q
->Constraint
[i
], 1+pos
);
405 Vector_Copy(P
->Constraint
[i
]+1+pos
, Q
->Constraint
[i
]+1+pos
+n
,
409 if (POL_HAS(P
, POL_POINTS
)) {
410 Q
->NbBid
= P
->NbBid
+ n
;
411 for (i
= 0; i
< n
; ++i
)
412 value_set_si(Q
->Ray
[i
][1+pos
+i
], 1);
413 for (i
= 0; i
< P
->NbRays
; ++i
) {
414 Vector_Copy(P
->Ray
[i
], Q
->Ray
[n
+i
], 1+pos
);
415 Vector_Copy(P
->Ray
[i
]+1+pos
, Q
->Ray
[n
+i
]+1+pos
+n
,
419 POL_SET(Q
, POL_VALID
);
420 if (POL_HAS(P
, POL_INEQUALITIES
))
421 POL_SET(Q
, POL_INEQUALITIES
);
422 if (POL_HAS(P
, POL_POINTS
))
423 POL_SET(Q
, POL_POINTS
);
424 if (POL_HAS(P
, POL_VERTICES
))
425 POL_SET(Q
, POL_VERTICES
);
429 /* Perform summation of e over a list of 1 or more factors F, with context C.
430 * nvar is the total number of variables in the remaining factors.
431 * extra is the number of placeholder parameters introduced in e,
432 * but not (yet) in F or C.
434 * If there is only one factor left, F is intersected with the
435 * context C, the placeholder variables are added, and then
436 * e is summed over the resulting parametric polytope.
438 * If there is more than one factor left, we create two polynomials
439 * in a new placeholder variable (which is placed after the regular
440 * parameters, but before any previously introduced placeholder
441 * variables) that has the factors of the variables in the first
442 * factor of F and the factor of the remaining variables of
443 * each term as its coefficients.
444 * These two polynomials are then summed over their domains
445 * and afterwards the results are combined and the placeholder
446 * variable is removed again.
448 static evalue
*sum_factors(Polyhedron
*F
, Polyhedron
*C
, evalue
*e
,
449 unsigned nvar
, unsigned extra
,
450 struct barvinok_options
*options
)
453 unsigned nparam
= C
->Dimension
;
454 unsigned F_var
= F
->Dimension
- C
->Dimension
;
460 Polyhedron
*CA
= align_context(C
, nvar
+nparam
, options
->MaxRays
);
461 Polyhedron
*P
= DomainIntersection(F
, CA
, options
->MaxRays
);
462 Polyhedron
*Q
= Polyhedron_Insert_Columns(P
, nvar
+nparam
, extra
);
466 evalue
*sum
= sum_base(Q
, e
, nvar
, options
);
471 n
= evalue_count_terms(e
, F_var
, 0);
472 ph
= create_placeholder(n
, nvar
+nparam
);
473 evalue_shift_variables(e
, nvar
+nparam
, 1);
474 evalue_unzip_terms(e
, ph
, F_var
, 0);
475 evalue_shift_variables(e
, nvar
, -(nvar
-F_var
));
476 evalue_reorder_terms(ph
);
477 evalue_shift_variables(ph
, 0, -F_var
);
479 s2
= sum_factors(F
->next
, C
, ph
, nvar
-F_var
, extra
+1, options
);
482 s1
= sum_factors(F
, C
, e
, F_var
, extra
+1, options
);
485 /* remove placeholder "polynomial" */
489 drop_parameters(s2
, nparam
, 1);
494 for (i
= 0; i
< n
; ++i
) {
496 t1
= extract_term(s1
, nparam
, i
);
497 t2
= extract_term(s2
, nparam
, i
);
506 drop_parameters(s
, nparam
, 1);
510 /* Perform summation over a product of factors F, obtained using
511 * variable transformation T from the original problem specification.
513 * We first perform the corresponding transformation on the polynomial E,
514 * compute the common context over all factors and then perform
515 * the actual summation over the factors.
517 static evalue
*sum_product(Polyhedron
*F
, evalue
*E
, Matrix
*T
, unsigned nparam
,
518 struct barvinok_options
*options
)
522 unsigned nvar
= T
->NbRows
;
526 assert(nvar
== T
->NbColumns
);
527 T2
= Matrix_Alloc(nvar
+1, nvar
+1);
528 for (i
= 0; i
< nvar
; ++i
)
529 Vector_Copy(T
->p
[i
], T2
->p
[i
], nvar
);
530 value_set_si(T2
->p
[nvar
][nvar
], 1);
532 transform_polynomial(E
, T2
, NULL
, nvar
, nparam
, nvar
, nparam
);
534 C
= Factor_Context(F
, nparam
, options
->MaxRays
);
535 if (F
->Dimension
== nparam
) {
541 sum
= sum_factors(F
, C
, E
, nvar
, 0, options
);
549 /* Add two constraints corresponding to floor = floor(e/d),
552 * -e + d t + d-1 >= 0
554 * e is assumed to be an affine expression.
556 Polyhedron
*add_floor_var(Polyhedron
*P
, unsigned nvar
, const evalue
*floor
,
557 struct barvinok_options
*options
)
560 unsigned dim
= P
->Dimension
+1;
561 Matrix
*M
= Matrix_Alloc(P
->NbConstraints
+2, 2+dim
);
563 Value
*d
= &M
->p
[0][1+nvar
];
564 evalue_extract_affine(floor
, M
->p
[0]+1, M
->p
[0]+1+dim
, d
);
565 value_oppose(*d
, *d
);
566 value_set_si(M
->p
[0][0], 1);
567 value_set_si(M
->p
[1][0], 1);
568 Vector_Oppose(M
->p
[0]+1, M
->p
[1]+1, M
->NbColumns
-1);
569 value_subtract(M
->p
[1][1+dim
], M
->p
[1][1+dim
], *d
);
570 value_decrement(M
->p
[1][1+dim
], M
->p
[1][1+dim
]);
572 for (i
= 0; i
< P
->NbConstraints
; ++i
) {
573 Vector_Copy(P
->Constraint
[i
], M
->p
[i
+2], 1+nvar
);
574 Vector_Copy(P
->Constraint
[i
]+1+nvar
, M
->p
[i
+2]+1+nvar
+1, dim
-nvar
-1+1);
577 CP
= Constraints2Polyhedron(M
, options
->MaxRays
);
582 static evalue
*evalue_add(evalue
*a
, evalue
*b
)
593 /* Compute sum of a step-polynomial over a polytope by grouping
594 * terms containing the same floor-expressions and introducing
595 * new variables for each such expression.
596 * In particular, while there is any floor-expression left,
597 * the step-polynomial is split into a polynomial containing
598 * the expression, which is then converted to a new variable,
599 * and a polynomial not containing the expression.
601 static evalue
*sum_step_polynomial(Polyhedron
*P
, evalue
*E
, unsigned nvar
,
602 struct barvinok_options
*options
)
609 while ((floor
= evalue_outer_floor(cur
))) {
612 evalue
*converted_floor
;
614 /* Ignore floors that do not depend on variables. */
615 if (value_notzero_p(floor
->d
) || floor
->x
.p
->pos
>= nvar
+1)
618 converted
= evalue_dup(cur
);
619 converted_floor
= evalue_dup(floor
);
620 evalue_shift_variables(converted
, nvar
, 1);
621 evalue_shift_variables(converted_floor
, nvar
, 1);
622 evalue_replace_floor(converted
, converted_floor
, nvar
);
623 CP
= add_floor_var(P
, nvar
, converted_floor
, options
);
624 evalue_free(converted_floor
);
625 t
= sum_step_polynomial(CP
, converted
, nvar
+1, options
);
626 evalue_free(converted
);
628 sum
= evalue_add(t
, sum
);
631 cur
= evalue_dup(cur
);
632 evalue_drop_floor(cur
, floor
);
636 evalue_floor2frac(cur
);
640 if (EVALUE_IS_ZERO(*cur
))
644 unsigned nparam
= P
->Dimension
- nvar
;
645 Polyhedron
*F
= Polyhedron_Factor(P
, nparam
, &T
, options
->MaxRays
);
647 t
= sum_base(P
, cur
, nvar
, options
);
650 cur
= evalue_dup(cur
);
651 t
= sum_product(F
, cur
, T
, nparam
, options
);
658 return evalue_add(t
, sum
);
661 evalue
*barvinok_sum_over_polytope(Polyhedron
*P
, evalue
*E
, unsigned nvar
,
662 struct evalue_section_array
*sections
,
663 struct barvinok_options
*options
)
666 return sum_over_polytope_with_equalities(P
, E
, nvar
, sections
, options
);
668 if (options
->summation
== BV_SUM_BERNOULLI
)
669 return bernoulli_summate(P
, E
, nvar
, sections
, options
);
670 else if (options
->summation
== BV_SUM_BOX
)
671 return box_summate(P
, E
, nvar
, options
->MaxRays
);
673 evalue_frac2floor2(E
, 0);
675 return sum_step_polynomial(P
, E
, nvar
, options
);
678 evalue
*barvinok_summate(evalue
*e
, int nvar
, struct barvinok_options
*options
)
681 struct evalue_section_array sections
;
685 if (nvar
== 0 || EVALUE_IS_ZERO(*e
))
686 return evalue_dup(e
);
688 assert(value_zero_p(e
->d
));
689 assert(e
->x
.p
->type
== partition
);
691 evalue_section_array_init(§ions
);
694 for (i
= 0; i
< e
->x
.p
->size
/2; ++i
) {
696 for (D
= EVALUE_DOMAIN(e
->x
.p
->arr
[2*i
]); D
; D
= D
->next
) {
697 Polyhedron
*next
= D
->next
;
701 tmp
= barvinok_sum_over_polytope(D
, &e
->x
.p
->arr
[2*i
+1], nvar
,
717 static __isl_give isl_pw_qpolynomial
*add_unbounded_guarded_qp(
718 __isl_take isl_pw_qpolynomial
*sum
,
719 __isl_take isl_basic_set
*bset
, __isl_take isl_qpolynomial
*qp
)
723 if (!sum
|| !bset
|| !qp
)
726 zero
= isl_qpolynomial_is_zero(qp
);
733 isl_pw_qpolynomial
*pwqp
;
735 dim
= isl_pw_qpolynomial_get_domain_space(sum
);
736 set
= isl_set_from_basic_set(isl_basic_set_copy(bset
));
737 set
= isl_map_domain(isl_map_from_range(set
));
738 set
= isl_set_reset_space(set
, isl_space_copy(dim
));
739 pwqp
= isl_pw_qpolynomial_alloc(set
, isl_qpolynomial_nan_on_domain(dim
));
740 sum
= isl_pw_qpolynomial_add(sum
, pwqp
);
743 isl_basic_set_free(bset
);
744 isl_qpolynomial_free(qp
);
747 isl_basic_set_free(bset
);
748 isl_qpolynomial_free(qp
);
749 isl_pw_qpolynomial_free(sum
);
753 struct barvinok_summate_data
{
755 __isl_take isl_qpolynomial
*qp
;
756 isl_pw_qpolynomial
*sum
;
760 struct evalue_section_array sections
;
761 struct barvinok_options
*options
;
764 static int add_basic_guarded_qp(__isl_take isl_basic_set
*bset
, void *user
)
766 struct barvinok_summate_data
*data
= user
;
769 isl_pw_qpolynomial
*pwqp
;
771 unsigned nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
772 unsigned nvar
= isl_basic_set_dim(bset
, isl_dim_set
);
778 bounded
= isl_basic_set_is_bounded(bset
);
783 data
->sum
= add_unbounded_guarded_qp(data
->sum
, bset
,
784 isl_qpolynomial_copy(data
->qp
));
788 dim
= isl_basic_set_get_space(bset
);
789 dim
= isl_space_domain(isl_space_from_range(dim
));
791 P
= isl_basic_set_to_polylib(bset
);
792 tmp
= barvinok_sum_over_polytope(P
, data
->e
, nvar
,
793 &data
->sections
, data
->options
);
796 pwqp
= isl_pw_qpolynomial_from_evalue(dim
, tmp
);
798 pwqp
= isl_pw_qpolynomial_reset_domain_space(pwqp
,
799 isl_space_domain(isl_space_copy(data
->dim
)));
800 data
->sum
= isl_pw_qpolynomial_add(data
->sum
, pwqp
);
802 isl_basic_set_free(bset
);
806 isl_basic_set_free(bset
);
810 static int add_guarded_qp(__isl_take isl_set
*set
, __isl_take isl_qpolynomial
*qp
,
814 struct barvinok_summate_data
*data
= user
;
821 if (data
->wrapping
) {
822 unsigned nparam
= isl_set_dim(set
, isl_dim_param
);
823 isl_qpolynomial
*qp2
= isl_qpolynomial_copy(qp
);
824 set
= isl_set_move_dims(set
, isl_dim_param
, nparam
,
825 isl_dim_set
, 0, data
->n_in
);
826 qp2
= isl_qpolynomial_move_dims(qp2
, isl_dim_param
, nparam
,
827 isl_dim_in
, 0, data
->n_in
);
828 data
->e
= isl_qpolynomial_to_evalue(qp2
);
829 isl_qpolynomial_free(qp2
);
831 data
->e
= isl_qpolynomial_to_evalue(qp
);
835 evalue_section_array_init(&data
->sections
);
837 set
= isl_set_make_disjoint(set
);
838 set
= isl_set_compute_divs(set
);
840 r
= isl_set_foreach_basic_set(set
, &add_basic_guarded_qp
, data
);
842 free(data
->sections
.s
);
844 evalue_free(data
->e
);
847 isl_qpolynomial_free(qp
);
852 isl_qpolynomial_free(qp
);
856 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_sum(
857 __isl_take isl_pw_qpolynomial
*pwqp
)
860 struct barvinok_summate_data data
;
861 int options_allocated
= 0;
870 nvar
= isl_pw_qpolynomial_dim(pwqp
, isl_dim_set
);
872 data
.dim
= isl_pw_qpolynomial_get_domain_space(pwqp
);
873 data
.wrapping
= isl_space_is_wrapping(data
.dim
);
875 data
.dim
= isl_space_unwrap(data
.dim
);
876 data
.n_in
= isl_space_dim(data
.dim
, isl_dim_in
);
877 nvar
= isl_space_dim(data
.dim
, isl_dim_out
);
881 data
.dim
= isl_space_domain(data
.dim
);
883 return isl_pw_qpolynomial_reset_domain_space(pwqp
, data
.dim
);
885 data
.dim
= isl_space_from_domain(data
.dim
);
886 data
.dim
= isl_space_add_dims(data
.dim
, isl_dim_out
, 1);
887 data
.sum
= isl_pw_qpolynomial_zero(isl_space_copy(data
.dim
));
889 ctx
= isl_pw_qpolynomial_get_ctx(pwqp
);
890 data
.options
= isl_ctx_peek_barvinok_options(ctx
);
892 data
.options
= barvinok_options_new_with_defaults();
893 options_allocated
= 1;
896 if (isl_pw_qpolynomial_foreach_lifted_piece(pwqp
,
897 add_guarded_qp
, &data
) < 0)
900 if (options_allocated
)
901 barvinok_options_free(data
.options
);
903 isl_space_free(data
.dim
);
905 isl_pw_qpolynomial_free(pwqp
);
909 if (options_allocated
)
910 barvinok_options_free(data
.options
);
911 isl_pw_qpolynomial_free(pwqp
);
912 isl_space_free(data
.dim
);
913 isl_pw_qpolynomial_free(data
.sum
);
917 static int pw_qpolynomial_sum(__isl_take isl_pw_qpolynomial
*pwqp
, void *user
)
919 isl_union_pw_qpolynomial
**res
= (isl_union_pw_qpolynomial
**)user
;
920 isl_pw_qpolynomial
*sum
;
922 sum
= isl_pw_qpolynomial_sum(pwqp
);
923 *res
= isl_union_pw_qpolynomial_add_pw_qpolynomial(*res
, sum
);
928 __isl_give isl_union_pw_qpolynomial
*isl_union_pw_qpolynomial_sum(
929 __isl_take isl_union_pw_qpolynomial
*upwqp
)
932 isl_union_pw_qpolynomial
*res
;
934 dim
= isl_union_pw_qpolynomial_get_space(upwqp
);
935 res
= isl_union_pw_qpolynomial_zero(dim
);
936 if (isl_union_pw_qpolynomial_foreach_pw_qpolynomial(upwqp
,
937 &pw_qpolynomial_sum
, &res
) < 0)
939 isl_union_pw_qpolynomial_free(upwqp
);
943 isl_union_pw_qpolynomial_free(upwqp
);
944 isl_union_pw_qpolynomial_free(res
);
948 static int join_compatible(__isl_keep isl_space
*dim1
, __isl_keep isl_space
*dim2
)
951 m
= isl_space_match(dim1
, isl_dim_param
, dim2
, isl_dim_param
);
954 return isl_space_tuple_match(dim1
, isl_dim_out
, dim2
, isl_dim_in
);
957 /* Compute the intersection of the range of the map and the domain
958 * of the piecewise quasipolynomial and then sum the associated
959 * quasipolynomial over all elements in this intersection.
961 * We first introduce some unconstrained dimensions in the
962 * piecewise quasipolynomial, intersect the resulting domain
963 * with the wrapped map and then compute the sum.
965 __isl_give isl_pw_qpolynomial
*isl_map_apply_pw_qpolynomial(
966 __isl_take isl_map
*map
, __isl_take isl_pw_qpolynomial
*pwqp
)
975 ctx
= isl_map_get_ctx(map
);
979 map_dim
= isl_map_get_space(map
);
980 pwqp_dim
= isl_pw_qpolynomial_get_space(pwqp
);
981 ok
= join_compatible(map_dim
, pwqp_dim
);
982 isl_space_free(map_dim
);
983 isl_space_free(pwqp_dim
);
985 isl_die(ctx
, isl_error_invalid
, "incompatible dimensions",
988 n_in
= isl_map_dim(map
, isl_dim_in
);
989 pwqp
= isl_pw_qpolynomial_insert_dims(pwqp
, isl_dim_in
, 0, n_in
);
991 dom
= isl_map_wrap(map
);
992 pwqp
= isl_pw_qpolynomial_reset_domain_space(pwqp
,
993 isl_set_get_space(dom
));
995 pwqp
= isl_pw_qpolynomial_intersect_domain(pwqp
, dom
);
996 pwqp
= isl_pw_qpolynomial_sum(pwqp
);
1001 isl_pw_qpolynomial_free(pwqp
);
1005 __isl_give isl_pw_qpolynomial
*isl_set_apply_pw_qpolynomial(
1006 __isl_take isl_set
*set
, __isl_take isl_pw_qpolynomial
*pwqp
)
1010 map
= isl_map_from_range(set
);
1011 pwqp
= isl_map_apply_pw_qpolynomial(map
, pwqp
);
1012 pwqp
= isl_pw_qpolynomial_project_domain_on_params(pwqp
);
1016 struct barvinok_apply_data
{
1017 isl_union_pw_qpolynomial
*upwqp
;
1018 isl_union_pw_qpolynomial
*res
;
1022 static int pw_qpolynomial_apply(__isl_take isl_pw_qpolynomial
*pwqp
, void *user
)
1025 isl_space
*pwqp_dim
;
1026 struct barvinok_apply_data
*data
= user
;
1029 map_dim
= isl_map_get_space(data
->map
);
1030 pwqp_dim
= isl_pw_qpolynomial_get_space(pwqp
);
1031 ok
= join_compatible(map_dim
, pwqp_dim
);
1032 isl_space_free(map_dim
);
1033 isl_space_free(pwqp_dim
);
1036 pwqp
= isl_map_apply_pw_qpolynomial(isl_map_copy(data
->map
),
1038 data
->res
= isl_union_pw_qpolynomial_add_pw_qpolynomial(
1041 isl_pw_qpolynomial_free(pwqp
);
1046 static int map_apply(__isl_take isl_map
*map
, void *user
)
1048 struct barvinok_apply_data
*data
= user
;
1052 r
= isl_union_pw_qpolynomial_foreach_pw_qpolynomial(data
->upwqp
,
1053 &pw_qpolynomial_apply
, data
);
1059 __isl_give isl_union_pw_qpolynomial
*isl_union_map_apply_union_pw_qpolynomial(
1060 __isl_take isl_union_map
*umap
,
1061 __isl_take isl_union_pw_qpolynomial
*upwqp
)
1064 struct barvinok_apply_data data
;
1066 upwqp
= isl_union_pw_qpolynomial_align_params(upwqp
,
1067 isl_union_map_get_space(umap
));
1068 umap
= isl_union_map_align_params(umap
,
1069 isl_union_pw_qpolynomial_get_space(upwqp
));
1072 dim
= isl_union_pw_qpolynomial_get_space(upwqp
);
1073 data
.res
= isl_union_pw_qpolynomial_zero(dim
);
1074 if (isl_union_map_foreach_map(umap
, &map_apply
, &data
) < 0)
1077 isl_union_map_free(umap
);
1078 isl_union_pw_qpolynomial_free(upwqp
);
1082 isl_union_map_free(umap
);
1083 isl_union_pw_qpolynomial_free(upwqp
);
1084 isl_union_pw_qpolynomial_free(data
.res
);
1088 struct barvinok_apply_set_data
{
1089 isl_union_pw_qpolynomial
*upwqp
;
1090 isl_union_pw_qpolynomial
*res
;
1094 static int pw_qpolynomial_apply_set(__isl_take isl_pw_qpolynomial
*pwqp
,
1098 isl_space
*pwqp_dim
;
1099 struct barvinok_apply_set_data
*data
= user
;
1102 set_dim
= isl_set_get_space(data
->set
);
1103 pwqp_dim
= isl_pw_qpolynomial_get_space(pwqp
);
1104 ok
= join_compatible(set_dim
, pwqp_dim
);
1105 isl_space_free(set_dim
);
1106 isl_space_free(pwqp_dim
);
1109 pwqp
= isl_set_apply_pw_qpolynomial(isl_set_copy(data
->set
),
1111 data
->res
= isl_union_pw_qpolynomial_add_pw_qpolynomial(
1114 isl_pw_qpolynomial_free(pwqp
);
1119 static int set_apply(__isl_take isl_set
*set
, void *user
)
1121 struct barvinok_apply_set_data
*data
= user
;
1125 r
= isl_union_pw_qpolynomial_foreach_pw_qpolynomial(data
->upwqp
,
1126 &pw_qpolynomial_apply_set
, data
);
1132 __isl_give isl_union_pw_qpolynomial
*isl_union_set_apply_union_pw_qpolynomial(
1133 __isl_take isl_union_set
*uset
,
1134 __isl_take isl_union_pw_qpolynomial
*upwqp
)
1137 struct barvinok_apply_set_data data
;
1139 upwqp
= isl_union_pw_qpolynomial_align_params(upwqp
,
1140 isl_union_set_get_space(uset
));
1141 uset
= isl_union_set_align_params(uset
,
1142 isl_union_pw_qpolynomial_get_space(upwqp
));
1145 dim
= isl_union_pw_qpolynomial_get_space(upwqp
);
1146 data
.res
= isl_union_pw_qpolynomial_zero(dim
);
1147 if (isl_union_set_foreach_set(uset
, &set_apply
, &data
) < 0)
1150 isl_union_set_free(uset
);
1151 isl_union_pw_qpolynomial_free(upwqp
);
1155 isl_union_set_free(uset
);
1156 isl_union_pw_qpolynomial_free(upwqp
);
1157 isl_union_pw_qpolynomial_free(data
.res
);
1161 evalue
*evalue_sum(evalue
*E
, int nvar
, unsigned MaxRays
)
1164 struct barvinok_options
*options
= barvinok_options_new_with_defaults();
1165 options
->MaxRays
= MaxRays
;
1166 sum
= barvinok_summate(E
, nvar
, options
);
1167 barvinok_options_free(options
);
1171 evalue
*esum(evalue
*e
, int nvar
)
1174 struct barvinok_options
*options
= barvinok_options_new_with_defaults();
1175 sum
= barvinok_summate(e
, nvar
, options
);
1176 barvinok_options_free(options
);
1180 /* Turn unweighted counting problem into "weighted" counting problem
1181 * with weight equal to 1 and call barvinok_summate on this weighted problem.
1183 evalue
*barvinok_summate_unweighted(Polyhedron
*P
, Polyhedron
*C
,
1184 struct barvinok_options
*options
)
1190 if (emptyQ(P
) || emptyQ(C
))
1191 return evalue_zero();
1193 CA
= align_context(C
, P
->Dimension
, options
->MaxRays
);
1194 D
= DomainIntersection(P
, CA
, options
->MaxRays
);
1199 return evalue_zero();
1203 e
.x
.p
= new_enode(partition
, 2, P
->Dimension
);
1204 EVALUE_SET_DOMAIN(e
.x
.p
->arr
[0], D
);
1205 evalue_set_si(&e
.x
.p
->arr
[1], 1, 1);
1206 sum
= barvinok_summate(&e
, P
->Dimension
- C
->Dimension
, options
);
1207 free_evalue_refs(&e
);