replace { E/m } by { (E-1)/m } + 1/m if { E/m } != 0
[barvinok.git] / barvinok.cc
blob672b4a39f277891041d37c3f537960ae16da9b5d
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"
15 #include "config.h"
16 #include <barvinok.h>
18 #ifdef NTL_STD_CXX
19 using namespace NTL;
20 #endif
21 using std::cout;
22 using std::endl;
23 using std::vector;
24 using std::deque;
25 using std::string;
26 using std::ostringstream;
28 #define ALLOC(p) (((long *) (p))[0])
29 #define SIZE(p) (((long *) (p))[1])
30 #define DATA(p) ((mp_limb_t *) (((long *) (p)) + 2))
32 static void value2zz(Value v, ZZ& z)
34 int sa = v[0]._mp_size;
35 int abs_sa = sa < 0 ? -sa : sa;
37 _ntl_gsetlength(&z.rep, abs_sa);
38 mp_limb_t * adata = DATA(z.rep);
39 for (int i = 0; i < abs_sa; ++i)
40 adata[i] = v[0]._mp_d[i];
41 SIZE(z.rep) = sa;
44 static void zz2value(ZZ& z, Value& v)
46 if (!z.rep) {
47 value_set_si(v, 0);
48 return;
51 int sa = SIZE(z.rep);
52 int abs_sa = sa < 0 ? -sa : sa;
54 mp_limb_t * adata = DATA(z.rep);
55 mpz_realloc2(v, __GMP_BITS_PER_MP_LIMB * abs_sa);
56 for (int i = 0; i < abs_sa; ++i)
57 v[0]._mp_d[i] = adata[i];
58 v[0]._mp_size = sa;
61 #undef ALLOC
62 #define ALLOC(p) p = (typeof(p))malloc(sizeof(*p))
65 * We just ignore the last column and row
66 * If the final element is not equal to one
67 * then the result will actually be a multiple of the input
69 static void matrix2zz(Matrix *M, mat_ZZ& m, unsigned nr, unsigned nc)
71 m.SetDims(nr, nc);
73 for (int i = 0; i < nr; ++i) {
74 // assert(value_one_p(M->p[i][M->NbColumns - 1]));
75 for (int j = 0; j < nc; ++j) {
76 value2zz(M->p[i][j], m[i][j]);
81 static void values2zz(Value *p, vec_ZZ& v, int len)
83 v.SetLength(len);
85 for (int i = 0; i < len; ++i) {
86 value2zz(p[i], v[i]);
92 static void zz2values(vec_ZZ& v, Value *p)
94 for (int i = 0; i < v.length(); ++i)
95 zz2value(v[i], p[i]);
98 static void rays(mat_ZZ& r, Polyhedron *C)
100 unsigned dim = C->NbRays - 1; /* don't count zero vertex */
101 assert(C->NbRays - 1 == C->Dimension);
102 r.SetDims(dim, dim);
103 ZZ tmp;
105 int i, c;
106 for (i = 0, c = 0; i < dim; ++i)
107 if (value_zero_p(C->Ray[i][dim+1])) {
108 for (int j = 0; j < dim; ++j) {
109 value2zz(C->Ray[i][j+1], tmp);
110 r[j][c] = tmp;
112 ++c;
116 static Matrix * rays(Polyhedron *C)
118 unsigned dim = C->NbRays - 1; /* don't count zero vertex */
119 assert(C->NbRays - 1 == C->Dimension);
121 Matrix *M = Matrix_Alloc(dim+1, dim+1);
122 assert(M);
124 int i, c;
125 for (i = 0, c = 0; i <= dim && c < dim; ++i)
126 if (value_zero_p(C->Ray[i][dim+1])) {
127 Vector_Copy(C->Ray[i] + 1, M->p[c], dim);
128 value_set_si(M->p[c++][dim], 0);
130 assert(c == dim);
131 value_set_si(M->p[dim][dim], 1);
133 return M;
136 static Matrix * rays2(Polyhedron *C)
138 unsigned dim = C->NbRays - 1; /* don't count zero vertex */
139 assert(C->NbRays - 1 == C->Dimension);
141 Matrix *M = Matrix_Alloc(dim, dim);
142 assert(M);
144 int i, c;
145 for (i = 0, c = 0; i <= dim && c < dim; ++i)
146 if (value_zero_p(C->Ray[i][dim+1]))
147 Vector_Copy(C->Ray[i] + 1, M->p[c++], dim);
148 assert(c == dim);
150 return M;
154 * Returns the largest absolute value in the vector
156 static ZZ max(vec_ZZ& v)
158 ZZ max = abs(v[0]);
159 for (int i = 1; i < v.length(); ++i)
160 if (abs(v[i]) > max)
161 max = abs(v[i]);
162 return max;
165 class cone {
166 public:
167 cone(Matrix *M) {
168 Cone = 0;
169 Rays = Matrix_Copy(M);
170 set_det();
172 cone(Polyhedron *C) {
173 Cone = Polyhedron_Copy(C);
174 Rays = rays(C);
175 set_det();
177 void set_det() {
178 mat_ZZ A;
179 matrix2zz(Rays, A, Rays->NbRows - 1, Rays->NbColumns - 1);
180 det = determinant(A);
181 Value v;
182 value_init(v);
183 zz2value(det, v);
184 value_clear(v);
187 Vector* short_vector(vec_ZZ& lambda) {
188 Matrix *M = Matrix_Copy(Rays);
189 Matrix *inv = Matrix_Alloc(M->NbRows, M->NbColumns);
190 int ok = Matrix_Inverse(M, inv);
191 assert(ok);
192 Matrix_Free(M);
194 ZZ det2;
195 mat_ZZ B;
196 mat_ZZ U;
197 matrix2zz(inv, B, inv->NbRows - 1, inv->NbColumns - 1);
198 long r = LLL(det2, B, U);
200 ZZ min = max(B[0]);
201 int index = 0;
202 for (int i = 1; i < B.NumRows(); ++i) {
203 ZZ tmp = max(B[i]);
204 if (tmp < min) {
205 min = tmp;
206 index = i;
210 Matrix_Free(inv);
212 lambda = B[index];
214 Vector *z = Vector_Alloc(U[index].length()+1);
215 assert(z);
216 zz2values(U[index], z->p);
217 value_set_si(z->p[U[index].length()], 0);
219 Value tmp;
220 value_init(tmp);
221 Polyhedron *C = poly();
222 int i;
223 for (i = 0; i < C->NbConstraints; ++i) {
224 Inner_Product(z->p, C->Constraint[i]+1, z->Size-1, &tmp);
225 if (value_pos_p(tmp))
226 break;
228 if (i == C->NbConstraints) {
229 value_set_si(tmp, -1);
230 Vector_Scale(z->p, z->p, tmp, z->Size-1);
232 value_clear(tmp);
233 return z;
236 ~cone() {
237 Polyhedron_Free(Cone);
238 Matrix_Free(Rays);
241 Polyhedron *poly() {
242 if (!Cone) {
243 Matrix *M = Matrix_Alloc(Rays->NbRows+1, Rays->NbColumns+1);
244 for (int i = 0; i < Rays->NbRows; ++i) {
245 Vector_Copy(Rays->p[i], M->p[i]+1, Rays->NbColumns);
246 value_set_si(M->p[i][0], 1);
248 Vector_Set(M->p[Rays->NbRows]+1, 0, Rays->NbColumns-1);
249 value_set_si(M->p[Rays->NbRows][0], 1);
250 value_set_si(M->p[Rays->NbRows][Rays->NbColumns], 1);
251 Cone = Rays2Polyhedron(M, M->NbRows+1);
252 assert(Cone->NbConstraints == Cone->NbRays);
253 Matrix_Free(M);
255 return Cone;
258 ZZ det;
259 Polyhedron *Cone;
260 Matrix *Rays;
263 class dpoly {
264 public:
265 vec_ZZ coeff;
266 dpoly(int d, ZZ& degree, int offset = 0) {
267 coeff.SetLength(d+1);
269 int min = d + offset;
270 if (degree < ZZ(INIT_VAL, min))
271 min = to_int(degree);
273 ZZ c = ZZ(INIT_VAL, 1);
274 if (!offset)
275 coeff[0] = c;
276 for (int i = 1; i <= min; ++i) {
277 c *= (degree -i + 1);
278 c /= i;
279 coeff[i-offset] = c;
282 void operator *= (dpoly& f) {
283 assert(coeff.length() == f.coeff.length());
284 vec_ZZ old = coeff;
285 coeff = f.coeff[0] * coeff;
286 for (int i = 1; i < coeff.length(); ++i)
287 for (int j = 0; i+j < coeff.length(); ++j)
288 coeff[i+j] += f.coeff[i] * old[j];
290 void div(dpoly& d, mpq_t count, ZZ& sign) {
291 int len = coeff.length();
292 Value tmp;
293 value_init(tmp);
294 mpq_t* c = new mpq_t[coeff.length()];
295 mpq_t qtmp;
296 mpq_init(qtmp);
297 for (int i = 0; i < len; ++i) {
298 mpq_init(c[i]);
299 zz2value(coeff[i], tmp);
300 mpq_set_z(c[i], tmp);
302 for (int j = 1; j <= i; ++j) {
303 zz2value(d.coeff[j], tmp);
304 mpq_set_z(qtmp, tmp);
305 mpq_mul(qtmp, qtmp, c[i-j]);
306 mpq_sub(c[i], c[i], qtmp);
309 zz2value(d.coeff[0], tmp);
310 mpq_set_z(qtmp, tmp);
311 mpq_div(c[i], c[i], qtmp);
313 if (sign == -1)
314 mpq_sub(count, count, c[len-1]);
315 else
316 mpq_add(count, count, c[len-1]);
318 value_clear(tmp);
319 mpq_clear(qtmp);
320 for (int i = 0; i < len; ++i)
321 mpq_clear(c[i]);
322 delete [] c;
326 class dpoly_n {
327 public:
328 Matrix *coeff;
329 ~dpoly_n() {
330 Matrix_Free(coeff);
332 dpoly_n(int d, ZZ& degree_0, ZZ& degree_1, int offset = 0) {
333 Value d0, d1;
334 value_init(d0);
335 value_init(d1);
336 zz2value(degree_0, d0);
337 zz2value(degree_1, d1);
338 coeff = Matrix_Alloc(d+1, d+1+1);
339 value_set_si(coeff->p[0][0], 1);
340 value_set_si(coeff->p[0][d+1], 1);
341 for (int i = 1; i <= d; ++i) {
342 value_multiply(coeff->p[i][0], coeff->p[i-1][0], d0);
343 Vector_Combine(coeff->p[i-1], coeff->p[i-1]+1, coeff->p[i]+1,
344 d1, d0, i);
345 value_set_si(coeff->p[i][d+1], i);
346 value_multiply(coeff->p[i][d+1], coeff->p[i][d+1], coeff->p[i-1][d+1]);
347 value_decrement(d0, d0);
349 value_clear(d0);
350 value_clear(d1);
352 void div(dpoly& d, Vector *count, ZZ& sign) {
353 int len = coeff->NbRows;
354 Matrix * c = Matrix_Alloc(coeff->NbRows, coeff->NbColumns);
355 Value tmp;
356 value_init(tmp);
357 for (int i = 0; i < len; ++i) {
358 Vector_Copy(coeff->p[i], c->p[i], len+1);
359 for (int j = 1; j <= i; ++j) {
360 zz2value(d.coeff[j], tmp);
361 value_multiply(tmp, tmp, c->p[i][len]);
362 value_oppose(tmp, tmp);
363 Vector_Combine(c->p[i], c->p[i-j], c->p[i],
364 c->p[i-j][len], tmp, len);
365 value_multiply(c->p[i][len], c->p[i][len], c->p[i-j][len]);
367 zz2value(d.coeff[0], tmp);
368 value_multiply(c->p[i][len], c->p[i][len], tmp);
370 if (sign == -1) {
371 value_set_si(tmp, -1);
372 Vector_Scale(c->p[len-1], count->p, tmp, len);
373 value_assign(count->p[len], c->p[len-1][len]);
374 } else
375 Vector_Copy(c->p[len-1], count->p, len+1);
376 Vector_Normalize(count->p, len+1);
377 value_clear(tmp);
378 Matrix_Free(c);
383 * Barvinok's Decomposition of a simplicial cone
385 * Returns two lists of polyhedra
387 void barvinok_decompose(Polyhedron *C, Polyhedron **ppos, Polyhedron **pneg)
389 Polyhedron *pos = *ppos, *neg = *pneg;
390 vector<cone *> nonuni;
391 cone * c = new cone(C);
392 ZZ det = c->det;
393 int s = sign(det);
394 assert(det != 0);
395 if (abs(det) > 1) {
396 nonuni.push_back(c);
397 } else {
398 Polyhedron *p = Polyhedron_Copy(c->Cone);
399 p->next = pos;
400 pos = p;
401 delete c;
403 vec_ZZ lambda;
404 while (!nonuni.empty()) {
405 c = nonuni.back();
406 nonuni.pop_back();
407 Vector* v = c->short_vector(lambda);
408 for (int i = 0; i < c->Rays->NbRows - 1; ++i) {
409 if (lambda[i] == 0)
410 continue;
411 Matrix* M = Matrix_Copy(c->Rays);
412 Vector_Copy(v->p, M->p[i], v->Size);
413 cone * pc = new cone(M);
414 assert (pc->det != 0);
415 if (abs(pc->det) > 1) {
416 assert(abs(pc->det) < abs(c->det));
417 nonuni.push_back(pc);
418 } else {
419 Polyhedron *p = pc->poly();
420 pc->Cone = 0;
421 if (sign(pc->det) == s) {
422 p->next = pos;
423 pos = p;
424 } else {
425 p->next = neg;
426 neg = p;
428 delete pc;
430 Matrix_Free(M);
432 Vector_Free(v);
433 delete c;
435 *ppos = pos;
436 *pneg = neg;
440 * Returns a single list of npos "positive" cones followed by nneg
441 * "negative" cones.
442 * The input cone is freed
444 void decompose(Polyhedron *cone, Polyhedron **parts, int *npos, int *nneg, unsigned MaxRays)
446 Polyhedron_Polarize(cone);
447 if (cone->NbRays - 1 != cone->Dimension) {
448 Polyhedron *tmp = cone;
449 cone = triangularize_cone(cone, MaxRays);
450 Polyhedron_Free(tmp);
452 Polyhedron *polpos = NULL, *polneg = NULL;
453 *npos = 0; *nneg = 0;
454 for (Polyhedron *Polar = cone; Polar; Polar = Polar->next)
455 barvinok_decompose(Polar, &polpos, &polneg);
457 Polyhedron *last;
458 for (Polyhedron *i = polpos; i; i = i->next) {
459 Polyhedron_Polarize(i);
460 ++*npos;
461 last = i;
463 for (Polyhedron *i = polneg; i; i = i->next) {
464 Polyhedron_Polarize(i);
465 ++*nneg;
467 if (last) {
468 last->next = polneg;
469 *parts = polpos;
470 } else
471 *parts = polneg;
472 Domain_Free(cone);
475 const int MAX_TRY=10;
477 * Searches for a vector that is not othogonal to any
478 * of the rays in rays.
480 static void nonorthog(mat_ZZ& rays, vec_ZZ& lambda)
482 int dim = rays.NumCols();
483 bool found = false;
484 lambda.SetLength(dim);
485 for (int i = 2; !found && i <= 50*dim; i+=4) {
486 for (int j = 0; j < MAX_TRY; ++j) {
487 for (int k = 0; k < dim; ++k) {
488 int r = random_int(i)+2;
489 int v = (2*(r%2)-1) * (r >> 1);
490 lambda[k] = v;
492 int k = 0;
493 for (; k < rays.NumRows(); ++k)
494 if (lambda * rays[k] == 0)
495 break;
496 if (k == rays.NumRows()) {
497 found = true;
498 break;
502 assert(found);
505 static void add_rays(mat_ZZ& rays, Polyhedron *i, int *r)
507 unsigned dim = i->Dimension;
508 for (int k = 0; k < i->NbRays; ++k) {
509 if (!value_zero_p(i->Ray[k][dim+1]))
510 continue;
511 values2zz(i->Ray[k]+1, rays[(*r)++], dim);
515 void lattice_point(Value* values, Polyhedron *i, vec_ZZ& lambda, ZZ& num)
517 vec_ZZ vertex;
518 unsigned dim = i->Dimension;
519 if(!value_one_p(values[dim])) {
520 Matrix* Rays = rays(i);
521 Matrix *inv = Matrix_Alloc(Rays->NbRows, Rays->NbColumns);
522 int ok = Matrix_Inverse(Rays, inv);
523 assert(ok);
524 Matrix_Free(Rays);
525 Rays = rays(i);
526 Vector *lambda = Vector_Alloc(dim+1);
527 Vector_Matrix_Product(values, inv, lambda->p);
528 Matrix_Free(inv);
529 for (int j = 0; j < dim; ++j)
530 mpz_cdiv_q(lambda->p[j], lambda->p[j], lambda->p[dim]);
531 value_set_si(lambda->p[dim], 1);
532 Vector *A = Vector_Alloc(dim+1);
533 Vector_Matrix_Product(lambda->p, Rays, A->p);
534 Vector_Free(lambda);
535 Matrix_Free(Rays);
536 values2zz(A->p, vertex, dim);
537 Vector_Free(A);
538 } else
539 values2zz(values, vertex, dim);
541 num = vertex * lambda;
544 static evalue *term(int param, ZZ& c, Value *den = NULL)
546 evalue *EP = new evalue();
547 value_init(EP->d);
548 value_set_si(EP->d,0);
549 EP->x.p = new_enode(polynomial, 2, param + 1);
550 evalue_set_si(&EP->x.p->arr[0], 0, 1);
551 value_init(EP->x.p->arr[1].x.n);
552 if (den == NULL)
553 value_set_si(EP->x.p->arr[1].d, 1);
554 else
555 value_assign(EP->x.p->arr[1].d, *den);
556 zz2value(c, EP->x.p->arr[1].x.n);
557 return EP;
560 static void vertex_period(
561 Polyhedron *i, vec_ZZ& lambda, Matrix *T,
562 Value lcm, int p, Vector *val,
563 evalue *E, evalue* ev,
564 ZZ& offset)
566 unsigned nparam = T->NbRows - 1;
567 unsigned dim = i->Dimension;
568 Value tmp;
569 ZZ nump;
571 if (p == nparam) {
572 ZZ num, l;
573 Vector * values = Vector_Alloc(dim + 1);
574 Vector_Matrix_Product(val->p, T, values->p);
575 value_assign(values->p[dim], lcm);
576 lattice_point(values->p, i, lambda, num);
577 value2zz(lcm, l);
578 num *= l;
579 num += offset;
580 value_init(ev->x.n);
581 zz2value(num, ev->x.n);
582 value_assign(ev->d, lcm);
583 Vector_Free(values);
584 return;
587 value_init(tmp);
588 vec_ZZ vertex;
589 values2zz(T->p[p], vertex, dim);
590 nump = vertex * lambda;
591 if (First_Non_Zero(val->p, p) == -1) {
592 value_assign(tmp, lcm);
593 evalue *ET = term(p, nump, &tmp);
594 eadd(ET, E);
595 free_evalue_refs(ET);
596 delete ET;
599 value_assign(tmp, lcm);
600 if (First_Non_Zero(T->p[p], dim) != -1)
601 Vector_Gcd(T->p[p], dim, &tmp);
602 Gcd(tmp, lcm, &tmp);
603 if (value_lt(tmp, lcm)) {
604 ZZ count;
606 value_division(tmp, lcm, tmp);
607 value_set_si(ev->d, 0);
608 ev->x.p = new_enode(periodic, VALUE_TO_INT(tmp), p+1);
609 value2zz(tmp, count);
610 do {
611 value_decrement(tmp, tmp);
612 --count;
613 ZZ new_offset = offset - count * nump;
614 value_assign(val->p[p], tmp);
615 vertex_period(i, lambda, T, lcm, p+1, val, E,
616 &ev->x.p->arr[VALUE_TO_INT(tmp)], new_offset);
617 } while (value_pos_p(tmp));
618 } else
619 vertex_period(i, lambda, T, lcm, p+1, val, E, ev, offset);
620 value_clear(tmp);
623 static void mask_r(Matrix *f, int nr, Vector *lcm, int p, Vector *val, evalue *ev)
625 unsigned nparam = lcm->Size;
627 if (p == nparam) {
628 Vector * prod = Vector_Alloc(f->NbRows);
629 Matrix_Vector_Product(f, val->p, prod->p);
630 int isint = 1;
631 for (int i = 0; i < nr; ++i) {
632 value_modulus(prod->p[i], prod->p[i], f->p[i][nparam+1]);
633 isint &= value_zero_p(prod->p[i]);
635 value_set_si(ev->d, 1);
636 value_init(ev->x.n);
637 value_set_si(ev->x.n, isint);
638 Vector_Free(prod);
639 return;
642 Value tmp;
643 value_init(tmp);
644 if (value_one_p(lcm->p[p]))
645 mask_r(f, nr, lcm, p+1, val, ev);
646 else {
647 value_assign(tmp, lcm->p[p]);
648 value_set_si(ev->d, 0);
649 ev->x.p = new_enode(periodic, VALUE_TO_INT(tmp), p+1);
650 do {
651 value_decrement(tmp, tmp);
652 value_assign(val->p[p], tmp);
653 mask_r(f, nr, lcm, p+1, val, &ev->x.p->arr[VALUE_TO_INT(tmp)]);
654 } while (value_pos_p(tmp));
656 value_clear(tmp);
659 static evalue *multi_monom(vec_ZZ& p)
661 evalue *X = new evalue();
662 value_init(X->d);
663 value_init(X->x.n);
664 unsigned nparam = p.length()-1;
665 zz2value(p[nparam], X->x.n);
666 value_set_si(X->d, 1);
667 for (int i = 0; i < nparam; ++i) {
668 if (p[i] == 0)
669 continue;
670 evalue *T = term(i, p[i]);
671 eadd(T, X);
672 free_evalue_refs(T);
673 delete T;
675 return X;
679 * Check whether mapping polyhedron P on the affine combination
680 * num yields a range that has a fixed quotient on integer
681 * division by d
682 * If zero is true, then we are only interested in the quotient
683 * for the cases where the remainder is zero.
684 * Returns NULL if false and a newly allocated value if true.
686 static Value *fixed_quotient(Polyhedron *P, vec_ZZ& num, Value d, bool zero)
688 Value* ret = NULL;
689 int len = num.length();
690 Matrix *T = Matrix_Alloc(2, len);
691 zz2values(num, T->p[0]);
692 value_set_si(T->p[1][len-1], 1);
693 Polyhedron *I = Polyhedron_Image(P, T, P->NbConstraints);
694 Matrix_Free(T);
696 int i;
697 for (i = 0; i < I->NbRays; ++i)
698 if (value_zero_p(I->Ray[i][2])) {
699 Polyhedron_Free(I);
700 return NULL;
703 Value min, max;
704 value_init(min);
705 value_init(max);
706 if (I->NbEq >= 1) {
707 value_oppose(I->Constraint[0][2], I->Constraint[0][2]);
708 /* There should never be a remainder here */
709 if (value_pos_p(I->Constraint[0][1]))
710 mpz_fdiv_q(min, I->Constraint[0][2], I->Constraint[0][1]);
711 else
712 mpz_fdiv_q(min, I->Constraint[0][2], I->Constraint[0][1]);
713 value_assign(max, min);
714 } else for (i = 0; i < I->NbConstraints; ++i) {
715 value_oppose(I->Constraint[i][2], I->Constraint[i][2]);
716 if (value_pos_p(I->Constraint[i][1]))
717 mpz_cdiv_q(min, I->Constraint[i][2], I->Constraint[i][1]);
718 else
719 mpz_fdiv_q(max, I->Constraint[i][2], I->Constraint[i][1]);
721 Polyhedron_Free(I);
723 if (zero)
724 mpz_cdiv_q(min, min, d);
725 else
726 mpz_fdiv_q(min, min, d);
727 mpz_fdiv_q(max, max, d);
728 if (value_eq(min, max)) {
729 ALLOC(ret);
730 value_init(*ret);
731 value_assign(*ret, min);
733 value_clear(min);
734 value_clear(max);
735 return ret;
739 * Normalize linear expression coef modulo m
740 * Removes common factor and reduces coefficients
741 * Returns index of first non-zero coefficient or len
743 static int normal_mod(Value *coef, int len, Value *m)
745 Value gcd;
746 value_init(gcd);
748 Vector_Gcd(coef, len, &gcd);
749 Gcd(gcd, *m, &gcd);
750 Vector_AntiScale(coef, coef, gcd, len);
752 value_division(*m, *m, gcd);
753 value_clear(gcd);
755 if (value_one_p(*m))
756 return len;
758 int j;
759 for (j = 0; j < len; ++j)
760 mpz_fdiv_r(coef[j], coef[j], *m);
761 for (j = 0; j < len; ++j)
762 if (value_notzero_p(coef[j]))
763 break;
765 return j;
768 #ifdef USE_MODULO
769 static void mask(Matrix *f, evalue *factor)
771 int nr = f->NbRows, nc = f->NbColumns;
772 int n;
773 bool found = false;
774 for (n = 0; n < nr && value_notzero_p(f->p[n][nc-1]); ++n)
775 if (value_notone_p(f->p[n][nc-1]) &&
776 value_notmone_p(f->p[n][nc-1]))
777 found = true;
778 if (!found)
779 return;
781 evalue EP;
782 nr = n;
784 Value m;
785 value_init(m);
787 evalue EV;
788 value_init(EV.d);
789 value_init(EV.x.n);
790 value_set_si(EV.x.n, 1);
792 for (n = 0; n < nr; ++n) {
793 value_assign(m, f->p[n][nc-1]);
794 if (value_one_p(m) || value_mone_p(m))
795 continue;
797 int j = normal_mod(f->p[n], nc-1, &m);
798 if (j == nc-1) {
799 free_evalue_refs(factor);
800 value_init(factor->d);
801 evalue_set_si(factor, 0, 1);
802 break;
804 vec_ZZ row;
805 values2zz(f->p[n], row, nc-1);
806 ZZ g;
807 value2zz(m, g);
808 if (j < (nc-1)-1 && row[j] > g/2) {
809 for (int k = j; k < (nc-1); ++k)
810 if (row[k] != 0)
811 row[k] = g - row[k];
814 value_init(EP.d);
815 value_set_si(EP.d, 0);
816 EP.x.p = new_enode(relation, 2, 0);
817 value_clear(EP.x.p->arr[1].d);
818 EP.x.p->arr[1] = *factor;
819 evalue *ev = &EP.x.p->arr[0];
820 value_set_si(ev->d, 0);
821 ev->x.p = new_enode(fractional, 3, -1);
822 evalue_set_si(&ev->x.p->arr[1], 0, 1);
823 evalue_set_si(&ev->x.p->arr[2], 1, 1);
824 evalue *E = multi_monom(row);
825 value_assign(EV.d, m);
826 emul(&EV, E);
827 value_clear(ev->x.p->arr[0].d);
828 ev->x.p->arr[0] = *E;
829 delete E;
830 *factor = EP;
833 value_clear(m);
834 free_evalue_refs(&EV);
836 #else
840 static void mask(Matrix *f, evalue *factor)
842 int nr = f->NbRows, nc = f->NbColumns;
843 int n;
844 bool found = false;
845 for (n = 0; n < nr && value_notzero_p(f->p[n][nc-1]); ++n)
846 if (value_notone_p(f->p[n][nc-1]) &&
847 value_notmone_p(f->p[n][nc-1]))
848 found = true;
849 if (!found)
850 return;
852 Value tmp;
853 value_init(tmp);
854 nr = n;
855 unsigned np = nc - 2;
856 Vector *lcm = Vector_Alloc(np);
857 Vector *val = Vector_Alloc(nc);
858 Vector_Set(val->p, 0, nc);
859 value_set_si(val->p[np], 1);
860 Vector_Set(lcm->p, 1, np);
861 for (n = 0; n < nr; ++n) {
862 if (value_one_p(f->p[n][nc-1]) ||
863 value_mone_p(f->p[n][nc-1]))
864 continue;
865 for (int j = 0; j < np; ++j)
866 if (value_notzero_p(f->p[n][j])) {
867 Gcd(f->p[n][j], f->p[n][nc-1], &tmp);
868 value_division(tmp, f->p[n][nc-1], tmp);
869 value_lcm(tmp, lcm->p[j], &lcm->p[j]);
872 evalue EP;
873 value_init(EP.d);
874 mask_r(f, nr, lcm, 0, val, &EP);
875 value_clear(tmp);
876 Vector_Free(val);
877 Vector_Free(lcm);
878 emul(&EP,factor);
879 free_evalue_refs(&EP);
881 #endif
883 struct term_info {
884 evalue *E;
885 ZZ constant;
886 ZZ coeff;
887 int pos;
890 static bool mod_needed(Polyhedron *PD, vec_ZZ& num, Value d, evalue *E)
892 Value *q = fixed_quotient(PD, num, d, false);
894 if (!q)
895 return true;
897 value_oppose(*q, *q);
898 evalue EV;
899 value_init(EV.d);
900 value_set_si(EV.d, 1);
901 value_init(EV.x.n);
902 value_multiply(EV.x.n, *q, d);
903 eadd(&EV, E);
904 free_evalue_refs(&EV);
905 value_clear(*q);
906 free(q);
907 return false;
910 static void ceil_mod(Value *coef, int len, Value d, ZZ& f, evalue *EP, Polyhedron *PD)
912 Value m;
913 value_init(m);
914 value_set_si(m, -1);
916 Vector_Scale(coef, coef, m, len);
918 value_assign(m, d);
919 int j = normal_mod(coef, len, &m);
921 if (j == len) {
922 value_clear(m);
923 return;
926 vec_ZZ num;
927 values2zz(coef, num, len);
929 ZZ g;
930 value2zz(m, g);
932 evalue tmp;
933 value_init(tmp.d);
934 evalue_set_si(&tmp, 0, 1);
936 int p = j;
937 if (g % 2 == 0)
938 while (j < len-1 && (num[j] == g/2 || num[j] == 0))
939 ++j;
940 if (j < len-1 && num[j] > g/2) {
941 for (int k = j; k < len-1; ++k)
942 if (num[k] != 0)
943 num[k] = g - num[k];
944 num[len-1] = g - 1 - num[len-1];
945 value_assign(tmp.d, m);
946 ZZ t = f*(g-1);
947 zz2value(t, tmp.x.n);
948 eadd(&tmp, EP);
949 f = -f;
952 if (p >= len-1) {
953 ZZ t = num[len-1] * f;
954 zz2value(t, tmp.x.n);
955 value_assign(tmp.d, m);
956 eadd(&tmp, EP);
957 } else {
958 evalue *E = multi_monom(num);
959 evalue EV;
960 value_init(EV.d);
962 if (PD && !mod_needed(PD, num, m, E)) {
963 value_init(EV.x.n);
964 zz2value(f, EV.x.n);
965 value_assign(EV.d, m);
966 emul(&EV, E);
967 eadd(E, EP);
968 } else {
969 value_init(EV.x.n);
970 value_set_si(EV.x.n, 1);
971 value_assign(EV.d, m);
972 emul(&EV, E);
973 value_clear(EV.x.n);
974 value_set_si(EV.d, 0);
975 EV.x.p = new_enode(fractional, 3, -1);
976 evalue_copy(&EV.x.p->arr[0], E);
977 evalue_set_si(&EV.x.p->arr[1], 0, 1);
978 value_init(EV.x.p->arr[2].x.n);
979 zz2value(f, EV.x.p->arr[2].x.n);
980 value_set_si(EV.x.p->arr[2].d, 1);
982 eadd(&EV, EP);
985 free_evalue_refs(&EV);
986 free_evalue_refs(E);
987 delete E;
990 free_evalue_refs(&tmp);
992 out:
993 value_clear(m);
996 evalue* bv_ceil3(Value *coef, int len, Value d, Polyhedron *P)
998 Vector *val = Vector_Alloc(len);
1000 Value t;
1001 value_init(t);
1002 value_set_si(t, -1);
1003 Vector_Scale(coef, val->p, t, len);
1004 value_absolute(t, d);
1006 vec_ZZ num;
1007 values2zz(val->p, num, len);
1008 evalue *EP = multi_monom(num);
1010 evalue tmp;
1011 value_init(tmp.d);
1012 value_init(tmp.x.n);
1013 value_set_si(tmp.x.n, 1);
1014 value_assign(tmp.d, t);
1016 emul(&tmp, EP);
1018 ZZ one;
1019 one = 1;
1020 ceil_mod(val->p, len, t, one, EP, P);
1021 value_clear(t);
1023 /* copy EP to malloc'ed evalue */
1024 evalue *E;
1025 ALLOC(E);
1026 *E = *EP;
1027 delete EP;
1029 free_evalue_refs(&tmp);
1030 Vector_Free(val);
1032 return E;
1035 #ifdef USE_MODULO
1036 evalue* lattice_point(
1037 Polyhedron *i, vec_ZZ& lambda, Matrix *W, Value lcm, Polyhedron *PD)
1039 unsigned nparam = W->NbColumns - 1;
1041 Matrix* Rays = rays2(i);
1042 Matrix *T = Transpose(Rays);
1043 Matrix *T2 = Matrix_Copy(T);
1044 Matrix *inv = Matrix_Alloc(T2->NbRows, T2->NbColumns);
1045 int ok = Matrix_Inverse(T2, inv);
1046 assert(ok);
1047 Matrix_Free(Rays);
1048 Matrix_Free(T2);
1049 mat_ZZ vertex;
1050 matrix2zz(W, vertex, W->NbRows, W->NbColumns);
1052 vec_ZZ num;
1053 num = lambda * vertex;
1055 evalue *EP = multi_monom(num);
1057 evalue tmp;
1058 value_init(tmp.d);
1059 value_init(tmp.x.n);
1060 value_set_si(tmp.x.n, 1);
1061 value_assign(tmp.d, lcm);
1063 emul(&tmp, EP);
1065 Matrix *L = Matrix_Alloc(inv->NbRows, W->NbColumns);
1066 Matrix_Product(inv, W, L);
1068 mat_ZZ RT;
1069 matrix2zz(T, RT, T->NbRows, T->NbColumns);
1070 Matrix_Free(T);
1072 vec_ZZ p = lambda * RT;
1074 for (int i = 0; i < L->NbRows; ++i) {
1075 ceil_mod(L->p[i], nparam+1, lcm, p[i], EP, PD);
1078 Matrix_Free(L);
1080 Matrix_Free(inv);
1081 free_evalue_refs(&tmp);
1082 return EP;
1084 #else
1085 evalue* lattice_point(
1086 Polyhedron *i, vec_ZZ& lambda, Matrix *W, Value lcm, Polyhedron *PD)
1088 Matrix *T = Transpose(W);
1089 unsigned nparam = T->NbRows - 1;
1091 evalue *EP = new evalue();
1092 value_init(EP->d);
1093 evalue_set_si(EP, 0, 1);
1095 evalue ev;
1096 Vector *val = Vector_Alloc(nparam+1);
1097 value_set_si(val->p[nparam], 1);
1098 ZZ offset(INIT_VAL, 0);
1099 value_init(ev.d);
1100 vertex_period(i, lambda, T, lcm, 0, val, EP, &ev, offset);
1101 Vector_Free(val);
1102 eadd(&ev, EP);
1103 free_evalue_refs(&ev);
1105 Matrix_Free(T);
1107 reduce_evalue(EP);
1109 return EP;
1111 #endif
1113 void lattice_point(
1114 Param_Vertices* V, Polyhedron *i, vec_ZZ& lambda, term_info* term,
1115 Polyhedron *PD)
1117 unsigned nparam = V->Vertex->NbColumns - 2;
1118 unsigned dim = i->Dimension;
1119 mat_ZZ vertex;
1120 vertex.SetDims(V->Vertex->NbRows, nparam+1);
1121 Value lcm, tmp;
1122 value_init(lcm);
1123 value_init(tmp);
1124 value_set_si(lcm, 1);
1125 for (int j = 0; j < V->Vertex->NbRows; ++j) {
1126 value_lcm(lcm, V->Vertex->p[j][nparam+1], &lcm);
1128 if (value_notone_p(lcm)) {
1129 Matrix * mv = Matrix_Alloc(dim, nparam+1);
1130 for (int j = 0 ; j < dim; ++j) {
1131 value_division(tmp, lcm, V->Vertex->p[j][nparam+1]);
1132 Vector_Scale(V->Vertex->p[j], mv->p[j], tmp, nparam+1);
1135 term->E = lattice_point(i, lambda, mv, lcm, PD);
1136 term->constant = 0;
1138 Matrix_Free(mv);
1139 value_clear(lcm);
1140 value_clear(tmp);
1141 return;
1143 for (int i = 0; i < V->Vertex->NbRows; ++i) {
1144 assert(value_one_p(V->Vertex->p[i][nparam+1])); // for now
1145 values2zz(V->Vertex->p[i], vertex[i], nparam+1);
1148 vec_ZZ num;
1149 num = lambda * vertex;
1151 int p = -1;
1152 int nn = 0;
1153 for (int j = 0; j < nparam; ++j)
1154 if (num[j] != 0) {
1155 ++nn;
1156 p = j;
1158 if (nn >= 2) {
1159 term->E = multi_monom(num);
1160 term->constant = 0;
1161 } else {
1162 term->E = NULL;
1163 term->constant = num[nparam];
1164 term->pos = p;
1165 if (p != -1)
1166 term->coeff = num[p];
1169 value_clear(lcm);
1170 value_clear(tmp);
1173 void normalize(Polyhedron *i, vec_ZZ& lambda, ZZ& sign, ZZ& num, vec_ZZ& den)
1175 unsigned dim = i->Dimension;
1177 int r = 0;
1178 mat_ZZ rays;
1179 rays.SetDims(dim, dim);
1180 add_rays(rays, i, &r);
1181 den = rays * lambda;
1182 int change = 0;
1184 for (int j = 0; j < den.length(); ++j) {
1185 if (den[j] > 0)
1186 change ^= 1;
1187 else {
1188 den[j] = abs(den[j]);
1189 num += den[j];
1192 if (change)
1193 sign = -sign;
1196 void barvinok_count(Polyhedron *P, Value* result, unsigned NbMaxCons)
1198 Polyhedron ** vcone;
1199 vec_ZZ sign;
1200 int ncone = 0;
1201 sign.SetLength(ncone);
1202 unsigned dim;
1203 int allocated = 0;
1204 Value factor;
1205 Polyhedron *Q;
1206 int r = 0;
1208 if (emptyQ(P)) {
1209 value_set_si(*result, 0);
1210 return;
1212 if (P->NbBid == 0)
1213 for (; r < P->NbRays; ++r)
1214 if (value_zero_p(P->Ray[r][P->Dimension+1]))
1215 break;
1216 if (P->NbBid !=0 || r < P->NbRays) {
1217 value_set_si(*result, -1);
1218 return;
1220 if (P->NbEq != 0) {
1221 P = remove_equalities(P);
1222 if (emptyQ(P)) {
1223 Polyhedron_Free(P);
1224 value_set_si(*result, 0);
1225 return;
1227 allocated = 1;
1229 value_init(factor);
1230 value_set_si(factor, 1);
1231 Q = Polyhedron_Reduce(P, &factor);
1232 if (Q) {
1233 if (allocated)
1234 Polyhedron_Free(P);
1235 P = Q;
1236 allocated = 1;
1238 if (P->Dimension == 0) {
1239 value_assign(*result, factor);
1240 if (allocated)
1241 Polyhedron_Free(P);
1242 value_clear(factor);
1243 return;
1246 dim = P->Dimension;
1247 vcone = new (Polyhedron *)[P->NbRays];
1249 for (int j = 0; j < P->NbRays; ++j) {
1250 int npos, nneg;
1251 Polyhedron *C = supporting_cone(P, j);
1252 decompose(C, &vcone[j], &npos, &nneg, NbMaxCons);
1253 ncone += npos + nneg;
1254 sign.SetLength(ncone);
1255 for (int k = 0; k < npos; ++k)
1256 sign[ncone-nneg-k-1] = 1;
1257 for (int k = 0; k < nneg; ++k)
1258 sign[ncone-k-1] = -1;
1261 mat_ZZ rays;
1262 rays.SetDims(ncone * dim, dim);
1263 r = 0;
1264 for (int j = 0; j < P->NbRays; ++j) {
1265 for (Polyhedron *i = vcone[j]; i; i = i->next) {
1266 assert(i->NbRays-1 == dim);
1267 add_rays(rays, i, &r);
1270 vec_ZZ lambda;
1271 nonorthog(rays, lambda);
1273 vec_ZZ num;
1274 mat_ZZ den;
1275 num.SetLength(ncone);
1276 den.SetDims(ncone,dim);
1278 int f = 0;
1279 for (int j = 0; j < P->NbRays; ++j) {
1280 for (Polyhedron *i = vcone[j]; i; i = i->next) {
1281 lattice_point(P->Ray[j]+1, i, lambda, num[f]);
1282 normalize(i, lambda, sign[f], num[f], den[f]);
1283 ++f;
1286 ZZ min = num[0];
1287 for (int j = 1; j < num.length(); ++j)
1288 if (num[j] < min)
1289 min = num[j];
1290 for (int j = 0; j < num.length(); ++j)
1291 num[j] -= min;
1293 f = 0;
1294 mpq_t count;
1295 mpq_init(count);
1296 for (int j = 0; j < P->NbRays; ++j) {
1297 for (Polyhedron *i = vcone[j]; i; i = i->next) {
1298 dpoly d(dim, num[f]);
1299 dpoly n(dim, den[f][0], 1);
1300 for (int k = 1; k < dim; ++k) {
1301 dpoly fact(dim, den[f][k], 1);
1302 n *= fact;
1304 d.div(n, count, sign[f]);
1305 ++f;
1308 assert(value_one_p(&count[0]._mp_den));
1309 value_multiply(*result, &count[0]._mp_num, factor);
1310 mpq_clear(count);
1312 for (int j = 0; j < P->NbRays; ++j)
1313 Domain_Free(vcone[j]);
1315 delete [] vcone;
1317 if (allocated)
1318 Polyhedron_Free(P);
1319 value_clear(factor);
1322 static void uni_polynom(int param, Vector *c, evalue *EP)
1324 unsigned dim = c->Size-2;
1325 value_init(EP->d);
1326 value_set_si(EP->d,0);
1327 EP->x.p = new_enode(polynomial, dim+1, param+1);
1328 for (int j = 0; j <= dim; ++j)
1329 evalue_set(&EP->x.p->arr[j], c->p[j], c->p[dim+1]);
1332 static void multi_polynom(Vector *c, evalue* X, evalue *EP)
1334 unsigned dim = c->Size-2;
1335 evalue EC;
1337 value_init(EC.d);
1338 evalue_set(&EC, c->p[dim], c->p[dim+1]);
1340 value_init(EP->d);
1341 evalue_set(EP, c->p[dim], c->p[dim+1]);
1343 for (int i = dim-1; i >= 0; --i) {
1344 emul(X, EP);
1345 value_assign(EC.x.n, c->p[i]);
1346 eadd(&EC, EP);
1348 free_evalue_refs(&EC);
1351 Polyhedron *unfringe (Polyhedron *P, unsigned MaxRays)
1353 int len = P->Dimension+2;
1354 Polyhedron *T, *R = P;
1355 Value g;
1356 value_init(g);
1357 puts("in");
1358 Polyhedron_Print(stdout, P_VALUE_FMT, P);
1359 Vector *row = Vector_Alloc(len);
1360 value_set_si(row->p[0], 1);
1362 R = DomainConstraintSimplify(Polyhedron_Copy(P), MaxRays);
1364 Matrix *M = Matrix_Alloc(2, len-1);
1365 value_set_si(M->p[1][len-2], 1);
1366 for (int v = 0; v < P->Dimension; ++v) {
1367 value_set_si(M->p[0][v], 1);
1368 Polyhedron *I = Polyhedron_Image(P, M, 2+1);
1369 Polyhedron_Print(stdout, P_VALUE_FMT, I);
1370 value_set_si(M->p[0][v], 0);
1371 for (int r = 0; r < I->NbConstraints; ++r) {
1372 if (value_zero_p(I->Constraint[r][0]))
1373 continue;
1374 if (value_zero_p(I->Constraint[r][1]))
1375 continue;
1376 if (value_one_p(I->Constraint[r][1]))
1377 continue;
1378 if (value_mone_p(I->Constraint[r][1]))
1379 continue;
1380 value_absolute(g, I->Constraint[r][1]);
1381 Vector_Set(row->p+1, 0, len-2);
1382 value_division(row->p[1+v], I->Constraint[r][1], g);
1383 mpz_fdiv_q(row->p[len-1], I->Constraint[r][2], g);
1384 puts("row");
1385 Vector_Print(stdout, P_VALUE_FMT, row);
1386 T = R;
1387 R = AddConstraints(row->p, 1, R, MaxRays);
1388 if (T != P)
1389 Polyhedron_Free(T);
1392 puts("out");
1393 Polyhedron_Print(stdout, P_VALUE_FMT, R);
1394 value_clear(g);
1395 return R;
1398 evalue* barvinok_enumerate_ev(Polyhedron *P, Polyhedron* C, unsigned MaxRays)
1400 //P = unfringe(P, MaxRays);
1401 Polyhedron *CEq = NULL, *rVD, *pVD, *fVD, *CA;
1402 Matrix *CT = NULL;
1403 Param_Polyhedron *PP = NULL;
1404 Param_Domain *D, *next;
1405 Param_Vertices *V;
1406 int r = 0;
1407 unsigned nparam = C->Dimension;
1408 evalue *eres;
1409 ALLOC(eres);
1410 value_init(eres->d);
1411 value_set_si(eres->d, 0);
1413 evalue factor;
1414 value_init(factor.d);
1415 evalue_set_si(&factor, 1, 1);
1417 CA = align_context(C, P->Dimension, MaxRays);
1418 P = DomainIntersection(P, CA, MaxRays);
1419 Polyhedron_Free(CA);
1421 if (C->Dimension == 0 || emptyQ(P)) {
1422 constant:
1423 eres->x.p = new_enode(partition, 2, -1);
1424 EVALUE_SET_DOMAIN(eres->x.p->arr[0],
1425 DomainConstraintSimplify(CEq ? CEq : Polyhedron_Copy(C), MaxRays));
1426 value_set_si(eres->x.p->arr[1].d, 1);
1427 value_init(eres->x.p->arr[1].x.n);
1428 if (emptyQ(P))
1429 value_set_si(eres->x.p->arr[1].x.n, 0);
1430 else
1431 barvinok_count(P, &eres->x.p->arr[1].x.n, MaxRays);
1432 out:
1433 emul(&factor, eres);
1434 reduce_evalue(eres);
1435 free_evalue_refs(&factor);
1436 Polyhedron_Free(P);
1437 if (CT)
1438 Matrix_Free(CT);
1439 if (PP)
1440 Param_Polyhedron_Free(PP);
1442 return eres;
1444 for (r = 0; r < P->NbRays; ++r)
1445 if (value_zero_p(P->Ray[r][0]) ||
1446 value_zero_p(P->Ray[r][P->Dimension+1])) {
1447 int i;
1448 for (i = P->Dimension - nparam; i < P->Dimension; ++i)
1449 if (value_notzero_p(P->Ray[r][i+1]))
1450 break;
1451 if (i >= P->Dimension)
1452 break;
1454 if (r < P->NbRays)
1455 goto constant;
1457 if (P->NbEq != 0) {
1458 Matrix *f;
1459 P = remove_equalities_p(P, P->Dimension-nparam, &f);
1460 mask(f, &factor);
1461 Matrix_Free(f);
1462 if (P->Dimension == nparam) {
1463 CEq = P;
1464 P = Universe_Polyhedron(0);
1465 goto constant;
1469 Polyhedron *Q = ParamPolyhedron_Reduce(P, P->Dimension-nparam, &factor);
1470 if (Q) {
1471 Polyhedron_Free(P);
1472 if (Q->Dimension == nparam) {
1473 CEq = Q;
1474 P = Universe_Polyhedron(0);
1475 goto constant;
1477 P = Q;
1479 Polyhedron *oldP = P;
1480 PP = Polyhedron2Param_SimplifiedDomain(&P,C,MaxRays,&CEq,&CT);
1481 if (P != oldP)
1482 Polyhedron_Free(oldP);
1484 if (isIdentity(CT)) {
1485 Matrix_Free(CT);
1486 CT = NULL;
1487 } else {
1488 assert(CT->NbRows != CT->NbColumns);
1489 if (CT->NbRows == 1) // no more parameters
1490 goto constant;
1491 nparam = CT->NbRows - 1;
1494 unsigned dim = P->Dimension - nparam;
1495 Polyhedron ** vcone = new (Polyhedron *)[PP->nbV];
1496 int * npos = new int[PP->nbV];
1497 int * nneg = new int[PP->nbV];
1498 vec_ZZ sign;
1500 int i;
1501 for (i = 0, V = PP->V; V; ++i, V = V->next) {
1502 Polyhedron *C = supporting_cone_p(P, V);
1503 decompose(C, &vcone[i], &npos[i], &nneg[i], MaxRays);
1506 Vector *c = Vector_Alloc(dim+2);
1508 int nd;
1509 for (nd = 0, D=PP->D; D; ++nd, D=D->next);
1510 struct section { Polyhedron *D; Polyhedron *full; evalue E; };
1511 section *s = new section[nd];
1513 for(nd = 0, D=PP->D; D; D=next) {
1514 next = D->next;
1515 if (!CEq) {
1516 fVD = Domain_Copy(D->Domain);
1517 D->Domain = DomainConstraintSimplify(D->Domain, MaxRays);
1518 pVD = rVD = D->Domain;
1519 D->Domain = NULL;
1520 } else {
1521 Polyhedron *Dt;
1522 Dt = CT ? DomainPreimage(D->Domain,CT,MaxRays) : D->Domain;
1523 rVD = DomainIntersection(Dt,CEq,MaxRays);
1525 /* if rVD is empty or too small in geometric dimension */
1526 if(!rVD || emptyQ(rVD) ||
1527 (rVD->Dimension-rVD->NbEq < Dt->Dimension-Dt->NbEq-CEq->NbEq)) {
1528 if(rVD)
1529 Domain_Free(rVD);
1530 if (CT)
1531 Domain_Free(Dt);
1532 continue; /* empty validity domain */
1535 if (CT)
1536 Domain_Free(Dt);
1538 fVD = Domain_Copy(rVD);
1539 for (int i = 0 ; i < nd; ++i) {
1540 Polyhedron *I = DomainIntersection(fVD, s[i].full, MaxRays);
1541 if (emptyQ(I)) {
1542 Domain_Free(I);
1543 continue;
1545 Polyhedron *F = DomainSimplify(I, fVD, MaxRays);
1546 if (F->NbEq == 1) {
1547 Polyhedron *T = rVD;
1548 rVD = DomainDifference(rVD, F, MaxRays);
1549 Domain_Free(T);
1551 Domain_Free(F);
1552 Domain_Free(I);
1554 rVD = DomainConstraintSimplify(rVD, MaxRays);
1555 if (emptyQ(rVD)) {
1556 Domain_Free(rVD);
1557 continue;
1559 pVD = CT ? DomainImage(rVD,CT,MaxRays) : rVD;
1561 int ncone = 0;
1562 sign.SetLength(ncone);
1563 FORALL_PVertex_in_ParamPolyhedron(V,D,PP) // _i is internal counter
1564 ncone += npos[_i] + nneg[_i];
1565 sign.SetLength(ncone);
1566 for (int k = 0; k < npos[_i]; ++k)
1567 sign[ncone-nneg[_i]-k-1] = 1;
1568 for (int k = 0; k < nneg[_i]; ++k)
1569 sign[ncone-k-1] = -1;
1570 END_FORALL_PVertex_in_ParamPolyhedron;
1572 mat_ZZ rays;
1573 rays.SetDims(ncone * dim, dim);
1574 r = 0;
1575 FORALL_PVertex_in_ParamPolyhedron(V,D,PP) // _i is internal counter
1576 for (Polyhedron *i = vcone[_i]; i; i = i->next) {
1577 assert(i->NbRays-1 == dim);
1578 add_rays(rays, i, &r);
1580 END_FORALL_PVertex_in_ParamPolyhedron;
1581 vec_ZZ lambda;
1582 nonorthog(rays, lambda);
1584 mat_ZZ den;
1585 den.SetDims(ncone,dim);
1586 term_info *num = new term_info[ncone];
1588 int f = 0;
1589 FORALL_PVertex_in_ParamPolyhedron(V,D,PP)
1590 for (Polyhedron *i = vcone[_i]; i; i = i->next) {
1591 lattice_point(V, i, lambda, &num[f], pVD);
1592 normalize(i, lambda, sign[f], num[f].constant, den[f]);
1593 ++f;
1595 END_FORALL_PVertex_in_ParamPolyhedron;
1596 ZZ min = num[0].constant;
1597 for (int j = 1; j < ncone; ++j)
1598 if (num[j].constant < min)
1599 min = num[j].constant;
1600 for (int j = 0; j < ncone; ++j)
1601 num[j].constant -= min;
1602 f = 0;
1603 value_init(s[nd].E.d);
1604 evalue_set_si(&s[nd].E, 0, 1);
1605 mpq_t count;
1606 mpq_init(count);
1607 FORALL_PVertex_in_ParamPolyhedron(V,D,PP)
1608 for (Polyhedron *i = vcone[_i]; i; i = i->next) {
1609 dpoly n(dim, den[f][0], 1);
1610 for (int k = 1; k < dim; ++k) {
1611 dpoly fact(dim, den[f][k], 1);
1612 n *= fact;
1614 if (num[f].E != NULL) {
1615 ZZ one(INIT_VAL, 1);
1616 dpoly_n d(dim, num[f].constant, one);
1617 d.div(n, c, sign[f]);
1618 evalue EV;
1619 multi_polynom(c, num[f].E, &EV);
1620 eadd(&EV , &s[nd].E);
1621 free_evalue_refs(&EV);
1622 free_evalue_refs(num[f].E);
1623 delete num[f].E;
1624 } else if (num[f].pos != -1) {
1625 dpoly_n d(dim, num[f].constant, num[f].coeff);
1626 d.div(n, c, sign[f]);
1627 evalue EV;
1628 uni_polynom(num[f].pos, c, &EV);
1629 eadd(&EV , &s[nd].E);
1630 free_evalue_refs(&EV);
1631 } else {
1632 mpq_set_si(count, 0, 1);
1633 dpoly d(dim, num[f].constant);
1634 d.div(n, count, sign[f]);
1635 evalue EV;
1636 value_init(EV.d);
1637 evalue_set(&EV, &count[0]._mp_num, &count[0]._mp_den);
1638 eadd(&EV , &s[nd].E);
1639 free_evalue_refs(&EV);
1641 ++f;
1643 END_FORALL_PVertex_in_ParamPolyhedron;
1645 mpq_clear(count);
1646 delete [] num;
1648 if (CT)
1649 addeliminatedparams_evalue(&s[nd].E, CT);
1650 s[nd].D = rVD;
1651 s[nd].full = fVD;
1652 ++nd;
1653 if (rVD != pVD)
1654 Domain_Free(pVD);
1657 eres->x.p = new_enode(partition, 2*nd, -1);
1658 for (int j = 0; j < nd; ++j) {
1659 EVALUE_SET_DOMAIN(eres->x.p->arr[2*j], s[j].D);
1660 value_clear(eres->x.p->arr[2*j+1].d);
1661 eres->x.p->arr[2*j+1] = s[j].E;
1662 Domain_Free(s[j].full);
1664 delete [] s;
1666 Vector_Free(c);
1668 for (int j = 0; j < PP->nbV; ++j)
1669 Domain_Free(vcone[j]);
1670 delete [] vcone;
1671 delete [] npos;
1672 delete [] nneg;
1674 if (CEq)
1675 Polyhedron_Free(CEq);
1677 goto out;
1680 Enumeration* barvinok_enumerate(Polyhedron *P, Polyhedron* C, unsigned MaxRays)
1682 Enumeration *en, *res = NULL;
1683 evalue *EP = barvinok_enumerate_ev(P, C, MaxRays);
1684 for (int i = 0; i < EP->x.p->size/2; ++i) {
1685 en = (Enumeration *)malloc(sizeof(Enumeration));
1686 en->next = res;
1687 res = en;
1688 res->ValidityDomain = EVALUE_DOMAIN(EP->x.p->arr[2*i]);
1689 value_clear(EP->x.p->arr[2*i].d);
1690 res->EP = EP->x.p->arr[2*i+1];
1692 free(EP->x.p);
1693 value_clear(EP->d);
1694 free(EP);
1695 return res;
1698 static void SwapColumns(Value **V, int n, int i, int j)
1700 for (int r = 0; r < n; ++r)
1701 value_swap(V[r][i], V[r][j]);
1704 static void SwapColumns(Polyhedron *P, int i, int j)
1706 SwapColumns(P->Constraint, P->NbConstraints, i, j);
1707 SwapColumns(P->Ray, P->NbRays, i, j);
1710 enum constraint {
1711 ALL_POS = 1 << 0,
1712 ONE_NEG = 1 << 1,
1713 INDEPENDENT = 1 << 2,
1716 evalue* barvinok_enumerate_e(Polyhedron *P,
1717 unsigned exist, unsigned nparam, unsigned MaxRays)
1719 //Polyhedron_Print(stderr, P_VALUE_FMT, P);
1720 if (exist == 0) {
1721 Polyhedron *U = Universe_Polyhedron(nparam);
1722 evalue *EP = barvinok_enumerate_ev(P, U, MaxRays);
1723 //char *param_name[] = {"P", "Q", "R", "S", "T" };
1724 //print_evalue(stdout, EP, param_name);
1725 Polyhedron_Free(U);
1726 return EP;
1729 int nvar = P->Dimension - exist - nparam;
1730 int len = P->Dimension + 2;
1732 //printf("%d %d %d\n", nvar, exist, nparam);
1734 int r;
1735 int first;
1736 for (r = 0; r < P->NbEq; ++r)
1737 if ((first = First_Non_Zero(P->Constraint[r]+1+nvar, exist)) != -1)
1738 break;
1739 if (r < P->NbEq) {
1740 if (First_Non_Zero(P->Constraint[r]+1+nvar+first+1,
1741 exist-first-1) != -1) {
1742 Value g;
1743 value_init(g);
1745 Vector *row = Vector_Alloc(exist);
1746 Vector_Copy(P->Constraint[r]+1+nvar, row->p, exist);
1747 Vector_Gcd(row->p, exist, &g);
1748 if (value_notone_p(g))
1749 Vector_AntiScale(row->p, row->p, g, exist);
1750 value_clear(g);
1752 Matrix *M = unimodular_complete(row);
1753 Matrix *M2 = Matrix_Alloc(P->Dimension+1, P->Dimension+1);
1754 for (r = 0; r < nvar; ++r)
1755 value_set_si(M2->p[r][r], 1);
1756 for ( ; r < nvar+exist; ++r)
1757 Vector_Copy(M->p[r-nvar], M2->p[r]+nvar, exist);
1758 for ( ; r < P->Dimension+1; ++r)
1759 value_set_si(M2->p[r][r], 1);
1760 Polyhedron *T = Polyhedron_Image(P, M2, MaxRays);
1761 evalue *EP = barvinok_enumerate_e(T, exist-1, nparam, MaxRays);
1762 Polyhedron_Free(T);
1763 Matrix_Free(M2);
1764 Matrix_Free(M);
1765 Vector_Free(row);
1766 return EP;
1767 } else {
1768 if (first == 0)
1769 return barvinok_enumerate_e(P, exist-1, nparam, MaxRays);
1770 else {
1771 Polyhedron *T = Polyhedron_Copy(P);
1772 SwapColumns(T, nvar+1, nvar+1+first);
1773 evalue *EP = barvinok_enumerate_e(T, exist-1, nparam, MaxRays);
1774 Polyhedron_Free(T);
1775 return EP;
1780 Vector *row = Vector_Alloc(len);
1781 value_set_si(row->p[0], 1);
1783 Value f;
1784 value_init(f);
1786 enum constraint info[exist];
1787 for (int i = 0; i < exist; ++i) {
1788 info[i] = ALL_POS;
1789 for (int l = P->NbEq; l < P->NbConstraints; ++l) {
1790 if (value_negz_p(P->Constraint[l][nvar+i+1]))
1791 continue;
1792 for (int u = P->NbEq; u < P->NbConstraints; ++u) {
1793 if (value_posz_p(P->Constraint[u][nvar+i+1]))
1794 continue;
1795 value_oppose(f, P->Constraint[u][nvar+i+1]);
1796 Vector_Combine(P->Constraint[l]+1, P->Constraint[u]+1, row->p+1,
1797 f, P->Constraint[l][nvar+i+1], len-1);
1798 if (!(info[i] & INDEPENDENT)) {
1799 int j;
1800 for (j = 0; j < exist; ++j)
1801 if (j != i && value_notzero_p(row->p[nvar+j+1]))
1802 break;
1803 if (j == exist) {
1804 //printf("independent: i: %d, l: %d, u: %d\n", i, l, u);
1805 info[i] = (constraint)(info[i] | INDEPENDENT);
1808 if (info[i] & ALL_POS) {
1809 value_addto(row->p[len-1], row->p[len-1],
1810 P->Constraint[l][nvar+i+1]);
1811 value_addto(row->p[len-1], row->p[len-1], f);
1812 value_multiply(f, f, P->Constraint[l][nvar+i+1]);
1813 value_substract(row->p[len-1], row->p[len-1], f);
1814 value_decrement(row->p[len-1], row->p[len-1]);
1815 Vector_Gcd(row->p+1, len - 2, &f);
1816 if (value_notone_p(f)) {
1817 Vector_AntiScale(row->p+1, row->p+1, f, len-2);
1818 mpz_fdiv_q(row->p[len-1], row->p[len-1], f);
1820 value_set_si(f, -1);
1821 Vector_Scale(row->p+1, row->p+1, f, len-1);
1822 value_decrement(row->p[len-1], row->p[len-1]);
1823 Polyhedron *T = AddConstraints(row->p, 1, P, MaxRays);
1824 if (!emptyQ(T)) {
1825 //printf("not all_pos: i: %d, l: %d, u: %d\n", i, l, u);
1826 info[i] = (constraint)(info[i] ^ ALL_POS);
1828 //puts("pos remainder");
1829 //Polyhedron_Print(stdout, P_VALUE_FMT, T);
1830 Polyhedron_Free(T);
1832 if (!(info[i] & ONE_NEG)) {
1833 int j;
1834 for (j = 0; j < exist; ++j)
1835 if (j != i &&
1836 (value_notzero_p(P->Constraint[l][nvar+j+1]) ||
1837 value_notzero_p(P->Constraint[u][nvar+j+1])))
1838 break;
1839 if (j == exist) {
1840 /* recalculate constant */
1841 /* We actually recalculate the whole row for
1842 * now, because it may have already been scaled
1844 value_oppose(f, P->Constraint[u][nvar+i+1]);
1845 Vector_Combine(P->Constraint[l]+1, P->Constraint[u]+1,
1846 row->p+1,
1847 f, P->Constraint[l][nvar+i+1], len-1);
1849 Vector_Combine(P->Constraint[l]+len-1,
1850 P->Constraint[u]+len-1, row->p+len-1,
1851 f, P->Constraint[l][nvar+i+1], 1);
1853 value_multiply(f, f, P->Constraint[l][nvar+i+1]);
1854 value_substract(row->p[len-1], row->p[len-1], f);
1855 value_set_si(f, -1);
1856 Vector_Scale(row->p+1, row->p+1, f, len-1);
1857 value_decrement(row->p[len-1], row->p[len-1]);
1858 Vector_Gcd(row->p+1, len - 2, &f);
1859 if (value_notone_p(f)) {
1860 Vector_AntiScale(row->p+1, row->p+1, f, len-2);
1861 mpz_fdiv_q(row->p[len-1], row->p[len-1], f);
1863 value_set_si(f, -1);
1864 Vector_Scale(row->p+1, row->p+1, f, len-1);
1865 value_decrement(row->p[len-1], row->p[len-1]);
1866 //puts("row");
1867 //Vector_Print(stdout, P_VALUE_FMT, row);
1868 Polyhedron *T = AddConstraints(row->p, 1, P, MaxRays);
1869 if (emptyQ(T)) {
1870 //printf("one_neg i: %d, l: %d, u: %d\n", i, l, u);
1871 info[i] = (constraint)(info[i] | ONE_NEG);
1873 //puts("neg remainder");
1874 //Polyhedron_Print(stdout, P_VALUE_FMT, T);
1875 Polyhedron_Free(T);
1878 if (!(info[i] & ALL_POS) && (info[i] & ONE_NEG))
1879 goto next;
1882 if (info[i] & ALL_POS)
1883 break;
1884 next:
1889 for (int i = 0; i < exist; ++i)
1890 printf("%i: %i\n", i, info[i]);
1892 for (int i = 0; i < exist; ++i)
1893 if (info[i] & ALL_POS) {
1894 // Eliminate
1895 // Maybe we should chew off some of the fat here
1896 Matrix *M = Matrix_Alloc(P->Dimension, P->Dimension+1);
1897 for (int j = 0; j < P->Dimension; ++j)
1898 value_set_si(M->p[j][j + (j >= i+nvar)], 1);
1899 Polyhedron *T = Polyhedron_Image(P, M, MaxRays);
1900 Matrix_Free(M);
1901 evalue *EP = barvinok_enumerate_e(T, exist-1, nparam, MaxRays);
1902 Polyhedron_Free(T);
1903 value_clear(f);
1904 Vector_Free(row);
1905 return EP;
1907 for (int i = 0; i < exist; ++i)
1908 if (info[i] & ONE_NEG) {
1909 Vector_Free(row);
1910 value_clear(f);
1911 if (i == 0)
1912 return barvinok_enumerate_e(P, exist-1, nparam, MaxRays);
1913 else {
1914 Polyhedron *T = Polyhedron_Copy(P);
1915 SwapColumns(T, nvar+1, nvar+1+i);
1916 evalue *EP = barvinok_enumerate_e(T, exist-1, nparam, MaxRays);
1917 Polyhedron_Free(T);
1918 return EP;
1921 for (int i = 0; i < exist; ++i)
1922 if (info[i] & INDEPENDENT) {
1923 /* Find constraint again and split off negative part */
1925 for (int l = P->NbEq; l < P->NbConstraints; ++l) {
1926 if (value_negz_p(P->Constraint[l][nvar+i+1]))
1927 continue;
1928 for (int u = P->NbEq; u < P->NbConstraints; ++u) {
1929 if (value_posz_p(P->Constraint[u][nvar+i+1]))
1930 continue;
1931 value_oppose(f, P->Constraint[u][nvar+i+1]);
1932 Vector_Combine(P->Constraint[l]+1, P->Constraint[u]+1,
1933 row->p+1,
1934 f, P->Constraint[l][nvar+i+1], len-1);
1936 int j;
1937 for (j = 0; j < exist; ++j)
1938 if (j != i && value_notzero_p(row->p[nvar+j+1]))
1939 break;
1940 if (j != exist)
1941 continue;
1943 //printf("l: %d, u: %d\n", l, u);
1944 value_multiply(f, f, P->Constraint[l][nvar+i+1]);
1945 value_substract(row->p[len-1], row->p[len-1], f);
1946 value_set_si(f, -1);
1947 Vector_Scale(row->p+1, row->p+1, f, len-1);
1948 value_decrement(row->p[len-1], row->p[len-1]);
1949 Vector_Gcd(row->p+1, len - 2, &f);
1950 if (value_notone_p(f)) {
1951 Vector_AntiScale(row->p+1, row->p+1, f, len-2);
1952 mpz_fdiv_q(row->p[len-1], row->p[len-1], f);
1954 Polyhedron *neg = AddConstraints(row->p, 1, P, MaxRays);
1955 value_set_si(f, -1);
1956 Vector_Scale(row->p+1, row->p+1, f, len-1);
1957 value_decrement(row->p[len-1], row->p[len-1]);
1958 Polyhedron *pos = AddConstraints(row->p, 1, P, MaxRays);
1960 assert(i == 0); // for now
1961 evalue *EP =
1962 barvinok_enumerate_e(neg, exist-1, nparam, MaxRays);
1963 evalue *E =
1964 barvinok_enumerate_e(pos, exist, nparam, MaxRays);
1965 eadd(E, EP);
1966 free_evalue_refs(E);
1967 free(E);
1968 Polyhedron_Free(neg);
1969 Polyhedron_Free(pos);
1970 value_clear(f);
1971 Vector_Free(row);
1972 return EP;
1975 assert(0); // can't happen
1978 assert(0);