barvinok_series: handle fixed polytopes
[barvinok.git] / genfun.cc
bloba641e77982a66e787cfc7f9c3e2443ef484902fe
1 #include <iostream>
2 #include <vector>
3 #include <assert.h>
4 #include "config.h"
5 #include <barvinok/genfun.h>
6 #include <barvinok/barvinok.h>
7 #include "conversion.h"
8 #include "genfun_constructor.h"
9 #include "mat_util.h"
11 using std::cout;
12 using std::cerr;
13 using std::endl;
14 using std::pair;
15 using std::vector;
17 static int lex_cmp(mat_ZZ& a, mat_ZZ& b)
19 assert(a.NumCols() == b.NumCols());
20 int alen = a.NumRows();
21 int blen = b.NumRows();
22 int len = alen < blen ? alen : blen;
24 for (int i = 0; i < len; ++i) {
25 int s = lex_cmp(a[i], b[i]);
26 if (s)
27 return s;
29 return alen-blen;
32 static void lex_order_terms(struct short_rat* rat)
34 for (int i = 0; i < rat->n.power.NumRows(); ++i) {
35 int m = i;
36 for (int j = i+1; j < rat->n.power.NumRows(); ++j)
37 if (lex_cmp(rat->n.power[j], rat->n.power[m]) < 0)
38 m = j;
39 if (m != i) {
40 vec_ZZ tmp = rat->n.power[m];
41 rat->n.power[m] = rat->n.power[i];
42 rat->n.power[i] = tmp;
43 QQ tmp_coeff = rat->n.coeff[m];
44 rat->n.coeff[m] = rat->n.coeff[i];
45 rat->n.coeff[i] = tmp_coeff;
50 void short_rat::add(short_rat *r)
52 for (int i = 0; i < r->n.power.NumRows(); ++i) {
53 int len = n.coeff.length();
54 int j;
55 for (j = 0; j < len; ++j)
56 if (r->n.power[i] == n.power[j])
57 break;
58 if (j < len) {
59 n.coeff[j] += r->n.coeff[i];
60 if (n.coeff[j].n == 0) {
61 if (j < len-1) {
62 n.power[j] = n.power[len-1];
63 n.coeff[j] = n.coeff[len-1];
65 int dim = n.power.NumCols();
66 n.coeff.SetLength(len-1);
67 n.power.SetDims(len-1, dim);
69 } else {
70 int dim = n.power.NumCols();
71 n.coeff.SetLength(len+1);
72 n.power.SetDims(len+1, dim);
73 n.coeff[len] = r->n.coeff[i];
74 n.power[len] = r->n.power[i];
79 bool short_rat::reduced()
81 int dim = n.power.NumCols();
82 lex_order_terms(this);
83 if (n.power.NumRows() % 2 == 0) {
84 if (n.coeff[0].n == -n.coeff[1].n &&
85 n.coeff[0].d == n.coeff[1].d) {
86 vec_ZZ step = n.power[1] - n.power[0];
87 int k;
88 for (k = 1; k < n.power.NumRows()/2; ++k) {
89 if (n.coeff[2*k].n != -n.coeff[2*k+1].n ||
90 n.coeff[2*k].d != n.coeff[2*k+1].d)
91 break;
92 if (step != n.power[2*k+1] - n.power[2*k])
93 break;
95 if (k == n.power.NumRows()/2) {
96 for (k = 0; k < d.power.NumRows(); ++k)
97 if (d.power[k] == step)
98 break;
99 if (k < d.power.NumRows()) {
100 for (++k; k < d.power.NumRows(); ++k)
101 d.power[k-1] = d.power[k];
102 d.power.SetDims(k-1, dim);
103 for (k = 1; k < n.power.NumRows()/2; ++k) {
104 n.coeff[k] = n.coeff[2*k];
105 n.power[k] = n.power[2*k];
107 n.coeff.SetLength(k);
108 n.power.SetDims(k, dim);
109 return true;
114 return false;
117 gen_fun::gen_fun(Value c)
119 context = NULL;
120 term.push_back(new short_rat);
121 term[0]->n.coeff.SetLength(1);
122 value2zz(c, term[0]->n.coeff[0].n);
123 term[0]->n.coeff[0].d = 1;
124 term[0]->n.power.SetDims(1, 0);
125 term[0]->d.power.SetDims(0, 0);
128 void gen_fun::add(const QQ& c, const vec_ZZ& num, const mat_ZZ& den)
130 if (c.n == 0)
131 return;
133 short_rat * r = new short_rat;
134 r->n.coeff.SetLength(1);
135 ZZ g = GCD(c.n, c.d);
136 r->n.coeff[0].n = c.n/g;
137 r->n.coeff[0].d = c.d/g;
138 r->n.power.SetDims(1, num.length());
139 r->n.power[0] = num;
140 r->d.power = den;
142 /* Make all powers in denominator lexico-positive */
143 for (int i = 0; i < r->d.power.NumRows(); ++i) {
144 int j;
145 for (j = 0; j < r->d.power.NumCols(); ++j)
146 if (r->d.power[i][j] != 0)
147 break;
148 if (r->d.power[i][j] < 0) {
149 r->d.power[i] = -r->d.power[i];
150 r->n.coeff[0].n = -r->n.coeff[0].n;
151 r->n.power[0] += r->d.power[i];
155 /* Order powers in denominator */
156 lex_order_rows(r->d.power);
158 for (int i = 0; i < term.size(); ++i)
159 if (lex_cmp(term[i]->d.power, r->d.power) == 0) {
160 term[i]->add(r);
161 if (term[i]->n.coeff.length() == 0) {
162 delete term[i];
163 if (i != term.size()-1)
164 term[i] = term[term.size()-1];
165 term.pop_back();
166 } else if (term[i]->reduced()) {
167 delete r;
168 /* we've modified term[i], so removed it
169 * and add it back again
171 r = term[i];
172 if (i != term.size()-1)
173 term[i] = term[term.size()-1];
174 term.pop_back();
175 i = -1;
176 continue;
178 delete r;
179 return;
182 term.push_back(r);
185 void gen_fun::add(const QQ& c, const gen_fun *gf)
187 QQ p;
188 for (int i = 0; i < gf->term.size(); ++i) {
189 for (int j = 0; j < gf->term[i]->n.power.NumRows(); ++j) {
190 p = c;
191 p *= gf->term[i]->n.coeff[j];
192 add(p, gf->term[i]->n.power[j], gf->term[i]->d.power);
197 static void split_param_compression(Matrix *CP, mat_ZZ& map, vec_ZZ& offset)
199 Matrix *T = Transpose(CP);
200 matrix2zz(T, map, T->NbRows-1, T->NbColumns-1);
201 values2zz(T->p[T->NbRows-1], offset, T->NbColumns-1);
202 Matrix_Free(T);
206 * Perform the substitution specified by CP
208 * CP is a homogeneous matrix that maps a set of "compressed parameters"
209 * to the original set of parameters.
211 * This function is applied to a gen_fun computed with the compressed parameters
212 * and adapts it to refer to the original parameters.
214 * That is, if y are the compressed parameters and x = A y + b are the original
215 * parameters, then we want the coefficient of the monomial t^y in the original
216 * generating function to be the coefficient of the monomial u^x in the resulting
217 * generating function.
218 * The original generating function has the form
220 * a t^m/(1-t^n) = a t^m + a t^{m+n} + a t^{m+2n} + ...
222 * Since each term t^y should correspond to a term u^x, with x = A y + b, we want
224 * a u^{A m + b} + a u^{A (m+n) + b} + a u^{A (m+2n) +b} + ... =
226 * = a u^{A m + b}/(1-u^{A n})
228 * Therefore, we multiply the powers m and n in both numerator and denominator by A
229 * and add b to the power in the numerator.
230 * Since the above powers are stored as row vectors m^T and n^T,
231 * we compute, say, m'^T = m^T A^T to obtain m' = A m.
233 * The pair (map, offset) contains the same information as CP.
234 * map is the transpose of the linear part of CP, while offset is the constant part.
236 void gen_fun::substitute(Matrix *CP)
238 mat_ZZ map;
239 vec_ZZ offset;
240 split_param_compression(CP, map, offset);
241 Polyhedron *C = Polyhedron_Image(context, CP, 0);
242 Polyhedron_Free(context);
243 context = C;
244 for (int i = 0; i < term.size(); ++i) {
245 term[i]->d.power *= map;
246 term[i]->n.power *= map;
247 for (int j = 0; j < term[i]->n.power.NumRows(); ++j)
248 term[i]->n.power[j] += offset;
252 struct cone {
253 int *pos;
254 vector<pair<Vector *, QQ> > vertices;
255 cone(int *pos) : pos(pos) {}
258 #ifndef HAVE_COMPRESS_PARMS
259 static Matrix *compress_parms(Matrix *M, unsigned nparam)
261 assert(0);
263 #endif
265 struct parallel_polytopes {
266 gf_base *red;
267 Matrix *Constraints;
268 Matrix *CP, *T;
269 int dim;
270 int nparam;
271 vector<cone> cones;
273 parallel_polytopes(int n, Polyhedron *context, int dim, int nparam) :
274 dim(dim), nparam(nparam) {
275 red = gf_base::create(Polyhedron_Copy(context), dim, nparam);
276 Constraints = NULL;
277 CP = NULL;
278 T = NULL;
280 void add(const QQ& c, Polyhedron *P, unsigned MaxRays) {
281 Polyhedron *Q = remove_equalities_p(Polyhedron_Copy(P), P->Dimension-nparam,
282 NULL);
283 POL_ENSURE_VERTICES(Q);
284 if (emptyQ(Q)) {
285 Polyhedron_Free(Q);
286 return;
289 if (Q->NbEq != 0) {
290 Polyhedron *R;
291 if (!CP) {
292 Matrix *M;
293 M = Matrix_Alloc(Q->NbEq, Q->Dimension+2);
294 Vector_Copy(Q->Constraint[0], M->p[0], Q->NbEq * (Q->Dimension+2));
295 CP = compress_parms(M, nparam);
296 T = align_matrix(CP, Q->Dimension+1);
297 Matrix_Free(M);
299 R = Polyhedron_Preimage(Q, T, MaxRays);
300 Polyhedron_Free(Q);
301 Q = remove_equalities_p(R, R->Dimension-nparam, NULL);
303 assert(Q->NbEq == 0);
304 assert(Q->Dimension == dim);
306 if (First_Non_Zero(Q->Constraint[Q->NbConstraints-1]+1, Q->Dimension) == -1)
307 Q->NbConstraints--;
309 if (!Constraints) {
310 red->base->init(Q);
311 Constraints = Matrix_Alloc(Q->NbConstraints, Q->Dimension);
312 for (int i = 0; i < Q->NbConstraints; ++i) {
313 Vector_Copy(Q->Constraint[i]+1, Constraints->p[i], Q->Dimension);
315 } else {
316 for (int i = 0; i < Q->NbConstraints; ++i) {
317 int j;
318 for (j = 0; j < Constraints->NbRows; ++j)
319 if (Vector_Equal(Q->Constraint[i]+1, Constraints->p[j],
320 Q->Dimension))
321 break;
322 assert(j < Constraints->NbRows);
326 for (int i = 0; i < Q->NbRays; ++i) {
327 if (!value_pos_p(Q->Ray[i][dim+1]))
328 continue;
330 Polyhedron *C = supporting_cone(Q, i);
332 if (First_Non_Zero(C->Constraint[C->NbConstraints-1]+1,
333 C->Dimension) == -1)
334 C->NbConstraints--;
336 int *pos = new int[1+C->NbConstraints];
337 pos[0] = C->NbConstraints;
338 int l = 0;
339 for (int k = 0; k < Constraints->NbRows; ++k) {
340 for (int j = 0; j < C->NbConstraints; ++j) {
341 if (Vector_Equal(C->Constraint[j]+1, Constraints->p[k],
342 C->Dimension)) {
343 pos[1+l++] = k;
344 break;
348 assert(l == C->NbConstraints);
350 int j;
351 for (j = 0; j < cones.size(); ++j)
352 if (!memcmp(pos, cones[j].pos, (1+C->NbConstraints)*sizeof(int)))
353 break;
354 if (j == cones.size())
355 cones.push_back(cone(pos));
356 else
357 delete [] pos;
359 Polyhedron_Free(C);
361 int k;
362 for (k = 0; k < cones[j].vertices.size(); ++k)
363 if (Vector_Equal(Q->Ray[i]+1, cones[j].vertices[k].first->p,
364 Q->Dimension+1))
365 break;
367 if (k == cones[j].vertices.size()) {
368 Vector *vertex = Vector_Alloc(Q->Dimension+1);
369 Vector_Copy(Q->Ray[i]+1, vertex->p, Q->Dimension+1);
370 cones[j].vertices.push_back(pair<Vector*,QQ>(vertex, c));
371 } else {
372 cones[j].vertices[k].second += c;
373 if (cones[j].vertices[k].second.n == 0) {
374 int size = cones[j].vertices.size();
375 Vector_Free(cones[j].vertices[k].first);
376 if (k < size-1)
377 cones[j].vertices[k] = cones[j].vertices[size-1];
378 cones[j].vertices.pop_back();
383 Polyhedron_Free(Q);
385 gen_fun *compute(unsigned MaxRays) {
386 for (int i = 0; i < cones.size(); ++i) {
387 Matrix *M = Matrix_Alloc(cones[i].pos[0], 1+Constraints->NbColumns+1);
388 Polyhedron *Cone;
389 for (int j = 0; j <cones[i].pos[0]; ++j) {
390 value_set_si(M->p[j][0], 1);
391 Vector_Copy(Constraints->p[cones[i].pos[1+j]], M->p[j]+1,
392 Constraints->NbColumns);
394 Cone = Constraints2Polyhedron(M, MaxRays);
395 Matrix_Free(M);
396 for (int j = 0; j < cones[i].vertices.size(); ++j) {
397 red->base->do_vertex_cone(cones[i].vertices[j].second,
398 Polyhedron_Copy(Cone),
399 cones[i].vertices[j].first->p,
400 MaxRays);
402 Polyhedron_Free(Cone);
404 if (CP)
405 red->gf->substitute(CP);
406 return red->gf;
408 void print(std::ostream& os) const {
409 for (int i = 0; i < cones.size(); ++i) {
410 os << "[";
411 for (int j = 0; j < cones[i].pos[0]; ++j) {
412 if (j)
413 os << ", ";
414 os << cones[i].pos[1+j];
416 os << "]" << endl;
417 for (int j = 0; j < cones[i].vertices.size(); ++j) {
418 Vector_Print(stderr, P_VALUE_FMT, cones[i].vertices[j].first);
419 os << cones[i].vertices[j].second << endl;
423 ~parallel_polytopes() {
424 for (int i = 0; i < cones.size(); ++i) {
425 delete [] cones[i].pos;
426 for (int j = 0; j < cones[i].vertices.size(); ++j)
427 Vector_Free(cones[i].vertices[j].first);
429 if (Constraints)
430 Matrix_Free(Constraints);
431 if (CP)
432 Matrix_Free(CP);
433 if (T)
434 Matrix_Free(T);
435 delete red;
439 gen_fun *gen_fun::Hadamard_product(const gen_fun *gf, unsigned MaxRays)
441 QQ one(1, 1);
442 Polyhedron *C = DomainIntersection(context, gf->context, MaxRays);
443 Polyhedron *U = Universe_Polyhedron(C->Dimension);
444 gen_fun *sum = new gen_fun(C);
445 for (int i = 0; i < term.size(); ++i) {
446 for (int i2 = 0; i2 < gf->term.size(); ++i2) {
447 int d = term[i]->d.power.NumCols();
448 int k1 = term[i]->d.power.NumRows();
449 int k2 = gf->term[i2]->d.power.NumRows();
450 assert(term[i]->d.power.NumCols() == gf->term[i2]->d.power.NumCols());
452 parallel_polytopes pp(term[i]->n.power.NumRows() *
453 gf->term[i2]->n.power.NumRows(),
454 sum->context, k1+k2-d, d);
456 for (int j = 0; j < term[i]->n.power.NumRows(); ++j) {
457 for (int j2 = 0; j2 < gf->term[i2]->n.power.NumRows(); ++j2) {
458 Matrix *M = Matrix_Alloc(k1+k2+d+d, 1+k1+k2+d+1);
459 for (int k = 0; k < k1+k2; ++k) {
460 value_set_si(M->p[k][0], 1);
461 value_set_si(M->p[k][1+k], 1);
463 for (int k = 0; k < d; ++k) {
464 value_set_si(M->p[k1+k2+k][1+k1+k2+k], -1);
465 zz2value(term[i]->n.power[j][k], M->p[k1+k2+k][1+k1+k2+d]);
466 for (int l = 0; l < k1; ++l)
467 zz2value(term[i]->d.power[l][k], M->p[k1+k2+k][1+l]);
469 for (int k = 0; k < d; ++k) {
470 value_set_si(M->p[k1+k2+d+k][1+k1+k2+k], -1);
471 zz2value(gf->term[i2]->n.power[j2][k],
472 M->p[k1+k2+d+k][1+k1+k2+d]);
473 for (int l = 0; l < k2; ++l)
474 zz2value(gf->term[i2]->d.power[l][k],
475 M->p[k1+k2+d+k][1+k1+l]);
477 Polyhedron *P = Constraints2Polyhedron(M, MaxRays);
478 Matrix_Free(M);
480 QQ c = term[i]->n.coeff[j];
481 c *= gf->term[i2]->n.coeff[j2];
482 pp.add(c, P, MaxRays);
484 Polyhedron_Free(P);
488 gen_fun *t = pp.compute(MaxRays);
489 sum->add(one, t);
490 delete t;
493 Polyhedron_Free(U);
494 return sum;
497 void gen_fun::add_union(gen_fun *gf, unsigned MaxRays)
499 QQ one(1, 1), mone(-1, 1);
501 gen_fun *hp = Hadamard_product(gf, MaxRays);
502 add(one, gf);
503 add(mone, hp);
504 delete hp;
507 static void Polyhedron_Shift(Polyhedron *P, Vector *offset)
509 Value tmp;
510 value_init(tmp);
511 for (int i = 0; i < P->NbConstraints; ++i) {
512 Inner_Product(P->Constraint[i]+1, offset->p, P->Dimension, &tmp);
513 value_subtract(P->Constraint[i][1+P->Dimension],
514 P->Constraint[i][1+P->Dimension], tmp);
516 for (int i = 0; i < P->NbRays; ++i) {
517 if (value_notone_p(P->Ray[i][0]))
518 continue;
519 if (value_zero_p(P->Ray[i][1+P->Dimension]))
520 continue;
521 Vector_Combine(P->Ray[i]+1, offset->p, P->Ray[i]+1,
522 P->Ray[i][0], P->Ray[i][1+P->Dimension], P->Dimension);
524 value_clear(tmp);
527 void gen_fun::shift(const vec_ZZ& offset)
529 for (int i = 0; i < term.size(); ++i)
530 for (int j = 0; j < term[i]->n.power.NumRows(); ++j)
531 term[i]->n.power[j] += offset;
533 Vector *v = Vector_Alloc(offset.length());
534 zz2values(offset, v->p);
535 Polyhedron_Shift(context, v);
536 Vector_Free(v);
539 /* Divide the generating functin by 1/(1-z^power).
540 * The effect on the corresponding explicit function f(x) is
541 * f'(x) = \sum_{i=0}^\infty f(x - i * power)
543 void gen_fun::divide(const vec_ZZ& power)
545 for (int i = 0; i < term.size(); ++i) {
546 int r = term[i]->d.power.NumRows();
547 int c = term[i]->d.power.NumCols();
548 term[i]->d.power.SetDims(r+1, c);
549 term[i]->d.power[r] = power;
552 Vector *v = Vector_Alloc(1+power.length()+1);
553 value_set_si(v->p[0], 1);
554 zz2values(power, v->p+1);
555 Polyhedron *C = AddRays(v->p, 1, context, context->NbConstraints+1);
556 Vector_Free(v);
557 Polyhedron_Free(context);
558 context = C;
561 static void print_power(std::ostream& os, QQ& c, vec_ZZ& p,
562 unsigned int nparam, char **param_name)
564 bool first = true;
566 for (int i = 0; i < p.length(); ++i) {
567 if (p[i] == 0)
568 continue;
569 if (first) {
570 if (c.n == -1 && c.d == 1)
571 os << "-";
572 else if (c.n != 1 || c.d != 1) {
573 os << c.n;
574 if (c.d != 1)
575 os << " / " << c.d;
576 os << "*";
578 first = false;
579 } else
580 os << "*";
581 if (i < nparam)
582 os << param_name[i];
583 else
584 os << "x" << i;
585 if (p[i] == 1)
586 continue;
587 if (p[i] < 0)
588 os << "^(" << p[i] << ")";
589 else
590 os << "^" << p[i];
592 if (first) {
593 os << c.n;
594 if (c.d != 1)
595 os << " / " << c.d;
599 void gen_fun::print(std::ostream& os, unsigned int nparam, char **param_name) const
601 QQ mone(-1, 1);
602 for (int i = 0; i < term.size(); ++i) {
603 if (i != 0)
604 os << " + ";
605 os << "(";
606 for (int j = 0; j < term[i]->n.coeff.length(); ++j) {
607 if (j != 0 && term[i]->n.coeff[j].n > 0)
608 os << "+";
609 print_power(os, term[i]->n.coeff[j], term[i]->n.power[j],
610 nparam, param_name);
612 os << ")/(";
613 for (int j = 0; j < term[i]->d.power.NumRows(); ++j) {
614 if (j != 0)
615 os << " * ";
616 os << "(1";
617 print_power(os, mone, term[i]->d.power[j], nparam, param_name);
618 os << ")";
620 os << ")";
624 gen_fun::operator evalue *() const
626 evalue *EP = NULL;
627 evalue factor;
628 value_init(factor.d);
629 value_init(factor.x.n);
630 for (int i = 0; i < term.size(); ++i) {
631 unsigned nvar = term[i]->d.power.NumRows();
632 unsigned nparam = term[i]->d.power.NumCols();
633 Matrix *C = Matrix_Alloc(nparam + nvar, 1 + nvar + nparam + 1);
634 mat_ZZ& d = term[i]->d.power;
635 Polyhedron *U = context ? context : Universe_Polyhedron(nparam);
637 for (int j = 0; j < term[i]->n.coeff.length(); ++j) {
638 for (int r = 0; r < nparam; ++r) {
639 value_set_si(C->p[r][0], 0);
640 for (int c = 0; c < nvar; ++c) {
641 zz2value(d[c][r], C->p[r][1+c]);
643 Vector_Set(&C->p[r][1+nvar], 0, nparam);
644 value_set_si(C->p[r][1+nvar+r], -1);
645 zz2value(term[i]->n.power[j][r], C->p[r][1+nvar+nparam]);
647 for (int r = 0; r < nvar; ++r) {
648 value_set_si(C->p[nparam+r][0], 1);
649 Vector_Set(&C->p[nparam+r][1], 0, nvar + nparam + 1);
650 value_set_si(C->p[nparam+r][1+r], 1);
652 Polyhedron *P = Constraints2Polyhedron(C, 0);
653 evalue *E = barvinok_enumerate_ev(P, U, 0);
654 Polyhedron_Free(P);
655 if (EVALUE_IS_ZERO(*E)) {
656 free_evalue_refs(E);
657 free(E);
658 continue;
660 zz2value(term[i]->n.coeff[j].n, factor.x.n);
661 zz2value(term[i]->n.coeff[j].d, factor.d);
662 emul(&factor, E);
664 Matrix_Print(stdout, P_VALUE_FMT, C);
665 char *test[] = { "A", "B", "C", "D", "E", "F", "G" };
666 print_evalue(stdout, E, test);
668 if (!EP)
669 EP = E;
670 else {
671 eadd(E, EP);
672 free_evalue_refs(E);
673 free(E);
676 Matrix_Free(C);
677 if (!context)
678 Polyhedron_Free(U);
680 value_clear(factor.d);
681 value_clear(factor.x.n);
682 return EP;
685 void gen_fun::coefficient(Value* params, Value* c) const
687 if (context && !in_domain(context, params)) {
688 value_set_si(*c, 0);
689 return;
692 evalue part;
693 value_init(part.d);
694 value_init(part.x.n);
695 evalue sum;
696 value_init(sum.d);
697 evalue_set_si(&sum, 0, 1);
698 Value tmp;
699 value_init(tmp);
701 for (int i = 0; i < term.size(); ++i) {
702 unsigned nvar = term[i]->d.power.NumRows();
703 unsigned nparam = term[i]->d.power.NumCols();
704 Matrix *C = Matrix_Alloc(nparam + nvar, 1 + nvar + 1);
705 mat_ZZ& d = term[i]->d.power;
707 for (int j = 0; j < term[i]->n.coeff.length(); ++j) {
708 C->NbRows = nparam+nvar;
709 for (int r = 0; r < nparam; ++r) {
710 value_set_si(C->p[r][0], 0);
711 for (int c = 0; c < nvar; ++c) {
712 zz2value(d[c][r], C->p[r][1+c]);
714 zz2value(term[i]->n.power[j][r], C->p[r][1+nvar]);
715 value_subtract(C->p[r][1+nvar], C->p[r][1+nvar], params[r]);
717 for (int r = 0; r < nvar; ++r) {
718 value_set_si(C->p[nparam+r][0], 1);
719 Vector_Set(&C->p[nparam+r][1], 0, nvar + 1);
720 value_set_si(C->p[nparam+r][1+r], 1);
722 Polyhedron *P = Constraints2Polyhedron(C, 0);
723 if (emptyQ(P)) {
724 Polyhedron_Free(P);
725 continue;
727 barvinok_count(P, &tmp, 0);
728 Polyhedron_Free(P);
729 if (value_zero_p(tmp))
730 continue;
731 zz2value(term[i]->n.coeff[j].n, part.x.n);
732 zz2value(term[i]->n.coeff[j].d, part.d);
733 value_multiply(part.x.n, part.x.n, tmp);
734 eadd(&part, &sum);
736 Matrix_Free(C);
739 assert(value_one_p(sum.d));
740 value_assign(*c, sum.x.n);
742 value_clear(tmp);
743 value_clear(part.d);
744 value_clear(part.x.n);
745 value_clear(sum.d);
746 value_clear(sum.x.n);
749 gen_fun *gen_fun::summate(int nvar) const
751 int dim = context->Dimension;
752 int nparam = dim - nvar;
754 #ifdef USE_INCREMENTAL_DF
755 partial_ireducer red(Polyhedron_Project(context, nparam), dim, nparam);
756 #else
757 partial_reducer red(Polyhedron_Project(context, nparam), dim, nparam);
758 #endif
759 red.init(context);
760 for (int i = 0; i < term.size(); ++i)
761 for (int j = 0; j < term[i]->n.power.NumRows(); ++j)
762 red.reduce(term[i]->n.coeff[j], term[i]->n.power[j], term[i]->d.power);
763 return red.gf;
766 /* returns true if the set was finite and false otherwise */
767 bool gen_fun::summate(Value *sum) const
769 if (term.size() == 0) {
770 value_set_si(*sum, 0);
771 return true;
774 int maxlen = 0;
775 for (int i = 0; i < term.size(); ++i)
776 if (term[i]->d.power.NumRows() > maxlen)
777 maxlen = term[i]->d.power.NumRows();
779 infinite_icounter cnt(term[0]->d.power.NumCols(), maxlen);
780 for (int i = 0; i < term.size(); ++i)
781 for (int j = 0; j < term[i]->n.power.NumRows(); ++j)
782 cnt.reduce(term[i]->n.coeff[j], term[i]->n.power[j], term[i]->d.power);
784 for (int i = 1; i <= maxlen; ++i)
785 if (value_notzero_p(mpq_numref(cnt.count[i]))) {
786 value_set_si(*sum, -1);
787 return false;
790 assert(value_one_p(mpq_denref(cnt.count[0])));
791 value_assign(*sum, mpq_numref(cnt.count[0]));
792 return true;