5 #include <barvinok/genfun.h>
6 #include <barvinok/barvinok.h>
7 #include "conversion.h"
8 #include "genfun_constructor.h"
17 static int lex_cmp(mat_ZZ
& a
, mat_ZZ
& b
)
19 assert(a
.NumCols() == b
.NumCols());
20 int alen
= a
.NumRows();
21 int blen
= b
.NumRows();
22 int len
= alen
< blen
? alen
: blen
;
24 for (int i
= 0; i
< len
; ++i
) {
25 int s
= lex_cmp(a
[i
], b
[i
]);
32 static void lex_order_terms(struct short_rat
* rat
)
34 for (int i
= 0; i
< rat
->n
.power
.NumRows(); ++i
) {
36 for (int j
= i
+1; j
< rat
->n
.power
.NumRows(); ++j
)
37 if (lex_cmp(rat
->n
.power
[j
], rat
->n
.power
[m
]) < 0)
40 vec_ZZ tmp
= rat
->n
.power
[m
];
41 rat
->n
.power
[m
] = rat
->n
.power
[i
];
42 rat
->n
.power
[i
] = tmp
;
43 QQ tmp_coeff
= rat
->n
.coeff
[m
];
44 rat
->n
.coeff
[m
] = rat
->n
.coeff
[i
];
45 rat
->n
.coeff
[i
] = tmp_coeff
;
50 void short_rat::add(short_rat
*r
)
52 for (int i
= 0; i
< r
->n
.power
.NumRows(); ++i
) {
53 int len
= n
.coeff
.length();
55 for (j
= 0; j
< len
; ++j
)
56 if (r
->n
.power
[i
] == n
.power
[j
])
59 n
.coeff
[j
] += r
->n
.coeff
[i
];
60 if (n
.coeff
[j
].n
== 0) {
62 n
.power
[j
] = n
.power
[len
-1];
63 n
.coeff
[j
] = n
.coeff
[len
-1];
65 int dim
= n
.power
.NumCols();
66 n
.coeff
.SetLength(len
-1);
67 n
.power
.SetDims(len
-1, dim
);
70 int dim
= n
.power
.NumCols();
71 n
.coeff
.SetLength(len
+1);
72 n
.power
.SetDims(len
+1, dim
);
73 n
.coeff
[len
] = r
->n
.coeff
[i
];
74 n
.power
[len
] = r
->n
.power
[i
];
79 bool short_rat::reduced()
81 int dim
= n
.power
.NumCols();
82 lex_order_terms(this);
83 if (n
.power
.NumRows() % 2 == 0) {
84 if (n
.coeff
[0].n
== -n
.coeff
[1].n
&&
85 n
.coeff
[0].d
== n
.coeff
[1].d
) {
86 vec_ZZ step
= n
.power
[1] - n
.power
[0];
88 for (k
= 1; k
< n
.power
.NumRows()/2; ++k
) {
89 if (n
.coeff
[2*k
].n
!= -n
.coeff
[2*k
+1].n
||
90 n
.coeff
[2*k
].d
!= n
.coeff
[2*k
+1].d
)
92 if (step
!= n
.power
[2*k
+1] - n
.power
[2*k
])
95 if (k
== n
.power
.NumRows()/2) {
96 for (k
= 0; k
< d
.power
.NumRows(); ++k
)
97 if (d
.power
[k
] == step
)
99 if (k
< d
.power
.NumRows()) {
100 for (++k
; k
< d
.power
.NumRows(); ++k
)
101 d
.power
[k
-1] = d
.power
[k
];
102 d
.power
.SetDims(k
-1, dim
);
103 for (k
= 1; k
< n
.power
.NumRows()/2; ++k
) {
104 n
.coeff
[k
] = n
.coeff
[2*k
];
105 n
.power
[k
] = n
.power
[2*k
];
107 n
.coeff
.SetLength(k
);
108 n
.power
.SetDims(k
, dim
);
117 gen_fun::gen_fun(Value c
)
120 term
.push_back(new short_rat
);
121 term
[0]->n
.coeff
.SetLength(1);
122 value2zz(c
, term
[0]->n
.coeff
[0].n
);
123 term
[0]->n
.coeff
[0].d
= 1;
124 term
[0]->n
.power
.SetDims(1, 0);
125 term
[0]->d
.power
.SetDims(0, 0);
128 void gen_fun::add(const QQ
& c
, const vec_ZZ
& num
, const mat_ZZ
& den
)
133 short_rat
* r
= new short_rat
;
134 r
->n
.coeff
.SetLength(1);
135 ZZ g
= GCD(c
.n
, c
.d
);
136 r
->n
.coeff
[0].n
= c
.n
/g
;
137 r
->n
.coeff
[0].d
= c
.d
/g
;
138 r
->n
.power
.SetDims(1, num
.length());
142 /* Make all powers in denominator lexico-positive */
143 for (int i
= 0; i
< r
->d
.power
.NumRows(); ++i
) {
145 for (j
= 0; j
< r
->d
.power
.NumCols(); ++j
)
146 if (r
->d
.power
[i
][j
] != 0)
148 if (r
->d
.power
[i
][j
] < 0) {
149 r
->d
.power
[i
] = -r
->d
.power
[i
];
150 r
->n
.coeff
[0].n
= -r
->n
.coeff
[0].n
;
151 r
->n
.power
[0] += r
->d
.power
[i
];
155 /* Order powers in denominator */
156 lex_order_rows(r
->d
.power
);
158 for (int i
= 0; i
< term
.size(); ++i
)
159 if (lex_cmp(term
[i
]->d
.power
, r
->d
.power
) == 0) {
161 if (term
[i
]->n
.coeff
.length() == 0) {
163 if (i
!= term
.size()-1)
164 term
[i
] = term
[term
.size()-1];
166 } else if (term
[i
]->reduced()) {
168 /* we've modified term[i], so removed it
169 * and add it back again
172 if (i
!= term
.size()-1)
173 term
[i
] = term
[term
.size()-1];
185 void gen_fun::add(const QQ
& c
, const gen_fun
*gf
)
188 for (int i
= 0; i
< gf
->term
.size(); ++i
) {
189 for (int j
= 0; j
< gf
->term
[i
]->n
.power
.NumRows(); ++j
) {
191 p
*= gf
->term
[i
]->n
.coeff
[j
];
192 add(p
, gf
->term
[i
]->n
.power
[j
], gf
->term
[i
]->d
.power
);
197 static void split_param_compression(Matrix
*CP
, mat_ZZ
& map
, vec_ZZ
& offset
)
199 Matrix
*T
= Transpose(CP
);
200 matrix2zz(T
, map
, T
->NbRows
-1, T
->NbColumns
-1);
201 values2zz(T
->p
[T
->NbRows
-1], offset
, T
->NbColumns
-1);
206 * Perform the substitution specified by CP
208 * CP is a homogeneous matrix that maps a set of "compressed parameters"
209 * to the original set of parameters.
211 * This function is applied to a gen_fun computed with the compressed parameters
212 * and adapts it to refer to the original parameters.
214 * That is, if y are the compressed parameters and x = A y + b are the original
215 * parameters, then we want the coefficient of the monomial t^y in the original
216 * generating function to be the coefficient of the monomial u^x in the resulting
217 * generating function.
218 * The original generating function has the form
220 * a t^m/(1-t^n) = a t^m + a t^{m+n} + a t^{m+2n} + ...
222 * Since each term t^y should correspond to a term u^x, with x = A y + b, we want
224 * a u^{A m + b} + a u^{A (m+n) + b} + a u^{A (m+2n) +b} + ... =
226 * = a u^{A m + b}/(1-u^{A n})
228 * Therefore, we multiply the powers m and n in both numerator and denominator by A
229 * and add b to the power in the numerator.
230 * Since the above powers are stored as row vectors m^T and n^T,
231 * we compute, say, m'^T = m^T A^T to obtain m' = A m.
233 * The pair (map, offset) contains the same information as CP.
234 * map is the transpose of the linear part of CP, while offset is the constant part.
236 void gen_fun::substitute(Matrix
*CP
)
240 split_param_compression(CP
, map
, offset
);
241 Polyhedron
*C
= Polyhedron_Image(context
, CP
, 0);
242 Polyhedron_Free(context
);
244 for (int i
= 0; i
< term
.size(); ++i
) {
245 term
[i
]->d
.power
*= map
;
246 term
[i
]->n
.power
*= map
;
247 for (int j
= 0; j
< term
[i
]->n
.power
.NumRows(); ++j
)
248 term
[i
]->n
.power
[j
] += offset
;
254 vector
<pair
<Vector
*, QQ
> > vertices
;
255 cone(int *pos
) : pos(pos
) {}
258 #ifndef HAVE_COMPRESS_PARMS
259 static Matrix
*compress_parms(Matrix
*M
, unsigned nparam
)
265 struct parallel_polytopes
{
273 parallel_polytopes(int n
, Polyhedron
*context
, int dim
, int nparam
) :
274 dim(dim
), nparam(nparam
) {
275 red
= gf_base::create(Polyhedron_Copy(context
), dim
, nparam
);
280 void add(const QQ
& c
, Polyhedron
*P
, unsigned MaxRays
) {
281 Polyhedron
*Q
= remove_equalities_p(Polyhedron_Copy(P
), P
->Dimension
-nparam
,
283 POL_ENSURE_VERTICES(Q
);
293 M
= Matrix_Alloc(Q
->NbEq
, Q
->Dimension
+2);
294 Vector_Copy(Q
->Constraint
[0], M
->p
[0], Q
->NbEq
* (Q
->Dimension
+2));
295 CP
= compress_parms(M
, nparam
);
296 T
= align_matrix(CP
, Q
->Dimension
+1);
299 R
= Polyhedron_Preimage(Q
, T
, MaxRays
);
301 Q
= remove_equalities_p(R
, R
->Dimension
-nparam
, NULL
);
303 assert(Q
->NbEq
== 0);
304 assert(Q
->Dimension
== dim
);
306 if (First_Non_Zero(Q
->Constraint
[Q
->NbConstraints
-1]+1, Q
->Dimension
) == -1)
311 Constraints
= Matrix_Alloc(Q
->NbConstraints
, Q
->Dimension
);
312 for (int i
= 0; i
< Q
->NbConstraints
; ++i
) {
313 Vector_Copy(Q
->Constraint
[i
]+1, Constraints
->p
[i
], Q
->Dimension
);
316 for (int i
= 0; i
< Q
->NbConstraints
; ++i
) {
318 for (j
= 0; j
< Constraints
->NbRows
; ++j
)
319 if (Vector_Equal(Q
->Constraint
[i
]+1, Constraints
->p
[j
],
322 assert(j
< Constraints
->NbRows
);
326 for (int i
= 0; i
< Q
->NbRays
; ++i
) {
327 if (!value_pos_p(Q
->Ray
[i
][dim
+1]))
330 Polyhedron
*C
= supporting_cone(Q
, i
);
332 if (First_Non_Zero(C
->Constraint
[C
->NbConstraints
-1]+1,
336 int *pos
= new int[1+C
->NbConstraints
];
337 pos
[0] = C
->NbConstraints
;
339 for (int k
= 0; k
< Constraints
->NbRows
; ++k
) {
340 for (int j
= 0; j
< C
->NbConstraints
; ++j
) {
341 if (Vector_Equal(C
->Constraint
[j
]+1, Constraints
->p
[k
],
348 assert(l
== C
->NbConstraints
);
351 for (j
= 0; j
< cones
.size(); ++j
)
352 if (!memcmp(pos
, cones
[j
].pos
, (1+C
->NbConstraints
)*sizeof(int)))
354 if (j
== cones
.size())
355 cones
.push_back(cone(pos
));
362 for (k
= 0; k
< cones
[j
].vertices
.size(); ++k
)
363 if (Vector_Equal(Q
->Ray
[i
]+1, cones
[j
].vertices
[k
].first
->p
,
367 if (k
== cones
[j
].vertices
.size()) {
368 Vector
*vertex
= Vector_Alloc(Q
->Dimension
+1);
369 Vector_Copy(Q
->Ray
[i
]+1, vertex
->p
, Q
->Dimension
+1);
370 cones
[j
].vertices
.push_back(pair
<Vector
*,QQ
>(vertex
, c
));
372 cones
[j
].vertices
[k
].second
+= c
;
373 if (cones
[j
].vertices
[k
].second
.n
== 0) {
374 int size
= cones
[j
].vertices
.size();
375 Vector_Free(cones
[j
].vertices
[k
].first
);
377 cones
[j
].vertices
[k
] = cones
[j
].vertices
[size
-1];
378 cones
[j
].vertices
.pop_back();
385 gen_fun
*compute(unsigned MaxRays
) {
386 for (int i
= 0; i
< cones
.size(); ++i
) {
387 Matrix
*M
= Matrix_Alloc(cones
[i
].pos
[0], 1+Constraints
->NbColumns
+1);
389 for (int j
= 0; j
<cones
[i
].pos
[0]; ++j
) {
390 value_set_si(M
->p
[j
][0], 1);
391 Vector_Copy(Constraints
->p
[cones
[i
].pos
[1+j
]], M
->p
[j
]+1,
392 Constraints
->NbColumns
);
394 Cone
= Constraints2Polyhedron(M
, MaxRays
);
396 for (int j
= 0; j
< cones
[i
].vertices
.size(); ++j
) {
397 red
->base
->do_vertex_cone(cones
[i
].vertices
[j
].second
,
398 Polyhedron_Copy(Cone
),
399 cones
[i
].vertices
[j
].first
->p
,
402 Polyhedron_Free(Cone
);
405 red
->gf
->substitute(CP
);
408 void print(std::ostream
& os
) const {
409 for (int i
= 0; i
< cones
.size(); ++i
) {
411 for (int j
= 0; j
< cones
[i
].pos
[0]; ++j
) {
414 os
<< cones
[i
].pos
[1+j
];
417 for (int j
= 0; j
< cones
[i
].vertices
.size(); ++j
) {
418 Vector_Print(stderr
, P_VALUE_FMT
, cones
[i
].vertices
[j
].first
);
419 os
<< cones
[i
].vertices
[j
].second
<< endl
;
423 ~parallel_polytopes() {
424 for (int i
= 0; i
< cones
.size(); ++i
) {
425 delete [] cones
[i
].pos
;
426 for (int j
= 0; j
< cones
[i
].vertices
.size(); ++j
)
427 Vector_Free(cones
[i
].vertices
[j
].first
);
430 Matrix_Free(Constraints
);
439 gen_fun
*gen_fun::Hadamard_product(const gen_fun
*gf
, unsigned MaxRays
)
442 Polyhedron
*C
= DomainIntersection(context
, gf
->context
, MaxRays
);
443 Polyhedron
*U
= Universe_Polyhedron(C
->Dimension
);
444 gen_fun
*sum
= new gen_fun(C
);
445 for (int i
= 0; i
< term
.size(); ++i
) {
446 for (int i2
= 0; i2
< gf
->term
.size(); ++i2
) {
447 int d
= term
[i
]->d
.power
.NumCols();
448 int k1
= term
[i
]->d
.power
.NumRows();
449 int k2
= gf
->term
[i2
]->d
.power
.NumRows();
450 assert(term
[i
]->d
.power
.NumCols() == gf
->term
[i2
]->d
.power
.NumCols());
452 parallel_polytopes
pp(term
[i
]->n
.power
.NumRows() *
453 gf
->term
[i2
]->n
.power
.NumRows(),
454 sum
->context
, k1
+k2
-d
, d
);
456 for (int j
= 0; j
< term
[i
]->n
.power
.NumRows(); ++j
) {
457 for (int j2
= 0; j2
< gf
->term
[i2
]->n
.power
.NumRows(); ++j2
) {
458 Matrix
*M
= Matrix_Alloc(k1
+k2
+d
+d
, 1+k1
+k2
+d
+1);
459 for (int k
= 0; k
< k1
+k2
; ++k
) {
460 value_set_si(M
->p
[k
][0], 1);
461 value_set_si(M
->p
[k
][1+k
], 1);
463 for (int k
= 0; k
< d
; ++k
) {
464 value_set_si(M
->p
[k1
+k2
+k
][1+k1
+k2
+k
], -1);
465 zz2value(term
[i
]->n
.power
[j
][k
], M
->p
[k1
+k2
+k
][1+k1
+k2
+d
]);
466 for (int l
= 0; l
< k1
; ++l
)
467 zz2value(term
[i
]->d
.power
[l
][k
], M
->p
[k1
+k2
+k
][1+l
]);
469 for (int k
= 0; k
< d
; ++k
) {
470 value_set_si(M
->p
[k1
+k2
+d
+k
][1+k1
+k2
+k
], -1);
471 zz2value(gf
->term
[i2
]->n
.power
[j2
][k
],
472 M
->p
[k1
+k2
+d
+k
][1+k1
+k2
+d
]);
473 for (int l
= 0; l
< k2
; ++l
)
474 zz2value(gf
->term
[i2
]->d
.power
[l
][k
],
475 M
->p
[k1
+k2
+d
+k
][1+k1
+l
]);
477 Polyhedron
*P
= Constraints2Polyhedron(M
, MaxRays
);
480 QQ c
= term
[i
]->n
.coeff
[j
];
481 c
*= gf
->term
[i2
]->n
.coeff
[j2
];
482 pp
.add(c
, P
, MaxRays
);
488 gen_fun
*t
= pp
.compute(MaxRays
);
497 void gen_fun::add_union(gen_fun
*gf
, unsigned MaxRays
)
499 QQ
one(1, 1), mone(-1, 1);
501 gen_fun
*hp
= Hadamard_product(gf
, MaxRays
);
507 static void Polyhedron_Shift(Polyhedron
*P
, Vector
*offset
)
511 for (int i
= 0; i
< P
->NbConstraints
; ++i
) {
512 Inner_Product(P
->Constraint
[i
]+1, offset
->p
, P
->Dimension
, &tmp
);
513 value_subtract(P
->Constraint
[i
][1+P
->Dimension
],
514 P
->Constraint
[i
][1+P
->Dimension
], tmp
);
516 for (int i
= 0; i
< P
->NbRays
; ++i
) {
517 if (value_notone_p(P
->Ray
[i
][0]))
519 if (value_zero_p(P
->Ray
[i
][1+P
->Dimension
]))
521 Vector_Combine(P
->Ray
[i
]+1, offset
->p
, P
->Ray
[i
]+1,
522 P
->Ray
[i
][0], P
->Ray
[i
][1+P
->Dimension
], P
->Dimension
);
527 void gen_fun::shift(const vec_ZZ
& offset
)
529 for (int i
= 0; i
< term
.size(); ++i
)
530 for (int j
= 0; j
< term
[i
]->n
.power
.NumRows(); ++j
)
531 term
[i
]->n
.power
[j
] += offset
;
533 Vector
*v
= Vector_Alloc(offset
.length());
534 zz2values(offset
, v
->p
);
535 Polyhedron_Shift(context
, v
);
539 /* Divide the generating functin by 1/(1-z^power).
540 * The effect on the corresponding explicit function f(x) is
541 * f'(x) = \sum_{i=0}^\infty f(x - i * power)
543 void gen_fun::divide(const vec_ZZ
& power
)
545 for (int i
= 0; i
< term
.size(); ++i
) {
546 int r
= term
[i
]->d
.power
.NumRows();
547 int c
= term
[i
]->d
.power
.NumCols();
548 term
[i
]->d
.power
.SetDims(r
+1, c
);
549 term
[i
]->d
.power
[r
] = power
;
552 Vector
*v
= Vector_Alloc(1+power
.length()+1);
553 value_set_si(v
->p
[0], 1);
554 zz2values(power
, v
->p
+1);
555 Polyhedron
*C
= AddRays(v
->p
, 1, context
, context
->NbConstraints
+1);
557 Polyhedron_Free(context
);
561 static void print_power(std::ostream
& os
, QQ
& c
, vec_ZZ
& p
,
562 unsigned int nparam
, char **param_name
)
566 for (int i
= 0; i
< p
.length(); ++i
) {
570 if (c
.n
== -1 && c
.d
== 1)
572 else if (c
.n
!= 1 || c
.d
!= 1) {
588 os
<< "^(" << p
[i
] << ")";
599 void gen_fun::print(std::ostream
& os
, unsigned int nparam
, char **param_name
) const
602 for (int i
= 0; i
< term
.size(); ++i
) {
606 for (int j
= 0; j
< term
[i
]->n
.coeff
.length(); ++j
) {
607 if (j
!= 0 && term
[i
]->n
.coeff
[j
].n
> 0)
609 print_power(os
, term
[i
]->n
.coeff
[j
], term
[i
]->n
.power
[j
],
613 for (int j
= 0; j
< term
[i
]->d
.power
.NumRows(); ++j
) {
617 print_power(os
, mone
, term
[i
]->d
.power
[j
], nparam
, param_name
);
624 gen_fun::operator evalue
*() const
628 value_init(factor
.d
);
629 value_init(factor
.x
.n
);
630 for (int i
= 0; i
< term
.size(); ++i
) {
631 unsigned nvar
= term
[i
]->d
.power
.NumRows();
632 unsigned nparam
= term
[i
]->d
.power
.NumCols();
633 Matrix
*C
= Matrix_Alloc(nparam
+ nvar
, 1 + nvar
+ nparam
+ 1);
634 mat_ZZ
& d
= term
[i
]->d
.power
;
635 Polyhedron
*U
= context
? context
: Universe_Polyhedron(nparam
);
637 for (int j
= 0; j
< term
[i
]->n
.coeff
.length(); ++j
) {
638 for (int r
= 0; r
< nparam
; ++r
) {
639 value_set_si(C
->p
[r
][0], 0);
640 for (int c
= 0; c
< nvar
; ++c
) {
641 zz2value(d
[c
][r
], C
->p
[r
][1+c
]);
643 Vector_Set(&C
->p
[r
][1+nvar
], 0, nparam
);
644 value_set_si(C
->p
[r
][1+nvar
+r
], -1);
645 zz2value(term
[i
]->n
.power
[j
][r
], C
->p
[r
][1+nvar
+nparam
]);
647 for (int r
= 0; r
< nvar
; ++r
) {
648 value_set_si(C
->p
[nparam
+r
][0], 1);
649 Vector_Set(&C
->p
[nparam
+r
][1], 0, nvar
+ nparam
+ 1);
650 value_set_si(C
->p
[nparam
+r
][1+r
], 1);
652 Polyhedron
*P
= Constraints2Polyhedron(C
, 0);
653 evalue
*E
= barvinok_enumerate_ev(P
, U
, 0);
655 if (EVALUE_IS_ZERO(*E
)) {
660 zz2value(term
[i
]->n
.coeff
[j
].n
, factor
.x
.n
);
661 zz2value(term
[i
]->n
.coeff
[j
].d
, factor
.d
);
664 Matrix_Print(stdout, P_VALUE_FMT, C);
665 char *test[] = { "A", "B", "C", "D", "E", "F", "G" };
666 print_evalue(stdout, E, test);
680 value_clear(factor
.d
);
681 value_clear(factor
.x
.n
);
685 void gen_fun::coefficient(Value
* params
, Value
* c
) const
687 if (context
&& !in_domain(context
, params
)) {
694 value_init(part
.x
.n
);
697 evalue_set_si(&sum
, 0, 1);
701 for (int i
= 0; i
< term
.size(); ++i
) {
702 unsigned nvar
= term
[i
]->d
.power
.NumRows();
703 unsigned nparam
= term
[i
]->d
.power
.NumCols();
704 Matrix
*C
= Matrix_Alloc(nparam
+ nvar
, 1 + nvar
+ 1);
705 mat_ZZ
& d
= term
[i
]->d
.power
;
707 for (int j
= 0; j
< term
[i
]->n
.coeff
.length(); ++j
) {
708 C
->NbRows
= nparam
+nvar
;
709 for (int r
= 0; r
< nparam
; ++r
) {
710 value_set_si(C
->p
[r
][0], 0);
711 for (int c
= 0; c
< nvar
; ++c
) {
712 zz2value(d
[c
][r
], C
->p
[r
][1+c
]);
714 zz2value(term
[i
]->n
.power
[j
][r
], C
->p
[r
][1+nvar
]);
715 value_subtract(C
->p
[r
][1+nvar
], C
->p
[r
][1+nvar
], params
[r
]);
717 for (int r
= 0; r
< nvar
; ++r
) {
718 value_set_si(C
->p
[nparam
+r
][0], 1);
719 Vector_Set(&C
->p
[nparam
+r
][1], 0, nvar
+ 1);
720 value_set_si(C
->p
[nparam
+r
][1+r
], 1);
722 Polyhedron
*P
= Constraints2Polyhedron(C
, 0);
727 barvinok_count(P
, &tmp
, 0);
729 if (value_zero_p(tmp
))
731 zz2value(term
[i
]->n
.coeff
[j
].n
, part
.x
.n
);
732 zz2value(term
[i
]->n
.coeff
[j
].d
, part
.d
);
733 value_multiply(part
.x
.n
, part
.x
.n
, tmp
);
739 assert(value_one_p(sum
.d
));
740 value_assign(*c
, sum
.x
.n
);
744 value_clear(part
.x
.n
);
746 value_clear(sum
.x
.n
);
749 gen_fun
*gen_fun::summate(int nvar
) const
751 int dim
= context
->Dimension
;
752 int nparam
= dim
- nvar
;
754 #ifdef USE_INCREMENTAL_DF
755 partial_ireducer
red(Polyhedron_Project(context
, nparam
), dim
, nparam
);
757 partial_reducer
red(Polyhedron_Project(context
, nparam
), dim
, nparam
);
760 for (int i
= 0; i
< term
.size(); ++i
)
761 for (int j
= 0; j
< term
[i
]->n
.power
.NumRows(); ++j
)
762 red
.reduce(term
[i
]->n
.coeff
[j
], term
[i
]->n
.power
[j
], term
[i
]->d
.power
);
766 /* returns true if the set was finite and false otherwise */
767 bool gen_fun::summate(Value
*sum
) const
769 if (term
.size() == 0) {
770 value_set_si(*sum
, 0);
775 for (int i
= 0; i
< term
.size(); ++i
)
776 if (term
[i
]->d
.power
.NumRows() > maxlen
)
777 maxlen
= term
[i
]->d
.power
.NumRows();
779 infinite_icounter
cnt(term
[0]->d
.power
.NumCols(), maxlen
);
780 for (int i
= 0; i
< term
.size(); ++i
)
781 for (int j
= 0; j
< term
[i
]->n
.power
.NumRows(); ++j
)
782 cnt
.reduce(term
[i
]->n
.coeff
[j
], term
[i
]->n
.power
[j
], term
[i
]->d
.power
);
784 for (int i
= 1; i
<= maxlen
; ++i
)
785 if (value_notzero_p(mpq_numref(cnt
.count
[i
]))) {
786 value_set_si(*sum
, -1);
790 assert(value_one_p(mpq_denref(cnt
.count
[0])));
791 value_assign(*sum
, mpq_numref(cnt
.count
[0]));