lexmin.cc: move some code around to prepare for new version
[barvinok.git] / genfun.cc
blob002f256aabcde6a63ba10e398745187582093964
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 = Universe_Polyhedron(0);
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 Polyhedron *context;
268 Matrix *Constraints;
269 Matrix *CP, *T;
270 int dim;
271 int nparam;
272 vector<cone> cones;
274 parallel_polytopes(int n, Polyhedron *context, int nparam) :
275 context(context), dim(-1), nparam(nparam) {
276 red = NULL;
277 Constraints = NULL;
278 CP = NULL;
279 T = NULL;
281 bool add(const QQ& c, Polyhedron *P, unsigned MaxRays) {
282 int i;
284 for (i = 0; i < P->NbEq; ++i)
285 if (First_Non_Zero(P->Constraint[i]+1,
286 P->Dimension-nparam) == -1)
287 break;
288 if (i < P->NbEq)
289 return false;
291 Polyhedron *Q = remove_equalities_p(Polyhedron_Copy(P), P->Dimension-nparam,
292 NULL);
293 POL_ENSURE_VERTICES(Q);
294 if (emptyQ(Q)) {
295 Polyhedron_Free(Q);
296 return true;
299 if (Q->NbEq != 0) {
300 Polyhedron *R;
301 if (!CP) {
302 Matrix *M;
303 M = Matrix_Alloc(Q->NbEq, Q->Dimension+2);
304 Vector_Copy(Q->Constraint[0], M->p[0], Q->NbEq * (Q->Dimension+2));
305 CP = compress_parms(M, nparam);
306 T = align_matrix(CP, Q->Dimension+1);
307 Matrix_Free(M);
309 R = Polyhedron_Preimage(Q, T, MaxRays);
310 Polyhedron_Free(Q);
311 Q = remove_equalities_p(R, R->Dimension-nparam, NULL);
313 assert(Q->NbEq == 0);
315 if (First_Non_Zero(Q->Constraint[Q->NbConstraints-1]+1, Q->Dimension) == -1)
316 Q->NbConstraints--;
318 if (!Constraints) {
319 dim = Q->Dimension;
320 red = gf_base::create(Polyhedron_Copy(context), dim, nparam);
321 red->base->init(Q);
322 Constraints = Matrix_Alloc(Q->NbConstraints, Q->Dimension);
323 for (int i = 0; i < Q->NbConstraints; ++i) {
324 Vector_Copy(Q->Constraint[i]+1, Constraints->p[i], Q->Dimension);
326 } else {
327 assert(Q->Dimension == dim);
328 for (int i = 0; i < Q->NbConstraints; ++i) {
329 int j;
330 for (j = 0; j < Constraints->NbRows; ++j)
331 if (Vector_Equal(Q->Constraint[i]+1, Constraints->p[j],
332 Q->Dimension))
333 break;
334 assert(j < Constraints->NbRows);
338 for (int i = 0; i < Q->NbRays; ++i) {
339 if (!value_pos_p(Q->Ray[i][dim+1]))
340 continue;
342 Polyhedron *C = supporting_cone(Q, i);
344 if (First_Non_Zero(C->Constraint[C->NbConstraints-1]+1,
345 C->Dimension) == -1)
346 C->NbConstraints--;
348 int *pos = new int[1+C->NbConstraints];
349 pos[0] = C->NbConstraints;
350 int l = 0;
351 for (int k = 0; k < Constraints->NbRows; ++k) {
352 for (int j = 0; j < C->NbConstraints; ++j) {
353 if (Vector_Equal(C->Constraint[j]+1, Constraints->p[k],
354 C->Dimension)) {
355 pos[1+l++] = k;
356 break;
360 assert(l == C->NbConstraints);
362 int j;
363 for (j = 0; j < cones.size(); ++j)
364 if (!memcmp(pos, cones[j].pos, (1+C->NbConstraints)*sizeof(int)))
365 break;
366 if (j == cones.size())
367 cones.push_back(cone(pos));
368 else
369 delete [] pos;
371 Polyhedron_Free(C);
373 int k;
374 for (k = 0; k < cones[j].vertices.size(); ++k)
375 if (Vector_Equal(Q->Ray[i]+1, cones[j].vertices[k].first->p,
376 Q->Dimension+1))
377 break;
379 if (k == cones[j].vertices.size()) {
380 Vector *vertex = Vector_Alloc(Q->Dimension+1);
381 Vector_Copy(Q->Ray[i]+1, vertex->p, Q->Dimension+1);
382 cones[j].vertices.push_back(pair<Vector*,QQ>(vertex, c));
383 } else {
384 cones[j].vertices[k].second += c;
385 if (cones[j].vertices[k].second.n == 0) {
386 int size = cones[j].vertices.size();
387 Vector_Free(cones[j].vertices[k].first);
388 if (k < size-1)
389 cones[j].vertices[k] = cones[j].vertices[size-1];
390 cones[j].vertices.pop_back();
395 Polyhedron_Free(Q);
396 return true;
398 gen_fun *compute(unsigned MaxRays) {
399 if (!red)
400 return NULL;
401 for (int i = 0; i < cones.size(); ++i) {
402 Matrix *M = Matrix_Alloc(cones[i].pos[0], 1+Constraints->NbColumns+1);
403 Polyhedron *Cone;
404 for (int j = 0; j <cones[i].pos[0]; ++j) {
405 value_set_si(M->p[j][0], 1);
406 Vector_Copy(Constraints->p[cones[i].pos[1+j]], M->p[j]+1,
407 Constraints->NbColumns);
409 Cone = Constraints2Polyhedron(M, MaxRays);
410 Matrix_Free(M);
411 for (int j = 0; j < cones[i].vertices.size(); ++j) {
412 red->base->do_vertex_cone(cones[i].vertices[j].second,
413 Polyhedron_Copy(Cone),
414 cones[i].vertices[j].first->p,
415 MaxRays);
417 Polyhedron_Free(Cone);
419 if (CP)
420 red->gf->substitute(CP);
421 return red->gf;
423 void print(std::ostream& os) const {
424 for (int i = 0; i < cones.size(); ++i) {
425 os << "[";
426 for (int j = 0; j < cones[i].pos[0]; ++j) {
427 if (j)
428 os << ", ";
429 os << cones[i].pos[1+j];
431 os << "]" << endl;
432 for (int j = 0; j < cones[i].vertices.size(); ++j) {
433 Vector_Print(stderr, P_VALUE_FMT, cones[i].vertices[j].first);
434 os << cones[i].vertices[j].second << endl;
438 ~parallel_polytopes() {
439 for (int i = 0; i < cones.size(); ++i) {
440 delete [] cones[i].pos;
441 for (int j = 0; j < cones[i].vertices.size(); ++j)
442 Vector_Free(cones[i].vertices[j].first);
444 if (Constraints)
445 Matrix_Free(Constraints);
446 if (CP)
447 Matrix_Free(CP);
448 if (T)
449 Matrix_Free(T);
450 delete red;
454 gen_fun *gen_fun::Hadamard_product(const gen_fun *gf, unsigned MaxRays)
456 QQ one(1, 1);
457 Polyhedron *C = DomainIntersection(context, gf->context, MaxRays);
458 Polyhedron *U = Universe_Polyhedron(C->Dimension);
459 gen_fun *sum = new gen_fun(C);
460 for (int i = 0; i < term.size(); ++i) {
461 for (int i2 = 0; i2 < gf->term.size(); ++i2) {
462 int d = term[i]->d.power.NumCols();
463 int k1 = term[i]->d.power.NumRows();
464 int k2 = gf->term[i2]->d.power.NumRows();
465 assert(term[i]->d.power.NumCols() == gf->term[i2]->d.power.NumCols());
467 parallel_polytopes pp(term[i]->n.power.NumRows() *
468 gf->term[i2]->n.power.NumRows(),
469 sum->context, d);
471 for (int j = 0; j < term[i]->n.power.NumRows(); ++j) {
472 for (int j2 = 0; j2 < gf->term[i2]->n.power.NumRows(); ++j2) {
473 Matrix *M = Matrix_Alloc(k1+k2+d+d, 1+k1+k2+d+1);
474 for (int k = 0; k < k1+k2; ++k) {
475 value_set_si(M->p[k][0], 1);
476 value_set_si(M->p[k][1+k], 1);
478 for (int k = 0; k < d; ++k) {
479 value_set_si(M->p[k1+k2+k][1+k1+k2+k], -1);
480 zz2value(term[i]->n.power[j][k], M->p[k1+k2+k][1+k1+k2+d]);
481 for (int l = 0; l < k1; ++l)
482 zz2value(term[i]->d.power[l][k], M->p[k1+k2+k][1+l]);
484 for (int k = 0; k < d; ++k) {
485 value_set_si(M->p[k1+k2+d+k][1+k1+k2+k], -1);
486 zz2value(gf->term[i2]->n.power[j2][k],
487 M->p[k1+k2+d+k][1+k1+k2+d]);
488 for (int l = 0; l < k2; ++l)
489 zz2value(gf->term[i2]->d.power[l][k],
490 M->p[k1+k2+d+k][1+k1+l]);
492 Polyhedron *P = Constraints2Polyhedron(M, MaxRays);
493 Matrix_Free(M);
495 QQ c = term[i]->n.coeff[j];
496 c *= gf->term[i2]->n.coeff[j2];
497 if (!pp.add(c, P, MaxRays)) {
498 gen_fun *t = barvinok_series(P, U, MaxRays);
499 sum->add(c, t);
500 delete t;
503 Polyhedron_Free(P);
507 gen_fun *t = pp.compute(MaxRays);
508 if (t) {
509 sum->add(one, t);
510 delete t;
514 Polyhedron_Free(U);
515 return sum;
518 void gen_fun::add_union(gen_fun *gf, unsigned MaxRays)
520 QQ one(1, 1), mone(-1, 1);
522 gen_fun *hp = Hadamard_product(gf, MaxRays);
523 add(one, gf);
524 add(mone, hp);
525 delete hp;
528 static void Polyhedron_Shift(Polyhedron *P, Vector *offset)
530 Value tmp;
531 value_init(tmp);
532 for (int i = 0; i < P->NbConstraints; ++i) {
533 Inner_Product(P->Constraint[i]+1, offset->p, P->Dimension, &tmp);
534 value_subtract(P->Constraint[i][1+P->Dimension],
535 P->Constraint[i][1+P->Dimension], tmp);
537 for (int i = 0; i < P->NbRays; ++i) {
538 if (value_notone_p(P->Ray[i][0]))
539 continue;
540 if (value_zero_p(P->Ray[i][1+P->Dimension]))
541 continue;
542 Vector_Combine(P->Ray[i]+1, offset->p, P->Ray[i]+1,
543 P->Ray[i][0], P->Ray[i][1+P->Dimension], P->Dimension);
545 value_clear(tmp);
548 void gen_fun::shift(const vec_ZZ& offset)
550 for (int i = 0; i < term.size(); ++i)
551 for (int j = 0; j < term[i]->n.power.NumRows(); ++j)
552 term[i]->n.power[j] += offset;
554 Vector *v = Vector_Alloc(offset.length());
555 zz2values(offset, v->p);
556 Polyhedron_Shift(context, v);
557 Vector_Free(v);
560 /* Divide the generating functin by 1/(1-z^power).
561 * The effect on the corresponding explicit function f(x) is
562 * f'(x) = \sum_{i=0}^\infty f(x - i * power)
564 void gen_fun::divide(const vec_ZZ& power)
566 for (int i = 0; i < term.size(); ++i) {
567 int r = term[i]->d.power.NumRows();
568 int c = term[i]->d.power.NumCols();
569 term[i]->d.power.SetDims(r+1, c);
570 term[i]->d.power[r] = power;
573 Vector *v = Vector_Alloc(1+power.length()+1);
574 value_set_si(v->p[0], 1);
575 zz2values(power, v->p+1);
576 Polyhedron *C = AddRays(v->p, 1, context, context->NbConstraints+1);
577 Vector_Free(v);
578 Polyhedron_Free(context);
579 context = C;
582 static void print_power(std::ostream& os, QQ& c, vec_ZZ& p,
583 unsigned int nparam, char **param_name)
585 bool first = true;
587 for (int i = 0; i < p.length(); ++i) {
588 if (p[i] == 0)
589 continue;
590 if (first) {
591 if (c.n == -1 && c.d == 1)
592 os << "-";
593 else if (c.n != 1 || c.d != 1) {
594 os << c.n;
595 if (c.d != 1)
596 os << " / " << c.d;
597 os << "*";
599 first = false;
600 } else
601 os << "*";
602 if (i < nparam)
603 os << param_name[i];
604 else
605 os << "x" << i;
606 if (p[i] == 1)
607 continue;
608 if (p[i] < 0)
609 os << "^(" << p[i] << ")";
610 else
611 os << "^" << p[i];
613 if (first) {
614 os << c.n;
615 if (c.d != 1)
616 os << " / " << c.d;
620 void gen_fun::print(std::ostream& os, unsigned int nparam, char **param_name) const
622 QQ mone(-1, 1);
623 for (int i = 0; i < term.size(); ++i) {
624 if (i != 0)
625 os << " + ";
626 os << "(";
627 for (int j = 0; j < term[i]->n.coeff.length(); ++j) {
628 if (j != 0 && term[i]->n.coeff[j].n > 0)
629 os << "+";
630 print_power(os, term[i]->n.coeff[j], term[i]->n.power[j],
631 nparam, param_name);
633 os << ")/(";
634 for (int j = 0; j < term[i]->d.power.NumRows(); ++j) {
635 if (j != 0)
636 os << " * ";
637 os << "(1";
638 print_power(os, mone, term[i]->d.power[j], nparam, param_name);
639 os << ")";
641 os << ")";
645 gen_fun::operator evalue *() const
647 evalue *EP = NULL;
648 evalue factor;
649 value_init(factor.d);
650 value_init(factor.x.n);
651 for (int i = 0; i < term.size(); ++i) {
652 unsigned nvar = term[i]->d.power.NumRows();
653 unsigned nparam = term[i]->d.power.NumCols();
654 Matrix *C = Matrix_Alloc(nparam + nvar, 1 + nvar + nparam + 1);
655 mat_ZZ& d = term[i]->d.power;
656 Polyhedron *U = context ? context : Universe_Polyhedron(nparam);
658 for (int j = 0; j < term[i]->n.coeff.length(); ++j) {
659 for (int r = 0; r < nparam; ++r) {
660 value_set_si(C->p[r][0], 0);
661 for (int c = 0; c < nvar; ++c) {
662 zz2value(d[c][r], C->p[r][1+c]);
664 Vector_Set(&C->p[r][1+nvar], 0, nparam);
665 value_set_si(C->p[r][1+nvar+r], -1);
666 zz2value(term[i]->n.power[j][r], C->p[r][1+nvar+nparam]);
668 for (int r = 0; r < nvar; ++r) {
669 value_set_si(C->p[nparam+r][0], 1);
670 Vector_Set(&C->p[nparam+r][1], 0, nvar + nparam + 1);
671 value_set_si(C->p[nparam+r][1+r], 1);
673 Polyhedron *P = Constraints2Polyhedron(C, 0);
674 evalue *E = barvinok_enumerate_ev(P, U, 0);
675 Polyhedron_Free(P);
676 if (EVALUE_IS_ZERO(*E)) {
677 free_evalue_refs(E);
678 free(E);
679 continue;
681 zz2value(term[i]->n.coeff[j].n, factor.x.n);
682 zz2value(term[i]->n.coeff[j].d, factor.d);
683 emul(&factor, E);
685 Matrix_Print(stdout, P_VALUE_FMT, C);
686 char *test[] = { "A", "B", "C", "D", "E", "F", "G" };
687 print_evalue(stdout, E, test);
689 if (!EP)
690 EP = E;
691 else {
692 eadd(E, EP);
693 free_evalue_refs(E);
694 free(E);
697 Matrix_Free(C);
698 if (!context)
699 Polyhedron_Free(U);
701 value_clear(factor.d);
702 value_clear(factor.x.n);
703 return EP;
706 void gen_fun::coefficient(Value* params, Value* c) const
708 if (context && !in_domain(context, params)) {
709 value_set_si(*c, 0);
710 return;
713 evalue part;
714 value_init(part.d);
715 value_init(part.x.n);
716 evalue sum;
717 value_init(sum.d);
718 evalue_set_si(&sum, 0, 1);
719 Value tmp;
720 value_init(tmp);
722 for (int i = 0; i < term.size(); ++i) {
723 unsigned nvar = term[i]->d.power.NumRows();
724 unsigned nparam = term[i]->d.power.NumCols();
725 Matrix *C = Matrix_Alloc(nparam + nvar, 1 + nvar + 1);
726 mat_ZZ& d = term[i]->d.power;
728 for (int j = 0; j < term[i]->n.coeff.length(); ++j) {
729 C->NbRows = nparam+nvar;
730 for (int r = 0; r < nparam; ++r) {
731 value_set_si(C->p[r][0], 0);
732 for (int c = 0; c < nvar; ++c) {
733 zz2value(d[c][r], C->p[r][1+c]);
735 zz2value(term[i]->n.power[j][r], C->p[r][1+nvar]);
736 value_subtract(C->p[r][1+nvar], C->p[r][1+nvar], params[r]);
738 for (int r = 0; r < nvar; ++r) {
739 value_set_si(C->p[nparam+r][0], 1);
740 Vector_Set(&C->p[nparam+r][1], 0, nvar + 1);
741 value_set_si(C->p[nparam+r][1+r], 1);
743 Polyhedron *P = Constraints2Polyhedron(C, 0);
744 if (emptyQ(P)) {
745 Polyhedron_Free(P);
746 continue;
748 barvinok_count(P, &tmp, 0);
749 Polyhedron_Free(P);
750 if (value_zero_p(tmp))
751 continue;
752 zz2value(term[i]->n.coeff[j].n, part.x.n);
753 zz2value(term[i]->n.coeff[j].d, part.d);
754 value_multiply(part.x.n, part.x.n, tmp);
755 eadd(&part, &sum);
757 Matrix_Free(C);
760 assert(value_one_p(sum.d));
761 value_assign(*c, sum.x.n);
763 value_clear(tmp);
764 value_clear(part.d);
765 value_clear(part.x.n);
766 value_clear(sum.d);
767 value_clear(sum.x.n);
770 gen_fun *gen_fun::summate(int nvar) const
772 int dim = context->Dimension;
773 int nparam = dim - nvar;
775 #ifdef USE_INCREMENTAL_DF
776 partial_ireducer red(Polyhedron_Project(context, nparam), dim, nparam);
777 #else
778 partial_reducer red(Polyhedron_Project(context, nparam), dim, nparam);
779 #endif
780 red.init(context);
781 for (int i = 0; i < term.size(); ++i)
782 for (int j = 0; j < term[i]->n.power.NumRows(); ++j)
783 red.reduce(term[i]->n.coeff[j], term[i]->n.power[j], term[i]->d.power);
784 return red.gf;
787 /* returns true if the set was finite and false otherwise */
788 bool gen_fun::summate(Value *sum) const
790 if (term.size() == 0) {
791 value_set_si(*sum, 0);
792 return true;
795 int maxlen = 0;
796 for (int i = 0; i < term.size(); ++i)
797 if (term[i]->d.power.NumRows() > maxlen)
798 maxlen = term[i]->d.power.NumRows();
800 infinite_icounter cnt(term[0]->d.power.NumCols(), maxlen);
801 for (int i = 0; i < term.size(); ++i)
802 for (int j = 0; j < term[i]->n.power.NumRows(); ++j)
803 cnt.reduce(term[i]->n.coeff[j], term[i]->n.power[j], term[i]->d.power);
805 for (int i = 1; i <= maxlen; ++i)
806 if (value_notzero_p(mpq_numref(cnt.count[i]))) {
807 value_set_si(*sum, -1);
808 return false;
811 assert(value_one_p(mpq_denref(cnt.count[0])));
812 value_assign(*sum, mpq_numref(cnt.count[0]));
813 return true;