partial "indicator function" support
[barvinok.git] / barvinok.cc
blobd41ddcfc57a099ceaddda4978aa344f32efe6896
1 #include <assert.h>
2 #include <iostream>
3 #include <vector>
4 #include <deque>
5 #include <string>
6 #include <sstream>
7 #include <gmp.h>
8 #include <NTL/mat_ZZ.h>
9 #include <NTL/LLL.h>
10 #include <util.h>
11 extern "C" {
12 #include <polylib/polylibgmp.h>
13 #include "ev_operations.h"
15 #include <barvinok.h>
16 #include "config.h"
18 #ifdef NTL_STD_CXX
19 using namespace NTL;
20 #endif
21 using std::cout;
22 using std::endl;
23 using std::vector;
24 using std::deque;
25 using std::string;
26 using std::ostringstream;
28 #define ALLOC(p) (((long *) (p))[0])
29 #define SIZE(p) (((long *) (p))[1])
30 #define DATA(p) ((mp_limb_t *) (((long *) (p)) + 2))
32 static void value2zz(Value v, ZZ& z)
34 int sa = v[0]._mp_size;
35 int abs_sa = sa < 0 ? -sa : sa;
37 _ntl_gsetlength(&z.rep, abs_sa);
38 mp_limb_t * adata = DATA(z.rep);
39 for (int i = 0; i < abs_sa; ++i)
40 adata[i] = v[0]._mp_d[i];
41 SIZE(z.rep) = sa;
44 static void zz2value(ZZ& z, Value& v)
46 if (!z.rep) {
47 value_set_si(v, 0);
48 return;
51 int sa = SIZE(z.rep);
52 int abs_sa = sa < 0 ? -sa : sa;
54 mp_limb_t * adata = DATA(z.rep);
55 mpz_realloc2(v, __GMP_BITS_PER_MP_LIMB * abs_sa);
56 for (int i = 0; i < abs_sa; ++i)
57 v[0]._mp_d[i] = adata[i];
58 v[0]._mp_size = sa;
62 * We just ignore the last column and row
63 * If the final element is not equal to one
64 * then the result will actually be a multiple of the input
66 static void matrix2zz(Matrix *M, mat_ZZ& m, unsigned nr, unsigned nc)
68 m.SetDims(nr, nc);
70 for (int i = 0; i < nr; ++i) {
71 // assert(value_one_p(M->p[i][M->NbColumns - 1]));
72 for (int j = 0; j < nc; ++j) {
73 value2zz(M->p[i][j], m[i][j]);
78 static void values2zz(Value *p, vec_ZZ& v, int len)
80 v.SetLength(len);
82 for (int i = 0; i < len; ++i) {
83 value2zz(p[i], v[i]);
88 * We add a 0 at the end, because we need it afterwards
90 static Vector * zz2vector(vec_ZZ& v)
92 Vector *vec = Vector_Alloc(v.length()+1);
93 assert(vec);
94 for (int i = 0; i < v.length(); ++i)
95 zz2value(v[i], vec->p[i]);
97 value_set_si(vec->p[v.length()], 0);
99 return vec;
102 static void rays(mat_ZZ& r, Polyhedron *C)
104 unsigned dim = C->NbRays - 1; /* don't count zero vertex */
105 assert(C->NbRays - 1 == C->Dimension);
106 r.SetDims(dim, dim);
107 ZZ tmp;
109 int i, c;
110 for (i = 0, c = 0; i < dim; ++i)
111 if (value_zero_p(C->Ray[i][dim+1])) {
112 for (int j = 0; j < dim; ++j) {
113 value2zz(C->Ray[i][j+1], tmp);
114 r[j][c] = tmp;
116 ++c;
120 static Matrix * rays(Polyhedron *C)
122 unsigned dim = C->NbRays - 1; /* don't count zero vertex */
123 assert(C->NbRays - 1 == C->Dimension);
125 Matrix *M = Matrix_Alloc(dim+1, dim+1);
126 assert(M);
128 int i, c;
129 for (i = 0, c = 0; i <= dim && c < dim; ++i)
130 if (value_zero_p(C->Ray[i][dim+1])) {
131 Vector_Copy(C->Ray[i] + 1, M->p[c], dim);
132 value_set_si(M->p[c++][dim], 0);
134 assert(c == dim);
135 value_set_si(M->p[dim][dim], 1);
137 return M;
140 static Matrix * rays2(Polyhedron *C)
142 unsigned dim = C->NbRays - 1; /* don't count zero vertex */
143 assert(C->NbRays - 1 == C->Dimension);
145 Matrix *M = Matrix_Alloc(dim, dim);
146 assert(M);
148 int i, c;
149 for (i = 0, c = 0; i <= dim && c < dim; ++i)
150 if (value_zero_p(C->Ray[i][dim+1]))
151 Vector_Copy(C->Ray[i] + 1, M->p[c++], dim);
152 assert(c == dim);
154 return M;
158 * Returns the largest absolute value in the vector
160 static ZZ max(vec_ZZ& v)
162 ZZ max = abs(v[0]);
163 for (int i = 1; i < v.length(); ++i)
164 if (abs(v[i]) > max)
165 max = abs(v[i]);
166 return max;
169 class cone {
170 public:
171 cone(Matrix *M) {
172 Cone = 0;
173 Rays = Matrix_Copy(M);
174 set_det();
176 cone(Polyhedron *C) {
177 Cone = Polyhedron_Copy(C);
178 Rays = rays(C);
179 set_det();
181 void set_det() {
182 mat_ZZ A;
183 matrix2zz(Rays, A, Rays->NbRows - 1, Rays->NbColumns - 1);
184 det = determinant(A);
185 Value v;
186 value_init(v);
187 zz2value(det, v);
188 value_clear(v);
191 Vector* short_vector(vec_ZZ& lambda) {
192 Matrix *M = Matrix_Copy(Rays);
193 Matrix *inv = Matrix_Alloc(M->NbRows, M->NbColumns);
194 int ok = Matrix_Inverse(M, inv);
195 assert(ok);
196 Matrix_Free(M);
198 ZZ det2;
199 mat_ZZ B;
200 mat_ZZ U;
201 matrix2zz(inv, B, inv->NbRows - 1, inv->NbColumns - 1);
202 long r = LLL(det2, B, U);
204 ZZ min = max(B[0]);
205 int index = 0;
206 for (int i = 1; i < B.NumRows(); ++i) {
207 ZZ tmp = max(B[i]);
208 if (tmp < min) {
209 min = tmp;
210 index = i;
214 Matrix_Free(inv);
216 lambda = B[index];
218 Vector *z = zz2vector(U[index]);
219 Value tmp;
220 value_init(tmp);
221 Polyhedron *C = poly();
222 int i;
223 for (i = 0; i < C->NbConstraints; ++i) {
224 Inner_Product(z->p, C->Constraint[i]+1, z->Size-1, &tmp);
225 if (value_pos_p(tmp))
226 break;
228 if (i == C->NbConstraints) {
229 value_set_si(tmp, -1);
230 Vector_Scale(z->p, z->p, tmp, z->Size-1);
232 value_clear(tmp);
233 return z;
236 ~cone() {
237 Polyhedron_Free(Cone);
238 Matrix_Free(Rays);
241 Polyhedron *poly() {
242 if (!Cone) {
243 Matrix *M = Matrix_Alloc(Rays->NbRows+1, Rays->NbColumns+1);
244 for (int i = 0; i < Rays->NbRows; ++i) {
245 Vector_Copy(Rays->p[i], M->p[i]+1, Rays->NbColumns);
246 value_set_si(M->p[i][0], 1);
248 Vector_Set(M->p[Rays->NbRows]+1, 0, Rays->NbColumns-1);
249 value_set_si(M->p[Rays->NbRows][0], 1);
250 value_set_si(M->p[Rays->NbRows][Rays->NbColumns], 1);
251 Cone = Rays2Polyhedron(M, M->NbRows+1);
252 assert(Cone->NbConstraints == Cone->NbRays);
253 Matrix_Free(M);
255 return Cone;
258 ZZ det;
259 Polyhedron *Cone;
260 Matrix *Rays;
263 class dpoly {
264 public:
265 vec_ZZ coeff;
266 dpoly(int d, ZZ& degree, int offset = 0) {
267 coeff.SetLength(d+1);
269 int min = d + offset;
270 if (degree < ZZ(INIT_VAL, min))
271 min = to_int(degree);
273 ZZ c = ZZ(INIT_VAL, 1);
274 if (!offset)
275 coeff[0] = c;
276 for (int i = 1; i <= min; ++i) {
277 c *= (degree -i + 1);
278 c /= i;
279 coeff[i-offset] = c;
282 void operator *= (dpoly& f) {
283 assert(coeff.length() == f.coeff.length());
284 vec_ZZ old = coeff;
285 coeff = f.coeff[0] * coeff;
286 for (int i = 1; i < coeff.length(); ++i)
287 for (int j = 0; i+j < coeff.length(); ++j)
288 coeff[i+j] += f.coeff[i] * old[j];
290 void div(dpoly& d, mpq_t count, ZZ& sign) {
291 int len = coeff.length();
292 Value tmp;
293 value_init(tmp);
294 mpq_t* c = new mpq_t[coeff.length()];
295 mpq_t qtmp;
296 mpq_init(qtmp);
297 for (int i = 0; i < len; ++i) {
298 mpq_init(c[i]);
299 zz2value(coeff[i], tmp);
300 mpq_set_z(c[i], tmp);
302 for (int j = 1; j <= i; ++j) {
303 zz2value(d.coeff[j], tmp);
304 mpq_set_z(qtmp, tmp);
305 mpq_mul(qtmp, qtmp, c[i-j]);
306 mpq_sub(c[i], c[i], qtmp);
309 zz2value(d.coeff[0], tmp);
310 mpq_set_z(qtmp, tmp);
311 mpq_div(c[i], c[i], qtmp);
313 if (sign == -1)
314 mpq_sub(count, count, c[len-1]);
315 else
316 mpq_add(count, count, c[len-1]);
318 value_clear(tmp);
319 mpq_clear(qtmp);
320 for (int i = 0; i < len; ++i)
321 mpq_clear(c[i]);
322 delete [] c;
326 class dpoly_n {
327 public:
328 Matrix *coeff;
329 ~dpoly_n() {
330 Matrix_Free(coeff);
332 dpoly_n(int d, ZZ& degree_0, ZZ& degree_1, int offset = 0) {
333 Value d0, d1;
334 value_init(d0);
335 value_init(d1);
336 zz2value(degree_0, d0);
337 zz2value(degree_1, d1);
338 coeff = Matrix_Alloc(d+1, d+1+1);
339 value_set_si(coeff->p[0][0], 1);
340 value_set_si(coeff->p[0][d+1], 1);
341 for (int i = 1; i <= d; ++i) {
342 value_multiply(coeff->p[i][0], coeff->p[i-1][0], d0);
343 Vector_Combine(coeff->p[i-1], coeff->p[i-1]+1, coeff->p[i]+1,
344 d1, d0, i);
345 value_set_si(coeff->p[i][d+1], i);
346 value_multiply(coeff->p[i][d+1], coeff->p[i][d+1], coeff->p[i-1][d+1]);
347 value_decrement(d0, d0);
349 value_clear(d0);
350 value_clear(d1);
352 void div(dpoly& d, Vector *count, ZZ& sign) {
353 int len = coeff->NbRows;
354 Matrix * c = Matrix_Alloc(coeff->NbRows, coeff->NbColumns);
355 Value tmp;
356 value_init(tmp);
357 for (int i = 0; i < len; ++i) {
358 Vector_Copy(coeff->p[i], c->p[i], len+1);
359 for (int j = 1; j <= i; ++j) {
360 zz2value(d.coeff[j], tmp);
361 value_multiply(tmp, tmp, c->p[i][len]);
362 value_oppose(tmp, tmp);
363 Vector_Combine(c->p[i], c->p[i-j], c->p[i],
364 c->p[i-j][len], tmp, len);
365 value_multiply(c->p[i][len], c->p[i][len], c->p[i-j][len]);
367 zz2value(d.coeff[0], tmp);
368 value_multiply(c->p[i][len], c->p[i][len], tmp);
370 if (sign == -1) {
371 value_set_si(tmp, -1);
372 Vector_Scale(c->p[len-1], count->p, tmp, len);
373 value_assign(count->p[len], c->p[len-1][len]);
374 } else
375 Vector_Copy(c->p[len-1], count->p, len+1);
376 Vector_Normalize(count->p, len+1);
377 value_clear(tmp);
378 Matrix_Free(c);
383 * Barvinok's Decomposition of a simplicial cone
385 * Returns two lists of polyhedra
387 void barvinok_decompose(Polyhedron *C, Polyhedron **ppos, Polyhedron **pneg)
389 Polyhedron *pos = *ppos, *neg = *pneg;
390 vector<cone *> nonuni;
391 cone * c = new cone(C);
392 ZZ det = c->det;
393 int s = sign(det);
394 assert(det != 0);
395 if (abs(det) > 1) {
396 nonuni.push_back(c);
397 } else {
398 Polyhedron *p = Polyhedron_Copy(c->Cone);
399 p->next = pos;
400 pos = p;
401 delete c;
403 vec_ZZ lambda;
404 while (!nonuni.empty()) {
405 c = nonuni.back();
406 nonuni.pop_back();
407 Vector* v = c->short_vector(lambda);
408 for (int i = 0; i < c->Rays->NbRows - 1; ++i) {
409 if (lambda[i] == 0)
410 continue;
411 Matrix* M = Matrix_Copy(c->Rays);
412 Vector_Copy(v->p, M->p[i], v->Size);
413 cone * pc = new cone(M);
414 assert (pc->det != 0);
415 if (abs(pc->det) > 1) {
416 assert(abs(pc->det) < abs(c->det));
417 nonuni.push_back(pc);
418 } else {
419 Polyhedron *p = pc->poly();
420 pc->Cone = 0;
421 if (sign(pc->det) == s) {
422 p->next = pos;
423 pos = p;
424 } else {
425 p->next = neg;
426 neg = p;
428 delete pc;
430 Matrix_Free(M);
432 Vector_Free(v);
433 delete c;
435 *ppos = pos;
436 *pneg = neg;
440 * Returns a single list of npos "positive" cones followed by nneg
441 * "negative" cones.
442 * The input cone is freed
444 void decompose(Polyhedron *cone, Polyhedron **parts, int *npos, int *nneg, unsigned MaxRays)
446 Polyhedron_Polarize(cone);
447 if (cone->NbRays - 1 != cone->Dimension) {
448 Polyhedron *tmp = cone;
449 cone = triangularize_cone(cone, MaxRays);
450 Polyhedron_Free(tmp);
452 Polyhedron *polpos = NULL, *polneg = NULL;
453 *npos = 0; *nneg = 0;
454 for (Polyhedron *Polar = cone; Polar; Polar = Polar->next)
455 barvinok_decompose(Polar, &polpos, &polneg);
457 Polyhedron *last;
458 for (Polyhedron *i = polpos; i; i = i->next) {
459 Polyhedron_Polarize(i);
460 ++*npos;
461 last = i;
463 for (Polyhedron *i = polneg; i; i = i->next) {
464 Polyhedron_Polarize(i);
465 ++*nneg;
467 if (last) {
468 last->next = polneg;
469 *parts = polpos;
470 } else
471 *parts = polneg;
472 Domain_Free(cone);
475 const int MAX_TRY=10;
477 * Searches for a vector that is not othogonal to any
478 * of the rays in rays.
480 static void nonorthog(mat_ZZ& rays, vec_ZZ& lambda)
482 int dim = rays.NumCols();
483 bool found = false;
484 lambda.SetLength(dim);
485 for (int i = 2; !found && i <= 50*dim; i+=4) {
486 for (int j = 0; j < MAX_TRY; ++j) {
487 for (int k = 0; k < dim; ++k) {
488 int r = random_int(i)+2;
489 int v = (2*(r%2)-1) * (r >> 1);
490 lambda[k] = v;
492 int k = 0;
493 for (; k < rays.NumRows(); ++k)
494 if (lambda * rays[k] == 0)
495 break;
496 if (k == rays.NumRows()) {
497 found = true;
498 break;
502 assert(found);
505 static void add_rays(mat_ZZ& rays, Polyhedron *i, int *r)
507 unsigned dim = i->Dimension;
508 for (int k = 0; k < i->NbRays; ++k) {
509 if (!value_zero_p(i->Ray[k][dim+1]))
510 continue;
511 values2zz(i->Ray[k]+1, rays[(*r)++], dim);
515 void lattice_point(Value* values, Polyhedron *i, vec_ZZ& lambda, ZZ& num)
517 vec_ZZ vertex;
518 unsigned dim = i->Dimension;
519 if(!value_one_p(values[dim])) {
520 Matrix* Rays = rays(i);
521 Matrix *inv = Matrix_Alloc(Rays->NbRows, Rays->NbColumns);
522 int ok = Matrix_Inverse(Rays, inv);
523 assert(ok);
524 Matrix_Free(Rays);
525 Rays = rays(i);
526 Vector *lambda = Vector_Alloc(dim+1);
527 Vector_Matrix_Product(values, inv, lambda->p);
528 Matrix_Free(inv);
529 for (int j = 0; j < dim; ++j)
530 mpz_cdiv_q(lambda->p[j], lambda->p[j], lambda->p[dim]);
531 value_set_si(lambda->p[dim], 1);
532 Vector *A = Vector_Alloc(dim+1);
533 Vector_Matrix_Product(lambda->p, Rays, A->p);
534 Vector_Free(lambda);
535 Matrix_Free(Rays);
536 values2zz(A->p, vertex, dim);
537 Vector_Free(A);
538 } else
539 values2zz(values, vertex, dim);
541 num = vertex * lambda;
544 static evalue *term(int param, ZZ& c, Value *den = NULL)
546 evalue *EP = new evalue();
547 value_init(EP->d);
548 value_set_si(EP->d,0);
549 EP->x.p = new_enode(polynomial, 2, param + 1);
550 evalue_set_si(&EP->x.p->arr[0], 0, 1);
551 value_init(EP->x.p->arr[1].x.n);
552 if (den == NULL)
553 value_set_si(EP->x.p->arr[1].d, 1);
554 else
555 value_assign(EP->x.p->arr[1].d, *den);
556 zz2value(c, EP->x.p->arr[1].x.n);
557 return EP;
560 static void vertex_period(
561 Polyhedron *i, vec_ZZ& lambda, Matrix *T,
562 Value lcm, int p, Vector *val,
563 evalue *E, evalue* ev,
564 ZZ& offset)
566 unsigned nparam = T->NbRows - 1;
567 unsigned dim = i->Dimension;
568 Value tmp;
569 ZZ nump;
571 if (p == nparam) {
572 ZZ num, l;
573 Vector * values = Vector_Alloc(dim + 1);
574 Vector_Matrix_Product(val->p, T, values->p);
575 value_assign(values->p[dim], lcm);
576 lattice_point(values->p, i, lambda, num);
577 value2zz(lcm, l);
578 num *= l;
579 num += offset;
580 value_init(ev->x.n);
581 zz2value(num, ev->x.n);
582 value_assign(ev->d, lcm);
583 Vector_Free(values);
584 return;
587 value_init(tmp);
588 vec_ZZ vertex;
589 values2zz(T->p[p], vertex, dim);
590 nump = vertex * lambda;
591 if (First_Non_Zero(val->p, p) == -1) {
592 value_assign(tmp, lcm);
593 evalue *ET = term(p, nump, &tmp);
594 eadd(ET, E);
595 free_evalue_refs(ET);
596 delete ET;
599 value_assign(tmp, lcm);
600 if (First_Non_Zero(T->p[p], dim) != -1)
601 Vector_Gcd(T->p[p], dim, &tmp);
602 Gcd(tmp, lcm, &tmp);
603 if (value_lt(tmp, lcm)) {
604 ZZ count;
606 value_division(tmp, lcm, tmp);
607 value_set_si(ev->d, 0);
608 ev->x.p = new_enode(periodic, VALUE_TO_INT(tmp), p+1);
609 value2zz(tmp, count);
610 do {
611 value_decrement(tmp, tmp);
612 --count;
613 ZZ new_offset = offset - count * nump;
614 value_assign(val->p[p], tmp);
615 vertex_period(i, lambda, T, lcm, p+1, val, E,
616 &ev->x.p->arr[VALUE_TO_INT(tmp)], new_offset);
617 } while (value_pos_p(tmp));
618 } else
619 vertex_period(i, lambda, T, lcm, p+1, val, E, ev, offset);
620 value_clear(tmp);
623 static void mask_r(Matrix *f, int nr, Vector *lcm, int p, Vector *val, evalue *ev)
625 unsigned nparam = lcm->Size;
627 if (p == nparam) {
628 Vector * prod = Vector_Alloc(f->NbRows);
629 Matrix_Vector_Product(f, val->p, prod->p);
630 int isint = 1;
631 for (int i = 0; i < nr; ++i) {
632 value_modulus(prod->p[i], prod->p[i], f->p[i][nparam+1]);
633 isint &= value_zero_p(prod->p[i]);
635 value_set_si(ev->d, 1);
636 value_init(ev->x.n);
637 value_set_si(ev->x.n, isint);
638 Vector_Free(prod);
639 return;
642 Value tmp;
643 value_init(tmp);
644 if (value_one_p(lcm->p[p]))
645 mask_r(f, nr, lcm, p+1, val, ev);
646 else {
647 value_assign(tmp, lcm->p[p]);
648 value_set_si(ev->d, 0);
649 ev->x.p = new_enode(periodic, VALUE_TO_INT(tmp), p+1);
650 do {
651 value_decrement(tmp, tmp);
652 value_assign(val->p[p], tmp);
653 mask_r(f, nr, lcm, p+1, val, &ev->x.p->arr[VALUE_TO_INT(tmp)]);
654 } while (value_pos_p(tmp));
656 value_clear(tmp);
662 static void mask(Matrix *f, evalue *factor)
664 int nr = f->NbRows, nc = f->NbColumns;
665 int n;
666 bool found = false;
667 for (n = 0; n < nr && value_notzero_p(f->p[n][nc-1]); ++n)
668 if (value_notone_p(f->p[n][nc-1]) &&
669 value_notmone_p(f->p[n][nc-1]))
670 found = true;
671 if (!found)
672 return;
674 Value tmp;
675 value_init(tmp);
676 nr = n;
677 unsigned np = nc - 2;
678 Vector *lcm = Vector_Alloc(np);
679 Vector *val = Vector_Alloc(nc);
680 Vector_Set(val->p, 0, nc);
681 value_set_si(val->p[np], 1);
682 Vector_Set(lcm->p, 1, np);
683 for (n = 0; n < nr; ++n) {
684 if (value_one_p(f->p[n][nc-1]) ||
685 value_mone_p(f->p[n][nc-1]))
686 continue;
687 for (int j = 0; j < np; ++j)
688 if (value_notzero_p(f->p[n][j])) {
689 Gcd(f->p[n][j], f->p[n][nc-1], &tmp);
690 value_division(tmp, f->p[n][nc-1], tmp);
691 value_lcm(tmp, lcm->p[j], &lcm->p[j]);
694 evalue EP;
695 value_init(EP.d);
696 mask_r(f, nr, lcm, 0, val, &EP);
697 value_clear(tmp);
698 Vector_Free(val);
699 Vector_Free(lcm);
700 emul(&EP,factor);
701 free_evalue_refs(&EP);
704 static evalue *multi_monom(vec_ZZ& p)
706 evalue *X = new evalue();
707 value_init(X->d);
708 value_init(X->x.n);
709 unsigned nparam = p.length()-1;
710 zz2value(p[nparam], X->x.n);
711 value_set_si(X->d, 1);
712 for (int i = 0; i < nparam; ++i) {
713 if (p[i] == 0)
714 continue;
715 evalue *T = term(i, p[i]);
716 eadd(T, X);
717 free_evalue_refs(T);
718 delete T;
720 return X;
723 struct term_info {
724 evalue *E;
725 ZZ constant;
726 ZZ coeff;
727 int pos;
730 #ifdef USE_MODULO
731 evalue* lattice_point(Polyhedron *i, vec_ZZ& lambda, Matrix *W, Value lcm)
733 unsigned nparam = W->NbColumns - 1;
735 Matrix* Rays = rays2(i);
736 Matrix *T = Transpose(Rays);
737 Matrix *T2 = Matrix_Copy(T);
738 Matrix *inv = Matrix_Alloc(T2->NbRows, T2->NbColumns);
739 int ok = Matrix_Inverse(T2, inv);
740 assert(ok);
741 Matrix_Free(Rays);
742 Matrix_Free(T2);
743 mat_ZZ vertex;
744 matrix2zz(W, vertex, W->NbRows, W->NbColumns);
746 vec_ZZ num;
747 num = lambda * vertex;
749 evalue *EP = multi_monom(num);
751 evalue tmp;
752 value_init(tmp.d);
753 value_init(tmp.x.n);
754 value_set_si(tmp.x.n, 1);
755 value_assign(tmp.d, lcm);
757 emul(&tmp, EP);
759 Matrix *L = Matrix_Alloc(inv->NbRows, W->NbColumns);
760 Matrix_Product(inv, W, L);
762 mat_ZZ RT;
763 matrix2zz(T, RT, T->NbRows, T->NbColumns);
764 Matrix_Free(T);
766 vec_ZZ p = lambda * RT;
768 Value gcd;
769 value_init(gcd);
770 Value mone;
771 value_init(mone);
772 value_set_si(mone, -1);
773 for (int i = 0; i < L->NbRows; ++i) {
774 Vector_Gcd(L->p[i], nparam+1, &gcd);
775 Gcd(gcd, lcm, &gcd);
776 Vector_AntiScale(L->p[i], L->p[i], gcd, nparam+1);
777 Vector_Scale(L->p[i], L->p[i], mone, nparam+1);
778 values2zz(L->p[i], num, nparam+1);
780 value_division(gcd, lcm, gcd);
781 if (value_one_p(gcd))
782 continue;
784 ZZ g;
785 value2zz(gcd, g);
787 int j;
788 for (j = 0; j < nparam+1; ++j)
789 num[j] = num[j] % g;
790 for (j = 0; j < nparam+1; ++j)
791 if (num[j] != 0)
792 break;
793 if (j == nparam+1)
794 continue;
796 if (j < nparam && num[j] > g/2) {
797 for (int k = j; k < nparam; ++k)
798 if (num[k] != 0)
799 num[k] = g - num[k];
800 num[nparam] = g - 1 - num[nparam];
801 value_assign(tmp.d, gcd);
802 ZZ t = p[i]*(g-1);
803 zz2value(t, tmp.x.n);
804 eadd(&tmp, EP);
805 p[i] = -p[i];
808 if (j >= nparam) {
809 ZZ t = num[nparam] * p[i];
810 zz2value(t, tmp.x.n);
811 value_assign(tmp.d, gcd);
812 eadd(&tmp, EP);
813 } else {
814 evalue *E = multi_monom(num);
816 evalue EV;
817 value_init(EV.d);
818 value_set_si(EV.d, 0);
819 EV.x.p = new_enode(modulo, 3, VALUE_TO_INT(gcd));
820 evalue_copy(&EV.x.p->arr[0], E);
821 evalue_set_si(&EV.x.p->arr[1], 0, 1);
822 value_init(EV.x.p->arr[2].x.n);
823 zz2value(p[i], EV.x.p->arr[2].x.n);
824 value_assign(EV.x.p->arr[2].d, gcd);
826 eadd(&EV, EP);
827 free_evalue_refs(&EV);
828 free_evalue_refs(E);
829 delete E;
833 Matrix_Free(L);
834 value_clear(gcd);
835 value_clear(mone);
837 Matrix_Free(inv);
838 free_evalue_refs(&tmp);
839 return EP;
841 #else
842 evalue* lattice_point(Polyhedron *i, vec_ZZ& lambda, Matrix *W, Value lcm)
844 Matrix *T = Transpose(W);
845 unsigned nparam = T->NbRows - 1;
847 evalue *EP = new evalue();
848 value_init(EP->d);
849 evalue_set_si(EP, 0, 1);
851 evalue ev;
852 Vector *val = Vector_Alloc(nparam+1);
853 value_set_si(val->p[nparam], 1);
854 ZZ offset(INIT_VAL, 0);
855 value_init(ev.d);
856 vertex_period(i, lambda, T, lcm, 0, val, EP, &ev, offset);
857 Vector_Free(val);
858 eadd(&ev, EP);
859 free_evalue_refs(&ev);
861 Matrix_Free(T);
863 reduce_evalue(EP);
865 return EP;
867 #endif
869 void lattice_point(
870 Param_Vertices* V, Polyhedron *i, vec_ZZ& lambda, term_info* term)
872 unsigned nparam = V->Vertex->NbColumns - 2;
873 unsigned dim = i->Dimension;
874 mat_ZZ vertex;
875 vertex.SetDims(V->Vertex->NbRows, nparam+1);
876 Value lcm, tmp;
877 value_init(lcm);
878 value_init(tmp);
879 value_set_si(lcm, 1);
880 for (int j = 0; j < V->Vertex->NbRows; ++j) {
881 value_lcm(lcm, V->Vertex->p[j][nparam+1], &lcm);
883 if (value_notone_p(lcm)) {
884 Matrix * mv = Matrix_Alloc(dim, nparam+1);
885 for (int j = 0 ; j < dim; ++j) {
886 value_division(tmp, lcm, V->Vertex->p[j][nparam+1]);
887 Vector_Scale(V->Vertex->p[j], mv->p[j], tmp, nparam+1);
890 term->E = lattice_point(i, lambda, mv, lcm);
891 term->constant = 0;
893 Matrix_Free(mv);
894 value_clear(lcm);
895 value_clear(tmp);
896 return;
898 for (int i = 0; i < V->Vertex->NbRows; ++i) {
899 assert(value_one_p(V->Vertex->p[i][nparam+1])); // for now
900 values2zz(V->Vertex->p[i], vertex[i], nparam+1);
903 vec_ZZ num;
904 num = lambda * vertex;
906 int p = -1;
907 int nn = 0;
908 for (int j = 0; j < nparam; ++j)
909 if (num[j] != 0) {
910 ++nn;
911 p = j;
913 if (nn >= 2) {
914 term->E = multi_monom(num);
915 term->constant = 0;
916 } else {
917 term->E = NULL;
918 term->constant = num[nparam];
919 term->pos = p;
920 if (p != -1)
921 term->coeff = num[p];
924 value_clear(lcm);
925 value_clear(tmp);
928 void normalize(Polyhedron *i, vec_ZZ& lambda, ZZ& sign, ZZ& num, vec_ZZ& den)
930 unsigned dim = i->Dimension;
932 int r = 0;
933 mat_ZZ rays;
934 rays.SetDims(dim, dim);
935 add_rays(rays, i, &r);
936 den = rays * lambda;
937 int change = 0;
939 for (int j = 0; j < den.length(); ++j) {
940 if (den[j] > 0)
941 change ^= 1;
942 else {
943 den[j] = abs(den[j]);
944 num += den[j];
947 if (change)
948 sign = -sign;
951 void barvinok_count(Polyhedron *P, Value* result, unsigned NbMaxCons)
953 Polyhedron ** vcone;
954 vec_ZZ sign;
955 int ncone = 0;
956 sign.SetLength(ncone);
957 unsigned dim;
958 int allocated = 0;
959 Value factor;
960 Polyhedron *Q;
961 int r = 0;
963 if (emptyQ(P)) {
964 value_set_si(*result, 0);
965 return;
967 if (P->NbBid == 0)
968 for (; r < P->NbRays; ++r)
969 if (value_zero_p(P->Ray[r][P->Dimension+1]))
970 break;
971 if (P->NbBid !=0 || r < P->NbRays) {
972 value_set_si(*result, -1);
973 return;
975 if (P->NbEq != 0) {
976 P = remove_equalities(P);
977 if (emptyQ(P)) {
978 Polyhedron_Free(P);
979 value_set_si(*result, 0);
980 return;
982 allocated = 1;
984 value_init(factor);
985 value_set_si(factor, 1);
986 Q = Polyhedron_Reduce(P, &factor);
987 if (Q) {
988 if (allocated)
989 Polyhedron_Free(P);
990 P = Q;
991 allocated = 1;
993 if (P->Dimension == 0) {
994 value_assign(*result, factor);
995 if (allocated)
996 Polyhedron_Free(P);
997 value_clear(factor);
998 return;
1001 dim = P->Dimension;
1002 vcone = new (Polyhedron *)[P->NbRays];
1004 for (int j = 0; j < P->NbRays; ++j) {
1005 int npos, nneg;
1006 Polyhedron *C = supporting_cone(P, j);
1007 decompose(C, &vcone[j], &npos, &nneg, NbMaxCons);
1008 ncone += npos + nneg;
1009 sign.SetLength(ncone);
1010 for (int k = 0; k < npos; ++k)
1011 sign[ncone-nneg-k-1] = 1;
1012 for (int k = 0; k < nneg; ++k)
1013 sign[ncone-k-1] = -1;
1016 mat_ZZ rays;
1017 rays.SetDims(ncone * dim, dim);
1018 r = 0;
1019 for (int j = 0; j < P->NbRays; ++j) {
1020 for (Polyhedron *i = vcone[j]; i; i = i->next) {
1021 assert(i->NbRays-1 == dim);
1022 add_rays(rays, i, &r);
1025 vec_ZZ lambda;
1026 nonorthog(rays, lambda);
1028 vec_ZZ num;
1029 mat_ZZ den;
1030 num.SetLength(ncone);
1031 den.SetDims(ncone,dim);
1033 int f = 0;
1034 for (int j = 0; j < P->NbRays; ++j) {
1035 for (Polyhedron *i = vcone[j]; i; i = i->next) {
1036 lattice_point(P->Ray[j]+1, i, lambda, num[f]);
1037 normalize(i, lambda, sign[f], num[f], den[f]);
1038 ++f;
1041 ZZ min = num[0];
1042 for (int j = 1; j < num.length(); ++j)
1043 if (num[j] < min)
1044 min = num[j];
1045 for (int j = 0; j < num.length(); ++j)
1046 num[j] -= min;
1048 f = 0;
1049 mpq_t count;
1050 mpq_init(count);
1051 for (int j = 0; j < P->NbRays; ++j) {
1052 for (Polyhedron *i = vcone[j]; i; i = i->next) {
1053 dpoly d(dim, num[f]);
1054 dpoly n(dim, den[f][0], 1);
1055 for (int k = 1; k < dim; ++k) {
1056 dpoly fact(dim, den[f][k], 1);
1057 n *= fact;
1059 d.div(n, count, sign[f]);
1060 ++f;
1063 assert(value_one_p(&count[0]._mp_den));
1064 value_multiply(*result, &count[0]._mp_num, factor);
1065 mpq_clear(count);
1067 for (int j = 0; j < P->NbRays; ++j)
1068 Domain_Free(vcone[j]);
1070 delete [] vcone;
1072 if (allocated)
1073 Polyhedron_Free(P);
1074 value_clear(factor);
1077 static void uni_polynom(int param, Vector *c, evalue *EP)
1079 unsigned dim = c->Size-2;
1080 value_init(EP->d);
1081 value_set_si(EP->d,0);
1082 EP->x.p = new_enode(polynomial, dim+1, param+1);
1083 for (int j = 0; j <= dim; ++j)
1084 evalue_set(&EP->x.p->arr[j], c->p[j], c->p[dim+1]);
1087 static void multi_polynom(Vector *c, evalue* X, evalue *EP)
1089 unsigned dim = c->Size-2;
1090 evalue EC;
1092 value_init(EC.d);
1093 evalue_set(&EC, c->p[dim], c->p[dim+1]);
1095 value_init(EP->d);
1096 evalue_set(EP, c->p[dim], c->p[dim+1]);
1098 for (int i = dim-1; i >= 0; --i) {
1099 emul(X, EP);
1100 value_assign(EC.x.n, c->p[i]);
1101 eadd(&EC, EP);
1103 free_evalue_refs(&EC);
1107 Enumeration* barvinok_enumerate(Polyhedron *P, Polyhedron* C, unsigned MaxRays)
1109 Polyhedron *CEq = NULL, *rVD, *CA;
1110 Matrix *CT = NULL;
1111 Param_Polyhedron *PP = NULL;
1112 Param_Domain *D, *next;
1113 Param_Vertices *V;
1114 Enumeration *en, *res;
1115 int r = 0;
1116 unsigned nparam = C->Dimension;
1117 evalue factor;
1118 value_init(factor.d);
1119 evalue_set_si(&factor, 1, 1);
1121 res = NULL;
1123 CA = align_context(C, P->Dimension, MaxRays);
1124 P = DomainIntersection(P, CA, MaxRays);
1125 Polyhedron_Free(CA);
1127 if (C->Dimension == 0 || emptyQ(P)) {
1128 constant:
1129 res = (Enumeration *)malloc(sizeof(Enumeration));
1130 res->ValidityDomain = CEq ? CEq : Polyhedron_Copy(C);
1131 res->next = NULL;
1132 value_init(res->EP.d);
1133 value_set_si(res->EP.d, 1);
1134 value_init(res->EP.x.n);
1135 if (emptyQ(P))
1136 value_set_si(res->EP.x.n, 0);
1137 else
1138 barvinok_count(P, &res->EP.x.n, MaxRays);
1139 emul(&factor, &res->EP);
1140 out:
1141 free_evalue_refs(&factor);
1142 Polyhedron_Free(P);
1143 if (CT)
1144 Matrix_Free(CT);
1145 if (PP)
1146 Param_Polyhedron_Free(PP);
1148 return res;
1151 if (P->NbEq != 0) {
1152 Matrix *f;
1153 P = remove_equalities_p(P, P->Dimension-nparam, &f);
1154 mask(f, &factor);
1155 Matrix_Free(f);
1156 if (P->Dimension == nparam) {
1157 CEq = P;
1158 P = Universe_Polyhedron(0);
1159 goto constant;
1162 Polyhedron *oldP = P;
1163 PP = Polyhedron2Param_SimplifiedDomain(&P,C,MaxRays,&CEq,&CT);
1164 if (P != oldP)
1165 Polyhedron_Free(oldP);
1167 if (isIdentity(CT)) {
1168 Matrix_Free(CT);
1169 CT = NULL;
1170 } else {
1171 assert(CT->NbRows != CT->NbColumns);
1172 if (CT->NbRows == 1) // no more parameters
1173 goto constant;
1174 nparam = CT->NbRows - 1;
1177 unsigned dim = P->Dimension - nparam;
1178 Polyhedron ** vcone = new (Polyhedron *)[PP->nbV];
1179 int * npos = new int[PP->nbV];
1180 int * nneg = new int[PP->nbV];
1181 vec_ZZ sign;
1183 int i;
1184 for (i = 0, V = PP->V; V; ++i, V = V->next) {
1185 Polyhedron *C = supporting_cone_p(P, V);
1186 decompose(C, &vcone[i], &npos[i], &nneg[i], MaxRays);
1189 Vector *c = Vector_Alloc(dim+2);
1191 for(D=PP->D; D; D=next) {
1192 next = D->next;
1193 if (!CEq) {
1194 rVD = D->Domain;
1195 D->Domain = NULL;
1196 } else {
1197 Polyhedron *Dt;
1198 Dt = CT ? Polyhedron_Preimage(D->Domain,CT,MaxRays) : D->Domain;
1199 rVD = DomainIntersection(Dt,CEq,MaxRays);
1201 /* if rVD is empty or too small in geometric dimension */
1202 if(!rVD || emptyQ(rVD) ||
1203 (rVD->Dimension-rVD->NbEq < Dt->Dimension-Dt->NbEq-CEq->NbEq)) {
1204 if(rVD)
1205 Polyhedron_Free(rVD);
1206 if (CT)
1207 Polyhedron_Free(Dt);
1208 continue; /* empty validity domain */
1210 if (CT)
1211 Polyhedron_Free(Dt);
1213 int ncone = 0;
1214 sign.SetLength(ncone);
1215 FORALL_PVertex_in_ParamPolyhedron(V,D,PP) // _i is internal counter
1216 ncone += npos[_i] + nneg[_i];
1217 sign.SetLength(ncone);
1218 for (int k = 0; k < npos[_i]; ++k)
1219 sign[ncone-nneg[_i]-k-1] = 1;
1220 for (int k = 0; k < nneg[_i]; ++k)
1221 sign[ncone-k-1] = -1;
1222 END_FORALL_PVertex_in_ParamPolyhedron;
1224 mat_ZZ rays;
1225 rays.SetDims(ncone * dim, dim);
1226 r = 0;
1227 FORALL_PVertex_in_ParamPolyhedron(V,D,PP) // _i is internal counter
1228 for (Polyhedron *i = vcone[_i]; i; i = i->next) {
1229 assert(i->NbRays-1 == dim);
1230 add_rays(rays, i, &r);
1232 END_FORALL_PVertex_in_ParamPolyhedron;
1233 vec_ZZ lambda;
1234 nonorthog(rays, lambda);
1236 mat_ZZ den;
1237 den.SetDims(ncone,dim);
1238 term_info *num = new term_info[ncone];
1240 int f = 0;
1241 FORALL_PVertex_in_ParamPolyhedron(V,D,PP)
1242 for (Polyhedron *i = vcone[_i]; i; i = i->next) {
1243 lattice_point(V, i, lambda, &num[f]);
1244 normalize(i, lambda, sign[f], num[f].constant, den[f]);
1245 ++f;
1247 END_FORALL_PVertex_in_ParamPolyhedron;
1248 ZZ min = num[0].constant;
1249 for (int j = 1; j < ncone; ++j)
1250 if (num[j].constant < min)
1251 min = num[j].constant;
1252 for (int j = 0; j < ncone; ++j)
1253 num[j].constant -= min;
1254 f = 0;
1255 evalue EP;
1256 value_init(EP.d);
1257 evalue_set_si(&EP, 0, 1);
1258 mpq_t count;
1259 mpq_init(count);
1260 FORALL_PVertex_in_ParamPolyhedron(V,D,PP)
1261 for (Polyhedron *i = vcone[_i]; i; i = i->next) {
1262 dpoly n(dim, den[f][0], 1);
1263 for (int k = 1; k < dim; ++k) {
1264 dpoly fact(dim, den[f][k], 1);
1265 n *= fact;
1267 if (num[f].E != NULL) {
1268 ZZ one(INIT_VAL, 1);
1269 dpoly_n d(dim, num[f].constant, one);
1270 d.div(n, c, sign[f]);
1271 evalue EV;
1272 multi_polynom(c, num[f].E, &EV);
1273 eadd(&EV , &EP);
1274 free_evalue_refs(&EV);
1275 free_evalue_refs(num[f].E);
1276 delete num[f].E;
1277 } else if (num[f].pos != -1) {
1278 dpoly_n d(dim, num[f].constant, num[f].coeff);
1279 d.div(n, c, sign[f]);
1280 evalue EV;
1281 uni_polynom(num[f].pos, c, &EV);
1282 eadd(&EV , &EP);
1283 free_evalue_refs(&EV);
1284 } else {
1285 mpq_set_si(count, 0, 1);
1286 dpoly d(dim, num[f].constant);
1287 d.div(n, count, sign[f]);
1288 evalue EV;
1289 value_init(EV.d);
1290 evalue_set(&EV, &count[0]._mp_num, &count[0]._mp_den);
1291 eadd(&EV , &EP);
1292 free_evalue_refs(&EV);
1294 ++f;
1296 END_FORALL_PVertex_in_ParamPolyhedron;
1298 mpq_clear(count);
1299 delete [] num;
1301 en = (Enumeration *)malloc(sizeof(Enumeration));
1302 en->next = res;
1303 res = en;
1304 res->ValidityDomain = rVD;
1305 if (CT)
1306 addeliminatedparams_evalue(&EP, CT);
1307 emul(&factor, &EP);
1308 res->EP = EP;
1309 reduce_evalue(&res->EP);
1312 Vector_Free(c);
1314 for (int j = 0; j < PP->nbV; ++j)
1315 Domain_Free(vcone[j]);
1316 delete [] vcone;
1317 delete [] npos;
1318 delete [] nneg;
1320 if (CEq)
1321 Polyhedron_Free(CEq);
1323 goto out;