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 /* Extend the context of "this" to include the one of "gf".
255 void gen_fun::extend_context(const gen_fun
*gf
, barvinok_options
*options
)
257 Polyhedron
*U
= DomainUnion(context
, gf
->context
, options
->MaxRays
);
258 Polyhedron
*C
= DomainConvex(U
, options
->MaxRays
);
260 Domain_Free(context
);
264 /* Add the generating "gf" to "this" on the union of their domains.
266 void gen_fun::add(const QQ
& c
, const gen_fun
*gf
, barvinok_options
*options
)
268 extend_context(gf
, options
);
272 void gen_fun::add(const QQ
& c
, const gen_fun
*gf
)
275 for (short_rat_list::iterator i
= gf
->term
.begin(); i
!= gf
->term
.end(); ++i
) {
276 for (int j
= 0; j
< (*i
)->n
.power
.NumRows(); ++j
) {
278 p
*= (*i
)->n
.coeff
[j
];
279 add(p
, (*i
)->n
.power
[j
], (*i
)->d
.power
);
284 static void split_param_compression(Matrix
*CP
, mat_ZZ
& map
, vec_ZZ
& offset
)
286 Matrix
*T
= Transpose(CP
);
287 matrix2zz(T
, map
, T
->NbRows
-1, T
->NbColumns
-1);
288 values2zz(T
->p
[T
->NbRows
-1], offset
, T
->NbColumns
-1);
293 * Perform the substitution specified by CP
295 * CP is a homogeneous matrix that maps a set of "compressed parameters"
296 * to the original set of parameters.
298 * This function is applied to a gen_fun computed with the compressed parameters
299 * and adapts it to refer to the original parameters.
301 * That is, if y are the compressed parameters and x = A y + b are the original
302 * parameters, then we want the coefficient of the monomial t^y in the original
303 * generating function to be the coefficient of the monomial u^x in the resulting
304 * generating function.
305 * The original generating function has the form
307 * a t^m/(1-t^n) = a t^m + a t^{m+n} + a t^{m+2n} + ...
309 * Since each term t^y should correspond to a term u^x, with x = A y + b, we want
311 * a u^{A m + b} + a u^{A (m+n) + b} + a u^{A (m+2n) +b} + ... =
313 * = a u^{A m + b}/(1-u^{A n})
315 * Therefore, we multiply the powers m and n in both numerator and denominator by A
316 * and add b to the power in the numerator.
317 * Since the above powers are stored as row vectors m^T and n^T,
318 * we compute, say, m'^T = m^T A^T to obtain m' = A m.
320 * The pair (map, offset) contains the same information as CP.
321 * map is the transpose of the linear part of CP, while offset is the constant part.
323 void gen_fun::substitute(Matrix
*CP
)
327 split_param_compression(CP
, map
, offset
);
328 Polyhedron
*C
= Polyhedron_Image(context
, CP
, 0);
329 Polyhedron_Free(context
);
332 short_rat_list new_term
;
333 for (short_rat_list::iterator i
= term
.begin(); i
!= term
.end(); ++i
) {
337 for (int j
= 0; j
< r
->n
.power
.NumRows(); ++j
)
338 r
->n
.power
[j
] += offset
;
345 static int Matrix_Equal(Matrix
*M1
, Matrix
*M2
)
349 if (M1
->NbRows
!= M2
->NbRows
)
351 if (M1
->NbColumns
!= M2
->NbColumns
)
353 for (i
= 0; i
< M1
->NbRows
; ++i
)
354 for (j
= 0; j
< M1
->NbColumns
; ++j
)
355 if (value_ne(M1
->p
[i
][j
], M2
->p
[i
][j
]))
361 struct parallel_cones
{
363 vector
<pair
<Vector
*, QQ
> > vertices
;
364 parallel_cones(int *pos
) : pos(pos
) {}
367 /* This structure helps in computing the generating functions
368 * of polytopes with pairwise parallel hyperplanes more efficiently.
369 * These occur when computing hadamard products of pairs of generating
370 * functions with the same denominators.
371 * If there are many such pairs then the same vertex cone
372 * may appear more than once. We therefore keep a list of all
373 * vertex cones and only compute the corresponding generating function
375 * However, even HPs of generating functions with the same denominators
376 * can result in polytopes of different "shapes", making them incomparable.
377 * In particular, they can have different equalities among the parameters
378 * and the variables. In such cases, only polytopes of the first "shape"
379 * that is encountered are kept in this way. The others are handled
380 * in the usual, non-optimized way.
382 struct parallel_polytopes
{
389 unsigned reduced_nparam
;
390 vector
<parallel_cones
> cones
;
391 barvinok_options
*options
;
393 parallel_polytopes(int n
, Polyhedron
*context
, int nparam
,
394 barvinok_options
*options
) :
395 context(context
), dim(-1), nparam(nparam
),
402 bool add(const QQ
& c
, Polyhedron
*P
) {
405 for (i
= 0; i
< P
->NbEq
; ++i
)
406 if (First_Non_Zero(P
->Constraint
[i
]+1,
407 P
->Dimension
-nparam
) == -1)
412 Polyhedron
*Q
= remove_equalities_p(Polyhedron_Copy(P
), P
->Dimension
-nparam
,
413 NULL
, options
->MaxRays
);
414 POL_ENSURE_VERTICES(Q
);
420 if (Q
->Dimension
== 0) {
429 remove_all_equalities(&Q
, NULL
, &Q_CP
, NULL
, nparam
,
432 POL_ENSURE_VERTICES(Q
);
433 if (emptyQ(Q
) || Q
->NbEq
> 0 || Q
->Dimension
== 0) {
442 if ((!CP
^ !Q_CP
) || (CP
&& !Matrix_Equal(CP
, Q_CP
))) {
451 T
= align_matrix(CP
, R
->Dimension
+1);
454 reduced_nparam
= CP
->NbColumns
-1;
461 reduced_nparam
= nparam
;
464 if (First_Non_Zero(Q
->Constraint
[Q
->NbConstraints
-1]+1, Q
->Dimension
) == -1)
468 Polyhedron
*reduced_context
;
471 reduced_context
= Polyhedron_Preimage(context
, CP
, options
->MaxRays
);
473 reduced_context
= Polyhedron_Copy(context
);
474 red
= gf_base::create(reduced_context
, dim
, reduced_nparam
, options
);
475 red
->base
->init(Q
, 0);
476 Constraints
= Matrix_Alloc(Q
->NbConstraints
, Q
->Dimension
);
477 for (int i
= 0; i
< Q
->NbConstraints
; ++i
) {
478 Vector_Copy(Q
->Constraint
[i
]+1, Constraints
->p
[i
], Q
->Dimension
);
481 if (Q
->Dimension
!= dim
) {
485 assert(Q
->Dimension
== dim
);
486 for (int i
= 0; i
< Q
->NbConstraints
; ++i
) {
488 for (j
= 0; j
< Constraints
->NbRows
; ++j
)
489 if (Vector_Equal(Q
->Constraint
[i
]+1, Constraints
->p
[j
],
492 if (j
>= Constraints
->NbRows
) {
493 Matrix_Extend(Constraints
, Constraints
->NbRows
+1);
494 Vector_Copy(Q
->Constraint
[i
]+1,
495 Constraints
->p
[Constraints
->NbRows
-1],
501 for (int i
= 0; i
< Q
->NbRays
; ++i
) {
502 if (!value_pos_p(Q
->Ray
[i
][dim
+1]))
505 Polyhedron
*C
= supporting_cone(Q
, i
);
507 if (First_Non_Zero(C
->Constraint
[C
->NbConstraints
-1]+1,
511 int *pos
= new int[1+C
->NbConstraints
];
513 for (int k
= 0; k
< Constraints
->NbRows
; ++k
) {
514 for (int j
= 0; j
< C
->NbConstraints
; ++j
) {
515 if (Vector_Equal(C
->Constraint
[j
]+1, Constraints
->p
[k
],
525 for (j
= 0; j
< cones
.size(); ++j
)
526 if (!memcmp(pos
, cones
[j
].pos
, (1+C
->NbConstraints
)*sizeof(int)))
528 if (j
== cones
.size())
529 cones
.push_back(parallel_cones(pos
));
536 for (k
= 0; k
< cones
[j
].vertices
.size(); ++k
)
537 if (Vector_Equal(Q
->Ray
[i
]+1, cones
[j
].vertices
[k
].first
->p
,
541 if (k
== cones
[j
].vertices
.size()) {
542 Vector
*vertex
= Vector_Alloc(Q
->Dimension
+1);
543 Vector_Copy(Q
->Ray
[i
]+1, vertex
->p
, Q
->Dimension
+1);
544 cones
[j
].vertices
.push_back(pair
<Vector
*,QQ
>(vertex
, c
));
546 cones
[j
].vertices
[k
].second
+= c
;
547 if (cones
[j
].vertices
[k
].second
.n
== 0) {
548 int size
= cones
[j
].vertices
.size();
549 Vector_Free(cones
[j
].vertices
[k
].first
);
551 cones
[j
].vertices
[k
] = cones
[j
].vertices
[size
-1];
552 cones
[j
].vertices
.pop_back();
563 for (int i
= 0; i
< cones
.size(); ++i
) {
564 Matrix
*M
= Matrix_Alloc(cones
[i
].pos
[0], 1+Constraints
->NbColumns
+1);
566 for (int j
= 0; j
<cones
[i
].pos
[0]; ++j
) {
567 value_set_si(M
->p
[j
][0], 1);
568 Vector_Copy(Constraints
->p
[cones
[i
].pos
[1+j
]], M
->p
[j
]+1,
569 Constraints
->NbColumns
);
571 Cone
= Constraints2Polyhedron(M
, options
->MaxRays
);
573 for (int j
= 0; j
< cones
[i
].vertices
.size(); ++j
) {
574 red
->base
->do_vertex_cone(cones
[i
].vertices
[j
].second
,
575 Polyhedron_Copy(Cone
),
576 cones
[i
].vertices
[j
].first
->p
, options
);
578 Polyhedron_Free(Cone
);
581 red
->gf
->substitute(CP
);
584 void print(std::ostream
& os
) const {
585 for (int i
= 0; i
< cones
.size(); ++i
) {
587 for (int j
= 0; j
< cones
[i
].pos
[0]; ++j
) {
590 os
<< cones
[i
].pos
[1+j
];
593 for (int j
= 0; j
< cones
[i
].vertices
.size(); ++j
) {
594 Vector_Print(stderr
, P_VALUE_FMT
, cones
[i
].vertices
[j
].first
);
595 os
<< cones
[i
].vertices
[j
].second
<< endl
;
599 ~parallel_polytopes() {
600 for (int i
= 0; i
< cones
.size(); ++i
) {
601 delete [] cones
[i
].pos
;
602 for (int j
= 0; j
< cones
[i
].vertices
.size(); ++j
)
603 Vector_Free(cones
[i
].vertices
[j
].first
);
606 Matrix_Free(Constraints
);
615 gen_fun
*gen_fun::Hadamard_product(const gen_fun
*gf
, barvinok_options
*options
)
618 Polyhedron
*C
= DomainIntersection(context
, gf
->context
, options
->MaxRays
);
619 gen_fun
*sum
= new gen_fun(C
);
622 for (short_rat_list::iterator i
= term
.begin(); i
!= term
.end(); ++i
, j
++) {
624 for (short_rat_list::iterator i2
= gf
->term
.begin();
625 i2
!= gf
->term
.end();
627 int d
= (*i
)->d
.power
.NumCols();
628 int k1
= (*i
)->d
.power
.NumRows();
629 int k2
= (*i2
)->d
.power
.NumRows();
630 assert((*i
)->d
.power
.NumCols() == (*i2
)->d
.power
.NumCols());
632 if (options
->verbose
)
633 fprintf(stderr
, "HP: %d/%zd %d/%zd \r",
634 j
, term
.size(), k
, gf
->term
.size());
636 parallel_polytopes
pp((*i
)->n
.power
.NumRows() *
637 (*i2
)->n
.power
.NumRows(),
638 sum
->context
, d
, options
);
640 for (int j
= 0; j
< (*i
)->n
.power
.NumRows(); ++j
) {
641 for (int j2
= 0; j2
< (*i2
)->n
.power
.NumRows(); ++j2
) {
642 Matrix
*M
= Matrix_Alloc(k1
+k2
+d
+d
, 1+k1
+k2
+d
+1);
643 for (int k
= 0; k
< k1
+k2
; ++k
) {
644 value_set_si(M
->p
[k
][0], 1);
645 value_set_si(M
->p
[k
][1+k
], 1);
647 for (int k
= 0; k
< d
; ++k
) {
648 value_set_si(M
->p
[k1
+k2
+k
][1+k1
+k2
+k
], -1);
649 zz2value((*i
)->n
.power
[j
][k
], M
->p
[k1
+k2
+k
][1+k1
+k2
+d
]);
650 for (int l
= 0; l
< k1
; ++l
)
651 zz2value((*i
)->d
.power
[l
][k
], M
->p
[k1
+k2
+k
][1+l
]);
653 for (int k
= 0; k
< d
; ++k
) {
654 value_set_si(M
->p
[k1
+k2
+d
+k
][1+k1
+k2
+k
], -1);
655 zz2value((*i2
)->n
.power
[j2
][k
],
656 M
->p
[k1
+k2
+d
+k
][1+k1
+k2
+d
]);
657 for (int l
= 0; l
< k2
; ++l
)
658 zz2value((*i2
)->d
.power
[l
][k
],
659 M
->p
[k1
+k2
+d
+k
][1+k1
+l
]);
661 Polyhedron
*P
= Constraints2Polyhedron(M
, options
->MaxRays
);
664 QQ c
= (*i
)->n
.coeff
[j
];
665 c
*= (*i2
)->n
.coeff
[j2
];
667 gen_fun
*t
= barvinok_enumerate_series(P
, C
->Dimension
, options
);
676 gen_fun
*t
= pp
.compute();
686 /* Assuming "this" and "gf" are generating functions for sets,
687 * replace "this" by the generating function for the union of these sets.
689 * In particular, compute
691 * this + gf - gen_fun(intersection of sets)
693 void gen_fun::add_union(gen_fun
*gf
, barvinok_options
*options
)
695 QQ
one(1, 1), mone(-1, 1);
697 gen_fun
*hp
= Hadamard_product(gf
, options
);
698 extend_context(gf
, options
);
704 static void Polyhedron_Shift(Polyhedron
*P
, Vector
*offset
)
708 for (int i
= 0; i
< P
->NbConstraints
; ++i
) {
709 Inner_Product(P
->Constraint
[i
]+1, offset
->p
, P
->Dimension
, &tmp
);
710 value_subtract(P
->Constraint
[i
][1+P
->Dimension
],
711 P
->Constraint
[i
][1+P
->Dimension
], tmp
);
713 for (int i
= 0; i
< P
->NbRays
; ++i
) {
714 if (value_notone_p(P
->Ray
[i
][0]))
716 if (value_zero_p(P
->Ray
[i
][1+P
->Dimension
]))
718 Vector_Combine(P
->Ray
[i
]+1, offset
->p
, P
->Ray
[i
]+1,
719 P
->Ray
[i
][0], P
->Ray
[i
][1+P
->Dimension
], P
->Dimension
);
724 void gen_fun::shift(const vec_ZZ
& offset
)
726 for (short_rat_list::iterator i
= term
.begin(); i
!= term
.end(); ++i
)
727 for (int j
= 0; j
< (*i
)->n
.power
.NumRows(); ++j
)
728 (*i
)->n
.power
[j
] += offset
;
730 Vector
*v
= Vector_Alloc(offset
.length());
731 zz2values(offset
, v
->p
);
732 Polyhedron_Shift(context
, v
);
736 /* Divide the generating functin by 1/(1-z^power).
737 * The effect on the corresponding explicit function f(x) is
738 * f'(x) = \sum_{i=0}^\infty f(x - i * power)
740 void gen_fun::divide(const vec_ZZ
& power
)
742 for (short_rat_list::iterator i
= term
.begin(); i
!= term
.end(); ++i
) {
743 int r
= (*i
)->d
.power
.NumRows();
744 int c
= (*i
)->d
.power
.NumCols();
745 (*i
)->d
.power
.SetDims(r
+1, c
);
746 (*i
)->d
.power
[r
] = power
;
749 Vector
*v
= Vector_Alloc(1+power
.length()+1);
750 value_set_si(v
->p
[0], 1);
751 zz2values(power
, v
->p
+1);
752 Polyhedron
*C
= AddRays(v
->p
, 1, context
, context
->NbConstraints
+1);
754 Polyhedron_Free(context
);
758 static void print_power(std::ostream
& os
, const QQ
& c
, const vec_ZZ
& p
,
759 unsigned int nparam
, const char **param_name
)
763 for (int i
= 0; i
< p
.length(); ++i
) {
767 if (c
.n
== -1 && c
.d
== 1)
769 else if (c
.n
!= 1 || c
.d
!= 1) {
785 os
<< "^(" << p
[i
] << ")";
796 void short_rat::print(std::ostream
& os
, unsigned int nparam
,
797 const char **param_name
) const
801 for (int j
= 0; j
< n
.coeff
.length(); ++j
) {
802 if (j
!= 0 && n
.coeff
[j
].n
>= 0)
804 print_power(os
, n
.coeff
[j
], n
.power
[j
], nparam
, param_name
);
807 if (d
.power
.NumRows() == 0)
810 for (int j
= 0; j
< d
.power
.NumRows(); ++j
) {
814 print_power(os
, mone
, d
.power
[j
], nparam
, param_name
);
820 void gen_fun::print(std::ostream
& os
, unsigned int nparam
,
821 const char **param_name
) const
823 for (short_rat_list::iterator i
= term
.begin(); i
!= term
.end(); ++i
) {
824 if (i
!= term
.begin())
826 (*i
)->print(os
, nparam
, param_name
);
830 std::ostream
& operator<< (std::ostream
& os
, const short_rat
& r
)
832 os
<< r
.n
.coeff
<< endl
;
833 os
<< r
.n
.power
<< endl
;
834 os
<< r
.d
.power
<< endl
;
838 extern "C" { typedef void (*gmp_free_t
)(void *, size_t); }
840 std::ostream
& operator<< (std::ostream
& os
, const Polyhedron
& P
)
844 mp_get_memory_functions(NULL
, NULL
, &gmp_free
);
845 os
<< P
.NbConstraints
<< " " << P
.Dimension
+2 << endl
;
846 for (int i
= 0; i
< P
.NbConstraints
; ++i
) {
847 for (int j
= 0; j
< P
.Dimension
+2; ++j
) {
848 str
= mpz_get_str(0, 10, P
.Constraint
[i
][j
]);
849 os
<< std::setw(4) << str
<< " ";
850 (*gmp_free
)(str
, strlen(str
)+1);
857 std::ostream
& operator<< (std::ostream
& os
, const gen_fun
& gf
)
859 os
<< *gf
.context
<< endl
;
861 os
<< gf
.term
.size() << endl
;
862 for (short_rat_list::iterator i
= gf
.term
.begin(); i
!= gf
.term
.end(); ++i
)
867 gen_fun
*gen_fun::read(std::istream
& is
, barvinok_options
*options
)
869 Matrix
*M
= Matrix_Read(is
);
870 Polyhedron
*C
= Constraints2Polyhedron(M
, options
->MaxRays
);
873 gen_fun
*gf
= new gen_fun(C
);
881 for (int i
= 0; i
< n
; ++i
) {
882 is
>> c
>> num
>> den
;
883 gf
->add(new short_rat(c
, num
, den
));
889 gen_fun::operator evalue
*() const
893 value_init(factor
.d
);
894 value_init(factor
.x
.n
);
895 for (short_rat_list::iterator i
= term
.begin(); i
!= term
.end(); ++i
) {
896 unsigned nvar
= (*i
)->d
.power
.NumRows();
897 unsigned nparam
= (*i
)->d
.power
.NumCols();
898 Matrix
*C
= Matrix_Alloc(nparam
+ nvar
, 1 + nvar
+ nparam
+ 1);
899 mat_ZZ
& d
= (*i
)->d
.power
;
900 Polyhedron
*U
= context
;
902 for (int j
= 0; j
< (*i
)->n
.coeff
.length(); ++j
) {
903 for (int r
= 0; r
< nparam
; ++r
) {
904 value_set_si(C
->p
[r
][0], 0);
905 for (int c
= 0; c
< nvar
; ++c
) {
906 zz2value(d
[c
][r
], C
->p
[r
][1+c
]);
908 Vector_Set(&C
->p
[r
][1+nvar
], 0, nparam
);
909 value_set_si(C
->p
[r
][1+nvar
+r
], -1);
910 zz2value((*i
)->n
.power
[j
][r
], C
->p
[r
][1+nvar
+nparam
]);
912 for (int r
= 0; r
< nvar
; ++r
) {
913 value_set_si(C
->p
[nparam
+r
][0], 1);
914 Vector_Set(&C
->p
[nparam
+r
][1], 0, nvar
+ nparam
+ 1);
915 value_set_si(C
->p
[nparam
+r
][1+r
], 1);
917 Polyhedron
*P
= Constraints2Polyhedron(C
, 0);
918 evalue
*E
= barvinok_enumerate_ev(P
, U
, 0);
920 if (EVALUE_IS_ZERO(*E
)) {
924 zz2value((*i
)->n
.coeff
[j
].n
, factor
.x
.n
);
925 zz2value((*i
)->n
.coeff
[j
].d
, factor
.d
);
936 value_clear(factor
.d
);
937 value_clear(factor
.x
.n
);
938 return EP
? EP
: evalue_zero();
941 ZZ
gen_fun::coefficient(Value
* params
, barvinok_options
*options
) const
943 if (!in_domain(context
, params
))
948 for (short_rat_list::iterator i
= term
.begin(); i
!= term
.end(); ++i
)
949 sum
+= (*i
)->coefficient(params
, options
);
955 void gen_fun::coefficient(Value
* params
, Value
* c
) const
957 barvinok_options
*options
= barvinok_options_new_with_defaults();
959 ZZ coeff
= coefficient(params
, options
);
963 barvinok_options_free(options
);
966 gen_fun
*gen_fun::summate(int nvar
, barvinok_options
*options
) const
969 int dim
= context
->Dimension
;
970 int nparam
= dim
- nvar
;
978 finite
= summate(&c
);
985 if (options
->incremental_specialization
== 1) {
986 red
= new partial_ireducer(Polyhedron_Project(context
, nparam
), dim
, nparam
);
988 red
= new partial_reducer(Polyhedron_Project(context
, nparam
), dim
, nparam
);
992 red
->init(context
, n_try
);
993 for (short_rat_list::iterator i
= term
.begin(); i
!= term
.end(); ++i
)
994 red
->reduce((*i
)->n
.coeff
, (*i
)->n
.power
, (*i
)->d
.power
);
996 } catch (OrthogonalException
&e
) {
1006 /* returns true if the set was finite and false otherwise */
1007 bool gen_fun::summate(Value
*sum
) const
1009 if (term
.size() == 0) {
1010 value_set_si(*sum
, 0);
1015 for (short_rat_list::iterator i
= term
.begin(); i
!= term
.end(); ++i
)
1016 if ((*i
)->d
.power
.NumRows() > maxlen
)
1017 maxlen
= (*i
)->d
.power
.NumRows();
1019 infinite_counter
cnt((*term
.begin())->d
.power
.NumCols(), maxlen
);
1020 cnt
.init(context
, 0);
1021 for (short_rat_list::iterator i
= term
.begin(); i
!= term
.end(); ++i
)
1022 cnt
.reduce((*i
)->n
.coeff
, (*i
)->n
.power
, (*i
)->d
.power
);
1024 for (int i
= 1; i
<= maxlen
; ++i
)
1025 if (value_notzero_p(mpq_numref(cnt
.count
[i
]))) {
1026 value_set_si(*sum
, -1);
1030 assert(value_one_p(mpq_denref(cnt
.count
[0])));
1031 value_assign(*sum
, mpq_numref(cnt
.count
[0]));
1035 bool gen_fun::is_zero() const
1042 empty
= summate(&c
) && value_zero_p(c
);