some code reordering
[barvinok.git] / barvinok.cc
bloba2d496937165c39e1242d4593e7ec7204a993ab7
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 div(dpoly& d, ZZ& sign, gen_fun *gf, mat_ZZ& pden, mat_ZZ& den,
494 vec_ZZ& num_p) {
495 dpoly_r * rc = div(d);
496 //rc.dump();
497 int common = pden.NumRows();
499 vector< dpoly_r_term * >& final = rc->c[len-1];
500 int rows;
501 for (int j = 0; j < final.size(); ++j) {
502 rows = common;
503 pden.SetDims(rows, pden.NumCols());
504 for (int k = 0; k < dim; ++k) {
505 int n = final[j]->powers[k];
506 if (n == 0)
507 continue;
508 int abs_n = n < 0 ? -n : n;
509 pden.SetDims(rows+abs_n, pden.NumCols());
510 for (int l = 0; l < abs_n; ++l) {
511 if (n > 0)
512 pden[rows+l] = den[k];
513 else
514 pden[rows+l] = -den[k];
516 rows += abs_n;
518 final[j]->coeff *= sign;
519 gf->add(final[j]->coeff, rc->denom, num_p, pden);
521 delete rc;
523 void dump(void) {
524 for (int i = 0; i < len; ++i) {
525 cout << endl;
526 cout << i << endl;
527 cout << c[i].size() << endl;
528 for (int j = 0; j < c[i].size(); ++j) {
529 for (int k = 0; k < dim; ++k) {
530 cout << c[i][j]->powers[k] << " ";
532 cout << ": " << c[i][j]->coeff << "/" << denom << endl;
534 cout << endl;
539 struct decomposer {
540 void decompose(Polyhedron *C);
541 virtual void handle(Polyhedron *P, int sign) = 0;
544 struct polar_decomposer : public decomposer {
545 void decompose(Polyhedron *C, unsigned MaxRays);
546 virtual void handle(Polyhedron *P, int sign);
547 virtual void handle_polar(Polyhedron *P, int sign) = 0;
550 void decomposer::decompose(Polyhedron *C)
552 vector<cone *> nonuni;
553 cone * c = new cone(C);
554 ZZ det = c->det;
555 int s = sign(det);
556 assert(det != 0);
557 if (abs(det) > 1) {
558 nonuni.push_back(c);
559 } else {
560 handle(C, 1);
561 delete c;
563 vec_ZZ lambda;
564 while (!nonuni.empty()) {
565 c = nonuni.back();
566 nonuni.pop_back();
567 Vector* v = c->short_vector(lambda);
568 for (int i = 0; i < c->Rays->NbRows - 1; ++i) {
569 if (lambda[i] == 0)
570 continue;
571 Matrix* M = Matrix_Copy(c->Rays);
572 Vector_Copy(v->p, M->p[i], v->Size);
573 cone * pc = new cone(M);
574 assert (pc->det != 0);
575 if (abs(pc->det) > 1) {
576 assert(abs(pc->det) < abs(c->det));
577 nonuni.push_back(pc);
578 } else {
579 handle(pc->poly(), sign(pc->det) * s);
580 delete pc;
582 Matrix_Free(M);
584 Vector_Free(v);
585 delete c;
589 void polar_decomposer::decompose(Polyhedron *cone, unsigned MaxRays)
591 Polyhedron_Polarize(cone);
592 if (cone->NbRays - 1 != cone->Dimension) {
593 Polyhedron *tmp = cone;
594 cone = triangularize_cone(cone, MaxRays);
595 Polyhedron_Free(tmp);
597 for (Polyhedron *Polar = cone; Polar; Polar = Polar->next)
598 decomposer::decompose(Polar);
599 Domain_Free(cone);
602 void polar_decomposer::handle(Polyhedron *P, int sign)
604 Polyhedron_Polarize(P);
605 handle_polar(P, sign);
609 * Barvinok's Decomposition of a simplicial cone
611 * Returns two lists of polyhedra
613 void barvinok_decompose(Polyhedron *C, Polyhedron **ppos, Polyhedron **pneg)
615 Polyhedron *pos = *ppos, *neg = *pneg;
616 vector<cone *> nonuni;
617 cone * c = new cone(C);
618 ZZ det = c->det;
619 int s = sign(det);
620 assert(det != 0);
621 if (abs(det) > 1) {
622 nonuni.push_back(c);
623 } else {
624 Polyhedron *p = Polyhedron_Copy(c->Cone);
625 p->next = pos;
626 pos = p;
627 delete c;
629 vec_ZZ lambda;
630 while (!nonuni.empty()) {
631 c = nonuni.back();
632 nonuni.pop_back();
633 Vector* v = c->short_vector(lambda);
634 for (int i = 0; i < c->Rays->NbRows - 1; ++i) {
635 if (lambda[i] == 0)
636 continue;
637 Matrix* M = Matrix_Copy(c->Rays);
638 Vector_Copy(v->p, M->p[i], v->Size);
639 cone * pc = new cone(M);
640 assert (pc->det != 0);
641 if (abs(pc->det) > 1) {
642 assert(abs(pc->det) < abs(c->det));
643 nonuni.push_back(pc);
644 } else {
645 Polyhedron *p = pc->poly();
646 pc->Cone = 0;
647 if (sign(pc->det) == s) {
648 p->next = pos;
649 pos = p;
650 } else {
651 p->next = neg;
652 neg = p;
654 delete pc;
656 Matrix_Free(M);
658 Vector_Free(v);
659 delete c;
661 *ppos = pos;
662 *pneg = neg;
666 * Returns a single list of npos "positive" cones followed by nneg
667 * "negative" cones.
668 * The input cone is freed
670 void decompose(Polyhedron *cone, Polyhedron **parts, int *npos, int *nneg, unsigned MaxRays)
672 Polyhedron_Polarize(cone);
673 if (cone->NbRays - 1 != cone->Dimension) {
674 Polyhedron *tmp = cone;
675 cone = triangularize_cone(cone, MaxRays);
676 Polyhedron_Free(tmp);
678 Polyhedron *polpos = NULL, *polneg = NULL;
679 *npos = 0; *nneg = 0;
680 for (Polyhedron *Polar = cone; Polar; Polar = Polar->next)
681 barvinok_decompose(Polar, &polpos, &polneg);
683 Polyhedron *last;
684 for (Polyhedron *i = polpos; i; i = i->next) {
685 Polyhedron_Polarize(i);
686 ++*npos;
687 last = i;
689 for (Polyhedron *i = polneg; i; i = i->next) {
690 Polyhedron_Polarize(i);
691 ++*nneg;
693 if (last) {
694 last->next = polneg;
695 *parts = polpos;
696 } else
697 *parts = polneg;
698 Domain_Free(cone);
701 const int MAX_TRY=10;
703 * Searches for a vector that is not orthogonal to any
704 * of the rays in rays.
706 static void nonorthog(mat_ZZ& rays, vec_ZZ& lambda)
708 int dim = rays.NumCols();
709 bool found = false;
710 lambda.SetLength(dim);
711 if (dim == 0)
712 return;
714 for (int i = 2; !found && i <= 50*dim; i+=4) {
715 for (int j = 0; j < MAX_TRY; ++j) {
716 for (int k = 0; k < dim; ++k) {
717 int r = random_int(i)+2;
718 int v = (2*(r%2)-1) * (r >> 1);
719 lambda[k] = v;
721 int k = 0;
722 for (; k < rays.NumRows(); ++k)
723 if (lambda * rays[k] == 0)
724 break;
725 if (k == rays.NumRows()) {
726 found = true;
727 break;
731 assert(found);
734 static void randomvector(Polyhedron *P, vec_ZZ& lambda, int nvar)
736 Value tmp;
737 int max = 10 * 16;
738 unsigned int dim = P->Dimension;
739 value_init(tmp);
741 for (int i = 0; i < P->NbRays; ++i) {
742 for (int j = 1; j <= dim; ++j) {
743 value_absolute(tmp, P->Ray[i][j]);
744 int t = VALUE_TO_LONG(tmp) * 16;
745 if (t > max)
746 max = t;
749 for (int i = 0; i < P->NbConstraints; ++i) {
750 for (int j = 1; j <= dim; ++j) {
751 value_absolute(tmp, P->Constraint[i][j]);
752 int t = VALUE_TO_LONG(tmp) * 16;
753 if (t > max)
754 max = t;
757 value_clear(tmp);
759 lambda.SetLength(nvar);
760 for (int k = 0; k < nvar; ++k) {
761 int r = random_int(max*dim)+2;
762 int v = (2*(r%2)-1) * (max/2*dim + (r >> 1));
763 lambda[k] = v;
767 static void add_rays(mat_ZZ& rays, Polyhedron *i, int *r, int nvar = -1,
768 bool all = false)
770 unsigned dim = i->Dimension;
771 if (nvar == -1)
772 nvar = dim;
773 for (int k = 0; k < i->NbRays; ++k) {
774 if (!value_zero_p(i->Ray[k][dim+1]))
775 continue;
776 if (!all && nvar != dim && First_Non_Zero(i->Ray[k]+1, nvar) == -1)
777 continue;
778 values2zz(i->Ray[k]+1, rays[(*r)++], nvar);
782 void lattice_point(Value* values, Polyhedron *i, vec_ZZ& vertex)
784 unsigned dim = i->Dimension;
785 if(!value_one_p(values[dim])) {
786 Matrix* Rays = rays(i);
787 Matrix *inv = Matrix_Alloc(Rays->NbRows, Rays->NbColumns);
788 int ok = Matrix_Inverse(Rays, inv);
789 assert(ok);
790 Matrix_Free(Rays);
791 Rays = rays(i);
792 Vector *lambda = Vector_Alloc(dim+1);
793 Vector_Matrix_Product(values, inv, lambda->p);
794 Matrix_Free(inv);
795 for (int j = 0; j < dim; ++j)
796 mpz_cdiv_q(lambda->p[j], lambda->p[j], lambda->p[dim]);
797 value_set_si(lambda->p[dim], 1);
798 Vector *A = Vector_Alloc(dim+1);
799 Vector_Matrix_Product(lambda->p, Rays, A->p);
800 Vector_Free(lambda);
801 Matrix_Free(Rays);
802 values2zz(A->p, vertex, dim);
803 Vector_Free(A);
804 } else
805 values2zz(values, vertex, dim);
808 static evalue *term(int param, ZZ& c, Value *den = NULL)
810 evalue *EP = new evalue();
811 value_init(EP->d);
812 value_set_si(EP->d,0);
813 EP->x.p = new_enode(polynomial, 2, param + 1);
814 evalue_set_si(&EP->x.p->arr[0], 0, 1);
815 value_init(EP->x.p->arr[1].x.n);
816 if (den == NULL)
817 value_set_si(EP->x.p->arr[1].d, 1);
818 else
819 value_assign(EP->x.p->arr[1].d, *den);
820 zz2value(c, EP->x.p->arr[1].x.n);
821 return EP;
824 static void vertex_period(
825 Polyhedron *i, vec_ZZ& lambda, Matrix *T,
826 Value lcm, int p, Vector *val,
827 evalue *E, evalue* ev,
828 ZZ& offset)
830 unsigned nparam = T->NbRows - 1;
831 unsigned dim = i->Dimension;
832 Value tmp;
833 ZZ nump;
835 if (p == nparam) {
836 vec_ZZ vertex;
837 ZZ num, l;
838 Vector * values = Vector_Alloc(dim + 1);
839 Vector_Matrix_Product(val->p, T, values->p);
840 value_assign(values->p[dim], lcm);
841 lattice_point(values->p, i, vertex);
842 num = vertex * lambda;
843 value2zz(lcm, l);
844 num *= l;
845 num += offset;
846 value_init(ev->x.n);
847 zz2value(num, ev->x.n);
848 value_assign(ev->d, lcm);
849 Vector_Free(values);
850 return;
853 value_init(tmp);
854 vec_ZZ vertex;
855 values2zz(T->p[p], vertex, dim);
856 nump = vertex * lambda;
857 if (First_Non_Zero(val->p, p) == -1) {
858 value_assign(tmp, lcm);
859 evalue *ET = term(p, nump, &tmp);
860 eadd(ET, E);
861 free_evalue_refs(ET);
862 delete ET;
865 value_assign(tmp, lcm);
866 if (First_Non_Zero(T->p[p], dim) != -1)
867 Vector_Gcd(T->p[p], dim, &tmp);
868 Gcd(tmp, lcm, &tmp);
869 if (value_lt(tmp, lcm)) {
870 ZZ count;
872 value_division(tmp, lcm, tmp);
873 value_set_si(ev->d, 0);
874 ev->x.p = new_enode(periodic, VALUE_TO_INT(tmp), p+1);
875 value2zz(tmp, count);
876 do {
877 value_decrement(tmp, tmp);
878 --count;
879 ZZ new_offset = offset - count * nump;
880 value_assign(val->p[p], tmp);
881 vertex_period(i, lambda, T, lcm, p+1, val, E,
882 &ev->x.p->arr[VALUE_TO_INT(tmp)], new_offset);
883 } while (value_pos_p(tmp));
884 } else
885 vertex_period(i, lambda, T, lcm, p+1, val, E, ev, offset);
886 value_clear(tmp);
889 static void mask_r(Matrix *f, int nr, Vector *lcm, int p, Vector *val, evalue *ev)
891 unsigned nparam = lcm->Size;
893 if (p == nparam) {
894 Vector * prod = Vector_Alloc(f->NbRows);
895 Matrix_Vector_Product(f, val->p, prod->p);
896 int isint = 1;
897 for (int i = 0; i < nr; ++i) {
898 value_modulus(prod->p[i], prod->p[i], f->p[i][nparam+1]);
899 isint &= value_zero_p(prod->p[i]);
901 value_set_si(ev->d, 1);
902 value_init(ev->x.n);
903 value_set_si(ev->x.n, isint);
904 Vector_Free(prod);
905 return;
908 Value tmp;
909 value_init(tmp);
910 if (value_one_p(lcm->p[p]))
911 mask_r(f, nr, lcm, p+1, val, ev);
912 else {
913 value_assign(tmp, lcm->p[p]);
914 value_set_si(ev->d, 0);
915 ev->x.p = new_enode(periodic, VALUE_TO_INT(tmp), p+1);
916 do {
917 value_decrement(tmp, tmp);
918 value_assign(val->p[p], tmp);
919 mask_r(f, nr, lcm, p+1, val, &ev->x.p->arr[VALUE_TO_INT(tmp)]);
920 } while (value_pos_p(tmp));
922 value_clear(tmp);
925 static evalue *multi_monom(vec_ZZ& p)
927 evalue *X = new evalue();
928 value_init(X->d);
929 value_init(X->x.n);
930 unsigned nparam = p.length()-1;
931 zz2value(p[nparam], X->x.n);
932 value_set_si(X->d, 1);
933 for (int i = 0; i < nparam; ++i) {
934 if (p[i] == 0)
935 continue;
936 evalue *T = term(i, p[i]);
937 eadd(T, X);
938 free_evalue_refs(T);
939 delete T;
941 return X;
945 * Check whether mapping polyhedron P on the affine combination
946 * num yields a range that has a fixed quotient on integer
947 * division by d
948 * If zero is true, then we are only interested in the quotient
949 * for the cases where the remainder is zero.
950 * Returns NULL if false and a newly allocated value if true.
952 static Value *fixed_quotient(Polyhedron *P, vec_ZZ& num, Value d, bool zero)
954 Value* ret = NULL;
955 int len = num.length();
956 Matrix *T = Matrix_Alloc(2, len);
957 zz2values(num, T->p[0]);
958 value_set_si(T->p[1][len-1], 1);
959 Polyhedron *I = Polyhedron_Image(P, T, P->NbConstraints);
960 Matrix_Free(T);
962 int i;
963 for (i = 0; i < I->NbRays; ++i)
964 if (value_zero_p(I->Ray[i][2])) {
965 Polyhedron_Free(I);
966 return NULL;
969 Value min, max;
970 value_init(min);
971 value_init(max);
972 int bounded = line_minmax(I, &min, &max);
973 assert(bounded);
975 if (zero)
976 mpz_cdiv_q(min, min, d);
977 else
978 mpz_fdiv_q(min, min, d);
979 mpz_fdiv_q(max, max, d);
981 if (value_eq(min, max)) {
982 ALLOC(Value, ret);
983 value_init(*ret);
984 value_assign(*ret, min);
986 value_clear(min);
987 value_clear(max);
988 return ret;
992 * Normalize linear expression coef modulo m
993 * Removes common factor and reduces coefficients
994 * Returns index of first non-zero coefficient or len
996 static int normal_mod(Value *coef, int len, Value *m)
998 Value gcd;
999 value_init(gcd);
1001 Vector_Gcd(coef, len, &gcd);
1002 Gcd(gcd, *m, &gcd);
1003 Vector_AntiScale(coef, coef, gcd, len);
1005 value_division(*m, *m, gcd);
1006 value_clear(gcd);
1008 if (value_one_p(*m))
1009 return len;
1011 int j;
1012 for (j = 0; j < len; ++j)
1013 mpz_fdiv_r(coef[j], coef[j], *m);
1014 for (j = 0; j < len; ++j)
1015 if (value_notzero_p(coef[j]))
1016 break;
1018 return j;
1021 #ifdef USE_MODULO
1022 static void mask(Matrix *f, evalue *factor)
1024 int nr = f->NbRows, nc = f->NbColumns;
1025 int n;
1026 bool found = false;
1027 for (n = 0; n < nr && value_notzero_p(f->p[n][nc-1]); ++n)
1028 if (value_notone_p(f->p[n][nc-1]) &&
1029 value_notmone_p(f->p[n][nc-1]))
1030 found = true;
1031 if (!found)
1032 return;
1034 evalue EP;
1035 nr = n;
1037 Value m;
1038 value_init(m);
1040 evalue EV;
1041 value_init(EV.d);
1042 value_init(EV.x.n);
1043 value_set_si(EV.x.n, 1);
1045 for (n = 0; n < nr; ++n) {
1046 value_assign(m, f->p[n][nc-1]);
1047 if (value_one_p(m) || value_mone_p(m))
1048 continue;
1050 int j = normal_mod(f->p[n], nc-1, &m);
1051 if (j == nc-1) {
1052 free_evalue_refs(factor);
1053 value_init(factor->d);
1054 evalue_set_si(factor, 0, 1);
1055 break;
1057 vec_ZZ row;
1058 values2zz(f->p[n], row, nc-1);
1059 ZZ g;
1060 value2zz(m, g);
1061 if (j < (nc-1)-1 && row[j] > g/2) {
1062 for (int k = j; k < (nc-1); ++k)
1063 if (row[k] != 0)
1064 row[k] = g - row[k];
1067 value_init(EP.d);
1068 value_set_si(EP.d, 0);
1069 EP.x.p = new_enode(relation, 2, 0);
1070 value_clear(EP.x.p->arr[1].d);
1071 EP.x.p->arr[1] = *factor;
1072 evalue *ev = &EP.x.p->arr[0];
1073 value_set_si(ev->d, 0);
1074 ev->x.p = new_enode(fractional, 3, -1);
1075 evalue_set_si(&ev->x.p->arr[1], 0, 1);
1076 evalue_set_si(&ev->x.p->arr[2], 1, 1);
1077 evalue *E = multi_monom(row);
1078 value_assign(EV.d, m);
1079 emul(&EV, E);
1080 value_clear(ev->x.p->arr[0].d);
1081 ev->x.p->arr[0] = *E;
1082 delete E;
1083 *factor = EP;
1086 value_clear(m);
1087 free_evalue_refs(&EV);
1089 #else
1093 static void mask(Matrix *f, evalue *factor)
1095 int nr = f->NbRows, nc = f->NbColumns;
1096 int n;
1097 bool found = false;
1098 for (n = 0; n < nr && value_notzero_p(f->p[n][nc-1]); ++n)
1099 if (value_notone_p(f->p[n][nc-1]) &&
1100 value_notmone_p(f->p[n][nc-1]))
1101 found = true;
1102 if (!found)
1103 return;
1105 Value tmp;
1106 value_init(tmp);
1107 nr = n;
1108 unsigned np = nc - 2;
1109 Vector *lcm = Vector_Alloc(np);
1110 Vector *val = Vector_Alloc(nc);
1111 Vector_Set(val->p, 0, nc);
1112 value_set_si(val->p[np], 1);
1113 Vector_Set(lcm->p, 1, np);
1114 for (n = 0; n < nr; ++n) {
1115 if (value_one_p(f->p[n][nc-1]) ||
1116 value_mone_p(f->p[n][nc-1]))
1117 continue;
1118 for (int j = 0; j < np; ++j)
1119 if (value_notzero_p(f->p[n][j])) {
1120 Gcd(f->p[n][j], f->p[n][nc-1], &tmp);
1121 value_division(tmp, f->p[n][nc-1], tmp);
1122 value_lcm(tmp, lcm->p[j], &lcm->p[j]);
1125 evalue EP;
1126 value_init(EP.d);
1127 mask_r(f, nr, lcm, 0, val, &EP);
1128 value_clear(tmp);
1129 Vector_Free(val);
1130 Vector_Free(lcm);
1131 emul(&EP,factor);
1132 free_evalue_refs(&EP);
1134 #endif
1136 struct term_info {
1137 evalue *E;
1138 ZZ constant;
1139 ZZ coeff;
1140 int pos;
1143 static bool mod_needed(Polyhedron *PD, vec_ZZ& num, Value d, evalue *E)
1145 Value *q = fixed_quotient(PD, num, d, false);
1147 if (!q)
1148 return true;
1150 value_oppose(*q, *q);
1151 evalue EV;
1152 value_init(EV.d);
1153 value_set_si(EV.d, 1);
1154 value_init(EV.x.n);
1155 value_multiply(EV.x.n, *q, d);
1156 eadd(&EV, E);
1157 free_evalue_refs(&EV);
1158 value_clear(*q);
1159 free(q);
1160 return false;
1163 static void ceil_mod(Value *coef, int len, Value d, ZZ& f, evalue *EP, Polyhedron *PD)
1165 Value m;
1166 value_init(m);
1167 value_set_si(m, -1);
1169 Vector_Scale(coef, coef, m, len);
1171 value_assign(m, d);
1172 int j = normal_mod(coef, len, &m);
1174 if (j == len) {
1175 value_clear(m);
1176 return;
1179 vec_ZZ num;
1180 values2zz(coef, num, len);
1182 ZZ g;
1183 value2zz(m, g);
1185 evalue tmp;
1186 value_init(tmp.d);
1187 evalue_set_si(&tmp, 0, 1);
1189 int p = j;
1190 if (g % 2 == 0)
1191 while (j < len-1 && (num[j] == g/2 || num[j] == 0))
1192 ++j;
1193 if ((j < len-1 && num[j] > g/2) || (j == len-1 && num[j] >= (g+1)/2)) {
1194 for (int k = j; k < len-1; ++k)
1195 if (num[k] != 0)
1196 num[k] = g - num[k];
1197 num[len-1] = g - 1 - num[len-1];
1198 value_assign(tmp.d, m);
1199 ZZ t = f*(g-1);
1200 zz2value(t, tmp.x.n);
1201 eadd(&tmp, EP);
1202 f = -f;
1205 if (p >= len-1) {
1206 ZZ t = num[len-1] * f;
1207 zz2value(t, tmp.x.n);
1208 value_assign(tmp.d, m);
1209 eadd(&tmp, EP);
1210 } else {
1211 evalue *E = multi_monom(num);
1212 evalue EV;
1213 value_init(EV.d);
1215 if (PD && !mod_needed(PD, num, m, E)) {
1216 value_init(EV.x.n);
1217 zz2value(f, EV.x.n);
1218 value_assign(EV.d, m);
1219 emul(&EV, E);
1220 eadd(E, EP);
1221 } else {
1222 value_init(EV.x.n);
1223 value_set_si(EV.x.n, 1);
1224 value_assign(EV.d, m);
1225 emul(&EV, E);
1226 value_clear(EV.x.n);
1227 value_set_si(EV.d, 0);
1228 EV.x.p = new_enode(fractional, 3, -1);
1229 evalue_copy(&EV.x.p->arr[0], E);
1230 evalue_set_si(&EV.x.p->arr[1], 0, 1);
1231 value_init(EV.x.p->arr[2].x.n);
1232 zz2value(f, EV.x.p->arr[2].x.n);
1233 value_set_si(EV.x.p->arr[2].d, 1);
1235 eadd(&EV, EP);
1238 free_evalue_refs(&EV);
1239 free_evalue_refs(E);
1240 delete E;
1243 free_evalue_refs(&tmp);
1245 out:
1246 value_clear(m);
1249 evalue* bv_ceil3(Value *coef, int len, Value d, Polyhedron *P)
1251 Vector *val = Vector_Alloc(len);
1253 Value t;
1254 value_init(t);
1255 value_set_si(t, -1);
1256 Vector_Scale(coef, val->p, t, len);
1257 value_absolute(t, d);
1259 vec_ZZ num;
1260 values2zz(val->p, num, len);
1261 evalue *EP = multi_monom(num);
1263 evalue tmp;
1264 value_init(tmp.d);
1265 value_init(tmp.x.n);
1266 value_set_si(tmp.x.n, 1);
1267 value_assign(tmp.d, t);
1269 emul(&tmp, EP);
1271 ZZ one;
1272 one = 1;
1273 ceil_mod(val->p, len, t, one, EP, P);
1274 value_clear(t);
1276 /* copy EP to malloc'ed evalue */
1277 evalue *E;
1278 ALLOC(evalue, E);
1279 *E = *EP;
1280 delete EP;
1282 free_evalue_refs(&tmp);
1283 Vector_Free(val);
1285 return E;
1288 #ifdef USE_MODULO
1289 evalue* lattice_point(
1290 Polyhedron *i, vec_ZZ& lambda, Matrix *W, Value lcm, Polyhedron *PD)
1292 unsigned nparam = W->NbColumns - 1;
1294 Matrix* Rays = rays2(i);
1295 Matrix *T = Transpose(Rays);
1296 Matrix *T2 = Matrix_Copy(T);
1297 Matrix *inv = Matrix_Alloc(T2->NbRows, T2->NbColumns);
1298 int ok = Matrix_Inverse(T2, inv);
1299 assert(ok);
1300 Matrix_Free(Rays);
1301 Matrix_Free(T2);
1302 mat_ZZ vertex;
1303 matrix2zz(W, vertex, W->NbRows, W->NbColumns);
1305 vec_ZZ num;
1306 num = lambda * vertex;
1308 evalue *EP = multi_monom(num);
1310 evalue tmp;
1311 value_init(tmp.d);
1312 value_init(tmp.x.n);
1313 value_set_si(tmp.x.n, 1);
1314 value_assign(tmp.d, lcm);
1316 emul(&tmp, EP);
1318 Matrix *L = Matrix_Alloc(inv->NbRows, W->NbColumns);
1319 Matrix_Product(inv, W, L);
1321 mat_ZZ RT;
1322 matrix2zz(T, RT, T->NbRows, T->NbColumns);
1323 Matrix_Free(T);
1325 vec_ZZ p = lambda * RT;
1327 for (int i = 0; i < L->NbRows; ++i) {
1328 ceil_mod(L->p[i], nparam+1, lcm, p[i], EP, PD);
1331 Matrix_Free(L);
1333 Matrix_Free(inv);
1334 free_evalue_refs(&tmp);
1335 return EP;
1337 #else
1338 evalue* lattice_point(
1339 Polyhedron *i, vec_ZZ& lambda, Matrix *W, Value lcm, Polyhedron *PD)
1341 Matrix *T = Transpose(W);
1342 unsigned nparam = T->NbRows - 1;
1344 evalue *EP = new evalue();
1345 value_init(EP->d);
1346 evalue_set_si(EP, 0, 1);
1348 evalue ev;
1349 Vector *val = Vector_Alloc(nparam+1);
1350 value_set_si(val->p[nparam], 1);
1351 ZZ offset(INIT_VAL, 0);
1352 value_init(ev.d);
1353 vertex_period(i, lambda, T, lcm, 0, val, EP, &ev, offset);
1354 Vector_Free(val);
1355 eadd(&ev, EP);
1356 free_evalue_refs(&ev);
1358 Matrix_Free(T);
1360 reduce_evalue(EP);
1362 return EP;
1364 #endif
1366 void lattice_point(
1367 Param_Vertices* V, Polyhedron *i, vec_ZZ& lambda, term_info* term,
1368 Polyhedron *PD)
1370 unsigned nparam = V->Vertex->NbColumns - 2;
1371 unsigned dim = i->Dimension;
1372 mat_ZZ vertex;
1373 vertex.SetDims(V->Vertex->NbRows, nparam+1);
1374 Value lcm, tmp;
1375 value_init(lcm);
1376 value_init(tmp);
1377 value_set_si(lcm, 1);
1378 for (int j = 0; j < V->Vertex->NbRows; ++j) {
1379 value_lcm(lcm, V->Vertex->p[j][nparam+1], &lcm);
1381 if (value_notone_p(lcm)) {
1382 Matrix * mv = Matrix_Alloc(dim, nparam+1);
1383 for (int j = 0 ; j < dim; ++j) {
1384 value_division(tmp, lcm, V->Vertex->p[j][nparam+1]);
1385 Vector_Scale(V->Vertex->p[j], mv->p[j], tmp, nparam+1);
1388 term->E = lattice_point(i, lambda, mv, lcm, PD);
1389 term->constant = 0;
1391 Matrix_Free(mv);
1392 value_clear(lcm);
1393 value_clear(tmp);
1394 return;
1396 for (int i = 0; i < V->Vertex->NbRows; ++i) {
1397 assert(value_one_p(V->Vertex->p[i][nparam+1])); // for now
1398 values2zz(V->Vertex->p[i], vertex[i], nparam+1);
1401 vec_ZZ num;
1402 num = lambda * vertex;
1404 int p = -1;
1405 int nn = 0;
1406 for (int j = 0; j < nparam; ++j)
1407 if (num[j] != 0) {
1408 ++nn;
1409 p = j;
1411 if (nn >= 2) {
1412 term->E = multi_monom(num);
1413 term->constant = 0;
1414 } else {
1415 term->E = NULL;
1416 term->constant = num[nparam];
1417 term->pos = p;
1418 if (p != -1)
1419 term->coeff = num[p];
1422 value_clear(lcm);
1423 value_clear(tmp);
1426 static void normalize(ZZ& sign, ZZ& num, vec_ZZ& den)
1428 unsigned dim = den.length();
1430 int change = 0;
1432 for (int j = 0; j < den.length(); ++j) {
1433 if (den[j] > 0)
1434 change ^= 1;
1435 else {
1436 den[j] = abs(den[j]);
1437 num += den[j];
1440 if (change)
1441 sign = -sign;
1444 /* input:
1445 * f: the powers in the denominator for the remaining vars
1446 * each row refers to a factor
1447 * den_s: for each factor, the power of (s+1)
1448 * sign
1449 * num_s: powers in the numerator corresponding to the summed vars
1450 * num_p: powers in the numerator corresponidng to the remaining vars
1451 * number of rays in cone: "dim" = "k"
1452 * length of each ray: "dim" = "d"
1453 * for now, it is assume: k == d
1454 * output:
1455 * den_p: for each factor
1456 * 0: independent of remaining vars
1457 * 1: power corresponds to corresponding row in f
1458 * -1: power is inverse of corresponding row in f
1460 static void normalize(ZZ& sign,
1461 ZZ& num_s, vec_ZZ& num_p, vec_ZZ& den_s, vec_ZZ& den_p,
1462 mat_ZZ& f)
1464 unsigned dim = f.NumRows();
1465 unsigned nparam = num_p.length();
1466 unsigned nvar = dim - nparam;
1468 int change = 0;
1470 for (int j = 0; j < den_s.length(); ++j) {
1471 if (den_s[j] == 0) {
1472 den_p[j] = 1;
1473 continue;
1475 int k;
1476 for (k = 0; k < nparam; ++k)
1477 if (f[j][k] != 0)
1478 break;
1479 if (k < nparam) {
1480 if (den_s[j] > 0) {
1481 den_p[j] = -1;
1482 num_p -= f[j];
1483 } else
1484 den_p[j] = 1;
1485 } else
1486 den_p[j] = 0;
1487 if (den_s[j] > 0)
1488 change ^= 1;
1489 else {
1490 den_s[j] = abs(den_s[j]);
1491 num_s += den_s[j];
1495 if (change)
1496 sign = -sign;
1499 struct counter : public polar_decomposer {
1500 vec_ZZ lambda;
1501 mat_ZZ rays;
1502 vec_ZZ vertex;
1503 vec_ZZ den;
1504 ZZ sign;
1505 ZZ num;
1506 int j;
1507 Polyhedron *P;
1508 unsigned dim;
1509 mpq_t count;
1511 counter(Polyhedron *P) {
1512 this->P = P;
1513 dim = P->Dimension;
1514 randomvector(P, lambda, dim);
1515 rays.SetDims(dim, dim);
1516 den.SetLength(dim);
1517 mpq_init(count);
1520 void start(unsigned MaxRays);
1522 ~counter() {
1523 mpq_clear(count);
1526 virtual void handle_polar(Polyhedron *P, int sign);
1529 void counter::handle_polar(Polyhedron *C, int s)
1531 int r = 0;
1532 assert(C->NbRays-1 == dim);
1533 add_rays(rays, C, &r);
1534 for (int k = 0; k < dim; ++k) {
1535 assert(lambda * rays[k] != 0);
1538 sign = s;
1540 lattice_point(P->Ray[j]+1, C, vertex);
1541 num = vertex * lambda;
1542 den = rays * lambda;
1543 normalize(sign, num, den);
1545 dpoly d(dim, num);
1546 dpoly n(dim, den[0], 1);
1547 for (int k = 1; k < dim; ++k) {
1548 dpoly fact(dim, den[k], 1);
1549 n *= fact;
1551 d.div(n, count, sign);
1554 void counter::start(unsigned MaxRays)
1556 for (j = 0; j < P->NbRays; ++j) {
1557 Polyhedron *C = supporting_cone(P, j);
1558 decompose(C, MaxRays);
1562 // incremental counter
1563 struct icounter : public polar_decomposer {
1564 vec_ZZ lambda;
1565 vec_ZZ vertex;
1566 //vec_ZZ den;
1567 ZZ sgn;
1568 ZZ num;
1569 ZZ one;
1570 int j;
1571 Polyhedron *P;
1572 unsigned dim;
1573 mpq_t count;
1574 mpq_t tcount;
1575 mpz_t tn;
1576 mpz_t td;
1578 icounter(Polyhedron *P) {
1579 this->P = P;
1580 dim = P->Dimension;
1581 lambda.SetLength(1);
1582 lambda[0] = 1;
1583 //den.SetLength(dim);
1584 mpq_init(count);
1585 mpq_init(tcount);
1586 mpz_init(tn);
1587 mpz_init(td);
1588 one = 1;
1591 void start(unsigned MaxRays);
1593 ~icounter() {
1594 mpq_clear(count);
1595 mpq_clear(tcount);
1596 mpz_clear(tn);
1597 mpz_clear(td);
1600 virtual void handle_polar(Polyhedron *P, int sign);
1601 void reduce(ZZ& c, ZZ& cd, vec_ZZ& num, mat_ZZ& den_f);
1602 void recurse(ZZ c, ZZ cd, vec_ZZ& num, mat_ZZ& den);
1605 void icounter::recurse(ZZ c, ZZ cd, vec_ZZ& num, mat_ZZ& den)
1607 unsigned d = num.length();
1608 unsigned len = den.NumRows(); // number of factors in den
1610 reduce(c, cd, num, den);
1613 void icounter::reduce(ZZ& c, ZZ& cd, vec_ZZ& num, mat_ZZ& den_f)
1615 unsigned len = den_f.NumRows(); // number of factors in den
1616 unsigned d = num.length()-1;
1618 vec_ZZ den_s;
1619 den_s.SetLength(len);
1620 mat_ZZ den_r;
1621 den_r.SetDims(len, d);
1623 int r, k;
1624 for (r = 0; r < len; ++r) {
1625 den_s[r] = den_f[r][0];
1626 for (k = 1; k <= d; ++k)
1627 den_r[r][k-1] = den_f[r][k];
1630 if (d == 0) {
1631 ZZ num_s = num[0];
1632 normalize(c, num_s, den_s);
1634 dpoly n(len, num_s);
1635 dpoly D(len, den_s[0], 1);
1636 for (int k = 1; k < len; ++k) {
1637 dpoly fact(len, den_s[k], 1);
1638 D *= fact;
1640 mpq_set_si(tcount, 0, 1);
1641 n.div(D, tcount, one);
1642 zz2value(c, tn);
1643 zz2value(cd, td);
1644 mpz_mul(mpq_numref(tcount), mpq_numref(tcount), tn);
1645 mpz_mul(mpq_denref(tcount), mpq_denref(tcount), td);
1646 mpq_canonicalize(tcount);
1647 mpq_add(count, count, tcount);
1648 return;
1650 assert(num.length() > 1);
1652 ZZ num_s = num[0];
1653 vec_ZZ num_p;
1654 num_p.SetLength(d);
1655 for (k = 1 ; k <= d; ++k)
1656 num_p[k-1] = num[k];
1658 vec_ZZ den_p;
1659 den_p.SetLength(len);
1661 normalize(c, num_s, num_p, den_s, den_p, den_r);
1663 /* Since we're working incrementally, we should look
1664 * for the "easiest" parameter first.
1665 * In particular we should handle the parameters such
1666 * that no_param + only_param == len, since that allows
1667 * us to decouple the problem and the split off part
1668 * may very well be zero
1670 int only_param = 0;
1671 int no_param = 0;
1672 for (int k = 0; k < len; ++k) {
1673 if (den_p[k] == 0)
1674 ++no_param;
1675 else if (den_s[k] == 0)
1676 ++only_param;
1678 if (no_param == 0) {
1679 for (int k = 0; k < len; ++k)
1680 if (den_p[k] == -1)
1681 den_r[k] = -den_r[k];
1682 recurse(c, cd, num_p, den_r);
1683 } else {
1684 int k, l;
1685 mat_ZZ pden;
1686 pden.SetDims(only_param, d);
1688 for (k = 0, l = 0; k < len; ++k)
1689 if (den_s[k] == 0)
1690 pden[l++] = den_r[k];
1692 for (k = 0; k < len; ++k)
1693 if (den_p[k] == 0)
1694 break;
1696 dpoly n(no_param, num_s);
1697 dpoly D(no_param, den_s[k], 1);
1698 for ( ; ++k < len; )
1699 if (den_p[k] == 0) {
1700 dpoly fact(no_param, den_s[k], 1);
1701 D *= fact;
1704 if (no_param + only_param == len) {
1705 mpq_set_si(tcount, 0, 1);
1706 n.div(D, tcount, one);
1708 ZZ qn, qd;
1709 value2zz(mpq_numref(tcount), qn);
1710 value2zz(mpq_denref(tcount), qd);
1712 qn *= c;
1713 qd *= cd;
1715 if (qn != 0)
1716 recurse(qn, qd, num_p, pden);
1717 } else {
1718 dpoly_r * r = 0;
1720 for (k = 0; k < len; ++k) {
1721 if (den_s[k] == 0 || den_p[k] == 0)
1722 continue;
1724 dpoly pd(no_param-1, den_s[k], 1);
1725 int s = den_p[k] < 0 ? -1 : 1;
1727 if (r == 0)
1728 r = new dpoly_r(n, pd, k, s, len);
1729 else {
1730 dpoly_r *nr = new dpoly_r(r, pd, k, s, len);
1731 delete r;
1732 r = nr;
1736 dpoly_r *rc = r->div(D);
1738 rc->denom *= cd;
1740 int common = pden.NumRows();
1741 vector< dpoly_r_term * >& final = rc->c[rc->len-1];
1742 int rows;
1743 for (int j = 0; j < final.size(); ++j) {
1744 if (final[j]->coeff == 0)
1745 continue;
1746 rows = common;
1747 pden.SetDims(rows, pden.NumCols());
1748 for (int k = 0; k < rc->dim; ++k) {
1749 int n = final[j]->powers[k];
1750 if (n == 0)
1751 continue;
1752 int abs_n = n < 0 ? -n : n;
1753 pden.SetDims(rows+abs_n, pden.NumCols());
1754 for (int l = 0; l < abs_n; ++l) {
1755 if (n > 0)
1756 pden[rows+l] = den_r[k];
1757 else
1758 pden[rows+l] = -den_r[k];
1760 rows += abs_n;
1762 final[j]->coeff *= c;
1763 recurse(final[j]->coeff, rc->denom, num_p, pden);
1766 delete rc;
1767 delete r;
1772 void icounter::handle_polar(Polyhedron *C, int s)
1774 assert(C->NbRays-1 == dim);
1776 sgn = s;
1778 lattice_point(P->Ray[j]+1, C, vertex);
1780 mat_ZZ den;
1781 den.SetDims(dim, dim);
1783 int r;
1784 for (r = 0; r < dim; ++r)
1785 values2zz(C->Ray[r]+1, den[r], dim);
1787 reduce(sgn, one, vertex, den);
1790 void icounter::start(unsigned MaxRays)
1792 for (j = 0; j < P->NbRays; ++j) {
1793 Polyhedron *C = supporting_cone(P, j);
1794 decompose(C, MaxRays);
1798 typedef Polyhedron * Polyhedron_p;
1800 void barvinok_count(Polyhedron *P, Value* result, unsigned NbMaxCons)
1802 Polyhedron ** vcone;
1803 ZZ sign;
1804 unsigned dim;
1805 int allocated = 0;
1806 Value factor;
1807 Polyhedron *Q;
1808 int r = 0;
1810 if (emptyQ(P)) {
1811 value_set_si(*result, 0);
1812 return;
1814 if (P->NbBid == 0)
1815 for (; r < P->NbRays; ++r)
1816 if (value_zero_p(P->Ray[r][P->Dimension+1]))
1817 break;
1818 if (P->NbBid !=0 || r < P->NbRays) {
1819 value_set_si(*result, -1);
1820 return;
1822 if (P->NbEq != 0) {
1823 P = remove_equalities(P);
1824 if (emptyQ(P)) {
1825 Polyhedron_Free(P);
1826 value_set_si(*result, 0);
1827 return;
1829 allocated = 1;
1831 value_init(factor);
1832 value_set_si(factor, 1);
1833 Q = Polyhedron_Reduce(P, &factor);
1834 if (Q) {
1835 if (allocated)
1836 Polyhedron_Free(P);
1837 P = Q;
1838 allocated = 1;
1840 if (P->Dimension == 0) {
1841 value_assign(*result, factor);
1842 if (allocated)
1843 Polyhedron_Free(P);
1844 value_clear(factor);
1845 return;
1848 icounter cnt(P);
1849 cnt.start(NbMaxCons);
1851 assert(value_one_p(&cnt.count[0]._mp_den));
1852 value_multiply(*result, &cnt.count[0]._mp_num, factor);
1854 if (allocated)
1855 Polyhedron_Free(P);
1856 value_clear(factor);
1859 static void uni_polynom(int param, Vector *c, evalue *EP)
1861 unsigned dim = c->Size-2;
1862 value_init(EP->d);
1863 value_set_si(EP->d,0);
1864 EP->x.p = new_enode(polynomial, dim+1, param+1);
1865 for (int j = 0; j <= dim; ++j)
1866 evalue_set(&EP->x.p->arr[j], c->p[j], c->p[dim+1]);
1869 static void multi_polynom(Vector *c, evalue* X, evalue *EP)
1871 unsigned dim = c->Size-2;
1872 evalue EC;
1874 value_init(EC.d);
1875 evalue_set(&EC, c->p[dim], c->p[dim+1]);
1877 value_init(EP->d);
1878 evalue_set(EP, c->p[dim], c->p[dim+1]);
1880 for (int i = dim-1; i >= 0; --i) {
1881 emul(X, EP);
1882 value_assign(EC.x.n, c->p[i]);
1883 eadd(&EC, EP);
1885 free_evalue_refs(&EC);
1888 Polyhedron *unfringe (Polyhedron *P, unsigned MaxRays)
1890 int len = P->Dimension+2;
1891 Polyhedron *T, *R = P;
1892 Value g;
1893 value_init(g);
1894 Vector *row = Vector_Alloc(len);
1895 value_set_si(row->p[0], 1);
1897 R = DomainConstraintSimplify(Polyhedron_Copy(P), MaxRays);
1899 Matrix *M = Matrix_Alloc(2, len-1);
1900 value_set_si(M->p[1][len-2], 1);
1901 for (int v = 0; v < P->Dimension; ++v) {
1902 value_set_si(M->p[0][v], 1);
1903 Polyhedron *I = Polyhedron_Image(P, M, 2+1);
1904 value_set_si(M->p[0][v], 0);
1905 for (int r = 0; r < I->NbConstraints; ++r) {
1906 if (value_zero_p(I->Constraint[r][0]))
1907 continue;
1908 if (value_zero_p(I->Constraint[r][1]))
1909 continue;
1910 if (value_one_p(I->Constraint[r][1]))
1911 continue;
1912 if (value_mone_p(I->Constraint[r][1]))
1913 continue;
1914 value_absolute(g, I->Constraint[r][1]);
1915 Vector_Set(row->p+1, 0, len-2);
1916 value_division(row->p[1+v], I->Constraint[r][1], g);
1917 mpz_fdiv_q(row->p[len-1], I->Constraint[r][2], g);
1918 T = R;
1919 R = AddConstraints(row->p, 1, R, MaxRays);
1920 if (T != P)
1921 Polyhedron_Free(T);
1923 Polyhedron_Free(I);
1925 Matrix_Free(M);
1926 Vector_Free(row);
1927 value_clear(g);
1928 return R;
1931 static Polyhedron *reduce_domain(Polyhedron *D, Matrix *CT, Polyhedron *CEq,
1932 Polyhedron **fVD, int nd, unsigned MaxRays)
1934 assert(CEq);
1936 Polyhedron *Dt;
1937 Dt = CT ? DomainPreimage(D, CT, MaxRays) : D;
1938 Polyhedron *rVD = DomainIntersection(Dt, CEq, MaxRays);
1940 /* if rVD is empty or too small in geometric dimension */
1941 if(!rVD || emptyQ(rVD) ||
1942 (rVD->Dimension-rVD->NbEq < Dt->Dimension-Dt->NbEq-CEq->NbEq)) {
1943 if(rVD)
1944 Domain_Free(rVD);
1945 if (CT)
1946 Domain_Free(Dt);
1947 return 0; /* empty validity domain */
1950 if (CT)
1951 Domain_Free(Dt);
1953 fVD[nd] = Domain_Copy(rVD);
1954 for (int i = 0 ; i < nd; ++i) {
1955 Polyhedron *I = DomainIntersection(fVD[nd], fVD[i], MaxRays);
1956 if (emptyQ(I)) {
1957 Domain_Free(I);
1958 continue;
1960 Polyhedron *F = DomainSimplify(I, fVD[nd], MaxRays);
1961 if (F->NbEq == 1) {
1962 Polyhedron *T = rVD;
1963 rVD = DomainDifference(rVD, F, MaxRays);
1964 Domain_Free(T);
1966 Domain_Free(F);
1967 Domain_Free(I);
1970 rVD = DomainConstraintSimplify(rVD, MaxRays);
1971 if (emptyQ(rVD)) {
1972 Domain_Free(fVD[nd]);
1973 Domain_Free(rVD);
1974 return 0;
1977 Value c;
1978 value_init(c);
1979 barvinok_count(rVD, &c, MaxRays);
1980 if (value_zero_p(c)) {
1981 Domain_Free(rVD);
1982 rVD = 0;
1984 value_clear(c);
1986 return rVD;
1989 static bool Polyhedron_is_infinite(Polyhedron *P, unsigned nparam)
1991 int r;
1992 for (r = 0; r < P->NbRays; ++r)
1993 if (value_zero_p(P->Ray[r][0]) ||
1994 value_zero_p(P->Ray[r][P->Dimension+1])) {
1995 int i;
1996 for (i = P->Dimension - nparam; i < P->Dimension; ++i)
1997 if (value_notzero_p(P->Ray[r][i+1]))
1998 break;
1999 if (i >= P->Dimension)
2000 break;
2002 return r < P->NbRays;
2005 /* Check whether all rays point in the positive directions
2006 * for the parameters
2008 static bool Polyhedron_has_positive_rays(Polyhedron *P, unsigned nparam)
2010 int r;
2011 for (r = 0; r < P->NbRays; ++r)
2012 if (value_zero_p(P->Ray[r][P->Dimension+1])) {
2013 int i;
2014 for (i = P->Dimension - nparam; i < P->Dimension; ++i)
2015 if (value_neg_p(P->Ray[r][i+1]))
2016 return false;
2018 return true;
2021 typedef evalue * evalue_p;
2023 struct enumerator : public polar_decomposer {
2024 vec_ZZ lambda;
2025 unsigned dim, nbV;
2026 evalue ** vE;
2027 int _i;
2028 mat_ZZ rays;
2029 vec_ZZ den;
2030 ZZ sign;
2031 Polyhedron *P;
2032 Param_Vertices *V;
2033 term_info num;
2034 Vector *c;
2035 mpq_t count;
2037 enumerator(Polyhedron *P, unsigned dim, unsigned nbV) {
2038 this->P = P;
2039 this->dim = dim;
2040 this->nbV = nbV;
2041 randomvector(P, lambda, dim);
2042 rays.SetDims(dim, dim);
2043 den.SetLength(dim);
2044 c = Vector_Alloc(dim+2);
2046 vE = new evalue_p[nbV];
2047 for (int j = 0; j < nbV; ++j)
2048 vE[j] = 0;
2050 mpq_init(count);
2053 void decompose_at(Param_Vertices *V, int _i, unsigned MaxRays) {
2054 Polyhedron *C = supporting_cone_p(P, V);
2055 this->_i = _i;
2056 this->V = V;
2058 vE[_i] = new evalue;
2059 value_init(vE[_i]->d);
2060 evalue_set_si(vE[_i], 0, 1);
2062 decompose(C, MaxRays);
2065 ~enumerator() {
2066 mpq_clear(count);
2067 Vector_Free(c);
2069 for (int j = 0; j < nbV; ++j)
2070 if (vE[j]) {
2071 free_evalue_refs(vE[j]);
2072 delete vE[j];
2074 delete [] vE;
2077 virtual void handle_polar(Polyhedron *P, int sign);
2080 void enumerator::handle_polar(Polyhedron *C, int s)
2082 int r = 0;
2083 assert(C->NbRays-1 == dim);
2084 add_rays(rays, C, &r);
2085 for (int k = 0; k < dim; ++k) {
2086 assert(lambda * rays[k] != 0);
2089 sign = s;
2091 lattice_point(V, C, lambda, &num, 0);
2092 den = rays * lambda;
2093 normalize(sign, num.constant, den);
2095 dpoly n(dim, den[0], 1);
2096 for (int k = 1; k < dim; ++k) {
2097 dpoly fact(dim, den[k], 1);
2098 n *= fact;
2100 if (num.E != NULL) {
2101 ZZ one(INIT_VAL, 1);
2102 dpoly_n d(dim, num.constant, one);
2103 d.div(n, c, sign);
2104 evalue EV;
2105 multi_polynom(c, num.E, &EV);
2106 eadd(&EV , vE[_i]);
2107 free_evalue_refs(&EV);
2108 free_evalue_refs(num.E);
2109 delete num.E;
2110 } else if (num.pos != -1) {
2111 dpoly_n d(dim, num.constant, num.coeff);
2112 d.div(n, c, sign);
2113 evalue EV;
2114 uni_polynom(num.pos, c, &EV);
2115 eadd(&EV , vE[_i]);
2116 free_evalue_refs(&EV);
2117 } else {
2118 mpq_set_si(count, 0, 1);
2119 dpoly d(dim, num.constant);
2120 d.div(n, count, sign);
2121 evalue EV;
2122 value_init(EV.d);
2123 evalue_set(&EV, &count[0]._mp_num, &count[0]._mp_den);
2124 eadd(&EV , vE[_i]);
2125 free_evalue_refs(&EV);
2129 evalue* barvinok_enumerate_ev(Polyhedron *P, Polyhedron* C, unsigned MaxRays)
2131 //P = unfringe(P, MaxRays);
2132 Polyhedron *CEq = NULL, *rVD, *pVD, *CA;
2133 Matrix *CT = NULL;
2134 Param_Polyhedron *PP = NULL;
2135 Param_Domain *D, *next;
2136 Param_Vertices *V;
2137 int r = 0;
2138 unsigned nparam = C->Dimension;
2139 evalue *eres;
2140 ALLOC(evalue, eres);
2141 value_init(eres->d);
2142 value_set_si(eres->d, 0);
2144 evalue factor;
2145 value_init(factor.d);
2146 evalue_set_si(&factor, 1, 1);
2148 CA = align_context(C, P->Dimension, MaxRays);
2149 P = DomainIntersection(P, CA, MaxRays);
2150 Polyhedron_Free(CA);
2152 if (C->Dimension == 0 || emptyQ(P)) {
2153 constant:
2154 eres->x.p = new_enode(partition, 2, C->Dimension);
2155 EVALUE_SET_DOMAIN(eres->x.p->arr[0],
2156 DomainConstraintSimplify(CEq ? CEq : Polyhedron_Copy(C), MaxRays));
2157 value_set_si(eres->x.p->arr[1].d, 1);
2158 value_init(eres->x.p->arr[1].x.n);
2159 if (emptyQ(P))
2160 value_set_si(eres->x.p->arr[1].x.n, 0);
2161 else
2162 barvinok_count(P, &eres->x.p->arr[1].x.n, MaxRays);
2163 out:
2164 emul(&factor, eres);
2165 reduce_evalue(eres);
2166 free_evalue_refs(&factor);
2167 Polyhedron_Free(P);
2168 if (CT)
2169 Matrix_Free(CT);
2170 if (PP)
2171 Param_Polyhedron_Free(PP);
2173 return eres;
2175 if (Polyhedron_is_infinite(P, nparam))
2176 goto constant;
2178 if (P->NbEq != 0) {
2179 Matrix *f;
2180 P = remove_equalities_p(P, P->Dimension-nparam, &f);
2181 mask(f, &factor);
2182 Matrix_Free(f);
2184 if (P->Dimension == nparam) {
2185 CEq = P;
2186 P = Universe_Polyhedron(0);
2187 goto constant;
2190 Polyhedron *Q = ParamPolyhedron_Reduce(P, P->Dimension-nparam, &factor);
2191 if (Q) {
2192 Polyhedron_Free(P);
2193 if (Q->Dimension == nparam) {
2194 CEq = Q;
2195 P = Universe_Polyhedron(0);
2196 goto constant;
2198 P = Q;
2200 Polyhedron *oldP = P;
2201 PP = Polyhedron2Param_SimplifiedDomain(&P,C,MaxRays,&CEq,&CT);
2202 if (P != oldP)
2203 Polyhedron_Free(oldP);
2205 if (isIdentity(CT)) {
2206 Matrix_Free(CT);
2207 CT = NULL;
2208 } else {
2209 assert(CT->NbRows != CT->NbColumns);
2210 if (CT->NbRows == 1) // no more parameters
2211 goto constant;
2212 nparam = CT->NbRows - 1;
2215 unsigned dim = P->Dimension - nparam;
2217 enumerator et(P, dim, PP->nbV);
2219 int nd;
2220 for (nd = 0, D=PP->D; D; ++nd, D=D->next);
2221 struct section { Polyhedron *D; evalue E; };
2222 section *s = new section[nd];
2223 Polyhedron **fVD = new Polyhedron_p[nd];
2225 for(nd = 0, D=PP->D; D; D=next) {
2226 next = D->next;
2228 Polyhedron *rVD = reduce_domain(D->Domain, CT, CEq,
2229 fVD, nd, MaxRays);
2230 if (!rVD)
2231 continue;
2233 pVD = CT ? DomainImage(rVD,CT,MaxRays) : rVD;
2235 value_init(s[nd].E.d);
2236 evalue_set_si(&s[nd].E, 0, 1);
2238 FORALL_PVertex_in_ParamPolyhedron(V,D,PP) // _i is internal counter
2239 if (!et.vE[_i])
2240 et.decompose_at(V, _i, MaxRays);
2241 eadd(et.vE[_i] , &s[nd].E);
2242 END_FORALL_PVertex_in_ParamPolyhedron;
2243 reduce_in_domain(&s[nd].E, pVD);
2245 if (CT)
2246 addeliminatedparams_evalue(&s[nd].E, CT);
2247 s[nd].D = rVD;
2248 ++nd;
2249 if (rVD != pVD)
2250 Domain_Free(pVD);
2253 if (nd == 0)
2254 evalue_set_si(eres, 0, 1);
2255 else {
2256 eres->x.p = new_enode(partition, 2*nd, C->Dimension);
2257 for (int j = 0; j < nd; ++j) {
2258 EVALUE_SET_DOMAIN(eres->x.p->arr[2*j], s[j].D);
2259 value_clear(eres->x.p->arr[2*j+1].d);
2260 eres->x.p->arr[2*j+1] = s[j].E;
2261 Domain_Free(fVD[j]);
2264 delete [] s;
2265 delete [] fVD;
2268 if (CEq)
2269 Polyhedron_Free(CEq);
2271 goto out;
2274 Enumeration* barvinok_enumerate(Polyhedron *P, Polyhedron* C, unsigned MaxRays)
2276 evalue *EP = barvinok_enumerate_ev(P, C, MaxRays);
2278 return partition2enumeration(EP);
2281 static void SwapColumns(Value **V, int n, int i, int j)
2283 for (int r = 0; r < n; ++r)
2284 value_swap(V[r][i], V[r][j]);
2287 static void SwapColumns(Polyhedron *P, int i, int j)
2289 SwapColumns(P->Constraint, P->NbConstraints, i, j);
2290 SwapColumns(P->Ray, P->NbRays, i, j);
2293 static void negative_test_constraint(Value *l, Value *u, Value *c, int pos,
2294 int len, Value *v)
2296 value_oppose(*v, u[pos+1]);
2297 Vector_Combine(l+1, u+1, c+1, *v, l[pos+1], len-1);
2298 value_multiply(*v, *v, l[pos+1]);
2299 value_substract(c[len-1], c[len-1], *v);
2300 value_set_si(*v, -1);
2301 Vector_Scale(c+1, c+1, *v, len-1);
2302 value_decrement(c[len-1], c[len-1]);
2303 ConstraintSimplify(c, c, len, v);
2306 static bool parallel_constraints(Value *l, Value *u, Value *c, int pos,
2307 int len)
2309 bool parallel;
2310 Value g1;
2311 Value g2;
2312 value_init(g1);
2313 value_init(g2);
2315 Vector_Gcd(&l[1+pos], len, &g1);
2316 Vector_Gcd(&u[1+pos], len, &g2);
2317 Vector_Combine(l+1+pos, u+1+pos, c+1, g2, g1, len);
2318 parallel = First_Non_Zero(c+1, len) == -1;
2320 value_clear(g1);
2321 value_clear(g2);
2323 return parallel;
2326 static void negative_test_constraint7(Value *l, Value *u, Value *c, int pos,
2327 int exist, int len, Value *v)
2329 Value g;
2330 value_init(g);
2332 Vector_Gcd(&u[1+pos], exist, v);
2333 Vector_Gcd(&l[1+pos], exist, &g);
2334 Vector_Combine(l+1, u+1, c+1, *v, g, len-1);
2335 value_multiply(*v, *v, g);
2336 value_substract(c[len-1], c[len-1], *v);
2337 value_set_si(*v, -1);
2338 Vector_Scale(c+1, c+1, *v, len-1);
2339 value_decrement(c[len-1], c[len-1]);
2340 ConstraintSimplify(c, c, len, v);
2342 value_clear(g);
2345 static void oppose_constraint(Value *c, int len, Value *v)
2347 value_set_si(*v, -1);
2348 Vector_Scale(c+1, c+1, *v, len-1);
2349 value_decrement(c[len-1], c[len-1]);
2352 static bool SplitOnConstraint(Polyhedron *P, int i, int l, int u,
2353 int nvar, int len, int exist, int MaxRays,
2354 Vector *row, Value& f, bool independent,
2355 Polyhedron **pos, Polyhedron **neg)
2357 negative_test_constraint(P->Constraint[l], P->Constraint[u],
2358 row->p, nvar+i, len, &f);
2359 *neg = AddConstraints(row->p, 1, P, MaxRays);
2361 /* We found an independent, but useless constraint
2362 * Maybe we should detect this earlier and not
2363 * mark the variable as INDEPENDENT
2365 if (emptyQ((*neg))) {
2366 Polyhedron_Free(*neg);
2367 return false;
2370 oppose_constraint(row->p, len, &f);
2371 *pos = AddConstraints(row->p, 1, P, MaxRays);
2373 if (emptyQ((*pos))) {
2374 Polyhedron_Free(*neg);
2375 Polyhedron_Free(*pos);
2376 return false;
2379 return true;
2383 * unimodularly transform P such that constraint r is transformed
2384 * into a constraint that involves only a single (the first)
2385 * existential variable
2388 static Polyhedron *rotate_along(Polyhedron *P, int r, int nvar, int exist,
2389 unsigned MaxRays)
2391 Value g;
2392 value_init(g);
2394 Vector *row = Vector_Alloc(exist);
2395 Vector_Copy(P->Constraint[r]+1+nvar, row->p, exist);
2396 Vector_Gcd(row->p, exist, &g);
2397 if (value_notone_p(g))
2398 Vector_AntiScale(row->p, row->p, g, exist);
2399 value_clear(g);
2401 Matrix *M = unimodular_complete(row);
2402 Matrix *M2 = Matrix_Alloc(P->Dimension+1, P->Dimension+1);
2403 for (r = 0; r < nvar; ++r)
2404 value_set_si(M2->p[r][r], 1);
2405 for ( ; r < nvar+exist; ++r)
2406 Vector_Copy(M->p[r-nvar], M2->p[r]+nvar, exist);
2407 for ( ; r < P->Dimension+1; ++r)
2408 value_set_si(M2->p[r][r], 1);
2409 Polyhedron *T = Polyhedron_Image(P, M2, MaxRays);
2411 Matrix_Free(M2);
2412 Matrix_Free(M);
2413 Vector_Free(row);
2415 return T;
2418 static bool SplitOnVar(Polyhedron *P, int i,
2419 int nvar, int len, int exist, int MaxRays,
2420 Vector *row, Value& f, bool independent,
2421 Polyhedron **pos, Polyhedron **neg)
2423 int j;
2425 for (int l = P->NbEq; l < P->NbConstraints; ++l) {
2426 if (value_negz_p(P->Constraint[l][nvar+i+1]))
2427 continue;
2429 if (independent) {
2430 for (j = 0; j < exist; ++j)
2431 if (j != i && value_notzero_p(P->Constraint[l][nvar+j+1]))
2432 break;
2433 if (j < exist)
2434 continue;
2437 for (int u = P->NbEq; u < P->NbConstraints; ++u) {
2438 if (value_posz_p(P->Constraint[u][nvar+i+1]))
2439 continue;
2441 if (independent) {
2442 for (j = 0; j < exist; ++j)
2443 if (j != i && value_notzero_p(P->Constraint[u][nvar+j+1]))
2444 break;
2445 if (j < exist)
2446 continue;
2449 if (SplitOnConstraint(P, i, l, u,
2450 nvar, len, exist, MaxRays,
2451 row, f, independent,
2452 pos, neg)) {
2453 if (independent) {
2454 if (i != 0)
2455 SwapColumns(*neg, nvar+1, nvar+1+i);
2457 return true;
2462 return false;
2465 static bool double_bound_pair(Polyhedron *P, int nvar, int exist,
2466 int i, int l1, int l2,
2467 Polyhedron **pos, Polyhedron **neg)
2469 Value f;
2470 value_init(f);
2471 Vector *row = Vector_Alloc(P->Dimension+2);
2472 value_set_si(row->p[0], 1);
2473 value_oppose(f, P->Constraint[l1][nvar+i+1]);
2474 Vector_Combine(P->Constraint[l1]+1, P->Constraint[l2]+1,
2475 row->p+1,
2476 P->Constraint[l2][nvar+i+1], f,
2477 P->Dimension+1);
2478 ConstraintSimplify(row->p, row->p, P->Dimension+2, &f);
2479 *pos = AddConstraints(row->p, 1, P, 0);
2480 value_set_si(f, -1);
2481 Vector_Scale(row->p+1, row->p+1, f, P->Dimension+1);
2482 value_decrement(row->p[P->Dimension+1], row->p[P->Dimension+1]);
2483 *neg = AddConstraints(row->p, 1, P, 0);
2484 Vector_Free(row);
2485 value_clear(f);
2487 return !emptyQ((*pos)) && !emptyQ((*neg));
2490 static bool double_bound(Polyhedron *P, int nvar, int exist,
2491 Polyhedron **pos, Polyhedron **neg)
2493 for (int i = 0; i < exist; ++i) {
2494 int l1, l2;
2495 for (l1 = P->NbEq; l1 < P->NbConstraints; ++l1) {
2496 if (value_negz_p(P->Constraint[l1][nvar+i+1]))
2497 continue;
2498 for (l2 = l1 + 1; l2 < P->NbConstraints; ++l2) {
2499 if (value_negz_p(P->Constraint[l2][nvar+i+1]))
2500 continue;
2501 if (double_bound_pair(P, nvar, exist, i, l1, l2, pos, neg))
2502 return true;
2505 for (l1 = P->NbEq; l1 < P->NbConstraints; ++l1) {
2506 if (value_posz_p(P->Constraint[l1][nvar+i+1]))
2507 continue;
2508 if (l1 < P->NbConstraints)
2509 for (l2 = l1 + 1; l2 < P->NbConstraints; ++l2) {
2510 if (value_posz_p(P->Constraint[l2][nvar+i+1]))
2511 continue;
2512 if (double_bound_pair(P, nvar, exist, i, l1, l2, pos, neg))
2513 return true;
2516 return false;
2518 return false;
2521 enum constraint {
2522 ALL_POS = 1 << 0,
2523 ONE_NEG = 1 << 1,
2524 INDEPENDENT = 1 << 2,
2525 ROT_NEG = 1 << 3
2528 static evalue* enumerate_or(Polyhedron *D,
2529 unsigned exist, unsigned nparam, unsigned MaxRays)
2531 #ifdef DEBUG_ER
2532 fprintf(stderr, "\nER: Or\n");
2533 #endif /* DEBUG_ER */
2535 Polyhedron *N = D->next;
2536 D->next = 0;
2537 evalue *EP =
2538 barvinok_enumerate_e(D, exist, nparam, MaxRays);
2539 Polyhedron_Free(D);
2541 for (D = N; D; D = N) {
2542 N = D->next;
2543 D->next = 0;
2545 evalue *EN =
2546 barvinok_enumerate_e(D, exist, nparam, MaxRays);
2548 eor(EN, EP);
2549 free_evalue_refs(EN);
2550 free(EN);
2551 Polyhedron_Free(D);
2554 reduce_evalue(EP);
2556 return EP;
2559 static evalue* enumerate_sum(Polyhedron *P,
2560 unsigned exist, unsigned nparam, unsigned MaxRays)
2562 int nvar = P->Dimension - exist - nparam;
2563 int toswap = nvar < exist ? nvar : exist;
2564 for (int i = 0; i < toswap; ++i)
2565 SwapColumns(P, 1 + i, nvar+exist - i);
2566 nparam += nvar;
2568 #ifdef DEBUG_ER
2569 fprintf(stderr, "\nER: Sum\n");
2570 #endif /* DEBUG_ER */
2572 evalue *EP = barvinok_enumerate_e(P, exist, nparam, MaxRays);
2574 for (int i = 0; i < /* nvar */ nparam; ++i) {
2575 Matrix *C = Matrix_Alloc(1, 1 + nparam + 1);
2576 value_set_si(C->p[0][0], 1);
2577 evalue split;
2578 value_init(split.d);
2579 value_set_si(split.d, 0);
2580 split.x.p = new_enode(partition, 4, nparam);
2581 value_set_si(C->p[0][1+i], 1);
2582 Matrix *C2 = Matrix_Copy(C);
2583 EVALUE_SET_DOMAIN(split.x.p->arr[0],
2584 Constraints2Polyhedron(C2, MaxRays));
2585 Matrix_Free(C2);
2586 evalue_set_si(&split.x.p->arr[1], 1, 1);
2587 value_set_si(C->p[0][1+i], -1);
2588 value_set_si(C->p[0][1+nparam], -1);
2589 EVALUE_SET_DOMAIN(split.x.p->arr[2],
2590 Constraints2Polyhedron(C, MaxRays));
2591 evalue_set_si(&split.x.p->arr[3], 1, 1);
2592 emul(&split, EP);
2593 free_evalue_refs(&split);
2594 Matrix_Free(C);
2596 reduce_evalue(EP);
2597 evalue_range_reduction(EP);
2599 evalue_frac2floor(EP);
2601 evalue *sum = esum(EP, nvar);
2603 free_evalue_refs(EP);
2604 free(EP);
2605 EP = sum;
2607 evalue_range_reduction(EP);
2609 return EP;
2612 static evalue* split_sure(Polyhedron *P, Polyhedron *S,
2613 unsigned exist, unsigned nparam, unsigned MaxRays)
2615 int nvar = P->Dimension - exist - nparam;
2617 Matrix *M = Matrix_Alloc(exist, S->Dimension+2);
2618 for (int i = 0; i < exist; ++i)
2619 value_set_si(M->p[i][nvar+i+1], 1);
2620 Polyhedron *O = S;
2621 S = DomainAddRays(S, M, MaxRays);
2622 Polyhedron_Free(O);
2623 Polyhedron *F = DomainAddRays(P, M, MaxRays);
2624 Polyhedron *D = DomainDifference(F, S, MaxRays);
2625 O = D;
2626 D = Disjoint_Domain(D, 0, MaxRays);
2627 Polyhedron_Free(F);
2628 Domain_Free(O);
2629 Matrix_Free(M);
2631 M = Matrix_Alloc(P->Dimension+1-exist, P->Dimension+1);
2632 for (int j = 0; j < nvar; ++j)
2633 value_set_si(M->p[j][j], 1);
2634 for (int j = 0; j < nparam+1; ++j)
2635 value_set_si(M->p[nvar+j][nvar+exist+j], 1);
2636 Polyhedron *T = Polyhedron_Image(S, M, MaxRays);
2637 evalue *EP = barvinok_enumerate_e(T, 0, nparam, MaxRays);
2638 Polyhedron_Free(S);
2639 Polyhedron_Free(T);
2640 Matrix_Free(M);
2642 for (Polyhedron *Q = D; Q; Q = Q->next) {
2643 Polyhedron *N = Q->next;
2644 Q->next = 0;
2645 T = DomainIntersection(P, Q, MaxRays);
2646 evalue *E = barvinok_enumerate_e(T, exist, nparam, MaxRays);
2647 eadd(E, EP);
2648 free_evalue_refs(E);
2649 free(E);
2650 Polyhedron_Free(T);
2651 Q->next = N;
2653 Domain_Free(D);
2654 return EP;
2657 static evalue* enumerate_sure(Polyhedron *P,
2658 unsigned exist, unsigned nparam, unsigned MaxRays)
2660 int i;
2661 Polyhedron *S = P;
2662 int nvar = P->Dimension - exist - nparam;
2663 Value lcm;
2664 Value f;
2665 value_init(lcm);
2666 value_init(f);
2668 for (i = 0; i < exist; ++i) {
2669 Matrix *M = Matrix_Alloc(S->NbConstraints, S->Dimension+2);
2670 int c = 0;
2671 value_set_si(lcm, 1);
2672 for (int j = 0; j < S->NbConstraints; ++j) {
2673 if (value_negz_p(S->Constraint[j][1+nvar+i]))
2674 continue;
2675 if (value_one_p(S->Constraint[j][1+nvar+i]))
2676 continue;
2677 value_lcm(lcm, S->Constraint[j][1+nvar+i], &lcm);
2680 for (int j = 0; j < S->NbConstraints; ++j) {
2681 if (value_negz_p(S->Constraint[j][1+nvar+i]))
2682 continue;
2683 if (value_one_p(S->Constraint[j][1+nvar+i]))
2684 continue;
2685 value_division(f, lcm, S->Constraint[j][1+nvar+i]);
2686 Vector_Scale(S->Constraint[j], M->p[c], f, S->Dimension+2);
2687 value_substract(M->p[c][S->Dimension+1],
2688 M->p[c][S->Dimension+1],
2689 lcm);
2690 value_increment(M->p[c][S->Dimension+1],
2691 M->p[c][S->Dimension+1]);
2692 ++c;
2694 Polyhedron *O = S;
2695 S = AddConstraints(M->p[0], c, S, MaxRays);
2696 if (O != P)
2697 Polyhedron_Free(O);
2698 Matrix_Free(M);
2699 if (emptyQ(S)) {
2700 Polyhedron_Free(S);
2701 value_clear(lcm);
2702 value_clear(f);
2703 return 0;
2706 value_clear(lcm);
2707 value_clear(f);
2709 #ifdef DEBUG_ER
2710 fprintf(stderr, "\nER: Sure\n");
2711 #endif /* DEBUG_ER */
2713 return split_sure(P, S, exist, nparam, MaxRays);
2716 static evalue* enumerate_sure2(Polyhedron *P,
2717 unsigned exist, unsigned nparam, unsigned MaxRays)
2719 int nvar = P->Dimension - exist - nparam;
2720 int r;
2721 for (r = 0; r < P->NbRays; ++r)
2722 if (value_one_p(P->Ray[r][0]) &&
2723 value_one_p(P->Ray[r][P->Dimension+1]))
2724 break;
2726 if (r >= P->NbRays)
2727 return 0;
2729 Matrix *M = Matrix_Alloc(nvar + 1 + nparam, P->Dimension+2);
2730 for (int i = 0; i < nvar; ++i)
2731 value_set_si(M->p[i][1+i], 1);
2732 for (int i = 0; i < nparam; ++i)
2733 value_set_si(M->p[i+nvar][1+nvar+exist+i], 1);
2734 Vector_Copy(P->Ray[r]+1+nvar, M->p[nvar+nparam]+1+nvar, exist);
2735 value_set_si(M->p[nvar+nparam][0], 1);
2736 value_set_si(M->p[nvar+nparam][P->Dimension+1], 1);
2737 Polyhedron * F = Rays2Polyhedron(M, MaxRays);
2738 Matrix_Free(M);
2740 Polyhedron *I = DomainIntersection(F, P, MaxRays);
2741 Polyhedron_Free(F);
2743 #ifdef DEBUG_ER
2744 fprintf(stderr, "\nER: Sure2\n");
2745 #endif /* DEBUG_ER */
2747 return split_sure(P, I, exist, nparam, MaxRays);
2750 static evalue* enumerate_cyclic(Polyhedron *P,
2751 unsigned exist, unsigned nparam,
2752 evalue * EP, int r, int p, unsigned MaxRays)
2754 int nvar = P->Dimension - exist - nparam;
2756 /* If EP in its fractional maps only contains references
2757 * to the remainder parameter with appropriate coefficients
2758 * then we could in principle avoid adding existentially
2759 * quantified variables to the validity domains.
2760 * We'd have to replace the remainder by m { p/m }
2761 * and multiply with an appropriate factor that is one
2762 * only in the appropriate range.
2763 * This last multiplication can be avoided if EP
2764 * has a single validity domain with no (further)
2765 * constraints on the remainder parameter
2768 Matrix *CT = Matrix_Alloc(nparam+1, nparam+3);
2769 Matrix *M = Matrix_Alloc(1, 1+nparam+3);
2770 for (int j = 0; j < nparam; ++j)
2771 if (j != p)
2772 value_set_si(CT->p[j][j], 1);
2773 value_set_si(CT->p[p][nparam+1], 1);
2774 value_set_si(CT->p[nparam][nparam+2], 1);
2775 value_set_si(M->p[0][1+p], -1);
2776 value_absolute(M->p[0][1+nparam], P->Ray[0][1+nvar+exist+p]);
2777 value_set_si(M->p[0][1+nparam+1], 1);
2778 Polyhedron *CEq = Constraints2Polyhedron(M, 1);
2779 Matrix_Free(M);
2780 addeliminatedparams_enum(EP, CT, CEq, MaxRays, nparam);
2781 Polyhedron_Free(CEq);
2782 Matrix_Free(CT);
2784 return EP;
2787 static void enumerate_vd_add_ray(evalue *EP, Matrix *Rays, unsigned MaxRays)
2789 if (value_notzero_p(EP->d))
2790 return;
2792 assert(EP->x.p->type == partition);
2793 assert(EP->x.p->pos == EVALUE_DOMAIN(EP->x.p->arr[0])->Dimension);
2794 for (int i = 0; i < EP->x.p->size/2; ++i) {
2795 Polyhedron *D = EVALUE_DOMAIN(EP->x.p->arr[2*i]);
2796 Polyhedron *N = DomainAddRays(D, Rays, MaxRays);
2797 EVALUE_SET_DOMAIN(EP->x.p->arr[2*i], N);
2798 Domain_Free(D);
2802 static evalue* enumerate_line(Polyhedron *P,
2803 unsigned exist, unsigned nparam, unsigned MaxRays)
2805 if (P->NbBid == 0)
2806 return 0;
2808 #ifdef DEBUG_ER
2809 fprintf(stderr, "\nER: Line\n");
2810 #endif /* DEBUG_ER */
2812 int nvar = P->Dimension - exist - nparam;
2813 int i, j;
2814 for (i = 0; i < nparam; ++i)
2815 if (value_notzero_p(P->Ray[0][1+nvar+exist+i]))
2816 break;
2817 assert(i < nparam);
2818 for (j = i+1; j < nparam; ++j)
2819 if (value_notzero_p(P->Ray[0][1+nvar+exist+i]))
2820 break;
2821 assert(j >= nparam); // for now
2823 Matrix *M = Matrix_Alloc(2, P->Dimension+2);
2824 value_set_si(M->p[0][0], 1);
2825 value_set_si(M->p[0][1+nvar+exist+i], 1);
2826 value_set_si(M->p[1][0], 1);
2827 value_set_si(M->p[1][1+nvar+exist+i], -1);
2828 value_absolute(M->p[1][1+P->Dimension], P->Ray[0][1+nvar+exist+i]);
2829 value_decrement(M->p[1][1+P->Dimension], M->p[1][1+P->Dimension]);
2830 Polyhedron *S = AddConstraints(M->p[0], 2, P, MaxRays);
2831 evalue *EP = barvinok_enumerate_e(S, exist, nparam, MaxRays);
2832 Polyhedron_Free(S);
2833 Matrix_Free(M);
2835 return enumerate_cyclic(P, exist, nparam, EP, 0, i, MaxRays);
2838 static int single_param_pos(Polyhedron*P, unsigned exist, unsigned nparam,
2839 int r)
2841 int nvar = P->Dimension - exist - nparam;
2842 if (First_Non_Zero(P->Ray[r]+1, nvar) != -1)
2843 return -1;
2844 int i = First_Non_Zero(P->Ray[r]+1+nvar+exist, nparam);
2845 if (i == -1)
2846 return -1;
2847 if (First_Non_Zero(P->Ray[r]+1+nvar+exist+1, nparam-i-1) != -1)
2848 return -1;
2849 return i;
2852 static evalue* enumerate_remove_ray(Polyhedron *P, int r,
2853 unsigned exist, unsigned nparam, unsigned MaxRays)
2855 #ifdef DEBUG_ER
2856 fprintf(stderr, "\nER: RedundantRay\n");
2857 #endif /* DEBUG_ER */
2859 Value one;
2860 value_init(one);
2861 value_set_si(one, 1);
2862 int len = P->NbRays-1;
2863 Matrix *M = Matrix_Alloc(2 * len, P->Dimension+2);
2864 Vector_Copy(P->Ray[0], M->p[0], r * (P->Dimension+2));
2865 Vector_Copy(P->Ray[r+1], M->p[r], (len-r) * (P->Dimension+2));
2866 for (int j = 0; j < P->NbRays; ++j) {
2867 if (j == r)
2868 continue;
2869 Vector_Combine(P->Ray[j], P->Ray[r], M->p[len+j-(j>r)],
2870 one, P->Ray[j][P->Dimension+1], P->Dimension+2);
2873 P = Rays2Polyhedron(M, MaxRays);
2874 Matrix_Free(M);
2875 evalue *EP = barvinok_enumerate_e(P, exist, nparam, MaxRays);
2876 Polyhedron_Free(P);
2877 value_clear(one);
2879 return EP;
2882 static evalue* enumerate_redundant_ray(Polyhedron *P,
2883 unsigned exist, unsigned nparam, unsigned MaxRays)
2885 assert(P->NbBid == 0);
2886 int nvar = P->Dimension - exist - nparam;
2887 Value m;
2888 value_init(m);
2890 for (int r = 0; r < P->NbRays; ++r) {
2891 if (value_notzero_p(P->Ray[r][P->Dimension+1]))
2892 continue;
2893 int i1 = single_param_pos(P, exist, nparam, r);
2894 if (i1 == -1)
2895 continue;
2896 for (int r2 = r+1; r2 < P->NbRays; ++r2) {
2897 if (value_notzero_p(P->Ray[r2][P->Dimension+1]))
2898 continue;
2899 int i2 = single_param_pos(P, exist, nparam, r2);
2900 if (i2 == -1)
2901 continue;
2902 if (i1 != i2)
2903 continue;
2905 value_division(m, P->Ray[r][1+nvar+exist+i1],
2906 P->Ray[r2][1+nvar+exist+i1]);
2907 value_multiply(m, m, P->Ray[r2][1+nvar+exist+i1]);
2908 /* r2 divides r => r redundant */
2909 if (value_eq(m, P->Ray[r][1+nvar+exist+i1])) {
2910 value_clear(m);
2911 return enumerate_remove_ray(P, r, exist, nparam, MaxRays);
2914 value_division(m, P->Ray[r2][1+nvar+exist+i1],
2915 P->Ray[r][1+nvar+exist+i1]);
2916 value_multiply(m, m, P->Ray[r][1+nvar+exist+i1]);
2917 /* r divides r2 => r2 redundant */
2918 if (value_eq(m, P->Ray[r2][1+nvar+exist+i1])) {
2919 value_clear(m);
2920 return enumerate_remove_ray(P, r2, exist, nparam, MaxRays);
2924 value_clear(m);
2925 return 0;
2928 static Polyhedron *upper_bound(Polyhedron *P,
2929 int pos, Value *max, Polyhedron **R)
2931 Value v;
2932 int r;
2933 value_init(v);
2935 *R = 0;
2936 Polyhedron *N;
2937 Polyhedron *B = 0;
2938 for (Polyhedron *Q = P; Q; Q = N) {
2939 N = Q->next;
2940 for (r = 0; r < P->NbRays; ++r) {
2941 if (value_zero_p(P->Ray[r][P->Dimension+1]) &&
2942 value_pos_p(P->Ray[r][1+pos]))
2943 break;
2945 if (r < P->NbRays) {
2946 Q->next = *R;
2947 *R = Q;
2948 continue;
2949 } else {
2950 Q->next = B;
2951 B = Q;
2953 for (r = 0; r < P->NbRays; ++r) {
2954 if (value_zero_p(P->Ray[r][P->Dimension+1]))
2955 continue;
2956 mpz_fdiv_q(v, P->Ray[r][1+pos], P->Ray[r][1+P->Dimension]);
2957 if ((!Q->next && r == 0) || value_gt(v, *max))
2958 value_assign(*max, v);
2961 value_clear(v);
2962 return B;
2965 static evalue* enumerate_ray(Polyhedron *P,
2966 unsigned exist, unsigned nparam, unsigned MaxRays)
2968 assert(P->NbBid == 0);
2969 int nvar = P->Dimension - exist - nparam;
2971 int r;
2972 for (r = 0; r < P->NbRays; ++r)
2973 if (value_zero_p(P->Ray[r][P->Dimension+1]))
2974 break;
2975 if (r >= P->NbRays)
2976 return 0;
2978 int r2;
2979 for (r2 = r+1; r2 < P->NbRays; ++r2)
2980 if (value_zero_p(P->Ray[r2][P->Dimension+1]))
2981 break;
2982 if (r2 < P->NbRays) {
2983 if (nvar > 0)
2984 return enumerate_sum(P, exist, nparam, MaxRays);
2987 #ifdef DEBUG_ER
2988 fprintf(stderr, "\nER: Ray\n");
2989 #endif /* DEBUG_ER */
2991 Value m;
2992 Value one;
2993 value_init(m);
2994 value_init(one);
2995 value_set_si(one, 1);
2996 int i = single_param_pos(P, exist, nparam, r);
2997 assert(i != -1); // for now;
2999 Matrix *M = Matrix_Alloc(P->NbRays, P->Dimension+2);
3000 for (int j = 0; j < P->NbRays; ++j) {
3001 Vector_Combine(P->Ray[j], P->Ray[r], M->p[j],
3002 one, P->Ray[j][P->Dimension+1], P->Dimension+2);
3004 Polyhedron *S = Rays2Polyhedron(M, MaxRays);
3005 Matrix_Free(M);
3006 Polyhedron *D = DomainDifference(P, S, MaxRays);
3007 Polyhedron_Free(S);
3008 // Polyhedron_Print(stderr, P_VALUE_FMT, D);
3009 assert(value_pos_p(P->Ray[r][1+nvar+exist+i])); // for now
3010 Polyhedron *R;
3011 D = upper_bound(D, nvar+exist+i, &m, &R);
3012 assert(D);
3013 Domain_Free(D);
3015 M = Matrix_Alloc(2, P->Dimension+2);
3016 value_set_si(M->p[0][0], 1);
3017 value_set_si(M->p[1][0], 1);
3018 value_set_si(M->p[0][1+nvar+exist+i], -1);
3019 value_set_si(M->p[1][1+nvar+exist+i], 1);
3020 value_assign(M->p[0][1+P->Dimension], m);
3021 value_oppose(M->p[1][1+P->Dimension], m);
3022 value_addto(M->p[1][1+P->Dimension], M->p[1][1+P->Dimension],
3023 P->Ray[r][1+nvar+exist+i]);
3024 value_decrement(M->p[1][1+P->Dimension], M->p[1][1+P->Dimension]);
3025 // Matrix_Print(stderr, P_VALUE_FMT, M);
3026 D = AddConstraints(M->p[0], 2, P, MaxRays);
3027 // Polyhedron_Print(stderr, P_VALUE_FMT, D);
3028 value_substract(M->p[0][1+P->Dimension], M->p[0][1+P->Dimension],
3029 P->Ray[r][1+nvar+exist+i]);
3030 // Matrix_Print(stderr, P_VALUE_FMT, M);
3031 S = AddConstraints(M->p[0], 1, P, MaxRays);
3032 // Polyhedron_Print(stderr, P_VALUE_FMT, S);
3033 Matrix_Free(M);
3035 evalue *EP = barvinok_enumerate_e(D, exist, nparam, MaxRays);
3036 Polyhedron_Free(D);
3037 value_clear(one);
3038 value_clear(m);
3040 if (value_notone_p(P->Ray[r][1+nvar+exist+i]))
3041 EP = enumerate_cyclic(P, exist, nparam, EP, r, i, MaxRays);
3042 else {
3043 M = Matrix_Alloc(1, nparam+2);
3044 value_set_si(M->p[0][0], 1);
3045 value_set_si(M->p[0][1+i], 1);
3046 enumerate_vd_add_ray(EP, M, MaxRays);
3047 Matrix_Free(M);
3050 if (!emptyQ(S)) {
3051 evalue *E = barvinok_enumerate_e(S, exist, nparam, MaxRays);
3052 eadd(E, EP);
3053 free_evalue_refs(E);
3054 free(E);
3056 Polyhedron_Free(S);
3058 if (R) {
3059 assert(nvar == 0);
3060 evalue *ER = enumerate_or(R, exist, nparam, MaxRays);
3061 eor(ER, EP);
3062 free_evalue_refs(ER);
3063 free(ER);
3066 return EP;
3069 static evalue* new_zero_ep()
3071 evalue *EP;
3072 ALLOC(evalue, EP);
3073 value_init(EP->d);
3074 evalue_set_si(EP, 0, 1);
3075 return EP;
3078 static evalue* enumerate_vd(Polyhedron **PA,
3079 unsigned exist, unsigned nparam, unsigned MaxRays)
3081 Polyhedron *P = *PA;
3082 int nvar = P->Dimension - exist - nparam;
3083 Param_Polyhedron *PP = NULL;
3084 Polyhedron *C = Universe_Polyhedron(nparam);
3085 Polyhedron *CEq;
3086 Matrix *CT;
3087 Polyhedron *PR = P;
3088 PP = Polyhedron2Param_SimplifiedDomain(&PR,C,MaxRays,&CEq,&CT);
3089 Polyhedron_Free(C);
3091 int nd;
3092 Param_Domain *D, *last;
3093 Value c;
3094 value_init(c);
3095 for (nd = 0, D=PP->D; D; D=D->next, ++nd)
3098 Polyhedron **VD = new Polyhedron_p[nd];
3099 Polyhedron **fVD = new Polyhedron_p[nd];
3100 for(nd = 0, D=PP->D; D; D=D->next) {
3101 Polyhedron *rVD = reduce_domain(D->Domain, CT, CEq,
3102 fVD, nd, MaxRays);
3103 if (!rVD)
3104 continue;
3106 VD[nd++] = rVD;
3107 last = D;
3110 evalue *EP = 0;
3112 if (nd == 0)
3113 EP = new_zero_ep();
3115 /* This doesn't seem to have any effect */
3116 if (nd == 1) {
3117 Polyhedron *CA = align_context(VD[0], P->Dimension, MaxRays);
3118 Polyhedron *O = P;
3119 P = DomainIntersection(P, CA, MaxRays);
3120 if (O != *PA)
3121 Polyhedron_Free(O);
3122 Polyhedron_Free(CA);
3123 if (emptyQ(P))
3124 EP = new_zero_ep();
3127 if (!EP && CT->NbColumns != CT->NbRows) {
3128 Polyhedron *CEqr = DomainImage(CEq, CT, MaxRays);
3129 Polyhedron *CA = align_context(CEqr, PR->Dimension, MaxRays);
3130 Polyhedron *I = DomainIntersection(PR, CA, MaxRays);
3131 Polyhedron_Free(CEqr);
3132 Polyhedron_Free(CA);
3133 #ifdef DEBUG_ER
3134 fprintf(stderr, "\nER: Eliminate\n");
3135 #endif /* DEBUG_ER */
3136 nparam -= CT->NbColumns - CT->NbRows;
3137 EP = barvinok_enumerate_e(I, exist, nparam, MaxRays);
3138 nparam += CT->NbColumns - CT->NbRows;
3139 addeliminatedparams_enum(EP, CT, CEq, MaxRays, nparam);
3140 Polyhedron_Free(I);
3142 if (PR != *PA)
3143 Polyhedron_Free(PR);
3144 PR = 0;
3146 if (!EP && nd > 1) {
3147 #ifdef DEBUG_ER
3148 fprintf(stderr, "\nER: VD\n");
3149 #endif /* DEBUG_ER */
3150 for (int i = 0; i < nd; ++i) {
3151 Polyhedron *CA = align_context(VD[i], P->Dimension, MaxRays);
3152 Polyhedron *I = DomainIntersection(P, CA, MaxRays);
3154 if (i == 0)
3155 EP = barvinok_enumerate_e(I, exist, nparam, MaxRays);
3156 else {
3157 evalue *E = barvinok_enumerate_e(I, exist, nparam, MaxRays);
3158 eadd(E, EP);
3159 free_evalue_refs(E);
3160 free(E);
3162 Polyhedron_Free(I);
3163 Polyhedron_Free(CA);
3167 for (int i = 0; i < nd; ++i) {
3168 Polyhedron_Free(VD[i]);
3169 Polyhedron_Free(fVD[i]);
3171 delete [] VD;
3172 delete [] fVD;
3173 value_clear(c);
3175 if (!EP && nvar == 0) {
3176 Value f;
3177 value_init(f);
3178 Param_Vertices *V, *V2;
3179 Matrix* M = Matrix_Alloc(1, P->Dimension+2);
3181 FORALL_PVertex_in_ParamPolyhedron(V, last, PP) {
3182 bool found = false;
3183 FORALL_PVertex_in_ParamPolyhedron(V2, last, PP) {
3184 if (V == V2) {
3185 found = true;
3186 continue;
3188 if (!found)
3189 continue;
3190 for (int i = 0; i < exist; ++i) {
3191 value_oppose(f, V->Vertex->p[i][nparam+1]);
3192 Vector_Combine(V->Vertex->p[i],
3193 V2->Vertex->p[i],
3194 M->p[0] + 1 + nvar + exist,
3195 V2->Vertex->p[i][nparam+1],
3197 nparam+1);
3198 int j;
3199 for (j = 0; j < nparam; ++j)
3200 if (value_notzero_p(M->p[0][1+nvar+exist+j]))
3201 break;
3202 if (j >= nparam)
3203 continue;
3204 ConstraintSimplify(M->p[0], M->p[0],
3205 P->Dimension+2, &f);
3206 value_set_si(M->p[0][0], 0);
3207 Polyhedron *para = AddConstraints(M->p[0], 1, P,
3208 MaxRays);
3209 if (emptyQ(para)) {
3210 Polyhedron_Free(para);
3211 continue;
3213 Polyhedron *pos, *neg;
3214 value_set_si(M->p[0][0], 1);
3215 value_decrement(M->p[0][P->Dimension+1],
3216 M->p[0][P->Dimension+1]);
3217 neg = AddConstraints(M->p[0], 1, P, MaxRays);
3218 value_set_si(f, -1);
3219 Vector_Scale(M->p[0]+1, M->p[0]+1, f,
3220 P->Dimension+1);
3221 value_decrement(M->p[0][P->Dimension+1],
3222 M->p[0][P->Dimension+1]);
3223 value_decrement(M->p[0][P->Dimension+1],
3224 M->p[0][P->Dimension+1]);
3225 pos = AddConstraints(M->p[0], 1, P, MaxRays);
3226 if (emptyQ(neg) && emptyQ(pos)) {
3227 Polyhedron_Free(para);
3228 Polyhedron_Free(pos);
3229 Polyhedron_Free(neg);
3230 continue;
3232 #ifdef DEBUG_ER
3233 fprintf(stderr, "\nER: Order\n");
3234 #endif /* DEBUG_ER */
3235 EP = barvinok_enumerate_e(para, exist, nparam, MaxRays);
3236 evalue *E;
3237 if (!emptyQ(pos)) {
3238 E = barvinok_enumerate_e(pos, exist, nparam, MaxRays);
3239 eadd(E, EP);
3240 free_evalue_refs(E);
3241 free(E);
3243 if (!emptyQ(neg)) {
3244 E = barvinok_enumerate_e(neg, exist, nparam, MaxRays);
3245 eadd(E, EP);
3246 free_evalue_refs(E);
3247 free(E);
3249 Polyhedron_Free(para);
3250 Polyhedron_Free(pos);
3251 Polyhedron_Free(neg);
3252 break;
3254 if (EP)
3255 break;
3256 } END_FORALL_PVertex_in_ParamPolyhedron;
3257 if (EP)
3258 break;
3259 } END_FORALL_PVertex_in_ParamPolyhedron;
3261 if (!EP) {
3262 /* Search for vertex coordinate to split on */
3263 /* First look for one independent of the parameters */
3264 FORALL_PVertex_in_ParamPolyhedron(V, last, PP) {
3265 for (int i = 0; i < exist; ++i) {
3266 int j;
3267 for (j = 0; j < nparam; ++j)
3268 if (value_notzero_p(V->Vertex->p[i][j]))
3269 break;
3270 if (j < nparam)
3271 continue;
3272 value_set_si(M->p[0][0], 1);
3273 Vector_Set(M->p[0]+1, 0, nvar+exist);
3274 Vector_Copy(V->Vertex->p[i],
3275 M->p[0] + 1 + nvar + exist, nparam+1);
3276 value_oppose(M->p[0][1+nvar+i],
3277 V->Vertex->p[i][nparam+1]);
3279 Polyhedron *pos, *neg;
3280 value_set_si(M->p[0][0], 1);
3281 value_decrement(M->p[0][P->Dimension+1],
3282 M->p[0][P->Dimension+1]);
3283 neg = AddConstraints(M->p[0], 1, P, MaxRays);
3284 value_set_si(f, -1);
3285 Vector_Scale(M->p[0]+1, M->p[0]+1, f,
3286 P->Dimension+1);
3287 value_decrement(M->p[0][P->Dimension+1],
3288 M->p[0][P->Dimension+1]);
3289 value_decrement(M->p[0][P->Dimension+1],
3290 M->p[0][P->Dimension+1]);
3291 pos = AddConstraints(M->p[0], 1, P, MaxRays);
3292 if (emptyQ(neg) || emptyQ(pos)) {
3293 Polyhedron_Free(pos);
3294 Polyhedron_Free(neg);
3295 continue;
3297 Polyhedron_Free(pos);
3298 value_increment(M->p[0][P->Dimension+1],
3299 M->p[0][P->Dimension+1]);
3300 pos = AddConstraints(M->p[0], 1, P, MaxRays);
3301 #ifdef DEBUG_ER
3302 fprintf(stderr, "\nER: Vertex\n");
3303 #endif /* DEBUG_ER */
3304 pos->next = neg;
3305 EP = enumerate_or(pos, exist, nparam, MaxRays);
3306 break;
3308 if (EP)
3309 break;
3310 } END_FORALL_PVertex_in_ParamPolyhedron;
3313 if (!EP) {
3314 /* Search for vertex coordinate to split on */
3315 /* Now look for one that depends on the parameters */
3316 FORALL_PVertex_in_ParamPolyhedron(V, last, PP) {
3317 for (int i = 0; i < exist; ++i) {
3318 value_set_si(M->p[0][0], 1);
3319 Vector_Set(M->p[0]+1, 0, nvar+exist);
3320 Vector_Copy(V->Vertex->p[i],
3321 M->p[0] + 1 + nvar + exist, nparam+1);
3322 value_oppose(M->p[0][1+nvar+i],
3323 V->Vertex->p[i][nparam+1]);
3325 Polyhedron *pos, *neg;
3326 value_set_si(M->p[0][0], 1);
3327 value_decrement(M->p[0][P->Dimension+1],
3328 M->p[0][P->Dimension+1]);
3329 neg = AddConstraints(M->p[0], 1, P, MaxRays);
3330 value_set_si(f, -1);
3331 Vector_Scale(M->p[0]+1, M->p[0]+1, f,
3332 P->Dimension+1);
3333 value_decrement(M->p[0][P->Dimension+1],
3334 M->p[0][P->Dimension+1]);
3335 value_decrement(M->p[0][P->Dimension+1],
3336 M->p[0][P->Dimension+1]);
3337 pos = AddConstraints(M->p[0], 1, P, MaxRays);
3338 if (emptyQ(neg) || emptyQ(pos)) {
3339 Polyhedron_Free(pos);
3340 Polyhedron_Free(neg);
3341 continue;
3343 Polyhedron_Free(pos);
3344 value_increment(M->p[0][P->Dimension+1],
3345 M->p[0][P->Dimension+1]);
3346 pos = AddConstraints(M->p[0], 1, P, MaxRays);
3347 #ifdef DEBUG_ER
3348 fprintf(stderr, "\nER: ParamVertex\n");
3349 #endif /* DEBUG_ER */
3350 pos->next = neg;
3351 EP = enumerate_or(pos, exist, nparam, MaxRays);
3352 break;
3354 if (EP)
3355 break;
3356 } END_FORALL_PVertex_in_ParamPolyhedron;
3359 Matrix_Free(M);
3360 value_clear(f);
3363 if (CEq)
3364 Polyhedron_Free(CEq);
3365 if (CT)
3366 Matrix_Free(CT);
3367 if (PP)
3368 Param_Polyhedron_Free(PP);
3369 *PA = P;
3371 return EP;
3374 #ifndef HAVE_PIPLIB
3375 evalue *barvinok_enumerate_pip(Polyhedron *P,
3376 unsigned exist, unsigned nparam, unsigned MaxRays)
3378 return 0;
3380 #else
3381 evalue *barvinok_enumerate_pip(Polyhedron *P,
3382 unsigned exist, unsigned nparam, unsigned MaxRays)
3384 int nvar = P->Dimension - exist - nparam;
3385 evalue *EP = new_zero_ep();
3386 Polyhedron *Q, *N, *T = 0;
3387 Value min, tmp;
3388 value_init(min);
3389 value_init(tmp);
3391 #ifdef DEBUG_ER
3392 fprintf(stderr, "\nER: PIP\n");
3393 #endif /* DEBUG_ER */
3395 for (int i = 0; i < P->Dimension; ++i) {
3396 bool pos = false;
3397 bool neg = false;
3398 bool posray = false;
3399 bool negray = false;
3400 value_set_si(min, 0);
3401 for (int j = 0; j < P->NbRays; ++j) {
3402 if (value_pos_p(P->Ray[j][1+i])) {
3403 pos = true;
3404 if (value_zero_p(P->Ray[j][1+P->Dimension]))
3405 posray = true;
3406 } else if (value_neg_p(P->Ray[j][1+i])) {
3407 neg = true;
3408 if (value_zero_p(P->Ray[j][1+P->Dimension]))
3409 negray = true;
3410 else {
3411 mpz_fdiv_q(tmp,
3412 P->Ray[j][1+i], P->Ray[j][1+P->Dimension]);
3413 if (value_lt(tmp, min))
3414 value_assign(min, tmp);
3418 if (pos && neg) {
3419 assert(!(posray && negray));
3420 assert(!negray); // for now
3421 Polyhedron *O = T ? T : P;
3422 /* shift by a safe amount */
3423 Matrix *M = Matrix_Alloc(O->NbRays, O->Dimension+2);
3424 Vector_Copy(O->Ray[0], M->p[0], O->NbRays * (O->Dimension+2));
3425 for (int j = 0; j < P->NbRays; ++j) {
3426 if (value_notzero_p(M->p[j][1+P->Dimension])) {
3427 value_multiply(tmp, min, M->p[j][1+P->Dimension]);
3428 value_substract(M->p[j][1+i], M->p[j][1+i], tmp);
3431 if (T)
3432 Polyhedron_Free(T);
3433 T = Rays2Polyhedron(M, MaxRays);
3434 Matrix_Free(M);
3435 } else if (neg) {
3436 /* negating a parameter requires that we substitute in the
3437 * sign again afterwards.
3438 * Disallow for now.
3440 assert(i < nvar+exist);
3441 if (!T)
3442 T = Polyhedron_Copy(P);
3443 for (int j = 0; j < T->NbRays; ++j)
3444 value_oppose(T->Ray[j][1+i], T->Ray[j][1+i]);
3445 for (int j = 0; j < T->NbConstraints; ++j)
3446 value_oppose(T->Constraint[j][1+i], T->Constraint[j][1+i]);
3449 value_clear(min);
3450 value_clear(tmp);
3452 Polyhedron *D = pip_lexmin(T ? T : P, exist, nparam);
3453 for (Q = D; Q; Q = N) {
3454 N = Q->next;
3455 Q->next = 0;
3456 evalue *E;
3457 exist = Q->Dimension - nvar - nparam;
3458 E = barvinok_enumerate_e(Q, exist, nparam, MaxRays);
3459 Polyhedron_Free(Q);
3460 eadd(E, EP);
3461 free_evalue_refs(E);
3462 free(E);
3465 if (T)
3466 Polyhedron_Free(T);
3468 return EP;
3470 #endif
3473 static bool is_single(Value *row, int pos, int len)
3475 return First_Non_Zero(row, pos) == -1 &&
3476 First_Non_Zero(row+pos+1, len-pos-1) == -1;
3479 static evalue* barvinok_enumerate_e_r(Polyhedron *P,
3480 unsigned exist, unsigned nparam, unsigned MaxRays);
3482 #ifdef DEBUG_ER
3483 static int er_level = 0;
3485 evalue* barvinok_enumerate_e(Polyhedron *P,
3486 unsigned exist, unsigned nparam, unsigned MaxRays)
3488 fprintf(stderr, "\nER: level %i\n", er_level);
3489 int nvar = P->Dimension - exist - nparam;
3490 fprintf(stderr, "%d %d %d\n", nvar, exist, nparam);
3492 Polyhedron_Print(stderr, P_VALUE_FMT, P);
3493 ++er_level;
3494 P = DomainConstraintSimplify(Polyhedron_Copy(P), MaxRays);
3495 evalue *EP = barvinok_enumerate_e_r(P, exist, nparam, MaxRays);
3496 Polyhedron_Free(P);
3497 --er_level;
3498 return EP;
3500 #else
3501 evalue* barvinok_enumerate_e(Polyhedron *P,
3502 unsigned exist, unsigned nparam, unsigned MaxRays)
3504 P = DomainConstraintSimplify(Polyhedron_Copy(P), MaxRays);
3505 evalue *EP = barvinok_enumerate_e_r(P, exist, nparam, MaxRays);
3506 Polyhedron_Free(P);
3507 return EP;
3509 #endif
3511 static evalue* barvinok_enumerate_e_r(Polyhedron *P,
3512 unsigned exist, unsigned nparam, unsigned MaxRays)
3514 if (exist == 0) {
3515 Polyhedron *U = Universe_Polyhedron(nparam);
3516 evalue *EP = barvinok_enumerate_ev(P, U, MaxRays);
3517 //char *param_name[] = {"P", "Q", "R", "S", "T" };
3518 //print_evalue(stdout, EP, param_name);
3519 Polyhedron_Free(U);
3520 return EP;
3523 int nvar = P->Dimension - exist - nparam;
3524 int len = P->Dimension + 2;
3526 if (emptyQ(P))
3527 return new_zero_ep();
3529 if (nvar == 0 && nparam == 0) {
3530 evalue *EP = new_zero_ep();
3531 barvinok_count(P, &EP->x.n, MaxRays);
3532 if (value_pos_p(EP->x.n))
3533 value_set_si(EP->x.n, 1);
3534 return EP;
3537 int r;
3538 for (r = 0; r < P->NbRays; ++r)
3539 if (value_zero_p(P->Ray[r][0]) ||
3540 value_zero_p(P->Ray[r][P->Dimension+1])) {
3541 int i;
3542 for (i = 0; i < nvar; ++i)
3543 if (value_notzero_p(P->Ray[r][i+1]))
3544 break;
3545 if (i >= nvar)
3546 continue;
3547 for (i = nvar + exist; i < nvar + exist + nparam; ++i)
3548 if (value_notzero_p(P->Ray[r][i+1]))
3549 break;
3550 if (i >= nvar + exist + nparam)
3551 break;
3553 if (r < P->NbRays) {
3554 evalue *EP = new_zero_ep();
3555 value_set_si(EP->x.n, -1);
3556 return EP;
3559 int first;
3560 for (r = 0; r < P->NbEq; ++r)
3561 if ((first = First_Non_Zero(P->Constraint[r]+1+nvar, exist)) != -1)
3562 break;
3563 if (r < P->NbEq) {
3564 if (First_Non_Zero(P->Constraint[r]+1+nvar+first+1,
3565 exist-first-1) != -1) {
3566 Polyhedron *T = rotate_along(P, r, nvar, exist, MaxRays);
3567 #ifdef DEBUG_ER
3568 fprintf(stderr, "\nER: Equality\n");
3569 #endif /* DEBUG_ER */
3570 evalue *EP = barvinok_enumerate_e(T, exist-1, nparam, MaxRays);
3571 Polyhedron_Free(T);
3572 return EP;
3573 } else {
3574 #ifdef DEBUG_ER
3575 fprintf(stderr, "\nER: Fixed\n");
3576 #endif /* DEBUG_ER */
3577 if (first == 0)
3578 return barvinok_enumerate_e(P, exist-1, nparam, MaxRays);
3579 else {
3580 Polyhedron *T = Polyhedron_Copy(P);
3581 SwapColumns(T, nvar+1, nvar+1+first);
3582 evalue *EP = barvinok_enumerate_e(T, exist-1, nparam, MaxRays);
3583 Polyhedron_Free(T);
3584 return EP;
3589 Vector *row = Vector_Alloc(len);
3590 value_set_si(row->p[0], 1);
3592 Value f;
3593 value_init(f);
3595 enum constraint* info = new constraint[exist];
3596 for (int i = 0; i < exist; ++i) {
3597 info[i] = ALL_POS;
3598 for (int l = P->NbEq; l < P->NbConstraints; ++l) {
3599 if (value_negz_p(P->Constraint[l][nvar+i+1]))
3600 continue;
3601 bool l_parallel = is_single(P->Constraint[l]+nvar+1, i, exist);
3602 for (int u = P->NbEq; u < P->NbConstraints; ++u) {
3603 if (value_posz_p(P->Constraint[u][nvar+i+1]))
3604 continue;
3605 bool lu_parallel = l_parallel ||
3606 is_single(P->Constraint[u]+nvar+1, i, exist);
3607 value_oppose(f, P->Constraint[u][nvar+i+1]);
3608 Vector_Combine(P->Constraint[l]+1, P->Constraint[u]+1, row->p+1,
3609 f, P->Constraint[l][nvar+i+1], len-1);
3610 if (!(info[i] & INDEPENDENT)) {
3611 int j;
3612 for (j = 0; j < exist; ++j)
3613 if (j != i && value_notzero_p(row->p[nvar+j+1]))
3614 break;
3615 if (j == exist) {
3616 //printf("independent: i: %d, l: %d, u: %d\n", i, l, u);
3617 info[i] = (constraint)(info[i] | INDEPENDENT);
3620 if (info[i] & ALL_POS) {
3621 value_addto(row->p[len-1], row->p[len-1],
3622 P->Constraint[l][nvar+i+1]);
3623 value_addto(row->p[len-1], row->p[len-1], f);
3624 value_multiply(f, f, P->Constraint[l][nvar+i+1]);
3625 value_substract(row->p[len-1], row->p[len-1], f);
3626 value_decrement(row->p[len-1], row->p[len-1]);
3627 ConstraintSimplify(row->p, row->p, len, &f);
3628 value_set_si(f, -1);
3629 Vector_Scale(row->p+1, row->p+1, f, len-1);
3630 value_decrement(row->p[len-1], row->p[len-1]);
3631 Polyhedron *T = AddConstraints(row->p, 1, P, MaxRays);
3632 if (!emptyQ(T)) {
3633 //printf("not all_pos: i: %d, l: %d, u: %d\n", i, l, u);
3634 info[i] = (constraint)(info[i] ^ ALL_POS);
3636 //puts("pos remainder");
3637 //Polyhedron_Print(stdout, P_VALUE_FMT, T);
3638 Polyhedron_Free(T);
3640 if (!(info[i] & ONE_NEG)) {
3641 if (lu_parallel) {
3642 negative_test_constraint(P->Constraint[l],
3643 P->Constraint[u],
3644 row->p, nvar+i, len, &f);
3645 oppose_constraint(row->p, len, &f);
3646 Polyhedron *T = AddConstraints(row->p, 1, P, MaxRays);
3647 if (emptyQ(T)) {
3648 //printf("one_neg i: %d, l: %d, u: %d\n", i, l, u);
3649 info[i] = (constraint)(info[i] | ONE_NEG);
3651 //puts("neg remainder");
3652 //Polyhedron_Print(stdout, P_VALUE_FMT, T);
3653 Polyhedron_Free(T);
3654 } else if (!(info[i] & ROT_NEG)) {
3655 if (parallel_constraints(P->Constraint[l],
3656 P->Constraint[u],
3657 row->p, nvar, exist)) {
3658 negative_test_constraint7(P->Constraint[l],
3659 P->Constraint[u],
3660 row->p, nvar, exist,
3661 len, &f);
3662 oppose_constraint(row->p, len, &f);
3663 Polyhedron *T = AddConstraints(row->p, 1, P, MaxRays);
3664 if (emptyQ(T)) {
3665 // printf("rot_neg i: %d, l: %d, u: %d\n", i, l, u);
3666 info[i] = (constraint)(info[i] | ROT_NEG);
3667 r = l;
3669 //puts("neg remainder");
3670 //Polyhedron_Print(stdout, P_VALUE_FMT, T);
3671 Polyhedron_Free(T);
3675 if (!(info[i] & ALL_POS) && (info[i] & (ONE_NEG | ROT_NEG)))
3676 goto next;
3679 if (info[i] & ALL_POS)
3680 break;
3681 next:
3686 for (int i = 0; i < exist; ++i)
3687 printf("%i: %i\n", i, info[i]);
3689 for (int i = 0; i < exist; ++i)
3690 if (info[i] & ALL_POS) {
3691 #ifdef DEBUG_ER
3692 fprintf(stderr, "\nER: Positive\n");
3693 #endif /* DEBUG_ER */
3694 // Eliminate
3695 // Maybe we should chew off some of the fat here
3696 Matrix *M = Matrix_Alloc(P->Dimension, P->Dimension+1);
3697 for (int j = 0; j < P->Dimension; ++j)
3698 value_set_si(M->p[j][j + (j >= i+nvar)], 1);
3699 Polyhedron *T = Polyhedron_Image(P, M, MaxRays);
3700 Matrix_Free(M);
3701 evalue *EP = barvinok_enumerate_e(T, exist-1, nparam, MaxRays);
3702 Polyhedron_Free(T);
3703 value_clear(f);
3704 Vector_Free(row);
3705 delete [] info;
3706 return EP;
3708 for (int i = 0; i < exist; ++i)
3709 if (info[i] & ONE_NEG) {
3710 #ifdef DEBUG_ER
3711 fprintf(stderr, "\nER: Negative\n");
3712 #endif /* DEBUG_ER */
3713 Vector_Free(row);
3714 value_clear(f);
3715 delete [] info;
3716 if (i == 0)
3717 return barvinok_enumerate_e(P, exist-1, nparam, MaxRays);
3718 else {
3719 Polyhedron *T = Polyhedron_Copy(P);
3720 SwapColumns(T, nvar+1, nvar+1+i);
3721 evalue *EP = barvinok_enumerate_e(T, exist-1, nparam, MaxRays);
3722 Polyhedron_Free(T);
3723 return EP;
3726 for (int i = 0; i < exist; ++i)
3727 if (info[i] & ROT_NEG) {
3728 #ifdef DEBUG_ER
3729 fprintf(stderr, "\nER: Rotate\n");
3730 #endif /* DEBUG_ER */
3731 Vector_Free(row);
3732 value_clear(f);
3733 delete [] info;
3734 Polyhedron *T = rotate_along(P, r, nvar, exist, MaxRays);
3735 evalue *EP = barvinok_enumerate_e(T, exist-1, nparam, MaxRays);
3736 Polyhedron_Free(T);
3737 return EP;
3739 for (int i = 0; i < exist; ++i)
3740 if (info[i] & INDEPENDENT) {
3741 Polyhedron *pos, *neg;
3743 /* Find constraint again and split off negative part */
3745 if (SplitOnVar(P, i, nvar, len, exist, MaxRays,
3746 row, f, true, &pos, &neg)) {
3747 #ifdef DEBUG_ER
3748 fprintf(stderr, "\nER: Split\n");
3749 #endif /* DEBUG_ER */
3751 evalue *EP =
3752 barvinok_enumerate_e(neg, exist-1, nparam, MaxRays);
3753 evalue *E =
3754 barvinok_enumerate_e(pos, exist, nparam, MaxRays);
3755 eadd(E, EP);
3756 free_evalue_refs(E);
3757 free(E);
3758 Polyhedron_Free(neg);
3759 Polyhedron_Free(pos);
3760 value_clear(f);
3761 Vector_Free(row);
3762 delete [] info;
3763 return EP;
3766 delete [] info;
3768 Polyhedron *O = P;
3769 Polyhedron *F;
3771 evalue *EP;
3773 EP = enumerate_line(P, exist, nparam, MaxRays);
3774 if (EP)
3775 goto out;
3777 EP = barvinok_enumerate_pip(P, exist, nparam, MaxRays);
3778 if (EP)
3779 goto out;
3781 EP = enumerate_redundant_ray(P, exist, nparam, MaxRays);
3782 if (EP)
3783 goto out;
3785 EP = enumerate_sure(P, exist, nparam, MaxRays);
3786 if (EP)
3787 goto out;
3789 EP = enumerate_ray(P, exist, nparam, MaxRays);
3790 if (EP)
3791 goto out;
3793 EP = enumerate_sure2(P, exist, nparam, MaxRays);
3794 if (EP)
3795 goto out;
3797 F = unfringe(P, MaxRays);
3798 if (!PolyhedronIncludes(F, P)) {
3799 #ifdef DEBUG_ER
3800 fprintf(stderr, "\nER: Fringed\n");
3801 #endif /* DEBUG_ER */
3802 EP = barvinok_enumerate_e(F, exist, nparam, MaxRays);
3803 Polyhedron_Free(F);
3804 goto out;
3806 Polyhedron_Free(F);
3808 if (nparam)
3809 EP = enumerate_vd(&P, exist, nparam, MaxRays);
3810 if (EP)
3811 goto out2;
3813 if (nvar != 0) {
3814 EP = enumerate_sum(P, exist, nparam, MaxRays);
3815 goto out2;
3818 assert(nvar == 0);
3820 int i;
3821 Polyhedron *pos, *neg;
3822 for (i = 0; i < exist; ++i)
3823 if (SplitOnVar(P, i, nvar, len, exist, MaxRays,
3824 row, f, false, &pos, &neg))
3825 break;
3827 assert (i < exist);
3829 pos->next = neg;
3830 EP = enumerate_or(pos, exist, nparam, MaxRays);
3832 out2:
3833 if (O != P)
3834 Polyhedron_Free(P);
3836 out:
3837 value_clear(f);
3838 Vector_Free(row);
3839 return EP;
3842 gen_fun * barvinok_series(Polyhedron *P, Polyhedron* C, unsigned MaxRays)
3844 Polyhedron ** vcone;
3845 Polyhedron *CA;
3846 unsigned nparam = C->Dimension;
3847 unsigned dim, nvar;
3848 vec_ZZ sign;
3849 int ncone = 0;
3850 sign.SetLength(ncone);
3852 CA = align_context(C, P->Dimension, MaxRays);
3853 P = DomainIntersection(P, CA, MaxRays);
3854 Polyhedron_Free(CA);
3856 assert(!Polyhedron_is_infinite(P, nparam));
3857 assert(P->NbBid == 0);
3858 assert(Polyhedron_has_positive_rays(P, nparam));
3859 assert(P->NbEq == 0);
3861 dim = P->Dimension;
3862 nvar = dim - nparam;
3863 vcone = new Polyhedron_p[P->NbRays];
3865 for (int j = 0; j < P->NbRays; ++j) {
3866 if (!value_pos_p(P->Ray[j][dim+1]))
3867 continue;
3869 int npos, nneg;
3870 Polyhedron *C = supporting_cone(P, j);
3871 decompose(C, &vcone[j], &npos, &nneg, MaxRays);
3872 ncone += npos + nneg;
3873 sign.SetLength(ncone);
3874 for (int k = 0; k < npos; ++k)
3875 sign[ncone-nneg-k-1] = 1;
3876 for (int k = 0; k < nneg; ++k)
3877 sign[ncone-k-1] = -1;
3880 mat_ZZ rays;
3881 rays.SetDims(ncone * dim, nvar);
3882 int r = 0;
3883 for (int j = 0; j < P->NbRays; ++j) {
3884 if (!value_pos_p(P->Ray[j][dim+1]))
3885 continue;
3887 for (Polyhedron *i = vcone[j]; i; i = i->next) {
3888 add_rays(rays, i, &r, nvar);
3891 rays.SetDims(r, nvar);
3892 vec_ZZ lambda;
3893 nonorthog(rays, lambda);
3894 //randomvector(P, lambda, nvar);
3897 cout << "rays: " << rays;
3898 cout << "lambda: " << lambda;
3901 int f = 0;
3902 ZZ num_s;
3903 vec_ZZ num_p;
3904 num_p.SetLength(nparam);
3905 vec_ZZ vertex;
3906 vec_ZZ den_s;
3907 den_s.SetLength(dim);
3908 vec_ZZ den_p;
3909 den_p.SetLength(dim);
3910 mat_ZZ den;
3911 den.SetDims(dim, nparam);
3912 ZZ one;
3913 one = 1;
3914 mpq_t count;
3915 mpq_init(count);
3917 gen_fun * gf = new gen_fun;
3919 rays.SetDims(dim, nvar);
3921 for (int j = 0; j < P->NbRays; ++j) {
3922 if (!value_pos_p(P->Ray[j][dim+1]))
3923 continue;
3925 for (Polyhedron *i = vcone[j]; i; i = i->next, ++f) {
3926 lattice_point(P->Ray[j]+1, i, vertex);
3927 int k = 0;
3928 num_s = 0;
3929 for ( ; k < nvar; ++k)
3930 num_s += vertex[k] * lambda[k];
3931 for ( ; k < dim; ++k)
3932 num_p[k-nvar] = vertex[k];
3934 int r = 0;
3935 add_rays(rays, i, &r, nvar, true);
3936 for (r = 0; r < dim; ++r)
3937 values2zz(i->Ray[r]+1+nvar, den[r], nparam);
3938 den_s = rays * lambda;
3940 normalize(sign[f], num_s, num_p, den_s, den_p, den);
3942 int only_param = 0;
3943 int no_param = 0;
3944 for (int k = 0; k < dim; ++k) {
3945 if (den_p[k] == 0)
3946 ++no_param;
3947 else if (den_s[k] == 0)
3948 ++only_param;
3950 if (no_param == 0) {
3951 for (int k = 0; k < dim; ++k)
3952 if (den_p[k] == -1)
3953 den[k] = -den[k];
3954 gf->add(sign[f], one, num_p, den);
3955 } else if (no_param + only_param == dim) {
3956 int k, l;
3957 mat_ZZ pden;
3958 pden.SetDims(only_param, nparam);
3960 for (k = 0, l = 0; k < dim; ++k)
3961 if (den_p[k] != 0)
3962 pden[l++] = den[k];
3964 for (k = 0; k < dim; ++k)
3965 if (den_s[k] != 0)
3966 break;
3968 dpoly n(no_param, num_s);
3969 dpoly d(no_param, den_s[k], 1);
3970 for ( ; ++k < dim; k)
3971 if (den_s[k] != 0) {
3972 dpoly fact(no_param, den_s[k], 1);
3973 d *= fact;
3976 mpq_set_si(count, 0, 1);
3977 n.div(d, count, sign[f]);
3979 ZZ qn, qd;
3980 value2zz(mpq_numref(count), qn);
3981 value2zz(mpq_denref(count), qd);
3983 gf->add(qn, qd, num_p, pden);
3984 } else {
3985 int k, l;
3986 dpoly_r * r = 0;
3987 mat_ZZ pden;
3988 pden.SetDims(only_param, nparam);
3990 for (k = 0, l = 0; k < dim; ++k)
3991 if (den_s[k] == 0)
3992 pden[l++] = den[k];
3994 for (k = 0; k < dim; ++k)
3995 if (den_p[k] == 0)
3996 break;
3998 dpoly n(no_param, num_s);
3999 dpoly d(no_param, den_s[k], 1);
4000 for ( ; ++k < dim; )
4001 if (den_p[k] == 0) {
4002 dpoly fact(no_param, den_s[k], 1);
4003 d *= fact;
4006 for (k = 0; k < dim; ++k) {
4007 if (den_s[k] == 0 || den_p[k] == 0)
4008 continue;
4010 dpoly pd(no_param-1, den_s[k], 1);
4011 int s = den_p[k] < 0 ? -1 : 1;
4013 if (r == 0)
4014 r = new dpoly_r(n, pd, k, s, dim);
4015 else
4016 assert(0); // for now
4019 r->div(d, sign[f], gf, pden, den, num_p);
4023 cout << "sign: " << sign[f];
4024 cout << "num_s: " << num_s;
4025 cout << "num_p: " << num_p;
4026 cout << "den_s: " << den_s;
4027 cout << "den_p: " << den_p;
4028 cout << "den: " << den;
4029 cout << "only_param: " << only_param;
4030 cout << "no_param: " << no_param;
4031 cout << endl;
4037 mpq_clear(count);
4039 return gf;