6 #include <barvinok/genfun.h>
7 #include <barvinok/barvinok.h>
8 #include "conversion.h"
9 #include "genfun_constructor.h"
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
) {
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)
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
)
49 short_rat::short_rat(Value c
)
52 value2zz(c
, n
.coeff
[0].n
);
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
)
64 n
.power
.SetDims(1, num
.length());
70 short_rat::short_rat(const vec_QQ
& c
, const mat_ZZ
& num
, const mat_ZZ
& den
)
78 void short_rat::normalize()
80 /* Make all powers in denominator reverse-lexico-positive */
81 for (int i
= 0; i
< d
.power
.NumRows(); ++i
) {
83 for (j
= d
.power
.NumCols()-1; j
>= 0; --j
)
84 if (!IsZero(d
.power
[i
][j
]))
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();
105 for (j
= 0; j
< len
; ++j
)
106 if (r
->n
.power
[i
] == n
.power
[j
])
109 n
.coeff
[j
] += r
->n
.coeff
[i
];
110 if (n
.coeff
[j
].n
== 0) {
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
);
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);
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
);
159 barvinok_count_with_options(P
, &tmp
, options
);
161 if (value_zero_p(tmp
))
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];
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
)
186 if (step
!= n
.power
[2*k
+1] - n
.power
[2*k
])
189 if (k
== n
.power
.NumRows()/2) {
190 for (k
= 0; k
< d
.power
.NumRows(); ++k
)
191 if (d
.power
[k
] == step
)
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
);
211 gen_fun::gen_fun(Value c
)
213 short_rat
*r
= new short_rat(c
);
214 context
= Universe_Polyhedron(0);
218 void gen_fun::add(const QQ
& c
, const vec_ZZ
& num
, const mat_ZZ
& den
)
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()) {
231 if ((*i
)->n
.coeff
.length() == 0) {
234 } else if ((*i
)->reduced()) {
236 /* we've modified term[i], so remove it
237 * and add it back again
251 void gen_fun::add(const QQ
& c
, const gen_fun
*gf
)
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
) {
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);
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
)
306 split_param_compression(CP
, map
, offset
);
307 Polyhedron
*C
= Polyhedron_Image(context
, CP
, 0);
308 Polyhedron_Free(context
);
311 short_rat_list new_term
;
312 for (short_rat_list::iterator i
= term
.begin(); i
!= term
.end(); ++i
) {
316 for (int j
= 0; j
< r
->n
.power
.NumRows(); ++j
)
317 r
->n
.power
[j
] += offset
;
324 struct parallel_cones
{
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
)
337 struct parallel_polytopes
{
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
),
356 bool add(const QQ
& c
, Polyhedron
*P
) {
359 for (i
= 0; i
< P
->NbEq
; ++i
)
360 if (First_Non_Zero(P
->Constraint
[i
]+1,
361 P
->Dimension
-nparam
) == -1)
366 Polyhedron
*Q
= remove_equalities_p(Polyhedron_Copy(P
), P
->Dimension
-nparam
,
368 POL_ENSURE_VERTICES(Q
);
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);
384 R
= Polyhedron_Preimage(Q
, T
, options
->MaxRays
);
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)
395 red
= gf_base::create(Polyhedron_Copy(context
), dim
, nparam
, options
);
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
);
402 assert(Q
->Dimension
== dim
);
403 for (int i
= 0; i
< Q
->NbConstraints
; ++i
) {
405 for (j
= 0; j
< Constraints
->NbRows
; ++j
)
406 if (Vector_Equal(Q
->Constraint
[i
]+1, Constraints
->p
[j
],
409 assert(j
< Constraints
->NbRows
);
413 for (int i
= 0; i
< Q
->NbRays
; ++i
) {
414 if (!value_pos_p(Q
->Ray
[i
][dim
+1]))
417 Polyhedron
*C
= supporting_cone(Q
, i
);
419 if (First_Non_Zero(C
->Constraint
[C
->NbConstraints
-1]+1,
423 int *pos
= new int[1+C
->NbConstraints
];
424 pos
[0] = C
->NbConstraints
;
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
],
435 assert(l
== C
->NbConstraints
);
438 for (j
= 0; j
< cones
.size(); ++j
)
439 if (!memcmp(pos
, cones
[j
].pos
, (1+C
->NbConstraints
)*sizeof(int)))
441 if (j
== cones
.size())
442 cones
.push_back(parallel_cones(pos
));
449 for (k
= 0; k
< cones
[j
].vertices
.size(); ++k
)
450 if (Vector_Equal(Q
->Ray
[i
]+1, cones
[j
].vertices
[k
].first
->p
,
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
));
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
);
464 cones
[j
].vertices
[k
] = cones
[j
].vertices
[size
-1];
465 cones
[j
].vertices
.pop_back();
476 for (int i
= 0; i
< cones
.size(); ++i
) {
477 Matrix
*M
= Matrix_Alloc(cones
[i
].pos
[0], 1+Constraints
->NbColumns
+1);
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
);
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
);
494 red
->gf
->substitute(CP
);
497 void print(std::ostream
& os
) const {
498 for (int i
= 0; i
< cones
.size(); ++i
) {
500 for (int j
= 0; j
< cones
[i
].pos
[0]; ++j
) {
503 os
<< cones
[i
].pos
[1+j
];
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
);
519 Matrix_Free(Constraints
);
528 gen_fun
*gen_fun::Hadamard_product(const gen_fun
*gf
, barvinok_options
*options
)
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();
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
);
570 QQ c
= (*i
)->n
.coeff
[j
];
571 c
*= (*i2
)->n
.coeff
[j2
];
573 gen_fun
*t
= barvinok_series_with_options(P
, U
, options
);
582 gen_fun
*t
= pp
.compute();
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
);
603 static void Polyhedron_Shift(Polyhedron
*P
, Vector
*offset
)
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]))
615 if (value_zero_p(P
->Ray
[i
][1+P
->Dimension
]))
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
);
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
);
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);
653 Polyhedron_Free(context
);
657 static void print_power(std::ostream
& os
, const QQ
& c
, const vec_ZZ
& p
,
658 unsigned int nparam
, char **param_name
)
662 for (int i
= 0; i
< p
.length(); ++i
) {
666 if (c
.n
== -1 && c
.d
== 1)
668 else if (c
.n
!= 1 || c
.d
!= 1) {
684 os
<< "^(" << p
[i
] << ")";
695 void short_rat::print(std::ostream
& os
, unsigned int nparam
, char **param_name
) const
699 for (int j
= 0; j
< n
.coeff
.length(); ++j
) {
700 if (j
!= 0 && n
.coeff
[j
].n
> 0)
702 print_power(os
, n
.coeff
[j
], n
.power
[j
], nparam
, param_name
);
705 for (int j
= 0; j
< d
.power
.NumRows(); ++j
) {
709 print_power(os
, mone
, d
.power
[j
], nparam
, param_name
);
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())
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
;
732 std::ostream
& operator<< (std::ostream
& os
, const Polyhedron
& P
)
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);
749 std::ostream
& operator<< (std::ostream
& os
, const gen_fun
& gf
)
751 os
<< *gf
.context
<< endl
;
753 os
<< gf
.term
.size() << endl
;
754 for (short_rat_list::iterator i
= gf
.term
.begin(); i
!= gf
.term
.end(); ++i
)
759 static Matrix
*Matrix_Read(std::istream
& is
)
766 M
= Matrix_Alloc(r
, c
);
767 for (int i
= 0; i
< r
; ++i
)
768 for (int j
= 0; j
< c
; ++j
) {
770 zz2value(tmp
, M
->p
[i
][j
]);
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
);
781 gen_fun
*gf
= new gen_fun(C
);
789 for (int i
= 0; i
< n
; ++i
) {
790 is
>> c
>> num
>> den
;
791 gf
->add(new short_rat(c
, num
, den
));
797 gen_fun::operator evalue
*() const
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);
828 if (EVALUE_IS_ZERO(*E
)) {
833 zz2value((*i
)->n
.coeff
[j
].n
, factor
.x
.n
);
834 zz2value((*i
)->n
.coeff
[j
].d
, factor
.d
);
837 Matrix_Print(stdout, P_VALUE_FMT, C);
838 char *test[] = { "A", "B", "C", "D", "E", "F", "G" };
839 print_evalue(stdout, E, test);
853 value_clear(factor
.d
);
854 value_clear(factor
.x
.n
);
858 ZZ
gen_fun::coefficient(Value
* params
, barvinok_options
*options
) const
860 if (context
&& !in_domain(context
, params
))
865 for (short_rat_list::iterator i
= term
.begin(); i
!= term
.end(); ++i
)
866 sum
+= (*i
)->coefficient(params
, options
);
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
);
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
;
890 if (options
->incremental_specialization
== 1) {
891 red
= new partial_ireducer(Polyhedron_Project(context
, nparam
), dim
, nparam
);
893 red
= new partial_reducer(Polyhedron_Project(context
, nparam
), dim
, nparam
);
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
);
900 } catch (OrthogonalException
&e
) {
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);
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);
932 assert(value_one_p(mpq_denref(cnt
.count
[0])));
933 value_assign(*sum
, mpq_numref(cnt
.count
[0]));