rename barvinok_maximize to barvinok_bound
[barvinok.git] / barvinok.cc
blob74e196b0f5adbdb4008eee7490ecaac48a43e4f2
1 #include <assert.h>
2 #include <iostream>
3 #include <vector>
4 #include <deque>
5 #include <string>
6 #include <sstream>
7 #include <gmp.h>
8 #include <NTL/mat_ZZ.h>
9 #include <NTL/LLL.h>
10 #include <barvinok/util.h>
11 #include <barvinok/evalue.h>
12 #include "config.h"
13 #include <barvinok/barvinok.h>
14 #include <barvinok/genfun.h>
15 #include <barvinok/options.h>
16 #include <barvinok/sample.h>
17 #include "bfcounter.h"
18 #include "conversion.h"
19 #include "counter.h"
20 #include "decomposer.h"
21 #include "euler.h"
22 #include "lattice_point.h"
23 #include "reduce_domain.h"
24 #include "remove_equalities.h"
25 #include "scale.h"
26 #include "volume.h"
27 #include "bernoulli.h"
28 #include "param_util.h"
30 #ifdef NTL_STD_CXX
31 using namespace NTL;
32 #endif
33 using std::cerr;
34 using std::cout;
35 using std::endl;
36 using std::vector;
37 using std::deque;
38 using std::string;
39 using std::ostringstream;
41 #define ALLOC(t,p) p = (t*)malloc(sizeof(*p))
43 static evalue *barvinok_summate_unweighted(Polyhedron *P, Polyhedron *C,
44 evalue *(*summate)(evalue *, unsigned, struct barvinok_options *options),
45 struct barvinok_options *options);
47 class dpoly_n {
48 public:
49 Matrix *coeff;
50 ~dpoly_n() {
51 Matrix_Free(coeff);
53 dpoly_n(int d) {
54 Value d0, one;
55 value_init(d0);
56 value_init(one);
57 value_set_si(one, 1);
58 coeff = Matrix_Alloc(d+1, d+1+1);
59 value_set_si(coeff->p[0][0], 1);
60 value_set_si(coeff->p[0][d+1], 1);
61 for (int i = 1; i <= d; ++i) {
62 value_multiply(coeff->p[i][0], coeff->p[i-1][0], d0);
63 Vector_Combine(coeff->p[i-1], coeff->p[i-1]+1, coeff->p[i]+1,
64 one, d0, i);
65 value_set_si(coeff->p[i][d+1], i);
66 value_multiply(coeff->p[i][d+1], coeff->p[i][d+1], coeff->p[i-1][d+1]);
67 value_decrement(d0, d0);
69 value_clear(d0);
70 value_clear(one);
72 void div(dpoly& d, Vector *count, int sign) {
73 int len = coeff->NbRows;
74 Matrix * c = Matrix_Alloc(coeff->NbRows, coeff->NbColumns);
75 Value tmp;
76 value_init(tmp);
77 for (int i = 0; i < len; ++i) {
78 Vector_Copy(coeff->p[i], c->p[i], len+1);
79 for (int j = 1; j <= i; ++j) {
80 value_multiply(tmp, d.coeff->p[j], c->p[i][len]);
81 value_oppose(tmp, tmp);
82 Vector_Combine(c->p[i], c->p[i-j], c->p[i],
83 c->p[i-j][len], tmp, len);
84 value_multiply(c->p[i][len], c->p[i][len], c->p[i-j][len]);
86 value_multiply(c->p[i][len], c->p[i][len], d.coeff->p[0]);
88 if (sign == -1) {
89 value_set_si(tmp, -1);
90 Vector_Scale(c->p[len-1], count->p, tmp, len);
91 value_assign(count->p[len], c->p[len-1][len]);
92 } else
93 Vector_Copy(c->p[len-1], count->p, len+1);
94 Vector_Normalize(count->p, len+1);
95 value_clear(tmp);
96 Matrix_Free(c);
100 const int MAX_TRY=10;
102 * Searches for a vector that is not orthogonal to any
103 * of the rays in rays.
105 static void nonorthog(mat_ZZ& rays, vec_ZZ& lambda)
107 int dim = rays.NumCols();
108 bool found = false;
109 lambda.SetLength(dim);
110 if (dim == 0)
111 return;
113 for (int i = 2; !found && i <= 50*dim; i+=4) {
114 for (int j = 0; j < MAX_TRY; ++j) {
115 for (int k = 0; k < dim; ++k) {
116 int r = random_int(i)+2;
117 int v = (2*(r%2)-1) * (r >> 1);
118 lambda[k] = v;
120 int k = 0;
121 for (; k < rays.NumRows(); ++k)
122 if (lambda * rays[k] == 0)
123 break;
124 if (k == rays.NumRows()) {
125 found = true;
126 break;
130 assert(found);
133 static void add_rays(mat_ZZ& rays, Polyhedron *i, int *r, int nvar = -1,
134 bool all = false)
136 unsigned dim = i->Dimension;
137 if (nvar == -1)
138 nvar = dim;
139 for (int k = 0; k < i->NbRays; ++k) {
140 if (!value_zero_p(i->Ray[k][dim+1]))
141 continue;
142 if (!all && nvar != dim && First_Non_Zero(i->Ray[k]+1, nvar) == -1)
143 continue;
144 values2zz(i->Ray[k]+1, rays[(*r)++], nvar);
148 struct bfe_term : public bfc_term_base {
149 vector<evalue *> factors;
151 bfe_term(int len) : bfc_term_base(len) {
154 ~bfe_term() {
155 for (int i = 0; i < factors.size(); ++i) {
156 if (!factors[i])
157 continue;
158 free_evalue_refs(factors[i]);
159 delete factors[i];
164 static void print_int_vector(int *v, int len, const char *name)
166 cerr << name << endl;
167 for (int j = 0; j < len; ++j) {
168 cerr << v[j] << " ";
170 cerr << endl;
173 static void print_bfc_terms(mat_ZZ& factors, bfc_vec& v)
175 cerr << endl;
176 cerr << "factors" << endl;
177 cerr << factors << endl;
178 for (int i = 0; i < v.size(); ++i) {
179 cerr << "term: " << i << endl;
180 print_int_vector(v[i]->powers, factors.NumRows(), "powers");
181 cerr << "terms" << endl;
182 cerr << v[i]->terms << endl;
183 bfc_term* bfct = static_cast<bfc_term *>(v[i]);
184 cerr << bfct->c << endl;
188 static void print_bfe_terms(mat_ZZ& factors, bfc_vec& v)
190 cerr << endl;
191 cerr << "factors" << endl;
192 cerr << factors << endl;
193 for (int i = 0; i < v.size(); ++i) {
194 cerr << "term: " << i << endl;
195 print_int_vector(v[i]->powers, factors.NumRows(), "powers");
196 cerr << "terms" << endl;
197 cerr << v[i]->terms << endl;
198 bfe_term* bfet = static_cast<bfe_term *>(v[i]);
199 for (int j = 0; j < v[i]->terms.NumRows(); ++j) {
200 const char * test[] = {"a", "b"};
201 print_evalue(stderr, bfet->factors[j], test);
202 fprintf(stderr, "\n");
207 struct bfcounter : public bfcounter_base {
208 mpq_t count;
209 Value tz;
211 bfcounter(unsigned dim) : bfcounter_base(dim) {
212 mpq_init(count);
213 lower = 1;
214 value_init(tz);
216 ~bfcounter() {
217 mpq_clear(count);
218 value_clear(tz);
220 virtual void base(mat_ZZ& factors, bfc_vec& v);
221 virtual void get_count(Value *result) {
222 assert(value_one_p(&count[0]._mp_den));
223 value_assign(*result, &count[0]._mp_num);
227 void bfcounter::base(mat_ZZ& factors, bfc_vec& v)
229 unsigned nf = factors.NumRows();
231 for (int i = 0; i < v.size(); ++i) {
232 bfc_term* bfct = static_cast<bfc_term *>(v[i]);
233 int total_power = 0;
234 // factor is always positive, so we always
235 // change signs
236 for (int k = 0; k < nf; ++k)
237 total_power += v[i]->powers[k];
239 int j;
240 for (j = 0; j < nf; ++j)
241 if (v[i]->powers[j] > 0)
242 break;
244 zz2value(factors[j][0], tz);
245 dpoly D(total_power, tz, 1);
246 for (int k = 1; k < v[i]->powers[j]; ++k) {
247 zz2value(factors[j][0], tz);
248 dpoly fact(total_power, tz, 1);
249 D *= fact;
251 for ( ; ++j < nf; )
252 for (int k = 0; k < v[i]->powers[j]; ++k) {
253 zz2value(factors[j][0], tz);
254 dpoly fact(total_power, tz, 1);
255 D *= fact;
258 for (int k = 0; k < v[i]->terms.NumRows(); ++k) {
259 zz2value(v[i]->terms[k][0], tz);
260 dpoly n(total_power, tz);
261 mpq_set_si(tcount, 0, 1);
262 n.div(D, tcount, 1);
263 if (total_power % 2)
264 bfct->c[k].n = -bfct->c[k].n;
265 zz2value(bfct->c[k].n, tn);
266 zz2value(bfct->c[k].d, td);
268 mpz_mul(mpq_numref(tcount), mpq_numref(tcount), tn);
269 mpz_mul(mpq_denref(tcount), mpq_denref(tcount), td);
270 mpq_canonicalize(tcount);
271 mpq_add(count, count, tcount);
273 delete v[i];
278 /* Check whether the polyhedron is unbounded and if so,
279 * check whether it has any (and therefore an infinite number of)
280 * integer points.
281 * If one of the vertices is integer, then we are done.
282 * Otherwise, transform the polyhedron such that one of the rays
283 * is the first unit vector and cut it off at a height that ensures
284 * that if the whole polyhedron has any points, then the remaining part
285 * has integer points. In particular we add the largest coefficient
286 * of a ray to the highest vertex (rounded up).
288 static bool Polyhedron_is_infinite(Polyhedron *P, Value* result,
289 barvinok_options *options)
291 int r = 0;
292 Matrix *M, *M2;
293 Value c, tmp;
294 Value g;
295 bool first;
296 Vector *v;
297 Value offset, size;
298 Polyhedron *R;
300 if (P->NbBid == 0)
301 for (; r < P->NbRays; ++r)
302 if (value_zero_p(P->Ray[r][P->Dimension+1]))
303 break;
304 if (P->NbBid == 0 && r == P->NbRays)
305 return false;
307 if (options->count_sample_infinite) {
308 Vector *sample;
310 sample = Polyhedron_Sample(P, options);
311 if (!sample)
312 value_set_si(*result, 0);
313 else {
314 value_set_si(*result, -1);
315 Vector_Free(sample);
317 return true;
320 for (int i = 0; i < P->NbRays; ++i)
321 if (value_one_p(P->Ray[i][1+P->Dimension])) {
322 value_set_si(*result, -1);
323 return true;
326 value_init(g);
327 M = Matrix_Alloc(P->Dimension+1, P->Dimension+1);
328 Vector_Gcd(P->Ray[r]+1, P->Dimension, &g);
329 Vector_AntiScale(P->Ray[r]+1, M->p[0], g, P->Dimension+1);
330 int ok = unimodular_complete(M, 1);
331 assert(ok);
332 value_set_si(M->p[P->Dimension][P->Dimension], 1);
333 M2 = Transpose(M);
334 Matrix_Free(M);
335 P = Polyhedron_Preimage(P, M2, 0);
336 Matrix_Free(M2);
337 value_clear(g);
339 first = true;
340 value_init(offset);
341 value_init(size);
342 value_init(tmp);
343 value_set_si(size, 0);
345 for (int i = 0; i < P->NbBid; ++i) {
346 value_absolute(tmp, P->Ray[i][1]);
347 if (value_gt(tmp, size))
348 value_assign(size, tmp);
350 for (int i = P->NbBid; i < P->NbRays; ++i) {
351 if (value_zero_p(P->Ray[i][P->Dimension+1])) {
352 if (value_gt(P->Ray[i][1], size))
353 value_assign(size, P->Ray[i][1]);
354 continue;
356 mpz_cdiv_q(tmp, P->Ray[i][1], P->Ray[i][P->Dimension+1]);
357 if (first || value_gt(tmp, offset)) {
358 value_assign(offset, tmp);
359 first = false;
362 value_addto(offset, offset, size);
363 value_clear(size);
364 value_clear(tmp);
366 v = Vector_Alloc(P->Dimension+2);
367 value_set_si(v->p[0], 1);
368 value_set_si(v->p[1], -1);
369 value_assign(v->p[1+P->Dimension], offset);
370 R = AddConstraints(v->p, 1, P, options->MaxRays);
371 Polyhedron_Free(P);
372 P = R;
374 value_clear(offset);
375 Vector_Free(v);
377 value_init(c);
378 barvinok_count_with_options(P, &c, options);
379 Polyhedron_Free(P);
380 if (value_zero_p(c))
381 value_set_si(*result, 0);
382 else
383 value_set_si(*result, -1);
384 value_clear(c);
386 return true;
389 static void evalue2value(evalue *e, Value *v)
391 if (EVALUE_IS_ZERO(*e)) {
392 value_set_si(*v, 0);
393 return;
396 if (value_notzero_p(e->d)) {
397 assert(value_one_p(e->d));
398 value_assign(*v, e->x.n);
399 return;
402 assert(e->x.p->type == partition);
403 assert(e->x.p->size == 2);
404 assert(EVALUE_DOMAIN(e->x.p->arr[0])->Dimension == 0);
405 evalue2value(&e->x.p->arr[1], v);
408 static void barvinok_count_f(Polyhedron *P, Value* result,
409 barvinok_options *options);
411 void barvinok_count_with_options(Polyhedron *P, Value* result,
412 struct barvinok_options *options)
414 unsigned dim;
415 int allocated = 0;
416 Polyhedron *Q;
417 bool infinite = false;
419 if (P->next)
420 fprintf(stderr,
421 "barvinok_count: input is a union; only first polyhedron is counted\n");
423 if (emptyQ2(P)) {
424 value_set_si(*result, 0);
425 return;
427 if (P->NbEq != 0) {
428 Q = NULL;
429 do {
430 P = remove_equalities(P, options->MaxRays);
431 P = DomainConstraintSimplify(P, options->MaxRays);
432 if (Q)
433 Polyhedron_Free(Q);
434 Q = P;
435 } while (!emptyQ(P) && P->NbEq != 0);
436 if (emptyQ(P)) {
437 Polyhedron_Free(P);
438 value_set_si(*result, 0);
439 return;
441 allocated = 1;
443 if (Polyhedron_is_infinite(P, result, options)) {
444 if (allocated)
445 Polyhedron_Free(P);
446 return;
448 if (P->Dimension == 0) {
449 /* Test whether the constraints are satisfied */
450 POL_ENSURE_VERTICES(P);
451 value_set_si(*result, !emptyQ(P));
452 if (allocated)
453 Polyhedron_Free(P);
454 return;
456 if (options->summation == BV_SUM_BERNOULLI) {
457 Polyhedron *C = Universe_Polyhedron(0);
458 evalue *sum = barvinok_summate_unweighted(P, C, Bernoulli_sum_evalue,
459 options);
460 Polyhedron_Free(C);
461 evalue2value(sum, result);
462 evalue_free(sum);
463 return;
465 Q = Polyhedron_Factor(P, 0, NULL, options->MaxRays);
466 if (Q) {
467 if (allocated)
468 Polyhedron_Free(P);
469 P = Q;
470 allocated = 1;
473 barvinok_count_f(P, result, options);
474 if (value_neg_p(*result))
475 infinite = true;
476 if (Q && P->next && value_notzero_p(*result)) {
477 Value factor;
478 value_init(factor);
480 for (Q = P->next; Q; Q = Q->next) {
481 barvinok_count_f(Q, &factor, options);
482 if (value_neg_p(factor)) {
483 infinite = true;
484 continue;
485 } else if (Q->next && value_zero_p(factor)) {
486 value_set_si(*result, 0);
487 break;
489 value_multiply(*result, *result, factor);
492 value_clear(factor);
495 if (allocated)
496 Domain_Free(P);
497 if (infinite)
498 value_set_si(*result, -1);
501 void barvinok_count(Polyhedron *P, Value* result, unsigned NbMaxCons)
503 barvinok_options *options = barvinok_options_new_with_defaults();
504 options->MaxRays = NbMaxCons;
505 barvinok_count_with_options(P, result, options);
506 barvinok_options_free(options);
509 static void barvinok_count_f(Polyhedron *P, Value* result,
510 barvinok_options *options)
512 if (emptyQ2(P)) {
513 value_set_si(*result, 0);
514 return;
517 if (P->Dimension == 1)
518 return Line_Length(P, result);
520 int c = P->NbConstraints;
521 POL_ENSURE_FACETS(P);
522 if (c != P->NbConstraints || P->NbEq != 0) {
523 Polyhedron *next = P->next;
524 P->next = NULL;
525 barvinok_count_with_options(P, result, options);
526 P->next = next;
527 return;
530 POL_ENSURE_VERTICES(P);
532 if (Polyhedron_is_infinite(P, result, options))
533 return;
535 np_base *cnt;
536 if (options->incremental_specialization == BV_SPECIALIZATION_BF)
537 cnt = new bfcounter(P->Dimension);
538 else if (options->incremental_specialization == BV_SPECIALIZATION_DF)
539 cnt = new icounter(P->Dimension);
540 else if (options->incremental_specialization == BV_SPECIALIZATION_TODD)
541 cnt = new tcounter(P->Dimension, options->max_index);
542 else
543 cnt = new counter(P->Dimension, options->max_index);
544 cnt->start(P, options);
546 cnt->get_count(result);
547 delete cnt;
550 static void uni_polynom(int param, Vector *c, evalue *EP)
552 unsigned dim = c->Size-2;
553 value_init(EP->d);
554 value_set_si(EP->d,0);
555 EP->x.p = new_enode(polynomial, dim+1, param+1);
556 for (int j = 0; j <= dim; ++j)
557 evalue_set(&EP->x.p->arr[j], c->p[j], c->p[dim+1]);
560 typedef evalue * evalue_p;
562 struct enumerator_base {
563 unsigned dim;
564 evalue ** vE;
565 evalue mone;
566 vertex_decomposer *vpd;
568 enumerator_base(unsigned dim, vertex_decomposer *vpd)
570 this->dim = dim;
571 this->vpd = vpd;
573 vE = new evalue_p[vpd->PP->nbV];
574 for (int j = 0; j < vpd->PP->nbV; ++j)
575 vE[j] = 0;
577 value_init(mone.d);
578 evalue_set_si(&mone, -1, 1);
581 void decompose_at(Param_Vertices *V, int _i, barvinok_options *options) {
582 //this->pVD = pVD;
584 vE[_i] = new evalue;
585 value_init(vE[_i]->d);
586 evalue_set_si(vE[_i], 0, 1);
588 vpd->decompose_at_vertex(V, _i, options);
591 virtual ~enumerator_base() {
592 for (int j = 0; j < vpd->PP->nbV; ++j)
593 if (vE[j]) {
594 free_evalue_refs(vE[j]);
595 delete vE[j];
597 delete [] vE;
599 free_evalue_refs(&mone);
602 static enumerator_base *create(Polyhedron *P, unsigned dim,
603 Param_Polyhedron *PP,
604 barvinok_options *options);
607 struct enumerator : public signed_cone_consumer, public vertex_decomposer,
608 public enumerator_base {
609 vec_ZZ lambda;
610 vec_ZZ den;
611 term_info num;
612 Vector *c;
613 mpq_t count;
614 Value tz;
616 enumerator(Polyhedron *P, unsigned dim, Param_Polyhedron *PP) :
617 vertex_decomposer(PP, *this), enumerator_base(dim, this) {
618 randomvector(P, lambda, dim);
619 den.SetLength(dim);
620 c = Vector_Alloc(dim+2);
622 mpq_init(count);
623 value_init(tz);
626 ~enumerator() {
627 mpq_clear(count);
628 Vector_Free(c);
629 value_clear(tz);
632 virtual void handle(const signed_cone& sc, barvinok_options *options);
635 void enumerator::handle(const signed_cone& sc, barvinok_options *options)
637 int sign = sc.sign;
638 int r = 0;
639 assert(sc.rays.NumRows() == dim);
640 for (int k = 0; k < dim; ++k) {
641 if (lambda * sc.rays[k] == 0)
642 throw Orthogonal;
645 lattice_point(V, sc.rays, lambda, &num, sc.det, options);
646 den = sc.rays * lambda;
648 if (dim % 2)
649 sign = -sign;
651 zz2value(den[0], tz);
652 dpoly n(dim, tz, 1);
653 for (int k = 1; k < dim; ++k) {
654 zz2value(den[k], tz);
655 dpoly fact(dim, tz, 1);
656 n *= fact;
658 if (num.E != NULL) {
659 dpoly_n d(dim);
660 d.div(n, c, sign);
661 for (unsigned long i = 0; i < sc.det; ++i) {
662 evalue *EV = evalue_polynomial(c, num.E[i]);
663 eadd(EV, vE[vert]);
664 evalue_free(EV);
665 free_evalue_refs(num.E[i]);
666 delete num.E[i];
668 delete [] num.E;
669 } else {
670 mpq_set_si(count, 0, 1);
671 if (num.constant.length() == 1) {
672 zz2value(num.constant[0], tz);
673 dpoly d(dim, tz);
674 d.div(n, count, sign);
675 } else {
676 dpoly_n d(dim);
677 d.div(n, c, sign);
678 Value x, sum, acc;
679 value_init(x);
680 value_init(acc);
681 for (unsigned long i = 0; i < sc.det; ++i) {
682 value_assign(acc, c->p[dim]);
683 zz2value(num.constant[i], x);
684 for (int j = dim-1; j >= 0; --j) {
685 value_multiply(acc, acc, x);
686 value_addto(acc, acc, c->p[j]);
688 value_addto(mpq_numref(count), mpq_numref(count), acc);
690 mpz_set(mpq_denref(count), c->p[dim+1]);
691 value_clear(acc);
692 value_clear(x);
694 evalue EV;
695 value_init(EV.d);
696 evalue_set(&EV, &count[0]._mp_num, &count[0]._mp_den);
697 eadd(&EV, vE[vert]);
698 free_evalue_refs(&EV);
702 struct ienumerator_base : enumerator_base {
703 evalue ** E_vertex;
705 ienumerator_base(unsigned dim, vertex_decomposer *vpd) :
706 enumerator_base(dim,vpd) {
707 E_vertex = new evalue_p[dim];
710 virtual ~ienumerator_base() {
711 delete [] E_vertex;
714 evalue *E_num(int i, int d) {
715 return E_vertex[i + (dim-d)];
719 struct cumulator {
720 evalue *factor;
721 evalue *v;
722 dpoly_r *r;
724 cumulator(evalue *factor, evalue *v, dpoly_r *r) :
725 factor(factor), v(v), r(r) {}
727 void cumulate(barvinok_options *options);
729 virtual void add_term(const vector<int>& powers, evalue *f2) = 0;
730 virtual ~cumulator() {}
733 void cumulator::cumulate(barvinok_options *options)
735 evalue cum; // factor * 1 * E_num[0]/1 * (E_num[0]-1)/2 *...
736 evalue f;
737 evalue t; // E_num[0] - (m-1)
738 evalue *cst;
739 evalue mone;
741 if (options->lookup_table) {
742 value_init(mone.d);
743 evalue_set_si(&mone, -1, 1);
746 value_init(cum.d);
747 evalue_copy(&cum, factor);
748 value_init(f.d);
749 value_init(f.x.n);
750 value_set_si(f.d, 1);
751 value_set_si(f.x.n, 1);
752 value_init(t.d);
753 evalue_copy(&t, v);
755 if (!options->lookup_table) {
756 for (cst = &t; value_zero_p(cst->d); ) {
757 if (cst->x.p->type == fractional)
758 cst = &cst->x.p->arr[1];
759 else
760 cst = &cst->x.p->arr[0];
764 for (int m = 0; m < r->len; ++m) {
765 if (m > 0) {
766 if (m > 1) {
767 value_set_si(f.d, m);
768 emul(&f, &cum);
769 if (!options->lookup_table)
770 value_subtract(cst->x.n, cst->x.n, cst->d);
771 else
772 eadd(&mone, &t);
774 emul(&t, &cum);
776 dpoly_r_term_list& current = r->c[r->len-1-m];
777 dpoly_r_term_list::iterator j;
778 for (j = current.begin(); j != current.end(); ++j) {
779 if ((*j)->coeff == 0)
780 continue;
781 evalue *f2 = new evalue;
782 value_init(f2->d);
783 value_init(f2->x.n);
784 zz2value((*j)->coeff, f2->x.n);
785 zz2value(r->denom, f2->d);
786 emul(&cum, f2);
788 add_term((*j)->powers, f2);
791 free_evalue_refs(&f);
792 free_evalue_refs(&t);
793 free_evalue_refs(&cum);
794 if (options->lookup_table)
795 free_evalue_refs(&mone);
798 struct E_poly_term {
799 vector<int> powers;
800 evalue *E;
803 struct ie_cum : public cumulator {
804 vector<E_poly_term *> terms;
806 ie_cum(evalue *factor, evalue *v, dpoly_r *r) : cumulator(factor, v, r) {}
808 virtual void add_term(const vector<int>& powers, evalue *f2);
811 void ie_cum::add_term(const vector<int>& powers, evalue *f2)
813 int k;
814 for (k = 0; k < terms.size(); ++k) {
815 if (terms[k]->powers == powers) {
816 eadd(f2, terms[k]->E);
817 free_evalue_refs(f2);
818 delete f2;
819 break;
822 if (k >= terms.size()) {
823 E_poly_term *ET = new E_poly_term;
824 ET->powers = powers;
825 ET->E = f2;
826 terms.push_back(ET);
830 struct ienumerator : public signed_cone_consumer, public vertex_decomposer,
831 public ienumerator_base {
832 //Polyhedron *pVD;
833 mat_ZZ den;
834 mat_ZZ vertex;
835 mpq_t tcount;
836 Value tz;
838 ienumerator(Polyhedron *P, unsigned dim, Param_Polyhedron *PP) :
839 vertex_decomposer(PP, *this), ienumerator_base(dim, this) {
840 vertex.SetDims(1, dim);
842 den.SetDims(dim, dim);
843 mpq_init(tcount);
844 value_init(tz);
847 ~ienumerator() {
848 mpq_clear(tcount);
849 value_clear(tz);
852 virtual void handle(const signed_cone& sc, barvinok_options *options);
853 void reduce(evalue *factor, const mat_ZZ& num, const mat_ZZ& den_f,
854 barvinok_options *options);
857 void ienumerator::reduce(evalue *factor, const mat_ZZ& num, const mat_ZZ& den_f,
858 barvinok_options *options)
860 unsigned len = den_f.NumRows(); // number of factors in den
861 unsigned dim = num.NumCols();
862 assert(num.NumRows() == 1);
864 if (dim == 0) {
865 eadd(factor, vE[vert]);
866 return;
869 vec_ZZ den_s;
870 mat_ZZ den_r;
871 vec_ZZ num_s;
872 mat_ZZ num_p;
874 split_one(num, num_s, num_p, den_f, den_s, den_r);
876 vec_ZZ den_p;
877 den_p.SetLength(len);
879 ZZ one;
880 one = 1;
881 normalize(one, num_s, num_p, den_s, den_p, den_r);
882 if (one != 1)
883 emul(&mone, factor);
885 int only_param = 0;
886 int no_param = 0;
887 for (int k = 0; k < len; ++k) {
888 if (den_p[k] == 0)
889 ++no_param;
890 else if (den_s[k] == 0)
891 ++only_param;
893 if (no_param == 0) {
894 reduce(factor, num_p, den_r, options);
895 } else {
896 int k, l;
897 mat_ZZ pden;
898 pden.SetDims(only_param, dim-1);
900 for (k = 0, l = 0; k < len; ++k)
901 if (den_s[k] == 0)
902 pden[l++] = den_r[k];
904 for (k = 0; k < len; ++k)
905 if (den_p[k] == 0)
906 break;
908 zz2value(num_s[0], tz);
909 dpoly n(no_param, tz);
910 zz2value(den_s[k], tz);
911 dpoly D(no_param, tz, 1);
912 for ( ; ++k < len; )
913 if (den_p[k] == 0) {
914 zz2value(den_s[k], tz);
915 dpoly fact(no_param, tz, 1);
916 D *= fact;
919 dpoly_r * r = 0;
920 // if no_param + only_param == len then all powers
921 // below will be all zero
922 if (no_param + only_param == len) {
923 if (E_num(0, dim) != 0)
924 r = new dpoly_r(n, len);
925 else {
926 mpq_set_si(tcount, 0, 1);
927 one = 1;
928 n.div(D, tcount, 1);
930 if (value_notzero_p(mpq_numref(tcount))) {
931 evalue f;
932 value_init(f.d);
933 value_init(f.x.n);
934 value_assign(f.x.n, mpq_numref(tcount));
935 value_assign(f.d, mpq_denref(tcount));
936 emul(&f, factor);
937 reduce(factor, num_p, pden, options);
938 free_evalue_refs(&f);
940 return;
942 } else {
943 for (k = 0; k < len; ++k) {
944 if (den_s[k] == 0 || den_p[k] == 0)
945 continue;
947 zz2value(den_s[k], tz);
948 dpoly pd(no_param-1, tz, 1);
950 int l;
951 for (l = 0; l < k; ++l)
952 if (den_r[l] == den_r[k])
953 break;
955 if (r == 0)
956 r = new dpoly_r(n, pd, l, len);
957 else {
958 dpoly_r *nr = new dpoly_r(r, pd, l, len);
959 delete r;
960 r = nr;
964 dpoly_r *rc = r->div(D);
965 delete r;
966 r = rc;
967 if (E_num(0, dim) == 0) {
968 int common = pden.NumRows();
969 dpoly_r_term_list& final = r->c[r->len-1];
970 int rows;
971 evalue t;
972 evalue f;
973 value_init(f.d);
974 value_init(f.x.n);
975 zz2value(r->denom, f.d);
976 dpoly_r_term_list::iterator j;
977 for (j = final.begin(); j != final.end(); ++j) {
978 if ((*j)->coeff == 0)
979 continue;
980 rows = common;
981 for (int k = 0; k < r->dim; ++k) {
982 int n = (*j)->powers[k];
983 if (n == 0)
984 continue;
985 pden.SetDims(rows+n, pden.NumCols());
986 for (int l = 0; l < n; ++l)
987 pden[rows+l] = den_r[k];
988 rows += n;
990 value_init(t.d);
991 evalue_copy(&t, factor);
992 zz2value((*j)->coeff, f.x.n);
993 emul(&f, &t);
994 reduce(&t, num_p, pden, options);
995 free_evalue_refs(&t);
997 free_evalue_refs(&f);
998 } else {
999 ie_cum cum(factor, E_num(0, dim), r);
1000 cum.cumulate(options);
1002 int common = pden.NumRows();
1003 int rows;
1004 for (int j = 0; j < cum.terms.size(); ++j) {
1005 rows = common;
1006 pden.SetDims(rows, pden.NumCols());
1007 for (int k = 0; k < r->dim; ++k) {
1008 int n = cum.terms[j]->powers[k];
1009 if (n == 0)
1010 continue;
1011 pden.SetDims(rows+n, pden.NumCols());
1012 for (int l = 0; l < n; ++l)
1013 pden[rows+l] = den_r[k];
1014 rows += n;
1016 reduce(cum.terms[j]->E, num_p, pden, options);
1017 free_evalue_refs(cum.terms[j]->E);
1018 delete cum.terms[j]->E;
1019 delete cum.terms[j];
1022 delete r;
1026 static int type_offset(enode *p)
1028 return p->type == fractional ? 1 :
1029 p->type == flooring ? 1 : 0;
1032 static int edegree(evalue *e)
1034 int d = 0;
1035 enode *p;
1037 if (value_notzero_p(e->d))
1038 return 0;
1040 p = e->x.p;
1041 int i = type_offset(p);
1042 if (p->size-i-1 > d)
1043 d = p->size - i - 1;
1044 for (; i < p->size; i++) {
1045 int d2 = edegree(&p->arr[i]);
1046 if (d2 > d)
1047 d = d2;
1049 return d;
1052 void ienumerator::handle(const signed_cone& sc, barvinok_options *options)
1054 assert(sc.det == 1);
1055 assert(sc.rays.NumRows() == dim);
1057 lattice_point(V, sc.rays, vertex[0], E_vertex, options);
1059 den = sc.rays;
1061 evalue one;
1062 value_init(one.d);
1063 evalue_set_si(&one, sc.sign, 1);
1064 reduce(&one, vertex, den, options);
1065 free_evalue_refs(&one);
1067 for (int i = 0; i < dim; ++i)
1068 if (E_vertex[i]) {
1069 free_evalue_refs(E_vertex[i]);
1070 delete E_vertex[i];
1074 struct bfenumerator : public vertex_decomposer, public bf_base,
1075 public ienumerator_base {
1076 evalue *factor;
1078 bfenumerator(Polyhedron *P, unsigned dim, Param_Polyhedron *PP) :
1079 vertex_decomposer(PP, *this),
1080 bf_base(dim), ienumerator_base(dim, this) {
1081 lower = 0;
1082 factor = NULL;
1085 ~bfenumerator() {
1088 virtual void handle(const signed_cone& sc, barvinok_options *options);
1089 virtual void base(mat_ZZ& factors, bfc_vec& v);
1091 bfc_term_base* new_bf_term(int len) {
1092 bfe_term* t = new bfe_term(len);
1093 return t;
1096 virtual void set_factor(bfc_term_base *t, int k, int change) {
1097 bfe_term* bfet = static_cast<bfe_term *>(t);
1098 factor = bfet->factors[k];
1099 assert(factor != NULL);
1100 bfet->factors[k] = NULL;
1101 if (change)
1102 emul(&mone, factor);
1105 virtual void set_factor(bfc_term_base *t, int k, mpq_t &q, int change) {
1106 bfe_term* bfet = static_cast<bfe_term *>(t);
1107 factor = bfet->factors[k];
1108 assert(factor != NULL);
1109 bfet->factors[k] = NULL;
1111 evalue f;
1112 value_init(f.d);
1113 value_init(f.x.n);
1114 if (change)
1115 value_oppose(f.x.n, mpq_numref(q));
1116 else
1117 value_assign(f.x.n, mpq_numref(q));
1118 value_assign(f.d, mpq_denref(q));
1119 emul(&f, factor);
1120 free_evalue_refs(&f);
1123 virtual void set_factor(bfc_term_base *t, int k, const QQ& c, int change) {
1124 bfe_term* bfet = static_cast<bfe_term *>(t);
1126 factor = new evalue;
1128 evalue f;
1129 value_init(f.d);
1130 value_init(f.x.n);
1131 zz2value(c.n, f.x.n);
1132 if (change)
1133 value_oppose(f.x.n, f.x.n);
1134 zz2value(c.d, f.d);
1136 value_init(factor->d);
1137 evalue_copy(factor, bfet->factors[k]);
1138 emul(&f, factor);
1139 free_evalue_refs(&f);
1142 void set_factor(evalue *f, int change) {
1143 if (change)
1144 emul(&mone, f);
1145 factor = f;
1148 virtual void insert_term(bfc_term_base *t, int i) {
1149 bfe_term* bfet = static_cast<bfe_term *>(t);
1150 int len = t->terms.NumRows()-1; // already increased by one
1152 bfet->factors.resize(len+1);
1153 for (int j = len; j > i; --j) {
1154 bfet->factors[j] = bfet->factors[j-1];
1155 t->terms[j] = t->terms[j-1];
1157 bfet->factors[i] = factor;
1158 factor = NULL;
1161 virtual void update_term(bfc_term_base *t, int i) {
1162 bfe_term* bfet = static_cast<bfe_term *>(t);
1164 eadd(factor, bfet->factors[i]);
1165 free_evalue_refs(factor);
1166 delete factor;
1169 virtual bool constant_vertex(int dim) { return E_num(0, dim) == 0; }
1171 virtual void cum(bf_reducer *bfr, bfc_term_base *t, int k, dpoly_r *r,
1172 barvinok_options *options);
1175 enumerator_base *enumerator_base::create(Polyhedron *P, unsigned dim,
1176 Param_Polyhedron *PP,
1177 barvinok_options *options)
1179 enumerator_base *eb;
1181 if (options->incremental_specialization == BV_SPECIALIZATION_BF)
1182 eb = new bfenumerator(P, dim, PP);
1183 else if (options->incremental_specialization == BV_SPECIALIZATION_DF)
1184 eb = new ienumerator(P, dim, PP);
1185 else
1186 eb = new enumerator(P, dim, PP);
1188 return eb;
1191 struct bfe_cum : public cumulator {
1192 bfenumerator *bfe;
1193 bfc_term_base *told;
1194 int k;
1195 bf_reducer *bfr;
1197 bfe_cum(evalue *factor, evalue *v, dpoly_r *r, bf_reducer *bfr,
1198 bfc_term_base *t, int k, bfenumerator *e) :
1199 cumulator(factor, v, r), told(t), k(k),
1200 bfr(bfr), bfe(e) {
1203 virtual void add_term(const vector<int>& powers, evalue *f2);
1206 void bfe_cum::add_term(const vector<int>& powers, evalue *f2)
1208 bfr->update_powers(powers);
1210 bfc_term_base * t = bfe->find_bfc_term(bfr->vn, bfr->npowers, bfr->nnf);
1211 bfe->set_factor(f2, bfr->l_changes % 2);
1212 bfe->add_term(t, told->terms[k], bfr->l_extra_num);
1215 void bfenumerator::cum(bf_reducer *bfr, bfc_term_base *t, int k,
1216 dpoly_r *r, barvinok_options *options)
1218 bfe_term* bfet = static_cast<bfe_term *>(t);
1219 bfe_cum cum(bfet->factors[k], E_num(0, bfr->d), r, bfr, t, k, this);
1220 cum.cumulate(options);
1223 void bfenumerator::base(mat_ZZ& factors, bfc_vec& v)
1225 for (int i = 0; i < v.size(); ++i) {
1226 assert(v[i]->terms.NumRows() == 1);
1227 evalue *factor = static_cast<bfe_term *>(v[i])->factors[0];
1228 eadd(factor, vE[vert]);
1229 delete v[i];
1233 void bfenumerator::handle(const signed_cone& sc, barvinok_options *options)
1235 assert(sc.det == 1);
1236 assert(sc.rays.NumRows() == enumerator_base::dim);
1238 bfe_term* t = new bfe_term(enumerator_base::dim);
1239 vector< bfc_term_base * > v;
1240 v.push_back(t);
1242 t->factors.resize(1);
1244 t->terms.SetDims(1, enumerator_base::dim);
1245 lattice_point(V, sc.rays, t->terms[0], E_vertex, options);
1247 // the elements of factors are always lexpositive
1248 mat_ZZ factors;
1249 int s = setup_factors(sc.rays, factors, t, sc.sign);
1251 t->factors[0] = new evalue;
1252 value_init(t->factors[0]->d);
1253 evalue_set_si(t->factors[0], s, 1);
1254 reduce(factors, v, options);
1256 for (int i = 0; i < enumerator_base::dim; ++i)
1257 if (E_vertex[i]) {
1258 free_evalue_refs(E_vertex[i]);
1259 delete E_vertex[i];
1263 static evalue* barvinok_enumerate_ev_f(Polyhedron *P, Polyhedron* C,
1264 barvinok_options *options);
1266 /* Destroys C */
1267 static evalue* barvinok_enumerate_cst(Polyhedron *P, Polyhedron* C,
1268 struct barvinok_options *options)
1270 evalue *eres;
1272 if (emptyQ2(C)) {
1273 Polyhedron_Free(C);
1274 return evalue_zero();
1277 ALLOC(evalue, eres);
1278 value_init(eres->d);
1279 value_set_si(eres->d, 0);
1280 eres->x.p = new_enode(partition, 2, C->Dimension);
1281 EVALUE_SET_DOMAIN(eres->x.p->arr[0],
1282 DomainConstraintSimplify(C, options->MaxRays));
1283 value_set_si(eres->x.p->arr[1].d, 1);
1284 value_init(eres->x.p->arr[1].x.n);
1285 if (emptyQ2(P))
1286 value_set_si(eres->x.p->arr[1].x.n, 0);
1287 else
1288 barvinok_count_with_options(P, &eres->x.p->arr[1].x.n, options);
1290 return eres;
1293 static evalue* enumerate(Polyhedron *P, Polyhedron* C,
1294 struct barvinok_options *options)
1296 Polyhedron *next;
1297 Polyhedron *Porig = P;
1298 Polyhedron *Corig = C;
1299 Polyhedron *CEq = NULL, *rVD;
1300 int r = 0;
1301 unsigned nparam = C->Dimension;
1302 evalue *eres;
1303 Matrix *CP = NULL;
1305 evalue factor;
1306 value_init(factor.d);
1307 evalue_set_si(&factor, 1, 1);
1309 /* for now */
1310 POL_ENSURE_FACETS(P);
1311 POL_ENSURE_VERTICES(P);
1312 POL_ENSURE_FACETS(C);
1313 POL_ENSURE_VERTICES(C);
1315 if (C->Dimension == 0 || emptyQ(P) || emptyQ(C)) {
1316 constant:
1317 if (CEq == Porig)
1318 CEq = Polyhedron_Copy(CEq);
1319 eres = barvinok_enumerate_cst(P, CEq ? CEq : Polyhedron_Copy(C), options);
1320 out:
1321 if (CP) {
1322 evalue_backsubstitute(eres, CP, options->MaxRays);
1323 Matrix_Free(CP);
1326 emul(&factor, eres);
1327 if (options->approximation_method == BV_APPROX_DROP) {
1328 if (options->polynomial_approximation == BV_APPROX_SIGN_UPPER)
1329 evalue_frac2polynomial(eres, 1, options->MaxRays);
1330 if (options->polynomial_approximation == BV_APPROX_SIGN_LOWER)
1331 evalue_frac2polynomial(eres, -1, options->MaxRays);
1332 if (options->polynomial_approximation == BV_APPROX_SIGN_APPROX)
1333 evalue_frac2polynomial(eres, 0, options->MaxRays);
1335 reduce_evalue(eres);
1336 free_evalue_refs(&factor);
1337 if (P != Porig)
1338 Domain_Free(P);
1339 if (C != Corig)
1340 Polyhedron_Free(C);
1342 return eres;
1344 if (Polyhedron_is_unbounded(P, nparam, options->MaxRays))
1345 goto constant;
1347 if (P->Dimension == nparam) {
1348 CEq = P;
1349 P = Universe_Polyhedron(0);
1350 goto constant;
1352 if (P->NbEq != 0 || C->NbEq != 0) {
1353 Polyhedron *Q = P;
1354 Polyhedron *D = C;
1355 remove_all_equalities(&P, &C, &CP, NULL, nparam, options->MaxRays);
1356 if (C != D && D != Corig)
1357 Polyhedron_Free(D);
1358 if (P != Q && Q != Porig)
1359 Domain_Free(Q);
1360 eres = enumerate(P, C, options);
1361 goto out;
1364 Polyhedron *T = Polyhedron_Factor(P, nparam, NULL, options->MaxRays);
1365 if (T || (P->Dimension == nparam+1)) {
1366 Polyhedron *Q;
1367 Polyhedron *C2;
1368 for (Q = T ? T : P; Q; Q = Q->next) {
1369 Polyhedron *next = Q->next;
1370 Q->next = NULL;
1372 Polyhedron *QC = Q;
1373 if (Q->Dimension != C->Dimension)
1374 QC = Polyhedron_Project(Q, nparam);
1376 C2 = C;
1377 C = DomainIntersection(C, QC, options->MaxRays);
1378 if (C2 != Corig)
1379 Polyhedron_Free(C2);
1380 if (QC != Q)
1381 Polyhedron_Free(QC);
1383 Q->next = next;
1386 if (T) {
1387 if (P != Porig)
1388 Polyhedron_Free(P);
1389 P = T;
1390 if (T->Dimension == C->Dimension) {
1391 P = T->next;
1392 T->next = NULL;
1393 Polyhedron_Free(T);
1397 next = P->next;
1398 P->next = NULL;
1399 eres = barvinok_enumerate_ev_f(P, C, options);
1400 P->next = next;
1402 if (P->next) {
1403 Polyhedron *Q;
1404 evalue *f;
1406 for (Q = P->next; Q; Q = Q->next) {
1407 Polyhedron *next = Q->next;
1408 Q->next = NULL;
1410 f = barvinok_enumerate_ev_f(Q, C, options);
1411 emul(f, eres);
1412 evalue_free(f);
1414 Q->next = next;
1418 goto out;
1421 evalue* barvinok_enumerate_with_options(Polyhedron *P, Polyhedron* C,
1422 struct barvinok_options *options)
1424 Polyhedron *next, *Cnext, *C1;
1425 Polyhedron *Corig = C;
1426 evalue *eres;
1428 if (P->next)
1429 fprintf(stderr,
1430 "barvinok_enumerate: input is a union; only first polyhedron is enumerated\n");
1432 if (C->next)
1433 fprintf(stderr,
1434 "barvinok_enumerate: context is a union; only first polyhedron is considered\n");
1436 Cnext = C->next;
1437 C->next = NULL;
1438 C1 = Polyhedron_Project(P, C->Dimension);
1439 C = DomainIntersection(C, C1, options->MaxRays);
1440 Polyhedron_Free(C1);
1441 next = P->next;
1442 P->next = NULL;
1444 if (options->approximation_method == BV_APPROX_BERNOULLI ||
1445 options->summation == BV_SUM_BERNOULLI)
1446 eres = barvinok_summate_unweighted(P, C, Bernoulli_sum_evalue, options);
1447 else
1448 eres = enumerate(P, C, options);
1449 Domain_Free(C);
1451 P->next= next;
1452 Corig->next = Cnext;
1454 return eres;
1457 evalue* barvinok_enumerate_ev(Polyhedron *P, Polyhedron* C, unsigned MaxRays)
1459 evalue *E;
1460 barvinok_options *options = barvinok_options_new_with_defaults();
1461 options->MaxRays = MaxRays;
1462 E = barvinok_enumerate_with_options(P, C, options);
1463 barvinok_options_free(options);
1464 return E;
1467 evalue *Param_Polyhedron_Enumerate(Param_Polyhedron *PP, Polyhedron *P,
1468 Polyhedron *C,
1469 struct barvinok_options *options)
1471 evalue *eres;
1472 Param_Domain *D;
1473 unsigned nparam = C->Dimension;
1474 unsigned dim = P->Dimension - nparam;
1476 int nd;
1477 for (nd = 0, D=PP->D; D; ++nd, D=D->next);
1478 evalue_section *s = new evalue_section[nd];
1480 enumerator_base *et = NULL;
1481 try_again:
1482 if (et)
1483 delete et;
1485 et = enumerator_base::create(P, dim, PP, options);
1487 Polyhedron *TC = true_context(P, C, options->MaxRays);
1488 FORALL_REDUCED_DOMAIN(PP, TC, nd, options, i, D, rVD)
1489 Param_Vertices *V;
1491 s[i].E = evalue_zero();
1492 s[i].D = rVD;
1494 FORALL_PVertex_in_ParamPolyhedron(V,D,PP) // _i is internal counter
1495 if (!et->vE[_i])
1496 try {
1497 et->decompose_at(V, _i, options);
1498 } catch (OrthogonalException &e) {
1499 FORALL_REDUCED_DOMAIN_RESET;
1500 for (; i >= 0; --i) {
1501 evalue_free(s[i].E);
1502 Domain_Free(s[i].D);
1504 goto try_again;
1506 eadd(et->vE[_i] , s[i].E);
1507 END_FORALL_PVertex_in_ParamPolyhedron;
1508 evalue_range_reduction_in_domain(s[i].E, rVD);
1509 END_FORALL_REDUCED_DOMAIN
1510 Polyhedron_Free(TC);
1512 delete et;
1513 eres = evalue_from_section_array(s, nd);
1514 delete [] s;
1516 return eres;
1519 static evalue* barvinok_enumerate_ev_f(Polyhedron *P, Polyhedron* C,
1520 barvinok_options *options)
1522 unsigned nparam = C->Dimension;
1523 bool do_scale = options->approximation_method == BV_APPROX_SCALE;
1525 if (options->summation == BV_SUM_EULER)
1526 return barvinok_summate_unweighted(P, C, euler_summate, options);
1528 if (options->approximation_method == BV_APPROX_VOLUME)
1529 return Param_Polyhedron_Volume(P, C, options);
1531 if (P->Dimension - nparam == 1 && !do_scale)
1532 return ParamLine_Length(P, C, options);
1534 Param_Polyhedron *PP = NULL;
1535 evalue *eres;
1537 if (do_scale) {
1538 eres = scale_bound(P, C, options);
1539 if (eres)
1540 return eres;
1543 PP = Polyhedron2Param_Polyhedron(P, C, options);
1545 if (do_scale)
1546 eres = scale(PP, P, C, options);
1547 else
1548 eres = Param_Polyhedron_Enumerate(PP, P, C, options);
1550 if (PP)
1551 Param_Polyhedron_Free(PP);
1553 return eres;
1556 Enumeration* barvinok_enumerate(Polyhedron *P, Polyhedron* C, unsigned MaxRays)
1558 evalue *EP = barvinok_enumerate_ev(P, C, MaxRays);
1560 return partition2enumeration(EP);
1563 evalue* barvinok_enumerate_union(Polyhedron *D, Polyhedron* C, unsigned MaxRays)
1565 evalue *EP;
1566 gen_fun *gf = barvinok_enumerate_union_series(D, C, MaxRays);
1567 EP = *gf;
1568 delete gf;
1569 return EP;
1572 evalue *barvinok_summate(evalue *e, int nvar, struct barvinok_options *options)
1574 if (options->summation == BV_SUM_EULER)
1575 return euler_summate(e, nvar, options);
1576 else if (options->summation == BV_SUM_BERNOULLI)
1577 return Bernoulli_sum_evalue(e, nvar, options);
1578 else
1579 return evalue_sum(e, nvar, options->MaxRays);
1582 /* Turn unweighted counting problem into "weighted" counting problem
1583 * with weight equal to 1 and call "summate" on this weighted problem.
1585 static evalue *barvinok_summate_unweighted(Polyhedron *P, Polyhedron *C,
1586 evalue *(*summate)(evalue *, unsigned, struct barvinok_options *options),
1587 struct barvinok_options *options)
1589 Polyhedron *CA, *D;
1590 evalue e;
1591 evalue *sum;
1593 if (emptyQ(P) || emptyQ(C))
1594 return evalue_zero();
1596 CA = align_context(C, P->Dimension, options->MaxRays);
1597 D = DomainIntersection(P, CA, options->MaxRays);
1598 Domain_Free(CA);
1600 if (emptyQ(D)) {
1601 Domain_Free(D);
1602 return evalue_zero();
1605 value_init(e.d);
1606 e.x.p = new_enode(partition, 2, P->Dimension);
1607 EVALUE_SET_DOMAIN(e.x.p->arr[0], D);
1608 evalue_set_si(&e.x.p->arr[1], 1, 1);
1609 sum = summate(&e, P->Dimension - C->Dimension, options);
1610 free_evalue_refs(&e);
1611 return sum;