omega_interface/Makefile.am: put depending libraries first
[barvinok/uuh.git] / barvinok.cc
blobc22eb816298116317110e9b10aa32ee34586e647
1 #include <assert.h>
2 #include <iostream>
3 #include <vector>
4 #include <deque>
5 #include <string>
6 #include <sstream>
7 #include <gmp.h>
8 #include <NTL/mat_ZZ.h>
9 #include <NTL/LLL.h>
10 #include <barvinok/util.h>
11 #include <barvinok/evalue.h>
12 #include "config.h"
13 #include <barvinok/barvinok.h>
14 #include <barvinok/genfun.h>
15 #include <barvinok/options.h>
16 #include <barvinok/sample.h>
17 #include "bfcounter.h"
18 #include "conversion.h"
19 #include "counter.h"
20 #include "decomposer.h"
21 #include "euler.h"
22 #include "lattice_point.h"
23 #include "laurent.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"
30 #include "summate.h"
32 #ifdef NTL_STD_CXX
33 using namespace NTL;
34 #endif
35 using std::cerr;
36 using std::cout;
37 using std::endl;
38 using std::vector;
39 using std::deque;
40 using std::string;
41 using std::ostringstream;
43 #define ALLOC(t,p) p = (t*)malloc(sizeof(*p))
45 class dpoly_n {
46 public:
47 Matrix *coeff;
48 ~dpoly_n() {
49 Matrix_Free(coeff);
51 dpoly_n(int d) {
52 Value d0, one;
53 value_init(d0);
54 value_init(one);
55 value_set_si(one, 1);
56 coeff = Matrix_Alloc(d+1, d+1+1);
57 value_set_si(coeff->p[0][0], 1);
58 value_set_si(coeff->p[0][d+1], 1);
59 for (int i = 1; i <= d; ++i) {
60 value_multiply(coeff->p[i][0], coeff->p[i-1][0], d0);
61 Vector_Combine(coeff->p[i-1], coeff->p[i-1]+1, coeff->p[i]+1,
62 one, d0, i);
63 value_set_si(coeff->p[i][d+1], i);
64 value_multiply(coeff->p[i][d+1], coeff->p[i][d+1], coeff->p[i-1][d+1]);
65 value_decrement(d0, d0);
67 value_clear(d0);
68 value_clear(one);
70 void div(dpoly& d, Vector *count, int sign) {
71 int len = coeff->NbRows;
72 Matrix * c = Matrix_Alloc(coeff->NbRows, coeff->NbColumns);
73 Value tmp;
74 value_init(tmp);
75 for (int i = 0; i < len; ++i) {
76 Vector_Copy(coeff->p[i], c->p[i], len+1);
77 for (int j = 1; j <= i; ++j) {
78 value_multiply(tmp, d.coeff->p[j], c->p[i][len]);
79 value_oppose(tmp, tmp);
80 Vector_Combine(c->p[i], c->p[i-j], c->p[i],
81 c->p[i-j][len], tmp, len);
82 value_multiply(c->p[i][len], c->p[i][len], c->p[i-j][len]);
84 value_multiply(c->p[i][len], c->p[i][len], d.coeff->p[0]);
86 if (sign == -1) {
87 value_set_si(tmp, -1);
88 Vector_Scale(c->p[len-1], count->p, tmp, len);
89 value_assign(count->p[len], c->p[len-1][len]);
90 } else
91 Vector_Copy(c->p[len-1], count->p, len+1);
92 Vector_Normalize(count->p, len+1);
93 value_clear(tmp);
94 Matrix_Free(c);
98 static void add_rays(mat_ZZ& rays, Polyhedron *i, int *r, int nvar = -1,
99 bool all = false)
101 unsigned dim = i->Dimension;
102 if (nvar == -1)
103 nvar = dim;
104 for (int k = 0; k < i->NbRays; ++k) {
105 if (!value_zero_p(i->Ray[k][dim+1]))
106 continue;
107 if (!all && nvar != dim && First_Non_Zero(i->Ray[k]+1, nvar) == -1)
108 continue;
109 values2zz(i->Ray[k]+1, rays[(*r)++], nvar);
113 struct bfe_term : public bfc_term_base {
114 vector<evalue *> factors;
116 bfe_term(int len) : bfc_term_base(len) {
119 ~bfe_term() {
120 for (int i = 0; i < factors.size(); ++i) {
121 if (!factors[i])
122 continue;
123 free_evalue_refs(factors[i]);
124 delete factors[i];
129 static void print_int_vector(int *v, int len, const char *name)
131 cerr << name << endl;
132 for (int j = 0; j < len; ++j) {
133 cerr << v[j] << " ";
135 cerr << endl;
138 static void print_bfc_terms(mat_ZZ& factors, bfc_vec& v)
140 cerr << endl;
141 cerr << "factors" << endl;
142 cerr << factors << endl;
143 for (int i = 0; i < v.size(); ++i) {
144 cerr << "term: " << i << endl;
145 print_int_vector(v[i]->powers, factors.NumRows(), "powers");
146 cerr << "terms" << endl;
147 cerr << v[i]->terms << endl;
148 bfc_term* bfct = static_cast<bfc_term *>(v[i]);
149 cerr << bfct->c << endl;
153 static void print_bfe_terms(mat_ZZ& factors, bfc_vec& v)
155 cerr << endl;
156 cerr << "factors" << endl;
157 cerr << factors << endl;
158 for (int i = 0; i < v.size(); ++i) {
159 cerr << "term: " << i << endl;
160 print_int_vector(v[i]->powers, factors.NumRows(), "powers");
161 cerr << "terms" << endl;
162 cerr << v[i]->terms << endl;
163 bfe_term* bfet = static_cast<bfe_term *>(v[i]);
164 for (int j = 0; j < v[i]->terms.NumRows(); ++j) {
165 const char * test[] = {"a", "b"};
166 print_evalue(stderr, bfet->factors[j], test);
167 fprintf(stderr, "\n");
172 struct bfcounter : public bfcounter_base {
173 mpq_t count;
174 Value tz;
176 bfcounter(unsigned dim) : bfcounter_base(dim) {
177 mpq_init(count);
178 lower = 1;
179 value_init(tz);
181 ~bfcounter() {
182 mpq_clear(count);
183 value_clear(tz);
185 virtual void base(mat_ZZ& factors, bfc_vec& v);
186 virtual void get_count(Value *result) {
187 assert(value_one_p(&count[0]._mp_den));
188 value_assign(*result, &count[0]._mp_num);
192 void bfcounter::base(mat_ZZ& factors, bfc_vec& v)
194 unsigned nf = factors.NumRows();
196 for (int i = 0; i < v.size(); ++i) {
197 bfc_term* bfct = static_cast<bfc_term *>(v[i]);
198 int total_power = 0;
199 // factor is always positive, so we always
200 // change signs
201 for (int k = 0; k < nf; ++k)
202 total_power += v[i]->powers[k];
204 int j;
205 for (j = 0; j < nf; ++j)
206 if (v[i]->powers[j] > 0)
207 break;
209 zz2value(factors[j][0], tz);
210 dpoly D(total_power, tz, 1);
211 for (int k = 1; k < v[i]->powers[j]; ++k) {
212 zz2value(factors[j][0], tz);
213 dpoly fact(total_power, tz, 1);
214 D *= fact;
216 for ( ; ++j < nf; )
217 for (int k = 0; k < v[i]->powers[j]; ++k) {
218 zz2value(factors[j][0], tz);
219 dpoly fact(total_power, tz, 1);
220 D *= fact;
223 for (int k = 0; k < v[i]->terms.NumRows(); ++k) {
224 zz2value(v[i]->terms[k][0], tz);
225 dpoly n(total_power, tz);
226 mpq_set_si(tcount, 0, 1);
227 n.div(D, tcount, 1);
228 if (total_power % 2)
229 bfct->c[k].n = -bfct->c[k].n;
230 zz2value(bfct->c[k].n, tn);
231 zz2value(bfct->c[k].d, td);
233 mpz_mul(mpq_numref(tcount), mpq_numref(tcount), tn);
234 mpz_mul(mpq_denref(tcount), mpq_denref(tcount), td);
235 mpq_canonicalize(tcount);
236 mpq_add(count, count, tcount);
238 delete v[i];
243 /* Check whether the polyhedron is unbounded and if so,
244 * check whether it has any (and therefore an infinite number of)
245 * integer points.
246 * If one of the vertices is integer, then we are done.
247 * Otherwise, transform the polyhedron such that one of the rays
248 * is the first unit vector and cut it off at a height that ensures
249 * that if the whole polyhedron has any points, then the remaining part
250 * has integer points. In particular we add the largest coefficient
251 * of a ray to the highest vertex (rounded up).
253 static bool Polyhedron_is_infinite(Polyhedron *P, Value* result,
254 barvinok_options *options)
256 int r = 0;
257 Matrix *M, *M2;
258 Value c, tmp;
259 Value g;
260 bool first;
261 Vector *v;
262 Value offset, size;
263 Polyhedron *R;
265 if (P->NbBid == 0)
266 for (; r < P->NbRays; ++r)
267 if (value_zero_p(P->Ray[r][P->Dimension+1]))
268 break;
269 if (P->NbBid == 0 && r == P->NbRays)
270 return false;
272 if (options->count_sample_infinite) {
273 Vector *sample;
275 sample = Polyhedron_Sample(P, options);
276 if (!sample)
277 value_set_si(*result, 0);
278 else {
279 value_set_si(*result, -1);
280 Vector_Free(sample);
282 return true;
285 for (int i = 0; i < P->NbRays; ++i)
286 if (value_one_p(P->Ray[i][1+P->Dimension])) {
287 value_set_si(*result, -1);
288 return true;
291 value_init(g);
292 M = Matrix_Alloc(P->Dimension+1, P->Dimension+1);
293 Vector_Gcd(P->Ray[r]+1, P->Dimension, &g);
294 Vector_AntiScale(P->Ray[r]+1, M->p[0], g, P->Dimension+1);
295 int ok = unimodular_complete(M, 1);
296 assert(ok);
297 value_set_si(M->p[P->Dimension][P->Dimension], 1);
298 M2 = Transpose(M);
299 Matrix_Free(M);
300 P = Polyhedron_Preimage(P, M2, 0);
301 Matrix_Free(M2);
302 value_clear(g);
304 first = true;
305 value_init(offset);
306 value_init(size);
307 value_init(tmp);
308 value_set_si(size, 0);
310 for (int i = 0; i < P->NbBid; ++i) {
311 value_absolute(tmp, P->Ray[i][1]);
312 if (value_gt(tmp, size))
313 value_assign(size, tmp);
315 for (int i = P->NbBid; i < P->NbRays; ++i) {
316 if (value_zero_p(P->Ray[i][P->Dimension+1])) {
317 if (value_gt(P->Ray[i][1], size))
318 value_assign(size, P->Ray[i][1]);
319 continue;
321 mpz_cdiv_q(tmp, P->Ray[i][1], P->Ray[i][P->Dimension+1]);
322 if (first || value_gt(tmp, offset)) {
323 value_assign(offset, tmp);
324 first = false;
327 value_addto(offset, offset, size);
328 value_clear(size);
329 value_clear(tmp);
331 v = Vector_Alloc(P->Dimension+2);
332 value_set_si(v->p[0], 1);
333 value_set_si(v->p[1], -1);
334 value_assign(v->p[1+P->Dimension], offset);
335 R = AddConstraints(v->p, 1, P, options->MaxRays);
336 Polyhedron_Free(P);
337 P = R;
339 value_clear(offset);
340 Vector_Free(v);
342 value_init(c);
343 barvinok_count_with_options(P, &c, options);
344 Polyhedron_Free(P);
345 if (value_zero_p(c))
346 value_set_si(*result, 0);
347 else
348 value_set_si(*result, -1);
349 value_clear(c);
351 return true;
354 static void evalue2value(evalue *e, Value *v)
356 if (EVALUE_IS_ZERO(*e)) {
357 value_set_si(*v, 0);
358 return;
361 if (value_notzero_p(e->d)) {
362 assert(value_one_p(e->d));
363 value_assign(*v, e->x.n);
364 return;
367 assert(e->x.p->type == partition);
368 assert(e->x.p->size == 2);
369 assert(EVALUE_DOMAIN(e->x.p->arr[0])->Dimension == 0);
370 evalue2value(&e->x.p->arr[1], v);
373 static void barvinok_count_f(Polyhedron *P, Value* result,
374 barvinok_options *options);
376 void barvinok_count_with_options(Polyhedron *P, Value* result,
377 struct barvinok_options *options)
379 unsigned dim;
380 int allocated = 0;
381 Polyhedron *Q;
382 bool infinite = false;
384 if (P->next)
385 fprintf(stderr,
386 "barvinok_count: input is a union; only first polyhedron is counted\n");
388 if (emptyQ2(P)) {
389 value_set_si(*result, 0);
390 return;
392 if (P->NbEq != 0) {
393 Q = NULL;
394 do {
395 P = remove_equalities(P, options->MaxRays);
396 P = DomainConstraintSimplify(P, options->MaxRays);
397 if (Q)
398 Polyhedron_Free(Q);
399 Q = P;
400 } while (!emptyQ(P) && P->NbEq != 0);
401 if (emptyQ(P)) {
402 Polyhedron_Free(P);
403 value_set_si(*result, 0);
404 return;
406 allocated = 1;
408 if (Polyhedron_is_infinite(P, result, options)) {
409 if (allocated)
410 Polyhedron_Free(P);
411 return;
413 if (P->Dimension == 0) {
414 /* Test whether the constraints are satisfied */
415 POL_ENSURE_VERTICES(P);
416 value_set_si(*result, !emptyQ(P));
417 if (allocated)
418 Polyhedron_Free(P);
419 return;
421 if (options->summation == BV_SUM_BERNOULLI) {
422 Polyhedron *C = Universe_Polyhedron(0);
423 evalue *sum = barvinok_summate_unweighted(P, C, options);
424 Polyhedron_Free(C);
425 evalue2value(sum, result);
426 evalue_free(sum);
427 return;
429 Q = Polyhedron_Factor(P, 0, NULL, options->MaxRays);
430 if (Q) {
431 if (allocated)
432 Polyhedron_Free(P);
433 P = Q;
434 allocated = 1;
437 barvinok_count_f(P, result, options);
438 if (value_neg_p(*result))
439 infinite = true;
440 if (Q && P->next && value_notzero_p(*result)) {
441 Value factor;
442 value_init(factor);
444 for (Q = P->next; Q; Q = Q->next) {
445 barvinok_count_f(Q, &factor, options);
446 if (value_neg_p(factor)) {
447 infinite = true;
448 continue;
449 } else if (Q->next && value_zero_p(factor)) {
450 value_set_si(*result, 0);
451 break;
453 value_multiply(*result, *result, factor);
456 value_clear(factor);
459 if (allocated)
460 Domain_Free(P);
461 if (infinite)
462 value_set_si(*result, -1);
465 void barvinok_count(Polyhedron *P, Value* result, unsigned NbMaxCons)
467 barvinok_options *options = barvinok_options_new_with_defaults();
468 options->MaxRays = NbMaxCons;
469 barvinok_count_with_options(P, result, options);
470 barvinok_options_free(options);
473 static void barvinok_count_f(Polyhedron *P, Value* result,
474 barvinok_options *options)
476 if (emptyQ2(P)) {
477 value_set_si(*result, 0);
478 return;
481 if (P->Dimension == 1)
482 return Line_Length(P, result);
484 int c = P->NbConstraints;
485 POL_ENSURE_FACETS(P);
486 if (c != P->NbConstraints || P->NbEq != 0) {
487 Polyhedron *next = P->next;
488 P->next = NULL;
489 barvinok_count_with_options(P, result, options);
490 P->next = next;
491 return;
494 POL_ENSURE_VERTICES(P);
496 if (Polyhedron_is_infinite(P, result, options))
497 return;
499 np_base *cnt;
500 if (options->incremental_specialization == BV_SPECIALIZATION_BF)
501 cnt = new bfcounter(P->Dimension);
502 else if (options->incremental_specialization == BV_SPECIALIZATION_DF)
503 cnt = new icounter(P->Dimension);
504 else if (options->incremental_specialization == BV_SPECIALIZATION_TODD)
505 cnt = new tcounter(P->Dimension, options->max_index);
506 else
507 cnt = new counter(P->Dimension, options->max_index);
508 cnt->start(P, options);
510 cnt->get_count(result);
511 delete cnt;
514 static void uni_polynom(int param, Vector *c, evalue *EP)
516 unsigned dim = c->Size-2;
517 value_init(EP->d);
518 value_set_si(EP->d,0);
519 EP->x.p = new_enode(polynomial, dim+1, param+1);
520 for (int j = 0; j <= dim; ++j)
521 evalue_set(&EP->x.p->arr[j], c->p[j], c->p[dim+1]);
524 typedef evalue * evalue_p;
526 struct enumerator_base {
527 unsigned dim;
528 evalue ** vE;
529 evalue mone;
530 vertex_decomposer *vpd;
532 enumerator_base(unsigned dim, vertex_decomposer *vpd)
534 this->dim = dim;
535 this->vpd = vpd;
537 vE = new evalue_p[vpd->PP->nbV];
538 for (int j = 0; j < vpd->PP->nbV; ++j)
539 vE[j] = 0;
541 value_init(mone.d);
542 evalue_set_si(&mone, -1, 1);
545 void decompose_at(Param_Vertices *V, int _i, barvinok_options *options) {
546 //this->pVD = pVD;
548 vE[_i] = new evalue;
549 value_init(vE[_i]->d);
550 evalue_set_si(vE[_i], 0, 1);
552 vpd->decompose_at_vertex(V, _i, options);
555 virtual ~enumerator_base() {
556 for (int j = 0; j < vpd->PP->nbV; ++j)
557 if (vE[j]) {
558 free_evalue_refs(vE[j]);
559 delete vE[j];
561 delete [] vE;
563 free_evalue_refs(&mone);
566 static enumerator_base *create(Polyhedron *P, unsigned dim,
567 Param_Polyhedron *PP,
568 barvinok_options *options);
571 struct enumerator : public signed_cone_consumer, public vertex_decomposer,
572 public enumerator_base {
573 vec_ZZ lambda;
574 vec_ZZ den;
575 term_info num;
576 Vector *c;
577 mpq_t count;
578 Value tz;
580 enumerator(Polyhedron *P, unsigned dim, Param_Polyhedron *PP) :
581 vertex_decomposer(PP, *this), enumerator_base(dim, this) {
582 randomvector(P, lambda, dim);
583 den.SetLength(dim);
584 c = Vector_Alloc(dim+2);
586 mpq_init(count);
587 value_init(tz);
590 ~enumerator() {
591 mpq_clear(count);
592 Vector_Free(c);
593 value_clear(tz);
596 virtual void handle(const signed_cone& sc, barvinok_options *options);
599 void enumerator::handle(const signed_cone& sc, barvinok_options *options)
601 int sign = sc.sign;
602 int r = 0;
603 assert(sc.rays.NumRows() == dim);
604 for (int k = 0; k < dim; ++k) {
605 if (lambda * sc.rays[k] == 0)
606 throw Orthogonal;
609 lattice_point(V, sc.rays, lambda, &num, sc.det, options);
610 den = sc.rays * lambda;
612 if (dim % 2)
613 sign = -sign;
615 zz2value(den[0], tz);
616 dpoly n(dim, tz, 1);
617 for (int k = 1; k < dim; ++k) {
618 zz2value(den[k], tz);
619 dpoly fact(dim, tz, 1);
620 n *= fact;
622 if (num.E != NULL) {
623 dpoly_n d(dim);
624 d.div(n, c, sign);
625 for (unsigned long i = 0; i < sc.det; ++i) {
626 evalue *EV = evalue_polynomial(c, num.E[i]);
627 eadd(EV, vE[vert]);
628 evalue_free(EV);
629 evalue_free(num.E[i]);
631 delete [] num.E;
632 } else {
633 mpq_set_si(count, 0, 1);
634 if (num.constant.length() == 1) {
635 zz2value(num.constant[0], tz);
636 dpoly d(dim, tz);
637 d.div(n, count, sign);
638 } else {
639 dpoly_n d(dim);
640 d.div(n, c, sign);
641 Value x, sum, acc;
642 value_init(x);
643 value_init(acc);
644 for (unsigned long i = 0; i < sc.det; ++i) {
645 value_assign(acc, c->p[dim]);
646 zz2value(num.constant[i], x);
647 for (int j = dim-1; j >= 0; --j) {
648 value_multiply(acc, acc, x);
649 value_addto(acc, acc, c->p[j]);
651 value_addto(mpq_numref(count), mpq_numref(count), acc);
653 mpz_set(mpq_denref(count), c->p[dim+1]);
654 value_clear(acc);
655 value_clear(x);
657 evalue EV;
658 value_init(EV.d);
659 evalue_set(&EV, &count[0]._mp_num, &count[0]._mp_den);
660 eadd(&EV, vE[vert]);
661 free_evalue_refs(&EV);
665 struct ienumerator_base : enumerator_base {
666 evalue ** E_vertex;
668 ienumerator_base(unsigned dim, vertex_decomposer *vpd) :
669 enumerator_base(dim,vpd) {
670 E_vertex = new evalue_p[dim];
673 virtual ~ienumerator_base() {
674 delete [] E_vertex;
677 evalue *E_num(int i, int d) {
678 return E_vertex[i + (dim-d)];
682 struct cumulator {
683 evalue *factor;
684 evalue *v;
685 dpoly_r *r;
687 cumulator(evalue *factor, evalue *v, dpoly_r *r) :
688 factor(factor), v(v), r(r) {}
690 void cumulate(barvinok_options *options);
692 virtual void add_term(const vector<int>& powers, evalue *f2) = 0;
693 virtual ~cumulator() {}
696 void cumulator::cumulate(barvinok_options *options)
698 evalue cum; // factor * 1 * E_num[0]/1 * (E_num[0]-1)/2 *...
699 evalue f;
700 evalue t; // E_num[0] - (m-1)
701 evalue *cst;
702 evalue mone;
704 if (options->lookup_table) {
705 value_init(mone.d);
706 evalue_set_si(&mone, -1, 1);
709 value_init(cum.d);
710 evalue_copy(&cum, factor);
711 value_init(f.d);
712 value_init(f.x.n);
713 value_set_si(f.d, 1);
714 value_set_si(f.x.n, 1);
715 value_init(t.d);
716 evalue_copy(&t, v);
718 if (!options->lookup_table) {
719 for (cst = &t; value_zero_p(cst->d); ) {
720 if (cst->x.p->type == fractional)
721 cst = &cst->x.p->arr[1];
722 else
723 cst = &cst->x.p->arr[0];
727 for (int m = 0; m < r->len; ++m) {
728 if (m > 0) {
729 if (m > 1) {
730 value_set_si(f.d, m);
731 emul(&f, &cum);
732 if (!options->lookup_table)
733 value_subtract(cst->x.n, cst->x.n, cst->d);
734 else
735 eadd(&mone, &t);
737 emul(&t, &cum);
739 dpoly_r_term_list& current = r->c[r->len-1-m];
740 dpoly_r_term_list::iterator j;
741 for (j = current.begin(); j != current.end(); ++j) {
742 if ((*j)->coeff == 0)
743 continue;
744 evalue *f2 = new evalue;
745 value_init(f2->d);
746 value_init(f2->x.n);
747 zz2value((*j)->coeff, f2->x.n);
748 zz2value(r->denom, f2->d);
749 emul(&cum, f2);
751 add_term((*j)->powers, f2);
754 free_evalue_refs(&f);
755 free_evalue_refs(&t);
756 free_evalue_refs(&cum);
757 if (options->lookup_table)
758 free_evalue_refs(&mone);
761 struct E_poly_term {
762 vector<int> powers;
763 evalue *E;
766 struct ie_cum : public cumulator {
767 vector<E_poly_term *> terms;
769 ie_cum(evalue *factor, evalue *v, dpoly_r *r) : cumulator(factor, v, r) {}
771 virtual void add_term(const vector<int>& powers, evalue *f2);
774 void ie_cum::add_term(const vector<int>& powers, evalue *f2)
776 int k;
777 for (k = 0; k < terms.size(); ++k) {
778 if (terms[k]->powers == powers) {
779 eadd(f2, terms[k]->E);
780 free_evalue_refs(f2);
781 delete f2;
782 break;
785 if (k >= terms.size()) {
786 E_poly_term *ET = new E_poly_term;
787 ET->powers = powers;
788 ET->E = f2;
789 terms.push_back(ET);
793 struct ienumerator : public signed_cone_consumer, public vertex_decomposer,
794 public ienumerator_base {
795 //Polyhedron *pVD;
796 mat_ZZ den;
797 mat_ZZ vertex;
798 mpq_t tcount;
799 Value tz;
801 ienumerator(Polyhedron *P, unsigned dim, Param_Polyhedron *PP) :
802 vertex_decomposer(PP, *this), ienumerator_base(dim, this) {
803 vertex.SetDims(1, dim);
805 den.SetDims(dim, dim);
806 mpq_init(tcount);
807 value_init(tz);
810 ~ienumerator() {
811 mpq_clear(tcount);
812 value_clear(tz);
815 virtual void handle(const signed_cone& sc, barvinok_options *options);
816 void reduce(evalue *factor, const mat_ZZ& num, const mat_ZZ& den_f,
817 barvinok_options *options);
820 void ienumerator::reduce(evalue *factor, const mat_ZZ& num, const mat_ZZ& den_f,
821 barvinok_options *options)
823 unsigned len = den_f.NumRows(); // number of factors in den
824 unsigned dim = num.NumCols();
825 assert(num.NumRows() == 1);
827 if (dim == 0) {
828 eadd(factor, vE[vert]);
829 return;
832 vec_ZZ den_s;
833 mat_ZZ den_r;
834 vec_ZZ num_s;
835 mat_ZZ num_p;
837 split_one(num, num_s, num_p, den_f, den_s, den_r);
839 vec_ZZ den_p;
840 den_p.SetLength(len);
842 ZZ one;
843 one = 1;
844 normalize(one, num_s, num_p, den_s, den_p, den_r);
845 if (one != 1)
846 emul(&mone, factor);
848 int only_param = 0;
849 int no_param = 0;
850 for (int k = 0; k < len; ++k) {
851 if (den_p[k] == 0)
852 ++no_param;
853 else if (den_s[k] == 0)
854 ++only_param;
856 if (no_param == 0) {
857 reduce(factor, num_p, den_r, options);
858 } else {
859 int k, l;
860 mat_ZZ pden;
861 pden.SetDims(only_param, dim-1);
863 for (k = 0, l = 0; k < len; ++k)
864 if (den_s[k] == 0)
865 pden[l++] = den_r[k];
867 for (k = 0; k < len; ++k)
868 if (den_p[k] == 0)
869 break;
871 zz2value(num_s[0], tz);
872 dpoly n(no_param, tz);
873 zz2value(den_s[k], tz);
874 dpoly D(no_param, tz, 1);
875 for ( ; ++k < len; )
876 if (den_p[k] == 0) {
877 zz2value(den_s[k], tz);
878 dpoly fact(no_param, tz, 1);
879 D *= fact;
882 dpoly_r * r = 0;
883 // if no_param + only_param == len then all powers
884 // below will be all zero
885 if (no_param + only_param == len) {
886 if (E_num(0, dim) != 0)
887 r = new dpoly_r(n, len);
888 else {
889 mpq_set_si(tcount, 0, 1);
890 one = 1;
891 n.div(D, tcount, 1);
893 if (value_notzero_p(mpq_numref(tcount))) {
894 evalue f;
895 value_init(f.d);
896 value_init(f.x.n);
897 value_assign(f.x.n, mpq_numref(tcount));
898 value_assign(f.d, mpq_denref(tcount));
899 emul(&f, factor);
900 reduce(factor, num_p, pden, options);
901 free_evalue_refs(&f);
903 return;
905 } else {
906 for (k = 0; k < len; ++k) {
907 if (den_s[k] == 0 || den_p[k] == 0)
908 continue;
910 zz2value(den_s[k], tz);
911 dpoly pd(no_param-1, tz, 1);
913 int l;
914 for (l = 0; l < k; ++l)
915 if (den_r[l] == den_r[k])
916 break;
918 if (r == 0)
919 r = new dpoly_r(n, pd, l, len);
920 else {
921 dpoly_r *nr = new dpoly_r(r, pd, l, len);
922 delete r;
923 r = nr;
927 dpoly_r *rc = r->div(D);
928 delete r;
929 r = rc;
930 if (E_num(0, dim) == 0) {
931 int common = pden.NumRows();
932 dpoly_r_term_list& final = r->c[r->len-1];
933 int rows;
934 evalue t;
935 evalue f;
936 value_init(f.d);
937 value_init(f.x.n);
938 zz2value(r->denom, f.d);
939 dpoly_r_term_list::iterator j;
940 for (j = final.begin(); j != final.end(); ++j) {
941 if ((*j)->coeff == 0)
942 continue;
943 rows = common;
944 for (int k = 0; k < r->dim; ++k) {
945 int n = (*j)->powers[k];
946 if (n == 0)
947 continue;
948 pden.SetDims(rows+n, pden.NumCols());
949 for (int l = 0; l < n; ++l)
950 pden[rows+l] = den_r[k];
951 rows += n;
953 value_init(t.d);
954 evalue_copy(&t, factor);
955 zz2value((*j)->coeff, f.x.n);
956 emul(&f, &t);
957 reduce(&t, num_p, pden, options);
958 free_evalue_refs(&t);
960 free_evalue_refs(&f);
961 } else {
962 ie_cum cum(factor, E_num(0, dim), r);
963 cum.cumulate(options);
965 int common = pden.NumRows();
966 int rows;
967 for (int j = 0; j < cum.terms.size(); ++j) {
968 rows = common;
969 pden.SetDims(rows, pden.NumCols());
970 for (int k = 0; k < r->dim; ++k) {
971 int n = cum.terms[j]->powers[k];
972 if (n == 0)
973 continue;
974 pden.SetDims(rows+n, pden.NumCols());
975 for (int l = 0; l < n; ++l)
976 pden[rows+l] = den_r[k];
977 rows += n;
979 reduce(cum.terms[j]->E, num_p, pden, options);
980 free_evalue_refs(cum.terms[j]->E);
981 delete cum.terms[j]->E;
982 delete cum.terms[j];
985 delete r;
989 static int type_offset(enode *p)
991 return p->type == fractional ? 1 :
992 p->type == flooring ? 1 : 0;
995 static int edegree(evalue *e)
997 int d = 0;
998 enode *p;
1000 if (value_notzero_p(e->d))
1001 return 0;
1003 p = e->x.p;
1004 int i = type_offset(p);
1005 if (p->size-i-1 > d)
1006 d = p->size - i - 1;
1007 for (; i < p->size; i++) {
1008 int d2 = edegree(&p->arr[i]);
1009 if (d2 > d)
1010 d = d2;
1012 return d;
1015 void ienumerator::handle(const signed_cone& sc, barvinok_options *options)
1017 assert(sc.det == 1);
1018 assert(sc.rays.NumRows() == dim);
1020 lattice_point(V, sc.rays, vertex[0], E_vertex, options);
1022 den = sc.rays;
1024 evalue one;
1025 value_init(one.d);
1026 evalue_set_si(&one, sc.sign, 1);
1027 reduce(&one, vertex, den, options);
1028 free_evalue_refs(&one);
1030 for (int i = 0; i < dim; ++i)
1031 if (E_vertex[i])
1032 evalue_free(E_vertex[i]);
1035 struct bfenumerator : public vertex_decomposer, public bf_base,
1036 public ienumerator_base {
1037 evalue *factor;
1039 bfenumerator(Polyhedron *P, unsigned dim, Param_Polyhedron *PP) :
1040 vertex_decomposer(PP, *this),
1041 bf_base(dim), ienumerator_base(dim, this) {
1042 lower = 0;
1043 factor = NULL;
1046 ~bfenumerator() {
1049 virtual void handle(const signed_cone& sc, barvinok_options *options);
1050 virtual void base(mat_ZZ& factors, bfc_vec& v);
1052 bfc_term_base* new_bf_term(int len) {
1053 bfe_term* t = new bfe_term(len);
1054 return t;
1057 virtual void set_factor(bfc_term_base *t, int k, int change) {
1058 bfe_term* bfet = static_cast<bfe_term *>(t);
1059 factor = bfet->factors[k];
1060 assert(factor != NULL);
1061 bfet->factors[k] = NULL;
1062 if (change)
1063 emul(&mone, factor);
1066 virtual void set_factor(bfc_term_base *t, int k, mpq_t &q, int change) {
1067 bfe_term* bfet = static_cast<bfe_term *>(t);
1068 factor = bfet->factors[k];
1069 assert(factor != NULL);
1070 bfet->factors[k] = NULL;
1072 evalue f;
1073 value_init(f.d);
1074 value_init(f.x.n);
1075 if (change)
1076 value_oppose(f.x.n, mpq_numref(q));
1077 else
1078 value_assign(f.x.n, mpq_numref(q));
1079 value_assign(f.d, mpq_denref(q));
1080 emul(&f, factor);
1081 free_evalue_refs(&f);
1084 virtual void set_factor(bfc_term_base *t, int k, const QQ& c, int change) {
1085 bfe_term* bfet = static_cast<bfe_term *>(t);
1087 factor = new evalue;
1089 evalue f;
1090 value_init(f.d);
1091 value_init(f.x.n);
1092 zz2value(c.n, f.x.n);
1093 if (change)
1094 value_oppose(f.x.n, f.x.n);
1095 zz2value(c.d, f.d);
1097 value_init(factor->d);
1098 evalue_copy(factor, bfet->factors[k]);
1099 emul(&f, factor);
1100 free_evalue_refs(&f);
1103 void set_factor(evalue *f, int change) {
1104 if (change)
1105 emul(&mone, f);
1106 factor = f;
1109 virtual void insert_term(bfc_term_base *t, int i) {
1110 bfe_term* bfet = static_cast<bfe_term *>(t);
1111 int len = t->terms.NumRows()-1; // already increased by one
1113 bfet->factors.resize(len+1);
1114 for (int j = len; j > i; --j) {
1115 bfet->factors[j] = bfet->factors[j-1];
1116 t->terms[j] = t->terms[j-1];
1118 bfet->factors[i] = factor;
1119 factor = NULL;
1122 virtual void update_term(bfc_term_base *t, int i) {
1123 bfe_term* bfet = static_cast<bfe_term *>(t);
1125 eadd(factor, bfet->factors[i]);
1126 free_evalue_refs(factor);
1127 delete factor;
1130 virtual bool constant_vertex(int dim) { return E_num(0, dim) == 0; }
1132 virtual void cum(bf_reducer *bfr, bfc_term_base *t, int k, dpoly_r *r,
1133 barvinok_options *options);
1136 enumerator_base *enumerator_base::create(Polyhedron *P, unsigned dim,
1137 Param_Polyhedron *PP,
1138 barvinok_options *options)
1140 enumerator_base *eb;
1142 if (options->incremental_specialization == BV_SPECIALIZATION_BF)
1143 eb = new bfenumerator(P, dim, PP);
1144 else if (options->incremental_specialization == BV_SPECIALIZATION_DF)
1145 eb = new ienumerator(P, dim, PP);
1146 else
1147 eb = new enumerator(P, dim, PP);
1149 return eb;
1152 struct bfe_cum : public cumulator {
1153 bfenumerator *bfe;
1154 bfc_term_base *told;
1155 int k;
1156 bf_reducer *bfr;
1158 bfe_cum(evalue *factor, evalue *v, dpoly_r *r, bf_reducer *bfr,
1159 bfc_term_base *t, int k, bfenumerator *e) :
1160 cumulator(factor, v, r), told(t), k(k),
1161 bfr(bfr), bfe(e) {
1164 virtual void add_term(const vector<int>& powers, evalue *f2);
1167 void bfe_cum::add_term(const vector<int>& powers, evalue *f2)
1169 bfr->update_powers(powers);
1171 bfc_term_base * t = bfe->find_bfc_term(bfr->vn, bfr->npowers, bfr->nnf);
1172 bfe->set_factor(f2, bfr->l_changes % 2);
1173 bfe->add_term(t, told->terms[k], bfr->l_extra_num);
1176 void bfenumerator::cum(bf_reducer *bfr, bfc_term_base *t, int k,
1177 dpoly_r *r, barvinok_options *options)
1179 bfe_term* bfet = static_cast<bfe_term *>(t);
1180 bfe_cum cum(bfet->factors[k], E_num(0, bfr->d), r, bfr, t, k, this);
1181 cum.cumulate(options);
1184 void bfenumerator::base(mat_ZZ& factors, bfc_vec& v)
1186 for (int i = 0; i < v.size(); ++i) {
1187 assert(v[i]->terms.NumRows() == 1);
1188 evalue *factor = static_cast<bfe_term *>(v[i])->factors[0];
1189 eadd(factor, vE[vert]);
1190 delete v[i];
1194 void bfenumerator::handle(const signed_cone& sc, barvinok_options *options)
1196 assert(sc.det == 1);
1197 assert(sc.rays.NumRows() == enumerator_base::dim);
1199 bfe_term* t = new bfe_term(enumerator_base::dim);
1200 vector< bfc_term_base * > v;
1201 v.push_back(t);
1203 t->factors.resize(1);
1205 t->terms.SetDims(1, enumerator_base::dim);
1206 lattice_point(V, sc.rays, t->terms[0], E_vertex, options);
1208 // the elements of factors are always lexpositive
1209 mat_ZZ factors;
1210 int s = setup_factors(sc.rays, factors, t, sc.sign);
1212 t->factors[0] = new evalue;
1213 value_init(t->factors[0]->d);
1214 evalue_set_si(t->factors[0], s, 1);
1215 reduce(factors, v, options);
1217 for (int i = 0; i < enumerator_base::dim; ++i)
1218 if (E_vertex[i])
1219 evalue_free(E_vertex[i]);
1222 static evalue* barvinok_enumerate_ev_f(Polyhedron *P, Polyhedron* C,
1223 barvinok_options *options);
1225 /* Destroys C */
1226 static evalue* barvinok_enumerate_cst(Polyhedron *P, Polyhedron* C,
1227 struct barvinok_options *options)
1229 evalue *eres;
1231 if (emptyQ2(C)) {
1232 Polyhedron_Free(C);
1233 return evalue_zero();
1236 ALLOC(evalue, eres);
1237 value_init(eres->d);
1238 value_set_si(eres->d, 0);
1239 eres->x.p = new_enode(partition, 2, C->Dimension);
1240 EVALUE_SET_DOMAIN(eres->x.p->arr[0],
1241 DomainConstraintSimplify(C, options->MaxRays));
1242 value_set_si(eres->x.p->arr[1].d, 1);
1243 value_init(eres->x.p->arr[1].x.n);
1244 if (emptyQ2(P))
1245 value_set_si(eres->x.p->arr[1].x.n, 0);
1246 else
1247 barvinok_count_with_options(P, &eres->x.p->arr[1].x.n, options);
1249 return eres;
1252 static evalue* enumerate(Polyhedron *P, Polyhedron* C,
1253 struct barvinok_options *options)
1255 Polyhedron *next;
1256 Polyhedron *Porig = P;
1257 Polyhedron *Corig = C;
1258 Polyhedron *CEq = NULL, *rVD;
1259 int r = 0;
1260 unsigned nparam = C->Dimension;
1261 evalue *eres;
1262 Matrix *CP = NULL;
1264 evalue factor;
1265 value_init(factor.d);
1266 evalue_set_si(&factor, 1, 1);
1268 /* for now */
1269 POL_ENSURE_FACETS(P);
1270 POL_ENSURE_VERTICES(P);
1271 POL_ENSURE_FACETS(C);
1272 POL_ENSURE_VERTICES(C);
1274 if (C->Dimension == 0 || emptyQ(P) || emptyQ(C)) {
1275 constant:
1276 if (CEq == Porig)
1277 CEq = Polyhedron_Copy(CEq);
1278 eres = barvinok_enumerate_cst(P, CEq ? CEq : Polyhedron_Copy(C), options);
1279 out:
1280 if (CP) {
1281 evalue_backsubstitute(eres, CP, options->MaxRays);
1282 Matrix_Free(CP);
1285 emul(&factor, eres);
1286 if (options->approximation_method == BV_APPROX_DROP) {
1287 if (options->polynomial_approximation == BV_APPROX_SIGN_UPPER)
1288 evalue_frac2polynomial(eres, 1, options->MaxRays);
1289 if (options->polynomial_approximation == BV_APPROX_SIGN_LOWER)
1290 evalue_frac2polynomial(eres, -1, options->MaxRays);
1291 if (options->polynomial_approximation == BV_APPROX_SIGN_APPROX)
1292 evalue_frac2polynomial(eres, 0, options->MaxRays);
1294 reduce_evalue(eres);
1295 free_evalue_refs(&factor);
1296 if (P != Porig)
1297 Domain_Free(P);
1298 if (C != Corig)
1299 Polyhedron_Free(C);
1301 return eres;
1303 if (Polyhedron_is_unbounded(P, nparam, options->MaxRays))
1304 goto constant;
1306 if (P->Dimension == nparam) {
1307 CEq = P;
1308 P = Universe_Polyhedron(0);
1309 goto constant;
1311 if (P->NbEq != 0 || C->NbEq != 0) {
1312 Polyhedron *Q = P;
1313 Polyhedron *D = C;
1314 remove_all_equalities(&P, &C, &CP, NULL, nparam, options->MaxRays);
1315 if (C != D && D != Corig)
1316 Polyhedron_Free(D);
1317 if (P != Q && Q != Porig)
1318 Domain_Free(Q);
1319 eres = enumerate(P, C, options);
1320 goto out;
1323 Polyhedron *T = Polyhedron_Factor(P, nparam, NULL, options->MaxRays);
1324 if (T || (P->Dimension == nparam+1)) {
1325 Polyhedron *C2 = C;
1326 Polyhedron *FC = Factor_Context(T ? T : P, nparam, options->MaxRays);
1327 C = DomainIntersection(C, FC, options->MaxRays);
1328 if (C2 != Corig)
1329 Polyhedron_Free(C2);
1330 Polyhedron_Free(FC);
1332 if (T) {
1333 if (P != Porig)
1334 Polyhedron_Free(P);
1335 P = T;
1336 if (T->Dimension == C->Dimension) {
1337 P = T->next;
1338 T->next = NULL;
1339 Polyhedron_Free(T);
1343 next = P->next;
1344 P->next = NULL;
1345 eres = barvinok_enumerate_ev_f(P, C, options);
1346 P->next = next;
1348 if (P->next) {
1349 Polyhedron *Q;
1350 evalue *f;
1352 for (Q = P->next; Q; Q = Q->next) {
1353 Polyhedron *next = Q->next;
1354 Q->next = NULL;
1356 f = barvinok_enumerate_ev_f(Q, C, options);
1357 emul(f, eres);
1358 evalue_free(f);
1360 Q->next = next;
1364 goto out;
1367 evalue* barvinok_enumerate_with_options(Polyhedron *P, Polyhedron* C,
1368 struct barvinok_options *options)
1370 Polyhedron *next, *Cnext, *C1;
1371 Polyhedron *Corig = C;
1372 evalue *eres;
1374 if (P->next)
1375 fprintf(stderr,
1376 "barvinok_enumerate: input is a union; only first polyhedron is enumerated\n");
1378 if (C->next)
1379 fprintf(stderr,
1380 "barvinok_enumerate: context is a union; only first polyhedron is considered\n");
1382 Cnext = C->next;
1383 C->next = NULL;
1384 C1 = Polyhedron_Project(P, C->Dimension);
1385 C = DomainIntersection(C, C1, options->MaxRays);
1386 Polyhedron_Free(C1);
1387 next = P->next;
1388 P->next = NULL;
1390 if (options->approximation_method == BV_APPROX_BERNOULLI ||
1391 options->summation == BV_SUM_BERNOULLI) {
1392 int summation = options->summation;
1393 options->summation = BV_SUM_BERNOULLI;
1394 eres = barvinok_summate_unweighted(P, C, options);
1395 options->summation = summation;
1396 } else
1397 eres = enumerate(P, C, options);
1398 Domain_Free(C);
1400 P->next= next;
1401 Corig->next = Cnext;
1403 return eres;
1406 evalue* barvinok_enumerate_ev(Polyhedron *P, Polyhedron* C, unsigned MaxRays)
1408 evalue *E;
1409 barvinok_options *options = barvinok_options_new_with_defaults();
1410 options->MaxRays = MaxRays;
1411 E = barvinok_enumerate_with_options(P, C, options);
1412 barvinok_options_free(options);
1413 return E;
1416 evalue *Param_Polyhedron_Enumerate(Param_Polyhedron *PP, Polyhedron *P,
1417 Polyhedron *C,
1418 struct barvinok_options *options)
1420 evalue *eres;
1421 Param_Domain *D;
1422 unsigned nparam = C->Dimension;
1423 unsigned dim = P->Dimension - nparam;
1425 int nd;
1426 for (nd = 0, D=PP->D; D; ++nd, D=D->next);
1427 evalue_section *s = new evalue_section[nd];
1429 enumerator_base *et = NULL;
1430 try_again:
1431 if (et)
1432 delete et;
1434 et = enumerator_base::create(P, dim, PP, options);
1436 Polyhedron *TC = true_context(P, C, options->MaxRays);
1437 FORALL_REDUCED_DOMAIN(PP, TC, nd, options, i, D, rVD)
1438 Param_Vertices *V;
1440 s[i].E = evalue_zero();
1441 s[i].D = rVD;
1443 FORALL_PVertex_in_ParamPolyhedron(V,D,PP) // _i is internal counter
1444 if (!et->vE[_i])
1445 try {
1446 et->decompose_at(V, _i, options);
1447 } catch (OrthogonalException &e) {
1448 FORALL_REDUCED_DOMAIN_RESET;
1449 for (; i >= 0; --i) {
1450 evalue_free(s[i].E);
1451 Domain_Free(s[i].D);
1453 goto try_again;
1455 eadd(et->vE[_i] , s[i].E);
1456 END_FORALL_PVertex_in_ParamPolyhedron;
1457 evalue_range_reduction_in_domain(s[i].E, rVD);
1458 END_FORALL_REDUCED_DOMAIN
1459 Polyhedron_Free(TC);
1461 delete et;
1462 eres = evalue_from_section_array(s, nd);
1463 delete [] s;
1465 return eres;
1468 static evalue* barvinok_enumerate_ev_f(Polyhedron *P, Polyhedron* C,
1469 barvinok_options *options)
1471 unsigned nparam = C->Dimension;
1472 bool do_scale = options->approximation_method == BV_APPROX_SCALE;
1474 if (options->summation == BV_SUM_EULER)
1475 return barvinok_summate_unweighted(P, C, options);
1477 if (options->approximation_method == BV_APPROX_VOLUME)
1478 return Param_Polyhedron_Volume(P, C, options);
1480 if (P->Dimension - nparam == 1 && !do_scale)
1481 return ParamLine_Length(P, C, options);
1483 Param_Polyhedron *PP = NULL;
1484 evalue *eres;
1486 if (do_scale) {
1487 eres = scale_bound(P, C, options);
1488 if (eres)
1489 return eres;
1492 PP = Polyhedron2Param_Polyhedron(P, C, options);
1494 if (do_scale)
1495 eres = scale(PP, P, C, options);
1496 else
1497 eres = Param_Polyhedron_Enumerate(PP, P, C, options);
1499 if (PP)
1500 Param_Polyhedron_Free(PP);
1502 return eres;
1505 Enumeration* barvinok_enumerate(Polyhedron *P, Polyhedron* C, unsigned MaxRays)
1507 evalue *EP = barvinok_enumerate_ev(P, C, MaxRays);
1509 return partition2enumeration(EP);
1512 evalue* barvinok_enumerate_union(Polyhedron *D, Polyhedron* C, unsigned MaxRays)
1514 evalue *EP;
1515 gen_fun *gf = barvinok_enumerate_union_series(D, C, MaxRays);
1516 EP = *gf;
1517 delete gf;
1518 return EP;