Replace incremental infinite set counter by "regular" infinite set counter
[barvinok.git] / genfun.cc
blob309bd89c2e83c13827fa76af4df50a293ab53534
1 #include <iostream>
2 #include <iomanip>
3 #include <vector>
4 #include <assert.h>
5 #include <barvinok/genfun.h>
6 #include <barvinok/barvinok.h>
7 #include "conversion.h"
8 #include "counter.h"
9 #include "genfun_constructor.h"
10 #include "mat_util.h"
11 #include "matrix_read.h"
13 using std::cout;
14 using std::cerr;
15 using std::endl;
16 using std::pair;
17 using std::vector;
19 bool short_rat_lex_smaller_denominator::operator()(const short_rat* r1,
20 const short_rat* r2) const
22 return lex_cmp(r1->d.power, r2->d.power) < 0;
25 static void lex_order_terms(struct short_rat* rat)
27 for (int i = 0; i < rat->n.power.NumRows(); ++i) {
28 int m = i;
29 for (int j = i+1; j < rat->n.power.NumRows(); ++j)
30 if (lex_cmp(rat->n.power[j], rat->n.power[m]) < 0)
31 m = j;
32 if (m != i) {
33 vec_ZZ tmp = rat->n.power[m];
34 rat->n.power[m] = rat->n.power[i];
35 rat->n.power[i] = tmp;
36 QQ tmp_coeff = rat->n.coeff[m];
37 rat->n.coeff[m] = rat->n.coeff[i];
38 rat->n.coeff[i] = tmp_coeff;
43 short_rat::short_rat(const short_rat& r)
45 n.coeff = r.n.coeff;
46 n.power = r.n.power;
47 d.power = r.d.power;
50 short_rat::short_rat(Value c)
52 n.coeff.SetLength(1);
53 value2zz(c, n.coeff[0].n);
54 n.coeff[0].d = 1;
55 n.power.SetDims(1, 0);
56 d.power.SetDims(0, 0);
59 short_rat::short_rat(const QQ& c, const vec_ZZ& num, const mat_ZZ& den)
61 n.coeff.SetLength(1);
62 ZZ g = GCD(c.n, c.d);
63 n.coeff[0].n = c.n/g;
64 n.coeff[0].d = c.d/g;
65 n.power.SetDims(1, num.length());
66 n.power[0] = num;
67 d.power = den;
68 normalize();
71 short_rat::short_rat(const vec_QQ& c, const mat_ZZ& num, const mat_ZZ& den)
73 n.coeff = c;
74 n.power = num;
75 d.power = den;
76 normalize();
79 void short_rat::normalize()
81 /* Make all powers in denominator reverse-lexico-positive */
82 for (int i = 0; i < d.power.NumRows(); ++i) {
83 int j;
84 for (j = d.power.NumCols()-1; j >= 0; --j)
85 if (!IsZero(d.power[i][j]))
86 break;
87 assert(j >= 0);
88 if (sign(d.power[i][j]) < 0) {
89 negate(d.power[i], d.power[i]);
90 for (int k = 0; k < n.coeff.length(); ++k) {
91 negate(n.coeff[k].n, n.coeff[k].n);
92 n.power[k] += d.power[i];
97 /* Order powers in denominator */
98 lex_order_rows(d.power);
101 void short_rat::add(const short_rat *r)
103 for (int i = 0; i < r->n.power.NumRows(); ++i) {
104 int len = n.coeff.length();
105 int j;
106 for (j = 0; j < len; ++j)
107 if (r->n.power[i] == n.power[j])
108 break;
109 if (j < len) {
110 n.coeff[j] += r->n.coeff[i];
111 if (n.coeff[j].n == 0) {
112 if (j < len-1) {
113 n.power[j] = n.power[len-1];
114 n.coeff[j] = n.coeff[len-1];
116 int dim = n.power.NumCols();
117 n.coeff.SetLength(len-1);
118 n.power.SetDims(len-1, dim);
120 } else {
121 int dim = n.power.NumCols();
122 n.coeff.SetLength(len+1);
123 n.power.SetDims(len+1, dim);
124 n.coeff[len] = r->n.coeff[i];
125 n.power[len] = r->n.power[i];
130 QQ short_rat::coefficient(Value* params, barvinok_options *options) const
132 unsigned nvar = d.power.NumRows();
133 unsigned nparam = d.power.NumCols();
134 Matrix *C = Matrix_Alloc(nparam + nvar, 1 + nvar + 1);
135 Value tmp;
136 value_init(tmp);
138 QQ c(0, 1);
140 for (int j = 0; j < n.coeff.length(); ++j) {
141 C->NbRows = nparam+nvar;
142 for (int r = 0; r < nparam; ++r) {
143 value_set_si(C->p[r][0], 0);
144 for (int c = 0; c < nvar; ++c) {
145 zz2value(d.power[c][r], C->p[r][1+c]);
147 zz2value(n.power[j][r], C->p[r][1+nvar]);
148 value_subtract(C->p[r][1+nvar], C->p[r][1+nvar], params[r]);
150 for (int r = 0; r < nvar; ++r) {
151 value_set_si(C->p[nparam+r][0], 1);
152 Vector_Set(&C->p[nparam+r][1], 0, nvar + 1);
153 value_set_si(C->p[nparam+r][1+r], 1);
155 Polyhedron *P = Constraints2Polyhedron(C, options->MaxRays);
156 if (emptyQ2(P)) {
157 Polyhedron_Free(P);
158 continue;
160 barvinok_count_with_options(P, &tmp, options);
161 Polyhedron_Free(P);
162 if (value_zero_p(tmp))
163 continue;
164 QQ c2(0, 1);
165 value2zz(tmp, c2.n);
166 c2 *= n.coeff[j];
167 c += c2;
169 Matrix_Free(C);
170 value_clear(tmp);
171 return c;
174 bool short_rat::reduced()
176 int dim = n.power.NumCols();
177 lex_order_terms(this);
178 if (n.power.NumRows() % 2 == 0) {
179 if (n.coeff[0].n == -n.coeff[1].n &&
180 n.coeff[0].d == n.coeff[1].d) {
181 vec_ZZ step = n.power[1] - n.power[0];
182 int k;
183 for (k = 1; k < n.power.NumRows()/2; ++k) {
184 if (n.coeff[2*k].n != -n.coeff[2*k+1].n ||
185 n.coeff[2*k].d != n.coeff[2*k+1].d)
186 break;
187 if (step != n.power[2*k+1] - n.power[2*k])
188 break;
190 if (k == n.power.NumRows()/2) {
191 for (k = 0; k < d.power.NumRows(); ++k)
192 if (d.power[k] == step)
193 break;
194 if (k < d.power.NumRows()) {
195 for (++k; k < d.power.NumRows(); ++k)
196 d.power[k-1] = d.power[k];
197 d.power.SetDims(k-1, dim);
198 for (k = 1; k < n.power.NumRows()/2; ++k) {
199 n.coeff[k] = n.coeff[2*k];
200 n.power[k] = n.power[2*k];
202 n.coeff.SetLength(k);
203 n.power.SetDims(k, dim);
204 return true;
209 return false;
212 gen_fun::gen_fun(Value c)
214 short_rat *r = new short_rat(c);
215 context = Universe_Polyhedron(0);
216 term.insert(r);
219 void gen_fun::add(const QQ& c, const vec_ZZ& num, const mat_ZZ& den)
221 if (c.n == 0)
222 return;
224 add(new short_rat(c, num, den));
227 void gen_fun::add(short_rat *r)
229 short_rat_list::iterator i = term.find(r);
230 while (i != term.end()) {
231 (*i)->add(r);
232 if ((*i)->n.coeff.length() == 0) {
233 delete *i;
234 term.erase(i);
235 } else if ((*i)->reduced()) {
236 delete r;
237 /* we've modified term[i], so remove it
238 * and add it back again
240 r = *i;
241 term.erase(i);
242 i = term.find(r);
243 continue;
245 delete r;
246 return;
249 term.insert(r);
252 void gen_fun::add(const QQ& c, const gen_fun *gf, barvinok_options *options)
254 Polyhedron *U = DomainUnion(context, gf->context, options->MaxRays);
255 Polyhedron *C = DomainConvex(U, options->MaxRays);
256 Domain_Free(U);
257 Domain_Free(context);
258 context = C;
260 add(c, gf);
263 void gen_fun::add(const QQ& c, const gen_fun *gf)
265 QQ p;
266 for (short_rat_list::iterator i = gf->term.begin(); i != gf->term.end(); ++i) {
267 for (int j = 0; j < (*i)->n.power.NumRows(); ++j) {
268 p = c;
269 p *= (*i)->n.coeff[j];
270 add(p, (*i)->n.power[j], (*i)->d.power);
275 static void split_param_compression(Matrix *CP, mat_ZZ& map, vec_ZZ& offset)
277 Matrix *T = Transpose(CP);
278 matrix2zz(T, map, T->NbRows-1, T->NbColumns-1);
279 values2zz(T->p[T->NbRows-1], offset, T->NbColumns-1);
280 Matrix_Free(T);
284 * Perform the substitution specified by CP
286 * CP is a homogeneous matrix that maps a set of "compressed parameters"
287 * to the original set of parameters.
289 * This function is applied to a gen_fun computed with the compressed parameters
290 * and adapts it to refer to the original parameters.
292 * That is, if y are the compressed parameters and x = A y + b are the original
293 * parameters, then we want the coefficient of the monomial t^y in the original
294 * generating function to be the coefficient of the monomial u^x in the resulting
295 * generating function.
296 * The original generating function has the form
298 * a t^m/(1-t^n) = a t^m + a t^{m+n} + a t^{m+2n} + ...
300 * Since each term t^y should correspond to a term u^x, with x = A y + b, we want
302 * a u^{A m + b} + a u^{A (m+n) + b} + a u^{A (m+2n) +b} + ... =
304 * = a u^{A m + b}/(1-u^{A n})
306 * Therefore, we multiply the powers m and n in both numerator and denominator by A
307 * and add b to the power in the numerator.
308 * Since the above powers are stored as row vectors m^T and n^T,
309 * we compute, say, m'^T = m^T A^T to obtain m' = A m.
311 * The pair (map, offset) contains the same information as CP.
312 * map is the transpose of the linear part of CP, while offset is the constant part.
314 void gen_fun::substitute(Matrix *CP)
316 mat_ZZ map;
317 vec_ZZ offset;
318 split_param_compression(CP, map, offset);
319 Polyhedron *C = Polyhedron_Image(context, CP, 0);
320 Polyhedron_Free(context);
321 context = C;
323 short_rat_list new_term;
324 for (short_rat_list::iterator i = term.begin(); i != term.end(); ++i) {
325 short_rat *r = (*i);
326 r->d.power *= map;
327 r->n.power *= map;
328 for (int j = 0; j < r->n.power.NumRows(); ++j)
329 r->n.power[j] += offset;
330 r->normalize();
331 new_term.insert(r);
333 term.swap(new_term);
336 struct parallel_cones {
337 int *pos;
338 vector<pair<Vector *, QQ> > vertices;
339 parallel_cones(int *pos) : pos(pos) {}
342 struct parallel_polytopes {
343 gf_base *red;
344 Polyhedron *context;
345 Matrix *Constraints;
346 Matrix *CP, *T;
347 int dim;
348 int nparam;
349 vector<parallel_cones> cones;
350 barvinok_options *options;
352 parallel_polytopes(int n, Polyhedron *context, int nparam,
353 barvinok_options *options) :
354 context(context), dim(-1), nparam(nparam),
355 options(options) {
356 red = NULL;
357 Constraints = NULL;
358 CP = NULL;
359 T = NULL;
361 bool add(const QQ& c, Polyhedron *P) {
362 int i;
364 for (i = 0; i < P->NbEq; ++i)
365 if (First_Non_Zero(P->Constraint[i]+1,
366 P->Dimension-nparam) == -1)
367 break;
368 if (i < P->NbEq)
369 return false;
371 Polyhedron *Q = remove_equalities_p(Polyhedron_Copy(P), P->Dimension-nparam,
372 NULL, options->MaxRays);
373 POL_ENSURE_VERTICES(Q);
374 if (emptyQ(Q)) {
375 Polyhedron_Free(Q);
376 return true;
379 if (Q->NbEq != 0) {
380 Polyhedron *R;
381 if (!CP) {
382 Matrix *M;
383 M = Matrix_Alloc(Q->NbEq, Q->Dimension+2);
384 Vector_Copy(Q->Constraint[0], M->p[0], Q->NbEq * (Q->Dimension+2));
385 CP = compress_parms(M, nparam);
386 T = align_matrix(CP, Q->Dimension+1);
387 Matrix_Free(M);
389 R = Polyhedron_Preimage(Q, T, options->MaxRays);
390 Polyhedron_Free(Q);
391 Q = remove_equalities_p(R, R->Dimension-nparam, NULL,
392 options->MaxRays);
394 assert(Q->NbEq == 0);
396 if (First_Non_Zero(Q->Constraint[Q->NbConstraints-1]+1, Q->Dimension) == -1)
397 Q->NbConstraints--;
399 if (!Constraints) {
400 dim = Q->Dimension;
401 red = gf_base::create(Polyhedron_Copy(context), dim, nparam, options);
402 red->base->init(Q);
403 Constraints = Matrix_Alloc(Q->NbConstraints, Q->Dimension);
404 for (int i = 0; i < Q->NbConstraints; ++i) {
405 Vector_Copy(Q->Constraint[i]+1, Constraints->p[i], Q->Dimension);
407 } else {
408 assert(Q->Dimension == dim);
409 for (int i = 0; i < Q->NbConstraints; ++i) {
410 int j;
411 for (j = 0; j < Constraints->NbRows; ++j)
412 if (Vector_Equal(Q->Constraint[i]+1, Constraints->p[j],
413 Q->Dimension))
414 break;
415 assert(j < Constraints->NbRows);
419 for (int i = 0; i < Q->NbRays; ++i) {
420 if (!value_pos_p(Q->Ray[i][dim+1]))
421 continue;
423 Polyhedron *C = supporting_cone(Q, i);
425 if (First_Non_Zero(C->Constraint[C->NbConstraints-1]+1,
426 C->Dimension) == -1)
427 C->NbConstraints--;
429 int *pos = new int[1+C->NbConstraints];
430 pos[0] = C->NbConstraints;
431 int l = 0;
432 for (int k = 0; k < Constraints->NbRows; ++k) {
433 for (int j = 0; j < C->NbConstraints; ++j) {
434 if (Vector_Equal(C->Constraint[j]+1, Constraints->p[k],
435 C->Dimension)) {
436 pos[1+l++] = k;
437 break;
441 assert(l == C->NbConstraints);
443 int j;
444 for (j = 0; j < cones.size(); ++j)
445 if (!memcmp(pos, cones[j].pos, (1+C->NbConstraints)*sizeof(int)))
446 break;
447 if (j == cones.size())
448 cones.push_back(parallel_cones(pos));
449 else
450 delete [] pos;
452 Polyhedron_Free(C);
454 int k;
455 for (k = 0; k < cones[j].vertices.size(); ++k)
456 if (Vector_Equal(Q->Ray[i]+1, cones[j].vertices[k].first->p,
457 Q->Dimension+1))
458 break;
460 if (k == cones[j].vertices.size()) {
461 Vector *vertex = Vector_Alloc(Q->Dimension+1);
462 Vector_Copy(Q->Ray[i]+1, vertex->p, Q->Dimension+1);
463 cones[j].vertices.push_back(pair<Vector*,QQ>(vertex, c));
464 } else {
465 cones[j].vertices[k].second += c;
466 if (cones[j].vertices[k].second.n == 0) {
467 int size = cones[j].vertices.size();
468 Vector_Free(cones[j].vertices[k].first);
469 if (k < size-1)
470 cones[j].vertices[k] = cones[j].vertices[size-1];
471 cones[j].vertices.pop_back();
476 Polyhedron_Free(Q);
477 return true;
479 gen_fun *compute() {
480 if (!red)
481 return NULL;
482 for (int i = 0; i < cones.size(); ++i) {
483 Matrix *M = Matrix_Alloc(cones[i].pos[0], 1+Constraints->NbColumns+1);
484 Polyhedron *Cone;
485 for (int j = 0; j <cones[i].pos[0]; ++j) {
486 value_set_si(M->p[j][0], 1);
487 Vector_Copy(Constraints->p[cones[i].pos[1+j]], M->p[j]+1,
488 Constraints->NbColumns);
490 Cone = Constraints2Polyhedron(M, options->MaxRays);
491 Matrix_Free(M);
492 for (int j = 0; j < cones[i].vertices.size(); ++j) {
493 red->base->do_vertex_cone(cones[i].vertices[j].second,
494 Polyhedron_Copy(Cone),
495 cones[i].vertices[j].first->p, options);
497 Polyhedron_Free(Cone);
499 if (CP)
500 red->gf->substitute(CP);
501 return red->gf;
503 void print(std::ostream& os) const {
504 for (int i = 0; i < cones.size(); ++i) {
505 os << "[";
506 for (int j = 0; j < cones[i].pos[0]; ++j) {
507 if (j)
508 os << ", ";
509 os << cones[i].pos[1+j];
511 os << "]" << endl;
512 for (int j = 0; j < cones[i].vertices.size(); ++j) {
513 Vector_Print(stderr, P_VALUE_FMT, cones[i].vertices[j].first);
514 os << cones[i].vertices[j].second << endl;
518 ~parallel_polytopes() {
519 for (int i = 0; i < cones.size(); ++i) {
520 delete [] cones[i].pos;
521 for (int j = 0; j < cones[i].vertices.size(); ++j)
522 Vector_Free(cones[i].vertices[j].first);
524 if (Constraints)
525 Matrix_Free(Constraints);
526 if (CP)
527 Matrix_Free(CP);
528 if (T)
529 Matrix_Free(T);
530 delete red;
534 gen_fun *gen_fun::Hadamard_product(const gen_fun *gf, barvinok_options *options)
536 QQ one(1, 1);
537 Polyhedron *C = DomainIntersection(context, gf->context, options->MaxRays);
538 Polyhedron *U = Universe_Polyhedron(C->Dimension);
539 gen_fun *sum = new gen_fun(C);
540 for (short_rat_list::iterator i = term.begin(); i != term.end(); ++i) {
541 for (short_rat_list::iterator i2 = gf->term.begin(); i2 != gf->term.end();
542 ++i2) {
543 int d = (*i)->d.power.NumCols();
544 int k1 = (*i)->d.power.NumRows();
545 int k2 = (*i2)->d.power.NumRows();
546 assert((*i)->d.power.NumCols() == (*i2)->d.power.NumCols());
548 parallel_polytopes pp((*i)->n.power.NumRows() *
549 (*i2)->n.power.NumRows(),
550 sum->context, d, options);
552 for (int j = 0; j < (*i)->n.power.NumRows(); ++j) {
553 for (int j2 = 0; j2 < (*i2)->n.power.NumRows(); ++j2) {
554 Matrix *M = Matrix_Alloc(k1+k2+d+d, 1+k1+k2+d+1);
555 for (int k = 0; k < k1+k2; ++k) {
556 value_set_si(M->p[k][0], 1);
557 value_set_si(M->p[k][1+k], 1);
559 for (int k = 0; k < d; ++k) {
560 value_set_si(M->p[k1+k2+k][1+k1+k2+k], -1);
561 zz2value((*i)->n.power[j][k], M->p[k1+k2+k][1+k1+k2+d]);
562 for (int l = 0; l < k1; ++l)
563 zz2value((*i)->d.power[l][k], M->p[k1+k2+k][1+l]);
565 for (int k = 0; k < d; ++k) {
566 value_set_si(M->p[k1+k2+d+k][1+k1+k2+k], -1);
567 zz2value((*i2)->n.power[j2][k],
568 M->p[k1+k2+d+k][1+k1+k2+d]);
569 for (int l = 0; l < k2; ++l)
570 zz2value((*i2)->d.power[l][k],
571 M->p[k1+k2+d+k][1+k1+l]);
573 Polyhedron *P = Constraints2Polyhedron(M, options->MaxRays);
574 Matrix_Free(M);
576 QQ c = (*i)->n.coeff[j];
577 c *= (*i2)->n.coeff[j2];
578 if (!pp.add(c, P)) {
579 gen_fun *t = barvinok_series_with_options(P, U, options);
580 sum->add(c, t);
581 delete t;
584 Polyhedron_Free(P);
588 gen_fun *t = pp.compute();
589 if (t) {
590 sum->add(one, t);
591 delete t;
595 Polyhedron_Free(U);
596 return sum;
599 void gen_fun::add_union(gen_fun *gf, barvinok_options *options)
601 QQ one(1, 1), mone(-1, 1);
603 gen_fun *hp = Hadamard_product(gf, options);
604 add(one, gf);
605 add(mone, hp);
606 delete hp;
609 static void Polyhedron_Shift(Polyhedron *P, Vector *offset)
611 Value tmp;
612 value_init(tmp);
613 for (int i = 0; i < P->NbConstraints; ++i) {
614 Inner_Product(P->Constraint[i]+1, offset->p, P->Dimension, &tmp);
615 value_subtract(P->Constraint[i][1+P->Dimension],
616 P->Constraint[i][1+P->Dimension], tmp);
618 for (int i = 0; i < P->NbRays; ++i) {
619 if (value_notone_p(P->Ray[i][0]))
620 continue;
621 if (value_zero_p(P->Ray[i][1+P->Dimension]))
622 continue;
623 Vector_Combine(P->Ray[i]+1, offset->p, P->Ray[i]+1,
624 P->Ray[i][0], P->Ray[i][1+P->Dimension], P->Dimension);
626 value_clear(tmp);
629 void gen_fun::shift(const vec_ZZ& offset)
631 for (short_rat_list::iterator i = term.begin(); i != term.end(); ++i)
632 for (int j = 0; j < (*i)->n.power.NumRows(); ++j)
633 (*i)->n.power[j] += offset;
635 Vector *v = Vector_Alloc(offset.length());
636 zz2values(offset, v->p);
637 Polyhedron_Shift(context, v);
638 Vector_Free(v);
641 /* Divide the generating functin by 1/(1-z^power).
642 * The effect on the corresponding explicit function f(x) is
643 * f'(x) = \sum_{i=0}^\infty f(x - i * power)
645 void gen_fun::divide(const vec_ZZ& power)
647 for (short_rat_list::iterator i = term.begin(); i != term.end(); ++i) {
648 int r = (*i)->d.power.NumRows();
649 int c = (*i)->d.power.NumCols();
650 (*i)->d.power.SetDims(r+1, c);
651 (*i)->d.power[r] = power;
654 Vector *v = Vector_Alloc(1+power.length()+1);
655 value_set_si(v->p[0], 1);
656 zz2values(power, v->p+1);
657 Polyhedron *C = AddRays(v->p, 1, context, context->NbConstraints+1);
658 Vector_Free(v);
659 Polyhedron_Free(context);
660 context = C;
663 static void print_power(std::ostream& os, const QQ& c, const vec_ZZ& p,
664 unsigned int nparam, char **param_name)
666 bool first = true;
668 for (int i = 0; i < p.length(); ++i) {
669 if (p[i] == 0)
670 continue;
671 if (first) {
672 if (c.n == -1 && c.d == 1)
673 os << "-";
674 else if (c.n != 1 || c.d != 1) {
675 os << c.n;
676 if (c.d != 1)
677 os << "/" << c.d;
678 os << "*";
680 first = false;
681 } else
682 os << "*";
683 if (i < nparam)
684 os << param_name[i];
685 else
686 os << "x" << i;
687 if (p[i] == 1)
688 continue;
689 if (p[i] < 0)
690 os << "^(" << p[i] << ")";
691 else
692 os << "^" << p[i];
694 if (first) {
695 os << c.n;
696 if (c.d != 1)
697 os << "/" << c.d;
701 void short_rat::print(std::ostream& os, unsigned int nparam, char **param_name) const
703 QQ mone(-1, 1);
704 os << "(";
705 for (int j = 0; j < n.coeff.length(); ++j) {
706 if (j != 0 && n.coeff[j].n >= 0)
707 os << "+";
708 print_power(os, n.coeff[j], n.power[j], nparam, param_name);
710 os << ")/(";
711 for (int j = 0; j < d.power.NumRows(); ++j) {
712 if (j != 0)
713 os << " * ";
714 os << "(1";
715 print_power(os, mone, d.power[j], nparam, param_name);
716 os << ")";
718 os << ")";
721 void gen_fun::print(std::ostream& os, unsigned int nparam, char **param_name) const
723 for (short_rat_list::iterator i = term.begin(); i != term.end(); ++i) {
724 if (i != term.begin())
725 os << " + ";
726 (*i)->print(os, nparam, param_name);
730 std::ostream & operator<< (std::ostream & os, const short_rat& r)
732 os << r.n.coeff << endl;
733 os << r.n.power << endl;
734 os << r.d.power << endl;
735 return os;
738 std::ostream & operator<< (std::ostream & os, const Polyhedron& P)
740 char *str;
741 void (*gmp_free)(void *, size_t);
742 mp_get_memory_functions(NULL, NULL, &gmp_free);
743 os << P.NbConstraints << " " << P.Dimension+2 << endl;
744 for (int i = 0; i < P.NbConstraints; ++i) {
745 for (int j = 0; j < P.Dimension+2; ++j) {
746 str = mpz_get_str(0, 10, P.Constraint[i][j]);
747 os << std::setw(4) << str << " ";
748 (*gmp_free)(str, strlen(str)+1);
750 os << endl;
752 return os;
755 std::ostream & operator<< (std::ostream & os, const gen_fun& gf)
757 os << *gf.context << endl;
758 os << endl;
759 os << gf.term.size() << endl;
760 for (short_rat_list::iterator i = gf.term.begin(); i != gf.term.end(); ++i)
761 os << **i;
762 return os;
765 gen_fun *gen_fun::read(std::istream& is, barvinok_options *options)
767 Matrix *M = Matrix_Read(is);
768 Polyhedron *C = Constraints2Polyhedron(M, options->MaxRays);
769 Matrix_Free(M);
771 gen_fun *gf = new gen_fun(C);
773 int n;
774 is >> n;
776 vec_QQ c;
777 mat_ZZ num;
778 mat_ZZ den;
779 for (int i = 0; i < n; ++i) {
780 is >> c >> num >> den;
781 gf->add(new short_rat(c, num, den));
784 return gf;
787 gen_fun::operator evalue *() const
789 evalue *EP = NULL;
790 evalue factor;
791 value_init(factor.d);
792 value_init(factor.x.n);
793 for (short_rat_list::iterator i = term.begin(); i != term.end(); ++i) {
794 unsigned nvar = (*i)->d.power.NumRows();
795 unsigned nparam = (*i)->d.power.NumCols();
796 Matrix *C = Matrix_Alloc(nparam + nvar, 1 + nvar + nparam + 1);
797 mat_ZZ& d = (*i)->d.power;
798 Polyhedron *U = context;
800 for (int j = 0; j < (*i)->n.coeff.length(); ++j) {
801 for (int r = 0; r < nparam; ++r) {
802 value_set_si(C->p[r][0], 0);
803 for (int c = 0; c < nvar; ++c) {
804 zz2value(d[c][r], C->p[r][1+c]);
806 Vector_Set(&C->p[r][1+nvar], 0, nparam);
807 value_set_si(C->p[r][1+nvar+r], -1);
808 zz2value((*i)->n.power[j][r], C->p[r][1+nvar+nparam]);
810 for (int r = 0; r < nvar; ++r) {
811 value_set_si(C->p[nparam+r][0], 1);
812 Vector_Set(&C->p[nparam+r][1], 0, nvar + nparam + 1);
813 value_set_si(C->p[nparam+r][1+r], 1);
815 Polyhedron *P = Constraints2Polyhedron(C, 0);
816 evalue *E = barvinok_enumerate_ev(P, U, 0);
817 Polyhedron_Free(P);
818 if (EVALUE_IS_ZERO(*E)) {
819 evalue_free(E);
820 continue;
822 zz2value((*i)->n.coeff[j].n, factor.x.n);
823 zz2value((*i)->n.coeff[j].d, factor.d);
824 emul(&factor, E);
825 if (!EP)
826 EP = E;
827 else {
828 eadd(E, EP);
829 evalue_free(E);
832 Matrix_Free(C);
834 value_clear(factor.d);
835 value_clear(factor.x.n);
836 return EP ? EP : evalue_zero();
839 ZZ gen_fun::coefficient(Value* params, barvinok_options *options) const
841 if (!in_domain(context, params))
842 return ZZ::zero();
844 QQ sum(0, 1);
846 for (short_rat_list::iterator i = term.begin(); i != term.end(); ++i)
847 sum += (*i)->coefficient(params, options);
849 assert(sum.d == 1);
850 return sum.n;
853 void gen_fun::coefficient(Value* params, Value* c) const
855 barvinok_options *options = barvinok_options_new_with_defaults();
857 ZZ coeff = coefficient(params, options);
859 zz2value(coeff, *c);
861 barvinok_options_free(options);
864 gen_fun *gen_fun::summate(int nvar, barvinok_options *options) const
866 int dim = context->Dimension;
867 int nparam = dim - nvar;
868 reducer *red;
869 gen_fun *gf;
871 if (options->incremental_specialization == 1) {
872 red = new partial_ireducer(Polyhedron_Project(context, nparam), dim, nparam);
873 } else
874 red = new partial_reducer(Polyhedron_Project(context, nparam), dim, nparam);
875 for (;;) {
876 try {
877 red->init(context);
878 for (short_rat_list::iterator i = term.begin(); i != term.end(); ++i)
879 red->reduce((*i)->n.coeff, (*i)->n.power, (*i)->d.power);
880 break;
881 } catch (OrthogonalException &e) {
882 red->reset();
885 gf = red->get_gf();
886 delete red;
887 return gf;
890 /* returns true if the set was finite and false otherwise */
891 bool gen_fun::summate(Value *sum) const
893 if (term.size() == 0) {
894 value_set_si(*sum, 0);
895 return true;
898 int maxlen = 0;
899 for (short_rat_list::iterator i = term.begin(); i != term.end(); ++i)
900 if ((*i)->d.power.NumRows() > maxlen)
901 maxlen = (*i)->d.power.NumRows();
903 infinite_counter cnt((*term.begin())->d.power.NumCols(), maxlen);
904 cnt.init(context);
905 for (short_rat_list::iterator i = term.begin(); i != term.end(); ++i)
906 cnt.reduce((*i)->n.coeff, (*i)->n.power, (*i)->d.power);
908 for (int i = 1; i <= maxlen; ++i)
909 if (value_notzero_p(mpq_numref(cnt.count[i]))) {
910 value_set_si(*sum, -1);
911 return false;
914 assert(value_one_p(mpq_denref(cnt.count[0])));
915 value_assign(*sum, mpq_numref(cnt.count[0]));
916 return true;