5 #include <barvinok/genfun.h>
6 #include <barvinok/barvinok.h>
7 #include "conversion.h"
9 #include "genfun_constructor.h"
11 #include "matrix_read.h"
12 #include "remove_equalities.h"
20 bool short_rat_lex_smaller_denominator::operator()(const short_rat
* r1
,
21 const short_rat
* r2
) const
23 return lex_cmp(r1
->d
.power
, r2
->d
.power
) < 0;
26 static void lex_order_terms(struct short_rat
* rat
)
28 for (int i
= 0; i
< rat
->n
.power
.NumRows(); ++i
) {
30 for (int j
= i
+1; j
< rat
->n
.power
.NumRows(); ++j
)
31 if (lex_cmp(rat
->n
.power
[j
], rat
->n
.power
[m
]) < 0)
34 vec_ZZ tmp
= rat
->n
.power
[m
];
35 rat
->n
.power
[m
] = rat
->n
.power
[i
];
36 rat
->n
.power
[i
] = tmp
;
37 QQ tmp_coeff
= rat
->n
.coeff
[m
];
38 rat
->n
.coeff
[m
] = rat
->n
.coeff
[i
];
39 rat
->n
.coeff
[i
] = tmp_coeff
;
44 short_rat::short_rat(const short_rat
& r
)
51 short_rat::short_rat(Value c
)
54 value2zz(c
, n
.coeff
[0].n
);
56 n
.power
.SetDims(1, 0);
57 d
.power
.SetDims(0, 0);
60 short_rat::short_rat(const QQ
& c
, const vec_ZZ
& num
, const mat_ZZ
& den
)
66 n
.power
.SetDims(1, num
.length());
72 short_rat::short_rat(const vec_QQ
& c
, const mat_ZZ
& num
, const mat_ZZ
& den
)
80 void short_rat::normalize()
82 /* Make all powers in denominator reverse-lexico-positive */
83 for (int i
= 0; i
< d
.power
.NumRows(); ++i
) {
85 for (j
= d
.power
.NumCols()-1; j
>= 0; --j
)
86 if (!IsZero(d
.power
[i
][j
]))
89 if (sign(d
.power
[i
][j
]) < 0) {
90 negate(d
.power
[i
], d
.power
[i
]);
91 for (int k
= 0; k
< n
.coeff
.length(); ++k
) {
92 negate(n
.coeff
[k
].n
, n
.coeff
[k
].n
);
93 n
.power
[k
] += d
.power
[i
];
98 /* Order powers in denominator */
99 lex_order_rows(d
.power
);
102 void short_rat::add(const short_rat
*r
)
104 for (int i
= 0; i
< r
->n
.power
.NumRows(); ++i
) {
105 int len
= n
.coeff
.length();
107 for (j
= 0; j
< len
; ++j
)
108 if (r
->n
.power
[i
] == n
.power
[j
])
111 n
.coeff
[j
] += r
->n
.coeff
[i
];
112 if (n
.coeff
[j
].n
== 0) {
114 n
.power
[j
] = n
.power
[len
-1];
115 n
.coeff
[j
] = n
.coeff
[len
-1];
117 int dim
= n
.power
.NumCols();
118 n
.coeff
.SetLength(len
-1);
119 n
.power
.SetDims(len
-1, dim
);
122 int dim
= n
.power
.NumCols();
123 n
.coeff
.SetLength(len
+1);
124 n
.power
.SetDims(len
+1, dim
);
125 n
.coeff
[len
] = r
->n
.coeff
[i
];
126 n
.power
[len
] = r
->n
.power
[i
];
131 QQ
short_rat::coefficient(Value
* params
, barvinok_options
*options
) const
133 unsigned nvar
= d
.power
.NumRows();
134 unsigned nparam
= d
.power
.NumCols();
135 Matrix
*C
= Matrix_Alloc(nparam
+ nvar
, 1 + nvar
+ 1);
141 for (int j
= 0; j
< n
.coeff
.length(); ++j
) {
142 C
->NbRows
= nparam
+nvar
;
143 for (int r
= 0; r
< nparam
; ++r
) {
144 value_set_si(C
->p
[r
][0], 0);
145 for (int c
= 0; c
< nvar
; ++c
) {
146 zz2value(d
.power
[c
][r
], C
->p
[r
][1+c
]);
148 zz2value(n
.power
[j
][r
], C
->p
[r
][1+nvar
]);
149 value_subtract(C
->p
[r
][1+nvar
], C
->p
[r
][1+nvar
], params
[r
]);
151 for (int r
= 0; r
< nvar
; ++r
) {
152 value_set_si(C
->p
[nparam
+r
][0], 1);
153 Vector_Set(&C
->p
[nparam
+r
][1], 0, nvar
+ 1);
154 value_set_si(C
->p
[nparam
+r
][1+r
], 1);
156 Polyhedron
*P
= Constraints2Polyhedron(C
, options
->MaxRays
);
161 barvinok_count_with_options(P
, &tmp
, options
);
163 if (value_zero_p(tmp
))
175 bool short_rat::reduced()
177 int dim
= n
.power
.NumCols();
178 lex_order_terms(this);
179 if (n
.power
.NumRows() % 2 == 0) {
180 if (n
.coeff
[0].n
== -n
.coeff
[1].n
&&
181 n
.coeff
[0].d
== n
.coeff
[1].d
) {
182 vec_ZZ step
= n
.power
[1] - n
.power
[0];
184 for (k
= 1; k
< n
.power
.NumRows()/2; ++k
) {
185 if (n
.coeff
[2*k
].n
!= -n
.coeff
[2*k
+1].n
||
186 n
.coeff
[2*k
].d
!= n
.coeff
[2*k
+1].d
)
188 if (step
!= n
.power
[2*k
+1] - n
.power
[2*k
])
191 if (k
== n
.power
.NumRows()/2) {
192 for (k
= 0; k
< d
.power
.NumRows(); ++k
)
193 if (d
.power
[k
] == step
)
195 if (k
< d
.power
.NumRows()) {
196 for (++k
; k
< d
.power
.NumRows(); ++k
)
197 d
.power
[k
-1] = d
.power
[k
];
198 d
.power
.SetDims(k
-1, dim
);
199 for (k
= 1; k
< n
.power
.NumRows()/2; ++k
) {
200 n
.coeff
[k
] = n
.coeff
[2*k
];
201 n
.power
[k
] = n
.power
[2*k
];
203 n
.coeff
.SetLength(k
);
204 n
.power
.SetDims(k
, dim
);
213 gen_fun::gen_fun(Value c
)
215 short_rat
*r
= new short_rat(c
);
216 context
= Universe_Polyhedron(0);
220 void gen_fun::add(const QQ
& c
, const vec_ZZ
& num
, const mat_ZZ
& den
)
225 add(new short_rat(c
, num
, den
));
228 void gen_fun::add(short_rat
*r
)
230 short_rat_list::iterator i
= term
.find(r
);
231 while (i
!= term
.end()) {
233 if ((*i
)->n
.coeff
.length() == 0) {
236 } else if ((*i
)->reduced()) {
238 /* we've modified term[i], so remove it
239 * and add it back again
253 void gen_fun::add(const QQ
& c
, const gen_fun
*gf
, barvinok_options
*options
)
255 Polyhedron
*U
= DomainUnion(context
, gf
->context
, options
->MaxRays
);
256 Polyhedron
*C
= DomainConvex(U
, options
->MaxRays
);
258 Domain_Free(context
);
264 void gen_fun::add(const QQ
& c
, const gen_fun
*gf
)
267 for (short_rat_list::iterator i
= gf
->term
.begin(); i
!= gf
->term
.end(); ++i
) {
268 for (int j
= 0; j
< (*i
)->n
.power
.NumRows(); ++j
) {
270 p
*= (*i
)->n
.coeff
[j
];
271 add(p
, (*i
)->n
.power
[j
], (*i
)->d
.power
);
276 static void split_param_compression(Matrix
*CP
, mat_ZZ
& map
, vec_ZZ
& offset
)
278 Matrix
*T
= Transpose(CP
);
279 matrix2zz(T
, map
, T
->NbRows
-1, T
->NbColumns
-1);
280 values2zz(T
->p
[T
->NbRows
-1], offset
, T
->NbColumns
-1);
285 * Perform the substitution specified by CP
287 * CP is a homogeneous matrix that maps a set of "compressed parameters"
288 * to the original set of parameters.
290 * This function is applied to a gen_fun computed with the compressed parameters
291 * and adapts it to refer to the original parameters.
293 * That is, if y are the compressed parameters and x = A y + b are the original
294 * parameters, then we want the coefficient of the monomial t^y in the original
295 * generating function to be the coefficient of the monomial u^x in the resulting
296 * generating function.
297 * The original generating function has the form
299 * a t^m/(1-t^n) = a t^m + a t^{m+n} + a t^{m+2n} + ...
301 * Since each term t^y should correspond to a term u^x, with x = A y + b, we want
303 * a u^{A m + b} + a u^{A (m+n) + b} + a u^{A (m+2n) +b} + ... =
305 * = a u^{A m + b}/(1-u^{A n})
307 * Therefore, we multiply the powers m and n in both numerator and denominator by A
308 * and add b to the power in the numerator.
309 * Since the above powers are stored as row vectors m^T and n^T,
310 * we compute, say, m'^T = m^T A^T to obtain m' = A m.
312 * The pair (map, offset) contains the same information as CP.
313 * map is the transpose of the linear part of CP, while offset is the constant part.
315 void gen_fun::substitute(Matrix
*CP
)
319 split_param_compression(CP
, map
, offset
);
320 Polyhedron
*C
= Polyhedron_Image(context
, CP
, 0);
321 Polyhedron_Free(context
);
324 short_rat_list new_term
;
325 for (short_rat_list::iterator i
= term
.begin(); i
!= term
.end(); ++i
) {
329 for (int j
= 0; j
< r
->n
.power
.NumRows(); ++j
)
330 r
->n
.power
[j
] += offset
;
337 static int Matrix_Equal(Matrix
*M1
, Matrix
*M2
)
341 if (M1
->NbRows
!= M2
->NbRows
)
343 if (M1
->NbColumns
!= M2
->NbColumns
)
345 for (i
= 0; i
< M1
->NbRows
; ++i
)
346 for (j
= 0; j
< M1
->NbColumns
; ++j
)
347 if (value_ne(M1
->p
[i
][j
], M2
->p
[i
][j
]))
353 struct parallel_cones
{
355 vector
<pair
<Vector
*, QQ
> > vertices
;
356 parallel_cones(int *pos
) : pos(pos
) {}
359 /* This structure helps in computing the generating functions
360 * of polytopes with pairwise parallel hyperplanes more efficiently.
361 * These occur when computing hadamard products of pairs of generating
362 * functions with the same denominators.
363 * If there are many such pairs then the same vertex cone
364 * may appear more than once. We therefore keep a list of all
365 * vertex cones and only compute the corresponding generating function
367 * However, even HPs of generating functions with the same denominators
368 * can result in polytopes of different "shapes", making them incomparable.
369 * In particular, they can have different equalities among the parameters
370 * and the variables. In such cases, only polytopes of the first "shape"
371 * that is encountered are kept in this way. The others are handled
372 * in the usual, non-optimized way.
374 struct parallel_polytopes
{
381 unsigned reduced_nparam
;
382 vector
<parallel_cones
> cones
;
383 barvinok_options
*options
;
385 parallel_polytopes(int n
, Polyhedron
*context
, int nparam
,
386 barvinok_options
*options
) :
387 context(context
), dim(-1), nparam(nparam
),
394 bool add(const QQ
& c
, Polyhedron
*P
) {
397 for (i
= 0; i
< P
->NbEq
; ++i
)
398 if (First_Non_Zero(P
->Constraint
[i
]+1,
399 P
->Dimension
-nparam
) == -1)
404 Polyhedron
*Q
= remove_equalities_p(Polyhedron_Copy(P
), P
->Dimension
-nparam
,
405 NULL
, options
->MaxRays
);
406 POL_ENSURE_VERTICES(Q
);
412 if (Q
->Dimension
== 0) {
421 remove_all_equalities(&Q
, NULL
, &Q_CP
, NULL
, nparam
,
424 POL_ENSURE_VERTICES(Q
);
425 if (emptyQ(Q
) || Q
->NbEq
> 0 || Q
->Dimension
== 0) {
434 if ((!CP
^ !Q_CP
) || (CP
&& !Matrix_Equal(CP
, Q_CP
))) {
443 T
= align_matrix(CP
, R
->Dimension
+1);
446 reduced_nparam
= CP
->NbColumns
-1;
453 reduced_nparam
= nparam
;
456 if (First_Non_Zero(Q
->Constraint
[Q
->NbConstraints
-1]+1, Q
->Dimension
) == -1)
460 Polyhedron
*reduced_context
;
463 reduced_context
= Polyhedron_Preimage(context
, CP
, options
->MaxRays
);
465 reduced_context
= Polyhedron_Copy(context
);
466 red
= gf_base::create(reduced_context
, dim
, reduced_nparam
, options
);
467 red
->base
->init(Q
, 0);
468 Constraints
= Matrix_Alloc(Q
->NbConstraints
, Q
->Dimension
);
469 for (int i
= 0; i
< Q
->NbConstraints
; ++i
) {
470 Vector_Copy(Q
->Constraint
[i
]+1, Constraints
->p
[i
], Q
->Dimension
);
473 if (Q
->Dimension
!= dim
) {
477 assert(Q
->Dimension
== dim
);
478 for (int i
= 0; i
< Q
->NbConstraints
; ++i
) {
480 for (j
= 0; j
< Constraints
->NbRows
; ++j
)
481 if (Vector_Equal(Q
->Constraint
[i
]+1, Constraints
->p
[j
],
484 if (j
>= Constraints
->NbRows
) {
485 Matrix_Extend(Constraints
, Constraints
->NbRows
+1);
486 Vector_Copy(Q
->Constraint
[i
]+1,
487 Constraints
->p
[Constraints
->NbRows
-1],
493 for (int i
= 0; i
< Q
->NbRays
; ++i
) {
494 if (!value_pos_p(Q
->Ray
[i
][dim
+1]))
497 Polyhedron
*C
= supporting_cone(Q
, i
);
499 if (First_Non_Zero(C
->Constraint
[C
->NbConstraints
-1]+1,
503 int *pos
= new int[1+C
->NbConstraints
];
505 for (int k
= 0; k
< Constraints
->NbRows
; ++k
) {
506 for (int j
= 0; j
< C
->NbConstraints
; ++j
) {
507 if (Vector_Equal(C
->Constraint
[j
]+1, Constraints
->p
[k
],
517 for (j
= 0; j
< cones
.size(); ++j
)
518 if (!memcmp(pos
, cones
[j
].pos
, (1+C
->NbConstraints
)*sizeof(int)))
520 if (j
== cones
.size())
521 cones
.push_back(parallel_cones(pos
));
528 for (k
= 0; k
< cones
[j
].vertices
.size(); ++k
)
529 if (Vector_Equal(Q
->Ray
[i
]+1, cones
[j
].vertices
[k
].first
->p
,
533 if (k
== cones
[j
].vertices
.size()) {
534 Vector
*vertex
= Vector_Alloc(Q
->Dimension
+1);
535 Vector_Copy(Q
->Ray
[i
]+1, vertex
->p
, Q
->Dimension
+1);
536 cones
[j
].vertices
.push_back(pair
<Vector
*,QQ
>(vertex
, c
));
538 cones
[j
].vertices
[k
].second
+= c
;
539 if (cones
[j
].vertices
[k
].second
.n
== 0) {
540 int size
= cones
[j
].vertices
.size();
541 Vector_Free(cones
[j
].vertices
[k
].first
);
543 cones
[j
].vertices
[k
] = cones
[j
].vertices
[size
-1];
544 cones
[j
].vertices
.pop_back();
555 for (int i
= 0; i
< cones
.size(); ++i
) {
556 Matrix
*M
= Matrix_Alloc(cones
[i
].pos
[0], 1+Constraints
->NbColumns
+1);
558 for (int j
= 0; j
<cones
[i
].pos
[0]; ++j
) {
559 value_set_si(M
->p
[j
][0], 1);
560 Vector_Copy(Constraints
->p
[cones
[i
].pos
[1+j
]], M
->p
[j
]+1,
561 Constraints
->NbColumns
);
563 Cone
= Constraints2Polyhedron(M
, options
->MaxRays
);
565 for (int j
= 0; j
< cones
[i
].vertices
.size(); ++j
) {
566 red
->base
->do_vertex_cone(cones
[i
].vertices
[j
].second
,
567 Polyhedron_Copy(Cone
),
568 cones
[i
].vertices
[j
].first
->p
, options
);
570 Polyhedron_Free(Cone
);
573 red
->gf
->substitute(CP
);
576 void print(std::ostream
& os
) const {
577 for (int i
= 0; i
< cones
.size(); ++i
) {
579 for (int j
= 0; j
< cones
[i
].pos
[0]; ++j
) {
582 os
<< cones
[i
].pos
[1+j
];
585 for (int j
= 0; j
< cones
[i
].vertices
.size(); ++j
) {
586 Vector_Print(stderr
, P_VALUE_FMT
, cones
[i
].vertices
[j
].first
);
587 os
<< cones
[i
].vertices
[j
].second
<< endl
;
591 ~parallel_polytopes() {
592 for (int i
= 0; i
< cones
.size(); ++i
) {
593 delete [] cones
[i
].pos
;
594 for (int j
= 0; j
< cones
[i
].vertices
.size(); ++j
)
595 Vector_Free(cones
[i
].vertices
[j
].first
);
598 Matrix_Free(Constraints
);
607 gen_fun
*gen_fun::Hadamard_product(const gen_fun
*gf
, barvinok_options
*options
)
610 Polyhedron
*C
= DomainIntersection(context
, gf
->context
, options
->MaxRays
);
611 gen_fun
*sum
= new gen_fun(C
);
614 for (short_rat_list::iterator i
= term
.begin(); i
!= term
.end(); ++i
, j
++) {
616 for (short_rat_list::iterator i2
= gf
->term
.begin();
617 i2
!= gf
->term
.end();
619 int d
= (*i
)->d
.power
.NumCols();
620 int k1
= (*i
)->d
.power
.NumRows();
621 int k2
= (*i2
)->d
.power
.NumRows();
622 assert((*i
)->d
.power
.NumCols() == (*i2
)->d
.power
.NumCols());
624 if (options
->verbose
)
625 fprintf(stderr
, "HP: %d/%zd %d/%zd \r",
626 j
, term
.size(), k
, gf
->term
.size());
628 parallel_polytopes
pp((*i
)->n
.power
.NumRows() *
629 (*i2
)->n
.power
.NumRows(),
630 sum
->context
, d
, options
);
632 for (int j
= 0; j
< (*i
)->n
.power
.NumRows(); ++j
) {
633 for (int j2
= 0; j2
< (*i2
)->n
.power
.NumRows(); ++j2
) {
634 Matrix
*M
= Matrix_Alloc(k1
+k2
+d
+d
, 1+k1
+k2
+d
+1);
635 for (int k
= 0; k
< k1
+k2
; ++k
) {
636 value_set_si(M
->p
[k
][0], 1);
637 value_set_si(M
->p
[k
][1+k
], 1);
639 for (int k
= 0; k
< d
; ++k
) {
640 value_set_si(M
->p
[k1
+k2
+k
][1+k1
+k2
+k
], -1);
641 zz2value((*i
)->n
.power
[j
][k
], M
->p
[k1
+k2
+k
][1+k1
+k2
+d
]);
642 for (int l
= 0; l
< k1
; ++l
)
643 zz2value((*i
)->d
.power
[l
][k
], M
->p
[k1
+k2
+k
][1+l
]);
645 for (int k
= 0; k
< d
; ++k
) {
646 value_set_si(M
->p
[k1
+k2
+d
+k
][1+k1
+k2
+k
], -1);
647 zz2value((*i2
)->n
.power
[j2
][k
],
648 M
->p
[k1
+k2
+d
+k
][1+k1
+k2
+d
]);
649 for (int l
= 0; l
< k2
; ++l
)
650 zz2value((*i2
)->d
.power
[l
][k
],
651 M
->p
[k1
+k2
+d
+k
][1+k1
+l
]);
653 Polyhedron
*P
= Constraints2Polyhedron(M
, options
->MaxRays
);
656 QQ c
= (*i
)->n
.coeff
[j
];
657 c
*= (*i2
)->n
.coeff
[j2
];
659 gen_fun
*t
= barvinok_enumerate_series(P
, C
->Dimension
, options
);
668 gen_fun
*t
= pp
.compute();
678 void gen_fun::add_union(gen_fun
*gf
, barvinok_options
*options
)
680 QQ
one(1, 1), mone(-1, 1);
682 gen_fun
*hp
= Hadamard_product(gf
, options
);
688 static void Polyhedron_Shift(Polyhedron
*P
, Vector
*offset
)
692 for (int i
= 0; i
< P
->NbConstraints
; ++i
) {
693 Inner_Product(P
->Constraint
[i
]+1, offset
->p
, P
->Dimension
, &tmp
);
694 value_subtract(P
->Constraint
[i
][1+P
->Dimension
],
695 P
->Constraint
[i
][1+P
->Dimension
], tmp
);
697 for (int i
= 0; i
< P
->NbRays
; ++i
) {
698 if (value_notone_p(P
->Ray
[i
][0]))
700 if (value_zero_p(P
->Ray
[i
][1+P
->Dimension
]))
702 Vector_Combine(P
->Ray
[i
]+1, offset
->p
, P
->Ray
[i
]+1,
703 P
->Ray
[i
][0], P
->Ray
[i
][1+P
->Dimension
], P
->Dimension
);
708 void gen_fun::shift(const vec_ZZ
& offset
)
710 for (short_rat_list::iterator i
= term
.begin(); i
!= term
.end(); ++i
)
711 for (int j
= 0; j
< (*i
)->n
.power
.NumRows(); ++j
)
712 (*i
)->n
.power
[j
] += offset
;
714 Vector
*v
= Vector_Alloc(offset
.length());
715 zz2values(offset
, v
->p
);
716 Polyhedron_Shift(context
, v
);
720 /* Divide the generating functin by 1/(1-z^power).
721 * The effect on the corresponding explicit function f(x) is
722 * f'(x) = \sum_{i=0}^\infty f(x - i * power)
724 void gen_fun::divide(const vec_ZZ
& power
)
726 for (short_rat_list::iterator i
= term
.begin(); i
!= term
.end(); ++i
) {
727 int r
= (*i
)->d
.power
.NumRows();
728 int c
= (*i
)->d
.power
.NumCols();
729 (*i
)->d
.power
.SetDims(r
+1, c
);
730 (*i
)->d
.power
[r
] = power
;
733 Vector
*v
= Vector_Alloc(1+power
.length()+1);
734 value_set_si(v
->p
[0], 1);
735 zz2values(power
, v
->p
+1);
736 Polyhedron
*C
= AddRays(v
->p
, 1, context
, context
->NbConstraints
+1);
738 Polyhedron_Free(context
);
742 static void print_power(std::ostream
& os
, const QQ
& c
, const vec_ZZ
& p
,
743 unsigned int nparam
, const char **param_name
)
747 for (int i
= 0; i
< p
.length(); ++i
) {
751 if (c
.n
== -1 && c
.d
== 1)
753 else if (c
.n
!= 1 || c
.d
!= 1) {
769 os
<< "^(" << p
[i
] << ")";
780 void short_rat::print(std::ostream
& os
, unsigned int nparam
,
781 const char **param_name
) const
785 for (int j
= 0; j
< n
.coeff
.length(); ++j
) {
786 if (j
!= 0 && n
.coeff
[j
].n
>= 0)
788 print_power(os
, n
.coeff
[j
], n
.power
[j
], nparam
, param_name
);
791 if (d
.power
.NumRows() == 0)
794 for (int j
= 0; j
< d
.power
.NumRows(); ++j
) {
798 print_power(os
, mone
, d
.power
[j
], nparam
, param_name
);
804 void gen_fun::print(std::ostream
& os
, unsigned int nparam
,
805 const char **param_name
) const
807 for (short_rat_list::iterator i
= term
.begin(); i
!= term
.end(); ++i
) {
808 if (i
!= term
.begin())
810 (*i
)->print(os
, nparam
, param_name
);
814 std::ostream
& operator<< (std::ostream
& os
, const short_rat
& r
)
816 os
<< r
.n
.coeff
<< endl
;
817 os
<< r
.n
.power
<< endl
;
818 os
<< r
.d
.power
<< endl
;
822 extern "C" { typedef void (*gmp_free_t
)(void *, size_t); }
824 std::ostream
& operator<< (std::ostream
& os
, const Polyhedron
& P
)
828 mp_get_memory_functions(NULL
, NULL
, &gmp_free
);
829 os
<< P
.NbConstraints
<< " " << P
.Dimension
+2 << endl
;
830 for (int i
= 0; i
< P
.NbConstraints
; ++i
) {
831 for (int j
= 0; j
< P
.Dimension
+2; ++j
) {
832 str
= mpz_get_str(0, 10, P
.Constraint
[i
][j
]);
833 os
<< std::setw(4) << str
<< " ";
834 (*gmp_free
)(str
, strlen(str
)+1);
841 std::ostream
& operator<< (std::ostream
& os
, const gen_fun
& gf
)
843 os
<< *gf
.context
<< endl
;
845 os
<< gf
.term
.size() << endl
;
846 for (short_rat_list::iterator i
= gf
.term
.begin(); i
!= gf
.term
.end(); ++i
)
851 gen_fun
*gen_fun::read(std::istream
& is
, barvinok_options
*options
)
853 Matrix
*M
= Matrix_Read(is
);
854 Polyhedron
*C
= Constraints2Polyhedron(M
, options
->MaxRays
);
857 gen_fun
*gf
= new gen_fun(C
);
865 for (int i
= 0; i
< n
; ++i
) {
866 is
>> c
>> num
>> den
;
867 gf
->add(new short_rat(c
, num
, den
));
873 gen_fun::operator evalue
*() const
877 value_init(factor
.d
);
878 value_init(factor
.x
.n
);
879 for (short_rat_list::iterator i
= term
.begin(); i
!= term
.end(); ++i
) {
880 unsigned nvar
= (*i
)->d
.power
.NumRows();
881 unsigned nparam
= (*i
)->d
.power
.NumCols();
882 Matrix
*C
= Matrix_Alloc(nparam
+ nvar
, 1 + nvar
+ nparam
+ 1);
883 mat_ZZ
& d
= (*i
)->d
.power
;
884 Polyhedron
*U
= context
;
886 for (int j
= 0; j
< (*i
)->n
.coeff
.length(); ++j
) {
887 for (int r
= 0; r
< nparam
; ++r
) {
888 value_set_si(C
->p
[r
][0], 0);
889 for (int c
= 0; c
< nvar
; ++c
) {
890 zz2value(d
[c
][r
], C
->p
[r
][1+c
]);
892 Vector_Set(&C
->p
[r
][1+nvar
], 0, nparam
);
893 value_set_si(C
->p
[r
][1+nvar
+r
], -1);
894 zz2value((*i
)->n
.power
[j
][r
], C
->p
[r
][1+nvar
+nparam
]);
896 for (int r
= 0; r
< nvar
; ++r
) {
897 value_set_si(C
->p
[nparam
+r
][0], 1);
898 Vector_Set(&C
->p
[nparam
+r
][1], 0, nvar
+ nparam
+ 1);
899 value_set_si(C
->p
[nparam
+r
][1+r
], 1);
901 Polyhedron
*P
= Constraints2Polyhedron(C
, 0);
902 evalue
*E
= barvinok_enumerate_ev(P
, U
, 0);
904 if (EVALUE_IS_ZERO(*E
)) {
908 zz2value((*i
)->n
.coeff
[j
].n
, factor
.x
.n
);
909 zz2value((*i
)->n
.coeff
[j
].d
, factor
.d
);
920 value_clear(factor
.d
);
921 value_clear(factor
.x
.n
);
922 return EP
? EP
: evalue_zero();
925 ZZ
gen_fun::coefficient(Value
* params
, barvinok_options
*options
) const
927 if (!in_domain(context
, params
))
932 for (short_rat_list::iterator i
= term
.begin(); i
!= term
.end(); ++i
)
933 sum
+= (*i
)->coefficient(params
, options
);
939 void gen_fun::coefficient(Value
* params
, Value
* c
) const
941 barvinok_options
*options
= barvinok_options_new_with_defaults();
943 ZZ coeff
= coefficient(params
, options
);
947 barvinok_options_free(options
);
950 gen_fun
*gen_fun::summate(int nvar
, barvinok_options
*options
) const
952 int dim
= context
->Dimension
;
953 int nparam
= dim
- nvar
;
961 finite
= summate(&c
);
968 if (options
->incremental_specialization
== 1) {
969 red
= new partial_ireducer(Polyhedron_Project(context
, nparam
), dim
, nparam
);
971 red
= new partial_reducer(Polyhedron_Project(context
, nparam
), dim
, nparam
);
975 red
->init(context
, n_try
);
976 for (short_rat_list::iterator i
= term
.begin(); i
!= term
.end(); ++i
)
977 red
->reduce((*i
)->n
.coeff
, (*i
)->n
.power
, (*i
)->d
.power
);
979 } catch (OrthogonalException
&e
) {
989 /* returns true if the set was finite and false otherwise */
990 bool gen_fun::summate(Value
*sum
) const
992 if (term
.size() == 0) {
993 value_set_si(*sum
, 0);
998 for (short_rat_list::iterator i
= term
.begin(); i
!= term
.end(); ++i
)
999 if ((*i
)->d
.power
.NumRows() > maxlen
)
1000 maxlen
= (*i
)->d
.power
.NumRows();
1002 infinite_counter
cnt((*term
.begin())->d
.power
.NumCols(), maxlen
);
1003 cnt
.init(context
, 0);
1004 for (short_rat_list::iterator i
= term
.begin(); i
!= term
.end(); ++i
)
1005 cnt
.reduce((*i
)->n
.coeff
, (*i
)->n
.power
, (*i
)->d
.power
);
1007 for (int i
= 1; i
<= maxlen
; ++i
)
1008 if (value_notzero_p(mpq_numref(cnt
.count
[i
]))) {
1009 value_set_si(*sum
, -1);
1013 assert(value_one_p(mpq_denref(cnt
.count
[0])));
1014 value_assign(*sum
, mpq_numref(cnt
.count
[0]));
1018 bool gen_fun::is_zero() const
1025 empty
= summate(&c
) && value_zero_p(c
);