Merge branch 'topcom'
[barvinok.git] / barvinok.cc
blob4245aa8b6c4d12a2eedc4a27049297c22563484d
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 "euler.h"
23 #include "lattice_point.h"
24 #include "reduce_domain.h"
25 #include "remove_equalities.h"
26 #include "scale.h"
27 #include "volume.h"
28 #include "bernoulli.h"
29 #include "param_util.h"
31 #ifdef NTL_STD_CXX
32 using namespace NTL;
33 #endif
34 using std::cerr;
35 using std::cout;
36 using std::endl;
37 using std::vector;
38 using std::deque;
39 using std::string;
40 using std::ostringstream;
42 #define ALLOC(t,p) p = (t*)malloc(sizeof(*p))
44 class dpoly_n {
45 public:
46 Matrix *coeff;
47 ~dpoly_n() {
48 Matrix_Free(coeff);
50 dpoly_n(int d) {
51 Value d0, one;
52 value_init(d0);
53 value_init(one);
54 value_set_si(one, 1);
55 coeff = Matrix_Alloc(d+1, d+1+1);
56 value_set_si(coeff->p[0][0], 1);
57 value_set_si(coeff->p[0][d+1], 1);
58 for (int i = 1; i <= d; ++i) {
59 value_multiply(coeff->p[i][0], coeff->p[i-1][0], d0);
60 Vector_Combine(coeff->p[i-1], coeff->p[i-1]+1, coeff->p[i]+1,
61 one, d0, i);
62 value_set_si(coeff->p[i][d+1], i);
63 value_multiply(coeff->p[i][d+1], coeff->p[i][d+1], coeff->p[i-1][d+1]);
64 value_decrement(d0, d0);
66 value_clear(d0);
67 value_clear(one);
69 void div(dpoly& d, Vector *count, int sign) {
70 int len = coeff->NbRows;
71 Matrix * c = Matrix_Alloc(coeff->NbRows, coeff->NbColumns);
72 Value tmp;
73 value_init(tmp);
74 for (int i = 0; i < len; ++i) {
75 Vector_Copy(coeff->p[i], c->p[i], len+1);
76 for (int j = 1; j <= i; ++j) {
77 value_multiply(tmp, d.coeff->p[j], c->p[i][len]);
78 value_oppose(tmp, tmp);
79 Vector_Combine(c->p[i], c->p[i-j], c->p[i],
80 c->p[i-j][len], tmp, len);
81 value_multiply(c->p[i][len], c->p[i][len], c->p[i-j][len]);
83 value_multiply(c->p[i][len], c->p[i][len], d.coeff->p[0]);
85 if (sign == -1) {
86 value_set_si(tmp, -1);
87 Vector_Scale(c->p[len-1], count->p, tmp, len);
88 value_assign(count->p[len], c->p[len-1][len]);
89 } else
90 Vector_Copy(c->p[len-1], count->p, len+1);
91 Vector_Normalize(count->p, len+1);
92 value_clear(tmp);
93 Matrix_Free(c);
97 const int MAX_TRY=10;
99 * Searches for a vector that is not orthogonal to any
100 * of the rays in rays.
102 static void nonorthog(mat_ZZ& rays, vec_ZZ& lambda)
104 int dim = rays.NumCols();
105 bool found = false;
106 lambda.SetLength(dim);
107 if (dim == 0)
108 return;
110 for (int i = 2; !found && i <= 50*dim; i+=4) {
111 for (int j = 0; j < MAX_TRY; ++j) {
112 for (int k = 0; k < dim; ++k) {
113 int r = random_int(i)+2;
114 int v = (2*(r%2)-1) * (r >> 1);
115 lambda[k] = v;
117 int k = 0;
118 for (; k < rays.NumRows(); ++k)
119 if (lambda * rays[k] == 0)
120 break;
121 if (k == rays.NumRows()) {
122 found = true;
123 break;
127 assert(found);
130 static void add_rays(mat_ZZ& rays, Polyhedron *i, int *r, int nvar = -1,
131 bool all = false)
133 unsigned dim = i->Dimension;
134 if (nvar == -1)
135 nvar = dim;
136 for (int k = 0; k < i->NbRays; ++k) {
137 if (!value_zero_p(i->Ray[k][dim+1]))
138 continue;
139 if (!all && nvar != dim && First_Non_Zero(i->Ray[k]+1, nvar) == -1)
140 continue;
141 values2zz(i->Ray[k]+1, rays[(*r)++], nvar);
145 static void mask_r(Matrix *f, int nr, Vector *lcm, int p, Vector *val, evalue *ev)
147 unsigned nparam = lcm->Size;
149 if (p == nparam) {
150 Vector * prod = Vector_Alloc(f->NbRows);
151 Matrix_Vector_Product(f, val->p, prod->p);
152 int isint = 1;
153 for (int i = 0; i < nr; ++i) {
154 value_modulus(prod->p[i], prod->p[i], f->p[i][nparam+1]);
155 isint &= value_zero_p(prod->p[i]);
157 value_set_si(ev->d, 1);
158 value_init(ev->x.n);
159 value_set_si(ev->x.n, isint);
160 Vector_Free(prod);
161 return;
164 Value tmp;
165 value_init(tmp);
166 if (value_one_p(lcm->p[p]))
167 mask_r(f, nr, lcm, p+1, val, ev);
168 else {
169 value_assign(tmp, lcm->p[p]);
170 value_set_si(ev->d, 0);
171 ev->x.p = new_enode(periodic, VALUE_TO_INT(tmp), p+1);
172 do {
173 value_decrement(tmp, tmp);
174 value_assign(val->p[p], tmp);
175 mask_r(f, nr, lcm, p+1, val, &ev->x.p->arr[VALUE_TO_INT(tmp)]);
176 } while (value_pos_p(tmp));
178 value_clear(tmp);
181 static void mask_fractional(Matrix *f, evalue *factor)
183 int nr = f->NbRows, nc = f->NbColumns;
184 int n;
185 bool found = false;
186 for (n = 0; n < nr && value_notzero_p(f->p[n][nc-1]); ++n)
187 if (value_notone_p(f->p[n][nc-1]) &&
188 value_notmone_p(f->p[n][nc-1]))
189 found = true;
190 if (!found)
191 return;
193 evalue EP;
194 nr = n;
196 Value m;
197 value_init(m);
199 evalue EV;
200 value_init(EV.d);
201 value_init(EV.x.n);
202 value_set_si(EV.x.n, 1);
204 for (n = 0; n < nr; ++n) {
205 value_assign(m, f->p[n][nc-1]);
206 if (value_one_p(m) || value_mone_p(m))
207 continue;
209 int j = normal_mod(f->p[n], nc-1, &m);
210 if (j == nc-1) {
211 free_evalue_refs(factor);
212 value_init(factor->d);
213 evalue_set_si(factor, 0, 1);
214 break;
216 vec_ZZ row;
217 values2zz(f->p[n], row, nc-1);
218 ZZ g;
219 value2zz(m, g);
220 if (j < (nc-1)-1 && row[j] > g/2) {
221 for (int k = j; k < (nc-1); ++k)
222 if (row[k] != 0)
223 row[k] = g - row[k];
226 value_init(EP.d);
227 value_set_si(EP.d, 0);
228 EP.x.p = new_enode(relation, 2, 0);
229 value_clear(EP.x.p->arr[1].d);
230 EP.x.p->arr[1] = *factor;
231 evalue *ev = &EP.x.p->arr[0];
232 value_set_si(ev->d, 0);
233 ev->x.p = new_enode(fractional, 3, -1);
234 evalue_set_si(&ev->x.p->arr[1], 0, 1);
235 evalue_set_si(&ev->x.p->arr[2], 1, 1);
236 evalue *E = multi_monom(row);
237 value_assign(EV.d, m);
238 emul(&EV, E);
239 value_clear(ev->x.p->arr[0].d);
240 ev->x.p->arr[0] = *E;
241 delete E;
242 *factor = EP;
245 value_clear(m);
246 free_evalue_refs(&EV);
252 static void mask_table(Matrix *f, evalue *factor)
254 int nr = f->NbRows, nc = f->NbColumns;
255 int n;
256 bool found = false;
257 for (n = 0; n < nr && value_notzero_p(f->p[n][nc-1]); ++n)
258 if (value_notone_p(f->p[n][nc-1]) &&
259 value_notmone_p(f->p[n][nc-1]))
260 found = true;
261 if (!found)
262 return;
264 Value tmp;
265 value_init(tmp);
266 nr = n;
267 unsigned np = nc - 2;
268 Vector *lcm = Vector_Alloc(np);
269 Vector *val = Vector_Alloc(nc);
270 Vector_Set(val->p, 0, nc);
271 value_set_si(val->p[np], 1);
272 Vector_Set(lcm->p, 1, np);
273 for (n = 0; n < nr; ++n) {
274 if (value_one_p(f->p[n][nc-1]) ||
275 value_mone_p(f->p[n][nc-1]))
276 continue;
277 for (int j = 0; j < np; ++j)
278 if (value_notzero_p(f->p[n][j])) {
279 Gcd(f->p[n][j], f->p[n][nc-1], &tmp);
280 value_division(tmp, f->p[n][nc-1], tmp);
281 value_lcm(tmp, lcm->p[j], &lcm->p[j]);
284 evalue EP;
285 value_init(EP.d);
286 mask_r(f, nr, lcm, 0, val, &EP);
287 value_clear(tmp);
288 Vector_Free(val);
289 Vector_Free(lcm);
290 emul(&EP,factor);
291 free_evalue_refs(&EP);
294 static void mask(Matrix *f, evalue *factor, barvinok_options *options)
296 if (options->lookup_table)
297 mask_table(f, factor);
298 else
299 mask_fractional(f, factor);
302 struct bfe_term : public bfc_term_base {
303 vector<evalue *> factors;
305 bfe_term(int len) : bfc_term_base(len) {
308 ~bfe_term() {
309 for (int i = 0; i < factors.size(); ++i) {
310 if (!factors[i])
311 continue;
312 free_evalue_refs(factors[i]);
313 delete factors[i];
318 static void print_int_vector(int *v, int len, const char *name)
320 cerr << name << endl;
321 for (int j = 0; j < len; ++j) {
322 cerr << v[j] << " ";
324 cerr << endl;
327 static void print_bfc_terms(mat_ZZ& factors, bfc_vec& v)
329 cerr << endl;
330 cerr << "factors" << endl;
331 cerr << factors << endl;
332 for (int i = 0; i < v.size(); ++i) {
333 cerr << "term: " << i << endl;
334 print_int_vector(v[i]->powers, factors.NumRows(), "powers");
335 cerr << "terms" << endl;
336 cerr << v[i]->terms << endl;
337 bfc_term* bfct = static_cast<bfc_term *>(v[i]);
338 cerr << bfct->c << endl;
342 static void print_bfe_terms(mat_ZZ& factors, bfc_vec& v)
344 cerr << endl;
345 cerr << "factors" << endl;
346 cerr << factors << endl;
347 for (int i = 0; i < v.size(); ++i) {
348 cerr << "term: " << i << endl;
349 print_int_vector(v[i]->powers, factors.NumRows(), "powers");
350 cerr << "terms" << endl;
351 cerr << v[i]->terms << endl;
352 bfe_term* bfet = static_cast<bfe_term *>(v[i]);
353 for (int j = 0; j < v[i]->terms.NumRows(); ++j) {
354 const char * test[] = {"a", "b"};
355 print_evalue(stderr, bfet->factors[j], test);
356 fprintf(stderr, "\n");
361 struct bfcounter : public bfcounter_base {
362 mpq_t count;
363 Value tz;
365 bfcounter(unsigned dim) : bfcounter_base(dim) {
366 mpq_init(count);
367 lower = 1;
368 value_init(tz);
370 ~bfcounter() {
371 mpq_clear(count);
372 value_clear(tz);
374 virtual void base(mat_ZZ& factors, bfc_vec& v);
375 virtual void get_count(Value *result) {
376 assert(value_one_p(&count[0]._mp_den));
377 value_assign(*result, &count[0]._mp_num);
381 void bfcounter::base(mat_ZZ& factors, bfc_vec& v)
383 unsigned nf = factors.NumRows();
385 for (int i = 0; i < v.size(); ++i) {
386 bfc_term* bfct = static_cast<bfc_term *>(v[i]);
387 int total_power = 0;
388 // factor is always positive, so we always
389 // change signs
390 for (int k = 0; k < nf; ++k)
391 total_power += v[i]->powers[k];
393 int j;
394 for (j = 0; j < nf; ++j)
395 if (v[i]->powers[j] > 0)
396 break;
398 zz2value(factors[j][0], tz);
399 dpoly D(total_power, tz, 1);
400 for (int k = 1; k < v[i]->powers[j]; ++k) {
401 zz2value(factors[j][0], tz);
402 dpoly fact(total_power, tz, 1);
403 D *= fact;
405 for ( ; ++j < nf; )
406 for (int k = 0; k < v[i]->powers[j]; ++k) {
407 zz2value(factors[j][0], tz);
408 dpoly fact(total_power, tz, 1);
409 D *= fact;
412 for (int k = 0; k < v[i]->terms.NumRows(); ++k) {
413 zz2value(v[i]->terms[k][0], tz);
414 dpoly n(total_power, tz);
415 mpq_set_si(tcount, 0, 1);
416 n.div(D, tcount, 1);
417 if (total_power % 2)
418 bfct->c[k].n = -bfct->c[k].n;
419 zz2value(bfct->c[k].n, tn);
420 zz2value(bfct->c[k].d, td);
422 mpz_mul(mpq_numref(tcount), mpq_numref(tcount), tn);
423 mpz_mul(mpq_denref(tcount), mpq_denref(tcount), td);
424 mpq_canonicalize(tcount);
425 mpq_add(count, count, tcount);
427 delete v[i];
432 /* Check whether the polyhedron is unbounded and if so,
433 * check whether it has any (and therefore an infinite number of)
434 * integer points.
435 * If one of the vertices is integer, then we are done.
436 * Otherwise, transform the polyhedron such that one of the rays
437 * is the first unit vector and cut it off at a height that ensures
438 * that if the whole polyhedron has any points, then the remaining part
439 * has integer points. In particular we add the largest coefficient
440 * of a ray to the highest vertex (rounded up).
442 static bool Polyhedron_is_infinite(Polyhedron *P, Value* result,
443 barvinok_options *options)
445 int r = 0;
446 Matrix *M, *M2;
447 Value c, tmp;
448 Value g;
449 bool first;
450 Vector *v;
451 Value offset, size;
452 Polyhedron *R;
454 if (P->NbBid == 0)
455 for (; r < P->NbRays; ++r)
456 if (value_zero_p(P->Ray[r][P->Dimension+1]))
457 break;
458 if (P->NbBid == 0 && r == P->NbRays)
459 return false;
461 if (options->count_sample_infinite) {
462 Vector *sample;
464 sample = Polyhedron_Sample(P, options);
465 if (!sample)
466 value_set_si(*result, 0);
467 else {
468 value_set_si(*result, -1);
469 Vector_Free(sample);
471 return true;
474 for (int i = 0; i < P->NbRays; ++i)
475 if (value_one_p(P->Ray[i][1+P->Dimension])) {
476 value_set_si(*result, -1);
477 return true;
480 value_init(g);
481 M = Matrix_Alloc(P->Dimension+1, P->Dimension+1);
482 Vector_Gcd(P->Ray[r]+1, P->Dimension, &g);
483 Vector_AntiScale(P->Ray[r]+1, M->p[0], g, P->Dimension+1);
484 int ok = unimodular_complete(M, 1);
485 assert(ok);
486 value_set_si(M->p[P->Dimension][P->Dimension], 1);
487 M2 = Transpose(M);
488 Matrix_Free(M);
489 P = Polyhedron_Preimage(P, M2, 0);
490 Matrix_Free(M2);
491 value_clear(g);
493 first = true;
494 value_init(offset);
495 value_init(size);
496 value_init(tmp);
497 value_set_si(size, 0);
499 for (int i = 0; i < P->NbBid; ++i) {
500 value_absolute(tmp, P->Ray[i][1]);
501 if (value_gt(tmp, size))
502 value_assign(size, tmp);
504 for (int i = P->NbBid; i < P->NbRays; ++i) {
505 if (value_zero_p(P->Ray[i][P->Dimension+1])) {
506 if (value_gt(P->Ray[i][1], size))
507 value_assign(size, P->Ray[i][1]);
508 continue;
510 mpz_cdiv_q(tmp, P->Ray[i][1], P->Ray[i][P->Dimension+1]);
511 if (first || value_gt(tmp, offset)) {
512 value_assign(offset, tmp);
513 first = false;
516 value_addto(offset, offset, size);
517 value_clear(size);
518 value_clear(tmp);
520 v = Vector_Alloc(P->Dimension+2);
521 value_set_si(v->p[0], 1);
522 value_set_si(v->p[1], -1);
523 value_assign(v->p[1+P->Dimension], offset);
524 R = AddConstraints(v->p, 1, P, options->MaxRays);
525 Polyhedron_Free(P);
526 P = R;
528 value_clear(offset);
529 Vector_Free(v);
531 value_init(c);
532 barvinok_count_with_options(P, &c, options);
533 Polyhedron_Free(P);
534 if (value_zero_p(c))
535 value_set_si(*result, 0);
536 else
537 value_set_si(*result, -1);
538 value_clear(c);
540 return true;
543 static void barvinok_count_f(Polyhedron *P, Value* result,
544 barvinok_options *options);
546 void barvinok_count_with_options(Polyhedron *P, Value* result,
547 struct barvinok_options *options)
549 unsigned dim;
550 int allocated = 0;
551 Polyhedron *Q;
552 bool infinite = false;
554 if (P->next)
555 fprintf(stderr,
556 "barvinok_count: input is a union; only first polyhedron is counted\n");
558 if (emptyQ2(P)) {
559 value_set_si(*result, 0);
560 return;
562 if (P->NbEq != 0) {
563 Q = NULL;
564 do {
565 P = remove_equalities(P, options->MaxRays);
566 P = DomainConstraintSimplify(P, options->MaxRays);
567 if (Q)
568 Polyhedron_Free(Q);
569 Q = P;
570 } while (!emptyQ(P) && P->NbEq != 0);
571 if (emptyQ(P)) {
572 Polyhedron_Free(P);
573 value_set_si(*result, 0);
574 return;
576 allocated = 1;
578 if (Polyhedron_is_infinite(P, result, options)) {
579 if (allocated)
580 Polyhedron_Free(P);
581 return;
583 if (P->Dimension == 0) {
584 /* Test whether the constraints are satisfied */
585 POL_ENSURE_VERTICES(P);
586 value_set_si(*result, !emptyQ(P));
587 if (allocated)
588 Polyhedron_Free(P);
589 return;
591 Q = Polyhedron_Factor(P, 0, NULL, options->MaxRays);
592 if (Q) {
593 if (allocated)
594 Polyhedron_Free(P);
595 P = Q;
596 allocated = 1;
599 barvinok_count_f(P, result, options);
600 if (value_neg_p(*result))
601 infinite = true;
602 if (Q && P->next && value_notzero_p(*result)) {
603 Value factor;
604 value_init(factor);
606 for (Q = P->next; Q; Q = Q->next) {
607 barvinok_count_f(Q, &factor, options);
608 if (value_neg_p(factor)) {
609 infinite = true;
610 continue;
611 } else if (Q->next && value_zero_p(factor)) {
612 value_set_si(*result, 0);
613 break;
615 value_multiply(*result, *result, factor);
618 value_clear(factor);
621 if (allocated)
622 Domain_Free(P);
623 if (infinite)
624 value_set_si(*result, -1);
627 void barvinok_count(Polyhedron *P, Value* result, unsigned NbMaxCons)
629 barvinok_options *options = barvinok_options_new_with_defaults();
630 options->MaxRays = NbMaxCons;
631 barvinok_count_with_options(P, result, options);
632 barvinok_options_free(options);
635 static void barvinok_count_f(Polyhedron *P, Value* result,
636 barvinok_options *options)
638 if (emptyQ2(P)) {
639 value_set_si(*result, 0);
640 return;
643 if (P->Dimension == 1)
644 return Line_Length(P, result);
646 int c = P->NbConstraints;
647 POL_ENSURE_FACETS(P);
648 if (c != P->NbConstraints || P->NbEq != 0) {
649 Polyhedron *next = P->next;
650 P->next = NULL;
651 barvinok_count_with_options(P, result, options);
652 P->next = next;
653 return;
656 POL_ENSURE_VERTICES(P);
658 if (Polyhedron_is_infinite(P, result, options))
659 return;
661 np_base *cnt;
662 if (options->incremental_specialization == BV_SPECIALIZATION_BF)
663 cnt = new bfcounter(P->Dimension);
664 else if (options->incremental_specialization == BV_SPECIALIZATION_DF)
665 cnt = new icounter(P->Dimension);
666 else if (options->incremental_specialization == BV_SPECIALIZATION_TODD)
667 cnt = new tcounter(P->Dimension, options->max_index);
668 else
669 cnt = new counter(P->Dimension, options->max_index);
670 cnt->start(P, options);
672 cnt->get_count(result);
673 delete cnt;
676 static void uni_polynom(int param, Vector *c, evalue *EP)
678 unsigned dim = c->Size-2;
679 value_init(EP->d);
680 value_set_si(EP->d,0);
681 EP->x.p = new_enode(polynomial, dim+1, param+1);
682 for (int j = 0; j <= dim; ++j)
683 evalue_set(&EP->x.p->arr[j], c->p[j], c->p[dim+1]);
686 typedef evalue * evalue_p;
688 struct enumerator_base {
689 unsigned dim;
690 evalue ** vE;
691 evalue mone;
692 vertex_decomposer *vpd;
694 enumerator_base(unsigned dim, vertex_decomposer *vpd)
696 this->dim = dim;
697 this->vpd = vpd;
699 vE = new evalue_p[vpd->nbV];
700 for (int j = 0; j < vpd->nbV; ++j)
701 vE[j] = 0;
703 value_init(mone.d);
704 evalue_set_si(&mone, -1, 1);
707 void decompose_at(Param_Vertices *V, int _i, barvinok_options *options) {
708 //this->pVD = pVD;
710 vE[_i] = new evalue;
711 value_init(vE[_i]->d);
712 evalue_set_si(vE[_i], 0, 1);
714 vpd->decompose_at_vertex(V, _i, options);
717 virtual ~enumerator_base() {
718 for (int j = 0; j < vpd->nbV; ++j)
719 if (vE[j]) {
720 free_evalue_refs(vE[j]);
721 delete vE[j];
723 delete [] vE;
725 free_evalue_refs(&mone);
728 static enumerator_base *create(Polyhedron *P, unsigned dim, unsigned nbV,
729 barvinok_options *options);
732 struct enumerator : public signed_cone_consumer, public vertex_decomposer,
733 public enumerator_base {
734 vec_ZZ lambda;
735 vec_ZZ den;
736 term_info num;
737 Vector *c;
738 mpq_t count;
739 Value tz;
741 enumerator(Polyhedron *P, unsigned dim, unsigned nbV) :
742 vertex_decomposer(P, nbV, *this), enumerator_base(dim, this) {
743 this->P = P;
744 this->nbV = nbV;
745 randomvector(P, lambda, dim);
746 den.SetLength(dim);
747 c = Vector_Alloc(dim+2);
749 mpq_init(count);
750 value_init(tz);
753 ~enumerator() {
754 mpq_clear(count);
755 Vector_Free(c);
756 value_clear(tz);
759 virtual void handle(const signed_cone& sc, barvinok_options *options);
762 void enumerator::handle(const signed_cone& sc, barvinok_options *options)
764 int sign = sc.sign;
765 int r = 0;
766 assert(sc.rays.NumRows() == dim);
767 for (int k = 0; k < dim; ++k) {
768 if (lambda * sc.rays[k] == 0)
769 throw Orthogonal;
772 lattice_point(V, sc.rays, lambda, &num, sc.det, options);
773 den = sc.rays * lambda;
775 if (dim % 2)
776 sign = -sign;
778 zz2value(den[0], tz);
779 dpoly n(dim, tz, 1);
780 for (int k = 1; k < dim; ++k) {
781 zz2value(den[k], tz);
782 dpoly fact(dim, tz, 1);
783 n *= fact;
785 if (num.E != NULL) {
786 dpoly_n d(dim);
787 d.div(n, c, sign);
788 for (unsigned long i = 0; i < sc.det; ++i) {
789 evalue *EV = evalue_polynomial(c, num.E[i]);
790 eadd(EV, vE[vert]);
791 free_evalue_refs(EV);
792 free(EV);
793 free_evalue_refs(num.E[i]);
794 delete num.E[i];
796 delete [] num.E;
797 } else {
798 mpq_set_si(count, 0, 1);
799 if (num.constant.length() == 1) {
800 zz2value(num.constant[0], tz);
801 dpoly d(dim, tz);
802 d.div(n, count, sign);
803 } else {
804 dpoly_n d(dim);
805 d.div(n, c, sign);
806 Value x, sum, acc;
807 value_init(x);
808 value_init(acc);
809 for (unsigned long i = 0; i < sc.det; ++i) {
810 value_assign(acc, c->p[dim]);
811 zz2value(num.constant[i], x);
812 for (int j = dim-1; j >= 0; --j) {
813 value_multiply(acc, acc, x);
814 value_addto(acc, acc, c->p[j]);
816 value_addto(mpq_numref(count), mpq_numref(count), acc);
818 mpz_set(mpq_denref(count), c->p[dim+1]);
819 value_clear(acc);
820 value_clear(x);
822 evalue EV;
823 value_init(EV.d);
824 evalue_set(&EV, &count[0]._mp_num, &count[0]._mp_den);
825 eadd(&EV, vE[vert]);
826 free_evalue_refs(&EV);
830 struct ienumerator_base : enumerator_base {
831 evalue ** E_vertex;
833 ienumerator_base(unsigned dim, vertex_decomposer *vpd) :
834 enumerator_base(dim,vpd) {
835 E_vertex = new evalue_p[dim];
838 virtual ~ienumerator_base() {
839 delete [] E_vertex;
842 evalue *E_num(int i, int d) {
843 return E_vertex[i + (dim-d)];
847 struct cumulator {
848 evalue *factor;
849 evalue *v;
850 dpoly_r *r;
852 cumulator(evalue *factor, evalue *v, dpoly_r *r) :
853 factor(factor), v(v), r(r) {}
855 void cumulate(barvinok_options *options);
857 virtual void add_term(const vector<int>& powers, evalue *f2) = 0;
858 virtual ~cumulator() {}
861 void cumulator::cumulate(barvinok_options *options)
863 evalue cum; // factor * 1 * E_num[0]/1 * (E_num[0]-1)/2 *...
864 evalue f;
865 evalue t; // E_num[0] - (m-1)
866 evalue *cst;
867 evalue mone;
869 if (options->lookup_table) {
870 value_init(mone.d);
871 evalue_set_si(&mone, -1, 1);
874 value_init(cum.d);
875 evalue_copy(&cum, factor);
876 value_init(f.d);
877 value_init(f.x.n);
878 value_set_si(f.d, 1);
879 value_set_si(f.x.n, 1);
880 value_init(t.d);
881 evalue_copy(&t, v);
883 if (!options->lookup_table) {
884 for (cst = &t; value_zero_p(cst->d); ) {
885 if (cst->x.p->type == fractional)
886 cst = &cst->x.p->arr[1];
887 else
888 cst = &cst->x.p->arr[0];
892 for (int m = 0; m < r->len; ++m) {
893 if (m > 0) {
894 if (m > 1) {
895 value_set_si(f.d, m);
896 emul(&f, &cum);
897 if (!options->lookup_table)
898 value_subtract(cst->x.n, cst->x.n, cst->d);
899 else
900 eadd(&mone, &t);
902 emul(&t, &cum);
904 dpoly_r_term_list& current = r->c[r->len-1-m];
905 dpoly_r_term_list::iterator j;
906 for (j = current.begin(); j != current.end(); ++j) {
907 if ((*j)->coeff == 0)
908 continue;
909 evalue *f2 = new evalue;
910 value_init(f2->d);
911 value_init(f2->x.n);
912 zz2value((*j)->coeff, f2->x.n);
913 zz2value(r->denom, f2->d);
914 emul(&cum, f2);
916 add_term((*j)->powers, f2);
919 free_evalue_refs(&f);
920 free_evalue_refs(&t);
921 free_evalue_refs(&cum);
922 if (options->lookup_table)
923 free_evalue_refs(&mone);
926 struct E_poly_term {
927 vector<int> powers;
928 evalue *E;
931 struct ie_cum : public cumulator {
932 vector<E_poly_term *> terms;
934 ie_cum(evalue *factor, evalue *v, dpoly_r *r) : cumulator(factor, v, r) {}
936 virtual void add_term(const vector<int>& powers, evalue *f2);
939 void ie_cum::add_term(const vector<int>& powers, evalue *f2)
941 int k;
942 for (k = 0; k < terms.size(); ++k) {
943 if (terms[k]->powers == powers) {
944 eadd(f2, terms[k]->E);
945 free_evalue_refs(f2);
946 delete f2;
947 break;
950 if (k >= terms.size()) {
951 E_poly_term *ET = new E_poly_term;
952 ET->powers = powers;
953 ET->E = f2;
954 terms.push_back(ET);
958 struct ienumerator : public signed_cone_consumer, public vertex_decomposer,
959 public ienumerator_base {
960 //Polyhedron *pVD;
961 mat_ZZ den;
962 mat_ZZ vertex;
963 mpq_t tcount;
964 Value tz;
966 ienumerator(Polyhedron *P, unsigned dim, unsigned nbV) :
967 vertex_decomposer(P, nbV, *this), ienumerator_base(dim, this) {
968 vertex.SetDims(1, dim);
970 den.SetDims(dim, dim);
971 mpq_init(tcount);
972 value_init(tz);
975 ~ienumerator() {
976 mpq_clear(tcount);
977 value_clear(tz);
980 virtual void handle(const signed_cone& sc, barvinok_options *options);
981 void reduce(evalue *factor, const mat_ZZ& num, const mat_ZZ& den_f,
982 barvinok_options *options);
985 void ienumerator::reduce(evalue *factor, const mat_ZZ& num, const mat_ZZ& den_f,
986 barvinok_options *options)
988 unsigned len = den_f.NumRows(); // number of factors in den
989 unsigned dim = num.NumCols();
990 assert(num.NumRows() == 1);
992 if (dim == 0) {
993 eadd(factor, vE[vert]);
994 return;
997 vec_ZZ den_s;
998 mat_ZZ den_r;
999 vec_ZZ num_s;
1000 mat_ZZ num_p;
1002 split_one(num, num_s, num_p, den_f, den_s, den_r);
1004 vec_ZZ den_p;
1005 den_p.SetLength(len);
1007 ZZ one;
1008 one = 1;
1009 normalize(one, num_s, num_p, den_s, den_p, den_r);
1010 if (one != 1)
1011 emul(&mone, factor);
1013 int only_param = 0;
1014 int no_param = 0;
1015 for (int k = 0; k < len; ++k) {
1016 if (den_p[k] == 0)
1017 ++no_param;
1018 else if (den_s[k] == 0)
1019 ++only_param;
1021 if (no_param == 0) {
1022 reduce(factor, num_p, den_r, options);
1023 } else {
1024 int k, l;
1025 mat_ZZ pden;
1026 pden.SetDims(only_param, dim-1);
1028 for (k = 0, l = 0; k < len; ++k)
1029 if (den_s[k] == 0)
1030 pden[l++] = den_r[k];
1032 for (k = 0; k < len; ++k)
1033 if (den_p[k] == 0)
1034 break;
1036 zz2value(num_s[0], tz);
1037 dpoly n(no_param, tz);
1038 zz2value(den_s[k], tz);
1039 dpoly D(no_param, tz, 1);
1040 for ( ; ++k < len; )
1041 if (den_p[k] == 0) {
1042 zz2value(den_s[k], tz);
1043 dpoly fact(no_param, tz, 1);
1044 D *= fact;
1047 dpoly_r * r = 0;
1048 // if no_param + only_param == len then all powers
1049 // below will be all zero
1050 if (no_param + only_param == len) {
1051 if (E_num(0, dim) != 0)
1052 r = new dpoly_r(n, len);
1053 else {
1054 mpq_set_si(tcount, 0, 1);
1055 one = 1;
1056 n.div(D, tcount, 1);
1058 if (value_notzero_p(mpq_numref(tcount))) {
1059 evalue f;
1060 value_init(f.d);
1061 value_init(f.x.n);
1062 value_assign(f.x.n, mpq_numref(tcount));
1063 value_assign(f.d, mpq_denref(tcount));
1064 emul(&f, factor);
1065 reduce(factor, num_p, pden, options);
1066 free_evalue_refs(&f);
1068 return;
1070 } else {
1071 for (k = 0; k < len; ++k) {
1072 if (den_s[k] == 0 || den_p[k] == 0)
1073 continue;
1075 zz2value(den_s[k], tz);
1076 dpoly pd(no_param-1, tz, 1);
1078 int l;
1079 for (l = 0; l < k; ++l)
1080 if (den_r[l] == den_r[k])
1081 break;
1083 if (r == 0)
1084 r = new dpoly_r(n, pd, l, len);
1085 else {
1086 dpoly_r *nr = new dpoly_r(r, pd, l, len);
1087 delete r;
1088 r = nr;
1092 dpoly_r *rc = r->div(D);
1093 delete r;
1094 r = rc;
1095 if (E_num(0, dim) == 0) {
1096 int common = pden.NumRows();
1097 dpoly_r_term_list& final = r->c[r->len-1];
1098 int rows;
1099 evalue t;
1100 evalue f;
1101 value_init(f.d);
1102 value_init(f.x.n);
1103 zz2value(r->denom, f.d);
1104 dpoly_r_term_list::iterator j;
1105 for (j = final.begin(); j != final.end(); ++j) {
1106 if ((*j)->coeff == 0)
1107 continue;
1108 rows = common;
1109 for (int k = 0; k < r->dim; ++k) {
1110 int n = (*j)->powers[k];
1111 if (n == 0)
1112 continue;
1113 pden.SetDims(rows+n, pden.NumCols());
1114 for (int l = 0; l < n; ++l)
1115 pden[rows+l] = den_r[k];
1116 rows += n;
1118 value_init(t.d);
1119 evalue_copy(&t, factor);
1120 zz2value((*j)->coeff, f.x.n);
1121 emul(&f, &t);
1122 reduce(&t, num_p, pden, options);
1123 free_evalue_refs(&t);
1125 free_evalue_refs(&f);
1126 } else {
1127 ie_cum cum(factor, E_num(0, dim), r);
1128 cum.cumulate(options);
1130 int common = pden.NumRows();
1131 int rows;
1132 for (int j = 0; j < cum.terms.size(); ++j) {
1133 rows = common;
1134 pden.SetDims(rows, pden.NumCols());
1135 for (int k = 0; k < r->dim; ++k) {
1136 int n = cum.terms[j]->powers[k];
1137 if (n == 0)
1138 continue;
1139 pden.SetDims(rows+n, pden.NumCols());
1140 for (int l = 0; l < n; ++l)
1141 pden[rows+l] = den_r[k];
1142 rows += n;
1144 reduce(cum.terms[j]->E, num_p, pden, options);
1145 free_evalue_refs(cum.terms[j]->E);
1146 delete cum.terms[j]->E;
1147 delete cum.terms[j];
1150 delete r;
1154 static int type_offset(enode *p)
1156 return p->type == fractional ? 1 :
1157 p->type == flooring ? 1 : 0;
1160 static int edegree(evalue *e)
1162 int d = 0;
1163 enode *p;
1165 if (value_notzero_p(e->d))
1166 return 0;
1168 p = e->x.p;
1169 int i = type_offset(p);
1170 if (p->size-i-1 > d)
1171 d = p->size - i - 1;
1172 for (; i < p->size; i++) {
1173 int d2 = edegree(&p->arr[i]);
1174 if (d2 > d)
1175 d = d2;
1177 return d;
1180 void ienumerator::handle(const signed_cone& sc, barvinok_options *options)
1182 assert(sc.det == 1);
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.rays.NumRows() == enumerator_base::dim);
1365 bfe_term* t = new bfe_term(enumerator_base::dim);
1366 vector< bfc_term_base * > v;
1367 v.push_back(t);
1369 t->factors.resize(1);
1371 t->terms.SetDims(1, enumerator_base::dim);
1372 lattice_point(V, sc.rays, t->terms[0], E_vertex, options);
1374 // the elements of factors are always lexpositive
1375 mat_ZZ factors;
1376 int s = setup_factors(sc.rays, factors, t, sc.sign);
1378 t->factors[0] = new evalue;
1379 value_init(t->factors[0]->d);
1380 evalue_set_si(t->factors[0], s, 1);
1381 reduce(factors, v, options);
1383 for (int i = 0; i < enumerator_base::dim; ++i)
1384 if (E_vertex[i]) {
1385 free_evalue_refs(E_vertex[i]);
1386 delete E_vertex[i];
1390 static evalue* barvinok_enumerate_ev_f(Polyhedron *P, Polyhedron* C,
1391 barvinok_options *options);
1393 /* Destroys C */
1394 static evalue* barvinok_enumerate_cst(Polyhedron *P, Polyhedron* C,
1395 struct barvinok_options *options)
1397 evalue *eres;
1399 if (emptyQ2(C)) {
1400 Polyhedron_Free(C);
1401 return evalue_zero();
1404 ALLOC(evalue, eres);
1405 value_init(eres->d);
1406 value_set_si(eres->d, 0);
1407 eres->x.p = new_enode(partition, 2, C->Dimension);
1408 EVALUE_SET_DOMAIN(eres->x.p->arr[0],
1409 DomainConstraintSimplify(C, options->MaxRays));
1410 value_set_si(eres->x.p->arr[1].d, 1);
1411 value_init(eres->x.p->arr[1].x.n);
1412 if (emptyQ2(P))
1413 value_set_si(eres->x.p->arr[1].x.n, 0);
1414 else
1415 barvinok_count_with_options(P, &eres->x.p->arr[1].x.n, options);
1417 return eres;
1420 static evalue* enumerate(Polyhedron *P, Polyhedron* C,
1421 struct barvinok_options *options)
1423 Polyhedron *next;
1424 Polyhedron *Porig = P;
1425 Polyhedron *Corig = C;
1426 Polyhedron *CEq = NULL, *rVD;
1427 int r = 0;
1428 unsigned nparam = C->Dimension;
1429 evalue *eres;
1430 Matrix *CP = NULL;
1432 evalue factor;
1433 value_init(factor.d);
1434 evalue_set_si(&factor, 1, 1);
1436 /* for now */
1437 POL_ENSURE_FACETS(P);
1438 POL_ENSURE_VERTICES(P);
1439 POL_ENSURE_FACETS(C);
1440 POL_ENSURE_VERTICES(C);
1442 if (C->Dimension == 0 || emptyQ(P) || emptyQ(C)) {
1443 constant:
1444 if (CEq == Porig)
1445 CEq = Polyhedron_Copy(CEq);
1446 eres = barvinok_enumerate_cst(P, CEq ? CEq : Polyhedron_Copy(C), options);
1447 out:
1448 if (CP) {
1449 evalue_backsubstitute(eres, CP, options->MaxRays);
1450 Matrix_Free(CP);
1453 emul(&factor, eres);
1454 if (options->approximation_method == BV_APPROX_DROP) {
1455 if (options->polynomial_approximation == BV_APPROX_SIGN_UPPER)
1456 evalue_frac2polynomial(eres, 1, options->MaxRays);
1457 if (options->polynomial_approximation == BV_APPROX_SIGN_LOWER)
1458 evalue_frac2polynomial(eres, -1, options->MaxRays);
1459 if (options->polynomial_approximation == BV_APPROX_SIGN_APPROX)
1460 evalue_frac2polynomial(eres, 0, options->MaxRays);
1462 reduce_evalue(eres);
1463 free_evalue_refs(&factor);
1464 if (P != Porig)
1465 Domain_Free(P);
1466 if (C != Corig)
1467 Polyhedron_Free(C);
1469 return eres;
1471 if (Polyhedron_is_unbounded(P, nparam, options->MaxRays))
1472 goto constant;
1474 if (P->NbEq != 0) {
1475 Matrix *f;
1476 P = remove_equalities_p(Polyhedron_Copy(P), P->Dimension-nparam, &f,
1477 options->MaxRays);
1478 mask(f, &factor, options);
1479 Matrix_Free(f);
1481 if (P->Dimension == nparam) {
1482 CEq = P;
1483 P = Universe_Polyhedron(0);
1484 goto constant;
1486 if (P->NbEq != 0 || C->NbEq != 0) {
1487 Polyhedron *Q = P;
1488 Polyhedron *D = C;
1489 remove_all_equalities(&P, &C, &CP, NULL, nparam, options->MaxRays);
1490 if (C != D && D != Corig)
1491 Polyhedron_Free(D);
1492 if (P != Q && Q != Porig)
1493 Domain_Free(Q);
1494 eres = enumerate(P, C, options);
1495 goto out;
1498 Polyhedron *T = Polyhedron_Factor(P, nparam, NULL, options->MaxRays);
1499 if (T || (P->Dimension == nparam+1)) {
1500 Polyhedron *Q;
1501 Polyhedron *C2;
1502 for (Q = T ? T : P; Q; Q = Q->next) {
1503 Polyhedron *next = Q->next;
1504 Q->next = NULL;
1506 Polyhedron *QC = Q;
1507 if (Q->Dimension != C->Dimension)
1508 QC = Polyhedron_Project(Q, nparam);
1510 C2 = C;
1511 C = DomainIntersection(C, QC, options->MaxRays);
1512 if (C2 != Corig)
1513 Polyhedron_Free(C2);
1514 if (QC != Q)
1515 Polyhedron_Free(QC);
1517 Q->next = next;
1520 if (T) {
1521 if (P != Porig)
1522 Polyhedron_Free(P);
1523 P = T;
1524 if (T->Dimension == C->Dimension) {
1525 P = T->next;
1526 T->next = NULL;
1527 Polyhedron_Free(T);
1531 next = P->next;
1532 P->next = NULL;
1533 eres = barvinok_enumerate_ev_f(P, C, options);
1534 P->next = next;
1536 if (P->next) {
1537 Polyhedron *Q;
1538 evalue *f;
1540 for (Q = P->next; Q; Q = Q->next) {
1541 Polyhedron *next = Q->next;
1542 Q->next = NULL;
1544 f = barvinok_enumerate_ev_f(Q, C, options);
1545 emul(f, eres);
1546 free_evalue_refs(f);
1547 free(f);
1549 Q->next = next;
1553 goto out;
1556 evalue* barvinok_enumerate_with_options(Polyhedron *P, Polyhedron* C,
1557 struct barvinok_options *options)
1559 Polyhedron *next, *Cnext, *C1;
1560 Polyhedron *Corig = C;
1561 evalue *eres;
1563 if (P->next)
1564 fprintf(stderr,
1565 "barvinok_enumerate: input is a union; only first polyhedron is enumerated\n");
1567 if (C->next)
1568 fprintf(stderr,
1569 "barvinok_enumerate: context is a union; only first polyhedron is considered\n");
1571 Cnext = C->next;
1572 C->next = NULL;
1573 C1 = Polyhedron_Project(P, C->Dimension);
1574 C = DomainIntersection(C, C1, options->MaxRays);
1575 Polyhedron_Free(C1);
1576 next = P->next;
1577 P->next = NULL;
1579 if (options->approximation_method == BV_APPROX_BERNOULLI)
1580 eres = Bernoulli_sum(P, C, options);
1581 else
1582 eres = enumerate(P, C, options);
1583 Domain_Free(C);
1585 P->next= next;
1586 Corig->next = Cnext;
1588 return eres;
1591 evalue* barvinok_enumerate_ev(Polyhedron *P, Polyhedron* C, unsigned MaxRays)
1593 evalue *E;
1594 barvinok_options *options = barvinok_options_new_with_defaults();
1595 options->MaxRays = MaxRays;
1596 E = barvinok_enumerate_with_options(P, C, options);
1597 barvinok_options_free(options);
1598 return E;
1601 evalue *Param_Polyhedron_Enumerate(Param_Polyhedron *PP, Polyhedron *P,
1602 Polyhedron *C,
1603 struct barvinok_options *options)
1605 evalue *eres;
1606 Param_Domain *D;
1607 unsigned nparam = C->Dimension;
1608 unsigned dim = P->Dimension - nparam;
1610 int nd;
1611 for (nd = 0, D=PP->D; D; ++nd, D=D->next);
1612 evalue_section *s = new evalue_section[nd];
1614 enumerator_base *et = NULL;
1615 try_again:
1616 if (et)
1617 delete et;
1619 et = enumerator_base::create(P, dim, PP->nbV, options);
1621 Polyhedron *TC = true_context(P, C, options->MaxRays);
1622 FORALL_REDUCED_DOMAIN(PP, TC, nd, options, i, D, rVD)
1623 Param_Vertices *V;
1625 s[i].E = evalue_zero();
1626 s[i].D = rVD;
1628 FORALL_PVertex_in_ParamPolyhedron(V,D,PP) // _i is internal counter
1629 if (!et->vE[_i])
1630 try {
1631 et->decompose_at(V, _i, options);
1632 } catch (OrthogonalException &e) {
1633 FORALL_REDUCED_DOMAIN_RESET;
1634 for (; i >= 0; --i) {
1635 free_evalue_refs(s[i].E);
1636 free(s[i].E);
1637 Domain_Free(s[i].D);
1639 goto try_again;
1641 eadd(et->vE[_i] , s[i].E);
1642 END_FORALL_PVertex_in_ParamPolyhedron;
1643 evalue_range_reduction_in_domain(s[i].E, rVD);
1644 END_FORALL_REDUCED_DOMAIN
1645 Polyhedron_Free(TC);
1647 delete et;
1648 eres = evalue_from_section_array(s, nd);
1649 delete [] s;
1651 return eres;
1654 static evalue* barvinok_enumerate_ev_f(Polyhedron *P, Polyhedron* C,
1655 barvinok_options *options)
1657 unsigned nparam = C->Dimension;
1658 bool do_scale = options->approximation_method == BV_APPROX_SCALE;
1660 if (options->approximation_method == BV_APPROX_VOLUME)
1661 return Param_Polyhedron_Volume(P, C, options);
1663 if (P->Dimension - nparam == 1 && !do_scale)
1664 return ParamLine_Length(P, C, options);
1666 Param_Polyhedron *PP = NULL;
1667 evalue *eres;
1669 if (do_scale) {
1670 eres = scale_bound(P, C, options);
1671 if (eres)
1672 return eres;
1675 PP = Polyhedron2Param_Polyhedron(P, C, options);
1677 if (do_scale)
1678 eres = scale(PP, P, C, options);
1679 else
1680 eres = Param_Polyhedron_Enumerate(PP, P, C, options);
1682 if (PP)
1683 Param_Polyhedron_Free(PP);
1685 return eres;
1688 Enumeration* barvinok_enumerate(Polyhedron *P, Polyhedron* C, unsigned MaxRays)
1690 evalue *EP = barvinok_enumerate_ev(P, C, MaxRays);
1692 return partition2enumeration(EP);
1695 evalue* barvinok_enumerate_union(Polyhedron *D, Polyhedron* C, unsigned MaxRays)
1697 evalue *EP;
1698 gen_fun *gf = barvinok_enumerate_union_series(D, C, MaxRays);
1699 EP = *gf;
1700 delete gf;
1701 return EP;
1704 evalue *barvinok_summate(evalue *e, int nvar, struct barvinok_options *options)
1706 if (options->summation == BV_SUM_EULER)
1707 return euler_summate(e, nvar, options);
1708 else
1709 return evalue_sum(e, nvar, options->MaxRays);