volume.c: drop redundant arguments to volume_simplex
[barvinok.git] / barvinok.cc
blob02157a799baf31b33a470a492a79834448104365
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 extern "C" {
13 #include "piputil.h"
15 #include "config.h"
16 #include <barvinok/barvinok.h>
17 #include <barvinok/genfun.h>
18 #include <barvinok/options.h>
19 #include <barvinok/sample.h>
20 #include "conversion.h"
21 #include "decomposer.h"
22 #include "lattice_point.h"
23 #include "reduce_domain.h"
24 #include "genfun_constructor.h"
25 #include "remove_equalities.h"
26 #include "scale.h"
27 #include "volume.h"
29 #ifdef NTL_STD_CXX
30 using namespace NTL;
31 #endif
32 using std::cerr;
33 using std::cout;
34 using std::endl;
35 using std::vector;
36 using std::deque;
37 using std::string;
38 using std::ostringstream;
40 #define ALLOC(t,p) p = (t*)malloc(sizeof(*p))
42 class dpoly_n {
43 public:
44 Matrix *coeff;
45 ~dpoly_n() {
46 Matrix_Free(coeff);
48 dpoly_n(int d, ZZ& degree_0, ZZ& degree_1, int offset = 0) {
49 Value d0, d1;
50 value_init(d0);
51 value_init(d1);
52 zz2value(degree_0, d0);
53 zz2value(degree_1, d1);
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 d1, 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(d1);
68 void div(dpoly& d, Vector *count, ZZ& 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 zz2value(d.coeff[j], tmp);
77 value_multiply(tmp, tmp, 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 zz2value(d.coeff[0], tmp);
84 value_multiply(c->p[i][len], c->p[i][len], tmp);
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 const int MAX_TRY=10;
100 * Searches for a vector that is not orthogonal to any
101 * of the rays in rays.
103 static void nonorthog(mat_ZZ& rays, vec_ZZ& lambda)
105 int dim = rays.NumCols();
106 bool found = false;
107 lambda.SetLength(dim);
108 if (dim == 0)
109 return;
111 for (int i = 2; !found && i <= 50*dim; i+=4) {
112 for (int j = 0; j < MAX_TRY; ++j) {
113 for (int k = 0; k < dim; ++k) {
114 int r = random_int(i)+2;
115 int v = (2*(r%2)-1) * (r >> 1);
116 lambda[k] = v;
118 int k = 0;
119 for (; k < rays.NumRows(); ++k)
120 if (lambda * rays[k] == 0)
121 break;
122 if (k == rays.NumRows()) {
123 found = true;
124 break;
128 assert(found);
131 static void add_rays(mat_ZZ& rays, Polyhedron *i, int *r, int nvar = -1,
132 bool all = false)
134 unsigned dim = i->Dimension;
135 if (nvar == -1)
136 nvar = dim;
137 for (int k = 0; k < i->NbRays; ++k) {
138 if (!value_zero_p(i->Ray[k][dim+1]))
139 continue;
140 if (!all && nvar != dim && First_Non_Zero(i->Ray[k]+1, nvar) == -1)
141 continue;
142 values2zz(i->Ray[k]+1, rays[(*r)++], nvar);
146 static void mask_r(Matrix *f, int nr, Vector *lcm, int p, Vector *val, evalue *ev)
148 unsigned nparam = lcm->Size;
150 if (p == nparam) {
151 Vector * prod = Vector_Alloc(f->NbRows);
152 Matrix_Vector_Product(f, val->p, prod->p);
153 int isint = 1;
154 for (int i = 0; i < nr; ++i) {
155 value_modulus(prod->p[i], prod->p[i], f->p[i][nparam+1]);
156 isint &= value_zero_p(prod->p[i]);
158 value_set_si(ev->d, 1);
159 value_init(ev->x.n);
160 value_set_si(ev->x.n, isint);
161 Vector_Free(prod);
162 return;
165 Value tmp;
166 value_init(tmp);
167 if (value_one_p(lcm->p[p]))
168 mask_r(f, nr, lcm, p+1, val, ev);
169 else {
170 value_assign(tmp, lcm->p[p]);
171 value_set_si(ev->d, 0);
172 ev->x.p = new_enode(periodic, VALUE_TO_INT(tmp), p+1);
173 do {
174 value_decrement(tmp, tmp);
175 value_assign(val->p[p], tmp);
176 mask_r(f, nr, lcm, p+1, val, &ev->x.p->arr[VALUE_TO_INT(tmp)]);
177 } while (value_pos_p(tmp));
179 value_clear(tmp);
182 static void mask_fractional(Matrix *f, evalue *factor)
184 int nr = f->NbRows, nc = f->NbColumns;
185 int n;
186 bool found = false;
187 for (n = 0; n < nr && value_notzero_p(f->p[n][nc-1]); ++n)
188 if (value_notone_p(f->p[n][nc-1]) &&
189 value_notmone_p(f->p[n][nc-1]))
190 found = true;
191 if (!found)
192 return;
194 evalue EP;
195 nr = n;
197 Value m;
198 value_init(m);
200 evalue EV;
201 value_init(EV.d);
202 value_init(EV.x.n);
203 value_set_si(EV.x.n, 1);
205 for (n = 0; n < nr; ++n) {
206 value_assign(m, f->p[n][nc-1]);
207 if (value_one_p(m) || value_mone_p(m))
208 continue;
210 int j = normal_mod(f->p[n], nc-1, &m);
211 if (j == nc-1) {
212 free_evalue_refs(factor);
213 value_init(factor->d);
214 evalue_set_si(factor, 0, 1);
215 break;
217 vec_ZZ row;
218 values2zz(f->p[n], row, nc-1);
219 ZZ g;
220 value2zz(m, g);
221 if (j < (nc-1)-1 && row[j] > g/2) {
222 for (int k = j; k < (nc-1); ++k)
223 if (row[k] != 0)
224 row[k] = g - row[k];
227 value_init(EP.d);
228 value_set_si(EP.d, 0);
229 EP.x.p = new_enode(relation, 2, 0);
230 value_clear(EP.x.p->arr[1].d);
231 EP.x.p->arr[1] = *factor;
232 evalue *ev = &EP.x.p->arr[0];
233 value_set_si(ev->d, 0);
234 ev->x.p = new_enode(fractional, 3, -1);
235 evalue_set_si(&ev->x.p->arr[1], 0, 1);
236 evalue_set_si(&ev->x.p->arr[2], 1, 1);
237 evalue *E = multi_monom(row);
238 value_assign(EV.d, m);
239 emul(&EV, E);
240 value_clear(ev->x.p->arr[0].d);
241 ev->x.p->arr[0] = *E;
242 delete E;
243 *factor = EP;
246 value_clear(m);
247 free_evalue_refs(&EV);
253 static void mask_table(Matrix *f, evalue *factor)
255 int nr = f->NbRows, nc = f->NbColumns;
256 int n;
257 bool found = false;
258 for (n = 0; n < nr && value_notzero_p(f->p[n][nc-1]); ++n)
259 if (value_notone_p(f->p[n][nc-1]) &&
260 value_notmone_p(f->p[n][nc-1]))
261 found = true;
262 if (!found)
263 return;
265 Value tmp;
266 value_init(tmp);
267 nr = n;
268 unsigned np = nc - 2;
269 Vector *lcm = Vector_Alloc(np);
270 Vector *val = Vector_Alloc(nc);
271 Vector_Set(val->p, 0, nc);
272 value_set_si(val->p[np], 1);
273 Vector_Set(lcm->p, 1, np);
274 for (n = 0; n < nr; ++n) {
275 if (value_one_p(f->p[n][nc-1]) ||
276 value_mone_p(f->p[n][nc-1]))
277 continue;
278 for (int j = 0; j < np; ++j)
279 if (value_notzero_p(f->p[n][j])) {
280 Gcd(f->p[n][j], f->p[n][nc-1], &tmp);
281 value_division(tmp, f->p[n][nc-1], tmp);
282 value_lcm(tmp, lcm->p[j], &lcm->p[j]);
285 evalue EP;
286 value_init(EP.d);
287 mask_r(f, nr, lcm, 0, val, &EP);
288 value_clear(tmp);
289 Vector_Free(val);
290 Vector_Free(lcm);
291 emul(&EP,factor);
292 free_evalue_refs(&EP);
295 static void mask(Matrix *f, evalue *factor, barvinok_options *options)
297 if (options->lookup_table)
298 mask_table(f, factor);
299 else
300 mask_fractional(f, factor);
303 /* This structure encodes the power of the term in a rational generating function.
305 * Either E == NULL or constant = 0
306 * If E != NULL, then the power is E
307 * If E == NULL, then the power is coeff * param[pos] + constant
309 struct term_info {
310 evalue *E;
311 ZZ constant;
312 ZZ coeff;
313 int pos;
316 /* Returns the power of (t+1) in the term of a rational generating function,
317 * i.e., the scalar product of the actual lattice point and lambda.
318 * The lattice point is the unique lattice point in the fundamental parallelepiped
319 * of the unimodual cone i shifted to the parametric vertex V.
321 * PD is the parameter domain, which, if != NULL, may be used to simply the
322 * resulting expression.
324 * The result is returned in term.
326 void lattice_point(Param_Vertices* V, const mat_ZZ& rays, vec_ZZ& lambda,
327 term_info* term, Polyhedron *PD, barvinok_options *options)
329 unsigned nparam = V->Vertex->NbColumns - 2;
330 unsigned dim = rays.NumCols();
331 mat_ZZ vertex;
332 vertex.SetDims(V->Vertex->NbRows, nparam+1);
333 Value lcm, tmp;
334 value_init(lcm);
335 value_init(tmp);
336 value_set_si(lcm, 1);
337 for (int j = 0; j < V->Vertex->NbRows; ++j) {
338 value_lcm(lcm, V->Vertex->p[j][nparam+1], &lcm);
340 if (value_notone_p(lcm)) {
341 Matrix * mv = Matrix_Alloc(dim, nparam+1);
342 for (int j = 0 ; j < dim; ++j) {
343 value_division(tmp, lcm, V->Vertex->p[j][nparam+1]);
344 Vector_Scale(V->Vertex->p[j], mv->p[j], tmp, nparam+1);
347 term->E = lattice_point(rays, lambda, mv, lcm, PD, options);
348 term->constant = 0;
350 Matrix_Free(mv);
351 value_clear(lcm);
352 value_clear(tmp);
353 return;
355 for (int i = 0; i < V->Vertex->NbRows; ++i) {
356 assert(value_one_p(V->Vertex->p[i][nparam+1])); // for now
357 values2zz(V->Vertex->p[i], vertex[i], nparam+1);
360 vec_ZZ num;
361 num = lambda * vertex;
363 int p = -1;
364 int nn = 0;
365 for (int j = 0; j < nparam; ++j)
366 if (num[j] != 0) {
367 ++nn;
368 p = j;
370 if (nn >= 2) {
371 term->E = multi_monom(num);
372 term->constant = 0;
373 } else {
374 term->E = NULL;
375 term->constant = num[nparam];
376 term->pos = p;
377 if (p != -1)
378 term->coeff = num[p];
381 value_clear(lcm);
382 value_clear(tmp);
386 struct counter : public np_base {
387 vec_ZZ lambda;
388 mat_ZZ vertex;
389 vec_ZZ den;
390 ZZ sign;
391 vec_ZZ num;
392 ZZ offset;
393 int j;
394 mpq_t count;
396 counter(unsigned dim) : np_base(dim) {
397 den.SetLength(dim);
398 mpq_init(count);
401 virtual void init(Polyhedron *P) {
402 randomvector(P, lambda, dim);
405 virtual void reset() {
406 mpq_set_si(count, 0, 0);
409 ~counter() {
410 mpq_clear(count);
413 virtual void handle(const mat_ZZ& rays, Value *vertex, const QQ& c,
414 unsigned long det, int *closed, barvinok_options *options);
415 virtual void get_count(Value *result) {
416 assert(value_one_p(&count[0]._mp_den));
417 value_assign(*result, &count[0]._mp_num);
421 void counter::handle(const mat_ZZ& rays, Value *V, const QQ& c, unsigned long det,
422 int *closed, barvinok_options *options)
424 for (int k = 0; k < dim; ++k) {
425 if (lambda * rays[k] == 0)
426 throw Orthogonal;
429 assert(c.d == 1);
430 assert(c.n == 1 || c.n == -1);
431 sign = c.n;
433 lattice_point(V, rays, vertex, det, closed);
434 num = vertex * lambda;
435 den = rays * lambda;
436 offset = 0;
437 normalize(sign, offset, den);
439 num[0] += offset;
440 dpoly d(dim, num[0]);
441 for (int k = 1; k < num.length(); ++k) {
442 num[k] += offset;
443 dpoly term(dim, num[k]);
444 d += term;
446 dpoly n(dim, den[0], 1);
447 for (int k = 1; k < dim; ++k) {
448 dpoly fact(dim, den[k], 1);
449 n *= fact;
451 d.div(n, count, sign);
454 struct bfe_term : public bfc_term_base {
455 vector<evalue *> factors;
457 bfe_term(int len) : bfc_term_base(len) {
460 ~bfe_term() {
461 for (int i = 0; i < factors.size(); ++i) {
462 if (!factors[i])
463 continue;
464 free_evalue_refs(factors[i]);
465 delete factors[i];
470 static void print_int_vector(int *v, int len, char *name)
472 cerr << name << endl;
473 for (int j = 0; j < len; ++j) {
474 cerr << v[j] << " ";
476 cerr << endl;
479 static void print_bfc_terms(mat_ZZ& factors, bfc_vec& v)
481 cerr << endl;
482 cerr << "factors" << endl;
483 cerr << factors << endl;
484 for (int i = 0; i < v.size(); ++i) {
485 cerr << "term: " << i << endl;
486 print_int_vector(v[i]->powers, factors.NumRows(), "powers");
487 cerr << "terms" << endl;
488 cerr << v[i]->terms << endl;
489 bfc_term* bfct = static_cast<bfc_term *>(v[i]);
490 cerr << bfct->c << endl;
494 static void print_bfe_terms(mat_ZZ& factors, bfc_vec& v)
496 cerr << endl;
497 cerr << "factors" << endl;
498 cerr << factors << endl;
499 for (int i = 0; i < v.size(); ++i) {
500 cerr << "term: " << i << endl;
501 print_int_vector(v[i]->powers, factors.NumRows(), "powers");
502 cerr << "terms" << endl;
503 cerr << v[i]->terms << endl;
504 bfe_term* bfet = static_cast<bfe_term *>(v[i]);
505 for (int j = 0; j < v[i]->terms.NumRows(); ++j) {
506 char * test[] = {"a", "b"};
507 print_evalue(stderr, bfet->factors[j], test);
508 fprintf(stderr, "\n");
513 struct bfcounter : public bfcounter_base {
514 mpq_t count;
516 bfcounter(unsigned dim) : bfcounter_base(dim) {
517 mpq_init(count);
518 lower = 1;
520 ~bfcounter() {
521 mpq_clear(count);
523 virtual void base(mat_ZZ& factors, bfc_vec& v);
524 virtual void get_count(Value *result) {
525 assert(value_one_p(&count[0]._mp_den));
526 value_assign(*result, &count[0]._mp_num);
530 void bfcounter::base(mat_ZZ& factors, bfc_vec& v)
532 unsigned nf = factors.NumRows();
534 for (int i = 0; i < v.size(); ++i) {
535 bfc_term* bfct = static_cast<bfc_term *>(v[i]);
536 int total_power = 0;
537 // factor is always positive, so we always
538 // change signs
539 for (int k = 0; k < nf; ++k)
540 total_power += v[i]->powers[k];
542 int j;
543 for (j = 0; j < nf; ++j)
544 if (v[i]->powers[j] > 0)
545 break;
547 dpoly D(total_power, factors[j][0], 1);
548 for (int k = 1; k < v[i]->powers[j]; ++k) {
549 dpoly fact(total_power, factors[j][0], 1);
550 D *= fact;
552 for ( ; ++j < nf; )
553 for (int k = 0; k < v[i]->powers[j]; ++k) {
554 dpoly fact(total_power, factors[j][0], 1);
555 D *= fact;
558 for (int k = 0; k < v[i]->terms.NumRows(); ++k) {
559 dpoly n(total_power, v[i]->terms[k][0]);
560 mpq_set_si(tcount, 0, 1);
561 n.div(D, tcount, one);
562 if (total_power % 2)
563 bfct->c[k].n = -bfct->c[k].n;
564 zz2value(bfct->c[k].n, tn);
565 zz2value(bfct->c[k].d, td);
567 mpz_mul(mpq_numref(tcount), mpq_numref(tcount), tn);
568 mpz_mul(mpq_denref(tcount), mpq_denref(tcount), td);
569 mpq_canonicalize(tcount);
570 mpq_add(count, count, tcount);
572 delete v[i];
577 /* Check whether the polyhedron is unbounded and if so,
578 * check whether it has any (and therefore an infinite number of)
579 * integer points.
580 * If one of the vertices is integer, then we are done.
581 * Otherwise, transform the polyhedron such that one of the rays
582 * is the first unit vector and cut it off at a height that ensures
583 * that if the whole polyhedron has any points, then the remaining part
584 * has integer points. In particular we add the largest coefficient
585 * of a ray to the highest vertex (rounded up).
587 static bool Polyhedron_is_infinite(Polyhedron *P, Value* result,
588 barvinok_options *options)
590 int r = 0;
591 Matrix *M, *M2;
592 Value c, tmp;
593 Value g;
594 bool first;
595 Vector *v;
596 Value offset, size;
597 Polyhedron *R;
599 if (P->NbBid == 0)
600 for (; r < P->NbRays; ++r)
601 if (value_zero_p(P->Ray[r][P->Dimension+1]))
602 break;
603 if (P->NbBid == 0 && r == P->NbRays)
604 return false;
606 if (options->count_sample_infinite) {
607 Vector *sample;
609 sample = Polyhedron_Sample(P, options);
610 if (!sample)
611 value_set_si(*result, 0);
612 else {
613 value_set_si(*result, -1);
614 Vector_Free(sample);
616 return true;
619 for (int i = 0; i < P->NbRays; ++i)
620 if (value_one_p(P->Ray[i][1+P->Dimension])) {
621 value_set_si(*result, -1);
622 return true;
625 value_init(g);
626 v = Vector_Alloc(P->Dimension+1);
627 Vector_Gcd(P->Ray[r]+1, P->Dimension, &g);
628 Vector_AntiScale(P->Ray[r]+1, v->p, g, P->Dimension+1);
629 M = unimodular_complete(v);
630 value_set_si(M->p[P->Dimension][P->Dimension], 1);
631 M2 = Transpose(M);
632 Matrix_Free(M);
633 P = Polyhedron_Preimage(P, M2, 0);
634 Matrix_Free(M2);
635 value_clear(g);
636 Vector_Free(v);
638 first = true;
639 value_init(offset);
640 value_init(size);
641 value_init(tmp);
642 value_set_si(size, 0);
644 for (int i = 0; i < P->NbBid; ++i) {
645 value_absolute(tmp, P->Ray[i][1]);
646 if (value_gt(tmp, size))
647 value_assign(size, tmp);
649 for (int i = P->NbBid; i < P->NbRays; ++i) {
650 if (value_zero_p(P->Ray[i][P->Dimension+1])) {
651 if (value_gt(P->Ray[i][1], size))
652 value_assign(size, P->Ray[i][1]);
653 continue;
655 mpz_cdiv_q(tmp, P->Ray[i][1], P->Ray[i][P->Dimension+1]);
656 if (first || value_gt(tmp, offset)) {
657 value_assign(offset, tmp);
658 first = false;
661 value_addto(offset, offset, size);
662 value_clear(size);
663 value_clear(tmp);
665 v = Vector_Alloc(P->Dimension+2);
666 value_set_si(v->p[0], 1);
667 value_set_si(v->p[1], -1);
668 value_assign(v->p[1+P->Dimension], offset);
669 R = AddConstraints(v->p, 1, P, options->MaxRays);
670 Polyhedron_Free(P);
671 P = R;
673 value_clear(offset);
674 Vector_Free(v);
676 value_init(c);
677 barvinok_count_with_options(P, &c, options);
678 Polyhedron_Free(P);
679 if (value_zero_p(c))
680 value_set_si(*result, 0);
681 else
682 value_set_si(*result, -1);
683 value_clear(c);
685 return true;
688 typedef Polyhedron * Polyhedron_p;
690 static void barvinok_count_f(Polyhedron *P, Value* result,
691 barvinok_options *options);
693 void barvinok_count_with_options(Polyhedron *P, Value* result,
694 struct barvinok_options *options)
696 unsigned dim;
697 int allocated = 0;
698 Polyhedron *Q;
699 bool infinite = false;
701 if (P->next)
702 fprintf(stderr,
703 "barvinok_count: input is a union; only first polyhedron is counted\n");
705 if (emptyQ2(P)) {
706 value_set_si(*result, 0);
707 return;
709 if (P->NbEq != 0) {
710 Q = NULL;
711 do {
712 P = remove_equalities(P);
713 P = DomainConstraintSimplify(P, options->MaxRays);
714 if (Q)
715 Polyhedron_Free(Q);
716 Q = P;
717 } while (!emptyQ(P) && P->NbEq != 0);
718 if (emptyQ(P)) {
719 Polyhedron_Free(P);
720 value_set_si(*result, 0);
721 return;
723 allocated = 1;
725 if (Polyhedron_is_infinite(P, result, options)) {
726 if (allocated)
727 Polyhedron_Free(P);
728 return;
730 if (P->Dimension == 0) {
731 /* Test whether the constraints are satisfied */
732 POL_ENSURE_VERTICES(P);
733 value_set_si(*result, !emptyQ(P));
734 if (allocated)
735 Polyhedron_Free(P);
736 return;
738 Q = Polyhedron_Factor(P, 0, NULL, options->MaxRays);
739 if (Q) {
740 if (allocated)
741 Polyhedron_Free(P);
742 P = Q;
743 allocated = 1;
746 barvinok_count_f(P, result, options);
747 if (value_neg_p(*result))
748 infinite = true;
749 if (Q && P->next && value_notzero_p(*result)) {
750 Value factor;
751 value_init(factor);
753 for (Q = P->next; Q; Q = Q->next) {
754 barvinok_count_f(Q, &factor, options);
755 if (value_neg_p(factor)) {
756 infinite = true;
757 continue;
758 } else if (Q->next && value_zero_p(factor)) {
759 value_set_si(*result, 0);
760 break;
762 value_multiply(*result, *result, factor);
765 value_clear(factor);
768 if (allocated)
769 Domain_Free(P);
770 if (infinite)
771 value_set_si(*result, -1);
774 void barvinok_count(Polyhedron *P, Value* result, unsigned NbMaxCons)
776 barvinok_options *options = barvinok_options_new_with_defaults();
777 options->MaxRays = NbMaxCons;
778 barvinok_count_with_options(P, result, options);
779 barvinok_options_free(options);
782 static void barvinok_count_f(Polyhedron *P, Value* result,
783 barvinok_options *options)
785 if (emptyQ2(P)) {
786 value_set_si(*result, 0);
787 return;
790 if (P->Dimension == 1)
791 return Line_Length(P, result);
793 int c = P->NbConstraints;
794 POL_ENSURE_FACETS(P);
795 if (c != P->NbConstraints || P->NbEq != 0)
796 return barvinok_count_with_options(P, result, options);
798 POL_ENSURE_VERTICES(P);
800 if (Polyhedron_is_infinite(P, result, options))
801 return;
803 np_base *cnt;
804 if (options->incremental_specialization == 2)
805 cnt = new bfcounter(P->Dimension);
806 else if (options->incremental_specialization == 1)
807 cnt = new icounter(P->Dimension);
808 else
809 cnt = new counter(P->Dimension);
810 cnt->start(P, options);
812 cnt->get_count(result);
813 delete cnt;
816 static void uni_polynom(int param, Vector *c, evalue *EP)
818 unsigned dim = c->Size-2;
819 value_init(EP->d);
820 value_set_si(EP->d,0);
821 EP->x.p = new_enode(polynomial, dim+1, param+1);
822 for (int j = 0; j <= dim; ++j)
823 evalue_set(&EP->x.p->arr[j], c->p[j], c->p[dim+1]);
826 static void multi_polynom(Vector *c, evalue* X, evalue *EP)
828 unsigned dim = c->Size-2;
829 evalue EC;
831 value_init(EC.d);
832 evalue_set(&EC, c->p[dim], c->p[dim+1]);
834 value_init(EP->d);
835 evalue_set(EP, c->p[dim], c->p[dim+1]);
837 for (int i = dim-1; i >= 0; --i) {
838 emul(X, EP);
839 value_assign(EC.x.n, c->p[i]);
840 eadd(&EC, EP);
842 free_evalue_refs(&EC);
845 Polyhedron *unfringe (Polyhedron *P, unsigned MaxRays)
847 int len = P->Dimension+2;
848 Polyhedron *T, *R = P;
849 Value g;
850 value_init(g);
851 Vector *row = Vector_Alloc(len);
852 value_set_si(row->p[0], 1);
854 R = DomainConstraintSimplify(Polyhedron_Copy(P), MaxRays);
856 Matrix *M = Matrix_Alloc(2, len-1);
857 value_set_si(M->p[1][len-2], 1);
858 for (int v = 0; v < P->Dimension; ++v) {
859 value_set_si(M->p[0][v], 1);
860 Polyhedron *I = Polyhedron_Image(R, M, 2+1);
861 value_set_si(M->p[0][v], 0);
862 for (int r = 0; r < I->NbConstraints; ++r) {
863 if (value_zero_p(I->Constraint[r][0]))
864 continue;
865 if (value_zero_p(I->Constraint[r][1]))
866 continue;
867 if (value_one_p(I->Constraint[r][1]))
868 continue;
869 if (value_mone_p(I->Constraint[r][1]))
870 continue;
871 value_absolute(g, I->Constraint[r][1]);
872 Vector_Set(row->p+1, 0, len-2);
873 value_division(row->p[1+v], I->Constraint[r][1], g);
874 mpz_fdiv_q(row->p[len-1], I->Constraint[r][2], g);
875 T = R;
876 R = AddConstraints(row->p, 1, R, MaxRays);
877 if (T != P)
878 Polyhedron_Free(T);
880 Polyhedron_Free(I);
882 Matrix_Free(M);
883 Vector_Free(row);
884 value_clear(g);
885 return R;
888 /* Check whether all rays point in the positive directions
889 * for the parameters
891 static bool Polyhedron_has_positive_rays(Polyhedron *P, unsigned nparam)
893 int r;
894 for (r = 0; r < P->NbRays; ++r)
895 if (value_zero_p(P->Ray[r][P->Dimension+1])) {
896 int i;
897 for (i = P->Dimension - nparam; i < P->Dimension; ++i)
898 if (value_neg_p(P->Ray[r][i+1]))
899 return false;
901 return true;
904 typedef evalue * evalue_p;
906 struct enumerator_base {
907 unsigned dim;
908 evalue ** vE;
909 evalue mone;
910 vertex_decomposer *vpd;
912 enumerator_base(unsigned dim, vertex_decomposer *vpd)
914 this->dim = dim;
915 this->vpd = vpd;
917 vE = new evalue_p[vpd->nbV];
918 for (int j = 0; j < vpd->nbV; ++j)
919 vE[j] = 0;
921 value_init(mone.d);
922 evalue_set_si(&mone, -1, 1);
925 void decompose_at(Param_Vertices *V, int _i, barvinok_options *options) {
926 //this->pVD = pVD;
928 vE[_i] = new evalue;
929 value_init(vE[_i]->d);
930 evalue_set_si(vE[_i], 0, 1);
932 vpd->decompose_at_vertex(V, _i, options);
935 virtual ~enumerator_base() {
936 for (int j = 0; j < vpd->nbV; ++j)
937 if (vE[j]) {
938 free_evalue_refs(vE[j]);
939 delete vE[j];
941 delete [] vE;
943 free_evalue_refs(&mone);
946 static enumerator_base *create(Polyhedron *P, unsigned dim, unsigned nbV,
947 barvinok_options *options);
950 struct enumerator : public signed_cone_consumer, public vertex_decomposer,
951 public enumerator_base {
952 vec_ZZ lambda;
953 vec_ZZ den;
954 ZZ sign;
955 term_info num;
956 Vector *c;
957 mpq_t count;
959 enumerator(Polyhedron *P, unsigned dim, unsigned nbV) :
960 vertex_decomposer(P, nbV, *this), enumerator_base(dim, this) {
961 this->P = P;
962 this->nbV = nbV;
963 randomvector(P, lambda, dim);
964 den.SetLength(dim);
965 c = Vector_Alloc(dim+2);
967 mpq_init(count);
970 ~enumerator() {
971 mpq_clear(count);
972 Vector_Free(c);
975 virtual void handle(const signed_cone& sc, barvinok_options *options);
978 void enumerator::handle(const signed_cone& sc, barvinok_options *options)
980 assert(sc.det == 1);
981 assert(!sc.closed);
982 int r = 0;
983 assert(sc.rays.NumRows() == dim);
984 for (int k = 0; k < dim; ++k) {
985 if (lambda * sc.rays[k] == 0)
986 throw Orthogonal;
989 sign = sc.sign;
991 lattice_point(V, sc.rays, lambda, &num, 0, options);
992 den = sc.rays * lambda;
993 normalize(sign, num.constant, den);
995 dpoly n(dim, den[0], 1);
996 for (int k = 1; k < dim; ++k) {
997 dpoly fact(dim, den[k], 1);
998 n *= fact;
1000 if (num.E != NULL) {
1001 ZZ one(INIT_VAL, 1);
1002 dpoly_n d(dim, num.constant, one);
1003 d.div(n, c, sign);
1004 evalue EV;
1005 multi_polynom(c, num.E, &EV);
1006 eadd(&EV , vE[vert]);
1007 free_evalue_refs(&EV);
1008 free_evalue_refs(num.E);
1009 delete num.E;
1010 } else if (num.pos != -1) {
1011 dpoly_n d(dim, num.constant, num.coeff);
1012 d.div(n, c, sign);
1013 evalue EV;
1014 uni_polynom(num.pos, c, &EV);
1015 eadd(&EV , vE[vert]);
1016 free_evalue_refs(&EV);
1017 } else {
1018 mpq_set_si(count, 0, 1);
1019 dpoly d(dim, num.constant);
1020 d.div(n, count, sign);
1021 evalue EV;
1022 value_init(EV.d);
1023 evalue_set(&EV, &count[0]._mp_num, &count[0]._mp_den);
1024 eadd(&EV , vE[vert]);
1025 free_evalue_refs(&EV);
1029 struct ienumerator_base : enumerator_base {
1030 evalue ** E_vertex;
1032 ienumerator_base(unsigned dim, vertex_decomposer *vpd) :
1033 enumerator_base(dim,vpd) {
1034 E_vertex = new evalue_p[dim];
1037 virtual ~ienumerator_base() {
1038 delete [] E_vertex;
1041 evalue *E_num(int i, int d) {
1042 return E_vertex[i + (dim-d)];
1046 struct cumulator {
1047 evalue *factor;
1048 evalue *v;
1049 dpoly_r *r;
1051 cumulator(evalue *factor, evalue *v, dpoly_r *r) :
1052 factor(factor), v(v), r(r) {}
1054 void cumulate(barvinok_options *options);
1056 virtual void add_term(const vector<int>& powers, evalue *f2) = 0;
1057 virtual ~cumulator() {}
1060 void cumulator::cumulate(barvinok_options *options)
1062 evalue cum; // factor * 1 * E_num[0]/1 * (E_num[0]-1)/2 *...
1063 evalue f;
1064 evalue t; // E_num[0] - (m-1)
1065 evalue *cst;
1066 evalue mone;
1068 if (options->lookup_table) {
1069 value_init(mone.d);
1070 evalue_set_si(&mone, -1, 1);
1073 value_init(cum.d);
1074 evalue_copy(&cum, factor);
1075 value_init(f.d);
1076 value_init(f.x.n);
1077 value_set_si(f.d, 1);
1078 value_set_si(f.x.n, 1);
1079 value_init(t.d);
1080 evalue_copy(&t, v);
1082 if (!options->lookup_table) {
1083 for (cst = &t; value_zero_p(cst->d); ) {
1084 if (cst->x.p->type == fractional)
1085 cst = &cst->x.p->arr[1];
1086 else
1087 cst = &cst->x.p->arr[0];
1091 for (int m = 0; m < r->len; ++m) {
1092 if (m > 0) {
1093 if (m > 1) {
1094 value_set_si(f.d, m);
1095 emul(&f, &cum);
1096 if (!options->lookup_table)
1097 value_subtract(cst->x.n, cst->x.n, cst->d);
1098 else
1099 eadd(&mone, &t);
1101 emul(&t, &cum);
1103 dpoly_r_term_list& current = r->c[r->len-1-m];
1104 dpoly_r_term_list::iterator j;
1105 for (j = current.begin(); j != current.end(); ++j) {
1106 if ((*j)->coeff == 0)
1107 continue;
1108 evalue *f2 = new evalue;
1109 value_init(f2->d);
1110 value_init(f2->x.n);
1111 zz2value((*j)->coeff, f2->x.n);
1112 zz2value(r->denom, f2->d);
1113 emul(&cum, f2);
1115 add_term((*j)->powers, f2);
1118 free_evalue_refs(&f);
1119 free_evalue_refs(&t);
1120 free_evalue_refs(&cum);
1121 if (options->lookup_table)
1122 free_evalue_refs(&mone);
1125 struct E_poly_term {
1126 vector<int> powers;
1127 evalue *E;
1130 struct ie_cum : public cumulator {
1131 vector<E_poly_term *> terms;
1133 ie_cum(evalue *factor, evalue *v, dpoly_r *r) : cumulator(factor, v, r) {}
1135 virtual void add_term(const vector<int>& powers, evalue *f2);
1138 void ie_cum::add_term(const vector<int>& powers, evalue *f2)
1140 int k;
1141 for (k = 0; k < terms.size(); ++k) {
1142 if (terms[k]->powers == powers) {
1143 eadd(f2, terms[k]->E);
1144 free_evalue_refs(f2);
1145 delete f2;
1146 break;
1149 if (k >= terms.size()) {
1150 E_poly_term *ET = new E_poly_term;
1151 ET->powers = powers;
1152 ET->E = f2;
1153 terms.push_back(ET);
1157 struct ienumerator : public signed_cone_consumer, public vertex_decomposer,
1158 public ienumerator_base {
1159 //Polyhedron *pVD;
1160 mat_ZZ den;
1161 mat_ZZ vertex;
1162 mpq_t tcount;
1164 ienumerator(Polyhedron *P, unsigned dim, unsigned nbV) :
1165 vertex_decomposer(P, nbV, *this), ienumerator_base(dim, this) {
1166 vertex.SetDims(1, dim);
1168 den.SetDims(dim, dim);
1169 mpq_init(tcount);
1172 ~ienumerator() {
1173 mpq_clear(tcount);
1176 virtual void handle(const signed_cone& sc, barvinok_options *options);
1177 void reduce(evalue *factor, const mat_ZZ& num, const mat_ZZ& den_f,
1178 barvinok_options *options);
1181 void ienumerator::reduce(evalue *factor, const mat_ZZ& num, const mat_ZZ& den_f,
1182 barvinok_options *options)
1184 unsigned len = den_f.NumRows(); // number of factors in den
1185 unsigned dim = num.NumCols();
1186 assert(num.NumRows() == 1);
1188 if (dim == 0) {
1189 eadd(factor, vE[vert]);
1190 return;
1193 vec_ZZ den_s;
1194 mat_ZZ den_r;
1195 vec_ZZ num_s;
1196 mat_ZZ num_p;
1198 split_one(num, num_s, num_p, den_f, den_s, den_r);
1200 vec_ZZ den_p;
1201 den_p.SetLength(len);
1203 ZZ one;
1204 one = 1;
1205 normalize(one, num_s, num_p, den_s, den_p, den_r);
1206 if (one != 1)
1207 emul(&mone, factor);
1209 int only_param = 0;
1210 int no_param = 0;
1211 for (int k = 0; k < len; ++k) {
1212 if (den_p[k] == 0)
1213 ++no_param;
1214 else if (den_s[k] == 0)
1215 ++only_param;
1217 if (no_param == 0) {
1218 reduce(factor, num_p, den_r, options);
1219 } else {
1220 int k, l;
1221 mat_ZZ pden;
1222 pden.SetDims(only_param, dim-1);
1224 for (k = 0, l = 0; k < len; ++k)
1225 if (den_s[k] == 0)
1226 pden[l++] = den_r[k];
1228 for (k = 0; k < len; ++k)
1229 if (den_p[k] == 0)
1230 break;
1232 dpoly n(no_param, num_s[0]);
1233 dpoly D(no_param, den_s[k], 1);
1234 for ( ; ++k < len; )
1235 if (den_p[k] == 0) {
1236 dpoly fact(no_param, den_s[k], 1);
1237 D *= fact;
1240 dpoly_r * r = 0;
1241 // if no_param + only_param == len then all powers
1242 // below will be all zero
1243 if (no_param + only_param == len) {
1244 if (E_num(0, dim) != 0)
1245 r = new dpoly_r(n, len);
1246 else {
1247 mpq_set_si(tcount, 0, 1);
1248 one = 1;
1249 n.div(D, tcount, one);
1251 if (value_notzero_p(mpq_numref(tcount))) {
1252 evalue f;
1253 value_init(f.d);
1254 value_init(f.x.n);
1255 value_assign(f.x.n, mpq_numref(tcount));
1256 value_assign(f.d, mpq_denref(tcount));
1257 emul(&f, factor);
1258 reduce(factor, num_p, pden, options);
1259 free_evalue_refs(&f);
1261 return;
1263 } else {
1264 for (k = 0; k < len; ++k) {
1265 if (den_s[k] == 0 || den_p[k] == 0)
1266 continue;
1268 dpoly pd(no_param-1, den_s[k], 1);
1270 int l;
1271 for (l = 0; l < k; ++l)
1272 if (den_r[l] == den_r[k])
1273 break;
1275 if (r == 0)
1276 r = new dpoly_r(n, pd, l, len);
1277 else {
1278 dpoly_r *nr = new dpoly_r(r, pd, l, len);
1279 delete r;
1280 r = nr;
1284 dpoly_r *rc = r->div(D);
1285 delete r;
1286 r = rc;
1287 if (E_num(0, dim) == 0) {
1288 int common = pden.NumRows();
1289 dpoly_r_term_list& final = r->c[r->len-1];
1290 int rows;
1291 evalue t;
1292 evalue f;
1293 value_init(f.d);
1294 value_init(f.x.n);
1295 zz2value(r->denom, f.d);
1296 dpoly_r_term_list::iterator j;
1297 for (j = final.begin(); j != final.end(); ++j) {
1298 if ((*j)->coeff == 0)
1299 continue;
1300 rows = common;
1301 for (int k = 0; k < r->dim; ++k) {
1302 int n = (*j)->powers[k];
1303 if (n == 0)
1304 continue;
1305 pden.SetDims(rows+n, pden.NumCols());
1306 for (int l = 0; l < n; ++l)
1307 pden[rows+l] = den_r[k];
1308 rows += n;
1310 value_init(t.d);
1311 evalue_copy(&t, factor);
1312 zz2value((*j)->coeff, f.x.n);
1313 emul(&f, &t);
1314 reduce(&t, num_p, pden, options);
1315 free_evalue_refs(&t);
1317 free_evalue_refs(&f);
1318 } else {
1319 ie_cum cum(factor, E_num(0, dim), r);
1320 cum.cumulate(options);
1322 int common = pden.NumRows();
1323 int rows;
1324 for (int j = 0; j < cum.terms.size(); ++j) {
1325 rows = common;
1326 pden.SetDims(rows, pden.NumCols());
1327 for (int k = 0; k < r->dim; ++k) {
1328 int n = cum.terms[j]->powers[k];
1329 if (n == 0)
1330 continue;
1331 pden.SetDims(rows+n, pden.NumCols());
1332 for (int l = 0; l < n; ++l)
1333 pden[rows+l] = den_r[k];
1334 rows += n;
1336 reduce(cum.terms[j]->E, num_p, pden, options);
1337 free_evalue_refs(cum.terms[j]->E);
1338 delete cum.terms[j]->E;
1339 delete cum.terms[j];
1342 delete r;
1346 static int type_offset(enode *p)
1348 return p->type == fractional ? 1 :
1349 p->type == flooring ? 1 : 0;
1352 static int edegree(evalue *e)
1354 int d = 0;
1355 enode *p;
1357 if (value_notzero_p(e->d))
1358 return 0;
1360 p = e->x.p;
1361 int i = type_offset(p);
1362 if (p->size-i-1 > d)
1363 d = p->size - i - 1;
1364 for (; i < p->size; i++) {
1365 int d2 = edegree(&p->arr[i]);
1366 if (d2 > d)
1367 d = d2;
1369 return d;
1372 void ienumerator::handle(const signed_cone& sc, barvinok_options *options)
1374 assert(sc.det == 1);
1375 assert(!sc.closed);
1376 assert(sc.rays.NumRows() == dim);
1378 lattice_point(V, sc.rays, vertex[0], E_vertex, options);
1380 den = sc.rays;
1382 evalue one;
1383 value_init(one.d);
1384 evalue_set_si(&one, sc.sign, 1);
1385 reduce(&one, vertex, den, options);
1386 free_evalue_refs(&one);
1388 for (int i = 0; i < dim; ++i)
1389 if (E_vertex[i]) {
1390 free_evalue_refs(E_vertex[i]);
1391 delete E_vertex[i];
1395 struct bfenumerator : public vertex_decomposer, public bf_base,
1396 public ienumerator_base {
1397 evalue *factor;
1399 bfenumerator(Polyhedron *P, unsigned dim, unsigned nbV) :
1400 vertex_decomposer(P, nbV, *this),
1401 bf_base(dim), ienumerator_base(dim, this) {
1402 lower = 0;
1403 factor = NULL;
1406 ~bfenumerator() {
1409 virtual void handle(const signed_cone& sc, barvinok_options *options);
1410 virtual void base(mat_ZZ& factors, bfc_vec& v);
1412 bfc_term_base* new_bf_term(int len) {
1413 bfe_term* t = new bfe_term(len);
1414 return t;
1417 virtual void set_factor(bfc_term_base *t, int k, int change) {
1418 bfe_term* bfet = static_cast<bfe_term *>(t);
1419 factor = bfet->factors[k];
1420 assert(factor != NULL);
1421 bfet->factors[k] = NULL;
1422 if (change)
1423 emul(&mone, factor);
1426 virtual void set_factor(bfc_term_base *t, int k, mpq_t &q, int change) {
1427 bfe_term* bfet = static_cast<bfe_term *>(t);
1428 factor = bfet->factors[k];
1429 assert(factor != NULL);
1430 bfet->factors[k] = NULL;
1432 evalue f;
1433 value_init(f.d);
1434 value_init(f.x.n);
1435 if (change)
1436 value_oppose(f.x.n, mpq_numref(q));
1437 else
1438 value_assign(f.x.n, mpq_numref(q));
1439 value_assign(f.d, mpq_denref(q));
1440 emul(&f, factor);
1441 free_evalue_refs(&f);
1444 virtual void set_factor(bfc_term_base *t, int k, const QQ& c, int change) {
1445 bfe_term* bfet = static_cast<bfe_term *>(t);
1447 factor = new evalue;
1449 evalue f;
1450 value_init(f.d);
1451 value_init(f.x.n);
1452 zz2value(c.n, f.x.n);
1453 if (change)
1454 value_oppose(f.x.n, f.x.n);
1455 zz2value(c.d, f.d);
1457 value_init(factor->d);
1458 evalue_copy(factor, bfet->factors[k]);
1459 emul(&f, factor);
1460 free_evalue_refs(&f);
1463 void set_factor(evalue *f, int change) {
1464 if (change)
1465 emul(&mone, f);
1466 factor = f;
1469 virtual void insert_term(bfc_term_base *t, int i) {
1470 bfe_term* bfet = static_cast<bfe_term *>(t);
1471 int len = t->terms.NumRows()-1; // already increased by one
1473 bfet->factors.resize(len+1);
1474 for (int j = len; j > i; --j) {
1475 bfet->factors[j] = bfet->factors[j-1];
1476 t->terms[j] = t->terms[j-1];
1478 bfet->factors[i] = factor;
1479 factor = NULL;
1482 virtual void update_term(bfc_term_base *t, int i) {
1483 bfe_term* bfet = static_cast<bfe_term *>(t);
1485 eadd(factor, bfet->factors[i]);
1486 free_evalue_refs(factor);
1487 delete factor;
1490 virtual bool constant_vertex(int dim) { return E_num(0, dim) == 0; }
1492 virtual void cum(bf_reducer *bfr, bfc_term_base *t, int k, dpoly_r *r,
1493 barvinok_options *options);
1496 enumerator_base *enumerator_base::create(Polyhedron *P, unsigned dim, unsigned nbV,
1497 barvinok_options *options)
1499 enumerator_base *eb;
1501 if (options->incremental_specialization == BV_SPECIALIZATION_BF)
1502 eb = new bfenumerator(P, dim, nbV);
1503 else if (options->incremental_specialization == BV_SPECIALIZATION_DF)
1504 eb = new ienumerator(P, dim, nbV);
1505 else
1506 eb = new enumerator(P, dim, nbV);
1508 return eb;
1511 struct bfe_cum : public cumulator {
1512 bfenumerator *bfe;
1513 bfc_term_base *told;
1514 int k;
1515 bf_reducer *bfr;
1517 bfe_cum(evalue *factor, evalue *v, dpoly_r *r, bf_reducer *bfr,
1518 bfc_term_base *t, int k, bfenumerator *e) :
1519 cumulator(factor, v, r), told(t), k(k),
1520 bfr(bfr), bfe(e) {
1523 virtual void add_term(const vector<int>& powers, evalue *f2);
1526 void bfe_cum::add_term(const vector<int>& powers, evalue *f2)
1528 bfr->update_powers(powers);
1530 bfc_term_base * t = bfe->find_bfc_term(bfr->vn, bfr->npowers, bfr->nnf);
1531 bfe->set_factor(f2, bfr->l_changes % 2);
1532 bfe->add_term(t, told->terms[k], bfr->l_extra_num);
1535 void bfenumerator::cum(bf_reducer *bfr, bfc_term_base *t, int k,
1536 dpoly_r *r, barvinok_options *options)
1538 bfe_term* bfet = static_cast<bfe_term *>(t);
1539 bfe_cum cum(bfet->factors[k], E_num(0, bfr->d), r, bfr, t, k, this);
1540 cum.cumulate(options);
1543 void bfenumerator::base(mat_ZZ& factors, bfc_vec& v)
1545 for (int i = 0; i < v.size(); ++i) {
1546 assert(v[i]->terms.NumRows() == 1);
1547 evalue *factor = static_cast<bfe_term *>(v[i])->factors[0];
1548 eadd(factor, vE[vert]);
1549 delete v[i];
1553 void bfenumerator::handle(const signed_cone& sc, barvinok_options *options)
1555 assert(sc.det == 1);
1556 assert(!sc.closed);
1557 assert(sc.rays.NumRows() == enumerator_base::dim);
1559 bfe_term* t = new bfe_term(enumerator_base::dim);
1560 vector< bfc_term_base * > v;
1561 v.push_back(t);
1563 t->factors.resize(1);
1565 t->terms.SetDims(1, enumerator_base::dim);
1566 lattice_point(V, sc.rays, t->terms[0], E_vertex, options);
1568 // the elements of factors are always lexpositive
1569 mat_ZZ factors;
1570 int s = setup_factors(sc.rays, factors, t, sc.sign);
1572 t->factors[0] = new evalue;
1573 value_init(t->factors[0]->d);
1574 evalue_set_si(t->factors[0], s, 1);
1575 reduce(factors, v, options);
1577 for (int i = 0; i < enumerator_base::dim; ++i)
1578 if (E_vertex[i]) {
1579 free_evalue_refs(E_vertex[i]);
1580 delete E_vertex[i];
1584 #ifdef HAVE_CORRECT_VERTICES
1585 static inline Param_Polyhedron *Polyhedron2Param_SD(Polyhedron **Din,
1586 Polyhedron *Cin,int WS,Polyhedron **CEq,Matrix **CT)
1588 if (WS & POL_NO_DUAL)
1589 WS = 0;
1590 return Polyhedron2Param_SimplifiedDomain(Din, Cin, WS, CEq, CT);
1592 #else
1593 static Param_Polyhedron *Polyhedron2Param_SD(Polyhedron **Din,
1594 Polyhedron *Cin,int WS,Polyhedron **CEq,Matrix **CT)
1596 static char data[] = " 1 0 0 0 0 1 -18 "
1597 " 1 0 0 -20 0 19 1 "
1598 " 1 0 1 20 0 -20 16 "
1599 " 1 0 0 0 0 -1 19 "
1600 " 1 0 -1 0 0 0 4 "
1601 " 1 4 -20 0 0 -1 23 "
1602 " 1 -4 20 0 0 1 -22 "
1603 " 1 0 1 0 20 -20 16 "
1604 " 1 0 0 0 -20 19 1 ";
1605 static int checked = 0;
1606 if (!checked) {
1607 checked = 1;
1608 char *p = data;
1609 int n, v, i;
1610 Matrix *M = Matrix_Alloc(9, 7);
1611 for (i = 0; i < 9; ++i)
1612 for (int j = 0; j < 7; ++j) {
1613 sscanf(p, "%d%n", &v, &n);
1614 p += n;
1615 value_set_si(M->p[i][j], v);
1617 Polyhedron *P = Constraints2Polyhedron(M, 1024);
1618 Matrix_Free(M);
1619 Polyhedron *U = Universe_Polyhedron(1);
1620 Param_Polyhedron *PP = Polyhedron2Param_Domain(P, U, 1024);
1621 Polyhedron_Free(P);
1622 Polyhedron_Free(U);
1623 Param_Vertices *V;
1624 for (i = 0, V = PP->V; V; ++i, V = V->next)
1626 if (PP)
1627 Param_Polyhedron_Free(PP);
1628 if (i != 10) {
1629 fprintf(stderr, "WARNING: results may be incorrect\n");
1630 fprintf(stderr,
1631 "WARNING: use latest version of PolyLib to remove this warning\n");
1635 return Polyhedron2Param_SimplifiedDomain(Din, Cin, WS, CEq, CT);
1637 #endif
1639 static evalue* barvinok_enumerate_ev_f(Polyhedron *P, Polyhedron* C,
1640 barvinok_options *options);
1642 /* Destroys C */
1643 static evalue* barvinok_enumerate_cst(Polyhedron *P, Polyhedron* C,
1644 struct barvinok_options *options)
1646 evalue *eres;
1648 ALLOC(evalue, eres);
1649 value_init(eres->d);
1650 value_set_si(eres->d, 0);
1651 eres->x.p = new_enode(partition, 2, C->Dimension);
1652 EVALUE_SET_DOMAIN(eres->x.p->arr[0],
1653 DomainConstraintSimplify(C, options->MaxRays));
1654 value_set_si(eres->x.p->arr[1].d, 1);
1655 value_init(eres->x.p->arr[1].x.n);
1656 if (emptyQ(P))
1657 value_set_si(eres->x.p->arr[1].x.n, 0);
1658 else
1659 barvinok_count_with_options(P, &eres->x.p->arr[1].x.n, options);
1661 return eres;
1664 evalue* barvinok_enumerate_with_options(Polyhedron *P, Polyhedron* C,
1665 struct barvinok_options *options)
1667 //P = unfringe(P, MaxRays);
1668 Polyhedron *next, *Cnext;
1669 Polyhedron *Corig = C;
1670 Polyhedron *Porig = P;
1671 Polyhedron *CEq = NULL, *rVD, *CA;
1672 int r = 0;
1673 unsigned nparam = C->Dimension;
1674 evalue *eres;
1676 if (P->next)
1677 fprintf(stderr,
1678 "barvinok_enumerate: input is a union; only first polyhedron is enumerated\n");
1680 if (C->next)
1681 fprintf(stderr,
1682 "barvinok_enumerate: context is a union; only first polyhedron is considered\n");
1684 evalue factor;
1685 value_init(factor.d);
1686 evalue_set_si(&factor, 1, 1);
1688 Cnext = C->next;
1689 C->next = NULL;
1690 CA = align_context(C, P->Dimension, options->MaxRays);
1691 next = P->next;
1692 P->next = NULL;
1693 P = DomainIntersection(P, CA, options->MaxRays);
1694 Porig->next = next;
1695 Polyhedron_Free(CA);
1697 /* for now */
1698 POL_ENSURE_FACETS(P);
1699 POL_ENSURE_VERTICES(P);
1700 POL_ENSURE_FACETS(C);
1701 POL_ENSURE_VERTICES(C);
1703 if (C->Dimension == 0 || emptyQ(P)) {
1704 constant:
1705 eres = barvinok_enumerate_cst(P, CEq ? CEq : Polyhedron_Copy(C), options);
1706 out:
1707 emul(&factor, eres);
1708 if (options->approximation_method == BV_APPROX_DROP) {
1709 if (options->polynomial_approximation == BV_APPROX_SIGN_UPPER)
1710 evalue_frac2polynomial(eres, 1, options->MaxRays);
1711 if (options->polynomial_approximation == BV_APPROX_SIGN_LOWER)
1712 evalue_frac2polynomial(eres, -1, options->MaxRays);
1713 if (options->polynomial_approximation == BV_APPROX_SIGN_APPROX)
1714 evalue_frac2polynomial(eres, 0, options->MaxRays);
1716 reduce_evalue(eres);
1717 free_evalue_refs(&factor);
1718 Domain_Free(P);
1719 if (C != Corig)
1720 Polyhedron_Free(C);
1722 Corig->next = Cnext;
1723 return eres;
1725 if (Polyhedron_is_unbounded(P, nparam, options->MaxRays))
1726 goto constant;
1728 if (P->NbEq != 0) {
1729 Matrix *f;
1730 P = remove_equalities_p(P, P->Dimension-nparam, &f);
1731 mask(f, &factor, options);
1732 Matrix_Free(f);
1734 if (P->Dimension == nparam) {
1735 CEq = P;
1736 P = Universe_Polyhedron(0);
1737 goto constant;
1740 Polyhedron *T = Polyhedron_Factor(P, nparam, NULL, options->MaxRays);
1741 if (T || (P->Dimension == nparam+1)) {
1742 Polyhedron *Q;
1743 Polyhedron *C2;
1744 for (Q = T ? T : P; Q; Q = Q->next) {
1745 Polyhedron *next = Q->next;
1746 Q->next = NULL;
1748 Polyhedron *QC = Q;
1749 if (Q->Dimension != C->Dimension)
1750 QC = Polyhedron_Project(Q, nparam);
1752 C2 = C;
1753 C = DomainIntersection(C, QC, options->MaxRays);
1754 if (C2 != Corig)
1755 Polyhedron_Free(C2);
1756 if (QC != Q)
1757 Polyhedron_Free(QC);
1759 Q->next = next;
1762 if (T) {
1763 Polyhedron_Free(P);
1764 P = T;
1765 if (T->Dimension == C->Dimension) {
1766 P = T->next;
1767 T->next = NULL;
1768 Polyhedron_Free(T);
1772 next = P->next;
1773 P->next = NULL;
1774 eres = barvinok_enumerate_ev_f(P, C, options);
1775 P->next = next;
1777 if (P->next) {
1778 Polyhedron *Q;
1779 evalue *f;
1781 for (Q = P->next; Q; Q = Q->next) {
1782 Polyhedron *next = Q->next;
1783 Q->next = NULL;
1785 f = barvinok_enumerate_ev_f(Q, C, options);
1786 emul(f, eres);
1787 free_evalue_refs(f);
1788 free(f);
1790 Q->next = next;
1794 goto out;
1797 evalue* barvinok_enumerate_ev(Polyhedron *P, Polyhedron* C, unsigned MaxRays)
1799 evalue *E;
1800 barvinok_options *options = barvinok_options_new_with_defaults();
1801 options->MaxRays = MaxRays;
1802 E = barvinok_enumerate_with_options(P, C, options);
1803 barvinok_options_free(options);
1804 return E;
1807 static evalue* barvinok_enumerate_ev_f(Polyhedron *P, Polyhedron* C,
1808 barvinok_options *options)
1810 unsigned nparam = C->Dimension;
1811 bool do_scale = options->approximation_method == BV_APPROX_SCALE;
1813 if (options->approximation_method == BV_APPROX_VOLUME)
1814 return Param_Polyhedron_Volume(P, C, options);
1816 if (P->Dimension - nparam == 1 && !do_scale)
1817 return ParamLine_Length(P, C, options);
1819 Param_Polyhedron *PP = NULL;
1820 Polyhedron *CEq = NULL, *pVD;
1821 Matrix *CT = NULL;
1822 Param_Domain *D, *next;
1823 Param_Vertices *V;
1824 evalue *eres;
1825 Polyhedron *Porig = P;
1826 scale_data scaling;
1827 Polyhedron *T;
1829 if (do_scale) {
1830 P = scale_init(P, C, &scaling, options);
1831 if (P != Porig) {
1832 eres = barvinok_enumerate_with_options(P, C, options);
1833 Polyhedron_Free(P);
1834 scale_finish(eres, &scaling, options);
1835 return eres;
1839 T = P;
1840 PP = Polyhedron2Param_SD(&T, C, options->MaxRays, &CEq, &CT);
1841 if (T != P && P != Porig)
1842 Polyhedron_Free(P);
1843 P = T;
1845 if (isIdentity(CT)) {
1846 Matrix_Free(CT);
1847 CT = NULL;
1848 } else {
1849 assert(CT->NbRows != CT->NbColumns);
1850 if (CT->NbRows == 1) { // no more parameters
1851 eres = barvinok_enumerate_cst(P, CEq, options);
1852 out:
1853 if (CT)
1854 Matrix_Free(CT);
1855 if (PP)
1856 Param_Polyhedron_Free(PP);
1857 if (P != Porig)
1858 Polyhedron_Free(P);
1860 return eres;
1862 nparam = CT->NbRows - 1;
1865 if (do_scale)
1866 P = scale(PP, P, &scaling, P != Porig, options);
1868 unsigned dim = P->Dimension - nparam;
1870 ALLOC(evalue, eres);
1871 value_init(eres->d);
1872 value_set_si(eres->d, 0);
1874 int nd;
1875 for (nd = 0, D=PP->D; D; ++nd, D=D->next);
1876 struct section { Polyhedron *D; evalue E; };
1877 section *s = new section[nd];
1878 Polyhedron **fVD = new Polyhedron_p[nd];
1880 enumerator_base *et = NULL;
1881 try_again:
1882 if (et)
1883 delete et;
1885 et = enumerator_base::create(P, dim, PP->nbV, options);
1887 for(nd = 0, D=PP->D; D; D=next) {
1888 next = D->next;
1890 Polyhedron *rVD = reduce_domain(D->Domain, CT, CEq, fVD, nd, options);
1891 if (!rVD)
1892 continue;
1894 pVD = CT ? DomainImage(rVD,CT,options->MaxRays) : rVD;
1896 value_init(s[nd].E.d);
1897 evalue_set_si(&s[nd].E, 0, 1);
1898 s[nd].D = rVD;
1900 FORALL_PVertex_in_ParamPolyhedron(V,D,PP) // _i is internal counter
1901 if (!et->vE[_i])
1902 try {
1903 et->decompose_at(V, _i, options);
1904 } catch (OrthogonalException &e) {
1905 if (rVD != pVD)
1906 Domain_Free(pVD);
1907 for (; nd >= 0; --nd) {
1908 free_evalue_refs(&s[nd].E);
1909 Domain_Free(s[nd].D);
1910 Domain_Free(fVD[nd]);
1912 goto try_again;
1914 eadd(et->vE[_i] , &s[nd].E);
1915 END_FORALL_PVertex_in_ParamPolyhedron;
1916 evalue_range_reduction_in_domain(&s[nd].E, pVD);
1918 if (CT)
1919 addeliminatedparams_evalue(&s[nd].E, CT);
1920 ++nd;
1921 if (rVD != pVD)
1922 Domain_Free(pVD);
1925 delete et;
1926 if (nd == 0)
1927 evalue_set_si(eres, 0, 1);
1928 else {
1929 eres->x.p = new_enode(partition, 2*nd, C->Dimension);
1930 for (int j = 0; j < nd; ++j) {
1931 EVALUE_SET_DOMAIN(eres->x.p->arr[2*j], s[j].D);
1932 value_clear(eres->x.p->arr[2*j+1].d);
1933 eres->x.p->arr[2*j+1] = s[j].E;
1934 Domain_Free(fVD[j]);
1937 delete [] s;
1938 delete [] fVD;
1940 if (do_scale)
1941 scale_finish(eres, &scaling, options);
1943 if (CEq)
1944 Polyhedron_Free(CEq);
1945 goto out;
1948 Enumeration* barvinok_enumerate(Polyhedron *P, Polyhedron* C, unsigned MaxRays)
1950 evalue *EP = barvinok_enumerate_ev(P, C, MaxRays);
1952 return partition2enumeration(EP);
1955 static void SwapColumns(Value **V, int n, int i, int j)
1957 for (int r = 0; r < n; ++r)
1958 value_swap(V[r][i], V[r][j]);
1961 static void SwapColumns(Polyhedron *P, int i, int j)
1963 SwapColumns(P->Constraint, P->NbConstraints, i, j);
1964 SwapColumns(P->Ray, P->NbRays, i, j);
1967 /* Construct a constraint c from constraints l and u such that if
1968 * if constraint c holds then for each value of the other variables
1969 * there is at most one value of variable pos (position pos+1 in the constraints).
1971 * Given a lower and an upper bound
1972 * n_l v_i + <c_l,x> + c_l >= 0
1973 * -n_u v_i + <c_u,x> + c_u >= 0
1974 * the constructed constraint is
1976 * -(n_l<c_u,x> + n_u<c_l,x>) + (-n_l c_u - n_u c_l + n_l n_u - 1)
1978 * which is then simplified to remove the content of the non-constant coefficients
1980 * len is the total length of the constraints.
1981 * v is a temporary variable that can be used by this procedure
1983 static void negative_test_constraint(Value *l, Value *u, Value *c, int pos,
1984 int len, Value *v)
1986 value_oppose(*v, u[pos+1]);
1987 Vector_Combine(l+1, u+1, c+1, *v, l[pos+1], len-1);
1988 value_multiply(*v, *v, l[pos+1]);
1989 value_subtract(c[len-1], c[len-1], *v);
1990 value_set_si(*v, -1);
1991 Vector_Scale(c+1, c+1, *v, len-1);
1992 value_decrement(c[len-1], c[len-1]);
1993 ConstraintSimplify(c, c, len, v);
1996 static bool parallel_constraints(Value *l, Value *u, Value *c, int pos,
1997 int len)
1999 bool parallel;
2000 Value g1;
2001 Value g2;
2002 value_init(g1);
2003 value_init(g2);
2005 Vector_Gcd(&l[1+pos], len, &g1);
2006 Vector_Gcd(&u[1+pos], len, &g2);
2007 Vector_Combine(l+1+pos, u+1+pos, c+1, g2, g1, len);
2008 parallel = First_Non_Zero(c+1, len) == -1;
2010 value_clear(g1);
2011 value_clear(g2);
2013 return parallel;
2016 static void negative_test_constraint7(Value *l, Value *u, Value *c, int pos,
2017 int exist, int len, Value *v)
2019 Value g;
2020 value_init(g);
2022 Vector_Gcd(&u[1+pos], exist, v);
2023 Vector_Gcd(&l[1+pos], exist, &g);
2024 Vector_Combine(l+1, u+1, c+1, *v, g, len-1);
2025 value_multiply(*v, *v, g);
2026 value_subtract(c[len-1], c[len-1], *v);
2027 value_set_si(*v, -1);
2028 Vector_Scale(c+1, c+1, *v, len-1);
2029 value_decrement(c[len-1], c[len-1]);
2030 ConstraintSimplify(c, c, len, v);
2032 value_clear(g);
2035 /* Turns a x + b >= 0 into a x + b <= -1
2037 * len is the total length of the constraint.
2038 * v is a temporary variable that can be used by this procedure
2040 static void oppose_constraint(Value *c, int len, Value *v)
2042 value_set_si(*v, -1);
2043 Vector_Scale(c+1, c+1, *v, len-1);
2044 value_decrement(c[len-1], c[len-1]);
2047 /* Split polyhedron P into two polyhedra *pos and *neg, where
2048 * existential variable i has at most one solution for each
2049 * value of the other variables in *neg.
2051 * The splitting is performed using constraints l and u.
2053 * nvar: number of set variables
2054 * row: temporary vector that can be used by this procedure
2055 * f: temporary value that can be used by this procedure
2057 static bool SplitOnConstraint(Polyhedron *P, int i, int l, int u,
2058 int nvar, int MaxRays, Vector *row, Value& f,
2059 Polyhedron **pos, Polyhedron **neg)
2061 negative_test_constraint(P->Constraint[l], P->Constraint[u],
2062 row->p, nvar+i, P->Dimension+2, &f);
2063 *neg = AddConstraints(row->p, 1, P, MaxRays);
2065 /* We found an independent, but useless constraint
2066 * Maybe we should detect this earlier and not
2067 * mark the variable as INDEPENDENT
2069 if (emptyQ((*neg))) {
2070 Polyhedron_Free(*neg);
2071 return false;
2074 oppose_constraint(row->p, P->Dimension+2, &f);
2075 *pos = AddConstraints(row->p, 1, P, MaxRays);
2077 if (emptyQ((*pos))) {
2078 Polyhedron_Free(*neg);
2079 Polyhedron_Free(*pos);
2080 return false;
2083 return true;
2087 * unimodularly transform P such that constraint r is transformed
2088 * into a constraint that involves only a single (the first)
2089 * existential variable
2092 static Polyhedron *rotate_along(Polyhedron *P, int r, int nvar, int exist,
2093 unsigned MaxRays)
2095 Value g;
2096 value_init(g);
2098 Vector *row = Vector_Alloc(exist);
2099 Vector_Copy(P->Constraint[r]+1+nvar, row->p, exist);
2100 Vector_Gcd(row->p, exist, &g);
2101 if (value_notone_p(g))
2102 Vector_AntiScale(row->p, row->p, g, exist);
2103 value_clear(g);
2105 Matrix *M = unimodular_complete(row);
2106 Matrix *M2 = Matrix_Alloc(P->Dimension+1, P->Dimension+1);
2107 for (r = 0; r < nvar; ++r)
2108 value_set_si(M2->p[r][r], 1);
2109 for ( ; r < nvar+exist; ++r)
2110 Vector_Copy(M->p[r-nvar], M2->p[r]+nvar, exist);
2111 for ( ; r < P->Dimension+1; ++r)
2112 value_set_si(M2->p[r][r], 1);
2113 Polyhedron *T = Polyhedron_Image(P, M2, MaxRays);
2115 Matrix_Free(M2);
2116 Matrix_Free(M);
2117 Vector_Free(row);
2119 return T;
2122 /* Split polyhedron P into two polyhedra *pos and *neg, where
2123 * existential variable i has at most one solution for each
2124 * value of the other variables in *neg.
2126 * If independent is set, then the two constraints on which the
2127 * split will be performed need to be independent of the other
2128 * existential variables.
2130 * Return true if an appropriate split could be performed.
2132 * nvar: number of set variables
2133 * exist: number of existential variables
2134 * row: temporary vector that can be used by this procedure
2135 * f: temporary value that can be used by this procedure
2137 static bool SplitOnVar(Polyhedron *P, int i,
2138 int nvar, int exist, int MaxRays,
2139 Vector *row, Value& f, bool independent,
2140 Polyhedron **pos, Polyhedron **neg)
2142 int j;
2144 for (int l = P->NbEq; l < P->NbConstraints; ++l) {
2145 if (value_negz_p(P->Constraint[l][nvar+i+1]))
2146 continue;
2148 if (independent) {
2149 for (j = 0; j < exist; ++j)
2150 if (j != i && value_notzero_p(P->Constraint[l][nvar+j+1]))
2151 break;
2152 if (j < exist)
2153 continue;
2156 for (int u = P->NbEq; u < P->NbConstraints; ++u) {
2157 if (value_posz_p(P->Constraint[u][nvar+i+1]))
2158 continue;
2160 if (independent) {
2161 for (j = 0; j < exist; ++j)
2162 if (j != i && value_notzero_p(P->Constraint[u][nvar+j+1]))
2163 break;
2164 if (j < exist)
2165 continue;
2168 if (SplitOnConstraint(P, i, l, u, nvar, MaxRays, row, f, pos, neg)) {
2169 if (independent) {
2170 if (i != 0)
2171 SwapColumns(*neg, nvar+1, nvar+1+i);
2173 return true;
2178 return false;
2181 static bool double_bound_pair(Polyhedron *P, int nvar, int exist,
2182 int i, int l1, int l2,
2183 Polyhedron **pos, Polyhedron **neg)
2185 Value f;
2186 value_init(f);
2187 Vector *row = Vector_Alloc(P->Dimension+2);
2188 value_set_si(row->p[0], 1);
2189 value_oppose(f, P->Constraint[l1][nvar+i+1]);
2190 Vector_Combine(P->Constraint[l1]+1, P->Constraint[l2]+1,
2191 row->p+1,
2192 P->Constraint[l2][nvar+i+1], f,
2193 P->Dimension+1);
2194 ConstraintSimplify(row->p, row->p, P->Dimension+2, &f);
2195 *pos = AddConstraints(row->p, 1, P, 0);
2196 value_set_si(f, -1);
2197 Vector_Scale(row->p+1, row->p+1, f, P->Dimension+1);
2198 value_decrement(row->p[P->Dimension+1], row->p[P->Dimension+1]);
2199 *neg = AddConstraints(row->p, 1, P, 0);
2200 Vector_Free(row);
2201 value_clear(f);
2203 return !emptyQ((*pos)) && !emptyQ((*neg));
2206 static bool double_bound(Polyhedron *P, int nvar, int exist,
2207 Polyhedron **pos, Polyhedron **neg)
2209 for (int i = 0; i < exist; ++i) {
2210 int l1, l2;
2211 for (l1 = P->NbEq; l1 < P->NbConstraints; ++l1) {
2212 if (value_negz_p(P->Constraint[l1][nvar+i+1]))
2213 continue;
2214 for (l2 = l1 + 1; l2 < P->NbConstraints; ++l2) {
2215 if (value_negz_p(P->Constraint[l2][nvar+i+1]))
2216 continue;
2217 if (double_bound_pair(P, nvar, exist, i, l1, l2, pos, neg))
2218 return true;
2221 for (l1 = P->NbEq; l1 < P->NbConstraints; ++l1) {
2222 if (value_posz_p(P->Constraint[l1][nvar+i+1]))
2223 continue;
2224 if (l1 < P->NbConstraints)
2225 for (l2 = l1 + 1; l2 < P->NbConstraints; ++l2) {
2226 if (value_posz_p(P->Constraint[l2][nvar+i+1]))
2227 continue;
2228 if (double_bound_pair(P, nvar, exist, i, l1, l2, pos, neg))
2229 return true;
2232 return false;
2234 return false;
2237 enum constraint {
2238 ALL_POS = 1 << 0,
2239 ONE_NEG = 1 << 1,
2240 INDEPENDENT = 1 << 2,
2241 ROT_NEG = 1 << 3
2244 static evalue* enumerate_or(Polyhedron *D,
2245 unsigned exist, unsigned nparam, barvinok_options *options)
2247 #ifdef DEBUG_ER
2248 fprintf(stderr, "\nER: Or\n");
2249 #endif /* DEBUG_ER */
2251 Polyhedron *N = D->next;
2252 D->next = 0;
2253 evalue *EP =
2254 barvinok_enumerate_e_with_options(D, exist, nparam, options);
2255 Polyhedron_Free(D);
2257 for (D = N; D; D = N) {
2258 N = D->next;
2259 D->next = 0;
2261 evalue *EN =
2262 barvinok_enumerate_e_with_options(D, exist, nparam, options);
2264 eor(EN, EP);
2265 free_evalue_refs(EN);
2266 free(EN);
2267 Polyhedron_Free(D);
2270 reduce_evalue(EP);
2272 return EP;
2275 static evalue* enumerate_sum(Polyhedron *P,
2276 unsigned exist, unsigned nparam, barvinok_options *options)
2278 int nvar = P->Dimension - exist - nparam;
2279 int toswap = nvar < exist ? nvar : exist;
2280 for (int i = 0; i < toswap; ++i)
2281 SwapColumns(P, 1 + i, nvar+exist - i);
2282 nparam += nvar;
2284 #ifdef DEBUG_ER
2285 fprintf(stderr, "\nER: Sum\n");
2286 #endif /* DEBUG_ER */
2288 evalue *EP = barvinok_enumerate_e_with_options(P, exist, nparam, options);
2290 evalue_split_domains_into_orthants(EP, options->MaxRays);
2291 reduce_evalue(EP);
2292 evalue_range_reduction(EP);
2294 evalue_frac2floor2(EP, 1);
2296 evalue *sum = esum(EP, nvar);
2298 free_evalue_refs(EP);
2299 free(EP);
2300 EP = sum;
2302 evalue_range_reduction(EP);
2304 return EP;
2307 static evalue* split_sure(Polyhedron *P, Polyhedron *S,
2308 unsigned exist, unsigned nparam, barvinok_options *options)
2310 int nvar = P->Dimension - exist - nparam;
2312 Matrix *M = Matrix_Alloc(exist, S->Dimension+2);
2313 for (int i = 0; i < exist; ++i)
2314 value_set_si(M->p[i][nvar+i+1], 1);
2315 Polyhedron *O = S;
2316 S = DomainAddRays(S, M, options->MaxRays);
2317 Polyhedron_Free(O);
2318 Polyhedron *F = DomainAddRays(P, M, options->MaxRays);
2319 Polyhedron *D = DomainDifference(F, S, options->MaxRays);
2320 O = D;
2321 D = Disjoint_Domain(D, 0, options->MaxRays);
2322 Polyhedron_Free(F);
2323 Domain_Free(O);
2324 Matrix_Free(M);
2326 M = Matrix_Alloc(P->Dimension+1-exist, P->Dimension+1);
2327 for (int j = 0; j < nvar; ++j)
2328 value_set_si(M->p[j][j], 1);
2329 for (int j = 0; j < nparam+1; ++j)
2330 value_set_si(M->p[nvar+j][nvar+exist+j], 1);
2331 Polyhedron *T = Polyhedron_Image(S, M, options->MaxRays);
2332 evalue *EP = barvinok_enumerate_e_with_options(T, 0, nparam, options);
2333 Polyhedron_Free(S);
2334 Polyhedron_Free(T);
2335 Matrix_Free(M);
2337 for (Polyhedron *Q = D; Q; Q = Q->next) {
2338 Polyhedron *N = Q->next;
2339 Q->next = 0;
2340 T = DomainIntersection(P, Q, options->MaxRays);
2341 evalue *E = barvinok_enumerate_e_with_options(T, exist, nparam, options);
2342 eadd(E, EP);
2343 free_evalue_refs(E);
2344 free(E);
2345 Polyhedron_Free(T);
2346 Q->next = N;
2348 Domain_Free(D);
2349 return EP;
2352 static evalue* enumerate_sure(Polyhedron *P,
2353 unsigned exist, unsigned nparam, barvinok_options *options)
2355 int i;
2356 Polyhedron *S = P;
2357 int nvar = P->Dimension - exist - nparam;
2358 Value lcm;
2359 Value f;
2360 value_init(lcm);
2361 value_init(f);
2363 for (i = 0; i < exist; ++i) {
2364 Matrix *M = Matrix_Alloc(S->NbConstraints, S->Dimension+2);
2365 int c = 0;
2366 value_set_si(lcm, 1);
2367 for (int j = 0; j < S->NbConstraints; ++j) {
2368 if (value_negz_p(S->Constraint[j][1+nvar+i]))
2369 continue;
2370 if (value_one_p(S->Constraint[j][1+nvar+i]))
2371 continue;
2372 value_lcm(lcm, S->Constraint[j][1+nvar+i], &lcm);
2375 for (int j = 0; j < S->NbConstraints; ++j) {
2376 if (value_negz_p(S->Constraint[j][1+nvar+i]))
2377 continue;
2378 if (value_one_p(S->Constraint[j][1+nvar+i]))
2379 continue;
2380 value_division(f, lcm, S->Constraint[j][1+nvar+i]);
2381 Vector_Scale(S->Constraint[j], M->p[c], f, S->Dimension+2);
2382 value_subtract(M->p[c][S->Dimension+1],
2383 M->p[c][S->Dimension+1],
2384 lcm);
2385 value_increment(M->p[c][S->Dimension+1],
2386 M->p[c][S->Dimension+1]);
2387 ++c;
2389 Polyhedron *O = S;
2390 S = AddConstraints(M->p[0], c, S, options->MaxRays);
2391 if (O != P)
2392 Polyhedron_Free(O);
2393 Matrix_Free(M);
2394 if (emptyQ(S)) {
2395 Polyhedron_Free(S);
2396 value_clear(lcm);
2397 value_clear(f);
2398 return 0;
2401 value_clear(lcm);
2402 value_clear(f);
2404 #ifdef DEBUG_ER
2405 fprintf(stderr, "\nER: Sure\n");
2406 #endif /* DEBUG_ER */
2408 return split_sure(P, S, exist, nparam, options);
2411 static evalue* enumerate_sure2(Polyhedron *P,
2412 unsigned exist, unsigned nparam, barvinok_options *options)
2414 int nvar = P->Dimension - exist - nparam;
2415 int r;
2416 for (r = 0; r < P->NbRays; ++r)
2417 if (value_one_p(P->Ray[r][0]) &&
2418 value_one_p(P->Ray[r][P->Dimension+1]))
2419 break;
2421 if (r >= P->NbRays)
2422 return 0;
2424 Matrix *M = Matrix_Alloc(nvar + 1 + nparam, P->Dimension+2);
2425 for (int i = 0; i < nvar; ++i)
2426 value_set_si(M->p[i][1+i], 1);
2427 for (int i = 0; i < nparam; ++i)
2428 value_set_si(M->p[i+nvar][1+nvar+exist+i], 1);
2429 Vector_Copy(P->Ray[r]+1+nvar, M->p[nvar+nparam]+1+nvar, exist);
2430 value_set_si(M->p[nvar+nparam][0], 1);
2431 value_set_si(M->p[nvar+nparam][P->Dimension+1], 1);
2432 Polyhedron * F = Rays2Polyhedron(M, options->MaxRays);
2433 Matrix_Free(M);
2435 Polyhedron *I = DomainIntersection(F, P, options->MaxRays);
2436 Polyhedron_Free(F);
2438 #ifdef DEBUG_ER
2439 fprintf(stderr, "\nER: Sure2\n");
2440 #endif /* DEBUG_ER */
2442 return split_sure(P, I, exist, nparam, options);
2445 static evalue* enumerate_cyclic(Polyhedron *P,
2446 unsigned exist, unsigned nparam,
2447 evalue * EP, int r, int p, unsigned MaxRays)
2449 int nvar = P->Dimension - exist - nparam;
2451 /* If EP in its fractional maps only contains references
2452 * to the remainder parameter with appropriate coefficients
2453 * then we could in principle avoid adding existentially
2454 * quantified variables to the validity domains.
2455 * We'd have to replace the remainder by m { p/m }
2456 * and multiply with an appropriate factor that is one
2457 * only in the appropriate range.
2458 * This last multiplication can be avoided if EP
2459 * has a single validity domain with no (further)
2460 * constraints on the remainder parameter
2463 Matrix *CT = Matrix_Alloc(nparam+1, nparam+3);
2464 Matrix *M = Matrix_Alloc(1, 1+nparam+3);
2465 for (int j = 0; j < nparam; ++j)
2466 if (j != p)
2467 value_set_si(CT->p[j][j], 1);
2468 value_set_si(CT->p[p][nparam+1], 1);
2469 value_set_si(CT->p[nparam][nparam+2], 1);
2470 value_set_si(M->p[0][1+p], -1);
2471 value_absolute(M->p[0][1+nparam], P->Ray[0][1+nvar+exist+p]);
2472 value_set_si(M->p[0][1+nparam+1], 1);
2473 Polyhedron *CEq = Constraints2Polyhedron(M, 1);
2474 Matrix_Free(M);
2475 addeliminatedparams_enum(EP, CT, CEq, MaxRays, nparam);
2476 Polyhedron_Free(CEq);
2477 Matrix_Free(CT);
2479 return EP;
2482 static void enumerate_vd_add_ray(evalue *EP, Matrix *Rays, unsigned MaxRays)
2484 if (value_notzero_p(EP->d))
2485 return;
2487 assert(EP->x.p->type == partition);
2488 assert(EP->x.p->pos == EVALUE_DOMAIN(EP->x.p->arr[0])->Dimension);
2489 for (int i = 0; i < EP->x.p->size/2; ++i) {
2490 Polyhedron *D = EVALUE_DOMAIN(EP->x.p->arr[2*i]);
2491 Polyhedron *N = DomainAddRays(D, Rays, MaxRays);
2492 EVALUE_SET_DOMAIN(EP->x.p->arr[2*i], N);
2493 Domain_Free(D);
2497 static evalue* enumerate_line(Polyhedron *P,
2498 unsigned exist, unsigned nparam, barvinok_options *options)
2500 if (P->NbBid == 0)
2501 return 0;
2503 #ifdef DEBUG_ER
2504 fprintf(stderr, "\nER: Line\n");
2505 #endif /* DEBUG_ER */
2507 int nvar = P->Dimension - exist - nparam;
2508 int i, j;
2509 for (i = 0; i < nparam; ++i)
2510 if (value_notzero_p(P->Ray[0][1+nvar+exist+i]))
2511 break;
2512 assert(i < nparam);
2513 for (j = i+1; j < nparam; ++j)
2514 if (value_notzero_p(P->Ray[0][1+nvar+exist+i]))
2515 break;
2516 assert(j >= nparam); // for now
2518 Matrix *M = Matrix_Alloc(2, P->Dimension+2);
2519 value_set_si(M->p[0][0], 1);
2520 value_set_si(M->p[0][1+nvar+exist+i], 1);
2521 value_set_si(M->p[1][0], 1);
2522 value_set_si(M->p[1][1+nvar+exist+i], -1);
2523 value_absolute(M->p[1][1+P->Dimension], P->Ray[0][1+nvar+exist+i]);
2524 value_decrement(M->p[1][1+P->Dimension], M->p[1][1+P->Dimension]);
2525 Polyhedron *S = AddConstraints(M->p[0], 2, P, options->MaxRays);
2526 evalue *EP = barvinok_enumerate_e_with_options(S, exist, nparam, options);
2527 Polyhedron_Free(S);
2528 Matrix_Free(M);
2530 return enumerate_cyclic(P, exist, nparam, EP, 0, i, options->MaxRays);
2533 static int single_param_pos(Polyhedron*P, unsigned exist, unsigned nparam,
2534 int r)
2536 int nvar = P->Dimension - exist - nparam;
2537 if (First_Non_Zero(P->Ray[r]+1, nvar) != -1)
2538 return -1;
2539 int i = First_Non_Zero(P->Ray[r]+1+nvar+exist, nparam);
2540 if (i == -1)
2541 return -1;
2542 if (First_Non_Zero(P->Ray[r]+1+nvar+exist+1, nparam-i-1) != -1)
2543 return -1;
2544 return i;
2547 static evalue* enumerate_remove_ray(Polyhedron *P, int r,
2548 unsigned exist, unsigned nparam, barvinok_options *options)
2550 #ifdef DEBUG_ER
2551 fprintf(stderr, "\nER: RedundantRay\n");
2552 #endif /* DEBUG_ER */
2554 Value one;
2555 value_init(one);
2556 value_set_si(one, 1);
2557 int len = P->NbRays-1;
2558 Matrix *M = Matrix_Alloc(2 * len, P->Dimension+2);
2559 Vector_Copy(P->Ray[0], M->p[0], r * (P->Dimension+2));
2560 Vector_Copy(P->Ray[r+1], M->p[r], (len-r) * (P->Dimension+2));
2561 for (int j = 0; j < P->NbRays; ++j) {
2562 if (j == r)
2563 continue;
2564 Vector_Combine(P->Ray[j], P->Ray[r], M->p[len+j-(j>r)],
2565 one, P->Ray[j][P->Dimension+1], P->Dimension+2);
2568 P = Rays2Polyhedron(M, options->MaxRays);
2569 Matrix_Free(M);
2570 evalue *EP = barvinok_enumerate_e_with_options(P, exist, nparam, options);
2571 Polyhedron_Free(P);
2572 value_clear(one);
2574 return EP;
2577 static evalue* enumerate_redundant_ray(Polyhedron *P,
2578 unsigned exist, unsigned nparam, barvinok_options *options)
2580 assert(P->NbBid == 0);
2581 int nvar = P->Dimension - exist - nparam;
2582 Value m;
2583 value_init(m);
2585 for (int r = 0; r < P->NbRays; ++r) {
2586 if (value_notzero_p(P->Ray[r][P->Dimension+1]))
2587 continue;
2588 int i1 = single_param_pos(P, exist, nparam, r);
2589 if (i1 == -1)
2590 continue;
2591 for (int r2 = r+1; r2 < P->NbRays; ++r2) {
2592 if (value_notzero_p(P->Ray[r2][P->Dimension+1]))
2593 continue;
2594 int i2 = single_param_pos(P, exist, nparam, r2);
2595 if (i2 == -1)
2596 continue;
2597 if (i1 != i2)
2598 continue;
2600 value_division(m, P->Ray[r][1+nvar+exist+i1],
2601 P->Ray[r2][1+nvar+exist+i1]);
2602 value_multiply(m, m, P->Ray[r2][1+nvar+exist+i1]);
2603 /* r2 divides r => r redundant */
2604 if (value_eq(m, P->Ray[r][1+nvar+exist+i1])) {
2605 value_clear(m);
2606 return enumerate_remove_ray(P, r, exist, nparam, options);
2609 value_division(m, P->Ray[r2][1+nvar+exist+i1],
2610 P->Ray[r][1+nvar+exist+i1]);
2611 value_multiply(m, m, P->Ray[r][1+nvar+exist+i1]);
2612 /* r divides r2 => r2 redundant */
2613 if (value_eq(m, P->Ray[r2][1+nvar+exist+i1])) {
2614 value_clear(m);
2615 return enumerate_remove_ray(P, r2, exist, nparam, options);
2619 value_clear(m);
2620 return 0;
2623 static Polyhedron *upper_bound(Polyhedron *P,
2624 int pos, Value *max, Polyhedron **R)
2626 Value v;
2627 int r;
2628 value_init(v);
2630 *R = 0;
2631 Polyhedron *N;
2632 Polyhedron *B = 0;
2633 for (Polyhedron *Q = P; Q; Q = N) {
2634 N = Q->next;
2635 for (r = 0; r < P->NbRays; ++r) {
2636 if (value_zero_p(P->Ray[r][P->Dimension+1]) &&
2637 value_pos_p(P->Ray[r][1+pos]))
2638 break;
2640 if (r < P->NbRays) {
2641 Q->next = *R;
2642 *R = Q;
2643 continue;
2644 } else {
2645 Q->next = B;
2646 B = Q;
2648 for (r = 0; r < P->NbRays; ++r) {
2649 if (value_zero_p(P->Ray[r][P->Dimension+1]))
2650 continue;
2651 mpz_fdiv_q(v, P->Ray[r][1+pos], P->Ray[r][1+P->Dimension]);
2652 if ((!Q->next && r == 0) || value_gt(v, *max))
2653 value_assign(*max, v);
2656 value_clear(v);
2657 return B;
2660 static evalue* enumerate_ray(Polyhedron *P,
2661 unsigned exist, unsigned nparam, barvinok_options *options)
2663 assert(P->NbBid == 0);
2664 int nvar = P->Dimension - exist - nparam;
2666 int r;
2667 for (r = 0; r < P->NbRays; ++r)
2668 if (value_zero_p(P->Ray[r][P->Dimension+1]))
2669 break;
2670 if (r >= P->NbRays)
2671 return 0;
2673 int r2;
2674 for (r2 = r+1; r2 < P->NbRays; ++r2)
2675 if (value_zero_p(P->Ray[r2][P->Dimension+1]))
2676 break;
2677 if (r2 < P->NbRays) {
2678 if (nvar > 0)
2679 return enumerate_sum(P, exist, nparam, options);
2682 #ifdef DEBUG_ER
2683 fprintf(stderr, "\nER: Ray\n");
2684 #endif /* DEBUG_ER */
2686 Value m;
2687 Value one;
2688 value_init(m);
2689 value_init(one);
2690 value_set_si(one, 1);
2691 int i = single_param_pos(P, exist, nparam, r);
2692 assert(i != -1); // for now;
2694 Matrix *M = Matrix_Alloc(P->NbRays, P->Dimension+2);
2695 for (int j = 0; j < P->NbRays; ++j) {
2696 Vector_Combine(P->Ray[j], P->Ray[r], M->p[j],
2697 one, P->Ray[j][P->Dimension+1], P->Dimension+2);
2699 Polyhedron *S = Rays2Polyhedron(M, options->MaxRays);
2700 Matrix_Free(M);
2701 Polyhedron *D = DomainDifference(P, S, options->MaxRays);
2702 Polyhedron_Free(S);
2703 // Polyhedron_Print(stderr, P_VALUE_FMT, D);
2704 assert(value_pos_p(P->Ray[r][1+nvar+exist+i])); // for now
2705 Polyhedron *R;
2706 D = upper_bound(D, nvar+exist+i, &m, &R);
2707 assert(D);
2708 Domain_Free(D);
2710 M = Matrix_Alloc(2, P->Dimension+2);
2711 value_set_si(M->p[0][0], 1);
2712 value_set_si(M->p[1][0], 1);
2713 value_set_si(M->p[0][1+nvar+exist+i], -1);
2714 value_set_si(M->p[1][1+nvar+exist+i], 1);
2715 value_assign(M->p[0][1+P->Dimension], m);
2716 value_oppose(M->p[1][1+P->Dimension], m);
2717 value_addto(M->p[1][1+P->Dimension], M->p[1][1+P->Dimension],
2718 P->Ray[r][1+nvar+exist+i]);
2719 value_decrement(M->p[1][1+P->Dimension], M->p[1][1+P->Dimension]);
2720 // Matrix_Print(stderr, P_VALUE_FMT, M);
2721 D = AddConstraints(M->p[0], 2, P, options->MaxRays);
2722 // Polyhedron_Print(stderr, P_VALUE_FMT, D);
2723 value_subtract(M->p[0][1+P->Dimension], M->p[0][1+P->Dimension],
2724 P->Ray[r][1+nvar+exist+i]);
2725 // Matrix_Print(stderr, P_VALUE_FMT, M);
2726 S = AddConstraints(M->p[0], 1, P, options->MaxRays);
2727 // Polyhedron_Print(stderr, P_VALUE_FMT, S);
2728 Matrix_Free(M);
2730 evalue *EP = barvinok_enumerate_e_with_options(D, exist, nparam, options);
2731 Polyhedron_Free(D);
2732 value_clear(one);
2733 value_clear(m);
2735 if (value_notone_p(P->Ray[r][1+nvar+exist+i]))
2736 EP = enumerate_cyclic(P, exist, nparam, EP, r, i, options->MaxRays);
2737 else {
2738 M = Matrix_Alloc(1, nparam+2);
2739 value_set_si(M->p[0][0], 1);
2740 value_set_si(M->p[0][1+i], 1);
2741 enumerate_vd_add_ray(EP, M, options->MaxRays);
2742 Matrix_Free(M);
2745 if (!emptyQ(S)) {
2746 evalue *E = barvinok_enumerate_e_with_options(S, exist, nparam, options);
2747 eadd(E, EP);
2748 free_evalue_refs(E);
2749 free(E);
2751 Polyhedron_Free(S);
2753 if (R) {
2754 assert(nvar == 0);
2755 evalue *ER = enumerate_or(R, exist, nparam, options);
2756 eor(ER, EP);
2757 free_evalue_refs(ER);
2758 free(ER);
2761 return EP;
2764 static evalue* enumerate_vd(Polyhedron **PA,
2765 unsigned exist, unsigned nparam, barvinok_options *options)
2767 Polyhedron *P = *PA;
2768 int nvar = P->Dimension - exist - nparam;
2769 Param_Polyhedron *PP = NULL;
2770 Polyhedron *C = Universe_Polyhedron(nparam);
2771 Polyhedron *CEq;
2772 Matrix *CT;
2773 Polyhedron *PR = P;
2774 PP = Polyhedron2Param_SimplifiedDomain(&PR,C, options->MaxRays,&CEq,&CT);
2775 Polyhedron_Free(C);
2777 int nd;
2778 Param_Domain *D, *last;
2779 Value c;
2780 value_init(c);
2781 for (nd = 0, D=PP->D; D; D=D->next, ++nd)
2784 Polyhedron **VD = new Polyhedron_p[nd];
2785 Polyhedron **fVD = new Polyhedron_p[nd];
2786 for(nd = 0, D=PP->D; D; D=D->next) {
2787 Polyhedron *rVD = reduce_domain(D->Domain, CT, CEq, fVD, nd, options);
2788 if (!rVD)
2789 continue;
2791 VD[nd++] = rVD;
2792 last = D;
2795 evalue *EP = 0;
2797 if (nd == 0)
2798 EP = evalue_zero();
2800 /* This doesn't seem to have any effect */
2801 if (nd == 1) {
2802 Polyhedron *CA = align_context(VD[0], P->Dimension, options->MaxRays);
2803 Polyhedron *O = P;
2804 P = DomainIntersection(P, CA, options->MaxRays);
2805 if (O != *PA)
2806 Polyhedron_Free(O);
2807 Polyhedron_Free(CA);
2808 if (emptyQ(P))
2809 EP = evalue_zero();
2812 if (!EP && CT->NbColumns != CT->NbRows) {
2813 Polyhedron *CEqr = DomainImage(CEq, CT, options->MaxRays);
2814 Polyhedron *CA = align_context(CEqr, PR->Dimension, options->MaxRays);
2815 Polyhedron *I = DomainIntersection(PR, CA, options->MaxRays);
2816 Polyhedron_Free(CEqr);
2817 Polyhedron_Free(CA);
2818 #ifdef DEBUG_ER
2819 fprintf(stderr, "\nER: Eliminate\n");
2820 #endif /* DEBUG_ER */
2821 nparam -= CT->NbColumns - CT->NbRows;
2822 EP = barvinok_enumerate_e_with_options(I, exist, nparam, options);
2823 nparam += CT->NbColumns - CT->NbRows;
2824 addeliminatedparams_enum(EP, CT, CEq, options->MaxRays, nparam);
2825 Polyhedron_Free(I);
2827 if (PR != *PA)
2828 Polyhedron_Free(PR);
2829 PR = 0;
2831 if (!EP && nd > 1) {
2832 #ifdef DEBUG_ER
2833 fprintf(stderr, "\nER: VD\n");
2834 #endif /* DEBUG_ER */
2835 for (int i = 0; i < nd; ++i) {
2836 Polyhedron *CA = align_context(VD[i], P->Dimension, options->MaxRays);
2837 Polyhedron *I = DomainIntersection(P, CA, options->MaxRays);
2839 if (i == 0)
2840 EP = barvinok_enumerate_e_with_options(I, exist, nparam, options);
2841 else {
2842 evalue *E = barvinok_enumerate_e_with_options(I, exist, nparam,
2843 options);
2844 eadd(E, EP);
2845 free_evalue_refs(E);
2846 free(E);
2848 Polyhedron_Free(I);
2849 Polyhedron_Free(CA);
2853 for (int i = 0; i < nd; ++i) {
2854 Polyhedron_Free(VD[i]);
2855 Polyhedron_Free(fVD[i]);
2857 delete [] VD;
2858 delete [] fVD;
2859 value_clear(c);
2861 if (!EP && nvar == 0) {
2862 Value f;
2863 value_init(f);
2864 Param_Vertices *V, *V2;
2865 Matrix* M = Matrix_Alloc(1, P->Dimension+2);
2867 FORALL_PVertex_in_ParamPolyhedron(V, last, PP) {
2868 bool found = false;
2869 FORALL_PVertex_in_ParamPolyhedron(V2, last, PP) {
2870 if (V == V2) {
2871 found = true;
2872 continue;
2874 if (!found)
2875 continue;
2876 for (int i = 0; i < exist; ++i) {
2877 value_oppose(f, V->Vertex->p[i][nparam+1]);
2878 Vector_Combine(V->Vertex->p[i],
2879 V2->Vertex->p[i],
2880 M->p[0] + 1 + nvar + exist,
2881 V2->Vertex->p[i][nparam+1],
2883 nparam+1);
2884 int j;
2885 for (j = 0; j < nparam; ++j)
2886 if (value_notzero_p(M->p[0][1+nvar+exist+j]))
2887 break;
2888 if (j >= nparam)
2889 continue;
2890 ConstraintSimplify(M->p[0], M->p[0],
2891 P->Dimension+2, &f);
2892 value_set_si(M->p[0][0], 0);
2893 Polyhedron *para = AddConstraints(M->p[0], 1, P,
2894 options->MaxRays);
2895 if (emptyQ(para)) {
2896 Polyhedron_Free(para);
2897 continue;
2899 Polyhedron *pos, *neg;
2900 value_set_si(M->p[0][0], 1);
2901 value_decrement(M->p[0][P->Dimension+1],
2902 M->p[0][P->Dimension+1]);
2903 neg = AddConstraints(M->p[0], 1, P, options->MaxRays);
2904 value_set_si(f, -1);
2905 Vector_Scale(M->p[0]+1, M->p[0]+1, f,
2906 P->Dimension+1);
2907 value_decrement(M->p[0][P->Dimension+1],
2908 M->p[0][P->Dimension+1]);
2909 value_decrement(M->p[0][P->Dimension+1],
2910 M->p[0][P->Dimension+1]);
2911 pos = AddConstraints(M->p[0], 1, P, options->MaxRays);
2912 if (emptyQ(neg) && emptyQ(pos)) {
2913 Polyhedron_Free(para);
2914 Polyhedron_Free(pos);
2915 Polyhedron_Free(neg);
2916 continue;
2918 #ifdef DEBUG_ER
2919 fprintf(stderr, "\nER: Order\n");
2920 #endif /* DEBUG_ER */
2921 EP = barvinok_enumerate_e_with_options(para, exist, nparam,
2922 options);
2923 evalue *E;
2924 if (!emptyQ(pos)) {
2925 E = barvinok_enumerate_e_with_options(pos, exist, nparam,
2926 options);
2927 eadd(E, EP);
2928 free_evalue_refs(E);
2929 free(E);
2931 if (!emptyQ(neg)) {
2932 E = barvinok_enumerate_e_with_options(neg, exist, nparam,
2933 options);
2934 eadd(E, EP);
2935 free_evalue_refs(E);
2936 free(E);
2938 Polyhedron_Free(para);
2939 Polyhedron_Free(pos);
2940 Polyhedron_Free(neg);
2941 break;
2943 if (EP)
2944 break;
2945 } END_FORALL_PVertex_in_ParamPolyhedron;
2946 if (EP)
2947 break;
2948 } END_FORALL_PVertex_in_ParamPolyhedron;
2950 if (!EP) {
2951 /* Search for vertex coordinate to split on */
2952 /* First look for one independent of the parameters */
2953 FORALL_PVertex_in_ParamPolyhedron(V, last, PP) {
2954 for (int i = 0; i < exist; ++i) {
2955 int j;
2956 for (j = 0; j < nparam; ++j)
2957 if (value_notzero_p(V->Vertex->p[i][j]))
2958 break;
2959 if (j < nparam)
2960 continue;
2961 value_set_si(M->p[0][0], 1);
2962 Vector_Set(M->p[0]+1, 0, nvar+exist);
2963 Vector_Copy(V->Vertex->p[i],
2964 M->p[0] + 1 + nvar + exist, nparam+1);
2965 value_oppose(M->p[0][1+nvar+i],
2966 V->Vertex->p[i][nparam+1]);
2968 Polyhedron *pos, *neg;
2969 value_set_si(M->p[0][0], 1);
2970 value_decrement(M->p[0][P->Dimension+1],
2971 M->p[0][P->Dimension+1]);
2972 neg = AddConstraints(M->p[0], 1, P, options->MaxRays);
2973 value_set_si(f, -1);
2974 Vector_Scale(M->p[0]+1, M->p[0]+1, f,
2975 P->Dimension+1);
2976 value_decrement(M->p[0][P->Dimension+1],
2977 M->p[0][P->Dimension+1]);
2978 value_decrement(M->p[0][P->Dimension+1],
2979 M->p[0][P->Dimension+1]);
2980 pos = AddConstraints(M->p[0], 1, P, options->MaxRays);
2981 if (emptyQ(neg) || emptyQ(pos)) {
2982 Polyhedron_Free(pos);
2983 Polyhedron_Free(neg);
2984 continue;
2986 Polyhedron_Free(pos);
2987 value_increment(M->p[0][P->Dimension+1],
2988 M->p[0][P->Dimension+1]);
2989 pos = AddConstraints(M->p[0], 1, P, options->MaxRays);
2990 #ifdef DEBUG_ER
2991 fprintf(stderr, "\nER: Vertex\n");
2992 #endif /* DEBUG_ER */
2993 pos->next = neg;
2994 EP = enumerate_or(pos, exist, nparam, options);
2995 break;
2997 if (EP)
2998 break;
2999 } END_FORALL_PVertex_in_ParamPolyhedron;
3002 if (!EP) {
3003 /* Search for vertex coordinate to split on */
3004 /* Now look for one that depends on the parameters */
3005 FORALL_PVertex_in_ParamPolyhedron(V, last, PP) {
3006 for (int i = 0; i < exist; ++i) {
3007 value_set_si(M->p[0][0], 1);
3008 Vector_Set(M->p[0]+1, 0, nvar+exist);
3009 Vector_Copy(V->Vertex->p[i],
3010 M->p[0] + 1 + nvar + exist, nparam+1);
3011 value_oppose(M->p[0][1+nvar+i],
3012 V->Vertex->p[i][nparam+1]);
3014 Polyhedron *pos, *neg;
3015 value_set_si(M->p[0][0], 1);
3016 value_decrement(M->p[0][P->Dimension+1],
3017 M->p[0][P->Dimension+1]);
3018 neg = AddConstraints(M->p[0], 1, P, options->MaxRays);
3019 value_set_si(f, -1);
3020 Vector_Scale(M->p[0]+1, M->p[0]+1, f,
3021 P->Dimension+1);
3022 value_decrement(M->p[0][P->Dimension+1],
3023 M->p[0][P->Dimension+1]);
3024 value_decrement(M->p[0][P->Dimension+1],
3025 M->p[0][P->Dimension+1]);
3026 pos = AddConstraints(M->p[0], 1, P, options->MaxRays);
3027 if (emptyQ(neg) || emptyQ(pos)) {
3028 Polyhedron_Free(pos);
3029 Polyhedron_Free(neg);
3030 continue;
3032 Polyhedron_Free(pos);
3033 value_increment(M->p[0][P->Dimension+1],
3034 M->p[0][P->Dimension+1]);
3035 pos = AddConstraints(M->p[0], 1, P, options->MaxRays);
3036 #ifdef DEBUG_ER
3037 fprintf(stderr, "\nER: ParamVertex\n");
3038 #endif /* DEBUG_ER */
3039 pos->next = neg;
3040 EP = enumerate_or(pos, exist, nparam, options);
3041 break;
3043 if (EP)
3044 break;
3045 } END_FORALL_PVertex_in_ParamPolyhedron;
3048 Matrix_Free(M);
3049 value_clear(f);
3052 if (CEq)
3053 Polyhedron_Free(CEq);
3054 if (CT)
3055 Matrix_Free(CT);
3056 if (PP)
3057 Param_Polyhedron_Free(PP);
3058 *PA = P;
3060 return EP;
3063 evalue* barvinok_enumerate_pip(Polyhedron *P, unsigned exist, unsigned nparam,
3064 unsigned MaxRays)
3066 evalue *E;
3067 barvinok_options *options = barvinok_options_new_with_defaults();
3068 options->MaxRays = MaxRays;
3069 E = barvinok_enumerate_pip_with_options(P, exist, nparam, options);
3070 barvinok_options_free(options);
3071 return E;
3074 #ifndef HAVE_PIPLIB
3075 evalue *barvinok_enumerate_pip_with_options(Polyhedron *P,
3076 unsigned exist, unsigned nparam, struct barvinok_options *options)
3078 return 0;
3080 #else
3081 evalue *barvinok_enumerate_pip_with_options(Polyhedron *P,
3082 unsigned exist, unsigned nparam, struct barvinok_options *options)
3084 int nvar = P->Dimension - exist - nparam;
3085 evalue *EP = evalue_zero();
3086 Polyhedron *Q, *N;
3088 #ifdef DEBUG_ER
3089 fprintf(stderr, "\nER: PIP\n");
3090 #endif /* DEBUG_ER */
3092 Polyhedron *D = pip_projectout(P, nvar, exist, nparam);
3093 for (Q = D; Q; Q = N) {
3094 N = Q->next;
3095 Q->next = 0;
3096 evalue *E;
3097 exist = Q->Dimension - nvar - nparam;
3098 E = barvinok_enumerate_e_with_options(Q, exist, nparam, options);
3099 Polyhedron_Free(Q);
3100 eadd(E, EP);
3101 free_evalue_refs(E);
3102 free(E);
3105 return EP;
3107 #endif
3110 static bool is_single(Value *row, int pos, int len)
3112 return First_Non_Zero(row, pos) == -1 &&
3113 First_Non_Zero(row+pos+1, len-pos-1) == -1;
3116 static evalue* barvinok_enumerate_e_r(Polyhedron *P,
3117 unsigned exist, unsigned nparam, barvinok_options *options);
3119 #ifdef DEBUG_ER
3120 static int er_level = 0;
3122 evalue* barvinok_enumerate_e_with_options(Polyhedron *P,
3123 unsigned exist, unsigned nparam, barvinok_options *options)
3125 fprintf(stderr, "\nER: level %i\n", er_level);
3127 Polyhedron_PrintConstraints(stderr, P_VALUE_FMT, P);
3128 fprintf(stderr, "\nE %d\nP %d\n", exist, nparam);
3129 ++er_level;
3130 P = DomainConstraintSimplify(Polyhedron_Copy(P), options->MaxRays);
3131 evalue *EP = barvinok_enumerate_e_r(P, exist, nparam, options);
3132 Polyhedron_Free(P);
3133 --er_level;
3134 return EP;
3136 #else
3137 evalue* barvinok_enumerate_e_with_options(Polyhedron *P,
3138 unsigned exist, unsigned nparam, barvinok_options *options)
3140 P = DomainConstraintSimplify(Polyhedron_Copy(P), options->MaxRays);
3141 evalue *EP = barvinok_enumerate_e_r(P, exist, nparam, options);
3142 Polyhedron_Free(P);
3143 return EP;
3145 #endif
3147 evalue* barvinok_enumerate_e(Polyhedron *P, unsigned exist, unsigned nparam,
3148 unsigned MaxRays)
3150 evalue *E;
3151 barvinok_options *options = barvinok_options_new_with_defaults();
3152 options->MaxRays = MaxRays;
3153 E = barvinok_enumerate_e_with_options(P, exist, nparam, options);
3154 barvinok_options_free(options);
3155 return E;
3158 static evalue* barvinok_enumerate_e_r(Polyhedron *P,
3159 unsigned exist, unsigned nparam, barvinok_options *options)
3161 if (exist == 0) {
3162 Polyhedron *U = Universe_Polyhedron(nparam);
3163 evalue *EP = barvinok_enumerate_with_options(P, U, options);
3164 //char *param_name[] = {"P", "Q", "R", "S", "T" };
3165 //print_evalue(stdout, EP, param_name);
3166 Polyhedron_Free(U);
3167 return EP;
3170 int nvar = P->Dimension - exist - nparam;
3171 int len = P->Dimension + 2;
3173 /* for now */
3174 POL_ENSURE_FACETS(P);
3175 POL_ENSURE_VERTICES(P);
3177 if (emptyQ(P))
3178 return evalue_zero();
3180 if (nvar == 0 && nparam == 0) {
3181 evalue *EP = evalue_zero();
3182 barvinok_count_with_options(P, &EP->x.n, options);
3183 if (value_pos_p(EP->x.n))
3184 value_set_si(EP->x.n, 1);
3185 return EP;
3188 int r;
3189 for (r = 0; r < P->NbRays; ++r)
3190 if (value_zero_p(P->Ray[r][0]) ||
3191 value_zero_p(P->Ray[r][P->Dimension+1])) {
3192 int i;
3193 for (i = 0; i < nvar; ++i)
3194 if (value_notzero_p(P->Ray[r][i+1]))
3195 break;
3196 if (i >= nvar)
3197 continue;
3198 for (i = nvar + exist; i < nvar + exist + nparam; ++i)
3199 if (value_notzero_p(P->Ray[r][i+1]))
3200 break;
3201 if (i >= nvar + exist + nparam)
3202 break;
3204 if (r < P->NbRays) {
3205 evalue *EP = evalue_zero();
3206 value_set_si(EP->x.n, -1);
3207 return EP;
3210 int first;
3211 for (r = 0; r < P->NbEq; ++r)
3212 if ((first = First_Non_Zero(P->Constraint[r]+1+nvar, exist)) != -1)
3213 break;
3214 if (r < P->NbEq) {
3215 if (First_Non_Zero(P->Constraint[r]+1+nvar+first+1,
3216 exist-first-1) != -1) {
3217 Polyhedron *T = rotate_along(P, r, nvar, exist, options->MaxRays);
3218 #ifdef DEBUG_ER
3219 fprintf(stderr, "\nER: Equality\n");
3220 #endif /* DEBUG_ER */
3221 evalue *EP = barvinok_enumerate_e_with_options(T, exist-1, nparam,
3222 options);
3223 Polyhedron_Free(T);
3224 return EP;
3225 } else {
3226 #ifdef DEBUG_ER
3227 fprintf(stderr, "\nER: Fixed\n");
3228 #endif /* DEBUG_ER */
3229 if (first == 0)
3230 return barvinok_enumerate_e_with_options(P, exist-1, nparam,
3231 options);
3232 else {
3233 Polyhedron *T = Polyhedron_Copy(P);
3234 SwapColumns(T, nvar+1, nvar+1+first);
3235 evalue *EP = barvinok_enumerate_e_with_options(T, exist-1, nparam,
3236 options);
3237 Polyhedron_Free(T);
3238 return EP;
3243 Vector *row = Vector_Alloc(len);
3244 value_set_si(row->p[0], 1);
3246 Value f;
3247 value_init(f);
3249 enum constraint* info = new constraint[exist];
3250 for (int i = 0; i < exist; ++i) {
3251 info[i] = ALL_POS;
3252 for (int l = P->NbEq; l < P->NbConstraints; ++l) {
3253 if (value_negz_p(P->Constraint[l][nvar+i+1]))
3254 continue;
3255 bool l_parallel = is_single(P->Constraint[l]+nvar+1, i, exist);
3256 for (int u = P->NbEq; u < P->NbConstraints; ++u) {
3257 if (value_posz_p(P->Constraint[u][nvar+i+1]))
3258 continue;
3259 bool lu_parallel = l_parallel ||
3260 is_single(P->Constraint[u]+nvar+1, i, exist);
3261 value_oppose(f, P->Constraint[u][nvar+i+1]);
3262 Vector_Combine(P->Constraint[l]+1, P->Constraint[u]+1, row->p+1,
3263 f, P->Constraint[l][nvar+i+1], len-1);
3264 if (!(info[i] & INDEPENDENT)) {
3265 int j;
3266 for (j = 0; j < exist; ++j)
3267 if (j != i && value_notzero_p(row->p[nvar+j+1]))
3268 break;
3269 if (j == exist) {
3270 //printf("independent: i: %d, l: %d, u: %d\n", i, l, u);
3271 info[i] = (constraint)(info[i] | INDEPENDENT);
3274 if (info[i] & ALL_POS) {
3275 value_addto(row->p[len-1], row->p[len-1],
3276 P->Constraint[l][nvar+i+1]);
3277 value_addto(row->p[len-1], row->p[len-1], f);
3278 value_multiply(f, f, P->Constraint[l][nvar+i+1]);
3279 value_subtract(row->p[len-1], row->p[len-1], f);
3280 value_decrement(row->p[len-1], row->p[len-1]);
3281 ConstraintSimplify(row->p, row->p, len, &f);
3282 value_set_si(f, -1);
3283 Vector_Scale(row->p+1, row->p+1, f, len-1);
3284 value_decrement(row->p[len-1], row->p[len-1]);
3285 Polyhedron *T = AddConstraints(row->p, 1, P, options->MaxRays);
3286 if (!emptyQ(T)) {
3287 //printf("not all_pos: i: %d, l: %d, u: %d\n", i, l, u);
3288 info[i] = (constraint)(info[i] ^ ALL_POS);
3290 //puts("pos remainder");
3291 //Polyhedron_Print(stdout, P_VALUE_FMT, T);
3292 Polyhedron_Free(T);
3294 if (!(info[i] & ONE_NEG)) {
3295 if (lu_parallel) {
3296 negative_test_constraint(P->Constraint[l],
3297 P->Constraint[u],
3298 row->p, nvar+i, len, &f);
3299 oppose_constraint(row->p, len, &f);
3300 Polyhedron *T = AddConstraints(row->p, 1, P,
3301 options->MaxRays);
3302 if (emptyQ(T)) {
3303 //printf("one_neg i: %d, l: %d, u: %d\n", i, l, u);
3304 info[i] = (constraint)(info[i] | ONE_NEG);
3306 //puts("neg remainder");
3307 //Polyhedron_Print(stdout, P_VALUE_FMT, T);
3308 Polyhedron_Free(T);
3309 } else if (!(info[i] & ROT_NEG)) {
3310 if (parallel_constraints(P->Constraint[l],
3311 P->Constraint[u],
3312 row->p, nvar, exist)) {
3313 negative_test_constraint7(P->Constraint[l],
3314 P->Constraint[u],
3315 row->p, nvar, exist,
3316 len, &f);
3317 oppose_constraint(row->p, len, &f);
3318 Polyhedron *T = AddConstraints(row->p, 1, P,
3319 options->MaxRays);
3320 if (emptyQ(T)) {
3321 // printf("rot_neg i: %d, l: %d, u: %d\n", i, l, u);
3322 info[i] = (constraint)(info[i] | ROT_NEG);
3323 r = l;
3325 //puts("neg remainder");
3326 //Polyhedron_Print(stdout, P_VALUE_FMT, T);
3327 Polyhedron_Free(T);
3331 if (!(info[i] & ALL_POS) && (info[i] & (ONE_NEG | ROT_NEG)))
3332 goto next;
3335 if (info[i] & ALL_POS)
3336 break;
3337 next:
3342 for (int i = 0; i < exist; ++i)
3343 printf("%i: %i\n", i, info[i]);
3345 for (int i = 0; i < exist; ++i)
3346 if (info[i] & ALL_POS) {
3347 #ifdef DEBUG_ER
3348 fprintf(stderr, "\nER: Positive\n");
3349 #endif /* DEBUG_ER */
3350 // Eliminate
3351 // Maybe we should chew off some of the fat here
3352 Matrix *M = Matrix_Alloc(P->Dimension, P->Dimension+1);
3353 for (int j = 0; j < P->Dimension; ++j)
3354 value_set_si(M->p[j][j + (j >= i+nvar)], 1);
3355 Polyhedron *T = Polyhedron_Image(P, M, options->MaxRays);
3356 Matrix_Free(M);
3357 evalue *EP = barvinok_enumerate_e_with_options(T, exist-1, nparam,
3358 options);
3359 Polyhedron_Free(T);
3360 value_clear(f);
3361 Vector_Free(row);
3362 delete [] info;
3363 return EP;
3365 for (int i = 0; i < exist; ++i)
3366 if (info[i] & ONE_NEG) {
3367 #ifdef DEBUG_ER
3368 fprintf(stderr, "\nER: Negative\n");
3369 #endif /* DEBUG_ER */
3370 Vector_Free(row);
3371 value_clear(f);
3372 delete [] info;
3373 if (i == 0)
3374 return barvinok_enumerate_e_with_options(P, exist-1, nparam,
3375 options);
3376 else {
3377 Polyhedron *T = Polyhedron_Copy(P);
3378 SwapColumns(T, nvar+1, nvar+1+i);
3379 evalue *EP = barvinok_enumerate_e_with_options(T, exist-1, nparam,
3380 options);
3381 Polyhedron_Free(T);
3382 return EP;
3385 for (int i = 0; i < exist; ++i)
3386 if (info[i] & ROT_NEG) {
3387 #ifdef DEBUG_ER
3388 fprintf(stderr, "\nER: Rotate\n");
3389 #endif /* DEBUG_ER */
3390 Vector_Free(row);
3391 value_clear(f);
3392 delete [] info;
3393 Polyhedron *T = rotate_along(P, r, nvar, exist, options->MaxRays);
3394 evalue *EP = barvinok_enumerate_e_with_options(T, exist-1, nparam,
3395 options);
3396 Polyhedron_Free(T);
3397 return EP;
3399 for (int i = 0; i < exist; ++i)
3400 if (info[i] & INDEPENDENT) {
3401 Polyhedron *pos, *neg;
3403 /* Find constraint again and split off negative part */
3405 if (SplitOnVar(P, i, nvar, exist, options->MaxRays,
3406 row, f, true, &pos, &neg)) {
3407 #ifdef DEBUG_ER
3408 fprintf(stderr, "\nER: Split\n");
3409 #endif /* DEBUG_ER */
3411 evalue *EP =
3412 barvinok_enumerate_e_with_options(neg, exist-1, nparam, options);
3413 evalue *E =
3414 barvinok_enumerate_e_with_options(pos, exist, nparam, options);
3415 eadd(E, EP);
3416 free_evalue_refs(E);
3417 free(E);
3418 Polyhedron_Free(neg);
3419 Polyhedron_Free(pos);
3420 value_clear(f);
3421 Vector_Free(row);
3422 delete [] info;
3423 return EP;
3426 delete [] info;
3428 Polyhedron *O = P;
3429 Polyhedron *F;
3431 evalue *EP;
3433 EP = enumerate_line(P, exist, nparam, options);
3434 if (EP)
3435 goto out;
3437 EP = barvinok_enumerate_pip_with_options(P, exist, nparam, options);
3438 if (EP)
3439 goto out;
3441 EP = enumerate_redundant_ray(P, exist, nparam, options);
3442 if (EP)
3443 goto out;
3445 EP = enumerate_sure(P, exist, nparam, options);
3446 if (EP)
3447 goto out;
3449 EP = enumerate_ray(P, exist, nparam, options);
3450 if (EP)
3451 goto out;
3453 EP = enumerate_sure2(P, exist, nparam, options);
3454 if (EP)
3455 goto out;
3457 F = unfringe(P, options->MaxRays);
3458 if (!PolyhedronIncludes(F, P)) {
3459 #ifdef DEBUG_ER
3460 fprintf(stderr, "\nER: Fringed\n");
3461 #endif /* DEBUG_ER */
3462 EP = barvinok_enumerate_e_with_options(F, exist, nparam, options);
3463 Polyhedron_Free(F);
3464 goto out;
3466 Polyhedron_Free(F);
3468 if (nparam)
3469 EP = enumerate_vd(&P, exist, nparam, options);
3470 if (EP)
3471 goto out2;
3473 if (nvar != 0) {
3474 EP = enumerate_sum(P, exist, nparam, options);
3475 goto out2;
3478 assert(nvar == 0);
3480 int i;
3481 Polyhedron *pos, *neg;
3482 for (i = 0; i < exist; ++i)
3483 if (SplitOnVar(P, i, nvar, exist, options->MaxRays,
3484 row, f, false, &pos, &neg))
3485 break;
3487 assert (i < exist);
3489 pos->next = neg;
3490 EP = enumerate_or(pos, exist, nparam, options);
3492 out2:
3493 if (O != P)
3494 Polyhedron_Free(P);
3496 out:
3497 value_clear(f);
3498 Vector_Free(row);
3499 return EP;
3503 * remove equalities that require a "compression" of the parameters
3505 static Polyhedron *remove_more_equalities(Polyhedron *P, unsigned nparam,
3506 Matrix **CP, unsigned MaxRays)
3508 Polyhedron *Q = P;
3509 remove_all_equalities(&P, NULL, CP, NULL, nparam, MaxRays);
3510 if (P != Q)
3511 Polyhedron_Free(Q);
3512 return P;
3515 /* frees P */
3516 static gen_fun *series(Polyhedron *P, unsigned nparam, barvinok_options *options)
3518 Matrix *CP = NULL;
3519 gen_fun *gf;
3521 if (emptyQ2(P)) {
3522 Polyhedron_Free(P);
3523 return new gen_fun;
3526 assert(!Polyhedron_is_unbounded(P, nparam, options->MaxRays));
3527 assert(P->NbBid == 0);
3528 assert(Polyhedron_has_revlex_positive_rays(P, nparam));
3529 if (P->NbEq != 0)
3530 P = remove_more_equalities(P, nparam, &CP, options->MaxRays);
3531 assert(P->NbEq == 0);
3532 if (CP)
3533 nparam = CP->NbColumns-1;
3535 if (nparam == 0) {
3536 Value c;
3537 value_init(c);
3538 barvinok_count_with_options(P, &c, options);
3539 gf = new gen_fun(c);
3540 value_clear(c);
3541 } else {
3542 gf_base *red;
3543 red = gf_base::create(Polyhedron_Project(P, nparam),
3544 P->Dimension, nparam, options);
3545 POL_ENSURE_VERTICES(P);
3546 red->start_gf(P, options);
3547 gf = red->gf;
3548 delete red;
3550 if (CP) {
3551 gf->substitute(CP);
3552 Matrix_Free(CP);
3554 Polyhedron_Free(P);
3555 return gf;
3558 gen_fun * barvinok_series_with_options(Polyhedron *P, Polyhedron* C,
3559 barvinok_options *options)
3561 Polyhedron *CA;
3562 unsigned nparam = C->Dimension;
3563 gen_fun *gf;
3565 CA = align_context(C, P->Dimension, options->MaxRays);
3566 P = DomainIntersection(P, CA, options->MaxRays);
3567 Polyhedron_Free(CA);
3569 gf = series(P, nparam, options);
3571 return gf;
3574 gen_fun * barvinok_series(Polyhedron *P, Polyhedron* C, unsigned MaxRays)
3576 gen_fun *gf;
3577 barvinok_options *options = barvinok_options_new_with_defaults();
3578 options->MaxRays = MaxRays;
3579 gf = barvinok_series_with_options(P, C, options);
3580 barvinok_options_free(options);
3581 return gf;
3584 static Polyhedron *skew_into_positive_orthant(Polyhedron *D, unsigned nparam,
3585 unsigned MaxRays)
3587 Matrix *M = NULL;
3588 Value tmp;
3589 value_init(tmp);
3590 for (Polyhedron *P = D; P; P = P->next) {
3591 POL_ENSURE_VERTICES(P);
3592 assert(!Polyhedron_is_unbounded(P, nparam, MaxRays));
3593 assert(P->NbBid == 0);
3594 assert(Polyhedron_has_positive_rays(P, nparam));
3596 for (int r = 0; r < P->NbRays; ++r) {
3597 if (value_notzero_p(P->Ray[r][P->Dimension+1]))
3598 continue;
3599 for (int i = 0; i < nparam; ++i) {
3600 int j;
3601 if (value_posz_p(P->Ray[r][i+1]))
3602 continue;
3603 if (!M) {
3604 M = Matrix_Alloc(D->Dimension+1, D->Dimension+1);
3605 for (int i = 0; i < D->Dimension+1; ++i)
3606 value_set_si(M->p[i][i], 1);
3607 } else {
3608 Inner_Product(P->Ray[r]+1, M->p[i], D->Dimension+1, &tmp);
3609 if (value_posz_p(tmp))
3610 continue;
3612 for (j = P->Dimension - nparam; j < P->Dimension; ++j)
3613 if (value_pos_p(P->Ray[r][j+1]))
3614 break;
3615 assert(j < P->Dimension);
3616 value_pdivision(tmp, P->Ray[r][j+1], P->Ray[r][i+1]);
3617 value_subtract(M->p[i][j], M->p[i][j], tmp);
3621 value_clear(tmp);
3622 if (M) {
3623 D = DomainImage(D, M, MaxRays);
3624 Matrix_Free(M);
3626 return D;
3629 gen_fun* barvinok_enumerate_union_series_with_options(Polyhedron *D, Polyhedron* C,
3630 barvinok_options *options)
3632 Polyhedron *conv, *D2;
3633 Polyhedron *CA;
3634 gen_fun *gf = NULL, *gf2;
3635 unsigned nparam = C->Dimension;
3636 ZZ one, mone;
3637 one = 1;
3638 mone = -1;
3640 CA = align_context(C, D->Dimension, options->MaxRays);
3641 D = DomainIntersection(D, CA, options->MaxRays);
3642 Polyhedron_Free(CA);
3644 D2 = skew_into_positive_orthant(D, nparam, options->MaxRays);
3645 for (Polyhedron *P = D2; P; P = P->next) {
3646 assert(P->Dimension == D2->Dimension);
3647 gen_fun *P_gf;
3649 P_gf = series(Polyhedron_Copy(P), nparam, options);
3650 if (!gf)
3651 gf = P_gf;
3652 else {
3653 gf->add_union(P_gf, options);
3654 delete P_gf;
3657 /* we actually only need the convex union of the parameter space
3658 * but the reducer classes currently expect a polyhedron in
3659 * the combined space
3661 Polyhedron_Free(gf->context);
3662 gf->context = DomainConvex(D2, options->MaxRays);
3664 gf2 = gf->summate(D2->Dimension - nparam, options);
3666 delete gf;
3667 if (D != D2)
3668 Domain_Free(D2);
3669 Domain_Free(D);
3670 return gf2;
3673 gen_fun* barvinok_enumerate_union_series(Polyhedron *D, Polyhedron* C,
3674 unsigned MaxRays)
3676 gen_fun *gf;
3677 barvinok_options *options = barvinok_options_new_with_defaults();
3678 options->MaxRays = MaxRays;
3679 gf = barvinok_enumerate_union_series_with_options(D, C, options);
3680 barvinok_options_free(options);
3681 return gf;
3684 evalue* barvinok_enumerate_union(Polyhedron *D, Polyhedron* C, unsigned MaxRays)
3686 evalue *EP;
3687 gen_fun *gf = barvinok_enumerate_union_series(D, C, MaxRays);
3688 EP = *gf;
3689 delete gf;
3690 return EP;