separate computation of Bernoulli coefficients and Faulhaber polynomials
[barvinok.git] / barvinok.cc
bloba8908d4151b9c657e2dee89d8cd831aaff3ceb6d
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 "tcounter.h"
21 #include "decomposer.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 class dpoly_n {
44 public:
45 Matrix *coeff;
46 ~dpoly_n() {
47 Matrix_Free(coeff);
49 dpoly_n(int d) {
50 Value d0, one;
51 value_init(d0);
52 value_init(one);
53 value_set_si(one, 1);
54 coeff = Matrix_Alloc(d+1, d+1+1);
55 value_set_si(coeff->p[0][0], 1);
56 value_set_si(coeff->p[0][d+1], 1);
57 for (int i = 1; i <= d; ++i) {
58 value_multiply(coeff->p[i][0], coeff->p[i-1][0], d0);
59 Vector_Combine(coeff->p[i-1], coeff->p[i-1]+1, coeff->p[i]+1,
60 one, d0, i);
61 value_set_si(coeff->p[i][d+1], i);
62 value_multiply(coeff->p[i][d+1], coeff->p[i][d+1], coeff->p[i-1][d+1]);
63 value_decrement(d0, d0);
65 value_clear(d0);
66 value_clear(one);
68 void div(dpoly& d, Vector *count, int sign) {
69 int len = coeff->NbRows;
70 Matrix * c = Matrix_Alloc(coeff->NbRows, coeff->NbColumns);
71 Value tmp;
72 value_init(tmp);
73 for (int i = 0; i < len; ++i) {
74 Vector_Copy(coeff->p[i], c->p[i], len+1);
75 for (int j = 1; j <= i; ++j) {
76 value_multiply(tmp, d.coeff->p[j], c->p[i][len]);
77 value_oppose(tmp, tmp);
78 Vector_Combine(c->p[i], c->p[i-j], c->p[i],
79 c->p[i-j][len], tmp, len);
80 value_multiply(c->p[i][len], c->p[i][len], c->p[i-j][len]);
82 value_multiply(c->p[i][len], c->p[i][len], d.coeff->p[0]);
84 if (sign == -1) {
85 value_set_si(tmp, -1);
86 Vector_Scale(c->p[len-1], count->p, tmp, len);
87 value_assign(count->p[len], c->p[len-1][len]);
88 } else
89 Vector_Copy(c->p[len-1], count->p, len+1);
90 Vector_Normalize(count->p, len+1);
91 value_clear(tmp);
92 Matrix_Free(c);
96 const int MAX_TRY=10;
98 * Searches for a vector that is not orthogonal to any
99 * of the rays in rays.
101 static void nonorthog(mat_ZZ& rays, vec_ZZ& lambda)
103 int dim = rays.NumCols();
104 bool found = false;
105 lambda.SetLength(dim);
106 if (dim == 0)
107 return;
109 for (int i = 2; !found && i <= 50*dim; i+=4) {
110 for (int j = 0; j < MAX_TRY; ++j) {
111 for (int k = 0; k < dim; ++k) {
112 int r = random_int(i)+2;
113 int v = (2*(r%2)-1) * (r >> 1);
114 lambda[k] = v;
116 int k = 0;
117 for (; k < rays.NumRows(); ++k)
118 if (lambda * rays[k] == 0)
119 break;
120 if (k == rays.NumRows()) {
121 found = true;
122 break;
126 assert(found);
129 static void add_rays(mat_ZZ& rays, Polyhedron *i, int *r, int nvar = -1,
130 bool all = false)
132 unsigned dim = i->Dimension;
133 if (nvar == -1)
134 nvar = dim;
135 for (int k = 0; k < i->NbRays; ++k) {
136 if (!value_zero_p(i->Ray[k][dim+1]))
137 continue;
138 if (!all && nvar != dim && First_Non_Zero(i->Ray[k]+1, nvar) == -1)
139 continue;
140 values2zz(i->Ray[k]+1, rays[(*r)++], nvar);
144 static void mask_r(Matrix *f, int nr, Vector *lcm, int p, Vector *val, evalue *ev)
146 unsigned nparam = lcm->Size;
148 if (p == nparam) {
149 Vector * prod = Vector_Alloc(f->NbRows);
150 Matrix_Vector_Product(f, val->p, prod->p);
151 int isint = 1;
152 for (int i = 0; i < nr; ++i) {
153 value_modulus(prod->p[i], prod->p[i], f->p[i][nparam+1]);
154 isint &= value_zero_p(prod->p[i]);
156 value_set_si(ev->d, 1);
157 value_init(ev->x.n);
158 value_set_si(ev->x.n, isint);
159 Vector_Free(prod);
160 return;
163 Value tmp;
164 value_init(tmp);
165 if (value_one_p(lcm->p[p]))
166 mask_r(f, nr, lcm, p+1, val, ev);
167 else {
168 value_assign(tmp, lcm->p[p]);
169 value_set_si(ev->d, 0);
170 ev->x.p = new_enode(periodic, VALUE_TO_INT(tmp), p+1);
171 do {
172 value_decrement(tmp, tmp);
173 value_assign(val->p[p], tmp);
174 mask_r(f, nr, lcm, p+1, val, &ev->x.p->arr[VALUE_TO_INT(tmp)]);
175 } while (value_pos_p(tmp));
177 value_clear(tmp);
180 static void mask_fractional(Matrix *f, evalue *factor)
182 int nr = f->NbRows, nc = f->NbColumns;
183 int n;
184 bool found = false;
185 for (n = 0; n < nr && value_notzero_p(f->p[n][nc-1]); ++n)
186 if (value_notone_p(f->p[n][nc-1]) &&
187 value_notmone_p(f->p[n][nc-1]))
188 found = true;
189 if (!found)
190 return;
192 evalue EP;
193 nr = n;
195 Value m;
196 value_init(m);
198 evalue EV;
199 value_init(EV.d);
200 value_init(EV.x.n);
201 value_set_si(EV.x.n, 1);
203 for (n = 0; n < nr; ++n) {
204 value_assign(m, f->p[n][nc-1]);
205 if (value_one_p(m) || value_mone_p(m))
206 continue;
208 int j = normal_mod(f->p[n], nc-1, &m);
209 if (j == nc-1) {
210 free_evalue_refs(factor);
211 value_init(factor->d);
212 evalue_set_si(factor, 0, 1);
213 break;
215 vec_ZZ row;
216 values2zz(f->p[n], row, nc-1);
217 ZZ g;
218 value2zz(m, g);
219 if (j < (nc-1)-1 && row[j] > g/2) {
220 for (int k = j; k < (nc-1); ++k)
221 if (row[k] != 0)
222 row[k] = g - row[k];
225 value_init(EP.d);
226 value_set_si(EP.d, 0);
227 EP.x.p = new_enode(relation, 2, 0);
228 value_clear(EP.x.p->arr[1].d);
229 EP.x.p->arr[1] = *factor;
230 evalue *ev = &EP.x.p->arr[0];
231 value_set_si(ev->d, 0);
232 ev->x.p = new_enode(fractional, 3, -1);
233 evalue_set_si(&ev->x.p->arr[1], 0, 1);
234 evalue_set_si(&ev->x.p->arr[2], 1, 1);
235 evalue *E = multi_monom(row);
236 value_assign(EV.d, m);
237 emul(&EV, E);
238 value_clear(ev->x.p->arr[0].d);
239 ev->x.p->arr[0] = *E;
240 delete E;
241 *factor = EP;
244 value_clear(m);
245 free_evalue_refs(&EV);
251 static void mask_table(Matrix *f, evalue *factor)
253 int nr = f->NbRows, nc = f->NbColumns;
254 int n;
255 bool found = false;
256 for (n = 0; n < nr && value_notzero_p(f->p[n][nc-1]); ++n)
257 if (value_notone_p(f->p[n][nc-1]) &&
258 value_notmone_p(f->p[n][nc-1]))
259 found = true;
260 if (!found)
261 return;
263 Value tmp;
264 value_init(tmp);
265 nr = n;
266 unsigned np = nc - 2;
267 Vector *lcm = Vector_Alloc(np);
268 Vector *val = Vector_Alloc(nc);
269 Vector_Set(val->p, 0, nc);
270 value_set_si(val->p[np], 1);
271 Vector_Set(lcm->p, 1, np);
272 for (n = 0; n < nr; ++n) {
273 if (value_one_p(f->p[n][nc-1]) ||
274 value_mone_p(f->p[n][nc-1]))
275 continue;
276 for (int j = 0; j < np; ++j)
277 if (value_notzero_p(f->p[n][j])) {
278 Gcd(f->p[n][j], f->p[n][nc-1], &tmp);
279 value_division(tmp, f->p[n][nc-1], tmp);
280 value_lcm(tmp, lcm->p[j], &lcm->p[j]);
283 evalue EP;
284 value_init(EP.d);
285 mask_r(f, nr, lcm, 0, val, &EP);
286 value_clear(tmp);
287 Vector_Free(val);
288 Vector_Free(lcm);
289 emul(&EP,factor);
290 free_evalue_refs(&EP);
293 static void mask(Matrix *f, evalue *factor, barvinok_options *options)
295 if (options->lookup_table)
296 mask_table(f, factor);
297 else
298 mask_fractional(f, factor);
301 struct bfe_term : public bfc_term_base {
302 vector<evalue *> factors;
304 bfe_term(int len) : bfc_term_base(len) {
307 ~bfe_term() {
308 for (int i = 0; i < factors.size(); ++i) {
309 if (!factors[i])
310 continue;
311 free_evalue_refs(factors[i]);
312 delete factors[i];
317 static void print_int_vector(int *v, int len, const char *name)
319 cerr << name << endl;
320 for (int j = 0; j < len; ++j) {
321 cerr << v[j] << " ";
323 cerr << endl;
326 static void print_bfc_terms(mat_ZZ& factors, bfc_vec& v)
328 cerr << endl;
329 cerr << "factors" << endl;
330 cerr << factors << endl;
331 for (int i = 0; i < v.size(); ++i) {
332 cerr << "term: " << i << endl;
333 print_int_vector(v[i]->powers, factors.NumRows(), "powers");
334 cerr << "terms" << endl;
335 cerr << v[i]->terms << endl;
336 bfc_term* bfct = static_cast<bfc_term *>(v[i]);
337 cerr << bfct->c << endl;
341 static void print_bfe_terms(mat_ZZ& factors, bfc_vec& v)
343 cerr << endl;
344 cerr << "factors" << endl;
345 cerr << factors << endl;
346 for (int i = 0; i < v.size(); ++i) {
347 cerr << "term: " << i << endl;
348 print_int_vector(v[i]->powers, factors.NumRows(), "powers");
349 cerr << "terms" << endl;
350 cerr << v[i]->terms << endl;
351 bfe_term* bfet = static_cast<bfe_term *>(v[i]);
352 for (int j = 0; j < v[i]->terms.NumRows(); ++j) {
353 const char * test[] = {"a", "b"};
354 print_evalue(stderr, bfet->factors[j], test);
355 fprintf(stderr, "\n");
360 struct bfcounter : public bfcounter_base {
361 mpq_t count;
362 Value tz;
364 bfcounter(unsigned dim) : bfcounter_base(dim) {
365 mpq_init(count);
366 lower = 1;
367 value_init(tz);
369 ~bfcounter() {
370 mpq_clear(count);
371 value_clear(tz);
373 virtual void base(mat_ZZ& factors, bfc_vec& v);
374 virtual void get_count(Value *result) {
375 assert(value_one_p(&count[0]._mp_den));
376 value_assign(*result, &count[0]._mp_num);
380 void bfcounter::base(mat_ZZ& factors, bfc_vec& v)
382 unsigned nf = factors.NumRows();
384 for (int i = 0; i < v.size(); ++i) {
385 bfc_term* bfct = static_cast<bfc_term *>(v[i]);
386 int total_power = 0;
387 // factor is always positive, so we always
388 // change signs
389 for (int k = 0; k < nf; ++k)
390 total_power += v[i]->powers[k];
392 int j;
393 for (j = 0; j < nf; ++j)
394 if (v[i]->powers[j] > 0)
395 break;
397 zz2value(factors[j][0], tz);
398 dpoly D(total_power, tz, 1);
399 for (int k = 1; k < v[i]->powers[j]; ++k) {
400 zz2value(factors[j][0], tz);
401 dpoly fact(total_power, tz, 1);
402 D *= fact;
404 for ( ; ++j < nf; )
405 for (int k = 0; k < v[i]->powers[j]; ++k) {
406 zz2value(factors[j][0], tz);
407 dpoly fact(total_power, tz, 1);
408 D *= fact;
411 for (int k = 0; k < v[i]->terms.NumRows(); ++k) {
412 zz2value(v[i]->terms[k][0], tz);
413 dpoly n(total_power, tz);
414 mpq_set_si(tcount, 0, 1);
415 n.div(D, tcount, 1);
416 if (total_power % 2)
417 bfct->c[k].n = -bfct->c[k].n;
418 zz2value(bfct->c[k].n, tn);
419 zz2value(bfct->c[k].d, td);
421 mpz_mul(mpq_numref(tcount), mpq_numref(tcount), tn);
422 mpz_mul(mpq_denref(tcount), mpq_denref(tcount), td);
423 mpq_canonicalize(tcount);
424 mpq_add(count, count, tcount);
426 delete v[i];
431 /* Check whether the polyhedron is unbounded and if so,
432 * check whether it has any (and therefore an infinite number of)
433 * integer points.
434 * If one of the vertices is integer, then we are done.
435 * Otherwise, transform the polyhedron such that one of the rays
436 * is the first unit vector and cut it off at a height that ensures
437 * that if the whole polyhedron has any points, then the remaining part
438 * has integer points. In particular we add the largest coefficient
439 * of a ray to the highest vertex (rounded up).
441 static bool Polyhedron_is_infinite(Polyhedron *P, Value* result,
442 barvinok_options *options)
444 int r = 0;
445 Matrix *M, *M2;
446 Value c, tmp;
447 Value g;
448 bool first;
449 Vector *v;
450 Value offset, size;
451 Polyhedron *R;
453 if (P->NbBid == 0)
454 for (; r < P->NbRays; ++r)
455 if (value_zero_p(P->Ray[r][P->Dimension+1]))
456 break;
457 if (P->NbBid == 0 && r == P->NbRays)
458 return false;
460 if (options->count_sample_infinite) {
461 Vector *sample;
463 sample = Polyhedron_Sample(P, options);
464 if (!sample)
465 value_set_si(*result, 0);
466 else {
467 value_set_si(*result, -1);
468 Vector_Free(sample);
470 return true;
473 for (int i = 0; i < P->NbRays; ++i)
474 if (value_one_p(P->Ray[i][1+P->Dimension])) {
475 value_set_si(*result, -1);
476 return true;
479 value_init(g);
480 M = Matrix_Alloc(P->Dimension+1, P->Dimension+1);
481 Vector_Gcd(P->Ray[r]+1, P->Dimension, &g);
482 Vector_AntiScale(P->Ray[r]+1, M->p[0], g, P->Dimension+1);
483 int ok = unimodular_complete(M, 1);
484 assert(ok);
485 value_set_si(M->p[P->Dimension][P->Dimension], 1);
486 M2 = Transpose(M);
487 Matrix_Free(M);
488 P = Polyhedron_Preimage(P, M2, 0);
489 Matrix_Free(M2);
490 value_clear(g);
492 first = true;
493 value_init(offset);
494 value_init(size);
495 value_init(tmp);
496 value_set_si(size, 0);
498 for (int i = 0; i < P->NbBid; ++i) {
499 value_absolute(tmp, P->Ray[i][1]);
500 if (value_gt(tmp, size))
501 value_assign(size, tmp);
503 for (int i = P->NbBid; i < P->NbRays; ++i) {
504 if (value_zero_p(P->Ray[i][P->Dimension+1])) {
505 if (value_gt(P->Ray[i][1], size))
506 value_assign(size, P->Ray[i][1]);
507 continue;
509 mpz_cdiv_q(tmp, P->Ray[i][1], P->Ray[i][P->Dimension+1]);
510 if (first || value_gt(tmp, offset)) {
511 value_assign(offset, tmp);
512 first = false;
515 value_addto(offset, offset, size);
516 value_clear(size);
517 value_clear(tmp);
519 v = Vector_Alloc(P->Dimension+2);
520 value_set_si(v->p[0], 1);
521 value_set_si(v->p[1], -1);
522 value_assign(v->p[1+P->Dimension], offset);
523 R = AddConstraints(v->p, 1, P, options->MaxRays);
524 Polyhedron_Free(P);
525 P = R;
527 value_clear(offset);
528 Vector_Free(v);
530 value_init(c);
531 barvinok_count_with_options(P, &c, options);
532 Polyhedron_Free(P);
533 if (value_zero_p(c))
534 value_set_si(*result, 0);
535 else
536 value_set_si(*result, -1);
537 value_clear(c);
539 return true;
542 static void barvinok_count_f(Polyhedron *P, Value* result,
543 barvinok_options *options);
545 void barvinok_count_with_options(Polyhedron *P, Value* result,
546 struct barvinok_options *options)
548 unsigned dim;
549 int allocated = 0;
550 Polyhedron *Q;
551 bool infinite = false;
553 if (P->next)
554 fprintf(stderr,
555 "barvinok_count: input is a union; only first polyhedron is counted\n");
557 if (emptyQ2(P)) {
558 value_set_si(*result, 0);
559 return;
561 if (P->NbEq != 0) {
562 Q = NULL;
563 do {
564 P = remove_equalities(P, options->MaxRays);
565 P = DomainConstraintSimplify(P, options->MaxRays);
566 if (Q)
567 Polyhedron_Free(Q);
568 Q = P;
569 } while (!emptyQ(P) && P->NbEq != 0);
570 if (emptyQ(P)) {
571 Polyhedron_Free(P);
572 value_set_si(*result, 0);
573 return;
575 allocated = 1;
577 if (Polyhedron_is_infinite(P, result, options)) {
578 if (allocated)
579 Polyhedron_Free(P);
580 return;
582 if (P->Dimension == 0) {
583 /* Test whether the constraints are satisfied */
584 POL_ENSURE_VERTICES(P);
585 value_set_si(*result, !emptyQ(P));
586 if (allocated)
587 Polyhedron_Free(P);
588 return;
590 Q = Polyhedron_Factor(P, 0, NULL, options->MaxRays);
591 if (Q) {
592 if (allocated)
593 Polyhedron_Free(P);
594 P = Q;
595 allocated = 1;
598 barvinok_count_f(P, result, options);
599 if (value_neg_p(*result))
600 infinite = true;
601 if (Q && P->next && value_notzero_p(*result)) {
602 Value factor;
603 value_init(factor);
605 for (Q = P->next; Q; Q = Q->next) {
606 barvinok_count_f(Q, &factor, options);
607 if (value_neg_p(factor)) {
608 infinite = true;
609 continue;
610 } else if (Q->next && value_zero_p(factor)) {
611 value_set_si(*result, 0);
612 break;
614 value_multiply(*result, *result, factor);
617 value_clear(factor);
620 if (allocated)
621 Domain_Free(P);
622 if (infinite)
623 value_set_si(*result, -1);
626 void barvinok_count(Polyhedron *P, Value* result, unsigned NbMaxCons)
628 barvinok_options *options = barvinok_options_new_with_defaults();
629 options->MaxRays = NbMaxCons;
630 barvinok_count_with_options(P, result, options);
631 barvinok_options_free(options);
634 static void barvinok_count_f(Polyhedron *P, Value* result,
635 barvinok_options *options)
637 if (emptyQ2(P)) {
638 value_set_si(*result, 0);
639 return;
642 if (P->Dimension == 1)
643 return Line_Length(P, result);
645 int c = P->NbConstraints;
646 POL_ENSURE_FACETS(P);
647 if (c != P->NbConstraints || P->NbEq != 0) {
648 Polyhedron *next = P->next;
649 P->next = NULL;
650 barvinok_count_with_options(P, result, options);
651 P->next = next;
652 return;
655 POL_ENSURE_VERTICES(P);
657 if (Polyhedron_is_infinite(P, result, options))
658 return;
660 np_base *cnt;
661 if (options->incremental_specialization == BV_SPECIALIZATION_BF)
662 cnt = new bfcounter(P->Dimension);
663 else if (options->incremental_specialization == BV_SPECIALIZATION_DF)
664 cnt = new icounter(P->Dimension);
665 else if (options->incremental_specialization == BV_SPECIALIZATION_TODD)
666 cnt = new tcounter(P->Dimension, options->max_index);
667 else
668 cnt = new counter(P->Dimension, options->max_index);
669 cnt->start(P, options);
671 cnt->get_count(result);
672 delete cnt;
675 static void uni_polynom(int param, Vector *c, evalue *EP)
677 unsigned dim = c->Size-2;
678 value_init(EP->d);
679 value_set_si(EP->d,0);
680 EP->x.p = new_enode(polynomial, dim+1, param+1);
681 for (int j = 0; j <= dim; ++j)
682 evalue_set(&EP->x.p->arr[j], c->p[j], c->p[dim+1]);
685 typedef evalue * evalue_p;
687 struct enumerator_base {
688 unsigned dim;
689 evalue ** vE;
690 evalue mone;
691 vertex_decomposer *vpd;
693 enumerator_base(unsigned dim, vertex_decomposer *vpd)
695 this->dim = dim;
696 this->vpd = vpd;
698 vE = new evalue_p[vpd->nbV];
699 for (int j = 0; j < vpd->nbV; ++j)
700 vE[j] = 0;
702 value_init(mone.d);
703 evalue_set_si(&mone, -1, 1);
706 void decompose_at(Param_Vertices *V, int _i, barvinok_options *options) {
707 //this->pVD = pVD;
709 vE[_i] = new evalue;
710 value_init(vE[_i]->d);
711 evalue_set_si(vE[_i], 0, 1);
713 vpd->decompose_at_vertex(V, _i, options);
716 virtual ~enumerator_base() {
717 for (int j = 0; j < vpd->nbV; ++j)
718 if (vE[j]) {
719 free_evalue_refs(vE[j]);
720 delete vE[j];
722 delete [] vE;
724 free_evalue_refs(&mone);
727 static enumerator_base *create(Polyhedron *P, unsigned dim, unsigned nbV,
728 barvinok_options *options);
731 struct enumerator : public signed_cone_consumer, public vertex_decomposer,
732 public enumerator_base {
733 vec_ZZ lambda;
734 vec_ZZ den;
735 term_info num;
736 Vector *c;
737 mpq_t count;
738 Value tz;
740 enumerator(Polyhedron *P, unsigned dim, unsigned nbV) :
741 vertex_decomposer(P, nbV, *this), enumerator_base(dim, this) {
742 this->P = P;
743 this->nbV = nbV;
744 randomvector(P, lambda, dim);
745 den.SetLength(dim);
746 c = Vector_Alloc(dim+2);
748 mpq_init(count);
749 value_init(tz);
752 ~enumerator() {
753 mpq_clear(count);
754 Vector_Free(c);
755 value_clear(tz);
758 virtual void handle(const signed_cone& sc, barvinok_options *options);
761 void enumerator::handle(const signed_cone& sc, barvinok_options *options)
763 int sign = sc.sign;
764 int r = 0;
765 assert(sc.rays.NumRows() == dim);
766 for (int k = 0; k < dim; ++k) {
767 if (lambda * sc.rays[k] == 0)
768 throw Orthogonal;
771 lattice_point(V, sc.rays, lambda, &num, sc.det, sc.closed, options);
772 den = sc.rays * lambda;
774 if (dim % 2)
775 sign = -sign;
777 zz2value(den[0], tz);
778 dpoly n(dim, tz, 1);
779 for (int k = 1; k < dim; ++k) {
780 zz2value(den[k], tz);
781 dpoly fact(dim, tz, 1);
782 n *= fact;
784 if (num.E != NULL) {
785 dpoly_n d(dim);
786 d.div(n, c, sign);
787 for (unsigned long i = 0; i < sc.det; ++i) {
788 evalue *EV = evalue_polynomial(c, num.E[i]);
789 eadd(EV, vE[vert]);
790 free_evalue_refs(EV);
791 free(EV);
792 free_evalue_refs(num.E[i]);
793 delete num.E[i];
795 delete [] num.E;
796 } else {
797 mpq_set_si(count, 0, 1);
798 if (num.constant.length() == 1) {
799 zz2value(num.constant[0], tz);
800 dpoly d(dim, tz);
801 d.div(n, count, sign);
802 } else {
803 dpoly_n d(dim);
804 d.div(n, c, sign);
805 Value x, sum, acc;
806 value_init(x);
807 value_init(acc);
808 for (unsigned long i = 0; i < sc.det; ++i) {
809 value_assign(acc, c->p[dim]);
810 zz2value(num.constant[i], x);
811 for (int j = dim-1; j >= 0; --j) {
812 value_multiply(acc, acc, x);
813 value_addto(acc, acc, c->p[j]);
815 value_addto(mpq_numref(count), mpq_numref(count), acc);
817 mpz_set(mpq_denref(count), c->p[dim+1]);
818 value_clear(acc);
819 value_clear(x);
821 evalue EV;
822 value_init(EV.d);
823 evalue_set(&EV, &count[0]._mp_num, &count[0]._mp_den);
824 eadd(&EV, vE[vert]);
825 free_evalue_refs(&EV);
829 struct ienumerator_base : enumerator_base {
830 evalue ** E_vertex;
832 ienumerator_base(unsigned dim, vertex_decomposer *vpd) :
833 enumerator_base(dim,vpd) {
834 E_vertex = new evalue_p[dim];
837 virtual ~ienumerator_base() {
838 delete [] E_vertex;
841 evalue *E_num(int i, int d) {
842 return E_vertex[i + (dim-d)];
846 struct cumulator {
847 evalue *factor;
848 evalue *v;
849 dpoly_r *r;
851 cumulator(evalue *factor, evalue *v, dpoly_r *r) :
852 factor(factor), v(v), r(r) {}
854 void cumulate(barvinok_options *options);
856 virtual void add_term(const vector<int>& powers, evalue *f2) = 0;
857 virtual ~cumulator() {}
860 void cumulator::cumulate(barvinok_options *options)
862 evalue cum; // factor * 1 * E_num[0]/1 * (E_num[0]-1)/2 *...
863 evalue f;
864 evalue t; // E_num[0] - (m-1)
865 evalue *cst;
866 evalue mone;
868 if (options->lookup_table) {
869 value_init(mone.d);
870 evalue_set_si(&mone, -1, 1);
873 value_init(cum.d);
874 evalue_copy(&cum, factor);
875 value_init(f.d);
876 value_init(f.x.n);
877 value_set_si(f.d, 1);
878 value_set_si(f.x.n, 1);
879 value_init(t.d);
880 evalue_copy(&t, v);
882 if (!options->lookup_table) {
883 for (cst = &t; value_zero_p(cst->d); ) {
884 if (cst->x.p->type == fractional)
885 cst = &cst->x.p->arr[1];
886 else
887 cst = &cst->x.p->arr[0];
891 for (int m = 0; m < r->len; ++m) {
892 if (m > 0) {
893 if (m > 1) {
894 value_set_si(f.d, m);
895 emul(&f, &cum);
896 if (!options->lookup_table)
897 value_subtract(cst->x.n, cst->x.n, cst->d);
898 else
899 eadd(&mone, &t);
901 emul(&t, &cum);
903 dpoly_r_term_list& current = r->c[r->len-1-m];
904 dpoly_r_term_list::iterator j;
905 for (j = current.begin(); j != current.end(); ++j) {
906 if ((*j)->coeff == 0)
907 continue;
908 evalue *f2 = new evalue;
909 value_init(f2->d);
910 value_init(f2->x.n);
911 zz2value((*j)->coeff, f2->x.n);
912 zz2value(r->denom, f2->d);
913 emul(&cum, f2);
915 add_term((*j)->powers, f2);
918 free_evalue_refs(&f);
919 free_evalue_refs(&t);
920 free_evalue_refs(&cum);
921 if (options->lookup_table)
922 free_evalue_refs(&mone);
925 struct E_poly_term {
926 vector<int> powers;
927 evalue *E;
930 struct ie_cum : public cumulator {
931 vector<E_poly_term *> terms;
933 ie_cum(evalue *factor, evalue *v, dpoly_r *r) : cumulator(factor, v, r) {}
935 virtual void add_term(const vector<int>& powers, evalue *f2);
938 void ie_cum::add_term(const vector<int>& powers, evalue *f2)
940 int k;
941 for (k = 0; k < terms.size(); ++k) {
942 if (terms[k]->powers == powers) {
943 eadd(f2, terms[k]->E);
944 free_evalue_refs(f2);
945 delete f2;
946 break;
949 if (k >= terms.size()) {
950 E_poly_term *ET = new E_poly_term;
951 ET->powers = powers;
952 ET->E = f2;
953 terms.push_back(ET);
957 struct ienumerator : public signed_cone_consumer, public vertex_decomposer,
958 public ienumerator_base {
959 //Polyhedron *pVD;
960 mat_ZZ den;
961 mat_ZZ vertex;
962 mpq_t tcount;
963 Value tz;
965 ienumerator(Polyhedron *P, unsigned dim, unsigned nbV) :
966 vertex_decomposer(P, nbV, *this), ienumerator_base(dim, this) {
967 vertex.SetDims(1, dim);
969 den.SetDims(dim, dim);
970 mpq_init(tcount);
971 value_init(tz);
974 ~ienumerator() {
975 mpq_clear(tcount);
976 value_clear(tz);
979 virtual void handle(const signed_cone& sc, barvinok_options *options);
980 void reduce(evalue *factor, const mat_ZZ& num, const mat_ZZ& den_f,
981 barvinok_options *options);
984 void ienumerator::reduce(evalue *factor, const mat_ZZ& num, const mat_ZZ& den_f,
985 barvinok_options *options)
987 unsigned len = den_f.NumRows(); // number of factors in den
988 unsigned dim = num.NumCols();
989 assert(num.NumRows() == 1);
991 if (dim == 0) {
992 eadd(factor, vE[vert]);
993 return;
996 vec_ZZ den_s;
997 mat_ZZ den_r;
998 vec_ZZ num_s;
999 mat_ZZ num_p;
1001 split_one(num, num_s, num_p, den_f, den_s, den_r);
1003 vec_ZZ den_p;
1004 den_p.SetLength(len);
1006 ZZ one;
1007 one = 1;
1008 normalize(one, num_s, num_p, den_s, den_p, den_r);
1009 if (one != 1)
1010 emul(&mone, factor);
1012 int only_param = 0;
1013 int no_param = 0;
1014 for (int k = 0; k < len; ++k) {
1015 if (den_p[k] == 0)
1016 ++no_param;
1017 else if (den_s[k] == 0)
1018 ++only_param;
1020 if (no_param == 0) {
1021 reduce(factor, num_p, den_r, options);
1022 } else {
1023 int k, l;
1024 mat_ZZ pden;
1025 pden.SetDims(only_param, dim-1);
1027 for (k = 0, l = 0; k < len; ++k)
1028 if (den_s[k] == 0)
1029 pden[l++] = den_r[k];
1031 for (k = 0; k < len; ++k)
1032 if (den_p[k] == 0)
1033 break;
1035 zz2value(num_s[0], tz);
1036 dpoly n(no_param, tz);
1037 zz2value(den_s[k], tz);
1038 dpoly D(no_param, tz, 1);
1039 for ( ; ++k < len; )
1040 if (den_p[k] == 0) {
1041 zz2value(den_s[k], tz);
1042 dpoly fact(no_param, tz, 1);
1043 D *= fact;
1046 dpoly_r * r = 0;
1047 // if no_param + only_param == len then all powers
1048 // below will be all zero
1049 if (no_param + only_param == len) {
1050 if (E_num(0, dim) != 0)
1051 r = new dpoly_r(n, len);
1052 else {
1053 mpq_set_si(tcount, 0, 1);
1054 one = 1;
1055 n.div(D, tcount, 1);
1057 if (value_notzero_p(mpq_numref(tcount))) {
1058 evalue f;
1059 value_init(f.d);
1060 value_init(f.x.n);
1061 value_assign(f.x.n, mpq_numref(tcount));
1062 value_assign(f.d, mpq_denref(tcount));
1063 emul(&f, factor);
1064 reduce(factor, num_p, pden, options);
1065 free_evalue_refs(&f);
1067 return;
1069 } else {
1070 for (k = 0; k < len; ++k) {
1071 if (den_s[k] == 0 || den_p[k] == 0)
1072 continue;
1074 zz2value(den_s[k], tz);
1075 dpoly pd(no_param-1, tz, 1);
1077 int l;
1078 for (l = 0; l < k; ++l)
1079 if (den_r[l] == den_r[k])
1080 break;
1082 if (r == 0)
1083 r = new dpoly_r(n, pd, l, len);
1084 else {
1085 dpoly_r *nr = new dpoly_r(r, pd, l, len);
1086 delete r;
1087 r = nr;
1091 dpoly_r *rc = r->div(D);
1092 delete r;
1093 r = rc;
1094 if (E_num(0, dim) == 0) {
1095 int common = pden.NumRows();
1096 dpoly_r_term_list& final = r->c[r->len-1];
1097 int rows;
1098 evalue t;
1099 evalue f;
1100 value_init(f.d);
1101 value_init(f.x.n);
1102 zz2value(r->denom, f.d);
1103 dpoly_r_term_list::iterator j;
1104 for (j = final.begin(); j != final.end(); ++j) {
1105 if ((*j)->coeff == 0)
1106 continue;
1107 rows = common;
1108 for (int k = 0; k < r->dim; ++k) {
1109 int n = (*j)->powers[k];
1110 if (n == 0)
1111 continue;
1112 pden.SetDims(rows+n, pden.NumCols());
1113 for (int l = 0; l < n; ++l)
1114 pden[rows+l] = den_r[k];
1115 rows += n;
1117 value_init(t.d);
1118 evalue_copy(&t, factor);
1119 zz2value((*j)->coeff, f.x.n);
1120 emul(&f, &t);
1121 reduce(&t, num_p, pden, options);
1122 free_evalue_refs(&t);
1124 free_evalue_refs(&f);
1125 } else {
1126 ie_cum cum(factor, E_num(0, dim), r);
1127 cum.cumulate(options);
1129 int common = pden.NumRows();
1130 int rows;
1131 for (int j = 0; j < cum.terms.size(); ++j) {
1132 rows = common;
1133 pden.SetDims(rows, pden.NumCols());
1134 for (int k = 0; k < r->dim; ++k) {
1135 int n = cum.terms[j]->powers[k];
1136 if (n == 0)
1137 continue;
1138 pden.SetDims(rows+n, pden.NumCols());
1139 for (int l = 0; l < n; ++l)
1140 pden[rows+l] = den_r[k];
1141 rows += n;
1143 reduce(cum.terms[j]->E, num_p, pden, options);
1144 free_evalue_refs(cum.terms[j]->E);
1145 delete cum.terms[j]->E;
1146 delete cum.terms[j];
1149 delete r;
1153 static int type_offset(enode *p)
1155 return p->type == fractional ? 1 :
1156 p->type == flooring ? 1 : 0;
1159 static int edegree(evalue *e)
1161 int d = 0;
1162 enode *p;
1164 if (value_notzero_p(e->d))
1165 return 0;
1167 p = e->x.p;
1168 int i = type_offset(p);
1169 if (p->size-i-1 > d)
1170 d = p->size - i - 1;
1171 for (; i < p->size; i++) {
1172 int d2 = edegree(&p->arr[i]);
1173 if (d2 > d)
1174 d = d2;
1176 return d;
1179 void ienumerator::handle(const signed_cone& sc, barvinok_options *options)
1181 assert(sc.det == 1);
1182 assert(!sc.closed);
1183 assert(sc.rays.NumRows() == dim);
1185 lattice_point(V, sc.rays, vertex[0], E_vertex, options);
1187 den = sc.rays;
1189 evalue one;
1190 value_init(one.d);
1191 evalue_set_si(&one, sc.sign, 1);
1192 reduce(&one, vertex, den, options);
1193 free_evalue_refs(&one);
1195 for (int i = 0; i < dim; ++i)
1196 if (E_vertex[i]) {
1197 free_evalue_refs(E_vertex[i]);
1198 delete E_vertex[i];
1202 struct bfenumerator : public vertex_decomposer, public bf_base,
1203 public ienumerator_base {
1204 evalue *factor;
1206 bfenumerator(Polyhedron *P, unsigned dim, unsigned nbV) :
1207 vertex_decomposer(P, nbV, *this),
1208 bf_base(dim), ienumerator_base(dim, this) {
1209 lower = 0;
1210 factor = NULL;
1213 ~bfenumerator() {
1216 virtual void handle(const signed_cone& sc, barvinok_options *options);
1217 virtual void base(mat_ZZ& factors, bfc_vec& v);
1219 bfc_term_base* new_bf_term(int len) {
1220 bfe_term* t = new bfe_term(len);
1221 return t;
1224 virtual void set_factor(bfc_term_base *t, int k, int change) {
1225 bfe_term* bfet = static_cast<bfe_term *>(t);
1226 factor = bfet->factors[k];
1227 assert(factor != NULL);
1228 bfet->factors[k] = NULL;
1229 if (change)
1230 emul(&mone, factor);
1233 virtual void set_factor(bfc_term_base *t, int k, mpq_t &q, int change) {
1234 bfe_term* bfet = static_cast<bfe_term *>(t);
1235 factor = bfet->factors[k];
1236 assert(factor != NULL);
1237 bfet->factors[k] = NULL;
1239 evalue f;
1240 value_init(f.d);
1241 value_init(f.x.n);
1242 if (change)
1243 value_oppose(f.x.n, mpq_numref(q));
1244 else
1245 value_assign(f.x.n, mpq_numref(q));
1246 value_assign(f.d, mpq_denref(q));
1247 emul(&f, factor);
1248 free_evalue_refs(&f);
1251 virtual void set_factor(bfc_term_base *t, int k, const QQ& c, int change) {
1252 bfe_term* bfet = static_cast<bfe_term *>(t);
1254 factor = new evalue;
1256 evalue f;
1257 value_init(f.d);
1258 value_init(f.x.n);
1259 zz2value(c.n, f.x.n);
1260 if (change)
1261 value_oppose(f.x.n, f.x.n);
1262 zz2value(c.d, f.d);
1264 value_init(factor->d);
1265 evalue_copy(factor, bfet->factors[k]);
1266 emul(&f, factor);
1267 free_evalue_refs(&f);
1270 void set_factor(evalue *f, int change) {
1271 if (change)
1272 emul(&mone, f);
1273 factor = f;
1276 virtual void insert_term(bfc_term_base *t, int i) {
1277 bfe_term* bfet = static_cast<bfe_term *>(t);
1278 int len = t->terms.NumRows()-1; // already increased by one
1280 bfet->factors.resize(len+1);
1281 for (int j = len; j > i; --j) {
1282 bfet->factors[j] = bfet->factors[j-1];
1283 t->terms[j] = t->terms[j-1];
1285 bfet->factors[i] = factor;
1286 factor = NULL;
1289 virtual void update_term(bfc_term_base *t, int i) {
1290 bfe_term* bfet = static_cast<bfe_term *>(t);
1292 eadd(factor, bfet->factors[i]);
1293 free_evalue_refs(factor);
1294 delete factor;
1297 virtual bool constant_vertex(int dim) { return E_num(0, dim) == 0; }
1299 virtual void cum(bf_reducer *bfr, bfc_term_base *t, int k, dpoly_r *r,
1300 barvinok_options *options);
1303 enumerator_base *enumerator_base::create(Polyhedron *P, unsigned dim, unsigned nbV,
1304 barvinok_options *options)
1306 enumerator_base *eb;
1308 if (options->incremental_specialization == BV_SPECIALIZATION_BF)
1309 eb = new bfenumerator(P, dim, nbV);
1310 else if (options->incremental_specialization == BV_SPECIALIZATION_DF)
1311 eb = new ienumerator(P, dim, nbV);
1312 else
1313 eb = new enumerator(P, dim, nbV);
1315 return eb;
1318 struct bfe_cum : public cumulator {
1319 bfenumerator *bfe;
1320 bfc_term_base *told;
1321 int k;
1322 bf_reducer *bfr;
1324 bfe_cum(evalue *factor, evalue *v, dpoly_r *r, bf_reducer *bfr,
1325 bfc_term_base *t, int k, bfenumerator *e) :
1326 cumulator(factor, v, r), told(t), k(k),
1327 bfr(bfr), bfe(e) {
1330 virtual void add_term(const vector<int>& powers, evalue *f2);
1333 void bfe_cum::add_term(const vector<int>& powers, evalue *f2)
1335 bfr->update_powers(powers);
1337 bfc_term_base * t = bfe->find_bfc_term(bfr->vn, bfr->npowers, bfr->nnf);
1338 bfe->set_factor(f2, bfr->l_changes % 2);
1339 bfe->add_term(t, told->terms[k], bfr->l_extra_num);
1342 void bfenumerator::cum(bf_reducer *bfr, bfc_term_base *t, int k,
1343 dpoly_r *r, barvinok_options *options)
1345 bfe_term* bfet = static_cast<bfe_term *>(t);
1346 bfe_cum cum(bfet->factors[k], E_num(0, bfr->d), r, bfr, t, k, this);
1347 cum.cumulate(options);
1350 void bfenumerator::base(mat_ZZ& factors, bfc_vec& v)
1352 for (int i = 0; i < v.size(); ++i) {
1353 assert(v[i]->terms.NumRows() == 1);
1354 evalue *factor = static_cast<bfe_term *>(v[i])->factors[0];
1355 eadd(factor, vE[vert]);
1356 delete v[i];
1360 void bfenumerator::handle(const signed_cone& sc, barvinok_options *options)
1362 assert(sc.det == 1);
1363 assert(!sc.closed);
1364 assert(sc.rays.NumRows() == enumerator_base::dim);
1366 bfe_term* t = new bfe_term(enumerator_base::dim);
1367 vector< bfc_term_base * > v;
1368 v.push_back(t);
1370 t->factors.resize(1);
1372 t->terms.SetDims(1, enumerator_base::dim);
1373 lattice_point(V, sc.rays, t->terms[0], E_vertex, options);
1375 // the elements of factors are always lexpositive
1376 mat_ZZ factors;
1377 int s = setup_factors(sc.rays, factors, t, sc.sign);
1379 t->factors[0] = new evalue;
1380 value_init(t->factors[0]->d);
1381 evalue_set_si(t->factors[0], s, 1);
1382 reduce(factors, v, options);
1384 for (int i = 0; i < enumerator_base::dim; ++i)
1385 if (E_vertex[i]) {
1386 free_evalue_refs(E_vertex[i]);
1387 delete E_vertex[i];
1391 static evalue* barvinok_enumerate_ev_f(Polyhedron *P, Polyhedron* C,
1392 barvinok_options *options);
1394 /* Destroys C */
1395 static evalue* barvinok_enumerate_cst(Polyhedron *P, Polyhedron* C,
1396 struct barvinok_options *options)
1398 evalue *eres;
1400 if (emptyQ2(C)) {
1401 Polyhedron_Free(C);
1402 return evalue_zero();
1405 ALLOC(evalue, eres);
1406 value_init(eres->d);
1407 value_set_si(eres->d, 0);
1408 eres->x.p = new_enode(partition, 2, C->Dimension);
1409 EVALUE_SET_DOMAIN(eres->x.p->arr[0],
1410 DomainConstraintSimplify(C, options->MaxRays));
1411 value_set_si(eres->x.p->arr[1].d, 1);
1412 value_init(eres->x.p->arr[1].x.n);
1413 if (emptyQ2(P))
1414 value_set_si(eres->x.p->arr[1].x.n, 0);
1415 else
1416 barvinok_count_with_options(P, &eres->x.p->arr[1].x.n, options);
1418 return eres;
1421 static evalue* enumerate(Polyhedron *P, Polyhedron* C,
1422 struct barvinok_options *options)
1424 Polyhedron *next;
1425 Polyhedron *Porig = P;
1426 Polyhedron *Corig = C;
1427 Polyhedron *CEq = NULL, *rVD;
1428 int r = 0;
1429 unsigned nparam = C->Dimension;
1430 evalue *eres;
1431 Matrix *CP = NULL;
1433 evalue factor;
1434 value_init(factor.d);
1435 evalue_set_si(&factor, 1, 1);
1437 /* for now */
1438 POL_ENSURE_FACETS(P);
1439 POL_ENSURE_VERTICES(P);
1440 POL_ENSURE_FACETS(C);
1441 POL_ENSURE_VERTICES(C);
1443 if (C->Dimension == 0 || emptyQ(P) || emptyQ(C)) {
1444 constant:
1445 if (CEq == Porig)
1446 CEq = Polyhedron_Copy(CEq);
1447 eres = barvinok_enumerate_cst(P, CEq ? CEq : Polyhedron_Copy(C), options);
1448 out:
1449 if (CP) {
1450 evalue_backsubstitute(eres, CP, options->MaxRays);
1451 Matrix_Free(CP);
1454 emul(&factor, eres);
1455 if (options->approximation_method == BV_APPROX_DROP) {
1456 if (options->polynomial_approximation == BV_APPROX_SIGN_UPPER)
1457 evalue_frac2polynomial(eres, 1, options->MaxRays);
1458 if (options->polynomial_approximation == BV_APPROX_SIGN_LOWER)
1459 evalue_frac2polynomial(eres, -1, options->MaxRays);
1460 if (options->polynomial_approximation == BV_APPROX_SIGN_APPROX)
1461 evalue_frac2polynomial(eres, 0, options->MaxRays);
1463 reduce_evalue(eres);
1464 free_evalue_refs(&factor);
1465 if (P != Porig)
1466 Domain_Free(P);
1467 if (C != Corig)
1468 Polyhedron_Free(C);
1470 return eres;
1472 if (Polyhedron_is_unbounded(P, nparam, options->MaxRays))
1473 goto constant;
1475 if (P->NbEq != 0) {
1476 Matrix *f;
1477 P = remove_equalities_p(Polyhedron_Copy(P), P->Dimension-nparam, &f,
1478 options->MaxRays);
1479 mask(f, &factor, options);
1480 Matrix_Free(f);
1482 if (P->Dimension == nparam) {
1483 CEq = P;
1484 P = Universe_Polyhedron(0);
1485 goto constant;
1487 if (P->NbEq != 0 || C->NbEq != 0) {
1488 Polyhedron *Q = P;
1489 Polyhedron *D = C;
1490 remove_all_equalities(&P, &C, &CP, NULL, nparam, options->MaxRays);
1491 if (C != D && D != Corig)
1492 Polyhedron_Free(D);
1493 if (P != Q && Q != Porig)
1494 Domain_Free(Q);
1495 eres = enumerate(P, C, options);
1496 goto out;
1499 Polyhedron *T = Polyhedron_Factor(P, nparam, NULL, options->MaxRays);
1500 if (T || (P->Dimension == nparam+1)) {
1501 Polyhedron *Q;
1502 Polyhedron *C2;
1503 for (Q = T ? T : P; Q; Q = Q->next) {
1504 Polyhedron *next = Q->next;
1505 Q->next = NULL;
1507 Polyhedron *QC = Q;
1508 if (Q->Dimension != C->Dimension)
1509 QC = Polyhedron_Project(Q, nparam);
1511 C2 = C;
1512 C = DomainIntersection(C, QC, options->MaxRays);
1513 if (C2 != Corig)
1514 Polyhedron_Free(C2);
1515 if (QC != Q)
1516 Polyhedron_Free(QC);
1518 Q->next = next;
1521 if (T) {
1522 if (P != Porig)
1523 Polyhedron_Free(P);
1524 P = T;
1525 if (T->Dimension == C->Dimension) {
1526 P = T->next;
1527 T->next = NULL;
1528 Polyhedron_Free(T);
1532 next = P->next;
1533 P->next = NULL;
1534 eres = barvinok_enumerate_ev_f(P, C, options);
1535 P->next = next;
1537 if (P->next) {
1538 Polyhedron *Q;
1539 evalue *f;
1541 for (Q = P->next; Q; Q = Q->next) {
1542 Polyhedron *next = Q->next;
1543 Q->next = NULL;
1545 f = barvinok_enumerate_ev_f(Q, C, options);
1546 emul(f, eres);
1547 free_evalue_refs(f);
1548 free(f);
1550 Q->next = next;
1554 goto out;
1557 evalue* barvinok_enumerate_with_options(Polyhedron *P, Polyhedron* C,
1558 struct barvinok_options *options)
1560 Polyhedron *next, *Cnext, *C1;
1561 Polyhedron *Corig = C;
1562 evalue *eres;
1564 if (P->next)
1565 fprintf(stderr,
1566 "barvinok_enumerate: input is a union; only first polyhedron is enumerated\n");
1568 if (C->next)
1569 fprintf(stderr,
1570 "barvinok_enumerate: context is a union; only first polyhedron is considered\n");
1572 Cnext = C->next;
1573 C->next = NULL;
1574 C1 = Polyhedron_Project(P, C->Dimension);
1575 C = DomainIntersection(C, C1, options->MaxRays);
1576 Polyhedron_Free(C1);
1577 next = P->next;
1578 P->next = NULL;
1580 if (options->approximation_method == BV_APPROX_BERNOULLI)
1581 eres = Bernoulli_sum(P, C, options);
1582 else
1583 eres = enumerate(P, C, options);
1584 Domain_Free(C);
1586 P->next= next;
1587 Corig->next = Cnext;
1589 return eres;
1592 evalue* barvinok_enumerate_ev(Polyhedron *P, Polyhedron* C, unsigned MaxRays)
1594 evalue *E;
1595 barvinok_options *options = barvinok_options_new_with_defaults();
1596 options->MaxRays = MaxRays;
1597 E = barvinok_enumerate_with_options(P, C, options);
1598 barvinok_options_free(options);
1599 return E;
1602 evalue *Param_Polyhedron_Enumerate(Param_Polyhedron *PP, Polyhedron *P,
1603 Polyhedron *C,
1604 struct barvinok_options *options)
1606 evalue *eres;
1607 Param_Domain *D;
1608 unsigned nparam = C->Dimension;
1609 unsigned dim = P->Dimension - nparam;
1611 ALLOC(evalue, eres);
1612 value_init(eres->d);
1613 value_set_si(eres->d, 0);
1615 int nd;
1616 for (nd = 0, D=PP->D; D; ++nd, D=D->next);
1617 struct section { Polyhedron *D; evalue E; };
1618 section *s = new section[nd];
1620 enumerator_base *et = NULL;
1621 try_again:
1622 if (et)
1623 delete et;
1625 et = enumerator_base::create(P, dim, PP->nbV, options);
1627 Polyhedron *TC = true_context(P, C, options->MaxRays);
1628 FORALL_REDUCED_DOMAIN(PP, TC, nd, options, i, D, rVD)
1629 Param_Vertices *V;
1631 value_init(s[i].E.d);
1632 evalue_set_si(&s[i].E, 0, 1);
1633 s[i].D = rVD;
1635 FORALL_PVertex_in_ParamPolyhedron(V,D,PP) // _i is internal counter
1636 if (!et->vE[_i])
1637 try {
1638 et->decompose_at(V, _i, options);
1639 } catch (OrthogonalException &e) {
1640 FORALL_REDUCED_DOMAIN_RESET;
1641 for (; i >= 0; --i) {
1642 free_evalue_refs(&s[i].E);
1643 Domain_Free(s[i].D);
1645 goto try_again;
1647 eadd(et->vE[_i] , &s[i].E);
1648 END_FORALL_PVertex_in_ParamPolyhedron;
1649 evalue_range_reduction_in_domain(&s[i].E, rVD);
1650 END_FORALL_REDUCED_DOMAIN
1651 Polyhedron_Free(TC);
1653 delete et;
1654 if (nd == 0)
1655 evalue_set_si(eres, 0, 1);
1656 else {
1657 eres->x.p = new_enode(partition, 2*nd, C->Dimension);
1658 for (int j = 0; j < nd; ++j) {
1659 EVALUE_SET_DOMAIN(eres->x.p->arr[2*j], s[j].D);
1660 value_clear(eres->x.p->arr[2*j+1].d);
1661 eres->x.p->arr[2*j+1] = s[j].E;
1664 delete [] s;
1666 return eres;
1669 static evalue* barvinok_enumerate_ev_f(Polyhedron *P, Polyhedron* C,
1670 barvinok_options *options)
1672 unsigned nparam = C->Dimension;
1673 bool do_scale = options->approximation_method == BV_APPROX_SCALE;
1675 if (options->approximation_method == BV_APPROX_VOLUME)
1676 return Param_Polyhedron_Volume(P, C, options);
1678 if (P->Dimension - nparam == 1 && !do_scale)
1679 return ParamLine_Length(P, C, options);
1681 Param_Polyhedron *PP = NULL;
1682 evalue *eres;
1684 if (do_scale) {
1685 eres = scale_bound(P, C, options);
1686 if (eres)
1687 return eres;
1690 PP = Polyhedron2Param_Polyhedron(P, C, options);
1692 if (do_scale)
1693 eres = scale(PP, P, C, options);
1694 else
1695 eres = Param_Polyhedron_Enumerate(PP, P, C, options);
1697 if (PP)
1698 Param_Polyhedron_Free(PP);
1700 return eres;
1703 Enumeration* barvinok_enumerate(Polyhedron *P, Polyhedron* C, unsigned MaxRays)
1705 evalue *EP = barvinok_enumerate_ev(P, C, MaxRays);
1707 return partition2enumeration(EP);
1710 evalue* barvinok_enumerate_union(Polyhedron *D, Polyhedron* C, unsigned MaxRays)
1712 evalue *EP;
1713 gen_fun *gf = barvinok_enumerate_union_series(D, C, MaxRays);
1714 EP = *gf;
1715 delete gf;
1716 return EP;