8 #include <NTL/mat_ZZ.h>
11 #include <isl_set_polylib.h>
12 #include <barvinok/util.h>
13 #include <barvinok/evalue.h>
15 #include <barvinok/barvinok.h>
16 #include <barvinok/genfun.h>
17 #include <barvinok/options.h>
18 #include <barvinok/sample.h>
19 #include "bfcounter.h"
20 #include "conversion.h"
22 #include "decomposer.h"
24 #include "lattice_point.h"
26 #include "reduce_domain.h"
27 #include "remove_equalities.h"
30 #include "bernoulli.h"
31 #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 struct bfe_term
: public bfc_term_base
{
99 vector
<evalue
*> factors
;
101 bfe_term(int len
) : bfc_term_base(len
) {
105 for (int i
= 0; i
< factors
.size(); ++i
) {
108 free_evalue_refs(factors
[i
]);
114 static void print_int_vector(int *v
, int len
, const char *name
)
116 cerr
<< name
<< endl
;
117 for (int j
= 0; j
< len
; ++j
) {
123 static void print_bfc_terms(mat_ZZ
& factors
, bfc_vec
& v
)
126 cerr
<< "factors" << endl
;
127 cerr
<< factors
<< endl
;
128 for (int i
= 0; i
< v
.size(); ++i
) {
129 cerr
<< "term: " << i
<< endl
;
130 print_int_vector(v
[i
]->powers
, factors
.NumRows(), "powers");
131 cerr
<< "terms" << endl
;
132 cerr
<< v
[i
]->terms
<< endl
;
133 bfc_term
* bfct
= static_cast<bfc_term
*>(v
[i
]);
134 cerr
<< bfct
->c
<< endl
;
138 static void print_bfe_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 bfe_term
* bfet
= static_cast<bfe_term
*>(v
[i
]);
149 for (int j
= 0; j
< v
[i
]->terms
.NumRows(); ++j
) {
150 const char * test
[] = {"a", "b"};
151 print_evalue(stderr
, bfet
->factors
[j
], test
);
152 fprintf(stderr
, "\n");
157 struct bfcounter
: public bfcounter_base
{
161 bfcounter(unsigned dim
) : bfcounter_base(dim
) {
170 virtual void base(mat_ZZ
& factors
, bfc_vec
& v
);
171 virtual void get_count(Value
*result
) {
172 assert(value_one_p(&count
[0]._mp_den
));
173 value_assign(*result
, &count
[0]._mp_num
);
177 void bfcounter::base(mat_ZZ
& factors
, bfc_vec
& v
)
179 unsigned nf
= factors
.NumRows();
181 for (int i
= 0; i
< v
.size(); ++i
) {
182 bfc_term
* bfct
= static_cast<bfc_term
*>(v
[i
]);
184 // factor is always positive, so we always
186 for (int k
= 0; k
< nf
; ++k
)
187 total_power
+= v
[i
]->powers
[k
];
190 for (j
= 0; j
< nf
; ++j
)
191 if (v
[i
]->powers
[j
] > 0)
194 zz2value(factors
[j
][0], tz
);
195 dpoly
D(total_power
, tz
, 1);
196 for (int k
= 1; k
< v
[i
]->powers
[j
]; ++k
) {
197 zz2value(factors
[j
][0], tz
);
198 dpoly
fact(total_power
, tz
, 1);
202 for (int k
= 0; k
< v
[i
]->powers
[j
]; ++k
) {
203 zz2value(factors
[j
][0], tz
);
204 dpoly
fact(total_power
, tz
, 1);
208 for (int k
= 0; k
< v
[i
]->terms
.NumRows(); ++k
) {
209 zz2value(v
[i
]->terms
[k
][0], tz
);
210 dpoly
n(total_power
, tz
);
211 mpq_set_si(tcount
, 0, 1);
214 bfct
->c
[k
].n
= -bfct
->c
[k
].n
;
215 zz2value(bfct
->c
[k
].n
, tn
);
216 zz2value(bfct
->c
[k
].d
, td
);
218 mpz_mul(mpq_numref(tcount
), mpq_numref(tcount
), tn
);
219 mpz_mul(mpq_denref(tcount
), mpq_denref(tcount
), td
);
220 mpq_canonicalize(tcount
);
221 mpq_add(count
, count
, tcount
);
228 /* Check whether the polyhedron is unbounded and if so,
229 * check whether it has any (and therefore an infinite number of)
231 * If one of the vertices is integer, then we are done.
232 * Otherwise, transform the polyhedron such that one of the rays
233 * is the first unit vector and cut it off at a height that ensures
234 * that if the whole polyhedron has any points, then the remaining part
235 * has integer points. In particular we add the largest coefficient
236 * of a ray to the highest vertex (rounded up).
238 static bool Polyhedron_is_infinite(Polyhedron
*P
, Value
* result
,
239 barvinok_options
*options
)
251 for (; r
< P
->NbRays
; ++r
)
252 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1]))
254 if (P
->NbBid
== 0 && r
== P
->NbRays
)
257 if (options
->count_sample_infinite
) {
260 sample
= Polyhedron_Sample(P
, options
);
262 value_set_si(*result
, 0);
264 value_set_si(*result
, -1);
270 for (int i
= 0; i
< P
->NbRays
; ++i
)
271 if (value_one_p(P
->Ray
[i
][1+P
->Dimension
])) {
272 value_set_si(*result
, -1);
277 M
= Matrix_Alloc(P
->Dimension
+1, P
->Dimension
+1);
278 Vector_Gcd(P
->Ray
[r
]+1, P
->Dimension
, &g
);
279 Vector_AntiScale(P
->Ray
[r
]+1, M
->p
[0], g
, P
->Dimension
+1);
280 int ok
= unimodular_complete(M
, 1);
282 value_set_si(M
->p
[P
->Dimension
][P
->Dimension
], 1);
285 P
= Polyhedron_Preimage(P
, M2
, 0);
293 value_set_si(size
, 0);
295 for (int i
= 0; i
< P
->NbBid
; ++i
) {
296 value_absolute(tmp
, P
->Ray
[i
][1]);
297 if (value_gt(tmp
, size
))
298 value_assign(size
, tmp
);
300 for (int i
= P
->NbBid
; i
< P
->NbRays
; ++i
) {
301 if (value_zero_p(P
->Ray
[i
][P
->Dimension
+1])) {
302 if (value_gt(P
->Ray
[i
][1], size
))
303 value_assign(size
, P
->Ray
[i
][1]);
306 mpz_cdiv_q(tmp
, P
->Ray
[i
][1], P
->Ray
[i
][P
->Dimension
+1]);
307 if (first
|| value_gt(tmp
, offset
)) {
308 value_assign(offset
, tmp
);
312 value_addto(offset
, offset
, size
);
316 v
= Vector_Alloc(P
->Dimension
+2);
317 value_set_si(v
->p
[0], 1);
318 value_set_si(v
->p
[1], -1);
319 value_assign(v
->p
[1+P
->Dimension
], offset
);
320 R
= AddConstraints(v
->p
, 1, P
, options
->MaxRays
);
328 barvinok_count_with_options(P
, &c
, options
);
331 value_set_si(*result
, 0);
333 value_set_si(*result
, -1);
339 static void evalue2value(evalue
*e
, Value
*v
)
341 if (EVALUE_IS_ZERO(*e
)) {
346 if (value_notzero_p(e
->d
)) {
347 assert(value_one_p(e
->d
));
348 value_assign(*v
, e
->x
.n
);
352 assert(e
->x
.p
->type
== partition
);
353 assert(e
->x
.p
->size
== 2);
354 assert(EVALUE_DOMAIN(e
->x
.p
->arr
[0])->Dimension
== 0);
355 evalue2value(&e
->x
.p
->arr
[1], v
);
358 static void barvinok_count_f(Polyhedron
*P
, Value
* result
,
359 barvinok_options
*options
);
361 void barvinok_count_with_options(Polyhedron
*P
, Value
* result
,
362 struct barvinok_options
*options
)
367 bool infinite
= false;
371 "barvinok_count: input is a union; only first polyhedron is counted\n");
374 value_set_si(*result
, 0);
380 P
= remove_equalities(P
, options
->MaxRays
);
382 P
= DomainConstraintSimplify(P
, options
->MaxRays
);
386 } while (P
&& !emptyQ(P
) && P
->NbEq
!= 0);
387 if (!P
|| emptyQ(P
)) {
389 value_set_si(*result
, 0);
394 if (Polyhedron_is_infinite(P
, result
, options
)) {
399 if (P
->Dimension
== 0) {
400 /* Test whether the constraints are satisfied */
401 POL_ENSURE_VERTICES(P
);
402 value_set_si(*result
, !emptyQ(P
));
407 if (options
->summation
== BV_SUM_BERNOULLI
) {
408 Polyhedron
*C
= Universe_Polyhedron(0);
409 evalue
*sum
= barvinok_summate_unweighted(P
, C
, options
);
411 evalue2value(sum
, result
);
415 Q
= Polyhedron_Factor(P
, 0, NULL
, options
->MaxRays
);
423 barvinok_count_f(P
, result
, options
);
424 if (value_neg_p(*result
))
426 if (Q
&& P
->next
&& value_notzero_p(*result
)) {
430 for (Q
= P
->next
; Q
; Q
= Q
->next
) {
431 barvinok_count_f(Q
, &factor
, options
);
432 if (value_neg_p(factor
)) {
435 } else if (Q
->next
&& value_zero_p(factor
)) {
436 value_set_si(*result
, 0);
439 value_multiply(*result
, *result
, factor
);
448 value_set_si(*result
, -1);
451 void barvinok_count(Polyhedron
*P
, Value
* result
, unsigned NbMaxCons
)
453 barvinok_options
*options
= barvinok_options_new_with_defaults();
454 options
->MaxRays
= NbMaxCons
;
455 barvinok_count_with_options(P
, result
, options
);
456 barvinok_options_free(options
);
459 static void barvinok_count_f(Polyhedron
*P
, Value
* result
,
460 barvinok_options
*options
)
463 value_set_si(*result
, 0);
467 if (P
->Dimension
== 1)
468 return Line_Length(P
, result
);
470 int c
= P
->NbConstraints
;
471 POL_ENSURE_FACETS(P
);
472 if (c
!= P
->NbConstraints
|| P
->NbEq
!= 0) {
473 Polyhedron
*next
= P
->next
;
475 barvinok_count_with_options(P
, result
, options
);
480 POL_ENSURE_VERTICES(P
);
482 if (Polyhedron_is_infinite(P
, result
, options
))
486 if (options
->incremental_specialization
== BV_SPECIALIZATION_BF
)
487 cnt
= new bfcounter(P
->Dimension
);
488 else if (options
->incremental_specialization
== BV_SPECIALIZATION_DF
)
489 cnt
= new icounter(P
->Dimension
);
490 else if (options
->incremental_specialization
== BV_SPECIALIZATION_TODD
)
491 cnt
= new tcounter(P
->Dimension
, options
->max_index
);
493 cnt
= new counter(P
->Dimension
, options
->max_index
);
494 cnt
->start(P
, options
);
496 cnt
->get_count(result
);
500 typedef evalue
* evalue_p
;
502 struct enumerator_base
{
506 vertex_decomposer
*vpd
;
508 enumerator_base(unsigned dim
, vertex_decomposer
*vpd
)
513 vE
= new evalue_p
[vpd
->PP
->nbV
];
514 for (int j
= 0; j
< vpd
->PP
->nbV
; ++j
)
518 evalue_set_si(&mone
, -1, 1);
521 void decompose_at(Param_Vertices
*V
, int _i
, barvinok_options
*options
) {
525 value_init(vE
[_i
]->d
);
526 evalue_set_si(vE
[_i
], 0, 1);
528 vpd
->decompose_at_vertex(V
, _i
, options
);
531 virtual ~enumerator_base() {
532 for (int j
= 0; j
< vpd
->PP
->nbV
; ++j
)
534 free_evalue_refs(vE
[j
]);
539 free_evalue_refs(&mone
);
542 static enumerator_base
*create(Polyhedron
*P
, unsigned dim
,
543 Param_Polyhedron
*PP
,
544 barvinok_options
*options
);
547 struct enumerator
: public signed_cone_consumer
, public vertex_decomposer
,
548 public enumerator_base
{
556 enumerator(Polyhedron
*P
, unsigned dim
, Param_Polyhedron
*PP
) :
557 vertex_decomposer(PP
, *this), enumerator_base(dim
, this) {
558 randomvector(P
, lambda
, dim
, 0);
560 c
= Vector_Alloc(dim
+2);
572 virtual void handle(const signed_cone
& sc
, barvinok_options
*options
);
575 void enumerator::handle(const signed_cone
& sc
, barvinok_options
*options
)
578 assert(sc
.rays
.NumRows() == dim
);
579 for (int k
= 0; k
< dim
; ++k
) {
580 if (lambda
* sc
.rays
[k
] == 0)
584 lattice_point(V
, sc
.rays
, lambda
, &num
, sc
.det
, options
);
585 den
= sc
.rays
* lambda
;
590 zz2value(den
[0], tz
);
592 for (int k
= 1; k
< dim
; ++k
) {
593 zz2value(den
[k
], tz
);
594 dpoly
fact(dim
, tz
, 1);
600 for (unsigned long i
= 0; i
< sc
.det
; ++i
) {
601 evalue
*EV
= evalue_polynomial(c
, num
.E
[i
]);
604 evalue_free(num
.E
[i
]);
608 mpq_set_si(count
, 0, 1);
609 if (num
.constant
.length() == 1) {
610 zz2value(num
.constant
[0], tz
);
612 d
.div(n
, count
, sign
);
619 for (unsigned long i
= 0; i
< sc
.det
; ++i
) {
620 value_assign(acc
, c
->p
[dim
]);
621 zz2value(num
.constant
[i
], x
);
622 for (int j
= dim
-1; j
>= 0; --j
) {
623 value_multiply(acc
, acc
, x
);
624 value_addto(acc
, acc
, c
->p
[j
]);
626 value_addto(mpq_numref(count
), mpq_numref(count
), acc
);
628 mpz_set(mpq_denref(count
), c
->p
[dim
+1]);
634 evalue_set(&EV
, &count
[0]._mp_num
, &count
[0]._mp_den
);
636 free_evalue_refs(&EV
);
640 struct ienumerator_base
: enumerator_base
{
643 ienumerator_base(unsigned dim
, vertex_decomposer
*vpd
) :
644 enumerator_base(dim
,vpd
) {
645 E_vertex
= new evalue_p
[dim
];
648 virtual ~ienumerator_base() {
652 evalue
*E_num(int i
, int d
) {
653 return E_vertex
[i
+ (dim
-d
)];
662 cumulator(evalue
*factor
, evalue
*v
, dpoly_r
*r
) :
663 factor(factor
), v(v
), r(r
) {}
665 void cumulate(barvinok_options
*options
);
667 virtual void add_term(const vector
<int>& powers
, evalue
*f2
) = 0;
668 virtual ~cumulator() {}
671 void cumulator::cumulate(barvinok_options
*options
)
673 evalue cum
; // factor * 1 * E_num[0]/1 * (E_num[0]-1)/2 *...
675 evalue t
; // E_num[0] - (m-1)
679 if (options
->lookup_table
) {
681 evalue_set_si(&mone
, -1, 1);
685 evalue_copy(&cum
, factor
);
688 value_set_si(f
.d
, 1);
689 value_set_si(f
.x
.n
, 1);
693 if (!options
->lookup_table
) {
694 for (cst
= &t
; value_zero_p(cst
->d
); ) {
695 if (cst
->x
.p
->type
== fractional
)
696 cst
= &cst
->x
.p
->arr
[1];
698 cst
= &cst
->x
.p
->arr
[0];
702 for (int m
= 0; m
< r
->len
; ++m
) {
705 value_set_si(f
.d
, m
);
707 if (!options
->lookup_table
)
708 value_subtract(cst
->x
.n
, cst
->x
.n
, cst
->d
);
714 dpoly_r_term_list
& current
= r
->c
[r
->len
-1-m
];
715 dpoly_r_term_list::iterator j
;
716 for (j
= current
.begin(); j
!= current
.end(); ++j
) {
717 if ((*j
)->coeff
== 0)
719 evalue
*f2
= new evalue
;
722 zz2value((*j
)->coeff
, f2
->x
.n
);
723 zz2value(r
->denom
, f2
->d
);
726 add_term((*j
)->powers
, f2
);
729 free_evalue_refs(&f
);
730 free_evalue_refs(&t
);
731 free_evalue_refs(&cum
);
732 if (options
->lookup_table
)
733 free_evalue_refs(&mone
);
741 struct ie_cum
: public cumulator
{
742 vector
<E_poly_term
*> terms
;
744 ie_cum(evalue
*factor
, evalue
*v
, dpoly_r
*r
) : cumulator(factor
, v
, r
) {}
746 virtual void add_term(const vector
<int>& powers
, evalue
*f2
);
749 void ie_cum::add_term(const vector
<int>& powers
, evalue
*f2
)
752 for (k
= 0; k
< terms
.size(); ++k
) {
753 if (terms
[k
]->powers
== powers
) {
754 eadd(f2
, terms
[k
]->E
);
755 free_evalue_refs(f2
);
760 if (k
>= terms
.size()) {
761 E_poly_term
*ET
= new E_poly_term
;
768 struct ienumerator
: public signed_cone_consumer
, public vertex_decomposer
,
769 public ienumerator_base
{
775 ienumerator(Polyhedron
*P
, unsigned dim
, Param_Polyhedron
*PP
) :
776 vertex_decomposer(PP
, *this), ienumerator_base(dim
, this) {
777 vertex
.SetDims(1, dim
);
779 den
.SetDims(dim
, dim
);
789 virtual void handle(const signed_cone
& sc
, barvinok_options
*options
);
790 void reduce(evalue
*factor
, const mat_ZZ
& num
, const mat_ZZ
& den_f
,
791 barvinok_options
*options
);
794 void ienumerator::reduce(evalue
*factor
, const mat_ZZ
& num
, const mat_ZZ
& den_f
,
795 barvinok_options
*options
)
797 unsigned len
= den_f
.NumRows(); // number of factors in den
798 unsigned dim
= num
.NumCols();
799 assert(num
.NumRows() == 1);
802 eadd(factor
, vE
[vert
]);
811 split_one(num
, num_s
, num_p
, den_f
, den_s
, den_r
);
814 den_p
.SetLength(len
);
818 normalize(one
, num_s
, num_p
, den_s
, den_p
, den_r
);
824 for (int k
= 0; k
< len
; ++k
) {
827 else if (den_s
[k
] == 0)
831 reduce(factor
, num_p
, den_r
, options
);
835 pden
.SetDims(only_param
, dim
-1);
837 for (k
= 0, l
= 0; k
< len
; ++k
)
839 pden
[l
++] = den_r
[k
];
841 for (k
= 0; k
< len
; ++k
)
845 zz2value(num_s
[0], tz
);
846 dpoly
n(no_param
, tz
);
847 zz2value(den_s
[k
], tz
);
848 dpoly
D(no_param
, tz
, 1);
851 zz2value(den_s
[k
], tz
);
852 dpoly
fact(no_param
, tz
, 1);
857 // if no_param + only_param == len then all powers
858 // below will be all zero
859 if (no_param
+ only_param
== len
) {
860 if (E_num(0, dim
) != 0)
861 r
= new dpoly_r(n
, len
);
863 mpq_set_si(tcount
, 0, 1);
867 if (value_notzero_p(mpq_numref(tcount
))) {
871 value_assign(f
.x
.n
, mpq_numref(tcount
));
872 value_assign(f
.d
, mpq_denref(tcount
));
874 reduce(factor
, num_p
, pden
, options
);
875 free_evalue_refs(&f
);
880 for (k
= 0; k
< len
; ++k
) {
881 if (den_s
[k
] == 0 || den_p
[k
] == 0)
884 zz2value(den_s
[k
], tz
);
885 dpoly
pd(no_param
-1, tz
, 1);
888 for (l
= 0; l
< k
; ++l
)
889 if (den_r
[l
] == den_r
[k
])
893 r
= new dpoly_r(n
, pd
, l
, len
);
895 dpoly_r
*nr
= new dpoly_r(r
, pd
, l
, len
);
901 dpoly_r
*rc
= r
->div(D
);
904 if (E_num(0, dim
) == 0) {
905 int common
= pden
.NumRows();
906 dpoly_r_term_list
& final
= r
->c
[r
->len
-1];
912 zz2value(r
->denom
, f
.d
);
913 dpoly_r_term_list::iterator j
;
914 for (j
= final
.begin(); j
!= final
.end(); ++j
) {
915 if ((*j
)->coeff
== 0)
918 for (int k
= 0; k
< r
->dim
; ++k
) {
919 int n
= (*j
)->powers
[k
];
922 pden
.SetDims(rows
+n
, pden
.NumCols());
923 for (int l
= 0; l
< n
; ++l
)
924 pden
[rows
+l
] = den_r
[k
];
928 evalue_copy(&t
, factor
);
929 zz2value((*j
)->coeff
, f
.x
.n
);
931 reduce(&t
, num_p
, pden
, options
);
932 free_evalue_refs(&t
);
934 free_evalue_refs(&f
);
936 ie_cum
cum(factor
, E_num(0, dim
), r
);
937 cum
.cumulate(options
);
939 int common
= pden
.NumRows();
941 for (int j
= 0; j
< cum
.terms
.size(); ++j
) {
943 pden
.SetDims(rows
, pden
.NumCols());
944 for (int k
= 0; k
< r
->dim
; ++k
) {
945 int n
= cum
.terms
[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
];
953 reduce(cum
.terms
[j
]->E
, num_p
, pden
, options
);
954 free_evalue_refs(cum
.terms
[j
]->E
);
955 delete cum
.terms
[j
]->E
;
963 void ienumerator::handle(const signed_cone
& sc
, barvinok_options
*options
)
966 assert(sc
.rays
.NumRows() == dim
);
968 lattice_point(V
, sc
.rays
, vertex
[0], E_vertex
, options
);
974 evalue_set_si(&one
, sc
.sign
, 1);
975 reduce(&one
, vertex
, den
, options
);
976 free_evalue_refs(&one
);
978 for (int i
= 0; i
< dim
; ++i
)
980 evalue_free(E_vertex
[i
]);
983 struct bfenumerator
: public vertex_decomposer
, public bf_base
,
984 public ienumerator_base
{
987 bfenumerator(Polyhedron
*P
, unsigned dim
, Param_Polyhedron
*PP
) :
988 vertex_decomposer(PP
, *this),
989 bf_base(dim
), ienumerator_base(dim
, this) {
997 virtual void handle(const signed_cone
& sc
, barvinok_options
*options
);
998 virtual void base(mat_ZZ
& factors
, bfc_vec
& v
);
1000 bfc_term_base
* new_bf_term(int len
) {
1001 bfe_term
* t
= new bfe_term(len
);
1005 virtual void set_factor(bfc_term_base
*t
, int k
, int change
) {
1006 bfe_term
* bfet
= static_cast<bfe_term
*>(t
);
1007 factor
= bfet
->factors
[k
];
1008 assert(factor
!= NULL
);
1009 bfet
->factors
[k
] = NULL
;
1011 emul(&mone
, factor
);
1014 virtual void set_factor(bfc_term_base
*t
, int k
, mpq_t
&q
, int change
) {
1015 bfe_term
* bfet
= static_cast<bfe_term
*>(t
);
1016 factor
= bfet
->factors
[k
];
1017 assert(factor
!= NULL
);
1018 bfet
->factors
[k
] = NULL
;
1024 value_oppose(f
.x
.n
, mpq_numref(q
));
1026 value_assign(f
.x
.n
, mpq_numref(q
));
1027 value_assign(f
.d
, mpq_denref(q
));
1029 free_evalue_refs(&f
);
1032 virtual void set_factor(bfc_term_base
*t
, int k
, const QQ
& c
, int change
) {
1033 bfe_term
* bfet
= static_cast<bfe_term
*>(t
);
1035 factor
= new evalue
;
1040 zz2value(c
.n
, f
.x
.n
);
1042 value_oppose(f
.x
.n
, f
.x
.n
);
1045 value_init(factor
->d
);
1046 evalue_copy(factor
, bfet
->factors
[k
]);
1048 free_evalue_refs(&f
);
1051 void set_factor(evalue
*f
, int change
) {
1057 virtual void insert_term(bfc_term_base
*t
, int i
) {
1058 bfe_term
* bfet
= static_cast<bfe_term
*>(t
);
1059 int len
= t
->terms
.NumRows()-1; // already increased by one
1061 bfet
->factors
.resize(len
+1);
1062 for (int j
= len
; j
> i
; --j
) {
1063 bfet
->factors
[j
] = bfet
->factors
[j
-1];
1064 t
->terms
[j
] = t
->terms
[j
-1];
1066 bfet
->factors
[i
] = factor
;
1070 virtual void update_term(bfc_term_base
*t
, int i
) {
1071 bfe_term
* bfet
= static_cast<bfe_term
*>(t
);
1073 eadd(factor
, bfet
->factors
[i
]);
1074 free_evalue_refs(factor
);
1078 virtual bool constant_vertex(int dim
) { return E_num(0, dim
) == 0; }
1080 virtual void cum(bf_reducer
*bfr
, bfc_term_base
*t
, int k
, dpoly_r
*r
,
1081 barvinok_options
*options
);
1084 enumerator_base
*enumerator_base::create(Polyhedron
*P
, unsigned dim
,
1085 Param_Polyhedron
*PP
,
1086 barvinok_options
*options
)
1088 enumerator_base
*eb
;
1090 if (options
->incremental_specialization
== BV_SPECIALIZATION_BF
)
1091 eb
= new bfenumerator(P
, dim
, PP
);
1092 else if (options
->incremental_specialization
== BV_SPECIALIZATION_DF
)
1093 eb
= new ienumerator(P
, dim
, PP
);
1095 eb
= new enumerator(P
, dim
, PP
);
1100 struct bfe_cum
: public cumulator
{
1102 bfc_term_base
*told
;
1106 bfe_cum(evalue
*factor
, evalue
*v
, dpoly_r
*r
, bf_reducer
*bfr
,
1107 bfc_term_base
*t
, int k
, bfenumerator
*e
) :
1108 cumulator(factor
, v
, r
), told(t
), k(k
),
1112 virtual void add_term(const vector
<int>& powers
, evalue
*f2
);
1115 void bfe_cum::add_term(const vector
<int>& powers
, evalue
*f2
)
1117 bfr
->update_powers(powers
);
1119 bfc_term_base
* t
= bfe
->find_bfc_term(bfr
->vn
, bfr
->npowers
, bfr
->nnf
);
1120 bfe
->set_factor(f2
, bfr
->l_changes
% 2);
1121 bfe
->add_term(t
, told
->terms
[k
], bfr
->l_extra_num
);
1124 void bfenumerator::cum(bf_reducer
*bfr
, bfc_term_base
*t
, int k
,
1125 dpoly_r
*r
, barvinok_options
*options
)
1127 bfe_term
* bfet
= static_cast<bfe_term
*>(t
);
1128 bfe_cum
cum(bfet
->factors
[k
], E_num(0, bfr
->d
), r
, bfr
, t
, k
, this);
1129 cum
.cumulate(options
);
1132 void bfenumerator::base(mat_ZZ
& factors
, bfc_vec
& v
)
1134 for (int i
= 0; i
< v
.size(); ++i
) {
1135 assert(v
[i
]->terms
.NumRows() == 1);
1136 evalue
*factor
= static_cast<bfe_term
*>(v
[i
])->factors
[0];
1137 eadd(factor
, vE
[vert
]);
1142 void bfenumerator::handle(const signed_cone
& sc
, barvinok_options
*options
)
1144 assert(sc
.det
== 1);
1145 assert(sc
.rays
.NumRows() == enumerator_base::dim
);
1147 bfe_term
* t
= new bfe_term(enumerator_base::dim
);
1148 vector
< bfc_term_base
* > v
;
1151 t
->factors
.resize(1);
1153 t
->terms
.SetDims(1, enumerator_base::dim
);
1154 lattice_point(V
, sc
.rays
, t
->terms
[0], E_vertex
, options
);
1156 // the elements of factors are always lexpositive
1158 int s
= setup_factors(sc
.rays
, factors
, t
, sc
.sign
);
1160 t
->factors
[0] = new evalue
;
1161 value_init(t
->factors
[0]->d
);
1162 evalue_set_si(t
->factors
[0], s
, 1);
1163 reduce(factors
, v
, options
);
1165 for (int i
= 0; i
< enumerator_base::dim
; ++i
)
1167 evalue_free(E_vertex
[i
]);
1170 static evalue
* barvinok_enumerate_ev_f(Polyhedron
*P
, Polyhedron
* C
,
1171 barvinok_options
*options
);
1174 static evalue
* barvinok_enumerate_cst(Polyhedron
*P
, Polyhedron
* C
,
1175 struct barvinok_options
*options
)
1181 return evalue_zero();
1184 ALLOC(evalue
, eres
);
1185 value_init(eres
->d
);
1186 value_set_si(eres
->d
, 0);
1187 eres
->x
.p
= new_enode(partition
, 2, C
->Dimension
);
1188 EVALUE_SET_DOMAIN(eres
->x
.p
->arr
[0],
1189 DomainConstraintSimplify(C
, options
->MaxRays
));
1190 value_set_si(eres
->x
.p
->arr
[1].d
, 1);
1191 value_init(eres
->x
.p
->arr
[1].x
.n
);
1193 value_set_si(eres
->x
.p
->arr
[1].x
.n
, 0);
1195 barvinok_count_with_options(P
, &eres
->x
.p
->arr
[1].x
.n
, options
);
1196 if (value_mone_p(eres
->x
.p
->arr
[1].x
.n
)) {
1197 value_clear(eres
->x
.p
->arr
[1].x
.n
);
1198 value_set_si(eres
->x
.p
->arr
[1].d
, -2); /* NaN */
1204 static evalue
* enumerate(Polyhedron
*P
, Polyhedron
* C
,
1205 struct barvinok_options
*options
)
1208 Polyhedron
*Porig
= P
;
1209 Polyhedron
*Corig
= C
;
1210 Polyhedron
*CEq
= NULL
;
1211 unsigned nparam
= C
->Dimension
;
1216 value_init(factor
.d
);
1217 evalue_set_si(&factor
, 1, 1);
1220 POL_ENSURE_FACETS(P
);
1221 POL_ENSURE_VERTICES(P
);
1222 POL_ENSURE_FACETS(C
);
1223 POL_ENSURE_VERTICES(C
);
1225 if (C
->Dimension
== 0 || emptyQ(P
) || emptyQ(C
)) {
1228 CEq
= Polyhedron_Copy(CEq
);
1229 eres
= barvinok_enumerate_cst(P
, CEq
? CEq
: Polyhedron_Copy(C
), options
);
1232 evalue_backsubstitute(eres
, CP
, options
->MaxRays
);
1236 emul(&factor
, eres
);
1237 if (options
->approx
->method
== BV_APPROX_DROP
) {
1238 if (options
->approx
->approximation
== BV_APPROX_SIGN_UPPER
)
1239 evalue_frac2polynomial(eres
, 1, options
->MaxRays
);
1240 if (options
->approx
->approximation
== BV_APPROX_SIGN_LOWER
)
1241 evalue_frac2polynomial(eres
, -1, options
->MaxRays
);
1242 if (options
->approx
->approximation
== BV_APPROX_SIGN_APPROX
)
1243 evalue_frac2polynomial(eres
, 0, options
->MaxRays
);
1245 reduce_evalue(eres
);
1246 free_evalue_refs(&factor
);
1254 if (Polyhedron_is_unbounded(P
, nparam
, options
->MaxRays
))
1257 if (P
->Dimension
== nparam
) {
1258 CEq
= DomainIntersection(P
, C
, options
->MaxRays
);
1259 P
= Universe_Polyhedron(0);
1262 if (P
->NbEq
!= 0 || C
->NbEq
!= 0) {
1265 remove_all_equalities(&P
, &C
, &CP
, NULL
, nparam
, options
->MaxRays
);
1266 if (C
!= D
&& D
!= Corig
)
1268 if (P
!= Q
&& Q
!= Porig
)
1270 eres
= enumerate(P
, C
, options
);
1274 Polyhedron
*T
= Polyhedron_Factor(P
, nparam
, NULL
, options
->MaxRays
);
1275 if (T
|| (P
->Dimension
== nparam
+1)) {
1277 Polyhedron
*FC
= Factor_Context(T
? T
: P
, nparam
, options
->MaxRays
);
1278 C
= DomainIntersection(C
, FC
, options
->MaxRays
);
1280 Polyhedron_Free(C2
);
1281 Polyhedron_Free(FC
);
1287 if (T
->Dimension
== C
->Dimension
) {
1296 eres
= barvinok_enumerate_ev_f(P
, C
, options
);
1303 for (Q
= P
->next
; Q
; Q
= Q
->next
) {
1304 Polyhedron
*next
= Q
->next
;
1307 f
= barvinok_enumerate_ev_f(Q
, C
, options
);
1318 evalue
* barvinok_enumerate_with_options(Polyhedron
*P
, Polyhedron
* C
,
1319 struct barvinok_options
*options
)
1321 Polyhedron
*next
, *Cnext
, *C1
;
1322 Polyhedron
*Corig
= C
;
1327 "barvinok_enumerate: input is a union; only first polyhedron is enumerated\n");
1331 "barvinok_enumerate: context is a union; only first polyhedron is considered\n");
1335 C1
= Polyhedron_Project(P
, C
->Dimension
);
1336 C
= DomainIntersection(C
, C1
, options
->MaxRays
);
1337 Polyhedron_Free(C1
);
1341 if (options
->approx
->method
== BV_APPROX_BERNOULLI
||
1342 options
->summation
== BV_SUM_BERNOULLI
) {
1343 int summation
= options
->summation
;
1344 options
->summation
= BV_SUM_BERNOULLI
;
1345 eres
= barvinok_summate_unweighted(P
, C
, options
);
1346 options
->summation
= summation
;
1348 eres
= enumerate(P
, C
, options
);
1352 Corig
->next
= Cnext
;
1357 evalue
* barvinok_enumerate_ev(Polyhedron
*P
, Polyhedron
* C
, unsigned MaxRays
)
1360 barvinok_options
*options
= barvinok_options_new_with_defaults();
1361 options
->MaxRays
= MaxRays
;
1362 E
= barvinok_enumerate_with_options(P
, C
, options
);
1363 barvinok_options_free(options
);
1367 evalue
*Param_Polyhedron_Enumerate(Param_Polyhedron
*PP
, Polyhedron
*P
,
1369 struct barvinok_options
*options
)
1373 unsigned nparam
= C
->Dimension
;
1374 unsigned dim
= P
->Dimension
- nparam
;
1377 for (nd
= 0, D
=PP
->D
; D
; ++nd
, D
=D
->next
);
1378 evalue_section
*s
= new evalue_section
[nd
];
1379 Polyhedron
*TC
= true_context(P
, C
, options
->MaxRays
);
1381 enumerator_base
*et
= NULL
;
1386 et
= enumerator_base::create(P
, dim
, PP
, options
);
1388 FORALL_REDUCED_DOMAIN(PP
, TC
, nd
, options
, i
, D
, rVD
)
1391 s
[i
].E
= evalue_zero();
1394 FORALL_PVertex_in_ParamPolyhedron(V
,D
,PP
) // _i is internal counter
1397 et
->decompose_at(V
, _i
, options
);
1398 } catch (OrthogonalException
&e
) {
1399 FORALL_REDUCED_DOMAIN_RESET
;
1400 for (; i
>= 0; --i
) {
1401 evalue_free(s
[i
].E
);
1402 Domain_Free(s
[i
].D
);
1406 eadd(et
->vE
[_i
] , s
[i
].E
);
1407 END_FORALL_PVertex_in_ParamPolyhedron
;
1408 evalue_range_reduction_in_domain(s
[i
].E
, rVD
);
1409 END_FORALL_REDUCED_DOMAIN
1410 Polyhedron_Free(TC
);
1413 eres
= evalue_from_section_array(s
, nd
);
1419 static evalue
* barvinok_enumerate_ev_f(Polyhedron
*P
, Polyhedron
* C
,
1420 barvinok_options
*options
)
1422 unsigned nparam
= C
->Dimension
;
1423 bool do_scale
= options
->approx
->method
== BV_APPROX_SCALE
;
1425 if (options
->summation
== BV_SUM_EULER
)
1426 return barvinok_summate_unweighted(P
, C
, options
);
1428 if (options
->approx
->method
== BV_APPROX_VOLUME
)
1429 return Param_Polyhedron_Volume(P
, C
, options
);
1431 if (P
->Dimension
- nparam
== 1 && !do_scale
)
1432 return ParamLine_Length(P
, C
, options
);
1434 Param_Polyhedron
*PP
= NULL
;
1438 eres
= scale_bound(P
, C
, options
);
1443 PP
= Polyhedron2Param_Polyhedron(P
, C
, options
);
1446 eres
= scale(PP
, P
, C
, options
);
1448 eres
= Param_Polyhedron_Enumerate(PP
, P
, C
, options
);
1451 Param_Polyhedron_Free(PP
);
1456 Enumeration
* barvinok_enumerate(Polyhedron
*P
, Polyhedron
* C
, unsigned MaxRays
)
1458 evalue
*EP
= barvinok_enumerate_ev(P
, C
, MaxRays
);
1460 return partition2enumeration(EP
);
1463 evalue
* barvinok_enumerate_union(Polyhedron
*D
, Polyhedron
* C
, unsigned MaxRays
)
1466 gen_fun
*gf
= barvinok_enumerate_union_series(D
, C
, MaxRays
);
1472 static __isl_give isl_pw_qpolynomial
*basic_set_card(
1473 __isl_take isl_basic_set
*bset
)
1477 isl_pw_qpolynomial
*pwqp
;
1478 unsigned nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
1479 Polyhedron
*U
= Universe_Polyhedron(nparam
);
1482 barvinok_options
*options
;
1483 int options_allocated
= 0;
1485 ctx
= isl_basic_set_get_ctx(bset
);
1486 options
= isl_ctx_peek_barvinok_options(ctx
);
1488 options
= barvinok_options_new_with_defaults();
1489 options_allocated
= 1;
1492 dim
= isl_basic_set_get_space(bset
);
1493 dim
= isl_space_domain(dim
);
1495 P
= isl_basic_set_to_polylib(bset
);
1496 E
= enumerate(P
, U
, options
);
1498 pwqp
= isl_pw_qpolynomial_from_evalue(dim
, E
);
1499 isl_basic_set_free(bset
);
1504 if (options_allocated
)
1505 barvinok_options_free(options
);
1510 static isl_stat
basic_map_card(__isl_take isl_basic_map
*bmap
, void *user
)
1512 isl_pw_qpolynomial
**sum
= (isl_pw_qpolynomial
**)user
;
1513 isl_pw_qpolynomial
*pwqp
;
1514 unsigned nparam
= isl_basic_map_dim(bmap
, isl_dim_param
);
1515 unsigned n_in
= isl_basic_map_dim(bmap
, isl_dim_in
);
1516 isl_space
*target_dim
;
1517 isl_basic_set
*bset
;
1519 target_dim
= isl_basic_map_get_space(bmap
);
1520 target_dim
= isl_space_domain(target_dim
);
1522 bmap
= isl_basic_map_move_dims(bmap
, isl_dim_param
, nparam
,
1523 isl_dim_in
, 0, n_in
);
1525 bset
= isl_basic_map_range(bmap
);
1526 bset
= isl_basic_set_lift(bset
);
1527 pwqp
= isl_basic_set_multiplicative_call(bset
, &basic_set_card
);
1529 pwqp
= isl_pw_qpolynomial_move_dims(pwqp
, isl_dim_in
, 0,
1530 isl_dim_param
, nparam
, n_in
);
1531 pwqp
= isl_pw_qpolynomial_reset_domain_space(pwqp
, target_dim
);
1532 *sum
= isl_pw_qpolynomial_add(*sum
, pwqp
);
1537 static __isl_give isl_pw_qpolynomial
*card_as_sum(__isl_take isl_map
*map
,
1538 barvinok_options
*options
)
1544 isl_qpolynomial
*qp
;
1545 isl_pw_qpolynomial
*pwqp
;
1546 int summation
= options
->summation
;
1551 options
->summation
= BV_SUM_BERNOULLI
;
1553 set
= isl_map_wrap(map
);
1554 dim
= isl_set_get_space(set
);
1555 ctx
= isl_map_get_ctx(map
);
1556 one
= isl_val_one(ctx
);
1557 qp
= isl_qpolynomial_val_on_domain(dim
, one
);
1559 pwqp
= isl_pw_qpolynomial_alloc(set
, qp
);
1560 pwqp
= isl_pw_qpolynomial_sum(pwqp
);
1562 options
->summation
= summation
;
1567 __isl_give isl_pw_qpolynomial
*isl_map_card(__isl_take isl_map
*map
)
1571 isl_pw_qpolynomial
*sum
;
1572 barvinok_options
*options
;
1574 ctx
= isl_map_get_ctx(map
);
1575 options
= isl_ctx_peek_barvinok_options(ctx
);
1577 (options
->approx
->method
== BV_APPROX_BERNOULLI
||
1578 options
->summation
== BV_SUM_BERNOULLI
))
1579 return card_as_sum(map
, options
);
1581 dim
= isl_map_get_space(map
);
1582 dim
= isl_space_domain(dim
);
1583 dim
= isl_space_from_domain(dim
);
1584 dim
= isl_space_add_dims(dim
, isl_dim_out
, 1);
1585 sum
= isl_pw_qpolynomial_zero(dim
);
1587 map
= isl_map_make_disjoint(map
);
1588 map
= isl_map_compute_divs(map
);
1590 if (isl_map_foreach_basic_map(map
, &basic_map_card
, &sum
) < 0)
1598 isl_pw_qpolynomial_free(sum
);
1602 __isl_give isl_pw_qpolynomial
*isl_set_card(__isl_take isl_set
*set
)
1604 isl_pw_qpolynomial
*pwqp
;
1605 pwqp
= isl_map_card(isl_map_from_range(set
));
1606 pwqp
= isl_pw_qpolynomial_project_domain_on_params(pwqp
);
1610 __isl_give isl_pw_qpolynomial
*isl_basic_map_card(__isl_take isl_basic_map
*bmap
)
1612 return isl_map_card(isl_map_from_basic_map(bmap
));
1615 __isl_give isl_pw_qpolynomial
*isl_basic_set_card(__isl_take isl_basic_set
*bset
)
1617 isl_pw_qpolynomial
*pwqp
;
1618 pwqp
= isl_basic_map_card(isl_basic_map_from_range(bset
));
1619 pwqp
= isl_pw_qpolynomial_project_domain_on_params(pwqp
);
1623 static isl_stat
set_card(__isl_take isl_set
*set
, void *user
)
1625 isl_union_pw_qpolynomial
**res
= (isl_union_pw_qpolynomial
**)user
;
1626 isl_pw_qpolynomial
*pwqp
;
1627 isl_union_pw_qpolynomial
*upwqp
;
1629 pwqp
= isl_set_card(set
);
1630 upwqp
= isl_union_pw_qpolynomial_from_pw_qpolynomial(pwqp
);
1631 *res
= isl_union_pw_qpolynomial_add(*res
, upwqp
);
1636 __isl_give isl_union_pw_qpolynomial
*isl_union_set_card(
1637 __isl_take isl_union_set
*uset
)
1640 isl_union_pw_qpolynomial
*res
;
1642 dim
= isl_union_set_get_space(uset
);
1643 res
= isl_union_pw_qpolynomial_zero(dim
);
1644 if (isl_union_set_foreach_set(uset
, &set_card
, &res
) < 0)
1646 isl_union_set_free(uset
);
1650 isl_union_set_free(uset
);
1651 isl_union_pw_qpolynomial_free(res
);
1655 static isl_stat
map_card(__isl_take isl_map
*map
, void *user
)
1657 isl_union_pw_qpolynomial
**res
= (isl_union_pw_qpolynomial
**)user
;
1658 isl_pw_qpolynomial
*pwqp
;
1659 isl_union_pw_qpolynomial
*upwqp
;
1661 pwqp
= isl_map_card(map
);
1662 upwqp
= isl_union_pw_qpolynomial_from_pw_qpolynomial(pwqp
);
1663 *res
= isl_union_pw_qpolynomial_add(*res
, upwqp
);
1668 __isl_give isl_union_pw_qpolynomial
*isl_union_map_card(
1669 __isl_take isl_union_map
*umap
)
1672 isl_union_pw_qpolynomial
*res
;
1674 dim
= isl_union_map_get_space(umap
);
1675 res
= isl_union_pw_qpolynomial_zero(dim
);
1676 if (isl_union_map_foreach_map(umap
, &map_card
, &res
) < 0)
1678 isl_union_map_free(umap
);
1682 isl_union_map_free(umap
);
1683 isl_union_pw_qpolynomial_free(res
);