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
)
119 context
= Universe_Polyhedron(0);
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
{
274 parallel_polytopes(int n
, Polyhedron
*context
, int nparam
) :
275 context(context
), dim(-1), nparam(nparam
) {
281 bool add(const QQ
& c
, Polyhedron
*P
, unsigned MaxRays
) {
284 for (i
= 0; i
< P
->NbEq
; ++i
)
285 if (First_Non_Zero(P
->Constraint
[i
]+1,
286 P
->Dimension
-nparam
) == -1)
291 Polyhedron
*Q
= remove_equalities_p(Polyhedron_Copy(P
), P
->Dimension
-nparam
,
293 POL_ENSURE_VERTICES(Q
);
303 M
= Matrix_Alloc(Q
->NbEq
, Q
->Dimension
+2);
304 Vector_Copy(Q
->Constraint
[0], M
->p
[0], Q
->NbEq
* (Q
->Dimension
+2));
305 CP
= compress_parms(M
, nparam
);
306 T
= align_matrix(CP
, Q
->Dimension
+1);
309 R
= Polyhedron_Preimage(Q
, T
, MaxRays
);
311 Q
= remove_equalities_p(R
, R
->Dimension
-nparam
, NULL
);
313 assert(Q
->NbEq
== 0);
315 if (First_Non_Zero(Q
->Constraint
[Q
->NbConstraints
-1]+1, Q
->Dimension
) == -1)
320 red
= gf_base::create(Polyhedron_Copy(context
), dim
, nparam
);
322 Constraints
= Matrix_Alloc(Q
->NbConstraints
, Q
->Dimension
);
323 for (int i
= 0; i
< Q
->NbConstraints
; ++i
) {
324 Vector_Copy(Q
->Constraint
[i
]+1, Constraints
->p
[i
], Q
->Dimension
);
327 assert(Q
->Dimension
== dim
);
328 for (int i
= 0; i
< Q
->NbConstraints
; ++i
) {
330 for (j
= 0; j
< Constraints
->NbRows
; ++j
)
331 if (Vector_Equal(Q
->Constraint
[i
]+1, Constraints
->p
[j
],
334 assert(j
< Constraints
->NbRows
);
338 for (int i
= 0; i
< Q
->NbRays
; ++i
) {
339 if (!value_pos_p(Q
->Ray
[i
][dim
+1]))
342 Polyhedron
*C
= supporting_cone(Q
, i
);
344 if (First_Non_Zero(C
->Constraint
[C
->NbConstraints
-1]+1,
348 int *pos
= new int[1+C
->NbConstraints
];
349 pos
[0] = C
->NbConstraints
;
351 for (int k
= 0; k
< Constraints
->NbRows
; ++k
) {
352 for (int j
= 0; j
< C
->NbConstraints
; ++j
) {
353 if (Vector_Equal(C
->Constraint
[j
]+1, Constraints
->p
[k
],
360 assert(l
== C
->NbConstraints
);
363 for (j
= 0; j
< cones
.size(); ++j
)
364 if (!memcmp(pos
, cones
[j
].pos
, (1+C
->NbConstraints
)*sizeof(int)))
366 if (j
== cones
.size())
367 cones
.push_back(cone(pos
));
374 for (k
= 0; k
< cones
[j
].vertices
.size(); ++k
)
375 if (Vector_Equal(Q
->Ray
[i
]+1, cones
[j
].vertices
[k
].first
->p
,
379 if (k
== cones
[j
].vertices
.size()) {
380 Vector
*vertex
= Vector_Alloc(Q
->Dimension
+1);
381 Vector_Copy(Q
->Ray
[i
]+1, vertex
->p
, Q
->Dimension
+1);
382 cones
[j
].vertices
.push_back(pair
<Vector
*,QQ
>(vertex
, c
));
384 cones
[j
].vertices
[k
].second
+= c
;
385 if (cones
[j
].vertices
[k
].second
.n
== 0) {
386 int size
= cones
[j
].vertices
.size();
387 Vector_Free(cones
[j
].vertices
[k
].first
);
389 cones
[j
].vertices
[k
] = cones
[j
].vertices
[size
-1];
390 cones
[j
].vertices
.pop_back();
398 gen_fun
*compute(unsigned MaxRays
) {
401 for (int i
= 0; i
< cones
.size(); ++i
) {
402 Matrix
*M
= Matrix_Alloc(cones
[i
].pos
[0], 1+Constraints
->NbColumns
+1);
404 for (int j
= 0; j
<cones
[i
].pos
[0]; ++j
) {
405 value_set_si(M
->p
[j
][0], 1);
406 Vector_Copy(Constraints
->p
[cones
[i
].pos
[1+j
]], M
->p
[j
]+1,
407 Constraints
->NbColumns
);
409 Cone
= Constraints2Polyhedron(M
, MaxRays
);
411 for (int j
= 0; j
< cones
[i
].vertices
.size(); ++j
) {
412 red
->base
->do_vertex_cone(cones
[i
].vertices
[j
].second
,
413 Polyhedron_Copy(Cone
),
414 cones
[i
].vertices
[j
].first
->p
,
417 Polyhedron_Free(Cone
);
420 red
->gf
->substitute(CP
);
423 void print(std::ostream
& os
) const {
424 for (int i
= 0; i
< cones
.size(); ++i
) {
426 for (int j
= 0; j
< cones
[i
].pos
[0]; ++j
) {
429 os
<< cones
[i
].pos
[1+j
];
432 for (int j
= 0; j
< cones
[i
].vertices
.size(); ++j
) {
433 Vector_Print(stderr
, P_VALUE_FMT
, cones
[i
].vertices
[j
].first
);
434 os
<< cones
[i
].vertices
[j
].second
<< endl
;
438 ~parallel_polytopes() {
439 for (int i
= 0; i
< cones
.size(); ++i
) {
440 delete [] cones
[i
].pos
;
441 for (int j
= 0; j
< cones
[i
].vertices
.size(); ++j
)
442 Vector_Free(cones
[i
].vertices
[j
].first
);
445 Matrix_Free(Constraints
);
454 gen_fun
*gen_fun::Hadamard_product(const gen_fun
*gf
, unsigned MaxRays
)
457 Polyhedron
*C
= DomainIntersection(context
, gf
->context
, MaxRays
);
458 Polyhedron
*U
= Universe_Polyhedron(C
->Dimension
);
459 gen_fun
*sum
= new gen_fun(C
);
460 for (int i
= 0; i
< term
.size(); ++i
) {
461 for (int i2
= 0; i2
< gf
->term
.size(); ++i2
) {
462 int d
= term
[i
]->d
.power
.NumCols();
463 int k1
= term
[i
]->d
.power
.NumRows();
464 int k2
= gf
->term
[i2
]->d
.power
.NumRows();
465 assert(term
[i
]->d
.power
.NumCols() == gf
->term
[i2
]->d
.power
.NumCols());
467 parallel_polytopes
pp(term
[i
]->n
.power
.NumRows() *
468 gf
->term
[i2
]->n
.power
.NumRows(),
471 for (int j
= 0; j
< term
[i
]->n
.power
.NumRows(); ++j
) {
472 for (int j2
= 0; j2
< gf
->term
[i2
]->n
.power
.NumRows(); ++j2
) {
473 Matrix
*M
= Matrix_Alloc(k1
+k2
+d
+d
, 1+k1
+k2
+d
+1);
474 for (int k
= 0; k
< k1
+k2
; ++k
) {
475 value_set_si(M
->p
[k
][0], 1);
476 value_set_si(M
->p
[k
][1+k
], 1);
478 for (int k
= 0; k
< d
; ++k
) {
479 value_set_si(M
->p
[k1
+k2
+k
][1+k1
+k2
+k
], -1);
480 zz2value(term
[i
]->n
.power
[j
][k
], M
->p
[k1
+k2
+k
][1+k1
+k2
+d
]);
481 for (int l
= 0; l
< k1
; ++l
)
482 zz2value(term
[i
]->d
.power
[l
][k
], M
->p
[k1
+k2
+k
][1+l
]);
484 for (int k
= 0; k
< d
; ++k
) {
485 value_set_si(M
->p
[k1
+k2
+d
+k
][1+k1
+k2
+k
], -1);
486 zz2value(gf
->term
[i2
]->n
.power
[j2
][k
],
487 M
->p
[k1
+k2
+d
+k
][1+k1
+k2
+d
]);
488 for (int l
= 0; l
< k2
; ++l
)
489 zz2value(gf
->term
[i2
]->d
.power
[l
][k
],
490 M
->p
[k1
+k2
+d
+k
][1+k1
+l
]);
492 Polyhedron
*P
= Constraints2Polyhedron(M
, MaxRays
);
495 QQ c
= term
[i
]->n
.coeff
[j
];
496 c
*= gf
->term
[i2
]->n
.coeff
[j2
];
497 if (!pp
.add(c
, P
, MaxRays
)) {
498 gen_fun
*t
= barvinok_series(P
, U
, MaxRays
);
507 gen_fun
*t
= pp
.compute(MaxRays
);
518 void gen_fun::add_union(gen_fun
*gf
, unsigned MaxRays
)
520 QQ
one(1, 1), mone(-1, 1);
522 gen_fun
*hp
= Hadamard_product(gf
, MaxRays
);
528 static void Polyhedron_Shift(Polyhedron
*P
, Vector
*offset
)
532 for (int i
= 0; i
< P
->NbConstraints
; ++i
) {
533 Inner_Product(P
->Constraint
[i
]+1, offset
->p
, P
->Dimension
, &tmp
);
534 value_subtract(P
->Constraint
[i
][1+P
->Dimension
],
535 P
->Constraint
[i
][1+P
->Dimension
], tmp
);
537 for (int i
= 0; i
< P
->NbRays
; ++i
) {
538 if (value_notone_p(P
->Ray
[i
][0]))
540 if (value_zero_p(P
->Ray
[i
][1+P
->Dimension
]))
542 Vector_Combine(P
->Ray
[i
]+1, offset
->p
, P
->Ray
[i
]+1,
543 P
->Ray
[i
][0], P
->Ray
[i
][1+P
->Dimension
], P
->Dimension
);
548 void gen_fun::shift(const vec_ZZ
& offset
)
550 for (int i
= 0; i
< term
.size(); ++i
)
551 for (int j
= 0; j
< term
[i
]->n
.power
.NumRows(); ++j
)
552 term
[i
]->n
.power
[j
] += offset
;
554 Vector
*v
= Vector_Alloc(offset
.length());
555 zz2values(offset
, v
->p
);
556 Polyhedron_Shift(context
, v
);
560 /* Divide the generating functin by 1/(1-z^power).
561 * The effect on the corresponding explicit function f(x) is
562 * f'(x) = \sum_{i=0}^\infty f(x - i * power)
564 void gen_fun::divide(const vec_ZZ
& power
)
566 for (int i
= 0; i
< term
.size(); ++i
) {
567 int r
= term
[i
]->d
.power
.NumRows();
568 int c
= term
[i
]->d
.power
.NumCols();
569 term
[i
]->d
.power
.SetDims(r
+1, c
);
570 term
[i
]->d
.power
[r
] = power
;
573 Vector
*v
= Vector_Alloc(1+power
.length()+1);
574 value_set_si(v
->p
[0], 1);
575 zz2values(power
, v
->p
+1);
576 Polyhedron
*C
= AddRays(v
->p
, 1, context
, context
->NbConstraints
+1);
578 Polyhedron_Free(context
);
582 static void print_power(std::ostream
& os
, QQ
& c
, vec_ZZ
& p
,
583 unsigned int nparam
, char **param_name
)
587 for (int i
= 0; i
< p
.length(); ++i
) {
591 if (c
.n
== -1 && c
.d
== 1)
593 else if (c
.n
!= 1 || c
.d
!= 1) {
609 os
<< "^(" << p
[i
] << ")";
620 void gen_fun::print(std::ostream
& os
, unsigned int nparam
, char **param_name
) const
623 for (int i
= 0; i
< term
.size(); ++i
) {
627 for (int j
= 0; j
< term
[i
]->n
.coeff
.length(); ++j
) {
628 if (j
!= 0 && term
[i
]->n
.coeff
[j
].n
> 0)
630 print_power(os
, term
[i
]->n
.coeff
[j
], term
[i
]->n
.power
[j
],
634 for (int j
= 0; j
< term
[i
]->d
.power
.NumRows(); ++j
) {
638 print_power(os
, mone
, term
[i
]->d
.power
[j
], nparam
, param_name
);
645 gen_fun::operator evalue
*() const
649 value_init(factor
.d
);
650 value_init(factor
.x
.n
);
651 for (int i
= 0; i
< term
.size(); ++i
) {
652 unsigned nvar
= term
[i
]->d
.power
.NumRows();
653 unsigned nparam
= term
[i
]->d
.power
.NumCols();
654 Matrix
*C
= Matrix_Alloc(nparam
+ nvar
, 1 + nvar
+ nparam
+ 1);
655 mat_ZZ
& d
= term
[i
]->d
.power
;
656 Polyhedron
*U
= context
? context
: Universe_Polyhedron(nparam
);
658 for (int j
= 0; j
< term
[i
]->n
.coeff
.length(); ++j
) {
659 for (int r
= 0; r
< nparam
; ++r
) {
660 value_set_si(C
->p
[r
][0], 0);
661 for (int c
= 0; c
< nvar
; ++c
) {
662 zz2value(d
[c
][r
], C
->p
[r
][1+c
]);
664 Vector_Set(&C
->p
[r
][1+nvar
], 0, nparam
);
665 value_set_si(C
->p
[r
][1+nvar
+r
], -1);
666 zz2value(term
[i
]->n
.power
[j
][r
], C
->p
[r
][1+nvar
+nparam
]);
668 for (int r
= 0; r
< nvar
; ++r
) {
669 value_set_si(C
->p
[nparam
+r
][0], 1);
670 Vector_Set(&C
->p
[nparam
+r
][1], 0, nvar
+ nparam
+ 1);
671 value_set_si(C
->p
[nparam
+r
][1+r
], 1);
673 Polyhedron
*P
= Constraints2Polyhedron(C
, 0);
674 evalue
*E
= barvinok_enumerate_ev(P
, U
, 0);
676 if (EVALUE_IS_ZERO(*E
)) {
681 zz2value(term
[i
]->n
.coeff
[j
].n
, factor
.x
.n
);
682 zz2value(term
[i
]->n
.coeff
[j
].d
, factor
.d
);
685 Matrix_Print(stdout, P_VALUE_FMT, C);
686 char *test[] = { "A", "B", "C", "D", "E", "F", "G" };
687 print_evalue(stdout, E, test);
701 value_clear(factor
.d
);
702 value_clear(factor
.x
.n
);
706 void gen_fun::coefficient(Value
* params
, Value
* c
) const
708 if (context
&& !in_domain(context
, params
)) {
715 value_init(part
.x
.n
);
718 evalue_set_si(&sum
, 0, 1);
722 for (int i
= 0; i
< term
.size(); ++i
) {
723 unsigned nvar
= term
[i
]->d
.power
.NumRows();
724 unsigned nparam
= term
[i
]->d
.power
.NumCols();
725 Matrix
*C
= Matrix_Alloc(nparam
+ nvar
, 1 + nvar
+ 1);
726 mat_ZZ
& d
= term
[i
]->d
.power
;
728 for (int j
= 0; j
< term
[i
]->n
.coeff
.length(); ++j
) {
729 C
->NbRows
= nparam
+nvar
;
730 for (int r
= 0; r
< nparam
; ++r
) {
731 value_set_si(C
->p
[r
][0], 0);
732 for (int c
= 0; c
< nvar
; ++c
) {
733 zz2value(d
[c
][r
], C
->p
[r
][1+c
]);
735 zz2value(term
[i
]->n
.power
[j
][r
], C
->p
[r
][1+nvar
]);
736 value_subtract(C
->p
[r
][1+nvar
], C
->p
[r
][1+nvar
], params
[r
]);
738 for (int r
= 0; r
< nvar
; ++r
) {
739 value_set_si(C
->p
[nparam
+r
][0], 1);
740 Vector_Set(&C
->p
[nparam
+r
][1], 0, nvar
+ 1);
741 value_set_si(C
->p
[nparam
+r
][1+r
], 1);
743 Polyhedron
*P
= Constraints2Polyhedron(C
, 0);
748 barvinok_count(P
, &tmp
, 0);
750 if (value_zero_p(tmp
))
752 zz2value(term
[i
]->n
.coeff
[j
].n
, part
.x
.n
);
753 zz2value(term
[i
]->n
.coeff
[j
].d
, part
.d
);
754 value_multiply(part
.x
.n
, part
.x
.n
, tmp
);
760 assert(value_one_p(sum
.d
));
761 value_assign(*c
, sum
.x
.n
);
765 value_clear(part
.x
.n
);
767 value_clear(sum
.x
.n
);
770 gen_fun
*gen_fun::summate(int nvar
) const
772 int dim
= context
->Dimension
;
773 int nparam
= dim
- nvar
;
775 #ifdef USE_INCREMENTAL_DF
776 partial_ireducer
red(Polyhedron_Project(context
, nparam
), dim
, nparam
);
778 partial_reducer
red(Polyhedron_Project(context
, nparam
), dim
, nparam
);
781 for (int i
= 0; i
< term
.size(); ++i
)
782 for (int j
= 0; j
< term
[i
]->n
.power
.NumRows(); ++j
)
783 red
.reduce(term
[i
]->n
.coeff
[j
], term
[i
]->n
.power
[j
], term
[i
]->d
.power
);
787 /* returns true if the set was finite and false otherwise */
788 bool gen_fun::summate(Value
*sum
) const
790 if (term
.size() == 0) {
791 value_set_si(*sum
, 0);
796 for (int i
= 0; i
< term
.size(); ++i
)
797 if (term
[i
]->d
.power
.NumRows() > maxlen
)
798 maxlen
= term
[i
]->d
.power
.NumRows();
800 infinite_icounter
cnt(term
[0]->d
.power
.NumCols(), maxlen
);
801 for (int i
= 0; i
< term
.size(); ++i
)
802 for (int j
= 0; j
< term
[i
]->n
.power
.NumRows(); ++j
)
803 cnt
.reduce(term
[i
]->n
.coeff
[j
], term
[i
]->n
.power
[j
], term
[i
]->d
.power
);
805 for (int i
= 1; i
<= maxlen
; ++i
)
806 if (value_notzero_p(mpq_numref(cnt
.count
[i
]))) {
807 value_set_si(*sum
, -1);
811 assert(value_one_p(mpq_denref(cnt
.count
[0])));
812 value_assign(*sum
, mpq_numref(cnt
.count
[0]));