also dump recently added denominator
[barvinok.git] / barvinok.cc
blobf2e0baefc5965d378e151498c926d8322d5c6fd0
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);
442 ZZ inv_d = rc->denom / d.coeff[0];
443 ZZ coeff;
445 for (int i = 0; i < len; ++i) {
446 for (int k = 0; k < c[i].size(); ++k) {
447 coeff = c[i][k]->coeff * inv_d;
448 rc->add_term(i, c[i][k]->powers, coeff);
451 for (int j = 1; j <= i; ++j) {
452 for (int k = 0; k < rc->c[i-j].size(); ++k) {
453 coeff = - d.coeff[j] * rc->c[i-j][k]->coeff / d.coeff[0];
454 rc->add_term(i, rc->c[i-j][k]->powers, coeff);
458 return rc;
460 void div(dpoly& d, ZZ& sign, gen_fun *gf, mat_ZZ& pden, mat_ZZ& den,
461 vec_ZZ& num_p) {
462 dpoly_r * rc = div(d);
463 //rc.dump();
464 int common = pden.NumRows();
466 vector< dpoly_r_term * >& final = rc->c[len-1];
467 int rows;
468 for (int j = 0; j < final.size(); ++j) {
469 rows = common;
470 pden.SetDims(rows, pden.NumCols());
471 for (int k = 0; k < dim; ++k) {
472 int n = final[j]->powers[k];
473 if (n == 0)
474 continue;
475 int abs_n = n < 0 ? -n : n;
476 pden.SetDims(rows+abs_n, pden.NumCols());
477 for (int l = 0; l < abs_n; ++l) {
478 if (n > 0)
479 pden[rows+l] = den[k];
480 else
481 pden[rows+l] = -den[k];
483 rows += abs_n;
485 final[j]->coeff *= sign;
486 gf->add(final[j]->coeff, rc->denom, num_p, pden);
488 delete rc;
490 void dump(void) {
491 for (int i = 0; i < len; ++i) {
492 cout << endl;
493 cout << i << endl;
494 cout << c[i].size() << endl;
495 for (int j = 0; j < c[i].size(); ++j) {
496 for (int k = 0; k < dim; ++k) {
497 cout << c[i][j]->powers[k] << " ";
499 cout << ": " << c[i][j]->coeff << "/" << denom << endl;
501 cout << endl;
506 struct decomposer {
507 void decompose(Polyhedron *C);
508 virtual void handle(Polyhedron *P, int sign) = 0;
511 struct polar_decomposer : public decomposer {
512 void decompose(Polyhedron *C, unsigned MaxRays);
513 virtual void handle(Polyhedron *P, int sign);
514 virtual void handle_polar(Polyhedron *P, int sign) = 0;
517 void decomposer::decompose(Polyhedron *C)
519 vector<cone *> nonuni;
520 cone * c = new cone(C);
521 ZZ det = c->det;
522 int s = sign(det);
523 assert(det != 0);
524 if (abs(det) > 1) {
525 nonuni.push_back(c);
526 } else {
527 handle(C, 1);
528 delete c;
530 vec_ZZ lambda;
531 while (!nonuni.empty()) {
532 c = nonuni.back();
533 nonuni.pop_back();
534 Vector* v = c->short_vector(lambda);
535 for (int i = 0; i < c->Rays->NbRows - 1; ++i) {
536 if (lambda[i] == 0)
537 continue;
538 Matrix* M = Matrix_Copy(c->Rays);
539 Vector_Copy(v->p, M->p[i], v->Size);
540 cone * pc = new cone(M);
541 assert (pc->det != 0);
542 if (abs(pc->det) > 1) {
543 assert(abs(pc->det) < abs(c->det));
544 nonuni.push_back(pc);
545 } else {
546 handle(pc->poly(), sign(pc->det) * s);
547 delete pc;
549 Matrix_Free(M);
551 Vector_Free(v);
552 delete c;
556 void polar_decomposer::decompose(Polyhedron *cone, unsigned MaxRays)
558 Polyhedron_Polarize(cone);
559 if (cone->NbRays - 1 != cone->Dimension) {
560 Polyhedron *tmp = cone;
561 cone = triangularize_cone(cone, MaxRays);
562 Polyhedron_Free(tmp);
564 for (Polyhedron *Polar = cone; Polar; Polar = Polar->next)
565 decomposer::decompose(Polar);
566 Domain_Free(cone);
569 void polar_decomposer::handle(Polyhedron *P, int sign)
571 Polyhedron_Polarize(P);
572 handle_polar(P, sign);
576 * Barvinok's Decomposition of a simplicial cone
578 * Returns two lists of polyhedra
580 void barvinok_decompose(Polyhedron *C, Polyhedron **ppos, Polyhedron **pneg)
582 Polyhedron *pos = *ppos, *neg = *pneg;
583 vector<cone *> nonuni;
584 cone * c = new cone(C);
585 ZZ det = c->det;
586 int s = sign(det);
587 assert(det != 0);
588 if (abs(det) > 1) {
589 nonuni.push_back(c);
590 } else {
591 Polyhedron *p = Polyhedron_Copy(c->Cone);
592 p->next = pos;
593 pos = p;
594 delete c;
596 vec_ZZ lambda;
597 while (!nonuni.empty()) {
598 c = nonuni.back();
599 nonuni.pop_back();
600 Vector* v = c->short_vector(lambda);
601 for (int i = 0; i < c->Rays->NbRows - 1; ++i) {
602 if (lambda[i] == 0)
603 continue;
604 Matrix* M = Matrix_Copy(c->Rays);
605 Vector_Copy(v->p, M->p[i], v->Size);
606 cone * pc = new cone(M);
607 assert (pc->det != 0);
608 if (abs(pc->det) > 1) {
609 assert(abs(pc->det) < abs(c->det));
610 nonuni.push_back(pc);
611 } else {
612 Polyhedron *p = pc->poly();
613 pc->Cone = 0;
614 if (sign(pc->det) == s) {
615 p->next = pos;
616 pos = p;
617 } else {
618 p->next = neg;
619 neg = p;
621 delete pc;
623 Matrix_Free(M);
625 Vector_Free(v);
626 delete c;
628 *ppos = pos;
629 *pneg = neg;
633 * Returns a single list of npos "positive" cones followed by nneg
634 * "negative" cones.
635 * The input cone is freed
637 void decompose(Polyhedron *cone, Polyhedron **parts, int *npos, int *nneg, unsigned MaxRays)
639 Polyhedron_Polarize(cone);
640 if (cone->NbRays - 1 != cone->Dimension) {
641 Polyhedron *tmp = cone;
642 cone = triangularize_cone(cone, MaxRays);
643 Polyhedron_Free(tmp);
645 Polyhedron *polpos = NULL, *polneg = NULL;
646 *npos = 0; *nneg = 0;
647 for (Polyhedron *Polar = cone; Polar; Polar = Polar->next)
648 barvinok_decompose(Polar, &polpos, &polneg);
650 Polyhedron *last;
651 for (Polyhedron *i = polpos; i; i = i->next) {
652 Polyhedron_Polarize(i);
653 ++*npos;
654 last = i;
656 for (Polyhedron *i = polneg; i; i = i->next) {
657 Polyhedron_Polarize(i);
658 ++*nneg;
660 if (last) {
661 last->next = polneg;
662 *parts = polpos;
663 } else
664 *parts = polneg;
665 Domain_Free(cone);
668 const int MAX_TRY=10;
670 * Searches for a vector that is not orthogonal to any
671 * of the rays in rays.
673 static void nonorthog(mat_ZZ& rays, vec_ZZ& lambda)
675 int dim = rays.NumCols();
676 bool found = false;
677 lambda.SetLength(dim);
678 if (dim == 0)
679 return;
681 for (int i = 2; !found && i <= 50*dim; i+=4) {
682 for (int j = 0; j < MAX_TRY; ++j) {
683 for (int k = 0; k < dim; ++k) {
684 int r = random_int(i)+2;
685 int v = (2*(r%2)-1) * (r >> 1);
686 lambda[k] = v;
688 int k = 0;
689 for (; k < rays.NumRows(); ++k)
690 if (lambda * rays[k] == 0)
691 break;
692 if (k == rays.NumRows()) {
693 found = true;
694 break;
698 assert(found);
701 static void randomvector(Polyhedron *P, vec_ZZ& lambda, int nvar)
703 Value tmp;
704 int max = 10 * 16;
705 unsigned int dim = P->Dimension;
706 value_init(tmp);
708 for (int i = 0; i < P->NbRays; ++i) {
709 for (int j = 1; j <= dim; ++j) {
710 value_absolute(tmp, P->Ray[i][j]);
711 int t = VALUE_TO_LONG(tmp) * 16;
712 if (t > max)
713 max = t;
716 for (int i = 0; i < P->NbConstraints; ++i) {
717 for (int j = 1; j <= dim; ++j) {
718 value_absolute(tmp, P->Constraint[i][j]);
719 int t = VALUE_TO_LONG(tmp) * 16;
720 if (t > max)
721 max = t;
724 value_clear(tmp);
726 lambda.SetLength(nvar);
727 for (int k = 0; k < nvar; ++k) {
728 int r = random_int(max*dim)+2;
729 int v = (2*(r%2)-1) * (max/2*dim + (r >> 1));
730 lambda[k] = v;
734 static void add_rays(mat_ZZ& rays, Polyhedron *i, int *r, int nvar = -1,
735 bool all = false)
737 unsigned dim = i->Dimension;
738 if (nvar == -1)
739 nvar = dim;
740 for (int k = 0; k < i->NbRays; ++k) {
741 if (!value_zero_p(i->Ray[k][dim+1]))
742 continue;
743 if (!all && nvar != dim && First_Non_Zero(i->Ray[k]+1, nvar) == -1)
744 continue;
745 values2zz(i->Ray[k]+1, rays[(*r)++], nvar);
749 void lattice_point(Value* values, Polyhedron *i, vec_ZZ& vertex)
751 unsigned dim = i->Dimension;
752 if(!value_one_p(values[dim])) {
753 Matrix* Rays = rays(i);
754 Matrix *inv = Matrix_Alloc(Rays->NbRows, Rays->NbColumns);
755 int ok = Matrix_Inverse(Rays, inv);
756 assert(ok);
757 Matrix_Free(Rays);
758 Rays = rays(i);
759 Vector *lambda = Vector_Alloc(dim+1);
760 Vector_Matrix_Product(values, inv, lambda->p);
761 Matrix_Free(inv);
762 for (int j = 0; j < dim; ++j)
763 mpz_cdiv_q(lambda->p[j], lambda->p[j], lambda->p[dim]);
764 value_set_si(lambda->p[dim], 1);
765 Vector *A = Vector_Alloc(dim+1);
766 Vector_Matrix_Product(lambda->p, Rays, A->p);
767 Vector_Free(lambda);
768 Matrix_Free(Rays);
769 values2zz(A->p, vertex, dim);
770 Vector_Free(A);
771 } else
772 values2zz(values, vertex, dim);
775 static evalue *term(int param, ZZ& c, Value *den = NULL)
777 evalue *EP = new evalue();
778 value_init(EP->d);
779 value_set_si(EP->d,0);
780 EP->x.p = new_enode(polynomial, 2, param + 1);
781 evalue_set_si(&EP->x.p->arr[0], 0, 1);
782 value_init(EP->x.p->arr[1].x.n);
783 if (den == NULL)
784 value_set_si(EP->x.p->arr[1].d, 1);
785 else
786 value_assign(EP->x.p->arr[1].d, *den);
787 zz2value(c, EP->x.p->arr[1].x.n);
788 return EP;
791 static void vertex_period(
792 Polyhedron *i, vec_ZZ& lambda, Matrix *T,
793 Value lcm, int p, Vector *val,
794 evalue *E, evalue* ev,
795 ZZ& offset)
797 unsigned nparam = T->NbRows - 1;
798 unsigned dim = i->Dimension;
799 Value tmp;
800 ZZ nump;
802 if (p == nparam) {
803 vec_ZZ vertex;
804 ZZ num, l;
805 Vector * values = Vector_Alloc(dim + 1);
806 Vector_Matrix_Product(val->p, T, values->p);
807 value_assign(values->p[dim], lcm);
808 lattice_point(values->p, i, vertex);
809 num = vertex * lambda;
810 value2zz(lcm, l);
811 num *= l;
812 num += offset;
813 value_init(ev->x.n);
814 zz2value(num, ev->x.n);
815 value_assign(ev->d, lcm);
816 Vector_Free(values);
817 return;
820 value_init(tmp);
821 vec_ZZ vertex;
822 values2zz(T->p[p], vertex, dim);
823 nump = vertex * lambda;
824 if (First_Non_Zero(val->p, p) == -1) {
825 value_assign(tmp, lcm);
826 evalue *ET = term(p, nump, &tmp);
827 eadd(ET, E);
828 free_evalue_refs(ET);
829 delete ET;
832 value_assign(tmp, lcm);
833 if (First_Non_Zero(T->p[p], dim) != -1)
834 Vector_Gcd(T->p[p], dim, &tmp);
835 Gcd(tmp, lcm, &tmp);
836 if (value_lt(tmp, lcm)) {
837 ZZ count;
839 value_division(tmp, lcm, tmp);
840 value_set_si(ev->d, 0);
841 ev->x.p = new_enode(periodic, VALUE_TO_INT(tmp), p+1);
842 value2zz(tmp, count);
843 do {
844 value_decrement(tmp, tmp);
845 --count;
846 ZZ new_offset = offset - count * nump;
847 value_assign(val->p[p], tmp);
848 vertex_period(i, lambda, T, lcm, p+1, val, E,
849 &ev->x.p->arr[VALUE_TO_INT(tmp)], new_offset);
850 } while (value_pos_p(tmp));
851 } else
852 vertex_period(i, lambda, T, lcm, p+1, val, E, ev, offset);
853 value_clear(tmp);
856 static void mask_r(Matrix *f, int nr, Vector *lcm, int p, Vector *val, evalue *ev)
858 unsigned nparam = lcm->Size;
860 if (p == nparam) {
861 Vector * prod = Vector_Alloc(f->NbRows);
862 Matrix_Vector_Product(f, val->p, prod->p);
863 int isint = 1;
864 for (int i = 0; i < nr; ++i) {
865 value_modulus(prod->p[i], prod->p[i], f->p[i][nparam+1]);
866 isint &= value_zero_p(prod->p[i]);
868 value_set_si(ev->d, 1);
869 value_init(ev->x.n);
870 value_set_si(ev->x.n, isint);
871 Vector_Free(prod);
872 return;
875 Value tmp;
876 value_init(tmp);
877 if (value_one_p(lcm->p[p]))
878 mask_r(f, nr, lcm, p+1, val, ev);
879 else {
880 value_assign(tmp, lcm->p[p]);
881 value_set_si(ev->d, 0);
882 ev->x.p = new_enode(periodic, VALUE_TO_INT(tmp), p+1);
883 do {
884 value_decrement(tmp, tmp);
885 value_assign(val->p[p], tmp);
886 mask_r(f, nr, lcm, p+1, val, &ev->x.p->arr[VALUE_TO_INT(tmp)]);
887 } while (value_pos_p(tmp));
889 value_clear(tmp);
892 static evalue *multi_monom(vec_ZZ& p)
894 evalue *X = new evalue();
895 value_init(X->d);
896 value_init(X->x.n);
897 unsigned nparam = p.length()-1;
898 zz2value(p[nparam], X->x.n);
899 value_set_si(X->d, 1);
900 for (int i = 0; i < nparam; ++i) {
901 if (p[i] == 0)
902 continue;
903 evalue *T = term(i, p[i]);
904 eadd(T, X);
905 free_evalue_refs(T);
906 delete T;
908 return X;
912 * Check whether mapping polyhedron P on the affine combination
913 * num yields a range that has a fixed quotient on integer
914 * division by d
915 * If zero is true, then we are only interested in the quotient
916 * for the cases where the remainder is zero.
917 * Returns NULL if false and a newly allocated value if true.
919 static Value *fixed_quotient(Polyhedron *P, vec_ZZ& num, Value d, bool zero)
921 Value* ret = NULL;
922 int len = num.length();
923 Matrix *T = Matrix_Alloc(2, len);
924 zz2values(num, T->p[0]);
925 value_set_si(T->p[1][len-1], 1);
926 Polyhedron *I = Polyhedron_Image(P, T, P->NbConstraints);
927 Matrix_Free(T);
929 int i;
930 for (i = 0; i < I->NbRays; ++i)
931 if (value_zero_p(I->Ray[i][2])) {
932 Polyhedron_Free(I);
933 return NULL;
936 Value min, max;
937 value_init(min);
938 value_init(max);
939 int bounded = line_minmax(I, &min, &max);
940 assert(bounded);
942 if (zero)
943 mpz_cdiv_q(min, min, d);
944 else
945 mpz_fdiv_q(min, min, d);
946 mpz_fdiv_q(max, max, d);
948 if (value_eq(min, max)) {
949 ALLOC(Value, ret);
950 value_init(*ret);
951 value_assign(*ret, min);
953 value_clear(min);
954 value_clear(max);
955 return ret;
959 * Normalize linear expression coef modulo m
960 * Removes common factor and reduces coefficients
961 * Returns index of first non-zero coefficient or len
963 static int normal_mod(Value *coef, int len, Value *m)
965 Value gcd;
966 value_init(gcd);
968 Vector_Gcd(coef, len, &gcd);
969 Gcd(gcd, *m, &gcd);
970 Vector_AntiScale(coef, coef, gcd, len);
972 value_division(*m, *m, gcd);
973 value_clear(gcd);
975 if (value_one_p(*m))
976 return len;
978 int j;
979 for (j = 0; j < len; ++j)
980 mpz_fdiv_r(coef[j], coef[j], *m);
981 for (j = 0; j < len; ++j)
982 if (value_notzero_p(coef[j]))
983 break;
985 return j;
988 #ifdef USE_MODULO
989 static void mask(Matrix *f, evalue *factor)
991 int nr = f->NbRows, nc = f->NbColumns;
992 int n;
993 bool found = false;
994 for (n = 0; n < nr && value_notzero_p(f->p[n][nc-1]); ++n)
995 if (value_notone_p(f->p[n][nc-1]) &&
996 value_notmone_p(f->p[n][nc-1]))
997 found = true;
998 if (!found)
999 return;
1001 evalue EP;
1002 nr = n;
1004 Value m;
1005 value_init(m);
1007 evalue EV;
1008 value_init(EV.d);
1009 value_init(EV.x.n);
1010 value_set_si(EV.x.n, 1);
1012 for (n = 0; n < nr; ++n) {
1013 value_assign(m, f->p[n][nc-1]);
1014 if (value_one_p(m) || value_mone_p(m))
1015 continue;
1017 int j = normal_mod(f->p[n], nc-1, &m);
1018 if (j == nc-1) {
1019 free_evalue_refs(factor);
1020 value_init(factor->d);
1021 evalue_set_si(factor, 0, 1);
1022 break;
1024 vec_ZZ row;
1025 values2zz(f->p[n], row, nc-1);
1026 ZZ g;
1027 value2zz(m, g);
1028 if (j < (nc-1)-1 && row[j] > g/2) {
1029 for (int k = j; k < (nc-1); ++k)
1030 if (row[k] != 0)
1031 row[k] = g - row[k];
1034 value_init(EP.d);
1035 value_set_si(EP.d, 0);
1036 EP.x.p = new_enode(relation, 2, 0);
1037 value_clear(EP.x.p->arr[1].d);
1038 EP.x.p->arr[1] = *factor;
1039 evalue *ev = &EP.x.p->arr[0];
1040 value_set_si(ev->d, 0);
1041 ev->x.p = new_enode(fractional, 3, -1);
1042 evalue_set_si(&ev->x.p->arr[1], 0, 1);
1043 evalue_set_si(&ev->x.p->arr[2], 1, 1);
1044 evalue *E = multi_monom(row);
1045 value_assign(EV.d, m);
1046 emul(&EV, E);
1047 value_clear(ev->x.p->arr[0].d);
1048 ev->x.p->arr[0] = *E;
1049 delete E;
1050 *factor = EP;
1053 value_clear(m);
1054 free_evalue_refs(&EV);
1056 #else
1060 static void mask(Matrix *f, evalue *factor)
1062 int nr = f->NbRows, nc = f->NbColumns;
1063 int n;
1064 bool found = false;
1065 for (n = 0; n < nr && value_notzero_p(f->p[n][nc-1]); ++n)
1066 if (value_notone_p(f->p[n][nc-1]) &&
1067 value_notmone_p(f->p[n][nc-1]))
1068 found = true;
1069 if (!found)
1070 return;
1072 Value tmp;
1073 value_init(tmp);
1074 nr = n;
1075 unsigned np = nc - 2;
1076 Vector *lcm = Vector_Alloc(np);
1077 Vector *val = Vector_Alloc(nc);
1078 Vector_Set(val->p, 0, nc);
1079 value_set_si(val->p[np], 1);
1080 Vector_Set(lcm->p, 1, np);
1081 for (n = 0; n < nr; ++n) {
1082 if (value_one_p(f->p[n][nc-1]) ||
1083 value_mone_p(f->p[n][nc-1]))
1084 continue;
1085 for (int j = 0; j < np; ++j)
1086 if (value_notzero_p(f->p[n][j])) {
1087 Gcd(f->p[n][j], f->p[n][nc-1], &tmp);
1088 value_division(tmp, f->p[n][nc-1], tmp);
1089 value_lcm(tmp, lcm->p[j], &lcm->p[j]);
1092 evalue EP;
1093 value_init(EP.d);
1094 mask_r(f, nr, lcm, 0, val, &EP);
1095 value_clear(tmp);
1096 Vector_Free(val);
1097 Vector_Free(lcm);
1098 emul(&EP,factor);
1099 free_evalue_refs(&EP);
1101 #endif
1103 struct term_info {
1104 evalue *E;
1105 ZZ constant;
1106 ZZ coeff;
1107 int pos;
1110 static bool mod_needed(Polyhedron *PD, vec_ZZ& num, Value d, evalue *E)
1112 Value *q = fixed_quotient(PD, num, d, false);
1114 if (!q)
1115 return true;
1117 value_oppose(*q, *q);
1118 evalue EV;
1119 value_init(EV.d);
1120 value_set_si(EV.d, 1);
1121 value_init(EV.x.n);
1122 value_multiply(EV.x.n, *q, d);
1123 eadd(&EV, E);
1124 free_evalue_refs(&EV);
1125 value_clear(*q);
1126 free(q);
1127 return false;
1130 static void ceil_mod(Value *coef, int len, Value d, ZZ& f, evalue *EP, Polyhedron *PD)
1132 Value m;
1133 value_init(m);
1134 value_set_si(m, -1);
1136 Vector_Scale(coef, coef, m, len);
1138 value_assign(m, d);
1139 int j = normal_mod(coef, len, &m);
1141 if (j == len) {
1142 value_clear(m);
1143 return;
1146 vec_ZZ num;
1147 values2zz(coef, num, len);
1149 ZZ g;
1150 value2zz(m, g);
1152 evalue tmp;
1153 value_init(tmp.d);
1154 evalue_set_si(&tmp, 0, 1);
1156 int p = j;
1157 if (g % 2 == 0)
1158 while (j < len-1 && (num[j] == g/2 || num[j] == 0))
1159 ++j;
1160 if ((j < len-1 && num[j] > g/2) || (j == len-1 && num[j] >= (g+1)/2)) {
1161 for (int k = j; k < len-1; ++k)
1162 if (num[k] != 0)
1163 num[k] = g - num[k];
1164 num[len-1] = g - 1 - num[len-1];
1165 value_assign(tmp.d, m);
1166 ZZ t = f*(g-1);
1167 zz2value(t, tmp.x.n);
1168 eadd(&tmp, EP);
1169 f = -f;
1172 if (p >= len-1) {
1173 ZZ t = num[len-1] * f;
1174 zz2value(t, tmp.x.n);
1175 value_assign(tmp.d, m);
1176 eadd(&tmp, EP);
1177 } else {
1178 evalue *E = multi_monom(num);
1179 evalue EV;
1180 value_init(EV.d);
1182 if (PD && !mod_needed(PD, num, m, E)) {
1183 value_init(EV.x.n);
1184 zz2value(f, EV.x.n);
1185 value_assign(EV.d, m);
1186 emul(&EV, E);
1187 eadd(E, EP);
1188 } else {
1189 value_init(EV.x.n);
1190 value_set_si(EV.x.n, 1);
1191 value_assign(EV.d, m);
1192 emul(&EV, E);
1193 value_clear(EV.x.n);
1194 value_set_si(EV.d, 0);
1195 EV.x.p = new_enode(fractional, 3, -1);
1196 evalue_copy(&EV.x.p->arr[0], E);
1197 evalue_set_si(&EV.x.p->arr[1], 0, 1);
1198 value_init(EV.x.p->arr[2].x.n);
1199 zz2value(f, EV.x.p->arr[2].x.n);
1200 value_set_si(EV.x.p->arr[2].d, 1);
1202 eadd(&EV, EP);
1205 free_evalue_refs(&EV);
1206 free_evalue_refs(E);
1207 delete E;
1210 free_evalue_refs(&tmp);
1212 out:
1213 value_clear(m);
1216 evalue* bv_ceil3(Value *coef, int len, Value d, Polyhedron *P)
1218 Vector *val = Vector_Alloc(len);
1220 Value t;
1221 value_init(t);
1222 value_set_si(t, -1);
1223 Vector_Scale(coef, val->p, t, len);
1224 value_absolute(t, d);
1226 vec_ZZ num;
1227 values2zz(val->p, num, len);
1228 evalue *EP = multi_monom(num);
1230 evalue tmp;
1231 value_init(tmp.d);
1232 value_init(tmp.x.n);
1233 value_set_si(tmp.x.n, 1);
1234 value_assign(tmp.d, t);
1236 emul(&tmp, EP);
1238 ZZ one;
1239 one = 1;
1240 ceil_mod(val->p, len, t, one, EP, P);
1241 value_clear(t);
1243 /* copy EP to malloc'ed evalue */
1244 evalue *E;
1245 ALLOC(evalue, E);
1246 *E = *EP;
1247 delete EP;
1249 free_evalue_refs(&tmp);
1250 Vector_Free(val);
1252 return E;
1255 #ifdef USE_MODULO
1256 evalue* lattice_point(
1257 Polyhedron *i, vec_ZZ& lambda, Matrix *W, Value lcm, Polyhedron *PD)
1259 unsigned nparam = W->NbColumns - 1;
1261 Matrix* Rays = rays2(i);
1262 Matrix *T = Transpose(Rays);
1263 Matrix *T2 = Matrix_Copy(T);
1264 Matrix *inv = Matrix_Alloc(T2->NbRows, T2->NbColumns);
1265 int ok = Matrix_Inverse(T2, inv);
1266 assert(ok);
1267 Matrix_Free(Rays);
1268 Matrix_Free(T2);
1269 mat_ZZ vertex;
1270 matrix2zz(W, vertex, W->NbRows, W->NbColumns);
1272 vec_ZZ num;
1273 num = lambda * vertex;
1275 evalue *EP = multi_monom(num);
1277 evalue tmp;
1278 value_init(tmp.d);
1279 value_init(tmp.x.n);
1280 value_set_si(tmp.x.n, 1);
1281 value_assign(tmp.d, lcm);
1283 emul(&tmp, EP);
1285 Matrix *L = Matrix_Alloc(inv->NbRows, W->NbColumns);
1286 Matrix_Product(inv, W, L);
1288 mat_ZZ RT;
1289 matrix2zz(T, RT, T->NbRows, T->NbColumns);
1290 Matrix_Free(T);
1292 vec_ZZ p = lambda * RT;
1294 for (int i = 0; i < L->NbRows; ++i) {
1295 ceil_mod(L->p[i], nparam+1, lcm, p[i], EP, PD);
1298 Matrix_Free(L);
1300 Matrix_Free(inv);
1301 free_evalue_refs(&tmp);
1302 return EP;
1304 #else
1305 evalue* lattice_point(
1306 Polyhedron *i, vec_ZZ& lambda, Matrix *W, Value lcm, Polyhedron *PD)
1308 Matrix *T = Transpose(W);
1309 unsigned nparam = T->NbRows - 1;
1311 evalue *EP = new evalue();
1312 value_init(EP->d);
1313 evalue_set_si(EP, 0, 1);
1315 evalue ev;
1316 Vector *val = Vector_Alloc(nparam+1);
1317 value_set_si(val->p[nparam], 1);
1318 ZZ offset(INIT_VAL, 0);
1319 value_init(ev.d);
1320 vertex_period(i, lambda, T, lcm, 0, val, EP, &ev, offset);
1321 Vector_Free(val);
1322 eadd(&ev, EP);
1323 free_evalue_refs(&ev);
1325 Matrix_Free(T);
1327 reduce_evalue(EP);
1329 return EP;
1331 #endif
1333 void lattice_point(
1334 Param_Vertices* V, Polyhedron *i, vec_ZZ& lambda, term_info* term,
1335 Polyhedron *PD)
1337 unsigned nparam = V->Vertex->NbColumns - 2;
1338 unsigned dim = i->Dimension;
1339 mat_ZZ vertex;
1340 vertex.SetDims(V->Vertex->NbRows, nparam+1);
1341 Value lcm, tmp;
1342 value_init(lcm);
1343 value_init(tmp);
1344 value_set_si(lcm, 1);
1345 for (int j = 0; j < V->Vertex->NbRows; ++j) {
1346 value_lcm(lcm, V->Vertex->p[j][nparam+1], &lcm);
1348 if (value_notone_p(lcm)) {
1349 Matrix * mv = Matrix_Alloc(dim, nparam+1);
1350 for (int j = 0 ; j < dim; ++j) {
1351 value_division(tmp, lcm, V->Vertex->p[j][nparam+1]);
1352 Vector_Scale(V->Vertex->p[j], mv->p[j], tmp, nparam+1);
1355 term->E = lattice_point(i, lambda, mv, lcm, PD);
1356 term->constant = 0;
1358 Matrix_Free(mv);
1359 value_clear(lcm);
1360 value_clear(tmp);
1361 return;
1363 for (int i = 0; i < V->Vertex->NbRows; ++i) {
1364 assert(value_one_p(V->Vertex->p[i][nparam+1])); // for now
1365 values2zz(V->Vertex->p[i], vertex[i], nparam+1);
1368 vec_ZZ num;
1369 num = lambda * vertex;
1371 int p = -1;
1372 int nn = 0;
1373 for (int j = 0; j < nparam; ++j)
1374 if (num[j] != 0) {
1375 ++nn;
1376 p = j;
1378 if (nn >= 2) {
1379 term->E = multi_monom(num);
1380 term->constant = 0;
1381 } else {
1382 term->E = NULL;
1383 term->constant = num[nparam];
1384 term->pos = p;
1385 if (p != -1)
1386 term->coeff = num[p];
1389 value_clear(lcm);
1390 value_clear(tmp);
1393 static void normalize(ZZ& sign, ZZ& num, vec_ZZ& den)
1395 unsigned dim = den.length();
1397 int change = 0;
1399 for (int j = 0; j < den.length(); ++j) {
1400 if (den[j] > 0)
1401 change ^= 1;
1402 else {
1403 den[j] = abs(den[j]);
1404 num += den[j];
1407 if (change)
1408 sign = -sign;
1411 /* input:
1412 * f: the powers in the denominator for the remaining vars
1413 * each row refers to a factor
1414 * den_s: for each factor, the power of (s+1)
1415 * sign
1416 * num_s: powers in the numerator corresponding to the summed vars
1417 * num_p: powers in the numerator corresponidng to the remaining vars
1418 * number of rays in cone: "dim" = "k"
1419 * length of each ray: "dim" = "d"
1420 * for now, it is assume: k == d
1421 * output:
1422 * den_p: for each factor
1423 * 0: independent of remaining vars
1424 * 1: power corresponds to corresponding row in f
1425 * -1: power is inverse of corresponding row in f
1427 static void normalize(ZZ& sign,
1428 ZZ& num_s, vec_ZZ& num_p, vec_ZZ& den_s, vec_ZZ& den_p,
1429 mat_ZZ& f)
1431 unsigned dim = f.NumRows();
1432 unsigned nparam = num_p.length();
1433 unsigned nvar = dim - nparam;
1435 int change = 0;
1437 for (int j = 0; j < den_s.length(); ++j) {
1438 if (den_s[j] == 0) {
1439 den_p[j] = 1;
1440 continue;
1442 int k;
1443 for (k = 0; k < nparam; ++k)
1444 if (f[j][k] != 0)
1445 break;
1446 if (k < nparam) {
1447 if (den_s[j] > 0) {
1448 den_p[j] = -1;
1449 num_p -= f[j];
1450 } else
1451 den_p[j] = 1;
1452 } else
1453 den_p[j] = 0;
1454 if (den_s[j] > 0)
1455 change ^= 1;
1456 else {
1457 den_s[j] = abs(den_s[j]);
1458 num_s += den_s[j];
1462 if (change)
1463 sign = -sign;
1466 struct counter : public polar_decomposer {
1467 vec_ZZ lambda;
1468 mat_ZZ rays;
1469 vec_ZZ vertex;
1470 vec_ZZ den;
1471 ZZ sign;
1472 ZZ num;
1473 int j;
1474 Polyhedron *P;
1475 unsigned dim;
1476 mpq_t count;
1478 counter(Polyhedron *P) {
1479 this->P = P;
1480 dim = P->Dimension;
1481 randomvector(P, lambda, dim);
1482 rays.SetDims(dim, dim);
1483 den.SetLength(dim);
1484 mpq_init(count);
1487 void start(unsigned MaxRays);
1489 ~counter() {
1490 mpq_clear(count);
1493 virtual void handle_polar(Polyhedron *P, int sign);
1496 void counter::handle_polar(Polyhedron *C, int s)
1498 int r = 0;
1499 assert(C->NbRays-1 == dim);
1500 add_rays(rays, C, &r);
1501 for (int k = 0; k < dim; ++k) {
1502 assert(lambda * rays[k] != 0);
1505 sign = s;
1507 lattice_point(P->Ray[j]+1, C, vertex);
1508 num = vertex * lambda;
1509 den = rays * lambda;
1510 normalize(sign, num, den);
1512 dpoly d(dim, num);
1513 dpoly n(dim, den[0], 1);
1514 for (int k = 1; k < dim; ++k) {
1515 dpoly fact(dim, den[k], 1);
1516 n *= fact;
1518 d.div(n, count, sign);
1521 void counter::start(unsigned MaxRays)
1523 for (j = 0; j < P->NbRays; ++j) {
1524 Polyhedron *C = supporting_cone(P, j);
1525 decompose(C, MaxRays);
1529 typedef Polyhedron * Polyhedron_p;
1531 void barvinok_count(Polyhedron *P, Value* result, unsigned NbMaxCons)
1533 Polyhedron ** vcone;
1534 ZZ sign;
1535 unsigned dim;
1536 int allocated = 0;
1537 Value factor;
1538 Polyhedron *Q;
1539 int r = 0;
1541 if (emptyQ(P)) {
1542 value_set_si(*result, 0);
1543 return;
1545 if (P->NbBid == 0)
1546 for (; r < P->NbRays; ++r)
1547 if (value_zero_p(P->Ray[r][P->Dimension+1]))
1548 break;
1549 if (P->NbBid !=0 || r < P->NbRays) {
1550 value_set_si(*result, -1);
1551 return;
1553 if (P->NbEq != 0) {
1554 P = remove_equalities(P);
1555 if (emptyQ(P)) {
1556 Polyhedron_Free(P);
1557 value_set_si(*result, 0);
1558 return;
1560 allocated = 1;
1562 value_init(factor);
1563 value_set_si(factor, 1);
1564 Q = Polyhedron_Reduce(P, &factor);
1565 if (Q) {
1566 if (allocated)
1567 Polyhedron_Free(P);
1568 P = Q;
1569 allocated = 1;
1571 if (P->Dimension == 0) {
1572 value_assign(*result, factor);
1573 if (allocated)
1574 Polyhedron_Free(P);
1575 value_clear(factor);
1576 return;
1579 counter cnt(P);
1580 cnt.start(NbMaxCons);
1582 assert(value_one_p(&cnt.count[0]._mp_den));
1583 value_multiply(*result, &cnt.count[0]._mp_num, factor);
1585 if (allocated)
1586 Polyhedron_Free(P);
1587 value_clear(factor);
1590 static void uni_polynom(int param, Vector *c, evalue *EP)
1592 unsigned dim = c->Size-2;
1593 value_init(EP->d);
1594 value_set_si(EP->d,0);
1595 EP->x.p = new_enode(polynomial, dim+1, param+1);
1596 for (int j = 0; j <= dim; ++j)
1597 evalue_set(&EP->x.p->arr[j], c->p[j], c->p[dim+1]);
1600 static void multi_polynom(Vector *c, evalue* X, evalue *EP)
1602 unsigned dim = c->Size-2;
1603 evalue EC;
1605 value_init(EC.d);
1606 evalue_set(&EC, c->p[dim], c->p[dim+1]);
1608 value_init(EP->d);
1609 evalue_set(EP, c->p[dim], c->p[dim+1]);
1611 for (int i = dim-1; i >= 0; --i) {
1612 emul(X, EP);
1613 value_assign(EC.x.n, c->p[i]);
1614 eadd(&EC, EP);
1616 free_evalue_refs(&EC);
1619 Polyhedron *unfringe (Polyhedron *P, unsigned MaxRays)
1621 int len = P->Dimension+2;
1622 Polyhedron *T, *R = P;
1623 Value g;
1624 value_init(g);
1625 Vector *row = Vector_Alloc(len);
1626 value_set_si(row->p[0], 1);
1628 R = DomainConstraintSimplify(Polyhedron_Copy(P), MaxRays);
1630 Matrix *M = Matrix_Alloc(2, len-1);
1631 value_set_si(M->p[1][len-2], 1);
1632 for (int v = 0; v < P->Dimension; ++v) {
1633 value_set_si(M->p[0][v], 1);
1634 Polyhedron *I = Polyhedron_Image(P, M, 2+1);
1635 value_set_si(M->p[0][v], 0);
1636 for (int r = 0; r < I->NbConstraints; ++r) {
1637 if (value_zero_p(I->Constraint[r][0]))
1638 continue;
1639 if (value_zero_p(I->Constraint[r][1]))
1640 continue;
1641 if (value_one_p(I->Constraint[r][1]))
1642 continue;
1643 if (value_mone_p(I->Constraint[r][1]))
1644 continue;
1645 value_absolute(g, I->Constraint[r][1]);
1646 Vector_Set(row->p+1, 0, len-2);
1647 value_division(row->p[1+v], I->Constraint[r][1], g);
1648 mpz_fdiv_q(row->p[len-1], I->Constraint[r][2], g);
1649 T = R;
1650 R = AddConstraints(row->p, 1, R, MaxRays);
1651 if (T != P)
1652 Polyhedron_Free(T);
1654 Polyhedron_Free(I);
1656 Matrix_Free(M);
1657 Vector_Free(row);
1658 value_clear(g);
1659 return R;
1662 static Polyhedron *reduce_domain(Polyhedron *D, Matrix *CT, Polyhedron *CEq,
1663 Polyhedron **fVD, int nd, unsigned MaxRays)
1665 assert(CEq);
1667 Polyhedron *Dt;
1668 Dt = CT ? DomainPreimage(D, CT, MaxRays) : D;
1669 Polyhedron *rVD = DomainIntersection(Dt, CEq, MaxRays);
1671 /* if rVD is empty or too small in geometric dimension */
1672 if(!rVD || emptyQ(rVD) ||
1673 (rVD->Dimension-rVD->NbEq < Dt->Dimension-Dt->NbEq-CEq->NbEq)) {
1674 if(rVD)
1675 Domain_Free(rVD);
1676 if (CT)
1677 Domain_Free(Dt);
1678 return 0; /* empty validity domain */
1681 if (CT)
1682 Domain_Free(Dt);
1684 fVD[nd] = Domain_Copy(rVD);
1685 for (int i = 0 ; i < nd; ++i) {
1686 Polyhedron *I = DomainIntersection(fVD[nd], fVD[i], MaxRays);
1687 if (emptyQ(I)) {
1688 Domain_Free(I);
1689 continue;
1691 Polyhedron *F = DomainSimplify(I, fVD[nd], MaxRays);
1692 if (F->NbEq == 1) {
1693 Polyhedron *T = rVD;
1694 rVD = DomainDifference(rVD, F, MaxRays);
1695 Domain_Free(T);
1697 Domain_Free(F);
1698 Domain_Free(I);
1701 rVD = DomainConstraintSimplify(rVD, MaxRays);
1702 if (emptyQ(rVD)) {
1703 Domain_Free(fVD[nd]);
1704 Domain_Free(rVD);
1705 return 0;
1708 Value c;
1709 value_init(c);
1710 barvinok_count(rVD, &c, MaxRays);
1711 if (value_zero_p(c)) {
1712 Domain_Free(rVD);
1713 rVD = 0;
1715 value_clear(c);
1717 return rVD;
1720 static bool Polyhedron_is_infinite(Polyhedron *P, unsigned nparam)
1722 int r;
1723 for (r = 0; r < P->NbRays; ++r)
1724 if (value_zero_p(P->Ray[r][0]) ||
1725 value_zero_p(P->Ray[r][P->Dimension+1])) {
1726 int i;
1727 for (i = P->Dimension - nparam; i < P->Dimension; ++i)
1728 if (value_notzero_p(P->Ray[r][i+1]))
1729 break;
1730 if (i >= P->Dimension)
1731 break;
1733 return r < P->NbRays;
1736 /* Check whether all rays point in the positive directions
1737 * for the parameters
1739 static bool Polyhedron_has_positive_rays(Polyhedron *P, unsigned nparam)
1741 int r;
1742 for (r = 0; r < P->NbRays; ++r)
1743 if (value_zero_p(P->Ray[r][P->Dimension+1])) {
1744 int i;
1745 for (i = P->Dimension - nparam; i < P->Dimension; ++i)
1746 if (value_neg_p(P->Ray[r][i+1]))
1747 return false;
1749 return true;
1752 typedef evalue * evalue_p;
1754 struct enumerator : public polar_decomposer {
1755 vec_ZZ lambda;
1756 unsigned dim, nbV;
1757 evalue ** vE;
1758 int _i;
1759 mat_ZZ rays;
1760 vec_ZZ den;
1761 ZZ sign;
1762 Polyhedron *P;
1763 Param_Vertices *V;
1764 term_info num;
1765 Vector *c;
1766 mpq_t count;
1768 enumerator(Polyhedron *P, unsigned dim, unsigned nbV) {
1769 this->P = P;
1770 this->dim = dim;
1771 this->nbV = nbV;
1772 randomvector(P, lambda, dim);
1773 rays.SetDims(dim, dim);
1774 den.SetLength(dim);
1775 c = Vector_Alloc(dim+2);
1777 vE = new evalue_p[nbV];
1778 for (int j = 0; j < nbV; ++j)
1779 vE[j] = 0;
1781 mpq_init(count);
1784 void decompose_at(Param_Vertices *V, int _i, unsigned MaxRays) {
1785 Polyhedron *C = supporting_cone_p(P, V);
1786 this->_i = _i;
1787 this->V = V;
1789 vE[_i] = new evalue;
1790 value_init(vE[_i]->d);
1791 evalue_set_si(vE[_i], 0, 1);
1793 decompose(C, MaxRays);
1796 ~enumerator() {
1797 mpq_clear(count);
1798 Vector_Free(c);
1800 for (int j = 0; j < nbV; ++j)
1801 if (vE[j]) {
1802 free_evalue_refs(vE[j]);
1803 delete vE[j];
1805 delete [] vE;
1808 virtual void handle_polar(Polyhedron *P, int sign);
1811 void enumerator::handle_polar(Polyhedron *C, int s)
1813 int r = 0;
1814 assert(C->NbRays-1 == dim);
1815 add_rays(rays, C, &r);
1816 for (int k = 0; k < dim; ++k) {
1817 assert(lambda * rays[k] != 0);
1820 sign = s;
1822 lattice_point(V, C, lambda, &num, 0);
1823 den = rays * lambda;
1824 normalize(sign, num.constant, den);
1826 dpoly n(dim, den[0], 1);
1827 for (int k = 1; k < dim; ++k) {
1828 dpoly fact(dim, den[k], 1);
1829 n *= fact;
1831 if (num.E != NULL) {
1832 ZZ one(INIT_VAL, 1);
1833 dpoly_n d(dim, num.constant, one);
1834 d.div(n, c, sign);
1835 evalue EV;
1836 multi_polynom(c, num.E, &EV);
1837 eadd(&EV , vE[_i]);
1838 free_evalue_refs(&EV);
1839 free_evalue_refs(num.E);
1840 delete num.E;
1841 } else if (num.pos != -1) {
1842 dpoly_n d(dim, num.constant, num.coeff);
1843 d.div(n, c, sign);
1844 evalue EV;
1845 uni_polynom(num.pos, c, &EV);
1846 eadd(&EV , vE[_i]);
1847 free_evalue_refs(&EV);
1848 } else {
1849 mpq_set_si(count, 0, 1);
1850 dpoly d(dim, num.constant);
1851 d.div(n, count, sign);
1852 evalue EV;
1853 value_init(EV.d);
1854 evalue_set(&EV, &count[0]._mp_num, &count[0]._mp_den);
1855 eadd(&EV , vE[_i]);
1856 free_evalue_refs(&EV);
1860 evalue* barvinok_enumerate_ev(Polyhedron *P, Polyhedron* C, unsigned MaxRays)
1862 //P = unfringe(P, MaxRays);
1863 Polyhedron *CEq = NULL, *rVD, *pVD, *CA;
1864 Matrix *CT = NULL;
1865 Param_Polyhedron *PP = NULL;
1866 Param_Domain *D, *next;
1867 Param_Vertices *V;
1868 int r = 0;
1869 unsigned nparam = C->Dimension;
1870 evalue *eres;
1871 ALLOC(evalue, eres);
1872 value_init(eres->d);
1873 value_set_si(eres->d, 0);
1875 evalue factor;
1876 value_init(factor.d);
1877 evalue_set_si(&factor, 1, 1);
1879 CA = align_context(C, P->Dimension, MaxRays);
1880 P = DomainIntersection(P, CA, MaxRays);
1881 Polyhedron_Free(CA);
1883 if (C->Dimension == 0 || emptyQ(P)) {
1884 constant:
1885 eres->x.p = new_enode(partition, 2, C->Dimension);
1886 EVALUE_SET_DOMAIN(eres->x.p->arr[0],
1887 DomainConstraintSimplify(CEq ? CEq : Polyhedron_Copy(C), MaxRays));
1888 value_set_si(eres->x.p->arr[1].d, 1);
1889 value_init(eres->x.p->arr[1].x.n);
1890 if (emptyQ(P))
1891 value_set_si(eres->x.p->arr[1].x.n, 0);
1892 else
1893 barvinok_count(P, &eres->x.p->arr[1].x.n, MaxRays);
1894 out:
1895 emul(&factor, eres);
1896 reduce_evalue(eres);
1897 free_evalue_refs(&factor);
1898 Polyhedron_Free(P);
1899 if (CT)
1900 Matrix_Free(CT);
1901 if (PP)
1902 Param_Polyhedron_Free(PP);
1904 return eres;
1906 if (Polyhedron_is_infinite(P, nparam))
1907 goto constant;
1909 if (P->NbEq != 0) {
1910 Matrix *f;
1911 P = remove_equalities_p(P, P->Dimension-nparam, &f);
1912 mask(f, &factor);
1913 Matrix_Free(f);
1915 if (P->Dimension == nparam) {
1916 CEq = P;
1917 P = Universe_Polyhedron(0);
1918 goto constant;
1921 Polyhedron *Q = ParamPolyhedron_Reduce(P, P->Dimension-nparam, &factor);
1922 if (Q) {
1923 Polyhedron_Free(P);
1924 if (Q->Dimension == nparam) {
1925 CEq = Q;
1926 P = Universe_Polyhedron(0);
1927 goto constant;
1929 P = Q;
1931 Polyhedron *oldP = P;
1932 PP = Polyhedron2Param_SimplifiedDomain(&P,C,MaxRays,&CEq,&CT);
1933 if (P != oldP)
1934 Polyhedron_Free(oldP);
1936 if (isIdentity(CT)) {
1937 Matrix_Free(CT);
1938 CT = NULL;
1939 } else {
1940 assert(CT->NbRows != CT->NbColumns);
1941 if (CT->NbRows == 1) // no more parameters
1942 goto constant;
1943 nparam = CT->NbRows - 1;
1946 unsigned dim = P->Dimension - nparam;
1948 enumerator et(P, dim, PP->nbV);
1950 int nd;
1951 for (nd = 0, D=PP->D; D; ++nd, D=D->next);
1952 struct section { Polyhedron *D; evalue E; };
1953 section *s = new section[nd];
1954 Polyhedron **fVD = new Polyhedron_p[nd];
1956 for(nd = 0, D=PP->D; D; D=next) {
1957 next = D->next;
1959 Polyhedron *rVD = reduce_domain(D->Domain, CT, CEq,
1960 fVD, nd, MaxRays);
1961 if (!rVD)
1962 continue;
1964 pVD = CT ? DomainImage(rVD,CT,MaxRays) : rVD;
1966 value_init(s[nd].E.d);
1967 evalue_set_si(&s[nd].E, 0, 1);
1969 FORALL_PVertex_in_ParamPolyhedron(V,D,PP) // _i is internal counter
1970 if (!et.vE[_i])
1971 et.decompose_at(V, _i, MaxRays);
1972 eadd(et.vE[_i] , &s[nd].E);
1973 END_FORALL_PVertex_in_ParamPolyhedron;
1974 reduce_in_domain(&s[nd].E, pVD);
1976 if (CT)
1977 addeliminatedparams_evalue(&s[nd].E, CT);
1978 s[nd].D = rVD;
1979 ++nd;
1980 if (rVD != pVD)
1981 Domain_Free(pVD);
1984 if (nd == 0)
1985 evalue_set_si(eres, 0, 1);
1986 else {
1987 eres->x.p = new_enode(partition, 2*nd, C->Dimension);
1988 for (int j = 0; j < nd; ++j) {
1989 EVALUE_SET_DOMAIN(eres->x.p->arr[2*j], s[j].D);
1990 value_clear(eres->x.p->arr[2*j+1].d);
1991 eres->x.p->arr[2*j+1] = s[j].E;
1992 Domain_Free(fVD[j]);
1995 delete [] s;
1996 delete [] fVD;
1999 if (CEq)
2000 Polyhedron_Free(CEq);
2002 goto out;
2005 Enumeration* barvinok_enumerate(Polyhedron *P, Polyhedron* C, unsigned MaxRays)
2007 evalue *EP = barvinok_enumerate_ev(P, C, MaxRays);
2009 return partition2enumeration(EP);
2012 static void SwapColumns(Value **V, int n, int i, int j)
2014 for (int r = 0; r < n; ++r)
2015 value_swap(V[r][i], V[r][j]);
2018 static void SwapColumns(Polyhedron *P, int i, int j)
2020 SwapColumns(P->Constraint, P->NbConstraints, i, j);
2021 SwapColumns(P->Ray, P->NbRays, i, j);
2024 static void negative_test_constraint(Value *l, Value *u, Value *c, int pos,
2025 int len, Value *v)
2027 value_oppose(*v, u[pos+1]);
2028 Vector_Combine(l+1, u+1, c+1, *v, l[pos+1], len-1);
2029 value_multiply(*v, *v, l[pos+1]);
2030 value_substract(c[len-1], c[len-1], *v);
2031 value_set_si(*v, -1);
2032 Vector_Scale(c+1, c+1, *v, len-1);
2033 value_decrement(c[len-1], c[len-1]);
2034 ConstraintSimplify(c, c, len, v);
2037 static bool parallel_constraints(Value *l, Value *u, Value *c, int pos,
2038 int len)
2040 bool parallel;
2041 Value g1;
2042 Value g2;
2043 value_init(g1);
2044 value_init(g2);
2046 Vector_Gcd(&l[1+pos], len, &g1);
2047 Vector_Gcd(&u[1+pos], len, &g2);
2048 Vector_Combine(l+1+pos, u+1+pos, c+1, g2, g1, len);
2049 parallel = First_Non_Zero(c+1, len) == -1;
2051 value_clear(g1);
2052 value_clear(g2);
2054 return parallel;
2057 static void negative_test_constraint7(Value *l, Value *u, Value *c, int pos,
2058 int exist, int len, Value *v)
2060 Value g;
2061 value_init(g);
2063 Vector_Gcd(&u[1+pos], exist, v);
2064 Vector_Gcd(&l[1+pos], exist, &g);
2065 Vector_Combine(l+1, u+1, c+1, *v, g, len-1);
2066 value_multiply(*v, *v, g);
2067 value_substract(c[len-1], c[len-1], *v);
2068 value_set_si(*v, -1);
2069 Vector_Scale(c+1, c+1, *v, len-1);
2070 value_decrement(c[len-1], c[len-1]);
2071 ConstraintSimplify(c, c, len, v);
2073 value_clear(g);
2076 static void oppose_constraint(Value *c, int len, Value *v)
2078 value_set_si(*v, -1);
2079 Vector_Scale(c+1, c+1, *v, len-1);
2080 value_decrement(c[len-1], c[len-1]);
2083 static bool SplitOnConstraint(Polyhedron *P, int i, int l, int u,
2084 int nvar, int len, int exist, int MaxRays,
2085 Vector *row, Value& f, bool independent,
2086 Polyhedron **pos, Polyhedron **neg)
2088 negative_test_constraint(P->Constraint[l], P->Constraint[u],
2089 row->p, nvar+i, len, &f);
2090 *neg = AddConstraints(row->p, 1, P, MaxRays);
2092 /* We found an independent, but useless constraint
2093 * Maybe we should detect this earlier and not
2094 * mark the variable as INDEPENDENT
2096 if (emptyQ((*neg))) {
2097 Polyhedron_Free(*neg);
2098 return false;
2101 oppose_constraint(row->p, len, &f);
2102 *pos = AddConstraints(row->p, 1, P, MaxRays);
2104 if (emptyQ((*pos))) {
2105 Polyhedron_Free(*neg);
2106 Polyhedron_Free(*pos);
2107 return false;
2110 return true;
2114 * unimodularly transform P such that constraint r is transformed
2115 * into a constraint that involves only a single (the first)
2116 * existential variable
2119 static Polyhedron *rotate_along(Polyhedron *P, int r, int nvar, int exist,
2120 unsigned MaxRays)
2122 Value g;
2123 value_init(g);
2125 Vector *row = Vector_Alloc(exist);
2126 Vector_Copy(P->Constraint[r]+1+nvar, row->p, exist);
2127 Vector_Gcd(row->p, exist, &g);
2128 if (value_notone_p(g))
2129 Vector_AntiScale(row->p, row->p, g, exist);
2130 value_clear(g);
2132 Matrix *M = unimodular_complete(row);
2133 Matrix *M2 = Matrix_Alloc(P->Dimension+1, P->Dimension+1);
2134 for (r = 0; r < nvar; ++r)
2135 value_set_si(M2->p[r][r], 1);
2136 for ( ; r < nvar+exist; ++r)
2137 Vector_Copy(M->p[r-nvar], M2->p[r]+nvar, exist);
2138 for ( ; r < P->Dimension+1; ++r)
2139 value_set_si(M2->p[r][r], 1);
2140 Polyhedron *T = Polyhedron_Image(P, M2, MaxRays);
2142 Matrix_Free(M2);
2143 Matrix_Free(M);
2144 Vector_Free(row);
2146 return T;
2149 static bool SplitOnVar(Polyhedron *P, int i,
2150 int nvar, int len, int exist, int MaxRays,
2151 Vector *row, Value& f, bool independent,
2152 Polyhedron **pos, Polyhedron **neg)
2154 int j;
2156 for (int l = P->NbEq; l < P->NbConstraints; ++l) {
2157 if (value_negz_p(P->Constraint[l][nvar+i+1]))
2158 continue;
2160 if (independent) {
2161 for (j = 0; j < exist; ++j)
2162 if (j != i && value_notzero_p(P->Constraint[l][nvar+j+1]))
2163 break;
2164 if (j < exist)
2165 continue;
2168 for (int u = P->NbEq; u < P->NbConstraints; ++u) {
2169 if (value_posz_p(P->Constraint[u][nvar+i+1]))
2170 continue;
2172 if (independent) {
2173 for (j = 0; j < exist; ++j)
2174 if (j != i && value_notzero_p(P->Constraint[u][nvar+j+1]))
2175 break;
2176 if (j < exist)
2177 continue;
2180 if (SplitOnConstraint(P, i, l, u,
2181 nvar, len, exist, MaxRays,
2182 row, f, independent,
2183 pos, neg)) {
2184 if (independent) {
2185 if (i != 0)
2186 SwapColumns(*neg, nvar+1, nvar+1+i);
2188 return true;
2193 return false;
2196 static bool double_bound_pair(Polyhedron *P, int nvar, int exist,
2197 int i, int l1, int l2,
2198 Polyhedron **pos, Polyhedron **neg)
2200 Value f;
2201 value_init(f);
2202 Vector *row = Vector_Alloc(P->Dimension+2);
2203 value_set_si(row->p[0], 1);
2204 value_oppose(f, P->Constraint[l1][nvar+i+1]);
2205 Vector_Combine(P->Constraint[l1]+1, P->Constraint[l2]+1,
2206 row->p+1,
2207 P->Constraint[l2][nvar+i+1], f,
2208 P->Dimension+1);
2209 ConstraintSimplify(row->p, row->p, P->Dimension+2, &f);
2210 *pos = AddConstraints(row->p, 1, P, 0);
2211 value_set_si(f, -1);
2212 Vector_Scale(row->p+1, row->p+1, f, P->Dimension+1);
2213 value_decrement(row->p[P->Dimension+1], row->p[P->Dimension+1]);
2214 *neg = AddConstraints(row->p, 1, P, 0);
2215 Vector_Free(row);
2216 value_clear(f);
2218 return !emptyQ((*pos)) && !emptyQ((*neg));
2221 static bool double_bound(Polyhedron *P, int nvar, int exist,
2222 Polyhedron **pos, Polyhedron **neg)
2224 for (int i = 0; i < exist; ++i) {
2225 int l1, l2;
2226 for (l1 = P->NbEq; l1 < P->NbConstraints; ++l1) {
2227 if (value_negz_p(P->Constraint[l1][nvar+i+1]))
2228 continue;
2229 for (l2 = l1 + 1; l2 < P->NbConstraints; ++l2) {
2230 if (value_negz_p(P->Constraint[l2][nvar+i+1]))
2231 continue;
2232 if (double_bound_pair(P, nvar, exist, i, l1, l2, pos, neg))
2233 return true;
2236 for (l1 = P->NbEq; l1 < P->NbConstraints; ++l1) {
2237 if (value_posz_p(P->Constraint[l1][nvar+i+1]))
2238 continue;
2239 if (l1 < P->NbConstraints)
2240 for (l2 = l1 + 1; l2 < P->NbConstraints; ++l2) {
2241 if (value_posz_p(P->Constraint[l2][nvar+i+1]))
2242 continue;
2243 if (double_bound_pair(P, nvar, exist, i, l1, l2, pos, neg))
2244 return true;
2247 return false;
2249 return false;
2252 enum constraint {
2253 ALL_POS = 1 << 0,
2254 ONE_NEG = 1 << 1,
2255 INDEPENDENT = 1 << 2,
2256 ROT_NEG = 1 << 3
2259 static evalue* enumerate_or(Polyhedron *D,
2260 unsigned exist, unsigned nparam, unsigned MaxRays)
2262 #ifdef DEBUG_ER
2263 fprintf(stderr, "\nER: Or\n");
2264 #endif /* DEBUG_ER */
2266 Polyhedron *N = D->next;
2267 D->next = 0;
2268 evalue *EP =
2269 barvinok_enumerate_e(D, exist, nparam, MaxRays);
2270 Polyhedron_Free(D);
2272 for (D = N; D; D = N) {
2273 N = D->next;
2274 D->next = 0;
2276 evalue *EN =
2277 barvinok_enumerate_e(D, exist, nparam, MaxRays);
2279 eor(EN, EP);
2280 free_evalue_refs(EN);
2281 free(EN);
2282 Polyhedron_Free(D);
2285 reduce_evalue(EP);
2287 return EP;
2290 static evalue* enumerate_sum(Polyhedron *P,
2291 unsigned exist, unsigned nparam, unsigned MaxRays)
2293 int nvar = P->Dimension - exist - nparam;
2294 int toswap = nvar < exist ? nvar : exist;
2295 for (int i = 0; i < toswap; ++i)
2296 SwapColumns(P, 1 + i, nvar+exist - i);
2297 nparam += nvar;
2299 #ifdef DEBUG_ER
2300 fprintf(stderr, "\nER: Sum\n");
2301 #endif /* DEBUG_ER */
2303 evalue *EP = barvinok_enumerate_e(P, exist, nparam, MaxRays);
2305 for (int i = 0; i < /* nvar */ nparam; ++i) {
2306 Matrix *C = Matrix_Alloc(1, 1 + nparam + 1);
2307 value_set_si(C->p[0][0], 1);
2308 evalue split;
2309 value_init(split.d);
2310 value_set_si(split.d, 0);
2311 split.x.p = new_enode(partition, 4, nparam);
2312 value_set_si(C->p[0][1+i], 1);
2313 Matrix *C2 = Matrix_Copy(C);
2314 EVALUE_SET_DOMAIN(split.x.p->arr[0],
2315 Constraints2Polyhedron(C2, MaxRays));
2316 Matrix_Free(C2);
2317 evalue_set_si(&split.x.p->arr[1], 1, 1);
2318 value_set_si(C->p[0][1+i], -1);
2319 value_set_si(C->p[0][1+nparam], -1);
2320 EVALUE_SET_DOMAIN(split.x.p->arr[2],
2321 Constraints2Polyhedron(C, MaxRays));
2322 evalue_set_si(&split.x.p->arr[3], 1, 1);
2323 emul(&split, EP);
2324 free_evalue_refs(&split);
2325 Matrix_Free(C);
2327 reduce_evalue(EP);
2328 evalue_range_reduction(EP);
2330 evalue_frac2floor(EP);
2332 evalue *sum = esum(EP, nvar);
2334 free_evalue_refs(EP);
2335 free(EP);
2336 EP = sum;
2338 evalue_range_reduction(EP);
2340 return EP;
2343 static evalue* split_sure(Polyhedron *P, Polyhedron *S,
2344 unsigned exist, unsigned nparam, unsigned MaxRays)
2346 int nvar = P->Dimension - exist - nparam;
2348 Matrix *M = Matrix_Alloc(exist, S->Dimension+2);
2349 for (int i = 0; i < exist; ++i)
2350 value_set_si(M->p[i][nvar+i+1], 1);
2351 Polyhedron *O = S;
2352 S = DomainAddRays(S, M, MaxRays);
2353 Polyhedron_Free(O);
2354 Polyhedron *F = DomainAddRays(P, M, MaxRays);
2355 Polyhedron *D = DomainDifference(F, S, MaxRays);
2356 O = D;
2357 D = Disjoint_Domain(D, 0, MaxRays);
2358 Polyhedron_Free(F);
2359 Domain_Free(O);
2360 Matrix_Free(M);
2362 M = Matrix_Alloc(P->Dimension+1-exist, P->Dimension+1);
2363 for (int j = 0; j < nvar; ++j)
2364 value_set_si(M->p[j][j], 1);
2365 for (int j = 0; j < nparam+1; ++j)
2366 value_set_si(M->p[nvar+j][nvar+exist+j], 1);
2367 Polyhedron *T = Polyhedron_Image(S, M, MaxRays);
2368 evalue *EP = barvinok_enumerate_e(T, 0, nparam, MaxRays);
2369 Polyhedron_Free(S);
2370 Polyhedron_Free(T);
2371 Matrix_Free(M);
2373 for (Polyhedron *Q = D; Q; Q = Q->next) {
2374 Polyhedron *N = Q->next;
2375 Q->next = 0;
2376 T = DomainIntersection(P, Q, MaxRays);
2377 evalue *E = barvinok_enumerate_e(T, exist, nparam, MaxRays);
2378 eadd(E, EP);
2379 free_evalue_refs(E);
2380 free(E);
2381 Polyhedron_Free(T);
2382 Q->next = N;
2384 Domain_Free(D);
2385 return EP;
2388 static evalue* enumerate_sure(Polyhedron *P,
2389 unsigned exist, unsigned nparam, unsigned MaxRays)
2391 int i;
2392 Polyhedron *S = P;
2393 int nvar = P->Dimension - exist - nparam;
2394 Value lcm;
2395 Value f;
2396 value_init(lcm);
2397 value_init(f);
2399 for (i = 0; i < exist; ++i) {
2400 Matrix *M = Matrix_Alloc(S->NbConstraints, S->Dimension+2);
2401 int c = 0;
2402 value_set_si(lcm, 1);
2403 for (int j = 0; j < S->NbConstraints; ++j) {
2404 if (value_negz_p(S->Constraint[j][1+nvar+i]))
2405 continue;
2406 if (value_one_p(S->Constraint[j][1+nvar+i]))
2407 continue;
2408 value_lcm(lcm, S->Constraint[j][1+nvar+i], &lcm);
2411 for (int j = 0; j < S->NbConstraints; ++j) {
2412 if (value_negz_p(S->Constraint[j][1+nvar+i]))
2413 continue;
2414 if (value_one_p(S->Constraint[j][1+nvar+i]))
2415 continue;
2416 value_division(f, lcm, S->Constraint[j][1+nvar+i]);
2417 Vector_Scale(S->Constraint[j], M->p[c], f, S->Dimension+2);
2418 value_substract(M->p[c][S->Dimension+1],
2419 M->p[c][S->Dimension+1],
2420 lcm);
2421 value_increment(M->p[c][S->Dimension+1],
2422 M->p[c][S->Dimension+1]);
2423 ++c;
2425 Polyhedron *O = S;
2426 S = AddConstraints(M->p[0], c, S, MaxRays);
2427 if (O != P)
2428 Polyhedron_Free(O);
2429 Matrix_Free(M);
2430 if (emptyQ(S)) {
2431 Polyhedron_Free(S);
2432 value_clear(lcm);
2433 value_clear(f);
2434 return 0;
2437 value_clear(lcm);
2438 value_clear(f);
2440 #ifdef DEBUG_ER
2441 fprintf(stderr, "\nER: Sure\n");
2442 #endif /* DEBUG_ER */
2444 return split_sure(P, S, exist, nparam, MaxRays);
2447 static evalue* enumerate_sure2(Polyhedron *P,
2448 unsigned exist, unsigned nparam, unsigned MaxRays)
2450 int nvar = P->Dimension - exist - nparam;
2451 int r;
2452 for (r = 0; r < P->NbRays; ++r)
2453 if (value_one_p(P->Ray[r][0]) &&
2454 value_one_p(P->Ray[r][P->Dimension+1]))
2455 break;
2457 if (r >= P->NbRays)
2458 return 0;
2460 Matrix *M = Matrix_Alloc(nvar + 1 + nparam, P->Dimension+2);
2461 for (int i = 0; i < nvar; ++i)
2462 value_set_si(M->p[i][1+i], 1);
2463 for (int i = 0; i < nparam; ++i)
2464 value_set_si(M->p[i+nvar][1+nvar+exist+i], 1);
2465 Vector_Copy(P->Ray[r]+1+nvar, M->p[nvar+nparam]+1+nvar, exist);
2466 value_set_si(M->p[nvar+nparam][0], 1);
2467 value_set_si(M->p[nvar+nparam][P->Dimension+1], 1);
2468 Polyhedron * F = Rays2Polyhedron(M, MaxRays);
2469 Matrix_Free(M);
2471 Polyhedron *I = DomainIntersection(F, P, MaxRays);
2472 Polyhedron_Free(F);
2474 #ifdef DEBUG_ER
2475 fprintf(stderr, "\nER: Sure2\n");
2476 #endif /* DEBUG_ER */
2478 return split_sure(P, I, exist, nparam, MaxRays);
2481 static evalue* enumerate_cyclic(Polyhedron *P,
2482 unsigned exist, unsigned nparam,
2483 evalue * EP, int r, int p, unsigned MaxRays)
2485 int nvar = P->Dimension - exist - nparam;
2487 /* If EP in its fractional maps only contains references
2488 * to the remainder parameter with appropriate coefficients
2489 * then we could in principle avoid adding existentially
2490 * quantified variables to the validity domains.
2491 * We'd have to replace the remainder by m { p/m }
2492 * and multiply with an appropriate factor that is one
2493 * only in the appropriate range.
2494 * This last multiplication can be avoided if EP
2495 * has a single validity domain with no (further)
2496 * constraints on the remainder parameter
2499 Matrix *CT = Matrix_Alloc(nparam+1, nparam+3);
2500 Matrix *M = Matrix_Alloc(1, 1+nparam+3);
2501 for (int j = 0; j < nparam; ++j)
2502 if (j != p)
2503 value_set_si(CT->p[j][j], 1);
2504 value_set_si(CT->p[p][nparam+1], 1);
2505 value_set_si(CT->p[nparam][nparam+2], 1);
2506 value_set_si(M->p[0][1+p], -1);
2507 value_absolute(M->p[0][1+nparam], P->Ray[0][1+nvar+exist+p]);
2508 value_set_si(M->p[0][1+nparam+1], 1);
2509 Polyhedron *CEq = Constraints2Polyhedron(M, 1);
2510 Matrix_Free(M);
2511 addeliminatedparams_enum(EP, CT, CEq, MaxRays, nparam);
2512 Polyhedron_Free(CEq);
2513 Matrix_Free(CT);
2515 return EP;
2518 static void enumerate_vd_add_ray(evalue *EP, Matrix *Rays, unsigned MaxRays)
2520 if (value_notzero_p(EP->d))
2521 return;
2523 assert(EP->x.p->type == partition);
2524 assert(EP->x.p->pos == EVALUE_DOMAIN(EP->x.p->arr[0])->Dimension);
2525 for (int i = 0; i < EP->x.p->size/2; ++i) {
2526 Polyhedron *D = EVALUE_DOMAIN(EP->x.p->arr[2*i]);
2527 Polyhedron *N = DomainAddRays(D, Rays, MaxRays);
2528 EVALUE_SET_DOMAIN(EP->x.p->arr[2*i], N);
2529 Domain_Free(D);
2533 static evalue* enumerate_line(Polyhedron *P,
2534 unsigned exist, unsigned nparam, unsigned MaxRays)
2536 if (P->NbBid == 0)
2537 return 0;
2539 #ifdef DEBUG_ER
2540 fprintf(stderr, "\nER: Line\n");
2541 #endif /* DEBUG_ER */
2543 int nvar = P->Dimension - exist - nparam;
2544 int i, j;
2545 for (i = 0; i < nparam; ++i)
2546 if (value_notzero_p(P->Ray[0][1+nvar+exist+i]))
2547 break;
2548 assert(i < nparam);
2549 for (j = i+1; j < nparam; ++j)
2550 if (value_notzero_p(P->Ray[0][1+nvar+exist+i]))
2551 break;
2552 assert(j >= nparam); // for now
2554 Matrix *M = Matrix_Alloc(2, P->Dimension+2);
2555 value_set_si(M->p[0][0], 1);
2556 value_set_si(M->p[0][1+nvar+exist+i], 1);
2557 value_set_si(M->p[1][0], 1);
2558 value_set_si(M->p[1][1+nvar+exist+i], -1);
2559 value_absolute(M->p[1][1+P->Dimension], P->Ray[0][1+nvar+exist+i]);
2560 value_decrement(M->p[1][1+P->Dimension], M->p[1][1+P->Dimension]);
2561 Polyhedron *S = AddConstraints(M->p[0], 2, P, MaxRays);
2562 evalue *EP = barvinok_enumerate_e(S, exist, nparam, MaxRays);
2563 Polyhedron_Free(S);
2564 Matrix_Free(M);
2566 return enumerate_cyclic(P, exist, nparam, EP, 0, i, MaxRays);
2569 static int single_param_pos(Polyhedron*P, unsigned exist, unsigned nparam,
2570 int r)
2572 int nvar = P->Dimension - exist - nparam;
2573 if (First_Non_Zero(P->Ray[r]+1, nvar) != -1)
2574 return -1;
2575 int i = First_Non_Zero(P->Ray[r]+1+nvar+exist, nparam);
2576 if (i == -1)
2577 return -1;
2578 if (First_Non_Zero(P->Ray[r]+1+nvar+exist+1, nparam-i-1) != -1)
2579 return -1;
2580 return i;
2583 static evalue* enumerate_remove_ray(Polyhedron *P, int r,
2584 unsigned exist, unsigned nparam, unsigned MaxRays)
2586 #ifdef DEBUG_ER
2587 fprintf(stderr, "\nER: RedundantRay\n");
2588 #endif /* DEBUG_ER */
2590 Value one;
2591 value_init(one);
2592 value_set_si(one, 1);
2593 int len = P->NbRays-1;
2594 Matrix *M = Matrix_Alloc(2 * len, P->Dimension+2);
2595 Vector_Copy(P->Ray[0], M->p[0], r * (P->Dimension+2));
2596 Vector_Copy(P->Ray[r+1], M->p[r], (len-r) * (P->Dimension+2));
2597 for (int j = 0; j < P->NbRays; ++j) {
2598 if (j == r)
2599 continue;
2600 Vector_Combine(P->Ray[j], P->Ray[r], M->p[len+j-(j>r)],
2601 one, P->Ray[j][P->Dimension+1], P->Dimension+2);
2604 P = Rays2Polyhedron(M, MaxRays);
2605 Matrix_Free(M);
2606 evalue *EP = barvinok_enumerate_e(P, exist, nparam, MaxRays);
2607 Polyhedron_Free(P);
2608 value_clear(one);
2610 return EP;
2613 static evalue* enumerate_redundant_ray(Polyhedron *P,
2614 unsigned exist, unsigned nparam, unsigned MaxRays)
2616 assert(P->NbBid == 0);
2617 int nvar = P->Dimension - exist - nparam;
2618 Value m;
2619 value_init(m);
2621 for (int r = 0; r < P->NbRays; ++r) {
2622 if (value_notzero_p(P->Ray[r][P->Dimension+1]))
2623 continue;
2624 int i1 = single_param_pos(P, exist, nparam, r);
2625 if (i1 == -1)
2626 continue;
2627 for (int r2 = r+1; r2 < P->NbRays; ++r2) {
2628 if (value_notzero_p(P->Ray[r2][P->Dimension+1]))
2629 continue;
2630 int i2 = single_param_pos(P, exist, nparam, r2);
2631 if (i2 == -1)
2632 continue;
2633 if (i1 != i2)
2634 continue;
2636 value_division(m, P->Ray[r][1+nvar+exist+i1],
2637 P->Ray[r2][1+nvar+exist+i1]);
2638 value_multiply(m, m, P->Ray[r2][1+nvar+exist+i1]);
2639 /* r2 divides r => r redundant */
2640 if (value_eq(m, P->Ray[r][1+nvar+exist+i1])) {
2641 value_clear(m);
2642 return enumerate_remove_ray(P, r, exist, nparam, MaxRays);
2645 value_division(m, P->Ray[r2][1+nvar+exist+i1],
2646 P->Ray[r][1+nvar+exist+i1]);
2647 value_multiply(m, m, P->Ray[r][1+nvar+exist+i1]);
2648 /* r divides r2 => r2 redundant */
2649 if (value_eq(m, P->Ray[r2][1+nvar+exist+i1])) {
2650 value_clear(m);
2651 return enumerate_remove_ray(P, r2, exist, nparam, MaxRays);
2655 value_clear(m);
2656 return 0;
2659 static Polyhedron *upper_bound(Polyhedron *P,
2660 int pos, Value *max, Polyhedron **R)
2662 Value v;
2663 int r;
2664 value_init(v);
2666 *R = 0;
2667 Polyhedron *N;
2668 Polyhedron *B = 0;
2669 for (Polyhedron *Q = P; Q; Q = N) {
2670 N = Q->next;
2671 for (r = 0; r < P->NbRays; ++r) {
2672 if (value_zero_p(P->Ray[r][P->Dimension+1]) &&
2673 value_pos_p(P->Ray[r][1+pos]))
2674 break;
2676 if (r < P->NbRays) {
2677 Q->next = *R;
2678 *R = Q;
2679 continue;
2680 } else {
2681 Q->next = B;
2682 B = Q;
2684 for (r = 0; r < P->NbRays; ++r) {
2685 if (value_zero_p(P->Ray[r][P->Dimension+1]))
2686 continue;
2687 mpz_fdiv_q(v, P->Ray[r][1+pos], P->Ray[r][1+P->Dimension]);
2688 if ((!Q->next && r == 0) || value_gt(v, *max))
2689 value_assign(*max, v);
2692 value_clear(v);
2693 return B;
2696 static evalue* enumerate_ray(Polyhedron *P,
2697 unsigned exist, unsigned nparam, unsigned MaxRays)
2699 assert(P->NbBid == 0);
2700 int nvar = P->Dimension - exist - nparam;
2702 int r;
2703 for (r = 0; r < P->NbRays; ++r)
2704 if (value_zero_p(P->Ray[r][P->Dimension+1]))
2705 break;
2706 if (r >= P->NbRays)
2707 return 0;
2709 int r2;
2710 for (r2 = r+1; r2 < P->NbRays; ++r2)
2711 if (value_zero_p(P->Ray[r2][P->Dimension+1]))
2712 break;
2713 if (r2 < P->NbRays) {
2714 if (nvar > 0)
2715 return enumerate_sum(P, exist, nparam, MaxRays);
2718 #ifdef DEBUG_ER
2719 fprintf(stderr, "\nER: Ray\n");
2720 #endif /* DEBUG_ER */
2722 Value m;
2723 Value one;
2724 value_init(m);
2725 value_init(one);
2726 value_set_si(one, 1);
2727 int i = single_param_pos(P, exist, nparam, r);
2728 assert(i != -1); // for now;
2730 Matrix *M = Matrix_Alloc(P->NbRays, P->Dimension+2);
2731 for (int j = 0; j < P->NbRays; ++j) {
2732 Vector_Combine(P->Ray[j], P->Ray[r], M->p[j],
2733 one, P->Ray[j][P->Dimension+1], P->Dimension+2);
2735 Polyhedron *S = Rays2Polyhedron(M, MaxRays);
2736 Matrix_Free(M);
2737 Polyhedron *D = DomainDifference(P, S, MaxRays);
2738 Polyhedron_Free(S);
2739 // Polyhedron_Print(stderr, P_VALUE_FMT, D);
2740 assert(value_pos_p(P->Ray[r][1+nvar+exist+i])); // for now
2741 Polyhedron *R;
2742 D = upper_bound(D, nvar+exist+i, &m, &R);
2743 assert(D);
2744 Domain_Free(D);
2746 M = Matrix_Alloc(2, P->Dimension+2);
2747 value_set_si(M->p[0][0], 1);
2748 value_set_si(M->p[1][0], 1);
2749 value_set_si(M->p[0][1+nvar+exist+i], -1);
2750 value_set_si(M->p[1][1+nvar+exist+i], 1);
2751 value_assign(M->p[0][1+P->Dimension], m);
2752 value_oppose(M->p[1][1+P->Dimension], m);
2753 value_addto(M->p[1][1+P->Dimension], M->p[1][1+P->Dimension],
2754 P->Ray[r][1+nvar+exist+i]);
2755 value_decrement(M->p[1][1+P->Dimension], M->p[1][1+P->Dimension]);
2756 // Matrix_Print(stderr, P_VALUE_FMT, M);
2757 D = AddConstraints(M->p[0], 2, P, MaxRays);
2758 // Polyhedron_Print(stderr, P_VALUE_FMT, D);
2759 value_substract(M->p[0][1+P->Dimension], M->p[0][1+P->Dimension],
2760 P->Ray[r][1+nvar+exist+i]);
2761 // Matrix_Print(stderr, P_VALUE_FMT, M);
2762 S = AddConstraints(M->p[0], 1, P, MaxRays);
2763 // Polyhedron_Print(stderr, P_VALUE_FMT, S);
2764 Matrix_Free(M);
2766 evalue *EP = barvinok_enumerate_e(D, exist, nparam, MaxRays);
2767 Polyhedron_Free(D);
2768 value_clear(one);
2769 value_clear(m);
2771 if (value_notone_p(P->Ray[r][1+nvar+exist+i]))
2772 EP = enumerate_cyclic(P, exist, nparam, EP, r, i, MaxRays);
2773 else {
2774 M = Matrix_Alloc(1, nparam+2);
2775 value_set_si(M->p[0][0], 1);
2776 value_set_si(M->p[0][1+i], 1);
2777 enumerate_vd_add_ray(EP, M, MaxRays);
2778 Matrix_Free(M);
2781 if (!emptyQ(S)) {
2782 evalue *E = barvinok_enumerate_e(S, exist, nparam, MaxRays);
2783 eadd(E, EP);
2784 free_evalue_refs(E);
2785 free(E);
2787 Polyhedron_Free(S);
2789 if (R) {
2790 assert(nvar == 0);
2791 evalue *ER = enumerate_or(R, exist, nparam, MaxRays);
2792 eor(ER, EP);
2793 free_evalue_refs(ER);
2794 free(ER);
2797 return EP;
2800 static evalue* new_zero_ep()
2802 evalue *EP;
2803 ALLOC(evalue, EP);
2804 value_init(EP->d);
2805 evalue_set_si(EP, 0, 1);
2806 return EP;
2809 static evalue* enumerate_vd(Polyhedron **PA,
2810 unsigned exist, unsigned nparam, unsigned MaxRays)
2812 Polyhedron *P = *PA;
2813 int nvar = P->Dimension - exist - nparam;
2814 Param_Polyhedron *PP = NULL;
2815 Polyhedron *C = Universe_Polyhedron(nparam);
2816 Polyhedron *CEq;
2817 Matrix *CT;
2818 Polyhedron *PR = P;
2819 PP = Polyhedron2Param_SimplifiedDomain(&PR,C,MaxRays,&CEq,&CT);
2820 Polyhedron_Free(C);
2822 int nd;
2823 Param_Domain *D, *last;
2824 Value c;
2825 value_init(c);
2826 for (nd = 0, D=PP->D; D; D=D->next, ++nd)
2829 Polyhedron **VD = new Polyhedron_p[nd];
2830 Polyhedron **fVD = new Polyhedron_p[nd];
2831 for(nd = 0, D=PP->D; D; D=D->next) {
2832 Polyhedron *rVD = reduce_domain(D->Domain, CT, CEq,
2833 fVD, nd, MaxRays);
2834 if (!rVD)
2835 continue;
2837 VD[nd++] = rVD;
2838 last = D;
2841 evalue *EP = 0;
2843 if (nd == 0)
2844 EP = new_zero_ep();
2846 /* This doesn't seem to have any effect */
2847 if (nd == 1) {
2848 Polyhedron *CA = align_context(VD[0], P->Dimension, MaxRays);
2849 Polyhedron *O = P;
2850 P = DomainIntersection(P, CA, MaxRays);
2851 if (O != *PA)
2852 Polyhedron_Free(O);
2853 Polyhedron_Free(CA);
2854 if (emptyQ(P))
2855 EP = new_zero_ep();
2858 if (!EP && CT->NbColumns != CT->NbRows) {
2859 Polyhedron *CEqr = DomainImage(CEq, CT, MaxRays);
2860 Polyhedron *CA = align_context(CEqr, PR->Dimension, MaxRays);
2861 Polyhedron *I = DomainIntersection(PR, CA, MaxRays);
2862 Polyhedron_Free(CEqr);
2863 Polyhedron_Free(CA);
2864 #ifdef DEBUG_ER
2865 fprintf(stderr, "\nER: Eliminate\n");
2866 #endif /* DEBUG_ER */
2867 nparam -= CT->NbColumns - CT->NbRows;
2868 EP = barvinok_enumerate_e(I, exist, nparam, MaxRays);
2869 nparam += CT->NbColumns - CT->NbRows;
2870 addeliminatedparams_enum(EP, CT, CEq, MaxRays, nparam);
2871 Polyhedron_Free(I);
2873 if (PR != *PA)
2874 Polyhedron_Free(PR);
2875 PR = 0;
2877 if (!EP && nd > 1) {
2878 #ifdef DEBUG_ER
2879 fprintf(stderr, "\nER: VD\n");
2880 #endif /* DEBUG_ER */
2881 for (int i = 0; i < nd; ++i) {
2882 Polyhedron *CA = align_context(VD[i], P->Dimension, MaxRays);
2883 Polyhedron *I = DomainIntersection(P, CA, MaxRays);
2885 if (i == 0)
2886 EP = barvinok_enumerate_e(I, exist, nparam, MaxRays);
2887 else {
2888 evalue *E = barvinok_enumerate_e(I, exist, nparam, MaxRays);
2889 eadd(E, EP);
2890 free_evalue_refs(E);
2891 free(E);
2893 Polyhedron_Free(I);
2894 Polyhedron_Free(CA);
2898 for (int i = 0; i < nd; ++i) {
2899 Polyhedron_Free(VD[i]);
2900 Polyhedron_Free(fVD[i]);
2902 delete [] VD;
2903 delete [] fVD;
2904 value_clear(c);
2906 if (!EP && nvar == 0) {
2907 Value f;
2908 value_init(f);
2909 Param_Vertices *V, *V2;
2910 Matrix* M = Matrix_Alloc(1, P->Dimension+2);
2912 FORALL_PVertex_in_ParamPolyhedron(V, last, PP) {
2913 bool found = false;
2914 FORALL_PVertex_in_ParamPolyhedron(V2, last, PP) {
2915 if (V == V2) {
2916 found = true;
2917 continue;
2919 if (!found)
2920 continue;
2921 for (int i = 0; i < exist; ++i) {
2922 value_oppose(f, V->Vertex->p[i][nparam+1]);
2923 Vector_Combine(V->Vertex->p[i],
2924 V2->Vertex->p[i],
2925 M->p[0] + 1 + nvar + exist,
2926 V2->Vertex->p[i][nparam+1],
2928 nparam+1);
2929 int j;
2930 for (j = 0; j < nparam; ++j)
2931 if (value_notzero_p(M->p[0][1+nvar+exist+j]))
2932 break;
2933 if (j >= nparam)
2934 continue;
2935 ConstraintSimplify(M->p[0], M->p[0],
2936 P->Dimension+2, &f);
2937 value_set_si(M->p[0][0], 0);
2938 Polyhedron *para = AddConstraints(M->p[0], 1, P,
2939 MaxRays);
2940 if (emptyQ(para)) {
2941 Polyhedron_Free(para);
2942 continue;
2944 Polyhedron *pos, *neg;
2945 value_set_si(M->p[0][0], 1);
2946 value_decrement(M->p[0][P->Dimension+1],
2947 M->p[0][P->Dimension+1]);
2948 neg = AddConstraints(M->p[0], 1, P, MaxRays);
2949 value_set_si(f, -1);
2950 Vector_Scale(M->p[0]+1, M->p[0]+1, f,
2951 P->Dimension+1);
2952 value_decrement(M->p[0][P->Dimension+1],
2953 M->p[0][P->Dimension+1]);
2954 value_decrement(M->p[0][P->Dimension+1],
2955 M->p[0][P->Dimension+1]);
2956 pos = AddConstraints(M->p[0], 1, P, MaxRays);
2957 if (emptyQ(neg) && emptyQ(pos)) {
2958 Polyhedron_Free(para);
2959 Polyhedron_Free(pos);
2960 Polyhedron_Free(neg);
2961 continue;
2963 #ifdef DEBUG_ER
2964 fprintf(stderr, "\nER: Order\n");
2965 #endif /* DEBUG_ER */
2966 EP = barvinok_enumerate_e(para, exist, nparam, MaxRays);
2967 evalue *E;
2968 if (!emptyQ(pos)) {
2969 E = barvinok_enumerate_e(pos, exist, nparam, MaxRays);
2970 eadd(E, EP);
2971 free_evalue_refs(E);
2972 free(E);
2974 if (!emptyQ(neg)) {
2975 E = barvinok_enumerate_e(neg, exist, nparam, MaxRays);
2976 eadd(E, EP);
2977 free_evalue_refs(E);
2978 free(E);
2980 Polyhedron_Free(para);
2981 Polyhedron_Free(pos);
2982 Polyhedron_Free(neg);
2983 break;
2985 if (EP)
2986 break;
2987 } END_FORALL_PVertex_in_ParamPolyhedron;
2988 if (EP)
2989 break;
2990 } END_FORALL_PVertex_in_ParamPolyhedron;
2992 if (!EP) {
2993 /* Search for vertex coordinate to split on */
2994 /* First look for one independent of the parameters */
2995 FORALL_PVertex_in_ParamPolyhedron(V, last, PP) {
2996 for (int i = 0; i < exist; ++i) {
2997 int j;
2998 for (j = 0; j < nparam; ++j)
2999 if (value_notzero_p(V->Vertex->p[i][j]))
3000 break;
3001 if (j < nparam)
3002 continue;
3003 value_set_si(M->p[0][0], 1);
3004 Vector_Set(M->p[0]+1, 0, nvar+exist);
3005 Vector_Copy(V->Vertex->p[i],
3006 M->p[0] + 1 + nvar + exist, nparam+1);
3007 value_oppose(M->p[0][1+nvar+i],
3008 V->Vertex->p[i][nparam+1]);
3010 Polyhedron *pos, *neg;
3011 value_set_si(M->p[0][0], 1);
3012 value_decrement(M->p[0][P->Dimension+1],
3013 M->p[0][P->Dimension+1]);
3014 neg = AddConstraints(M->p[0], 1, P, MaxRays);
3015 value_set_si(f, -1);
3016 Vector_Scale(M->p[0]+1, M->p[0]+1, f,
3017 P->Dimension+1);
3018 value_decrement(M->p[0][P->Dimension+1],
3019 M->p[0][P->Dimension+1]);
3020 value_decrement(M->p[0][P->Dimension+1],
3021 M->p[0][P->Dimension+1]);
3022 pos = AddConstraints(M->p[0], 1, P, MaxRays);
3023 if (emptyQ(neg) || emptyQ(pos)) {
3024 Polyhedron_Free(pos);
3025 Polyhedron_Free(neg);
3026 continue;
3028 Polyhedron_Free(pos);
3029 value_increment(M->p[0][P->Dimension+1],
3030 M->p[0][P->Dimension+1]);
3031 pos = AddConstraints(M->p[0], 1, P, MaxRays);
3032 #ifdef DEBUG_ER
3033 fprintf(stderr, "\nER: Vertex\n");
3034 #endif /* DEBUG_ER */
3035 pos->next = neg;
3036 EP = enumerate_or(pos, exist, nparam, MaxRays);
3037 break;
3039 if (EP)
3040 break;
3041 } END_FORALL_PVertex_in_ParamPolyhedron;
3044 if (!EP) {
3045 /* Search for vertex coordinate to split on */
3046 /* Now look for one that depends on the parameters */
3047 FORALL_PVertex_in_ParamPolyhedron(V, last, PP) {
3048 for (int i = 0; i < exist; ++i) {
3049 value_set_si(M->p[0][0], 1);
3050 Vector_Set(M->p[0]+1, 0, nvar+exist);
3051 Vector_Copy(V->Vertex->p[i],
3052 M->p[0] + 1 + nvar + exist, nparam+1);
3053 value_oppose(M->p[0][1+nvar+i],
3054 V->Vertex->p[i][nparam+1]);
3056 Polyhedron *pos, *neg;
3057 value_set_si(M->p[0][0], 1);
3058 value_decrement(M->p[0][P->Dimension+1],
3059 M->p[0][P->Dimension+1]);
3060 neg = AddConstraints(M->p[0], 1, P, MaxRays);
3061 value_set_si(f, -1);
3062 Vector_Scale(M->p[0]+1, M->p[0]+1, f,
3063 P->Dimension+1);
3064 value_decrement(M->p[0][P->Dimension+1],
3065 M->p[0][P->Dimension+1]);
3066 value_decrement(M->p[0][P->Dimension+1],
3067 M->p[0][P->Dimension+1]);
3068 pos = AddConstraints(M->p[0], 1, P, MaxRays);
3069 if (emptyQ(neg) || emptyQ(pos)) {
3070 Polyhedron_Free(pos);
3071 Polyhedron_Free(neg);
3072 continue;
3074 Polyhedron_Free(pos);
3075 value_increment(M->p[0][P->Dimension+1],
3076 M->p[0][P->Dimension+1]);
3077 pos = AddConstraints(M->p[0], 1, P, MaxRays);
3078 #ifdef DEBUG_ER
3079 fprintf(stderr, "\nER: ParamVertex\n");
3080 #endif /* DEBUG_ER */
3081 pos->next = neg;
3082 EP = enumerate_or(pos, exist, nparam, MaxRays);
3083 break;
3085 if (EP)
3086 break;
3087 } END_FORALL_PVertex_in_ParamPolyhedron;
3090 Matrix_Free(M);
3091 value_clear(f);
3094 if (CEq)
3095 Polyhedron_Free(CEq);
3096 if (CT)
3097 Matrix_Free(CT);
3098 if (PP)
3099 Param_Polyhedron_Free(PP);
3100 *PA = P;
3102 return EP;
3105 #ifndef HAVE_PIPLIB
3106 evalue *barvinok_enumerate_pip(Polyhedron *P,
3107 unsigned exist, unsigned nparam, unsigned MaxRays)
3109 return 0;
3111 #else
3112 evalue *barvinok_enumerate_pip(Polyhedron *P,
3113 unsigned exist, unsigned nparam, unsigned MaxRays)
3115 int nvar = P->Dimension - exist - nparam;
3116 evalue *EP = new_zero_ep();
3117 Polyhedron *Q, *N, *T = 0;
3118 Value min, tmp;
3119 value_init(min);
3120 value_init(tmp);
3122 #ifdef DEBUG_ER
3123 fprintf(stderr, "\nER: PIP\n");
3124 #endif /* DEBUG_ER */
3126 for (int i = 0; i < P->Dimension; ++i) {
3127 bool pos = false;
3128 bool neg = false;
3129 bool posray = false;
3130 bool negray = false;
3131 value_set_si(min, 0);
3132 for (int j = 0; j < P->NbRays; ++j) {
3133 if (value_pos_p(P->Ray[j][1+i])) {
3134 pos = true;
3135 if (value_zero_p(P->Ray[j][1+P->Dimension]))
3136 posray = true;
3137 } else if (value_neg_p(P->Ray[j][1+i])) {
3138 neg = true;
3139 if (value_zero_p(P->Ray[j][1+P->Dimension]))
3140 negray = true;
3141 else {
3142 mpz_fdiv_q(tmp,
3143 P->Ray[j][1+i], P->Ray[j][1+P->Dimension]);
3144 if (value_lt(tmp, min))
3145 value_assign(min, tmp);
3149 if (pos && neg) {
3150 assert(!(posray && negray));
3151 assert(!negray); // for now
3152 Polyhedron *O = T ? T : P;
3153 /* shift by a safe amount */
3154 Matrix *M = Matrix_Alloc(O->NbRays, O->Dimension+2);
3155 Vector_Copy(O->Ray[0], M->p[0], O->NbRays * (O->Dimension+2));
3156 for (int j = 0; j < P->NbRays; ++j) {
3157 if (value_notzero_p(M->p[j][1+P->Dimension])) {
3158 value_multiply(tmp, min, M->p[j][1+P->Dimension]);
3159 value_substract(M->p[j][1+i], M->p[j][1+i], tmp);
3162 if (T)
3163 Polyhedron_Free(T);
3164 T = Rays2Polyhedron(M, MaxRays);
3165 Matrix_Free(M);
3166 } else if (neg) {
3167 /* negating a parameter requires that we substitute in the
3168 * sign again afterwards.
3169 * Disallow for now.
3171 assert(i < nvar+exist);
3172 if (!T)
3173 T = Polyhedron_Copy(P);
3174 for (int j = 0; j < T->NbRays; ++j)
3175 value_oppose(T->Ray[j][1+i], T->Ray[j][1+i]);
3176 for (int j = 0; j < T->NbConstraints; ++j)
3177 value_oppose(T->Constraint[j][1+i], T->Constraint[j][1+i]);
3180 value_clear(min);
3181 value_clear(tmp);
3183 Polyhedron *D = pip_lexmin(T ? T : P, exist, nparam);
3184 for (Q = D; Q; Q = N) {
3185 N = Q->next;
3186 Q->next = 0;
3187 evalue *E;
3188 exist = Q->Dimension - nvar - nparam;
3189 E = barvinok_enumerate_e(Q, exist, nparam, MaxRays);
3190 Polyhedron_Free(Q);
3191 eadd(E, EP);
3192 free_evalue_refs(E);
3193 free(E);
3196 if (T)
3197 Polyhedron_Free(T);
3199 return EP;
3201 #endif
3204 static bool is_single(Value *row, int pos, int len)
3206 return First_Non_Zero(row, pos) == -1 &&
3207 First_Non_Zero(row+pos+1, len-pos-1) == -1;
3210 static evalue* barvinok_enumerate_e_r(Polyhedron *P,
3211 unsigned exist, unsigned nparam, unsigned MaxRays);
3213 #ifdef DEBUG_ER
3214 static int er_level = 0;
3216 evalue* barvinok_enumerate_e(Polyhedron *P,
3217 unsigned exist, unsigned nparam, unsigned MaxRays)
3219 fprintf(stderr, "\nER: level %i\n", er_level);
3220 int nvar = P->Dimension - exist - nparam;
3221 fprintf(stderr, "%d %d %d\n", nvar, exist, nparam);
3223 Polyhedron_Print(stderr, P_VALUE_FMT, P);
3224 ++er_level;
3225 P = DomainConstraintSimplify(Polyhedron_Copy(P), MaxRays);
3226 evalue *EP = barvinok_enumerate_e_r(P, exist, nparam, MaxRays);
3227 Polyhedron_Free(P);
3228 --er_level;
3229 return EP;
3231 #else
3232 evalue* barvinok_enumerate_e(Polyhedron *P,
3233 unsigned exist, unsigned nparam, unsigned MaxRays)
3235 P = DomainConstraintSimplify(Polyhedron_Copy(P), MaxRays);
3236 evalue *EP = barvinok_enumerate_e_r(P, exist, nparam, MaxRays);
3237 Polyhedron_Free(P);
3238 return EP;
3240 #endif
3242 static evalue* barvinok_enumerate_e_r(Polyhedron *P,
3243 unsigned exist, unsigned nparam, unsigned MaxRays)
3245 if (exist == 0) {
3246 Polyhedron *U = Universe_Polyhedron(nparam);
3247 evalue *EP = barvinok_enumerate_ev(P, U, MaxRays);
3248 //char *param_name[] = {"P", "Q", "R", "S", "T" };
3249 //print_evalue(stdout, EP, param_name);
3250 Polyhedron_Free(U);
3251 return EP;
3254 int nvar = P->Dimension - exist - nparam;
3255 int len = P->Dimension + 2;
3257 if (emptyQ(P))
3258 return new_zero_ep();
3260 if (nvar == 0 && nparam == 0) {
3261 evalue *EP = new_zero_ep();
3262 barvinok_count(P, &EP->x.n, MaxRays);
3263 if (value_pos_p(EP->x.n))
3264 value_set_si(EP->x.n, 1);
3265 return EP;
3268 int r;
3269 for (r = 0; r < P->NbRays; ++r)
3270 if (value_zero_p(P->Ray[r][0]) ||
3271 value_zero_p(P->Ray[r][P->Dimension+1])) {
3272 int i;
3273 for (i = 0; i < nvar; ++i)
3274 if (value_notzero_p(P->Ray[r][i+1]))
3275 break;
3276 if (i >= nvar)
3277 continue;
3278 for (i = nvar + exist; i < nvar + exist + nparam; ++i)
3279 if (value_notzero_p(P->Ray[r][i+1]))
3280 break;
3281 if (i >= nvar + exist + nparam)
3282 break;
3284 if (r < P->NbRays) {
3285 evalue *EP = new_zero_ep();
3286 value_set_si(EP->x.n, -1);
3287 return EP;
3290 int first;
3291 for (r = 0; r < P->NbEq; ++r)
3292 if ((first = First_Non_Zero(P->Constraint[r]+1+nvar, exist)) != -1)
3293 break;
3294 if (r < P->NbEq) {
3295 if (First_Non_Zero(P->Constraint[r]+1+nvar+first+1,
3296 exist-first-1) != -1) {
3297 Polyhedron *T = rotate_along(P, r, nvar, exist, MaxRays);
3298 #ifdef DEBUG_ER
3299 fprintf(stderr, "\nER: Equality\n");
3300 #endif /* DEBUG_ER */
3301 evalue *EP = barvinok_enumerate_e(T, exist-1, nparam, MaxRays);
3302 Polyhedron_Free(T);
3303 return EP;
3304 } else {
3305 #ifdef DEBUG_ER
3306 fprintf(stderr, "\nER: Fixed\n");
3307 #endif /* DEBUG_ER */
3308 if (first == 0)
3309 return barvinok_enumerate_e(P, exist-1, nparam, MaxRays);
3310 else {
3311 Polyhedron *T = Polyhedron_Copy(P);
3312 SwapColumns(T, nvar+1, nvar+1+first);
3313 evalue *EP = barvinok_enumerate_e(T, exist-1, nparam, MaxRays);
3314 Polyhedron_Free(T);
3315 return EP;
3320 Vector *row = Vector_Alloc(len);
3321 value_set_si(row->p[0], 1);
3323 Value f;
3324 value_init(f);
3326 enum constraint* info = new constraint[exist];
3327 for (int i = 0; i < exist; ++i) {
3328 info[i] = ALL_POS;
3329 for (int l = P->NbEq; l < P->NbConstraints; ++l) {
3330 if (value_negz_p(P->Constraint[l][nvar+i+1]))
3331 continue;
3332 bool l_parallel = is_single(P->Constraint[l]+nvar+1, i, exist);
3333 for (int u = P->NbEq; u < P->NbConstraints; ++u) {
3334 if (value_posz_p(P->Constraint[u][nvar+i+1]))
3335 continue;
3336 bool lu_parallel = l_parallel ||
3337 is_single(P->Constraint[u]+nvar+1, i, exist);
3338 value_oppose(f, P->Constraint[u][nvar+i+1]);
3339 Vector_Combine(P->Constraint[l]+1, P->Constraint[u]+1, row->p+1,
3340 f, P->Constraint[l][nvar+i+1], len-1);
3341 if (!(info[i] & INDEPENDENT)) {
3342 int j;
3343 for (j = 0; j < exist; ++j)
3344 if (j != i && value_notzero_p(row->p[nvar+j+1]))
3345 break;
3346 if (j == exist) {
3347 //printf("independent: i: %d, l: %d, u: %d\n", i, l, u);
3348 info[i] = (constraint)(info[i] | INDEPENDENT);
3351 if (info[i] & ALL_POS) {
3352 value_addto(row->p[len-1], row->p[len-1],
3353 P->Constraint[l][nvar+i+1]);
3354 value_addto(row->p[len-1], row->p[len-1], f);
3355 value_multiply(f, f, P->Constraint[l][nvar+i+1]);
3356 value_substract(row->p[len-1], row->p[len-1], f);
3357 value_decrement(row->p[len-1], row->p[len-1]);
3358 ConstraintSimplify(row->p, row->p, len, &f);
3359 value_set_si(f, -1);
3360 Vector_Scale(row->p+1, row->p+1, f, len-1);
3361 value_decrement(row->p[len-1], row->p[len-1]);
3362 Polyhedron *T = AddConstraints(row->p, 1, P, MaxRays);
3363 if (!emptyQ(T)) {
3364 //printf("not all_pos: i: %d, l: %d, u: %d\n", i, l, u);
3365 info[i] = (constraint)(info[i] ^ ALL_POS);
3367 //puts("pos remainder");
3368 //Polyhedron_Print(stdout, P_VALUE_FMT, T);
3369 Polyhedron_Free(T);
3371 if (!(info[i] & ONE_NEG)) {
3372 if (lu_parallel) {
3373 negative_test_constraint(P->Constraint[l],
3374 P->Constraint[u],
3375 row->p, nvar+i, len, &f);
3376 oppose_constraint(row->p, len, &f);
3377 Polyhedron *T = AddConstraints(row->p, 1, P, MaxRays);
3378 if (emptyQ(T)) {
3379 //printf("one_neg i: %d, l: %d, u: %d\n", i, l, u);
3380 info[i] = (constraint)(info[i] | ONE_NEG);
3382 //puts("neg remainder");
3383 //Polyhedron_Print(stdout, P_VALUE_FMT, T);
3384 Polyhedron_Free(T);
3385 } else if (!(info[i] & ROT_NEG)) {
3386 if (parallel_constraints(P->Constraint[l],
3387 P->Constraint[u],
3388 row->p, nvar, exist)) {
3389 negative_test_constraint7(P->Constraint[l],
3390 P->Constraint[u],
3391 row->p, nvar, exist,
3392 len, &f);
3393 oppose_constraint(row->p, len, &f);
3394 Polyhedron *T = AddConstraints(row->p, 1, P, MaxRays);
3395 if (emptyQ(T)) {
3396 // printf("rot_neg i: %d, l: %d, u: %d\n", i, l, u);
3397 info[i] = (constraint)(info[i] | ROT_NEG);
3398 r = l;
3400 //puts("neg remainder");
3401 //Polyhedron_Print(stdout, P_VALUE_FMT, T);
3402 Polyhedron_Free(T);
3406 if (!(info[i] & ALL_POS) && (info[i] & (ONE_NEG | ROT_NEG)))
3407 goto next;
3410 if (info[i] & ALL_POS)
3411 break;
3412 next:
3417 for (int i = 0; i < exist; ++i)
3418 printf("%i: %i\n", i, info[i]);
3420 for (int i = 0; i < exist; ++i)
3421 if (info[i] & ALL_POS) {
3422 #ifdef DEBUG_ER
3423 fprintf(stderr, "\nER: Positive\n");
3424 #endif /* DEBUG_ER */
3425 // Eliminate
3426 // Maybe we should chew off some of the fat here
3427 Matrix *M = Matrix_Alloc(P->Dimension, P->Dimension+1);
3428 for (int j = 0; j < P->Dimension; ++j)
3429 value_set_si(M->p[j][j + (j >= i+nvar)], 1);
3430 Polyhedron *T = Polyhedron_Image(P, M, MaxRays);
3431 Matrix_Free(M);
3432 evalue *EP = barvinok_enumerate_e(T, exist-1, nparam, MaxRays);
3433 Polyhedron_Free(T);
3434 value_clear(f);
3435 Vector_Free(row);
3436 delete [] info;
3437 return EP;
3439 for (int i = 0; i < exist; ++i)
3440 if (info[i] & ONE_NEG) {
3441 #ifdef DEBUG_ER
3442 fprintf(stderr, "\nER: Negative\n");
3443 #endif /* DEBUG_ER */
3444 Vector_Free(row);
3445 value_clear(f);
3446 delete [] info;
3447 if (i == 0)
3448 return barvinok_enumerate_e(P, exist-1, nparam, MaxRays);
3449 else {
3450 Polyhedron *T = Polyhedron_Copy(P);
3451 SwapColumns(T, nvar+1, nvar+1+i);
3452 evalue *EP = barvinok_enumerate_e(T, exist-1, nparam, MaxRays);
3453 Polyhedron_Free(T);
3454 return EP;
3457 for (int i = 0; i < exist; ++i)
3458 if (info[i] & ROT_NEG) {
3459 #ifdef DEBUG_ER
3460 fprintf(stderr, "\nER: Rotate\n");
3461 #endif /* DEBUG_ER */
3462 Vector_Free(row);
3463 value_clear(f);
3464 delete [] info;
3465 Polyhedron *T = rotate_along(P, r, nvar, exist, MaxRays);
3466 evalue *EP = barvinok_enumerate_e(T, exist-1, nparam, MaxRays);
3467 Polyhedron_Free(T);
3468 return EP;
3470 for (int i = 0; i < exist; ++i)
3471 if (info[i] & INDEPENDENT) {
3472 Polyhedron *pos, *neg;
3474 /* Find constraint again and split off negative part */
3476 if (SplitOnVar(P, i, nvar, len, exist, MaxRays,
3477 row, f, true, &pos, &neg)) {
3478 #ifdef DEBUG_ER
3479 fprintf(stderr, "\nER: Split\n");
3480 #endif /* DEBUG_ER */
3482 evalue *EP =
3483 barvinok_enumerate_e(neg, exist-1, nparam, MaxRays);
3484 evalue *E =
3485 barvinok_enumerate_e(pos, exist, nparam, MaxRays);
3486 eadd(E, EP);
3487 free_evalue_refs(E);
3488 free(E);
3489 Polyhedron_Free(neg);
3490 Polyhedron_Free(pos);
3491 value_clear(f);
3492 Vector_Free(row);
3493 delete [] info;
3494 return EP;
3497 delete [] info;
3499 Polyhedron *O = P;
3500 Polyhedron *F;
3502 evalue *EP;
3504 EP = enumerate_line(P, exist, nparam, MaxRays);
3505 if (EP)
3506 goto out;
3508 EP = barvinok_enumerate_pip(P, exist, nparam, MaxRays);
3509 if (EP)
3510 goto out;
3512 EP = enumerate_redundant_ray(P, exist, nparam, MaxRays);
3513 if (EP)
3514 goto out;
3516 EP = enumerate_sure(P, exist, nparam, MaxRays);
3517 if (EP)
3518 goto out;
3520 EP = enumerate_ray(P, exist, nparam, MaxRays);
3521 if (EP)
3522 goto out;
3524 EP = enumerate_sure2(P, exist, nparam, MaxRays);
3525 if (EP)
3526 goto out;
3528 F = unfringe(P, MaxRays);
3529 if (!PolyhedronIncludes(F, P)) {
3530 #ifdef DEBUG_ER
3531 fprintf(stderr, "\nER: Fringed\n");
3532 #endif /* DEBUG_ER */
3533 EP = barvinok_enumerate_e(F, exist, nparam, MaxRays);
3534 Polyhedron_Free(F);
3535 goto out;
3537 Polyhedron_Free(F);
3539 if (nparam)
3540 EP = enumerate_vd(&P, exist, nparam, MaxRays);
3541 if (EP)
3542 goto out2;
3544 if (nvar != 0) {
3545 EP = enumerate_sum(P, exist, nparam, MaxRays);
3546 goto out2;
3549 assert(nvar == 0);
3551 int i;
3552 Polyhedron *pos, *neg;
3553 for (i = 0; i < exist; ++i)
3554 if (SplitOnVar(P, i, nvar, len, exist, MaxRays,
3555 row, f, false, &pos, &neg))
3556 break;
3558 assert (i < exist);
3560 pos->next = neg;
3561 EP = enumerate_or(pos, exist, nparam, MaxRays);
3563 out2:
3564 if (O != P)
3565 Polyhedron_Free(P);
3567 out:
3568 value_clear(f);
3569 Vector_Free(row);
3570 return EP;
3573 gen_fun * barvinok_series(Polyhedron *P, Polyhedron* C, unsigned MaxRays)
3575 Polyhedron ** vcone;
3576 Polyhedron *CA;
3577 unsigned nparam = C->Dimension;
3578 unsigned dim, nvar;
3579 vec_ZZ sign;
3580 int ncone = 0;
3581 sign.SetLength(ncone);
3583 CA = align_context(C, P->Dimension, MaxRays);
3584 P = DomainIntersection(P, CA, MaxRays);
3585 Polyhedron_Free(CA);
3587 assert(!Polyhedron_is_infinite(P, nparam));
3588 assert(P->NbBid == 0);
3589 assert(Polyhedron_has_positive_rays(P, nparam));
3590 assert(P->NbEq == 0);
3592 dim = P->Dimension;
3593 nvar = dim - nparam;
3594 vcone = new Polyhedron_p[P->NbRays];
3596 for (int j = 0; j < P->NbRays; ++j) {
3597 if (!value_pos_p(P->Ray[j][dim+1]))
3598 continue;
3600 int npos, nneg;
3601 Polyhedron *C = supporting_cone(P, j);
3602 decompose(C, &vcone[j], &npos, &nneg, MaxRays);
3603 ncone += npos + nneg;
3604 sign.SetLength(ncone);
3605 for (int k = 0; k < npos; ++k)
3606 sign[ncone-nneg-k-1] = 1;
3607 for (int k = 0; k < nneg; ++k)
3608 sign[ncone-k-1] = -1;
3611 mat_ZZ rays;
3612 rays.SetDims(ncone * dim, nvar);
3613 int r = 0;
3614 for (int j = 0; j < P->NbRays; ++j) {
3615 if (!value_pos_p(P->Ray[j][dim+1]))
3616 continue;
3618 for (Polyhedron *i = vcone[j]; i; i = i->next) {
3619 add_rays(rays, i, &r, nvar);
3622 rays.SetDims(r, nvar);
3623 vec_ZZ lambda;
3624 nonorthog(rays, lambda);
3625 //randomvector(P, lambda, nvar);
3628 cout << "rays: " << rays;
3629 cout << "lambda: " << lambda;
3632 int f = 0;
3633 ZZ num_s;
3634 vec_ZZ num_p;
3635 num_p.SetLength(nparam);
3636 vec_ZZ vertex;
3637 vec_ZZ den_s;
3638 den_s.SetLength(dim);
3639 vec_ZZ den_p;
3640 den_p.SetLength(dim);
3641 mat_ZZ den;
3642 den.SetDims(dim, nparam);
3643 ZZ one;
3644 one = 1;
3645 mpq_t count;
3646 mpq_init(count);
3648 gen_fun * gf = new gen_fun;
3650 rays.SetDims(dim, nvar);
3652 for (int j = 0; j < P->NbRays; ++j) {
3653 if (!value_pos_p(P->Ray[j][dim+1]))
3654 continue;
3656 for (Polyhedron *i = vcone[j]; i; i = i->next, ++f) {
3657 lattice_point(P->Ray[j]+1, i, vertex);
3658 int k = 0;
3659 num_s = 0;
3660 for ( ; k < nvar; ++k)
3661 num_s += vertex[k] * lambda[k];
3662 for ( ; k < dim; ++k)
3663 num_p[k-nvar] = vertex[k];
3665 int r = 0;
3666 add_rays(rays, i, &r, nvar, true);
3667 for (r = 0; r < dim; ++r)
3668 values2zz(i->Ray[r]+1+nvar, den[r], nparam);
3669 den_s = rays * lambda;
3671 normalize(sign[f], num_s, num_p, den_s, den_p, den);
3673 int only_param = 0;
3674 int no_param = 0;
3675 for (int k = 0; k < dim; ++k) {
3676 if (den_p[k] == 0)
3677 ++no_param;
3678 else if (den_s[k] == 0)
3679 ++only_param;
3681 if (no_param == 0) {
3682 for (int k = 0; k < dim; ++k)
3683 if (den_p[k] == -1)
3684 den[k] = -den[k];
3685 gf->add(sign[f], one, num_p, den);
3686 } else if (no_param + only_param == dim) {
3687 int k, l;
3688 mat_ZZ pden;
3689 pden.SetDims(only_param, nparam);
3691 for (k = 0, l = 0; k < dim; ++k)
3692 if (den_p[k] != 0)
3693 pden[l++] = den[k];
3695 for (k = 0; k < dim; ++k)
3696 if (den_s[k] != 0)
3697 break;
3699 dpoly n(no_param, num_s);
3700 dpoly d(no_param, den_s[k], 1);
3701 for ( ; ++k < dim; k)
3702 if (den_s[k] != 0) {
3703 dpoly fact(no_param, den_s[k], 1);
3704 d *= fact;
3707 mpq_set_si(count, 0, 1);
3708 n.div(d, count, sign[f]);
3710 ZZ qn, qd;
3711 value2zz(mpq_numref(count), qn);
3712 value2zz(mpq_denref(count), qd);
3714 gf->add(qn, qd, num_p, pden);
3715 } else {
3716 int k, l;
3717 dpoly_r * r = 0;
3718 mat_ZZ pden;
3719 pden.SetDims(only_param, nparam);
3721 for (k = 0, l = 0; k < dim; ++k)
3722 if (den_s[k] == 0)
3723 pden[l++] = den[k];
3725 for (k = 0; k < dim; ++k)
3726 if (den_p[k] == 0)
3727 break;
3729 dpoly n(no_param, num_s);
3730 dpoly d(no_param, den_s[k], 1);
3731 for ( ; ++k < dim; )
3732 if (den_p[k] == 0) {
3733 dpoly fact(no_param, den_s[k], 1);
3734 d *= fact;
3737 for (k = 0; k < dim; ++k) {
3738 if (den_s[k] == 0 || den_p[k] == 0)
3739 continue;
3741 dpoly pd(no_param-1, den_s[k], 1);
3742 int s = den_p[k] < 0 ? -1 : 1;
3744 if (r == 0)
3745 r = new dpoly_r(n, pd, k, s, dim);
3746 else
3747 assert(0); // for now
3750 r->div(d, sign[f], gf, pden, den, num_p);
3754 cout << "sign: " << sign[f];
3755 cout << "num_s: " << num_s;
3756 cout << "num_p: " << num_p;
3757 cout << "den_s: " << den_s;
3758 cout << "den_p: " << den_p;
3759 cout << "den: " << den;
3760 cout << "only_param: " << only_param;
3761 cout << "no_param: " << no_param;
3762 cout << endl;
3768 mpq_clear(count);
3770 return gf;