array contraction
[barvinok.git] / barvinok.cc
blob32e0540d18948bf7b457c565ff8dcc778017b801
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;
500 * Barvinok's Decomposition of a simplicial cone
502 * Returns two lists of polyhedra
504 void barvinok_decompose(Polyhedron *C, Polyhedron **ppos, Polyhedron **pneg)
506 Polyhedron *pos = *ppos, *neg = *pneg;
507 vector<cone *> nonuni;
508 cone * c = new cone(C);
509 ZZ det = c->det;
510 int s = sign(det);
511 assert(det != 0);
512 if (abs(det) > 1) {
513 nonuni.push_back(c);
514 } else {
515 Polyhedron *p = Polyhedron_Copy(c->Cone);
516 p->next = pos;
517 pos = p;
518 delete c;
520 vec_ZZ lambda;
521 while (!nonuni.empty()) {
522 c = nonuni.back();
523 nonuni.pop_back();
524 Vector* v = c->short_vector(lambda);
525 for (int i = 0; i < c->Rays->NbRows - 1; ++i) {
526 if (lambda[i] == 0)
527 continue;
528 Matrix* M = Matrix_Copy(c->Rays);
529 Vector_Copy(v->p, M->p[i], v->Size);
530 cone * pc = new cone(M);
531 assert (pc->det != 0);
532 if (abs(pc->det) > 1) {
533 assert(abs(pc->det) < abs(c->det));
534 nonuni.push_back(pc);
535 } else {
536 Polyhedron *p = pc->poly();
537 pc->Cone = 0;
538 if (sign(pc->det) == s) {
539 p->next = pos;
540 pos = p;
541 } else {
542 p->next = neg;
543 neg = p;
545 delete pc;
547 Matrix_Free(M);
549 Vector_Free(v);
550 delete c;
552 *ppos = pos;
553 *pneg = neg;
557 * Returns a single list of npos "positive" cones followed by nneg
558 * "negative" cones.
559 * The input cone is freed
561 void decompose(Polyhedron *cone, Polyhedron **parts, int *npos, int *nneg, unsigned MaxRays)
563 Polyhedron_Polarize(cone);
564 if (cone->NbRays - 1 != cone->Dimension) {
565 Polyhedron *tmp = cone;
566 cone = triangularize_cone(cone, MaxRays);
567 Polyhedron_Free(tmp);
569 Polyhedron *polpos = NULL, *polneg = NULL;
570 *npos = 0; *nneg = 0;
571 for (Polyhedron *Polar = cone; Polar; Polar = Polar->next)
572 barvinok_decompose(Polar, &polpos, &polneg);
574 Polyhedron *last;
575 for (Polyhedron *i = polpos; i; i = i->next) {
576 Polyhedron_Polarize(i);
577 ++*npos;
578 last = i;
580 for (Polyhedron *i = polneg; i; i = i->next) {
581 Polyhedron_Polarize(i);
582 ++*nneg;
584 if (last) {
585 last->next = polneg;
586 *parts = polpos;
587 } else
588 *parts = polneg;
589 Domain_Free(cone);
592 const int MAX_TRY=10;
594 * Searches for a vector that is not orthogonal to any
595 * of the rays in rays.
597 static void nonorthog(mat_ZZ& rays, vec_ZZ& lambda)
599 int dim = rays.NumCols();
600 bool found = false;
601 lambda.SetLength(dim);
602 if (dim == 0)
603 return;
605 for (int i = 2; !found && i <= 50*dim; i+=4) {
606 for (int j = 0; j < MAX_TRY; ++j) {
607 for (int k = 0; k < dim; ++k) {
608 int r = random_int(i)+2;
609 int v = (2*(r%2)-1) * (r >> 1);
610 lambda[k] = v;
612 int k = 0;
613 for (; k < rays.NumRows(); ++k)
614 if (lambda * rays[k] == 0)
615 break;
616 if (k == rays.NumRows()) {
617 found = true;
618 break;
622 assert(found);
625 static void randomvector(Polyhedron *P, vec_ZZ& lambda, int nvar)
627 Value tmp;
628 int max = 10;
629 unsigned int dim = P->Dimension;
630 value_init(tmp);
632 for (int i = 0; i < P->NbRays; ++i) {
633 for (int j = 1; j <= dim; ++j) {
634 value_absolute(tmp, P->Ray[i][j]);
635 int t = VALUE_TO_LONG(tmp);
636 if (t > max)
637 max = t;
640 for (int i = 0; i < P->NbConstraints; ++i) {
641 for (int j = 1; j <= dim; ++j) {
642 value_absolute(tmp, P->Constraint[i][j]);
643 int t = VALUE_TO_LONG(tmp);
644 if (t > max)
645 max = t;
648 value_clear(tmp);
650 lambda.SetLength(nvar);
651 for (int k = 0; k < nvar; ++k) {
652 int r = random_int(8*max*dim)+2;
653 int v = (2*(r%2)-1) * (4*max*dim + (r >> 1));
654 lambda[k] = v;
658 static void add_rays(mat_ZZ& rays, Polyhedron *i, int *r, int nvar = -1,
659 bool all = false)
661 unsigned dim = i->Dimension;
662 if (nvar == -1)
663 nvar = dim;
664 for (int k = 0; k < i->NbRays; ++k) {
665 if (!value_zero_p(i->Ray[k][dim+1]))
666 continue;
667 if (!all && nvar != dim && First_Non_Zero(i->Ray[k]+1, nvar) == -1)
668 continue;
669 values2zz(i->Ray[k]+1, rays[(*r)++], nvar);
673 void lattice_point(Value* values, Polyhedron *i, vec_ZZ& vertex)
675 unsigned dim = i->Dimension;
676 if(!value_one_p(values[dim])) {
677 Matrix* Rays = rays(i);
678 Matrix *inv = Matrix_Alloc(Rays->NbRows, Rays->NbColumns);
679 int ok = Matrix_Inverse(Rays, inv);
680 assert(ok);
681 Matrix_Free(Rays);
682 Rays = rays(i);
683 Vector *lambda = Vector_Alloc(dim+1);
684 Vector_Matrix_Product(values, inv, lambda->p);
685 Matrix_Free(inv);
686 for (int j = 0; j < dim; ++j)
687 mpz_cdiv_q(lambda->p[j], lambda->p[j], lambda->p[dim]);
688 value_set_si(lambda->p[dim], 1);
689 Vector *A = Vector_Alloc(dim+1);
690 Vector_Matrix_Product(lambda->p, Rays, A->p);
691 Vector_Free(lambda);
692 Matrix_Free(Rays);
693 values2zz(A->p, vertex, dim);
694 Vector_Free(A);
695 } else
696 values2zz(values, vertex, dim);
699 static evalue *term(int param, ZZ& c, Value *den = NULL)
701 evalue *EP = new evalue();
702 value_init(EP->d);
703 value_set_si(EP->d,0);
704 EP->x.p = new_enode(polynomial, 2, param + 1);
705 evalue_set_si(&EP->x.p->arr[0], 0, 1);
706 value_init(EP->x.p->arr[1].x.n);
707 if (den == NULL)
708 value_set_si(EP->x.p->arr[1].d, 1);
709 else
710 value_assign(EP->x.p->arr[1].d, *den);
711 zz2value(c, EP->x.p->arr[1].x.n);
712 return EP;
715 static void vertex_period(
716 Polyhedron *i, vec_ZZ& lambda, Matrix *T,
717 Value lcm, int p, Vector *val,
718 evalue *E, evalue* ev,
719 ZZ& offset)
721 unsigned nparam = T->NbRows - 1;
722 unsigned dim = i->Dimension;
723 Value tmp;
724 ZZ nump;
726 if (p == nparam) {
727 vec_ZZ vertex;
728 ZZ num, l;
729 Vector * values = Vector_Alloc(dim + 1);
730 Vector_Matrix_Product(val->p, T, values->p);
731 value_assign(values->p[dim], lcm);
732 lattice_point(values->p, i, vertex);
733 num = vertex * lambda;
734 value2zz(lcm, l);
735 num *= l;
736 num += offset;
737 value_init(ev->x.n);
738 zz2value(num, ev->x.n);
739 value_assign(ev->d, lcm);
740 Vector_Free(values);
741 return;
744 value_init(tmp);
745 vec_ZZ vertex;
746 values2zz(T->p[p], vertex, dim);
747 nump = vertex * lambda;
748 if (First_Non_Zero(val->p, p) == -1) {
749 value_assign(tmp, lcm);
750 evalue *ET = term(p, nump, &tmp);
751 eadd(ET, E);
752 free_evalue_refs(ET);
753 delete ET;
756 value_assign(tmp, lcm);
757 if (First_Non_Zero(T->p[p], dim) != -1)
758 Vector_Gcd(T->p[p], dim, &tmp);
759 Gcd(tmp, lcm, &tmp);
760 if (value_lt(tmp, lcm)) {
761 ZZ count;
763 value_division(tmp, lcm, tmp);
764 value_set_si(ev->d, 0);
765 ev->x.p = new_enode(periodic, VALUE_TO_INT(tmp), p+1);
766 value2zz(tmp, count);
767 do {
768 value_decrement(tmp, tmp);
769 --count;
770 ZZ new_offset = offset - count * nump;
771 value_assign(val->p[p], tmp);
772 vertex_period(i, lambda, T, lcm, p+1, val, E,
773 &ev->x.p->arr[VALUE_TO_INT(tmp)], new_offset);
774 } while (value_pos_p(tmp));
775 } else
776 vertex_period(i, lambda, T, lcm, p+1, val, E, ev, offset);
777 value_clear(tmp);
780 static void mask_r(Matrix *f, int nr, Vector *lcm, int p, Vector *val, evalue *ev)
782 unsigned nparam = lcm->Size;
784 if (p == nparam) {
785 Vector * prod = Vector_Alloc(f->NbRows);
786 Matrix_Vector_Product(f, val->p, prod->p);
787 int isint = 1;
788 for (int i = 0; i < nr; ++i) {
789 value_modulus(prod->p[i], prod->p[i], f->p[i][nparam+1]);
790 isint &= value_zero_p(prod->p[i]);
792 value_set_si(ev->d, 1);
793 value_init(ev->x.n);
794 value_set_si(ev->x.n, isint);
795 Vector_Free(prod);
796 return;
799 Value tmp;
800 value_init(tmp);
801 if (value_one_p(lcm->p[p]))
802 mask_r(f, nr, lcm, p+1, val, ev);
803 else {
804 value_assign(tmp, lcm->p[p]);
805 value_set_si(ev->d, 0);
806 ev->x.p = new_enode(periodic, VALUE_TO_INT(tmp), p+1);
807 do {
808 value_decrement(tmp, tmp);
809 value_assign(val->p[p], tmp);
810 mask_r(f, nr, lcm, p+1, val, &ev->x.p->arr[VALUE_TO_INT(tmp)]);
811 } while (value_pos_p(tmp));
813 value_clear(tmp);
816 static evalue *multi_monom(vec_ZZ& p)
818 evalue *X = new evalue();
819 value_init(X->d);
820 value_init(X->x.n);
821 unsigned nparam = p.length()-1;
822 zz2value(p[nparam], X->x.n);
823 value_set_si(X->d, 1);
824 for (int i = 0; i < nparam; ++i) {
825 if (p[i] == 0)
826 continue;
827 evalue *T = term(i, p[i]);
828 eadd(T, X);
829 free_evalue_refs(T);
830 delete T;
832 return X;
836 * Check whether mapping polyhedron P on the affine combination
837 * num yields a range that has a fixed quotient on integer
838 * division by d
839 * If zero is true, then we are only interested in the quotient
840 * for the cases where the remainder is zero.
841 * Returns NULL if false and a newly allocated value if true.
843 static Value *fixed_quotient(Polyhedron *P, vec_ZZ& num, Value d, bool zero)
845 Value* ret = NULL;
846 int len = num.length();
847 Matrix *T = Matrix_Alloc(2, len);
848 zz2values(num, T->p[0]);
849 value_set_si(T->p[1][len-1], 1);
850 Polyhedron *I = Polyhedron_Image(P, T, P->NbConstraints);
851 Matrix_Free(T);
853 int i;
854 for (i = 0; i < I->NbRays; ++i)
855 if (value_zero_p(I->Ray[i][2])) {
856 Polyhedron_Free(I);
857 return NULL;
860 Value min, max;
861 value_init(min);
862 value_init(max);
863 int bounded = line_minmax(I, &min, &max);
864 assert(bounded);
866 if (zero)
867 mpz_cdiv_q(min, min, d);
868 else
869 mpz_fdiv_q(min, min, d);
870 mpz_fdiv_q(max, max, d);
872 if (value_eq(min, max)) {
873 ALLOC(Value, ret);
874 value_init(*ret);
875 value_assign(*ret, min);
877 value_clear(min);
878 value_clear(max);
879 return ret;
883 * Normalize linear expression coef modulo m
884 * Removes common factor and reduces coefficients
885 * Returns index of first non-zero coefficient or len
887 static int normal_mod(Value *coef, int len, Value *m)
889 Value gcd;
890 value_init(gcd);
892 Vector_Gcd(coef, len, &gcd);
893 Gcd(gcd, *m, &gcd);
894 Vector_AntiScale(coef, coef, gcd, len);
896 value_division(*m, *m, gcd);
897 value_clear(gcd);
899 if (value_one_p(*m))
900 return len;
902 int j;
903 for (j = 0; j < len; ++j)
904 mpz_fdiv_r(coef[j], coef[j], *m);
905 for (j = 0; j < len; ++j)
906 if (value_notzero_p(coef[j]))
907 break;
909 return j;
912 #ifdef USE_MODULO
913 static void mask(Matrix *f, evalue *factor)
915 int nr = f->NbRows, nc = f->NbColumns;
916 int n;
917 bool found = false;
918 for (n = 0; n < nr && value_notzero_p(f->p[n][nc-1]); ++n)
919 if (value_notone_p(f->p[n][nc-1]) &&
920 value_notmone_p(f->p[n][nc-1]))
921 found = true;
922 if (!found)
923 return;
925 evalue EP;
926 nr = n;
928 Value m;
929 value_init(m);
931 evalue EV;
932 value_init(EV.d);
933 value_init(EV.x.n);
934 value_set_si(EV.x.n, 1);
936 for (n = 0; n < nr; ++n) {
937 value_assign(m, f->p[n][nc-1]);
938 if (value_one_p(m) || value_mone_p(m))
939 continue;
941 int j = normal_mod(f->p[n], nc-1, &m);
942 if (j == nc-1) {
943 free_evalue_refs(factor);
944 value_init(factor->d);
945 evalue_set_si(factor, 0, 1);
946 break;
948 vec_ZZ row;
949 values2zz(f->p[n], row, nc-1);
950 ZZ g;
951 value2zz(m, g);
952 if (j < (nc-1)-1 && row[j] > g/2) {
953 for (int k = j; k < (nc-1); ++k)
954 if (row[k] != 0)
955 row[k] = g - row[k];
958 value_init(EP.d);
959 value_set_si(EP.d, 0);
960 EP.x.p = new_enode(relation, 2, 0);
961 value_clear(EP.x.p->arr[1].d);
962 EP.x.p->arr[1] = *factor;
963 evalue *ev = &EP.x.p->arr[0];
964 value_set_si(ev->d, 0);
965 ev->x.p = new_enode(fractional, 3, -1);
966 evalue_set_si(&ev->x.p->arr[1], 0, 1);
967 evalue_set_si(&ev->x.p->arr[2], 1, 1);
968 evalue *E = multi_monom(row);
969 value_assign(EV.d, m);
970 emul(&EV, E);
971 value_clear(ev->x.p->arr[0].d);
972 ev->x.p->arr[0] = *E;
973 delete E;
974 *factor = EP;
977 value_clear(m);
978 free_evalue_refs(&EV);
980 #else
984 static void mask(Matrix *f, evalue *factor)
986 int nr = f->NbRows, nc = f->NbColumns;
987 int n;
988 bool found = false;
989 for (n = 0; n < nr && value_notzero_p(f->p[n][nc-1]); ++n)
990 if (value_notone_p(f->p[n][nc-1]) &&
991 value_notmone_p(f->p[n][nc-1]))
992 found = true;
993 if (!found)
994 return;
996 Value tmp;
997 value_init(tmp);
998 nr = n;
999 unsigned np = nc - 2;
1000 Vector *lcm = Vector_Alloc(np);
1001 Vector *val = Vector_Alloc(nc);
1002 Vector_Set(val->p, 0, nc);
1003 value_set_si(val->p[np], 1);
1004 Vector_Set(lcm->p, 1, np);
1005 for (n = 0; n < nr; ++n) {
1006 if (value_one_p(f->p[n][nc-1]) ||
1007 value_mone_p(f->p[n][nc-1]))
1008 continue;
1009 for (int j = 0; j < np; ++j)
1010 if (value_notzero_p(f->p[n][j])) {
1011 Gcd(f->p[n][j], f->p[n][nc-1], &tmp);
1012 value_division(tmp, f->p[n][nc-1], tmp);
1013 value_lcm(tmp, lcm->p[j], &lcm->p[j]);
1016 evalue EP;
1017 value_init(EP.d);
1018 mask_r(f, nr, lcm, 0, val, &EP);
1019 value_clear(tmp);
1020 Vector_Free(val);
1021 Vector_Free(lcm);
1022 emul(&EP,factor);
1023 free_evalue_refs(&EP);
1025 #endif
1027 struct term_info {
1028 evalue *E;
1029 ZZ constant;
1030 ZZ coeff;
1031 int pos;
1034 static bool mod_needed(Polyhedron *PD, vec_ZZ& num, Value d, evalue *E)
1036 Value *q = fixed_quotient(PD, num, d, false);
1038 if (!q)
1039 return true;
1041 value_oppose(*q, *q);
1042 evalue EV;
1043 value_init(EV.d);
1044 value_set_si(EV.d, 1);
1045 value_init(EV.x.n);
1046 value_multiply(EV.x.n, *q, d);
1047 eadd(&EV, E);
1048 free_evalue_refs(&EV);
1049 value_clear(*q);
1050 free(q);
1051 return false;
1054 static void ceil_mod(Value *coef, int len, Value d, ZZ& f, evalue *EP, Polyhedron *PD)
1056 Value m;
1057 value_init(m);
1058 value_set_si(m, -1);
1060 Vector_Scale(coef, coef, m, len);
1062 value_assign(m, d);
1063 int j = normal_mod(coef, len, &m);
1065 if (j == len) {
1066 value_clear(m);
1067 return;
1070 vec_ZZ num;
1071 values2zz(coef, num, len);
1073 ZZ g;
1074 value2zz(m, g);
1076 evalue tmp;
1077 value_init(tmp.d);
1078 evalue_set_si(&tmp, 0, 1);
1080 int p = j;
1081 if (g % 2 == 0)
1082 while (j < len-1 && (num[j] == g/2 || num[j] == 0))
1083 ++j;
1084 if ((j < len-1 && num[j] > g/2) || (j == len-1 && num[j] >= (g+1)/2)) {
1085 for (int k = j; k < len-1; ++k)
1086 if (num[k] != 0)
1087 num[k] = g - num[k];
1088 num[len-1] = g - 1 - num[len-1];
1089 value_assign(tmp.d, m);
1090 ZZ t = f*(g-1);
1091 zz2value(t, tmp.x.n);
1092 eadd(&tmp, EP);
1093 f = -f;
1096 if (p >= len-1) {
1097 ZZ t = num[len-1] * f;
1098 zz2value(t, tmp.x.n);
1099 value_assign(tmp.d, m);
1100 eadd(&tmp, EP);
1101 } else {
1102 evalue *E = multi_monom(num);
1103 evalue EV;
1104 value_init(EV.d);
1106 if (PD && !mod_needed(PD, num, m, E)) {
1107 value_init(EV.x.n);
1108 zz2value(f, EV.x.n);
1109 value_assign(EV.d, m);
1110 emul(&EV, E);
1111 eadd(E, EP);
1112 } else {
1113 value_init(EV.x.n);
1114 value_set_si(EV.x.n, 1);
1115 value_assign(EV.d, m);
1116 emul(&EV, E);
1117 value_clear(EV.x.n);
1118 value_set_si(EV.d, 0);
1119 EV.x.p = new_enode(fractional, 3, -1);
1120 evalue_copy(&EV.x.p->arr[0], E);
1121 evalue_set_si(&EV.x.p->arr[1], 0, 1);
1122 value_init(EV.x.p->arr[2].x.n);
1123 zz2value(f, EV.x.p->arr[2].x.n);
1124 value_set_si(EV.x.p->arr[2].d, 1);
1126 eadd(&EV, EP);
1129 free_evalue_refs(&EV);
1130 free_evalue_refs(E);
1131 delete E;
1134 free_evalue_refs(&tmp);
1136 out:
1137 value_clear(m);
1140 evalue* bv_ceil3(Value *coef, int len, Value d, Polyhedron *P)
1142 Vector *val = Vector_Alloc(len);
1144 Value t;
1145 value_init(t);
1146 value_set_si(t, -1);
1147 Vector_Scale(coef, val->p, t, len);
1148 value_absolute(t, d);
1150 vec_ZZ num;
1151 values2zz(val->p, num, len);
1152 evalue *EP = multi_monom(num);
1154 evalue tmp;
1155 value_init(tmp.d);
1156 value_init(tmp.x.n);
1157 value_set_si(tmp.x.n, 1);
1158 value_assign(tmp.d, t);
1160 emul(&tmp, EP);
1162 ZZ one;
1163 one = 1;
1164 ceil_mod(val->p, len, t, one, EP, P);
1165 value_clear(t);
1167 /* copy EP to malloc'ed evalue */
1168 evalue *E;
1169 ALLOC(evalue, E);
1170 *E = *EP;
1171 delete EP;
1173 free_evalue_refs(&tmp);
1174 Vector_Free(val);
1176 return E;
1179 #ifdef USE_MODULO
1180 evalue* lattice_point(
1181 Polyhedron *i, vec_ZZ& lambda, Matrix *W, Value lcm, Polyhedron *PD)
1183 unsigned nparam = W->NbColumns - 1;
1185 Matrix* Rays = rays2(i);
1186 Matrix *T = Transpose(Rays);
1187 Matrix *T2 = Matrix_Copy(T);
1188 Matrix *inv = Matrix_Alloc(T2->NbRows, T2->NbColumns);
1189 int ok = Matrix_Inverse(T2, inv);
1190 assert(ok);
1191 Matrix_Free(Rays);
1192 Matrix_Free(T2);
1193 mat_ZZ vertex;
1194 matrix2zz(W, vertex, W->NbRows, W->NbColumns);
1196 vec_ZZ num;
1197 num = lambda * vertex;
1199 evalue *EP = multi_monom(num);
1201 evalue tmp;
1202 value_init(tmp.d);
1203 value_init(tmp.x.n);
1204 value_set_si(tmp.x.n, 1);
1205 value_assign(tmp.d, lcm);
1207 emul(&tmp, EP);
1209 Matrix *L = Matrix_Alloc(inv->NbRows, W->NbColumns);
1210 Matrix_Product(inv, W, L);
1212 mat_ZZ RT;
1213 matrix2zz(T, RT, T->NbRows, T->NbColumns);
1214 Matrix_Free(T);
1216 vec_ZZ p = lambda * RT;
1218 for (int i = 0; i < L->NbRows; ++i) {
1219 ceil_mod(L->p[i], nparam+1, lcm, p[i], EP, PD);
1222 Matrix_Free(L);
1224 Matrix_Free(inv);
1225 free_evalue_refs(&tmp);
1226 return EP;
1228 #else
1229 evalue* lattice_point(
1230 Polyhedron *i, vec_ZZ& lambda, Matrix *W, Value lcm, Polyhedron *PD)
1232 Matrix *T = Transpose(W);
1233 unsigned nparam = T->NbRows - 1;
1235 evalue *EP = new evalue();
1236 value_init(EP->d);
1237 evalue_set_si(EP, 0, 1);
1239 evalue ev;
1240 Vector *val = Vector_Alloc(nparam+1);
1241 value_set_si(val->p[nparam], 1);
1242 ZZ offset(INIT_VAL, 0);
1243 value_init(ev.d);
1244 vertex_period(i, lambda, T, lcm, 0, val, EP, &ev, offset);
1245 Vector_Free(val);
1246 eadd(&ev, EP);
1247 free_evalue_refs(&ev);
1249 Matrix_Free(T);
1251 reduce_evalue(EP);
1253 return EP;
1255 #endif
1257 void lattice_point(
1258 Param_Vertices* V, Polyhedron *i, vec_ZZ& lambda, term_info* term,
1259 Polyhedron *PD)
1261 unsigned nparam = V->Vertex->NbColumns - 2;
1262 unsigned dim = i->Dimension;
1263 mat_ZZ vertex;
1264 vertex.SetDims(V->Vertex->NbRows, nparam+1);
1265 Value lcm, tmp;
1266 value_init(lcm);
1267 value_init(tmp);
1268 value_set_si(lcm, 1);
1269 for (int j = 0; j < V->Vertex->NbRows; ++j) {
1270 value_lcm(lcm, V->Vertex->p[j][nparam+1], &lcm);
1272 if (value_notone_p(lcm)) {
1273 Matrix * mv = Matrix_Alloc(dim, nparam+1);
1274 for (int j = 0 ; j < dim; ++j) {
1275 value_division(tmp, lcm, V->Vertex->p[j][nparam+1]);
1276 Vector_Scale(V->Vertex->p[j], mv->p[j], tmp, nparam+1);
1279 term->E = lattice_point(i, lambda, mv, lcm, PD);
1280 term->constant = 0;
1282 Matrix_Free(mv);
1283 value_clear(lcm);
1284 value_clear(tmp);
1285 return;
1287 for (int i = 0; i < V->Vertex->NbRows; ++i) {
1288 assert(value_one_p(V->Vertex->p[i][nparam+1])); // for now
1289 values2zz(V->Vertex->p[i], vertex[i], nparam+1);
1292 vec_ZZ num;
1293 num = lambda * vertex;
1295 int p = -1;
1296 int nn = 0;
1297 for (int j = 0; j < nparam; ++j)
1298 if (num[j] != 0) {
1299 ++nn;
1300 p = j;
1302 if (nn >= 2) {
1303 term->E = multi_monom(num);
1304 term->constant = 0;
1305 } else {
1306 term->E = NULL;
1307 term->constant = num[nparam];
1308 term->pos = p;
1309 if (p != -1)
1310 term->coeff = num[p];
1313 value_clear(lcm);
1314 value_clear(tmp);
1317 void normalize(Polyhedron *i, vec_ZZ& lambda, ZZ& sign, ZZ& num, vec_ZZ& den)
1319 unsigned dim = i->Dimension;
1321 int r = 0;
1322 mat_ZZ rays;
1323 rays.SetDims(dim, dim);
1324 add_rays(rays, i, &r);
1325 den = rays * lambda;
1326 int change = 0;
1328 for (int j = 0; j < den.length(); ++j) {
1329 if (den[j] > 0)
1330 change ^= 1;
1331 else {
1332 den[j] = abs(den[j]);
1333 num += den[j];
1336 if (change)
1337 sign = -sign;
1340 typedef Polyhedron * Polyhedron_p;
1342 void barvinok_count(Polyhedron *P, Value* result, unsigned NbMaxCons)
1344 Polyhedron ** vcone;
1345 ZZ sign;
1346 int ncone = 0;
1347 unsigned dim;
1348 int allocated = 0;
1349 Value factor;
1350 Polyhedron *Q;
1351 int r = 0;
1353 if (emptyQ(P)) {
1354 value_set_si(*result, 0);
1355 return;
1357 if (P->NbBid == 0)
1358 for (; r < P->NbRays; ++r)
1359 if (value_zero_p(P->Ray[r][P->Dimension+1]))
1360 break;
1361 if (P->NbBid !=0 || r < P->NbRays) {
1362 value_set_si(*result, -1);
1363 return;
1365 if (P->NbEq != 0) {
1366 P = remove_equalities(P);
1367 if (emptyQ(P)) {
1368 Polyhedron_Free(P);
1369 value_set_si(*result, 0);
1370 return;
1372 allocated = 1;
1374 value_init(factor);
1375 value_set_si(factor, 1);
1376 Q = Polyhedron_Reduce(P, &factor);
1377 if (Q) {
1378 if (allocated)
1379 Polyhedron_Free(P);
1380 P = Q;
1381 allocated = 1;
1383 if (P->Dimension == 0) {
1384 value_assign(*result, factor);
1385 if (allocated)
1386 Polyhedron_Free(P);
1387 value_clear(factor);
1388 return;
1391 dim = P->Dimension;
1393 vec_ZZ lambda;
1394 //nonorthog(rays, lambda);
1395 randomvector(P, lambda, dim);
1396 //cout << "lambda: " << lambda << endl;
1398 mat_ZZ rays;
1399 rays.SetDims(dim, dim);
1401 ZZ num;
1402 vec_ZZ den;
1403 den.SetLength(dim);
1405 vec_ZZ vertex;
1406 mpq_t count;
1407 mpq_init(count);
1409 for (int j = 0; j < P->NbRays; ++j) {
1410 Polyhedron *vcone;
1411 int npos, nneg;
1412 Polyhedron *C = supporting_cone(P, j);
1413 decompose(C, &vcone, &npos, &nneg, NbMaxCons);
1414 ncone += npos + nneg;
1416 Polyhedron *i;
1417 int l;
1418 for (i = vcone, l = 0; i; i = i->next, ++l) {
1419 r = 0;
1420 assert(i->NbRays-1 == dim);
1421 add_rays(rays, i, &r);
1422 for (int k = 0; k < dim; ++k) {
1423 assert(lambda * rays[k] != 0);
1426 sign = (l < npos) ? 1 : -1;
1428 lattice_point(P->Ray[j]+1, i, vertex);
1429 num = vertex * lambda;
1430 normalize(i, lambda, sign, num, den);
1432 dpoly d(dim, num);
1433 dpoly n(dim, den[0], 1);
1434 for (int k = 1; k < dim; ++k) {
1435 dpoly fact(dim, den[k], 1);
1436 n *= fact;
1438 d.div(n, count, sign);
1440 Domain_Free(vcone);
1443 assert(value_one_p(&count[0]._mp_den));
1444 value_multiply(*result, &count[0]._mp_num, factor);
1445 mpq_clear(count);
1447 if (allocated)
1448 Polyhedron_Free(P);
1449 value_clear(factor);
1452 static void uni_polynom(int param, Vector *c, evalue *EP)
1454 unsigned dim = c->Size-2;
1455 value_init(EP->d);
1456 value_set_si(EP->d,0);
1457 EP->x.p = new_enode(polynomial, dim+1, param+1);
1458 for (int j = 0; j <= dim; ++j)
1459 evalue_set(&EP->x.p->arr[j], c->p[j], c->p[dim+1]);
1462 static void multi_polynom(Vector *c, evalue* X, evalue *EP)
1464 unsigned dim = c->Size-2;
1465 evalue EC;
1467 value_init(EC.d);
1468 evalue_set(&EC, c->p[dim], c->p[dim+1]);
1470 value_init(EP->d);
1471 evalue_set(EP, c->p[dim], c->p[dim+1]);
1473 for (int i = dim-1; i >= 0; --i) {
1474 emul(X, EP);
1475 value_assign(EC.x.n, c->p[i]);
1476 eadd(&EC, EP);
1478 free_evalue_refs(&EC);
1481 Polyhedron *unfringe (Polyhedron *P, unsigned MaxRays)
1483 int len = P->Dimension+2;
1484 Polyhedron *T, *R = P;
1485 Value g;
1486 value_init(g);
1487 Vector *row = Vector_Alloc(len);
1488 value_set_si(row->p[0], 1);
1490 R = DomainConstraintSimplify(Polyhedron_Copy(P), MaxRays);
1492 Matrix *M = Matrix_Alloc(2, len-1);
1493 value_set_si(M->p[1][len-2], 1);
1494 for (int v = 0; v < P->Dimension; ++v) {
1495 value_set_si(M->p[0][v], 1);
1496 Polyhedron *I = Polyhedron_Image(P, M, 2+1);
1497 value_set_si(M->p[0][v], 0);
1498 for (int r = 0; r < I->NbConstraints; ++r) {
1499 if (value_zero_p(I->Constraint[r][0]))
1500 continue;
1501 if (value_zero_p(I->Constraint[r][1]))
1502 continue;
1503 if (value_one_p(I->Constraint[r][1]))
1504 continue;
1505 if (value_mone_p(I->Constraint[r][1]))
1506 continue;
1507 value_absolute(g, I->Constraint[r][1]);
1508 Vector_Set(row->p+1, 0, len-2);
1509 value_division(row->p[1+v], I->Constraint[r][1], g);
1510 mpz_fdiv_q(row->p[len-1], I->Constraint[r][2], g);
1511 T = R;
1512 R = AddConstraints(row->p, 1, R, MaxRays);
1513 if (T != P)
1514 Polyhedron_Free(T);
1516 Polyhedron_Free(I);
1518 Matrix_Free(M);
1519 Vector_Free(row);
1520 value_clear(g);
1521 return R;
1524 static Polyhedron *reduce_domain(Polyhedron *D, Matrix *CT, Polyhedron *CEq,
1525 Polyhedron **fVD, int nd, unsigned MaxRays)
1527 assert(CEq);
1529 Polyhedron *Dt;
1530 Dt = CT ? DomainPreimage(D, CT, MaxRays) : D;
1531 Polyhedron *rVD = DomainIntersection(Dt, CEq, MaxRays);
1533 /* if rVD is empty or too small in geometric dimension */
1534 if(!rVD || emptyQ(rVD) ||
1535 (rVD->Dimension-rVD->NbEq < Dt->Dimension-Dt->NbEq-CEq->NbEq)) {
1536 if(rVD)
1537 Domain_Free(rVD);
1538 if (CT)
1539 Domain_Free(Dt);
1540 return 0; /* empty validity domain */
1543 if (CT)
1544 Domain_Free(Dt);
1546 fVD[nd] = Domain_Copy(rVD);
1547 for (int i = 0 ; i < nd; ++i) {
1548 Polyhedron *I = DomainIntersection(fVD[nd], fVD[i], MaxRays);
1549 if (emptyQ(I)) {
1550 Domain_Free(I);
1551 continue;
1553 Polyhedron *F = DomainSimplify(I, fVD[nd], MaxRays);
1554 if (F->NbEq == 1) {
1555 Polyhedron *T = rVD;
1556 rVD = DomainDifference(rVD, F, MaxRays);
1557 Domain_Free(T);
1559 Domain_Free(F);
1560 Domain_Free(I);
1563 rVD = DomainConstraintSimplify(rVD, MaxRays);
1564 if (emptyQ(rVD)) {
1565 Domain_Free(fVD[nd]);
1566 Domain_Free(rVD);
1567 return 0;
1570 Value c;
1571 value_init(c);
1572 barvinok_count(rVD, &c, MaxRays);
1573 if (value_zero_p(c)) {
1574 Domain_Free(rVD);
1575 rVD = 0;
1577 value_clear(c);
1579 return rVD;
1582 static bool Polyhedron_is_infinite(Polyhedron *P, unsigned nparam)
1584 int r;
1585 for (r = 0; r < P->NbRays; ++r)
1586 if (value_zero_p(P->Ray[r][0]) ||
1587 value_zero_p(P->Ray[r][P->Dimension+1])) {
1588 int i;
1589 for (i = P->Dimension - nparam; i < P->Dimension; ++i)
1590 if (value_notzero_p(P->Ray[r][i+1]))
1591 break;
1592 if (i >= P->Dimension)
1593 break;
1595 return r < P->NbRays;
1598 /* Check whether all rays point in the positive directions
1599 * for the parameters
1601 static bool Polyhedron_has_positive_rays(Polyhedron *P, unsigned nparam)
1603 int r;
1604 for (r = 0; r < P->NbRays; ++r)
1605 if (value_zero_p(P->Ray[r][P->Dimension+1])) {
1606 int i;
1607 for (i = P->Dimension - nparam; i < P->Dimension; ++i)
1608 if (value_neg_p(P->Ray[r][i+1]))
1609 return false;
1611 return true;
1614 evalue* barvinok_enumerate_ev(Polyhedron *P, Polyhedron* C, unsigned MaxRays)
1616 //P = unfringe(P, MaxRays);
1617 Polyhedron *CEq = NULL, *rVD, *pVD, *CA;
1618 Matrix *CT = NULL;
1619 Param_Polyhedron *PP = NULL;
1620 Param_Domain *D, *next;
1621 Param_Vertices *V;
1622 int r = 0;
1623 unsigned nparam = C->Dimension;
1624 evalue *eres;
1625 ALLOC(evalue, eres);
1626 value_init(eres->d);
1627 value_set_si(eres->d, 0);
1629 evalue factor;
1630 value_init(factor.d);
1631 evalue_set_si(&factor, 1, 1);
1633 CA = align_context(C, P->Dimension, MaxRays);
1634 P = DomainIntersection(P, CA, MaxRays);
1635 Polyhedron_Free(CA);
1637 if (C->Dimension == 0 || emptyQ(P)) {
1638 constant:
1639 eres->x.p = new_enode(partition, 2, C->Dimension);
1640 EVALUE_SET_DOMAIN(eres->x.p->arr[0],
1641 DomainConstraintSimplify(CEq ? CEq : Polyhedron_Copy(C), MaxRays));
1642 value_set_si(eres->x.p->arr[1].d, 1);
1643 value_init(eres->x.p->arr[1].x.n);
1644 if (emptyQ(P))
1645 value_set_si(eres->x.p->arr[1].x.n, 0);
1646 else
1647 barvinok_count(P, &eres->x.p->arr[1].x.n, MaxRays);
1648 out:
1649 emul(&factor, eres);
1650 reduce_evalue(eres);
1651 free_evalue_refs(&factor);
1652 Polyhedron_Free(P);
1653 if (CT)
1654 Matrix_Free(CT);
1655 if (PP)
1656 Param_Polyhedron_Free(PP);
1658 return eres;
1660 if (Polyhedron_is_infinite(P, nparam))
1661 goto constant;
1663 if (P->NbEq != 0) {
1664 Matrix *f;
1665 P = remove_equalities_p(P, P->Dimension-nparam, &f);
1666 mask(f, &factor);
1667 Matrix_Free(f);
1669 if (P->Dimension == nparam) {
1670 CEq = P;
1671 P = Universe_Polyhedron(0);
1672 goto constant;
1675 Polyhedron *Q = ParamPolyhedron_Reduce(P, P->Dimension-nparam, &factor);
1676 if (Q) {
1677 Polyhedron_Free(P);
1678 if (Q->Dimension == nparam) {
1679 CEq = Q;
1680 P = Universe_Polyhedron(0);
1681 goto constant;
1683 P = Q;
1685 Polyhedron *oldP = P;
1686 PP = Polyhedron2Param_SimplifiedDomain(&P,C,MaxRays,&CEq,&CT);
1687 if (P != oldP)
1688 Polyhedron_Free(oldP);
1690 if (isIdentity(CT)) {
1691 Matrix_Free(CT);
1692 CT = NULL;
1693 } else {
1694 assert(CT->NbRows != CT->NbColumns);
1695 if (CT->NbRows == 1) // no more parameters
1696 goto constant;
1697 nparam = CT->NbRows - 1;
1700 unsigned dim = P->Dimension - nparam;
1701 Polyhedron ** vcone = new Polyhedron_p[PP->nbV];
1702 int * npos = new int[PP->nbV];
1703 int * nneg = new int[PP->nbV];
1704 ZZ sign;
1706 int i;
1707 for (i = 0, V = PP->V; V; ++i, V = V->next) {
1708 Polyhedron *C = supporting_cone_p(P, V);
1709 decompose(C, &vcone[i], &npos[i], &nneg[i], MaxRays);
1712 Vector *c = Vector_Alloc(dim+2);
1714 int nd;
1715 for (nd = 0, D=PP->D; D; ++nd, D=D->next);
1716 struct section { Polyhedron *D; evalue E; };
1717 section *s = new section[nd];
1718 Polyhedron **fVD = new Polyhedron_p[nd];
1720 for(nd = 0, D=PP->D; D; D=next) {
1721 next = D->next;
1723 Polyhedron *rVD = reduce_domain(D->Domain, CT, CEq,
1724 fVD, nd, MaxRays);
1725 if (!rVD)
1726 continue;
1728 pVD = CT ? DomainImage(rVD,CT,MaxRays) : rVD;
1730 int ncone = 0;
1731 FORALL_PVertex_in_ParamPolyhedron(V,D,PP) // _i is internal counter
1732 ncone += npos[_i] + nneg[_i];
1733 END_FORALL_PVertex_in_ParamPolyhedron;
1735 mat_ZZ rays;
1736 rays.SetDims(ncone * dim, dim);
1737 r = 0;
1738 FORALL_PVertex_in_ParamPolyhedron(V,D,PP) // _i is internal counter
1739 for (Polyhedron *i = vcone[_i]; i; i = i->next) {
1740 assert(i->NbRays-1 == dim);
1741 add_rays(rays, i, &r);
1743 END_FORALL_PVertex_in_ParamPolyhedron;
1744 vec_ZZ lambda;
1745 nonorthog(rays, lambda);
1747 vec_ZZ den;
1748 den.SetLength(dim);
1749 term_info num;
1751 value_init(s[nd].E.d);
1752 evalue_set_si(&s[nd].E, 0, 1);
1753 mpq_t count;
1754 mpq_init(count);
1755 FORALL_PVertex_in_ParamPolyhedron(V,D,PP)
1756 int f = 0;
1757 for (Polyhedron *i = vcone[_i]; i; i = i->next) {
1758 sign = f < npos[_i] ? 1 : -1;
1759 lattice_point(V, i, lambda, &num, pVD);
1760 normalize(i, lambda, sign, num.constant, den);
1762 dpoly n(dim, den[0], 1);
1763 for (int k = 1; k < dim; ++k) {
1764 dpoly fact(dim, den[k], 1);
1765 n *= fact;
1767 if (num.E != NULL) {
1768 ZZ one(INIT_VAL, 1);
1769 dpoly_n d(dim, num.constant, one);
1770 d.div(n, c, sign);
1771 evalue EV;
1772 multi_polynom(c, num.E, &EV);
1773 eadd(&EV , &s[nd].E);
1774 free_evalue_refs(&EV);
1775 free_evalue_refs(num.E);
1776 delete num.E;
1777 } else if (num.pos != -1) {
1778 dpoly_n d(dim, num.constant, num.coeff);
1779 d.div(n, c, sign);
1780 evalue EV;
1781 uni_polynom(num.pos, c, &EV);
1782 eadd(&EV , &s[nd].E);
1783 free_evalue_refs(&EV);
1784 } else {
1785 mpq_set_si(count, 0, 1);
1786 dpoly d(dim, num.constant);
1787 d.div(n, count, sign);
1788 evalue EV;
1789 value_init(EV.d);
1790 evalue_set(&EV, &count[0]._mp_num, &count[0]._mp_den);
1791 eadd(&EV , &s[nd].E);
1792 free_evalue_refs(&EV);
1794 ++f;
1796 END_FORALL_PVertex_in_ParamPolyhedron;
1798 mpq_clear(count);
1800 if (CT)
1801 addeliminatedparams_evalue(&s[nd].E, CT);
1802 s[nd].D = rVD;
1803 ++nd;
1804 if (rVD != pVD)
1805 Domain_Free(pVD);
1808 if (nd == 0)
1809 evalue_set_si(eres, 0, 1);
1810 else {
1811 eres->x.p = new_enode(partition, 2*nd, C->Dimension);
1812 for (int j = 0; j < nd; ++j) {
1813 EVALUE_SET_DOMAIN(eres->x.p->arr[2*j], s[j].D);
1814 value_clear(eres->x.p->arr[2*j+1].d);
1815 eres->x.p->arr[2*j+1] = s[j].E;
1816 Domain_Free(fVD[j]);
1819 delete [] s;
1820 delete [] fVD;
1822 Vector_Free(c);
1824 for (int j = 0; j < PP->nbV; ++j)
1825 Domain_Free(vcone[j]);
1826 delete [] vcone;
1827 delete [] npos;
1828 delete [] nneg;
1830 if (CEq)
1831 Polyhedron_Free(CEq);
1833 goto out;
1836 Enumeration* barvinok_enumerate(Polyhedron *P, Polyhedron* C, unsigned MaxRays)
1838 evalue *EP = barvinok_enumerate_ev(P, C, MaxRays);
1840 return partition2enumeration(EP);
1843 static void SwapColumns(Value **V, int n, int i, int j)
1845 for (int r = 0; r < n; ++r)
1846 value_swap(V[r][i], V[r][j]);
1849 static void SwapColumns(Polyhedron *P, int i, int j)
1851 SwapColumns(P->Constraint, P->NbConstraints, i, j);
1852 SwapColumns(P->Ray, P->NbRays, i, j);
1855 static void negative_test_constraint(Value *l, Value *u, Value *c, int pos,
1856 int len, Value *v)
1858 value_oppose(*v, u[pos+1]);
1859 Vector_Combine(l+1, u+1, c+1, *v, l[pos+1], len-1);
1860 value_multiply(*v, *v, l[pos+1]);
1861 value_substract(c[len-1], c[len-1], *v);
1862 value_set_si(*v, -1);
1863 Vector_Scale(c+1, c+1, *v, len-1);
1864 value_decrement(c[len-1], c[len-1]);
1865 ConstraintSimplify(c, c, len, v);
1868 static void oppose_constraint(Value *c, int len, Value *v)
1870 value_set_si(*v, -1);
1871 Vector_Scale(c+1, c+1, *v, len-1);
1872 value_decrement(c[len-1], c[len-1]);
1875 static bool SplitOnConstraint(Polyhedron *P, int i, int l, int u,
1876 int nvar, int len, int exist, int MaxRays,
1877 Vector *row, Value& f, bool independent,
1878 Polyhedron **pos, Polyhedron **neg)
1880 negative_test_constraint(P->Constraint[l], P->Constraint[u],
1881 row->p, nvar+i, len, &f);
1882 *neg = AddConstraints(row->p, 1, P, MaxRays);
1884 /* We found an independent, but useless constraint
1885 * Maybe we should detect this earlier and not
1886 * mark the variable as INDEPENDENT
1888 if (emptyQ((*neg))) {
1889 Polyhedron_Free(*neg);
1890 return false;
1893 oppose_constraint(row->p, len, &f);
1894 *pos = AddConstraints(row->p, 1, P, MaxRays);
1896 if (emptyQ((*pos))) {
1897 Polyhedron_Free(*neg);
1898 Polyhedron_Free(*pos);
1899 return false;
1902 return true;
1906 * unimodularly transform P such that constraint r is transformed
1907 * into a constraint that involves only a single (the first)
1908 * existential variable
1911 static Polyhedron *rotate_along(Polyhedron *P, int r, int nvar, int exist,
1912 unsigned MaxRays)
1914 Value g;
1915 value_init(g);
1917 Vector *row = Vector_Alloc(exist);
1918 Vector_Copy(P->Constraint[r]+1+nvar, row->p, exist);
1919 Vector_Gcd(row->p, exist, &g);
1920 if (value_notone_p(g))
1921 Vector_AntiScale(row->p, row->p, g, exist);
1922 value_clear(g);
1924 Matrix *M = unimodular_complete(row);
1925 Matrix *M2 = Matrix_Alloc(P->Dimension+1, P->Dimension+1);
1926 for (r = 0; r < nvar; ++r)
1927 value_set_si(M2->p[r][r], 1);
1928 for ( ; r < nvar+exist; ++r)
1929 Vector_Copy(M->p[r-nvar], M2->p[r]+nvar, exist);
1930 for ( ; r < P->Dimension+1; ++r)
1931 value_set_si(M2->p[r][r], 1);
1932 Polyhedron *T = Polyhedron_Image(P, M2, MaxRays);
1934 Matrix_Free(M2);
1935 Matrix_Free(M);
1936 Vector_Free(row);
1938 return T;
1941 static bool SplitOnVar(Polyhedron *P, int i,
1942 int nvar, int len, int exist, int MaxRays,
1943 Vector *row, Value& f, bool independent,
1944 Polyhedron **pos, Polyhedron **neg)
1946 int j;
1948 for (int l = P->NbEq; l < P->NbConstraints; ++l) {
1949 if (value_negz_p(P->Constraint[l][nvar+i+1]))
1950 continue;
1952 if (independent) {
1953 for (j = 0; j < exist; ++j)
1954 if (j != i && value_notzero_p(P->Constraint[l][nvar+j+1]))
1955 break;
1956 if (j < exist)
1957 continue;
1960 for (int u = P->NbEq; u < P->NbConstraints; ++u) {
1961 if (value_posz_p(P->Constraint[u][nvar+i+1]))
1962 continue;
1964 if (independent) {
1965 for (j = 0; j < exist; ++j)
1966 if (j != i && value_notzero_p(P->Constraint[u][nvar+j+1]))
1967 break;
1968 if (j < exist)
1969 continue;
1972 if (SplitOnConstraint(P, i, l, u,
1973 nvar, len, exist, MaxRays,
1974 row, f, independent,
1975 pos, neg)) {
1976 if (independent) {
1977 if (i != 0)
1978 SwapColumns(*neg, nvar+1, nvar+1+i);
1980 return true;
1985 return false;
1988 static bool double_bound_pair(Polyhedron *P, int nvar, int exist,
1989 int i, int l1, int l2,
1990 Polyhedron **pos, Polyhedron **neg)
1992 Value f;
1993 value_init(f);
1994 Vector *row = Vector_Alloc(P->Dimension+2);
1995 value_set_si(row->p[0], 1);
1996 value_oppose(f, P->Constraint[l1][nvar+i+1]);
1997 Vector_Combine(P->Constraint[l1]+1, P->Constraint[l2]+1,
1998 row->p+1,
1999 P->Constraint[l2][nvar+i+1], f,
2000 P->Dimension+1);
2001 ConstraintSimplify(row->p, row->p, P->Dimension+2, &f);
2002 *pos = AddConstraints(row->p, 1, P, 0);
2003 value_set_si(f, -1);
2004 Vector_Scale(row->p+1, row->p+1, f, P->Dimension+1);
2005 value_decrement(row->p[P->Dimension+1], row->p[P->Dimension+1]);
2006 *neg = AddConstraints(row->p, 1, P, 0);
2007 Vector_Free(row);
2008 value_clear(f);
2010 return !emptyQ((*pos)) && !emptyQ((*neg));
2013 static bool double_bound(Polyhedron *P, int nvar, int exist,
2014 Polyhedron **pos, Polyhedron **neg)
2016 for (int i = 0; i < exist; ++i) {
2017 int l1, l2;
2018 for (l1 = P->NbEq; l1 < P->NbConstraints; ++l1) {
2019 if (value_negz_p(P->Constraint[l1][nvar+i+1]))
2020 continue;
2021 for (l2 = l1 + 1; l2 < P->NbConstraints; ++l2) {
2022 if (value_negz_p(P->Constraint[l2][nvar+i+1]))
2023 continue;
2024 if (double_bound_pair(P, nvar, exist, i, l1, l2, pos, neg))
2025 return true;
2028 for (l1 = P->NbEq; l1 < P->NbConstraints; ++l1) {
2029 if (value_posz_p(P->Constraint[l1][nvar+i+1]))
2030 continue;
2031 if (l1 < P->NbConstraints)
2032 for (l2 = l1 + 1; l2 < P->NbConstraints; ++l2) {
2033 if (value_posz_p(P->Constraint[l2][nvar+i+1]))
2034 continue;
2035 if (double_bound_pair(P, nvar, exist, i, l1, l2, pos, neg))
2036 return true;
2039 return false;
2041 return false;
2044 enum constraint {
2045 ALL_POS = 1 << 0,
2046 ONE_NEG = 1 << 1,
2047 INDEPENDENT = 1 << 2
2050 static evalue* enumerate_or(Polyhedron *D,
2051 unsigned exist, unsigned nparam, unsigned MaxRays)
2053 #ifdef DEBUG_ER
2054 fprintf(stderr, "\nER: Or\n");
2055 #endif /* DEBUG_ER */
2057 Polyhedron *N = D->next;
2058 D->next = 0;
2059 evalue *EP =
2060 barvinok_enumerate_e(D, exist, nparam, MaxRays);
2061 Polyhedron_Free(D);
2063 for (D = N; D; D = N) {
2064 N = D->next;
2065 D->next = 0;
2067 evalue *EN =
2068 barvinok_enumerate_e(D, exist, nparam, MaxRays);
2070 eor(EN, EP);
2071 free_evalue_refs(EN);
2072 free(EN);
2073 Polyhedron_Free(D);
2076 reduce_evalue(EP);
2078 return EP;
2081 static evalue* enumerate_sum(Polyhedron *P,
2082 unsigned exist, unsigned nparam, unsigned MaxRays)
2084 int nvar = P->Dimension - exist - nparam;
2085 int toswap = nvar < exist ? nvar : exist;
2086 for (int i = 0; i < toswap; ++i)
2087 SwapColumns(P, 1 + i, nvar+exist - i);
2088 nparam += nvar;
2090 #ifdef DEBUG_ER
2091 fprintf(stderr, "\nER: Sum\n");
2092 #endif /* DEBUG_ER */
2094 evalue *EP = barvinok_enumerate_e(P, exist, nparam, MaxRays);
2096 for (int i = 0; i < /* nvar */ nparam; ++i) {
2097 Matrix *C = Matrix_Alloc(1, 1 + nparam + 1);
2098 value_set_si(C->p[0][0], 1);
2099 evalue split;
2100 value_init(split.d);
2101 value_set_si(split.d, 0);
2102 split.x.p = new_enode(partition, 4, nparam);
2103 value_set_si(C->p[0][1+i], 1);
2104 Matrix *C2 = Matrix_Copy(C);
2105 EVALUE_SET_DOMAIN(split.x.p->arr[0],
2106 Constraints2Polyhedron(C2, MaxRays));
2107 Matrix_Free(C2);
2108 evalue_set_si(&split.x.p->arr[1], 1, 1);
2109 value_set_si(C->p[0][1+i], -1);
2110 value_set_si(C->p[0][1+nparam], -1);
2111 EVALUE_SET_DOMAIN(split.x.p->arr[2],
2112 Constraints2Polyhedron(C, MaxRays));
2113 evalue_set_si(&split.x.p->arr[3], 1, 1);
2114 emul(&split, EP);
2115 free_evalue_refs(&split);
2116 Matrix_Free(C);
2118 reduce_evalue(EP);
2119 evalue_range_reduction(EP);
2121 evalue_frac2floor(EP);
2123 evalue *sum = esum(EP, nvar);
2125 free_evalue_refs(EP);
2126 free(EP);
2127 EP = sum;
2129 evalue_range_reduction(EP);
2131 return EP;
2134 static evalue* split_sure(Polyhedron *P, Polyhedron *S,
2135 unsigned exist, unsigned nparam, unsigned MaxRays)
2137 int nvar = P->Dimension - exist - nparam;
2139 Matrix *M = Matrix_Alloc(exist, S->Dimension+2);
2140 for (int i = 0; i < exist; ++i)
2141 value_set_si(M->p[i][nvar+i+1], 1);
2142 Polyhedron *O = S;
2143 S = DomainAddRays(S, M, MaxRays);
2144 Polyhedron_Free(O);
2145 Polyhedron *F = DomainAddRays(P, M, MaxRays);
2146 Polyhedron *D = DomainDifference(F, S, MaxRays);
2147 O = D;
2148 D = Disjoint_Domain(D, 0, MaxRays);
2149 Polyhedron_Free(F);
2150 Domain_Free(O);
2151 Matrix_Free(M);
2153 M = Matrix_Alloc(P->Dimension+1-exist, P->Dimension+1);
2154 for (int j = 0; j < nvar; ++j)
2155 value_set_si(M->p[j][j], 1);
2156 for (int j = 0; j < nparam+1; ++j)
2157 value_set_si(M->p[nvar+j][nvar+exist+j], 1);
2158 Polyhedron *T = Polyhedron_Image(S, M, MaxRays);
2159 evalue *EP = barvinok_enumerate_e(T, 0, nparam, MaxRays);
2160 Polyhedron_Free(S);
2161 Polyhedron_Free(T);
2162 Matrix_Free(M);
2164 for (Polyhedron *Q = D; Q; Q = Q->next) {
2165 Polyhedron *N = Q->next;
2166 Q->next = 0;
2167 T = DomainIntersection(P, Q, MaxRays);
2168 evalue *E = barvinok_enumerate_e(T, exist, nparam, MaxRays);
2169 eadd(E, EP);
2170 free_evalue_refs(E);
2171 free(E);
2172 Polyhedron_Free(T);
2173 Q->next = N;
2175 Domain_Free(D);
2176 return EP;
2179 static evalue* enumerate_sure(Polyhedron *P,
2180 unsigned exist, unsigned nparam, unsigned MaxRays)
2182 int i;
2183 Polyhedron *S = P;
2184 int nvar = P->Dimension - exist - nparam;
2185 Value lcm;
2186 Value f;
2187 value_init(lcm);
2188 value_init(f);
2190 for (i = 0; i < exist; ++i) {
2191 Matrix *M = Matrix_Alloc(S->NbConstraints, S->Dimension+2);
2192 int c = 0;
2193 value_set_si(lcm, 1);
2194 for (int j = 0; j < S->NbConstraints; ++j) {
2195 if (value_negz_p(S->Constraint[j][1+nvar+i]))
2196 continue;
2197 if (value_one_p(S->Constraint[j][1+nvar+i]))
2198 continue;
2199 value_lcm(lcm, S->Constraint[j][1+nvar+i], &lcm);
2202 for (int j = 0; j < S->NbConstraints; ++j) {
2203 if (value_negz_p(S->Constraint[j][1+nvar+i]))
2204 continue;
2205 if (value_one_p(S->Constraint[j][1+nvar+i]))
2206 continue;
2207 value_division(f, lcm, S->Constraint[j][1+nvar+i]);
2208 Vector_Scale(S->Constraint[j], M->p[c], f, S->Dimension+2);
2209 value_substract(M->p[c][S->Dimension+1],
2210 M->p[c][S->Dimension+1],
2211 lcm);
2212 value_increment(M->p[c][S->Dimension+1],
2213 M->p[c][S->Dimension+1]);
2214 ++c;
2216 Polyhedron *O = S;
2217 S = AddConstraints(M->p[0], c, S, MaxRays);
2218 if (O != P)
2219 Polyhedron_Free(O);
2220 Matrix_Free(M);
2221 if (emptyQ(S)) {
2222 Polyhedron_Free(S);
2223 value_clear(lcm);
2224 value_clear(f);
2225 return 0;
2228 value_clear(lcm);
2229 value_clear(f);
2231 #ifdef DEBUG_ER
2232 fprintf(stderr, "\nER: Sure\n");
2233 #endif /* DEBUG_ER */
2235 return split_sure(P, S, exist, nparam, MaxRays);
2238 static evalue* enumerate_sure2(Polyhedron *P,
2239 unsigned exist, unsigned nparam, unsigned MaxRays)
2241 int nvar = P->Dimension - exist - nparam;
2242 int r;
2243 for (r = 0; r < P->NbRays; ++r)
2244 if (value_one_p(P->Ray[r][0]) &&
2245 value_one_p(P->Ray[r][P->Dimension+1]))
2246 break;
2248 if (r >= P->NbRays)
2249 return 0;
2251 Matrix *M = Matrix_Alloc(nvar + 1 + nparam, P->Dimension+2);
2252 for (int i = 0; i < nvar; ++i)
2253 value_set_si(M->p[i][1+i], 1);
2254 for (int i = 0; i < nparam; ++i)
2255 value_set_si(M->p[i+nvar][1+nvar+exist+i], 1);
2256 Vector_Copy(P->Ray[r]+1+nvar, M->p[nvar+nparam]+1+nvar, exist);
2257 value_set_si(M->p[nvar+nparam][0], 1);
2258 value_set_si(M->p[nvar+nparam][P->Dimension+1], 1);
2259 Polyhedron * F = Rays2Polyhedron(M, MaxRays);
2260 Matrix_Free(M);
2262 Polyhedron *I = DomainIntersection(F, P, MaxRays);
2263 Polyhedron_Free(F);
2265 #ifdef DEBUG_ER
2266 fprintf(stderr, "\nER: Sure2\n");
2267 #endif /* DEBUG_ER */
2269 return split_sure(P, I, exist, nparam, MaxRays);
2272 static evalue* enumerate_cyclic(Polyhedron *P,
2273 unsigned exist, unsigned nparam,
2274 evalue * EP, int r, int p, unsigned MaxRays)
2276 int nvar = P->Dimension - exist - nparam;
2278 /* If EP in its fractional maps only contains references
2279 * to the remainder parameter with appropriate coefficients
2280 * then we could in principle avoid adding existentially
2281 * quantified variables to the validity domains.
2282 * We'd have to replace the remainder by m { p/m }
2283 * and multiply with an appropriate factor that is one
2284 * only in the appropriate range.
2285 * This last multiplication can be avoided if EP
2286 * has a single validity domain with no (further)
2287 * constraints on the remainder parameter
2290 Matrix *CT = Matrix_Alloc(nparam+1, nparam+3);
2291 Matrix *M = Matrix_Alloc(1, 1+nparam+3);
2292 for (int j = 0; j < nparam; ++j)
2293 if (j != p)
2294 value_set_si(CT->p[j][j], 1);
2295 value_set_si(CT->p[p][nparam+1], 1);
2296 value_set_si(CT->p[nparam][nparam+2], 1);
2297 value_set_si(M->p[0][1+p], -1);
2298 value_absolute(M->p[0][1+nparam], P->Ray[0][1+nvar+exist+p]);
2299 value_set_si(M->p[0][1+nparam+1], 1);
2300 Polyhedron *CEq = Constraints2Polyhedron(M, 1);
2301 Matrix_Free(M);
2302 addeliminatedparams_enum(EP, CT, CEq, MaxRays, nparam);
2303 Polyhedron_Free(CEq);
2304 Matrix_Free(CT);
2306 return EP;
2309 static void enumerate_vd_add_ray(evalue *EP, Matrix *Rays, unsigned MaxRays)
2311 if (value_notzero_p(EP->d))
2312 return;
2314 assert(EP->x.p->type == partition);
2315 assert(EP->x.p->pos == EVALUE_DOMAIN(EP->x.p->arr[0])->Dimension);
2316 for (int i = 0; i < EP->x.p->size/2; ++i) {
2317 Polyhedron *D = EVALUE_DOMAIN(EP->x.p->arr[2*i]);
2318 Polyhedron *N = DomainAddRays(D, Rays, MaxRays);
2319 EVALUE_SET_DOMAIN(EP->x.p->arr[2*i], N);
2320 Domain_Free(D);
2324 static evalue* enumerate_line(Polyhedron *P,
2325 unsigned exist, unsigned nparam, unsigned MaxRays)
2327 if (P->NbBid == 0)
2328 return 0;
2330 #ifdef DEBUG_ER
2331 fprintf(stderr, "\nER: Line\n");
2332 #endif /* DEBUG_ER */
2334 int nvar = P->Dimension - exist - nparam;
2335 int i, j;
2336 for (i = 0; i < nparam; ++i)
2337 if (value_notzero_p(P->Ray[0][1+nvar+exist+i]))
2338 break;
2339 assert(i < nparam);
2340 for (j = i+1; j < nparam; ++j)
2341 if (value_notzero_p(P->Ray[0][1+nvar+exist+i]))
2342 break;
2343 assert(j >= nparam); // for now
2345 Matrix *M = Matrix_Alloc(2, P->Dimension+2);
2346 value_set_si(M->p[0][0], 1);
2347 value_set_si(M->p[0][1+nvar+exist+i], 1);
2348 value_set_si(M->p[1][0], 1);
2349 value_set_si(M->p[1][1+nvar+exist+i], -1);
2350 value_absolute(M->p[1][1+P->Dimension], P->Ray[0][1+nvar+exist+i]);
2351 value_decrement(M->p[1][1+P->Dimension], M->p[1][1+P->Dimension]);
2352 Polyhedron *S = AddConstraints(M->p[0], 2, P, MaxRays);
2353 evalue *EP = barvinok_enumerate_e(S, exist, nparam, MaxRays);
2354 Polyhedron_Free(S);
2355 Matrix_Free(M);
2357 return enumerate_cyclic(P, exist, nparam, EP, 0, i, MaxRays);
2360 static int single_param_pos(Polyhedron*P, unsigned exist, unsigned nparam,
2361 int r)
2363 int nvar = P->Dimension - exist - nparam;
2364 if (First_Non_Zero(P->Ray[r]+1, nvar) != -1)
2365 return -1;
2366 int i = First_Non_Zero(P->Ray[r]+1+nvar+exist, nparam);
2367 if (i == -1)
2368 return -1;
2369 if (First_Non_Zero(P->Ray[r]+1+nvar+exist+1, nparam-i-1) != -1)
2370 return -1;
2371 return i;
2374 static evalue* enumerate_remove_ray(Polyhedron *P, int r,
2375 unsigned exist, unsigned nparam, unsigned MaxRays)
2377 #ifdef DEBUG_ER
2378 fprintf(stderr, "\nER: RedundantRay\n");
2379 #endif /* DEBUG_ER */
2381 Value one;
2382 value_init(one);
2383 value_set_si(one, 1);
2384 int len = P->NbRays-1;
2385 Matrix *M = Matrix_Alloc(2 * len, P->Dimension+2);
2386 Vector_Copy(P->Ray[0], M->p[0], r * (P->Dimension+2));
2387 Vector_Copy(P->Ray[r+1], M->p[r], (len-r) * (P->Dimension+2));
2388 for (int j = 0; j < P->NbRays; ++j) {
2389 if (j == r)
2390 continue;
2391 Vector_Combine(P->Ray[j], P->Ray[r], M->p[len+j-(j>r)],
2392 one, P->Ray[j][P->Dimension+1], P->Dimension+2);
2395 P = Rays2Polyhedron(M, MaxRays);
2396 Matrix_Free(M);
2397 evalue *EP = barvinok_enumerate_e(P, exist, nparam, MaxRays);
2398 Polyhedron_Free(P);
2399 value_clear(one);
2401 return EP;
2404 static evalue* enumerate_redundant_ray(Polyhedron *P,
2405 unsigned exist, unsigned nparam, unsigned MaxRays)
2407 assert(P->NbBid == 0);
2408 int nvar = P->Dimension - exist - nparam;
2409 Value m;
2410 value_init(m);
2412 for (int r = 0; r < P->NbRays; ++r) {
2413 if (value_notzero_p(P->Ray[r][P->Dimension+1]))
2414 continue;
2415 int i1 = single_param_pos(P, exist, nparam, r);
2416 if (i1 == -1)
2417 continue;
2418 for (int r2 = r+1; r2 < P->NbRays; ++r2) {
2419 if (value_notzero_p(P->Ray[r2][P->Dimension+1]))
2420 continue;
2421 int i2 = single_param_pos(P, exist, nparam, r2);
2422 if (i2 == -1)
2423 continue;
2424 if (i1 != i2)
2425 continue;
2427 value_division(m, P->Ray[r][1+nvar+exist+i1],
2428 P->Ray[r2][1+nvar+exist+i1]);
2429 value_multiply(m, m, P->Ray[r2][1+nvar+exist+i1]);
2430 /* r2 divides r => r redundant */
2431 if (value_eq(m, P->Ray[r][1+nvar+exist+i1])) {
2432 value_clear(m);
2433 return enumerate_remove_ray(P, r, exist, nparam, MaxRays);
2436 value_division(m, P->Ray[r2][1+nvar+exist+i1],
2437 P->Ray[r][1+nvar+exist+i1]);
2438 value_multiply(m, m, P->Ray[r][1+nvar+exist+i1]);
2439 /* r divides r2 => r2 redundant */
2440 if (value_eq(m, P->Ray[r2][1+nvar+exist+i1])) {
2441 value_clear(m);
2442 return enumerate_remove_ray(P, r2, exist, nparam, MaxRays);
2446 value_clear(m);
2447 return 0;
2450 static Polyhedron *upper_bound(Polyhedron *P,
2451 int pos, Value *max, Polyhedron **R)
2453 Value v;
2454 int r;
2455 value_init(v);
2457 *R = 0;
2458 Polyhedron *N;
2459 Polyhedron *B = 0;
2460 for (Polyhedron *Q = P; Q; Q = N) {
2461 N = Q->next;
2462 for (r = 0; r < P->NbRays; ++r) {
2463 if (value_zero_p(P->Ray[r][P->Dimension+1]) &&
2464 value_pos_p(P->Ray[r][1+pos]))
2465 break;
2467 if (r < P->NbRays) {
2468 Q->next = *R;
2469 *R = Q;
2470 continue;
2471 } else {
2472 Q->next = B;
2473 B = Q;
2475 for (r = 0; r < P->NbRays; ++r) {
2476 if (value_zero_p(P->Ray[r][P->Dimension+1]))
2477 continue;
2478 mpz_fdiv_q(v, P->Ray[r][1+pos], P->Ray[r][1+P->Dimension]);
2479 if ((!Q->next && r == 0) || value_gt(v, *max))
2480 value_assign(*max, v);
2483 value_clear(v);
2484 return B;
2487 static evalue* enumerate_ray(Polyhedron *P,
2488 unsigned exist, unsigned nparam, unsigned MaxRays)
2490 assert(P->NbBid == 0);
2491 int nvar = P->Dimension - exist - nparam;
2493 int r;
2494 for (r = 0; r < P->NbRays; ++r)
2495 if (value_zero_p(P->Ray[r][P->Dimension+1]))
2496 break;
2497 if (r >= P->NbRays)
2498 return 0;
2500 int r2;
2501 for (r2 = r+1; r2 < P->NbRays; ++r2)
2502 if (value_zero_p(P->Ray[r2][P->Dimension+1]))
2503 break;
2504 if (r2 < P->NbRays) {
2505 if (nvar > 0)
2506 return enumerate_sum(P, exist, nparam, MaxRays);
2509 #ifdef DEBUG_ER
2510 fprintf(stderr, "\nER: Ray\n");
2511 #endif /* DEBUG_ER */
2513 Value m;
2514 Value one;
2515 value_init(m);
2516 value_init(one);
2517 value_set_si(one, 1);
2518 int i = single_param_pos(P, exist, nparam, r);
2519 assert(i != -1); // for now;
2521 Matrix *M = Matrix_Alloc(P->NbRays, P->Dimension+2);
2522 for (int j = 0; j < P->NbRays; ++j) {
2523 Vector_Combine(P->Ray[j], P->Ray[r], M->p[j],
2524 one, P->Ray[j][P->Dimension+1], P->Dimension+2);
2526 Polyhedron *S = Rays2Polyhedron(M, MaxRays);
2527 Matrix_Free(M);
2528 Polyhedron *D = DomainDifference(P, S, MaxRays);
2529 Polyhedron_Free(S);
2530 // Polyhedron_Print(stderr, P_VALUE_FMT, D);
2531 assert(value_pos_p(P->Ray[r][1+nvar+exist+i])); // for now
2532 Polyhedron *R;
2533 D = upper_bound(D, nvar+exist+i, &m, &R);
2534 assert(D);
2535 Domain_Free(D);
2537 M = Matrix_Alloc(2, P->Dimension+2);
2538 value_set_si(M->p[0][0], 1);
2539 value_set_si(M->p[1][0], 1);
2540 value_set_si(M->p[0][1+nvar+exist+i], -1);
2541 value_set_si(M->p[1][1+nvar+exist+i], 1);
2542 value_assign(M->p[0][1+P->Dimension], m);
2543 value_oppose(M->p[1][1+P->Dimension], m);
2544 value_addto(M->p[1][1+P->Dimension], M->p[1][1+P->Dimension],
2545 P->Ray[r][1+nvar+exist+i]);
2546 value_decrement(M->p[1][1+P->Dimension], M->p[1][1+P->Dimension]);
2547 // Matrix_Print(stderr, P_VALUE_FMT, M);
2548 D = AddConstraints(M->p[0], 2, P, MaxRays);
2549 // Polyhedron_Print(stderr, P_VALUE_FMT, D);
2550 value_substract(M->p[0][1+P->Dimension], M->p[0][1+P->Dimension],
2551 P->Ray[r][1+nvar+exist+i]);
2552 // Matrix_Print(stderr, P_VALUE_FMT, M);
2553 S = AddConstraints(M->p[0], 1, P, MaxRays);
2554 // Polyhedron_Print(stderr, P_VALUE_FMT, S);
2555 Matrix_Free(M);
2557 evalue *EP = barvinok_enumerate_e(D, exist, nparam, MaxRays);
2558 Polyhedron_Free(D);
2559 value_clear(one);
2560 value_clear(m);
2562 if (value_notone_p(P->Ray[r][1+nvar+exist+i]))
2563 EP = enumerate_cyclic(P, exist, nparam, EP, r, i, MaxRays);
2564 else {
2565 M = Matrix_Alloc(1, nparam+2);
2566 value_set_si(M->p[0][0], 1);
2567 value_set_si(M->p[0][1+i], 1);
2568 enumerate_vd_add_ray(EP, M, MaxRays);
2569 Matrix_Free(M);
2572 if (!emptyQ(S)) {
2573 evalue *E = barvinok_enumerate_e(S, exist, nparam, MaxRays);
2574 eadd(E, EP);
2575 free_evalue_refs(E);
2576 free(E);
2578 Polyhedron_Free(S);
2580 if (R) {
2581 assert(nvar == 0);
2582 evalue *ER = enumerate_or(R, exist, nparam, MaxRays);
2583 eor(ER, EP);
2584 free_evalue_refs(ER);
2585 free(ER);
2588 return EP;
2591 static evalue* new_zero_ep()
2593 evalue *EP;
2594 ALLOC(evalue, EP);
2595 value_init(EP->d);
2596 evalue_set_si(EP, 0, 1);
2597 return EP;
2600 static evalue* enumerate_vd(Polyhedron **PA,
2601 unsigned exist, unsigned nparam, unsigned MaxRays)
2603 Polyhedron *P = *PA;
2604 int nvar = P->Dimension - exist - nparam;
2605 Param_Polyhedron *PP = NULL;
2606 Polyhedron *C = Universe_Polyhedron(nparam);
2607 Polyhedron *CEq;
2608 Matrix *CT;
2609 Polyhedron *PR = P;
2610 PP = Polyhedron2Param_SimplifiedDomain(&PR,C,MaxRays,&CEq,&CT);
2611 Polyhedron_Free(C);
2613 int nd;
2614 Param_Domain *D, *last;
2615 Value c;
2616 value_init(c);
2617 for (nd = 0, D=PP->D; D; D=D->next, ++nd)
2620 Polyhedron **VD = new Polyhedron_p[nd];
2621 Polyhedron **fVD = new Polyhedron_p[nd];
2622 for(nd = 0, D=PP->D; D; D=D->next) {
2623 Polyhedron *rVD = reduce_domain(D->Domain, CT, CEq,
2624 fVD, nd, MaxRays);
2625 if (!rVD)
2626 continue;
2628 VD[nd++] = rVD;
2629 last = D;
2632 evalue *EP = 0;
2634 if (nd == 0)
2635 EP = new_zero_ep();
2637 /* This doesn't seem to have any effect */
2638 if (nd == 1) {
2639 Polyhedron *CA = align_context(VD[0], P->Dimension, MaxRays);
2640 Polyhedron *O = P;
2641 P = DomainIntersection(P, CA, MaxRays);
2642 if (O != *PA)
2643 Polyhedron_Free(O);
2644 Polyhedron_Free(CA);
2645 if (emptyQ(P))
2646 EP = new_zero_ep();
2649 if (!EP && CT->NbColumns != CT->NbRows) {
2650 Polyhedron *CEqr = DomainImage(CEq, CT, MaxRays);
2651 Polyhedron *CA = align_context(CEqr, PR->Dimension, MaxRays);
2652 Polyhedron *I = DomainIntersection(PR, CA, MaxRays);
2653 Polyhedron_Free(CEqr);
2654 Polyhedron_Free(CA);
2655 #ifdef DEBUG_ER
2656 fprintf(stderr, "\nER: Eliminate\n");
2657 #endif /* DEBUG_ER */
2658 nparam -= CT->NbColumns - CT->NbRows;
2659 EP = barvinok_enumerate_e(I, exist, nparam, MaxRays);
2660 nparam += CT->NbColumns - CT->NbRows;
2661 addeliminatedparams_enum(EP, CT, CEq, MaxRays, nparam);
2662 Polyhedron_Free(I);
2664 if (PR != *PA)
2665 Polyhedron_Free(PR);
2666 PR = 0;
2668 if (!EP && nd > 1) {
2669 #ifdef DEBUG_ER
2670 fprintf(stderr, "\nER: VD\n");
2671 #endif /* DEBUG_ER */
2672 for (int i = 0; i < nd; ++i) {
2673 Polyhedron *CA = align_context(VD[i], P->Dimension, MaxRays);
2674 Polyhedron *I = DomainIntersection(P, CA, MaxRays);
2676 if (i == 0)
2677 EP = barvinok_enumerate_e(I, exist, nparam, MaxRays);
2678 else {
2679 evalue *E = barvinok_enumerate_e(I, exist, nparam, MaxRays);
2680 eadd(E, EP);
2681 free_evalue_refs(E);
2682 free(E);
2684 Polyhedron_Free(I);
2685 Polyhedron_Free(CA);
2689 for (int i = 0; i < nd; ++i) {
2690 Polyhedron_Free(VD[i]);
2691 Polyhedron_Free(fVD[i]);
2693 delete [] VD;
2694 delete [] fVD;
2695 value_clear(c);
2697 if (!EP && nvar == 0) {
2698 Value f;
2699 value_init(f);
2700 Param_Vertices *V, *V2;
2701 Matrix* M = Matrix_Alloc(1, P->Dimension+2);
2703 FORALL_PVertex_in_ParamPolyhedron(V, last, PP) {
2704 bool found = false;
2705 FORALL_PVertex_in_ParamPolyhedron(V2, last, PP) {
2706 if (V == V2) {
2707 found = true;
2708 continue;
2710 if (!found)
2711 continue;
2712 for (int i = 0; i < exist; ++i) {
2713 value_oppose(f, V->Vertex->p[i][nparam+1]);
2714 Vector_Combine(V->Vertex->p[i],
2715 V2->Vertex->p[i],
2716 M->p[0] + 1 + nvar + exist,
2717 V2->Vertex->p[i][nparam+1],
2719 nparam+1);
2720 int j;
2721 for (j = 0; j < nparam; ++j)
2722 if (value_notzero_p(M->p[0][1+nvar+exist+j]))
2723 break;
2724 if (j >= nparam)
2725 continue;
2726 ConstraintSimplify(M->p[0], M->p[0],
2727 P->Dimension+2, &f);
2728 value_set_si(M->p[0][0], 0);
2729 Polyhedron *para = AddConstraints(M->p[0], 1, P,
2730 MaxRays);
2731 if (emptyQ(para)) {
2732 Polyhedron_Free(para);
2733 continue;
2735 Polyhedron *pos, *neg;
2736 value_set_si(M->p[0][0], 1);
2737 value_decrement(M->p[0][P->Dimension+1],
2738 M->p[0][P->Dimension+1]);
2739 neg = AddConstraints(M->p[0], 1, P, MaxRays);
2740 value_set_si(f, -1);
2741 Vector_Scale(M->p[0]+1, M->p[0]+1, f,
2742 P->Dimension+1);
2743 value_decrement(M->p[0][P->Dimension+1],
2744 M->p[0][P->Dimension+1]);
2745 value_decrement(M->p[0][P->Dimension+1],
2746 M->p[0][P->Dimension+1]);
2747 pos = AddConstraints(M->p[0], 1, P, MaxRays);
2748 if (emptyQ(neg) && emptyQ(pos)) {
2749 Polyhedron_Free(para);
2750 Polyhedron_Free(pos);
2751 Polyhedron_Free(neg);
2752 continue;
2754 #ifdef DEBUG_ER
2755 fprintf(stderr, "\nER: Order\n");
2756 #endif /* DEBUG_ER */
2757 EP = barvinok_enumerate_e(para, exist, nparam, MaxRays);
2758 evalue *E;
2759 if (!emptyQ(pos)) {
2760 E = barvinok_enumerate_e(pos, exist, nparam, MaxRays);
2761 eadd(E, EP);
2762 free_evalue_refs(E);
2763 free(E);
2765 if (!emptyQ(neg)) {
2766 E = barvinok_enumerate_e(neg, exist, nparam, MaxRays);
2767 eadd(E, EP);
2768 free_evalue_refs(E);
2769 free(E);
2771 Polyhedron_Free(para);
2772 Polyhedron_Free(pos);
2773 Polyhedron_Free(neg);
2774 break;
2776 if (EP)
2777 break;
2778 } END_FORALL_PVertex_in_ParamPolyhedron;
2779 if (EP)
2780 break;
2781 } END_FORALL_PVertex_in_ParamPolyhedron;
2783 if (!EP) {
2784 /* Search for vertex coordinate to split on */
2785 /* First look for one independent of the parameters */
2786 FORALL_PVertex_in_ParamPolyhedron(V, last, PP) {
2787 for (int i = 0; i < exist; ++i) {
2788 int j;
2789 for (j = 0; j < nparam; ++j)
2790 if (value_notzero_p(V->Vertex->p[i][j]))
2791 break;
2792 if (j < nparam)
2793 continue;
2794 value_set_si(M->p[0][0], 1);
2795 Vector_Set(M->p[0]+1, 0, nvar+exist);
2796 Vector_Copy(V->Vertex->p[i],
2797 M->p[0] + 1 + nvar + exist, nparam+1);
2798 value_oppose(M->p[0][1+nvar+i],
2799 V->Vertex->p[i][nparam+1]);
2801 Polyhedron *pos, *neg;
2802 value_set_si(M->p[0][0], 1);
2803 value_decrement(M->p[0][P->Dimension+1],
2804 M->p[0][P->Dimension+1]);
2805 neg = AddConstraints(M->p[0], 1, P, MaxRays);
2806 value_set_si(f, -1);
2807 Vector_Scale(M->p[0]+1, M->p[0]+1, f,
2808 P->Dimension+1);
2809 value_decrement(M->p[0][P->Dimension+1],
2810 M->p[0][P->Dimension+1]);
2811 value_decrement(M->p[0][P->Dimension+1],
2812 M->p[0][P->Dimension+1]);
2813 pos = AddConstraints(M->p[0], 1, P, MaxRays);
2814 if (emptyQ(neg) || emptyQ(pos)) {
2815 Polyhedron_Free(pos);
2816 Polyhedron_Free(neg);
2817 continue;
2819 Polyhedron_Free(pos);
2820 value_increment(M->p[0][P->Dimension+1],
2821 M->p[0][P->Dimension+1]);
2822 pos = AddConstraints(M->p[0], 1, P, MaxRays);
2823 #ifdef DEBUG_ER
2824 fprintf(stderr, "\nER: Vertex\n");
2825 #endif /* DEBUG_ER */
2826 pos->next = neg;
2827 EP = enumerate_or(pos, exist, nparam, MaxRays);
2828 break;
2830 if (EP)
2831 break;
2832 } END_FORALL_PVertex_in_ParamPolyhedron;
2835 if (!EP) {
2836 /* Search for vertex coordinate to split on */
2837 /* Now look for one that depends on the parameters */
2838 FORALL_PVertex_in_ParamPolyhedron(V, last, PP) {
2839 for (int i = 0; i < exist; ++i) {
2840 value_set_si(M->p[0][0], 1);
2841 Vector_Set(M->p[0]+1, 0, nvar+exist);
2842 Vector_Copy(V->Vertex->p[i],
2843 M->p[0] + 1 + nvar + exist, nparam+1);
2844 value_oppose(M->p[0][1+nvar+i],
2845 V->Vertex->p[i][nparam+1]);
2847 Polyhedron *pos, *neg;
2848 value_set_si(M->p[0][0], 1);
2849 value_decrement(M->p[0][P->Dimension+1],
2850 M->p[0][P->Dimension+1]);
2851 neg = AddConstraints(M->p[0], 1, P, MaxRays);
2852 value_set_si(f, -1);
2853 Vector_Scale(M->p[0]+1, M->p[0]+1, f,
2854 P->Dimension+1);
2855 value_decrement(M->p[0][P->Dimension+1],
2856 M->p[0][P->Dimension+1]);
2857 value_decrement(M->p[0][P->Dimension+1],
2858 M->p[0][P->Dimension+1]);
2859 pos = AddConstraints(M->p[0], 1, P, MaxRays);
2860 if (emptyQ(neg) || emptyQ(pos)) {
2861 Polyhedron_Free(pos);
2862 Polyhedron_Free(neg);
2863 continue;
2865 Polyhedron_Free(pos);
2866 value_increment(M->p[0][P->Dimension+1],
2867 M->p[0][P->Dimension+1]);
2868 pos = AddConstraints(M->p[0], 1, P, MaxRays);
2869 #ifdef DEBUG_ER
2870 fprintf(stderr, "\nER: ParamVertex\n");
2871 #endif /* DEBUG_ER */
2872 pos->next = neg;
2873 EP = enumerate_or(pos, exist, nparam, MaxRays);
2874 break;
2876 if (EP)
2877 break;
2878 } END_FORALL_PVertex_in_ParamPolyhedron;
2881 Matrix_Free(M);
2882 value_clear(f);
2885 if (CEq)
2886 Polyhedron_Free(CEq);
2887 if (CT)
2888 Matrix_Free(CT);
2889 if (PP)
2890 Param_Polyhedron_Free(PP);
2891 *PA = P;
2893 return EP;
2896 #ifndef HAVE_PIPLIB
2897 evalue *barvinok_enumerate_pip(Polyhedron *P,
2898 unsigned exist, unsigned nparam, unsigned MaxRays)
2900 return 0;
2902 #else
2903 evalue *barvinok_enumerate_pip(Polyhedron *P,
2904 unsigned exist, unsigned nparam, unsigned MaxRays)
2906 int nvar = P->Dimension - exist - nparam;
2907 evalue *EP = new_zero_ep();
2908 Polyhedron *Q, *N, *T = 0;
2909 Value min, tmp;
2910 value_init(min);
2911 value_init(tmp);
2913 #ifdef DEBUG_ER
2914 fprintf(stderr, "\nER: PIP\n");
2915 #endif /* DEBUG_ER */
2917 for (int i = 0; i < P->Dimension; ++i) {
2918 bool pos = false;
2919 bool neg = false;
2920 bool posray = false;
2921 bool negray = false;
2922 value_set_si(min, 0);
2923 for (int j = 0; j < P->NbRays; ++j) {
2924 if (value_pos_p(P->Ray[j][1+i])) {
2925 pos = true;
2926 if (value_zero_p(P->Ray[j][1+P->Dimension]))
2927 posray = true;
2928 } else if (value_neg_p(P->Ray[j][1+i])) {
2929 neg = true;
2930 if (value_zero_p(P->Ray[j][1+P->Dimension]))
2931 negray = true;
2932 else {
2933 mpz_fdiv_q(tmp,
2934 P->Ray[j][1+i], P->Ray[j][1+P->Dimension]);
2935 if (value_lt(tmp, min))
2936 value_assign(min, tmp);
2940 if (pos && neg) {
2941 assert(!(posray && negray));
2942 assert(!negray); // for now
2943 Polyhedron *O = T ? T : P;
2944 /* shift by a safe amount */
2945 Matrix *M = Matrix_Alloc(O->NbRays, O->Dimension+2);
2946 Vector_Copy(O->Ray[0], M->p[0], O->NbRays * (O->Dimension+2));
2947 for (int j = 0; j < P->NbRays; ++j) {
2948 if (value_notzero_p(M->p[j][1+P->Dimension])) {
2949 value_multiply(tmp, min, M->p[j][1+P->Dimension]);
2950 value_substract(M->p[j][1+i], M->p[j][1+i], tmp);
2953 if (T)
2954 Polyhedron_Free(T);
2955 T = Rays2Polyhedron(M, MaxRays);
2956 Matrix_Free(M);
2957 } else if (neg) {
2958 /* negating a parameter requires that we substitute in the
2959 * sign again afterwards.
2960 * Disallow for now.
2962 assert(i < nvar+exist);
2963 if (!T)
2964 T = Polyhedron_Copy(P);
2965 for (int j = 0; j < T->NbRays; ++j)
2966 value_oppose(T->Ray[j][1+i], T->Ray[j][1+i]);
2967 for (int j = 0; j < T->NbConstraints; ++j)
2968 value_oppose(T->Constraint[j][1+i], T->Constraint[j][1+i]);
2971 value_clear(min);
2972 value_clear(tmp);
2974 Polyhedron *D = pip_lexmin(T ? T : P, exist, nparam);
2975 for (Q = D; Q; Q = N) {
2976 N = Q->next;
2977 Q->next = 0;
2978 evalue *E;
2979 exist = Q->Dimension - nvar - nparam;
2980 E = barvinok_enumerate_e(Q, exist, nparam, MaxRays);
2981 Polyhedron_Free(Q);
2982 eadd(E, EP);
2983 free_evalue_refs(E);
2984 free(E);
2987 if (T)
2988 Polyhedron_Free(T);
2990 return EP;
2992 #endif
2995 static bool is_single(Value *row, int pos, int len)
2997 return First_Non_Zero(row, pos) == -1 &&
2998 First_Non_Zero(row+pos+1, len-pos-1) == -1;
3001 static evalue* barvinok_enumerate_e_r(Polyhedron *P,
3002 unsigned exist, unsigned nparam, unsigned MaxRays);
3004 #ifdef DEBUG_ER
3005 static int er_level = 0;
3007 evalue* barvinok_enumerate_e(Polyhedron *P,
3008 unsigned exist, unsigned nparam, unsigned MaxRays)
3010 fprintf(stderr, "\nER: level %i\n", er_level);
3011 int nvar = P->Dimension - exist - nparam;
3012 fprintf(stderr, "%d %d %d\n", nvar, exist, nparam);
3014 Polyhedron_Print(stderr, P_VALUE_FMT, P);
3015 ++er_level;
3016 P = DomainConstraintSimplify(Polyhedron_Copy(P), MaxRays);
3017 evalue *EP = barvinok_enumerate_e_r(P, exist, nparam, MaxRays);
3018 Polyhedron_Free(P);
3019 --er_level;
3020 return EP;
3022 #else
3023 evalue* barvinok_enumerate_e(Polyhedron *P,
3024 unsigned exist, unsigned nparam, unsigned MaxRays)
3026 P = DomainConstraintSimplify(Polyhedron_Copy(P), MaxRays);
3027 evalue *EP = barvinok_enumerate_e_r(P, exist, nparam, MaxRays);
3028 Polyhedron_Free(P);
3029 return EP;
3031 #endif
3033 static evalue* barvinok_enumerate_e_r(Polyhedron *P,
3034 unsigned exist, unsigned nparam, unsigned MaxRays)
3036 if (exist == 0) {
3037 Polyhedron *U = Universe_Polyhedron(nparam);
3038 evalue *EP = barvinok_enumerate_ev(P, U, MaxRays);
3039 //char *param_name[] = {"P", "Q", "R", "S", "T" };
3040 //print_evalue(stdout, EP, param_name);
3041 Polyhedron_Free(U);
3042 return EP;
3045 int nvar = P->Dimension - exist - nparam;
3046 int len = P->Dimension + 2;
3048 if (emptyQ(P))
3049 return new_zero_ep();
3051 if (nvar == 0 && nparam == 0) {
3052 evalue *EP = new_zero_ep();
3053 barvinok_count(P, &EP->x.n, MaxRays);
3054 if (value_pos_p(EP->x.n))
3055 value_set_si(EP->x.n, 1);
3056 return EP;
3059 int r;
3060 for (r = 0; r < P->NbRays; ++r)
3061 if (value_zero_p(P->Ray[r][0]) ||
3062 value_zero_p(P->Ray[r][P->Dimension+1])) {
3063 int i;
3064 for (i = 0; i < nvar; ++i)
3065 if (value_notzero_p(P->Ray[r][i+1]))
3066 break;
3067 if (i >= nvar)
3068 continue;
3069 for (i = nvar + exist; i < nvar + exist + nparam; ++i)
3070 if (value_notzero_p(P->Ray[r][i+1]))
3071 break;
3072 if (i >= nvar + exist + nparam)
3073 break;
3075 if (r < P->NbRays) {
3076 evalue *EP = new_zero_ep();
3077 value_set_si(EP->x.n, -1);
3078 return EP;
3081 int first;
3082 for (r = 0; r < P->NbEq; ++r)
3083 if ((first = First_Non_Zero(P->Constraint[r]+1+nvar, exist)) != -1)
3084 break;
3085 if (r < P->NbEq) {
3086 if (First_Non_Zero(P->Constraint[r]+1+nvar+first+1,
3087 exist-first-1) != -1) {
3088 Polyhedron *T = rotate_along(P, r, nvar, exist, MaxRays);
3089 #ifdef DEBUG_ER
3090 fprintf(stderr, "\nER: Equality\n");
3091 #endif /* DEBUG_ER */
3092 evalue *EP = barvinok_enumerate_e(T, exist-1, nparam, MaxRays);
3093 Polyhedron_Free(T);
3094 return EP;
3095 } else {
3096 #ifdef DEBUG_ER
3097 fprintf(stderr, "\nER: Fixed\n");
3098 #endif /* DEBUG_ER */
3099 if (first == 0)
3100 return barvinok_enumerate_e(P, exist-1, nparam, MaxRays);
3101 else {
3102 Polyhedron *T = Polyhedron_Copy(P);
3103 SwapColumns(T, nvar+1, nvar+1+first);
3104 evalue *EP = barvinok_enumerate_e(T, exist-1, nparam, MaxRays);
3105 Polyhedron_Free(T);
3106 return EP;
3111 Vector *row = Vector_Alloc(len);
3112 value_set_si(row->p[0], 1);
3114 Value f;
3115 value_init(f);
3117 enum constraint* info = new constraint[exist];
3118 for (int i = 0; i < exist; ++i) {
3119 info[i] = ALL_POS;
3120 for (int l = P->NbEq; l < P->NbConstraints; ++l) {
3121 if (value_negz_p(P->Constraint[l][nvar+i+1]))
3122 continue;
3123 bool l_parallel = is_single(P->Constraint[l]+nvar+1, i, exist);
3124 for (int u = P->NbEq; u < P->NbConstraints; ++u) {
3125 if (value_posz_p(P->Constraint[u][nvar+i+1]))
3126 continue;
3127 bool lu_parallel = l_parallel ||
3128 is_single(P->Constraint[u]+nvar+1, i, exist);
3129 value_oppose(f, P->Constraint[u][nvar+i+1]);
3130 Vector_Combine(P->Constraint[l]+1, P->Constraint[u]+1, row->p+1,
3131 f, P->Constraint[l][nvar+i+1], len-1);
3132 if (!(info[i] & INDEPENDENT)) {
3133 int j;
3134 for (j = 0; j < exist; ++j)
3135 if (j != i && value_notzero_p(row->p[nvar+j+1]))
3136 break;
3137 if (j == exist) {
3138 //printf("independent: i: %d, l: %d, u: %d\n", i, l, u);
3139 info[i] = (constraint)(info[i] | INDEPENDENT);
3142 if (info[i] & ALL_POS) {
3143 value_addto(row->p[len-1], row->p[len-1],
3144 P->Constraint[l][nvar+i+1]);
3145 value_addto(row->p[len-1], row->p[len-1], f);
3146 value_multiply(f, f, P->Constraint[l][nvar+i+1]);
3147 value_substract(row->p[len-1], row->p[len-1], f);
3148 value_decrement(row->p[len-1], row->p[len-1]);
3149 ConstraintSimplify(row->p, row->p, len, &f);
3150 value_set_si(f, -1);
3151 Vector_Scale(row->p+1, row->p+1, f, len-1);
3152 value_decrement(row->p[len-1], row->p[len-1]);
3153 Polyhedron *T = AddConstraints(row->p, 1, P, MaxRays);
3154 if (!emptyQ(T)) {
3155 //printf("not all_pos: i: %d, l: %d, u: %d\n", i, l, u);
3156 info[i] = (constraint)(info[i] ^ ALL_POS);
3158 //puts("pos remainder");
3159 //Polyhedron_Print(stdout, P_VALUE_FMT, T);
3160 Polyhedron_Free(T);
3162 if (!(info[i] & ONE_NEG)) {
3163 if (lu_parallel) {
3164 negative_test_constraint(P->Constraint[l],
3165 P->Constraint[u],
3166 row->p, nvar+i, len, &f);
3167 oppose_constraint(row->p, len, &f);
3168 Polyhedron *T = AddConstraints(row->p, 1, P, MaxRays);
3169 if (emptyQ(T)) {
3170 //printf("one_neg i: %d, l: %d, u: %d\n", i, l, u);
3171 info[i] = (constraint)(info[i] | ONE_NEG);
3173 //puts("neg remainder");
3174 //Polyhedron_Print(stdout, P_VALUE_FMT, T);
3175 Polyhedron_Free(T);
3178 if (!(info[i] & ALL_POS) && (info[i] & ONE_NEG))
3179 goto next;
3182 if (info[i] & ALL_POS)
3183 break;
3184 next:
3189 for (int i = 0; i < exist; ++i)
3190 printf("%i: %i\n", i, info[i]);
3192 for (int i = 0; i < exist; ++i)
3193 if (info[i] & ALL_POS) {
3194 #ifdef DEBUG_ER
3195 fprintf(stderr, "\nER: Positive\n");
3196 #endif /* DEBUG_ER */
3197 // Eliminate
3198 // Maybe we should chew off some of the fat here
3199 Matrix *M = Matrix_Alloc(P->Dimension, P->Dimension+1);
3200 for (int j = 0; j < P->Dimension; ++j)
3201 value_set_si(M->p[j][j + (j >= i+nvar)], 1);
3202 Polyhedron *T = Polyhedron_Image(P, M, MaxRays);
3203 Matrix_Free(M);
3204 evalue *EP = barvinok_enumerate_e(T, exist-1, nparam, MaxRays);
3205 Polyhedron_Free(T);
3206 value_clear(f);
3207 Vector_Free(row);
3208 delete [] info;
3209 return EP;
3211 for (int i = 0; i < exist; ++i)
3212 if (info[i] & ONE_NEG) {
3213 #ifdef DEBUG_ER
3214 fprintf(stderr, "\nER: Negative\n");
3215 #endif /* DEBUG_ER */
3216 Vector_Free(row);
3217 value_clear(f);
3218 delete [] info;
3219 if (i == 0)
3220 return barvinok_enumerate_e(P, exist-1, nparam, MaxRays);
3221 else {
3222 Polyhedron *T = Polyhedron_Copy(P);
3223 SwapColumns(T, nvar+1, nvar+1+i);
3224 evalue *EP = barvinok_enumerate_e(T, exist-1, nparam, MaxRays);
3225 Polyhedron_Free(T);
3226 return EP;
3229 for (int i = 0; i < exist; ++i)
3230 if (info[i] & INDEPENDENT) {
3231 Polyhedron *pos, *neg;
3233 /* Find constraint again and split off negative part */
3235 if (SplitOnVar(P, i, nvar, len, exist, MaxRays,
3236 row, f, true, &pos, &neg)) {
3237 #ifdef DEBUG_ER
3238 fprintf(stderr, "\nER: Split\n");
3239 #endif /* DEBUG_ER */
3241 evalue *EP =
3242 barvinok_enumerate_e(neg, exist-1, nparam, MaxRays);
3243 evalue *E =
3244 barvinok_enumerate_e(pos, exist, nparam, MaxRays);
3245 eadd(E, EP);
3246 free_evalue_refs(E);
3247 free(E);
3248 Polyhedron_Free(neg);
3249 Polyhedron_Free(pos);
3250 value_clear(f);
3251 Vector_Free(row);
3252 delete [] info;
3253 return EP;
3256 delete [] info;
3258 Polyhedron *O = P;
3259 Polyhedron *F;
3261 evalue *EP;
3263 EP = enumerate_line(P, exist, nparam, MaxRays);
3264 if (EP)
3265 goto out;
3267 EP = barvinok_enumerate_pip(P, exist, nparam, MaxRays);
3268 if (EP)
3269 goto out;
3271 EP = enumerate_redundant_ray(P, exist, nparam, MaxRays);
3272 if (EP)
3273 goto out;
3275 EP = enumerate_sure(P, exist, nparam, MaxRays);
3276 if (EP)
3277 goto out;
3279 EP = enumerate_ray(P, exist, nparam, MaxRays);
3280 if (EP)
3281 goto out;
3283 EP = enumerate_sure2(P, exist, nparam, MaxRays);
3284 if (EP)
3285 goto out;
3287 F = unfringe(P, MaxRays);
3288 if (!PolyhedronIncludes(F, P)) {
3289 #ifdef DEBUG_ER
3290 fprintf(stderr, "\nER: Fringed\n");
3291 #endif /* DEBUG_ER */
3292 EP = barvinok_enumerate_e(F, exist, nparam, MaxRays);
3293 Polyhedron_Free(F);
3294 goto out;
3296 Polyhedron_Free(F);
3298 if (nparam)
3299 EP = enumerate_vd(&P, exist, nparam, MaxRays);
3300 if (EP)
3301 goto out2;
3303 if (nvar != 0) {
3304 EP = enumerate_sum(P, exist, nparam, MaxRays);
3305 goto out2;
3308 assert(nvar == 0);
3310 int i;
3311 Polyhedron *pos, *neg;
3312 for (i = 0; i < exist; ++i)
3313 if (SplitOnVar(P, i, nvar, len, exist, MaxRays,
3314 row, f, false, &pos, &neg))
3315 break;
3317 assert (i < exist);
3319 pos->next = neg;
3320 EP = enumerate_or(pos, exist, nparam, MaxRays);
3322 out2:
3323 if (O != P)
3324 Polyhedron_Free(P);
3326 out:
3327 value_clear(f);
3328 Vector_Free(row);
3329 return EP;
3332 static void normalize(Polyhedron *i, vec_ZZ& lambda, ZZ& sign,
3333 ZZ& num_s, vec_ZZ& num_p, vec_ZZ& den_s, vec_ZZ& den_p,
3334 mat_ZZ& f)
3336 unsigned dim = i->Dimension;
3337 unsigned nparam = num_p.length();
3338 unsigned nvar = dim - nparam;
3340 int r = 0;
3341 mat_ZZ rays;
3342 rays.SetDims(dim, nvar);
3343 add_rays(rays, i, &r, nvar, true);
3344 den_s = rays * lambda;
3345 int change = 0;
3348 for (int j = 0; j < den_s.length(); ++j) {
3349 values2zz(i->Ray[j]+1+nvar, f[j], nparam);
3350 if (den_s[j] == 0) {
3351 den_p[j] = 1;
3352 continue;
3354 if (First_Non_Zero(i->Ray[j]+1+nvar, nparam) != -1) {
3355 if (den_s[j] > 0) {
3356 den_p[j] = -1;
3357 num_p -= f[j];
3358 } else
3359 den_p[j] = 1;
3360 } else
3361 den_p[j] = 0;
3362 if (den_s[j] > 0)
3363 change ^= 1;
3364 else {
3365 den_s[j] = abs(den_s[j]);
3366 num_s += den_s[j];
3370 if (change)
3371 sign = -sign;
3374 gen_fun * barvinok_series(Polyhedron *P, Polyhedron* C, unsigned MaxRays)
3376 Polyhedron ** vcone;
3377 Polyhedron *CA;
3378 unsigned nparam = C->Dimension;
3379 unsigned dim, nvar;
3380 vec_ZZ sign;
3381 int ncone = 0;
3382 sign.SetLength(ncone);
3384 CA = align_context(C, P->Dimension, MaxRays);
3385 P = DomainIntersection(P, CA, MaxRays);
3386 Polyhedron_Free(CA);
3388 assert(!Polyhedron_is_infinite(P, nparam));
3389 assert(P->NbBid == 0);
3390 assert(Polyhedron_has_positive_rays(P, nparam));
3391 assert(P->NbEq == 0);
3393 dim = P->Dimension;
3394 nvar = dim - nparam;
3395 vcone = new Polyhedron_p[P->NbRays];
3397 for (int j = 0; j < P->NbRays; ++j) {
3398 if (!value_pos_p(P->Ray[j][dim+1]))
3399 continue;
3401 int npos, nneg;
3402 Polyhedron *C = supporting_cone(P, j);
3403 decompose(C, &vcone[j], &npos, &nneg, MaxRays);
3404 ncone += npos + nneg;
3405 sign.SetLength(ncone);
3406 for (int k = 0; k < npos; ++k)
3407 sign[ncone-nneg-k-1] = 1;
3408 for (int k = 0; k < nneg; ++k)
3409 sign[ncone-k-1] = -1;
3412 mat_ZZ rays;
3413 rays.SetDims(ncone * dim, nvar);
3414 int r = 0;
3415 for (int j = 0; j < P->NbRays; ++j) {
3416 if (!value_pos_p(P->Ray[j][dim+1]))
3417 continue;
3419 for (Polyhedron *i = vcone[j]; i; i = i->next) {
3420 add_rays(rays, i, &r, nvar);
3423 rays.SetDims(r, nvar);
3424 vec_ZZ lambda;
3425 nonorthog(rays, lambda);
3426 //randomvector(P, lambda, nvar);
3429 cout << "rays: " << rays;
3430 cout << "lambda: " << lambda;
3433 int f = 0;
3434 ZZ num_s;
3435 vec_ZZ num_p;
3436 num_p.SetLength(nparam);
3437 vec_ZZ vertex;
3438 vec_ZZ den_s;
3439 den_s.SetLength(dim);
3440 vec_ZZ den_p;
3441 den_p.SetLength(dim);
3442 mat_ZZ den;
3443 den.SetDims(dim, nparam);
3444 ZZ one;
3445 one = 1;
3446 mpq_t count;
3447 mpq_init(count);
3449 gen_fun * gf = new gen_fun;
3451 for (int j = 0; j < P->NbRays; ++j) {
3452 if (!value_pos_p(P->Ray[j][dim+1]))
3453 continue;
3455 for (Polyhedron *i = vcone[j]; i; i = i->next, ++f) {
3456 lattice_point(P->Ray[j]+1, i, vertex);
3457 int k = 0;
3458 num_s = 0;
3459 for ( ; k < nvar; ++k)
3460 num_s += vertex[k] * lambda[k];
3461 for ( ; k < dim; ++k)
3462 num_p[k-nvar] = vertex[k];
3463 normalize(i, lambda, sign[f], num_s, num_p,
3464 den_s, den_p, den);
3466 int only_param = 0;
3467 int no_param = 0;
3468 for (int k = 0; k < dim; ++k) {
3469 if (den_p[k] == 0)
3470 ++no_param;
3471 else if (den_s[k] == 0)
3472 ++only_param;
3474 if (no_param == 0) {
3475 for (int k = 0; k < dim; ++k)
3476 if (den_p[k] == -1)
3477 den[k] = -den[k];
3478 gf->add(sign[f], one, num_p, den);
3479 } else if (no_param + only_param == dim) {
3480 int k, l;
3481 mat_ZZ pden;
3482 pden.SetDims(only_param, nparam);
3484 for (k = 0, l = 0; k < dim; ++k)
3485 if (den_p[k] != 0)
3486 pden[l++] = den[k];
3488 for (k = 0; k < dim; ++k)
3489 if (den_s[k] != 0)
3490 break;
3492 dpoly n(no_param, num_s);
3493 dpoly d(no_param, den_s[k], 1);
3494 for ( ; k < dim; ++k)
3495 if (den_s[k] != 0) {
3496 dpoly fact(no_param, den_s[k], 1);
3497 d *= fact;
3500 mpq_set_si(count, 0, 1);
3501 n.div(d, count, sign[f]);
3503 ZZ qn, qd;
3504 value2zz(mpq_numref(count), qn);
3505 value2zz(mpq_denref(count), qd);
3507 gf->add(qn, qd, num_p, pden);
3508 } else {
3509 int k, l;
3510 dpoly_r * r = 0;
3511 mat_ZZ pden;
3512 pden.SetDims(only_param, nparam);
3514 for (k = 0, l = 0; k < dim; ++k)
3515 if (den_s[k] == 0)
3516 pden[l++] = den[k];
3518 for (k = 0; k < dim; ++k)
3519 if (den_p[k] == 0)
3520 break;
3522 dpoly n(no_param, num_s);
3523 dpoly d(no_param, den_s[k], 1);
3524 for ( ; k < dim; ++k)
3525 if (den_p[k] == 0) {
3526 dpoly fact(no_param, den_s[k], 1);
3527 d *= fact;
3530 for (k = 0; k < dim; ++k) {
3531 if (den_s[k] == 0 || den_p[k] == 0)
3532 continue;
3534 dpoly pd(no_param-1, den_s[k], 1);
3535 int s = den_p[k] < 0 ? -1 : 1;
3537 if (r == 0)
3538 r = new dpoly_r(n, pd, k, s, dim);
3539 else
3540 assert(0); // for now
3543 r->div(d, sign[f], gf, pden, den, num_p);
3547 cout << "sign: " << sign[f];
3548 cout << "num_s: " << num_s;
3549 cout << "num_p: " << num_p;
3550 cout << "den_s: " << den_s;
3551 cout << "den_p: " << den_p;
3552 cout << "den: " << den;
3553 cout << "only_param: " << only_param;
3554 cout << "no_param: " << no_param;
3555 cout << endl;
3561 mpq_clear(count);
3563 return gf;