summate.c: sum_with_equalities: extract out sum_over_polytope_0D
[barvinok.git] / summate.c
blobabbf22b854347e7c0cfa6b05c0f5fc3fc7a1c073
1 #include <isl/map.h>
2 #include <isl_set_polylib.h>
3 #include <barvinok/options.h>
4 #include <barvinok/util.h>
5 #include "bernoulli.h"
6 #include "euler.h"
7 #include "laurent.h"
8 #include "laurent_old.h"
9 #include "summate.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
24 * parameters.
26 static void transform_polynomial(evalue *E, Matrix *T, Matrix *CP,
27 unsigned nvar, unsigned nparam,
28 unsigned new_nvar, unsigned new_nparam)
30 int j;
31 evalue **subs;
33 subs = ALLOCN(evalue *, nvar+nparam);
35 for (j = 0; j < nvar; ++j) {
36 if (T)
37 subs[j] = affine2evalue(T->p[j], T->p[T->NbRows-1][T->NbColumns-1],
38 T->NbColumns-1);
39 else
40 subs[j] = evalue_var(j);
42 for (j = 0; j < nparam; ++j) {
43 if (CP)
44 subs[nvar+j] = affine2evalue(CP->p[j], CP->p[nparam][new_nparam],
45 new_nparam);
46 else
47 subs[nvar+j] = evalue_var(j);
48 evalue_shift_variables(subs[nvar+j], 0, new_nvar);
51 evalue_substitute(E, subs);
52 reduce_evalue(E);
54 for (j = 0; j < nvar+nparam; ++j)
55 evalue_free(subs[j]);
56 free(subs);
59 /* Compute the sum of the quasi-polynomial E
60 * over a 0D (non-empty, but possibly parametric) polytope P.
62 * Consumes P and E.
64 * We simply return a partition evalue with P as domain and E as value.
66 static evalue *sum_over_polytope_0D(Polyhedron *P, evalue *E)
68 evalue *sum;
70 sum = ALLOC(evalue);
71 value_init(sum->d);
72 sum->x.p = new_enode(partition, 2, P->Dimension);
73 EVALUE_SET_DOMAIN(sum->x.p->arr[0], P);
74 value_clear(sum->x.p->arr[1].d);
75 sum->x.p->arr[1] = *E;
76 free(E);
78 return sum;
81 static evalue *sum_with_equalities(Polyhedron *P, evalue *E,
82 unsigned nvar, struct evalue_section_array *sections,
83 struct barvinok_options *options,
84 evalue *(*base)(Polyhedron *P, evalue *E, unsigned nvar,
85 struct evalue_section_array *sections,
86 struct barvinok_options *options))
88 unsigned dim = P->Dimension;
89 unsigned new_dim, new_nparam;
90 Matrix *T = NULL, *CP = NULL;
91 evalue *sum;
93 if (emptyQ(P))
94 return evalue_zero();
96 assert(P->NbEq > 0);
98 remove_all_equalities(&P, NULL, &CP, &T, dim-nvar, options->MaxRays);
100 if (emptyQ(P)) {
101 Polyhedron_Free(P);
102 return evalue_zero();
105 new_nparam = CP ? CP->NbColumns-1 : dim - nvar;
106 new_dim = T ? T->NbColumns-1 : nvar + new_nparam;
108 /* We can avoid these substitutions if E is a constant */
109 E = evalue_dup(E);
110 transform_polynomial(E, T, CP, nvar, dim-nvar,
111 new_dim-new_nparam, new_nparam);
113 if (new_dim-new_nparam > 0) {
114 sum = base(P, E, new_dim-new_nparam, sections, options);
115 evalue_free(E);
116 Polyhedron_Free(P);
117 } else {
118 sum = sum_over_polytope_0D(P, E);
121 if (CP) {
122 evalue_backsubstitute(sum, CP, options->MaxRays);
123 Matrix_Free(CP);
126 if (T)
127 Matrix_Free(T);
129 return sum;
132 static evalue *sum_over_polytope_with_equalities(Polyhedron *P, evalue *E,
133 unsigned nvar, struct evalue_section_array *sections,
134 struct barvinok_options *options)
136 return sum_with_equalities(P, E, nvar, sections, options,
137 &barvinok_sum_over_polytope);
140 static evalue *sum_base(Polyhedron *P, evalue *E, unsigned nvar,
141 struct barvinok_options *options);
143 static evalue *sum_base_wrap(Polyhedron *P, evalue *E, unsigned nvar,
144 struct evalue_section_array *sections, struct barvinok_options *options)
146 return sum_base(P, E, nvar, options);
149 static evalue *sum_base_with_equalities(Polyhedron *P, evalue *E,
150 unsigned nvar, struct barvinok_options *options)
152 return sum_with_equalities(P, E, nvar, NULL, options, &sum_base_wrap);
155 /* The substitutions in sum_step_polynomial may have reintroduced equalities
156 * (in particular, one of the floor expressions may be equal to one of
157 * the variables), so we need to check for them again.
159 static evalue *sum_base(Polyhedron *P, evalue *E, unsigned nvar,
160 struct barvinok_options *options)
162 if (P->NbEq)
163 return sum_base_with_equalities(P, E, nvar, options);
164 if (options->summation == BV_SUM_EULER)
165 return euler_summate(P, E, nvar, options);
166 else if (options->summation == BV_SUM_LAURENT)
167 return laurent_summate(P, E, nvar, options);
168 else if (options->summation == BV_SUM_LAURENT_OLD)
169 return laurent_summate_old(P, E, nvar, options);
170 assert(0);
173 /* Count the number of non-zero terms in e when viewed as a polynomial
174 * in only the first nvar variables. "count" is the number counted
175 * so far.
177 static int evalue_count_terms(const evalue *e, unsigned nvar, int count)
179 int i;
181 if (EVALUE_IS_ZERO(*e))
182 return count;
184 if (value_zero_p(e->d))
185 assert(e->x.p->type == polynomial);
186 if (value_notzero_p(e->d) || e->x.p->pos >= nvar+1)
187 return count+1;
189 for (i = 0; i < e->x.p->size; ++i)
190 count = evalue_count_terms(&e->x.p->arr[i], nvar, count);
192 return count;
195 /* Create placeholder structure for unzipping.
196 * A "polynomial" is created with size terms in variable pos,
197 * with each term having itself as coefficient.
199 static evalue *create_placeholder(int size, int pos)
201 int i, j;
202 evalue *E = ALLOC(evalue);
203 value_init(E->d);
204 E->x.p = new_enode(polynomial, size, pos+1);
205 for (i = 0; i < size; ++i) {
206 E->x.p->arr[i].x.p = new_enode(polynomial, i+1, pos+1);
207 for (j = 0; j < i; ++j)
208 evalue_set_si(&E->x.p->arr[i].x.p->arr[j], 0, 1);
209 evalue_set_si(&E->x.p->arr[i].x.p->arr[i], 1, 1);
211 return E;
214 /* Interchange each non-zero term in e (when viewed as a polynomial
215 * in only the first nvar variables) with a placeholder in ph (created
216 * by create_placeholder), resulting in two polynomials in the
217 * placeholder variable such that for each non-zero term in e
218 * there is a power of the placeholder variable such that the factors
219 * in the first nvar variables form the coefficient of that power in
220 * the first polynomial (e) and the factors in the remaining variables
221 * form the coefficient of that power in the second polynomial (ph).
223 static int evalue_unzip_terms(evalue *e, evalue *ph, unsigned nvar, int count)
225 int i;
227 if (EVALUE_IS_ZERO(*e))
228 return count;
230 if (value_zero_p(e->d))
231 assert(e->x.p->type == polynomial);
232 if (value_notzero_p(e->d) || e->x.p->pos >= nvar+1) {
233 evalue t = *e;
234 *e = ph->x.p->arr[count];
235 ph->x.p->arr[count] = t;
236 return count+1;
239 for (i = 0; i < e->x.p->size; ++i)
240 count = evalue_unzip_terms(&e->x.p->arr[i], ph, nvar, count);
242 return count;
245 /* Remove n variables at pos (0-based) from the polyhedron P.
246 * Each of these variables is assumed to be completely free,
247 * i.e., there is a line in the polyhedron corresponding to
248 * each of these variables.
250 static Polyhedron *Polyhedron_Remove_Columns(Polyhedron *P, unsigned pos,
251 unsigned n)
253 int i, j;
254 unsigned NbConstraints = 0;
255 unsigned NbRays = 0;
256 Polyhedron *Q;
258 if (n == 0)
259 return P;
261 assert(pos <= P->Dimension);
263 if (POL_HAS(P, POL_INEQUALITIES))
264 NbConstraints = P->NbConstraints;
265 if (POL_HAS(P, POL_POINTS))
266 NbRays = P->NbRays - n;
268 Q = Polyhedron_Alloc(P->Dimension - n, NbConstraints, NbRays);
269 if (POL_HAS(P, POL_INEQUALITIES)) {
270 Q->NbEq = P->NbEq;
271 for (i = 0; i < P->NbConstraints; ++i) {
272 Vector_Copy(P->Constraint[i], Q->Constraint[i], 1+pos);
273 Vector_Copy(P->Constraint[i]+1+pos+n, Q->Constraint[i]+1+pos,
274 Q->Dimension-pos+1);
277 if (POL_HAS(P, POL_POINTS)) {
278 Q->NbBid = P->NbBid - n;
279 for (i = 0; i < n; ++i)
280 value_set_si(Q->Ray[i][1+pos+i], 1);
281 for (i = 0, j = 0; i < P->NbRays; ++i) {
282 int line = First_Non_Zero(P->Ray[i], 1+P->Dimension+1);
283 assert(line != -1);
284 if (line-1 >= pos && line-1 < pos+n) {
285 ++j;
286 assert(j <= n);
287 continue;
289 assert(i-j < Q->NbRays);
290 Vector_Copy(P->Ray[i], Q->Ray[i-j], 1+pos);
291 Vector_Copy(P->Ray[i]+1+pos+n, Q->Ray[i-j]+1+pos,
292 Q->Dimension-pos+1);
295 POL_SET(Q, POL_VALID);
296 if (POL_HAS(P, POL_INEQUALITIES))
297 POL_SET(Q, POL_INEQUALITIES);
298 if (POL_HAS(P, POL_POINTS))
299 POL_SET(Q, POL_POINTS);
300 if (POL_HAS(P, POL_VERTICES))
301 POL_SET(Q, POL_VERTICES);
302 return Q;
305 /* Remove n variables at pos (0-based) from the union of polyhedra P.
306 * Each of these variables is assumed to be completely free,
307 * i.e., there is a line in the polyhedron corresponding to
308 * each of these variables.
310 static Polyhedron *Domain_Remove_Columns(Polyhedron *P, unsigned pos,
311 unsigned n)
313 Polyhedron *R;
314 Polyhedron **next = &R;
316 for (; P; P = P->next) {
317 *next = Polyhedron_Remove_Columns(P, pos, n);
318 next = &(*next)->next;
320 return R;
323 /* Drop n parameters starting at first from partition evalue e */
324 static void drop_parameters(evalue *e, int first, int n)
326 int i;
328 if (EVALUE_IS_ZERO(*e))
329 return;
331 assert(value_zero_p(e->d) && e->x.p->type == partition);
332 for (i = 0; i < e->x.p->size/2; ++i) {
333 Polyhedron *P = EVALUE_DOMAIN(e->x.p->arr[2*i]);
334 Polyhedron *Q = Domain_Remove_Columns(P, first, n);
335 EVALUE_SET_DOMAIN(e->x.p->arr[2*i], Q);
336 Domain_Free(P);
337 evalue_shift_variables(&e->x.p->arr[2*i+1], first, -n);
339 e->x.p->pos -= n;
342 static void extract_term_into(const evalue *src, int var, int exp, evalue *dst)
344 int i;
346 if (value_notzero_p(src->d) ||
347 src->x.p->type != polynomial ||
348 src->x.p->pos > var+1) {
349 if (exp == 0)
350 evalue_copy(dst, src);
351 else
352 evalue_set_si(dst, 0, 1);
353 return;
356 if (src->x.p->pos == var+1) {
357 if (src->x.p->size > exp)
358 evalue_copy(dst, &src->x.p->arr[exp]);
359 else
360 evalue_set_si(dst, 0, 1);
361 return;
364 dst->x.p = new_enode(polynomial, src->x.p->size, src->x.p->pos);
365 for (i = 0; i < src->x.p->size; ++i)
366 extract_term_into(&src->x.p->arr[i], var, exp,
367 &dst->x.p->arr[i]);
370 /* Extract the coefficient of var^exp.
372 static evalue *extract_term(const evalue *e, int var, int exp)
374 int i;
375 evalue *res;
377 if (EVALUE_IS_ZERO(*e))
378 return evalue_zero();
380 assert(value_zero_p(e->d) && e->x.p->type == partition);
381 res = ALLOC(evalue);
382 value_init(res->d);
383 res->x.p = new_enode(partition, e->x.p->size, e->x.p->pos);
384 for (i = 0; i < e->x.p->size/2; ++i) {
385 EVALUE_SET_DOMAIN(res->x.p->arr[2*i],
386 Domain_Copy(EVALUE_DOMAIN(e->x.p->arr[2*i])));
387 extract_term_into(&e->x.p->arr[2*i+1], var, exp,
388 &res->x.p->arr[2*i+1]);
389 reduce_evalue(&res->x.p->arr[2*i+1]);
391 return res;
394 /* Insert n free variables at pos (0-based) in the polyhedron P.
396 static Polyhedron *Polyhedron_Insert_Columns(Polyhedron *P, unsigned pos,
397 unsigned n)
399 int i;
400 unsigned NbConstraints = 0;
401 unsigned NbRays = 0;
402 Polyhedron *Q;
404 if (!P)
405 return P;
406 if (n == 0)
407 return P;
409 assert(pos <= P->Dimension);
411 if (POL_HAS(P, POL_INEQUALITIES))
412 NbConstraints = P->NbConstraints;
413 if (POL_HAS(P, POL_POINTS))
414 NbRays = P->NbRays + n;
416 Q = Polyhedron_Alloc(P->Dimension+n, NbConstraints, NbRays);
417 if (POL_HAS(P, POL_INEQUALITIES)) {
418 Q->NbEq = P->NbEq;
419 for (i = 0; i < P->NbConstraints; ++i) {
420 Vector_Copy(P->Constraint[i], Q->Constraint[i], 1+pos);
421 Vector_Copy(P->Constraint[i]+1+pos, Q->Constraint[i]+1+pos+n,
422 P->Dimension-pos+1);
425 if (POL_HAS(P, POL_POINTS)) {
426 Q->NbBid = P->NbBid + n;
427 for (i = 0; i < n; ++i)
428 value_set_si(Q->Ray[i][1+pos+i], 1);
429 for (i = 0; i < P->NbRays; ++i) {
430 Vector_Copy(P->Ray[i], Q->Ray[n+i], 1+pos);
431 Vector_Copy(P->Ray[i]+1+pos, Q->Ray[n+i]+1+pos+n,
432 P->Dimension-pos+1);
435 POL_SET(Q, POL_VALID);
436 if (POL_HAS(P, POL_INEQUALITIES))
437 POL_SET(Q, POL_INEQUALITIES);
438 if (POL_HAS(P, POL_POINTS))
439 POL_SET(Q, POL_POINTS);
440 if (POL_HAS(P, POL_VERTICES))
441 POL_SET(Q, POL_VERTICES);
442 return Q;
445 /* Perform summation of e over a list of 1 or more factors F, with context C.
446 * nvar is the total number of variables in the remaining factors.
447 * extra is the number of placeholder parameters introduced in e,
448 * but not (yet) in F or C.
450 * If there is only one factor left, F is intersected with the
451 * context C, the placeholder variables are added, and then
452 * e is summed over the resulting parametric polytope.
454 * If there is more than one factor left, we create two polynomials
455 * in a new placeholder variable (which is placed after the regular
456 * parameters, but before any previously introduced placeholder
457 * variables) that has the factors of the variables in the first
458 * factor of F and the factor of the remaining variables of
459 * each term as its coefficients.
460 * These two polynomials are then summed over their domains
461 * and afterwards the results are combined and the placeholder
462 * variable is removed again.
464 static evalue *sum_factors(Polyhedron *F, Polyhedron *C, evalue *e,
465 unsigned nvar, unsigned extra,
466 struct barvinok_options *options)
468 Polyhedron *P = F;
469 unsigned nparam = C->Dimension;
470 unsigned F_var = F->Dimension - C->Dimension;
471 int i, n;
472 evalue *s1, *s2, *s;
473 evalue *ph;
475 if (!F->next) {
476 Polyhedron *CA = align_context(C, nvar+nparam, options->MaxRays);
477 Polyhedron *P = DomainIntersection(F, CA, options->MaxRays);
478 Polyhedron *Q = Polyhedron_Insert_Columns(P, nvar+nparam, extra);
479 Polyhedron_Free(CA);
480 Polyhedron_Free(F);
481 Polyhedron_Free(P);
482 evalue *sum = sum_base(Q, e, nvar, options);
483 Polyhedron_Free(Q);
484 return sum;
487 n = evalue_count_terms(e, F_var, 0);
488 ph = create_placeholder(n, nvar+nparam);
489 evalue_shift_variables(e, nvar+nparam, 1);
490 evalue_unzip_terms(e, ph, F_var, 0);
491 evalue_shift_variables(e, nvar, -(nvar-F_var));
492 evalue_reorder_terms(ph);
493 evalue_shift_variables(ph, 0, -F_var);
495 s2 = sum_factors(F->next, C, ph, nvar-F_var, extra+1, options);
496 evalue_free(ph);
497 F->next = NULL;
498 s1 = sum_factors(F, C, e, F_var, extra+1, options);
500 if (n == 1) {
501 /* remove placeholder "polynomial" */
502 reduce_evalue(s1);
503 emul(s1, s2);
504 evalue_free(s1);
505 drop_parameters(s2, nparam, 1);
506 return s2;
509 s = evalue_zero();
510 for (i = 0; i < n; ++i) {
511 evalue *t1, *t2;
512 t1 = extract_term(s1, nparam, i);
513 t2 = extract_term(s2, nparam, i);
514 emul(t1, t2);
515 eadd(t2, s);
516 evalue_free(t1);
517 evalue_free(t2);
519 evalue_free(s1);
520 evalue_free(s2);
522 drop_parameters(s, nparam, 1);
523 return s;
526 /* Perform summation over a product of factors F, obtained using
527 * variable transformation T from the original problem specification.
529 * We first perform the corresponding transformation on the polynomial E,
530 * compute the common context over all factors and then perform
531 * the actual summation over the factors.
533 static evalue *sum_product(Polyhedron *F, evalue *E, Matrix *T, unsigned nparam,
534 struct barvinok_options *options)
536 int i;
537 Matrix *T2;
538 unsigned nvar = T->NbRows;
539 Polyhedron *C;
540 evalue *sum;
542 assert(nvar == T->NbColumns);
543 T2 = Matrix_Alloc(nvar+1, nvar+1);
544 for (i = 0; i < nvar; ++i)
545 Vector_Copy(T->p[i], T2->p[i], nvar);
546 value_set_si(T2->p[nvar][nvar], 1);
548 transform_polynomial(E, T2, NULL, nvar, nparam, nvar, nparam);
550 C = Factor_Context(F, nparam, options->MaxRays);
551 if (F->Dimension == nparam) {
552 Polyhedron *T = F;
553 F = F->next;
554 T->next = NULL;
555 Polyhedron_Free(T);
557 sum = sum_factors(F, C, E, nvar, 0, options);
559 Polyhedron_Free(C);
560 Matrix_Free(T2);
561 Matrix_Free(T);
562 return sum;
565 /* Add two constraints corresponding to floor = floor(e/d),
567 * e - d t >= 0
568 * -e + d t + d-1 >= 0
570 * e is assumed to be an affine expression.
572 Polyhedron *add_floor_var(Polyhedron *P, unsigned nvar, const evalue *floor,
573 struct barvinok_options *options)
575 int i;
576 unsigned dim = P->Dimension+1;
577 Matrix *M = Matrix_Alloc(P->NbConstraints+2, 2+dim);
578 Polyhedron *CP;
579 Value *d = &M->p[0][1+nvar];
580 evalue_extract_affine(floor, M->p[0]+1, M->p[0]+1+dim, d);
581 value_oppose(*d, *d);
582 value_set_si(M->p[0][0], 1);
583 value_set_si(M->p[1][0], 1);
584 Vector_Oppose(M->p[0]+1, M->p[1]+1, M->NbColumns-1);
585 value_subtract(M->p[1][1+dim], M->p[1][1+dim], *d);
586 value_decrement(M->p[1][1+dim], M->p[1][1+dim]);
588 for (i = 0; i < P->NbConstraints; ++i) {
589 Vector_Copy(P->Constraint[i], M->p[i+2], 1+nvar);
590 Vector_Copy(P->Constraint[i]+1+nvar, M->p[i+2]+1+nvar+1, dim-nvar-1+1);
593 CP = Constraints2Polyhedron(M, options->MaxRays);
594 Matrix_Free(M);
595 return CP;
598 static evalue *evalue_add(evalue *a, evalue *b)
600 if (!a)
601 return b;
602 if (!b)
603 return a;
604 eadd(a, b);
605 evalue_free(a);
606 return b;
609 /* Compute sum of a step-polynomial over a polytope by grouping
610 * terms containing the same floor-expressions and introducing
611 * new variables for each such expression.
612 * In particular, while there is any floor-expression left,
613 * the step-polynomial is split into a polynomial containing
614 * the expression, which is then converted to a new variable,
615 * and a polynomial not containing the expression.
617 static evalue *sum_step_polynomial(Polyhedron *P, evalue *E, unsigned nvar,
618 struct barvinok_options *options)
620 evalue *floor;
621 evalue *cur = E;
622 evalue *sum = NULL;
623 evalue *t;
625 while ((floor = evalue_outer_floor(cur))) {
626 Polyhedron *CP;
627 evalue *converted;
628 evalue *converted_floor;
630 /* Ignore floors that do not depend on variables. */
631 if (value_notzero_p(floor->d) || floor->x.p->pos >= nvar+1)
632 break;
634 converted = evalue_dup(cur);
635 converted_floor = evalue_dup(floor);
636 evalue_shift_variables(converted, nvar, 1);
637 evalue_shift_variables(converted_floor, nvar, 1);
638 evalue_replace_floor(converted, converted_floor, nvar);
639 CP = add_floor_var(P, nvar, converted_floor, options);
640 evalue_free(converted_floor);
641 t = sum_step_polynomial(CP, converted, nvar+1, options);
642 evalue_free(converted);
643 Polyhedron_Free(CP);
644 sum = evalue_add(t, sum);
646 if (cur == E)
647 cur = evalue_dup(cur);
648 evalue_drop_floor(cur, floor);
649 evalue_free(floor);
651 if (floor) {
652 evalue_floor2frac(cur);
653 evalue_free(floor);
656 if (EVALUE_IS_ZERO(*cur))
657 t = NULL;
658 else {
659 Matrix *T;
660 unsigned nparam = P->Dimension - nvar;
661 Polyhedron *F = Polyhedron_Factor(P, nparam, &T, options->MaxRays);
662 if (!F)
663 t = sum_base(P, cur, nvar, options);
664 else {
665 if (cur == E)
666 cur = evalue_dup(cur);
667 t = sum_product(F, cur, T, nparam, options);
671 if (E != cur)
672 evalue_free(cur);
674 return evalue_add(t, sum);
677 evalue *barvinok_sum_over_polytope(Polyhedron *P, evalue *E, unsigned nvar,
678 struct evalue_section_array *sections,
679 struct barvinok_options *options)
681 if (P->NbEq)
682 return sum_over_polytope_with_equalities(P, E, nvar, sections, options);
684 if (options->summation == BV_SUM_BERNOULLI)
685 return bernoulli_summate(P, E, nvar, sections, options);
686 else if (options->summation == BV_SUM_BOX)
687 return box_summate(P, E, nvar, options->MaxRays);
689 evalue_frac2floor2(E, 0);
691 return sum_step_polynomial(P, E, nvar, options);
694 evalue *barvinok_summate(evalue *e, int nvar, struct barvinok_options *options)
696 int i;
697 struct evalue_section_array sections;
698 evalue *sum;
700 assert(nvar >= 0);
701 if (nvar == 0 || EVALUE_IS_ZERO(*e))
702 return evalue_dup(e);
704 assert(value_zero_p(e->d));
705 assert(e->x.p->type == partition);
707 evalue_section_array_init(&sections);
708 sum = evalue_zero();
710 for (i = 0; i < e->x.p->size/2; ++i) {
711 Polyhedron *D;
712 for (D = EVALUE_DOMAIN(e->x.p->arr[2*i]); D; D = D->next) {
713 Polyhedron *next = D->next;
714 evalue *tmp;
715 D->next = NULL;
717 tmp = barvinok_sum_over_polytope(D, &e->x.p->arr[2*i+1], nvar,
718 &sections, options);
719 assert(tmp);
720 eadd(tmp, sum);
721 evalue_free(tmp);
723 D->next = next;
727 free(sections.s);
729 reduce_evalue(sum);
730 return sum;
733 static __isl_give isl_pw_qpolynomial *add_unbounded_guarded_qp(
734 __isl_take isl_pw_qpolynomial *sum,
735 __isl_take isl_basic_set *bset, __isl_take isl_qpolynomial *qp)
737 int zero;
739 if (!sum || !bset || !qp)
740 goto error;
742 zero = isl_qpolynomial_is_zero(qp);
743 if (zero < 0)
744 goto error;
746 if (!zero) {
747 isl_space *dim;
748 isl_set *set;
749 isl_pw_qpolynomial *pwqp;
751 dim = isl_pw_qpolynomial_get_domain_space(sum);
752 set = isl_set_from_basic_set(isl_basic_set_copy(bset));
753 set = isl_map_domain(isl_map_from_range(set));
754 set = isl_set_reset_space(set, isl_space_copy(dim));
755 pwqp = isl_pw_qpolynomial_alloc(set, isl_qpolynomial_nan_on_domain(dim));
756 sum = isl_pw_qpolynomial_add(sum, pwqp);
759 isl_basic_set_free(bset);
760 isl_qpolynomial_free(qp);
761 return sum;
762 error:
763 isl_basic_set_free(bset);
764 isl_qpolynomial_free(qp);
765 isl_pw_qpolynomial_free(sum);
766 return NULL;
769 struct barvinok_summate_data {
770 isl_space *dim;
771 __isl_take isl_qpolynomial *qp;
772 isl_pw_qpolynomial *sum;
773 int n_in;
774 int wrapping;
775 evalue *e;
776 struct evalue_section_array sections;
777 struct barvinok_options *options;
780 static int add_basic_guarded_qp(__isl_take isl_basic_set *bset, void *user)
782 struct barvinok_summate_data *data = user;
783 Polyhedron *P;
784 evalue *tmp;
785 isl_pw_qpolynomial *pwqp;
786 int bounded;
787 unsigned nparam = isl_basic_set_dim(bset, isl_dim_param);
788 unsigned nvar = isl_basic_set_dim(bset, isl_dim_set);
789 isl_space *dim;
791 if (!bset)
792 return -1;
794 bounded = isl_basic_set_is_bounded(bset);
795 if (bounded < 0)
796 goto error;
798 if (!bounded) {
799 data->sum = add_unbounded_guarded_qp(data->sum, bset,
800 isl_qpolynomial_copy(data->qp));
801 return 0;
804 dim = isl_basic_set_get_space(bset);
805 dim = isl_space_domain(isl_space_from_range(dim));
807 P = isl_basic_set_to_polylib(bset);
808 tmp = barvinok_sum_over_polytope(P, data->e, nvar,
809 &data->sections, data->options);
810 Polyhedron_Free(P);
811 assert(tmp);
812 pwqp = isl_pw_qpolynomial_from_evalue(dim, tmp);
813 evalue_free(tmp);
814 pwqp = isl_pw_qpolynomial_reset_domain_space(pwqp,
815 isl_space_domain(isl_space_copy(data->dim)));
816 data->sum = isl_pw_qpolynomial_add(data->sum, pwqp);
818 isl_basic_set_free(bset);
820 return 0;
821 error:
822 isl_basic_set_free(bset);
823 return -1;
826 static int add_guarded_qp(__isl_take isl_set *set, __isl_take isl_qpolynomial *qp,
827 void *user)
829 int r;
830 struct barvinok_summate_data *data = user;
832 if (!set || !qp)
833 goto error;
835 data->qp = qp;
837 if (data->wrapping) {
838 unsigned nparam = isl_set_dim(set, isl_dim_param);
839 isl_qpolynomial *qp2 = isl_qpolynomial_copy(qp);
840 set = isl_set_move_dims(set, isl_dim_param, nparam,
841 isl_dim_set, 0, data->n_in);
842 qp2 = isl_qpolynomial_move_dims(qp2, isl_dim_param, nparam,
843 isl_dim_in, 0, data->n_in);
844 data->e = isl_qpolynomial_to_evalue(qp2);
845 isl_qpolynomial_free(qp2);
846 } else
847 data->e = isl_qpolynomial_to_evalue(qp);
848 if (!data->e)
849 goto error;
851 evalue_section_array_init(&data->sections);
853 set = isl_set_make_disjoint(set);
854 set = isl_set_compute_divs(set);
856 r = isl_set_foreach_basic_set(set, &add_basic_guarded_qp, data);
858 free(data->sections.s);
860 evalue_free(data->e);
862 isl_set_free(set);
863 isl_qpolynomial_free(qp);
865 return r;
866 error:
867 isl_set_free(set);
868 isl_qpolynomial_free(qp);
869 return -1;
872 __isl_give isl_pw_qpolynomial *isl_pw_qpolynomial_sum(
873 __isl_take isl_pw_qpolynomial *pwqp)
875 isl_ctx *ctx;
876 struct barvinok_summate_data data;
877 int options_allocated = 0;
878 int nvar;
880 data.dim = NULL;
881 data.options = NULL;
883 if (!pwqp)
884 return NULL;
886 nvar = isl_pw_qpolynomial_dim(pwqp, isl_dim_set);
888 data.dim = isl_pw_qpolynomial_get_domain_space(pwqp);
889 data.wrapping = isl_space_is_wrapping(data.dim);
890 if (data.wrapping) {
891 data.dim = isl_space_unwrap(data.dim);
892 data.n_in = isl_space_dim(data.dim, isl_dim_in);
893 nvar = isl_space_dim(data.dim, isl_dim_out);
894 } else
895 data.n_in = 0;
897 data.dim = isl_space_domain(data.dim);
898 if (nvar == 0)
899 return isl_pw_qpolynomial_reset_domain_space(pwqp, data.dim);
901 data.dim = isl_space_from_domain(data.dim);
902 data.dim = isl_space_add_dims(data.dim, isl_dim_out, 1);
903 data.sum = isl_pw_qpolynomial_zero(isl_space_copy(data.dim));
905 ctx = isl_pw_qpolynomial_get_ctx(pwqp);
906 data.options = isl_ctx_peek_barvinok_options(ctx);
907 if (!data.options) {
908 data.options = barvinok_options_new_with_defaults();
909 options_allocated = 1;
912 if (isl_pw_qpolynomial_foreach_lifted_piece(pwqp,
913 add_guarded_qp, &data) < 0)
914 goto error;
916 if (options_allocated)
917 barvinok_options_free(data.options);
919 isl_space_free(data.dim);
921 isl_pw_qpolynomial_free(pwqp);
923 return data.sum;
924 error:
925 if (options_allocated)
926 barvinok_options_free(data.options);
927 isl_pw_qpolynomial_free(pwqp);
928 isl_space_free(data.dim);
929 isl_pw_qpolynomial_free(data.sum);
930 return NULL;
933 static int pw_qpolynomial_sum(__isl_take isl_pw_qpolynomial *pwqp, void *user)
935 isl_union_pw_qpolynomial **res = (isl_union_pw_qpolynomial **)user;
936 isl_pw_qpolynomial *sum;
938 sum = isl_pw_qpolynomial_sum(pwqp);
939 *res = isl_union_pw_qpolynomial_add_pw_qpolynomial(*res, sum);
941 return 0;
944 __isl_give isl_union_pw_qpolynomial *isl_union_pw_qpolynomial_sum(
945 __isl_take isl_union_pw_qpolynomial *upwqp)
947 isl_space *dim;
948 isl_union_pw_qpolynomial *res;
950 dim = isl_union_pw_qpolynomial_get_space(upwqp);
951 res = isl_union_pw_qpolynomial_zero(dim);
952 if (isl_union_pw_qpolynomial_foreach_pw_qpolynomial(upwqp,
953 &pw_qpolynomial_sum, &res) < 0)
954 goto error;
955 isl_union_pw_qpolynomial_free(upwqp);
957 return res;
958 error:
959 isl_union_pw_qpolynomial_free(upwqp);
960 isl_union_pw_qpolynomial_free(res);
961 return NULL;
964 static int join_compatible(__isl_keep isl_space *dim1, __isl_keep isl_space *dim2)
966 int m;
967 m = isl_space_match(dim1, isl_dim_param, dim2, isl_dim_param);
968 if (m < 0 || !m)
969 return m;
970 return isl_space_tuple_match(dim1, isl_dim_out, dim2, isl_dim_in);
973 /* Compute the intersection of the range of the map and the domain
974 * of the piecewise quasipolynomial and then sum the associated
975 * quasipolynomial over all elements in this intersection.
977 * We first introduce some unconstrained dimensions in the
978 * piecewise quasipolynomial, intersect the resulting domain
979 * with the wrapped map and then compute the sum.
981 __isl_give isl_pw_qpolynomial *isl_map_apply_pw_qpolynomial(
982 __isl_take isl_map *map, __isl_take isl_pw_qpolynomial *pwqp)
984 isl_ctx *ctx;
985 isl_set *dom;
986 isl_space *map_dim;
987 isl_space *pwqp_dim;
988 unsigned n_in;
989 int ok;
991 ctx = isl_map_get_ctx(map);
992 if (!ctx)
993 goto error;
995 map_dim = isl_map_get_space(map);
996 pwqp_dim = isl_pw_qpolynomial_get_space(pwqp);
997 ok = join_compatible(map_dim, pwqp_dim);
998 isl_space_free(map_dim);
999 isl_space_free(pwqp_dim);
1000 if (!ok)
1001 isl_die(ctx, isl_error_invalid, "incompatible dimensions",
1002 goto error);
1004 n_in = isl_map_dim(map, isl_dim_in);
1005 pwqp = isl_pw_qpolynomial_insert_dims(pwqp, isl_dim_in, 0, n_in);
1007 dom = isl_map_wrap(map);
1008 pwqp = isl_pw_qpolynomial_reset_domain_space(pwqp,
1009 isl_set_get_space(dom));
1011 pwqp = isl_pw_qpolynomial_intersect_domain(pwqp, dom);
1012 pwqp = isl_pw_qpolynomial_sum(pwqp);
1014 return pwqp;
1015 error:
1016 isl_map_free(map);
1017 isl_pw_qpolynomial_free(pwqp);
1018 return NULL;
1021 __isl_give isl_pw_qpolynomial *isl_set_apply_pw_qpolynomial(
1022 __isl_take isl_set *set, __isl_take isl_pw_qpolynomial *pwqp)
1024 isl_map *map;
1026 map = isl_map_from_range(set);
1027 pwqp = isl_map_apply_pw_qpolynomial(map, pwqp);
1028 pwqp = isl_pw_qpolynomial_project_domain_on_params(pwqp);
1029 return pwqp;
1032 struct barvinok_apply_data {
1033 isl_union_pw_qpolynomial *upwqp;
1034 isl_union_pw_qpolynomial *res;
1035 isl_map *map;
1038 static int pw_qpolynomial_apply(__isl_take isl_pw_qpolynomial *pwqp, void *user)
1040 isl_space *map_dim;
1041 isl_space *pwqp_dim;
1042 struct barvinok_apply_data *data = user;
1043 int ok;
1045 map_dim = isl_map_get_space(data->map);
1046 pwqp_dim = isl_pw_qpolynomial_get_space(pwqp);
1047 ok = join_compatible(map_dim, pwqp_dim);
1048 isl_space_free(map_dim);
1049 isl_space_free(pwqp_dim);
1051 if (ok) {
1052 pwqp = isl_map_apply_pw_qpolynomial(isl_map_copy(data->map),
1053 pwqp);
1054 data->res = isl_union_pw_qpolynomial_add_pw_qpolynomial(
1055 data->res, pwqp);
1056 } else
1057 isl_pw_qpolynomial_free(pwqp);
1059 return 0;
1062 static int map_apply(__isl_take isl_map *map, void *user)
1064 struct barvinok_apply_data *data = user;
1065 int r;
1067 data->map = map;
1068 r = isl_union_pw_qpolynomial_foreach_pw_qpolynomial(data->upwqp,
1069 &pw_qpolynomial_apply, data);
1071 isl_map_free(map);
1072 return r;
1075 __isl_give isl_union_pw_qpolynomial *isl_union_map_apply_union_pw_qpolynomial(
1076 __isl_take isl_union_map *umap,
1077 __isl_take isl_union_pw_qpolynomial *upwqp)
1079 isl_space *dim;
1080 struct barvinok_apply_data data;
1082 upwqp = isl_union_pw_qpolynomial_align_params(upwqp,
1083 isl_union_map_get_space(umap));
1084 umap = isl_union_map_align_params(umap,
1085 isl_union_pw_qpolynomial_get_space(upwqp));
1087 data.upwqp = upwqp;
1088 dim = isl_union_pw_qpolynomial_get_space(upwqp);
1089 data.res = isl_union_pw_qpolynomial_zero(dim);
1090 if (isl_union_map_foreach_map(umap, &map_apply, &data) < 0)
1091 goto error;
1093 isl_union_map_free(umap);
1094 isl_union_pw_qpolynomial_free(upwqp);
1096 return data.res;
1097 error:
1098 isl_union_map_free(umap);
1099 isl_union_pw_qpolynomial_free(upwqp);
1100 isl_union_pw_qpolynomial_free(data.res);
1101 return NULL;
1104 struct barvinok_apply_set_data {
1105 isl_union_pw_qpolynomial *upwqp;
1106 isl_union_pw_qpolynomial *res;
1107 isl_set *set;
1110 static int pw_qpolynomial_apply_set(__isl_take isl_pw_qpolynomial *pwqp,
1111 void *user)
1113 isl_space *set_dim;
1114 isl_space *pwqp_dim;
1115 struct barvinok_apply_set_data *data = user;
1116 int ok;
1118 set_dim = isl_set_get_space(data->set);
1119 pwqp_dim = isl_pw_qpolynomial_get_space(pwqp);
1120 ok = join_compatible(set_dim, pwqp_dim);
1121 isl_space_free(set_dim);
1122 isl_space_free(pwqp_dim);
1124 if (ok) {
1125 pwqp = isl_set_apply_pw_qpolynomial(isl_set_copy(data->set),
1126 pwqp);
1127 data->res = isl_union_pw_qpolynomial_add_pw_qpolynomial(
1128 data->res, pwqp);
1129 } else
1130 isl_pw_qpolynomial_free(pwqp);
1132 return 0;
1135 static int set_apply(__isl_take isl_set *set, void *user)
1137 struct barvinok_apply_set_data *data = user;
1138 int r;
1140 data->set = set;
1141 r = isl_union_pw_qpolynomial_foreach_pw_qpolynomial(data->upwqp,
1142 &pw_qpolynomial_apply_set, data);
1144 isl_set_free(set);
1145 return r;
1148 __isl_give isl_union_pw_qpolynomial *isl_union_set_apply_union_pw_qpolynomial(
1149 __isl_take isl_union_set *uset,
1150 __isl_take isl_union_pw_qpolynomial *upwqp)
1152 isl_space *dim;
1153 struct barvinok_apply_set_data data;
1155 upwqp = isl_union_pw_qpolynomial_align_params(upwqp,
1156 isl_union_set_get_space(uset));
1157 uset = isl_union_set_align_params(uset,
1158 isl_union_pw_qpolynomial_get_space(upwqp));
1160 data.upwqp = upwqp;
1161 dim = isl_union_pw_qpolynomial_get_space(upwqp);
1162 data.res = isl_union_pw_qpolynomial_zero(dim);
1163 if (isl_union_set_foreach_set(uset, &set_apply, &data) < 0)
1164 goto error;
1166 isl_union_set_free(uset);
1167 isl_union_pw_qpolynomial_free(upwqp);
1169 return data.res;
1170 error:
1171 isl_union_set_free(uset);
1172 isl_union_pw_qpolynomial_free(upwqp);
1173 isl_union_pw_qpolynomial_free(data.res);
1174 return NULL;
1177 evalue *evalue_sum(evalue *E, int nvar, unsigned MaxRays)
1179 evalue *sum;
1180 struct barvinok_options *options = barvinok_options_new_with_defaults();
1181 options->MaxRays = MaxRays;
1182 sum = barvinok_summate(E, nvar, options);
1183 barvinok_options_free(options);
1184 return sum;
1187 evalue *esum(evalue *e, int nvar)
1189 evalue *sum;
1190 struct barvinok_options *options = barvinok_options_new_with_defaults();
1191 sum = barvinok_summate(e, nvar, options);
1192 barvinok_options_free(options);
1193 return sum;
1196 /* Turn unweighted counting problem into "weighted" counting problem
1197 * with weight equal to 1 and call barvinok_summate on this weighted problem.
1199 evalue *barvinok_summate_unweighted(Polyhedron *P, Polyhedron *C,
1200 struct barvinok_options *options)
1202 Polyhedron *CA, *D;
1203 evalue e;
1204 evalue *sum;
1206 if (emptyQ(P) || emptyQ(C))
1207 return evalue_zero();
1209 CA = align_context(C, P->Dimension, options->MaxRays);
1210 D = DomainIntersection(P, CA, options->MaxRays);
1211 Domain_Free(CA);
1213 if (emptyQ(D)) {
1214 Domain_Free(D);
1215 return evalue_zero();
1218 value_init(e.d);
1219 e.x.p = new_enode(partition, 2, P->Dimension);
1220 EVALUE_SET_DOMAIN(e.x.p->arr[0], D);
1221 evalue_set_si(&e.x.p->arr[1], 1, 1);
1222 sum = barvinok_summate(&e, P->Dimension - C->Dimension, options);
1223 free_evalue_refs(&e);
1224 return sum;