short_rat::print: correctly print out terms with a zero coefficient
[barvinok.git] / genfun.cc
blobafc2d0c02086240d6606b88406b0fb303f2f6362
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 "genfun_constructor.h"
9 #include "mat_util.h"
10 #include "matrix_read.h"
12 using std::cout;
13 using std::cerr;
14 using std::endl;
15 using std::pair;
16 using std::vector;
18 bool short_rat_lex_smaller_denominator::operator()(const short_rat* r1,
19 const short_rat* r2) const
21 return lex_cmp(r1->d.power, r2->d.power) < 0;
24 static void lex_order_terms(struct short_rat* rat)
26 for (int i = 0; i < rat->n.power.NumRows(); ++i) {
27 int m = i;
28 for (int j = i+1; j < rat->n.power.NumRows(); ++j)
29 if (lex_cmp(rat->n.power[j], rat->n.power[m]) < 0)
30 m = j;
31 if (m != i) {
32 vec_ZZ tmp = rat->n.power[m];
33 rat->n.power[m] = rat->n.power[i];
34 rat->n.power[i] = tmp;
35 QQ tmp_coeff = rat->n.coeff[m];
36 rat->n.coeff[m] = rat->n.coeff[i];
37 rat->n.coeff[i] = tmp_coeff;
42 short_rat::short_rat(const short_rat& r)
44 n.coeff = r.n.coeff;
45 n.power = r.n.power;
46 d.power = r.d.power;
49 short_rat::short_rat(Value c)
51 n.coeff.SetLength(1);
52 value2zz(c, n.coeff[0].n);
53 n.coeff[0].d = 1;
54 n.power.SetDims(1, 0);
55 d.power.SetDims(0, 0);
58 short_rat::short_rat(const QQ& c, const vec_ZZ& num, const mat_ZZ& den)
60 n.coeff.SetLength(1);
61 ZZ g = GCD(c.n, c.d);
62 n.coeff[0].n = c.n/g;
63 n.coeff[0].d = c.d/g;
64 n.power.SetDims(1, num.length());
65 n.power[0] = num;
66 d.power = den;
67 normalize();
70 short_rat::short_rat(const vec_QQ& c, const mat_ZZ& num, const mat_ZZ& den)
72 n.coeff = c;
73 n.power = num;
74 d.power = den;
75 normalize();
78 void short_rat::normalize()
80 /* Make all powers in denominator reverse-lexico-positive */
81 for (int i = 0; i < d.power.NumRows(); ++i) {
82 int j;
83 for (j = d.power.NumCols()-1; j >= 0; --j)
84 if (!IsZero(d.power[i][j]))
85 break;
86 assert(j >= 0);
87 if (sign(d.power[i][j]) < 0) {
88 negate(d.power[i], d.power[i]);
89 for (int k = 0; k < n.coeff.length(); ++k) {
90 negate(n.coeff[k].n, n.coeff[k].n);
91 n.power[k] += d.power[i];
96 /* Order powers in denominator */
97 lex_order_rows(d.power);
100 void short_rat::add(const short_rat *r)
102 for (int i = 0; i < r->n.power.NumRows(); ++i) {
103 int len = n.coeff.length();
104 int j;
105 for (j = 0; j < len; ++j)
106 if (r->n.power[i] == n.power[j])
107 break;
108 if (j < len) {
109 n.coeff[j] += r->n.coeff[i];
110 if (n.coeff[j].n == 0) {
111 if (j < len-1) {
112 n.power[j] = n.power[len-1];
113 n.coeff[j] = n.coeff[len-1];
115 int dim = n.power.NumCols();
116 n.coeff.SetLength(len-1);
117 n.power.SetDims(len-1, dim);
119 } else {
120 int dim = n.power.NumCols();
121 n.coeff.SetLength(len+1);
122 n.power.SetDims(len+1, dim);
123 n.coeff[len] = r->n.coeff[i];
124 n.power[len] = r->n.power[i];
129 QQ short_rat::coefficient(Value* params, barvinok_options *options) const
131 unsigned nvar = d.power.NumRows();
132 unsigned nparam = d.power.NumCols();
133 Matrix *C = Matrix_Alloc(nparam + nvar, 1 + nvar + 1);
134 Value tmp;
135 value_init(tmp);
137 QQ c(0, 1);
139 for (int j = 0; j < n.coeff.length(); ++j) {
140 C->NbRows = nparam+nvar;
141 for (int r = 0; r < nparam; ++r) {
142 value_set_si(C->p[r][0], 0);
143 for (int c = 0; c < nvar; ++c) {
144 zz2value(d.power[c][r], C->p[r][1+c]);
146 zz2value(n.power[j][r], C->p[r][1+nvar]);
147 value_subtract(C->p[r][1+nvar], C->p[r][1+nvar], params[r]);
149 for (int r = 0; r < nvar; ++r) {
150 value_set_si(C->p[nparam+r][0], 1);
151 Vector_Set(&C->p[nparam+r][1], 0, nvar + 1);
152 value_set_si(C->p[nparam+r][1+r], 1);
154 Polyhedron *P = Constraints2Polyhedron(C, options->MaxRays);
155 if (emptyQ2(P)) {
156 Polyhedron_Free(P);
157 continue;
159 barvinok_count_with_options(P, &tmp, options);
160 Polyhedron_Free(P);
161 if (value_zero_p(tmp))
162 continue;
163 QQ c2(0, 1);
164 value2zz(tmp, c2.n);
165 c2 *= n.coeff[j];
166 c += c2;
168 Matrix_Free(C);
169 value_clear(tmp);
170 return c;
173 bool short_rat::reduced()
175 int dim = n.power.NumCols();
176 lex_order_terms(this);
177 if (n.power.NumRows() % 2 == 0) {
178 if (n.coeff[0].n == -n.coeff[1].n &&
179 n.coeff[0].d == n.coeff[1].d) {
180 vec_ZZ step = n.power[1] - n.power[0];
181 int k;
182 for (k = 1; k < n.power.NumRows()/2; ++k) {
183 if (n.coeff[2*k].n != -n.coeff[2*k+1].n ||
184 n.coeff[2*k].d != n.coeff[2*k+1].d)
185 break;
186 if (step != n.power[2*k+1] - n.power[2*k])
187 break;
189 if (k == n.power.NumRows()/2) {
190 for (k = 0; k < d.power.NumRows(); ++k)
191 if (d.power[k] == step)
192 break;
193 if (k < d.power.NumRows()) {
194 for (++k; k < d.power.NumRows(); ++k)
195 d.power[k-1] = d.power[k];
196 d.power.SetDims(k-1, dim);
197 for (k = 1; k < n.power.NumRows()/2; ++k) {
198 n.coeff[k] = n.coeff[2*k];
199 n.power[k] = n.power[2*k];
201 n.coeff.SetLength(k);
202 n.power.SetDims(k, dim);
203 return true;
208 return false;
211 gen_fun::gen_fun(Value c)
213 short_rat *r = new short_rat(c);
214 context = Universe_Polyhedron(0);
215 term.insert(r);
218 void gen_fun::add(const QQ& c, const vec_ZZ& num, const mat_ZZ& den)
220 if (c.n == 0)
221 return;
223 add(new short_rat(c, num, den));
226 void gen_fun::add(short_rat *r)
228 short_rat_list::iterator i = term.find(r);
229 while (i != term.end()) {
230 (*i)->add(r);
231 if ((*i)->n.coeff.length() == 0) {
232 delete *i;
233 term.erase(i);
234 } else if ((*i)->reduced()) {
235 delete r;
236 /* we've modified term[i], so remove it
237 * and add it back again
239 r = *i;
240 term.erase(i);
241 i = term.find(r);
242 continue;
244 delete r;
245 return;
248 term.insert(r);
251 void gen_fun::add(const QQ& c, const gen_fun *gf)
253 QQ p;
254 for (short_rat_list::iterator i = gf->term.begin(); i != gf->term.end(); ++i) {
255 for (int j = 0; j < (*i)->n.power.NumRows(); ++j) {
256 p = c;
257 p *= (*i)->n.coeff[j];
258 add(p, (*i)->n.power[j], (*i)->d.power);
263 static void split_param_compression(Matrix *CP, mat_ZZ& map, vec_ZZ& offset)
265 Matrix *T = Transpose(CP);
266 matrix2zz(T, map, T->NbRows-1, T->NbColumns-1);
267 values2zz(T->p[T->NbRows-1], offset, T->NbColumns-1);
268 Matrix_Free(T);
272 * Perform the substitution specified by CP
274 * CP is a homogeneous matrix that maps a set of "compressed parameters"
275 * to the original set of parameters.
277 * This function is applied to a gen_fun computed with the compressed parameters
278 * and adapts it to refer to the original parameters.
280 * That is, if y are the compressed parameters and x = A y + b are the original
281 * parameters, then we want the coefficient of the monomial t^y in the original
282 * generating function to be the coefficient of the monomial u^x in the resulting
283 * generating function.
284 * The original generating function has the form
286 * a t^m/(1-t^n) = a t^m + a t^{m+n} + a t^{m+2n} + ...
288 * Since each term t^y should correspond to a term u^x, with x = A y + b, we want
290 * a u^{A m + b} + a u^{A (m+n) + b} + a u^{A (m+2n) +b} + ... =
292 * = a u^{A m + b}/(1-u^{A n})
294 * Therefore, we multiply the powers m and n in both numerator and denominator by A
295 * and add b to the power in the numerator.
296 * Since the above powers are stored as row vectors m^T and n^T,
297 * we compute, say, m'^T = m^T A^T to obtain m' = A m.
299 * The pair (map, offset) contains the same information as CP.
300 * map is the transpose of the linear part of CP, while offset is the constant part.
302 void gen_fun::substitute(Matrix *CP)
304 mat_ZZ map;
305 vec_ZZ offset;
306 split_param_compression(CP, map, offset);
307 Polyhedron *C = Polyhedron_Image(context, CP, 0);
308 Polyhedron_Free(context);
309 context = C;
311 short_rat_list new_term;
312 for (short_rat_list::iterator i = term.begin(); i != term.end(); ++i) {
313 short_rat *r = (*i);
314 r->d.power *= map;
315 r->n.power *= map;
316 for (int j = 0; j < r->n.power.NumRows(); ++j)
317 r->n.power[j] += offset;
318 r->normalize();
319 new_term.insert(r);
321 term.swap(new_term);
324 struct parallel_cones {
325 int *pos;
326 vector<pair<Vector *, QQ> > vertices;
327 parallel_cones(int *pos) : pos(pos) {}
330 struct parallel_polytopes {
331 gf_base *red;
332 Polyhedron *context;
333 Matrix *Constraints;
334 Matrix *CP, *T;
335 int dim;
336 int nparam;
337 vector<parallel_cones> cones;
338 barvinok_options *options;
340 parallel_polytopes(int n, Polyhedron *context, int nparam,
341 barvinok_options *options) :
342 context(context), dim(-1), nparam(nparam),
343 options(options) {
344 red = NULL;
345 Constraints = NULL;
346 CP = NULL;
347 T = NULL;
349 bool add(const QQ& c, Polyhedron *P) {
350 int i;
352 for (i = 0; i < P->NbEq; ++i)
353 if (First_Non_Zero(P->Constraint[i]+1,
354 P->Dimension-nparam) == -1)
355 break;
356 if (i < P->NbEq)
357 return false;
359 Polyhedron *Q = remove_equalities_p(Polyhedron_Copy(P), P->Dimension-nparam,
360 NULL, options->MaxRays);
361 POL_ENSURE_VERTICES(Q);
362 if (emptyQ(Q)) {
363 Polyhedron_Free(Q);
364 return true;
367 if (Q->NbEq != 0) {
368 Polyhedron *R;
369 if (!CP) {
370 Matrix *M;
371 M = Matrix_Alloc(Q->NbEq, Q->Dimension+2);
372 Vector_Copy(Q->Constraint[0], M->p[0], Q->NbEq * (Q->Dimension+2));
373 CP = compress_parms(M, nparam);
374 T = align_matrix(CP, Q->Dimension+1);
375 Matrix_Free(M);
377 R = Polyhedron_Preimage(Q, T, options->MaxRays);
378 Polyhedron_Free(Q);
379 Q = remove_equalities_p(R, R->Dimension-nparam, NULL,
380 options->MaxRays);
382 assert(Q->NbEq == 0);
384 if (First_Non_Zero(Q->Constraint[Q->NbConstraints-1]+1, Q->Dimension) == -1)
385 Q->NbConstraints--;
387 if (!Constraints) {
388 dim = Q->Dimension;
389 red = gf_base::create(Polyhedron_Copy(context), dim, nparam, options);
390 red->base->init(Q);
391 Constraints = Matrix_Alloc(Q->NbConstraints, Q->Dimension);
392 for (int i = 0; i < Q->NbConstraints; ++i) {
393 Vector_Copy(Q->Constraint[i]+1, Constraints->p[i], Q->Dimension);
395 } else {
396 assert(Q->Dimension == dim);
397 for (int i = 0; i < Q->NbConstraints; ++i) {
398 int j;
399 for (j = 0; j < Constraints->NbRows; ++j)
400 if (Vector_Equal(Q->Constraint[i]+1, Constraints->p[j],
401 Q->Dimension))
402 break;
403 assert(j < Constraints->NbRows);
407 for (int i = 0; i < Q->NbRays; ++i) {
408 if (!value_pos_p(Q->Ray[i][dim+1]))
409 continue;
411 Polyhedron *C = supporting_cone(Q, i);
413 if (First_Non_Zero(C->Constraint[C->NbConstraints-1]+1,
414 C->Dimension) == -1)
415 C->NbConstraints--;
417 int *pos = new int[1+C->NbConstraints];
418 pos[0] = C->NbConstraints;
419 int l = 0;
420 for (int k = 0; k < Constraints->NbRows; ++k) {
421 for (int j = 0; j < C->NbConstraints; ++j) {
422 if (Vector_Equal(C->Constraint[j]+1, Constraints->p[k],
423 C->Dimension)) {
424 pos[1+l++] = k;
425 break;
429 assert(l == C->NbConstraints);
431 int j;
432 for (j = 0; j < cones.size(); ++j)
433 if (!memcmp(pos, cones[j].pos, (1+C->NbConstraints)*sizeof(int)))
434 break;
435 if (j == cones.size())
436 cones.push_back(parallel_cones(pos));
437 else
438 delete [] pos;
440 Polyhedron_Free(C);
442 int k;
443 for (k = 0; k < cones[j].vertices.size(); ++k)
444 if (Vector_Equal(Q->Ray[i]+1, cones[j].vertices[k].first->p,
445 Q->Dimension+1))
446 break;
448 if (k == cones[j].vertices.size()) {
449 Vector *vertex = Vector_Alloc(Q->Dimension+1);
450 Vector_Copy(Q->Ray[i]+1, vertex->p, Q->Dimension+1);
451 cones[j].vertices.push_back(pair<Vector*,QQ>(vertex, c));
452 } else {
453 cones[j].vertices[k].second += c;
454 if (cones[j].vertices[k].second.n == 0) {
455 int size = cones[j].vertices.size();
456 Vector_Free(cones[j].vertices[k].first);
457 if (k < size-1)
458 cones[j].vertices[k] = cones[j].vertices[size-1];
459 cones[j].vertices.pop_back();
464 Polyhedron_Free(Q);
465 return true;
467 gen_fun *compute() {
468 if (!red)
469 return NULL;
470 for (int i = 0; i < cones.size(); ++i) {
471 Matrix *M = Matrix_Alloc(cones[i].pos[0], 1+Constraints->NbColumns+1);
472 Polyhedron *Cone;
473 for (int j = 0; j <cones[i].pos[0]; ++j) {
474 value_set_si(M->p[j][0], 1);
475 Vector_Copy(Constraints->p[cones[i].pos[1+j]], M->p[j]+1,
476 Constraints->NbColumns);
478 Cone = Constraints2Polyhedron(M, options->MaxRays);
479 Matrix_Free(M);
480 for (int j = 0; j < cones[i].vertices.size(); ++j) {
481 red->base->do_vertex_cone(cones[i].vertices[j].second,
482 Polyhedron_Copy(Cone),
483 cones[i].vertices[j].first->p, options);
485 Polyhedron_Free(Cone);
487 if (CP)
488 red->gf->substitute(CP);
489 return red->gf;
491 void print(std::ostream& os) const {
492 for (int i = 0; i < cones.size(); ++i) {
493 os << "[";
494 for (int j = 0; j < cones[i].pos[0]; ++j) {
495 if (j)
496 os << ", ";
497 os << cones[i].pos[1+j];
499 os << "]" << endl;
500 for (int j = 0; j < cones[i].vertices.size(); ++j) {
501 Vector_Print(stderr, P_VALUE_FMT, cones[i].vertices[j].first);
502 os << cones[i].vertices[j].second << endl;
506 ~parallel_polytopes() {
507 for (int i = 0; i < cones.size(); ++i) {
508 delete [] cones[i].pos;
509 for (int j = 0; j < cones[i].vertices.size(); ++j)
510 Vector_Free(cones[i].vertices[j].first);
512 if (Constraints)
513 Matrix_Free(Constraints);
514 if (CP)
515 Matrix_Free(CP);
516 if (T)
517 Matrix_Free(T);
518 delete red;
522 gen_fun *gen_fun::Hadamard_product(const gen_fun *gf, barvinok_options *options)
524 QQ one(1, 1);
525 Polyhedron *C = DomainIntersection(context, gf->context, options->MaxRays);
526 Polyhedron *U = Universe_Polyhedron(C->Dimension);
527 gen_fun *sum = new gen_fun(C);
528 for (short_rat_list::iterator i = term.begin(); i != term.end(); ++i) {
529 for (short_rat_list::iterator i2 = gf->term.begin(); i2 != gf->term.end();
530 ++i2) {
531 int d = (*i)->d.power.NumCols();
532 int k1 = (*i)->d.power.NumRows();
533 int k2 = (*i2)->d.power.NumRows();
534 assert((*i)->d.power.NumCols() == (*i2)->d.power.NumCols());
536 parallel_polytopes pp((*i)->n.power.NumRows() *
537 (*i2)->n.power.NumRows(),
538 sum->context, d, options);
540 for (int j = 0; j < (*i)->n.power.NumRows(); ++j) {
541 for (int j2 = 0; j2 < (*i2)->n.power.NumRows(); ++j2) {
542 Matrix *M = Matrix_Alloc(k1+k2+d+d, 1+k1+k2+d+1);
543 for (int k = 0; k < k1+k2; ++k) {
544 value_set_si(M->p[k][0], 1);
545 value_set_si(M->p[k][1+k], 1);
547 for (int k = 0; k < d; ++k) {
548 value_set_si(M->p[k1+k2+k][1+k1+k2+k], -1);
549 zz2value((*i)->n.power[j][k], M->p[k1+k2+k][1+k1+k2+d]);
550 for (int l = 0; l < k1; ++l)
551 zz2value((*i)->d.power[l][k], M->p[k1+k2+k][1+l]);
553 for (int k = 0; k < d; ++k) {
554 value_set_si(M->p[k1+k2+d+k][1+k1+k2+k], -1);
555 zz2value((*i2)->n.power[j2][k],
556 M->p[k1+k2+d+k][1+k1+k2+d]);
557 for (int l = 0; l < k2; ++l)
558 zz2value((*i2)->d.power[l][k],
559 M->p[k1+k2+d+k][1+k1+l]);
561 Polyhedron *P = Constraints2Polyhedron(M, options->MaxRays);
562 Matrix_Free(M);
564 QQ c = (*i)->n.coeff[j];
565 c *= (*i2)->n.coeff[j2];
566 if (!pp.add(c, P)) {
567 gen_fun *t = barvinok_series_with_options(P, U, options);
568 sum->add(c, t);
569 delete t;
572 Polyhedron_Free(P);
576 gen_fun *t = pp.compute();
577 if (t) {
578 sum->add(one, t);
579 delete t;
583 Polyhedron_Free(U);
584 return sum;
587 void gen_fun::add_union(gen_fun *gf, barvinok_options *options)
589 QQ one(1, 1), mone(-1, 1);
591 gen_fun *hp = Hadamard_product(gf, options);
592 add(one, gf);
593 add(mone, hp);
594 delete hp;
597 static void Polyhedron_Shift(Polyhedron *P, Vector *offset)
599 Value tmp;
600 value_init(tmp);
601 for (int i = 0; i < P->NbConstraints; ++i) {
602 Inner_Product(P->Constraint[i]+1, offset->p, P->Dimension, &tmp);
603 value_subtract(P->Constraint[i][1+P->Dimension],
604 P->Constraint[i][1+P->Dimension], tmp);
606 for (int i = 0; i < P->NbRays; ++i) {
607 if (value_notone_p(P->Ray[i][0]))
608 continue;
609 if (value_zero_p(P->Ray[i][1+P->Dimension]))
610 continue;
611 Vector_Combine(P->Ray[i]+1, offset->p, P->Ray[i]+1,
612 P->Ray[i][0], P->Ray[i][1+P->Dimension], P->Dimension);
614 value_clear(tmp);
617 void gen_fun::shift(const vec_ZZ& offset)
619 for (short_rat_list::iterator i = term.begin(); i != term.end(); ++i)
620 for (int j = 0; j < (*i)->n.power.NumRows(); ++j)
621 (*i)->n.power[j] += offset;
623 Vector *v = Vector_Alloc(offset.length());
624 zz2values(offset, v->p);
625 Polyhedron_Shift(context, v);
626 Vector_Free(v);
629 /* Divide the generating functin by 1/(1-z^power).
630 * The effect on the corresponding explicit function f(x) is
631 * f'(x) = \sum_{i=0}^\infty f(x - i * power)
633 void gen_fun::divide(const vec_ZZ& power)
635 for (short_rat_list::iterator i = term.begin(); i != term.end(); ++i) {
636 int r = (*i)->d.power.NumRows();
637 int c = (*i)->d.power.NumCols();
638 (*i)->d.power.SetDims(r+1, c);
639 (*i)->d.power[r] = power;
642 Vector *v = Vector_Alloc(1+power.length()+1);
643 value_set_si(v->p[0], 1);
644 zz2values(power, v->p+1);
645 Polyhedron *C = AddRays(v->p, 1, context, context->NbConstraints+1);
646 Vector_Free(v);
647 Polyhedron_Free(context);
648 context = C;
651 static void print_power(std::ostream& os, const QQ& c, const vec_ZZ& p,
652 unsigned int nparam, char **param_name)
654 bool first = true;
656 for (int i = 0; i < p.length(); ++i) {
657 if (p[i] == 0)
658 continue;
659 if (first) {
660 if (c.n == -1 && c.d == 1)
661 os << "-";
662 else if (c.n != 1 || c.d != 1) {
663 os << c.n;
664 if (c.d != 1)
665 os << " / " << c.d;
666 os << "*";
668 first = false;
669 } else
670 os << "*";
671 if (i < nparam)
672 os << param_name[i];
673 else
674 os << "x" << i;
675 if (p[i] == 1)
676 continue;
677 if (p[i] < 0)
678 os << "^(" << p[i] << ")";
679 else
680 os << "^" << p[i];
682 if (first) {
683 os << c.n;
684 if (c.d != 1)
685 os << " / " << c.d;
689 void short_rat::print(std::ostream& os, unsigned int nparam, char **param_name) const
691 QQ mone(-1, 1);
692 os << "(";
693 for (int j = 0; j < n.coeff.length(); ++j) {
694 if (j != 0 && n.coeff[j].n >= 0)
695 os << "+";
696 print_power(os, n.coeff[j], n.power[j], nparam, param_name);
698 os << ")/(";
699 for (int j = 0; j < d.power.NumRows(); ++j) {
700 if (j != 0)
701 os << " * ";
702 os << "(1";
703 print_power(os, mone, d.power[j], nparam, param_name);
704 os << ")";
706 os << ")";
709 void gen_fun::print(std::ostream& os, unsigned int nparam, char **param_name) const
711 for (short_rat_list::iterator i = term.begin(); i != term.end(); ++i) {
712 if (i != term.begin())
713 os << " + ";
714 (*i)->print(os, nparam, param_name);
718 std::ostream & operator<< (std::ostream & os, const short_rat& r)
720 os << r.n.coeff << endl;
721 os << r.n.power << endl;
722 os << r.d.power << endl;
723 return os;
726 std::ostream & operator<< (std::ostream & os, const Polyhedron& P)
728 char *str;
729 void (*gmp_free)(void *, size_t);
730 mp_get_memory_functions(NULL, NULL, &gmp_free);
731 os << P.NbConstraints << " " << P.Dimension+2 << endl;
732 for (int i = 0; i < P.NbConstraints; ++i) {
733 for (int j = 0; j < P.Dimension+2; ++j) {
734 str = mpz_get_str(0, 10, P.Constraint[i][j]);
735 os << std::setw(4) << str << " ";
736 (*gmp_free)(str, strlen(str)+1);
738 os << endl;
740 return os;
743 std::ostream & operator<< (std::ostream & os, const gen_fun& gf)
745 os << *gf.context << endl;
746 os << endl;
747 os << gf.term.size() << endl;
748 for (short_rat_list::iterator i = gf.term.begin(); i != gf.term.end(); ++i)
749 os << **i;
750 return os;
753 gen_fun *gen_fun::read(std::istream& is, barvinok_options *options)
755 Matrix *M = Matrix_Read(is);
756 Polyhedron *C = Constraints2Polyhedron(M, options->MaxRays);
757 Matrix_Free(M);
759 gen_fun *gf = new gen_fun(C);
761 int n;
762 is >> n;
764 vec_QQ c;
765 mat_ZZ num;
766 mat_ZZ den;
767 for (int i = 0; i < n; ++i) {
768 is >> c >> num >> den;
769 gf->add(new short_rat(c, num, den));
772 return gf;
775 gen_fun::operator evalue *() const
777 evalue *EP = NULL;
778 evalue factor;
779 value_init(factor.d);
780 value_init(factor.x.n);
781 for (short_rat_list::iterator i = term.begin(); i != term.end(); ++i) {
782 unsigned nvar = (*i)->d.power.NumRows();
783 unsigned nparam = (*i)->d.power.NumCols();
784 Matrix *C = Matrix_Alloc(nparam + nvar, 1 + nvar + nparam + 1);
785 mat_ZZ& d = (*i)->d.power;
786 Polyhedron *U = context ? context : Universe_Polyhedron(nparam);
788 for (int j = 0; j < (*i)->n.coeff.length(); ++j) {
789 for (int r = 0; r < nparam; ++r) {
790 value_set_si(C->p[r][0], 0);
791 for (int c = 0; c < nvar; ++c) {
792 zz2value(d[c][r], C->p[r][1+c]);
794 Vector_Set(&C->p[r][1+nvar], 0, nparam);
795 value_set_si(C->p[r][1+nvar+r], -1);
796 zz2value((*i)->n.power[j][r], C->p[r][1+nvar+nparam]);
798 for (int r = 0; r < nvar; ++r) {
799 value_set_si(C->p[nparam+r][0], 1);
800 Vector_Set(&C->p[nparam+r][1], 0, nvar + nparam + 1);
801 value_set_si(C->p[nparam+r][1+r], 1);
803 Polyhedron *P = Constraints2Polyhedron(C, 0);
804 evalue *E = barvinok_enumerate_ev(P, U, 0);
805 Polyhedron_Free(P);
806 if (EVALUE_IS_ZERO(*E)) {
807 evalue_free(E);
808 continue;
810 zz2value((*i)->n.coeff[j].n, factor.x.n);
811 zz2value((*i)->n.coeff[j].d, factor.d);
812 emul(&factor, E);
814 Matrix_Print(stdout, P_VALUE_FMT, C);
815 char *test[] = { "A", "B", "C", "D", "E", "F", "G" };
816 print_evalue(stdout, E, test);
818 if (!EP)
819 EP = E;
820 else {
821 eadd(E, EP);
822 evalue_free(E);
825 Matrix_Free(C);
826 if (!context)
827 Polyhedron_Free(U);
829 value_clear(factor.d);
830 value_clear(factor.x.n);
831 return EP;
834 ZZ gen_fun::coefficient(Value* params, barvinok_options *options) const
836 if (context && !in_domain(context, params))
837 return ZZ::zero();
839 QQ sum(0, 1);
841 for (short_rat_list::iterator i = term.begin(); i != term.end(); ++i)
842 sum += (*i)->coefficient(params, options);
844 assert(sum.d == 1);
845 return sum.n;
848 void gen_fun::coefficient(Value* params, Value* c) const
850 barvinok_options *options = barvinok_options_new_with_defaults();
852 ZZ coeff = coefficient(params, options);
854 zz2value(coeff, *c);
856 barvinok_options_free(options);
859 gen_fun *gen_fun::summate(int nvar, barvinok_options *options) const
861 int dim = context->Dimension;
862 int nparam = dim - nvar;
863 reducer *red;
864 gen_fun *gf;
866 if (options->incremental_specialization == 1) {
867 red = new partial_ireducer(Polyhedron_Project(context, nparam), dim, nparam);
868 } else
869 red = new partial_reducer(Polyhedron_Project(context, nparam), dim, nparam);
870 for (;;) {
871 try {
872 red->init(context);
873 for (short_rat_list::iterator i = term.begin(); i != term.end(); ++i)
874 red->reduce((*i)->n.coeff, (*i)->n.power, (*i)->d.power);
875 break;
876 } catch (OrthogonalException &e) {
877 red->reset();
880 gf = red->get_gf();
881 delete red;
882 return gf;
885 /* returns true if the set was finite and false otherwise */
886 bool gen_fun::summate(Value *sum) const
888 if (term.size() == 0) {
889 value_set_si(*sum, 0);
890 return true;
893 int maxlen = 0;
894 for (short_rat_list::iterator i = term.begin(); i != term.end(); ++i)
895 if ((*i)->d.power.NumRows() > maxlen)
896 maxlen = (*i)->d.power.NumRows();
898 infinite_icounter cnt((*term.begin())->d.power.NumCols(), maxlen);
899 for (short_rat_list::iterator i = term.begin(); i != term.end(); ++i)
900 cnt.reduce((*i)->n.coeff, (*i)->n.power, (*i)->d.power);
902 for (int i = 1; i <= maxlen; ++i)
903 if (value_notzero_p(mpq_numref(cnt.count[i]))) {
904 value_set_si(*sum, -1);
905 return false;
908 assert(value_one_p(mpq_denref(cnt.count[0])));
909 value_assign(*sum, mpq_numref(cnt.count[0]));
910 return true;