test: simple test for PolyLib's Smith
[barvinok.git] / barvinok.cc
blobae0db49cd579afe215c38dde46e137bfd0f10944
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 #ifdef NTL_STD_CXX
28 using namespace NTL;
29 #endif
30 using std::cerr;
31 using std::cout;
32 using std::endl;
33 using std::vector;
34 using std::deque;
35 using std::string;
36 using std::ostringstream;
38 #define ALLOC(t,p) p = (t*)malloc(sizeof(*p))
40 class dpoly_n {
41 public:
42 Matrix *coeff;
43 ~dpoly_n() {
44 Matrix_Free(coeff);
46 dpoly_n(int d, ZZ& degree_0, ZZ& degree_1, int offset = 0) {
47 Value d0, d1;
48 value_init(d0);
49 value_init(d1);
50 zz2value(degree_0, d0);
51 zz2value(degree_1, d1);
52 coeff = Matrix_Alloc(d+1, d+1+1);
53 value_set_si(coeff->p[0][0], 1);
54 value_set_si(coeff->p[0][d+1], 1);
55 for (int i = 1; i <= d; ++i) {
56 value_multiply(coeff->p[i][0], coeff->p[i-1][0], d0);
57 Vector_Combine(coeff->p[i-1], coeff->p[i-1]+1, coeff->p[i]+1,
58 d1, d0, i);
59 value_set_si(coeff->p[i][d+1], i);
60 value_multiply(coeff->p[i][d+1], coeff->p[i][d+1], coeff->p[i-1][d+1]);
61 value_decrement(d0, d0);
63 value_clear(d0);
64 value_clear(d1);
66 void div(dpoly& d, Vector *count, ZZ& sign) {
67 int len = coeff->NbRows;
68 Matrix * c = Matrix_Alloc(coeff->NbRows, coeff->NbColumns);
69 Value tmp;
70 value_init(tmp);
71 for (int i = 0; i < len; ++i) {
72 Vector_Copy(coeff->p[i], c->p[i], len+1);
73 for (int j = 1; j <= i; ++j) {
74 zz2value(d.coeff[j], tmp);
75 value_multiply(tmp, tmp, c->p[i][len]);
76 value_oppose(tmp, tmp);
77 Vector_Combine(c->p[i], c->p[i-j], c->p[i],
78 c->p[i-j][len], tmp, len);
79 value_multiply(c->p[i][len], c->p[i][len], c->p[i-j][len]);
81 zz2value(d.coeff[0], tmp);
82 value_multiply(c->p[i][len], c->p[i][len], tmp);
84 if (sign == -1) {
85 value_set_si(tmp, -1);
86 Vector_Scale(c->p[len-1], count->p, tmp, len);
87 value_assign(count->p[len], c->p[len-1][len]);
88 } else
89 Vector_Copy(c->p[len-1], count->p, len+1);
90 Vector_Normalize(count->p, len+1);
91 value_clear(tmp);
92 Matrix_Free(c);
96 const int MAX_TRY=10;
98 * Searches for a vector that is not orthogonal to any
99 * of the rays in rays.
101 static void nonorthog(mat_ZZ& rays, vec_ZZ& lambda)
103 int dim = rays.NumCols();
104 bool found = false;
105 lambda.SetLength(dim);
106 if (dim == 0)
107 return;
109 for (int i = 2; !found && i <= 50*dim; i+=4) {
110 for (int j = 0; j < MAX_TRY; ++j) {
111 for (int k = 0; k < dim; ++k) {
112 int r = random_int(i)+2;
113 int v = (2*(r%2)-1) * (r >> 1);
114 lambda[k] = v;
116 int k = 0;
117 for (; k < rays.NumRows(); ++k)
118 if (lambda * rays[k] == 0)
119 break;
120 if (k == rays.NumRows()) {
121 found = true;
122 break;
126 assert(found);
129 static void add_rays(mat_ZZ& rays, Polyhedron *i, int *r, int nvar = -1,
130 bool all = false)
132 unsigned dim = i->Dimension;
133 if (nvar == -1)
134 nvar = dim;
135 for (int k = 0; k < i->NbRays; ++k) {
136 if (!value_zero_p(i->Ray[k][dim+1]))
137 continue;
138 if (!all && nvar != dim && First_Non_Zero(i->Ray[k]+1, nvar) == -1)
139 continue;
140 values2zz(i->Ray[k]+1, rays[(*r)++], nvar);
144 static void mask_r(Matrix *f, int nr, Vector *lcm, int p, Vector *val, evalue *ev)
146 unsigned nparam = lcm->Size;
148 if (p == nparam) {
149 Vector * prod = Vector_Alloc(f->NbRows);
150 Matrix_Vector_Product(f, val->p, prod->p);
151 int isint = 1;
152 for (int i = 0; i < nr; ++i) {
153 value_modulus(prod->p[i], prod->p[i], f->p[i][nparam+1]);
154 isint &= value_zero_p(prod->p[i]);
156 value_set_si(ev->d, 1);
157 value_init(ev->x.n);
158 value_set_si(ev->x.n, isint);
159 Vector_Free(prod);
160 return;
163 Value tmp;
164 value_init(tmp);
165 if (value_one_p(lcm->p[p]))
166 mask_r(f, nr, lcm, p+1, val, ev);
167 else {
168 value_assign(tmp, lcm->p[p]);
169 value_set_si(ev->d, 0);
170 ev->x.p = new_enode(periodic, VALUE_TO_INT(tmp), p+1);
171 do {
172 value_decrement(tmp, tmp);
173 value_assign(val->p[p], tmp);
174 mask_r(f, nr, lcm, p+1, val, &ev->x.p->arr[VALUE_TO_INT(tmp)]);
175 } while (value_pos_p(tmp));
177 value_clear(tmp);
180 static void mask_fractional(Matrix *f, evalue *factor)
182 int nr = f->NbRows, nc = f->NbColumns;
183 int n;
184 bool found = false;
185 for (n = 0; n < nr && value_notzero_p(f->p[n][nc-1]); ++n)
186 if (value_notone_p(f->p[n][nc-1]) &&
187 value_notmone_p(f->p[n][nc-1]))
188 found = true;
189 if (!found)
190 return;
192 evalue EP;
193 nr = n;
195 Value m;
196 value_init(m);
198 evalue EV;
199 value_init(EV.d);
200 value_init(EV.x.n);
201 value_set_si(EV.x.n, 1);
203 for (n = 0; n < nr; ++n) {
204 value_assign(m, f->p[n][nc-1]);
205 if (value_one_p(m) || value_mone_p(m))
206 continue;
208 int j = normal_mod(f->p[n], nc-1, &m);
209 if (j == nc-1) {
210 free_evalue_refs(factor);
211 value_init(factor->d);
212 evalue_set_si(factor, 0, 1);
213 break;
215 vec_ZZ row;
216 values2zz(f->p[n], row, nc-1);
217 ZZ g;
218 value2zz(m, g);
219 if (j < (nc-1)-1 && row[j] > g/2) {
220 for (int k = j; k < (nc-1); ++k)
221 if (row[k] != 0)
222 row[k] = g - row[k];
225 value_init(EP.d);
226 value_set_si(EP.d, 0);
227 EP.x.p = new_enode(relation, 2, 0);
228 value_clear(EP.x.p->arr[1].d);
229 EP.x.p->arr[1] = *factor;
230 evalue *ev = &EP.x.p->arr[0];
231 value_set_si(ev->d, 0);
232 ev->x.p = new_enode(fractional, 3, -1);
233 evalue_set_si(&ev->x.p->arr[1], 0, 1);
234 evalue_set_si(&ev->x.p->arr[2], 1, 1);
235 evalue *E = multi_monom(row);
236 value_assign(EV.d, m);
237 emul(&EV, E);
238 value_clear(ev->x.p->arr[0].d);
239 ev->x.p->arr[0] = *E;
240 delete E;
241 *factor = EP;
244 value_clear(m);
245 free_evalue_refs(&EV);
251 static void mask_table(Matrix *f, evalue *factor)
253 int nr = f->NbRows, nc = f->NbColumns;
254 int n;
255 bool found = false;
256 for (n = 0; n < nr && value_notzero_p(f->p[n][nc-1]); ++n)
257 if (value_notone_p(f->p[n][nc-1]) &&
258 value_notmone_p(f->p[n][nc-1]))
259 found = true;
260 if (!found)
261 return;
263 Value tmp;
264 value_init(tmp);
265 nr = n;
266 unsigned np = nc - 2;
267 Vector *lcm = Vector_Alloc(np);
268 Vector *val = Vector_Alloc(nc);
269 Vector_Set(val->p, 0, nc);
270 value_set_si(val->p[np], 1);
271 Vector_Set(lcm->p, 1, np);
272 for (n = 0; n < nr; ++n) {
273 if (value_one_p(f->p[n][nc-1]) ||
274 value_mone_p(f->p[n][nc-1]))
275 continue;
276 for (int j = 0; j < np; ++j)
277 if (value_notzero_p(f->p[n][j])) {
278 Gcd(f->p[n][j], f->p[n][nc-1], &tmp);
279 value_division(tmp, f->p[n][nc-1], tmp);
280 value_lcm(tmp, lcm->p[j], &lcm->p[j]);
283 evalue EP;
284 value_init(EP.d);
285 mask_r(f, nr, lcm, 0, val, &EP);
286 value_clear(tmp);
287 Vector_Free(val);
288 Vector_Free(lcm);
289 emul(&EP,factor);
290 free_evalue_refs(&EP);
293 static void mask(Matrix *f, evalue *factor, barvinok_options *options)
295 if (options->lookup_table)
296 mask_table(f, factor);
297 else
298 mask_fractional(f, factor);
301 /* This structure encodes the power of the term in a rational generating function.
303 * Either E == NULL or constant = 0
304 * If E != NULL, then the power is E
305 * If E == NULL, then the power is coeff * param[pos] + constant
307 struct term_info {
308 evalue *E;
309 ZZ constant;
310 ZZ coeff;
311 int pos;
314 /* Returns the power of (t+1) in the term of a rational generating function,
315 * i.e., the scalar product of the actual lattice point and lambda.
316 * The lattice point is the unique lattice point in the fundamental parallelepiped
317 * of the unimodual cone i shifted to the parametric vertex V.
319 * PD is the parameter domain, which, if != NULL, may be used to simply the
320 * resulting expression.
322 * The result is returned in term.
324 void lattice_point(Param_Vertices* V, const mat_ZZ& rays, vec_ZZ& lambda,
325 term_info* term, Polyhedron *PD, barvinok_options *options)
327 unsigned nparam = V->Vertex->NbColumns - 2;
328 unsigned dim = rays.NumCols();
329 mat_ZZ vertex;
330 vertex.SetDims(V->Vertex->NbRows, nparam+1);
331 Value lcm, tmp;
332 value_init(lcm);
333 value_init(tmp);
334 value_set_si(lcm, 1);
335 for (int j = 0; j < V->Vertex->NbRows; ++j) {
336 value_lcm(lcm, V->Vertex->p[j][nparam+1], &lcm);
338 if (value_notone_p(lcm)) {
339 Matrix * mv = Matrix_Alloc(dim, nparam+1);
340 for (int j = 0 ; j < dim; ++j) {
341 value_division(tmp, lcm, V->Vertex->p[j][nparam+1]);
342 Vector_Scale(V->Vertex->p[j], mv->p[j], tmp, nparam+1);
345 term->E = lattice_point(rays, lambda, mv, lcm, PD, options);
346 term->constant = 0;
348 Matrix_Free(mv);
349 value_clear(lcm);
350 value_clear(tmp);
351 return;
353 for (int i = 0; i < V->Vertex->NbRows; ++i) {
354 assert(value_one_p(V->Vertex->p[i][nparam+1])); // for now
355 values2zz(V->Vertex->p[i], vertex[i], nparam+1);
358 vec_ZZ num;
359 num = lambda * vertex;
361 int p = -1;
362 int nn = 0;
363 for (int j = 0; j < nparam; ++j)
364 if (num[j] != 0) {
365 ++nn;
366 p = j;
368 if (nn >= 2) {
369 term->E = multi_monom(num);
370 term->constant = 0;
371 } else {
372 term->E = NULL;
373 term->constant = num[nparam];
374 term->pos = p;
375 if (p != -1)
376 term->coeff = num[p];
379 value_clear(lcm);
380 value_clear(tmp);
384 struct counter : public np_base {
385 vec_ZZ lambda;
386 mat_ZZ vertex;
387 vec_ZZ den;
388 ZZ sign;
389 vec_ZZ num;
390 ZZ offset;
391 int j;
392 mpq_t count;
394 counter(unsigned dim) : np_base(dim) {
395 den.SetLength(dim);
396 mpq_init(count);
399 virtual void init(Polyhedron *P) {
400 randomvector(P, lambda, dim);
403 virtual void reset() {
404 mpq_set_si(count, 0, 0);
407 ~counter() {
408 mpq_clear(count);
411 virtual void handle(const mat_ZZ& rays, Value *vertex, const QQ& c,
412 unsigned long det, int *closed, barvinok_options *options);
413 virtual void get_count(Value *result) {
414 assert(value_one_p(&count[0]._mp_den));
415 value_assign(*result, &count[0]._mp_num);
419 void counter::handle(const mat_ZZ& rays, Value *V, const QQ& c, unsigned long det,
420 int *closed, barvinok_options *options)
422 for (int k = 0; k < dim; ++k) {
423 if (lambda * rays[k] == 0)
424 throw Orthogonal;
427 assert(c.d == 1);
428 assert(c.n == 1 || c.n == -1);
429 sign = c.n;
431 lattice_point(V, rays, vertex, det, closed);
432 num = vertex * lambda;
433 den = rays * lambda;
434 offset = 0;
435 normalize(sign, offset, den);
437 num[0] += offset;
438 dpoly d(dim, num[0]);
439 for (int k = 1; k < num.length(); ++k) {
440 num[k] += offset;
441 dpoly term(dim, num[k]);
442 d += term;
444 dpoly n(dim, den[0], 1);
445 for (int k = 1; k < dim; ++k) {
446 dpoly fact(dim, den[k], 1);
447 n *= fact;
449 d.div(n, count, sign);
452 struct bfe_term : public bfc_term_base {
453 vector<evalue *> factors;
455 bfe_term(int len) : bfc_term_base(len) {
458 ~bfe_term() {
459 for (int i = 0; i < factors.size(); ++i) {
460 if (!factors[i])
461 continue;
462 free_evalue_refs(factors[i]);
463 delete factors[i];
468 static void print_int_vector(int *v, int len, char *name)
470 cerr << name << endl;
471 for (int j = 0; j < len; ++j) {
472 cerr << v[j] << " ";
474 cerr << endl;
477 static void print_bfc_terms(mat_ZZ& factors, bfc_vec& v)
479 cerr << endl;
480 cerr << "factors" << endl;
481 cerr << factors << endl;
482 for (int i = 0; i < v.size(); ++i) {
483 cerr << "term: " << i << endl;
484 print_int_vector(v[i]->powers, factors.NumRows(), "powers");
485 cerr << "terms" << endl;
486 cerr << v[i]->terms << endl;
487 bfc_term* bfct = static_cast<bfc_term *>(v[i]);
488 cerr << bfct->c << endl;
492 static void print_bfe_terms(mat_ZZ& factors, bfc_vec& v)
494 cerr << endl;
495 cerr << "factors" << endl;
496 cerr << factors << endl;
497 for (int i = 0; i < v.size(); ++i) {
498 cerr << "term: " << i << endl;
499 print_int_vector(v[i]->powers, factors.NumRows(), "powers");
500 cerr << "terms" << endl;
501 cerr << v[i]->terms << endl;
502 bfe_term* bfet = static_cast<bfe_term *>(v[i]);
503 for (int j = 0; j < v[i]->terms.NumRows(); ++j) {
504 char * test[] = {"a", "b"};
505 print_evalue(stderr, bfet->factors[j], test);
506 fprintf(stderr, "\n");
511 struct bfcounter : public bfcounter_base {
512 mpq_t count;
514 bfcounter(unsigned dim) : bfcounter_base(dim) {
515 mpq_init(count);
516 lower = 1;
518 ~bfcounter() {
519 mpq_clear(count);
521 virtual void base(mat_ZZ& factors, bfc_vec& v);
522 virtual void get_count(Value *result) {
523 assert(value_one_p(&count[0]._mp_den));
524 value_assign(*result, &count[0]._mp_num);
528 void bfcounter::base(mat_ZZ& factors, bfc_vec& v)
530 unsigned nf = factors.NumRows();
532 for (int i = 0; i < v.size(); ++i) {
533 bfc_term* bfct = static_cast<bfc_term *>(v[i]);
534 int total_power = 0;
535 // factor is always positive, so we always
536 // change signs
537 for (int k = 0; k < nf; ++k)
538 total_power += v[i]->powers[k];
540 int j;
541 for (j = 0; j < nf; ++j)
542 if (v[i]->powers[j] > 0)
543 break;
545 dpoly D(total_power, factors[j][0], 1);
546 for (int k = 1; k < v[i]->powers[j]; ++k) {
547 dpoly fact(total_power, factors[j][0], 1);
548 D *= fact;
550 for ( ; ++j < nf; )
551 for (int k = 0; k < v[i]->powers[j]; ++k) {
552 dpoly fact(total_power, factors[j][0], 1);
553 D *= fact;
556 for (int k = 0; k < v[i]->terms.NumRows(); ++k) {
557 dpoly n(total_power, v[i]->terms[k][0]);
558 mpq_set_si(tcount, 0, 1);
559 n.div(D, tcount, one);
560 if (total_power % 2)
561 bfct->c[k].n = -bfct->c[k].n;
562 zz2value(bfct->c[k].n, tn);
563 zz2value(bfct->c[k].d, td);
565 mpz_mul(mpq_numref(tcount), mpq_numref(tcount), tn);
566 mpz_mul(mpq_denref(tcount), mpq_denref(tcount), td);
567 mpq_canonicalize(tcount);
568 mpq_add(count, count, tcount);
570 delete v[i];
575 /* Check whether the polyhedron is unbounded and if so,
576 * check whether it has any (and therefore an infinite number of)
577 * integer points.
578 * If one of the vertices is integer, then we are done.
579 * Otherwise, transform the polyhedron such that one of the rays
580 * is the first unit vector and cut it off at a height that ensures
581 * that if the whole polyhedron has any points, then the remaining part
582 * has integer points. In particular we add the largest coefficient
583 * of a ray to the highest vertex (rounded up).
585 static bool Polyhedron_is_infinite(Polyhedron *P, Value* result,
586 barvinok_options *options)
588 int r = 0;
589 Matrix *M, *M2;
590 Value c, tmp;
591 Value g;
592 bool first;
593 Vector *v;
594 Value offset, size;
595 Polyhedron *R;
597 if (P->NbBid == 0)
598 for (; r < P->NbRays; ++r)
599 if (value_zero_p(P->Ray[r][P->Dimension+1]))
600 break;
601 if (P->NbBid == 0 && r == P->NbRays)
602 return false;
604 if (options->count_sample_infinite) {
605 Vector *sample;
607 sample = Polyhedron_Sample(P, options);
608 if (!sample)
609 value_set_si(*result, 0);
610 else {
611 value_set_si(*result, -1);
612 Vector_Free(sample);
614 return true;
617 for (int i = 0; i < P->NbRays; ++i)
618 if (value_one_p(P->Ray[i][1+P->Dimension])) {
619 value_set_si(*result, -1);
620 return true;
623 value_init(g);
624 v = Vector_Alloc(P->Dimension+1);
625 Vector_Gcd(P->Ray[r]+1, P->Dimension, &g);
626 Vector_AntiScale(P->Ray[r]+1, v->p, g, P->Dimension+1);
627 M = unimodular_complete(v);
628 value_set_si(M->p[P->Dimension][P->Dimension], 1);
629 M2 = Transpose(M);
630 Matrix_Free(M);
631 P = Polyhedron_Preimage(P, M2, 0);
632 Matrix_Free(M2);
633 value_clear(g);
634 Vector_Free(v);
636 first = true;
637 value_init(offset);
638 value_init(size);
639 value_init(tmp);
640 value_set_si(size, 0);
642 for (int i = 0; i < P->NbBid; ++i) {
643 value_absolute(tmp, P->Ray[i][1]);
644 if (value_gt(tmp, size))
645 value_assign(size, tmp);
647 for (int i = P->NbBid; i < P->NbRays; ++i) {
648 if (value_zero_p(P->Ray[i][P->Dimension+1])) {
649 if (value_gt(P->Ray[i][1], size))
650 value_assign(size, P->Ray[i][1]);
651 continue;
653 mpz_cdiv_q(tmp, P->Ray[i][1], P->Ray[i][P->Dimension+1]);
654 if (first || value_gt(tmp, offset)) {
655 value_assign(offset, tmp);
656 first = false;
659 value_addto(offset, offset, size);
660 value_clear(size);
661 value_clear(tmp);
663 v = Vector_Alloc(P->Dimension+2);
664 value_set_si(v->p[0], 1);
665 value_set_si(v->p[1], -1);
666 value_assign(v->p[1+P->Dimension], offset);
667 R = AddConstraints(v->p, 1, P, options->MaxRays);
668 Polyhedron_Free(P);
669 P = R;
671 value_clear(offset);
672 Vector_Free(v);
674 value_init(c);
675 barvinok_count_with_options(P, &c, options);
676 Polyhedron_Free(P);
677 if (value_zero_p(c))
678 value_set_si(*result, 0);
679 else
680 value_set_si(*result, -1);
681 value_clear(c);
683 return true;
686 typedef Polyhedron * Polyhedron_p;
688 static void barvinok_count_f(Polyhedron *P, Value* result,
689 barvinok_options *options);
691 void barvinok_count_with_options(Polyhedron *P, Value* result,
692 struct barvinok_options *options)
694 unsigned dim;
695 int allocated = 0;
696 Polyhedron *Q;
697 bool infinite = false;
699 if (emptyQ2(P)) {
700 value_set_si(*result, 0);
701 return;
703 if (P->NbEq != 0) {
704 Q = NULL;
705 do {
706 P = remove_equalities(P);
707 P = DomainConstraintSimplify(P, options->MaxRays);
708 if (Q)
709 Polyhedron_Free(Q);
710 Q = P;
711 } while (!emptyQ(P) && P->NbEq != 0);
712 if (emptyQ(P)) {
713 Polyhedron_Free(P);
714 value_set_si(*result, 0);
715 return;
717 allocated = 1;
719 if (Polyhedron_is_infinite(P, result, options)) {
720 if (allocated)
721 Polyhedron_Free(P);
722 return;
724 if (P->Dimension == 0) {
725 /* Test whether the constraints are satisfied */
726 POL_ENSURE_VERTICES(P);
727 value_set_si(*result, !emptyQ(P));
728 if (allocated)
729 Polyhedron_Free(P);
730 return;
732 Q = Polyhedron_Factor(P, 0, options->MaxRays);
733 if (Q) {
734 if (allocated)
735 Polyhedron_Free(P);
736 P = Q;
737 allocated = 1;
740 barvinok_count_f(P, result, options);
741 if (value_neg_p(*result))
742 infinite = true;
743 if (Q && P->next && value_notzero_p(*result)) {
744 Value factor;
745 value_init(factor);
747 for (Q = P->next; Q; Q = Q->next) {
748 barvinok_count_f(Q, &factor, options);
749 if (value_neg_p(factor)) {
750 infinite = true;
751 continue;
752 } else if (Q->next && value_zero_p(factor)) {
753 value_set_si(*result, 0);
754 break;
756 value_multiply(*result, *result, factor);
759 value_clear(factor);
762 if (allocated)
763 Domain_Free(P);
764 if (infinite)
765 value_set_si(*result, -1);
768 void barvinok_count(Polyhedron *P, Value* result, unsigned NbMaxCons)
770 barvinok_options *options = barvinok_options_new_with_defaults();
771 options->MaxRays = NbMaxCons;
772 barvinok_count_with_options(P, result, options);
773 barvinok_options_free(options);
776 static void barvinok_count_f(Polyhedron *P, Value* result,
777 barvinok_options *options)
779 if (emptyQ2(P)) {
780 value_set_si(*result, 0);
781 return;
784 if (P->Dimension == 1)
785 return Line_Length(P, result);
787 int c = P->NbConstraints;
788 POL_ENSURE_FACETS(P);
789 if (c != P->NbConstraints || P->NbEq != 0)
790 return barvinok_count_with_options(P, result, options);
792 POL_ENSURE_VERTICES(P);
794 if (Polyhedron_is_infinite(P, result, options))
795 return;
797 np_base *cnt;
798 if (options->incremental_specialization == 2)
799 cnt = new bfcounter(P->Dimension);
800 else if (options->incremental_specialization == 1)
801 cnt = new icounter(P->Dimension);
802 else
803 cnt = new counter(P->Dimension);
804 cnt->start(P, options);
806 cnt->get_count(result);
807 delete cnt;
810 static void uni_polynom(int param, Vector *c, evalue *EP)
812 unsigned dim = c->Size-2;
813 value_init(EP->d);
814 value_set_si(EP->d,0);
815 EP->x.p = new_enode(polynomial, dim+1, param+1);
816 for (int j = 0; j <= dim; ++j)
817 evalue_set(&EP->x.p->arr[j], c->p[j], c->p[dim+1]);
820 static void multi_polynom(Vector *c, evalue* X, evalue *EP)
822 unsigned dim = c->Size-2;
823 evalue EC;
825 value_init(EC.d);
826 evalue_set(&EC, c->p[dim], c->p[dim+1]);
828 value_init(EP->d);
829 evalue_set(EP, c->p[dim], c->p[dim+1]);
831 for (int i = dim-1; i >= 0; --i) {
832 emul(X, EP);
833 value_assign(EC.x.n, c->p[i]);
834 eadd(&EC, EP);
836 free_evalue_refs(&EC);
839 Polyhedron *unfringe (Polyhedron *P, unsigned MaxRays)
841 int len = P->Dimension+2;
842 Polyhedron *T, *R = P;
843 Value g;
844 value_init(g);
845 Vector *row = Vector_Alloc(len);
846 value_set_si(row->p[0], 1);
848 R = DomainConstraintSimplify(Polyhedron_Copy(P), MaxRays);
850 Matrix *M = Matrix_Alloc(2, len-1);
851 value_set_si(M->p[1][len-2], 1);
852 for (int v = 0; v < P->Dimension; ++v) {
853 value_set_si(M->p[0][v], 1);
854 Polyhedron *I = Polyhedron_Image(R, M, 2+1);
855 value_set_si(M->p[0][v], 0);
856 for (int r = 0; r < I->NbConstraints; ++r) {
857 if (value_zero_p(I->Constraint[r][0]))
858 continue;
859 if (value_zero_p(I->Constraint[r][1]))
860 continue;
861 if (value_one_p(I->Constraint[r][1]))
862 continue;
863 if (value_mone_p(I->Constraint[r][1]))
864 continue;
865 value_absolute(g, I->Constraint[r][1]);
866 Vector_Set(row->p+1, 0, len-2);
867 value_division(row->p[1+v], I->Constraint[r][1], g);
868 mpz_fdiv_q(row->p[len-1], I->Constraint[r][2], g);
869 T = R;
870 R = AddConstraints(row->p, 1, R, MaxRays);
871 if (T != P)
872 Polyhedron_Free(T);
874 Polyhedron_Free(I);
876 Matrix_Free(M);
877 Vector_Free(row);
878 value_clear(g);
879 return R;
882 /* this procedure may have false negatives */
883 static bool Polyhedron_is_infinite_param(Polyhedron *P, unsigned nparam)
885 int r;
886 for (r = 0; r < P->NbRays; ++r) {
887 if (!value_zero_p(P->Ray[r][0]) &&
888 !value_zero_p(P->Ray[r][P->Dimension+1]))
889 continue;
890 if (First_Non_Zero(P->Ray[r]+1+P->Dimension-nparam, nparam) == -1)
891 return true;
893 return false;
896 /* Check whether all rays point in the positive directions
897 * for the parameters
899 static bool Polyhedron_has_positive_rays(Polyhedron *P, unsigned nparam)
901 int r;
902 for (r = 0; r < P->NbRays; ++r)
903 if (value_zero_p(P->Ray[r][P->Dimension+1])) {
904 int i;
905 for (i = P->Dimension - nparam; i < P->Dimension; ++i)
906 if (value_neg_p(P->Ray[r][i+1]))
907 return false;
909 return true;
912 typedef evalue * evalue_p;
914 struct enumerator_base {
915 unsigned dim;
916 evalue ** vE;
917 evalue mone;
918 vertex_decomposer *vpd;
920 enumerator_base(unsigned dim, vertex_decomposer *vpd)
922 this->dim = dim;
923 this->vpd = vpd;
925 vE = new evalue_p[vpd->nbV];
926 for (int j = 0; j < vpd->nbV; ++j)
927 vE[j] = 0;
929 value_init(mone.d);
930 evalue_set_si(&mone, -1, 1);
933 void decompose_at(Param_Vertices *V, int _i, barvinok_options *options) {
934 //this->pVD = pVD;
936 vE[_i] = new evalue;
937 value_init(vE[_i]->d);
938 evalue_set_si(vE[_i], 0, 1);
940 vpd->decompose_at_vertex(V, _i, options);
943 virtual ~enumerator_base() {
944 for (int j = 0; j < vpd->nbV; ++j)
945 if (vE[j]) {
946 free_evalue_refs(vE[j]);
947 delete vE[j];
949 delete [] vE;
951 free_evalue_refs(&mone);
954 static enumerator_base *create(Polyhedron *P, unsigned dim, unsigned nbV,
955 barvinok_options *options);
958 struct enumerator : public signed_cone_consumer, public vertex_decomposer,
959 public enumerator_base {
960 vec_ZZ lambda;
961 vec_ZZ den;
962 ZZ sign;
963 term_info num;
964 Vector *c;
965 mpq_t count;
967 enumerator(Polyhedron *P, unsigned dim, unsigned nbV) :
968 vertex_decomposer(P, nbV, *this), enumerator_base(dim, this) {
969 this->P = P;
970 this->nbV = nbV;
971 randomvector(P, lambda, dim);
972 den.SetLength(dim);
973 c = Vector_Alloc(dim+2);
975 mpq_init(count);
978 ~enumerator() {
979 mpq_clear(count);
980 Vector_Free(c);
983 virtual void handle(const signed_cone& sc, barvinok_options *options);
986 void enumerator::handle(const signed_cone& sc, barvinok_options *options)
988 assert(sc.det == 1);
989 assert(!sc.closed);
990 int r = 0;
991 assert(sc.rays.NumRows() == dim);
992 for (int k = 0; k < dim; ++k) {
993 if (lambda * sc.rays[k] == 0)
994 throw Orthogonal;
997 sign = sc.sign;
999 lattice_point(V, sc.rays, lambda, &num, 0, options);
1000 den = sc.rays * lambda;
1001 normalize(sign, num.constant, den);
1003 dpoly n(dim, den[0], 1);
1004 for (int k = 1; k < dim; ++k) {
1005 dpoly fact(dim, den[k], 1);
1006 n *= fact;
1008 if (num.E != NULL) {
1009 ZZ one(INIT_VAL, 1);
1010 dpoly_n d(dim, num.constant, one);
1011 d.div(n, c, sign);
1012 evalue EV;
1013 multi_polynom(c, num.E, &EV);
1014 eadd(&EV , vE[vert]);
1015 free_evalue_refs(&EV);
1016 free_evalue_refs(num.E);
1017 delete num.E;
1018 } else if (num.pos != -1) {
1019 dpoly_n d(dim, num.constant, num.coeff);
1020 d.div(n, c, sign);
1021 evalue EV;
1022 uni_polynom(num.pos, c, &EV);
1023 eadd(&EV , vE[vert]);
1024 free_evalue_refs(&EV);
1025 } else {
1026 mpq_set_si(count, 0, 1);
1027 dpoly d(dim, num.constant);
1028 d.div(n, count, sign);
1029 evalue EV;
1030 value_init(EV.d);
1031 evalue_set(&EV, &count[0]._mp_num, &count[0]._mp_den);
1032 eadd(&EV , vE[vert]);
1033 free_evalue_refs(&EV);
1037 struct ienumerator_base : enumerator_base {
1038 evalue ** E_vertex;
1040 ienumerator_base(unsigned dim, vertex_decomposer *vpd) :
1041 enumerator_base(dim,vpd) {
1042 E_vertex = new evalue_p[dim];
1045 virtual ~ienumerator_base() {
1046 delete [] E_vertex;
1049 evalue *E_num(int i, int d) {
1050 return E_vertex[i + (dim-d)];
1054 struct cumulator {
1055 evalue *factor;
1056 evalue *v;
1057 dpoly_r *r;
1059 cumulator(evalue *factor, evalue *v, dpoly_r *r) :
1060 factor(factor), v(v), r(r) {}
1062 void cumulate(barvinok_options *options);
1064 virtual void add_term(const vector<int>& powers, evalue *f2) = 0;
1067 void cumulator::cumulate(barvinok_options *options)
1069 evalue cum; // factor * 1 * E_num[0]/1 * (E_num[0]-1)/2 *...
1070 evalue f;
1071 evalue t; // E_num[0] - (m-1)
1072 evalue *cst;
1073 evalue mone;
1075 if (options->lookup_table) {
1076 value_init(mone.d);
1077 evalue_set_si(&mone, -1, 1);
1080 value_init(cum.d);
1081 evalue_copy(&cum, factor);
1082 value_init(f.d);
1083 value_init(f.x.n);
1084 value_set_si(f.d, 1);
1085 value_set_si(f.x.n, 1);
1086 value_init(t.d);
1087 evalue_copy(&t, v);
1089 if (!options->lookup_table) {
1090 for (cst = &t; value_zero_p(cst->d); ) {
1091 if (cst->x.p->type == fractional)
1092 cst = &cst->x.p->arr[1];
1093 else
1094 cst = &cst->x.p->arr[0];
1098 for (int m = 0; m < r->len; ++m) {
1099 if (m > 0) {
1100 if (m > 1) {
1101 value_set_si(f.d, m);
1102 emul(&f, &cum);
1103 if (!options->lookup_table)
1104 value_subtract(cst->x.n, cst->x.n, cst->d);
1105 else
1106 eadd(&mone, &t);
1108 emul(&t, &cum);
1110 dpoly_r_term_list& current = r->c[r->len-1-m];
1111 dpoly_r_term_list::iterator j;
1112 for (j = current.begin(); j != current.end(); ++j) {
1113 if ((*j)->coeff == 0)
1114 continue;
1115 evalue *f2 = new evalue;
1116 value_init(f2->d);
1117 value_init(f2->x.n);
1118 zz2value((*j)->coeff, f2->x.n);
1119 zz2value(r->denom, f2->d);
1120 emul(&cum, f2);
1122 add_term((*j)->powers, f2);
1125 free_evalue_refs(&f);
1126 free_evalue_refs(&t);
1127 free_evalue_refs(&cum);
1128 if (options->lookup_table)
1129 free_evalue_refs(&mone);
1132 struct E_poly_term {
1133 vector<int> powers;
1134 evalue *E;
1137 struct ie_cum : public cumulator {
1138 vector<E_poly_term *> terms;
1140 ie_cum(evalue *factor, evalue *v, dpoly_r *r) : cumulator(factor, v, r) {}
1142 virtual void add_term(const vector<int>& powers, evalue *f2);
1145 void ie_cum::add_term(const vector<int>& powers, evalue *f2)
1147 int k;
1148 for (k = 0; k < terms.size(); ++k) {
1149 if (terms[k]->powers == powers) {
1150 eadd(f2, terms[k]->E);
1151 free_evalue_refs(f2);
1152 delete f2;
1153 break;
1156 if (k >= terms.size()) {
1157 E_poly_term *ET = new E_poly_term;
1158 ET->powers = powers;
1159 ET->E = f2;
1160 terms.push_back(ET);
1164 struct ienumerator : public signed_cone_consumer, public vertex_decomposer,
1165 public ienumerator_base {
1166 //Polyhedron *pVD;
1167 mat_ZZ den;
1168 mat_ZZ vertex;
1169 mpq_t tcount;
1171 ienumerator(Polyhedron *P, unsigned dim, unsigned nbV) :
1172 vertex_decomposer(P, nbV, *this), ienumerator_base(dim, this) {
1173 vertex.SetDims(1, dim);
1175 den.SetDims(dim, dim);
1176 mpq_init(tcount);
1179 ~ienumerator() {
1180 mpq_clear(tcount);
1183 virtual void handle(const signed_cone& sc, barvinok_options *options);
1184 void reduce(evalue *factor, const mat_ZZ& num, const mat_ZZ& den_f,
1185 barvinok_options *options);
1188 void ienumerator::reduce(evalue *factor, const mat_ZZ& num, const mat_ZZ& den_f,
1189 barvinok_options *options)
1191 unsigned len = den_f.NumRows(); // number of factors in den
1192 unsigned dim = num.NumCols();
1193 assert(num.NumRows() == 1);
1195 if (dim == 0) {
1196 eadd(factor, vE[vert]);
1197 return;
1200 vec_ZZ den_s;
1201 mat_ZZ den_r;
1202 vec_ZZ num_s;
1203 mat_ZZ num_p;
1205 split_one(num, num_s, num_p, den_f, den_s, den_r);
1207 vec_ZZ den_p;
1208 den_p.SetLength(len);
1210 ZZ one;
1211 one = 1;
1212 normalize(one, num_s, num_p, den_s, den_p, den_r);
1213 if (one != 1)
1214 emul(&mone, factor);
1216 int only_param = 0;
1217 int no_param = 0;
1218 for (int k = 0; k < len; ++k) {
1219 if (den_p[k] == 0)
1220 ++no_param;
1221 else if (den_s[k] == 0)
1222 ++only_param;
1224 if (no_param == 0) {
1225 reduce(factor, num_p, den_r, options);
1226 } else {
1227 int k, l;
1228 mat_ZZ pden;
1229 pden.SetDims(only_param, dim-1);
1231 for (k = 0, l = 0; k < len; ++k)
1232 if (den_s[k] == 0)
1233 pden[l++] = den_r[k];
1235 for (k = 0; k < len; ++k)
1236 if (den_p[k] == 0)
1237 break;
1239 dpoly n(no_param, num_s[0]);
1240 dpoly D(no_param, den_s[k], 1);
1241 for ( ; ++k < len; )
1242 if (den_p[k] == 0) {
1243 dpoly fact(no_param, den_s[k], 1);
1244 D *= fact;
1247 dpoly_r * r = 0;
1248 // if no_param + only_param == len then all powers
1249 // below will be all zero
1250 if (no_param + only_param == len) {
1251 if (E_num(0, dim) != 0)
1252 r = new dpoly_r(n, len);
1253 else {
1254 mpq_set_si(tcount, 0, 1);
1255 one = 1;
1256 n.div(D, tcount, one);
1258 if (value_notzero_p(mpq_numref(tcount))) {
1259 evalue f;
1260 value_init(f.d);
1261 value_init(f.x.n);
1262 value_assign(f.x.n, mpq_numref(tcount));
1263 value_assign(f.d, mpq_denref(tcount));
1264 emul(&f, factor);
1265 reduce(factor, num_p, pden, options);
1266 free_evalue_refs(&f);
1268 return;
1270 } else {
1271 for (k = 0; k < len; ++k) {
1272 if (den_s[k] == 0 || den_p[k] == 0)
1273 continue;
1275 dpoly pd(no_param-1, den_s[k], 1);
1277 int l;
1278 for (l = 0; l < k; ++l)
1279 if (den_r[l] == den_r[k])
1280 break;
1282 if (r == 0)
1283 r = new dpoly_r(n, pd, l, len);
1284 else {
1285 dpoly_r *nr = new dpoly_r(r, pd, l, len);
1286 delete r;
1287 r = nr;
1291 dpoly_r *rc = r->div(D);
1292 delete r;
1293 r = rc;
1294 if (E_num(0, dim) == 0) {
1295 int common = pden.NumRows();
1296 dpoly_r_term_list& final = r->c[r->len-1];
1297 int rows;
1298 evalue t;
1299 evalue f;
1300 value_init(f.d);
1301 value_init(f.x.n);
1302 zz2value(r->denom, f.d);
1303 dpoly_r_term_list::iterator j;
1304 for (j = final.begin(); j != final.end(); ++j) {
1305 if ((*j)->coeff == 0)
1306 continue;
1307 rows = common;
1308 for (int k = 0; k < r->dim; ++k) {
1309 int n = (*j)->powers[k];
1310 if (n == 0)
1311 continue;
1312 pden.SetDims(rows+n, pden.NumCols());
1313 for (int l = 0; l < n; ++l)
1314 pden[rows+l] = den_r[k];
1315 rows += n;
1317 value_init(t.d);
1318 evalue_copy(&t, factor);
1319 zz2value((*j)->coeff, f.x.n);
1320 emul(&f, &t);
1321 reduce(&t, num_p, pden, options);
1322 free_evalue_refs(&t);
1324 free_evalue_refs(&f);
1325 } else {
1326 ie_cum cum(factor, E_num(0, dim), r);
1327 cum.cumulate(options);
1329 int common = pden.NumRows();
1330 int rows;
1331 for (int j = 0; j < cum.terms.size(); ++j) {
1332 rows = common;
1333 pden.SetDims(rows, pden.NumCols());
1334 for (int k = 0; k < r->dim; ++k) {
1335 int n = cum.terms[j]->powers[k];
1336 if (n == 0)
1337 continue;
1338 pden.SetDims(rows+n, pden.NumCols());
1339 for (int l = 0; l < n; ++l)
1340 pden[rows+l] = den_r[k];
1341 rows += n;
1343 reduce(cum.terms[j]->E, num_p, pden, options);
1344 free_evalue_refs(cum.terms[j]->E);
1345 delete cum.terms[j]->E;
1346 delete cum.terms[j];
1349 delete r;
1353 static int type_offset(enode *p)
1355 return p->type == fractional ? 1 :
1356 p->type == flooring ? 1 : 0;
1359 static int edegree(evalue *e)
1361 int d = 0;
1362 enode *p;
1364 if (value_notzero_p(e->d))
1365 return 0;
1367 p = e->x.p;
1368 int i = type_offset(p);
1369 if (p->size-i-1 > d)
1370 d = p->size - i - 1;
1371 for (; i < p->size; i++) {
1372 int d2 = edegree(&p->arr[i]);
1373 if (d2 > d)
1374 d = d2;
1376 return d;
1379 void ienumerator::handle(const signed_cone& sc, barvinok_options *options)
1381 assert(sc.det == 1);
1382 assert(!sc.closed);
1383 assert(sc.rays.NumRows() == dim);
1385 lattice_point(V, sc.rays, vertex[0], E_vertex, options);
1387 den = sc.rays;
1389 evalue one;
1390 value_init(one.d);
1391 evalue_set_si(&one, sc.sign, 1);
1392 reduce(&one, vertex, den, options);
1393 free_evalue_refs(&one);
1395 for (int i = 0; i < dim; ++i)
1396 if (E_vertex[i]) {
1397 free_evalue_refs(E_vertex[i]);
1398 delete E_vertex[i];
1402 struct bfenumerator : public vertex_decomposer, public bf_base,
1403 public ienumerator_base {
1404 evalue *factor;
1406 bfenumerator(Polyhedron *P, unsigned dim, unsigned nbV) :
1407 vertex_decomposer(P, nbV, *this),
1408 bf_base(dim), ienumerator_base(dim, this) {
1409 lower = 0;
1410 factor = NULL;
1413 ~bfenumerator() {
1416 virtual void handle(const signed_cone& sc, barvinok_options *options);
1417 virtual void base(mat_ZZ& factors, bfc_vec& v);
1419 bfc_term_base* new_bf_term(int len) {
1420 bfe_term* t = new bfe_term(len);
1421 return t;
1424 virtual void set_factor(bfc_term_base *t, int k, int change) {
1425 bfe_term* bfet = static_cast<bfe_term *>(t);
1426 factor = bfet->factors[k];
1427 assert(factor != NULL);
1428 bfet->factors[k] = NULL;
1429 if (change)
1430 emul(&mone, factor);
1433 virtual void set_factor(bfc_term_base *t, int k, mpq_t &q, int change) {
1434 bfe_term* bfet = static_cast<bfe_term *>(t);
1435 factor = bfet->factors[k];
1436 assert(factor != NULL);
1437 bfet->factors[k] = NULL;
1439 evalue f;
1440 value_init(f.d);
1441 value_init(f.x.n);
1442 if (change)
1443 value_oppose(f.x.n, mpq_numref(q));
1444 else
1445 value_assign(f.x.n, mpq_numref(q));
1446 value_assign(f.d, mpq_denref(q));
1447 emul(&f, factor);
1448 free_evalue_refs(&f);
1451 virtual void set_factor(bfc_term_base *t, int k, const QQ& c, int change) {
1452 bfe_term* bfet = static_cast<bfe_term *>(t);
1454 factor = new evalue;
1456 evalue f;
1457 value_init(f.d);
1458 value_init(f.x.n);
1459 zz2value(c.n, f.x.n);
1460 if (change)
1461 value_oppose(f.x.n, f.x.n);
1462 zz2value(c.d, f.d);
1464 value_init(factor->d);
1465 evalue_copy(factor, bfet->factors[k]);
1466 emul(&f, factor);
1467 free_evalue_refs(&f);
1470 void set_factor(evalue *f, int change) {
1471 if (change)
1472 emul(&mone, f);
1473 factor = f;
1476 virtual void insert_term(bfc_term_base *t, int i) {
1477 bfe_term* bfet = static_cast<bfe_term *>(t);
1478 int len = t->terms.NumRows()-1; // already increased by one
1480 bfet->factors.resize(len+1);
1481 for (int j = len; j > i; --j) {
1482 bfet->factors[j] = bfet->factors[j-1];
1483 t->terms[j] = t->terms[j-1];
1485 bfet->factors[i] = factor;
1486 factor = NULL;
1489 virtual void update_term(bfc_term_base *t, int i) {
1490 bfe_term* bfet = static_cast<bfe_term *>(t);
1492 eadd(factor, bfet->factors[i]);
1493 free_evalue_refs(factor);
1494 delete factor;
1497 virtual bool constant_vertex(int dim) { return E_num(0, dim) == 0; }
1499 virtual void cum(bf_reducer *bfr, bfc_term_base *t, int k, dpoly_r *r,
1500 barvinok_options *options);
1503 enumerator_base *enumerator_base::create(Polyhedron *P, unsigned dim, unsigned nbV,
1504 barvinok_options *options)
1506 enumerator_base *eb;
1508 if (options->incremental_specialization == BV_SPECIALIZATION_BF)
1509 eb = new bfenumerator(P, dim, nbV);
1510 else if (options->incremental_specialization == BV_SPECIALIZATION_DF)
1511 eb = new ienumerator(P, dim, nbV);
1512 else
1513 eb = new enumerator(P, dim, nbV);
1515 return eb;
1518 struct bfe_cum : public cumulator {
1519 bfenumerator *bfe;
1520 bfc_term_base *told;
1521 int k;
1522 bf_reducer *bfr;
1524 bfe_cum(evalue *factor, evalue *v, dpoly_r *r, bf_reducer *bfr,
1525 bfc_term_base *t, int k, bfenumerator *e) :
1526 cumulator(factor, v, r), told(t), k(k),
1527 bfr(bfr), bfe(e) {
1530 virtual void add_term(const vector<int>& powers, evalue *f2);
1533 void bfe_cum::add_term(const vector<int>& powers, evalue *f2)
1535 bfr->update_powers(powers);
1537 bfc_term_base * t = bfe->find_bfc_term(bfr->vn, bfr->npowers, bfr->nnf);
1538 bfe->set_factor(f2, bfr->l_changes % 2);
1539 bfe->add_term(t, told->terms[k], bfr->l_extra_num);
1542 void bfenumerator::cum(bf_reducer *bfr, bfc_term_base *t, int k,
1543 dpoly_r *r, barvinok_options *options)
1545 bfe_term* bfet = static_cast<bfe_term *>(t);
1546 bfe_cum cum(bfet->factors[k], E_num(0, bfr->d), r, bfr, t, k, this);
1547 cum.cumulate(options);
1550 void bfenumerator::base(mat_ZZ& factors, bfc_vec& v)
1552 for (int i = 0; i < v.size(); ++i) {
1553 assert(v[i]->terms.NumRows() == 1);
1554 evalue *factor = static_cast<bfe_term *>(v[i])->factors[0];
1555 eadd(factor, vE[vert]);
1556 delete v[i];
1560 void bfenumerator::handle(const signed_cone& sc, barvinok_options *options)
1562 assert(sc.det == 1);
1563 assert(!sc.closed);
1564 assert(sc.rays.NumRows() == enumerator_base::dim);
1566 bfe_term* t = new bfe_term(enumerator_base::dim);
1567 vector< bfc_term_base * > v;
1568 v.push_back(t);
1570 t->factors.resize(1);
1572 t->terms.SetDims(1, enumerator_base::dim);
1573 lattice_point(V, sc.rays, t->terms[0], E_vertex, options);
1575 // the elements of factors are always lexpositive
1576 mat_ZZ factors;
1577 int s = setup_factors(sc.rays, factors, t, sc.sign);
1579 t->factors[0] = new evalue;
1580 value_init(t->factors[0]->d);
1581 evalue_set_si(t->factors[0], s, 1);
1582 reduce(factors, v, options);
1584 for (int i = 0; i < enumerator_base::dim; ++i)
1585 if (E_vertex[i]) {
1586 free_evalue_refs(E_vertex[i]);
1587 delete E_vertex[i];
1591 #ifdef HAVE_CORRECT_VERTICES
1592 static inline Param_Polyhedron *Polyhedron2Param_SD(Polyhedron **Din,
1593 Polyhedron *Cin,int WS,Polyhedron **CEq,Matrix **CT)
1595 if (WS & POL_NO_DUAL)
1596 WS = 0;
1597 return Polyhedron2Param_SimplifiedDomain(Din, Cin, WS, CEq, CT);
1599 #else
1600 static Param_Polyhedron *Polyhedron2Param_SD(Polyhedron **Din,
1601 Polyhedron *Cin,int WS,Polyhedron **CEq,Matrix **CT)
1603 static char data[] = " 1 0 0 0 0 1 -18 "
1604 " 1 0 0 -20 0 19 1 "
1605 " 1 0 1 20 0 -20 16 "
1606 " 1 0 0 0 0 -1 19 "
1607 " 1 0 -1 0 0 0 4 "
1608 " 1 4 -20 0 0 -1 23 "
1609 " 1 -4 20 0 0 1 -22 "
1610 " 1 0 1 0 20 -20 16 "
1611 " 1 0 0 0 -20 19 1 ";
1612 static int checked = 0;
1613 if (!checked) {
1614 checked = 1;
1615 char *p = data;
1616 int n, v, i;
1617 Matrix *M = Matrix_Alloc(9, 7);
1618 for (i = 0; i < 9; ++i)
1619 for (int j = 0; j < 7; ++j) {
1620 sscanf(p, "%d%n", &v, &n);
1621 p += n;
1622 value_set_si(M->p[i][j], v);
1624 Polyhedron *P = Constraints2Polyhedron(M, 1024);
1625 Matrix_Free(M);
1626 Polyhedron *U = Universe_Polyhedron(1);
1627 Param_Polyhedron *PP = Polyhedron2Param_Domain(P, U, 1024);
1628 Polyhedron_Free(P);
1629 Polyhedron_Free(U);
1630 Param_Vertices *V;
1631 for (i = 0, V = PP->V; V; ++i, V = V->next)
1633 if (PP)
1634 Param_Polyhedron_Free(PP);
1635 if (i != 10) {
1636 fprintf(stderr, "WARNING: results may be incorrect\n");
1637 fprintf(stderr,
1638 "WARNING: use latest version of PolyLib to remove this warning\n");
1642 return Polyhedron2Param_SimplifiedDomain(Din, Cin, WS, CEq, CT);
1644 #endif
1646 static evalue* barvinok_enumerate_ev_f(Polyhedron *P, Polyhedron* C,
1647 barvinok_options *options);
1649 /* Destroys C */
1650 static evalue* barvinok_enumerate_cst(Polyhedron *P, Polyhedron* C,
1651 struct barvinok_options *options)
1653 evalue *eres;
1655 ALLOC(evalue, eres);
1656 value_init(eres->d);
1657 value_set_si(eres->d, 0);
1658 eres->x.p = new_enode(partition, 2, C->Dimension);
1659 EVALUE_SET_DOMAIN(eres->x.p->arr[0],
1660 DomainConstraintSimplify(C, options->MaxRays));
1661 value_set_si(eres->x.p->arr[1].d, 1);
1662 value_init(eres->x.p->arr[1].x.n);
1663 if (emptyQ(P))
1664 value_set_si(eres->x.p->arr[1].x.n, 0);
1665 else
1666 barvinok_count_with_options(P, &eres->x.p->arr[1].x.n, options);
1668 return eres;
1671 evalue* barvinok_enumerate_with_options(Polyhedron *P, Polyhedron* C,
1672 struct barvinok_options *options)
1674 //P = unfringe(P, MaxRays);
1675 Polyhedron *Corig = C;
1676 Polyhedron *CEq = NULL, *rVD, *CA;
1677 int r = 0;
1678 unsigned nparam = C->Dimension;
1679 evalue *eres;
1681 evalue factor;
1682 value_init(factor.d);
1683 evalue_set_si(&factor, 1, 1);
1685 CA = align_context(C, P->Dimension, options->MaxRays);
1686 P = DomainIntersection(P, CA, options->MaxRays);
1687 Polyhedron_Free(CA);
1689 /* for now */
1690 POL_ENSURE_FACETS(P);
1691 POL_ENSURE_VERTICES(P);
1692 POL_ENSURE_FACETS(C);
1693 POL_ENSURE_VERTICES(C);
1695 if (C->Dimension == 0 || emptyQ(P)) {
1696 constant:
1697 eres = barvinok_enumerate_cst(P, CEq ? CEq : Polyhedron_Copy(C), options);
1698 out:
1699 emul(&factor, eres);
1700 reduce_evalue(eres);
1701 free_evalue_refs(&factor);
1702 Domain_Free(P);
1703 if (C != Corig)
1704 Polyhedron_Free(C);
1706 return eres;
1708 if (Polyhedron_is_infinite_param(P, nparam))
1709 goto constant;
1711 if (P->NbEq != 0) {
1712 Matrix *f;
1713 P = remove_equalities_p(P, P->Dimension-nparam, &f);
1714 mask(f, &factor, options);
1715 Matrix_Free(f);
1717 if (P->Dimension == nparam) {
1718 CEq = P;
1719 P = Universe_Polyhedron(0);
1720 goto constant;
1723 Polyhedron *T = Polyhedron_Factor(P, nparam, options->MaxRays);
1724 if (T || (P->Dimension == nparam+1)) {
1725 Polyhedron *Q;
1726 Polyhedron *C2;
1727 for (Q = T ? T : P; Q; Q = Q->next) {
1728 Polyhedron *next = Q->next;
1729 Q->next = NULL;
1731 Polyhedron *QC = Q;
1732 if (Q->Dimension != C->Dimension)
1733 QC = Polyhedron_Project(Q, nparam);
1735 C2 = C;
1736 C = DomainIntersection(C, QC, options->MaxRays);
1737 if (C2 != Corig)
1738 Polyhedron_Free(C2);
1739 if (QC != Q)
1740 Polyhedron_Free(QC);
1742 Q->next = next;
1745 if (T) {
1746 Polyhedron_Free(P);
1747 P = T;
1748 if (T->Dimension == C->Dimension) {
1749 P = T->next;
1750 T->next = NULL;
1751 Polyhedron_Free(T);
1755 Polyhedron *next = P->next;
1756 P->next = NULL;
1757 eres = barvinok_enumerate_ev_f(P, C, options);
1758 P->next = next;
1760 if (P->next) {
1761 Polyhedron *Q;
1762 evalue *f;
1764 for (Q = P->next; Q; Q = Q->next) {
1765 Polyhedron *next = Q->next;
1766 Q->next = NULL;
1768 f = barvinok_enumerate_ev_f(Q, C, options);
1769 emul(f, eres);
1770 free_evalue_refs(f);
1771 free(f);
1773 Q->next = next;
1777 goto out;
1780 evalue* barvinok_enumerate_ev(Polyhedron *P, Polyhedron* C, unsigned MaxRays)
1782 evalue *E;
1783 barvinok_options *options = barvinok_options_new_with_defaults();
1784 options->MaxRays = MaxRays;
1785 E = barvinok_enumerate_with_options(P, C, options);
1786 barvinok_options_free(options);
1787 return E;
1790 static evalue* barvinok_enumerate_ev_f(Polyhedron *P, Polyhedron* C,
1791 barvinok_options *options)
1793 unsigned nparam = C->Dimension;
1795 if (P->Dimension - nparam == 1)
1796 return ParamLine_Length(P, C, options);
1798 Param_Polyhedron *PP = NULL;
1799 Polyhedron *CEq = NULL, *pVD;
1800 Matrix *CT = NULL;
1801 Param_Domain *D, *next;
1802 Param_Vertices *V;
1803 evalue *eres;
1804 Polyhedron *Porig = P;
1806 PP = Polyhedron2Param_SD(&P,C,options->MaxRays,&CEq,&CT);
1808 if (isIdentity(CT)) {
1809 Matrix_Free(CT);
1810 CT = NULL;
1811 } else {
1812 assert(CT->NbRows != CT->NbColumns);
1813 if (CT->NbRows == 1) { // no more parameters
1814 eres = barvinok_enumerate_cst(P, CEq, options);
1815 out:
1816 if (CT)
1817 Matrix_Free(CT);
1818 if (PP)
1819 Param_Polyhedron_Free(PP);
1820 if (P != Porig)
1821 Polyhedron_Free(P);
1823 return eres;
1825 nparam = CT->NbRows - 1;
1828 unsigned dim = P->Dimension - nparam;
1830 ALLOC(evalue, eres);
1831 value_init(eres->d);
1832 value_set_si(eres->d, 0);
1834 int nd;
1835 for (nd = 0, D=PP->D; D; ++nd, D=D->next);
1836 struct section { Polyhedron *D; evalue E; };
1837 section *s = new section[nd];
1838 Polyhedron **fVD = new Polyhedron_p[nd];
1840 enumerator_base *et = NULL;
1841 try_again:
1842 if (et)
1843 delete et;
1845 et = enumerator_base::create(P, dim, PP->nbV, options);
1847 for(nd = 0, D=PP->D; D; D=next) {
1848 next = D->next;
1850 Polyhedron *rVD = reduce_domain(D->Domain, CT, CEq, fVD, nd, options);
1851 if (!rVD)
1852 continue;
1854 pVD = CT ? DomainImage(rVD,CT,options->MaxRays) : rVD;
1856 value_init(s[nd].E.d);
1857 evalue_set_si(&s[nd].E, 0, 1);
1858 s[nd].D = rVD;
1860 FORALL_PVertex_in_ParamPolyhedron(V,D,PP) // _i is internal counter
1861 if (!et->vE[_i])
1862 try {
1863 et->decompose_at(V, _i, options);
1864 } catch (OrthogonalException &e) {
1865 if (rVD != pVD)
1866 Domain_Free(pVD);
1867 for (; nd >= 0; --nd) {
1868 free_evalue_refs(&s[nd].E);
1869 Domain_Free(s[nd].D);
1870 Domain_Free(fVD[nd]);
1872 goto try_again;
1874 eadd(et->vE[_i] , &s[nd].E);
1875 END_FORALL_PVertex_in_ParamPolyhedron;
1876 evalue_range_reduction_in_domain(&s[nd].E, pVD);
1878 if (CT)
1879 addeliminatedparams_evalue(&s[nd].E, CT);
1880 ++nd;
1881 if (rVD != pVD)
1882 Domain_Free(pVD);
1885 delete et;
1886 if (nd == 0)
1887 evalue_set_si(eres, 0, 1);
1888 else {
1889 eres->x.p = new_enode(partition, 2*nd, C->Dimension);
1890 for (int j = 0; j < nd; ++j) {
1891 EVALUE_SET_DOMAIN(eres->x.p->arr[2*j], s[j].D);
1892 value_clear(eres->x.p->arr[2*j+1].d);
1893 eres->x.p->arr[2*j+1] = s[j].E;
1894 Domain_Free(fVD[j]);
1897 delete [] s;
1898 delete [] fVD;
1900 if (CEq)
1901 Polyhedron_Free(CEq);
1902 goto out;
1905 Enumeration* barvinok_enumerate(Polyhedron *P, Polyhedron* C, unsigned MaxRays)
1907 evalue *EP = barvinok_enumerate_ev(P, C, MaxRays);
1909 return partition2enumeration(EP);
1912 static void SwapColumns(Value **V, int n, int i, int j)
1914 for (int r = 0; r < n; ++r)
1915 value_swap(V[r][i], V[r][j]);
1918 static void SwapColumns(Polyhedron *P, int i, int j)
1920 SwapColumns(P->Constraint, P->NbConstraints, i, j);
1921 SwapColumns(P->Ray, P->NbRays, i, j);
1924 /* Construct a constraint c from constraints l and u such that if
1925 * if constraint c holds then for each value of the other variables
1926 * there is at most one value of variable pos (position pos+1 in the constraints).
1928 * Given a lower and an upper bound
1929 * n_l v_i + <c_l,x> + c_l >= 0
1930 * -n_u v_i + <c_u,x> + c_u >= 0
1931 * the constructed constraint is
1933 * -(n_l<c_u,x> + n_u<c_l,x>) + (-n_l c_u - n_u c_l + n_l n_u - 1)
1935 * which is then simplified to remove the content of the non-constant coefficients
1937 * len is the total length of the constraints.
1938 * v is a temporary variable that can be used by this procedure
1940 static void negative_test_constraint(Value *l, Value *u, Value *c, int pos,
1941 int len, Value *v)
1943 value_oppose(*v, u[pos+1]);
1944 Vector_Combine(l+1, u+1, c+1, *v, l[pos+1], len-1);
1945 value_multiply(*v, *v, l[pos+1]);
1946 value_subtract(c[len-1], c[len-1], *v);
1947 value_set_si(*v, -1);
1948 Vector_Scale(c+1, c+1, *v, len-1);
1949 value_decrement(c[len-1], c[len-1]);
1950 ConstraintSimplify(c, c, len, v);
1953 static bool parallel_constraints(Value *l, Value *u, Value *c, int pos,
1954 int len)
1956 bool parallel;
1957 Value g1;
1958 Value g2;
1959 value_init(g1);
1960 value_init(g2);
1962 Vector_Gcd(&l[1+pos], len, &g1);
1963 Vector_Gcd(&u[1+pos], len, &g2);
1964 Vector_Combine(l+1+pos, u+1+pos, c+1, g2, g1, len);
1965 parallel = First_Non_Zero(c+1, len) == -1;
1967 value_clear(g1);
1968 value_clear(g2);
1970 return parallel;
1973 static void negative_test_constraint7(Value *l, Value *u, Value *c, int pos,
1974 int exist, int len, Value *v)
1976 Value g;
1977 value_init(g);
1979 Vector_Gcd(&u[1+pos], exist, v);
1980 Vector_Gcd(&l[1+pos], exist, &g);
1981 Vector_Combine(l+1, u+1, c+1, *v, g, len-1);
1982 value_multiply(*v, *v, g);
1983 value_subtract(c[len-1], c[len-1], *v);
1984 value_set_si(*v, -1);
1985 Vector_Scale(c+1, c+1, *v, len-1);
1986 value_decrement(c[len-1], c[len-1]);
1987 ConstraintSimplify(c, c, len, v);
1989 value_clear(g);
1992 /* Turns a x + b >= 0 into a x + b <= -1
1994 * len is the total length of the constraint.
1995 * v is a temporary variable that can be used by this procedure
1997 static void oppose_constraint(Value *c, int len, Value *v)
1999 value_set_si(*v, -1);
2000 Vector_Scale(c+1, c+1, *v, len-1);
2001 value_decrement(c[len-1], c[len-1]);
2004 /* Split polyhedron P into two polyhedra *pos and *neg, where
2005 * existential variable i has at most one solution for each
2006 * value of the other variables in *neg.
2008 * The splitting is performed using constraints l and u.
2010 * nvar: number of set variables
2011 * row: temporary vector that can be used by this procedure
2012 * f: temporary value that can be used by this procedure
2014 static bool SplitOnConstraint(Polyhedron *P, int i, int l, int u,
2015 int nvar, int MaxRays, Vector *row, Value& f,
2016 Polyhedron **pos, Polyhedron **neg)
2018 negative_test_constraint(P->Constraint[l], P->Constraint[u],
2019 row->p, nvar+i, P->Dimension+2, &f);
2020 *neg = AddConstraints(row->p, 1, P, MaxRays);
2022 /* We found an independent, but useless constraint
2023 * Maybe we should detect this earlier and not
2024 * mark the variable as INDEPENDENT
2026 if (emptyQ((*neg))) {
2027 Polyhedron_Free(*neg);
2028 return false;
2031 oppose_constraint(row->p, P->Dimension+2, &f);
2032 *pos = AddConstraints(row->p, 1, P, MaxRays);
2034 if (emptyQ((*pos))) {
2035 Polyhedron_Free(*neg);
2036 Polyhedron_Free(*pos);
2037 return false;
2040 return true;
2044 * unimodularly transform P such that constraint r is transformed
2045 * into a constraint that involves only a single (the first)
2046 * existential variable
2049 static Polyhedron *rotate_along(Polyhedron *P, int r, int nvar, int exist,
2050 unsigned MaxRays)
2052 Value g;
2053 value_init(g);
2055 Vector *row = Vector_Alloc(exist);
2056 Vector_Copy(P->Constraint[r]+1+nvar, row->p, exist);
2057 Vector_Gcd(row->p, exist, &g);
2058 if (value_notone_p(g))
2059 Vector_AntiScale(row->p, row->p, g, exist);
2060 value_clear(g);
2062 Matrix *M = unimodular_complete(row);
2063 Matrix *M2 = Matrix_Alloc(P->Dimension+1, P->Dimension+1);
2064 for (r = 0; r < nvar; ++r)
2065 value_set_si(M2->p[r][r], 1);
2066 for ( ; r < nvar+exist; ++r)
2067 Vector_Copy(M->p[r-nvar], M2->p[r]+nvar, exist);
2068 for ( ; r < P->Dimension+1; ++r)
2069 value_set_si(M2->p[r][r], 1);
2070 Polyhedron *T = Polyhedron_Image(P, M2, MaxRays);
2072 Matrix_Free(M2);
2073 Matrix_Free(M);
2074 Vector_Free(row);
2076 return T;
2079 /* Split polyhedron P into two polyhedra *pos and *neg, where
2080 * existential variable i has at most one solution for each
2081 * value of the other variables in *neg.
2083 * If independent is set, then the two constraints on which the
2084 * split will be performed need to be independent of the other
2085 * existential variables.
2087 * Return true if an appropriate split could be performed.
2089 * nvar: number of set variables
2090 * exist: number of existential variables
2091 * row: temporary vector that can be used by this procedure
2092 * f: temporary value that can be used by this procedure
2094 static bool SplitOnVar(Polyhedron *P, int i,
2095 int nvar, int exist, int MaxRays,
2096 Vector *row, Value& f, bool independent,
2097 Polyhedron **pos, Polyhedron **neg)
2099 int j;
2101 for (int l = P->NbEq; l < P->NbConstraints; ++l) {
2102 if (value_negz_p(P->Constraint[l][nvar+i+1]))
2103 continue;
2105 if (independent) {
2106 for (j = 0; j < exist; ++j)
2107 if (j != i && value_notzero_p(P->Constraint[l][nvar+j+1]))
2108 break;
2109 if (j < exist)
2110 continue;
2113 for (int u = P->NbEq; u < P->NbConstraints; ++u) {
2114 if (value_posz_p(P->Constraint[u][nvar+i+1]))
2115 continue;
2117 if (independent) {
2118 for (j = 0; j < exist; ++j)
2119 if (j != i && value_notzero_p(P->Constraint[u][nvar+j+1]))
2120 break;
2121 if (j < exist)
2122 continue;
2125 if (SplitOnConstraint(P, i, l, u, nvar, MaxRays, row, f, pos, neg)) {
2126 if (independent) {
2127 if (i != 0)
2128 SwapColumns(*neg, nvar+1, nvar+1+i);
2130 return true;
2135 return false;
2138 static bool double_bound_pair(Polyhedron *P, int nvar, int exist,
2139 int i, int l1, int l2,
2140 Polyhedron **pos, Polyhedron **neg)
2142 Value f;
2143 value_init(f);
2144 Vector *row = Vector_Alloc(P->Dimension+2);
2145 value_set_si(row->p[0], 1);
2146 value_oppose(f, P->Constraint[l1][nvar+i+1]);
2147 Vector_Combine(P->Constraint[l1]+1, P->Constraint[l2]+1,
2148 row->p+1,
2149 P->Constraint[l2][nvar+i+1], f,
2150 P->Dimension+1);
2151 ConstraintSimplify(row->p, row->p, P->Dimension+2, &f);
2152 *pos = AddConstraints(row->p, 1, P, 0);
2153 value_set_si(f, -1);
2154 Vector_Scale(row->p+1, row->p+1, f, P->Dimension+1);
2155 value_decrement(row->p[P->Dimension+1], row->p[P->Dimension+1]);
2156 *neg = AddConstraints(row->p, 1, P, 0);
2157 Vector_Free(row);
2158 value_clear(f);
2160 return !emptyQ((*pos)) && !emptyQ((*neg));
2163 static bool double_bound(Polyhedron *P, int nvar, int exist,
2164 Polyhedron **pos, Polyhedron **neg)
2166 for (int i = 0; i < exist; ++i) {
2167 int l1, l2;
2168 for (l1 = P->NbEq; l1 < P->NbConstraints; ++l1) {
2169 if (value_negz_p(P->Constraint[l1][nvar+i+1]))
2170 continue;
2171 for (l2 = l1 + 1; l2 < P->NbConstraints; ++l2) {
2172 if (value_negz_p(P->Constraint[l2][nvar+i+1]))
2173 continue;
2174 if (double_bound_pair(P, nvar, exist, i, l1, l2, pos, neg))
2175 return true;
2178 for (l1 = P->NbEq; l1 < P->NbConstraints; ++l1) {
2179 if (value_posz_p(P->Constraint[l1][nvar+i+1]))
2180 continue;
2181 if (l1 < P->NbConstraints)
2182 for (l2 = l1 + 1; l2 < P->NbConstraints; ++l2) {
2183 if (value_posz_p(P->Constraint[l2][nvar+i+1]))
2184 continue;
2185 if (double_bound_pair(P, nvar, exist, i, l1, l2, pos, neg))
2186 return true;
2189 return false;
2191 return false;
2194 enum constraint {
2195 ALL_POS = 1 << 0,
2196 ONE_NEG = 1 << 1,
2197 INDEPENDENT = 1 << 2,
2198 ROT_NEG = 1 << 3
2201 static evalue* enumerate_or(Polyhedron *D,
2202 unsigned exist, unsigned nparam, barvinok_options *options)
2204 #ifdef DEBUG_ER
2205 fprintf(stderr, "\nER: Or\n");
2206 #endif /* DEBUG_ER */
2208 Polyhedron *N = D->next;
2209 D->next = 0;
2210 evalue *EP =
2211 barvinok_enumerate_e_with_options(D, exist, nparam, options);
2212 Polyhedron_Free(D);
2214 for (D = N; D; D = N) {
2215 N = D->next;
2216 D->next = 0;
2218 evalue *EN =
2219 barvinok_enumerate_e_with_options(D, exist, nparam, options);
2221 eor(EN, EP);
2222 free_evalue_refs(EN);
2223 free(EN);
2224 Polyhedron_Free(D);
2227 reduce_evalue(EP);
2229 return EP;
2232 static evalue* enumerate_sum(Polyhedron *P,
2233 unsigned exist, unsigned nparam, barvinok_options *options)
2235 int nvar = P->Dimension - exist - nparam;
2236 int toswap = nvar < exist ? nvar : exist;
2237 for (int i = 0; i < toswap; ++i)
2238 SwapColumns(P, 1 + i, nvar+exist - i);
2239 nparam += nvar;
2241 #ifdef DEBUG_ER
2242 fprintf(stderr, "\nER: Sum\n");
2243 #endif /* DEBUG_ER */
2245 evalue *EP = barvinok_enumerate_e_with_options(P, exist, nparam, options);
2247 for (int i = 0; i < /* nvar */ nparam; ++i) {
2248 Matrix *C = Matrix_Alloc(1, 1 + nparam + 1);
2249 value_set_si(C->p[0][0], 1);
2250 evalue split;
2251 value_init(split.d);
2252 value_set_si(split.d, 0);
2253 split.x.p = new_enode(partition, 4, nparam);
2254 value_set_si(C->p[0][1+i], 1);
2255 Matrix *C2 = Matrix_Copy(C);
2256 EVALUE_SET_DOMAIN(split.x.p->arr[0],
2257 Constraints2Polyhedron(C2, options->MaxRays));
2258 Matrix_Free(C2);
2259 evalue_set_si(&split.x.p->arr[1], 1, 1);
2260 value_set_si(C->p[0][1+i], -1);
2261 value_set_si(C->p[0][1+nparam], -1);
2262 EVALUE_SET_DOMAIN(split.x.p->arr[2],
2263 Constraints2Polyhedron(C, options->MaxRays));
2264 evalue_set_si(&split.x.p->arr[3], 1, 1);
2265 emul(&split, EP);
2266 free_evalue_refs(&split);
2267 Matrix_Free(C);
2269 reduce_evalue(EP);
2270 evalue_range_reduction(EP);
2272 evalue_frac2floor2(EP, 1);
2274 evalue *sum = esum(EP, nvar);
2276 free_evalue_refs(EP);
2277 free(EP);
2278 EP = sum;
2280 evalue_range_reduction(EP);
2282 return EP;
2285 static evalue* split_sure(Polyhedron *P, Polyhedron *S,
2286 unsigned exist, unsigned nparam, barvinok_options *options)
2288 int nvar = P->Dimension - exist - nparam;
2290 Matrix *M = Matrix_Alloc(exist, S->Dimension+2);
2291 for (int i = 0; i < exist; ++i)
2292 value_set_si(M->p[i][nvar+i+1], 1);
2293 Polyhedron *O = S;
2294 S = DomainAddRays(S, M, options->MaxRays);
2295 Polyhedron_Free(O);
2296 Polyhedron *F = DomainAddRays(P, M, options->MaxRays);
2297 Polyhedron *D = DomainDifference(F, S, options->MaxRays);
2298 O = D;
2299 D = Disjoint_Domain(D, 0, options->MaxRays);
2300 Polyhedron_Free(F);
2301 Domain_Free(O);
2302 Matrix_Free(M);
2304 M = Matrix_Alloc(P->Dimension+1-exist, P->Dimension+1);
2305 for (int j = 0; j < nvar; ++j)
2306 value_set_si(M->p[j][j], 1);
2307 for (int j = 0; j < nparam+1; ++j)
2308 value_set_si(M->p[nvar+j][nvar+exist+j], 1);
2309 Polyhedron *T = Polyhedron_Image(S, M, options->MaxRays);
2310 evalue *EP = barvinok_enumerate_e_with_options(T, 0, nparam, options);
2311 Polyhedron_Free(S);
2312 Polyhedron_Free(T);
2313 Matrix_Free(M);
2315 for (Polyhedron *Q = D; Q; Q = Q->next) {
2316 Polyhedron *N = Q->next;
2317 Q->next = 0;
2318 T = DomainIntersection(P, Q, options->MaxRays);
2319 evalue *E = barvinok_enumerate_e_with_options(T, exist, nparam, options);
2320 eadd(E, EP);
2321 free_evalue_refs(E);
2322 free(E);
2323 Polyhedron_Free(T);
2324 Q->next = N;
2326 Domain_Free(D);
2327 return EP;
2330 static evalue* enumerate_sure(Polyhedron *P,
2331 unsigned exist, unsigned nparam, barvinok_options *options)
2333 int i;
2334 Polyhedron *S = P;
2335 int nvar = P->Dimension - exist - nparam;
2336 Value lcm;
2337 Value f;
2338 value_init(lcm);
2339 value_init(f);
2341 for (i = 0; i < exist; ++i) {
2342 Matrix *M = Matrix_Alloc(S->NbConstraints, S->Dimension+2);
2343 int c = 0;
2344 value_set_si(lcm, 1);
2345 for (int j = 0; j < S->NbConstraints; ++j) {
2346 if (value_negz_p(S->Constraint[j][1+nvar+i]))
2347 continue;
2348 if (value_one_p(S->Constraint[j][1+nvar+i]))
2349 continue;
2350 value_lcm(lcm, S->Constraint[j][1+nvar+i], &lcm);
2353 for (int j = 0; j < S->NbConstraints; ++j) {
2354 if (value_negz_p(S->Constraint[j][1+nvar+i]))
2355 continue;
2356 if (value_one_p(S->Constraint[j][1+nvar+i]))
2357 continue;
2358 value_division(f, lcm, S->Constraint[j][1+nvar+i]);
2359 Vector_Scale(S->Constraint[j], M->p[c], f, S->Dimension+2);
2360 value_subtract(M->p[c][S->Dimension+1],
2361 M->p[c][S->Dimension+1],
2362 lcm);
2363 value_increment(M->p[c][S->Dimension+1],
2364 M->p[c][S->Dimension+1]);
2365 ++c;
2367 Polyhedron *O = S;
2368 S = AddConstraints(M->p[0], c, S, options->MaxRays);
2369 if (O != P)
2370 Polyhedron_Free(O);
2371 Matrix_Free(M);
2372 if (emptyQ(S)) {
2373 Polyhedron_Free(S);
2374 value_clear(lcm);
2375 value_clear(f);
2376 return 0;
2379 value_clear(lcm);
2380 value_clear(f);
2382 #ifdef DEBUG_ER
2383 fprintf(stderr, "\nER: Sure\n");
2384 #endif /* DEBUG_ER */
2386 return split_sure(P, S, exist, nparam, options);
2389 static evalue* enumerate_sure2(Polyhedron *P,
2390 unsigned exist, unsigned nparam, barvinok_options *options)
2392 int nvar = P->Dimension - exist - nparam;
2393 int r;
2394 for (r = 0; r < P->NbRays; ++r)
2395 if (value_one_p(P->Ray[r][0]) &&
2396 value_one_p(P->Ray[r][P->Dimension+1]))
2397 break;
2399 if (r >= P->NbRays)
2400 return 0;
2402 Matrix *M = Matrix_Alloc(nvar + 1 + nparam, P->Dimension+2);
2403 for (int i = 0; i < nvar; ++i)
2404 value_set_si(M->p[i][1+i], 1);
2405 for (int i = 0; i < nparam; ++i)
2406 value_set_si(M->p[i+nvar][1+nvar+exist+i], 1);
2407 Vector_Copy(P->Ray[r]+1+nvar, M->p[nvar+nparam]+1+nvar, exist);
2408 value_set_si(M->p[nvar+nparam][0], 1);
2409 value_set_si(M->p[nvar+nparam][P->Dimension+1], 1);
2410 Polyhedron * F = Rays2Polyhedron(M, options->MaxRays);
2411 Matrix_Free(M);
2413 Polyhedron *I = DomainIntersection(F, P, options->MaxRays);
2414 Polyhedron_Free(F);
2416 #ifdef DEBUG_ER
2417 fprintf(stderr, "\nER: Sure2\n");
2418 #endif /* DEBUG_ER */
2420 return split_sure(P, I, exist, nparam, options);
2423 static evalue* enumerate_cyclic(Polyhedron *P,
2424 unsigned exist, unsigned nparam,
2425 evalue * EP, int r, int p, unsigned MaxRays)
2427 int nvar = P->Dimension - exist - nparam;
2429 /* If EP in its fractional maps only contains references
2430 * to the remainder parameter with appropriate coefficients
2431 * then we could in principle avoid adding existentially
2432 * quantified variables to the validity domains.
2433 * We'd have to replace the remainder by m { p/m }
2434 * and multiply with an appropriate factor that is one
2435 * only in the appropriate range.
2436 * This last multiplication can be avoided if EP
2437 * has a single validity domain with no (further)
2438 * constraints on the remainder parameter
2441 Matrix *CT = Matrix_Alloc(nparam+1, nparam+3);
2442 Matrix *M = Matrix_Alloc(1, 1+nparam+3);
2443 for (int j = 0; j < nparam; ++j)
2444 if (j != p)
2445 value_set_si(CT->p[j][j], 1);
2446 value_set_si(CT->p[p][nparam+1], 1);
2447 value_set_si(CT->p[nparam][nparam+2], 1);
2448 value_set_si(M->p[0][1+p], -1);
2449 value_absolute(M->p[0][1+nparam], P->Ray[0][1+nvar+exist+p]);
2450 value_set_si(M->p[0][1+nparam+1], 1);
2451 Polyhedron *CEq = Constraints2Polyhedron(M, 1);
2452 Matrix_Free(M);
2453 addeliminatedparams_enum(EP, CT, CEq, MaxRays, nparam);
2454 Polyhedron_Free(CEq);
2455 Matrix_Free(CT);
2457 return EP;
2460 static void enumerate_vd_add_ray(evalue *EP, Matrix *Rays, unsigned MaxRays)
2462 if (value_notzero_p(EP->d))
2463 return;
2465 assert(EP->x.p->type == partition);
2466 assert(EP->x.p->pos == EVALUE_DOMAIN(EP->x.p->arr[0])->Dimension);
2467 for (int i = 0; i < EP->x.p->size/2; ++i) {
2468 Polyhedron *D = EVALUE_DOMAIN(EP->x.p->arr[2*i]);
2469 Polyhedron *N = DomainAddRays(D, Rays, MaxRays);
2470 EVALUE_SET_DOMAIN(EP->x.p->arr[2*i], N);
2471 Domain_Free(D);
2475 static evalue* enumerate_line(Polyhedron *P,
2476 unsigned exist, unsigned nparam, barvinok_options *options)
2478 if (P->NbBid == 0)
2479 return 0;
2481 #ifdef DEBUG_ER
2482 fprintf(stderr, "\nER: Line\n");
2483 #endif /* DEBUG_ER */
2485 int nvar = P->Dimension - exist - nparam;
2486 int i, j;
2487 for (i = 0; i < nparam; ++i)
2488 if (value_notzero_p(P->Ray[0][1+nvar+exist+i]))
2489 break;
2490 assert(i < nparam);
2491 for (j = i+1; j < nparam; ++j)
2492 if (value_notzero_p(P->Ray[0][1+nvar+exist+i]))
2493 break;
2494 assert(j >= nparam); // for now
2496 Matrix *M = Matrix_Alloc(2, P->Dimension+2);
2497 value_set_si(M->p[0][0], 1);
2498 value_set_si(M->p[0][1+nvar+exist+i], 1);
2499 value_set_si(M->p[1][0], 1);
2500 value_set_si(M->p[1][1+nvar+exist+i], -1);
2501 value_absolute(M->p[1][1+P->Dimension], P->Ray[0][1+nvar+exist+i]);
2502 value_decrement(M->p[1][1+P->Dimension], M->p[1][1+P->Dimension]);
2503 Polyhedron *S = AddConstraints(M->p[0], 2, P, options->MaxRays);
2504 evalue *EP = barvinok_enumerate_e_with_options(S, exist, nparam, options);
2505 Polyhedron_Free(S);
2506 Matrix_Free(M);
2508 return enumerate_cyclic(P, exist, nparam, EP, 0, i, options->MaxRays);
2511 static int single_param_pos(Polyhedron*P, unsigned exist, unsigned nparam,
2512 int r)
2514 int nvar = P->Dimension - exist - nparam;
2515 if (First_Non_Zero(P->Ray[r]+1, nvar) != -1)
2516 return -1;
2517 int i = First_Non_Zero(P->Ray[r]+1+nvar+exist, nparam);
2518 if (i == -1)
2519 return -1;
2520 if (First_Non_Zero(P->Ray[r]+1+nvar+exist+1, nparam-i-1) != -1)
2521 return -1;
2522 return i;
2525 static evalue* enumerate_remove_ray(Polyhedron *P, int r,
2526 unsigned exist, unsigned nparam, barvinok_options *options)
2528 #ifdef DEBUG_ER
2529 fprintf(stderr, "\nER: RedundantRay\n");
2530 #endif /* DEBUG_ER */
2532 Value one;
2533 value_init(one);
2534 value_set_si(one, 1);
2535 int len = P->NbRays-1;
2536 Matrix *M = Matrix_Alloc(2 * len, P->Dimension+2);
2537 Vector_Copy(P->Ray[0], M->p[0], r * (P->Dimension+2));
2538 Vector_Copy(P->Ray[r+1], M->p[r], (len-r) * (P->Dimension+2));
2539 for (int j = 0; j < P->NbRays; ++j) {
2540 if (j == r)
2541 continue;
2542 Vector_Combine(P->Ray[j], P->Ray[r], M->p[len+j-(j>r)],
2543 one, P->Ray[j][P->Dimension+1], P->Dimension+2);
2546 P = Rays2Polyhedron(M, options->MaxRays);
2547 Matrix_Free(M);
2548 evalue *EP = barvinok_enumerate_e_with_options(P, exist, nparam, options);
2549 Polyhedron_Free(P);
2550 value_clear(one);
2552 return EP;
2555 static evalue* enumerate_redundant_ray(Polyhedron *P,
2556 unsigned exist, unsigned nparam, barvinok_options *options)
2558 assert(P->NbBid == 0);
2559 int nvar = P->Dimension - exist - nparam;
2560 Value m;
2561 value_init(m);
2563 for (int r = 0; r < P->NbRays; ++r) {
2564 if (value_notzero_p(P->Ray[r][P->Dimension+1]))
2565 continue;
2566 int i1 = single_param_pos(P, exist, nparam, r);
2567 if (i1 == -1)
2568 continue;
2569 for (int r2 = r+1; r2 < P->NbRays; ++r2) {
2570 if (value_notzero_p(P->Ray[r2][P->Dimension+1]))
2571 continue;
2572 int i2 = single_param_pos(P, exist, nparam, r2);
2573 if (i2 == -1)
2574 continue;
2575 if (i1 != i2)
2576 continue;
2578 value_division(m, P->Ray[r][1+nvar+exist+i1],
2579 P->Ray[r2][1+nvar+exist+i1]);
2580 value_multiply(m, m, P->Ray[r2][1+nvar+exist+i1]);
2581 /* r2 divides r => r redundant */
2582 if (value_eq(m, P->Ray[r][1+nvar+exist+i1])) {
2583 value_clear(m);
2584 return enumerate_remove_ray(P, r, exist, nparam, options);
2587 value_division(m, P->Ray[r2][1+nvar+exist+i1],
2588 P->Ray[r][1+nvar+exist+i1]);
2589 value_multiply(m, m, P->Ray[r][1+nvar+exist+i1]);
2590 /* r divides r2 => r2 redundant */
2591 if (value_eq(m, P->Ray[r2][1+nvar+exist+i1])) {
2592 value_clear(m);
2593 return enumerate_remove_ray(P, r2, exist, nparam, options);
2597 value_clear(m);
2598 return 0;
2601 static Polyhedron *upper_bound(Polyhedron *P,
2602 int pos, Value *max, Polyhedron **R)
2604 Value v;
2605 int r;
2606 value_init(v);
2608 *R = 0;
2609 Polyhedron *N;
2610 Polyhedron *B = 0;
2611 for (Polyhedron *Q = P; Q; Q = N) {
2612 N = Q->next;
2613 for (r = 0; r < P->NbRays; ++r) {
2614 if (value_zero_p(P->Ray[r][P->Dimension+1]) &&
2615 value_pos_p(P->Ray[r][1+pos]))
2616 break;
2618 if (r < P->NbRays) {
2619 Q->next = *R;
2620 *R = Q;
2621 continue;
2622 } else {
2623 Q->next = B;
2624 B = Q;
2626 for (r = 0; r < P->NbRays; ++r) {
2627 if (value_zero_p(P->Ray[r][P->Dimension+1]))
2628 continue;
2629 mpz_fdiv_q(v, P->Ray[r][1+pos], P->Ray[r][1+P->Dimension]);
2630 if ((!Q->next && r == 0) || value_gt(v, *max))
2631 value_assign(*max, v);
2634 value_clear(v);
2635 return B;
2638 static evalue* enumerate_ray(Polyhedron *P,
2639 unsigned exist, unsigned nparam, barvinok_options *options)
2641 assert(P->NbBid == 0);
2642 int nvar = P->Dimension - exist - nparam;
2644 int r;
2645 for (r = 0; r < P->NbRays; ++r)
2646 if (value_zero_p(P->Ray[r][P->Dimension+1]))
2647 break;
2648 if (r >= P->NbRays)
2649 return 0;
2651 int r2;
2652 for (r2 = r+1; r2 < P->NbRays; ++r2)
2653 if (value_zero_p(P->Ray[r2][P->Dimension+1]))
2654 break;
2655 if (r2 < P->NbRays) {
2656 if (nvar > 0)
2657 return enumerate_sum(P, exist, nparam, options);
2660 #ifdef DEBUG_ER
2661 fprintf(stderr, "\nER: Ray\n");
2662 #endif /* DEBUG_ER */
2664 Value m;
2665 Value one;
2666 value_init(m);
2667 value_init(one);
2668 value_set_si(one, 1);
2669 int i = single_param_pos(P, exist, nparam, r);
2670 assert(i != -1); // for now;
2672 Matrix *M = Matrix_Alloc(P->NbRays, P->Dimension+2);
2673 for (int j = 0; j < P->NbRays; ++j) {
2674 Vector_Combine(P->Ray[j], P->Ray[r], M->p[j],
2675 one, P->Ray[j][P->Dimension+1], P->Dimension+2);
2677 Polyhedron *S = Rays2Polyhedron(M, options->MaxRays);
2678 Matrix_Free(M);
2679 Polyhedron *D = DomainDifference(P, S, options->MaxRays);
2680 Polyhedron_Free(S);
2681 // Polyhedron_Print(stderr, P_VALUE_FMT, D);
2682 assert(value_pos_p(P->Ray[r][1+nvar+exist+i])); // for now
2683 Polyhedron *R;
2684 D = upper_bound(D, nvar+exist+i, &m, &R);
2685 assert(D);
2686 Domain_Free(D);
2688 M = Matrix_Alloc(2, P->Dimension+2);
2689 value_set_si(M->p[0][0], 1);
2690 value_set_si(M->p[1][0], 1);
2691 value_set_si(M->p[0][1+nvar+exist+i], -1);
2692 value_set_si(M->p[1][1+nvar+exist+i], 1);
2693 value_assign(M->p[0][1+P->Dimension], m);
2694 value_oppose(M->p[1][1+P->Dimension], m);
2695 value_addto(M->p[1][1+P->Dimension], M->p[1][1+P->Dimension],
2696 P->Ray[r][1+nvar+exist+i]);
2697 value_decrement(M->p[1][1+P->Dimension], M->p[1][1+P->Dimension]);
2698 // Matrix_Print(stderr, P_VALUE_FMT, M);
2699 D = AddConstraints(M->p[0], 2, P, options->MaxRays);
2700 // Polyhedron_Print(stderr, P_VALUE_FMT, D);
2701 value_subtract(M->p[0][1+P->Dimension], M->p[0][1+P->Dimension],
2702 P->Ray[r][1+nvar+exist+i]);
2703 // Matrix_Print(stderr, P_VALUE_FMT, M);
2704 S = AddConstraints(M->p[0], 1, P, options->MaxRays);
2705 // Polyhedron_Print(stderr, P_VALUE_FMT, S);
2706 Matrix_Free(M);
2708 evalue *EP = barvinok_enumerate_e_with_options(D, exist, nparam, options);
2709 Polyhedron_Free(D);
2710 value_clear(one);
2711 value_clear(m);
2713 if (value_notone_p(P->Ray[r][1+nvar+exist+i]))
2714 EP = enumerate_cyclic(P, exist, nparam, EP, r, i, options->MaxRays);
2715 else {
2716 M = Matrix_Alloc(1, nparam+2);
2717 value_set_si(M->p[0][0], 1);
2718 value_set_si(M->p[0][1+i], 1);
2719 enumerate_vd_add_ray(EP, M, options->MaxRays);
2720 Matrix_Free(M);
2723 if (!emptyQ(S)) {
2724 evalue *E = barvinok_enumerate_e_with_options(S, exist, nparam, options);
2725 eadd(E, EP);
2726 free_evalue_refs(E);
2727 free(E);
2729 Polyhedron_Free(S);
2731 if (R) {
2732 assert(nvar == 0);
2733 evalue *ER = enumerate_or(R, exist, nparam, options);
2734 eor(ER, EP);
2735 free_evalue_refs(ER);
2736 free(ER);
2739 return EP;
2742 static evalue* enumerate_vd(Polyhedron **PA,
2743 unsigned exist, unsigned nparam, barvinok_options *options)
2745 Polyhedron *P = *PA;
2746 int nvar = P->Dimension - exist - nparam;
2747 Param_Polyhedron *PP = NULL;
2748 Polyhedron *C = Universe_Polyhedron(nparam);
2749 Polyhedron *CEq;
2750 Matrix *CT;
2751 Polyhedron *PR = P;
2752 PP = Polyhedron2Param_SimplifiedDomain(&PR,C, options->MaxRays,&CEq,&CT);
2753 Polyhedron_Free(C);
2755 int nd;
2756 Param_Domain *D, *last;
2757 Value c;
2758 value_init(c);
2759 for (nd = 0, D=PP->D; D; D=D->next, ++nd)
2762 Polyhedron **VD = new Polyhedron_p[nd];
2763 Polyhedron **fVD = new Polyhedron_p[nd];
2764 for(nd = 0, D=PP->D; D; D=D->next) {
2765 Polyhedron *rVD = reduce_domain(D->Domain, CT, CEq, fVD, nd, options);
2766 if (!rVD)
2767 continue;
2769 VD[nd++] = rVD;
2770 last = D;
2773 evalue *EP = 0;
2775 if (nd == 0)
2776 EP = evalue_zero();
2778 /* This doesn't seem to have any effect */
2779 if (nd == 1) {
2780 Polyhedron *CA = align_context(VD[0], P->Dimension, options->MaxRays);
2781 Polyhedron *O = P;
2782 P = DomainIntersection(P, CA, options->MaxRays);
2783 if (O != *PA)
2784 Polyhedron_Free(O);
2785 Polyhedron_Free(CA);
2786 if (emptyQ(P))
2787 EP = evalue_zero();
2790 if (!EP && CT->NbColumns != CT->NbRows) {
2791 Polyhedron *CEqr = DomainImage(CEq, CT, options->MaxRays);
2792 Polyhedron *CA = align_context(CEqr, PR->Dimension, options->MaxRays);
2793 Polyhedron *I = DomainIntersection(PR, CA, options->MaxRays);
2794 Polyhedron_Free(CEqr);
2795 Polyhedron_Free(CA);
2796 #ifdef DEBUG_ER
2797 fprintf(stderr, "\nER: Eliminate\n");
2798 #endif /* DEBUG_ER */
2799 nparam -= CT->NbColumns - CT->NbRows;
2800 EP = barvinok_enumerate_e_with_options(I, exist, nparam, options);
2801 nparam += CT->NbColumns - CT->NbRows;
2802 addeliminatedparams_enum(EP, CT, CEq, options->MaxRays, nparam);
2803 Polyhedron_Free(I);
2805 if (PR != *PA)
2806 Polyhedron_Free(PR);
2807 PR = 0;
2809 if (!EP && nd > 1) {
2810 #ifdef DEBUG_ER
2811 fprintf(stderr, "\nER: VD\n");
2812 #endif /* DEBUG_ER */
2813 for (int i = 0; i < nd; ++i) {
2814 Polyhedron *CA = align_context(VD[i], P->Dimension, options->MaxRays);
2815 Polyhedron *I = DomainIntersection(P, CA, options->MaxRays);
2817 if (i == 0)
2818 EP = barvinok_enumerate_e_with_options(I, exist, nparam, options);
2819 else {
2820 evalue *E = barvinok_enumerate_e_with_options(I, exist, nparam,
2821 options);
2822 eadd(E, EP);
2823 free_evalue_refs(E);
2824 free(E);
2826 Polyhedron_Free(I);
2827 Polyhedron_Free(CA);
2831 for (int i = 0; i < nd; ++i) {
2832 Polyhedron_Free(VD[i]);
2833 Polyhedron_Free(fVD[i]);
2835 delete [] VD;
2836 delete [] fVD;
2837 value_clear(c);
2839 if (!EP && nvar == 0) {
2840 Value f;
2841 value_init(f);
2842 Param_Vertices *V, *V2;
2843 Matrix* M = Matrix_Alloc(1, P->Dimension+2);
2845 FORALL_PVertex_in_ParamPolyhedron(V, last, PP) {
2846 bool found = false;
2847 FORALL_PVertex_in_ParamPolyhedron(V2, last, PP) {
2848 if (V == V2) {
2849 found = true;
2850 continue;
2852 if (!found)
2853 continue;
2854 for (int i = 0; i < exist; ++i) {
2855 value_oppose(f, V->Vertex->p[i][nparam+1]);
2856 Vector_Combine(V->Vertex->p[i],
2857 V2->Vertex->p[i],
2858 M->p[0] + 1 + nvar + exist,
2859 V2->Vertex->p[i][nparam+1],
2861 nparam+1);
2862 int j;
2863 for (j = 0; j < nparam; ++j)
2864 if (value_notzero_p(M->p[0][1+nvar+exist+j]))
2865 break;
2866 if (j >= nparam)
2867 continue;
2868 ConstraintSimplify(M->p[0], M->p[0],
2869 P->Dimension+2, &f);
2870 value_set_si(M->p[0][0], 0);
2871 Polyhedron *para = AddConstraints(M->p[0], 1, P,
2872 options->MaxRays);
2873 if (emptyQ(para)) {
2874 Polyhedron_Free(para);
2875 continue;
2877 Polyhedron *pos, *neg;
2878 value_set_si(M->p[0][0], 1);
2879 value_decrement(M->p[0][P->Dimension+1],
2880 M->p[0][P->Dimension+1]);
2881 neg = AddConstraints(M->p[0], 1, P, options->MaxRays);
2882 value_set_si(f, -1);
2883 Vector_Scale(M->p[0]+1, M->p[0]+1, f,
2884 P->Dimension+1);
2885 value_decrement(M->p[0][P->Dimension+1],
2886 M->p[0][P->Dimension+1]);
2887 value_decrement(M->p[0][P->Dimension+1],
2888 M->p[0][P->Dimension+1]);
2889 pos = AddConstraints(M->p[0], 1, P, options->MaxRays);
2890 if (emptyQ(neg) && emptyQ(pos)) {
2891 Polyhedron_Free(para);
2892 Polyhedron_Free(pos);
2893 Polyhedron_Free(neg);
2894 continue;
2896 #ifdef DEBUG_ER
2897 fprintf(stderr, "\nER: Order\n");
2898 #endif /* DEBUG_ER */
2899 EP = barvinok_enumerate_e_with_options(para, exist, nparam,
2900 options);
2901 evalue *E;
2902 if (!emptyQ(pos)) {
2903 E = barvinok_enumerate_e_with_options(pos, exist, nparam,
2904 options);
2905 eadd(E, EP);
2906 free_evalue_refs(E);
2907 free(E);
2909 if (!emptyQ(neg)) {
2910 E = barvinok_enumerate_e_with_options(neg, exist, nparam,
2911 options);
2912 eadd(E, EP);
2913 free_evalue_refs(E);
2914 free(E);
2916 Polyhedron_Free(para);
2917 Polyhedron_Free(pos);
2918 Polyhedron_Free(neg);
2919 break;
2921 if (EP)
2922 break;
2923 } END_FORALL_PVertex_in_ParamPolyhedron;
2924 if (EP)
2925 break;
2926 } END_FORALL_PVertex_in_ParamPolyhedron;
2928 if (!EP) {
2929 /* Search for vertex coordinate to split on */
2930 /* First look for one independent of the parameters */
2931 FORALL_PVertex_in_ParamPolyhedron(V, last, PP) {
2932 for (int i = 0; i < exist; ++i) {
2933 int j;
2934 for (j = 0; j < nparam; ++j)
2935 if (value_notzero_p(V->Vertex->p[i][j]))
2936 break;
2937 if (j < nparam)
2938 continue;
2939 value_set_si(M->p[0][0], 1);
2940 Vector_Set(M->p[0]+1, 0, nvar+exist);
2941 Vector_Copy(V->Vertex->p[i],
2942 M->p[0] + 1 + nvar + exist, nparam+1);
2943 value_oppose(M->p[0][1+nvar+i],
2944 V->Vertex->p[i][nparam+1]);
2946 Polyhedron *pos, *neg;
2947 value_set_si(M->p[0][0], 1);
2948 value_decrement(M->p[0][P->Dimension+1],
2949 M->p[0][P->Dimension+1]);
2950 neg = AddConstraints(M->p[0], 1, P, options->MaxRays);
2951 value_set_si(f, -1);
2952 Vector_Scale(M->p[0]+1, M->p[0]+1, f,
2953 P->Dimension+1);
2954 value_decrement(M->p[0][P->Dimension+1],
2955 M->p[0][P->Dimension+1]);
2956 value_decrement(M->p[0][P->Dimension+1],
2957 M->p[0][P->Dimension+1]);
2958 pos = AddConstraints(M->p[0], 1, P, options->MaxRays);
2959 if (emptyQ(neg) || emptyQ(pos)) {
2960 Polyhedron_Free(pos);
2961 Polyhedron_Free(neg);
2962 continue;
2964 Polyhedron_Free(pos);
2965 value_increment(M->p[0][P->Dimension+1],
2966 M->p[0][P->Dimension+1]);
2967 pos = AddConstraints(M->p[0], 1, P, options->MaxRays);
2968 #ifdef DEBUG_ER
2969 fprintf(stderr, "\nER: Vertex\n");
2970 #endif /* DEBUG_ER */
2971 pos->next = neg;
2972 EP = enumerate_or(pos, exist, nparam, options);
2973 break;
2975 if (EP)
2976 break;
2977 } END_FORALL_PVertex_in_ParamPolyhedron;
2980 if (!EP) {
2981 /* Search for vertex coordinate to split on */
2982 /* Now look for one that depends on the parameters */
2983 FORALL_PVertex_in_ParamPolyhedron(V, last, PP) {
2984 for (int i = 0; i < exist; ++i) {
2985 value_set_si(M->p[0][0], 1);
2986 Vector_Set(M->p[0]+1, 0, nvar+exist);
2987 Vector_Copy(V->Vertex->p[i],
2988 M->p[0] + 1 + nvar + exist, nparam+1);
2989 value_oppose(M->p[0][1+nvar+i],
2990 V->Vertex->p[i][nparam+1]);
2992 Polyhedron *pos, *neg;
2993 value_set_si(M->p[0][0], 1);
2994 value_decrement(M->p[0][P->Dimension+1],
2995 M->p[0][P->Dimension+1]);
2996 neg = AddConstraints(M->p[0], 1, P, options->MaxRays);
2997 value_set_si(f, -1);
2998 Vector_Scale(M->p[0]+1, M->p[0]+1, f,
2999 P->Dimension+1);
3000 value_decrement(M->p[0][P->Dimension+1],
3001 M->p[0][P->Dimension+1]);
3002 value_decrement(M->p[0][P->Dimension+1],
3003 M->p[0][P->Dimension+1]);
3004 pos = AddConstraints(M->p[0], 1, P, options->MaxRays);
3005 if (emptyQ(neg) || emptyQ(pos)) {
3006 Polyhedron_Free(pos);
3007 Polyhedron_Free(neg);
3008 continue;
3010 Polyhedron_Free(pos);
3011 value_increment(M->p[0][P->Dimension+1],
3012 M->p[0][P->Dimension+1]);
3013 pos = AddConstraints(M->p[0], 1, P, options->MaxRays);
3014 #ifdef DEBUG_ER
3015 fprintf(stderr, "\nER: ParamVertex\n");
3016 #endif /* DEBUG_ER */
3017 pos->next = neg;
3018 EP = enumerate_or(pos, exist, nparam, options);
3019 break;
3021 if (EP)
3022 break;
3023 } END_FORALL_PVertex_in_ParamPolyhedron;
3026 Matrix_Free(M);
3027 value_clear(f);
3030 if (CEq)
3031 Polyhedron_Free(CEq);
3032 if (CT)
3033 Matrix_Free(CT);
3034 if (PP)
3035 Param_Polyhedron_Free(PP);
3036 *PA = P;
3038 return EP;
3041 evalue* barvinok_enumerate_pip(Polyhedron *P, unsigned exist, unsigned nparam,
3042 unsigned MaxRays)
3044 evalue *E;
3045 barvinok_options *options = barvinok_options_new_with_defaults();
3046 options->MaxRays = MaxRays;
3047 E = barvinok_enumerate_pip_with_options(P, exist, nparam, options);
3048 barvinok_options_free(options);
3049 return E;
3052 #ifndef HAVE_PIPLIB
3053 evalue *barvinok_enumerate_pip_with_options(Polyhedron *P,
3054 unsigned exist, unsigned nparam, struct barvinok_options *options)
3056 return 0;
3058 #else
3059 evalue *barvinok_enumerate_pip_with_options(Polyhedron *P,
3060 unsigned exist, unsigned nparam, struct barvinok_options *options)
3062 int nvar = P->Dimension - exist - nparam;
3063 evalue *EP = evalue_zero();
3064 Polyhedron *Q, *N;
3066 #ifdef DEBUG_ER
3067 fprintf(stderr, "\nER: PIP\n");
3068 #endif /* DEBUG_ER */
3070 Polyhedron *D = pip_projectout(P, nvar, exist, nparam);
3071 for (Q = D; Q; Q = N) {
3072 N = Q->next;
3073 Q->next = 0;
3074 evalue *E;
3075 exist = Q->Dimension - nvar - nparam;
3076 E = barvinok_enumerate_e_with_options(Q, exist, nparam, options);
3077 Polyhedron_Free(Q);
3078 eadd(E, EP);
3079 free_evalue_refs(E);
3080 free(E);
3083 return EP;
3085 #endif
3088 static bool is_single(Value *row, int pos, int len)
3090 return First_Non_Zero(row, pos) == -1 &&
3091 First_Non_Zero(row+pos+1, len-pos-1) == -1;
3094 static evalue* barvinok_enumerate_e_r(Polyhedron *P,
3095 unsigned exist, unsigned nparam, barvinok_options *options);
3097 #ifdef DEBUG_ER
3098 static int er_level = 0;
3100 evalue* barvinok_enumerate_e_with_options(Polyhedron *P,
3101 unsigned exist, unsigned nparam, barvinok_options *options)
3103 fprintf(stderr, "\nER: level %i\n", er_level);
3105 Polyhedron_PrintConstraints(stderr, P_VALUE_FMT, P);
3106 fprintf(stderr, "\nE %d\nP %d\n", exist, nparam);
3107 ++er_level;
3108 P = DomainConstraintSimplify(Polyhedron_Copy(P), options->MaxRays);
3109 evalue *EP = barvinok_enumerate_e_r(P, exist, nparam, options);
3110 Polyhedron_Free(P);
3111 --er_level;
3112 return EP;
3114 #else
3115 evalue* barvinok_enumerate_e_with_options(Polyhedron *P,
3116 unsigned exist, unsigned nparam, barvinok_options *options)
3118 P = DomainConstraintSimplify(Polyhedron_Copy(P), options->MaxRays);
3119 evalue *EP = barvinok_enumerate_e_r(P, exist, nparam, options);
3120 Polyhedron_Free(P);
3121 return EP;
3123 #endif
3125 evalue* barvinok_enumerate_e(Polyhedron *P, unsigned exist, unsigned nparam,
3126 unsigned MaxRays)
3128 evalue *E;
3129 barvinok_options *options = barvinok_options_new_with_defaults();
3130 options->MaxRays = MaxRays;
3131 E = barvinok_enumerate_e_with_options(P, exist, nparam, options);
3132 barvinok_options_free(options);
3133 return E;
3136 static evalue* barvinok_enumerate_e_r(Polyhedron *P,
3137 unsigned exist, unsigned nparam, barvinok_options *options)
3139 if (exist == 0) {
3140 Polyhedron *U = Universe_Polyhedron(nparam);
3141 evalue *EP = barvinok_enumerate_with_options(P, U, options);
3142 //char *param_name[] = {"P", "Q", "R", "S", "T" };
3143 //print_evalue(stdout, EP, param_name);
3144 Polyhedron_Free(U);
3145 return EP;
3148 int nvar = P->Dimension - exist - nparam;
3149 int len = P->Dimension + 2;
3151 /* for now */
3152 POL_ENSURE_FACETS(P);
3153 POL_ENSURE_VERTICES(P);
3155 if (emptyQ(P))
3156 return evalue_zero();
3158 if (nvar == 0 && nparam == 0) {
3159 evalue *EP = evalue_zero();
3160 barvinok_count_with_options(P, &EP->x.n, options);
3161 if (value_pos_p(EP->x.n))
3162 value_set_si(EP->x.n, 1);
3163 return EP;
3166 int r;
3167 for (r = 0; r < P->NbRays; ++r)
3168 if (value_zero_p(P->Ray[r][0]) ||
3169 value_zero_p(P->Ray[r][P->Dimension+1])) {
3170 int i;
3171 for (i = 0; i < nvar; ++i)
3172 if (value_notzero_p(P->Ray[r][i+1]))
3173 break;
3174 if (i >= nvar)
3175 continue;
3176 for (i = nvar + exist; i < nvar + exist + nparam; ++i)
3177 if (value_notzero_p(P->Ray[r][i+1]))
3178 break;
3179 if (i >= nvar + exist + nparam)
3180 break;
3182 if (r < P->NbRays) {
3183 evalue *EP = evalue_zero();
3184 value_set_si(EP->x.n, -1);
3185 return EP;
3188 int first;
3189 for (r = 0; r < P->NbEq; ++r)
3190 if ((first = First_Non_Zero(P->Constraint[r]+1+nvar, exist)) != -1)
3191 break;
3192 if (r < P->NbEq) {
3193 if (First_Non_Zero(P->Constraint[r]+1+nvar+first+1,
3194 exist-first-1) != -1) {
3195 Polyhedron *T = rotate_along(P, r, nvar, exist, options->MaxRays);
3196 #ifdef DEBUG_ER
3197 fprintf(stderr, "\nER: Equality\n");
3198 #endif /* DEBUG_ER */
3199 evalue *EP = barvinok_enumerate_e_with_options(T, exist-1, nparam,
3200 options);
3201 Polyhedron_Free(T);
3202 return EP;
3203 } else {
3204 #ifdef DEBUG_ER
3205 fprintf(stderr, "\nER: Fixed\n");
3206 #endif /* DEBUG_ER */
3207 if (first == 0)
3208 return barvinok_enumerate_e_with_options(P, exist-1, nparam,
3209 options);
3210 else {
3211 Polyhedron *T = Polyhedron_Copy(P);
3212 SwapColumns(T, nvar+1, nvar+1+first);
3213 evalue *EP = barvinok_enumerate_e_with_options(T, exist-1, nparam,
3214 options);
3215 Polyhedron_Free(T);
3216 return EP;
3221 Vector *row = Vector_Alloc(len);
3222 value_set_si(row->p[0], 1);
3224 Value f;
3225 value_init(f);
3227 enum constraint* info = new constraint[exist];
3228 for (int i = 0; i < exist; ++i) {
3229 info[i] = ALL_POS;
3230 for (int l = P->NbEq; l < P->NbConstraints; ++l) {
3231 if (value_negz_p(P->Constraint[l][nvar+i+1]))
3232 continue;
3233 bool l_parallel = is_single(P->Constraint[l]+nvar+1, i, exist);
3234 for (int u = P->NbEq; u < P->NbConstraints; ++u) {
3235 if (value_posz_p(P->Constraint[u][nvar+i+1]))
3236 continue;
3237 bool lu_parallel = l_parallel ||
3238 is_single(P->Constraint[u]+nvar+1, i, exist);
3239 value_oppose(f, P->Constraint[u][nvar+i+1]);
3240 Vector_Combine(P->Constraint[l]+1, P->Constraint[u]+1, row->p+1,
3241 f, P->Constraint[l][nvar+i+1], len-1);
3242 if (!(info[i] & INDEPENDENT)) {
3243 int j;
3244 for (j = 0; j < exist; ++j)
3245 if (j != i && value_notzero_p(row->p[nvar+j+1]))
3246 break;
3247 if (j == exist) {
3248 //printf("independent: i: %d, l: %d, u: %d\n", i, l, u);
3249 info[i] = (constraint)(info[i] | INDEPENDENT);
3252 if (info[i] & ALL_POS) {
3253 value_addto(row->p[len-1], row->p[len-1],
3254 P->Constraint[l][nvar+i+1]);
3255 value_addto(row->p[len-1], row->p[len-1], f);
3256 value_multiply(f, f, P->Constraint[l][nvar+i+1]);
3257 value_subtract(row->p[len-1], row->p[len-1], f);
3258 value_decrement(row->p[len-1], row->p[len-1]);
3259 ConstraintSimplify(row->p, row->p, len, &f);
3260 value_set_si(f, -1);
3261 Vector_Scale(row->p+1, row->p+1, f, len-1);
3262 value_decrement(row->p[len-1], row->p[len-1]);
3263 Polyhedron *T = AddConstraints(row->p, 1, P, options->MaxRays);
3264 if (!emptyQ(T)) {
3265 //printf("not all_pos: i: %d, l: %d, u: %d\n", i, l, u);
3266 info[i] = (constraint)(info[i] ^ ALL_POS);
3268 //puts("pos remainder");
3269 //Polyhedron_Print(stdout, P_VALUE_FMT, T);
3270 Polyhedron_Free(T);
3272 if (!(info[i] & ONE_NEG)) {
3273 if (lu_parallel) {
3274 negative_test_constraint(P->Constraint[l],
3275 P->Constraint[u],
3276 row->p, nvar+i, len, &f);
3277 oppose_constraint(row->p, len, &f);
3278 Polyhedron *T = AddConstraints(row->p, 1, P,
3279 options->MaxRays);
3280 if (emptyQ(T)) {
3281 //printf("one_neg i: %d, l: %d, u: %d\n", i, l, u);
3282 info[i] = (constraint)(info[i] | ONE_NEG);
3284 //puts("neg remainder");
3285 //Polyhedron_Print(stdout, P_VALUE_FMT, T);
3286 Polyhedron_Free(T);
3287 } else if (!(info[i] & ROT_NEG)) {
3288 if (parallel_constraints(P->Constraint[l],
3289 P->Constraint[u],
3290 row->p, nvar, exist)) {
3291 negative_test_constraint7(P->Constraint[l],
3292 P->Constraint[u],
3293 row->p, nvar, exist,
3294 len, &f);
3295 oppose_constraint(row->p, len, &f);
3296 Polyhedron *T = AddConstraints(row->p, 1, P,
3297 options->MaxRays);
3298 if (emptyQ(T)) {
3299 // printf("rot_neg i: %d, l: %d, u: %d\n", i, l, u);
3300 info[i] = (constraint)(info[i] | ROT_NEG);
3301 r = l;
3303 //puts("neg remainder");
3304 //Polyhedron_Print(stdout, P_VALUE_FMT, T);
3305 Polyhedron_Free(T);
3309 if (!(info[i] & ALL_POS) && (info[i] & (ONE_NEG | ROT_NEG)))
3310 goto next;
3313 if (info[i] & ALL_POS)
3314 break;
3315 next:
3320 for (int i = 0; i < exist; ++i)
3321 printf("%i: %i\n", i, info[i]);
3323 for (int i = 0; i < exist; ++i)
3324 if (info[i] & ALL_POS) {
3325 #ifdef DEBUG_ER
3326 fprintf(stderr, "\nER: Positive\n");
3327 #endif /* DEBUG_ER */
3328 // Eliminate
3329 // Maybe we should chew off some of the fat here
3330 Matrix *M = Matrix_Alloc(P->Dimension, P->Dimension+1);
3331 for (int j = 0; j < P->Dimension; ++j)
3332 value_set_si(M->p[j][j + (j >= i+nvar)], 1);
3333 Polyhedron *T = Polyhedron_Image(P, M, options->MaxRays);
3334 Matrix_Free(M);
3335 evalue *EP = barvinok_enumerate_e_with_options(T, exist-1, nparam,
3336 options);
3337 Polyhedron_Free(T);
3338 value_clear(f);
3339 Vector_Free(row);
3340 delete [] info;
3341 return EP;
3343 for (int i = 0; i < exist; ++i)
3344 if (info[i] & ONE_NEG) {
3345 #ifdef DEBUG_ER
3346 fprintf(stderr, "\nER: Negative\n");
3347 #endif /* DEBUG_ER */
3348 Vector_Free(row);
3349 value_clear(f);
3350 delete [] info;
3351 if (i == 0)
3352 return barvinok_enumerate_e_with_options(P, exist-1, nparam,
3353 options);
3354 else {
3355 Polyhedron *T = Polyhedron_Copy(P);
3356 SwapColumns(T, nvar+1, nvar+1+i);
3357 evalue *EP = barvinok_enumerate_e_with_options(T, exist-1, nparam,
3358 options);
3359 Polyhedron_Free(T);
3360 return EP;
3363 for (int i = 0; i < exist; ++i)
3364 if (info[i] & ROT_NEG) {
3365 #ifdef DEBUG_ER
3366 fprintf(stderr, "\nER: Rotate\n");
3367 #endif /* DEBUG_ER */
3368 Vector_Free(row);
3369 value_clear(f);
3370 delete [] info;
3371 Polyhedron *T = rotate_along(P, r, nvar, exist, options->MaxRays);
3372 evalue *EP = barvinok_enumerate_e_with_options(T, exist-1, nparam,
3373 options);
3374 Polyhedron_Free(T);
3375 return EP;
3377 for (int i = 0; i < exist; ++i)
3378 if (info[i] & INDEPENDENT) {
3379 Polyhedron *pos, *neg;
3381 /* Find constraint again and split off negative part */
3383 if (SplitOnVar(P, i, nvar, exist, options->MaxRays,
3384 row, f, true, &pos, &neg)) {
3385 #ifdef DEBUG_ER
3386 fprintf(stderr, "\nER: Split\n");
3387 #endif /* DEBUG_ER */
3389 evalue *EP =
3390 barvinok_enumerate_e_with_options(neg, exist-1, nparam, options);
3391 evalue *E =
3392 barvinok_enumerate_e_with_options(pos, exist, nparam, options);
3393 eadd(E, EP);
3394 free_evalue_refs(E);
3395 free(E);
3396 Polyhedron_Free(neg);
3397 Polyhedron_Free(pos);
3398 value_clear(f);
3399 Vector_Free(row);
3400 delete [] info;
3401 return EP;
3404 delete [] info;
3406 Polyhedron *O = P;
3407 Polyhedron *F;
3409 evalue *EP;
3411 EP = enumerate_line(P, exist, nparam, options);
3412 if (EP)
3413 goto out;
3415 EP = barvinok_enumerate_pip_with_options(P, exist, nparam, options);
3416 if (EP)
3417 goto out;
3419 EP = enumerate_redundant_ray(P, exist, nparam, options);
3420 if (EP)
3421 goto out;
3423 EP = enumerate_sure(P, exist, nparam, options);
3424 if (EP)
3425 goto out;
3427 EP = enumerate_ray(P, exist, nparam, options);
3428 if (EP)
3429 goto out;
3431 EP = enumerate_sure2(P, exist, nparam, options);
3432 if (EP)
3433 goto out;
3435 F = unfringe(P, options->MaxRays);
3436 if (!PolyhedronIncludes(F, P)) {
3437 #ifdef DEBUG_ER
3438 fprintf(stderr, "\nER: Fringed\n");
3439 #endif /* DEBUG_ER */
3440 EP = barvinok_enumerate_e_with_options(F, exist, nparam, options);
3441 Polyhedron_Free(F);
3442 goto out;
3444 Polyhedron_Free(F);
3446 if (nparam)
3447 EP = enumerate_vd(&P, exist, nparam, options);
3448 if (EP)
3449 goto out2;
3451 if (nvar != 0) {
3452 EP = enumerate_sum(P, exist, nparam, options);
3453 goto out2;
3456 assert(nvar == 0);
3458 int i;
3459 Polyhedron *pos, *neg;
3460 for (i = 0; i < exist; ++i)
3461 if (SplitOnVar(P, i, nvar, exist, options->MaxRays,
3462 row, f, false, &pos, &neg))
3463 break;
3465 assert (i < exist);
3467 pos->next = neg;
3468 EP = enumerate_or(pos, exist, nparam, options);
3470 out2:
3471 if (O != P)
3472 Polyhedron_Free(P);
3474 out:
3475 value_clear(f);
3476 Vector_Free(row);
3477 return EP;
3481 * remove equalities that require a "compression" of the parameters
3483 static Polyhedron *remove_more_equalities(Polyhedron *P, unsigned nparam,
3484 Matrix **CP, unsigned MaxRays)
3486 Polyhedron *Q = P;
3487 remove_all_equalities(&P, NULL, CP, NULL, nparam, MaxRays);
3488 if (P != Q)
3489 Polyhedron_Free(Q);
3490 return P;
3493 /* frees P */
3494 static gen_fun *series(Polyhedron *P, unsigned nparam, barvinok_options *options)
3496 Matrix *CP = NULL;
3497 gen_fun *gf;
3499 if (emptyQ2(P)) {
3500 Polyhedron_Free(P);
3501 return new gen_fun;
3504 assert(!Polyhedron_is_infinite_param(P, nparam));
3505 assert(P->NbBid == 0);
3506 assert(Polyhedron_has_revlex_positive_rays(P, nparam));
3507 if (P->NbEq != 0)
3508 P = remove_more_equalities(P, nparam, &CP, options->MaxRays);
3509 assert(P->NbEq == 0);
3510 if (CP)
3511 nparam = CP->NbColumns-1;
3513 if (nparam == 0) {
3514 Value c;
3515 value_init(c);
3516 barvinok_count_with_options(P, &c, options);
3517 gf = new gen_fun(c);
3518 value_clear(c);
3519 } else {
3520 gf_base *red;
3521 red = gf_base::create(Polyhedron_Project(P, nparam),
3522 P->Dimension, nparam, options);
3523 POL_ENSURE_VERTICES(P);
3524 red->start_gf(P, options);
3525 gf = red->gf;
3526 delete red;
3528 if (CP) {
3529 gf->substitute(CP);
3530 Matrix_Free(CP);
3532 Polyhedron_Free(P);
3533 return gf;
3536 gen_fun * barvinok_series_with_options(Polyhedron *P, Polyhedron* C,
3537 barvinok_options *options)
3539 Polyhedron *CA;
3540 unsigned nparam = C->Dimension;
3541 gen_fun *gf;
3543 CA = align_context(C, P->Dimension, options->MaxRays);
3544 P = DomainIntersection(P, CA, options->MaxRays);
3545 Polyhedron_Free(CA);
3547 gf = series(P, nparam, options);
3549 return gf;
3552 gen_fun * barvinok_series(Polyhedron *P, Polyhedron* C, unsigned MaxRays)
3554 gen_fun *gf;
3555 barvinok_options *options = barvinok_options_new_with_defaults();
3556 options->MaxRays = MaxRays;
3557 gf = barvinok_series_with_options(P, C, options);
3558 barvinok_options_free(options);
3559 return gf;
3562 static Polyhedron *skew_into_positive_orthant(Polyhedron *D, unsigned nparam,
3563 unsigned MaxRays)
3565 Matrix *M = NULL;
3566 Value tmp;
3567 value_init(tmp);
3568 for (Polyhedron *P = D; P; P = P->next) {
3569 POL_ENSURE_VERTICES(P);
3570 assert(!Polyhedron_is_infinite_param(P, nparam));
3571 assert(P->NbBid == 0);
3572 assert(Polyhedron_has_positive_rays(P, nparam));
3574 for (int r = 0; r < P->NbRays; ++r) {
3575 if (value_notzero_p(P->Ray[r][P->Dimension+1]))
3576 continue;
3577 for (int i = 0; i < nparam; ++i) {
3578 int j;
3579 if (value_posz_p(P->Ray[r][i+1]))
3580 continue;
3581 if (!M) {
3582 M = Matrix_Alloc(D->Dimension+1, D->Dimension+1);
3583 for (int i = 0; i < D->Dimension+1; ++i)
3584 value_set_si(M->p[i][i], 1);
3585 } else {
3586 Inner_Product(P->Ray[r]+1, M->p[i], D->Dimension+1, &tmp);
3587 if (value_posz_p(tmp))
3588 continue;
3590 for (j = P->Dimension - nparam; j < P->Dimension; ++j)
3591 if (value_pos_p(P->Ray[r][j+1]))
3592 break;
3593 assert(j < P->Dimension);
3594 value_pdivision(tmp, P->Ray[r][j+1], P->Ray[r][i+1]);
3595 value_subtract(M->p[i][j], M->p[i][j], tmp);
3599 value_clear(tmp);
3600 if (M) {
3601 D = DomainImage(D, M, MaxRays);
3602 Matrix_Free(M);
3604 return D;
3607 gen_fun* barvinok_enumerate_union_series_with_options(Polyhedron *D, Polyhedron* C,
3608 barvinok_options *options)
3610 Polyhedron *conv, *D2;
3611 Polyhedron *CA;
3612 gen_fun *gf = NULL, *gf2;
3613 unsigned nparam = C->Dimension;
3614 ZZ one, mone;
3615 one = 1;
3616 mone = -1;
3618 CA = align_context(C, D->Dimension, options->MaxRays);
3619 D = DomainIntersection(D, CA, options->MaxRays);
3620 Polyhedron_Free(CA);
3622 D2 = skew_into_positive_orthant(D, nparam, options->MaxRays);
3623 for (Polyhedron *P = D2; P; P = P->next) {
3624 assert(P->Dimension == D2->Dimension);
3625 gen_fun *P_gf;
3627 P_gf = series(Polyhedron_Copy(P), nparam, options);
3628 if (!gf)
3629 gf = P_gf;
3630 else {
3631 gf->add_union(P_gf, options);
3632 delete P_gf;
3635 /* we actually only need the convex union of the parameter space
3636 * but the reducer classes currently expect a polyhedron in
3637 * the combined space
3639 Polyhedron_Free(gf->context);
3640 gf->context = DomainConvex(D2, options->MaxRays);
3642 gf2 = gf->summate(D2->Dimension - nparam, options);
3644 delete gf;
3645 if (D != D2)
3646 Domain_Free(D2);
3647 Domain_Free(D);
3648 return gf2;
3651 gen_fun* barvinok_enumerate_union_series(Polyhedron *D, Polyhedron* C,
3652 unsigned MaxRays)
3654 gen_fun *gf;
3655 barvinok_options *options = barvinok_options_new_with_defaults();
3656 options->MaxRays = MaxRays;
3657 gf = barvinok_enumerate_union_series_with_options(D, C, options);
3658 barvinok_options_free(options);
3659 return gf;
3662 evalue* barvinok_enumerate_union(Polyhedron *D, Polyhedron* C, unsigned MaxRays)
3664 evalue *EP;
3665 gen_fun *gf = barvinok_enumerate_union_series(D, C, MaxRays);
3666 EP = *gf;
3667 delete gf;
3668 return EP;