reorganize polynomial approximation options
[barvinok.git] / barvinok.cc
blob6ec712217ff09a3c2954b8fbafedeb3fdaff77e8
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 (P->next)
705 fprintf(stderr,
706 "barvinok_count: input is a union; only first polyhedron is counted\n");
708 if (emptyQ2(P)) {
709 value_set_si(*result, 0);
710 return;
712 if (P->NbEq != 0) {
713 Q = NULL;
714 do {
715 P = remove_equalities(P);
716 P = DomainConstraintSimplify(P, options->MaxRays);
717 if (Q)
718 Polyhedron_Free(Q);
719 Q = P;
720 } while (!emptyQ(P) && P->NbEq != 0);
721 if (emptyQ(P)) {
722 Polyhedron_Free(P);
723 value_set_si(*result, 0);
724 return;
726 allocated = 1;
728 if (Polyhedron_is_infinite(P, result, options)) {
729 if (allocated)
730 Polyhedron_Free(P);
731 return;
733 if (P->Dimension == 0) {
734 /* Test whether the constraints are satisfied */
735 POL_ENSURE_VERTICES(P);
736 value_set_si(*result, !emptyQ(P));
737 if (allocated)
738 Polyhedron_Free(P);
739 return;
741 Q = Polyhedron_Factor(P, 0, NULL, options->MaxRays);
742 if (Q) {
743 if (allocated)
744 Polyhedron_Free(P);
745 P = Q;
746 allocated = 1;
749 barvinok_count_f(P, result, options);
750 if (value_neg_p(*result))
751 infinite = true;
752 if (Q && P->next && value_notzero_p(*result)) {
753 Value factor;
754 value_init(factor);
756 for (Q = P->next; Q; Q = Q->next) {
757 barvinok_count_f(Q, &factor, options);
758 if (value_neg_p(factor)) {
759 infinite = true;
760 continue;
761 } else if (Q->next && value_zero_p(factor)) {
762 value_set_si(*result, 0);
763 break;
765 value_multiply(*result, *result, factor);
768 value_clear(factor);
771 if (allocated)
772 Domain_Free(P);
773 if (infinite)
774 value_set_si(*result, -1);
777 void barvinok_count(Polyhedron *P, Value* result, unsigned NbMaxCons)
779 barvinok_options *options = barvinok_options_new_with_defaults();
780 options->MaxRays = NbMaxCons;
781 barvinok_count_with_options(P, result, options);
782 barvinok_options_free(options);
785 static void barvinok_count_f(Polyhedron *P, Value* result,
786 barvinok_options *options)
788 if (emptyQ2(P)) {
789 value_set_si(*result, 0);
790 return;
793 if (P->Dimension == 1)
794 return Line_Length(P, result);
796 int c = P->NbConstraints;
797 POL_ENSURE_FACETS(P);
798 if (c != P->NbConstraints || P->NbEq != 0)
799 return barvinok_count_with_options(P, result, options);
801 POL_ENSURE_VERTICES(P);
803 if (Polyhedron_is_infinite(P, result, options))
804 return;
806 np_base *cnt;
807 if (options->incremental_specialization == 2)
808 cnt = new bfcounter(P->Dimension);
809 else if (options->incremental_specialization == 1)
810 cnt = new icounter(P->Dimension);
811 else
812 cnt = new counter(P->Dimension);
813 cnt->start(P, options);
815 cnt->get_count(result);
816 delete cnt;
819 static void uni_polynom(int param, Vector *c, evalue *EP)
821 unsigned dim = c->Size-2;
822 value_init(EP->d);
823 value_set_si(EP->d,0);
824 EP->x.p = new_enode(polynomial, dim+1, param+1);
825 for (int j = 0; j <= dim; ++j)
826 evalue_set(&EP->x.p->arr[j], c->p[j], c->p[dim+1]);
829 static void multi_polynom(Vector *c, evalue* X, evalue *EP)
831 unsigned dim = c->Size-2;
832 evalue EC;
834 value_init(EC.d);
835 evalue_set(&EC, c->p[dim], c->p[dim+1]);
837 value_init(EP->d);
838 evalue_set(EP, c->p[dim], c->p[dim+1]);
840 for (int i = dim-1; i >= 0; --i) {
841 emul(X, EP);
842 value_assign(EC.x.n, c->p[i]);
843 eadd(&EC, EP);
845 free_evalue_refs(&EC);
848 Polyhedron *unfringe (Polyhedron *P, unsigned MaxRays)
850 int len = P->Dimension+2;
851 Polyhedron *T, *R = P;
852 Value g;
853 value_init(g);
854 Vector *row = Vector_Alloc(len);
855 value_set_si(row->p[0], 1);
857 R = DomainConstraintSimplify(Polyhedron_Copy(P), MaxRays);
859 Matrix *M = Matrix_Alloc(2, len-1);
860 value_set_si(M->p[1][len-2], 1);
861 for (int v = 0; v < P->Dimension; ++v) {
862 value_set_si(M->p[0][v], 1);
863 Polyhedron *I = Polyhedron_Image(R, M, 2+1);
864 value_set_si(M->p[0][v], 0);
865 for (int r = 0; r < I->NbConstraints; ++r) {
866 if (value_zero_p(I->Constraint[r][0]))
867 continue;
868 if (value_zero_p(I->Constraint[r][1]))
869 continue;
870 if (value_one_p(I->Constraint[r][1]))
871 continue;
872 if (value_mone_p(I->Constraint[r][1]))
873 continue;
874 value_absolute(g, I->Constraint[r][1]);
875 Vector_Set(row->p+1, 0, len-2);
876 value_division(row->p[1+v], I->Constraint[r][1], g);
877 mpz_fdiv_q(row->p[len-1], I->Constraint[r][2], g);
878 T = R;
879 R = AddConstraints(row->p, 1, R, MaxRays);
880 if (T != P)
881 Polyhedron_Free(T);
883 Polyhedron_Free(I);
885 Matrix_Free(M);
886 Vector_Free(row);
887 value_clear(g);
888 return R;
891 /* Check whether all rays point in the positive directions
892 * for the parameters
894 static bool Polyhedron_has_positive_rays(Polyhedron *P, unsigned nparam)
896 int r;
897 for (r = 0; r < P->NbRays; ++r)
898 if (value_zero_p(P->Ray[r][P->Dimension+1])) {
899 int i;
900 for (i = P->Dimension - nparam; i < P->Dimension; ++i)
901 if (value_neg_p(P->Ray[r][i+1]))
902 return false;
904 return true;
907 typedef evalue * evalue_p;
909 struct enumerator_base {
910 unsigned dim;
911 evalue ** vE;
912 evalue mone;
913 vertex_decomposer *vpd;
915 enumerator_base(unsigned dim, vertex_decomposer *vpd)
917 this->dim = dim;
918 this->vpd = vpd;
920 vE = new evalue_p[vpd->nbV];
921 for (int j = 0; j < vpd->nbV; ++j)
922 vE[j] = 0;
924 value_init(mone.d);
925 evalue_set_si(&mone, -1, 1);
928 void decompose_at(Param_Vertices *V, int _i, barvinok_options *options) {
929 //this->pVD = pVD;
931 vE[_i] = new evalue;
932 value_init(vE[_i]->d);
933 evalue_set_si(vE[_i], 0, 1);
935 vpd->decompose_at_vertex(V, _i, options);
938 virtual ~enumerator_base() {
939 for (int j = 0; j < vpd->nbV; ++j)
940 if (vE[j]) {
941 free_evalue_refs(vE[j]);
942 delete vE[j];
944 delete [] vE;
946 free_evalue_refs(&mone);
949 static enumerator_base *create(Polyhedron *P, unsigned dim, unsigned nbV,
950 barvinok_options *options);
953 struct enumerator : public signed_cone_consumer, public vertex_decomposer,
954 public enumerator_base {
955 vec_ZZ lambda;
956 vec_ZZ den;
957 ZZ sign;
958 term_info num;
959 Vector *c;
960 mpq_t count;
962 enumerator(Polyhedron *P, unsigned dim, unsigned nbV) :
963 vertex_decomposer(P, nbV, *this), enumerator_base(dim, this) {
964 this->P = P;
965 this->nbV = nbV;
966 randomvector(P, lambda, dim);
967 den.SetLength(dim);
968 c = Vector_Alloc(dim+2);
970 mpq_init(count);
973 ~enumerator() {
974 mpq_clear(count);
975 Vector_Free(c);
978 virtual void handle(const signed_cone& sc, barvinok_options *options);
981 void enumerator::handle(const signed_cone& sc, barvinok_options *options)
983 assert(sc.det == 1);
984 assert(!sc.closed);
985 int r = 0;
986 assert(sc.rays.NumRows() == dim);
987 for (int k = 0; k < dim; ++k) {
988 if (lambda * sc.rays[k] == 0)
989 throw Orthogonal;
992 sign = sc.sign;
994 lattice_point(V, sc.rays, lambda, &num, 0, options);
995 den = sc.rays * lambda;
996 normalize(sign, num.constant, den);
998 dpoly n(dim, den[0], 1);
999 for (int k = 1; k < dim; ++k) {
1000 dpoly fact(dim, den[k], 1);
1001 n *= fact;
1003 if (num.E != NULL) {
1004 ZZ one(INIT_VAL, 1);
1005 dpoly_n d(dim, num.constant, one);
1006 d.div(n, c, sign);
1007 evalue EV;
1008 multi_polynom(c, num.E, &EV);
1009 eadd(&EV , vE[vert]);
1010 free_evalue_refs(&EV);
1011 free_evalue_refs(num.E);
1012 delete num.E;
1013 } else if (num.pos != -1) {
1014 dpoly_n d(dim, num.constant, num.coeff);
1015 d.div(n, c, sign);
1016 evalue EV;
1017 uni_polynom(num.pos, c, &EV);
1018 eadd(&EV , vE[vert]);
1019 free_evalue_refs(&EV);
1020 } else {
1021 mpq_set_si(count, 0, 1);
1022 dpoly d(dim, num.constant);
1023 d.div(n, count, sign);
1024 evalue EV;
1025 value_init(EV.d);
1026 evalue_set(&EV, &count[0]._mp_num, &count[0]._mp_den);
1027 eadd(&EV , vE[vert]);
1028 free_evalue_refs(&EV);
1032 struct ienumerator_base : enumerator_base {
1033 evalue ** E_vertex;
1035 ienumerator_base(unsigned dim, vertex_decomposer *vpd) :
1036 enumerator_base(dim,vpd) {
1037 E_vertex = new evalue_p[dim];
1040 virtual ~ienumerator_base() {
1041 delete [] E_vertex;
1044 evalue *E_num(int i, int d) {
1045 return E_vertex[i + (dim-d)];
1049 struct cumulator {
1050 evalue *factor;
1051 evalue *v;
1052 dpoly_r *r;
1054 cumulator(evalue *factor, evalue *v, dpoly_r *r) :
1055 factor(factor), v(v), r(r) {}
1057 void cumulate(barvinok_options *options);
1059 virtual void add_term(const vector<int>& powers, evalue *f2) = 0;
1060 virtual ~cumulator() {}
1063 void cumulator::cumulate(barvinok_options *options)
1065 evalue cum; // factor * 1 * E_num[0]/1 * (E_num[0]-1)/2 *...
1066 evalue f;
1067 evalue t; // E_num[0] - (m-1)
1068 evalue *cst;
1069 evalue mone;
1071 if (options->lookup_table) {
1072 value_init(mone.d);
1073 evalue_set_si(&mone, -1, 1);
1076 value_init(cum.d);
1077 evalue_copy(&cum, factor);
1078 value_init(f.d);
1079 value_init(f.x.n);
1080 value_set_si(f.d, 1);
1081 value_set_si(f.x.n, 1);
1082 value_init(t.d);
1083 evalue_copy(&t, v);
1085 if (!options->lookup_table) {
1086 for (cst = &t; value_zero_p(cst->d); ) {
1087 if (cst->x.p->type == fractional)
1088 cst = &cst->x.p->arr[1];
1089 else
1090 cst = &cst->x.p->arr[0];
1094 for (int m = 0; m < r->len; ++m) {
1095 if (m > 0) {
1096 if (m > 1) {
1097 value_set_si(f.d, m);
1098 emul(&f, &cum);
1099 if (!options->lookup_table)
1100 value_subtract(cst->x.n, cst->x.n, cst->d);
1101 else
1102 eadd(&mone, &t);
1104 emul(&t, &cum);
1106 dpoly_r_term_list& current = r->c[r->len-1-m];
1107 dpoly_r_term_list::iterator j;
1108 for (j = current.begin(); j != current.end(); ++j) {
1109 if ((*j)->coeff == 0)
1110 continue;
1111 evalue *f2 = new evalue;
1112 value_init(f2->d);
1113 value_init(f2->x.n);
1114 zz2value((*j)->coeff, f2->x.n);
1115 zz2value(r->denom, f2->d);
1116 emul(&cum, f2);
1118 add_term((*j)->powers, f2);
1121 free_evalue_refs(&f);
1122 free_evalue_refs(&t);
1123 free_evalue_refs(&cum);
1124 if (options->lookup_table)
1125 free_evalue_refs(&mone);
1128 struct E_poly_term {
1129 vector<int> powers;
1130 evalue *E;
1133 struct ie_cum : public cumulator {
1134 vector<E_poly_term *> terms;
1136 ie_cum(evalue *factor, evalue *v, dpoly_r *r) : cumulator(factor, v, r) {}
1138 virtual void add_term(const vector<int>& powers, evalue *f2);
1141 void ie_cum::add_term(const vector<int>& powers, evalue *f2)
1143 int k;
1144 for (k = 0; k < terms.size(); ++k) {
1145 if (terms[k]->powers == powers) {
1146 eadd(f2, terms[k]->E);
1147 free_evalue_refs(f2);
1148 delete f2;
1149 break;
1152 if (k >= terms.size()) {
1153 E_poly_term *ET = new E_poly_term;
1154 ET->powers = powers;
1155 ET->E = f2;
1156 terms.push_back(ET);
1160 struct ienumerator : public signed_cone_consumer, public vertex_decomposer,
1161 public ienumerator_base {
1162 //Polyhedron *pVD;
1163 mat_ZZ den;
1164 mat_ZZ vertex;
1165 mpq_t tcount;
1167 ienumerator(Polyhedron *P, unsigned dim, unsigned nbV) :
1168 vertex_decomposer(P, nbV, *this), ienumerator_base(dim, this) {
1169 vertex.SetDims(1, dim);
1171 den.SetDims(dim, dim);
1172 mpq_init(tcount);
1175 ~ienumerator() {
1176 mpq_clear(tcount);
1179 virtual void handle(const signed_cone& sc, barvinok_options *options);
1180 void reduce(evalue *factor, const mat_ZZ& num, const mat_ZZ& den_f,
1181 barvinok_options *options);
1184 void ienumerator::reduce(evalue *factor, const mat_ZZ& num, const mat_ZZ& den_f,
1185 barvinok_options *options)
1187 unsigned len = den_f.NumRows(); // number of factors in den
1188 unsigned dim = num.NumCols();
1189 assert(num.NumRows() == 1);
1191 if (dim == 0) {
1192 eadd(factor, vE[vert]);
1193 return;
1196 vec_ZZ den_s;
1197 mat_ZZ den_r;
1198 vec_ZZ num_s;
1199 mat_ZZ num_p;
1201 split_one(num, num_s, num_p, den_f, den_s, den_r);
1203 vec_ZZ den_p;
1204 den_p.SetLength(len);
1206 ZZ one;
1207 one = 1;
1208 normalize(one, num_s, num_p, den_s, den_p, den_r);
1209 if (one != 1)
1210 emul(&mone, factor);
1212 int only_param = 0;
1213 int no_param = 0;
1214 for (int k = 0; k < len; ++k) {
1215 if (den_p[k] == 0)
1216 ++no_param;
1217 else if (den_s[k] == 0)
1218 ++only_param;
1220 if (no_param == 0) {
1221 reduce(factor, num_p, den_r, options);
1222 } else {
1223 int k, l;
1224 mat_ZZ pden;
1225 pden.SetDims(only_param, dim-1);
1227 for (k = 0, l = 0; k < len; ++k)
1228 if (den_s[k] == 0)
1229 pden[l++] = den_r[k];
1231 for (k = 0; k < len; ++k)
1232 if (den_p[k] == 0)
1233 break;
1235 dpoly n(no_param, num_s[0]);
1236 dpoly D(no_param, den_s[k], 1);
1237 for ( ; ++k < len; )
1238 if (den_p[k] == 0) {
1239 dpoly fact(no_param, den_s[k], 1);
1240 D *= fact;
1243 dpoly_r * r = 0;
1244 // if no_param + only_param == len then all powers
1245 // below will be all zero
1246 if (no_param + only_param == len) {
1247 if (E_num(0, dim) != 0)
1248 r = new dpoly_r(n, len);
1249 else {
1250 mpq_set_si(tcount, 0, 1);
1251 one = 1;
1252 n.div(D, tcount, one);
1254 if (value_notzero_p(mpq_numref(tcount))) {
1255 evalue f;
1256 value_init(f.d);
1257 value_init(f.x.n);
1258 value_assign(f.x.n, mpq_numref(tcount));
1259 value_assign(f.d, mpq_denref(tcount));
1260 emul(&f, factor);
1261 reduce(factor, num_p, pden, options);
1262 free_evalue_refs(&f);
1264 return;
1266 } else {
1267 for (k = 0; k < len; ++k) {
1268 if (den_s[k] == 0 || den_p[k] == 0)
1269 continue;
1271 dpoly pd(no_param-1, den_s[k], 1);
1273 int l;
1274 for (l = 0; l < k; ++l)
1275 if (den_r[l] == den_r[k])
1276 break;
1278 if (r == 0)
1279 r = new dpoly_r(n, pd, l, len);
1280 else {
1281 dpoly_r *nr = new dpoly_r(r, pd, l, len);
1282 delete r;
1283 r = nr;
1287 dpoly_r *rc = r->div(D);
1288 delete r;
1289 r = rc;
1290 if (E_num(0, dim) == 0) {
1291 int common = pden.NumRows();
1292 dpoly_r_term_list& final = r->c[r->len-1];
1293 int rows;
1294 evalue t;
1295 evalue f;
1296 value_init(f.d);
1297 value_init(f.x.n);
1298 zz2value(r->denom, f.d);
1299 dpoly_r_term_list::iterator j;
1300 for (j = final.begin(); j != final.end(); ++j) {
1301 if ((*j)->coeff == 0)
1302 continue;
1303 rows = common;
1304 for (int k = 0; k < r->dim; ++k) {
1305 int n = (*j)->powers[k];
1306 if (n == 0)
1307 continue;
1308 pden.SetDims(rows+n, pden.NumCols());
1309 for (int l = 0; l < n; ++l)
1310 pden[rows+l] = den_r[k];
1311 rows += n;
1313 value_init(t.d);
1314 evalue_copy(&t, factor);
1315 zz2value((*j)->coeff, f.x.n);
1316 emul(&f, &t);
1317 reduce(&t, num_p, pden, options);
1318 free_evalue_refs(&t);
1320 free_evalue_refs(&f);
1321 } else {
1322 ie_cum cum(factor, E_num(0, dim), r);
1323 cum.cumulate(options);
1325 int common = pden.NumRows();
1326 int rows;
1327 for (int j = 0; j < cum.terms.size(); ++j) {
1328 rows = common;
1329 pden.SetDims(rows, pden.NumCols());
1330 for (int k = 0; k < r->dim; ++k) {
1331 int n = cum.terms[j]->powers[k];
1332 if (n == 0)
1333 continue;
1334 pden.SetDims(rows+n, pden.NumCols());
1335 for (int l = 0; l < n; ++l)
1336 pden[rows+l] = den_r[k];
1337 rows += n;
1339 reduce(cum.terms[j]->E, num_p, pden, options);
1340 free_evalue_refs(cum.terms[j]->E);
1341 delete cum.terms[j]->E;
1342 delete cum.terms[j];
1345 delete r;
1349 static int type_offset(enode *p)
1351 return p->type == fractional ? 1 :
1352 p->type == flooring ? 1 : 0;
1355 static int edegree(evalue *e)
1357 int d = 0;
1358 enode *p;
1360 if (value_notzero_p(e->d))
1361 return 0;
1363 p = e->x.p;
1364 int i = type_offset(p);
1365 if (p->size-i-1 > d)
1366 d = p->size - i - 1;
1367 for (; i < p->size; i++) {
1368 int d2 = edegree(&p->arr[i]);
1369 if (d2 > d)
1370 d = d2;
1372 return d;
1375 void ienumerator::handle(const signed_cone& sc, barvinok_options *options)
1377 assert(sc.det == 1);
1378 assert(!sc.closed);
1379 assert(sc.rays.NumRows() == dim);
1381 lattice_point(V, sc.rays, vertex[0], E_vertex, options);
1383 den = sc.rays;
1385 evalue one;
1386 value_init(one.d);
1387 evalue_set_si(&one, sc.sign, 1);
1388 reduce(&one, vertex, den, options);
1389 free_evalue_refs(&one);
1391 for (int i = 0; i < dim; ++i)
1392 if (E_vertex[i]) {
1393 free_evalue_refs(E_vertex[i]);
1394 delete E_vertex[i];
1398 struct bfenumerator : public vertex_decomposer, public bf_base,
1399 public ienumerator_base {
1400 evalue *factor;
1402 bfenumerator(Polyhedron *P, unsigned dim, unsigned nbV) :
1403 vertex_decomposer(P, nbV, *this),
1404 bf_base(dim), ienumerator_base(dim, this) {
1405 lower = 0;
1406 factor = NULL;
1409 ~bfenumerator() {
1412 virtual void handle(const signed_cone& sc, barvinok_options *options);
1413 virtual void base(mat_ZZ& factors, bfc_vec& v);
1415 bfc_term_base* new_bf_term(int len) {
1416 bfe_term* t = new bfe_term(len);
1417 return t;
1420 virtual void set_factor(bfc_term_base *t, int k, int change) {
1421 bfe_term* bfet = static_cast<bfe_term *>(t);
1422 factor = bfet->factors[k];
1423 assert(factor != NULL);
1424 bfet->factors[k] = NULL;
1425 if (change)
1426 emul(&mone, factor);
1429 virtual void set_factor(bfc_term_base *t, int k, mpq_t &q, 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;
1435 evalue f;
1436 value_init(f.d);
1437 value_init(f.x.n);
1438 if (change)
1439 value_oppose(f.x.n, mpq_numref(q));
1440 else
1441 value_assign(f.x.n, mpq_numref(q));
1442 value_assign(f.d, mpq_denref(q));
1443 emul(&f, factor);
1444 free_evalue_refs(&f);
1447 virtual void set_factor(bfc_term_base *t, int k, const QQ& c, int change) {
1448 bfe_term* bfet = static_cast<bfe_term *>(t);
1450 factor = new evalue;
1452 evalue f;
1453 value_init(f.d);
1454 value_init(f.x.n);
1455 zz2value(c.n, f.x.n);
1456 if (change)
1457 value_oppose(f.x.n, f.x.n);
1458 zz2value(c.d, f.d);
1460 value_init(factor->d);
1461 evalue_copy(factor, bfet->factors[k]);
1462 emul(&f, factor);
1463 free_evalue_refs(&f);
1466 void set_factor(evalue *f, int change) {
1467 if (change)
1468 emul(&mone, f);
1469 factor = f;
1472 virtual void insert_term(bfc_term_base *t, int i) {
1473 bfe_term* bfet = static_cast<bfe_term *>(t);
1474 int len = t->terms.NumRows()-1; // already increased by one
1476 bfet->factors.resize(len+1);
1477 for (int j = len; j > i; --j) {
1478 bfet->factors[j] = bfet->factors[j-1];
1479 t->terms[j] = t->terms[j-1];
1481 bfet->factors[i] = factor;
1482 factor = NULL;
1485 virtual void update_term(bfc_term_base *t, int i) {
1486 bfe_term* bfet = static_cast<bfe_term *>(t);
1488 eadd(factor, bfet->factors[i]);
1489 free_evalue_refs(factor);
1490 delete factor;
1493 virtual bool constant_vertex(int dim) { return E_num(0, dim) == 0; }
1495 virtual void cum(bf_reducer *bfr, bfc_term_base *t, int k, dpoly_r *r,
1496 barvinok_options *options);
1499 enumerator_base *enumerator_base::create(Polyhedron *P, unsigned dim, unsigned nbV,
1500 barvinok_options *options)
1502 enumerator_base *eb;
1504 if (options->incremental_specialization == BV_SPECIALIZATION_BF)
1505 eb = new bfenumerator(P, dim, nbV);
1506 else if (options->incremental_specialization == BV_SPECIALIZATION_DF)
1507 eb = new ienumerator(P, dim, nbV);
1508 else
1509 eb = new enumerator(P, dim, nbV);
1511 return eb;
1514 struct bfe_cum : public cumulator {
1515 bfenumerator *bfe;
1516 bfc_term_base *told;
1517 int k;
1518 bf_reducer *bfr;
1520 bfe_cum(evalue *factor, evalue *v, dpoly_r *r, bf_reducer *bfr,
1521 bfc_term_base *t, int k, bfenumerator *e) :
1522 cumulator(factor, v, r), told(t), k(k),
1523 bfr(bfr), bfe(e) {
1526 virtual void add_term(const vector<int>& powers, evalue *f2);
1529 void bfe_cum::add_term(const vector<int>& powers, evalue *f2)
1531 bfr->update_powers(powers);
1533 bfc_term_base * t = bfe->find_bfc_term(bfr->vn, bfr->npowers, bfr->nnf);
1534 bfe->set_factor(f2, bfr->l_changes % 2);
1535 bfe->add_term(t, told->terms[k], bfr->l_extra_num);
1538 void bfenumerator::cum(bf_reducer *bfr, bfc_term_base *t, int k,
1539 dpoly_r *r, barvinok_options *options)
1541 bfe_term* bfet = static_cast<bfe_term *>(t);
1542 bfe_cum cum(bfet->factors[k], E_num(0, bfr->d), r, bfr, t, k, this);
1543 cum.cumulate(options);
1546 void bfenumerator::base(mat_ZZ& factors, bfc_vec& v)
1548 for (int i = 0; i < v.size(); ++i) {
1549 assert(v[i]->terms.NumRows() == 1);
1550 evalue *factor = static_cast<bfe_term *>(v[i])->factors[0];
1551 eadd(factor, vE[vert]);
1552 delete v[i];
1556 void bfenumerator::handle(const signed_cone& sc, barvinok_options *options)
1558 assert(sc.det == 1);
1559 assert(!sc.closed);
1560 assert(sc.rays.NumRows() == enumerator_base::dim);
1562 bfe_term* t = new bfe_term(enumerator_base::dim);
1563 vector< bfc_term_base * > v;
1564 v.push_back(t);
1566 t->factors.resize(1);
1568 t->terms.SetDims(1, enumerator_base::dim);
1569 lattice_point(V, sc.rays, t->terms[0], E_vertex, options);
1571 // the elements of factors are always lexpositive
1572 mat_ZZ factors;
1573 int s = setup_factors(sc.rays, factors, t, sc.sign);
1575 t->factors[0] = new evalue;
1576 value_init(t->factors[0]->d);
1577 evalue_set_si(t->factors[0], s, 1);
1578 reduce(factors, v, options);
1580 for (int i = 0; i < enumerator_base::dim; ++i)
1581 if (E_vertex[i]) {
1582 free_evalue_refs(E_vertex[i]);
1583 delete E_vertex[i];
1587 #ifdef HAVE_CORRECT_VERTICES
1588 static inline Param_Polyhedron *Polyhedron2Param_SD(Polyhedron **Din,
1589 Polyhedron *Cin,int WS,Polyhedron **CEq,Matrix **CT)
1591 if (WS & POL_NO_DUAL)
1592 WS = 0;
1593 return Polyhedron2Param_SimplifiedDomain(Din, Cin, WS, CEq, CT);
1595 #else
1596 static Param_Polyhedron *Polyhedron2Param_SD(Polyhedron **Din,
1597 Polyhedron *Cin,int WS,Polyhedron **CEq,Matrix **CT)
1599 static char data[] = " 1 0 0 0 0 1 -18 "
1600 " 1 0 0 -20 0 19 1 "
1601 " 1 0 1 20 0 -20 16 "
1602 " 1 0 0 0 0 -1 19 "
1603 " 1 0 -1 0 0 0 4 "
1604 " 1 4 -20 0 0 -1 23 "
1605 " 1 -4 20 0 0 1 -22 "
1606 " 1 0 1 0 20 -20 16 "
1607 " 1 0 0 0 -20 19 1 ";
1608 static int checked = 0;
1609 if (!checked) {
1610 checked = 1;
1611 char *p = data;
1612 int n, v, i;
1613 Matrix *M = Matrix_Alloc(9, 7);
1614 for (i = 0; i < 9; ++i)
1615 for (int j = 0; j < 7; ++j) {
1616 sscanf(p, "%d%n", &v, &n);
1617 p += n;
1618 value_set_si(M->p[i][j], v);
1620 Polyhedron *P = Constraints2Polyhedron(M, 1024);
1621 Matrix_Free(M);
1622 Polyhedron *U = Universe_Polyhedron(1);
1623 Param_Polyhedron *PP = Polyhedron2Param_Domain(P, U, 1024);
1624 Polyhedron_Free(P);
1625 Polyhedron_Free(U);
1626 Param_Vertices *V;
1627 for (i = 0, V = PP->V; V; ++i, V = V->next)
1629 if (PP)
1630 Param_Polyhedron_Free(PP);
1631 if (i != 10) {
1632 fprintf(stderr, "WARNING: results may be incorrect\n");
1633 fprintf(stderr,
1634 "WARNING: use latest version of PolyLib to remove this warning\n");
1638 return Polyhedron2Param_SimplifiedDomain(Din, Cin, WS, CEq, CT);
1640 #endif
1642 static evalue* barvinok_enumerate_ev_f(Polyhedron *P, Polyhedron* C,
1643 barvinok_options *options);
1645 /* Destroys C */
1646 static evalue* barvinok_enumerate_cst(Polyhedron *P, Polyhedron* C,
1647 struct barvinok_options *options)
1649 evalue *eres;
1651 ALLOC(evalue, eres);
1652 value_init(eres->d);
1653 value_set_si(eres->d, 0);
1654 eres->x.p = new_enode(partition, 2, C->Dimension);
1655 EVALUE_SET_DOMAIN(eres->x.p->arr[0],
1656 DomainConstraintSimplify(C, options->MaxRays));
1657 value_set_si(eres->x.p->arr[1].d, 1);
1658 value_init(eres->x.p->arr[1].x.n);
1659 if (emptyQ(P))
1660 value_set_si(eres->x.p->arr[1].x.n, 0);
1661 else
1662 barvinok_count_with_options(P, &eres->x.p->arr[1].x.n, options);
1664 return eres;
1667 evalue* barvinok_enumerate_with_options(Polyhedron *P, Polyhedron* C,
1668 struct barvinok_options *options)
1670 //P = unfringe(P, MaxRays);
1671 Polyhedron *next, *Cnext;
1672 Polyhedron *Corig = C;
1673 Polyhedron *Porig = P;
1674 Polyhedron *CEq = NULL, *rVD, *CA;
1675 int r = 0;
1676 unsigned nparam = C->Dimension;
1677 evalue *eres;
1679 if (P->next)
1680 fprintf(stderr,
1681 "barvinok_enumerate: input is a union; only first polyhedron is enumerated\n");
1683 if (C->next)
1684 fprintf(stderr,
1685 "barvinok_enumerate: context is a union; only first polyhedron is considered\n");
1687 evalue factor;
1688 value_init(factor.d);
1689 evalue_set_si(&factor, 1, 1);
1691 Cnext = C->next;
1692 C->next = NULL;
1693 CA = align_context(C, P->Dimension, options->MaxRays);
1694 next = P->next;
1695 P->next = NULL;
1696 P = DomainIntersection(P, CA, options->MaxRays);
1697 Porig->next = next;
1698 Polyhedron_Free(CA);
1700 /* for now */
1701 POL_ENSURE_FACETS(P);
1702 POL_ENSURE_VERTICES(P);
1703 POL_ENSURE_FACETS(C);
1704 POL_ENSURE_VERTICES(C);
1706 if (C->Dimension == 0 || emptyQ(P)) {
1707 constant:
1708 eres = barvinok_enumerate_cst(P, CEq ? CEq : Polyhedron_Copy(C), options);
1709 out:
1710 emul(&factor, eres);
1711 if (options->approximation_method == BV_APPROX_DROP) {
1712 if (options->polynomial_approximation == BV_APPROX_SIGN_UPPER)
1713 evalue_frac2polynomial(eres, 1, options->MaxRays);
1714 if (options->polynomial_approximation == BV_APPROX_SIGN_LOWER)
1715 evalue_frac2polynomial(eres, -1, options->MaxRays);
1717 reduce_evalue(eres);
1718 free_evalue_refs(&factor);
1719 Domain_Free(P);
1720 if (C != Corig)
1721 Polyhedron_Free(C);
1723 Corig->next = Cnext;
1724 return eres;
1726 if (Polyhedron_is_unbounded(P, nparam, options->MaxRays))
1727 goto constant;
1729 if (P->NbEq != 0) {
1730 Matrix *f;
1731 P = remove_equalities_p(P, P->Dimension-nparam, &f);
1732 mask(f, &factor, options);
1733 Matrix_Free(f);
1735 if (P->Dimension == nparam) {
1736 CEq = P;
1737 P = Universe_Polyhedron(0);
1738 goto constant;
1741 Polyhedron *T = Polyhedron_Factor(P, nparam, NULL, options->MaxRays);
1742 if (T || (P->Dimension == nparam+1)) {
1743 Polyhedron *Q;
1744 Polyhedron *C2;
1745 for (Q = T ? T : P; Q; Q = Q->next) {
1746 Polyhedron *next = Q->next;
1747 Q->next = NULL;
1749 Polyhedron *QC = Q;
1750 if (Q->Dimension != C->Dimension)
1751 QC = Polyhedron_Project(Q, nparam);
1753 C2 = C;
1754 C = DomainIntersection(C, QC, options->MaxRays);
1755 if (C2 != Corig)
1756 Polyhedron_Free(C2);
1757 if (QC != Q)
1758 Polyhedron_Free(QC);
1760 Q->next = next;
1763 if (T) {
1764 Polyhedron_Free(P);
1765 P = T;
1766 if (T->Dimension == C->Dimension) {
1767 P = T->next;
1768 T->next = NULL;
1769 Polyhedron_Free(T);
1773 next = P->next;
1774 P->next = NULL;
1775 eres = barvinok_enumerate_ev_f(P, C, options);
1776 P->next = next;
1778 if (P->next) {
1779 Polyhedron *Q;
1780 evalue *f;
1782 for (Q = P->next; Q; Q = Q->next) {
1783 Polyhedron *next = Q->next;
1784 Q->next = NULL;
1786 f = barvinok_enumerate_ev_f(Q, C, options);
1787 emul(f, eres);
1788 free_evalue_refs(f);
1789 free(f);
1791 Q->next = next;
1795 goto out;
1798 evalue* barvinok_enumerate_ev(Polyhedron *P, Polyhedron* C, unsigned MaxRays)
1800 evalue *E;
1801 barvinok_options *options = barvinok_options_new_with_defaults();
1802 options->MaxRays = MaxRays;
1803 E = barvinok_enumerate_with_options(P, C, options);
1804 barvinok_options_free(options);
1805 return E;
1808 /* adapted from mpolyhedron_inflate in PolyLib */
1809 static Polyhedron *Polyhedron_Inflate(Polyhedron *P, unsigned nparam,
1810 unsigned MaxRays)
1812 Value sum;
1813 int nvar = P->Dimension - nparam;
1814 Matrix *C = Polyhedron2Constraints(P);
1815 Polyhedron *P2;
1817 value_init(sum);
1818 /* subtract the sum of the negative coefficients of each inequality */
1819 for (int i = 0; i < C->NbRows; ++i) {
1820 value_set_si(sum, 0);
1821 for (int j = 0; j < nvar; ++j)
1822 if (value_neg_p(C->p[i][1+j]))
1823 value_addto(sum, sum, C->p[i][1+j]);
1824 value_subtract(C->p[i][1+P->Dimension], C->p[i][1+P->Dimension], sum);
1826 value_clear(sum);
1827 P2 = Constraints2Polyhedron(C, MaxRays);
1828 Matrix_Free(C);
1829 return P2;
1832 /* adapted from mpolyhedron_deflate in PolyLib */
1833 static Polyhedron *Polyhedron_Deflate(Polyhedron *P, unsigned nparam,
1834 unsigned MaxRays)
1836 Value sum;
1837 int nvar = P->Dimension - nparam;
1838 Matrix *C = Polyhedron2Constraints(P);
1839 Polyhedron *P2;
1841 value_init(sum);
1842 /* subtract the sum of the positive coefficients of each inequality */
1843 for (int i = 0; i < C->NbRows; ++i) {
1844 value_set_si(sum, 0);
1845 for (int j = 0; j < nvar; ++j)
1846 if (value_pos_p(C->p[i][1+j]))
1847 value_addto(sum, sum, C->p[i][1+j]);
1848 value_subtract(C->p[i][1+P->Dimension], C->p[i][1+P->Dimension], sum);
1850 value_clear(sum);
1851 P2 = Constraints2Polyhedron(C, MaxRays);
1852 Matrix_Free(C);
1853 return P2;
1856 static evalue* barvinok_enumerate_ev_f(Polyhedron *P, Polyhedron* C,
1857 barvinok_options *options)
1859 unsigned nparam = C->Dimension;
1860 bool pre_approx = options->approximation_method == BV_APPROX_SCALE;
1862 if (P->Dimension - nparam == 1 && !pre_approx)
1863 return ParamLine_Length(P, C, options);
1865 Param_Polyhedron *PP = NULL;
1866 Polyhedron *CEq = NULL, *pVD;
1867 Matrix *CT = NULL;
1868 Param_Domain *D, *next;
1869 Param_Vertices *V;
1870 evalue *eres;
1871 Polyhedron *Porig = P;
1872 Value det;
1873 Polyhedron *T;
1875 if (options->approximation_method == BV_APPROX_SCALE) {
1876 if (options->polynomial_approximation == BV_APPROX_SIGN_UPPER)
1877 P = Polyhedron_Inflate(P, nparam, options->MaxRays);
1878 if (options->polynomial_approximation == BV_APPROX_SIGN_LOWER) {
1879 P = Polyhedron_Deflate(P, nparam, options->MaxRays);
1880 POL_ENSURE_VERTICES(P);
1881 if (emptyQ(P)) {
1882 eres = barvinok_enumerate_cst(P, Polyhedron_Copy(C), options);
1883 Polyhedron_Free(P);
1884 return eres;
1889 T = P;
1890 PP = Polyhedron2Param_SD(&T, C, options->MaxRays, &CEq, &CT);
1891 if (T != P && P != Porig)
1892 Polyhedron_Free(P);
1893 P = T;
1895 if (isIdentity(CT)) {
1896 Matrix_Free(CT);
1897 CT = NULL;
1898 } else {
1899 assert(CT->NbRows != CT->NbColumns);
1900 if (CT->NbRows == 1) { // no more parameters
1901 eres = barvinok_enumerate_cst(P, CEq, options);
1902 out:
1903 if (CT)
1904 Matrix_Free(CT);
1905 if (PP)
1906 Param_Polyhedron_Free(PP);
1907 if (P != Porig)
1908 Polyhedron_Free(P);
1910 return eres;
1912 nparam = CT->NbRows - 1;
1915 if (pre_approx) {
1916 value_init(det);
1917 Polyhedron *T = P;
1918 Param_Polyhedron_Scale_Integer(PP, &T, &det, options->MaxRays);
1919 if (P != Porig)
1920 Polyhedron_Free(P);
1921 P = T;
1924 unsigned dim = P->Dimension - nparam;
1926 ALLOC(evalue, eres);
1927 value_init(eres->d);
1928 value_set_si(eres->d, 0);
1930 int nd;
1931 for (nd = 0, D=PP->D; D; ++nd, D=D->next);
1932 struct section { Polyhedron *D; evalue E; };
1933 section *s = new section[nd];
1934 Polyhedron **fVD = new Polyhedron_p[nd];
1936 enumerator_base *et = NULL;
1937 try_again:
1938 if (et)
1939 delete et;
1941 et = enumerator_base::create(P, dim, PP->nbV, options);
1943 for(nd = 0, D=PP->D; D; D=next) {
1944 next = D->next;
1946 Polyhedron *rVD = reduce_domain(D->Domain, CT, CEq, fVD, nd, options);
1947 if (!rVD)
1948 continue;
1950 pVD = CT ? DomainImage(rVD,CT,options->MaxRays) : rVD;
1952 value_init(s[nd].E.d);
1953 evalue_set_si(&s[nd].E, 0, 1);
1954 s[nd].D = rVD;
1956 FORALL_PVertex_in_ParamPolyhedron(V,D,PP) // _i is internal counter
1957 if (!et->vE[_i])
1958 try {
1959 et->decompose_at(V, _i, options);
1960 } catch (OrthogonalException &e) {
1961 if (rVD != pVD)
1962 Domain_Free(pVD);
1963 for (; nd >= 0; --nd) {
1964 free_evalue_refs(&s[nd].E);
1965 Domain_Free(s[nd].D);
1966 Domain_Free(fVD[nd]);
1968 goto try_again;
1970 eadd(et->vE[_i] , &s[nd].E);
1971 END_FORALL_PVertex_in_ParamPolyhedron;
1972 evalue_range_reduction_in_domain(&s[nd].E, pVD);
1974 if (CT)
1975 addeliminatedparams_evalue(&s[nd].E, CT);
1976 ++nd;
1977 if (rVD != pVD)
1978 Domain_Free(pVD);
1981 delete et;
1982 if (nd == 0)
1983 evalue_set_si(eres, 0, 1);
1984 else {
1985 eres->x.p = new_enode(partition, 2*nd, C->Dimension);
1986 for (int j = 0; j < nd; ++j) {
1987 EVALUE_SET_DOMAIN(eres->x.p->arr[2*j], s[j].D);
1988 value_clear(eres->x.p->arr[2*j+1].d);
1989 eres->x.p->arr[2*j+1] = s[j].E;
1990 Domain_Free(fVD[j]);
1993 delete [] s;
1994 delete [] fVD;
1996 if (pre_approx) {
1997 evalue_div(eres, det);
1998 value_clear(det);
2001 if (CEq)
2002 Polyhedron_Free(CEq);
2003 goto out;
2006 Enumeration* barvinok_enumerate(Polyhedron *P, Polyhedron* C, unsigned MaxRays)
2008 evalue *EP = barvinok_enumerate_ev(P, C, MaxRays);
2010 return partition2enumeration(EP);
2013 static void SwapColumns(Value **V, int n, int i, int j)
2015 for (int r = 0; r < n; ++r)
2016 value_swap(V[r][i], V[r][j]);
2019 static void SwapColumns(Polyhedron *P, int i, int j)
2021 SwapColumns(P->Constraint, P->NbConstraints, i, j);
2022 SwapColumns(P->Ray, P->NbRays, i, j);
2025 /* Construct a constraint c from constraints l and u such that if
2026 * if constraint c holds then for each value of the other variables
2027 * there is at most one value of variable pos (position pos+1 in the constraints).
2029 * Given a lower and an upper bound
2030 * n_l v_i + <c_l,x> + c_l >= 0
2031 * -n_u v_i + <c_u,x> + c_u >= 0
2032 * the constructed constraint is
2034 * -(n_l<c_u,x> + n_u<c_l,x>) + (-n_l c_u - n_u c_l + n_l n_u - 1)
2036 * which is then simplified to remove the content of the non-constant coefficients
2038 * len is the total length of the constraints.
2039 * v is a temporary variable that can be used by this procedure
2041 static void negative_test_constraint(Value *l, Value *u, Value *c, int pos,
2042 int len, Value *v)
2044 value_oppose(*v, u[pos+1]);
2045 Vector_Combine(l+1, u+1, c+1, *v, l[pos+1], len-1);
2046 value_multiply(*v, *v, l[pos+1]);
2047 value_subtract(c[len-1], c[len-1], *v);
2048 value_set_si(*v, -1);
2049 Vector_Scale(c+1, c+1, *v, len-1);
2050 value_decrement(c[len-1], c[len-1]);
2051 ConstraintSimplify(c, c, len, v);
2054 static bool parallel_constraints(Value *l, Value *u, Value *c, int pos,
2055 int len)
2057 bool parallel;
2058 Value g1;
2059 Value g2;
2060 value_init(g1);
2061 value_init(g2);
2063 Vector_Gcd(&l[1+pos], len, &g1);
2064 Vector_Gcd(&u[1+pos], len, &g2);
2065 Vector_Combine(l+1+pos, u+1+pos, c+1, g2, g1, len);
2066 parallel = First_Non_Zero(c+1, len) == -1;
2068 value_clear(g1);
2069 value_clear(g2);
2071 return parallel;
2074 static void negative_test_constraint7(Value *l, Value *u, Value *c, int pos,
2075 int exist, int len, Value *v)
2077 Value g;
2078 value_init(g);
2080 Vector_Gcd(&u[1+pos], exist, v);
2081 Vector_Gcd(&l[1+pos], exist, &g);
2082 Vector_Combine(l+1, u+1, c+1, *v, g, len-1);
2083 value_multiply(*v, *v, g);
2084 value_subtract(c[len-1], c[len-1], *v);
2085 value_set_si(*v, -1);
2086 Vector_Scale(c+1, c+1, *v, len-1);
2087 value_decrement(c[len-1], c[len-1]);
2088 ConstraintSimplify(c, c, len, v);
2090 value_clear(g);
2093 /* Turns a x + b >= 0 into a x + b <= -1
2095 * len is the total length of the constraint.
2096 * v is a temporary variable that can be used by this procedure
2098 static void oppose_constraint(Value *c, int len, Value *v)
2100 value_set_si(*v, -1);
2101 Vector_Scale(c+1, c+1, *v, len-1);
2102 value_decrement(c[len-1], c[len-1]);
2105 /* Split polyhedron P into two polyhedra *pos and *neg, where
2106 * existential variable i has at most one solution for each
2107 * value of the other variables in *neg.
2109 * The splitting is performed using constraints l and u.
2111 * nvar: number of set variables
2112 * row: temporary vector that can be used by this procedure
2113 * f: temporary value that can be used by this procedure
2115 static bool SplitOnConstraint(Polyhedron *P, int i, int l, int u,
2116 int nvar, int MaxRays, Vector *row, Value& f,
2117 Polyhedron **pos, Polyhedron **neg)
2119 negative_test_constraint(P->Constraint[l], P->Constraint[u],
2120 row->p, nvar+i, P->Dimension+2, &f);
2121 *neg = AddConstraints(row->p, 1, P, MaxRays);
2123 /* We found an independent, but useless constraint
2124 * Maybe we should detect this earlier and not
2125 * mark the variable as INDEPENDENT
2127 if (emptyQ((*neg))) {
2128 Polyhedron_Free(*neg);
2129 return false;
2132 oppose_constraint(row->p, P->Dimension+2, &f);
2133 *pos = AddConstraints(row->p, 1, P, MaxRays);
2135 if (emptyQ((*pos))) {
2136 Polyhedron_Free(*neg);
2137 Polyhedron_Free(*pos);
2138 return false;
2141 return true;
2145 * unimodularly transform P such that constraint r is transformed
2146 * into a constraint that involves only a single (the first)
2147 * existential variable
2150 static Polyhedron *rotate_along(Polyhedron *P, int r, int nvar, int exist,
2151 unsigned MaxRays)
2153 Value g;
2154 value_init(g);
2156 Vector *row = Vector_Alloc(exist);
2157 Vector_Copy(P->Constraint[r]+1+nvar, row->p, exist);
2158 Vector_Gcd(row->p, exist, &g);
2159 if (value_notone_p(g))
2160 Vector_AntiScale(row->p, row->p, g, exist);
2161 value_clear(g);
2163 Matrix *M = unimodular_complete(row);
2164 Matrix *M2 = Matrix_Alloc(P->Dimension+1, P->Dimension+1);
2165 for (r = 0; r < nvar; ++r)
2166 value_set_si(M2->p[r][r], 1);
2167 for ( ; r < nvar+exist; ++r)
2168 Vector_Copy(M->p[r-nvar], M2->p[r]+nvar, exist);
2169 for ( ; r < P->Dimension+1; ++r)
2170 value_set_si(M2->p[r][r], 1);
2171 Polyhedron *T = Polyhedron_Image(P, M2, MaxRays);
2173 Matrix_Free(M2);
2174 Matrix_Free(M);
2175 Vector_Free(row);
2177 return T;
2180 /* Split polyhedron P into two polyhedra *pos and *neg, where
2181 * existential variable i has at most one solution for each
2182 * value of the other variables in *neg.
2184 * If independent is set, then the two constraints on which the
2185 * split will be performed need to be independent of the other
2186 * existential variables.
2188 * Return true if an appropriate split could be performed.
2190 * nvar: number of set variables
2191 * exist: number of existential variables
2192 * row: temporary vector that can be used by this procedure
2193 * f: temporary value that can be used by this procedure
2195 static bool SplitOnVar(Polyhedron *P, int i,
2196 int nvar, int exist, int MaxRays,
2197 Vector *row, Value& f, bool independent,
2198 Polyhedron **pos, Polyhedron **neg)
2200 int j;
2202 for (int l = P->NbEq; l < P->NbConstraints; ++l) {
2203 if (value_negz_p(P->Constraint[l][nvar+i+1]))
2204 continue;
2206 if (independent) {
2207 for (j = 0; j < exist; ++j)
2208 if (j != i && value_notzero_p(P->Constraint[l][nvar+j+1]))
2209 break;
2210 if (j < exist)
2211 continue;
2214 for (int u = P->NbEq; u < P->NbConstraints; ++u) {
2215 if (value_posz_p(P->Constraint[u][nvar+i+1]))
2216 continue;
2218 if (independent) {
2219 for (j = 0; j < exist; ++j)
2220 if (j != i && value_notzero_p(P->Constraint[u][nvar+j+1]))
2221 break;
2222 if (j < exist)
2223 continue;
2226 if (SplitOnConstraint(P, i, l, u, nvar, MaxRays, row, f, pos, neg)) {
2227 if (independent) {
2228 if (i != 0)
2229 SwapColumns(*neg, nvar+1, nvar+1+i);
2231 return true;
2236 return false;
2239 static bool double_bound_pair(Polyhedron *P, int nvar, int exist,
2240 int i, int l1, int l2,
2241 Polyhedron **pos, Polyhedron **neg)
2243 Value f;
2244 value_init(f);
2245 Vector *row = Vector_Alloc(P->Dimension+2);
2246 value_set_si(row->p[0], 1);
2247 value_oppose(f, P->Constraint[l1][nvar+i+1]);
2248 Vector_Combine(P->Constraint[l1]+1, P->Constraint[l2]+1,
2249 row->p+1,
2250 P->Constraint[l2][nvar+i+1], f,
2251 P->Dimension+1);
2252 ConstraintSimplify(row->p, row->p, P->Dimension+2, &f);
2253 *pos = AddConstraints(row->p, 1, P, 0);
2254 value_set_si(f, -1);
2255 Vector_Scale(row->p+1, row->p+1, f, P->Dimension+1);
2256 value_decrement(row->p[P->Dimension+1], row->p[P->Dimension+1]);
2257 *neg = AddConstraints(row->p, 1, P, 0);
2258 Vector_Free(row);
2259 value_clear(f);
2261 return !emptyQ((*pos)) && !emptyQ((*neg));
2264 static bool double_bound(Polyhedron *P, int nvar, int exist,
2265 Polyhedron **pos, Polyhedron **neg)
2267 for (int i = 0; i < exist; ++i) {
2268 int l1, l2;
2269 for (l1 = P->NbEq; l1 < P->NbConstraints; ++l1) {
2270 if (value_negz_p(P->Constraint[l1][nvar+i+1]))
2271 continue;
2272 for (l2 = l1 + 1; l2 < P->NbConstraints; ++l2) {
2273 if (value_negz_p(P->Constraint[l2][nvar+i+1]))
2274 continue;
2275 if (double_bound_pair(P, nvar, exist, i, l1, l2, pos, neg))
2276 return true;
2279 for (l1 = P->NbEq; l1 < P->NbConstraints; ++l1) {
2280 if (value_posz_p(P->Constraint[l1][nvar+i+1]))
2281 continue;
2282 if (l1 < P->NbConstraints)
2283 for (l2 = l1 + 1; l2 < P->NbConstraints; ++l2) {
2284 if (value_posz_p(P->Constraint[l2][nvar+i+1]))
2285 continue;
2286 if (double_bound_pair(P, nvar, exist, i, l1, l2, pos, neg))
2287 return true;
2290 return false;
2292 return false;
2295 enum constraint {
2296 ALL_POS = 1 << 0,
2297 ONE_NEG = 1 << 1,
2298 INDEPENDENT = 1 << 2,
2299 ROT_NEG = 1 << 3
2302 static evalue* enumerate_or(Polyhedron *D,
2303 unsigned exist, unsigned nparam, barvinok_options *options)
2305 #ifdef DEBUG_ER
2306 fprintf(stderr, "\nER: Or\n");
2307 #endif /* DEBUG_ER */
2309 Polyhedron *N = D->next;
2310 D->next = 0;
2311 evalue *EP =
2312 barvinok_enumerate_e_with_options(D, exist, nparam, options);
2313 Polyhedron_Free(D);
2315 for (D = N; D; D = N) {
2316 N = D->next;
2317 D->next = 0;
2319 evalue *EN =
2320 barvinok_enumerate_e_with_options(D, exist, nparam, options);
2322 eor(EN, EP);
2323 free_evalue_refs(EN);
2324 free(EN);
2325 Polyhedron_Free(D);
2328 reduce_evalue(EP);
2330 return EP;
2333 static evalue* enumerate_sum(Polyhedron *P,
2334 unsigned exist, unsigned nparam, barvinok_options *options)
2336 int nvar = P->Dimension - exist - nparam;
2337 int toswap = nvar < exist ? nvar : exist;
2338 for (int i = 0; i < toswap; ++i)
2339 SwapColumns(P, 1 + i, nvar+exist - i);
2340 nparam += nvar;
2342 #ifdef DEBUG_ER
2343 fprintf(stderr, "\nER: Sum\n");
2344 #endif /* DEBUG_ER */
2346 evalue *EP = barvinok_enumerate_e_with_options(P, exist, nparam, options);
2348 evalue_split_domains_into_orthants(EP, options->MaxRays);
2349 reduce_evalue(EP);
2350 evalue_range_reduction(EP);
2352 evalue_frac2floor2(EP, 1);
2354 evalue *sum = esum(EP, nvar);
2356 free_evalue_refs(EP);
2357 free(EP);
2358 EP = sum;
2360 evalue_range_reduction(EP);
2362 return EP;
2365 static evalue* split_sure(Polyhedron *P, Polyhedron *S,
2366 unsigned exist, unsigned nparam, barvinok_options *options)
2368 int nvar = P->Dimension - exist - nparam;
2370 Matrix *M = Matrix_Alloc(exist, S->Dimension+2);
2371 for (int i = 0; i < exist; ++i)
2372 value_set_si(M->p[i][nvar+i+1], 1);
2373 Polyhedron *O = S;
2374 S = DomainAddRays(S, M, options->MaxRays);
2375 Polyhedron_Free(O);
2376 Polyhedron *F = DomainAddRays(P, M, options->MaxRays);
2377 Polyhedron *D = DomainDifference(F, S, options->MaxRays);
2378 O = D;
2379 D = Disjoint_Domain(D, 0, options->MaxRays);
2380 Polyhedron_Free(F);
2381 Domain_Free(O);
2382 Matrix_Free(M);
2384 M = Matrix_Alloc(P->Dimension+1-exist, P->Dimension+1);
2385 for (int j = 0; j < nvar; ++j)
2386 value_set_si(M->p[j][j], 1);
2387 for (int j = 0; j < nparam+1; ++j)
2388 value_set_si(M->p[nvar+j][nvar+exist+j], 1);
2389 Polyhedron *T = Polyhedron_Image(S, M, options->MaxRays);
2390 evalue *EP = barvinok_enumerate_e_with_options(T, 0, nparam, options);
2391 Polyhedron_Free(S);
2392 Polyhedron_Free(T);
2393 Matrix_Free(M);
2395 for (Polyhedron *Q = D; Q; Q = Q->next) {
2396 Polyhedron *N = Q->next;
2397 Q->next = 0;
2398 T = DomainIntersection(P, Q, options->MaxRays);
2399 evalue *E = barvinok_enumerate_e_with_options(T, exist, nparam, options);
2400 eadd(E, EP);
2401 free_evalue_refs(E);
2402 free(E);
2403 Polyhedron_Free(T);
2404 Q->next = N;
2406 Domain_Free(D);
2407 return EP;
2410 static evalue* enumerate_sure(Polyhedron *P,
2411 unsigned exist, unsigned nparam, barvinok_options *options)
2413 int i;
2414 Polyhedron *S = P;
2415 int nvar = P->Dimension - exist - nparam;
2416 Value lcm;
2417 Value f;
2418 value_init(lcm);
2419 value_init(f);
2421 for (i = 0; i < exist; ++i) {
2422 Matrix *M = Matrix_Alloc(S->NbConstraints, S->Dimension+2);
2423 int c = 0;
2424 value_set_si(lcm, 1);
2425 for (int j = 0; j < S->NbConstraints; ++j) {
2426 if (value_negz_p(S->Constraint[j][1+nvar+i]))
2427 continue;
2428 if (value_one_p(S->Constraint[j][1+nvar+i]))
2429 continue;
2430 value_lcm(lcm, S->Constraint[j][1+nvar+i], &lcm);
2433 for (int j = 0; j < S->NbConstraints; ++j) {
2434 if (value_negz_p(S->Constraint[j][1+nvar+i]))
2435 continue;
2436 if (value_one_p(S->Constraint[j][1+nvar+i]))
2437 continue;
2438 value_division(f, lcm, S->Constraint[j][1+nvar+i]);
2439 Vector_Scale(S->Constraint[j], M->p[c], f, S->Dimension+2);
2440 value_subtract(M->p[c][S->Dimension+1],
2441 M->p[c][S->Dimension+1],
2442 lcm);
2443 value_increment(M->p[c][S->Dimension+1],
2444 M->p[c][S->Dimension+1]);
2445 ++c;
2447 Polyhedron *O = S;
2448 S = AddConstraints(M->p[0], c, S, options->MaxRays);
2449 if (O != P)
2450 Polyhedron_Free(O);
2451 Matrix_Free(M);
2452 if (emptyQ(S)) {
2453 Polyhedron_Free(S);
2454 value_clear(lcm);
2455 value_clear(f);
2456 return 0;
2459 value_clear(lcm);
2460 value_clear(f);
2462 #ifdef DEBUG_ER
2463 fprintf(stderr, "\nER: Sure\n");
2464 #endif /* DEBUG_ER */
2466 return split_sure(P, S, exist, nparam, options);
2469 static evalue* enumerate_sure2(Polyhedron *P,
2470 unsigned exist, unsigned nparam, barvinok_options *options)
2472 int nvar = P->Dimension - exist - nparam;
2473 int r;
2474 for (r = 0; r < P->NbRays; ++r)
2475 if (value_one_p(P->Ray[r][0]) &&
2476 value_one_p(P->Ray[r][P->Dimension+1]))
2477 break;
2479 if (r >= P->NbRays)
2480 return 0;
2482 Matrix *M = Matrix_Alloc(nvar + 1 + nparam, P->Dimension+2);
2483 for (int i = 0; i < nvar; ++i)
2484 value_set_si(M->p[i][1+i], 1);
2485 for (int i = 0; i < nparam; ++i)
2486 value_set_si(M->p[i+nvar][1+nvar+exist+i], 1);
2487 Vector_Copy(P->Ray[r]+1+nvar, M->p[nvar+nparam]+1+nvar, exist);
2488 value_set_si(M->p[nvar+nparam][0], 1);
2489 value_set_si(M->p[nvar+nparam][P->Dimension+1], 1);
2490 Polyhedron * F = Rays2Polyhedron(M, options->MaxRays);
2491 Matrix_Free(M);
2493 Polyhedron *I = DomainIntersection(F, P, options->MaxRays);
2494 Polyhedron_Free(F);
2496 #ifdef DEBUG_ER
2497 fprintf(stderr, "\nER: Sure2\n");
2498 #endif /* DEBUG_ER */
2500 return split_sure(P, I, exist, nparam, options);
2503 static evalue* enumerate_cyclic(Polyhedron *P,
2504 unsigned exist, unsigned nparam,
2505 evalue * EP, int r, int p, unsigned MaxRays)
2507 int nvar = P->Dimension - exist - nparam;
2509 /* If EP in its fractional maps only contains references
2510 * to the remainder parameter with appropriate coefficients
2511 * then we could in principle avoid adding existentially
2512 * quantified variables to the validity domains.
2513 * We'd have to replace the remainder by m { p/m }
2514 * and multiply with an appropriate factor that is one
2515 * only in the appropriate range.
2516 * This last multiplication can be avoided if EP
2517 * has a single validity domain with no (further)
2518 * constraints on the remainder parameter
2521 Matrix *CT = Matrix_Alloc(nparam+1, nparam+3);
2522 Matrix *M = Matrix_Alloc(1, 1+nparam+3);
2523 for (int j = 0; j < nparam; ++j)
2524 if (j != p)
2525 value_set_si(CT->p[j][j], 1);
2526 value_set_si(CT->p[p][nparam+1], 1);
2527 value_set_si(CT->p[nparam][nparam+2], 1);
2528 value_set_si(M->p[0][1+p], -1);
2529 value_absolute(M->p[0][1+nparam], P->Ray[0][1+nvar+exist+p]);
2530 value_set_si(M->p[0][1+nparam+1], 1);
2531 Polyhedron *CEq = Constraints2Polyhedron(M, 1);
2532 Matrix_Free(M);
2533 addeliminatedparams_enum(EP, CT, CEq, MaxRays, nparam);
2534 Polyhedron_Free(CEq);
2535 Matrix_Free(CT);
2537 return EP;
2540 static void enumerate_vd_add_ray(evalue *EP, Matrix *Rays, unsigned MaxRays)
2542 if (value_notzero_p(EP->d))
2543 return;
2545 assert(EP->x.p->type == partition);
2546 assert(EP->x.p->pos == EVALUE_DOMAIN(EP->x.p->arr[0])->Dimension);
2547 for (int i = 0; i < EP->x.p->size/2; ++i) {
2548 Polyhedron *D = EVALUE_DOMAIN(EP->x.p->arr[2*i]);
2549 Polyhedron *N = DomainAddRays(D, Rays, MaxRays);
2550 EVALUE_SET_DOMAIN(EP->x.p->arr[2*i], N);
2551 Domain_Free(D);
2555 static evalue* enumerate_line(Polyhedron *P,
2556 unsigned exist, unsigned nparam, barvinok_options *options)
2558 if (P->NbBid == 0)
2559 return 0;
2561 #ifdef DEBUG_ER
2562 fprintf(stderr, "\nER: Line\n");
2563 #endif /* DEBUG_ER */
2565 int nvar = P->Dimension - exist - nparam;
2566 int i, j;
2567 for (i = 0; i < nparam; ++i)
2568 if (value_notzero_p(P->Ray[0][1+nvar+exist+i]))
2569 break;
2570 assert(i < nparam);
2571 for (j = i+1; j < nparam; ++j)
2572 if (value_notzero_p(P->Ray[0][1+nvar+exist+i]))
2573 break;
2574 assert(j >= nparam); // for now
2576 Matrix *M = Matrix_Alloc(2, P->Dimension+2);
2577 value_set_si(M->p[0][0], 1);
2578 value_set_si(M->p[0][1+nvar+exist+i], 1);
2579 value_set_si(M->p[1][0], 1);
2580 value_set_si(M->p[1][1+nvar+exist+i], -1);
2581 value_absolute(M->p[1][1+P->Dimension], P->Ray[0][1+nvar+exist+i]);
2582 value_decrement(M->p[1][1+P->Dimension], M->p[1][1+P->Dimension]);
2583 Polyhedron *S = AddConstraints(M->p[0], 2, P, options->MaxRays);
2584 evalue *EP = barvinok_enumerate_e_with_options(S, exist, nparam, options);
2585 Polyhedron_Free(S);
2586 Matrix_Free(M);
2588 return enumerate_cyclic(P, exist, nparam, EP, 0, i, options->MaxRays);
2591 static int single_param_pos(Polyhedron*P, unsigned exist, unsigned nparam,
2592 int r)
2594 int nvar = P->Dimension - exist - nparam;
2595 if (First_Non_Zero(P->Ray[r]+1, nvar) != -1)
2596 return -1;
2597 int i = First_Non_Zero(P->Ray[r]+1+nvar+exist, nparam);
2598 if (i == -1)
2599 return -1;
2600 if (First_Non_Zero(P->Ray[r]+1+nvar+exist+1, nparam-i-1) != -1)
2601 return -1;
2602 return i;
2605 static evalue* enumerate_remove_ray(Polyhedron *P, int r,
2606 unsigned exist, unsigned nparam, barvinok_options *options)
2608 #ifdef DEBUG_ER
2609 fprintf(stderr, "\nER: RedundantRay\n");
2610 #endif /* DEBUG_ER */
2612 Value one;
2613 value_init(one);
2614 value_set_si(one, 1);
2615 int len = P->NbRays-1;
2616 Matrix *M = Matrix_Alloc(2 * len, P->Dimension+2);
2617 Vector_Copy(P->Ray[0], M->p[0], r * (P->Dimension+2));
2618 Vector_Copy(P->Ray[r+1], M->p[r], (len-r) * (P->Dimension+2));
2619 for (int j = 0; j < P->NbRays; ++j) {
2620 if (j == r)
2621 continue;
2622 Vector_Combine(P->Ray[j], P->Ray[r], M->p[len+j-(j>r)],
2623 one, P->Ray[j][P->Dimension+1], P->Dimension+2);
2626 P = Rays2Polyhedron(M, options->MaxRays);
2627 Matrix_Free(M);
2628 evalue *EP = barvinok_enumerate_e_with_options(P, exist, nparam, options);
2629 Polyhedron_Free(P);
2630 value_clear(one);
2632 return EP;
2635 static evalue* enumerate_redundant_ray(Polyhedron *P,
2636 unsigned exist, unsigned nparam, barvinok_options *options)
2638 assert(P->NbBid == 0);
2639 int nvar = P->Dimension - exist - nparam;
2640 Value m;
2641 value_init(m);
2643 for (int r = 0; r < P->NbRays; ++r) {
2644 if (value_notzero_p(P->Ray[r][P->Dimension+1]))
2645 continue;
2646 int i1 = single_param_pos(P, exist, nparam, r);
2647 if (i1 == -1)
2648 continue;
2649 for (int r2 = r+1; r2 < P->NbRays; ++r2) {
2650 if (value_notzero_p(P->Ray[r2][P->Dimension+1]))
2651 continue;
2652 int i2 = single_param_pos(P, exist, nparam, r2);
2653 if (i2 == -1)
2654 continue;
2655 if (i1 != i2)
2656 continue;
2658 value_division(m, P->Ray[r][1+nvar+exist+i1],
2659 P->Ray[r2][1+nvar+exist+i1]);
2660 value_multiply(m, m, P->Ray[r2][1+nvar+exist+i1]);
2661 /* r2 divides r => r redundant */
2662 if (value_eq(m, P->Ray[r][1+nvar+exist+i1])) {
2663 value_clear(m);
2664 return enumerate_remove_ray(P, r, exist, nparam, options);
2667 value_division(m, P->Ray[r2][1+nvar+exist+i1],
2668 P->Ray[r][1+nvar+exist+i1]);
2669 value_multiply(m, m, P->Ray[r][1+nvar+exist+i1]);
2670 /* r divides r2 => r2 redundant */
2671 if (value_eq(m, P->Ray[r2][1+nvar+exist+i1])) {
2672 value_clear(m);
2673 return enumerate_remove_ray(P, r2, exist, nparam, options);
2677 value_clear(m);
2678 return 0;
2681 static Polyhedron *upper_bound(Polyhedron *P,
2682 int pos, Value *max, Polyhedron **R)
2684 Value v;
2685 int r;
2686 value_init(v);
2688 *R = 0;
2689 Polyhedron *N;
2690 Polyhedron *B = 0;
2691 for (Polyhedron *Q = P; Q; Q = N) {
2692 N = Q->next;
2693 for (r = 0; r < P->NbRays; ++r) {
2694 if (value_zero_p(P->Ray[r][P->Dimension+1]) &&
2695 value_pos_p(P->Ray[r][1+pos]))
2696 break;
2698 if (r < P->NbRays) {
2699 Q->next = *R;
2700 *R = Q;
2701 continue;
2702 } else {
2703 Q->next = B;
2704 B = Q;
2706 for (r = 0; r < P->NbRays; ++r) {
2707 if (value_zero_p(P->Ray[r][P->Dimension+1]))
2708 continue;
2709 mpz_fdiv_q(v, P->Ray[r][1+pos], P->Ray[r][1+P->Dimension]);
2710 if ((!Q->next && r == 0) || value_gt(v, *max))
2711 value_assign(*max, v);
2714 value_clear(v);
2715 return B;
2718 static evalue* enumerate_ray(Polyhedron *P,
2719 unsigned exist, unsigned nparam, barvinok_options *options)
2721 assert(P->NbBid == 0);
2722 int nvar = P->Dimension - exist - nparam;
2724 int r;
2725 for (r = 0; r < P->NbRays; ++r)
2726 if (value_zero_p(P->Ray[r][P->Dimension+1]))
2727 break;
2728 if (r >= P->NbRays)
2729 return 0;
2731 int r2;
2732 for (r2 = r+1; r2 < P->NbRays; ++r2)
2733 if (value_zero_p(P->Ray[r2][P->Dimension+1]))
2734 break;
2735 if (r2 < P->NbRays) {
2736 if (nvar > 0)
2737 return enumerate_sum(P, exist, nparam, options);
2740 #ifdef DEBUG_ER
2741 fprintf(stderr, "\nER: Ray\n");
2742 #endif /* DEBUG_ER */
2744 Value m;
2745 Value one;
2746 value_init(m);
2747 value_init(one);
2748 value_set_si(one, 1);
2749 int i = single_param_pos(P, exist, nparam, r);
2750 assert(i != -1); // for now;
2752 Matrix *M = Matrix_Alloc(P->NbRays, P->Dimension+2);
2753 for (int j = 0; j < P->NbRays; ++j) {
2754 Vector_Combine(P->Ray[j], P->Ray[r], M->p[j],
2755 one, P->Ray[j][P->Dimension+1], P->Dimension+2);
2757 Polyhedron *S = Rays2Polyhedron(M, options->MaxRays);
2758 Matrix_Free(M);
2759 Polyhedron *D = DomainDifference(P, S, options->MaxRays);
2760 Polyhedron_Free(S);
2761 // Polyhedron_Print(stderr, P_VALUE_FMT, D);
2762 assert(value_pos_p(P->Ray[r][1+nvar+exist+i])); // for now
2763 Polyhedron *R;
2764 D = upper_bound(D, nvar+exist+i, &m, &R);
2765 assert(D);
2766 Domain_Free(D);
2768 M = Matrix_Alloc(2, P->Dimension+2);
2769 value_set_si(M->p[0][0], 1);
2770 value_set_si(M->p[1][0], 1);
2771 value_set_si(M->p[0][1+nvar+exist+i], -1);
2772 value_set_si(M->p[1][1+nvar+exist+i], 1);
2773 value_assign(M->p[0][1+P->Dimension], m);
2774 value_oppose(M->p[1][1+P->Dimension], m);
2775 value_addto(M->p[1][1+P->Dimension], M->p[1][1+P->Dimension],
2776 P->Ray[r][1+nvar+exist+i]);
2777 value_decrement(M->p[1][1+P->Dimension], M->p[1][1+P->Dimension]);
2778 // Matrix_Print(stderr, P_VALUE_FMT, M);
2779 D = AddConstraints(M->p[0], 2, P, options->MaxRays);
2780 // Polyhedron_Print(stderr, P_VALUE_FMT, D);
2781 value_subtract(M->p[0][1+P->Dimension], M->p[0][1+P->Dimension],
2782 P->Ray[r][1+nvar+exist+i]);
2783 // Matrix_Print(stderr, P_VALUE_FMT, M);
2784 S = AddConstraints(M->p[0], 1, P, options->MaxRays);
2785 // Polyhedron_Print(stderr, P_VALUE_FMT, S);
2786 Matrix_Free(M);
2788 evalue *EP = barvinok_enumerate_e_with_options(D, exist, nparam, options);
2789 Polyhedron_Free(D);
2790 value_clear(one);
2791 value_clear(m);
2793 if (value_notone_p(P->Ray[r][1+nvar+exist+i]))
2794 EP = enumerate_cyclic(P, exist, nparam, EP, r, i, options->MaxRays);
2795 else {
2796 M = Matrix_Alloc(1, nparam+2);
2797 value_set_si(M->p[0][0], 1);
2798 value_set_si(M->p[0][1+i], 1);
2799 enumerate_vd_add_ray(EP, M, options->MaxRays);
2800 Matrix_Free(M);
2803 if (!emptyQ(S)) {
2804 evalue *E = barvinok_enumerate_e_with_options(S, exist, nparam, options);
2805 eadd(E, EP);
2806 free_evalue_refs(E);
2807 free(E);
2809 Polyhedron_Free(S);
2811 if (R) {
2812 assert(nvar == 0);
2813 evalue *ER = enumerate_or(R, exist, nparam, options);
2814 eor(ER, EP);
2815 free_evalue_refs(ER);
2816 free(ER);
2819 return EP;
2822 static evalue* enumerate_vd(Polyhedron **PA,
2823 unsigned exist, unsigned nparam, barvinok_options *options)
2825 Polyhedron *P = *PA;
2826 int nvar = P->Dimension - exist - nparam;
2827 Param_Polyhedron *PP = NULL;
2828 Polyhedron *C = Universe_Polyhedron(nparam);
2829 Polyhedron *CEq;
2830 Matrix *CT;
2831 Polyhedron *PR = P;
2832 PP = Polyhedron2Param_SimplifiedDomain(&PR,C, options->MaxRays,&CEq,&CT);
2833 Polyhedron_Free(C);
2835 int nd;
2836 Param_Domain *D, *last;
2837 Value c;
2838 value_init(c);
2839 for (nd = 0, D=PP->D; D; D=D->next, ++nd)
2842 Polyhedron **VD = new Polyhedron_p[nd];
2843 Polyhedron **fVD = new Polyhedron_p[nd];
2844 for(nd = 0, D=PP->D; D; D=D->next) {
2845 Polyhedron *rVD = reduce_domain(D->Domain, CT, CEq, fVD, nd, options);
2846 if (!rVD)
2847 continue;
2849 VD[nd++] = rVD;
2850 last = D;
2853 evalue *EP = 0;
2855 if (nd == 0)
2856 EP = evalue_zero();
2858 /* This doesn't seem to have any effect */
2859 if (nd == 1) {
2860 Polyhedron *CA = align_context(VD[0], P->Dimension, options->MaxRays);
2861 Polyhedron *O = P;
2862 P = DomainIntersection(P, CA, options->MaxRays);
2863 if (O != *PA)
2864 Polyhedron_Free(O);
2865 Polyhedron_Free(CA);
2866 if (emptyQ(P))
2867 EP = evalue_zero();
2870 if (!EP && CT->NbColumns != CT->NbRows) {
2871 Polyhedron *CEqr = DomainImage(CEq, CT, options->MaxRays);
2872 Polyhedron *CA = align_context(CEqr, PR->Dimension, options->MaxRays);
2873 Polyhedron *I = DomainIntersection(PR, CA, options->MaxRays);
2874 Polyhedron_Free(CEqr);
2875 Polyhedron_Free(CA);
2876 #ifdef DEBUG_ER
2877 fprintf(stderr, "\nER: Eliminate\n");
2878 #endif /* DEBUG_ER */
2879 nparam -= CT->NbColumns - CT->NbRows;
2880 EP = barvinok_enumerate_e_with_options(I, exist, nparam, options);
2881 nparam += CT->NbColumns - CT->NbRows;
2882 addeliminatedparams_enum(EP, CT, CEq, options->MaxRays, nparam);
2883 Polyhedron_Free(I);
2885 if (PR != *PA)
2886 Polyhedron_Free(PR);
2887 PR = 0;
2889 if (!EP && nd > 1) {
2890 #ifdef DEBUG_ER
2891 fprintf(stderr, "\nER: VD\n");
2892 #endif /* DEBUG_ER */
2893 for (int i = 0; i < nd; ++i) {
2894 Polyhedron *CA = align_context(VD[i], P->Dimension, options->MaxRays);
2895 Polyhedron *I = DomainIntersection(P, CA, options->MaxRays);
2897 if (i == 0)
2898 EP = barvinok_enumerate_e_with_options(I, exist, nparam, options);
2899 else {
2900 evalue *E = barvinok_enumerate_e_with_options(I, exist, nparam,
2901 options);
2902 eadd(E, EP);
2903 free_evalue_refs(E);
2904 free(E);
2906 Polyhedron_Free(I);
2907 Polyhedron_Free(CA);
2911 for (int i = 0; i < nd; ++i) {
2912 Polyhedron_Free(VD[i]);
2913 Polyhedron_Free(fVD[i]);
2915 delete [] VD;
2916 delete [] fVD;
2917 value_clear(c);
2919 if (!EP && nvar == 0) {
2920 Value f;
2921 value_init(f);
2922 Param_Vertices *V, *V2;
2923 Matrix* M = Matrix_Alloc(1, P->Dimension+2);
2925 FORALL_PVertex_in_ParamPolyhedron(V, last, PP) {
2926 bool found = false;
2927 FORALL_PVertex_in_ParamPolyhedron(V2, last, PP) {
2928 if (V == V2) {
2929 found = true;
2930 continue;
2932 if (!found)
2933 continue;
2934 for (int i = 0; i < exist; ++i) {
2935 value_oppose(f, V->Vertex->p[i][nparam+1]);
2936 Vector_Combine(V->Vertex->p[i],
2937 V2->Vertex->p[i],
2938 M->p[0] + 1 + nvar + exist,
2939 V2->Vertex->p[i][nparam+1],
2941 nparam+1);
2942 int j;
2943 for (j = 0; j < nparam; ++j)
2944 if (value_notzero_p(M->p[0][1+nvar+exist+j]))
2945 break;
2946 if (j >= nparam)
2947 continue;
2948 ConstraintSimplify(M->p[0], M->p[0],
2949 P->Dimension+2, &f);
2950 value_set_si(M->p[0][0], 0);
2951 Polyhedron *para = AddConstraints(M->p[0], 1, P,
2952 options->MaxRays);
2953 if (emptyQ(para)) {
2954 Polyhedron_Free(para);
2955 continue;
2957 Polyhedron *pos, *neg;
2958 value_set_si(M->p[0][0], 1);
2959 value_decrement(M->p[0][P->Dimension+1],
2960 M->p[0][P->Dimension+1]);
2961 neg = AddConstraints(M->p[0], 1, P, options->MaxRays);
2962 value_set_si(f, -1);
2963 Vector_Scale(M->p[0]+1, M->p[0]+1, f,
2964 P->Dimension+1);
2965 value_decrement(M->p[0][P->Dimension+1],
2966 M->p[0][P->Dimension+1]);
2967 value_decrement(M->p[0][P->Dimension+1],
2968 M->p[0][P->Dimension+1]);
2969 pos = AddConstraints(M->p[0], 1, P, options->MaxRays);
2970 if (emptyQ(neg) && emptyQ(pos)) {
2971 Polyhedron_Free(para);
2972 Polyhedron_Free(pos);
2973 Polyhedron_Free(neg);
2974 continue;
2976 #ifdef DEBUG_ER
2977 fprintf(stderr, "\nER: Order\n");
2978 #endif /* DEBUG_ER */
2979 EP = barvinok_enumerate_e_with_options(para, exist, nparam,
2980 options);
2981 evalue *E;
2982 if (!emptyQ(pos)) {
2983 E = barvinok_enumerate_e_with_options(pos, exist, nparam,
2984 options);
2985 eadd(E, EP);
2986 free_evalue_refs(E);
2987 free(E);
2989 if (!emptyQ(neg)) {
2990 E = barvinok_enumerate_e_with_options(neg, exist, nparam,
2991 options);
2992 eadd(E, EP);
2993 free_evalue_refs(E);
2994 free(E);
2996 Polyhedron_Free(para);
2997 Polyhedron_Free(pos);
2998 Polyhedron_Free(neg);
2999 break;
3001 if (EP)
3002 break;
3003 } END_FORALL_PVertex_in_ParamPolyhedron;
3004 if (EP)
3005 break;
3006 } END_FORALL_PVertex_in_ParamPolyhedron;
3008 if (!EP) {
3009 /* Search for vertex coordinate to split on */
3010 /* First look for one independent of the parameters */
3011 FORALL_PVertex_in_ParamPolyhedron(V, last, PP) {
3012 for (int i = 0; i < exist; ++i) {
3013 int j;
3014 for (j = 0; j < nparam; ++j)
3015 if (value_notzero_p(V->Vertex->p[i][j]))
3016 break;
3017 if (j < nparam)
3018 continue;
3019 value_set_si(M->p[0][0], 1);
3020 Vector_Set(M->p[0]+1, 0, nvar+exist);
3021 Vector_Copy(V->Vertex->p[i],
3022 M->p[0] + 1 + nvar + exist, nparam+1);
3023 value_oppose(M->p[0][1+nvar+i],
3024 V->Vertex->p[i][nparam+1]);
3026 Polyhedron *pos, *neg;
3027 value_set_si(M->p[0][0], 1);
3028 value_decrement(M->p[0][P->Dimension+1],
3029 M->p[0][P->Dimension+1]);
3030 neg = AddConstraints(M->p[0], 1, P, options->MaxRays);
3031 value_set_si(f, -1);
3032 Vector_Scale(M->p[0]+1, M->p[0]+1, f,
3033 P->Dimension+1);
3034 value_decrement(M->p[0][P->Dimension+1],
3035 M->p[0][P->Dimension+1]);
3036 value_decrement(M->p[0][P->Dimension+1],
3037 M->p[0][P->Dimension+1]);
3038 pos = AddConstraints(M->p[0], 1, P, options->MaxRays);
3039 if (emptyQ(neg) || emptyQ(pos)) {
3040 Polyhedron_Free(pos);
3041 Polyhedron_Free(neg);
3042 continue;
3044 Polyhedron_Free(pos);
3045 value_increment(M->p[0][P->Dimension+1],
3046 M->p[0][P->Dimension+1]);
3047 pos = AddConstraints(M->p[0], 1, P, options->MaxRays);
3048 #ifdef DEBUG_ER
3049 fprintf(stderr, "\nER: Vertex\n");
3050 #endif /* DEBUG_ER */
3051 pos->next = neg;
3052 EP = enumerate_or(pos, exist, nparam, options);
3053 break;
3055 if (EP)
3056 break;
3057 } END_FORALL_PVertex_in_ParamPolyhedron;
3060 if (!EP) {
3061 /* Search for vertex coordinate to split on */
3062 /* Now look for one that depends on the parameters */
3063 FORALL_PVertex_in_ParamPolyhedron(V, last, PP) {
3064 for (int i = 0; i < exist; ++i) {
3065 value_set_si(M->p[0][0], 1);
3066 Vector_Set(M->p[0]+1, 0, nvar+exist);
3067 Vector_Copy(V->Vertex->p[i],
3068 M->p[0] + 1 + nvar + exist, nparam+1);
3069 value_oppose(M->p[0][1+nvar+i],
3070 V->Vertex->p[i][nparam+1]);
3072 Polyhedron *pos, *neg;
3073 value_set_si(M->p[0][0], 1);
3074 value_decrement(M->p[0][P->Dimension+1],
3075 M->p[0][P->Dimension+1]);
3076 neg = AddConstraints(M->p[0], 1, P, options->MaxRays);
3077 value_set_si(f, -1);
3078 Vector_Scale(M->p[0]+1, M->p[0]+1, f,
3079 P->Dimension+1);
3080 value_decrement(M->p[0][P->Dimension+1],
3081 M->p[0][P->Dimension+1]);
3082 value_decrement(M->p[0][P->Dimension+1],
3083 M->p[0][P->Dimension+1]);
3084 pos = AddConstraints(M->p[0], 1, P, options->MaxRays);
3085 if (emptyQ(neg) || emptyQ(pos)) {
3086 Polyhedron_Free(pos);
3087 Polyhedron_Free(neg);
3088 continue;
3090 Polyhedron_Free(pos);
3091 value_increment(M->p[0][P->Dimension+1],
3092 M->p[0][P->Dimension+1]);
3093 pos = AddConstraints(M->p[0], 1, P, options->MaxRays);
3094 #ifdef DEBUG_ER
3095 fprintf(stderr, "\nER: ParamVertex\n");
3096 #endif /* DEBUG_ER */
3097 pos->next = neg;
3098 EP = enumerate_or(pos, exist, nparam, options);
3099 break;
3101 if (EP)
3102 break;
3103 } END_FORALL_PVertex_in_ParamPolyhedron;
3106 Matrix_Free(M);
3107 value_clear(f);
3110 if (CEq)
3111 Polyhedron_Free(CEq);
3112 if (CT)
3113 Matrix_Free(CT);
3114 if (PP)
3115 Param_Polyhedron_Free(PP);
3116 *PA = P;
3118 return EP;
3121 evalue* barvinok_enumerate_pip(Polyhedron *P, unsigned exist, unsigned nparam,
3122 unsigned MaxRays)
3124 evalue *E;
3125 barvinok_options *options = barvinok_options_new_with_defaults();
3126 options->MaxRays = MaxRays;
3127 E = barvinok_enumerate_pip_with_options(P, exist, nparam, options);
3128 barvinok_options_free(options);
3129 return E;
3132 #ifndef HAVE_PIPLIB
3133 evalue *barvinok_enumerate_pip_with_options(Polyhedron *P,
3134 unsigned exist, unsigned nparam, struct barvinok_options *options)
3136 return 0;
3138 #else
3139 evalue *barvinok_enumerate_pip_with_options(Polyhedron *P,
3140 unsigned exist, unsigned nparam, struct barvinok_options *options)
3142 int nvar = P->Dimension - exist - nparam;
3143 evalue *EP = evalue_zero();
3144 Polyhedron *Q, *N;
3146 #ifdef DEBUG_ER
3147 fprintf(stderr, "\nER: PIP\n");
3148 #endif /* DEBUG_ER */
3150 Polyhedron *D = pip_projectout(P, nvar, exist, nparam);
3151 for (Q = D; Q; Q = N) {
3152 N = Q->next;
3153 Q->next = 0;
3154 evalue *E;
3155 exist = Q->Dimension - nvar - nparam;
3156 E = barvinok_enumerate_e_with_options(Q, exist, nparam, options);
3157 Polyhedron_Free(Q);
3158 eadd(E, EP);
3159 free_evalue_refs(E);
3160 free(E);
3163 return EP;
3165 #endif
3168 static bool is_single(Value *row, int pos, int len)
3170 return First_Non_Zero(row, pos) == -1 &&
3171 First_Non_Zero(row+pos+1, len-pos-1) == -1;
3174 static evalue* barvinok_enumerate_e_r(Polyhedron *P,
3175 unsigned exist, unsigned nparam, barvinok_options *options);
3177 #ifdef DEBUG_ER
3178 static int er_level = 0;
3180 evalue* barvinok_enumerate_e_with_options(Polyhedron *P,
3181 unsigned exist, unsigned nparam, barvinok_options *options)
3183 fprintf(stderr, "\nER: level %i\n", er_level);
3185 Polyhedron_PrintConstraints(stderr, P_VALUE_FMT, P);
3186 fprintf(stderr, "\nE %d\nP %d\n", exist, nparam);
3187 ++er_level;
3188 P = DomainConstraintSimplify(Polyhedron_Copy(P), options->MaxRays);
3189 evalue *EP = barvinok_enumerate_e_r(P, exist, nparam, options);
3190 Polyhedron_Free(P);
3191 --er_level;
3192 return EP;
3194 #else
3195 evalue* barvinok_enumerate_e_with_options(Polyhedron *P,
3196 unsigned exist, unsigned nparam, barvinok_options *options)
3198 P = DomainConstraintSimplify(Polyhedron_Copy(P), options->MaxRays);
3199 evalue *EP = barvinok_enumerate_e_r(P, exist, nparam, options);
3200 Polyhedron_Free(P);
3201 return EP;
3203 #endif
3205 evalue* barvinok_enumerate_e(Polyhedron *P, unsigned exist, unsigned nparam,
3206 unsigned MaxRays)
3208 evalue *E;
3209 barvinok_options *options = barvinok_options_new_with_defaults();
3210 options->MaxRays = MaxRays;
3211 E = barvinok_enumerate_e_with_options(P, exist, nparam, options);
3212 barvinok_options_free(options);
3213 return E;
3216 static evalue* barvinok_enumerate_e_r(Polyhedron *P,
3217 unsigned exist, unsigned nparam, barvinok_options *options)
3219 if (exist == 0) {
3220 Polyhedron *U = Universe_Polyhedron(nparam);
3221 evalue *EP = barvinok_enumerate_with_options(P, U, options);
3222 //char *param_name[] = {"P", "Q", "R", "S", "T" };
3223 //print_evalue(stdout, EP, param_name);
3224 Polyhedron_Free(U);
3225 return EP;
3228 int nvar = P->Dimension - exist - nparam;
3229 int len = P->Dimension + 2;
3231 /* for now */
3232 POL_ENSURE_FACETS(P);
3233 POL_ENSURE_VERTICES(P);
3235 if (emptyQ(P))
3236 return evalue_zero();
3238 if (nvar == 0 && nparam == 0) {
3239 evalue *EP = evalue_zero();
3240 barvinok_count_with_options(P, &EP->x.n, options);
3241 if (value_pos_p(EP->x.n))
3242 value_set_si(EP->x.n, 1);
3243 return EP;
3246 int r;
3247 for (r = 0; r < P->NbRays; ++r)
3248 if (value_zero_p(P->Ray[r][0]) ||
3249 value_zero_p(P->Ray[r][P->Dimension+1])) {
3250 int i;
3251 for (i = 0; i < nvar; ++i)
3252 if (value_notzero_p(P->Ray[r][i+1]))
3253 break;
3254 if (i >= nvar)
3255 continue;
3256 for (i = nvar + exist; i < nvar + exist + nparam; ++i)
3257 if (value_notzero_p(P->Ray[r][i+1]))
3258 break;
3259 if (i >= nvar + exist + nparam)
3260 break;
3262 if (r < P->NbRays) {
3263 evalue *EP = evalue_zero();
3264 value_set_si(EP->x.n, -1);
3265 return EP;
3268 int first;
3269 for (r = 0; r < P->NbEq; ++r)
3270 if ((first = First_Non_Zero(P->Constraint[r]+1+nvar, exist)) != -1)
3271 break;
3272 if (r < P->NbEq) {
3273 if (First_Non_Zero(P->Constraint[r]+1+nvar+first+1,
3274 exist-first-1) != -1) {
3275 Polyhedron *T = rotate_along(P, r, nvar, exist, options->MaxRays);
3276 #ifdef DEBUG_ER
3277 fprintf(stderr, "\nER: Equality\n");
3278 #endif /* DEBUG_ER */
3279 evalue *EP = barvinok_enumerate_e_with_options(T, exist-1, nparam,
3280 options);
3281 Polyhedron_Free(T);
3282 return EP;
3283 } else {
3284 #ifdef DEBUG_ER
3285 fprintf(stderr, "\nER: Fixed\n");
3286 #endif /* DEBUG_ER */
3287 if (first == 0)
3288 return barvinok_enumerate_e_with_options(P, exist-1, nparam,
3289 options);
3290 else {
3291 Polyhedron *T = Polyhedron_Copy(P);
3292 SwapColumns(T, nvar+1, nvar+1+first);
3293 evalue *EP = barvinok_enumerate_e_with_options(T, exist-1, nparam,
3294 options);
3295 Polyhedron_Free(T);
3296 return EP;
3301 Vector *row = Vector_Alloc(len);
3302 value_set_si(row->p[0], 1);
3304 Value f;
3305 value_init(f);
3307 enum constraint* info = new constraint[exist];
3308 for (int i = 0; i < exist; ++i) {
3309 info[i] = ALL_POS;
3310 for (int l = P->NbEq; l < P->NbConstraints; ++l) {
3311 if (value_negz_p(P->Constraint[l][nvar+i+1]))
3312 continue;
3313 bool l_parallel = is_single(P->Constraint[l]+nvar+1, i, exist);
3314 for (int u = P->NbEq; u < P->NbConstraints; ++u) {
3315 if (value_posz_p(P->Constraint[u][nvar+i+1]))
3316 continue;
3317 bool lu_parallel = l_parallel ||
3318 is_single(P->Constraint[u]+nvar+1, i, exist);
3319 value_oppose(f, P->Constraint[u][nvar+i+1]);
3320 Vector_Combine(P->Constraint[l]+1, P->Constraint[u]+1, row->p+1,
3321 f, P->Constraint[l][nvar+i+1], len-1);
3322 if (!(info[i] & INDEPENDENT)) {
3323 int j;
3324 for (j = 0; j < exist; ++j)
3325 if (j != i && value_notzero_p(row->p[nvar+j+1]))
3326 break;
3327 if (j == exist) {
3328 //printf("independent: i: %d, l: %d, u: %d\n", i, l, u);
3329 info[i] = (constraint)(info[i] | INDEPENDENT);
3332 if (info[i] & ALL_POS) {
3333 value_addto(row->p[len-1], row->p[len-1],
3334 P->Constraint[l][nvar+i+1]);
3335 value_addto(row->p[len-1], row->p[len-1], f);
3336 value_multiply(f, f, P->Constraint[l][nvar+i+1]);
3337 value_subtract(row->p[len-1], row->p[len-1], f);
3338 value_decrement(row->p[len-1], row->p[len-1]);
3339 ConstraintSimplify(row->p, row->p, len, &f);
3340 value_set_si(f, -1);
3341 Vector_Scale(row->p+1, row->p+1, f, len-1);
3342 value_decrement(row->p[len-1], row->p[len-1]);
3343 Polyhedron *T = AddConstraints(row->p, 1, P, options->MaxRays);
3344 if (!emptyQ(T)) {
3345 //printf("not all_pos: i: %d, l: %d, u: %d\n", i, l, u);
3346 info[i] = (constraint)(info[i] ^ ALL_POS);
3348 //puts("pos remainder");
3349 //Polyhedron_Print(stdout, P_VALUE_FMT, T);
3350 Polyhedron_Free(T);
3352 if (!(info[i] & ONE_NEG)) {
3353 if (lu_parallel) {
3354 negative_test_constraint(P->Constraint[l],
3355 P->Constraint[u],
3356 row->p, nvar+i, len, &f);
3357 oppose_constraint(row->p, len, &f);
3358 Polyhedron *T = AddConstraints(row->p, 1, P,
3359 options->MaxRays);
3360 if (emptyQ(T)) {
3361 //printf("one_neg i: %d, l: %d, u: %d\n", i, l, u);
3362 info[i] = (constraint)(info[i] | ONE_NEG);
3364 //puts("neg remainder");
3365 //Polyhedron_Print(stdout, P_VALUE_FMT, T);
3366 Polyhedron_Free(T);
3367 } else if (!(info[i] & ROT_NEG)) {
3368 if (parallel_constraints(P->Constraint[l],
3369 P->Constraint[u],
3370 row->p, nvar, exist)) {
3371 negative_test_constraint7(P->Constraint[l],
3372 P->Constraint[u],
3373 row->p, nvar, exist,
3374 len, &f);
3375 oppose_constraint(row->p, len, &f);
3376 Polyhedron *T = AddConstraints(row->p, 1, P,
3377 options->MaxRays);
3378 if (emptyQ(T)) {
3379 // printf("rot_neg i: %d, l: %d, u: %d\n", i, l, u);
3380 info[i] = (constraint)(info[i] | ROT_NEG);
3381 r = l;
3383 //puts("neg remainder");
3384 //Polyhedron_Print(stdout, P_VALUE_FMT, T);
3385 Polyhedron_Free(T);
3389 if (!(info[i] & ALL_POS) && (info[i] & (ONE_NEG | ROT_NEG)))
3390 goto next;
3393 if (info[i] & ALL_POS)
3394 break;
3395 next:
3400 for (int i = 0; i < exist; ++i)
3401 printf("%i: %i\n", i, info[i]);
3403 for (int i = 0; i < exist; ++i)
3404 if (info[i] & ALL_POS) {
3405 #ifdef DEBUG_ER
3406 fprintf(stderr, "\nER: Positive\n");
3407 #endif /* DEBUG_ER */
3408 // Eliminate
3409 // Maybe we should chew off some of the fat here
3410 Matrix *M = Matrix_Alloc(P->Dimension, P->Dimension+1);
3411 for (int j = 0; j < P->Dimension; ++j)
3412 value_set_si(M->p[j][j + (j >= i+nvar)], 1);
3413 Polyhedron *T = Polyhedron_Image(P, M, options->MaxRays);
3414 Matrix_Free(M);
3415 evalue *EP = barvinok_enumerate_e_with_options(T, exist-1, nparam,
3416 options);
3417 Polyhedron_Free(T);
3418 value_clear(f);
3419 Vector_Free(row);
3420 delete [] info;
3421 return EP;
3423 for (int i = 0; i < exist; ++i)
3424 if (info[i] & ONE_NEG) {
3425 #ifdef DEBUG_ER
3426 fprintf(stderr, "\nER: Negative\n");
3427 #endif /* DEBUG_ER */
3428 Vector_Free(row);
3429 value_clear(f);
3430 delete [] info;
3431 if (i == 0)
3432 return barvinok_enumerate_e_with_options(P, exist-1, nparam,
3433 options);
3434 else {
3435 Polyhedron *T = Polyhedron_Copy(P);
3436 SwapColumns(T, nvar+1, nvar+1+i);
3437 evalue *EP = barvinok_enumerate_e_with_options(T, exist-1, nparam,
3438 options);
3439 Polyhedron_Free(T);
3440 return EP;
3443 for (int i = 0; i < exist; ++i)
3444 if (info[i] & ROT_NEG) {
3445 #ifdef DEBUG_ER
3446 fprintf(stderr, "\nER: Rotate\n");
3447 #endif /* DEBUG_ER */
3448 Vector_Free(row);
3449 value_clear(f);
3450 delete [] info;
3451 Polyhedron *T = rotate_along(P, r, nvar, exist, options->MaxRays);
3452 evalue *EP = barvinok_enumerate_e_with_options(T, exist-1, nparam,
3453 options);
3454 Polyhedron_Free(T);
3455 return EP;
3457 for (int i = 0; i < exist; ++i)
3458 if (info[i] & INDEPENDENT) {
3459 Polyhedron *pos, *neg;
3461 /* Find constraint again and split off negative part */
3463 if (SplitOnVar(P, i, nvar, exist, options->MaxRays,
3464 row, f, true, &pos, &neg)) {
3465 #ifdef DEBUG_ER
3466 fprintf(stderr, "\nER: Split\n");
3467 #endif /* DEBUG_ER */
3469 evalue *EP =
3470 barvinok_enumerate_e_with_options(neg, exist-1, nparam, options);
3471 evalue *E =
3472 barvinok_enumerate_e_with_options(pos, exist, nparam, options);
3473 eadd(E, EP);
3474 free_evalue_refs(E);
3475 free(E);
3476 Polyhedron_Free(neg);
3477 Polyhedron_Free(pos);
3478 value_clear(f);
3479 Vector_Free(row);
3480 delete [] info;
3481 return EP;
3484 delete [] info;
3486 Polyhedron *O = P;
3487 Polyhedron *F;
3489 evalue *EP;
3491 EP = enumerate_line(P, exist, nparam, options);
3492 if (EP)
3493 goto out;
3495 EP = barvinok_enumerate_pip_with_options(P, exist, nparam, options);
3496 if (EP)
3497 goto out;
3499 EP = enumerate_redundant_ray(P, exist, nparam, options);
3500 if (EP)
3501 goto out;
3503 EP = enumerate_sure(P, exist, nparam, options);
3504 if (EP)
3505 goto out;
3507 EP = enumerate_ray(P, exist, nparam, options);
3508 if (EP)
3509 goto out;
3511 EP = enumerate_sure2(P, exist, nparam, options);
3512 if (EP)
3513 goto out;
3515 F = unfringe(P, options->MaxRays);
3516 if (!PolyhedronIncludes(F, P)) {
3517 #ifdef DEBUG_ER
3518 fprintf(stderr, "\nER: Fringed\n");
3519 #endif /* DEBUG_ER */
3520 EP = barvinok_enumerate_e_with_options(F, exist, nparam, options);
3521 Polyhedron_Free(F);
3522 goto out;
3524 Polyhedron_Free(F);
3526 if (nparam)
3527 EP = enumerate_vd(&P, exist, nparam, options);
3528 if (EP)
3529 goto out2;
3531 if (nvar != 0) {
3532 EP = enumerate_sum(P, exist, nparam, options);
3533 goto out2;
3536 assert(nvar == 0);
3538 int i;
3539 Polyhedron *pos, *neg;
3540 for (i = 0; i < exist; ++i)
3541 if (SplitOnVar(P, i, nvar, exist, options->MaxRays,
3542 row, f, false, &pos, &neg))
3543 break;
3545 assert (i < exist);
3547 pos->next = neg;
3548 EP = enumerate_or(pos, exist, nparam, options);
3550 out2:
3551 if (O != P)
3552 Polyhedron_Free(P);
3554 out:
3555 value_clear(f);
3556 Vector_Free(row);
3557 return EP;
3561 * remove equalities that require a "compression" of the parameters
3563 static Polyhedron *remove_more_equalities(Polyhedron *P, unsigned nparam,
3564 Matrix **CP, unsigned MaxRays)
3566 Polyhedron *Q = P;
3567 remove_all_equalities(&P, NULL, CP, NULL, nparam, MaxRays);
3568 if (P != Q)
3569 Polyhedron_Free(Q);
3570 return P;
3573 /* frees P */
3574 static gen_fun *series(Polyhedron *P, unsigned nparam, barvinok_options *options)
3576 Matrix *CP = NULL;
3577 gen_fun *gf;
3579 if (emptyQ2(P)) {
3580 Polyhedron_Free(P);
3581 return new gen_fun;
3584 assert(!Polyhedron_is_unbounded(P, nparam, options->MaxRays));
3585 assert(P->NbBid == 0);
3586 assert(Polyhedron_has_revlex_positive_rays(P, nparam));
3587 if (P->NbEq != 0)
3588 P = remove_more_equalities(P, nparam, &CP, options->MaxRays);
3589 assert(P->NbEq == 0);
3590 if (CP)
3591 nparam = CP->NbColumns-1;
3593 if (nparam == 0) {
3594 Value c;
3595 value_init(c);
3596 barvinok_count_with_options(P, &c, options);
3597 gf = new gen_fun(c);
3598 value_clear(c);
3599 } else {
3600 gf_base *red;
3601 red = gf_base::create(Polyhedron_Project(P, nparam),
3602 P->Dimension, nparam, options);
3603 POL_ENSURE_VERTICES(P);
3604 red->start_gf(P, options);
3605 gf = red->gf;
3606 delete red;
3608 if (CP) {
3609 gf->substitute(CP);
3610 Matrix_Free(CP);
3612 Polyhedron_Free(P);
3613 return gf;
3616 gen_fun * barvinok_series_with_options(Polyhedron *P, Polyhedron* C,
3617 barvinok_options *options)
3619 Polyhedron *CA;
3620 unsigned nparam = C->Dimension;
3621 gen_fun *gf;
3623 CA = align_context(C, P->Dimension, options->MaxRays);
3624 P = DomainIntersection(P, CA, options->MaxRays);
3625 Polyhedron_Free(CA);
3627 gf = series(P, nparam, options);
3629 return gf;
3632 gen_fun * barvinok_series(Polyhedron *P, Polyhedron* C, unsigned MaxRays)
3634 gen_fun *gf;
3635 barvinok_options *options = barvinok_options_new_with_defaults();
3636 options->MaxRays = MaxRays;
3637 gf = barvinok_series_with_options(P, C, options);
3638 barvinok_options_free(options);
3639 return gf;
3642 static Polyhedron *skew_into_positive_orthant(Polyhedron *D, unsigned nparam,
3643 unsigned MaxRays)
3645 Matrix *M = NULL;
3646 Value tmp;
3647 value_init(tmp);
3648 for (Polyhedron *P = D; P; P = P->next) {
3649 POL_ENSURE_VERTICES(P);
3650 assert(!Polyhedron_is_unbounded(P, nparam, MaxRays));
3651 assert(P->NbBid == 0);
3652 assert(Polyhedron_has_positive_rays(P, nparam));
3654 for (int r = 0; r < P->NbRays; ++r) {
3655 if (value_notzero_p(P->Ray[r][P->Dimension+1]))
3656 continue;
3657 for (int i = 0; i < nparam; ++i) {
3658 int j;
3659 if (value_posz_p(P->Ray[r][i+1]))
3660 continue;
3661 if (!M) {
3662 M = Matrix_Alloc(D->Dimension+1, D->Dimension+1);
3663 for (int i = 0; i < D->Dimension+1; ++i)
3664 value_set_si(M->p[i][i], 1);
3665 } else {
3666 Inner_Product(P->Ray[r]+1, M->p[i], D->Dimension+1, &tmp);
3667 if (value_posz_p(tmp))
3668 continue;
3670 for (j = P->Dimension - nparam; j < P->Dimension; ++j)
3671 if (value_pos_p(P->Ray[r][j+1]))
3672 break;
3673 assert(j < P->Dimension);
3674 value_pdivision(tmp, P->Ray[r][j+1], P->Ray[r][i+1]);
3675 value_subtract(M->p[i][j], M->p[i][j], tmp);
3679 value_clear(tmp);
3680 if (M) {
3681 D = DomainImage(D, M, MaxRays);
3682 Matrix_Free(M);
3684 return D;
3687 gen_fun* barvinok_enumerate_union_series_with_options(Polyhedron *D, Polyhedron* C,
3688 barvinok_options *options)
3690 Polyhedron *conv, *D2;
3691 Polyhedron *CA;
3692 gen_fun *gf = NULL, *gf2;
3693 unsigned nparam = C->Dimension;
3694 ZZ one, mone;
3695 one = 1;
3696 mone = -1;
3698 CA = align_context(C, D->Dimension, options->MaxRays);
3699 D = DomainIntersection(D, CA, options->MaxRays);
3700 Polyhedron_Free(CA);
3702 D2 = skew_into_positive_orthant(D, nparam, options->MaxRays);
3703 for (Polyhedron *P = D2; P; P = P->next) {
3704 assert(P->Dimension == D2->Dimension);
3705 gen_fun *P_gf;
3707 P_gf = series(Polyhedron_Copy(P), nparam, options);
3708 if (!gf)
3709 gf = P_gf;
3710 else {
3711 gf->add_union(P_gf, options);
3712 delete P_gf;
3715 /* we actually only need the convex union of the parameter space
3716 * but the reducer classes currently expect a polyhedron in
3717 * the combined space
3719 Polyhedron_Free(gf->context);
3720 gf->context = DomainConvex(D2, options->MaxRays);
3722 gf2 = gf->summate(D2->Dimension - nparam, options);
3724 delete gf;
3725 if (D != D2)
3726 Domain_Free(D2);
3727 Domain_Free(D);
3728 return gf2;
3731 gen_fun* barvinok_enumerate_union_series(Polyhedron *D, Polyhedron* C,
3732 unsigned MaxRays)
3734 gen_fun *gf;
3735 barvinok_options *options = barvinok_options_new_with_defaults();
3736 options->MaxRays = MaxRays;
3737 gf = barvinok_enumerate_union_series_with_options(D, C, options);
3738 barvinok_options_free(options);
3739 return gf;
3742 evalue* barvinok_enumerate_union(Polyhedron *D, Polyhedron* C, unsigned MaxRays)
3744 evalue *EP;
3745 gen_fun *gf = barvinok_enumerate_union_series(D, C, MaxRays);
3746 EP = *gf;
3747 delete gf;
3748 return EP;