2 #include <isl/union_set.h>
3 #include <isl/union_map.h>
4 #include <isl_set_polylib.h>
5 #include <barvinok/options.h>
6 #include <barvinok/util.h>
10 #include "laurent_old.h"
12 #include "section_array.h"
13 #include "remove_equalities.h"
15 extern evalue
*evalue_outer_floor(evalue
*e
);
16 extern int evalue_replace_floor(evalue
*e
, const evalue
*floor
, int var
);
17 extern void evalue_drop_floor(evalue
*e
, const evalue
*floor
);
19 #define ALLOC(type) (type*)malloc(sizeof(type))
20 #define ALLOCN(type,n) (type*)malloc((n) * sizeof(type))
22 /* Apply the variable transformation specified by T and CP on
23 * the polynomial e. T expresses the old variables in terms
24 * of the new variables (and optionally also the new parameters),
25 * while CP expresses the old parameters in terms of the new
28 static void transform_polynomial(evalue
*E
, Matrix
*T
, Matrix
*CP
,
29 unsigned nvar
, unsigned nparam
,
30 unsigned new_nvar
, unsigned new_nparam
)
35 subs
= ALLOCN(evalue
*, nvar
+nparam
);
37 for (j
= 0; j
< nvar
; ++j
) {
39 subs
[j
] = affine2evalue(T
->p
[j
], T
->p
[T
->NbRows
-1][T
->NbColumns
-1],
42 subs
[j
] = evalue_var(j
);
44 for (j
= 0; j
< nparam
; ++j
) {
46 subs
[nvar
+j
] = affine2evalue(CP
->p
[j
], CP
->p
[nparam
][new_nparam
],
49 subs
[nvar
+j
] = evalue_var(j
);
50 evalue_shift_variables(subs
[nvar
+j
], 0, new_nvar
);
53 evalue_substitute(E
, subs
);
56 for (j
= 0; j
< nvar
+nparam
; ++j
)
61 /* Compute the sum of the quasi-polynomial E
62 * over a 0D (non-empty, but possibly parametric) polytope P.
66 * We simply return a partition evalue with P as domain and E as value.
68 static evalue
*sum_over_polytope_0D(Polyhedron
*P
, evalue
*E
)
74 sum
->x
.p
= new_enode(partition
, 2, P
->Dimension
);
75 EVALUE_SET_DOMAIN(sum
->x
.p
->arr
[0], P
);
76 value_clear(sum
->x
.p
->arr
[1].d
);
77 sum
->x
.p
->arr
[1] = *E
;
83 static evalue
*sum_with_equalities(Polyhedron
*P
, evalue
*E
,
84 unsigned nvar
, struct evalue_section_array
*sections
,
85 struct barvinok_options
*options
,
86 evalue
*(*base
)(Polyhedron
*P
, evalue
*E
, unsigned nvar
,
87 struct evalue_section_array
*sections
,
88 struct barvinok_options
*options
))
90 unsigned dim
= P
->Dimension
;
91 unsigned new_dim
, new_nparam
;
92 Matrix
*T
= NULL
, *CP
= NULL
;
100 remove_all_equalities(&P
, NULL
, &CP
, &T
, dim
-nvar
, options
->MaxRays
);
104 return evalue_zero();
107 new_nparam
= CP
? CP
->NbColumns
-1 : dim
- nvar
;
108 new_dim
= T
? T
->NbColumns
-1 : nvar
+ new_nparam
;
110 /* We can avoid these substitutions if E is a constant */
112 transform_polynomial(E
, T
, CP
, nvar
, dim
-nvar
,
113 new_dim
-new_nparam
, new_nparam
);
115 if (new_dim
-new_nparam
> 0) {
116 sum
= base(P
, E
, new_dim
-new_nparam
, sections
, options
);
120 sum
= sum_over_polytope_0D(P
, E
);
124 evalue_backsubstitute(sum
, CP
, options
->MaxRays
);
134 static evalue
*sum_over_polytope_with_equalities(Polyhedron
*P
, evalue
*E
,
135 unsigned nvar
, struct evalue_section_array
*sections
,
136 struct barvinok_options
*options
)
138 return sum_with_equalities(P
, E
, nvar
, sections
, options
,
139 &barvinok_sum_over_polytope
);
142 static evalue
*sum_base(Polyhedron
*P
, evalue
*E
, unsigned nvar
,
143 struct barvinok_options
*options
);
145 static evalue
*sum_base_wrap(Polyhedron
*P
, evalue
*E
, unsigned nvar
,
146 struct evalue_section_array
*sections
, struct barvinok_options
*options
)
148 return sum_base(P
, E
, nvar
, options
);
151 static evalue
*sum_base_with_equalities(Polyhedron
*P
, evalue
*E
,
152 unsigned nvar
, struct barvinok_options
*options
)
154 return sum_with_equalities(P
, E
, nvar
, NULL
, options
, &sum_base_wrap
);
157 /* The substitutions in sum_step_polynomial may have reintroduced equalities
158 * (in particular, one of the floor expressions may be equal to one of
159 * the variables), so we need to check for them again.
161 static evalue
*sum_base(Polyhedron
*P
, evalue
*E
, unsigned nvar
,
162 struct barvinok_options
*options
)
165 return sum_base_with_equalities(P
, E
, nvar
, options
);
166 if (options
->summation
== BV_SUM_EULER
)
167 return euler_summate(P
, E
, nvar
, options
);
168 else if (options
->summation
== BV_SUM_LAURENT
)
169 return laurent_summate(P
, E
, nvar
, options
);
170 else if (options
->summation
== BV_SUM_LAURENT_OLD
)
171 return laurent_summate_old(P
, E
, nvar
, options
);
175 /* Count the number of non-zero terms in e when viewed as a polynomial
176 * in only the first nvar variables. "count" is the number counted
179 static int evalue_count_terms(const evalue
*e
, unsigned nvar
, int count
)
183 if (EVALUE_IS_ZERO(*e
))
186 if (value_zero_p(e
->d
))
187 assert(e
->x
.p
->type
== polynomial
);
188 if (value_notzero_p(e
->d
) || e
->x
.p
->pos
>= nvar
+1)
191 for (i
= 0; i
< e
->x
.p
->size
; ++i
)
192 count
= evalue_count_terms(&e
->x
.p
->arr
[i
], nvar
, count
);
197 /* Create placeholder structure for unzipping.
198 * A "polynomial" is created with size terms in variable pos,
199 * with each term having itself as coefficient.
201 static evalue
*create_placeholder(int size
, int pos
)
204 evalue
*E
= ALLOC(evalue
);
206 E
->x
.p
= new_enode(polynomial
, size
, pos
+1);
207 for (i
= 0; i
< size
; ++i
) {
208 E
->x
.p
->arr
[i
].x
.p
= new_enode(polynomial
, i
+1, pos
+1);
209 for (j
= 0; j
< i
; ++j
)
210 evalue_set_si(&E
->x
.p
->arr
[i
].x
.p
->arr
[j
], 0, 1);
211 evalue_set_si(&E
->x
.p
->arr
[i
].x
.p
->arr
[i
], 1, 1);
216 /* Interchange each non-zero term in e (when viewed as a polynomial
217 * in only the first nvar variables) with a placeholder in ph (created
218 * by create_placeholder), resulting in two polynomials in the
219 * placeholder variable such that for each non-zero term in e
220 * there is a power of the placeholder variable such that the factors
221 * in the first nvar variables form the coefficient of that power in
222 * the first polynomial (e) and the factors in the remaining variables
223 * form the coefficient of that power in the second polynomial (ph).
225 static int evalue_unzip_terms(evalue
*e
, evalue
*ph
, unsigned nvar
, int count
)
229 if (EVALUE_IS_ZERO(*e
))
232 if (value_zero_p(e
->d
))
233 assert(e
->x
.p
->type
== polynomial
);
234 if (value_notzero_p(e
->d
) || e
->x
.p
->pos
>= nvar
+1) {
236 *e
= ph
->x
.p
->arr
[count
];
237 ph
->x
.p
->arr
[count
] = t
;
241 for (i
= 0; i
< e
->x
.p
->size
; ++i
)
242 count
= evalue_unzip_terms(&e
->x
.p
->arr
[i
], ph
, nvar
, count
);
247 /* Remove n variables at pos (0-based) from the polyhedron P.
248 * Each of these variables is assumed to be completely free,
249 * i.e., there is a line in the polyhedron corresponding to
250 * each of these variables.
252 static Polyhedron
*Polyhedron_Remove_Columns(Polyhedron
*P
, unsigned pos
,
256 unsigned NbConstraints
= 0;
263 assert(pos
<= P
->Dimension
);
265 if (POL_HAS(P
, POL_INEQUALITIES
))
266 NbConstraints
= P
->NbConstraints
;
267 if (POL_HAS(P
, POL_POINTS
))
268 NbRays
= P
->NbRays
- n
;
270 Q
= Polyhedron_Alloc(P
->Dimension
- n
, NbConstraints
, NbRays
);
271 if (POL_HAS(P
, POL_INEQUALITIES
)) {
273 for (i
= 0; i
< P
->NbConstraints
; ++i
) {
274 Vector_Copy(P
->Constraint
[i
], Q
->Constraint
[i
], 1+pos
);
275 Vector_Copy(P
->Constraint
[i
]+1+pos
+n
, Q
->Constraint
[i
]+1+pos
,
279 if (POL_HAS(P
, POL_POINTS
)) {
280 Q
->NbBid
= P
->NbBid
- n
;
281 for (i
= 0; i
< n
; ++i
)
282 value_set_si(Q
->Ray
[i
][1+pos
+i
], 1);
283 for (i
= 0, j
= 0; i
< P
->NbRays
; ++i
) {
284 int line
= First_Non_Zero(P
->Ray
[i
], 1+P
->Dimension
+1);
286 if (line
-1 >= pos
&& line
-1 < pos
+n
) {
291 assert(i
-j
< Q
->NbRays
);
292 Vector_Copy(P
->Ray
[i
], Q
->Ray
[i
-j
], 1+pos
);
293 Vector_Copy(P
->Ray
[i
]+1+pos
+n
, Q
->Ray
[i
-j
]+1+pos
,
297 POL_SET(Q
, POL_VALID
);
298 if (POL_HAS(P
, POL_INEQUALITIES
))
299 POL_SET(Q
, POL_INEQUALITIES
);
300 if (POL_HAS(P
, POL_POINTS
))
301 POL_SET(Q
, POL_POINTS
);
302 if (POL_HAS(P
, POL_VERTICES
))
303 POL_SET(Q
, POL_VERTICES
);
307 /* Remove n variables at pos (0-based) from the union of polyhedra P.
308 * Each of these variables is assumed to be completely free,
309 * i.e., there is a line in the polyhedron corresponding to
310 * each of these variables.
312 static Polyhedron
*Domain_Remove_Columns(Polyhedron
*P
, unsigned pos
,
316 Polyhedron
**next
= &R
;
318 for (; P
; P
= P
->next
) {
319 *next
= Polyhedron_Remove_Columns(P
, pos
, n
);
320 next
= &(*next
)->next
;
325 /* Drop n parameters starting at first from partition evalue e */
326 static void drop_parameters(evalue
*e
, int first
, int n
)
330 if (EVALUE_IS_ZERO(*e
))
333 assert(value_zero_p(e
->d
) && e
->x
.p
->type
== partition
);
334 for (i
= 0; i
< e
->x
.p
->size
/2; ++i
) {
335 Polyhedron
*P
= EVALUE_DOMAIN(e
->x
.p
->arr
[2*i
]);
336 Polyhedron
*Q
= Domain_Remove_Columns(P
, first
, n
);
337 EVALUE_SET_DOMAIN(e
->x
.p
->arr
[2*i
], Q
);
339 evalue_shift_variables(&e
->x
.p
->arr
[2*i
+1], first
, -n
);
344 static void extract_term_into(const evalue
*src
, int var
, int exp
, evalue
*dst
)
348 if (value_notzero_p(src
->d
) ||
349 src
->x
.p
->type
!= polynomial
||
350 src
->x
.p
->pos
> var
+1) {
352 evalue_copy(dst
, src
);
354 evalue_set_si(dst
, 0, 1);
358 if (src
->x
.p
->pos
== var
+1) {
359 if (src
->x
.p
->size
> exp
)
360 evalue_copy(dst
, &src
->x
.p
->arr
[exp
]);
362 evalue_set_si(dst
, 0, 1);
366 dst
->x
.p
= new_enode(polynomial
, src
->x
.p
->size
, src
->x
.p
->pos
);
367 for (i
= 0; i
< src
->x
.p
->size
; ++i
)
368 extract_term_into(&src
->x
.p
->arr
[i
], var
, exp
,
372 /* Extract the coefficient of var^exp.
374 static evalue
*extract_term(const evalue
*e
, int var
, int exp
)
379 if (EVALUE_IS_ZERO(*e
))
380 return evalue_zero();
382 assert(value_zero_p(e
->d
) && e
->x
.p
->type
== partition
);
385 res
->x
.p
= new_enode(partition
, e
->x
.p
->size
, e
->x
.p
->pos
);
386 for (i
= 0; i
< e
->x
.p
->size
/2; ++i
) {
387 EVALUE_SET_DOMAIN(res
->x
.p
->arr
[2*i
],
388 Domain_Copy(EVALUE_DOMAIN(e
->x
.p
->arr
[2*i
])));
389 extract_term_into(&e
->x
.p
->arr
[2*i
+1], var
, exp
,
390 &res
->x
.p
->arr
[2*i
+1]);
391 reduce_evalue(&res
->x
.p
->arr
[2*i
+1]);
396 /* Insert n free variables at pos (0-based) in the polyhedron P.
398 static Polyhedron
*Polyhedron_Insert_Columns(Polyhedron
*P
, unsigned pos
,
402 unsigned NbConstraints
= 0;
411 assert(pos
<= P
->Dimension
);
413 if (POL_HAS(P
, POL_INEQUALITIES
))
414 NbConstraints
= P
->NbConstraints
;
415 if (POL_HAS(P
, POL_POINTS
))
416 NbRays
= P
->NbRays
+ n
;
418 Q
= Polyhedron_Alloc(P
->Dimension
+n
, NbConstraints
, NbRays
);
419 if (POL_HAS(P
, POL_INEQUALITIES
)) {
421 for (i
= 0; i
< P
->NbConstraints
; ++i
) {
422 Vector_Copy(P
->Constraint
[i
], Q
->Constraint
[i
], 1+pos
);
423 Vector_Copy(P
->Constraint
[i
]+1+pos
, Q
->Constraint
[i
]+1+pos
+n
,
427 if (POL_HAS(P
, POL_POINTS
)) {
428 Q
->NbBid
= P
->NbBid
+ n
;
429 for (i
= 0; i
< n
; ++i
)
430 value_set_si(Q
->Ray
[i
][1+pos
+i
], 1);
431 for (i
= 0; i
< P
->NbRays
; ++i
) {
432 Vector_Copy(P
->Ray
[i
], Q
->Ray
[n
+i
], 1+pos
);
433 Vector_Copy(P
->Ray
[i
]+1+pos
, Q
->Ray
[n
+i
]+1+pos
+n
,
437 POL_SET(Q
, POL_VALID
);
438 if (POL_HAS(P
, POL_INEQUALITIES
))
439 POL_SET(Q
, POL_INEQUALITIES
);
440 if (POL_HAS(P
, POL_POINTS
))
441 POL_SET(Q
, POL_POINTS
);
442 if (POL_HAS(P
, POL_VERTICES
))
443 POL_SET(Q
, POL_VERTICES
);
447 /* Perform summation of e over a list of 1 or more factors F, with context C.
448 * nvar is the total number of variables in the remaining factors.
449 * extra is the number of placeholder parameters introduced in e,
450 * but not (yet) in F or C.
452 * If there is only one factor left, F is intersected with the
453 * context C, the placeholder variables are added, and then
454 * e is summed over the resulting parametric polytope.
456 * If there is more than one factor left, we create two polynomials
457 * in a new placeholder variable (which is placed after the regular
458 * parameters, but before any previously introduced placeholder
459 * variables) that has the factors of the variables in the first
460 * factor of F and the factor of the remaining variables of
461 * each term as its coefficients.
462 * These two polynomials are then summed over their domains
463 * and afterwards the results are combined and the placeholder
464 * variable is removed again.
466 static evalue
*sum_factors(Polyhedron
*F
, Polyhedron
*C
, evalue
*e
,
467 unsigned nvar
, unsigned extra
,
468 struct barvinok_options
*options
)
470 unsigned nparam
= C
->Dimension
;
471 unsigned F_var
= F
->Dimension
- C
->Dimension
;
477 Polyhedron
*CA
= align_context(C
, nvar
+nparam
, options
->MaxRays
);
478 Polyhedron
*P
= DomainIntersection(F
, CA
, options
->MaxRays
);
479 Polyhedron
*Q
= Polyhedron_Insert_Columns(P
, nvar
+nparam
, extra
);
483 evalue
*sum
= sum_base(Q
, e
, nvar
, options
);
488 n
= evalue_count_terms(e
, F_var
, 0);
489 ph
= create_placeholder(n
, nvar
+nparam
);
490 evalue_shift_variables(e
, nvar
+nparam
, 1);
491 evalue_unzip_terms(e
, ph
, F_var
, 0);
492 evalue_shift_variables(e
, nvar
, -(nvar
-F_var
));
493 evalue_reorder_terms(ph
);
494 evalue_shift_variables(ph
, 0, -F_var
);
496 s2
= sum_factors(F
->next
, C
, ph
, nvar
-F_var
, extra
+1, options
);
499 s1
= sum_factors(F
, C
, e
, F_var
, extra
+1, options
);
502 /* remove placeholder "polynomial" */
506 drop_parameters(s2
, nparam
, 1);
511 for (i
= 0; i
< n
; ++i
) {
513 t1
= extract_term(s1
, nparam
, i
);
514 t2
= extract_term(s2
, nparam
, i
);
523 drop_parameters(s
, nparam
, 1);
527 /* Perform summation over a product of factors F, obtained using
528 * variable transformation T from the original problem specification.
530 * We first perform the corresponding transformation on the polynomial E,
531 * compute the common context over all factors and then perform
532 * the actual summation over the factors.
534 static evalue
*sum_product(Polyhedron
*F
, evalue
*E
, Matrix
*T
, unsigned nparam
,
535 struct barvinok_options
*options
)
539 unsigned nvar
= T
->NbRows
;
543 assert(nvar
== T
->NbColumns
);
544 T2
= Matrix_Alloc(nvar
+1, nvar
+1);
545 for (i
= 0; i
< nvar
; ++i
)
546 Vector_Copy(T
->p
[i
], T2
->p
[i
], nvar
);
547 value_set_si(T2
->p
[nvar
][nvar
], 1);
549 transform_polynomial(E
, T2
, NULL
, nvar
, nparam
, nvar
, nparam
);
551 C
= Factor_Context(F
, nparam
, options
->MaxRays
);
552 if (F
->Dimension
== nparam
) {
558 sum
= sum_factors(F
, C
, E
, nvar
, 0, options
);
566 /* Add two constraints corresponding to floor = floor(e/d),
569 * -e + d t + d-1 >= 0
571 * e is assumed to be an affine expression.
573 Polyhedron
*add_floor_var(Polyhedron
*P
, unsigned nvar
, const evalue
*floor
,
574 struct barvinok_options
*options
)
577 unsigned dim
= P
->Dimension
+1;
578 Matrix
*M
= Matrix_Alloc(P
->NbConstraints
+2, 2+dim
);
580 Value
*d
= &M
->p
[0][1+nvar
];
581 evalue_extract_affine(floor
, M
->p
[0]+1, M
->p
[0]+1+dim
, d
);
582 value_oppose(*d
, *d
);
583 value_set_si(M
->p
[0][0], 1);
584 value_set_si(M
->p
[1][0], 1);
585 Vector_Oppose(M
->p
[0]+1, M
->p
[1]+1, M
->NbColumns
-1);
586 value_subtract(M
->p
[1][1+dim
], M
->p
[1][1+dim
], *d
);
587 value_decrement(M
->p
[1][1+dim
], M
->p
[1][1+dim
]);
589 for (i
= 0; i
< P
->NbConstraints
; ++i
) {
590 Vector_Copy(P
->Constraint
[i
], M
->p
[i
+2], 1+nvar
);
591 Vector_Copy(P
->Constraint
[i
]+1+nvar
, M
->p
[i
+2]+1+nvar
+1, dim
-nvar
-1+1);
594 CP
= Constraints2Polyhedron(M
, options
->MaxRays
);
599 static evalue
*evalue_add(evalue
*a
, evalue
*b
)
610 /* Compute sum of a step-polynomial over a polytope by grouping
611 * terms containing the same floor-expressions and introducing
612 * new variables for each such expression.
613 * In particular, while there is any floor-expression left,
614 * the step-polynomial is split into a polynomial containing
615 * the expression, which is then converted to a new variable,
616 * and a polynomial not containing the expression.
618 static evalue
*sum_step_polynomial(Polyhedron
*P
, evalue
*E
, unsigned nvar
,
619 struct barvinok_options
*options
)
626 while ((floor
= evalue_outer_floor(cur
))) {
629 evalue
*converted_floor
;
631 /* Ignore floors that do not depend on variables. */
632 if (value_notzero_p(floor
->d
) || floor
->x
.p
->pos
>= nvar
+1)
635 converted
= evalue_dup(cur
);
636 converted_floor
= evalue_dup(floor
);
637 evalue_shift_variables(converted
, nvar
, 1);
638 evalue_shift_variables(converted_floor
, nvar
, 1);
639 evalue_replace_floor(converted
, converted_floor
, nvar
);
640 CP
= add_floor_var(P
, nvar
, converted_floor
, options
);
641 evalue_free(converted_floor
);
642 t
= sum_step_polynomial(CP
, converted
, nvar
+1, options
);
643 evalue_free(converted
);
645 sum
= evalue_add(t
, sum
);
648 cur
= evalue_dup(cur
);
649 evalue_drop_floor(cur
, floor
);
653 evalue_floor2frac(cur
);
657 if (EVALUE_IS_ZERO(*cur
))
661 unsigned nparam
= P
->Dimension
- nvar
;
662 Polyhedron
*F
= Polyhedron_Factor(P
, nparam
, &T
, options
->MaxRays
);
664 t
= sum_base(P
, cur
, nvar
, options
);
667 cur
= evalue_dup(cur
);
668 t
= sum_product(F
, cur
, T
, nparam
, options
);
675 return evalue_add(t
, sum
);
678 evalue
*barvinok_sum_over_polytope(Polyhedron
*P
, evalue
*E
, unsigned nvar
,
679 struct evalue_section_array
*sections
,
680 struct barvinok_options
*options
)
683 return sum_over_polytope_with_equalities(P
, E
, nvar
, sections
, options
);
686 return sum_over_polytope_0D(Polyhedron_Copy(P
), evalue_dup(E
));
688 if (options
->summation
== BV_SUM_BERNOULLI
)
689 return bernoulli_summate(P
, E
, nvar
, sections
, options
);
690 else if (options
->summation
== BV_SUM_BOX
)
691 return box_summate(P
, E
, nvar
, options
->MaxRays
);
693 evalue_frac2floor2(E
, 0);
695 return sum_step_polynomial(P
, E
, nvar
, options
);
698 evalue
*barvinok_summate(evalue
*e
, int nvar
, struct barvinok_options
*options
)
701 struct evalue_section_array sections
;
705 if (nvar
== 0 || EVALUE_IS_ZERO(*e
))
706 return evalue_dup(e
);
708 assert(value_zero_p(e
->d
));
709 assert(e
->x
.p
->type
== partition
);
711 evalue_section_array_init(§ions
);
714 for (i
= 0; i
< e
->x
.p
->size
/2; ++i
) {
716 for (D
= EVALUE_DOMAIN(e
->x
.p
->arr
[2*i
]); D
; D
= D
->next
) {
717 Polyhedron
*next
= D
->next
;
721 tmp
= barvinok_sum_over_polytope(D
, &e
->x
.p
->arr
[2*i
+1], nvar
,
737 static __isl_give isl_pw_qpolynomial
*add_unbounded_guarded_qp(
738 __isl_take isl_pw_qpolynomial
*sum
,
739 __isl_take isl_basic_set
*bset
, __isl_take isl_qpolynomial
*qp
)
743 if (!sum
|| !bset
|| !qp
)
746 zero
= isl_qpolynomial_is_zero(qp
);
753 isl_pw_qpolynomial
*pwqp
;
755 dim
= isl_pw_qpolynomial_get_domain_space(sum
);
756 set
= isl_set_from_basic_set(isl_basic_set_copy(bset
));
757 set
= isl_map_domain(isl_map_from_range(set
));
758 set
= isl_set_reset_space(set
, isl_space_copy(dim
));
759 pwqp
= isl_pw_qpolynomial_alloc(set
, isl_qpolynomial_nan_on_domain(dim
));
760 sum
= isl_pw_qpolynomial_add(sum
, pwqp
);
763 isl_basic_set_free(bset
);
764 isl_qpolynomial_free(qp
);
767 isl_basic_set_free(bset
);
768 isl_qpolynomial_free(qp
);
769 isl_pw_qpolynomial_free(sum
);
773 struct barvinok_summate_data
{
776 isl_pw_qpolynomial
*sum
;
780 struct evalue_section_array sections
;
781 struct barvinok_options
*options
;
784 static isl_stat
add_basic_guarded_qp(__isl_take isl_basic_set
*bset
, void *user
)
786 struct barvinok_summate_data
*data
= user
;
789 isl_pw_qpolynomial
*pwqp
;
791 unsigned nvar
= isl_basic_set_dim(bset
, isl_dim_set
);
795 return isl_stat_error
;
797 bounded
= isl_basic_set_is_bounded(bset
);
802 data
->sum
= add_unbounded_guarded_qp(data
->sum
, bset
,
803 isl_qpolynomial_copy(data
->qp
));
807 dim
= isl_basic_set_get_space(bset
);
808 dim
= isl_space_domain(isl_space_from_range(dim
));
810 P
= isl_basic_set_to_polylib(bset
);
811 tmp
= barvinok_sum_over_polytope(P
, data
->e
, nvar
,
812 &data
->sections
, data
->options
);
815 pwqp
= isl_pw_qpolynomial_from_evalue(dim
, tmp
);
817 pwqp
= isl_pw_qpolynomial_reset_domain_space(pwqp
,
818 isl_space_domain(isl_space_copy(data
->dim
)));
819 data
->sum
= isl_pw_qpolynomial_add(data
->sum
, pwqp
);
821 isl_basic_set_free(bset
);
825 isl_basic_set_free(bset
);
826 return isl_stat_error
;
829 static isl_stat
add_guarded_qp(__isl_take isl_set
*set
,
830 __isl_take isl_qpolynomial
*qp
, void *user
)
833 struct barvinok_summate_data
*data
= user
;
840 if (data
->wrapping
) {
841 unsigned nparam
= isl_set_dim(set
, isl_dim_param
);
842 isl_qpolynomial
*qp2
= isl_qpolynomial_copy(qp
);
843 set
= isl_set_move_dims(set
, isl_dim_param
, nparam
,
844 isl_dim_set
, 0, data
->n_in
);
845 qp2
= isl_qpolynomial_move_dims(qp2
, isl_dim_param
, nparam
,
846 isl_dim_in
, 0, data
->n_in
);
847 data
->e
= isl_qpolynomial_to_evalue(qp2
);
848 isl_qpolynomial_free(qp2
);
850 data
->e
= isl_qpolynomial_to_evalue(qp
);
854 evalue_section_array_init(&data
->sections
);
856 set
= isl_set_make_disjoint(set
);
857 set
= isl_set_compute_divs(set
);
859 r
= isl_set_foreach_basic_set(set
, &add_basic_guarded_qp
, data
);
861 free(data
->sections
.s
);
863 evalue_free(data
->e
);
866 isl_qpolynomial_free(qp
);
871 isl_qpolynomial_free(qp
);
872 return isl_stat_error
;
875 __isl_give isl_pw_qpolynomial
*isl_pw_qpolynomial_sum(
876 __isl_take isl_pw_qpolynomial
*pwqp
)
879 struct barvinok_summate_data data
;
880 int options_allocated
= 0;
890 nvar
= isl_pw_qpolynomial_dim(pwqp
, isl_dim_set
);
892 data
.dim
= isl_pw_qpolynomial_get_domain_space(pwqp
);
895 if (isl_space_is_params(data
.dim
))
896 isl_die(isl_pw_qpolynomial_get_ctx(pwqp
), isl_error_invalid
,
897 "input polynomial has no domain", goto error
);
898 data
.wrapping
= isl_space_is_wrapping(data
.dim
);
900 data
.dim
= isl_space_unwrap(data
.dim
);
901 data
.n_in
= isl_space_dim(data
.dim
, isl_dim_in
);
902 nvar
= isl_space_dim(data
.dim
, isl_dim_out
);
906 data
.dim
= isl_space_domain(data
.dim
);
908 return isl_pw_qpolynomial_reset_domain_space(pwqp
, data
.dim
);
910 data
.dim
= isl_space_from_domain(data
.dim
);
911 data
.dim
= isl_space_add_dims(data
.dim
, isl_dim_out
, 1);
912 data
.sum
= isl_pw_qpolynomial_zero(isl_space_copy(data
.dim
));
914 ctx
= isl_pw_qpolynomial_get_ctx(pwqp
);
915 data
.options
= isl_ctx_peek_barvinok_options(ctx
);
917 data
.options
= barvinok_options_new_with_defaults();
918 options_allocated
= 1;
921 if (isl_pw_qpolynomial_foreach_lifted_piece(pwqp
,
922 add_guarded_qp
, &data
) < 0)
925 if (options_allocated
)
926 barvinok_options_free(data
.options
);
928 isl_space_free(data
.dim
);
930 isl_pw_qpolynomial_free(pwqp
);
934 if (options_allocated
)
935 barvinok_options_free(data
.options
);
936 isl_pw_qpolynomial_free(pwqp
);
937 isl_space_free(data
.dim
);
938 isl_pw_qpolynomial_free(data
.sum
);
942 static isl_stat
pw_qpolynomial_sum(__isl_take isl_pw_qpolynomial
*pwqp
,
945 isl_union_pw_qpolynomial
**res
= (isl_union_pw_qpolynomial
**)user
;
946 isl_pw_qpolynomial
*sum
;
947 isl_union_pw_qpolynomial
*upwqp
;
949 sum
= isl_pw_qpolynomial_sum(pwqp
);
950 upwqp
= isl_union_pw_qpolynomial_from_pw_qpolynomial(sum
);
951 *res
= isl_union_pw_qpolynomial_add(*res
, upwqp
);
956 __isl_give isl_union_pw_qpolynomial
*isl_union_pw_qpolynomial_sum(
957 __isl_take isl_union_pw_qpolynomial
*upwqp
)
960 isl_union_pw_qpolynomial
*res
;
962 dim
= isl_union_pw_qpolynomial_get_space(upwqp
);
963 res
= isl_union_pw_qpolynomial_zero(dim
);
964 if (isl_union_pw_qpolynomial_foreach_pw_qpolynomial(upwqp
,
965 &pw_qpolynomial_sum
, &res
) < 0)
967 isl_union_pw_qpolynomial_free(upwqp
);
971 isl_union_pw_qpolynomial_free(upwqp
);
972 isl_union_pw_qpolynomial_free(res
);
976 static int join_compatible(__isl_keep isl_space
*space1
,
977 __isl_keep isl_space
*space2
)
980 m
= isl_space_match(space1
, isl_dim_param
, space2
, isl_dim_param
);
983 return isl_space_tuple_is_equal(space1
, isl_dim_out
,
987 /* Compute the intersection of the range of the map and the domain
988 * of the piecewise quasipolynomial and then sum the associated
989 * quasipolynomial over all elements in this intersection.
991 * We first introduce some unconstrained dimensions in the
992 * piecewise quasipolynomial, intersect the resulting domain
993 * with the wrapped map and then compute the sum.
995 __isl_give isl_pw_qpolynomial
*isl_map_apply_pw_qpolynomial(
996 __isl_take isl_map
*map
, __isl_take isl_pw_qpolynomial
*pwqp
)
1001 isl_space
*pwqp_dim
;
1005 ctx
= isl_map_get_ctx(map
);
1009 map_dim
= isl_map_get_space(map
);
1010 pwqp_dim
= isl_pw_qpolynomial_get_space(pwqp
);
1011 ok
= join_compatible(map_dim
, pwqp_dim
);
1012 isl_space_free(map_dim
);
1013 isl_space_free(pwqp_dim
);
1015 isl_die(ctx
, isl_error_invalid
, "incompatible dimensions",
1018 n_in
= isl_map_dim(map
, isl_dim_in
);
1019 pwqp
= isl_pw_qpolynomial_insert_dims(pwqp
, isl_dim_in
, 0, n_in
);
1021 dom
= isl_map_wrap(map
);
1022 pwqp
= isl_pw_qpolynomial_reset_domain_space(pwqp
,
1023 isl_set_get_space(dom
));
1025 pwqp
= isl_pw_qpolynomial_intersect_domain(pwqp
, dom
);
1026 pwqp
= isl_pw_qpolynomial_sum(pwqp
);
1031 isl_pw_qpolynomial_free(pwqp
);
1035 __isl_give isl_pw_qpolynomial
*isl_set_apply_pw_qpolynomial(
1036 __isl_take isl_set
*set
, __isl_take isl_pw_qpolynomial
*pwqp
)
1040 map
= isl_map_from_range(set
);
1041 pwqp
= isl_map_apply_pw_qpolynomial(map
, pwqp
);
1042 pwqp
= isl_pw_qpolynomial_project_domain_on_params(pwqp
);
1046 struct barvinok_apply_data
{
1047 isl_union_pw_qpolynomial
*upwqp
;
1048 isl_union_pw_qpolynomial
*res
;
1052 static isl_stat
pw_qpolynomial_apply(__isl_take isl_pw_qpolynomial
*pwqp
,
1056 isl_space
*pwqp_dim
;
1057 struct barvinok_apply_data
*data
= user
;
1060 map_dim
= isl_map_get_space(data
->map
);
1061 pwqp_dim
= isl_pw_qpolynomial_get_space(pwqp
);
1062 ok
= join_compatible(map_dim
, pwqp_dim
);
1063 isl_space_free(map_dim
);
1064 isl_space_free(pwqp_dim
);
1067 isl_union_pw_qpolynomial
*upwqp
;
1069 pwqp
= isl_map_apply_pw_qpolynomial(isl_map_copy(data
->map
),
1071 upwqp
= isl_union_pw_qpolynomial_from_pw_qpolynomial(pwqp
);
1072 data
->res
= isl_union_pw_qpolynomial_add(data
->res
, upwqp
);
1074 isl_pw_qpolynomial_free(pwqp
);
1079 static isl_stat
map_apply(__isl_take isl_map
*map
, void *user
)
1081 struct barvinok_apply_data
*data
= user
;
1085 r
= isl_union_pw_qpolynomial_foreach_pw_qpolynomial(data
->upwqp
,
1086 &pw_qpolynomial_apply
, data
);
1092 __isl_give isl_union_pw_qpolynomial
*isl_union_map_apply_union_pw_qpolynomial(
1093 __isl_take isl_union_map
*umap
,
1094 __isl_take isl_union_pw_qpolynomial
*upwqp
)
1097 struct barvinok_apply_data data
;
1099 upwqp
= isl_union_pw_qpolynomial_align_params(upwqp
,
1100 isl_union_map_get_space(umap
));
1101 umap
= isl_union_map_align_params(umap
,
1102 isl_union_pw_qpolynomial_get_space(upwqp
));
1105 dim
= isl_union_pw_qpolynomial_get_space(upwqp
);
1106 data
.res
= isl_union_pw_qpolynomial_zero(dim
);
1107 if (isl_union_map_foreach_map(umap
, &map_apply
, &data
) < 0)
1110 isl_union_map_free(umap
);
1111 isl_union_pw_qpolynomial_free(upwqp
);
1115 isl_union_map_free(umap
);
1116 isl_union_pw_qpolynomial_free(upwqp
);
1117 isl_union_pw_qpolynomial_free(data
.res
);
1121 struct barvinok_apply_set_data
{
1122 isl_union_pw_qpolynomial
*upwqp
;
1123 isl_union_pw_qpolynomial
*res
;
1127 static isl_stat
pw_qpolynomial_apply_set(__isl_take isl_pw_qpolynomial
*pwqp
,
1131 isl_space
*pwqp_dim
;
1132 struct barvinok_apply_set_data
*data
= user
;
1135 set_dim
= isl_set_get_space(data
->set
);
1136 pwqp_dim
= isl_pw_qpolynomial_get_space(pwqp
);
1137 ok
= join_compatible(set_dim
, pwqp_dim
);
1138 isl_space_free(set_dim
);
1139 isl_space_free(pwqp_dim
);
1142 isl_union_pw_qpolynomial
*upwqp
;
1144 pwqp
= isl_set_apply_pw_qpolynomial(isl_set_copy(data
->set
),
1146 upwqp
= isl_union_pw_qpolynomial_from_pw_qpolynomial(pwqp
);
1147 data
->res
= isl_union_pw_qpolynomial_add(data
->res
, upwqp
);
1149 isl_pw_qpolynomial_free(pwqp
);
1154 static isl_stat
set_apply(__isl_take isl_set
*set
, void *user
)
1156 struct barvinok_apply_set_data
*data
= user
;
1160 r
= isl_union_pw_qpolynomial_foreach_pw_qpolynomial(data
->upwqp
,
1161 &pw_qpolynomial_apply_set
, data
);
1167 __isl_give isl_union_pw_qpolynomial
*isl_union_set_apply_union_pw_qpolynomial(
1168 __isl_take isl_union_set
*uset
,
1169 __isl_take isl_union_pw_qpolynomial
*upwqp
)
1172 struct barvinok_apply_set_data data
;
1174 upwqp
= isl_union_pw_qpolynomial_align_params(upwqp
,
1175 isl_union_set_get_space(uset
));
1176 uset
= isl_union_set_align_params(uset
,
1177 isl_union_pw_qpolynomial_get_space(upwqp
));
1180 dim
= isl_union_pw_qpolynomial_get_space(upwqp
);
1181 data
.res
= isl_union_pw_qpolynomial_zero(dim
);
1182 if (isl_union_set_foreach_set(uset
, &set_apply
, &data
) < 0)
1185 isl_union_set_free(uset
);
1186 isl_union_pw_qpolynomial_free(upwqp
);
1190 isl_union_set_free(uset
);
1191 isl_union_pw_qpolynomial_free(upwqp
);
1192 isl_union_pw_qpolynomial_free(data
.res
);
1196 evalue
*evalue_sum(evalue
*E
, int nvar
, unsigned MaxRays
)
1199 struct barvinok_options
*options
= barvinok_options_new_with_defaults();
1200 options
->MaxRays
= MaxRays
;
1201 sum
= barvinok_summate(E
, nvar
, options
);
1202 barvinok_options_free(options
);
1206 evalue
*esum(evalue
*e
, int nvar
)
1209 struct barvinok_options
*options
= barvinok_options_new_with_defaults();
1210 sum
= barvinok_summate(e
, nvar
, options
);
1211 barvinok_options_free(options
);
1215 /* Turn unweighted counting problem into "weighted" counting problem
1216 * with weight equal to 1 and call barvinok_summate on this weighted problem.
1218 evalue
*barvinok_summate_unweighted(Polyhedron
*P
, Polyhedron
*C
,
1219 struct barvinok_options
*options
)
1225 if (emptyQ(P
) || emptyQ(C
))
1226 return evalue_zero();
1228 CA
= align_context(C
, P
->Dimension
, options
->MaxRays
);
1229 D
= DomainIntersection(P
, CA
, options
->MaxRays
);
1234 return evalue_zero();
1238 e
.x
.p
= new_enode(partition
, 2, P
->Dimension
);
1239 EVALUE_SET_DOMAIN(e
.x
.p
->arr
[0], D
);
1240 evalue_set_si(&e
.x
.p
->arr
[1], 1, 1);
1241 sum
= barvinok_summate(&e
, P
->Dimension
- C
->Dimension
, options
);
1242 free_evalue_refs(&e
);