distribute cdd2polylib.pl
[barvinok.git] / barvinok.cc
blob279efec659cd9e2fe995a51d4476b0c204724fbb
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 othogonal 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 add_rays(mat_ZZ& rays, Polyhedron *i, int *r, int nvar = -1,
626 bool all = false)
628 unsigned dim = i->Dimension;
629 if (nvar == -1)
630 nvar = dim;
631 for (int k = 0; k < i->NbRays; ++k) {
632 if (!value_zero_p(i->Ray[k][dim+1]))
633 continue;
634 if (!all && nvar != dim && First_Non_Zero(i->Ray[k]+1, nvar) == -1)
635 continue;
636 values2zz(i->Ray[k]+1, rays[(*r)++], nvar);
640 void lattice_point(Value* values, Polyhedron *i, vec_ZZ& vertex)
642 unsigned dim = i->Dimension;
643 if(!value_one_p(values[dim])) {
644 Matrix* Rays = rays(i);
645 Matrix *inv = Matrix_Alloc(Rays->NbRows, Rays->NbColumns);
646 int ok = Matrix_Inverse(Rays, inv);
647 assert(ok);
648 Matrix_Free(Rays);
649 Rays = rays(i);
650 Vector *lambda = Vector_Alloc(dim+1);
651 Vector_Matrix_Product(values, inv, lambda->p);
652 Matrix_Free(inv);
653 for (int j = 0; j < dim; ++j)
654 mpz_cdiv_q(lambda->p[j], lambda->p[j], lambda->p[dim]);
655 value_set_si(lambda->p[dim], 1);
656 Vector *A = Vector_Alloc(dim+1);
657 Vector_Matrix_Product(lambda->p, Rays, A->p);
658 Vector_Free(lambda);
659 Matrix_Free(Rays);
660 values2zz(A->p, vertex, dim);
661 Vector_Free(A);
662 } else
663 values2zz(values, vertex, dim);
666 static evalue *term(int param, ZZ& c, Value *den = NULL)
668 evalue *EP = new evalue();
669 value_init(EP->d);
670 value_set_si(EP->d,0);
671 EP->x.p = new_enode(polynomial, 2, param + 1);
672 evalue_set_si(&EP->x.p->arr[0], 0, 1);
673 value_init(EP->x.p->arr[1].x.n);
674 if (den == NULL)
675 value_set_si(EP->x.p->arr[1].d, 1);
676 else
677 value_assign(EP->x.p->arr[1].d, *den);
678 zz2value(c, EP->x.p->arr[1].x.n);
679 return EP;
682 static void vertex_period(
683 Polyhedron *i, vec_ZZ& lambda, Matrix *T,
684 Value lcm, int p, Vector *val,
685 evalue *E, evalue* ev,
686 ZZ& offset)
688 unsigned nparam = T->NbRows - 1;
689 unsigned dim = i->Dimension;
690 Value tmp;
691 ZZ nump;
693 if (p == nparam) {
694 vec_ZZ vertex;
695 ZZ num, l;
696 Vector * values = Vector_Alloc(dim + 1);
697 Vector_Matrix_Product(val->p, T, values->p);
698 value_assign(values->p[dim], lcm);
699 lattice_point(values->p, i, vertex);
700 num = vertex * lambda;
701 value2zz(lcm, l);
702 num *= l;
703 num += offset;
704 value_init(ev->x.n);
705 zz2value(num, ev->x.n);
706 value_assign(ev->d, lcm);
707 Vector_Free(values);
708 return;
711 value_init(tmp);
712 vec_ZZ vertex;
713 values2zz(T->p[p], vertex, dim);
714 nump = vertex * lambda;
715 if (First_Non_Zero(val->p, p) == -1) {
716 value_assign(tmp, lcm);
717 evalue *ET = term(p, nump, &tmp);
718 eadd(ET, E);
719 free_evalue_refs(ET);
720 delete ET;
723 value_assign(tmp, lcm);
724 if (First_Non_Zero(T->p[p], dim) != -1)
725 Vector_Gcd(T->p[p], dim, &tmp);
726 Gcd(tmp, lcm, &tmp);
727 if (value_lt(tmp, lcm)) {
728 ZZ count;
730 value_division(tmp, lcm, tmp);
731 value_set_si(ev->d, 0);
732 ev->x.p = new_enode(periodic, VALUE_TO_INT(tmp), p+1);
733 value2zz(tmp, count);
734 do {
735 value_decrement(tmp, tmp);
736 --count;
737 ZZ new_offset = offset - count * nump;
738 value_assign(val->p[p], tmp);
739 vertex_period(i, lambda, T, lcm, p+1, val, E,
740 &ev->x.p->arr[VALUE_TO_INT(tmp)], new_offset);
741 } while (value_pos_p(tmp));
742 } else
743 vertex_period(i, lambda, T, lcm, p+1, val, E, ev, offset);
744 value_clear(tmp);
747 static void mask_r(Matrix *f, int nr, Vector *lcm, int p, Vector *val, evalue *ev)
749 unsigned nparam = lcm->Size;
751 if (p == nparam) {
752 Vector * prod = Vector_Alloc(f->NbRows);
753 Matrix_Vector_Product(f, val->p, prod->p);
754 int isint = 1;
755 for (int i = 0; i < nr; ++i) {
756 value_modulus(prod->p[i], prod->p[i], f->p[i][nparam+1]);
757 isint &= value_zero_p(prod->p[i]);
759 value_set_si(ev->d, 1);
760 value_init(ev->x.n);
761 value_set_si(ev->x.n, isint);
762 Vector_Free(prod);
763 return;
766 Value tmp;
767 value_init(tmp);
768 if (value_one_p(lcm->p[p]))
769 mask_r(f, nr, lcm, p+1, val, ev);
770 else {
771 value_assign(tmp, lcm->p[p]);
772 value_set_si(ev->d, 0);
773 ev->x.p = new_enode(periodic, VALUE_TO_INT(tmp), p+1);
774 do {
775 value_decrement(tmp, tmp);
776 value_assign(val->p[p], tmp);
777 mask_r(f, nr, lcm, p+1, val, &ev->x.p->arr[VALUE_TO_INT(tmp)]);
778 } while (value_pos_p(tmp));
780 value_clear(tmp);
783 static evalue *multi_monom(vec_ZZ& p)
785 evalue *X = new evalue();
786 value_init(X->d);
787 value_init(X->x.n);
788 unsigned nparam = p.length()-1;
789 zz2value(p[nparam], X->x.n);
790 value_set_si(X->d, 1);
791 for (int i = 0; i < nparam; ++i) {
792 if (p[i] == 0)
793 continue;
794 evalue *T = term(i, p[i]);
795 eadd(T, X);
796 free_evalue_refs(T);
797 delete T;
799 return X;
803 * Check whether mapping polyhedron P on the affine combination
804 * num yields a range that has a fixed quotient on integer
805 * division by d
806 * If zero is true, then we are only interested in the quotient
807 * for the cases where the remainder is zero.
808 * Returns NULL if false and a newly allocated value if true.
810 static Value *fixed_quotient(Polyhedron *P, vec_ZZ& num, Value d, bool zero)
812 Value* ret = NULL;
813 int len = num.length();
814 Matrix *T = Matrix_Alloc(2, len);
815 zz2values(num, T->p[0]);
816 value_set_si(T->p[1][len-1], 1);
817 Polyhedron *I = Polyhedron_Image(P, T, P->NbConstraints);
818 Matrix_Free(T);
820 int i;
821 for (i = 0; i < I->NbRays; ++i)
822 if (value_zero_p(I->Ray[i][2])) {
823 Polyhedron_Free(I);
824 return NULL;
827 Value min, max;
828 value_init(min);
829 value_init(max);
830 int bounded = line_minmax(I, &min, &max);
831 assert(bounded);
833 if (zero)
834 mpz_cdiv_q(min, min, d);
835 else
836 mpz_fdiv_q(min, min, d);
837 mpz_fdiv_q(max, max, d);
839 if (value_eq(min, max)) {
840 ALLOC(Value, ret);
841 value_init(*ret);
842 value_assign(*ret, min);
844 value_clear(min);
845 value_clear(max);
846 return ret;
850 * Normalize linear expression coef modulo m
851 * Removes common factor and reduces coefficients
852 * Returns index of first non-zero coefficient or len
854 static int normal_mod(Value *coef, int len, Value *m)
856 Value gcd;
857 value_init(gcd);
859 Vector_Gcd(coef, len, &gcd);
860 Gcd(gcd, *m, &gcd);
861 Vector_AntiScale(coef, coef, gcd, len);
863 value_division(*m, *m, gcd);
864 value_clear(gcd);
866 if (value_one_p(*m))
867 return len;
869 int j;
870 for (j = 0; j < len; ++j)
871 mpz_fdiv_r(coef[j], coef[j], *m);
872 for (j = 0; j < len; ++j)
873 if (value_notzero_p(coef[j]))
874 break;
876 return j;
879 #ifdef USE_MODULO
880 static void mask(Matrix *f, evalue *factor)
882 int nr = f->NbRows, nc = f->NbColumns;
883 int n;
884 bool found = false;
885 for (n = 0; n < nr && value_notzero_p(f->p[n][nc-1]); ++n)
886 if (value_notone_p(f->p[n][nc-1]) &&
887 value_notmone_p(f->p[n][nc-1]))
888 found = true;
889 if (!found)
890 return;
892 evalue EP;
893 nr = n;
895 Value m;
896 value_init(m);
898 evalue EV;
899 value_init(EV.d);
900 value_init(EV.x.n);
901 value_set_si(EV.x.n, 1);
903 for (n = 0; n < nr; ++n) {
904 value_assign(m, f->p[n][nc-1]);
905 if (value_one_p(m) || value_mone_p(m))
906 continue;
908 int j = normal_mod(f->p[n], nc-1, &m);
909 if (j == nc-1) {
910 free_evalue_refs(factor);
911 value_init(factor->d);
912 evalue_set_si(factor, 0, 1);
913 break;
915 vec_ZZ row;
916 values2zz(f->p[n], row, nc-1);
917 ZZ g;
918 value2zz(m, g);
919 if (j < (nc-1)-1 && row[j] > g/2) {
920 for (int k = j; k < (nc-1); ++k)
921 if (row[k] != 0)
922 row[k] = g - row[k];
925 value_init(EP.d);
926 value_set_si(EP.d, 0);
927 EP.x.p = new_enode(relation, 2, 0);
928 value_clear(EP.x.p->arr[1].d);
929 EP.x.p->arr[1] = *factor;
930 evalue *ev = &EP.x.p->arr[0];
931 value_set_si(ev->d, 0);
932 ev->x.p = new_enode(fractional, 3, -1);
933 evalue_set_si(&ev->x.p->arr[1], 0, 1);
934 evalue_set_si(&ev->x.p->arr[2], 1, 1);
935 evalue *E = multi_monom(row);
936 value_assign(EV.d, m);
937 emul(&EV, E);
938 value_clear(ev->x.p->arr[0].d);
939 ev->x.p->arr[0] = *E;
940 delete E;
941 *factor = EP;
944 value_clear(m);
945 free_evalue_refs(&EV);
947 #else
951 static void mask(Matrix *f, evalue *factor)
953 int nr = f->NbRows, nc = f->NbColumns;
954 int n;
955 bool found = false;
956 for (n = 0; n < nr && value_notzero_p(f->p[n][nc-1]); ++n)
957 if (value_notone_p(f->p[n][nc-1]) &&
958 value_notmone_p(f->p[n][nc-1]))
959 found = true;
960 if (!found)
961 return;
963 Value tmp;
964 value_init(tmp);
965 nr = n;
966 unsigned np = nc - 2;
967 Vector *lcm = Vector_Alloc(np);
968 Vector *val = Vector_Alloc(nc);
969 Vector_Set(val->p, 0, nc);
970 value_set_si(val->p[np], 1);
971 Vector_Set(lcm->p, 1, np);
972 for (n = 0; n < nr; ++n) {
973 if (value_one_p(f->p[n][nc-1]) ||
974 value_mone_p(f->p[n][nc-1]))
975 continue;
976 for (int j = 0; j < np; ++j)
977 if (value_notzero_p(f->p[n][j])) {
978 Gcd(f->p[n][j], f->p[n][nc-1], &tmp);
979 value_division(tmp, f->p[n][nc-1], tmp);
980 value_lcm(tmp, lcm->p[j], &lcm->p[j]);
983 evalue EP;
984 value_init(EP.d);
985 mask_r(f, nr, lcm, 0, val, &EP);
986 value_clear(tmp);
987 Vector_Free(val);
988 Vector_Free(lcm);
989 emul(&EP,factor);
990 free_evalue_refs(&EP);
992 #endif
994 struct term_info {
995 evalue *E;
996 ZZ constant;
997 ZZ coeff;
998 int pos;
1001 static bool mod_needed(Polyhedron *PD, vec_ZZ& num, Value d, evalue *E)
1003 Value *q = fixed_quotient(PD, num, d, false);
1005 if (!q)
1006 return true;
1008 value_oppose(*q, *q);
1009 evalue EV;
1010 value_init(EV.d);
1011 value_set_si(EV.d, 1);
1012 value_init(EV.x.n);
1013 value_multiply(EV.x.n, *q, d);
1014 eadd(&EV, E);
1015 free_evalue_refs(&EV);
1016 value_clear(*q);
1017 free(q);
1018 return false;
1021 static void ceil_mod(Value *coef, int len, Value d, ZZ& f, evalue *EP, Polyhedron *PD)
1023 Value m;
1024 value_init(m);
1025 value_set_si(m, -1);
1027 Vector_Scale(coef, coef, m, len);
1029 value_assign(m, d);
1030 int j = normal_mod(coef, len, &m);
1032 if (j == len) {
1033 value_clear(m);
1034 return;
1037 vec_ZZ num;
1038 values2zz(coef, num, len);
1040 ZZ g;
1041 value2zz(m, g);
1043 evalue tmp;
1044 value_init(tmp.d);
1045 evalue_set_si(&tmp, 0, 1);
1047 int p = j;
1048 if (g % 2 == 0)
1049 while (j < len-1 && (num[j] == g/2 || num[j] == 0))
1050 ++j;
1051 if ((j < len-1 && num[j] > g/2) || (j == len-1 && num[j] >= (g+1)/2)) {
1052 for (int k = j; k < len-1; ++k)
1053 if (num[k] != 0)
1054 num[k] = g - num[k];
1055 num[len-1] = g - 1 - num[len-1];
1056 value_assign(tmp.d, m);
1057 ZZ t = f*(g-1);
1058 zz2value(t, tmp.x.n);
1059 eadd(&tmp, EP);
1060 f = -f;
1063 if (p >= len-1) {
1064 ZZ t = num[len-1] * f;
1065 zz2value(t, tmp.x.n);
1066 value_assign(tmp.d, m);
1067 eadd(&tmp, EP);
1068 } else {
1069 evalue *E = multi_monom(num);
1070 evalue EV;
1071 value_init(EV.d);
1073 if (PD && !mod_needed(PD, num, m, E)) {
1074 value_init(EV.x.n);
1075 zz2value(f, EV.x.n);
1076 value_assign(EV.d, m);
1077 emul(&EV, E);
1078 eadd(E, EP);
1079 } else {
1080 value_init(EV.x.n);
1081 value_set_si(EV.x.n, 1);
1082 value_assign(EV.d, m);
1083 emul(&EV, E);
1084 value_clear(EV.x.n);
1085 value_set_si(EV.d, 0);
1086 EV.x.p = new_enode(fractional, 3, -1);
1087 evalue_copy(&EV.x.p->arr[0], E);
1088 evalue_set_si(&EV.x.p->arr[1], 0, 1);
1089 value_init(EV.x.p->arr[2].x.n);
1090 zz2value(f, EV.x.p->arr[2].x.n);
1091 value_set_si(EV.x.p->arr[2].d, 1);
1093 eadd(&EV, EP);
1096 free_evalue_refs(&EV);
1097 free_evalue_refs(E);
1098 delete E;
1101 free_evalue_refs(&tmp);
1103 out:
1104 value_clear(m);
1107 evalue* bv_ceil3(Value *coef, int len, Value d, Polyhedron *P)
1109 Vector *val = Vector_Alloc(len);
1111 Value t;
1112 value_init(t);
1113 value_set_si(t, -1);
1114 Vector_Scale(coef, val->p, t, len);
1115 value_absolute(t, d);
1117 vec_ZZ num;
1118 values2zz(val->p, num, len);
1119 evalue *EP = multi_monom(num);
1121 evalue tmp;
1122 value_init(tmp.d);
1123 value_init(tmp.x.n);
1124 value_set_si(tmp.x.n, 1);
1125 value_assign(tmp.d, t);
1127 emul(&tmp, EP);
1129 ZZ one;
1130 one = 1;
1131 ceil_mod(val->p, len, t, one, EP, P);
1132 value_clear(t);
1134 /* copy EP to malloc'ed evalue */
1135 evalue *E;
1136 ALLOC(evalue, E);
1137 *E = *EP;
1138 delete EP;
1140 free_evalue_refs(&tmp);
1141 Vector_Free(val);
1143 return E;
1146 #ifdef USE_MODULO
1147 evalue* lattice_point(
1148 Polyhedron *i, vec_ZZ& lambda, Matrix *W, Value lcm, Polyhedron *PD)
1150 unsigned nparam = W->NbColumns - 1;
1152 Matrix* Rays = rays2(i);
1153 Matrix *T = Transpose(Rays);
1154 Matrix *T2 = Matrix_Copy(T);
1155 Matrix *inv = Matrix_Alloc(T2->NbRows, T2->NbColumns);
1156 int ok = Matrix_Inverse(T2, inv);
1157 assert(ok);
1158 Matrix_Free(Rays);
1159 Matrix_Free(T2);
1160 mat_ZZ vertex;
1161 matrix2zz(W, vertex, W->NbRows, W->NbColumns);
1163 vec_ZZ num;
1164 num = lambda * vertex;
1166 evalue *EP = multi_monom(num);
1168 evalue tmp;
1169 value_init(tmp.d);
1170 value_init(tmp.x.n);
1171 value_set_si(tmp.x.n, 1);
1172 value_assign(tmp.d, lcm);
1174 emul(&tmp, EP);
1176 Matrix *L = Matrix_Alloc(inv->NbRows, W->NbColumns);
1177 Matrix_Product(inv, W, L);
1179 mat_ZZ RT;
1180 matrix2zz(T, RT, T->NbRows, T->NbColumns);
1181 Matrix_Free(T);
1183 vec_ZZ p = lambda * RT;
1185 for (int i = 0; i < L->NbRows; ++i) {
1186 ceil_mod(L->p[i], nparam+1, lcm, p[i], EP, PD);
1189 Matrix_Free(L);
1191 Matrix_Free(inv);
1192 free_evalue_refs(&tmp);
1193 return EP;
1195 #else
1196 evalue* lattice_point(
1197 Polyhedron *i, vec_ZZ& lambda, Matrix *W, Value lcm, Polyhedron *PD)
1199 Matrix *T = Transpose(W);
1200 unsigned nparam = T->NbRows - 1;
1202 evalue *EP = new evalue();
1203 value_init(EP->d);
1204 evalue_set_si(EP, 0, 1);
1206 evalue ev;
1207 Vector *val = Vector_Alloc(nparam+1);
1208 value_set_si(val->p[nparam], 1);
1209 ZZ offset(INIT_VAL, 0);
1210 value_init(ev.d);
1211 vertex_period(i, lambda, T, lcm, 0, val, EP, &ev, offset);
1212 Vector_Free(val);
1213 eadd(&ev, EP);
1214 free_evalue_refs(&ev);
1216 Matrix_Free(T);
1218 reduce_evalue(EP);
1220 return EP;
1222 #endif
1224 void lattice_point(
1225 Param_Vertices* V, Polyhedron *i, vec_ZZ& lambda, term_info* term,
1226 Polyhedron *PD)
1228 unsigned nparam = V->Vertex->NbColumns - 2;
1229 unsigned dim = i->Dimension;
1230 mat_ZZ vertex;
1231 vertex.SetDims(V->Vertex->NbRows, nparam+1);
1232 Value lcm, tmp;
1233 value_init(lcm);
1234 value_init(tmp);
1235 value_set_si(lcm, 1);
1236 for (int j = 0; j < V->Vertex->NbRows; ++j) {
1237 value_lcm(lcm, V->Vertex->p[j][nparam+1], &lcm);
1239 if (value_notone_p(lcm)) {
1240 Matrix * mv = Matrix_Alloc(dim, nparam+1);
1241 for (int j = 0 ; j < dim; ++j) {
1242 value_division(tmp, lcm, V->Vertex->p[j][nparam+1]);
1243 Vector_Scale(V->Vertex->p[j], mv->p[j], tmp, nparam+1);
1246 term->E = lattice_point(i, lambda, mv, lcm, PD);
1247 term->constant = 0;
1249 Matrix_Free(mv);
1250 value_clear(lcm);
1251 value_clear(tmp);
1252 return;
1254 for (int i = 0; i < V->Vertex->NbRows; ++i) {
1255 assert(value_one_p(V->Vertex->p[i][nparam+1])); // for now
1256 values2zz(V->Vertex->p[i], vertex[i], nparam+1);
1259 vec_ZZ num;
1260 num = lambda * vertex;
1262 int p = -1;
1263 int nn = 0;
1264 for (int j = 0; j < nparam; ++j)
1265 if (num[j] != 0) {
1266 ++nn;
1267 p = j;
1269 if (nn >= 2) {
1270 term->E = multi_monom(num);
1271 term->constant = 0;
1272 } else {
1273 term->E = NULL;
1274 term->constant = num[nparam];
1275 term->pos = p;
1276 if (p != -1)
1277 term->coeff = num[p];
1280 value_clear(lcm);
1281 value_clear(tmp);
1284 void normalize(Polyhedron *i, vec_ZZ& lambda, ZZ& sign, ZZ& num, vec_ZZ& den)
1286 unsigned dim = i->Dimension;
1288 int r = 0;
1289 mat_ZZ rays;
1290 rays.SetDims(dim, dim);
1291 add_rays(rays, i, &r);
1292 den = rays * lambda;
1293 int change = 0;
1295 for (int j = 0; j < den.length(); ++j) {
1296 if (den[j] > 0)
1297 change ^= 1;
1298 else {
1299 den[j] = abs(den[j]);
1300 num += den[j];
1303 if (change)
1304 sign = -sign;
1307 typedef Polyhedron * Polyhedron_p;
1309 void barvinok_count(Polyhedron *P, Value* result, unsigned NbMaxCons)
1311 Polyhedron ** vcone;
1312 vec_ZZ sign;
1313 int ncone = 0;
1314 sign.SetLength(ncone);
1315 unsigned dim;
1316 int allocated = 0;
1317 Value factor;
1318 Polyhedron *Q;
1319 int r = 0;
1321 if (emptyQ(P)) {
1322 value_set_si(*result, 0);
1323 return;
1325 if (P->NbBid == 0)
1326 for (; r < P->NbRays; ++r)
1327 if (value_zero_p(P->Ray[r][P->Dimension+1]))
1328 break;
1329 if (P->NbBid !=0 || r < P->NbRays) {
1330 value_set_si(*result, -1);
1331 return;
1333 if (P->NbEq != 0) {
1334 P = remove_equalities(P);
1335 if (emptyQ(P)) {
1336 Polyhedron_Free(P);
1337 value_set_si(*result, 0);
1338 return;
1340 allocated = 1;
1342 value_init(factor);
1343 value_set_si(factor, 1);
1344 Q = Polyhedron_Reduce(P, &factor);
1345 if (Q) {
1346 if (allocated)
1347 Polyhedron_Free(P);
1348 P = Q;
1349 allocated = 1;
1351 if (P->Dimension == 0) {
1352 value_assign(*result, factor);
1353 if (allocated)
1354 Polyhedron_Free(P);
1355 value_clear(factor);
1356 return;
1359 dim = P->Dimension;
1360 vcone = new Polyhedron_p[P->NbRays];
1362 for (int j = 0; j < P->NbRays; ++j) {
1363 int npos, nneg;
1364 Polyhedron *C = supporting_cone(P, j);
1365 decompose(C, &vcone[j], &npos, &nneg, NbMaxCons);
1366 ncone += npos + nneg;
1367 sign.SetLength(ncone);
1368 for (int k = 0; k < npos; ++k)
1369 sign[ncone-nneg-k-1] = 1;
1370 for (int k = 0; k < nneg; ++k)
1371 sign[ncone-k-1] = -1;
1374 mat_ZZ rays;
1375 rays.SetDims(ncone * dim, dim);
1376 r = 0;
1377 for (int j = 0; j < P->NbRays; ++j) {
1378 for (Polyhedron *i = vcone[j]; i; i = i->next) {
1379 assert(i->NbRays-1 == dim);
1380 add_rays(rays, i, &r);
1383 vec_ZZ lambda;
1384 nonorthog(rays, lambda);
1386 ZZ num;
1387 vec_ZZ den;
1388 den.SetLength(dim);
1390 int f = 0;
1391 vec_ZZ vertex;
1392 mpq_t count;
1393 mpq_init(count);
1394 for (int j = 0; j < P->NbRays; ++j) {
1395 for (Polyhedron *i = vcone[j]; i; i = i->next) {
1396 lattice_point(P->Ray[j]+1, i, vertex);
1397 num = vertex * lambda;
1398 normalize(i, lambda, sign[f], num, den);
1400 dpoly d(dim, num);
1401 dpoly n(dim, den[0], 1);
1402 for (int k = 1; k < dim; ++k) {
1403 dpoly fact(dim, den[k], 1);
1404 n *= fact;
1406 d.div(n, count, sign[f]);
1408 ++f;
1410 Domain_Free(vcone[j]);
1413 assert(value_one_p(&count[0]._mp_den));
1414 value_multiply(*result, &count[0]._mp_num, factor);
1415 mpq_clear(count);
1417 delete [] vcone;
1419 if (allocated)
1420 Polyhedron_Free(P);
1421 value_clear(factor);
1424 static void uni_polynom(int param, Vector *c, evalue *EP)
1426 unsigned dim = c->Size-2;
1427 value_init(EP->d);
1428 value_set_si(EP->d,0);
1429 EP->x.p = new_enode(polynomial, dim+1, param+1);
1430 for (int j = 0; j <= dim; ++j)
1431 evalue_set(&EP->x.p->arr[j], c->p[j], c->p[dim+1]);
1434 static void multi_polynom(Vector *c, evalue* X, evalue *EP)
1436 unsigned dim = c->Size-2;
1437 evalue EC;
1439 value_init(EC.d);
1440 evalue_set(&EC, c->p[dim], c->p[dim+1]);
1442 value_init(EP->d);
1443 evalue_set(EP, c->p[dim], c->p[dim+1]);
1445 for (int i = dim-1; i >= 0; --i) {
1446 emul(X, EP);
1447 value_assign(EC.x.n, c->p[i]);
1448 eadd(&EC, EP);
1450 free_evalue_refs(&EC);
1453 Polyhedron *unfringe (Polyhedron *P, unsigned MaxRays)
1455 int len = P->Dimension+2;
1456 Polyhedron *T, *R = P;
1457 Value g;
1458 value_init(g);
1459 Vector *row = Vector_Alloc(len);
1460 value_set_si(row->p[0], 1);
1462 R = DomainConstraintSimplify(Polyhedron_Copy(P), MaxRays);
1464 Matrix *M = Matrix_Alloc(2, len-1);
1465 value_set_si(M->p[1][len-2], 1);
1466 for (int v = 0; v < P->Dimension; ++v) {
1467 value_set_si(M->p[0][v], 1);
1468 Polyhedron *I = Polyhedron_Image(P, M, 2+1);
1469 value_set_si(M->p[0][v], 0);
1470 for (int r = 0; r < I->NbConstraints; ++r) {
1471 if (value_zero_p(I->Constraint[r][0]))
1472 continue;
1473 if (value_zero_p(I->Constraint[r][1]))
1474 continue;
1475 if (value_one_p(I->Constraint[r][1]))
1476 continue;
1477 if (value_mone_p(I->Constraint[r][1]))
1478 continue;
1479 value_absolute(g, I->Constraint[r][1]);
1480 Vector_Set(row->p+1, 0, len-2);
1481 value_division(row->p[1+v], I->Constraint[r][1], g);
1482 mpz_fdiv_q(row->p[len-1], I->Constraint[r][2], g);
1483 T = R;
1484 R = AddConstraints(row->p, 1, R, MaxRays);
1485 if (T != P)
1486 Polyhedron_Free(T);
1488 Polyhedron_Free(I);
1490 Matrix_Free(M);
1491 Vector_Free(row);
1492 value_clear(g);
1493 return R;
1496 static Polyhedron *reduce_domain(Polyhedron *D, Matrix *CT, Polyhedron *CEq,
1497 Polyhedron **fVD, int nd, unsigned MaxRays)
1499 assert(CEq);
1501 Polyhedron *Dt;
1502 Dt = CT ? DomainPreimage(D, CT, MaxRays) : D;
1503 Polyhedron *rVD = DomainIntersection(Dt, CEq, MaxRays);
1505 /* if rVD is empty or too small in geometric dimension */
1506 if(!rVD || emptyQ(rVD) ||
1507 (rVD->Dimension-rVD->NbEq < Dt->Dimension-Dt->NbEq-CEq->NbEq)) {
1508 if(rVD)
1509 Domain_Free(rVD);
1510 if (CT)
1511 Domain_Free(Dt);
1512 return 0; /* empty validity domain */
1515 if (CT)
1516 Domain_Free(Dt);
1518 fVD[nd] = Domain_Copy(rVD);
1519 for (int i = 0 ; i < nd; ++i) {
1520 Polyhedron *I = DomainIntersection(fVD[nd], fVD[i], MaxRays);
1521 if (emptyQ(I)) {
1522 Domain_Free(I);
1523 continue;
1525 Polyhedron *F = DomainSimplify(I, fVD[nd], MaxRays);
1526 if (F->NbEq == 1) {
1527 Polyhedron *T = rVD;
1528 rVD = DomainDifference(rVD, F, MaxRays);
1529 Domain_Free(T);
1531 Domain_Free(F);
1532 Domain_Free(I);
1535 rVD = DomainConstraintSimplify(rVD, MaxRays);
1536 if (emptyQ(rVD)) {
1537 Domain_Free(fVD[nd]);
1538 Domain_Free(rVD);
1539 return 0;
1542 Value c;
1543 value_init(c);
1544 barvinok_count(rVD, &c, MaxRays);
1545 if (value_zero_p(c)) {
1546 Domain_Free(rVD);
1547 rVD = 0;
1549 value_clear(c);
1551 return rVD;
1554 static bool Polyhedron_is_infinite(Polyhedron *P, unsigned nparam)
1556 int r;
1557 for (r = 0; r < P->NbRays; ++r)
1558 if (value_zero_p(P->Ray[r][0]) ||
1559 value_zero_p(P->Ray[r][P->Dimension+1])) {
1560 int i;
1561 for (i = P->Dimension - nparam; i < P->Dimension; ++i)
1562 if (value_notzero_p(P->Ray[r][i+1]))
1563 break;
1564 if (i >= P->Dimension)
1565 break;
1567 return r < P->NbRays;
1570 /* Check whether all rays point in the positive directions
1571 * for the parameters
1573 static bool Polyhedron_has_positive_rays(Polyhedron *P, unsigned nparam)
1575 int r;
1576 for (r = 0; r < P->NbRays; ++r)
1577 if (value_zero_p(P->Ray[r][P->Dimension+1])) {
1578 int i;
1579 for (i = P->Dimension - nparam; i < P->Dimension; ++i)
1580 if (value_neg_p(P->Ray[r][i+1]))
1581 return false;
1583 return true;
1586 evalue* barvinok_enumerate_ev(Polyhedron *P, Polyhedron* C, unsigned MaxRays)
1588 //P = unfringe(P, MaxRays);
1589 Polyhedron *CEq = NULL, *rVD, *pVD, *CA;
1590 Matrix *CT = NULL;
1591 Param_Polyhedron *PP = NULL;
1592 Param_Domain *D, *next;
1593 Param_Vertices *V;
1594 int r = 0;
1595 unsigned nparam = C->Dimension;
1596 evalue *eres;
1597 ALLOC(evalue, eres);
1598 value_init(eres->d);
1599 value_set_si(eres->d, 0);
1601 evalue factor;
1602 value_init(factor.d);
1603 evalue_set_si(&factor, 1, 1);
1605 CA = align_context(C, P->Dimension, MaxRays);
1606 P = DomainIntersection(P, CA, MaxRays);
1607 Polyhedron_Free(CA);
1609 if (C->Dimension == 0 || emptyQ(P)) {
1610 constant:
1611 eres->x.p = new_enode(partition, 2, C->Dimension);
1612 EVALUE_SET_DOMAIN(eres->x.p->arr[0],
1613 DomainConstraintSimplify(CEq ? CEq : Polyhedron_Copy(C), MaxRays));
1614 value_set_si(eres->x.p->arr[1].d, 1);
1615 value_init(eres->x.p->arr[1].x.n);
1616 if (emptyQ(P))
1617 value_set_si(eres->x.p->arr[1].x.n, 0);
1618 else
1619 barvinok_count(P, &eres->x.p->arr[1].x.n, MaxRays);
1620 out:
1621 emul(&factor, eres);
1622 reduce_evalue(eres);
1623 free_evalue_refs(&factor);
1624 Polyhedron_Free(P);
1625 if (CT)
1626 Matrix_Free(CT);
1627 if (PP)
1628 Param_Polyhedron_Free(PP);
1630 return eres;
1632 if (Polyhedron_is_infinite(P, nparam))
1633 goto constant;
1635 if (P->NbEq != 0) {
1636 Matrix *f;
1637 P = remove_equalities_p(P, P->Dimension-nparam, &f);
1638 mask(f, &factor);
1639 Matrix_Free(f);
1641 if (P->Dimension == nparam) {
1642 CEq = P;
1643 P = Universe_Polyhedron(0);
1644 goto constant;
1647 Polyhedron *Q = ParamPolyhedron_Reduce(P, P->Dimension-nparam, &factor);
1648 if (Q) {
1649 Polyhedron_Free(P);
1650 if (Q->Dimension == nparam) {
1651 CEq = Q;
1652 P = Universe_Polyhedron(0);
1653 goto constant;
1655 P = Q;
1657 Polyhedron *oldP = P;
1658 PP = Polyhedron2Param_SimplifiedDomain(&P,C,MaxRays,&CEq,&CT);
1659 if (P != oldP)
1660 Polyhedron_Free(oldP);
1662 if (isIdentity(CT)) {
1663 Matrix_Free(CT);
1664 CT = NULL;
1665 } else {
1666 assert(CT->NbRows != CT->NbColumns);
1667 if (CT->NbRows == 1) // no more parameters
1668 goto constant;
1669 nparam = CT->NbRows - 1;
1672 unsigned dim = P->Dimension - nparam;
1673 Polyhedron ** vcone = new Polyhedron_p[PP->nbV];
1674 int * npos = new int[PP->nbV];
1675 int * nneg = new int[PP->nbV];
1676 ZZ sign;
1678 int i;
1679 for (i = 0, V = PP->V; V; ++i, V = V->next) {
1680 Polyhedron *C = supporting_cone_p(P, V);
1681 decompose(C, &vcone[i], &npos[i], &nneg[i], MaxRays);
1684 Vector *c = Vector_Alloc(dim+2);
1686 int nd;
1687 for (nd = 0, D=PP->D; D; ++nd, D=D->next);
1688 struct section { Polyhedron *D; evalue E; };
1689 section *s = new section[nd];
1690 Polyhedron **fVD = new Polyhedron_p[nd];
1692 for(nd = 0, D=PP->D; D; D=next) {
1693 next = D->next;
1695 Polyhedron *rVD = reduce_domain(D->Domain, CT, CEq,
1696 fVD, nd, MaxRays);
1697 if (!rVD)
1698 continue;
1700 pVD = CT ? DomainImage(rVD,CT,MaxRays) : rVD;
1702 int ncone = 0;
1703 FORALL_PVertex_in_ParamPolyhedron(V,D,PP) // _i is internal counter
1704 ncone += npos[_i] + nneg[_i];
1705 END_FORALL_PVertex_in_ParamPolyhedron;
1707 mat_ZZ rays;
1708 rays.SetDims(ncone * dim, dim);
1709 r = 0;
1710 FORALL_PVertex_in_ParamPolyhedron(V,D,PP) // _i is internal counter
1711 for (Polyhedron *i = vcone[_i]; i; i = i->next) {
1712 assert(i->NbRays-1 == dim);
1713 add_rays(rays, i, &r);
1715 END_FORALL_PVertex_in_ParamPolyhedron;
1716 vec_ZZ lambda;
1717 nonorthog(rays, lambda);
1719 vec_ZZ den;
1720 den.SetLength(dim);
1721 term_info num;
1723 value_init(s[nd].E.d);
1724 evalue_set_si(&s[nd].E, 0, 1);
1725 mpq_t count;
1726 mpq_init(count);
1727 FORALL_PVertex_in_ParamPolyhedron(V,D,PP)
1728 int f = 0;
1729 for (Polyhedron *i = vcone[_i]; i; i = i->next) {
1730 sign = f < npos[_i] ? 1 : -1;
1731 lattice_point(V, i, lambda, &num, pVD);
1732 normalize(i, lambda, sign, num.constant, den);
1734 dpoly n(dim, den[0], 1);
1735 for (int k = 1; k < dim; ++k) {
1736 dpoly fact(dim, den[k], 1);
1737 n *= fact;
1739 if (num.E != NULL) {
1740 ZZ one(INIT_VAL, 1);
1741 dpoly_n d(dim, num.constant, one);
1742 d.div(n, c, sign);
1743 evalue EV;
1744 multi_polynom(c, num.E, &EV);
1745 eadd(&EV , &s[nd].E);
1746 free_evalue_refs(&EV);
1747 free_evalue_refs(num.E);
1748 delete num.E;
1749 } else if (num.pos != -1) {
1750 dpoly_n d(dim, num.constant, num.coeff);
1751 d.div(n, c, sign);
1752 evalue EV;
1753 uni_polynom(num.pos, c, &EV);
1754 eadd(&EV , &s[nd].E);
1755 free_evalue_refs(&EV);
1756 } else {
1757 mpq_set_si(count, 0, 1);
1758 dpoly d(dim, num.constant);
1759 d.div(n, count, sign);
1760 evalue EV;
1761 value_init(EV.d);
1762 evalue_set(&EV, &count[0]._mp_num, &count[0]._mp_den);
1763 eadd(&EV , &s[nd].E);
1764 free_evalue_refs(&EV);
1766 ++f;
1768 END_FORALL_PVertex_in_ParamPolyhedron;
1770 mpq_clear(count);
1772 if (CT)
1773 addeliminatedparams_evalue(&s[nd].E, CT);
1774 s[nd].D = rVD;
1775 ++nd;
1776 if (rVD != pVD)
1777 Domain_Free(pVD);
1780 if (nd == 0)
1781 evalue_set_si(eres, 0, 1);
1782 else {
1783 eres->x.p = new_enode(partition, 2*nd, C->Dimension);
1784 for (int j = 0; j < nd; ++j) {
1785 EVALUE_SET_DOMAIN(eres->x.p->arr[2*j], s[j].D);
1786 value_clear(eres->x.p->arr[2*j+1].d);
1787 eres->x.p->arr[2*j+1] = s[j].E;
1788 Domain_Free(fVD[j]);
1791 delete [] s;
1792 delete [] fVD;
1794 Vector_Free(c);
1796 for (int j = 0; j < PP->nbV; ++j)
1797 Domain_Free(vcone[j]);
1798 delete [] vcone;
1799 delete [] npos;
1800 delete [] nneg;
1802 if (CEq)
1803 Polyhedron_Free(CEq);
1805 goto out;
1808 Enumeration* barvinok_enumerate(Polyhedron *P, Polyhedron* C, unsigned MaxRays)
1810 evalue *EP = barvinok_enumerate_ev(P, C, MaxRays);
1812 return partition2enumeration(EP);
1815 static void SwapColumns(Value **V, int n, int i, int j)
1817 for (int r = 0; r < n; ++r)
1818 value_swap(V[r][i], V[r][j]);
1821 static void SwapColumns(Polyhedron *P, int i, int j)
1823 SwapColumns(P->Constraint, P->NbConstraints, i, j);
1824 SwapColumns(P->Ray, P->NbRays, i, j);
1827 static void negative_test_constraint(Value *l, Value *u, Value *c, int pos,
1828 int len, Value *v)
1830 value_oppose(*v, u[pos+1]);
1831 Vector_Combine(l+1, u+1, c+1, *v, l[pos+1], len-1);
1832 value_multiply(*v, *v, l[pos+1]);
1833 value_substract(c[len-1], c[len-1], *v);
1834 value_set_si(*v, -1);
1835 Vector_Scale(c+1, c+1, *v, len-1);
1836 value_decrement(c[len-1], c[len-1]);
1837 ConstraintSimplify(c, c, len, v);
1840 static void oppose_constraint(Value *c, int len, Value *v)
1842 value_set_si(*v, -1);
1843 Vector_Scale(c+1, c+1, *v, len-1);
1844 value_decrement(c[len-1], c[len-1]);
1847 static bool SplitOnConstraint(Polyhedron *P, int i, int l, int u,
1848 int nvar, int len, int exist, int MaxRays,
1849 Vector *row, Value& f, bool independent,
1850 Polyhedron **pos, Polyhedron **neg)
1852 negative_test_constraint(P->Constraint[l], P->Constraint[u],
1853 row->p, nvar+i, len, &f);
1854 *neg = AddConstraints(row->p, 1, P, MaxRays);
1856 /* We found an independent, but useless constraint
1857 * Maybe we should detect this earlier and not
1858 * mark the variable as INDEPENDENT
1860 if (emptyQ((*neg))) {
1861 Polyhedron_Free(*neg);
1862 return false;
1865 oppose_constraint(row->p, len, &f);
1866 *pos = AddConstraints(row->p, 1, P, MaxRays);
1868 if (emptyQ((*pos))) {
1869 Polyhedron_Free(*neg);
1870 Polyhedron_Free(*pos);
1871 return false;
1874 return true;
1878 * unimodularly transform P such that constraint r is transformed
1879 * into a constraint that involves only a single (the first)
1880 * existential variable
1883 static Polyhedron *rotate_along(Polyhedron *P, int r, int nvar, int exist,
1884 unsigned MaxRays)
1886 Value g;
1887 value_init(g);
1889 Vector *row = Vector_Alloc(exist);
1890 Vector_Copy(P->Constraint[r]+1+nvar, row->p, exist);
1891 Vector_Gcd(row->p, exist, &g);
1892 if (value_notone_p(g))
1893 Vector_AntiScale(row->p, row->p, g, exist);
1894 value_clear(g);
1896 Matrix *M = unimodular_complete(row);
1897 Matrix *M2 = Matrix_Alloc(P->Dimension+1, P->Dimension+1);
1898 for (r = 0; r < nvar; ++r)
1899 value_set_si(M2->p[r][r], 1);
1900 for ( ; r < nvar+exist; ++r)
1901 Vector_Copy(M->p[r-nvar], M2->p[r]+nvar, exist);
1902 for ( ; r < P->Dimension+1; ++r)
1903 value_set_si(M2->p[r][r], 1);
1904 Polyhedron *T = Polyhedron_Image(P, M2, MaxRays);
1906 Matrix_Free(M2);
1907 Matrix_Free(M);
1908 Vector_Free(row);
1910 return T;
1913 static bool SplitOnVar(Polyhedron *P, int i,
1914 int nvar, int len, int exist, int MaxRays,
1915 Vector *row, Value& f, bool independent,
1916 Polyhedron **pos, Polyhedron **neg)
1918 int j;
1920 for (int l = P->NbEq; l < P->NbConstraints; ++l) {
1921 if (value_negz_p(P->Constraint[l][nvar+i+1]))
1922 continue;
1924 if (independent) {
1925 for (j = 0; j < exist; ++j)
1926 if (j != i && value_notzero_p(P->Constraint[l][nvar+j+1]))
1927 break;
1928 if (j < exist)
1929 continue;
1932 for (int u = P->NbEq; u < P->NbConstraints; ++u) {
1933 if (value_posz_p(P->Constraint[u][nvar+i+1]))
1934 continue;
1936 if (independent) {
1937 for (j = 0; j < exist; ++j)
1938 if (j != i && value_notzero_p(P->Constraint[u][nvar+j+1]))
1939 break;
1940 if (j < exist)
1941 continue;
1944 if (SplitOnConstraint(P, i, l, u,
1945 nvar, len, exist, MaxRays,
1946 row, f, independent,
1947 pos, neg)) {
1948 if (independent) {
1949 if (i != 0)
1950 SwapColumns(*neg, nvar+1, nvar+1+i);
1952 return true;
1957 return false;
1960 static bool double_bound_pair(Polyhedron *P, int nvar, int exist,
1961 int i, int l1, int l2,
1962 Polyhedron **pos, Polyhedron **neg)
1964 Value f;
1965 value_init(f);
1966 Vector *row = Vector_Alloc(P->Dimension+2);
1967 value_set_si(row->p[0], 1);
1968 value_oppose(f, P->Constraint[l1][nvar+i+1]);
1969 Vector_Combine(P->Constraint[l1]+1, P->Constraint[l2]+1,
1970 row->p+1,
1971 P->Constraint[l2][nvar+i+1], f,
1972 P->Dimension+1);
1973 ConstraintSimplify(row->p, row->p, P->Dimension+2, &f);
1974 *pos = AddConstraints(row->p, 1, P, 0);
1975 value_set_si(f, -1);
1976 Vector_Scale(row->p+1, row->p+1, f, P->Dimension+1);
1977 value_decrement(row->p[P->Dimension+1], row->p[P->Dimension+1]);
1978 *neg = AddConstraints(row->p, 1, P, 0);
1979 Vector_Free(row);
1980 value_clear(f);
1982 return !emptyQ((*pos)) && !emptyQ((*neg));
1985 static bool double_bound(Polyhedron *P, int nvar, int exist,
1986 Polyhedron **pos, Polyhedron **neg)
1988 for (int i = 0; i < exist; ++i) {
1989 int l1, l2;
1990 for (l1 = P->NbEq; l1 < P->NbConstraints; ++l1) {
1991 if (value_negz_p(P->Constraint[l1][nvar+i+1]))
1992 continue;
1993 for (l2 = l1 + 1; l2 < P->NbConstraints; ++l2) {
1994 if (value_negz_p(P->Constraint[l2][nvar+i+1]))
1995 continue;
1996 if (double_bound_pair(P, nvar, exist, i, l1, l2, pos, neg))
1997 return true;
2000 for (l1 = P->NbEq; l1 < P->NbConstraints; ++l1) {
2001 if (value_posz_p(P->Constraint[l1][nvar+i+1]))
2002 continue;
2003 if (l1 < P->NbConstraints)
2004 for (l2 = l1 + 1; l2 < P->NbConstraints; ++l2) {
2005 if (value_posz_p(P->Constraint[l2][nvar+i+1]))
2006 continue;
2007 if (double_bound_pair(P, nvar, exist, i, l1, l2, pos, neg))
2008 return true;
2011 return false;
2013 return false;
2016 enum constraint {
2017 ALL_POS = 1 << 0,
2018 ONE_NEG = 1 << 1,
2019 INDEPENDENT = 1 << 2
2022 static evalue* enumerate_or(Polyhedron *D,
2023 unsigned exist, unsigned nparam, unsigned MaxRays)
2025 #ifdef DEBUG_ER
2026 fprintf(stderr, "\nER: Or\n");
2027 #endif /* DEBUG_ER */
2029 Polyhedron *N = D->next;
2030 D->next = 0;
2031 evalue *EP =
2032 barvinok_enumerate_e(D, exist, nparam, MaxRays);
2033 Polyhedron_Free(D);
2035 for (D = N; D; D = N) {
2036 N = D->next;
2037 D->next = 0;
2039 evalue *EN =
2040 barvinok_enumerate_e(D, exist, nparam, MaxRays);
2042 eor(EN, EP);
2043 free_evalue_refs(EN);
2044 free(EN);
2045 Polyhedron_Free(D);
2048 reduce_evalue(EP);
2050 return EP;
2053 static evalue* enumerate_sum(Polyhedron *P,
2054 unsigned exist, unsigned nparam, unsigned MaxRays)
2056 int nvar = P->Dimension - exist - nparam;
2057 int toswap = nvar < exist ? nvar : exist;
2058 for (int i = 0; i < toswap; ++i)
2059 SwapColumns(P, 1 + i, nvar+exist - i);
2060 nparam += nvar;
2062 #ifdef DEBUG_ER
2063 fprintf(stderr, "\nER: Sum\n");
2064 #endif /* DEBUG_ER */
2066 evalue *EP = barvinok_enumerate_e(P, exist, nparam, MaxRays);
2068 for (int i = 0; i < /* nvar */ nparam; ++i) {
2069 Matrix *C = Matrix_Alloc(1, 1 + nparam + 1);
2070 value_set_si(C->p[0][0], 1);
2071 evalue split;
2072 value_init(split.d);
2073 value_set_si(split.d, 0);
2074 split.x.p = new_enode(partition, 4, nparam);
2075 value_set_si(C->p[0][1+i], 1);
2076 Matrix *C2 = Matrix_Copy(C);
2077 EVALUE_SET_DOMAIN(split.x.p->arr[0],
2078 Constraints2Polyhedron(C2, MaxRays));
2079 Matrix_Free(C2);
2080 evalue_set_si(&split.x.p->arr[1], 1, 1);
2081 value_set_si(C->p[0][1+i], -1);
2082 value_set_si(C->p[0][1+nparam], -1);
2083 EVALUE_SET_DOMAIN(split.x.p->arr[2],
2084 Constraints2Polyhedron(C, MaxRays));
2085 evalue_set_si(&split.x.p->arr[3], 1, 1);
2086 emul(&split, EP);
2087 free_evalue_refs(&split);
2088 Matrix_Free(C);
2090 reduce_evalue(EP);
2091 evalue_range_reduction(EP);
2093 evalue_frac2floor(EP);
2095 evalue *sum = esum(EP, nvar);
2097 free_evalue_refs(EP);
2098 free(EP);
2099 EP = sum;
2101 evalue_range_reduction(EP);
2103 return EP;
2106 static evalue* split_sure(Polyhedron *P, Polyhedron *S,
2107 unsigned exist, unsigned nparam, unsigned MaxRays)
2109 int nvar = P->Dimension - exist - nparam;
2111 Matrix *M = Matrix_Alloc(exist, S->Dimension+2);
2112 for (int i = 0; i < exist; ++i)
2113 value_set_si(M->p[i][nvar+i+1], 1);
2114 Polyhedron *O = S;
2115 S = DomainAddRays(S, M, MaxRays);
2116 Polyhedron_Free(O);
2117 Polyhedron *F = DomainAddRays(P, M, MaxRays);
2118 Polyhedron *D = DomainDifference(F, S, MaxRays);
2119 O = D;
2120 D = Disjoint_Domain(D, 0, MaxRays);
2121 Polyhedron_Free(F);
2122 Domain_Free(O);
2123 Matrix_Free(M);
2125 M = Matrix_Alloc(P->Dimension+1-exist, P->Dimension+1);
2126 for (int j = 0; j < nvar; ++j)
2127 value_set_si(M->p[j][j], 1);
2128 for (int j = 0; j < nparam+1; ++j)
2129 value_set_si(M->p[nvar+j][nvar+exist+j], 1);
2130 Polyhedron *T = Polyhedron_Image(S, M, MaxRays);
2131 evalue *EP = barvinok_enumerate_e(T, 0, nparam, MaxRays);
2132 Polyhedron_Free(S);
2133 Polyhedron_Free(T);
2134 Matrix_Free(M);
2136 for (Polyhedron *Q = D; Q; Q = Q->next) {
2137 Polyhedron *N = Q->next;
2138 Q->next = 0;
2139 T = DomainIntersection(P, Q, MaxRays);
2140 evalue *E = barvinok_enumerate_e(T, exist, nparam, MaxRays);
2141 eadd(E, EP);
2142 free_evalue_refs(E);
2143 free(E);
2144 Polyhedron_Free(T);
2145 Q->next = N;
2147 Domain_Free(D);
2148 return EP;
2151 static evalue* enumerate_sure(Polyhedron *P,
2152 unsigned exist, unsigned nparam, unsigned MaxRays)
2154 int i;
2155 Polyhedron *S = P;
2156 int nvar = P->Dimension - exist - nparam;
2157 Value lcm;
2158 Value f;
2159 value_init(lcm);
2160 value_init(f);
2162 for (i = 0; i < exist; ++i) {
2163 Matrix *M = Matrix_Alloc(S->NbConstraints, S->Dimension+2);
2164 int c = 0;
2165 value_set_si(lcm, 1);
2166 for (int j = 0; j < S->NbConstraints; ++j) {
2167 if (value_negz_p(S->Constraint[j][1+nvar+i]))
2168 continue;
2169 if (value_one_p(S->Constraint[j][1+nvar+i]))
2170 continue;
2171 value_lcm(lcm, S->Constraint[j][1+nvar+i], &lcm);
2174 for (int j = 0; j < S->NbConstraints; ++j) {
2175 if (value_negz_p(S->Constraint[j][1+nvar+i]))
2176 continue;
2177 if (value_one_p(S->Constraint[j][1+nvar+i]))
2178 continue;
2179 value_division(f, lcm, S->Constraint[j][1+nvar+i]);
2180 Vector_Scale(S->Constraint[j], M->p[c], f, S->Dimension+2);
2181 value_substract(M->p[c][S->Dimension+1],
2182 M->p[c][S->Dimension+1],
2183 lcm);
2184 value_increment(M->p[c][S->Dimension+1],
2185 M->p[c][S->Dimension+1]);
2186 ++c;
2188 Polyhedron *O = S;
2189 S = AddConstraints(M->p[0], c, S, MaxRays);
2190 if (O != P)
2191 Polyhedron_Free(O);
2192 Matrix_Free(M);
2193 if (emptyQ(S)) {
2194 Polyhedron_Free(S);
2195 value_clear(lcm);
2196 value_clear(f);
2197 return 0;
2200 value_clear(lcm);
2201 value_clear(f);
2203 #ifdef DEBUG_ER
2204 fprintf(stderr, "\nER: Sure\n");
2205 #endif /* DEBUG_ER */
2207 return split_sure(P, S, exist, nparam, MaxRays);
2210 static evalue* enumerate_sure2(Polyhedron *P,
2211 unsigned exist, unsigned nparam, unsigned MaxRays)
2213 int nvar = P->Dimension - exist - nparam;
2214 int r;
2215 for (r = 0; r < P->NbRays; ++r)
2216 if (value_one_p(P->Ray[r][0]) &&
2217 value_one_p(P->Ray[r][P->Dimension+1]))
2218 break;
2220 if (r >= P->NbRays)
2221 return 0;
2223 Matrix *M = Matrix_Alloc(nvar + 1 + nparam, P->Dimension+2);
2224 for (int i = 0; i < nvar; ++i)
2225 value_set_si(M->p[i][1+i], 1);
2226 for (int i = 0; i < nparam; ++i)
2227 value_set_si(M->p[i+nvar][1+nvar+exist+i], 1);
2228 Vector_Copy(P->Ray[r]+1+nvar, M->p[nvar+nparam]+1+nvar, exist);
2229 value_set_si(M->p[nvar+nparam][0], 1);
2230 value_set_si(M->p[nvar+nparam][P->Dimension+1], 1);
2231 Polyhedron * F = Rays2Polyhedron(M, MaxRays);
2232 Matrix_Free(M);
2234 Polyhedron *I = DomainIntersection(F, P, MaxRays);
2235 Polyhedron_Free(F);
2237 #ifdef DEBUG_ER
2238 fprintf(stderr, "\nER: Sure2\n");
2239 #endif /* DEBUG_ER */
2241 return split_sure(P, I, exist, nparam, MaxRays);
2244 static evalue* enumerate_cyclic(Polyhedron *P,
2245 unsigned exist, unsigned nparam,
2246 evalue * EP, int r, int p, unsigned MaxRays)
2248 int nvar = P->Dimension - exist - nparam;
2250 /* If EP in its fractional maps only contains references
2251 * to the remainder parameter with appropriate coefficients
2252 * then we could in principle avoid adding existentially
2253 * quantified variables to the validity domains.
2254 * We'd have to replace the remainder by m { p/m }
2255 * and multiply with an appropriate factor that is one
2256 * only in the appropriate range.
2257 * This last multiplication can be avoided if EP
2258 * has a single validity domain with no (further)
2259 * constraints on the remainder parameter
2262 Matrix *CT = Matrix_Alloc(nparam+1, nparam+3);
2263 Matrix *M = Matrix_Alloc(1, 1+nparam+3);
2264 for (int j = 0; j < nparam; ++j)
2265 if (j != p)
2266 value_set_si(CT->p[j][j], 1);
2267 value_set_si(CT->p[p][nparam+1], 1);
2268 value_set_si(CT->p[nparam][nparam+2], 1);
2269 value_set_si(M->p[0][1+p], -1);
2270 value_absolute(M->p[0][1+nparam], P->Ray[0][1+nvar+exist+p]);
2271 value_set_si(M->p[0][1+nparam+1], 1);
2272 Polyhedron *CEq = Constraints2Polyhedron(M, 1);
2273 Matrix_Free(M);
2274 addeliminatedparams_enum(EP, CT, CEq, MaxRays, nparam);
2275 Polyhedron_Free(CEq);
2276 Matrix_Free(CT);
2278 return EP;
2281 static void enumerate_vd_add_ray(evalue *EP, Matrix *Rays, unsigned MaxRays)
2283 if (value_notzero_p(EP->d))
2284 return;
2286 assert(EP->x.p->type == partition);
2287 assert(EP->x.p->pos == EVALUE_DOMAIN(EP->x.p->arr[0])->Dimension);
2288 for (int i = 0; i < EP->x.p->size/2; ++i) {
2289 Polyhedron *D = EVALUE_DOMAIN(EP->x.p->arr[2*i]);
2290 Polyhedron *N = DomainAddRays(D, Rays, MaxRays);
2291 EVALUE_SET_DOMAIN(EP->x.p->arr[2*i], N);
2292 Domain_Free(D);
2296 static evalue* enumerate_line(Polyhedron *P,
2297 unsigned exist, unsigned nparam, unsigned MaxRays)
2299 if (P->NbBid == 0)
2300 return 0;
2302 #ifdef DEBUG_ER
2303 fprintf(stderr, "\nER: Line\n");
2304 #endif /* DEBUG_ER */
2306 int nvar = P->Dimension - exist - nparam;
2307 int i, j;
2308 for (i = 0; i < nparam; ++i)
2309 if (value_notzero_p(P->Ray[0][1+nvar+exist+i]))
2310 break;
2311 assert(i < nparam);
2312 for (j = i+1; j < nparam; ++j)
2313 if (value_notzero_p(P->Ray[0][1+nvar+exist+i]))
2314 break;
2315 assert(j >= nparam); // for now
2317 Matrix *M = Matrix_Alloc(2, P->Dimension+2);
2318 value_set_si(M->p[0][0], 1);
2319 value_set_si(M->p[0][1+nvar+exist+i], 1);
2320 value_set_si(M->p[1][0], 1);
2321 value_set_si(M->p[1][1+nvar+exist+i], -1);
2322 value_absolute(M->p[1][1+P->Dimension], P->Ray[0][1+nvar+exist+i]);
2323 value_decrement(M->p[1][1+P->Dimension], M->p[1][1+P->Dimension]);
2324 Polyhedron *S = AddConstraints(M->p[0], 2, P, MaxRays);
2325 evalue *EP = barvinok_enumerate_e(S, exist, nparam, MaxRays);
2326 Polyhedron_Free(S);
2327 Matrix_Free(M);
2329 return enumerate_cyclic(P, exist, nparam, EP, 0, i, MaxRays);
2332 static int single_param_pos(Polyhedron*P, unsigned exist, unsigned nparam,
2333 int r)
2335 int nvar = P->Dimension - exist - nparam;
2336 if (First_Non_Zero(P->Ray[r]+1, nvar) != -1)
2337 return -1;
2338 int i = First_Non_Zero(P->Ray[r]+1+nvar+exist, nparam);
2339 if (i == -1)
2340 return -1;
2341 if (First_Non_Zero(P->Ray[r]+1+nvar+exist+1, nparam-i-1) != -1)
2342 return -1;
2343 return i;
2346 static evalue* enumerate_remove_ray(Polyhedron *P, int r,
2347 unsigned exist, unsigned nparam, unsigned MaxRays)
2349 #ifdef DEBUG_ER
2350 fprintf(stderr, "\nER: RedundantRay\n");
2351 #endif /* DEBUG_ER */
2353 Value one;
2354 value_init(one);
2355 value_set_si(one, 1);
2356 int len = P->NbRays-1;
2357 Matrix *M = Matrix_Alloc(2 * len, P->Dimension+2);
2358 Vector_Copy(P->Ray[0], M->p[0], r * (P->Dimension+2));
2359 Vector_Copy(P->Ray[r+1], M->p[r], (len-r) * (P->Dimension+2));
2360 for (int j = 0; j < P->NbRays; ++j) {
2361 if (j == r)
2362 continue;
2363 Vector_Combine(P->Ray[j], P->Ray[r], M->p[len+j-(j>r)],
2364 one, P->Ray[j][P->Dimension+1], P->Dimension+2);
2367 P = Rays2Polyhedron(M, MaxRays);
2368 Matrix_Free(M);
2369 evalue *EP = barvinok_enumerate_e(P, exist, nparam, MaxRays);
2370 Polyhedron_Free(P);
2371 value_clear(one);
2373 return EP;
2376 static evalue* enumerate_redundant_ray(Polyhedron *P,
2377 unsigned exist, unsigned nparam, unsigned MaxRays)
2379 assert(P->NbBid == 0);
2380 int nvar = P->Dimension - exist - nparam;
2381 Value m;
2382 value_init(m);
2384 for (int r = 0; r < P->NbRays; ++r) {
2385 if (value_notzero_p(P->Ray[r][P->Dimension+1]))
2386 continue;
2387 int i1 = single_param_pos(P, exist, nparam, r);
2388 if (i1 == -1)
2389 continue;
2390 for (int r2 = r+1; r2 < P->NbRays; ++r2) {
2391 if (value_notzero_p(P->Ray[r2][P->Dimension+1]))
2392 continue;
2393 int i2 = single_param_pos(P, exist, nparam, r2);
2394 if (i2 == -1)
2395 continue;
2396 if (i1 != i2)
2397 continue;
2399 value_division(m, P->Ray[r][1+nvar+exist+i1],
2400 P->Ray[r2][1+nvar+exist+i1]);
2401 value_multiply(m, m, P->Ray[r2][1+nvar+exist+i1]);
2402 /* r2 divides r => r redundant */
2403 if (value_eq(m, P->Ray[r][1+nvar+exist+i1])) {
2404 value_clear(m);
2405 return enumerate_remove_ray(P, r, exist, nparam, MaxRays);
2408 value_division(m, P->Ray[r2][1+nvar+exist+i1],
2409 P->Ray[r][1+nvar+exist+i1]);
2410 value_multiply(m, m, P->Ray[r][1+nvar+exist+i1]);
2411 /* r divides r2 => r2 redundant */
2412 if (value_eq(m, P->Ray[r2][1+nvar+exist+i1])) {
2413 value_clear(m);
2414 return enumerate_remove_ray(P, r2, exist, nparam, MaxRays);
2418 value_clear(m);
2419 return 0;
2422 static Polyhedron *upper_bound(Polyhedron *P,
2423 int pos, Value *max, Polyhedron **R)
2425 Value v;
2426 int r;
2427 value_init(v);
2429 *R = 0;
2430 Polyhedron *N;
2431 Polyhedron *B = 0;
2432 for (Polyhedron *Q = P; Q; Q = N) {
2433 N = Q->next;
2434 for (r = 0; r < P->NbRays; ++r) {
2435 if (value_zero_p(P->Ray[r][P->Dimension+1]) &&
2436 value_pos_p(P->Ray[r][1+pos]))
2437 break;
2439 if (r < P->NbRays) {
2440 Q->next = *R;
2441 *R = Q;
2442 continue;
2443 } else {
2444 Q->next = B;
2445 B = Q;
2447 for (r = 0; r < P->NbRays; ++r) {
2448 if (value_zero_p(P->Ray[r][P->Dimension+1]))
2449 continue;
2450 mpz_fdiv_q(v, P->Ray[r][1+pos], P->Ray[r][1+P->Dimension]);
2451 if ((!Q->next && r == 0) || value_gt(v, *max))
2452 value_assign(*max, v);
2455 value_clear(v);
2456 return B;
2459 static evalue* enumerate_ray(Polyhedron *P,
2460 unsigned exist, unsigned nparam, unsigned MaxRays)
2462 assert(P->NbBid == 0);
2463 int nvar = P->Dimension - exist - nparam;
2465 int r;
2466 for (r = 0; r < P->NbRays; ++r)
2467 if (value_zero_p(P->Ray[r][P->Dimension+1]))
2468 break;
2469 if (r >= P->NbRays)
2470 return 0;
2472 int r2;
2473 for (r2 = r+1; r2 < P->NbRays; ++r2)
2474 if (value_zero_p(P->Ray[r2][P->Dimension+1]))
2475 break;
2476 if (r2 < P->NbRays) {
2477 if (nvar > 0)
2478 return enumerate_sum(P, exist, nparam, MaxRays);
2481 #ifdef DEBUG_ER
2482 fprintf(stderr, "\nER: Ray\n");
2483 #endif /* DEBUG_ER */
2485 Value m;
2486 Value one;
2487 value_init(m);
2488 value_init(one);
2489 value_set_si(one, 1);
2490 int i = single_param_pos(P, exist, nparam, r);
2491 assert(i != -1); // for now;
2493 Matrix *M = Matrix_Alloc(P->NbRays, P->Dimension+2);
2494 for (int j = 0; j < P->NbRays; ++j) {
2495 Vector_Combine(P->Ray[j], P->Ray[r], M->p[j],
2496 one, P->Ray[j][P->Dimension+1], P->Dimension+2);
2498 Polyhedron *S = Rays2Polyhedron(M, MaxRays);
2499 Matrix_Free(M);
2500 Polyhedron *D = DomainDifference(P, S, MaxRays);
2501 Polyhedron_Free(S);
2502 // Polyhedron_Print(stderr, P_VALUE_FMT, D);
2503 assert(value_pos_p(P->Ray[r][1+nvar+exist+i])); // for now
2504 Polyhedron *R;
2505 D = upper_bound(D, nvar+exist+i, &m, &R);
2506 assert(D);
2507 Domain_Free(D);
2509 M = Matrix_Alloc(2, P->Dimension+2);
2510 value_set_si(M->p[0][0], 1);
2511 value_set_si(M->p[1][0], 1);
2512 value_set_si(M->p[0][1+nvar+exist+i], -1);
2513 value_set_si(M->p[1][1+nvar+exist+i], 1);
2514 value_assign(M->p[0][1+P->Dimension], m);
2515 value_oppose(M->p[1][1+P->Dimension], m);
2516 value_addto(M->p[1][1+P->Dimension], M->p[1][1+P->Dimension],
2517 P->Ray[r][1+nvar+exist+i]);
2518 value_decrement(M->p[1][1+P->Dimension], M->p[1][1+P->Dimension]);
2519 // Matrix_Print(stderr, P_VALUE_FMT, M);
2520 D = AddConstraints(M->p[0], 2, P, MaxRays);
2521 // Polyhedron_Print(stderr, P_VALUE_FMT, D);
2522 value_substract(M->p[0][1+P->Dimension], M->p[0][1+P->Dimension],
2523 P->Ray[r][1+nvar+exist+i]);
2524 // Matrix_Print(stderr, P_VALUE_FMT, M);
2525 S = AddConstraints(M->p[0], 1, P, MaxRays);
2526 // Polyhedron_Print(stderr, P_VALUE_FMT, S);
2527 Matrix_Free(M);
2529 evalue *EP = barvinok_enumerate_e(D, exist, nparam, MaxRays);
2530 Polyhedron_Free(D);
2531 value_clear(one);
2532 value_clear(m);
2534 if (value_notone_p(P->Ray[r][1+nvar+exist+i]))
2535 EP = enumerate_cyclic(P, exist, nparam, EP, r, i, MaxRays);
2536 else {
2537 M = Matrix_Alloc(1, nparam+2);
2538 value_set_si(M->p[0][0], 1);
2539 value_set_si(M->p[0][1+i], 1);
2540 enumerate_vd_add_ray(EP, M, MaxRays);
2541 Matrix_Free(M);
2544 if (!emptyQ(S)) {
2545 evalue *E = barvinok_enumerate_e(S, exist, nparam, MaxRays);
2546 eadd(E, EP);
2547 free_evalue_refs(E);
2548 free(E);
2550 Polyhedron_Free(S);
2552 if (R) {
2553 assert(nvar == 0);
2554 evalue *ER = enumerate_or(R, exist, nparam, MaxRays);
2555 eor(ER, EP);
2556 free_evalue_refs(ER);
2557 free(ER);
2560 return EP;
2563 static evalue* new_zero_ep()
2565 evalue *EP;
2566 ALLOC(evalue, EP);
2567 value_init(EP->d);
2568 evalue_set_si(EP, 0, 1);
2569 return EP;
2572 static evalue* enumerate_vd(Polyhedron **PA,
2573 unsigned exist, unsigned nparam, unsigned MaxRays)
2575 Polyhedron *P = *PA;
2576 int nvar = P->Dimension - exist - nparam;
2577 Param_Polyhedron *PP = NULL;
2578 Polyhedron *C = Universe_Polyhedron(nparam);
2579 Polyhedron *CEq;
2580 Matrix *CT;
2581 Polyhedron *PR = P;
2582 PP = Polyhedron2Param_SimplifiedDomain(&PR,C,MaxRays,&CEq,&CT);
2583 Polyhedron_Free(C);
2585 int nd;
2586 Param_Domain *D, *last;
2587 Value c;
2588 value_init(c);
2589 for (nd = 0, D=PP->D; D; D=D->next, ++nd)
2592 Polyhedron **VD = new Polyhedron_p[nd];
2593 Polyhedron **fVD = new Polyhedron_p[nd];
2594 for(nd = 0, D=PP->D; D; D=D->next) {
2595 Polyhedron *rVD = reduce_domain(D->Domain, CT, CEq,
2596 fVD, nd, MaxRays);
2597 if (!rVD)
2598 continue;
2600 VD[nd++] = rVD;
2601 last = D;
2604 evalue *EP = 0;
2606 if (nd == 0)
2607 EP = new_zero_ep();
2609 /* This doesn't seem to have any effect */
2610 if (nd == 1) {
2611 Polyhedron *CA = align_context(VD[0], P->Dimension, MaxRays);
2612 Polyhedron *O = P;
2613 P = DomainIntersection(P, CA, MaxRays);
2614 if (O != *PA)
2615 Polyhedron_Free(O);
2616 Polyhedron_Free(CA);
2617 if (emptyQ(P))
2618 EP = new_zero_ep();
2621 if (!EP && CT->NbColumns != CT->NbRows) {
2622 Polyhedron *CEqr = DomainImage(CEq, CT, MaxRays);
2623 Polyhedron *CA = align_context(CEqr, PR->Dimension, MaxRays);
2624 Polyhedron *I = DomainIntersection(PR, CA, MaxRays);
2625 Polyhedron_Free(CEqr);
2626 Polyhedron_Free(CA);
2627 #ifdef DEBUG_ER
2628 fprintf(stderr, "\nER: Eliminate\n");
2629 #endif /* DEBUG_ER */
2630 nparam -= CT->NbColumns - CT->NbRows;
2631 EP = barvinok_enumerate_e(I, exist, nparam, MaxRays);
2632 nparam += CT->NbColumns - CT->NbRows;
2633 addeliminatedparams_enum(EP, CT, CEq, MaxRays, nparam);
2634 Polyhedron_Free(I);
2636 if (PR != *PA)
2637 Polyhedron_Free(PR);
2638 PR = 0;
2640 if (!EP && nd > 1) {
2641 #ifdef DEBUG_ER
2642 fprintf(stderr, "\nER: VD\n");
2643 #endif /* DEBUG_ER */
2644 for (int i = 0; i < nd; ++i) {
2645 Polyhedron *CA = align_context(VD[i], P->Dimension, MaxRays);
2646 Polyhedron *I = DomainIntersection(P, CA, MaxRays);
2648 if (i == 0)
2649 EP = barvinok_enumerate_e(I, exist, nparam, MaxRays);
2650 else {
2651 evalue *E = barvinok_enumerate_e(I, exist, nparam, MaxRays);
2652 eadd(E, EP);
2653 free_evalue_refs(E);
2654 free(E);
2656 Polyhedron_Free(I);
2657 Polyhedron_Free(CA);
2661 for (int i = 0; i < nd; ++i) {
2662 Polyhedron_Free(VD[i]);
2663 Polyhedron_Free(fVD[i]);
2665 delete [] VD;
2666 delete [] fVD;
2667 value_clear(c);
2669 if (!EP && nvar == 0) {
2670 Value f;
2671 value_init(f);
2672 Param_Vertices *V, *V2;
2673 Matrix* M = Matrix_Alloc(1, P->Dimension+2);
2675 FORALL_PVertex_in_ParamPolyhedron(V, last, PP) {
2676 bool found = false;
2677 FORALL_PVertex_in_ParamPolyhedron(V2, last, PP) {
2678 if (V == V2) {
2679 found = true;
2680 continue;
2682 if (!found)
2683 continue;
2684 for (int i = 0; i < exist; ++i) {
2685 value_oppose(f, V->Vertex->p[i][nparam+1]);
2686 Vector_Combine(V->Vertex->p[i],
2687 V2->Vertex->p[i],
2688 M->p[0] + 1 + nvar + exist,
2689 V2->Vertex->p[i][nparam+1],
2691 nparam+1);
2692 int j;
2693 for (j = 0; j < nparam; ++j)
2694 if (value_notzero_p(M->p[0][1+nvar+exist+j]))
2695 break;
2696 if (j >= nparam)
2697 continue;
2698 ConstraintSimplify(M->p[0], M->p[0],
2699 P->Dimension+2, &f);
2700 value_set_si(M->p[0][0], 0);
2701 Polyhedron *para = AddConstraints(M->p[0], 1, P,
2702 MaxRays);
2703 if (emptyQ(para)) {
2704 Polyhedron_Free(para);
2705 continue;
2707 Polyhedron *pos, *neg;
2708 value_set_si(M->p[0][0], 1);
2709 value_decrement(M->p[0][P->Dimension+1],
2710 M->p[0][P->Dimension+1]);
2711 neg = AddConstraints(M->p[0], 1, P, MaxRays);
2712 value_set_si(f, -1);
2713 Vector_Scale(M->p[0]+1, M->p[0]+1, f,
2714 P->Dimension+1);
2715 value_decrement(M->p[0][P->Dimension+1],
2716 M->p[0][P->Dimension+1]);
2717 value_decrement(M->p[0][P->Dimension+1],
2718 M->p[0][P->Dimension+1]);
2719 pos = AddConstraints(M->p[0], 1, P, MaxRays);
2720 if (emptyQ(neg) && emptyQ(pos)) {
2721 Polyhedron_Free(para);
2722 Polyhedron_Free(pos);
2723 Polyhedron_Free(neg);
2724 continue;
2726 #ifdef DEBUG_ER
2727 fprintf(stderr, "\nER: Order\n");
2728 #endif /* DEBUG_ER */
2729 EP = barvinok_enumerate_e(para, exist, nparam, MaxRays);
2730 evalue *E;
2731 if (!emptyQ(pos)) {
2732 E = barvinok_enumerate_e(pos, exist, nparam, MaxRays);
2733 eadd(E, EP);
2734 free_evalue_refs(E);
2735 free(E);
2737 if (!emptyQ(neg)) {
2738 E = barvinok_enumerate_e(neg, exist, nparam, MaxRays);
2739 eadd(E, EP);
2740 free_evalue_refs(E);
2741 free(E);
2743 Polyhedron_Free(para);
2744 Polyhedron_Free(pos);
2745 Polyhedron_Free(neg);
2746 break;
2748 if (EP)
2749 break;
2750 } END_FORALL_PVertex_in_ParamPolyhedron;
2751 if (EP)
2752 break;
2753 } END_FORALL_PVertex_in_ParamPolyhedron;
2755 if (!EP) {
2756 /* Search for vertex coordinate to split on */
2757 /* First look for one independent of the parameters */
2758 FORALL_PVertex_in_ParamPolyhedron(V, last, PP) {
2759 for (int i = 0; i < exist; ++i) {
2760 int j;
2761 for (j = 0; j < nparam; ++j)
2762 if (value_notzero_p(V->Vertex->p[i][j]))
2763 break;
2764 if (j < nparam)
2765 continue;
2766 value_set_si(M->p[0][0], 1);
2767 Vector_Set(M->p[0]+1, 0, nvar+exist);
2768 Vector_Copy(V->Vertex->p[i],
2769 M->p[0] + 1 + nvar + exist, nparam+1);
2770 value_oppose(M->p[0][1+nvar+i],
2771 V->Vertex->p[i][nparam+1]);
2773 Polyhedron *pos, *neg;
2774 value_set_si(M->p[0][0], 1);
2775 value_decrement(M->p[0][P->Dimension+1],
2776 M->p[0][P->Dimension+1]);
2777 neg = AddConstraints(M->p[0], 1, P, MaxRays);
2778 value_set_si(f, -1);
2779 Vector_Scale(M->p[0]+1, M->p[0]+1, f,
2780 P->Dimension+1);
2781 value_decrement(M->p[0][P->Dimension+1],
2782 M->p[0][P->Dimension+1]);
2783 value_decrement(M->p[0][P->Dimension+1],
2784 M->p[0][P->Dimension+1]);
2785 pos = AddConstraints(M->p[0], 1, P, MaxRays);
2786 if (emptyQ(neg) || emptyQ(pos)) {
2787 Polyhedron_Free(pos);
2788 Polyhedron_Free(neg);
2789 continue;
2791 Polyhedron_Free(pos);
2792 value_increment(M->p[0][P->Dimension+1],
2793 M->p[0][P->Dimension+1]);
2794 pos = AddConstraints(M->p[0], 1, P, MaxRays);
2795 #ifdef DEBUG_ER
2796 fprintf(stderr, "\nER: Vertex\n");
2797 #endif /* DEBUG_ER */
2798 pos->next = neg;
2799 EP = enumerate_or(pos, exist, nparam, MaxRays);
2800 break;
2802 if (EP)
2803 break;
2804 } END_FORALL_PVertex_in_ParamPolyhedron;
2807 if (!EP) {
2808 /* Search for vertex coordinate to split on */
2809 /* Now look for one that depends on the parameters */
2810 FORALL_PVertex_in_ParamPolyhedron(V, last, PP) {
2811 for (int i = 0; i < exist; ++i) {
2812 value_set_si(M->p[0][0], 1);
2813 Vector_Set(M->p[0]+1, 0, nvar+exist);
2814 Vector_Copy(V->Vertex->p[i],
2815 M->p[0] + 1 + nvar + exist, nparam+1);
2816 value_oppose(M->p[0][1+nvar+i],
2817 V->Vertex->p[i][nparam+1]);
2819 Polyhedron *pos, *neg;
2820 value_set_si(M->p[0][0], 1);
2821 value_decrement(M->p[0][P->Dimension+1],
2822 M->p[0][P->Dimension+1]);
2823 neg = AddConstraints(M->p[0], 1, P, MaxRays);
2824 value_set_si(f, -1);
2825 Vector_Scale(M->p[0]+1, M->p[0]+1, f,
2826 P->Dimension+1);
2827 value_decrement(M->p[0][P->Dimension+1],
2828 M->p[0][P->Dimension+1]);
2829 value_decrement(M->p[0][P->Dimension+1],
2830 M->p[0][P->Dimension+1]);
2831 pos = AddConstraints(M->p[0], 1, P, MaxRays);
2832 if (emptyQ(neg) || emptyQ(pos)) {
2833 Polyhedron_Free(pos);
2834 Polyhedron_Free(neg);
2835 continue;
2837 Polyhedron_Free(pos);
2838 value_increment(M->p[0][P->Dimension+1],
2839 M->p[0][P->Dimension+1]);
2840 pos = AddConstraints(M->p[0], 1, P, MaxRays);
2841 #ifdef DEBUG_ER
2842 fprintf(stderr, "\nER: ParamVertex\n");
2843 #endif /* DEBUG_ER */
2844 pos->next = neg;
2845 EP = enumerate_or(pos, exist, nparam, MaxRays);
2846 break;
2848 if (EP)
2849 break;
2850 } END_FORALL_PVertex_in_ParamPolyhedron;
2853 Matrix_Free(M);
2854 value_clear(f);
2857 if (CEq)
2858 Polyhedron_Free(CEq);
2859 if (CT)
2860 Matrix_Free(CT);
2861 if (PP)
2862 Param_Polyhedron_Free(PP);
2863 *PA = P;
2865 return EP;
2868 #ifndef HAVE_PIPLIB
2869 evalue *barvinok_enumerate_pip(Polyhedron *P,
2870 unsigned exist, unsigned nparam, unsigned MaxRays)
2872 return 0;
2874 #else
2875 evalue *barvinok_enumerate_pip(Polyhedron *P,
2876 unsigned exist, unsigned nparam, unsigned MaxRays)
2878 int nvar = P->Dimension - exist - nparam;
2879 evalue *EP = new_zero_ep();
2880 Polyhedron *Q, *N, *T = 0;
2881 Value min, tmp;
2882 value_init(min);
2883 value_init(tmp);
2885 #ifdef DEBUG_ER
2886 fprintf(stderr, "\nER: PIP\n");
2887 #endif /* DEBUG_ER */
2889 for (int i = 0; i < P->Dimension; ++i) {
2890 bool pos = false;
2891 bool neg = false;
2892 bool posray = false;
2893 bool negray = false;
2894 value_set_si(min, 0);
2895 for (int j = 0; j < P->NbRays; ++j) {
2896 if (value_pos_p(P->Ray[j][1+i])) {
2897 pos = true;
2898 if (value_zero_p(P->Ray[j][1+P->Dimension]))
2899 posray = true;
2900 } else if (value_neg_p(P->Ray[j][1+i])) {
2901 neg = true;
2902 if (value_zero_p(P->Ray[j][1+P->Dimension]))
2903 negray = true;
2904 else {
2905 mpz_fdiv_q(tmp,
2906 P->Ray[j][1+i], P->Ray[j][1+P->Dimension]);
2907 if (value_lt(tmp, min))
2908 value_assign(min, tmp);
2912 if (pos && neg) {
2913 assert(!(posray && negray));
2914 assert(!negray); // for now
2915 Polyhedron *O = T ? T : P;
2916 /* shift by a safe amount */
2917 Matrix *M = Matrix_Alloc(O->NbRays, O->Dimension+2);
2918 Vector_Copy(O->Ray[0], M->p[0], O->NbRays * (O->Dimension+2));
2919 for (int j = 0; j < P->NbRays; ++j) {
2920 if (value_notzero_p(M->p[j][1+P->Dimension])) {
2921 value_multiply(tmp, min, M->p[j][1+P->Dimension]);
2922 value_substract(M->p[j][1+i], M->p[j][1+i], tmp);
2925 if (T)
2926 Polyhedron_Free(T);
2927 T = Rays2Polyhedron(M, MaxRays);
2928 Matrix_Free(M);
2929 } else if (neg) {
2930 /* negating a parameter requires that we substitute in the
2931 * sign again afterwards.
2932 * Disallow for now.
2934 assert(i < nvar+exist);
2935 if (!T)
2936 T = Polyhedron_Copy(P);
2937 for (int j = 0; j < T->NbRays; ++j)
2938 value_oppose(T->Ray[j][1+i], T->Ray[j][1+i]);
2939 for (int j = 0; j < T->NbConstraints; ++j)
2940 value_oppose(T->Constraint[j][1+i], T->Constraint[j][1+i]);
2943 value_clear(min);
2944 value_clear(tmp);
2946 Polyhedron *D = pip_lexmin(T ? T : P, exist, nparam);
2947 for (Q = D; Q; Q = N) {
2948 N = Q->next;
2949 Q->next = 0;
2950 evalue *E;
2951 exist = Q->Dimension - nvar - nparam;
2952 E = barvinok_enumerate_e(Q, exist, nparam, MaxRays);
2953 Polyhedron_Free(Q);
2954 eadd(E, EP);
2955 free_evalue_refs(E);
2956 free(E);
2959 if (T)
2960 Polyhedron_Free(T);
2962 return EP;
2964 #endif
2967 static bool is_single(Value *row, int pos, int len)
2969 return First_Non_Zero(row, pos) == -1 &&
2970 First_Non_Zero(row+pos+1, len-pos-1) == -1;
2973 static evalue* barvinok_enumerate_e_r(Polyhedron *P,
2974 unsigned exist, unsigned nparam, unsigned MaxRays);
2976 #ifdef DEBUG_ER
2977 static int er_level = 0;
2979 evalue* barvinok_enumerate_e(Polyhedron *P,
2980 unsigned exist, unsigned nparam, unsigned MaxRays)
2982 fprintf(stderr, "\nER: level %i\n", er_level);
2983 int nvar = P->Dimension - exist - nparam;
2984 fprintf(stderr, "%d %d %d\n", nvar, exist, nparam);
2986 Polyhedron_Print(stderr, P_VALUE_FMT, P);
2987 ++er_level;
2988 P = DomainConstraintSimplify(Polyhedron_Copy(P), MaxRays);
2989 evalue *EP = barvinok_enumerate_e_r(P, exist, nparam, MaxRays);
2990 Polyhedron_Free(P);
2991 --er_level;
2992 return EP;
2994 #else
2995 evalue* barvinok_enumerate_e(Polyhedron *P,
2996 unsigned exist, unsigned nparam, unsigned MaxRays)
2998 P = DomainConstraintSimplify(Polyhedron_Copy(P), MaxRays);
2999 evalue *EP = barvinok_enumerate_e_r(P, exist, nparam, MaxRays);
3000 Polyhedron_Free(P);
3001 return EP;
3003 #endif
3005 static evalue* barvinok_enumerate_e_r(Polyhedron *P,
3006 unsigned exist, unsigned nparam, unsigned MaxRays)
3008 if (exist == 0) {
3009 Polyhedron *U = Universe_Polyhedron(nparam);
3010 evalue *EP = barvinok_enumerate_ev(P, U, MaxRays);
3011 //char *param_name[] = {"P", "Q", "R", "S", "T" };
3012 //print_evalue(stdout, EP, param_name);
3013 Polyhedron_Free(U);
3014 return EP;
3017 int nvar = P->Dimension - exist - nparam;
3018 int len = P->Dimension + 2;
3020 if (emptyQ(P))
3021 return new_zero_ep();
3023 if (nvar == 0 && nparam == 0) {
3024 evalue *EP = new_zero_ep();
3025 barvinok_count(P, &EP->x.n, MaxRays);
3026 if (value_pos_p(EP->x.n))
3027 value_set_si(EP->x.n, 1);
3028 return EP;
3031 int r;
3032 for (r = 0; r < P->NbRays; ++r)
3033 if (value_zero_p(P->Ray[r][0]) ||
3034 value_zero_p(P->Ray[r][P->Dimension+1])) {
3035 int i;
3036 for (i = 0; i < nvar; ++i)
3037 if (value_notzero_p(P->Ray[r][i+1]))
3038 break;
3039 if (i >= nvar)
3040 continue;
3041 for (i = nvar + exist; i < nvar + exist + nparam; ++i)
3042 if (value_notzero_p(P->Ray[r][i+1]))
3043 break;
3044 if (i >= nvar + exist + nparam)
3045 break;
3047 if (r < P->NbRays) {
3048 evalue *EP = new_zero_ep();
3049 value_set_si(EP->x.n, -1);
3050 return EP;
3053 int first;
3054 for (r = 0; r < P->NbEq; ++r)
3055 if ((first = First_Non_Zero(P->Constraint[r]+1+nvar, exist)) != -1)
3056 break;
3057 if (r < P->NbEq) {
3058 if (First_Non_Zero(P->Constraint[r]+1+nvar+first+1,
3059 exist-first-1) != -1) {
3060 Polyhedron *T = rotate_along(P, r, nvar, exist, MaxRays);
3061 #ifdef DEBUG_ER
3062 fprintf(stderr, "\nER: Equality\n");
3063 #endif /* DEBUG_ER */
3064 evalue *EP = barvinok_enumerate_e(T, exist-1, nparam, MaxRays);
3065 Polyhedron_Free(T);
3066 return EP;
3067 } else {
3068 #ifdef DEBUG_ER
3069 fprintf(stderr, "\nER: Fixed\n");
3070 #endif /* DEBUG_ER */
3071 if (first == 0)
3072 return barvinok_enumerate_e(P, exist-1, nparam, MaxRays);
3073 else {
3074 Polyhedron *T = Polyhedron_Copy(P);
3075 SwapColumns(T, nvar+1, nvar+1+first);
3076 evalue *EP = barvinok_enumerate_e(T, exist-1, nparam, MaxRays);
3077 Polyhedron_Free(T);
3078 return EP;
3083 Vector *row = Vector_Alloc(len);
3084 value_set_si(row->p[0], 1);
3086 Value f;
3087 value_init(f);
3089 enum constraint* info = new constraint[exist];
3090 for (int i = 0; i < exist; ++i) {
3091 info[i] = ALL_POS;
3092 for (int l = P->NbEq; l < P->NbConstraints; ++l) {
3093 if (value_negz_p(P->Constraint[l][nvar+i+1]))
3094 continue;
3095 bool l_parallel = is_single(P->Constraint[l]+nvar+1, i, exist);
3096 for (int u = P->NbEq; u < P->NbConstraints; ++u) {
3097 if (value_posz_p(P->Constraint[u][nvar+i+1]))
3098 continue;
3099 bool lu_parallel = l_parallel ||
3100 is_single(P->Constraint[u]+nvar+1, i, exist);
3101 value_oppose(f, P->Constraint[u][nvar+i+1]);
3102 Vector_Combine(P->Constraint[l]+1, P->Constraint[u]+1, row->p+1,
3103 f, P->Constraint[l][nvar+i+1], len-1);
3104 if (!(info[i] & INDEPENDENT)) {
3105 int j;
3106 for (j = 0; j < exist; ++j)
3107 if (j != i && value_notzero_p(row->p[nvar+j+1]))
3108 break;
3109 if (j == exist) {
3110 //printf("independent: i: %d, l: %d, u: %d\n", i, l, u);
3111 info[i] = (constraint)(info[i] | INDEPENDENT);
3114 if (info[i] & ALL_POS) {
3115 value_addto(row->p[len-1], row->p[len-1],
3116 P->Constraint[l][nvar+i+1]);
3117 value_addto(row->p[len-1], row->p[len-1], f);
3118 value_multiply(f, f, P->Constraint[l][nvar+i+1]);
3119 value_substract(row->p[len-1], row->p[len-1], f);
3120 value_decrement(row->p[len-1], row->p[len-1]);
3121 ConstraintSimplify(row->p, row->p, len, &f);
3122 value_set_si(f, -1);
3123 Vector_Scale(row->p+1, row->p+1, f, len-1);
3124 value_decrement(row->p[len-1], row->p[len-1]);
3125 Polyhedron *T = AddConstraints(row->p, 1, P, MaxRays);
3126 if (!emptyQ(T)) {
3127 //printf("not all_pos: i: %d, l: %d, u: %d\n", i, l, u);
3128 info[i] = (constraint)(info[i] ^ ALL_POS);
3130 //puts("pos remainder");
3131 //Polyhedron_Print(stdout, P_VALUE_FMT, T);
3132 Polyhedron_Free(T);
3134 if (!(info[i] & ONE_NEG)) {
3135 if (lu_parallel) {
3136 negative_test_constraint(P->Constraint[l],
3137 P->Constraint[u],
3138 row->p, nvar+i, len, &f);
3139 oppose_constraint(row->p, len, &f);
3140 Polyhedron *T = AddConstraints(row->p, 1, P, MaxRays);
3141 if (emptyQ(T)) {
3142 //printf("one_neg i: %d, l: %d, u: %d\n", i, l, u);
3143 info[i] = (constraint)(info[i] | ONE_NEG);
3145 //puts("neg remainder");
3146 //Polyhedron_Print(stdout, P_VALUE_FMT, T);
3147 Polyhedron_Free(T);
3150 if (!(info[i] & ALL_POS) && (info[i] & ONE_NEG))
3151 goto next;
3154 if (info[i] & ALL_POS)
3155 break;
3156 next:
3161 for (int i = 0; i < exist; ++i)
3162 printf("%i: %i\n", i, info[i]);
3164 for (int i = 0; i < exist; ++i)
3165 if (info[i] & ALL_POS) {
3166 #ifdef DEBUG_ER
3167 fprintf(stderr, "\nER: Positive\n");
3168 #endif /* DEBUG_ER */
3169 // Eliminate
3170 // Maybe we should chew off some of the fat here
3171 Matrix *M = Matrix_Alloc(P->Dimension, P->Dimension+1);
3172 for (int j = 0; j < P->Dimension; ++j)
3173 value_set_si(M->p[j][j + (j >= i+nvar)], 1);
3174 Polyhedron *T = Polyhedron_Image(P, M, MaxRays);
3175 Matrix_Free(M);
3176 evalue *EP = barvinok_enumerate_e(T, exist-1, nparam, MaxRays);
3177 Polyhedron_Free(T);
3178 value_clear(f);
3179 Vector_Free(row);
3180 delete [] info;
3181 return EP;
3183 for (int i = 0; i < exist; ++i)
3184 if (info[i] & ONE_NEG) {
3185 #ifdef DEBUG_ER
3186 fprintf(stderr, "\nER: Negative\n");
3187 #endif /* DEBUG_ER */
3188 Vector_Free(row);
3189 value_clear(f);
3190 delete [] info;
3191 if (i == 0)
3192 return barvinok_enumerate_e(P, exist-1, nparam, MaxRays);
3193 else {
3194 Polyhedron *T = Polyhedron_Copy(P);
3195 SwapColumns(T, nvar+1, nvar+1+i);
3196 evalue *EP = barvinok_enumerate_e(T, exist-1, nparam, MaxRays);
3197 Polyhedron_Free(T);
3198 return EP;
3201 for (int i = 0; i < exist; ++i)
3202 if (info[i] & INDEPENDENT) {
3203 Polyhedron *pos, *neg;
3205 /* Find constraint again and split off negative part */
3207 if (SplitOnVar(P, i, nvar, len, exist, MaxRays,
3208 row, f, true, &pos, &neg)) {
3209 #ifdef DEBUG_ER
3210 fprintf(stderr, "\nER: Split\n");
3211 #endif /* DEBUG_ER */
3213 evalue *EP =
3214 barvinok_enumerate_e(neg, exist-1, nparam, MaxRays);
3215 evalue *E =
3216 barvinok_enumerate_e(pos, exist, nparam, MaxRays);
3217 eadd(E, EP);
3218 free_evalue_refs(E);
3219 free(E);
3220 Polyhedron_Free(neg);
3221 Polyhedron_Free(pos);
3222 value_clear(f);
3223 Vector_Free(row);
3224 delete [] info;
3225 return EP;
3228 delete [] info;
3230 Polyhedron *O = P;
3231 Polyhedron *F;
3233 evalue *EP;
3235 EP = enumerate_line(P, exist, nparam, MaxRays);
3236 if (EP)
3237 goto out;
3239 EP = barvinok_enumerate_pip(P, exist, nparam, MaxRays);
3240 if (EP)
3241 goto out;
3243 EP = enumerate_redundant_ray(P, exist, nparam, MaxRays);
3244 if (EP)
3245 goto out;
3247 EP = enumerate_sure(P, exist, nparam, MaxRays);
3248 if (EP)
3249 goto out;
3251 EP = enumerate_ray(P, exist, nparam, MaxRays);
3252 if (EP)
3253 goto out;
3255 EP = enumerate_sure2(P, exist, nparam, MaxRays);
3256 if (EP)
3257 goto out;
3259 F = unfringe(P, MaxRays);
3260 if (!PolyhedronIncludes(F, P)) {
3261 #ifdef DEBUG_ER
3262 fprintf(stderr, "\nER: Fringed\n");
3263 #endif /* DEBUG_ER */
3264 EP = barvinok_enumerate_e(F, exist, nparam, MaxRays);
3265 Polyhedron_Free(F);
3266 goto out;
3268 Polyhedron_Free(F);
3270 if (nparam)
3271 EP = enumerate_vd(&P, exist, nparam, MaxRays);
3272 if (EP)
3273 goto out2;
3275 if (nvar != 0) {
3276 EP = enumerate_sum(P, exist, nparam, MaxRays);
3277 goto out2;
3280 assert(nvar == 0);
3282 int i;
3283 Polyhedron *pos, *neg;
3284 for (i = 0; i < exist; ++i)
3285 if (SplitOnVar(P, i, nvar, len, exist, MaxRays,
3286 row, f, false, &pos, &neg))
3287 break;
3289 assert (i < exist);
3291 pos->next = neg;
3292 EP = enumerate_or(pos, exist, nparam, MaxRays);
3294 out2:
3295 if (O != P)
3296 Polyhedron_Free(P);
3298 out:
3299 value_clear(f);
3300 Vector_Free(row);
3301 return EP;
3304 static void normalize(Polyhedron *i, vec_ZZ& lambda, ZZ& sign,
3305 ZZ& num_s, vec_ZZ& num_p, vec_ZZ& den_s, vec_ZZ& den_p,
3306 mat_ZZ& f)
3308 unsigned dim = i->Dimension;
3309 unsigned nparam = num_p.length();
3310 unsigned nvar = dim - nparam;
3312 int r = 0;
3313 mat_ZZ rays;
3314 rays.SetDims(dim, nvar);
3315 add_rays(rays, i, &r, nvar, true);
3316 den_s = rays * lambda;
3317 int change = 0;
3320 for (int j = 0; j < den_s.length(); ++j) {
3321 values2zz(i->Ray[j]+1+nvar, f[j], nparam);
3322 if (den_s[j] == 0) {
3323 den_p[j] = 1;
3324 continue;
3326 if (First_Non_Zero(i->Ray[j]+1+nvar, nparam) != -1) {
3327 if (den_s[j] > 0) {
3328 den_p[j] = -1;
3329 num_p -= f[j];
3330 } else
3331 den_p[j] = 1;
3332 } else
3333 den_p[j] = 0;
3334 if (den_s[j] > 0)
3335 change ^= 1;
3336 else {
3337 den_s[j] = abs(den_s[j]);
3338 num_s += den_s[j];
3342 if (change)
3343 sign = -sign;
3346 gen_fun * barvinok_series(Polyhedron *P, Polyhedron* C, unsigned MaxRays)
3348 Polyhedron ** vcone;
3349 Polyhedron *CA;
3350 unsigned nparam = C->Dimension;
3351 unsigned dim, nvar;
3352 vec_ZZ sign;
3353 int ncone = 0;
3354 sign.SetLength(ncone);
3356 CA = align_context(C, P->Dimension, MaxRays);
3357 P = DomainIntersection(P, CA, MaxRays);
3358 Polyhedron_Free(CA);
3360 assert(!Polyhedron_is_infinite(P, nparam));
3361 assert(P->NbBid == 0);
3362 assert(Polyhedron_has_positive_rays(P, nparam));
3363 assert(P->NbEq == 0);
3365 dim = P->Dimension;
3366 nvar = dim - nparam;
3367 vcone = new Polyhedron_p[P->NbRays];
3369 for (int j = 0; j < P->NbRays; ++j) {
3370 if (!value_pos_p(P->Ray[j][dim+1]))
3371 continue;
3373 int npos, nneg;
3374 Polyhedron *C = supporting_cone(P, j);
3375 decompose(C, &vcone[j], &npos, &nneg, MaxRays);
3376 ncone += npos + nneg;
3377 sign.SetLength(ncone);
3378 for (int k = 0; k < npos; ++k)
3379 sign[ncone-nneg-k-1] = 1;
3380 for (int k = 0; k < nneg; ++k)
3381 sign[ncone-k-1] = -1;
3384 mat_ZZ rays;
3385 rays.SetDims(ncone * dim, nvar);
3386 int r = 0;
3387 for (int j = 0; j < P->NbRays; ++j) {
3388 if (!value_pos_p(P->Ray[j][dim+1]))
3389 continue;
3391 for (Polyhedron *i = vcone[j]; i; i = i->next) {
3392 add_rays(rays, i, &r, nvar);
3395 rays.SetDims(r, nvar);
3396 vec_ZZ lambda;
3397 nonorthog(rays, lambda);
3400 cout << "rays: " << rays;
3401 cout << "lambda: " << lambda;
3404 int f = 0;
3405 ZZ num_s;
3406 vec_ZZ num_p;
3407 num_p.SetLength(nparam);
3408 vec_ZZ vertex;
3409 vec_ZZ den_s;
3410 den_s.SetLength(dim);
3411 vec_ZZ den_p;
3412 den_p.SetLength(dim);
3413 mat_ZZ den;
3414 den.SetDims(dim, nparam);
3415 ZZ one;
3416 one = 1;
3417 mpq_t count;
3418 mpq_init(count);
3420 gen_fun * gf = new gen_fun;
3422 for (int j = 0; j < P->NbRays; ++j) {
3423 if (!value_pos_p(P->Ray[j][dim+1]))
3424 continue;
3426 for (Polyhedron *i = vcone[j]; i; i = i->next, ++f) {
3427 lattice_point(P->Ray[j]+1, i, vertex);
3428 int k = 0;
3429 num_s = 0;
3430 for ( ; k < nvar; ++k)
3431 num_s += vertex[k] * lambda[k];
3432 for ( ; k < dim; ++k)
3433 num_p[k-nvar] = vertex[k];
3434 normalize(i, lambda, sign[f], num_s, num_p,
3435 den_s, den_p, den);
3437 int only_param = 0;
3438 int no_param = 0;
3439 for (int k = 0; k < dim; ++k) {
3440 if (den_p[k] == 0)
3441 ++no_param;
3442 else if (den_s[k] == 0)
3443 ++only_param;
3445 if (no_param == 0) {
3446 for (int k = 0; k < dim; ++k)
3447 if (den_p[k] == -1)
3448 den[k] = -den[k];
3449 gf->add(sign[f], one, num_p, den);
3450 } else if (no_param + only_param == dim) {
3451 int k, l;
3452 mat_ZZ pden;
3453 pden.SetDims(only_param, nparam);
3455 for (k = 0, l = 0; k < dim; ++k)
3456 if (den_p[k] != 0)
3457 pden[l++] = den[k];
3459 for (k = 0; k < dim; ++k)
3460 if (den_s[k] != 0)
3461 break;
3463 dpoly n(no_param, num_s);
3464 dpoly d(no_param, den_s[k], 1);
3465 for ( ; k < dim; ++k)
3466 if (den_s[k] != 0) {
3467 dpoly fact(no_param, den_s[k], 1);
3468 d *= fact;
3471 mpq_set_si(count, 0, 1);
3472 n.div(d, count, sign[f]);
3474 ZZ qn, qd;
3475 value2zz(mpq_numref(count), qn);
3476 value2zz(mpq_denref(count), qd);
3478 gf->add(qn, qd, num_p, pden);
3479 } else {
3480 int k, l;
3481 dpoly_r * r = 0;
3482 mat_ZZ pden;
3483 pden.SetDims(only_param, nparam);
3485 for (k = 0, l = 0; k < dim; ++k)
3486 if (den_s[k] == 0)
3487 pden[l++] = den[k];
3489 for (k = 0; k < dim; ++k)
3490 if (den_p[k] == 0)
3491 break;
3493 dpoly n(no_param, num_s);
3494 dpoly d(no_param, den_s[k], 1);
3495 for ( ; k < dim; ++k)
3496 if (den_p[k] == 0) {
3497 dpoly fact(no_param, den_s[k], 1);
3498 d *= fact;
3501 for (k = 0; k < dim; ++k) {
3502 if (den_s[k] == 0 || den_p[k] == 0)
3503 continue;
3505 dpoly pd(no_param-1, den_s[k], 1);
3506 int s = den_p[k] < 0 ? -1 : 1;
3508 if (r == 0)
3509 r = new dpoly_r(n, pd, k, s, dim);
3510 else
3511 assert(0); // for now
3514 r->div(d, sign[f], gf, pden, den, num_p);
3518 cout << "sign: " << sign[f];
3519 cout << "num_s: " << num_s;
3520 cout << "num_p: " << num_p;
3521 cout << "den_s: " << den_s;
3522 cout << "den_p: " << den_p;
3523 cout << "den: " << den;
3524 cout << "only_param: " << only_param;
3525 cout << "no_param: " << no_param;
3526 cout << endl;
3532 mpq_clear(count);
3534 return gf;