barvinok.cc: split off part dealing with existential variables
[barvinok.git] / barvinok.cc
blobc4087ad9c75eefa2ede353d9f8b6615ee41188dd
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 "conversion.h"
18 #include "counter.h"
19 #include "tcounter.h"
20 #include "decomposer.h"
21 #include "lattice_point.h"
22 #include "reduce_domain.h"
23 #include "genfun_constructor.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 /* Check whether all rays point in the positive directions
686 * for the parameters
688 static bool Polyhedron_has_positive_rays(Polyhedron *P, unsigned nparam)
690 int r;
691 for (r = 0; r < P->NbRays; ++r)
692 if (value_zero_p(P->Ray[r][P->Dimension+1])) {
693 int i;
694 for (i = P->Dimension - nparam; i < P->Dimension; ++i)
695 if (value_neg_p(P->Ray[r][i+1]))
696 return false;
698 return true;
701 typedef evalue * evalue_p;
703 struct enumerator_base {
704 unsigned dim;
705 evalue ** vE;
706 evalue mone;
707 vertex_decomposer *vpd;
709 enumerator_base(unsigned dim, vertex_decomposer *vpd)
711 this->dim = dim;
712 this->vpd = vpd;
714 vE = new evalue_p[vpd->nbV];
715 for (int j = 0; j < vpd->nbV; ++j)
716 vE[j] = 0;
718 value_init(mone.d);
719 evalue_set_si(&mone, -1, 1);
722 void decompose_at(Param_Vertices *V, int _i, barvinok_options *options) {
723 //this->pVD = pVD;
725 vE[_i] = new evalue;
726 value_init(vE[_i]->d);
727 evalue_set_si(vE[_i], 0, 1);
729 vpd->decompose_at_vertex(V, _i, options);
732 virtual ~enumerator_base() {
733 for (int j = 0; j < vpd->nbV; ++j)
734 if (vE[j]) {
735 free_evalue_refs(vE[j]);
736 delete vE[j];
738 delete [] vE;
740 free_evalue_refs(&mone);
743 static enumerator_base *create(Polyhedron *P, unsigned dim, unsigned nbV,
744 barvinok_options *options);
747 struct enumerator : public signed_cone_consumer, public vertex_decomposer,
748 public enumerator_base {
749 vec_ZZ lambda;
750 vec_ZZ den;
751 term_info num;
752 Vector *c;
753 mpq_t count;
754 Value tz;
756 enumerator(Polyhedron *P, unsigned dim, unsigned nbV) :
757 vertex_decomposer(P, nbV, *this), enumerator_base(dim, this) {
758 this->P = P;
759 this->nbV = nbV;
760 randomvector(P, lambda, dim);
761 den.SetLength(dim);
762 c = Vector_Alloc(dim+2);
764 mpq_init(count);
765 value_init(tz);
768 ~enumerator() {
769 mpq_clear(count);
770 Vector_Free(c);
771 value_clear(tz);
774 virtual void handle(const signed_cone& sc, barvinok_options *options);
777 void enumerator::handle(const signed_cone& sc, barvinok_options *options)
779 int sign = sc.sign;
780 int r = 0;
781 assert(sc.rays.NumRows() == dim);
782 for (int k = 0; k < dim; ++k) {
783 if (lambda * sc.rays[k] == 0)
784 throw Orthogonal;
787 lattice_point(V, sc.rays, lambda, &num, sc.det, sc.closed, options);
788 den = sc.rays * lambda;
790 if (dim % 2)
791 sign = -sign;
793 zz2value(den[0], tz);
794 dpoly n(dim, tz, 1);
795 for (int k = 1; k < dim; ++k) {
796 zz2value(den[k], tz);
797 dpoly fact(dim, tz, 1);
798 n *= fact;
800 if (num.E != NULL) {
801 dpoly_n d(dim);
802 d.div(n, c, sign);
803 for (unsigned long i = 0; i < sc.det; ++i) {
804 evalue *EV = evalue_polynomial(c, num.E[i]);
805 eadd(EV, vE[vert]);
806 free_evalue_refs(EV);
807 free(EV);
808 free_evalue_refs(num.E[i]);
809 delete num.E[i];
811 delete [] num.E;
812 } else {
813 mpq_set_si(count, 0, 1);
814 if (num.constant.length() == 1) {
815 zz2value(num.constant[0], tz);
816 dpoly d(dim, tz);
817 d.div(n, count, sign);
818 } else {
819 dpoly_n d(dim);
820 d.div(n, c, sign);
821 Value x, sum, acc;
822 value_init(x);
823 value_init(acc);
824 for (unsigned long i = 0; i < sc.det; ++i) {
825 value_assign(acc, c->p[dim]);
826 zz2value(num.constant[i], x);
827 for (int j = dim-1; j >= 0; --j) {
828 value_multiply(acc, acc, x);
829 value_addto(acc, acc, c->p[j]);
831 value_addto(mpq_numref(count), mpq_numref(count), acc);
833 mpz_set(mpq_denref(count), c->p[dim+1]);
834 value_clear(acc);
835 value_clear(x);
837 evalue EV;
838 value_init(EV.d);
839 evalue_set(&EV, &count[0]._mp_num, &count[0]._mp_den);
840 eadd(&EV, vE[vert]);
841 free_evalue_refs(&EV);
845 struct ienumerator_base : enumerator_base {
846 evalue ** E_vertex;
848 ienumerator_base(unsigned dim, vertex_decomposer *vpd) :
849 enumerator_base(dim,vpd) {
850 E_vertex = new evalue_p[dim];
853 virtual ~ienumerator_base() {
854 delete [] E_vertex;
857 evalue *E_num(int i, int d) {
858 return E_vertex[i + (dim-d)];
862 struct cumulator {
863 evalue *factor;
864 evalue *v;
865 dpoly_r *r;
867 cumulator(evalue *factor, evalue *v, dpoly_r *r) :
868 factor(factor), v(v), r(r) {}
870 void cumulate(barvinok_options *options);
872 virtual void add_term(const vector<int>& powers, evalue *f2) = 0;
873 virtual ~cumulator() {}
876 void cumulator::cumulate(barvinok_options *options)
878 evalue cum; // factor * 1 * E_num[0]/1 * (E_num[0]-1)/2 *...
879 evalue f;
880 evalue t; // E_num[0] - (m-1)
881 evalue *cst;
882 evalue mone;
884 if (options->lookup_table) {
885 value_init(mone.d);
886 evalue_set_si(&mone, -1, 1);
889 value_init(cum.d);
890 evalue_copy(&cum, factor);
891 value_init(f.d);
892 value_init(f.x.n);
893 value_set_si(f.d, 1);
894 value_set_si(f.x.n, 1);
895 value_init(t.d);
896 evalue_copy(&t, v);
898 if (!options->lookup_table) {
899 for (cst = &t; value_zero_p(cst->d); ) {
900 if (cst->x.p->type == fractional)
901 cst = &cst->x.p->arr[1];
902 else
903 cst = &cst->x.p->arr[0];
907 for (int m = 0; m < r->len; ++m) {
908 if (m > 0) {
909 if (m > 1) {
910 value_set_si(f.d, m);
911 emul(&f, &cum);
912 if (!options->lookup_table)
913 value_subtract(cst->x.n, cst->x.n, cst->d);
914 else
915 eadd(&mone, &t);
917 emul(&t, &cum);
919 dpoly_r_term_list& current = r->c[r->len-1-m];
920 dpoly_r_term_list::iterator j;
921 for (j = current.begin(); j != current.end(); ++j) {
922 if ((*j)->coeff == 0)
923 continue;
924 evalue *f2 = new evalue;
925 value_init(f2->d);
926 value_init(f2->x.n);
927 zz2value((*j)->coeff, f2->x.n);
928 zz2value(r->denom, f2->d);
929 emul(&cum, f2);
931 add_term((*j)->powers, f2);
934 free_evalue_refs(&f);
935 free_evalue_refs(&t);
936 free_evalue_refs(&cum);
937 if (options->lookup_table)
938 free_evalue_refs(&mone);
941 struct E_poly_term {
942 vector<int> powers;
943 evalue *E;
946 struct ie_cum : public cumulator {
947 vector<E_poly_term *> terms;
949 ie_cum(evalue *factor, evalue *v, dpoly_r *r) : cumulator(factor, v, r) {}
951 virtual void add_term(const vector<int>& powers, evalue *f2);
954 void ie_cum::add_term(const vector<int>& powers, evalue *f2)
956 int k;
957 for (k = 0; k < terms.size(); ++k) {
958 if (terms[k]->powers == powers) {
959 eadd(f2, terms[k]->E);
960 free_evalue_refs(f2);
961 delete f2;
962 break;
965 if (k >= terms.size()) {
966 E_poly_term *ET = new E_poly_term;
967 ET->powers = powers;
968 ET->E = f2;
969 terms.push_back(ET);
973 struct ienumerator : public signed_cone_consumer, public vertex_decomposer,
974 public ienumerator_base {
975 //Polyhedron *pVD;
976 mat_ZZ den;
977 mat_ZZ vertex;
978 mpq_t tcount;
979 Value tz;
981 ienumerator(Polyhedron *P, unsigned dim, unsigned nbV) :
982 vertex_decomposer(P, nbV, *this), ienumerator_base(dim, this) {
983 vertex.SetDims(1, dim);
985 den.SetDims(dim, dim);
986 mpq_init(tcount);
987 value_init(tz);
990 ~ienumerator() {
991 mpq_clear(tcount);
992 value_clear(tz);
995 virtual void handle(const signed_cone& sc, barvinok_options *options);
996 void reduce(evalue *factor, const mat_ZZ& num, const mat_ZZ& den_f,
997 barvinok_options *options);
1000 void ienumerator::reduce(evalue *factor, const mat_ZZ& num, const mat_ZZ& den_f,
1001 barvinok_options *options)
1003 unsigned len = den_f.NumRows(); // number of factors in den
1004 unsigned dim = num.NumCols();
1005 assert(num.NumRows() == 1);
1007 if (dim == 0) {
1008 eadd(factor, vE[vert]);
1009 return;
1012 vec_ZZ den_s;
1013 mat_ZZ den_r;
1014 vec_ZZ num_s;
1015 mat_ZZ num_p;
1017 split_one(num, num_s, num_p, den_f, den_s, den_r);
1019 vec_ZZ den_p;
1020 den_p.SetLength(len);
1022 ZZ one;
1023 one = 1;
1024 normalize(one, num_s, num_p, den_s, den_p, den_r);
1025 if (one != 1)
1026 emul(&mone, factor);
1028 int only_param = 0;
1029 int no_param = 0;
1030 for (int k = 0; k < len; ++k) {
1031 if (den_p[k] == 0)
1032 ++no_param;
1033 else if (den_s[k] == 0)
1034 ++only_param;
1036 if (no_param == 0) {
1037 reduce(factor, num_p, den_r, options);
1038 } else {
1039 int k, l;
1040 mat_ZZ pden;
1041 pden.SetDims(only_param, dim-1);
1043 for (k = 0, l = 0; k < len; ++k)
1044 if (den_s[k] == 0)
1045 pden[l++] = den_r[k];
1047 for (k = 0; k < len; ++k)
1048 if (den_p[k] == 0)
1049 break;
1051 zz2value(num_s[0], tz);
1052 dpoly n(no_param, tz);
1053 zz2value(den_s[k], tz);
1054 dpoly D(no_param, tz, 1);
1055 for ( ; ++k < len; )
1056 if (den_p[k] == 0) {
1057 zz2value(den_s[k], tz);
1058 dpoly fact(no_param, tz, 1);
1059 D *= fact;
1062 dpoly_r * r = 0;
1063 // if no_param + only_param == len then all powers
1064 // below will be all zero
1065 if (no_param + only_param == len) {
1066 if (E_num(0, dim) != 0)
1067 r = new dpoly_r(n, len);
1068 else {
1069 mpq_set_si(tcount, 0, 1);
1070 one = 1;
1071 n.div(D, tcount, 1);
1073 if (value_notzero_p(mpq_numref(tcount))) {
1074 evalue f;
1075 value_init(f.d);
1076 value_init(f.x.n);
1077 value_assign(f.x.n, mpq_numref(tcount));
1078 value_assign(f.d, mpq_denref(tcount));
1079 emul(&f, factor);
1080 reduce(factor, num_p, pden, options);
1081 free_evalue_refs(&f);
1083 return;
1085 } else {
1086 for (k = 0; k < len; ++k) {
1087 if (den_s[k] == 0 || den_p[k] == 0)
1088 continue;
1090 zz2value(den_s[k], tz);
1091 dpoly pd(no_param-1, tz, 1);
1093 int l;
1094 for (l = 0; l < k; ++l)
1095 if (den_r[l] == den_r[k])
1096 break;
1098 if (r == 0)
1099 r = new dpoly_r(n, pd, l, len);
1100 else {
1101 dpoly_r *nr = new dpoly_r(r, pd, l, len);
1102 delete r;
1103 r = nr;
1107 dpoly_r *rc = r->div(D);
1108 delete r;
1109 r = rc;
1110 if (E_num(0, dim) == 0) {
1111 int common = pden.NumRows();
1112 dpoly_r_term_list& final = r->c[r->len-1];
1113 int rows;
1114 evalue t;
1115 evalue f;
1116 value_init(f.d);
1117 value_init(f.x.n);
1118 zz2value(r->denom, f.d);
1119 dpoly_r_term_list::iterator j;
1120 for (j = final.begin(); j != final.end(); ++j) {
1121 if ((*j)->coeff == 0)
1122 continue;
1123 rows = common;
1124 for (int k = 0; k < r->dim; ++k) {
1125 int n = (*j)->powers[k];
1126 if (n == 0)
1127 continue;
1128 pden.SetDims(rows+n, pden.NumCols());
1129 for (int l = 0; l < n; ++l)
1130 pden[rows+l] = den_r[k];
1131 rows += n;
1133 value_init(t.d);
1134 evalue_copy(&t, factor);
1135 zz2value((*j)->coeff, f.x.n);
1136 emul(&f, &t);
1137 reduce(&t, num_p, pden, options);
1138 free_evalue_refs(&t);
1140 free_evalue_refs(&f);
1141 } else {
1142 ie_cum cum(factor, E_num(0, dim), r);
1143 cum.cumulate(options);
1145 int common = pden.NumRows();
1146 int rows;
1147 for (int j = 0; j < cum.terms.size(); ++j) {
1148 rows = common;
1149 pden.SetDims(rows, pden.NumCols());
1150 for (int k = 0; k < r->dim; ++k) {
1151 int n = cum.terms[j]->powers[k];
1152 if (n == 0)
1153 continue;
1154 pden.SetDims(rows+n, pden.NumCols());
1155 for (int l = 0; l < n; ++l)
1156 pden[rows+l] = den_r[k];
1157 rows += n;
1159 reduce(cum.terms[j]->E, num_p, pden, options);
1160 free_evalue_refs(cum.terms[j]->E);
1161 delete cum.terms[j]->E;
1162 delete cum.terms[j];
1165 delete r;
1169 static int type_offset(enode *p)
1171 return p->type == fractional ? 1 :
1172 p->type == flooring ? 1 : 0;
1175 static int edegree(evalue *e)
1177 int d = 0;
1178 enode *p;
1180 if (value_notzero_p(e->d))
1181 return 0;
1183 p = e->x.p;
1184 int i = type_offset(p);
1185 if (p->size-i-1 > d)
1186 d = p->size - i - 1;
1187 for (; i < p->size; i++) {
1188 int d2 = edegree(&p->arr[i]);
1189 if (d2 > d)
1190 d = d2;
1192 return d;
1195 void ienumerator::handle(const signed_cone& sc, barvinok_options *options)
1197 assert(sc.det == 1);
1198 assert(!sc.closed);
1199 assert(sc.rays.NumRows() == dim);
1201 lattice_point(V, sc.rays, vertex[0], E_vertex, options);
1203 den = sc.rays;
1205 evalue one;
1206 value_init(one.d);
1207 evalue_set_si(&one, sc.sign, 1);
1208 reduce(&one, vertex, den, options);
1209 free_evalue_refs(&one);
1211 for (int i = 0; i < dim; ++i)
1212 if (E_vertex[i]) {
1213 free_evalue_refs(E_vertex[i]);
1214 delete E_vertex[i];
1218 struct bfenumerator : public vertex_decomposer, public bf_base,
1219 public ienumerator_base {
1220 evalue *factor;
1222 bfenumerator(Polyhedron *P, unsigned dim, unsigned nbV) :
1223 vertex_decomposer(P, nbV, *this),
1224 bf_base(dim), ienumerator_base(dim, this) {
1225 lower = 0;
1226 factor = NULL;
1229 ~bfenumerator() {
1232 virtual void handle(const signed_cone& sc, barvinok_options *options);
1233 virtual void base(mat_ZZ& factors, bfc_vec& v);
1235 bfc_term_base* new_bf_term(int len) {
1236 bfe_term* t = new bfe_term(len);
1237 return t;
1240 virtual void set_factor(bfc_term_base *t, int k, int change) {
1241 bfe_term* bfet = static_cast<bfe_term *>(t);
1242 factor = bfet->factors[k];
1243 assert(factor != NULL);
1244 bfet->factors[k] = NULL;
1245 if (change)
1246 emul(&mone, factor);
1249 virtual void set_factor(bfc_term_base *t, int k, mpq_t &q, int change) {
1250 bfe_term* bfet = static_cast<bfe_term *>(t);
1251 factor = bfet->factors[k];
1252 assert(factor != NULL);
1253 bfet->factors[k] = NULL;
1255 evalue f;
1256 value_init(f.d);
1257 value_init(f.x.n);
1258 if (change)
1259 value_oppose(f.x.n, mpq_numref(q));
1260 else
1261 value_assign(f.x.n, mpq_numref(q));
1262 value_assign(f.d, mpq_denref(q));
1263 emul(&f, factor);
1264 free_evalue_refs(&f);
1267 virtual void set_factor(bfc_term_base *t, int k, const QQ& c, int change) {
1268 bfe_term* bfet = static_cast<bfe_term *>(t);
1270 factor = new evalue;
1272 evalue f;
1273 value_init(f.d);
1274 value_init(f.x.n);
1275 zz2value(c.n, f.x.n);
1276 if (change)
1277 value_oppose(f.x.n, f.x.n);
1278 zz2value(c.d, f.d);
1280 value_init(factor->d);
1281 evalue_copy(factor, bfet->factors[k]);
1282 emul(&f, factor);
1283 free_evalue_refs(&f);
1286 void set_factor(evalue *f, int change) {
1287 if (change)
1288 emul(&mone, f);
1289 factor = f;
1292 virtual void insert_term(bfc_term_base *t, int i) {
1293 bfe_term* bfet = static_cast<bfe_term *>(t);
1294 int len = t->terms.NumRows()-1; // already increased by one
1296 bfet->factors.resize(len+1);
1297 for (int j = len; j > i; --j) {
1298 bfet->factors[j] = bfet->factors[j-1];
1299 t->terms[j] = t->terms[j-1];
1301 bfet->factors[i] = factor;
1302 factor = NULL;
1305 virtual void update_term(bfc_term_base *t, int i) {
1306 bfe_term* bfet = static_cast<bfe_term *>(t);
1308 eadd(factor, bfet->factors[i]);
1309 free_evalue_refs(factor);
1310 delete factor;
1313 virtual bool constant_vertex(int dim) { return E_num(0, dim) == 0; }
1315 virtual void cum(bf_reducer *bfr, bfc_term_base *t, int k, dpoly_r *r,
1316 barvinok_options *options);
1319 enumerator_base *enumerator_base::create(Polyhedron *P, unsigned dim, unsigned nbV,
1320 barvinok_options *options)
1322 enumerator_base *eb;
1324 if (options->incremental_specialization == BV_SPECIALIZATION_BF)
1325 eb = new bfenumerator(P, dim, nbV);
1326 else if (options->incremental_specialization == BV_SPECIALIZATION_DF)
1327 eb = new ienumerator(P, dim, nbV);
1328 else
1329 eb = new enumerator(P, dim, nbV);
1331 return eb;
1334 struct bfe_cum : public cumulator {
1335 bfenumerator *bfe;
1336 bfc_term_base *told;
1337 int k;
1338 bf_reducer *bfr;
1340 bfe_cum(evalue *factor, evalue *v, dpoly_r *r, bf_reducer *bfr,
1341 bfc_term_base *t, int k, bfenumerator *e) :
1342 cumulator(factor, v, r), told(t), k(k),
1343 bfr(bfr), bfe(e) {
1346 virtual void add_term(const vector<int>& powers, evalue *f2);
1349 void bfe_cum::add_term(const vector<int>& powers, evalue *f2)
1351 bfr->update_powers(powers);
1353 bfc_term_base * t = bfe->find_bfc_term(bfr->vn, bfr->npowers, bfr->nnf);
1354 bfe->set_factor(f2, bfr->l_changes % 2);
1355 bfe->add_term(t, told->terms[k], bfr->l_extra_num);
1358 void bfenumerator::cum(bf_reducer *bfr, bfc_term_base *t, int k,
1359 dpoly_r *r, barvinok_options *options)
1361 bfe_term* bfet = static_cast<bfe_term *>(t);
1362 bfe_cum cum(bfet->factors[k], E_num(0, bfr->d), r, bfr, t, k, this);
1363 cum.cumulate(options);
1366 void bfenumerator::base(mat_ZZ& factors, bfc_vec& v)
1368 for (int i = 0; i < v.size(); ++i) {
1369 assert(v[i]->terms.NumRows() == 1);
1370 evalue *factor = static_cast<bfe_term *>(v[i])->factors[0];
1371 eadd(factor, vE[vert]);
1372 delete v[i];
1376 void bfenumerator::handle(const signed_cone& sc, barvinok_options *options)
1378 assert(sc.det == 1);
1379 assert(!sc.closed);
1380 assert(sc.rays.NumRows() == enumerator_base::dim);
1382 bfe_term* t = new bfe_term(enumerator_base::dim);
1383 vector< bfc_term_base * > v;
1384 v.push_back(t);
1386 t->factors.resize(1);
1388 t->terms.SetDims(1, enumerator_base::dim);
1389 lattice_point(V, sc.rays, t->terms[0], E_vertex, options);
1391 // the elements of factors are always lexpositive
1392 mat_ZZ factors;
1393 int s = setup_factors(sc.rays, factors, t, sc.sign);
1395 t->factors[0] = new evalue;
1396 value_init(t->factors[0]->d);
1397 evalue_set_si(t->factors[0], s, 1);
1398 reduce(factors, v, options);
1400 for (int i = 0; i < enumerator_base::dim; ++i)
1401 if (E_vertex[i]) {
1402 free_evalue_refs(E_vertex[i]);
1403 delete E_vertex[i];
1407 static evalue* barvinok_enumerate_ev_f(Polyhedron *P, Polyhedron* C,
1408 barvinok_options *options);
1410 /* Destroys C */
1411 static evalue* barvinok_enumerate_cst(Polyhedron *P, Polyhedron* C,
1412 struct barvinok_options *options)
1414 evalue *eres;
1416 if (emptyQ2(C)) {
1417 Polyhedron_Free(C);
1418 return evalue_zero();
1421 ALLOC(evalue, eres);
1422 value_init(eres->d);
1423 value_set_si(eres->d, 0);
1424 eres->x.p = new_enode(partition, 2, C->Dimension);
1425 EVALUE_SET_DOMAIN(eres->x.p->arr[0],
1426 DomainConstraintSimplify(C, options->MaxRays));
1427 value_set_si(eres->x.p->arr[1].d, 1);
1428 value_init(eres->x.p->arr[1].x.n);
1429 if (emptyQ2(P))
1430 value_set_si(eres->x.p->arr[1].x.n, 0);
1431 else
1432 barvinok_count_with_options(P, &eres->x.p->arr[1].x.n, options);
1434 return eres;
1437 static evalue* enumerate(Polyhedron *P, Polyhedron* C,
1438 struct barvinok_options *options)
1440 Polyhedron *next;
1441 Polyhedron *Porig = P;
1442 Polyhedron *Corig = C;
1443 Polyhedron *CEq = NULL, *rVD;
1444 int r = 0;
1445 unsigned nparam = C->Dimension;
1446 evalue *eres;
1447 Matrix *CP = NULL;
1449 evalue factor;
1450 value_init(factor.d);
1451 evalue_set_si(&factor, 1, 1);
1453 /* for now */
1454 POL_ENSURE_FACETS(P);
1455 POL_ENSURE_VERTICES(P);
1456 POL_ENSURE_FACETS(C);
1457 POL_ENSURE_VERTICES(C);
1459 if (C->Dimension == 0 || emptyQ(P) || emptyQ(C)) {
1460 constant:
1461 if (CEq == Porig)
1462 CEq = Polyhedron_Copy(CEq);
1463 eres = barvinok_enumerate_cst(P, CEq ? CEq : Polyhedron_Copy(C), options);
1464 out:
1465 if (CP) {
1466 evalue_backsubstitute(eres, CP, options->MaxRays);
1467 Matrix_Free(CP);
1470 emul(&factor, eres);
1471 if (options->approximation_method == BV_APPROX_DROP) {
1472 if (options->polynomial_approximation == BV_APPROX_SIGN_UPPER)
1473 evalue_frac2polynomial(eres, 1, options->MaxRays);
1474 if (options->polynomial_approximation == BV_APPROX_SIGN_LOWER)
1475 evalue_frac2polynomial(eres, -1, options->MaxRays);
1476 if (options->polynomial_approximation == BV_APPROX_SIGN_APPROX)
1477 evalue_frac2polynomial(eres, 0, options->MaxRays);
1479 reduce_evalue(eres);
1480 free_evalue_refs(&factor);
1481 if (P != Porig)
1482 Domain_Free(P);
1483 if (C != Corig)
1484 Polyhedron_Free(C);
1486 return eres;
1488 if (Polyhedron_is_unbounded(P, nparam, options->MaxRays))
1489 goto constant;
1491 if (P->NbEq != 0) {
1492 Matrix *f;
1493 P = remove_equalities_p(Polyhedron_Copy(P), P->Dimension-nparam, &f,
1494 options->MaxRays);
1495 mask(f, &factor, options);
1496 Matrix_Free(f);
1498 if (P->Dimension == nparam) {
1499 CEq = P;
1500 P = Universe_Polyhedron(0);
1501 goto constant;
1503 if (P->NbEq != 0 || C->NbEq != 0) {
1504 Polyhedron *Q = P;
1505 Polyhedron *D = C;
1506 remove_all_equalities(&P, &C, &CP, NULL, nparam, options->MaxRays);
1507 if (C != D && D != Corig)
1508 Polyhedron_Free(D);
1509 if (P != Q && Q != Porig)
1510 Domain_Free(Q);
1511 eres = enumerate(P, C, options);
1512 goto out;
1515 Polyhedron *T = Polyhedron_Factor(P, nparam, NULL, options->MaxRays);
1516 if (T || (P->Dimension == nparam+1)) {
1517 Polyhedron *Q;
1518 Polyhedron *C2;
1519 for (Q = T ? T : P; Q; Q = Q->next) {
1520 Polyhedron *next = Q->next;
1521 Q->next = NULL;
1523 Polyhedron *QC = Q;
1524 if (Q->Dimension != C->Dimension)
1525 QC = Polyhedron_Project(Q, nparam);
1527 C2 = C;
1528 C = DomainIntersection(C, QC, options->MaxRays);
1529 if (C2 != Corig)
1530 Polyhedron_Free(C2);
1531 if (QC != Q)
1532 Polyhedron_Free(QC);
1534 Q->next = next;
1537 if (T) {
1538 if (P != Porig)
1539 Polyhedron_Free(P);
1540 P = T;
1541 if (T->Dimension == C->Dimension) {
1542 P = T->next;
1543 T->next = NULL;
1544 Polyhedron_Free(T);
1548 next = P->next;
1549 P->next = NULL;
1550 eres = barvinok_enumerate_ev_f(P, C, options);
1551 P->next = next;
1553 if (P->next) {
1554 Polyhedron *Q;
1555 evalue *f;
1557 for (Q = P->next; Q; Q = Q->next) {
1558 Polyhedron *next = Q->next;
1559 Q->next = NULL;
1561 f = barvinok_enumerate_ev_f(Q, C, options);
1562 emul(f, eres);
1563 free_evalue_refs(f);
1564 free(f);
1566 Q->next = next;
1570 goto out;
1573 evalue* barvinok_enumerate_with_options(Polyhedron *P, Polyhedron* C,
1574 struct barvinok_options *options)
1576 Polyhedron *next, *Cnext, *C1;
1577 Polyhedron *Corig = C;
1578 evalue *eres;
1580 if (P->next)
1581 fprintf(stderr,
1582 "barvinok_enumerate: input is a union; only first polyhedron is enumerated\n");
1584 if (C->next)
1585 fprintf(stderr,
1586 "barvinok_enumerate: context is a union; only first polyhedron is considered\n");
1588 Cnext = C->next;
1589 C->next = NULL;
1590 C1 = Polyhedron_Project(P, C->Dimension);
1591 C = DomainIntersection(C, C1, options->MaxRays);
1592 Polyhedron_Free(C1);
1593 next = P->next;
1594 P->next = NULL;
1596 if (options->approximation_method == BV_APPROX_BERNOULLI)
1597 eres = Bernoulli_sum(P, C, options);
1598 else
1599 eres = enumerate(P, C, options);
1600 Domain_Free(C);
1602 P->next= next;
1603 Corig->next = Cnext;
1605 return eres;
1608 evalue* barvinok_enumerate_ev(Polyhedron *P, Polyhedron* C, unsigned MaxRays)
1610 evalue *E;
1611 barvinok_options *options = barvinok_options_new_with_defaults();
1612 options->MaxRays = MaxRays;
1613 E = barvinok_enumerate_with_options(P, C, options);
1614 barvinok_options_free(options);
1615 return E;
1618 evalue *Param_Polyhedron_Enumerate(Param_Polyhedron *PP, Polyhedron *P,
1619 Polyhedron *C,
1620 struct barvinok_options *options)
1622 evalue *eres;
1623 Param_Domain *D;
1624 unsigned nparam = C->Dimension;
1625 unsigned dim = P->Dimension - nparam;
1627 ALLOC(evalue, eres);
1628 value_init(eres->d);
1629 value_set_si(eres->d, 0);
1631 int nd;
1632 for (nd = 0, D=PP->D; D; ++nd, D=D->next);
1633 struct section { Polyhedron *D; evalue E; };
1634 section *s = new section[nd];
1636 enumerator_base *et = NULL;
1637 try_again:
1638 if (et)
1639 delete et;
1641 et = enumerator_base::create(P, dim, PP->nbV, options);
1643 Polyhedron *TC = true_context(P, C, options->MaxRays);
1644 FORALL_REDUCED_DOMAIN(PP, TC, nd, options, i, D, rVD)
1645 Param_Vertices *V;
1647 value_init(s[i].E.d);
1648 evalue_set_si(&s[i].E, 0, 1);
1649 s[i].D = rVD;
1651 FORALL_PVertex_in_ParamPolyhedron(V,D,PP) // _i is internal counter
1652 if (!et->vE[_i])
1653 try {
1654 et->decompose_at(V, _i, options);
1655 } catch (OrthogonalException &e) {
1656 FORALL_REDUCED_DOMAIN_RESET;
1657 for (; i >= 0; --i) {
1658 free_evalue_refs(&s[i].E);
1659 Domain_Free(s[i].D);
1661 goto try_again;
1663 eadd(et->vE[_i] , &s[i].E);
1664 END_FORALL_PVertex_in_ParamPolyhedron;
1665 evalue_range_reduction_in_domain(&s[i].E, rVD);
1666 END_FORALL_REDUCED_DOMAIN
1667 Polyhedron_Free(TC);
1669 delete et;
1670 if (nd == 0)
1671 evalue_set_si(eres, 0, 1);
1672 else {
1673 eres->x.p = new_enode(partition, 2*nd, C->Dimension);
1674 for (int j = 0; j < nd; ++j) {
1675 EVALUE_SET_DOMAIN(eres->x.p->arr[2*j], s[j].D);
1676 value_clear(eres->x.p->arr[2*j+1].d);
1677 eres->x.p->arr[2*j+1] = s[j].E;
1680 delete [] s;
1682 return eres;
1685 static evalue* barvinok_enumerate_ev_f(Polyhedron *P, Polyhedron* C,
1686 barvinok_options *options)
1688 unsigned nparam = C->Dimension;
1689 bool do_scale = options->approximation_method == BV_APPROX_SCALE;
1691 if (options->approximation_method == BV_APPROX_VOLUME)
1692 return Param_Polyhedron_Volume(P, C, options);
1694 if (P->Dimension - nparam == 1 && !do_scale)
1695 return ParamLine_Length(P, C, options);
1697 Param_Polyhedron *PP = NULL;
1698 evalue *eres;
1700 if (do_scale) {
1701 eres = scale_bound(P, C, options);
1702 if (eres)
1703 return eres;
1706 PP = Polyhedron2Param_Polyhedron(P, C, options);
1708 if (do_scale)
1709 eres = scale(PP, P, C, options);
1710 else
1711 eres = Param_Polyhedron_Enumerate(PP, P, C, options);
1713 if (PP)
1714 Param_Polyhedron_Free(PP);
1716 return eres;
1719 Enumeration* barvinok_enumerate(Polyhedron *P, Polyhedron* C, unsigned MaxRays)
1721 evalue *EP = barvinok_enumerate_ev(P, C, MaxRays);
1723 return partition2enumeration(EP);
1727 * remove equalities that require a "compression" of the parameters
1729 static Polyhedron *remove_more_equalities(Polyhedron *P, unsigned nparam,
1730 Matrix **CP, unsigned MaxRays)
1732 Polyhedron *Q = P;
1733 remove_all_equalities(&P, NULL, CP, NULL, nparam, MaxRays);
1734 if (P != Q)
1735 Polyhedron_Free(Q);
1736 return P;
1739 /* frees P */
1740 static gen_fun *series(Polyhedron *P, unsigned nparam, barvinok_options *options)
1742 Matrix *CP = NULL;
1743 gen_fun *gf;
1745 if (emptyQ2(P)) {
1746 Polyhedron_Free(P);
1747 return new gen_fun;
1750 assert(!Polyhedron_is_unbounded(P, nparam, options->MaxRays));
1751 assert(P->NbBid == 0);
1752 assert(Polyhedron_has_revlex_positive_rays(P, nparam));
1753 if (P->NbEq != 0)
1754 P = remove_more_equalities(P, nparam, &CP, options->MaxRays);
1755 assert(P->NbEq == 0);
1756 if (CP)
1757 nparam = CP->NbColumns-1;
1759 if (nparam == 0) {
1760 Value c;
1761 value_init(c);
1762 barvinok_count_with_options(P, &c, options);
1763 gf = new gen_fun(c);
1764 value_clear(c);
1765 } else {
1766 gf_base *red;
1767 red = gf_base::create(Polyhedron_Project(P, nparam),
1768 P->Dimension, nparam, options);
1769 POL_ENSURE_VERTICES(P);
1770 red->start_gf(P, options);
1771 gf = red->gf;
1772 delete red;
1774 if (CP) {
1775 gf->substitute(CP);
1776 Matrix_Free(CP);
1778 Polyhedron_Free(P);
1779 return gf;
1782 gen_fun * barvinok_series_with_options(Polyhedron *P, Polyhedron* C,
1783 barvinok_options *options)
1785 Polyhedron *CA;
1786 unsigned nparam = C->Dimension;
1787 gen_fun *gf;
1789 CA = align_context(C, P->Dimension, options->MaxRays);
1790 P = DomainIntersection(P, CA, options->MaxRays);
1791 Polyhedron_Free(CA);
1793 gf = series(P, nparam, options);
1795 return gf;
1798 gen_fun * barvinok_series(Polyhedron *P, Polyhedron* C, unsigned MaxRays)
1800 gen_fun *gf;
1801 barvinok_options *options = barvinok_options_new_with_defaults();
1802 options->MaxRays = MaxRays;
1803 gf = barvinok_series_with_options(P, C, options);
1804 barvinok_options_free(options);
1805 return gf;
1808 static Polyhedron *skew_into_positive_orthant(Polyhedron *D, unsigned nparam,
1809 unsigned MaxRays)
1811 Matrix *M = NULL;
1812 Value tmp;
1813 value_init(tmp);
1814 for (Polyhedron *P = D; P; P = P->next) {
1815 POL_ENSURE_VERTICES(P);
1816 assert(!Polyhedron_is_unbounded(P, nparam, MaxRays));
1817 assert(P->NbBid == 0);
1818 assert(Polyhedron_has_positive_rays(P, nparam));
1820 for (int r = 0; r < P->NbRays; ++r) {
1821 if (value_notzero_p(P->Ray[r][P->Dimension+1]))
1822 continue;
1823 for (int i = 0; i < nparam; ++i) {
1824 int j;
1825 if (value_posz_p(P->Ray[r][i+1]))
1826 continue;
1827 if (!M) {
1828 M = Matrix_Alloc(D->Dimension+1, D->Dimension+1);
1829 for (int i = 0; i < D->Dimension+1; ++i)
1830 value_set_si(M->p[i][i], 1);
1831 } else {
1832 Inner_Product(P->Ray[r]+1, M->p[i], D->Dimension+1, &tmp);
1833 if (value_posz_p(tmp))
1834 continue;
1836 for (j = P->Dimension - nparam; j < P->Dimension; ++j)
1837 if (value_pos_p(P->Ray[r][j+1]))
1838 break;
1839 assert(j < P->Dimension);
1840 value_pdivision(tmp, P->Ray[r][j+1], P->Ray[r][i+1]);
1841 value_subtract(M->p[i][j], M->p[i][j], tmp);
1845 value_clear(tmp);
1846 if (M) {
1847 D = DomainImage(D, M, MaxRays);
1848 Matrix_Free(M);
1850 return D;
1853 gen_fun* barvinok_enumerate_union_series_with_options(Polyhedron *D, Polyhedron* C,
1854 barvinok_options *options)
1856 Polyhedron *conv, *D2;
1857 Polyhedron *CA;
1858 gen_fun *gf = NULL, *gf2;
1859 unsigned nparam = C->Dimension;
1860 ZZ one, mone;
1861 one = 1;
1862 mone = -1;
1864 CA = align_context(C, D->Dimension, options->MaxRays);
1865 D = DomainIntersection(D, CA, options->MaxRays);
1866 Polyhedron_Free(CA);
1868 D2 = skew_into_positive_orthant(D, nparam, options->MaxRays);
1869 for (Polyhedron *P = D2; P; P = P->next) {
1870 assert(P->Dimension == D2->Dimension);
1871 gen_fun *P_gf;
1873 P_gf = series(Polyhedron_Copy(P), P->Dimension, options);
1874 if (!gf)
1875 gf = P_gf;
1876 else {
1877 gf->add_union(P_gf, options);
1878 delete P_gf;
1881 /* we actually only need the convex union of the parameter space
1882 * but the reducer classes currently expect a polyhedron in
1883 * the combined space
1885 Polyhedron_Free(gf->context);
1886 gf->context = DomainConvex(D2, options->MaxRays);
1888 gf2 = gf->summate(D2->Dimension - nparam, options);
1890 delete gf;
1891 if (D != D2)
1892 Domain_Free(D2);
1893 Domain_Free(D);
1894 return gf2;
1897 gen_fun* barvinok_enumerate_union_series(Polyhedron *D, Polyhedron* C,
1898 unsigned MaxRays)
1900 gen_fun *gf;
1901 barvinok_options *options = barvinok_options_new_with_defaults();
1902 options->MaxRays = MaxRays;
1903 gf = barvinok_enumerate_union_series_with_options(D, C, options);
1904 barvinok_options_free(options);
1905 return gf;
1908 evalue* barvinok_enumerate_union(Polyhedron *D, Polyhedron* C, unsigned MaxRays)
1910 evalue *EP;
1911 gen_fun *gf = barvinok_enumerate_union_series(D, C, MaxRays);
1912 EP = *gf;
1913 delete gf;
1914 return EP;