volume.c: allow computation of lower and upper bound volumes
[barvinok.git] / genfun.cc
blobc0a2b73707f42f08cec6dd93cc1bf40be44d2212
1 #include <iostream>
2 #include <iomanip>
3 #include <vector>
4 #include <assert.h>
5 #include "config.h"
6 #include <barvinok/genfun.h>
7 #include <barvinok/barvinok.h>
8 #include "conversion.h"
9 #include "genfun_constructor.h"
10 #include "mat_util.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 #ifndef HAVE_COMPRESS_PARMS
331 static Matrix *compress_parms(Matrix *M, unsigned nparam)
333 assert(0);
335 #endif
337 struct parallel_polytopes {
338 gf_base *red;
339 Polyhedron *context;
340 Matrix *Constraints;
341 Matrix *CP, *T;
342 int dim;
343 int nparam;
344 vector<parallel_cones> cones;
345 barvinok_options *options;
347 parallel_polytopes(int n, Polyhedron *context, int nparam,
348 barvinok_options *options) :
349 context(context), dim(-1), nparam(nparam),
350 options(options) {
351 red = NULL;
352 Constraints = NULL;
353 CP = NULL;
354 T = NULL;
356 bool add(const QQ& c, Polyhedron *P) {
357 int i;
359 for (i = 0; i < P->NbEq; ++i)
360 if (First_Non_Zero(P->Constraint[i]+1,
361 P->Dimension-nparam) == -1)
362 break;
363 if (i < P->NbEq)
364 return false;
366 Polyhedron *Q = remove_equalities_p(Polyhedron_Copy(P), P->Dimension-nparam,
367 NULL);
368 POL_ENSURE_VERTICES(Q);
369 if (emptyQ(Q)) {
370 Polyhedron_Free(Q);
371 return true;
374 if (Q->NbEq != 0) {
375 Polyhedron *R;
376 if (!CP) {
377 Matrix *M;
378 M = Matrix_Alloc(Q->NbEq, Q->Dimension+2);
379 Vector_Copy(Q->Constraint[0], M->p[0], Q->NbEq * (Q->Dimension+2));
380 CP = compress_parms(M, nparam);
381 T = align_matrix(CP, Q->Dimension+1);
382 Matrix_Free(M);
384 R = Polyhedron_Preimage(Q, T, options->MaxRays);
385 Polyhedron_Free(Q);
386 Q = remove_equalities_p(R, R->Dimension-nparam, NULL);
388 assert(Q->NbEq == 0);
390 if (First_Non_Zero(Q->Constraint[Q->NbConstraints-1]+1, Q->Dimension) == -1)
391 Q->NbConstraints--;
393 if (!Constraints) {
394 dim = Q->Dimension;
395 red = gf_base::create(Polyhedron_Copy(context), dim, nparam, options);
396 red->base->init(Q);
397 Constraints = Matrix_Alloc(Q->NbConstraints, Q->Dimension);
398 for (int i = 0; i < Q->NbConstraints; ++i) {
399 Vector_Copy(Q->Constraint[i]+1, Constraints->p[i], Q->Dimension);
401 } else {
402 assert(Q->Dimension == dim);
403 for (int i = 0; i < Q->NbConstraints; ++i) {
404 int j;
405 for (j = 0; j < Constraints->NbRows; ++j)
406 if (Vector_Equal(Q->Constraint[i]+1, Constraints->p[j],
407 Q->Dimension))
408 break;
409 assert(j < Constraints->NbRows);
413 for (int i = 0; i < Q->NbRays; ++i) {
414 if (!value_pos_p(Q->Ray[i][dim+1]))
415 continue;
417 Polyhedron *C = supporting_cone(Q, i);
419 if (First_Non_Zero(C->Constraint[C->NbConstraints-1]+1,
420 C->Dimension) == -1)
421 C->NbConstraints--;
423 int *pos = new int[1+C->NbConstraints];
424 pos[0] = C->NbConstraints;
425 int l = 0;
426 for (int k = 0; k < Constraints->NbRows; ++k) {
427 for (int j = 0; j < C->NbConstraints; ++j) {
428 if (Vector_Equal(C->Constraint[j]+1, Constraints->p[k],
429 C->Dimension)) {
430 pos[1+l++] = k;
431 break;
435 assert(l == C->NbConstraints);
437 int j;
438 for (j = 0; j < cones.size(); ++j)
439 if (!memcmp(pos, cones[j].pos, (1+C->NbConstraints)*sizeof(int)))
440 break;
441 if (j == cones.size())
442 cones.push_back(parallel_cones(pos));
443 else
444 delete [] pos;
446 Polyhedron_Free(C);
448 int k;
449 for (k = 0; k < cones[j].vertices.size(); ++k)
450 if (Vector_Equal(Q->Ray[i]+1, cones[j].vertices[k].first->p,
451 Q->Dimension+1))
452 break;
454 if (k == cones[j].vertices.size()) {
455 Vector *vertex = Vector_Alloc(Q->Dimension+1);
456 Vector_Copy(Q->Ray[i]+1, vertex->p, Q->Dimension+1);
457 cones[j].vertices.push_back(pair<Vector*,QQ>(vertex, c));
458 } else {
459 cones[j].vertices[k].second += c;
460 if (cones[j].vertices[k].second.n == 0) {
461 int size = cones[j].vertices.size();
462 Vector_Free(cones[j].vertices[k].first);
463 if (k < size-1)
464 cones[j].vertices[k] = cones[j].vertices[size-1];
465 cones[j].vertices.pop_back();
470 Polyhedron_Free(Q);
471 return true;
473 gen_fun *compute() {
474 if (!red)
475 return NULL;
476 for (int i = 0; i < cones.size(); ++i) {
477 Matrix *M = Matrix_Alloc(cones[i].pos[0], 1+Constraints->NbColumns+1);
478 Polyhedron *Cone;
479 for (int j = 0; j <cones[i].pos[0]; ++j) {
480 value_set_si(M->p[j][0], 1);
481 Vector_Copy(Constraints->p[cones[i].pos[1+j]], M->p[j]+1,
482 Constraints->NbColumns);
484 Cone = Constraints2Polyhedron(M, options->MaxRays);
485 Matrix_Free(M);
486 for (int j = 0; j < cones[i].vertices.size(); ++j) {
487 red->base->do_vertex_cone(cones[i].vertices[j].second,
488 Polyhedron_Copy(Cone),
489 cones[i].vertices[j].first->p, options);
491 Polyhedron_Free(Cone);
493 if (CP)
494 red->gf->substitute(CP);
495 return red->gf;
497 void print(std::ostream& os) const {
498 for (int i = 0; i < cones.size(); ++i) {
499 os << "[";
500 for (int j = 0; j < cones[i].pos[0]; ++j) {
501 if (j)
502 os << ", ";
503 os << cones[i].pos[1+j];
505 os << "]" << endl;
506 for (int j = 0; j < cones[i].vertices.size(); ++j) {
507 Vector_Print(stderr, P_VALUE_FMT, cones[i].vertices[j].first);
508 os << cones[i].vertices[j].second << endl;
512 ~parallel_polytopes() {
513 for (int i = 0; i < cones.size(); ++i) {
514 delete [] cones[i].pos;
515 for (int j = 0; j < cones[i].vertices.size(); ++j)
516 Vector_Free(cones[i].vertices[j].first);
518 if (Constraints)
519 Matrix_Free(Constraints);
520 if (CP)
521 Matrix_Free(CP);
522 if (T)
523 Matrix_Free(T);
524 delete red;
528 gen_fun *gen_fun::Hadamard_product(const gen_fun *gf, barvinok_options *options)
530 QQ one(1, 1);
531 Polyhedron *C = DomainIntersection(context, gf->context, options->MaxRays);
532 Polyhedron *U = Universe_Polyhedron(C->Dimension);
533 gen_fun *sum = new gen_fun(C);
534 for (short_rat_list::iterator i = term.begin(); i != term.end(); ++i) {
535 for (short_rat_list::iterator i2 = gf->term.begin(); i2 != gf->term.end();
536 ++i2) {
537 int d = (*i)->d.power.NumCols();
538 int k1 = (*i)->d.power.NumRows();
539 int k2 = (*i2)->d.power.NumRows();
540 assert((*i)->d.power.NumCols() == (*i2)->d.power.NumCols());
542 parallel_polytopes pp((*i)->n.power.NumRows() *
543 (*i2)->n.power.NumRows(),
544 sum->context, d, options);
546 for (int j = 0; j < (*i)->n.power.NumRows(); ++j) {
547 for (int j2 = 0; j2 < (*i2)->n.power.NumRows(); ++j2) {
548 Matrix *M = Matrix_Alloc(k1+k2+d+d, 1+k1+k2+d+1);
549 for (int k = 0; k < k1+k2; ++k) {
550 value_set_si(M->p[k][0], 1);
551 value_set_si(M->p[k][1+k], 1);
553 for (int k = 0; k < d; ++k) {
554 value_set_si(M->p[k1+k2+k][1+k1+k2+k], -1);
555 zz2value((*i)->n.power[j][k], M->p[k1+k2+k][1+k1+k2+d]);
556 for (int l = 0; l < k1; ++l)
557 zz2value((*i)->d.power[l][k], M->p[k1+k2+k][1+l]);
559 for (int k = 0; k < d; ++k) {
560 value_set_si(M->p[k1+k2+d+k][1+k1+k2+k], -1);
561 zz2value((*i2)->n.power[j2][k],
562 M->p[k1+k2+d+k][1+k1+k2+d]);
563 for (int l = 0; l < k2; ++l)
564 zz2value((*i2)->d.power[l][k],
565 M->p[k1+k2+d+k][1+k1+l]);
567 Polyhedron *P = Constraints2Polyhedron(M, options->MaxRays);
568 Matrix_Free(M);
570 QQ c = (*i)->n.coeff[j];
571 c *= (*i2)->n.coeff[j2];
572 if (!pp.add(c, P)) {
573 gen_fun *t = barvinok_series_with_options(P, U, options);
574 sum->add(c, t);
575 delete t;
578 Polyhedron_Free(P);
582 gen_fun *t = pp.compute();
583 if (t) {
584 sum->add(one, t);
585 delete t;
589 Polyhedron_Free(U);
590 return sum;
593 void gen_fun::add_union(gen_fun *gf, barvinok_options *options)
595 QQ one(1, 1), mone(-1, 1);
597 gen_fun *hp = Hadamard_product(gf, options);
598 add(one, gf);
599 add(mone, hp);
600 delete hp;
603 static void Polyhedron_Shift(Polyhedron *P, Vector *offset)
605 Value tmp;
606 value_init(tmp);
607 for (int i = 0; i < P->NbConstraints; ++i) {
608 Inner_Product(P->Constraint[i]+1, offset->p, P->Dimension, &tmp);
609 value_subtract(P->Constraint[i][1+P->Dimension],
610 P->Constraint[i][1+P->Dimension], tmp);
612 for (int i = 0; i < P->NbRays; ++i) {
613 if (value_notone_p(P->Ray[i][0]))
614 continue;
615 if (value_zero_p(P->Ray[i][1+P->Dimension]))
616 continue;
617 Vector_Combine(P->Ray[i]+1, offset->p, P->Ray[i]+1,
618 P->Ray[i][0], P->Ray[i][1+P->Dimension], P->Dimension);
620 value_clear(tmp);
623 void gen_fun::shift(const vec_ZZ& offset)
625 for (short_rat_list::iterator i = term.begin(); i != term.end(); ++i)
626 for (int j = 0; j < (*i)->n.power.NumRows(); ++j)
627 (*i)->n.power[j] += offset;
629 Vector *v = Vector_Alloc(offset.length());
630 zz2values(offset, v->p);
631 Polyhedron_Shift(context, v);
632 Vector_Free(v);
635 /* Divide the generating functin by 1/(1-z^power).
636 * The effect on the corresponding explicit function f(x) is
637 * f'(x) = \sum_{i=0}^\infty f(x - i * power)
639 void gen_fun::divide(const vec_ZZ& power)
641 for (short_rat_list::iterator i = term.begin(); i != term.end(); ++i) {
642 int r = (*i)->d.power.NumRows();
643 int c = (*i)->d.power.NumCols();
644 (*i)->d.power.SetDims(r+1, c);
645 (*i)->d.power[r] = power;
648 Vector *v = Vector_Alloc(1+power.length()+1);
649 value_set_si(v->p[0], 1);
650 zz2values(power, v->p+1);
651 Polyhedron *C = AddRays(v->p, 1, context, context->NbConstraints+1);
652 Vector_Free(v);
653 Polyhedron_Free(context);
654 context = C;
657 static void print_power(std::ostream& os, const QQ& c, const vec_ZZ& p,
658 unsigned int nparam, char **param_name)
660 bool first = true;
662 for (int i = 0; i < p.length(); ++i) {
663 if (p[i] == 0)
664 continue;
665 if (first) {
666 if (c.n == -1 && c.d == 1)
667 os << "-";
668 else if (c.n != 1 || c.d != 1) {
669 os << c.n;
670 if (c.d != 1)
671 os << " / " << c.d;
672 os << "*";
674 first = false;
675 } else
676 os << "*";
677 if (i < nparam)
678 os << param_name[i];
679 else
680 os << "x" << i;
681 if (p[i] == 1)
682 continue;
683 if (p[i] < 0)
684 os << "^(" << p[i] << ")";
685 else
686 os << "^" << p[i];
688 if (first) {
689 os << c.n;
690 if (c.d != 1)
691 os << " / " << c.d;
695 void short_rat::print(std::ostream& os, unsigned int nparam, char **param_name) const
697 QQ mone(-1, 1);
698 os << "(";
699 for (int j = 0; j < n.coeff.length(); ++j) {
700 if (j != 0 && n.coeff[j].n > 0)
701 os << "+";
702 print_power(os, n.coeff[j], n.power[j], nparam, param_name);
704 os << ")/(";
705 for (int j = 0; j < d.power.NumRows(); ++j) {
706 if (j != 0)
707 os << " * ";
708 os << "(1";
709 print_power(os, mone, d.power[j], nparam, param_name);
710 os << ")";
712 os << ")";
715 void gen_fun::print(std::ostream& os, unsigned int nparam, char **param_name) const
717 for (short_rat_list::iterator i = term.begin(); i != term.end(); ++i) {
718 if (i != term.begin())
719 os << " + ";
720 (*i)->print(os, nparam, param_name);
724 std::ostream & operator<< (std::ostream & os, const short_rat& r)
726 os << r.n.coeff << endl;
727 os << r.n.power << endl;
728 os << r.d.power << endl;
729 return os;
732 std::ostream & operator<< (std::ostream & os, const Polyhedron& P)
734 char *str;
735 void (*gmp_free)(void *, size_t);
736 mp_get_memory_functions(NULL, NULL, &gmp_free);
737 os << P.NbConstraints << " " << P.Dimension+2 << endl;
738 for (int i = 0; i < P.NbConstraints; ++i) {
739 for (int j = 0; j < P.Dimension+2; ++j) {
740 str = mpz_get_str(0, 10, P.Constraint[i][j]);
741 os << std::setw(4) << str << " ";
742 (*gmp_free)(str, strlen(str)+1);
744 os << endl;
746 return os;
749 std::ostream & operator<< (std::ostream & os, const gen_fun& gf)
751 os << *gf.context << endl;
752 os << endl;
753 os << gf.term.size() << endl;
754 for (short_rat_list::iterator i = gf.term.begin(); i != gf.term.end(); ++i)
755 os << **i;
756 return os;
759 static Matrix *Matrix_Read(std::istream& is)
761 Matrix *M;
762 int r, c;
763 ZZ tmp;
765 is >> r >> c;
766 M = Matrix_Alloc(r, c);
767 for (int i = 0; i < r; ++i)
768 for (int j = 0; j < c; ++j) {
769 is >> tmp;
770 zz2value(tmp, M->p[i][j]);
772 return M;
775 gen_fun *gen_fun::read(std::istream& is, barvinok_options *options)
777 Matrix *M = Matrix_Read(is);
778 Polyhedron *C = Constraints2Polyhedron(M, options->MaxRays);
779 Matrix_Free(M);
781 gen_fun *gf = new gen_fun(C);
783 int n;
784 is >> n;
786 vec_QQ c;
787 mat_ZZ num;
788 mat_ZZ den;
789 for (int i = 0; i < n; ++i) {
790 is >> c >> num >> den;
791 gf->add(new short_rat(c, num, den));
794 return gf;
797 gen_fun::operator evalue *() const
799 evalue *EP = NULL;
800 evalue factor;
801 value_init(factor.d);
802 value_init(factor.x.n);
803 for (short_rat_list::iterator i = term.begin(); i != term.end(); ++i) {
804 unsigned nvar = (*i)->d.power.NumRows();
805 unsigned nparam = (*i)->d.power.NumCols();
806 Matrix *C = Matrix_Alloc(nparam + nvar, 1 + nvar + nparam + 1);
807 mat_ZZ& d = (*i)->d.power;
808 Polyhedron *U = context ? context : Universe_Polyhedron(nparam);
810 for (int j = 0; j < (*i)->n.coeff.length(); ++j) {
811 for (int r = 0; r < nparam; ++r) {
812 value_set_si(C->p[r][0], 0);
813 for (int c = 0; c < nvar; ++c) {
814 zz2value(d[c][r], C->p[r][1+c]);
816 Vector_Set(&C->p[r][1+nvar], 0, nparam);
817 value_set_si(C->p[r][1+nvar+r], -1);
818 zz2value((*i)->n.power[j][r], C->p[r][1+nvar+nparam]);
820 for (int r = 0; r < nvar; ++r) {
821 value_set_si(C->p[nparam+r][0], 1);
822 Vector_Set(&C->p[nparam+r][1], 0, nvar + nparam + 1);
823 value_set_si(C->p[nparam+r][1+r], 1);
825 Polyhedron *P = Constraints2Polyhedron(C, 0);
826 evalue *E = barvinok_enumerate_ev(P, U, 0);
827 Polyhedron_Free(P);
828 if (EVALUE_IS_ZERO(*E)) {
829 free_evalue_refs(E);
830 free(E);
831 continue;
833 zz2value((*i)->n.coeff[j].n, factor.x.n);
834 zz2value((*i)->n.coeff[j].d, factor.d);
835 emul(&factor, E);
837 Matrix_Print(stdout, P_VALUE_FMT, C);
838 char *test[] = { "A", "B", "C", "D", "E", "F", "G" };
839 print_evalue(stdout, E, test);
841 if (!EP)
842 EP = E;
843 else {
844 eadd(E, EP);
845 free_evalue_refs(E);
846 free(E);
849 Matrix_Free(C);
850 if (!context)
851 Polyhedron_Free(U);
853 value_clear(factor.d);
854 value_clear(factor.x.n);
855 return EP;
858 ZZ gen_fun::coefficient(Value* params, barvinok_options *options) const
860 if (context && !in_domain(context, params))
861 return ZZ::zero();
863 QQ sum(0, 1);
865 for (short_rat_list::iterator i = term.begin(); i != term.end(); ++i)
866 sum += (*i)->coefficient(params, options);
868 assert(sum.d == 1);
869 return sum.n;
872 void gen_fun::coefficient(Value* params, Value* c) const
874 barvinok_options *options = barvinok_options_new_with_defaults();
876 ZZ coeff = coefficient(params, options);
878 zz2value(coeff, *c);
880 barvinok_options_free(options);
883 gen_fun *gen_fun::summate(int nvar, barvinok_options *options) const
885 int dim = context->Dimension;
886 int nparam = dim - nvar;
887 reducer *red;
888 gen_fun *gf;
890 if (options->incremental_specialization == 1) {
891 red = new partial_ireducer(Polyhedron_Project(context, nparam), dim, nparam);
892 } else
893 red = new partial_reducer(Polyhedron_Project(context, nparam), dim, nparam);
894 for (;;) {
895 try {
896 red->init(context);
897 for (short_rat_list::iterator i = term.begin(); i != term.end(); ++i)
898 red->reduce((*i)->n.coeff, (*i)->n.power, (*i)->d.power);
899 break;
900 } catch (OrthogonalException &e) {
901 red->reset();
904 gf = red->get_gf();
905 delete red;
906 return gf;
909 /* returns true if the set was finite and false otherwise */
910 bool gen_fun::summate(Value *sum) const
912 if (term.size() == 0) {
913 value_set_si(*sum, 0);
914 return true;
917 int maxlen = 0;
918 for (short_rat_list::iterator i = term.begin(); i != term.end(); ++i)
919 if ((*i)->d.power.NumRows() > maxlen)
920 maxlen = (*i)->d.power.NumRows();
922 infinite_icounter cnt((*term.begin())->d.power.NumCols(), maxlen);
923 for (short_rat_list::iterator i = term.begin(); i != term.end(); ++i)
924 cnt.reduce((*i)->n.coeff, (*i)->n.power, (*i)->d.power);
926 for (int i = 1; i <= maxlen; ++i)
927 if (value_notzero_p(mpq_numref(cnt.count[i]))) {
928 value_set_si(*sum, -1);
929 return false;
932 assert(value_one_p(mpq_denref(cnt.count[0])));
933 value_assign(*sum, mpq_numref(cnt.count[0]));
934 return true;