no need for lambda anymore
[barvinok.git] / barvinok.cc
blob9b7470b5b00f3677753f37f91b35a4b3bfaed729
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 if (coeff == 0)
397 return;
398 for (int k = 0; k < c[i].size(); ++k) {
399 if (memcmp(c[i][k]->powers, powers, dim * sizeof(int)) == 0) {
400 c[i][k]->coeff += coeff;
401 return;
404 dpoly_r_term *t = new dpoly_r_term;
405 t->powers = new int[dim];
406 memcpy(t->powers, powers, dim * sizeof(int));
407 t->coeff = coeff;
408 c[i].push_back(t);
410 dpoly_r(int len, int dim) {
411 denom = 1;
412 this->len = len;
413 this->dim = dim;
414 c = new vector< dpoly_r_term * > [len];
416 dpoly_r(dpoly& num, dpoly& den, int pos, int sign, int dim) {
417 denom = 1;
418 len = num.coeff.length();
419 c = new vector< dpoly_r_term * > [len];
420 this->dim = dim;
421 int powers[dim];
423 for (int i = 0; i < len; ++i) {
424 ZZ coeff = num.coeff[i];
425 memset(powers, 0, dim * sizeof(int));
426 powers[pos] = sign;
428 add_term(i, powers, coeff);
430 for (int j = 1; j <= i; ++j) {
431 for (int k = 0; k < c[i-j].size(); ++k) {
432 memcpy(powers, c[i-j][k]->powers, dim*sizeof(int));
433 powers[pos] += sign;
434 coeff = -den.coeff[j-1] * c[i-j][k]->coeff;
435 add_term(i, powers, coeff);
439 //dump();
441 dpoly_r(dpoly_r* num, dpoly& den, int pos, int sign, int dim) {
442 denom = num->denom;
443 len = num->len;
444 c = new vector< dpoly_r_term * > [len];
445 this->dim = dim;
446 int powers[dim];
447 ZZ coeff;
449 for (int i = 0 ; i < len; ++i) {
450 for (int k = 0; k < num->c[i].size(); ++k) {
451 memcpy(powers, num->c[i][k]->powers, dim*sizeof(int));
452 powers[pos] += sign;
453 add_term(i, powers, num->c[i][k]->coeff);
456 for (int j = 1; j <= i; ++j) {
457 for (int k = 0; k < c[i-j].size(); ++k) {
458 memcpy(powers, c[i-j][k]->powers, dim*sizeof(int));
459 powers[pos] += sign;
460 coeff = -den.coeff[j-1] * c[i-j][k]->coeff;
461 add_term(i, powers, coeff);
466 ~dpoly_r() {
467 for (int i = 0 ; i < len; ++i)
468 for (int k = 0; k < c[i].size(); ++k) {
469 delete [] c[i][k]->powers;
470 delete c[i][k];
472 delete [] c;
474 dpoly_r *div(dpoly& d) {
475 dpoly_r *rc = new dpoly_r(len, dim);
476 rc->denom = power(d.coeff[0], len);
477 ZZ inv_d = rc->denom / d.coeff[0];
478 ZZ coeff;
480 for (int i = 0; i < len; ++i) {
481 for (int k = 0; k < c[i].size(); ++k) {
482 coeff = c[i][k]->coeff * inv_d;
483 rc->add_term(i, c[i][k]->powers, coeff);
486 for (int j = 1; j <= i; ++j) {
487 for (int k = 0; k < rc->c[i-j].size(); ++k) {
488 coeff = - d.coeff[j] * rc->c[i-j][k]->coeff / d.coeff[0];
489 rc->add_term(i, rc->c[i-j][k]->powers, coeff);
493 return rc;
495 void dump(void) {
496 for (int i = 0; i < len; ++i) {
497 cout << endl;
498 cout << i << endl;
499 cout << c[i].size() << endl;
500 for (int j = 0; j < c[i].size(); ++j) {
501 for (int k = 0; k < dim; ++k) {
502 cout << c[i][j]->powers[k] << " ";
504 cout << ": " << c[i][j]->coeff << "/" << denom << endl;
506 cout << endl;
511 struct decomposer {
512 void decompose(Polyhedron *C);
513 virtual void handle(Polyhedron *P, int sign) = 0;
516 struct polar_decomposer : public decomposer {
517 void decompose(Polyhedron *C, unsigned MaxRays);
518 virtual void handle(Polyhedron *P, int sign);
519 virtual void handle_polar(Polyhedron *P, int sign) = 0;
522 void decomposer::decompose(Polyhedron *C)
524 vector<cone *> nonuni;
525 cone * c = new cone(C);
526 ZZ det = c->det;
527 int s = sign(det);
528 assert(det != 0);
529 if (abs(det) > 1) {
530 nonuni.push_back(c);
531 } else {
532 handle(C, 1);
533 delete c;
535 vec_ZZ lambda;
536 while (!nonuni.empty()) {
537 c = nonuni.back();
538 nonuni.pop_back();
539 Vector* v = c->short_vector(lambda);
540 for (int i = 0; i < c->Rays->NbRows - 1; ++i) {
541 if (lambda[i] == 0)
542 continue;
543 Matrix* M = Matrix_Copy(c->Rays);
544 Vector_Copy(v->p, M->p[i], v->Size);
545 cone * pc = new cone(M);
546 assert (pc->det != 0);
547 if (abs(pc->det) > 1) {
548 assert(abs(pc->det) < abs(c->det));
549 nonuni.push_back(pc);
550 } else {
551 handle(pc->poly(), sign(pc->det) * s);
552 delete pc;
554 Matrix_Free(M);
556 Vector_Free(v);
557 delete c;
561 void polar_decomposer::decompose(Polyhedron *cone, unsigned MaxRays)
563 Polyhedron_Polarize(cone);
564 if (cone->NbRays - 1 != cone->Dimension) {
565 Polyhedron *tmp = cone;
566 cone = triangularize_cone(cone, MaxRays);
567 Polyhedron_Free(tmp);
569 for (Polyhedron *Polar = cone; Polar; Polar = Polar->next)
570 decomposer::decompose(Polar);
571 Domain_Free(cone);
574 void polar_decomposer::handle(Polyhedron *P, int sign)
576 Polyhedron_Polarize(P);
577 handle_polar(P, sign);
581 * Barvinok's Decomposition of a simplicial cone
583 * Returns two lists of polyhedra
585 void barvinok_decompose(Polyhedron *C, Polyhedron **ppos, Polyhedron **pneg)
587 Polyhedron *pos = *ppos, *neg = *pneg;
588 vector<cone *> nonuni;
589 cone * c = new cone(C);
590 ZZ det = c->det;
591 int s = sign(det);
592 assert(det != 0);
593 if (abs(det) > 1) {
594 nonuni.push_back(c);
595 } else {
596 Polyhedron *p = Polyhedron_Copy(c->Cone);
597 p->next = pos;
598 pos = p;
599 delete c;
601 vec_ZZ lambda;
602 while (!nonuni.empty()) {
603 c = nonuni.back();
604 nonuni.pop_back();
605 Vector* v = c->short_vector(lambda);
606 for (int i = 0; i < c->Rays->NbRows - 1; ++i) {
607 if (lambda[i] == 0)
608 continue;
609 Matrix* M = Matrix_Copy(c->Rays);
610 Vector_Copy(v->p, M->p[i], v->Size);
611 cone * pc = new cone(M);
612 assert (pc->det != 0);
613 if (abs(pc->det) > 1) {
614 assert(abs(pc->det) < abs(c->det));
615 nonuni.push_back(pc);
616 } else {
617 Polyhedron *p = pc->poly();
618 pc->Cone = 0;
619 if (sign(pc->det) == s) {
620 p->next = pos;
621 pos = p;
622 } else {
623 p->next = neg;
624 neg = p;
626 delete pc;
628 Matrix_Free(M);
630 Vector_Free(v);
631 delete c;
633 *ppos = pos;
634 *pneg = neg;
637 const int MAX_TRY=10;
639 * Searches for a vector that is not orthogonal to any
640 * of the rays in rays.
642 static void nonorthog(mat_ZZ& rays, vec_ZZ& lambda)
644 int dim = rays.NumCols();
645 bool found = false;
646 lambda.SetLength(dim);
647 if (dim == 0)
648 return;
650 for (int i = 2; !found && i <= 50*dim; i+=4) {
651 for (int j = 0; j < MAX_TRY; ++j) {
652 for (int k = 0; k < dim; ++k) {
653 int r = random_int(i)+2;
654 int v = (2*(r%2)-1) * (r >> 1);
655 lambda[k] = v;
657 int k = 0;
658 for (; k < rays.NumRows(); ++k)
659 if (lambda * rays[k] == 0)
660 break;
661 if (k == rays.NumRows()) {
662 found = true;
663 break;
667 assert(found);
670 static void randomvector(Polyhedron *P, vec_ZZ& lambda, int nvar)
672 Value tmp;
673 int max = 10 * 16;
674 unsigned int dim = P->Dimension;
675 value_init(tmp);
677 for (int i = 0; i < P->NbRays; ++i) {
678 for (int j = 1; j <= dim; ++j) {
679 value_absolute(tmp, P->Ray[i][j]);
680 int t = VALUE_TO_LONG(tmp) * 16;
681 if (t > max)
682 max = t;
685 for (int i = 0; i < P->NbConstraints; ++i) {
686 for (int j = 1; j <= dim; ++j) {
687 value_absolute(tmp, P->Constraint[i][j]);
688 int t = VALUE_TO_LONG(tmp) * 16;
689 if (t > max)
690 max = t;
693 value_clear(tmp);
695 lambda.SetLength(nvar);
696 for (int k = 0; k < nvar; ++k) {
697 int r = random_int(max*dim)+2;
698 int v = (2*(r%2)-1) * (max/2*dim + (r >> 1));
699 lambda[k] = v;
703 static void add_rays(mat_ZZ& rays, Polyhedron *i, int *r, int nvar = -1,
704 bool all = false)
706 unsigned dim = i->Dimension;
707 if (nvar == -1)
708 nvar = dim;
709 for (int k = 0; k < i->NbRays; ++k) {
710 if (!value_zero_p(i->Ray[k][dim+1]))
711 continue;
712 if (!all && nvar != dim && First_Non_Zero(i->Ray[k]+1, nvar) == -1)
713 continue;
714 values2zz(i->Ray[k]+1, rays[(*r)++], nvar);
718 void lattice_point(Value* values, Polyhedron *i, vec_ZZ& vertex)
720 unsigned dim = i->Dimension;
721 if(!value_one_p(values[dim])) {
722 Matrix* Rays = rays(i);
723 Matrix *inv = Matrix_Alloc(Rays->NbRows, Rays->NbColumns);
724 int ok = Matrix_Inverse(Rays, inv);
725 assert(ok);
726 Matrix_Free(Rays);
727 Rays = rays(i);
728 Vector *lambda = Vector_Alloc(dim+1);
729 Vector_Matrix_Product(values, inv, lambda->p);
730 Matrix_Free(inv);
731 for (int j = 0; j < dim; ++j)
732 mpz_cdiv_q(lambda->p[j], lambda->p[j], lambda->p[dim]);
733 value_set_si(lambda->p[dim], 1);
734 Vector *A = Vector_Alloc(dim+1);
735 Vector_Matrix_Product(lambda->p, Rays, A->p);
736 Vector_Free(lambda);
737 Matrix_Free(Rays);
738 values2zz(A->p, vertex, dim);
739 Vector_Free(A);
740 } else
741 values2zz(values, vertex, dim);
744 static evalue *term(int param, ZZ& c, Value *den = NULL)
746 evalue *EP = new evalue();
747 value_init(EP->d);
748 value_set_si(EP->d,0);
749 EP->x.p = new_enode(polynomial, 2, param + 1);
750 evalue_set_si(&EP->x.p->arr[0], 0, 1);
751 value_init(EP->x.p->arr[1].x.n);
752 if (den == NULL)
753 value_set_si(EP->x.p->arr[1].d, 1);
754 else
755 value_assign(EP->x.p->arr[1].d, *den);
756 zz2value(c, EP->x.p->arr[1].x.n);
757 return EP;
760 static void vertex_period(
761 Polyhedron *i, vec_ZZ& lambda, Matrix *T,
762 Value lcm, int p, Vector *val,
763 evalue *E, evalue* ev,
764 ZZ& offset)
766 unsigned nparam = T->NbRows - 1;
767 unsigned dim = i->Dimension;
768 Value tmp;
769 ZZ nump;
771 if (p == nparam) {
772 vec_ZZ vertex;
773 ZZ num, l;
774 Vector * values = Vector_Alloc(dim + 1);
775 Vector_Matrix_Product(val->p, T, values->p);
776 value_assign(values->p[dim], lcm);
777 lattice_point(values->p, i, vertex);
778 num = vertex * lambda;
779 value2zz(lcm, l);
780 num *= l;
781 num += offset;
782 value_init(ev->x.n);
783 zz2value(num, ev->x.n);
784 value_assign(ev->d, lcm);
785 Vector_Free(values);
786 return;
789 value_init(tmp);
790 vec_ZZ vertex;
791 values2zz(T->p[p], vertex, dim);
792 nump = vertex * lambda;
793 if (First_Non_Zero(val->p, p) == -1) {
794 value_assign(tmp, lcm);
795 evalue *ET = term(p, nump, &tmp);
796 eadd(ET, E);
797 free_evalue_refs(ET);
798 delete ET;
801 value_assign(tmp, lcm);
802 if (First_Non_Zero(T->p[p], dim) != -1)
803 Vector_Gcd(T->p[p], dim, &tmp);
804 Gcd(tmp, lcm, &tmp);
805 if (value_lt(tmp, lcm)) {
806 ZZ count;
808 value_division(tmp, lcm, tmp);
809 value_set_si(ev->d, 0);
810 ev->x.p = new_enode(periodic, VALUE_TO_INT(tmp), p+1);
811 value2zz(tmp, count);
812 do {
813 value_decrement(tmp, tmp);
814 --count;
815 ZZ new_offset = offset - count * nump;
816 value_assign(val->p[p], tmp);
817 vertex_period(i, lambda, T, lcm, p+1, val, E,
818 &ev->x.p->arr[VALUE_TO_INT(tmp)], new_offset);
819 } while (value_pos_p(tmp));
820 } else
821 vertex_period(i, lambda, T, lcm, p+1, val, E, ev, offset);
822 value_clear(tmp);
825 static void mask_r(Matrix *f, int nr, Vector *lcm, int p, Vector *val, evalue *ev)
827 unsigned nparam = lcm->Size;
829 if (p == nparam) {
830 Vector * prod = Vector_Alloc(f->NbRows);
831 Matrix_Vector_Product(f, val->p, prod->p);
832 int isint = 1;
833 for (int i = 0; i < nr; ++i) {
834 value_modulus(prod->p[i], prod->p[i], f->p[i][nparam+1]);
835 isint &= value_zero_p(prod->p[i]);
837 value_set_si(ev->d, 1);
838 value_init(ev->x.n);
839 value_set_si(ev->x.n, isint);
840 Vector_Free(prod);
841 return;
844 Value tmp;
845 value_init(tmp);
846 if (value_one_p(lcm->p[p]))
847 mask_r(f, nr, lcm, p+1, val, ev);
848 else {
849 value_assign(tmp, lcm->p[p]);
850 value_set_si(ev->d, 0);
851 ev->x.p = new_enode(periodic, VALUE_TO_INT(tmp), p+1);
852 do {
853 value_decrement(tmp, tmp);
854 value_assign(val->p[p], tmp);
855 mask_r(f, nr, lcm, p+1, val, &ev->x.p->arr[VALUE_TO_INT(tmp)]);
856 } while (value_pos_p(tmp));
858 value_clear(tmp);
861 static evalue *multi_monom(vec_ZZ& p)
863 evalue *X = new evalue();
864 value_init(X->d);
865 value_init(X->x.n);
866 unsigned nparam = p.length()-1;
867 zz2value(p[nparam], X->x.n);
868 value_set_si(X->d, 1);
869 for (int i = 0; i < nparam; ++i) {
870 if (p[i] == 0)
871 continue;
872 evalue *T = term(i, p[i]);
873 eadd(T, X);
874 free_evalue_refs(T);
875 delete T;
877 return X;
881 * Check whether mapping polyhedron P on the affine combination
882 * num yields a range that has a fixed quotient on integer
883 * division by d
884 * If zero is true, then we are only interested in the quotient
885 * for the cases where the remainder is zero.
886 * Returns NULL if false and a newly allocated value if true.
888 static Value *fixed_quotient(Polyhedron *P, vec_ZZ& num, Value d, bool zero)
890 Value* ret = NULL;
891 int len = num.length();
892 Matrix *T = Matrix_Alloc(2, len);
893 zz2values(num, T->p[0]);
894 value_set_si(T->p[1][len-1], 1);
895 Polyhedron *I = Polyhedron_Image(P, T, P->NbConstraints);
896 Matrix_Free(T);
898 int i;
899 for (i = 0; i < I->NbRays; ++i)
900 if (value_zero_p(I->Ray[i][2])) {
901 Polyhedron_Free(I);
902 return NULL;
905 Value min, max;
906 value_init(min);
907 value_init(max);
908 int bounded = line_minmax(I, &min, &max);
909 assert(bounded);
911 if (zero)
912 mpz_cdiv_q(min, min, d);
913 else
914 mpz_fdiv_q(min, min, d);
915 mpz_fdiv_q(max, max, d);
917 if (value_eq(min, max)) {
918 ALLOC(Value, ret);
919 value_init(*ret);
920 value_assign(*ret, min);
922 value_clear(min);
923 value_clear(max);
924 return ret;
928 * Normalize linear expression coef modulo m
929 * Removes common factor and reduces coefficients
930 * Returns index of first non-zero coefficient or len
932 static int normal_mod(Value *coef, int len, Value *m)
934 Value gcd;
935 value_init(gcd);
937 Vector_Gcd(coef, len, &gcd);
938 Gcd(gcd, *m, &gcd);
939 Vector_AntiScale(coef, coef, gcd, len);
941 value_division(*m, *m, gcd);
942 value_clear(gcd);
944 if (value_one_p(*m))
945 return len;
947 int j;
948 for (j = 0; j < len; ++j)
949 mpz_fdiv_r(coef[j], coef[j], *m);
950 for (j = 0; j < len; ++j)
951 if (value_notzero_p(coef[j]))
952 break;
954 return j;
957 #ifdef USE_MODULO
958 static void mask(Matrix *f, evalue *factor)
960 int nr = f->NbRows, nc = f->NbColumns;
961 int n;
962 bool found = false;
963 for (n = 0; n < nr && value_notzero_p(f->p[n][nc-1]); ++n)
964 if (value_notone_p(f->p[n][nc-1]) &&
965 value_notmone_p(f->p[n][nc-1]))
966 found = true;
967 if (!found)
968 return;
970 evalue EP;
971 nr = n;
973 Value m;
974 value_init(m);
976 evalue EV;
977 value_init(EV.d);
978 value_init(EV.x.n);
979 value_set_si(EV.x.n, 1);
981 for (n = 0; n < nr; ++n) {
982 value_assign(m, f->p[n][nc-1]);
983 if (value_one_p(m) || value_mone_p(m))
984 continue;
986 int j = normal_mod(f->p[n], nc-1, &m);
987 if (j == nc-1) {
988 free_evalue_refs(factor);
989 value_init(factor->d);
990 evalue_set_si(factor, 0, 1);
991 break;
993 vec_ZZ row;
994 values2zz(f->p[n], row, nc-1);
995 ZZ g;
996 value2zz(m, g);
997 if (j < (nc-1)-1 && row[j] > g/2) {
998 for (int k = j; k < (nc-1); ++k)
999 if (row[k] != 0)
1000 row[k] = g - row[k];
1003 value_init(EP.d);
1004 value_set_si(EP.d, 0);
1005 EP.x.p = new_enode(relation, 2, 0);
1006 value_clear(EP.x.p->arr[1].d);
1007 EP.x.p->arr[1] = *factor;
1008 evalue *ev = &EP.x.p->arr[0];
1009 value_set_si(ev->d, 0);
1010 ev->x.p = new_enode(fractional, 3, -1);
1011 evalue_set_si(&ev->x.p->arr[1], 0, 1);
1012 evalue_set_si(&ev->x.p->arr[2], 1, 1);
1013 evalue *E = multi_monom(row);
1014 value_assign(EV.d, m);
1015 emul(&EV, E);
1016 value_clear(ev->x.p->arr[0].d);
1017 ev->x.p->arr[0] = *E;
1018 delete E;
1019 *factor = EP;
1022 value_clear(m);
1023 free_evalue_refs(&EV);
1025 #else
1029 static void mask(Matrix *f, evalue *factor)
1031 int nr = f->NbRows, nc = f->NbColumns;
1032 int n;
1033 bool found = false;
1034 for (n = 0; n < nr && value_notzero_p(f->p[n][nc-1]); ++n)
1035 if (value_notone_p(f->p[n][nc-1]) &&
1036 value_notmone_p(f->p[n][nc-1]))
1037 found = true;
1038 if (!found)
1039 return;
1041 Value tmp;
1042 value_init(tmp);
1043 nr = n;
1044 unsigned np = nc - 2;
1045 Vector *lcm = Vector_Alloc(np);
1046 Vector *val = Vector_Alloc(nc);
1047 Vector_Set(val->p, 0, nc);
1048 value_set_si(val->p[np], 1);
1049 Vector_Set(lcm->p, 1, np);
1050 for (n = 0; n < nr; ++n) {
1051 if (value_one_p(f->p[n][nc-1]) ||
1052 value_mone_p(f->p[n][nc-1]))
1053 continue;
1054 for (int j = 0; j < np; ++j)
1055 if (value_notzero_p(f->p[n][j])) {
1056 Gcd(f->p[n][j], f->p[n][nc-1], &tmp);
1057 value_division(tmp, f->p[n][nc-1], tmp);
1058 value_lcm(tmp, lcm->p[j], &lcm->p[j]);
1061 evalue EP;
1062 value_init(EP.d);
1063 mask_r(f, nr, lcm, 0, val, &EP);
1064 value_clear(tmp);
1065 Vector_Free(val);
1066 Vector_Free(lcm);
1067 emul(&EP,factor);
1068 free_evalue_refs(&EP);
1070 #endif
1072 struct term_info {
1073 evalue *E;
1074 ZZ constant;
1075 ZZ coeff;
1076 int pos;
1079 static bool mod_needed(Polyhedron *PD, vec_ZZ& num, Value d, evalue *E)
1081 Value *q = fixed_quotient(PD, num, d, false);
1083 if (!q)
1084 return true;
1086 value_oppose(*q, *q);
1087 evalue EV;
1088 value_init(EV.d);
1089 value_set_si(EV.d, 1);
1090 value_init(EV.x.n);
1091 value_multiply(EV.x.n, *q, d);
1092 eadd(&EV, E);
1093 free_evalue_refs(&EV);
1094 value_clear(*q);
1095 free(q);
1096 return false;
1099 static void ceil_mod(Value *coef, int len, Value d, ZZ& f, evalue *EP, Polyhedron *PD)
1101 Value m;
1102 value_init(m);
1103 value_set_si(m, -1);
1105 Vector_Scale(coef, coef, m, len);
1107 value_assign(m, d);
1108 int j = normal_mod(coef, len, &m);
1110 if (j == len) {
1111 value_clear(m);
1112 return;
1115 vec_ZZ num;
1116 values2zz(coef, num, len);
1118 ZZ g;
1119 value2zz(m, g);
1121 evalue tmp;
1122 value_init(tmp.d);
1123 evalue_set_si(&tmp, 0, 1);
1125 int p = j;
1126 if (g % 2 == 0)
1127 while (j < len-1 && (num[j] == g/2 || num[j] == 0))
1128 ++j;
1129 if ((j < len-1 && num[j] > g/2) || (j == len-1 && num[j] >= (g+1)/2)) {
1130 for (int k = j; k < len-1; ++k)
1131 if (num[k] != 0)
1132 num[k] = g - num[k];
1133 num[len-1] = g - 1 - num[len-1];
1134 value_assign(tmp.d, m);
1135 ZZ t = f*(g-1);
1136 zz2value(t, tmp.x.n);
1137 eadd(&tmp, EP);
1138 f = -f;
1141 if (p >= len-1) {
1142 ZZ t = num[len-1] * f;
1143 zz2value(t, tmp.x.n);
1144 value_assign(tmp.d, m);
1145 eadd(&tmp, EP);
1146 } else {
1147 evalue *E = multi_monom(num);
1148 evalue EV;
1149 value_init(EV.d);
1151 if (PD && !mod_needed(PD, num, m, E)) {
1152 value_init(EV.x.n);
1153 zz2value(f, EV.x.n);
1154 value_assign(EV.d, m);
1155 emul(&EV, E);
1156 eadd(E, EP);
1157 } else {
1158 value_init(EV.x.n);
1159 value_set_si(EV.x.n, 1);
1160 value_assign(EV.d, m);
1161 emul(&EV, E);
1162 value_clear(EV.x.n);
1163 value_set_si(EV.d, 0);
1164 EV.x.p = new_enode(fractional, 3, -1);
1165 evalue_copy(&EV.x.p->arr[0], E);
1166 evalue_set_si(&EV.x.p->arr[1], 0, 1);
1167 value_init(EV.x.p->arr[2].x.n);
1168 zz2value(f, EV.x.p->arr[2].x.n);
1169 value_set_si(EV.x.p->arr[2].d, 1);
1171 eadd(&EV, EP);
1174 free_evalue_refs(&EV);
1175 free_evalue_refs(E);
1176 delete E;
1179 free_evalue_refs(&tmp);
1181 out:
1182 value_clear(m);
1185 evalue* bv_ceil3(Value *coef, int len, Value d, Polyhedron *P)
1187 Vector *val = Vector_Alloc(len);
1189 Value t;
1190 value_init(t);
1191 value_set_si(t, -1);
1192 Vector_Scale(coef, val->p, t, len);
1193 value_absolute(t, d);
1195 vec_ZZ num;
1196 values2zz(val->p, num, len);
1197 evalue *EP = multi_monom(num);
1199 evalue tmp;
1200 value_init(tmp.d);
1201 value_init(tmp.x.n);
1202 value_set_si(tmp.x.n, 1);
1203 value_assign(tmp.d, t);
1205 emul(&tmp, EP);
1207 ZZ one;
1208 one = 1;
1209 ceil_mod(val->p, len, t, one, EP, P);
1210 value_clear(t);
1212 /* copy EP to malloc'ed evalue */
1213 evalue *E;
1214 ALLOC(evalue, E);
1215 *E = *EP;
1216 delete EP;
1218 free_evalue_refs(&tmp);
1219 Vector_Free(val);
1221 return E;
1224 #ifdef USE_MODULO
1225 evalue* lattice_point(
1226 Polyhedron *i, vec_ZZ& lambda, Matrix *W, Value lcm, Polyhedron *PD)
1228 unsigned nparam = W->NbColumns - 1;
1230 Matrix* Rays = rays2(i);
1231 Matrix *T = Transpose(Rays);
1232 Matrix *T2 = Matrix_Copy(T);
1233 Matrix *inv = Matrix_Alloc(T2->NbRows, T2->NbColumns);
1234 int ok = Matrix_Inverse(T2, inv);
1235 assert(ok);
1236 Matrix_Free(Rays);
1237 Matrix_Free(T2);
1238 mat_ZZ vertex;
1239 matrix2zz(W, vertex, W->NbRows, W->NbColumns);
1241 vec_ZZ num;
1242 num = lambda * vertex;
1244 evalue *EP = multi_monom(num);
1246 evalue tmp;
1247 value_init(tmp.d);
1248 value_init(tmp.x.n);
1249 value_set_si(tmp.x.n, 1);
1250 value_assign(tmp.d, lcm);
1252 emul(&tmp, EP);
1254 Matrix *L = Matrix_Alloc(inv->NbRows, W->NbColumns);
1255 Matrix_Product(inv, W, L);
1257 mat_ZZ RT;
1258 matrix2zz(T, RT, T->NbRows, T->NbColumns);
1259 Matrix_Free(T);
1261 vec_ZZ p = lambda * RT;
1263 for (int i = 0; i < L->NbRows; ++i) {
1264 ceil_mod(L->p[i], nparam+1, lcm, p[i], EP, PD);
1267 Matrix_Free(L);
1269 Matrix_Free(inv);
1270 free_evalue_refs(&tmp);
1271 return EP;
1273 #else
1274 evalue* lattice_point(
1275 Polyhedron *i, vec_ZZ& lambda, Matrix *W, Value lcm, Polyhedron *PD)
1277 Matrix *T = Transpose(W);
1278 unsigned nparam = T->NbRows - 1;
1280 evalue *EP = new evalue();
1281 value_init(EP->d);
1282 evalue_set_si(EP, 0, 1);
1284 evalue ev;
1285 Vector *val = Vector_Alloc(nparam+1);
1286 value_set_si(val->p[nparam], 1);
1287 ZZ offset(INIT_VAL, 0);
1288 value_init(ev.d);
1289 vertex_period(i, lambda, T, lcm, 0, val, EP, &ev, offset);
1290 Vector_Free(val);
1291 eadd(&ev, EP);
1292 free_evalue_refs(&ev);
1294 Matrix_Free(T);
1296 reduce_evalue(EP);
1298 return EP;
1300 #endif
1302 void lattice_point(
1303 Param_Vertices* V, Polyhedron *i, vec_ZZ& lambda, term_info* term,
1304 Polyhedron *PD)
1306 unsigned nparam = V->Vertex->NbColumns - 2;
1307 unsigned dim = i->Dimension;
1308 mat_ZZ vertex;
1309 vertex.SetDims(V->Vertex->NbRows, nparam+1);
1310 Value lcm, tmp;
1311 value_init(lcm);
1312 value_init(tmp);
1313 value_set_si(lcm, 1);
1314 for (int j = 0; j < V->Vertex->NbRows; ++j) {
1315 value_lcm(lcm, V->Vertex->p[j][nparam+1], &lcm);
1317 if (value_notone_p(lcm)) {
1318 Matrix * mv = Matrix_Alloc(dim, nparam+1);
1319 for (int j = 0 ; j < dim; ++j) {
1320 value_division(tmp, lcm, V->Vertex->p[j][nparam+1]);
1321 Vector_Scale(V->Vertex->p[j], mv->p[j], tmp, nparam+1);
1324 term->E = lattice_point(i, lambda, mv, lcm, PD);
1325 term->constant = 0;
1327 Matrix_Free(mv);
1328 value_clear(lcm);
1329 value_clear(tmp);
1330 return;
1332 for (int i = 0; i < V->Vertex->NbRows; ++i) {
1333 assert(value_one_p(V->Vertex->p[i][nparam+1])); // for now
1334 values2zz(V->Vertex->p[i], vertex[i], nparam+1);
1337 vec_ZZ num;
1338 num = lambda * vertex;
1340 int p = -1;
1341 int nn = 0;
1342 for (int j = 0; j < nparam; ++j)
1343 if (num[j] != 0) {
1344 ++nn;
1345 p = j;
1347 if (nn >= 2) {
1348 term->E = multi_monom(num);
1349 term->constant = 0;
1350 } else {
1351 term->E = NULL;
1352 term->constant = num[nparam];
1353 term->pos = p;
1354 if (p != -1)
1355 term->coeff = num[p];
1358 value_clear(lcm);
1359 value_clear(tmp);
1362 static void normalize(ZZ& sign, ZZ& num, vec_ZZ& den)
1364 unsigned dim = den.length();
1366 int change = 0;
1368 for (int j = 0; j < den.length(); ++j) {
1369 if (den[j] > 0)
1370 change ^= 1;
1371 else {
1372 den[j] = abs(den[j]);
1373 num += den[j];
1376 if (change)
1377 sign = -sign;
1380 /* input:
1381 * f: the powers in the denominator for the remaining vars
1382 * each row refers to a factor
1383 * den_s: for each factor, the power of (s+1)
1384 * sign
1385 * num_s: powers in the numerator corresponding to the summed vars
1386 * num_p: powers in the numerator corresponidng to the remaining vars
1387 * number of rays in cone: "dim" = "k"
1388 * length of each ray: "dim" = "d"
1389 * for now, it is assume: k == d
1390 * output:
1391 * den_p: for each factor
1392 * 0: independent of remaining vars
1393 * 1: power corresponds to corresponding row in f
1394 * -1: power is inverse of corresponding row in f
1396 static void normalize(ZZ& sign,
1397 ZZ& num_s, vec_ZZ& num_p, vec_ZZ& den_s, vec_ZZ& den_p,
1398 mat_ZZ& f)
1400 unsigned dim = f.NumRows();
1401 unsigned nparam = num_p.length();
1402 unsigned nvar = dim - nparam;
1404 int change = 0;
1406 for (int j = 0; j < den_s.length(); ++j) {
1407 if (den_s[j] == 0) {
1408 den_p[j] = 1;
1409 continue;
1411 int k;
1412 for (k = 0; k < nparam; ++k)
1413 if (f[j][k] != 0)
1414 break;
1415 if (k < nparam) {
1416 if (den_s[j] > 0) {
1417 den_p[j] = -1;
1418 num_p -= f[j];
1419 } else
1420 den_p[j] = 1;
1421 } else
1422 den_p[j] = 0;
1423 if (den_s[j] > 0)
1424 change ^= 1;
1425 else {
1426 den_s[j] = abs(den_s[j]);
1427 num_s += den_s[j];
1431 if (change)
1432 sign = -sign;
1435 struct counter : public polar_decomposer {
1436 vec_ZZ lambda;
1437 mat_ZZ rays;
1438 vec_ZZ vertex;
1439 vec_ZZ den;
1440 ZZ sign;
1441 ZZ num;
1442 int j;
1443 Polyhedron *P;
1444 unsigned dim;
1445 mpq_t count;
1447 counter(Polyhedron *P) {
1448 this->P = P;
1449 dim = P->Dimension;
1450 randomvector(P, lambda, dim);
1451 rays.SetDims(dim, dim);
1452 den.SetLength(dim);
1453 mpq_init(count);
1456 void start(unsigned MaxRays);
1458 ~counter() {
1459 mpq_clear(count);
1462 virtual void handle_polar(Polyhedron *P, int sign);
1465 void counter::handle_polar(Polyhedron *C, int s)
1467 int r = 0;
1468 assert(C->NbRays-1 == dim);
1469 add_rays(rays, C, &r);
1470 for (int k = 0; k < dim; ++k) {
1471 assert(lambda * rays[k] != 0);
1474 sign = s;
1476 lattice_point(P->Ray[j]+1, C, vertex);
1477 num = vertex * lambda;
1478 den = rays * lambda;
1479 normalize(sign, num, den);
1481 dpoly d(dim, num);
1482 dpoly n(dim, den[0], 1);
1483 for (int k = 1; k < dim; ++k) {
1484 dpoly fact(dim, den[k], 1);
1485 n *= fact;
1487 d.div(n, count, sign);
1490 void counter::start(unsigned MaxRays)
1492 for (j = 0; j < P->NbRays; ++j) {
1493 Polyhedron *C = supporting_cone(P, j);
1494 decompose(C, MaxRays);
1498 struct reducer : public polar_decomposer {
1499 vec_ZZ vertex;
1500 //vec_ZZ den;
1501 ZZ sgn;
1502 ZZ num;
1503 ZZ one;
1504 int j;
1505 Polyhedron *P;
1506 unsigned dim;
1507 mpq_t tcount;
1508 mpz_t tn;
1509 mpz_t td;
1510 int lower; // call base when only this many variables is left
1511 int untouched; // keep this many variables untouched
1513 reducer(Polyhedron *P) {
1514 this->P = P;
1515 dim = P->Dimension;
1516 //den.SetLength(dim);
1517 mpq_init(tcount);
1518 mpz_init(tn);
1519 mpz_init(td);
1520 one = 1;
1523 void start(unsigned MaxRays);
1525 ~reducer() {
1526 mpq_clear(tcount);
1527 mpz_clear(tn);
1528 mpz_clear(td);
1531 virtual void handle_polar(Polyhedron *P, int sign);
1532 void reduce(ZZ c, ZZ cd, vec_ZZ& num, mat_ZZ& den_f);
1533 virtual void base(ZZ& c, ZZ& cd, vec_ZZ& num, mat_ZZ& den_f) = 0;
1536 void reducer::reduce(ZZ c, ZZ cd, vec_ZZ& num, mat_ZZ& den_f)
1538 unsigned len = den_f.NumRows(); // number of factors in den
1539 unsigned d = num.length()-1;
1541 if (d+1 == lower) {
1542 base(c, cd, num, den_f);
1543 return;
1545 assert(num.length() > 1);
1547 vec_ZZ den_s;
1548 den_s.SetLength(len);
1549 mat_ZZ den_r;
1550 den_r.SetDims(len, d);
1552 /* Since we're working incrementally, we can look
1553 * for the "easiest" parameter first.
1554 * In particular we first handle the parameters such
1555 * that no_param + only_param == len, since that allows
1556 * us to decouple the problem and the split off part
1557 * may very well be zero
1559 int i = 0;
1560 int r, k;
1561 for (i = 0; i < d+1-untouched; ++i) {
1562 for (r = 0; r < len; ++r) {
1563 if (den_f[r][i] != 0) {
1564 for (k = 0; k <= d; ++k)
1565 if (i != k && den_f[r][k] != 0)
1566 break;
1567 if (k <= d)
1568 break;
1571 if (r >= len)
1572 break;
1574 if (i > d-untouched)
1575 i = 0;
1577 for (r = 0; r < len; ++r) {
1578 den_s[r] = den_f[r][i];
1579 for (k = 0; k <= d; ++k)
1580 if (k != i)
1581 den_r[r][k-(k>i)] = den_f[r][k];
1584 ZZ num_s = num[i];
1585 vec_ZZ num_p;
1586 num_p.SetLength(d);
1587 for (k = 0 ; k <= d; ++k)
1588 if (k != i)
1589 num_p[k-(k>i)] = num[k];
1591 vec_ZZ den_p;
1592 den_p.SetLength(len);
1594 normalize(c, num_s, num_p, den_s, den_p, den_r);
1596 int only_param = 0;
1597 int no_param = 0;
1598 for (int k = 0; k < len; ++k) {
1599 if (den_p[k] == 0)
1600 ++no_param;
1601 else if (den_s[k] == 0)
1602 ++only_param;
1604 if (no_param == 0) {
1605 for (int k = 0; k < len; ++k)
1606 if (den_p[k] == -1)
1607 den_r[k] = -den_r[k];
1608 reduce(c, cd, num_p, den_r);
1609 } else {
1610 int k, l;
1611 mat_ZZ pden;
1612 pden.SetDims(only_param, d);
1614 for (k = 0, l = 0; k < len; ++k)
1615 if (den_s[k] == 0)
1616 pden[l++] = den_r[k];
1618 for (k = 0; k < len; ++k)
1619 if (den_p[k] == 0)
1620 break;
1622 dpoly n(no_param, num_s);
1623 dpoly D(no_param, den_s[k], 1);
1624 for ( ; ++k < len; )
1625 if (den_p[k] == 0) {
1626 dpoly fact(no_param, den_s[k], 1);
1627 D *= fact;
1630 if (no_param + only_param == len) {
1631 mpq_set_si(tcount, 0, 1);
1632 n.div(D, tcount, one);
1634 ZZ qn, qd;
1635 value2zz(mpq_numref(tcount), qn);
1636 value2zz(mpq_denref(tcount), qd);
1638 qn *= c;
1639 qd *= cd;
1641 if (qn != 0)
1642 reduce(qn, qd, num_p, pden);
1643 } else {
1644 dpoly_r * r = 0;
1646 for (k = 0; k < len; ++k) {
1647 if (den_s[k] == 0 || den_p[k] == 0)
1648 continue;
1650 dpoly pd(no_param-1, den_s[k], 1);
1651 int s = den_p[k] < 0 ? -1 : 1;
1653 if (r == 0)
1654 r = new dpoly_r(n, pd, k, s, len);
1655 else {
1656 dpoly_r *nr = new dpoly_r(r, pd, k, s, len);
1657 delete r;
1658 r = nr;
1662 dpoly_r *rc = r->div(D);
1664 rc->denom *= cd;
1666 int common = pden.NumRows();
1667 vector< dpoly_r_term * >& final = rc->c[rc->len-1];
1668 int rows;
1669 for (int j = 0; j < final.size(); ++j) {
1670 if (final[j]->coeff == 0)
1671 continue;
1672 rows = common;
1673 pden.SetDims(rows, pden.NumCols());
1674 for (int k = 0; k < rc->dim; ++k) {
1675 int n = final[j]->powers[k];
1676 if (n == 0)
1677 continue;
1678 int abs_n = n < 0 ? -n : n;
1679 pden.SetDims(rows+abs_n, pden.NumCols());
1680 for (int l = 0; l < abs_n; ++l) {
1681 if (n > 0)
1682 pden[rows+l] = den_r[k];
1683 else
1684 pden[rows+l] = -den_r[k];
1686 rows += abs_n;
1688 final[j]->coeff *= c;
1689 reduce(final[j]->coeff, rc->denom, num_p, pden);
1692 delete rc;
1693 delete r;
1698 void reducer::handle_polar(Polyhedron *C, int s)
1700 assert(C->NbRays-1 == dim);
1702 sgn = s;
1704 lattice_point(P->Ray[j]+1, C, vertex);
1706 mat_ZZ den;
1707 den.SetDims(dim, dim);
1709 int r;
1710 for (r = 0; r < dim; ++r)
1711 values2zz(C->Ray[r]+1, den[r], dim);
1713 reduce(sgn, one, vertex, den);
1716 void reducer::start(unsigned MaxRays)
1718 for (j = 0; j < P->NbRays; ++j) {
1719 Polyhedron *C = supporting_cone(P, j);
1720 decompose(C, MaxRays);
1724 // incremental counter
1725 struct icounter : public reducer {
1726 mpq_t count;
1728 icounter(Polyhedron *P) : reducer(P) {
1729 mpq_init(count);
1730 lower = 1;
1731 untouched = 0;
1733 ~icounter() {
1734 mpq_clear(count);
1736 virtual void base(ZZ& c, ZZ& cd, vec_ZZ& num, mat_ZZ& den_f);
1739 void icounter::base(ZZ& c, ZZ& cd, vec_ZZ& num, mat_ZZ& den_f)
1741 int r;
1742 unsigned len = den_f.NumRows(); // number of factors in den
1743 vec_ZZ den_s;
1744 den_s.SetLength(len);
1745 ZZ num_s = num[0];
1746 for (r = 0; r < len; ++r)
1747 den_s[r] = den_f[r][0];
1748 normalize(c, num_s, den_s);
1750 dpoly n(len, num_s);
1751 dpoly D(len, den_s[0], 1);
1752 for (int k = 1; k < len; ++k) {
1753 dpoly fact(len, den_s[k], 1);
1754 D *= fact;
1756 mpq_set_si(tcount, 0, 1);
1757 n.div(D, tcount, one);
1758 zz2value(c, tn);
1759 zz2value(cd, td);
1760 mpz_mul(mpq_numref(tcount), mpq_numref(tcount), tn);
1761 mpz_mul(mpq_denref(tcount), mpq_denref(tcount), td);
1762 mpq_canonicalize(tcount);
1763 mpq_add(count, count, tcount);
1766 struct partial_reducer : public reducer {
1767 gen_fun * gf;
1769 partial_reducer(Polyhedron *P, unsigned nparam) : reducer(P) {
1770 gf = new gen_fun;
1771 lower = nparam;
1772 untouched = nparam;
1774 ~partial_reducer() {
1776 virtual void base(ZZ& c, ZZ& cd, vec_ZZ& num, mat_ZZ& den_f);
1777 void start(unsigned MaxRays);
1780 void partial_reducer::base(ZZ& c, ZZ& cd, vec_ZZ& num, mat_ZZ& den_f)
1782 gf->add(c, cd, num, den_f);
1785 void partial_reducer::start(unsigned MaxRays)
1787 for (j = 0; j < P->NbRays; ++j) {
1788 if (!value_pos_p(P->Ray[j][dim+1]))
1789 continue;
1791 Polyhedron *C = supporting_cone(P, j);
1792 decompose(C, MaxRays);
1796 typedef Polyhedron * Polyhedron_p;
1798 void barvinok_count(Polyhedron *P, Value* result, unsigned NbMaxCons)
1800 Polyhedron ** vcone;
1801 ZZ sign;
1802 unsigned dim;
1803 int allocated = 0;
1804 Value factor;
1805 Polyhedron *Q;
1806 int r = 0;
1808 if (emptyQ(P)) {
1809 value_set_si(*result, 0);
1810 return;
1812 if (P->NbBid == 0)
1813 for (; r < P->NbRays; ++r)
1814 if (value_zero_p(P->Ray[r][P->Dimension+1]))
1815 break;
1816 if (P->NbBid !=0 || r < P->NbRays) {
1817 value_set_si(*result, -1);
1818 return;
1820 if (P->NbEq != 0) {
1821 P = remove_equalities(P);
1822 if (emptyQ(P)) {
1823 Polyhedron_Free(P);
1824 value_set_si(*result, 0);
1825 return;
1827 allocated = 1;
1829 value_init(factor);
1830 value_set_si(factor, 1);
1831 Q = Polyhedron_Reduce(P, &factor);
1832 if (Q) {
1833 if (allocated)
1834 Polyhedron_Free(P);
1835 P = Q;
1836 allocated = 1;
1838 if (P->Dimension == 0) {
1839 value_assign(*result, factor);
1840 if (allocated)
1841 Polyhedron_Free(P);
1842 value_clear(factor);
1843 return;
1846 icounter cnt(P);
1847 cnt.start(NbMaxCons);
1849 assert(value_one_p(&cnt.count[0]._mp_den));
1850 value_multiply(*result, &cnt.count[0]._mp_num, factor);
1852 if (allocated)
1853 Polyhedron_Free(P);
1854 value_clear(factor);
1857 static void uni_polynom(int param, Vector *c, evalue *EP)
1859 unsigned dim = c->Size-2;
1860 value_init(EP->d);
1861 value_set_si(EP->d,0);
1862 EP->x.p = new_enode(polynomial, dim+1, param+1);
1863 for (int j = 0; j <= dim; ++j)
1864 evalue_set(&EP->x.p->arr[j], c->p[j], c->p[dim+1]);
1867 static void multi_polynom(Vector *c, evalue* X, evalue *EP)
1869 unsigned dim = c->Size-2;
1870 evalue EC;
1872 value_init(EC.d);
1873 evalue_set(&EC, c->p[dim], c->p[dim+1]);
1875 value_init(EP->d);
1876 evalue_set(EP, c->p[dim], c->p[dim+1]);
1878 for (int i = dim-1; i >= 0; --i) {
1879 emul(X, EP);
1880 value_assign(EC.x.n, c->p[i]);
1881 eadd(&EC, EP);
1883 free_evalue_refs(&EC);
1886 Polyhedron *unfringe (Polyhedron *P, unsigned MaxRays)
1888 int len = P->Dimension+2;
1889 Polyhedron *T, *R = P;
1890 Value g;
1891 value_init(g);
1892 Vector *row = Vector_Alloc(len);
1893 value_set_si(row->p[0], 1);
1895 R = DomainConstraintSimplify(Polyhedron_Copy(P), MaxRays);
1897 Matrix *M = Matrix_Alloc(2, len-1);
1898 value_set_si(M->p[1][len-2], 1);
1899 for (int v = 0; v < P->Dimension; ++v) {
1900 value_set_si(M->p[0][v], 1);
1901 Polyhedron *I = Polyhedron_Image(P, M, 2+1);
1902 value_set_si(M->p[0][v], 0);
1903 for (int r = 0; r < I->NbConstraints; ++r) {
1904 if (value_zero_p(I->Constraint[r][0]))
1905 continue;
1906 if (value_zero_p(I->Constraint[r][1]))
1907 continue;
1908 if (value_one_p(I->Constraint[r][1]))
1909 continue;
1910 if (value_mone_p(I->Constraint[r][1]))
1911 continue;
1912 value_absolute(g, I->Constraint[r][1]);
1913 Vector_Set(row->p+1, 0, len-2);
1914 value_division(row->p[1+v], I->Constraint[r][1], g);
1915 mpz_fdiv_q(row->p[len-1], I->Constraint[r][2], g);
1916 T = R;
1917 R = AddConstraints(row->p, 1, R, MaxRays);
1918 if (T != P)
1919 Polyhedron_Free(T);
1921 Polyhedron_Free(I);
1923 Matrix_Free(M);
1924 Vector_Free(row);
1925 value_clear(g);
1926 return R;
1929 static Polyhedron *reduce_domain(Polyhedron *D, Matrix *CT, Polyhedron *CEq,
1930 Polyhedron **fVD, int nd, unsigned MaxRays)
1932 assert(CEq);
1934 Polyhedron *Dt;
1935 Dt = CT ? DomainPreimage(D, CT, MaxRays) : D;
1936 Polyhedron *rVD = DomainIntersection(Dt, CEq, MaxRays);
1938 /* if rVD is empty or too small in geometric dimension */
1939 if(!rVD || emptyQ(rVD) ||
1940 (rVD->Dimension-rVD->NbEq < Dt->Dimension-Dt->NbEq-CEq->NbEq)) {
1941 if(rVD)
1942 Domain_Free(rVD);
1943 if (CT)
1944 Domain_Free(Dt);
1945 return 0; /* empty validity domain */
1948 if (CT)
1949 Domain_Free(Dt);
1951 fVD[nd] = Domain_Copy(rVD);
1952 for (int i = 0 ; i < nd; ++i) {
1953 Polyhedron *I = DomainIntersection(fVD[nd], fVD[i], MaxRays);
1954 if (emptyQ(I)) {
1955 Domain_Free(I);
1956 continue;
1958 Polyhedron *F = DomainSimplify(I, fVD[nd], MaxRays);
1959 if (F->NbEq == 1) {
1960 Polyhedron *T = rVD;
1961 rVD = DomainDifference(rVD, F, MaxRays);
1962 Domain_Free(T);
1964 Domain_Free(F);
1965 Domain_Free(I);
1968 rVD = DomainConstraintSimplify(rVD, MaxRays);
1969 if (emptyQ(rVD)) {
1970 Domain_Free(fVD[nd]);
1971 Domain_Free(rVD);
1972 return 0;
1975 Value c;
1976 value_init(c);
1977 barvinok_count(rVD, &c, MaxRays);
1978 if (value_zero_p(c)) {
1979 Domain_Free(rVD);
1980 rVD = 0;
1982 value_clear(c);
1984 return rVD;
1987 static bool Polyhedron_is_infinite(Polyhedron *P, unsigned nparam)
1989 int r;
1990 for (r = 0; r < P->NbRays; ++r)
1991 if (value_zero_p(P->Ray[r][0]) ||
1992 value_zero_p(P->Ray[r][P->Dimension+1])) {
1993 int i;
1994 for (i = P->Dimension - nparam; i < P->Dimension; ++i)
1995 if (value_notzero_p(P->Ray[r][i+1]))
1996 break;
1997 if (i >= P->Dimension)
1998 break;
2000 return r < P->NbRays;
2003 /* Check whether all rays point in the positive directions
2004 * for the parameters
2006 static bool Polyhedron_has_positive_rays(Polyhedron *P, unsigned nparam)
2008 int r;
2009 for (r = 0; r < P->NbRays; ++r)
2010 if (value_zero_p(P->Ray[r][P->Dimension+1])) {
2011 int i;
2012 for (i = P->Dimension - nparam; i < P->Dimension; ++i)
2013 if (value_neg_p(P->Ray[r][i+1]))
2014 return false;
2016 return true;
2019 typedef evalue * evalue_p;
2021 struct enumerator : public polar_decomposer {
2022 vec_ZZ lambda;
2023 unsigned dim, nbV;
2024 evalue ** vE;
2025 int _i;
2026 mat_ZZ rays;
2027 vec_ZZ den;
2028 ZZ sign;
2029 Polyhedron *P;
2030 Param_Vertices *V;
2031 term_info num;
2032 Vector *c;
2033 mpq_t count;
2035 enumerator(Polyhedron *P, unsigned dim, unsigned nbV) {
2036 this->P = P;
2037 this->dim = dim;
2038 this->nbV = nbV;
2039 randomvector(P, lambda, dim);
2040 rays.SetDims(dim, dim);
2041 den.SetLength(dim);
2042 c = Vector_Alloc(dim+2);
2044 vE = new evalue_p[nbV];
2045 for (int j = 0; j < nbV; ++j)
2046 vE[j] = 0;
2048 mpq_init(count);
2051 void decompose_at(Param_Vertices *V, int _i, unsigned MaxRays) {
2052 Polyhedron *C = supporting_cone_p(P, V);
2053 this->_i = _i;
2054 this->V = V;
2056 vE[_i] = new evalue;
2057 value_init(vE[_i]->d);
2058 evalue_set_si(vE[_i], 0, 1);
2060 decompose(C, MaxRays);
2063 ~enumerator() {
2064 mpq_clear(count);
2065 Vector_Free(c);
2067 for (int j = 0; j < nbV; ++j)
2068 if (vE[j]) {
2069 free_evalue_refs(vE[j]);
2070 delete vE[j];
2072 delete [] vE;
2075 virtual void handle_polar(Polyhedron *P, int sign);
2078 void enumerator::handle_polar(Polyhedron *C, int s)
2080 int r = 0;
2081 assert(C->NbRays-1 == dim);
2082 add_rays(rays, C, &r);
2083 for (int k = 0; k < dim; ++k) {
2084 assert(lambda * rays[k] != 0);
2087 sign = s;
2089 lattice_point(V, C, lambda, &num, 0);
2090 den = rays * lambda;
2091 normalize(sign, num.constant, den);
2093 dpoly n(dim, den[0], 1);
2094 for (int k = 1; k < dim; ++k) {
2095 dpoly fact(dim, den[k], 1);
2096 n *= fact;
2098 if (num.E != NULL) {
2099 ZZ one(INIT_VAL, 1);
2100 dpoly_n d(dim, num.constant, one);
2101 d.div(n, c, sign);
2102 evalue EV;
2103 multi_polynom(c, num.E, &EV);
2104 eadd(&EV , vE[_i]);
2105 free_evalue_refs(&EV);
2106 free_evalue_refs(num.E);
2107 delete num.E;
2108 } else if (num.pos != -1) {
2109 dpoly_n d(dim, num.constant, num.coeff);
2110 d.div(n, c, sign);
2111 evalue EV;
2112 uni_polynom(num.pos, c, &EV);
2113 eadd(&EV , vE[_i]);
2114 free_evalue_refs(&EV);
2115 } else {
2116 mpq_set_si(count, 0, 1);
2117 dpoly d(dim, num.constant);
2118 d.div(n, count, sign);
2119 evalue EV;
2120 value_init(EV.d);
2121 evalue_set(&EV, &count[0]._mp_num, &count[0]._mp_den);
2122 eadd(&EV , vE[_i]);
2123 free_evalue_refs(&EV);
2127 evalue* barvinok_enumerate_ev(Polyhedron *P, Polyhedron* C, unsigned MaxRays)
2129 //P = unfringe(P, MaxRays);
2130 Polyhedron *CEq = NULL, *rVD, *pVD, *CA;
2131 Matrix *CT = NULL;
2132 Param_Polyhedron *PP = NULL;
2133 Param_Domain *D, *next;
2134 Param_Vertices *V;
2135 int r = 0;
2136 unsigned nparam = C->Dimension;
2137 evalue *eres;
2138 ALLOC(evalue, eres);
2139 value_init(eres->d);
2140 value_set_si(eres->d, 0);
2142 evalue factor;
2143 value_init(factor.d);
2144 evalue_set_si(&factor, 1, 1);
2146 CA = align_context(C, P->Dimension, MaxRays);
2147 P = DomainIntersection(P, CA, MaxRays);
2148 Polyhedron_Free(CA);
2150 if (C->Dimension == 0 || emptyQ(P)) {
2151 constant:
2152 eres->x.p = new_enode(partition, 2, C->Dimension);
2153 EVALUE_SET_DOMAIN(eres->x.p->arr[0],
2154 DomainConstraintSimplify(CEq ? CEq : Polyhedron_Copy(C), MaxRays));
2155 value_set_si(eres->x.p->arr[1].d, 1);
2156 value_init(eres->x.p->arr[1].x.n);
2157 if (emptyQ(P))
2158 value_set_si(eres->x.p->arr[1].x.n, 0);
2159 else
2160 barvinok_count(P, &eres->x.p->arr[1].x.n, MaxRays);
2161 out:
2162 emul(&factor, eres);
2163 reduce_evalue(eres);
2164 free_evalue_refs(&factor);
2165 Polyhedron_Free(P);
2166 if (CT)
2167 Matrix_Free(CT);
2168 if (PP)
2169 Param_Polyhedron_Free(PP);
2171 return eres;
2173 if (Polyhedron_is_infinite(P, nparam))
2174 goto constant;
2176 if (P->NbEq != 0) {
2177 Matrix *f;
2178 P = remove_equalities_p(P, P->Dimension-nparam, &f);
2179 mask(f, &factor);
2180 Matrix_Free(f);
2182 if (P->Dimension == nparam) {
2183 CEq = P;
2184 P = Universe_Polyhedron(0);
2185 goto constant;
2188 Polyhedron *Q = ParamPolyhedron_Reduce(P, P->Dimension-nparam, &factor);
2189 if (Q) {
2190 Polyhedron_Free(P);
2191 if (Q->Dimension == nparam) {
2192 CEq = Q;
2193 P = Universe_Polyhedron(0);
2194 goto constant;
2196 P = Q;
2198 Polyhedron *oldP = P;
2199 PP = Polyhedron2Param_SimplifiedDomain(&P,C,MaxRays,&CEq,&CT);
2200 if (P != oldP)
2201 Polyhedron_Free(oldP);
2203 if (isIdentity(CT)) {
2204 Matrix_Free(CT);
2205 CT = NULL;
2206 } else {
2207 assert(CT->NbRows != CT->NbColumns);
2208 if (CT->NbRows == 1) // no more parameters
2209 goto constant;
2210 nparam = CT->NbRows - 1;
2213 unsigned dim = P->Dimension - nparam;
2215 enumerator et(P, dim, PP->nbV);
2217 int nd;
2218 for (nd = 0, D=PP->D; D; ++nd, D=D->next);
2219 struct section { Polyhedron *D; evalue E; };
2220 section *s = new section[nd];
2221 Polyhedron **fVD = new Polyhedron_p[nd];
2223 for(nd = 0, D=PP->D; D; D=next) {
2224 next = D->next;
2226 Polyhedron *rVD = reduce_domain(D->Domain, CT, CEq,
2227 fVD, nd, MaxRays);
2228 if (!rVD)
2229 continue;
2231 pVD = CT ? DomainImage(rVD,CT,MaxRays) : rVD;
2233 value_init(s[nd].E.d);
2234 evalue_set_si(&s[nd].E, 0, 1);
2236 FORALL_PVertex_in_ParamPolyhedron(V,D,PP) // _i is internal counter
2237 if (!et.vE[_i])
2238 et.decompose_at(V, _i, MaxRays);
2239 eadd(et.vE[_i] , &s[nd].E);
2240 END_FORALL_PVertex_in_ParamPolyhedron;
2241 reduce_in_domain(&s[nd].E, pVD);
2243 if (CT)
2244 addeliminatedparams_evalue(&s[nd].E, CT);
2245 s[nd].D = rVD;
2246 ++nd;
2247 if (rVD != pVD)
2248 Domain_Free(pVD);
2251 if (nd == 0)
2252 evalue_set_si(eres, 0, 1);
2253 else {
2254 eres->x.p = new_enode(partition, 2*nd, C->Dimension);
2255 for (int j = 0; j < nd; ++j) {
2256 EVALUE_SET_DOMAIN(eres->x.p->arr[2*j], s[j].D);
2257 value_clear(eres->x.p->arr[2*j+1].d);
2258 eres->x.p->arr[2*j+1] = s[j].E;
2259 Domain_Free(fVD[j]);
2262 delete [] s;
2263 delete [] fVD;
2266 if (CEq)
2267 Polyhedron_Free(CEq);
2269 goto out;
2272 Enumeration* barvinok_enumerate(Polyhedron *P, Polyhedron* C, unsigned MaxRays)
2274 evalue *EP = barvinok_enumerate_ev(P, C, MaxRays);
2276 return partition2enumeration(EP);
2279 static void SwapColumns(Value **V, int n, int i, int j)
2281 for (int r = 0; r < n; ++r)
2282 value_swap(V[r][i], V[r][j]);
2285 static void SwapColumns(Polyhedron *P, int i, int j)
2287 SwapColumns(P->Constraint, P->NbConstraints, i, j);
2288 SwapColumns(P->Ray, P->NbRays, i, j);
2291 static void negative_test_constraint(Value *l, Value *u, Value *c, int pos,
2292 int len, Value *v)
2294 value_oppose(*v, u[pos+1]);
2295 Vector_Combine(l+1, u+1, c+1, *v, l[pos+1], len-1);
2296 value_multiply(*v, *v, l[pos+1]);
2297 value_substract(c[len-1], c[len-1], *v);
2298 value_set_si(*v, -1);
2299 Vector_Scale(c+1, c+1, *v, len-1);
2300 value_decrement(c[len-1], c[len-1]);
2301 ConstraintSimplify(c, c, len, v);
2304 static bool parallel_constraints(Value *l, Value *u, Value *c, int pos,
2305 int len)
2307 bool parallel;
2308 Value g1;
2309 Value g2;
2310 value_init(g1);
2311 value_init(g2);
2313 Vector_Gcd(&l[1+pos], len, &g1);
2314 Vector_Gcd(&u[1+pos], len, &g2);
2315 Vector_Combine(l+1+pos, u+1+pos, c+1, g2, g1, len);
2316 parallel = First_Non_Zero(c+1, len) == -1;
2318 value_clear(g1);
2319 value_clear(g2);
2321 return parallel;
2324 static void negative_test_constraint7(Value *l, Value *u, Value *c, int pos,
2325 int exist, int len, Value *v)
2327 Value g;
2328 value_init(g);
2330 Vector_Gcd(&u[1+pos], exist, v);
2331 Vector_Gcd(&l[1+pos], exist, &g);
2332 Vector_Combine(l+1, u+1, c+1, *v, g, len-1);
2333 value_multiply(*v, *v, g);
2334 value_substract(c[len-1], c[len-1], *v);
2335 value_set_si(*v, -1);
2336 Vector_Scale(c+1, c+1, *v, len-1);
2337 value_decrement(c[len-1], c[len-1]);
2338 ConstraintSimplify(c, c, len, v);
2340 value_clear(g);
2343 static void oppose_constraint(Value *c, int len, Value *v)
2345 value_set_si(*v, -1);
2346 Vector_Scale(c+1, c+1, *v, len-1);
2347 value_decrement(c[len-1], c[len-1]);
2350 static bool SplitOnConstraint(Polyhedron *P, int i, int l, int u,
2351 int nvar, int len, int exist, int MaxRays,
2352 Vector *row, Value& f, bool independent,
2353 Polyhedron **pos, Polyhedron **neg)
2355 negative_test_constraint(P->Constraint[l], P->Constraint[u],
2356 row->p, nvar+i, len, &f);
2357 *neg = AddConstraints(row->p, 1, P, MaxRays);
2359 /* We found an independent, but useless constraint
2360 * Maybe we should detect this earlier and not
2361 * mark the variable as INDEPENDENT
2363 if (emptyQ((*neg))) {
2364 Polyhedron_Free(*neg);
2365 return false;
2368 oppose_constraint(row->p, len, &f);
2369 *pos = AddConstraints(row->p, 1, P, MaxRays);
2371 if (emptyQ((*pos))) {
2372 Polyhedron_Free(*neg);
2373 Polyhedron_Free(*pos);
2374 return false;
2377 return true;
2381 * unimodularly transform P such that constraint r is transformed
2382 * into a constraint that involves only a single (the first)
2383 * existential variable
2386 static Polyhedron *rotate_along(Polyhedron *P, int r, int nvar, int exist,
2387 unsigned MaxRays)
2389 Value g;
2390 value_init(g);
2392 Vector *row = Vector_Alloc(exist);
2393 Vector_Copy(P->Constraint[r]+1+nvar, row->p, exist);
2394 Vector_Gcd(row->p, exist, &g);
2395 if (value_notone_p(g))
2396 Vector_AntiScale(row->p, row->p, g, exist);
2397 value_clear(g);
2399 Matrix *M = unimodular_complete(row);
2400 Matrix *M2 = Matrix_Alloc(P->Dimension+1, P->Dimension+1);
2401 for (r = 0; r < nvar; ++r)
2402 value_set_si(M2->p[r][r], 1);
2403 for ( ; r < nvar+exist; ++r)
2404 Vector_Copy(M->p[r-nvar], M2->p[r]+nvar, exist);
2405 for ( ; r < P->Dimension+1; ++r)
2406 value_set_si(M2->p[r][r], 1);
2407 Polyhedron *T = Polyhedron_Image(P, M2, MaxRays);
2409 Matrix_Free(M2);
2410 Matrix_Free(M);
2411 Vector_Free(row);
2413 return T;
2416 static bool SplitOnVar(Polyhedron *P, int i,
2417 int nvar, int len, int exist, int MaxRays,
2418 Vector *row, Value& f, bool independent,
2419 Polyhedron **pos, Polyhedron **neg)
2421 int j;
2423 for (int l = P->NbEq; l < P->NbConstraints; ++l) {
2424 if (value_negz_p(P->Constraint[l][nvar+i+1]))
2425 continue;
2427 if (independent) {
2428 for (j = 0; j < exist; ++j)
2429 if (j != i && value_notzero_p(P->Constraint[l][nvar+j+1]))
2430 break;
2431 if (j < exist)
2432 continue;
2435 for (int u = P->NbEq; u < P->NbConstraints; ++u) {
2436 if (value_posz_p(P->Constraint[u][nvar+i+1]))
2437 continue;
2439 if (independent) {
2440 for (j = 0; j < exist; ++j)
2441 if (j != i && value_notzero_p(P->Constraint[u][nvar+j+1]))
2442 break;
2443 if (j < exist)
2444 continue;
2447 if (SplitOnConstraint(P, i, l, u,
2448 nvar, len, exist, MaxRays,
2449 row, f, independent,
2450 pos, neg)) {
2451 if (independent) {
2452 if (i != 0)
2453 SwapColumns(*neg, nvar+1, nvar+1+i);
2455 return true;
2460 return false;
2463 static bool double_bound_pair(Polyhedron *P, int nvar, int exist,
2464 int i, int l1, int l2,
2465 Polyhedron **pos, Polyhedron **neg)
2467 Value f;
2468 value_init(f);
2469 Vector *row = Vector_Alloc(P->Dimension+2);
2470 value_set_si(row->p[0], 1);
2471 value_oppose(f, P->Constraint[l1][nvar+i+1]);
2472 Vector_Combine(P->Constraint[l1]+1, P->Constraint[l2]+1,
2473 row->p+1,
2474 P->Constraint[l2][nvar+i+1], f,
2475 P->Dimension+1);
2476 ConstraintSimplify(row->p, row->p, P->Dimension+2, &f);
2477 *pos = AddConstraints(row->p, 1, P, 0);
2478 value_set_si(f, -1);
2479 Vector_Scale(row->p+1, row->p+1, f, P->Dimension+1);
2480 value_decrement(row->p[P->Dimension+1], row->p[P->Dimension+1]);
2481 *neg = AddConstraints(row->p, 1, P, 0);
2482 Vector_Free(row);
2483 value_clear(f);
2485 return !emptyQ((*pos)) && !emptyQ((*neg));
2488 static bool double_bound(Polyhedron *P, int nvar, int exist,
2489 Polyhedron **pos, Polyhedron **neg)
2491 for (int i = 0; i < exist; ++i) {
2492 int l1, l2;
2493 for (l1 = P->NbEq; l1 < P->NbConstraints; ++l1) {
2494 if (value_negz_p(P->Constraint[l1][nvar+i+1]))
2495 continue;
2496 for (l2 = l1 + 1; l2 < P->NbConstraints; ++l2) {
2497 if (value_negz_p(P->Constraint[l2][nvar+i+1]))
2498 continue;
2499 if (double_bound_pair(P, nvar, exist, i, l1, l2, pos, neg))
2500 return true;
2503 for (l1 = P->NbEq; l1 < P->NbConstraints; ++l1) {
2504 if (value_posz_p(P->Constraint[l1][nvar+i+1]))
2505 continue;
2506 if (l1 < P->NbConstraints)
2507 for (l2 = l1 + 1; l2 < P->NbConstraints; ++l2) {
2508 if (value_posz_p(P->Constraint[l2][nvar+i+1]))
2509 continue;
2510 if (double_bound_pair(P, nvar, exist, i, l1, l2, pos, neg))
2511 return true;
2514 return false;
2516 return false;
2519 enum constraint {
2520 ALL_POS = 1 << 0,
2521 ONE_NEG = 1 << 1,
2522 INDEPENDENT = 1 << 2,
2523 ROT_NEG = 1 << 3
2526 static evalue* enumerate_or(Polyhedron *D,
2527 unsigned exist, unsigned nparam, unsigned MaxRays)
2529 #ifdef DEBUG_ER
2530 fprintf(stderr, "\nER: Or\n");
2531 #endif /* DEBUG_ER */
2533 Polyhedron *N = D->next;
2534 D->next = 0;
2535 evalue *EP =
2536 barvinok_enumerate_e(D, exist, nparam, MaxRays);
2537 Polyhedron_Free(D);
2539 for (D = N; D; D = N) {
2540 N = D->next;
2541 D->next = 0;
2543 evalue *EN =
2544 barvinok_enumerate_e(D, exist, nparam, MaxRays);
2546 eor(EN, EP);
2547 free_evalue_refs(EN);
2548 free(EN);
2549 Polyhedron_Free(D);
2552 reduce_evalue(EP);
2554 return EP;
2557 static evalue* enumerate_sum(Polyhedron *P,
2558 unsigned exist, unsigned nparam, unsigned MaxRays)
2560 int nvar = P->Dimension - exist - nparam;
2561 int toswap = nvar < exist ? nvar : exist;
2562 for (int i = 0; i < toswap; ++i)
2563 SwapColumns(P, 1 + i, nvar+exist - i);
2564 nparam += nvar;
2566 #ifdef DEBUG_ER
2567 fprintf(stderr, "\nER: Sum\n");
2568 #endif /* DEBUG_ER */
2570 evalue *EP = barvinok_enumerate_e(P, exist, nparam, MaxRays);
2572 for (int i = 0; i < /* nvar */ nparam; ++i) {
2573 Matrix *C = Matrix_Alloc(1, 1 + nparam + 1);
2574 value_set_si(C->p[0][0], 1);
2575 evalue split;
2576 value_init(split.d);
2577 value_set_si(split.d, 0);
2578 split.x.p = new_enode(partition, 4, nparam);
2579 value_set_si(C->p[0][1+i], 1);
2580 Matrix *C2 = Matrix_Copy(C);
2581 EVALUE_SET_DOMAIN(split.x.p->arr[0],
2582 Constraints2Polyhedron(C2, MaxRays));
2583 Matrix_Free(C2);
2584 evalue_set_si(&split.x.p->arr[1], 1, 1);
2585 value_set_si(C->p[0][1+i], -1);
2586 value_set_si(C->p[0][1+nparam], -1);
2587 EVALUE_SET_DOMAIN(split.x.p->arr[2],
2588 Constraints2Polyhedron(C, MaxRays));
2589 evalue_set_si(&split.x.p->arr[3], 1, 1);
2590 emul(&split, EP);
2591 free_evalue_refs(&split);
2592 Matrix_Free(C);
2594 reduce_evalue(EP);
2595 evalue_range_reduction(EP);
2597 evalue_frac2floor(EP);
2599 evalue *sum = esum(EP, nvar);
2601 free_evalue_refs(EP);
2602 free(EP);
2603 EP = sum;
2605 evalue_range_reduction(EP);
2607 return EP;
2610 static evalue* split_sure(Polyhedron *P, Polyhedron *S,
2611 unsigned exist, unsigned nparam, unsigned MaxRays)
2613 int nvar = P->Dimension - exist - nparam;
2615 Matrix *M = Matrix_Alloc(exist, S->Dimension+2);
2616 for (int i = 0; i < exist; ++i)
2617 value_set_si(M->p[i][nvar+i+1], 1);
2618 Polyhedron *O = S;
2619 S = DomainAddRays(S, M, MaxRays);
2620 Polyhedron_Free(O);
2621 Polyhedron *F = DomainAddRays(P, M, MaxRays);
2622 Polyhedron *D = DomainDifference(F, S, MaxRays);
2623 O = D;
2624 D = Disjoint_Domain(D, 0, MaxRays);
2625 Polyhedron_Free(F);
2626 Domain_Free(O);
2627 Matrix_Free(M);
2629 M = Matrix_Alloc(P->Dimension+1-exist, P->Dimension+1);
2630 for (int j = 0; j < nvar; ++j)
2631 value_set_si(M->p[j][j], 1);
2632 for (int j = 0; j < nparam+1; ++j)
2633 value_set_si(M->p[nvar+j][nvar+exist+j], 1);
2634 Polyhedron *T = Polyhedron_Image(S, M, MaxRays);
2635 evalue *EP = barvinok_enumerate_e(T, 0, nparam, MaxRays);
2636 Polyhedron_Free(S);
2637 Polyhedron_Free(T);
2638 Matrix_Free(M);
2640 for (Polyhedron *Q = D; Q; Q = Q->next) {
2641 Polyhedron *N = Q->next;
2642 Q->next = 0;
2643 T = DomainIntersection(P, Q, MaxRays);
2644 evalue *E = barvinok_enumerate_e(T, exist, nparam, MaxRays);
2645 eadd(E, EP);
2646 free_evalue_refs(E);
2647 free(E);
2648 Polyhedron_Free(T);
2649 Q->next = N;
2651 Domain_Free(D);
2652 return EP;
2655 static evalue* enumerate_sure(Polyhedron *P,
2656 unsigned exist, unsigned nparam, unsigned MaxRays)
2658 int i;
2659 Polyhedron *S = P;
2660 int nvar = P->Dimension - exist - nparam;
2661 Value lcm;
2662 Value f;
2663 value_init(lcm);
2664 value_init(f);
2666 for (i = 0; i < exist; ++i) {
2667 Matrix *M = Matrix_Alloc(S->NbConstraints, S->Dimension+2);
2668 int c = 0;
2669 value_set_si(lcm, 1);
2670 for (int j = 0; j < S->NbConstraints; ++j) {
2671 if (value_negz_p(S->Constraint[j][1+nvar+i]))
2672 continue;
2673 if (value_one_p(S->Constraint[j][1+nvar+i]))
2674 continue;
2675 value_lcm(lcm, S->Constraint[j][1+nvar+i], &lcm);
2678 for (int j = 0; j < S->NbConstraints; ++j) {
2679 if (value_negz_p(S->Constraint[j][1+nvar+i]))
2680 continue;
2681 if (value_one_p(S->Constraint[j][1+nvar+i]))
2682 continue;
2683 value_division(f, lcm, S->Constraint[j][1+nvar+i]);
2684 Vector_Scale(S->Constraint[j], M->p[c], f, S->Dimension+2);
2685 value_substract(M->p[c][S->Dimension+1],
2686 M->p[c][S->Dimension+1],
2687 lcm);
2688 value_increment(M->p[c][S->Dimension+1],
2689 M->p[c][S->Dimension+1]);
2690 ++c;
2692 Polyhedron *O = S;
2693 S = AddConstraints(M->p[0], c, S, MaxRays);
2694 if (O != P)
2695 Polyhedron_Free(O);
2696 Matrix_Free(M);
2697 if (emptyQ(S)) {
2698 Polyhedron_Free(S);
2699 value_clear(lcm);
2700 value_clear(f);
2701 return 0;
2704 value_clear(lcm);
2705 value_clear(f);
2707 #ifdef DEBUG_ER
2708 fprintf(stderr, "\nER: Sure\n");
2709 #endif /* DEBUG_ER */
2711 return split_sure(P, S, exist, nparam, MaxRays);
2714 static evalue* enumerate_sure2(Polyhedron *P,
2715 unsigned exist, unsigned nparam, unsigned MaxRays)
2717 int nvar = P->Dimension - exist - nparam;
2718 int r;
2719 for (r = 0; r < P->NbRays; ++r)
2720 if (value_one_p(P->Ray[r][0]) &&
2721 value_one_p(P->Ray[r][P->Dimension+1]))
2722 break;
2724 if (r >= P->NbRays)
2725 return 0;
2727 Matrix *M = Matrix_Alloc(nvar + 1 + nparam, P->Dimension+2);
2728 for (int i = 0; i < nvar; ++i)
2729 value_set_si(M->p[i][1+i], 1);
2730 for (int i = 0; i < nparam; ++i)
2731 value_set_si(M->p[i+nvar][1+nvar+exist+i], 1);
2732 Vector_Copy(P->Ray[r]+1+nvar, M->p[nvar+nparam]+1+nvar, exist);
2733 value_set_si(M->p[nvar+nparam][0], 1);
2734 value_set_si(M->p[nvar+nparam][P->Dimension+1], 1);
2735 Polyhedron * F = Rays2Polyhedron(M, MaxRays);
2736 Matrix_Free(M);
2738 Polyhedron *I = DomainIntersection(F, P, MaxRays);
2739 Polyhedron_Free(F);
2741 #ifdef DEBUG_ER
2742 fprintf(stderr, "\nER: Sure2\n");
2743 #endif /* DEBUG_ER */
2745 return split_sure(P, I, exist, nparam, MaxRays);
2748 static evalue* enumerate_cyclic(Polyhedron *P,
2749 unsigned exist, unsigned nparam,
2750 evalue * EP, int r, int p, unsigned MaxRays)
2752 int nvar = P->Dimension - exist - nparam;
2754 /* If EP in its fractional maps only contains references
2755 * to the remainder parameter with appropriate coefficients
2756 * then we could in principle avoid adding existentially
2757 * quantified variables to the validity domains.
2758 * We'd have to replace the remainder by m { p/m }
2759 * and multiply with an appropriate factor that is one
2760 * only in the appropriate range.
2761 * This last multiplication can be avoided if EP
2762 * has a single validity domain with no (further)
2763 * constraints on the remainder parameter
2766 Matrix *CT = Matrix_Alloc(nparam+1, nparam+3);
2767 Matrix *M = Matrix_Alloc(1, 1+nparam+3);
2768 for (int j = 0; j < nparam; ++j)
2769 if (j != p)
2770 value_set_si(CT->p[j][j], 1);
2771 value_set_si(CT->p[p][nparam+1], 1);
2772 value_set_si(CT->p[nparam][nparam+2], 1);
2773 value_set_si(M->p[0][1+p], -1);
2774 value_absolute(M->p[0][1+nparam], P->Ray[0][1+nvar+exist+p]);
2775 value_set_si(M->p[0][1+nparam+1], 1);
2776 Polyhedron *CEq = Constraints2Polyhedron(M, 1);
2777 Matrix_Free(M);
2778 addeliminatedparams_enum(EP, CT, CEq, MaxRays, nparam);
2779 Polyhedron_Free(CEq);
2780 Matrix_Free(CT);
2782 return EP;
2785 static void enumerate_vd_add_ray(evalue *EP, Matrix *Rays, unsigned MaxRays)
2787 if (value_notzero_p(EP->d))
2788 return;
2790 assert(EP->x.p->type == partition);
2791 assert(EP->x.p->pos == EVALUE_DOMAIN(EP->x.p->arr[0])->Dimension);
2792 for (int i = 0; i < EP->x.p->size/2; ++i) {
2793 Polyhedron *D = EVALUE_DOMAIN(EP->x.p->arr[2*i]);
2794 Polyhedron *N = DomainAddRays(D, Rays, MaxRays);
2795 EVALUE_SET_DOMAIN(EP->x.p->arr[2*i], N);
2796 Domain_Free(D);
2800 static evalue* enumerate_line(Polyhedron *P,
2801 unsigned exist, unsigned nparam, unsigned MaxRays)
2803 if (P->NbBid == 0)
2804 return 0;
2806 #ifdef DEBUG_ER
2807 fprintf(stderr, "\nER: Line\n");
2808 #endif /* DEBUG_ER */
2810 int nvar = P->Dimension - exist - nparam;
2811 int i, j;
2812 for (i = 0; i < nparam; ++i)
2813 if (value_notzero_p(P->Ray[0][1+nvar+exist+i]))
2814 break;
2815 assert(i < nparam);
2816 for (j = i+1; j < nparam; ++j)
2817 if (value_notzero_p(P->Ray[0][1+nvar+exist+i]))
2818 break;
2819 assert(j >= nparam); // for now
2821 Matrix *M = Matrix_Alloc(2, P->Dimension+2);
2822 value_set_si(M->p[0][0], 1);
2823 value_set_si(M->p[0][1+nvar+exist+i], 1);
2824 value_set_si(M->p[1][0], 1);
2825 value_set_si(M->p[1][1+nvar+exist+i], -1);
2826 value_absolute(M->p[1][1+P->Dimension], P->Ray[0][1+nvar+exist+i]);
2827 value_decrement(M->p[1][1+P->Dimension], M->p[1][1+P->Dimension]);
2828 Polyhedron *S = AddConstraints(M->p[0], 2, P, MaxRays);
2829 evalue *EP = barvinok_enumerate_e(S, exist, nparam, MaxRays);
2830 Polyhedron_Free(S);
2831 Matrix_Free(M);
2833 return enumerate_cyclic(P, exist, nparam, EP, 0, i, MaxRays);
2836 static int single_param_pos(Polyhedron*P, unsigned exist, unsigned nparam,
2837 int r)
2839 int nvar = P->Dimension - exist - nparam;
2840 if (First_Non_Zero(P->Ray[r]+1, nvar) != -1)
2841 return -1;
2842 int i = First_Non_Zero(P->Ray[r]+1+nvar+exist, nparam);
2843 if (i == -1)
2844 return -1;
2845 if (First_Non_Zero(P->Ray[r]+1+nvar+exist+1, nparam-i-1) != -1)
2846 return -1;
2847 return i;
2850 static evalue* enumerate_remove_ray(Polyhedron *P, int r,
2851 unsigned exist, unsigned nparam, unsigned MaxRays)
2853 #ifdef DEBUG_ER
2854 fprintf(stderr, "\nER: RedundantRay\n");
2855 #endif /* DEBUG_ER */
2857 Value one;
2858 value_init(one);
2859 value_set_si(one, 1);
2860 int len = P->NbRays-1;
2861 Matrix *M = Matrix_Alloc(2 * len, P->Dimension+2);
2862 Vector_Copy(P->Ray[0], M->p[0], r * (P->Dimension+2));
2863 Vector_Copy(P->Ray[r+1], M->p[r], (len-r) * (P->Dimension+2));
2864 for (int j = 0; j < P->NbRays; ++j) {
2865 if (j == r)
2866 continue;
2867 Vector_Combine(P->Ray[j], P->Ray[r], M->p[len+j-(j>r)],
2868 one, P->Ray[j][P->Dimension+1], P->Dimension+2);
2871 P = Rays2Polyhedron(M, MaxRays);
2872 Matrix_Free(M);
2873 evalue *EP = barvinok_enumerate_e(P, exist, nparam, MaxRays);
2874 Polyhedron_Free(P);
2875 value_clear(one);
2877 return EP;
2880 static evalue* enumerate_redundant_ray(Polyhedron *P,
2881 unsigned exist, unsigned nparam, unsigned MaxRays)
2883 assert(P->NbBid == 0);
2884 int nvar = P->Dimension - exist - nparam;
2885 Value m;
2886 value_init(m);
2888 for (int r = 0; r < P->NbRays; ++r) {
2889 if (value_notzero_p(P->Ray[r][P->Dimension+1]))
2890 continue;
2891 int i1 = single_param_pos(P, exist, nparam, r);
2892 if (i1 == -1)
2893 continue;
2894 for (int r2 = r+1; r2 < P->NbRays; ++r2) {
2895 if (value_notzero_p(P->Ray[r2][P->Dimension+1]))
2896 continue;
2897 int i2 = single_param_pos(P, exist, nparam, r2);
2898 if (i2 == -1)
2899 continue;
2900 if (i1 != i2)
2901 continue;
2903 value_division(m, P->Ray[r][1+nvar+exist+i1],
2904 P->Ray[r2][1+nvar+exist+i1]);
2905 value_multiply(m, m, P->Ray[r2][1+nvar+exist+i1]);
2906 /* r2 divides r => r redundant */
2907 if (value_eq(m, P->Ray[r][1+nvar+exist+i1])) {
2908 value_clear(m);
2909 return enumerate_remove_ray(P, r, exist, nparam, MaxRays);
2912 value_division(m, P->Ray[r2][1+nvar+exist+i1],
2913 P->Ray[r][1+nvar+exist+i1]);
2914 value_multiply(m, m, P->Ray[r][1+nvar+exist+i1]);
2915 /* r divides r2 => r2 redundant */
2916 if (value_eq(m, P->Ray[r2][1+nvar+exist+i1])) {
2917 value_clear(m);
2918 return enumerate_remove_ray(P, r2, exist, nparam, MaxRays);
2922 value_clear(m);
2923 return 0;
2926 static Polyhedron *upper_bound(Polyhedron *P,
2927 int pos, Value *max, Polyhedron **R)
2929 Value v;
2930 int r;
2931 value_init(v);
2933 *R = 0;
2934 Polyhedron *N;
2935 Polyhedron *B = 0;
2936 for (Polyhedron *Q = P; Q; Q = N) {
2937 N = Q->next;
2938 for (r = 0; r < P->NbRays; ++r) {
2939 if (value_zero_p(P->Ray[r][P->Dimension+1]) &&
2940 value_pos_p(P->Ray[r][1+pos]))
2941 break;
2943 if (r < P->NbRays) {
2944 Q->next = *R;
2945 *R = Q;
2946 continue;
2947 } else {
2948 Q->next = B;
2949 B = Q;
2951 for (r = 0; r < P->NbRays; ++r) {
2952 if (value_zero_p(P->Ray[r][P->Dimension+1]))
2953 continue;
2954 mpz_fdiv_q(v, P->Ray[r][1+pos], P->Ray[r][1+P->Dimension]);
2955 if ((!Q->next && r == 0) || value_gt(v, *max))
2956 value_assign(*max, v);
2959 value_clear(v);
2960 return B;
2963 static evalue* enumerate_ray(Polyhedron *P,
2964 unsigned exist, unsigned nparam, unsigned MaxRays)
2966 assert(P->NbBid == 0);
2967 int nvar = P->Dimension - exist - nparam;
2969 int r;
2970 for (r = 0; r < P->NbRays; ++r)
2971 if (value_zero_p(P->Ray[r][P->Dimension+1]))
2972 break;
2973 if (r >= P->NbRays)
2974 return 0;
2976 int r2;
2977 for (r2 = r+1; r2 < P->NbRays; ++r2)
2978 if (value_zero_p(P->Ray[r2][P->Dimension+1]))
2979 break;
2980 if (r2 < P->NbRays) {
2981 if (nvar > 0)
2982 return enumerate_sum(P, exist, nparam, MaxRays);
2985 #ifdef DEBUG_ER
2986 fprintf(stderr, "\nER: Ray\n");
2987 #endif /* DEBUG_ER */
2989 Value m;
2990 Value one;
2991 value_init(m);
2992 value_init(one);
2993 value_set_si(one, 1);
2994 int i = single_param_pos(P, exist, nparam, r);
2995 assert(i != -1); // for now;
2997 Matrix *M = Matrix_Alloc(P->NbRays, P->Dimension+2);
2998 for (int j = 0; j < P->NbRays; ++j) {
2999 Vector_Combine(P->Ray[j], P->Ray[r], M->p[j],
3000 one, P->Ray[j][P->Dimension+1], P->Dimension+2);
3002 Polyhedron *S = Rays2Polyhedron(M, MaxRays);
3003 Matrix_Free(M);
3004 Polyhedron *D = DomainDifference(P, S, MaxRays);
3005 Polyhedron_Free(S);
3006 // Polyhedron_Print(stderr, P_VALUE_FMT, D);
3007 assert(value_pos_p(P->Ray[r][1+nvar+exist+i])); // for now
3008 Polyhedron *R;
3009 D = upper_bound(D, nvar+exist+i, &m, &R);
3010 assert(D);
3011 Domain_Free(D);
3013 M = Matrix_Alloc(2, P->Dimension+2);
3014 value_set_si(M->p[0][0], 1);
3015 value_set_si(M->p[1][0], 1);
3016 value_set_si(M->p[0][1+nvar+exist+i], -1);
3017 value_set_si(M->p[1][1+nvar+exist+i], 1);
3018 value_assign(M->p[0][1+P->Dimension], m);
3019 value_oppose(M->p[1][1+P->Dimension], m);
3020 value_addto(M->p[1][1+P->Dimension], M->p[1][1+P->Dimension],
3021 P->Ray[r][1+nvar+exist+i]);
3022 value_decrement(M->p[1][1+P->Dimension], M->p[1][1+P->Dimension]);
3023 // Matrix_Print(stderr, P_VALUE_FMT, M);
3024 D = AddConstraints(M->p[0], 2, P, MaxRays);
3025 // Polyhedron_Print(stderr, P_VALUE_FMT, D);
3026 value_substract(M->p[0][1+P->Dimension], M->p[0][1+P->Dimension],
3027 P->Ray[r][1+nvar+exist+i]);
3028 // Matrix_Print(stderr, P_VALUE_FMT, M);
3029 S = AddConstraints(M->p[0], 1, P, MaxRays);
3030 // Polyhedron_Print(stderr, P_VALUE_FMT, S);
3031 Matrix_Free(M);
3033 evalue *EP = barvinok_enumerate_e(D, exist, nparam, MaxRays);
3034 Polyhedron_Free(D);
3035 value_clear(one);
3036 value_clear(m);
3038 if (value_notone_p(P->Ray[r][1+nvar+exist+i]))
3039 EP = enumerate_cyclic(P, exist, nparam, EP, r, i, MaxRays);
3040 else {
3041 M = Matrix_Alloc(1, nparam+2);
3042 value_set_si(M->p[0][0], 1);
3043 value_set_si(M->p[0][1+i], 1);
3044 enumerate_vd_add_ray(EP, M, MaxRays);
3045 Matrix_Free(M);
3048 if (!emptyQ(S)) {
3049 evalue *E = barvinok_enumerate_e(S, exist, nparam, MaxRays);
3050 eadd(E, EP);
3051 free_evalue_refs(E);
3052 free(E);
3054 Polyhedron_Free(S);
3056 if (R) {
3057 assert(nvar == 0);
3058 evalue *ER = enumerate_or(R, exist, nparam, MaxRays);
3059 eor(ER, EP);
3060 free_evalue_refs(ER);
3061 free(ER);
3064 return EP;
3067 static evalue* new_zero_ep()
3069 evalue *EP;
3070 ALLOC(evalue, EP);
3071 value_init(EP->d);
3072 evalue_set_si(EP, 0, 1);
3073 return EP;
3076 static evalue* enumerate_vd(Polyhedron **PA,
3077 unsigned exist, unsigned nparam, unsigned MaxRays)
3079 Polyhedron *P = *PA;
3080 int nvar = P->Dimension - exist - nparam;
3081 Param_Polyhedron *PP = NULL;
3082 Polyhedron *C = Universe_Polyhedron(nparam);
3083 Polyhedron *CEq;
3084 Matrix *CT;
3085 Polyhedron *PR = P;
3086 PP = Polyhedron2Param_SimplifiedDomain(&PR,C,MaxRays,&CEq,&CT);
3087 Polyhedron_Free(C);
3089 int nd;
3090 Param_Domain *D, *last;
3091 Value c;
3092 value_init(c);
3093 for (nd = 0, D=PP->D; D; D=D->next, ++nd)
3096 Polyhedron **VD = new Polyhedron_p[nd];
3097 Polyhedron **fVD = new Polyhedron_p[nd];
3098 for(nd = 0, D=PP->D; D; D=D->next) {
3099 Polyhedron *rVD = reduce_domain(D->Domain, CT, CEq,
3100 fVD, nd, MaxRays);
3101 if (!rVD)
3102 continue;
3104 VD[nd++] = rVD;
3105 last = D;
3108 evalue *EP = 0;
3110 if (nd == 0)
3111 EP = new_zero_ep();
3113 /* This doesn't seem to have any effect */
3114 if (nd == 1) {
3115 Polyhedron *CA = align_context(VD[0], P->Dimension, MaxRays);
3116 Polyhedron *O = P;
3117 P = DomainIntersection(P, CA, MaxRays);
3118 if (O != *PA)
3119 Polyhedron_Free(O);
3120 Polyhedron_Free(CA);
3121 if (emptyQ(P))
3122 EP = new_zero_ep();
3125 if (!EP && CT->NbColumns != CT->NbRows) {
3126 Polyhedron *CEqr = DomainImage(CEq, CT, MaxRays);
3127 Polyhedron *CA = align_context(CEqr, PR->Dimension, MaxRays);
3128 Polyhedron *I = DomainIntersection(PR, CA, MaxRays);
3129 Polyhedron_Free(CEqr);
3130 Polyhedron_Free(CA);
3131 #ifdef DEBUG_ER
3132 fprintf(stderr, "\nER: Eliminate\n");
3133 #endif /* DEBUG_ER */
3134 nparam -= CT->NbColumns - CT->NbRows;
3135 EP = barvinok_enumerate_e(I, exist, nparam, MaxRays);
3136 nparam += CT->NbColumns - CT->NbRows;
3137 addeliminatedparams_enum(EP, CT, CEq, MaxRays, nparam);
3138 Polyhedron_Free(I);
3140 if (PR != *PA)
3141 Polyhedron_Free(PR);
3142 PR = 0;
3144 if (!EP && nd > 1) {
3145 #ifdef DEBUG_ER
3146 fprintf(stderr, "\nER: VD\n");
3147 #endif /* DEBUG_ER */
3148 for (int i = 0; i < nd; ++i) {
3149 Polyhedron *CA = align_context(VD[i], P->Dimension, MaxRays);
3150 Polyhedron *I = DomainIntersection(P, CA, MaxRays);
3152 if (i == 0)
3153 EP = barvinok_enumerate_e(I, exist, nparam, MaxRays);
3154 else {
3155 evalue *E = barvinok_enumerate_e(I, exist, nparam, MaxRays);
3156 eadd(E, EP);
3157 free_evalue_refs(E);
3158 free(E);
3160 Polyhedron_Free(I);
3161 Polyhedron_Free(CA);
3165 for (int i = 0; i < nd; ++i) {
3166 Polyhedron_Free(VD[i]);
3167 Polyhedron_Free(fVD[i]);
3169 delete [] VD;
3170 delete [] fVD;
3171 value_clear(c);
3173 if (!EP && nvar == 0) {
3174 Value f;
3175 value_init(f);
3176 Param_Vertices *V, *V2;
3177 Matrix* M = Matrix_Alloc(1, P->Dimension+2);
3179 FORALL_PVertex_in_ParamPolyhedron(V, last, PP) {
3180 bool found = false;
3181 FORALL_PVertex_in_ParamPolyhedron(V2, last, PP) {
3182 if (V == V2) {
3183 found = true;
3184 continue;
3186 if (!found)
3187 continue;
3188 for (int i = 0; i < exist; ++i) {
3189 value_oppose(f, V->Vertex->p[i][nparam+1]);
3190 Vector_Combine(V->Vertex->p[i],
3191 V2->Vertex->p[i],
3192 M->p[0] + 1 + nvar + exist,
3193 V2->Vertex->p[i][nparam+1],
3195 nparam+1);
3196 int j;
3197 for (j = 0; j < nparam; ++j)
3198 if (value_notzero_p(M->p[0][1+nvar+exist+j]))
3199 break;
3200 if (j >= nparam)
3201 continue;
3202 ConstraintSimplify(M->p[0], M->p[0],
3203 P->Dimension+2, &f);
3204 value_set_si(M->p[0][0], 0);
3205 Polyhedron *para = AddConstraints(M->p[0], 1, P,
3206 MaxRays);
3207 if (emptyQ(para)) {
3208 Polyhedron_Free(para);
3209 continue;
3211 Polyhedron *pos, *neg;
3212 value_set_si(M->p[0][0], 1);
3213 value_decrement(M->p[0][P->Dimension+1],
3214 M->p[0][P->Dimension+1]);
3215 neg = AddConstraints(M->p[0], 1, P, MaxRays);
3216 value_set_si(f, -1);
3217 Vector_Scale(M->p[0]+1, M->p[0]+1, f,
3218 P->Dimension+1);
3219 value_decrement(M->p[0][P->Dimension+1],
3220 M->p[0][P->Dimension+1]);
3221 value_decrement(M->p[0][P->Dimension+1],
3222 M->p[0][P->Dimension+1]);
3223 pos = AddConstraints(M->p[0], 1, P, MaxRays);
3224 if (emptyQ(neg) && emptyQ(pos)) {
3225 Polyhedron_Free(para);
3226 Polyhedron_Free(pos);
3227 Polyhedron_Free(neg);
3228 continue;
3230 #ifdef DEBUG_ER
3231 fprintf(stderr, "\nER: Order\n");
3232 #endif /* DEBUG_ER */
3233 EP = barvinok_enumerate_e(para, exist, nparam, MaxRays);
3234 evalue *E;
3235 if (!emptyQ(pos)) {
3236 E = barvinok_enumerate_e(pos, exist, nparam, MaxRays);
3237 eadd(E, EP);
3238 free_evalue_refs(E);
3239 free(E);
3241 if (!emptyQ(neg)) {
3242 E = barvinok_enumerate_e(neg, exist, nparam, MaxRays);
3243 eadd(E, EP);
3244 free_evalue_refs(E);
3245 free(E);
3247 Polyhedron_Free(para);
3248 Polyhedron_Free(pos);
3249 Polyhedron_Free(neg);
3250 break;
3252 if (EP)
3253 break;
3254 } END_FORALL_PVertex_in_ParamPolyhedron;
3255 if (EP)
3256 break;
3257 } END_FORALL_PVertex_in_ParamPolyhedron;
3259 if (!EP) {
3260 /* Search for vertex coordinate to split on */
3261 /* First look for one independent of the parameters */
3262 FORALL_PVertex_in_ParamPolyhedron(V, last, PP) {
3263 for (int i = 0; i < exist; ++i) {
3264 int j;
3265 for (j = 0; j < nparam; ++j)
3266 if (value_notzero_p(V->Vertex->p[i][j]))
3267 break;
3268 if (j < nparam)
3269 continue;
3270 value_set_si(M->p[0][0], 1);
3271 Vector_Set(M->p[0]+1, 0, nvar+exist);
3272 Vector_Copy(V->Vertex->p[i],
3273 M->p[0] + 1 + nvar + exist, nparam+1);
3274 value_oppose(M->p[0][1+nvar+i],
3275 V->Vertex->p[i][nparam+1]);
3277 Polyhedron *pos, *neg;
3278 value_set_si(M->p[0][0], 1);
3279 value_decrement(M->p[0][P->Dimension+1],
3280 M->p[0][P->Dimension+1]);
3281 neg = AddConstraints(M->p[0], 1, P, MaxRays);
3282 value_set_si(f, -1);
3283 Vector_Scale(M->p[0]+1, M->p[0]+1, f,
3284 P->Dimension+1);
3285 value_decrement(M->p[0][P->Dimension+1],
3286 M->p[0][P->Dimension+1]);
3287 value_decrement(M->p[0][P->Dimension+1],
3288 M->p[0][P->Dimension+1]);
3289 pos = AddConstraints(M->p[0], 1, P, MaxRays);
3290 if (emptyQ(neg) || emptyQ(pos)) {
3291 Polyhedron_Free(pos);
3292 Polyhedron_Free(neg);
3293 continue;
3295 Polyhedron_Free(pos);
3296 value_increment(M->p[0][P->Dimension+1],
3297 M->p[0][P->Dimension+1]);
3298 pos = AddConstraints(M->p[0], 1, P, MaxRays);
3299 #ifdef DEBUG_ER
3300 fprintf(stderr, "\nER: Vertex\n");
3301 #endif /* DEBUG_ER */
3302 pos->next = neg;
3303 EP = enumerate_or(pos, exist, nparam, MaxRays);
3304 break;
3306 if (EP)
3307 break;
3308 } END_FORALL_PVertex_in_ParamPolyhedron;
3311 if (!EP) {
3312 /* Search for vertex coordinate to split on */
3313 /* Now look for one that depends on the parameters */
3314 FORALL_PVertex_in_ParamPolyhedron(V, last, PP) {
3315 for (int i = 0; i < exist; ++i) {
3316 value_set_si(M->p[0][0], 1);
3317 Vector_Set(M->p[0]+1, 0, nvar+exist);
3318 Vector_Copy(V->Vertex->p[i],
3319 M->p[0] + 1 + nvar + exist, nparam+1);
3320 value_oppose(M->p[0][1+nvar+i],
3321 V->Vertex->p[i][nparam+1]);
3323 Polyhedron *pos, *neg;
3324 value_set_si(M->p[0][0], 1);
3325 value_decrement(M->p[0][P->Dimension+1],
3326 M->p[0][P->Dimension+1]);
3327 neg = AddConstraints(M->p[0], 1, P, MaxRays);
3328 value_set_si(f, -1);
3329 Vector_Scale(M->p[0]+1, M->p[0]+1, f,
3330 P->Dimension+1);
3331 value_decrement(M->p[0][P->Dimension+1],
3332 M->p[0][P->Dimension+1]);
3333 value_decrement(M->p[0][P->Dimension+1],
3334 M->p[0][P->Dimension+1]);
3335 pos = AddConstraints(M->p[0], 1, P, MaxRays);
3336 if (emptyQ(neg) || emptyQ(pos)) {
3337 Polyhedron_Free(pos);
3338 Polyhedron_Free(neg);
3339 continue;
3341 Polyhedron_Free(pos);
3342 value_increment(M->p[0][P->Dimension+1],
3343 M->p[0][P->Dimension+1]);
3344 pos = AddConstraints(M->p[0], 1, P, MaxRays);
3345 #ifdef DEBUG_ER
3346 fprintf(stderr, "\nER: ParamVertex\n");
3347 #endif /* DEBUG_ER */
3348 pos->next = neg;
3349 EP = enumerate_or(pos, exist, nparam, MaxRays);
3350 break;
3352 if (EP)
3353 break;
3354 } END_FORALL_PVertex_in_ParamPolyhedron;
3357 Matrix_Free(M);
3358 value_clear(f);
3361 if (CEq)
3362 Polyhedron_Free(CEq);
3363 if (CT)
3364 Matrix_Free(CT);
3365 if (PP)
3366 Param_Polyhedron_Free(PP);
3367 *PA = P;
3369 return EP;
3372 #ifndef HAVE_PIPLIB
3373 evalue *barvinok_enumerate_pip(Polyhedron *P,
3374 unsigned exist, unsigned nparam, unsigned MaxRays)
3376 return 0;
3378 #else
3379 evalue *barvinok_enumerate_pip(Polyhedron *P,
3380 unsigned exist, unsigned nparam, unsigned MaxRays)
3382 int nvar = P->Dimension - exist - nparam;
3383 evalue *EP = new_zero_ep();
3384 Polyhedron *Q, *N, *T = 0;
3385 Value min, tmp;
3386 value_init(min);
3387 value_init(tmp);
3389 #ifdef DEBUG_ER
3390 fprintf(stderr, "\nER: PIP\n");
3391 #endif /* DEBUG_ER */
3393 for (int i = 0; i < P->Dimension; ++i) {
3394 bool pos = false;
3395 bool neg = false;
3396 bool posray = false;
3397 bool negray = false;
3398 value_set_si(min, 0);
3399 for (int j = 0; j < P->NbRays; ++j) {
3400 if (value_pos_p(P->Ray[j][1+i])) {
3401 pos = true;
3402 if (value_zero_p(P->Ray[j][1+P->Dimension]))
3403 posray = true;
3404 } else if (value_neg_p(P->Ray[j][1+i])) {
3405 neg = true;
3406 if (value_zero_p(P->Ray[j][1+P->Dimension]))
3407 negray = true;
3408 else {
3409 mpz_fdiv_q(tmp,
3410 P->Ray[j][1+i], P->Ray[j][1+P->Dimension]);
3411 if (value_lt(tmp, min))
3412 value_assign(min, tmp);
3416 if (pos && neg) {
3417 assert(!(posray && negray));
3418 assert(!negray); // for now
3419 Polyhedron *O = T ? T : P;
3420 /* shift by a safe amount */
3421 Matrix *M = Matrix_Alloc(O->NbRays, O->Dimension+2);
3422 Vector_Copy(O->Ray[0], M->p[0], O->NbRays * (O->Dimension+2));
3423 for (int j = 0; j < P->NbRays; ++j) {
3424 if (value_notzero_p(M->p[j][1+P->Dimension])) {
3425 value_multiply(tmp, min, M->p[j][1+P->Dimension]);
3426 value_substract(M->p[j][1+i], M->p[j][1+i], tmp);
3429 if (T)
3430 Polyhedron_Free(T);
3431 T = Rays2Polyhedron(M, MaxRays);
3432 Matrix_Free(M);
3433 } else if (neg) {
3434 /* negating a parameter requires that we substitute in the
3435 * sign again afterwards.
3436 * Disallow for now.
3438 assert(i < nvar+exist);
3439 if (!T)
3440 T = Polyhedron_Copy(P);
3441 for (int j = 0; j < T->NbRays; ++j)
3442 value_oppose(T->Ray[j][1+i], T->Ray[j][1+i]);
3443 for (int j = 0; j < T->NbConstraints; ++j)
3444 value_oppose(T->Constraint[j][1+i], T->Constraint[j][1+i]);
3447 value_clear(min);
3448 value_clear(tmp);
3450 Polyhedron *D = pip_lexmin(T ? T : P, exist, nparam);
3451 for (Q = D; Q; Q = N) {
3452 N = Q->next;
3453 Q->next = 0;
3454 evalue *E;
3455 exist = Q->Dimension - nvar - nparam;
3456 E = barvinok_enumerate_e(Q, exist, nparam, MaxRays);
3457 Polyhedron_Free(Q);
3458 eadd(E, EP);
3459 free_evalue_refs(E);
3460 free(E);
3463 if (T)
3464 Polyhedron_Free(T);
3466 return EP;
3468 #endif
3471 static bool is_single(Value *row, int pos, int len)
3473 return First_Non_Zero(row, pos) == -1 &&
3474 First_Non_Zero(row+pos+1, len-pos-1) == -1;
3477 static evalue* barvinok_enumerate_e_r(Polyhedron *P,
3478 unsigned exist, unsigned nparam, unsigned MaxRays);
3480 #ifdef DEBUG_ER
3481 static int er_level = 0;
3483 evalue* barvinok_enumerate_e(Polyhedron *P,
3484 unsigned exist, unsigned nparam, unsigned MaxRays)
3486 fprintf(stderr, "\nER: level %i\n", er_level);
3487 int nvar = P->Dimension - exist - nparam;
3488 fprintf(stderr, "%d %d %d\n", nvar, exist, nparam);
3490 Polyhedron_Print(stderr, P_VALUE_FMT, P);
3491 ++er_level;
3492 P = DomainConstraintSimplify(Polyhedron_Copy(P), MaxRays);
3493 evalue *EP = barvinok_enumerate_e_r(P, exist, nparam, MaxRays);
3494 Polyhedron_Free(P);
3495 --er_level;
3496 return EP;
3498 #else
3499 evalue* barvinok_enumerate_e(Polyhedron *P,
3500 unsigned exist, unsigned nparam, unsigned MaxRays)
3502 P = DomainConstraintSimplify(Polyhedron_Copy(P), MaxRays);
3503 evalue *EP = barvinok_enumerate_e_r(P, exist, nparam, MaxRays);
3504 Polyhedron_Free(P);
3505 return EP;
3507 #endif
3509 static evalue* barvinok_enumerate_e_r(Polyhedron *P,
3510 unsigned exist, unsigned nparam, unsigned MaxRays)
3512 if (exist == 0) {
3513 Polyhedron *U = Universe_Polyhedron(nparam);
3514 evalue *EP = barvinok_enumerate_ev(P, U, MaxRays);
3515 //char *param_name[] = {"P", "Q", "R", "S", "T" };
3516 //print_evalue(stdout, EP, param_name);
3517 Polyhedron_Free(U);
3518 return EP;
3521 int nvar = P->Dimension - exist - nparam;
3522 int len = P->Dimension + 2;
3524 if (emptyQ(P))
3525 return new_zero_ep();
3527 if (nvar == 0 && nparam == 0) {
3528 evalue *EP = new_zero_ep();
3529 barvinok_count(P, &EP->x.n, MaxRays);
3530 if (value_pos_p(EP->x.n))
3531 value_set_si(EP->x.n, 1);
3532 return EP;
3535 int r;
3536 for (r = 0; r < P->NbRays; ++r)
3537 if (value_zero_p(P->Ray[r][0]) ||
3538 value_zero_p(P->Ray[r][P->Dimension+1])) {
3539 int i;
3540 for (i = 0; i < nvar; ++i)
3541 if (value_notzero_p(P->Ray[r][i+1]))
3542 break;
3543 if (i >= nvar)
3544 continue;
3545 for (i = nvar + exist; i < nvar + exist + nparam; ++i)
3546 if (value_notzero_p(P->Ray[r][i+1]))
3547 break;
3548 if (i >= nvar + exist + nparam)
3549 break;
3551 if (r < P->NbRays) {
3552 evalue *EP = new_zero_ep();
3553 value_set_si(EP->x.n, -1);
3554 return EP;
3557 int first;
3558 for (r = 0; r < P->NbEq; ++r)
3559 if ((first = First_Non_Zero(P->Constraint[r]+1+nvar, exist)) != -1)
3560 break;
3561 if (r < P->NbEq) {
3562 if (First_Non_Zero(P->Constraint[r]+1+nvar+first+1,
3563 exist-first-1) != -1) {
3564 Polyhedron *T = rotate_along(P, r, nvar, exist, MaxRays);
3565 #ifdef DEBUG_ER
3566 fprintf(stderr, "\nER: Equality\n");
3567 #endif /* DEBUG_ER */
3568 evalue *EP = barvinok_enumerate_e(T, exist-1, nparam, MaxRays);
3569 Polyhedron_Free(T);
3570 return EP;
3571 } else {
3572 #ifdef DEBUG_ER
3573 fprintf(stderr, "\nER: Fixed\n");
3574 #endif /* DEBUG_ER */
3575 if (first == 0)
3576 return barvinok_enumerate_e(P, exist-1, nparam, MaxRays);
3577 else {
3578 Polyhedron *T = Polyhedron_Copy(P);
3579 SwapColumns(T, nvar+1, nvar+1+first);
3580 evalue *EP = barvinok_enumerate_e(T, exist-1, nparam, MaxRays);
3581 Polyhedron_Free(T);
3582 return EP;
3587 Vector *row = Vector_Alloc(len);
3588 value_set_si(row->p[0], 1);
3590 Value f;
3591 value_init(f);
3593 enum constraint* info = new constraint[exist];
3594 for (int i = 0; i < exist; ++i) {
3595 info[i] = ALL_POS;
3596 for (int l = P->NbEq; l < P->NbConstraints; ++l) {
3597 if (value_negz_p(P->Constraint[l][nvar+i+1]))
3598 continue;
3599 bool l_parallel = is_single(P->Constraint[l]+nvar+1, i, exist);
3600 for (int u = P->NbEq; u < P->NbConstraints; ++u) {
3601 if (value_posz_p(P->Constraint[u][nvar+i+1]))
3602 continue;
3603 bool lu_parallel = l_parallel ||
3604 is_single(P->Constraint[u]+nvar+1, i, exist);
3605 value_oppose(f, P->Constraint[u][nvar+i+1]);
3606 Vector_Combine(P->Constraint[l]+1, P->Constraint[u]+1, row->p+1,
3607 f, P->Constraint[l][nvar+i+1], len-1);
3608 if (!(info[i] & INDEPENDENT)) {
3609 int j;
3610 for (j = 0; j < exist; ++j)
3611 if (j != i && value_notzero_p(row->p[nvar+j+1]))
3612 break;
3613 if (j == exist) {
3614 //printf("independent: i: %d, l: %d, u: %d\n", i, l, u);
3615 info[i] = (constraint)(info[i] | INDEPENDENT);
3618 if (info[i] & ALL_POS) {
3619 value_addto(row->p[len-1], row->p[len-1],
3620 P->Constraint[l][nvar+i+1]);
3621 value_addto(row->p[len-1], row->p[len-1], f);
3622 value_multiply(f, f, P->Constraint[l][nvar+i+1]);
3623 value_substract(row->p[len-1], row->p[len-1], f);
3624 value_decrement(row->p[len-1], row->p[len-1]);
3625 ConstraintSimplify(row->p, row->p, len, &f);
3626 value_set_si(f, -1);
3627 Vector_Scale(row->p+1, row->p+1, f, len-1);
3628 value_decrement(row->p[len-1], row->p[len-1]);
3629 Polyhedron *T = AddConstraints(row->p, 1, P, MaxRays);
3630 if (!emptyQ(T)) {
3631 //printf("not all_pos: i: %d, l: %d, u: %d\n", i, l, u);
3632 info[i] = (constraint)(info[i] ^ ALL_POS);
3634 //puts("pos remainder");
3635 //Polyhedron_Print(stdout, P_VALUE_FMT, T);
3636 Polyhedron_Free(T);
3638 if (!(info[i] & ONE_NEG)) {
3639 if (lu_parallel) {
3640 negative_test_constraint(P->Constraint[l],
3641 P->Constraint[u],
3642 row->p, nvar+i, len, &f);
3643 oppose_constraint(row->p, len, &f);
3644 Polyhedron *T = AddConstraints(row->p, 1, P, MaxRays);
3645 if (emptyQ(T)) {
3646 //printf("one_neg i: %d, l: %d, u: %d\n", i, l, u);
3647 info[i] = (constraint)(info[i] | ONE_NEG);
3649 //puts("neg remainder");
3650 //Polyhedron_Print(stdout, P_VALUE_FMT, T);
3651 Polyhedron_Free(T);
3652 } else if (!(info[i] & ROT_NEG)) {
3653 if (parallel_constraints(P->Constraint[l],
3654 P->Constraint[u],
3655 row->p, nvar, exist)) {
3656 negative_test_constraint7(P->Constraint[l],
3657 P->Constraint[u],
3658 row->p, nvar, exist,
3659 len, &f);
3660 oppose_constraint(row->p, len, &f);
3661 Polyhedron *T = AddConstraints(row->p, 1, P, MaxRays);
3662 if (emptyQ(T)) {
3663 // printf("rot_neg i: %d, l: %d, u: %d\n", i, l, u);
3664 info[i] = (constraint)(info[i] | ROT_NEG);
3665 r = l;
3667 //puts("neg remainder");
3668 //Polyhedron_Print(stdout, P_VALUE_FMT, T);
3669 Polyhedron_Free(T);
3673 if (!(info[i] & ALL_POS) && (info[i] & (ONE_NEG | ROT_NEG)))
3674 goto next;
3677 if (info[i] & ALL_POS)
3678 break;
3679 next:
3684 for (int i = 0; i < exist; ++i)
3685 printf("%i: %i\n", i, info[i]);
3687 for (int i = 0; i < exist; ++i)
3688 if (info[i] & ALL_POS) {
3689 #ifdef DEBUG_ER
3690 fprintf(stderr, "\nER: Positive\n");
3691 #endif /* DEBUG_ER */
3692 // Eliminate
3693 // Maybe we should chew off some of the fat here
3694 Matrix *M = Matrix_Alloc(P->Dimension, P->Dimension+1);
3695 for (int j = 0; j < P->Dimension; ++j)
3696 value_set_si(M->p[j][j + (j >= i+nvar)], 1);
3697 Polyhedron *T = Polyhedron_Image(P, M, MaxRays);
3698 Matrix_Free(M);
3699 evalue *EP = barvinok_enumerate_e(T, exist-1, nparam, MaxRays);
3700 Polyhedron_Free(T);
3701 value_clear(f);
3702 Vector_Free(row);
3703 delete [] info;
3704 return EP;
3706 for (int i = 0; i < exist; ++i)
3707 if (info[i] & ONE_NEG) {
3708 #ifdef DEBUG_ER
3709 fprintf(stderr, "\nER: Negative\n");
3710 #endif /* DEBUG_ER */
3711 Vector_Free(row);
3712 value_clear(f);
3713 delete [] info;
3714 if (i == 0)
3715 return barvinok_enumerate_e(P, exist-1, nparam, MaxRays);
3716 else {
3717 Polyhedron *T = Polyhedron_Copy(P);
3718 SwapColumns(T, nvar+1, nvar+1+i);
3719 evalue *EP = barvinok_enumerate_e(T, exist-1, nparam, MaxRays);
3720 Polyhedron_Free(T);
3721 return EP;
3724 for (int i = 0; i < exist; ++i)
3725 if (info[i] & ROT_NEG) {
3726 #ifdef DEBUG_ER
3727 fprintf(stderr, "\nER: Rotate\n");
3728 #endif /* DEBUG_ER */
3729 Vector_Free(row);
3730 value_clear(f);
3731 delete [] info;
3732 Polyhedron *T = rotate_along(P, r, nvar, exist, MaxRays);
3733 evalue *EP = barvinok_enumerate_e(T, exist-1, nparam, MaxRays);
3734 Polyhedron_Free(T);
3735 return EP;
3737 for (int i = 0; i < exist; ++i)
3738 if (info[i] & INDEPENDENT) {
3739 Polyhedron *pos, *neg;
3741 /* Find constraint again and split off negative part */
3743 if (SplitOnVar(P, i, nvar, len, exist, MaxRays,
3744 row, f, true, &pos, &neg)) {
3745 #ifdef DEBUG_ER
3746 fprintf(stderr, "\nER: Split\n");
3747 #endif /* DEBUG_ER */
3749 evalue *EP =
3750 barvinok_enumerate_e(neg, exist-1, nparam, MaxRays);
3751 evalue *E =
3752 barvinok_enumerate_e(pos, exist, nparam, MaxRays);
3753 eadd(E, EP);
3754 free_evalue_refs(E);
3755 free(E);
3756 Polyhedron_Free(neg);
3757 Polyhedron_Free(pos);
3758 value_clear(f);
3759 Vector_Free(row);
3760 delete [] info;
3761 return EP;
3764 delete [] info;
3766 Polyhedron *O = P;
3767 Polyhedron *F;
3769 evalue *EP;
3771 EP = enumerate_line(P, exist, nparam, MaxRays);
3772 if (EP)
3773 goto out;
3775 EP = barvinok_enumerate_pip(P, exist, nparam, MaxRays);
3776 if (EP)
3777 goto out;
3779 EP = enumerate_redundant_ray(P, exist, nparam, MaxRays);
3780 if (EP)
3781 goto out;
3783 EP = enumerate_sure(P, exist, nparam, MaxRays);
3784 if (EP)
3785 goto out;
3787 EP = enumerate_ray(P, exist, nparam, MaxRays);
3788 if (EP)
3789 goto out;
3791 EP = enumerate_sure2(P, exist, nparam, MaxRays);
3792 if (EP)
3793 goto out;
3795 F = unfringe(P, MaxRays);
3796 if (!PolyhedronIncludes(F, P)) {
3797 #ifdef DEBUG_ER
3798 fprintf(stderr, "\nER: Fringed\n");
3799 #endif /* DEBUG_ER */
3800 EP = barvinok_enumerate_e(F, exist, nparam, MaxRays);
3801 Polyhedron_Free(F);
3802 goto out;
3804 Polyhedron_Free(F);
3806 if (nparam)
3807 EP = enumerate_vd(&P, exist, nparam, MaxRays);
3808 if (EP)
3809 goto out2;
3811 if (nvar != 0) {
3812 EP = enumerate_sum(P, exist, nparam, MaxRays);
3813 goto out2;
3816 assert(nvar == 0);
3818 int i;
3819 Polyhedron *pos, *neg;
3820 for (i = 0; i < exist; ++i)
3821 if (SplitOnVar(P, i, nvar, len, exist, MaxRays,
3822 row, f, false, &pos, &neg))
3823 break;
3825 assert (i < exist);
3827 pos->next = neg;
3828 EP = enumerate_or(pos, exist, nparam, MaxRays);
3830 out2:
3831 if (O != P)
3832 Polyhedron_Free(P);
3834 out:
3835 value_clear(f);
3836 Vector_Free(row);
3837 return EP;
3840 gen_fun * barvinok_series(Polyhedron *P, Polyhedron* C, unsigned MaxRays)
3842 Polyhedron ** vcone;
3843 Polyhedron *CA;
3844 unsigned nparam = C->Dimension;
3845 unsigned dim, nvar;
3846 vec_ZZ sign;
3847 int ncone = 0;
3848 sign.SetLength(ncone);
3850 CA = align_context(C, P->Dimension, MaxRays);
3851 P = DomainIntersection(P, CA, MaxRays);
3852 Polyhedron_Free(CA);
3854 assert(!Polyhedron_is_infinite(P, nparam));
3855 assert(P->NbBid == 0);
3856 assert(Polyhedron_has_positive_rays(P, nparam));
3857 assert(P->NbEq == 0);
3859 partial_reducer red(P, nparam);
3860 red.start(MaxRays);
3861 return red.gf;