dead code elimination
[barvinok.git] / barvinok.cc
blobb84b38af99122d722fa3a4c94d8bf0b40de9978f
1 #include <assert.h>
2 #include <iostream>
3 #include <vector>
4 #include <deque>
5 #include <string>
6 #include <sstream>
7 #include <gmp.h>
8 #include <NTL/mat_ZZ.h>
9 #include <NTL/LLL.h>
10 #include <util.h>
11 extern "C" {
12 #include <polylib/polylibgmp.h>
13 #include "ev_operations.h"
14 #include "piputil.h"
16 #include "config.h"
17 #include <barvinok.h>
18 #include <genfun.h>
20 #ifdef NTL_STD_CXX
21 using namespace NTL;
22 #endif
23 using std::cout;
24 using std::endl;
25 using std::vector;
26 using std::deque;
27 using std::string;
28 using std::ostringstream;
30 #define ALLOC(p) (((long *) (p))[0])
31 #define SIZE(p) (((long *) (p))[1])
32 #define DATA(p) ((mp_limb_t *) (((long *) (p)) + 2))
34 static void value2zz(Value v, ZZ& z)
36 int sa = v[0]._mp_size;
37 int abs_sa = sa < 0 ? -sa : sa;
39 _ntl_gsetlength(&z.rep, abs_sa);
40 mp_limb_t * adata = DATA(z.rep);
41 for (int i = 0; i < abs_sa; ++i)
42 adata[i] = v[0]._mp_d[i];
43 SIZE(z.rep) = sa;
46 void zz2value(ZZ& z, Value& v)
48 if (!z.rep) {
49 value_set_si(v, 0);
50 return;
53 int sa = SIZE(z.rep);
54 int abs_sa = sa < 0 ? -sa : sa;
56 mp_limb_t * adata = DATA(z.rep);
57 _mpz_realloc(v, abs_sa);
58 for (int i = 0; i < abs_sa; ++i)
59 v[0]._mp_d[i] = adata[i];
60 v[0]._mp_size = sa;
63 #undef ALLOC
64 #define ALLOC(t,p) p = (t*)malloc(sizeof(*p))
67 * We just ignore the last column and row
68 * If the final element is not equal to one
69 * then the result will actually be a multiple of the input
71 static void matrix2zz(Matrix *M, mat_ZZ& m, unsigned nr, unsigned nc)
73 m.SetDims(nr, nc);
75 for (int i = 0; i < nr; ++i) {
76 // assert(value_one_p(M->p[i][M->NbColumns - 1]));
77 for (int j = 0; j < nc; ++j) {
78 value2zz(M->p[i][j], m[i][j]);
83 static void values2zz(Value *p, vec_ZZ& v, int len)
85 v.SetLength(len);
87 for (int i = 0; i < len; ++i) {
88 value2zz(p[i], v[i]);
94 static void zz2values(vec_ZZ& v, Value *p)
96 for (int i = 0; i < v.length(); ++i)
97 zz2value(v[i], p[i]);
100 static void rays(mat_ZZ& r, Polyhedron *C)
102 unsigned dim = C->NbRays - 1; /* don't count zero vertex */
103 assert(C->NbRays - 1 == C->Dimension);
104 r.SetDims(dim, dim);
105 ZZ tmp;
107 int i, c;
108 for (i = 0, c = 0; i < dim; ++i)
109 if (value_zero_p(C->Ray[i][dim+1])) {
110 for (int j = 0; j < dim; ++j) {
111 value2zz(C->Ray[i][j+1], tmp);
112 r[j][c] = tmp;
114 ++c;
118 static Matrix * rays(Polyhedron *C)
120 unsigned dim = C->NbRays - 1; /* don't count zero vertex */
121 assert(C->NbRays - 1 == C->Dimension);
123 Matrix *M = Matrix_Alloc(dim+1, dim+1);
124 assert(M);
126 int i, c;
127 for (i = 0, c = 0; i <= dim && c < dim; ++i)
128 if (value_zero_p(C->Ray[i][dim+1])) {
129 Vector_Copy(C->Ray[i] + 1, M->p[c], dim);
130 value_set_si(M->p[c++][dim], 0);
132 assert(c == dim);
133 value_set_si(M->p[dim][dim], 1);
135 return M;
138 static Matrix * rays2(Polyhedron *C)
140 unsigned dim = C->NbRays - 1; /* don't count zero vertex */
141 assert(C->NbRays - 1 == C->Dimension);
143 Matrix *M = Matrix_Alloc(dim, dim);
144 assert(M);
146 int i, c;
147 for (i = 0, c = 0; i <= dim && c < dim; ++i)
148 if (value_zero_p(C->Ray[i][dim+1]))
149 Vector_Copy(C->Ray[i] + 1, M->p[c++], dim);
150 assert(c == dim);
152 return M;
156 * Returns the largest absolute value in the vector
158 static ZZ max(vec_ZZ& v)
160 ZZ max = abs(v[0]);
161 for (int i = 1; i < v.length(); ++i)
162 if (abs(v[i]) > max)
163 max = abs(v[i]);
164 return max;
167 class cone {
168 public:
169 cone(Matrix *M) {
170 Cone = 0;
171 Rays = Matrix_Copy(M);
172 set_det();
174 cone(Polyhedron *C) {
175 Cone = Polyhedron_Copy(C);
176 Rays = rays(C);
177 set_det();
179 void set_det() {
180 mat_ZZ A;
181 matrix2zz(Rays, A, Rays->NbRows - 1, Rays->NbColumns - 1);
182 det = determinant(A);
185 Vector* short_vector(vec_ZZ& lambda) {
186 Matrix *M = Matrix_Copy(Rays);
187 Matrix *inv = Matrix_Alloc(M->NbRows, M->NbColumns);
188 int ok = Matrix_Inverse(M, inv);
189 assert(ok);
190 Matrix_Free(M);
192 ZZ det2;
193 mat_ZZ B;
194 mat_ZZ U;
195 matrix2zz(inv, B, inv->NbRows - 1, inv->NbColumns - 1);
196 long r = LLL(det2, B, U);
198 ZZ min = max(B[0]);
199 int index = 0;
200 for (int i = 1; i < B.NumRows(); ++i) {
201 ZZ tmp = max(B[i]);
202 if (tmp < min) {
203 min = tmp;
204 index = i;
208 Matrix_Free(inv);
210 lambda = B[index];
212 Vector *z = Vector_Alloc(U[index].length()+1);
213 assert(z);
214 zz2values(U[index], z->p);
215 value_set_si(z->p[U[index].length()], 0);
217 Value tmp;
218 value_init(tmp);
219 Polyhedron *C = poly();
220 int i;
221 for (i = 0; i < C->NbConstraints; ++i) {
222 Inner_Product(z->p, C->Constraint[i]+1, z->Size-1, &tmp);
223 if (value_pos_p(tmp))
224 break;
226 if (i == C->NbConstraints) {
227 value_set_si(tmp, -1);
228 Vector_Scale(z->p, z->p, tmp, z->Size-1);
230 value_clear(tmp);
231 return z;
234 ~cone() {
235 Polyhedron_Free(Cone);
236 Matrix_Free(Rays);
239 Polyhedron *poly() {
240 if (!Cone) {
241 Matrix *M = Matrix_Alloc(Rays->NbRows+1, Rays->NbColumns+1);
242 for (int i = 0; i < Rays->NbRows; ++i) {
243 Vector_Copy(Rays->p[i], M->p[i]+1, Rays->NbColumns);
244 value_set_si(M->p[i][0], 1);
246 Vector_Set(M->p[Rays->NbRows]+1, 0, Rays->NbColumns-1);
247 value_set_si(M->p[Rays->NbRows][0], 1);
248 value_set_si(M->p[Rays->NbRows][Rays->NbColumns], 1);
249 Cone = Rays2Polyhedron(M, M->NbRows+1);
250 assert(Cone->NbConstraints == Cone->NbRays);
251 Matrix_Free(M);
253 return Cone;
256 ZZ det;
257 Polyhedron *Cone;
258 Matrix *Rays;
261 class dpoly {
262 public:
263 vec_ZZ coeff;
264 dpoly(int d, ZZ& degree, int offset = 0) {
265 coeff.SetLength(d+1);
267 int min = d + offset;
268 if (degree >= 0 && degree < ZZ(INIT_VAL, min))
269 min = to_int(degree);
271 ZZ c = ZZ(INIT_VAL, 1);
272 if (!offset)
273 coeff[0] = c;
274 for (int i = 1; i <= min; ++i) {
275 c *= (degree -i + 1);
276 c /= i;
277 coeff[i-offset] = c;
280 void operator *= (dpoly& f) {
281 assert(coeff.length() == f.coeff.length());
282 vec_ZZ old = coeff;
283 coeff = f.coeff[0] * coeff;
284 for (int i = 1; i < coeff.length(); ++i)
285 for (int j = 0; i+j < coeff.length(); ++j)
286 coeff[i+j] += f.coeff[i] * old[j];
288 void div(dpoly& d, mpq_t count, ZZ& sign) {
289 int len = coeff.length();
290 Value tmp;
291 value_init(tmp);
292 mpq_t* c = new mpq_t[coeff.length()];
293 mpq_t qtmp;
294 mpq_init(qtmp);
295 for (int i = 0; i < len; ++i) {
296 mpq_init(c[i]);
297 zz2value(coeff[i], tmp);
298 mpq_set_z(c[i], tmp);
300 for (int j = 1; j <= i; ++j) {
301 zz2value(d.coeff[j], tmp);
302 mpq_set_z(qtmp, tmp);
303 mpq_mul(qtmp, qtmp, c[i-j]);
304 mpq_sub(c[i], c[i], qtmp);
307 zz2value(d.coeff[0], tmp);
308 mpq_set_z(qtmp, tmp);
309 mpq_div(c[i], c[i], qtmp);
311 if (sign == -1)
312 mpq_sub(count, count, c[len-1]);
313 else
314 mpq_add(count, count, c[len-1]);
316 value_clear(tmp);
317 mpq_clear(qtmp);
318 for (int i = 0; i < len; ++i)
319 mpq_clear(c[i]);
320 delete [] c;
324 class dpoly_n {
325 public:
326 Matrix *coeff;
327 ~dpoly_n() {
328 Matrix_Free(coeff);
330 dpoly_n(int d, ZZ& degree_0, ZZ& degree_1, int offset = 0) {
331 Value d0, d1;
332 value_init(d0);
333 value_init(d1);
334 zz2value(degree_0, d0);
335 zz2value(degree_1, d1);
336 coeff = Matrix_Alloc(d+1, d+1+1);
337 value_set_si(coeff->p[0][0], 1);
338 value_set_si(coeff->p[0][d+1], 1);
339 for (int i = 1; i <= d; ++i) {
340 value_multiply(coeff->p[i][0], coeff->p[i-1][0], d0);
341 Vector_Combine(coeff->p[i-1], coeff->p[i-1]+1, coeff->p[i]+1,
342 d1, d0, i);
343 value_set_si(coeff->p[i][d+1], i);
344 value_multiply(coeff->p[i][d+1], coeff->p[i][d+1], coeff->p[i-1][d+1]);
345 value_decrement(d0, d0);
347 value_clear(d0);
348 value_clear(d1);
350 void div(dpoly& d, Vector *count, ZZ& sign) {
351 int len = coeff->NbRows;
352 Matrix * c = Matrix_Alloc(coeff->NbRows, coeff->NbColumns);
353 Value tmp;
354 value_init(tmp);
355 for (int i = 0; i < len; ++i) {
356 Vector_Copy(coeff->p[i], c->p[i], len+1);
357 for (int j = 1; j <= i; ++j) {
358 zz2value(d.coeff[j], tmp);
359 value_multiply(tmp, tmp, c->p[i][len]);
360 value_oppose(tmp, tmp);
361 Vector_Combine(c->p[i], c->p[i-j], c->p[i],
362 c->p[i-j][len], tmp, len);
363 value_multiply(c->p[i][len], c->p[i][len], c->p[i-j][len]);
365 zz2value(d.coeff[0], tmp);
366 value_multiply(c->p[i][len], c->p[i][len], tmp);
368 if (sign == -1) {
369 value_set_si(tmp, -1);
370 Vector_Scale(c->p[len-1], count->p, tmp, len);
371 value_assign(count->p[len], c->p[len-1][len]);
372 } else
373 Vector_Copy(c->p[len-1], count->p, len+1);
374 Vector_Normalize(count->p, len+1);
375 value_clear(tmp);
376 Matrix_Free(c);
380 struct dpoly_r_term {
381 int *powers;
382 ZZ coeff;
385 /* len: number of elements in c
386 * each element in c is the coefficient of a power of t
387 * in the MacLaurin expansion
389 struct dpoly_r {
390 vector< dpoly_r_term * > *c;
391 int len;
392 int dim;
393 ZZ denom;
395 void add_term(int i, int * powers, ZZ& coeff) {
396 for (int k = 0; k < c[i].size(); ++k) {
397 if (memcmp(c[i][k]->powers, powers, dim * sizeof(int)) == 0) {
398 c[i][k]->coeff += coeff;
399 return;
402 dpoly_r_term *t = new dpoly_r_term;
403 t->powers = new int[dim];
404 memcpy(t->powers, powers, dim * sizeof(int));
405 t->coeff = coeff;
406 c[i].push_back(t);
408 dpoly_r(int len, int dim) {
409 denom = 1;
410 this->len = len;
411 this->dim = dim;
412 c = new vector< dpoly_r_term * > [len];
414 dpoly_r(dpoly& num, dpoly& den, int pos, int sign, int dim) {
415 denom = 1;
416 len = num.coeff.length();
417 c = new vector< dpoly_r_term * > [len];
418 this->dim = dim;
419 int powers[dim];
421 for (int i = 0; i < len; ++i) {
422 ZZ coeff = num.coeff[i];
423 memset(powers, 0, dim * sizeof(int));
424 powers[pos] = sign;
426 add_term(i, powers, coeff);
428 for (int j = 1; j <= i; ++j) {
429 for (int k = 0; k < c[i-j].size(); ++k) {
430 memcpy(powers, c[i-j][k]->powers, dim*sizeof(int));
431 powers[pos] += sign;
432 coeff = -den.coeff[j-1] * c[i-j][k]->coeff;
433 add_term(i, powers, coeff);
437 //dump();
439 dpoly_r(dpoly_r* num, dpoly& den, int pos, int sign, int dim) {
440 denom = num->denom;
441 len = num->len;
442 c = new vector< dpoly_r_term * > [len];
443 this->dim = dim;
444 int powers[dim];
445 ZZ coeff;
447 for (int i = 0 ; i < len; ++i) {
448 for (int k = 0; k < num->c[i].size(); ++k) {
449 memcpy(powers, num->c[i][k]->powers, dim*sizeof(int));
450 powers[pos] += sign;
451 add_term(i, powers, num->c[i][k]->coeff);
454 for (int j = 1; j <= i; ++j) {
455 for (int k = 0; k < c[i-j].size(); ++k) {
456 memcpy(powers, c[i-j][k]->powers, dim*sizeof(int));
457 powers[pos] += sign;
458 coeff = -den.coeff[j-1] * c[i-j][k]->coeff;
459 add_term(i, powers, coeff);
464 ~dpoly_r() {
465 for (int i = 0 ; i < len; ++i)
466 for (int k = 0; k < c[i].size(); ++k) {
467 delete [] c[i][k]->powers;
468 delete c[i][k];
470 delete [] c;
472 dpoly_r *div(dpoly& d) {
473 dpoly_r *rc = new dpoly_r(len, dim);
474 rc->denom = power(d.coeff[0], len);
475 ZZ inv_d = rc->denom / d.coeff[0];
476 ZZ coeff;
478 for (int i = 0; i < len; ++i) {
479 for (int k = 0; k < c[i].size(); ++k) {
480 coeff = c[i][k]->coeff * inv_d;
481 rc->add_term(i, c[i][k]->powers, coeff);
484 for (int j = 1; j <= i; ++j) {
485 for (int k = 0; k < rc->c[i-j].size(); ++k) {
486 coeff = - d.coeff[j] * rc->c[i-j][k]->coeff / d.coeff[0];
487 rc->add_term(i, rc->c[i-j][k]->powers, coeff);
491 return rc;
493 void dump(void) {
494 for (int i = 0; i < len; ++i) {
495 cout << endl;
496 cout << i << endl;
497 cout << c[i].size() << endl;
498 for (int j = 0; j < c[i].size(); ++j) {
499 for (int k = 0; k < dim; ++k) {
500 cout << c[i][j]->powers[k] << " ";
502 cout << ": " << c[i][j]->coeff << "/" << denom << endl;
504 cout << endl;
509 struct decomposer {
510 void decompose(Polyhedron *C);
511 virtual void handle(Polyhedron *P, int sign) = 0;
514 struct polar_decomposer : public decomposer {
515 void decompose(Polyhedron *C, unsigned MaxRays);
516 virtual void handle(Polyhedron *P, int sign);
517 virtual void handle_polar(Polyhedron *P, int sign) = 0;
520 void decomposer::decompose(Polyhedron *C)
522 vector<cone *> nonuni;
523 cone * c = new cone(C);
524 ZZ det = c->det;
525 int s = sign(det);
526 assert(det != 0);
527 if (abs(det) > 1) {
528 nonuni.push_back(c);
529 } else {
530 handle(C, 1);
531 delete c;
533 vec_ZZ lambda;
534 while (!nonuni.empty()) {
535 c = nonuni.back();
536 nonuni.pop_back();
537 Vector* v = c->short_vector(lambda);
538 for (int i = 0; i < c->Rays->NbRows - 1; ++i) {
539 if (lambda[i] == 0)
540 continue;
541 Matrix* M = Matrix_Copy(c->Rays);
542 Vector_Copy(v->p, M->p[i], v->Size);
543 cone * pc = new cone(M);
544 assert (pc->det != 0);
545 if (abs(pc->det) > 1) {
546 assert(abs(pc->det) < abs(c->det));
547 nonuni.push_back(pc);
548 } else {
549 handle(pc->poly(), sign(pc->det) * s);
550 delete pc;
552 Matrix_Free(M);
554 Vector_Free(v);
555 delete c;
559 void polar_decomposer::decompose(Polyhedron *cone, unsigned MaxRays)
561 Polyhedron_Polarize(cone);
562 if (cone->NbRays - 1 != cone->Dimension) {
563 Polyhedron *tmp = cone;
564 cone = triangularize_cone(cone, MaxRays);
565 Polyhedron_Free(tmp);
567 for (Polyhedron *Polar = cone; Polar; Polar = Polar->next)
568 decomposer::decompose(Polar);
569 Domain_Free(cone);
572 void polar_decomposer::handle(Polyhedron *P, int sign)
574 Polyhedron_Polarize(P);
575 handle_polar(P, sign);
579 * Barvinok's Decomposition of a simplicial cone
581 * Returns two lists of polyhedra
583 void barvinok_decompose(Polyhedron *C, Polyhedron **ppos, Polyhedron **pneg)
585 Polyhedron *pos = *ppos, *neg = *pneg;
586 vector<cone *> nonuni;
587 cone * c = new cone(C);
588 ZZ det = c->det;
589 int s = sign(det);
590 assert(det != 0);
591 if (abs(det) > 1) {
592 nonuni.push_back(c);
593 } else {
594 Polyhedron *p = Polyhedron_Copy(c->Cone);
595 p->next = pos;
596 pos = p;
597 delete c;
599 vec_ZZ lambda;
600 while (!nonuni.empty()) {
601 c = nonuni.back();
602 nonuni.pop_back();
603 Vector* v = c->short_vector(lambda);
604 for (int i = 0; i < c->Rays->NbRows - 1; ++i) {
605 if (lambda[i] == 0)
606 continue;
607 Matrix* M = Matrix_Copy(c->Rays);
608 Vector_Copy(v->p, M->p[i], v->Size);
609 cone * pc = new cone(M);
610 assert (pc->det != 0);
611 if (abs(pc->det) > 1) {
612 assert(abs(pc->det) < abs(c->det));
613 nonuni.push_back(pc);
614 } else {
615 Polyhedron *p = pc->poly();
616 pc->Cone = 0;
617 if (sign(pc->det) == s) {
618 p->next = pos;
619 pos = p;
620 } else {
621 p->next = neg;
622 neg = p;
624 delete pc;
626 Matrix_Free(M);
628 Vector_Free(v);
629 delete c;
631 *ppos = pos;
632 *pneg = neg;
635 const int MAX_TRY=10;
637 * Searches for a vector that is not orthogonal to any
638 * of the rays in rays.
640 static void nonorthog(mat_ZZ& rays, vec_ZZ& lambda)
642 int dim = rays.NumCols();
643 bool found = false;
644 lambda.SetLength(dim);
645 if (dim == 0)
646 return;
648 for (int i = 2; !found && i <= 50*dim; i+=4) {
649 for (int j = 0; j < MAX_TRY; ++j) {
650 for (int k = 0; k < dim; ++k) {
651 int r = random_int(i)+2;
652 int v = (2*(r%2)-1) * (r >> 1);
653 lambda[k] = v;
655 int k = 0;
656 for (; k < rays.NumRows(); ++k)
657 if (lambda * rays[k] == 0)
658 break;
659 if (k == rays.NumRows()) {
660 found = true;
661 break;
665 assert(found);
668 static void randomvector(Polyhedron *P, vec_ZZ& lambda, int nvar)
670 Value tmp;
671 int max = 10 * 16;
672 unsigned int dim = P->Dimension;
673 value_init(tmp);
675 for (int i = 0; i < P->NbRays; ++i) {
676 for (int j = 1; j <= dim; ++j) {
677 value_absolute(tmp, P->Ray[i][j]);
678 int t = VALUE_TO_LONG(tmp) * 16;
679 if (t > max)
680 max = t;
683 for (int i = 0; i < P->NbConstraints; ++i) {
684 for (int j = 1; j <= dim; ++j) {
685 value_absolute(tmp, P->Constraint[i][j]);
686 int t = VALUE_TO_LONG(tmp) * 16;
687 if (t > max)
688 max = t;
691 value_clear(tmp);
693 lambda.SetLength(nvar);
694 for (int k = 0; k < nvar; ++k) {
695 int r = random_int(max*dim)+2;
696 int v = (2*(r%2)-1) * (max/2*dim + (r >> 1));
697 lambda[k] = v;
701 static void add_rays(mat_ZZ& rays, Polyhedron *i, int *r, int nvar = -1,
702 bool all = false)
704 unsigned dim = i->Dimension;
705 if (nvar == -1)
706 nvar = dim;
707 for (int k = 0; k < i->NbRays; ++k) {
708 if (!value_zero_p(i->Ray[k][dim+1]))
709 continue;
710 if (!all && nvar != dim && First_Non_Zero(i->Ray[k]+1, nvar) == -1)
711 continue;
712 values2zz(i->Ray[k]+1, rays[(*r)++], nvar);
716 void lattice_point(Value* values, Polyhedron *i, vec_ZZ& vertex)
718 unsigned dim = i->Dimension;
719 if(!value_one_p(values[dim])) {
720 Matrix* Rays = rays(i);
721 Matrix *inv = Matrix_Alloc(Rays->NbRows, Rays->NbColumns);
722 int ok = Matrix_Inverse(Rays, inv);
723 assert(ok);
724 Matrix_Free(Rays);
725 Rays = rays(i);
726 Vector *lambda = Vector_Alloc(dim+1);
727 Vector_Matrix_Product(values, inv, lambda->p);
728 Matrix_Free(inv);
729 for (int j = 0; j < dim; ++j)
730 mpz_cdiv_q(lambda->p[j], lambda->p[j], lambda->p[dim]);
731 value_set_si(lambda->p[dim], 1);
732 Vector *A = Vector_Alloc(dim+1);
733 Vector_Matrix_Product(lambda->p, Rays, A->p);
734 Vector_Free(lambda);
735 Matrix_Free(Rays);
736 values2zz(A->p, vertex, dim);
737 Vector_Free(A);
738 } else
739 values2zz(values, vertex, dim);
742 static evalue *term(int param, ZZ& c, Value *den = NULL)
744 evalue *EP = new evalue();
745 value_init(EP->d);
746 value_set_si(EP->d,0);
747 EP->x.p = new_enode(polynomial, 2, param + 1);
748 evalue_set_si(&EP->x.p->arr[0], 0, 1);
749 value_init(EP->x.p->arr[1].x.n);
750 if (den == NULL)
751 value_set_si(EP->x.p->arr[1].d, 1);
752 else
753 value_assign(EP->x.p->arr[1].d, *den);
754 zz2value(c, EP->x.p->arr[1].x.n);
755 return EP;
758 static void vertex_period(
759 Polyhedron *i, vec_ZZ& lambda, Matrix *T,
760 Value lcm, int p, Vector *val,
761 evalue *E, evalue* ev,
762 ZZ& offset)
764 unsigned nparam = T->NbRows - 1;
765 unsigned dim = i->Dimension;
766 Value tmp;
767 ZZ nump;
769 if (p == nparam) {
770 vec_ZZ vertex;
771 ZZ num, l;
772 Vector * values = Vector_Alloc(dim + 1);
773 Vector_Matrix_Product(val->p, T, values->p);
774 value_assign(values->p[dim], lcm);
775 lattice_point(values->p, i, vertex);
776 num = vertex * lambda;
777 value2zz(lcm, l);
778 num *= l;
779 num += offset;
780 value_init(ev->x.n);
781 zz2value(num, ev->x.n);
782 value_assign(ev->d, lcm);
783 Vector_Free(values);
784 return;
787 value_init(tmp);
788 vec_ZZ vertex;
789 values2zz(T->p[p], vertex, dim);
790 nump = vertex * lambda;
791 if (First_Non_Zero(val->p, p) == -1) {
792 value_assign(tmp, lcm);
793 evalue *ET = term(p, nump, &tmp);
794 eadd(ET, E);
795 free_evalue_refs(ET);
796 delete ET;
799 value_assign(tmp, lcm);
800 if (First_Non_Zero(T->p[p], dim) != -1)
801 Vector_Gcd(T->p[p], dim, &tmp);
802 Gcd(tmp, lcm, &tmp);
803 if (value_lt(tmp, lcm)) {
804 ZZ count;
806 value_division(tmp, lcm, tmp);
807 value_set_si(ev->d, 0);
808 ev->x.p = new_enode(periodic, VALUE_TO_INT(tmp), p+1);
809 value2zz(tmp, count);
810 do {
811 value_decrement(tmp, tmp);
812 --count;
813 ZZ new_offset = offset - count * nump;
814 value_assign(val->p[p], tmp);
815 vertex_period(i, lambda, T, lcm, p+1, val, E,
816 &ev->x.p->arr[VALUE_TO_INT(tmp)], new_offset);
817 } while (value_pos_p(tmp));
818 } else
819 vertex_period(i, lambda, T, lcm, p+1, val, E, ev, offset);
820 value_clear(tmp);
823 static void mask_r(Matrix *f, int nr, Vector *lcm, int p, Vector *val, evalue *ev)
825 unsigned nparam = lcm->Size;
827 if (p == nparam) {
828 Vector * prod = Vector_Alloc(f->NbRows);
829 Matrix_Vector_Product(f, val->p, prod->p);
830 int isint = 1;
831 for (int i = 0; i < nr; ++i) {
832 value_modulus(prod->p[i], prod->p[i], f->p[i][nparam+1]);
833 isint &= value_zero_p(prod->p[i]);
835 value_set_si(ev->d, 1);
836 value_init(ev->x.n);
837 value_set_si(ev->x.n, isint);
838 Vector_Free(prod);
839 return;
842 Value tmp;
843 value_init(tmp);
844 if (value_one_p(lcm->p[p]))
845 mask_r(f, nr, lcm, p+1, val, ev);
846 else {
847 value_assign(tmp, lcm->p[p]);
848 value_set_si(ev->d, 0);
849 ev->x.p = new_enode(periodic, VALUE_TO_INT(tmp), p+1);
850 do {
851 value_decrement(tmp, tmp);
852 value_assign(val->p[p], tmp);
853 mask_r(f, nr, lcm, p+1, val, &ev->x.p->arr[VALUE_TO_INT(tmp)]);
854 } while (value_pos_p(tmp));
856 value_clear(tmp);
859 static evalue *multi_monom(vec_ZZ& p)
861 evalue *X = new evalue();
862 value_init(X->d);
863 value_init(X->x.n);
864 unsigned nparam = p.length()-1;
865 zz2value(p[nparam], X->x.n);
866 value_set_si(X->d, 1);
867 for (int i = 0; i < nparam; ++i) {
868 if (p[i] == 0)
869 continue;
870 evalue *T = term(i, p[i]);
871 eadd(T, X);
872 free_evalue_refs(T);
873 delete T;
875 return X;
879 * Check whether mapping polyhedron P on the affine combination
880 * num yields a range that has a fixed quotient on integer
881 * division by d
882 * If zero is true, then we are only interested in the quotient
883 * for the cases where the remainder is zero.
884 * Returns NULL if false and a newly allocated value if true.
886 static Value *fixed_quotient(Polyhedron *P, vec_ZZ& num, Value d, bool zero)
888 Value* ret = NULL;
889 int len = num.length();
890 Matrix *T = Matrix_Alloc(2, len);
891 zz2values(num, T->p[0]);
892 value_set_si(T->p[1][len-1], 1);
893 Polyhedron *I = Polyhedron_Image(P, T, P->NbConstraints);
894 Matrix_Free(T);
896 int i;
897 for (i = 0; i < I->NbRays; ++i)
898 if (value_zero_p(I->Ray[i][2])) {
899 Polyhedron_Free(I);
900 return NULL;
903 Value min, max;
904 value_init(min);
905 value_init(max);
906 int bounded = line_minmax(I, &min, &max);
907 assert(bounded);
909 if (zero)
910 mpz_cdiv_q(min, min, d);
911 else
912 mpz_fdiv_q(min, min, d);
913 mpz_fdiv_q(max, max, d);
915 if (value_eq(min, max)) {
916 ALLOC(Value, ret);
917 value_init(*ret);
918 value_assign(*ret, min);
920 value_clear(min);
921 value_clear(max);
922 return ret;
926 * Normalize linear expression coef modulo m
927 * Removes common factor and reduces coefficients
928 * Returns index of first non-zero coefficient or len
930 static int normal_mod(Value *coef, int len, Value *m)
932 Value gcd;
933 value_init(gcd);
935 Vector_Gcd(coef, len, &gcd);
936 Gcd(gcd, *m, &gcd);
937 Vector_AntiScale(coef, coef, gcd, len);
939 value_division(*m, *m, gcd);
940 value_clear(gcd);
942 if (value_one_p(*m))
943 return len;
945 int j;
946 for (j = 0; j < len; ++j)
947 mpz_fdiv_r(coef[j], coef[j], *m);
948 for (j = 0; j < len; ++j)
949 if (value_notzero_p(coef[j]))
950 break;
952 return j;
955 #ifdef USE_MODULO
956 static void mask(Matrix *f, evalue *factor)
958 int nr = f->NbRows, nc = f->NbColumns;
959 int n;
960 bool found = false;
961 for (n = 0; n < nr && value_notzero_p(f->p[n][nc-1]); ++n)
962 if (value_notone_p(f->p[n][nc-1]) &&
963 value_notmone_p(f->p[n][nc-1]))
964 found = true;
965 if (!found)
966 return;
968 evalue EP;
969 nr = n;
971 Value m;
972 value_init(m);
974 evalue EV;
975 value_init(EV.d);
976 value_init(EV.x.n);
977 value_set_si(EV.x.n, 1);
979 for (n = 0; n < nr; ++n) {
980 value_assign(m, f->p[n][nc-1]);
981 if (value_one_p(m) || value_mone_p(m))
982 continue;
984 int j = normal_mod(f->p[n], nc-1, &m);
985 if (j == nc-1) {
986 free_evalue_refs(factor);
987 value_init(factor->d);
988 evalue_set_si(factor, 0, 1);
989 break;
991 vec_ZZ row;
992 values2zz(f->p[n], row, nc-1);
993 ZZ g;
994 value2zz(m, g);
995 if (j < (nc-1)-1 && row[j] > g/2) {
996 for (int k = j; k < (nc-1); ++k)
997 if (row[k] != 0)
998 row[k] = g - row[k];
1001 value_init(EP.d);
1002 value_set_si(EP.d, 0);
1003 EP.x.p = new_enode(relation, 2, 0);
1004 value_clear(EP.x.p->arr[1].d);
1005 EP.x.p->arr[1] = *factor;
1006 evalue *ev = &EP.x.p->arr[0];
1007 value_set_si(ev->d, 0);
1008 ev->x.p = new_enode(fractional, 3, -1);
1009 evalue_set_si(&ev->x.p->arr[1], 0, 1);
1010 evalue_set_si(&ev->x.p->arr[2], 1, 1);
1011 evalue *E = multi_monom(row);
1012 value_assign(EV.d, m);
1013 emul(&EV, E);
1014 value_clear(ev->x.p->arr[0].d);
1015 ev->x.p->arr[0] = *E;
1016 delete E;
1017 *factor = EP;
1020 value_clear(m);
1021 free_evalue_refs(&EV);
1023 #else
1027 static void mask(Matrix *f, evalue *factor)
1029 int nr = f->NbRows, nc = f->NbColumns;
1030 int n;
1031 bool found = false;
1032 for (n = 0; n < nr && value_notzero_p(f->p[n][nc-1]); ++n)
1033 if (value_notone_p(f->p[n][nc-1]) &&
1034 value_notmone_p(f->p[n][nc-1]))
1035 found = true;
1036 if (!found)
1037 return;
1039 Value tmp;
1040 value_init(tmp);
1041 nr = n;
1042 unsigned np = nc - 2;
1043 Vector *lcm = Vector_Alloc(np);
1044 Vector *val = Vector_Alloc(nc);
1045 Vector_Set(val->p, 0, nc);
1046 value_set_si(val->p[np], 1);
1047 Vector_Set(lcm->p, 1, np);
1048 for (n = 0; n < nr; ++n) {
1049 if (value_one_p(f->p[n][nc-1]) ||
1050 value_mone_p(f->p[n][nc-1]))
1051 continue;
1052 for (int j = 0; j < np; ++j)
1053 if (value_notzero_p(f->p[n][j])) {
1054 Gcd(f->p[n][j], f->p[n][nc-1], &tmp);
1055 value_division(tmp, f->p[n][nc-1], tmp);
1056 value_lcm(tmp, lcm->p[j], &lcm->p[j]);
1059 evalue EP;
1060 value_init(EP.d);
1061 mask_r(f, nr, lcm, 0, val, &EP);
1062 value_clear(tmp);
1063 Vector_Free(val);
1064 Vector_Free(lcm);
1065 emul(&EP,factor);
1066 free_evalue_refs(&EP);
1068 #endif
1070 struct term_info {
1071 evalue *E;
1072 ZZ constant;
1073 ZZ coeff;
1074 int pos;
1077 static bool mod_needed(Polyhedron *PD, vec_ZZ& num, Value d, evalue *E)
1079 Value *q = fixed_quotient(PD, num, d, false);
1081 if (!q)
1082 return true;
1084 value_oppose(*q, *q);
1085 evalue EV;
1086 value_init(EV.d);
1087 value_set_si(EV.d, 1);
1088 value_init(EV.x.n);
1089 value_multiply(EV.x.n, *q, d);
1090 eadd(&EV, E);
1091 free_evalue_refs(&EV);
1092 value_clear(*q);
1093 free(q);
1094 return false;
1097 static void ceil_mod(Value *coef, int len, Value d, ZZ& f, evalue *EP, Polyhedron *PD)
1099 Value m;
1100 value_init(m);
1101 value_set_si(m, -1);
1103 Vector_Scale(coef, coef, m, len);
1105 value_assign(m, d);
1106 int j = normal_mod(coef, len, &m);
1108 if (j == len) {
1109 value_clear(m);
1110 return;
1113 vec_ZZ num;
1114 values2zz(coef, num, len);
1116 ZZ g;
1117 value2zz(m, g);
1119 evalue tmp;
1120 value_init(tmp.d);
1121 evalue_set_si(&tmp, 0, 1);
1123 int p = j;
1124 if (g % 2 == 0)
1125 while (j < len-1 && (num[j] == g/2 || num[j] == 0))
1126 ++j;
1127 if ((j < len-1 && num[j] > g/2) || (j == len-1 && num[j] >= (g+1)/2)) {
1128 for (int k = j; k < len-1; ++k)
1129 if (num[k] != 0)
1130 num[k] = g - num[k];
1131 num[len-1] = g - 1 - num[len-1];
1132 value_assign(tmp.d, m);
1133 ZZ t = f*(g-1);
1134 zz2value(t, tmp.x.n);
1135 eadd(&tmp, EP);
1136 f = -f;
1139 if (p >= len-1) {
1140 ZZ t = num[len-1] * f;
1141 zz2value(t, tmp.x.n);
1142 value_assign(tmp.d, m);
1143 eadd(&tmp, EP);
1144 } else {
1145 evalue *E = multi_monom(num);
1146 evalue EV;
1147 value_init(EV.d);
1149 if (PD && !mod_needed(PD, num, m, E)) {
1150 value_init(EV.x.n);
1151 zz2value(f, EV.x.n);
1152 value_assign(EV.d, m);
1153 emul(&EV, E);
1154 eadd(E, EP);
1155 } else {
1156 value_init(EV.x.n);
1157 value_set_si(EV.x.n, 1);
1158 value_assign(EV.d, m);
1159 emul(&EV, E);
1160 value_clear(EV.x.n);
1161 value_set_si(EV.d, 0);
1162 EV.x.p = new_enode(fractional, 3, -1);
1163 evalue_copy(&EV.x.p->arr[0], E);
1164 evalue_set_si(&EV.x.p->arr[1], 0, 1);
1165 value_init(EV.x.p->arr[2].x.n);
1166 zz2value(f, EV.x.p->arr[2].x.n);
1167 value_set_si(EV.x.p->arr[2].d, 1);
1169 eadd(&EV, EP);
1172 free_evalue_refs(&EV);
1173 free_evalue_refs(E);
1174 delete E;
1177 free_evalue_refs(&tmp);
1179 out:
1180 value_clear(m);
1183 evalue* bv_ceil3(Value *coef, int len, Value d, Polyhedron *P)
1185 Vector *val = Vector_Alloc(len);
1187 Value t;
1188 value_init(t);
1189 value_set_si(t, -1);
1190 Vector_Scale(coef, val->p, t, len);
1191 value_absolute(t, d);
1193 vec_ZZ num;
1194 values2zz(val->p, num, len);
1195 evalue *EP = multi_monom(num);
1197 evalue tmp;
1198 value_init(tmp.d);
1199 value_init(tmp.x.n);
1200 value_set_si(tmp.x.n, 1);
1201 value_assign(tmp.d, t);
1203 emul(&tmp, EP);
1205 ZZ one;
1206 one = 1;
1207 ceil_mod(val->p, len, t, one, EP, P);
1208 value_clear(t);
1210 /* copy EP to malloc'ed evalue */
1211 evalue *E;
1212 ALLOC(evalue, E);
1213 *E = *EP;
1214 delete EP;
1216 free_evalue_refs(&tmp);
1217 Vector_Free(val);
1219 return E;
1222 #ifdef USE_MODULO
1223 evalue* lattice_point(
1224 Polyhedron *i, vec_ZZ& lambda, Matrix *W, Value lcm, Polyhedron *PD)
1226 unsigned nparam = W->NbColumns - 1;
1228 Matrix* Rays = rays2(i);
1229 Matrix *T = Transpose(Rays);
1230 Matrix *T2 = Matrix_Copy(T);
1231 Matrix *inv = Matrix_Alloc(T2->NbRows, T2->NbColumns);
1232 int ok = Matrix_Inverse(T2, inv);
1233 assert(ok);
1234 Matrix_Free(Rays);
1235 Matrix_Free(T2);
1236 mat_ZZ vertex;
1237 matrix2zz(W, vertex, W->NbRows, W->NbColumns);
1239 vec_ZZ num;
1240 num = lambda * vertex;
1242 evalue *EP = multi_monom(num);
1244 evalue tmp;
1245 value_init(tmp.d);
1246 value_init(tmp.x.n);
1247 value_set_si(tmp.x.n, 1);
1248 value_assign(tmp.d, lcm);
1250 emul(&tmp, EP);
1252 Matrix *L = Matrix_Alloc(inv->NbRows, W->NbColumns);
1253 Matrix_Product(inv, W, L);
1255 mat_ZZ RT;
1256 matrix2zz(T, RT, T->NbRows, T->NbColumns);
1257 Matrix_Free(T);
1259 vec_ZZ p = lambda * RT;
1261 for (int i = 0; i < L->NbRows; ++i) {
1262 ceil_mod(L->p[i], nparam+1, lcm, p[i], EP, PD);
1265 Matrix_Free(L);
1267 Matrix_Free(inv);
1268 free_evalue_refs(&tmp);
1269 return EP;
1271 #else
1272 evalue* lattice_point(
1273 Polyhedron *i, vec_ZZ& lambda, Matrix *W, Value lcm, Polyhedron *PD)
1275 Matrix *T = Transpose(W);
1276 unsigned nparam = T->NbRows - 1;
1278 evalue *EP = new evalue();
1279 value_init(EP->d);
1280 evalue_set_si(EP, 0, 1);
1282 evalue ev;
1283 Vector *val = Vector_Alloc(nparam+1);
1284 value_set_si(val->p[nparam], 1);
1285 ZZ offset(INIT_VAL, 0);
1286 value_init(ev.d);
1287 vertex_period(i, lambda, T, lcm, 0, val, EP, &ev, offset);
1288 Vector_Free(val);
1289 eadd(&ev, EP);
1290 free_evalue_refs(&ev);
1292 Matrix_Free(T);
1294 reduce_evalue(EP);
1296 return EP;
1298 #endif
1300 void lattice_point(
1301 Param_Vertices* V, Polyhedron *i, vec_ZZ& lambda, term_info* term,
1302 Polyhedron *PD)
1304 unsigned nparam = V->Vertex->NbColumns - 2;
1305 unsigned dim = i->Dimension;
1306 mat_ZZ vertex;
1307 vertex.SetDims(V->Vertex->NbRows, nparam+1);
1308 Value lcm, tmp;
1309 value_init(lcm);
1310 value_init(tmp);
1311 value_set_si(lcm, 1);
1312 for (int j = 0; j < V->Vertex->NbRows; ++j) {
1313 value_lcm(lcm, V->Vertex->p[j][nparam+1], &lcm);
1315 if (value_notone_p(lcm)) {
1316 Matrix * mv = Matrix_Alloc(dim, nparam+1);
1317 for (int j = 0 ; j < dim; ++j) {
1318 value_division(tmp, lcm, V->Vertex->p[j][nparam+1]);
1319 Vector_Scale(V->Vertex->p[j], mv->p[j], tmp, nparam+1);
1322 term->E = lattice_point(i, lambda, mv, lcm, PD);
1323 term->constant = 0;
1325 Matrix_Free(mv);
1326 value_clear(lcm);
1327 value_clear(tmp);
1328 return;
1330 for (int i = 0; i < V->Vertex->NbRows; ++i) {
1331 assert(value_one_p(V->Vertex->p[i][nparam+1])); // for now
1332 values2zz(V->Vertex->p[i], vertex[i], nparam+1);
1335 vec_ZZ num;
1336 num = lambda * vertex;
1338 int p = -1;
1339 int nn = 0;
1340 for (int j = 0; j < nparam; ++j)
1341 if (num[j] != 0) {
1342 ++nn;
1343 p = j;
1345 if (nn >= 2) {
1346 term->E = multi_monom(num);
1347 term->constant = 0;
1348 } else {
1349 term->E = NULL;
1350 term->constant = num[nparam];
1351 term->pos = p;
1352 if (p != -1)
1353 term->coeff = num[p];
1356 value_clear(lcm);
1357 value_clear(tmp);
1360 static void normalize(ZZ& sign, ZZ& num, vec_ZZ& den)
1362 unsigned dim = den.length();
1364 int change = 0;
1366 for (int j = 0; j < den.length(); ++j) {
1367 if (den[j] > 0)
1368 change ^= 1;
1369 else {
1370 den[j] = abs(den[j]);
1371 num += den[j];
1374 if (change)
1375 sign = -sign;
1378 /* input:
1379 * f: the powers in the denominator for the remaining vars
1380 * each row refers to a factor
1381 * den_s: for each factor, the power of (s+1)
1382 * sign
1383 * num_s: powers in the numerator corresponding to the summed vars
1384 * num_p: powers in the numerator corresponidng to the remaining vars
1385 * number of rays in cone: "dim" = "k"
1386 * length of each ray: "dim" = "d"
1387 * for now, it is assume: k == d
1388 * output:
1389 * den_p: for each factor
1390 * 0: independent of remaining vars
1391 * 1: power corresponds to corresponding row in f
1392 * -1: power is inverse of corresponding row in f
1394 static void normalize(ZZ& sign,
1395 ZZ& num_s, vec_ZZ& num_p, vec_ZZ& den_s, vec_ZZ& den_p,
1396 mat_ZZ& f)
1398 unsigned dim = f.NumRows();
1399 unsigned nparam = num_p.length();
1400 unsigned nvar = dim - nparam;
1402 int change = 0;
1404 for (int j = 0; j < den_s.length(); ++j) {
1405 if (den_s[j] == 0) {
1406 den_p[j] = 1;
1407 continue;
1409 int k;
1410 for (k = 0; k < nparam; ++k)
1411 if (f[j][k] != 0)
1412 break;
1413 if (k < nparam) {
1414 if (den_s[j] > 0) {
1415 den_p[j] = -1;
1416 num_p -= f[j];
1417 } else
1418 den_p[j] = 1;
1419 } else
1420 den_p[j] = 0;
1421 if (den_s[j] > 0)
1422 change ^= 1;
1423 else {
1424 den_s[j] = abs(den_s[j]);
1425 num_s += den_s[j];
1429 if (change)
1430 sign = -sign;
1433 struct counter : public polar_decomposer {
1434 vec_ZZ lambda;
1435 mat_ZZ rays;
1436 vec_ZZ vertex;
1437 vec_ZZ den;
1438 ZZ sign;
1439 ZZ num;
1440 int j;
1441 Polyhedron *P;
1442 unsigned dim;
1443 mpq_t count;
1445 counter(Polyhedron *P) {
1446 this->P = P;
1447 dim = P->Dimension;
1448 randomvector(P, lambda, dim);
1449 rays.SetDims(dim, dim);
1450 den.SetLength(dim);
1451 mpq_init(count);
1454 void start(unsigned MaxRays);
1456 ~counter() {
1457 mpq_clear(count);
1460 virtual void handle_polar(Polyhedron *P, int sign);
1463 void counter::handle_polar(Polyhedron *C, int s)
1465 int r = 0;
1466 assert(C->NbRays-1 == dim);
1467 add_rays(rays, C, &r);
1468 for (int k = 0; k < dim; ++k) {
1469 assert(lambda * rays[k] != 0);
1472 sign = s;
1474 lattice_point(P->Ray[j]+1, C, vertex);
1475 num = vertex * lambda;
1476 den = rays * lambda;
1477 normalize(sign, num, den);
1479 dpoly d(dim, num);
1480 dpoly n(dim, den[0], 1);
1481 for (int k = 1; k < dim; ++k) {
1482 dpoly fact(dim, den[k], 1);
1483 n *= fact;
1485 d.div(n, count, sign);
1488 void counter::start(unsigned MaxRays)
1490 for (j = 0; j < P->NbRays; ++j) {
1491 Polyhedron *C = supporting_cone(P, j);
1492 decompose(C, MaxRays);
1496 struct reducer : public polar_decomposer {
1497 vec_ZZ lambda;
1498 vec_ZZ vertex;
1499 //vec_ZZ den;
1500 ZZ sgn;
1501 ZZ num;
1502 ZZ one;
1503 int j;
1504 Polyhedron *P;
1505 unsigned dim;
1506 mpq_t tcount;
1507 mpz_t tn;
1508 mpz_t td;
1509 int lower; // call base when only this many variables is left
1510 int untouched; // keep this many variables untouched
1512 reducer(Polyhedron *P) {
1513 this->P = P;
1514 dim = P->Dimension;
1515 lambda.SetLength(1);
1516 lambda[0] = 1;
1517 //den.SetLength(dim);
1518 mpq_init(tcount);
1519 mpz_init(tn);
1520 mpz_init(td);
1521 one = 1;
1524 void start(unsigned MaxRays);
1526 ~reducer() {
1527 mpq_clear(tcount);
1528 mpz_clear(tn);
1529 mpz_clear(td);
1532 virtual void handle_polar(Polyhedron *P, int sign);
1533 void reduce(ZZ c, ZZ cd, vec_ZZ& num, mat_ZZ& den_f);
1534 virtual void base(ZZ& c, ZZ& cd, vec_ZZ& num, mat_ZZ& den_f) = 0;
1537 void reducer::reduce(ZZ c, ZZ cd, vec_ZZ& num, mat_ZZ& den_f)
1539 unsigned len = den_f.NumRows(); // number of factors in den
1540 unsigned d = num.length()-1;
1542 if (d+1 == lower) {
1543 base(c, cd, num, den_f);
1544 return;
1546 assert(num.length() > 1);
1548 vec_ZZ den_s;
1549 den_s.SetLength(len);
1550 mat_ZZ den_r;
1551 den_r.SetDims(len, d);
1553 /* Since we're working incrementally, we can look
1554 * for the "easiest" parameter first.
1555 * In particular we first handle the parameters such
1556 * that no_param + only_param == len, since that allows
1557 * us to decouple the problem and the split off part
1558 * may very well be zero
1560 int i = 0;
1561 int r, k;
1562 for (i = 0; i < d+1-untouched; ++i) {
1563 for (r = 0; r < len; ++r) {
1564 if (den_f[r][i] != 0) {
1565 for (k = 0; k <= d; ++k)
1566 if (i != k && den_f[r][k] != 0)
1567 break;
1568 if (k <= d)
1569 break;
1572 if (r >= len)
1573 break;
1575 if (i > d-untouched)
1576 i = 0;
1578 for (r = 0; r < len; ++r) {
1579 den_s[r] = den_f[r][i];
1580 for (k = 0; k <= d; ++k)
1581 if (k != i)
1582 den_r[r][k-(k>i)] = den_f[r][k];
1585 ZZ num_s = num[i];
1586 vec_ZZ num_p;
1587 num_p.SetLength(d);
1588 for (k = 0 ; k <= d; ++k)
1589 if (k != i)
1590 num_p[k-(k>i)] = num[k];
1592 vec_ZZ den_p;
1593 den_p.SetLength(len);
1595 normalize(c, num_s, num_p, den_s, den_p, den_r);
1597 int only_param = 0;
1598 int no_param = 0;
1599 for (int k = 0; k < len; ++k) {
1600 if (den_p[k] == 0)
1601 ++no_param;
1602 else if (den_s[k] == 0)
1603 ++only_param;
1605 if (no_param == 0) {
1606 for (int k = 0; k < len; ++k)
1607 if (den_p[k] == -1)
1608 den_r[k] = -den_r[k];
1609 reduce(c, cd, num_p, den_r);
1610 } else {
1611 int k, l;
1612 mat_ZZ pden;
1613 pden.SetDims(only_param, d);
1615 for (k = 0, l = 0; k < len; ++k)
1616 if (den_s[k] == 0)
1617 pden[l++] = den_r[k];
1619 for (k = 0; k < len; ++k)
1620 if (den_p[k] == 0)
1621 break;
1623 dpoly n(no_param, num_s);
1624 dpoly D(no_param, den_s[k], 1);
1625 for ( ; ++k < len; )
1626 if (den_p[k] == 0) {
1627 dpoly fact(no_param, den_s[k], 1);
1628 D *= fact;
1631 if (no_param + only_param == len) {
1632 mpq_set_si(tcount, 0, 1);
1633 n.div(D, tcount, one);
1635 ZZ qn, qd;
1636 value2zz(mpq_numref(tcount), qn);
1637 value2zz(mpq_denref(tcount), qd);
1639 qn *= c;
1640 qd *= cd;
1642 if (qn != 0)
1643 reduce(qn, qd, num_p, pden);
1644 } else {
1645 dpoly_r * r = 0;
1647 for (k = 0; k < len; ++k) {
1648 if (den_s[k] == 0 || den_p[k] == 0)
1649 continue;
1651 dpoly pd(no_param-1, den_s[k], 1);
1652 int s = den_p[k] < 0 ? -1 : 1;
1654 if (r == 0)
1655 r = new dpoly_r(n, pd, k, s, len);
1656 else {
1657 dpoly_r *nr = new dpoly_r(r, pd, k, s, len);
1658 delete r;
1659 r = nr;
1663 dpoly_r *rc = r->div(D);
1665 rc->denom *= cd;
1667 int common = pden.NumRows();
1668 vector< dpoly_r_term * >& final = rc->c[rc->len-1];
1669 int rows;
1670 for (int j = 0; j < final.size(); ++j) {
1671 if (final[j]->coeff == 0)
1672 continue;
1673 rows = common;
1674 pden.SetDims(rows, pden.NumCols());
1675 for (int k = 0; k < rc->dim; ++k) {
1676 int n = final[j]->powers[k];
1677 if (n == 0)
1678 continue;
1679 int abs_n = n < 0 ? -n : n;
1680 pden.SetDims(rows+abs_n, pden.NumCols());
1681 for (int l = 0; l < abs_n; ++l) {
1682 if (n > 0)
1683 pden[rows+l] = den_r[k];
1684 else
1685 pden[rows+l] = -den_r[k];
1687 rows += abs_n;
1689 final[j]->coeff *= c;
1690 reduce(final[j]->coeff, rc->denom, num_p, pden);
1693 delete rc;
1694 delete r;
1699 void reducer::handle_polar(Polyhedron *C, int s)
1701 assert(C->NbRays-1 == dim);
1703 sgn = s;
1705 lattice_point(P->Ray[j]+1, C, vertex);
1707 mat_ZZ den;
1708 den.SetDims(dim, dim);
1710 int r;
1711 for (r = 0; r < dim; ++r)
1712 values2zz(C->Ray[r]+1, den[r], dim);
1714 reduce(sgn, one, vertex, den);
1717 void reducer::start(unsigned MaxRays)
1719 for (j = 0; j < P->NbRays; ++j) {
1720 Polyhedron *C = supporting_cone(P, j);
1721 decompose(C, MaxRays);
1725 // incremental counter
1726 struct icounter : public reducer {
1727 mpq_t count;
1729 icounter(Polyhedron *P) : reducer(P) {
1730 mpq_init(count);
1731 lower = 1;
1732 untouched = 0;
1734 ~icounter() {
1735 mpq_clear(count);
1737 virtual void base(ZZ& c, ZZ& cd, vec_ZZ& num, mat_ZZ& den_f);
1740 void icounter::base(ZZ& c, ZZ& cd, vec_ZZ& num, mat_ZZ& den_f)
1742 int r;
1743 unsigned len = den_f.NumRows(); // number of factors in den
1744 vec_ZZ den_s;
1745 den_s.SetLength(len);
1746 ZZ num_s = num[0];
1747 for (r = 0; r < len; ++r)
1748 den_s[r] = den_f[r][0];
1749 normalize(c, num_s, den_s);
1751 dpoly n(len, num_s);
1752 dpoly D(len, den_s[0], 1);
1753 for (int k = 1; k < len; ++k) {
1754 dpoly fact(len, den_s[k], 1);
1755 D *= fact;
1757 mpq_set_si(tcount, 0, 1);
1758 n.div(D, tcount, one);
1759 zz2value(c, tn);
1760 zz2value(cd, td);
1761 mpz_mul(mpq_numref(tcount), mpq_numref(tcount), tn);
1762 mpz_mul(mpq_denref(tcount), mpq_denref(tcount), td);
1763 mpq_canonicalize(tcount);
1764 mpq_add(count, count, tcount);
1767 struct partial_reducer : public reducer {
1768 gen_fun * gf;
1770 partial_reducer(Polyhedron *P, unsigned nparam) : reducer(P) {
1771 gf = new gen_fun;
1772 lower = nparam;
1773 untouched = nparam;
1775 ~partial_reducer() {
1777 virtual void base(ZZ& c, ZZ& cd, vec_ZZ& num, mat_ZZ& den_f);
1778 void start(unsigned MaxRays);
1781 void partial_reducer::base(ZZ& c, ZZ& cd, vec_ZZ& num, mat_ZZ& den_f)
1783 gf->add(c, cd, num, den_f);
1786 void partial_reducer::start(unsigned MaxRays)
1788 for (j = 0; j < P->NbRays; ++j) {
1789 if (!value_pos_p(P->Ray[j][dim+1]))
1790 continue;
1792 Polyhedron *C = supporting_cone(P, j);
1793 decompose(C, MaxRays);
1797 typedef Polyhedron * Polyhedron_p;
1799 void barvinok_count(Polyhedron *P, Value* result, unsigned NbMaxCons)
1801 Polyhedron ** vcone;
1802 ZZ sign;
1803 unsigned dim;
1804 int allocated = 0;
1805 Value factor;
1806 Polyhedron *Q;
1807 int r = 0;
1809 if (emptyQ(P)) {
1810 value_set_si(*result, 0);
1811 return;
1813 if (P->NbBid == 0)
1814 for (; r < P->NbRays; ++r)
1815 if (value_zero_p(P->Ray[r][P->Dimension+1]))
1816 break;
1817 if (P->NbBid !=0 || r < P->NbRays) {
1818 value_set_si(*result, -1);
1819 return;
1821 if (P->NbEq != 0) {
1822 P = remove_equalities(P);
1823 if (emptyQ(P)) {
1824 Polyhedron_Free(P);
1825 value_set_si(*result, 0);
1826 return;
1828 allocated = 1;
1830 value_init(factor);
1831 value_set_si(factor, 1);
1832 Q = Polyhedron_Reduce(P, &factor);
1833 if (Q) {
1834 if (allocated)
1835 Polyhedron_Free(P);
1836 P = Q;
1837 allocated = 1;
1839 if (P->Dimension == 0) {
1840 value_assign(*result, factor);
1841 if (allocated)
1842 Polyhedron_Free(P);
1843 value_clear(factor);
1844 return;
1847 icounter cnt(P);
1848 cnt.start(NbMaxCons);
1850 assert(value_one_p(&cnt.count[0]._mp_den));
1851 value_multiply(*result, &cnt.count[0]._mp_num, factor);
1853 if (allocated)
1854 Polyhedron_Free(P);
1855 value_clear(factor);
1858 static void uni_polynom(int param, Vector *c, evalue *EP)
1860 unsigned dim = c->Size-2;
1861 value_init(EP->d);
1862 value_set_si(EP->d,0);
1863 EP->x.p = new_enode(polynomial, dim+1, param+1);
1864 for (int j = 0; j <= dim; ++j)
1865 evalue_set(&EP->x.p->arr[j], c->p[j], c->p[dim+1]);
1868 static void multi_polynom(Vector *c, evalue* X, evalue *EP)
1870 unsigned dim = c->Size-2;
1871 evalue EC;
1873 value_init(EC.d);
1874 evalue_set(&EC, c->p[dim], c->p[dim+1]);
1876 value_init(EP->d);
1877 evalue_set(EP, c->p[dim], c->p[dim+1]);
1879 for (int i = dim-1; i >= 0; --i) {
1880 emul(X, EP);
1881 value_assign(EC.x.n, c->p[i]);
1882 eadd(&EC, EP);
1884 free_evalue_refs(&EC);
1887 Polyhedron *unfringe (Polyhedron *P, unsigned MaxRays)
1889 int len = P->Dimension+2;
1890 Polyhedron *T, *R = P;
1891 Value g;
1892 value_init(g);
1893 Vector *row = Vector_Alloc(len);
1894 value_set_si(row->p[0], 1);
1896 R = DomainConstraintSimplify(Polyhedron_Copy(P), MaxRays);
1898 Matrix *M = Matrix_Alloc(2, len-1);
1899 value_set_si(M->p[1][len-2], 1);
1900 for (int v = 0; v < P->Dimension; ++v) {
1901 value_set_si(M->p[0][v], 1);
1902 Polyhedron *I = Polyhedron_Image(P, M, 2+1);
1903 value_set_si(M->p[0][v], 0);
1904 for (int r = 0; r < I->NbConstraints; ++r) {
1905 if (value_zero_p(I->Constraint[r][0]))
1906 continue;
1907 if (value_zero_p(I->Constraint[r][1]))
1908 continue;
1909 if (value_one_p(I->Constraint[r][1]))
1910 continue;
1911 if (value_mone_p(I->Constraint[r][1]))
1912 continue;
1913 value_absolute(g, I->Constraint[r][1]);
1914 Vector_Set(row->p+1, 0, len-2);
1915 value_division(row->p[1+v], I->Constraint[r][1], g);
1916 mpz_fdiv_q(row->p[len-1], I->Constraint[r][2], g);
1917 T = R;
1918 R = AddConstraints(row->p, 1, R, MaxRays);
1919 if (T != P)
1920 Polyhedron_Free(T);
1922 Polyhedron_Free(I);
1924 Matrix_Free(M);
1925 Vector_Free(row);
1926 value_clear(g);
1927 return R;
1930 static Polyhedron *reduce_domain(Polyhedron *D, Matrix *CT, Polyhedron *CEq,
1931 Polyhedron **fVD, int nd, unsigned MaxRays)
1933 assert(CEq);
1935 Polyhedron *Dt;
1936 Dt = CT ? DomainPreimage(D, CT, MaxRays) : D;
1937 Polyhedron *rVD = DomainIntersection(Dt, CEq, MaxRays);
1939 /* if rVD is empty or too small in geometric dimension */
1940 if(!rVD || emptyQ(rVD) ||
1941 (rVD->Dimension-rVD->NbEq < Dt->Dimension-Dt->NbEq-CEq->NbEq)) {
1942 if(rVD)
1943 Domain_Free(rVD);
1944 if (CT)
1945 Domain_Free(Dt);
1946 return 0; /* empty validity domain */
1949 if (CT)
1950 Domain_Free(Dt);
1952 fVD[nd] = Domain_Copy(rVD);
1953 for (int i = 0 ; i < nd; ++i) {
1954 Polyhedron *I = DomainIntersection(fVD[nd], fVD[i], MaxRays);
1955 if (emptyQ(I)) {
1956 Domain_Free(I);
1957 continue;
1959 Polyhedron *F = DomainSimplify(I, fVD[nd], MaxRays);
1960 if (F->NbEq == 1) {
1961 Polyhedron *T = rVD;
1962 rVD = DomainDifference(rVD, F, MaxRays);
1963 Domain_Free(T);
1965 Domain_Free(F);
1966 Domain_Free(I);
1969 rVD = DomainConstraintSimplify(rVD, MaxRays);
1970 if (emptyQ(rVD)) {
1971 Domain_Free(fVD[nd]);
1972 Domain_Free(rVD);
1973 return 0;
1976 Value c;
1977 value_init(c);
1978 barvinok_count(rVD, &c, MaxRays);
1979 if (value_zero_p(c)) {
1980 Domain_Free(rVD);
1981 rVD = 0;
1983 value_clear(c);
1985 return rVD;
1988 static bool Polyhedron_is_infinite(Polyhedron *P, unsigned nparam)
1990 int r;
1991 for (r = 0; r < P->NbRays; ++r)
1992 if (value_zero_p(P->Ray[r][0]) ||
1993 value_zero_p(P->Ray[r][P->Dimension+1])) {
1994 int i;
1995 for (i = P->Dimension - nparam; i < P->Dimension; ++i)
1996 if (value_notzero_p(P->Ray[r][i+1]))
1997 break;
1998 if (i >= P->Dimension)
1999 break;
2001 return r < P->NbRays;
2004 /* Check whether all rays point in the positive directions
2005 * for the parameters
2007 static bool Polyhedron_has_positive_rays(Polyhedron *P, unsigned nparam)
2009 int r;
2010 for (r = 0; r < P->NbRays; ++r)
2011 if (value_zero_p(P->Ray[r][P->Dimension+1])) {
2012 int i;
2013 for (i = P->Dimension - nparam; i < P->Dimension; ++i)
2014 if (value_neg_p(P->Ray[r][i+1]))
2015 return false;
2017 return true;
2020 typedef evalue * evalue_p;
2022 struct enumerator : public polar_decomposer {
2023 vec_ZZ lambda;
2024 unsigned dim, nbV;
2025 evalue ** vE;
2026 int _i;
2027 mat_ZZ rays;
2028 vec_ZZ den;
2029 ZZ sign;
2030 Polyhedron *P;
2031 Param_Vertices *V;
2032 term_info num;
2033 Vector *c;
2034 mpq_t count;
2036 enumerator(Polyhedron *P, unsigned dim, unsigned nbV) {
2037 this->P = P;
2038 this->dim = dim;
2039 this->nbV = nbV;
2040 randomvector(P, lambda, dim);
2041 rays.SetDims(dim, dim);
2042 den.SetLength(dim);
2043 c = Vector_Alloc(dim+2);
2045 vE = new evalue_p[nbV];
2046 for (int j = 0; j < nbV; ++j)
2047 vE[j] = 0;
2049 mpq_init(count);
2052 void decompose_at(Param_Vertices *V, int _i, unsigned MaxRays) {
2053 Polyhedron *C = supporting_cone_p(P, V);
2054 this->_i = _i;
2055 this->V = V;
2057 vE[_i] = new evalue;
2058 value_init(vE[_i]->d);
2059 evalue_set_si(vE[_i], 0, 1);
2061 decompose(C, MaxRays);
2064 ~enumerator() {
2065 mpq_clear(count);
2066 Vector_Free(c);
2068 for (int j = 0; j < nbV; ++j)
2069 if (vE[j]) {
2070 free_evalue_refs(vE[j]);
2071 delete vE[j];
2073 delete [] vE;
2076 virtual void handle_polar(Polyhedron *P, int sign);
2079 void enumerator::handle_polar(Polyhedron *C, int s)
2081 int r = 0;
2082 assert(C->NbRays-1 == dim);
2083 add_rays(rays, C, &r);
2084 for (int k = 0; k < dim; ++k) {
2085 assert(lambda * rays[k] != 0);
2088 sign = s;
2090 lattice_point(V, C, lambda, &num, 0);
2091 den = rays * lambda;
2092 normalize(sign, num.constant, den);
2094 dpoly n(dim, den[0], 1);
2095 for (int k = 1; k < dim; ++k) {
2096 dpoly fact(dim, den[k], 1);
2097 n *= fact;
2099 if (num.E != NULL) {
2100 ZZ one(INIT_VAL, 1);
2101 dpoly_n d(dim, num.constant, one);
2102 d.div(n, c, sign);
2103 evalue EV;
2104 multi_polynom(c, num.E, &EV);
2105 eadd(&EV , vE[_i]);
2106 free_evalue_refs(&EV);
2107 free_evalue_refs(num.E);
2108 delete num.E;
2109 } else if (num.pos != -1) {
2110 dpoly_n d(dim, num.constant, num.coeff);
2111 d.div(n, c, sign);
2112 evalue EV;
2113 uni_polynom(num.pos, c, &EV);
2114 eadd(&EV , vE[_i]);
2115 free_evalue_refs(&EV);
2116 } else {
2117 mpq_set_si(count, 0, 1);
2118 dpoly d(dim, num.constant);
2119 d.div(n, count, sign);
2120 evalue EV;
2121 value_init(EV.d);
2122 evalue_set(&EV, &count[0]._mp_num, &count[0]._mp_den);
2123 eadd(&EV , vE[_i]);
2124 free_evalue_refs(&EV);
2128 evalue* barvinok_enumerate_ev(Polyhedron *P, Polyhedron* C, unsigned MaxRays)
2130 //P = unfringe(P, MaxRays);
2131 Polyhedron *CEq = NULL, *rVD, *pVD, *CA;
2132 Matrix *CT = NULL;
2133 Param_Polyhedron *PP = NULL;
2134 Param_Domain *D, *next;
2135 Param_Vertices *V;
2136 int r = 0;
2137 unsigned nparam = C->Dimension;
2138 evalue *eres;
2139 ALLOC(evalue, eres);
2140 value_init(eres->d);
2141 value_set_si(eres->d, 0);
2143 evalue factor;
2144 value_init(factor.d);
2145 evalue_set_si(&factor, 1, 1);
2147 CA = align_context(C, P->Dimension, MaxRays);
2148 P = DomainIntersection(P, CA, MaxRays);
2149 Polyhedron_Free(CA);
2151 if (C->Dimension == 0 || emptyQ(P)) {
2152 constant:
2153 eres->x.p = new_enode(partition, 2, C->Dimension);
2154 EVALUE_SET_DOMAIN(eres->x.p->arr[0],
2155 DomainConstraintSimplify(CEq ? CEq : Polyhedron_Copy(C), MaxRays));
2156 value_set_si(eres->x.p->arr[1].d, 1);
2157 value_init(eres->x.p->arr[1].x.n);
2158 if (emptyQ(P))
2159 value_set_si(eres->x.p->arr[1].x.n, 0);
2160 else
2161 barvinok_count(P, &eres->x.p->arr[1].x.n, MaxRays);
2162 out:
2163 emul(&factor, eres);
2164 reduce_evalue(eres);
2165 free_evalue_refs(&factor);
2166 Polyhedron_Free(P);
2167 if (CT)
2168 Matrix_Free(CT);
2169 if (PP)
2170 Param_Polyhedron_Free(PP);
2172 return eres;
2174 if (Polyhedron_is_infinite(P, nparam))
2175 goto constant;
2177 if (P->NbEq != 0) {
2178 Matrix *f;
2179 P = remove_equalities_p(P, P->Dimension-nparam, &f);
2180 mask(f, &factor);
2181 Matrix_Free(f);
2183 if (P->Dimension == nparam) {
2184 CEq = P;
2185 P = Universe_Polyhedron(0);
2186 goto constant;
2189 Polyhedron *Q = ParamPolyhedron_Reduce(P, P->Dimension-nparam, &factor);
2190 if (Q) {
2191 Polyhedron_Free(P);
2192 if (Q->Dimension == nparam) {
2193 CEq = Q;
2194 P = Universe_Polyhedron(0);
2195 goto constant;
2197 P = Q;
2199 Polyhedron *oldP = P;
2200 PP = Polyhedron2Param_SimplifiedDomain(&P,C,MaxRays,&CEq,&CT);
2201 if (P != oldP)
2202 Polyhedron_Free(oldP);
2204 if (isIdentity(CT)) {
2205 Matrix_Free(CT);
2206 CT = NULL;
2207 } else {
2208 assert(CT->NbRows != CT->NbColumns);
2209 if (CT->NbRows == 1) // no more parameters
2210 goto constant;
2211 nparam = CT->NbRows - 1;
2214 unsigned dim = P->Dimension - nparam;
2216 enumerator et(P, dim, PP->nbV);
2218 int nd;
2219 for (nd = 0, D=PP->D; D; ++nd, D=D->next);
2220 struct section { Polyhedron *D; evalue E; };
2221 section *s = new section[nd];
2222 Polyhedron **fVD = new Polyhedron_p[nd];
2224 for(nd = 0, D=PP->D; D; D=next) {
2225 next = D->next;
2227 Polyhedron *rVD = reduce_domain(D->Domain, CT, CEq,
2228 fVD, nd, MaxRays);
2229 if (!rVD)
2230 continue;
2232 pVD = CT ? DomainImage(rVD,CT,MaxRays) : rVD;
2234 value_init(s[nd].E.d);
2235 evalue_set_si(&s[nd].E, 0, 1);
2237 FORALL_PVertex_in_ParamPolyhedron(V,D,PP) // _i is internal counter
2238 if (!et.vE[_i])
2239 et.decompose_at(V, _i, MaxRays);
2240 eadd(et.vE[_i] , &s[nd].E);
2241 END_FORALL_PVertex_in_ParamPolyhedron;
2242 reduce_in_domain(&s[nd].E, pVD);
2244 if (CT)
2245 addeliminatedparams_evalue(&s[nd].E, CT);
2246 s[nd].D = rVD;
2247 ++nd;
2248 if (rVD != pVD)
2249 Domain_Free(pVD);
2252 if (nd == 0)
2253 evalue_set_si(eres, 0, 1);
2254 else {
2255 eres->x.p = new_enode(partition, 2*nd, C->Dimension);
2256 for (int j = 0; j < nd; ++j) {
2257 EVALUE_SET_DOMAIN(eres->x.p->arr[2*j], s[j].D);
2258 value_clear(eres->x.p->arr[2*j+1].d);
2259 eres->x.p->arr[2*j+1] = s[j].E;
2260 Domain_Free(fVD[j]);
2263 delete [] s;
2264 delete [] fVD;
2267 if (CEq)
2268 Polyhedron_Free(CEq);
2270 goto out;
2273 Enumeration* barvinok_enumerate(Polyhedron *P, Polyhedron* C, unsigned MaxRays)
2275 evalue *EP = barvinok_enumerate_ev(P, C, MaxRays);
2277 return partition2enumeration(EP);
2280 static void SwapColumns(Value **V, int n, int i, int j)
2282 for (int r = 0; r < n; ++r)
2283 value_swap(V[r][i], V[r][j]);
2286 static void SwapColumns(Polyhedron *P, int i, int j)
2288 SwapColumns(P->Constraint, P->NbConstraints, i, j);
2289 SwapColumns(P->Ray, P->NbRays, i, j);
2292 static void negative_test_constraint(Value *l, Value *u, Value *c, int pos,
2293 int len, Value *v)
2295 value_oppose(*v, u[pos+1]);
2296 Vector_Combine(l+1, u+1, c+1, *v, l[pos+1], len-1);
2297 value_multiply(*v, *v, l[pos+1]);
2298 value_substract(c[len-1], c[len-1], *v);
2299 value_set_si(*v, -1);
2300 Vector_Scale(c+1, c+1, *v, len-1);
2301 value_decrement(c[len-1], c[len-1]);
2302 ConstraintSimplify(c, c, len, v);
2305 static bool parallel_constraints(Value *l, Value *u, Value *c, int pos,
2306 int len)
2308 bool parallel;
2309 Value g1;
2310 Value g2;
2311 value_init(g1);
2312 value_init(g2);
2314 Vector_Gcd(&l[1+pos], len, &g1);
2315 Vector_Gcd(&u[1+pos], len, &g2);
2316 Vector_Combine(l+1+pos, u+1+pos, c+1, g2, g1, len);
2317 parallel = First_Non_Zero(c+1, len) == -1;
2319 value_clear(g1);
2320 value_clear(g2);
2322 return parallel;
2325 static void negative_test_constraint7(Value *l, Value *u, Value *c, int pos,
2326 int exist, int len, Value *v)
2328 Value g;
2329 value_init(g);
2331 Vector_Gcd(&u[1+pos], exist, v);
2332 Vector_Gcd(&l[1+pos], exist, &g);
2333 Vector_Combine(l+1, u+1, c+1, *v, g, len-1);
2334 value_multiply(*v, *v, g);
2335 value_substract(c[len-1], c[len-1], *v);
2336 value_set_si(*v, -1);
2337 Vector_Scale(c+1, c+1, *v, len-1);
2338 value_decrement(c[len-1], c[len-1]);
2339 ConstraintSimplify(c, c, len, v);
2341 value_clear(g);
2344 static void oppose_constraint(Value *c, int len, Value *v)
2346 value_set_si(*v, -1);
2347 Vector_Scale(c+1, c+1, *v, len-1);
2348 value_decrement(c[len-1], c[len-1]);
2351 static bool SplitOnConstraint(Polyhedron *P, int i, int l, int u,
2352 int nvar, int len, int exist, int MaxRays,
2353 Vector *row, Value& f, bool independent,
2354 Polyhedron **pos, Polyhedron **neg)
2356 negative_test_constraint(P->Constraint[l], P->Constraint[u],
2357 row->p, nvar+i, len, &f);
2358 *neg = AddConstraints(row->p, 1, P, MaxRays);
2360 /* We found an independent, but useless constraint
2361 * Maybe we should detect this earlier and not
2362 * mark the variable as INDEPENDENT
2364 if (emptyQ((*neg))) {
2365 Polyhedron_Free(*neg);
2366 return false;
2369 oppose_constraint(row->p, len, &f);
2370 *pos = AddConstraints(row->p, 1, P, MaxRays);
2372 if (emptyQ((*pos))) {
2373 Polyhedron_Free(*neg);
2374 Polyhedron_Free(*pos);
2375 return false;
2378 return true;
2382 * unimodularly transform P such that constraint r is transformed
2383 * into a constraint that involves only a single (the first)
2384 * existential variable
2387 static Polyhedron *rotate_along(Polyhedron *P, int r, int nvar, int exist,
2388 unsigned MaxRays)
2390 Value g;
2391 value_init(g);
2393 Vector *row = Vector_Alloc(exist);
2394 Vector_Copy(P->Constraint[r]+1+nvar, row->p, exist);
2395 Vector_Gcd(row->p, exist, &g);
2396 if (value_notone_p(g))
2397 Vector_AntiScale(row->p, row->p, g, exist);
2398 value_clear(g);
2400 Matrix *M = unimodular_complete(row);
2401 Matrix *M2 = Matrix_Alloc(P->Dimension+1, P->Dimension+1);
2402 for (r = 0; r < nvar; ++r)
2403 value_set_si(M2->p[r][r], 1);
2404 for ( ; r < nvar+exist; ++r)
2405 Vector_Copy(M->p[r-nvar], M2->p[r]+nvar, exist);
2406 for ( ; r < P->Dimension+1; ++r)
2407 value_set_si(M2->p[r][r], 1);
2408 Polyhedron *T = Polyhedron_Image(P, M2, MaxRays);
2410 Matrix_Free(M2);
2411 Matrix_Free(M);
2412 Vector_Free(row);
2414 return T;
2417 static bool SplitOnVar(Polyhedron *P, int i,
2418 int nvar, int len, int exist, int MaxRays,
2419 Vector *row, Value& f, bool independent,
2420 Polyhedron **pos, Polyhedron **neg)
2422 int j;
2424 for (int l = P->NbEq; l < P->NbConstraints; ++l) {
2425 if (value_negz_p(P->Constraint[l][nvar+i+1]))
2426 continue;
2428 if (independent) {
2429 for (j = 0; j < exist; ++j)
2430 if (j != i && value_notzero_p(P->Constraint[l][nvar+j+1]))
2431 break;
2432 if (j < exist)
2433 continue;
2436 for (int u = P->NbEq; u < P->NbConstraints; ++u) {
2437 if (value_posz_p(P->Constraint[u][nvar+i+1]))
2438 continue;
2440 if (independent) {
2441 for (j = 0; j < exist; ++j)
2442 if (j != i && value_notzero_p(P->Constraint[u][nvar+j+1]))
2443 break;
2444 if (j < exist)
2445 continue;
2448 if (SplitOnConstraint(P, i, l, u,
2449 nvar, len, exist, MaxRays,
2450 row, f, independent,
2451 pos, neg)) {
2452 if (independent) {
2453 if (i != 0)
2454 SwapColumns(*neg, nvar+1, nvar+1+i);
2456 return true;
2461 return false;
2464 static bool double_bound_pair(Polyhedron *P, int nvar, int exist,
2465 int i, int l1, int l2,
2466 Polyhedron **pos, Polyhedron **neg)
2468 Value f;
2469 value_init(f);
2470 Vector *row = Vector_Alloc(P->Dimension+2);
2471 value_set_si(row->p[0], 1);
2472 value_oppose(f, P->Constraint[l1][nvar+i+1]);
2473 Vector_Combine(P->Constraint[l1]+1, P->Constraint[l2]+1,
2474 row->p+1,
2475 P->Constraint[l2][nvar+i+1], f,
2476 P->Dimension+1);
2477 ConstraintSimplify(row->p, row->p, P->Dimension+2, &f);
2478 *pos = AddConstraints(row->p, 1, P, 0);
2479 value_set_si(f, -1);
2480 Vector_Scale(row->p+1, row->p+1, f, P->Dimension+1);
2481 value_decrement(row->p[P->Dimension+1], row->p[P->Dimension+1]);
2482 *neg = AddConstraints(row->p, 1, P, 0);
2483 Vector_Free(row);
2484 value_clear(f);
2486 return !emptyQ((*pos)) && !emptyQ((*neg));
2489 static bool double_bound(Polyhedron *P, int nvar, int exist,
2490 Polyhedron **pos, Polyhedron **neg)
2492 for (int i = 0; i < exist; ++i) {
2493 int l1, l2;
2494 for (l1 = P->NbEq; l1 < P->NbConstraints; ++l1) {
2495 if (value_negz_p(P->Constraint[l1][nvar+i+1]))
2496 continue;
2497 for (l2 = l1 + 1; l2 < P->NbConstraints; ++l2) {
2498 if (value_negz_p(P->Constraint[l2][nvar+i+1]))
2499 continue;
2500 if (double_bound_pair(P, nvar, exist, i, l1, l2, pos, neg))
2501 return true;
2504 for (l1 = P->NbEq; l1 < P->NbConstraints; ++l1) {
2505 if (value_posz_p(P->Constraint[l1][nvar+i+1]))
2506 continue;
2507 if (l1 < P->NbConstraints)
2508 for (l2 = l1 + 1; l2 < P->NbConstraints; ++l2) {
2509 if (value_posz_p(P->Constraint[l2][nvar+i+1]))
2510 continue;
2511 if (double_bound_pair(P, nvar, exist, i, l1, l2, pos, neg))
2512 return true;
2515 return false;
2517 return false;
2520 enum constraint {
2521 ALL_POS = 1 << 0,
2522 ONE_NEG = 1 << 1,
2523 INDEPENDENT = 1 << 2,
2524 ROT_NEG = 1 << 3
2527 static evalue* enumerate_or(Polyhedron *D,
2528 unsigned exist, unsigned nparam, unsigned MaxRays)
2530 #ifdef DEBUG_ER
2531 fprintf(stderr, "\nER: Or\n");
2532 #endif /* DEBUG_ER */
2534 Polyhedron *N = D->next;
2535 D->next = 0;
2536 evalue *EP =
2537 barvinok_enumerate_e(D, exist, nparam, MaxRays);
2538 Polyhedron_Free(D);
2540 for (D = N; D; D = N) {
2541 N = D->next;
2542 D->next = 0;
2544 evalue *EN =
2545 barvinok_enumerate_e(D, exist, nparam, MaxRays);
2547 eor(EN, EP);
2548 free_evalue_refs(EN);
2549 free(EN);
2550 Polyhedron_Free(D);
2553 reduce_evalue(EP);
2555 return EP;
2558 static evalue* enumerate_sum(Polyhedron *P,
2559 unsigned exist, unsigned nparam, unsigned MaxRays)
2561 int nvar = P->Dimension - exist - nparam;
2562 int toswap = nvar < exist ? nvar : exist;
2563 for (int i = 0; i < toswap; ++i)
2564 SwapColumns(P, 1 + i, nvar+exist - i);
2565 nparam += nvar;
2567 #ifdef DEBUG_ER
2568 fprintf(stderr, "\nER: Sum\n");
2569 #endif /* DEBUG_ER */
2571 evalue *EP = barvinok_enumerate_e(P, exist, nparam, MaxRays);
2573 for (int i = 0; i < /* nvar */ nparam; ++i) {
2574 Matrix *C = Matrix_Alloc(1, 1 + nparam + 1);
2575 value_set_si(C->p[0][0], 1);
2576 evalue split;
2577 value_init(split.d);
2578 value_set_si(split.d, 0);
2579 split.x.p = new_enode(partition, 4, nparam);
2580 value_set_si(C->p[0][1+i], 1);
2581 Matrix *C2 = Matrix_Copy(C);
2582 EVALUE_SET_DOMAIN(split.x.p->arr[0],
2583 Constraints2Polyhedron(C2, MaxRays));
2584 Matrix_Free(C2);
2585 evalue_set_si(&split.x.p->arr[1], 1, 1);
2586 value_set_si(C->p[0][1+i], -1);
2587 value_set_si(C->p[0][1+nparam], -1);
2588 EVALUE_SET_DOMAIN(split.x.p->arr[2],
2589 Constraints2Polyhedron(C, MaxRays));
2590 evalue_set_si(&split.x.p->arr[3], 1, 1);
2591 emul(&split, EP);
2592 free_evalue_refs(&split);
2593 Matrix_Free(C);
2595 reduce_evalue(EP);
2596 evalue_range_reduction(EP);
2598 evalue_frac2floor(EP);
2600 evalue *sum = esum(EP, nvar);
2602 free_evalue_refs(EP);
2603 free(EP);
2604 EP = sum;
2606 evalue_range_reduction(EP);
2608 return EP;
2611 static evalue* split_sure(Polyhedron *P, Polyhedron *S,
2612 unsigned exist, unsigned nparam, unsigned MaxRays)
2614 int nvar = P->Dimension - exist - nparam;
2616 Matrix *M = Matrix_Alloc(exist, S->Dimension+2);
2617 for (int i = 0; i < exist; ++i)
2618 value_set_si(M->p[i][nvar+i+1], 1);
2619 Polyhedron *O = S;
2620 S = DomainAddRays(S, M, MaxRays);
2621 Polyhedron_Free(O);
2622 Polyhedron *F = DomainAddRays(P, M, MaxRays);
2623 Polyhedron *D = DomainDifference(F, S, MaxRays);
2624 O = D;
2625 D = Disjoint_Domain(D, 0, MaxRays);
2626 Polyhedron_Free(F);
2627 Domain_Free(O);
2628 Matrix_Free(M);
2630 M = Matrix_Alloc(P->Dimension+1-exist, P->Dimension+1);
2631 for (int j = 0; j < nvar; ++j)
2632 value_set_si(M->p[j][j], 1);
2633 for (int j = 0; j < nparam+1; ++j)
2634 value_set_si(M->p[nvar+j][nvar+exist+j], 1);
2635 Polyhedron *T = Polyhedron_Image(S, M, MaxRays);
2636 evalue *EP = barvinok_enumerate_e(T, 0, nparam, MaxRays);
2637 Polyhedron_Free(S);
2638 Polyhedron_Free(T);
2639 Matrix_Free(M);
2641 for (Polyhedron *Q = D; Q; Q = Q->next) {
2642 Polyhedron *N = Q->next;
2643 Q->next = 0;
2644 T = DomainIntersection(P, Q, MaxRays);
2645 evalue *E = barvinok_enumerate_e(T, exist, nparam, MaxRays);
2646 eadd(E, EP);
2647 free_evalue_refs(E);
2648 free(E);
2649 Polyhedron_Free(T);
2650 Q->next = N;
2652 Domain_Free(D);
2653 return EP;
2656 static evalue* enumerate_sure(Polyhedron *P,
2657 unsigned exist, unsigned nparam, unsigned MaxRays)
2659 int i;
2660 Polyhedron *S = P;
2661 int nvar = P->Dimension - exist - nparam;
2662 Value lcm;
2663 Value f;
2664 value_init(lcm);
2665 value_init(f);
2667 for (i = 0; i < exist; ++i) {
2668 Matrix *M = Matrix_Alloc(S->NbConstraints, S->Dimension+2);
2669 int c = 0;
2670 value_set_si(lcm, 1);
2671 for (int j = 0; j < S->NbConstraints; ++j) {
2672 if (value_negz_p(S->Constraint[j][1+nvar+i]))
2673 continue;
2674 if (value_one_p(S->Constraint[j][1+nvar+i]))
2675 continue;
2676 value_lcm(lcm, S->Constraint[j][1+nvar+i], &lcm);
2679 for (int j = 0; j < S->NbConstraints; ++j) {
2680 if (value_negz_p(S->Constraint[j][1+nvar+i]))
2681 continue;
2682 if (value_one_p(S->Constraint[j][1+nvar+i]))
2683 continue;
2684 value_division(f, lcm, S->Constraint[j][1+nvar+i]);
2685 Vector_Scale(S->Constraint[j], M->p[c], f, S->Dimension+2);
2686 value_substract(M->p[c][S->Dimension+1],
2687 M->p[c][S->Dimension+1],
2688 lcm);
2689 value_increment(M->p[c][S->Dimension+1],
2690 M->p[c][S->Dimension+1]);
2691 ++c;
2693 Polyhedron *O = S;
2694 S = AddConstraints(M->p[0], c, S, MaxRays);
2695 if (O != P)
2696 Polyhedron_Free(O);
2697 Matrix_Free(M);
2698 if (emptyQ(S)) {
2699 Polyhedron_Free(S);
2700 value_clear(lcm);
2701 value_clear(f);
2702 return 0;
2705 value_clear(lcm);
2706 value_clear(f);
2708 #ifdef DEBUG_ER
2709 fprintf(stderr, "\nER: Sure\n");
2710 #endif /* DEBUG_ER */
2712 return split_sure(P, S, exist, nparam, MaxRays);
2715 static evalue* enumerate_sure2(Polyhedron *P,
2716 unsigned exist, unsigned nparam, unsigned MaxRays)
2718 int nvar = P->Dimension - exist - nparam;
2719 int r;
2720 for (r = 0; r < P->NbRays; ++r)
2721 if (value_one_p(P->Ray[r][0]) &&
2722 value_one_p(P->Ray[r][P->Dimension+1]))
2723 break;
2725 if (r >= P->NbRays)
2726 return 0;
2728 Matrix *M = Matrix_Alloc(nvar + 1 + nparam, P->Dimension+2);
2729 for (int i = 0; i < nvar; ++i)
2730 value_set_si(M->p[i][1+i], 1);
2731 for (int i = 0; i < nparam; ++i)
2732 value_set_si(M->p[i+nvar][1+nvar+exist+i], 1);
2733 Vector_Copy(P->Ray[r]+1+nvar, M->p[nvar+nparam]+1+nvar, exist);
2734 value_set_si(M->p[nvar+nparam][0], 1);
2735 value_set_si(M->p[nvar+nparam][P->Dimension+1], 1);
2736 Polyhedron * F = Rays2Polyhedron(M, MaxRays);
2737 Matrix_Free(M);
2739 Polyhedron *I = DomainIntersection(F, P, MaxRays);
2740 Polyhedron_Free(F);
2742 #ifdef DEBUG_ER
2743 fprintf(stderr, "\nER: Sure2\n");
2744 #endif /* DEBUG_ER */
2746 return split_sure(P, I, exist, nparam, MaxRays);
2749 static evalue* enumerate_cyclic(Polyhedron *P,
2750 unsigned exist, unsigned nparam,
2751 evalue * EP, int r, int p, unsigned MaxRays)
2753 int nvar = P->Dimension - exist - nparam;
2755 /* If EP in its fractional maps only contains references
2756 * to the remainder parameter with appropriate coefficients
2757 * then we could in principle avoid adding existentially
2758 * quantified variables to the validity domains.
2759 * We'd have to replace the remainder by m { p/m }
2760 * and multiply with an appropriate factor that is one
2761 * only in the appropriate range.
2762 * This last multiplication can be avoided if EP
2763 * has a single validity domain with no (further)
2764 * constraints on the remainder parameter
2767 Matrix *CT = Matrix_Alloc(nparam+1, nparam+3);
2768 Matrix *M = Matrix_Alloc(1, 1+nparam+3);
2769 for (int j = 0; j < nparam; ++j)
2770 if (j != p)
2771 value_set_si(CT->p[j][j], 1);
2772 value_set_si(CT->p[p][nparam+1], 1);
2773 value_set_si(CT->p[nparam][nparam+2], 1);
2774 value_set_si(M->p[0][1+p], -1);
2775 value_absolute(M->p[0][1+nparam], P->Ray[0][1+nvar+exist+p]);
2776 value_set_si(M->p[0][1+nparam+1], 1);
2777 Polyhedron *CEq = Constraints2Polyhedron(M, 1);
2778 Matrix_Free(M);
2779 addeliminatedparams_enum(EP, CT, CEq, MaxRays, nparam);
2780 Polyhedron_Free(CEq);
2781 Matrix_Free(CT);
2783 return EP;
2786 static void enumerate_vd_add_ray(evalue *EP, Matrix *Rays, unsigned MaxRays)
2788 if (value_notzero_p(EP->d))
2789 return;
2791 assert(EP->x.p->type == partition);
2792 assert(EP->x.p->pos == EVALUE_DOMAIN(EP->x.p->arr[0])->Dimension);
2793 for (int i = 0; i < EP->x.p->size/2; ++i) {
2794 Polyhedron *D = EVALUE_DOMAIN(EP->x.p->arr[2*i]);
2795 Polyhedron *N = DomainAddRays(D, Rays, MaxRays);
2796 EVALUE_SET_DOMAIN(EP->x.p->arr[2*i], N);
2797 Domain_Free(D);
2801 static evalue* enumerate_line(Polyhedron *P,
2802 unsigned exist, unsigned nparam, unsigned MaxRays)
2804 if (P->NbBid == 0)
2805 return 0;
2807 #ifdef DEBUG_ER
2808 fprintf(stderr, "\nER: Line\n");
2809 #endif /* DEBUG_ER */
2811 int nvar = P->Dimension - exist - nparam;
2812 int i, j;
2813 for (i = 0; i < nparam; ++i)
2814 if (value_notzero_p(P->Ray[0][1+nvar+exist+i]))
2815 break;
2816 assert(i < nparam);
2817 for (j = i+1; j < nparam; ++j)
2818 if (value_notzero_p(P->Ray[0][1+nvar+exist+i]))
2819 break;
2820 assert(j >= nparam); // for now
2822 Matrix *M = Matrix_Alloc(2, P->Dimension+2);
2823 value_set_si(M->p[0][0], 1);
2824 value_set_si(M->p[0][1+nvar+exist+i], 1);
2825 value_set_si(M->p[1][0], 1);
2826 value_set_si(M->p[1][1+nvar+exist+i], -1);
2827 value_absolute(M->p[1][1+P->Dimension], P->Ray[0][1+nvar+exist+i]);
2828 value_decrement(M->p[1][1+P->Dimension], M->p[1][1+P->Dimension]);
2829 Polyhedron *S = AddConstraints(M->p[0], 2, P, MaxRays);
2830 evalue *EP = barvinok_enumerate_e(S, exist, nparam, MaxRays);
2831 Polyhedron_Free(S);
2832 Matrix_Free(M);
2834 return enumerate_cyclic(P, exist, nparam, EP, 0, i, MaxRays);
2837 static int single_param_pos(Polyhedron*P, unsigned exist, unsigned nparam,
2838 int r)
2840 int nvar = P->Dimension - exist - nparam;
2841 if (First_Non_Zero(P->Ray[r]+1, nvar) != -1)
2842 return -1;
2843 int i = First_Non_Zero(P->Ray[r]+1+nvar+exist, nparam);
2844 if (i == -1)
2845 return -1;
2846 if (First_Non_Zero(P->Ray[r]+1+nvar+exist+1, nparam-i-1) != -1)
2847 return -1;
2848 return i;
2851 static evalue* enumerate_remove_ray(Polyhedron *P, int r,
2852 unsigned exist, unsigned nparam, unsigned MaxRays)
2854 #ifdef DEBUG_ER
2855 fprintf(stderr, "\nER: RedundantRay\n");
2856 #endif /* DEBUG_ER */
2858 Value one;
2859 value_init(one);
2860 value_set_si(one, 1);
2861 int len = P->NbRays-1;
2862 Matrix *M = Matrix_Alloc(2 * len, P->Dimension+2);
2863 Vector_Copy(P->Ray[0], M->p[0], r * (P->Dimension+2));
2864 Vector_Copy(P->Ray[r+1], M->p[r], (len-r) * (P->Dimension+2));
2865 for (int j = 0; j < P->NbRays; ++j) {
2866 if (j == r)
2867 continue;
2868 Vector_Combine(P->Ray[j], P->Ray[r], M->p[len+j-(j>r)],
2869 one, P->Ray[j][P->Dimension+1], P->Dimension+2);
2872 P = Rays2Polyhedron(M, MaxRays);
2873 Matrix_Free(M);
2874 evalue *EP = barvinok_enumerate_e(P, exist, nparam, MaxRays);
2875 Polyhedron_Free(P);
2876 value_clear(one);
2878 return EP;
2881 static evalue* enumerate_redundant_ray(Polyhedron *P,
2882 unsigned exist, unsigned nparam, unsigned MaxRays)
2884 assert(P->NbBid == 0);
2885 int nvar = P->Dimension - exist - nparam;
2886 Value m;
2887 value_init(m);
2889 for (int r = 0; r < P->NbRays; ++r) {
2890 if (value_notzero_p(P->Ray[r][P->Dimension+1]))
2891 continue;
2892 int i1 = single_param_pos(P, exist, nparam, r);
2893 if (i1 == -1)
2894 continue;
2895 for (int r2 = r+1; r2 < P->NbRays; ++r2) {
2896 if (value_notzero_p(P->Ray[r2][P->Dimension+1]))
2897 continue;
2898 int i2 = single_param_pos(P, exist, nparam, r2);
2899 if (i2 == -1)
2900 continue;
2901 if (i1 != i2)
2902 continue;
2904 value_division(m, P->Ray[r][1+nvar+exist+i1],
2905 P->Ray[r2][1+nvar+exist+i1]);
2906 value_multiply(m, m, P->Ray[r2][1+nvar+exist+i1]);
2907 /* r2 divides r => r redundant */
2908 if (value_eq(m, P->Ray[r][1+nvar+exist+i1])) {
2909 value_clear(m);
2910 return enumerate_remove_ray(P, r, exist, nparam, MaxRays);
2913 value_division(m, P->Ray[r2][1+nvar+exist+i1],
2914 P->Ray[r][1+nvar+exist+i1]);
2915 value_multiply(m, m, P->Ray[r][1+nvar+exist+i1]);
2916 /* r divides r2 => r2 redundant */
2917 if (value_eq(m, P->Ray[r2][1+nvar+exist+i1])) {
2918 value_clear(m);
2919 return enumerate_remove_ray(P, r2, exist, nparam, MaxRays);
2923 value_clear(m);
2924 return 0;
2927 static Polyhedron *upper_bound(Polyhedron *P,
2928 int pos, Value *max, Polyhedron **R)
2930 Value v;
2931 int r;
2932 value_init(v);
2934 *R = 0;
2935 Polyhedron *N;
2936 Polyhedron *B = 0;
2937 for (Polyhedron *Q = P; Q; Q = N) {
2938 N = Q->next;
2939 for (r = 0; r < P->NbRays; ++r) {
2940 if (value_zero_p(P->Ray[r][P->Dimension+1]) &&
2941 value_pos_p(P->Ray[r][1+pos]))
2942 break;
2944 if (r < P->NbRays) {
2945 Q->next = *R;
2946 *R = Q;
2947 continue;
2948 } else {
2949 Q->next = B;
2950 B = Q;
2952 for (r = 0; r < P->NbRays; ++r) {
2953 if (value_zero_p(P->Ray[r][P->Dimension+1]))
2954 continue;
2955 mpz_fdiv_q(v, P->Ray[r][1+pos], P->Ray[r][1+P->Dimension]);
2956 if ((!Q->next && r == 0) || value_gt(v, *max))
2957 value_assign(*max, v);
2960 value_clear(v);
2961 return B;
2964 static evalue* enumerate_ray(Polyhedron *P,
2965 unsigned exist, unsigned nparam, unsigned MaxRays)
2967 assert(P->NbBid == 0);
2968 int nvar = P->Dimension - exist - nparam;
2970 int r;
2971 for (r = 0; r < P->NbRays; ++r)
2972 if (value_zero_p(P->Ray[r][P->Dimension+1]))
2973 break;
2974 if (r >= P->NbRays)
2975 return 0;
2977 int r2;
2978 for (r2 = r+1; r2 < P->NbRays; ++r2)
2979 if (value_zero_p(P->Ray[r2][P->Dimension+1]))
2980 break;
2981 if (r2 < P->NbRays) {
2982 if (nvar > 0)
2983 return enumerate_sum(P, exist, nparam, MaxRays);
2986 #ifdef DEBUG_ER
2987 fprintf(stderr, "\nER: Ray\n");
2988 #endif /* DEBUG_ER */
2990 Value m;
2991 Value one;
2992 value_init(m);
2993 value_init(one);
2994 value_set_si(one, 1);
2995 int i = single_param_pos(P, exist, nparam, r);
2996 assert(i != -1); // for now;
2998 Matrix *M = Matrix_Alloc(P->NbRays, P->Dimension+2);
2999 for (int j = 0; j < P->NbRays; ++j) {
3000 Vector_Combine(P->Ray[j], P->Ray[r], M->p[j],
3001 one, P->Ray[j][P->Dimension+1], P->Dimension+2);
3003 Polyhedron *S = Rays2Polyhedron(M, MaxRays);
3004 Matrix_Free(M);
3005 Polyhedron *D = DomainDifference(P, S, MaxRays);
3006 Polyhedron_Free(S);
3007 // Polyhedron_Print(stderr, P_VALUE_FMT, D);
3008 assert(value_pos_p(P->Ray[r][1+nvar+exist+i])); // for now
3009 Polyhedron *R;
3010 D = upper_bound(D, nvar+exist+i, &m, &R);
3011 assert(D);
3012 Domain_Free(D);
3014 M = Matrix_Alloc(2, P->Dimension+2);
3015 value_set_si(M->p[0][0], 1);
3016 value_set_si(M->p[1][0], 1);
3017 value_set_si(M->p[0][1+nvar+exist+i], -1);
3018 value_set_si(M->p[1][1+nvar+exist+i], 1);
3019 value_assign(M->p[0][1+P->Dimension], m);
3020 value_oppose(M->p[1][1+P->Dimension], m);
3021 value_addto(M->p[1][1+P->Dimension], M->p[1][1+P->Dimension],
3022 P->Ray[r][1+nvar+exist+i]);
3023 value_decrement(M->p[1][1+P->Dimension], M->p[1][1+P->Dimension]);
3024 // Matrix_Print(stderr, P_VALUE_FMT, M);
3025 D = AddConstraints(M->p[0], 2, P, MaxRays);
3026 // Polyhedron_Print(stderr, P_VALUE_FMT, D);
3027 value_substract(M->p[0][1+P->Dimension], M->p[0][1+P->Dimension],
3028 P->Ray[r][1+nvar+exist+i]);
3029 // Matrix_Print(stderr, P_VALUE_FMT, M);
3030 S = AddConstraints(M->p[0], 1, P, MaxRays);
3031 // Polyhedron_Print(stderr, P_VALUE_FMT, S);
3032 Matrix_Free(M);
3034 evalue *EP = barvinok_enumerate_e(D, exist, nparam, MaxRays);
3035 Polyhedron_Free(D);
3036 value_clear(one);
3037 value_clear(m);
3039 if (value_notone_p(P->Ray[r][1+nvar+exist+i]))
3040 EP = enumerate_cyclic(P, exist, nparam, EP, r, i, MaxRays);
3041 else {
3042 M = Matrix_Alloc(1, nparam+2);
3043 value_set_si(M->p[0][0], 1);
3044 value_set_si(M->p[0][1+i], 1);
3045 enumerate_vd_add_ray(EP, M, MaxRays);
3046 Matrix_Free(M);
3049 if (!emptyQ(S)) {
3050 evalue *E = barvinok_enumerate_e(S, exist, nparam, MaxRays);
3051 eadd(E, EP);
3052 free_evalue_refs(E);
3053 free(E);
3055 Polyhedron_Free(S);
3057 if (R) {
3058 assert(nvar == 0);
3059 evalue *ER = enumerate_or(R, exist, nparam, MaxRays);
3060 eor(ER, EP);
3061 free_evalue_refs(ER);
3062 free(ER);
3065 return EP;
3068 static evalue* new_zero_ep()
3070 evalue *EP;
3071 ALLOC(evalue, EP);
3072 value_init(EP->d);
3073 evalue_set_si(EP, 0, 1);
3074 return EP;
3077 static evalue* enumerate_vd(Polyhedron **PA,
3078 unsigned exist, unsigned nparam, unsigned MaxRays)
3080 Polyhedron *P = *PA;
3081 int nvar = P->Dimension - exist - nparam;
3082 Param_Polyhedron *PP = NULL;
3083 Polyhedron *C = Universe_Polyhedron(nparam);
3084 Polyhedron *CEq;
3085 Matrix *CT;
3086 Polyhedron *PR = P;
3087 PP = Polyhedron2Param_SimplifiedDomain(&PR,C,MaxRays,&CEq,&CT);
3088 Polyhedron_Free(C);
3090 int nd;
3091 Param_Domain *D, *last;
3092 Value c;
3093 value_init(c);
3094 for (nd = 0, D=PP->D; D; D=D->next, ++nd)
3097 Polyhedron **VD = new Polyhedron_p[nd];
3098 Polyhedron **fVD = new Polyhedron_p[nd];
3099 for(nd = 0, D=PP->D; D; D=D->next) {
3100 Polyhedron *rVD = reduce_domain(D->Domain, CT, CEq,
3101 fVD, nd, MaxRays);
3102 if (!rVD)
3103 continue;
3105 VD[nd++] = rVD;
3106 last = D;
3109 evalue *EP = 0;
3111 if (nd == 0)
3112 EP = new_zero_ep();
3114 /* This doesn't seem to have any effect */
3115 if (nd == 1) {
3116 Polyhedron *CA = align_context(VD[0], P->Dimension, MaxRays);
3117 Polyhedron *O = P;
3118 P = DomainIntersection(P, CA, MaxRays);
3119 if (O != *PA)
3120 Polyhedron_Free(O);
3121 Polyhedron_Free(CA);
3122 if (emptyQ(P))
3123 EP = new_zero_ep();
3126 if (!EP && CT->NbColumns != CT->NbRows) {
3127 Polyhedron *CEqr = DomainImage(CEq, CT, MaxRays);
3128 Polyhedron *CA = align_context(CEqr, PR->Dimension, MaxRays);
3129 Polyhedron *I = DomainIntersection(PR, CA, MaxRays);
3130 Polyhedron_Free(CEqr);
3131 Polyhedron_Free(CA);
3132 #ifdef DEBUG_ER
3133 fprintf(stderr, "\nER: Eliminate\n");
3134 #endif /* DEBUG_ER */
3135 nparam -= CT->NbColumns - CT->NbRows;
3136 EP = barvinok_enumerate_e(I, exist, nparam, MaxRays);
3137 nparam += CT->NbColumns - CT->NbRows;
3138 addeliminatedparams_enum(EP, CT, CEq, MaxRays, nparam);
3139 Polyhedron_Free(I);
3141 if (PR != *PA)
3142 Polyhedron_Free(PR);
3143 PR = 0;
3145 if (!EP && nd > 1) {
3146 #ifdef DEBUG_ER
3147 fprintf(stderr, "\nER: VD\n");
3148 #endif /* DEBUG_ER */
3149 for (int i = 0; i < nd; ++i) {
3150 Polyhedron *CA = align_context(VD[i], P->Dimension, MaxRays);
3151 Polyhedron *I = DomainIntersection(P, CA, MaxRays);
3153 if (i == 0)
3154 EP = barvinok_enumerate_e(I, exist, nparam, MaxRays);
3155 else {
3156 evalue *E = barvinok_enumerate_e(I, exist, nparam, MaxRays);
3157 eadd(E, EP);
3158 free_evalue_refs(E);
3159 free(E);
3161 Polyhedron_Free(I);
3162 Polyhedron_Free(CA);
3166 for (int i = 0; i < nd; ++i) {
3167 Polyhedron_Free(VD[i]);
3168 Polyhedron_Free(fVD[i]);
3170 delete [] VD;
3171 delete [] fVD;
3172 value_clear(c);
3174 if (!EP && nvar == 0) {
3175 Value f;
3176 value_init(f);
3177 Param_Vertices *V, *V2;
3178 Matrix* M = Matrix_Alloc(1, P->Dimension+2);
3180 FORALL_PVertex_in_ParamPolyhedron(V, last, PP) {
3181 bool found = false;
3182 FORALL_PVertex_in_ParamPolyhedron(V2, last, PP) {
3183 if (V == V2) {
3184 found = true;
3185 continue;
3187 if (!found)
3188 continue;
3189 for (int i = 0; i < exist; ++i) {
3190 value_oppose(f, V->Vertex->p[i][nparam+1]);
3191 Vector_Combine(V->Vertex->p[i],
3192 V2->Vertex->p[i],
3193 M->p[0] + 1 + nvar + exist,
3194 V2->Vertex->p[i][nparam+1],
3196 nparam+1);
3197 int j;
3198 for (j = 0; j < nparam; ++j)
3199 if (value_notzero_p(M->p[0][1+nvar+exist+j]))
3200 break;
3201 if (j >= nparam)
3202 continue;
3203 ConstraintSimplify(M->p[0], M->p[0],
3204 P->Dimension+2, &f);
3205 value_set_si(M->p[0][0], 0);
3206 Polyhedron *para = AddConstraints(M->p[0], 1, P,
3207 MaxRays);
3208 if (emptyQ(para)) {
3209 Polyhedron_Free(para);
3210 continue;
3212 Polyhedron *pos, *neg;
3213 value_set_si(M->p[0][0], 1);
3214 value_decrement(M->p[0][P->Dimension+1],
3215 M->p[0][P->Dimension+1]);
3216 neg = AddConstraints(M->p[0], 1, P, MaxRays);
3217 value_set_si(f, -1);
3218 Vector_Scale(M->p[0]+1, M->p[0]+1, f,
3219 P->Dimension+1);
3220 value_decrement(M->p[0][P->Dimension+1],
3221 M->p[0][P->Dimension+1]);
3222 value_decrement(M->p[0][P->Dimension+1],
3223 M->p[0][P->Dimension+1]);
3224 pos = AddConstraints(M->p[0], 1, P, MaxRays);
3225 if (emptyQ(neg) && emptyQ(pos)) {
3226 Polyhedron_Free(para);
3227 Polyhedron_Free(pos);
3228 Polyhedron_Free(neg);
3229 continue;
3231 #ifdef DEBUG_ER
3232 fprintf(stderr, "\nER: Order\n");
3233 #endif /* DEBUG_ER */
3234 EP = barvinok_enumerate_e(para, exist, nparam, MaxRays);
3235 evalue *E;
3236 if (!emptyQ(pos)) {
3237 E = barvinok_enumerate_e(pos, exist, nparam, MaxRays);
3238 eadd(E, EP);
3239 free_evalue_refs(E);
3240 free(E);
3242 if (!emptyQ(neg)) {
3243 E = barvinok_enumerate_e(neg, exist, nparam, MaxRays);
3244 eadd(E, EP);
3245 free_evalue_refs(E);
3246 free(E);
3248 Polyhedron_Free(para);
3249 Polyhedron_Free(pos);
3250 Polyhedron_Free(neg);
3251 break;
3253 if (EP)
3254 break;
3255 } END_FORALL_PVertex_in_ParamPolyhedron;
3256 if (EP)
3257 break;
3258 } END_FORALL_PVertex_in_ParamPolyhedron;
3260 if (!EP) {
3261 /* Search for vertex coordinate to split on */
3262 /* First look for one independent of the parameters */
3263 FORALL_PVertex_in_ParamPolyhedron(V, last, PP) {
3264 for (int i = 0; i < exist; ++i) {
3265 int j;
3266 for (j = 0; j < nparam; ++j)
3267 if (value_notzero_p(V->Vertex->p[i][j]))
3268 break;
3269 if (j < nparam)
3270 continue;
3271 value_set_si(M->p[0][0], 1);
3272 Vector_Set(M->p[0]+1, 0, nvar+exist);
3273 Vector_Copy(V->Vertex->p[i],
3274 M->p[0] + 1 + nvar + exist, nparam+1);
3275 value_oppose(M->p[0][1+nvar+i],
3276 V->Vertex->p[i][nparam+1]);
3278 Polyhedron *pos, *neg;
3279 value_set_si(M->p[0][0], 1);
3280 value_decrement(M->p[0][P->Dimension+1],
3281 M->p[0][P->Dimension+1]);
3282 neg = AddConstraints(M->p[0], 1, P, MaxRays);
3283 value_set_si(f, -1);
3284 Vector_Scale(M->p[0]+1, M->p[0]+1, f,
3285 P->Dimension+1);
3286 value_decrement(M->p[0][P->Dimension+1],
3287 M->p[0][P->Dimension+1]);
3288 value_decrement(M->p[0][P->Dimension+1],
3289 M->p[0][P->Dimension+1]);
3290 pos = AddConstraints(M->p[0], 1, P, MaxRays);
3291 if (emptyQ(neg) || emptyQ(pos)) {
3292 Polyhedron_Free(pos);
3293 Polyhedron_Free(neg);
3294 continue;
3296 Polyhedron_Free(pos);
3297 value_increment(M->p[0][P->Dimension+1],
3298 M->p[0][P->Dimension+1]);
3299 pos = AddConstraints(M->p[0], 1, P, MaxRays);
3300 #ifdef DEBUG_ER
3301 fprintf(stderr, "\nER: Vertex\n");
3302 #endif /* DEBUG_ER */
3303 pos->next = neg;
3304 EP = enumerate_or(pos, exist, nparam, MaxRays);
3305 break;
3307 if (EP)
3308 break;
3309 } END_FORALL_PVertex_in_ParamPolyhedron;
3312 if (!EP) {
3313 /* Search for vertex coordinate to split on */
3314 /* Now look for one that depends on the parameters */
3315 FORALL_PVertex_in_ParamPolyhedron(V, last, PP) {
3316 for (int i = 0; i < exist; ++i) {
3317 value_set_si(M->p[0][0], 1);
3318 Vector_Set(M->p[0]+1, 0, nvar+exist);
3319 Vector_Copy(V->Vertex->p[i],
3320 M->p[0] + 1 + nvar + exist, nparam+1);
3321 value_oppose(M->p[0][1+nvar+i],
3322 V->Vertex->p[i][nparam+1]);
3324 Polyhedron *pos, *neg;
3325 value_set_si(M->p[0][0], 1);
3326 value_decrement(M->p[0][P->Dimension+1],
3327 M->p[0][P->Dimension+1]);
3328 neg = AddConstraints(M->p[0], 1, P, MaxRays);
3329 value_set_si(f, -1);
3330 Vector_Scale(M->p[0]+1, M->p[0]+1, f,
3331 P->Dimension+1);
3332 value_decrement(M->p[0][P->Dimension+1],
3333 M->p[0][P->Dimension+1]);
3334 value_decrement(M->p[0][P->Dimension+1],
3335 M->p[0][P->Dimension+1]);
3336 pos = AddConstraints(M->p[0], 1, P, MaxRays);
3337 if (emptyQ(neg) || emptyQ(pos)) {
3338 Polyhedron_Free(pos);
3339 Polyhedron_Free(neg);
3340 continue;
3342 Polyhedron_Free(pos);
3343 value_increment(M->p[0][P->Dimension+1],
3344 M->p[0][P->Dimension+1]);
3345 pos = AddConstraints(M->p[0], 1, P, MaxRays);
3346 #ifdef DEBUG_ER
3347 fprintf(stderr, "\nER: ParamVertex\n");
3348 #endif /* DEBUG_ER */
3349 pos->next = neg;
3350 EP = enumerate_or(pos, exist, nparam, MaxRays);
3351 break;
3353 if (EP)
3354 break;
3355 } END_FORALL_PVertex_in_ParamPolyhedron;
3358 Matrix_Free(M);
3359 value_clear(f);
3362 if (CEq)
3363 Polyhedron_Free(CEq);
3364 if (CT)
3365 Matrix_Free(CT);
3366 if (PP)
3367 Param_Polyhedron_Free(PP);
3368 *PA = P;
3370 return EP;
3373 #ifndef HAVE_PIPLIB
3374 evalue *barvinok_enumerate_pip(Polyhedron *P,
3375 unsigned exist, unsigned nparam, unsigned MaxRays)
3377 return 0;
3379 #else
3380 evalue *barvinok_enumerate_pip(Polyhedron *P,
3381 unsigned exist, unsigned nparam, unsigned MaxRays)
3383 int nvar = P->Dimension - exist - nparam;
3384 evalue *EP = new_zero_ep();
3385 Polyhedron *Q, *N, *T = 0;
3386 Value min, tmp;
3387 value_init(min);
3388 value_init(tmp);
3390 #ifdef DEBUG_ER
3391 fprintf(stderr, "\nER: PIP\n");
3392 #endif /* DEBUG_ER */
3394 for (int i = 0; i < P->Dimension; ++i) {
3395 bool pos = false;
3396 bool neg = false;
3397 bool posray = false;
3398 bool negray = false;
3399 value_set_si(min, 0);
3400 for (int j = 0; j < P->NbRays; ++j) {
3401 if (value_pos_p(P->Ray[j][1+i])) {
3402 pos = true;
3403 if (value_zero_p(P->Ray[j][1+P->Dimension]))
3404 posray = true;
3405 } else if (value_neg_p(P->Ray[j][1+i])) {
3406 neg = true;
3407 if (value_zero_p(P->Ray[j][1+P->Dimension]))
3408 negray = true;
3409 else {
3410 mpz_fdiv_q(tmp,
3411 P->Ray[j][1+i], P->Ray[j][1+P->Dimension]);
3412 if (value_lt(tmp, min))
3413 value_assign(min, tmp);
3417 if (pos && neg) {
3418 assert(!(posray && negray));
3419 assert(!negray); // for now
3420 Polyhedron *O = T ? T : P;
3421 /* shift by a safe amount */
3422 Matrix *M = Matrix_Alloc(O->NbRays, O->Dimension+2);
3423 Vector_Copy(O->Ray[0], M->p[0], O->NbRays * (O->Dimension+2));
3424 for (int j = 0; j < P->NbRays; ++j) {
3425 if (value_notzero_p(M->p[j][1+P->Dimension])) {
3426 value_multiply(tmp, min, M->p[j][1+P->Dimension]);
3427 value_substract(M->p[j][1+i], M->p[j][1+i], tmp);
3430 if (T)
3431 Polyhedron_Free(T);
3432 T = Rays2Polyhedron(M, MaxRays);
3433 Matrix_Free(M);
3434 } else if (neg) {
3435 /* negating a parameter requires that we substitute in the
3436 * sign again afterwards.
3437 * Disallow for now.
3439 assert(i < nvar+exist);
3440 if (!T)
3441 T = Polyhedron_Copy(P);
3442 for (int j = 0; j < T->NbRays; ++j)
3443 value_oppose(T->Ray[j][1+i], T->Ray[j][1+i]);
3444 for (int j = 0; j < T->NbConstraints; ++j)
3445 value_oppose(T->Constraint[j][1+i], T->Constraint[j][1+i]);
3448 value_clear(min);
3449 value_clear(tmp);
3451 Polyhedron *D = pip_lexmin(T ? T : P, exist, nparam);
3452 for (Q = D; Q; Q = N) {
3453 N = Q->next;
3454 Q->next = 0;
3455 evalue *E;
3456 exist = Q->Dimension - nvar - nparam;
3457 E = barvinok_enumerate_e(Q, exist, nparam, MaxRays);
3458 Polyhedron_Free(Q);
3459 eadd(E, EP);
3460 free_evalue_refs(E);
3461 free(E);
3464 if (T)
3465 Polyhedron_Free(T);
3467 return EP;
3469 #endif
3472 static bool is_single(Value *row, int pos, int len)
3474 return First_Non_Zero(row, pos) == -1 &&
3475 First_Non_Zero(row+pos+1, len-pos-1) == -1;
3478 static evalue* barvinok_enumerate_e_r(Polyhedron *P,
3479 unsigned exist, unsigned nparam, unsigned MaxRays);
3481 #ifdef DEBUG_ER
3482 static int er_level = 0;
3484 evalue* barvinok_enumerate_e(Polyhedron *P,
3485 unsigned exist, unsigned nparam, unsigned MaxRays)
3487 fprintf(stderr, "\nER: level %i\n", er_level);
3488 int nvar = P->Dimension - exist - nparam;
3489 fprintf(stderr, "%d %d %d\n", nvar, exist, nparam);
3491 Polyhedron_Print(stderr, P_VALUE_FMT, P);
3492 ++er_level;
3493 P = DomainConstraintSimplify(Polyhedron_Copy(P), MaxRays);
3494 evalue *EP = barvinok_enumerate_e_r(P, exist, nparam, MaxRays);
3495 Polyhedron_Free(P);
3496 --er_level;
3497 return EP;
3499 #else
3500 evalue* barvinok_enumerate_e(Polyhedron *P,
3501 unsigned exist, unsigned nparam, unsigned MaxRays)
3503 P = DomainConstraintSimplify(Polyhedron_Copy(P), MaxRays);
3504 evalue *EP = barvinok_enumerate_e_r(P, exist, nparam, MaxRays);
3505 Polyhedron_Free(P);
3506 return EP;
3508 #endif
3510 static evalue* barvinok_enumerate_e_r(Polyhedron *P,
3511 unsigned exist, unsigned nparam, unsigned MaxRays)
3513 if (exist == 0) {
3514 Polyhedron *U = Universe_Polyhedron(nparam);
3515 evalue *EP = barvinok_enumerate_ev(P, U, MaxRays);
3516 //char *param_name[] = {"P", "Q", "R", "S", "T" };
3517 //print_evalue(stdout, EP, param_name);
3518 Polyhedron_Free(U);
3519 return EP;
3522 int nvar = P->Dimension - exist - nparam;
3523 int len = P->Dimension + 2;
3525 if (emptyQ(P))
3526 return new_zero_ep();
3528 if (nvar == 0 && nparam == 0) {
3529 evalue *EP = new_zero_ep();
3530 barvinok_count(P, &EP->x.n, MaxRays);
3531 if (value_pos_p(EP->x.n))
3532 value_set_si(EP->x.n, 1);
3533 return EP;
3536 int r;
3537 for (r = 0; r < P->NbRays; ++r)
3538 if (value_zero_p(P->Ray[r][0]) ||
3539 value_zero_p(P->Ray[r][P->Dimension+1])) {
3540 int i;
3541 for (i = 0; i < nvar; ++i)
3542 if (value_notzero_p(P->Ray[r][i+1]))
3543 break;
3544 if (i >= nvar)
3545 continue;
3546 for (i = nvar + exist; i < nvar + exist + nparam; ++i)
3547 if (value_notzero_p(P->Ray[r][i+1]))
3548 break;
3549 if (i >= nvar + exist + nparam)
3550 break;
3552 if (r < P->NbRays) {
3553 evalue *EP = new_zero_ep();
3554 value_set_si(EP->x.n, -1);
3555 return EP;
3558 int first;
3559 for (r = 0; r < P->NbEq; ++r)
3560 if ((first = First_Non_Zero(P->Constraint[r]+1+nvar, exist)) != -1)
3561 break;
3562 if (r < P->NbEq) {
3563 if (First_Non_Zero(P->Constraint[r]+1+nvar+first+1,
3564 exist-first-1) != -1) {
3565 Polyhedron *T = rotate_along(P, r, nvar, exist, MaxRays);
3566 #ifdef DEBUG_ER
3567 fprintf(stderr, "\nER: Equality\n");
3568 #endif /* DEBUG_ER */
3569 evalue *EP = barvinok_enumerate_e(T, exist-1, nparam, MaxRays);
3570 Polyhedron_Free(T);
3571 return EP;
3572 } else {
3573 #ifdef DEBUG_ER
3574 fprintf(stderr, "\nER: Fixed\n");
3575 #endif /* DEBUG_ER */
3576 if (first == 0)
3577 return barvinok_enumerate_e(P, exist-1, nparam, MaxRays);
3578 else {
3579 Polyhedron *T = Polyhedron_Copy(P);
3580 SwapColumns(T, nvar+1, nvar+1+first);
3581 evalue *EP = barvinok_enumerate_e(T, exist-1, nparam, MaxRays);
3582 Polyhedron_Free(T);
3583 return EP;
3588 Vector *row = Vector_Alloc(len);
3589 value_set_si(row->p[0], 1);
3591 Value f;
3592 value_init(f);
3594 enum constraint* info = new constraint[exist];
3595 for (int i = 0; i < exist; ++i) {
3596 info[i] = ALL_POS;
3597 for (int l = P->NbEq; l < P->NbConstraints; ++l) {
3598 if (value_negz_p(P->Constraint[l][nvar+i+1]))
3599 continue;
3600 bool l_parallel = is_single(P->Constraint[l]+nvar+1, i, exist);
3601 for (int u = P->NbEq; u < P->NbConstraints; ++u) {
3602 if (value_posz_p(P->Constraint[u][nvar+i+1]))
3603 continue;
3604 bool lu_parallel = l_parallel ||
3605 is_single(P->Constraint[u]+nvar+1, i, exist);
3606 value_oppose(f, P->Constraint[u][nvar+i+1]);
3607 Vector_Combine(P->Constraint[l]+1, P->Constraint[u]+1, row->p+1,
3608 f, P->Constraint[l][nvar+i+1], len-1);
3609 if (!(info[i] & INDEPENDENT)) {
3610 int j;
3611 for (j = 0; j < exist; ++j)
3612 if (j != i && value_notzero_p(row->p[nvar+j+1]))
3613 break;
3614 if (j == exist) {
3615 //printf("independent: i: %d, l: %d, u: %d\n", i, l, u);
3616 info[i] = (constraint)(info[i] | INDEPENDENT);
3619 if (info[i] & ALL_POS) {
3620 value_addto(row->p[len-1], row->p[len-1],
3621 P->Constraint[l][nvar+i+1]);
3622 value_addto(row->p[len-1], row->p[len-1], f);
3623 value_multiply(f, f, P->Constraint[l][nvar+i+1]);
3624 value_substract(row->p[len-1], row->p[len-1], f);
3625 value_decrement(row->p[len-1], row->p[len-1]);
3626 ConstraintSimplify(row->p, row->p, len, &f);
3627 value_set_si(f, -1);
3628 Vector_Scale(row->p+1, row->p+1, f, len-1);
3629 value_decrement(row->p[len-1], row->p[len-1]);
3630 Polyhedron *T = AddConstraints(row->p, 1, P, MaxRays);
3631 if (!emptyQ(T)) {
3632 //printf("not all_pos: i: %d, l: %d, u: %d\n", i, l, u);
3633 info[i] = (constraint)(info[i] ^ ALL_POS);
3635 //puts("pos remainder");
3636 //Polyhedron_Print(stdout, P_VALUE_FMT, T);
3637 Polyhedron_Free(T);
3639 if (!(info[i] & ONE_NEG)) {
3640 if (lu_parallel) {
3641 negative_test_constraint(P->Constraint[l],
3642 P->Constraint[u],
3643 row->p, nvar+i, len, &f);
3644 oppose_constraint(row->p, len, &f);
3645 Polyhedron *T = AddConstraints(row->p, 1, P, MaxRays);
3646 if (emptyQ(T)) {
3647 //printf("one_neg i: %d, l: %d, u: %d\n", i, l, u);
3648 info[i] = (constraint)(info[i] | ONE_NEG);
3650 //puts("neg remainder");
3651 //Polyhedron_Print(stdout, P_VALUE_FMT, T);
3652 Polyhedron_Free(T);
3653 } else if (!(info[i] & ROT_NEG)) {
3654 if (parallel_constraints(P->Constraint[l],
3655 P->Constraint[u],
3656 row->p, nvar, exist)) {
3657 negative_test_constraint7(P->Constraint[l],
3658 P->Constraint[u],
3659 row->p, nvar, exist,
3660 len, &f);
3661 oppose_constraint(row->p, len, &f);
3662 Polyhedron *T = AddConstraints(row->p, 1, P, MaxRays);
3663 if (emptyQ(T)) {
3664 // printf("rot_neg i: %d, l: %d, u: %d\n", i, l, u);
3665 info[i] = (constraint)(info[i] | ROT_NEG);
3666 r = l;
3668 //puts("neg remainder");
3669 //Polyhedron_Print(stdout, P_VALUE_FMT, T);
3670 Polyhedron_Free(T);
3674 if (!(info[i] & ALL_POS) && (info[i] & (ONE_NEG | ROT_NEG)))
3675 goto next;
3678 if (info[i] & ALL_POS)
3679 break;
3680 next:
3685 for (int i = 0; i < exist; ++i)
3686 printf("%i: %i\n", i, info[i]);
3688 for (int i = 0; i < exist; ++i)
3689 if (info[i] & ALL_POS) {
3690 #ifdef DEBUG_ER
3691 fprintf(stderr, "\nER: Positive\n");
3692 #endif /* DEBUG_ER */
3693 // Eliminate
3694 // Maybe we should chew off some of the fat here
3695 Matrix *M = Matrix_Alloc(P->Dimension, P->Dimension+1);
3696 for (int j = 0; j < P->Dimension; ++j)
3697 value_set_si(M->p[j][j + (j >= i+nvar)], 1);
3698 Polyhedron *T = Polyhedron_Image(P, M, MaxRays);
3699 Matrix_Free(M);
3700 evalue *EP = barvinok_enumerate_e(T, exist-1, nparam, MaxRays);
3701 Polyhedron_Free(T);
3702 value_clear(f);
3703 Vector_Free(row);
3704 delete [] info;
3705 return EP;
3707 for (int i = 0; i < exist; ++i)
3708 if (info[i] & ONE_NEG) {
3709 #ifdef DEBUG_ER
3710 fprintf(stderr, "\nER: Negative\n");
3711 #endif /* DEBUG_ER */
3712 Vector_Free(row);
3713 value_clear(f);
3714 delete [] info;
3715 if (i == 0)
3716 return barvinok_enumerate_e(P, exist-1, nparam, MaxRays);
3717 else {
3718 Polyhedron *T = Polyhedron_Copy(P);
3719 SwapColumns(T, nvar+1, nvar+1+i);
3720 evalue *EP = barvinok_enumerate_e(T, exist-1, nparam, MaxRays);
3721 Polyhedron_Free(T);
3722 return EP;
3725 for (int i = 0; i < exist; ++i)
3726 if (info[i] & ROT_NEG) {
3727 #ifdef DEBUG_ER
3728 fprintf(stderr, "\nER: Rotate\n");
3729 #endif /* DEBUG_ER */
3730 Vector_Free(row);
3731 value_clear(f);
3732 delete [] info;
3733 Polyhedron *T = rotate_along(P, r, nvar, exist, MaxRays);
3734 evalue *EP = barvinok_enumerate_e(T, exist-1, nparam, MaxRays);
3735 Polyhedron_Free(T);
3736 return EP;
3738 for (int i = 0; i < exist; ++i)
3739 if (info[i] & INDEPENDENT) {
3740 Polyhedron *pos, *neg;
3742 /* Find constraint again and split off negative part */
3744 if (SplitOnVar(P, i, nvar, len, exist, MaxRays,
3745 row, f, true, &pos, &neg)) {
3746 #ifdef DEBUG_ER
3747 fprintf(stderr, "\nER: Split\n");
3748 #endif /* DEBUG_ER */
3750 evalue *EP =
3751 barvinok_enumerate_e(neg, exist-1, nparam, MaxRays);
3752 evalue *E =
3753 barvinok_enumerate_e(pos, exist, nparam, MaxRays);
3754 eadd(E, EP);
3755 free_evalue_refs(E);
3756 free(E);
3757 Polyhedron_Free(neg);
3758 Polyhedron_Free(pos);
3759 value_clear(f);
3760 Vector_Free(row);
3761 delete [] info;
3762 return EP;
3765 delete [] info;
3767 Polyhedron *O = P;
3768 Polyhedron *F;
3770 evalue *EP;
3772 EP = enumerate_line(P, exist, nparam, MaxRays);
3773 if (EP)
3774 goto out;
3776 EP = barvinok_enumerate_pip(P, exist, nparam, MaxRays);
3777 if (EP)
3778 goto out;
3780 EP = enumerate_redundant_ray(P, exist, nparam, MaxRays);
3781 if (EP)
3782 goto out;
3784 EP = enumerate_sure(P, exist, nparam, MaxRays);
3785 if (EP)
3786 goto out;
3788 EP = enumerate_ray(P, exist, nparam, MaxRays);
3789 if (EP)
3790 goto out;
3792 EP = enumerate_sure2(P, exist, nparam, MaxRays);
3793 if (EP)
3794 goto out;
3796 F = unfringe(P, MaxRays);
3797 if (!PolyhedronIncludes(F, P)) {
3798 #ifdef DEBUG_ER
3799 fprintf(stderr, "\nER: Fringed\n");
3800 #endif /* DEBUG_ER */
3801 EP = barvinok_enumerate_e(F, exist, nparam, MaxRays);
3802 Polyhedron_Free(F);
3803 goto out;
3805 Polyhedron_Free(F);
3807 if (nparam)
3808 EP = enumerate_vd(&P, exist, nparam, MaxRays);
3809 if (EP)
3810 goto out2;
3812 if (nvar != 0) {
3813 EP = enumerate_sum(P, exist, nparam, MaxRays);
3814 goto out2;
3817 assert(nvar == 0);
3819 int i;
3820 Polyhedron *pos, *neg;
3821 for (i = 0; i < exist; ++i)
3822 if (SplitOnVar(P, i, nvar, len, exist, MaxRays,
3823 row, f, false, &pos, &neg))
3824 break;
3826 assert (i < exist);
3828 pos->next = neg;
3829 EP = enumerate_or(pos, exist, nparam, MaxRays);
3831 out2:
3832 if (O != P)
3833 Polyhedron_Free(P);
3835 out:
3836 value_clear(f);
3837 Vector_Free(row);
3838 return EP;
3841 gen_fun * barvinok_series(Polyhedron *P, Polyhedron* C, unsigned MaxRays)
3843 Polyhedron ** vcone;
3844 Polyhedron *CA;
3845 unsigned nparam = C->Dimension;
3846 unsigned dim, nvar;
3847 vec_ZZ sign;
3848 int ncone = 0;
3849 sign.SetLength(ncone);
3851 CA = align_context(C, P->Dimension, MaxRays);
3852 P = DomainIntersection(P, CA, MaxRays);
3853 Polyhedron_Free(CA);
3855 assert(!Polyhedron_is_infinite(P, nparam));
3856 assert(P->NbBid == 0);
3857 assert(Polyhedron_has_positive_rays(P, nparam));
3858 assert(P->NbEq == 0);
3860 partial_reducer red(P, nparam);
3861 red.start(MaxRays);
3862 return red.gf;