8 #include <NTL/mat_ZZ.h>
10 #include <barvinok/util.h>
11 #include <barvinok/evalue.h>
16 #include <barvinok/barvinok.h>
17 #include <barvinok/genfun.h>
18 #include <barvinok/options.h>
19 #include <barvinok/sample.h>
20 #include "conversion.h"
21 #include "decomposer.h"
22 #include "lattice_point.h"
23 #include "reduce_domain.h"
24 #include "genfun_constructor.h"
25 #include "remove_equalities.h"
36 using std::ostringstream
;
38 #define ALLOC(t,p) p = (t*)malloc(sizeof(*p))
46 dpoly_n(int d
, ZZ
& degree_0
, ZZ
& degree_1
, int offset
= 0) {
50 zz2value(degree_0
, d0
);
51 zz2value(degree_1
, d1
);
52 coeff
= Matrix_Alloc(d
+1, d
+1+1);
53 value_set_si(coeff
->p
[0][0], 1);
54 value_set_si(coeff
->p
[0][d
+1], 1);
55 for (int i
= 1; i
<= d
; ++i
) {
56 value_multiply(coeff
->p
[i
][0], coeff
->p
[i
-1][0], d0
);
57 Vector_Combine(coeff
->p
[i
-1], coeff
->p
[i
-1]+1, coeff
->p
[i
]+1,
59 value_set_si(coeff
->p
[i
][d
+1], i
);
60 value_multiply(coeff
->p
[i
][d
+1], coeff
->p
[i
][d
+1], coeff
->p
[i
-1][d
+1]);
61 value_decrement(d0
, d0
);
66 void div(dpoly
& d
, Vector
*count
, ZZ
& sign
) {
67 int len
= coeff
->NbRows
;
68 Matrix
* c
= Matrix_Alloc(coeff
->NbRows
, coeff
->NbColumns
);
71 for (int i
= 0; i
< len
; ++i
) {
72 Vector_Copy(coeff
->p
[i
], c
->p
[i
], len
+1);
73 for (int j
= 1; j
<= i
; ++j
) {
74 zz2value(d
.coeff
[j
], tmp
);
75 value_multiply(tmp
, tmp
, c
->p
[i
][len
]);
76 value_oppose(tmp
, tmp
);
77 Vector_Combine(c
->p
[i
], c
->p
[i
-j
], c
->p
[i
],
78 c
->p
[i
-j
][len
], tmp
, len
);
79 value_multiply(c
->p
[i
][len
], c
->p
[i
][len
], c
->p
[i
-j
][len
]);
81 zz2value(d
.coeff
[0], tmp
);
82 value_multiply(c
->p
[i
][len
], c
->p
[i
][len
], tmp
);
85 value_set_si(tmp
, -1);
86 Vector_Scale(c
->p
[len
-1], count
->p
, tmp
, len
);
87 value_assign(count
->p
[len
], c
->p
[len
-1][len
]);
89 Vector_Copy(c
->p
[len
-1], count
->p
, len
+1);
90 Vector_Normalize(count
->p
, len
+1);
98 * Searches for a vector that is not orthogonal to any
99 * of the rays in rays.
101 static void nonorthog(mat_ZZ
& rays
, vec_ZZ
& lambda
)
103 int dim
= rays
.NumCols();
105 lambda
.SetLength(dim
);
109 for (int i
= 2; !found
&& i
<= 50*dim
; i
+=4) {
110 for (int j
= 0; j
< MAX_TRY
; ++j
) {
111 for (int k
= 0; k
< dim
; ++k
) {
112 int r
= random_int(i
)+2;
113 int v
= (2*(r
%2)-1) * (r
>> 1);
117 for (; k
< rays
.NumRows(); ++k
)
118 if (lambda
* rays
[k
] == 0)
120 if (k
== rays
.NumRows()) {
129 static void add_rays(mat_ZZ
& rays
, Polyhedron
*i
, int *r
, int nvar
= -1,
132 unsigned dim
= i
->Dimension
;
135 for (int k
= 0; k
< i
->NbRays
; ++k
) {
136 if (!value_zero_p(i
->Ray
[k
][dim
+1]))
138 if (!all
&& nvar
!= dim
&& First_Non_Zero(i
->Ray
[k
]+1, nvar
) == -1)
140 values2zz(i
->Ray
[k
]+1, rays
[(*r
)++], nvar
);
144 static void mask_r(Matrix
*f
, int nr
, Vector
*lcm
, int p
, Vector
*val
, evalue
*ev
)
146 unsigned nparam
= lcm
->Size
;
149 Vector
* prod
= Vector_Alloc(f
->NbRows
);
150 Matrix_Vector_Product(f
, val
->p
, prod
->p
);
152 for (int i
= 0; i
< nr
; ++i
) {
153 value_modulus(prod
->p
[i
], prod
->p
[i
], f
->p
[i
][nparam
+1]);
154 isint
&= value_zero_p(prod
->p
[i
]);
156 value_set_si(ev
->d
, 1);
158 value_set_si(ev
->x
.n
, isint
);
165 if (value_one_p(lcm
->p
[p
]))
166 mask_r(f
, nr
, lcm
, p
+1, val
, ev
);
168 value_assign(tmp
, lcm
->p
[p
]);
169 value_set_si(ev
->d
, 0);
170 ev
->x
.p
= new_enode(periodic
, VALUE_TO_INT(tmp
), p
+1);
172 value_decrement(tmp
, tmp
);
173 value_assign(val
->p
[p
], tmp
);
174 mask_r(f
, nr
, lcm
, p
+1, val
, &ev
->x
.p
->arr
[VALUE_TO_INT(tmp
)]);
175 } while (value_pos_p(tmp
));
180 static void mask_fractional(Matrix
*f
, evalue
*factor
)
182 int nr
= f
->NbRows
, nc
= f
->NbColumns
;
185 for (n
= 0; n
< nr
&& value_notzero_p(f
->p
[n
][nc
-1]); ++n
)
186 if (value_notone_p(f
->p
[n
][nc
-1]) &&
187 value_notmone_p(f
->p
[n
][nc
-1]))
201 value_set_si(EV
.x
.n
, 1);
203 for (n
= 0; n
< nr
; ++n
) {
204 value_assign(m
, f
->p
[n
][nc
-1]);
205 if (value_one_p(m
) || value_mone_p(m
))
208 int j
= normal_mod(f
->p
[n
], nc
-1, &m
);
210 free_evalue_refs(factor
);
211 value_init(factor
->d
);
212 evalue_set_si(factor
, 0, 1);
216 values2zz(f
->p
[n
], row
, nc
-1);
219 if (j
< (nc
-1)-1 && row
[j
] > g
/2) {
220 for (int k
= j
; k
< (nc
-1); ++k
)
226 value_set_si(EP
.d
, 0);
227 EP
.x
.p
= new_enode(relation
, 2, 0);
228 value_clear(EP
.x
.p
->arr
[1].d
);
229 EP
.x
.p
->arr
[1] = *factor
;
230 evalue
*ev
= &EP
.x
.p
->arr
[0];
231 value_set_si(ev
->d
, 0);
232 ev
->x
.p
= new_enode(fractional
, 3, -1);
233 evalue_set_si(&ev
->x
.p
->arr
[1], 0, 1);
234 evalue_set_si(&ev
->x
.p
->arr
[2], 1, 1);
235 evalue
*E
= multi_monom(row
);
236 value_assign(EV
.d
, m
);
238 value_clear(ev
->x
.p
->arr
[0].d
);
239 ev
->x
.p
->arr
[0] = *E
;
245 free_evalue_refs(&EV
);
251 static void mask_table(Matrix
*f
, evalue
*factor
)
253 int nr
= f
->NbRows
, nc
= f
->NbColumns
;
256 for (n
= 0; n
< nr
&& value_notzero_p(f
->p
[n
][nc
-1]); ++n
)
257 if (value_notone_p(f
->p
[n
][nc
-1]) &&
258 value_notmone_p(f
->p
[n
][nc
-1]))
266 unsigned np
= nc
- 2;
267 Vector
*lcm
= Vector_Alloc(np
);
268 Vector
*val
= Vector_Alloc(nc
);
269 Vector_Set(val
->p
, 0, nc
);
270 value_set_si(val
->p
[np
], 1);
271 Vector_Set(lcm
->p
, 1, np
);
272 for (n
= 0; n
< nr
; ++n
) {
273 if (value_one_p(f
->p
[n
][nc
-1]) ||
274 value_mone_p(f
->p
[n
][nc
-1]))
276 for (int j
= 0; j
< np
; ++j
)
277 if (value_notzero_p(f
->p
[n
][j
])) {
278 Gcd(f
->p
[n
][j
], f
->p
[n
][nc
-1], &tmp
);
279 value_division(tmp
, f
->p
[n
][nc
-1], tmp
);
280 value_lcm(tmp
, lcm
->p
[j
], &lcm
->p
[j
]);
285 mask_r(f
, nr
, lcm
, 0, val
, &EP
);
290 free_evalue_refs(&EP
);
293 static void mask(Matrix
*f
, evalue
*factor
, barvinok_options
*options
)
295 if (options
->lookup_table
)
296 mask_table(f
, factor
);
298 mask_fractional(f
, factor
);
301 /* This structure encodes the power of the term in a rational generating function.
303 * Either E == NULL or constant = 0
304 * If E != NULL, then the power is E
305 * If E == NULL, then the power is coeff * param[pos] + constant
314 /* Returns the power of (t+1) in the term of a rational generating function,
315 * i.e., the scalar product of the actual lattice point and lambda.
316 * The lattice point is the unique lattice point in the fundamental parallelepiped
317 * of the unimodual cone i shifted to the parametric vertex V.
319 * PD is the parameter domain, which, if != NULL, may be used to simply the
320 * resulting expression.
322 * The result is returned in term.
324 void lattice_point(Param_Vertices
* V
, const mat_ZZ
& rays
, vec_ZZ
& lambda
,
325 term_info
* term
, Polyhedron
*PD
, barvinok_options
*options
)
327 unsigned nparam
= V
->Vertex
->NbColumns
- 2;
328 unsigned dim
= rays
.NumCols();
330 vertex
.SetDims(V
->Vertex
->NbRows
, nparam
+1);
334 value_set_si(lcm
, 1);
335 for (int j
= 0; j
< V
->Vertex
->NbRows
; ++j
) {
336 value_lcm(lcm
, V
->Vertex
->p
[j
][nparam
+1], &lcm
);
338 if (value_notone_p(lcm
)) {
339 Matrix
* mv
= Matrix_Alloc(dim
, nparam
+1);
340 for (int j
= 0 ; j
< dim
; ++j
) {
341 value_division(tmp
, lcm
, V
->Vertex
->p
[j
][nparam
+1]);
342 Vector_Scale(V
->Vertex
->p
[j
], mv
->p
[j
], tmp
, nparam
+1);
345 term
->E
= lattice_point(rays
, lambda
, mv
, lcm
, PD
, options
);
353 for (int i
= 0; i
< V
->Vertex
->NbRows
; ++i
) {
354 assert(value_one_p(V
->Vertex
->p
[i
][nparam
+1])); // for now
355 values2zz(V
->Vertex
->p
[i
], vertex
[i
], nparam
+1);
359 num
= lambda
* vertex
;
363 for (int j
= 0; j
< nparam
; ++j
)
369 term
->E
= multi_monom(num
);
373 term
->constant
= num
[nparam
];
376 term
->coeff
= num
[p
];
384 struct counter
: public np_base
{
393 counter(unsigned dim
) : np_base(dim
) {
398 virtual void init(Polyhedron
*P
) {
399 randomvector(P
, lambda
, dim
);
402 virtual void reset() {
403 mpq_set_si(count
, 0, 0);
410 virtual void handle(const mat_ZZ
& rays
, Value
*vertex
, QQ c
, int *closed
,
411 barvinok_options
*options
);
412 virtual void get_count(Value
*result
) {
413 assert(value_one_p(&count
[0]._mp_den
));
414 value_assign(*result
, &count
[0]._mp_num
);
418 void counter::handle(const mat_ZZ
& rays
, Value
*V
, QQ c
, int *closed
,
419 barvinok_options
*options
)
421 for (int k
= 0; k
< dim
; ++k
) {
422 if (lambda
* rays
[k
] == 0)
427 assert(c
.n
== 1 || c
.n
== -1);
430 lattice_point(V
, rays
, vertex
, closed
);
431 num
= vertex
* lambda
;
433 normalize(sign
, num
, den
);
436 dpoly
n(dim
, den
[0], 1);
437 for (int k
= 1; k
< dim
; ++k
) {
438 dpoly
fact(dim
, den
[k
], 1);
441 d
.div(n
, count
, sign
);
444 struct bfe_term
: public bfc_term_base
{
445 vector
<evalue
*> factors
;
447 bfe_term(int len
) : bfc_term_base(len
) {
451 for (int i
= 0; i
< factors
.size(); ++i
) {
454 free_evalue_refs(factors
[i
]);
460 static void print_int_vector(int *v
, int len
, char *name
)
462 cerr
<< name
<< endl
;
463 for (int j
= 0; j
< len
; ++j
) {
469 static void print_bfc_terms(mat_ZZ
& factors
, bfc_vec
& v
)
472 cerr
<< "factors" << endl
;
473 cerr
<< factors
<< endl
;
474 for (int i
= 0; i
< v
.size(); ++i
) {
475 cerr
<< "term: " << i
<< endl
;
476 print_int_vector(v
[i
]->powers
, factors
.NumRows(), "powers");
477 cerr
<< "terms" << endl
;
478 cerr
<< v
[i
]->terms
<< endl
;
479 bfc_term
* bfct
= static_cast<bfc_term
*>(v
[i
]);
480 cerr
<< bfct
->c
<< endl
;
484 static void print_bfe_terms(mat_ZZ
& factors
, bfc_vec
& v
)
487 cerr
<< "factors" << endl
;
488 cerr
<< factors
<< endl
;
489 for (int i
= 0; i
< v
.size(); ++i
) {
490 cerr
<< "term: " << i
<< endl
;
491 print_int_vector(v
[i
]->powers
, factors
.NumRows(), "powers");
492 cerr
<< "terms" << endl
;
493 cerr
<< v
[i
]->terms
<< endl
;
494 bfe_term
* bfet
= static_cast<bfe_term
*>(v
[i
]);
495 for (int j
= 0; j
< v
[i
]->terms
.NumRows(); ++j
) {
496 char * test
[] = {"a", "b"};
497 print_evalue(stderr
, bfet
->factors
[j
], test
);
498 fprintf(stderr
, "\n");
503 struct bfcounter
: public bfcounter_base
{
506 bfcounter(unsigned dim
) : bfcounter_base(dim
) {
513 virtual void base(mat_ZZ
& factors
, bfc_vec
& v
);
514 virtual void get_count(Value
*result
) {
515 assert(value_one_p(&count
[0]._mp_den
));
516 value_assign(*result
, &count
[0]._mp_num
);
520 void bfcounter::base(mat_ZZ
& factors
, bfc_vec
& v
)
522 unsigned nf
= factors
.NumRows();
524 for (int i
= 0; i
< v
.size(); ++i
) {
525 bfc_term
* bfct
= static_cast<bfc_term
*>(v
[i
]);
527 // factor is always positive, so we always
529 for (int k
= 0; k
< nf
; ++k
)
530 total_power
+= v
[i
]->powers
[k
];
533 for (j
= 0; j
< nf
; ++j
)
534 if (v
[i
]->powers
[j
] > 0)
537 dpoly
D(total_power
, factors
[j
][0], 1);
538 for (int k
= 1; k
< v
[i
]->powers
[j
]; ++k
) {
539 dpoly
fact(total_power
, factors
[j
][0], 1);
543 for (int k
= 0; k
< v
[i
]->powers
[j
]; ++k
) {
544 dpoly
fact(total_power
, factors
[j
][0], 1);
548 for (int k
= 0; k
< v
[i
]->terms
.NumRows(); ++k
) {
549 dpoly
n(total_power
, v
[i
]->terms
[k
][0]);
550 mpq_set_si(tcount
, 0, 1);
551 n
.div(D
, tcount
, one
);
553 bfct
->c
[k
].n
= -bfct
->c
[k
].n
;
554 zz2value(bfct
->c
[k
].n
, tn
);
555 zz2value(bfct
->c
[k
].d
, td
);
557 mpz_mul(mpq_numref(tcount
), mpq_numref(tcount
), tn
);
558 mpz_mul(mpq_denref(tcount
), mpq_denref(tcount
), td
);
559 mpq_canonicalize(tcount
);
560 mpq_add(count
, count
, tcount
);
567 /* Check whether the polyhedron is unbounded and if so,
568 * check whether it has any (and therefore an infinite number of)
570 * If one of the vertices is integer, then we are done.
571 * Otherwise, transform the polyhedron such that one of the rays
572 * is the first unit vector and cut it off at a height that ensures
573 * that if the whole polyhedron has any points, then the remaining part
574 * has integer points. In particular we add the largest coefficient
575 * of a ray to the highest vertex (rounded up).
577 static bool Polyhedron_is_infinite(Polyhedron
*P
, Value
* result
,
578 barvinok_options
*options
)
590 for (; r
< P
->NbRays
; ++r
)
591 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1]))
593 if (P
->NbBid
== 0 && r
== P
->NbRays
)
596 if (options
->count_sample_infinite
) {
599 sample
= Polyhedron_Sample(P
, options
);
601 value_set_si(*result
, 0);
603 value_set_si(*result
, -1);
609 for (int i
= 0; i
< P
->NbRays
; ++i
)
610 if (value_one_p(P
->Ray
[i
][1+P
->Dimension
])) {
611 value_set_si(*result
, -1);
616 v
= Vector_Alloc(P
->Dimension
+1);
617 Vector_Gcd(P
->Ray
[r
]+1, P
->Dimension
, &g
);
618 Vector_AntiScale(P
->Ray
[r
]+1, v
->p
, g
, P
->Dimension
+1);
619 M
= unimodular_complete(v
);
620 value_set_si(M
->p
[P
->Dimension
][P
->Dimension
], 1);
623 P
= Polyhedron_Preimage(P
, M2
, 0);
632 value_set_si(size
, 0);
634 for (int i
= 0; i
< P
->NbBid
; ++i
) {
635 value_absolute(tmp
, P
->Ray
[i
][1]);
636 if (value_gt(tmp
, size
))
637 value_assign(size
, tmp
);
639 for (int i
= P
->NbBid
; i
< P
->NbRays
; ++i
) {
640 if (value_zero_p(P
->Ray
[i
][P
->Dimension
+1])) {
641 if (value_gt(P
->Ray
[i
][1], size
))
642 value_assign(size
, P
->Ray
[i
][1]);
645 mpz_cdiv_q(tmp
, P
->Ray
[i
][1], P
->Ray
[i
][P
->Dimension
+1]);
646 if (first
|| value_gt(tmp
, offset
)) {
647 value_assign(offset
, tmp
);
651 value_addto(offset
, offset
, size
);
655 v
= Vector_Alloc(P
->Dimension
+2);
656 value_set_si(v
->p
[0], 1);
657 value_set_si(v
->p
[1], -1);
658 value_assign(v
->p
[1+P
->Dimension
], offset
);
659 R
= AddConstraints(v
->p
, 1, P
, options
->MaxRays
);
667 barvinok_count_with_options(P
, &c
, options
);
670 value_set_si(*result
, 0);
672 value_set_si(*result
, -1);
678 typedef Polyhedron
* Polyhedron_p
;
680 static void barvinok_count_f(Polyhedron
*P
, Value
* result
,
681 barvinok_options
*options
);
683 void barvinok_count_with_options(Polyhedron
*P
, Value
* result
,
684 struct barvinok_options
*options
)
689 bool infinite
= false;
692 value_set_si(*result
, 0);
698 P
= remove_equalities(P
);
699 P
= DomainConstraintSimplify(P
, options
->MaxRays
);
703 } while (!emptyQ(P
) && P
->NbEq
!= 0);
706 value_set_si(*result
, 0);
711 if (Polyhedron_is_infinite(P
, result
, options
)) {
716 if (P
->Dimension
== 0) {
717 /* Test whether the constraints are satisfied */
718 POL_ENSURE_VERTICES(P
);
719 value_set_si(*result
, !emptyQ(P
));
724 Q
= Polyhedron_Factor(P
, 0, options
->MaxRays
);
732 barvinok_count_f(P
, result
, options
);
733 if (value_neg_p(*result
))
735 if (Q
&& P
->next
&& value_notzero_p(*result
)) {
739 for (Q
= P
->next
; Q
; Q
= Q
->next
) {
740 barvinok_count_f(Q
, &factor
, options
);
741 if (value_neg_p(factor
)) {
744 } else if (Q
->next
&& value_zero_p(factor
)) {
745 value_set_si(*result
, 0);
748 value_multiply(*result
, *result
, factor
);
757 value_set_si(*result
, -1);
760 void barvinok_count(Polyhedron
*P
, Value
* result
, unsigned NbMaxCons
)
762 barvinok_options
*options
= barvinok_options_new_with_defaults();
763 options
->MaxRays
= NbMaxCons
;
764 barvinok_count_with_options(P
, result
, options
);
768 static void barvinok_count_f(Polyhedron
*P
, Value
* result
,
769 barvinok_options
*options
)
772 value_set_si(*result
, 0);
776 if (P
->Dimension
== 1)
777 return Line_Length(P
, result
);
779 int c
= P
->NbConstraints
;
780 POL_ENSURE_FACETS(P
);
781 if (c
!= P
->NbConstraints
|| P
->NbEq
!= 0)
782 return barvinok_count_with_options(P
, result
, options
);
784 POL_ENSURE_VERTICES(P
);
786 if (Polyhedron_is_infinite(P
, result
, options
))
790 if (options
->incremental_specialization
== 2)
791 cnt
= new bfcounter(P
->Dimension
);
792 else if (options
->incremental_specialization
== 1)
793 cnt
= new icounter(P
->Dimension
);
795 cnt
= new counter(P
->Dimension
);
796 cnt
->start(P
, options
);
798 cnt
->get_count(result
);
802 static void uni_polynom(int param
, Vector
*c
, evalue
*EP
)
804 unsigned dim
= c
->Size
-2;
806 value_set_si(EP
->d
,0);
807 EP
->x
.p
= new_enode(polynomial
, dim
+1, param
+1);
808 for (int j
= 0; j
<= dim
; ++j
)
809 evalue_set(&EP
->x
.p
->arr
[j
], c
->p
[j
], c
->p
[dim
+1]);
812 static void multi_polynom(Vector
*c
, evalue
* X
, evalue
*EP
)
814 unsigned dim
= c
->Size
-2;
818 evalue_set(&EC
, c
->p
[dim
], c
->p
[dim
+1]);
821 evalue_set(EP
, c
->p
[dim
], c
->p
[dim
+1]);
823 for (int i
= dim
-1; i
>= 0; --i
) {
825 value_assign(EC
.x
.n
, c
->p
[i
]);
828 free_evalue_refs(&EC
);
831 Polyhedron
*unfringe (Polyhedron
*P
, unsigned MaxRays
)
833 int len
= P
->Dimension
+2;
834 Polyhedron
*T
, *R
= P
;
837 Vector
*row
= Vector_Alloc(len
);
838 value_set_si(row
->p
[0], 1);
840 R
= DomainConstraintSimplify(Polyhedron_Copy(P
), MaxRays
);
842 Matrix
*M
= Matrix_Alloc(2, len
-1);
843 value_set_si(M
->p
[1][len
-2], 1);
844 for (int v
= 0; v
< P
->Dimension
; ++v
) {
845 value_set_si(M
->p
[0][v
], 1);
846 Polyhedron
*I
= Polyhedron_Image(R
, M
, 2+1);
847 value_set_si(M
->p
[0][v
], 0);
848 for (int r
= 0; r
< I
->NbConstraints
; ++r
) {
849 if (value_zero_p(I
->Constraint
[r
][0]))
851 if (value_zero_p(I
->Constraint
[r
][1]))
853 if (value_one_p(I
->Constraint
[r
][1]))
855 if (value_mone_p(I
->Constraint
[r
][1]))
857 value_absolute(g
, I
->Constraint
[r
][1]);
858 Vector_Set(row
->p
+1, 0, len
-2);
859 value_division(row
->p
[1+v
], I
->Constraint
[r
][1], g
);
860 mpz_fdiv_q(row
->p
[len
-1], I
->Constraint
[r
][2], g
);
862 R
= AddConstraints(row
->p
, 1, R
, MaxRays
);
874 /* this procedure may have false negatives */
875 static bool Polyhedron_is_infinite_param(Polyhedron
*P
, unsigned nparam
)
878 for (r
= 0; r
< P
->NbRays
; ++r
) {
879 if (!value_zero_p(P
->Ray
[r
][0]) &&
880 !value_zero_p(P
->Ray
[r
][P
->Dimension
+1]))
882 if (First_Non_Zero(P
->Ray
[r
]+1+P
->Dimension
-nparam
, nparam
) == -1)
888 /* Check whether all rays point in the positive directions
891 static bool Polyhedron_has_positive_rays(Polyhedron
*P
, unsigned nparam
)
894 for (r
= 0; r
< P
->NbRays
; ++r
)
895 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1])) {
897 for (i
= P
->Dimension
- nparam
; i
< P
->Dimension
; ++i
)
898 if (value_neg_p(P
->Ray
[r
][i
+1]))
904 /* Check whether all rays are revlex positive in the parameters
906 static bool Polyhedron_has_revlex_positive_rays(Polyhedron
*P
, unsigned nparam
)
909 for (r
= 0; r
< P
->NbRays
; ++r
) {
910 if (value_notzero_p(P
->Ray
[r
][P
->Dimension
+1]))
913 for (i
= P
->Dimension
-1; i
>= P
->Dimension
-nparam
; --i
) {
914 if (value_neg_p(P
->Ray
[r
][i
+1]))
916 if (value_pos_p(P
->Ray
[r
][i
+1]))
919 /* A ray independent of the parameters */
920 if (i
< P
->Dimension
-nparam
)
926 typedef evalue
* evalue_p
;
928 struct enumerator_base
{
932 vertex_decomposer
*vpd
;
934 enumerator_base(unsigned dim
, vertex_decomposer
*vpd
)
939 vE
= new evalue_p
[vpd
->nbV
];
940 for (int j
= 0; j
< vpd
->nbV
; ++j
)
944 evalue_set_si(&mone
, -1, 1);
947 void decompose_at(Param_Vertices
*V
, int _i
, barvinok_options
*options
) {
951 value_init(vE
[_i
]->d
);
952 evalue_set_si(vE
[_i
], 0, 1);
954 vpd
->decompose_at_vertex(V
, _i
, options
);
957 virtual ~enumerator_base() {
958 for (int j
= 0; j
< vpd
->nbV
; ++j
)
960 free_evalue_refs(vE
[j
]);
965 free_evalue_refs(&mone
);
968 static enumerator_base
*create(Polyhedron
*P
, unsigned dim
, unsigned nbV
,
969 barvinok_options
*options
);
972 struct enumerator
: public signed_cone_consumer
, public vertex_decomposer
,
973 public enumerator_base
{
981 enumerator(Polyhedron
*P
, unsigned dim
, unsigned nbV
) :
982 vertex_decomposer(P
, nbV
, *this), enumerator_base(dim
, this) {
985 randomvector(P
, lambda
, dim
);
987 c
= Vector_Alloc(dim
+2);
997 virtual void handle(const signed_cone
& sc
, barvinok_options
*options
);
1000 void enumerator::handle(const signed_cone
& sc
, barvinok_options
*options
)
1004 assert(sc
.rays
.NumRows() == dim
);
1005 for (int k
= 0; k
< dim
; ++k
) {
1006 if (lambda
* sc
.rays
[k
] == 0)
1012 lattice_point(V
, sc
.rays
, lambda
, &num
, 0, options
);
1013 den
= sc
.rays
* lambda
;
1014 normalize(sign
, num
.constant
, den
);
1016 dpoly
n(dim
, den
[0], 1);
1017 for (int k
= 1; k
< dim
; ++k
) {
1018 dpoly
fact(dim
, den
[k
], 1);
1021 if (num
.E
!= NULL
) {
1022 ZZ
one(INIT_VAL
, 1);
1023 dpoly_n
d(dim
, num
.constant
, one
);
1026 multi_polynom(c
, num
.E
, &EV
);
1027 eadd(&EV
, vE
[vert
]);
1028 free_evalue_refs(&EV
);
1029 free_evalue_refs(num
.E
);
1031 } else if (num
.pos
!= -1) {
1032 dpoly_n
d(dim
, num
.constant
, num
.coeff
);
1035 uni_polynom(num
.pos
, c
, &EV
);
1036 eadd(&EV
, vE
[vert
]);
1037 free_evalue_refs(&EV
);
1039 mpq_set_si(count
, 0, 1);
1040 dpoly
d(dim
, num
.constant
);
1041 d
.div(n
, count
, sign
);
1044 evalue_set(&EV
, &count
[0]._mp_num
, &count
[0]._mp_den
);
1045 eadd(&EV
, vE
[vert
]);
1046 free_evalue_refs(&EV
);
1050 struct ienumerator_base
: enumerator_base
{
1053 ienumerator_base(unsigned dim
, vertex_decomposer
*vpd
) :
1054 enumerator_base(dim
,vpd
) {
1055 E_vertex
= new evalue_p
[dim
];
1058 virtual ~ienumerator_base() {
1062 evalue
*E_num(int i
, int d
) {
1063 return E_vertex
[i
+ (dim
-d
)];
1072 cumulator(evalue
*factor
, evalue
*v
, dpoly_r
*r
) :
1073 factor(factor
), v(v
), r(r
) {}
1075 void cumulate(barvinok_options
*options
);
1077 virtual void add_term(const vector
<int>& powers
, evalue
*f2
) = 0;
1080 void cumulator::cumulate(barvinok_options
*options
)
1082 evalue cum
; // factor * 1 * E_num[0]/1 * (E_num[0]-1)/2 *...
1084 evalue t
; // E_num[0] - (m-1)
1088 if (options
->lookup_table
) {
1090 evalue_set_si(&mone
, -1, 1);
1094 evalue_copy(&cum
, factor
);
1097 value_set_si(f
.d
, 1);
1098 value_set_si(f
.x
.n
, 1);
1102 if (!options
->lookup_table
) {
1103 for (cst
= &t
; value_zero_p(cst
->d
); ) {
1104 if (cst
->x
.p
->type
== fractional
)
1105 cst
= &cst
->x
.p
->arr
[1];
1107 cst
= &cst
->x
.p
->arr
[0];
1111 for (int m
= 0; m
< r
->len
; ++m
) {
1114 value_set_si(f
.d
, m
);
1116 if (!options
->lookup_table
)
1117 value_subtract(cst
->x
.n
, cst
->x
.n
, cst
->d
);
1123 dpoly_r_term_list
& current
= r
->c
[r
->len
-1-m
];
1124 dpoly_r_term_list::iterator j
;
1125 for (j
= current
.begin(); j
!= current
.end(); ++j
) {
1126 if ((*j
)->coeff
== 0)
1128 evalue
*f2
= new evalue
;
1130 value_init(f2
->x
.n
);
1131 zz2value((*j
)->coeff
, f2
->x
.n
);
1132 zz2value(r
->denom
, f2
->d
);
1135 add_term((*j
)->powers
, f2
);
1138 free_evalue_refs(&f
);
1139 free_evalue_refs(&t
);
1140 free_evalue_refs(&cum
);
1141 if (options
->lookup_table
)
1142 free_evalue_refs(&mone
);
1145 struct E_poly_term
{
1150 struct ie_cum
: public cumulator
{
1151 vector
<E_poly_term
*> terms
;
1153 ie_cum(evalue
*factor
, evalue
*v
, dpoly_r
*r
) : cumulator(factor
, v
, r
) {}
1155 virtual void add_term(const vector
<int>& powers
, evalue
*f2
);
1158 void ie_cum::add_term(const vector
<int>& powers
, evalue
*f2
)
1161 for (k
= 0; k
< terms
.size(); ++k
) {
1162 if (terms
[k
]->powers
== powers
) {
1163 eadd(f2
, terms
[k
]->E
);
1164 free_evalue_refs(f2
);
1169 if (k
>= terms
.size()) {
1170 E_poly_term
*ET
= new E_poly_term
;
1171 ET
->powers
= powers
;
1173 terms
.push_back(ET
);
1177 struct ienumerator
: public signed_cone_consumer
, public vertex_decomposer
,
1178 public ienumerator_base
{
1184 ienumerator(Polyhedron
*P
, unsigned dim
, unsigned nbV
) :
1185 vertex_decomposer(P
, nbV
, *this), ienumerator_base(dim
, this) {
1186 vertex
.SetDims(1, dim
);
1188 den
.SetDims(dim
, dim
);
1196 virtual void handle(const signed_cone
& sc
, barvinok_options
*options
);
1197 void reduce(evalue
*factor
, const mat_ZZ
& num
, const mat_ZZ
& den_f
,
1198 barvinok_options
*options
);
1201 void ienumerator::reduce(evalue
*factor
, const mat_ZZ
& num
, const mat_ZZ
& den_f
,
1202 barvinok_options
*options
)
1204 unsigned len
= den_f
.NumRows(); // number of factors in den
1205 unsigned dim
= num
.NumCols();
1206 assert(num
.NumRows() == 1);
1209 eadd(factor
, vE
[vert
]);
1218 split_one(num
, num_s
, num_p
, den_f
, den_s
, den_r
);
1221 den_p
.SetLength(len
);
1225 normalize(one
, num_s
, num_p
, den_s
, den_p
, den_r
);
1227 emul(&mone
, factor
);
1231 for (int k
= 0; k
< len
; ++k
) {
1234 else if (den_s
[k
] == 0)
1237 if (no_param
== 0) {
1238 reduce(factor
, num_p
, den_r
, options
);
1242 pden
.SetDims(only_param
, dim
-1);
1244 for (k
= 0, l
= 0; k
< len
; ++k
)
1246 pden
[l
++] = den_r
[k
];
1248 for (k
= 0; k
< len
; ++k
)
1252 dpoly
n(no_param
, num_s
[0]);
1253 dpoly
D(no_param
, den_s
[k
], 1);
1254 for ( ; ++k
< len
; )
1255 if (den_p
[k
] == 0) {
1256 dpoly
fact(no_param
, den_s
[k
], 1);
1261 // if no_param + only_param == len then all powers
1262 // below will be all zero
1263 if (no_param
+ only_param
== len
) {
1264 if (E_num(0, dim
) != 0)
1265 r
= new dpoly_r(n
, len
);
1267 mpq_set_si(tcount
, 0, 1);
1269 n
.div(D
, tcount
, one
);
1271 if (value_notzero_p(mpq_numref(tcount
))) {
1275 value_assign(f
.x
.n
, mpq_numref(tcount
));
1276 value_assign(f
.d
, mpq_denref(tcount
));
1278 reduce(factor
, num_p
, pden
, options
);
1279 free_evalue_refs(&f
);
1284 for (k
= 0; k
< len
; ++k
) {
1285 if (den_s
[k
] == 0 || den_p
[k
] == 0)
1288 dpoly
pd(no_param
-1, den_s
[k
], 1);
1291 for (l
= 0; l
< k
; ++l
)
1292 if (den_r
[l
] == den_r
[k
])
1296 r
= new dpoly_r(n
, pd
, l
, len
);
1298 dpoly_r
*nr
= new dpoly_r(r
, pd
, l
, len
);
1304 dpoly_r
*rc
= r
->div(D
);
1307 if (E_num(0, dim
) == 0) {
1308 int common
= pden
.NumRows();
1309 dpoly_r_term_list
& final
= r
->c
[r
->len
-1];
1315 zz2value(r
->denom
, f
.d
);
1316 dpoly_r_term_list::iterator j
;
1317 for (j
= final
.begin(); j
!= final
.end(); ++j
) {
1318 if ((*j
)->coeff
== 0)
1321 for (int k
= 0; k
< r
->dim
; ++k
) {
1322 int n
= (*j
)->powers
[k
];
1325 pden
.SetDims(rows
+n
, pden
.NumCols());
1326 for (int l
= 0; l
< n
; ++l
)
1327 pden
[rows
+l
] = den_r
[k
];
1331 evalue_copy(&t
, factor
);
1332 zz2value((*j
)->coeff
, f
.x
.n
);
1334 reduce(&t
, num_p
, pden
, options
);
1335 free_evalue_refs(&t
);
1337 free_evalue_refs(&f
);
1339 ie_cum
cum(factor
, E_num(0, dim
), r
);
1340 cum
.cumulate(options
);
1342 int common
= pden
.NumRows();
1344 for (int j
= 0; j
< cum
.terms
.size(); ++j
) {
1346 pden
.SetDims(rows
, pden
.NumCols());
1347 for (int k
= 0; k
< r
->dim
; ++k
) {
1348 int n
= cum
.terms
[j
]->powers
[k
];
1351 pden
.SetDims(rows
+n
, pden
.NumCols());
1352 for (int l
= 0; l
< n
; ++l
)
1353 pden
[rows
+l
] = den_r
[k
];
1356 reduce(cum
.terms
[j
]->E
, num_p
, pden
, options
);
1357 free_evalue_refs(cum
.terms
[j
]->E
);
1358 delete cum
.terms
[j
]->E
;
1359 delete cum
.terms
[j
];
1366 static int type_offset(enode
*p
)
1368 return p
->type
== fractional
? 1 :
1369 p
->type
== flooring
? 1 : 0;
1372 static int edegree(evalue
*e
)
1377 if (value_notzero_p(e
->d
))
1381 int i
= type_offset(p
);
1382 if (p
->size
-i
-1 > d
)
1383 d
= p
->size
- i
- 1;
1384 for (; i
< p
->size
; i
++) {
1385 int d2
= edegree(&p
->arr
[i
]);
1392 void ienumerator::handle(const signed_cone
& sc
, barvinok_options
*options
)
1395 assert(sc
.rays
.NumRows() == dim
);
1397 lattice_point(V
, sc
.rays
, vertex
[0], E_vertex
, options
);
1403 evalue_set_si(&one
, sc
.sign
, 1);
1404 reduce(&one
, vertex
, den
, options
);
1405 free_evalue_refs(&one
);
1407 for (int i
= 0; i
< dim
; ++i
)
1409 free_evalue_refs(E_vertex
[i
]);
1414 struct bfenumerator
: public vertex_decomposer
, public bf_base
,
1415 public ienumerator_base
{
1418 bfenumerator(Polyhedron
*P
, unsigned dim
, unsigned nbV
) :
1419 vertex_decomposer(P
, nbV
, *this),
1420 bf_base(dim
), ienumerator_base(dim
, this) {
1428 virtual void handle(const signed_cone
& sc
, barvinok_options
*options
);
1429 virtual void base(mat_ZZ
& factors
, bfc_vec
& v
);
1431 bfc_term_base
* new_bf_term(int len
) {
1432 bfe_term
* t
= new bfe_term(len
);
1436 virtual void set_factor(bfc_term_base
*t
, int k
, int change
) {
1437 bfe_term
* bfet
= static_cast<bfe_term
*>(t
);
1438 factor
= bfet
->factors
[k
];
1439 assert(factor
!= NULL
);
1440 bfet
->factors
[k
] = NULL
;
1442 emul(&mone
, factor
);
1445 virtual void set_factor(bfc_term_base
*t
, int k
, mpq_t
&q
, int change
) {
1446 bfe_term
* bfet
= static_cast<bfe_term
*>(t
);
1447 factor
= bfet
->factors
[k
];
1448 assert(factor
!= NULL
);
1449 bfet
->factors
[k
] = NULL
;
1455 value_oppose(f
.x
.n
, mpq_numref(q
));
1457 value_assign(f
.x
.n
, mpq_numref(q
));
1458 value_assign(f
.d
, mpq_denref(q
));
1460 free_evalue_refs(&f
);
1463 virtual void set_factor(bfc_term_base
*t
, int k
, const QQ
& c
, int change
) {
1464 bfe_term
* bfet
= static_cast<bfe_term
*>(t
);
1466 factor
= new evalue
;
1471 zz2value(c
.n
, f
.x
.n
);
1473 value_oppose(f
.x
.n
, f
.x
.n
);
1476 value_init(factor
->d
);
1477 evalue_copy(factor
, bfet
->factors
[k
]);
1479 free_evalue_refs(&f
);
1482 void set_factor(evalue
*f
, int change
) {
1488 virtual void insert_term(bfc_term_base
*t
, int i
) {
1489 bfe_term
* bfet
= static_cast<bfe_term
*>(t
);
1490 int len
= t
->terms
.NumRows()-1; // already increased by one
1492 bfet
->factors
.resize(len
+1);
1493 for (int j
= len
; j
> i
; --j
) {
1494 bfet
->factors
[j
] = bfet
->factors
[j
-1];
1495 t
->terms
[j
] = t
->terms
[j
-1];
1497 bfet
->factors
[i
] = factor
;
1501 virtual void update_term(bfc_term_base
*t
, int i
) {
1502 bfe_term
* bfet
= static_cast<bfe_term
*>(t
);
1504 eadd(factor
, bfet
->factors
[i
]);
1505 free_evalue_refs(factor
);
1509 virtual bool constant_vertex(int dim
) { return E_num(0, dim
) == 0; }
1511 virtual void cum(bf_reducer
*bfr
, bfc_term_base
*t
, int k
, dpoly_r
*r
,
1512 barvinok_options
*options
);
1515 enumerator_base
*enumerator_base::create(Polyhedron
*P
, unsigned dim
, unsigned nbV
,
1516 barvinok_options
*options
)
1518 enumerator_base
*eb
;
1520 if (options
->incremental_specialization
== BV_SPECIALIZATION_BF
)
1521 eb
= new bfenumerator(P
, dim
, nbV
);
1522 else if (options
->incremental_specialization
== BV_SPECIALIZATION_DF
)
1523 eb
= new ienumerator(P
, dim
, nbV
);
1525 eb
= new enumerator(P
, dim
, nbV
);
1530 struct bfe_cum
: public cumulator
{
1532 bfc_term_base
*told
;
1536 bfe_cum(evalue
*factor
, evalue
*v
, dpoly_r
*r
, bf_reducer
*bfr
,
1537 bfc_term_base
*t
, int k
, bfenumerator
*e
) :
1538 cumulator(factor
, v
, r
), told(t
), k(k
),
1542 virtual void add_term(const vector
<int>& powers
, evalue
*f2
);
1545 void bfe_cum::add_term(const vector
<int>& powers
, evalue
*f2
)
1547 bfr
->update_powers(powers
);
1549 bfc_term_base
* t
= bfe
->find_bfc_term(bfr
->vn
, bfr
->npowers
, bfr
->nnf
);
1550 bfe
->set_factor(f2
, bfr
->l_changes
% 2);
1551 bfe
->add_term(t
, told
->terms
[k
], bfr
->l_extra_num
);
1554 void bfenumerator::cum(bf_reducer
*bfr
, bfc_term_base
*t
, int k
,
1555 dpoly_r
*r
, barvinok_options
*options
)
1557 bfe_term
* bfet
= static_cast<bfe_term
*>(t
);
1558 bfe_cum
cum(bfet
->factors
[k
], E_num(0, bfr
->d
), r
, bfr
, t
, k
, this);
1559 cum
.cumulate(options
);
1562 void bfenumerator::base(mat_ZZ
& factors
, bfc_vec
& v
)
1564 for (int i
= 0; i
< v
.size(); ++i
) {
1565 assert(v
[i
]->terms
.NumRows() == 1);
1566 evalue
*factor
= static_cast<bfe_term
*>(v
[i
])->factors
[0];
1567 eadd(factor
, vE
[vert
]);
1572 void bfenumerator::handle(const signed_cone
& sc
, barvinok_options
*options
)
1575 assert(sc
.rays
.NumRows() == enumerator_base::dim
);
1577 bfe_term
* t
= new bfe_term(enumerator_base::dim
);
1578 vector
< bfc_term_base
* > v
;
1581 t
->factors
.resize(1);
1583 t
->terms
.SetDims(1, enumerator_base::dim
);
1584 lattice_point(V
, sc
.rays
, t
->terms
[0], E_vertex
, options
);
1586 // the elements of factors are always lexpositive
1588 int s
= setup_factors(sc
.rays
, factors
, t
, sc
.sign
);
1590 t
->factors
[0] = new evalue
;
1591 value_init(t
->factors
[0]->d
);
1592 evalue_set_si(t
->factors
[0], s
, 1);
1593 reduce(factors
, v
, options
);
1595 for (int i
= 0; i
< enumerator_base::dim
; ++i
)
1597 free_evalue_refs(E_vertex
[i
]);
1602 #ifdef HAVE_CORRECT_VERTICES
1603 static inline Param_Polyhedron
*Polyhedron2Param_SD(Polyhedron
**Din
,
1604 Polyhedron
*Cin
,int WS
,Polyhedron
**CEq
,Matrix
**CT
)
1606 if (WS
& POL_NO_DUAL
)
1608 return Polyhedron2Param_SimplifiedDomain(Din
, Cin
, WS
, CEq
, CT
);
1611 static Param_Polyhedron
*Polyhedron2Param_SD(Polyhedron
**Din
,
1612 Polyhedron
*Cin
,int WS
,Polyhedron
**CEq
,Matrix
**CT
)
1614 static char data
[] = " 1 0 0 0 0 1 -18 "
1615 " 1 0 0 -20 0 19 1 "
1616 " 1 0 1 20 0 -20 16 "
1619 " 1 4 -20 0 0 -1 23 "
1620 " 1 -4 20 0 0 1 -22 "
1621 " 1 0 1 0 20 -20 16 "
1622 " 1 0 0 0 -20 19 1 ";
1623 static int checked
= 0;
1628 Matrix
*M
= Matrix_Alloc(9, 7);
1629 for (i
= 0; i
< 9; ++i
)
1630 for (int j
= 0; j
< 7; ++j
) {
1631 sscanf(p
, "%d%n", &v
, &n
);
1633 value_set_si(M
->p
[i
][j
], v
);
1635 Polyhedron
*P
= Constraints2Polyhedron(M
, 1024);
1637 Polyhedron
*U
= Universe_Polyhedron(1);
1638 Param_Polyhedron
*PP
= Polyhedron2Param_Domain(P
, U
, 1024);
1642 for (i
= 0, V
= PP
->V
; V
; ++i
, V
= V
->next
)
1645 Param_Polyhedron_Free(PP
);
1647 fprintf(stderr
, "WARNING: results may be incorrect\n");
1649 "WARNING: use latest version of PolyLib to remove this warning\n");
1653 return Polyhedron2Param_SimplifiedDomain(Din
, Cin
, WS
, CEq
, CT
);
1657 static evalue
* barvinok_enumerate_ev_f(Polyhedron
*P
, Polyhedron
* C
,
1658 barvinok_options
*options
);
1661 static evalue
* barvinok_enumerate_cst(Polyhedron
*P
, Polyhedron
* C
,
1666 ALLOC(evalue
, eres
);
1667 value_init(eres
->d
);
1668 value_set_si(eres
->d
, 0);
1669 eres
->x
.p
= new_enode(partition
, 2, C
->Dimension
);
1670 EVALUE_SET_DOMAIN(eres
->x
.p
->arr
[0], DomainConstraintSimplify(C
, MaxRays
));
1671 value_set_si(eres
->x
.p
->arr
[1].d
, 1);
1672 value_init(eres
->x
.p
->arr
[1].x
.n
);
1674 value_set_si(eres
->x
.p
->arr
[1].x
.n
, 0);
1676 barvinok_count(P
, &eres
->x
.p
->arr
[1].x
.n
, MaxRays
);
1681 evalue
* barvinok_enumerate_with_options(Polyhedron
*P
, Polyhedron
* C
,
1682 struct barvinok_options
*options
)
1684 //P = unfringe(P, MaxRays);
1685 Polyhedron
*Corig
= C
;
1686 Polyhedron
*CEq
= NULL
, *rVD
, *CA
;
1688 unsigned nparam
= C
->Dimension
;
1692 value_init(factor
.d
);
1693 evalue_set_si(&factor
, 1, 1);
1695 CA
= align_context(C
, P
->Dimension
, options
->MaxRays
);
1696 P
= DomainIntersection(P
, CA
, options
->MaxRays
);
1697 Polyhedron_Free(CA
);
1700 POL_ENSURE_FACETS(P
);
1701 POL_ENSURE_VERTICES(P
);
1702 POL_ENSURE_FACETS(C
);
1703 POL_ENSURE_VERTICES(C
);
1705 if (C
->Dimension
== 0 || emptyQ(P
)) {
1707 eres
= barvinok_enumerate_cst(P
, CEq
? CEq
: Polyhedron_Copy(C
),
1710 emul(&factor
, eres
);
1711 reduce_evalue(eres
);
1712 free_evalue_refs(&factor
);
1719 if (Polyhedron_is_infinite_param(P
, nparam
))
1724 P
= remove_equalities_p(P
, P
->Dimension
-nparam
, &f
);
1725 mask(f
, &factor
, options
);
1728 if (P
->Dimension
== nparam
) {
1730 P
= Universe_Polyhedron(0);
1734 Polyhedron
*T
= Polyhedron_Factor(P
, nparam
, options
->MaxRays
);
1735 if (T
|| (P
->Dimension
== nparam
+1)) {
1738 for (Q
= T
? T
: P
; Q
; Q
= Q
->next
) {
1739 Polyhedron
*next
= Q
->next
;
1743 if (Q
->Dimension
!= C
->Dimension
)
1744 QC
= Polyhedron_Project(Q
, nparam
);
1747 C
= DomainIntersection(C
, QC
, options
->MaxRays
);
1749 Polyhedron_Free(C2
);
1751 Polyhedron_Free(QC
);
1759 if (T
->Dimension
== C
->Dimension
) {
1766 Polyhedron
*next
= P
->next
;
1768 eres
= barvinok_enumerate_ev_f(P
, C
, options
);
1775 for (Q
= P
->next
; Q
; Q
= Q
->next
) {
1776 Polyhedron
*next
= Q
->next
;
1779 f
= barvinok_enumerate_ev_f(Q
, C
, options
);
1781 free_evalue_refs(f
);
1791 evalue
* barvinok_enumerate_ev(Polyhedron
*P
, Polyhedron
* C
, unsigned MaxRays
)
1794 barvinok_options
*options
= barvinok_options_new_with_defaults();
1795 options
->MaxRays
= MaxRays
;
1796 E
= barvinok_enumerate_with_options(P
, C
, options
);
1801 static evalue
* barvinok_enumerate_ev_f(Polyhedron
*P
, Polyhedron
* C
,
1802 barvinok_options
*options
)
1804 unsigned nparam
= C
->Dimension
;
1806 if (P
->Dimension
- nparam
== 1)
1807 return ParamLine_Length(P
, C
, options
);
1809 Param_Polyhedron
*PP
= NULL
;
1810 Polyhedron
*CEq
= NULL
, *pVD
;
1812 Param_Domain
*D
, *next
;
1815 Polyhedron
*Porig
= P
;
1817 PP
= Polyhedron2Param_SD(&P
,C
,options
->MaxRays
,&CEq
,&CT
);
1819 if (isIdentity(CT
)) {
1823 assert(CT
->NbRows
!= CT
->NbColumns
);
1824 if (CT
->NbRows
== 1) { // no more parameters
1825 eres
= barvinok_enumerate_cst(P
, CEq
, options
->MaxRays
);
1830 Param_Polyhedron_Free(PP
);
1836 nparam
= CT
->NbRows
- 1;
1839 unsigned dim
= P
->Dimension
- nparam
;
1841 ALLOC(evalue
, eres
);
1842 value_init(eres
->d
);
1843 value_set_si(eres
->d
, 0);
1846 for (nd
= 0, D
=PP
->D
; D
; ++nd
, D
=D
->next
);
1847 struct section
{ Polyhedron
*D
; evalue E
; };
1848 section
*s
= new section
[nd
];
1849 Polyhedron
**fVD
= new Polyhedron_p
[nd
];
1851 enumerator_base
*et
= NULL
;
1856 et
= enumerator_base::create(P
, dim
, PP
->nbV
, options
);
1858 for(nd
= 0, D
=PP
->D
; D
; D
=next
) {
1861 Polyhedron
*rVD
= reduce_domain(D
->Domain
, CT
, CEq
,
1862 fVD
, nd
, options
->MaxRays
);
1866 pVD
= CT
? DomainImage(rVD
,CT
,options
->MaxRays
) : rVD
;
1868 value_init(s
[nd
].E
.d
);
1869 evalue_set_si(&s
[nd
].E
, 0, 1);
1872 FORALL_PVertex_in_ParamPolyhedron(V
,D
,PP
) // _i is internal counter
1875 et
->decompose_at(V
, _i
, options
);
1876 } catch (OrthogonalException
&e
) {
1879 for (; nd
>= 0; --nd
) {
1880 free_evalue_refs(&s
[nd
].E
);
1881 Domain_Free(s
[nd
].D
);
1882 Domain_Free(fVD
[nd
]);
1886 eadd(et
->vE
[_i
] , &s
[nd
].E
);
1887 END_FORALL_PVertex_in_ParamPolyhedron
;
1888 evalue_range_reduction_in_domain(&s
[nd
].E
, pVD
);
1891 addeliminatedparams_evalue(&s
[nd
].E
, CT
);
1899 evalue_set_si(eres
, 0, 1);
1901 eres
->x
.p
= new_enode(partition
, 2*nd
, C
->Dimension
);
1902 for (int j
= 0; j
< nd
; ++j
) {
1903 EVALUE_SET_DOMAIN(eres
->x
.p
->arr
[2*j
], s
[j
].D
);
1904 value_clear(eres
->x
.p
->arr
[2*j
+1].d
);
1905 eres
->x
.p
->arr
[2*j
+1] = s
[j
].E
;
1906 Domain_Free(fVD
[j
]);
1913 Polyhedron_Free(CEq
);
1917 Enumeration
* barvinok_enumerate(Polyhedron
*P
, Polyhedron
* C
, unsigned MaxRays
)
1919 evalue
*EP
= barvinok_enumerate_ev(P
, C
, MaxRays
);
1921 return partition2enumeration(EP
);
1924 static void SwapColumns(Value
**V
, int n
, int i
, int j
)
1926 for (int r
= 0; r
< n
; ++r
)
1927 value_swap(V
[r
][i
], V
[r
][j
]);
1930 static void SwapColumns(Polyhedron
*P
, int i
, int j
)
1932 SwapColumns(P
->Constraint
, P
->NbConstraints
, i
, j
);
1933 SwapColumns(P
->Ray
, P
->NbRays
, i
, j
);
1936 /* Construct a constraint c from constraints l and u such that if
1937 * if constraint c holds then for each value of the other variables
1938 * there is at most one value of variable pos (position pos+1 in the constraints).
1940 * Given a lower and an upper bound
1941 * n_l v_i + <c_l,x> + c_l >= 0
1942 * -n_u v_i + <c_u,x> + c_u >= 0
1943 * the constructed constraint is
1945 * -(n_l<c_u,x> + n_u<c_l,x>) + (-n_l c_u - n_u c_l + n_l n_u - 1)
1947 * which is then simplified to remove the content of the non-constant coefficients
1949 * len is the total length of the constraints.
1950 * v is a temporary variable that can be used by this procedure
1952 static void negative_test_constraint(Value
*l
, Value
*u
, Value
*c
, int pos
,
1955 value_oppose(*v
, u
[pos
+1]);
1956 Vector_Combine(l
+1, u
+1, c
+1, *v
, l
[pos
+1], len
-1);
1957 value_multiply(*v
, *v
, l
[pos
+1]);
1958 value_subtract(c
[len
-1], c
[len
-1], *v
);
1959 value_set_si(*v
, -1);
1960 Vector_Scale(c
+1, c
+1, *v
, len
-1);
1961 value_decrement(c
[len
-1], c
[len
-1]);
1962 ConstraintSimplify(c
, c
, len
, v
);
1965 static bool parallel_constraints(Value
*l
, Value
*u
, Value
*c
, int pos
,
1974 Vector_Gcd(&l
[1+pos
], len
, &g1
);
1975 Vector_Gcd(&u
[1+pos
], len
, &g2
);
1976 Vector_Combine(l
+1+pos
, u
+1+pos
, c
+1, g2
, g1
, len
);
1977 parallel
= First_Non_Zero(c
+1, len
) == -1;
1985 static void negative_test_constraint7(Value
*l
, Value
*u
, Value
*c
, int pos
,
1986 int exist
, int len
, Value
*v
)
1991 Vector_Gcd(&u
[1+pos
], exist
, v
);
1992 Vector_Gcd(&l
[1+pos
], exist
, &g
);
1993 Vector_Combine(l
+1, u
+1, c
+1, *v
, g
, len
-1);
1994 value_multiply(*v
, *v
, g
);
1995 value_subtract(c
[len
-1], c
[len
-1], *v
);
1996 value_set_si(*v
, -1);
1997 Vector_Scale(c
+1, c
+1, *v
, len
-1);
1998 value_decrement(c
[len
-1], c
[len
-1]);
1999 ConstraintSimplify(c
, c
, len
, v
);
2004 /* Turns a x + b >= 0 into a x + b <= -1
2006 * len is the total length of the constraint.
2007 * v is a temporary variable that can be used by this procedure
2009 static void oppose_constraint(Value
*c
, int len
, Value
*v
)
2011 value_set_si(*v
, -1);
2012 Vector_Scale(c
+1, c
+1, *v
, len
-1);
2013 value_decrement(c
[len
-1], c
[len
-1]);
2016 /* Split polyhedron P into two polyhedra *pos and *neg, where
2017 * existential variable i has at most one solution for each
2018 * value of the other variables in *neg.
2020 * The splitting is performed using constraints l and u.
2022 * nvar: number of set variables
2023 * row: temporary vector that can be used by this procedure
2024 * f: temporary value that can be used by this procedure
2026 static bool SplitOnConstraint(Polyhedron
*P
, int i
, int l
, int u
,
2027 int nvar
, int MaxRays
, Vector
*row
, Value
& f
,
2028 Polyhedron
**pos
, Polyhedron
**neg
)
2030 negative_test_constraint(P
->Constraint
[l
], P
->Constraint
[u
],
2031 row
->p
, nvar
+i
, P
->Dimension
+2, &f
);
2032 *neg
= AddConstraints(row
->p
, 1, P
, MaxRays
);
2034 /* We found an independent, but useless constraint
2035 * Maybe we should detect this earlier and not
2036 * mark the variable as INDEPENDENT
2038 if (emptyQ((*neg
))) {
2039 Polyhedron_Free(*neg
);
2043 oppose_constraint(row
->p
, P
->Dimension
+2, &f
);
2044 *pos
= AddConstraints(row
->p
, 1, P
, MaxRays
);
2046 if (emptyQ((*pos
))) {
2047 Polyhedron_Free(*neg
);
2048 Polyhedron_Free(*pos
);
2056 * unimodularly transform P such that constraint r is transformed
2057 * into a constraint that involves only a single (the first)
2058 * existential variable
2061 static Polyhedron
*rotate_along(Polyhedron
*P
, int r
, int nvar
, int exist
,
2067 Vector
*row
= Vector_Alloc(exist
);
2068 Vector_Copy(P
->Constraint
[r
]+1+nvar
, row
->p
, exist
);
2069 Vector_Gcd(row
->p
, exist
, &g
);
2070 if (value_notone_p(g
))
2071 Vector_AntiScale(row
->p
, row
->p
, g
, exist
);
2074 Matrix
*M
= unimodular_complete(row
);
2075 Matrix
*M2
= Matrix_Alloc(P
->Dimension
+1, P
->Dimension
+1);
2076 for (r
= 0; r
< nvar
; ++r
)
2077 value_set_si(M2
->p
[r
][r
], 1);
2078 for ( ; r
< nvar
+exist
; ++r
)
2079 Vector_Copy(M
->p
[r
-nvar
], M2
->p
[r
]+nvar
, exist
);
2080 for ( ; r
< P
->Dimension
+1; ++r
)
2081 value_set_si(M2
->p
[r
][r
], 1);
2082 Polyhedron
*T
= Polyhedron_Image(P
, M2
, MaxRays
);
2091 /* Split polyhedron P into two polyhedra *pos and *neg, where
2092 * existential variable i has at most one solution for each
2093 * value of the other variables in *neg.
2095 * If independent is set, then the two constraints on which the
2096 * split will be performed need to be independent of the other
2097 * existential variables.
2099 * Return true if an appropriate split could be performed.
2101 * nvar: number of set variables
2102 * exist: number of existential variables
2103 * row: temporary vector that can be used by this procedure
2104 * f: temporary value that can be used by this procedure
2106 static bool SplitOnVar(Polyhedron
*P
, int i
,
2107 int nvar
, int exist
, int MaxRays
,
2108 Vector
*row
, Value
& f
, bool independent
,
2109 Polyhedron
**pos
, Polyhedron
**neg
)
2113 for (int l
= P
->NbEq
; l
< P
->NbConstraints
; ++l
) {
2114 if (value_negz_p(P
->Constraint
[l
][nvar
+i
+1]))
2118 for (j
= 0; j
< exist
; ++j
)
2119 if (j
!= i
&& value_notzero_p(P
->Constraint
[l
][nvar
+j
+1]))
2125 for (int u
= P
->NbEq
; u
< P
->NbConstraints
; ++u
) {
2126 if (value_posz_p(P
->Constraint
[u
][nvar
+i
+1]))
2130 for (j
= 0; j
< exist
; ++j
)
2131 if (j
!= i
&& value_notzero_p(P
->Constraint
[u
][nvar
+j
+1]))
2137 if (SplitOnConstraint(P
, i
, l
, u
, nvar
, MaxRays
, row
, f
, pos
, neg
)) {
2140 SwapColumns(*neg
, nvar
+1, nvar
+1+i
);
2150 static bool double_bound_pair(Polyhedron
*P
, int nvar
, int exist
,
2151 int i
, int l1
, int l2
,
2152 Polyhedron
**pos
, Polyhedron
**neg
)
2156 Vector
*row
= Vector_Alloc(P
->Dimension
+2);
2157 value_set_si(row
->p
[0], 1);
2158 value_oppose(f
, P
->Constraint
[l1
][nvar
+i
+1]);
2159 Vector_Combine(P
->Constraint
[l1
]+1, P
->Constraint
[l2
]+1,
2161 P
->Constraint
[l2
][nvar
+i
+1], f
,
2163 ConstraintSimplify(row
->p
, row
->p
, P
->Dimension
+2, &f
);
2164 *pos
= AddConstraints(row
->p
, 1, P
, 0);
2165 value_set_si(f
, -1);
2166 Vector_Scale(row
->p
+1, row
->p
+1, f
, P
->Dimension
+1);
2167 value_decrement(row
->p
[P
->Dimension
+1], row
->p
[P
->Dimension
+1]);
2168 *neg
= AddConstraints(row
->p
, 1, P
, 0);
2172 return !emptyQ((*pos
)) && !emptyQ((*neg
));
2175 static bool double_bound(Polyhedron
*P
, int nvar
, int exist
,
2176 Polyhedron
**pos
, Polyhedron
**neg
)
2178 for (int i
= 0; i
< exist
; ++i
) {
2180 for (l1
= P
->NbEq
; l1
< P
->NbConstraints
; ++l1
) {
2181 if (value_negz_p(P
->Constraint
[l1
][nvar
+i
+1]))
2183 for (l2
= l1
+ 1; l2
< P
->NbConstraints
; ++l2
) {
2184 if (value_negz_p(P
->Constraint
[l2
][nvar
+i
+1]))
2186 if (double_bound_pair(P
, nvar
, exist
, i
, l1
, l2
, pos
, neg
))
2190 for (l1
= P
->NbEq
; l1
< P
->NbConstraints
; ++l1
) {
2191 if (value_posz_p(P
->Constraint
[l1
][nvar
+i
+1]))
2193 if (l1
< P
->NbConstraints
)
2194 for (l2
= l1
+ 1; l2
< P
->NbConstraints
; ++l2
) {
2195 if (value_posz_p(P
->Constraint
[l2
][nvar
+i
+1]))
2197 if (double_bound_pair(P
, nvar
, exist
, i
, l1
, l2
, pos
, neg
))
2209 INDEPENDENT
= 1 << 2,
2213 static evalue
* enumerate_or(Polyhedron
*D
,
2214 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2217 fprintf(stderr
, "\nER: Or\n");
2218 #endif /* DEBUG_ER */
2220 Polyhedron
*N
= D
->next
;
2223 barvinok_enumerate_e_with_options(D
, exist
, nparam
, options
);
2226 for (D
= N
; D
; D
= N
) {
2231 barvinok_enumerate_e_with_options(D
, exist
, nparam
, options
);
2234 free_evalue_refs(EN
);
2244 static evalue
* enumerate_sum(Polyhedron
*P
,
2245 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2247 int nvar
= P
->Dimension
- exist
- nparam
;
2248 int toswap
= nvar
< exist
? nvar
: exist
;
2249 for (int i
= 0; i
< toswap
; ++i
)
2250 SwapColumns(P
, 1 + i
, nvar
+exist
- i
);
2254 fprintf(stderr
, "\nER: Sum\n");
2255 #endif /* DEBUG_ER */
2257 evalue
*EP
= barvinok_enumerate_e_with_options(P
, exist
, nparam
, options
);
2259 for (int i
= 0; i
< /* nvar */ nparam
; ++i
) {
2260 Matrix
*C
= Matrix_Alloc(1, 1 + nparam
+ 1);
2261 value_set_si(C
->p
[0][0], 1);
2263 value_init(split
.d
);
2264 value_set_si(split
.d
, 0);
2265 split
.x
.p
= new_enode(partition
, 4, nparam
);
2266 value_set_si(C
->p
[0][1+i
], 1);
2267 Matrix
*C2
= Matrix_Copy(C
);
2268 EVALUE_SET_DOMAIN(split
.x
.p
->arr
[0],
2269 Constraints2Polyhedron(C2
, options
->MaxRays
));
2271 evalue_set_si(&split
.x
.p
->arr
[1], 1, 1);
2272 value_set_si(C
->p
[0][1+i
], -1);
2273 value_set_si(C
->p
[0][1+nparam
], -1);
2274 EVALUE_SET_DOMAIN(split
.x
.p
->arr
[2],
2275 Constraints2Polyhedron(C
, options
->MaxRays
));
2276 evalue_set_si(&split
.x
.p
->arr
[3], 1, 1);
2278 free_evalue_refs(&split
);
2282 evalue_range_reduction(EP
);
2284 evalue_frac2floor2(EP
, 1);
2286 evalue
*sum
= esum(EP
, nvar
);
2288 free_evalue_refs(EP
);
2292 evalue_range_reduction(EP
);
2297 static evalue
* split_sure(Polyhedron
*P
, Polyhedron
*S
,
2298 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2300 int nvar
= P
->Dimension
- exist
- nparam
;
2302 Matrix
*M
= Matrix_Alloc(exist
, S
->Dimension
+2);
2303 for (int i
= 0; i
< exist
; ++i
)
2304 value_set_si(M
->p
[i
][nvar
+i
+1], 1);
2306 S
= DomainAddRays(S
, M
, options
->MaxRays
);
2308 Polyhedron
*F
= DomainAddRays(P
, M
, options
->MaxRays
);
2309 Polyhedron
*D
= DomainDifference(F
, S
, options
->MaxRays
);
2311 D
= Disjoint_Domain(D
, 0, options
->MaxRays
);
2316 M
= Matrix_Alloc(P
->Dimension
+1-exist
, P
->Dimension
+1);
2317 for (int j
= 0; j
< nvar
; ++j
)
2318 value_set_si(M
->p
[j
][j
], 1);
2319 for (int j
= 0; j
< nparam
+1; ++j
)
2320 value_set_si(M
->p
[nvar
+j
][nvar
+exist
+j
], 1);
2321 Polyhedron
*T
= Polyhedron_Image(S
, M
, options
->MaxRays
);
2322 evalue
*EP
= barvinok_enumerate_e_with_options(T
, 0, nparam
, options
);
2327 for (Polyhedron
*Q
= D
; Q
; Q
= Q
->next
) {
2328 Polyhedron
*N
= Q
->next
;
2330 T
= DomainIntersection(P
, Q
, options
->MaxRays
);
2331 evalue
*E
= barvinok_enumerate_e_with_options(T
, exist
, nparam
, options
);
2333 free_evalue_refs(E
);
2342 static evalue
* enumerate_sure(Polyhedron
*P
,
2343 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2347 int nvar
= P
->Dimension
- exist
- nparam
;
2353 for (i
= 0; i
< exist
; ++i
) {
2354 Matrix
*M
= Matrix_Alloc(S
->NbConstraints
, S
->Dimension
+2);
2356 value_set_si(lcm
, 1);
2357 for (int j
= 0; j
< S
->NbConstraints
; ++j
) {
2358 if (value_negz_p(S
->Constraint
[j
][1+nvar
+i
]))
2360 if (value_one_p(S
->Constraint
[j
][1+nvar
+i
]))
2362 value_lcm(lcm
, S
->Constraint
[j
][1+nvar
+i
], &lcm
);
2365 for (int j
= 0; j
< S
->NbConstraints
; ++j
) {
2366 if (value_negz_p(S
->Constraint
[j
][1+nvar
+i
]))
2368 if (value_one_p(S
->Constraint
[j
][1+nvar
+i
]))
2370 value_division(f
, lcm
, S
->Constraint
[j
][1+nvar
+i
]);
2371 Vector_Scale(S
->Constraint
[j
], M
->p
[c
], f
, S
->Dimension
+2);
2372 value_subtract(M
->p
[c
][S
->Dimension
+1],
2373 M
->p
[c
][S
->Dimension
+1],
2375 value_increment(M
->p
[c
][S
->Dimension
+1],
2376 M
->p
[c
][S
->Dimension
+1]);
2380 S
= AddConstraints(M
->p
[0], c
, S
, options
->MaxRays
);
2395 fprintf(stderr
, "\nER: Sure\n");
2396 #endif /* DEBUG_ER */
2398 return split_sure(P
, S
, exist
, nparam
, options
);
2401 static evalue
* enumerate_sure2(Polyhedron
*P
,
2402 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2404 int nvar
= P
->Dimension
- exist
- nparam
;
2406 for (r
= 0; r
< P
->NbRays
; ++r
)
2407 if (value_one_p(P
->Ray
[r
][0]) &&
2408 value_one_p(P
->Ray
[r
][P
->Dimension
+1]))
2414 Matrix
*M
= Matrix_Alloc(nvar
+ 1 + nparam
, P
->Dimension
+2);
2415 for (int i
= 0; i
< nvar
; ++i
)
2416 value_set_si(M
->p
[i
][1+i
], 1);
2417 for (int i
= 0; i
< nparam
; ++i
)
2418 value_set_si(M
->p
[i
+nvar
][1+nvar
+exist
+i
], 1);
2419 Vector_Copy(P
->Ray
[r
]+1+nvar
, M
->p
[nvar
+nparam
]+1+nvar
, exist
);
2420 value_set_si(M
->p
[nvar
+nparam
][0], 1);
2421 value_set_si(M
->p
[nvar
+nparam
][P
->Dimension
+1], 1);
2422 Polyhedron
* F
= Rays2Polyhedron(M
, options
->MaxRays
);
2425 Polyhedron
*I
= DomainIntersection(F
, P
, options
->MaxRays
);
2429 fprintf(stderr
, "\nER: Sure2\n");
2430 #endif /* DEBUG_ER */
2432 return split_sure(P
, I
, exist
, nparam
, options
);
2435 static evalue
* enumerate_cyclic(Polyhedron
*P
,
2436 unsigned exist
, unsigned nparam
,
2437 evalue
* EP
, int r
, int p
, unsigned MaxRays
)
2439 int nvar
= P
->Dimension
- exist
- nparam
;
2441 /* If EP in its fractional maps only contains references
2442 * to the remainder parameter with appropriate coefficients
2443 * then we could in principle avoid adding existentially
2444 * quantified variables to the validity domains.
2445 * We'd have to replace the remainder by m { p/m }
2446 * and multiply with an appropriate factor that is one
2447 * only in the appropriate range.
2448 * This last multiplication can be avoided if EP
2449 * has a single validity domain with no (further)
2450 * constraints on the remainder parameter
2453 Matrix
*CT
= Matrix_Alloc(nparam
+1, nparam
+3);
2454 Matrix
*M
= Matrix_Alloc(1, 1+nparam
+3);
2455 for (int j
= 0; j
< nparam
; ++j
)
2457 value_set_si(CT
->p
[j
][j
], 1);
2458 value_set_si(CT
->p
[p
][nparam
+1], 1);
2459 value_set_si(CT
->p
[nparam
][nparam
+2], 1);
2460 value_set_si(M
->p
[0][1+p
], -1);
2461 value_absolute(M
->p
[0][1+nparam
], P
->Ray
[0][1+nvar
+exist
+p
]);
2462 value_set_si(M
->p
[0][1+nparam
+1], 1);
2463 Polyhedron
*CEq
= Constraints2Polyhedron(M
, 1);
2465 addeliminatedparams_enum(EP
, CT
, CEq
, MaxRays
, nparam
);
2466 Polyhedron_Free(CEq
);
2472 static void enumerate_vd_add_ray(evalue
*EP
, Matrix
*Rays
, unsigned MaxRays
)
2474 if (value_notzero_p(EP
->d
))
2477 assert(EP
->x
.p
->type
== partition
);
2478 assert(EP
->x
.p
->pos
== EVALUE_DOMAIN(EP
->x
.p
->arr
[0])->Dimension
);
2479 for (int i
= 0; i
< EP
->x
.p
->size
/2; ++i
) {
2480 Polyhedron
*D
= EVALUE_DOMAIN(EP
->x
.p
->arr
[2*i
]);
2481 Polyhedron
*N
= DomainAddRays(D
, Rays
, MaxRays
);
2482 EVALUE_SET_DOMAIN(EP
->x
.p
->arr
[2*i
], N
);
2487 static evalue
* enumerate_line(Polyhedron
*P
,
2488 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2494 fprintf(stderr
, "\nER: Line\n");
2495 #endif /* DEBUG_ER */
2497 int nvar
= P
->Dimension
- exist
- nparam
;
2499 for (i
= 0; i
< nparam
; ++i
)
2500 if (value_notzero_p(P
->Ray
[0][1+nvar
+exist
+i
]))
2503 for (j
= i
+1; j
< nparam
; ++j
)
2504 if (value_notzero_p(P
->Ray
[0][1+nvar
+exist
+i
]))
2506 assert(j
>= nparam
); // for now
2508 Matrix
*M
= Matrix_Alloc(2, P
->Dimension
+2);
2509 value_set_si(M
->p
[0][0], 1);
2510 value_set_si(M
->p
[0][1+nvar
+exist
+i
], 1);
2511 value_set_si(M
->p
[1][0], 1);
2512 value_set_si(M
->p
[1][1+nvar
+exist
+i
], -1);
2513 value_absolute(M
->p
[1][1+P
->Dimension
], P
->Ray
[0][1+nvar
+exist
+i
]);
2514 value_decrement(M
->p
[1][1+P
->Dimension
], M
->p
[1][1+P
->Dimension
]);
2515 Polyhedron
*S
= AddConstraints(M
->p
[0], 2, P
, options
->MaxRays
);
2516 evalue
*EP
= barvinok_enumerate_e_with_options(S
, exist
, nparam
, options
);
2520 return enumerate_cyclic(P
, exist
, nparam
, EP
, 0, i
, options
->MaxRays
);
2523 static int single_param_pos(Polyhedron
*P
, unsigned exist
, unsigned nparam
,
2526 int nvar
= P
->Dimension
- exist
- nparam
;
2527 if (First_Non_Zero(P
->Ray
[r
]+1, nvar
) != -1)
2529 int i
= First_Non_Zero(P
->Ray
[r
]+1+nvar
+exist
, nparam
);
2532 if (First_Non_Zero(P
->Ray
[r
]+1+nvar
+exist
+1, nparam
-i
-1) != -1)
2537 static evalue
* enumerate_remove_ray(Polyhedron
*P
, int r
,
2538 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2541 fprintf(stderr
, "\nER: RedundantRay\n");
2542 #endif /* DEBUG_ER */
2546 value_set_si(one
, 1);
2547 int len
= P
->NbRays
-1;
2548 Matrix
*M
= Matrix_Alloc(2 * len
, P
->Dimension
+2);
2549 Vector_Copy(P
->Ray
[0], M
->p
[0], r
* (P
->Dimension
+2));
2550 Vector_Copy(P
->Ray
[r
+1], M
->p
[r
], (len
-r
) * (P
->Dimension
+2));
2551 for (int j
= 0; j
< P
->NbRays
; ++j
) {
2554 Vector_Combine(P
->Ray
[j
], P
->Ray
[r
], M
->p
[len
+j
-(j
>r
)],
2555 one
, P
->Ray
[j
][P
->Dimension
+1], P
->Dimension
+2);
2558 P
= Rays2Polyhedron(M
, options
->MaxRays
);
2560 evalue
*EP
= barvinok_enumerate_e_with_options(P
, exist
, nparam
, options
);
2567 static evalue
* enumerate_redundant_ray(Polyhedron
*P
,
2568 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2570 assert(P
->NbBid
== 0);
2571 int nvar
= P
->Dimension
- exist
- nparam
;
2575 for (int r
= 0; r
< P
->NbRays
; ++r
) {
2576 if (value_notzero_p(P
->Ray
[r
][P
->Dimension
+1]))
2578 int i1
= single_param_pos(P
, exist
, nparam
, r
);
2581 for (int r2
= r
+1; r2
< P
->NbRays
; ++r2
) {
2582 if (value_notzero_p(P
->Ray
[r2
][P
->Dimension
+1]))
2584 int i2
= single_param_pos(P
, exist
, nparam
, r2
);
2590 value_division(m
, P
->Ray
[r
][1+nvar
+exist
+i1
],
2591 P
->Ray
[r2
][1+nvar
+exist
+i1
]);
2592 value_multiply(m
, m
, P
->Ray
[r2
][1+nvar
+exist
+i1
]);
2593 /* r2 divides r => r redundant */
2594 if (value_eq(m
, P
->Ray
[r
][1+nvar
+exist
+i1
])) {
2596 return enumerate_remove_ray(P
, r
, exist
, nparam
, options
);
2599 value_division(m
, P
->Ray
[r2
][1+nvar
+exist
+i1
],
2600 P
->Ray
[r
][1+nvar
+exist
+i1
]);
2601 value_multiply(m
, m
, P
->Ray
[r
][1+nvar
+exist
+i1
]);
2602 /* r divides r2 => r2 redundant */
2603 if (value_eq(m
, P
->Ray
[r2
][1+nvar
+exist
+i1
])) {
2605 return enumerate_remove_ray(P
, r2
, exist
, nparam
, options
);
2613 static Polyhedron
*upper_bound(Polyhedron
*P
,
2614 int pos
, Value
*max
, Polyhedron
**R
)
2623 for (Polyhedron
*Q
= P
; Q
; Q
= N
) {
2625 for (r
= 0; r
< P
->NbRays
; ++r
) {
2626 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1]) &&
2627 value_pos_p(P
->Ray
[r
][1+pos
]))
2630 if (r
< P
->NbRays
) {
2638 for (r
= 0; r
< P
->NbRays
; ++r
) {
2639 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1]))
2641 mpz_fdiv_q(v
, P
->Ray
[r
][1+pos
], P
->Ray
[r
][1+P
->Dimension
]);
2642 if ((!Q
->next
&& r
== 0) || value_gt(v
, *max
))
2643 value_assign(*max
, v
);
2650 static evalue
* enumerate_ray(Polyhedron
*P
,
2651 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2653 assert(P
->NbBid
== 0);
2654 int nvar
= P
->Dimension
- exist
- nparam
;
2657 for (r
= 0; r
< P
->NbRays
; ++r
)
2658 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1]))
2664 for (r2
= r
+1; r2
< P
->NbRays
; ++r2
)
2665 if (value_zero_p(P
->Ray
[r2
][P
->Dimension
+1]))
2667 if (r2
< P
->NbRays
) {
2669 return enumerate_sum(P
, exist
, nparam
, options
);
2673 fprintf(stderr
, "\nER: Ray\n");
2674 #endif /* DEBUG_ER */
2680 value_set_si(one
, 1);
2681 int i
= single_param_pos(P
, exist
, nparam
, r
);
2682 assert(i
!= -1); // for now;
2684 Matrix
*M
= Matrix_Alloc(P
->NbRays
, P
->Dimension
+2);
2685 for (int j
= 0; j
< P
->NbRays
; ++j
) {
2686 Vector_Combine(P
->Ray
[j
], P
->Ray
[r
], M
->p
[j
],
2687 one
, P
->Ray
[j
][P
->Dimension
+1], P
->Dimension
+2);
2689 Polyhedron
*S
= Rays2Polyhedron(M
, options
->MaxRays
);
2691 Polyhedron
*D
= DomainDifference(P
, S
, options
->MaxRays
);
2693 // Polyhedron_Print(stderr, P_VALUE_FMT, D);
2694 assert(value_pos_p(P
->Ray
[r
][1+nvar
+exist
+i
])); // for now
2696 D
= upper_bound(D
, nvar
+exist
+i
, &m
, &R
);
2700 M
= Matrix_Alloc(2, P
->Dimension
+2);
2701 value_set_si(M
->p
[0][0], 1);
2702 value_set_si(M
->p
[1][0], 1);
2703 value_set_si(M
->p
[0][1+nvar
+exist
+i
], -1);
2704 value_set_si(M
->p
[1][1+nvar
+exist
+i
], 1);
2705 value_assign(M
->p
[0][1+P
->Dimension
], m
);
2706 value_oppose(M
->p
[1][1+P
->Dimension
], m
);
2707 value_addto(M
->p
[1][1+P
->Dimension
], M
->p
[1][1+P
->Dimension
],
2708 P
->Ray
[r
][1+nvar
+exist
+i
]);
2709 value_decrement(M
->p
[1][1+P
->Dimension
], M
->p
[1][1+P
->Dimension
]);
2710 // Matrix_Print(stderr, P_VALUE_FMT, M);
2711 D
= AddConstraints(M
->p
[0], 2, P
, options
->MaxRays
);
2712 // Polyhedron_Print(stderr, P_VALUE_FMT, D);
2713 value_subtract(M
->p
[0][1+P
->Dimension
], M
->p
[0][1+P
->Dimension
],
2714 P
->Ray
[r
][1+nvar
+exist
+i
]);
2715 // Matrix_Print(stderr, P_VALUE_FMT, M);
2716 S
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
2717 // Polyhedron_Print(stderr, P_VALUE_FMT, S);
2720 evalue
*EP
= barvinok_enumerate_e_with_options(D
, exist
, nparam
, options
);
2725 if (value_notone_p(P
->Ray
[r
][1+nvar
+exist
+i
]))
2726 EP
= enumerate_cyclic(P
, exist
, nparam
, EP
, r
, i
, options
->MaxRays
);
2728 M
= Matrix_Alloc(1, nparam
+2);
2729 value_set_si(M
->p
[0][0], 1);
2730 value_set_si(M
->p
[0][1+i
], 1);
2731 enumerate_vd_add_ray(EP
, M
, options
->MaxRays
);
2736 evalue
*E
= barvinok_enumerate_e_with_options(S
, exist
, nparam
, options
);
2738 free_evalue_refs(E
);
2745 evalue
*ER
= enumerate_or(R
, exist
, nparam
, options
);
2747 free_evalue_refs(ER
);
2754 static evalue
* enumerate_vd(Polyhedron
**PA
,
2755 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2757 Polyhedron
*P
= *PA
;
2758 int nvar
= P
->Dimension
- exist
- nparam
;
2759 Param_Polyhedron
*PP
= NULL
;
2760 Polyhedron
*C
= Universe_Polyhedron(nparam
);
2764 PP
= Polyhedron2Param_SimplifiedDomain(&PR
,C
, options
->MaxRays
,&CEq
,&CT
);
2768 Param_Domain
*D
, *last
;
2771 for (nd
= 0, D
=PP
->D
; D
; D
=D
->next
, ++nd
)
2774 Polyhedron
**VD
= new Polyhedron_p
[nd
];
2775 Polyhedron
**fVD
= new Polyhedron_p
[nd
];
2776 for(nd
= 0, D
=PP
->D
; D
; D
=D
->next
) {
2777 Polyhedron
*rVD
= reduce_domain(D
->Domain
, CT
, CEq
,
2778 fVD
, nd
, options
->MaxRays
);
2791 /* This doesn't seem to have any effect */
2793 Polyhedron
*CA
= align_context(VD
[0], P
->Dimension
, options
->MaxRays
);
2795 P
= DomainIntersection(P
, CA
, options
->MaxRays
);
2798 Polyhedron_Free(CA
);
2803 if (!EP
&& CT
->NbColumns
!= CT
->NbRows
) {
2804 Polyhedron
*CEqr
= DomainImage(CEq
, CT
, options
->MaxRays
);
2805 Polyhedron
*CA
= align_context(CEqr
, PR
->Dimension
, options
->MaxRays
);
2806 Polyhedron
*I
= DomainIntersection(PR
, CA
, options
->MaxRays
);
2807 Polyhedron_Free(CEqr
);
2808 Polyhedron_Free(CA
);
2810 fprintf(stderr
, "\nER: Eliminate\n");
2811 #endif /* DEBUG_ER */
2812 nparam
-= CT
->NbColumns
- CT
->NbRows
;
2813 EP
= barvinok_enumerate_e_with_options(I
, exist
, nparam
, options
);
2814 nparam
+= CT
->NbColumns
- CT
->NbRows
;
2815 addeliminatedparams_enum(EP
, CT
, CEq
, options
->MaxRays
, nparam
);
2819 Polyhedron_Free(PR
);
2822 if (!EP
&& nd
> 1) {
2824 fprintf(stderr
, "\nER: VD\n");
2825 #endif /* DEBUG_ER */
2826 for (int i
= 0; i
< nd
; ++i
) {
2827 Polyhedron
*CA
= align_context(VD
[i
], P
->Dimension
, options
->MaxRays
);
2828 Polyhedron
*I
= DomainIntersection(P
, CA
, options
->MaxRays
);
2831 EP
= barvinok_enumerate_e_with_options(I
, exist
, nparam
, options
);
2833 evalue
*E
= barvinok_enumerate_e_with_options(I
, exist
, nparam
,
2836 free_evalue_refs(E
);
2840 Polyhedron_Free(CA
);
2844 for (int i
= 0; i
< nd
; ++i
) {
2845 Polyhedron_Free(VD
[i
]);
2846 Polyhedron_Free(fVD
[i
]);
2852 if (!EP
&& nvar
== 0) {
2855 Param_Vertices
*V
, *V2
;
2856 Matrix
* M
= Matrix_Alloc(1, P
->Dimension
+2);
2858 FORALL_PVertex_in_ParamPolyhedron(V
, last
, PP
) {
2860 FORALL_PVertex_in_ParamPolyhedron(V2
, last
, PP
) {
2867 for (int i
= 0; i
< exist
; ++i
) {
2868 value_oppose(f
, V
->Vertex
->p
[i
][nparam
+1]);
2869 Vector_Combine(V
->Vertex
->p
[i
],
2871 M
->p
[0] + 1 + nvar
+ exist
,
2872 V2
->Vertex
->p
[i
][nparam
+1],
2876 for (j
= 0; j
< nparam
; ++j
)
2877 if (value_notzero_p(M
->p
[0][1+nvar
+exist
+j
]))
2881 ConstraintSimplify(M
->p
[0], M
->p
[0],
2882 P
->Dimension
+2, &f
);
2883 value_set_si(M
->p
[0][0], 0);
2884 Polyhedron
*para
= AddConstraints(M
->p
[0], 1, P
,
2887 Polyhedron_Free(para
);
2890 Polyhedron
*pos
, *neg
;
2891 value_set_si(M
->p
[0][0], 1);
2892 value_decrement(M
->p
[0][P
->Dimension
+1],
2893 M
->p
[0][P
->Dimension
+1]);
2894 neg
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
2895 value_set_si(f
, -1);
2896 Vector_Scale(M
->p
[0]+1, M
->p
[0]+1, f
,
2898 value_decrement(M
->p
[0][P
->Dimension
+1],
2899 M
->p
[0][P
->Dimension
+1]);
2900 value_decrement(M
->p
[0][P
->Dimension
+1],
2901 M
->p
[0][P
->Dimension
+1]);
2902 pos
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
2903 if (emptyQ(neg
) && emptyQ(pos
)) {
2904 Polyhedron_Free(para
);
2905 Polyhedron_Free(pos
);
2906 Polyhedron_Free(neg
);
2910 fprintf(stderr
, "\nER: Order\n");
2911 #endif /* DEBUG_ER */
2912 EP
= barvinok_enumerate_e_with_options(para
, exist
, nparam
,
2916 E
= barvinok_enumerate_e_with_options(pos
, exist
, nparam
,
2919 free_evalue_refs(E
);
2923 E
= barvinok_enumerate_e_with_options(neg
, exist
, nparam
,
2926 free_evalue_refs(E
);
2929 Polyhedron_Free(para
);
2930 Polyhedron_Free(pos
);
2931 Polyhedron_Free(neg
);
2936 } END_FORALL_PVertex_in_ParamPolyhedron
;
2939 } END_FORALL_PVertex_in_ParamPolyhedron
;
2942 /* Search for vertex coordinate to split on */
2943 /* First look for one independent of the parameters */
2944 FORALL_PVertex_in_ParamPolyhedron(V
, last
, PP
) {
2945 for (int i
= 0; i
< exist
; ++i
) {
2947 for (j
= 0; j
< nparam
; ++j
)
2948 if (value_notzero_p(V
->Vertex
->p
[i
][j
]))
2952 value_set_si(M
->p
[0][0], 1);
2953 Vector_Set(M
->p
[0]+1, 0, nvar
+exist
);
2954 Vector_Copy(V
->Vertex
->p
[i
],
2955 M
->p
[0] + 1 + nvar
+ exist
, nparam
+1);
2956 value_oppose(M
->p
[0][1+nvar
+i
],
2957 V
->Vertex
->p
[i
][nparam
+1]);
2959 Polyhedron
*pos
, *neg
;
2960 value_set_si(M
->p
[0][0], 1);
2961 value_decrement(M
->p
[0][P
->Dimension
+1],
2962 M
->p
[0][P
->Dimension
+1]);
2963 neg
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
2964 value_set_si(f
, -1);
2965 Vector_Scale(M
->p
[0]+1, M
->p
[0]+1, f
,
2967 value_decrement(M
->p
[0][P
->Dimension
+1],
2968 M
->p
[0][P
->Dimension
+1]);
2969 value_decrement(M
->p
[0][P
->Dimension
+1],
2970 M
->p
[0][P
->Dimension
+1]);
2971 pos
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
2972 if (emptyQ(neg
) || emptyQ(pos
)) {
2973 Polyhedron_Free(pos
);
2974 Polyhedron_Free(neg
);
2977 Polyhedron_Free(pos
);
2978 value_increment(M
->p
[0][P
->Dimension
+1],
2979 M
->p
[0][P
->Dimension
+1]);
2980 pos
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
2982 fprintf(stderr
, "\nER: Vertex\n");
2983 #endif /* DEBUG_ER */
2985 EP
= enumerate_or(pos
, exist
, nparam
, options
);
2990 } END_FORALL_PVertex_in_ParamPolyhedron
;
2994 /* Search for vertex coordinate to split on */
2995 /* Now look for one that depends on the parameters */
2996 FORALL_PVertex_in_ParamPolyhedron(V
, last
, PP
) {
2997 for (int i
= 0; i
< exist
; ++i
) {
2998 value_set_si(M
->p
[0][0], 1);
2999 Vector_Set(M
->p
[0]+1, 0, nvar
+exist
);
3000 Vector_Copy(V
->Vertex
->p
[i
],
3001 M
->p
[0] + 1 + nvar
+ exist
, nparam
+1);
3002 value_oppose(M
->p
[0][1+nvar
+i
],
3003 V
->Vertex
->p
[i
][nparam
+1]);
3005 Polyhedron
*pos
, *neg
;
3006 value_set_si(M
->p
[0][0], 1);
3007 value_decrement(M
->p
[0][P
->Dimension
+1],
3008 M
->p
[0][P
->Dimension
+1]);
3009 neg
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
3010 value_set_si(f
, -1);
3011 Vector_Scale(M
->p
[0]+1, M
->p
[0]+1, f
,
3013 value_decrement(M
->p
[0][P
->Dimension
+1],
3014 M
->p
[0][P
->Dimension
+1]);
3015 value_decrement(M
->p
[0][P
->Dimension
+1],
3016 M
->p
[0][P
->Dimension
+1]);
3017 pos
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
3018 if (emptyQ(neg
) || emptyQ(pos
)) {
3019 Polyhedron_Free(pos
);
3020 Polyhedron_Free(neg
);
3023 Polyhedron_Free(pos
);
3024 value_increment(M
->p
[0][P
->Dimension
+1],
3025 M
->p
[0][P
->Dimension
+1]);
3026 pos
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
3028 fprintf(stderr
, "\nER: ParamVertex\n");
3029 #endif /* DEBUG_ER */
3031 EP
= enumerate_or(pos
, exist
, nparam
, options
);
3036 } END_FORALL_PVertex_in_ParamPolyhedron
;
3044 Polyhedron_Free(CEq
);
3048 Param_Polyhedron_Free(PP
);
3054 evalue
* barvinok_enumerate_pip(Polyhedron
*P
, unsigned exist
, unsigned nparam
,
3058 barvinok_options
*options
= barvinok_options_new_with_defaults();
3059 options
->MaxRays
= MaxRays
;
3060 E
= barvinok_enumerate_pip_with_options(P
, exist
, nparam
, options
);
3066 evalue
*barvinok_enumerate_pip_with_options(Polyhedron
*P
,
3067 unsigned exist
, unsigned nparam
, struct barvinok_options
*options
)
3072 evalue
*barvinok_enumerate_pip_with_options(Polyhedron
*P
,
3073 unsigned exist
, unsigned nparam
, struct barvinok_options
*options
)
3075 int nvar
= P
->Dimension
- exist
- nparam
;
3076 evalue
*EP
= evalue_zero();
3080 fprintf(stderr
, "\nER: PIP\n");
3081 #endif /* DEBUG_ER */
3083 Polyhedron
*D
= pip_projectout(P
, nvar
, exist
, nparam
);
3084 for (Q
= D
; Q
; Q
= N
) {
3088 exist
= Q
->Dimension
- nvar
- nparam
;
3089 E
= barvinok_enumerate_e_with_options(Q
, exist
, nparam
, options
);
3092 free_evalue_refs(E
);
3101 static bool is_single(Value
*row
, int pos
, int len
)
3103 return First_Non_Zero(row
, pos
) == -1 &&
3104 First_Non_Zero(row
+pos
+1, len
-pos
-1) == -1;
3107 static evalue
* barvinok_enumerate_e_r(Polyhedron
*P
,
3108 unsigned exist
, unsigned nparam
, barvinok_options
*options
);
3111 static int er_level
= 0;
3113 evalue
* barvinok_enumerate_e_with_options(Polyhedron
*P
,
3114 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
3116 fprintf(stderr
, "\nER: level %i\n", er_level
);
3118 Polyhedron_PrintConstraints(stderr
, P_VALUE_FMT
, P
);
3119 fprintf(stderr
, "\nE %d\nP %d\n", exist
, nparam
);
3121 P
= DomainConstraintSimplify(Polyhedron_Copy(P
), options
->MaxRays
);
3122 evalue
*EP
= barvinok_enumerate_e_r(P
, exist
, nparam
, options
);
3128 evalue
* barvinok_enumerate_e_with_options(Polyhedron
*P
,
3129 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
3131 P
= DomainConstraintSimplify(Polyhedron_Copy(P
), options
->MaxRays
);
3132 evalue
*EP
= barvinok_enumerate_e_r(P
, exist
, nparam
, options
);
3138 evalue
* barvinok_enumerate_e(Polyhedron
*P
, unsigned exist
, unsigned nparam
,
3142 barvinok_options
*options
= barvinok_options_new_with_defaults();
3143 options
->MaxRays
= MaxRays
;
3144 E
= barvinok_enumerate_e_with_options(P
, exist
, nparam
, options
);
3149 static evalue
* barvinok_enumerate_e_r(Polyhedron
*P
,
3150 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
3153 Polyhedron
*U
= Universe_Polyhedron(nparam
);
3154 evalue
*EP
= barvinok_enumerate_with_options(P
, U
, options
);
3155 //char *param_name[] = {"P", "Q", "R", "S", "T" };
3156 //print_evalue(stdout, EP, param_name);
3161 int nvar
= P
->Dimension
- exist
- nparam
;
3162 int len
= P
->Dimension
+ 2;
3165 POL_ENSURE_FACETS(P
);
3166 POL_ENSURE_VERTICES(P
);
3169 return evalue_zero();
3171 if (nvar
== 0 && nparam
== 0) {
3172 evalue
*EP
= evalue_zero();
3173 barvinok_count_with_options(P
, &EP
->x
.n
, options
);
3174 if (value_pos_p(EP
->x
.n
))
3175 value_set_si(EP
->x
.n
, 1);
3180 for (r
= 0; r
< P
->NbRays
; ++r
)
3181 if (value_zero_p(P
->Ray
[r
][0]) ||
3182 value_zero_p(P
->Ray
[r
][P
->Dimension
+1])) {
3184 for (i
= 0; i
< nvar
; ++i
)
3185 if (value_notzero_p(P
->Ray
[r
][i
+1]))
3189 for (i
= nvar
+ exist
; i
< nvar
+ exist
+ nparam
; ++i
)
3190 if (value_notzero_p(P
->Ray
[r
][i
+1]))
3192 if (i
>= nvar
+ exist
+ nparam
)
3195 if (r
< P
->NbRays
) {
3196 evalue
*EP
= evalue_zero();
3197 value_set_si(EP
->x
.n
, -1);
3202 for (r
= 0; r
< P
->NbEq
; ++r
)
3203 if ((first
= First_Non_Zero(P
->Constraint
[r
]+1+nvar
, exist
)) != -1)
3206 if (First_Non_Zero(P
->Constraint
[r
]+1+nvar
+first
+1,
3207 exist
-first
-1) != -1) {
3208 Polyhedron
*T
= rotate_along(P
, r
, nvar
, exist
, options
->MaxRays
);
3210 fprintf(stderr
, "\nER: Equality\n");
3211 #endif /* DEBUG_ER */
3212 evalue
*EP
= barvinok_enumerate_e_with_options(T
, exist
-1, nparam
,
3218 fprintf(stderr
, "\nER: Fixed\n");
3219 #endif /* DEBUG_ER */
3221 return barvinok_enumerate_e_with_options(P
, exist
-1, nparam
,
3224 Polyhedron
*T
= Polyhedron_Copy(P
);
3225 SwapColumns(T
, nvar
+1, nvar
+1+first
);
3226 evalue
*EP
= barvinok_enumerate_e_with_options(T
, exist
-1, nparam
,
3234 Vector
*row
= Vector_Alloc(len
);
3235 value_set_si(row
->p
[0], 1);
3240 enum constraint
* info
= new constraint
[exist
];
3241 for (int i
= 0; i
< exist
; ++i
) {
3243 for (int l
= P
->NbEq
; l
< P
->NbConstraints
; ++l
) {
3244 if (value_negz_p(P
->Constraint
[l
][nvar
+i
+1]))
3246 bool l_parallel
= is_single(P
->Constraint
[l
]+nvar
+1, i
, exist
);
3247 for (int u
= P
->NbEq
; u
< P
->NbConstraints
; ++u
) {
3248 if (value_posz_p(P
->Constraint
[u
][nvar
+i
+1]))
3250 bool lu_parallel
= l_parallel
||
3251 is_single(P
->Constraint
[u
]+nvar
+1, i
, exist
);
3252 value_oppose(f
, P
->Constraint
[u
][nvar
+i
+1]);
3253 Vector_Combine(P
->Constraint
[l
]+1, P
->Constraint
[u
]+1, row
->p
+1,
3254 f
, P
->Constraint
[l
][nvar
+i
+1], len
-1);
3255 if (!(info
[i
] & INDEPENDENT
)) {
3257 for (j
= 0; j
< exist
; ++j
)
3258 if (j
!= i
&& value_notzero_p(row
->p
[nvar
+j
+1]))
3261 //printf("independent: i: %d, l: %d, u: %d\n", i, l, u);
3262 info
[i
] = (constraint
)(info
[i
] | INDEPENDENT
);
3265 if (info
[i
] & ALL_POS
) {
3266 value_addto(row
->p
[len
-1], row
->p
[len
-1],
3267 P
->Constraint
[l
][nvar
+i
+1]);
3268 value_addto(row
->p
[len
-1], row
->p
[len
-1], f
);
3269 value_multiply(f
, f
, P
->Constraint
[l
][nvar
+i
+1]);
3270 value_subtract(row
->p
[len
-1], row
->p
[len
-1], f
);
3271 value_decrement(row
->p
[len
-1], row
->p
[len
-1]);
3272 ConstraintSimplify(row
->p
, row
->p
, len
, &f
);
3273 value_set_si(f
, -1);
3274 Vector_Scale(row
->p
+1, row
->p
+1, f
, len
-1);
3275 value_decrement(row
->p
[len
-1], row
->p
[len
-1]);
3276 Polyhedron
*T
= AddConstraints(row
->p
, 1, P
, options
->MaxRays
);
3278 //printf("not all_pos: i: %d, l: %d, u: %d\n", i, l, u);
3279 info
[i
] = (constraint
)(info
[i
] ^ ALL_POS
);
3281 //puts("pos remainder");
3282 //Polyhedron_Print(stdout, P_VALUE_FMT, T);
3285 if (!(info
[i
] & ONE_NEG
)) {
3287 negative_test_constraint(P
->Constraint
[l
],
3289 row
->p
, nvar
+i
, len
, &f
);
3290 oppose_constraint(row
->p
, len
, &f
);
3291 Polyhedron
*T
= AddConstraints(row
->p
, 1, P
,
3294 //printf("one_neg i: %d, l: %d, u: %d\n", i, l, u);
3295 info
[i
] = (constraint
)(info
[i
] | ONE_NEG
);
3297 //puts("neg remainder");
3298 //Polyhedron_Print(stdout, P_VALUE_FMT, T);
3300 } else if (!(info
[i
] & ROT_NEG
)) {
3301 if (parallel_constraints(P
->Constraint
[l
],
3303 row
->p
, nvar
, exist
)) {
3304 negative_test_constraint7(P
->Constraint
[l
],
3306 row
->p
, nvar
, exist
,
3308 oppose_constraint(row
->p
, len
, &f
);
3309 Polyhedron
*T
= AddConstraints(row
->p
, 1, P
,
3312 // printf("rot_neg i: %d, l: %d, u: %d\n", i, l, u);
3313 info
[i
] = (constraint
)(info
[i
] | ROT_NEG
);
3316 //puts("neg remainder");
3317 //Polyhedron_Print(stdout, P_VALUE_FMT, T);
3322 if (!(info
[i
] & ALL_POS
) && (info
[i
] & (ONE_NEG
| ROT_NEG
)))
3326 if (info
[i
] & ALL_POS
)
3333 for (int i = 0; i < exist; ++i)
3334 printf("%i: %i\n", i, info[i]);
3336 for (int i
= 0; i
< exist
; ++i
)
3337 if (info
[i
] & ALL_POS
) {
3339 fprintf(stderr
, "\nER: Positive\n");
3340 #endif /* DEBUG_ER */
3342 // Maybe we should chew off some of the fat here
3343 Matrix
*M
= Matrix_Alloc(P
->Dimension
, P
->Dimension
+1);
3344 for (int j
= 0; j
< P
->Dimension
; ++j
)
3345 value_set_si(M
->p
[j
][j
+ (j
>= i
+nvar
)], 1);
3346 Polyhedron
*T
= Polyhedron_Image(P
, M
, options
->MaxRays
);
3348 evalue
*EP
= barvinok_enumerate_e_with_options(T
, exist
-1, nparam
,
3356 for (int i
= 0; i
< exist
; ++i
)
3357 if (info
[i
] & ONE_NEG
) {
3359 fprintf(stderr
, "\nER: Negative\n");
3360 #endif /* DEBUG_ER */
3365 return barvinok_enumerate_e_with_options(P
, exist
-1, nparam
,
3368 Polyhedron
*T
= Polyhedron_Copy(P
);
3369 SwapColumns(T
, nvar
+1, nvar
+1+i
);
3370 evalue
*EP
= barvinok_enumerate_e_with_options(T
, exist
-1, nparam
,
3376 for (int i
= 0; i
< exist
; ++i
)
3377 if (info
[i
] & ROT_NEG
) {
3379 fprintf(stderr
, "\nER: Rotate\n");
3380 #endif /* DEBUG_ER */
3384 Polyhedron
*T
= rotate_along(P
, r
, nvar
, exist
, options
->MaxRays
);
3385 evalue
*EP
= barvinok_enumerate_e_with_options(T
, exist
-1, nparam
,
3390 for (int i
= 0; i
< exist
; ++i
)
3391 if (info
[i
] & INDEPENDENT
) {
3392 Polyhedron
*pos
, *neg
;
3394 /* Find constraint again and split off negative part */
3396 if (SplitOnVar(P
, i
, nvar
, exist
, options
->MaxRays
,
3397 row
, f
, true, &pos
, &neg
)) {
3399 fprintf(stderr
, "\nER: Split\n");
3400 #endif /* DEBUG_ER */
3403 barvinok_enumerate_e_with_options(neg
, exist
-1, nparam
, options
);
3405 barvinok_enumerate_e_with_options(pos
, exist
, nparam
, options
);
3407 free_evalue_refs(E
);
3409 Polyhedron_Free(neg
);
3410 Polyhedron_Free(pos
);
3424 EP
= enumerate_line(P
, exist
, nparam
, options
);
3428 EP
= barvinok_enumerate_pip_with_options(P
, exist
, nparam
, options
);
3432 EP
= enumerate_redundant_ray(P
, exist
, nparam
, options
);
3436 EP
= enumerate_sure(P
, exist
, nparam
, options
);
3440 EP
= enumerate_ray(P
, exist
, nparam
, options
);
3444 EP
= enumerate_sure2(P
, exist
, nparam
, options
);
3448 F
= unfringe(P
, options
->MaxRays
);
3449 if (!PolyhedronIncludes(F
, P
)) {
3451 fprintf(stderr
, "\nER: Fringed\n");
3452 #endif /* DEBUG_ER */
3453 EP
= barvinok_enumerate_e_with_options(F
, exist
, nparam
, options
);
3460 EP
= enumerate_vd(&P
, exist
, nparam
, options
);
3465 EP
= enumerate_sum(P
, exist
, nparam
, options
);
3472 Polyhedron
*pos
, *neg
;
3473 for (i
= 0; i
< exist
; ++i
)
3474 if (SplitOnVar(P
, i
, nvar
, exist
, options
->MaxRays
,
3475 row
, f
, false, &pos
, &neg
))
3481 EP
= enumerate_or(pos
, exist
, nparam
, options
);
3494 * remove equalities that require a "compression" of the parameters
3496 static Polyhedron
*remove_more_equalities(Polyhedron
*P
, unsigned nparam
,
3497 Matrix
**CP
, unsigned MaxRays
)
3500 remove_all_equalities(&P
, NULL
, CP
, NULL
, nparam
, MaxRays
);
3507 static gen_fun
*series(Polyhedron
*P
, unsigned nparam
, barvinok_options
*options
)
3517 assert(!Polyhedron_is_infinite_param(P
, nparam
));
3518 assert(P
->NbBid
== 0);
3519 assert(Polyhedron_has_revlex_positive_rays(P
, nparam
));
3521 P
= remove_more_equalities(P
, nparam
, &CP
, options
->MaxRays
);
3522 assert(P
->NbEq
== 0);
3524 nparam
= CP
->NbColumns
-1;
3529 barvinok_count_with_options(P
, &c
, options
);
3530 gf
= new gen_fun(c
);
3534 red
= gf_base::create(Polyhedron_Project(P
, nparam
),
3535 P
->Dimension
, nparam
, options
);
3536 POL_ENSURE_VERTICES(P
);
3537 red
->start_gf(P
, options
);
3549 gen_fun
* barvinok_series_with_options(Polyhedron
*P
, Polyhedron
* C
,
3550 barvinok_options
*options
)
3553 unsigned nparam
= C
->Dimension
;
3556 CA
= align_context(C
, P
->Dimension
, options
->MaxRays
);
3557 P
= DomainIntersection(P
, CA
, options
->MaxRays
);
3558 Polyhedron_Free(CA
);
3560 gf
= series(P
, nparam
, options
);
3565 gen_fun
* barvinok_series(Polyhedron
*P
, Polyhedron
* C
, unsigned MaxRays
)
3568 barvinok_options
*options
= barvinok_options_new_with_defaults();
3569 options
->MaxRays
= MaxRays
;
3570 gf
= barvinok_series_with_options(P
, C
, options
);
3575 static Polyhedron
*skew_into_positive_orthant(Polyhedron
*D
, unsigned nparam
,
3581 for (Polyhedron
*P
= D
; P
; P
= P
->next
) {
3582 POL_ENSURE_VERTICES(P
);
3583 assert(!Polyhedron_is_infinite_param(P
, nparam
));
3584 assert(P
->NbBid
== 0);
3585 assert(Polyhedron_has_positive_rays(P
, nparam
));
3587 for (int r
= 0; r
< P
->NbRays
; ++r
) {
3588 if (value_notzero_p(P
->Ray
[r
][P
->Dimension
+1]))
3590 for (int i
= 0; i
< nparam
; ++i
) {
3592 if (value_posz_p(P
->Ray
[r
][i
+1]))
3595 M
= Matrix_Alloc(D
->Dimension
+1, D
->Dimension
+1);
3596 for (int i
= 0; i
< D
->Dimension
+1; ++i
)
3597 value_set_si(M
->p
[i
][i
], 1);
3599 Inner_Product(P
->Ray
[r
]+1, M
->p
[i
], D
->Dimension
+1, &tmp
);
3600 if (value_posz_p(tmp
))
3603 for (j
= P
->Dimension
- nparam
; j
< P
->Dimension
; ++j
)
3604 if (value_pos_p(P
->Ray
[r
][j
+1]))
3606 assert(j
< P
->Dimension
);
3607 value_pdivision(tmp
, P
->Ray
[r
][j
+1], P
->Ray
[r
][i
+1]);
3608 value_subtract(M
->p
[i
][j
], M
->p
[i
][j
], tmp
);
3614 D
= DomainImage(D
, M
, MaxRays
);
3620 gen_fun
* barvinok_enumerate_union_series_with_options(Polyhedron
*D
, Polyhedron
* C
,
3621 barvinok_options
*options
)
3623 Polyhedron
*conv
, *D2
;
3625 gen_fun
*gf
= NULL
, *gf2
;
3626 unsigned nparam
= C
->Dimension
;
3631 CA
= align_context(C
, D
->Dimension
, options
->MaxRays
);
3632 D
= DomainIntersection(D
, CA
, options
->MaxRays
);
3633 Polyhedron_Free(CA
);
3635 D2
= skew_into_positive_orthant(D
, nparam
, options
->MaxRays
);
3636 for (Polyhedron
*P
= D2
; P
; P
= P
->next
) {
3637 assert(P
->Dimension
== D2
->Dimension
);
3640 P_gf
= series(Polyhedron_Copy(P
), nparam
, options
);
3644 gf
->add_union(P_gf
, options
);
3648 /* we actually only need the convex union of the parameter space
3649 * but the reducer classes currently expect a polyhedron in
3650 * the combined space
3652 Polyhedron_Free(gf
->context
);
3653 gf
->context
= DomainConvex(D2
, options
->MaxRays
);
3655 gf2
= gf
->summate(D2
->Dimension
- nparam
, options
);
3664 gen_fun
* barvinok_enumerate_union_series(Polyhedron
*D
, Polyhedron
* C
,
3668 barvinok_options
*options
= barvinok_options_new_with_defaults();
3669 options
->MaxRays
= MaxRays
;
3670 gf
= barvinok_enumerate_union_series_with_options(D
, C
, options
);
3675 evalue
* barvinok_enumerate_union(Polyhedron
*D
, Polyhedron
* C
, unsigned MaxRays
)
3678 gen_fun
*gf
= barvinok_enumerate_union_series(D
, C
, MaxRays
);