bump version
[barvinok.git] / barvinok.cc
blobfafdaecad95fe21feeb3abbe5f67888bbc434780
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 static 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);
183 Value v;
184 value_init(v);
185 zz2value(det, v);
186 value_clear(v);
189 Vector* short_vector(vec_ZZ& lambda) {
190 Matrix *M = Matrix_Copy(Rays);
191 Matrix *inv = Matrix_Alloc(M->NbRows, M->NbColumns);
192 int ok = Matrix_Inverse(M, inv);
193 assert(ok);
194 Matrix_Free(M);
196 ZZ det2;
197 mat_ZZ B;
198 mat_ZZ U;
199 matrix2zz(inv, B, inv->NbRows - 1, inv->NbColumns - 1);
200 long r = LLL(det2, B, U);
202 ZZ min = max(B[0]);
203 int index = 0;
204 for (int i = 1; i < B.NumRows(); ++i) {
205 ZZ tmp = max(B[i]);
206 if (tmp < min) {
207 min = tmp;
208 index = i;
212 Matrix_Free(inv);
214 lambda = B[index];
216 Vector *z = Vector_Alloc(U[index].length()+1);
217 assert(z);
218 zz2values(U[index], z->p);
219 value_set_si(z->p[U[index].length()], 0);
221 Value tmp;
222 value_init(tmp);
223 Polyhedron *C = poly();
224 int i;
225 for (i = 0; i < C->NbConstraints; ++i) {
226 Inner_Product(z->p, C->Constraint[i]+1, z->Size-1, &tmp);
227 if (value_pos_p(tmp))
228 break;
230 if (i == C->NbConstraints) {
231 value_set_si(tmp, -1);
232 Vector_Scale(z->p, z->p, tmp, z->Size-1);
234 value_clear(tmp);
235 return z;
238 ~cone() {
239 Polyhedron_Free(Cone);
240 Matrix_Free(Rays);
243 Polyhedron *poly() {
244 if (!Cone) {
245 Matrix *M = Matrix_Alloc(Rays->NbRows+1, Rays->NbColumns+1);
246 for (int i = 0; i < Rays->NbRows; ++i) {
247 Vector_Copy(Rays->p[i], M->p[i]+1, Rays->NbColumns);
248 value_set_si(M->p[i][0], 1);
250 Vector_Set(M->p[Rays->NbRows]+1, 0, Rays->NbColumns-1);
251 value_set_si(M->p[Rays->NbRows][0], 1);
252 value_set_si(M->p[Rays->NbRows][Rays->NbColumns], 1);
253 Cone = Rays2Polyhedron(M, M->NbRows+1);
254 assert(Cone->NbConstraints == Cone->NbRays);
255 Matrix_Free(M);
257 return Cone;
260 ZZ det;
261 Polyhedron *Cone;
262 Matrix *Rays;
265 class dpoly {
266 public:
267 vec_ZZ coeff;
268 dpoly(int d, ZZ& degree, int offset = 0) {
269 coeff.SetLength(d+1);
271 int min = d + offset;
272 if (degree >= 0 && degree < ZZ(INIT_VAL, min))
273 min = to_int(degree);
275 ZZ c = ZZ(INIT_VAL, 1);
276 if (!offset)
277 coeff[0] = c;
278 for (int i = 1; i <= min; ++i) {
279 c *= (degree -i + 1);
280 c /= i;
281 coeff[i-offset] = c;
284 void operator *= (dpoly& f) {
285 assert(coeff.length() == f.coeff.length());
286 vec_ZZ old = coeff;
287 coeff = f.coeff[0] * coeff;
288 for (int i = 1; i < coeff.length(); ++i)
289 for (int j = 0; i+j < coeff.length(); ++j)
290 coeff[i+j] += f.coeff[i] * old[j];
292 void div(dpoly& d, mpq_t count, ZZ& sign) {
293 int len = coeff.length();
294 Value tmp;
295 value_init(tmp);
296 mpq_t* c = new mpq_t[coeff.length()];
297 mpq_t qtmp;
298 mpq_init(qtmp);
299 for (int i = 0; i < len; ++i) {
300 mpq_init(c[i]);
301 zz2value(coeff[i], tmp);
302 mpq_set_z(c[i], tmp);
304 for (int j = 1; j <= i; ++j) {
305 zz2value(d.coeff[j], tmp);
306 mpq_set_z(qtmp, tmp);
307 mpq_mul(qtmp, qtmp, c[i-j]);
308 mpq_sub(c[i], c[i], qtmp);
311 zz2value(d.coeff[0], tmp);
312 mpq_set_z(qtmp, tmp);
313 mpq_div(c[i], c[i], qtmp);
315 if (sign == -1)
316 mpq_sub(count, count, c[len-1]);
317 else
318 mpq_add(count, count, c[len-1]);
320 value_clear(tmp);
321 mpq_clear(qtmp);
322 for (int i = 0; i < len; ++i)
323 mpq_clear(c[i]);
324 delete [] c;
328 class dpoly_n {
329 public:
330 Matrix *coeff;
331 ~dpoly_n() {
332 Matrix_Free(coeff);
334 dpoly_n(int d, ZZ& degree_0, ZZ& degree_1, int offset = 0) {
335 Value d0, d1;
336 value_init(d0);
337 value_init(d1);
338 zz2value(degree_0, d0);
339 zz2value(degree_1, d1);
340 coeff = Matrix_Alloc(d+1, d+1+1);
341 value_set_si(coeff->p[0][0], 1);
342 value_set_si(coeff->p[0][d+1], 1);
343 for (int i = 1; i <= d; ++i) {
344 value_multiply(coeff->p[i][0], coeff->p[i-1][0], d0);
345 Vector_Combine(coeff->p[i-1], coeff->p[i-1]+1, coeff->p[i]+1,
346 d1, d0, i);
347 value_set_si(coeff->p[i][d+1], i);
348 value_multiply(coeff->p[i][d+1], coeff->p[i][d+1], coeff->p[i-1][d+1]);
349 value_decrement(d0, d0);
351 value_clear(d0);
352 value_clear(d1);
354 void div(dpoly& d, Vector *count, ZZ& sign) {
355 int len = coeff->NbRows;
356 Matrix * c = Matrix_Alloc(coeff->NbRows, coeff->NbColumns);
357 Value tmp;
358 value_init(tmp);
359 for (int i = 0; i < len; ++i) {
360 Vector_Copy(coeff->p[i], c->p[i], len+1);
361 for (int j = 1; j <= i; ++j) {
362 zz2value(d.coeff[j], tmp);
363 value_multiply(tmp, tmp, c->p[i][len]);
364 value_oppose(tmp, tmp);
365 Vector_Combine(c->p[i], c->p[i-j], c->p[i],
366 c->p[i-j][len], tmp, len);
367 value_multiply(c->p[i][len], c->p[i][len], c->p[i-j][len]);
369 zz2value(d.coeff[0], tmp);
370 value_multiply(c->p[i][len], c->p[i][len], tmp);
372 if (sign == -1) {
373 value_set_si(tmp, -1);
374 Vector_Scale(c->p[len-1], count->p, tmp, len);
375 value_assign(count->p[len], c->p[len-1][len]);
376 } else
377 Vector_Copy(c->p[len-1], count->p, len+1);
378 Vector_Normalize(count->p, len+1);
379 value_clear(tmp);
380 Matrix_Free(c);
384 struct dpoly_r_term {
385 int *powers;
386 ZZ coeff;
389 struct dpoly_r {
390 vector< dpoly_r_term * > *c;
391 int len;
392 int dim;
394 void add_term(int i, int * powers, ZZ& coeff) {
395 for (int k = 0; k < c[i].size(); ++k) {
396 if (memcmp(c[i][k]->powers, powers, dim * sizeof(int)) == 0) {
397 c[i][k]->coeff += coeff;
398 return;
401 dpoly_r_term *t = new dpoly_r_term;
402 t->powers = new int[dim];
403 memcpy(t->powers, powers, dim * sizeof(int));
404 t->coeff = coeff;
405 c[i].push_back(t);
407 dpoly_r(int len, int dim) {
408 this->len = len;
409 this->dim = dim;
410 c = new vector< dpoly_r_term * > [len];
412 dpoly_r(dpoly& num, dpoly& den, int pos, int sign, int dim) {
413 len = num.coeff.length();
414 c = new vector< dpoly_r_term * > [len];
415 this->dim = dim;
416 int powers[dim];
418 for (int i = 0; i < len; ++i) {
419 ZZ coeff = num.coeff[i];
420 memset(powers, 0, dim * sizeof(int));
421 powers[pos] = sign;
423 add_term(i, powers, coeff);
425 for (int j = 1; j <= i; ++j) {
426 for (int k = 0; k < c[i-j].size(); ++k) {
427 memcpy(powers, c[i-j][k]->powers, dim*sizeof(int));
428 powers[pos] += sign;
429 coeff = -den.coeff[j-1] * c[i-j][k]->coeff;
430 add_term(i, powers, coeff);
434 //dump();
436 void div(dpoly& d, ZZ& sign, gen_fun *gf, mat_ZZ& pden, mat_ZZ& den,
437 vec_ZZ& num_p) {
438 dpoly_r rc(len, dim);
439 ZZ max_d = power(d.coeff[0], len+1);
440 ZZ cur_d = max_d;
441 ZZ coeff;
443 for (int i = 0; i < len; ++i) {
444 cur_d /= d.coeff[0];
446 for (int k = 0; k < c[i].size(); ++k) {
447 coeff = c[i][k]->coeff * cur_d;
448 rc.add_term(i, c[i][k]->powers, coeff);
451 for (int j = 1; j <= i; ++j) {
452 for (int k = 0; k < rc.c[i-j].size(); ++k) {
453 coeff = - d.coeff[j] * rc.c[i-j][k]->coeff / d.coeff[0];
454 rc.add_term(i, rc.c[i-j][k]->powers, coeff);
458 //rc.dump();
459 int common = pden.NumRows();
461 vector< dpoly_r_term * >& final = rc.c[len-1];
462 int rows;
463 for (int j = 0; j < final.size(); ++j) {
464 rows = common;
465 pden.SetDims(rows, pden.NumCols());
466 for (int k = 0; k < dim; ++k) {
467 int n = final[j]->powers[k];
468 if (n == 0)
469 continue;
470 int abs_n = n < 0 ? -n : n;
471 pden.SetDims(rows+abs_n, pden.NumCols());
472 for (int l = 0; l < abs_n; ++l) {
473 if (n > 0)
474 pden[rows+l] = den[k];
475 else
476 pden[rows+l] = -den[k];
478 rows += abs_n;
480 gf->add(final[j]->coeff, max_d, num_p, pden);
483 void dump(void) {
484 for (int i = 0; i < len; ++i) {
485 cout << endl;
486 cout << i << endl;
487 cout << c[i].size() << endl;
488 for (int j = 0; j < c[i].size(); ++j) {
489 for (int k = 0; k < dim; ++k) {
490 cout << c[i][j]->powers[k] << " ";
492 cout << ": " << c[i][j]->coeff << endl;
494 cout << endl;
499 struct decomposer {
500 void decompose(Polyhedron *C);
501 virtual void handle(Polyhedron *P, int sign) = 0;
504 struct polar_decomposer : public decomposer {
505 void decompose(Polyhedron *C, unsigned MaxRays);
506 virtual void handle(Polyhedron *P, int sign);
507 virtual void handle_polar(Polyhedron *P, int sign) = 0;
510 void decomposer::decompose(Polyhedron *C)
512 vector<cone *> nonuni;
513 cone * c = new cone(C);
514 ZZ det = c->det;
515 int s = sign(det);
516 assert(det != 0);
517 if (abs(det) > 1) {
518 nonuni.push_back(c);
519 } else {
520 handle(C, 1);
521 delete c;
523 vec_ZZ lambda;
524 while (!nonuni.empty()) {
525 c = nonuni.back();
526 nonuni.pop_back();
527 Vector* v = c->short_vector(lambda);
528 for (int i = 0; i < c->Rays->NbRows - 1; ++i) {
529 if (lambda[i] == 0)
530 continue;
531 Matrix* M = Matrix_Copy(c->Rays);
532 Vector_Copy(v->p, M->p[i], v->Size);
533 cone * pc = new cone(M);
534 assert (pc->det != 0);
535 if (abs(pc->det) > 1) {
536 assert(abs(pc->det) < abs(c->det));
537 nonuni.push_back(pc);
538 } else {
539 handle(pc->poly(), sign(pc->det) * s);
540 delete pc;
542 Matrix_Free(M);
544 Vector_Free(v);
545 delete c;
549 void polar_decomposer::decompose(Polyhedron *cone, unsigned MaxRays)
551 Polyhedron_Polarize(cone);
552 if (cone->NbRays - 1 != cone->Dimension) {
553 Polyhedron *tmp = cone;
554 cone = triangularize_cone(cone, MaxRays);
555 Polyhedron_Free(tmp);
557 for (Polyhedron *Polar = cone; Polar; Polar = Polar->next)
558 decomposer::decompose(Polar);
559 Domain_Free(cone);
562 void polar_decomposer::handle(Polyhedron *P, int sign)
564 Polyhedron_Polarize(P);
565 handle_polar(P, sign);
569 * Barvinok's Decomposition of a simplicial cone
571 * Returns two lists of polyhedra
573 void barvinok_decompose(Polyhedron *C, Polyhedron **ppos, Polyhedron **pneg)
575 Polyhedron *pos = *ppos, *neg = *pneg;
576 vector<cone *> nonuni;
577 cone * c = new cone(C);
578 ZZ det = c->det;
579 int s = sign(det);
580 assert(det != 0);
581 if (abs(det) > 1) {
582 nonuni.push_back(c);
583 } else {
584 Polyhedron *p = Polyhedron_Copy(c->Cone);
585 p->next = pos;
586 pos = p;
587 delete c;
589 vec_ZZ lambda;
590 while (!nonuni.empty()) {
591 c = nonuni.back();
592 nonuni.pop_back();
593 Vector* v = c->short_vector(lambda);
594 for (int i = 0; i < c->Rays->NbRows - 1; ++i) {
595 if (lambda[i] == 0)
596 continue;
597 Matrix* M = Matrix_Copy(c->Rays);
598 Vector_Copy(v->p, M->p[i], v->Size);
599 cone * pc = new cone(M);
600 assert (pc->det != 0);
601 if (abs(pc->det) > 1) {
602 assert(abs(pc->det) < abs(c->det));
603 nonuni.push_back(pc);
604 } else {
605 Polyhedron *p = pc->poly();
606 pc->Cone = 0;
607 if (sign(pc->det) == s) {
608 p->next = pos;
609 pos = p;
610 } else {
611 p->next = neg;
612 neg = p;
614 delete pc;
616 Matrix_Free(M);
618 Vector_Free(v);
619 delete c;
621 *ppos = pos;
622 *pneg = neg;
626 * Returns a single list of npos "positive" cones followed by nneg
627 * "negative" cones.
628 * The input cone is freed
630 void decompose(Polyhedron *cone, Polyhedron **parts, int *npos, int *nneg, unsigned MaxRays)
632 Polyhedron_Polarize(cone);
633 if (cone->NbRays - 1 != cone->Dimension) {
634 Polyhedron *tmp = cone;
635 cone = triangularize_cone(cone, MaxRays);
636 Polyhedron_Free(tmp);
638 Polyhedron *polpos = NULL, *polneg = NULL;
639 *npos = 0; *nneg = 0;
640 for (Polyhedron *Polar = cone; Polar; Polar = Polar->next)
641 barvinok_decompose(Polar, &polpos, &polneg);
643 Polyhedron *last;
644 for (Polyhedron *i = polpos; i; i = i->next) {
645 Polyhedron_Polarize(i);
646 ++*npos;
647 last = i;
649 for (Polyhedron *i = polneg; i; i = i->next) {
650 Polyhedron_Polarize(i);
651 ++*nneg;
653 if (last) {
654 last->next = polneg;
655 *parts = polpos;
656 } else
657 *parts = polneg;
658 Domain_Free(cone);
661 const int MAX_TRY=10;
663 * Searches for a vector that is not orthogonal to any
664 * of the rays in rays.
666 static void nonorthog(mat_ZZ& rays, vec_ZZ& lambda)
668 int dim = rays.NumCols();
669 bool found = false;
670 lambda.SetLength(dim);
671 if (dim == 0)
672 return;
674 for (int i = 2; !found && i <= 50*dim; i+=4) {
675 for (int j = 0; j < MAX_TRY; ++j) {
676 for (int k = 0; k < dim; ++k) {
677 int r = random_int(i)+2;
678 int v = (2*(r%2)-1) * (r >> 1);
679 lambda[k] = v;
681 int k = 0;
682 for (; k < rays.NumRows(); ++k)
683 if (lambda * rays[k] == 0)
684 break;
685 if (k == rays.NumRows()) {
686 found = true;
687 break;
691 assert(found);
694 static void randomvector(Polyhedron *P, vec_ZZ& lambda, int nvar)
696 Value tmp;
697 int max = 10;
698 unsigned int dim = P->Dimension;
699 value_init(tmp);
701 for (int i = 0; i < P->NbRays; ++i) {
702 for (int j = 1; j <= dim; ++j) {
703 value_absolute(tmp, P->Ray[i][j]);
704 int t = VALUE_TO_LONG(tmp);
705 if (t > max)
706 max = t;
709 for (int i = 0; i < P->NbConstraints; ++i) {
710 for (int j = 1; j <= dim; ++j) {
711 value_absolute(tmp, P->Constraint[i][j]);
712 int t = VALUE_TO_LONG(tmp);
713 if (t > max)
714 max = t;
717 value_clear(tmp);
719 lambda.SetLength(nvar);
720 for (int k = 0; k < nvar; ++k) {
721 int r = random_int(8*max*dim)+2;
722 int v = (2*(r%2)-1) * (4*max*dim + (r >> 1));
723 lambda[k] = v;
727 static void add_rays(mat_ZZ& rays, Polyhedron *i, int *r, int nvar = -1,
728 bool all = false)
730 unsigned dim = i->Dimension;
731 if (nvar == -1)
732 nvar = dim;
733 for (int k = 0; k < i->NbRays; ++k) {
734 if (!value_zero_p(i->Ray[k][dim+1]))
735 continue;
736 if (!all && nvar != dim && First_Non_Zero(i->Ray[k]+1, nvar) == -1)
737 continue;
738 values2zz(i->Ray[k]+1, rays[(*r)++], nvar);
742 void lattice_point(Value* values, Polyhedron *i, vec_ZZ& vertex)
744 unsigned dim = i->Dimension;
745 if(!value_one_p(values[dim])) {
746 Matrix* Rays = rays(i);
747 Matrix *inv = Matrix_Alloc(Rays->NbRows, Rays->NbColumns);
748 int ok = Matrix_Inverse(Rays, inv);
749 assert(ok);
750 Matrix_Free(Rays);
751 Rays = rays(i);
752 Vector *lambda = Vector_Alloc(dim+1);
753 Vector_Matrix_Product(values, inv, lambda->p);
754 Matrix_Free(inv);
755 for (int j = 0; j < dim; ++j)
756 mpz_cdiv_q(lambda->p[j], lambda->p[j], lambda->p[dim]);
757 value_set_si(lambda->p[dim], 1);
758 Vector *A = Vector_Alloc(dim+1);
759 Vector_Matrix_Product(lambda->p, Rays, A->p);
760 Vector_Free(lambda);
761 Matrix_Free(Rays);
762 values2zz(A->p, vertex, dim);
763 Vector_Free(A);
764 } else
765 values2zz(values, vertex, dim);
768 static evalue *term(int param, ZZ& c, Value *den = NULL)
770 evalue *EP = new evalue();
771 value_init(EP->d);
772 value_set_si(EP->d,0);
773 EP->x.p = new_enode(polynomial, 2, param + 1);
774 evalue_set_si(&EP->x.p->arr[0], 0, 1);
775 value_init(EP->x.p->arr[1].x.n);
776 if (den == NULL)
777 value_set_si(EP->x.p->arr[1].d, 1);
778 else
779 value_assign(EP->x.p->arr[1].d, *den);
780 zz2value(c, EP->x.p->arr[1].x.n);
781 return EP;
784 static void vertex_period(
785 Polyhedron *i, vec_ZZ& lambda, Matrix *T,
786 Value lcm, int p, Vector *val,
787 evalue *E, evalue* ev,
788 ZZ& offset)
790 unsigned nparam = T->NbRows - 1;
791 unsigned dim = i->Dimension;
792 Value tmp;
793 ZZ nump;
795 if (p == nparam) {
796 vec_ZZ vertex;
797 ZZ num, l;
798 Vector * values = Vector_Alloc(dim + 1);
799 Vector_Matrix_Product(val->p, T, values->p);
800 value_assign(values->p[dim], lcm);
801 lattice_point(values->p, i, vertex);
802 num = vertex * lambda;
803 value2zz(lcm, l);
804 num *= l;
805 num += offset;
806 value_init(ev->x.n);
807 zz2value(num, ev->x.n);
808 value_assign(ev->d, lcm);
809 Vector_Free(values);
810 return;
813 value_init(tmp);
814 vec_ZZ vertex;
815 values2zz(T->p[p], vertex, dim);
816 nump = vertex * lambda;
817 if (First_Non_Zero(val->p, p) == -1) {
818 value_assign(tmp, lcm);
819 evalue *ET = term(p, nump, &tmp);
820 eadd(ET, E);
821 free_evalue_refs(ET);
822 delete ET;
825 value_assign(tmp, lcm);
826 if (First_Non_Zero(T->p[p], dim) != -1)
827 Vector_Gcd(T->p[p], dim, &tmp);
828 Gcd(tmp, lcm, &tmp);
829 if (value_lt(tmp, lcm)) {
830 ZZ count;
832 value_division(tmp, lcm, tmp);
833 value_set_si(ev->d, 0);
834 ev->x.p = new_enode(periodic, VALUE_TO_INT(tmp), p+1);
835 value2zz(tmp, count);
836 do {
837 value_decrement(tmp, tmp);
838 --count;
839 ZZ new_offset = offset - count * nump;
840 value_assign(val->p[p], tmp);
841 vertex_period(i, lambda, T, lcm, p+1, val, E,
842 &ev->x.p->arr[VALUE_TO_INT(tmp)], new_offset);
843 } while (value_pos_p(tmp));
844 } else
845 vertex_period(i, lambda, T, lcm, p+1, val, E, ev, offset);
846 value_clear(tmp);
849 static void mask_r(Matrix *f, int nr, Vector *lcm, int p, Vector *val, evalue *ev)
851 unsigned nparam = lcm->Size;
853 if (p == nparam) {
854 Vector * prod = Vector_Alloc(f->NbRows);
855 Matrix_Vector_Product(f, val->p, prod->p);
856 int isint = 1;
857 for (int i = 0; i < nr; ++i) {
858 value_modulus(prod->p[i], prod->p[i], f->p[i][nparam+1]);
859 isint &= value_zero_p(prod->p[i]);
861 value_set_si(ev->d, 1);
862 value_init(ev->x.n);
863 value_set_si(ev->x.n, isint);
864 Vector_Free(prod);
865 return;
868 Value tmp;
869 value_init(tmp);
870 if (value_one_p(lcm->p[p]))
871 mask_r(f, nr, lcm, p+1, val, ev);
872 else {
873 value_assign(tmp, lcm->p[p]);
874 value_set_si(ev->d, 0);
875 ev->x.p = new_enode(periodic, VALUE_TO_INT(tmp), p+1);
876 do {
877 value_decrement(tmp, tmp);
878 value_assign(val->p[p], tmp);
879 mask_r(f, nr, lcm, p+1, val, &ev->x.p->arr[VALUE_TO_INT(tmp)]);
880 } while (value_pos_p(tmp));
882 value_clear(tmp);
885 static evalue *multi_monom(vec_ZZ& p)
887 evalue *X = new evalue();
888 value_init(X->d);
889 value_init(X->x.n);
890 unsigned nparam = p.length()-1;
891 zz2value(p[nparam], X->x.n);
892 value_set_si(X->d, 1);
893 for (int i = 0; i < nparam; ++i) {
894 if (p[i] == 0)
895 continue;
896 evalue *T = term(i, p[i]);
897 eadd(T, X);
898 free_evalue_refs(T);
899 delete T;
901 return X;
905 * Check whether mapping polyhedron P on the affine combination
906 * num yields a range that has a fixed quotient on integer
907 * division by d
908 * If zero is true, then we are only interested in the quotient
909 * for the cases where the remainder is zero.
910 * Returns NULL if false and a newly allocated value if true.
912 static Value *fixed_quotient(Polyhedron *P, vec_ZZ& num, Value d, bool zero)
914 Value* ret = NULL;
915 int len = num.length();
916 Matrix *T = Matrix_Alloc(2, len);
917 zz2values(num, T->p[0]);
918 value_set_si(T->p[1][len-1], 1);
919 Polyhedron *I = Polyhedron_Image(P, T, P->NbConstraints);
920 Matrix_Free(T);
922 int i;
923 for (i = 0; i < I->NbRays; ++i)
924 if (value_zero_p(I->Ray[i][2])) {
925 Polyhedron_Free(I);
926 return NULL;
929 Value min, max;
930 value_init(min);
931 value_init(max);
932 int bounded = line_minmax(I, &min, &max);
933 assert(bounded);
935 if (zero)
936 mpz_cdiv_q(min, min, d);
937 else
938 mpz_fdiv_q(min, min, d);
939 mpz_fdiv_q(max, max, d);
941 if (value_eq(min, max)) {
942 ALLOC(Value, ret);
943 value_init(*ret);
944 value_assign(*ret, min);
946 value_clear(min);
947 value_clear(max);
948 return ret;
952 * Normalize linear expression coef modulo m
953 * Removes common factor and reduces coefficients
954 * Returns index of first non-zero coefficient or len
956 static int normal_mod(Value *coef, int len, Value *m)
958 Value gcd;
959 value_init(gcd);
961 Vector_Gcd(coef, len, &gcd);
962 Gcd(gcd, *m, &gcd);
963 Vector_AntiScale(coef, coef, gcd, len);
965 value_division(*m, *m, gcd);
966 value_clear(gcd);
968 if (value_one_p(*m))
969 return len;
971 int j;
972 for (j = 0; j < len; ++j)
973 mpz_fdiv_r(coef[j], coef[j], *m);
974 for (j = 0; j < len; ++j)
975 if (value_notzero_p(coef[j]))
976 break;
978 return j;
981 #ifdef USE_MODULO
982 static void mask(Matrix *f, evalue *factor)
984 int nr = f->NbRows, nc = f->NbColumns;
985 int n;
986 bool found = false;
987 for (n = 0; n < nr && value_notzero_p(f->p[n][nc-1]); ++n)
988 if (value_notone_p(f->p[n][nc-1]) &&
989 value_notmone_p(f->p[n][nc-1]))
990 found = true;
991 if (!found)
992 return;
994 evalue EP;
995 nr = n;
997 Value m;
998 value_init(m);
1000 evalue EV;
1001 value_init(EV.d);
1002 value_init(EV.x.n);
1003 value_set_si(EV.x.n, 1);
1005 for (n = 0; n < nr; ++n) {
1006 value_assign(m, f->p[n][nc-1]);
1007 if (value_one_p(m) || value_mone_p(m))
1008 continue;
1010 int j = normal_mod(f->p[n], nc-1, &m);
1011 if (j == nc-1) {
1012 free_evalue_refs(factor);
1013 value_init(factor->d);
1014 evalue_set_si(factor, 0, 1);
1015 break;
1017 vec_ZZ row;
1018 values2zz(f->p[n], row, nc-1);
1019 ZZ g;
1020 value2zz(m, g);
1021 if (j < (nc-1)-1 && row[j] > g/2) {
1022 for (int k = j; k < (nc-1); ++k)
1023 if (row[k] != 0)
1024 row[k] = g - row[k];
1027 value_init(EP.d);
1028 value_set_si(EP.d, 0);
1029 EP.x.p = new_enode(relation, 2, 0);
1030 value_clear(EP.x.p->arr[1].d);
1031 EP.x.p->arr[1] = *factor;
1032 evalue *ev = &EP.x.p->arr[0];
1033 value_set_si(ev->d, 0);
1034 ev->x.p = new_enode(fractional, 3, -1);
1035 evalue_set_si(&ev->x.p->arr[1], 0, 1);
1036 evalue_set_si(&ev->x.p->arr[2], 1, 1);
1037 evalue *E = multi_monom(row);
1038 value_assign(EV.d, m);
1039 emul(&EV, E);
1040 value_clear(ev->x.p->arr[0].d);
1041 ev->x.p->arr[0] = *E;
1042 delete E;
1043 *factor = EP;
1046 value_clear(m);
1047 free_evalue_refs(&EV);
1049 #else
1053 static void mask(Matrix *f, evalue *factor)
1055 int nr = f->NbRows, nc = f->NbColumns;
1056 int n;
1057 bool found = false;
1058 for (n = 0; n < nr && value_notzero_p(f->p[n][nc-1]); ++n)
1059 if (value_notone_p(f->p[n][nc-1]) &&
1060 value_notmone_p(f->p[n][nc-1]))
1061 found = true;
1062 if (!found)
1063 return;
1065 Value tmp;
1066 value_init(tmp);
1067 nr = n;
1068 unsigned np = nc - 2;
1069 Vector *lcm = Vector_Alloc(np);
1070 Vector *val = Vector_Alloc(nc);
1071 Vector_Set(val->p, 0, nc);
1072 value_set_si(val->p[np], 1);
1073 Vector_Set(lcm->p, 1, np);
1074 for (n = 0; n < nr; ++n) {
1075 if (value_one_p(f->p[n][nc-1]) ||
1076 value_mone_p(f->p[n][nc-1]))
1077 continue;
1078 for (int j = 0; j < np; ++j)
1079 if (value_notzero_p(f->p[n][j])) {
1080 Gcd(f->p[n][j], f->p[n][nc-1], &tmp);
1081 value_division(tmp, f->p[n][nc-1], tmp);
1082 value_lcm(tmp, lcm->p[j], &lcm->p[j]);
1085 evalue EP;
1086 value_init(EP.d);
1087 mask_r(f, nr, lcm, 0, val, &EP);
1088 value_clear(tmp);
1089 Vector_Free(val);
1090 Vector_Free(lcm);
1091 emul(&EP,factor);
1092 free_evalue_refs(&EP);
1094 #endif
1096 struct term_info {
1097 evalue *E;
1098 ZZ constant;
1099 ZZ coeff;
1100 int pos;
1103 static bool mod_needed(Polyhedron *PD, vec_ZZ& num, Value d, evalue *E)
1105 Value *q = fixed_quotient(PD, num, d, false);
1107 if (!q)
1108 return true;
1110 value_oppose(*q, *q);
1111 evalue EV;
1112 value_init(EV.d);
1113 value_set_si(EV.d, 1);
1114 value_init(EV.x.n);
1115 value_multiply(EV.x.n, *q, d);
1116 eadd(&EV, E);
1117 free_evalue_refs(&EV);
1118 value_clear(*q);
1119 free(q);
1120 return false;
1123 static void ceil_mod(Value *coef, int len, Value d, ZZ& f, evalue *EP, Polyhedron *PD)
1125 Value m;
1126 value_init(m);
1127 value_set_si(m, -1);
1129 Vector_Scale(coef, coef, m, len);
1131 value_assign(m, d);
1132 int j = normal_mod(coef, len, &m);
1134 if (j == len) {
1135 value_clear(m);
1136 return;
1139 vec_ZZ num;
1140 values2zz(coef, num, len);
1142 ZZ g;
1143 value2zz(m, g);
1145 evalue tmp;
1146 value_init(tmp.d);
1147 evalue_set_si(&tmp, 0, 1);
1149 int p = j;
1150 if (g % 2 == 0)
1151 while (j < len-1 && (num[j] == g/2 || num[j] == 0))
1152 ++j;
1153 if ((j < len-1 && num[j] > g/2) || (j == len-1 && num[j] >= (g+1)/2)) {
1154 for (int k = j; k < len-1; ++k)
1155 if (num[k] != 0)
1156 num[k] = g - num[k];
1157 num[len-1] = g - 1 - num[len-1];
1158 value_assign(tmp.d, m);
1159 ZZ t = f*(g-1);
1160 zz2value(t, tmp.x.n);
1161 eadd(&tmp, EP);
1162 f = -f;
1165 if (p >= len-1) {
1166 ZZ t = num[len-1] * f;
1167 zz2value(t, tmp.x.n);
1168 value_assign(tmp.d, m);
1169 eadd(&tmp, EP);
1170 } else {
1171 evalue *E = multi_monom(num);
1172 evalue EV;
1173 value_init(EV.d);
1175 if (PD && !mod_needed(PD, num, m, E)) {
1176 value_init(EV.x.n);
1177 zz2value(f, EV.x.n);
1178 value_assign(EV.d, m);
1179 emul(&EV, E);
1180 eadd(E, EP);
1181 } else {
1182 value_init(EV.x.n);
1183 value_set_si(EV.x.n, 1);
1184 value_assign(EV.d, m);
1185 emul(&EV, E);
1186 value_clear(EV.x.n);
1187 value_set_si(EV.d, 0);
1188 EV.x.p = new_enode(fractional, 3, -1);
1189 evalue_copy(&EV.x.p->arr[0], E);
1190 evalue_set_si(&EV.x.p->arr[1], 0, 1);
1191 value_init(EV.x.p->arr[2].x.n);
1192 zz2value(f, EV.x.p->arr[2].x.n);
1193 value_set_si(EV.x.p->arr[2].d, 1);
1195 eadd(&EV, EP);
1198 free_evalue_refs(&EV);
1199 free_evalue_refs(E);
1200 delete E;
1203 free_evalue_refs(&tmp);
1205 out:
1206 value_clear(m);
1209 evalue* bv_ceil3(Value *coef, int len, Value d, Polyhedron *P)
1211 Vector *val = Vector_Alloc(len);
1213 Value t;
1214 value_init(t);
1215 value_set_si(t, -1);
1216 Vector_Scale(coef, val->p, t, len);
1217 value_absolute(t, d);
1219 vec_ZZ num;
1220 values2zz(val->p, num, len);
1221 evalue *EP = multi_monom(num);
1223 evalue tmp;
1224 value_init(tmp.d);
1225 value_init(tmp.x.n);
1226 value_set_si(tmp.x.n, 1);
1227 value_assign(tmp.d, t);
1229 emul(&tmp, EP);
1231 ZZ one;
1232 one = 1;
1233 ceil_mod(val->p, len, t, one, EP, P);
1234 value_clear(t);
1236 /* copy EP to malloc'ed evalue */
1237 evalue *E;
1238 ALLOC(evalue, E);
1239 *E = *EP;
1240 delete EP;
1242 free_evalue_refs(&tmp);
1243 Vector_Free(val);
1245 return E;
1248 #ifdef USE_MODULO
1249 evalue* lattice_point(
1250 Polyhedron *i, vec_ZZ& lambda, Matrix *W, Value lcm, Polyhedron *PD)
1252 unsigned nparam = W->NbColumns - 1;
1254 Matrix* Rays = rays2(i);
1255 Matrix *T = Transpose(Rays);
1256 Matrix *T2 = Matrix_Copy(T);
1257 Matrix *inv = Matrix_Alloc(T2->NbRows, T2->NbColumns);
1258 int ok = Matrix_Inverse(T2, inv);
1259 assert(ok);
1260 Matrix_Free(Rays);
1261 Matrix_Free(T2);
1262 mat_ZZ vertex;
1263 matrix2zz(W, vertex, W->NbRows, W->NbColumns);
1265 vec_ZZ num;
1266 num = lambda * vertex;
1268 evalue *EP = multi_monom(num);
1270 evalue tmp;
1271 value_init(tmp.d);
1272 value_init(tmp.x.n);
1273 value_set_si(tmp.x.n, 1);
1274 value_assign(tmp.d, lcm);
1276 emul(&tmp, EP);
1278 Matrix *L = Matrix_Alloc(inv->NbRows, W->NbColumns);
1279 Matrix_Product(inv, W, L);
1281 mat_ZZ RT;
1282 matrix2zz(T, RT, T->NbRows, T->NbColumns);
1283 Matrix_Free(T);
1285 vec_ZZ p = lambda * RT;
1287 for (int i = 0; i < L->NbRows; ++i) {
1288 ceil_mod(L->p[i], nparam+1, lcm, p[i], EP, PD);
1291 Matrix_Free(L);
1293 Matrix_Free(inv);
1294 free_evalue_refs(&tmp);
1295 return EP;
1297 #else
1298 evalue* lattice_point(
1299 Polyhedron *i, vec_ZZ& lambda, Matrix *W, Value lcm, Polyhedron *PD)
1301 Matrix *T = Transpose(W);
1302 unsigned nparam = T->NbRows - 1;
1304 evalue *EP = new evalue();
1305 value_init(EP->d);
1306 evalue_set_si(EP, 0, 1);
1308 evalue ev;
1309 Vector *val = Vector_Alloc(nparam+1);
1310 value_set_si(val->p[nparam], 1);
1311 ZZ offset(INIT_VAL, 0);
1312 value_init(ev.d);
1313 vertex_period(i, lambda, T, lcm, 0, val, EP, &ev, offset);
1314 Vector_Free(val);
1315 eadd(&ev, EP);
1316 free_evalue_refs(&ev);
1318 Matrix_Free(T);
1320 reduce_evalue(EP);
1322 return EP;
1324 #endif
1326 void lattice_point(
1327 Param_Vertices* V, Polyhedron *i, vec_ZZ& lambda, term_info* term,
1328 Polyhedron *PD)
1330 unsigned nparam = V->Vertex->NbColumns - 2;
1331 unsigned dim = i->Dimension;
1332 mat_ZZ vertex;
1333 vertex.SetDims(V->Vertex->NbRows, nparam+1);
1334 Value lcm, tmp;
1335 value_init(lcm);
1336 value_init(tmp);
1337 value_set_si(lcm, 1);
1338 for (int j = 0; j < V->Vertex->NbRows; ++j) {
1339 value_lcm(lcm, V->Vertex->p[j][nparam+1], &lcm);
1341 if (value_notone_p(lcm)) {
1342 Matrix * mv = Matrix_Alloc(dim, nparam+1);
1343 for (int j = 0 ; j < dim; ++j) {
1344 value_division(tmp, lcm, V->Vertex->p[j][nparam+1]);
1345 Vector_Scale(V->Vertex->p[j], mv->p[j], tmp, nparam+1);
1348 term->E = lattice_point(i, lambda, mv, lcm, PD);
1349 term->constant = 0;
1351 Matrix_Free(mv);
1352 value_clear(lcm);
1353 value_clear(tmp);
1354 return;
1356 for (int i = 0; i < V->Vertex->NbRows; ++i) {
1357 assert(value_one_p(V->Vertex->p[i][nparam+1])); // for now
1358 values2zz(V->Vertex->p[i], vertex[i], nparam+1);
1361 vec_ZZ num;
1362 num = lambda * vertex;
1364 int p = -1;
1365 int nn = 0;
1366 for (int j = 0; j < nparam; ++j)
1367 if (num[j] != 0) {
1368 ++nn;
1369 p = j;
1371 if (nn >= 2) {
1372 term->E = multi_monom(num);
1373 term->constant = 0;
1374 } else {
1375 term->E = NULL;
1376 term->constant = num[nparam];
1377 term->pos = p;
1378 if (p != -1)
1379 term->coeff = num[p];
1382 value_clear(lcm);
1383 value_clear(tmp);
1386 void normalize(Polyhedron *i, vec_ZZ& lambda, ZZ& sign, ZZ& num, vec_ZZ& den)
1388 unsigned dim = i->Dimension;
1390 int r = 0;
1391 mat_ZZ rays;
1392 rays.SetDims(dim, dim);
1393 add_rays(rays, i, &r);
1394 den = rays * lambda;
1395 int change = 0;
1397 for (int j = 0; j < den.length(); ++j) {
1398 if (den[j] > 0)
1399 change ^= 1;
1400 else {
1401 den[j] = abs(den[j]);
1402 num += den[j];
1405 if (change)
1406 sign = -sign;
1409 struct counter : public polar_decomposer {
1410 vec_ZZ lambda;
1411 mat_ZZ rays;
1412 vec_ZZ vertex;
1413 vec_ZZ den;
1414 ZZ sign;
1415 ZZ num;
1416 int j;
1417 Polyhedron *P;
1418 unsigned dim;
1419 mpq_t count;
1421 counter(Polyhedron *P) {
1422 this->P = P;
1423 dim = P->Dimension;
1424 randomvector(P, lambda, dim);
1425 rays.SetDims(dim, dim);
1426 den.SetLength(dim);
1427 mpq_init(count);
1430 void start(unsigned MaxRays);
1432 ~counter() {
1433 mpq_clear(count);
1436 virtual void handle_polar(Polyhedron *P, int sign);
1439 void counter::handle_polar(Polyhedron *C, int s)
1441 int r = 0;
1442 assert(C->NbRays-1 == dim);
1443 add_rays(rays, C, &r);
1444 for (int k = 0; k < dim; ++k) {
1445 assert(lambda * rays[k] != 0);
1448 sign = s;
1450 lattice_point(P->Ray[j]+1, C, vertex);
1451 num = vertex * lambda;
1452 normalize(C, lambda, sign, num, den);
1454 dpoly d(dim, num);
1455 dpoly n(dim, den[0], 1);
1456 for (int k = 1; k < dim; ++k) {
1457 dpoly fact(dim, den[k], 1);
1458 n *= fact;
1460 d.div(n, count, sign);
1463 void counter::start(unsigned MaxRays)
1465 for (j = 0; j < P->NbRays; ++j) {
1466 Polyhedron *C = supporting_cone(P, j);
1467 decompose(C, MaxRays);
1471 typedef Polyhedron * Polyhedron_p;
1473 void barvinok_count(Polyhedron *P, Value* result, unsigned NbMaxCons)
1475 Polyhedron ** vcone;
1476 ZZ sign;
1477 unsigned dim;
1478 int allocated = 0;
1479 Value factor;
1480 Polyhedron *Q;
1481 int r = 0;
1483 if (emptyQ(P)) {
1484 value_set_si(*result, 0);
1485 return;
1487 if (P->NbBid == 0)
1488 for (; r < P->NbRays; ++r)
1489 if (value_zero_p(P->Ray[r][P->Dimension+1]))
1490 break;
1491 if (P->NbBid !=0 || r < P->NbRays) {
1492 value_set_si(*result, -1);
1493 return;
1495 if (P->NbEq != 0) {
1496 P = remove_equalities(P);
1497 if (emptyQ(P)) {
1498 Polyhedron_Free(P);
1499 value_set_si(*result, 0);
1500 return;
1502 allocated = 1;
1504 value_init(factor);
1505 value_set_si(factor, 1);
1506 Q = Polyhedron_Reduce(P, &factor);
1507 if (Q) {
1508 if (allocated)
1509 Polyhedron_Free(P);
1510 P = Q;
1511 allocated = 1;
1513 if (P->Dimension == 0) {
1514 value_assign(*result, factor);
1515 if (allocated)
1516 Polyhedron_Free(P);
1517 value_clear(factor);
1518 return;
1521 counter cnt(P);
1522 cnt.start(NbMaxCons);
1524 assert(value_one_p(&cnt.count[0]._mp_den));
1525 value_multiply(*result, &cnt.count[0]._mp_num, factor);
1527 if (allocated)
1528 Polyhedron_Free(P);
1529 value_clear(factor);
1532 static void uni_polynom(int param, Vector *c, evalue *EP)
1534 unsigned dim = c->Size-2;
1535 value_init(EP->d);
1536 value_set_si(EP->d,0);
1537 EP->x.p = new_enode(polynomial, dim+1, param+1);
1538 for (int j = 0; j <= dim; ++j)
1539 evalue_set(&EP->x.p->arr[j], c->p[j], c->p[dim+1]);
1542 static void multi_polynom(Vector *c, evalue* X, evalue *EP)
1544 unsigned dim = c->Size-2;
1545 evalue EC;
1547 value_init(EC.d);
1548 evalue_set(&EC, c->p[dim], c->p[dim+1]);
1550 value_init(EP->d);
1551 evalue_set(EP, c->p[dim], c->p[dim+1]);
1553 for (int i = dim-1; i >= 0; --i) {
1554 emul(X, EP);
1555 value_assign(EC.x.n, c->p[i]);
1556 eadd(&EC, EP);
1558 free_evalue_refs(&EC);
1561 Polyhedron *unfringe (Polyhedron *P, unsigned MaxRays)
1563 int len = P->Dimension+2;
1564 Polyhedron *T, *R = P;
1565 Value g;
1566 value_init(g);
1567 Vector *row = Vector_Alloc(len);
1568 value_set_si(row->p[0], 1);
1570 R = DomainConstraintSimplify(Polyhedron_Copy(P), MaxRays);
1572 Matrix *M = Matrix_Alloc(2, len-1);
1573 value_set_si(M->p[1][len-2], 1);
1574 for (int v = 0; v < P->Dimension; ++v) {
1575 value_set_si(M->p[0][v], 1);
1576 Polyhedron *I = Polyhedron_Image(P, M, 2+1);
1577 value_set_si(M->p[0][v], 0);
1578 for (int r = 0; r < I->NbConstraints; ++r) {
1579 if (value_zero_p(I->Constraint[r][0]))
1580 continue;
1581 if (value_zero_p(I->Constraint[r][1]))
1582 continue;
1583 if (value_one_p(I->Constraint[r][1]))
1584 continue;
1585 if (value_mone_p(I->Constraint[r][1]))
1586 continue;
1587 value_absolute(g, I->Constraint[r][1]);
1588 Vector_Set(row->p+1, 0, len-2);
1589 value_division(row->p[1+v], I->Constraint[r][1], g);
1590 mpz_fdiv_q(row->p[len-1], I->Constraint[r][2], g);
1591 T = R;
1592 R = AddConstraints(row->p, 1, R, MaxRays);
1593 if (T != P)
1594 Polyhedron_Free(T);
1596 Polyhedron_Free(I);
1598 Matrix_Free(M);
1599 Vector_Free(row);
1600 value_clear(g);
1601 return R;
1604 static Polyhedron *reduce_domain(Polyhedron *D, Matrix *CT, Polyhedron *CEq,
1605 Polyhedron **fVD, int nd, unsigned MaxRays)
1607 assert(CEq);
1609 Polyhedron *Dt;
1610 Dt = CT ? DomainPreimage(D, CT, MaxRays) : D;
1611 Polyhedron *rVD = DomainIntersection(Dt, CEq, MaxRays);
1613 /* if rVD is empty or too small in geometric dimension */
1614 if(!rVD || emptyQ(rVD) ||
1615 (rVD->Dimension-rVD->NbEq < Dt->Dimension-Dt->NbEq-CEq->NbEq)) {
1616 if(rVD)
1617 Domain_Free(rVD);
1618 if (CT)
1619 Domain_Free(Dt);
1620 return 0; /* empty validity domain */
1623 if (CT)
1624 Domain_Free(Dt);
1626 fVD[nd] = Domain_Copy(rVD);
1627 for (int i = 0 ; i < nd; ++i) {
1628 Polyhedron *I = DomainIntersection(fVD[nd], fVD[i], MaxRays);
1629 if (emptyQ(I)) {
1630 Domain_Free(I);
1631 continue;
1633 Polyhedron *F = DomainSimplify(I, fVD[nd], MaxRays);
1634 if (F->NbEq == 1) {
1635 Polyhedron *T = rVD;
1636 rVD = DomainDifference(rVD, F, MaxRays);
1637 Domain_Free(T);
1639 Domain_Free(F);
1640 Domain_Free(I);
1643 rVD = DomainConstraintSimplify(rVD, MaxRays);
1644 if (emptyQ(rVD)) {
1645 Domain_Free(fVD[nd]);
1646 Domain_Free(rVD);
1647 return 0;
1650 Value c;
1651 value_init(c);
1652 barvinok_count(rVD, &c, MaxRays);
1653 if (value_zero_p(c)) {
1654 Domain_Free(rVD);
1655 rVD = 0;
1657 value_clear(c);
1659 return rVD;
1662 static bool Polyhedron_is_infinite(Polyhedron *P, unsigned nparam)
1664 int r;
1665 for (r = 0; r < P->NbRays; ++r)
1666 if (value_zero_p(P->Ray[r][0]) ||
1667 value_zero_p(P->Ray[r][P->Dimension+1])) {
1668 int i;
1669 for (i = P->Dimension - nparam; i < P->Dimension; ++i)
1670 if (value_notzero_p(P->Ray[r][i+1]))
1671 break;
1672 if (i >= P->Dimension)
1673 break;
1675 return r < P->NbRays;
1678 /* Check whether all rays point in the positive directions
1679 * for the parameters
1681 static bool Polyhedron_has_positive_rays(Polyhedron *P, unsigned nparam)
1683 int r;
1684 for (r = 0; r < P->NbRays; ++r)
1685 if (value_zero_p(P->Ray[r][P->Dimension+1])) {
1686 int i;
1687 for (i = P->Dimension - nparam; i < P->Dimension; ++i)
1688 if (value_neg_p(P->Ray[r][i+1]))
1689 return false;
1691 return true;
1694 typedef evalue * evalue_p;
1696 struct enumerator : public polar_decomposer {
1697 vec_ZZ lambda;
1698 unsigned dim, nbV;
1699 evalue ** vE;
1700 int _i;
1701 mat_ZZ rays;
1702 vec_ZZ den;
1703 ZZ sign;
1704 Polyhedron *P;
1705 Param_Vertices *V;
1706 term_info num;
1707 Vector *c;
1708 mpq_t count;
1710 enumerator(Polyhedron *P, unsigned dim, unsigned nbV) {
1711 this->P = P;
1712 this->dim = dim;
1713 this->nbV = nbV;
1714 randomvector(P, lambda, dim);
1715 rays.SetDims(dim, dim);
1716 den.SetLength(dim);
1717 c = Vector_Alloc(dim+2);
1719 vE = new evalue_p[nbV];
1720 for (int j = 0; j < nbV; ++j)
1721 vE[j] = 0;
1723 mpq_init(count);
1726 void decompose_at(Param_Vertices *V, int _i, unsigned MaxRays) {
1727 Polyhedron *C = supporting_cone_p(P, V);
1728 this->_i = _i;
1729 this->V = V;
1731 vE[_i] = new evalue;
1732 value_init(vE[_i]->d);
1733 evalue_set_si(vE[_i], 0, 1);
1735 decompose(C, MaxRays);
1738 ~enumerator() {
1739 mpq_clear(count);
1740 Vector_Free(c);
1742 for (int j = 0; j < nbV; ++j)
1743 if (vE[j]) {
1744 free_evalue_refs(vE[j]);
1745 delete vE[j];
1747 delete [] vE;
1750 virtual void handle_polar(Polyhedron *P, int sign);
1753 void enumerator::handle_polar(Polyhedron *C, int s)
1755 int r = 0;
1756 assert(C->NbRays-1 == dim);
1757 add_rays(rays, C, &r);
1758 for (int k = 0; k < dim; ++k) {
1759 assert(lambda * rays[k] != 0);
1762 sign = s;
1764 lattice_point(V, C, lambda, &num, 0);
1765 normalize(C, lambda, sign, num.constant, den);
1767 dpoly n(dim, den[0], 1);
1768 for (int k = 1; k < dim; ++k) {
1769 dpoly fact(dim, den[k], 1);
1770 n *= fact;
1772 if (num.E != NULL) {
1773 ZZ one(INIT_VAL, 1);
1774 dpoly_n d(dim, num.constant, one);
1775 d.div(n, c, sign);
1776 evalue EV;
1777 multi_polynom(c, num.E, &EV);
1778 eadd(&EV , vE[_i]);
1779 free_evalue_refs(&EV);
1780 free_evalue_refs(num.E);
1781 delete num.E;
1782 } else if (num.pos != -1) {
1783 dpoly_n d(dim, num.constant, num.coeff);
1784 d.div(n, c, sign);
1785 evalue EV;
1786 uni_polynom(num.pos, c, &EV);
1787 eadd(&EV , vE[_i]);
1788 free_evalue_refs(&EV);
1789 } else {
1790 mpq_set_si(count, 0, 1);
1791 dpoly d(dim, num.constant);
1792 d.div(n, count, sign);
1793 evalue EV;
1794 value_init(EV.d);
1795 evalue_set(&EV, &count[0]._mp_num, &count[0]._mp_den);
1796 eadd(&EV , vE[_i]);
1797 free_evalue_refs(&EV);
1801 evalue* barvinok_enumerate_ev(Polyhedron *P, Polyhedron* C, unsigned MaxRays)
1803 //P = unfringe(P, MaxRays);
1804 Polyhedron *CEq = NULL, *rVD, *pVD, *CA;
1805 Matrix *CT = NULL;
1806 Param_Polyhedron *PP = NULL;
1807 Param_Domain *D, *next;
1808 Param_Vertices *V;
1809 int r = 0;
1810 unsigned nparam = C->Dimension;
1811 evalue *eres;
1812 ALLOC(evalue, eres);
1813 value_init(eres->d);
1814 value_set_si(eres->d, 0);
1816 evalue factor;
1817 value_init(factor.d);
1818 evalue_set_si(&factor, 1, 1);
1820 CA = align_context(C, P->Dimension, MaxRays);
1821 P = DomainIntersection(P, CA, MaxRays);
1822 Polyhedron_Free(CA);
1824 if (C->Dimension == 0 || emptyQ(P)) {
1825 constant:
1826 eres->x.p = new_enode(partition, 2, C->Dimension);
1827 EVALUE_SET_DOMAIN(eres->x.p->arr[0],
1828 DomainConstraintSimplify(CEq ? CEq : Polyhedron_Copy(C), MaxRays));
1829 value_set_si(eres->x.p->arr[1].d, 1);
1830 value_init(eres->x.p->arr[1].x.n);
1831 if (emptyQ(P))
1832 value_set_si(eres->x.p->arr[1].x.n, 0);
1833 else
1834 barvinok_count(P, &eres->x.p->arr[1].x.n, MaxRays);
1835 out:
1836 emul(&factor, eres);
1837 reduce_evalue(eres);
1838 free_evalue_refs(&factor);
1839 Polyhedron_Free(P);
1840 if (CT)
1841 Matrix_Free(CT);
1842 if (PP)
1843 Param_Polyhedron_Free(PP);
1845 return eres;
1847 if (Polyhedron_is_infinite(P, nparam))
1848 goto constant;
1850 if (P->NbEq != 0) {
1851 Matrix *f;
1852 P = remove_equalities_p(P, P->Dimension-nparam, &f);
1853 mask(f, &factor);
1854 Matrix_Free(f);
1856 if (P->Dimension == nparam) {
1857 CEq = P;
1858 P = Universe_Polyhedron(0);
1859 goto constant;
1862 Polyhedron *Q = ParamPolyhedron_Reduce(P, P->Dimension-nparam, &factor);
1863 if (Q) {
1864 Polyhedron_Free(P);
1865 if (Q->Dimension == nparam) {
1866 CEq = Q;
1867 P = Universe_Polyhedron(0);
1868 goto constant;
1870 P = Q;
1872 Polyhedron *oldP = P;
1873 PP = Polyhedron2Param_SimplifiedDomain(&P,C,MaxRays,&CEq,&CT);
1874 if (P != oldP)
1875 Polyhedron_Free(oldP);
1877 if (isIdentity(CT)) {
1878 Matrix_Free(CT);
1879 CT = NULL;
1880 } else {
1881 assert(CT->NbRows != CT->NbColumns);
1882 if (CT->NbRows == 1) // no more parameters
1883 goto constant;
1884 nparam = CT->NbRows - 1;
1887 unsigned dim = P->Dimension - nparam;
1889 enumerator et(P, dim, PP->nbV);
1891 int nd;
1892 for (nd = 0, D=PP->D; D; ++nd, D=D->next);
1893 struct section { Polyhedron *D; evalue E; };
1894 section *s = new section[nd];
1895 Polyhedron **fVD = new Polyhedron_p[nd];
1897 for(nd = 0, D=PP->D; D; D=next) {
1898 next = D->next;
1900 Polyhedron *rVD = reduce_domain(D->Domain, CT, CEq,
1901 fVD, nd, MaxRays);
1902 if (!rVD)
1903 continue;
1905 pVD = CT ? DomainImage(rVD,CT,MaxRays) : rVD;
1907 value_init(s[nd].E.d);
1908 evalue_set_si(&s[nd].E, 0, 1);
1910 FORALL_PVertex_in_ParamPolyhedron(V,D,PP) // _i is internal counter
1911 if (!et.vE[_i])
1912 et.decompose_at(V, _i, MaxRays);
1913 eadd(et.vE[_i] , &s[nd].E);
1914 END_FORALL_PVertex_in_ParamPolyhedron;
1915 reduce_in_domain(&s[nd].E, pVD);
1917 if (CT)
1918 addeliminatedparams_evalue(&s[nd].E, CT);
1919 s[nd].D = rVD;
1920 ++nd;
1921 if (rVD != pVD)
1922 Domain_Free(pVD);
1925 if (nd == 0)
1926 evalue_set_si(eres, 0, 1);
1927 else {
1928 eres->x.p = new_enode(partition, 2*nd, C->Dimension);
1929 for (int j = 0; j < nd; ++j) {
1930 EVALUE_SET_DOMAIN(eres->x.p->arr[2*j], s[j].D);
1931 value_clear(eres->x.p->arr[2*j+1].d);
1932 eres->x.p->arr[2*j+1] = s[j].E;
1933 Domain_Free(fVD[j]);
1936 delete [] s;
1937 delete [] fVD;
1940 if (CEq)
1941 Polyhedron_Free(CEq);
1943 goto out;
1946 Enumeration* barvinok_enumerate(Polyhedron *P, Polyhedron* C, unsigned MaxRays)
1948 evalue *EP = barvinok_enumerate_ev(P, C, MaxRays);
1950 return partition2enumeration(EP);
1953 static void SwapColumns(Value **V, int n, int i, int j)
1955 for (int r = 0; r < n; ++r)
1956 value_swap(V[r][i], V[r][j]);
1959 static void SwapColumns(Polyhedron *P, int i, int j)
1961 SwapColumns(P->Constraint, P->NbConstraints, i, j);
1962 SwapColumns(P->Ray, P->NbRays, i, j);
1965 static void negative_test_constraint(Value *l, Value *u, Value *c, int pos,
1966 int len, Value *v)
1968 value_oppose(*v, u[pos+1]);
1969 Vector_Combine(l+1, u+1, c+1, *v, l[pos+1], len-1);
1970 value_multiply(*v, *v, l[pos+1]);
1971 value_substract(c[len-1], c[len-1], *v);
1972 value_set_si(*v, -1);
1973 Vector_Scale(c+1, c+1, *v, len-1);
1974 value_decrement(c[len-1], c[len-1]);
1975 ConstraintSimplify(c, c, len, v);
1978 static void oppose_constraint(Value *c, int len, Value *v)
1980 value_set_si(*v, -1);
1981 Vector_Scale(c+1, c+1, *v, len-1);
1982 value_decrement(c[len-1], c[len-1]);
1985 static bool SplitOnConstraint(Polyhedron *P, int i, int l, int u,
1986 int nvar, int len, int exist, int MaxRays,
1987 Vector *row, Value& f, bool independent,
1988 Polyhedron **pos, Polyhedron **neg)
1990 negative_test_constraint(P->Constraint[l], P->Constraint[u],
1991 row->p, nvar+i, len, &f);
1992 *neg = AddConstraints(row->p, 1, P, MaxRays);
1994 /* We found an independent, but useless constraint
1995 * Maybe we should detect this earlier and not
1996 * mark the variable as INDEPENDENT
1998 if (emptyQ((*neg))) {
1999 Polyhedron_Free(*neg);
2000 return false;
2003 oppose_constraint(row->p, len, &f);
2004 *pos = AddConstraints(row->p, 1, P, MaxRays);
2006 if (emptyQ((*pos))) {
2007 Polyhedron_Free(*neg);
2008 Polyhedron_Free(*pos);
2009 return false;
2012 return true;
2016 * unimodularly transform P such that constraint r is transformed
2017 * into a constraint that involves only a single (the first)
2018 * existential variable
2021 static Polyhedron *rotate_along(Polyhedron *P, int r, int nvar, int exist,
2022 unsigned MaxRays)
2024 Value g;
2025 value_init(g);
2027 Vector *row = Vector_Alloc(exist);
2028 Vector_Copy(P->Constraint[r]+1+nvar, row->p, exist);
2029 Vector_Gcd(row->p, exist, &g);
2030 if (value_notone_p(g))
2031 Vector_AntiScale(row->p, row->p, g, exist);
2032 value_clear(g);
2034 Matrix *M = unimodular_complete(row);
2035 Matrix *M2 = Matrix_Alloc(P->Dimension+1, P->Dimension+1);
2036 for (r = 0; r < nvar; ++r)
2037 value_set_si(M2->p[r][r], 1);
2038 for ( ; r < nvar+exist; ++r)
2039 Vector_Copy(M->p[r-nvar], M2->p[r]+nvar, exist);
2040 for ( ; r < P->Dimension+1; ++r)
2041 value_set_si(M2->p[r][r], 1);
2042 Polyhedron *T = Polyhedron_Image(P, M2, MaxRays);
2044 Matrix_Free(M2);
2045 Matrix_Free(M);
2046 Vector_Free(row);
2048 return T;
2051 static bool SplitOnVar(Polyhedron *P, int i,
2052 int nvar, int len, int exist, int MaxRays,
2053 Vector *row, Value& f, bool independent,
2054 Polyhedron **pos, Polyhedron **neg)
2056 int j;
2058 for (int l = P->NbEq; l < P->NbConstraints; ++l) {
2059 if (value_negz_p(P->Constraint[l][nvar+i+1]))
2060 continue;
2062 if (independent) {
2063 for (j = 0; j < exist; ++j)
2064 if (j != i && value_notzero_p(P->Constraint[l][nvar+j+1]))
2065 break;
2066 if (j < exist)
2067 continue;
2070 for (int u = P->NbEq; u < P->NbConstraints; ++u) {
2071 if (value_posz_p(P->Constraint[u][nvar+i+1]))
2072 continue;
2074 if (independent) {
2075 for (j = 0; j < exist; ++j)
2076 if (j != i && value_notzero_p(P->Constraint[u][nvar+j+1]))
2077 break;
2078 if (j < exist)
2079 continue;
2082 if (SplitOnConstraint(P, i, l, u,
2083 nvar, len, exist, MaxRays,
2084 row, f, independent,
2085 pos, neg)) {
2086 if (independent) {
2087 if (i != 0)
2088 SwapColumns(*neg, nvar+1, nvar+1+i);
2090 return true;
2095 return false;
2098 static bool double_bound_pair(Polyhedron *P, int nvar, int exist,
2099 int i, int l1, int l2,
2100 Polyhedron **pos, Polyhedron **neg)
2102 Value f;
2103 value_init(f);
2104 Vector *row = Vector_Alloc(P->Dimension+2);
2105 value_set_si(row->p[0], 1);
2106 value_oppose(f, P->Constraint[l1][nvar+i+1]);
2107 Vector_Combine(P->Constraint[l1]+1, P->Constraint[l2]+1,
2108 row->p+1,
2109 P->Constraint[l2][nvar+i+1], f,
2110 P->Dimension+1);
2111 ConstraintSimplify(row->p, row->p, P->Dimension+2, &f);
2112 *pos = AddConstraints(row->p, 1, P, 0);
2113 value_set_si(f, -1);
2114 Vector_Scale(row->p+1, row->p+1, f, P->Dimension+1);
2115 value_decrement(row->p[P->Dimension+1], row->p[P->Dimension+1]);
2116 *neg = AddConstraints(row->p, 1, P, 0);
2117 Vector_Free(row);
2118 value_clear(f);
2120 return !emptyQ((*pos)) && !emptyQ((*neg));
2123 static bool double_bound(Polyhedron *P, int nvar, int exist,
2124 Polyhedron **pos, Polyhedron **neg)
2126 for (int i = 0; i < exist; ++i) {
2127 int l1, l2;
2128 for (l1 = P->NbEq; l1 < P->NbConstraints; ++l1) {
2129 if (value_negz_p(P->Constraint[l1][nvar+i+1]))
2130 continue;
2131 for (l2 = l1 + 1; l2 < P->NbConstraints; ++l2) {
2132 if (value_negz_p(P->Constraint[l2][nvar+i+1]))
2133 continue;
2134 if (double_bound_pair(P, nvar, exist, i, l1, l2, pos, neg))
2135 return true;
2138 for (l1 = P->NbEq; l1 < P->NbConstraints; ++l1) {
2139 if (value_posz_p(P->Constraint[l1][nvar+i+1]))
2140 continue;
2141 if (l1 < P->NbConstraints)
2142 for (l2 = l1 + 1; l2 < P->NbConstraints; ++l2) {
2143 if (value_posz_p(P->Constraint[l2][nvar+i+1]))
2144 continue;
2145 if (double_bound_pair(P, nvar, exist, i, l1, l2, pos, neg))
2146 return true;
2149 return false;
2151 return false;
2154 enum constraint {
2155 ALL_POS = 1 << 0,
2156 ONE_NEG = 1 << 1,
2157 INDEPENDENT = 1 << 2
2160 static evalue* enumerate_or(Polyhedron *D,
2161 unsigned exist, unsigned nparam, unsigned MaxRays)
2163 #ifdef DEBUG_ER
2164 fprintf(stderr, "\nER: Or\n");
2165 #endif /* DEBUG_ER */
2167 Polyhedron *N = D->next;
2168 D->next = 0;
2169 evalue *EP =
2170 barvinok_enumerate_e(D, exist, nparam, MaxRays);
2171 Polyhedron_Free(D);
2173 for (D = N; D; D = N) {
2174 N = D->next;
2175 D->next = 0;
2177 evalue *EN =
2178 barvinok_enumerate_e(D, exist, nparam, MaxRays);
2180 eor(EN, EP);
2181 free_evalue_refs(EN);
2182 free(EN);
2183 Polyhedron_Free(D);
2186 reduce_evalue(EP);
2188 return EP;
2191 static evalue* enumerate_sum(Polyhedron *P,
2192 unsigned exist, unsigned nparam, unsigned MaxRays)
2194 int nvar = P->Dimension - exist - nparam;
2195 int toswap = nvar < exist ? nvar : exist;
2196 for (int i = 0; i < toswap; ++i)
2197 SwapColumns(P, 1 + i, nvar+exist - i);
2198 nparam += nvar;
2200 #ifdef DEBUG_ER
2201 fprintf(stderr, "\nER: Sum\n");
2202 #endif /* DEBUG_ER */
2204 evalue *EP = barvinok_enumerate_e(P, exist, nparam, MaxRays);
2206 for (int i = 0; i < /* nvar */ nparam; ++i) {
2207 Matrix *C = Matrix_Alloc(1, 1 + nparam + 1);
2208 value_set_si(C->p[0][0], 1);
2209 evalue split;
2210 value_init(split.d);
2211 value_set_si(split.d, 0);
2212 split.x.p = new_enode(partition, 4, nparam);
2213 value_set_si(C->p[0][1+i], 1);
2214 Matrix *C2 = Matrix_Copy(C);
2215 EVALUE_SET_DOMAIN(split.x.p->arr[0],
2216 Constraints2Polyhedron(C2, MaxRays));
2217 Matrix_Free(C2);
2218 evalue_set_si(&split.x.p->arr[1], 1, 1);
2219 value_set_si(C->p[0][1+i], -1);
2220 value_set_si(C->p[0][1+nparam], -1);
2221 EVALUE_SET_DOMAIN(split.x.p->arr[2],
2222 Constraints2Polyhedron(C, MaxRays));
2223 evalue_set_si(&split.x.p->arr[3], 1, 1);
2224 emul(&split, EP);
2225 free_evalue_refs(&split);
2226 Matrix_Free(C);
2228 reduce_evalue(EP);
2229 evalue_range_reduction(EP);
2231 evalue_frac2floor(EP);
2233 evalue *sum = esum(EP, nvar);
2235 free_evalue_refs(EP);
2236 free(EP);
2237 EP = sum;
2239 evalue_range_reduction(EP);
2241 return EP;
2244 static evalue* split_sure(Polyhedron *P, Polyhedron *S,
2245 unsigned exist, unsigned nparam, unsigned MaxRays)
2247 int nvar = P->Dimension - exist - nparam;
2249 Matrix *M = Matrix_Alloc(exist, S->Dimension+2);
2250 for (int i = 0; i < exist; ++i)
2251 value_set_si(M->p[i][nvar+i+1], 1);
2252 Polyhedron *O = S;
2253 S = DomainAddRays(S, M, MaxRays);
2254 Polyhedron_Free(O);
2255 Polyhedron *F = DomainAddRays(P, M, MaxRays);
2256 Polyhedron *D = DomainDifference(F, S, MaxRays);
2257 O = D;
2258 D = Disjoint_Domain(D, 0, MaxRays);
2259 Polyhedron_Free(F);
2260 Domain_Free(O);
2261 Matrix_Free(M);
2263 M = Matrix_Alloc(P->Dimension+1-exist, P->Dimension+1);
2264 for (int j = 0; j < nvar; ++j)
2265 value_set_si(M->p[j][j], 1);
2266 for (int j = 0; j < nparam+1; ++j)
2267 value_set_si(M->p[nvar+j][nvar+exist+j], 1);
2268 Polyhedron *T = Polyhedron_Image(S, M, MaxRays);
2269 evalue *EP = barvinok_enumerate_e(T, 0, nparam, MaxRays);
2270 Polyhedron_Free(S);
2271 Polyhedron_Free(T);
2272 Matrix_Free(M);
2274 for (Polyhedron *Q = D; Q; Q = Q->next) {
2275 Polyhedron *N = Q->next;
2276 Q->next = 0;
2277 T = DomainIntersection(P, Q, MaxRays);
2278 evalue *E = barvinok_enumerate_e(T, exist, nparam, MaxRays);
2279 eadd(E, EP);
2280 free_evalue_refs(E);
2281 free(E);
2282 Polyhedron_Free(T);
2283 Q->next = N;
2285 Domain_Free(D);
2286 return EP;
2289 static evalue* enumerate_sure(Polyhedron *P,
2290 unsigned exist, unsigned nparam, unsigned MaxRays)
2292 int i;
2293 Polyhedron *S = P;
2294 int nvar = P->Dimension - exist - nparam;
2295 Value lcm;
2296 Value f;
2297 value_init(lcm);
2298 value_init(f);
2300 for (i = 0; i < exist; ++i) {
2301 Matrix *M = Matrix_Alloc(S->NbConstraints, S->Dimension+2);
2302 int c = 0;
2303 value_set_si(lcm, 1);
2304 for (int j = 0; j < S->NbConstraints; ++j) {
2305 if (value_negz_p(S->Constraint[j][1+nvar+i]))
2306 continue;
2307 if (value_one_p(S->Constraint[j][1+nvar+i]))
2308 continue;
2309 value_lcm(lcm, S->Constraint[j][1+nvar+i], &lcm);
2312 for (int j = 0; j < S->NbConstraints; ++j) {
2313 if (value_negz_p(S->Constraint[j][1+nvar+i]))
2314 continue;
2315 if (value_one_p(S->Constraint[j][1+nvar+i]))
2316 continue;
2317 value_division(f, lcm, S->Constraint[j][1+nvar+i]);
2318 Vector_Scale(S->Constraint[j], M->p[c], f, S->Dimension+2);
2319 value_substract(M->p[c][S->Dimension+1],
2320 M->p[c][S->Dimension+1],
2321 lcm);
2322 value_increment(M->p[c][S->Dimension+1],
2323 M->p[c][S->Dimension+1]);
2324 ++c;
2326 Polyhedron *O = S;
2327 S = AddConstraints(M->p[0], c, S, MaxRays);
2328 if (O != P)
2329 Polyhedron_Free(O);
2330 Matrix_Free(M);
2331 if (emptyQ(S)) {
2332 Polyhedron_Free(S);
2333 value_clear(lcm);
2334 value_clear(f);
2335 return 0;
2338 value_clear(lcm);
2339 value_clear(f);
2341 #ifdef DEBUG_ER
2342 fprintf(stderr, "\nER: Sure\n");
2343 #endif /* DEBUG_ER */
2345 return split_sure(P, S, exist, nparam, MaxRays);
2348 static evalue* enumerate_sure2(Polyhedron *P,
2349 unsigned exist, unsigned nparam, unsigned MaxRays)
2351 int nvar = P->Dimension - exist - nparam;
2352 int r;
2353 for (r = 0; r < P->NbRays; ++r)
2354 if (value_one_p(P->Ray[r][0]) &&
2355 value_one_p(P->Ray[r][P->Dimension+1]))
2356 break;
2358 if (r >= P->NbRays)
2359 return 0;
2361 Matrix *M = Matrix_Alloc(nvar + 1 + nparam, P->Dimension+2);
2362 for (int i = 0; i < nvar; ++i)
2363 value_set_si(M->p[i][1+i], 1);
2364 for (int i = 0; i < nparam; ++i)
2365 value_set_si(M->p[i+nvar][1+nvar+exist+i], 1);
2366 Vector_Copy(P->Ray[r]+1+nvar, M->p[nvar+nparam]+1+nvar, exist);
2367 value_set_si(M->p[nvar+nparam][0], 1);
2368 value_set_si(M->p[nvar+nparam][P->Dimension+1], 1);
2369 Polyhedron * F = Rays2Polyhedron(M, MaxRays);
2370 Matrix_Free(M);
2372 Polyhedron *I = DomainIntersection(F, P, MaxRays);
2373 Polyhedron_Free(F);
2375 #ifdef DEBUG_ER
2376 fprintf(stderr, "\nER: Sure2\n");
2377 #endif /* DEBUG_ER */
2379 return split_sure(P, I, exist, nparam, MaxRays);
2382 static evalue* enumerate_cyclic(Polyhedron *P,
2383 unsigned exist, unsigned nparam,
2384 evalue * EP, int r, int p, unsigned MaxRays)
2386 int nvar = P->Dimension - exist - nparam;
2388 /* If EP in its fractional maps only contains references
2389 * to the remainder parameter with appropriate coefficients
2390 * then we could in principle avoid adding existentially
2391 * quantified variables to the validity domains.
2392 * We'd have to replace the remainder by m { p/m }
2393 * and multiply with an appropriate factor that is one
2394 * only in the appropriate range.
2395 * This last multiplication can be avoided if EP
2396 * has a single validity domain with no (further)
2397 * constraints on the remainder parameter
2400 Matrix *CT = Matrix_Alloc(nparam+1, nparam+3);
2401 Matrix *M = Matrix_Alloc(1, 1+nparam+3);
2402 for (int j = 0; j < nparam; ++j)
2403 if (j != p)
2404 value_set_si(CT->p[j][j], 1);
2405 value_set_si(CT->p[p][nparam+1], 1);
2406 value_set_si(CT->p[nparam][nparam+2], 1);
2407 value_set_si(M->p[0][1+p], -1);
2408 value_absolute(M->p[0][1+nparam], P->Ray[0][1+nvar+exist+p]);
2409 value_set_si(M->p[0][1+nparam+1], 1);
2410 Polyhedron *CEq = Constraints2Polyhedron(M, 1);
2411 Matrix_Free(M);
2412 addeliminatedparams_enum(EP, CT, CEq, MaxRays, nparam);
2413 Polyhedron_Free(CEq);
2414 Matrix_Free(CT);
2416 return EP;
2419 static void enumerate_vd_add_ray(evalue *EP, Matrix *Rays, unsigned MaxRays)
2421 if (value_notzero_p(EP->d))
2422 return;
2424 assert(EP->x.p->type == partition);
2425 assert(EP->x.p->pos == EVALUE_DOMAIN(EP->x.p->arr[0])->Dimension);
2426 for (int i = 0; i < EP->x.p->size/2; ++i) {
2427 Polyhedron *D = EVALUE_DOMAIN(EP->x.p->arr[2*i]);
2428 Polyhedron *N = DomainAddRays(D, Rays, MaxRays);
2429 EVALUE_SET_DOMAIN(EP->x.p->arr[2*i], N);
2430 Domain_Free(D);
2434 static evalue* enumerate_line(Polyhedron *P,
2435 unsigned exist, unsigned nparam, unsigned MaxRays)
2437 if (P->NbBid == 0)
2438 return 0;
2440 #ifdef DEBUG_ER
2441 fprintf(stderr, "\nER: Line\n");
2442 #endif /* DEBUG_ER */
2444 int nvar = P->Dimension - exist - nparam;
2445 int i, j;
2446 for (i = 0; i < nparam; ++i)
2447 if (value_notzero_p(P->Ray[0][1+nvar+exist+i]))
2448 break;
2449 assert(i < nparam);
2450 for (j = i+1; j < nparam; ++j)
2451 if (value_notzero_p(P->Ray[0][1+nvar+exist+i]))
2452 break;
2453 assert(j >= nparam); // for now
2455 Matrix *M = Matrix_Alloc(2, P->Dimension+2);
2456 value_set_si(M->p[0][0], 1);
2457 value_set_si(M->p[0][1+nvar+exist+i], 1);
2458 value_set_si(M->p[1][0], 1);
2459 value_set_si(M->p[1][1+nvar+exist+i], -1);
2460 value_absolute(M->p[1][1+P->Dimension], P->Ray[0][1+nvar+exist+i]);
2461 value_decrement(M->p[1][1+P->Dimension], M->p[1][1+P->Dimension]);
2462 Polyhedron *S = AddConstraints(M->p[0], 2, P, MaxRays);
2463 evalue *EP = barvinok_enumerate_e(S, exist, nparam, MaxRays);
2464 Polyhedron_Free(S);
2465 Matrix_Free(M);
2467 return enumerate_cyclic(P, exist, nparam, EP, 0, i, MaxRays);
2470 static int single_param_pos(Polyhedron*P, unsigned exist, unsigned nparam,
2471 int r)
2473 int nvar = P->Dimension - exist - nparam;
2474 if (First_Non_Zero(P->Ray[r]+1, nvar) != -1)
2475 return -1;
2476 int i = First_Non_Zero(P->Ray[r]+1+nvar+exist, nparam);
2477 if (i == -1)
2478 return -1;
2479 if (First_Non_Zero(P->Ray[r]+1+nvar+exist+1, nparam-i-1) != -1)
2480 return -1;
2481 return i;
2484 static evalue* enumerate_remove_ray(Polyhedron *P, int r,
2485 unsigned exist, unsigned nparam, unsigned MaxRays)
2487 #ifdef DEBUG_ER
2488 fprintf(stderr, "\nER: RedundantRay\n");
2489 #endif /* DEBUG_ER */
2491 Value one;
2492 value_init(one);
2493 value_set_si(one, 1);
2494 int len = P->NbRays-1;
2495 Matrix *M = Matrix_Alloc(2 * len, P->Dimension+2);
2496 Vector_Copy(P->Ray[0], M->p[0], r * (P->Dimension+2));
2497 Vector_Copy(P->Ray[r+1], M->p[r], (len-r) * (P->Dimension+2));
2498 for (int j = 0; j < P->NbRays; ++j) {
2499 if (j == r)
2500 continue;
2501 Vector_Combine(P->Ray[j], P->Ray[r], M->p[len+j-(j>r)],
2502 one, P->Ray[j][P->Dimension+1], P->Dimension+2);
2505 P = Rays2Polyhedron(M, MaxRays);
2506 Matrix_Free(M);
2507 evalue *EP = barvinok_enumerate_e(P, exist, nparam, MaxRays);
2508 Polyhedron_Free(P);
2509 value_clear(one);
2511 return EP;
2514 static evalue* enumerate_redundant_ray(Polyhedron *P,
2515 unsigned exist, unsigned nparam, unsigned MaxRays)
2517 assert(P->NbBid == 0);
2518 int nvar = P->Dimension - exist - nparam;
2519 Value m;
2520 value_init(m);
2522 for (int r = 0; r < P->NbRays; ++r) {
2523 if (value_notzero_p(P->Ray[r][P->Dimension+1]))
2524 continue;
2525 int i1 = single_param_pos(P, exist, nparam, r);
2526 if (i1 == -1)
2527 continue;
2528 for (int r2 = r+1; r2 < P->NbRays; ++r2) {
2529 if (value_notzero_p(P->Ray[r2][P->Dimension+1]))
2530 continue;
2531 int i2 = single_param_pos(P, exist, nparam, r2);
2532 if (i2 == -1)
2533 continue;
2534 if (i1 != i2)
2535 continue;
2537 value_division(m, P->Ray[r][1+nvar+exist+i1],
2538 P->Ray[r2][1+nvar+exist+i1]);
2539 value_multiply(m, m, P->Ray[r2][1+nvar+exist+i1]);
2540 /* r2 divides r => r redundant */
2541 if (value_eq(m, P->Ray[r][1+nvar+exist+i1])) {
2542 value_clear(m);
2543 return enumerate_remove_ray(P, r, exist, nparam, MaxRays);
2546 value_division(m, P->Ray[r2][1+nvar+exist+i1],
2547 P->Ray[r][1+nvar+exist+i1]);
2548 value_multiply(m, m, P->Ray[r][1+nvar+exist+i1]);
2549 /* r divides r2 => r2 redundant */
2550 if (value_eq(m, P->Ray[r2][1+nvar+exist+i1])) {
2551 value_clear(m);
2552 return enumerate_remove_ray(P, r2, exist, nparam, MaxRays);
2556 value_clear(m);
2557 return 0;
2560 static Polyhedron *upper_bound(Polyhedron *P,
2561 int pos, Value *max, Polyhedron **R)
2563 Value v;
2564 int r;
2565 value_init(v);
2567 *R = 0;
2568 Polyhedron *N;
2569 Polyhedron *B = 0;
2570 for (Polyhedron *Q = P; Q; Q = N) {
2571 N = Q->next;
2572 for (r = 0; r < P->NbRays; ++r) {
2573 if (value_zero_p(P->Ray[r][P->Dimension+1]) &&
2574 value_pos_p(P->Ray[r][1+pos]))
2575 break;
2577 if (r < P->NbRays) {
2578 Q->next = *R;
2579 *R = Q;
2580 continue;
2581 } else {
2582 Q->next = B;
2583 B = Q;
2585 for (r = 0; r < P->NbRays; ++r) {
2586 if (value_zero_p(P->Ray[r][P->Dimension+1]))
2587 continue;
2588 mpz_fdiv_q(v, P->Ray[r][1+pos], P->Ray[r][1+P->Dimension]);
2589 if ((!Q->next && r == 0) || value_gt(v, *max))
2590 value_assign(*max, v);
2593 value_clear(v);
2594 return B;
2597 static evalue* enumerate_ray(Polyhedron *P,
2598 unsigned exist, unsigned nparam, unsigned MaxRays)
2600 assert(P->NbBid == 0);
2601 int nvar = P->Dimension - exist - nparam;
2603 int r;
2604 for (r = 0; r < P->NbRays; ++r)
2605 if (value_zero_p(P->Ray[r][P->Dimension+1]))
2606 break;
2607 if (r >= P->NbRays)
2608 return 0;
2610 int r2;
2611 for (r2 = r+1; r2 < P->NbRays; ++r2)
2612 if (value_zero_p(P->Ray[r2][P->Dimension+1]))
2613 break;
2614 if (r2 < P->NbRays) {
2615 if (nvar > 0)
2616 return enumerate_sum(P, exist, nparam, MaxRays);
2619 #ifdef DEBUG_ER
2620 fprintf(stderr, "\nER: Ray\n");
2621 #endif /* DEBUG_ER */
2623 Value m;
2624 Value one;
2625 value_init(m);
2626 value_init(one);
2627 value_set_si(one, 1);
2628 int i = single_param_pos(P, exist, nparam, r);
2629 assert(i != -1); // for now;
2631 Matrix *M = Matrix_Alloc(P->NbRays, P->Dimension+2);
2632 for (int j = 0; j < P->NbRays; ++j) {
2633 Vector_Combine(P->Ray[j], P->Ray[r], M->p[j],
2634 one, P->Ray[j][P->Dimension+1], P->Dimension+2);
2636 Polyhedron *S = Rays2Polyhedron(M, MaxRays);
2637 Matrix_Free(M);
2638 Polyhedron *D = DomainDifference(P, S, MaxRays);
2639 Polyhedron_Free(S);
2640 // Polyhedron_Print(stderr, P_VALUE_FMT, D);
2641 assert(value_pos_p(P->Ray[r][1+nvar+exist+i])); // for now
2642 Polyhedron *R;
2643 D = upper_bound(D, nvar+exist+i, &m, &R);
2644 assert(D);
2645 Domain_Free(D);
2647 M = Matrix_Alloc(2, P->Dimension+2);
2648 value_set_si(M->p[0][0], 1);
2649 value_set_si(M->p[1][0], 1);
2650 value_set_si(M->p[0][1+nvar+exist+i], -1);
2651 value_set_si(M->p[1][1+nvar+exist+i], 1);
2652 value_assign(M->p[0][1+P->Dimension], m);
2653 value_oppose(M->p[1][1+P->Dimension], m);
2654 value_addto(M->p[1][1+P->Dimension], M->p[1][1+P->Dimension],
2655 P->Ray[r][1+nvar+exist+i]);
2656 value_decrement(M->p[1][1+P->Dimension], M->p[1][1+P->Dimension]);
2657 // Matrix_Print(stderr, P_VALUE_FMT, M);
2658 D = AddConstraints(M->p[0], 2, P, MaxRays);
2659 // Polyhedron_Print(stderr, P_VALUE_FMT, D);
2660 value_substract(M->p[0][1+P->Dimension], M->p[0][1+P->Dimension],
2661 P->Ray[r][1+nvar+exist+i]);
2662 // Matrix_Print(stderr, P_VALUE_FMT, M);
2663 S = AddConstraints(M->p[0], 1, P, MaxRays);
2664 // Polyhedron_Print(stderr, P_VALUE_FMT, S);
2665 Matrix_Free(M);
2667 evalue *EP = barvinok_enumerate_e(D, exist, nparam, MaxRays);
2668 Polyhedron_Free(D);
2669 value_clear(one);
2670 value_clear(m);
2672 if (value_notone_p(P->Ray[r][1+nvar+exist+i]))
2673 EP = enumerate_cyclic(P, exist, nparam, EP, r, i, MaxRays);
2674 else {
2675 M = Matrix_Alloc(1, nparam+2);
2676 value_set_si(M->p[0][0], 1);
2677 value_set_si(M->p[0][1+i], 1);
2678 enumerate_vd_add_ray(EP, M, MaxRays);
2679 Matrix_Free(M);
2682 if (!emptyQ(S)) {
2683 evalue *E = barvinok_enumerate_e(S, exist, nparam, MaxRays);
2684 eadd(E, EP);
2685 free_evalue_refs(E);
2686 free(E);
2688 Polyhedron_Free(S);
2690 if (R) {
2691 assert(nvar == 0);
2692 evalue *ER = enumerate_or(R, exist, nparam, MaxRays);
2693 eor(ER, EP);
2694 free_evalue_refs(ER);
2695 free(ER);
2698 return EP;
2701 static evalue* new_zero_ep()
2703 evalue *EP;
2704 ALLOC(evalue, EP);
2705 value_init(EP->d);
2706 evalue_set_si(EP, 0, 1);
2707 return EP;
2710 static evalue* enumerate_vd(Polyhedron **PA,
2711 unsigned exist, unsigned nparam, unsigned MaxRays)
2713 Polyhedron *P = *PA;
2714 int nvar = P->Dimension - exist - nparam;
2715 Param_Polyhedron *PP = NULL;
2716 Polyhedron *C = Universe_Polyhedron(nparam);
2717 Polyhedron *CEq;
2718 Matrix *CT;
2719 Polyhedron *PR = P;
2720 PP = Polyhedron2Param_SimplifiedDomain(&PR,C,MaxRays,&CEq,&CT);
2721 Polyhedron_Free(C);
2723 int nd;
2724 Param_Domain *D, *last;
2725 Value c;
2726 value_init(c);
2727 for (nd = 0, D=PP->D; D; D=D->next, ++nd)
2730 Polyhedron **VD = new Polyhedron_p[nd];
2731 Polyhedron **fVD = new Polyhedron_p[nd];
2732 for(nd = 0, D=PP->D; D; D=D->next) {
2733 Polyhedron *rVD = reduce_domain(D->Domain, CT, CEq,
2734 fVD, nd, MaxRays);
2735 if (!rVD)
2736 continue;
2738 VD[nd++] = rVD;
2739 last = D;
2742 evalue *EP = 0;
2744 if (nd == 0)
2745 EP = new_zero_ep();
2747 /* This doesn't seem to have any effect */
2748 if (nd == 1) {
2749 Polyhedron *CA = align_context(VD[0], P->Dimension, MaxRays);
2750 Polyhedron *O = P;
2751 P = DomainIntersection(P, CA, MaxRays);
2752 if (O != *PA)
2753 Polyhedron_Free(O);
2754 Polyhedron_Free(CA);
2755 if (emptyQ(P))
2756 EP = new_zero_ep();
2759 if (!EP && CT->NbColumns != CT->NbRows) {
2760 Polyhedron *CEqr = DomainImage(CEq, CT, MaxRays);
2761 Polyhedron *CA = align_context(CEqr, PR->Dimension, MaxRays);
2762 Polyhedron *I = DomainIntersection(PR, CA, MaxRays);
2763 Polyhedron_Free(CEqr);
2764 Polyhedron_Free(CA);
2765 #ifdef DEBUG_ER
2766 fprintf(stderr, "\nER: Eliminate\n");
2767 #endif /* DEBUG_ER */
2768 nparam -= CT->NbColumns - CT->NbRows;
2769 EP = barvinok_enumerate_e(I, exist, nparam, MaxRays);
2770 nparam += CT->NbColumns - CT->NbRows;
2771 addeliminatedparams_enum(EP, CT, CEq, MaxRays, nparam);
2772 Polyhedron_Free(I);
2774 if (PR != *PA)
2775 Polyhedron_Free(PR);
2776 PR = 0;
2778 if (!EP && nd > 1) {
2779 #ifdef DEBUG_ER
2780 fprintf(stderr, "\nER: VD\n");
2781 #endif /* DEBUG_ER */
2782 for (int i = 0; i < nd; ++i) {
2783 Polyhedron *CA = align_context(VD[i], P->Dimension, MaxRays);
2784 Polyhedron *I = DomainIntersection(P, CA, MaxRays);
2786 if (i == 0)
2787 EP = barvinok_enumerate_e(I, exist, nparam, MaxRays);
2788 else {
2789 evalue *E = barvinok_enumerate_e(I, exist, nparam, MaxRays);
2790 eadd(E, EP);
2791 free_evalue_refs(E);
2792 free(E);
2794 Polyhedron_Free(I);
2795 Polyhedron_Free(CA);
2799 for (int i = 0; i < nd; ++i) {
2800 Polyhedron_Free(VD[i]);
2801 Polyhedron_Free(fVD[i]);
2803 delete [] VD;
2804 delete [] fVD;
2805 value_clear(c);
2807 if (!EP && nvar == 0) {
2808 Value f;
2809 value_init(f);
2810 Param_Vertices *V, *V2;
2811 Matrix* M = Matrix_Alloc(1, P->Dimension+2);
2813 FORALL_PVertex_in_ParamPolyhedron(V, last, PP) {
2814 bool found = false;
2815 FORALL_PVertex_in_ParamPolyhedron(V2, last, PP) {
2816 if (V == V2) {
2817 found = true;
2818 continue;
2820 if (!found)
2821 continue;
2822 for (int i = 0; i < exist; ++i) {
2823 value_oppose(f, V->Vertex->p[i][nparam+1]);
2824 Vector_Combine(V->Vertex->p[i],
2825 V2->Vertex->p[i],
2826 M->p[0] + 1 + nvar + exist,
2827 V2->Vertex->p[i][nparam+1],
2829 nparam+1);
2830 int j;
2831 for (j = 0; j < nparam; ++j)
2832 if (value_notzero_p(M->p[0][1+nvar+exist+j]))
2833 break;
2834 if (j >= nparam)
2835 continue;
2836 ConstraintSimplify(M->p[0], M->p[0],
2837 P->Dimension+2, &f);
2838 value_set_si(M->p[0][0], 0);
2839 Polyhedron *para = AddConstraints(M->p[0], 1, P,
2840 MaxRays);
2841 if (emptyQ(para)) {
2842 Polyhedron_Free(para);
2843 continue;
2845 Polyhedron *pos, *neg;
2846 value_set_si(M->p[0][0], 1);
2847 value_decrement(M->p[0][P->Dimension+1],
2848 M->p[0][P->Dimension+1]);
2849 neg = AddConstraints(M->p[0], 1, P, MaxRays);
2850 value_set_si(f, -1);
2851 Vector_Scale(M->p[0]+1, M->p[0]+1, f,
2852 P->Dimension+1);
2853 value_decrement(M->p[0][P->Dimension+1],
2854 M->p[0][P->Dimension+1]);
2855 value_decrement(M->p[0][P->Dimension+1],
2856 M->p[0][P->Dimension+1]);
2857 pos = AddConstraints(M->p[0], 1, P, MaxRays);
2858 if (emptyQ(neg) && emptyQ(pos)) {
2859 Polyhedron_Free(para);
2860 Polyhedron_Free(pos);
2861 Polyhedron_Free(neg);
2862 continue;
2864 #ifdef DEBUG_ER
2865 fprintf(stderr, "\nER: Order\n");
2866 #endif /* DEBUG_ER */
2867 EP = barvinok_enumerate_e(para, exist, nparam, MaxRays);
2868 evalue *E;
2869 if (!emptyQ(pos)) {
2870 E = barvinok_enumerate_e(pos, exist, nparam, MaxRays);
2871 eadd(E, EP);
2872 free_evalue_refs(E);
2873 free(E);
2875 if (!emptyQ(neg)) {
2876 E = barvinok_enumerate_e(neg, exist, nparam, MaxRays);
2877 eadd(E, EP);
2878 free_evalue_refs(E);
2879 free(E);
2881 Polyhedron_Free(para);
2882 Polyhedron_Free(pos);
2883 Polyhedron_Free(neg);
2884 break;
2886 if (EP)
2887 break;
2888 } END_FORALL_PVertex_in_ParamPolyhedron;
2889 if (EP)
2890 break;
2891 } END_FORALL_PVertex_in_ParamPolyhedron;
2893 if (!EP) {
2894 /* Search for vertex coordinate to split on */
2895 /* First look for one independent of the parameters */
2896 FORALL_PVertex_in_ParamPolyhedron(V, last, PP) {
2897 for (int i = 0; i < exist; ++i) {
2898 int j;
2899 for (j = 0; j < nparam; ++j)
2900 if (value_notzero_p(V->Vertex->p[i][j]))
2901 break;
2902 if (j < nparam)
2903 continue;
2904 value_set_si(M->p[0][0], 1);
2905 Vector_Set(M->p[0]+1, 0, nvar+exist);
2906 Vector_Copy(V->Vertex->p[i],
2907 M->p[0] + 1 + nvar + exist, nparam+1);
2908 value_oppose(M->p[0][1+nvar+i],
2909 V->Vertex->p[i][nparam+1]);
2911 Polyhedron *pos, *neg;
2912 value_set_si(M->p[0][0], 1);
2913 value_decrement(M->p[0][P->Dimension+1],
2914 M->p[0][P->Dimension+1]);
2915 neg = AddConstraints(M->p[0], 1, P, MaxRays);
2916 value_set_si(f, -1);
2917 Vector_Scale(M->p[0]+1, M->p[0]+1, f,
2918 P->Dimension+1);
2919 value_decrement(M->p[0][P->Dimension+1],
2920 M->p[0][P->Dimension+1]);
2921 value_decrement(M->p[0][P->Dimension+1],
2922 M->p[0][P->Dimension+1]);
2923 pos = AddConstraints(M->p[0], 1, P, MaxRays);
2924 if (emptyQ(neg) || emptyQ(pos)) {
2925 Polyhedron_Free(pos);
2926 Polyhedron_Free(neg);
2927 continue;
2929 Polyhedron_Free(pos);
2930 value_increment(M->p[0][P->Dimension+1],
2931 M->p[0][P->Dimension+1]);
2932 pos = AddConstraints(M->p[0], 1, P, MaxRays);
2933 #ifdef DEBUG_ER
2934 fprintf(stderr, "\nER: Vertex\n");
2935 #endif /* DEBUG_ER */
2936 pos->next = neg;
2937 EP = enumerate_or(pos, exist, nparam, MaxRays);
2938 break;
2940 if (EP)
2941 break;
2942 } END_FORALL_PVertex_in_ParamPolyhedron;
2945 if (!EP) {
2946 /* Search for vertex coordinate to split on */
2947 /* Now look for one that depends on the parameters */
2948 FORALL_PVertex_in_ParamPolyhedron(V, last, PP) {
2949 for (int i = 0; i < exist; ++i) {
2950 value_set_si(M->p[0][0], 1);
2951 Vector_Set(M->p[0]+1, 0, nvar+exist);
2952 Vector_Copy(V->Vertex->p[i],
2953 M->p[0] + 1 + nvar + exist, nparam+1);
2954 value_oppose(M->p[0][1+nvar+i],
2955 V->Vertex->p[i][nparam+1]);
2957 Polyhedron *pos, *neg;
2958 value_set_si(M->p[0][0], 1);
2959 value_decrement(M->p[0][P->Dimension+1],
2960 M->p[0][P->Dimension+1]);
2961 neg = AddConstraints(M->p[0], 1, P, MaxRays);
2962 value_set_si(f, -1);
2963 Vector_Scale(M->p[0]+1, M->p[0]+1, f,
2964 P->Dimension+1);
2965 value_decrement(M->p[0][P->Dimension+1],
2966 M->p[0][P->Dimension+1]);
2967 value_decrement(M->p[0][P->Dimension+1],
2968 M->p[0][P->Dimension+1]);
2969 pos = AddConstraints(M->p[0], 1, P, MaxRays);
2970 if (emptyQ(neg) || emptyQ(pos)) {
2971 Polyhedron_Free(pos);
2972 Polyhedron_Free(neg);
2973 continue;
2975 Polyhedron_Free(pos);
2976 value_increment(M->p[0][P->Dimension+1],
2977 M->p[0][P->Dimension+1]);
2978 pos = AddConstraints(M->p[0], 1, P, MaxRays);
2979 #ifdef DEBUG_ER
2980 fprintf(stderr, "\nER: ParamVertex\n");
2981 #endif /* DEBUG_ER */
2982 pos->next = neg;
2983 EP = enumerate_or(pos, exist, nparam, MaxRays);
2984 break;
2986 if (EP)
2987 break;
2988 } END_FORALL_PVertex_in_ParamPolyhedron;
2991 Matrix_Free(M);
2992 value_clear(f);
2995 if (CEq)
2996 Polyhedron_Free(CEq);
2997 if (CT)
2998 Matrix_Free(CT);
2999 if (PP)
3000 Param_Polyhedron_Free(PP);
3001 *PA = P;
3003 return EP;
3006 #ifndef HAVE_PIPLIB
3007 evalue *barvinok_enumerate_pip(Polyhedron *P,
3008 unsigned exist, unsigned nparam, unsigned MaxRays)
3010 return 0;
3012 #else
3013 evalue *barvinok_enumerate_pip(Polyhedron *P,
3014 unsigned exist, unsigned nparam, unsigned MaxRays)
3016 int nvar = P->Dimension - exist - nparam;
3017 evalue *EP = new_zero_ep();
3018 Polyhedron *Q, *N, *T = 0;
3019 Value min, tmp;
3020 value_init(min);
3021 value_init(tmp);
3023 #ifdef DEBUG_ER
3024 fprintf(stderr, "\nER: PIP\n");
3025 #endif /* DEBUG_ER */
3027 for (int i = 0; i < P->Dimension; ++i) {
3028 bool pos = false;
3029 bool neg = false;
3030 bool posray = false;
3031 bool negray = false;
3032 value_set_si(min, 0);
3033 for (int j = 0; j < P->NbRays; ++j) {
3034 if (value_pos_p(P->Ray[j][1+i])) {
3035 pos = true;
3036 if (value_zero_p(P->Ray[j][1+P->Dimension]))
3037 posray = true;
3038 } else if (value_neg_p(P->Ray[j][1+i])) {
3039 neg = true;
3040 if (value_zero_p(P->Ray[j][1+P->Dimension]))
3041 negray = true;
3042 else {
3043 mpz_fdiv_q(tmp,
3044 P->Ray[j][1+i], P->Ray[j][1+P->Dimension]);
3045 if (value_lt(tmp, min))
3046 value_assign(min, tmp);
3050 if (pos && neg) {
3051 assert(!(posray && negray));
3052 assert(!negray); // for now
3053 Polyhedron *O = T ? T : P;
3054 /* shift by a safe amount */
3055 Matrix *M = Matrix_Alloc(O->NbRays, O->Dimension+2);
3056 Vector_Copy(O->Ray[0], M->p[0], O->NbRays * (O->Dimension+2));
3057 for (int j = 0; j < P->NbRays; ++j) {
3058 if (value_notzero_p(M->p[j][1+P->Dimension])) {
3059 value_multiply(tmp, min, M->p[j][1+P->Dimension]);
3060 value_substract(M->p[j][1+i], M->p[j][1+i], tmp);
3063 if (T)
3064 Polyhedron_Free(T);
3065 T = Rays2Polyhedron(M, MaxRays);
3066 Matrix_Free(M);
3067 } else if (neg) {
3068 /* negating a parameter requires that we substitute in the
3069 * sign again afterwards.
3070 * Disallow for now.
3072 assert(i < nvar+exist);
3073 if (!T)
3074 T = Polyhedron_Copy(P);
3075 for (int j = 0; j < T->NbRays; ++j)
3076 value_oppose(T->Ray[j][1+i], T->Ray[j][1+i]);
3077 for (int j = 0; j < T->NbConstraints; ++j)
3078 value_oppose(T->Constraint[j][1+i], T->Constraint[j][1+i]);
3081 value_clear(min);
3082 value_clear(tmp);
3084 Polyhedron *D = pip_lexmin(T ? T : P, exist, nparam);
3085 for (Q = D; Q; Q = N) {
3086 N = Q->next;
3087 Q->next = 0;
3088 evalue *E;
3089 exist = Q->Dimension - nvar - nparam;
3090 E = barvinok_enumerate_e(Q, exist, nparam, MaxRays);
3091 Polyhedron_Free(Q);
3092 eadd(E, EP);
3093 free_evalue_refs(E);
3094 free(E);
3097 if (T)
3098 Polyhedron_Free(T);
3100 return EP;
3102 #endif
3105 static bool is_single(Value *row, int pos, int len)
3107 return First_Non_Zero(row, pos) == -1 &&
3108 First_Non_Zero(row+pos+1, len-pos-1) == -1;
3111 static evalue* barvinok_enumerate_e_r(Polyhedron *P,
3112 unsigned exist, unsigned nparam, unsigned MaxRays);
3114 #ifdef DEBUG_ER
3115 static int er_level = 0;
3117 evalue* barvinok_enumerate_e(Polyhedron *P,
3118 unsigned exist, unsigned nparam, unsigned MaxRays)
3120 fprintf(stderr, "\nER: level %i\n", er_level);
3121 int nvar = P->Dimension - exist - nparam;
3122 fprintf(stderr, "%d %d %d\n", nvar, exist, nparam);
3124 Polyhedron_Print(stderr, P_VALUE_FMT, P);
3125 ++er_level;
3126 P = DomainConstraintSimplify(Polyhedron_Copy(P), MaxRays);
3127 evalue *EP = barvinok_enumerate_e_r(P, exist, nparam, MaxRays);
3128 Polyhedron_Free(P);
3129 --er_level;
3130 return EP;
3132 #else
3133 evalue* barvinok_enumerate_e(Polyhedron *P,
3134 unsigned exist, unsigned nparam, unsigned MaxRays)
3136 P = DomainConstraintSimplify(Polyhedron_Copy(P), MaxRays);
3137 evalue *EP = barvinok_enumerate_e_r(P, exist, nparam, MaxRays);
3138 Polyhedron_Free(P);
3139 return EP;
3141 #endif
3143 static evalue* barvinok_enumerate_e_r(Polyhedron *P,
3144 unsigned exist, unsigned nparam, unsigned MaxRays)
3146 if (exist == 0) {
3147 Polyhedron *U = Universe_Polyhedron(nparam);
3148 evalue *EP = barvinok_enumerate_ev(P, U, MaxRays);
3149 //char *param_name[] = {"P", "Q", "R", "S", "T" };
3150 //print_evalue(stdout, EP, param_name);
3151 Polyhedron_Free(U);
3152 return EP;
3155 int nvar = P->Dimension - exist - nparam;
3156 int len = P->Dimension + 2;
3158 if (emptyQ(P))
3159 return new_zero_ep();
3161 if (nvar == 0 && nparam == 0) {
3162 evalue *EP = new_zero_ep();
3163 barvinok_count(P, &EP->x.n, MaxRays);
3164 if (value_pos_p(EP->x.n))
3165 value_set_si(EP->x.n, 1);
3166 return EP;
3169 int r;
3170 for (r = 0; r < P->NbRays; ++r)
3171 if (value_zero_p(P->Ray[r][0]) ||
3172 value_zero_p(P->Ray[r][P->Dimension+1])) {
3173 int i;
3174 for (i = 0; i < nvar; ++i)
3175 if (value_notzero_p(P->Ray[r][i+1]))
3176 break;
3177 if (i >= nvar)
3178 continue;
3179 for (i = nvar + exist; i < nvar + exist + nparam; ++i)
3180 if (value_notzero_p(P->Ray[r][i+1]))
3181 break;
3182 if (i >= nvar + exist + nparam)
3183 break;
3185 if (r < P->NbRays) {
3186 evalue *EP = new_zero_ep();
3187 value_set_si(EP->x.n, -1);
3188 return EP;
3191 int first;
3192 for (r = 0; r < P->NbEq; ++r)
3193 if ((first = First_Non_Zero(P->Constraint[r]+1+nvar, exist)) != -1)
3194 break;
3195 if (r < P->NbEq) {
3196 if (First_Non_Zero(P->Constraint[r]+1+nvar+first+1,
3197 exist-first-1) != -1) {
3198 Polyhedron *T = rotate_along(P, r, nvar, exist, MaxRays);
3199 #ifdef DEBUG_ER
3200 fprintf(stderr, "\nER: Equality\n");
3201 #endif /* DEBUG_ER */
3202 evalue *EP = barvinok_enumerate_e(T, exist-1, nparam, MaxRays);
3203 Polyhedron_Free(T);
3204 return EP;
3205 } else {
3206 #ifdef DEBUG_ER
3207 fprintf(stderr, "\nER: Fixed\n");
3208 #endif /* DEBUG_ER */
3209 if (first == 0)
3210 return barvinok_enumerate_e(P, exist-1, nparam, MaxRays);
3211 else {
3212 Polyhedron *T = Polyhedron_Copy(P);
3213 SwapColumns(T, nvar+1, nvar+1+first);
3214 evalue *EP = barvinok_enumerate_e(T, exist-1, nparam, MaxRays);
3215 Polyhedron_Free(T);
3216 return EP;
3221 Vector *row = Vector_Alloc(len);
3222 value_set_si(row->p[0], 1);
3224 Value f;
3225 value_init(f);
3227 enum constraint* info = new constraint[exist];
3228 for (int i = 0; i < exist; ++i) {
3229 info[i] = ALL_POS;
3230 for (int l = P->NbEq; l < P->NbConstraints; ++l) {
3231 if (value_negz_p(P->Constraint[l][nvar+i+1]))
3232 continue;
3233 bool l_parallel = is_single(P->Constraint[l]+nvar+1, i, exist);
3234 for (int u = P->NbEq; u < P->NbConstraints; ++u) {
3235 if (value_posz_p(P->Constraint[u][nvar+i+1]))
3236 continue;
3237 bool lu_parallel = l_parallel ||
3238 is_single(P->Constraint[u]+nvar+1, i, exist);
3239 value_oppose(f, P->Constraint[u][nvar+i+1]);
3240 Vector_Combine(P->Constraint[l]+1, P->Constraint[u]+1, row->p+1,
3241 f, P->Constraint[l][nvar+i+1], len-1);
3242 if (!(info[i] & INDEPENDENT)) {
3243 int j;
3244 for (j = 0; j < exist; ++j)
3245 if (j != i && value_notzero_p(row->p[nvar+j+1]))
3246 break;
3247 if (j == exist) {
3248 //printf("independent: i: %d, l: %d, u: %d\n", i, l, u);
3249 info[i] = (constraint)(info[i] | INDEPENDENT);
3252 if (info[i] & ALL_POS) {
3253 value_addto(row->p[len-1], row->p[len-1],
3254 P->Constraint[l][nvar+i+1]);
3255 value_addto(row->p[len-1], row->p[len-1], f);
3256 value_multiply(f, f, P->Constraint[l][nvar+i+1]);
3257 value_substract(row->p[len-1], row->p[len-1], f);
3258 value_decrement(row->p[len-1], row->p[len-1]);
3259 ConstraintSimplify(row->p, row->p, len, &f);
3260 value_set_si(f, -1);
3261 Vector_Scale(row->p+1, row->p+1, f, len-1);
3262 value_decrement(row->p[len-1], row->p[len-1]);
3263 Polyhedron *T = AddConstraints(row->p, 1, P, MaxRays);
3264 if (!emptyQ(T)) {
3265 //printf("not all_pos: i: %d, l: %d, u: %d\n", i, l, u);
3266 info[i] = (constraint)(info[i] ^ ALL_POS);
3268 //puts("pos remainder");
3269 //Polyhedron_Print(stdout, P_VALUE_FMT, T);
3270 Polyhedron_Free(T);
3272 if (!(info[i] & ONE_NEG)) {
3273 if (lu_parallel) {
3274 negative_test_constraint(P->Constraint[l],
3275 P->Constraint[u],
3276 row->p, nvar+i, len, &f);
3277 oppose_constraint(row->p, len, &f);
3278 Polyhedron *T = AddConstraints(row->p, 1, P, MaxRays);
3279 if (emptyQ(T)) {
3280 //printf("one_neg i: %d, l: %d, u: %d\n", i, l, u);
3281 info[i] = (constraint)(info[i] | ONE_NEG);
3283 //puts("neg remainder");
3284 //Polyhedron_Print(stdout, P_VALUE_FMT, T);
3285 Polyhedron_Free(T);
3288 if (!(info[i] & ALL_POS) && (info[i] & ONE_NEG))
3289 goto next;
3292 if (info[i] & ALL_POS)
3293 break;
3294 next:
3299 for (int i = 0; i < exist; ++i)
3300 printf("%i: %i\n", i, info[i]);
3302 for (int i = 0; i < exist; ++i)
3303 if (info[i] & ALL_POS) {
3304 #ifdef DEBUG_ER
3305 fprintf(stderr, "\nER: Positive\n");
3306 #endif /* DEBUG_ER */
3307 // Eliminate
3308 // Maybe we should chew off some of the fat here
3309 Matrix *M = Matrix_Alloc(P->Dimension, P->Dimension+1);
3310 for (int j = 0; j < P->Dimension; ++j)
3311 value_set_si(M->p[j][j + (j >= i+nvar)], 1);
3312 Polyhedron *T = Polyhedron_Image(P, M, MaxRays);
3313 Matrix_Free(M);
3314 evalue *EP = barvinok_enumerate_e(T, exist-1, nparam, MaxRays);
3315 Polyhedron_Free(T);
3316 value_clear(f);
3317 Vector_Free(row);
3318 delete [] info;
3319 return EP;
3321 for (int i = 0; i < exist; ++i)
3322 if (info[i] & ONE_NEG) {
3323 #ifdef DEBUG_ER
3324 fprintf(stderr, "\nER: Negative\n");
3325 #endif /* DEBUG_ER */
3326 Vector_Free(row);
3327 value_clear(f);
3328 delete [] info;
3329 if (i == 0)
3330 return barvinok_enumerate_e(P, exist-1, nparam, MaxRays);
3331 else {
3332 Polyhedron *T = Polyhedron_Copy(P);
3333 SwapColumns(T, nvar+1, nvar+1+i);
3334 evalue *EP = barvinok_enumerate_e(T, exist-1, nparam, MaxRays);
3335 Polyhedron_Free(T);
3336 return EP;
3339 for (int i = 0; i < exist; ++i)
3340 if (info[i] & INDEPENDENT) {
3341 Polyhedron *pos, *neg;
3343 /* Find constraint again and split off negative part */
3345 if (SplitOnVar(P, i, nvar, len, exist, MaxRays,
3346 row, f, true, &pos, &neg)) {
3347 #ifdef DEBUG_ER
3348 fprintf(stderr, "\nER: Split\n");
3349 #endif /* DEBUG_ER */
3351 evalue *EP =
3352 barvinok_enumerate_e(neg, exist-1, nparam, MaxRays);
3353 evalue *E =
3354 barvinok_enumerate_e(pos, exist, nparam, MaxRays);
3355 eadd(E, EP);
3356 free_evalue_refs(E);
3357 free(E);
3358 Polyhedron_Free(neg);
3359 Polyhedron_Free(pos);
3360 value_clear(f);
3361 Vector_Free(row);
3362 delete [] info;
3363 return EP;
3366 delete [] info;
3368 Polyhedron *O = P;
3369 Polyhedron *F;
3371 evalue *EP;
3373 EP = enumerate_line(P, exist, nparam, MaxRays);
3374 if (EP)
3375 goto out;
3377 EP = barvinok_enumerate_pip(P, exist, nparam, MaxRays);
3378 if (EP)
3379 goto out;
3381 EP = enumerate_redundant_ray(P, exist, nparam, MaxRays);
3382 if (EP)
3383 goto out;
3385 EP = enumerate_sure(P, exist, nparam, MaxRays);
3386 if (EP)
3387 goto out;
3389 EP = enumerate_ray(P, exist, nparam, MaxRays);
3390 if (EP)
3391 goto out;
3393 EP = enumerate_sure2(P, exist, nparam, MaxRays);
3394 if (EP)
3395 goto out;
3397 F = unfringe(P, MaxRays);
3398 if (!PolyhedronIncludes(F, P)) {
3399 #ifdef DEBUG_ER
3400 fprintf(stderr, "\nER: Fringed\n");
3401 #endif /* DEBUG_ER */
3402 EP = barvinok_enumerate_e(F, exist, nparam, MaxRays);
3403 Polyhedron_Free(F);
3404 goto out;
3406 Polyhedron_Free(F);
3408 if (nparam)
3409 EP = enumerate_vd(&P, exist, nparam, MaxRays);
3410 if (EP)
3411 goto out2;
3413 if (nvar != 0) {
3414 EP = enumerate_sum(P, exist, nparam, MaxRays);
3415 goto out2;
3418 assert(nvar == 0);
3420 int i;
3421 Polyhedron *pos, *neg;
3422 for (i = 0; i < exist; ++i)
3423 if (SplitOnVar(P, i, nvar, len, exist, MaxRays,
3424 row, f, false, &pos, &neg))
3425 break;
3427 assert (i < exist);
3429 pos->next = neg;
3430 EP = enumerate_or(pos, exist, nparam, MaxRays);
3432 out2:
3433 if (O != P)
3434 Polyhedron_Free(P);
3436 out:
3437 value_clear(f);
3438 Vector_Free(row);
3439 return EP;
3442 static void normalize(Polyhedron *i, vec_ZZ& lambda, ZZ& sign,
3443 ZZ& num_s, vec_ZZ& num_p, vec_ZZ& den_s, vec_ZZ& den_p,
3444 mat_ZZ& f)
3446 unsigned dim = i->Dimension;
3447 unsigned nparam = num_p.length();
3448 unsigned nvar = dim - nparam;
3450 int r = 0;
3451 mat_ZZ rays;
3452 rays.SetDims(dim, nvar);
3453 add_rays(rays, i, &r, nvar, true);
3454 den_s = rays * lambda;
3455 int change = 0;
3458 for (int j = 0; j < den_s.length(); ++j) {
3459 values2zz(i->Ray[j]+1+nvar, f[j], nparam);
3460 if (den_s[j] == 0) {
3461 den_p[j] = 1;
3462 continue;
3464 if (First_Non_Zero(i->Ray[j]+1+nvar, nparam) != -1) {
3465 if (den_s[j] > 0) {
3466 den_p[j] = -1;
3467 num_p -= f[j];
3468 } else
3469 den_p[j] = 1;
3470 } else
3471 den_p[j] = 0;
3472 if (den_s[j] > 0)
3473 change ^= 1;
3474 else {
3475 den_s[j] = abs(den_s[j]);
3476 num_s += den_s[j];
3480 if (change)
3481 sign = -sign;
3484 gen_fun * barvinok_series(Polyhedron *P, Polyhedron* C, unsigned MaxRays)
3486 Polyhedron ** vcone;
3487 Polyhedron *CA;
3488 unsigned nparam = C->Dimension;
3489 unsigned dim, nvar;
3490 vec_ZZ sign;
3491 int ncone = 0;
3492 sign.SetLength(ncone);
3494 CA = align_context(C, P->Dimension, MaxRays);
3495 P = DomainIntersection(P, CA, MaxRays);
3496 Polyhedron_Free(CA);
3498 assert(!Polyhedron_is_infinite(P, nparam));
3499 assert(P->NbBid == 0);
3500 assert(Polyhedron_has_positive_rays(P, nparam));
3501 assert(P->NbEq == 0);
3503 dim = P->Dimension;
3504 nvar = dim - nparam;
3505 vcone = new Polyhedron_p[P->NbRays];
3507 for (int j = 0; j < P->NbRays; ++j) {
3508 if (!value_pos_p(P->Ray[j][dim+1]))
3509 continue;
3511 int npos, nneg;
3512 Polyhedron *C = supporting_cone(P, j);
3513 decompose(C, &vcone[j], &npos, &nneg, MaxRays);
3514 ncone += npos + nneg;
3515 sign.SetLength(ncone);
3516 for (int k = 0; k < npos; ++k)
3517 sign[ncone-nneg-k-1] = 1;
3518 for (int k = 0; k < nneg; ++k)
3519 sign[ncone-k-1] = -1;
3522 mat_ZZ rays;
3523 rays.SetDims(ncone * dim, nvar);
3524 int r = 0;
3525 for (int j = 0; j < P->NbRays; ++j) {
3526 if (!value_pos_p(P->Ray[j][dim+1]))
3527 continue;
3529 for (Polyhedron *i = vcone[j]; i; i = i->next) {
3530 add_rays(rays, i, &r, nvar);
3533 rays.SetDims(r, nvar);
3534 vec_ZZ lambda;
3535 nonorthog(rays, lambda);
3536 //randomvector(P, lambda, nvar);
3539 cout << "rays: " << rays;
3540 cout << "lambda: " << lambda;
3543 int f = 0;
3544 ZZ num_s;
3545 vec_ZZ num_p;
3546 num_p.SetLength(nparam);
3547 vec_ZZ vertex;
3548 vec_ZZ den_s;
3549 den_s.SetLength(dim);
3550 vec_ZZ den_p;
3551 den_p.SetLength(dim);
3552 mat_ZZ den;
3553 den.SetDims(dim, nparam);
3554 ZZ one;
3555 one = 1;
3556 mpq_t count;
3557 mpq_init(count);
3559 gen_fun * gf = new gen_fun;
3561 for (int j = 0; j < P->NbRays; ++j) {
3562 if (!value_pos_p(P->Ray[j][dim+1]))
3563 continue;
3565 for (Polyhedron *i = vcone[j]; i; i = i->next, ++f) {
3566 lattice_point(P->Ray[j]+1, i, vertex);
3567 int k = 0;
3568 num_s = 0;
3569 for ( ; k < nvar; ++k)
3570 num_s += vertex[k] * lambda[k];
3571 for ( ; k < dim; ++k)
3572 num_p[k-nvar] = vertex[k];
3573 normalize(i, lambda, sign[f], num_s, num_p,
3574 den_s, den_p, den);
3576 int only_param = 0;
3577 int no_param = 0;
3578 for (int k = 0; k < dim; ++k) {
3579 if (den_p[k] == 0)
3580 ++no_param;
3581 else if (den_s[k] == 0)
3582 ++only_param;
3584 if (no_param == 0) {
3585 for (int k = 0; k < dim; ++k)
3586 if (den_p[k] == -1)
3587 den[k] = -den[k];
3588 gf->add(sign[f], one, num_p, den);
3589 } else if (no_param + only_param == dim) {
3590 int k, l;
3591 mat_ZZ pden;
3592 pden.SetDims(only_param, nparam);
3594 for (k = 0, l = 0; k < dim; ++k)
3595 if (den_p[k] != 0)
3596 pden[l++] = den[k];
3598 for (k = 0; k < dim; ++k)
3599 if (den_s[k] != 0)
3600 break;
3602 dpoly n(no_param, num_s);
3603 dpoly d(no_param, den_s[k], 1);
3604 for ( ; k < dim; ++k)
3605 if (den_s[k] != 0) {
3606 dpoly fact(no_param, den_s[k], 1);
3607 d *= fact;
3610 mpq_set_si(count, 0, 1);
3611 n.div(d, count, sign[f]);
3613 ZZ qn, qd;
3614 value2zz(mpq_numref(count), qn);
3615 value2zz(mpq_denref(count), qd);
3617 gf->add(qn, qd, num_p, pden);
3618 } else {
3619 int k, l;
3620 dpoly_r * r = 0;
3621 mat_ZZ pden;
3622 pden.SetDims(only_param, nparam);
3624 for (k = 0, l = 0; k < dim; ++k)
3625 if (den_s[k] == 0)
3626 pden[l++] = den[k];
3628 for (k = 0; k < dim; ++k)
3629 if (den_p[k] == 0)
3630 break;
3632 dpoly n(no_param, num_s);
3633 dpoly d(no_param, den_s[k], 1);
3634 for ( ; k < dim; ++k)
3635 if (den_p[k] == 0) {
3636 dpoly fact(no_param, den_s[k], 1);
3637 d *= fact;
3640 for (k = 0; k < dim; ++k) {
3641 if (den_s[k] == 0 || den_p[k] == 0)
3642 continue;
3644 dpoly pd(no_param-1, den_s[k], 1);
3645 int s = den_p[k] < 0 ? -1 : 1;
3647 if (r == 0)
3648 r = new dpoly_r(n, pd, k, s, dim);
3649 else
3650 assert(0); // for now
3653 r->div(d, sign[f], gf, pden, den, num_p);
3657 cout << "sign: " << sign[f];
3658 cout << "num_s: " << num_s;
3659 cout << "num_p: " << num_p;
3660 cout << "den_s: " << den_s;
3661 cout << "den_p: " << den_p;
3662 cout << "den: " << den;
3663 cout << "only_param: " << only_param;
3664 cout << "no_param: " << no_param;
3665 cout << endl;
3671 mpq_clear(count);
3673 return gf;