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"
41 using std::ostringstream
;
43 #define ALLOC(t,p) p = (t*)malloc(sizeof(*p))
56 coeff
= Matrix_Alloc(d
+1, d
+1+1);
57 value_set_si(coeff
->p
[0][0], 1);
58 value_set_si(coeff
->p
[0][d
+1], 1);
59 for (int i
= 1; i
<= d
; ++i
) {
60 value_multiply(coeff
->p
[i
][0], coeff
->p
[i
-1][0], d0
);
61 Vector_Combine(coeff
->p
[i
-1], coeff
->p
[i
-1]+1, coeff
->p
[i
]+1,
63 value_set_si(coeff
->p
[i
][d
+1], i
);
64 value_multiply(coeff
->p
[i
][d
+1], coeff
->p
[i
][d
+1], coeff
->p
[i
-1][d
+1]);
65 value_decrement(d0
, d0
);
70 void div(dpoly
& d
, Vector
*count
, int sign
) {
71 int len
= coeff
->NbRows
;
72 Matrix
* c
= Matrix_Alloc(coeff
->NbRows
, coeff
->NbColumns
);
75 for (int i
= 0; i
< len
; ++i
) {
76 Vector_Copy(coeff
->p
[i
], c
->p
[i
], len
+1);
77 for (int j
= 1; j
<= i
; ++j
) {
78 value_multiply(tmp
, d
.coeff
->p
[j
], c
->p
[i
][len
]);
79 value_oppose(tmp
, tmp
);
80 Vector_Combine(c
->p
[i
], c
->p
[i
-j
], c
->p
[i
],
81 c
->p
[i
-j
][len
], tmp
, len
);
82 value_multiply(c
->p
[i
][len
], c
->p
[i
][len
], c
->p
[i
-j
][len
]);
84 value_multiply(c
->p
[i
][len
], c
->p
[i
][len
], d
.coeff
->p
[0]);
87 value_set_si(tmp
, -1);
88 Vector_Scale(c
->p
[len
-1], count
->p
, tmp
, len
);
89 value_assign(count
->p
[len
], c
->p
[len
-1][len
]);
91 Vector_Copy(c
->p
[len
-1], count
->p
, len
+1);
92 Vector_Normalize(count
->p
, len
+1);
98 static void add_rays(mat_ZZ
& rays
, Polyhedron
*i
, int *r
, int nvar
= -1,
101 unsigned dim
= i
->Dimension
;
104 for (int k
= 0; k
< i
->NbRays
; ++k
) {
105 if (!value_zero_p(i
->Ray
[k
][dim
+1]))
107 if (!all
&& nvar
!= dim
&& First_Non_Zero(i
->Ray
[k
]+1, nvar
) == -1)
109 values2zz(i
->Ray
[k
]+1, rays
[(*r
)++], nvar
);
113 struct bfe_term
: public bfc_term_base
{
114 vector
<evalue
*> factors
;
116 bfe_term(int len
) : bfc_term_base(len
) {
120 for (int i
= 0; i
< factors
.size(); ++i
) {
123 free_evalue_refs(factors
[i
]);
129 static void print_int_vector(int *v
, int len
, const char *name
)
131 cerr
<< name
<< endl
;
132 for (int j
= 0; j
< len
; ++j
) {
138 static void print_bfc_terms(mat_ZZ
& factors
, bfc_vec
& v
)
141 cerr
<< "factors" << endl
;
142 cerr
<< factors
<< endl
;
143 for (int i
= 0; i
< v
.size(); ++i
) {
144 cerr
<< "term: " << i
<< endl
;
145 print_int_vector(v
[i
]->powers
, factors
.NumRows(), "powers");
146 cerr
<< "terms" << endl
;
147 cerr
<< v
[i
]->terms
<< endl
;
148 bfc_term
* bfct
= static_cast<bfc_term
*>(v
[i
]);
149 cerr
<< bfct
->c
<< endl
;
153 static void print_bfe_terms(mat_ZZ
& factors
, bfc_vec
& v
)
156 cerr
<< "factors" << endl
;
157 cerr
<< factors
<< endl
;
158 for (int i
= 0; i
< v
.size(); ++i
) {
159 cerr
<< "term: " << i
<< endl
;
160 print_int_vector(v
[i
]->powers
, factors
.NumRows(), "powers");
161 cerr
<< "terms" << endl
;
162 cerr
<< v
[i
]->terms
<< endl
;
163 bfe_term
* bfet
= static_cast<bfe_term
*>(v
[i
]);
164 for (int j
= 0; j
< v
[i
]->terms
.NumRows(); ++j
) {
165 const char * test
[] = {"a", "b"};
166 print_evalue(stderr
, bfet
->factors
[j
], test
);
167 fprintf(stderr
, "\n");
172 struct bfcounter
: public bfcounter_base
{
176 bfcounter(unsigned dim
) : bfcounter_base(dim
) {
185 virtual void base(mat_ZZ
& factors
, bfc_vec
& v
);
186 virtual void get_count(Value
*result
) {
187 assert(value_one_p(&count
[0]._mp_den
));
188 value_assign(*result
, &count
[0]._mp_num
);
192 void bfcounter::base(mat_ZZ
& factors
, bfc_vec
& v
)
194 unsigned nf
= factors
.NumRows();
196 for (int i
= 0; i
< v
.size(); ++i
) {
197 bfc_term
* bfct
= static_cast<bfc_term
*>(v
[i
]);
199 // factor is always positive, so we always
201 for (int k
= 0; k
< nf
; ++k
)
202 total_power
+= v
[i
]->powers
[k
];
205 for (j
= 0; j
< nf
; ++j
)
206 if (v
[i
]->powers
[j
] > 0)
209 zz2value(factors
[j
][0], tz
);
210 dpoly
D(total_power
, tz
, 1);
211 for (int k
= 1; k
< v
[i
]->powers
[j
]; ++k
) {
212 zz2value(factors
[j
][0], tz
);
213 dpoly
fact(total_power
, tz
, 1);
217 for (int k
= 0; k
< v
[i
]->powers
[j
]; ++k
) {
218 zz2value(factors
[j
][0], tz
);
219 dpoly
fact(total_power
, tz
, 1);
223 for (int k
= 0; k
< v
[i
]->terms
.NumRows(); ++k
) {
224 zz2value(v
[i
]->terms
[k
][0], tz
);
225 dpoly
n(total_power
, tz
);
226 mpq_set_si(tcount
, 0, 1);
229 bfct
->c
[k
].n
= -bfct
->c
[k
].n
;
230 zz2value(bfct
->c
[k
].n
, tn
);
231 zz2value(bfct
->c
[k
].d
, td
);
233 mpz_mul(mpq_numref(tcount
), mpq_numref(tcount
), tn
);
234 mpz_mul(mpq_denref(tcount
), mpq_denref(tcount
), td
);
235 mpq_canonicalize(tcount
);
236 mpq_add(count
, count
, tcount
);
243 /* Check whether the polyhedron is unbounded and if so,
244 * check whether it has any (and therefore an infinite number of)
246 * If one of the vertices is integer, then we are done.
247 * Otherwise, transform the polyhedron such that one of the rays
248 * is the first unit vector and cut it off at a height that ensures
249 * that if the whole polyhedron has any points, then the remaining part
250 * has integer points. In particular we add the largest coefficient
251 * of a ray to the highest vertex (rounded up).
253 static bool Polyhedron_is_infinite(Polyhedron
*P
, Value
* result
,
254 barvinok_options
*options
)
266 for (; r
< P
->NbRays
; ++r
)
267 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1]))
269 if (P
->NbBid
== 0 && r
== P
->NbRays
)
272 if (options
->count_sample_infinite
) {
275 sample
= Polyhedron_Sample(P
, options
);
277 value_set_si(*result
, 0);
279 value_set_si(*result
, -1);
285 for (int i
= 0; i
< P
->NbRays
; ++i
)
286 if (value_one_p(P
->Ray
[i
][1+P
->Dimension
])) {
287 value_set_si(*result
, -1);
292 M
= Matrix_Alloc(P
->Dimension
+1, P
->Dimension
+1);
293 Vector_Gcd(P
->Ray
[r
]+1, P
->Dimension
, &g
);
294 Vector_AntiScale(P
->Ray
[r
]+1, M
->p
[0], g
, P
->Dimension
+1);
295 int ok
= unimodular_complete(M
, 1);
297 value_set_si(M
->p
[P
->Dimension
][P
->Dimension
], 1);
300 P
= Polyhedron_Preimage(P
, M2
, 0);
308 value_set_si(size
, 0);
310 for (int i
= 0; i
< P
->NbBid
; ++i
) {
311 value_absolute(tmp
, P
->Ray
[i
][1]);
312 if (value_gt(tmp
, size
))
313 value_assign(size
, tmp
);
315 for (int i
= P
->NbBid
; i
< P
->NbRays
; ++i
) {
316 if (value_zero_p(P
->Ray
[i
][P
->Dimension
+1])) {
317 if (value_gt(P
->Ray
[i
][1], size
))
318 value_assign(size
, P
->Ray
[i
][1]);
321 mpz_cdiv_q(tmp
, P
->Ray
[i
][1], P
->Ray
[i
][P
->Dimension
+1]);
322 if (first
|| value_gt(tmp
, offset
)) {
323 value_assign(offset
, tmp
);
327 value_addto(offset
, offset
, size
);
331 v
= Vector_Alloc(P
->Dimension
+2);
332 value_set_si(v
->p
[0], 1);
333 value_set_si(v
->p
[1], -1);
334 value_assign(v
->p
[1+P
->Dimension
], offset
);
335 R
= AddConstraints(v
->p
, 1, P
, options
->MaxRays
);
343 barvinok_count_with_options(P
, &c
, options
);
346 value_set_si(*result
, 0);
348 value_set_si(*result
, -1);
354 static void evalue2value(evalue
*e
, Value
*v
)
356 if (EVALUE_IS_ZERO(*e
)) {
361 if (value_notzero_p(e
->d
)) {
362 assert(value_one_p(e
->d
));
363 value_assign(*v
, e
->x
.n
);
367 assert(e
->x
.p
->type
== partition
);
368 assert(e
->x
.p
->size
== 2);
369 assert(EVALUE_DOMAIN(e
->x
.p
->arr
[0])->Dimension
== 0);
370 evalue2value(&e
->x
.p
->arr
[1], v
);
373 static void barvinok_count_f(Polyhedron
*P
, Value
* result
,
374 barvinok_options
*options
);
376 void barvinok_count_with_options(Polyhedron
*P
, Value
* result
,
377 struct barvinok_options
*options
)
382 bool infinite
= false;
386 "barvinok_count: input is a union; only first polyhedron is counted\n");
389 value_set_si(*result
, 0);
395 P
= remove_equalities(P
, options
->MaxRays
);
396 P
= DomainConstraintSimplify(P
, options
->MaxRays
);
400 } while (!emptyQ(P
) && P
->NbEq
!= 0);
403 value_set_si(*result
, 0);
408 if (Polyhedron_is_infinite(P
, result
, options
)) {
413 if (P
->Dimension
== 0) {
414 /* Test whether the constraints are satisfied */
415 POL_ENSURE_VERTICES(P
);
416 value_set_si(*result
, !emptyQ(P
));
421 if (options
->summation
== BV_SUM_BERNOULLI
) {
422 Polyhedron
*C
= Universe_Polyhedron(0);
423 evalue
*sum
= barvinok_summate_unweighted(P
, C
, options
);
425 evalue2value(sum
, result
);
429 Q
= Polyhedron_Factor(P
, 0, NULL
, options
->MaxRays
);
437 barvinok_count_f(P
, result
, options
);
438 if (value_neg_p(*result
))
440 if (Q
&& P
->next
&& value_notzero_p(*result
)) {
444 for (Q
= P
->next
; Q
; Q
= Q
->next
) {
445 barvinok_count_f(Q
, &factor
, options
);
446 if (value_neg_p(factor
)) {
449 } else if (Q
->next
&& value_zero_p(factor
)) {
450 value_set_si(*result
, 0);
453 value_multiply(*result
, *result
, factor
);
462 value_set_si(*result
, -1);
465 void barvinok_count(Polyhedron
*P
, Value
* result
, unsigned NbMaxCons
)
467 barvinok_options
*options
= barvinok_options_new_with_defaults();
468 options
->MaxRays
= NbMaxCons
;
469 barvinok_count_with_options(P
, result
, options
);
470 barvinok_options_free(options
);
473 static void barvinok_count_f(Polyhedron
*P
, Value
* result
,
474 barvinok_options
*options
)
477 value_set_si(*result
, 0);
481 if (P
->Dimension
== 1)
482 return Line_Length(P
, result
);
484 int c
= P
->NbConstraints
;
485 POL_ENSURE_FACETS(P
);
486 if (c
!= P
->NbConstraints
|| P
->NbEq
!= 0) {
487 Polyhedron
*next
= P
->next
;
489 barvinok_count_with_options(P
, result
, options
);
494 POL_ENSURE_VERTICES(P
);
496 if (Polyhedron_is_infinite(P
, result
, options
))
500 if (options
->incremental_specialization
== BV_SPECIALIZATION_BF
)
501 cnt
= new bfcounter(P
->Dimension
);
502 else if (options
->incremental_specialization
== BV_SPECIALIZATION_DF
)
503 cnt
= new icounter(P
->Dimension
);
504 else if (options
->incremental_specialization
== BV_SPECIALIZATION_TODD
)
505 cnt
= new tcounter(P
->Dimension
, options
->max_index
);
507 cnt
= new counter(P
->Dimension
, options
->max_index
);
508 cnt
->start(P
, options
);
510 cnt
->get_count(result
);
514 static void uni_polynom(int param
, Vector
*c
, evalue
*EP
)
516 unsigned dim
= c
->Size
-2;
518 value_set_si(EP
->d
,0);
519 EP
->x
.p
= new_enode(polynomial
, dim
+1, param
+1);
520 for (int j
= 0; j
<= dim
; ++j
)
521 evalue_set(&EP
->x
.p
->arr
[j
], c
->p
[j
], c
->p
[dim
+1]);
524 typedef evalue
* evalue_p
;
526 struct enumerator_base
{
530 vertex_decomposer
*vpd
;
532 enumerator_base(unsigned dim
, vertex_decomposer
*vpd
)
537 vE
= new evalue_p
[vpd
->PP
->nbV
];
538 for (int j
= 0; j
< vpd
->PP
->nbV
; ++j
)
542 evalue_set_si(&mone
, -1, 1);
545 void decompose_at(Param_Vertices
*V
, int _i
, barvinok_options
*options
) {
549 value_init(vE
[_i
]->d
);
550 evalue_set_si(vE
[_i
], 0, 1);
552 vpd
->decompose_at_vertex(V
, _i
, options
);
555 virtual ~enumerator_base() {
556 for (int j
= 0; j
< vpd
->PP
->nbV
; ++j
)
558 free_evalue_refs(vE
[j
]);
563 free_evalue_refs(&mone
);
566 static enumerator_base
*create(Polyhedron
*P
, unsigned dim
,
567 Param_Polyhedron
*PP
,
568 barvinok_options
*options
);
571 struct enumerator
: public signed_cone_consumer
, public vertex_decomposer
,
572 public enumerator_base
{
580 enumerator(Polyhedron
*P
, unsigned dim
, Param_Polyhedron
*PP
) :
581 vertex_decomposer(PP
, *this), enumerator_base(dim
, this) {
582 randomvector(P
, lambda
, dim
);
584 c
= Vector_Alloc(dim
+2);
596 virtual void handle(const signed_cone
& sc
, barvinok_options
*options
);
599 void enumerator::handle(const signed_cone
& sc
, barvinok_options
*options
)
603 assert(sc
.rays
.NumRows() == dim
);
604 for (int k
= 0; k
< dim
; ++k
) {
605 if (lambda
* sc
.rays
[k
] == 0)
609 lattice_point(V
, sc
.rays
, lambda
, &num
, sc
.det
, options
);
610 den
= sc
.rays
* lambda
;
615 zz2value(den
[0], tz
);
617 for (int k
= 1; k
< dim
; ++k
) {
618 zz2value(den
[k
], tz
);
619 dpoly
fact(dim
, tz
, 1);
625 for (unsigned long i
= 0; i
< sc
.det
; ++i
) {
626 evalue
*EV
= evalue_polynomial(c
, num
.E
[i
]);
629 evalue_free(num
.E
[i
]);
633 mpq_set_si(count
, 0, 1);
634 if (num
.constant
.length() == 1) {
635 zz2value(num
.constant
[0], tz
);
637 d
.div(n
, count
, sign
);
644 for (unsigned long i
= 0; i
< sc
.det
; ++i
) {
645 value_assign(acc
, c
->p
[dim
]);
646 zz2value(num
.constant
[i
], x
);
647 for (int j
= dim
-1; j
>= 0; --j
) {
648 value_multiply(acc
, acc
, x
);
649 value_addto(acc
, acc
, c
->p
[j
]);
651 value_addto(mpq_numref(count
), mpq_numref(count
), acc
);
653 mpz_set(mpq_denref(count
), c
->p
[dim
+1]);
659 evalue_set(&EV
, &count
[0]._mp_num
, &count
[0]._mp_den
);
661 free_evalue_refs(&EV
);
665 struct ienumerator_base
: enumerator_base
{
668 ienumerator_base(unsigned dim
, vertex_decomposer
*vpd
) :
669 enumerator_base(dim
,vpd
) {
670 E_vertex
= new evalue_p
[dim
];
673 virtual ~ienumerator_base() {
677 evalue
*E_num(int i
, int d
) {
678 return E_vertex
[i
+ (dim
-d
)];
687 cumulator(evalue
*factor
, evalue
*v
, dpoly_r
*r
) :
688 factor(factor
), v(v
), r(r
) {}
690 void cumulate(barvinok_options
*options
);
692 virtual void add_term(const vector
<int>& powers
, evalue
*f2
) = 0;
693 virtual ~cumulator() {}
696 void cumulator::cumulate(barvinok_options
*options
)
698 evalue cum
; // factor * 1 * E_num[0]/1 * (E_num[0]-1)/2 *...
700 evalue t
; // E_num[0] - (m-1)
704 if (options
->lookup_table
) {
706 evalue_set_si(&mone
, -1, 1);
710 evalue_copy(&cum
, factor
);
713 value_set_si(f
.d
, 1);
714 value_set_si(f
.x
.n
, 1);
718 if (!options
->lookup_table
) {
719 for (cst
= &t
; value_zero_p(cst
->d
); ) {
720 if (cst
->x
.p
->type
== fractional
)
721 cst
= &cst
->x
.p
->arr
[1];
723 cst
= &cst
->x
.p
->arr
[0];
727 for (int m
= 0; m
< r
->len
; ++m
) {
730 value_set_si(f
.d
, m
);
732 if (!options
->lookup_table
)
733 value_subtract(cst
->x
.n
, cst
->x
.n
, cst
->d
);
739 dpoly_r_term_list
& current
= r
->c
[r
->len
-1-m
];
740 dpoly_r_term_list::iterator j
;
741 for (j
= current
.begin(); j
!= current
.end(); ++j
) {
742 if ((*j
)->coeff
== 0)
744 evalue
*f2
= new evalue
;
747 zz2value((*j
)->coeff
, f2
->x
.n
);
748 zz2value(r
->denom
, f2
->d
);
751 add_term((*j
)->powers
, f2
);
754 free_evalue_refs(&f
);
755 free_evalue_refs(&t
);
756 free_evalue_refs(&cum
);
757 if (options
->lookup_table
)
758 free_evalue_refs(&mone
);
766 struct ie_cum
: public cumulator
{
767 vector
<E_poly_term
*> terms
;
769 ie_cum(evalue
*factor
, evalue
*v
, dpoly_r
*r
) : cumulator(factor
, v
, r
) {}
771 virtual void add_term(const vector
<int>& powers
, evalue
*f2
);
774 void ie_cum::add_term(const vector
<int>& powers
, evalue
*f2
)
777 for (k
= 0; k
< terms
.size(); ++k
) {
778 if (terms
[k
]->powers
== powers
) {
779 eadd(f2
, terms
[k
]->E
);
780 free_evalue_refs(f2
);
785 if (k
>= terms
.size()) {
786 E_poly_term
*ET
= new E_poly_term
;
793 struct ienumerator
: public signed_cone_consumer
, public vertex_decomposer
,
794 public ienumerator_base
{
801 ienumerator(Polyhedron
*P
, unsigned dim
, Param_Polyhedron
*PP
) :
802 vertex_decomposer(PP
, *this), ienumerator_base(dim
, this) {
803 vertex
.SetDims(1, dim
);
805 den
.SetDims(dim
, dim
);
815 virtual void handle(const signed_cone
& sc
, barvinok_options
*options
);
816 void reduce(evalue
*factor
, const mat_ZZ
& num
, const mat_ZZ
& den_f
,
817 barvinok_options
*options
);
820 void ienumerator::reduce(evalue
*factor
, const mat_ZZ
& num
, const mat_ZZ
& den_f
,
821 barvinok_options
*options
)
823 unsigned len
= den_f
.NumRows(); // number of factors in den
824 unsigned dim
= num
.NumCols();
825 assert(num
.NumRows() == 1);
828 eadd(factor
, vE
[vert
]);
837 split_one(num
, num_s
, num_p
, den_f
, den_s
, den_r
);
840 den_p
.SetLength(len
);
844 normalize(one
, num_s
, num_p
, den_s
, den_p
, den_r
);
850 for (int k
= 0; k
< len
; ++k
) {
853 else if (den_s
[k
] == 0)
857 reduce(factor
, num_p
, den_r
, options
);
861 pden
.SetDims(only_param
, dim
-1);
863 for (k
= 0, l
= 0; k
< len
; ++k
)
865 pden
[l
++] = den_r
[k
];
867 for (k
= 0; k
< len
; ++k
)
871 zz2value(num_s
[0], tz
);
872 dpoly
n(no_param
, tz
);
873 zz2value(den_s
[k
], tz
);
874 dpoly
D(no_param
, tz
, 1);
877 zz2value(den_s
[k
], tz
);
878 dpoly
fact(no_param
, tz
, 1);
883 // if no_param + only_param == len then all powers
884 // below will be all zero
885 if (no_param
+ only_param
== len
) {
886 if (E_num(0, dim
) != 0)
887 r
= new dpoly_r(n
, len
);
889 mpq_set_si(tcount
, 0, 1);
893 if (value_notzero_p(mpq_numref(tcount
))) {
897 value_assign(f
.x
.n
, mpq_numref(tcount
));
898 value_assign(f
.d
, mpq_denref(tcount
));
900 reduce(factor
, num_p
, pden
, options
);
901 free_evalue_refs(&f
);
906 for (k
= 0; k
< len
; ++k
) {
907 if (den_s
[k
] == 0 || den_p
[k
] == 0)
910 zz2value(den_s
[k
], tz
);
911 dpoly
pd(no_param
-1, tz
, 1);
914 for (l
= 0; l
< k
; ++l
)
915 if (den_r
[l
] == den_r
[k
])
919 r
= new dpoly_r(n
, pd
, l
, len
);
921 dpoly_r
*nr
= new dpoly_r(r
, pd
, l
, len
);
927 dpoly_r
*rc
= r
->div(D
);
930 if (E_num(0, dim
) == 0) {
931 int common
= pden
.NumRows();
932 dpoly_r_term_list
& final
= r
->c
[r
->len
-1];
938 zz2value(r
->denom
, f
.d
);
939 dpoly_r_term_list::iterator j
;
940 for (j
= final
.begin(); j
!= final
.end(); ++j
) {
941 if ((*j
)->coeff
== 0)
944 for (int k
= 0; k
< r
->dim
; ++k
) {
945 int n
= (*j
)->powers
[k
];
948 pden
.SetDims(rows
+n
, pden
.NumCols());
949 for (int l
= 0; l
< n
; ++l
)
950 pden
[rows
+l
] = den_r
[k
];
954 evalue_copy(&t
, factor
);
955 zz2value((*j
)->coeff
, f
.x
.n
);
957 reduce(&t
, num_p
, pden
, options
);
958 free_evalue_refs(&t
);
960 free_evalue_refs(&f
);
962 ie_cum
cum(factor
, E_num(0, dim
), r
);
963 cum
.cumulate(options
);
965 int common
= pden
.NumRows();
967 for (int j
= 0; j
< cum
.terms
.size(); ++j
) {
969 pden
.SetDims(rows
, pden
.NumCols());
970 for (int k
= 0; k
< r
->dim
; ++k
) {
971 int n
= cum
.terms
[j
]->powers
[k
];
974 pden
.SetDims(rows
+n
, pden
.NumCols());
975 for (int l
= 0; l
< n
; ++l
)
976 pden
[rows
+l
] = den_r
[k
];
979 reduce(cum
.terms
[j
]->E
, num_p
, pden
, options
);
980 free_evalue_refs(cum
.terms
[j
]->E
);
981 delete cum
.terms
[j
]->E
;
989 static int type_offset(enode
*p
)
991 return p
->type
== fractional
? 1 :
992 p
->type
== flooring
? 1 : 0;
995 static int edegree(evalue
*e
)
1000 if (value_notzero_p(e
->d
))
1004 int i
= type_offset(p
);
1005 if (p
->size
-i
-1 > d
)
1006 d
= p
->size
- i
- 1;
1007 for (; i
< p
->size
; i
++) {
1008 int d2
= edegree(&p
->arr
[i
]);
1015 void ienumerator::handle(const signed_cone
& sc
, barvinok_options
*options
)
1017 assert(sc
.det
== 1);
1018 assert(sc
.rays
.NumRows() == dim
);
1020 lattice_point(V
, sc
.rays
, vertex
[0], E_vertex
, options
);
1026 evalue_set_si(&one
, sc
.sign
, 1);
1027 reduce(&one
, vertex
, den
, options
);
1028 free_evalue_refs(&one
);
1030 for (int i
= 0; i
< dim
; ++i
)
1032 evalue_free(E_vertex
[i
]);
1035 struct bfenumerator
: public vertex_decomposer
, public bf_base
,
1036 public ienumerator_base
{
1039 bfenumerator(Polyhedron
*P
, unsigned dim
, Param_Polyhedron
*PP
) :
1040 vertex_decomposer(PP
, *this),
1041 bf_base(dim
), ienumerator_base(dim
, this) {
1049 virtual void handle(const signed_cone
& sc
, barvinok_options
*options
);
1050 virtual void base(mat_ZZ
& factors
, bfc_vec
& v
);
1052 bfc_term_base
* new_bf_term(int len
) {
1053 bfe_term
* t
= new bfe_term(len
);
1057 virtual void set_factor(bfc_term_base
*t
, int k
, int change
) {
1058 bfe_term
* bfet
= static_cast<bfe_term
*>(t
);
1059 factor
= bfet
->factors
[k
];
1060 assert(factor
!= NULL
);
1061 bfet
->factors
[k
] = NULL
;
1063 emul(&mone
, factor
);
1066 virtual void set_factor(bfc_term_base
*t
, int k
, mpq_t
&q
, int change
) {
1067 bfe_term
* bfet
= static_cast<bfe_term
*>(t
);
1068 factor
= bfet
->factors
[k
];
1069 assert(factor
!= NULL
);
1070 bfet
->factors
[k
] = NULL
;
1076 value_oppose(f
.x
.n
, mpq_numref(q
));
1078 value_assign(f
.x
.n
, mpq_numref(q
));
1079 value_assign(f
.d
, mpq_denref(q
));
1081 free_evalue_refs(&f
);
1084 virtual void set_factor(bfc_term_base
*t
, int k
, const QQ
& c
, int change
) {
1085 bfe_term
* bfet
= static_cast<bfe_term
*>(t
);
1087 factor
= new evalue
;
1092 zz2value(c
.n
, f
.x
.n
);
1094 value_oppose(f
.x
.n
, f
.x
.n
);
1097 value_init(factor
->d
);
1098 evalue_copy(factor
, bfet
->factors
[k
]);
1100 free_evalue_refs(&f
);
1103 void set_factor(evalue
*f
, int change
) {
1109 virtual void insert_term(bfc_term_base
*t
, int i
) {
1110 bfe_term
* bfet
= static_cast<bfe_term
*>(t
);
1111 int len
= t
->terms
.NumRows()-1; // already increased by one
1113 bfet
->factors
.resize(len
+1);
1114 for (int j
= len
; j
> i
; --j
) {
1115 bfet
->factors
[j
] = bfet
->factors
[j
-1];
1116 t
->terms
[j
] = t
->terms
[j
-1];
1118 bfet
->factors
[i
] = factor
;
1122 virtual void update_term(bfc_term_base
*t
, int i
) {
1123 bfe_term
* bfet
= static_cast<bfe_term
*>(t
);
1125 eadd(factor
, bfet
->factors
[i
]);
1126 free_evalue_refs(factor
);
1130 virtual bool constant_vertex(int dim
) { return E_num(0, dim
) == 0; }
1132 virtual void cum(bf_reducer
*bfr
, bfc_term_base
*t
, int k
, dpoly_r
*r
,
1133 barvinok_options
*options
);
1136 enumerator_base
*enumerator_base::create(Polyhedron
*P
, unsigned dim
,
1137 Param_Polyhedron
*PP
,
1138 barvinok_options
*options
)
1140 enumerator_base
*eb
;
1142 if (options
->incremental_specialization
== BV_SPECIALIZATION_BF
)
1143 eb
= new bfenumerator(P
, dim
, PP
);
1144 else if (options
->incremental_specialization
== BV_SPECIALIZATION_DF
)
1145 eb
= new ienumerator(P
, dim
, PP
);
1147 eb
= new enumerator(P
, dim
, PP
);
1152 struct bfe_cum
: public cumulator
{
1154 bfc_term_base
*told
;
1158 bfe_cum(evalue
*factor
, evalue
*v
, dpoly_r
*r
, bf_reducer
*bfr
,
1159 bfc_term_base
*t
, int k
, bfenumerator
*e
) :
1160 cumulator(factor
, v
, r
), told(t
), k(k
),
1164 virtual void add_term(const vector
<int>& powers
, evalue
*f2
);
1167 void bfe_cum::add_term(const vector
<int>& powers
, evalue
*f2
)
1169 bfr
->update_powers(powers
);
1171 bfc_term_base
* t
= bfe
->find_bfc_term(bfr
->vn
, bfr
->npowers
, bfr
->nnf
);
1172 bfe
->set_factor(f2
, bfr
->l_changes
% 2);
1173 bfe
->add_term(t
, told
->terms
[k
], bfr
->l_extra_num
);
1176 void bfenumerator::cum(bf_reducer
*bfr
, bfc_term_base
*t
, int k
,
1177 dpoly_r
*r
, barvinok_options
*options
)
1179 bfe_term
* bfet
= static_cast<bfe_term
*>(t
);
1180 bfe_cum
cum(bfet
->factors
[k
], E_num(0, bfr
->d
), r
, bfr
, t
, k
, this);
1181 cum
.cumulate(options
);
1184 void bfenumerator::base(mat_ZZ
& factors
, bfc_vec
& v
)
1186 for (int i
= 0; i
< v
.size(); ++i
) {
1187 assert(v
[i
]->terms
.NumRows() == 1);
1188 evalue
*factor
= static_cast<bfe_term
*>(v
[i
])->factors
[0];
1189 eadd(factor
, vE
[vert
]);
1194 void bfenumerator::handle(const signed_cone
& sc
, barvinok_options
*options
)
1196 assert(sc
.det
== 1);
1197 assert(sc
.rays
.NumRows() == enumerator_base::dim
);
1199 bfe_term
* t
= new bfe_term(enumerator_base::dim
);
1200 vector
< bfc_term_base
* > v
;
1203 t
->factors
.resize(1);
1205 t
->terms
.SetDims(1, enumerator_base::dim
);
1206 lattice_point(V
, sc
.rays
, t
->terms
[0], E_vertex
, options
);
1208 // the elements of factors are always lexpositive
1210 int s
= setup_factors(sc
.rays
, factors
, t
, sc
.sign
);
1212 t
->factors
[0] = new evalue
;
1213 value_init(t
->factors
[0]->d
);
1214 evalue_set_si(t
->factors
[0], s
, 1);
1215 reduce(factors
, v
, options
);
1217 for (int i
= 0; i
< enumerator_base::dim
; ++i
)
1219 evalue_free(E_vertex
[i
]);
1222 static evalue
* barvinok_enumerate_ev_f(Polyhedron
*P
, Polyhedron
* C
,
1223 barvinok_options
*options
);
1226 static evalue
* barvinok_enumerate_cst(Polyhedron
*P
, Polyhedron
* C
,
1227 struct barvinok_options
*options
)
1233 return evalue_zero();
1236 ALLOC(evalue
, eres
);
1237 value_init(eres
->d
);
1238 value_set_si(eres
->d
, 0);
1239 eres
->x
.p
= new_enode(partition
, 2, C
->Dimension
);
1240 EVALUE_SET_DOMAIN(eres
->x
.p
->arr
[0],
1241 DomainConstraintSimplify(C
, options
->MaxRays
));
1242 value_set_si(eres
->x
.p
->arr
[1].d
, 1);
1243 value_init(eres
->x
.p
->arr
[1].x
.n
);
1245 value_set_si(eres
->x
.p
->arr
[1].x
.n
, 0);
1247 barvinok_count_with_options(P
, &eres
->x
.p
->arr
[1].x
.n
, options
);
1252 static evalue
* enumerate(Polyhedron
*P
, Polyhedron
* C
,
1253 struct barvinok_options
*options
)
1256 Polyhedron
*Porig
= P
;
1257 Polyhedron
*Corig
= C
;
1258 Polyhedron
*CEq
= NULL
, *rVD
;
1260 unsigned nparam
= C
->Dimension
;
1265 value_init(factor
.d
);
1266 evalue_set_si(&factor
, 1, 1);
1269 POL_ENSURE_FACETS(P
);
1270 POL_ENSURE_VERTICES(P
);
1271 POL_ENSURE_FACETS(C
);
1272 POL_ENSURE_VERTICES(C
);
1274 if (C
->Dimension
== 0 || emptyQ(P
) || emptyQ(C
)) {
1277 CEq
= Polyhedron_Copy(CEq
);
1278 eres
= barvinok_enumerate_cst(P
, CEq
? CEq
: Polyhedron_Copy(C
), options
);
1281 evalue_backsubstitute(eres
, CP
, options
->MaxRays
);
1285 emul(&factor
, eres
);
1286 if (options
->approximation_method
== BV_APPROX_DROP
) {
1287 if (options
->polynomial_approximation
== BV_APPROX_SIGN_UPPER
)
1288 evalue_frac2polynomial(eres
, 1, options
->MaxRays
);
1289 if (options
->polynomial_approximation
== BV_APPROX_SIGN_LOWER
)
1290 evalue_frac2polynomial(eres
, -1, options
->MaxRays
);
1291 if (options
->polynomial_approximation
== BV_APPROX_SIGN_APPROX
)
1292 evalue_frac2polynomial(eres
, 0, options
->MaxRays
);
1294 reduce_evalue(eres
);
1295 free_evalue_refs(&factor
);
1303 if (Polyhedron_is_unbounded(P
, nparam
, options
->MaxRays
))
1306 if (P
->Dimension
== nparam
) {
1308 P
= Universe_Polyhedron(0);
1311 if (P
->NbEq
!= 0 || C
->NbEq
!= 0) {
1314 remove_all_equalities(&P
, &C
, &CP
, NULL
, nparam
, options
->MaxRays
);
1315 if (C
!= D
&& D
!= Corig
)
1317 if (P
!= Q
&& Q
!= Porig
)
1319 eres
= enumerate(P
, C
, options
);
1323 Polyhedron
*T
= Polyhedron_Factor(P
, nparam
, NULL
, options
->MaxRays
);
1324 if (T
|| (P
->Dimension
== nparam
+1)) {
1326 Polyhedron
*FC
= Factor_Context(T
? T
: P
, nparam
, options
->MaxRays
);
1327 C
= DomainIntersection(C
, FC
, options
->MaxRays
);
1329 Polyhedron_Free(C2
);
1330 Polyhedron_Free(FC
);
1336 if (T
->Dimension
== C
->Dimension
) {
1345 eres
= barvinok_enumerate_ev_f(P
, C
, options
);
1352 for (Q
= P
->next
; Q
; Q
= Q
->next
) {
1353 Polyhedron
*next
= Q
->next
;
1356 f
= barvinok_enumerate_ev_f(Q
, C
, options
);
1367 evalue
* barvinok_enumerate_with_options(Polyhedron
*P
, Polyhedron
* C
,
1368 struct barvinok_options
*options
)
1370 Polyhedron
*next
, *Cnext
, *C1
;
1371 Polyhedron
*Corig
= C
;
1376 "barvinok_enumerate: input is a union; only first polyhedron is enumerated\n");
1380 "barvinok_enumerate: context is a union; only first polyhedron is considered\n");
1384 C1
= Polyhedron_Project(P
, C
->Dimension
);
1385 C
= DomainIntersection(C
, C1
, options
->MaxRays
);
1386 Polyhedron_Free(C1
);
1390 if (options
->approximation_method
== BV_APPROX_BERNOULLI
||
1391 options
->summation
== BV_SUM_BERNOULLI
) {
1392 int summation
= options
->summation
;
1393 options
->summation
= BV_SUM_BERNOULLI
;
1394 eres
= barvinok_summate_unweighted(P
, C
, options
);
1395 options
->summation
= summation
;
1397 eres
= enumerate(P
, C
, options
);
1401 Corig
->next
= Cnext
;
1406 evalue
* barvinok_enumerate_ev(Polyhedron
*P
, Polyhedron
* C
, unsigned MaxRays
)
1409 barvinok_options
*options
= barvinok_options_new_with_defaults();
1410 options
->MaxRays
= MaxRays
;
1411 E
= barvinok_enumerate_with_options(P
, C
, options
);
1412 barvinok_options_free(options
);
1416 evalue
*Param_Polyhedron_Enumerate(Param_Polyhedron
*PP
, Polyhedron
*P
,
1418 struct barvinok_options
*options
)
1422 unsigned nparam
= C
->Dimension
;
1423 unsigned dim
= P
->Dimension
- nparam
;
1426 for (nd
= 0, D
=PP
->D
; D
; ++nd
, D
=D
->next
);
1427 evalue_section
*s
= new evalue_section
[nd
];
1429 enumerator_base
*et
= NULL
;
1434 et
= enumerator_base::create(P
, dim
, PP
, options
);
1436 Polyhedron
*TC
= true_context(P
, C
, options
->MaxRays
);
1437 FORALL_REDUCED_DOMAIN(PP
, TC
, nd
, options
, i
, D
, rVD
)
1440 s
[i
].E
= evalue_zero();
1443 FORALL_PVertex_in_ParamPolyhedron(V
,D
,PP
) // _i is internal counter
1446 et
->decompose_at(V
, _i
, options
);
1447 } catch (OrthogonalException
&e
) {
1448 FORALL_REDUCED_DOMAIN_RESET
;
1449 for (; i
>= 0; --i
) {
1450 evalue_free(s
[i
].E
);
1451 Domain_Free(s
[i
].D
);
1455 eadd(et
->vE
[_i
] , s
[i
].E
);
1456 END_FORALL_PVertex_in_ParamPolyhedron
;
1457 evalue_range_reduction_in_domain(s
[i
].E
, rVD
);
1458 END_FORALL_REDUCED_DOMAIN
1459 Polyhedron_Free(TC
);
1462 eres
= evalue_from_section_array(s
, nd
);
1468 static evalue
* barvinok_enumerate_ev_f(Polyhedron
*P
, Polyhedron
* C
,
1469 barvinok_options
*options
)
1471 unsigned nparam
= C
->Dimension
;
1472 bool do_scale
= options
->approximation_method
== BV_APPROX_SCALE
;
1474 if (options
->summation
== BV_SUM_EULER
)
1475 return barvinok_summate_unweighted(P
, C
, options
);
1477 if (options
->approximation_method
== BV_APPROX_VOLUME
)
1478 return Param_Polyhedron_Volume(P
, C
, options
);
1480 if (P
->Dimension
- nparam
== 1 && !do_scale
)
1481 return ParamLine_Length(P
, C
, options
);
1483 Param_Polyhedron
*PP
= NULL
;
1487 eres
= scale_bound(P
, C
, options
);
1492 PP
= Polyhedron2Param_Polyhedron(P
, C
, options
);
1495 eres
= scale(PP
, P
, C
, options
);
1497 eres
= Param_Polyhedron_Enumerate(PP
, P
, C
, options
);
1500 Param_Polyhedron_Free(PP
);
1505 Enumeration
* barvinok_enumerate(Polyhedron
*P
, Polyhedron
* C
, unsigned MaxRays
)
1507 evalue
*EP
= barvinok_enumerate_ev(P
, C
, MaxRays
);
1509 return partition2enumeration(EP
);
1512 evalue
* barvinok_enumerate_union(Polyhedron
*D
, Polyhedron
* C
, unsigned MaxRays
)
1515 gen_fun
*gf
= barvinok_enumerate_union_series(D
, C
, MaxRays
);