remove bottleneck + merge loops
[barvinok.git] / barvinok.cc
blobfeae70c092427ccfd3b0aa76932bc3c89c79d4ad
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 vec_ZZ sign;
1346 int ncone = 0;
1347 sign.SetLength(ncone);
1348 unsigned dim;
1349 int allocated = 0;
1350 Value factor;
1351 Polyhedron *Q;
1352 int r = 0;
1354 if (emptyQ(P)) {
1355 value_set_si(*result, 0);
1356 return;
1358 if (P->NbBid == 0)
1359 for (; r < P->NbRays; ++r)
1360 if (value_zero_p(P->Ray[r][P->Dimension+1]))
1361 break;
1362 if (P->NbBid !=0 || r < P->NbRays) {
1363 value_set_si(*result, -1);
1364 return;
1366 if (P->NbEq != 0) {
1367 P = remove_equalities(P);
1368 if (emptyQ(P)) {
1369 Polyhedron_Free(P);
1370 value_set_si(*result, 0);
1371 return;
1373 allocated = 1;
1375 value_init(factor);
1376 value_set_si(factor, 1);
1377 Q = Polyhedron_Reduce(P, &factor);
1378 if (Q) {
1379 if (allocated)
1380 Polyhedron_Free(P);
1381 P = Q;
1382 allocated = 1;
1384 if (P->Dimension == 0) {
1385 value_assign(*result, factor);
1386 if (allocated)
1387 Polyhedron_Free(P);
1388 value_clear(factor);
1389 return;
1392 dim = P->Dimension;
1393 vcone = new Polyhedron_p[P->NbRays];
1395 vec_ZZ lambda;
1396 //nonorthog(rays, lambda);
1397 randomvector(P, lambda, dim);
1398 //cout << "lambda: " << lambda << endl;
1400 mat_ZZ rays;
1401 rays.SetDims(dim, dim);
1403 ZZ num;
1404 vec_ZZ den;
1405 den.SetLength(dim);
1407 int f = 0;
1408 vec_ZZ vertex;
1409 mpq_t count;
1410 mpq_init(count);
1412 for (int j = 0; j < P->NbRays; ++j) {
1413 int npos, nneg;
1414 Polyhedron *C = supporting_cone(P, j);
1415 decompose(C, &vcone[j], &npos, &nneg, NbMaxCons);
1416 ncone += npos + nneg;
1417 sign.SetLength(ncone);
1418 for (int k = 0; k < npos; ++k)
1419 sign[ncone-nneg-k-1] = 1;
1420 for (int k = 0; k < nneg; ++k)
1421 sign[ncone-k-1] = -1;
1422 for (Polyhedron *i = vcone[j]; i; i = i->next) {
1423 r = 0;
1424 assert(i->NbRays-1 == dim);
1425 add_rays(rays, i, &r);
1426 for (int k = 0; k < dim; ++k) {
1427 assert(lambda * rays[k] != 0);
1430 lattice_point(P->Ray[j]+1, i, vertex);
1431 num = vertex * lambda;
1432 normalize(i, lambda, sign[f], num, den);
1434 dpoly d(dim, num);
1435 dpoly n(dim, den[0], 1);
1436 for (int k = 1; k < dim; ++k) {
1437 dpoly fact(dim, den[k], 1);
1438 n *= fact;
1440 d.div(n, count, sign[f]);
1442 ++f;
1444 Domain_Free(vcone[j]);
1447 assert(value_one_p(&count[0]._mp_den));
1448 value_multiply(*result, &count[0]._mp_num, factor);
1449 mpq_clear(count);
1451 delete [] vcone;
1453 if (allocated)
1454 Polyhedron_Free(P);
1455 value_clear(factor);
1458 static void uni_polynom(int param, Vector *c, evalue *EP)
1460 unsigned dim = c->Size-2;
1461 value_init(EP->d);
1462 value_set_si(EP->d,0);
1463 EP->x.p = new_enode(polynomial, dim+1, param+1);
1464 for (int j = 0; j <= dim; ++j)
1465 evalue_set(&EP->x.p->arr[j], c->p[j], c->p[dim+1]);
1468 static void multi_polynom(Vector *c, evalue* X, evalue *EP)
1470 unsigned dim = c->Size-2;
1471 evalue EC;
1473 value_init(EC.d);
1474 evalue_set(&EC, c->p[dim], c->p[dim+1]);
1476 value_init(EP->d);
1477 evalue_set(EP, c->p[dim], c->p[dim+1]);
1479 for (int i = dim-1; i >= 0; --i) {
1480 emul(X, EP);
1481 value_assign(EC.x.n, c->p[i]);
1482 eadd(&EC, EP);
1484 free_evalue_refs(&EC);
1487 Polyhedron *unfringe (Polyhedron *P, unsigned MaxRays)
1489 int len = P->Dimension+2;
1490 Polyhedron *T, *R = P;
1491 Value g;
1492 value_init(g);
1493 Vector *row = Vector_Alloc(len);
1494 value_set_si(row->p[0], 1);
1496 R = DomainConstraintSimplify(Polyhedron_Copy(P), MaxRays);
1498 Matrix *M = Matrix_Alloc(2, len-1);
1499 value_set_si(M->p[1][len-2], 1);
1500 for (int v = 0; v < P->Dimension; ++v) {
1501 value_set_si(M->p[0][v], 1);
1502 Polyhedron *I = Polyhedron_Image(P, M, 2+1);
1503 value_set_si(M->p[0][v], 0);
1504 for (int r = 0; r < I->NbConstraints; ++r) {
1505 if (value_zero_p(I->Constraint[r][0]))
1506 continue;
1507 if (value_zero_p(I->Constraint[r][1]))
1508 continue;
1509 if (value_one_p(I->Constraint[r][1]))
1510 continue;
1511 if (value_mone_p(I->Constraint[r][1]))
1512 continue;
1513 value_absolute(g, I->Constraint[r][1]);
1514 Vector_Set(row->p+1, 0, len-2);
1515 value_division(row->p[1+v], I->Constraint[r][1], g);
1516 mpz_fdiv_q(row->p[len-1], I->Constraint[r][2], g);
1517 T = R;
1518 R = AddConstraints(row->p, 1, R, MaxRays);
1519 if (T != P)
1520 Polyhedron_Free(T);
1522 Polyhedron_Free(I);
1524 Matrix_Free(M);
1525 Vector_Free(row);
1526 value_clear(g);
1527 return R;
1530 static Polyhedron *reduce_domain(Polyhedron *D, Matrix *CT, Polyhedron *CEq,
1531 Polyhedron **fVD, int nd, unsigned MaxRays)
1533 assert(CEq);
1535 Polyhedron *Dt;
1536 Dt = CT ? DomainPreimage(D, CT, MaxRays) : D;
1537 Polyhedron *rVD = DomainIntersection(Dt, CEq, MaxRays);
1539 /* if rVD is empty or too small in geometric dimension */
1540 if(!rVD || emptyQ(rVD) ||
1541 (rVD->Dimension-rVD->NbEq < Dt->Dimension-Dt->NbEq-CEq->NbEq)) {
1542 if(rVD)
1543 Domain_Free(rVD);
1544 if (CT)
1545 Domain_Free(Dt);
1546 return 0; /* empty validity domain */
1549 if (CT)
1550 Domain_Free(Dt);
1552 fVD[nd] = Domain_Copy(rVD);
1553 for (int i = 0 ; i < nd; ++i) {
1554 Polyhedron *I = DomainIntersection(fVD[nd], fVD[i], MaxRays);
1555 if (emptyQ(I)) {
1556 Domain_Free(I);
1557 continue;
1559 Polyhedron *F = DomainSimplify(I, fVD[nd], MaxRays);
1560 if (F->NbEq == 1) {
1561 Polyhedron *T = rVD;
1562 rVD = DomainDifference(rVD, F, MaxRays);
1563 Domain_Free(T);
1565 Domain_Free(F);
1566 Domain_Free(I);
1569 rVD = DomainConstraintSimplify(rVD, MaxRays);
1570 if (emptyQ(rVD)) {
1571 Domain_Free(fVD[nd]);
1572 Domain_Free(rVD);
1573 return 0;
1576 Value c;
1577 value_init(c);
1578 barvinok_count(rVD, &c, MaxRays);
1579 if (value_zero_p(c)) {
1580 Domain_Free(rVD);
1581 rVD = 0;
1583 value_clear(c);
1585 return rVD;
1588 static bool Polyhedron_is_infinite(Polyhedron *P, unsigned nparam)
1590 int r;
1591 for (r = 0; r < P->NbRays; ++r)
1592 if (value_zero_p(P->Ray[r][0]) ||
1593 value_zero_p(P->Ray[r][P->Dimension+1])) {
1594 int i;
1595 for (i = P->Dimension - nparam; i < P->Dimension; ++i)
1596 if (value_notzero_p(P->Ray[r][i+1]))
1597 break;
1598 if (i >= P->Dimension)
1599 break;
1601 return r < P->NbRays;
1604 /* Check whether all rays point in the positive directions
1605 * for the parameters
1607 static bool Polyhedron_has_positive_rays(Polyhedron *P, unsigned nparam)
1609 int r;
1610 for (r = 0; r < P->NbRays; ++r)
1611 if (value_zero_p(P->Ray[r][P->Dimension+1])) {
1612 int i;
1613 for (i = P->Dimension - nparam; i < P->Dimension; ++i)
1614 if (value_neg_p(P->Ray[r][i+1]))
1615 return false;
1617 return true;
1620 evalue* barvinok_enumerate_ev(Polyhedron *P, Polyhedron* C, unsigned MaxRays)
1622 //P = unfringe(P, MaxRays);
1623 Polyhedron *CEq = NULL, *rVD, *pVD, *CA;
1624 Matrix *CT = NULL;
1625 Param_Polyhedron *PP = NULL;
1626 Param_Domain *D, *next;
1627 Param_Vertices *V;
1628 int r = 0;
1629 unsigned nparam = C->Dimension;
1630 evalue *eres;
1631 ALLOC(evalue, eres);
1632 value_init(eres->d);
1633 value_set_si(eres->d, 0);
1635 evalue factor;
1636 value_init(factor.d);
1637 evalue_set_si(&factor, 1, 1);
1639 CA = align_context(C, P->Dimension, MaxRays);
1640 P = DomainIntersection(P, CA, MaxRays);
1641 Polyhedron_Free(CA);
1643 if (C->Dimension == 0 || emptyQ(P)) {
1644 constant:
1645 eres->x.p = new_enode(partition, 2, C->Dimension);
1646 EVALUE_SET_DOMAIN(eres->x.p->arr[0],
1647 DomainConstraintSimplify(CEq ? CEq : Polyhedron_Copy(C), MaxRays));
1648 value_set_si(eres->x.p->arr[1].d, 1);
1649 value_init(eres->x.p->arr[1].x.n);
1650 if (emptyQ(P))
1651 value_set_si(eres->x.p->arr[1].x.n, 0);
1652 else
1653 barvinok_count(P, &eres->x.p->arr[1].x.n, MaxRays);
1654 out:
1655 emul(&factor, eres);
1656 reduce_evalue(eres);
1657 free_evalue_refs(&factor);
1658 Polyhedron_Free(P);
1659 if (CT)
1660 Matrix_Free(CT);
1661 if (PP)
1662 Param_Polyhedron_Free(PP);
1664 return eres;
1666 if (Polyhedron_is_infinite(P, nparam))
1667 goto constant;
1669 if (P->NbEq != 0) {
1670 Matrix *f;
1671 P = remove_equalities_p(P, P->Dimension-nparam, &f);
1672 mask(f, &factor);
1673 Matrix_Free(f);
1675 if (P->Dimension == nparam) {
1676 CEq = P;
1677 P = Universe_Polyhedron(0);
1678 goto constant;
1681 Polyhedron *Q = ParamPolyhedron_Reduce(P, P->Dimension-nparam, &factor);
1682 if (Q) {
1683 Polyhedron_Free(P);
1684 if (Q->Dimension == nparam) {
1685 CEq = Q;
1686 P = Universe_Polyhedron(0);
1687 goto constant;
1689 P = Q;
1691 Polyhedron *oldP = P;
1692 PP = Polyhedron2Param_SimplifiedDomain(&P,C,MaxRays,&CEq,&CT);
1693 if (P != oldP)
1694 Polyhedron_Free(oldP);
1696 if (isIdentity(CT)) {
1697 Matrix_Free(CT);
1698 CT = NULL;
1699 } else {
1700 assert(CT->NbRows != CT->NbColumns);
1701 if (CT->NbRows == 1) // no more parameters
1702 goto constant;
1703 nparam = CT->NbRows - 1;
1706 unsigned dim = P->Dimension - nparam;
1707 Polyhedron ** vcone = new Polyhedron_p[PP->nbV];
1708 int * npos = new int[PP->nbV];
1709 int * nneg = new int[PP->nbV];
1710 ZZ sign;
1712 int i;
1713 for (i = 0, V = PP->V; V; ++i, V = V->next) {
1714 Polyhedron *C = supporting_cone_p(P, V);
1715 decompose(C, &vcone[i], &npos[i], &nneg[i], MaxRays);
1718 Vector *c = Vector_Alloc(dim+2);
1720 int nd;
1721 for (nd = 0, D=PP->D; D; ++nd, D=D->next);
1722 struct section { Polyhedron *D; evalue E; };
1723 section *s = new section[nd];
1724 Polyhedron **fVD = new Polyhedron_p[nd];
1726 for(nd = 0, D=PP->D; D; D=next) {
1727 next = D->next;
1729 Polyhedron *rVD = reduce_domain(D->Domain, CT, CEq,
1730 fVD, nd, MaxRays);
1731 if (!rVD)
1732 continue;
1734 pVD = CT ? DomainImage(rVD,CT,MaxRays) : rVD;
1736 int ncone = 0;
1737 FORALL_PVertex_in_ParamPolyhedron(V,D,PP) // _i is internal counter
1738 ncone += npos[_i] + nneg[_i];
1739 END_FORALL_PVertex_in_ParamPolyhedron;
1741 mat_ZZ rays;
1742 rays.SetDims(ncone * dim, dim);
1743 r = 0;
1744 FORALL_PVertex_in_ParamPolyhedron(V,D,PP) // _i is internal counter
1745 for (Polyhedron *i = vcone[_i]; i; i = i->next) {
1746 assert(i->NbRays-1 == dim);
1747 add_rays(rays, i, &r);
1749 END_FORALL_PVertex_in_ParamPolyhedron;
1750 vec_ZZ lambda;
1751 nonorthog(rays, lambda);
1753 vec_ZZ den;
1754 den.SetLength(dim);
1755 term_info num;
1757 value_init(s[nd].E.d);
1758 evalue_set_si(&s[nd].E, 0, 1);
1759 mpq_t count;
1760 mpq_init(count);
1761 FORALL_PVertex_in_ParamPolyhedron(V,D,PP)
1762 int f = 0;
1763 for (Polyhedron *i = vcone[_i]; i; i = i->next) {
1764 sign = f < npos[_i] ? 1 : -1;
1765 lattice_point(V, i, lambda, &num, pVD);
1766 normalize(i, lambda, sign, num.constant, den);
1768 dpoly n(dim, den[0], 1);
1769 for (int k = 1; k < dim; ++k) {
1770 dpoly fact(dim, den[k], 1);
1771 n *= fact;
1773 if (num.E != NULL) {
1774 ZZ one(INIT_VAL, 1);
1775 dpoly_n d(dim, num.constant, one);
1776 d.div(n, c, sign);
1777 evalue EV;
1778 multi_polynom(c, num.E, &EV);
1779 eadd(&EV , &s[nd].E);
1780 free_evalue_refs(&EV);
1781 free_evalue_refs(num.E);
1782 delete num.E;
1783 } else if (num.pos != -1) {
1784 dpoly_n d(dim, num.constant, num.coeff);
1785 d.div(n, c, sign);
1786 evalue EV;
1787 uni_polynom(num.pos, c, &EV);
1788 eadd(&EV , &s[nd].E);
1789 free_evalue_refs(&EV);
1790 } else {
1791 mpq_set_si(count, 0, 1);
1792 dpoly d(dim, num.constant);
1793 d.div(n, count, sign);
1794 evalue EV;
1795 value_init(EV.d);
1796 evalue_set(&EV, &count[0]._mp_num, &count[0]._mp_den);
1797 eadd(&EV , &s[nd].E);
1798 free_evalue_refs(&EV);
1800 ++f;
1802 END_FORALL_PVertex_in_ParamPolyhedron;
1804 mpq_clear(count);
1806 if (CT)
1807 addeliminatedparams_evalue(&s[nd].E, CT);
1808 s[nd].D = rVD;
1809 ++nd;
1810 if (rVD != pVD)
1811 Domain_Free(pVD);
1814 if (nd == 0)
1815 evalue_set_si(eres, 0, 1);
1816 else {
1817 eres->x.p = new_enode(partition, 2*nd, C->Dimension);
1818 for (int j = 0; j < nd; ++j) {
1819 EVALUE_SET_DOMAIN(eres->x.p->arr[2*j], s[j].D);
1820 value_clear(eres->x.p->arr[2*j+1].d);
1821 eres->x.p->arr[2*j+1] = s[j].E;
1822 Domain_Free(fVD[j]);
1825 delete [] s;
1826 delete [] fVD;
1828 Vector_Free(c);
1830 for (int j = 0; j < PP->nbV; ++j)
1831 Domain_Free(vcone[j]);
1832 delete [] vcone;
1833 delete [] npos;
1834 delete [] nneg;
1836 if (CEq)
1837 Polyhedron_Free(CEq);
1839 goto out;
1842 Enumeration* barvinok_enumerate(Polyhedron *P, Polyhedron* C, unsigned MaxRays)
1844 evalue *EP = barvinok_enumerate_ev(P, C, MaxRays);
1846 return partition2enumeration(EP);
1849 static void SwapColumns(Value **V, int n, int i, int j)
1851 for (int r = 0; r < n; ++r)
1852 value_swap(V[r][i], V[r][j]);
1855 static void SwapColumns(Polyhedron *P, int i, int j)
1857 SwapColumns(P->Constraint, P->NbConstraints, i, j);
1858 SwapColumns(P->Ray, P->NbRays, i, j);
1861 static void negative_test_constraint(Value *l, Value *u, Value *c, int pos,
1862 int len, Value *v)
1864 value_oppose(*v, u[pos+1]);
1865 Vector_Combine(l+1, u+1, c+1, *v, l[pos+1], len-1);
1866 value_multiply(*v, *v, l[pos+1]);
1867 value_substract(c[len-1], c[len-1], *v);
1868 value_set_si(*v, -1);
1869 Vector_Scale(c+1, c+1, *v, len-1);
1870 value_decrement(c[len-1], c[len-1]);
1871 ConstraintSimplify(c, c, len, v);
1874 static void oppose_constraint(Value *c, int len, Value *v)
1876 value_set_si(*v, -1);
1877 Vector_Scale(c+1, c+1, *v, len-1);
1878 value_decrement(c[len-1], c[len-1]);
1881 static bool SplitOnConstraint(Polyhedron *P, int i, int l, int u,
1882 int nvar, int len, int exist, int MaxRays,
1883 Vector *row, Value& f, bool independent,
1884 Polyhedron **pos, Polyhedron **neg)
1886 negative_test_constraint(P->Constraint[l], P->Constraint[u],
1887 row->p, nvar+i, len, &f);
1888 *neg = AddConstraints(row->p, 1, P, MaxRays);
1890 /* We found an independent, but useless constraint
1891 * Maybe we should detect this earlier and not
1892 * mark the variable as INDEPENDENT
1894 if (emptyQ((*neg))) {
1895 Polyhedron_Free(*neg);
1896 return false;
1899 oppose_constraint(row->p, len, &f);
1900 *pos = AddConstraints(row->p, 1, P, MaxRays);
1902 if (emptyQ((*pos))) {
1903 Polyhedron_Free(*neg);
1904 Polyhedron_Free(*pos);
1905 return false;
1908 return true;
1912 * unimodularly transform P such that constraint r is transformed
1913 * into a constraint that involves only a single (the first)
1914 * existential variable
1917 static Polyhedron *rotate_along(Polyhedron *P, int r, int nvar, int exist,
1918 unsigned MaxRays)
1920 Value g;
1921 value_init(g);
1923 Vector *row = Vector_Alloc(exist);
1924 Vector_Copy(P->Constraint[r]+1+nvar, row->p, exist);
1925 Vector_Gcd(row->p, exist, &g);
1926 if (value_notone_p(g))
1927 Vector_AntiScale(row->p, row->p, g, exist);
1928 value_clear(g);
1930 Matrix *M = unimodular_complete(row);
1931 Matrix *M2 = Matrix_Alloc(P->Dimension+1, P->Dimension+1);
1932 for (r = 0; r < nvar; ++r)
1933 value_set_si(M2->p[r][r], 1);
1934 for ( ; r < nvar+exist; ++r)
1935 Vector_Copy(M->p[r-nvar], M2->p[r]+nvar, exist);
1936 for ( ; r < P->Dimension+1; ++r)
1937 value_set_si(M2->p[r][r], 1);
1938 Polyhedron *T = Polyhedron_Image(P, M2, MaxRays);
1940 Matrix_Free(M2);
1941 Matrix_Free(M);
1942 Vector_Free(row);
1944 return T;
1947 static bool SplitOnVar(Polyhedron *P, int i,
1948 int nvar, int len, int exist, int MaxRays,
1949 Vector *row, Value& f, bool independent,
1950 Polyhedron **pos, Polyhedron **neg)
1952 int j;
1954 for (int l = P->NbEq; l < P->NbConstraints; ++l) {
1955 if (value_negz_p(P->Constraint[l][nvar+i+1]))
1956 continue;
1958 if (independent) {
1959 for (j = 0; j < exist; ++j)
1960 if (j != i && value_notzero_p(P->Constraint[l][nvar+j+1]))
1961 break;
1962 if (j < exist)
1963 continue;
1966 for (int u = P->NbEq; u < P->NbConstraints; ++u) {
1967 if (value_posz_p(P->Constraint[u][nvar+i+1]))
1968 continue;
1970 if (independent) {
1971 for (j = 0; j < exist; ++j)
1972 if (j != i && value_notzero_p(P->Constraint[u][nvar+j+1]))
1973 break;
1974 if (j < exist)
1975 continue;
1978 if (SplitOnConstraint(P, i, l, u,
1979 nvar, len, exist, MaxRays,
1980 row, f, independent,
1981 pos, neg)) {
1982 if (independent) {
1983 if (i != 0)
1984 SwapColumns(*neg, nvar+1, nvar+1+i);
1986 return true;
1991 return false;
1994 static bool double_bound_pair(Polyhedron *P, int nvar, int exist,
1995 int i, int l1, int l2,
1996 Polyhedron **pos, Polyhedron **neg)
1998 Value f;
1999 value_init(f);
2000 Vector *row = Vector_Alloc(P->Dimension+2);
2001 value_set_si(row->p[0], 1);
2002 value_oppose(f, P->Constraint[l1][nvar+i+1]);
2003 Vector_Combine(P->Constraint[l1]+1, P->Constraint[l2]+1,
2004 row->p+1,
2005 P->Constraint[l2][nvar+i+1], f,
2006 P->Dimension+1);
2007 ConstraintSimplify(row->p, row->p, P->Dimension+2, &f);
2008 *pos = AddConstraints(row->p, 1, P, 0);
2009 value_set_si(f, -1);
2010 Vector_Scale(row->p+1, row->p+1, f, P->Dimension+1);
2011 value_decrement(row->p[P->Dimension+1], row->p[P->Dimension+1]);
2012 *neg = AddConstraints(row->p, 1, P, 0);
2013 Vector_Free(row);
2014 value_clear(f);
2016 return !emptyQ((*pos)) && !emptyQ((*neg));
2019 static bool double_bound(Polyhedron *P, int nvar, int exist,
2020 Polyhedron **pos, Polyhedron **neg)
2022 for (int i = 0; i < exist; ++i) {
2023 int l1, l2;
2024 for (l1 = P->NbEq; l1 < P->NbConstraints; ++l1) {
2025 if (value_negz_p(P->Constraint[l1][nvar+i+1]))
2026 continue;
2027 for (l2 = l1 + 1; l2 < P->NbConstraints; ++l2) {
2028 if (value_negz_p(P->Constraint[l2][nvar+i+1]))
2029 continue;
2030 if (double_bound_pair(P, nvar, exist, i, l1, l2, pos, neg))
2031 return true;
2034 for (l1 = P->NbEq; l1 < P->NbConstraints; ++l1) {
2035 if (value_posz_p(P->Constraint[l1][nvar+i+1]))
2036 continue;
2037 if (l1 < P->NbConstraints)
2038 for (l2 = l1 + 1; l2 < P->NbConstraints; ++l2) {
2039 if (value_posz_p(P->Constraint[l2][nvar+i+1]))
2040 continue;
2041 if (double_bound_pair(P, nvar, exist, i, l1, l2, pos, neg))
2042 return true;
2045 return false;
2047 return false;
2050 enum constraint {
2051 ALL_POS = 1 << 0,
2052 ONE_NEG = 1 << 1,
2053 INDEPENDENT = 1 << 2
2056 static evalue* enumerate_or(Polyhedron *D,
2057 unsigned exist, unsigned nparam, unsigned MaxRays)
2059 #ifdef DEBUG_ER
2060 fprintf(stderr, "\nER: Or\n");
2061 #endif /* DEBUG_ER */
2063 Polyhedron *N = D->next;
2064 D->next = 0;
2065 evalue *EP =
2066 barvinok_enumerate_e(D, exist, nparam, MaxRays);
2067 Polyhedron_Free(D);
2069 for (D = N; D; D = N) {
2070 N = D->next;
2071 D->next = 0;
2073 evalue *EN =
2074 barvinok_enumerate_e(D, exist, nparam, MaxRays);
2076 eor(EN, EP);
2077 free_evalue_refs(EN);
2078 free(EN);
2079 Polyhedron_Free(D);
2082 reduce_evalue(EP);
2084 return EP;
2087 static evalue* enumerate_sum(Polyhedron *P,
2088 unsigned exist, unsigned nparam, unsigned MaxRays)
2090 int nvar = P->Dimension - exist - nparam;
2091 int toswap = nvar < exist ? nvar : exist;
2092 for (int i = 0; i < toswap; ++i)
2093 SwapColumns(P, 1 + i, nvar+exist - i);
2094 nparam += nvar;
2096 #ifdef DEBUG_ER
2097 fprintf(stderr, "\nER: Sum\n");
2098 #endif /* DEBUG_ER */
2100 evalue *EP = barvinok_enumerate_e(P, exist, nparam, MaxRays);
2102 for (int i = 0; i < /* nvar */ nparam; ++i) {
2103 Matrix *C = Matrix_Alloc(1, 1 + nparam + 1);
2104 value_set_si(C->p[0][0], 1);
2105 evalue split;
2106 value_init(split.d);
2107 value_set_si(split.d, 0);
2108 split.x.p = new_enode(partition, 4, nparam);
2109 value_set_si(C->p[0][1+i], 1);
2110 Matrix *C2 = Matrix_Copy(C);
2111 EVALUE_SET_DOMAIN(split.x.p->arr[0],
2112 Constraints2Polyhedron(C2, MaxRays));
2113 Matrix_Free(C2);
2114 evalue_set_si(&split.x.p->arr[1], 1, 1);
2115 value_set_si(C->p[0][1+i], -1);
2116 value_set_si(C->p[0][1+nparam], -1);
2117 EVALUE_SET_DOMAIN(split.x.p->arr[2],
2118 Constraints2Polyhedron(C, MaxRays));
2119 evalue_set_si(&split.x.p->arr[3], 1, 1);
2120 emul(&split, EP);
2121 free_evalue_refs(&split);
2122 Matrix_Free(C);
2124 reduce_evalue(EP);
2125 evalue_range_reduction(EP);
2127 evalue_frac2floor(EP);
2129 evalue *sum = esum(EP, nvar);
2131 free_evalue_refs(EP);
2132 free(EP);
2133 EP = sum;
2135 evalue_range_reduction(EP);
2137 return EP;
2140 static evalue* split_sure(Polyhedron *P, Polyhedron *S,
2141 unsigned exist, unsigned nparam, unsigned MaxRays)
2143 int nvar = P->Dimension - exist - nparam;
2145 Matrix *M = Matrix_Alloc(exist, S->Dimension+2);
2146 for (int i = 0; i < exist; ++i)
2147 value_set_si(M->p[i][nvar+i+1], 1);
2148 Polyhedron *O = S;
2149 S = DomainAddRays(S, M, MaxRays);
2150 Polyhedron_Free(O);
2151 Polyhedron *F = DomainAddRays(P, M, MaxRays);
2152 Polyhedron *D = DomainDifference(F, S, MaxRays);
2153 O = D;
2154 D = Disjoint_Domain(D, 0, MaxRays);
2155 Polyhedron_Free(F);
2156 Domain_Free(O);
2157 Matrix_Free(M);
2159 M = Matrix_Alloc(P->Dimension+1-exist, P->Dimension+1);
2160 for (int j = 0; j < nvar; ++j)
2161 value_set_si(M->p[j][j], 1);
2162 for (int j = 0; j < nparam+1; ++j)
2163 value_set_si(M->p[nvar+j][nvar+exist+j], 1);
2164 Polyhedron *T = Polyhedron_Image(S, M, MaxRays);
2165 evalue *EP = barvinok_enumerate_e(T, 0, nparam, MaxRays);
2166 Polyhedron_Free(S);
2167 Polyhedron_Free(T);
2168 Matrix_Free(M);
2170 for (Polyhedron *Q = D; Q; Q = Q->next) {
2171 Polyhedron *N = Q->next;
2172 Q->next = 0;
2173 T = DomainIntersection(P, Q, MaxRays);
2174 evalue *E = barvinok_enumerate_e(T, exist, nparam, MaxRays);
2175 eadd(E, EP);
2176 free_evalue_refs(E);
2177 free(E);
2178 Polyhedron_Free(T);
2179 Q->next = N;
2181 Domain_Free(D);
2182 return EP;
2185 static evalue* enumerate_sure(Polyhedron *P,
2186 unsigned exist, unsigned nparam, unsigned MaxRays)
2188 int i;
2189 Polyhedron *S = P;
2190 int nvar = P->Dimension - exist - nparam;
2191 Value lcm;
2192 Value f;
2193 value_init(lcm);
2194 value_init(f);
2196 for (i = 0; i < exist; ++i) {
2197 Matrix *M = Matrix_Alloc(S->NbConstraints, S->Dimension+2);
2198 int c = 0;
2199 value_set_si(lcm, 1);
2200 for (int j = 0; j < S->NbConstraints; ++j) {
2201 if (value_negz_p(S->Constraint[j][1+nvar+i]))
2202 continue;
2203 if (value_one_p(S->Constraint[j][1+nvar+i]))
2204 continue;
2205 value_lcm(lcm, S->Constraint[j][1+nvar+i], &lcm);
2208 for (int j = 0; j < S->NbConstraints; ++j) {
2209 if (value_negz_p(S->Constraint[j][1+nvar+i]))
2210 continue;
2211 if (value_one_p(S->Constraint[j][1+nvar+i]))
2212 continue;
2213 value_division(f, lcm, S->Constraint[j][1+nvar+i]);
2214 Vector_Scale(S->Constraint[j], M->p[c], f, S->Dimension+2);
2215 value_substract(M->p[c][S->Dimension+1],
2216 M->p[c][S->Dimension+1],
2217 lcm);
2218 value_increment(M->p[c][S->Dimension+1],
2219 M->p[c][S->Dimension+1]);
2220 ++c;
2222 Polyhedron *O = S;
2223 S = AddConstraints(M->p[0], c, S, MaxRays);
2224 if (O != P)
2225 Polyhedron_Free(O);
2226 Matrix_Free(M);
2227 if (emptyQ(S)) {
2228 Polyhedron_Free(S);
2229 value_clear(lcm);
2230 value_clear(f);
2231 return 0;
2234 value_clear(lcm);
2235 value_clear(f);
2237 #ifdef DEBUG_ER
2238 fprintf(stderr, "\nER: Sure\n");
2239 #endif /* DEBUG_ER */
2241 return split_sure(P, S, exist, nparam, MaxRays);
2244 static evalue* enumerate_sure2(Polyhedron *P,
2245 unsigned exist, unsigned nparam, unsigned MaxRays)
2247 int nvar = P->Dimension - exist - nparam;
2248 int r;
2249 for (r = 0; r < P->NbRays; ++r)
2250 if (value_one_p(P->Ray[r][0]) &&
2251 value_one_p(P->Ray[r][P->Dimension+1]))
2252 break;
2254 if (r >= P->NbRays)
2255 return 0;
2257 Matrix *M = Matrix_Alloc(nvar + 1 + nparam, P->Dimension+2);
2258 for (int i = 0; i < nvar; ++i)
2259 value_set_si(M->p[i][1+i], 1);
2260 for (int i = 0; i < nparam; ++i)
2261 value_set_si(M->p[i+nvar][1+nvar+exist+i], 1);
2262 Vector_Copy(P->Ray[r]+1+nvar, M->p[nvar+nparam]+1+nvar, exist);
2263 value_set_si(M->p[nvar+nparam][0], 1);
2264 value_set_si(M->p[nvar+nparam][P->Dimension+1], 1);
2265 Polyhedron * F = Rays2Polyhedron(M, MaxRays);
2266 Matrix_Free(M);
2268 Polyhedron *I = DomainIntersection(F, P, MaxRays);
2269 Polyhedron_Free(F);
2271 #ifdef DEBUG_ER
2272 fprintf(stderr, "\nER: Sure2\n");
2273 #endif /* DEBUG_ER */
2275 return split_sure(P, I, exist, nparam, MaxRays);
2278 static evalue* enumerate_cyclic(Polyhedron *P,
2279 unsigned exist, unsigned nparam,
2280 evalue * EP, int r, int p, unsigned MaxRays)
2282 int nvar = P->Dimension - exist - nparam;
2284 /* If EP in its fractional maps only contains references
2285 * to the remainder parameter with appropriate coefficients
2286 * then we could in principle avoid adding existentially
2287 * quantified variables to the validity domains.
2288 * We'd have to replace the remainder by m { p/m }
2289 * and multiply with an appropriate factor that is one
2290 * only in the appropriate range.
2291 * This last multiplication can be avoided if EP
2292 * has a single validity domain with no (further)
2293 * constraints on the remainder parameter
2296 Matrix *CT = Matrix_Alloc(nparam+1, nparam+3);
2297 Matrix *M = Matrix_Alloc(1, 1+nparam+3);
2298 for (int j = 0; j < nparam; ++j)
2299 if (j != p)
2300 value_set_si(CT->p[j][j], 1);
2301 value_set_si(CT->p[p][nparam+1], 1);
2302 value_set_si(CT->p[nparam][nparam+2], 1);
2303 value_set_si(M->p[0][1+p], -1);
2304 value_absolute(M->p[0][1+nparam], P->Ray[0][1+nvar+exist+p]);
2305 value_set_si(M->p[0][1+nparam+1], 1);
2306 Polyhedron *CEq = Constraints2Polyhedron(M, 1);
2307 Matrix_Free(M);
2308 addeliminatedparams_enum(EP, CT, CEq, MaxRays, nparam);
2309 Polyhedron_Free(CEq);
2310 Matrix_Free(CT);
2312 return EP;
2315 static void enumerate_vd_add_ray(evalue *EP, Matrix *Rays, unsigned MaxRays)
2317 if (value_notzero_p(EP->d))
2318 return;
2320 assert(EP->x.p->type == partition);
2321 assert(EP->x.p->pos == EVALUE_DOMAIN(EP->x.p->arr[0])->Dimension);
2322 for (int i = 0; i < EP->x.p->size/2; ++i) {
2323 Polyhedron *D = EVALUE_DOMAIN(EP->x.p->arr[2*i]);
2324 Polyhedron *N = DomainAddRays(D, Rays, MaxRays);
2325 EVALUE_SET_DOMAIN(EP->x.p->arr[2*i], N);
2326 Domain_Free(D);
2330 static evalue* enumerate_line(Polyhedron *P,
2331 unsigned exist, unsigned nparam, unsigned MaxRays)
2333 if (P->NbBid == 0)
2334 return 0;
2336 #ifdef DEBUG_ER
2337 fprintf(stderr, "\nER: Line\n");
2338 #endif /* DEBUG_ER */
2340 int nvar = P->Dimension - exist - nparam;
2341 int i, j;
2342 for (i = 0; i < nparam; ++i)
2343 if (value_notzero_p(P->Ray[0][1+nvar+exist+i]))
2344 break;
2345 assert(i < nparam);
2346 for (j = i+1; j < nparam; ++j)
2347 if (value_notzero_p(P->Ray[0][1+nvar+exist+i]))
2348 break;
2349 assert(j >= nparam); // for now
2351 Matrix *M = Matrix_Alloc(2, P->Dimension+2);
2352 value_set_si(M->p[0][0], 1);
2353 value_set_si(M->p[0][1+nvar+exist+i], 1);
2354 value_set_si(M->p[1][0], 1);
2355 value_set_si(M->p[1][1+nvar+exist+i], -1);
2356 value_absolute(M->p[1][1+P->Dimension], P->Ray[0][1+nvar+exist+i]);
2357 value_decrement(M->p[1][1+P->Dimension], M->p[1][1+P->Dimension]);
2358 Polyhedron *S = AddConstraints(M->p[0], 2, P, MaxRays);
2359 evalue *EP = barvinok_enumerate_e(S, exist, nparam, MaxRays);
2360 Polyhedron_Free(S);
2361 Matrix_Free(M);
2363 return enumerate_cyclic(P, exist, nparam, EP, 0, i, MaxRays);
2366 static int single_param_pos(Polyhedron*P, unsigned exist, unsigned nparam,
2367 int r)
2369 int nvar = P->Dimension - exist - nparam;
2370 if (First_Non_Zero(P->Ray[r]+1, nvar) != -1)
2371 return -1;
2372 int i = First_Non_Zero(P->Ray[r]+1+nvar+exist, nparam);
2373 if (i == -1)
2374 return -1;
2375 if (First_Non_Zero(P->Ray[r]+1+nvar+exist+1, nparam-i-1) != -1)
2376 return -1;
2377 return i;
2380 static evalue* enumerate_remove_ray(Polyhedron *P, int r,
2381 unsigned exist, unsigned nparam, unsigned MaxRays)
2383 #ifdef DEBUG_ER
2384 fprintf(stderr, "\nER: RedundantRay\n");
2385 #endif /* DEBUG_ER */
2387 Value one;
2388 value_init(one);
2389 value_set_si(one, 1);
2390 int len = P->NbRays-1;
2391 Matrix *M = Matrix_Alloc(2 * len, P->Dimension+2);
2392 Vector_Copy(P->Ray[0], M->p[0], r * (P->Dimension+2));
2393 Vector_Copy(P->Ray[r+1], M->p[r], (len-r) * (P->Dimension+2));
2394 for (int j = 0; j < P->NbRays; ++j) {
2395 if (j == r)
2396 continue;
2397 Vector_Combine(P->Ray[j], P->Ray[r], M->p[len+j-(j>r)],
2398 one, P->Ray[j][P->Dimension+1], P->Dimension+2);
2401 P = Rays2Polyhedron(M, MaxRays);
2402 Matrix_Free(M);
2403 evalue *EP = barvinok_enumerate_e(P, exist, nparam, MaxRays);
2404 Polyhedron_Free(P);
2405 value_clear(one);
2407 return EP;
2410 static evalue* enumerate_redundant_ray(Polyhedron *P,
2411 unsigned exist, unsigned nparam, unsigned MaxRays)
2413 assert(P->NbBid == 0);
2414 int nvar = P->Dimension - exist - nparam;
2415 Value m;
2416 value_init(m);
2418 for (int r = 0; r < P->NbRays; ++r) {
2419 if (value_notzero_p(P->Ray[r][P->Dimension+1]))
2420 continue;
2421 int i1 = single_param_pos(P, exist, nparam, r);
2422 if (i1 == -1)
2423 continue;
2424 for (int r2 = r+1; r2 < P->NbRays; ++r2) {
2425 if (value_notzero_p(P->Ray[r2][P->Dimension+1]))
2426 continue;
2427 int i2 = single_param_pos(P, exist, nparam, r2);
2428 if (i2 == -1)
2429 continue;
2430 if (i1 != i2)
2431 continue;
2433 value_division(m, P->Ray[r][1+nvar+exist+i1],
2434 P->Ray[r2][1+nvar+exist+i1]);
2435 value_multiply(m, m, P->Ray[r2][1+nvar+exist+i1]);
2436 /* r2 divides r => r redundant */
2437 if (value_eq(m, P->Ray[r][1+nvar+exist+i1])) {
2438 value_clear(m);
2439 return enumerate_remove_ray(P, r, exist, nparam, MaxRays);
2442 value_division(m, P->Ray[r2][1+nvar+exist+i1],
2443 P->Ray[r][1+nvar+exist+i1]);
2444 value_multiply(m, m, P->Ray[r][1+nvar+exist+i1]);
2445 /* r divides r2 => r2 redundant */
2446 if (value_eq(m, P->Ray[r2][1+nvar+exist+i1])) {
2447 value_clear(m);
2448 return enumerate_remove_ray(P, r2, exist, nparam, MaxRays);
2452 value_clear(m);
2453 return 0;
2456 static Polyhedron *upper_bound(Polyhedron *P,
2457 int pos, Value *max, Polyhedron **R)
2459 Value v;
2460 int r;
2461 value_init(v);
2463 *R = 0;
2464 Polyhedron *N;
2465 Polyhedron *B = 0;
2466 for (Polyhedron *Q = P; Q; Q = N) {
2467 N = Q->next;
2468 for (r = 0; r < P->NbRays; ++r) {
2469 if (value_zero_p(P->Ray[r][P->Dimension+1]) &&
2470 value_pos_p(P->Ray[r][1+pos]))
2471 break;
2473 if (r < P->NbRays) {
2474 Q->next = *R;
2475 *R = Q;
2476 continue;
2477 } else {
2478 Q->next = B;
2479 B = Q;
2481 for (r = 0; r < P->NbRays; ++r) {
2482 if (value_zero_p(P->Ray[r][P->Dimension+1]))
2483 continue;
2484 mpz_fdiv_q(v, P->Ray[r][1+pos], P->Ray[r][1+P->Dimension]);
2485 if ((!Q->next && r == 0) || value_gt(v, *max))
2486 value_assign(*max, v);
2489 value_clear(v);
2490 return B;
2493 static evalue* enumerate_ray(Polyhedron *P,
2494 unsigned exist, unsigned nparam, unsigned MaxRays)
2496 assert(P->NbBid == 0);
2497 int nvar = P->Dimension - exist - nparam;
2499 int r;
2500 for (r = 0; r < P->NbRays; ++r)
2501 if (value_zero_p(P->Ray[r][P->Dimension+1]))
2502 break;
2503 if (r >= P->NbRays)
2504 return 0;
2506 int r2;
2507 for (r2 = r+1; r2 < P->NbRays; ++r2)
2508 if (value_zero_p(P->Ray[r2][P->Dimension+1]))
2509 break;
2510 if (r2 < P->NbRays) {
2511 if (nvar > 0)
2512 return enumerate_sum(P, exist, nparam, MaxRays);
2515 #ifdef DEBUG_ER
2516 fprintf(stderr, "\nER: Ray\n");
2517 #endif /* DEBUG_ER */
2519 Value m;
2520 Value one;
2521 value_init(m);
2522 value_init(one);
2523 value_set_si(one, 1);
2524 int i = single_param_pos(P, exist, nparam, r);
2525 assert(i != -1); // for now;
2527 Matrix *M = Matrix_Alloc(P->NbRays, P->Dimension+2);
2528 for (int j = 0; j < P->NbRays; ++j) {
2529 Vector_Combine(P->Ray[j], P->Ray[r], M->p[j],
2530 one, P->Ray[j][P->Dimension+1], P->Dimension+2);
2532 Polyhedron *S = Rays2Polyhedron(M, MaxRays);
2533 Matrix_Free(M);
2534 Polyhedron *D = DomainDifference(P, S, MaxRays);
2535 Polyhedron_Free(S);
2536 // Polyhedron_Print(stderr, P_VALUE_FMT, D);
2537 assert(value_pos_p(P->Ray[r][1+nvar+exist+i])); // for now
2538 Polyhedron *R;
2539 D = upper_bound(D, nvar+exist+i, &m, &R);
2540 assert(D);
2541 Domain_Free(D);
2543 M = Matrix_Alloc(2, P->Dimension+2);
2544 value_set_si(M->p[0][0], 1);
2545 value_set_si(M->p[1][0], 1);
2546 value_set_si(M->p[0][1+nvar+exist+i], -1);
2547 value_set_si(M->p[1][1+nvar+exist+i], 1);
2548 value_assign(M->p[0][1+P->Dimension], m);
2549 value_oppose(M->p[1][1+P->Dimension], m);
2550 value_addto(M->p[1][1+P->Dimension], M->p[1][1+P->Dimension],
2551 P->Ray[r][1+nvar+exist+i]);
2552 value_decrement(M->p[1][1+P->Dimension], M->p[1][1+P->Dimension]);
2553 // Matrix_Print(stderr, P_VALUE_FMT, M);
2554 D = AddConstraints(M->p[0], 2, P, MaxRays);
2555 // Polyhedron_Print(stderr, P_VALUE_FMT, D);
2556 value_substract(M->p[0][1+P->Dimension], M->p[0][1+P->Dimension],
2557 P->Ray[r][1+nvar+exist+i]);
2558 // Matrix_Print(stderr, P_VALUE_FMT, M);
2559 S = AddConstraints(M->p[0], 1, P, MaxRays);
2560 // Polyhedron_Print(stderr, P_VALUE_FMT, S);
2561 Matrix_Free(M);
2563 evalue *EP = barvinok_enumerate_e(D, exist, nparam, MaxRays);
2564 Polyhedron_Free(D);
2565 value_clear(one);
2566 value_clear(m);
2568 if (value_notone_p(P->Ray[r][1+nvar+exist+i]))
2569 EP = enumerate_cyclic(P, exist, nparam, EP, r, i, MaxRays);
2570 else {
2571 M = Matrix_Alloc(1, nparam+2);
2572 value_set_si(M->p[0][0], 1);
2573 value_set_si(M->p[0][1+i], 1);
2574 enumerate_vd_add_ray(EP, M, MaxRays);
2575 Matrix_Free(M);
2578 if (!emptyQ(S)) {
2579 evalue *E = barvinok_enumerate_e(S, exist, nparam, MaxRays);
2580 eadd(E, EP);
2581 free_evalue_refs(E);
2582 free(E);
2584 Polyhedron_Free(S);
2586 if (R) {
2587 assert(nvar == 0);
2588 evalue *ER = enumerate_or(R, exist, nparam, MaxRays);
2589 eor(ER, EP);
2590 free_evalue_refs(ER);
2591 free(ER);
2594 return EP;
2597 static evalue* new_zero_ep()
2599 evalue *EP;
2600 ALLOC(evalue, EP);
2601 value_init(EP->d);
2602 evalue_set_si(EP, 0, 1);
2603 return EP;
2606 static evalue* enumerate_vd(Polyhedron **PA,
2607 unsigned exist, unsigned nparam, unsigned MaxRays)
2609 Polyhedron *P = *PA;
2610 int nvar = P->Dimension - exist - nparam;
2611 Param_Polyhedron *PP = NULL;
2612 Polyhedron *C = Universe_Polyhedron(nparam);
2613 Polyhedron *CEq;
2614 Matrix *CT;
2615 Polyhedron *PR = P;
2616 PP = Polyhedron2Param_SimplifiedDomain(&PR,C,MaxRays,&CEq,&CT);
2617 Polyhedron_Free(C);
2619 int nd;
2620 Param_Domain *D, *last;
2621 Value c;
2622 value_init(c);
2623 for (nd = 0, D=PP->D; D; D=D->next, ++nd)
2626 Polyhedron **VD = new Polyhedron_p[nd];
2627 Polyhedron **fVD = new Polyhedron_p[nd];
2628 for(nd = 0, D=PP->D; D; D=D->next) {
2629 Polyhedron *rVD = reduce_domain(D->Domain, CT, CEq,
2630 fVD, nd, MaxRays);
2631 if (!rVD)
2632 continue;
2634 VD[nd++] = rVD;
2635 last = D;
2638 evalue *EP = 0;
2640 if (nd == 0)
2641 EP = new_zero_ep();
2643 /* This doesn't seem to have any effect */
2644 if (nd == 1) {
2645 Polyhedron *CA = align_context(VD[0], P->Dimension, MaxRays);
2646 Polyhedron *O = P;
2647 P = DomainIntersection(P, CA, MaxRays);
2648 if (O != *PA)
2649 Polyhedron_Free(O);
2650 Polyhedron_Free(CA);
2651 if (emptyQ(P))
2652 EP = new_zero_ep();
2655 if (!EP && CT->NbColumns != CT->NbRows) {
2656 Polyhedron *CEqr = DomainImage(CEq, CT, MaxRays);
2657 Polyhedron *CA = align_context(CEqr, PR->Dimension, MaxRays);
2658 Polyhedron *I = DomainIntersection(PR, CA, MaxRays);
2659 Polyhedron_Free(CEqr);
2660 Polyhedron_Free(CA);
2661 #ifdef DEBUG_ER
2662 fprintf(stderr, "\nER: Eliminate\n");
2663 #endif /* DEBUG_ER */
2664 nparam -= CT->NbColumns - CT->NbRows;
2665 EP = barvinok_enumerate_e(I, exist, nparam, MaxRays);
2666 nparam += CT->NbColumns - CT->NbRows;
2667 addeliminatedparams_enum(EP, CT, CEq, MaxRays, nparam);
2668 Polyhedron_Free(I);
2670 if (PR != *PA)
2671 Polyhedron_Free(PR);
2672 PR = 0;
2674 if (!EP && nd > 1) {
2675 #ifdef DEBUG_ER
2676 fprintf(stderr, "\nER: VD\n");
2677 #endif /* DEBUG_ER */
2678 for (int i = 0; i < nd; ++i) {
2679 Polyhedron *CA = align_context(VD[i], P->Dimension, MaxRays);
2680 Polyhedron *I = DomainIntersection(P, CA, MaxRays);
2682 if (i == 0)
2683 EP = barvinok_enumerate_e(I, exist, nparam, MaxRays);
2684 else {
2685 evalue *E = barvinok_enumerate_e(I, exist, nparam, MaxRays);
2686 eadd(E, EP);
2687 free_evalue_refs(E);
2688 free(E);
2690 Polyhedron_Free(I);
2691 Polyhedron_Free(CA);
2695 for (int i = 0; i < nd; ++i) {
2696 Polyhedron_Free(VD[i]);
2697 Polyhedron_Free(fVD[i]);
2699 delete [] VD;
2700 delete [] fVD;
2701 value_clear(c);
2703 if (!EP && nvar == 0) {
2704 Value f;
2705 value_init(f);
2706 Param_Vertices *V, *V2;
2707 Matrix* M = Matrix_Alloc(1, P->Dimension+2);
2709 FORALL_PVertex_in_ParamPolyhedron(V, last, PP) {
2710 bool found = false;
2711 FORALL_PVertex_in_ParamPolyhedron(V2, last, PP) {
2712 if (V == V2) {
2713 found = true;
2714 continue;
2716 if (!found)
2717 continue;
2718 for (int i = 0; i < exist; ++i) {
2719 value_oppose(f, V->Vertex->p[i][nparam+1]);
2720 Vector_Combine(V->Vertex->p[i],
2721 V2->Vertex->p[i],
2722 M->p[0] + 1 + nvar + exist,
2723 V2->Vertex->p[i][nparam+1],
2725 nparam+1);
2726 int j;
2727 for (j = 0; j < nparam; ++j)
2728 if (value_notzero_p(M->p[0][1+nvar+exist+j]))
2729 break;
2730 if (j >= nparam)
2731 continue;
2732 ConstraintSimplify(M->p[0], M->p[0],
2733 P->Dimension+2, &f);
2734 value_set_si(M->p[0][0], 0);
2735 Polyhedron *para = AddConstraints(M->p[0], 1, P,
2736 MaxRays);
2737 if (emptyQ(para)) {
2738 Polyhedron_Free(para);
2739 continue;
2741 Polyhedron *pos, *neg;
2742 value_set_si(M->p[0][0], 1);
2743 value_decrement(M->p[0][P->Dimension+1],
2744 M->p[0][P->Dimension+1]);
2745 neg = AddConstraints(M->p[0], 1, P, MaxRays);
2746 value_set_si(f, -1);
2747 Vector_Scale(M->p[0]+1, M->p[0]+1, f,
2748 P->Dimension+1);
2749 value_decrement(M->p[0][P->Dimension+1],
2750 M->p[0][P->Dimension+1]);
2751 value_decrement(M->p[0][P->Dimension+1],
2752 M->p[0][P->Dimension+1]);
2753 pos = AddConstraints(M->p[0], 1, P, MaxRays);
2754 if (emptyQ(neg) && emptyQ(pos)) {
2755 Polyhedron_Free(para);
2756 Polyhedron_Free(pos);
2757 Polyhedron_Free(neg);
2758 continue;
2760 #ifdef DEBUG_ER
2761 fprintf(stderr, "\nER: Order\n");
2762 #endif /* DEBUG_ER */
2763 EP = barvinok_enumerate_e(para, exist, nparam, MaxRays);
2764 evalue *E;
2765 if (!emptyQ(pos)) {
2766 E = barvinok_enumerate_e(pos, exist, nparam, MaxRays);
2767 eadd(E, EP);
2768 free_evalue_refs(E);
2769 free(E);
2771 if (!emptyQ(neg)) {
2772 E = barvinok_enumerate_e(neg, exist, nparam, MaxRays);
2773 eadd(E, EP);
2774 free_evalue_refs(E);
2775 free(E);
2777 Polyhedron_Free(para);
2778 Polyhedron_Free(pos);
2779 Polyhedron_Free(neg);
2780 break;
2782 if (EP)
2783 break;
2784 } END_FORALL_PVertex_in_ParamPolyhedron;
2785 if (EP)
2786 break;
2787 } END_FORALL_PVertex_in_ParamPolyhedron;
2789 if (!EP) {
2790 /* Search for vertex coordinate to split on */
2791 /* First look for one independent of the parameters */
2792 FORALL_PVertex_in_ParamPolyhedron(V, last, PP) {
2793 for (int i = 0; i < exist; ++i) {
2794 int j;
2795 for (j = 0; j < nparam; ++j)
2796 if (value_notzero_p(V->Vertex->p[i][j]))
2797 break;
2798 if (j < nparam)
2799 continue;
2800 value_set_si(M->p[0][0], 1);
2801 Vector_Set(M->p[0]+1, 0, nvar+exist);
2802 Vector_Copy(V->Vertex->p[i],
2803 M->p[0] + 1 + nvar + exist, nparam+1);
2804 value_oppose(M->p[0][1+nvar+i],
2805 V->Vertex->p[i][nparam+1]);
2807 Polyhedron *pos, *neg;
2808 value_set_si(M->p[0][0], 1);
2809 value_decrement(M->p[0][P->Dimension+1],
2810 M->p[0][P->Dimension+1]);
2811 neg = AddConstraints(M->p[0], 1, P, MaxRays);
2812 value_set_si(f, -1);
2813 Vector_Scale(M->p[0]+1, M->p[0]+1, f,
2814 P->Dimension+1);
2815 value_decrement(M->p[0][P->Dimension+1],
2816 M->p[0][P->Dimension+1]);
2817 value_decrement(M->p[0][P->Dimension+1],
2818 M->p[0][P->Dimension+1]);
2819 pos = AddConstraints(M->p[0], 1, P, MaxRays);
2820 if (emptyQ(neg) || emptyQ(pos)) {
2821 Polyhedron_Free(pos);
2822 Polyhedron_Free(neg);
2823 continue;
2825 Polyhedron_Free(pos);
2826 value_increment(M->p[0][P->Dimension+1],
2827 M->p[0][P->Dimension+1]);
2828 pos = AddConstraints(M->p[0], 1, P, MaxRays);
2829 #ifdef DEBUG_ER
2830 fprintf(stderr, "\nER: Vertex\n");
2831 #endif /* DEBUG_ER */
2832 pos->next = neg;
2833 EP = enumerate_or(pos, exist, nparam, MaxRays);
2834 break;
2836 if (EP)
2837 break;
2838 } END_FORALL_PVertex_in_ParamPolyhedron;
2841 if (!EP) {
2842 /* Search for vertex coordinate to split on */
2843 /* Now look for one that depends on the parameters */
2844 FORALL_PVertex_in_ParamPolyhedron(V, last, PP) {
2845 for (int i = 0; i < exist; ++i) {
2846 value_set_si(M->p[0][0], 1);
2847 Vector_Set(M->p[0]+1, 0, nvar+exist);
2848 Vector_Copy(V->Vertex->p[i],
2849 M->p[0] + 1 + nvar + exist, nparam+1);
2850 value_oppose(M->p[0][1+nvar+i],
2851 V->Vertex->p[i][nparam+1]);
2853 Polyhedron *pos, *neg;
2854 value_set_si(M->p[0][0], 1);
2855 value_decrement(M->p[0][P->Dimension+1],
2856 M->p[0][P->Dimension+1]);
2857 neg = AddConstraints(M->p[0], 1, P, MaxRays);
2858 value_set_si(f, -1);
2859 Vector_Scale(M->p[0]+1, M->p[0]+1, f,
2860 P->Dimension+1);
2861 value_decrement(M->p[0][P->Dimension+1],
2862 M->p[0][P->Dimension+1]);
2863 value_decrement(M->p[0][P->Dimension+1],
2864 M->p[0][P->Dimension+1]);
2865 pos = AddConstraints(M->p[0], 1, P, MaxRays);
2866 if (emptyQ(neg) || emptyQ(pos)) {
2867 Polyhedron_Free(pos);
2868 Polyhedron_Free(neg);
2869 continue;
2871 Polyhedron_Free(pos);
2872 value_increment(M->p[0][P->Dimension+1],
2873 M->p[0][P->Dimension+1]);
2874 pos = AddConstraints(M->p[0], 1, P, MaxRays);
2875 #ifdef DEBUG_ER
2876 fprintf(stderr, "\nER: ParamVertex\n");
2877 #endif /* DEBUG_ER */
2878 pos->next = neg;
2879 EP = enumerate_or(pos, exist, nparam, MaxRays);
2880 break;
2882 if (EP)
2883 break;
2884 } END_FORALL_PVertex_in_ParamPolyhedron;
2887 Matrix_Free(M);
2888 value_clear(f);
2891 if (CEq)
2892 Polyhedron_Free(CEq);
2893 if (CT)
2894 Matrix_Free(CT);
2895 if (PP)
2896 Param_Polyhedron_Free(PP);
2897 *PA = P;
2899 return EP;
2902 #ifndef HAVE_PIPLIB
2903 evalue *barvinok_enumerate_pip(Polyhedron *P,
2904 unsigned exist, unsigned nparam, unsigned MaxRays)
2906 return 0;
2908 #else
2909 evalue *barvinok_enumerate_pip(Polyhedron *P,
2910 unsigned exist, unsigned nparam, unsigned MaxRays)
2912 int nvar = P->Dimension - exist - nparam;
2913 evalue *EP = new_zero_ep();
2914 Polyhedron *Q, *N, *T = 0;
2915 Value min, tmp;
2916 value_init(min);
2917 value_init(tmp);
2919 #ifdef DEBUG_ER
2920 fprintf(stderr, "\nER: PIP\n");
2921 #endif /* DEBUG_ER */
2923 for (int i = 0; i < P->Dimension; ++i) {
2924 bool pos = false;
2925 bool neg = false;
2926 bool posray = false;
2927 bool negray = false;
2928 value_set_si(min, 0);
2929 for (int j = 0; j < P->NbRays; ++j) {
2930 if (value_pos_p(P->Ray[j][1+i])) {
2931 pos = true;
2932 if (value_zero_p(P->Ray[j][1+P->Dimension]))
2933 posray = true;
2934 } else if (value_neg_p(P->Ray[j][1+i])) {
2935 neg = true;
2936 if (value_zero_p(P->Ray[j][1+P->Dimension]))
2937 negray = true;
2938 else {
2939 mpz_fdiv_q(tmp,
2940 P->Ray[j][1+i], P->Ray[j][1+P->Dimension]);
2941 if (value_lt(tmp, min))
2942 value_assign(min, tmp);
2946 if (pos && neg) {
2947 assert(!(posray && negray));
2948 assert(!negray); // for now
2949 Polyhedron *O = T ? T : P;
2950 /* shift by a safe amount */
2951 Matrix *M = Matrix_Alloc(O->NbRays, O->Dimension+2);
2952 Vector_Copy(O->Ray[0], M->p[0], O->NbRays * (O->Dimension+2));
2953 for (int j = 0; j < P->NbRays; ++j) {
2954 if (value_notzero_p(M->p[j][1+P->Dimension])) {
2955 value_multiply(tmp, min, M->p[j][1+P->Dimension]);
2956 value_substract(M->p[j][1+i], M->p[j][1+i], tmp);
2959 if (T)
2960 Polyhedron_Free(T);
2961 T = Rays2Polyhedron(M, MaxRays);
2962 Matrix_Free(M);
2963 } else if (neg) {
2964 /* negating a parameter requires that we substitute in the
2965 * sign again afterwards.
2966 * Disallow for now.
2968 assert(i < nvar+exist);
2969 if (!T)
2970 T = Polyhedron_Copy(P);
2971 for (int j = 0; j < T->NbRays; ++j)
2972 value_oppose(T->Ray[j][1+i], T->Ray[j][1+i]);
2973 for (int j = 0; j < T->NbConstraints; ++j)
2974 value_oppose(T->Constraint[j][1+i], T->Constraint[j][1+i]);
2977 value_clear(min);
2978 value_clear(tmp);
2980 Polyhedron *D = pip_lexmin(T ? T : P, exist, nparam);
2981 for (Q = D; Q; Q = N) {
2982 N = Q->next;
2983 Q->next = 0;
2984 evalue *E;
2985 exist = Q->Dimension - nvar - nparam;
2986 E = barvinok_enumerate_e(Q, exist, nparam, MaxRays);
2987 Polyhedron_Free(Q);
2988 eadd(E, EP);
2989 free_evalue_refs(E);
2990 free(E);
2993 if (T)
2994 Polyhedron_Free(T);
2996 return EP;
2998 #endif
3001 static bool is_single(Value *row, int pos, int len)
3003 return First_Non_Zero(row, pos) == -1 &&
3004 First_Non_Zero(row+pos+1, len-pos-1) == -1;
3007 static evalue* barvinok_enumerate_e_r(Polyhedron *P,
3008 unsigned exist, unsigned nparam, unsigned MaxRays);
3010 #ifdef DEBUG_ER
3011 static int er_level = 0;
3013 evalue* barvinok_enumerate_e(Polyhedron *P,
3014 unsigned exist, unsigned nparam, unsigned MaxRays)
3016 fprintf(stderr, "\nER: level %i\n", er_level);
3017 int nvar = P->Dimension - exist - nparam;
3018 fprintf(stderr, "%d %d %d\n", nvar, exist, nparam);
3020 Polyhedron_Print(stderr, P_VALUE_FMT, P);
3021 ++er_level;
3022 P = DomainConstraintSimplify(Polyhedron_Copy(P), MaxRays);
3023 evalue *EP = barvinok_enumerate_e_r(P, exist, nparam, MaxRays);
3024 Polyhedron_Free(P);
3025 --er_level;
3026 return EP;
3028 #else
3029 evalue* barvinok_enumerate_e(Polyhedron *P,
3030 unsigned exist, unsigned nparam, unsigned MaxRays)
3032 P = DomainConstraintSimplify(Polyhedron_Copy(P), MaxRays);
3033 evalue *EP = barvinok_enumerate_e_r(P, exist, nparam, MaxRays);
3034 Polyhedron_Free(P);
3035 return EP;
3037 #endif
3039 static evalue* barvinok_enumerate_e_r(Polyhedron *P,
3040 unsigned exist, unsigned nparam, unsigned MaxRays)
3042 if (exist == 0) {
3043 Polyhedron *U = Universe_Polyhedron(nparam);
3044 evalue *EP = barvinok_enumerate_ev(P, U, MaxRays);
3045 //char *param_name[] = {"P", "Q", "R", "S", "T" };
3046 //print_evalue(stdout, EP, param_name);
3047 Polyhedron_Free(U);
3048 return EP;
3051 int nvar = P->Dimension - exist - nparam;
3052 int len = P->Dimension + 2;
3054 if (emptyQ(P))
3055 return new_zero_ep();
3057 if (nvar == 0 && nparam == 0) {
3058 evalue *EP = new_zero_ep();
3059 barvinok_count(P, &EP->x.n, MaxRays);
3060 if (value_pos_p(EP->x.n))
3061 value_set_si(EP->x.n, 1);
3062 return EP;
3065 int r;
3066 for (r = 0; r < P->NbRays; ++r)
3067 if (value_zero_p(P->Ray[r][0]) ||
3068 value_zero_p(P->Ray[r][P->Dimension+1])) {
3069 int i;
3070 for (i = 0; i < nvar; ++i)
3071 if (value_notzero_p(P->Ray[r][i+1]))
3072 break;
3073 if (i >= nvar)
3074 continue;
3075 for (i = nvar + exist; i < nvar + exist + nparam; ++i)
3076 if (value_notzero_p(P->Ray[r][i+1]))
3077 break;
3078 if (i >= nvar + exist + nparam)
3079 break;
3081 if (r < P->NbRays) {
3082 evalue *EP = new_zero_ep();
3083 value_set_si(EP->x.n, -1);
3084 return EP;
3087 int first;
3088 for (r = 0; r < P->NbEq; ++r)
3089 if ((first = First_Non_Zero(P->Constraint[r]+1+nvar, exist)) != -1)
3090 break;
3091 if (r < P->NbEq) {
3092 if (First_Non_Zero(P->Constraint[r]+1+nvar+first+1,
3093 exist-first-1) != -1) {
3094 Polyhedron *T = rotate_along(P, r, nvar, exist, MaxRays);
3095 #ifdef DEBUG_ER
3096 fprintf(stderr, "\nER: Equality\n");
3097 #endif /* DEBUG_ER */
3098 evalue *EP = barvinok_enumerate_e(T, exist-1, nparam, MaxRays);
3099 Polyhedron_Free(T);
3100 return EP;
3101 } else {
3102 #ifdef DEBUG_ER
3103 fprintf(stderr, "\nER: Fixed\n");
3104 #endif /* DEBUG_ER */
3105 if (first == 0)
3106 return barvinok_enumerate_e(P, exist-1, nparam, MaxRays);
3107 else {
3108 Polyhedron *T = Polyhedron_Copy(P);
3109 SwapColumns(T, nvar+1, nvar+1+first);
3110 evalue *EP = barvinok_enumerate_e(T, exist-1, nparam, MaxRays);
3111 Polyhedron_Free(T);
3112 return EP;
3117 Vector *row = Vector_Alloc(len);
3118 value_set_si(row->p[0], 1);
3120 Value f;
3121 value_init(f);
3123 enum constraint* info = new constraint[exist];
3124 for (int i = 0; i < exist; ++i) {
3125 info[i] = ALL_POS;
3126 for (int l = P->NbEq; l < P->NbConstraints; ++l) {
3127 if (value_negz_p(P->Constraint[l][nvar+i+1]))
3128 continue;
3129 bool l_parallel = is_single(P->Constraint[l]+nvar+1, i, exist);
3130 for (int u = P->NbEq; u < P->NbConstraints; ++u) {
3131 if (value_posz_p(P->Constraint[u][nvar+i+1]))
3132 continue;
3133 bool lu_parallel = l_parallel ||
3134 is_single(P->Constraint[u]+nvar+1, i, exist);
3135 value_oppose(f, P->Constraint[u][nvar+i+1]);
3136 Vector_Combine(P->Constraint[l]+1, P->Constraint[u]+1, row->p+1,
3137 f, P->Constraint[l][nvar+i+1], len-1);
3138 if (!(info[i] & INDEPENDENT)) {
3139 int j;
3140 for (j = 0; j < exist; ++j)
3141 if (j != i && value_notzero_p(row->p[nvar+j+1]))
3142 break;
3143 if (j == exist) {
3144 //printf("independent: i: %d, l: %d, u: %d\n", i, l, u);
3145 info[i] = (constraint)(info[i] | INDEPENDENT);
3148 if (info[i] & ALL_POS) {
3149 value_addto(row->p[len-1], row->p[len-1],
3150 P->Constraint[l][nvar+i+1]);
3151 value_addto(row->p[len-1], row->p[len-1], f);
3152 value_multiply(f, f, P->Constraint[l][nvar+i+1]);
3153 value_substract(row->p[len-1], row->p[len-1], f);
3154 value_decrement(row->p[len-1], row->p[len-1]);
3155 ConstraintSimplify(row->p, row->p, len, &f);
3156 value_set_si(f, -1);
3157 Vector_Scale(row->p+1, row->p+1, f, len-1);
3158 value_decrement(row->p[len-1], row->p[len-1]);
3159 Polyhedron *T = AddConstraints(row->p, 1, P, MaxRays);
3160 if (!emptyQ(T)) {
3161 //printf("not all_pos: i: %d, l: %d, u: %d\n", i, l, u);
3162 info[i] = (constraint)(info[i] ^ ALL_POS);
3164 //puts("pos remainder");
3165 //Polyhedron_Print(stdout, P_VALUE_FMT, T);
3166 Polyhedron_Free(T);
3168 if (!(info[i] & ONE_NEG)) {
3169 if (lu_parallel) {
3170 negative_test_constraint(P->Constraint[l],
3171 P->Constraint[u],
3172 row->p, nvar+i, len, &f);
3173 oppose_constraint(row->p, len, &f);
3174 Polyhedron *T = AddConstraints(row->p, 1, P, MaxRays);
3175 if (emptyQ(T)) {
3176 //printf("one_neg i: %d, l: %d, u: %d\n", i, l, u);
3177 info[i] = (constraint)(info[i] | ONE_NEG);
3179 //puts("neg remainder");
3180 //Polyhedron_Print(stdout, P_VALUE_FMT, T);
3181 Polyhedron_Free(T);
3184 if (!(info[i] & ALL_POS) && (info[i] & ONE_NEG))
3185 goto next;
3188 if (info[i] & ALL_POS)
3189 break;
3190 next:
3195 for (int i = 0; i < exist; ++i)
3196 printf("%i: %i\n", i, info[i]);
3198 for (int i = 0; i < exist; ++i)
3199 if (info[i] & ALL_POS) {
3200 #ifdef DEBUG_ER
3201 fprintf(stderr, "\nER: Positive\n");
3202 #endif /* DEBUG_ER */
3203 // Eliminate
3204 // Maybe we should chew off some of the fat here
3205 Matrix *M = Matrix_Alloc(P->Dimension, P->Dimension+1);
3206 for (int j = 0; j < P->Dimension; ++j)
3207 value_set_si(M->p[j][j + (j >= i+nvar)], 1);
3208 Polyhedron *T = Polyhedron_Image(P, M, MaxRays);
3209 Matrix_Free(M);
3210 evalue *EP = barvinok_enumerate_e(T, exist-1, nparam, MaxRays);
3211 Polyhedron_Free(T);
3212 value_clear(f);
3213 Vector_Free(row);
3214 delete [] info;
3215 return EP;
3217 for (int i = 0; i < exist; ++i)
3218 if (info[i] & ONE_NEG) {
3219 #ifdef DEBUG_ER
3220 fprintf(stderr, "\nER: Negative\n");
3221 #endif /* DEBUG_ER */
3222 Vector_Free(row);
3223 value_clear(f);
3224 delete [] info;
3225 if (i == 0)
3226 return barvinok_enumerate_e(P, exist-1, nparam, MaxRays);
3227 else {
3228 Polyhedron *T = Polyhedron_Copy(P);
3229 SwapColumns(T, nvar+1, nvar+1+i);
3230 evalue *EP = barvinok_enumerate_e(T, exist-1, nparam, MaxRays);
3231 Polyhedron_Free(T);
3232 return EP;
3235 for (int i = 0; i < exist; ++i)
3236 if (info[i] & INDEPENDENT) {
3237 Polyhedron *pos, *neg;
3239 /* Find constraint again and split off negative part */
3241 if (SplitOnVar(P, i, nvar, len, exist, MaxRays,
3242 row, f, true, &pos, &neg)) {
3243 #ifdef DEBUG_ER
3244 fprintf(stderr, "\nER: Split\n");
3245 #endif /* DEBUG_ER */
3247 evalue *EP =
3248 barvinok_enumerate_e(neg, exist-1, nparam, MaxRays);
3249 evalue *E =
3250 barvinok_enumerate_e(pos, exist, nparam, MaxRays);
3251 eadd(E, EP);
3252 free_evalue_refs(E);
3253 free(E);
3254 Polyhedron_Free(neg);
3255 Polyhedron_Free(pos);
3256 value_clear(f);
3257 Vector_Free(row);
3258 delete [] info;
3259 return EP;
3262 delete [] info;
3264 Polyhedron *O = P;
3265 Polyhedron *F;
3267 evalue *EP;
3269 EP = enumerate_line(P, exist, nparam, MaxRays);
3270 if (EP)
3271 goto out;
3273 EP = barvinok_enumerate_pip(P, exist, nparam, MaxRays);
3274 if (EP)
3275 goto out;
3277 EP = enumerate_redundant_ray(P, exist, nparam, MaxRays);
3278 if (EP)
3279 goto out;
3281 EP = enumerate_sure(P, exist, nparam, MaxRays);
3282 if (EP)
3283 goto out;
3285 EP = enumerate_ray(P, exist, nparam, MaxRays);
3286 if (EP)
3287 goto out;
3289 EP = enumerate_sure2(P, exist, nparam, MaxRays);
3290 if (EP)
3291 goto out;
3293 F = unfringe(P, MaxRays);
3294 if (!PolyhedronIncludes(F, P)) {
3295 #ifdef DEBUG_ER
3296 fprintf(stderr, "\nER: Fringed\n");
3297 #endif /* DEBUG_ER */
3298 EP = barvinok_enumerate_e(F, exist, nparam, MaxRays);
3299 Polyhedron_Free(F);
3300 goto out;
3302 Polyhedron_Free(F);
3304 if (nparam)
3305 EP = enumerate_vd(&P, exist, nparam, MaxRays);
3306 if (EP)
3307 goto out2;
3309 if (nvar != 0) {
3310 EP = enumerate_sum(P, exist, nparam, MaxRays);
3311 goto out2;
3314 assert(nvar == 0);
3316 int i;
3317 Polyhedron *pos, *neg;
3318 for (i = 0; i < exist; ++i)
3319 if (SplitOnVar(P, i, nvar, len, exist, MaxRays,
3320 row, f, false, &pos, &neg))
3321 break;
3323 assert (i < exist);
3325 pos->next = neg;
3326 EP = enumerate_or(pos, exist, nparam, MaxRays);
3328 out2:
3329 if (O != P)
3330 Polyhedron_Free(P);
3332 out:
3333 value_clear(f);
3334 Vector_Free(row);
3335 return EP;
3338 static void normalize(Polyhedron *i, vec_ZZ& lambda, ZZ& sign,
3339 ZZ& num_s, vec_ZZ& num_p, vec_ZZ& den_s, vec_ZZ& den_p,
3340 mat_ZZ& f)
3342 unsigned dim = i->Dimension;
3343 unsigned nparam = num_p.length();
3344 unsigned nvar = dim - nparam;
3346 int r = 0;
3347 mat_ZZ rays;
3348 rays.SetDims(dim, nvar);
3349 add_rays(rays, i, &r, nvar, true);
3350 den_s = rays * lambda;
3351 int change = 0;
3354 for (int j = 0; j < den_s.length(); ++j) {
3355 values2zz(i->Ray[j]+1+nvar, f[j], nparam);
3356 if (den_s[j] == 0) {
3357 den_p[j] = 1;
3358 continue;
3360 if (First_Non_Zero(i->Ray[j]+1+nvar, nparam) != -1) {
3361 if (den_s[j] > 0) {
3362 den_p[j] = -1;
3363 num_p -= f[j];
3364 } else
3365 den_p[j] = 1;
3366 } else
3367 den_p[j] = 0;
3368 if (den_s[j] > 0)
3369 change ^= 1;
3370 else {
3371 den_s[j] = abs(den_s[j]);
3372 num_s += den_s[j];
3376 if (change)
3377 sign = -sign;
3380 gen_fun * barvinok_series(Polyhedron *P, Polyhedron* C, unsigned MaxRays)
3382 Polyhedron ** vcone;
3383 Polyhedron *CA;
3384 unsigned nparam = C->Dimension;
3385 unsigned dim, nvar;
3386 vec_ZZ sign;
3387 int ncone = 0;
3388 sign.SetLength(ncone);
3390 CA = align_context(C, P->Dimension, MaxRays);
3391 P = DomainIntersection(P, CA, MaxRays);
3392 Polyhedron_Free(CA);
3394 assert(!Polyhedron_is_infinite(P, nparam));
3395 assert(P->NbBid == 0);
3396 assert(Polyhedron_has_positive_rays(P, nparam));
3397 assert(P->NbEq == 0);
3399 dim = P->Dimension;
3400 nvar = dim - nparam;
3401 vcone = new Polyhedron_p[P->NbRays];
3403 for (int j = 0; j < P->NbRays; ++j) {
3404 if (!value_pos_p(P->Ray[j][dim+1]))
3405 continue;
3407 int npos, nneg;
3408 Polyhedron *C = supporting_cone(P, j);
3409 decompose(C, &vcone[j], &npos, &nneg, MaxRays);
3410 ncone += npos + nneg;
3411 sign.SetLength(ncone);
3412 for (int k = 0; k < npos; ++k)
3413 sign[ncone-nneg-k-1] = 1;
3414 for (int k = 0; k < nneg; ++k)
3415 sign[ncone-k-1] = -1;
3418 mat_ZZ rays;
3419 rays.SetDims(ncone * dim, nvar);
3420 int r = 0;
3421 for (int j = 0; j < P->NbRays; ++j) {
3422 if (!value_pos_p(P->Ray[j][dim+1]))
3423 continue;
3425 for (Polyhedron *i = vcone[j]; i; i = i->next) {
3426 add_rays(rays, i, &r, nvar);
3429 rays.SetDims(r, nvar);
3430 vec_ZZ lambda;
3431 nonorthog(rays, lambda);
3432 //randomvector(P, lambda, nvar);
3435 cout << "rays: " << rays;
3436 cout << "lambda: " << lambda;
3439 int f = 0;
3440 ZZ num_s;
3441 vec_ZZ num_p;
3442 num_p.SetLength(nparam);
3443 vec_ZZ vertex;
3444 vec_ZZ den_s;
3445 den_s.SetLength(dim);
3446 vec_ZZ den_p;
3447 den_p.SetLength(dim);
3448 mat_ZZ den;
3449 den.SetDims(dim, nparam);
3450 ZZ one;
3451 one = 1;
3452 mpq_t count;
3453 mpq_init(count);
3455 gen_fun * gf = new gen_fun;
3457 for (int j = 0; j < P->NbRays; ++j) {
3458 if (!value_pos_p(P->Ray[j][dim+1]))
3459 continue;
3461 for (Polyhedron *i = vcone[j]; i; i = i->next, ++f) {
3462 lattice_point(P->Ray[j]+1, i, vertex);
3463 int k = 0;
3464 num_s = 0;
3465 for ( ; k < nvar; ++k)
3466 num_s += vertex[k] * lambda[k];
3467 for ( ; k < dim; ++k)
3468 num_p[k-nvar] = vertex[k];
3469 normalize(i, lambda, sign[f], num_s, num_p,
3470 den_s, den_p, den);
3472 int only_param = 0;
3473 int no_param = 0;
3474 for (int k = 0; k < dim; ++k) {
3475 if (den_p[k] == 0)
3476 ++no_param;
3477 else if (den_s[k] == 0)
3478 ++only_param;
3480 if (no_param == 0) {
3481 for (int k = 0; k < dim; ++k)
3482 if (den_p[k] == -1)
3483 den[k] = -den[k];
3484 gf->add(sign[f], one, num_p, den);
3485 } else if (no_param + only_param == dim) {
3486 int k, l;
3487 mat_ZZ pden;
3488 pden.SetDims(only_param, nparam);
3490 for (k = 0, l = 0; k < dim; ++k)
3491 if (den_p[k] != 0)
3492 pden[l++] = den[k];
3494 for (k = 0; k < dim; ++k)
3495 if (den_s[k] != 0)
3496 break;
3498 dpoly n(no_param, num_s);
3499 dpoly d(no_param, den_s[k], 1);
3500 for ( ; k < dim; ++k)
3501 if (den_s[k] != 0) {
3502 dpoly fact(no_param, den_s[k], 1);
3503 d *= fact;
3506 mpq_set_si(count, 0, 1);
3507 n.div(d, count, sign[f]);
3509 ZZ qn, qd;
3510 value2zz(mpq_numref(count), qn);
3511 value2zz(mpq_denref(count), qd);
3513 gf->add(qn, qd, num_p, pden);
3514 } else {
3515 int k, l;
3516 dpoly_r * r = 0;
3517 mat_ZZ pden;
3518 pden.SetDims(only_param, nparam);
3520 for (k = 0, l = 0; k < dim; ++k)
3521 if (den_s[k] == 0)
3522 pden[l++] = den[k];
3524 for (k = 0; k < dim; ++k)
3525 if (den_p[k] == 0)
3526 break;
3528 dpoly n(no_param, num_s);
3529 dpoly d(no_param, den_s[k], 1);
3530 for ( ; k < dim; ++k)
3531 if (den_p[k] == 0) {
3532 dpoly fact(no_param, den_s[k], 1);
3533 d *= fact;
3536 for (k = 0; k < dim; ++k) {
3537 if (den_s[k] == 0 || den_p[k] == 0)
3538 continue;
3540 dpoly pd(no_param-1, den_s[k], 1);
3541 int s = den_p[k] < 0 ? -1 : 1;
3543 if (r == 0)
3544 r = new dpoly_r(n, pd, k, s, dim);
3545 else
3546 assert(0); // for now
3549 r->div(d, sign[f], gf, pden, den, num_p);
3553 cout << "sign: " << sign[f];
3554 cout << "num_s: " << num_s;
3555 cout << "num_p: " << num_p;
3556 cout << "den_s: " << den_s;
3557 cout << "den_p: " << den_p;
3558 cout << "den: " << den;
3559 cout << "only_param: " << only_param;
3560 cout << "no_param: " << no_param;
3561 cout << endl;
3567 mpq_clear(count);
3569 return gf;