8 #include <NTL/mat_ZZ.h>
10 #include <barvinok/util.h>
11 #include <barvinok/evalue.h>
16 #include <barvinok/barvinok.h>
17 #include <barvinok/genfun.h>
18 #include <barvinok/options.h>
19 #include <barvinok/sample.h>
20 #include "conversion.h"
21 #include "decomposer.h"
22 #include "lattice_point.h"
23 #include "reduce_domain.h"
24 #include "genfun_constructor.h"
25 #include "remove_equalities.h"
36 using std::ostringstream
;
38 #define ALLOC(t,p) p = (t*)malloc(sizeof(*p))
46 dpoly_n(int d
, ZZ
& degree_0
, ZZ
& degree_1
, int offset
= 0) {
50 zz2value(degree_0
, d0
);
51 zz2value(degree_1
, d1
);
52 coeff
= Matrix_Alloc(d
+1, d
+1+1);
53 value_set_si(coeff
->p
[0][0], 1);
54 value_set_si(coeff
->p
[0][d
+1], 1);
55 for (int i
= 1; i
<= d
; ++i
) {
56 value_multiply(coeff
->p
[i
][0], coeff
->p
[i
-1][0], d0
);
57 Vector_Combine(coeff
->p
[i
-1], coeff
->p
[i
-1]+1, coeff
->p
[i
]+1,
59 value_set_si(coeff
->p
[i
][d
+1], i
);
60 value_multiply(coeff
->p
[i
][d
+1], coeff
->p
[i
][d
+1], coeff
->p
[i
-1][d
+1]);
61 value_decrement(d0
, d0
);
66 void div(dpoly
& d
, Vector
*count
, ZZ
& sign
) {
67 int len
= coeff
->NbRows
;
68 Matrix
* c
= Matrix_Alloc(coeff
->NbRows
, coeff
->NbColumns
);
71 for (int i
= 0; i
< len
; ++i
) {
72 Vector_Copy(coeff
->p
[i
], c
->p
[i
], len
+1);
73 for (int j
= 1; j
<= i
; ++j
) {
74 zz2value(d
.coeff
[j
], tmp
);
75 value_multiply(tmp
, tmp
, c
->p
[i
][len
]);
76 value_oppose(tmp
, tmp
);
77 Vector_Combine(c
->p
[i
], c
->p
[i
-j
], c
->p
[i
],
78 c
->p
[i
-j
][len
], tmp
, len
);
79 value_multiply(c
->p
[i
][len
], c
->p
[i
][len
], c
->p
[i
-j
][len
]);
81 zz2value(d
.coeff
[0], tmp
);
82 value_multiply(c
->p
[i
][len
], c
->p
[i
][len
], tmp
);
85 value_set_si(tmp
, -1);
86 Vector_Scale(c
->p
[len
-1], count
->p
, tmp
, len
);
87 value_assign(count
->p
[len
], c
->p
[len
-1][len
]);
89 Vector_Copy(c
->p
[len
-1], count
->p
, len
+1);
90 Vector_Normalize(count
->p
, len
+1);
98 * Searches for a vector that is not orthogonal to any
99 * of the rays in rays.
101 static void nonorthog(mat_ZZ
& rays
, vec_ZZ
& lambda
)
103 int dim
= rays
.NumCols();
105 lambda
.SetLength(dim
);
109 for (int i
= 2; !found
&& i
<= 50*dim
; i
+=4) {
110 for (int j
= 0; j
< MAX_TRY
; ++j
) {
111 for (int k
= 0; k
< dim
; ++k
) {
112 int r
= random_int(i
)+2;
113 int v
= (2*(r
%2)-1) * (r
>> 1);
117 for (; k
< rays
.NumRows(); ++k
)
118 if (lambda
* rays
[k
] == 0)
120 if (k
== rays
.NumRows()) {
129 static void add_rays(mat_ZZ
& rays
, Polyhedron
*i
, int *r
, int nvar
= -1,
132 unsigned dim
= i
->Dimension
;
135 for (int k
= 0; k
< i
->NbRays
; ++k
) {
136 if (!value_zero_p(i
->Ray
[k
][dim
+1]))
138 if (!all
&& nvar
!= dim
&& First_Non_Zero(i
->Ray
[k
]+1, nvar
) == -1)
140 values2zz(i
->Ray
[k
]+1, rays
[(*r
)++], nvar
);
144 static void mask_r(Matrix
*f
, int nr
, Vector
*lcm
, int p
, Vector
*val
, evalue
*ev
)
146 unsigned nparam
= lcm
->Size
;
149 Vector
* prod
= Vector_Alloc(f
->NbRows
);
150 Matrix_Vector_Product(f
, val
->p
, prod
->p
);
152 for (int i
= 0; i
< nr
; ++i
) {
153 value_modulus(prod
->p
[i
], prod
->p
[i
], f
->p
[i
][nparam
+1]);
154 isint
&= value_zero_p(prod
->p
[i
]);
156 value_set_si(ev
->d
, 1);
158 value_set_si(ev
->x
.n
, isint
);
165 if (value_one_p(lcm
->p
[p
]))
166 mask_r(f
, nr
, lcm
, p
+1, val
, ev
);
168 value_assign(tmp
, lcm
->p
[p
]);
169 value_set_si(ev
->d
, 0);
170 ev
->x
.p
= new_enode(periodic
, VALUE_TO_INT(tmp
), p
+1);
172 value_decrement(tmp
, tmp
);
173 value_assign(val
->p
[p
], tmp
);
174 mask_r(f
, nr
, lcm
, p
+1, val
, &ev
->x
.p
->arr
[VALUE_TO_INT(tmp
)]);
175 } while (value_pos_p(tmp
));
180 static void mask_fractional(Matrix
*f
, evalue
*factor
)
182 int nr
= f
->NbRows
, nc
= f
->NbColumns
;
185 for (n
= 0; n
< nr
&& value_notzero_p(f
->p
[n
][nc
-1]); ++n
)
186 if (value_notone_p(f
->p
[n
][nc
-1]) &&
187 value_notmone_p(f
->p
[n
][nc
-1]))
201 value_set_si(EV
.x
.n
, 1);
203 for (n
= 0; n
< nr
; ++n
) {
204 value_assign(m
, f
->p
[n
][nc
-1]);
205 if (value_one_p(m
) || value_mone_p(m
))
208 int j
= normal_mod(f
->p
[n
], nc
-1, &m
);
210 free_evalue_refs(factor
);
211 value_init(factor
->d
);
212 evalue_set_si(factor
, 0, 1);
216 values2zz(f
->p
[n
], row
, nc
-1);
219 if (j
< (nc
-1)-1 && row
[j
] > g
/2) {
220 for (int k
= j
; k
< (nc
-1); ++k
)
226 value_set_si(EP
.d
, 0);
227 EP
.x
.p
= new_enode(relation
, 2, 0);
228 value_clear(EP
.x
.p
->arr
[1].d
);
229 EP
.x
.p
->arr
[1] = *factor
;
230 evalue
*ev
= &EP
.x
.p
->arr
[0];
231 value_set_si(ev
->d
, 0);
232 ev
->x
.p
= new_enode(fractional
, 3, -1);
233 evalue_set_si(&ev
->x
.p
->arr
[1], 0, 1);
234 evalue_set_si(&ev
->x
.p
->arr
[2], 1, 1);
235 evalue
*E
= multi_monom(row
);
236 value_assign(EV
.d
, m
);
238 value_clear(ev
->x
.p
->arr
[0].d
);
239 ev
->x
.p
->arr
[0] = *E
;
245 free_evalue_refs(&EV
);
251 static void mask_table(Matrix
*f
, evalue
*factor
)
253 int nr
= f
->NbRows
, nc
= f
->NbColumns
;
256 for (n
= 0; n
< nr
&& value_notzero_p(f
->p
[n
][nc
-1]); ++n
)
257 if (value_notone_p(f
->p
[n
][nc
-1]) &&
258 value_notmone_p(f
->p
[n
][nc
-1]))
266 unsigned np
= nc
- 2;
267 Vector
*lcm
= Vector_Alloc(np
);
268 Vector
*val
= Vector_Alloc(nc
);
269 Vector_Set(val
->p
, 0, nc
);
270 value_set_si(val
->p
[np
], 1);
271 Vector_Set(lcm
->p
, 1, np
);
272 for (n
= 0; n
< nr
; ++n
) {
273 if (value_one_p(f
->p
[n
][nc
-1]) ||
274 value_mone_p(f
->p
[n
][nc
-1]))
276 for (int j
= 0; j
< np
; ++j
)
277 if (value_notzero_p(f
->p
[n
][j
])) {
278 Gcd(f
->p
[n
][j
], f
->p
[n
][nc
-1], &tmp
);
279 value_division(tmp
, f
->p
[n
][nc
-1], tmp
);
280 value_lcm(tmp
, lcm
->p
[j
], &lcm
->p
[j
]);
285 mask_r(f
, nr
, lcm
, 0, val
, &EP
);
290 free_evalue_refs(&EP
);
293 static void mask(Matrix
*f
, evalue
*factor
, barvinok_options
*options
)
295 if (options
->lookup_table
)
296 mask_table(f
, factor
);
298 mask_fractional(f
, factor
);
301 /* This structure encodes the power of the term in a rational generating function.
303 * Either E == NULL or constant = 0
304 * If E != NULL, then the power is E
305 * If E == NULL, then the power is coeff * param[pos] + constant
314 /* Returns the power of (t+1) in the term of a rational generating function,
315 * i.e., the scalar product of the actual lattice point and lambda.
316 * The lattice point is the unique lattice point in the fundamental parallelepiped
317 * of the unimodual cone i shifted to the parametric vertex V.
319 * PD is the parameter domain, which, if != NULL, may be used to simply the
320 * resulting expression.
322 * The result is returned in term.
324 void lattice_point(Param_Vertices
* V
, const mat_ZZ
& rays
, vec_ZZ
& lambda
,
325 term_info
* term
, Polyhedron
*PD
, barvinok_options
*options
)
327 unsigned nparam
= V
->Vertex
->NbColumns
- 2;
328 unsigned dim
= rays
.NumCols();
330 vertex
.SetDims(V
->Vertex
->NbRows
, nparam
+1);
334 value_set_si(lcm
, 1);
335 for (int j
= 0; j
< V
->Vertex
->NbRows
; ++j
) {
336 value_lcm(lcm
, V
->Vertex
->p
[j
][nparam
+1], &lcm
);
338 if (value_notone_p(lcm
)) {
339 Matrix
* mv
= Matrix_Alloc(dim
, nparam
+1);
340 for (int j
= 0 ; j
< dim
; ++j
) {
341 value_division(tmp
, lcm
, V
->Vertex
->p
[j
][nparam
+1]);
342 Vector_Scale(V
->Vertex
->p
[j
], mv
->p
[j
], tmp
, nparam
+1);
345 term
->E
= lattice_point(rays
, lambda
, mv
, lcm
, PD
, options
);
353 for (int i
= 0; i
< V
->Vertex
->NbRows
; ++i
) {
354 assert(value_one_p(V
->Vertex
->p
[i
][nparam
+1])); // for now
355 values2zz(V
->Vertex
->p
[i
], vertex
[i
], nparam
+1);
359 num
= lambda
* vertex
;
363 for (int j
= 0; j
< nparam
; ++j
)
369 term
->E
= multi_monom(num
);
373 term
->constant
= num
[nparam
];
376 term
->coeff
= num
[p
];
384 struct counter
: public np_base
{
394 counter(unsigned dim
) : np_base(dim
) {
399 virtual void init(Polyhedron
*P
) {
400 randomvector(P
, lambda
, dim
);
403 virtual void reset() {
404 mpq_set_si(count
, 0, 0);
411 virtual void handle(const mat_ZZ
& rays
, Value
*vertex
, const QQ
& c
,
412 unsigned long det
, int *closed
, barvinok_options
*options
);
413 virtual void get_count(Value
*result
) {
414 assert(value_one_p(&count
[0]._mp_den
));
415 value_assign(*result
, &count
[0]._mp_num
);
419 void counter::handle(const mat_ZZ
& rays
, Value
*V
, const QQ
& c
, unsigned long det
,
420 int *closed
, barvinok_options
*options
)
422 for (int k
= 0; k
< dim
; ++k
) {
423 if (lambda
* rays
[k
] == 0)
428 assert(c
.n
== 1 || c
.n
== -1);
431 lattice_point(V
, rays
, vertex
, det
, closed
);
432 num
= vertex
* lambda
;
435 normalize(sign
, offset
, den
);
438 dpoly
d(dim
, num
[0]);
439 for (int k
= 1; k
< num
.length(); ++k
) {
441 dpoly
term(dim
, num
[k
]);
444 dpoly
n(dim
, den
[0], 1);
445 for (int k
= 1; k
< dim
; ++k
) {
446 dpoly
fact(dim
, den
[k
], 1);
449 d
.div(n
, count
, sign
);
452 struct bfe_term
: public bfc_term_base
{
453 vector
<evalue
*> factors
;
455 bfe_term(int len
) : bfc_term_base(len
) {
459 for (int i
= 0; i
< factors
.size(); ++i
) {
462 free_evalue_refs(factors
[i
]);
468 static void print_int_vector(int *v
, int len
, char *name
)
470 cerr
<< name
<< endl
;
471 for (int j
= 0; j
< len
; ++j
) {
477 static void print_bfc_terms(mat_ZZ
& factors
, bfc_vec
& v
)
480 cerr
<< "factors" << endl
;
481 cerr
<< factors
<< endl
;
482 for (int i
= 0; i
< v
.size(); ++i
) {
483 cerr
<< "term: " << i
<< endl
;
484 print_int_vector(v
[i
]->powers
, factors
.NumRows(), "powers");
485 cerr
<< "terms" << endl
;
486 cerr
<< v
[i
]->terms
<< endl
;
487 bfc_term
* bfct
= static_cast<bfc_term
*>(v
[i
]);
488 cerr
<< bfct
->c
<< endl
;
492 static void print_bfe_terms(mat_ZZ
& factors
, bfc_vec
& v
)
495 cerr
<< "factors" << endl
;
496 cerr
<< factors
<< endl
;
497 for (int i
= 0; i
< v
.size(); ++i
) {
498 cerr
<< "term: " << i
<< endl
;
499 print_int_vector(v
[i
]->powers
, factors
.NumRows(), "powers");
500 cerr
<< "terms" << endl
;
501 cerr
<< v
[i
]->terms
<< endl
;
502 bfe_term
* bfet
= static_cast<bfe_term
*>(v
[i
]);
503 for (int j
= 0; j
< v
[i
]->terms
.NumRows(); ++j
) {
504 char * test
[] = {"a", "b"};
505 print_evalue(stderr
, bfet
->factors
[j
], test
);
506 fprintf(stderr
, "\n");
511 struct bfcounter
: public bfcounter_base
{
514 bfcounter(unsigned dim
) : bfcounter_base(dim
) {
521 virtual void base(mat_ZZ
& factors
, bfc_vec
& v
);
522 virtual void get_count(Value
*result
) {
523 assert(value_one_p(&count
[0]._mp_den
));
524 value_assign(*result
, &count
[0]._mp_num
);
528 void bfcounter::base(mat_ZZ
& factors
, bfc_vec
& v
)
530 unsigned nf
= factors
.NumRows();
532 for (int i
= 0; i
< v
.size(); ++i
) {
533 bfc_term
* bfct
= static_cast<bfc_term
*>(v
[i
]);
535 // factor is always positive, so we always
537 for (int k
= 0; k
< nf
; ++k
)
538 total_power
+= v
[i
]->powers
[k
];
541 for (j
= 0; j
< nf
; ++j
)
542 if (v
[i
]->powers
[j
] > 0)
545 dpoly
D(total_power
, factors
[j
][0], 1);
546 for (int k
= 1; k
< v
[i
]->powers
[j
]; ++k
) {
547 dpoly
fact(total_power
, factors
[j
][0], 1);
551 for (int k
= 0; k
< v
[i
]->powers
[j
]; ++k
) {
552 dpoly
fact(total_power
, factors
[j
][0], 1);
556 for (int k
= 0; k
< v
[i
]->terms
.NumRows(); ++k
) {
557 dpoly
n(total_power
, v
[i
]->terms
[k
][0]);
558 mpq_set_si(tcount
, 0, 1);
559 n
.div(D
, tcount
, one
);
561 bfct
->c
[k
].n
= -bfct
->c
[k
].n
;
562 zz2value(bfct
->c
[k
].n
, tn
);
563 zz2value(bfct
->c
[k
].d
, td
);
565 mpz_mul(mpq_numref(tcount
), mpq_numref(tcount
), tn
);
566 mpz_mul(mpq_denref(tcount
), mpq_denref(tcount
), td
);
567 mpq_canonicalize(tcount
);
568 mpq_add(count
, count
, tcount
);
575 /* Check whether the polyhedron is unbounded and if so,
576 * check whether it has any (and therefore an infinite number of)
578 * If one of the vertices is integer, then we are done.
579 * Otherwise, transform the polyhedron such that one of the rays
580 * is the first unit vector and cut it off at a height that ensures
581 * that if the whole polyhedron has any points, then the remaining part
582 * has integer points. In particular we add the largest coefficient
583 * of a ray to the highest vertex (rounded up).
585 static bool Polyhedron_is_infinite(Polyhedron
*P
, Value
* result
,
586 barvinok_options
*options
)
598 for (; r
< P
->NbRays
; ++r
)
599 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1]))
601 if (P
->NbBid
== 0 && r
== P
->NbRays
)
604 if (options
->count_sample_infinite
) {
607 sample
= Polyhedron_Sample(P
, options
);
609 value_set_si(*result
, 0);
611 value_set_si(*result
, -1);
617 for (int i
= 0; i
< P
->NbRays
; ++i
)
618 if (value_one_p(P
->Ray
[i
][1+P
->Dimension
])) {
619 value_set_si(*result
, -1);
624 v
= Vector_Alloc(P
->Dimension
+1);
625 Vector_Gcd(P
->Ray
[r
]+1, P
->Dimension
, &g
);
626 Vector_AntiScale(P
->Ray
[r
]+1, v
->p
, g
, P
->Dimension
+1);
627 M
= unimodular_complete(v
);
628 value_set_si(M
->p
[P
->Dimension
][P
->Dimension
], 1);
631 P
= Polyhedron_Preimage(P
, M2
, 0);
640 value_set_si(size
, 0);
642 for (int i
= 0; i
< P
->NbBid
; ++i
) {
643 value_absolute(tmp
, P
->Ray
[i
][1]);
644 if (value_gt(tmp
, size
))
645 value_assign(size
, tmp
);
647 for (int i
= P
->NbBid
; i
< P
->NbRays
; ++i
) {
648 if (value_zero_p(P
->Ray
[i
][P
->Dimension
+1])) {
649 if (value_gt(P
->Ray
[i
][1], size
))
650 value_assign(size
, P
->Ray
[i
][1]);
653 mpz_cdiv_q(tmp
, P
->Ray
[i
][1], P
->Ray
[i
][P
->Dimension
+1]);
654 if (first
|| value_gt(tmp
, offset
)) {
655 value_assign(offset
, tmp
);
659 value_addto(offset
, offset
, size
);
663 v
= Vector_Alloc(P
->Dimension
+2);
664 value_set_si(v
->p
[0], 1);
665 value_set_si(v
->p
[1], -1);
666 value_assign(v
->p
[1+P
->Dimension
], offset
);
667 R
= AddConstraints(v
->p
, 1, P
, options
->MaxRays
);
675 barvinok_count_with_options(P
, &c
, options
);
678 value_set_si(*result
, 0);
680 value_set_si(*result
, -1);
686 typedef Polyhedron
* Polyhedron_p
;
688 static void barvinok_count_f(Polyhedron
*P
, Value
* result
,
689 barvinok_options
*options
);
691 void barvinok_count_with_options(Polyhedron
*P
, Value
* result
,
692 struct barvinok_options
*options
)
697 bool infinite
= false;
700 value_set_si(*result
, 0);
706 P
= remove_equalities(P
);
707 P
= DomainConstraintSimplify(P
, options
->MaxRays
);
711 } while (!emptyQ(P
) && P
->NbEq
!= 0);
714 value_set_si(*result
, 0);
719 if (Polyhedron_is_infinite(P
, result
, options
)) {
724 if (P
->Dimension
== 0) {
725 /* Test whether the constraints are satisfied */
726 POL_ENSURE_VERTICES(P
);
727 value_set_si(*result
, !emptyQ(P
));
732 Q
= Polyhedron_Factor(P
, 0, options
->MaxRays
);
740 barvinok_count_f(P
, result
, options
);
741 if (value_neg_p(*result
))
743 if (Q
&& P
->next
&& value_notzero_p(*result
)) {
747 for (Q
= P
->next
; Q
; Q
= Q
->next
) {
748 barvinok_count_f(Q
, &factor
, options
);
749 if (value_neg_p(factor
)) {
752 } else if (Q
->next
&& value_zero_p(factor
)) {
753 value_set_si(*result
, 0);
756 value_multiply(*result
, *result
, factor
);
765 value_set_si(*result
, -1);
768 void barvinok_count(Polyhedron
*P
, Value
* result
, unsigned NbMaxCons
)
770 barvinok_options
*options
= barvinok_options_new_with_defaults();
771 options
->MaxRays
= NbMaxCons
;
772 barvinok_count_with_options(P
, result
, options
);
773 barvinok_options_free(options
);
776 static void barvinok_count_f(Polyhedron
*P
, Value
* result
,
777 barvinok_options
*options
)
780 value_set_si(*result
, 0);
784 if (P
->Dimension
== 1)
785 return Line_Length(P
, result
);
787 int c
= P
->NbConstraints
;
788 POL_ENSURE_FACETS(P
);
789 if (c
!= P
->NbConstraints
|| P
->NbEq
!= 0)
790 return barvinok_count_with_options(P
, result
, options
);
792 POL_ENSURE_VERTICES(P
);
794 if (Polyhedron_is_infinite(P
, result
, options
))
798 if (options
->incremental_specialization
== 2)
799 cnt
= new bfcounter(P
->Dimension
);
800 else if (options
->incremental_specialization
== 1)
801 cnt
= new icounter(P
->Dimension
);
803 cnt
= new counter(P
->Dimension
);
804 cnt
->start(P
, options
);
806 cnt
->get_count(result
);
810 static void uni_polynom(int param
, Vector
*c
, evalue
*EP
)
812 unsigned dim
= c
->Size
-2;
814 value_set_si(EP
->d
,0);
815 EP
->x
.p
= new_enode(polynomial
, dim
+1, param
+1);
816 for (int j
= 0; j
<= dim
; ++j
)
817 evalue_set(&EP
->x
.p
->arr
[j
], c
->p
[j
], c
->p
[dim
+1]);
820 static void multi_polynom(Vector
*c
, evalue
* X
, evalue
*EP
)
822 unsigned dim
= c
->Size
-2;
826 evalue_set(&EC
, c
->p
[dim
], c
->p
[dim
+1]);
829 evalue_set(EP
, c
->p
[dim
], c
->p
[dim
+1]);
831 for (int i
= dim
-1; i
>= 0; --i
) {
833 value_assign(EC
.x
.n
, c
->p
[i
]);
836 free_evalue_refs(&EC
);
839 Polyhedron
*unfringe (Polyhedron
*P
, unsigned MaxRays
)
841 int len
= P
->Dimension
+2;
842 Polyhedron
*T
, *R
= P
;
845 Vector
*row
= Vector_Alloc(len
);
846 value_set_si(row
->p
[0], 1);
848 R
= DomainConstraintSimplify(Polyhedron_Copy(P
), MaxRays
);
850 Matrix
*M
= Matrix_Alloc(2, len
-1);
851 value_set_si(M
->p
[1][len
-2], 1);
852 for (int v
= 0; v
< P
->Dimension
; ++v
) {
853 value_set_si(M
->p
[0][v
], 1);
854 Polyhedron
*I
= Polyhedron_Image(R
, M
, 2+1);
855 value_set_si(M
->p
[0][v
], 0);
856 for (int r
= 0; r
< I
->NbConstraints
; ++r
) {
857 if (value_zero_p(I
->Constraint
[r
][0]))
859 if (value_zero_p(I
->Constraint
[r
][1]))
861 if (value_one_p(I
->Constraint
[r
][1]))
863 if (value_mone_p(I
->Constraint
[r
][1]))
865 value_absolute(g
, I
->Constraint
[r
][1]);
866 Vector_Set(row
->p
+1, 0, len
-2);
867 value_division(row
->p
[1+v
], I
->Constraint
[r
][1], g
);
868 mpz_fdiv_q(row
->p
[len
-1], I
->Constraint
[r
][2], g
);
870 R
= AddConstraints(row
->p
, 1, R
, MaxRays
);
882 /* this procedure may have false negatives */
883 static bool Polyhedron_is_infinite_param(Polyhedron
*P
, unsigned nparam
)
886 for (r
= 0; r
< P
->NbRays
; ++r
) {
887 if (!value_zero_p(P
->Ray
[r
][0]) &&
888 !value_zero_p(P
->Ray
[r
][P
->Dimension
+1]))
890 if (First_Non_Zero(P
->Ray
[r
]+1+P
->Dimension
-nparam
, nparam
) == -1)
896 /* Check whether all rays point in the positive directions
899 static bool Polyhedron_has_positive_rays(Polyhedron
*P
, unsigned nparam
)
902 for (r
= 0; r
< P
->NbRays
; ++r
)
903 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1])) {
905 for (i
= P
->Dimension
- nparam
; i
< P
->Dimension
; ++i
)
906 if (value_neg_p(P
->Ray
[r
][i
+1]))
912 typedef evalue
* evalue_p
;
914 struct enumerator_base
{
918 vertex_decomposer
*vpd
;
920 enumerator_base(unsigned dim
, vertex_decomposer
*vpd
)
925 vE
= new evalue_p
[vpd
->nbV
];
926 for (int j
= 0; j
< vpd
->nbV
; ++j
)
930 evalue_set_si(&mone
, -1, 1);
933 void decompose_at(Param_Vertices
*V
, int _i
, barvinok_options
*options
) {
937 value_init(vE
[_i
]->d
);
938 evalue_set_si(vE
[_i
], 0, 1);
940 vpd
->decompose_at_vertex(V
, _i
, options
);
943 virtual ~enumerator_base() {
944 for (int j
= 0; j
< vpd
->nbV
; ++j
)
946 free_evalue_refs(vE
[j
]);
951 free_evalue_refs(&mone
);
954 static enumerator_base
*create(Polyhedron
*P
, unsigned dim
, unsigned nbV
,
955 barvinok_options
*options
);
958 struct enumerator
: public signed_cone_consumer
, public vertex_decomposer
,
959 public enumerator_base
{
967 enumerator(Polyhedron
*P
, unsigned dim
, unsigned nbV
) :
968 vertex_decomposer(P
, nbV
, *this), enumerator_base(dim
, this) {
971 randomvector(P
, lambda
, dim
);
973 c
= Vector_Alloc(dim
+2);
983 virtual void handle(const signed_cone
& sc
, barvinok_options
*options
);
986 void enumerator::handle(const signed_cone
& sc
, barvinok_options
*options
)
991 assert(sc
.rays
.NumRows() == dim
);
992 for (int k
= 0; k
< dim
; ++k
) {
993 if (lambda
* sc
.rays
[k
] == 0)
999 lattice_point(V
, sc
.rays
, lambda
, &num
, 0, options
);
1000 den
= sc
.rays
* lambda
;
1001 normalize(sign
, num
.constant
, den
);
1003 dpoly
n(dim
, den
[0], 1);
1004 for (int k
= 1; k
< dim
; ++k
) {
1005 dpoly
fact(dim
, den
[k
], 1);
1008 if (num
.E
!= NULL
) {
1009 ZZ
one(INIT_VAL
, 1);
1010 dpoly_n
d(dim
, num
.constant
, one
);
1013 multi_polynom(c
, num
.E
, &EV
);
1014 eadd(&EV
, vE
[vert
]);
1015 free_evalue_refs(&EV
);
1016 free_evalue_refs(num
.E
);
1018 } else if (num
.pos
!= -1) {
1019 dpoly_n
d(dim
, num
.constant
, num
.coeff
);
1022 uni_polynom(num
.pos
, c
, &EV
);
1023 eadd(&EV
, vE
[vert
]);
1024 free_evalue_refs(&EV
);
1026 mpq_set_si(count
, 0, 1);
1027 dpoly
d(dim
, num
.constant
);
1028 d
.div(n
, count
, sign
);
1031 evalue_set(&EV
, &count
[0]._mp_num
, &count
[0]._mp_den
);
1032 eadd(&EV
, vE
[vert
]);
1033 free_evalue_refs(&EV
);
1037 struct ienumerator_base
: enumerator_base
{
1040 ienumerator_base(unsigned dim
, vertex_decomposer
*vpd
) :
1041 enumerator_base(dim
,vpd
) {
1042 E_vertex
= new evalue_p
[dim
];
1045 virtual ~ienumerator_base() {
1049 evalue
*E_num(int i
, int d
) {
1050 return E_vertex
[i
+ (dim
-d
)];
1059 cumulator(evalue
*factor
, evalue
*v
, dpoly_r
*r
) :
1060 factor(factor
), v(v
), r(r
) {}
1062 void cumulate(barvinok_options
*options
);
1064 virtual void add_term(const vector
<int>& powers
, evalue
*f2
) = 0;
1067 void cumulator::cumulate(barvinok_options
*options
)
1069 evalue cum
; // factor * 1 * E_num[0]/1 * (E_num[0]-1)/2 *...
1071 evalue t
; // E_num[0] - (m-1)
1075 if (options
->lookup_table
) {
1077 evalue_set_si(&mone
, -1, 1);
1081 evalue_copy(&cum
, factor
);
1084 value_set_si(f
.d
, 1);
1085 value_set_si(f
.x
.n
, 1);
1089 if (!options
->lookup_table
) {
1090 for (cst
= &t
; value_zero_p(cst
->d
); ) {
1091 if (cst
->x
.p
->type
== fractional
)
1092 cst
= &cst
->x
.p
->arr
[1];
1094 cst
= &cst
->x
.p
->arr
[0];
1098 for (int m
= 0; m
< r
->len
; ++m
) {
1101 value_set_si(f
.d
, m
);
1103 if (!options
->lookup_table
)
1104 value_subtract(cst
->x
.n
, cst
->x
.n
, cst
->d
);
1110 dpoly_r_term_list
& current
= r
->c
[r
->len
-1-m
];
1111 dpoly_r_term_list::iterator j
;
1112 for (j
= current
.begin(); j
!= current
.end(); ++j
) {
1113 if ((*j
)->coeff
== 0)
1115 evalue
*f2
= new evalue
;
1117 value_init(f2
->x
.n
);
1118 zz2value((*j
)->coeff
, f2
->x
.n
);
1119 zz2value(r
->denom
, f2
->d
);
1122 add_term((*j
)->powers
, f2
);
1125 free_evalue_refs(&f
);
1126 free_evalue_refs(&t
);
1127 free_evalue_refs(&cum
);
1128 if (options
->lookup_table
)
1129 free_evalue_refs(&mone
);
1132 struct E_poly_term
{
1137 struct ie_cum
: public cumulator
{
1138 vector
<E_poly_term
*> terms
;
1140 ie_cum(evalue
*factor
, evalue
*v
, dpoly_r
*r
) : cumulator(factor
, v
, r
) {}
1142 virtual void add_term(const vector
<int>& powers
, evalue
*f2
);
1145 void ie_cum::add_term(const vector
<int>& powers
, evalue
*f2
)
1148 for (k
= 0; k
< terms
.size(); ++k
) {
1149 if (terms
[k
]->powers
== powers
) {
1150 eadd(f2
, terms
[k
]->E
);
1151 free_evalue_refs(f2
);
1156 if (k
>= terms
.size()) {
1157 E_poly_term
*ET
= new E_poly_term
;
1158 ET
->powers
= powers
;
1160 terms
.push_back(ET
);
1164 struct ienumerator
: public signed_cone_consumer
, public vertex_decomposer
,
1165 public ienumerator_base
{
1171 ienumerator(Polyhedron
*P
, unsigned dim
, unsigned nbV
) :
1172 vertex_decomposer(P
, nbV
, *this), ienumerator_base(dim
, this) {
1173 vertex
.SetDims(1, dim
);
1175 den
.SetDims(dim
, dim
);
1183 virtual void handle(const signed_cone
& sc
, barvinok_options
*options
);
1184 void reduce(evalue
*factor
, const mat_ZZ
& num
, const mat_ZZ
& den_f
,
1185 barvinok_options
*options
);
1188 void ienumerator::reduce(evalue
*factor
, const mat_ZZ
& num
, const mat_ZZ
& den_f
,
1189 barvinok_options
*options
)
1191 unsigned len
= den_f
.NumRows(); // number of factors in den
1192 unsigned dim
= num
.NumCols();
1193 assert(num
.NumRows() == 1);
1196 eadd(factor
, vE
[vert
]);
1205 split_one(num
, num_s
, num_p
, den_f
, den_s
, den_r
);
1208 den_p
.SetLength(len
);
1212 normalize(one
, num_s
, num_p
, den_s
, den_p
, den_r
);
1214 emul(&mone
, factor
);
1218 for (int k
= 0; k
< len
; ++k
) {
1221 else if (den_s
[k
] == 0)
1224 if (no_param
== 0) {
1225 reduce(factor
, num_p
, den_r
, options
);
1229 pden
.SetDims(only_param
, dim
-1);
1231 for (k
= 0, l
= 0; k
< len
; ++k
)
1233 pden
[l
++] = den_r
[k
];
1235 for (k
= 0; k
< len
; ++k
)
1239 dpoly
n(no_param
, num_s
[0]);
1240 dpoly
D(no_param
, den_s
[k
], 1);
1241 for ( ; ++k
< len
; )
1242 if (den_p
[k
] == 0) {
1243 dpoly
fact(no_param
, den_s
[k
], 1);
1248 // if no_param + only_param == len then all powers
1249 // below will be all zero
1250 if (no_param
+ only_param
== len
) {
1251 if (E_num(0, dim
) != 0)
1252 r
= new dpoly_r(n
, len
);
1254 mpq_set_si(tcount
, 0, 1);
1256 n
.div(D
, tcount
, one
);
1258 if (value_notzero_p(mpq_numref(tcount
))) {
1262 value_assign(f
.x
.n
, mpq_numref(tcount
));
1263 value_assign(f
.d
, mpq_denref(tcount
));
1265 reduce(factor
, num_p
, pden
, options
);
1266 free_evalue_refs(&f
);
1271 for (k
= 0; k
< len
; ++k
) {
1272 if (den_s
[k
] == 0 || den_p
[k
] == 0)
1275 dpoly
pd(no_param
-1, den_s
[k
], 1);
1278 for (l
= 0; l
< k
; ++l
)
1279 if (den_r
[l
] == den_r
[k
])
1283 r
= new dpoly_r(n
, pd
, l
, len
);
1285 dpoly_r
*nr
= new dpoly_r(r
, pd
, l
, len
);
1291 dpoly_r
*rc
= r
->div(D
);
1294 if (E_num(0, dim
) == 0) {
1295 int common
= pden
.NumRows();
1296 dpoly_r_term_list
& final
= r
->c
[r
->len
-1];
1302 zz2value(r
->denom
, f
.d
);
1303 dpoly_r_term_list::iterator j
;
1304 for (j
= final
.begin(); j
!= final
.end(); ++j
) {
1305 if ((*j
)->coeff
== 0)
1308 for (int k
= 0; k
< r
->dim
; ++k
) {
1309 int n
= (*j
)->powers
[k
];
1312 pden
.SetDims(rows
+n
, pden
.NumCols());
1313 for (int l
= 0; l
< n
; ++l
)
1314 pden
[rows
+l
] = den_r
[k
];
1318 evalue_copy(&t
, factor
);
1319 zz2value((*j
)->coeff
, f
.x
.n
);
1321 reduce(&t
, num_p
, pden
, options
);
1322 free_evalue_refs(&t
);
1324 free_evalue_refs(&f
);
1326 ie_cum
cum(factor
, E_num(0, dim
), r
);
1327 cum
.cumulate(options
);
1329 int common
= pden
.NumRows();
1331 for (int j
= 0; j
< cum
.terms
.size(); ++j
) {
1333 pden
.SetDims(rows
, pden
.NumCols());
1334 for (int k
= 0; k
< r
->dim
; ++k
) {
1335 int n
= cum
.terms
[j
]->powers
[k
];
1338 pden
.SetDims(rows
+n
, pden
.NumCols());
1339 for (int l
= 0; l
< n
; ++l
)
1340 pden
[rows
+l
] = den_r
[k
];
1343 reduce(cum
.terms
[j
]->E
, num_p
, pden
, options
);
1344 free_evalue_refs(cum
.terms
[j
]->E
);
1345 delete cum
.terms
[j
]->E
;
1346 delete cum
.terms
[j
];
1353 static int type_offset(enode
*p
)
1355 return p
->type
== fractional
? 1 :
1356 p
->type
== flooring
? 1 : 0;
1359 static int edegree(evalue
*e
)
1364 if (value_notzero_p(e
->d
))
1368 int i
= type_offset(p
);
1369 if (p
->size
-i
-1 > d
)
1370 d
= p
->size
- i
- 1;
1371 for (; i
< p
->size
; i
++) {
1372 int d2
= edegree(&p
->arr
[i
]);
1379 void ienumerator::handle(const signed_cone
& sc
, barvinok_options
*options
)
1381 assert(sc
.det
== 1);
1383 assert(sc
.rays
.NumRows() == dim
);
1385 lattice_point(V
, sc
.rays
, vertex
[0], E_vertex
, options
);
1391 evalue_set_si(&one
, sc
.sign
, 1);
1392 reduce(&one
, vertex
, den
, options
);
1393 free_evalue_refs(&one
);
1395 for (int i
= 0; i
< dim
; ++i
)
1397 free_evalue_refs(E_vertex
[i
]);
1402 struct bfenumerator
: public vertex_decomposer
, public bf_base
,
1403 public ienumerator_base
{
1406 bfenumerator(Polyhedron
*P
, unsigned dim
, unsigned nbV
) :
1407 vertex_decomposer(P
, nbV
, *this),
1408 bf_base(dim
), ienumerator_base(dim
, this) {
1416 virtual void handle(const signed_cone
& sc
, barvinok_options
*options
);
1417 virtual void base(mat_ZZ
& factors
, bfc_vec
& v
);
1419 bfc_term_base
* new_bf_term(int len
) {
1420 bfe_term
* t
= new bfe_term(len
);
1424 virtual void set_factor(bfc_term_base
*t
, int k
, int change
) {
1425 bfe_term
* bfet
= static_cast<bfe_term
*>(t
);
1426 factor
= bfet
->factors
[k
];
1427 assert(factor
!= NULL
);
1428 bfet
->factors
[k
] = NULL
;
1430 emul(&mone
, factor
);
1433 virtual void set_factor(bfc_term_base
*t
, int k
, mpq_t
&q
, int change
) {
1434 bfe_term
* bfet
= static_cast<bfe_term
*>(t
);
1435 factor
= bfet
->factors
[k
];
1436 assert(factor
!= NULL
);
1437 bfet
->factors
[k
] = NULL
;
1443 value_oppose(f
.x
.n
, mpq_numref(q
));
1445 value_assign(f
.x
.n
, mpq_numref(q
));
1446 value_assign(f
.d
, mpq_denref(q
));
1448 free_evalue_refs(&f
);
1451 virtual void set_factor(bfc_term_base
*t
, int k
, const QQ
& c
, int change
) {
1452 bfe_term
* bfet
= static_cast<bfe_term
*>(t
);
1454 factor
= new evalue
;
1459 zz2value(c
.n
, f
.x
.n
);
1461 value_oppose(f
.x
.n
, f
.x
.n
);
1464 value_init(factor
->d
);
1465 evalue_copy(factor
, bfet
->factors
[k
]);
1467 free_evalue_refs(&f
);
1470 void set_factor(evalue
*f
, int change
) {
1476 virtual void insert_term(bfc_term_base
*t
, int i
) {
1477 bfe_term
* bfet
= static_cast<bfe_term
*>(t
);
1478 int len
= t
->terms
.NumRows()-1; // already increased by one
1480 bfet
->factors
.resize(len
+1);
1481 for (int j
= len
; j
> i
; --j
) {
1482 bfet
->factors
[j
] = bfet
->factors
[j
-1];
1483 t
->terms
[j
] = t
->terms
[j
-1];
1485 bfet
->factors
[i
] = factor
;
1489 virtual void update_term(bfc_term_base
*t
, int i
) {
1490 bfe_term
* bfet
= static_cast<bfe_term
*>(t
);
1492 eadd(factor
, bfet
->factors
[i
]);
1493 free_evalue_refs(factor
);
1497 virtual bool constant_vertex(int dim
) { return E_num(0, dim
) == 0; }
1499 virtual void cum(bf_reducer
*bfr
, bfc_term_base
*t
, int k
, dpoly_r
*r
,
1500 barvinok_options
*options
);
1503 enumerator_base
*enumerator_base::create(Polyhedron
*P
, unsigned dim
, unsigned nbV
,
1504 barvinok_options
*options
)
1506 enumerator_base
*eb
;
1508 if (options
->incremental_specialization
== BV_SPECIALIZATION_BF
)
1509 eb
= new bfenumerator(P
, dim
, nbV
);
1510 else if (options
->incremental_specialization
== BV_SPECIALIZATION_DF
)
1511 eb
= new ienumerator(P
, dim
, nbV
);
1513 eb
= new enumerator(P
, dim
, nbV
);
1518 struct bfe_cum
: public cumulator
{
1520 bfc_term_base
*told
;
1524 bfe_cum(evalue
*factor
, evalue
*v
, dpoly_r
*r
, bf_reducer
*bfr
,
1525 bfc_term_base
*t
, int k
, bfenumerator
*e
) :
1526 cumulator(factor
, v
, r
), told(t
), k(k
),
1530 virtual void add_term(const vector
<int>& powers
, evalue
*f2
);
1533 void bfe_cum::add_term(const vector
<int>& powers
, evalue
*f2
)
1535 bfr
->update_powers(powers
);
1537 bfc_term_base
* t
= bfe
->find_bfc_term(bfr
->vn
, bfr
->npowers
, bfr
->nnf
);
1538 bfe
->set_factor(f2
, bfr
->l_changes
% 2);
1539 bfe
->add_term(t
, told
->terms
[k
], bfr
->l_extra_num
);
1542 void bfenumerator::cum(bf_reducer
*bfr
, bfc_term_base
*t
, int k
,
1543 dpoly_r
*r
, barvinok_options
*options
)
1545 bfe_term
* bfet
= static_cast<bfe_term
*>(t
);
1546 bfe_cum
cum(bfet
->factors
[k
], E_num(0, bfr
->d
), r
, bfr
, t
, k
, this);
1547 cum
.cumulate(options
);
1550 void bfenumerator::base(mat_ZZ
& factors
, bfc_vec
& v
)
1552 for (int i
= 0; i
< v
.size(); ++i
) {
1553 assert(v
[i
]->terms
.NumRows() == 1);
1554 evalue
*factor
= static_cast<bfe_term
*>(v
[i
])->factors
[0];
1555 eadd(factor
, vE
[vert
]);
1560 void bfenumerator::handle(const signed_cone
& sc
, barvinok_options
*options
)
1562 assert(sc
.det
== 1);
1564 assert(sc
.rays
.NumRows() == enumerator_base::dim
);
1566 bfe_term
* t
= new bfe_term(enumerator_base::dim
);
1567 vector
< bfc_term_base
* > v
;
1570 t
->factors
.resize(1);
1572 t
->terms
.SetDims(1, enumerator_base::dim
);
1573 lattice_point(V
, sc
.rays
, t
->terms
[0], E_vertex
, options
);
1575 // the elements of factors are always lexpositive
1577 int s
= setup_factors(sc
.rays
, factors
, t
, sc
.sign
);
1579 t
->factors
[0] = new evalue
;
1580 value_init(t
->factors
[0]->d
);
1581 evalue_set_si(t
->factors
[0], s
, 1);
1582 reduce(factors
, v
, options
);
1584 for (int i
= 0; i
< enumerator_base::dim
; ++i
)
1586 free_evalue_refs(E_vertex
[i
]);
1591 #ifdef HAVE_CORRECT_VERTICES
1592 static inline Param_Polyhedron
*Polyhedron2Param_SD(Polyhedron
**Din
,
1593 Polyhedron
*Cin
,int WS
,Polyhedron
**CEq
,Matrix
**CT
)
1595 if (WS
& POL_NO_DUAL
)
1597 return Polyhedron2Param_SimplifiedDomain(Din
, Cin
, WS
, CEq
, CT
);
1600 static Param_Polyhedron
*Polyhedron2Param_SD(Polyhedron
**Din
,
1601 Polyhedron
*Cin
,int WS
,Polyhedron
**CEq
,Matrix
**CT
)
1603 static char data
[] = " 1 0 0 0 0 1 -18 "
1604 " 1 0 0 -20 0 19 1 "
1605 " 1 0 1 20 0 -20 16 "
1608 " 1 4 -20 0 0 -1 23 "
1609 " 1 -4 20 0 0 1 -22 "
1610 " 1 0 1 0 20 -20 16 "
1611 " 1 0 0 0 -20 19 1 ";
1612 static int checked
= 0;
1617 Matrix
*M
= Matrix_Alloc(9, 7);
1618 for (i
= 0; i
< 9; ++i
)
1619 for (int j
= 0; j
< 7; ++j
) {
1620 sscanf(p
, "%d%n", &v
, &n
);
1622 value_set_si(M
->p
[i
][j
], v
);
1624 Polyhedron
*P
= Constraints2Polyhedron(M
, 1024);
1626 Polyhedron
*U
= Universe_Polyhedron(1);
1627 Param_Polyhedron
*PP
= Polyhedron2Param_Domain(P
, U
, 1024);
1631 for (i
= 0, V
= PP
->V
; V
; ++i
, V
= V
->next
)
1634 Param_Polyhedron_Free(PP
);
1636 fprintf(stderr
, "WARNING: results may be incorrect\n");
1638 "WARNING: use latest version of PolyLib to remove this warning\n");
1642 return Polyhedron2Param_SimplifiedDomain(Din
, Cin
, WS
, CEq
, CT
);
1646 static evalue
* barvinok_enumerate_ev_f(Polyhedron
*P
, Polyhedron
* C
,
1647 barvinok_options
*options
);
1650 static evalue
* barvinok_enumerate_cst(Polyhedron
*P
, Polyhedron
* C
,
1651 struct barvinok_options
*options
)
1655 ALLOC(evalue
, eres
);
1656 value_init(eres
->d
);
1657 value_set_si(eres
->d
, 0);
1658 eres
->x
.p
= new_enode(partition
, 2, C
->Dimension
);
1659 EVALUE_SET_DOMAIN(eres
->x
.p
->arr
[0],
1660 DomainConstraintSimplify(C
, options
->MaxRays
));
1661 value_set_si(eres
->x
.p
->arr
[1].d
, 1);
1662 value_init(eres
->x
.p
->arr
[1].x
.n
);
1664 value_set_si(eres
->x
.p
->arr
[1].x
.n
, 0);
1666 barvinok_count_with_options(P
, &eres
->x
.p
->arr
[1].x
.n
, options
);
1671 evalue
* barvinok_enumerate_with_options(Polyhedron
*P
, Polyhedron
* C
,
1672 struct barvinok_options
*options
)
1674 //P = unfringe(P, MaxRays);
1675 Polyhedron
*Corig
= C
;
1676 Polyhedron
*CEq
= NULL
, *rVD
, *CA
;
1678 unsigned nparam
= C
->Dimension
;
1682 value_init(factor
.d
);
1683 evalue_set_si(&factor
, 1, 1);
1685 CA
= align_context(C
, P
->Dimension
, options
->MaxRays
);
1686 P
= DomainIntersection(P
, CA
, options
->MaxRays
);
1687 Polyhedron_Free(CA
);
1690 POL_ENSURE_FACETS(P
);
1691 POL_ENSURE_VERTICES(P
);
1692 POL_ENSURE_FACETS(C
);
1693 POL_ENSURE_VERTICES(C
);
1695 if (C
->Dimension
== 0 || emptyQ(P
)) {
1697 eres
= barvinok_enumerate_cst(P
, CEq
? CEq
: Polyhedron_Copy(C
), options
);
1699 emul(&factor
, eres
);
1700 reduce_evalue(eres
);
1701 free_evalue_refs(&factor
);
1708 if (Polyhedron_is_infinite_param(P
, nparam
))
1713 P
= remove_equalities_p(P
, P
->Dimension
-nparam
, &f
);
1714 mask(f
, &factor
, options
);
1717 if (P
->Dimension
== nparam
) {
1719 P
= Universe_Polyhedron(0);
1723 Polyhedron
*T
= Polyhedron_Factor(P
, nparam
, options
->MaxRays
);
1724 if (T
|| (P
->Dimension
== nparam
+1)) {
1727 for (Q
= T
? T
: P
; Q
; Q
= Q
->next
) {
1728 Polyhedron
*next
= Q
->next
;
1732 if (Q
->Dimension
!= C
->Dimension
)
1733 QC
= Polyhedron_Project(Q
, nparam
);
1736 C
= DomainIntersection(C
, QC
, options
->MaxRays
);
1738 Polyhedron_Free(C2
);
1740 Polyhedron_Free(QC
);
1748 if (T
->Dimension
== C
->Dimension
) {
1755 Polyhedron
*next
= P
->next
;
1757 eres
= barvinok_enumerate_ev_f(P
, C
, options
);
1764 for (Q
= P
->next
; Q
; Q
= Q
->next
) {
1765 Polyhedron
*next
= Q
->next
;
1768 f
= barvinok_enumerate_ev_f(Q
, C
, options
);
1770 free_evalue_refs(f
);
1780 evalue
* barvinok_enumerate_ev(Polyhedron
*P
, Polyhedron
* C
, unsigned MaxRays
)
1783 barvinok_options
*options
= barvinok_options_new_with_defaults();
1784 options
->MaxRays
= MaxRays
;
1785 E
= barvinok_enumerate_with_options(P
, C
, options
);
1786 barvinok_options_free(options
);
1790 /* adapted from mpolyhedron_inflate in PolyLib */
1791 static Polyhedron
*Polyhedron_Inflate(Polyhedron
*P
, unsigned nparam
,
1795 int nvar
= P
->Dimension
- nparam
;
1796 Matrix
*C
= Polyhedron2Constraints(P
);
1800 /* subtract the sum of the negative coefficients of each inequality */
1801 for (int i
= 0; i
< C
->NbRows
; ++i
) {
1802 value_set_si(sum
, 0);
1803 for (int j
= 0; j
< nvar
; ++j
)
1804 if (value_neg_p(C
->p
[i
][1+j
]))
1805 value_addto(sum
, sum
, C
->p
[i
][1+j
]);
1806 value_subtract(C
->p
[i
][1+P
->Dimension
], C
->p
[i
][1+P
->Dimension
], sum
);
1809 P2
= Constraints2Polyhedron(C
, MaxRays
);
1814 /* adapted from mpolyhedron_deflate in PolyLib */
1815 static Polyhedron
*Polyhedron_Deflate(Polyhedron
*P
, unsigned nparam
,
1819 int nvar
= P
->Dimension
- nparam
;
1820 Matrix
*C
= Polyhedron2Constraints(P
);
1824 /* subtract the sum of the positive coefficients of each inequality */
1825 for (int i
= 0; i
< C
->NbRows
; ++i
) {
1826 value_set_si(sum
, 0);
1827 for (int j
= 0; j
< nvar
; ++j
)
1828 if (value_pos_p(C
->p
[i
][1+j
]))
1829 value_addto(sum
, sum
, C
->p
[i
][1+j
]);
1830 value_subtract(C
->p
[i
][1+P
->Dimension
], C
->p
[i
][1+P
->Dimension
], sum
);
1833 P2
= Constraints2Polyhedron(C
, MaxRays
);
1838 static evalue
* barvinok_enumerate_ev_f(Polyhedron
*P
, Polyhedron
* C
,
1839 barvinok_options
*options
)
1841 unsigned nparam
= C
->Dimension
;
1842 bool pre_approx
= options
->polynomial_approximation
>= BV_POLAPPROX_PRE_LOWER
&&
1843 options
->polynomial_approximation
<= BV_POLAPPROX_PRE_APPROX
;
1845 if (P
->Dimension
- nparam
== 1 && !pre_approx
)
1846 return ParamLine_Length(P
, C
, options
);
1848 Param_Polyhedron
*PP
= NULL
;
1849 Polyhedron
*CEq
= NULL
, *pVD
;
1851 Param_Domain
*D
, *next
;
1854 Polyhedron
*Porig
= P
;
1858 if (options
->polynomial_approximation
== BV_POLAPPROX_PRE_UPPER
)
1859 P
= Polyhedron_Inflate(P
, nparam
, options
->MaxRays
);
1860 if (options
->polynomial_approximation
== BV_POLAPPROX_PRE_LOWER
)
1861 P
= Polyhedron_Deflate(P
, nparam
, options
->MaxRays
);
1864 PP
= Polyhedron2Param_SD(&T
, C
, options
->MaxRays
, &CEq
, &CT
);
1865 if (T
!= P
&& P
!= Porig
)
1869 if (isIdentity(CT
)) {
1873 assert(CT
->NbRows
!= CT
->NbColumns
);
1874 if (CT
->NbRows
== 1) { // no more parameters
1875 eres
= barvinok_enumerate_cst(P
, CEq
, options
);
1880 Param_Polyhedron_Free(PP
);
1886 nparam
= CT
->NbRows
- 1;
1892 Param_Polyhedron_Scale_Integer(PP
, &T
, &det
, options
->MaxRays
);
1898 unsigned dim
= P
->Dimension
- nparam
;
1900 ALLOC(evalue
, eres
);
1901 value_init(eres
->d
);
1902 value_set_si(eres
->d
, 0);
1905 for (nd
= 0, D
=PP
->D
; D
; ++nd
, D
=D
->next
);
1906 struct section
{ Polyhedron
*D
; evalue E
; };
1907 section
*s
= new section
[nd
];
1908 Polyhedron
**fVD
= new Polyhedron_p
[nd
];
1910 enumerator_base
*et
= NULL
;
1915 et
= enumerator_base::create(P
, dim
, PP
->nbV
, options
);
1917 for(nd
= 0, D
=PP
->D
; D
; D
=next
) {
1920 Polyhedron
*rVD
= reduce_domain(D
->Domain
, CT
, CEq
, fVD
, nd
, options
);
1924 pVD
= CT
? DomainImage(rVD
,CT
,options
->MaxRays
) : rVD
;
1926 value_init(s
[nd
].E
.d
);
1927 evalue_set_si(&s
[nd
].E
, 0, 1);
1930 FORALL_PVertex_in_ParamPolyhedron(V
,D
,PP
) // _i is internal counter
1933 et
->decompose_at(V
, _i
, options
);
1934 } catch (OrthogonalException
&e
) {
1937 for (; nd
>= 0; --nd
) {
1938 free_evalue_refs(&s
[nd
].E
);
1939 Domain_Free(s
[nd
].D
);
1940 Domain_Free(fVD
[nd
]);
1944 eadd(et
->vE
[_i
] , &s
[nd
].E
);
1945 END_FORALL_PVertex_in_ParamPolyhedron
;
1946 evalue_range_reduction_in_domain(&s
[nd
].E
, pVD
);
1949 addeliminatedparams_evalue(&s
[nd
].E
, CT
);
1957 evalue_set_si(eres
, 0, 1);
1959 eres
->x
.p
= new_enode(partition
, 2*nd
, C
->Dimension
);
1960 for (int j
= 0; j
< nd
; ++j
) {
1961 EVALUE_SET_DOMAIN(eres
->x
.p
->arr
[2*j
], s
[j
].D
);
1962 value_clear(eres
->x
.p
->arr
[2*j
+1].d
);
1963 eres
->x
.p
->arr
[2*j
+1] = s
[j
].E
;
1964 Domain_Free(fVD
[j
]);
1971 evalue_div(eres
, det
);
1976 Polyhedron_Free(CEq
);
1980 Enumeration
* barvinok_enumerate(Polyhedron
*P
, Polyhedron
* C
, unsigned MaxRays
)
1982 evalue
*EP
= barvinok_enumerate_ev(P
, C
, MaxRays
);
1984 return partition2enumeration(EP
);
1987 static void SwapColumns(Value
**V
, int n
, int i
, int j
)
1989 for (int r
= 0; r
< n
; ++r
)
1990 value_swap(V
[r
][i
], V
[r
][j
]);
1993 static void SwapColumns(Polyhedron
*P
, int i
, int j
)
1995 SwapColumns(P
->Constraint
, P
->NbConstraints
, i
, j
);
1996 SwapColumns(P
->Ray
, P
->NbRays
, i
, j
);
1999 /* Construct a constraint c from constraints l and u such that if
2000 * if constraint c holds then for each value of the other variables
2001 * there is at most one value of variable pos (position pos+1 in the constraints).
2003 * Given a lower and an upper bound
2004 * n_l v_i + <c_l,x> + c_l >= 0
2005 * -n_u v_i + <c_u,x> + c_u >= 0
2006 * the constructed constraint is
2008 * -(n_l<c_u,x> + n_u<c_l,x>) + (-n_l c_u - n_u c_l + n_l n_u - 1)
2010 * which is then simplified to remove the content of the non-constant coefficients
2012 * len is the total length of the constraints.
2013 * v is a temporary variable that can be used by this procedure
2015 static void negative_test_constraint(Value
*l
, Value
*u
, Value
*c
, int pos
,
2018 value_oppose(*v
, u
[pos
+1]);
2019 Vector_Combine(l
+1, u
+1, c
+1, *v
, l
[pos
+1], len
-1);
2020 value_multiply(*v
, *v
, l
[pos
+1]);
2021 value_subtract(c
[len
-1], c
[len
-1], *v
);
2022 value_set_si(*v
, -1);
2023 Vector_Scale(c
+1, c
+1, *v
, len
-1);
2024 value_decrement(c
[len
-1], c
[len
-1]);
2025 ConstraintSimplify(c
, c
, len
, v
);
2028 static bool parallel_constraints(Value
*l
, Value
*u
, Value
*c
, int pos
,
2037 Vector_Gcd(&l
[1+pos
], len
, &g1
);
2038 Vector_Gcd(&u
[1+pos
], len
, &g2
);
2039 Vector_Combine(l
+1+pos
, u
+1+pos
, c
+1, g2
, g1
, len
);
2040 parallel
= First_Non_Zero(c
+1, len
) == -1;
2048 static void negative_test_constraint7(Value
*l
, Value
*u
, Value
*c
, int pos
,
2049 int exist
, int len
, Value
*v
)
2054 Vector_Gcd(&u
[1+pos
], exist
, v
);
2055 Vector_Gcd(&l
[1+pos
], exist
, &g
);
2056 Vector_Combine(l
+1, u
+1, c
+1, *v
, g
, len
-1);
2057 value_multiply(*v
, *v
, g
);
2058 value_subtract(c
[len
-1], c
[len
-1], *v
);
2059 value_set_si(*v
, -1);
2060 Vector_Scale(c
+1, c
+1, *v
, len
-1);
2061 value_decrement(c
[len
-1], c
[len
-1]);
2062 ConstraintSimplify(c
, c
, len
, v
);
2067 /* Turns a x + b >= 0 into a x + b <= -1
2069 * len is the total length of the constraint.
2070 * v is a temporary variable that can be used by this procedure
2072 static void oppose_constraint(Value
*c
, int len
, Value
*v
)
2074 value_set_si(*v
, -1);
2075 Vector_Scale(c
+1, c
+1, *v
, len
-1);
2076 value_decrement(c
[len
-1], c
[len
-1]);
2079 /* Split polyhedron P into two polyhedra *pos and *neg, where
2080 * existential variable i has at most one solution for each
2081 * value of the other variables in *neg.
2083 * The splitting is performed using constraints l and u.
2085 * nvar: number of set variables
2086 * row: temporary vector that can be used by this procedure
2087 * f: temporary value that can be used by this procedure
2089 static bool SplitOnConstraint(Polyhedron
*P
, int i
, int l
, int u
,
2090 int nvar
, int MaxRays
, Vector
*row
, Value
& f
,
2091 Polyhedron
**pos
, Polyhedron
**neg
)
2093 negative_test_constraint(P
->Constraint
[l
], P
->Constraint
[u
],
2094 row
->p
, nvar
+i
, P
->Dimension
+2, &f
);
2095 *neg
= AddConstraints(row
->p
, 1, P
, MaxRays
);
2097 /* We found an independent, but useless constraint
2098 * Maybe we should detect this earlier and not
2099 * mark the variable as INDEPENDENT
2101 if (emptyQ((*neg
))) {
2102 Polyhedron_Free(*neg
);
2106 oppose_constraint(row
->p
, P
->Dimension
+2, &f
);
2107 *pos
= AddConstraints(row
->p
, 1, P
, MaxRays
);
2109 if (emptyQ((*pos
))) {
2110 Polyhedron_Free(*neg
);
2111 Polyhedron_Free(*pos
);
2119 * unimodularly transform P such that constraint r is transformed
2120 * into a constraint that involves only a single (the first)
2121 * existential variable
2124 static Polyhedron
*rotate_along(Polyhedron
*P
, int r
, int nvar
, int exist
,
2130 Vector
*row
= Vector_Alloc(exist
);
2131 Vector_Copy(P
->Constraint
[r
]+1+nvar
, row
->p
, exist
);
2132 Vector_Gcd(row
->p
, exist
, &g
);
2133 if (value_notone_p(g
))
2134 Vector_AntiScale(row
->p
, row
->p
, g
, exist
);
2137 Matrix
*M
= unimodular_complete(row
);
2138 Matrix
*M2
= Matrix_Alloc(P
->Dimension
+1, P
->Dimension
+1);
2139 for (r
= 0; r
< nvar
; ++r
)
2140 value_set_si(M2
->p
[r
][r
], 1);
2141 for ( ; r
< nvar
+exist
; ++r
)
2142 Vector_Copy(M
->p
[r
-nvar
], M2
->p
[r
]+nvar
, exist
);
2143 for ( ; r
< P
->Dimension
+1; ++r
)
2144 value_set_si(M2
->p
[r
][r
], 1);
2145 Polyhedron
*T
= Polyhedron_Image(P
, M2
, MaxRays
);
2154 /* Split polyhedron P into two polyhedra *pos and *neg, where
2155 * existential variable i has at most one solution for each
2156 * value of the other variables in *neg.
2158 * If independent is set, then the two constraints on which the
2159 * split will be performed need to be independent of the other
2160 * existential variables.
2162 * Return true if an appropriate split could be performed.
2164 * nvar: number of set variables
2165 * exist: number of existential variables
2166 * row: temporary vector that can be used by this procedure
2167 * f: temporary value that can be used by this procedure
2169 static bool SplitOnVar(Polyhedron
*P
, int i
,
2170 int nvar
, int exist
, int MaxRays
,
2171 Vector
*row
, Value
& f
, bool independent
,
2172 Polyhedron
**pos
, Polyhedron
**neg
)
2176 for (int l
= P
->NbEq
; l
< P
->NbConstraints
; ++l
) {
2177 if (value_negz_p(P
->Constraint
[l
][nvar
+i
+1]))
2181 for (j
= 0; j
< exist
; ++j
)
2182 if (j
!= i
&& value_notzero_p(P
->Constraint
[l
][nvar
+j
+1]))
2188 for (int u
= P
->NbEq
; u
< P
->NbConstraints
; ++u
) {
2189 if (value_posz_p(P
->Constraint
[u
][nvar
+i
+1]))
2193 for (j
= 0; j
< exist
; ++j
)
2194 if (j
!= i
&& value_notzero_p(P
->Constraint
[u
][nvar
+j
+1]))
2200 if (SplitOnConstraint(P
, i
, l
, u
, nvar
, MaxRays
, row
, f
, pos
, neg
)) {
2203 SwapColumns(*neg
, nvar
+1, nvar
+1+i
);
2213 static bool double_bound_pair(Polyhedron
*P
, int nvar
, int exist
,
2214 int i
, int l1
, int l2
,
2215 Polyhedron
**pos
, Polyhedron
**neg
)
2219 Vector
*row
= Vector_Alloc(P
->Dimension
+2);
2220 value_set_si(row
->p
[0], 1);
2221 value_oppose(f
, P
->Constraint
[l1
][nvar
+i
+1]);
2222 Vector_Combine(P
->Constraint
[l1
]+1, P
->Constraint
[l2
]+1,
2224 P
->Constraint
[l2
][nvar
+i
+1], f
,
2226 ConstraintSimplify(row
->p
, row
->p
, P
->Dimension
+2, &f
);
2227 *pos
= AddConstraints(row
->p
, 1, P
, 0);
2228 value_set_si(f
, -1);
2229 Vector_Scale(row
->p
+1, row
->p
+1, f
, P
->Dimension
+1);
2230 value_decrement(row
->p
[P
->Dimension
+1], row
->p
[P
->Dimension
+1]);
2231 *neg
= AddConstraints(row
->p
, 1, P
, 0);
2235 return !emptyQ((*pos
)) && !emptyQ((*neg
));
2238 static bool double_bound(Polyhedron
*P
, int nvar
, int exist
,
2239 Polyhedron
**pos
, Polyhedron
**neg
)
2241 for (int i
= 0; i
< exist
; ++i
) {
2243 for (l1
= P
->NbEq
; l1
< P
->NbConstraints
; ++l1
) {
2244 if (value_negz_p(P
->Constraint
[l1
][nvar
+i
+1]))
2246 for (l2
= l1
+ 1; l2
< P
->NbConstraints
; ++l2
) {
2247 if (value_negz_p(P
->Constraint
[l2
][nvar
+i
+1]))
2249 if (double_bound_pair(P
, nvar
, exist
, i
, l1
, l2
, pos
, neg
))
2253 for (l1
= P
->NbEq
; l1
< P
->NbConstraints
; ++l1
) {
2254 if (value_posz_p(P
->Constraint
[l1
][nvar
+i
+1]))
2256 if (l1
< P
->NbConstraints
)
2257 for (l2
= l1
+ 1; l2
< P
->NbConstraints
; ++l2
) {
2258 if (value_posz_p(P
->Constraint
[l2
][nvar
+i
+1]))
2260 if (double_bound_pair(P
, nvar
, exist
, i
, l1
, l2
, pos
, neg
))
2272 INDEPENDENT
= 1 << 2,
2276 static evalue
* enumerate_or(Polyhedron
*D
,
2277 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2280 fprintf(stderr
, "\nER: Or\n");
2281 #endif /* DEBUG_ER */
2283 Polyhedron
*N
= D
->next
;
2286 barvinok_enumerate_e_with_options(D
, exist
, nparam
, options
);
2289 for (D
= N
; D
; D
= N
) {
2294 barvinok_enumerate_e_with_options(D
, exist
, nparam
, options
);
2297 free_evalue_refs(EN
);
2307 static evalue
* enumerate_sum(Polyhedron
*P
,
2308 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2310 int nvar
= P
->Dimension
- exist
- nparam
;
2311 int toswap
= nvar
< exist
? nvar
: exist
;
2312 for (int i
= 0; i
< toswap
; ++i
)
2313 SwapColumns(P
, 1 + i
, nvar
+exist
- i
);
2317 fprintf(stderr
, "\nER: Sum\n");
2318 #endif /* DEBUG_ER */
2320 evalue
*EP
= barvinok_enumerate_e_with_options(P
, exist
, nparam
, options
);
2322 for (int i
= 0; i
< /* nvar */ nparam
; ++i
) {
2323 Matrix
*C
= Matrix_Alloc(1, 1 + nparam
+ 1);
2324 value_set_si(C
->p
[0][0], 1);
2326 value_init(split
.d
);
2327 value_set_si(split
.d
, 0);
2328 split
.x
.p
= new_enode(partition
, 4, nparam
);
2329 value_set_si(C
->p
[0][1+i
], 1);
2330 Matrix
*C2
= Matrix_Copy(C
);
2331 EVALUE_SET_DOMAIN(split
.x
.p
->arr
[0],
2332 Constraints2Polyhedron(C2
, options
->MaxRays
));
2334 evalue_set_si(&split
.x
.p
->arr
[1], 1, 1);
2335 value_set_si(C
->p
[0][1+i
], -1);
2336 value_set_si(C
->p
[0][1+nparam
], -1);
2337 EVALUE_SET_DOMAIN(split
.x
.p
->arr
[2],
2338 Constraints2Polyhedron(C
, options
->MaxRays
));
2339 evalue_set_si(&split
.x
.p
->arr
[3], 1, 1);
2341 free_evalue_refs(&split
);
2345 evalue_range_reduction(EP
);
2347 evalue_frac2floor2(EP
, 1);
2349 evalue
*sum
= esum(EP
, nvar
);
2351 free_evalue_refs(EP
);
2355 evalue_range_reduction(EP
);
2360 static evalue
* split_sure(Polyhedron
*P
, Polyhedron
*S
,
2361 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2363 int nvar
= P
->Dimension
- exist
- nparam
;
2365 Matrix
*M
= Matrix_Alloc(exist
, S
->Dimension
+2);
2366 for (int i
= 0; i
< exist
; ++i
)
2367 value_set_si(M
->p
[i
][nvar
+i
+1], 1);
2369 S
= DomainAddRays(S
, M
, options
->MaxRays
);
2371 Polyhedron
*F
= DomainAddRays(P
, M
, options
->MaxRays
);
2372 Polyhedron
*D
= DomainDifference(F
, S
, options
->MaxRays
);
2374 D
= Disjoint_Domain(D
, 0, options
->MaxRays
);
2379 M
= Matrix_Alloc(P
->Dimension
+1-exist
, P
->Dimension
+1);
2380 for (int j
= 0; j
< nvar
; ++j
)
2381 value_set_si(M
->p
[j
][j
], 1);
2382 for (int j
= 0; j
< nparam
+1; ++j
)
2383 value_set_si(M
->p
[nvar
+j
][nvar
+exist
+j
], 1);
2384 Polyhedron
*T
= Polyhedron_Image(S
, M
, options
->MaxRays
);
2385 evalue
*EP
= barvinok_enumerate_e_with_options(T
, 0, nparam
, options
);
2390 for (Polyhedron
*Q
= D
; Q
; Q
= Q
->next
) {
2391 Polyhedron
*N
= Q
->next
;
2393 T
= DomainIntersection(P
, Q
, options
->MaxRays
);
2394 evalue
*E
= barvinok_enumerate_e_with_options(T
, exist
, nparam
, options
);
2396 free_evalue_refs(E
);
2405 static evalue
* enumerate_sure(Polyhedron
*P
,
2406 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2410 int nvar
= P
->Dimension
- exist
- nparam
;
2416 for (i
= 0; i
< exist
; ++i
) {
2417 Matrix
*M
= Matrix_Alloc(S
->NbConstraints
, S
->Dimension
+2);
2419 value_set_si(lcm
, 1);
2420 for (int j
= 0; j
< S
->NbConstraints
; ++j
) {
2421 if (value_negz_p(S
->Constraint
[j
][1+nvar
+i
]))
2423 if (value_one_p(S
->Constraint
[j
][1+nvar
+i
]))
2425 value_lcm(lcm
, S
->Constraint
[j
][1+nvar
+i
], &lcm
);
2428 for (int j
= 0; j
< S
->NbConstraints
; ++j
) {
2429 if (value_negz_p(S
->Constraint
[j
][1+nvar
+i
]))
2431 if (value_one_p(S
->Constraint
[j
][1+nvar
+i
]))
2433 value_division(f
, lcm
, S
->Constraint
[j
][1+nvar
+i
]);
2434 Vector_Scale(S
->Constraint
[j
], M
->p
[c
], f
, S
->Dimension
+2);
2435 value_subtract(M
->p
[c
][S
->Dimension
+1],
2436 M
->p
[c
][S
->Dimension
+1],
2438 value_increment(M
->p
[c
][S
->Dimension
+1],
2439 M
->p
[c
][S
->Dimension
+1]);
2443 S
= AddConstraints(M
->p
[0], c
, S
, options
->MaxRays
);
2458 fprintf(stderr
, "\nER: Sure\n");
2459 #endif /* DEBUG_ER */
2461 return split_sure(P
, S
, exist
, nparam
, options
);
2464 static evalue
* enumerate_sure2(Polyhedron
*P
,
2465 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2467 int nvar
= P
->Dimension
- exist
- nparam
;
2469 for (r
= 0; r
< P
->NbRays
; ++r
)
2470 if (value_one_p(P
->Ray
[r
][0]) &&
2471 value_one_p(P
->Ray
[r
][P
->Dimension
+1]))
2477 Matrix
*M
= Matrix_Alloc(nvar
+ 1 + nparam
, P
->Dimension
+2);
2478 for (int i
= 0; i
< nvar
; ++i
)
2479 value_set_si(M
->p
[i
][1+i
], 1);
2480 for (int i
= 0; i
< nparam
; ++i
)
2481 value_set_si(M
->p
[i
+nvar
][1+nvar
+exist
+i
], 1);
2482 Vector_Copy(P
->Ray
[r
]+1+nvar
, M
->p
[nvar
+nparam
]+1+nvar
, exist
);
2483 value_set_si(M
->p
[nvar
+nparam
][0], 1);
2484 value_set_si(M
->p
[nvar
+nparam
][P
->Dimension
+1], 1);
2485 Polyhedron
* F
= Rays2Polyhedron(M
, options
->MaxRays
);
2488 Polyhedron
*I
= DomainIntersection(F
, P
, options
->MaxRays
);
2492 fprintf(stderr
, "\nER: Sure2\n");
2493 #endif /* DEBUG_ER */
2495 return split_sure(P
, I
, exist
, nparam
, options
);
2498 static evalue
* enumerate_cyclic(Polyhedron
*P
,
2499 unsigned exist
, unsigned nparam
,
2500 evalue
* EP
, int r
, int p
, unsigned MaxRays
)
2502 int nvar
= P
->Dimension
- exist
- nparam
;
2504 /* If EP in its fractional maps only contains references
2505 * to the remainder parameter with appropriate coefficients
2506 * then we could in principle avoid adding existentially
2507 * quantified variables to the validity domains.
2508 * We'd have to replace the remainder by m { p/m }
2509 * and multiply with an appropriate factor that is one
2510 * only in the appropriate range.
2511 * This last multiplication can be avoided if EP
2512 * has a single validity domain with no (further)
2513 * constraints on the remainder parameter
2516 Matrix
*CT
= Matrix_Alloc(nparam
+1, nparam
+3);
2517 Matrix
*M
= Matrix_Alloc(1, 1+nparam
+3);
2518 for (int j
= 0; j
< nparam
; ++j
)
2520 value_set_si(CT
->p
[j
][j
], 1);
2521 value_set_si(CT
->p
[p
][nparam
+1], 1);
2522 value_set_si(CT
->p
[nparam
][nparam
+2], 1);
2523 value_set_si(M
->p
[0][1+p
], -1);
2524 value_absolute(M
->p
[0][1+nparam
], P
->Ray
[0][1+nvar
+exist
+p
]);
2525 value_set_si(M
->p
[0][1+nparam
+1], 1);
2526 Polyhedron
*CEq
= Constraints2Polyhedron(M
, 1);
2528 addeliminatedparams_enum(EP
, CT
, CEq
, MaxRays
, nparam
);
2529 Polyhedron_Free(CEq
);
2535 static void enumerate_vd_add_ray(evalue
*EP
, Matrix
*Rays
, unsigned MaxRays
)
2537 if (value_notzero_p(EP
->d
))
2540 assert(EP
->x
.p
->type
== partition
);
2541 assert(EP
->x
.p
->pos
== EVALUE_DOMAIN(EP
->x
.p
->arr
[0])->Dimension
);
2542 for (int i
= 0; i
< EP
->x
.p
->size
/2; ++i
) {
2543 Polyhedron
*D
= EVALUE_DOMAIN(EP
->x
.p
->arr
[2*i
]);
2544 Polyhedron
*N
= DomainAddRays(D
, Rays
, MaxRays
);
2545 EVALUE_SET_DOMAIN(EP
->x
.p
->arr
[2*i
], N
);
2550 static evalue
* enumerate_line(Polyhedron
*P
,
2551 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2557 fprintf(stderr
, "\nER: Line\n");
2558 #endif /* DEBUG_ER */
2560 int nvar
= P
->Dimension
- exist
- nparam
;
2562 for (i
= 0; i
< nparam
; ++i
)
2563 if (value_notzero_p(P
->Ray
[0][1+nvar
+exist
+i
]))
2566 for (j
= i
+1; j
< nparam
; ++j
)
2567 if (value_notzero_p(P
->Ray
[0][1+nvar
+exist
+i
]))
2569 assert(j
>= nparam
); // for now
2571 Matrix
*M
= Matrix_Alloc(2, P
->Dimension
+2);
2572 value_set_si(M
->p
[0][0], 1);
2573 value_set_si(M
->p
[0][1+nvar
+exist
+i
], 1);
2574 value_set_si(M
->p
[1][0], 1);
2575 value_set_si(M
->p
[1][1+nvar
+exist
+i
], -1);
2576 value_absolute(M
->p
[1][1+P
->Dimension
], P
->Ray
[0][1+nvar
+exist
+i
]);
2577 value_decrement(M
->p
[1][1+P
->Dimension
], M
->p
[1][1+P
->Dimension
]);
2578 Polyhedron
*S
= AddConstraints(M
->p
[0], 2, P
, options
->MaxRays
);
2579 evalue
*EP
= barvinok_enumerate_e_with_options(S
, exist
, nparam
, options
);
2583 return enumerate_cyclic(P
, exist
, nparam
, EP
, 0, i
, options
->MaxRays
);
2586 static int single_param_pos(Polyhedron
*P
, unsigned exist
, unsigned nparam
,
2589 int nvar
= P
->Dimension
- exist
- nparam
;
2590 if (First_Non_Zero(P
->Ray
[r
]+1, nvar
) != -1)
2592 int i
= First_Non_Zero(P
->Ray
[r
]+1+nvar
+exist
, nparam
);
2595 if (First_Non_Zero(P
->Ray
[r
]+1+nvar
+exist
+1, nparam
-i
-1) != -1)
2600 static evalue
* enumerate_remove_ray(Polyhedron
*P
, int r
,
2601 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2604 fprintf(stderr
, "\nER: RedundantRay\n");
2605 #endif /* DEBUG_ER */
2609 value_set_si(one
, 1);
2610 int len
= P
->NbRays
-1;
2611 Matrix
*M
= Matrix_Alloc(2 * len
, P
->Dimension
+2);
2612 Vector_Copy(P
->Ray
[0], M
->p
[0], r
* (P
->Dimension
+2));
2613 Vector_Copy(P
->Ray
[r
+1], M
->p
[r
], (len
-r
) * (P
->Dimension
+2));
2614 for (int j
= 0; j
< P
->NbRays
; ++j
) {
2617 Vector_Combine(P
->Ray
[j
], P
->Ray
[r
], M
->p
[len
+j
-(j
>r
)],
2618 one
, P
->Ray
[j
][P
->Dimension
+1], P
->Dimension
+2);
2621 P
= Rays2Polyhedron(M
, options
->MaxRays
);
2623 evalue
*EP
= barvinok_enumerate_e_with_options(P
, exist
, nparam
, options
);
2630 static evalue
* enumerate_redundant_ray(Polyhedron
*P
,
2631 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2633 assert(P
->NbBid
== 0);
2634 int nvar
= P
->Dimension
- exist
- nparam
;
2638 for (int r
= 0; r
< P
->NbRays
; ++r
) {
2639 if (value_notzero_p(P
->Ray
[r
][P
->Dimension
+1]))
2641 int i1
= single_param_pos(P
, exist
, nparam
, r
);
2644 for (int r2
= r
+1; r2
< P
->NbRays
; ++r2
) {
2645 if (value_notzero_p(P
->Ray
[r2
][P
->Dimension
+1]))
2647 int i2
= single_param_pos(P
, exist
, nparam
, r2
);
2653 value_division(m
, P
->Ray
[r
][1+nvar
+exist
+i1
],
2654 P
->Ray
[r2
][1+nvar
+exist
+i1
]);
2655 value_multiply(m
, m
, P
->Ray
[r2
][1+nvar
+exist
+i1
]);
2656 /* r2 divides r => r redundant */
2657 if (value_eq(m
, P
->Ray
[r
][1+nvar
+exist
+i1
])) {
2659 return enumerate_remove_ray(P
, r
, exist
, nparam
, options
);
2662 value_division(m
, P
->Ray
[r2
][1+nvar
+exist
+i1
],
2663 P
->Ray
[r
][1+nvar
+exist
+i1
]);
2664 value_multiply(m
, m
, P
->Ray
[r
][1+nvar
+exist
+i1
]);
2665 /* r divides r2 => r2 redundant */
2666 if (value_eq(m
, P
->Ray
[r2
][1+nvar
+exist
+i1
])) {
2668 return enumerate_remove_ray(P
, r2
, exist
, nparam
, options
);
2676 static Polyhedron
*upper_bound(Polyhedron
*P
,
2677 int pos
, Value
*max
, Polyhedron
**R
)
2686 for (Polyhedron
*Q
= P
; Q
; Q
= N
) {
2688 for (r
= 0; r
< P
->NbRays
; ++r
) {
2689 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1]) &&
2690 value_pos_p(P
->Ray
[r
][1+pos
]))
2693 if (r
< P
->NbRays
) {
2701 for (r
= 0; r
< P
->NbRays
; ++r
) {
2702 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1]))
2704 mpz_fdiv_q(v
, P
->Ray
[r
][1+pos
], P
->Ray
[r
][1+P
->Dimension
]);
2705 if ((!Q
->next
&& r
== 0) || value_gt(v
, *max
))
2706 value_assign(*max
, v
);
2713 static evalue
* enumerate_ray(Polyhedron
*P
,
2714 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2716 assert(P
->NbBid
== 0);
2717 int nvar
= P
->Dimension
- exist
- nparam
;
2720 for (r
= 0; r
< P
->NbRays
; ++r
)
2721 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1]))
2727 for (r2
= r
+1; r2
< P
->NbRays
; ++r2
)
2728 if (value_zero_p(P
->Ray
[r2
][P
->Dimension
+1]))
2730 if (r2
< P
->NbRays
) {
2732 return enumerate_sum(P
, exist
, nparam
, options
);
2736 fprintf(stderr
, "\nER: Ray\n");
2737 #endif /* DEBUG_ER */
2743 value_set_si(one
, 1);
2744 int i
= single_param_pos(P
, exist
, nparam
, r
);
2745 assert(i
!= -1); // for now;
2747 Matrix
*M
= Matrix_Alloc(P
->NbRays
, P
->Dimension
+2);
2748 for (int j
= 0; j
< P
->NbRays
; ++j
) {
2749 Vector_Combine(P
->Ray
[j
], P
->Ray
[r
], M
->p
[j
],
2750 one
, P
->Ray
[j
][P
->Dimension
+1], P
->Dimension
+2);
2752 Polyhedron
*S
= Rays2Polyhedron(M
, options
->MaxRays
);
2754 Polyhedron
*D
= DomainDifference(P
, S
, options
->MaxRays
);
2756 // Polyhedron_Print(stderr, P_VALUE_FMT, D);
2757 assert(value_pos_p(P
->Ray
[r
][1+nvar
+exist
+i
])); // for now
2759 D
= upper_bound(D
, nvar
+exist
+i
, &m
, &R
);
2763 M
= Matrix_Alloc(2, P
->Dimension
+2);
2764 value_set_si(M
->p
[0][0], 1);
2765 value_set_si(M
->p
[1][0], 1);
2766 value_set_si(M
->p
[0][1+nvar
+exist
+i
], -1);
2767 value_set_si(M
->p
[1][1+nvar
+exist
+i
], 1);
2768 value_assign(M
->p
[0][1+P
->Dimension
], m
);
2769 value_oppose(M
->p
[1][1+P
->Dimension
], m
);
2770 value_addto(M
->p
[1][1+P
->Dimension
], M
->p
[1][1+P
->Dimension
],
2771 P
->Ray
[r
][1+nvar
+exist
+i
]);
2772 value_decrement(M
->p
[1][1+P
->Dimension
], M
->p
[1][1+P
->Dimension
]);
2773 // Matrix_Print(stderr, P_VALUE_FMT, M);
2774 D
= AddConstraints(M
->p
[0], 2, P
, options
->MaxRays
);
2775 // Polyhedron_Print(stderr, P_VALUE_FMT, D);
2776 value_subtract(M
->p
[0][1+P
->Dimension
], M
->p
[0][1+P
->Dimension
],
2777 P
->Ray
[r
][1+nvar
+exist
+i
]);
2778 // Matrix_Print(stderr, P_VALUE_FMT, M);
2779 S
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
2780 // Polyhedron_Print(stderr, P_VALUE_FMT, S);
2783 evalue
*EP
= barvinok_enumerate_e_with_options(D
, exist
, nparam
, options
);
2788 if (value_notone_p(P
->Ray
[r
][1+nvar
+exist
+i
]))
2789 EP
= enumerate_cyclic(P
, exist
, nparam
, EP
, r
, i
, options
->MaxRays
);
2791 M
= Matrix_Alloc(1, nparam
+2);
2792 value_set_si(M
->p
[0][0], 1);
2793 value_set_si(M
->p
[0][1+i
], 1);
2794 enumerate_vd_add_ray(EP
, M
, options
->MaxRays
);
2799 evalue
*E
= barvinok_enumerate_e_with_options(S
, exist
, nparam
, options
);
2801 free_evalue_refs(E
);
2808 evalue
*ER
= enumerate_or(R
, exist
, nparam
, options
);
2810 free_evalue_refs(ER
);
2817 static evalue
* enumerate_vd(Polyhedron
**PA
,
2818 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2820 Polyhedron
*P
= *PA
;
2821 int nvar
= P
->Dimension
- exist
- nparam
;
2822 Param_Polyhedron
*PP
= NULL
;
2823 Polyhedron
*C
= Universe_Polyhedron(nparam
);
2827 PP
= Polyhedron2Param_SimplifiedDomain(&PR
,C
, options
->MaxRays
,&CEq
,&CT
);
2831 Param_Domain
*D
, *last
;
2834 for (nd
= 0, D
=PP
->D
; D
; D
=D
->next
, ++nd
)
2837 Polyhedron
**VD
= new Polyhedron_p
[nd
];
2838 Polyhedron
**fVD
= new Polyhedron_p
[nd
];
2839 for(nd
= 0, D
=PP
->D
; D
; D
=D
->next
) {
2840 Polyhedron
*rVD
= reduce_domain(D
->Domain
, CT
, CEq
, fVD
, nd
, options
);
2853 /* This doesn't seem to have any effect */
2855 Polyhedron
*CA
= align_context(VD
[0], P
->Dimension
, options
->MaxRays
);
2857 P
= DomainIntersection(P
, CA
, options
->MaxRays
);
2860 Polyhedron_Free(CA
);
2865 if (!EP
&& CT
->NbColumns
!= CT
->NbRows
) {
2866 Polyhedron
*CEqr
= DomainImage(CEq
, CT
, options
->MaxRays
);
2867 Polyhedron
*CA
= align_context(CEqr
, PR
->Dimension
, options
->MaxRays
);
2868 Polyhedron
*I
= DomainIntersection(PR
, CA
, options
->MaxRays
);
2869 Polyhedron_Free(CEqr
);
2870 Polyhedron_Free(CA
);
2872 fprintf(stderr
, "\nER: Eliminate\n");
2873 #endif /* DEBUG_ER */
2874 nparam
-= CT
->NbColumns
- CT
->NbRows
;
2875 EP
= barvinok_enumerate_e_with_options(I
, exist
, nparam
, options
);
2876 nparam
+= CT
->NbColumns
- CT
->NbRows
;
2877 addeliminatedparams_enum(EP
, CT
, CEq
, options
->MaxRays
, nparam
);
2881 Polyhedron_Free(PR
);
2884 if (!EP
&& nd
> 1) {
2886 fprintf(stderr
, "\nER: VD\n");
2887 #endif /* DEBUG_ER */
2888 for (int i
= 0; i
< nd
; ++i
) {
2889 Polyhedron
*CA
= align_context(VD
[i
], P
->Dimension
, options
->MaxRays
);
2890 Polyhedron
*I
= DomainIntersection(P
, CA
, options
->MaxRays
);
2893 EP
= barvinok_enumerate_e_with_options(I
, exist
, nparam
, options
);
2895 evalue
*E
= barvinok_enumerate_e_with_options(I
, exist
, nparam
,
2898 free_evalue_refs(E
);
2902 Polyhedron_Free(CA
);
2906 for (int i
= 0; i
< nd
; ++i
) {
2907 Polyhedron_Free(VD
[i
]);
2908 Polyhedron_Free(fVD
[i
]);
2914 if (!EP
&& nvar
== 0) {
2917 Param_Vertices
*V
, *V2
;
2918 Matrix
* M
= Matrix_Alloc(1, P
->Dimension
+2);
2920 FORALL_PVertex_in_ParamPolyhedron(V
, last
, PP
) {
2922 FORALL_PVertex_in_ParamPolyhedron(V2
, last
, PP
) {
2929 for (int i
= 0; i
< exist
; ++i
) {
2930 value_oppose(f
, V
->Vertex
->p
[i
][nparam
+1]);
2931 Vector_Combine(V
->Vertex
->p
[i
],
2933 M
->p
[0] + 1 + nvar
+ exist
,
2934 V2
->Vertex
->p
[i
][nparam
+1],
2938 for (j
= 0; j
< nparam
; ++j
)
2939 if (value_notzero_p(M
->p
[0][1+nvar
+exist
+j
]))
2943 ConstraintSimplify(M
->p
[0], M
->p
[0],
2944 P
->Dimension
+2, &f
);
2945 value_set_si(M
->p
[0][0], 0);
2946 Polyhedron
*para
= AddConstraints(M
->p
[0], 1, P
,
2949 Polyhedron_Free(para
);
2952 Polyhedron
*pos
, *neg
;
2953 value_set_si(M
->p
[0][0], 1);
2954 value_decrement(M
->p
[0][P
->Dimension
+1],
2955 M
->p
[0][P
->Dimension
+1]);
2956 neg
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
2957 value_set_si(f
, -1);
2958 Vector_Scale(M
->p
[0]+1, M
->p
[0]+1, f
,
2960 value_decrement(M
->p
[0][P
->Dimension
+1],
2961 M
->p
[0][P
->Dimension
+1]);
2962 value_decrement(M
->p
[0][P
->Dimension
+1],
2963 M
->p
[0][P
->Dimension
+1]);
2964 pos
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
2965 if (emptyQ(neg
) && emptyQ(pos
)) {
2966 Polyhedron_Free(para
);
2967 Polyhedron_Free(pos
);
2968 Polyhedron_Free(neg
);
2972 fprintf(stderr
, "\nER: Order\n");
2973 #endif /* DEBUG_ER */
2974 EP
= barvinok_enumerate_e_with_options(para
, exist
, nparam
,
2978 E
= barvinok_enumerate_e_with_options(pos
, exist
, nparam
,
2981 free_evalue_refs(E
);
2985 E
= barvinok_enumerate_e_with_options(neg
, exist
, nparam
,
2988 free_evalue_refs(E
);
2991 Polyhedron_Free(para
);
2992 Polyhedron_Free(pos
);
2993 Polyhedron_Free(neg
);
2998 } END_FORALL_PVertex_in_ParamPolyhedron
;
3001 } END_FORALL_PVertex_in_ParamPolyhedron
;
3004 /* Search for vertex coordinate to split on */
3005 /* First look for one independent of the parameters */
3006 FORALL_PVertex_in_ParamPolyhedron(V
, last
, PP
) {
3007 for (int i
= 0; i
< exist
; ++i
) {
3009 for (j
= 0; j
< nparam
; ++j
)
3010 if (value_notzero_p(V
->Vertex
->p
[i
][j
]))
3014 value_set_si(M
->p
[0][0], 1);
3015 Vector_Set(M
->p
[0]+1, 0, nvar
+exist
);
3016 Vector_Copy(V
->Vertex
->p
[i
],
3017 M
->p
[0] + 1 + nvar
+ exist
, nparam
+1);
3018 value_oppose(M
->p
[0][1+nvar
+i
],
3019 V
->Vertex
->p
[i
][nparam
+1]);
3021 Polyhedron
*pos
, *neg
;
3022 value_set_si(M
->p
[0][0], 1);
3023 value_decrement(M
->p
[0][P
->Dimension
+1],
3024 M
->p
[0][P
->Dimension
+1]);
3025 neg
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
3026 value_set_si(f
, -1);
3027 Vector_Scale(M
->p
[0]+1, M
->p
[0]+1, f
,
3029 value_decrement(M
->p
[0][P
->Dimension
+1],
3030 M
->p
[0][P
->Dimension
+1]);
3031 value_decrement(M
->p
[0][P
->Dimension
+1],
3032 M
->p
[0][P
->Dimension
+1]);
3033 pos
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
3034 if (emptyQ(neg
) || emptyQ(pos
)) {
3035 Polyhedron_Free(pos
);
3036 Polyhedron_Free(neg
);
3039 Polyhedron_Free(pos
);
3040 value_increment(M
->p
[0][P
->Dimension
+1],
3041 M
->p
[0][P
->Dimension
+1]);
3042 pos
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
3044 fprintf(stderr
, "\nER: Vertex\n");
3045 #endif /* DEBUG_ER */
3047 EP
= enumerate_or(pos
, exist
, nparam
, options
);
3052 } END_FORALL_PVertex_in_ParamPolyhedron
;
3056 /* Search for vertex coordinate to split on */
3057 /* Now look for one that depends on the parameters */
3058 FORALL_PVertex_in_ParamPolyhedron(V
, last
, PP
) {
3059 for (int i
= 0; i
< exist
; ++i
) {
3060 value_set_si(M
->p
[0][0], 1);
3061 Vector_Set(M
->p
[0]+1, 0, nvar
+exist
);
3062 Vector_Copy(V
->Vertex
->p
[i
],
3063 M
->p
[0] + 1 + nvar
+ exist
, nparam
+1);
3064 value_oppose(M
->p
[0][1+nvar
+i
],
3065 V
->Vertex
->p
[i
][nparam
+1]);
3067 Polyhedron
*pos
, *neg
;
3068 value_set_si(M
->p
[0][0], 1);
3069 value_decrement(M
->p
[0][P
->Dimension
+1],
3070 M
->p
[0][P
->Dimension
+1]);
3071 neg
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
3072 value_set_si(f
, -1);
3073 Vector_Scale(M
->p
[0]+1, M
->p
[0]+1, f
,
3075 value_decrement(M
->p
[0][P
->Dimension
+1],
3076 M
->p
[0][P
->Dimension
+1]);
3077 value_decrement(M
->p
[0][P
->Dimension
+1],
3078 M
->p
[0][P
->Dimension
+1]);
3079 pos
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
3080 if (emptyQ(neg
) || emptyQ(pos
)) {
3081 Polyhedron_Free(pos
);
3082 Polyhedron_Free(neg
);
3085 Polyhedron_Free(pos
);
3086 value_increment(M
->p
[0][P
->Dimension
+1],
3087 M
->p
[0][P
->Dimension
+1]);
3088 pos
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
3090 fprintf(stderr
, "\nER: ParamVertex\n");
3091 #endif /* DEBUG_ER */
3093 EP
= enumerate_or(pos
, exist
, nparam
, options
);
3098 } END_FORALL_PVertex_in_ParamPolyhedron
;
3106 Polyhedron_Free(CEq
);
3110 Param_Polyhedron_Free(PP
);
3116 evalue
* barvinok_enumerate_pip(Polyhedron
*P
, unsigned exist
, unsigned nparam
,
3120 barvinok_options
*options
= barvinok_options_new_with_defaults();
3121 options
->MaxRays
= MaxRays
;
3122 E
= barvinok_enumerate_pip_with_options(P
, exist
, nparam
, options
);
3123 barvinok_options_free(options
);
3128 evalue
*barvinok_enumerate_pip_with_options(Polyhedron
*P
,
3129 unsigned exist
, unsigned nparam
, struct barvinok_options
*options
)
3134 evalue
*barvinok_enumerate_pip_with_options(Polyhedron
*P
,
3135 unsigned exist
, unsigned nparam
, struct barvinok_options
*options
)
3137 int nvar
= P
->Dimension
- exist
- nparam
;
3138 evalue
*EP
= evalue_zero();
3142 fprintf(stderr
, "\nER: PIP\n");
3143 #endif /* DEBUG_ER */
3145 Polyhedron
*D
= pip_projectout(P
, nvar
, exist
, nparam
);
3146 for (Q
= D
; Q
; Q
= N
) {
3150 exist
= Q
->Dimension
- nvar
- nparam
;
3151 E
= barvinok_enumerate_e_with_options(Q
, exist
, nparam
, options
);
3154 free_evalue_refs(E
);
3163 static bool is_single(Value
*row
, int pos
, int len
)
3165 return First_Non_Zero(row
, pos
) == -1 &&
3166 First_Non_Zero(row
+pos
+1, len
-pos
-1) == -1;
3169 static evalue
* barvinok_enumerate_e_r(Polyhedron
*P
,
3170 unsigned exist
, unsigned nparam
, barvinok_options
*options
);
3173 static int er_level
= 0;
3175 evalue
* barvinok_enumerate_e_with_options(Polyhedron
*P
,
3176 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
3178 fprintf(stderr
, "\nER: level %i\n", er_level
);
3180 Polyhedron_PrintConstraints(stderr
, P_VALUE_FMT
, P
);
3181 fprintf(stderr
, "\nE %d\nP %d\n", exist
, nparam
);
3183 P
= DomainConstraintSimplify(Polyhedron_Copy(P
), options
->MaxRays
);
3184 evalue
*EP
= barvinok_enumerate_e_r(P
, exist
, nparam
, options
);
3190 evalue
* barvinok_enumerate_e_with_options(Polyhedron
*P
,
3191 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
3193 P
= DomainConstraintSimplify(Polyhedron_Copy(P
), options
->MaxRays
);
3194 evalue
*EP
= barvinok_enumerate_e_r(P
, exist
, nparam
, options
);
3200 evalue
* barvinok_enumerate_e(Polyhedron
*P
, unsigned exist
, unsigned nparam
,
3204 barvinok_options
*options
= barvinok_options_new_with_defaults();
3205 options
->MaxRays
= MaxRays
;
3206 E
= barvinok_enumerate_e_with_options(P
, exist
, nparam
, options
);
3207 barvinok_options_free(options
);
3211 static evalue
* barvinok_enumerate_e_r(Polyhedron
*P
,
3212 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
3215 Polyhedron
*U
= Universe_Polyhedron(nparam
);
3216 evalue
*EP
= barvinok_enumerate_with_options(P
, U
, options
);
3217 //char *param_name[] = {"P", "Q", "R", "S", "T" };
3218 //print_evalue(stdout, EP, param_name);
3223 int nvar
= P
->Dimension
- exist
- nparam
;
3224 int len
= P
->Dimension
+ 2;
3227 POL_ENSURE_FACETS(P
);
3228 POL_ENSURE_VERTICES(P
);
3231 return evalue_zero();
3233 if (nvar
== 0 && nparam
== 0) {
3234 evalue
*EP
= evalue_zero();
3235 barvinok_count_with_options(P
, &EP
->x
.n
, options
);
3236 if (value_pos_p(EP
->x
.n
))
3237 value_set_si(EP
->x
.n
, 1);
3242 for (r
= 0; r
< P
->NbRays
; ++r
)
3243 if (value_zero_p(P
->Ray
[r
][0]) ||
3244 value_zero_p(P
->Ray
[r
][P
->Dimension
+1])) {
3246 for (i
= 0; i
< nvar
; ++i
)
3247 if (value_notzero_p(P
->Ray
[r
][i
+1]))
3251 for (i
= nvar
+ exist
; i
< nvar
+ exist
+ nparam
; ++i
)
3252 if (value_notzero_p(P
->Ray
[r
][i
+1]))
3254 if (i
>= nvar
+ exist
+ nparam
)
3257 if (r
< P
->NbRays
) {
3258 evalue
*EP
= evalue_zero();
3259 value_set_si(EP
->x
.n
, -1);
3264 for (r
= 0; r
< P
->NbEq
; ++r
)
3265 if ((first
= First_Non_Zero(P
->Constraint
[r
]+1+nvar
, exist
)) != -1)
3268 if (First_Non_Zero(P
->Constraint
[r
]+1+nvar
+first
+1,
3269 exist
-first
-1) != -1) {
3270 Polyhedron
*T
= rotate_along(P
, r
, nvar
, exist
, options
->MaxRays
);
3272 fprintf(stderr
, "\nER: Equality\n");
3273 #endif /* DEBUG_ER */
3274 evalue
*EP
= barvinok_enumerate_e_with_options(T
, exist
-1, nparam
,
3280 fprintf(stderr
, "\nER: Fixed\n");
3281 #endif /* DEBUG_ER */
3283 return barvinok_enumerate_e_with_options(P
, exist
-1, nparam
,
3286 Polyhedron
*T
= Polyhedron_Copy(P
);
3287 SwapColumns(T
, nvar
+1, nvar
+1+first
);
3288 evalue
*EP
= barvinok_enumerate_e_with_options(T
, exist
-1, nparam
,
3296 Vector
*row
= Vector_Alloc(len
);
3297 value_set_si(row
->p
[0], 1);
3302 enum constraint
* info
= new constraint
[exist
];
3303 for (int i
= 0; i
< exist
; ++i
) {
3305 for (int l
= P
->NbEq
; l
< P
->NbConstraints
; ++l
) {
3306 if (value_negz_p(P
->Constraint
[l
][nvar
+i
+1]))
3308 bool l_parallel
= is_single(P
->Constraint
[l
]+nvar
+1, i
, exist
);
3309 for (int u
= P
->NbEq
; u
< P
->NbConstraints
; ++u
) {
3310 if (value_posz_p(P
->Constraint
[u
][nvar
+i
+1]))
3312 bool lu_parallel
= l_parallel
||
3313 is_single(P
->Constraint
[u
]+nvar
+1, i
, exist
);
3314 value_oppose(f
, P
->Constraint
[u
][nvar
+i
+1]);
3315 Vector_Combine(P
->Constraint
[l
]+1, P
->Constraint
[u
]+1, row
->p
+1,
3316 f
, P
->Constraint
[l
][nvar
+i
+1], len
-1);
3317 if (!(info
[i
] & INDEPENDENT
)) {
3319 for (j
= 0; j
< exist
; ++j
)
3320 if (j
!= i
&& value_notzero_p(row
->p
[nvar
+j
+1]))
3323 //printf("independent: i: %d, l: %d, u: %d\n", i, l, u);
3324 info
[i
] = (constraint
)(info
[i
] | INDEPENDENT
);
3327 if (info
[i
] & ALL_POS
) {
3328 value_addto(row
->p
[len
-1], row
->p
[len
-1],
3329 P
->Constraint
[l
][nvar
+i
+1]);
3330 value_addto(row
->p
[len
-1], row
->p
[len
-1], f
);
3331 value_multiply(f
, f
, P
->Constraint
[l
][nvar
+i
+1]);
3332 value_subtract(row
->p
[len
-1], row
->p
[len
-1], f
);
3333 value_decrement(row
->p
[len
-1], row
->p
[len
-1]);
3334 ConstraintSimplify(row
->p
, row
->p
, len
, &f
);
3335 value_set_si(f
, -1);
3336 Vector_Scale(row
->p
+1, row
->p
+1, f
, len
-1);
3337 value_decrement(row
->p
[len
-1], row
->p
[len
-1]);
3338 Polyhedron
*T
= AddConstraints(row
->p
, 1, P
, options
->MaxRays
);
3340 //printf("not all_pos: i: %d, l: %d, u: %d\n", i, l, u);
3341 info
[i
] = (constraint
)(info
[i
] ^ ALL_POS
);
3343 //puts("pos remainder");
3344 //Polyhedron_Print(stdout, P_VALUE_FMT, T);
3347 if (!(info
[i
] & ONE_NEG
)) {
3349 negative_test_constraint(P
->Constraint
[l
],
3351 row
->p
, nvar
+i
, len
, &f
);
3352 oppose_constraint(row
->p
, len
, &f
);
3353 Polyhedron
*T
= AddConstraints(row
->p
, 1, P
,
3356 //printf("one_neg i: %d, l: %d, u: %d\n", i, l, u);
3357 info
[i
] = (constraint
)(info
[i
] | ONE_NEG
);
3359 //puts("neg remainder");
3360 //Polyhedron_Print(stdout, P_VALUE_FMT, T);
3362 } else if (!(info
[i
] & ROT_NEG
)) {
3363 if (parallel_constraints(P
->Constraint
[l
],
3365 row
->p
, nvar
, exist
)) {
3366 negative_test_constraint7(P
->Constraint
[l
],
3368 row
->p
, nvar
, exist
,
3370 oppose_constraint(row
->p
, len
, &f
);
3371 Polyhedron
*T
= AddConstraints(row
->p
, 1, P
,
3374 // printf("rot_neg i: %d, l: %d, u: %d\n", i, l, u);
3375 info
[i
] = (constraint
)(info
[i
] | ROT_NEG
);
3378 //puts("neg remainder");
3379 //Polyhedron_Print(stdout, P_VALUE_FMT, T);
3384 if (!(info
[i
] & ALL_POS
) && (info
[i
] & (ONE_NEG
| ROT_NEG
)))
3388 if (info
[i
] & ALL_POS
)
3395 for (int i = 0; i < exist; ++i)
3396 printf("%i: %i\n", i, info[i]);
3398 for (int i
= 0; i
< exist
; ++i
)
3399 if (info
[i
] & ALL_POS
) {
3401 fprintf(stderr
, "\nER: Positive\n");
3402 #endif /* DEBUG_ER */
3404 // Maybe we should chew off some of the fat here
3405 Matrix
*M
= Matrix_Alloc(P
->Dimension
, P
->Dimension
+1);
3406 for (int j
= 0; j
< P
->Dimension
; ++j
)
3407 value_set_si(M
->p
[j
][j
+ (j
>= i
+nvar
)], 1);
3408 Polyhedron
*T
= Polyhedron_Image(P
, M
, options
->MaxRays
);
3410 evalue
*EP
= barvinok_enumerate_e_with_options(T
, exist
-1, nparam
,
3418 for (int i
= 0; i
< exist
; ++i
)
3419 if (info
[i
] & ONE_NEG
) {
3421 fprintf(stderr
, "\nER: Negative\n");
3422 #endif /* DEBUG_ER */
3427 return barvinok_enumerate_e_with_options(P
, exist
-1, nparam
,
3430 Polyhedron
*T
= Polyhedron_Copy(P
);
3431 SwapColumns(T
, nvar
+1, nvar
+1+i
);
3432 evalue
*EP
= barvinok_enumerate_e_with_options(T
, exist
-1, nparam
,
3438 for (int i
= 0; i
< exist
; ++i
)
3439 if (info
[i
] & ROT_NEG
) {
3441 fprintf(stderr
, "\nER: Rotate\n");
3442 #endif /* DEBUG_ER */
3446 Polyhedron
*T
= rotate_along(P
, r
, nvar
, exist
, options
->MaxRays
);
3447 evalue
*EP
= barvinok_enumerate_e_with_options(T
, exist
-1, nparam
,
3452 for (int i
= 0; i
< exist
; ++i
)
3453 if (info
[i
] & INDEPENDENT
) {
3454 Polyhedron
*pos
, *neg
;
3456 /* Find constraint again and split off negative part */
3458 if (SplitOnVar(P
, i
, nvar
, exist
, options
->MaxRays
,
3459 row
, f
, true, &pos
, &neg
)) {
3461 fprintf(stderr
, "\nER: Split\n");
3462 #endif /* DEBUG_ER */
3465 barvinok_enumerate_e_with_options(neg
, exist
-1, nparam
, options
);
3467 barvinok_enumerate_e_with_options(pos
, exist
, nparam
, options
);
3469 free_evalue_refs(E
);
3471 Polyhedron_Free(neg
);
3472 Polyhedron_Free(pos
);
3486 EP
= enumerate_line(P
, exist
, nparam
, options
);
3490 EP
= barvinok_enumerate_pip_with_options(P
, exist
, nparam
, options
);
3494 EP
= enumerate_redundant_ray(P
, exist
, nparam
, options
);
3498 EP
= enumerate_sure(P
, exist
, nparam
, options
);
3502 EP
= enumerate_ray(P
, exist
, nparam
, options
);
3506 EP
= enumerate_sure2(P
, exist
, nparam
, options
);
3510 F
= unfringe(P
, options
->MaxRays
);
3511 if (!PolyhedronIncludes(F
, P
)) {
3513 fprintf(stderr
, "\nER: Fringed\n");
3514 #endif /* DEBUG_ER */
3515 EP
= barvinok_enumerate_e_with_options(F
, exist
, nparam
, options
);
3522 EP
= enumerate_vd(&P
, exist
, nparam
, options
);
3527 EP
= enumerate_sum(P
, exist
, nparam
, options
);
3534 Polyhedron
*pos
, *neg
;
3535 for (i
= 0; i
< exist
; ++i
)
3536 if (SplitOnVar(P
, i
, nvar
, exist
, options
->MaxRays
,
3537 row
, f
, false, &pos
, &neg
))
3543 EP
= enumerate_or(pos
, exist
, nparam
, options
);
3556 * remove equalities that require a "compression" of the parameters
3558 static Polyhedron
*remove_more_equalities(Polyhedron
*P
, unsigned nparam
,
3559 Matrix
**CP
, unsigned MaxRays
)
3562 remove_all_equalities(&P
, NULL
, CP
, NULL
, nparam
, MaxRays
);
3569 static gen_fun
*series(Polyhedron
*P
, unsigned nparam
, barvinok_options
*options
)
3579 assert(!Polyhedron_is_infinite_param(P
, nparam
));
3580 assert(P
->NbBid
== 0);
3581 assert(Polyhedron_has_revlex_positive_rays(P
, nparam
));
3583 P
= remove_more_equalities(P
, nparam
, &CP
, options
->MaxRays
);
3584 assert(P
->NbEq
== 0);
3586 nparam
= CP
->NbColumns
-1;
3591 barvinok_count_with_options(P
, &c
, options
);
3592 gf
= new gen_fun(c
);
3596 red
= gf_base::create(Polyhedron_Project(P
, nparam
),
3597 P
->Dimension
, nparam
, options
);
3598 POL_ENSURE_VERTICES(P
);
3599 red
->start_gf(P
, options
);
3611 gen_fun
* barvinok_series_with_options(Polyhedron
*P
, Polyhedron
* C
,
3612 barvinok_options
*options
)
3615 unsigned nparam
= C
->Dimension
;
3618 CA
= align_context(C
, P
->Dimension
, options
->MaxRays
);
3619 P
= DomainIntersection(P
, CA
, options
->MaxRays
);
3620 Polyhedron_Free(CA
);
3622 gf
= series(P
, nparam
, options
);
3627 gen_fun
* barvinok_series(Polyhedron
*P
, Polyhedron
* C
, unsigned MaxRays
)
3630 barvinok_options
*options
= barvinok_options_new_with_defaults();
3631 options
->MaxRays
= MaxRays
;
3632 gf
= barvinok_series_with_options(P
, C
, options
);
3633 barvinok_options_free(options
);
3637 static Polyhedron
*skew_into_positive_orthant(Polyhedron
*D
, unsigned nparam
,
3643 for (Polyhedron
*P
= D
; P
; P
= P
->next
) {
3644 POL_ENSURE_VERTICES(P
);
3645 assert(!Polyhedron_is_infinite_param(P
, nparam
));
3646 assert(P
->NbBid
== 0);
3647 assert(Polyhedron_has_positive_rays(P
, nparam
));
3649 for (int r
= 0; r
< P
->NbRays
; ++r
) {
3650 if (value_notzero_p(P
->Ray
[r
][P
->Dimension
+1]))
3652 for (int i
= 0; i
< nparam
; ++i
) {
3654 if (value_posz_p(P
->Ray
[r
][i
+1]))
3657 M
= Matrix_Alloc(D
->Dimension
+1, D
->Dimension
+1);
3658 for (int i
= 0; i
< D
->Dimension
+1; ++i
)
3659 value_set_si(M
->p
[i
][i
], 1);
3661 Inner_Product(P
->Ray
[r
]+1, M
->p
[i
], D
->Dimension
+1, &tmp
);
3662 if (value_posz_p(tmp
))
3665 for (j
= P
->Dimension
- nparam
; j
< P
->Dimension
; ++j
)
3666 if (value_pos_p(P
->Ray
[r
][j
+1]))
3668 assert(j
< P
->Dimension
);
3669 value_pdivision(tmp
, P
->Ray
[r
][j
+1], P
->Ray
[r
][i
+1]);
3670 value_subtract(M
->p
[i
][j
], M
->p
[i
][j
], tmp
);
3676 D
= DomainImage(D
, M
, MaxRays
);
3682 gen_fun
* barvinok_enumerate_union_series_with_options(Polyhedron
*D
, Polyhedron
* C
,
3683 barvinok_options
*options
)
3685 Polyhedron
*conv
, *D2
;
3687 gen_fun
*gf
= NULL
, *gf2
;
3688 unsigned nparam
= C
->Dimension
;
3693 CA
= align_context(C
, D
->Dimension
, options
->MaxRays
);
3694 D
= DomainIntersection(D
, CA
, options
->MaxRays
);
3695 Polyhedron_Free(CA
);
3697 D2
= skew_into_positive_orthant(D
, nparam
, options
->MaxRays
);
3698 for (Polyhedron
*P
= D2
; P
; P
= P
->next
) {
3699 assert(P
->Dimension
== D2
->Dimension
);
3702 P_gf
= series(Polyhedron_Copy(P
), nparam
, options
);
3706 gf
->add_union(P_gf
, options
);
3710 /* we actually only need the convex union of the parameter space
3711 * but the reducer classes currently expect a polyhedron in
3712 * the combined space
3714 Polyhedron_Free(gf
->context
);
3715 gf
->context
= DomainConvex(D2
, options
->MaxRays
);
3717 gf2
= gf
->summate(D2
->Dimension
- nparam
, options
);
3726 gen_fun
* barvinok_enumerate_union_series(Polyhedron
*D
, Polyhedron
* C
,
3730 barvinok_options
*options
= barvinok_options_new_with_defaults();
3731 options
->MaxRays
= MaxRays
;
3732 gf
= barvinok_enumerate_union_series_with_options(D
, C
, options
);
3733 barvinok_options_free(options
);
3737 evalue
* barvinok_enumerate_union(Polyhedron
*D
, Polyhedron
* C
, unsigned MaxRays
)
3740 gen_fun
*gf
= barvinok_enumerate_union_series(D
, C
, MaxRays
);