add denominator to dpoly_r and split div method
[barvinok.git] / barvinok.cc
blob9953353813e94e4028ac1604a1e5a036162ab190
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 <util.h>
11 extern "C" {
12 #include <polylib/polylibgmp.h>
13 #include "ev_operations.h"
14 #include "piputil.h"
16 #include "config.h"
17 #include <barvinok.h>
18 #include <genfun.h>
20 #ifdef NTL_STD_CXX
21 using namespace NTL;
22 #endif
23 using std::cout;
24 using std::endl;
25 using std::vector;
26 using std::deque;
27 using std::string;
28 using std::ostringstream;
30 #define ALLOC(p) (((long *) (p))[0])
31 #define SIZE(p) (((long *) (p))[1])
32 #define DATA(p) ((mp_limb_t *) (((long *) (p)) + 2))
34 static void value2zz(Value v, ZZ& z)
36 int sa = v[0]._mp_size;
37 int abs_sa = sa < 0 ? -sa : sa;
39 _ntl_gsetlength(&z.rep, abs_sa);
40 mp_limb_t * adata = DATA(z.rep);
41 for (int i = 0; i < abs_sa; ++i)
42 adata[i] = v[0]._mp_d[i];
43 SIZE(z.rep) = sa;
46 void zz2value(ZZ& z, Value& v)
48 if (!z.rep) {
49 value_set_si(v, 0);
50 return;
53 int sa = SIZE(z.rep);
54 int abs_sa = sa < 0 ? -sa : sa;
56 mp_limb_t * adata = DATA(z.rep);
57 _mpz_realloc(v, abs_sa);
58 for (int i = 0; i < abs_sa; ++i)
59 v[0]._mp_d[i] = adata[i];
60 v[0]._mp_size = sa;
63 #undef ALLOC
64 #define ALLOC(t,p) p = (t*)malloc(sizeof(*p))
67 * We just ignore the last column and row
68 * If the final element is not equal to one
69 * then the result will actually be a multiple of the input
71 static void matrix2zz(Matrix *M, mat_ZZ& m, unsigned nr, unsigned nc)
73 m.SetDims(nr, nc);
75 for (int i = 0; i < nr; ++i) {
76 // assert(value_one_p(M->p[i][M->NbColumns - 1]));
77 for (int j = 0; j < nc; ++j) {
78 value2zz(M->p[i][j], m[i][j]);
83 static void values2zz(Value *p, vec_ZZ& v, int len)
85 v.SetLength(len);
87 for (int i = 0; i < len; ++i) {
88 value2zz(p[i], v[i]);
94 static void zz2values(vec_ZZ& v, Value *p)
96 for (int i = 0; i < v.length(); ++i)
97 zz2value(v[i], p[i]);
100 static void rays(mat_ZZ& r, Polyhedron *C)
102 unsigned dim = C->NbRays - 1; /* don't count zero vertex */
103 assert(C->NbRays - 1 == C->Dimension);
104 r.SetDims(dim, dim);
105 ZZ tmp;
107 int i, c;
108 for (i = 0, c = 0; i < dim; ++i)
109 if (value_zero_p(C->Ray[i][dim+1])) {
110 for (int j = 0; j < dim; ++j) {
111 value2zz(C->Ray[i][j+1], tmp);
112 r[j][c] = tmp;
114 ++c;
118 static Matrix * rays(Polyhedron *C)
120 unsigned dim = C->NbRays - 1; /* don't count zero vertex */
121 assert(C->NbRays - 1 == C->Dimension);
123 Matrix *M = Matrix_Alloc(dim+1, dim+1);
124 assert(M);
126 int i, c;
127 for (i = 0, c = 0; i <= dim && c < dim; ++i)
128 if (value_zero_p(C->Ray[i][dim+1])) {
129 Vector_Copy(C->Ray[i] + 1, M->p[c], dim);
130 value_set_si(M->p[c++][dim], 0);
132 assert(c == dim);
133 value_set_si(M->p[dim][dim], 1);
135 return M;
138 static Matrix * rays2(Polyhedron *C)
140 unsigned dim = C->NbRays - 1; /* don't count zero vertex */
141 assert(C->NbRays - 1 == C->Dimension);
143 Matrix *M = Matrix_Alloc(dim, dim);
144 assert(M);
146 int i, c;
147 for (i = 0, c = 0; i <= dim && c < dim; ++i)
148 if (value_zero_p(C->Ray[i][dim+1]))
149 Vector_Copy(C->Ray[i] + 1, M->p[c++], dim);
150 assert(c == dim);
152 return M;
156 * Returns the largest absolute value in the vector
158 static ZZ max(vec_ZZ& v)
160 ZZ max = abs(v[0]);
161 for (int i = 1; i < v.length(); ++i)
162 if (abs(v[i]) > max)
163 max = abs(v[i]);
164 return max;
167 class cone {
168 public:
169 cone(Matrix *M) {
170 Cone = 0;
171 Rays = Matrix_Copy(M);
172 set_det();
174 cone(Polyhedron *C) {
175 Cone = Polyhedron_Copy(C);
176 Rays = rays(C);
177 set_det();
179 void set_det() {
180 mat_ZZ A;
181 matrix2zz(Rays, A, Rays->NbRows - 1, Rays->NbColumns - 1);
182 det = determinant(A);
185 Vector* short_vector(vec_ZZ& lambda) {
186 Matrix *M = Matrix_Copy(Rays);
187 Matrix *inv = Matrix_Alloc(M->NbRows, M->NbColumns);
188 int ok = Matrix_Inverse(M, inv);
189 assert(ok);
190 Matrix_Free(M);
192 ZZ det2;
193 mat_ZZ B;
194 mat_ZZ U;
195 matrix2zz(inv, B, inv->NbRows - 1, inv->NbColumns - 1);
196 long r = LLL(det2, B, U);
198 ZZ min = max(B[0]);
199 int index = 0;
200 for (int i = 1; i < B.NumRows(); ++i) {
201 ZZ tmp = max(B[i]);
202 if (tmp < min) {
203 min = tmp;
204 index = i;
208 Matrix_Free(inv);
210 lambda = B[index];
212 Vector *z = Vector_Alloc(U[index].length()+1);
213 assert(z);
214 zz2values(U[index], z->p);
215 value_set_si(z->p[U[index].length()], 0);
217 Value tmp;
218 value_init(tmp);
219 Polyhedron *C = poly();
220 int i;
221 for (i = 0; i < C->NbConstraints; ++i) {
222 Inner_Product(z->p, C->Constraint[i]+1, z->Size-1, &tmp);
223 if (value_pos_p(tmp))
224 break;
226 if (i == C->NbConstraints) {
227 value_set_si(tmp, -1);
228 Vector_Scale(z->p, z->p, tmp, z->Size-1);
230 value_clear(tmp);
231 return z;
234 ~cone() {
235 Polyhedron_Free(Cone);
236 Matrix_Free(Rays);
239 Polyhedron *poly() {
240 if (!Cone) {
241 Matrix *M = Matrix_Alloc(Rays->NbRows+1, Rays->NbColumns+1);
242 for (int i = 0; i < Rays->NbRows; ++i) {
243 Vector_Copy(Rays->p[i], M->p[i]+1, Rays->NbColumns);
244 value_set_si(M->p[i][0], 1);
246 Vector_Set(M->p[Rays->NbRows]+1, 0, Rays->NbColumns-1);
247 value_set_si(M->p[Rays->NbRows][0], 1);
248 value_set_si(M->p[Rays->NbRows][Rays->NbColumns], 1);
249 Cone = Rays2Polyhedron(M, M->NbRows+1);
250 assert(Cone->NbConstraints == Cone->NbRays);
251 Matrix_Free(M);
253 return Cone;
256 ZZ det;
257 Polyhedron *Cone;
258 Matrix *Rays;
261 class dpoly {
262 public:
263 vec_ZZ coeff;
264 dpoly(int d, ZZ& degree, int offset = 0) {
265 coeff.SetLength(d+1);
267 int min = d + offset;
268 if (degree >= 0 && degree < ZZ(INIT_VAL, min))
269 min = to_int(degree);
271 ZZ c = ZZ(INIT_VAL, 1);
272 if (!offset)
273 coeff[0] = c;
274 for (int i = 1; i <= min; ++i) {
275 c *= (degree -i + 1);
276 c /= i;
277 coeff[i-offset] = c;
280 void operator *= (dpoly& f) {
281 assert(coeff.length() == f.coeff.length());
282 vec_ZZ old = coeff;
283 coeff = f.coeff[0] * coeff;
284 for (int i = 1; i < coeff.length(); ++i)
285 for (int j = 0; i+j < coeff.length(); ++j)
286 coeff[i+j] += f.coeff[i] * old[j];
288 void div(dpoly& d, mpq_t count, ZZ& sign) {
289 int len = coeff.length();
290 Value tmp;
291 value_init(tmp);
292 mpq_t* c = new mpq_t[coeff.length()];
293 mpq_t qtmp;
294 mpq_init(qtmp);
295 for (int i = 0; i < len; ++i) {
296 mpq_init(c[i]);
297 zz2value(coeff[i], tmp);
298 mpq_set_z(c[i], tmp);
300 for (int j = 1; j <= i; ++j) {
301 zz2value(d.coeff[j], tmp);
302 mpq_set_z(qtmp, tmp);
303 mpq_mul(qtmp, qtmp, c[i-j]);
304 mpq_sub(c[i], c[i], qtmp);
307 zz2value(d.coeff[0], tmp);
308 mpq_set_z(qtmp, tmp);
309 mpq_div(c[i], c[i], qtmp);
311 if (sign == -1)
312 mpq_sub(count, count, c[len-1]);
313 else
314 mpq_add(count, count, c[len-1]);
316 value_clear(tmp);
317 mpq_clear(qtmp);
318 for (int i = 0; i < len; ++i)
319 mpq_clear(c[i]);
320 delete [] c;
324 class dpoly_n {
325 public:
326 Matrix *coeff;
327 ~dpoly_n() {
328 Matrix_Free(coeff);
330 dpoly_n(int d, ZZ& degree_0, ZZ& degree_1, int offset = 0) {
331 Value d0, d1;
332 value_init(d0);
333 value_init(d1);
334 zz2value(degree_0, d0);
335 zz2value(degree_1, d1);
336 coeff = Matrix_Alloc(d+1, d+1+1);
337 value_set_si(coeff->p[0][0], 1);
338 value_set_si(coeff->p[0][d+1], 1);
339 for (int i = 1; i <= d; ++i) {
340 value_multiply(coeff->p[i][0], coeff->p[i-1][0], d0);
341 Vector_Combine(coeff->p[i-1], coeff->p[i-1]+1, coeff->p[i]+1,
342 d1, d0, i);
343 value_set_si(coeff->p[i][d+1], i);
344 value_multiply(coeff->p[i][d+1], coeff->p[i][d+1], coeff->p[i-1][d+1]);
345 value_decrement(d0, d0);
347 value_clear(d0);
348 value_clear(d1);
350 void div(dpoly& d, Vector *count, ZZ& sign) {
351 int len = coeff->NbRows;
352 Matrix * c = Matrix_Alloc(coeff->NbRows, coeff->NbColumns);
353 Value tmp;
354 value_init(tmp);
355 for (int i = 0; i < len; ++i) {
356 Vector_Copy(coeff->p[i], c->p[i], len+1);
357 for (int j = 1; j <= i; ++j) {
358 zz2value(d.coeff[j], tmp);
359 value_multiply(tmp, tmp, c->p[i][len]);
360 value_oppose(tmp, tmp);
361 Vector_Combine(c->p[i], c->p[i-j], c->p[i],
362 c->p[i-j][len], tmp, len);
363 value_multiply(c->p[i][len], c->p[i][len], c->p[i-j][len]);
365 zz2value(d.coeff[0], tmp);
366 value_multiply(c->p[i][len], c->p[i][len], tmp);
368 if (sign == -1) {
369 value_set_si(tmp, -1);
370 Vector_Scale(c->p[len-1], count->p, tmp, len);
371 value_assign(count->p[len], c->p[len-1][len]);
372 } else
373 Vector_Copy(c->p[len-1], count->p, len+1);
374 Vector_Normalize(count->p, len+1);
375 value_clear(tmp);
376 Matrix_Free(c);
380 struct dpoly_r_term {
381 int *powers;
382 ZZ coeff;
385 /* len: number of elements in c
386 * each element in c is the coefficient of a power of t
387 * in the MacLaurin expansion
389 struct dpoly_r {
390 vector< dpoly_r_term * > *c;
391 int len;
392 int dim;
393 ZZ denom;
395 void add_term(int i, int * powers, ZZ& coeff) {
396 for (int k = 0; k < c[i].size(); ++k) {
397 if (memcmp(c[i][k]->powers, powers, dim * sizeof(int)) == 0) {
398 c[i][k]->coeff += coeff;
399 return;
402 dpoly_r_term *t = new dpoly_r_term;
403 t->powers = new int[dim];
404 memcpy(t->powers, powers, dim * sizeof(int));
405 t->coeff = coeff;
406 c[i].push_back(t);
408 dpoly_r(int len, int dim) {
409 denom = 1;
410 this->len = len;
411 this->dim = dim;
412 c = new vector< dpoly_r_term * > [len];
414 dpoly_r(dpoly& num, dpoly& den, int pos, int sign, int dim) {
415 denom = 1;
416 len = num.coeff.length();
417 c = new vector< dpoly_r_term * > [len];
418 this->dim = dim;
419 int powers[dim];
421 for (int i = 0; i < len; ++i) {
422 ZZ coeff = num.coeff[i];
423 memset(powers, 0, dim * sizeof(int));
424 powers[pos] = sign;
426 add_term(i, powers, coeff);
428 for (int j = 1; j <= i; ++j) {
429 for (int k = 0; k < c[i-j].size(); ++k) {
430 memcpy(powers, c[i-j][k]->powers, dim*sizeof(int));
431 powers[pos] += sign;
432 coeff = -den.coeff[j-1] * c[i-j][k]->coeff;
433 add_term(i, powers, coeff);
437 //dump();
439 dpoly_r *div(dpoly& d) {
440 dpoly_r *rc = new dpoly_r(len, dim);
441 rc->denom = power(d.coeff[0], len+1);
442 ZZ cur_d = rc->denom;
443 ZZ coeff;
445 for (int i = 0; i < len; ++i) {
446 cur_d /= d.coeff[0];
448 for (int k = 0; k < c[i].size(); ++k) {
449 coeff = c[i][k]->coeff * cur_d;
450 rc->add_term(i, c[i][k]->powers, coeff);
453 for (int j = 1; j <= i; ++j) {
454 for (int k = 0; k < rc->c[i-j].size(); ++k) {
455 coeff = - d.coeff[j] * rc->c[i-j][k]->coeff / d.coeff[0];
456 rc->add_term(i, rc->c[i-j][k]->powers, coeff);
460 return rc;
462 void div(dpoly& d, ZZ& sign, gen_fun *gf, mat_ZZ& pden, mat_ZZ& den,
463 vec_ZZ& num_p) {
464 dpoly_r * rc = div(d);
465 //rc.dump();
466 int common = pden.NumRows();
468 vector< dpoly_r_term * >& final = rc->c[len-1];
469 int rows;
470 for (int j = 0; j < final.size(); ++j) {
471 rows = common;
472 pden.SetDims(rows, pden.NumCols());
473 for (int k = 0; k < dim; ++k) {
474 int n = final[j]->powers[k];
475 if (n == 0)
476 continue;
477 int abs_n = n < 0 ? -n : n;
478 pden.SetDims(rows+abs_n, pden.NumCols());
479 for (int l = 0; l < abs_n; ++l) {
480 if (n > 0)
481 pden[rows+l] = den[k];
482 else
483 pden[rows+l] = -den[k];
485 rows += abs_n;
487 gf->add(final[j]->coeff, rc->denom, num_p, pden);
489 delete rc;
491 void dump(void) {
492 for (int i = 0; i < len; ++i) {
493 cout << endl;
494 cout << i << endl;
495 cout << c[i].size() << endl;
496 for (int j = 0; j < c[i].size(); ++j) {
497 for (int k = 0; k < dim; ++k) {
498 cout << c[i][j]->powers[k] << " ";
500 cout << ": " << c[i][j]->coeff << endl;
502 cout << endl;
507 struct decomposer {
508 void decompose(Polyhedron *C);
509 virtual void handle(Polyhedron *P, int sign) = 0;
512 struct polar_decomposer : public decomposer {
513 void decompose(Polyhedron *C, unsigned MaxRays);
514 virtual void handle(Polyhedron *P, int sign);
515 virtual void handle_polar(Polyhedron *P, int sign) = 0;
518 void decomposer::decompose(Polyhedron *C)
520 vector<cone *> nonuni;
521 cone * c = new cone(C);
522 ZZ det = c->det;
523 int s = sign(det);
524 assert(det != 0);
525 if (abs(det) > 1) {
526 nonuni.push_back(c);
527 } else {
528 handle(C, 1);
529 delete c;
531 vec_ZZ lambda;
532 while (!nonuni.empty()) {
533 c = nonuni.back();
534 nonuni.pop_back();
535 Vector* v = c->short_vector(lambda);
536 for (int i = 0; i < c->Rays->NbRows - 1; ++i) {
537 if (lambda[i] == 0)
538 continue;
539 Matrix* M = Matrix_Copy(c->Rays);
540 Vector_Copy(v->p, M->p[i], v->Size);
541 cone * pc = new cone(M);
542 assert (pc->det != 0);
543 if (abs(pc->det) > 1) {
544 assert(abs(pc->det) < abs(c->det));
545 nonuni.push_back(pc);
546 } else {
547 handle(pc->poly(), sign(pc->det) * s);
548 delete pc;
550 Matrix_Free(M);
552 Vector_Free(v);
553 delete c;
557 void polar_decomposer::decompose(Polyhedron *cone, unsigned MaxRays)
559 Polyhedron_Polarize(cone);
560 if (cone->NbRays - 1 != cone->Dimension) {
561 Polyhedron *tmp = cone;
562 cone = triangularize_cone(cone, MaxRays);
563 Polyhedron_Free(tmp);
565 for (Polyhedron *Polar = cone; Polar; Polar = Polar->next)
566 decomposer::decompose(Polar);
567 Domain_Free(cone);
570 void polar_decomposer::handle(Polyhedron *P, int sign)
572 Polyhedron_Polarize(P);
573 handle_polar(P, sign);
577 * Barvinok's Decomposition of a simplicial cone
579 * Returns two lists of polyhedra
581 void barvinok_decompose(Polyhedron *C, Polyhedron **ppos, Polyhedron **pneg)
583 Polyhedron *pos = *ppos, *neg = *pneg;
584 vector<cone *> nonuni;
585 cone * c = new cone(C);
586 ZZ det = c->det;
587 int s = sign(det);
588 assert(det != 0);
589 if (abs(det) > 1) {
590 nonuni.push_back(c);
591 } else {
592 Polyhedron *p = Polyhedron_Copy(c->Cone);
593 p->next = pos;
594 pos = p;
595 delete c;
597 vec_ZZ lambda;
598 while (!nonuni.empty()) {
599 c = nonuni.back();
600 nonuni.pop_back();
601 Vector* v = c->short_vector(lambda);
602 for (int i = 0; i < c->Rays->NbRows - 1; ++i) {
603 if (lambda[i] == 0)
604 continue;
605 Matrix* M = Matrix_Copy(c->Rays);
606 Vector_Copy(v->p, M->p[i], v->Size);
607 cone * pc = new cone(M);
608 assert (pc->det != 0);
609 if (abs(pc->det) > 1) {
610 assert(abs(pc->det) < abs(c->det));
611 nonuni.push_back(pc);
612 } else {
613 Polyhedron *p = pc->poly();
614 pc->Cone = 0;
615 if (sign(pc->det) == s) {
616 p->next = pos;
617 pos = p;
618 } else {
619 p->next = neg;
620 neg = p;
622 delete pc;
624 Matrix_Free(M);
626 Vector_Free(v);
627 delete c;
629 *ppos = pos;
630 *pneg = neg;
634 * Returns a single list of npos "positive" cones followed by nneg
635 * "negative" cones.
636 * The input cone is freed
638 void decompose(Polyhedron *cone, Polyhedron **parts, int *npos, int *nneg, unsigned MaxRays)
640 Polyhedron_Polarize(cone);
641 if (cone->NbRays - 1 != cone->Dimension) {
642 Polyhedron *tmp = cone;
643 cone = triangularize_cone(cone, MaxRays);
644 Polyhedron_Free(tmp);
646 Polyhedron *polpos = NULL, *polneg = NULL;
647 *npos = 0; *nneg = 0;
648 for (Polyhedron *Polar = cone; Polar; Polar = Polar->next)
649 barvinok_decompose(Polar, &polpos, &polneg);
651 Polyhedron *last;
652 for (Polyhedron *i = polpos; i; i = i->next) {
653 Polyhedron_Polarize(i);
654 ++*npos;
655 last = i;
657 for (Polyhedron *i = polneg; i; i = i->next) {
658 Polyhedron_Polarize(i);
659 ++*nneg;
661 if (last) {
662 last->next = polneg;
663 *parts = polpos;
664 } else
665 *parts = polneg;
666 Domain_Free(cone);
669 const int MAX_TRY=10;
671 * Searches for a vector that is not orthogonal to any
672 * of the rays in rays.
674 static void nonorthog(mat_ZZ& rays, vec_ZZ& lambda)
676 int dim = rays.NumCols();
677 bool found = false;
678 lambda.SetLength(dim);
679 if (dim == 0)
680 return;
682 for (int i = 2; !found && i <= 50*dim; i+=4) {
683 for (int j = 0; j < MAX_TRY; ++j) {
684 for (int k = 0; k < dim; ++k) {
685 int r = random_int(i)+2;
686 int v = (2*(r%2)-1) * (r >> 1);
687 lambda[k] = v;
689 int k = 0;
690 for (; k < rays.NumRows(); ++k)
691 if (lambda * rays[k] == 0)
692 break;
693 if (k == rays.NumRows()) {
694 found = true;
695 break;
699 assert(found);
702 static void randomvector(Polyhedron *P, vec_ZZ& lambda, int nvar)
704 Value tmp;
705 int max = 10 * 16;
706 unsigned int dim = P->Dimension;
707 value_init(tmp);
709 for (int i = 0; i < P->NbRays; ++i) {
710 for (int j = 1; j <= dim; ++j) {
711 value_absolute(tmp, P->Ray[i][j]);
712 int t = VALUE_TO_LONG(tmp) * 16;
713 if (t > max)
714 max = t;
717 for (int i = 0; i < P->NbConstraints; ++i) {
718 for (int j = 1; j <= dim; ++j) {
719 value_absolute(tmp, P->Constraint[i][j]);
720 int t = VALUE_TO_LONG(tmp) * 16;
721 if (t > max)
722 max = t;
725 value_clear(tmp);
727 lambda.SetLength(nvar);
728 for (int k = 0; k < nvar; ++k) {
729 int r = random_int(max*dim)+2;
730 int v = (2*(r%2)-1) * (max/2*dim + (r >> 1));
731 lambda[k] = v;
735 static void add_rays(mat_ZZ& rays, Polyhedron *i, int *r, int nvar = -1,
736 bool all = false)
738 unsigned dim = i->Dimension;
739 if (nvar == -1)
740 nvar = dim;
741 for (int k = 0; k < i->NbRays; ++k) {
742 if (!value_zero_p(i->Ray[k][dim+1]))
743 continue;
744 if (!all && nvar != dim && First_Non_Zero(i->Ray[k]+1, nvar) == -1)
745 continue;
746 values2zz(i->Ray[k]+1, rays[(*r)++], nvar);
750 void lattice_point(Value* values, Polyhedron *i, vec_ZZ& vertex)
752 unsigned dim = i->Dimension;
753 if(!value_one_p(values[dim])) {
754 Matrix* Rays = rays(i);
755 Matrix *inv = Matrix_Alloc(Rays->NbRows, Rays->NbColumns);
756 int ok = Matrix_Inverse(Rays, inv);
757 assert(ok);
758 Matrix_Free(Rays);
759 Rays = rays(i);
760 Vector *lambda = Vector_Alloc(dim+1);
761 Vector_Matrix_Product(values, inv, lambda->p);
762 Matrix_Free(inv);
763 for (int j = 0; j < dim; ++j)
764 mpz_cdiv_q(lambda->p[j], lambda->p[j], lambda->p[dim]);
765 value_set_si(lambda->p[dim], 1);
766 Vector *A = Vector_Alloc(dim+1);
767 Vector_Matrix_Product(lambda->p, Rays, A->p);
768 Vector_Free(lambda);
769 Matrix_Free(Rays);
770 values2zz(A->p, vertex, dim);
771 Vector_Free(A);
772 } else
773 values2zz(values, vertex, dim);
776 static evalue *term(int param, ZZ& c, Value *den = NULL)
778 evalue *EP = new evalue();
779 value_init(EP->d);
780 value_set_si(EP->d,0);
781 EP->x.p = new_enode(polynomial, 2, param + 1);
782 evalue_set_si(&EP->x.p->arr[0], 0, 1);
783 value_init(EP->x.p->arr[1].x.n);
784 if (den == NULL)
785 value_set_si(EP->x.p->arr[1].d, 1);
786 else
787 value_assign(EP->x.p->arr[1].d, *den);
788 zz2value(c, EP->x.p->arr[1].x.n);
789 return EP;
792 static void vertex_period(
793 Polyhedron *i, vec_ZZ& lambda, Matrix *T,
794 Value lcm, int p, Vector *val,
795 evalue *E, evalue* ev,
796 ZZ& offset)
798 unsigned nparam = T->NbRows - 1;
799 unsigned dim = i->Dimension;
800 Value tmp;
801 ZZ nump;
803 if (p == nparam) {
804 vec_ZZ vertex;
805 ZZ num, l;
806 Vector * values = Vector_Alloc(dim + 1);
807 Vector_Matrix_Product(val->p, T, values->p);
808 value_assign(values->p[dim], lcm);
809 lattice_point(values->p, i, vertex);
810 num = vertex * lambda;
811 value2zz(lcm, l);
812 num *= l;
813 num += offset;
814 value_init(ev->x.n);
815 zz2value(num, ev->x.n);
816 value_assign(ev->d, lcm);
817 Vector_Free(values);
818 return;
821 value_init(tmp);
822 vec_ZZ vertex;
823 values2zz(T->p[p], vertex, dim);
824 nump = vertex * lambda;
825 if (First_Non_Zero(val->p, p) == -1) {
826 value_assign(tmp, lcm);
827 evalue *ET = term(p, nump, &tmp);
828 eadd(ET, E);
829 free_evalue_refs(ET);
830 delete ET;
833 value_assign(tmp, lcm);
834 if (First_Non_Zero(T->p[p], dim) != -1)
835 Vector_Gcd(T->p[p], dim, &tmp);
836 Gcd(tmp, lcm, &tmp);
837 if (value_lt(tmp, lcm)) {
838 ZZ count;
840 value_division(tmp, lcm, tmp);
841 value_set_si(ev->d, 0);
842 ev->x.p = new_enode(periodic, VALUE_TO_INT(tmp), p+1);
843 value2zz(tmp, count);
844 do {
845 value_decrement(tmp, tmp);
846 --count;
847 ZZ new_offset = offset - count * nump;
848 value_assign(val->p[p], tmp);
849 vertex_period(i, lambda, T, lcm, p+1, val, E,
850 &ev->x.p->arr[VALUE_TO_INT(tmp)], new_offset);
851 } while (value_pos_p(tmp));
852 } else
853 vertex_period(i, lambda, T, lcm, p+1, val, E, ev, offset);
854 value_clear(tmp);
857 static void mask_r(Matrix *f, int nr, Vector *lcm, int p, Vector *val, evalue *ev)
859 unsigned nparam = lcm->Size;
861 if (p == nparam) {
862 Vector * prod = Vector_Alloc(f->NbRows);
863 Matrix_Vector_Product(f, val->p, prod->p);
864 int isint = 1;
865 for (int i = 0; i < nr; ++i) {
866 value_modulus(prod->p[i], prod->p[i], f->p[i][nparam+1]);
867 isint &= value_zero_p(prod->p[i]);
869 value_set_si(ev->d, 1);
870 value_init(ev->x.n);
871 value_set_si(ev->x.n, isint);
872 Vector_Free(prod);
873 return;
876 Value tmp;
877 value_init(tmp);
878 if (value_one_p(lcm->p[p]))
879 mask_r(f, nr, lcm, p+1, val, ev);
880 else {
881 value_assign(tmp, lcm->p[p]);
882 value_set_si(ev->d, 0);
883 ev->x.p = new_enode(periodic, VALUE_TO_INT(tmp), p+1);
884 do {
885 value_decrement(tmp, tmp);
886 value_assign(val->p[p], tmp);
887 mask_r(f, nr, lcm, p+1, val, &ev->x.p->arr[VALUE_TO_INT(tmp)]);
888 } while (value_pos_p(tmp));
890 value_clear(tmp);
893 static evalue *multi_monom(vec_ZZ& p)
895 evalue *X = new evalue();
896 value_init(X->d);
897 value_init(X->x.n);
898 unsigned nparam = p.length()-1;
899 zz2value(p[nparam], X->x.n);
900 value_set_si(X->d, 1);
901 for (int i = 0; i < nparam; ++i) {
902 if (p[i] == 0)
903 continue;
904 evalue *T = term(i, p[i]);
905 eadd(T, X);
906 free_evalue_refs(T);
907 delete T;
909 return X;
913 * Check whether mapping polyhedron P on the affine combination
914 * num yields a range that has a fixed quotient on integer
915 * division by d
916 * If zero is true, then we are only interested in the quotient
917 * for the cases where the remainder is zero.
918 * Returns NULL if false and a newly allocated value if true.
920 static Value *fixed_quotient(Polyhedron *P, vec_ZZ& num, Value d, bool zero)
922 Value* ret = NULL;
923 int len = num.length();
924 Matrix *T = Matrix_Alloc(2, len);
925 zz2values(num, T->p[0]);
926 value_set_si(T->p[1][len-1], 1);
927 Polyhedron *I = Polyhedron_Image(P, T, P->NbConstraints);
928 Matrix_Free(T);
930 int i;
931 for (i = 0; i < I->NbRays; ++i)
932 if (value_zero_p(I->Ray[i][2])) {
933 Polyhedron_Free(I);
934 return NULL;
937 Value min, max;
938 value_init(min);
939 value_init(max);
940 int bounded = line_minmax(I, &min, &max);
941 assert(bounded);
943 if (zero)
944 mpz_cdiv_q(min, min, d);
945 else
946 mpz_fdiv_q(min, min, d);
947 mpz_fdiv_q(max, max, d);
949 if (value_eq(min, max)) {
950 ALLOC(Value, ret);
951 value_init(*ret);
952 value_assign(*ret, min);
954 value_clear(min);
955 value_clear(max);
956 return ret;
960 * Normalize linear expression coef modulo m
961 * Removes common factor and reduces coefficients
962 * Returns index of first non-zero coefficient or len
964 static int normal_mod(Value *coef, int len, Value *m)
966 Value gcd;
967 value_init(gcd);
969 Vector_Gcd(coef, len, &gcd);
970 Gcd(gcd, *m, &gcd);
971 Vector_AntiScale(coef, coef, gcd, len);
973 value_division(*m, *m, gcd);
974 value_clear(gcd);
976 if (value_one_p(*m))
977 return len;
979 int j;
980 for (j = 0; j < len; ++j)
981 mpz_fdiv_r(coef[j], coef[j], *m);
982 for (j = 0; j < len; ++j)
983 if (value_notzero_p(coef[j]))
984 break;
986 return j;
989 #ifdef USE_MODULO
990 static void mask(Matrix *f, evalue *factor)
992 int nr = f->NbRows, nc = f->NbColumns;
993 int n;
994 bool found = false;
995 for (n = 0; n < nr && value_notzero_p(f->p[n][nc-1]); ++n)
996 if (value_notone_p(f->p[n][nc-1]) &&
997 value_notmone_p(f->p[n][nc-1]))
998 found = true;
999 if (!found)
1000 return;
1002 evalue EP;
1003 nr = n;
1005 Value m;
1006 value_init(m);
1008 evalue EV;
1009 value_init(EV.d);
1010 value_init(EV.x.n);
1011 value_set_si(EV.x.n, 1);
1013 for (n = 0; n < nr; ++n) {
1014 value_assign(m, f->p[n][nc-1]);
1015 if (value_one_p(m) || value_mone_p(m))
1016 continue;
1018 int j = normal_mod(f->p[n], nc-1, &m);
1019 if (j == nc-1) {
1020 free_evalue_refs(factor);
1021 value_init(factor->d);
1022 evalue_set_si(factor, 0, 1);
1023 break;
1025 vec_ZZ row;
1026 values2zz(f->p[n], row, nc-1);
1027 ZZ g;
1028 value2zz(m, g);
1029 if (j < (nc-1)-1 && row[j] > g/2) {
1030 for (int k = j; k < (nc-1); ++k)
1031 if (row[k] != 0)
1032 row[k] = g - row[k];
1035 value_init(EP.d);
1036 value_set_si(EP.d, 0);
1037 EP.x.p = new_enode(relation, 2, 0);
1038 value_clear(EP.x.p->arr[1].d);
1039 EP.x.p->arr[1] = *factor;
1040 evalue *ev = &EP.x.p->arr[0];
1041 value_set_si(ev->d, 0);
1042 ev->x.p = new_enode(fractional, 3, -1);
1043 evalue_set_si(&ev->x.p->arr[1], 0, 1);
1044 evalue_set_si(&ev->x.p->arr[2], 1, 1);
1045 evalue *E = multi_monom(row);
1046 value_assign(EV.d, m);
1047 emul(&EV, E);
1048 value_clear(ev->x.p->arr[0].d);
1049 ev->x.p->arr[0] = *E;
1050 delete E;
1051 *factor = EP;
1054 value_clear(m);
1055 free_evalue_refs(&EV);
1057 #else
1061 static void mask(Matrix *f, evalue *factor)
1063 int nr = f->NbRows, nc = f->NbColumns;
1064 int n;
1065 bool found = false;
1066 for (n = 0; n < nr && value_notzero_p(f->p[n][nc-1]); ++n)
1067 if (value_notone_p(f->p[n][nc-1]) &&
1068 value_notmone_p(f->p[n][nc-1]))
1069 found = true;
1070 if (!found)
1071 return;
1073 Value tmp;
1074 value_init(tmp);
1075 nr = n;
1076 unsigned np = nc - 2;
1077 Vector *lcm = Vector_Alloc(np);
1078 Vector *val = Vector_Alloc(nc);
1079 Vector_Set(val->p, 0, nc);
1080 value_set_si(val->p[np], 1);
1081 Vector_Set(lcm->p, 1, np);
1082 for (n = 0; n < nr; ++n) {
1083 if (value_one_p(f->p[n][nc-1]) ||
1084 value_mone_p(f->p[n][nc-1]))
1085 continue;
1086 for (int j = 0; j < np; ++j)
1087 if (value_notzero_p(f->p[n][j])) {
1088 Gcd(f->p[n][j], f->p[n][nc-1], &tmp);
1089 value_division(tmp, f->p[n][nc-1], tmp);
1090 value_lcm(tmp, lcm->p[j], &lcm->p[j]);
1093 evalue EP;
1094 value_init(EP.d);
1095 mask_r(f, nr, lcm, 0, val, &EP);
1096 value_clear(tmp);
1097 Vector_Free(val);
1098 Vector_Free(lcm);
1099 emul(&EP,factor);
1100 free_evalue_refs(&EP);
1102 #endif
1104 struct term_info {
1105 evalue *E;
1106 ZZ constant;
1107 ZZ coeff;
1108 int pos;
1111 static bool mod_needed(Polyhedron *PD, vec_ZZ& num, Value d, evalue *E)
1113 Value *q = fixed_quotient(PD, num, d, false);
1115 if (!q)
1116 return true;
1118 value_oppose(*q, *q);
1119 evalue EV;
1120 value_init(EV.d);
1121 value_set_si(EV.d, 1);
1122 value_init(EV.x.n);
1123 value_multiply(EV.x.n, *q, d);
1124 eadd(&EV, E);
1125 free_evalue_refs(&EV);
1126 value_clear(*q);
1127 free(q);
1128 return false;
1131 static void ceil_mod(Value *coef, int len, Value d, ZZ& f, evalue *EP, Polyhedron *PD)
1133 Value m;
1134 value_init(m);
1135 value_set_si(m, -1);
1137 Vector_Scale(coef, coef, m, len);
1139 value_assign(m, d);
1140 int j = normal_mod(coef, len, &m);
1142 if (j == len) {
1143 value_clear(m);
1144 return;
1147 vec_ZZ num;
1148 values2zz(coef, num, len);
1150 ZZ g;
1151 value2zz(m, g);
1153 evalue tmp;
1154 value_init(tmp.d);
1155 evalue_set_si(&tmp, 0, 1);
1157 int p = j;
1158 if (g % 2 == 0)
1159 while (j < len-1 && (num[j] == g/2 || num[j] == 0))
1160 ++j;
1161 if ((j < len-1 && num[j] > g/2) || (j == len-1 && num[j] >= (g+1)/2)) {
1162 for (int k = j; k < len-1; ++k)
1163 if (num[k] != 0)
1164 num[k] = g - num[k];
1165 num[len-1] = g - 1 - num[len-1];
1166 value_assign(tmp.d, m);
1167 ZZ t = f*(g-1);
1168 zz2value(t, tmp.x.n);
1169 eadd(&tmp, EP);
1170 f = -f;
1173 if (p >= len-1) {
1174 ZZ t = num[len-1] * f;
1175 zz2value(t, tmp.x.n);
1176 value_assign(tmp.d, m);
1177 eadd(&tmp, EP);
1178 } else {
1179 evalue *E = multi_monom(num);
1180 evalue EV;
1181 value_init(EV.d);
1183 if (PD && !mod_needed(PD, num, m, E)) {
1184 value_init(EV.x.n);
1185 zz2value(f, EV.x.n);
1186 value_assign(EV.d, m);
1187 emul(&EV, E);
1188 eadd(E, EP);
1189 } else {
1190 value_init(EV.x.n);
1191 value_set_si(EV.x.n, 1);
1192 value_assign(EV.d, m);
1193 emul(&EV, E);
1194 value_clear(EV.x.n);
1195 value_set_si(EV.d, 0);
1196 EV.x.p = new_enode(fractional, 3, -1);
1197 evalue_copy(&EV.x.p->arr[0], E);
1198 evalue_set_si(&EV.x.p->arr[1], 0, 1);
1199 value_init(EV.x.p->arr[2].x.n);
1200 zz2value(f, EV.x.p->arr[2].x.n);
1201 value_set_si(EV.x.p->arr[2].d, 1);
1203 eadd(&EV, EP);
1206 free_evalue_refs(&EV);
1207 free_evalue_refs(E);
1208 delete E;
1211 free_evalue_refs(&tmp);
1213 out:
1214 value_clear(m);
1217 evalue* bv_ceil3(Value *coef, int len, Value d, Polyhedron *P)
1219 Vector *val = Vector_Alloc(len);
1221 Value t;
1222 value_init(t);
1223 value_set_si(t, -1);
1224 Vector_Scale(coef, val->p, t, len);
1225 value_absolute(t, d);
1227 vec_ZZ num;
1228 values2zz(val->p, num, len);
1229 evalue *EP = multi_monom(num);
1231 evalue tmp;
1232 value_init(tmp.d);
1233 value_init(tmp.x.n);
1234 value_set_si(tmp.x.n, 1);
1235 value_assign(tmp.d, t);
1237 emul(&tmp, EP);
1239 ZZ one;
1240 one = 1;
1241 ceil_mod(val->p, len, t, one, EP, P);
1242 value_clear(t);
1244 /* copy EP to malloc'ed evalue */
1245 evalue *E;
1246 ALLOC(evalue, E);
1247 *E = *EP;
1248 delete EP;
1250 free_evalue_refs(&tmp);
1251 Vector_Free(val);
1253 return E;
1256 #ifdef USE_MODULO
1257 evalue* lattice_point(
1258 Polyhedron *i, vec_ZZ& lambda, Matrix *W, Value lcm, Polyhedron *PD)
1260 unsigned nparam = W->NbColumns - 1;
1262 Matrix* Rays = rays2(i);
1263 Matrix *T = Transpose(Rays);
1264 Matrix *T2 = Matrix_Copy(T);
1265 Matrix *inv = Matrix_Alloc(T2->NbRows, T2->NbColumns);
1266 int ok = Matrix_Inverse(T2, inv);
1267 assert(ok);
1268 Matrix_Free(Rays);
1269 Matrix_Free(T2);
1270 mat_ZZ vertex;
1271 matrix2zz(W, vertex, W->NbRows, W->NbColumns);
1273 vec_ZZ num;
1274 num = lambda * vertex;
1276 evalue *EP = multi_monom(num);
1278 evalue tmp;
1279 value_init(tmp.d);
1280 value_init(tmp.x.n);
1281 value_set_si(tmp.x.n, 1);
1282 value_assign(tmp.d, lcm);
1284 emul(&tmp, EP);
1286 Matrix *L = Matrix_Alloc(inv->NbRows, W->NbColumns);
1287 Matrix_Product(inv, W, L);
1289 mat_ZZ RT;
1290 matrix2zz(T, RT, T->NbRows, T->NbColumns);
1291 Matrix_Free(T);
1293 vec_ZZ p = lambda * RT;
1295 for (int i = 0; i < L->NbRows; ++i) {
1296 ceil_mod(L->p[i], nparam+1, lcm, p[i], EP, PD);
1299 Matrix_Free(L);
1301 Matrix_Free(inv);
1302 free_evalue_refs(&tmp);
1303 return EP;
1305 #else
1306 evalue* lattice_point(
1307 Polyhedron *i, vec_ZZ& lambda, Matrix *W, Value lcm, Polyhedron *PD)
1309 Matrix *T = Transpose(W);
1310 unsigned nparam = T->NbRows - 1;
1312 evalue *EP = new evalue();
1313 value_init(EP->d);
1314 evalue_set_si(EP, 0, 1);
1316 evalue ev;
1317 Vector *val = Vector_Alloc(nparam+1);
1318 value_set_si(val->p[nparam], 1);
1319 ZZ offset(INIT_VAL, 0);
1320 value_init(ev.d);
1321 vertex_period(i, lambda, T, lcm, 0, val, EP, &ev, offset);
1322 Vector_Free(val);
1323 eadd(&ev, EP);
1324 free_evalue_refs(&ev);
1326 Matrix_Free(T);
1328 reduce_evalue(EP);
1330 return EP;
1332 #endif
1334 void lattice_point(
1335 Param_Vertices* V, Polyhedron *i, vec_ZZ& lambda, term_info* term,
1336 Polyhedron *PD)
1338 unsigned nparam = V->Vertex->NbColumns - 2;
1339 unsigned dim = i->Dimension;
1340 mat_ZZ vertex;
1341 vertex.SetDims(V->Vertex->NbRows, nparam+1);
1342 Value lcm, tmp;
1343 value_init(lcm);
1344 value_init(tmp);
1345 value_set_si(lcm, 1);
1346 for (int j = 0; j < V->Vertex->NbRows; ++j) {
1347 value_lcm(lcm, V->Vertex->p[j][nparam+1], &lcm);
1349 if (value_notone_p(lcm)) {
1350 Matrix * mv = Matrix_Alloc(dim, nparam+1);
1351 for (int j = 0 ; j < dim; ++j) {
1352 value_division(tmp, lcm, V->Vertex->p[j][nparam+1]);
1353 Vector_Scale(V->Vertex->p[j], mv->p[j], tmp, nparam+1);
1356 term->E = lattice_point(i, lambda, mv, lcm, PD);
1357 term->constant = 0;
1359 Matrix_Free(mv);
1360 value_clear(lcm);
1361 value_clear(tmp);
1362 return;
1364 for (int i = 0; i < V->Vertex->NbRows; ++i) {
1365 assert(value_one_p(V->Vertex->p[i][nparam+1])); // for now
1366 values2zz(V->Vertex->p[i], vertex[i], nparam+1);
1369 vec_ZZ num;
1370 num = lambda * vertex;
1372 int p = -1;
1373 int nn = 0;
1374 for (int j = 0; j < nparam; ++j)
1375 if (num[j] != 0) {
1376 ++nn;
1377 p = j;
1379 if (nn >= 2) {
1380 term->E = multi_monom(num);
1381 term->constant = 0;
1382 } else {
1383 term->E = NULL;
1384 term->constant = num[nparam];
1385 term->pos = p;
1386 if (p != -1)
1387 term->coeff = num[p];
1390 value_clear(lcm);
1391 value_clear(tmp);
1394 static void normalize(ZZ& sign, ZZ& num, vec_ZZ& den)
1396 unsigned dim = den.length();
1398 int change = 0;
1400 for (int j = 0; j < den.length(); ++j) {
1401 if (den[j] > 0)
1402 change ^= 1;
1403 else {
1404 den[j] = abs(den[j]);
1405 num += den[j];
1408 if (change)
1409 sign = -sign;
1412 /* input:
1413 * f: the powers in the denominator for the remaining vars
1414 * each row refers to a factor
1415 * den_s: for each factor, the power of (s+1)
1416 * sign
1417 * num_s: powers in the numerator corresponding to the summed vars
1418 * num_p: powers in the numerator corresponidng to the remaining vars
1419 * number of rays in cone: "dim" = "k"
1420 * length of each ray: "dim" = "d"
1421 * for now, it is assume: k == d
1422 * output:
1423 * den_p: for each factor
1424 * 0: independent of remaining vars
1425 * 1: power corresponds to corresponding row in f
1426 * -1: power is inverse of corresponding row in f
1428 static void normalize(ZZ& sign,
1429 ZZ& num_s, vec_ZZ& num_p, vec_ZZ& den_s, vec_ZZ& den_p,
1430 mat_ZZ& f)
1432 unsigned dim = f.NumRows();
1433 unsigned nparam = num_p.length();
1434 unsigned nvar = dim - nparam;
1436 int change = 0;
1438 for (int j = 0; j < den_s.length(); ++j) {
1439 if (den_s[j] == 0) {
1440 den_p[j] = 1;
1441 continue;
1443 int k;
1444 for (k = 0; k < nparam; ++k)
1445 if (f[j][k] != 0)
1446 break;
1447 if (k < nparam) {
1448 if (den_s[j] > 0) {
1449 den_p[j] = -1;
1450 num_p -= f[j];
1451 } else
1452 den_p[j] = 1;
1453 } else
1454 den_p[j] = 0;
1455 if (den_s[j] > 0)
1456 change ^= 1;
1457 else {
1458 den_s[j] = abs(den_s[j]);
1459 num_s += den_s[j];
1463 if (change)
1464 sign = -sign;
1467 struct counter : public polar_decomposer {
1468 vec_ZZ lambda;
1469 mat_ZZ rays;
1470 vec_ZZ vertex;
1471 vec_ZZ den;
1472 ZZ sign;
1473 ZZ num;
1474 int j;
1475 Polyhedron *P;
1476 unsigned dim;
1477 mpq_t count;
1479 counter(Polyhedron *P) {
1480 this->P = P;
1481 dim = P->Dimension;
1482 randomvector(P, lambda, dim);
1483 rays.SetDims(dim, dim);
1484 den.SetLength(dim);
1485 mpq_init(count);
1488 void start(unsigned MaxRays);
1490 ~counter() {
1491 mpq_clear(count);
1494 virtual void handle_polar(Polyhedron *P, int sign);
1497 void counter::handle_polar(Polyhedron *C, int s)
1499 int r = 0;
1500 assert(C->NbRays-1 == dim);
1501 add_rays(rays, C, &r);
1502 for (int k = 0; k < dim; ++k) {
1503 assert(lambda * rays[k] != 0);
1506 sign = s;
1508 lattice_point(P->Ray[j]+1, C, vertex);
1509 num = vertex * lambda;
1510 den = rays * lambda;
1511 normalize(sign, num, den);
1513 dpoly d(dim, num);
1514 dpoly n(dim, den[0], 1);
1515 for (int k = 1; k < dim; ++k) {
1516 dpoly fact(dim, den[k], 1);
1517 n *= fact;
1519 d.div(n, count, sign);
1522 void counter::start(unsigned MaxRays)
1524 for (j = 0; j < P->NbRays; ++j) {
1525 Polyhedron *C = supporting_cone(P, j);
1526 decompose(C, MaxRays);
1530 typedef Polyhedron * Polyhedron_p;
1532 void barvinok_count(Polyhedron *P, Value* result, unsigned NbMaxCons)
1534 Polyhedron ** vcone;
1535 ZZ sign;
1536 unsigned dim;
1537 int allocated = 0;
1538 Value factor;
1539 Polyhedron *Q;
1540 int r = 0;
1542 if (emptyQ(P)) {
1543 value_set_si(*result, 0);
1544 return;
1546 if (P->NbBid == 0)
1547 for (; r < P->NbRays; ++r)
1548 if (value_zero_p(P->Ray[r][P->Dimension+1]))
1549 break;
1550 if (P->NbBid !=0 || r < P->NbRays) {
1551 value_set_si(*result, -1);
1552 return;
1554 if (P->NbEq != 0) {
1555 P = remove_equalities(P);
1556 if (emptyQ(P)) {
1557 Polyhedron_Free(P);
1558 value_set_si(*result, 0);
1559 return;
1561 allocated = 1;
1563 value_init(factor);
1564 value_set_si(factor, 1);
1565 Q = Polyhedron_Reduce(P, &factor);
1566 if (Q) {
1567 if (allocated)
1568 Polyhedron_Free(P);
1569 P = Q;
1570 allocated = 1;
1572 if (P->Dimension == 0) {
1573 value_assign(*result, factor);
1574 if (allocated)
1575 Polyhedron_Free(P);
1576 value_clear(factor);
1577 return;
1580 counter cnt(P);
1581 cnt.start(NbMaxCons);
1583 assert(value_one_p(&cnt.count[0]._mp_den));
1584 value_multiply(*result, &cnt.count[0]._mp_num, factor);
1586 if (allocated)
1587 Polyhedron_Free(P);
1588 value_clear(factor);
1591 static void uni_polynom(int param, Vector *c, evalue *EP)
1593 unsigned dim = c->Size-2;
1594 value_init(EP->d);
1595 value_set_si(EP->d,0);
1596 EP->x.p = new_enode(polynomial, dim+1, param+1);
1597 for (int j = 0; j <= dim; ++j)
1598 evalue_set(&EP->x.p->arr[j], c->p[j], c->p[dim+1]);
1601 static void multi_polynom(Vector *c, evalue* X, evalue *EP)
1603 unsigned dim = c->Size-2;
1604 evalue EC;
1606 value_init(EC.d);
1607 evalue_set(&EC, c->p[dim], c->p[dim+1]);
1609 value_init(EP->d);
1610 evalue_set(EP, c->p[dim], c->p[dim+1]);
1612 for (int i = dim-1; i >= 0; --i) {
1613 emul(X, EP);
1614 value_assign(EC.x.n, c->p[i]);
1615 eadd(&EC, EP);
1617 free_evalue_refs(&EC);
1620 Polyhedron *unfringe (Polyhedron *P, unsigned MaxRays)
1622 int len = P->Dimension+2;
1623 Polyhedron *T, *R = P;
1624 Value g;
1625 value_init(g);
1626 Vector *row = Vector_Alloc(len);
1627 value_set_si(row->p[0], 1);
1629 R = DomainConstraintSimplify(Polyhedron_Copy(P), MaxRays);
1631 Matrix *M = Matrix_Alloc(2, len-1);
1632 value_set_si(M->p[1][len-2], 1);
1633 for (int v = 0; v < P->Dimension; ++v) {
1634 value_set_si(M->p[0][v], 1);
1635 Polyhedron *I = Polyhedron_Image(P, M, 2+1);
1636 value_set_si(M->p[0][v], 0);
1637 for (int r = 0; r < I->NbConstraints; ++r) {
1638 if (value_zero_p(I->Constraint[r][0]))
1639 continue;
1640 if (value_zero_p(I->Constraint[r][1]))
1641 continue;
1642 if (value_one_p(I->Constraint[r][1]))
1643 continue;
1644 if (value_mone_p(I->Constraint[r][1]))
1645 continue;
1646 value_absolute(g, I->Constraint[r][1]);
1647 Vector_Set(row->p+1, 0, len-2);
1648 value_division(row->p[1+v], I->Constraint[r][1], g);
1649 mpz_fdiv_q(row->p[len-1], I->Constraint[r][2], g);
1650 T = R;
1651 R = AddConstraints(row->p, 1, R, MaxRays);
1652 if (T != P)
1653 Polyhedron_Free(T);
1655 Polyhedron_Free(I);
1657 Matrix_Free(M);
1658 Vector_Free(row);
1659 value_clear(g);
1660 return R;
1663 static Polyhedron *reduce_domain(Polyhedron *D, Matrix *CT, Polyhedron *CEq,
1664 Polyhedron **fVD, int nd, unsigned MaxRays)
1666 assert(CEq);
1668 Polyhedron *Dt;
1669 Dt = CT ? DomainPreimage(D, CT, MaxRays) : D;
1670 Polyhedron *rVD = DomainIntersection(Dt, CEq, MaxRays);
1672 /* if rVD is empty or too small in geometric dimension */
1673 if(!rVD || emptyQ(rVD) ||
1674 (rVD->Dimension-rVD->NbEq < Dt->Dimension-Dt->NbEq-CEq->NbEq)) {
1675 if(rVD)
1676 Domain_Free(rVD);
1677 if (CT)
1678 Domain_Free(Dt);
1679 return 0; /* empty validity domain */
1682 if (CT)
1683 Domain_Free(Dt);
1685 fVD[nd] = Domain_Copy(rVD);
1686 for (int i = 0 ; i < nd; ++i) {
1687 Polyhedron *I = DomainIntersection(fVD[nd], fVD[i], MaxRays);
1688 if (emptyQ(I)) {
1689 Domain_Free(I);
1690 continue;
1692 Polyhedron *F = DomainSimplify(I, fVD[nd], MaxRays);
1693 if (F->NbEq == 1) {
1694 Polyhedron *T = rVD;
1695 rVD = DomainDifference(rVD, F, MaxRays);
1696 Domain_Free(T);
1698 Domain_Free(F);
1699 Domain_Free(I);
1702 rVD = DomainConstraintSimplify(rVD, MaxRays);
1703 if (emptyQ(rVD)) {
1704 Domain_Free(fVD[nd]);
1705 Domain_Free(rVD);
1706 return 0;
1709 Value c;
1710 value_init(c);
1711 barvinok_count(rVD, &c, MaxRays);
1712 if (value_zero_p(c)) {
1713 Domain_Free(rVD);
1714 rVD = 0;
1716 value_clear(c);
1718 return rVD;
1721 static bool Polyhedron_is_infinite(Polyhedron *P, unsigned nparam)
1723 int r;
1724 for (r = 0; r < P->NbRays; ++r)
1725 if (value_zero_p(P->Ray[r][0]) ||
1726 value_zero_p(P->Ray[r][P->Dimension+1])) {
1727 int i;
1728 for (i = P->Dimension - nparam; i < P->Dimension; ++i)
1729 if (value_notzero_p(P->Ray[r][i+1]))
1730 break;
1731 if (i >= P->Dimension)
1732 break;
1734 return r < P->NbRays;
1737 /* Check whether all rays point in the positive directions
1738 * for the parameters
1740 static bool Polyhedron_has_positive_rays(Polyhedron *P, unsigned nparam)
1742 int r;
1743 for (r = 0; r < P->NbRays; ++r)
1744 if (value_zero_p(P->Ray[r][P->Dimension+1])) {
1745 int i;
1746 for (i = P->Dimension - nparam; i < P->Dimension; ++i)
1747 if (value_neg_p(P->Ray[r][i+1]))
1748 return false;
1750 return true;
1753 typedef evalue * evalue_p;
1755 struct enumerator : public polar_decomposer {
1756 vec_ZZ lambda;
1757 unsigned dim, nbV;
1758 evalue ** vE;
1759 int _i;
1760 mat_ZZ rays;
1761 vec_ZZ den;
1762 ZZ sign;
1763 Polyhedron *P;
1764 Param_Vertices *V;
1765 term_info num;
1766 Vector *c;
1767 mpq_t count;
1769 enumerator(Polyhedron *P, unsigned dim, unsigned nbV) {
1770 this->P = P;
1771 this->dim = dim;
1772 this->nbV = nbV;
1773 randomvector(P, lambda, dim);
1774 rays.SetDims(dim, dim);
1775 den.SetLength(dim);
1776 c = Vector_Alloc(dim+2);
1778 vE = new evalue_p[nbV];
1779 for (int j = 0; j < nbV; ++j)
1780 vE[j] = 0;
1782 mpq_init(count);
1785 void decompose_at(Param_Vertices *V, int _i, unsigned MaxRays) {
1786 Polyhedron *C = supporting_cone_p(P, V);
1787 this->_i = _i;
1788 this->V = V;
1790 vE[_i] = new evalue;
1791 value_init(vE[_i]->d);
1792 evalue_set_si(vE[_i], 0, 1);
1794 decompose(C, MaxRays);
1797 ~enumerator() {
1798 mpq_clear(count);
1799 Vector_Free(c);
1801 for (int j = 0; j < nbV; ++j)
1802 if (vE[j]) {
1803 free_evalue_refs(vE[j]);
1804 delete vE[j];
1806 delete [] vE;
1809 virtual void handle_polar(Polyhedron *P, int sign);
1812 void enumerator::handle_polar(Polyhedron *C, int s)
1814 int r = 0;
1815 assert(C->NbRays-1 == dim);
1816 add_rays(rays, C, &r);
1817 for (int k = 0; k < dim; ++k) {
1818 assert(lambda * rays[k] != 0);
1821 sign = s;
1823 lattice_point(V, C, lambda, &num, 0);
1824 den = rays * lambda;
1825 normalize(sign, num.constant, den);
1827 dpoly n(dim, den[0], 1);
1828 for (int k = 1; k < dim; ++k) {
1829 dpoly fact(dim, den[k], 1);
1830 n *= fact;
1832 if (num.E != NULL) {
1833 ZZ one(INIT_VAL, 1);
1834 dpoly_n d(dim, num.constant, one);
1835 d.div(n, c, sign);
1836 evalue EV;
1837 multi_polynom(c, num.E, &EV);
1838 eadd(&EV , vE[_i]);
1839 free_evalue_refs(&EV);
1840 free_evalue_refs(num.E);
1841 delete num.E;
1842 } else if (num.pos != -1) {
1843 dpoly_n d(dim, num.constant, num.coeff);
1844 d.div(n, c, sign);
1845 evalue EV;
1846 uni_polynom(num.pos, c, &EV);
1847 eadd(&EV , vE[_i]);
1848 free_evalue_refs(&EV);
1849 } else {
1850 mpq_set_si(count, 0, 1);
1851 dpoly d(dim, num.constant);
1852 d.div(n, count, sign);
1853 evalue EV;
1854 value_init(EV.d);
1855 evalue_set(&EV, &count[0]._mp_num, &count[0]._mp_den);
1856 eadd(&EV , vE[_i]);
1857 free_evalue_refs(&EV);
1861 evalue* barvinok_enumerate_ev(Polyhedron *P, Polyhedron* C, unsigned MaxRays)
1863 //P = unfringe(P, MaxRays);
1864 Polyhedron *CEq = NULL, *rVD, *pVD, *CA;
1865 Matrix *CT = NULL;
1866 Param_Polyhedron *PP = NULL;
1867 Param_Domain *D, *next;
1868 Param_Vertices *V;
1869 int r = 0;
1870 unsigned nparam = C->Dimension;
1871 evalue *eres;
1872 ALLOC(evalue, eres);
1873 value_init(eres->d);
1874 value_set_si(eres->d, 0);
1876 evalue factor;
1877 value_init(factor.d);
1878 evalue_set_si(&factor, 1, 1);
1880 CA = align_context(C, P->Dimension, MaxRays);
1881 P = DomainIntersection(P, CA, MaxRays);
1882 Polyhedron_Free(CA);
1884 if (C->Dimension == 0 || emptyQ(P)) {
1885 constant:
1886 eres->x.p = new_enode(partition, 2, C->Dimension);
1887 EVALUE_SET_DOMAIN(eres->x.p->arr[0],
1888 DomainConstraintSimplify(CEq ? CEq : Polyhedron_Copy(C), MaxRays));
1889 value_set_si(eres->x.p->arr[1].d, 1);
1890 value_init(eres->x.p->arr[1].x.n);
1891 if (emptyQ(P))
1892 value_set_si(eres->x.p->arr[1].x.n, 0);
1893 else
1894 barvinok_count(P, &eres->x.p->arr[1].x.n, MaxRays);
1895 out:
1896 emul(&factor, eres);
1897 reduce_evalue(eres);
1898 free_evalue_refs(&factor);
1899 Polyhedron_Free(P);
1900 if (CT)
1901 Matrix_Free(CT);
1902 if (PP)
1903 Param_Polyhedron_Free(PP);
1905 return eres;
1907 if (Polyhedron_is_infinite(P, nparam))
1908 goto constant;
1910 if (P->NbEq != 0) {
1911 Matrix *f;
1912 P = remove_equalities_p(P, P->Dimension-nparam, &f);
1913 mask(f, &factor);
1914 Matrix_Free(f);
1916 if (P->Dimension == nparam) {
1917 CEq = P;
1918 P = Universe_Polyhedron(0);
1919 goto constant;
1922 Polyhedron *Q = ParamPolyhedron_Reduce(P, P->Dimension-nparam, &factor);
1923 if (Q) {
1924 Polyhedron_Free(P);
1925 if (Q->Dimension == nparam) {
1926 CEq = Q;
1927 P = Universe_Polyhedron(0);
1928 goto constant;
1930 P = Q;
1932 Polyhedron *oldP = P;
1933 PP = Polyhedron2Param_SimplifiedDomain(&P,C,MaxRays,&CEq,&CT);
1934 if (P != oldP)
1935 Polyhedron_Free(oldP);
1937 if (isIdentity(CT)) {
1938 Matrix_Free(CT);
1939 CT = NULL;
1940 } else {
1941 assert(CT->NbRows != CT->NbColumns);
1942 if (CT->NbRows == 1) // no more parameters
1943 goto constant;
1944 nparam = CT->NbRows - 1;
1947 unsigned dim = P->Dimension - nparam;
1949 enumerator et(P, dim, PP->nbV);
1951 int nd;
1952 for (nd = 0, D=PP->D; D; ++nd, D=D->next);
1953 struct section { Polyhedron *D; evalue E; };
1954 section *s = new section[nd];
1955 Polyhedron **fVD = new Polyhedron_p[nd];
1957 for(nd = 0, D=PP->D; D; D=next) {
1958 next = D->next;
1960 Polyhedron *rVD = reduce_domain(D->Domain, CT, CEq,
1961 fVD, nd, MaxRays);
1962 if (!rVD)
1963 continue;
1965 pVD = CT ? DomainImage(rVD,CT,MaxRays) : rVD;
1967 value_init(s[nd].E.d);
1968 evalue_set_si(&s[nd].E, 0, 1);
1970 FORALL_PVertex_in_ParamPolyhedron(V,D,PP) // _i is internal counter
1971 if (!et.vE[_i])
1972 et.decompose_at(V, _i, MaxRays);
1973 eadd(et.vE[_i] , &s[nd].E);
1974 END_FORALL_PVertex_in_ParamPolyhedron;
1975 reduce_in_domain(&s[nd].E, pVD);
1977 if (CT)
1978 addeliminatedparams_evalue(&s[nd].E, CT);
1979 s[nd].D = rVD;
1980 ++nd;
1981 if (rVD != pVD)
1982 Domain_Free(pVD);
1985 if (nd == 0)
1986 evalue_set_si(eres, 0, 1);
1987 else {
1988 eres->x.p = new_enode(partition, 2*nd, C->Dimension);
1989 for (int j = 0; j < nd; ++j) {
1990 EVALUE_SET_DOMAIN(eres->x.p->arr[2*j], s[j].D);
1991 value_clear(eres->x.p->arr[2*j+1].d);
1992 eres->x.p->arr[2*j+1] = s[j].E;
1993 Domain_Free(fVD[j]);
1996 delete [] s;
1997 delete [] fVD;
2000 if (CEq)
2001 Polyhedron_Free(CEq);
2003 goto out;
2006 Enumeration* barvinok_enumerate(Polyhedron *P, Polyhedron* C, unsigned MaxRays)
2008 evalue *EP = barvinok_enumerate_ev(P, C, MaxRays);
2010 return partition2enumeration(EP);
2013 static void SwapColumns(Value **V, int n, int i, int j)
2015 for (int r = 0; r < n; ++r)
2016 value_swap(V[r][i], V[r][j]);
2019 static void SwapColumns(Polyhedron *P, int i, int j)
2021 SwapColumns(P->Constraint, P->NbConstraints, i, j);
2022 SwapColumns(P->Ray, P->NbRays, i, j);
2025 static void negative_test_constraint(Value *l, Value *u, Value *c, int pos,
2026 int len, Value *v)
2028 value_oppose(*v, u[pos+1]);
2029 Vector_Combine(l+1, u+1, c+1, *v, l[pos+1], len-1);
2030 value_multiply(*v, *v, l[pos+1]);
2031 value_substract(c[len-1], c[len-1], *v);
2032 value_set_si(*v, -1);
2033 Vector_Scale(c+1, c+1, *v, len-1);
2034 value_decrement(c[len-1], c[len-1]);
2035 ConstraintSimplify(c, c, len, v);
2038 static bool parallel_constraints(Value *l, Value *u, Value *c, int pos,
2039 int len)
2041 bool parallel;
2042 Value g1;
2043 Value g2;
2044 value_init(g1);
2045 value_init(g2);
2047 Vector_Gcd(&l[1+pos], len, &g1);
2048 Vector_Gcd(&u[1+pos], len, &g2);
2049 Vector_Combine(l+1+pos, u+1+pos, c+1, g2, g1, len);
2050 parallel = First_Non_Zero(c+1, len) == -1;
2052 value_clear(g1);
2053 value_clear(g2);
2055 return parallel;
2058 static void negative_test_constraint7(Value *l, Value *u, Value *c, int pos,
2059 int exist, int len, Value *v)
2061 Value g;
2062 value_init(g);
2064 Vector_Gcd(&u[1+pos], exist, v);
2065 Vector_Gcd(&l[1+pos], exist, &g);
2066 Vector_Combine(l+1, u+1, c+1, *v, g, len-1);
2067 value_multiply(*v, *v, g);
2068 value_substract(c[len-1], c[len-1], *v);
2069 value_set_si(*v, -1);
2070 Vector_Scale(c+1, c+1, *v, len-1);
2071 value_decrement(c[len-1], c[len-1]);
2072 ConstraintSimplify(c, c, len, v);
2074 value_clear(g);
2077 static void oppose_constraint(Value *c, int len, Value *v)
2079 value_set_si(*v, -1);
2080 Vector_Scale(c+1, c+1, *v, len-1);
2081 value_decrement(c[len-1], c[len-1]);
2084 static bool SplitOnConstraint(Polyhedron *P, int i, int l, int u,
2085 int nvar, int len, int exist, int MaxRays,
2086 Vector *row, Value& f, bool independent,
2087 Polyhedron **pos, Polyhedron **neg)
2089 negative_test_constraint(P->Constraint[l], P->Constraint[u],
2090 row->p, nvar+i, len, &f);
2091 *neg = AddConstraints(row->p, 1, P, MaxRays);
2093 /* We found an independent, but useless constraint
2094 * Maybe we should detect this earlier and not
2095 * mark the variable as INDEPENDENT
2097 if (emptyQ((*neg))) {
2098 Polyhedron_Free(*neg);
2099 return false;
2102 oppose_constraint(row->p, len, &f);
2103 *pos = AddConstraints(row->p, 1, P, MaxRays);
2105 if (emptyQ((*pos))) {
2106 Polyhedron_Free(*neg);
2107 Polyhedron_Free(*pos);
2108 return false;
2111 return true;
2115 * unimodularly transform P such that constraint r is transformed
2116 * into a constraint that involves only a single (the first)
2117 * existential variable
2120 static Polyhedron *rotate_along(Polyhedron *P, int r, int nvar, int exist,
2121 unsigned MaxRays)
2123 Value g;
2124 value_init(g);
2126 Vector *row = Vector_Alloc(exist);
2127 Vector_Copy(P->Constraint[r]+1+nvar, row->p, exist);
2128 Vector_Gcd(row->p, exist, &g);
2129 if (value_notone_p(g))
2130 Vector_AntiScale(row->p, row->p, g, exist);
2131 value_clear(g);
2133 Matrix *M = unimodular_complete(row);
2134 Matrix *M2 = Matrix_Alloc(P->Dimension+1, P->Dimension+1);
2135 for (r = 0; r < nvar; ++r)
2136 value_set_si(M2->p[r][r], 1);
2137 for ( ; r < nvar+exist; ++r)
2138 Vector_Copy(M->p[r-nvar], M2->p[r]+nvar, exist);
2139 for ( ; r < P->Dimension+1; ++r)
2140 value_set_si(M2->p[r][r], 1);
2141 Polyhedron *T = Polyhedron_Image(P, M2, MaxRays);
2143 Matrix_Free(M2);
2144 Matrix_Free(M);
2145 Vector_Free(row);
2147 return T;
2150 static bool SplitOnVar(Polyhedron *P, int i,
2151 int nvar, int len, int exist, int MaxRays,
2152 Vector *row, Value& f, bool independent,
2153 Polyhedron **pos, Polyhedron **neg)
2155 int j;
2157 for (int l = P->NbEq; l < P->NbConstraints; ++l) {
2158 if (value_negz_p(P->Constraint[l][nvar+i+1]))
2159 continue;
2161 if (independent) {
2162 for (j = 0; j < exist; ++j)
2163 if (j != i && value_notzero_p(P->Constraint[l][nvar+j+1]))
2164 break;
2165 if (j < exist)
2166 continue;
2169 for (int u = P->NbEq; u < P->NbConstraints; ++u) {
2170 if (value_posz_p(P->Constraint[u][nvar+i+1]))
2171 continue;
2173 if (independent) {
2174 for (j = 0; j < exist; ++j)
2175 if (j != i && value_notzero_p(P->Constraint[u][nvar+j+1]))
2176 break;
2177 if (j < exist)
2178 continue;
2181 if (SplitOnConstraint(P, i, l, u,
2182 nvar, len, exist, MaxRays,
2183 row, f, independent,
2184 pos, neg)) {
2185 if (independent) {
2186 if (i != 0)
2187 SwapColumns(*neg, nvar+1, nvar+1+i);
2189 return true;
2194 return false;
2197 static bool double_bound_pair(Polyhedron *P, int nvar, int exist,
2198 int i, int l1, int l2,
2199 Polyhedron **pos, Polyhedron **neg)
2201 Value f;
2202 value_init(f);
2203 Vector *row = Vector_Alloc(P->Dimension+2);
2204 value_set_si(row->p[0], 1);
2205 value_oppose(f, P->Constraint[l1][nvar+i+1]);
2206 Vector_Combine(P->Constraint[l1]+1, P->Constraint[l2]+1,
2207 row->p+1,
2208 P->Constraint[l2][nvar+i+1], f,
2209 P->Dimension+1);
2210 ConstraintSimplify(row->p, row->p, P->Dimension+2, &f);
2211 *pos = AddConstraints(row->p, 1, P, 0);
2212 value_set_si(f, -1);
2213 Vector_Scale(row->p+1, row->p+1, f, P->Dimension+1);
2214 value_decrement(row->p[P->Dimension+1], row->p[P->Dimension+1]);
2215 *neg = AddConstraints(row->p, 1, P, 0);
2216 Vector_Free(row);
2217 value_clear(f);
2219 return !emptyQ((*pos)) && !emptyQ((*neg));
2222 static bool double_bound(Polyhedron *P, int nvar, int exist,
2223 Polyhedron **pos, Polyhedron **neg)
2225 for (int i = 0; i < exist; ++i) {
2226 int l1, l2;
2227 for (l1 = P->NbEq; l1 < P->NbConstraints; ++l1) {
2228 if (value_negz_p(P->Constraint[l1][nvar+i+1]))
2229 continue;
2230 for (l2 = l1 + 1; l2 < P->NbConstraints; ++l2) {
2231 if (value_negz_p(P->Constraint[l2][nvar+i+1]))
2232 continue;
2233 if (double_bound_pair(P, nvar, exist, i, l1, l2, pos, neg))
2234 return true;
2237 for (l1 = P->NbEq; l1 < P->NbConstraints; ++l1) {
2238 if (value_posz_p(P->Constraint[l1][nvar+i+1]))
2239 continue;
2240 if (l1 < P->NbConstraints)
2241 for (l2 = l1 + 1; l2 < P->NbConstraints; ++l2) {
2242 if (value_posz_p(P->Constraint[l2][nvar+i+1]))
2243 continue;
2244 if (double_bound_pair(P, nvar, exist, i, l1, l2, pos, neg))
2245 return true;
2248 return false;
2250 return false;
2253 enum constraint {
2254 ALL_POS = 1 << 0,
2255 ONE_NEG = 1 << 1,
2256 INDEPENDENT = 1 << 2,
2257 ROT_NEG = 1 << 3
2260 static evalue* enumerate_or(Polyhedron *D,
2261 unsigned exist, unsigned nparam, unsigned MaxRays)
2263 #ifdef DEBUG_ER
2264 fprintf(stderr, "\nER: Or\n");
2265 #endif /* DEBUG_ER */
2267 Polyhedron *N = D->next;
2268 D->next = 0;
2269 evalue *EP =
2270 barvinok_enumerate_e(D, exist, nparam, MaxRays);
2271 Polyhedron_Free(D);
2273 for (D = N; D; D = N) {
2274 N = D->next;
2275 D->next = 0;
2277 evalue *EN =
2278 barvinok_enumerate_e(D, exist, nparam, MaxRays);
2280 eor(EN, EP);
2281 free_evalue_refs(EN);
2282 free(EN);
2283 Polyhedron_Free(D);
2286 reduce_evalue(EP);
2288 return EP;
2291 static evalue* enumerate_sum(Polyhedron *P,
2292 unsigned exist, unsigned nparam, unsigned MaxRays)
2294 int nvar = P->Dimension - exist - nparam;
2295 int toswap = nvar < exist ? nvar : exist;
2296 for (int i = 0; i < toswap; ++i)
2297 SwapColumns(P, 1 + i, nvar+exist - i);
2298 nparam += nvar;
2300 #ifdef DEBUG_ER
2301 fprintf(stderr, "\nER: Sum\n");
2302 #endif /* DEBUG_ER */
2304 evalue *EP = barvinok_enumerate_e(P, exist, nparam, MaxRays);
2306 for (int i = 0; i < /* nvar */ nparam; ++i) {
2307 Matrix *C = Matrix_Alloc(1, 1 + nparam + 1);
2308 value_set_si(C->p[0][0], 1);
2309 evalue split;
2310 value_init(split.d);
2311 value_set_si(split.d, 0);
2312 split.x.p = new_enode(partition, 4, nparam);
2313 value_set_si(C->p[0][1+i], 1);
2314 Matrix *C2 = Matrix_Copy(C);
2315 EVALUE_SET_DOMAIN(split.x.p->arr[0],
2316 Constraints2Polyhedron(C2, MaxRays));
2317 Matrix_Free(C2);
2318 evalue_set_si(&split.x.p->arr[1], 1, 1);
2319 value_set_si(C->p[0][1+i], -1);
2320 value_set_si(C->p[0][1+nparam], -1);
2321 EVALUE_SET_DOMAIN(split.x.p->arr[2],
2322 Constraints2Polyhedron(C, MaxRays));
2323 evalue_set_si(&split.x.p->arr[3], 1, 1);
2324 emul(&split, EP);
2325 free_evalue_refs(&split);
2326 Matrix_Free(C);
2328 reduce_evalue(EP);
2329 evalue_range_reduction(EP);
2331 evalue_frac2floor(EP);
2333 evalue *sum = esum(EP, nvar);
2335 free_evalue_refs(EP);
2336 free(EP);
2337 EP = sum;
2339 evalue_range_reduction(EP);
2341 return EP;
2344 static evalue* split_sure(Polyhedron *P, Polyhedron *S,
2345 unsigned exist, unsigned nparam, unsigned MaxRays)
2347 int nvar = P->Dimension - exist - nparam;
2349 Matrix *M = Matrix_Alloc(exist, S->Dimension+2);
2350 for (int i = 0; i < exist; ++i)
2351 value_set_si(M->p[i][nvar+i+1], 1);
2352 Polyhedron *O = S;
2353 S = DomainAddRays(S, M, MaxRays);
2354 Polyhedron_Free(O);
2355 Polyhedron *F = DomainAddRays(P, M, MaxRays);
2356 Polyhedron *D = DomainDifference(F, S, MaxRays);
2357 O = D;
2358 D = Disjoint_Domain(D, 0, MaxRays);
2359 Polyhedron_Free(F);
2360 Domain_Free(O);
2361 Matrix_Free(M);
2363 M = Matrix_Alloc(P->Dimension+1-exist, P->Dimension+1);
2364 for (int j = 0; j < nvar; ++j)
2365 value_set_si(M->p[j][j], 1);
2366 for (int j = 0; j < nparam+1; ++j)
2367 value_set_si(M->p[nvar+j][nvar+exist+j], 1);
2368 Polyhedron *T = Polyhedron_Image(S, M, MaxRays);
2369 evalue *EP = barvinok_enumerate_e(T, 0, nparam, MaxRays);
2370 Polyhedron_Free(S);
2371 Polyhedron_Free(T);
2372 Matrix_Free(M);
2374 for (Polyhedron *Q = D; Q; Q = Q->next) {
2375 Polyhedron *N = Q->next;
2376 Q->next = 0;
2377 T = DomainIntersection(P, Q, MaxRays);
2378 evalue *E = barvinok_enumerate_e(T, exist, nparam, MaxRays);
2379 eadd(E, EP);
2380 free_evalue_refs(E);
2381 free(E);
2382 Polyhedron_Free(T);
2383 Q->next = N;
2385 Domain_Free(D);
2386 return EP;
2389 static evalue* enumerate_sure(Polyhedron *P,
2390 unsigned exist, unsigned nparam, unsigned MaxRays)
2392 int i;
2393 Polyhedron *S = P;
2394 int nvar = P->Dimension - exist - nparam;
2395 Value lcm;
2396 Value f;
2397 value_init(lcm);
2398 value_init(f);
2400 for (i = 0; i < exist; ++i) {
2401 Matrix *M = Matrix_Alloc(S->NbConstraints, S->Dimension+2);
2402 int c = 0;
2403 value_set_si(lcm, 1);
2404 for (int j = 0; j < S->NbConstraints; ++j) {
2405 if (value_negz_p(S->Constraint[j][1+nvar+i]))
2406 continue;
2407 if (value_one_p(S->Constraint[j][1+nvar+i]))
2408 continue;
2409 value_lcm(lcm, S->Constraint[j][1+nvar+i], &lcm);
2412 for (int j = 0; j < S->NbConstraints; ++j) {
2413 if (value_negz_p(S->Constraint[j][1+nvar+i]))
2414 continue;
2415 if (value_one_p(S->Constraint[j][1+nvar+i]))
2416 continue;
2417 value_division(f, lcm, S->Constraint[j][1+nvar+i]);
2418 Vector_Scale(S->Constraint[j], M->p[c], f, S->Dimension+2);
2419 value_substract(M->p[c][S->Dimension+1],
2420 M->p[c][S->Dimension+1],
2421 lcm);
2422 value_increment(M->p[c][S->Dimension+1],
2423 M->p[c][S->Dimension+1]);
2424 ++c;
2426 Polyhedron *O = S;
2427 S = AddConstraints(M->p[0], c, S, MaxRays);
2428 if (O != P)
2429 Polyhedron_Free(O);
2430 Matrix_Free(M);
2431 if (emptyQ(S)) {
2432 Polyhedron_Free(S);
2433 value_clear(lcm);
2434 value_clear(f);
2435 return 0;
2438 value_clear(lcm);
2439 value_clear(f);
2441 #ifdef DEBUG_ER
2442 fprintf(stderr, "\nER: Sure\n");
2443 #endif /* DEBUG_ER */
2445 return split_sure(P, S, exist, nparam, MaxRays);
2448 static evalue* enumerate_sure2(Polyhedron *P,
2449 unsigned exist, unsigned nparam, unsigned MaxRays)
2451 int nvar = P->Dimension - exist - nparam;
2452 int r;
2453 for (r = 0; r < P->NbRays; ++r)
2454 if (value_one_p(P->Ray[r][0]) &&
2455 value_one_p(P->Ray[r][P->Dimension+1]))
2456 break;
2458 if (r >= P->NbRays)
2459 return 0;
2461 Matrix *M = Matrix_Alloc(nvar + 1 + nparam, P->Dimension+2);
2462 for (int i = 0; i < nvar; ++i)
2463 value_set_si(M->p[i][1+i], 1);
2464 for (int i = 0; i < nparam; ++i)
2465 value_set_si(M->p[i+nvar][1+nvar+exist+i], 1);
2466 Vector_Copy(P->Ray[r]+1+nvar, M->p[nvar+nparam]+1+nvar, exist);
2467 value_set_si(M->p[nvar+nparam][0], 1);
2468 value_set_si(M->p[nvar+nparam][P->Dimension+1], 1);
2469 Polyhedron * F = Rays2Polyhedron(M, MaxRays);
2470 Matrix_Free(M);
2472 Polyhedron *I = DomainIntersection(F, P, MaxRays);
2473 Polyhedron_Free(F);
2475 #ifdef DEBUG_ER
2476 fprintf(stderr, "\nER: Sure2\n");
2477 #endif /* DEBUG_ER */
2479 return split_sure(P, I, exist, nparam, MaxRays);
2482 static evalue* enumerate_cyclic(Polyhedron *P,
2483 unsigned exist, unsigned nparam,
2484 evalue * EP, int r, int p, unsigned MaxRays)
2486 int nvar = P->Dimension - exist - nparam;
2488 /* If EP in its fractional maps only contains references
2489 * to the remainder parameter with appropriate coefficients
2490 * then we could in principle avoid adding existentially
2491 * quantified variables to the validity domains.
2492 * We'd have to replace the remainder by m { p/m }
2493 * and multiply with an appropriate factor that is one
2494 * only in the appropriate range.
2495 * This last multiplication can be avoided if EP
2496 * has a single validity domain with no (further)
2497 * constraints on the remainder parameter
2500 Matrix *CT = Matrix_Alloc(nparam+1, nparam+3);
2501 Matrix *M = Matrix_Alloc(1, 1+nparam+3);
2502 for (int j = 0; j < nparam; ++j)
2503 if (j != p)
2504 value_set_si(CT->p[j][j], 1);
2505 value_set_si(CT->p[p][nparam+1], 1);
2506 value_set_si(CT->p[nparam][nparam+2], 1);
2507 value_set_si(M->p[0][1+p], -1);
2508 value_absolute(M->p[0][1+nparam], P->Ray[0][1+nvar+exist+p]);
2509 value_set_si(M->p[0][1+nparam+1], 1);
2510 Polyhedron *CEq = Constraints2Polyhedron(M, 1);
2511 Matrix_Free(M);
2512 addeliminatedparams_enum(EP, CT, CEq, MaxRays, nparam);
2513 Polyhedron_Free(CEq);
2514 Matrix_Free(CT);
2516 return EP;
2519 static void enumerate_vd_add_ray(evalue *EP, Matrix *Rays, unsigned MaxRays)
2521 if (value_notzero_p(EP->d))
2522 return;
2524 assert(EP->x.p->type == partition);
2525 assert(EP->x.p->pos == EVALUE_DOMAIN(EP->x.p->arr[0])->Dimension);
2526 for (int i = 0; i < EP->x.p->size/2; ++i) {
2527 Polyhedron *D = EVALUE_DOMAIN(EP->x.p->arr[2*i]);
2528 Polyhedron *N = DomainAddRays(D, Rays, MaxRays);
2529 EVALUE_SET_DOMAIN(EP->x.p->arr[2*i], N);
2530 Domain_Free(D);
2534 static evalue* enumerate_line(Polyhedron *P,
2535 unsigned exist, unsigned nparam, unsigned MaxRays)
2537 if (P->NbBid == 0)
2538 return 0;
2540 #ifdef DEBUG_ER
2541 fprintf(stderr, "\nER: Line\n");
2542 #endif /* DEBUG_ER */
2544 int nvar = P->Dimension - exist - nparam;
2545 int i, j;
2546 for (i = 0; i < nparam; ++i)
2547 if (value_notzero_p(P->Ray[0][1+nvar+exist+i]))
2548 break;
2549 assert(i < nparam);
2550 for (j = i+1; j < nparam; ++j)
2551 if (value_notzero_p(P->Ray[0][1+nvar+exist+i]))
2552 break;
2553 assert(j >= nparam); // for now
2555 Matrix *M = Matrix_Alloc(2, P->Dimension+2);
2556 value_set_si(M->p[0][0], 1);
2557 value_set_si(M->p[0][1+nvar+exist+i], 1);
2558 value_set_si(M->p[1][0], 1);
2559 value_set_si(M->p[1][1+nvar+exist+i], -1);
2560 value_absolute(M->p[1][1+P->Dimension], P->Ray[0][1+nvar+exist+i]);
2561 value_decrement(M->p[1][1+P->Dimension], M->p[1][1+P->Dimension]);
2562 Polyhedron *S = AddConstraints(M->p[0], 2, P, MaxRays);
2563 evalue *EP = barvinok_enumerate_e(S, exist, nparam, MaxRays);
2564 Polyhedron_Free(S);
2565 Matrix_Free(M);
2567 return enumerate_cyclic(P, exist, nparam, EP, 0, i, MaxRays);
2570 static int single_param_pos(Polyhedron*P, unsigned exist, unsigned nparam,
2571 int r)
2573 int nvar = P->Dimension - exist - nparam;
2574 if (First_Non_Zero(P->Ray[r]+1, nvar) != -1)
2575 return -1;
2576 int i = First_Non_Zero(P->Ray[r]+1+nvar+exist, nparam);
2577 if (i == -1)
2578 return -1;
2579 if (First_Non_Zero(P->Ray[r]+1+nvar+exist+1, nparam-i-1) != -1)
2580 return -1;
2581 return i;
2584 static evalue* enumerate_remove_ray(Polyhedron *P, int r,
2585 unsigned exist, unsigned nparam, unsigned MaxRays)
2587 #ifdef DEBUG_ER
2588 fprintf(stderr, "\nER: RedundantRay\n");
2589 #endif /* DEBUG_ER */
2591 Value one;
2592 value_init(one);
2593 value_set_si(one, 1);
2594 int len = P->NbRays-1;
2595 Matrix *M = Matrix_Alloc(2 * len, P->Dimension+2);
2596 Vector_Copy(P->Ray[0], M->p[0], r * (P->Dimension+2));
2597 Vector_Copy(P->Ray[r+1], M->p[r], (len-r) * (P->Dimension+2));
2598 for (int j = 0; j < P->NbRays; ++j) {
2599 if (j == r)
2600 continue;
2601 Vector_Combine(P->Ray[j], P->Ray[r], M->p[len+j-(j>r)],
2602 one, P->Ray[j][P->Dimension+1], P->Dimension+2);
2605 P = Rays2Polyhedron(M, MaxRays);
2606 Matrix_Free(M);
2607 evalue *EP = barvinok_enumerate_e(P, exist, nparam, MaxRays);
2608 Polyhedron_Free(P);
2609 value_clear(one);
2611 return EP;
2614 static evalue* enumerate_redundant_ray(Polyhedron *P,
2615 unsigned exist, unsigned nparam, unsigned MaxRays)
2617 assert(P->NbBid == 0);
2618 int nvar = P->Dimension - exist - nparam;
2619 Value m;
2620 value_init(m);
2622 for (int r = 0; r < P->NbRays; ++r) {
2623 if (value_notzero_p(P->Ray[r][P->Dimension+1]))
2624 continue;
2625 int i1 = single_param_pos(P, exist, nparam, r);
2626 if (i1 == -1)
2627 continue;
2628 for (int r2 = r+1; r2 < P->NbRays; ++r2) {
2629 if (value_notzero_p(P->Ray[r2][P->Dimension+1]))
2630 continue;
2631 int i2 = single_param_pos(P, exist, nparam, r2);
2632 if (i2 == -1)
2633 continue;
2634 if (i1 != i2)
2635 continue;
2637 value_division(m, P->Ray[r][1+nvar+exist+i1],
2638 P->Ray[r2][1+nvar+exist+i1]);
2639 value_multiply(m, m, P->Ray[r2][1+nvar+exist+i1]);
2640 /* r2 divides r => r redundant */
2641 if (value_eq(m, P->Ray[r][1+nvar+exist+i1])) {
2642 value_clear(m);
2643 return enumerate_remove_ray(P, r, exist, nparam, MaxRays);
2646 value_division(m, P->Ray[r2][1+nvar+exist+i1],
2647 P->Ray[r][1+nvar+exist+i1]);
2648 value_multiply(m, m, P->Ray[r][1+nvar+exist+i1]);
2649 /* r divides r2 => r2 redundant */
2650 if (value_eq(m, P->Ray[r2][1+nvar+exist+i1])) {
2651 value_clear(m);
2652 return enumerate_remove_ray(P, r2, exist, nparam, MaxRays);
2656 value_clear(m);
2657 return 0;
2660 static Polyhedron *upper_bound(Polyhedron *P,
2661 int pos, Value *max, Polyhedron **R)
2663 Value v;
2664 int r;
2665 value_init(v);
2667 *R = 0;
2668 Polyhedron *N;
2669 Polyhedron *B = 0;
2670 for (Polyhedron *Q = P; Q; Q = N) {
2671 N = Q->next;
2672 for (r = 0; r < P->NbRays; ++r) {
2673 if (value_zero_p(P->Ray[r][P->Dimension+1]) &&
2674 value_pos_p(P->Ray[r][1+pos]))
2675 break;
2677 if (r < P->NbRays) {
2678 Q->next = *R;
2679 *R = Q;
2680 continue;
2681 } else {
2682 Q->next = B;
2683 B = Q;
2685 for (r = 0; r < P->NbRays; ++r) {
2686 if (value_zero_p(P->Ray[r][P->Dimension+1]))
2687 continue;
2688 mpz_fdiv_q(v, P->Ray[r][1+pos], P->Ray[r][1+P->Dimension]);
2689 if ((!Q->next && r == 0) || value_gt(v, *max))
2690 value_assign(*max, v);
2693 value_clear(v);
2694 return B;
2697 static evalue* enumerate_ray(Polyhedron *P,
2698 unsigned exist, unsigned nparam, unsigned MaxRays)
2700 assert(P->NbBid == 0);
2701 int nvar = P->Dimension - exist - nparam;
2703 int r;
2704 for (r = 0; r < P->NbRays; ++r)
2705 if (value_zero_p(P->Ray[r][P->Dimension+1]))
2706 break;
2707 if (r >= P->NbRays)
2708 return 0;
2710 int r2;
2711 for (r2 = r+1; r2 < P->NbRays; ++r2)
2712 if (value_zero_p(P->Ray[r2][P->Dimension+1]))
2713 break;
2714 if (r2 < P->NbRays) {
2715 if (nvar > 0)
2716 return enumerate_sum(P, exist, nparam, MaxRays);
2719 #ifdef DEBUG_ER
2720 fprintf(stderr, "\nER: Ray\n");
2721 #endif /* DEBUG_ER */
2723 Value m;
2724 Value one;
2725 value_init(m);
2726 value_init(one);
2727 value_set_si(one, 1);
2728 int i = single_param_pos(P, exist, nparam, r);
2729 assert(i != -1); // for now;
2731 Matrix *M = Matrix_Alloc(P->NbRays, P->Dimension+2);
2732 for (int j = 0; j < P->NbRays; ++j) {
2733 Vector_Combine(P->Ray[j], P->Ray[r], M->p[j],
2734 one, P->Ray[j][P->Dimension+1], P->Dimension+2);
2736 Polyhedron *S = Rays2Polyhedron(M, MaxRays);
2737 Matrix_Free(M);
2738 Polyhedron *D = DomainDifference(P, S, MaxRays);
2739 Polyhedron_Free(S);
2740 // Polyhedron_Print(stderr, P_VALUE_FMT, D);
2741 assert(value_pos_p(P->Ray[r][1+nvar+exist+i])); // for now
2742 Polyhedron *R;
2743 D = upper_bound(D, nvar+exist+i, &m, &R);
2744 assert(D);
2745 Domain_Free(D);
2747 M = Matrix_Alloc(2, P->Dimension+2);
2748 value_set_si(M->p[0][0], 1);
2749 value_set_si(M->p[1][0], 1);
2750 value_set_si(M->p[0][1+nvar+exist+i], -1);
2751 value_set_si(M->p[1][1+nvar+exist+i], 1);
2752 value_assign(M->p[0][1+P->Dimension], m);
2753 value_oppose(M->p[1][1+P->Dimension], m);
2754 value_addto(M->p[1][1+P->Dimension], M->p[1][1+P->Dimension],
2755 P->Ray[r][1+nvar+exist+i]);
2756 value_decrement(M->p[1][1+P->Dimension], M->p[1][1+P->Dimension]);
2757 // Matrix_Print(stderr, P_VALUE_FMT, M);
2758 D = AddConstraints(M->p[0], 2, P, MaxRays);
2759 // Polyhedron_Print(stderr, P_VALUE_FMT, D);
2760 value_substract(M->p[0][1+P->Dimension], M->p[0][1+P->Dimension],
2761 P->Ray[r][1+nvar+exist+i]);
2762 // Matrix_Print(stderr, P_VALUE_FMT, M);
2763 S = AddConstraints(M->p[0], 1, P, MaxRays);
2764 // Polyhedron_Print(stderr, P_VALUE_FMT, S);
2765 Matrix_Free(M);
2767 evalue *EP = barvinok_enumerate_e(D, exist, nparam, MaxRays);
2768 Polyhedron_Free(D);
2769 value_clear(one);
2770 value_clear(m);
2772 if (value_notone_p(P->Ray[r][1+nvar+exist+i]))
2773 EP = enumerate_cyclic(P, exist, nparam, EP, r, i, MaxRays);
2774 else {
2775 M = Matrix_Alloc(1, nparam+2);
2776 value_set_si(M->p[0][0], 1);
2777 value_set_si(M->p[0][1+i], 1);
2778 enumerate_vd_add_ray(EP, M, MaxRays);
2779 Matrix_Free(M);
2782 if (!emptyQ(S)) {
2783 evalue *E = barvinok_enumerate_e(S, exist, nparam, MaxRays);
2784 eadd(E, EP);
2785 free_evalue_refs(E);
2786 free(E);
2788 Polyhedron_Free(S);
2790 if (R) {
2791 assert(nvar == 0);
2792 evalue *ER = enumerate_or(R, exist, nparam, MaxRays);
2793 eor(ER, EP);
2794 free_evalue_refs(ER);
2795 free(ER);
2798 return EP;
2801 static evalue* new_zero_ep()
2803 evalue *EP;
2804 ALLOC(evalue, EP);
2805 value_init(EP->d);
2806 evalue_set_si(EP, 0, 1);
2807 return EP;
2810 static evalue* enumerate_vd(Polyhedron **PA,
2811 unsigned exist, unsigned nparam, unsigned MaxRays)
2813 Polyhedron *P = *PA;
2814 int nvar = P->Dimension - exist - nparam;
2815 Param_Polyhedron *PP = NULL;
2816 Polyhedron *C = Universe_Polyhedron(nparam);
2817 Polyhedron *CEq;
2818 Matrix *CT;
2819 Polyhedron *PR = P;
2820 PP = Polyhedron2Param_SimplifiedDomain(&PR,C,MaxRays,&CEq,&CT);
2821 Polyhedron_Free(C);
2823 int nd;
2824 Param_Domain *D, *last;
2825 Value c;
2826 value_init(c);
2827 for (nd = 0, D=PP->D; D; D=D->next, ++nd)
2830 Polyhedron **VD = new Polyhedron_p[nd];
2831 Polyhedron **fVD = new Polyhedron_p[nd];
2832 for(nd = 0, D=PP->D; D; D=D->next) {
2833 Polyhedron *rVD = reduce_domain(D->Domain, CT, CEq,
2834 fVD, nd, MaxRays);
2835 if (!rVD)
2836 continue;
2838 VD[nd++] = rVD;
2839 last = D;
2842 evalue *EP = 0;
2844 if (nd == 0)
2845 EP = new_zero_ep();
2847 /* This doesn't seem to have any effect */
2848 if (nd == 1) {
2849 Polyhedron *CA = align_context(VD[0], P->Dimension, MaxRays);
2850 Polyhedron *O = P;
2851 P = DomainIntersection(P, CA, MaxRays);
2852 if (O != *PA)
2853 Polyhedron_Free(O);
2854 Polyhedron_Free(CA);
2855 if (emptyQ(P))
2856 EP = new_zero_ep();
2859 if (!EP && CT->NbColumns != CT->NbRows) {
2860 Polyhedron *CEqr = DomainImage(CEq, CT, MaxRays);
2861 Polyhedron *CA = align_context(CEqr, PR->Dimension, MaxRays);
2862 Polyhedron *I = DomainIntersection(PR, CA, MaxRays);
2863 Polyhedron_Free(CEqr);
2864 Polyhedron_Free(CA);
2865 #ifdef DEBUG_ER
2866 fprintf(stderr, "\nER: Eliminate\n");
2867 #endif /* DEBUG_ER */
2868 nparam -= CT->NbColumns - CT->NbRows;
2869 EP = barvinok_enumerate_e(I, exist, nparam, MaxRays);
2870 nparam += CT->NbColumns - CT->NbRows;
2871 addeliminatedparams_enum(EP, CT, CEq, MaxRays, nparam);
2872 Polyhedron_Free(I);
2874 if (PR != *PA)
2875 Polyhedron_Free(PR);
2876 PR = 0;
2878 if (!EP && nd > 1) {
2879 #ifdef DEBUG_ER
2880 fprintf(stderr, "\nER: VD\n");
2881 #endif /* DEBUG_ER */
2882 for (int i = 0; i < nd; ++i) {
2883 Polyhedron *CA = align_context(VD[i], P->Dimension, MaxRays);
2884 Polyhedron *I = DomainIntersection(P, CA, MaxRays);
2886 if (i == 0)
2887 EP = barvinok_enumerate_e(I, exist, nparam, MaxRays);
2888 else {
2889 evalue *E = barvinok_enumerate_e(I, exist, nparam, MaxRays);
2890 eadd(E, EP);
2891 free_evalue_refs(E);
2892 free(E);
2894 Polyhedron_Free(I);
2895 Polyhedron_Free(CA);
2899 for (int i = 0; i < nd; ++i) {
2900 Polyhedron_Free(VD[i]);
2901 Polyhedron_Free(fVD[i]);
2903 delete [] VD;
2904 delete [] fVD;
2905 value_clear(c);
2907 if (!EP && nvar == 0) {
2908 Value f;
2909 value_init(f);
2910 Param_Vertices *V, *V2;
2911 Matrix* M = Matrix_Alloc(1, P->Dimension+2);
2913 FORALL_PVertex_in_ParamPolyhedron(V, last, PP) {
2914 bool found = false;
2915 FORALL_PVertex_in_ParamPolyhedron(V2, last, PP) {
2916 if (V == V2) {
2917 found = true;
2918 continue;
2920 if (!found)
2921 continue;
2922 for (int i = 0; i < exist; ++i) {
2923 value_oppose(f, V->Vertex->p[i][nparam+1]);
2924 Vector_Combine(V->Vertex->p[i],
2925 V2->Vertex->p[i],
2926 M->p[0] + 1 + nvar + exist,
2927 V2->Vertex->p[i][nparam+1],
2929 nparam+1);
2930 int j;
2931 for (j = 0; j < nparam; ++j)
2932 if (value_notzero_p(M->p[0][1+nvar+exist+j]))
2933 break;
2934 if (j >= nparam)
2935 continue;
2936 ConstraintSimplify(M->p[0], M->p[0],
2937 P->Dimension+2, &f);
2938 value_set_si(M->p[0][0], 0);
2939 Polyhedron *para = AddConstraints(M->p[0], 1, P,
2940 MaxRays);
2941 if (emptyQ(para)) {
2942 Polyhedron_Free(para);
2943 continue;
2945 Polyhedron *pos, *neg;
2946 value_set_si(M->p[0][0], 1);
2947 value_decrement(M->p[0][P->Dimension+1],
2948 M->p[0][P->Dimension+1]);
2949 neg = AddConstraints(M->p[0], 1, P, MaxRays);
2950 value_set_si(f, -1);
2951 Vector_Scale(M->p[0]+1, M->p[0]+1, f,
2952 P->Dimension+1);
2953 value_decrement(M->p[0][P->Dimension+1],
2954 M->p[0][P->Dimension+1]);
2955 value_decrement(M->p[0][P->Dimension+1],
2956 M->p[0][P->Dimension+1]);
2957 pos = AddConstraints(M->p[0], 1, P, MaxRays);
2958 if (emptyQ(neg) && emptyQ(pos)) {
2959 Polyhedron_Free(para);
2960 Polyhedron_Free(pos);
2961 Polyhedron_Free(neg);
2962 continue;
2964 #ifdef DEBUG_ER
2965 fprintf(stderr, "\nER: Order\n");
2966 #endif /* DEBUG_ER */
2967 EP = barvinok_enumerate_e(para, exist, nparam, MaxRays);
2968 evalue *E;
2969 if (!emptyQ(pos)) {
2970 E = barvinok_enumerate_e(pos, exist, nparam, MaxRays);
2971 eadd(E, EP);
2972 free_evalue_refs(E);
2973 free(E);
2975 if (!emptyQ(neg)) {
2976 E = barvinok_enumerate_e(neg, exist, nparam, MaxRays);
2977 eadd(E, EP);
2978 free_evalue_refs(E);
2979 free(E);
2981 Polyhedron_Free(para);
2982 Polyhedron_Free(pos);
2983 Polyhedron_Free(neg);
2984 break;
2986 if (EP)
2987 break;
2988 } END_FORALL_PVertex_in_ParamPolyhedron;
2989 if (EP)
2990 break;
2991 } END_FORALL_PVertex_in_ParamPolyhedron;
2993 if (!EP) {
2994 /* Search for vertex coordinate to split on */
2995 /* First look for one independent of the parameters */
2996 FORALL_PVertex_in_ParamPolyhedron(V, last, PP) {
2997 for (int i = 0; i < exist; ++i) {
2998 int j;
2999 for (j = 0; j < nparam; ++j)
3000 if (value_notzero_p(V->Vertex->p[i][j]))
3001 break;
3002 if (j < nparam)
3003 continue;
3004 value_set_si(M->p[0][0], 1);
3005 Vector_Set(M->p[0]+1, 0, nvar+exist);
3006 Vector_Copy(V->Vertex->p[i],
3007 M->p[0] + 1 + nvar + exist, nparam+1);
3008 value_oppose(M->p[0][1+nvar+i],
3009 V->Vertex->p[i][nparam+1]);
3011 Polyhedron *pos, *neg;
3012 value_set_si(M->p[0][0], 1);
3013 value_decrement(M->p[0][P->Dimension+1],
3014 M->p[0][P->Dimension+1]);
3015 neg = AddConstraints(M->p[0], 1, P, MaxRays);
3016 value_set_si(f, -1);
3017 Vector_Scale(M->p[0]+1, M->p[0]+1, f,
3018 P->Dimension+1);
3019 value_decrement(M->p[0][P->Dimension+1],
3020 M->p[0][P->Dimension+1]);
3021 value_decrement(M->p[0][P->Dimension+1],
3022 M->p[0][P->Dimension+1]);
3023 pos = AddConstraints(M->p[0], 1, P, MaxRays);
3024 if (emptyQ(neg) || emptyQ(pos)) {
3025 Polyhedron_Free(pos);
3026 Polyhedron_Free(neg);
3027 continue;
3029 Polyhedron_Free(pos);
3030 value_increment(M->p[0][P->Dimension+1],
3031 M->p[0][P->Dimension+1]);
3032 pos = AddConstraints(M->p[0], 1, P, MaxRays);
3033 #ifdef DEBUG_ER
3034 fprintf(stderr, "\nER: Vertex\n");
3035 #endif /* DEBUG_ER */
3036 pos->next = neg;
3037 EP = enumerate_or(pos, exist, nparam, MaxRays);
3038 break;
3040 if (EP)
3041 break;
3042 } END_FORALL_PVertex_in_ParamPolyhedron;
3045 if (!EP) {
3046 /* Search for vertex coordinate to split on */
3047 /* Now look for one that depends on the parameters */
3048 FORALL_PVertex_in_ParamPolyhedron(V, last, PP) {
3049 for (int i = 0; i < exist; ++i) {
3050 value_set_si(M->p[0][0], 1);
3051 Vector_Set(M->p[0]+1, 0, nvar+exist);
3052 Vector_Copy(V->Vertex->p[i],
3053 M->p[0] + 1 + nvar + exist, nparam+1);
3054 value_oppose(M->p[0][1+nvar+i],
3055 V->Vertex->p[i][nparam+1]);
3057 Polyhedron *pos, *neg;
3058 value_set_si(M->p[0][0], 1);
3059 value_decrement(M->p[0][P->Dimension+1],
3060 M->p[0][P->Dimension+1]);
3061 neg = AddConstraints(M->p[0], 1, P, MaxRays);
3062 value_set_si(f, -1);
3063 Vector_Scale(M->p[0]+1, M->p[0]+1, f,
3064 P->Dimension+1);
3065 value_decrement(M->p[0][P->Dimension+1],
3066 M->p[0][P->Dimension+1]);
3067 value_decrement(M->p[0][P->Dimension+1],
3068 M->p[0][P->Dimension+1]);
3069 pos = AddConstraints(M->p[0], 1, P, MaxRays);
3070 if (emptyQ(neg) || emptyQ(pos)) {
3071 Polyhedron_Free(pos);
3072 Polyhedron_Free(neg);
3073 continue;
3075 Polyhedron_Free(pos);
3076 value_increment(M->p[0][P->Dimension+1],
3077 M->p[0][P->Dimension+1]);
3078 pos = AddConstraints(M->p[0], 1, P, MaxRays);
3079 #ifdef DEBUG_ER
3080 fprintf(stderr, "\nER: ParamVertex\n");
3081 #endif /* DEBUG_ER */
3082 pos->next = neg;
3083 EP = enumerate_or(pos, exist, nparam, MaxRays);
3084 break;
3086 if (EP)
3087 break;
3088 } END_FORALL_PVertex_in_ParamPolyhedron;
3091 Matrix_Free(M);
3092 value_clear(f);
3095 if (CEq)
3096 Polyhedron_Free(CEq);
3097 if (CT)
3098 Matrix_Free(CT);
3099 if (PP)
3100 Param_Polyhedron_Free(PP);
3101 *PA = P;
3103 return EP;
3106 #ifndef HAVE_PIPLIB
3107 evalue *barvinok_enumerate_pip(Polyhedron *P,
3108 unsigned exist, unsigned nparam, unsigned MaxRays)
3110 return 0;
3112 #else
3113 evalue *barvinok_enumerate_pip(Polyhedron *P,
3114 unsigned exist, unsigned nparam, unsigned MaxRays)
3116 int nvar = P->Dimension - exist - nparam;
3117 evalue *EP = new_zero_ep();
3118 Polyhedron *Q, *N, *T = 0;
3119 Value min, tmp;
3120 value_init(min);
3121 value_init(tmp);
3123 #ifdef DEBUG_ER
3124 fprintf(stderr, "\nER: PIP\n");
3125 #endif /* DEBUG_ER */
3127 for (int i = 0; i < P->Dimension; ++i) {
3128 bool pos = false;
3129 bool neg = false;
3130 bool posray = false;
3131 bool negray = false;
3132 value_set_si(min, 0);
3133 for (int j = 0; j < P->NbRays; ++j) {
3134 if (value_pos_p(P->Ray[j][1+i])) {
3135 pos = true;
3136 if (value_zero_p(P->Ray[j][1+P->Dimension]))
3137 posray = true;
3138 } else if (value_neg_p(P->Ray[j][1+i])) {
3139 neg = true;
3140 if (value_zero_p(P->Ray[j][1+P->Dimension]))
3141 negray = true;
3142 else {
3143 mpz_fdiv_q(tmp,
3144 P->Ray[j][1+i], P->Ray[j][1+P->Dimension]);
3145 if (value_lt(tmp, min))
3146 value_assign(min, tmp);
3150 if (pos && neg) {
3151 assert(!(posray && negray));
3152 assert(!negray); // for now
3153 Polyhedron *O = T ? T : P;
3154 /* shift by a safe amount */
3155 Matrix *M = Matrix_Alloc(O->NbRays, O->Dimension+2);
3156 Vector_Copy(O->Ray[0], M->p[0], O->NbRays * (O->Dimension+2));
3157 for (int j = 0; j < P->NbRays; ++j) {
3158 if (value_notzero_p(M->p[j][1+P->Dimension])) {
3159 value_multiply(tmp, min, M->p[j][1+P->Dimension]);
3160 value_substract(M->p[j][1+i], M->p[j][1+i], tmp);
3163 if (T)
3164 Polyhedron_Free(T);
3165 T = Rays2Polyhedron(M, MaxRays);
3166 Matrix_Free(M);
3167 } else if (neg) {
3168 /* negating a parameter requires that we substitute in the
3169 * sign again afterwards.
3170 * Disallow for now.
3172 assert(i < nvar+exist);
3173 if (!T)
3174 T = Polyhedron_Copy(P);
3175 for (int j = 0; j < T->NbRays; ++j)
3176 value_oppose(T->Ray[j][1+i], T->Ray[j][1+i]);
3177 for (int j = 0; j < T->NbConstraints; ++j)
3178 value_oppose(T->Constraint[j][1+i], T->Constraint[j][1+i]);
3181 value_clear(min);
3182 value_clear(tmp);
3184 Polyhedron *D = pip_lexmin(T ? T : P, exist, nparam);
3185 for (Q = D; Q; Q = N) {
3186 N = Q->next;
3187 Q->next = 0;
3188 evalue *E;
3189 exist = Q->Dimension - nvar - nparam;
3190 E = barvinok_enumerate_e(Q, exist, nparam, MaxRays);
3191 Polyhedron_Free(Q);
3192 eadd(E, EP);
3193 free_evalue_refs(E);
3194 free(E);
3197 if (T)
3198 Polyhedron_Free(T);
3200 return EP;
3202 #endif
3205 static bool is_single(Value *row, int pos, int len)
3207 return First_Non_Zero(row, pos) == -1 &&
3208 First_Non_Zero(row+pos+1, len-pos-1) == -1;
3211 static evalue* barvinok_enumerate_e_r(Polyhedron *P,
3212 unsigned exist, unsigned nparam, unsigned MaxRays);
3214 #ifdef DEBUG_ER
3215 static int er_level = 0;
3217 evalue* barvinok_enumerate_e(Polyhedron *P,
3218 unsigned exist, unsigned nparam, unsigned MaxRays)
3220 fprintf(stderr, "\nER: level %i\n", er_level);
3221 int nvar = P->Dimension - exist - nparam;
3222 fprintf(stderr, "%d %d %d\n", nvar, exist, nparam);
3224 Polyhedron_Print(stderr, P_VALUE_FMT, P);
3225 ++er_level;
3226 P = DomainConstraintSimplify(Polyhedron_Copy(P), MaxRays);
3227 evalue *EP = barvinok_enumerate_e_r(P, exist, nparam, MaxRays);
3228 Polyhedron_Free(P);
3229 --er_level;
3230 return EP;
3232 #else
3233 evalue* barvinok_enumerate_e(Polyhedron *P,
3234 unsigned exist, unsigned nparam, unsigned MaxRays)
3236 P = DomainConstraintSimplify(Polyhedron_Copy(P), MaxRays);
3237 evalue *EP = barvinok_enumerate_e_r(P, exist, nparam, MaxRays);
3238 Polyhedron_Free(P);
3239 return EP;
3241 #endif
3243 static evalue* barvinok_enumerate_e_r(Polyhedron *P,
3244 unsigned exist, unsigned nparam, unsigned MaxRays)
3246 if (exist == 0) {
3247 Polyhedron *U = Universe_Polyhedron(nparam);
3248 evalue *EP = barvinok_enumerate_ev(P, U, MaxRays);
3249 //char *param_name[] = {"P", "Q", "R", "S", "T" };
3250 //print_evalue(stdout, EP, param_name);
3251 Polyhedron_Free(U);
3252 return EP;
3255 int nvar = P->Dimension - exist - nparam;
3256 int len = P->Dimension + 2;
3258 if (emptyQ(P))
3259 return new_zero_ep();
3261 if (nvar == 0 && nparam == 0) {
3262 evalue *EP = new_zero_ep();
3263 barvinok_count(P, &EP->x.n, MaxRays);
3264 if (value_pos_p(EP->x.n))
3265 value_set_si(EP->x.n, 1);
3266 return EP;
3269 int r;
3270 for (r = 0; r < P->NbRays; ++r)
3271 if (value_zero_p(P->Ray[r][0]) ||
3272 value_zero_p(P->Ray[r][P->Dimension+1])) {
3273 int i;
3274 for (i = 0; i < nvar; ++i)
3275 if (value_notzero_p(P->Ray[r][i+1]))
3276 break;
3277 if (i >= nvar)
3278 continue;
3279 for (i = nvar + exist; i < nvar + exist + nparam; ++i)
3280 if (value_notzero_p(P->Ray[r][i+1]))
3281 break;
3282 if (i >= nvar + exist + nparam)
3283 break;
3285 if (r < P->NbRays) {
3286 evalue *EP = new_zero_ep();
3287 value_set_si(EP->x.n, -1);
3288 return EP;
3291 int first;
3292 for (r = 0; r < P->NbEq; ++r)
3293 if ((first = First_Non_Zero(P->Constraint[r]+1+nvar, exist)) != -1)
3294 break;
3295 if (r < P->NbEq) {
3296 if (First_Non_Zero(P->Constraint[r]+1+nvar+first+1,
3297 exist-first-1) != -1) {
3298 Polyhedron *T = rotate_along(P, r, nvar, exist, MaxRays);
3299 #ifdef DEBUG_ER
3300 fprintf(stderr, "\nER: Equality\n");
3301 #endif /* DEBUG_ER */
3302 evalue *EP = barvinok_enumerate_e(T, exist-1, nparam, MaxRays);
3303 Polyhedron_Free(T);
3304 return EP;
3305 } else {
3306 #ifdef DEBUG_ER
3307 fprintf(stderr, "\nER: Fixed\n");
3308 #endif /* DEBUG_ER */
3309 if (first == 0)
3310 return barvinok_enumerate_e(P, exist-1, nparam, MaxRays);
3311 else {
3312 Polyhedron *T = Polyhedron_Copy(P);
3313 SwapColumns(T, nvar+1, nvar+1+first);
3314 evalue *EP = barvinok_enumerate_e(T, exist-1, nparam, MaxRays);
3315 Polyhedron_Free(T);
3316 return EP;
3321 Vector *row = Vector_Alloc(len);
3322 value_set_si(row->p[0], 1);
3324 Value f;
3325 value_init(f);
3327 enum constraint* info = new constraint[exist];
3328 for (int i = 0; i < exist; ++i) {
3329 info[i] = ALL_POS;
3330 for (int l = P->NbEq; l < P->NbConstraints; ++l) {
3331 if (value_negz_p(P->Constraint[l][nvar+i+1]))
3332 continue;
3333 bool l_parallel = is_single(P->Constraint[l]+nvar+1, i, exist);
3334 for (int u = P->NbEq; u < P->NbConstraints; ++u) {
3335 if (value_posz_p(P->Constraint[u][nvar+i+1]))
3336 continue;
3337 bool lu_parallel = l_parallel ||
3338 is_single(P->Constraint[u]+nvar+1, i, exist);
3339 value_oppose(f, P->Constraint[u][nvar+i+1]);
3340 Vector_Combine(P->Constraint[l]+1, P->Constraint[u]+1, row->p+1,
3341 f, P->Constraint[l][nvar+i+1], len-1);
3342 if (!(info[i] & INDEPENDENT)) {
3343 int j;
3344 for (j = 0; j < exist; ++j)
3345 if (j != i && value_notzero_p(row->p[nvar+j+1]))
3346 break;
3347 if (j == exist) {
3348 //printf("independent: i: %d, l: %d, u: %d\n", i, l, u);
3349 info[i] = (constraint)(info[i] | INDEPENDENT);
3352 if (info[i] & ALL_POS) {
3353 value_addto(row->p[len-1], row->p[len-1],
3354 P->Constraint[l][nvar+i+1]);
3355 value_addto(row->p[len-1], row->p[len-1], f);
3356 value_multiply(f, f, P->Constraint[l][nvar+i+1]);
3357 value_substract(row->p[len-1], row->p[len-1], f);
3358 value_decrement(row->p[len-1], row->p[len-1]);
3359 ConstraintSimplify(row->p, row->p, len, &f);
3360 value_set_si(f, -1);
3361 Vector_Scale(row->p+1, row->p+1, f, len-1);
3362 value_decrement(row->p[len-1], row->p[len-1]);
3363 Polyhedron *T = AddConstraints(row->p, 1, P, MaxRays);
3364 if (!emptyQ(T)) {
3365 //printf("not all_pos: i: %d, l: %d, u: %d\n", i, l, u);
3366 info[i] = (constraint)(info[i] ^ ALL_POS);
3368 //puts("pos remainder");
3369 //Polyhedron_Print(stdout, P_VALUE_FMT, T);
3370 Polyhedron_Free(T);
3372 if (!(info[i] & ONE_NEG)) {
3373 if (lu_parallel) {
3374 negative_test_constraint(P->Constraint[l],
3375 P->Constraint[u],
3376 row->p, nvar+i, len, &f);
3377 oppose_constraint(row->p, len, &f);
3378 Polyhedron *T = AddConstraints(row->p, 1, P, MaxRays);
3379 if (emptyQ(T)) {
3380 //printf("one_neg i: %d, l: %d, u: %d\n", i, l, u);
3381 info[i] = (constraint)(info[i] | ONE_NEG);
3383 //puts("neg remainder");
3384 //Polyhedron_Print(stdout, P_VALUE_FMT, T);
3385 Polyhedron_Free(T);
3386 } else if (!(info[i] & ROT_NEG)) {
3387 if (parallel_constraints(P->Constraint[l],
3388 P->Constraint[u],
3389 row->p, nvar, exist)) {
3390 negative_test_constraint7(P->Constraint[l],
3391 P->Constraint[u],
3392 row->p, nvar, exist,
3393 len, &f);
3394 oppose_constraint(row->p, len, &f);
3395 Polyhedron *T = AddConstraints(row->p, 1, P, MaxRays);
3396 if (emptyQ(T)) {
3397 // printf("rot_neg i: %d, l: %d, u: %d\n", i, l, u);
3398 info[i] = (constraint)(info[i] | ROT_NEG);
3399 r = l;
3401 //puts("neg remainder");
3402 //Polyhedron_Print(stdout, P_VALUE_FMT, T);
3403 Polyhedron_Free(T);
3407 if (!(info[i] & ALL_POS) && (info[i] & (ONE_NEG | ROT_NEG)))
3408 goto next;
3411 if (info[i] & ALL_POS)
3412 break;
3413 next:
3418 for (int i = 0; i < exist; ++i)
3419 printf("%i: %i\n", i, info[i]);
3421 for (int i = 0; i < exist; ++i)
3422 if (info[i] & ALL_POS) {
3423 #ifdef DEBUG_ER
3424 fprintf(stderr, "\nER: Positive\n");
3425 #endif /* DEBUG_ER */
3426 // Eliminate
3427 // Maybe we should chew off some of the fat here
3428 Matrix *M = Matrix_Alloc(P->Dimension, P->Dimension+1);
3429 for (int j = 0; j < P->Dimension; ++j)
3430 value_set_si(M->p[j][j + (j >= i+nvar)], 1);
3431 Polyhedron *T = Polyhedron_Image(P, M, MaxRays);
3432 Matrix_Free(M);
3433 evalue *EP = barvinok_enumerate_e(T, exist-1, nparam, MaxRays);
3434 Polyhedron_Free(T);
3435 value_clear(f);
3436 Vector_Free(row);
3437 delete [] info;
3438 return EP;
3440 for (int i = 0; i < exist; ++i)
3441 if (info[i] & ONE_NEG) {
3442 #ifdef DEBUG_ER
3443 fprintf(stderr, "\nER: Negative\n");
3444 #endif /* DEBUG_ER */
3445 Vector_Free(row);
3446 value_clear(f);
3447 delete [] info;
3448 if (i == 0)
3449 return barvinok_enumerate_e(P, exist-1, nparam, MaxRays);
3450 else {
3451 Polyhedron *T = Polyhedron_Copy(P);
3452 SwapColumns(T, nvar+1, nvar+1+i);
3453 evalue *EP = barvinok_enumerate_e(T, exist-1, nparam, MaxRays);
3454 Polyhedron_Free(T);
3455 return EP;
3458 for (int i = 0; i < exist; ++i)
3459 if (info[i] & ROT_NEG) {
3460 #ifdef DEBUG_ER
3461 fprintf(stderr, "\nER: Rotate\n");
3462 #endif /* DEBUG_ER */
3463 Vector_Free(row);
3464 value_clear(f);
3465 delete [] info;
3466 Polyhedron *T = rotate_along(P, r, nvar, exist, MaxRays);
3467 evalue *EP = barvinok_enumerate_e(T, exist-1, nparam, MaxRays);
3468 Polyhedron_Free(T);
3469 return EP;
3471 for (int i = 0; i < exist; ++i)
3472 if (info[i] & INDEPENDENT) {
3473 Polyhedron *pos, *neg;
3475 /* Find constraint again and split off negative part */
3477 if (SplitOnVar(P, i, nvar, len, exist, MaxRays,
3478 row, f, true, &pos, &neg)) {
3479 #ifdef DEBUG_ER
3480 fprintf(stderr, "\nER: Split\n");
3481 #endif /* DEBUG_ER */
3483 evalue *EP =
3484 barvinok_enumerate_e(neg, exist-1, nparam, MaxRays);
3485 evalue *E =
3486 barvinok_enumerate_e(pos, exist, nparam, MaxRays);
3487 eadd(E, EP);
3488 free_evalue_refs(E);
3489 free(E);
3490 Polyhedron_Free(neg);
3491 Polyhedron_Free(pos);
3492 value_clear(f);
3493 Vector_Free(row);
3494 delete [] info;
3495 return EP;
3498 delete [] info;
3500 Polyhedron *O = P;
3501 Polyhedron *F;
3503 evalue *EP;
3505 EP = enumerate_line(P, exist, nparam, MaxRays);
3506 if (EP)
3507 goto out;
3509 EP = barvinok_enumerate_pip(P, exist, nparam, MaxRays);
3510 if (EP)
3511 goto out;
3513 EP = enumerate_redundant_ray(P, exist, nparam, MaxRays);
3514 if (EP)
3515 goto out;
3517 EP = enumerate_sure(P, exist, nparam, MaxRays);
3518 if (EP)
3519 goto out;
3521 EP = enumerate_ray(P, exist, nparam, MaxRays);
3522 if (EP)
3523 goto out;
3525 EP = enumerate_sure2(P, exist, nparam, MaxRays);
3526 if (EP)
3527 goto out;
3529 F = unfringe(P, MaxRays);
3530 if (!PolyhedronIncludes(F, P)) {
3531 #ifdef DEBUG_ER
3532 fprintf(stderr, "\nER: Fringed\n");
3533 #endif /* DEBUG_ER */
3534 EP = barvinok_enumerate_e(F, exist, nparam, MaxRays);
3535 Polyhedron_Free(F);
3536 goto out;
3538 Polyhedron_Free(F);
3540 if (nparam)
3541 EP = enumerate_vd(&P, exist, nparam, MaxRays);
3542 if (EP)
3543 goto out2;
3545 if (nvar != 0) {
3546 EP = enumerate_sum(P, exist, nparam, MaxRays);
3547 goto out2;
3550 assert(nvar == 0);
3552 int i;
3553 Polyhedron *pos, *neg;
3554 for (i = 0; i < exist; ++i)
3555 if (SplitOnVar(P, i, nvar, len, exist, MaxRays,
3556 row, f, false, &pos, &neg))
3557 break;
3559 assert (i < exist);
3561 pos->next = neg;
3562 EP = enumerate_or(pos, exist, nparam, MaxRays);
3564 out2:
3565 if (O != P)
3566 Polyhedron_Free(P);
3568 out:
3569 value_clear(f);
3570 Vector_Free(row);
3571 return EP;
3574 gen_fun * barvinok_series(Polyhedron *P, Polyhedron* C, unsigned MaxRays)
3576 Polyhedron ** vcone;
3577 Polyhedron *CA;
3578 unsigned nparam = C->Dimension;
3579 unsigned dim, nvar;
3580 vec_ZZ sign;
3581 int ncone = 0;
3582 sign.SetLength(ncone);
3584 CA = align_context(C, P->Dimension, MaxRays);
3585 P = DomainIntersection(P, CA, MaxRays);
3586 Polyhedron_Free(CA);
3588 assert(!Polyhedron_is_infinite(P, nparam));
3589 assert(P->NbBid == 0);
3590 assert(Polyhedron_has_positive_rays(P, nparam));
3591 assert(P->NbEq == 0);
3593 dim = P->Dimension;
3594 nvar = dim - nparam;
3595 vcone = new Polyhedron_p[P->NbRays];
3597 for (int j = 0; j < P->NbRays; ++j) {
3598 if (!value_pos_p(P->Ray[j][dim+1]))
3599 continue;
3601 int npos, nneg;
3602 Polyhedron *C = supporting_cone(P, j);
3603 decompose(C, &vcone[j], &npos, &nneg, MaxRays);
3604 ncone += npos + nneg;
3605 sign.SetLength(ncone);
3606 for (int k = 0; k < npos; ++k)
3607 sign[ncone-nneg-k-1] = 1;
3608 for (int k = 0; k < nneg; ++k)
3609 sign[ncone-k-1] = -1;
3612 mat_ZZ rays;
3613 rays.SetDims(ncone * dim, nvar);
3614 int r = 0;
3615 for (int j = 0; j < P->NbRays; ++j) {
3616 if (!value_pos_p(P->Ray[j][dim+1]))
3617 continue;
3619 for (Polyhedron *i = vcone[j]; i; i = i->next) {
3620 add_rays(rays, i, &r, nvar);
3623 rays.SetDims(r, nvar);
3624 vec_ZZ lambda;
3625 nonorthog(rays, lambda);
3626 //randomvector(P, lambda, nvar);
3629 cout << "rays: " << rays;
3630 cout << "lambda: " << lambda;
3633 int f = 0;
3634 ZZ num_s;
3635 vec_ZZ num_p;
3636 num_p.SetLength(nparam);
3637 vec_ZZ vertex;
3638 vec_ZZ den_s;
3639 den_s.SetLength(dim);
3640 vec_ZZ den_p;
3641 den_p.SetLength(dim);
3642 mat_ZZ den;
3643 den.SetDims(dim, nparam);
3644 ZZ one;
3645 one = 1;
3646 mpq_t count;
3647 mpq_init(count);
3649 gen_fun * gf = new gen_fun;
3651 rays.SetDims(dim, nvar);
3653 for (int j = 0; j < P->NbRays; ++j) {
3654 if (!value_pos_p(P->Ray[j][dim+1]))
3655 continue;
3657 for (Polyhedron *i = vcone[j]; i; i = i->next, ++f) {
3658 lattice_point(P->Ray[j]+1, i, vertex);
3659 int k = 0;
3660 num_s = 0;
3661 for ( ; k < nvar; ++k)
3662 num_s += vertex[k] * lambda[k];
3663 for ( ; k < dim; ++k)
3664 num_p[k-nvar] = vertex[k];
3666 int r = 0;
3667 add_rays(rays, i, &r, nvar, true);
3668 for (r = 0; r < dim; ++r)
3669 values2zz(i->Ray[r]+1+nvar, den[r], nparam);
3670 den_s = rays * lambda;
3672 normalize(sign[f], num_s, num_p, den_s, den_p, den);
3674 int only_param = 0;
3675 int no_param = 0;
3676 for (int k = 0; k < dim; ++k) {
3677 if (den_p[k] == 0)
3678 ++no_param;
3679 else if (den_s[k] == 0)
3680 ++only_param;
3682 if (no_param == 0) {
3683 for (int k = 0; k < dim; ++k)
3684 if (den_p[k] == -1)
3685 den[k] = -den[k];
3686 gf->add(sign[f], one, num_p, den);
3687 } else if (no_param + only_param == dim) {
3688 int k, l;
3689 mat_ZZ pden;
3690 pden.SetDims(only_param, nparam);
3692 for (k = 0, l = 0; k < dim; ++k)
3693 if (den_p[k] != 0)
3694 pden[l++] = den[k];
3696 for (k = 0; k < dim; ++k)
3697 if (den_s[k] != 0)
3698 break;
3700 dpoly n(no_param, num_s);
3701 dpoly d(no_param, den_s[k], 1);
3702 for ( ; k < dim; ++k)
3703 if (den_s[k] != 0) {
3704 dpoly fact(no_param, den_s[k], 1);
3705 d *= fact;
3708 mpq_set_si(count, 0, 1);
3709 n.div(d, count, sign[f]);
3711 ZZ qn, qd;
3712 value2zz(mpq_numref(count), qn);
3713 value2zz(mpq_denref(count), qd);
3715 gf->add(qn, qd, num_p, pden);
3716 } else {
3717 int k, l;
3718 dpoly_r * r = 0;
3719 mat_ZZ pden;
3720 pden.SetDims(only_param, nparam);
3722 for (k = 0, l = 0; k < dim; ++k)
3723 if (den_s[k] == 0)
3724 pden[l++] = den[k];
3726 for (k = 0; k < dim; ++k)
3727 if (den_p[k] == 0)
3728 break;
3730 dpoly n(no_param, num_s);
3731 dpoly d(no_param, den_s[k], 1);
3732 for ( ; k < dim; ++k)
3733 if (den_p[k] == 0) {
3734 dpoly fact(no_param, den_s[k], 1);
3735 d *= fact;
3738 for (k = 0; k < dim; ++k) {
3739 if (den_s[k] == 0 || den_p[k] == 0)
3740 continue;
3742 dpoly pd(no_param-1, den_s[k], 1);
3743 int s = den_p[k] < 0 ? -1 : 1;
3745 if (r == 0)
3746 r = new dpoly_r(n, pd, k, s, dim);
3747 else
3748 assert(0); // for now
3751 r->div(d, sign[f], gf, pden, den, num_p);
3755 cout << "sign: " << sign[f];
3756 cout << "num_s: " << num_s;
3757 cout << "num_p: " << num_p;
3758 cout << "den_s: " << den_s;
3759 cout << "den_p: " << den_p;
3760 cout << "den: " << den;
3761 cout << "only_param: " << only_param;
3762 cout << "no_param: " << no_param;
3763 cout << endl;
3769 mpq_clear(count);
3771 return gf;