8 #include <NTL/mat_ZZ.h>
10 #include <barvinok/util.h>
11 #include <barvinok/evalue.h>
13 #include <barvinok/barvinok.h>
14 #include <barvinok/genfun.h>
15 #include <barvinok/options.h>
16 #include <barvinok/sample.h>
17 #include "bfcounter.h"
18 #include "conversion.h"
20 #include "decomposer.h"
22 #include "lattice_point.h"
24 #include "reduce_domain.h"
25 #include "remove_equalities.h"
28 #include "bernoulli.h"
29 #include "param_util.h"
40 using std::ostringstream
;
42 #define ALLOC(t,p) p = (t*)malloc(sizeof(*p))
44 static evalue
*barvinok_summate_unweighted(Polyhedron
*P
, Polyhedron
*C
,
45 evalue
*(*summate
)(evalue
*, unsigned, struct barvinok_options
*options
),
46 struct barvinok_options
*options
);
59 coeff
= Matrix_Alloc(d
+1, d
+1+1);
60 value_set_si(coeff
->p
[0][0], 1);
61 value_set_si(coeff
->p
[0][d
+1], 1);
62 for (int i
= 1; i
<= d
; ++i
) {
63 value_multiply(coeff
->p
[i
][0], coeff
->p
[i
-1][0], d0
);
64 Vector_Combine(coeff
->p
[i
-1], coeff
->p
[i
-1]+1, coeff
->p
[i
]+1,
66 value_set_si(coeff
->p
[i
][d
+1], i
);
67 value_multiply(coeff
->p
[i
][d
+1], coeff
->p
[i
][d
+1], coeff
->p
[i
-1][d
+1]);
68 value_decrement(d0
, d0
);
73 void div(dpoly
& d
, Vector
*count
, int sign
) {
74 int len
= coeff
->NbRows
;
75 Matrix
* c
= Matrix_Alloc(coeff
->NbRows
, coeff
->NbColumns
);
78 for (int i
= 0; i
< len
; ++i
) {
79 Vector_Copy(coeff
->p
[i
], c
->p
[i
], len
+1);
80 for (int j
= 1; j
<= i
; ++j
) {
81 value_multiply(tmp
, d
.coeff
->p
[j
], c
->p
[i
][len
]);
82 value_oppose(tmp
, tmp
);
83 Vector_Combine(c
->p
[i
], c
->p
[i
-j
], c
->p
[i
],
84 c
->p
[i
-j
][len
], tmp
, len
);
85 value_multiply(c
->p
[i
][len
], c
->p
[i
][len
], c
->p
[i
-j
][len
]);
87 value_multiply(c
->p
[i
][len
], c
->p
[i
][len
], d
.coeff
->p
[0]);
90 value_set_si(tmp
, -1);
91 Vector_Scale(c
->p
[len
-1], count
->p
, tmp
, len
);
92 value_assign(count
->p
[len
], c
->p
[len
-1][len
]);
94 Vector_Copy(c
->p
[len
-1], count
->p
, len
+1);
95 Vector_Normalize(count
->p
, len
+1);
101 const int MAX_TRY
=10;
103 * Searches for a vector that is not orthogonal to any
104 * of the rays in rays.
106 static void nonorthog(mat_ZZ
& rays
, vec_ZZ
& lambda
)
108 int dim
= rays
.NumCols();
110 lambda
.SetLength(dim
);
114 for (int i
= 2; !found
&& i
<= 50*dim
; i
+=4) {
115 for (int j
= 0; j
< MAX_TRY
; ++j
) {
116 for (int k
= 0; k
< dim
; ++k
) {
117 int r
= random_int(i
)+2;
118 int v
= (2*(r
%2)-1) * (r
>> 1);
122 for (; k
< rays
.NumRows(); ++k
)
123 if (lambda
* rays
[k
] == 0)
125 if (k
== rays
.NumRows()) {
134 static void add_rays(mat_ZZ
& rays
, Polyhedron
*i
, int *r
, int nvar
= -1,
137 unsigned dim
= i
->Dimension
;
140 for (int k
= 0; k
< i
->NbRays
; ++k
) {
141 if (!value_zero_p(i
->Ray
[k
][dim
+1]))
143 if (!all
&& nvar
!= dim
&& First_Non_Zero(i
->Ray
[k
]+1, nvar
) == -1)
145 values2zz(i
->Ray
[k
]+1, rays
[(*r
)++], nvar
);
149 struct bfe_term
: public bfc_term_base
{
150 vector
<evalue
*> factors
;
152 bfe_term(int len
) : bfc_term_base(len
) {
156 for (int i
= 0; i
< factors
.size(); ++i
) {
159 free_evalue_refs(factors
[i
]);
165 static void print_int_vector(int *v
, int len
, const char *name
)
167 cerr
<< name
<< endl
;
168 for (int j
= 0; j
< len
; ++j
) {
174 static void print_bfc_terms(mat_ZZ
& factors
, bfc_vec
& v
)
177 cerr
<< "factors" << endl
;
178 cerr
<< factors
<< endl
;
179 for (int i
= 0; i
< v
.size(); ++i
) {
180 cerr
<< "term: " << i
<< endl
;
181 print_int_vector(v
[i
]->powers
, factors
.NumRows(), "powers");
182 cerr
<< "terms" << endl
;
183 cerr
<< v
[i
]->terms
<< endl
;
184 bfc_term
* bfct
= static_cast<bfc_term
*>(v
[i
]);
185 cerr
<< bfct
->c
<< endl
;
189 static void print_bfe_terms(mat_ZZ
& factors
, bfc_vec
& v
)
192 cerr
<< "factors" << endl
;
193 cerr
<< factors
<< endl
;
194 for (int i
= 0; i
< v
.size(); ++i
) {
195 cerr
<< "term: " << i
<< endl
;
196 print_int_vector(v
[i
]->powers
, factors
.NumRows(), "powers");
197 cerr
<< "terms" << endl
;
198 cerr
<< v
[i
]->terms
<< endl
;
199 bfe_term
* bfet
= static_cast<bfe_term
*>(v
[i
]);
200 for (int j
= 0; j
< v
[i
]->terms
.NumRows(); ++j
) {
201 const char * test
[] = {"a", "b"};
202 print_evalue(stderr
, bfet
->factors
[j
], test
);
203 fprintf(stderr
, "\n");
208 struct bfcounter
: public bfcounter_base
{
212 bfcounter(unsigned dim
) : bfcounter_base(dim
) {
221 virtual void base(mat_ZZ
& factors
, bfc_vec
& v
);
222 virtual void get_count(Value
*result
) {
223 assert(value_one_p(&count
[0]._mp_den
));
224 value_assign(*result
, &count
[0]._mp_num
);
228 void bfcounter::base(mat_ZZ
& factors
, bfc_vec
& v
)
230 unsigned nf
= factors
.NumRows();
232 for (int i
= 0; i
< v
.size(); ++i
) {
233 bfc_term
* bfct
= static_cast<bfc_term
*>(v
[i
]);
235 // factor is always positive, so we always
237 for (int k
= 0; k
< nf
; ++k
)
238 total_power
+= v
[i
]->powers
[k
];
241 for (j
= 0; j
< nf
; ++j
)
242 if (v
[i
]->powers
[j
] > 0)
245 zz2value(factors
[j
][0], tz
);
246 dpoly
D(total_power
, tz
, 1);
247 for (int k
= 1; k
< v
[i
]->powers
[j
]; ++k
) {
248 zz2value(factors
[j
][0], tz
);
249 dpoly
fact(total_power
, tz
, 1);
253 for (int k
= 0; k
< v
[i
]->powers
[j
]; ++k
) {
254 zz2value(factors
[j
][0], tz
);
255 dpoly
fact(total_power
, tz
, 1);
259 for (int k
= 0; k
< v
[i
]->terms
.NumRows(); ++k
) {
260 zz2value(v
[i
]->terms
[k
][0], tz
);
261 dpoly
n(total_power
, tz
);
262 mpq_set_si(tcount
, 0, 1);
265 bfct
->c
[k
].n
= -bfct
->c
[k
].n
;
266 zz2value(bfct
->c
[k
].n
, tn
);
267 zz2value(bfct
->c
[k
].d
, td
);
269 mpz_mul(mpq_numref(tcount
), mpq_numref(tcount
), tn
);
270 mpz_mul(mpq_denref(tcount
), mpq_denref(tcount
), td
);
271 mpq_canonicalize(tcount
);
272 mpq_add(count
, count
, tcount
);
279 /* Check whether the polyhedron is unbounded and if so,
280 * check whether it has any (and therefore an infinite number of)
282 * If one of the vertices is integer, then we are done.
283 * Otherwise, transform the polyhedron such that one of the rays
284 * is the first unit vector and cut it off at a height that ensures
285 * that if the whole polyhedron has any points, then the remaining part
286 * has integer points. In particular we add the largest coefficient
287 * of a ray to the highest vertex (rounded up).
289 static bool Polyhedron_is_infinite(Polyhedron
*P
, Value
* result
,
290 barvinok_options
*options
)
302 for (; r
< P
->NbRays
; ++r
)
303 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1]))
305 if (P
->NbBid
== 0 && r
== P
->NbRays
)
308 if (options
->count_sample_infinite
) {
311 sample
= Polyhedron_Sample(P
, options
);
313 value_set_si(*result
, 0);
315 value_set_si(*result
, -1);
321 for (int i
= 0; i
< P
->NbRays
; ++i
)
322 if (value_one_p(P
->Ray
[i
][1+P
->Dimension
])) {
323 value_set_si(*result
, -1);
328 M
= Matrix_Alloc(P
->Dimension
+1, P
->Dimension
+1);
329 Vector_Gcd(P
->Ray
[r
]+1, P
->Dimension
, &g
);
330 Vector_AntiScale(P
->Ray
[r
]+1, M
->p
[0], g
, P
->Dimension
+1);
331 int ok
= unimodular_complete(M
, 1);
333 value_set_si(M
->p
[P
->Dimension
][P
->Dimension
], 1);
336 P
= Polyhedron_Preimage(P
, M2
, 0);
344 value_set_si(size
, 0);
346 for (int i
= 0; i
< P
->NbBid
; ++i
) {
347 value_absolute(tmp
, P
->Ray
[i
][1]);
348 if (value_gt(tmp
, size
))
349 value_assign(size
, tmp
);
351 for (int i
= P
->NbBid
; i
< P
->NbRays
; ++i
) {
352 if (value_zero_p(P
->Ray
[i
][P
->Dimension
+1])) {
353 if (value_gt(P
->Ray
[i
][1], size
))
354 value_assign(size
, P
->Ray
[i
][1]);
357 mpz_cdiv_q(tmp
, P
->Ray
[i
][1], P
->Ray
[i
][P
->Dimension
+1]);
358 if (first
|| value_gt(tmp
, offset
)) {
359 value_assign(offset
, tmp
);
363 value_addto(offset
, offset
, size
);
367 v
= Vector_Alloc(P
->Dimension
+2);
368 value_set_si(v
->p
[0], 1);
369 value_set_si(v
->p
[1], -1);
370 value_assign(v
->p
[1+P
->Dimension
], offset
);
371 R
= AddConstraints(v
->p
, 1, P
, options
->MaxRays
);
379 barvinok_count_with_options(P
, &c
, options
);
382 value_set_si(*result
, 0);
384 value_set_si(*result
, -1);
390 static void evalue2value(evalue
*e
, Value
*v
)
392 if (EVALUE_IS_ZERO(*e
)) {
397 if (value_notzero_p(e
->d
)) {
398 assert(value_one_p(e
->d
));
399 value_assign(*v
, e
->x
.n
);
403 assert(e
->x
.p
->type
== partition
);
404 assert(e
->x
.p
->size
== 2);
405 assert(EVALUE_DOMAIN(e
->x
.p
->arr
[0])->Dimension
== 0);
406 evalue2value(&e
->x
.p
->arr
[1], v
);
409 static void barvinok_count_f(Polyhedron
*P
, Value
* result
,
410 barvinok_options
*options
);
412 void barvinok_count_with_options(Polyhedron
*P
, Value
* result
,
413 struct barvinok_options
*options
)
418 bool infinite
= false;
422 "barvinok_count: input is a union; only first polyhedron is counted\n");
425 value_set_si(*result
, 0);
431 P
= remove_equalities(P
, options
->MaxRays
);
432 P
= DomainConstraintSimplify(P
, options
->MaxRays
);
436 } while (!emptyQ(P
) && P
->NbEq
!= 0);
439 value_set_si(*result
, 0);
444 if (Polyhedron_is_infinite(P
, result
, options
)) {
449 if (P
->Dimension
== 0) {
450 /* Test whether the constraints are satisfied */
451 POL_ENSURE_VERTICES(P
);
452 value_set_si(*result
, !emptyQ(P
));
457 if (options
->summation
== BV_SUM_BERNOULLI
) {
458 Polyhedron
*C
= Universe_Polyhedron(0);
459 evalue
*sum
= barvinok_summate_unweighted(P
, C
, Bernoulli_sum_evalue
,
462 evalue2value(sum
, result
);
466 Q
= Polyhedron_Factor(P
, 0, NULL
, options
->MaxRays
);
474 barvinok_count_f(P
, result
, options
);
475 if (value_neg_p(*result
))
477 if (Q
&& P
->next
&& value_notzero_p(*result
)) {
481 for (Q
= P
->next
; Q
; Q
= Q
->next
) {
482 barvinok_count_f(Q
, &factor
, options
);
483 if (value_neg_p(factor
)) {
486 } else if (Q
->next
&& value_zero_p(factor
)) {
487 value_set_si(*result
, 0);
490 value_multiply(*result
, *result
, factor
);
499 value_set_si(*result
, -1);
502 void barvinok_count(Polyhedron
*P
, Value
* result
, unsigned NbMaxCons
)
504 barvinok_options
*options
= barvinok_options_new_with_defaults();
505 options
->MaxRays
= NbMaxCons
;
506 barvinok_count_with_options(P
, result
, options
);
507 barvinok_options_free(options
);
510 static void barvinok_count_f(Polyhedron
*P
, Value
* result
,
511 barvinok_options
*options
)
514 value_set_si(*result
, 0);
518 if (P
->Dimension
== 1)
519 return Line_Length(P
, result
);
521 int c
= P
->NbConstraints
;
522 POL_ENSURE_FACETS(P
);
523 if (c
!= P
->NbConstraints
|| P
->NbEq
!= 0) {
524 Polyhedron
*next
= P
->next
;
526 barvinok_count_with_options(P
, result
, options
);
531 POL_ENSURE_VERTICES(P
);
533 if (Polyhedron_is_infinite(P
, result
, options
))
537 if (options
->incremental_specialization
== BV_SPECIALIZATION_BF
)
538 cnt
= new bfcounter(P
->Dimension
);
539 else if (options
->incremental_specialization
== BV_SPECIALIZATION_DF
)
540 cnt
= new icounter(P
->Dimension
);
541 else if (options
->incremental_specialization
== BV_SPECIALIZATION_TODD
)
542 cnt
= new tcounter(P
->Dimension
, options
->max_index
);
544 cnt
= new counter(P
->Dimension
, options
->max_index
);
545 cnt
->start(P
, options
);
547 cnt
->get_count(result
);
551 static void uni_polynom(int param
, Vector
*c
, evalue
*EP
)
553 unsigned dim
= c
->Size
-2;
555 value_set_si(EP
->d
,0);
556 EP
->x
.p
= new_enode(polynomial
, dim
+1, param
+1);
557 for (int j
= 0; j
<= dim
; ++j
)
558 evalue_set(&EP
->x
.p
->arr
[j
], c
->p
[j
], c
->p
[dim
+1]);
561 typedef evalue
* evalue_p
;
563 struct enumerator_base
{
567 vertex_decomposer
*vpd
;
569 enumerator_base(unsigned dim
, vertex_decomposer
*vpd
)
574 vE
= new evalue_p
[vpd
->PP
->nbV
];
575 for (int j
= 0; j
< vpd
->PP
->nbV
; ++j
)
579 evalue_set_si(&mone
, -1, 1);
582 void decompose_at(Param_Vertices
*V
, int _i
, barvinok_options
*options
) {
586 value_init(vE
[_i
]->d
);
587 evalue_set_si(vE
[_i
], 0, 1);
589 vpd
->decompose_at_vertex(V
, _i
, options
);
592 virtual ~enumerator_base() {
593 for (int j
= 0; j
< vpd
->PP
->nbV
; ++j
)
595 free_evalue_refs(vE
[j
]);
600 free_evalue_refs(&mone
);
603 static enumerator_base
*create(Polyhedron
*P
, unsigned dim
,
604 Param_Polyhedron
*PP
,
605 barvinok_options
*options
);
608 struct enumerator
: public signed_cone_consumer
, public vertex_decomposer
,
609 public enumerator_base
{
617 enumerator(Polyhedron
*P
, unsigned dim
, Param_Polyhedron
*PP
) :
618 vertex_decomposer(PP
, *this), enumerator_base(dim
, this) {
619 randomvector(P
, lambda
, dim
);
621 c
= Vector_Alloc(dim
+2);
633 virtual void handle(const signed_cone
& sc
, barvinok_options
*options
);
636 void enumerator::handle(const signed_cone
& sc
, barvinok_options
*options
)
640 assert(sc
.rays
.NumRows() == dim
);
641 for (int k
= 0; k
< dim
; ++k
) {
642 if (lambda
* sc
.rays
[k
] == 0)
646 lattice_point(V
, sc
.rays
, lambda
, &num
, sc
.det
, options
);
647 den
= sc
.rays
* lambda
;
652 zz2value(den
[0], tz
);
654 for (int k
= 1; k
< dim
; ++k
) {
655 zz2value(den
[k
], tz
);
656 dpoly
fact(dim
, tz
, 1);
662 for (unsigned long i
= 0; i
< sc
.det
; ++i
) {
663 evalue
*EV
= evalue_polynomial(c
, num
.E
[i
]);
666 free_evalue_refs(num
.E
[i
]);
671 mpq_set_si(count
, 0, 1);
672 if (num
.constant
.length() == 1) {
673 zz2value(num
.constant
[0], tz
);
675 d
.div(n
, count
, sign
);
682 for (unsigned long i
= 0; i
< sc
.det
; ++i
) {
683 value_assign(acc
, c
->p
[dim
]);
684 zz2value(num
.constant
[i
], x
);
685 for (int j
= dim
-1; j
>= 0; --j
) {
686 value_multiply(acc
, acc
, x
);
687 value_addto(acc
, acc
, c
->p
[j
]);
689 value_addto(mpq_numref(count
), mpq_numref(count
), acc
);
691 mpz_set(mpq_denref(count
), c
->p
[dim
+1]);
697 evalue_set(&EV
, &count
[0]._mp_num
, &count
[0]._mp_den
);
699 free_evalue_refs(&EV
);
703 struct ienumerator_base
: enumerator_base
{
706 ienumerator_base(unsigned dim
, vertex_decomposer
*vpd
) :
707 enumerator_base(dim
,vpd
) {
708 E_vertex
= new evalue_p
[dim
];
711 virtual ~ienumerator_base() {
715 evalue
*E_num(int i
, int d
) {
716 return E_vertex
[i
+ (dim
-d
)];
725 cumulator(evalue
*factor
, evalue
*v
, dpoly_r
*r
) :
726 factor(factor
), v(v
), r(r
) {}
728 void cumulate(barvinok_options
*options
);
730 virtual void add_term(const vector
<int>& powers
, evalue
*f2
) = 0;
731 virtual ~cumulator() {}
734 void cumulator::cumulate(barvinok_options
*options
)
736 evalue cum
; // factor * 1 * E_num[0]/1 * (E_num[0]-1)/2 *...
738 evalue t
; // E_num[0] - (m-1)
742 if (options
->lookup_table
) {
744 evalue_set_si(&mone
, -1, 1);
748 evalue_copy(&cum
, factor
);
751 value_set_si(f
.d
, 1);
752 value_set_si(f
.x
.n
, 1);
756 if (!options
->lookup_table
) {
757 for (cst
= &t
; value_zero_p(cst
->d
); ) {
758 if (cst
->x
.p
->type
== fractional
)
759 cst
= &cst
->x
.p
->arr
[1];
761 cst
= &cst
->x
.p
->arr
[0];
765 for (int m
= 0; m
< r
->len
; ++m
) {
768 value_set_si(f
.d
, m
);
770 if (!options
->lookup_table
)
771 value_subtract(cst
->x
.n
, cst
->x
.n
, cst
->d
);
777 dpoly_r_term_list
& current
= r
->c
[r
->len
-1-m
];
778 dpoly_r_term_list::iterator j
;
779 for (j
= current
.begin(); j
!= current
.end(); ++j
) {
780 if ((*j
)->coeff
== 0)
782 evalue
*f2
= new evalue
;
785 zz2value((*j
)->coeff
, f2
->x
.n
);
786 zz2value(r
->denom
, f2
->d
);
789 add_term((*j
)->powers
, f2
);
792 free_evalue_refs(&f
);
793 free_evalue_refs(&t
);
794 free_evalue_refs(&cum
);
795 if (options
->lookup_table
)
796 free_evalue_refs(&mone
);
804 struct ie_cum
: public cumulator
{
805 vector
<E_poly_term
*> terms
;
807 ie_cum(evalue
*factor
, evalue
*v
, dpoly_r
*r
) : cumulator(factor
, v
, r
) {}
809 virtual void add_term(const vector
<int>& powers
, evalue
*f2
);
812 void ie_cum::add_term(const vector
<int>& powers
, evalue
*f2
)
815 for (k
= 0; k
< terms
.size(); ++k
) {
816 if (terms
[k
]->powers
== powers
) {
817 eadd(f2
, terms
[k
]->E
);
818 free_evalue_refs(f2
);
823 if (k
>= terms
.size()) {
824 E_poly_term
*ET
= new E_poly_term
;
831 struct ienumerator
: public signed_cone_consumer
, public vertex_decomposer
,
832 public ienumerator_base
{
839 ienumerator(Polyhedron
*P
, unsigned dim
, Param_Polyhedron
*PP
) :
840 vertex_decomposer(PP
, *this), ienumerator_base(dim
, this) {
841 vertex
.SetDims(1, dim
);
843 den
.SetDims(dim
, dim
);
853 virtual void handle(const signed_cone
& sc
, barvinok_options
*options
);
854 void reduce(evalue
*factor
, const mat_ZZ
& num
, const mat_ZZ
& den_f
,
855 barvinok_options
*options
);
858 void ienumerator::reduce(evalue
*factor
, const mat_ZZ
& num
, const mat_ZZ
& den_f
,
859 barvinok_options
*options
)
861 unsigned len
= den_f
.NumRows(); // number of factors in den
862 unsigned dim
= num
.NumCols();
863 assert(num
.NumRows() == 1);
866 eadd(factor
, vE
[vert
]);
875 split_one(num
, num_s
, num_p
, den_f
, den_s
, den_r
);
878 den_p
.SetLength(len
);
882 normalize(one
, num_s
, num_p
, den_s
, den_p
, den_r
);
888 for (int k
= 0; k
< len
; ++k
) {
891 else if (den_s
[k
] == 0)
895 reduce(factor
, num_p
, den_r
, options
);
899 pden
.SetDims(only_param
, dim
-1);
901 for (k
= 0, l
= 0; k
< len
; ++k
)
903 pden
[l
++] = den_r
[k
];
905 for (k
= 0; k
< len
; ++k
)
909 zz2value(num_s
[0], tz
);
910 dpoly
n(no_param
, tz
);
911 zz2value(den_s
[k
], tz
);
912 dpoly
D(no_param
, tz
, 1);
915 zz2value(den_s
[k
], tz
);
916 dpoly
fact(no_param
, tz
, 1);
921 // if no_param + only_param == len then all powers
922 // below will be all zero
923 if (no_param
+ only_param
== len
) {
924 if (E_num(0, dim
) != 0)
925 r
= new dpoly_r(n
, len
);
927 mpq_set_si(tcount
, 0, 1);
931 if (value_notzero_p(mpq_numref(tcount
))) {
935 value_assign(f
.x
.n
, mpq_numref(tcount
));
936 value_assign(f
.d
, mpq_denref(tcount
));
938 reduce(factor
, num_p
, pden
, options
);
939 free_evalue_refs(&f
);
944 for (k
= 0; k
< len
; ++k
) {
945 if (den_s
[k
] == 0 || den_p
[k
] == 0)
948 zz2value(den_s
[k
], tz
);
949 dpoly
pd(no_param
-1, tz
, 1);
952 for (l
= 0; l
< k
; ++l
)
953 if (den_r
[l
] == den_r
[k
])
957 r
= new dpoly_r(n
, pd
, l
, len
);
959 dpoly_r
*nr
= new dpoly_r(r
, pd
, l
, len
);
965 dpoly_r
*rc
= r
->div(D
);
968 if (E_num(0, dim
) == 0) {
969 int common
= pden
.NumRows();
970 dpoly_r_term_list
& final
= r
->c
[r
->len
-1];
976 zz2value(r
->denom
, f
.d
);
977 dpoly_r_term_list::iterator j
;
978 for (j
= final
.begin(); j
!= final
.end(); ++j
) {
979 if ((*j
)->coeff
== 0)
982 for (int k
= 0; k
< r
->dim
; ++k
) {
983 int n
= (*j
)->powers
[k
];
986 pden
.SetDims(rows
+n
, pden
.NumCols());
987 for (int l
= 0; l
< n
; ++l
)
988 pden
[rows
+l
] = den_r
[k
];
992 evalue_copy(&t
, factor
);
993 zz2value((*j
)->coeff
, f
.x
.n
);
995 reduce(&t
, num_p
, pden
, options
);
996 free_evalue_refs(&t
);
998 free_evalue_refs(&f
);
1000 ie_cum
cum(factor
, E_num(0, dim
), r
);
1001 cum
.cumulate(options
);
1003 int common
= pden
.NumRows();
1005 for (int j
= 0; j
< cum
.terms
.size(); ++j
) {
1007 pden
.SetDims(rows
, pden
.NumCols());
1008 for (int k
= 0; k
< r
->dim
; ++k
) {
1009 int n
= cum
.terms
[j
]->powers
[k
];
1012 pden
.SetDims(rows
+n
, pden
.NumCols());
1013 for (int l
= 0; l
< n
; ++l
)
1014 pden
[rows
+l
] = den_r
[k
];
1017 reduce(cum
.terms
[j
]->E
, num_p
, pden
, options
);
1018 free_evalue_refs(cum
.terms
[j
]->E
);
1019 delete cum
.terms
[j
]->E
;
1020 delete cum
.terms
[j
];
1027 static int type_offset(enode
*p
)
1029 return p
->type
== fractional
? 1 :
1030 p
->type
== flooring
? 1 : 0;
1033 static int edegree(evalue
*e
)
1038 if (value_notzero_p(e
->d
))
1042 int i
= type_offset(p
);
1043 if (p
->size
-i
-1 > d
)
1044 d
= p
->size
- i
- 1;
1045 for (; i
< p
->size
; i
++) {
1046 int d2
= edegree(&p
->arr
[i
]);
1053 void ienumerator::handle(const signed_cone
& sc
, barvinok_options
*options
)
1055 assert(sc
.det
== 1);
1056 assert(sc
.rays
.NumRows() == dim
);
1058 lattice_point(V
, sc
.rays
, vertex
[0], E_vertex
, options
);
1064 evalue_set_si(&one
, sc
.sign
, 1);
1065 reduce(&one
, vertex
, den
, options
);
1066 free_evalue_refs(&one
);
1068 for (int i
= 0; i
< dim
; ++i
)
1070 evalue_free(E_vertex
[i
]);
1073 struct bfenumerator
: public vertex_decomposer
, public bf_base
,
1074 public ienumerator_base
{
1077 bfenumerator(Polyhedron
*P
, unsigned dim
, Param_Polyhedron
*PP
) :
1078 vertex_decomposer(PP
, *this),
1079 bf_base(dim
), ienumerator_base(dim
, this) {
1087 virtual void handle(const signed_cone
& sc
, barvinok_options
*options
);
1088 virtual void base(mat_ZZ
& factors
, bfc_vec
& v
);
1090 bfc_term_base
* new_bf_term(int len
) {
1091 bfe_term
* t
= new bfe_term(len
);
1095 virtual void set_factor(bfc_term_base
*t
, int k
, int change
) {
1096 bfe_term
* bfet
= static_cast<bfe_term
*>(t
);
1097 factor
= bfet
->factors
[k
];
1098 assert(factor
!= NULL
);
1099 bfet
->factors
[k
] = NULL
;
1101 emul(&mone
, factor
);
1104 virtual void set_factor(bfc_term_base
*t
, int k
, mpq_t
&q
, int change
) {
1105 bfe_term
* bfet
= static_cast<bfe_term
*>(t
);
1106 factor
= bfet
->factors
[k
];
1107 assert(factor
!= NULL
);
1108 bfet
->factors
[k
] = NULL
;
1114 value_oppose(f
.x
.n
, mpq_numref(q
));
1116 value_assign(f
.x
.n
, mpq_numref(q
));
1117 value_assign(f
.d
, mpq_denref(q
));
1119 free_evalue_refs(&f
);
1122 virtual void set_factor(bfc_term_base
*t
, int k
, const QQ
& c
, int change
) {
1123 bfe_term
* bfet
= static_cast<bfe_term
*>(t
);
1125 factor
= new evalue
;
1130 zz2value(c
.n
, f
.x
.n
);
1132 value_oppose(f
.x
.n
, f
.x
.n
);
1135 value_init(factor
->d
);
1136 evalue_copy(factor
, bfet
->factors
[k
]);
1138 free_evalue_refs(&f
);
1141 void set_factor(evalue
*f
, int change
) {
1147 virtual void insert_term(bfc_term_base
*t
, int i
) {
1148 bfe_term
* bfet
= static_cast<bfe_term
*>(t
);
1149 int len
= t
->terms
.NumRows()-1; // already increased by one
1151 bfet
->factors
.resize(len
+1);
1152 for (int j
= len
; j
> i
; --j
) {
1153 bfet
->factors
[j
] = bfet
->factors
[j
-1];
1154 t
->terms
[j
] = t
->terms
[j
-1];
1156 bfet
->factors
[i
] = factor
;
1160 virtual void update_term(bfc_term_base
*t
, int i
) {
1161 bfe_term
* bfet
= static_cast<bfe_term
*>(t
);
1163 eadd(factor
, bfet
->factors
[i
]);
1164 free_evalue_refs(factor
);
1168 virtual bool constant_vertex(int dim
) { return E_num(0, dim
) == 0; }
1170 virtual void cum(bf_reducer
*bfr
, bfc_term_base
*t
, int k
, dpoly_r
*r
,
1171 barvinok_options
*options
);
1174 enumerator_base
*enumerator_base::create(Polyhedron
*P
, unsigned dim
,
1175 Param_Polyhedron
*PP
,
1176 barvinok_options
*options
)
1178 enumerator_base
*eb
;
1180 if (options
->incremental_specialization
== BV_SPECIALIZATION_BF
)
1181 eb
= new bfenumerator(P
, dim
, PP
);
1182 else if (options
->incremental_specialization
== BV_SPECIALIZATION_DF
)
1183 eb
= new ienumerator(P
, dim
, PP
);
1185 eb
= new enumerator(P
, dim
, PP
);
1190 struct bfe_cum
: public cumulator
{
1192 bfc_term_base
*told
;
1196 bfe_cum(evalue
*factor
, evalue
*v
, dpoly_r
*r
, bf_reducer
*bfr
,
1197 bfc_term_base
*t
, int k
, bfenumerator
*e
) :
1198 cumulator(factor
, v
, r
), told(t
), k(k
),
1202 virtual void add_term(const vector
<int>& powers
, evalue
*f2
);
1205 void bfe_cum::add_term(const vector
<int>& powers
, evalue
*f2
)
1207 bfr
->update_powers(powers
);
1209 bfc_term_base
* t
= bfe
->find_bfc_term(bfr
->vn
, bfr
->npowers
, bfr
->nnf
);
1210 bfe
->set_factor(f2
, bfr
->l_changes
% 2);
1211 bfe
->add_term(t
, told
->terms
[k
], bfr
->l_extra_num
);
1214 void bfenumerator::cum(bf_reducer
*bfr
, bfc_term_base
*t
, int k
,
1215 dpoly_r
*r
, barvinok_options
*options
)
1217 bfe_term
* bfet
= static_cast<bfe_term
*>(t
);
1218 bfe_cum
cum(bfet
->factors
[k
], E_num(0, bfr
->d
), r
, bfr
, t
, k
, this);
1219 cum
.cumulate(options
);
1222 void bfenumerator::base(mat_ZZ
& factors
, bfc_vec
& v
)
1224 for (int i
= 0; i
< v
.size(); ++i
) {
1225 assert(v
[i
]->terms
.NumRows() == 1);
1226 evalue
*factor
= static_cast<bfe_term
*>(v
[i
])->factors
[0];
1227 eadd(factor
, vE
[vert
]);
1232 void bfenumerator::handle(const signed_cone
& sc
, barvinok_options
*options
)
1234 assert(sc
.det
== 1);
1235 assert(sc
.rays
.NumRows() == enumerator_base::dim
);
1237 bfe_term
* t
= new bfe_term(enumerator_base::dim
);
1238 vector
< bfc_term_base
* > v
;
1241 t
->factors
.resize(1);
1243 t
->terms
.SetDims(1, enumerator_base::dim
);
1244 lattice_point(V
, sc
.rays
, t
->terms
[0], E_vertex
, options
);
1246 // the elements of factors are always lexpositive
1248 int s
= setup_factors(sc
.rays
, factors
, t
, sc
.sign
);
1250 t
->factors
[0] = new evalue
;
1251 value_init(t
->factors
[0]->d
);
1252 evalue_set_si(t
->factors
[0], s
, 1);
1253 reduce(factors
, v
, options
);
1255 for (int i
= 0; i
< enumerator_base::dim
; ++i
)
1257 evalue_free(E_vertex
[i
]);
1260 static evalue
* barvinok_enumerate_ev_f(Polyhedron
*P
, Polyhedron
* C
,
1261 barvinok_options
*options
);
1264 static evalue
* barvinok_enumerate_cst(Polyhedron
*P
, Polyhedron
* C
,
1265 struct barvinok_options
*options
)
1271 return evalue_zero();
1274 ALLOC(evalue
, eres
);
1275 value_init(eres
->d
);
1276 value_set_si(eres
->d
, 0);
1277 eres
->x
.p
= new_enode(partition
, 2, C
->Dimension
);
1278 EVALUE_SET_DOMAIN(eres
->x
.p
->arr
[0],
1279 DomainConstraintSimplify(C
, options
->MaxRays
));
1280 value_set_si(eres
->x
.p
->arr
[1].d
, 1);
1281 value_init(eres
->x
.p
->arr
[1].x
.n
);
1283 value_set_si(eres
->x
.p
->arr
[1].x
.n
, 0);
1285 barvinok_count_with_options(P
, &eres
->x
.p
->arr
[1].x
.n
, options
);
1290 static evalue
* enumerate(Polyhedron
*P
, Polyhedron
* C
,
1291 struct barvinok_options
*options
)
1294 Polyhedron
*Porig
= P
;
1295 Polyhedron
*Corig
= C
;
1296 Polyhedron
*CEq
= NULL
, *rVD
;
1298 unsigned nparam
= C
->Dimension
;
1303 value_init(factor
.d
);
1304 evalue_set_si(&factor
, 1, 1);
1307 POL_ENSURE_FACETS(P
);
1308 POL_ENSURE_VERTICES(P
);
1309 POL_ENSURE_FACETS(C
);
1310 POL_ENSURE_VERTICES(C
);
1312 if (C
->Dimension
== 0 || emptyQ(P
) || emptyQ(C
)) {
1315 CEq
= Polyhedron_Copy(CEq
);
1316 eres
= barvinok_enumerate_cst(P
, CEq
? CEq
: Polyhedron_Copy(C
), options
);
1319 evalue_backsubstitute(eres
, CP
, options
->MaxRays
);
1323 emul(&factor
, eres
);
1324 if (options
->approximation_method
== BV_APPROX_DROP
) {
1325 if (options
->polynomial_approximation
== BV_APPROX_SIGN_UPPER
)
1326 evalue_frac2polynomial(eres
, 1, options
->MaxRays
);
1327 if (options
->polynomial_approximation
== BV_APPROX_SIGN_LOWER
)
1328 evalue_frac2polynomial(eres
, -1, options
->MaxRays
);
1329 if (options
->polynomial_approximation
== BV_APPROX_SIGN_APPROX
)
1330 evalue_frac2polynomial(eres
, 0, options
->MaxRays
);
1332 reduce_evalue(eres
);
1333 free_evalue_refs(&factor
);
1341 if (Polyhedron_is_unbounded(P
, nparam
, options
->MaxRays
))
1344 if (P
->Dimension
== nparam
) {
1346 P
= Universe_Polyhedron(0);
1349 if (P
->NbEq
!= 0 || C
->NbEq
!= 0) {
1352 remove_all_equalities(&P
, &C
, &CP
, NULL
, nparam
, options
->MaxRays
);
1353 if (C
!= D
&& D
!= Corig
)
1355 if (P
!= Q
&& Q
!= Porig
)
1357 eres
= enumerate(P
, C
, options
);
1361 Polyhedron
*T
= Polyhedron_Factor(P
, nparam
, NULL
, options
->MaxRays
);
1362 if (T
|| (P
->Dimension
== nparam
+1)) {
1365 for (Q
= T
? T
: P
; Q
; Q
= Q
->next
) {
1366 Polyhedron
*next
= Q
->next
;
1370 if (Q
->Dimension
!= C
->Dimension
)
1371 QC
= Polyhedron_Project(Q
, nparam
);
1374 C
= DomainIntersection(C
, QC
, options
->MaxRays
);
1376 Polyhedron_Free(C2
);
1378 Polyhedron_Free(QC
);
1387 if (T
->Dimension
== C
->Dimension
) {
1396 eres
= barvinok_enumerate_ev_f(P
, C
, options
);
1403 for (Q
= P
->next
; Q
; Q
= Q
->next
) {
1404 Polyhedron
*next
= Q
->next
;
1407 f
= barvinok_enumerate_ev_f(Q
, C
, options
);
1418 evalue
* barvinok_enumerate_with_options(Polyhedron
*P
, Polyhedron
* C
,
1419 struct barvinok_options
*options
)
1421 Polyhedron
*next
, *Cnext
, *C1
;
1422 Polyhedron
*Corig
= C
;
1427 "barvinok_enumerate: input is a union; only first polyhedron is enumerated\n");
1431 "barvinok_enumerate: context is a union; only first polyhedron is considered\n");
1435 C1
= Polyhedron_Project(P
, C
->Dimension
);
1436 C
= DomainIntersection(C
, C1
, options
->MaxRays
);
1437 Polyhedron_Free(C1
);
1441 if (options
->approximation_method
== BV_APPROX_BERNOULLI
||
1442 options
->summation
== BV_SUM_BERNOULLI
)
1443 eres
= barvinok_summate_unweighted(P
, C
, Bernoulli_sum_evalue
, options
);
1445 eres
= enumerate(P
, C
, options
);
1449 Corig
->next
= Cnext
;
1454 evalue
* barvinok_enumerate_ev(Polyhedron
*P
, Polyhedron
* C
, unsigned MaxRays
)
1457 barvinok_options
*options
= barvinok_options_new_with_defaults();
1458 options
->MaxRays
= MaxRays
;
1459 E
= barvinok_enumerate_with_options(P
, C
, options
);
1460 barvinok_options_free(options
);
1464 evalue
*Param_Polyhedron_Enumerate(Param_Polyhedron
*PP
, Polyhedron
*P
,
1466 struct barvinok_options
*options
)
1470 unsigned nparam
= C
->Dimension
;
1471 unsigned dim
= P
->Dimension
- nparam
;
1474 for (nd
= 0, D
=PP
->D
; D
; ++nd
, D
=D
->next
);
1475 evalue_section
*s
= new evalue_section
[nd
];
1477 enumerator_base
*et
= NULL
;
1482 et
= enumerator_base::create(P
, dim
, PP
, options
);
1484 Polyhedron
*TC
= true_context(P
, C
, options
->MaxRays
);
1485 FORALL_REDUCED_DOMAIN(PP
, TC
, nd
, options
, i
, D
, rVD
)
1488 s
[i
].E
= evalue_zero();
1491 FORALL_PVertex_in_ParamPolyhedron(V
,D
,PP
) // _i is internal counter
1494 et
->decompose_at(V
, _i
, options
);
1495 } catch (OrthogonalException
&e
) {
1496 FORALL_REDUCED_DOMAIN_RESET
;
1497 for (; i
>= 0; --i
) {
1498 evalue_free(s
[i
].E
);
1499 Domain_Free(s
[i
].D
);
1503 eadd(et
->vE
[_i
] , s
[i
].E
);
1504 END_FORALL_PVertex_in_ParamPolyhedron
;
1505 evalue_range_reduction_in_domain(s
[i
].E
, rVD
);
1506 END_FORALL_REDUCED_DOMAIN
1507 Polyhedron_Free(TC
);
1510 eres
= evalue_from_section_array(s
, nd
);
1516 static evalue
* barvinok_enumerate_ev_f(Polyhedron
*P
, Polyhedron
* C
,
1517 barvinok_options
*options
)
1519 unsigned nparam
= C
->Dimension
;
1520 bool do_scale
= options
->approximation_method
== BV_APPROX_SCALE
;
1522 if (options
->summation
== BV_SUM_EULER
)
1523 return barvinok_summate_unweighted(P
, C
, euler_summate
, options
);
1525 if (options
->approximation_method
== BV_APPROX_VOLUME
)
1526 return Param_Polyhedron_Volume(P
, C
, options
);
1528 if (P
->Dimension
- nparam
== 1 && !do_scale
)
1529 return ParamLine_Length(P
, C
, options
);
1531 Param_Polyhedron
*PP
= NULL
;
1535 eres
= scale_bound(P
, C
, options
);
1540 PP
= Polyhedron2Param_Polyhedron(P
, C
, options
);
1543 eres
= scale(PP
, P
, C
, options
);
1545 eres
= Param_Polyhedron_Enumerate(PP
, P
, C
, options
);
1548 Param_Polyhedron_Free(PP
);
1553 Enumeration
* barvinok_enumerate(Polyhedron
*P
, Polyhedron
* C
, unsigned MaxRays
)
1555 evalue
*EP
= barvinok_enumerate_ev(P
, C
, MaxRays
);
1557 return partition2enumeration(EP
);
1560 evalue
* barvinok_enumerate_union(Polyhedron
*D
, Polyhedron
* C
, unsigned MaxRays
)
1563 gen_fun
*gf
= barvinok_enumerate_union_series(D
, C
, MaxRays
);
1569 evalue
*barvinok_summate(evalue
*e
, int nvar
, struct barvinok_options
*options
)
1571 if (options
->summation
== BV_SUM_EULER
)
1572 return euler_summate(e
, nvar
, options
);
1573 else if (options
->summation
== BV_SUM_LAURENT
)
1574 return laurent_summate(e
, nvar
, options
);
1575 else if (options
->summation
== BV_SUM_BERNOULLI
)
1576 return Bernoulli_sum_evalue(e
, nvar
, options
);
1578 return evalue_sum(e
, nvar
, options
->MaxRays
);
1581 /* Turn unweighted counting problem into "weighted" counting problem
1582 * with weight equal to 1 and call "summate" on this weighted problem.
1584 static evalue
*barvinok_summate_unweighted(Polyhedron
*P
, Polyhedron
*C
,
1585 evalue
*(*summate
)(evalue
*, unsigned, struct barvinok_options
*options
),
1586 struct barvinok_options
*options
)
1592 if (emptyQ(P
) || emptyQ(C
))
1593 return evalue_zero();
1595 CA
= align_context(C
, P
->Dimension
, options
->MaxRays
);
1596 D
= DomainIntersection(P
, CA
, options
->MaxRays
);
1601 return evalue_zero();
1605 e
.x
.p
= new_enode(partition
, 2, P
->Dimension
);
1606 EVALUE_SET_DOMAIN(e
.x
.p
->arr
[0], D
);
1607 evalue_set_si(&e
.x
.p
->arr
[1], 1, 1);
1608 sum
= summate(&e
, P
->Dimension
- C
->Dimension
, options
);
1609 free_evalue_refs(&e
);