scale.c: Param_Polyhedron_Scale_Integer_Fast: ignore constants
[barvinok.git] / barvinok.cc
blobd8213332605c6ae64b326bc3a93811559acc0fa8
1 #include <assert.h>
2 #include <iostream>
3 #include <vector>
4 #include <deque>
5 #include <string>
6 #include <sstream>
7 #include <gmp.h>
8 #include <NTL/mat_ZZ.h>
9 #include <NTL/LLL.h>
10 #include <barvinok/util.h>
11 #include <barvinok/evalue.h>
12 extern "C" {
13 #include "piputil.h"
15 #include "config.h"
16 #include <barvinok/barvinok.h>
17 #include <barvinok/genfun.h>
18 #include <barvinok/options.h>
19 #include <barvinok/sample.h>
20 #include "conversion.h"
21 #include "decomposer.h"
22 #include "lattice_point.h"
23 #include "reduce_domain.h"
24 #include "genfun_constructor.h"
25 #include "remove_equalities.h"
26 #include "scale.h"
28 #ifdef NTL_STD_CXX
29 using namespace NTL;
30 #endif
31 using std::cerr;
32 using std::cout;
33 using std::endl;
34 using std::vector;
35 using std::deque;
36 using std::string;
37 using std::ostringstream;
39 #define ALLOC(t,p) p = (t*)malloc(sizeof(*p))
41 class dpoly_n {
42 public:
43 Matrix *coeff;
44 ~dpoly_n() {
45 Matrix_Free(coeff);
47 dpoly_n(int d, ZZ& degree_0, ZZ& degree_1, int offset = 0) {
48 Value d0, d1;
49 value_init(d0);
50 value_init(d1);
51 zz2value(degree_0, d0);
52 zz2value(degree_1, d1);
53 coeff = Matrix_Alloc(d+1, d+1+1);
54 value_set_si(coeff->p[0][0], 1);
55 value_set_si(coeff->p[0][d+1], 1);
56 for (int i = 1; i <= d; ++i) {
57 value_multiply(coeff->p[i][0], coeff->p[i-1][0], d0);
58 Vector_Combine(coeff->p[i-1], coeff->p[i-1]+1, coeff->p[i]+1,
59 d1, d0, i);
60 value_set_si(coeff->p[i][d+1], i);
61 value_multiply(coeff->p[i][d+1], coeff->p[i][d+1], coeff->p[i-1][d+1]);
62 value_decrement(d0, d0);
64 value_clear(d0);
65 value_clear(d1);
67 void div(dpoly& d, Vector *count, ZZ& sign) {
68 int len = coeff->NbRows;
69 Matrix * c = Matrix_Alloc(coeff->NbRows, coeff->NbColumns);
70 Value tmp;
71 value_init(tmp);
72 for (int i = 0; i < len; ++i) {
73 Vector_Copy(coeff->p[i], c->p[i], len+1);
74 for (int j = 1; j <= i; ++j) {
75 zz2value(d.coeff[j], tmp);
76 value_multiply(tmp, tmp, c->p[i][len]);
77 value_oppose(tmp, tmp);
78 Vector_Combine(c->p[i], c->p[i-j], c->p[i],
79 c->p[i-j][len], tmp, len);
80 value_multiply(c->p[i][len], c->p[i][len], c->p[i-j][len]);
82 zz2value(d.coeff[0], tmp);
83 value_multiply(c->p[i][len], c->p[i][len], tmp);
85 if (sign == -1) {
86 value_set_si(tmp, -1);
87 Vector_Scale(c->p[len-1], count->p, tmp, len);
88 value_assign(count->p[len], c->p[len-1][len]);
89 } else
90 Vector_Copy(c->p[len-1], count->p, len+1);
91 Vector_Normalize(count->p, len+1);
92 value_clear(tmp);
93 Matrix_Free(c);
97 const int MAX_TRY=10;
99 * Searches for a vector that is not orthogonal to any
100 * of the rays in rays.
102 static void nonorthog(mat_ZZ& rays, vec_ZZ& lambda)
104 int dim = rays.NumCols();
105 bool found = false;
106 lambda.SetLength(dim);
107 if (dim == 0)
108 return;
110 for (int i = 2; !found && i <= 50*dim; i+=4) {
111 for (int j = 0; j < MAX_TRY; ++j) {
112 for (int k = 0; k < dim; ++k) {
113 int r = random_int(i)+2;
114 int v = (2*(r%2)-1) * (r >> 1);
115 lambda[k] = v;
117 int k = 0;
118 for (; k < rays.NumRows(); ++k)
119 if (lambda * rays[k] == 0)
120 break;
121 if (k == rays.NumRows()) {
122 found = true;
123 break;
127 assert(found);
130 static void add_rays(mat_ZZ& rays, Polyhedron *i, int *r, int nvar = -1,
131 bool all = false)
133 unsigned dim = i->Dimension;
134 if (nvar == -1)
135 nvar = dim;
136 for (int k = 0; k < i->NbRays; ++k) {
137 if (!value_zero_p(i->Ray[k][dim+1]))
138 continue;
139 if (!all && nvar != dim && First_Non_Zero(i->Ray[k]+1, nvar) == -1)
140 continue;
141 values2zz(i->Ray[k]+1, rays[(*r)++], nvar);
145 static void mask_r(Matrix *f, int nr, Vector *lcm, int p, Vector *val, evalue *ev)
147 unsigned nparam = lcm->Size;
149 if (p == nparam) {
150 Vector * prod = Vector_Alloc(f->NbRows);
151 Matrix_Vector_Product(f, val->p, prod->p);
152 int isint = 1;
153 for (int i = 0; i < nr; ++i) {
154 value_modulus(prod->p[i], prod->p[i], f->p[i][nparam+1]);
155 isint &= value_zero_p(prod->p[i]);
157 value_set_si(ev->d, 1);
158 value_init(ev->x.n);
159 value_set_si(ev->x.n, isint);
160 Vector_Free(prod);
161 return;
164 Value tmp;
165 value_init(tmp);
166 if (value_one_p(lcm->p[p]))
167 mask_r(f, nr, lcm, p+1, val, ev);
168 else {
169 value_assign(tmp, lcm->p[p]);
170 value_set_si(ev->d, 0);
171 ev->x.p = new_enode(periodic, VALUE_TO_INT(tmp), p+1);
172 do {
173 value_decrement(tmp, tmp);
174 value_assign(val->p[p], tmp);
175 mask_r(f, nr, lcm, p+1, val, &ev->x.p->arr[VALUE_TO_INT(tmp)]);
176 } while (value_pos_p(tmp));
178 value_clear(tmp);
181 static void mask_fractional(Matrix *f, evalue *factor)
183 int nr = f->NbRows, nc = f->NbColumns;
184 int n;
185 bool found = false;
186 for (n = 0; n < nr && value_notzero_p(f->p[n][nc-1]); ++n)
187 if (value_notone_p(f->p[n][nc-1]) &&
188 value_notmone_p(f->p[n][nc-1]))
189 found = true;
190 if (!found)
191 return;
193 evalue EP;
194 nr = n;
196 Value m;
197 value_init(m);
199 evalue EV;
200 value_init(EV.d);
201 value_init(EV.x.n);
202 value_set_si(EV.x.n, 1);
204 for (n = 0; n < nr; ++n) {
205 value_assign(m, f->p[n][nc-1]);
206 if (value_one_p(m) || value_mone_p(m))
207 continue;
209 int j = normal_mod(f->p[n], nc-1, &m);
210 if (j == nc-1) {
211 free_evalue_refs(factor);
212 value_init(factor->d);
213 evalue_set_si(factor, 0, 1);
214 break;
216 vec_ZZ row;
217 values2zz(f->p[n], row, nc-1);
218 ZZ g;
219 value2zz(m, g);
220 if (j < (nc-1)-1 && row[j] > g/2) {
221 for (int k = j; k < (nc-1); ++k)
222 if (row[k] != 0)
223 row[k] = g - row[k];
226 value_init(EP.d);
227 value_set_si(EP.d, 0);
228 EP.x.p = new_enode(relation, 2, 0);
229 value_clear(EP.x.p->arr[1].d);
230 EP.x.p->arr[1] = *factor;
231 evalue *ev = &EP.x.p->arr[0];
232 value_set_si(ev->d, 0);
233 ev->x.p = new_enode(fractional, 3, -1);
234 evalue_set_si(&ev->x.p->arr[1], 0, 1);
235 evalue_set_si(&ev->x.p->arr[2], 1, 1);
236 evalue *E = multi_monom(row);
237 value_assign(EV.d, m);
238 emul(&EV, E);
239 value_clear(ev->x.p->arr[0].d);
240 ev->x.p->arr[0] = *E;
241 delete E;
242 *factor = EP;
245 value_clear(m);
246 free_evalue_refs(&EV);
252 static void mask_table(Matrix *f, evalue *factor)
254 int nr = f->NbRows, nc = f->NbColumns;
255 int n;
256 bool found = false;
257 for (n = 0; n < nr && value_notzero_p(f->p[n][nc-1]); ++n)
258 if (value_notone_p(f->p[n][nc-1]) &&
259 value_notmone_p(f->p[n][nc-1]))
260 found = true;
261 if (!found)
262 return;
264 Value tmp;
265 value_init(tmp);
266 nr = n;
267 unsigned np = nc - 2;
268 Vector *lcm = Vector_Alloc(np);
269 Vector *val = Vector_Alloc(nc);
270 Vector_Set(val->p, 0, nc);
271 value_set_si(val->p[np], 1);
272 Vector_Set(lcm->p, 1, np);
273 for (n = 0; n < nr; ++n) {
274 if (value_one_p(f->p[n][nc-1]) ||
275 value_mone_p(f->p[n][nc-1]))
276 continue;
277 for (int j = 0; j < np; ++j)
278 if (value_notzero_p(f->p[n][j])) {
279 Gcd(f->p[n][j], f->p[n][nc-1], &tmp);
280 value_division(tmp, f->p[n][nc-1], tmp);
281 value_lcm(tmp, lcm->p[j], &lcm->p[j]);
284 evalue EP;
285 value_init(EP.d);
286 mask_r(f, nr, lcm, 0, val, &EP);
287 value_clear(tmp);
288 Vector_Free(val);
289 Vector_Free(lcm);
290 emul(&EP,factor);
291 free_evalue_refs(&EP);
294 static void mask(Matrix *f, evalue *factor, barvinok_options *options)
296 if (options->lookup_table)
297 mask_table(f, factor);
298 else
299 mask_fractional(f, factor);
302 /* This structure encodes the power of the term in a rational generating function.
304 * Either E == NULL or constant = 0
305 * If E != NULL, then the power is E
306 * If E == NULL, then the power is coeff * param[pos] + constant
308 struct term_info {
309 evalue *E;
310 ZZ constant;
311 ZZ coeff;
312 int pos;
315 /* Returns the power of (t+1) in the term of a rational generating function,
316 * i.e., the scalar product of the actual lattice point and lambda.
317 * The lattice point is the unique lattice point in the fundamental parallelepiped
318 * of the unimodual cone i shifted to the parametric vertex V.
320 * PD is the parameter domain, which, if != NULL, may be used to simply the
321 * resulting expression.
323 * The result is returned in term.
325 void lattice_point(Param_Vertices* V, const mat_ZZ& rays, vec_ZZ& lambda,
326 term_info* term, Polyhedron *PD, barvinok_options *options)
328 unsigned nparam = V->Vertex->NbColumns - 2;
329 unsigned dim = rays.NumCols();
330 mat_ZZ vertex;
331 vertex.SetDims(V->Vertex->NbRows, nparam+1);
332 Value lcm, tmp;
333 value_init(lcm);
334 value_init(tmp);
335 value_set_si(lcm, 1);
336 for (int j = 0; j < V->Vertex->NbRows; ++j) {
337 value_lcm(lcm, V->Vertex->p[j][nparam+1], &lcm);
339 if (value_notone_p(lcm)) {
340 Matrix * mv = Matrix_Alloc(dim, nparam+1);
341 for (int j = 0 ; j < dim; ++j) {
342 value_division(tmp, lcm, V->Vertex->p[j][nparam+1]);
343 Vector_Scale(V->Vertex->p[j], mv->p[j], tmp, nparam+1);
346 term->E = lattice_point(rays, lambda, mv, lcm, PD, options);
347 term->constant = 0;
349 Matrix_Free(mv);
350 value_clear(lcm);
351 value_clear(tmp);
352 return;
354 for (int i = 0; i < V->Vertex->NbRows; ++i) {
355 assert(value_one_p(V->Vertex->p[i][nparam+1])); // for now
356 values2zz(V->Vertex->p[i], vertex[i], nparam+1);
359 vec_ZZ num;
360 num = lambda * vertex;
362 int p = -1;
363 int nn = 0;
364 for (int j = 0; j < nparam; ++j)
365 if (num[j] != 0) {
366 ++nn;
367 p = j;
369 if (nn >= 2) {
370 term->E = multi_monom(num);
371 term->constant = 0;
372 } else {
373 term->E = NULL;
374 term->constant = num[nparam];
375 term->pos = p;
376 if (p != -1)
377 term->coeff = num[p];
380 value_clear(lcm);
381 value_clear(tmp);
385 struct counter : public np_base {
386 vec_ZZ lambda;
387 mat_ZZ vertex;
388 vec_ZZ den;
389 ZZ sign;
390 vec_ZZ num;
391 ZZ offset;
392 int j;
393 mpq_t count;
395 counter(unsigned dim) : np_base(dim) {
396 den.SetLength(dim);
397 mpq_init(count);
400 virtual void init(Polyhedron *P) {
401 randomvector(P, lambda, dim);
404 virtual void reset() {
405 mpq_set_si(count, 0, 0);
408 ~counter() {
409 mpq_clear(count);
412 virtual void handle(const mat_ZZ& rays, Value *vertex, const QQ& c,
413 unsigned long det, int *closed, barvinok_options *options);
414 virtual void get_count(Value *result) {
415 assert(value_one_p(&count[0]._mp_den));
416 value_assign(*result, &count[0]._mp_num);
420 void counter::handle(const mat_ZZ& rays, Value *V, const QQ& c, unsigned long det,
421 int *closed, barvinok_options *options)
423 for (int k = 0; k < dim; ++k) {
424 if (lambda * rays[k] == 0)
425 throw Orthogonal;
428 assert(c.d == 1);
429 assert(c.n == 1 || c.n == -1);
430 sign = c.n;
432 lattice_point(V, rays, vertex, det, closed);
433 num = vertex * lambda;
434 den = rays * lambda;
435 offset = 0;
436 normalize(sign, offset, den);
438 num[0] += offset;
439 dpoly d(dim, num[0]);
440 for (int k = 1; k < num.length(); ++k) {
441 num[k] += offset;
442 dpoly term(dim, num[k]);
443 d += term;
445 dpoly n(dim, den[0], 1);
446 for (int k = 1; k < dim; ++k) {
447 dpoly fact(dim, den[k], 1);
448 n *= fact;
450 d.div(n, count, sign);
453 struct bfe_term : public bfc_term_base {
454 vector<evalue *> factors;
456 bfe_term(int len) : bfc_term_base(len) {
459 ~bfe_term() {
460 for (int i = 0; i < factors.size(); ++i) {
461 if (!factors[i])
462 continue;
463 free_evalue_refs(factors[i]);
464 delete factors[i];
469 static void print_int_vector(int *v, int len, char *name)
471 cerr << name << endl;
472 for (int j = 0; j < len; ++j) {
473 cerr << v[j] << " ";
475 cerr << endl;
478 static void print_bfc_terms(mat_ZZ& factors, bfc_vec& v)
480 cerr << endl;
481 cerr << "factors" << endl;
482 cerr << factors << endl;
483 for (int i = 0; i < v.size(); ++i) {
484 cerr << "term: " << i << endl;
485 print_int_vector(v[i]->powers, factors.NumRows(), "powers");
486 cerr << "terms" << endl;
487 cerr << v[i]->terms << endl;
488 bfc_term* bfct = static_cast<bfc_term *>(v[i]);
489 cerr << bfct->c << endl;
493 static void print_bfe_terms(mat_ZZ& factors, bfc_vec& v)
495 cerr << endl;
496 cerr << "factors" << endl;
497 cerr << factors << endl;
498 for (int i = 0; i < v.size(); ++i) {
499 cerr << "term: " << i << endl;
500 print_int_vector(v[i]->powers, factors.NumRows(), "powers");
501 cerr << "terms" << endl;
502 cerr << v[i]->terms << endl;
503 bfe_term* bfet = static_cast<bfe_term *>(v[i]);
504 for (int j = 0; j < v[i]->terms.NumRows(); ++j) {
505 char * test[] = {"a", "b"};
506 print_evalue(stderr, bfet->factors[j], test);
507 fprintf(stderr, "\n");
512 struct bfcounter : public bfcounter_base {
513 mpq_t count;
515 bfcounter(unsigned dim) : bfcounter_base(dim) {
516 mpq_init(count);
517 lower = 1;
519 ~bfcounter() {
520 mpq_clear(count);
522 virtual void base(mat_ZZ& factors, bfc_vec& v);
523 virtual void get_count(Value *result) {
524 assert(value_one_p(&count[0]._mp_den));
525 value_assign(*result, &count[0]._mp_num);
529 void bfcounter::base(mat_ZZ& factors, bfc_vec& v)
531 unsigned nf = factors.NumRows();
533 for (int i = 0; i < v.size(); ++i) {
534 bfc_term* bfct = static_cast<bfc_term *>(v[i]);
535 int total_power = 0;
536 // factor is always positive, so we always
537 // change signs
538 for (int k = 0; k < nf; ++k)
539 total_power += v[i]->powers[k];
541 int j;
542 for (j = 0; j < nf; ++j)
543 if (v[i]->powers[j] > 0)
544 break;
546 dpoly D(total_power, factors[j][0], 1);
547 for (int k = 1; k < v[i]->powers[j]; ++k) {
548 dpoly fact(total_power, factors[j][0], 1);
549 D *= fact;
551 for ( ; ++j < nf; )
552 for (int k = 0; k < v[i]->powers[j]; ++k) {
553 dpoly fact(total_power, factors[j][0], 1);
554 D *= fact;
557 for (int k = 0; k < v[i]->terms.NumRows(); ++k) {
558 dpoly n(total_power, v[i]->terms[k][0]);
559 mpq_set_si(tcount, 0, 1);
560 n.div(D, tcount, one);
561 if (total_power % 2)
562 bfct->c[k].n = -bfct->c[k].n;
563 zz2value(bfct->c[k].n, tn);
564 zz2value(bfct->c[k].d, td);
566 mpz_mul(mpq_numref(tcount), mpq_numref(tcount), tn);
567 mpz_mul(mpq_denref(tcount), mpq_denref(tcount), td);
568 mpq_canonicalize(tcount);
569 mpq_add(count, count, tcount);
571 delete v[i];
576 /* Check whether the polyhedron is unbounded and if so,
577 * check whether it has any (and therefore an infinite number of)
578 * integer points.
579 * If one of the vertices is integer, then we are done.
580 * Otherwise, transform the polyhedron such that one of the rays
581 * is the first unit vector and cut it off at a height that ensures
582 * that if the whole polyhedron has any points, then the remaining part
583 * has integer points. In particular we add the largest coefficient
584 * of a ray to the highest vertex (rounded up).
586 static bool Polyhedron_is_infinite(Polyhedron *P, Value* result,
587 barvinok_options *options)
589 int r = 0;
590 Matrix *M, *M2;
591 Value c, tmp;
592 Value g;
593 bool first;
594 Vector *v;
595 Value offset, size;
596 Polyhedron *R;
598 if (P->NbBid == 0)
599 for (; r < P->NbRays; ++r)
600 if (value_zero_p(P->Ray[r][P->Dimension+1]))
601 break;
602 if (P->NbBid == 0 && r == P->NbRays)
603 return false;
605 if (options->count_sample_infinite) {
606 Vector *sample;
608 sample = Polyhedron_Sample(P, options);
609 if (!sample)
610 value_set_si(*result, 0);
611 else {
612 value_set_si(*result, -1);
613 Vector_Free(sample);
615 return true;
618 for (int i = 0; i < P->NbRays; ++i)
619 if (value_one_p(P->Ray[i][1+P->Dimension])) {
620 value_set_si(*result, -1);
621 return true;
624 value_init(g);
625 v = Vector_Alloc(P->Dimension+1);
626 Vector_Gcd(P->Ray[r]+1, P->Dimension, &g);
627 Vector_AntiScale(P->Ray[r]+1, v->p, g, P->Dimension+1);
628 M = unimodular_complete(v);
629 value_set_si(M->p[P->Dimension][P->Dimension], 1);
630 M2 = Transpose(M);
631 Matrix_Free(M);
632 P = Polyhedron_Preimage(P, M2, 0);
633 Matrix_Free(M2);
634 value_clear(g);
635 Vector_Free(v);
637 first = true;
638 value_init(offset);
639 value_init(size);
640 value_init(tmp);
641 value_set_si(size, 0);
643 for (int i = 0; i < P->NbBid; ++i) {
644 value_absolute(tmp, P->Ray[i][1]);
645 if (value_gt(tmp, size))
646 value_assign(size, tmp);
648 for (int i = P->NbBid; i < P->NbRays; ++i) {
649 if (value_zero_p(P->Ray[i][P->Dimension+1])) {
650 if (value_gt(P->Ray[i][1], size))
651 value_assign(size, P->Ray[i][1]);
652 continue;
654 mpz_cdiv_q(tmp, P->Ray[i][1], P->Ray[i][P->Dimension+1]);
655 if (first || value_gt(tmp, offset)) {
656 value_assign(offset, tmp);
657 first = false;
660 value_addto(offset, offset, size);
661 value_clear(size);
662 value_clear(tmp);
664 v = Vector_Alloc(P->Dimension+2);
665 value_set_si(v->p[0], 1);
666 value_set_si(v->p[1], -1);
667 value_assign(v->p[1+P->Dimension], offset);
668 R = AddConstraints(v->p, 1, P, options->MaxRays);
669 Polyhedron_Free(P);
670 P = R;
672 value_clear(offset);
673 Vector_Free(v);
675 value_init(c);
676 barvinok_count_with_options(P, &c, options);
677 Polyhedron_Free(P);
678 if (value_zero_p(c))
679 value_set_si(*result, 0);
680 else
681 value_set_si(*result, -1);
682 value_clear(c);
684 return true;
687 typedef Polyhedron * Polyhedron_p;
689 static void barvinok_count_f(Polyhedron *P, Value* result,
690 barvinok_options *options);
692 void barvinok_count_with_options(Polyhedron *P, Value* result,
693 struct barvinok_options *options)
695 unsigned dim;
696 int allocated = 0;
697 Polyhedron *Q;
698 bool infinite = false;
700 if (P->next)
701 fprintf(stderr,
702 "barvinok_count: input is a union; only first polyhedron is counted\n");
704 if (emptyQ2(P)) {
705 value_set_si(*result, 0);
706 return;
708 if (P->NbEq != 0) {
709 Q = NULL;
710 do {
711 P = remove_equalities(P);
712 P = DomainConstraintSimplify(P, options->MaxRays);
713 if (Q)
714 Polyhedron_Free(Q);
715 Q = P;
716 } while (!emptyQ(P) && P->NbEq != 0);
717 if (emptyQ(P)) {
718 Polyhedron_Free(P);
719 value_set_si(*result, 0);
720 return;
722 allocated = 1;
724 if (Polyhedron_is_infinite(P, result, options)) {
725 if (allocated)
726 Polyhedron_Free(P);
727 return;
729 if (P->Dimension == 0) {
730 /* Test whether the constraints are satisfied */
731 POL_ENSURE_VERTICES(P);
732 value_set_si(*result, !emptyQ(P));
733 if (allocated)
734 Polyhedron_Free(P);
735 return;
737 Q = Polyhedron_Factor(P, 0, NULL, options->MaxRays);
738 if (Q) {
739 if (allocated)
740 Polyhedron_Free(P);
741 P = Q;
742 allocated = 1;
745 barvinok_count_f(P, result, options);
746 if (value_neg_p(*result))
747 infinite = true;
748 if (Q && P->next && value_notzero_p(*result)) {
749 Value factor;
750 value_init(factor);
752 for (Q = P->next; Q; Q = Q->next) {
753 barvinok_count_f(Q, &factor, options);
754 if (value_neg_p(factor)) {
755 infinite = true;
756 continue;
757 } else if (Q->next && value_zero_p(factor)) {
758 value_set_si(*result, 0);
759 break;
761 value_multiply(*result, *result, factor);
764 value_clear(factor);
767 if (allocated)
768 Domain_Free(P);
769 if (infinite)
770 value_set_si(*result, -1);
773 void barvinok_count(Polyhedron *P, Value* result, unsigned NbMaxCons)
775 barvinok_options *options = barvinok_options_new_with_defaults();
776 options->MaxRays = NbMaxCons;
777 barvinok_count_with_options(P, result, options);
778 barvinok_options_free(options);
781 static void barvinok_count_f(Polyhedron *P, Value* result,
782 barvinok_options *options)
784 if (emptyQ2(P)) {
785 value_set_si(*result, 0);
786 return;
789 if (P->Dimension == 1)
790 return Line_Length(P, result);
792 int c = P->NbConstraints;
793 POL_ENSURE_FACETS(P);
794 if (c != P->NbConstraints || P->NbEq != 0)
795 return barvinok_count_with_options(P, result, options);
797 POL_ENSURE_VERTICES(P);
799 if (Polyhedron_is_infinite(P, result, options))
800 return;
802 np_base *cnt;
803 if (options->incremental_specialization == 2)
804 cnt = new bfcounter(P->Dimension);
805 else if (options->incremental_specialization == 1)
806 cnt = new icounter(P->Dimension);
807 else
808 cnt = new counter(P->Dimension);
809 cnt->start(P, options);
811 cnt->get_count(result);
812 delete cnt;
815 static void uni_polynom(int param, Vector *c, evalue *EP)
817 unsigned dim = c->Size-2;
818 value_init(EP->d);
819 value_set_si(EP->d,0);
820 EP->x.p = new_enode(polynomial, dim+1, param+1);
821 for (int j = 0; j <= dim; ++j)
822 evalue_set(&EP->x.p->arr[j], c->p[j], c->p[dim+1]);
825 static void multi_polynom(Vector *c, evalue* X, evalue *EP)
827 unsigned dim = c->Size-2;
828 evalue EC;
830 value_init(EC.d);
831 evalue_set(&EC, c->p[dim], c->p[dim+1]);
833 value_init(EP->d);
834 evalue_set(EP, c->p[dim], c->p[dim+1]);
836 for (int i = dim-1; i >= 0; --i) {
837 emul(X, EP);
838 value_assign(EC.x.n, c->p[i]);
839 eadd(&EC, EP);
841 free_evalue_refs(&EC);
844 Polyhedron *unfringe (Polyhedron *P, unsigned MaxRays)
846 int len = P->Dimension+2;
847 Polyhedron *T, *R = P;
848 Value g;
849 value_init(g);
850 Vector *row = Vector_Alloc(len);
851 value_set_si(row->p[0], 1);
853 R = DomainConstraintSimplify(Polyhedron_Copy(P), MaxRays);
855 Matrix *M = Matrix_Alloc(2, len-1);
856 value_set_si(M->p[1][len-2], 1);
857 for (int v = 0; v < P->Dimension; ++v) {
858 value_set_si(M->p[0][v], 1);
859 Polyhedron *I = Polyhedron_Image(R, M, 2+1);
860 value_set_si(M->p[0][v], 0);
861 for (int r = 0; r < I->NbConstraints; ++r) {
862 if (value_zero_p(I->Constraint[r][0]))
863 continue;
864 if (value_zero_p(I->Constraint[r][1]))
865 continue;
866 if (value_one_p(I->Constraint[r][1]))
867 continue;
868 if (value_mone_p(I->Constraint[r][1]))
869 continue;
870 value_absolute(g, I->Constraint[r][1]);
871 Vector_Set(row->p+1, 0, len-2);
872 value_division(row->p[1+v], I->Constraint[r][1], g);
873 mpz_fdiv_q(row->p[len-1], I->Constraint[r][2], g);
874 T = R;
875 R = AddConstraints(row->p, 1, R, MaxRays);
876 if (T != P)
877 Polyhedron_Free(T);
879 Polyhedron_Free(I);
881 Matrix_Free(M);
882 Vector_Free(row);
883 value_clear(g);
884 return R;
887 /* Check whether all rays point in the positive directions
888 * for the parameters
890 static bool Polyhedron_has_positive_rays(Polyhedron *P, unsigned nparam)
892 int r;
893 for (r = 0; r < P->NbRays; ++r)
894 if (value_zero_p(P->Ray[r][P->Dimension+1])) {
895 int i;
896 for (i = P->Dimension - nparam; i < P->Dimension; ++i)
897 if (value_neg_p(P->Ray[r][i+1]))
898 return false;
900 return true;
903 typedef evalue * evalue_p;
905 struct enumerator_base {
906 unsigned dim;
907 evalue ** vE;
908 evalue mone;
909 vertex_decomposer *vpd;
911 enumerator_base(unsigned dim, vertex_decomposer *vpd)
913 this->dim = dim;
914 this->vpd = vpd;
916 vE = new evalue_p[vpd->nbV];
917 for (int j = 0; j < vpd->nbV; ++j)
918 vE[j] = 0;
920 value_init(mone.d);
921 evalue_set_si(&mone, -1, 1);
924 void decompose_at(Param_Vertices *V, int _i, barvinok_options *options) {
925 //this->pVD = pVD;
927 vE[_i] = new evalue;
928 value_init(vE[_i]->d);
929 evalue_set_si(vE[_i], 0, 1);
931 vpd->decompose_at_vertex(V, _i, options);
934 virtual ~enumerator_base() {
935 for (int j = 0; j < vpd->nbV; ++j)
936 if (vE[j]) {
937 free_evalue_refs(vE[j]);
938 delete vE[j];
940 delete [] vE;
942 free_evalue_refs(&mone);
945 static enumerator_base *create(Polyhedron *P, unsigned dim, unsigned nbV,
946 barvinok_options *options);
949 struct enumerator : public signed_cone_consumer, public vertex_decomposer,
950 public enumerator_base {
951 vec_ZZ lambda;
952 vec_ZZ den;
953 ZZ sign;
954 term_info num;
955 Vector *c;
956 mpq_t count;
958 enumerator(Polyhedron *P, unsigned dim, unsigned nbV) :
959 vertex_decomposer(P, nbV, *this), enumerator_base(dim, this) {
960 this->P = P;
961 this->nbV = nbV;
962 randomvector(P, lambda, dim);
963 den.SetLength(dim);
964 c = Vector_Alloc(dim+2);
966 mpq_init(count);
969 ~enumerator() {
970 mpq_clear(count);
971 Vector_Free(c);
974 virtual void handle(const signed_cone& sc, barvinok_options *options);
977 void enumerator::handle(const signed_cone& sc, barvinok_options *options)
979 assert(sc.det == 1);
980 assert(!sc.closed);
981 int r = 0;
982 assert(sc.rays.NumRows() == dim);
983 for (int k = 0; k < dim; ++k) {
984 if (lambda * sc.rays[k] == 0)
985 throw Orthogonal;
988 sign = sc.sign;
990 lattice_point(V, sc.rays, lambda, &num, 0, options);
991 den = sc.rays * lambda;
992 normalize(sign, num.constant, den);
994 dpoly n(dim, den[0], 1);
995 for (int k = 1; k < dim; ++k) {
996 dpoly fact(dim, den[k], 1);
997 n *= fact;
999 if (num.E != NULL) {
1000 ZZ one(INIT_VAL, 1);
1001 dpoly_n d(dim, num.constant, one);
1002 d.div(n, c, sign);
1003 evalue EV;
1004 multi_polynom(c, num.E, &EV);
1005 eadd(&EV , vE[vert]);
1006 free_evalue_refs(&EV);
1007 free_evalue_refs(num.E);
1008 delete num.E;
1009 } else if (num.pos != -1) {
1010 dpoly_n d(dim, num.constant, num.coeff);
1011 d.div(n, c, sign);
1012 evalue EV;
1013 uni_polynom(num.pos, c, &EV);
1014 eadd(&EV , vE[vert]);
1015 free_evalue_refs(&EV);
1016 } else {
1017 mpq_set_si(count, 0, 1);
1018 dpoly d(dim, num.constant);
1019 d.div(n, count, sign);
1020 evalue EV;
1021 value_init(EV.d);
1022 evalue_set(&EV, &count[0]._mp_num, &count[0]._mp_den);
1023 eadd(&EV , vE[vert]);
1024 free_evalue_refs(&EV);
1028 struct ienumerator_base : enumerator_base {
1029 evalue ** E_vertex;
1031 ienumerator_base(unsigned dim, vertex_decomposer *vpd) :
1032 enumerator_base(dim,vpd) {
1033 E_vertex = new evalue_p[dim];
1036 virtual ~ienumerator_base() {
1037 delete [] E_vertex;
1040 evalue *E_num(int i, int d) {
1041 return E_vertex[i + (dim-d)];
1045 struct cumulator {
1046 evalue *factor;
1047 evalue *v;
1048 dpoly_r *r;
1050 cumulator(evalue *factor, evalue *v, dpoly_r *r) :
1051 factor(factor), v(v), r(r) {}
1053 void cumulate(barvinok_options *options);
1055 virtual void add_term(const vector<int>& powers, evalue *f2) = 0;
1056 virtual ~cumulator() {}
1059 void cumulator::cumulate(barvinok_options *options)
1061 evalue cum; // factor * 1 * E_num[0]/1 * (E_num[0]-1)/2 *...
1062 evalue f;
1063 evalue t; // E_num[0] - (m-1)
1064 evalue *cst;
1065 evalue mone;
1067 if (options->lookup_table) {
1068 value_init(mone.d);
1069 evalue_set_si(&mone, -1, 1);
1072 value_init(cum.d);
1073 evalue_copy(&cum, factor);
1074 value_init(f.d);
1075 value_init(f.x.n);
1076 value_set_si(f.d, 1);
1077 value_set_si(f.x.n, 1);
1078 value_init(t.d);
1079 evalue_copy(&t, v);
1081 if (!options->lookup_table) {
1082 for (cst = &t; value_zero_p(cst->d); ) {
1083 if (cst->x.p->type == fractional)
1084 cst = &cst->x.p->arr[1];
1085 else
1086 cst = &cst->x.p->arr[0];
1090 for (int m = 0; m < r->len; ++m) {
1091 if (m > 0) {
1092 if (m > 1) {
1093 value_set_si(f.d, m);
1094 emul(&f, &cum);
1095 if (!options->lookup_table)
1096 value_subtract(cst->x.n, cst->x.n, cst->d);
1097 else
1098 eadd(&mone, &t);
1100 emul(&t, &cum);
1102 dpoly_r_term_list& current = r->c[r->len-1-m];
1103 dpoly_r_term_list::iterator j;
1104 for (j = current.begin(); j != current.end(); ++j) {
1105 if ((*j)->coeff == 0)
1106 continue;
1107 evalue *f2 = new evalue;
1108 value_init(f2->d);
1109 value_init(f2->x.n);
1110 zz2value((*j)->coeff, f2->x.n);
1111 zz2value(r->denom, f2->d);
1112 emul(&cum, f2);
1114 add_term((*j)->powers, f2);
1117 free_evalue_refs(&f);
1118 free_evalue_refs(&t);
1119 free_evalue_refs(&cum);
1120 if (options->lookup_table)
1121 free_evalue_refs(&mone);
1124 struct E_poly_term {
1125 vector<int> powers;
1126 evalue *E;
1129 struct ie_cum : public cumulator {
1130 vector<E_poly_term *> terms;
1132 ie_cum(evalue *factor, evalue *v, dpoly_r *r) : cumulator(factor, v, r) {}
1134 virtual void add_term(const vector<int>& powers, evalue *f2);
1137 void ie_cum::add_term(const vector<int>& powers, evalue *f2)
1139 int k;
1140 for (k = 0; k < terms.size(); ++k) {
1141 if (terms[k]->powers == powers) {
1142 eadd(f2, terms[k]->E);
1143 free_evalue_refs(f2);
1144 delete f2;
1145 break;
1148 if (k >= terms.size()) {
1149 E_poly_term *ET = new E_poly_term;
1150 ET->powers = powers;
1151 ET->E = f2;
1152 terms.push_back(ET);
1156 struct ienumerator : public signed_cone_consumer, public vertex_decomposer,
1157 public ienumerator_base {
1158 //Polyhedron *pVD;
1159 mat_ZZ den;
1160 mat_ZZ vertex;
1161 mpq_t tcount;
1163 ienumerator(Polyhedron *P, unsigned dim, unsigned nbV) :
1164 vertex_decomposer(P, nbV, *this), ienumerator_base(dim, this) {
1165 vertex.SetDims(1, dim);
1167 den.SetDims(dim, dim);
1168 mpq_init(tcount);
1171 ~ienumerator() {
1172 mpq_clear(tcount);
1175 virtual void handle(const signed_cone& sc, barvinok_options *options);
1176 void reduce(evalue *factor, const mat_ZZ& num, const mat_ZZ& den_f,
1177 barvinok_options *options);
1180 void ienumerator::reduce(evalue *factor, const mat_ZZ& num, const mat_ZZ& den_f,
1181 barvinok_options *options)
1183 unsigned len = den_f.NumRows(); // number of factors in den
1184 unsigned dim = num.NumCols();
1185 assert(num.NumRows() == 1);
1187 if (dim == 0) {
1188 eadd(factor, vE[vert]);
1189 return;
1192 vec_ZZ den_s;
1193 mat_ZZ den_r;
1194 vec_ZZ num_s;
1195 mat_ZZ num_p;
1197 split_one(num, num_s, num_p, den_f, den_s, den_r);
1199 vec_ZZ den_p;
1200 den_p.SetLength(len);
1202 ZZ one;
1203 one = 1;
1204 normalize(one, num_s, num_p, den_s, den_p, den_r);
1205 if (one != 1)
1206 emul(&mone, factor);
1208 int only_param = 0;
1209 int no_param = 0;
1210 for (int k = 0; k < len; ++k) {
1211 if (den_p[k] == 0)
1212 ++no_param;
1213 else if (den_s[k] == 0)
1214 ++only_param;
1216 if (no_param == 0) {
1217 reduce(factor, num_p, den_r, options);
1218 } else {
1219 int k, l;
1220 mat_ZZ pden;
1221 pden.SetDims(only_param, dim-1);
1223 for (k = 0, l = 0; k < len; ++k)
1224 if (den_s[k] == 0)
1225 pden[l++] = den_r[k];
1227 for (k = 0; k < len; ++k)
1228 if (den_p[k] == 0)
1229 break;
1231 dpoly n(no_param, num_s[0]);
1232 dpoly D(no_param, den_s[k], 1);
1233 for ( ; ++k < len; )
1234 if (den_p[k] == 0) {
1235 dpoly fact(no_param, den_s[k], 1);
1236 D *= fact;
1239 dpoly_r * r = 0;
1240 // if no_param + only_param == len then all powers
1241 // below will be all zero
1242 if (no_param + only_param == len) {
1243 if (E_num(0, dim) != 0)
1244 r = new dpoly_r(n, len);
1245 else {
1246 mpq_set_si(tcount, 0, 1);
1247 one = 1;
1248 n.div(D, tcount, one);
1250 if (value_notzero_p(mpq_numref(tcount))) {
1251 evalue f;
1252 value_init(f.d);
1253 value_init(f.x.n);
1254 value_assign(f.x.n, mpq_numref(tcount));
1255 value_assign(f.d, mpq_denref(tcount));
1256 emul(&f, factor);
1257 reduce(factor, num_p, pden, options);
1258 free_evalue_refs(&f);
1260 return;
1262 } else {
1263 for (k = 0; k < len; ++k) {
1264 if (den_s[k] == 0 || den_p[k] == 0)
1265 continue;
1267 dpoly pd(no_param-1, den_s[k], 1);
1269 int l;
1270 for (l = 0; l < k; ++l)
1271 if (den_r[l] == den_r[k])
1272 break;
1274 if (r == 0)
1275 r = new dpoly_r(n, pd, l, len);
1276 else {
1277 dpoly_r *nr = new dpoly_r(r, pd, l, len);
1278 delete r;
1279 r = nr;
1283 dpoly_r *rc = r->div(D);
1284 delete r;
1285 r = rc;
1286 if (E_num(0, dim) == 0) {
1287 int common = pden.NumRows();
1288 dpoly_r_term_list& final = r->c[r->len-1];
1289 int rows;
1290 evalue t;
1291 evalue f;
1292 value_init(f.d);
1293 value_init(f.x.n);
1294 zz2value(r->denom, f.d);
1295 dpoly_r_term_list::iterator j;
1296 for (j = final.begin(); j != final.end(); ++j) {
1297 if ((*j)->coeff == 0)
1298 continue;
1299 rows = common;
1300 for (int k = 0; k < r->dim; ++k) {
1301 int n = (*j)->powers[k];
1302 if (n == 0)
1303 continue;
1304 pden.SetDims(rows+n, pden.NumCols());
1305 for (int l = 0; l < n; ++l)
1306 pden[rows+l] = den_r[k];
1307 rows += n;
1309 value_init(t.d);
1310 evalue_copy(&t, factor);
1311 zz2value((*j)->coeff, f.x.n);
1312 emul(&f, &t);
1313 reduce(&t, num_p, pden, options);
1314 free_evalue_refs(&t);
1316 free_evalue_refs(&f);
1317 } else {
1318 ie_cum cum(factor, E_num(0, dim), r);
1319 cum.cumulate(options);
1321 int common = pden.NumRows();
1322 int rows;
1323 for (int j = 0; j < cum.terms.size(); ++j) {
1324 rows = common;
1325 pden.SetDims(rows, pden.NumCols());
1326 for (int k = 0; k < r->dim; ++k) {
1327 int n = cum.terms[j]->powers[k];
1328 if (n == 0)
1329 continue;
1330 pden.SetDims(rows+n, pden.NumCols());
1331 for (int l = 0; l < n; ++l)
1332 pden[rows+l] = den_r[k];
1333 rows += n;
1335 reduce(cum.terms[j]->E, num_p, pden, options);
1336 free_evalue_refs(cum.terms[j]->E);
1337 delete cum.terms[j]->E;
1338 delete cum.terms[j];
1341 delete r;
1345 static int type_offset(enode *p)
1347 return p->type == fractional ? 1 :
1348 p->type == flooring ? 1 : 0;
1351 static int edegree(evalue *e)
1353 int d = 0;
1354 enode *p;
1356 if (value_notzero_p(e->d))
1357 return 0;
1359 p = e->x.p;
1360 int i = type_offset(p);
1361 if (p->size-i-1 > d)
1362 d = p->size - i - 1;
1363 for (; i < p->size; i++) {
1364 int d2 = edegree(&p->arr[i]);
1365 if (d2 > d)
1366 d = d2;
1368 return d;
1371 void ienumerator::handle(const signed_cone& sc, barvinok_options *options)
1373 assert(sc.det == 1);
1374 assert(!sc.closed);
1375 assert(sc.rays.NumRows() == dim);
1377 lattice_point(V, sc.rays, vertex[0], E_vertex, options);
1379 den = sc.rays;
1381 evalue one;
1382 value_init(one.d);
1383 evalue_set_si(&one, sc.sign, 1);
1384 reduce(&one, vertex, den, options);
1385 free_evalue_refs(&one);
1387 for (int i = 0; i < dim; ++i)
1388 if (E_vertex[i]) {
1389 free_evalue_refs(E_vertex[i]);
1390 delete E_vertex[i];
1394 struct bfenumerator : public vertex_decomposer, public bf_base,
1395 public ienumerator_base {
1396 evalue *factor;
1398 bfenumerator(Polyhedron *P, unsigned dim, unsigned nbV) :
1399 vertex_decomposer(P, nbV, *this),
1400 bf_base(dim), ienumerator_base(dim, this) {
1401 lower = 0;
1402 factor = NULL;
1405 ~bfenumerator() {
1408 virtual void handle(const signed_cone& sc, barvinok_options *options);
1409 virtual void base(mat_ZZ& factors, bfc_vec& v);
1411 bfc_term_base* new_bf_term(int len) {
1412 bfe_term* t = new bfe_term(len);
1413 return t;
1416 virtual void set_factor(bfc_term_base *t, int k, int change) {
1417 bfe_term* bfet = static_cast<bfe_term *>(t);
1418 factor = bfet->factors[k];
1419 assert(factor != NULL);
1420 bfet->factors[k] = NULL;
1421 if (change)
1422 emul(&mone, factor);
1425 virtual void set_factor(bfc_term_base *t, int k, mpq_t &q, int change) {
1426 bfe_term* bfet = static_cast<bfe_term *>(t);
1427 factor = bfet->factors[k];
1428 assert(factor != NULL);
1429 bfet->factors[k] = NULL;
1431 evalue f;
1432 value_init(f.d);
1433 value_init(f.x.n);
1434 if (change)
1435 value_oppose(f.x.n, mpq_numref(q));
1436 else
1437 value_assign(f.x.n, mpq_numref(q));
1438 value_assign(f.d, mpq_denref(q));
1439 emul(&f, factor);
1440 free_evalue_refs(&f);
1443 virtual void set_factor(bfc_term_base *t, int k, const QQ& c, int change) {
1444 bfe_term* bfet = static_cast<bfe_term *>(t);
1446 factor = new evalue;
1448 evalue f;
1449 value_init(f.d);
1450 value_init(f.x.n);
1451 zz2value(c.n, f.x.n);
1452 if (change)
1453 value_oppose(f.x.n, f.x.n);
1454 zz2value(c.d, f.d);
1456 value_init(factor->d);
1457 evalue_copy(factor, bfet->factors[k]);
1458 emul(&f, factor);
1459 free_evalue_refs(&f);
1462 void set_factor(evalue *f, int change) {
1463 if (change)
1464 emul(&mone, f);
1465 factor = f;
1468 virtual void insert_term(bfc_term_base *t, int i) {
1469 bfe_term* bfet = static_cast<bfe_term *>(t);
1470 int len = t->terms.NumRows()-1; // already increased by one
1472 bfet->factors.resize(len+1);
1473 for (int j = len; j > i; --j) {
1474 bfet->factors[j] = bfet->factors[j-1];
1475 t->terms[j] = t->terms[j-1];
1477 bfet->factors[i] = factor;
1478 factor = NULL;
1481 virtual void update_term(bfc_term_base *t, int i) {
1482 bfe_term* bfet = static_cast<bfe_term *>(t);
1484 eadd(factor, bfet->factors[i]);
1485 free_evalue_refs(factor);
1486 delete factor;
1489 virtual bool constant_vertex(int dim) { return E_num(0, dim) == 0; }
1491 virtual void cum(bf_reducer *bfr, bfc_term_base *t, int k, dpoly_r *r,
1492 barvinok_options *options);
1495 enumerator_base *enumerator_base::create(Polyhedron *P, unsigned dim, unsigned nbV,
1496 barvinok_options *options)
1498 enumerator_base *eb;
1500 if (options->incremental_specialization == BV_SPECIALIZATION_BF)
1501 eb = new bfenumerator(P, dim, nbV);
1502 else if (options->incremental_specialization == BV_SPECIALIZATION_DF)
1503 eb = new ienumerator(P, dim, nbV);
1504 else
1505 eb = new enumerator(P, dim, nbV);
1507 return eb;
1510 struct bfe_cum : public cumulator {
1511 bfenumerator *bfe;
1512 bfc_term_base *told;
1513 int k;
1514 bf_reducer *bfr;
1516 bfe_cum(evalue *factor, evalue *v, dpoly_r *r, bf_reducer *bfr,
1517 bfc_term_base *t, int k, bfenumerator *e) :
1518 cumulator(factor, v, r), told(t), k(k),
1519 bfr(bfr), bfe(e) {
1522 virtual void add_term(const vector<int>& powers, evalue *f2);
1525 void bfe_cum::add_term(const vector<int>& powers, evalue *f2)
1527 bfr->update_powers(powers);
1529 bfc_term_base * t = bfe->find_bfc_term(bfr->vn, bfr->npowers, bfr->nnf);
1530 bfe->set_factor(f2, bfr->l_changes % 2);
1531 bfe->add_term(t, told->terms[k], bfr->l_extra_num);
1534 void bfenumerator::cum(bf_reducer *bfr, bfc_term_base *t, int k,
1535 dpoly_r *r, barvinok_options *options)
1537 bfe_term* bfet = static_cast<bfe_term *>(t);
1538 bfe_cum cum(bfet->factors[k], E_num(0, bfr->d), r, bfr, t, k, this);
1539 cum.cumulate(options);
1542 void bfenumerator::base(mat_ZZ& factors, bfc_vec& v)
1544 for (int i = 0; i < v.size(); ++i) {
1545 assert(v[i]->terms.NumRows() == 1);
1546 evalue *factor = static_cast<bfe_term *>(v[i])->factors[0];
1547 eadd(factor, vE[vert]);
1548 delete v[i];
1552 void bfenumerator::handle(const signed_cone& sc, barvinok_options *options)
1554 assert(sc.det == 1);
1555 assert(!sc.closed);
1556 assert(sc.rays.NumRows() == enumerator_base::dim);
1558 bfe_term* t = new bfe_term(enumerator_base::dim);
1559 vector< bfc_term_base * > v;
1560 v.push_back(t);
1562 t->factors.resize(1);
1564 t->terms.SetDims(1, enumerator_base::dim);
1565 lattice_point(V, sc.rays, t->terms[0], E_vertex, options);
1567 // the elements of factors are always lexpositive
1568 mat_ZZ factors;
1569 int s = setup_factors(sc.rays, factors, t, sc.sign);
1571 t->factors[0] = new evalue;
1572 value_init(t->factors[0]->d);
1573 evalue_set_si(t->factors[0], s, 1);
1574 reduce(factors, v, options);
1576 for (int i = 0; i < enumerator_base::dim; ++i)
1577 if (E_vertex[i]) {
1578 free_evalue_refs(E_vertex[i]);
1579 delete E_vertex[i];
1583 #ifdef HAVE_CORRECT_VERTICES
1584 static inline Param_Polyhedron *Polyhedron2Param_SD(Polyhedron **Din,
1585 Polyhedron *Cin,int WS,Polyhedron **CEq,Matrix **CT)
1587 if (WS & POL_NO_DUAL)
1588 WS = 0;
1589 return Polyhedron2Param_SimplifiedDomain(Din, Cin, WS, CEq, CT);
1591 #else
1592 static Param_Polyhedron *Polyhedron2Param_SD(Polyhedron **Din,
1593 Polyhedron *Cin,int WS,Polyhedron **CEq,Matrix **CT)
1595 static char data[] = " 1 0 0 0 0 1 -18 "
1596 " 1 0 0 -20 0 19 1 "
1597 " 1 0 1 20 0 -20 16 "
1598 " 1 0 0 0 0 -1 19 "
1599 " 1 0 -1 0 0 0 4 "
1600 " 1 4 -20 0 0 -1 23 "
1601 " 1 -4 20 0 0 1 -22 "
1602 " 1 0 1 0 20 -20 16 "
1603 " 1 0 0 0 -20 19 1 ";
1604 static int checked = 0;
1605 if (!checked) {
1606 checked = 1;
1607 char *p = data;
1608 int n, v, i;
1609 Matrix *M = Matrix_Alloc(9, 7);
1610 for (i = 0; i < 9; ++i)
1611 for (int j = 0; j < 7; ++j) {
1612 sscanf(p, "%d%n", &v, &n);
1613 p += n;
1614 value_set_si(M->p[i][j], v);
1616 Polyhedron *P = Constraints2Polyhedron(M, 1024);
1617 Matrix_Free(M);
1618 Polyhedron *U = Universe_Polyhedron(1);
1619 Param_Polyhedron *PP = Polyhedron2Param_Domain(P, U, 1024);
1620 Polyhedron_Free(P);
1621 Polyhedron_Free(U);
1622 Param_Vertices *V;
1623 for (i = 0, V = PP->V; V; ++i, V = V->next)
1625 if (PP)
1626 Param_Polyhedron_Free(PP);
1627 if (i != 10) {
1628 fprintf(stderr, "WARNING: results may be incorrect\n");
1629 fprintf(stderr,
1630 "WARNING: use latest version of PolyLib to remove this warning\n");
1634 return Polyhedron2Param_SimplifiedDomain(Din, Cin, WS, CEq, CT);
1636 #endif
1638 static evalue* barvinok_enumerate_ev_f(Polyhedron *P, Polyhedron* C,
1639 barvinok_options *options);
1641 /* Destroys C */
1642 static evalue* barvinok_enumerate_cst(Polyhedron *P, Polyhedron* C,
1643 struct barvinok_options *options)
1645 evalue *eres;
1647 ALLOC(evalue, eres);
1648 value_init(eres->d);
1649 value_set_si(eres->d, 0);
1650 eres->x.p = new_enode(partition, 2, C->Dimension);
1651 EVALUE_SET_DOMAIN(eres->x.p->arr[0],
1652 DomainConstraintSimplify(C, options->MaxRays));
1653 value_set_si(eres->x.p->arr[1].d, 1);
1654 value_init(eres->x.p->arr[1].x.n);
1655 if (emptyQ(P))
1656 value_set_si(eres->x.p->arr[1].x.n, 0);
1657 else
1658 barvinok_count_with_options(P, &eres->x.p->arr[1].x.n, options);
1660 return eres;
1663 evalue* barvinok_enumerate_with_options(Polyhedron *P, Polyhedron* C,
1664 struct barvinok_options *options)
1666 //P = unfringe(P, MaxRays);
1667 Polyhedron *next, *Cnext;
1668 Polyhedron *Corig = C;
1669 Polyhedron *Porig = P;
1670 Polyhedron *CEq = NULL, *rVD, *CA;
1671 int r = 0;
1672 unsigned nparam = C->Dimension;
1673 evalue *eres;
1675 if (P->next)
1676 fprintf(stderr,
1677 "barvinok_enumerate: input is a union; only first polyhedron is enumerated\n");
1679 if (C->next)
1680 fprintf(stderr,
1681 "barvinok_enumerate: context is a union; only first polyhedron is considered\n");
1683 evalue factor;
1684 value_init(factor.d);
1685 evalue_set_si(&factor, 1, 1);
1687 Cnext = C->next;
1688 C->next = NULL;
1689 CA = align_context(C, P->Dimension, options->MaxRays);
1690 next = P->next;
1691 P->next = NULL;
1692 P = DomainIntersection(P, CA, options->MaxRays);
1693 Porig->next = next;
1694 Polyhedron_Free(CA);
1696 /* for now */
1697 POL_ENSURE_FACETS(P);
1698 POL_ENSURE_VERTICES(P);
1699 POL_ENSURE_FACETS(C);
1700 POL_ENSURE_VERTICES(C);
1702 if (C->Dimension == 0 || emptyQ(P)) {
1703 constant:
1704 eres = barvinok_enumerate_cst(P, CEq ? CEq : Polyhedron_Copy(C), options);
1705 out:
1706 emul(&factor, eres);
1707 if (options->approximation_method == BV_APPROX_DROP) {
1708 if (options->polynomial_approximation == BV_APPROX_SIGN_UPPER)
1709 evalue_frac2polynomial(eres, 1, options->MaxRays);
1710 if (options->polynomial_approximation == BV_APPROX_SIGN_LOWER)
1711 evalue_frac2polynomial(eres, -1, options->MaxRays);
1713 reduce_evalue(eres);
1714 free_evalue_refs(&factor);
1715 Domain_Free(P);
1716 if (C != Corig)
1717 Polyhedron_Free(C);
1719 Corig->next = Cnext;
1720 return eres;
1722 if (Polyhedron_is_unbounded(P, nparam, options->MaxRays))
1723 goto constant;
1725 if (P->NbEq != 0) {
1726 Matrix *f;
1727 P = remove_equalities_p(P, P->Dimension-nparam, &f);
1728 mask(f, &factor, options);
1729 Matrix_Free(f);
1731 if (P->Dimension == nparam) {
1732 CEq = P;
1733 P = Universe_Polyhedron(0);
1734 goto constant;
1737 Polyhedron *T = Polyhedron_Factor(P, nparam, NULL, options->MaxRays);
1738 if (T || (P->Dimension == nparam+1)) {
1739 Polyhedron *Q;
1740 Polyhedron *C2;
1741 for (Q = T ? T : P; Q; Q = Q->next) {
1742 Polyhedron *next = Q->next;
1743 Q->next = NULL;
1745 Polyhedron *QC = Q;
1746 if (Q->Dimension != C->Dimension)
1747 QC = Polyhedron_Project(Q, nparam);
1749 C2 = C;
1750 C = DomainIntersection(C, QC, options->MaxRays);
1751 if (C2 != Corig)
1752 Polyhedron_Free(C2);
1753 if (QC != Q)
1754 Polyhedron_Free(QC);
1756 Q->next = next;
1759 if (T) {
1760 Polyhedron_Free(P);
1761 P = T;
1762 if (T->Dimension == C->Dimension) {
1763 P = T->next;
1764 T->next = NULL;
1765 Polyhedron_Free(T);
1769 next = P->next;
1770 P->next = NULL;
1771 eres = barvinok_enumerate_ev_f(P, C, options);
1772 P->next = next;
1774 if (P->next) {
1775 Polyhedron *Q;
1776 evalue *f;
1778 for (Q = P->next; Q; Q = Q->next) {
1779 Polyhedron *next = Q->next;
1780 Q->next = NULL;
1782 f = barvinok_enumerate_ev_f(Q, C, options);
1783 emul(f, eres);
1784 free_evalue_refs(f);
1785 free(f);
1787 Q->next = next;
1791 goto out;
1794 evalue* barvinok_enumerate_ev(Polyhedron *P, Polyhedron* C, unsigned MaxRays)
1796 evalue *E;
1797 barvinok_options *options = barvinok_options_new_with_defaults();
1798 options->MaxRays = MaxRays;
1799 E = barvinok_enumerate_with_options(P, C, options);
1800 barvinok_options_free(options);
1801 return E;
1804 static evalue* barvinok_enumerate_ev_f(Polyhedron *P, Polyhedron* C,
1805 barvinok_options *options)
1807 unsigned nparam = C->Dimension;
1808 bool scale_fast = options->approximation_method == BV_APPROX_SCALE_FAST;
1809 bool do_scale = options->approximation_method == BV_APPROX_SCALE;
1811 if (P->Dimension - nparam == 1 && !scale_fast && !do_scale)
1812 return ParamLine_Length(P, C, options);
1814 Param_Polyhedron *PP = NULL;
1815 Polyhedron *CEq = NULL, *pVD;
1816 Matrix *CT = NULL;
1817 Param_Domain *D, *next;
1818 Param_Vertices *V;
1819 evalue *eres;
1820 Polyhedron *Porig = P;
1821 scale_data scaling;
1822 Polyhedron *T;
1824 if (do_scale || scale_fast) {
1825 P = scale_init(P, C, &scaling, options);
1826 if (P != Porig) {
1827 eres = barvinok_enumerate_with_options(P, C, options);
1828 Polyhedron_Free(P);
1829 scale_finish(eres, &scaling, options);
1830 return eres;
1834 T = P;
1835 PP = Polyhedron2Param_SD(&T, C, options->MaxRays, &CEq, &CT);
1836 if (T != P && P != Porig)
1837 Polyhedron_Free(P);
1838 P = T;
1840 if (isIdentity(CT)) {
1841 Matrix_Free(CT);
1842 CT = NULL;
1843 } else {
1844 assert(CT->NbRows != CT->NbColumns);
1845 if (CT->NbRows == 1) { // no more parameters
1846 eres = barvinok_enumerate_cst(P, CEq, options);
1847 out:
1848 if (CT)
1849 Matrix_Free(CT);
1850 if (PP)
1851 Param_Polyhedron_Free(PP);
1852 if (P != Porig)
1853 Polyhedron_Free(P);
1855 return eres;
1857 nparam = CT->NbRows - 1;
1860 if (do_scale || scale_fast)
1861 P = scale(PP, P, &scaling, P != Porig, options);
1863 unsigned dim = P->Dimension - nparam;
1865 ALLOC(evalue, eres);
1866 value_init(eres->d);
1867 value_set_si(eres->d, 0);
1869 int nd;
1870 for (nd = 0, D=PP->D; D; ++nd, D=D->next);
1871 struct section { Polyhedron *D; evalue E; };
1872 section *s = new section[nd];
1873 Polyhedron **fVD = new Polyhedron_p[nd];
1875 enumerator_base *et = NULL;
1876 try_again:
1877 if (et)
1878 delete et;
1880 et = enumerator_base::create(P, dim, PP->nbV, options);
1882 for(nd = 0, D=PP->D; D; D=next) {
1883 next = D->next;
1885 Polyhedron *rVD = reduce_domain(D->Domain, CT, CEq, fVD, nd, options);
1886 if (!rVD)
1887 continue;
1889 pVD = CT ? DomainImage(rVD,CT,options->MaxRays) : rVD;
1891 value_init(s[nd].E.d);
1892 evalue_set_si(&s[nd].E, 0, 1);
1893 s[nd].D = rVD;
1895 FORALL_PVertex_in_ParamPolyhedron(V,D,PP) // _i is internal counter
1896 if (!et->vE[_i])
1897 try {
1898 et->decompose_at(V, _i, options);
1899 } catch (OrthogonalException &e) {
1900 if (rVD != pVD)
1901 Domain_Free(pVD);
1902 for (; nd >= 0; --nd) {
1903 free_evalue_refs(&s[nd].E);
1904 Domain_Free(s[nd].D);
1905 Domain_Free(fVD[nd]);
1907 goto try_again;
1909 eadd(et->vE[_i] , &s[nd].E);
1910 END_FORALL_PVertex_in_ParamPolyhedron;
1911 evalue_range_reduction_in_domain(&s[nd].E, pVD);
1913 if (CT)
1914 addeliminatedparams_evalue(&s[nd].E, CT);
1915 ++nd;
1916 if (rVD != pVD)
1917 Domain_Free(pVD);
1920 delete et;
1921 if (nd == 0)
1922 evalue_set_si(eres, 0, 1);
1923 else {
1924 eres->x.p = new_enode(partition, 2*nd, C->Dimension);
1925 for (int j = 0; j < nd; ++j) {
1926 EVALUE_SET_DOMAIN(eres->x.p->arr[2*j], s[j].D);
1927 value_clear(eres->x.p->arr[2*j+1].d);
1928 eres->x.p->arr[2*j+1] = s[j].E;
1929 Domain_Free(fVD[j]);
1932 delete [] s;
1933 delete [] fVD;
1935 if (do_scale || scale_fast)
1936 scale_finish(eres, &scaling, options);
1938 if (CEq)
1939 Polyhedron_Free(CEq);
1940 goto out;
1943 Enumeration* barvinok_enumerate(Polyhedron *P, Polyhedron* C, unsigned MaxRays)
1945 evalue *EP = barvinok_enumerate_ev(P, C, MaxRays);
1947 return partition2enumeration(EP);
1950 static void SwapColumns(Value **V, int n, int i, int j)
1952 for (int r = 0; r < n; ++r)
1953 value_swap(V[r][i], V[r][j]);
1956 static void SwapColumns(Polyhedron *P, int i, int j)
1958 SwapColumns(P->Constraint, P->NbConstraints, i, j);
1959 SwapColumns(P->Ray, P->NbRays, i, j);
1962 /* Construct a constraint c from constraints l and u such that if
1963 * if constraint c holds then for each value of the other variables
1964 * there is at most one value of variable pos (position pos+1 in the constraints).
1966 * Given a lower and an upper bound
1967 * n_l v_i + <c_l,x> + c_l >= 0
1968 * -n_u v_i + <c_u,x> + c_u >= 0
1969 * the constructed constraint is
1971 * -(n_l<c_u,x> + n_u<c_l,x>) + (-n_l c_u - n_u c_l + n_l n_u - 1)
1973 * which is then simplified to remove the content of the non-constant coefficients
1975 * len is the total length of the constraints.
1976 * v is a temporary variable that can be used by this procedure
1978 static void negative_test_constraint(Value *l, Value *u, Value *c, int pos,
1979 int len, Value *v)
1981 value_oppose(*v, u[pos+1]);
1982 Vector_Combine(l+1, u+1, c+1, *v, l[pos+1], len-1);
1983 value_multiply(*v, *v, l[pos+1]);
1984 value_subtract(c[len-1], c[len-1], *v);
1985 value_set_si(*v, -1);
1986 Vector_Scale(c+1, c+1, *v, len-1);
1987 value_decrement(c[len-1], c[len-1]);
1988 ConstraintSimplify(c, c, len, v);
1991 static bool parallel_constraints(Value *l, Value *u, Value *c, int pos,
1992 int len)
1994 bool parallel;
1995 Value g1;
1996 Value g2;
1997 value_init(g1);
1998 value_init(g2);
2000 Vector_Gcd(&l[1+pos], len, &g1);
2001 Vector_Gcd(&u[1+pos], len, &g2);
2002 Vector_Combine(l+1+pos, u+1+pos, c+1, g2, g1, len);
2003 parallel = First_Non_Zero(c+1, len) == -1;
2005 value_clear(g1);
2006 value_clear(g2);
2008 return parallel;
2011 static void negative_test_constraint7(Value *l, Value *u, Value *c, int pos,
2012 int exist, int len, Value *v)
2014 Value g;
2015 value_init(g);
2017 Vector_Gcd(&u[1+pos], exist, v);
2018 Vector_Gcd(&l[1+pos], exist, &g);
2019 Vector_Combine(l+1, u+1, c+1, *v, g, len-1);
2020 value_multiply(*v, *v, g);
2021 value_subtract(c[len-1], c[len-1], *v);
2022 value_set_si(*v, -1);
2023 Vector_Scale(c+1, c+1, *v, len-1);
2024 value_decrement(c[len-1], c[len-1]);
2025 ConstraintSimplify(c, c, len, v);
2027 value_clear(g);
2030 /* Turns a x + b >= 0 into a x + b <= -1
2032 * len is the total length of the constraint.
2033 * v is a temporary variable that can be used by this procedure
2035 static void oppose_constraint(Value *c, int len, Value *v)
2037 value_set_si(*v, -1);
2038 Vector_Scale(c+1, c+1, *v, len-1);
2039 value_decrement(c[len-1], c[len-1]);
2042 /* Split polyhedron P into two polyhedra *pos and *neg, where
2043 * existential variable i has at most one solution for each
2044 * value of the other variables in *neg.
2046 * The splitting is performed using constraints l and u.
2048 * nvar: number of set variables
2049 * row: temporary vector that can be used by this procedure
2050 * f: temporary value that can be used by this procedure
2052 static bool SplitOnConstraint(Polyhedron *P, int i, int l, int u,
2053 int nvar, int MaxRays, Vector *row, Value& f,
2054 Polyhedron **pos, Polyhedron **neg)
2056 negative_test_constraint(P->Constraint[l], P->Constraint[u],
2057 row->p, nvar+i, P->Dimension+2, &f);
2058 *neg = AddConstraints(row->p, 1, P, MaxRays);
2060 /* We found an independent, but useless constraint
2061 * Maybe we should detect this earlier and not
2062 * mark the variable as INDEPENDENT
2064 if (emptyQ((*neg))) {
2065 Polyhedron_Free(*neg);
2066 return false;
2069 oppose_constraint(row->p, P->Dimension+2, &f);
2070 *pos = AddConstraints(row->p, 1, P, MaxRays);
2072 if (emptyQ((*pos))) {
2073 Polyhedron_Free(*neg);
2074 Polyhedron_Free(*pos);
2075 return false;
2078 return true;
2082 * unimodularly transform P such that constraint r is transformed
2083 * into a constraint that involves only a single (the first)
2084 * existential variable
2087 static Polyhedron *rotate_along(Polyhedron *P, int r, int nvar, int exist,
2088 unsigned MaxRays)
2090 Value g;
2091 value_init(g);
2093 Vector *row = Vector_Alloc(exist);
2094 Vector_Copy(P->Constraint[r]+1+nvar, row->p, exist);
2095 Vector_Gcd(row->p, exist, &g);
2096 if (value_notone_p(g))
2097 Vector_AntiScale(row->p, row->p, g, exist);
2098 value_clear(g);
2100 Matrix *M = unimodular_complete(row);
2101 Matrix *M2 = Matrix_Alloc(P->Dimension+1, P->Dimension+1);
2102 for (r = 0; r < nvar; ++r)
2103 value_set_si(M2->p[r][r], 1);
2104 for ( ; r < nvar+exist; ++r)
2105 Vector_Copy(M->p[r-nvar], M2->p[r]+nvar, exist);
2106 for ( ; r < P->Dimension+1; ++r)
2107 value_set_si(M2->p[r][r], 1);
2108 Polyhedron *T = Polyhedron_Image(P, M2, MaxRays);
2110 Matrix_Free(M2);
2111 Matrix_Free(M);
2112 Vector_Free(row);
2114 return T;
2117 /* Split polyhedron P into two polyhedra *pos and *neg, where
2118 * existential variable i has at most one solution for each
2119 * value of the other variables in *neg.
2121 * If independent is set, then the two constraints on which the
2122 * split will be performed need to be independent of the other
2123 * existential variables.
2125 * Return true if an appropriate split could be performed.
2127 * nvar: number of set variables
2128 * exist: number of existential variables
2129 * row: temporary vector that can be used by this procedure
2130 * f: temporary value that can be used by this procedure
2132 static bool SplitOnVar(Polyhedron *P, int i,
2133 int nvar, int exist, int MaxRays,
2134 Vector *row, Value& f, bool independent,
2135 Polyhedron **pos, Polyhedron **neg)
2137 int j;
2139 for (int l = P->NbEq; l < P->NbConstraints; ++l) {
2140 if (value_negz_p(P->Constraint[l][nvar+i+1]))
2141 continue;
2143 if (independent) {
2144 for (j = 0; j < exist; ++j)
2145 if (j != i && value_notzero_p(P->Constraint[l][nvar+j+1]))
2146 break;
2147 if (j < exist)
2148 continue;
2151 for (int u = P->NbEq; u < P->NbConstraints; ++u) {
2152 if (value_posz_p(P->Constraint[u][nvar+i+1]))
2153 continue;
2155 if (independent) {
2156 for (j = 0; j < exist; ++j)
2157 if (j != i && value_notzero_p(P->Constraint[u][nvar+j+1]))
2158 break;
2159 if (j < exist)
2160 continue;
2163 if (SplitOnConstraint(P, i, l, u, nvar, MaxRays, row, f, pos, neg)) {
2164 if (independent) {
2165 if (i != 0)
2166 SwapColumns(*neg, nvar+1, nvar+1+i);
2168 return true;
2173 return false;
2176 static bool double_bound_pair(Polyhedron *P, int nvar, int exist,
2177 int i, int l1, int l2,
2178 Polyhedron **pos, Polyhedron **neg)
2180 Value f;
2181 value_init(f);
2182 Vector *row = Vector_Alloc(P->Dimension+2);
2183 value_set_si(row->p[0], 1);
2184 value_oppose(f, P->Constraint[l1][nvar+i+1]);
2185 Vector_Combine(P->Constraint[l1]+1, P->Constraint[l2]+1,
2186 row->p+1,
2187 P->Constraint[l2][nvar+i+1], f,
2188 P->Dimension+1);
2189 ConstraintSimplify(row->p, row->p, P->Dimension+2, &f);
2190 *pos = AddConstraints(row->p, 1, P, 0);
2191 value_set_si(f, -1);
2192 Vector_Scale(row->p+1, row->p+1, f, P->Dimension+1);
2193 value_decrement(row->p[P->Dimension+1], row->p[P->Dimension+1]);
2194 *neg = AddConstraints(row->p, 1, P, 0);
2195 Vector_Free(row);
2196 value_clear(f);
2198 return !emptyQ((*pos)) && !emptyQ((*neg));
2201 static bool double_bound(Polyhedron *P, int nvar, int exist,
2202 Polyhedron **pos, Polyhedron **neg)
2204 for (int i = 0; i < exist; ++i) {
2205 int l1, l2;
2206 for (l1 = P->NbEq; l1 < P->NbConstraints; ++l1) {
2207 if (value_negz_p(P->Constraint[l1][nvar+i+1]))
2208 continue;
2209 for (l2 = l1 + 1; l2 < P->NbConstraints; ++l2) {
2210 if (value_negz_p(P->Constraint[l2][nvar+i+1]))
2211 continue;
2212 if (double_bound_pair(P, nvar, exist, i, l1, l2, pos, neg))
2213 return true;
2216 for (l1 = P->NbEq; l1 < P->NbConstraints; ++l1) {
2217 if (value_posz_p(P->Constraint[l1][nvar+i+1]))
2218 continue;
2219 if (l1 < P->NbConstraints)
2220 for (l2 = l1 + 1; l2 < P->NbConstraints; ++l2) {
2221 if (value_posz_p(P->Constraint[l2][nvar+i+1]))
2222 continue;
2223 if (double_bound_pair(P, nvar, exist, i, l1, l2, pos, neg))
2224 return true;
2227 return false;
2229 return false;
2232 enum constraint {
2233 ALL_POS = 1 << 0,
2234 ONE_NEG = 1 << 1,
2235 INDEPENDENT = 1 << 2,
2236 ROT_NEG = 1 << 3
2239 static evalue* enumerate_or(Polyhedron *D,
2240 unsigned exist, unsigned nparam, barvinok_options *options)
2242 #ifdef DEBUG_ER
2243 fprintf(stderr, "\nER: Or\n");
2244 #endif /* DEBUG_ER */
2246 Polyhedron *N = D->next;
2247 D->next = 0;
2248 evalue *EP =
2249 barvinok_enumerate_e_with_options(D, exist, nparam, options);
2250 Polyhedron_Free(D);
2252 for (D = N; D; D = N) {
2253 N = D->next;
2254 D->next = 0;
2256 evalue *EN =
2257 barvinok_enumerate_e_with_options(D, exist, nparam, options);
2259 eor(EN, EP);
2260 free_evalue_refs(EN);
2261 free(EN);
2262 Polyhedron_Free(D);
2265 reduce_evalue(EP);
2267 return EP;
2270 static evalue* enumerate_sum(Polyhedron *P,
2271 unsigned exist, unsigned nparam, barvinok_options *options)
2273 int nvar = P->Dimension - exist - nparam;
2274 int toswap = nvar < exist ? nvar : exist;
2275 for (int i = 0; i < toswap; ++i)
2276 SwapColumns(P, 1 + i, nvar+exist - i);
2277 nparam += nvar;
2279 #ifdef DEBUG_ER
2280 fprintf(stderr, "\nER: Sum\n");
2281 #endif /* DEBUG_ER */
2283 evalue *EP = barvinok_enumerate_e_with_options(P, exist, nparam, options);
2285 evalue_split_domains_into_orthants(EP, options->MaxRays);
2286 reduce_evalue(EP);
2287 evalue_range_reduction(EP);
2289 evalue_frac2floor2(EP, 1);
2291 evalue *sum = esum(EP, nvar);
2293 free_evalue_refs(EP);
2294 free(EP);
2295 EP = sum;
2297 evalue_range_reduction(EP);
2299 return EP;
2302 static evalue* split_sure(Polyhedron *P, Polyhedron *S,
2303 unsigned exist, unsigned nparam, barvinok_options *options)
2305 int nvar = P->Dimension - exist - nparam;
2307 Matrix *M = Matrix_Alloc(exist, S->Dimension+2);
2308 for (int i = 0; i < exist; ++i)
2309 value_set_si(M->p[i][nvar+i+1], 1);
2310 Polyhedron *O = S;
2311 S = DomainAddRays(S, M, options->MaxRays);
2312 Polyhedron_Free(O);
2313 Polyhedron *F = DomainAddRays(P, M, options->MaxRays);
2314 Polyhedron *D = DomainDifference(F, S, options->MaxRays);
2315 O = D;
2316 D = Disjoint_Domain(D, 0, options->MaxRays);
2317 Polyhedron_Free(F);
2318 Domain_Free(O);
2319 Matrix_Free(M);
2321 M = Matrix_Alloc(P->Dimension+1-exist, P->Dimension+1);
2322 for (int j = 0; j < nvar; ++j)
2323 value_set_si(M->p[j][j], 1);
2324 for (int j = 0; j < nparam+1; ++j)
2325 value_set_si(M->p[nvar+j][nvar+exist+j], 1);
2326 Polyhedron *T = Polyhedron_Image(S, M, options->MaxRays);
2327 evalue *EP = barvinok_enumerate_e_with_options(T, 0, nparam, options);
2328 Polyhedron_Free(S);
2329 Polyhedron_Free(T);
2330 Matrix_Free(M);
2332 for (Polyhedron *Q = D; Q; Q = Q->next) {
2333 Polyhedron *N = Q->next;
2334 Q->next = 0;
2335 T = DomainIntersection(P, Q, options->MaxRays);
2336 evalue *E = barvinok_enumerate_e_with_options(T, exist, nparam, options);
2337 eadd(E, EP);
2338 free_evalue_refs(E);
2339 free(E);
2340 Polyhedron_Free(T);
2341 Q->next = N;
2343 Domain_Free(D);
2344 return EP;
2347 static evalue* enumerate_sure(Polyhedron *P,
2348 unsigned exist, unsigned nparam, barvinok_options *options)
2350 int i;
2351 Polyhedron *S = P;
2352 int nvar = P->Dimension - exist - nparam;
2353 Value lcm;
2354 Value f;
2355 value_init(lcm);
2356 value_init(f);
2358 for (i = 0; i < exist; ++i) {
2359 Matrix *M = Matrix_Alloc(S->NbConstraints, S->Dimension+2);
2360 int c = 0;
2361 value_set_si(lcm, 1);
2362 for (int j = 0; j < S->NbConstraints; ++j) {
2363 if (value_negz_p(S->Constraint[j][1+nvar+i]))
2364 continue;
2365 if (value_one_p(S->Constraint[j][1+nvar+i]))
2366 continue;
2367 value_lcm(lcm, S->Constraint[j][1+nvar+i], &lcm);
2370 for (int j = 0; j < S->NbConstraints; ++j) {
2371 if (value_negz_p(S->Constraint[j][1+nvar+i]))
2372 continue;
2373 if (value_one_p(S->Constraint[j][1+nvar+i]))
2374 continue;
2375 value_division(f, lcm, S->Constraint[j][1+nvar+i]);
2376 Vector_Scale(S->Constraint[j], M->p[c], f, S->Dimension+2);
2377 value_subtract(M->p[c][S->Dimension+1],
2378 M->p[c][S->Dimension+1],
2379 lcm);
2380 value_increment(M->p[c][S->Dimension+1],
2381 M->p[c][S->Dimension+1]);
2382 ++c;
2384 Polyhedron *O = S;
2385 S = AddConstraints(M->p[0], c, S, options->MaxRays);
2386 if (O != P)
2387 Polyhedron_Free(O);
2388 Matrix_Free(M);
2389 if (emptyQ(S)) {
2390 Polyhedron_Free(S);
2391 value_clear(lcm);
2392 value_clear(f);
2393 return 0;
2396 value_clear(lcm);
2397 value_clear(f);
2399 #ifdef DEBUG_ER
2400 fprintf(stderr, "\nER: Sure\n");
2401 #endif /* DEBUG_ER */
2403 return split_sure(P, S, exist, nparam, options);
2406 static evalue* enumerate_sure2(Polyhedron *P,
2407 unsigned exist, unsigned nparam, barvinok_options *options)
2409 int nvar = P->Dimension - exist - nparam;
2410 int r;
2411 for (r = 0; r < P->NbRays; ++r)
2412 if (value_one_p(P->Ray[r][0]) &&
2413 value_one_p(P->Ray[r][P->Dimension+1]))
2414 break;
2416 if (r >= P->NbRays)
2417 return 0;
2419 Matrix *M = Matrix_Alloc(nvar + 1 + nparam, P->Dimension+2);
2420 for (int i = 0; i < nvar; ++i)
2421 value_set_si(M->p[i][1+i], 1);
2422 for (int i = 0; i < nparam; ++i)
2423 value_set_si(M->p[i+nvar][1+nvar+exist+i], 1);
2424 Vector_Copy(P->Ray[r]+1+nvar, M->p[nvar+nparam]+1+nvar, exist);
2425 value_set_si(M->p[nvar+nparam][0], 1);
2426 value_set_si(M->p[nvar+nparam][P->Dimension+1], 1);
2427 Polyhedron * F = Rays2Polyhedron(M, options->MaxRays);
2428 Matrix_Free(M);
2430 Polyhedron *I = DomainIntersection(F, P, options->MaxRays);
2431 Polyhedron_Free(F);
2433 #ifdef DEBUG_ER
2434 fprintf(stderr, "\nER: Sure2\n");
2435 #endif /* DEBUG_ER */
2437 return split_sure(P, I, exist, nparam, options);
2440 static evalue* enumerate_cyclic(Polyhedron *P,
2441 unsigned exist, unsigned nparam,
2442 evalue * EP, int r, int p, unsigned MaxRays)
2444 int nvar = P->Dimension - exist - nparam;
2446 /* If EP in its fractional maps only contains references
2447 * to the remainder parameter with appropriate coefficients
2448 * then we could in principle avoid adding existentially
2449 * quantified variables to the validity domains.
2450 * We'd have to replace the remainder by m { p/m }
2451 * and multiply with an appropriate factor that is one
2452 * only in the appropriate range.
2453 * This last multiplication can be avoided if EP
2454 * has a single validity domain with no (further)
2455 * constraints on the remainder parameter
2458 Matrix *CT = Matrix_Alloc(nparam+1, nparam+3);
2459 Matrix *M = Matrix_Alloc(1, 1+nparam+3);
2460 for (int j = 0; j < nparam; ++j)
2461 if (j != p)
2462 value_set_si(CT->p[j][j], 1);
2463 value_set_si(CT->p[p][nparam+1], 1);
2464 value_set_si(CT->p[nparam][nparam+2], 1);
2465 value_set_si(M->p[0][1+p], -1);
2466 value_absolute(M->p[0][1+nparam], P->Ray[0][1+nvar+exist+p]);
2467 value_set_si(M->p[0][1+nparam+1], 1);
2468 Polyhedron *CEq = Constraints2Polyhedron(M, 1);
2469 Matrix_Free(M);
2470 addeliminatedparams_enum(EP, CT, CEq, MaxRays, nparam);
2471 Polyhedron_Free(CEq);
2472 Matrix_Free(CT);
2474 return EP;
2477 static void enumerate_vd_add_ray(evalue *EP, Matrix *Rays, unsigned MaxRays)
2479 if (value_notzero_p(EP->d))
2480 return;
2482 assert(EP->x.p->type == partition);
2483 assert(EP->x.p->pos == EVALUE_DOMAIN(EP->x.p->arr[0])->Dimension);
2484 for (int i = 0; i < EP->x.p->size/2; ++i) {
2485 Polyhedron *D = EVALUE_DOMAIN(EP->x.p->arr[2*i]);
2486 Polyhedron *N = DomainAddRays(D, Rays, MaxRays);
2487 EVALUE_SET_DOMAIN(EP->x.p->arr[2*i], N);
2488 Domain_Free(D);
2492 static evalue* enumerate_line(Polyhedron *P,
2493 unsigned exist, unsigned nparam, barvinok_options *options)
2495 if (P->NbBid == 0)
2496 return 0;
2498 #ifdef DEBUG_ER
2499 fprintf(stderr, "\nER: Line\n");
2500 #endif /* DEBUG_ER */
2502 int nvar = P->Dimension - exist - nparam;
2503 int i, j;
2504 for (i = 0; i < nparam; ++i)
2505 if (value_notzero_p(P->Ray[0][1+nvar+exist+i]))
2506 break;
2507 assert(i < nparam);
2508 for (j = i+1; j < nparam; ++j)
2509 if (value_notzero_p(P->Ray[0][1+nvar+exist+i]))
2510 break;
2511 assert(j >= nparam); // for now
2513 Matrix *M = Matrix_Alloc(2, P->Dimension+2);
2514 value_set_si(M->p[0][0], 1);
2515 value_set_si(M->p[0][1+nvar+exist+i], 1);
2516 value_set_si(M->p[1][0], 1);
2517 value_set_si(M->p[1][1+nvar+exist+i], -1);
2518 value_absolute(M->p[1][1+P->Dimension], P->Ray[0][1+nvar+exist+i]);
2519 value_decrement(M->p[1][1+P->Dimension], M->p[1][1+P->Dimension]);
2520 Polyhedron *S = AddConstraints(M->p[0], 2, P, options->MaxRays);
2521 evalue *EP = barvinok_enumerate_e_with_options(S, exist, nparam, options);
2522 Polyhedron_Free(S);
2523 Matrix_Free(M);
2525 return enumerate_cyclic(P, exist, nparam, EP, 0, i, options->MaxRays);
2528 static int single_param_pos(Polyhedron*P, unsigned exist, unsigned nparam,
2529 int r)
2531 int nvar = P->Dimension - exist - nparam;
2532 if (First_Non_Zero(P->Ray[r]+1, nvar) != -1)
2533 return -1;
2534 int i = First_Non_Zero(P->Ray[r]+1+nvar+exist, nparam);
2535 if (i == -1)
2536 return -1;
2537 if (First_Non_Zero(P->Ray[r]+1+nvar+exist+1, nparam-i-1) != -1)
2538 return -1;
2539 return i;
2542 static evalue* enumerate_remove_ray(Polyhedron *P, int r,
2543 unsigned exist, unsigned nparam, barvinok_options *options)
2545 #ifdef DEBUG_ER
2546 fprintf(stderr, "\nER: RedundantRay\n");
2547 #endif /* DEBUG_ER */
2549 Value one;
2550 value_init(one);
2551 value_set_si(one, 1);
2552 int len = P->NbRays-1;
2553 Matrix *M = Matrix_Alloc(2 * len, P->Dimension+2);
2554 Vector_Copy(P->Ray[0], M->p[0], r * (P->Dimension+2));
2555 Vector_Copy(P->Ray[r+1], M->p[r], (len-r) * (P->Dimension+2));
2556 for (int j = 0; j < P->NbRays; ++j) {
2557 if (j == r)
2558 continue;
2559 Vector_Combine(P->Ray[j], P->Ray[r], M->p[len+j-(j>r)],
2560 one, P->Ray[j][P->Dimension+1], P->Dimension+2);
2563 P = Rays2Polyhedron(M, options->MaxRays);
2564 Matrix_Free(M);
2565 evalue *EP = barvinok_enumerate_e_with_options(P, exist, nparam, options);
2566 Polyhedron_Free(P);
2567 value_clear(one);
2569 return EP;
2572 static evalue* enumerate_redundant_ray(Polyhedron *P,
2573 unsigned exist, unsigned nparam, barvinok_options *options)
2575 assert(P->NbBid == 0);
2576 int nvar = P->Dimension - exist - nparam;
2577 Value m;
2578 value_init(m);
2580 for (int r = 0; r < P->NbRays; ++r) {
2581 if (value_notzero_p(P->Ray[r][P->Dimension+1]))
2582 continue;
2583 int i1 = single_param_pos(P, exist, nparam, r);
2584 if (i1 == -1)
2585 continue;
2586 for (int r2 = r+1; r2 < P->NbRays; ++r2) {
2587 if (value_notzero_p(P->Ray[r2][P->Dimension+1]))
2588 continue;
2589 int i2 = single_param_pos(P, exist, nparam, r2);
2590 if (i2 == -1)
2591 continue;
2592 if (i1 != i2)
2593 continue;
2595 value_division(m, P->Ray[r][1+nvar+exist+i1],
2596 P->Ray[r2][1+nvar+exist+i1]);
2597 value_multiply(m, m, P->Ray[r2][1+nvar+exist+i1]);
2598 /* r2 divides r => r redundant */
2599 if (value_eq(m, P->Ray[r][1+nvar+exist+i1])) {
2600 value_clear(m);
2601 return enumerate_remove_ray(P, r, exist, nparam, options);
2604 value_division(m, P->Ray[r2][1+nvar+exist+i1],
2605 P->Ray[r][1+nvar+exist+i1]);
2606 value_multiply(m, m, P->Ray[r][1+nvar+exist+i1]);
2607 /* r divides r2 => r2 redundant */
2608 if (value_eq(m, P->Ray[r2][1+nvar+exist+i1])) {
2609 value_clear(m);
2610 return enumerate_remove_ray(P, r2, exist, nparam, options);
2614 value_clear(m);
2615 return 0;
2618 static Polyhedron *upper_bound(Polyhedron *P,
2619 int pos, Value *max, Polyhedron **R)
2621 Value v;
2622 int r;
2623 value_init(v);
2625 *R = 0;
2626 Polyhedron *N;
2627 Polyhedron *B = 0;
2628 for (Polyhedron *Q = P; Q; Q = N) {
2629 N = Q->next;
2630 for (r = 0; r < P->NbRays; ++r) {
2631 if (value_zero_p(P->Ray[r][P->Dimension+1]) &&
2632 value_pos_p(P->Ray[r][1+pos]))
2633 break;
2635 if (r < P->NbRays) {
2636 Q->next = *R;
2637 *R = Q;
2638 continue;
2639 } else {
2640 Q->next = B;
2641 B = Q;
2643 for (r = 0; r < P->NbRays; ++r) {
2644 if (value_zero_p(P->Ray[r][P->Dimension+1]))
2645 continue;
2646 mpz_fdiv_q(v, P->Ray[r][1+pos], P->Ray[r][1+P->Dimension]);
2647 if ((!Q->next && r == 0) || value_gt(v, *max))
2648 value_assign(*max, v);
2651 value_clear(v);
2652 return B;
2655 static evalue* enumerate_ray(Polyhedron *P,
2656 unsigned exist, unsigned nparam, barvinok_options *options)
2658 assert(P->NbBid == 0);
2659 int nvar = P->Dimension - exist - nparam;
2661 int r;
2662 for (r = 0; r < P->NbRays; ++r)
2663 if (value_zero_p(P->Ray[r][P->Dimension+1]))
2664 break;
2665 if (r >= P->NbRays)
2666 return 0;
2668 int r2;
2669 for (r2 = r+1; r2 < P->NbRays; ++r2)
2670 if (value_zero_p(P->Ray[r2][P->Dimension+1]))
2671 break;
2672 if (r2 < P->NbRays) {
2673 if (nvar > 0)
2674 return enumerate_sum(P, exist, nparam, options);
2677 #ifdef DEBUG_ER
2678 fprintf(stderr, "\nER: Ray\n");
2679 #endif /* DEBUG_ER */
2681 Value m;
2682 Value one;
2683 value_init(m);
2684 value_init(one);
2685 value_set_si(one, 1);
2686 int i = single_param_pos(P, exist, nparam, r);
2687 assert(i != -1); // for now;
2689 Matrix *M = Matrix_Alloc(P->NbRays, P->Dimension+2);
2690 for (int j = 0; j < P->NbRays; ++j) {
2691 Vector_Combine(P->Ray[j], P->Ray[r], M->p[j],
2692 one, P->Ray[j][P->Dimension+1], P->Dimension+2);
2694 Polyhedron *S = Rays2Polyhedron(M, options->MaxRays);
2695 Matrix_Free(M);
2696 Polyhedron *D = DomainDifference(P, S, options->MaxRays);
2697 Polyhedron_Free(S);
2698 // Polyhedron_Print(stderr, P_VALUE_FMT, D);
2699 assert(value_pos_p(P->Ray[r][1+nvar+exist+i])); // for now
2700 Polyhedron *R;
2701 D = upper_bound(D, nvar+exist+i, &m, &R);
2702 assert(D);
2703 Domain_Free(D);
2705 M = Matrix_Alloc(2, P->Dimension+2);
2706 value_set_si(M->p[0][0], 1);
2707 value_set_si(M->p[1][0], 1);
2708 value_set_si(M->p[0][1+nvar+exist+i], -1);
2709 value_set_si(M->p[1][1+nvar+exist+i], 1);
2710 value_assign(M->p[0][1+P->Dimension], m);
2711 value_oppose(M->p[1][1+P->Dimension], m);
2712 value_addto(M->p[1][1+P->Dimension], M->p[1][1+P->Dimension],
2713 P->Ray[r][1+nvar+exist+i]);
2714 value_decrement(M->p[1][1+P->Dimension], M->p[1][1+P->Dimension]);
2715 // Matrix_Print(stderr, P_VALUE_FMT, M);
2716 D = AddConstraints(M->p[0], 2, P, options->MaxRays);
2717 // Polyhedron_Print(stderr, P_VALUE_FMT, D);
2718 value_subtract(M->p[0][1+P->Dimension], M->p[0][1+P->Dimension],
2719 P->Ray[r][1+nvar+exist+i]);
2720 // Matrix_Print(stderr, P_VALUE_FMT, M);
2721 S = AddConstraints(M->p[0], 1, P, options->MaxRays);
2722 // Polyhedron_Print(stderr, P_VALUE_FMT, S);
2723 Matrix_Free(M);
2725 evalue *EP = barvinok_enumerate_e_with_options(D, exist, nparam, options);
2726 Polyhedron_Free(D);
2727 value_clear(one);
2728 value_clear(m);
2730 if (value_notone_p(P->Ray[r][1+nvar+exist+i]))
2731 EP = enumerate_cyclic(P, exist, nparam, EP, r, i, options->MaxRays);
2732 else {
2733 M = Matrix_Alloc(1, nparam+2);
2734 value_set_si(M->p[0][0], 1);
2735 value_set_si(M->p[0][1+i], 1);
2736 enumerate_vd_add_ray(EP, M, options->MaxRays);
2737 Matrix_Free(M);
2740 if (!emptyQ(S)) {
2741 evalue *E = barvinok_enumerate_e_with_options(S, exist, nparam, options);
2742 eadd(E, EP);
2743 free_evalue_refs(E);
2744 free(E);
2746 Polyhedron_Free(S);
2748 if (R) {
2749 assert(nvar == 0);
2750 evalue *ER = enumerate_or(R, exist, nparam, options);
2751 eor(ER, EP);
2752 free_evalue_refs(ER);
2753 free(ER);
2756 return EP;
2759 static evalue* enumerate_vd(Polyhedron **PA,
2760 unsigned exist, unsigned nparam, barvinok_options *options)
2762 Polyhedron *P = *PA;
2763 int nvar = P->Dimension - exist - nparam;
2764 Param_Polyhedron *PP = NULL;
2765 Polyhedron *C = Universe_Polyhedron(nparam);
2766 Polyhedron *CEq;
2767 Matrix *CT;
2768 Polyhedron *PR = P;
2769 PP = Polyhedron2Param_SimplifiedDomain(&PR,C, options->MaxRays,&CEq,&CT);
2770 Polyhedron_Free(C);
2772 int nd;
2773 Param_Domain *D, *last;
2774 Value c;
2775 value_init(c);
2776 for (nd = 0, D=PP->D; D; D=D->next, ++nd)
2779 Polyhedron **VD = new Polyhedron_p[nd];
2780 Polyhedron **fVD = new Polyhedron_p[nd];
2781 for(nd = 0, D=PP->D; D; D=D->next) {
2782 Polyhedron *rVD = reduce_domain(D->Domain, CT, CEq, fVD, nd, options);
2783 if (!rVD)
2784 continue;
2786 VD[nd++] = rVD;
2787 last = D;
2790 evalue *EP = 0;
2792 if (nd == 0)
2793 EP = evalue_zero();
2795 /* This doesn't seem to have any effect */
2796 if (nd == 1) {
2797 Polyhedron *CA = align_context(VD[0], P->Dimension, options->MaxRays);
2798 Polyhedron *O = P;
2799 P = DomainIntersection(P, CA, options->MaxRays);
2800 if (O != *PA)
2801 Polyhedron_Free(O);
2802 Polyhedron_Free(CA);
2803 if (emptyQ(P))
2804 EP = evalue_zero();
2807 if (!EP && CT->NbColumns != CT->NbRows) {
2808 Polyhedron *CEqr = DomainImage(CEq, CT, options->MaxRays);
2809 Polyhedron *CA = align_context(CEqr, PR->Dimension, options->MaxRays);
2810 Polyhedron *I = DomainIntersection(PR, CA, options->MaxRays);
2811 Polyhedron_Free(CEqr);
2812 Polyhedron_Free(CA);
2813 #ifdef DEBUG_ER
2814 fprintf(stderr, "\nER: Eliminate\n");
2815 #endif /* DEBUG_ER */
2816 nparam -= CT->NbColumns - CT->NbRows;
2817 EP = barvinok_enumerate_e_with_options(I, exist, nparam, options);
2818 nparam += CT->NbColumns - CT->NbRows;
2819 addeliminatedparams_enum(EP, CT, CEq, options->MaxRays, nparam);
2820 Polyhedron_Free(I);
2822 if (PR != *PA)
2823 Polyhedron_Free(PR);
2824 PR = 0;
2826 if (!EP && nd > 1) {
2827 #ifdef DEBUG_ER
2828 fprintf(stderr, "\nER: VD\n");
2829 #endif /* DEBUG_ER */
2830 for (int i = 0; i < nd; ++i) {
2831 Polyhedron *CA = align_context(VD[i], P->Dimension, options->MaxRays);
2832 Polyhedron *I = DomainIntersection(P, CA, options->MaxRays);
2834 if (i == 0)
2835 EP = barvinok_enumerate_e_with_options(I, exist, nparam, options);
2836 else {
2837 evalue *E = barvinok_enumerate_e_with_options(I, exist, nparam,
2838 options);
2839 eadd(E, EP);
2840 free_evalue_refs(E);
2841 free(E);
2843 Polyhedron_Free(I);
2844 Polyhedron_Free(CA);
2848 for (int i = 0; i < nd; ++i) {
2849 Polyhedron_Free(VD[i]);
2850 Polyhedron_Free(fVD[i]);
2852 delete [] VD;
2853 delete [] fVD;
2854 value_clear(c);
2856 if (!EP && nvar == 0) {
2857 Value f;
2858 value_init(f);
2859 Param_Vertices *V, *V2;
2860 Matrix* M = Matrix_Alloc(1, P->Dimension+2);
2862 FORALL_PVertex_in_ParamPolyhedron(V, last, PP) {
2863 bool found = false;
2864 FORALL_PVertex_in_ParamPolyhedron(V2, last, PP) {
2865 if (V == V2) {
2866 found = true;
2867 continue;
2869 if (!found)
2870 continue;
2871 for (int i = 0; i < exist; ++i) {
2872 value_oppose(f, V->Vertex->p[i][nparam+1]);
2873 Vector_Combine(V->Vertex->p[i],
2874 V2->Vertex->p[i],
2875 M->p[0] + 1 + nvar + exist,
2876 V2->Vertex->p[i][nparam+1],
2878 nparam+1);
2879 int j;
2880 for (j = 0; j < nparam; ++j)
2881 if (value_notzero_p(M->p[0][1+nvar+exist+j]))
2882 break;
2883 if (j >= nparam)
2884 continue;
2885 ConstraintSimplify(M->p[0], M->p[0],
2886 P->Dimension+2, &f);
2887 value_set_si(M->p[0][0], 0);
2888 Polyhedron *para = AddConstraints(M->p[0], 1, P,
2889 options->MaxRays);
2890 if (emptyQ(para)) {
2891 Polyhedron_Free(para);
2892 continue;
2894 Polyhedron *pos, *neg;
2895 value_set_si(M->p[0][0], 1);
2896 value_decrement(M->p[0][P->Dimension+1],
2897 M->p[0][P->Dimension+1]);
2898 neg = AddConstraints(M->p[0], 1, P, options->MaxRays);
2899 value_set_si(f, -1);
2900 Vector_Scale(M->p[0]+1, M->p[0]+1, f,
2901 P->Dimension+1);
2902 value_decrement(M->p[0][P->Dimension+1],
2903 M->p[0][P->Dimension+1]);
2904 value_decrement(M->p[0][P->Dimension+1],
2905 M->p[0][P->Dimension+1]);
2906 pos = AddConstraints(M->p[0], 1, P, options->MaxRays);
2907 if (emptyQ(neg) && emptyQ(pos)) {
2908 Polyhedron_Free(para);
2909 Polyhedron_Free(pos);
2910 Polyhedron_Free(neg);
2911 continue;
2913 #ifdef DEBUG_ER
2914 fprintf(stderr, "\nER: Order\n");
2915 #endif /* DEBUG_ER */
2916 EP = barvinok_enumerate_e_with_options(para, exist, nparam,
2917 options);
2918 evalue *E;
2919 if (!emptyQ(pos)) {
2920 E = barvinok_enumerate_e_with_options(pos, exist, nparam,
2921 options);
2922 eadd(E, EP);
2923 free_evalue_refs(E);
2924 free(E);
2926 if (!emptyQ(neg)) {
2927 E = barvinok_enumerate_e_with_options(neg, exist, nparam,
2928 options);
2929 eadd(E, EP);
2930 free_evalue_refs(E);
2931 free(E);
2933 Polyhedron_Free(para);
2934 Polyhedron_Free(pos);
2935 Polyhedron_Free(neg);
2936 break;
2938 if (EP)
2939 break;
2940 } END_FORALL_PVertex_in_ParamPolyhedron;
2941 if (EP)
2942 break;
2943 } END_FORALL_PVertex_in_ParamPolyhedron;
2945 if (!EP) {
2946 /* Search for vertex coordinate to split on */
2947 /* First look for one independent of the parameters */
2948 FORALL_PVertex_in_ParamPolyhedron(V, last, PP) {
2949 for (int i = 0; i < exist; ++i) {
2950 int j;
2951 for (j = 0; j < nparam; ++j)
2952 if (value_notzero_p(V->Vertex->p[i][j]))
2953 break;
2954 if (j < nparam)
2955 continue;
2956 value_set_si(M->p[0][0], 1);
2957 Vector_Set(M->p[0]+1, 0, nvar+exist);
2958 Vector_Copy(V->Vertex->p[i],
2959 M->p[0] + 1 + nvar + exist, nparam+1);
2960 value_oppose(M->p[0][1+nvar+i],
2961 V->Vertex->p[i][nparam+1]);
2963 Polyhedron *pos, *neg;
2964 value_set_si(M->p[0][0], 1);
2965 value_decrement(M->p[0][P->Dimension+1],
2966 M->p[0][P->Dimension+1]);
2967 neg = AddConstraints(M->p[0], 1, P, options->MaxRays);
2968 value_set_si(f, -1);
2969 Vector_Scale(M->p[0]+1, M->p[0]+1, f,
2970 P->Dimension+1);
2971 value_decrement(M->p[0][P->Dimension+1],
2972 M->p[0][P->Dimension+1]);
2973 value_decrement(M->p[0][P->Dimension+1],
2974 M->p[0][P->Dimension+1]);
2975 pos = AddConstraints(M->p[0], 1, P, options->MaxRays);
2976 if (emptyQ(neg) || emptyQ(pos)) {
2977 Polyhedron_Free(pos);
2978 Polyhedron_Free(neg);
2979 continue;
2981 Polyhedron_Free(pos);
2982 value_increment(M->p[0][P->Dimension+1],
2983 M->p[0][P->Dimension+1]);
2984 pos = AddConstraints(M->p[0], 1, P, options->MaxRays);
2985 #ifdef DEBUG_ER
2986 fprintf(stderr, "\nER: Vertex\n");
2987 #endif /* DEBUG_ER */
2988 pos->next = neg;
2989 EP = enumerate_or(pos, exist, nparam, options);
2990 break;
2992 if (EP)
2993 break;
2994 } END_FORALL_PVertex_in_ParamPolyhedron;
2997 if (!EP) {
2998 /* Search for vertex coordinate to split on */
2999 /* Now look for one that depends on the parameters */
3000 FORALL_PVertex_in_ParamPolyhedron(V, last, PP) {
3001 for (int i = 0; i < exist; ++i) {
3002 value_set_si(M->p[0][0], 1);
3003 Vector_Set(M->p[0]+1, 0, nvar+exist);
3004 Vector_Copy(V->Vertex->p[i],
3005 M->p[0] + 1 + nvar + exist, nparam+1);
3006 value_oppose(M->p[0][1+nvar+i],
3007 V->Vertex->p[i][nparam+1]);
3009 Polyhedron *pos, *neg;
3010 value_set_si(M->p[0][0], 1);
3011 value_decrement(M->p[0][P->Dimension+1],
3012 M->p[0][P->Dimension+1]);
3013 neg = AddConstraints(M->p[0], 1, P, options->MaxRays);
3014 value_set_si(f, -1);
3015 Vector_Scale(M->p[0]+1, M->p[0]+1, f,
3016 P->Dimension+1);
3017 value_decrement(M->p[0][P->Dimension+1],
3018 M->p[0][P->Dimension+1]);
3019 value_decrement(M->p[0][P->Dimension+1],
3020 M->p[0][P->Dimension+1]);
3021 pos = AddConstraints(M->p[0], 1, P, options->MaxRays);
3022 if (emptyQ(neg) || emptyQ(pos)) {
3023 Polyhedron_Free(pos);
3024 Polyhedron_Free(neg);
3025 continue;
3027 Polyhedron_Free(pos);
3028 value_increment(M->p[0][P->Dimension+1],
3029 M->p[0][P->Dimension+1]);
3030 pos = AddConstraints(M->p[0], 1, P, options->MaxRays);
3031 #ifdef DEBUG_ER
3032 fprintf(stderr, "\nER: ParamVertex\n");
3033 #endif /* DEBUG_ER */
3034 pos->next = neg;
3035 EP = enumerate_or(pos, exist, nparam, options);
3036 break;
3038 if (EP)
3039 break;
3040 } END_FORALL_PVertex_in_ParamPolyhedron;
3043 Matrix_Free(M);
3044 value_clear(f);
3047 if (CEq)
3048 Polyhedron_Free(CEq);
3049 if (CT)
3050 Matrix_Free(CT);
3051 if (PP)
3052 Param_Polyhedron_Free(PP);
3053 *PA = P;
3055 return EP;
3058 evalue* barvinok_enumerate_pip(Polyhedron *P, unsigned exist, unsigned nparam,
3059 unsigned MaxRays)
3061 evalue *E;
3062 barvinok_options *options = barvinok_options_new_with_defaults();
3063 options->MaxRays = MaxRays;
3064 E = barvinok_enumerate_pip_with_options(P, exist, nparam, options);
3065 barvinok_options_free(options);
3066 return E;
3069 #ifndef HAVE_PIPLIB
3070 evalue *barvinok_enumerate_pip_with_options(Polyhedron *P,
3071 unsigned exist, unsigned nparam, struct barvinok_options *options)
3073 return 0;
3075 #else
3076 evalue *barvinok_enumerate_pip_with_options(Polyhedron *P,
3077 unsigned exist, unsigned nparam, struct barvinok_options *options)
3079 int nvar = P->Dimension - exist - nparam;
3080 evalue *EP = evalue_zero();
3081 Polyhedron *Q, *N;
3083 #ifdef DEBUG_ER
3084 fprintf(stderr, "\nER: PIP\n");
3085 #endif /* DEBUG_ER */
3087 Polyhedron *D = pip_projectout(P, nvar, exist, nparam);
3088 for (Q = D; Q; Q = N) {
3089 N = Q->next;
3090 Q->next = 0;
3091 evalue *E;
3092 exist = Q->Dimension - nvar - nparam;
3093 E = barvinok_enumerate_e_with_options(Q, exist, nparam, options);
3094 Polyhedron_Free(Q);
3095 eadd(E, EP);
3096 free_evalue_refs(E);
3097 free(E);
3100 return EP;
3102 #endif
3105 static bool is_single(Value *row, int pos, int len)
3107 return First_Non_Zero(row, pos) == -1 &&
3108 First_Non_Zero(row+pos+1, len-pos-1) == -1;
3111 static evalue* barvinok_enumerate_e_r(Polyhedron *P,
3112 unsigned exist, unsigned nparam, barvinok_options *options);
3114 #ifdef DEBUG_ER
3115 static int er_level = 0;
3117 evalue* barvinok_enumerate_e_with_options(Polyhedron *P,
3118 unsigned exist, unsigned nparam, barvinok_options *options)
3120 fprintf(stderr, "\nER: level %i\n", er_level);
3122 Polyhedron_PrintConstraints(stderr, P_VALUE_FMT, P);
3123 fprintf(stderr, "\nE %d\nP %d\n", exist, nparam);
3124 ++er_level;
3125 P = DomainConstraintSimplify(Polyhedron_Copy(P), options->MaxRays);
3126 evalue *EP = barvinok_enumerate_e_r(P, exist, nparam, options);
3127 Polyhedron_Free(P);
3128 --er_level;
3129 return EP;
3131 #else
3132 evalue* barvinok_enumerate_e_with_options(Polyhedron *P,
3133 unsigned exist, unsigned nparam, barvinok_options *options)
3135 P = DomainConstraintSimplify(Polyhedron_Copy(P), options->MaxRays);
3136 evalue *EP = barvinok_enumerate_e_r(P, exist, nparam, options);
3137 Polyhedron_Free(P);
3138 return EP;
3140 #endif
3142 evalue* barvinok_enumerate_e(Polyhedron *P, unsigned exist, unsigned nparam,
3143 unsigned MaxRays)
3145 evalue *E;
3146 barvinok_options *options = barvinok_options_new_with_defaults();
3147 options->MaxRays = MaxRays;
3148 E = barvinok_enumerate_e_with_options(P, exist, nparam, options);
3149 barvinok_options_free(options);
3150 return E;
3153 static evalue* barvinok_enumerate_e_r(Polyhedron *P,
3154 unsigned exist, unsigned nparam, barvinok_options *options)
3156 if (exist == 0) {
3157 Polyhedron *U = Universe_Polyhedron(nparam);
3158 evalue *EP = barvinok_enumerate_with_options(P, U, options);
3159 //char *param_name[] = {"P", "Q", "R", "S", "T" };
3160 //print_evalue(stdout, EP, param_name);
3161 Polyhedron_Free(U);
3162 return EP;
3165 int nvar = P->Dimension - exist - nparam;
3166 int len = P->Dimension + 2;
3168 /* for now */
3169 POL_ENSURE_FACETS(P);
3170 POL_ENSURE_VERTICES(P);
3172 if (emptyQ(P))
3173 return evalue_zero();
3175 if (nvar == 0 && nparam == 0) {
3176 evalue *EP = evalue_zero();
3177 barvinok_count_with_options(P, &EP->x.n, options);
3178 if (value_pos_p(EP->x.n))
3179 value_set_si(EP->x.n, 1);
3180 return EP;
3183 int r;
3184 for (r = 0; r < P->NbRays; ++r)
3185 if (value_zero_p(P->Ray[r][0]) ||
3186 value_zero_p(P->Ray[r][P->Dimension+1])) {
3187 int i;
3188 for (i = 0; i < nvar; ++i)
3189 if (value_notzero_p(P->Ray[r][i+1]))
3190 break;
3191 if (i >= nvar)
3192 continue;
3193 for (i = nvar + exist; i < nvar + exist + nparam; ++i)
3194 if (value_notzero_p(P->Ray[r][i+1]))
3195 break;
3196 if (i >= nvar + exist + nparam)
3197 break;
3199 if (r < P->NbRays) {
3200 evalue *EP = evalue_zero();
3201 value_set_si(EP->x.n, -1);
3202 return EP;
3205 int first;
3206 for (r = 0; r < P->NbEq; ++r)
3207 if ((first = First_Non_Zero(P->Constraint[r]+1+nvar, exist)) != -1)
3208 break;
3209 if (r < P->NbEq) {
3210 if (First_Non_Zero(P->Constraint[r]+1+nvar+first+1,
3211 exist-first-1) != -1) {
3212 Polyhedron *T = rotate_along(P, r, nvar, exist, options->MaxRays);
3213 #ifdef DEBUG_ER
3214 fprintf(stderr, "\nER: Equality\n");
3215 #endif /* DEBUG_ER */
3216 evalue *EP = barvinok_enumerate_e_with_options(T, exist-1, nparam,
3217 options);
3218 Polyhedron_Free(T);
3219 return EP;
3220 } else {
3221 #ifdef DEBUG_ER
3222 fprintf(stderr, "\nER: Fixed\n");
3223 #endif /* DEBUG_ER */
3224 if (first == 0)
3225 return barvinok_enumerate_e_with_options(P, exist-1, nparam,
3226 options);
3227 else {
3228 Polyhedron *T = Polyhedron_Copy(P);
3229 SwapColumns(T, nvar+1, nvar+1+first);
3230 evalue *EP = barvinok_enumerate_e_with_options(T, exist-1, nparam,
3231 options);
3232 Polyhedron_Free(T);
3233 return EP;
3238 Vector *row = Vector_Alloc(len);
3239 value_set_si(row->p[0], 1);
3241 Value f;
3242 value_init(f);
3244 enum constraint* info = new constraint[exist];
3245 for (int i = 0; i < exist; ++i) {
3246 info[i] = ALL_POS;
3247 for (int l = P->NbEq; l < P->NbConstraints; ++l) {
3248 if (value_negz_p(P->Constraint[l][nvar+i+1]))
3249 continue;
3250 bool l_parallel = is_single(P->Constraint[l]+nvar+1, i, exist);
3251 for (int u = P->NbEq; u < P->NbConstraints; ++u) {
3252 if (value_posz_p(P->Constraint[u][nvar+i+1]))
3253 continue;
3254 bool lu_parallel = l_parallel ||
3255 is_single(P->Constraint[u]+nvar+1, i, exist);
3256 value_oppose(f, P->Constraint[u][nvar+i+1]);
3257 Vector_Combine(P->Constraint[l]+1, P->Constraint[u]+1, row->p+1,
3258 f, P->Constraint[l][nvar+i+1], len-1);
3259 if (!(info[i] & INDEPENDENT)) {
3260 int j;
3261 for (j = 0; j < exist; ++j)
3262 if (j != i && value_notzero_p(row->p[nvar+j+1]))
3263 break;
3264 if (j == exist) {
3265 //printf("independent: i: %d, l: %d, u: %d\n", i, l, u);
3266 info[i] = (constraint)(info[i] | INDEPENDENT);
3269 if (info[i] & ALL_POS) {
3270 value_addto(row->p[len-1], row->p[len-1],
3271 P->Constraint[l][nvar+i+1]);
3272 value_addto(row->p[len-1], row->p[len-1], f);
3273 value_multiply(f, f, P->Constraint[l][nvar+i+1]);
3274 value_subtract(row->p[len-1], row->p[len-1], f);
3275 value_decrement(row->p[len-1], row->p[len-1]);
3276 ConstraintSimplify(row->p, row->p, len, &f);
3277 value_set_si(f, -1);
3278 Vector_Scale(row->p+1, row->p+1, f, len-1);
3279 value_decrement(row->p[len-1], row->p[len-1]);
3280 Polyhedron *T = AddConstraints(row->p, 1, P, options->MaxRays);
3281 if (!emptyQ(T)) {
3282 //printf("not all_pos: i: %d, l: %d, u: %d\n", i, l, u);
3283 info[i] = (constraint)(info[i] ^ ALL_POS);
3285 //puts("pos remainder");
3286 //Polyhedron_Print(stdout, P_VALUE_FMT, T);
3287 Polyhedron_Free(T);
3289 if (!(info[i] & ONE_NEG)) {
3290 if (lu_parallel) {
3291 negative_test_constraint(P->Constraint[l],
3292 P->Constraint[u],
3293 row->p, nvar+i, len, &f);
3294 oppose_constraint(row->p, len, &f);
3295 Polyhedron *T = AddConstraints(row->p, 1, P,
3296 options->MaxRays);
3297 if (emptyQ(T)) {
3298 //printf("one_neg i: %d, l: %d, u: %d\n", i, l, u);
3299 info[i] = (constraint)(info[i] | ONE_NEG);
3301 //puts("neg remainder");
3302 //Polyhedron_Print(stdout, P_VALUE_FMT, T);
3303 Polyhedron_Free(T);
3304 } else if (!(info[i] & ROT_NEG)) {
3305 if (parallel_constraints(P->Constraint[l],
3306 P->Constraint[u],
3307 row->p, nvar, exist)) {
3308 negative_test_constraint7(P->Constraint[l],
3309 P->Constraint[u],
3310 row->p, nvar, exist,
3311 len, &f);
3312 oppose_constraint(row->p, len, &f);
3313 Polyhedron *T = AddConstraints(row->p, 1, P,
3314 options->MaxRays);
3315 if (emptyQ(T)) {
3316 // printf("rot_neg i: %d, l: %d, u: %d\n", i, l, u);
3317 info[i] = (constraint)(info[i] | ROT_NEG);
3318 r = l;
3320 //puts("neg remainder");
3321 //Polyhedron_Print(stdout, P_VALUE_FMT, T);
3322 Polyhedron_Free(T);
3326 if (!(info[i] & ALL_POS) && (info[i] & (ONE_NEG | ROT_NEG)))
3327 goto next;
3330 if (info[i] & ALL_POS)
3331 break;
3332 next:
3337 for (int i = 0; i < exist; ++i)
3338 printf("%i: %i\n", i, info[i]);
3340 for (int i = 0; i < exist; ++i)
3341 if (info[i] & ALL_POS) {
3342 #ifdef DEBUG_ER
3343 fprintf(stderr, "\nER: Positive\n");
3344 #endif /* DEBUG_ER */
3345 // Eliminate
3346 // Maybe we should chew off some of the fat here
3347 Matrix *M = Matrix_Alloc(P->Dimension, P->Dimension+1);
3348 for (int j = 0; j < P->Dimension; ++j)
3349 value_set_si(M->p[j][j + (j >= i+nvar)], 1);
3350 Polyhedron *T = Polyhedron_Image(P, M, options->MaxRays);
3351 Matrix_Free(M);
3352 evalue *EP = barvinok_enumerate_e_with_options(T, exist-1, nparam,
3353 options);
3354 Polyhedron_Free(T);
3355 value_clear(f);
3356 Vector_Free(row);
3357 delete [] info;
3358 return EP;
3360 for (int i = 0; i < exist; ++i)
3361 if (info[i] & ONE_NEG) {
3362 #ifdef DEBUG_ER
3363 fprintf(stderr, "\nER: Negative\n");
3364 #endif /* DEBUG_ER */
3365 Vector_Free(row);
3366 value_clear(f);
3367 delete [] info;
3368 if (i == 0)
3369 return barvinok_enumerate_e_with_options(P, exist-1, nparam,
3370 options);
3371 else {
3372 Polyhedron *T = Polyhedron_Copy(P);
3373 SwapColumns(T, nvar+1, nvar+1+i);
3374 evalue *EP = barvinok_enumerate_e_with_options(T, exist-1, nparam,
3375 options);
3376 Polyhedron_Free(T);
3377 return EP;
3380 for (int i = 0; i < exist; ++i)
3381 if (info[i] & ROT_NEG) {
3382 #ifdef DEBUG_ER
3383 fprintf(stderr, "\nER: Rotate\n");
3384 #endif /* DEBUG_ER */
3385 Vector_Free(row);
3386 value_clear(f);
3387 delete [] info;
3388 Polyhedron *T = rotate_along(P, r, nvar, exist, options->MaxRays);
3389 evalue *EP = barvinok_enumerate_e_with_options(T, exist-1, nparam,
3390 options);
3391 Polyhedron_Free(T);
3392 return EP;
3394 for (int i = 0; i < exist; ++i)
3395 if (info[i] & INDEPENDENT) {
3396 Polyhedron *pos, *neg;
3398 /* Find constraint again and split off negative part */
3400 if (SplitOnVar(P, i, nvar, exist, options->MaxRays,
3401 row, f, true, &pos, &neg)) {
3402 #ifdef DEBUG_ER
3403 fprintf(stderr, "\nER: Split\n");
3404 #endif /* DEBUG_ER */
3406 evalue *EP =
3407 barvinok_enumerate_e_with_options(neg, exist-1, nparam, options);
3408 evalue *E =
3409 barvinok_enumerate_e_with_options(pos, exist, nparam, options);
3410 eadd(E, EP);
3411 free_evalue_refs(E);
3412 free(E);
3413 Polyhedron_Free(neg);
3414 Polyhedron_Free(pos);
3415 value_clear(f);
3416 Vector_Free(row);
3417 delete [] info;
3418 return EP;
3421 delete [] info;
3423 Polyhedron *O = P;
3424 Polyhedron *F;
3426 evalue *EP;
3428 EP = enumerate_line(P, exist, nparam, options);
3429 if (EP)
3430 goto out;
3432 EP = barvinok_enumerate_pip_with_options(P, exist, nparam, options);
3433 if (EP)
3434 goto out;
3436 EP = enumerate_redundant_ray(P, exist, nparam, options);
3437 if (EP)
3438 goto out;
3440 EP = enumerate_sure(P, exist, nparam, options);
3441 if (EP)
3442 goto out;
3444 EP = enumerate_ray(P, exist, nparam, options);
3445 if (EP)
3446 goto out;
3448 EP = enumerate_sure2(P, exist, nparam, options);
3449 if (EP)
3450 goto out;
3452 F = unfringe(P, options->MaxRays);
3453 if (!PolyhedronIncludes(F, P)) {
3454 #ifdef DEBUG_ER
3455 fprintf(stderr, "\nER: Fringed\n");
3456 #endif /* DEBUG_ER */
3457 EP = barvinok_enumerate_e_with_options(F, exist, nparam, options);
3458 Polyhedron_Free(F);
3459 goto out;
3461 Polyhedron_Free(F);
3463 if (nparam)
3464 EP = enumerate_vd(&P, exist, nparam, options);
3465 if (EP)
3466 goto out2;
3468 if (nvar != 0) {
3469 EP = enumerate_sum(P, exist, nparam, options);
3470 goto out2;
3473 assert(nvar == 0);
3475 int i;
3476 Polyhedron *pos, *neg;
3477 for (i = 0; i < exist; ++i)
3478 if (SplitOnVar(P, i, nvar, exist, options->MaxRays,
3479 row, f, false, &pos, &neg))
3480 break;
3482 assert (i < exist);
3484 pos->next = neg;
3485 EP = enumerate_or(pos, exist, nparam, options);
3487 out2:
3488 if (O != P)
3489 Polyhedron_Free(P);
3491 out:
3492 value_clear(f);
3493 Vector_Free(row);
3494 return EP;
3498 * remove equalities that require a "compression" of the parameters
3500 static Polyhedron *remove_more_equalities(Polyhedron *P, unsigned nparam,
3501 Matrix **CP, unsigned MaxRays)
3503 Polyhedron *Q = P;
3504 remove_all_equalities(&P, NULL, CP, NULL, nparam, MaxRays);
3505 if (P != Q)
3506 Polyhedron_Free(Q);
3507 return P;
3510 /* frees P */
3511 static gen_fun *series(Polyhedron *P, unsigned nparam, barvinok_options *options)
3513 Matrix *CP = NULL;
3514 gen_fun *gf;
3516 if (emptyQ2(P)) {
3517 Polyhedron_Free(P);
3518 return new gen_fun;
3521 assert(!Polyhedron_is_unbounded(P, nparam, options->MaxRays));
3522 assert(P->NbBid == 0);
3523 assert(Polyhedron_has_revlex_positive_rays(P, nparam));
3524 if (P->NbEq != 0)
3525 P = remove_more_equalities(P, nparam, &CP, options->MaxRays);
3526 assert(P->NbEq == 0);
3527 if (CP)
3528 nparam = CP->NbColumns-1;
3530 if (nparam == 0) {
3531 Value c;
3532 value_init(c);
3533 barvinok_count_with_options(P, &c, options);
3534 gf = new gen_fun(c);
3535 value_clear(c);
3536 } else {
3537 gf_base *red;
3538 red = gf_base::create(Polyhedron_Project(P, nparam),
3539 P->Dimension, nparam, options);
3540 POL_ENSURE_VERTICES(P);
3541 red->start_gf(P, options);
3542 gf = red->gf;
3543 delete red;
3545 if (CP) {
3546 gf->substitute(CP);
3547 Matrix_Free(CP);
3549 Polyhedron_Free(P);
3550 return gf;
3553 gen_fun * barvinok_series_with_options(Polyhedron *P, Polyhedron* C,
3554 barvinok_options *options)
3556 Polyhedron *CA;
3557 unsigned nparam = C->Dimension;
3558 gen_fun *gf;
3560 CA = align_context(C, P->Dimension, options->MaxRays);
3561 P = DomainIntersection(P, CA, options->MaxRays);
3562 Polyhedron_Free(CA);
3564 gf = series(P, nparam, options);
3566 return gf;
3569 gen_fun * barvinok_series(Polyhedron *P, Polyhedron* C, unsigned MaxRays)
3571 gen_fun *gf;
3572 barvinok_options *options = barvinok_options_new_with_defaults();
3573 options->MaxRays = MaxRays;
3574 gf = barvinok_series_with_options(P, C, options);
3575 barvinok_options_free(options);
3576 return gf;
3579 static Polyhedron *skew_into_positive_orthant(Polyhedron *D, unsigned nparam,
3580 unsigned MaxRays)
3582 Matrix *M = NULL;
3583 Value tmp;
3584 value_init(tmp);
3585 for (Polyhedron *P = D; P; P = P->next) {
3586 POL_ENSURE_VERTICES(P);
3587 assert(!Polyhedron_is_unbounded(P, nparam, MaxRays));
3588 assert(P->NbBid == 0);
3589 assert(Polyhedron_has_positive_rays(P, nparam));
3591 for (int r = 0; r < P->NbRays; ++r) {
3592 if (value_notzero_p(P->Ray[r][P->Dimension+1]))
3593 continue;
3594 for (int i = 0; i < nparam; ++i) {
3595 int j;
3596 if (value_posz_p(P->Ray[r][i+1]))
3597 continue;
3598 if (!M) {
3599 M = Matrix_Alloc(D->Dimension+1, D->Dimension+1);
3600 for (int i = 0; i < D->Dimension+1; ++i)
3601 value_set_si(M->p[i][i], 1);
3602 } else {
3603 Inner_Product(P->Ray[r]+1, M->p[i], D->Dimension+1, &tmp);
3604 if (value_posz_p(tmp))
3605 continue;
3607 for (j = P->Dimension - nparam; j < P->Dimension; ++j)
3608 if (value_pos_p(P->Ray[r][j+1]))
3609 break;
3610 assert(j < P->Dimension);
3611 value_pdivision(tmp, P->Ray[r][j+1], P->Ray[r][i+1]);
3612 value_subtract(M->p[i][j], M->p[i][j], tmp);
3616 value_clear(tmp);
3617 if (M) {
3618 D = DomainImage(D, M, MaxRays);
3619 Matrix_Free(M);
3621 return D;
3624 gen_fun* barvinok_enumerate_union_series_with_options(Polyhedron *D, Polyhedron* C,
3625 barvinok_options *options)
3627 Polyhedron *conv, *D2;
3628 Polyhedron *CA;
3629 gen_fun *gf = NULL, *gf2;
3630 unsigned nparam = C->Dimension;
3631 ZZ one, mone;
3632 one = 1;
3633 mone = -1;
3635 CA = align_context(C, D->Dimension, options->MaxRays);
3636 D = DomainIntersection(D, CA, options->MaxRays);
3637 Polyhedron_Free(CA);
3639 D2 = skew_into_positive_orthant(D, nparam, options->MaxRays);
3640 for (Polyhedron *P = D2; P; P = P->next) {
3641 assert(P->Dimension == D2->Dimension);
3642 gen_fun *P_gf;
3644 P_gf = series(Polyhedron_Copy(P), nparam, options);
3645 if (!gf)
3646 gf = P_gf;
3647 else {
3648 gf->add_union(P_gf, options);
3649 delete P_gf;
3652 /* we actually only need the convex union of the parameter space
3653 * but the reducer classes currently expect a polyhedron in
3654 * the combined space
3656 Polyhedron_Free(gf->context);
3657 gf->context = DomainConvex(D2, options->MaxRays);
3659 gf2 = gf->summate(D2->Dimension - nparam, options);
3661 delete gf;
3662 if (D != D2)
3663 Domain_Free(D2);
3664 Domain_Free(D);
3665 return gf2;
3668 gen_fun* barvinok_enumerate_union_series(Polyhedron *D, Polyhedron* C,
3669 unsigned MaxRays)
3671 gen_fun *gf;
3672 barvinok_options *options = barvinok_options_new_with_defaults();
3673 options->MaxRays = MaxRays;
3674 gf = barvinok_enumerate_union_series_with_options(D, C, options);
3675 barvinok_options_free(options);
3676 return gf;
3679 evalue* barvinok_enumerate_union(Polyhedron *D, Polyhedron* C, unsigned MaxRays)
3681 evalue *EP;
3682 gen_fun *gf = barvinok_enumerate_union_series(D, C, MaxRays);
3683 EP = *gf;
3684 delete gf;
3685 return EP;