8 #include <NTL/mat_ZZ.h>
10 #include <barvinok/util.h>
12 #include <polylib/polylibgmp.h>
13 #include <barvinok/evalue.h>
17 #include <barvinok/barvinok.h>
18 #include <barvinok/genfun.h>
19 #include <barvinok/options.h>
20 #include <barvinok/sample.h>
21 #include "conversion.h"
22 #include "decomposer.h"
23 #include "lattice_point.h"
24 #include "reduce_domain.h"
25 #include "genfun_constructor.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_polar(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_polar(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
, unsigned MaxRays
)
613 for (; r
< P
->NbRays
; ++r
)
614 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1]))
616 if (P
->NbBid
== 0 && r
== P
->NbRays
)
622 sample
= Polyhedron_Sample(P
, MaxRays
);
624 value_set_si(*result
, 0);
626 value_set_si(*result
, -1);
632 for (int i
= 0; i
< P
->NbRays
; ++i
)
633 if (value_one_p(P
->Ray
[i
][1+P
->Dimension
])) {
634 value_set_si(*result
, -1);
639 v
= Vector_Alloc(P
->Dimension
+1);
640 Vector_Gcd(P
->Ray
[r
]+1, P
->Dimension
, &g
);
641 Vector_AntiScale(P
->Ray
[r
]+1, v
->p
, g
, P
->Dimension
+1);
642 M
= unimodular_complete(v
);
643 value_set_si(M
->p
[P
->Dimension
][P
->Dimension
], 1);
646 P
= Polyhedron_Preimage(P
, M2
, 0);
655 value_set_si(size
, 0);
657 for (int i
= 0; i
< P
->NbBid
; ++i
) {
658 value_absolute(tmp
, P
->Ray
[i
][1]);
659 if (value_gt(tmp
, size
))
660 value_assign(size
, tmp
);
662 for (int i
= P
->NbBid
; i
< P
->NbRays
; ++i
) {
663 if (value_zero_p(P
->Ray
[i
][P
->Dimension
+1])) {
664 if (value_gt(P
->Ray
[i
][1], size
))
665 value_assign(size
, P
->Ray
[i
][1]);
668 mpz_cdiv_q(tmp
, P
->Ray
[i
][1], P
->Ray
[i
][P
->Dimension
+1]);
669 if (first
|| value_gt(tmp
, offset
)) {
670 value_assign(offset
, tmp
);
674 value_addto(offset
, offset
, size
);
678 v
= Vector_Alloc(P
->Dimension
+2);
679 value_set_si(v
->p
[0], 1);
680 value_set_si(v
->p
[1], -1);
681 value_assign(v
->p
[1+P
->Dimension
], offset
);
682 R
= AddConstraints(v
->p
, 1, P
, MaxRays
);
690 barvinok_count(P
, &c
, MaxRays
);
693 value_set_si(*result
, 0);
695 value_set_si(*result
, -1);
701 typedef Polyhedron
* Polyhedron_p
;
703 static void barvinok_count_f(Polyhedron
*P
, Value
* result
,
704 barvinok_options
*options
);
706 void barvinok_count_with_options(Polyhedron
*P
, Value
* result
,
707 struct barvinok_options
*options
)
712 bool infinite
= false;
715 value_set_si(*result
, 0);
721 P
= remove_equalities(P
);
722 P
= DomainConstraintSimplify(P
, options
->MaxRays
);
726 } while (!emptyQ(P
) && P
->NbEq
!= 0);
729 value_set_si(*result
, 0);
734 if (Polyhedron_is_infinite(P
, result
, options
->MaxRays
)) {
739 if (P
->Dimension
== 0) {
740 /* Test whether the constraints are satisfied */
741 POL_ENSURE_VERTICES(P
);
742 value_set_si(*result
, !emptyQ(P
));
747 Q
= Polyhedron_Factor(P
, 0, options
->MaxRays
);
755 barvinok_count_f(P
, result
, options
);
756 if (value_neg_p(*result
))
758 if (Q
&& P
->next
&& value_notzero_p(*result
)) {
762 for (Q
= P
->next
; Q
; Q
= Q
->next
) {
763 barvinok_count_f(Q
, &factor
, options
);
764 if (value_neg_p(factor
)) {
767 } else if (Q
->next
&& value_zero_p(factor
)) {
768 value_set_si(*result
, 0);
771 value_multiply(*result
, *result
, factor
);
780 value_set_si(*result
, -1);
783 void barvinok_count(Polyhedron
*P
, Value
* result
, unsigned NbMaxCons
)
785 barvinok_options
*options
= barvinok_options_new_with_defaults();
786 options
->MaxRays
= NbMaxCons
;
787 barvinok_count_with_options(P
, result
, options
);
791 static void barvinok_count_f(Polyhedron
*P
, Value
* result
,
792 barvinok_options
*options
)
795 value_set_si(*result
, 0);
799 if (P
->Dimension
== 1)
800 return Line_Length(P
, result
);
802 int c
= P
->NbConstraints
;
803 POL_ENSURE_FACETS(P
);
804 if (c
!= P
->NbConstraints
|| P
->NbEq
!= 0)
805 return barvinok_count_with_options(P
, result
, options
);
807 POL_ENSURE_VERTICES(P
);
809 if (Polyhedron_is_infinite(P
, result
, options
->MaxRays
))
813 if (options
->incremental_specialization
== 2)
814 cnt
= new bfcounter(P
->Dimension
);
815 else if (options
->incremental_specialization
== 1)
816 cnt
= new icounter(P
->Dimension
);
818 cnt
= new counter(P
->Dimension
);
819 cnt
->start(P
, options
);
821 cnt
->get_count(result
);
825 static void uni_polynom(int param
, Vector
*c
, evalue
*EP
)
827 unsigned dim
= c
->Size
-2;
829 value_set_si(EP
->d
,0);
830 EP
->x
.p
= new_enode(polynomial
, dim
+1, param
+1);
831 for (int j
= 0; j
<= dim
; ++j
)
832 evalue_set(&EP
->x
.p
->arr
[j
], c
->p
[j
], c
->p
[dim
+1]);
835 static void multi_polynom(Vector
*c
, evalue
* X
, evalue
*EP
)
837 unsigned dim
= c
->Size
-2;
841 evalue_set(&EC
, c
->p
[dim
], c
->p
[dim
+1]);
844 evalue_set(EP
, c
->p
[dim
], c
->p
[dim
+1]);
846 for (int i
= dim
-1; i
>= 0; --i
) {
848 value_assign(EC
.x
.n
, c
->p
[i
]);
851 free_evalue_refs(&EC
);
854 Polyhedron
*unfringe (Polyhedron
*P
, unsigned MaxRays
)
856 int len
= P
->Dimension
+2;
857 Polyhedron
*T
, *R
= P
;
860 Vector
*row
= Vector_Alloc(len
);
861 value_set_si(row
->p
[0], 1);
863 R
= DomainConstraintSimplify(Polyhedron_Copy(P
), MaxRays
);
865 Matrix
*M
= Matrix_Alloc(2, len
-1);
866 value_set_si(M
->p
[1][len
-2], 1);
867 for (int v
= 0; v
< P
->Dimension
; ++v
) {
868 value_set_si(M
->p
[0][v
], 1);
869 Polyhedron
*I
= Polyhedron_Image(R
, M
, 2+1);
870 value_set_si(M
->p
[0][v
], 0);
871 for (int r
= 0; r
< I
->NbConstraints
; ++r
) {
872 if (value_zero_p(I
->Constraint
[r
][0]))
874 if (value_zero_p(I
->Constraint
[r
][1]))
876 if (value_one_p(I
->Constraint
[r
][1]))
878 if (value_mone_p(I
->Constraint
[r
][1]))
880 value_absolute(g
, I
->Constraint
[r
][1]);
881 Vector_Set(row
->p
+1, 0, len
-2);
882 value_division(row
->p
[1+v
], I
->Constraint
[r
][1], g
);
883 mpz_fdiv_q(row
->p
[len
-1], I
->Constraint
[r
][2], g
);
885 R
= AddConstraints(row
->p
, 1, R
, MaxRays
);
897 /* this procedure may have false negatives */
898 static bool Polyhedron_is_infinite_param(Polyhedron
*P
, unsigned nparam
)
901 for (r
= 0; r
< P
->NbRays
; ++r
) {
902 if (!value_zero_p(P
->Ray
[r
][0]) &&
903 !value_zero_p(P
->Ray
[r
][P
->Dimension
+1]))
905 if (First_Non_Zero(P
->Ray
[r
]+1+P
->Dimension
-nparam
, nparam
) == -1)
911 /* Check whether all rays point in the positive directions
914 static bool Polyhedron_has_positive_rays(Polyhedron
*P
, unsigned nparam
)
917 for (r
= 0; r
< P
->NbRays
; ++r
)
918 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1])) {
920 for (i
= P
->Dimension
- nparam
; i
< P
->Dimension
; ++i
)
921 if (value_neg_p(P
->Ray
[r
][i
+1]))
927 typedef evalue
* evalue_p
;
929 struct enumerator
: public polar_decomposer
{
943 enumerator(Polyhedron
*P
, unsigned dim
, unsigned nbV
) {
947 randomvector(P
, lambda
, dim
);
948 rays
.SetDims(dim
, dim
);
950 c
= Vector_Alloc(dim
+2);
952 vE
= new evalue_p
[nbV
];
953 for (int j
= 0; j
< nbV
; ++j
)
959 void decompose_at(Param_Vertices
*V
, int _i
, barvinok_options
*options
) {
960 Polyhedron
*C
= supporting_cone_p(P
, V
);
965 value_init(vE
[_i
]->d
);
966 evalue_set_si(vE
[_i
], 0, 1);
968 decompose(C
, options
);
975 for (int j
= 0; j
< nbV
; ++j
)
977 free_evalue_refs(vE
[j
]);
983 virtual void handle_polar(Polyhedron
*P
, int sign
);
986 void enumerator::handle_polar(Polyhedron
*C
, int s
)
989 assert(C
->NbRays
-1 == dim
);
990 add_rays(rays
, C
, &r
);
991 for (int k
= 0; k
< dim
; ++k
) {
992 if (lambda
* rays
[k
] == 0)
998 lattice_point(V
, C
, lambda
, &num
, 0);
1000 normalize(sign
, num
.constant
, den
);
1002 dpoly
n(dim
, den
[0], 1);
1003 for (int k
= 1; k
< dim
; ++k
) {
1004 dpoly
fact(dim
, den
[k
], 1);
1007 if (num
.E
!= NULL
) {
1008 ZZ
one(INIT_VAL
, 1);
1009 dpoly_n
d(dim
, num
.constant
, one
);
1012 multi_polynom(c
, num
.E
, &EV
);
1014 free_evalue_refs(&EV
);
1015 free_evalue_refs(num
.E
);
1017 } else if (num
.pos
!= -1) {
1018 dpoly_n
d(dim
, num
.constant
, num
.coeff
);
1021 uni_polynom(num
.pos
, c
, &EV
);
1023 free_evalue_refs(&EV
);
1025 mpq_set_si(count
, 0, 1);
1026 dpoly
d(dim
, num
.constant
);
1027 d
.div(n
, count
, sign
);
1030 evalue_set(&EV
, &count
[0]._mp_num
, &count
[0]._mp_den
);
1032 free_evalue_refs(&EV
);
1036 struct enumerator_base
{
1041 vertex_decomposer
*vpd
;
1043 enumerator_base(unsigned dim
, vertex_decomposer
*vpd
)
1048 vE
= new evalue_p
[vpd
->nbV
];
1049 for (int j
= 0; j
< vpd
->nbV
; ++j
)
1052 E_vertex
= new evalue_p
[dim
];
1055 evalue_set_si(&mone
, -1, 1);
1058 void decompose_at(Param_Vertices
*V
, int _i
, barvinok_options
*options
) {
1061 vE
[_i
] = new evalue
;
1062 value_init(vE
[_i
]->d
);
1063 evalue_set_si(vE
[_i
], 0, 1);
1065 vpd
->decompose_at_vertex(V
, _i
, options
);
1068 ~enumerator_base() {
1069 for (int j
= 0; j
< vpd
->nbV
; ++j
)
1071 free_evalue_refs(vE
[j
]);
1078 free_evalue_refs(&mone
);
1081 evalue
*E_num(int i
, int d
) {
1082 return E_vertex
[i
+ (dim
-d
)];
1091 cumulator(evalue
*factor
, evalue
*v
, dpoly_r
*r
) :
1092 factor(factor
), v(v
), r(r
) {}
1096 virtual void add_term(int *powers
, int len
, evalue
*f2
) = 0;
1099 void cumulator::cumulate()
1101 evalue cum
; // factor * 1 * E_num[0]/1 * (E_num[0]-1)/2 *...
1103 evalue t
; // E_num[0] - (m-1)
1109 evalue_set_si(&mone
, -1, 1);
1113 evalue_copy(&cum
, factor
);
1116 value_set_si(f
.d
, 1);
1117 value_set_si(f
.x
.n
, 1);
1122 for (cst
= &t
; value_zero_p(cst
->d
); ) {
1123 if (cst
->x
.p
->type
== fractional
)
1124 cst
= &cst
->x
.p
->arr
[1];
1126 cst
= &cst
->x
.p
->arr
[0];
1130 for (int m
= 0; m
< r
->len
; ++m
) {
1133 value_set_si(f
.d
, m
);
1136 value_subtract(cst
->x
.n
, cst
->x
.n
, cst
->d
);
1143 vector
< dpoly_r_term
* >& current
= r
->c
[r
->len
-1-m
];
1144 for (int j
= 0; j
< current
.size(); ++j
) {
1145 if (current
[j
]->coeff
== 0)
1147 evalue
*f2
= new evalue
;
1149 value_init(f2
->x
.n
);
1150 zz2value(current
[j
]->coeff
, f2
->x
.n
);
1151 zz2value(r
->denom
, f2
->d
);
1154 add_term(current
[j
]->powers
, r
->dim
, f2
);
1157 free_evalue_refs(&f
);
1158 free_evalue_refs(&t
);
1159 free_evalue_refs(&cum
);
1161 free_evalue_refs(&mone
);
1165 struct E_poly_term
{
1170 struct ie_cum
: public cumulator
{
1171 vector
<E_poly_term
*> terms
;
1173 ie_cum(evalue
*factor
, evalue
*v
, dpoly_r
*r
) : cumulator(factor
, v
, r
) {}
1175 virtual void add_term(int *powers
, int len
, evalue
*f2
);
1178 void ie_cum::add_term(int *powers
, int len
, evalue
*f2
)
1181 for (k
= 0; k
< terms
.size(); ++k
) {
1182 if (memcmp(terms
[k
]->powers
, powers
, len
* sizeof(int)) == 0) {
1183 eadd(f2
, terms
[k
]->E
);
1184 free_evalue_refs(f2
);
1189 if (k
>= terms
.size()) {
1190 E_poly_term
*ET
= new E_poly_term
;
1191 ET
->powers
= new int[len
];
1192 memcpy(ET
->powers
, powers
, len
* sizeof(int));
1194 terms
.push_back(ET
);
1198 struct ienumerator
: public polar_decomposer
, public vertex_decomposer
,
1199 public enumerator_base
{
1205 ienumerator(Polyhedron
*P
, unsigned dim
, unsigned nbV
) :
1206 vertex_decomposer(P
, nbV
, this), enumerator_base(dim
, this) {
1207 vertex
.SetLength(dim
);
1209 den
.SetDims(dim
, dim
);
1217 virtual void handle_polar(Polyhedron
*P
, int sign
);
1218 void reduce(evalue
*factor
, vec_ZZ
& num
, mat_ZZ
& den_f
);
1221 void ienumerator::reduce(
1222 evalue
*factor
, vec_ZZ
& num
, mat_ZZ
& den_f
)
1224 unsigned len
= den_f
.NumRows(); // number of factors in den
1225 unsigned dim
= num
.length();
1228 eadd(factor
, vE
[vert
]);
1233 den_s
.SetLength(len
);
1235 den_r
.SetDims(len
, dim
-1);
1239 for (r
= 0; r
< len
; ++r
) {
1240 den_s
[r
] = den_f
[r
][0];
1241 for (k
= 0; k
<= dim
-1; ++k
)
1243 den_r
[r
][k
-(k
>0)] = den_f
[r
][k
];
1248 num_p
.SetLength(dim
-1);
1249 for (k
= 0 ; k
<= dim
-1; ++k
)
1251 num_p
[k
-(k
>0)] = num
[k
];
1254 den_p
.SetLength(len
);
1258 normalize(one
, num_s
, num_p
, den_s
, den_p
, den_r
);
1260 emul(&mone
, factor
);
1264 for (int k
= 0; k
< len
; ++k
) {
1267 else if (den_s
[k
] == 0)
1270 if (no_param
== 0) {
1271 reduce(factor
, num_p
, den_r
);
1275 pden
.SetDims(only_param
, dim
-1);
1277 for (k
= 0, l
= 0; k
< len
; ++k
)
1279 pden
[l
++] = den_r
[k
];
1281 for (k
= 0; k
< len
; ++k
)
1285 dpoly
n(no_param
, num_s
);
1286 dpoly
D(no_param
, den_s
[k
], 1);
1287 for ( ; ++k
< len
; )
1288 if (den_p
[k
] == 0) {
1289 dpoly
fact(no_param
, den_s
[k
], 1);
1294 // if no_param + only_param == len then all powers
1295 // below will be all zero
1296 if (no_param
+ only_param
== len
) {
1297 if (E_num(0, dim
) != 0)
1298 r
= new dpoly_r(n
, len
);
1300 mpq_set_si(tcount
, 0, 1);
1302 n
.div(D
, tcount
, one
);
1304 if (value_notzero_p(mpq_numref(tcount
))) {
1308 value_assign(f
.x
.n
, mpq_numref(tcount
));
1309 value_assign(f
.d
, mpq_denref(tcount
));
1311 reduce(factor
, num_p
, pden
);
1312 free_evalue_refs(&f
);
1317 for (k
= 0; k
< len
; ++k
) {
1318 if (den_s
[k
] == 0 || den_p
[k
] == 0)
1321 dpoly
pd(no_param
-1, den_s
[k
], 1);
1324 for (l
= 0; l
< k
; ++l
)
1325 if (den_r
[l
] == den_r
[k
])
1329 r
= new dpoly_r(n
, pd
, l
, len
);
1331 dpoly_r
*nr
= new dpoly_r(r
, pd
, l
, len
);
1337 dpoly_r
*rc
= r
->div(D
);
1340 if (E_num(0, dim
) == 0) {
1341 int common
= pden
.NumRows();
1342 vector
< dpoly_r_term
* >& final
= r
->c
[r
->len
-1];
1348 zz2value(r
->denom
, f
.d
);
1349 for (int j
= 0; j
< final
.size(); ++j
) {
1350 if (final
[j
]->coeff
== 0)
1353 for (int k
= 0; k
< r
->dim
; ++k
) {
1354 int n
= final
[j
]->powers
[k
];
1357 pden
.SetDims(rows
+n
, pden
.NumCols());
1358 for (int l
= 0; l
< n
; ++l
)
1359 pden
[rows
+l
] = den_r
[k
];
1363 evalue_copy(&t
, factor
);
1364 zz2value(final
[j
]->coeff
, f
.x
.n
);
1366 reduce(&t
, num_p
, pden
);
1367 free_evalue_refs(&t
);
1369 free_evalue_refs(&f
);
1371 ie_cum
cum(factor
, E_num(0, dim
), r
);
1374 int common
= pden
.NumRows();
1376 for (int j
= 0; j
< cum
.terms
.size(); ++j
) {
1378 pden
.SetDims(rows
, pden
.NumCols());
1379 for (int k
= 0; k
< r
->dim
; ++k
) {
1380 int n
= cum
.terms
[j
]->powers
[k
];
1383 pden
.SetDims(rows
+n
, pden
.NumCols());
1384 for (int l
= 0; l
< n
; ++l
)
1385 pden
[rows
+l
] = den_r
[k
];
1388 reduce(cum
.terms
[j
]->E
, num_p
, pden
);
1389 free_evalue_refs(cum
.terms
[j
]->E
);
1390 delete cum
.terms
[j
]->E
;
1391 delete [] cum
.terms
[j
]->powers
;
1392 delete cum
.terms
[j
];
1399 static int type_offset(enode
*p
)
1401 return p
->type
== fractional
? 1 :
1402 p
->type
== flooring
? 1 : 0;
1405 static int edegree(evalue
*e
)
1410 if (value_notzero_p(e
->d
))
1414 int i
= type_offset(p
);
1415 if (p
->size
-i
-1 > d
)
1416 d
= p
->size
- i
- 1;
1417 for (; i
< p
->size
; i
++) {
1418 int d2
= edegree(&p
->arr
[i
]);
1425 void ienumerator::handle_polar(Polyhedron
*C
, int s
)
1427 assert(C
->NbRays
-1 == dim
);
1429 lattice_point(V
, C
, vertex
, E_vertex
);
1432 for (r
= 0; r
< dim
; ++r
)
1433 values2zz(C
->Ray
[r
]+1, den
[r
], dim
);
1437 evalue_set_si(&one
, s
, 1);
1438 reduce(&one
, vertex
, den
);
1439 free_evalue_refs(&one
);
1441 for (int i
= 0; i
< dim
; ++i
)
1443 free_evalue_refs(E_vertex
[i
]);
1448 struct bfenumerator
: public vertex_decomposer
, public bf_base
,
1449 public enumerator_base
{
1452 bfenumerator(Polyhedron
*P
, unsigned dim
, unsigned nbV
) :
1453 vertex_decomposer(P
, nbV
, this),
1454 bf_base(dim
), enumerator_base(dim
, this) {
1462 virtual void handle_polar(Polyhedron
*P
, int sign
);
1463 virtual void base(mat_ZZ
& factors
, bfc_vec
& v
);
1465 bfc_term_base
* new_bf_term(int len
) {
1466 bfe_term
* t
= new bfe_term(len
);
1470 virtual void set_factor(bfc_term_base
*t
, int k
, int change
) {
1471 bfe_term
* bfet
= static_cast<bfe_term
*>(t
);
1472 factor
= bfet
->factors
[k
];
1473 assert(factor
!= NULL
);
1474 bfet
->factors
[k
] = NULL
;
1476 emul(&mone
, factor
);
1479 virtual void set_factor(bfc_term_base
*t
, int k
, mpq_t
&q
, int change
) {
1480 bfe_term
* bfet
= static_cast<bfe_term
*>(t
);
1481 factor
= bfet
->factors
[k
];
1482 assert(factor
!= NULL
);
1483 bfet
->factors
[k
] = NULL
;
1489 value_oppose(f
.x
.n
, mpq_numref(q
));
1491 value_assign(f
.x
.n
, mpq_numref(q
));
1492 value_assign(f
.d
, mpq_denref(q
));
1494 free_evalue_refs(&f
);
1497 virtual void set_factor(bfc_term_base
*t
, int k
, const QQ
& c
, int change
) {
1498 bfe_term
* bfet
= static_cast<bfe_term
*>(t
);
1500 factor
= new evalue
;
1505 zz2value(c
.n
, f
.x
.n
);
1507 value_oppose(f
.x
.n
, f
.x
.n
);
1510 value_init(factor
->d
);
1511 evalue_copy(factor
, bfet
->factors
[k
]);
1513 free_evalue_refs(&f
);
1516 void set_factor(evalue
*f
, int change
) {
1522 virtual void insert_term(bfc_term_base
*t
, int i
) {
1523 bfe_term
* bfet
= static_cast<bfe_term
*>(t
);
1524 int len
= t
->terms
.NumRows()-1; // already increased by one
1526 bfet
->factors
.resize(len
+1);
1527 for (int j
= len
; j
> i
; --j
) {
1528 bfet
->factors
[j
] = bfet
->factors
[j
-1];
1529 t
->terms
[j
] = t
->terms
[j
-1];
1531 bfet
->factors
[i
] = factor
;
1535 virtual void update_term(bfc_term_base
*t
, int i
) {
1536 bfe_term
* bfet
= static_cast<bfe_term
*>(t
);
1538 eadd(factor
, bfet
->factors
[i
]);
1539 free_evalue_refs(factor
);
1543 virtual bool constant_vertex(int dim
) { return E_num(0, dim
) == 0; }
1545 virtual void cum(bf_reducer
*bfr
, bfc_term_base
*t
, int k
, dpoly_r
*r
);
1548 struct bfe_cum
: public cumulator
{
1550 bfc_term_base
*told
;
1554 bfe_cum(evalue
*factor
, evalue
*v
, dpoly_r
*r
, bf_reducer
*bfr
,
1555 bfc_term_base
*t
, int k
, bfenumerator
*e
) :
1556 cumulator(factor
, v
, r
), told(t
), k(k
),
1560 virtual void add_term(int *powers
, int len
, evalue
*f2
);
1563 void bfe_cum::add_term(int *powers
, int len
, evalue
*f2
)
1565 bfr
->update_powers(powers
, len
);
1567 bfc_term_base
* t
= bfe
->find_bfc_term(bfr
->vn
, bfr
->npowers
, bfr
->nnf
);
1568 bfe
->set_factor(f2
, bfr
->l_changes
% 2);
1569 bfe
->add_term(t
, told
->terms
[k
], bfr
->l_extra_num
);
1572 void bfenumerator::cum(bf_reducer
*bfr
, bfc_term_base
*t
, int k
,
1575 bfe_term
* bfet
= static_cast<bfe_term
*>(t
);
1576 bfe_cum
cum(bfet
->factors
[k
], E_num(0, bfr
->d
), r
, bfr
, t
, k
, this);
1580 void bfenumerator::base(mat_ZZ
& factors
, bfc_vec
& v
)
1582 for (int i
= 0; i
< v
.size(); ++i
) {
1583 assert(v
[i
]->terms
.NumRows() == 1);
1584 evalue
*factor
= static_cast<bfe_term
*>(v
[i
])->factors
[0];
1585 eadd(factor
, vE
[vert
]);
1590 void bfenumerator::handle_polar(Polyhedron
*C
, int s
)
1592 assert(C
->NbRays
-1 == enumerator_base::dim
);
1594 bfe_term
* t
= new bfe_term(enumerator_base::dim
);
1595 vector
< bfc_term_base
* > v
;
1598 t
->factors
.resize(1);
1600 t
->terms
.SetDims(1, enumerator_base::dim
);
1601 lattice_point(V
, C
, t
->terms
[0], E_vertex
);
1603 // the elements of factors are always lexpositive
1605 s
= setup_factors(C
, factors
, t
, s
);
1607 t
->factors
[0] = new evalue
;
1608 value_init(t
->factors
[0]->d
);
1609 evalue_set_si(t
->factors
[0], s
, 1);
1612 for (int i
= 0; i
< enumerator_base::dim
; ++i
)
1614 free_evalue_refs(E_vertex
[i
]);
1619 #ifdef HAVE_CORRECT_VERTICES
1620 static inline Param_Polyhedron
*Polyhedron2Param_SD(Polyhedron
**Din
,
1621 Polyhedron
*Cin
,int WS
,Polyhedron
**CEq
,Matrix
**CT
)
1623 if (WS
& POL_NO_DUAL
)
1625 return Polyhedron2Param_SimplifiedDomain(Din
, Cin
, WS
, CEq
, CT
);
1628 static Param_Polyhedron
*Polyhedron2Param_SD(Polyhedron
**Din
,
1629 Polyhedron
*Cin
,int WS
,Polyhedron
**CEq
,Matrix
**CT
)
1631 static char data
[] = " 1 0 0 0 0 1 -18 "
1632 " 1 0 0 -20 0 19 1 "
1633 " 1 0 1 20 0 -20 16 "
1636 " 1 4 -20 0 0 -1 23 "
1637 " 1 -4 20 0 0 1 -22 "
1638 " 1 0 1 0 20 -20 16 "
1639 " 1 0 0 0 -20 19 1 ";
1640 static int checked
= 0;
1645 Matrix
*M
= Matrix_Alloc(9, 7);
1646 for (i
= 0; i
< 9; ++i
)
1647 for (int j
= 0; j
< 7; ++j
) {
1648 sscanf(p
, "%d%n", &v
, &n
);
1650 value_set_si(M
->p
[i
][j
], v
);
1652 Polyhedron
*P
= Constraints2Polyhedron(M
, 1024);
1654 Polyhedron
*U
= Universe_Polyhedron(1);
1655 Param_Polyhedron
*PP
= Polyhedron2Param_Domain(P
, U
, 1024);
1659 for (i
= 0, V
= PP
->V
; V
; ++i
, V
= V
->next
)
1662 Param_Polyhedron_Free(PP
);
1664 fprintf(stderr
, "WARNING: results may be incorrect\n");
1666 "WARNING: use latest version of PolyLib to remove this warning\n");
1670 return Polyhedron2Param_SimplifiedDomain(Din
, Cin
, WS
, CEq
, CT
);
1674 static evalue
* barvinok_enumerate_ev_f(Polyhedron
*P
, Polyhedron
* C
,
1675 barvinok_options
*options
);
1678 static evalue
* barvinok_enumerate_cst(Polyhedron
*P
, Polyhedron
* C
,
1683 ALLOC(evalue
, eres
);
1684 value_init(eres
->d
);
1685 value_set_si(eres
->d
, 0);
1686 eres
->x
.p
= new_enode(partition
, 2, C
->Dimension
);
1687 EVALUE_SET_DOMAIN(eres
->x
.p
->arr
[0], DomainConstraintSimplify(C
, MaxRays
));
1688 value_set_si(eres
->x
.p
->arr
[1].d
, 1);
1689 value_init(eres
->x
.p
->arr
[1].x
.n
);
1691 value_set_si(eres
->x
.p
->arr
[1].x
.n
, 0);
1693 barvinok_count(P
, &eres
->x
.p
->arr
[1].x
.n
, MaxRays
);
1698 evalue
* barvinok_enumerate_with_options(Polyhedron
*P
, Polyhedron
* C
,
1699 struct barvinok_options
*options
)
1701 //P = unfringe(P, MaxRays);
1702 Polyhedron
*Corig
= C
;
1703 Polyhedron
*CEq
= NULL
, *rVD
, *CA
;
1705 unsigned nparam
= C
->Dimension
;
1709 value_init(factor
.d
);
1710 evalue_set_si(&factor
, 1, 1);
1712 CA
= align_context(C
, P
->Dimension
, options
->MaxRays
);
1713 P
= DomainIntersection(P
, CA
, options
->MaxRays
);
1714 Polyhedron_Free(CA
);
1717 POL_ENSURE_FACETS(P
);
1718 POL_ENSURE_VERTICES(P
);
1719 POL_ENSURE_FACETS(C
);
1720 POL_ENSURE_VERTICES(C
);
1722 if (C
->Dimension
== 0 || emptyQ(P
)) {
1724 eres
= barvinok_enumerate_cst(P
, CEq
? CEq
: Polyhedron_Copy(C
),
1727 emul(&factor
, eres
);
1728 reduce_evalue(eres
);
1729 free_evalue_refs(&factor
);
1736 if (Polyhedron_is_infinite_param(P
, nparam
))
1741 P
= remove_equalities_p(P
, P
->Dimension
-nparam
, &f
);
1745 if (P
->Dimension
== nparam
) {
1747 P
= Universe_Polyhedron(0);
1751 Polyhedron
*T
= Polyhedron_Factor(P
, nparam
, options
->MaxRays
);
1752 if (T
|| (P
->Dimension
== nparam
+1)) {
1755 for (Q
= T
? T
: P
; Q
; Q
= Q
->next
) {
1756 Polyhedron
*next
= Q
->next
;
1760 if (Q
->Dimension
!= C
->Dimension
)
1761 QC
= Polyhedron_Project(Q
, nparam
);
1764 C
= DomainIntersection(C
, QC
, options
->MaxRays
);
1766 Polyhedron_Free(C2
);
1768 Polyhedron_Free(QC
);
1776 if (T
->Dimension
== C
->Dimension
) {
1783 Polyhedron
*next
= P
->next
;
1785 eres
= barvinok_enumerate_ev_f(P
, C
, options
);
1792 for (Q
= P
->next
; Q
; Q
= Q
->next
) {
1793 Polyhedron
*next
= Q
->next
;
1796 f
= barvinok_enumerate_ev_f(Q
, C
, options
);
1798 free_evalue_refs(f
);
1808 evalue
* barvinok_enumerate_ev(Polyhedron
*P
, Polyhedron
* C
, unsigned MaxRays
)
1811 barvinok_options
*options
= barvinok_options_new_with_defaults();
1812 options
->MaxRays
= MaxRays
;
1813 E
= barvinok_enumerate_with_options(P
, C
, options
);
1818 static evalue
* barvinok_enumerate_ev_f(Polyhedron
*P
, Polyhedron
* C
,
1819 barvinok_options
*options
)
1821 unsigned nparam
= C
->Dimension
;
1823 if (P
->Dimension
- nparam
== 1)
1824 return ParamLine_Length(P
, C
, options
->MaxRays
);
1826 Param_Polyhedron
*PP
= NULL
;
1827 Polyhedron
*CEq
= NULL
, *pVD
;
1829 Param_Domain
*D
, *next
;
1832 Polyhedron
*Porig
= P
;
1834 PP
= Polyhedron2Param_SD(&P
,C
,options
->MaxRays
,&CEq
,&CT
);
1836 if (isIdentity(CT
)) {
1840 assert(CT
->NbRows
!= CT
->NbColumns
);
1841 if (CT
->NbRows
== 1) { // no more parameters
1842 eres
= barvinok_enumerate_cst(P
, CEq
, options
->MaxRays
);
1847 Param_Polyhedron_Free(PP
);
1853 nparam
= CT
->NbRows
- 1;
1856 unsigned dim
= P
->Dimension
- nparam
;
1858 ALLOC(evalue
, eres
);
1859 value_init(eres
->d
);
1860 value_set_si(eres
->d
, 0);
1863 for (nd
= 0, D
=PP
->D
; D
; ++nd
, D
=D
->next
);
1864 struct section
{ Polyhedron
*D
; evalue E
; };
1865 section
*s
= new section
[nd
];
1866 Polyhedron
**fVD
= new Polyhedron_p
[nd
];
1869 #ifdef USE_INCREMENTAL_BF
1870 bfenumerator
et(P
, dim
, PP
->nbV
);
1871 #elif defined USE_INCREMENTAL_DF
1872 ienumerator
et(P
, dim
, PP
->nbV
);
1874 enumerator
et(P
, dim
, PP
->nbV
);
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
);
1917 evalue_set_si(eres
, 0, 1);
1919 eres
->x
.p
= new_enode(partition
, 2*nd
, C
->Dimension
);
1920 for (int j
= 0; j
< nd
; ++j
) {
1921 EVALUE_SET_DOMAIN(eres
->x
.p
->arr
[2*j
], s
[j
].D
);
1922 value_clear(eres
->x
.p
->arr
[2*j
+1].d
);
1923 eres
->x
.p
->arr
[2*j
+1] = s
[j
].E
;
1924 Domain_Free(fVD
[j
]);
1931 Polyhedron_Free(CEq
);
1935 Enumeration
* barvinok_enumerate(Polyhedron
*P
, Polyhedron
* C
, unsigned MaxRays
)
1937 evalue
*EP
= barvinok_enumerate_ev(P
, C
, MaxRays
);
1939 return partition2enumeration(EP
);
1942 static void SwapColumns(Value
**V
, int n
, int i
, int j
)
1944 for (int r
= 0; r
< n
; ++r
)
1945 value_swap(V
[r
][i
], V
[r
][j
]);
1948 static void SwapColumns(Polyhedron
*P
, int i
, int j
)
1950 SwapColumns(P
->Constraint
, P
->NbConstraints
, i
, j
);
1951 SwapColumns(P
->Ray
, P
->NbRays
, i
, j
);
1954 /* Construct a constraint c from constraints l and u such that if
1955 * if constraint c holds then for each value of the other variables
1956 * there is at most one value of variable pos (position pos+1 in the constraints).
1958 * Given a lower and an upper bound
1959 * n_l v_i + <c_l,x> + c_l >= 0
1960 * -n_u v_i + <c_u,x> + c_u >= 0
1961 * the constructed constraint is
1963 * -(n_l<c_u,x> + n_u<c_l,x>) + (-n_l c_u - n_u c_l + n_l n_u - 1)
1965 * which is then simplified to remove the content of the non-constant coefficients
1967 * len is the total length of the constraints.
1968 * v is a temporary variable that can be used by this procedure
1970 static void negative_test_constraint(Value
*l
, Value
*u
, Value
*c
, int pos
,
1973 value_oppose(*v
, u
[pos
+1]);
1974 Vector_Combine(l
+1, u
+1, c
+1, *v
, l
[pos
+1], len
-1);
1975 value_multiply(*v
, *v
, l
[pos
+1]);
1976 value_subtract(c
[len
-1], c
[len
-1], *v
);
1977 value_set_si(*v
, -1);
1978 Vector_Scale(c
+1, c
+1, *v
, len
-1);
1979 value_decrement(c
[len
-1], c
[len
-1]);
1980 ConstraintSimplify(c
, c
, len
, v
);
1983 static bool parallel_constraints(Value
*l
, Value
*u
, Value
*c
, int pos
,
1992 Vector_Gcd(&l
[1+pos
], len
, &g1
);
1993 Vector_Gcd(&u
[1+pos
], len
, &g2
);
1994 Vector_Combine(l
+1+pos
, u
+1+pos
, c
+1, g2
, g1
, len
);
1995 parallel
= First_Non_Zero(c
+1, len
) == -1;
2003 static void negative_test_constraint7(Value
*l
, Value
*u
, Value
*c
, int pos
,
2004 int exist
, int len
, Value
*v
)
2009 Vector_Gcd(&u
[1+pos
], exist
, v
);
2010 Vector_Gcd(&l
[1+pos
], exist
, &g
);
2011 Vector_Combine(l
+1, u
+1, c
+1, *v
, g
, len
-1);
2012 value_multiply(*v
, *v
, g
);
2013 value_subtract(c
[len
-1], c
[len
-1], *v
);
2014 value_set_si(*v
, -1);
2015 Vector_Scale(c
+1, c
+1, *v
, len
-1);
2016 value_decrement(c
[len
-1], c
[len
-1]);
2017 ConstraintSimplify(c
, c
, len
, v
);
2022 /* Turns a x + b >= 0 into a x + b <= -1
2024 * len is the total length of the constraint.
2025 * v is a temporary variable that can be used by this procedure
2027 static void oppose_constraint(Value
*c
, int len
, Value
*v
)
2029 value_set_si(*v
, -1);
2030 Vector_Scale(c
+1, c
+1, *v
, len
-1);
2031 value_decrement(c
[len
-1], c
[len
-1]);
2034 /* Split polyhedron P into two polyhedra *pos and *neg, where
2035 * existential variable i has at most one solution for each
2036 * value of the other variables in *neg.
2038 * The splitting is performed using constraints l and u.
2040 * nvar: number of set variables
2041 * row: temporary vector that can be used by this procedure
2042 * f: temporary value that can be used by this procedure
2044 static bool SplitOnConstraint(Polyhedron
*P
, int i
, int l
, int u
,
2045 int nvar
, int MaxRays
, Vector
*row
, Value
& f
,
2046 Polyhedron
**pos
, Polyhedron
**neg
)
2048 negative_test_constraint(P
->Constraint
[l
], P
->Constraint
[u
],
2049 row
->p
, nvar
+i
, P
->Dimension
+2, &f
);
2050 *neg
= AddConstraints(row
->p
, 1, P
, MaxRays
);
2052 /* We found an independent, but useless constraint
2053 * Maybe we should detect this earlier and not
2054 * mark the variable as INDEPENDENT
2056 if (emptyQ((*neg
))) {
2057 Polyhedron_Free(*neg
);
2061 oppose_constraint(row
->p
, P
->Dimension
+2, &f
);
2062 *pos
= AddConstraints(row
->p
, 1, P
, MaxRays
);
2064 if (emptyQ((*pos
))) {
2065 Polyhedron_Free(*neg
);
2066 Polyhedron_Free(*pos
);
2074 * unimodularly transform P such that constraint r is transformed
2075 * into a constraint that involves only a single (the first)
2076 * existential variable
2079 static Polyhedron
*rotate_along(Polyhedron
*P
, int r
, int nvar
, int exist
,
2085 Vector
*row
= Vector_Alloc(exist
);
2086 Vector_Copy(P
->Constraint
[r
]+1+nvar
, row
->p
, exist
);
2087 Vector_Gcd(row
->p
, exist
, &g
);
2088 if (value_notone_p(g
))
2089 Vector_AntiScale(row
->p
, row
->p
, g
, exist
);
2092 Matrix
*M
= unimodular_complete(row
);
2093 Matrix
*M2
= Matrix_Alloc(P
->Dimension
+1, P
->Dimension
+1);
2094 for (r
= 0; r
< nvar
; ++r
)
2095 value_set_si(M2
->p
[r
][r
], 1);
2096 for ( ; r
< nvar
+exist
; ++r
)
2097 Vector_Copy(M
->p
[r
-nvar
], M2
->p
[r
]+nvar
, exist
);
2098 for ( ; r
< P
->Dimension
+1; ++r
)
2099 value_set_si(M2
->p
[r
][r
], 1);
2100 Polyhedron
*T
= Polyhedron_Image(P
, M2
, MaxRays
);
2109 /* Split polyhedron P into two polyhedra *pos and *neg, where
2110 * existential variable i has at most one solution for each
2111 * value of the other variables in *neg.
2113 * If independent is set, then the two constraints on which the
2114 * split will be performed need to be independent of the other
2115 * existential variables.
2117 * Return true if an appropriate split could be performed.
2119 * nvar: number of set variables
2120 * exist: number of existential variables
2121 * row: temporary vector that can be used by this procedure
2122 * f: temporary value that can be used by this procedure
2124 static bool SplitOnVar(Polyhedron
*P
, int i
,
2125 int nvar
, int exist
, int MaxRays
,
2126 Vector
*row
, Value
& f
, bool independent
,
2127 Polyhedron
**pos
, Polyhedron
**neg
)
2131 for (int l
= P
->NbEq
; l
< P
->NbConstraints
; ++l
) {
2132 if (value_negz_p(P
->Constraint
[l
][nvar
+i
+1]))
2136 for (j
= 0; j
< exist
; ++j
)
2137 if (j
!= i
&& value_notzero_p(P
->Constraint
[l
][nvar
+j
+1]))
2143 for (int u
= P
->NbEq
; u
< P
->NbConstraints
; ++u
) {
2144 if (value_posz_p(P
->Constraint
[u
][nvar
+i
+1]))
2148 for (j
= 0; j
< exist
; ++j
)
2149 if (j
!= i
&& value_notzero_p(P
->Constraint
[u
][nvar
+j
+1]))
2155 if (SplitOnConstraint(P
, i
, l
, u
, nvar
, MaxRays
, row
, f
, pos
, neg
)) {
2158 SwapColumns(*neg
, nvar
+1, nvar
+1+i
);
2168 static bool double_bound_pair(Polyhedron
*P
, int nvar
, int exist
,
2169 int i
, int l1
, int l2
,
2170 Polyhedron
**pos
, Polyhedron
**neg
)
2174 Vector
*row
= Vector_Alloc(P
->Dimension
+2);
2175 value_set_si(row
->p
[0], 1);
2176 value_oppose(f
, P
->Constraint
[l1
][nvar
+i
+1]);
2177 Vector_Combine(P
->Constraint
[l1
]+1, P
->Constraint
[l2
]+1,
2179 P
->Constraint
[l2
][nvar
+i
+1], f
,
2181 ConstraintSimplify(row
->p
, row
->p
, P
->Dimension
+2, &f
);
2182 *pos
= AddConstraints(row
->p
, 1, P
, 0);
2183 value_set_si(f
, -1);
2184 Vector_Scale(row
->p
+1, row
->p
+1, f
, P
->Dimension
+1);
2185 value_decrement(row
->p
[P
->Dimension
+1], row
->p
[P
->Dimension
+1]);
2186 *neg
= AddConstraints(row
->p
, 1, P
, 0);
2190 return !emptyQ((*pos
)) && !emptyQ((*neg
));
2193 static bool double_bound(Polyhedron
*P
, int nvar
, int exist
,
2194 Polyhedron
**pos
, Polyhedron
**neg
)
2196 for (int i
= 0; i
< exist
; ++i
) {
2198 for (l1
= P
->NbEq
; l1
< P
->NbConstraints
; ++l1
) {
2199 if (value_negz_p(P
->Constraint
[l1
][nvar
+i
+1]))
2201 for (l2
= l1
+ 1; l2
< P
->NbConstraints
; ++l2
) {
2202 if (value_negz_p(P
->Constraint
[l2
][nvar
+i
+1]))
2204 if (double_bound_pair(P
, nvar
, exist
, i
, l1
, l2
, pos
, neg
))
2208 for (l1
= P
->NbEq
; l1
< P
->NbConstraints
; ++l1
) {
2209 if (value_posz_p(P
->Constraint
[l1
][nvar
+i
+1]))
2211 if (l1
< P
->NbConstraints
)
2212 for (l2
= l1
+ 1; l2
< P
->NbConstraints
; ++l2
) {
2213 if (value_posz_p(P
->Constraint
[l2
][nvar
+i
+1]))
2215 if (double_bound_pair(P
, nvar
, exist
, i
, l1
, l2
, pos
, neg
))
2227 INDEPENDENT
= 1 << 2,
2231 static evalue
* enumerate_or(Polyhedron
*D
,
2232 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
2235 fprintf(stderr
, "\nER: Or\n");
2236 #endif /* DEBUG_ER */
2238 Polyhedron
*N
= D
->next
;
2241 barvinok_enumerate_e(D
, exist
, nparam
, MaxRays
);
2244 for (D
= N
; D
; D
= N
) {
2249 barvinok_enumerate_e(D
, exist
, nparam
, MaxRays
);
2252 free_evalue_refs(EN
);
2262 static evalue
* enumerate_sum(Polyhedron
*P
,
2263 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
2265 int nvar
= P
->Dimension
- exist
- nparam
;
2266 int toswap
= nvar
< exist
? nvar
: exist
;
2267 for (int i
= 0; i
< toswap
; ++i
)
2268 SwapColumns(P
, 1 + i
, nvar
+exist
- i
);
2272 fprintf(stderr
, "\nER: Sum\n");
2273 #endif /* DEBUG_ER */
2275 evalue
*EP
= barvinok_enumerate_e(P
, exist
, nparam
, MaxRays
);
2277 for (int i
= 0; i
< /* nvar */ nparam
; ++i
) {
2278 Matrix
*C
= Matrix_Alloc(1, 1 + nparam
+ 1);
2279 value_set_si(C
->p
[0][0], 1);
2281 value_init(split
.d
);
2282 value_set_si(split
.d
, 0);
2283 split
.x
.p
= new_enode(partition
, 4, nparam
);
2284 value_set_si(C
->p
[0][1+i
], 1);
2285 Matrix
*C2
= Matrix_Copy(C
);
2286 EVALUE_SET_DOMAIN(split
.x
.p
->arr
[0],
2287 Constraints2Polyhedron(C2
, MaxRays
));
2289 evalue_set_si(&split
.x
.p
->arr
[1], 1, 1);
2290 value_set_si(C
->p
[0][1+i
], -1);
2291 value_set_si(C
->p
[0][1+nparam
], -1);
2292 EVALUE_SET_DOMAIN(split
.x
.p
->arr
[2],
2293 Constraints2Polyhedron(C
, MaxRays
));
2294 evalue_set_si(&split
.x
.p
->arr
[3], 1, 1);
2296 free_evalue_refs(&split
);
2300 evalue_range_reduction(EP
);
2302 evalue_frac2floor(EP
);
2304 evalue
*sum
= esum(EP
, nvar
);
2306 free_evalue_refs(EP
);
2310 evalue_range_reduction(EP
);
2315 static evalue
* split_sure(Polyhedron
*P
, Polyhedron
*S
,
2316 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
2318 int nvar
= P
->Dimension
- exist
- nparam
;
2320 Matrix
*M
= Matrix_Alloc(exist
, S
->Dimension
+2);
2321 for (int i
= 0; i
< exist
; ++i
)
2322 value_set_si(M
->p
[i
][nvar
+i
+1], 1);
2324 S
= DomainAddRays(S
, M
, MaxRays
);
2326 Polyhedron
*F
= DomainAddRays(P
, M
, MaxRays
);
2327 Polyhedron
*D
= DomainDifference(F
, S
, MaxRays
);
2329 D
= Disjoint_Domain(D
, 0, MaxRays
);
2334 M
= Matrix_Alloc(P
->Dimension
+1-exist
, P
->Dimension
+1);
2335 for (int j
= 0; j
< nvar
; ++j
)
2336 value_set_si(M
->p
[j
][j
], 1);
2337 for (int j
= 0; j
< nparam
+1; ++j
)
2338 value_set_si(M
->p
[nvar
+j
][nvar
+exist
+j
], 1);
2339 Polyhedron
*T
= Polyhedron_Image(S
, M
, MaxRays
);
2340 evalue
*EP
= barvinok_enumerate_e(T
, 0, nparam
, MaxRays
);
2345 for (Polyhedron
*Q
= D
; Q
; Q
= Q
->next
) {
2346 Polyhedron
*N
= Q
->next
;
2348 T
= DomainIntersection(P
, Q
, MaxRays
);
2349 evalue
*E
= barvinok_enumerate_e(T
, exist
, nparam
, MaxRays
);
2351 free_evalue_refs(E
);
2360 static evalue
* enumerate_sure(Polyhedron
*P
,
2361 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
2365 int nvar
= P
->Dimension
- exist
- nparam
;
2371 for (i
= 0; i
< exist
; ++i
) {
2372 Matrix
*M
= Matrix_Alloc(S
->NbConstraints
, S
->Dimension
+2);
2374 value_set_si(lcm
, 1);
2375 for (int j
= 0; j
< S
->NbConstraints
; ++j
) {
2376 if (value_negz_p(S
->Constraint
[j
][1+nvar
+i
]))
2378 if (value_one_p(S
->Constraint
[j
][1+nvar
+i
]))
2380 value_lcm(lcm
, S
->Constraint
[j
][1+nvar
+i
], &lcm
);
2383 for (int j
= 0; j
< S
->NbConstraints
; ++j
) {
2384 if (value_negz_p(S
->Constraint
[j
][1+nvar
+i
]))
2386 if (value_one_p(S
->Constraint
[j
][1+nvar
+i
]))
2388 value_division(f
, lcm
, S
->Constraint
[j
][1+nvar
+i
]);
2389 Vector_Scale(S
->Constraint
[j
], M
->p
[c
], f
, S
->Dimension
+2);
2390 value_subtract(M
->p
[c
][S
->Dimension
+1],
2391 M
->p
[c
][S
->Dimension
+1],
2393 value_increment(M
->p
[c
][S
->Dimension
+1],
2394 M
->p
[c
][S
->Dimension
+1]);
2398 S
= AddConstraints(M
->p
[0], c
, S
, MaxRays
);
2413 fprintf(stderr
, "\nER: Sure\n");
2414 #endif /* DEBUG_ER */
2416 return split_sure(P
, S
, exist
, nparam
, MaxRays
);
2419 static evalue
* enumerate_sure2(Polyhedron
*P
,
2420 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
2422 int nvar
= P
->Dimension
- exist
- nparam
;
2424 for (r
= 0; r
< P
->NbRays
; ++r
)
2425 if (value_one_p(P
->Ray
[r
][0]) &&
2426 value_one_p(P
->Ray
[r
][P
->Dimension
+1]))
2432 Matrix
*M
= Matrix_Alloc(nvar
+ 1 + nparam
, P
->Dimension
+2);
2433 for (int i
= 0; i
< nvar
; ++i
)
2434 value_set_si(M
->p
[i
][1+i
], 1);
2435 for (int i
= 0; i
< nparam
; ++i
)
2436 value_set_si(M
->p
[i
+nvar
][1+nvar
+exist
+i
], 1);
2437 Vector_Copy(P
->Ray
[r
]+1+nvar
, M
->p
[nvar
+nparam
]+1+nvar
, exist
);
2438 value_set_si(M
->p
[nvar
+nparam
][0], 1);
2439 value_set_si(M
->p
[nvar
+nparam
][P
->Dimension
+1], 1);
2440 Polyhedron
* F
= Rays2Polyhedron(M
, MaxRays
);
2443 Polyhedron
*I
= DomainIntersection(F
, P
, MaxRays
);
2447 fprintf(stderr
, "\nER: Sure2\n");
2448 #endif /* DEBUG_ER */
2450 return split_sure(P
, I
, exist
, nparam
, MaxRays
);
2453 static evalue
* enumerate_cyclic(Polyhedron
*P
,
2454 unsigned exist
, unsigned nparam
,
2455 evalue
* EP
, int r
, int p
, unsigned MaxRays
)
2457 int nvar
= P
->Dimension
- exist
- nparam
;
2459 /* If EP in its fractional maps only contains references
2460 * to the remainder parameter with appropriate coefficients
2461 * then we could in principle avoid adding existentially
2462 * quantified variables to the validity domains.
2463 * We'd have to replace the remainder by m { p/m }
2464 * and multiply with an appropriate factor that is one
2465 * only in the appropriate range.
2466 * This last multiplication can be avoided if EP
2467 * has a single validity domain with no (further)
2468 * constraints on the remainder parameter
2471 Matrix
*CT
= Matrix_Alloc(nparam
+1, nparam
+3);
2472 Matrix
*M
= Matrix_Alloc(1, 1+nparam
+3);
2473 for (int j
= 0; j
< nparam
; ++j
)
2475 value_set_si(CT
->p
[j
][j
], 1);
2476 value_set_si(CT
->p
[p
][nparam
+1], 1);
2477 value_set_si(CT
->p
[nparam
][nparam
+2], 1);
2478 value_set_si(M
->p
[0][1+p
], -1);
2479 value_absolute(M
->p
[0][1+nparam
], P
->Ray
[0][1+nvar
+exist
+p
]);
2480 value_set_si(M
->p
[0][1+nparam
+1], 1);
2481 Polyhedron
*CEq
= Constraints2Polyhedron(M
, 1);
2483 addeliminatedparams_enum(EP
, CT
, CEq
, MaxRays
, nparam
);
2484 Polyhedron_Free(CEq
);
2490 static void enumerate_vd_add_ray(evalue
*EP
, Matrix
*Rays
, unsigned MaxRays
)
2492 if (value_notzero_p(EP
->d
))
2495 assert(EP
->x
.p
->type
== partition
);
2496 assert(EP
->x
.p
->pos
== EVALUE_DOMAIN(EP
->x
.p
->arr
[0])->Dimension
);
2497 for (int i
= 0; i
< EP
->x
.p
->size
/2; ++i
) {
2498 Polyhedron
*D
= EVALUE_DOMAIN(EP
->x
.p
->arr
[2*i
]);
2499 Polyhedron
*N
= DomainAddRays(D
, Rays
, MaxRays
);
2500 EVALUE_SET_DOMAIN(EP
->x
.p
->arr
[2*i
], N
);
2505 static evalue
* enumerate_line(Polyhedron
*P
,
2506 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
2512 fprintf(stderr
, "\nER: Line\n");
2513 #endif /* DEBUG_ER */
2515 int nvar
= P
->Dimension
- exist
- nparam
;
2517 for (i
= 0; i
< nparam
; ++i
)
2518 if (value_notzero_p(P
->Ray
[0][1+nvar
+exist
+i
]))
2521 for (j
= i
+1; j
< nparam
; ++j
)
2522 if (value_notzero_p(P
->Ray
[0][1+nvar
+exist
+i
]))
2524 assert(j
>= nparam
); // for now
2526 Matrix
*M
= Matrix_Alloc(2, P
->Dimension
+2);
2527 value_set_si(M
->p
[0][0], 1);
2528 value_set_si(M
->p
[0][1+nvar
+exist
+i
], 1);
2529 value_set_si(M
->p
[1][0], 1);
2530 value_set_si(M
->p
[1][1+nvar
+exist
+i
], -1);
2531 value_absolute(M
->p
[1][1+P
->Dimension
], P
->Ray
[0][1+nvar
+exist
+i
]);
2532 value_decrement(M
->p
[1][1+P
->Dimension
], M
->p
[1][1+P
->Dimension
]);
2533 Polyhedron
*S
= AddConstraints(M
->p
[0], 2, P
, MaxRays
);
2534 evalue
*EP
= barvinok_enumerate_e(S
, exist
, nparam
, MaxRays
);
2538 return enumerate_cyclic(P
, exist
, nparam
, EP
, 0, i
, MaxRays
);
2541 static int single_param_pos(Polyhedron
*P
, unsigned exist
, unsigned nparam
,
2544 int nvar
= P
->Dimension
- exist
- nparam
;
2545 if (First_Non_Zero(P
->Ray
[r
]+1, nvar
) != -1)
2547 int i
= First_Non_Zero(P
->Ray
[r
]+1+nvar
+exist
, nparam
);
2550 if (First_Non_Zero(P
->Ray
[r
]+1+nvar
+exist
+1, nparam
-i
-1) != -1)
2555 static evalue
* enumerate_remove_ray(Polyhedron
*P
, int r
,
2556 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
2559 fprintf(stderr
, "\nER: RedundantRay\n");
2560 #endif /* DEBUG_ER */
2564 value_set_si(one
, 1);
2565 int len
= P
->NbRays
-1;
2566 Matrix
*M
= Matrix_Alloc(2 * len
, P
->Dimension
+2);
2567 Vector_Copy(P
->Ray
[0], M
->p
[0], r
* (P
->Dimension
+2));
2568 Vector_Copy(P
->Ray
[r
+1], M
->p
[r
], (len
-r
) * (P
->Dimension
+2));
2569 for (int j
= 0; j
< P
->NbRays
; ++j
) {
2572 Vector_Combine(P
->Ray
[j
], P
->Ray
[r
], M
->p
[len
+j
-(j
>r
)],
2573 one
, P
->Ray
[j
][P
->Dimension
+1], P
->Dimension
+2);
2576 P
= Rays2Polyhedron(M
, MaxRays
);
2578 evalue
*EP
= barvinok_enumerate_e(P
, exist
, nparam
, MaxRays
);
2585 static evalue
* enumerate_redundant_ray(Polyhedron
*P
,
2586 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
2588 assert(P
->NbBid
== 0);
2589 int nvar
= P
->Dimension
- exist
- nparam
;
2593 for (int r
= 0; r
< P
->NbRays
; ++r
) {
2594 if (value_notzero_p(P
->Ray
[r
][P
->Dimension
+1]))
2596 int i1
= single_param_pos(P
, exist
, nparam
, r
);
2599 for (int r2
= r
+1; r2
< P
->NbRays
; ++r2
) {
2600 if (value_notzero_p(P
->Ray
[r2
][P
->Dimension
+1]))
2602 int i2
= single_param_pos(P
, exist
, nparam
, r2
);
2608 value_division(m
, P
->Ray
[r
][1+nvar
+exist
+i1
],
2609 P
->Ray
[r2
][1+nvar
+exist
+i1
]);
2610 value_multiply(m
, m
, P
->Ray
[r2
][1+nvar
+exist
+i1
]);
2611 /* r2 divides r => r redundant */
2612 if (value_eq(m
, P
->Ray
[r
][1+nvar
+exist
+i1
])) {
2614 return enumerate_remove_ray(P
, r
, exist
, nparam
, MaxRays
);
2617 value_division(m
, P
->Ray
[r2
][1+nvar
+exist
+i1
],
2618 P
->Ray
[r
][1+nvar
+exist
+i1
]);
2619 value_multiply(m
, m
, P
->Ray
[r
][1+nvar
+exist
+i1
]);
2620 /* r divides r2 => r2 redundant */
2621 if (value_eq(m
, P
->Ray
[r2
][1+nvar
+exist
+i1
])) {
2623 return enumerate_remove_ray(P
, r2
, exist
, nparam
, MaxRays
);
2631 static Polyhedron
*upper_bound(Polyhedron
*P
,
2632 int pos
, Value
*max
, Polyhedron
**R
)
2641 for (Polyhedron
*Q
= P
; Q
; Q
= N
) {
2643 for (r
= 0; r
< P
->NbRays
; ++r
) {
2644 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1]) &&
2645 value_pos_p(P
->Ray
[r
][1+pos
]))
2648 if (r
< P
->NbRays
) {
2656 for (r
= 0; r
< P
->NbRays
; ++r
) {
2657 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1]))
2659 mpz_fdiv_q(v
, P
->Ray
[r
][1+pos
], P
->Ray
[r
][1+P
->Dimension
]);
2660 if ((!Q
->next
&& r
== 0) || value_gt(v
, *max
))
2661 value_assign(*max
, v
);
2668 static evalue
* enumerate_ray(Polyhedron
*P
,
2669 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
2671 assert(P
->NbBid
== 0);
2672 int nvar
= P
->Dimension
- exist
- nparam
;
2675 for (r
= 0; r
< P
->NbRays
; ++r
)
2676 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1]))
2682 for (r2
= r
+1; r2
< P
->NbRays
; ++r2
)
2683 if (value_zero_p(P
->Ray
[r2
][P
->Dimension
+1]))
2685 if (r2
< P
->NbRays
) {
2687 return enumerate_sum(P
, exist
, nparam
, MaxRays
);
2691 fprintf(stderr
, "\nER: Ray\n");
2692 #endif /* DEBUG_ER */
2698 value_set_si(one
, 1);
2699 int i
= single_param_pos(P
, exist
, nparam
, r
);
2700 assert(i
!= -1); // for now;
2702 Matrix
*M
= Matrix_Alloc(P
->NbRays
, P
->Dimension
+2);
2703 for (int j
= 0; j
< P
->NbRays
; ++j
) {
2704 Vector_Combine(P
->Ray
[j
], P
->Ray
[r
], M
->p
[j
],
2705 one
, P
->Ray
[j
][P
->Dimension
+1], P
->Dimension
+2);
2707 Polyhedron
*S
= Rays2Polyhedron(M
, MaxRays
);
2709 Polyhedron
*D
= DomainDifference(P
, S
, MaxRays
);
2711 // Polyhedron_Print(stderr, P_VALUE_FMT, D);
2712 assert(value_pos_p(P
->Ray
[r
][1+nvar
+exist
+i
])); // for now
2714 D
= upper_bound(D
, nvar
+exist
+i
, &m
, &R
);
2718 M
= Matrix_Alloc(2, P
->Dimension
+2);
2719 value_set_si(M
->p
[0][0], 1);
2720 value_set_si(M
->p
[1][0], 1);
2721 value_set_si(M
->p
[0][1+nvar
+exist
+i
], -1);
2722 value_set_si(M
->p
[1][1+nvar
+exist
+i
], 1);
2723 value_assign(M
->p
[0][1+P
->Dimension
], m
);
2724 value_oppose(M
->p
[1][1+P
->Dimension
], m
);
2725 value_addto(M
->p
[1][1+P
->Dimension
], M
->p
[1][1+P
->Dimension
],
2726 P
->Ray
[r
][1+nvar
+exist
+i
]);
2727 value_decrement(M
->p
[1][1+P
->Dimension
], M
->p
[1][1+P
->Dimension
]);
2728 // Matrix_Print(stderr, P_VALUE_FMT, M);
2729 D
= AddConstraints(M
->p
[0], 2, P
, MaxRays
);
2730 // Polyhedron_Print(stderr, P_VALUE_FMT, D);
2731 value_subtract(M
->p
[0][1+P
->Dimension
], M
->p
[0][1+P
->Dimension
],
2732 P
->Ray
[r
][1+nvar
+exist
+i
]);
2733 // Matrix_Print(stderr, P_VALUE_FMT, M);
2734 S
= AddConstraints(M
->p
[0], 1, P
, MaxRays
);
2735 // Polyhedron_Print(stderr, P_VALUE_FMT, S);
2738 evalue
*EP
= barvinok_enumerate_e(D
, exist
, nparam
, MaxRays
);
2743 if (value_notone_p(P
->Ray
[r
][1+nvar
+exist
+i
]))
2744 EP
= enumerate_cyclic(P
, exist
, nparam
, EP
, r
, i
, MaxRays
);
2746 M
= Matrix_Alloc(1, nparam
+2);
2747 value_set_si(M
->p
[0][0], 1);
2748 value_set_si(M
->p
[0][1+i
], 1);
2749 enumerate_vd_add_ray(EP
, M
, MaxRays
);
2754 evalue
*E
= barvinok_enumerate_e(S
, exist
, nparam
, MaxRays
);
2756 free_evalue_refs(E
);
2763 evalue
*ER
= enumerate_or(R
, exist
, nparam
, MaxRays
);
2765 free_evalue_refs(ER
);
2772 static evalue
* enumerate_vd(Polyhedron
**PA
,
2773 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
2775 Polyhedron
*P
= *PA
;
2776 int nvar
= P
->Dimension
- exist
- nparam
;
2777 Param_Polyhedron
*PP
= NULL
;
2778 Polyhedron
*C
= Universe_Polyhedron(nparam
);
2782 PP
= Polyhedron2Param_SimplifiedDomain(&PR
,C
,MaxRays
,&CEq
,&CT
);
2786 Param_Domain
*D
, *last
;
2789 for (nd
= 0, D
=PP
->D
; D
; D
=D
->next
, ++nd
)
2792 Polyhedron
**VD
= new Polyhedron_p
[nd
];
2793 Polyhedron
**fVD
= new Polyhedron_p
[nd
];
2794 for(nd
= 0, D
=PP
->D
; D
; D
=D
->next
) {
2795 Polyhedron
*rVD
= reduce_domain(D
->Domain
, CT
, CEq
,
2809 /* This doesn't seem to have any effect */
2811 Polyhedron
*CA
= align_context(VD
[0], P
->Dimension
, MaxRays
);
2813 P
= DomainIntersection(P
, CA
, MaxRays
);
2816 Polyhedron_Free(CA
);
2821 if (!EP
&& CT
->NbColumns
!= CT
->NbRows
) {
2822 Polyhedron
*CEqr
= DomainImage(CEq
, CT
, MaxRays
);
2823 Polyhedron
*CA
= align_context(CEqr
, PR
->Dimension
, MaxRays
);
2824 Polyhedron
*I
= DomainIntersection(PR
, CA
, MaxRays
);
2825 Polyhedron_Free(CEqr
);
2826 Polyhedron_Free(CA
);
2828 fprintf(stderr
, "\nER: Eliminate\n");
2829 #endif /* DEBUG_ER */
2830 nparam
-= CT
->NbColumns
- CT
->NbRows
;
2831 EP
= barvinok_enumerate_e(I
, exist
, nparam
, MaxRays
);
2832 nparam
+= CT
->NbColumns
- CT
->NbRows
;
2833 addeliminatedparams_enum(EP
, CT
, CEq
, MaxRays
, nparam
);
2837 Polyhedron_Free(PR
);
2840 if (!EP
&& nd
> 1) {
2842 fprintf(stderr
, "\nER: VD\n");
2843 #endif /* DEBUG_ER */
2844 for (int i
= 0; i
< nd
; ++i
) {
2845 Polyhedron
*CA
= align_context(VD
[i
], P
->Dimension
, MaxRays
);
2846 Polyhedron
*I
= DomainIntersection(P
, CA
, MaxRays
);
2849 EP
= barvinok_enumerate_e(I
, exist
, nparam
, MaxRays
);
2851 evalue
*E
= barvinok_enumerate_e(I
, exist
, nparam
, MaxRays
);
2853 free_evalue_refs(E
);
2857 Polyhedron_Free(CA
);
2861 for (int i
= 0; i
< nd
; ++i
) {
2862 Polyhedron_Free(VD
[i
]);
2863 Polyhedron_Free(fVD
[i
]);
2869 if (!EP
&& nvar
== 0) {
2872 Param_Vertices
*V
, *V2
;
2873 Matrix
* M
= Matrix_Alloc(1, P
->Dimension
+2);
2875 FORALL_PVertex_in_ParamPolyhedron(V
, last
, PP
) {
2877 FORALL_PVertex_in_ParamPolyhedron(V2
, last
, PP
) {
2884 for (int i
= 0; i
< exist
; ++i
) {
2885 value_oppose(f
, V
->Vertex
->p
[i
][nparam
+1]);
2886 Vector_Combine(V
->Vertex
->p
[i
],
2888 M
->p
[0] + 1 + nvar
+ exist
,
2889 V2
->Vertex
->p
[i
][nparam
+1],
2893 for (j
= 0; j
< nparam
; ++j
)
2894 if (value_notzero_p(M
->p
[0][1+nvar
+exist
+j
]))
2898 ConstraintSimplify(M
->p
[0], M
->p
[0],
2899 P
->Dimension
+2, &f
);
2900 value_set_si(M
->p
[0][0], 0);
2901 Polyhedron
*para
= AddConstraints(M
->p
[0], 1, P
,
2904 Polyhedron_Free(para
);
2907 Polyhedron
*pos
, *neg
;
2908 value_set_si(M
->p
[0][0], 1);
2909 value_decrement(M
->p
[0][P
->Dimension
+1],
2910 M
->p
[0][P
->Dimension
+1]);
2911 neg
= AddConstraints(M
->p
[0], 1, P
, MaxRays
);
2912 value_set_si(f
, -1);
2913 Vector_Scale(M
->p
[0]+1, M
->p
[0]+1, f
,
2915 value_decrement(M
->p
[0][P
->Dimension
+1],
2916 M
->p
[0][P
->Dimension
+1]);
2917 value_decrement(M
->p
[0][P
->Dimension
+1],
2918 M
->p
[0][P
->Dimension
+1]);
2919 pos
= AddConstraints(M
->p
[0], 1, P
, MaxRays
);
2920 if (emptyQ(neg
) && emptyQ(pos
)) {
2921 Polyhedron_Free(para
);
2922 Polyhedron_Free(pos
);
2923 Polyhedron_Free(neg
);
2927 fprintf(stderr
, "\nER: Order\n");
2928 #endif /* DEBUG_ER */
2929 EP
= barvinok_enumerate_e(para
, exist
, nparam
, MaxRays
);
2932 E
= barvinok_enumerate_e(pos
, exist
, nparam
, MaxRays
);
2934 free_evalue_refs(E
);
2938 E
= barvinok_enumerate_e(neg
, exist
, nparam
, MaxRays
);
2940 free_evalue_refs(E
);
2943 Polyhedron_Free(para
);
2944 Polyhedron_Free(pos
);
2945 Polyhedron_Free(neg
);
2950 } END_FORALL_PVertex_in_ParamPolyhedron
;
2953 } END_FORALL_PVertex_in_ParamPolyhedron
;
2956 /* Search for vertex coordinate to split on */
2957 /* First look for one independent of the parameters */
2958 FORALL_PVertex_in_ParamPolyhedron(V
, last
, PP
) {
2959 for (int i
= 0; i
< exist
; ++i
) {
2961 for (j
= 0; j
< nparam
; ++j
)
2962 if (value_notzero_p(V
->Vertex
->p
[i
][j
]))
2966 value_set_si(M
->p
[0][0], 1);
2967 Vector_Set(M
->p
[0]+1, 0, nvar
+exist
);
2968 Vector_Copy(V
->Vertex
->p
[i
],
2969 M
->p
[0] + 1 + nvar
+ exist
, nparam
+1);
2970 value_oppose(M
->p
[0][1+nvar
+i
],
2971 V
->Vertex
->p
[i
][nparam
+1]);
2973 Polyhedron
*pos
, *neg
;
2974 value_set_si(M
->p
[0][0], 1);
2975 value_decrement(M
->p
[0][P
->Dimension
+1],
2976 M
->p
[0][P
->Dimension
+1]);
2977 neg
= AddConstraints(M
->p
[0], 1, P
, MaxRays
);
2978 value_set_si(f
, -1);
2979 Vector_Scale(M
->p
[0]+1, M
->p
[0]+1, f
,
2981 value_decrement(M
->p
[0][P
->Dimension
+1],
2982 M
->p
[0][P
->Dimension
+1]);
2983 value_decrement(M
->p
[0][P
->Dimension
+1],
2984 M
->p
[0][P
->Dimension
+1]);
2985 pos
= AddConstraints(M
->p
[0], 1, P
, MaxRays
);
2986 if (emptyQ(neg
) || emptyQ(pos
)) {
2987 Polyhedron_Free(pos
);
2988 Polyhedron_Free(neg
);
2991 Polyhedron_Free(pos
);
2992 value_increment(M
->p
[0][P
->Dimension
+1],
2993 M
->p
[0][P
->Dimension
+1]);
2994 pos
= AddConstraints(M
->p
[0], 1, P
, MaxRays
);
2996 fprintf(stderr
, "\nER: Vertex\n");
2997 #endif /* DEBUG_ER */
2999 EP
= enumerate_or(pos
, exist
, nparam
, MaxRays
);
3004 } END_FORALL_PVertex_in_ParamPolyhedron
;
3008 /* Search for vertex coordinate to split on */
3009 /* Now look for one that depends on the parameters */
3010 FORALL_PVertex_in_ParamPolyhedron(V
, last
, PP
) {
3011 for (int i
= 0; i
< exist
; ++i
) {
3012 value_set_si(M
->p
[0][0], 1);
3013 Vector_Set(M
->p
[0]+1, 0, nvar
+exist
);
3014 Vector_Copy(V
->Vertex
->p
[i
],
3015 M
->p
[0] + 1 + nvar
+ exist
, nparam
+1);
3016 value_oppose(M
->p
[0][1+nvar
+i
],
3017 V
->Vertex
->p
[i
][nparam
+1]);
3019 Polyhedron
*pos
, *neg
;
3020 value_set_si(M
->p
[0][0], 1);
3021 value_decrement(M
->p
[0][P
->Dimension
+1],
3022 M
->p
[0][P
->Dimension
+1]);
3023 neg
= AddConstraints(M
->p
[0], 1, P
, MaxRays
);
3024 value_set_si(f
, -1);
3025 Vector_Scale(M
->p
[0]+1, M
->p
[0]+1, f
,
3027 value_decrement(M
->p
[0][P
->Dimension
+1],
3028 M
->p
[0][P
->Dimension
+1]);
3029 value_decrement(M
->p
[0][P
->Dimension
+1],
3030 M
->p
[0][P
->Dimension
+1]);
3031 pos
= AddConstraints(M
->p
[0], 1, P
, MaxRays
);
3032 if (emptyQ(neg
) || emptyQ(pos
)) {
3033 Polyhedron_Free(pos
);
3034 Polyhedron_Free(neg
);
3037 Polyhedron_Free(pos
);
3038 value_increment(M
->p
[0][P
->Dimension
+1],
3039 M
->p
[0][P
->Dimension
+1]);
3040 pos
= AddConstraints(M
->p
[0], 1, P
, MaxRays
);
3042 fprintf(stderr
, "\nER: ParamVertex\n");
3043 #endif /* DEBUG_ER */
3045 EP
= enumerate_or(pos
, exist
, nparam
, MaxRays
);
3050 } END_FORALL_PVertex_in_ParamPolyhedron
;
3058 Polyhedron_Free(CEq
);
3062 Param_Polyhedron_Free(PP
);
3069 evalue
*barvinok_enumerate_pip(Polyhedron
*P
,
3070 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
3075 evalue
*barvinok_enumerate_pip(Polyhedron
*P
,
3076 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
3078 int nvar
= P
->Dimension
- exist
- nparam
;
3079 evalue
*EP
= evalue_zero();
3083 fprintf(stderr
, "\nER: PIP\n");
3084 #endif /* DEBUG_ER */
3086 Polyhedron
*D
= pip_projectout(P
, nvar
, exist
, nparam
);
3087 for (Q
= D
; Q
; Q
= N
) {
3091 exist
= Q
->Dimension
- nvar
- nparam
;
3092 E
= barvinok_enumerate_e(Q
, exist
, nparam
, MaxRays
);
3095 free_evalue_refs(E
);
3104 static bool is_single(Value
*row
, int pos
, int len
)
3106 return First_Non_Zero(row
, pos
) == -1 &&
3107 First_Non_Zero(row
+pos
+1, len
-pos
-1) == -1;
3110 static evalue
* barvinok_enumerate_e_r(Polyhedron
*P
,
3111 unsigned exist
, unsigned nparam
, unsigned MaxRays
);
3114 static int er_level
= 0;
3116 evalue
* barvinok_enumerate_e(Polyhedron
*P
,
3117 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
3119 fprintf(stderr
, "\nER: level %i\n", er_level
);
3121 Polyhedron_PrintConstraints(stderr
, P_VALUE_FMT
, P
);
3122 fprintf(stderr
, "\nE %d\nP %d\n", exist
, nparam
);
3124 P
= DomainConstraintSimplify(Polyhedron_Copy(P
), MaxRays
);
3125 evalue
*EP
= barvinok_enumerate_e_r(P
, exist
, nparam
, MaxRays
);
3131 evalue
* barvinok_enumerate_e(Polyhedron
*P
,
3132 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
3134 P
= DomainConstraintSimplify(Polyhedron_Copy(P
), MaxRays
);
3135 evalue
*EP
= barvinok_enumerate_e_r(P
, exist
, nparam
, MaxRays
);
3141 static evalue
* barvinok_enumerate_e_r(Polyhedron
*P
,
3142 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
3145 Polyhedron
*U
= Universe_Polyhedron(nparam
);
3146 evalue
*EP
= barvinok_enumerate_ev(P
, U
, MaxRays
);
3147 //char *param_name[] = {"P", "Q", "R", "S", "T" };
3148 //print_evalue(stdout, EP, param_name);
3153 int nvar
= P
->Dimension
- exist
- nparam
;
3154 int len
= P
->Dimension
+ 2;
3157 POL_ENSURE_FACETS(P
);
3158 POL_ENSURE_VERTICES(P
);
3161 return evalue_zero();
3163 if (nvar
== 0 && nparam
== 0) {
3164 evalue
*EP
= evalue_zero();
3165 barvinok_count(P
, &EP
->x
.n
, MaxRays
);
3166 if (value_pos_p(EP
->x
.n
))
3167 value_set_si(EP
->x
.n
, 1);
3172 for (r
= 0; r
< P
->NbRays
; ++r
)
3173 if (value_zero_p(P
->Ray
[r
][0]) ||
3174 value_zero_p(P
->Ray
[r
][P
->Dimension
+1])) {
3176 for (i
= 0; i
< nvar
; ++i
)
3177 if (value_notzero_p(P
->Ray
[r
][i
+1]))
3181 for (i
= nvar
+ exist
; i
< nvar
+ exist
+ nparam
; ++i
)
3182 if (value_notzero_p(P
->Ray
[r
][i
+1]))
3184 if (i
>= nvar
+ exist
+ nparam
)
3187 if (r
< P
->NbRays
) {
3188 evalue
*EP
= evalue_zero();
3189 value_set_si(EP
->x
.n
, -1);
3194 for (r
= 0; r
< P
->NbEq
; ++r
)
3195 if ((first
= First_Non_Zero(P
->Constraint
[r
]+1+nvar
, exist
)) != -1)
3198 if (First_Non_Zero(P
->Constraint
[r
]+1+nvar
+first
+1,
3199 exist
-first
-1) != -1) {
3200 Polyhedron
*T
= rotate_along(P
, r
, nvar
, exist
, MaxRays
);
3202 fprintf(stderr
, "\nER: Equality\n");
3203 #endif /* DEBUG_ER */
3204 evalue
*EP
= barvinok_enumerate_e(T
, exist
-1, nparam
, MaxRays
);
3209 fprintf(stderr
, "\nER: Fixed\n");
3210 #endif /* DEBUG_ER */
3212 return barvinok_enumerate_e(P
, exist
-1, nparam
, MaxRays
);
3214 Polyhedron
*T
= Polyhedron_Copy(P
);
3215 SwapColumns(T
, nvar
+1, nvar
+1+first
);
3216 evalue
*EP
= barvinok_enumerate_e(T
, exist
-1, nparam
, MaxRays
);
3223 Vector
*row
= Vector_Alloc(len
);
3224 value_set_si(row
->p
[0], 1);
3229 enum constraint
* info
= new constraint
[exist
];
3230 for (int i
= 0; i
< exist
; ++i
) {
3232 for (int l
= P
->NbEq
; l
< P
->NbConstraints
; ++l
) {
3233 if (value_negz_p(P
->Constraint
[l
][nvar
+i
+1]))
3235 bool l_parallel
= is_single(P
->Constraint
[l
]+nvar
+1, i
, exist
);
3236 for (int u
= P
->NbEq
; u
< P
->NbConstraints
; ++u
) {
3237 if (value_posz_p(P
->Constraint
[u
][nvar
+i
+1]))
3239 bool lu_parallel
= l_parallel
||
3240 is_single(P
->Constraint
[u
]+nvar
+1, i
, exist
);
3241 value_oppose(f
, P
->Constraint
[u
][nvar
+i
+1]);
3242 Vector_Combine(P
->Constraint
[l
]+1, P
->Constraint
[u
]+1, row
->p
+1,
3243 f
, P
->Constraint
[l
][nvar
+i
+1], len
-1);
3244 if (!(info
[i
] & INDEPENDENT
)) {
3246 for (j
= 0; j
< exist
; ++j
)
3247 if (j
!= i
&& value_notzero_p(row
->p
[nvar
+j
+1]))
3250 //printf("independent: i: %d, l: %d, u: %d\n", i, l, u);
3251 info
[i
] = (constraint
)(info
[i
] | INDEPENDENT
);
3254 if (info
[i
] & ALL_POS
) {
3255 value_addto(row
->p
[len
-1], row
->p
[len
-1],
3256 P
->Constraint
[l
][nvar
+i
+1]);
3257 value_addto(row
->p
[len
-1], row
->p
[len
-1], f
);
3258 value_multiply(f
, f
, P
->Constraint
[l
][nvar
+i
+1]);
3259 value_subtract(row
->p
[len
-1], row
->p
[len
-1], f
);
3260 value_decrement(row
->p
[len
-1], row
->p
[len
-1]);
3261 ConstraintSimplify(row
->p
, row
->p
, len
, &f
);
3262 value_set_si(f
, -1);
3263 Vector_Scale(row
->p
+1, row
->p
+1, f
, len
-1);
3264 value_decrement(row
->p
[len
-1], row
->p
[len
-1]);
3265 Polyhedron
*T
= AddConstraints(row
->p
, 1, P
, MaxRays
);
3267 //printf("not all_pos: i: %d, l: %d, u: %d\n", i, l, u);
3268 info
[i
] = (constraint
)(info
[i
] ^ ALL_POS
);
3270 //puts("pos remainder");
3271 //Polyhedron_Print(stdout, P_VALUE_FMT, T);
3274 if (!(info
[i
] & ONE_NEG
)) {
3276 negative_test_constraint(P
->Constraint
[l
],
3278 row
->p
, nvar
+i
, len
, &f
);
3279 oppose_constraint(row
->p
, len
, &f
);
3280 Polyhedron
*T
= AddConstraints(row
->p
, 1, P
, MaxRays
);
3282 //printf("one_neg i: %d, l: %d, u: %d\n", i, l, u);
3283 info
[i
] = (constraint
)(info
[i
] | ONE_NEG
);
3285 //puts("neg remainder");
3286 //Polyhedron_Print(stdout, P_VALUE_FMT, T);
3288 } else if (!(info
[i
] & ROT_NEG
)) {
3289 if (parallel_constraints(P
->Constraint
[l
],
3291 row
->p
, nvar
, exist
)) {
3292 negative_test_constraint7(P
->Constraint
[l
],
3294 row
->p
, nvar
, exist
,
3296 oppose_constraint(row
->p
, len
, &f
);
3297 Polyhedron
*T
= AddConstraints(row
->p
, 1, P
, MaxRays
);
3299 // printf("rot_neg i: %d, l: %d, u: %d\n", i, l, u);
3300 info
[i
] = (constraint
)(info
[i
] | ROT_NEG
);
3303 //puts("neg remainder");
3304 //Polyhedron_Print(stdout, P_VALUE_FMT, T);
3309 if (!(info
[i
] & ALL_POS
) && (info
[i
] & (ONE_NEG
| ROT_NEG
)))
3313 if (info
[i
] & ALL_POS
)
3320 for (int i = 0; i < exist; ++i)
3321 printf("%i: %i\n", i, info[i]);
3323 for (int i
= 0; i
< exist
; ++i
)
3324 if (info
[i
] & ALL_POS
) {
3326 fprintf(stderr
, "\nER: Positive\n");
3327 #endif /* DEBUG_ER */
3329 // Maybe we should chew off some of the fat here
3330 Matrix
*M
= Matrix_Alloc(P
->Dimension
, P
->Dimension
+1);
3331 for (int j
= 0; j
< P
->Dimension
; ++j
)
3332 value_set_si(M
->p
[j
][j
+ (j
>= i
+nvar
)], 1);
3333 Polyhedron
*T
= Polyhedron_Image(P
, M
, MaxRays
);
3335 evalue
*EP
= barvinok_enumerate_e(T
, exist
-1, nparam
, MaxRays
);
3342 for (int i
= 0; i
< exist
; ++i
)
3343 if (info
[i
] & ONE_NEG
) {
3345 fprintf(stderr
, "\nER: Negative\n");
3346 #endif /* DEBUG_ER */
3351 return barvinok_enumerate_e(P
, exist
-1, nparam
, MaxRays
);
3353 Polyhedron
*T
= Polyhedron_Copy(P
);
3354 SwapColumns(T
, nvar
+1, nvar
+1+i
);
3355 evalue
*EP
= barvinok_enumerate_e(T
, exist
-1, nparam
, MaxRays
);
3360 for (int i
= 0; i
< exist
; ++i
)
3361 if (info
[i
] & ROT_NEG
) {
3363 fprintf(stderr
, "\nER: Rotate\n");
3364 #endif /* DEBUG_ER */
3368 Polyhedron
*T
= rotate_along(P
, r
, nvar
, exist
, MaxRays
);
3369 evalue
*EP
= barvinok_enumerate_e(T
, exist
-1, nparam
, MaxRays
);
3373 for (int i
= 0; i
< exist
; ++i
)
3374 if (info
[i
] & INDEPENDENT
) {
3375 Polyhedron
*pos
, *neg
;
3377 /* Find constraint again and split off negative part */
3379 if (SplitOnVar(P
, i
, nvar
, exist
, MaxRays
,
3380 row
, f
, true, &pos
, &neg
)) {
3382 fprintf(stderr
, "\nER: Split\n");
3383 #endif /* DEBUG_ER */
3386 barvinok_enumerate_e(neg
, exist
-1, nparam
, MaxRays
);
3388 barvinok_enumerate_e(pos
, exist
, nparam
, MaxRays
);
3390 free_evalue_refs(E
);
3392 Polyhedron_Free(neg
);
3393 Polyhedron_Free(pos
);
3407 EP
= enumerate_line(P
, exist
, nparam
, MaxRays
);
3411 EP
= barvinok_enumerate_pip(P
, exist
, nparam
, MaxRays
);
3415 EP
= enumerate_redundant_ray(P
, exist
, nparam
, MaxRays
);
3419 EP
= enumerate_sure(P
, exist
, nparam
, MaxRays
);
3423 EP
= enumerate_ray(P
, exist
, nparam
, MaxRays
);
3427 EP
= enumerate_sure2(P
, exist
, nparam
, MaxRays
);
3431 F
= unfringe(P
, MaxRays
);
3432 if (!PolyhedronIncludes(F
, P
)) {
3434 fprintf(stderr
, "\nER: Fringed\n");
3435 #endif /* DEBUG_ER */
3436 EP
= barvinok_enumerate_e(F
, exist
, nparam
, MaxRays
);
3443 EP
= enumerate_vd(&P
, exist
, nparam
, MaxRays
);
3448 EP
= enumerate_sum(P
, exist
, nparam
, MaxRays
);
3455 Polyhedron
*pos
, *neg
;
3456 for (i
= 0; i
< exist
; ++i
)
3457 if (SplitOnVar(P
, i
, nvar
, exist
, MaxRays
,
3458 row
, f
, false, &pos
, &neg
))
3464 EP
= enumerate_or(pos
, exist
, nparam
, MaxRays
);
3477 * remove equalities that require a "compression" of the parameters
3479 #ifndef HAVE_COMPRESS_PARMS
3480 static Polyhedron
*remove_more_equalities(Polyhedron
*P
, unsigned nparam
,
3481 Matrix
**CP
, unsigned MaxRays
)
3486 static Polyhedron
*remove_more_equalities(Polyhedron
*P
, unsigned nparam
,
3487 Matrix
**CP
, unsigned MaxRays
)
3494 /* compress_parms doesn't like equalities that only involve parameters */
3495 for (i
= 0; i
< P
->NbEq
; ++i
)
3496 if (First_Non_Zero(P
->Constraint
[i
]+1, P
->Dimension
-nparam
) == -1)
3500 Matrix
*M
= Matrix_Alloc(P
->NbEq
, 1+nparam
+1);
3502 for (; i
< P
->NbEq
; ++i
) {
3503 if (First_Non_Zero(P
->Constraint
[i
]+1, P
->Dimension
-nparam
) == -1)
3504 Vector_Copy(P
->Constraint
[i
]+1+P
->Dimension
-nparam
,
3505 M
->p
[n
++]+1, nparam
+1);
3508 CV
= compress_variables(M
, 0);
3509 T
= align_matrix(CV
, P
->Dimension
+1);
3510 Q
= Polyhedron_Preimage(P
, T
, MaxRays
);
3515 nparam
= CV
->NbColumns
-1;
3523 M
= Matrix_Alloc(P
->NbEq
, P
->Dimension
+2);
3524 Vector_Copy(P
->Constraint
[0], M
->p
[0], P
->NbEq
* (P
->Dimension
+2));
3525 *CP
= compress_parms(M
, nparam
);
3526 T
= align_matrix(*CP
, P
->Dimension
+1);
3527 Q
= Polyhedron_Preimage(P
, T
, MaxRays
);
3530 P
= remove_equalities_p(P
, P
->Dimension
-nparam
, NULL
);
3536 *CP
= Matrix_Alloc(CV
->NbRows
, T
->NbColumns
);
3537 Matrix_Product(CV
, T
, *CP
);
3547 static gen_fun
*series(Polyhedron
*P
, unsigned nparam
, barvinok_options
*options
)
3557 assert(!Polyhedron_is_infinite_param(P
, nparam
));
3558 assert(P
->NbBid
== 0);
3559 assert(Polyhedron_has_positive_rays(P
, nparam
));
3561 P
= remove_equalities_p(P
, P
->Dimension
-nparam
, NULL
);
3563 P
= remove_more_equalities(P
, nparam
, &CP
, options
->MaxRays
);
3564 assert(P
->NbEq
== 0);
3566 nparam
= CP
->NbColumns
-1;
3571 barvinok_count(P
, &c
, options
->MaxRays
);
3572 gf
= new gen_fun(c
);
3576 red
= gf_base::create(Polyhedron_Project(P
, nparam
),
3577 P
->Dimension
, nparam
, options
);
3578 POL_ENSURE_VERTICES(P
);
3579 red
->start_gf(P
, options
);
3591 gen_fun
* barvinok_series_with_options(Polyhedron
*P
, Polyhedron
* C
,
3592 barvinok_options
*options
)
3595 unsigned nparam
= C
->Dimension
;
3598 CA
= align_context(C
, P
->Dimension
, options
->MaxRays
);
3599 P
= DomainIntersection(P
, CA
, options
->MaxRays
);
3600 Polyhedron_Free(CA
);
3602 gf
= series(P
, nparam
, options
);
3607 gen_fun
* barvinok_series(Polyhedron
*P
, Polyhedron
* C
, unsigned MaxRays
)
3610 barvinok_options
*options
= barvinok_options_new_with_defaults();
3611 options
->MaxRays
= MaxRays
;
3612 gf
= barvinok_series_with_options(P
, C
, options
);
3617 static Polyhedron
*skew_into_positive_orthant(Polyhedron
*D
, unsigned nparam
,
3623 for (Polyhedron
*P
= D
; P
; P
= P
->next
) {
3624 POL_ENSURE_VERTICES(P
);
3625 assert(!Polyhedron_is_infinite_param(P
, nparam
));
3626 assert(P
->NbBid
== 0);
3627 assert(Polyhedron_has_positive_rays(P
, nparam
));
3629 for (int r
= 0; r
< P
->NbRays
; ++r
) {
3630 if (value_notzero_p(P
->Ray
[r
][P
->Dimension
+1]))
3632 for (int i
= 0; i
< nparam
; ++i
) {
3634 if (value_posz_p(P
->Ray
[r
][i
+1]))
3637 M
= Matrix_Alloc(D
->Dimension
+1, D
->Dimension
+1);
3638 for (int i
= 0; i
< D
->Dimension
+1; ++i
)
3639 value_set_si(M
->p
[i
][i
], 1);
3641 Inner_Product(P
->Ray
[r
]+1, M
->p
[i
], D
->Dimension
+1, &tmp
);
3642 if (value_posz_p(tmp
))
3645 for (j
= P
->Dimension
- nparam
; j
< P
->Dimension
; ++j
)
3646 if (value_pos_p(P
->Ray
[r
][j
+1]))
3648 assert(j
< P
->Dimension
);
3649 value_pdivision(tmp
, P
->Ray
[r
][j
+1], P
->Ray
[r
][i
+1]);
3650 value_subtract(M
->p
[i
][j
], M
->p
[i
][j
], tmp
);
3656 D
= DomainImage(D
, M
, MaxRays
);
3662 gen_fun
* barvinok_enumerate_union_series_with_options(Polyhedron
*D
, Polyhedron
* C
,
3663 barvinok_options
*options
)
3665 Polyhedron
*conv
, *D2
;
3667 gen_fun
*gf
= NULL
, *gf2
;
3668 unsigned nparam
= C
->Dimension
;
3673 CA
= align_context(C
, D
->Dimension
, options
->MaxRays
);
3674 D
= DomainIntersection(D
, CA
, options
->MaxRays
);
3675 Polyhedron_Free(CA
);
3677 D2
= skew_into_positive_orthant(D
, nparam
, options
->MaxRays
);
3678 for (Polyhedron
*P
= D2
; P
; P
= P
->next
) {
3679 assert(P
->Dimension
== D2
->Dimension
);
3682 P_gf
= series(Polyhedron_Copy(P
), nparam
, options
);
3686 gf
->add_union(P_gf
, options
);
3690 /* we actually only need the convex union of the parameter space
3691 * but the reducer classes currently expect a polyhedron in
3692 * the combined space
3694 Polyhedron_Free(gf
->context
);
3695 gf
->context
= DomainConvex(D2
, options
->MaxRays
);
3697 gf2
= gf
->summate(D2
->Dimension
- nparam
, options
);
3706 gen_fun
* barvinok_enumerate_union_series(Polyhedron
*D
, Polyhedron
* C
,
3710 barvinok_options
*options
= barvinok_options_new_with_defaults();
3711 options
->MaxRays
= MaxRays
;
3712 gf
= barvinok_enumerate_union_series_with_options(D
, C
, options
);
3717 evalue
* barvinok_enumerate_union(Polyhedron
*D
, Polyhedron
* C
, unsigned MaxRays
)
3720 gen_fun
*gf
= barvinok_enumerate_union_series(D
, C
, MaxRays
);