8 #include <NTL/mat_ZZ.h>
10 #include <isl_map_polylib.h>
11 #include <barvinok/util.h>
12 #include <barvinok/evalue.h>
14 #include <barvinok/barvinok.h>
15 #include <barvinok/genfun.h>
16 #include <barvinok/options.h>
17 #include <barvinok/sample.h>
18 #include "bfcounter.h"
19 #include "conversion.h"
21 #include "decomposer.h"
23 #include "lattice_point.h"
25 #include "reduce_domain.h"
26 #include "remove_equalities.h"
29 #include "bernoulli.h"
30 #include "param_util.h"
42 using std::ostringstream
;
44 #define ALLOC(t,p) p = (t*)malloc(sizeof(*p))
57 coeff
= Matrix_Alloc(d
+1, d
+1+1);
58 value_set_si(coeff
->p
[0][0], 1);
59 value_set_si(coeff
->p
[0][d
+1], 1);
60 for (int i
= 1; i
<= d
; ++i
) {
61 value_multiply(coeff
->p
[i
][0], coeff
->p
[i
-1][0], d0
);
62 Vector_Combine(coeff
->p
[i
-1], coeff
->p
[i
-1]+1, coeff
->p
[i
]+1,
64 value_set_si(coeff
->p
[i
][d
+1], i
);
65 value_multiply(coeff
->p
[i
][d
+1], coeff
->p
[i
][d
+1], coeff
->p
[i
-1][d
+1]);
66 value_decrement(d0
, d0
);
71 void div(dpoly
& d
, Vector
*count
, int sign
) {
72 int len
= coeff
->NbRows
;
73 Matrix
* c
= Matrix_Alloc(coeff
->NbRows
, coeff
->NbColumns
);
76 for (int i
= 0; i
< len
; ++i
) {
77 Vector_Copy(coeff
->p
[i
], c
->p
[i
], len
+1);
78 for (int j
= 1; j
<= i
; ++j
) {
79 value_multiply(tmp
, d
.coeff
->p
[j
], c
->p
[i
][len
]);
80 value_oppose(tmp
, tmp
);
81 Vector_Combine(c
->p
[i
], c
->p
[i
-j
], c
->p
[i
],
82 c
->p
[i
-j
][len
], tmp
, len
);
83 value_multiply(c
->p
[i
][len
], c
->p
[i
][len
], c
->p
[i
-j
][len
]);
85 value_multiply(c
->p
[i
][len
], c
->p
[i
][len
], d
.coeff
->p
[0]);
88 value_set_si(tmp
, -1);
89 Vector_Scale(c
->p
[len
-1], count
->p
, tmp
, len
);
90 value_assign(count
->p
[len
], c
->p
[len
-1][len
]);
92 Vector_Copy(c
->p
[len
-1], count
->p
, len
+1);
93 Vector_Normalize(count
->p
, len
+1);
99 static void add_rays(mat_ZZ
& rays
, Polyhedron
*i
, int *r
, int nvar
= -1,
102 unsigned dim
= i
->Dimension
;
105 for (int k
= 0; k
< i
->NbRays
; ++k
) {
106 if (!value_zero_p(i
->Ray
[k
][dim
+1]))
108 if (!all
&& nvar
!= dim
&& First_Non_Zero(i
->Ray
[k
]+1, nvar
) == -1)
110 values2zz(i
->Ray
[k
]+1, rays
[(*r
)++], nvar
);
114 struct bfe_term
: public bfc_term_base
{
115 vector
<evalue
*> factors
;
117 bfe_term(int len
) : bfc_term_base(len
) {
121 for (int i
= 0; i
< factors
.size(); ++i
) {
124 free_evalue_refs(factors
[i
]);
130 static void print_int_vector(int *v
, int len
, const char *name
)
132 cerr
<< name
<< endl
;
133 for (int j
= 0; j
< len
; ++j
) {
139 static void print_bfc_terms(mat_ZZ
& factors
, bfc_vec
& v
)
142 cerr
<< "factors" << endl
;
143 cerr
<< factors
<< endl
;
144 for (int i
= 0; i
< v
.size(); ++i
) {
145 cerr
<< "term: " << i
<< endl
;
146 print_int_vector(v
[i
]->powers
, factors
.NumRows(), "powers");
147 cerr
<< "terms" << endl
;
148 cerr
<< v
[i
]->terms
<< endl
;
149 bfc_term
* bfct
= static_cast<bfc_term
*>(v
[i
]);
150 cerr
<< bfct
->c
<< endl
;
154 static void print_bfe_terms(mat_ZZ
& factors
, bfc_vec
& v
)
157 cerr
<< "factors" << endl
;
158 cerr
<< factors
<< endl
;
159 for (int i
= 0; i
< v
.size(); ++i
) {
160 cerr
<< "term: " << i
<< endl
;
161 print_int_vector(v
[i
]->powers
, factors
.NumRows(), "powers");
162 cerr
<< "terms" << endl
;
163 cerr
<< v
[i
]->terms
<< endl
;
164 bfe_term
* bfet
= static_cast<bfe_term
*>(v
[i
]);
165 for (int j
= 0; j
< v
[i
]->terms
.NumRows(); ++j
) {
166 const char * test
[] = {"a", "b"};
167 print_evalue(stderr
, bfet
->factors
[j
], test
);
168 fprintf(stderr
, "\n");
173 struct bfcounter
: public bfcounter_base
{
177 bfcounter(unsigned dim
) : bfcounter_base(dim
) {
186 virtual void base(mat_ZZ
& factors
, bfc_vec
& v
);
187 virtual void get_count(Value
*result
) {
188 assert(value_one_p(&count
[0]._mp_den
));
189 value_assign(*result
, &count
[0]._mp_num
);
193 void bfcounter::base(mat_ZZ
& factors
, bfc_vec
& v
)
195 unsigned nf
= factors
.NumRows();
197 for (int i
= 0; i
< v
.size(); ++i
) {
198 bfc_term
* bfct
= static_cast<bfc_term
*>(v
[i
]);
200 // factor is always positive, so we always
202 for (int k
= 0; k
< nf
; ++k
)
203 total_power
+= v
[i
]->powers
[k
];
206 for (j
= 0; j
< nf
; ++j
)
207 if (v
[i
]->powers
[j
] > 0)
210 zz2value(factors
[j
][0], tz
);
211 dpoly
D(total_power
, tz
, 1);
212 for (int k
= 1; k
< v
[i
]->powers
[j
]; ++k
) {
213 zz2value(factors
[j
][0], tz
);
214 dpoly
fact(total_power
, tz
, 1);
218 for (int k
= 0; k
< v
[i
]->powers
[j
]; ++k
) {
219 zz2value(factors
[j
][0], tz
);
220 dpoly
fact(total_power
, tz
, 1);
224 for (int k
= 0; k
< v
[i
]->terms
.NumRows(); ++k
) {
225 zz2value(v
[i
]->terms
[k
][0], tz
);
226 dpoly
n(total_power
, tz
);
227 mpq_set_si(tcount
, 0, 1);
230 bfct
->c
[k
].n
= -bfct
->c
[k
].n
;
231 zz2value(bfct
->c
[k
].n
, tn
);
232 zz2value(bfct
->c
[k
].d
, td
);
234 mpz_mul(mpq_numref(tcount
), mpq_numref(tcount
), tn
);
235 mpz_mul(mpq_denref(tcount
), mpq_denref(tcount
), td
);
236 mpq_canonicalize(tcount
);
237 mpq_add(count
, count
, tcount
);
244 /* Check whether the polyhedron is unbounded and if so,
245 * check whether it has any (and therefore an infinite number of)
247 * If one of the vertices is integer, then we are done.
248 * Otherwise, transform the polyhedron such that one of the rays
249 * is the first unit vector and cut it off at a height that ensures
250 * that if the whole polyhedron has any points, then the remaining part
251 * has integer points. In particular we add the largest coefficient
252 * of a ray to the highest vertex (rounded up).
254 static bool Polyhedron_is_infinite(Polyhedron
*P
, Value
* result
,
255 barvinok_options
*options
)
267 for (; r
< P
->NbRays
; ++r
)
268 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1]))
270 if (P
->NbBid
== 0 && r
== P
->NbRays
)
273 if (options
->count_sample_infinite
) {
276 sample
= Polyhedron_Sample(P
, options
);
278 value_set_si(*result
, 0);
280 value_set_si(*result
, -1);
286 for (int i
= 0; i
< P
->NbRays
; ++i
)
287 if (value_one_p(P
->Ray
[i
][1+P
->Dimension
])) {
288 value_set_si(*result
, -1);
293 M
= Matrix_Alloc(P
->Dimension
+1, P
->Dimension
+1);
294 Vector_Gcd(P
->Ray
[r
]+1, P
->Dimension
, &g
);
295 Vector_AntiScale(P
->Ray
[r
]+1, M
->p
[0], g
, P
->Dimension
+1);
296 int ok
= unimodular_complete(M
, 1);
298 value_set_si(M
->p
[P
->Dimension
][P
->Dimension
], 1);
301 P
= Polyhedron_Preimage(P
, M2
, 0);
309 value_set_si(size
, 0);
311 for (int i
= 0; i
< P
->NbBid
; ++i
) {
312 value_absolute(tmp
, P
->Ray
[i
][1]);
313 if (value_gt(tmp
, size
))
314 value_assign(size
, tmp
);
316 for (int i
= P
->NbBid
; i
< P
->NbRays
; ++i
) {
317 if (value_zero_p(P
->Ray
[i
][P
->Dimension
+1])) {
318 if (value_gt(P
->Ray
[i
][1], size
))
319 value_assign(size
, P
->Ray
[i
][1]);
322 mpz_cdiv_q(tmp
, P
->Ray
[i
][1], P
->Ray
[i
][P
->Dimension
+1]);
323 if (first
|| value_gt(tmp
, offset
)) {
324 value_assign(offset
, tmp
);
328 value_addto(offset
, offset
, size
);
332 v
= Vector_Alloc(P
->Dimension
+2);
333 value_set_si(v
->p
[0], 1);
334 value_set_si(v
->p
[1], -1);
335 value_assign(v
->p
[1+P
->Dimension
], offset
);
336 R
= AddConstraints(v
->p
, 1, P
, options
->MaxRays
);
344 barvinok_count_with_options(P
, &c
, options
);
347 value_set_si(*result
, 0);
349 value_set_si(*result
, -1);
355 static void evalue2value(evalue
*e
, Value
*v
)
357 if (EVALUE_IS_ZERO(*e
)) {
362 if (value_notzero_p(e
->d
)) {
363 assert(value_one_p(e
->d
));
364 value_assign(*v
, e
->x
.n
);
368 assert(e
->x
.p
->type
== partition
);
369 assert(e
->x
.p
->size
== 2);
370 assert(EVALUE_DOMAIN(e
->x
.p
->arr
[0])->Dimension
== 0);
371 evalue2value(&e
->x
.p
->arr
[1], v
);
374 static void barvinok_count_f(Polyhedron
*P
, Value
* result
,
375 barvinok_options
*options
);
377 void barvinok_count_with_options(Polyhedron
*P
, Value
* result
,
378 struct barvinok_options
*options
)
383 bool infinite
= false;
387 "barvinok_count: input is a union; only first polyhedron is counted\n");
390 value_set_si(*result
, 0);
396 P
= remove_equalities(P
, options
->MaxRays
);
398 P
= DomainConstraintSimplify(P
, options
->MaxRays
);
402 } while (P
&& !emptyQ(P
) && P
->NbEq
!= 0);
403 if (!P
|| emptyQ(P
)) {
405 value_set_si(*result
, 0);
410 if (Polyhedron_is_infinite(P
, result
, options
)) {
415 if (P
->Dimension
== 0) {
416 /* Test whether the constraints are satisfied */
417 POL_ENSURE_VERTICES(P
);
418 value_set_si(*result
, !emptyQ(P
));
423 if (options
->summation
== BV_SUM_BERNOULLI
) {
424 Polyhedron
*C
= Universe_Polyhedron(0);
425 evalue
*sum
= barvinok_summate_unweighted(P
, C
, options
);
427 evalue2value(sum
, result
);
431 Q
= Polyhedron_Factor(P
, 0, NULL
, options
->MaxRays
);
439 barvinok_count_f(P
, result
, options
);
440 if (value_neg_p(*result
))
442 if (Q
&& P
->next
&& value_notzero_p(*result
)) {
446 for (Q
= P
->next
; Q
; Q
= Q
->next
) {
447 barvinok_count_f(Q
, &factor
, options
);
448 if (value_neg_p(factor
)) {
451 } else if (Q
->next
&& value_zero_p(factor
)) {
452 value_set_si(*result
, 0);
455 value_multiply(*result
, *result
, factor
);
464 value_set_si(*result
, -1);
467 void barvinok_count(Polyhedron
*P
, Value
* result
, unsigned NbMaxCons
)
469 barvinok_options
*options
= barvinok_options_new_with_defaults();
470 options
->MaxRays
= NbMaxCons
;
471 barvinok_count_with_options(P
, result
, options
);
472 barvinok_options_free(options
);
475 static void barvinok_count_f(Polyhedron
*P
, Value
* result
,
476 barvinok_options
*options
)
479 value_set_si(*result
, 0);
483 if (P
->Dimension
== 1)
484 return Line_Length(P
, result
);
486 int c
= P
->NbConstraints
;
487 POL_ENSURE_FACETS(P
);
488 if (c
!= P
->NbConstraints
|| P
->NbEq
!= 0) {
489 Polyhedron
*next
= P
->next
;
491 barvinok_count_with_options(P
, result
, options
);
496 POL_ENSURE_VERTICES(P
);
498 if (Polyhedron_is_infinite(P
, result
, options
))
502 if (options
->incremental_specialization
== BV_SPECIALIZATION_BF
)
503 cnt
= new bfcounter(P
->Dimension
);
504 else if (options
->incremental_specialization
== BV_SPECIALIZATION_DF
)
505 cnt
= new icounter(P
->Dimension
);
506 else if (options
->incremental_specialization
== BV_SPECIALIZATION_TODD
)
507 cnt
= new tcounter(P
->Dimension
, options
->max_index
);
509 cnt
= new counter(P
->Dimension
, options
->max_index
);
510 cnt
->start(P
, options
);
512 cnt
->get_count(result
);
516 static void uni_polynom(int param
, Vector
*c
, evalue
*EP
)
518 unsigned dim
= c
->Size
-2;
520 value_set_si(EP
->d
,0);
521 EP
->x
.p
= new_enode(polynomial
, dim
+1, param
+1);
522 for (int j
= 0; j
<= dim
; ++j
)
523 evalue_set(&EP
->x
.p
->arr
[j
], c
->p
[j
], c
->p
[dim
+1]);
526 typedef evalue
* evalue_p
;
528 struct enumerator_base
{
532 vertex_decomposer
*vpd
;
534 enumerator_base(unsigned dim
, vertex_decomposer
*vpd
)
539 vE
= new evalue_p
[vpd
->PP
->nbV
];
540 for (int j
= 0; j
< vpd
->PP
->nbV
; ++j
)
544 evalue_set_si(&mone
, -1, 1);
547 void decompose_at(Param_Vertices
*V
, int _i
, barvinok_options
*options
) {
551 value_init(vE
[_i
]->d
);
552 evalue_set_si(vE
[_i
], 0, 1);
554 vpd
->decompose_at_vertex(V
, _i
, options
);
557 virtual ~enumerator_base() {
558 for (int j
= 0; j
< vpd
->PP
->nbV
; ++j
)
560 free_evalue_refs(vE
[j
]);
565 free_evalue_refs(&mone
);
568 static enumerator_base
*create(Polyhedron
*P
, unsigned dim
,
569 Param_Polyhedron
*PP
,
570 barvinok_options
*options
);
573 struct enumerator
: public signed_cone_consumer
, public vertex_decomposer
,
574 public enumerator_base
{
582 enumerator(Polyhedron
*P
, unsigned dim
, Param_Polyhedron
*PP
) :
583 vertex_decomposer(PP
, *this), enumerator_base(dim
, this) {
584 randomvector(P
, lambda
, dim
, 0);
586 c
= Vector_Alloc(dim
+2);
598 virtual void handle(const signed_cone
& sc
, barvinok_options
*options
);
601 void enumerator::handle(const signed_cone
& sc
, barvinok_options
*options
)
605 assert(sc
.rays
.NumRows() == dim
);
606 for (int k
= 0; k
< dim
; ++k
) {
607 if (lambda
* sc
.rays
[k
] == 0)
611 lattice_point(V
, sc
.rays
, lambda
, &num
, sc
.det
, options
);
612 den
= sc
.rays
* lambda
;
617 zz2value(den
[0], tz
);
619 for (int k
= 1; k
< dim
; ++k
) {
620 zz2value(den
[k
], tz
);
621 dpoly
fact(dim
, tz
, 1);
627 for (unsigned long i
= 0; i
< sc
.det
; ++i
) {
628 evalue
*EV
= evalue_polynomial(c
, num
.E
[i
]);
631 evalue_free(num
.E
[i
]);
635 mpq_set_si(count
, 0, 1);
636 if (num
.constant
.length() == 1) {
637 zz2value(num
.constant
[0], tz
);
639 d
.div(n
, count
, sign
);
646 for (unsigned long i
= 0; i
< sc
.det
; ++i
) {
647 value_assign(acc
, c
->p
[dim
]);
648 zz2value(num
.constant
[i
], x
);
649 for (int j
= dim
-1; j
>= 0; --j
) {
650 value_multiply(acc
, acc
, x
);
651 value_addto(acc
, acc
, c
->p
[j
]);
653 value_addto(mpq_numref(count
), mpq_numref(count
), acc
);
655 mpz_set(mpq_denref(count
), c
->p
[dim
+1]);
661 evalue_set(&EV
, &count
[0]._mp_num
, &count
[0]._mp_den
);
663 free_evalue_refs(&EV
);
667 struct ienumerator_base
: enumerator_base
{
670 ienumerator_base(unsigned dim
, vertex_decomposer
*vpd
) :
671 enumerator_base(dim
,vpd
) {
672 E_vertex
= new evalue_p
[dim
];
675 virtual ~ienumerator_base() {
679 evalue
*E_num(int i
, int d
) {
680 return E_vertex
[i
+ (dim
-d
)];
689 cumulator(evalue
*factor
, evalue
*v
, dpoly_r
*r
) :
690 factor(factor
), v(v
), r(r
) {}
692 void cumulate(barvinok_options
*options
);
694 virtual void add_term(const vector
<int>& powers
, evalue
*f2
) = 0;
695 virtual ~cumulator() {}
698 void cumulator::cumulate(barvinok_options
*options
)
700 evalue cum
; // factor * 1 * E_num[0]/1 * (E_num[0]-1)/2 *...
702 evalue t
; // E_num[0] - (m-1)
706 if (options
->lookup_table
) {
708 evalue_set_si(&mone
, -1, 1);
712 evalue_copy(&cum
, factor
);
715 value_set_si(f
.d
, 1);
716 value_set_si(f
.x
.n
, 1);
720 if (!options
->lookup_table
) {
721 for (cst
= &t
; value_zero_p(cst
->d
); ) {
722 if (cst
->x
.p
->type
== fractional
)
723 cst
= &cst
->x
.p
->arr
[1];
725 cst
= &cst
->x
.p
->arr
[0];
729 for (int m
= 0; m
< r
->len
; ++m
) {
732 value_set_si(f
.d
, m
);
734 if (!options
->lookup_table
)
735 value_subtract(cst
->x
.n
, cst
->x
.n
, cst
->d
);
741 dpoly_r_term_list
& current
= r
->c
[r
->len
-1-m
];
742 dpoly_r_term_list::iterator j
;
743 for (j
= current
.begin(); j
!= current
.end(); ++j
) {
744 if ((*j
)->coeff
== 0)
746 evalue
*f2
= new evalue
;
749 zz2value((*j
)->coeff
, f2
->x
.n
);
750 zz2value(r
->denom
, f2
->d
);
753 add_term((*j
)->powers
, f2
);
756 free_evalue_refs(&f
);
757 free_evalue_refs(&t
);
758 free_evalue_refs(&cum
);
759 if (options
->lookup_table
)
760 free_evalue_refs(&mone
);
768 struct ie_cum
: public cumulator
{
769 vector
<E_poly_term
*> terms
;
771 ie_cum(evalue
*factor
, evalue
*v
, dpoly_r
*r
) : cumulator(factor
, v
, r
) {}
773 virtual void add_term(const vector
<int>& powers
, evalue
*f2
);
776 void ie_cum::add_term(const vector
<int>& powers
, evalue
*f2
)
779 for (k
= 0; k
< terms
.size(); ++k
) {
780 if (terms
[k
]->powers
== powers
) {
781 eadd(f2
, terms
[k
]->E
);
782 free_evalue_refs(f2
);
787 if (k
>= terms
.size()) {
788 E_poly_term
*ET
= new E_poly_term
;
795 struct ienumerator
: public signed_cone_consumer
, public vertex_decomposer
,
796 public ienumerator_base
{
803 ienumerator(Polyhedron
*P
, unsigned dim
, Param_Polyhedron
*PP
) :
804 vertex_decomposer(PP
, *this), ienumerator_base(dim
, this) {
805 vertex
.SetDims(1, dim
);
807 den
.SetDims(dim
, dim
);
817 virtual void handle(const signed_cone
& sc
, barvinok_options
*options
);
818 void reduce(evalue
*factor
, const mat_ZZ
& num
, const mat_ZZ
& den_f
,
819 barvinok_options
*options
);
822 void ienumerator::reduce(evalue
*factor
, const mat_ZZ
& num
, const mat_ZZ
& den_f
,
823 barvinok_options
*options
)
825 unsigned len
= den_f
.NumRows(); // number of factors in den
826 unsigned dim
= num
.NumCols();
827 assert(num
.NumRows() == 1);
830 eadd(factor
, vE
[vert
]);
839 split_one(num
, num_s
, num_p
, den_f
, den_s
, den_r
);
842 den_p
.SetLength(len
);
846 normalize(one
, num_s
, num_p
, den_s
, den_p
, den_r
);
852 for (int k
= 0; k
< len
; ++k
) {
855 else if (den_s
[k
] == 0)
859 reduce(factor
, num_p
, den_r
, options
);
863 pden
.SetDims(only_param
, dim
-1);
865 for (k
= 0, l
= 0; k
< len
; ++k
)
867 pden
[l
++] = den_r
[k
];
869 for (k
= 0; k
< len
; ++k
)
873 zz2value(num_s
[0], tz
);
874 dpoly
n(no_param
, tz
);
875 zz2value(den_s
[k
], tz
);
876 dpoly
D(no_param
, tz
, 1);
879 zz2value(den_s
[k
], tz
);
880 dpoly
fact(no_param
, tz
, 1);
885 // if no_param + only_param == len then all powers
886 // below will be all zero
887 if (no_param
+ only_param
== len
) {
888 if (E_num(0, dim
) != 0)
889 r
= new dpoly_r(n
, len
);
891 mpq_set_si(tcount
, 0, 1);
895 if (value_notzero_p(mpq_numref(tcount
))) {
899 value_assign(f
.x
.n
, mpq_numref(tcount
));
900 value_assign(f
.d
, mpq_denref(tcount
));
902 reduce(factor
, num_p
, pden
, options
);
903 free_evalue_refs(&f
);
908 for (k
= 0; k
< len
; ++k
) {
909 if (den_s
[k
] == 0 || den_p
[k
] == 0)
912 zz2value(den_s
[k
], tz
);
913 dpoly
pd(no_param
-1, tz
, 1);
916 for (l
= 0; l
< k
; ++l
)
917 if (den_r
[l
] == den_r
[k
])
921 r
= new dpoly_r(n
, pd
, l
, len
);
923 dpoly_r
*nr
= new dpoly_r(r
, pd
, l
, len
);
929 dpoly_r
*rc
= r
->div(D
);
932 if (E_num(0, dim
) == 0) {
933 int common
= pden
.NumRows();
934 dpoly_r_term_list
& final
= r
->c
[r
->len
-1];
940 zz2value(r
->denom
, f
.d
);
941 dpoly_r_term_list::iterator j
;
942 for (j
= final
.begin(); j
!= final
.end(); ++j
) {
943 if ((*j
)->coeff
== 0)
946 for (int k
= 0; k
< r
->dim
; ++k
) {
947 int n
= (*j
)->powers
[k
];
950 pden
.SetDims(rows
+n
, pden
.NumCols());
951 for (int l
= 0; l
< n
; ++l
)
952 pden
[rows
+l
] = den_r
[k
];
956 evalue_copy(&t
, factor
);
957 zz2value((*j
)->coeff
, f
.x
.n
);
959 reduce(&t
, num_p
, pden
, options
);
960 free_evalue_refs(&t
);
962 free_evalue_refs(&f
);
964 ie_cum
cum(factor
, E_num(0, dim
), r
);
965 cum
.cumulate(options
);
967 int common
= pden
.NumRows();
969 for (int j
= 0; j
< cum
.terms
.size(); ++j
) {
971 pden
.SetDims(rows
, pden
.NumCols());
972 for (int k
= 0; k
< r
->dim
; ++k
) {
973 int n
= cum
.terms
[j
]->powers
[k
];
976 pden
.SetDims(rows
+n
, pden
.NumCols());
977 for (int l
= 0; l
< n
; ++l
)
978 pden
[rows
+l
] = den_r
[k
];
981 reduce(cum
.terms
[j
]->E
, num_p
, pden
, options
);
982 free_evalue_refs(cum
.terms
[j
]->E
);
983 delete cum
.terms
[j
]->E
;
991 static int type_offset(enode
*p
)
993 return p
->type
== fractional
? 1 :
994 p
->type
== flooring
? 1 : 0;
997 static int edegree(evalue
*e
)
1002 if (value_notzero_p(e
->d
))
1006 int i
= type_offset(p
);
1007 if (p
->size
-i
-1 > d
)
1008 d
= p
->size
- i
- 1;
1009 for (; i
< p
->size
; i
++) {
1010 int d2
= edegree(&p
->arr
[i
]);
1017 void ienumerator::handle(const signed_cone
& sc
, barvinok_options
*options
)
1019 assert(sc
.det
== 1);
1020 assert(sc
.rays
.NumRows() == dim
);
1022 lattice_point(V
, sc
.rays
, vertex
[0], E_vertex
, options
);
1028 evalue_set_si(&one
, sc
.sign
, 1);
1029 reduce(&one
, vertex
, den
, options
);
1030 free_evalue_refs(&one
);
1032 for (int i
= 0; i
< dim
; ++i
)
1034 evalue_free(E_vertex
[i
]);
1037 struct bfenumerator
: public vertex_decomposer
, public bf_base
,
1038 public ienumerator_base
{
1041 bfenumerator(Polyhedron
*P
, unsigned dim
, Param_Polyhedron
*PP
) :
1042 vertex_decomposer(PP
, *this),
1043 bf_base(dim
), ienumerator_base(dim
, this) {
1051 virtual void handle(const signed_cone
& sc
, barvinok_options
*options
);
1052 virtual void base(mat_ZZ
& factors
, bfc_vec
& v
);
1054 bfc_term_base
* new_bf_term(int len
) {
1055 bfe_term
* t
= new bfe_term(len
);
1059 virtual void set_factor(bfc_term_base
*t
, int k
, int change
) {
1060 bfe_term
* bfet
= static_cast<bfe_term
*>(t
);
1061 factor
= bfet
->factors
[k
];
1062 assert(factor
!= NULL
);
1063 bfet
->factors
[k
] = NULL
;
1065 emul(&mone
, factor
);
1068 virtual void set_factor(bfc_term_base
*t
, int k
, mpq_t
&q
, int change
) {
1069 bfe_term
* bfet
= static_cast<bfe_term
*>(t
);
1070 factor
= bfet
->factors
[k
];
1071 assert(factor
!= NULL
);
1072 bfet
->factors
[k
] = NULL
;
1078 value_oppose(f
.x
.n
, mpq_numref(q
));
1080 value_assign(f
.x
.n
, mpq_numref(q
));
1081 value_assign(f
.d
, mpq_denref(q
));
1083 free_evalue_refs(&f
);
1086 virtual void set_factor(bfc_term_base
*t
, int k
, const QQ
& c
, int change
) {
1087 bfe_term
* bfet
= static_cast<bfe_term
*>(t
);
1089 factor
= new evalue
;
1094 zz2value(c
.n
, f
.x
.n
);
1096 value_oppose(f
.x
.n
, f
.x
.n
);
1099 value_init(factor
->d
);
1100 evalue_copy(factor
, bfet
->factors
[k
]);
1102 free_evalue_refs(&f
);
1105 void set_factor(evalue
*f
, int change
) {
1111 virtual void insert_term(bfc_term_base
*t
, int i
) {
1112 bfe_term
* bfet
= static_cast<bfe_term
*>(t
);
1113 int len
= t
->terms
.NumRows()-1; // already increased by one
1115 bfet
->factors
.resize(len
+1);
1116 for (int j
= len
; j
> i
; --j
) {
1117 bfet
->factors
[j
] = bfet
->factors
[j
-1];
1118 t
->terms
[j
] = t
->terms
[j
-1];
1120 bfet
->factors
[i
] = factor
;
1124 virtual void update_term(bfc_term_base
*t
, int i
) {
1125 bfe_term
* bfet
= static_cast<bfe_term
*>(t
);
1127 eadd(factor
, bfet
->factors
[i
]);
1128 free_evalue_refs(factor
);
1132 virtual bool constant_vertex(int dim
) { return E_num(0, dim
) == 0; }
1134 virtual void cum(bf_reducer
*bfr
, bfc_term_base
*t
, int k
, dpoly_r
*r
,
1135 barvinok_options
*options
);
1138 enumerator_base
*enumerator_base::create(Polyhedron
*P
, unsigned dim
,
1139 Param_Polyhedron
*PP
,
1140 barvinok_options
*options
)
1142 enumerator_base
*eb
;
1144 if (options
->incremental_specialization
== BV_SPECIALIZATION_BF
)
1145 eb
= new bfenumerator(P
, dim
, PP
);
1146 else if (options
->incremental_specialization
== BV_SPECIALIZATION_DF
)
1147 eb
= new ienumerator(P
, dim
, PP
);
1149 eb
= new enumerator(P
, dim
, PP
);
1154 struct bfe_cum
: public cumulator
{
1156 bfc_term_base
*told
;
1160 bfe_cum(evalue
*factor
, evalue
*v
, dpoly_r
*r
, bf_reducer
*bfr
,
1161 bfc_term_base
*t
, int k
, bfenumerator
*e
) :
1162 cumulator(factor
, v
, r
), told(t
), k(k
),
1166 virtual void add_term(const vector
<int>& powers
, evalue
*f2
);
1169 void bfe_cum::add_term(const vector
<int>& powers
, evalue
*f2
)
1171 bfr
->update_powers(powers
);
1173 bfc_term_base
* t
= bfe
->find_bfc_term(bfr
->vn
, bfr
->npowers
, bfr
->nnf
);
1174 bfe
->set_factor(f2
, bfr
->l_changes
% 2);
1175 bfe
->add_term(t
, told
->terms
[k
], bfr
->l_extra_num
);
1178 void bfenumerator::cum(bf_reducer
*bfr
, bfc_term_base
*t
, int k
,
1179 dpoly_r
*r
, barvinok_options
*options
)
1181 bfe_term
* bfet
= static_cast<bfe_term
*>(t
);
1182 bfe_cum
cum(bfet
->factors
[k
], E_num(0, bfr
->d
), r
, bfr
, t
, k
, this);
1183 cum
.cumulate(options
);
1186 void bfenumerator::base(mat_ZZ
& factors
, bfc_vec
& v
)
1188 for (int i
= 0; i
< v
.size(); ++i
) {
1189 assert(v
[i
]->terms
.NumRows() == 1);
1190 evalue
*factor
= static_cast<bfe_term
*>(v
[i
])->factors
[0];
1191 eadd(factor
, vE
[vert
]);
1196 void bfenumerator::handle(const signed_cone
& sc
, barvinok_options
*options
)
1198 assert(sc
.det
== 1);
1199 assert(sc
.rays
.NumRows() == enumerator_base::dim
);
1201 bfe_term
* t
= new bfe_term(enumerator_base::dim
);
1202 vector
< bfc_term_base
* > v
;
1205 t
->factors
.resize(1);
1207 t
->terms
.SetDims(1, enumerator_base::dim
);
1208 lattice_point(V
, sc
.rays
, t
->terms
[0], E_vertex
, options
);
1210 // the elements of factors are always lexpositive
1212 int s
= setup_factors(sc
.rays
, factors
, t
, sc
.sign
);
1214 t
->factors
[0] = new evalue
;
1215 value_init(t
->factors
[0]->d
);
1216 evalue_set_si(t
->factors
[0], s
, 1);
1217 reduce(factors
, v
, options
);
1219 for (int i
= 0; i
< enumerator_base::dim
; ++i
)
1221 evalue_free(E_vertex
[i
]);
1224 static evalue
* barvinok_enumerate_ev_f(Polyhedron
*P
, Polyhedron
* C
,
1225 barvinok_options
*options
);
1228 static evalue
* barvinok_enumerate_cst(Polyhedron
*P
, Polyhedron
* C
,
1229 struct barvinok_options
*options
)
1235 return evalue_zero();
1238 ALLOC(evalue
, eres
);
1239 value_init(eres
->d
);
1240 value_set_si(eres
->d
, 0);
1241 eres
->x
.p
= new_enode(partition
, 2, C
->Dimension
);
1242 EVALUE_SET_DOMAIN(eres
->x
.p
->arr
[0],
1243 DomainConstraintSimplify(C
, options
->MaxRays
));
1244 value_set_si(eres
->x
.p
->arr
[1].d
, 1);
1245 value_init(eres
->x
.p
->arr
[1].x
.n
);
1247 value_set_si(eres
->x
.p
->arr
[1].x
.n
, 0);
1249 barvinok_count_with_options(P
, &eres
->x
.p
->arr
[1].x
.n
, options
);
1250 if (value_mone_p(eres
->x
.p
->arr
[1].x
.n
)) {
1251 value_clear(eres
->x
.p
->arr
[1].x
.n
);
1252 value_set_si(eres
->x
.p
->arr
[1].d
, -2); /* NaN */
1258 static evalue
* enumerate(Polyhedron
*P
, Polyhedron
* C
,
1259 struct barvinok_options
*options
)
1262 Polyhedron
*Porig
= P
;
1263 Polyhedron
*Corig
= C
;
1264 Polyhedron
*CEq
= NULL
, *rVD
;
1266 unsigned nparam
= C
->Dimension
;
1271 value_init(factor
.d
);
1272 evalue_set_si(&factor
, 1, 1);
1275 POL_ENSURE_FACETS(P
);
1276 POL_ENSURE_VERTICES(P
);
1277 POL_ENSURE_FACETS(C
);
1278 POL_ENSURE_VERTICES(C
);
1280 if (C
->Dimension
== 0 || emptyQ(P
) || emptyQ(C
)) {
1283 CEq
= Polyhedron_Copy(CEq
);
1284 eres
= barvinok_enumerate_cst(P
, CEq
? CEq
: Polyhedron_Copy(C
), options
);
1287 evalue_backsubstitute(eres
, CP
, options
->MaxRays
);
1291 emul(&factor
, eres
);
1292 if (options
->approx
->method
== BV_APPROX_DROP
) {
1293 if (options
->approx
->approximation
== BV_APPROX_SIGN_UPPER
)
1294 evalue_frac2polynomial(eres
, 1, options
->MaxRays
);
1295 if (options
->approx
->approximation
== BV_APPROX_SIGN_LOWER
)
1296 evalue_frac2polynomial(eres
, -1, options
->MaxRays
);
1297 if (options
->approx
->approximation
== BV_APPROX_SIGN_APPROX
)
1298 evalue_frac2polynomial(eres
, 0, options
->MaxRays
);
1300 reduce_evalue(eres
);
1301 free_evalue_refs(&factor
);
1309 if (Polyhedron_is_unbounded(P
, nparam
, options
->MaxRays
))
1312 if (P
->Dimension
== nparam
) {
1314 P
= Universe_Polyhedron(0);
1317 if (P
->NbEq
!= 0 || C
->NbEq
!= 0) {
1320 remove_all_equalities(&P
, &C
, &CP
, NULL
, nparam
, options
->MaxRays
);
1321 if (C
!= D
&& D
!= Corig
)
1323 if (P
!= Q
&& Q
!= Porig
)
1325 eres
= enumerate(P
, C
, options
);
1329 Polyhedron
*T
= Polyhedron_Factor(P
, nparam
, NULL
, options
->MaxRays
);
1330 if (T
|| (P
->Dimension
== nparam
+1)) {
1332 Polyhedron
*FC
= Factor_Context(T
? T
: P
, nparam
, options
->MaxRays
);
1333 C
= DomainIntersection(C
, FC
, options
->MaxRays
);
1335 Polyhedron_Free(C2
);
1336 Polyhedron_Free(FC
);
1342 if (T
->Dimension
== C
->Dimension
) {
1351 eres
= barvinok_enumerate_ev_f(P
, C
, options
);
1358 for (Q
= P
->next
; Q
; Q
= Q
->next
) {
1359 Polyhedron
*next
= Q
->next
;
1362 f
= barvinok_enumerate_ev_f(Q
, C
, options
);
1373 evalue
* barvinok_enumerate_with_options(Polyhedron
*P
, Polyhedron
* C
,
1374 struct barvinok_options
*options
)
1376 Polyhedron
*next
, *Cnext
, *C1
;
1377 Polyhedron
*Corig
= C
;
1382 "barvinok_enumerate: input is a union; only first polyhedron is enumerated\n");
1386 "barvinok_enumerate: context is a union; only first polyhedron is considered\n");
1390 C1
= Polyhedron_Project(P
, C
->Dimension
);
1391 C
= DomainIntersection(C
, C1
, options
->MaxRays
);
1392 Polyhedron_Free(C1
);
1396 if (options
->approx
->method
== BV_APPROX_BERNOULLI
||
1397 options
->summation
== BV_SUM_BERNOULLI
) {
1398 int summation
= options
->summation
;
1399 options
->summation
= BV_SUM_BERNOULLI
;
1400 eres
= barvinok_summate_unweighted(P
, C
, options
);
1401 options
->summation
= summation
;
1403 eres
= enumerate(P
, C
, options
);
1407 Corig
->next
= Cnext
;
1412 evalue
* barvinok_enumerate_ev(Polyhedron
*P
, Polyhedron
* C
, unsigned MaxRays
)
1415 barvinok_options
*options
= barvinok_options_new_with_defaults();
1416 options
->MaxRays
= MaxRays
;
1417 E
= barvinok_enumerate_with_options(P
, C
, options
);
1418 barvinok_options_free(options
);
1422 evalue
*Param_Polyhedron_Enumerate(Param_Polyhedron
*PP
, Polyhedron
*P
,
1424 struct barvinok_options
*options
)
1428 unsigned nparam
= C
->Dimension
;
1429 unsigned dim
= P
->Dimension
- nparam
;
1432 for (nd
= 0, D
=PP
->D
; D
; ++nd
, D
=D
->next
);
1433 evalue_section
*s
= new evalue_section
[nd
];
1435 enumerator_base
*et
= NULL
;
1440 et
= enumerator_base::create(P
, dim
, PP
, options
);
1442 Polyhedron
*TC
= true_context(P
, C
, options
->MaxRays
);
1443 FORALL_REDUCED_DOMAIN(PP
, TC
, nd
, options
, i
, D
, rVD
)
1446 s
[i
].E
= evalue_zero();
1449 FORALL_PVertex_in_ParamPolyhedron(V
,D
,PP
) // _i is internal counter
1452 et
->decompose_at(V
, _i
, options
);
1453 } catch (OrthogonalException
&e
) {
1454 FORALL_REDUCED_DOMAIN_RESET
;
1455 for (; i
>= 0; --i
) {
1456 evalue_free(s
[i
].E
);
1457 Domain_Free(s
[i
].D
);
1461 eadd(et
->vE
[_i
] , s
[i
].E
);
1462 END_FORALL_PVertex_in_ParamPolyhedron
;
1463 evalue_range_reduction_in_domain(s
[i
].E
, rVD
);
1464 END_FORALL_REDUCED_DOMAIN
1465 Polyhedron_Free(TC
);
1468 eres
= evalue_from_section_array(s
, nd
);
1474 static evalue
* barvinok_enumerate_ev_f(Polyhedron
*P
, Polyhedron
* C
,
1475 barvinok_options
*options
)
1477 unsigned nparam
= C
->Dimension
;
1478 bool do_scale
= options
->approx
->method
== BV_APPROX_SCALE
;
1480 if (options
->summation
== BV_SUM_EULER
)
1481 return barvinok_summate_unweighted(P
, C
, options
);
1483 if (options
->approx
->method
== BV_APPROX_VOLUME
)
1484 return Param_Polyhedron_Volume(P
, C
, options
);
1486 if (P
->Dimension
- nparam
== 1 && !do_scale
)
1487 return ParamLine_Length(P
, C
, options
);
1489 Param_Polyhedron
*PP
= NULL
;
1493 eres
= scale_bound(P
, C
, options
);
1498 PP
= Polyhedron2Param_Polyhedron(P
, C
, options
);
1501 eres
= scale(PP
, P
, C
, options
);
1503 eres
= Param_Polyhedron_Enumerate(PP
, P
, C
, options
);
1506 Param_Polyhedron_Free(PP
);
1511 Enumeration
* barvinok_enumerate(Polyhedron
*P
, Polyhedron
* C
, unsigned MaxRays
)
1513 evalue
*EP
= barvinok_enumerate_ev(P
, C
, MaxRays
);
1515 return partition2enumeration(EP
);
1518 evalue
* barvinok_enumerate_union(Polyhedron
*D
, Polyhedron
* C
, unsigned MaxRays
)
1521 gen_fun
*gf
= barvinok_enumerate_union_series(D
, C
, MaxRays
);
1527 static int basic_map_card(__isl_take isl_basic_map
*bmap
, void *user
)
1529 isl_pw_qpolynomial
**sum
= (isl_pw_qpolynomial
**)user
;
1530 isl_pw_qpolynomial
*pwqp
;
1533 barvinok_options
*options
= barvinok_options_new_with_defaults();
1534 unsigned nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
1535 unsigned n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
1536 Polyhedron
*U
= Universe_Polyhedron(nparam
+ n_in
);
1537 isl_dim
*dim
, *target_dim
;
1539 target_dim
= isl_basic_map_get_dim(bmap
);
1540 target_dim
= isl_dim_domain(target_dim
);
1542 bmap
= isl_basic_map_move_dims(bmap
, isl_dim_param
, nparam
,
1543 isl_dim_in
, 0, n_in
);
1545 dim
= isl_basic_map_get_dim(bmap
);
1546 dim
= isl_dim_domain(dim
);
1548 P
= isl_basic_map_to_polylib(bmap
);
1549 E
= barvinok_enumerate_with_options(P
, U
, options
);
1551 pwqp
= isl_pw_qpolynomial_from_evalue(dim
, E
);
1552 pwqp
= isl_pw_qpolynomial_move_dims(pwqp
, isl_dim_set
, 0,
1553 isl_dim_param
, nparam
, n_in
);
1554 pwqp
= isl_pw_qpolynomial_reset_dim(pwqp
, target_dim
);
1555 *sum
= isl_pw_qpolynomial_add(*sum
, pwqp
);
1560 barvinok_options_free(options
);
1561 isl_basic_map_free(bmap
);
1566 __isl_give isl_pw_qpolynomial
*isl_map_card(__isl_take isl_map
*map
)
1569 isl_pw_qpolynomial
*sum
;
1571 dim
= isl_map_get_dim(map
);
1572 dim
= isl_dim_domain(dim
);
1573 sum
= isl_pw_qpolynomial_zero(dim
);
1575 map
= isl_map_make_disjoint(map
);
1576 map
= isl_map_compute_divs(map
);
1578 if (isl_map_foreach_basic_map(map
, &basic_map_card
, &sum
) < 0)
1586 isl_pw_qpolynomial_free(sum
);
1590 __isl_give isl_pw_qpolynomial
*isl_set_card(__isl_take isl_set
*set
)
1592 return isl_map_card(isl_map_from_range(set
));
1595 static int set_card(__isl_take isl_set
*set
, void *user
)
1597 isl_union_pw_qpolynomial
**res
= (isl_union_pw_qpolynomial
**)user
;
1598 isl_pw_qpolynomial
*pwqp
;
1600 pwqp
= isl_set_card(set
);
1601 *res
= isl_union_pw_qpolynomial_add_pw_qpolynomial(*res
, pwqp
);
1606 __isl_give isl_union_pw_qpolynomial
*isl_union_set_card(
1607 __isl_take isl_union_set
*uset
)
1610 isl_union_pw_qpolynomial
*res
;
1612 dim
= isl_union_set_get_dim(uset
);
1613 res
= isl_union_pw_qpolynomial_zero(dim
);
1614 if (isl_union_set_foreach_set(uset
, &set_card
, &res
) < 0)
1616 isl_union_set_free(uset
);
1620 isl_union_set_free(uset
);
1621 isl_union_pw_qpolynomial_free(res
);
1625 static int map_card(__isl_take isl_map
*map
, void *user
)
1627 isl_union_pw_qpolynomial
**res
= (isl_union_pw_qpolynomial
**)user
;
1628 isl_pw_qpolynomial
*pwqp
;
1630 pwqp
= isl_map_card(map
);
1631 *res
= isl_union_pw_qpolynomial_add_pw_qpolynomial(*res
, pwqp
);
1636 __isl_give isl_union_pw_qpolynomial
*isl_union_map_card(
1637 __isl_take isl_union_map
*umap
)
1640 isl_union_pw_qpolynomial
*res
;
1642 dim
= isl_union_map_get_dim(umap
);
1643 res
= isl_union_pw_qpolynomial_zero(dim
);
1644 if (isl_union_map_foreach_map(umap
, &map_card
, &res
) < 0)
1646 isl_union_map_free(umap
);
1650 isl_union_map_free(umap
);
1651 isl_union_pw_qpolynomial_free(res
);