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))
40 static void rays(mat_ZZ
& r
, Polyhedron
*C
)
42 unsigned dim
= C
->NbRays
- 1; /* don't count zero vertex */
43 assert(C
->NbRays
- 1 == C
->Dimension
);
48 for (i
= 0, c
= 0; i
< dim
; ++i
)
49 if (value_zero_p(C
->Ray
[i
][dim
+1])) {
50 for (int j
= 0; j
< dim
; ++j
) {
51 value2zz(C
->Ray
[i
][j
+1], tmp
);
64 dpoly_n(int d
, ZZ
& degree_0
, ZZ
& degree_1
, int offset
= 0) {
68 zz2value(degree_0
, d0
);
69 zz2value(degree_1
, d1
);
70 coeff
= Matrix_Alloc(d
+1, d
+1+1);
71 value_set_si(coeff
->p
[0][0], 1);
72 value_set_si(coeff
->p
[0][d
+1], 1);
73 for (int i
= 1; i
<= d
; ++i
) {
74 value_multiply(coeff
->p
[i
][0], coeff
->p
[i
-1][0], d0
);
75 Vector_Combine(coeff
->p
[i
-1], coeff
->p
[i
-1]+1, coeff
->p
[i
]+1,
77 value_set_si(coeff
->p
[i
][d
+1], i
);
78 value_multiply(coeff
->p
[i
][d
+1], coeff
->p
[i
][d
+1], coeff
->p
[i
-1][d
+1]);
79 value_decrement(d0
, d0
);
84 void div(dpoly
& d
, Vector
*count
, ZZ
& sign
) {
85 int len
= coeff
->NbRows
;
86 Matrix
* c
= Matrix_Alloc(coeff
->NbRows
, coeff
->NbColumns
);
89 for (int i
= 0; i
< len
; ++i
) {
90 Vector_Copy(coeff
->p
[i
], c
->p
[i
], len
+1);
91 for (int j
= 1; j
<= i
; ++j
) {
92 zz2value(d
.coeff
[j
], tmp
);
93 value_multiply(tmp
, tmp
, c
->p
[i
][len
]);
94 value_oppose(tmp
, tmp
);
95 Vector_Combine(c
->p
[i
], c
->p
[i
-j
], c
->p
[i
],
96 c
->p
[i
-j
][len
], tmp
, len
);
97 value_multiply(c
->p
[i
][len
], c
->p
[i
][len
], c
->p
[i
-j
][len
]);
99 zz2value(d
.coeff
[0], tmp
);
100 value_multiply(c
->p
[i
][len
], c
->p
[i
][len
], tmp
);
103 value_set_si(tmp
, -1);
104 Vector_Scale(c
->p
[len
-1], count
->p
, tmp
, len
);
105 value_assign(count
->p
[len
], c
->p
[len
-1][len
]);
107 Vector_Copy(c
->p
[len
-1], count
->p
, len
+1);
108 Vector_Normalize(count
->p
, len
+1);
114 const int MAX_TRY
=10;
116 * Searches for a vector that is not orthogonal to any
117 * of the rays in rays.
119 static void nonorthog(mat_ZZ
& rays
, vec_ZZ
& lambda
)
121 int dim
= rays
.NumCols();
123 lambda
.SetLength(dim
);
127 for (int i
= 2; !found
&& i
<= 50*dim
; i
+=4) {
128 for (int j
= 0; j
< MAX_TRY
; ++j
) {
129 for (int k
= 0; k
< dim
; ++k
) {
130 int r
= random_int(i
)+2;
131 int v
= (2*(r
%2)-1) * (r
>> 1);
135 for (; k
< rays
.NumRows(); ++k
)
136 if (lambda
* rays
[k
] == 0)
138 if (k
== rays
.NumRows()) {
147 static void add_rays(mat_ZZ
& rays
, Polyhedron
*i
, int *r
, int nvar
= -1,
150 unsigned dim
= i
->Dimension
;
153 for (int k
= 0; k
< i
->NbRays
; ++k
) {
154 if (!value_zero_p(i
->Ray
[k
][dim
+1]))
156 if (!all
&& nvar
!= dim
&& First_Non_Zero(i
->Ray
[k
]+1, nvar
) == -1)
158 values2zz(i
->Ray
[k
]+1, rays
[(*r
)++], nvar
);
162 static void mask_r(Matrix
*f
, int nr
, Vector
*lcm
, int p
, Vector
*val
, evalue
*ev
)
164 unsigned nparam
= lcm
->Size
;
167 Vector
* prod
= Vector_Alloc(f
->NbRows
);
168 Matrix_Vector_Product(f
, val
->p
, prod
->p
);
170 for (int i
= 0; i
< nr
; ++i
) {
171 value_modulus(prod
->p
[i
], prod
->p
[i
], f
->p
[i
][nparam
+1]);
172 isint
&= value_zero_p(prod
->p
[i
]);
174 value_set_si(ev
->d
, 1);
176 value_set_si(ev
->x
.n
, isint
);
183 if (value_one_p(lcm
->p
[p
]))
184 mask_r(f
, nr
, lcm
, p
+1, val
, ev
);
186 value_assign(tmp
, lcm
->p
[p
]);
187 value_set_si(ev
->d
, 0);
188 ev
->x
.p
= new_enode(periodic
, VALUE_TO_INT(tmp
), p
+1);
190 value_decrement(tmp
, tmp
);
191 value_assign(val
->p
[p
], tmp
);
192 mask_r(f
, nr
, lcm
, p
+1, val
, &ev
->x
.p
->arr
[VALUE_TO_INT(tmp
)]);
193 } while (value_pos_p(tmp
));
199 static void mask(Matrix
*f
, evalue
*factor
)
201 int nr
= f
->NbRows
, nc
= f
->NbColumns
;
204 for (n
= 0; n
< nr
&& value_notzero_p(f
->p
[n
][nc
-1]); ++n
)
205 if (value_notone_p(f
->p
[n
][nc
-1]) &&
206 value_notmone_p(f
->p
[n
][nc
-1]))
220 value_set_si(EV
.x
.n
, 1);
222 for (n
= 0; n
< nr
; ++n
) {
223 value_assign(m
, f
->p
[n
][nc
-1]);
224 if (value_one_p(m
) || value_mone_p(m
))
227 int j
= normal_mod(f
->p
[n
], nc
-1, &m
);
229 free_evalue_refs(factor
);
230 value_init(factor
->d
);
231 evalue_set_si(factor
, 0, 1);
235 values2zz(f
->p
[n
], row
, nc
-1);
238 if (j
< (nc
-1)-1 && row
[j
] > g
/2) {
239 for (int k
= j
; k
< (nc
-1); ++k
)
245 value_set_si(EP
.d
, 0);
246 EP
.x
.p
= new_enode(relation
, 2, 0);
247 value_clear(EP
.x
.p
->arr
[1].d
);
248 EP
.x
.p
->arr
[1] = *factor
;
249 evalue
*ev
= &EP
.x
.p
->arr
[0];
250 value_set_si(ev
->d
, 0);
251 ev
->x
.p
= new_enode(fractional
, 3, -1);
252 evalue_set_si(&ev
->x
.p
->arr
[1], 0, 1);
253 evalue_set_si(&ev
->x
.p
->arr
[2], 1, 1);
254 evalue
*E
= multi_monom(row
);
255 value_assign(EV
.d
, m
);
257 value_clear(ev
->x
.p
->arr
[0].d
);
258 ev
->x
.p
->arr
[0] = *E
;
264 free_evalue_refs(&EV
);
270 static void mask(Matrix
*f
, evalue
*factor
)
272 int nr
= f
->NbRows
, nc
= f
->NbColumns
;
275 for (n
= 0; n
< nr
&& value_notzero_p(f
->p
[n
][nc
-1]); ++n
)
276 if (value_notone_p(f
->p
[n
][nc
-1]) &&
277 value_notmone_p(f
->p
[n
][nc
-1]))
285 unsigned np
= nc
- 2;
286 Vector
*lcm
= Vector_Alloc(np
);
287 Vector
*val
= Vector_Alloc(nc
);
288 Vector_Set(val
->p
, 0, nc
);
289 value_set_si(val
->p
[np
], 1);
290 Vector_Set(lcm
->p
, 1, np
);
291 for (n
= 0; n
< nr
; ++n
) {
292 if (value_one_p(f
->p
[n
][nc
-1]) ||
293 value_mone_p(f
->p
[n
][nc
-1]))
295 for (int j
= 0; j
< np
; ++j
)
296 if (value_notzero_p(f
->p
[n
][j
])) {
297 Gcd(f
->p
[n
][j
], f
->p
[n
][nc
-1], &tmp
);
298 value_division(tmp
, f
->p
[n
][nc
-1], tmp
);
299 value_lcm(tmp
, lcm
->p
[j
], &lcm
->p
[j
]);
304 mask_r(f
, nr
, lcm
, 0, val
, &EP
);
309 free_evalue_refs(&EP
);
313 /* This structure encodes the power of the term in a rational generating function.
315 * Either E == NULL or constant = 0
316 * If E != NULL, then the power is E
317 * If E == NULL, then the power is coeff * param[pos] + constant
326 /* Returns the power of (t+1) in the term of a rational generating function,
327 * i.e., the scalar product of the actual lattice point and lambda.
328 * The lattice point is the unique lattice point in the fundamental parallelepiped
329 * of the unimodual cone i shifted to the parametric vertex V.
331 * PD is the parameter domain, which, if != NULL, may be used to simply the
332 * resulting expression.
334 * The result is returned in term.
337 Param_Vertices
* V
, Polyhedron
*i
, vec_ZZ
& lambda
, term_info
* term
,
340 unsigned nparam
= V
->Vertex
->NbColumns
- 2;
341 unsigned dim
= i
->Dimension
;
343 vertex
.SetDims(V
->Vertex
->NbRows
, nparam
+1);
347 value_set_si(lcm
, 1);
348 for (int j
= 0; j
< V
->Vertex
->NbRows
; ++j
) {
349 value_lcm(lcm
, V
->Vertex
->p
[j
][nparam
+1], &lcm
);
351 if (value_notone_p(lcm
)) {
352 Matrix
* mv
= Matrix_Alloc(dim
, nparam
+1);
353 for (int j
= 0 ; j
< dim
; ++j
) {
354 value_division(tmp
, lcm
, V
->Vertex
->p
[j
][nparam
+1]);
355 Vector_Scale(V
->Vertex
->p
[j
], mv
->p
[j
], tmp
, nparam
+1);
358 term
->E
= lattice_point(i
, lambda
, mv
, lcm
, PD
);
366 for (int i
= 0; i
< V
->Vertex
->NbRows
; ++i
) {
367 assert(value_one_p(V
->Vertex
->p
[i
][nparam
+1])); // for now
368 values2zz(V
->Vertex
->p
[i
], vertex
[i
], nparam
+1);
372 num
= lambda
* vertex
;
376 for (int j
= 0; j
< nparam
; ++j
)
382 term
->E
= multi_monom(num
);
386 term
->constant
= num
[nparam
];
389 term
->coeff
= num
[p
];
397 struct counter
: public np_base
{
407 counter(unsigned dim
) : np_base(dim
) {
408 rays
.SetDims(dim
, dim
);
413 virtual void start(Polyhedron
*P
, barvinok_options
*options
);
419 virtual void handle(Polyhedron
*C
, Value
*vertex
, QQ c
);
420 virtual void get_count(Value
*result
) {
421 assert(value_one_p(&count
[0]._mp_den
));
422 value_assign(*result
, &count
[0]._mp_num
);
426 struct OrthogonalException
{} Orthogonal
;
428 void counter::handle(Polyhedron
*C
, Value
*V
, QQ c
)
431 add_rays(rays
, C
, &r
);
432 for (int k
= 0; k
< dim
; ++k
) {
433 if (lambda
* rays
[k
] == 0)
438 assert(c
.n
== 1 || c
.n
== -1);
441 lattice_point(V
, C
, vertex
);
442 num
= vertex
* lambda
;
444 normalize(sign
, num
, den
);
447 dpoly
n(dim
, den
[0], 1);
448 for (int k
= 1; k
< dim
; ++k
) {
449 dpoly
fact(dim
, den
[k
], 1);
452 d
.div(n
, count
, sign
);
455 void counter::start(Polyhedron
*P
, barvinok_options
*options
)
459 randomvector(P
, lambda
, dim
);
460 np_base::start(P
, options
);
462 } catch (OrthogonalException
&e
) {
463 mpq_set_si(count
, 0, 0);
468 struct bfe_term
: public bfc_term_base
{
469 vector
<evalue
*> factors
;
471 bfe_term(int len
) : bfc_term_base(len
) {
475 for (int i
= 0; i
< factors
.size(); ++i
) {
478 free_evalue_refs(factors
[i
]);
484 static void print_int_vector(int *v
, int len
, char *name
)
486 cerr
<< name
<< endl
;
487 for (int j
= 0; j
< len
; ++j
) {
493 static void print_bfc_terms(mat_ZZ
& factors
, bfc_vec
& v
)
496 cerr
<< "factors" << endl
;
497 cerr
<< factors
<< endl
;
498 for (int i
= 0; i
< v
.size(); ++i
) {
499 cerr
<< "term: " << i
<< endl
;
500 print_int_vector(v
[i
]->powers
, factors
.NumRows(), "powers");
501 cerr
<< "terms" << endl
;
502 cerr
<< v
[i
]->terms
<< endl
;
503 bfc_term
* bfct
= static_cast<bfc_term
*>(v
[i
]);
504 cerr
<< bfct
->c
<< endl
;
508 static void print_bfe_terms(mat_ZZ
& factors
, bfc_vec
& v
)
511 cerr
<< "factors" << endl
;
512 cerr
<< factors
<< endl
;
513 for (int i
= 0; i
< v
.size(); ++i
) {
514 cerr
<< "term: " << i
<< endl
;
515 print_int_vector(v
[i
]->powers
, factors
.NumRows(), "powers");
516 cerr
<< "terms" << endl
;
517 cerr
<< v
[i
]->terms
<< endl
;
518 bfe_term
* bfet
= static_cast<bfe_term
*>(v
[i
]);
519 for (int j
= 0; j
< v
[i
]->terms
.NumRows(); ++j
) {
520 char * test
[] = {"a", "b"};
521 print_evalue(stderr
, bfet
->factors
[j
], test
);
522 fprintf(stderr
, "\n");
527 struct bfcounter
: public bfcounter_base
{
530 bfcounter(unsigned dim
) : bfcounter_base(dim
) {
537 virtual void base(mat_ZZ
& factors
, bfc_vec
& v
);
538 virtual void get_count(Value
*result
) {
539 assert(value_one_p(&count
[0]._mp_den
));
540 value_assign(*result
, &count
[0]._mp_num
);
544 void bfcounter::base(mat_ZZ
& factors
, bfc_vec
& v
)
546 unsigned nf
= factors
.NumRows();
548 for (int i
= 0; i
< v
.size(); ++i
) {
549 bfc_term
* bfct
= static_cast<bfc_term
*>(v
[i
]);
551 // factor is always positive, so we always
553 for (int k
= 0; k
< nf
; ++k
)
554 total_power
+= v
[i
]->powers
[k
];
557 for (j
= 0; j
< nf
; ++j
)
558 if (v
[i
]->powers
[j
] > 0)
561 dpoly
D(total_power
, factors
[j
][0], 1);
562 for (int k
= 1; k
< v
[i
]->powers
[j
]; ++k
) {
563 dpoly
fact(total_power
, factors
[j
][0], 1);
567 for (int k
= 0; k
< v
[i
]->powers
[j
]; ++k
) {
568 dpoly
fact(total_power
, factors
[j
][0], 1);
572 for (int k
= 0; k
< v
[i
]->terms
.NumRows(); ++k
) {
573 dpoly
n(total_power
, v
[i
]->terms
[k
][0]);
574 mpq_set_si(tcount
, 0, 1);
575 n
.div(D
, tcount
, one
);
577 bfct
->c
[k
].n
= -bfct
->c
[k
].n
;
578 zz2value(bfct
->c
[k
].n
, tn
);
579 zz2value(bfct
->c
[k
].d
, td
);
581 mpz_mul(mpq_numref(tcount
), mpq_numref(tcount
), tn
);
582 mpz_mul(mpq_denref(tcount
), mpq_denref(tcount
), td
);
583 mpq_canonicalize(tcount
);
584 mpq_add(count
, count
, tcount
);
591 /* Check whether the polyhedron is unbounded and if so,
592 * check whether it has any (and therefore an infinite number of)
594 * If one of the vertices is integer, then we are done.
595 * Otherwise, transform the polyhedron such that one of the rays
596 * is the first unit vector and cut it off at a height that ensures
597 * that if the whole polyhedron has any points, then the remaining part
598 * has integer points. In particular we add the largest coefficient
599 * of a ray to the highest vertex (rounded up).
601 static bool Polyhedron_is_infinite(Polyhedron
*P
, Value
* result
,
602 barvinok_options
*options
)
614 for (; r
< P
->NbRays
; ++r
)
615 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1]))
617 if (P
->NbBid
== 0 && r
== P
->NbRays
)
621 if (options
->lexmin_emptiness_check
!= BV_LEXMIN_EMPTINESS_CHECK_COUNT
) {
624 sample
= Polyhedron_Sample(P
, options
);
626 value_set_si(*result
, 0);
628 value_set_si(*result
, -1);
635 for (int i
= 0; i
< P
->NbRays
; ++i
)
636 if (value_one_p(P
->Ray
[i
][1+P
->Dimension
])) {
637 value_set_si(*result
, -1);
642 v
= Vector_Alloc(P
->Dimension
+1);
643 Vector_Gcd(P
->Ray
[r
]+1, P
->Dimension
, &g
);
644 Vector_AntiScale(P
->Ray
[r
]+1, v
->p
, g
, P
->Dimension
+1);
645 M
= unimodular_complete(v
);
646 value_set_si(M
->p
[P
->Dimension
][P
->Dimension
], 1);
649 P
= Polyhedron_Preimage(P
, M2
, 0);
658 value_set_si(size
, 0);
660 for (int i
= 0; i
< P
->NbBid
; ++i
) {
661 value_absolute(tmp
, P
->Ray
[i
][1]);
662 if (value_gt(tmp
, size
))
663 value_assign(size
, tmp
);
665 for (int i
= P
->NbBid
; i
< P
->NbRays
; ++i
) {
666 if (value_zero_p(P
->Ray
[i
][P
->Dimension
+1])) {
667 if (value_gt(P
->Ray
[i
][1], size
))
668 value_assign(size
, P
->Ray
[i
][1]);
671 mpz_cdiv_q(tmp
, P
->Ray
[i
][1], P
->Ray
[i
][P
->Dimension
+1]);
672 if (first
|| value_gt(tmp
, offset
)) {
673 value_assign(offset
, tmp
);
677 value_addto(offset
, offset
, size
);
681 v
= Vector_Alloc(P
->Dimension
+2);
682 value_set_si(v
->p
[0], 1);
683 value_set_si(v
->p
[1], -1);
684 value_assign(v
->p
[1+P
->Dimension
], offset
);
685 R
= AddConstraints(v
->p
, 1, P
, options
->MaxRays
);
693 barvinok_count_with_options(P
, &c
, options
);
696 value_set_si(*result
, 0);
698 value_set_si(*result
, -1);
704 typedef Polyhedron
* Polyhedron_p
;
706 static void barvinok_count_f(Polyhedron
*P
, Value
* result
,
707 barvinok_options
*options
);
709 void barvinok_count_with_options(Polyhedron
*P
, Value
* result
,
710 struct barvinok_options
*options
)
715 bool infinite
= false;
718 value_set_si(*result
, 0);
724 P
= remove_equalities(P
);
725 P
= DomainConstraintSimplify(P
, options
->MaxRays
);
729 } while (!emptyQ(P
) && P
->NbEq
!= 0);
732 value_set_si(*result
, 0);
737 if (Polyhedron_is_infinite(P
, result
, options
)) {
742 if (P
->Dimension
== 0) {
743 /* Test whether the constraints are satisfied */
744 POL_ENSURE_VERTICES(P
);
745 value_set_si(*result
, !emptyQ(P
));
750 Q
= Polyhedron_Factor(P
, 0, options
->MaxRays
);
758 barvinok_count_f(P
, result
, options
);
759 if (value_neg_p(*result
))
761 if (Q
&& P
->next
&& value_notzero_p(*result
)) {
765 for (Q
= P
->next
; Q
; Q
= Q
->next
) {
766 barvinok_count_f(Q
, &factor
, options
);
767 if (value_neg_p(factor
)) {
770 } else if (Q
->next
&& value_zero_p(factor
)) {
771 value_set_si(*result
, 0);
774 value_multiply(*result
, *result
, factor
);
783 value_set_si(*result
, -1);
786 void barvinok_count(Polyhedron
*P
, Value
* result
, unsigned NbMaxCons
)
788 barvinok_options
*options
= barvinok_options_new_with_defaults();
789 options
->MaxRays
= NbMaxCons
;
790 barvinok_count_with_options(P
, result
, options
);
794 static void barvinok_count_f(Polyhedron
*P
, Value
* result
,
795 barvinok_options
*options
)
798 value_set_si(*result
, 0);
802 if (P
->Dimension
== 1)
803 return Line_Length(P
, result
);
805 int c
= P
->NbConstraints
;
806 POL_ENSURE_FACETS(P
);
807 if (c
!= P
->NbConstraints
|| P
->NbEq
!= 0)
808 return barvinok_count_with_options(P
, result
, options
);
810 POL_ENSURE_VERTICES(P
);
812 if (Polyhedron_is_infinite(P
, result
, options
))
816 if (options
->incremental_specialization
== 2)
817 cnt
= new bfcounter(P
->Dimension
);
818 else if (options
->incremental_specialization
== 1)
819 cnt
= new icounter(P
->Dimension
);
821 cnt
= new counter(P
->Dimension
);
822 cnt
->start(P
, options
);
824 cnt
->get_count(result
);
828 static void uni_polynom(int param
, Vector
*c
, evalue
*EP
)
830 unsigned dim
= c
->Size
-2;
832 value_set_si(EP
->d
,0);
833 EP
->x
.p
= new_enode(polynomial
, dim
+1, param
+1);
834 for (int j
= 0; j
<= dim
; ++j
)
835 evalue_set(&EP
->x
.p
->arr
[j
], c
->p
[j
], c
->p
[dim
+1]);
838 static void multi_polynom(Vector
*c
, evalue
* X
, evalue
*EP
)
840 unsigned dim
= c
->Size
-2;
844 evalue_set(&EC
, c
->p
[dim
], c
->p
[dim
+1]);
847 evalue_set(EP
, c
->p
[dim
], c
->p
[dim
+1]);
849 for (int i
= dim
-1; i
>= 0; --i
) {
851 value_assign(EC
.x
.n
, c
->p
[i
]);
854 free_evalue_refs(&EC
);
857 Polyhedron
*unfringe (Polyhedron
*P
, unsigned MaxRays
)
859 int len
= P
->Dimension
+2;
860 Polyhedron
*T
, *R
= P
;
863 Vector
*row
= Vector_Alloc(len
);
864 value_set_si(row
->p
[0], 1);
866 R
= DomainConstraintSimplify(Polyhedron_Copy(P
), MaxRays
);
868 Matrix
*M
= Matrix_Alloc(2, len
-1);
869 value_set_si(M
->p
[1][len
-2], 1);
870 for (int v
= 0; v
< P
->Dimension
; ++v
) {
871 value_set_si(M
->p
[0][v
], 1);
872 Polyhedron
*I
= Polyhedron_Image(R
, M
, 2+1);
873 value_set_si(M
->p
[0][v
], 0);
874 for (int r
= 0; r
< I
->NbConstraints
; ++r
) {
875 if (value_zero_p(I
->Constraint
[r
][0]))
877 if (value_zero_p(I
->Constraint
[r
][1]))
879 if (value_one_p(I
->Constraint
[r
][1]))
881 if (value_mone_p(I
->Constraint
[r
][1]))
883 value_absolute(g
, I
->Constraint
[r
][1]);
884 Vector_Set(row
->p
+1, 0, len
-2);
885 value_division(row
->p
[1+v
], I
->Constraint
[r
][1], g
);
886 mpz_fdiv_q(row
->p
[len
-1], I
->Constraint
[r
][2], g
);
888 R
= AddConstraints(row
->p
, 1, R
, MaxRays
);
900 /* this procedure may have false negatives */
901 static bool Polyhedron_is_infinite_param(Polyhedron
*P
, unsigned nparam
)
904 for (r
= 0; r
< P
->NbRays
; ++r
) {
905 if (!value_zero_p(P
->Ray
[r
][0]) &&
906 !value_zero_p(P
->Ray
[r
][P
->Dimension
+1]))
908 if (First_Non_Zero(P
->Ray
[r
]+1+P
->Dimension
-nparam
, nparam
) == -1)
914 /* Check whether all rays point in the positive directions
917 static bool Polyhedron_has_positive_rays(Polyhedron
*P
, unsigned nparam
)
920 for (r
= 0; r
< P
->NbRays
; ++r
)
921 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1])) {
923 for (i
= P
->Dimension
- nparam
; i
< P
->Dimension
; ++i
)
924 if (value_neg_p(P
->Ray
[r
][i
+1]))
930 typedef evalue
* evalue_p
;
932 struct enumerator_base
{
936 vertex_decomposer
*vpd
;
938 enumerator_base(unsigned dim
, vertex_decomposer
*vpd
)
943 vE
= new evalue_p
[vpd
->nbV
];
944 for (int j
= 0; j
< vpd
->nbV
; ++j
)
948 evalue_set_si(&mone
, -1, 1);
951 void decompose_at(Param_Vertices
*V
, int _i
, barvinok_options
*options
) {
955 value_init(vE
[_i
]->d
);
956 evalue_set_si(vE
[_i
], 0, 1);
958 vpd
->decompose_at_vertex(V
, _i
, options
);
961 virtual ~enumerator_base() {
962 for (int j
= 0; j
< vpd
->nbV
; ++j
)
964 free_evalue_refs(vE
[j
]);
969 free_evalue_refs(&mone
);
972 static enumerator_base
*create(Polyhedron
*P
, unsigned dim
, unsigned nbV
,
973 barvinok_options
*options
);
976 struct enumerator
: public signed_cone_consumer
, public vertex_decomposer
,
977 public enumerator_base
{
986 enumerator(Polyhedron
*P
, unsigned dim
, unsigned nbV
) :
987 vertex_decomposer(P
, nbV
, *this), enumerator_base(dim
, this) {
990 randomvector(P
, lambda
, dim
);
991 rays
.SetDims(dim
, dim
);
993 c
= Vector_Alloc(dim
+2);
1003 virtual void handle(const signed_cone
& sc
);
1006 void enumerator::handle(const signed_cone
& sc
)
1009 assert(sc
.C
->NbRays
-1 == dim
);
1010 add_rays(rays
, sc
.C
, &r
);
1011 for (int k
= 0; k
< dim
; ++k
) {
1012 if (lambda
* rays
[k
] == 0)
1018 lattice_point(V
, sc
.C
, lambda
, &num
, 0);
1019 den
= rays
* lambda
;
1020 normalize(sign
, num
.constant
, den
);
1022 dpoly
n(dim
, den
[0], 1);
1023 for (int k
= 1; k
< dim
; ++k
) {
1024 dpoly
fact(dim
, den
[k
], 1);
1027 if (num
.E
!= NULL
) {
1028 ZZ
one(INIT_VAL
, 1);
1029 dpoly_n
d(dim
, num
.constant
, one
);
1032 multi_polynom(c
, num
.E
, &EV
);
1033 eadd(&EV
, vE
[vert
]);
1034 free_evalue_refs(&EV
);
1035 free_evalue_refs(num
.E
);
1037 } else if (num
.pos
!= -1) {
1038 dpoly_n
d(dim
, num
.constant
, num
.coeff
);
1041 uni_polynom(num
.pos
, c
, &EV
);
1042 eadd(&EV
, vE
[vert
]);
1043 free_evalue_refs(&EV
);
1045 mpq_set_si(count
, 0, 1);
1046 dpoly
d(dim
, num
.constant
);
1047 d
.div(n
, count
, sign
);
1050 evalue_set(&EV
, &count
[0]._mp_num
, &count
[0]._mp_den
);
1051 eadd(&EV
, vE
[vert
]);
1052 free_evalue_refs(&EV
);
1056 struct ienumerator_base
: enumerator_base
{
1059 ienumerator_base(unsigned dim
, vertex_decomposer
*vpd
) :
1060 enumerator_base(dim
,vpd
) {
1061 E_vertex
= new evalue_p
[dim
];
1064 virtual ~ienumerator_base() {
1068 evalue
*E_num(int i
, int d
) {
1069 return E_vertex
[i
+ (dim
-d
)];
1078 cumulator(evalue
*factor
, evalue
*v
, dpoly_r
*r
) :
1079 factor(factor
), v(v
), r(r
) {}
1083 virtual void add_term(const vector
<int>& powers
, evalue
*f2
) = 0;
1086 void cumulator::cumulate()
1088 evalue cum
; // factor * 1 * E_num[0]/1 * (E_num[0]-1)/2 *...
1090 evalue t
; // E_num[0] - (m-1)
1096 evalue_set_si(&mone
, -1, 1);
1100 evalue_copy(&cum
, factor
);
1103 value_set_si(f
.d
, 1);
1104 value_set_si(f
.x
.n
, 1);
1109 for (cst
= &t
; value_zero_p(cst
->d
); ) {
1110 if (cst
->x
.p
->type
== fractional
)
1111 cst
= &cst
->x
.p
->arr
[1];
1113 cst
= &cst
->x
.p
->arr
[0];
1117 for (int m
= 0; m
< r
->len
; ++m
) {
1120 value_set_si(f
.d
, m
);
1123 value_subtract(cst
->x
.n
, cst
->x
.n
, cst
->d
);
1130 dpoly_r_term_list
& current
= r
->c
[r
->len
-1-m
];
1131 dpoly_r_term_list::iterator j
;
1132 for (j
= current
.begin(); j
!= current
.end(); ++j
) {
1133 if ((*j
)->coeff
== 0)
1135 evalue
*f2
= new evalue
;
1137 value_init(f2
->x
.n
);
1138 zz2value((*j
)->coeff
, f2
->x
.n
);
1139 zz2value(r
->denom
, f2
->d
);
1142 add_term((*j
)->powers
, f2
);
1145 free_evalue_refs(&f
);
1146 free_evalue_refs(&t
);
1147 free_evalue_refs(&cum
);
1149 free_evalue_refs(&mone
);
1153 struct E_poly_term
{
1158 struct ie_cum
: public cumulator
{
1159 vector
<E_poly_term
*> terms
;
1161 ie_cum(evalue
*factor
, evalue
*v
, dpoly_r
*r
) : cumulator(factor
, v
, r
) {}
1163 virtual void add_term(const vector
<int>& powers
, evalue
*f2
);
1166 void ie_cum::add_term(const vector
<int>& powers
, evalue
*f2
)
1169 for (k
= 0; k
< terms
.size(); ++k
) {
1170 if (terms
[k
]->powers
== powers
) {
1171 eadd(f2
, terms
[k
]->E
);
1172 free_evalue_refs(f2
);
1177 if (k
>= terms
.size()) {
1178 E_poly_term
*ET
= new E_poly_term
;
1179 ET
->powers
= powers
;
1181 terms
.push_back(ET
);
1185 struct ienumerator
: public signed_cone_consumer
, public vertex_decomposer
,
1186 public ienumerator_base
{
1192 ienumerator(Polyhedron
*P
, unsigned dim
, unsigned nbV
) :
1193 vertex_decomposer(P
, nbV
, *this), ienumerator_base(dim
, this) {
1194 vertex
.SetLength(dim
);
1196 den
.SetDims(dim
, dim
);
1204 virtual void handle(const signed_cone
& sc
);
1205 void reduce(evalue
*factor
, vec_ZZ
& num
, mat_ZZ
& den_f
);
1208 void ienumerator::reduce(
1209 evalue
*factor
, vec_ZZ
& num
, mat_ZZ
& den_f
)
1211 unsigned len
= den_f
.NumRows(); // number of factors in den
1212 unsigned dim
= num
.length();
1215 eadd(factor
, vE
[vert
]);
1220 den_s
.SetLength(len
);
1222 den_r
.SetDims(len
, dim
-1);
1226 for (r
= 0; r
< len
; ++r
) {
1227 den_s
[r
] = den_f
[r
][0];
1228 for (k
= 0; k
<= dim
-1; ++k
)
1230 den_r
[r
][k
-(k
>0)] = den_f
[r
][k
];
1235 num_p
.SetLength(dim
-1);
1236 for (k
= 0 ; k
<= dim
-1; ++k
)
1238 num_p
[k
-(k
>0)] = num
[k
];
1241 den_p
.SetLength(len
);
1245 normalize(one
, num_s
, num_p
, den_s
, den_p
, den_r
);
1247 emul(&mone
, factor
);
1251 for (int k
= 0; k
< len
; ++k
) {
1254 else if (den_s
[k
] == 0)
1257 if (no_param
== 0) {
1258 reduce(factor
, num_p
, den_r
);
1262 pden
.SetDims(only_param
, dim
-1);
1264 for (k
= 0, l
= 0; k
< len
; ++k
)
1266 pden
[l
++] = den_r
[k
];
1268 for (k
= 0; k
< len
; ++k
)
1272 dpoly
n(no_param
, num_s
);
1273 dpoly
D(no_param
, den_s
[k
], 1);
1274 for ( ; ++k
< len
; )
1275 if (den_p
[k
] == 0) {
1276 dpoly
fact(no_param
, den_s
[k
], 1);
1281 // if no_param + only_param == len then all powers
1282 // below will be all zero
1283 if (no_param
+ only_param
== len
) {
1284 if (E_num(0, dim
) != 0)
1285 r
= new dpoly_r(n
, len
);
1287 mpq_set_si(tcount
, 0, 1);
1289 n
.div(D
, tcount
, one
);
1291 if (value_notzero_p(mpq_numref(tcount
))) {
1295 value_assign(f
.x
.n
, mpq_numref(tcount
));
1296 value_assign(f
.d
, mpq_denref(tcount
));
1298 reduce(factor
, num_p
, pden
);
1299 free_evalue_refs(&f
);
1304 for (k
= 0; k
< len
; ++k
) {
1305 if (den_s
[k
] == 0 || den_p
[k
] == 0)
1308 dpoly
pd(no_param
-1, den_s
[k
], 1);
1311 for (l
= 0; l
< k
; ++l
)
1312 if (den_r
[l
] == den_r
[k
])
1316 r
= new dpoly_r(n
, pd
, l
, len
);
1318 dpoly_r
*nr
= new dpoly_r(r
, pd
, l
, len
);
1324 dpoly_r
*rc
= r
->div(D
);
1327 if (E_num(0, dim
) == 0) {
1328 int common
= pden
.NumRows();
1329 dpoly_r_term_list
& final
= r
->c
[r
->len
-1];
1335 zz2value(r
->denom
, f
.d
);
1336 dpoly_r_term_list::iterator j
;
1337 for (j
= final
.begin(); j
!= final
.end(); ++j
) {
1338 if ((*j
)->coeff
== 0)
1341 for (int k
= 0; k
< r
->dim
; ++k
) {
1342 int n
= (*j
)->powers
[k
];
1345 pden
.SetDims(rows
+n
, pden
.NumCols());
1346 for (int l
= 0; l
< n
; ++l
)
1347 pden
[rows
+l
] = den_r
[k
];
1351 evalue_copy(&t
, factor
);
1352 zz2value((*j
)->coeff
, f
.x
.n
);
1354 reduce(&t
, num_p
, pden
);
1355 free_evalue_refs(&t
);
1357 free_evalue_refs(&f
);
1359 ie_cum
cum(factor
, E_num(0, dim
), r
);
1362 int common
= pden
.NumRows();
1364 for (int j
= 0; j
< cum
.terms
.size(); ++j
) {
1366 pden
.SetDims(rows
, pden
.NumCols());
1367 for (int k
= 0; k
< r
->dim
; ++k
) {
1368 int n
= cum
.terms
[j
]->powers
[k
];
1371 pden
.SetDims(rows
+n
, pden
.NumCols());
1372 for (int l
= 0; l
< n
; ++l
)
1373 pden
[rows
+l
] = den_r
[k
];
1376 reduce(cum
.terms
[j
]->E
, num_p
, pden
);
1377 free_evalue_refs(cum
.terms
[j
]->E
);
1378 delete cum
.terms
[j
]->E
;
1379 delete cum
.terms
[j
];
1386 static int type_offset(enode
*p
)
1388 return p
->type
== fractional
? 1 :
1389 p
->type
== flooring
? 1 : 0;
1392 static int edegree(evalue
*e
)
1397 if (value_notzero_p(e
->d
))
1401 int i
= type_offset(p
);
1402 if (p
->size
-i
-1 > d
)
1403 d
= p
->size
- i
- 1;
1404 for (; i
< p
->size
; i
++) {
1405 int d2
= edegree(&p
->arr
[i
]);
1412 void ienumerator::handle(const signed_cone
& sc
)
1414 assert(sc
.C
->NbRays
-1 == dim
);
1416 lattice_point(V
, sc
.C
, vertex
, E_vertex
);
1419 for (r
= 0; r
< dim
; ++r
)
1420 values2zz(sc
.C
->Ray
[r
]+1, den
[r
], dim
);
1424 evalue_set_si(&one
, sc
.sign
, 1);
1425 reduce(&one
, vertex
, den
);
1426 free_evalue_refs(&one
);
1428 for (int i
= 0; i
< dim
; ++i
)
1430 free_evalue_refs(E_vertex
[i
]);
1435 struct bfenumerator
: public vertex_decomposer
, public bf_base
,
1436 public ienumerator_base
{
1439 bfenumerator(Polyhedron
*P
, unsigned dim
, unsigned nbV
) :
1440 vertex_decomposer(P
, nbV
, *this),
1441 bf_base(dim
), ienumerator_base(dim
, this) {
1449 virtual void handle(const signed_cone
& sc
);
1450 virtual void base(mat_ZZ
& factors
, bfc_vec
& v
);
1452 bfc_term_base
* new_bf_term(int len
) {
1453 bfe_term
* t
= new bfe_term(len
);
1457 virtual void set_factor(bfc_term_base
*t
, int k
, int change
) {
1458 bfe_term
* bfet
= static_cast<bfe_term
*>(t
);
1459 factor
= bfet
->factors
[k
];
1460 assert(factor
!= NULL
);
1461 bfet
->factors
[k
] = NULL
;
1463 emul(&mone
, factor
);
1466 virtual void set_factor(bfc_term_base
*t
, int k
, mpq_t
&q
, int change
) {
1467 bfe_term
* bfet
= static_cast<bfe_term
*>(t
);
1468 factor
= bfet
->factors
[k
];
1469 assert(factor
!= NULL
);
1470 bfet
->factors
[k
] = NULL
;
1476 value_oppose(f
.x
.n
, mpq_numref(q
));
1478 value_assign(f
.x
.n
, mpq_numref(q
));
1479 value_assign(f
.d
, mpq_denref(q
));
1481 free_evalue_refs(&f
);
1484 virtual void set_factor(bfc_term_base
*t
, int k
, const QQ
& c
, int change
) {
1485 bfe_term
* bfet
= static_cast<bfe_term
*>(t
);
1487 factor
= new evalue
;
1492 zz2value(c
.n
, f
.x
.n
);
1494 value_oppose(f
.x
.n
, f
.x
.n
);
1497 value_init(factor
->d
);
1498 evalue_copy(factor
, bfet
->factors
[k
]);
1500 free_evalue_refs(&f
);
1503 void set_factor(evalue
*f
, int change
) {
1509 virtual void insert_term(bfc_term_base
*t
, int i
) {
1510 bfe_term
* bfet
= static_cast<bfe_term
*>(t
);
1511 int len
= t
->terms
.NumRows()-1; // already increased by one
1513 bfet
->factors
.resize(len
+1);
1514 for (int j
= len
; j
> i
; --j
) {
1515 bfet
->factors
[j
] = bfet
->factors
[j
-1];
1516 t
->terms
[j
] = t
->terms
[j
-1];
1518 bfet
->factors
[i
] = factor
;
1522 virtual void update_term(bfc_term_base
*t
, int i
) {
1523 bfe_term
* bfet
= static_cast<bfe_term
*>(t
);
1525 eadd(factor
, bfet
->factors
[i
]);
1526 free_evalue_refs(factor
);
1530 virtual bool constant_vertex(int dim
) { return E_num(0, dim
) == 0; }
1532 virtual void cum(bf_reducer
*bfr
, bfc_term_base
*t
, int k
, dpoly_r
*r
);
1535 enumerator_base
*enumerator_base::create(Polyhedron
*P
, unsigned dim
, unsigned nbV
,
1536 barvinok_options
*options
)
1538 enumerator_base
*eb
;
1540 if (options
->incremental_specialization
== BV_SPECIALIZATION_BF
)
1541 eb
= new bfenumerator(P
, dim
, nbV
);
1542 else if (options
->incremental_specialization
== BV_SPECIALIZATION_DF
)
1543 eb
= new ienumerator(P
, dim
, nbV
);
1545 eb
= new enumerator(P
, dim
, nbV
);
1550 struct bfe_cum
: public cumulator
{
1552 bfc_term_base
*told
;
1556 bfe_cum(evalue
*factor
, evalue
*v
, dpoly_r
*r
, bf_reducer
*bfr
,
1557 bfc_term_base
*t
, int k
, bfenumerator
*e
) :
1558 cumulator(factor
, v
, r
), told(t
), k(k
),
1562 virtual void add_term(const vector
<int>& powers
, evalue
*f2
);
1565 void bfe_cum::add_term(const vector
<int>& powers
, evalue
*f2
)
1567 bfr
->update_powers(powers
);
1569 bfc_term_base
* t
= bfe
->find_bfc_term(bfr
->vn
, bfr
->npowers
, bfr
->nnf
);
1570 bfe
->set_factor(f2
, bfr
->l_changes
% 2);
1571 bfe
->add_term(t
, told
->terms
[k
], bfr
->l_extra_num
);
1574 void bfenumerator::cum(bf_reducer
*bfr
, bfc_term_base
*t
, int k
,
1577 bfe_term
* bfet
= static_cast<bfe_term
*>(t
);
1578 bfe_cum
cum(bfet
->factors
[k
], E_num(0, bfr
->d
), r
, bfr
, t
, k
, this);
1582 void bfenumerator::base(mat_ZZ
& factors
, bfc_vec
& v
)
1584 for (int i
= 0; i
< v
.size(); ++i
) {
1585 assert(v
[i
]->terms
.NumRows() == 1);
1586 evalue
*factor
= static_cast<bfe_term
*>(v
[i
])->factors
[0];
1587 eadd(factor
, vE
[vert
]);
1592 void bfenumerator::handle(const signed_cone
& sc
)
1594 assert(sc
.C
->NbRays
-1 == enumerator_base::dim
);
1596 bfe_term
* t
= new bfe_term(enumerator_base::dim
);
1597 vector
< bfc_term_base
* > v
;
1600 t
->factors
.resize(1);
1602 t
->terms
.SetDims(1, enumerator_base::dim
);
1603 lattice_point(V
, sc
.C
, t
->terms
[0], E_vertex
);
1605 // the elements of factors are always lexpositive
1607 int s
= setup_factors(sc
.C
, factors
, t
, sc
.sign
);
1609 t
->factors
[0] = new evalue
;
1610 value_init(t
->factors
[0]->d
);
1611 evalue_set_si(t
->factors
[0], s
, 1);
1614 for (int i
= 0; i
< enumerator_base::dim
; ++i
)
1616 free_evalue_refs(E_vertex
[i
]);
1621 #ifdef HAVE_CORRECT_VERTICES
1622 static inline Param_Polyhedron
*Polyhedron2Param_SD(Polyhedron
**Din
,
1623 Polyhedron
*Cin
,int WS
,Polyhedron
**CEq
,Matrix
**CT
)
1625 if (WS
& POL_NO_DUAL
)
1627 return Polyhedron2Param_SimplifiedDomain(Din
, Cin
, WS
, CEq
, CT
);
1630 static Param_Polyhedron
*Polyhedron2Param_SD(Polyhedron
**Din
,
1631 Polyhedron
*Cin
,int WS
,Polyhedron
**CEq
,Matrix
**CT
)
1633 static char data
[] = " 1 0 0 0 0 1 -18 "
1634 " 1 0 0 -20 0 19 1 "
1635 " 1 0 1 20 0 -20 16 "
1638 " 1 4 -20 0 0 -1 23 "
1639 " 1 -4 20 0 0 1 -22 "
1640 " 1 0 1 0 20 -20 16 "
1641 " 1 0 0 0 -20 19 1 ";
1642 static int checked
= 0;
1647 Matrix
*M
= Matrix_Alloc(9, 7);
1648 for (i
= 0; i
< 9; ++i
)
1649 for (int j
= 0; j
< 7; ++j
) {
1650 sscanf(p
, "%d%n", &v
, &n
);
1652 value_set_si(M
->p
[i
][j
], v
);
1654 Polyhedron
*P
= Constraints2Polyhedron(M
, 1024);
1656 Polyhedron
*U
= Universe_Polyhedron(1);
1657 Param_Polyhedron
*PP
= Polyhedron2Param_Domain(P
, U
, 1024);
1661 for (i
= 0, V
= PP
->V
; V
; ++i
, V
= V
->next
)
1664 Param_Polyhedron_Free(PP
);
1666 fprintf(stderr
, "WARNING: results may be incorrect\n");
1668 "WARNING: use latest version of PolyLib to remove this warning\n");
1672 return Polyhedron2Param_SimplifiedDomain(Din
, Cin
, WS
, CEq
, CT
);
1676 static evalue
* barvinok_enumerate_ev_f(Polyhedron
*P
, Polyhedron
* C
,
1677 barvinok_options
*options
);
1680 static evalue
* barvinok_enumerate_cst(Polyhedron
*P
, Polyhedron
* C
,
1685 ALLOC(evalue
, eres
);
1686 value_init(eres
->d
);
1687 value_set_si(eres
->d
, 0);
1688 eres
->x
.p
= new_enode(partition
, 2, C
->Dimension
);
1689 EVALUE_SET_DOMAIN(eres
->x
.p
->arr
[0], DomainConstraintSimplify(C
, MaxRays
));
1690 value_set_si(eres
->x
.p
->arr
[1].d
, 1);
1691 value_init(eres
->x
.p
->arr
[1].x
.n
);
1693 value_set_si(eres
->x
.p
->arr
[1].x
.n
, 0);
1695 barvinok_count(P
, &eres
->x
.p
->arr
[1].x
.n
, MaxRays
);
1700 evalue
* barvinok_enumerate_with_options(Polyhedron
*P
, Polyhedron
* C
,
1701 struct barvinok_options
*options
)
1703 //P = unfringe(P, MaxRays);
1704 Polyhedron
*Corig
= C
;
1705 Polyhedron
*CEq
= NULL
, *rVD
, *CA
;
1707 unsigned nparam
= C
->Dimension
;
1711 value_init(factor
.d
);
1712 evalue_set_si(&factor
, 1, 1);
1714 CA
= align_context(C
, P
->Dimension
, options
->MaxRays
);
1715 P
= DomainIntersection(P
, CA
, options
->MaxRays
);
1716 Polyhedron_Free(CA
);
1719 POL_ENSURE_FACETS(P
);
1720 POL_ENSURE_VERTICES(P
);
1721 POL_ENSURE_FACETS(C
);
1722 POL_ENSURE_VERTICES(C
);
1724 if (C
->Dimension
== 0 || emptyQ(P
)) {
1726 eres
= barvinok_enumerate_cst(P
, CEq
? CEq
: Polyhedron_Copy(C
),
1729 emul(&factor
, eres
);
1730 reduce_evalue(eres
);
1731 free_evalue_refs(&factor
);
1738 if (Polyhedron_is_infinite_param(P
, nparam
))
1743 P
= remove_equalities_p(P
, P
->Dimension
-nparam
, &f
);
1747 if (P
->Dimension
== nparam
) {
1749 P
= Universe_Polyhedron(0);
1753 Polyhedron
*T
= Polyhedron_Factor(P
, nparam
, options
->MaxRays
);
1754 if (T
|| (P
->Dimension
== nparam
+1)) {
1757 for (Q
= T
? T
: P
; Q
; Q
= Q
->next
) {
1758 Polyhedron
*next
= Q
->next
;
1762 if (Q
->Dimension
!= C
->Dimension
)
1763 QC
= Polyhedron_Project(Q
, nparam
);
1766 C
= DomainIntersection(C
, QC
, options
->MaxRays
);
1768 Polyhedron_Free(C2
);
1770 Polyhedron_Free(QC
);
1778 if (T
->Dimension
== C
->Dimension
) {
1785 Polyhedron
*next
= P
->next
;
1787 eres
= barvinok_enumerate_ev_f(P
, C
, options
);
1794 for (Q
= P
->next
; Q
; Q
= Q
->next
) {
1795 Polyhedron
*next
= Q
->next
;
1798 f
= barvinok_enumerate_ev_f(Q
, C
, options
);
1800 free_evalue_refs(f
);
1810 evalue
* barvinok_enumerate_ev(Polyhedron
*P
, Polyhedron
* C
, unsigned MaxRays
)
1813 barvinok_options
*options
= barvinok_options_new_with_defaults();
1814 options
->MaxRays
= MaxRays
;
1815 E
= barvinok_enumerate_with_options(P
, C
, options
);
1820 static evalue
* barvinok_enumerate_ev_f(Polyhedron
*P
, Polyhedron
* C
,
1821 barvinok_options
*options
)
1823 unsigned nparam
= C
->Dimension
;
1825 if (P
->Dimension
- nparam
== 1)
1826 return ParamLine_Length(P
, C
, options
->MaxRays
);
1828 Param_Polyhedron
*PP
= NULL
;
1829 Polyhedron
*CEq
= NULL
, *pVD
;
1831 Param_Domain
*D
, *next
;
1834 Polyhedron
*Porig
= P
;
1836 PP
= Polyhedron2Param_SD(&P
,C
,options
->MaxRays
,&CEq
,&CT
);
1838 if (isIdentity(CT
)) {
1842 assert(CT
->NbRows
!= CT
->NbColumns
);
1843 if (CT
->NbRows
== 1) { // no more parameters
1844 eres
= barvinok_enumerate_cst(P
, CEq
, options
->MaxRays
);
1849 Param_Polyhedron_Free(PP
);
1855 nparam
= CT
->NbRows
- 1;
1858 unsigned dim
= P
->Dimension
- nparam
;
1860 ALLOC(evalue
, eres
);
1861 value_init(eres
->d
);
1862 value_set_si(eres
->d
, 0);
1865 for (nd
= 0, D
=PP
->D
; D
; ++nd
, D
=D
->next
);
1866 struct section
{ Polyhedron
*D
; evalue E
; };
1867 section
*s
= new section
[nd
];
1868 Polyhedron
**fVD
= new Polyhedron_p
[nd
];
1870 enumerator_base
*et
= NULL
;
1875 et
= enumerator_base::create(P
, dim
, PP
->nbV
, options
);
1877 for(nd
= 0, D
=PP
->D
; D
; D
=next
) {
1880 Polyhedron
*rVD
= reduce_domain(D
->Domain
, CT
, CEq
,
1881 fVD
, nd
, options
->MaxRays
);
1885 pVD
= CT
? DomainImage(rVD
,CT
,options
->MaxRays
) : rVD
;
1887 value_init(s
[nd
].E
.d
);
1888 evalue_set_si(&s
[nd
].E
, 0, 1);
1891 FORALL_PVertex_in_ParamPolyhedron(V
,D
,PP
) // _i is internal counter
1894 et
->decompose_at(V
, _i
, options
);
1895 } catch (OrthogonalException
&e
) {
1898 for (; nd
>= 0; --nd
) {
1899 free_evalue_refs(&s
[nd
].E
);
1900 Domain_Free(s
[nd
].D
);
1901 Domain_Free(fVD
[nd
]);
1905 eadd(et
->vE
[_i
] , &s
[nd
].E
);
1906 END_FORALL_PVertex_in_ParamPolyhedron
;
1907 evalue_range_reduction_in_domain(&s
[nd
].E
, pVD
);
1910 addeliminatedparams_evalue(&s
[nd
].E
, CT
);
1918 evalue_set_si(eres
, 0, 1);
1920 eres
->x
.p
= new_enode(partition
, 2*nd
, C
->Dimension
);
1921 for (int j
= 0; j
< nd
; ++j
) {
1922 EVALUE_SET_DOMAIN(eres
->x
.p
->arr
[2*j
], s
[j
].D
);
1923 value_clear(eres
->x
.p
->arr
[2*j
+1].d
);
1924 eres
->x
.p
->arr
[2*j
+1] = s
[j
].E
;
1925 Domain_Free(fVD
[j
]);
1932 Polyhedron_Free(CEq
);
1936 Enumeration
* barvinok_enumerate(Polyhedron
*P
, Polyhedron
* C
, unsigned MaxRays
)
1938 evalue
*EP
= barvinok_enumerate_ev(P
, C
, MaxRays
);
1940 return partition2enumeration(EP
);
1943 static void SwapColumns(Value
**V
, int n
, int i
, int j
)
1945 for (int r
= 0; r
< n
; ++r
)
1946 value_swap(V
[r
][i
], V
[r
][j
]);
1949 static void SwapColumns(Polyhedron
*P
, int i
, int j
)
1951 SwapColumns(P
->Constraint
, P
->NbConstraints
, i
, j
);
1952 SwapColumns(P
->Ray
, P
->NbRays
, i
, j
);
1955 /* Construct a constraint c from constraints l and u such that if
1956 * if constraint c holds then for each value of the other variables
1957 * there is at most one value of variable pos (position pos+1 in the constraints).
1959 * Given a lower and an upper bound
1960 * n_l v_i + <c_l,x> + c_l >= 0
1961 * -n_u v_i + <c_u,x> + c_u >= 0
1962 * the constructed constraint is
1964 * -(n_l<c_u,x> + n_u<c_l,x>) + (-n_l c_u - n_u c_l + n_l n_u - 1)
1966 * which is then simplified to remove the content of the non-constant coefficients
1968 * len is the total length of the constraints.
1969 * v is a temporary variable that can be used by this procedure
1971 static void negative_test_constraint(Value
*l
, Value
*u
, Value
*c
, int pos
,
1974 value_oppose(*v
, u
[pos
+1]);
1975 Vector_Combine(l
+1, u
+1, c
+1, *v
, l
[pos
+1], len
-1);
1976 value_multiply(*v
, *v
, l
[pos
+1]);
1977 value_subtract(c
[len
-1], c
[len
-1], *v
);
1978 value_set_si(*v
, -1);
1979 Vector_Scale(c
+1, c
+1, *v
, len
-1);
1980 value_decrement(c
[len
-1], c
[len
-1]);
1981 ConstraintSimplify(c
, c
, len
, v
);
1984 static bool parallel_constraints(Value
*l
, Value
*u
, Value
*c
, int pos
,
1993 Vector_Gcd(&l
[1+pos
], len
, &g1
);
1994 Vector_Gcd(&u
[1+pos
], len
, &g2
);
1995 Vector_Combine(l
+1+pos
, u
+1+pos
, c
+1, g2
, g1
, len
);
1996 parallel
= First_Non_Zero(c
+1, len
) == -1;
2004 static void negative_test_constraint7(Value
*l
, Value
*u
, Value
*c
, int pos
,
2005 int exist
, int len
, Value
*v
)
2010 Vector_Gcd(&u
[1+pos
], exist
, v
);
2011 Vector_Gcd(&l
[1+pos
], exist
, &g
);
2012 Vector_Combine(l
+1, u
+1, c
+1, *v
, g
, len
-1);
2013 value_multiply(*v
, *v
, g
);
2014 value_subtract(c
[len
-1], c
[len
-1], *v
);
2015 value_set_si(*v
, -1);
2016 Vector_Scale(c
+1, c
+1, *v
, len
-1);
2017 value_decrement(c
[len
-1], c
[len
-1]);
2018 ConstraintSimplify(c
, c
, len
, v
);
2023 /* Turns a x + b >= 0 into a x + b <= -1
2025 * len is the total length of the constraint.
2026 * v is a temporary variable that can be used by this procedure
2028 static void oppose_constraint(Value
*c
, int len
, Value
*v
)
2030 value_set_si(*v
, -1);
2031 Vector_Scale(c
+1, c
+1, *v
, len
-1);
2032 value_decrement(c
[len
-1], c
[len
-1]);
2035 /* Split polyhedron P into two polyhedra *pos and *neg, where
2036 * existential variable i has at most one solution for each
2037 * value of the other variables in *neg.
2039 * The splitting is performed using constraints l and u.
2041 * nvar: number of set variables
2042 * row: temporary vector that can be used by this procedure
2043 * f: temporary value that can be used by this procedure
2045 static bool SplitOnConstraint(Polyhedron
*P
, int i
, int l
, int u
,
2046 int nvar
, int MaxRays
, Vector
*row
, Value
& f
,
2047 Polyhedron
**pos
, Polyhedron
**neg
)
2049 negative_test_constraint(P
->Constraint
[l
], P
->Constraint
[u
],
2050 row
->p
, nvar
+i
, P
->Dimension
+2, &f
);
2051 *neg
= AddConstraints(row
->p
, 1, P
, MaxRays
);
2053 /* We found an independent, but useless constraint
2054 * Maybe we should detect this earlier and not
2055 * mark the variable as INDEPENDENT
2057 if (emptyQ((*neg
))) {
2058 Polyhedron_Free(*neg
);
2062 oppose_constraint(row
->p
, P
->Dimension
+2, &f
);
2063 *pos
= AddConstraints(row
->p
, 1, P
, MaxRays
);
2065 if (emptyQ((*pos
))) {
2066 Polyhedron_Free(*neg
);
2067 Polyhedron_Free(*pos
);
2075 * unimodularly transform P such that constraint r is transformed
2076 * into a constraint that involves only a single (the first)
2077 * existential variable
2080 static Polyhedron
*rotate_along(Polyhedron
*P
, int r
, int nvar
, int exist
,
2086 Vector
*row
= Vector_Alloc(exist
);
2087 Vector_Copy(P
->Constraint
[r
]+1+nvar
, row
->p
, exist
);
2088 Vector_Gcd(row
->p
, exist
, &g
);
2089 if (value_notone_p(g
))
2090 Vector_AntiScale(row
->p
, row
->p
, g
, exist
);
2093 Matrix
*M
= unimodular_complete(row
);
2094 Matrix
*M2
= Matrix_Alloc(P
->Dimension
+1, P
->Dimension
+1);
2095 for (r
= 0; r
< nvar
; ++r
)
2096 value_set_si(M2
->p
[r
][r
], 1);
2097 for ( ; r
< nvar
+exist
; ++r
)
2098 Vector_Copy(M
->p
[r
-nvar
], M2
->p
[r
]+nvar
, exist
);
2099 for ( ; r
< P
->Dimension
+1; ++r
)
2100 value_set_si(M2
->p
[r
][r
], 1);
2101 Polyhedron
*T
= Polyhedron_Image(P
, M2
, MaxRays
);
2110 /* Split polyhedron P into two polyhedra *pos and *neg, where
2111 * existential variable i has at most one solution for each
2112 * value of the other variables in *neg.
2114 * If independent is set, then the two constraints on which the
2115 * split will be performed need to be independent of the other
2116 * existential variables.
2118 * Return true if an appropriate split could be performed.
2120 * nvar: number of set variables
2121 * exist: number of existential variables
2122 * row: temporary vector that can be used by this procedure
2123 * f: temporary value that can be used by this procedure
2125 static bool SplitOnVar(Polyhedron
*P
, int i
,
2126 int nvar
, int exist
, int MaxRays
,
2127 Vector
*row
, Value
& f
, bool independent
,
2128 Polyhedron
**pos
, Polyhedron
**neg
)
2132 for (int l
= P
->NbEq
; l
< P
->NbConstraints
; ++l
) {
2133 if (value_negz_p(P
->Constraint
[l
][nvar
+i
+1]))
2137 for (j
= 0; j
< exist
; ++j
)
2138 if (j
!= i
&& value_notzero_p(P
->Constraint
[l
][nvar
+j
+1]))
2144 for (int u
= P
->NbEq
; u
< P
->NbConstraints
; ++u
) {
2145 if (value_posz_p(P
->Constraint
[u
][nvar
+i
+1]))
2149 for (j
= 0; j
< exist
; ++j
)
2150 if (j
!= i
&& value_notzero_p(P
->Constraint
[u
][nvar
+j
+1]))
2156 if (SplitOnConstraint(P
, i
, l
, u
, nvar
, MaxRays
, row
, f
, pos
, neg
)) {
2159 SwapColumns(*neg
, nvar
+1, nvar
+1+i
);
2169 static bool double_bound_pair(Polyhedron
*P
, int nvar
, int exist
,
2170 int i
, int l1
, int l2
,
2171 Polyhedron
**pos
, Polyhedron
**neg
)
2175 Vector
*row
= Vector_Alloc(P
->Dimension
+2);
2176 value_set_si(row
->p
[0], 1);
2177 value_oppose(f
, P
->Constraint
[l1
][nvar
+i
+1]);
2178 Vector_Combine(P
->Constraint
[l1
]+1, P
->Constraint
[l2
]+1,
2180 P
->Constraint
[l2
][nvar
+i
+1], f
,
2182 ConstraintSimplify(row
->p
, row
->p
, P
->Dimension
+2, &f
);
2183 *pos
= AddConstraints(row
->p
, 1, P
, 0);
2184 value_set_si(f
, -1);
2185 Vector_Scale(row
->p
+1, row
->p
+1, f
, P
->Dimension
+1);
2186 value_decrement(row
->p
[P
->Dimension
+1], row
->p
[P
->Dimension
+1]);
2187 *neg
= AddConstraints(row
->p
, 1, P
, 0);
2191 return !emptyQ((*pos
)) && !emptyQ((*neg
));
2194 static bool double_bound(Polyhedron
*P
, int nvar
, int exist
,
2195 Polyhedron
**pos
, Polyhedron
**neg
)
2197 for (int i
= 0; i
< exist
; ++i
) {
2199 for (l1
= P
->NbEq
; l1
< P
->NbConstraints
; ++l1
) {
2200 if (value_negz_p(P
->Constraint
[l1
][nvar
+i
+1]))
2202 for (l2
= l1
+ 1; l2
< P
->NbConstraints
; ++l2
) {
2203 if (value_negz_p(P
->Constraint
[l2
][nvar
+i
+1]))
2205 if (double_bound_pair(P
, nvar
, exist
, i
, l1
, l2
, pos
, neg
))
2209 for (l1
= P
->NbEq
; l1
< P
->NbConstraints
; ++l1
) {
2210 if (value_posz_p(P
->Constraint
[l1
][nvar
+i
+1]))
2212 if (l1
< P
->NbConstraints
)
2213 for (l2
= l1
+ 1; l2
< P
->NbConstraints
; ++l2
) {
2214 if (value_posz_p(P
->Constraint
[l2
][nvar
+i
+1]))
2216 if (double_bound_pair(P
, nvar
, exist
, i
, l1
, l2
, pos
, neg
))
2228 INDEPENDENT
= 1 << 2,
2232 static evalue
* enumerate_or(Polyhedron
*D
,
2233 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2236 fprintf(stderr
, "\nER: Or\n");
2237 #endif /* DEBUG_ER */
2239 Polyhedron
*N
= D
->next
;
2242 barvinok_enumerate_e_with_options(D
, exist
, nparam
, options
);
2245 for (D
= N
; D
; D
= N
) {
2250 barvinok_enumerate_e_with_options(D
, exist
, nparam
, options
);
2253 free_evalue_refs(EN
);
2263 static evalue
* enumerate_sum(Polyhedron
*P
,
2264 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2266 int nvar
= P
->Dimension
- exist
- nparam
;
2267 int toswap
= nvar
< exist
? nvar
: exist
;
2268 for (int i
= 0; i
< toswap
; ++i
)
2269 SwapColumns(P
, 1 + i
, nvar
+exist
- i
);
2273 fprintf(stderr
, "\nER: Sum\n");
2274 #endif /* DEBUG_ER */
2276 evalue
*EP
= barvinok_enumerate_e_with_options(P
, exist
, nparam
, options
);
2278 for (int i
= 0; i
< /* nvar */ nparam
; ++i
) {
2279 Matrix
*C
= Matrix_Alloc(1, 1 + nparam
+ 1);
2280 value_set_si(C
->p
[0][0], 1);
2282 value_init(split
.d
);
2283 value_set_si(split
.d
, 0);
2284 split
.x
.p
= new_enode(partition
, 4, nparam
);
2285 value_set_si(C
->p
[0][1+i
], 1);
2286 Matrix
*C2
= Matrix_Copy(C
);
2287 EVALUE_SET_DOMAIN(split
.x
.p
->arr
[0],
2288 Constraints2Polyhedron(C2
, options
->MaxRays
));
2290 evalue_set_si(&split
.x
.p
->arr
[1], 1, 1);
2291 value_set_si(C
->p
[0][1+i
], -1);
2292 value_set_si(C
->p
[0][1+nparam
], -1);
2293 EVALUE_SET_DOMAIN(split
.x
.p
->arr
[2],
2294 Constraints2Polyhedron(C
, options
->MaxRays
));
2295 evalue_set_si(&split
.x
.p
->arr
[3], 1, 1);
2297 free_evalue_refs(&split
);
2301 evalue_range_reduction(EP
);
2303 evalue_frac2floor2(EP
, 1);
2305 evalue
*sum
= esum(EP
, nvar
);
2307 free_evalue_refs(EP
);
2311 evalue_range_reduction(EP
);
2316 static evalue
* split_sure(Polyhedron
*P
, Polyhedron
*S
,
2317 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2319 int nvar
= P
->Dimension
- exist
- nparam
;
2321 Matrix
*M
= Matrix_Alloc(exist
, S
->Dimension
+2);
2322 for (int i
= 0; i
< exist
; ++i
)
2323 value_set_si(M
->p
[i
][nvar
+i
+1], 1);
2325 S
= DomainAddRays(S
, M
, options
->MaxRays
);
2327 Polyhedron
*F
= DomainAddRays(P
, M
, options
->MaxRays
);
2328 Polyhedron
*D
= DomainDifference(F
, S
, options
->MaxRays
);
2330 D
= Disjoint_Domain(D
, 0, options
->MaxRays
);
2335 M
= Matrix_Alloc(P
->Dimension
+1-exist
, P
->Dimension
+1);
2336 for (int j
= 0; j
< nvar
; ++j
)
2337 value_set_si(M
->p
[j
][j
], 1);
2338 for (int j
= 0; j
< nparam
+1; ++j
)
2339 value_set_si(M
->p
[nvar
+j
][nvar
+exist
+j
], 1);
2340 Polyhedron
*T
= Polyhedron_Image(S
, M
, options
->MaxRays
);
2341 evalue
*EP
= barvinok_enumerate_e_with_options(T
, 0, nparam
, options
);
2346 for (Polyhedron
*Q
= D
; Q
; Q
= Q
->next
) {
2347 Polyhedron
*N
= Q
->next
;
2349 T
= DomainIntersection(P
, Q
, options
->MaxRays
);
2350 evalue
*E
= barvinok_enumerate_e_with_options(T
, exist
, nparam
, options
);
2352 free_evalue_refs(E
);
2361 static evalue
* enumerate_sure(Polyhedron
*P
,
2362 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2366 int nvar
= P
->Dimension
- exist
- nparam
;
2372 for (i
= 0; i
< exist
; ++i
) {
2373 Matrix
*M
= Matrix_Alloc(S
->NbConstraints
, S
->Dimension
+2);
2375 value_set_si(lcm
, 1);
2376 for (int j
= 0; j
< S
->NbConstraints
; ++j
) {
2377 if (value_negz_p(S
->Constraint
[j
][1+nvar
+i
]))
2379 if (value_one_p(S
->Constraint
[j
][1+nvar
+i
]))
2381 value_lcm(lcm
, S
->Constraint
[j
][1+nvar
+i
], &lcm
);
2384 for (int j
= 0; j
< S
->NbConstraints
; ++j
) {
2385 if (value_negz_p(S
->Constraint
[j
][1+nvar
+i
]))
2387 if (value_one_p(S
->Constraint
[j
][1+nvar
+i
]))
2389 value_division(f
, lcm
, S
->Constraint
[j
][1+nvar
+i
]);
2390 Vector_Scale(S
->Constraint
[j
], M
->p
[c
], f
, S
->Dimension
+2);
2391 value_subtract(M
->p
[c
][S
->Dimension
+1],
2392 M
->p
[c
][S
->Dimension
+1],
2394 value_increment(M
->p
[c
][S
->Dimension
+1],
2395 M
->p
[c
][S
->Dimension
+1]);
2399 S
= AddConstraints(M
->p
[0], c
, S
, options
->MaxRays
);
2414 fprintf(stderr
, "\nER: Sure\n");
2415 #endif /* DEBUG_ER */
2417 return split_sure(P
, S
, exist
, nparam
, options
);
2420 static evalue
* enumerate_sure2(Polyhedron
*P
,
2421 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2423 int nvar
= P
->Dimension
- exist
- nparam
;
2425 for (r
= 0; r
< P
->NbRays
; ++r
)
2426 if (value_one_p(P
->Ray
[r
][0]) &&
2427 value_one_p(P
->Ray
[r
][P
->Dimension
+1]))
2433 Matrix
*M
= Matrix_Alloc(nvar
+ 1 + nparam
, P
->Dimension
+2);
2434 for (int i
= 0; i
< nvar
; ++i
)
2435 value_set_si(M
->p
[i
][1+i
], 1);
2436 for (int i
= 0; i
< nparam
; ++i
)
2437 value_set_si(M
->p
[i
+nvar
][1+nvar
+exist
+i
], 1);
2438 Vector_Copy(P
->Ray
[r
]+1+nvar
, M
->p
[nvar
+nparam
]+1+nvar
, exist
);
2439 value_set_si(M
->p
[nvar
+nparam
][0], 1);
2440 value_set_si(M
->p
[nvar
+nparam
][P
->Dimension
+1], 1);
2441 Polyhedron
* F
= Rays2Polyhedron(M
, options
->MaxRays
);
2444 Polyhedron
*I
= DomainIntersection(F
, P
, options
->MaxRays
);
2448 fprintf(stderr
, "\nER: Sure2\n");
2449 #endif /* DEBUG_ER */
2451 return split_sure(P
, I
, exist
, nparam
, options
);
2454 static evalue
* enumerate_cyclic(Polyhedron
*P
,
2455 unsigned exist
, unsigned nparam
,
2456 evalue
* EP
, int r
, int p
, unsigned MaxRays
)
2458 int nvar
= P
->Dimension
- exist
- nparam
;
2460 /* If EP in its fractional maps only contains references
2461 * to the remainder parameter with appropriate coefficients
2462 * then we could in principle avoid adding existentially
2463 * quantified variables to the validity domains.
2464 * We'd have to replace the remainder by m { p/m }
2465 * and multiply with an appropriate factor that is one
2466 * only in the appropriate range.
2467 * This last multiplication can be avoided if EP
2468 * has a single validity domain with no (further)
2469 * constraints on the remainder parameter
2472 Matrix
*CT
= Matrix_Alloc(nparam
+1, nparam
+3);
2473 Matrix
*M
= Matrix_Alloc(1, 1+nparam
+3);
2474 for (int j
= 0; j
< nparam
; ++j
)
2476 value_set_si(CT
->p
[j
][j
], 1);
2477 value_set_si(CT
->p
[p
][nparam
+1], 1);
2478 value_set_si(CT
->p
[nparam
][nparam
+2], 1);
2479 value_set_si(M
->p
[0][1+p
], -1);
2480 value_absolute(M
->p
[0][1+nparam
], P
->Ray
[0][1+nvar
+exist
+p
]);
2481 value_set_si(M
->p
[0][1+nparam
+1], 1);
2482 Polyhedron
*CEq
= Constraints2Polyhedron(M
, 1);
2484 addeliminatedparams_enum(EP
, CT
, CEq
, MaxRays
, nparam
);
2485 Polyhedron_Free(CEq
);
2491 static void enumerate_vd_add_ray(evalue
*EP
, Matrix
*Rays
, unsigned MaxRays
)
2493 if (value_notzero_p(EP
->d
))
2496 assert(EP
->x
.p
->type
== partition
);
2497 assert(EP
->x
.p
->pos
== EVALUE_DOMAIN(EP
->x
.p
->arr
[0])->Dimension
);
2498 for (int i
= 0; i
< EP
->x
.p
->size
/2; ++i
) {
2499 Polyhedron
*D
= EVALUE_DOMAIN(EP
->x
.p
->arr
[2*i
]);
2500 Polyhedron
*N
= DomainAddRays(D
, Rays
, MaxRays
);
2501 EVALUE_SET_DOMAIN(EP
->x
.p
->arr
[2*i
], N
);
2506 static evalue
* enumerate_line(Polyhedron
*P
,
2507 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2513 fprintf(stderr
, "\nER: Line\n");
2514 #endif /* DEBUG_ER */
2516 int nvar
= P
->Dimension
- exist
- nparam
;
2518 for (i
= 0; i
< nparam
; ++i
)
2519 if (value_notzero_p(P
->Ray
[0][1+nvar
+exist
+i
]))
2522 for (j
= i
+1; j
< nparam
; ++j
)
2523 if (value_notzero_p(P
->Ray
[0][1+nvar
+exist
+i
]))
2525 assert(j
>= nparam
); // for now
2527 Matrix
*M
= Matrix_Alloc(2, P
->Dimension
+2);
2528 value_set_si(M
->p
[0][0], 1);
2529 value_set_si(M
->p
[0][1+nvar
+exist
+i
], 1);
2530 value_set_si(M
->p
[1][0], 1);
2531 value_set_si(M
->p
[1][1+nvar
+exist
+i
], -1);
2532 value_absolute(M
->p
[1][1+P
->Dimension
], P
->Ray
[0][1+nvar
+exist
+i
]);
2533 value_decrement(M
->p
[1][1+P
->Dimension
], M
->p
[1][1+P
->Dimension
]);
2534 Polyhedron
*S
= AddConstraints(M
->p
[0], 2, P
, options
->MaxRays
);
2535 evalue
*EP
= barvinok_enumerate_e_with_options(S
, exist
, nparam
, options
);
2539 return enumerate_cyclic(P
, exist
, nparam
, EP
, 0, i
, options
->MaxRays
);
2542 static int single_param_pos(Polyhedron
*P
, unsigned exist
, unsigned nparam
,
2545 int nvar
= P
->Dimension
- exist
- nparam
;
2546 if (First_Non_Zero(P
->Ray
[r
]+1, nvar
) != -1)
2548 int i
= First_Non_Zero(P
->Ray
[r
]+1+nvar
+exist
, nparam
);
2551 if (First_Non_Zero(P
->Ray
[r
]+1+nvar
+exist
+1, nparam
-i
-1) != -1)
2556 static evalue
* enumerate_remove_ray(Polyhedron
*P
, int r
,
2557 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2560 fprintf(stderr
, "\nER: RedundantRay\n");
2561 #endif /* DEBUG_ER */
2565 value_set_si(one
, 1);
2566 int len
= P
->NbRays
-1;
2567 Matrix
*M
= Matrix_Alloc(2 * len
, P
->Dimension
+2);
2568 Vector_Copy(P
->Ray
[0], M
->p
[0], r
* (P
->Dimension
+2));
2569 Vector_Copy(P
->Ray
[r
+1], M
->p
[r
], (len
-r
) * (P
->Dimension
+2));
2570 for (int j
= 0; j
< P
->NbRays
; ++j
) {
2573 Vector_Combine(P
->Ray
[j
], P
->Ray
[r
], M
->p
[len
+j
-(j
>r
)],
2574 one
, P
->Ray
[j
][P
->Dimension
+1], P
->Dimension
+2);
2577 P
= Rays2Polyhedron(M
, options
->MaxRays
);
2579 evalue
*EP
= barvinok_enumerate_e_with_options(P
, exist
, nparam
, options
);
2586 static evalue
* enumerate_redundant_ray(Polyhedron
*P
,
2587 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2589 assert(P
->NbBid
== 0);
2590 int nvar
= P
->Dimension
- exist
- nparam
;
2594 for (int r
= 0; r
< P
->NbRays
; ++r
) {
2595 if (value_notzero_p(P
->Ray
[r
][P
->Dimension
+1]))
2597 int i1
= single_param_pos(P
, exist
, nparam
, r
);
2600 for (int r2
= r
+1; r2
< P
->NbRays
; ++r2
) {
2601 if (value_notzero_p(P
->Ray
[r2
][P
->Dimension
+1]))
2603 int i2
= single_param_pos(P
, exist
, nparam
, r2
);
2609 value_division(m
, P
->Ray
[r
][1+nvar
+exist
+i1
],
2610 P
->Ray
[r2
][1+nvar
+exist
+i1
]);
2611 value_multiply(m
, m
, P
->Ray
[r2
][1+nvar
+exist
+i1
]);
2612 /* r2 divides r => r redundant */
2613 if (value_eq(m
, P
->Ray
[r
][1+nvar
+exist
+i1
])) {
2615 return enumerate_remove_ray(P
, r
, exist
, nparam
, options
);
2618 value_division(m
, P
->Ray
[r2
][1+nvar
+exist
+i1
],
2619 P
->Ray
[r
][1+nvar
+exist
+i1
]);
2620 value_multiply(m
, m
, P
->Ray
[r
][1+nvar
+exist
+i1
]);
2621 /* r divides r2 => r2 redundant */
2622 if (value_eq(m
, P
->Ray
[r2
][1+nvar
+exist
+i1
])) {
2624 return enumerate_remove_ray(P
, r2
, exist
, nparam
, options
);
2632 static Polyhedron
*upper_bound(Polyhedron
*P
,
2633 int pos
, Value
*max
, Polyhedron
**R
)
2642 for (Polyhedron
*Q
= P
; Q
; Q
= N
) {
2644 for (r
= 0; r
< P
->NbRays
; ++r
) {
2645 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1]) &&
2646 value_pos_p(P
->Ray
[r
][1+pos
]))
2649 if (r
< P
->NbRays
) {
2657 for (r
= 0; r
< P
->NbRays
; ++r
) {
2658 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1]))
2660 mpz_fdiv_q(v
, P
->Ray
[r
][1+pos
], P
->Ray
[r
][1+P
->Dimension
]);
2661 if ((!Q
->next
&& r
== 0) || value_gt(v
, *max
))
2662 value_assign(*max
, v
);
2669 static evalue
* enumerate_ray(Polyhedron
*P
,
2670 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2672 assert(P
->NbBid
== 0);
2673 int nvar
= P
->Dimension
- exist
- nparam
;
2676 for (r
= 0; r
< P
->NbRays
; ++r
)
2677 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1]))
2683 for (r2
= r
+1; r2
< P
->NbRays
; ++r2
)
2684 if (value_zero_p(P
->Ray
[r2
][P
->Dimension
+1]))
2686 if (r2
< P
->NbRays
) {
2688 return enumerate_sum(P
, exist
, nparam
, options
);
2692 fprintf(stderr
, "\nER: Ray\n");
2693 #endif /* DEBUG_ER */
2699 value_set_si(one
, 1);
2700 int i
= single_param_pos(P
, exist
, nparam
, r
);
2701 assert(i
!= -1); // for now;
2703 Matrix
*M
= Matrix_Alloc(P
->NbRays
, P
->Dimension
+2);
2704 for (int j
= 0; j
< P
->NbRays
; ++j
) {
2705 Vector_Combine(P
->Ray
[j
], P
->Ray
[r
], M
->p
[j
],
2706 one
, P
->Ray
[j
][P
->Dimension
+1], P
->Dimension
+2);
2708 Polyhedron
*S
= Rays2Polyhedron(M
, options
->MaxRays
);
2710 Polyhedron
*D
= DomainDifference(P
, S
, options
->MaxRays
);
2712 // Polyhedron_Print(stderr, P_VALUE_FMT, D);
2713 assert(value_pos_p(P
->Ray
[r
][1+nvar
+exist
+i
])); // for now
2715 D
= upper_bound(D
, nvar
+exist
+i
, &m
, &R
);
2719 M
= Matrix_Alloc(2, P
->Dimension
+2);
2720 value_set_si(M
->p
[0][0], 1);
2721 value_set_si(M
->p
[1][0], 1);
2722 value_set_si(M
->p
[0][1+nvar
+exist
+i
], -1);
2723 value_set_si(M
->p
[1][1+nvar
+exist
+i
], 1);
2724 value_assign(M
->p
[0][1+P
->Dimension
], m
);
2725 value_oppose(M
->p
[1][1+P
->Dimension
], m
);
2726 value_addto(M
->p
[1][1+P
->Dimension
], M
->p
[1][1+P
->Dimension
],
2727 P
->Ray
[r
][1+nvar
+exist
+i
]);
2728 value_decrement(M
->p
[1][1+P
->Dimension
], M
->p
[1][1+P
->Dimension
]);
2729 // Matrix_Print(stderr, P_VALUE_FMT, M);
2730 D
= AddConstraints(M
->p
[0], 2, P
, options
->MaxRays
);
2731 // Polyhedron_Print(stderr, P_VALUE_FMT, D);
2732 value_subtract(M
->p
[0][1+P
->Dimension
], M
->p
[0][1+P
->Dimension
],
2733 P
->Ray
[r
][1+nvar
+exist
+i
]);
2734 // Matrix_Print(stderr, P_VALUE_FMT, M);
2735 S
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
2736 // Polyhedron_Print(stderr, P_VALUE_FMT, S);
2739 evalue
*EP
= barvinok_enumerate_e_with_options(D
, exist
, nparam
, options
);
2744 if (value_notone_p(P
->Ray
[r
][1+nvar
+exist
+i
]))
2745 EP
= enumerate_cyclic(P
, exist
, nparam
, EP
, r
, i
, options
->MaxRays
);
2747 M
= Matrix_Alloc(1, nparam
+2);
2748 value_set_si(M
->p
[0][0], 1);
2749 value_set_si(M
->p
[0][1+i
], 1);
2750 enumerate_vd_add_ray(EP
, M
, options
->MaxRays
);
2755 evalue
*E
= barvinok_enumerate_e_with_options(S
, exist
, nparam
, options
);
2757 free_evalue_refs(E
);
2764 evalue
*ER
= enumerate_or(R
, exist
, nparam
, options
);
2766 free_evalue_refs(ER
);
2773 static evalue
* enumerate_vd(Polyhedron
**PA
,
2774 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2776 Polyhedron
*P
= *PA
;
2777 int nvar
= P
->Dimension
- exist
- nparam
;
2778 Param_Polyhedron
*PP
= NULL
;
2779 Polyhedron
*C
= Universe_Polyhedron(nparam
);
2783 PP
= Polyhedron2Param_SimplifiedDomain(&PR
,C
, options
->MaxRays
,&CEq
,&CT
);
2787 Param_Domain
*D
, *last
;
2790 for (nd
= 0, D
=PP
->D
; D
; D
=D
->next
, ++nd
)
2793 Polyhedron
**VD
= new Polyhedron_p
[nd
];
2794 Polyhedron
**fVD
= new Polyhedron_p
[nd
];
2795 for(nd
= 0, D
=PP
->D
; D
; D
=D
->next
) {
2796 Polyhedron
*rVD
= reduce_domain(D
->Domain
, CT
, CEq
,
2797 fVD
, nd
, options
->MaxRays
);
2810 /* This doesn't seem to have any effect */
2812 Polyhedron
*CA
= align_context(VD
[0], P
->Dimension
, options
->MaxRays
);
2814 P
= DomainIntersection(P
, CA
, options
->MaxRays
);
2817 Polyhedron_Free(CA
);
2822 if (!EP
&& CT
->NbColumns
!= CT
->NbRows
) {
2823 Polyhedron
*CEqr
= DomainImage(CEq
, CT
, options
->MaxRays
);
2824 Polyhedron
*CA
= align_context(CEqr
, PR
->Dimension
, options
->MaxRays
);
2825 Polyhedron
*I
= DomainIntersection(PR
, CA
, options
->MaxRays
);
2826 Polyhedron_Free(CEqr
);
2827 Polyhedron_Free(CA
);
2829 fprintf(stderr
, "\nER: Eliminate\n");
2830 #endif /* DEBUG_ER */
2831 nparam
-= CT
->NbColumns
- CT
->NbRows
;
2832 EP
= barvinok_enumerate_e_with_options(I
, exist
, nparam
, options
);
2833 nparam
+= CT
->NbColumns
- CT
->NbRows
;
2834 addeliminatedparams_enum(EP
, CT
, CEq
, options
->MaxRays
, nparam
);
2838 Polyhedron_Free(PR
);
2841 if (!EP
&& nd
> 1) {
2843 fprintf(stderr
, "\nER: VD\n");
2844 #endif /* DEBUG_ER */
2845 for (int i
= 0; i
< nd
; ++i
) {
2846 Polyhedron
*CA
= align_context(VD
[i
], P
->Dimension
, options
->MaxRays
);
2847 Polyhedron
*I
= DomainIntersection(P
, CA
, options
->MaxRays
);
2850 EP
= barvinok_enumerate_e_with_options(I
, exist
, nparam
, options
);
2852 evalue
*E
= barvinok_enumerate_e_with_options(I
, exist
, nparam
,
2855 free_evalue_refs(E
);
2859 Polyhedron_Free(CA
);
2863 for (int i
= 0; i
< nd
; ++i
) {
2864 Polyhedron_Free(VD
[i
]);
2865 Polyhedron_Free(fVD
[i
]);
2871 if (!EP
&& nvar
== 0) {
2874 Param_Vertices
*V
, *V2
;
2875 Matrix
* M
= Matrix_Alloc(1, P
->Dimension
+2);
2877 FORALL_PVertex_in_ParamPolyhedron(V
, last
, PP
) {
2879 FORALL_PVertex_in_ParamPolyhedron(V2
, last
, PP
) {
2886 for (int i
= 0; i
< exist
; ++i
) {
2887 value_oppose(f
, V
->Vertex
->p
[i
][nparam
+1]);
2888 Vector_Combine(V
->Vertex
->p
[i
],
2890 M
->p
[0] + 1 + nvar
+ exist
,
2891 V2
->Vertex
->p
[i
][nparam
+1],
2895 for (j
= 0; j
< nparam
; ++j
)
2896 if (value_notzero_p(M
->p
[0][1+nvar
+exist
+j
]))
2900 ConstraintSimplify(M
->p
[0], M
->p
[0],
2901 P
->Dimension
+2, &f
);
2902 value_set_si(M
->p
[0][0], 0);
2903 Polyhedron
*para
= AddConstraints(M
->p
[0], 1, P
,
2906 Polyhedron_Free(para
);
2909 Polyhedron
*pos
, *neg
;
2910 value_set_si(M
->p
[0][0], 1);
2911 value_decrement(M
->p
[0][P
->Dimension
+1],
2912 M
->p
[0][P
->Dimension
+1]);
2913 neg
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
2914 value_set_si(f
, -1);
2915 Vector_Scale(M
->p
[0]+1, M
->p
[0]+1, f
,
2917 value_decrement(M
->p
[0][P
->Dimension
+1],
2918 M
->p
[0][P
->Dimension
+1]);
2919 value_decrement(M
->p
[0][P
->Dimension
+1],
2920 M
->p
[0][P
->Dimension
+1]);
2921 pos
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
2922 if (emptyQ(neg
) && emptyQ(pos
)) {
2923 Polyhedron_Free(para
);
2924 Polyhedron_Free(pos
);
2925 Polyhedron_Free(neg
);
2929 fprintf(stderr
, "\nER: Order\n");
2930 #endif /* DEBUG_ER */
2931 EP
= barvinok_enumerate_e_with_options(para
, exist
, nparam
,
2935 E
= barvinok_enumerate_e_with_options(pos
, exist
, nparam
,
2938 free_evalue_refs(E
);
2942 E
= barvinok_enumerate_e_with_options(neg
, exist
, nparam
,
2945 free_evalue_refs(E
);
2948 Polyhedron_Free(para
);
2949 Polyhedron_Free(pos
);
2950 Polyhedron_Free(neg
);
2955 } END_FORALL_PVertex_in_ParamPolyhedron
;
2958 } END_FORALL_PVertex_in_ParamPolyhedron
;
2961 /* Search for vertex coordinate to split on */
2962 /* First look for one independent of the parameters */
2963 FORALL_PVertex_in_ParamPolyhedron(V
, last
, PP
) {
2964 for (int i
= 0; i
< exist
; ++i
) {
2966 for (j
= 0; j
< nparam
; ++j
)
2967 if (value_notzero_p(V
->Vertex
->p
[i
][j
]))
2971 value_set_si(M
->p
[0][0], 1);
2972 Vector_Set(M
->p
[0]+1, 0, nvar
+exist
);
2973 Vector_Copy(V
->Vertex
->p
[i
],
2974 M
->p
[0] + 1 + nvar
+ exist
, nparam
+1);
2975 value_oppose(M
->p
[0][1+nvar
+i
],
2976 V
->Vertex
->p
[i
][nparam
+1]);
2978 Polyhedron
*pos
, *neg
;
2979 value_set_si(M
->p
[0][0], 1);
2980 value_decrement(M
->p
[0][P
->Dimension
+1],
2981 M
->p
[0][P
->Dimension
+1]);
2982 neg
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
2983 value_set_si(f
, -1);
2984 Vector_Scale(M
->p
[0]+1, M
->p
[0]+1, f
,
2986 value_decrement(M
->p
[0][P
->Dimension
+1],
2987 M
->p
[0][P
->Dimension
+1]);
2988 value_decrement(M
->p
[0][P
->Dimension
+1],
2989 M
->p
[0][P
->Dimension
+1]);
2990 pos
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
2991 if (emptyQ(neg
) || emptyQ(pos
)) {
2992 Polyhedron_Free(pos
);
2993 Polyhedron_Free(neg
);
2996 Polyhedron_Free(pos
);
2997 value_increment(M
->p
[0][P
->Dimension
+1],
2998 M
->p
[0][P
->Dimension
+1]);
2999 pos
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
3001 fprintf(stderr
, "\nER: Vertex\n");
3002 #endif /* DEBUG_ER */
3004 EP
= enumerate_or(pos
, exist
, nparam
, options
);
3009 } END_FORALL_PVertex_in_ParamPolyhedron
;
3013 /* Search for vertex coordinate to split on */
3014 /* Now look for one that depends on the parameters */
3015 FORALL_PVertex_in_ParamPolyhedron(V
, last
, PP
) {
3016 for (int i
= 0; i
< exist
; ++i
) {
3017 value_set_si(M
->p
[0][0], 1);
3018 Vector_Set(M
->p
[0]+1, 0, nvar
+exist
);
3019 Vector_Copy(V
->Vertex
->p
[i
],
3020 M
->p
[0] + 1 + nvar
+ exist
, nparam
+1);
3021 value_oppose(M
->p
[0][1+nvar
+i
],
3022 V
->Vertex
->p
[i
][nparam
+1]);
3024 Polyhedron
*pos
, *neg
;
3025 value_set_si(M
->p
[0][0], 1);
3026 value_decrement(M
->p
[0][P
->Dimension
+1],
3027 M
->p
[0][P
->Dimension
+1]);
3028 neg
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
3029 value_set_si(f
, -1);
3030 Vector_Scale(M
->p
[0]+1, M
->p
[0]+1, f
,
3032 value_decrement(M
->p
[0][P
->Dimension
+1],
3033 M
->p
[0][P
->Dimension
+1]);
3034 value_decrement(M
->p
[0][P
->Dimension
+1],
3035 M
->p
[0][P
->Dimension
+1]);
3036 pos
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
3037 if (emptyQ(neg
) || emptyQ(pos
)) {
3038 Polyhedron_Free(pos
);
3039 Polyhedron_Free(neg
);
3042 Polyhedron_Free(pos
);
3043 value_increment(M
->p
[0][P
->Dimension
+1],
3044 M
->p
[0][P
->Dimension
+1]);
3045 pos
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
3047 fprintf(stderr
, "\nER: ParamVertex\n");
3048 #endif /* DEBUG_ER */
3050 EP
= enumerate_or(pos
, exist
, nparam
, options
);
3055 } END_FORALL_PVertex_in_ParamPolyhedron
;
3063 Polyhedron_Free(CEq
);
3067 Param_Polyhedron_Free(PP
);
3074 evalue
*barvinok_enumerate_pip(Polyhedron
*P
,
3075 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
3080 evalue
*barvinok_enumerate_pip(Polyhedron
*P
,
3081 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
3083 int nvar
= P
->Dimension
- exist
- nparam
;
3084 evalue
*EP
= evalue_zero();
3088 fprintf(stderr
, "\nER: PIP\n");
3089 #endif /* DEBUG_ER */
3091 Polyhedron
*D
= pip_projectout(P
, nvar
, exist
, nparam
);
3092 for (Q
= D
; Q
; Q
= N
) {
3096 exist
= Q
->Dimension
- nvar
- nparam
;
3097 E
= barvinok_enumerate_e(Q
, exist
, nparam
, MaxRays
);
3100 free_evalue_refs(E
);
3109 static bool is_single(Value
*row
, int pos
, int len
)
3111 return First_Non_Zero(row
, pos
) == -1 &&
3112 First_Non_Zero(row
+pos
+1, len
-pos
-1) == -1;
3115 static evalue
* barvinok_enumerate_e_r(Polyhedron
*P
,
3116 unsigned exist
, unsigned nparam
, barvinok_options
*options
);
3119 static int er_level
= 0;
3121 evalue
* barvinok_enumerate_e_with_options(Polyhedron
*P
,
3122 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
3124 fprintf(stderr
, "\nER: level %i\n", er_level
);
3126 Polyhedron_PrintConstraints(stderr
, P_VALUE_FMT
, P
);
3127 fprintf(stderr
, "\nE %d\nP %d\n", exist
, nparam
);
3129 P
= DomainConstraintSimplify(Polyhedron_Copy(P
), options
->MaxRays
);
3130 evalue
*EP
= barvinok_enumerate_e_r(P
, exist
, nparam
, options
);
3136 evalue
* barvinok_enumerate_e_with_options(Polyhedron
*P
,
3137 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
3139 P
= DomainConstraintSimplify(Polyhedron_Copy(P
), options
->MaxRays
);
3140 evalue
*EP
= barvinok_enumerate_e_r(P
, exist
, nparam
, options
);
3146 evalue
* barvinok_enumerate_e(Polyhedron
*P
, unsigned exist
, unsigned nparam
,
3150 barvinok_options
*options
= barvinok_options_new_with_defaults();
3151 options
->MaxRays
= MaxRays
;
3152 E
= barvinok_enumerate_e_with_options(P
, exist
, nparam
, options
);
3157 static evalue
* barvinok_enumerate_e_r(Polyhedron
*P
,
3158 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
3161 Polyhedron
*U
= Universe_Polyhedron(nparam
);
3162 evalue
*EP
= barvinok_enumerate_with_options(P
, U
, options
);
3163 //char *param_name[] = {"P", "Q", "R", "S", "T" };
3164 //print_evalue(stdout, EP, param_name);
3169 int nvar
= P
->Dimension
- exist
- nparam
;
3170 int len
= P
->Dimension
+ 2;
3173 POL_ENSURE_FACETS(P
);
3174 POL_ENSURE_VERTICES(P
);
3177 return evalue_zero();
3179 if (nvar
== 0 && nparam
== 0) {
3180 evalue
*EP
= evalue_zero();
3181 barvinok_count_with_options(P
, &EP
->x
.n
, options
);
3182 if (value_pos_p(EP
->x
.n
))
3183 value_set_si(EP
->x
.n
, 1);
3188 for (r
= 0; r
< P
->NbRays
; ++r
)
3189 if (value_zero_p(P
->Ray
[r
][0]) ||
3190 value_zero_p(P
->Ray
[r
][P
->Dimension
+1])) {
3192 for (i
= 0; i
< nvar
; ++i
)
3193 if (value_notzero_p(P
->Ray
[r
][i
+1]))
3197 for (i
= nvar
+ exist
; i
< nvar
+ exist
+ nparam
; ++i
)
3198 if (value_notzero_p(P
->Ray
[r
][i
+1]))
3200 if (i
>= nvar
+ exist
+ nparam
)
3203 if (r
< P
->NbRays
) {
3204 evalue
*EP
= evalue_zero();
3205 value_set_si(EP
->x
.n
, -1);
3210 for (r
= 0; r
< P
->NbEq
; ++r
)
3211 if ((first
= First_Non_Zero(P
->Constraint
[r
]+1+nvar
, exist
)) != -1)
3214 if (First_Non_Zero(P
->Constraint
[r
]+1+nvar
+first
+1,
3215 exist
-first
-1) != -1) {
3216 Polyhedron
*T
= rotate_along(P
, r
, nvar
, exist
, options
->MaxRays
);
3218 fprintf(stderr
, "\nER: Equality\n");
3219 #endif /* DEBUG_ER */
3220 evalue
*EP
= barvinok_enumerate_e_with_options(T
, exist
-1, nparam
,
3226 fprintf(stderr
, "\nER: Fixed\n");
3227 #endif /* DEBUG_ER */
3229 return barvinok_enumerate_e_with_options(P
, exist
-1, nparam
,
3232 Polyhedron
*T
= Polyhedron_Copy(P
);
3233 SwapColumns(T
, nvar
+1, nvar
+1+first
);
3234 evalue
*EP
= barvinok_enumerate_e_with_options(T
, exist
-1, nparam
,
3242 Vector
*row
= Vector_Alloc(len
);
3243 value_set_si(row
->p
[0], 1);
3248 enum constraint
* info
= new constraint
[exist
];
3249 for (int i
= 0; i
< exist
; ++i
) {
3251 for (int l
= P
->NbEq
; l
< P
->NbConstraints
; ++l
) {
3252 if (value_negz_p(P
->Constraint
[l
][nvar
+i
+1]))
3254 bool l_parallel
= is_single(P
->Constraint
[l
]+nvar
+1, i
, exist
);
3255 for (int u
= P
->NbEq
; u
< P
->NbConstraints
; ++u
) {
3256 if (value_posz_p(P
->Constraint
[u
][nvar
+i
+1]))
3258 bool lu_parallel
= l_parallel
||
3259 is_single(P
->Constraint
[u
]+nvar
+1, i
, exist
);
3260 value_oppose(f
, P
->Constraint
[u
][nvar
+i
+1]);
3261 Vector_Combine(P
->Constraint
[l
]+1, P
->Constraint
[u
]+1, row
->p
+1,
3262 f
, P
->Constraint
[l
][nvar
+i
+1], len
-1);
3263 if (!(info
[i
] & INDEPENDENT
)) {
3265 for (j
= 0; j
< exist
; ++j
)
3266 if (j
!= i
&& value_notzero_p(row
->p
[nvar
+j
+1]))
3269 //printf("independent: i: %d, l: %d, u: %d\n", i, l, u);
3270 info
[i
] = (constraint
)(info
[i
] | INDEPENDENT
);
3273 if (info
[i
] & ALL_POS
) {
3274 value_addto(row
->p
[len
-1], row
->p
[len
-1],
3275 P
->Constraint
[l
][nvar
+i
+1]);
3276 value_addto(row
->p
[len
-1], row
->p
[len
-1], f
);
3277 value_multiply(f
, f
, P
->Constraint
[l
][nvar
+i
+1]);
3278 value_subtract(row
->p
[len
-1], row
->p
[len
-1], f
);
3279 value_decrement(row
->p
[len
-1], row
->p
[len
-1]);
3280 ConstraintSimplify(row
->p
, row
->p
, len
, &f
);
3281 value_set_si(f
, -1);
3282 Vector_Scale(row
->p
+1, row
->p
+1, f
, len
-1);
3283 value_decrement(row
->p
[len
-1], row
->p
[len
-1]);
3284 Polyhedron
*T
= AddConstraints(row
->p
, 1, P
, options
->MaxRays
);
3286 //printf("not all_pos: i: %d, l: %d, u: %d\n", i, l, u);
3287 info
[i
] = (constraint
)(info
[i
] ^ ALL_POS
);
3289 //puts("pos remainder");
3290 //Polyhedron_Print(stdout, P_VALUE_FMT, T);
3293 if (!(info
[i
] & ONE_NEG
)) {
3295 negative_test_constraint(P
->Constraint
[l
],
3297 row
->p
, nvar
+i
, len
, &f
);
3298 oppose_constraint(row
->p
, len
, &f
);
3299 Polyhedron
*T
= AddConstraints(row
->p
, 1, P
,
3302 //printf("one_neg i: %d, l: %d, u: %d\n", i, l, u);
3303 info
[i
] = (constraint
)(info
[i
] | ONE_NEG
);
3305 //puts("neg remainder");
3306 //Polyhedron_Print(stdout, P_VALUE_FMT, T);
3308 } else if (!(info
[i
] & ROT_NEG
)) {
3309 if (parallel_constraints(P
->Constraint
[l
],
3311 row
->p
, nvar
, exist
)) {
3312 negative_test_constraint7(P
->Constraint
[l
],
3314 row
->p
, nvar
, exist
,
3316 oppose_constraint(row
->p
, len
, &f
);
3317 Polyhedron
*T
= AddConstraints(row
->p
, 1, P
,
3320 // printf("rot_neg i: %d, l: %d, u: %d\n", i, l, u);
3321 info
[i
] = (constraint
)(info
[i
] | ROT_NEG
);
3324 //puts("neg remainder");
3325 //Polyhedron_Print(stdout, P_VALUE_FMT, T);
3330 if (!(info
[i
] & ALL_POS
) && (info
[i
] & (ONE_NEG
| ROT_NEG
)))
3334 if (info
[i
] & ALL_POS
)
3341 for (int i = 0; i < exist; ++i)
3342 printf("%i: %i\n", i, info[i]);
3344 for (int i
= 0; i
< exist
; ++i
)
3345 if (info
[i
] & ALL_POS
) {
3347 fprintf(stderr
, "\nER: Positive\n");
3348 #endif /* DEBUG_ER */
3350 // Maybe we should chew off some of the fat here
3351 Matrix
*M
= Matrix_Alloc(P
->Dimension
, P
->Dimension
+1);
3352 for (int j
= 0; j
< P
->Dimension
; ++j
)
3353 value_set_si(M
->p
[j
][j
+ (j
>= i
+nvar
)], 1);
3354 Polyhedron
*T
= Polyhedron_Image(P
, M
, options
->MaxRays
);
3356 evalue
*EP
= barvinok_enumerate_e_with_options(T
, exist
-1, nparam
,
3364 for (int i
= 0; i
< exist
; ++i
)
3365 if (info
[i
] & ONE_NEG
) {
3367 fprintf(stderr
, "\nER: Negative\n");
3368 #endif /* DEBUG_ER */
3373 return barvinok_enumerate_e_with_options(P
, exist
-1, nparam
,
3376 Polyhedron
*T
= Polyhedron_Copy(P
);
3377 SwapColumns(T
, nvar
+1, nvar
+1+i
);
3378 evalue
*EP
= barvinok_enumerate_e_with_options(T
, exist
-1, nparam
,
3384 for (int i
= 0; i
< exist
; ++i
)
3385 if (info
[i
] & ROT_NEG
) {
3387 fprintf(stderr
, "\nER: Rotate\n");
3388 #endif /* DEBUG_ER */
3392 Polyhedron
*T
= rotate_along(P
, r
, nvar
, exist
, options
->MaxRays
);
3393 evalue
*EP
= barvinok_enumerate_e_with_options(T
, exist
-1, nparam
,
3398 for (int i
= 0; i
< exist
; ++i
)
3399 if (info
[i
] & INDEPENDENT
) {
3400 Polyhedron
*pos
, *neg
;
3402 /* Find constraint again and split off negative part */
3404 if (SplitOnVar(P
, i
, nvar
, exist
, options
->MaxRays
,
3405 row
, f
, true, &pos
, &neg
)) {
3407 fprintf(stderr
, "\nER: Split\n");
3408 #endif /* DEBUG_ER */
3411 barvinok_enumerate_e_with_options(neg
, exist
-1, nparam
, options
);
3413 barvinok_enumerate_e_with_options(pos
, exist
, nparam
, options
);
3415 free_evalue_refs(E
);
3417 Polyhedron_Free(neg
);
3418 Polyhedron_Free(pos
);
3432 EP
= enumerate_line(P
, exist
, nparam
, options
);
3436 EP
= barvinok_enumerate_pip(P
, exist
, nparam
, options
->MaxRays
);
3440 EP
= enumerate_redundant_ray(P
, exist
, nparam
, options
);
3444 EP
= enumerate_sure(P
, exist
, nparam
, options
);
3448 EP
= enumerate_ray(P
, exist
, nparam
, options
);
3452 EP
= enumerate_sure2(P
, exist
, nparam
, options
);
3456 F
= unfringe(P
, options
->MaxRays
);
3457 if (!PolyhedronIncludes(F
, P
)) {
3459 fprintf(stderr
, "\nER: Fringed\n");
3460 #endif /* DEBUG_ER */
3461 EP
= barvinok_enumerate_e_with_options(F
, exist
, nparam
, options
);
3468 EP
= enumerate_vd(&P
, exist
, nparam
, options
);
3473 EP
= enumerate_sum(P
, exist
, nparam
, options
);
3480 Polyhedron
*pos
, *neg
;
3481 for (i
= 0; i
< exist
; ++i
)
3482 if (SplitOnVar(P
, i
, nvar
, exist
, options
->MaxRays
,
3483 row
, f
, false, &pos
, &neg
))
3489 EP
= enumerate_or(pos
, exist
, nparam
, options
);
3502 * remove equalities that require a "compression" of the parameters
3504 static Polyhedron
*remove_more_equalities(Polyhedron
*P
, unsigned nparam
,
3505 Matrix
**CP
, unsigned MaxRays
)
3508 remove_all_equalities(&P
, NULL
, CP
, NULL
, nparam
, MaxRays
);
3515 static gen_fun
*series(Polyhedron
*P
, unsigned nparam
, barvinok_options
*options
)
3525 assert(!Polyhedron_is_infinite_param(P
, nparam
));
3526 assert(P
->NbBid
== 0);
3527 assert(Polyhedron_has_positive_rays(P
, nparam
));
3529 P
= remove_more_equalities(P
, nparam
, &CP
, options
->MaxRays
);
3530 assert(P
->NbEq
== 0);
3532 nparam
= CP
->NbColumns
-1;
3537 barvinok_count(P
, &c
, options
->MaxRays
);
3538 gf
= new gen_fun(c
);
3542 red
= gf_base::create(Polyhedron_Project(P
, nparam
),
3543 P
->Dimension
, nparam
, options
);
3544 POL_ENSURE_VERTICES(P
);
3545 red
->start_gf(P
, options
);
3557 gen_fun
* barvinok_series_with_options(Polyhedron
*P
, Polyhedron
* C
,
3558 barvinok_options
*options
)
3561 unsigned nparam
= C
->Dimension
;
3564 CA
= align_context(C
, P
->Dimension
, options
->MaxRays
);
3565 P
= DomainIntersection(P
, CA
, options
->MaxRays
);
3566 Polyhedron_Free(CA
);
3568 gf
= series(P
, nparam
, options
);
3573 gen_fun
* barvinok_series(Polyhedron
*P
, Polyhedron
* C
, unsigned MaxRays
)
3576 barvinok_options
*options
= barvinok_options_new_with_defaults();
3577 options
->MaxRays
= MaxRays
;
3578 gf
= barvinok_series_with_options(P
, C
, options
);
3583 static Polyhedron
*skew_into_positive_orthant(Polyhedron
*D
, unsigned nparam
,
3589 for (Polyhedron
*P
= D
; P
; P
= P
->next
) {
3590 POL_ENSURE_VERTICES(P
);
3591 assert(!Polyhedron_is_infinite_param(P
, nparam
));
3592 assert(P
->NbBid
== 0);
3593 assert(Polyhedron_has_positive_rays(P
, nparam
));
3595 for (int r
= 0; r
< P
->NbRays
; ++r
) {
3596 if (value_notzero_p(P
->Ray
[r
][P
->Dimension
+1]))
3598 for (int i
= 0; i
< nparam
; ++i
) {
3600 if (value_posz_p(P
->Ray
[r
][i
+1]))
3603 M
= Matrix_Alloc(D
->Dimension
+1, D
->Dimension
+1);
3604 for (int i
= 0; i
< D
->Dimension
+1; ++i
)
3605 value_set_si(M
->p
[i
][i
], 1);
3607 Inner_Product(P
->Ray
[r
]+1, M
->p
[i
], D
->Dimension
+1, &tmp
);
3608 if (value_posz_p(tmp
))
3611 for (j
= P
->Dimension
- nparam
; j
< P
->Dimension
; ++j
)
3612 if (value_pos_p(P
->Ray
[r
][j
+1]))
3614 assert(j
< P
->Dimension
);
3615 value_pdivision(tmp
, P
->Ray
[r
][j
+1], P
->Ray
[r
][i
+1]);
3616 value_subtract(M
->p
[i
][j
], M
->p
[i
][j
], tmp
);
3622 D
= DomainImage(D
, M
, MaxRays
);
3628 gen_fun
* barvinok_enumerate_union_series_with_options(Polyhedron
*D
, Polyhedron
* C
,
3629 barvinok_options
*options
)
3631 Polyhedron
*conv
, *D2
;
3633 gen_fun
*gf
= NULL
, *gf2
;
3634 unsigned nparam
= C
->Dimension
;
3639 CA
= align_context(C
, D
->Dimension
, options
->MaxRays
);
3640 D
= DomainIntersection(D
, CA
, options
->MaxRays
);
3641 Polyhedron_Free(CA
);
3643 D2
= skew_into_positive_orthant(D
, nparam
, options
->MaxRays
);
3644 for (Polyhedron
*P
= D2
; P
; P
= P
->next
) {
3645 assert(P
->Dimension
== D2
->Dimension
);
3648 P_gf
= series(Polyhedron_Copy(P
), nparam
, options
);
3652 gf
->add_union(P_gf
, options
);
3656 /* we actually only need the convex union of the parameter space
3657 * but the reducer classes currently expect a polyhedron in
3658 * the combined space
3660 Polyhedron_Free(gf
->context
);
3661 gf
->context
= DomainConvex(D2
, options
->MaxRays
);
3663 gf2
= gf
->summate(D2
->Dimension
- nparam
, options
);
3672 gen_fun
* barvinok_enumerate_union_series(Polyhedron
*D
, Polyhedron
* C
,
3676 barvinok_options
*options
= barvinok_options_new_with_defaults();
3677 options
->MaxRays
= MaxRays
;
3678 gf
= barvinok_enumerate_union_series_with_options(D
, C
, options
);
3683 evalue
* barvinok_enumerate_union(Polyhedron
*D
, Polyhedron
* C
, unsigned MaxRays
)
3686 gen_fun
*gf
= barvinok_enumerate_union_series(D
, C
, MaxRays
);