doc: integer points in the fundamental parallelepiped of simple cone
[barvinok.git] / barvinok.cc
blob4498826f4ac6146b0ad244c5618c23a9de51247b
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"
27 #ifndef HAVE_PARAM_POLYHEDRON_SCALE_INTEGER
28 extern "C" void Param_Polyhedron_Scale_Integer(Param_Polyhedron *PP, Polyhedron **P,
29 Value *det, unsigned MaxRays);
30 #endif
32 #ifdef NTL_STD_CXX
33 using namespace NTL;
34 #endif
35 using std::cerr;
36 using std::cout;
37 using std::endl;
38 using std::vector;
39 using std::deque;
40 using std::string;
41 using std::ostringstream;
43 #define ALLOC(t,p) p = (t*)malloc(sizeof(*p))
45 class dpoly_n {
46 public:
47 Matrix *coeff;
48 ~dpoly_n() {
49 Matrix_Free(coeff);
51 dpoly_n(int d, ZZ& degree_0, ZZ& degree_1, int offset = 0) {
52 Value d0, d1;
53 value_init(d0);
54 value_init(d1);
55 zz2value(degree_0, d0);
56 zz2value(degree_1, d1);
57 coeff = Matrix_Alloc(d+1, d+1+1);
58 value_set_si(coeff->p[0][0], 1);
59 value_set_si(coeff->p[0][d+1], 1);
60 for (int i = 1; i <= d; ++i) {
61 value_multiply(coeff->p[i][0], coeff->p[i-1][0], d0);
62 Vector_Combine(coeff->p[i-1], coeff->p[i-1]+1, coeff->p[i]+1,
63 d1, d0, i);
64 value_set_si(coeff->p[i][d+1], i);
65 value_multiply(coeff->p[i][d+1], coeff->p[i][d+1], coeff->p[i-1][d+1]);
66 value_decrement(d0, d0);
68 value_clear(d0);
69 value_clear(d1);
71 void div(dpoly& d, Vector *count, ZZ& sign) {
72 int len = coeff->NbRows;
73 Matrix * c = Matrix_Alloc(coeff->NbRows, coeff->NbColumns);
74 Value tmp;
75 value_init(tmp);
76 for (int i = 0; i < len; ++i) {
77 Vector_Copy(coeff->p[i], c->p[i], len+1);
78 for (int j = 1; j <= i; ++j) {
79 zz2value(d.coeff[j], tmp);
80 value_multiply(tmp, tmp, c->p[i][len]);
81 value_oppose(tmp, tmp);
82 Vector_Combine(c->p[i], c->p[i-j], c->p[i],
83 c->p[i-j][len], tmp, len);
84 value_multiply(c->p[i][len], c->p[i][len], c->p[i-j][len]);
86 zz2value(d.coeff[0], tmp);
87 value_multiply(c->p[i][len], c->p[i][len], tmp);
89 if (sign == -1) {
90 value_set_si(tmp, -1);
91 Vector_Scale(c->p[len-1], count->p, tmp, len);
92 value_assign(count->p[len], c->p[len-1][len]);
93 } else
94 Vector_Copy(c->p[len-1], count->p, len+1);
95 Vector_Normalize(count->p, len+1);
96 value_clear(tmp);
97 Matrix_Free(c);
101 const int MAX_TRY=10;
103 * Searches for a vector that is not orthogonal to any
104 * of the rays in rays.
106 static void nonorthog(mat_ZZ& rays, vec_ZZ& lambda)
108 int dim = rays.NumCols();
109 bool found = false;
110 lambda.SetLength(dim);
111 if (dim == 0)
112 return;
114 for (int i = 2; !found && i <= 50*dim; i+=4) {
115 for (int j = 0; j < MAX_TRY; ++j) {
116 for (int k = 0; k < dim; ++k) {
117 int r = random_int(i)+2;
118 int v = (2*(r%2)-1) * (r >> 1);
119 lambda[k] = v;
121 int k = 0;
122 for (; k < rays.NumRows(); ++k)
123 if (lambda * rays[k] == 0)
124 break;
125 if (k == rays.NumRows()) {
126 found = true;
127 break;
131 assert(found);
134 static void add_rays(mat_ZZ& rays, Polyhedron *i, int *r, int nvar = -1,
135 bool all = false)
137 unsigned dim = i->Dimension;
138 if (nvar == -1)
139 nvar = dim;
140 for (int k = 0; k < i->NbRays; ++k) {
141 if (!value_zero_p(i->Ray[k][dim+1]))
142 continue;
143 if (!all && nvar != dim && First_Non_Zero(i->Ray[k]+1, nvar) == -1)
144 continue;
145 values2zz(i->Ray[k]+1, rays[(*r)++], nvar);
149 static void mask_r(Matrix *f, int nr, Vector *lcm, int p, Vector *val, evalue *ev)
151 unsigned nparam = lcm->Size;
153 if (p == nparam) {
154 Vector * prod = Vector_Alloc(f->NbRows);
155 Matrix_Vector_Product(f, val->p, prod->p);
156 int isint = 1;
157 for (int i = 0; i < nr; ++i) {
158 value_modulus(prod->p[i], prod->p[i], f->p[i][nparam+1]);
159 isint &= value_zero_p(prod->p[i]);
161 value_set_si(ev->d, 1);
162 value_init(ev->x.n);
163 value_set_si(ev->x.n, isint);
164 Vector_Free(prod);
165 return;
168 Value tmp;
169 value_init(tmp);
170 if (value_one_p(lcm->p[p]))
171 mask_r(f, nr, lcm, p+1, val, ev);
172 else {
173 value_assign(tmp, lcm->p[p]);
174 value_set_si(ev->d, 0);
175 ev->x.p = new_enode(periodic, VALUE_TO_INT(tmp), p+1);
176 do {
177 value_decrement(tmp, tmp);
178 value_assign(val->p[p], tmp);
179 mask_r(f, nr, lcm, p+1, val, &ev->x.p->arr[VALUE_TO_INT(tmp)]);
180 } while (value_pos_p(tmp));
182 value_clear(tmp);
185 static void mask_fractional(Matrix *f, evalue *factor)
187 int nr = f->NbRows, nc = f->NbColumns;
188 int n;
189 bool found = false;
190 for (n = 0; n < nr && value_notzero_p(f->p[n][nc-1]); ++n)
191 if (value_notone_p(f->p[n][nc-1]) &&
192 value_notmone_p(f->p[n][nc-1]))
193 found = true;
194 if (!found)
195 return;
197 evalue EP;
198 nr = n;
200 Value m;
201 value_init(m);
203 evalue EV;
204 value_init(EV.d);
205 value_init(EV.x.n);
206 value_set_si(EV.x.n, 1);
208 for (n = 0; n < nr; ++n) {
209 value_assign(m, f->p[n][nc-1]);
210 if (value_one_p(m) || value_mone_p(m))
211 continue;
213 int j = normal_mod(f->p[n], nc-1, &m);
214 if (j == nc-1) {
215 free_evalue_refs(factor);
216 value_init(factor->d);
217 evalue_set_si(factor, 0, 1);
218 break;
220 vec_ZZ row;
221 values2zz(f->p[n], row, nc-1);
222 ZZ g;
223 value2zz(m, g);
224 if (j < (nc-1)-1 && row[j] > g/2) {
225 for (int k = j; k < (nc-1); ++k)
226 if (row[k] != 0)
227 row[k] = g - row[k];
230 value_init(EP.d);
231 value_set_si(EP.d, 0);
232 EP.x.p = new_enode(relation, 2, 0);
233 value_clear(EP.x.p->arr[1].d);
234 EP.x.p->arr[1] = *factor;
235 evalue *ev = &EP.x.p->arr[0];
236 value_set_si(ev->d, 0);
237 ev->x.p = new_enode(fractional, 3, -1);
238 evalue_set_si(&ev->x.p->arr[1], 0, 1);
239 evalue_set_si(&ev->x.p->arr[2], 1, 1);
240 evalue *E = multi_monom(row);
241 value_assign(EV.d, m);
242 emul(&EV, E);
243 value_clear(ev->x.p->arr[0].d);
244 ev->x.p->arr[0] = *E;
245 delete E;
246 *factor = EP;
249 value_clear(m);
250 free_evalue_refs(&EV);
256 static void mask_table(Matrix *f, evalue *factor)
258 int nr = f->NbRows, nc = f->NbColumns;
259 int n;
260 bool found = false;
261 for (n = 0; n < nr && value_notzero_p(f->p[n][nc-1]); ++n)
262 if (value_notone_p(f->p[n][nc-1]) &&
263 value_notmone_p(f->p[n][nc-1]))
264 found = true;
265 if (!found)
266 return;
268 Value tmp;
269 value_init(tmp);
270 nr = n;
271 unsigned np = nc - 2;
272 Vector *lcm = Vector_Alloc(np);
273 Vector *val = Vector_Alloc(nc);
274 Vector_Set(val->p, 0, nc);
275 value_set_si(val->p[np], 1);
276 Vector_Set(lcm->p, 1, np);
277 for (n = 0; n < nr; ++n) {
278 if (value_one_p(f->p[n][nc-1]) ||
279 value_mone_p(f->p[n][nc-1]))
280 continue;
281 for (int j = 0; j < np; ++j)
282 if (value_notzero_p(f->p[n][j])) {
283 Gcd(f->p[n][j], f->p[n][nc-1], &tmp);
284 value_division(tmp, f->p[n][nc-1], tmp);
285 value_lcm(tmp, lcm->p[j], &lcm->p[j]);
288 evalue EP;
289 value_init(EP.d);
290 mask_r(f, nr, lcm, 0, val, &EP);
291 value_clear(tmp);
292 Vector_Free(val);
293 Vector_Free(lcm);
294 emul(&EP,factor);
295 free_evalue_refs(&EP);
298 static void mask(Matrix *f, evalue *factor, barvinok_options *options)
300 if (options->lookup_table)
301 mask_table(f, factor);
302 else
303 mask_fractional(f, factor);
306 /* This structure encodes the power of the term in a rational generating function.
308 * Either E == NULL or constant = 0
309 * If E != NULL, then the power is E
310 * If E == NULL, then the power is coeff * param[pos] + constant
312 struct term_info {
313 evalue *E;
314 ZZ constant;
315 ZZ coeff;
316 int pos;
319 /* Returns the power of (t+1) in the term of a rational generating function,
320 * i.e., the scalar product of the actual lattice point and lambda.
321 * The lattice point is the unique lattice point in the fundamental parallelepiped
322 * of the unimodual cone i shifted to the parametric vertex V.
324 * PD is the parameter domain, which, if != NULL, may be used to simply the
325 * resulting expression.
327 * The result is returned in term.
329 void lattice_point(Param_Vertices* V, const mat_ZZ& rays, vec_ZZ& lambda,
330 term_info* term, Polyhedron *PD, barvinok_options *options)
332 unsigned nparam = V->Vertex->NbColumns - 2;
333 unsigned dim = rays.NumCols();
334 mat_ZZ vertex;
335 vertex.SetDims(V->Vertex->NbRows, nparam+1);
336 Value lcm, tmp;
337 value_init(lcm);
338 value_init(tmp);
339 value_set_si(lcm, 1);
340 for (int j = 0; j < V->Vertex->NbRows; ++j) {
341 value_lcm(lcm, V->Vertex->p[j][nparam+1], &lcm);
343 if (value_notone_p(lcm)) {
344 Matrix * mv = Matrix_Alloc(dim, nparam+1);
345 for (int j = 0 ; j < dim; ++j) {
346 value_division(tmp, lcm, V->Vertex->p[j][nparam+1]);
347 Vector_Scale(V->Vertex->p[j], mv->p[j], tmp, nparam+1);
350 term->E = lattice_point(rays, lambda, mv, lcm, PD, options);
351 term->constant = 0;
353 Matrix_Free(mv);
354 value_clear(lcm);
355 value_clear(tmp);
356 return;
358 for (int i = 0; i < V->Vertex->NbRows; ++i) {
359 assert(value_one_p(V->Vertex->p[i][nparam+1])); // for now
360 values2zz(V->Vertex->p[i], vertex[i], nparam+1);
363 vec_ZZ num;
364 num = lambda * vertex;
366 int p = -1;
367 int nn = 0;
368 for (int j = 0; j < nparam; ++j)
369 if (num[j] != 0) {
370 ++nn;
371 p = j;
373 if (nn >= 2) {
374 term->E = multi_monom(num);
375 term->constant = 0;
376 } else {
377 term->E = NULL;
378 term->constant = num[nparam];
379 term->pos = p;
380 if (p != -1)
381 term->coeff = num[p];
384 value_clear(lcm);
385 value_clear(tmp);
389 struct counter : public np_base {
390 vec_ZZ lambda;
391 mat_ZZ vertex;
392 vec_ZZ den;
393 ZZ sign;
394 vec_ZZ num;
395 ZZ offset;
396 int j;
397 mpq_t count;
399 counter(unsigned dim) : np_base(dim) {
400 den.SetLength(dim);
401 mpq_init(count);
404 virtual void init(Polyhedron *P) {
405 randomvector(P, lambda, dim);
408 virtual void reset() {
409 mpq_set_si(count, 0, 0);
412 ~counter() {
413 mpq_clear(count);
416 virtual void handle(const mat_ZZ& rays, Value *vertex, const QQ& c,
417 unsigned long det, int *closed, barvinok_options *options);
418 virtual void get_count(Value *result) {
419 assert(value_one_p(&count[0]._mp_den));
420 value_assign(*result, &count[0]._mp_num);
424 void counter::handle(const mat_ZZ& rays, Value *V, const QQ& c, unsigned long det,
425 int *closed, barvinok_options *options)
427 for (int k = 0; k < dim; ++k) {
428 if (lambda * rays[k] == 0)
429 throw Orthogonal;
432 assert(c.d == 1);
433 assert(c.n == 1 || c.n == -1);
434 sign = c.n;
436 lattice_point(V, rays, vertex, det, closed);
437 num = vertex * lambda;
438 den = rays * lambda;
439 offset = 0;
440 normalize(sign, offset, den);
442 num[0] += offset;
443 dpoly d(dim, num[0]);
444 for (int k = 1; k < num.length(); ++k) {
445 num[k] += offset;
446 dpoly term(dim, num[k]);
447 d += term;
449 dpoly n(dim, den[0], 1);
450 for (int k = 1; k < dim; ++k) {
451 dpoly fact(dim, den[k], 1);
452 n *= fact;
454 d.div(n, count, sign);
457 struct bfe_term : public bfc_term_base {
458 vector<evalue *> factors;
460 bfe_term(int len) : bfc_term_base(len) {
463 ~bfe_term() {
464 for (int i = 0; i < factors.size(); ++i) {
465 if (!factors[i])
466 continue;
467 free_evalue_refs(factors[i]);
468 delete factors[i];
473 static void print_int_vector(int *v, int len, char *name)
475 cerr << name << endl;
476 for (int j = 0; j < len; ++j) {
477 cerr << v[j] << " ";
479 cerr << endl;
482 static void print_bfc_terms(mat_ZZ& factors, bfc_vec& v)
484 cerr << endl;
485 cerr << "factors" << endl;
486 cerr << factors << endl;
487 for (int i = 0; i < v.size(); ++i) {
488 cerr << "term: " << i << endl;
489 print_int_vector(v[i]->powers, factors.NumRows(), "powers");
490 cerr << "terms" << endl;
491 cerr << v[i]->terms << endl;
492 bfc_term* bfct = static_cast<bfc_term *>(v[i]);
493 cerr << bfct->c << endl;
497 static void print_bfe_terms(mat_ZZ& factors, bfc_vec& v)
499 cerr << endl;
500 cerr << "factors" << endl;
501 cerr << factors << endl;
502 for (int i = 0; i < v.size(); ++i) {
503 cerr << "term: " << i << endl;
504 print_int_vector(v[i]->powers, factors.NumRows(), "powers");
505 cerr << "terms" << endl;
506 cerr << v[i]->terms << endl;
507 bfe_term* bfet = static_cast<bfe_term *>(v[i]);
508 for (int j = 0; j < v[i]->terms.NumRows(); ++j) {
509 char * test[] = {"a", "b"};
510 print_evalue(stderr, bfet->factors[j], test);
511 fprintf(stderr, "\n");
516 struct bfcounter : public bfcounter_base {
517 mpq_t count;
519 bfcounter(unsigned dim) : bfcounter_base(dim) {
520 mpq_init(count);
521 lower = 1;
523 ~bfcounter() {
524 mpq_clear(count);
526 virtual void base(mat_ZZ& factors, bfc_vec& v);
527 virtual void get_count(Value *result) {
528 assert(value_one_p(&count[0]._mp_den));
529 value_assign(*result, &count[0]._mp_num);
533 void bfcounter::base(mat_ZZ& factors, bfc_vec& v)
535 unsigned nf = factors.NumRows();
537 for (int i = 0; i < v.size(); ++i) {
538 bfc_term* bfct = static_cast<bfc_term *>(v[i]);
539 int total_power = 0;
540 // factor is always positive, so we always
541 // change signs
542 for (int k = 0; k < nf; ++k)
543 total_power += v[i]->powers[k];
545 int j;
546 for (j = 0; j < nf; ++j)
547 if (v[i]->powers[j] > 0)
548 break;
550 dpoly D(total_power, factors[j][0], 1);
551 for (int k = 1; k < v[i]->powers[j]; ++k) {
552 dpoly fact(total_power, factors[j][0], 1);
553 D *= fact;
555 for ( ; ++j < nf; )
556 for (int k = 0; k < v[i]->powers[j]; ++k) {
557 dpoly fact(total_power, factors[j][0], 1);
558 D *= fact;
561 for (int k = 0; k < v[i]->terms.NumRows(); ++k) {
562 dpoly n(total_power, v[i]->terms[k][0]);
563 mpq_set_si(tcount, 0, 1);
564 n.div(D, tcount, one);
565 if (total_power % 2)
566 bfct->c[k].n = -bfct->c[k].n;
567 zz2value(bfct->c[k].n, tn);
568 zz2value(bfct->c[k].d, td);
570 mpz_mul(mpq_numref(tcount), mpq_numref(tcount), tn);
571 mpz_mul(mpq_denref(tcount), mpq_denref(tcount), td);
572 mpq_canonicalize(tcount);
573 mpq_add(count, count, tcount);
575 delete v[i];
580 /* Check whether the polyhedron is unbounded and if so,
581 * check whether it has any (and therefore an infinite number of)
582 * integer points.
583 * If one of the vertices is integer, then we are done.
584 * Otherwise, transform the polyhedron such that one of the rays
585 * is the first unit vector and cut it off at a height that ensures
586 * that if the whole polyhedron has any points, then the remaining part
587 * has integer points. In particular we add the largest coefficient
588 * of a ray to the highest vertex (rounded up).
590 static bool Polyhedron_is_infinite(Polyhedron *P, Value* result,
591 barvinok_options *options)
593 int r = 0;
594 Matrix *M, *M2;
595 Value c, tmp;
596 Value g;
597 bool first;
598 Vector *v;
599 Value offset, size;
600 Polyhedron *R;
602 if (P->NbBid == 0)
603 for (; r < P->NbRays; ++r)
604 if (value_zero_p(P->Ray[r][P->Dimension+1]))
605 break;
606 if (P->NbBid == 0 && r == P->NbRays)
607 return false;
609 if (options->count_sample_infinite) {
610 Vector *sample;
612 sample = Polyhedron_Sample(P, options);
613 if (!sample)
614 value_set_si(*result, 0);
615 else {
616 value_set_si(*result, -1);
617 Vector_Free(sample);
619 return true;
622 for (int i = 0; i < P->NbRays; ++i)
623 if (value_one_p(P->Ray[i][1+P->Dimension])) {
624 value_set_si(*result, -1);
625 return true;
628 value_init(g);
629 v = Vector_Alloc(P->Dimension+1);
630 Vector_Gcd(P->Ray[r]+1, P->Dimension, &g);
631 Vector_AntiScale(P->Ray[r]+1, v->p, g, P->Dimension+1);
632 M = unimodular_complete(v);
633 value_set_si(M->p[P->Dimension][P->Dimension], 1);
634 M2 = Transpose(M);
635 Matrix_Free(M);
636 P = Polyhedron_Preimage(P, M2, 0);
637 Matrix_Free(M2);
638 value_clear(g);
639 Vector_Free(v);
641 first = true;
642 value_init(offset);
643 value_init(size);
644 value_init(tmp);
645 value_set_si(size, 0);
647 for (int i = 0; i < P->NbBid; ++i) {
648 value_absolute(tmp, P->Ray[i][1]);
649 if (value_gt(tmp, size))
650 value_assign(size, tmp);
652 for (int i = P->NbBid; i < P->NbRays; ++i) {
653 if (value_zero_p(P->Ray[i][P->Dimension+1])) {
654 if (value_gt(P->Ray[i][1], size))
655 value_assign(size, P->Ray[i][1]);
656 continue;
658 mpz_cdiv_q(tmp, P->Ray[i][1], P->Ray[i][P->Dimension+1]);
659 if (first || value_gt(tmp, offset)) {
660 value_assign(offset, tmp);
661 first = false;
664 value_addto(offset, offset, size);
665 value_clear(size);
666 value_clear(tmp);
668 v = Vector_Alloc(P->Dimension+2);
669 value_set_si(v->p[0], 1);
670 value_set_si(v->p[1], -1);
671 value_assign(v->p[1+P->Dimension], offset);
672 R = AddConstraints(v->p, 1, P, options->MaxRays);
673 Polyhedron_Free(P);
674 P = R;
676 value_clear(offset);
677 Vector_Free(v);
679 value_init(c);
680 barvinok_count_with_options(P, &c, options);
681 Polyhedron_Free(P);
682 if (value_zero_p(c))
683 value_set_si(*result, 0);
684 else
685 value_set_si(*result, -1);
686 value_clear(c);
688 return true;
691 typedef Polyhedron * Polyhedron_p;
693 static void barvinok_count_f(Polyhedron *P, Value* result,
694 barvinok_options *options);
696 void barvinok_count_with_options(Polyhedron *P, Value* result,
697 struct barvinok_options *options)
699 unsigned dim;
700 int allocated = 0;
701 Polyhedron *Q;
702 bool infinite = false;
704 if (emptyQ2(P)) {
705 value_set_si(*result, 0);
706 return;
708 if (P->NbEq != 0) {
709 Q = NULL;
710 do {
711 P = remove_equalities(P);
712 P = DomainConstraintSimplify(P, options->MaxRays);
713 if (Q)
714 Polyhedron_Free(Q);
715 Q = P;
716 } while (!emptyQ(P) && P->NbEq != 0);
717 if (emptyQ(P)) {
718 Polyhedron_Free(P);
719 value_set_si(*result, 0);
720 return;
722 allocated = 1;
724 if (Polyhedron_is_infinite(P, result, options)) {
725 if (allocated)
726 Polyhedron_Free(P);
727 return;
729 if (P->Dimension == 0) {
730 /* Test whether the constraints are satisfied */
731 POL_ENSURE_VERTICES(P);
732 value_set_si(*result, !emptyQ(P));
733 if (allocated)
734 Polyhedron_Free(P);
735 return;
737 Q = Polyhedron_Factor(P, 0, options->MaxRays);
738 if (Q) {
739 if (allocated)
740 Polyhedron_Free(P);
741 P = Q;
742 allocated = 1;
745 barvinok_count_f(P, result, options);
746 if (value_neg_p(*result))
747 infinite = true;
748 if (Q && P->next && value_notzero_p(*result)) {
749 Value factor;
750 value_init(factor);
752 for (Q = P->next; Q; Q = Q->next) {
753 barvinok_count_f(Q, &factor, options);
754 if (value_neg_p(factor)) {
755 infinite = true;
756 continue;
757 } else if (Q->next && value_zero_p(factor)) {
758 value_set_si(*result, 0);
759 break;
761 value_multiply(*result, *result, factor);
764 value_clear(factor);
767 if (allocated)
768 Domain_Free(P);
769 if (infinite)
770 value_set_si(*result, -1);
773 void barvinok_count(Polyhedron *P, Value* result, unsigned NbMaxCons)
775 barvinok_options *options = barvinok_options_new_with_defaults();
776 options->MaxRays = NbMaxCons;
777 barvinok_count_with_options(P, result, options);
778 barvinok_options_free(options);
781 static void barvinok_count_f(Polyhedron *P, Value* result,
782 barvinok_options *options)
784 if (emptyQ2(P)) {
785 value_set_si(*result, 0);
786 return;
789 if (P->Dimension == 1)
790 return Line_Length(P, result);
792 int c = P->NbConstraints;
793 POL_ENSURE_FACETS(P);
794 if (c != P->NbConstraints || P->NbEq != 0)
795 return barvinok_count_with_options(P, result, options);
797 POL_ENSURE_VERTICES(P);
799 if (Polyhedron_is_infinite(P, result, options))
800 return;
802 np_base *cnt;
803 if (options->incremental_specialization == 2)
804 cnt = new bfcounter(P->Dimension);
805 else if (options->incremental_specialization == 1)
806 cnt = new icounter(P->Dimension);
807 else
808 cnt = new counter(P->Dimension);
809 cnt->start(P, options);
811 cnt->get_count(result);
812 delete cnt;
815 static void uni_polynom(int param, Vector *c, evalue *EP)
817 unsigned dim = c->Size-2;
818 value_init(EP->d);
819 value_set_si(EP->d,0);
820 EP->x.p = new_enode(polynomial, dim+1, param+1);
821 for (int j = 0; j <= dim; ++j)
822 evalue_set(&EP->x.p->arr[j], c->p[j], c->p[dim+1]);
825 static void multi_polynom(Vector *c, evalue* X, evalue *EP)
827 unsigned dim = c->Size-2;
828 evalue EC;
830 value_init(EC.d);
831 evalue_set(&EC, c->p[dim], c->p[dim+1]);
833 value_init(EP->d);
834 evalue_set(EP, c->p[dim], c->p[dim+1]);
836 for (int i = dim-1; i >= 0; --i) {
837 emul(X, EP);
838 value_assign(EC.x.n, c->p[i]);
839 eadd(&EC, EP);
841 free_evalue_refs(&EC);
844 Polyhedron *unfringe (Polyhedron *P, unsigned MaxRays)
846 int len = P->Dimension+2;
847 Polyhedron *T, *R = P;
848 Value g;
849 value_init(g);
850 Vector *row = Vector_Alloc(len);
851 value_set_si(row->p[0], 1);
853 R = DomainConstraintSimplify(Polyhedron_Copy(P), MaxRays);
855 Matrix *M = Matrix_Alloc(2, len-1);
856 value_set_si(M->p[1][len-2], 1);
857 for (int v = 0; v < P->Dimension; ++v) {
858 value_set_si(M->p[0][v], 1);
859 Polyhedron *I = Polyhedron_Image(R, M, 2+1);
860 value_set_si(M->p[0][v], 0);
861 for (int r = 0; r < I->NbConstraints; ++r) {
862 if (value_zero_p(I->Constraint[r][0]))
863 continue;
864 if (value_zero_p(I->Constraint[r][1]))
865 continue;
866 if (value_one_p(I->Constraint[r][1]))
867 continue;
868 if (value_mone_p(I->Constraint[r][1]))
869 continue;
870 value_absolute(g, I->Constraint[r][1]);
871 Vector_Set(row->p+1, 0, len-2);
872 value_division(row->p[1+v], I->Constraint[r][1], g);
873 mpz_fdiv_q(row->p[len-1], I->Constraint[r][2], g);
874 T = R;
875 R = AddConstraints(row->p, 1, R, MaxRays);
876 if (T != P)
877 Polyhedron_Free(T);
879 Polyhedron_Free(I);
881 Matrix_Free(M);
882 Vector_Free(row);
883 value_clear(g);
884 return R;
887 /* this procedure may have false negatives */
888 static bool Polyhedron_is_infinite_param(Polyhedron *P, unsigned nparam)
890 int r;
891 for (r = 0; r < P->NbRays; ++r) {
892 if (!value_zero_p(P->Ray[r][0]) &&
893 !value_zero_p(P->Ray[r][P->Dimension+1]))
894 continue;
895 if (First_Non_Zero(P->Ray[r]+1+P->Dimension-nparam, nparam) == -1)
896 return true;
898 return false;
901 /* Check whether all rays point in the positive directions
902 * for the parameters
904 static bool Polyhedron_has_positive_rays(Polyhedron *P, unsigned nparam)
906 int r;
907 for (r = 0; r < P->NbRays; ++r)
908 if (value_zero_p(P->Ray[r][P->Dimension+1])) {
909 int i;
910 for (i = P->Dimension - nparam; i < P->Dimension; ++i)
911 if (value_neg_p(P->Ray[r][i+1]))
912 return false;
914 return true;
917 typedef evalue * evalue_p;
919 struct enumerator_base {
920 unsigned dim;
921 evalue ** vE;
922 evalue mone;
923 vertex_decomposer *vpd;
925 enumerator_base(unsigned dim, vertex_decomposer *vpd)
927 this->dim = dim;
928 this->vpd = vpd;
930 vE = new evalue_p[vpd->nbV];
931 for (int j = 0; j < vpd->nbV; ++j)
932 vE[j] = 0;
934 value_init(mone.d);
935 evalue_set_si(&mone, -1, 1);
938 void decompose_at(Param_Vertices *V, int _i, barvinok_options *options) {
939 //this->pVD = pVD;
941 vE[_i] = new evalue;
942 value_init(vE[_i]->d);
943 evalue_set_si(vE[_i], 0, 1);
945 vpd->decompose_at_vertex(V, _i, options);
948 virtual ~enumerator_base() {
949 for (int j = 0; j < vpd->nbV; ++j)
950 if (vE[j]) {
951 free_evalue_refs(vE[j]);
952 delete vE[j];
954 delete [] vE;
956 free_evalue_refs(&mone);
959 static enumerator_base *create(Polyhedron *P, unsigned dim, unsigned nbV,
960 barvinok_options *options);
963 struct enumerator : public signed_cone_consumer, public vertex_decomposer,
964 public enumerator_base {
965 vec_ZZ lambda;
966 vec_ZZ den;
967 ZZ sign;
968 term_info num;
969 Vector *c;
970 mpq_t count;
972 enumerator(Polyhedron *P, unsigned dim, unsigned nbV) :
973 vertex_decomposer(P, nbV, *this), enumerator_base(dim, this) {
974 this->P = P;
975 this->nbV = nbV;
976 randomvector(P, lambda, dim);
977 den.SetLength(dim);
978 c = Vector_Alloc(dim+2);
980 mpq_init(count);
983 ~enumerator() {
984 mpq_clear(count);
985 Vector_Free(c);
988 virtual void handle(const signed_cone& sc, barvinok_options *options);
991 void enumerator::handle(const signed_cone& sc, barvinok_options *options)
993 assert(sc.det == 1);
994 assert(!sc.closed);
995 int r = 0;
996 assert(sc.rays.NumRows() == dim);
997 for (int k = 0; k < dim; ++k) {
998 if (lambda * sc.rays[k] == 0)
999 throw Orthogonal;
1002 sign = sc.sign;
1004 lattice_point(V, sc.rays, lambda, &num, 0, options);
1005 den = sc.rays * lambda;
1006 normalize(sign, num.constant, den);
1008 dpoly n(dim, den[0], 1);
1009 for (int k = 1; k < dim; ++k) {
1010 dpoly fact(dim, den[k], 1);
1011 n *= fact;
1013 if (num.E != NULL) {
1014 ZZ one(INIT_VAL, 1);
1015 dpoly_n d(dim, num.constant, one);
1016 d.div(n, c, sign);
1017 evalue EV;
1018 multi_polynom(c, num.E, &EV);
1019 eadd(&EV , vE[vert]);
1020 free_evalue_refs(&EV);
1021 free_evalue_refs(num.E);
1022 delete num.E;
1023 } else if (num.pos != -1) {
1024 dpoly_n d(dim, num.constant, num.coeff);
1025 d.div(n, c, sign);
1026 evalue EV;
1027 uni_polynom(num.pos, c, &EV);
1028 eadd(&EV , vE[vert]);
1029 free_evalue_refs(&EV);
1030 } else {
1031 mpq_set_si(count, 0, 1);
1032 dpoly d(dim, num.constant);
1033 d.div(n, count, sign);
1034 evalue EV;
1035 value_init(EV.d);
1036 evalue_set(&EV, &count[0]._mp_num, &count[0]._mp_den);
1037 eadd(&EV , vE[vert]);
1038 free_evalue_refs(&EV);
1042 struct ienumerator_base : enumerator_base {
1043 evalue ** E_vertex;
1045 ienumerator_base(unsigned dim, vertex_decomposer *vpd) :
1046 enumerator_base(dim,vpd) {
1047 E_vertex = new evalue_p[dim];
1050 virtual ~ienumerator_base() {
1051 delete [] E_vertex;
1054 evalue *E_num(int i, int d) {
1055 return E_vertex[i + (dim-d)];
1059 struct cumulator {
1060 evalue *factor;
1061 evalue *v;
1062 dpoly_r *r;
1064 cumulator(evalue *factor, evalue *v, dpoly_r *r) :
1065 factor(factor), v(v), r(r) {}
1067 void cumulate(barvinok_options *options);
1069 virtual void add_term(const vector<int>& powers, evalue *f2) = 0;
1072 void cumulator::cumulate(barvinok_options *options)
1074 evalue cum; // factor * 1 * E_num[0]/1 * (E_num[0]-1)/2 *...
1075 evalue f;
1076 evalue t; // E_num[0] - (m-1)
1077 evalue *cst;
1078 evalue mone;
1080 if (options->lookup_table) {
1081 value_init(mone.d);
1082 evalue_set_si(&mone, -1, 1);
1085 value_init(cum.d);
1086 evalue_copy(&cum, factor);
1087 value_init(f.d);
1088 value_init(f.x.n);
1089 value_set_si(f.d, 1);
1090 value_set_si(f.x.n, 1);
1091 value_init(t.d);
1092 evalue_copy(&t, v);
1094 if (!options->lookup_table) {
1095 for (cst = &t; value_zero_p(cst->d); ) {
1096 if (cst->x.p->type == fractional)
1097 cst = &cst->x.p->arr[1];
1098 else
1099 cst = &cst->x.p->arr[0];
1103 for (int m = 0; m < r->len; ++m) {
1104 if (m > 0) {
1105 if (m > 1) {
1106 value_set_si(f.d, m);
1107 emul(&f, &cum);
1108 if (!options->lookup_table)
1109 value_subtract(cst->x.n, cst->x.n, cst->d);
1110 else
1111 eadd(&mone, &t);
1113 emul(&t, &cum);
1115 dpoly_r_term_list& current = r->c[r->len-1-m];
1116 dpoly_r_term_list::iterator j;
1117 for (j = current.begin(); j != current.end(); ++j) {
1118 if ((*j)->coeff == 0)
1119 continue;
1120 evalue *f2 = new evalue;
1121 value_init(f2->d);
1122 value_init(f2->x.n);
1123 zz2value((*j)->coeff, f2->x.n);
1124 zz2value(r->denom, f2->d);
1125 emul(&cum, f2);
1127 add_term((*j)->powers, f2);
1130 free_evalue_refs(&f);
1131 free_evalue_refs(&t);
1132 free_evalue_refs(&cum);
1133 if (options->lookup_table)
1134 free_evalue_refs(&mone);
1137 struct E_poly_term {
1138 vector<int> powers;
1139 evalue *E;
1142 struct ie_cum : public cumulator {
1143 vector<E_poly_term *> terms;
1145 ie_cum(evalue *factor, evalue *v, dpoly_r *r) : cumulator(factor, v, r) {}
1147 virtual void add_term(const vector<int>& powers, evalue *f2);
1150 void ie_cum::add_term(const vector<int>& powers, evalue *f2)
1152 int k;
1153 for (k = 0; k < terms.size(); ++k) {
1154 if (terms[k]->powers == powers) {
1155 eadd(f2, terms[k]->E);
1156 free_evalue_refs(f2);
1157 delete f2;
1158 break;
1161 if (k >= terms.size()) {
1162 E_poly_term *ET = new E_poly_term;
1163 ET->powers = powers;
1164 ET->E = f2;
1165 terms.push_back(ET);
1169 struct ienumerator : public signed_cone_consumer, public vertex_decomposer,
1170 public ienumerator_base {
1171 //Polyhedron *pVD;
1172 mat_ZZ den;
1173 mat_ZZ vertex;
1174 mpq_t tcount;
1176 ienumerator(Polyhedron *P, unsigned dim, unsigned nbV) :
1177 vertex_decomposer(P, nbV, *this), ienumerator_base(dim, this) {
1178 vertex.SetDims(1, dim);
1180 den.SetDims(dim, dim);
1181 mpq_init(tcount);
1184 ~ienumerator() {
1185 mpq_clear(tcount);
1188 virtual void handle(const signed_cone& sc, barvinok_options *options);
1189 void reduce(evalue *factor, const mat_ZZ& num, const mat_ZZ& den_f,
1190 barvinok_options *options);
1193 void ienumerator::reduce(evalue *factor, const mat_ZZ& num, const mat_ZZ& den_f,
1194 barvinok_options *options)
1196 unsigned len = den_f.NumRows(); // number of factors in den
1197 unsigned dim = num.NumCols();
1198 assert(num.NumRows() == 1);
1200 if (dim == 0) {
1201 eadd(factor, vE[vert]);
1202 return;
1205 vec_ZZ den_s;
1206 mat_ZZ den_r;
1207 vec_ZZ num_s;
1208 mat_ZZ num_p;
1210 split_one(num, num_s, num_p, den_f, den_s, den_r);
1212 vec_ZZ den_p;
1213 den_p.SetLength(len);
1215 ZZ one;
1216 one = 1;
1217 normalize(one, num_s, num_p, den_s, den_p, den_r);
1218 if (one != 1)
1219 emul(&mone, factor);
1221 int only_param = 0;
1222 int no_param = 0;
1223 for (int k = 0; k < len; ++k) {
1224 if (den_p[k] == 0)
1225 ++no_param;
1226 else if (den_s[k] == 0)
1227 ++only_param;
1229 if (no_param == 0) {
1230 reduce(factor, num_p, den_r, options);
1231 } else {
1232 int k, l;
1233 mat_ZZ pden;
1234 pden.SetDims(only_param, dim-1);
1236 for (k = 0, l = 0; k < len; ++k)
1237 if (den_s[k] == 0)
1238 pden[l++] = den_r[k];
1240 for (k = 0; k < len; ++k)
1241 if (den_p[k] == 0)
1242 break;
1244 dpoly n(no_param, num_s[0]);
1245 dpoly D(no_param, den_s[k], 1);
1246 for ( ; ++k < len; )
1247 if (den_p[k] == 0) {
1248 dpoly fact(no_param, den_s[k], 1);
1249 D *= fact;
1252 dpoly_r * r = 0;
1253 // if no_param + only_param == len then all powers
1254 // below will be all zero
1255 if (no_param + only_param == len) {
1256 if (E_num(0, dim) != 0)
1257 r = new dpoly_r(n, len);
1258 else {
1259 mpq_set_si(tcount, 0, 1);
1260 one = 1;
1261 n.div(D, tcount, one);
1263 if (value_notzero_p(mpq_numref(tcount))) {
1264 evalue f;
1265 value_init(f.d);
1266 value_init(f.x.n);
1267 value_assign(f.x.n, mpq_numref(tcount));
1268 value_assign(f.d, mpq_denref(tcount));
1269 emul(&f, factor);
1270 reduce(factor, num_p, pden, options);
1271 free_evalue_refs(&f);
1273 return;
1275 } else {
1276 for (k = 0; k < len; ++k) {
1277 if (den_s[k] == 0 || den_p[k] == 0)
1278 continue;
1280 dpoly pd(no_param-1, den_s[k], 1);
1282 int l;
1283 for (l = 0; l < k; ++l)
1284 if (den_r[l] == den_r[k])
1285 break;
1287 if (r == 0)
1288 r = new dpoly_r(n, pd, l, len);
1289 else {
1290 dpoly_r *nr = new dpoly_r(r, pd, l, len);
1291 delete r;
1292 r = nr;
1296 dpoly_r *rc = r->div(D);
1297 delete r;
1298 r = rc;
1299 if (E_num(0, dim) == 0) {
1300 int common = pden.NumRows();
1301 dpoly_r_term_list& final = r->c[r->len-1];
1302 int rows;
1303 evalue t;
1304 evalue f;
1305 value_init(f.d);
1306 value_init(f.x.n);
1307 zz2value(r->denom, f.d);
1308 dpoly_r_term_list::iterator j;
1309 for (j = final.begin(); j != final.end(); ++j) {
1310 if ((*j)->coeff == 0)
1311 continue;
1312 rows = common;
1313 for (int k = 0; k < r->dim; ++k) {
1314 int n = (*j)->powers[k];
1315 if (n == 0)
1316 continue;
1317 pden.SetDims(rows+n, pden.NumCols());
1318 for (int l = 0; l < n; ++l)
1319 pden[rows+l] = den_r[k];
1320 rows += n;
1322 value_init(t.d);
1323 evalue_copy(&t, factor);
1324 zz2value((*j)->coeff, f.x.n);
1325 emul(&f, &t);
1326 reduce(&t, num_p, pden, options);
1327 free_evalue_refs(&t);
1329 free_evalue_refs(&f);
1330 } else {
1331 ie_cum cum(factor, E_num(0, dim), r);
1332 cum.cumulate(options);
1334 int common = pden.NumRows();
1335 int rows;
1336 for (int j = 0; j < cum.terms.size(); ++j) {
1337 rows = common;
1338 pden.SetDims(rows, pden.NumCols());
1339 for (int k = 0; k < r->dim; ++k) {
1340 int n = cum.terms[j]->powers[k];
1341 if (n == 0)
1342 continue;
1343 pden.SetDims(rows+n, pden.NumCols());
1344 for (int l = 0; l < n; ++l)
1345 pden[rows+l] = den_r[k];
1346 rows += n;
1348 reduce(cum.terms[j]->E, num_p, pden, options);
1349 free_evalue_refs(cum.terms[j]->E);
1350 delete cum.terms[j]->E;
1351 delete cum.terms[j];
1354 delete r;
1358 static int type_offset(enode *p)
1360 return p->type == fractional ? 1 :
1361 p->type == flooring ? 1 : 0;
1364 static int edegree(evalue *e)
1366 int d = 0;
1367 enode *p;
1369 if (value_notzero_p(e->d))
1370 return 0;
1372 p = e->x.p;
1373 int i = type_offset(p);
1374 if (p->size-i-1 > d)
1375 d = p->size - i - 1;
1376 for (; i < p->size; i++) {
1377 int d2 = edegree(&p->arr[i]);
1378 if (d2 > d)
1379 d = d2;
1381 return d;
1384 void ienumerator::handle(const signed_cone& sc, barvinok_options *options)
1386 assert(sc.det == 1);
1387 assert(!sc.closed);
1388 assert(sc.rays.NumRows() == dim);
1390 lattice_point(V, sc.rays, vertex[0], E_vertex, options);
1392 den = sc.rays;
1394 evalue one;
1395 value_init(one.d);
1396 evalue_set_si(&one, sc.sign, 1);
1397 reduce(&one, vertex, den, options);
1398 free_evalue_refs(&one);
1400 for (int i = 0; i < dim; ++i)
1401 if (E_vertex[i]) {
1402 free_evalue_refs(E_vertex[i]);
1403 delete E_vertex[i];
1407 struct bfenumerator : public vertex_decomposer, public bf_base,
1408 public ienumerator_base {
1409 evalue *factor;
1411 bfenumerator(Polyhedron *P, unsigned dim, unsigned nbV) :
1412 vertex_decomposer(P, nbV, *this),
1413 bf_base(dim), ienumerator_base(dim, this) {
1414 lower = 0;
1415 factor = NULL;
1418 ~bfenumerator() {
1421 virtual void handle(const signed_cone& sc, barvinok_options *options);
1422 virtual void base(mat_ZZ& factors, bfc_vec& v);
1424 bfc_term_base* new_bf_term(int len) {
1425 bfe_term* t = new bfe_term(len);
1426 return t;
1429 virtual void set_factor(bfc_term_base *t, int k, int change) {
1430 bfe_term* bfet = static_cast<bfe_term *>(t);
1431 factor = bfet->factors[k];
1432 assert(factor != NULL);
1433 bfet->factors[k] = NULL;
1434 if (change)
1435 emul(&mone, factor);
1438 virtual void set_factor(bfc_term_base *t, int k, mpq_t &q, int change) {
1439 bfe_term* bfet = static_cast<bfe_term *>(t);
1440 factor = bfet->factors[k];
1441 assert(factor != NULL);
1442 bfet->factors[k] = NULL;
1444 evalue f;
1445 value_init(f.d);
1446 value_init(f.x.n);
1447 if (change)
1448 value_oppose(f.x.n, mpq_numref(q));
1449 else
1450 value_assign(f.x.n, mpq_numref(q));
1451 value_assign(f.d, mpq_denref(q));
1452 emul(&f, factor);
1453 free_evalue_refs(&f);
1456 virtual void set_factor(bfc_term_base *t, int k, const QQ& c, int change) {
1457 bfe_term* bfet = static_cast<bfe_term *>(t);
1459 factor = new evalue;
1461 evalue f;
1462 value_init(f.d);
1463 value_init(f.x.n);
1464 zz2value(c.n, f.x.n);
1465 if (change)
1466 value_oppose(f.x.n, f.x.n);
1467 zz2value(c.d, f.d);
1469 value_init(factor->d);
1470 evalue_copy(factor, bfet->factors[k]);
1471 emul(&f, factor);
1472 free_evalue_refs(&f);
1475 void set_factor(evalue *f, int change) {
1476 if (change)
1477 emul(&mone, f);
1478 factor = f;
1481 virtual void insert_term(bfc_term_base *t, int i) {
1482 bfe_term* bfet = static_cast<bfe_term *>(t);
1483 int len = t->terms.NumRows()-1; // already increased by one
1485 bfet->factors.resize(len+1);
1486 for (int j = len; j > i; --j) {
1487 bfet->factors[j] = bfet->factors[j-1];
1488 t->terms[j] = t->terms[j-1];
1490 bfet->factors[i] = factor;
1491 factor = NULL;
1494 virtual void update_term(bfc_term_base *t, int i) {
1495 bfe_term* bfet = static_cast<bfe_term *>(t);
1497 eadd(factor, bfet->factors[i]);
1498 free_evalue_refs(factor);
1499 delete factor;
1502 virtual bool constant_vertex(int dim) { return E_num(0, dim) == 0; }
1504 virtual void cum(bf_reducer *bfr, bfc_term_base *t, int k, dpoly_r *r,
1505 barvinok_options *options);
1508 enumerator_base *enumerator_base::create(Polyhedron *P, unsigned dim, unsigned nbV,
1509 barvinok_options *options)
1511 enumerator_base *eb;
1513 if (options->incremental_specialization == BV_SPECIALIZATION_BF)
1514 eb = new bfenumerator(P, dim, nbV);
1515 else if (options->incremental_specialization == BV_SPECIALIZATION_DF)
1516 eb = new ienumerator(P, dim, nbV);
1517 else
1518 eb = new enumerator(P, dim, nbV);
1520 return eb;
1523 struct bfe_cum : public cumulator {
1524 bfenumerator *bfe;
1525 bfc_term_base *told;
1526 int k;
1527 bf_reducer *bfr;
1529 bfe_cum(evalue *factor, evalue *v, dpoly_r *r, bf_reducer *bfr,
1530 bfc_term_base *t, int k, bfenumerator *e) :
1531 cumulator(factor, v, r), told(t), k(k),
1532 bfr(bfr), bfe(e) {
1535 virtual void add_term(const vector<int>& powers, evalue *f2);
1538 void bfe_cum::add_term(const vector<int>& powers, evalue *f2)
1540 bfr->update_powers(powers);
1542 bfc_term_base * t = bfe->find_bfc_term(bfr->vn, bfr->npowers, bfr->nnf);
1543 bfe->set_factor(f2, bfr->l_changes % 2);
1544 bfe->add_term(t, told->terms[k], bfr->l_extra_num);
1547 void bfenumerator::cum(bf_reducer *bfr, bfc_term_base *t, int k,
1548 dpoly_r *r, barvinok_options *options)
1550 bfe_term* bfet = static_cast<bfe_term *>(t);
1551 bfe_cum cum(bfet->factors[k], E_num(0, bfr->d), r, bfr, t, k, this);
1552 cum.cumulate(options);
1555 void bfenumerator::base(mat_ZZ& factors, bfc_vec& v)
1557 for (int i = 0; i < v.size(); ++i) {
1558 assert(v[i]->terms.NumRows() == 1);
1559 evalue *factor = static_cast<bfe_term *>(v[i])->factors[0];
1560 eadd(factor, vE[vert]);
1561 delete v[i];
1565 void bfenumerator::handle(const signed_cone& sc, barvinok_options *options)
1567 assert(sc.det == 1);
1568 assert(!sc.closed);
1569 assert(sc.rays.NumRows() == enumerator_base::dim);
1571 bfe_term* t = new bfe_term(enumerator_base::dim);
1572 vector< bfc_term_base * > v;
1573 v.push_back(t);
1575 t->factors.resize(1);
1577 t->terms.SetDims(1, enumerator_base::dim);
1578 lattice_point(V, sc.rays, t->terms[0], E_vertex, options);
1580 // the elements of factors are always lexpositive
1581 mat_ZZ factors;
1582 int s = setup_factors(sc.rays, factors, t, sc.sign);
1584 t->factors[0] = new evalue;
1585 value_init(t->factors[0]->d);
1586 evalue_set_si(t->factors[0], s, 1);
1587 reduce(factors, v, options);
1589 for (int i = 0; i < enumerator_base::dim; ++i)
1590 if (E_vertex[i]) {
1591 free_evalue_refs(E_vertex[i]);
1592 delete E_vertex[i];
1596 #ifdef HAVE_CORRECT_VERTICES
1597 static inline Param_Polyhedron *Polyhedron2Param_SD(Polyhedron **Din,
1598 Polyhedron *Cin,int WS,Polyhedron **CEq,Matrix **CT)
1600 if (WS & POL_NO_DUAL)
1601 WS = 0;
1602 return Polyhedron2Param_SimplifiedDomain(Din, Cin, WS, CEq, CT);
1604 #else
1605 static Param_Polyhedron *Polyhedron2Param_SD(Polyhedron **Din,
1606 Polyhedron *Cin,int WS,Polyhedron **CEq,Matrix **CT)
1608 static char data[] = " 1 0 0 0 0 1 -18 "
1609 " 1 0 0 -20 0 19 1 "
1610 " 1 0 1 20 0 -20 16 "
1611 " 1 0 0 0 0 -1 19 "
1612 " 1 0 -1 0 0 0 4 "
1613 " 1 4 -20 0 0 -1 23 "
1614 " 1 -4 20 0 0 1 -22 "
1615 " 1 0 1 0 20 -20 16 "
1616 " 1 0 0 0 -20 19 1 ";
1617 static int checked = 0;
1618 if (!checked) {
1619 checked = 1;
1620 char *p = data;
1621 int n, v, i;
1622 Matrix *M = Matrix_Alloc(9, 7);
1623 for (i = 0; i < 9; ++i)
1624 for (int j = 0; j < 7; ++j) {
1625 sscanf(p, "%d%n", &v, &n);
1626 p += n;
1627 value_set_si(M->p[i][j], v);
1629 Polyhedron *P = Constraints2Polyhedron(M, 1024);
1630 Matrix_Free(M);
1631 Polyhedron *U = Universe_Polyhedron(1);
1632 Param_Polyhedron *PP = Polyhedron2Param_Domain(P, U, 1024);
1633 Polyhedron_Free(P);
1634 Polyhedron_Free(U);
1635 Param_Vertices *V;
1636 for (i = 0, V = PP->V; V; ++i, V = V->next)
1638 if (PP)
1639 Param_Polyhedron_Free(PP);
1640 if (i != 10) {
1641 fprintf(stderr, "WARNING: results may be incorrect\n");
1642 fprintf(stderr,
1643 "WARNING: use latest version of PolyLib to remove this warning\n");
1647 return Polyhedron2Param_SimplifiedDomain(Din, Cin, WS, CEq, CT);
1649 #endif
1651 static evalue* barvinok_enumerate_ev_f(Polyhedron *P, Polyhedron* C,
1652 barvinok_options *options);
1654 /* Destroys C */
1655 static evalue* barvinok_enumerate_cst(Polyhedron *P, Polyhedron* C,
1656 struct barvinok_options *options)
1658 evalue *eres;
1660 ALLOC(evalue, eres);
1661 value_init(eres->d);
1662 value_set_si(eres->d, 0);
1663 eres->x.p = new_enode(partition, 2, C->Dimension);
1664 EVALUE_SET_DOMAIN(eres->x.p->arr[0],
1665 DomainConstraintSimplify(C, options->MaxRays));
1666 value_set_si(eres->x.p->arr[1].d, 1);
1667 value_init(eres->x.p->arr[1].x.n);
1668 if (emptyQ(P))
1669 value_set_si(eres->x.p->arr[1].x.n, 0);
1670 else
1671 barvinok_count_with_options(P, &eres->x.p->arr[1].x.n, options);
1673 return eres;
1676 evalue* barvinok_enumerate_with_options(Polyhedron *P, Polyhedron* C,
1677 struct barvinok_options *options)
1679 //P = unfringe(P, MaxRays);
1680 Polyhedron *Corig = C;
1681 Polyhedron *CEq = NULL, *rVD, *CA;
1682 int r = 0;
1683 unsigned nparam = C->Dimension;
1684 evalue *eres;
1686 evalue factor;
1687 value_init(factor.d);
1688 evalue_set_si(&factor, 1, 1);
1690 CA = align_context(C, P->Dimension, options->MaxRays);
1691 P = DomainIntersection(P, CA, options->MaxRays);
1692 Polyhedron_Free(CA);
1694 /* for now */
1695 POL_ENSURE_FACETS(P);
1696 POL_ENSURE_VERTICES(P);
1697 POL_ENSURE_FACETS(C);
1698 POL_ENSURE_VERTICES(C);
1700 if (C->Dimension == 0 || emptyQ(P)) {
1701 constant:
1702 eres = barvinok_enumerate_cst(P, CEq ? CEq : Polyhedron_Copy(C), options);
1703 out:
1704 emul(&factor, eres);
1705 if (options->polynomial_approximation == BV_POLAPPROX_UPPER)
1706 evalue_frac2polynomial(eres, 1, options->MaxRays);
1707 if (options->polynomial_approximation == BV_POLAPPROX_LOWER)
1708 evalue_frac2polynomial(eres, 0, options->MaxRays);
1709 reduce_evalue(eres);
1710 free_evalue_refs(&factor);
1711 Domain_Free(P);
1712 if (C != Corig)
1713 Polyhedron_Free(C);
1715 return eres;
1717 if (Polyhedron_is_infinite_param(P, nparam))
1718 goto constant;
1720 if (P->NbEq != 0) {
1721 Matrix *f;
1722 P = remove_equalities_p(P, P->Dimension-nparam, &f);
1723 mask(f, &factor, options);
1724 Matrix_Free(f);
1726 if (P->Dimension == nparam) {
1727 CEq = P;
1728 P = Universe_Polyhedron(0);
1729 goto constant;
1732 Polyhedron *T = Polyhedron_Factor(P, nparam, options->MaxRays);
1733 if (T || (P->Dimension == nparam+1)) {
1734 Polyhedron *Q;
1735 Polyhedron *C2;
1736 for (Q = T ? T : P; Q; Q = Q->next) {
1737 Polyhedron *next = Q->next;
1738 Q->next = NULL;
1740 Polyhedron *QC = Q;
1741 if (Q->Dimension != C->Dimension)
1742 QC = Polyhedron_Project(Q, nparam);
1744 C2 = C;
1745 C = DomainIntersection(C, QC, options->MaxRays);
1746 if (C2 != Corig)
1747 Polyhedron_Free(C2);
1748 if (QC != Q)
1749 Polyhedron_Free(QC);
1751 Q->next = next;
1754 if (T) {
1755 Polyhedron_Free(P);
1756 P = T;
1757 if (T->Dimension == C->Dimension) {
1758 P = T->next;
1759 T->next = NULL;
1760 Polyhedron_Free(T);
1764 Polyhedron *next = P->next;
1765 P->next = NULL;
1766 eres = barvinok_enumerate_ev_f(P, C, options);
1767 P->next = next;
1769 if (P->next) {
1770 Polyhedron *Q;
1771 evalue *f;
1773 for (Q = P->next; Q; Q = Q->next) {
1774 Polyhedron *next = Q->next;
1775 Q->next = NULL;
1777 f = barvinok_enumerate_ev_f(Q, C, options);
1778 emul(f, eres);
1779 free_evalue_refs(f);
1780 free(f);
1782 Q->next = next;
1786 goto out;
1789 evalue* barvinok_enumerate_ev(Polyhedron *P, Polyhedron* C, unsigned MaxRays)
1791 evalue *E;
1792 barvinok_options *options = barvinok_options_new_with_defaults();
1793 options->MaxRays = MaxRays;
1794 E = barvinok_enumerate_with_options(P, C, options);
1795 barvinok_options_free(options);
1796 return E;
1799 /* adapted from mpolyhedron_inflate in PolyLib */
1800 static Polyhedron *Polyhedron_Inflate(Polyhedron *P, unsigned nparam,
1801 unsigned MaxRays)
1803 Value sum;
1804 int nvar = P->Dimension - nparam;
1805 Matrix *C = Polyhedron2Constraints(P);
1806 Polyhedron *P2;
1808 value_init(sum);
1809 /* subtract the sum of the negative coefficients of each inequality */
1810 for (int i = 0; i < C->NbRows; ++i) {
1811 value_set_si(sum, 0);
1812 for (int j = 0; j < nvar; ++j)
1813 if (value_neg_p(C->p[i][1+j]))
1814 value_addto(sum, sum, C->p[i][1+j]);
1815 value_subtract(C->p[i][1+P->Dimension], C->p[i][1+P->Dimension], sum);
1817 value_clear(sum);
1818 P2 = Constraints2Polyhedron(C, MaxRays);
1819 Matrix_Free(C);
1820 return P2;
1823 /* adapted from mpolyhedron_deflate in PolyLib */
1824 static Polyhedron *Polyhedron_Deflate(Polyhedron *P, unsigned nparam,
1825 unsigned MaxRays)
1827 Value sum;
1828 int nvar = P->Dimension - nparam;
1829 Matrix *C = Polyhedron2Constraints(P);
1830 Polyhedron *P2;
1832 value_init(sum);
1833 /* subtract the sum of the positive coefficients of each inequality */
1834 for (int i = 0; i < C->NbRows; ++i) {
1835 value_set_si(sum, 0);
1836 for (int j = 0; j < nvar; ++j)
1837 if (value_pos_p(C->p[i][1+j]))
1838 value_addto(sum, sum, C->p[i][1+j]);
1839 value_subtract(C->p[i][1+P->Dimension], C->p[i][1+P->Dimension], sum);
1841 value_clear(sum);
1842 P2 = Constraints2Polyhedron(C, MaxRays);
1843 Matrix_Free(C);
1844 return P2;
1847 static evalue* barvinok_enumerate_ev_f(Polyhedron *P, Polyhedron* C,
1848 barvinok_options *options)
1850 unsigned nparam = C->Dimension;
1851 bool pre_approx = options->polynomial_approximation >= BV_POLAPPROX_PRE_LOWER &&
1852 options->polynomial_approximation <= BV_POLAPPROX_PRE_APPROX;
1854 if (P->Dimension - nparam == 1 && !pre_approx)
1855 return ParamLine_Length(P, C, options);
1857 Param_Polyhedron *PP = NULL;
1858 Polyhedron *CEq = NULL, *pVD;
1859 Matrix *CT = NULL;
1860 Param_Domain *D, *next;
1861 Param_Vertices *V;
1862 evalue *eres;
1863 Polyhedron *Porig = P;
1864 Value det;
1865 Polyhedron *T;
1867 if (options->polynomial_approximation == BV_POLAPPROX_PRE_UPPER)
1868 P = Polyhedron_Inflate(P, nparam, options->MaxRays);
1869 if (options->polynomial_approximation == BV_POLAPPROX_PRE_LOWER)
1870 P = Polyhedron_Deflate(P, nparam, options->MaxRays);
1872 T = P;
1873 PP = Polyhedron2Param_SD(&T, C, options->MaxRays, &CEq, &CT);
1874 if (T != P && P != Porig)
1875 Polyhedron_Free(P);
1876 P = T;
1878 if (isIdentity(CT)) {
1879 Matrix_Free(CT);
1880 CT = NULL;
1881 } else {
1882 assert(CT->NbRows != CT->NbColumns);
1883 if (CT->NbRows == 1) { // no more parameters
1884 eres = barvinok_enumerate_cst(P, CEq, options);
1885 out:
1886 if (CT)
1887 Matrix_Free(CT);
1888 if (PP)
1889 Param_Polyhedron_Free(PP);
1890 if (P != Porig)
1891 Polyhedron_Free(P);
1893 return eres;
1895 nparam = CT->NbRows - 1;
1898 if (pre_approx) {
1899 value_init(det);
1900 Polyhedron *T = P;
1901 Param_Polyhedron_Scale_Integer(PP, &T, &det, options->MaxRays);
1902 if (P != Porig)
1903 Polyhedron_Free(P);
1904 P = T;
1907 unsigned dim = P->Dimension - nparam;
1909 ALLOC(evalue, eres);
1910 value_init(eres->d);
1911 value_set_si(eres->d, 0);
1913 int nd;
1914 for (nd = 0, D=PP->D; D; ++nd, D=D->next);
1915 struct section { Polyhedron *D; evalue E; };
1916 section *s = new section[nd];
1917 Polyhedron **fVD = new Polyhedron_p[nd];
1919 enumerator_base *et = NULL;
1920 try_again:
1921 if (et)
1922 delete et;
1924 et = enumerator_base::create(P, dim, PP->nbV, options);
1926 for(nd = 0, D=PP->D; D; D=next) {
1927 next = D->next;
1929 Polyhedron *rVD = reduce_domain(D->Domain, CT, CEq, fVD, nd, options);
1930 if (!rVD)
1931 continue;
1933 pVD = CT ? DomainImage(rVD,CT,options->MaxRays) : rVD;
1935 value_init(s[nd].E.d);
1936 evalue_set_si(&s[nd].E, 0, 1);
1937 s[nd].D = rVD;
1939 FORALL_PVertex_in_ParamPolyhedron(V,D,PP) // _i is internal counter
1940 if (!et->vE[_i])
1941 try {
1942 et->decompose_at(V, _i, options);
1943 } catch (OrthogonalException &e) {
1944 if (rVD != pVD)
1945 Domain_Free(pVD);
1946 for (; nd >= 0; --nd) {
1947 free_evalue_refs(&s[nd].E);
1948 Domain_Free(s[nd].D);
1949 Domain_Free(fVD[nd]);
1951 goto try_again;
1953 eadd(et->vE[_i] , &s[nd].E);
1954 END_FORALL_PVertex_in_ParamPolyhedron;
1955 evalue_range_reduction_in_domain(&s[nd].E, pVD);
1957 if (CT)
1958 addeliminatedparams_evalue(&s[nd].E, CT);
1959 ++nd;
1960 if (rVD != pVD)
1961 Domain_Free(pVD);
1964 delete et;
1965 if (nd == 0)
1966 evalue_set_si(eres, 0, 1);
1967 else {
1968 eres->x.p = new_enode(partition, 2*nd, C->Dimension);
1969 for (int j = 0; j < nd; ++j) {
1970 EVALUE_SET_DOMAIN(eres->x.p->arr[2*j], s[j].D);
1971 value_clear(eres->x.p->arr[2*j+1].d);
1972 eres->x.p->arr[2*j+1] = s[j].E;
1973 Domain_Free(fVD[j]);
1976 delete [] s;
1977 delete [] fVD;
1979 if (pre_approx) {
1980 evalue_div(eres, det);
1981 value_clear(det);
1984 if (CEq)
1985 Polyhedron_Free(CEq);
1986 goto out;
1989 Enumeration* barvinok_enumerate(Polyhedron *P, Polyhedron* C, unsigned MaxRays)
1991 evalue *EP = barvinok_enumerate_ev(P, C, MaxRays);
1993 return partition2enumeration(EP);
1996 static void SwapColumns(Value **V, int n, int i, int j)
1998 for (int r = 0; r < n; ++r)
1999 value_swap(V[r][i], V[r][j]);
2002 static void SwapColumns(Polyhedron *P, int i, int j)
2004 SwapColumns(P->Constraint, P->NbConstraints, i, j);
2005 SwapColumns(P->Ray, P->NbRays, i, j);
2008 /* Construct a constraint c from constraints l and u such that if
2009 * if constraint c holds then for each value of the other variables
2010 * there is at most one value of variable pos (position pos+1 in the constraints).
2012 * Given a lower and an upper bound
2013 * n_l v_i + <c_l,x> + c_l >= 0
2014 * -n_u v_i + <c_u,x> + c_u >= 0
2015 * the constructed constraint is
2017 * -(n_l<c_u,x> + n_u<c_l,x>) + (-n_l c_u - n_u c_l + n_l n_u - 1)
2019 * which is then simplified to remove the content of the non-constant coefficients
2021 * len is the total length of the constraints.
2022 * v is a temporary variable that can be used by this procedure
2024 static void negative_test_constraint(Value *l, Value *u, Value *c, int pos,
2025 int len, Value *v)
2027 value_oppose(*v, u[pos+1]);
2028 Vector_Combine(l+1, u+1, c+1, *v, l[pos+1], len-1);
2029 value_multiply(*v, *v, l[pos+1]);
2030 value_subtract(c[len-1], c[len-1], *v);
2031 value_set_si(*v, -1);
2032 Vector_Scale(c+1, c+1, *v, len-1);
2033 value_decrement(c[len-1], c[len-1]);
2034 ConstraintSimplify(c, c, len, v);
2037 static bool parallel_constraints(Value *l, Value *u, Value *c, int pos,
2038 int len)
2040 bool parallel;
2041 Value g1;
2042 Value g2;
2043 value_init(g1);
2044 value_init(g2);
2046 Vector_Gcd(&l[1+pos], len, &g1);
2047 Vector_Gcd(&u[1+pos], len, &g2);
2048 Vector_Combine(l+1+pos, u+1+pos, c+1, g2, g1, len);
2049 parallel = First_Non_Zero(c+1, len) == -1;
2051 value_clear(g1);
2052 value_clear(g2);
2054 return parallel;
2057 static void negative_test_constraint7(Value *l, Value *u, Value *c, int pos,
2058 int exist, int len, Value *v)
2060 Value g;
2061 value_init(g);
2063 Vector_Gcd(&u[1+pos], exist, v);
2064 Vector_Gcd(&l[1+pos], exist, &g);
2065 Vector_Combine(l+1, u+1, c+1, *v, g, len-1);
2066 value_multiply(*v, *v, g);
2067 value_subtract(c[len-1], c[len-1], *v);
2068 value_set_si(*v, -1);
2069 Vector_Scale(c+1, c+1, *v, len-1);
2070 value_decrement(c[len-1], c[len-1]);
2071 ConstraintSimplify(c, c, len, v);
2073 value_clear(g);
2076 /* Turns a x + b >= 0 into a x + b <= -1
2078 * len is the total length of the constraint.
2079 * v is a temporary variable that can be used by this procedure
2081 static void oppose_constraint(Value *c, int len, Value *v)
2083 value_set_si(*v, -1);
2084 Vector_Scale(c+1, c+1, *v, len-1);
2085 value_decrement(c[len-1], c[len-1]);
2088 /* Split polyhedron P into two polyhedra *pos and *neg, where
2089 * existential variable i has at most one solution for each
2090 * value of the other variables in *neg.
2092 * The splitting is performed using constraints l and u.
2094 * nvar: number of set variables
2095 * row: temporary vector that can be used by this procedure
2096 * f: temporary value that can be used by this procedure
2098 static bool SplitOnConstraint(Polyhedron *P, int i, int l, int u,
2099 int nvar, int MaxRays, Vector *row, Value& f,
2100 Polyhedron **pos, Polyhedron **neg)
2102 negative_test_constraint(P->Constraint[l], P->Constraint[u],
2103 row->p, nvar+i, P->Dimension+2, &f);
2104 *neg = AddConstraints(row->p, 1, P, MaxRays);
2106 /* We found an independent, but useless constraint
2107 * Maybe we should detect this earlier and not
2108 * mark the variable as INDEPENDENT
2110 if (emptyQ((*neg))) {
2111 Polyhedron_Free(*neg);
2112 return false;
2115 oppose_constraint(row->p, P->Dimension+2, &f);
2116 *pos = AddConstraints(row->p, 1, P, MaxRays);
2118 if (emptyQ((*pos))) {
2119 Polyhedron_Free(*neg);
2120 Polyhedron_Free(*pos);
2121 return false;
2124 return true;
2128 * unimodularly transform P such that constraint r is transformed
2129 * into a constraint that involves only a single (the first)
2130 * existential variable
2133 static Polyhedron *rotate_along(Polyhedron *P, int r, int nvar, int exist,
2134 unsigned MaxRays)
2136 Value g;
2137 value_init(g);
2139 Vector *row = Vector_Alloc(exist);
2140 Vector_Copy(P->Constraint[r]+1+nvar, row->p, exist);
2141 Vector_Gcd(row->p, exist, &g);
2142 if (value_notone_p(g))
2143 Vector_AntiScale(row->p, row->p, g, exist);
2144 value_clear(g);
2146 Matrix *M = unimodular_complete(row);
2147 Matrix *M2 = Matrix_Alloc(P->Dimension+1, P->Dimension+1);
2148 for (r = 0; r < nvar; ++r)
2149 value_set_si(M2->p[r][r], 1);
2150 for ( ; r < nvar+exist; ++r)
2151 Vector_Copy(M->p[r-nvar], M2->p[r]+nvar, exist);
2152 for ( ; r < P->Dimension+1; ++r)
2153 value_set_si(M2->p[r][r], 1);
2154 Polyhedron *T = Polyhedron_Image(P, M2, MaxRays);
2156 Matrix_Free(M2);
2157 Matrix_Free(M);
2158 Vector_Free(row);
2160 return T;
2163 /* Split polyhedron P into two polyhedra *pos and *neg, where
2164 * existential variable i has at most one solution for each
2165 * value of the other variables in *neg.
2167 * If independent is set, then the two constraints on which the
2168 * split will be performed need to be independent of the other
2169 * existential variables.
2171 * Return true if an appropriate split could be performed.
2173 * nvar: number of set variables
2174 * exist: number of existential variables
2175 * row: temporary vector that can be used by this procedure
2176 * f: temporary value that can be used by this procedure
2178 static bool SplitOnVar(Polyhedron *P, int i,
2179 int nvar, int exist, int MaxRays,
2180 Vector *row, Value& f, bool independent,
2181 Polyhedron **pos, Polyhedron **neg)
2183 int j;
2185 for (int l = P->NbEq; l < P->NbConstraints; ++l) {
2186 if (value_negz_p(P->Constraint[l][nvar+i+1]))
2187 continue;
2189 if (independent) {
2190 for (j = 0; j < exist; ++j)
2191 if (j != i && value_notzero_p(P->Constraint[l][nvar+j+1]))
2192 break;
2193 if (j < exist)
2194 continue;
2197 for (int u = P->NbEq; u < P->NbConstraints; ++u) {
2198 if (value_posz_p(P->Constraint[u][nvar+i+1]))
2199 continue;
2201 if (independent) {
2202 for (j = 0; j < exist; ++j)
2203 if (j != i && value_notzero_p(P->Constraint[u][nvar+j+1]))
2204 break;
2205 if (j < exist)
2206 continue;
2209 if (SplitOnConstraint(P, i, l, u, nvar, MaxRays, row, f, pos, neg)) {
2210 if (independent) {
2211 if (i != 0)
2212 SwapColumns(*neg, nvar+1, nvar+1+i);
2214 return true;
2219 return false;
2222 static bool double_bound_pair(Polyhedron *P, int nvar, int exist,
2223 int i, int l1, int l2,
2224 Polyhedron **pos, Polyhedron **neg)
2226 Value f;
2227 value_init(f);
2228 Vector *row = Vector_Alloc(P->Dimension+2);
2229 value_set_si(row->p[0], 1);
2230 value_oppose(f, P->Constraint[l1][nvar+i+1]);
2231 Vector_Combine(P->Constraint[l1]+1, P->Constraint[l2]+1,
2232 row->p+1,
2233 P->Constraint[l2][nvar+i+1], f,
2234 P->Dimension+1);
2235 ConstraintSimplify(row->p, row->p, P->Dimension+2, &f);
2236 *pos = AddConstraints(row->p, 1, P, 0);
2237 value_set_si(f, -1);
2238 Vector_Scale(row->p+1, row->p+1, f, P->Dimension+1);
2239 value_decrement(row->p[P->Dimension+1], row->p[P->Dimension+1]);
2240 *neg = AddConstraints(row->p, 1, P, 0);
2241 Vector_Free(row);
2242 value_clear(f);
2244 return !emptyQ((*pos)) && !emptyQ((*neg));
2247 static bool double_bound(Polyhedron *P, int nvar, int exist,
2248 Polyhedron **pos, Polyhedron **neg)
2250 for (int i = 0; i < exist; ++i) {
2251 int l1, l2;
2252 for (l1 = P->NbEq; l1 < P->NbConstraints; ++l1) {
2253 if (value_negz_p(P->Constraint[l1][nvar+i+1]))
2254 continue;
2255 for (l2 = l1 + 1; l2 < P->NbConstraints; ++l2) {
2256 if (value_negz_p(P->Constraint[l2][nvar+i+1]))
2257 continue;
2258 if (double_bound_pair(P, nvar, exist, i, l1, l2, pos, neg))
2259 return true;
2262 for (l1 = P->NbEq; l1 < P->NbConstraints; ++l1) {
2263 if (value_posz_p(P->Constraint[l1][nvar+i+1]))
2264 continue;
2265 if (l1 < P->NbConstraints)
2266 for (l2 = l1 + 1; l2 < P->NbConstraints; ++l2) {
2267 if (value_posz_p(P->Constraint[l2][nvar+i+1]))
2268 continue;
2269 if (double_bound_pair(P, nvar, exist, i, l1, l2, pos, neg))
2270 return true;
2273 return false;
2275 return false;
2278 enum constraint {
2279 ALL_POS = 1 << 0,
2280 ONE_NEG = 1 << 1,
2281 INDEPENDENT = 1 << 2,
2282 ROT_NEG = 1 << 3
2285 static evalue* enumerate_or(Polyhedron *D,
2286 unsigned exist, unsigned nparam, barvinok_options *options)
2288 #ifdef DEBUG_ER
2289 fprintf(stderr, "\nER: Or\n");
2290 #endif /* DEBUG_ER */
2292 Polyhedron *N = D->next;
2293 D->next = 0;
2294 evalue *EP =
2295 barvinok_enumerate_e_with_options(D, exist, nparam, options);
2296 Polyhedron_Free(D);
2298 for (D = N; D; D = N) {
2299 N = D->next;
2300 D->next = 0;
2302 evalue *EN =
2303 barvinok_enumerate_e_with_options(D, exist, nparam, options);
2305 eor(EN, EP);
2306 free_evalue_refs(EN);
2307 free(EN);
2308 Polyhedron_Free(D);
2311 reduce_evalue(EP);
2313 return EP;
2316 static evalue* enumerate_sum(Polyhedron *P,
2317 unsigned exist, unsigned nparam, barvinok_options *options)
2319 int nvar = P->Dimension - exist - nparam;
2320 int toswap = nvar < exist ? nvar : exist;
2321 for (int i = 0; i < toswap; ++i)
2322 SwapColumns(P, 1 + i, nvar+exist - i);
2323 nparam += nvar;
2325 #ifdef DEBUG_ER
2326 fprintf(stderr, "\nER: Sum\n");
2327 #endif /* DEBUG_ER */
2329 evalue *EP = barvinok_enumerate_e_with_options(P, exist, nparam, options);
2331 evalue_split_domains_into_orthants(EP, options->MaxRays);
2332 reduce_evalue(EP);
2333 evalue_range_reduction(EP);
2335 evalue_frac2floor2(EP, 1);
2337 evalue *sum = esum(EP, nvar);
2339 free_evalue_refs(EP);
2340 free(EP);
2341 EP = sum;
2343 evalue_range_reduction(EP);
2345 return EP;
2348 static evalue* split_sure(Polyhedron *P, Polyhedron *S,
2349 unsigned exist, unsigned nparam, barvinok_options *options)
2351 int nvar = P->Dimension - exist - nparam;
2353 Matrix *M = Matrix_Alloc(exist, S->Dimension+2);
2354 for (int i = 0; i < exist; ++i)
2355 value_set_si(M->p[i][nvar+i+1], 1);
2356 Polyhedron *O = S;
2357 S = DomainAddRays(S, M, options->MaxRays);
2358 Polyhedron_Free(O);
2359 Polyhedron *F = DomainAddRays(P, M, options->MaxRays);
2360 Polyhedron *D = DomainDifference(F, S, options->MaxRays);
2361 O = D;
2362 D = Disjoint_Domain(D, 0, options->MaxRays);
2363 Polyhedron_Free(F);
2364 Domain_Free(O);
2365 Matrix_Free(M);
2367 M = Matrix_Alloc(P->Dimension+1-exist, P->Dimension+1);
2368 for (int j = 0; j < nvar; ++j)
2369 value_set_si(M->p[j][j], 1);
2370 for (int j = 0; j < nparam+1; ++j)
2371 value_set_si(M->p[nvar+j][nvar+exist+j], 1);
2372 Polyhedron *T = Polyhedron_Image(S, M, options->MaxRays);
2373 evalue *EP = barvinok_enumerate_e_with_options(T, 0, nparam, options);
2374 Polyhedron_Free(S);
2375 Polyhedron_Free(T);
2376 Matrix_Free(M);
2378 for (Polyhedron *Q = D; Q; Q = Q->next) {
2379 Polyhedron *N = Q->next;
2380 Q->next = 0;
2381 T = DomainIntersection(P, Q, options->MaxRays);
2382 evalue *E = barvinok_enumerate_e_with_options(T, exist, nparam, options);
2383 eadd(E, EP);
2384 free_evalue_refs(E);
2385 free(E);
2386 Polyhedron_Free(T);
2387 Q->next = N;
2389 Domain_Free(D);
2390 return EP;
2393 static evalue* enumerate_sure(Polyhedron *P,
2394 unsigned exist, unsigned nparam, barvinok_options *options)
2396 int i;
2397 Polyhedron *S = P;
2398 int nvar = P->Dimension - exist - nparam;
2399 Value lcm;
2400 Value f;
2401 value_init(lcm);
2402 value_init(f);
2404 for (i = 0; i < exist; ++i) {
2405 Matrix *M = Matrix_Alloc(S->NbConstraints, S->Dimension+2);
2406 int c = 0;
2407 value_set_si(lcm, 1);
2408 for (int j = 0; j < S->NbConstraints; ++j) {
2409 if (value_negz_p(S->Constraint[j][1+nvar+i]))
2410 continue;
2411 if (value_one_p(S->Constraint[j][1+nvar+i]))
2412 continue;
2413 value_lcm(lcm, S->Constraint[j][1+nvar+i], &lcm);
2416 for (int j = 0; j < S->NbConstraints; ++j) {
2417 if (value_negz_p(S->Constraint[j][1+nvar+i]))
2418 continue;
2419 if (value_one_p(S->Constraint[j][1+nvar+i]))
2420 continue;
2421 value_division(f, lcm, S->Constraint[j][1+nvar+i]);
2422 Vector_Scale(S->Constraint[j], M->p[c], f, S->Dimension+2);
2423 value_subtract(M->p[c][S->Dimension+1],
2424 M->p[c][S->Dimension+1],
2425 lcm);
2426 value_increment(M->p[c][S->Dimension+1],
2427 M->p[c][S->Dimension+1]);
2428 ++c;
2430 Polyhedron *O = S;
2431 S = AddConstraints(M->p[0], c, S, options->MaxRays);
2432 if (O != P)
2433 Polyhedron_Free(O);
2434 Matrix_Free(M);
2435 if (emptyQ(S)) {
2436 Polyhedron_Free(S);
2437 value_clear(lcm);
2438 value_clear(f);
2439 return 0;
2442 value_clear(lcm);
2443 value_clear(f);
2445 #ifdef DEBUG_ER
2446 fprintf(stderr, "\nER: Sure\n");
2447 #endif /* DEBUG_ER */
2449 return split_sure(P, S, exist, nparam, options);
2452 static evalue* enumerate_sure2(Polyhedron *P,
2453 unsigned exist, unsigned nparam, barvinok_options *options)
2455 int nvar = P->Dimension - exist - nparam;
2456 int r;
2457 for (r = 0; r < P->NbRays; ++r)
2458 if (value_one_p(P->Ray[r][0]) &&
2459 value_one_p(P->Ray[r][P->Dimension+1]))
2460 break;
2462 if (r >= P->NbRays)
2463 return 0;
2465 Matrix *M = Matrix_Alloc(nvar + 1 + nparam, P->Dimension+2);
2466 for (int i = 0; i < nvar; ++i)
2467 value_set_si(M->p[i][1+i], 1);
2468 for (int i = 0; i < nparam; ++i)
2469 value_set_si(M->p[i+nvar][1+nvar+exist+i], 1);
2470 Vector_Copy(P->Ray[r]+1+nvar, M->p[nvar+nparam]+1+nvar, exist);
2471 value_set_si(M->p[nvar+nparam][0], 1);
2472 value_set_si(M->p[nvar+nparam][P->Dimension+1], 1);
2473 Polyhedron * F = Rays2Polyhedron(M, options->MaxRays);
2474 Matrix_Free(M);
2476 Polyhedron *I = DomainIntersection(F, P, options->MaxRays);
2477 Polyhedron_Free(F);
2479 #ifdef DEBUG_ER
2480 fprintf(stderr, "\nER: Sure2\n");
2481 #endif /* DEBUG_ER */
2483 return split_sure(P, I, exist, nparam, options);
2486 static evalue* enumerate_cyclic(Polyhedron *P,
2487 unsigned exist, unsigned nparam,
2488 evalue * EP, int r, int p, unsigned MaxRays)
2490 int nvar = P->Dimension - exist - nparam;
2492 /* If EP in its fractional maps only contains references
2493 * to the remainder parameter with appropriate coefficients
2494 * then we could in principle avoid adding existentially
2495 * quantified variables to the validity domains.
2496 * We'd have to replace the remainder by m { p/m }
2497 * and multiply with an appropriate factor that is one
2498 * only in the appropriate range.
2499 * This last multiplication can be avoided if EP
2500 * has a single validity domain with no (further)
2501 * constraints on the remainder parameter
2504 Matrix *CT = Matrix_Alloc(nparam+1, nparam+3);
2505 Matrix *M = Matrix_Alloc(1, 1+nparam+3);
2506 for (int j = 0; j < nparam; ++j)
2507 if (j != p)
2508 value_set_si(CT->p[j][j], 1);
2509 value_set_si(CT->p[p][nparam+1], 1);
2510 value_set_si(CT->p[nparam][nparam+2], 1);
2511 value_set_si(M->p[0][1+p], -1);
2512 value_absolute(M->p[0][1+nparam], P->Ray[0][1+nvar+exist+p]);
2513 value_set_si(M->p[0][1+nparam+1], 1);
2514 Polyhedron *CEq = Constraints2Polyhedron(M, 1);
2515 Matrix_Free(M);
2516 addeliminatedparams_enum(EP, CT, CEq, MaxRays, nparam);
2517 Polyhedron_Free(CEq);
2518 Matrix_Free(CT);
2520 return EP;
2523 static void enumerate_vd_add_ray(evalue *EP, Matrix *Rays, unsigned MaxRays)
2525 if (value_notzero_p(EP->d))
2526 return;
2528 assert(EP->x.p->type == partition);
2529 assert(EP->x.p->pos == EVALUE_DOMAIN(EP->x.p->arr[0])->Dimension);
2530 for (int i = 0; i < EP->x.p->size/2; ++i) {
2531 Polyhedron *D = EVALUE_DOMAIN(EP->x.p->arr[2*i]);
2532 Polyhedron *N = DomainAddRays(D, Rays, MaxRays);
2533 EVALUE_SET_DOMAIN(EP->x.p->arr[2*i], N);
2534 Domain_Free(D);
2538 static evalue* enumerate_line(Polyhedron *P,
2539 unsigned exist, unsigned nparam, barvinok_options *options)
2541 if (P->NbBid == 0)
2542 return 0;
2544 #ifdef DEBUG_ER
2545 fprintf(stderr, "\nER: Line\n");
2546 #endif /* DEBUG_ER */
2548 int nvar = P->Dimension - exist - nparam;
2549 int i, j;
2550 for (i = 0; i < nparam; ++i)
2551 if (value_notzero_p(P->Ray[0][1+nvar+exist+i]))
2552 break;
2553 assert(i < nparam);
2554 for (j = i+1; j < nparam; ++j)
2555 if (value_notzero_p(P->Ray[0][1+nvar+exist+i]))
2556 break;
2557 assert(j >= nparam); // for now
2559 Matrix *M = Matrix_Alloc(2, P->Dimension+2);
2560 value_set_si(M->p[0][0], 1);
2561 value_set_si(M->p[0][1+nvar+exist+i], 1);
2562 value_set_si(M->p[1][0], 1);
2563 value_set_si(M->p[1][1+nvar+exist+i], -1);
2564 value_absolute(M->p[1][1+P->Dimension], P->Ray[0][1+nvar+exist+i]);
2565 value_decrement(M->p[1][1+P->Dimension], M->p[1][1+P->Dimension]);
2566 Polyhedron *S = AddConstraints(M->p[0], 2, P, options->MaxRays);
2567 evalue *EP = barvinok_enumerate_e_with_options(S, exist, nparam, options);
2568 Polyhedron_Free(S);
2569 Matrix_Free(M);
2571 return enumerate_cyclic(P, exist, nparam, EP, 0, i, options->MaxRays);
2574 static int single_param_pos(Polyhedron*P, unsigned exist, unsigned nparam,
2575 int r)
2577 int nvar = P->Dimension - exist - nparam;
2578 if (First_Non_Zero(P->Ray[r]+1, nvar) != -1)
2579 return -1;
2580 int i = First_Non_Zero(P->Ray[r]+1+nvar+exist, nparam);
2581 if (i == -1)
2582 return -1;
2583 if (First_Non_Zero(P->Ray[r]+1+nvar+exist+1, nparam-i-1) != -1)
2584 return -1;
2585 return i;
2588 static evalue* enumerate_remove_ray(Polyhedron *P, int r,
2589 unsigned exist, unsigned nparam, barvinok_options *options)
2591 #ifdef DEBUG_ER
2592 fprintf(stderr, "\nER: RedundantRay\n");
2593 #endif /* DEBUG_ER */
2595 Value one;
2596 value_init(one);
2597 value_set_si(one, 1);
2598 int len = P->NbRays-1;
2599 Matrix *M = Matrix_Alloc(2 * len, P->Dimension+2);
2600 Vector_Copy(P->Ray[0], M->p[0], r * (P->Dimension+2));
2601 Vector_Copy(P->Ray[r+1], M->p[r], (len-r) * (P->Dimension+2));
2602 for (int j = 0; j < P->NbRays; ++j) {
2603 if (j == r)
2604 continue;
2605 Vector_Combine(P->Ray[j], P->Ray[r], M->p[len+j-(j>r)],
2606 one, P->Ray[j][P->Dimension+1], P->Dimension+2);
2609 P = Rays2Polyhedron(M, options->MaxRays);
2610 Matrix_Free(M);
2611 evalue *EP = barvinok_enumerate_e_with_options(P, exist, nparam, options);
2612 Polyhedron_Free(P);
2613 value_clear(one);
2615 return EP;
2618 static evalue* enumerate_redundant_ray(Polyhedron *P,
2619 unsigned exist, unsigned nparam, barvinok_options *options)
2621 assert(P->NbBid == 0);
2622 int nvar = P->Dimension - exist - nparam;
2623 Value m;
2624 value_init(m);
2626 for (int r = 0; r < P->NbRays; ++r) {
2627 if (value_notzero_p(P->Ray[r][P->Dimension+1]))
2628 continue;
2629 int i1 = single_param_pos(P, exist, nparam, r);
2630 if (i1 == -1)
2631 continue;
2632 for (int r2 = r+1; r2 < P->NbRays; ++r2) {
2633 if (value_notzero_p(P->Ray[r2][P->Dimension+1]))
2634 continue;
2635 int i2 = single_param_pos(P, exist, nparam, r2);
2636 if (i2 == -1)
2637 continue;
2638 if (i1 != i2)
2639 continue;
2641 value_division(m, P->Ray[r][1+nvar+exist+i1],
2642 P->Ray[r2][1+nvar+exist+i1]);
2643 value_multiply(m, m, P->Ray[r2][1+nvar+exist+i1]);
2644 /* r2 divides r => r redundant */
2645 if (value_eq(m, P->Ray[r][1+nvar+exist+i1])) {
2646 value_clear(m);
2647 return enumerate_remove_ray(P, r, exist, nparam, options);
2650 value_division(m, P->Ray[r2][1+nvar+exist+i1],
2651 P->Ray[r][1+nvar+exist+i1]);
2652 value_multiply(m, m, P->Ray[r][1+nvar+exist+i1]);
2653 /* r divides r2 => r2 redundant */
2654 if (value_eq(m, P->Ray[r2][1+nvar+exist+i1])) {
2655 value_clear(m);
2656 return enumerate_remove_ray(P, r2, exist, nparam, options);
2660 value_clear(m);
2661 return 0;
2664 static Polyhedron *upper_bound(Polyhedron *P,
2665 int pos, Value *max, Polyhedron **R)
2667 Value v;
2668 int r;
2669 value_init(v);
2671 *R = 0;
2672 Polyhedron *N;
2673 Polyhedron *B = 0;
2674 for (Polyhedron *Q = P; Q; Q = N) {
2675 N = Q->next;
2676 for (r = 0; r < P->NbRays; ++r) {
2677 if (value_zero_p(P->Ray[r][P->Dimension+1]) &&
2678 value_pos_p(P->Ray[r][1+pos]))
2679 break;
2681 if (r < P->NbRays) {
2682 Q->next = *R;
2683 *R = Q;
2684 continue;
2685 } else {
2686 Q->next = B;
2687 B = Q;
2689 for (r = 0; r < P->NbRays; ++r) {
2690 if (value_zero_p(P->Ray[r][P->Dimension+1]))
2691 continue;
2692 mpz_fdiv_q(v, P->Ray[r][1+pos], P->Ray[r][1+P->Dimension]);
2693 if ((!Q->next && r == 0) || value_gt(v, *max))
2694 value_assign(*max, v);
2697 value_clear(v);
2698 return B;
2701 static evalue* enumerate_ray(Polyhedron *P,
2702 unsigned exist, unsigned nparam, barvinok_options *options)
2704 assert(P->NbBid == 0);
2705 int nvar = P->Dimension - exist - nparam;
2707 int r;
2708 for (r = 0; r < P->NbRays; ++r)
2709 if (value_zero_p(P->Ray[r][P->Dimension+1]))
2710 break;
2711 if (r >= P->NbRays)
2712 return 0;
2714 int r2;
2715 for (r2 = r+1; r2 < P->NbRays; ++r2)
2716 if (value_zero_p(P->Ray[r2][P->Dimension+1]))
2717 break;
2718 if (r2 < P->NbRays) {
2719 if (nvar > 0)
2720 return enumerate_sum(P, exist, nparam, options);
2723 #ifdef DEBUG_ER
2724 fprintf(stderr, "\nER: Ray\n");
2725 #endif /* DEBUG_ER */
2727 Value m;
2728 Value one;
2729 value_init(m);
2730 value_init(one);
2731 value_set_si(one, 1);
2732 int i = single_param_pos(P, exist, nparam, r);
2733 assert(i != -1); // for now;
2735 Matrix *M = Matrix_Alloc(P->NbRays, P->Dimension+2);
2736 for (int j = 0; j < P->NbRays; ++j) {
2737 Vector_Combine(P->Ray[j], P->Ray[r], M->p[j],
2738 one, P->Ray[j][P->Dimension+1], P->Dimension+2);
2740 Polyhedron *S = Rays2Polyhedron(M, options->MaxRays);
2741 Matrix_Free(M);
2742 Polyhedron *D = DomainDifference(P, S, options->MaxRays);
2743 Polyhedron_Free(S);
2744 // Polyhedron_Print(stderr, P_VALUE_FMT, D);
2745 assert(value_pos_p(P->Ray[r][1+nvar+exist+i])); // for now
2746 Polyhedron *R;
2747 D = upper_bound(D, nvar+exist+i, &m, &R);
2748 assert(D);
2749 Domain_Free(D);
2751 M = Matrix_Alloc(2, P->Dimension+2);
2752 value_set_si(M->p[0][0], 1);
2753 value_set_si(M->p[1][0], 1);
2754 value_set_si(M->p[0][1+nvar+exist+i], -1);
2755 value_set_si(M->p[1][1+nvar+exist+i], 1);
2756 value_assign(M->p[0][1+P->Dimension], m);
2757 value_oppose(M->p[1][1+P->Dimension], m);
2758 value_addto(M->p[1][1+P->Dimension], M->p[1][1+P->Dimension],
2759 P->Ray[r][1+nvar+exist+i]);
2760 value_decrement(M->p[1][1+P->Dimension], M->p[1][1+P->Dimension]);
2761 // Matrix_Print(stderr, P_VALUE_FMT, M);
2762 D = AddConstraints(M->p[0], 2, P, options->MaxRays);
2763 // Polyhedron_Print(stderr, P_VALUE_FMT, D);
2764 value_subtract(M->p[0][1+P->Dimension], M->p[0][1+P->Dimension],
2765 P->Ray[r][1+nvar+exist+i]);
2766 // Matrix_Print(stderr, P_VALUE_FMT, M);
2767 S = AddConstraints(M->p[0], 1, P, options->MaxRays);
2768 // Polyhedron_Print(stderr, P_VALUE_FMT, S);
2769 Matrix_Free(M);
2771 evalue *EP = barvinok_enumerate_e_with_options(D, exist, nparam, options);
2772 Polyhedron_Free(D);
2773 value_clear(one);
2774 value_clear(m);
2776 if (value_notone_p(P->Ray[r][1+nvar+exist+i]))
2777 EP = enumerate_cyclic(P, exist, nparam, EP, r, i, options->MaxRays);
2778 else {
2779 M = Matrix_Alloc(1, nparam+2);
2780 value_set_si(M->p[0][0], 1);
2781 value_set_si(M->p[0][1+i], 1);
2782 enumerate_vd_add_ray(EP, M, options->MaxRays);
2783 Matrix_Free(M);
2786 if (!emptyQ(S)) {
2787 evalue *E = barvinok_enumerate_e_with_options(S, exist, nparam, options);
2788 eadd(E, EP);
2789 free_evalue_refs(E);
2790 free(E);
2792 Polyhedron_Free(S);
2794 if (R) {
2795 assert(nvar == 0);
2796 evalue *ER = enumerate_or(R, exist, nparam, options);
2797 eor(ER, EP);
2798 free_evalue_refs(ER);
2799 free(ER);
2802 return EP;
2805 static evalue* enumerate_vd(Polyhedron **PA,
2806 unsigned exist, unsigned nparam, barvinok_options *options)
2808 Polyhedron *P = *PA;
2809 int nvar = P->Dimension - exist - nparam;
2810 Param_Polyhedron *PP = NULL;
2811 Polyhedron *C = Universe_Polyhedron(nparam);
2812 Polyhedron *CEq;
2813 Matrix *CT;
2814 Polyhedron *PR = P;
2815 PP = Polyhedron2Param_SimplifiedDomain(&PR,C, options->MaxRays,&CEq,&CT);
2816 Polyhedron_Free(C);
2818 int nd;
2819 Param_Domain *D, *last;
2820 Value c;
2821 value_init(c);
2822 for (nd = 0, D=PP->D; D; D=D->next, ++nd)
2825 Polyhedron **VD = new Polyhedron_p[nd];
2826 Polyhedron **fVD = new Polyhedron_p[nd];
2827 for(nd = 0, D=PP->D; D; D=D->next) {
2828 Polyhedron *rVD = reduce_domain(D->Domain, CT, CEq, fVD, nd, options);
2829 if (!rVD)
2830 continue;
2832 VD[nd++] = rVD;
2833 last = D;
2836 evalue *EP = 0;
2838 if (nd == 0)
2839 EP = evalue_zero();
2841 /* This doesn't seem to have any effect */
2842 if (nd == 1) {
2843 Polyhedron *CA = align_context(VD[0], P->Dimension, options->MaxRays);
2844 Polyhedron *O = P;
2845 P = DomainIntersection(P, CA, options->MaxRays);
2846 if (O != *PA)
2847 Polyhedron_Free(O);
2848 Polyhedron_Free(CA);
2849 if (emptyQ(P))
2850 EP = evalue_zero();
2853 if (!EP && CT->NbColumns != CT->NbRows) {
2854 Polyhedron *CEqr = DomainImage(CEq, CT, options->MaxRays);
2855 Polyhedron *CA = align_context(CEqr, PR->Dimension, options->MaxRays);
2856 Polyhedron *I = DomainIntersection(PR, CA, options->MaxRays);
2857 Polyhedron_Free(CEqr);
2858 Polyhedron_Free(CA);
2859 #ifdef DEBUG_ER
2860 fprintf(stderr, "\nER: Eliminate\n");
2861 #endif /* DEBUG_ER */
2862 nparam -= CT->NbColumns - CT->NbRows;
2863 EP = barvinok_enumerate_e_with_options(I, exist, nparam, options);
2864 nparam += CT->NbColumns - CT->NbRows;
2865 addeliminatedparams_enum(EP, CT, CEq, options->MaxRays, nparam);
2866 Polyhedron_Free(I);
2868 if (PR != *PA)
2869 Polyhedron_Free(PR);
2870 PR = 0;
2872 if (!EP && nd > 1) {
2873 #ifdef DEBUG_ER
2874 fprintf(stderr, "\nER: VD\n");
2875 #endif /* DEBUG_ER */
2876 for (int i = 0; i < nd; ++i) {
2877 Polyhedron *CA = align_context(VD[i], P->Dimension, options->MaxRays);
2878 Polyhedron *I = DomainIntersection(P, CA, options->MaxRays);
2880 if (i == 0)
2881 EP = barvinok_enumerate_e_with_options(I, exist, nparam, options);
2882 else {
2883 evalue *E = barvinok_enumerate_e_with_options(I, exist, nparam,
2884 options);
2885 eadd(E, EP);
2886 free_evalue_refs(E);
2887 free(E);
2889 Polyhedron_Free(I);
2890 Polyhedron_Free(CA);
2894 for (int i = 0; i < nd; ++i) {
2895 Polyhedron_Free(VD[i]);
2896 Polyhedron_Free(fVD[i]);
2898 delete [] VD;
2899 delete [] fVD;
2900 value_clear(c);
2902 if (!EP && nvar == 0) {
2903 Value f;
2904 value_init(f);
2905 Param_Vertices *V, *V2;
2906 Matrix* M = Matrix_Alloc(1, P->Dimension+2);
2908 FORALL_PVertex_in_ParamPolyhedron(V, last, PP) {
2909 bool found = false;
2910 FORALL_PVertex_in_ParamPolyhedron(V2, last, PP) {
2911 if (V == V2) {
2912 found = true;
2913 continue;
2915 if (!found)
2916 continue;
2917 for (int i = 0; i < exist; ++i) {
2918 value_oppose(f, V->Vertex->p[i][nparam+1]);
2919 Vector_Combine(V->Vertex->p[i],
2920 V2->Vertex->p[i],
2921 M->p[0] + 1 + nvar + exist,
2922 V2->Vertex->p[i][nparam+1],
2924 nparam+1);
2925 int j;
2926 for (j = 0; j < nparam; ++j)
2927 if (value_notzero_p(M->p[0][1+nvar+exist+j]))
2928 break;
2929 if (j >= nparam)
2930 continue;
2931 ConstraintSimplify(M->p[0], M->p[0],
2932 P->Dimension+2, &f);
2933 value_set_si(M->p[0][0], 0);
2934 Polyhedron *para = AddConstraints(M->p[0], 1, P,
2935 options->MaxRays);
2936 if (emptyQ(para)) {
2937 Polyhedron_Free(para);
2938 continue;
2940 Polyhedron *pos, *neg;
2941 value_set_si(M->p[0][0], 1);
2942 value_decrement(M->p[0][P->Dimension+1],
2943 M->p[0][P->Dimension+1]);
2944 neg = AddConstraints(M->p[0], 1, P, options->MaxRays);
2945 value_set_si(f, -1);
2946 Vector_Scale(M->p[0]+1, M->p[0]+1, f,
2947 P->Dimension+1);
2948 value_decrement(M->p[0][P->Dimension+1],
2949 M->p[0][P->Dimension+1]);
2950 value_decrement(M->p[0][P->Dimension+1],
2951 M->p[0][P->Dimension+1]);
2952 pos = AddConstraints(M->p[0], 1, P, options->MaxRays);
2953 if (emptyQ(neg) && emptyQ(pos)) {
2954 Polyhedron_Free(para);
2955 Polyhedron_Free(pos);
2956 Polyhedron_Free(neg);
2957 continue;
2959 #ifdef DEBUG_ER
2960 fprintf(stderr, "\nER: Order\n");
2961 #endif /* DEBUG_ER */
2962 EP = barvinok_enumerate_e_with_options(para, exist, nparam,
2963 options);
2964 evalue *E;
2965 if (!emptyQ(pos)) {
2966 E = barvinok_enumerate_e_with_options(pos, exist, nparam,
2967 options);
2968 eadd(E, EP);
2969 free_evalue_refs(E);
2970 free(E);
2972 if (!emptyQ(neg)) {
2973 E = barvinok_enumerate_e_with_options(neg, exist, nparam,
2974 options);
2975 eadd(E, EP);
2976 free_evalue_refs(E);
2977 free(E);
2979 Polyhedron_Free(para);
2980 Polyhedron_Free(pos);
2981 Polyhedron_Free(neg);
2982 break;
2984 if (EP)
2985 break;
2986 } END_FORALL_PVertex_in_ParamPolyhedron;
2987 if (EP)
2988 break;
2989 } END_FORALL_PVertex_in_ParamPolyhedron;
2991 if (!EP) {
2992 /* Search for vertex coordinate to split on */
2993 /* First look for one independent of the parameters */
2994 FORALL_PVertex_in_ParamPolyhedron(V, last, PP) {
2995 for (int i = 0; i < exist; ++i) {
2996 int j;
2997 for (j = 0; j < nparam; ++j)
2998 if (value_notzero_p(V->Vertex->p[i][j]))
2999 break;
3000 if (j < nparam)
3001 continue;
3002 value_set_si(M->p[0][0], 1);
3003 Vector_Set(M->p[0]+1, 0, nvar+exist);
3004 Vector_Copy(V->Vertex->p[i],
3005 M->p[0] + 1 + nvar + exist, nparam+1);
3006 value_oppose(M->p[0][1+nvar+i],
3007 V->Vertex->p[i][nparam+1]);
3009 Polyhedron *pos, *neg;
3010 value_set_si(M->p[0][0], 1);
3011 value_decrement(M->p[0][P->Dimension+1],
3012 M->p[0][P->Dimension+1]);
3013 neg = AddConstraints(M->p[0], 1, P, options->MaxRays);
3014 value_set_si(f, -1);
3015 Vector_Scale(M->p[0]+1, M->p[0]+1, f,
3016 P->Dimension+1);
3017 value_decrement(M->p[0][P->Dimension+1],
3018 M->p[0][P->Dimension+1]);
3019 value_decrement(M->p[0][P->Dimension+1],
3020 M->p[0][P->Dimension+1]);
3021 pos = AddConstraints(M->p[0], 1, P, options->MaxRays);
3022 if (emptyQ(neg) || emptyQ(pos)) {
3023 Polyhedron_Free(pos);
3024 Polyhedron_Free(neg);
3025 continue;
3027 Polyhedron_Free(pos);
3028 value_increment(M->p[0][P->Dimension+1],
3029 M->p[0][P->Dimension+1]);
3030 pos = AddConstraints(M->p[0], 1, P, options->MaxRays);
3031 #ifdef DEBUG_ER
3032 fprintf(stderr, "\nER: Vertex\n");
3033 #endif /* DEBUG_ER */
3034 pos->next = neg;
3035 EP = enumerate_or(pos, exist, nparam, options);
3036 break;
3038 if (EP)
3039 break;
3040 } END_FORALL_PVertex_in_ParamPolyhedron;
3043 if (!EP) {
3044 /* Search for vertex coordinate to split on */
3045 /* Now look for one that depends on the parameters */
3046 FORALL_PVertex_in_ParamPolyhedron(V, last, PP) {
3047 for (int i = 0; i < exist; ++i) {
3048 value_set_si(M->p[0][0], 1);
3049 Vector_Set(M->p[0]+1, 0, nvar+exist);
3050 Vector_Copy(V->Vertex->p[i],
3051 M->p[0] + 1 + nvar + exist, nparam+1);
3052 value_oppose(M->p[0][1+nvar+i],
3053 V->Vertex->p[i][nparam+1]);
3055 Polyhedron *pos, *neg;
3056 value_set_si(M->p[0][0], 1);
3057 value_decrement(M->p[0][P->Dimension+1],
3058 M->p[0][P->Dimension+1]);
3059 neg = AddConstraints(M->p[0], 1, P, options->MaxRays);
3060 value_set_si(f, -1);
3061 Vector_Scale(M->p[0]+1, M->p[0]+1, f,
3062 P->Dimension+1);
3063 value_decrement(M->p[0][P->Dimension+1],
3064 M->p[0][P->Dimension+1]);
3065 value_decrement(M->p[0][P->Dimension+1],
3066 M->p[0][P->Dimension+1]);
3067 pos = AddConstraints(M->p[0], 1, P, options->MaxRays);
3068 if (emptyQ(neg) || emptyQ(pos)) {
3069 Polyhedron_Free(pos);
3070 Polyhedron_Free(neg);
3071 continue;
3073 Polyhedron_Free(pos);
3074 value_increment(M->p[0][P->Dimension+1],
3075 M->p[0][P->Dimension+1]);
3076 pos = AddConstraints(M->p[0], 1, P, options->MaxRays);
3077 #ifdef DEBUG_ER
3078 fprintf(stderr, "\nER: ParamVertex\n");
3079 #endif /* DEBUG_ER */
3080 pos->next = neg;
3081 EP = enumerate_or(pos, exist, nparam, options);
3082 break;
3084 if (EP)
3085 break;
3086 } END_FORALL_PVertex_in_ParamPolyhedron;
3089 Matrix_Free(M);
3090 value_clear(f);
3093 if (CEq)
3094 Polyhedron_Free(CEq);
3095 if (CT)
3096 Matrix_Free(CT);
3097 if (PP)
3098 Param_Polyhedron_Free(PP);
3099 *PA = P;
3101 return EP;
3104 evalue* barvinok_enumerate_pip(Polyhedron *P, unsigned exist, unsigned nparam,
3105 unsigned MaxRays)
3107 evalue *E;
3108 barvinok_options *options = barvinok_options_new_with_defaults();
3109 options->MaxRays = MaxRays;
3110 E = barvinok_enumerate_pip_with_options(P, exist, nparam, options);
3111 barvinok_options_free(options);
3112 return E;
3115 #ifndef HAVE_PIPLIB
3116 evalue *barvinok_enumerate_pip_with_options(Polyhedron *P,
3117 unsigned exist, unsigned nparam, struct barvinok_options *options)
3119 return 0;
3121 #else
3122 evalue *barvinok_enumerate_pip_with_options(Polyhedron *P,
3123 unsigned exist, unsigned nparam, struct barvinok_options *options)
3125 int nvar = P->Dimension - exist - nparam;
3126 evalue *EP = evalue_zero();
3127 Polyhedron *Q, *N;
3129 #ifdef DEBUG_ER
3130 fprintf(stderr, "\nER: PIP\n");
3131 #endif /* DEBUG_ER */
3133 Polyhedron *D = pip_projectout(P, nvar, exist, nparam);
3134 for (Q = D; Q; Q = N) {
3135 N = Q->next;
3136 Q->next = 0;
3137 evalue *E;
3138 exist = Q->Dimension - nvar - nparam;
3139 E = barvinok_enumerate_e_with_options(Q, exist, nparam, options);
3140 Polyhedron_Free(Q);
3141 eadd(E, EP);
3142 free_evalue_refs(E);
3143 free(E);
3146 return EP;
3148 #endif
3151 static bool is_single(Value *row, int pos, int len)
3153 return First_Non_Zero(row, pos) == -1 &&
3154 First_Non_Zero(row+pos+1, len-pos-1) == -1;
3157 static evalue* barvinok_enumerate_e_r(Polyhedron *P,
3158 unsigned exist, unsigned nparam, barvinok_options *options);
3160 #ifdef DEBUG_ER
3161 static int er_level = 0;
3163 evalue* barvinok_enumerate_e_with_options(Polyhedron *P,
3164 unsigned exist, unsigned nparam, barvinok_options *options)
3166 fprintf(stderr, "\nER: level %i\n", er_level);
3168 Polyhedron_PrintConstraints(stderr, P_VALUE_FMT, P);
3169 fprintf(stderr, "\nE %d\nP %d\n", exist, nparam);
3170 ++er_level;
3171 P = DomainConstraintSimplify(Polyhedron_Copy(P), options->MaxRays);
3172 evalue *EP = barvinok_enumerate_e_r(P, exist, nparam, options);
3173 Polyhedron_Free(P);
3174 --er_level;
3175 return EP;
3177 #else
3178 evalue* barvinok_enumerate_e_with_options(Polyhedron *P,
3179 unsigned exist, unsigned nparam, barvinok_options *options)
3181 P = DomainConstraintSimplify(Polyhedron_Copy(P), options->MaxRays);
3182 evalue *EP = barvinok_enumerate_e_r(P, exist, nparam, options);
3183 Polyhedron_Free(P);
3184 return EP;
3186 #endif
3188 evalue* barvinok_enumerate_e(Polyhedron *P, unsigned exist, unsigned nparam,
3189 unsigned MaxRays)
3191 evalue *E;
3192 barvinok_options *options = barvinok_options_new_with_defaults();
3193 options->MaxRays = MaxRays;
3194 E = barvinok_enumerate_e_with_options(P, exist, nparam, options);
3195 barvinok_options_free(options);
3196 return E;
3199 static evalue* barvinok_enumerate_e_r(Polyhedron *P,
3200 unsigned exist, unsigned nparam, barvinok_options *options)
3202 if (exist == 0) {
3203 Polyhedron *U = Universe_Polyhedron(nparam);
3204 evalue *EP = barvinok_enumerate_with_options(P, U, options);
3205 //char *param_name[] = {"P", "Q", "R", "S", "T" };
3206 //print_evalue(stdout, EP, param_name);
3207 Polyhedron_Free(U);
3208 return EP;
3211 int nvar = P->Dimension - exist - nparam;
3212 int len = P->Dimension + 2;
3214 /* for now */
3215 POL_ENSURE_FACETS(P);
3216 POL_ENSURE_VERTICES(P);
3218 if (emptyQ(P))
3219 return evalue_zero();
3221 if (nvar == 0 && nparam == 0) {
3222 evalue *EP = evalue_zero();
3223 barvinok_count_with_options(P, &EP->x.n, options);
3224 if (value_pos_p(EP->x.n))
3225 value_set_si(EP->x.n, 1);
3226 return EP;
3229 int r;
3230 for (r = 0; r < P->NbRays; ++r)
3231 if (value_zero_p(P->Ray[r][0]) ||
3232 value_zero_p(P->Ray[r][P->Dimension+1])) {
3233 int i;
3234 for (i = 0; i < nvar; ++i)
3235 if (value_notzero_p(P->Ray[r][i+1]))
3236 break;
3237 if (i >= nvar)
3238 continue;
3239 for (i = nvar + exist; i < nvar + exist + nparam; ++i)
3240 if (value_notzero_p(P->Ray[r][i+1]))
3241 break;
3242 if (i >= nvar + exist + nparam)
3243 break;
3245 if (r < P->NbRays) {
3246 evalue *EP = evalue_zero();
3247 value_set_si(EP->x.n, -1);
3248 return EP;
3251 int first;
3252 for (r = 0; r < P->NbEq; ++r)
3253 if ((first = First_Non_Zero(P->Constraint[r]+1+nvar, exist)) != -1)
3254 break;
3255 if (r < P->NbEq) {
3256 if (First_Non_Zero(P->Constraint[r]+1+nvar+first+1,
3257 exist-first-1) != -1) {
3258 Polyhedron *T = rotate_along(P, r, nvar, exist, options->MaxRays);
3259 #ifdef DEBUG_ER
3260 fprintf(stderr, "\nER: Equality\n");
3261 #endif /* DEBUG_ER */
3262 evalue *EP = barvinok_enumerate_e_with_options(T, exist-1, nparam,
3263 options);
3264 Polyhedron_Free(T);
3265 return EP;
3266 } else {
3267 #ifdef DEBUG_ER
3268 fprintf(stderr, "\nER: Fixed\n");
3269 #endif /* DEBUG_ER */
3270 if (first == 0)
3271 return barvinok_enumerate_e_with_options(P, exist-1, nparam,
3272 options);
3273 else {
3274 Polyhedron *T = Polyhedron_Copy(P);
3275 SwapColumns(T, nvar+1, nvar+1+first);
3276 evalue *EP = barvinok_enumerate_e_with_options(T, exist-1, nparam,
3277 options);
3278 Polyhedron_Free(T);
3279 return EP;
3284 Vector *row = Vector_Alloc(len);
3285 value_set_si(row->p[0], 1);
3287 Value f;
3288 value_init(f);
3290 enum constraint* info = new constraint[exist];
3291 for (int i = 0; i < exist; ++i) {
3292 info[i] = ALL_POS;
3293 for (int l = P->NbEq; l < P->NbConstraints; ++l) {
3294 if (value_negz_p(P->Constraint[l][nvar+i+1]))
3295 continue;
3296 bool l_parallel = is_single(P->Constraint[l]+nvar+1, i, exist);
3297 for (int u = P->NbEq; u < P->NbConstraints; ++u) {
3298 if (value_posz_p(P->Constraint[u][nvar+i+1]))
3299 continue;
3300 bool lu_parallel = l_parallel ||
3301 is_single(P->Constraint[u]+nvar+1, i, exist);
3302 value_oppose(f, P->Constraint[u][nvar+i+1]);
3303 Vector_Combine(P->Constraint[l]+1, P->Constraint[u]+1, row->p+1,
3304 f, P->Constraint[l][nvar+i+1], len-1);
3305 if (!(info[i] & INDEPENDENT)) {
3306 int j;
3307 for (j = 0; j < exist; ++j)
3308 if (j != i && value_notzero_p(row->p[nvar+j+1]))
3309 break;
3310 if (j == exist) {
3311 //printf("independent: i: %d, l: %d, u: %d\n", i, l, u);
3312 info[i] = (constraint)(info[i] | INDEPENDENT);
3315 if (info[i] & ALL_POS) {
3316 value_addto(row->p[len-1], row->p[len-1],
3317 P->Constraint[l][nvar+i+1]);
3318 value_addto(row->p[len-1], row->p[len-1], f);
3319 value_multiply(f, f, P->Constraint[l][nvar+i+1]);
3320 value_subtract(row->p[len-1], row->p[len-1], f);
3321 value_decrement(row->p[len-1], row->p[len-1]);
3322 ConstraintSimplify(row->p, row->p, len, &f);
3323 value_set_si(f, -1);
3324 Vector_Scale(row->p+1, row->p+1, f, len-1);
3325 value_decrement(row->p[len-1], row->p[len-1]);
3326 Polyhedron *T = AddConstraints(row->p, 1, P, options->MaxRays);
3327 if (!emptyQ(T)) {
3328 //printf("not all_pos: i: %d, l: %d, u: %d\n", i, l, u);
3329 info[i] = (constraint)(info[i] ^ ALL_POS);
3331 //puts("pos remainder");
3332 //Polyhedron_Print(stdout, P_VALUE_FMT, T);
3333 Polyhedron_Free(T);
3335 if (!(info[i] & ONE_NEG)) {
3336 if (lu_parallel) {
3337 negative_test_constraint(P->Constraint[l],
3338 P->Constraint[u],
3339 row->p, nvar+i, len, &f);
3340 oppose_constraint(row->p, len, &f);
3341 Polyhedron *T = AddConstraints(row->p, 1, P,
3342 options->MaxRays);
3343 if (emptyQ(T)) {
3344 //printf("one_neg i: %d, l: %d, u: %d\n", i, l, u);
3345 info[i] = (constraint)(info[i] | ONE_NEG);
3347 //puts("neg remainder");
3348 //Polyhedron_Print(stdout, P_VALUE_FMT, T);
3349 Polyhedron_Free(T);
3350 } else if (!(info[i] & ROT_NEG)) {
3351 if (parallel_constraints(P->Constraint[l],
3352 P->Constraint[u],
3353 row->p, nvar, exist)) {
3354 negative_test_constraint7(P->Constraint[l],
3355 P->Constraint[u],
3356 row->p, nvar, exist,
3357 len, &f);
3358 oppose_constraint(row->p, len, &f);
3359 Polyhedron *T = AddConstraints(row->p, 1, P,
3360 options->MaxRays);
3361 if (emptyQ(T)) {
3362 // printf("rot_neg i: %d, l: %d, u: %d\n", i, l, u);
3363 info[i] = (constraint)(info[i] | ROT_NEG);
3364 r = l;
3366 //puts("neg remainder");
3367 //Polyhedron_Print(stdout, P_VALUE_FMT, T);
3368 Polyhedron_Free(T);
3372 if (!(info[i] & ALL_POS) && (info[i] & (ONE_NEG | ROT_NEG)))
3373 goto next;
3376 if (info[i] & ALL_POS)
3377 break;
3378 next:
3383 for (int i = 0; i < exist; ++i)
3384 printf("%i: %i\n", i, info[i]);
3386 for (int i = 0; i < exist; ++i)
3387 if (info[i] & ALL_POS) {
3388 #ifdef DEBUG_ER
3389 fprintf(stderr, "\nER: Positive\n");
3390 #endif /* DEBUG_ER */
3391 // Eliminate
3392 // Maybe we should chew off some of the fat here
3393 Matrix *M = Matrix_Alloc(P->Dimension, P->Dimension+1);
3394 for (int j = 0; j < P->Dimension; ++j)
3395 value_set_si(M->p[j][j + (j >= i+nvar)], 1);
3396 Polyhedron *T = Polyhedron_Image(P, M, options->MaxRays);
3397 Matrix_Free(M);
3398 evalue *EP = barvinok_enumerate_e_with_options(T, exist-1, nparam,
3399 options);
3400 Polyhedron_Free(T);
3401 value_clear(f);
3402 Vector_Free(row);
3403 delete [] info;
3404 return EP;
3406 for (int i = 0; i < exist; ++i)
3407 if (info[i] & ONE_NEG) {
3408 #ifdef DEBUG_ER
3409 fprintf(stderr, "\nER: Negative\n");
3410 #endif /* DEBUG_ER */
3411 Vector_Free(row);
3412 value_clear(f);
3413 delete [] info;
3414 if (i == 0)
3415 return barvinok_enumerate_e_with_options(P, exist-1, nparam,
3416 options);
3417 else {
3418 Polyhedron *T = Polyhedron_Copy(P);
3419 SwapColumns(T, nvar+1, nvar+1+i);
3420 evalue *EP = barvinok_enumerate_e_with_options(T, exist-1, nparam,
3421 options);
3422 Polyhedron_Free(T);
3423 return EP;
3426 for (int i = 0; i < exist; ++i)
3427 if (info[i] & ROT_NEG) {
3428 #ifdef DEBUG_ER
3429 fprintf(stderr, "\nER: Rotate\n");
3430 #endif /* DEBUG_ER */
3431 Vector_Free(row);
3432 value_clear(f);
3433 delete [] info;
3434 Polyhedron *T = rotate_along(P, r, nvar, exist, options->MaxRays);
3435 evalue *EP = barvinok_enumerate_e_with_options(T, exist-1, nparam,
3436 options);
3437 Polyhedron_Free(T);
3438 return EP;
3440 for (int i = 0; i < exist; ++i)
3441 if (info[i] & INDEPENDENT) {
3442 Polyhedron *pos, *neg;
3444 /* Find constraint again and split off negative part */
3446 if (SplitOnVar(P, i, nvar, exist, options->MaxRays,
3447 row, f, true, &pos, &neg)) {
3448 #ifdef DEBUG_ER
3449 fprintf(stderr, "\nER: Split\n");
3450 #endif /* DEBUG_ER */
3452 evalue *EP =
3453 barvinok_enumerate_e_with_options(neg, exist-1, nparam, options);
3454 evalue *E =
3455 barvinok_enumerate_e_with_options(pos, exist, nparam, options);
3456 eadd(E, EP);
3457 free_evalue_refs(E);
3458 free(E);
3459 Polyhedron_Free(neg);
3460 Polyhedron_Free(pos);
3461 value_clear(f);
3462 Vector_Free(row);
3463 delete [] info;
3464 return EP;
3467 delete [] info;
3469 Polyhedron *O = P;
3470 Polyhedron *F;
3472 evalue *EP;
3474 EP = enumerate_line(P, exist, nparam, options);
3475 if (EP)
3476 goto out;
3478 EP = barvinok_enumerate_pip_with_options(P, exist, nparam, options);
3479 if (EP)
3480 goto out;
3482 EP = enumerate_redundant_ray(P, exist, nparam, options);
3483 if (EP)
3484 goto out;
3486 EP = enumerate_sure(P, exist, nparam, options);
3487 if (EP)
3488 goto out;
3490 EP = enumerate_ray(P, exist, nparam, options);
3491 if (EP)
3492 goto out;
3494 EP = enumerate_sure2(P, exist, nparam, options);
3495 if (EP)
3496 goto out;
3498 F = unfringe(P, options->MaxRays);
3499 if (!PolyhedronIncludes(F, P)) {
3500 #ifdef DEBUG_ER
3501 fprintf(stderr, "\nER: Fringed\n");
3502 #endif /* DEBUG_ER */
3503 EP = barvinok_enumerate_e_with_options(F, exist, nparam, options);
3504 Polyhedron_Free(F);
3505 goto out;
3507 Polyhedron_Free(F);
3509 if (nparam)
3510 EP = enumerate_vd(&P, exist, nparam, options);
3511 if (EP)
3512 goto out2;
3514 if (nvar != 0) {
3515 EP = enumerate_sum(P, exist, nparam, options);
3516 goto out2;
3519 assert(nvar == 0);
3521 int i;
3522 Polyhedron *pos, *neg;
3523 for (i = 0; i < exist; ++i)
3524 if (SplitOnVar(P, i, nvar, exist, options->MaxRays,
3525 row, f, false, &pos, &neg))
3526 break;
3528 assert (i < exist);
3530 pos->next = neg;
3531 EP = enumerate_or(pos, exist, nparam, options);
3533 out2:
3534 if (O != P)
3535 Polyhedron_Free(P);
3537 out:
3538 value_clear(f);
3539 Vector_Free(row);
3540 return EP;
3544 * remove equalities that require a "compression" of the parameters
3546 static Polyhedron *remove_more_equalities(Polyhedron *P, unsigned nparam,
3547 Matrix **CP, unsigned MaxRays)
3549 Polyhedron *Q = P;
3550 remove_all_equalities(&P, NULL, CP, NULL, nparam, MaxRays);
3551 if (P != Q)
3552 Polyhedron_Free(Q);
3553 return P;
3556 /* frees P */
3557 static gen_fun *series(Polyhedron *P, unsigned nparam, barvinok_options *options)
3559 Matrix *CP = NULL;
3560 gen_fun *gf;
3562 if (emptyQ2(P)) {
3563 Polyhedron_Free(P);
3564 return new gen_fun;
3567 assert(!Polyhedron_is_infinite_param(P, nparam));
3568 assert(P->NbBid == 0);
3569 assert(Polyhedron_has_revlex_positive_rays(P, nparam));
3570 if (P->NbEq != 0)
3571 P = remove_more_equalities(P, nparam, &CP, options->MaxRays);
3572 assert(P->NbEq == 0);
3573 if (CP)
3574 nparam = CP->NbColumns-1;
3576 if (nparam == 0) {
3577 Value c;
3578 value_init(c);
3579 barvinok_count_with_options(P, &c, options);
3580 gf = new gen_fun(c);
3581 value_clear(c);
3582 } else {
3583 gf_base *red;
3584 red = gf_base::create(Polyhedron_Project(P, nparam),
3585 P->Dimension, nparam, options);
3586 POL_ENSURE_VERTICES(P);
3587 red->start_gf(P, options);
3588 gf = red->gf;
3589 delete red;
3591 if (CP) {
3592 gf->substitute(CP);
3593 Matrix_Free(CP);
3595 Polyhedron_Free(P);
3596 return gf;
3599 gen_fun * barvinok_series_with_options(Polyhedron *P, Polyhedron* C,
3600 barvinok_options *options)
3602 Polyhedron *CA;
3603 unsigned nparam = C->Dimension;
3604 gen_fun *gf;
3606 CA = align_context(C, P->Dimension, options->MaxRays);
3607 P = DomainIntersection(P, CA, options->MaxRays);
3608 Polyhedron_Free(CA);
3610 gf = series(P, nparam, options);
3612 return gf;
3615 gen_fun * barvinok_series(Polyhedron *P, Polyhedron* C, unsigned MaxRays)
3617 gen_fun *gf;
3618 barvinok_options *options = barvinok_options_new_with_defaults();
3619 options->MaxRays = MaxRays;
3620 gf = barvinok_series_with_options(P, C, options);
3621 barvinok_options_free(options);
3622 return gf;
3625 static Polyhedron *skew_into_positive_orthant(Polyhedron *D, unsigned nparam,
3626 unsigned MaxRays)
3628 Matrix *M = NULL;
3629 Value tmp;
3630 value_init(tmp);
3631 for (Polyhedron *P = D; P; P = P->next) {
3632 POL_ENSURE_VERTICES(P);
3633 assert(!Polyhedron_is_infinite_param(P, nparam));
3634 assert(P->NbBid == 0);
3635 assert(Polyhedron_has_positive_rays(P, nparam));
3637 for (int r = 0; r < P->NbRays; ++r) {
3638 if (value_notzero_p(P->Ray[r][P->Dimension+1]))
3639 continue;
3640 for (int i = 0; i < nparam; ++i) {
3641 int j;
3642 if (value_posz_p(P->Ray[r][i+1]))
3643 continue;
3644 if (!M) {
3645 M = Matrix_Alloc(D->Dimension+1, D->Dimension+1);
3646 for (int i = 0; i < D->Dimension+1; ++i)
3647 value_set_si(M->p[i][i], 1);
3648 } else {
3649 Inner_Product(P->Ray[r]+1, M->p[i], D->Dimension+1, &tmp);
3650 if (value_posz_p(tmp))
3651 continue;
3653 for (j = P->Dimension - nparam; j < P->Dimension; ++j)
3654 if (value_pos_p(P->Ray[r][j+1]))
3655 break;
3656 assert(j < P->Dimension);
3657 value_pdivision(tmp, P->Ray[r][j+1], P->Ray[r][i+1]);
3658 value_subtract(M->p[i][j], M->p[i][j], tmp);
3662 value_clear(tmp);
3663 if (M) {
3664 D = DomainImage(D, M, MaxRays);
3665 Matrix_Free(M);
3667 return D;
3670 gen_fun* barvinok_enumerate_union_series_with_options(Polyhedron *D, Polyhedron* C,
3671 barvinok_options *options)
3673 Polyhedron *conv, *D2;
3674 Polyhedron *CA;
3675 gen_fun *gf = NULL, *gf2;
3676 unsigned nparam = C->Dimension;
3677 ZZ one, mone;
3678 one = 1;
3679 mone = -1;
3681 CA = align_context(C, D->Dimension, options->MaxRays);
3682 D = DomainIntersection(D, CA, options->MaxRays);
3683 Polyhedron_Free(CA);
3685 D2 = skew_into_positive_orthant(D, nparam, options->MaxRays);
3686 for (Polyhedron *P = D2; P; P = P->next) {
3687 assert(P->Dimension == D2->Dimension);
3688 gen_fun *P_gf;
3690 P_gf = series(Polyhedron_Copy(P), nparam, options);
3691 if (!gf)
3692 gf = P_gf;
3693 else {
3694 gf->add_union(P_gf, options);
3695 delete P_gf;
3698 /* we actually only need the convex union of the parameter space
3699 * but the reducer classes currently expect a polyhedron in
3700 * the combined space
3702 Polyhedron_Free(gf->context);
3703 gf->context = DomainConvex(D2, options->MaxRays);
3705 gf2 = gf->summate(D2->Dimension - nparam, options);
3707 delete gf;
3708 if (D != D2)
3709 Domain_Free(D2);
3710 Domain_Free(D);
3711 return gf2;
3714 gen_fun* barvinok_enumerate_union_series(Polyhedron *D, Polyhedron* C,
3715 unsigned MaxRays)
3717 gen_fun *gf;
3718 barvinok_options *options = barvinok_options_new_with_defaults();
3719 options->MaxRays = MaxRays;
3720 gf = barvinok_enumerate_union_series_with_options(D, C, options);
3721 barvinok_options_free(options);
3722 return gf;
3725 evalue* barvinok_enumerate_union(Polyhedron *D, Polyhedron* C, unsigned MaxRays)
3727 evalue *EP;
3728 gen_fun *gf = barvinok_enumerate_union_series(D, C, MaxRays);
3729 EP = *gf;
3730 delete gf;
3731 return EP;