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
.SetLength(dim
);
1188 den
.SetDims(dim
, dim
);
1196 virtual void handle(const signed_cone
& sc
, barvinok_options
*options
);
1197 void reduce(evalue
*factor
, vec_ZZ
& num
, mat_ZZ
& den_f
,
1198 barvinok_options
*options
);
1201 void ienumerator::reduce(evalue
*factor
, vec_ZZ
& num
, mat_ZZ
& den_f
,
1202 barvinok_options
*options
)
1204 unsigned len
= den_f
.NumRows(); // number of factors in den
1205 unsigned dim
= num
.length();
1208 eadd(factor
, vE
[vert
]);
1213 den_s
.SetLength(len
);
1215 den_r
.SetDims(len
, dim
-1);
1219 for (r
= 0; r
< len
; ++r
) {
1220 den_s
[r
] = den_f
[r
][0];
1221 for (k
= 0; k
<= dim
-1; ++k
)
1223 den_r
[r
][k
-(k
>0)] = den_f
[r
][k
];
1228 num_p
.SetLength(dim
-1);
1229 for (k
= 0 ; k
<= dim
-1; ++k
)
1231 num_p
[k
-(k
>0)] = num
[k
];
1234 den_p
.SetLength(len
);
1238 normalize(one
, num_s
, num_p
, den_s
, den_p
, den_r
);
1240 emul(&mone
, factor
);
1244 for (int k
= 0; k
< len
; ++k
) {
1247 else if (den_s
[k
] == 0)
1250 if (no_param
== 0) {
1251 reduce(factor
, num_p
, den_r
, options
);
1255 pden
.SetDims(only_param
, dim
-1);
1257 for (k
= 0, l
= 0; k
< len
; ++k
)
1259 pden
[l
++] = den_r
[k
];
1261 for (k
= 0; k
< len
; ++k
)
1265 dpoly
n(no_param
, num_s
);
1266 dpoly
D(no_param
, den_s
[k
], 1);
1267 for ( ; ++k
< len
; )
1268 if (den_p
[k
] == 0) {
1269 dpoly
fact(no_param
, den_s
[k
], 1);
1274 // if no_param + only_param == len then all powers
1275 // below will be all zero
1276 if (no_param
+ only_param
== len
) {
1277 if (E_num(0, dim
) != 0)
1278 r
= new dpoly_r(n
, len
);
1280 mpq_set_si(tcount
, 0, 1);
1282 n
.div(D
, tcount
, one
);
1284 if (value_notzero_p(mpq_numref(tcount
))) {
1288 value_assign(f
.x
.n
, mpq_numref(tcount
));
1289 value_assign(f
.d
, mpq_denref(tcount
));
1291 reduce(factor
, num_p
, pden
, options
);
1292 free_evalue_refs(&f
);
1297 for (k
= 0; k
< len
; ++k
) {
1298 if (den_s
[k
] == 0 || den_p
[k
] == 0)
1301 dpoly
pd(no_param
-1, den_s
[k
], 1);
1304 for (l
= 0; l
< k
; ++l
)
1305 if (den_r
[l
] == den_r
[k
])
1309 r
= new dpoly_r(n
, pd
, l
, len
);
1311 dpoly_r
*nr
= new dpoly_r(r
, pd
, l
, len
);
1317 dpoly_r
*rc
= r
->div(D
);
1320 if (E_num(0, dim
) == 0) {
1321 int common
= pden
.NumRows();
1322 dpoly_r_term_list
& final
= r
->c
[r
->len
-1];
1328 zz2value(r
->denom
, f
.d
);
1329 dpoly_r_term_list::iterator j
;
1330 for (j
= final
.begin(); j
!= final
.end(); ++j
) {
1331 if ((*j
)->coeff
== 0)
1334 for (int k
= 0; k
< r
->dim
; ++k
) {
1335 int n
= (*j
)->powers
[k
];
1338 pden
.SetDims(rows
+n
, pden
.NumCols());
1339 for (int l
= 0; l
< n
; ++l
)
1340 pden
[rows
+l
] = den_r
[k
];
1344 evalue_copy(&t
, factor
);
1345 zz2value((*j
)->coeff
, f
.x
.n
);
1347 reduce(&t
, num_p
, pden
, options
);
1348 free_evalue_refs(&t
);
1350 free_evalue_refs(&f
);
1352 ie_cum
cum(factor
, E_num(0, dim
), r
);
1353 cum
.cumulate(options
);
1355 int common
= pden
.NumRows();
1357 for (int j
= 0; j
< cum
.terms
.size(); ++j
) {
1359 pden
.SetDims(rows
, pden
.NumCols());
1360 for (int k
= 0; k
< r
->dim
; ++k
) {
1361 int n
= cum
.terms
[j
]->powers
[k
];
1364 pden
.SetDims(rows
+n
, pden
.NumCols());
1365 for (int l
= 0; l
< n
; ++l
)
1366 pden
[rows
+l
] = den_r
[k
];
1369 reduce(cum
.terms
[j
]->E
, num_p
, pden
, options
);
1370 free_evalue_refs(cum
.terms
[j
]->E
);
1371 delete cum
.terms
[j
]->E
;
1372 delete cum
.terms
[j
];
1379 static int type_offset(enode
*p
)
1381 return p
->type
== fractional
? 1 :
1382 p
->type
== flooring
? 1 : 0;
1385 static int edegree(evalue
*e
)
1390 if (value_notzero_p(e
->d
))
1394 int i
= type_offset(p
);
1395 if (p
->size
-i
-1 > d
)
1396 d
= p
->size
- i
- 1;
1397 for (; i
< p
->size
; i
++) {
1398 int d2
= edegree(&p
->arr
[i
]);
1405 void ienumerator::handle(const signed_cone
& sc
, barvinok_options
*options
)
1408 assert(sc
.rays
.NumRows() == dim
);
1410 lattice_point(V
, sc
.rays
, vertex
, E_vertex
, options
);
1416 evalue_set_si(&one
, sc
.sign
, 1);
1417 reduce(&one
, vertex
, den
, options
);
1418 free_evalue_refs(&one
);
1420 for (int i
= 0; i
< dim
; ++i
)
1422 free_evalue_refs(E_vertex
[i
]);
1427 struct bfenumerator
: public vertex_decomposer
, public bf_base
,
1428 public ienumerator_base
{
1431 bfenumerator(Polyhedron
*P
, unsigned dim
, unsigned nbV
) :
1432 vertex_decomposer(P
, nbV
, *this),
1433 bf_base(dim
), ienumerator_base(dim
, this) {
1441 virtual void handle(const signed_cone
& sc
, barvinok_options
*options
);
1442 virtual void base(mat_ZZ
& factors
, bfc_vec
& v
);
1444 bfc_term_base
* new_bf_term(int len
) {
1445 bfe_term
* t
= new bfe_term(len
);
1449 virtual void set_factor(bfc_term_base
*t
, int k
, int change
) {
1450 bfe_term
* bfet
= static_cast<bfe_term
*>(t
);
1451 factor
= bfet
->factors
[k
];
1452 assert(factor
!= NULL
);
1453 bfet
->factors
[k
] = NULL
;
1455 emul(&mone
, factor
);
1458 virtual void set_factor(bfc_term_base
*t
, int k
, mpq_t
&q
, int change
) {
1459 bfe_term
* bfet
= static_cast<bfe_term
*>(t
);
1460 factor
= bfet
->factors
[k
];
1461 assert(factor
!= NULL
);
1462 bfet
->factors
[k
] = NULL
;
1468 value_oppose(f
.x
.n
, mpq_numref(q
));
1470 value_assign(f
.x
.n
, mpq_numref(q
));
1471 value_assign(f
.d
, mpq_denref(q
));
1473 free_evalue_refs(&f
);
1476 virtual void set_factor(bfc_term_base
*t
, int k
, const QQ
& c
, int change
) {
1477 bfe_term
* bfet
= static_cast<bfe_term
*>(t
);
1479 factor
= new evalue
;
1484 zz2value(c
.n
, f
.x
.n
);
1486 value_oppose(f
.x
.n
, f
.x
.n
);
1489 value_init(factor
->d
);
1490 evalue_copy(factor
, bfet
->factors
[k
]);
1492 free_evalue_refs(&f
);
1495 void set_factor(evalue
*f
, int change
) {
1501 virtual void insert_term(bfc_term_base
*t
, int i
) {
1502 bfe_term
* bfet
= static_cast<bfe_term
*>(t
);
1503 int len
= t
->terms
.NumRows()-1; // already increased by one
1505 bfet
->factors
.resize(len
+1);
1506 for (int j
= len
; j
> i
; --j
) {
1507 bfet
->factors
[j
] = bfet
->factors
[j
-1];
1508 t
->terms
[j
] = t
->terms
[j
-1];
1510 bfet
->factors
[i
] = factor
;
1514 virtual void update_term(bfc_term_base
*t
, int i
) {
1515 bfe_term
* bfet
= static_cast<bfe_term
*>(t
);
1517 eadd(factor
, bfet
->factors
[i
]);
1518 free_evalue_refs(factor
);
1522 virtual bool constant_vertex(int dim
) { return E_num(0, dim
) == 0; }
1524 virtual void cum(bf_reducer
*bfr
, bfc_term_base
*t
, int k
, dpoly_r
*r
,
1525 barvinok_options
*options
);
1528 enumerator_base
*enumerator_base::create(Polyhedron
*P
, unsigned dim
, unsigned nbV
,
1529 barvinok_options
*options
)
1531 enumerator_base
*eb
;
1533 if (options
->incremental_specialization
== BV_SPECIALIZATION_BF
)
1534 eb
= new bfenumerator(P
, dim
, nbV
);
1535 else if (options
->incremental_specialization
== BV_SPECIALIZATION_DF
)
1536 eb
= new ienumerator(P
, dim
, nbV
);
1538 eb
= new enumerator(P
, dim
, nbV
);
1543 struct bfe_cum
: public cumulator
{
1545 bfc_term_base
*told
;
1549 bfe_cum(evalue
*factor
, evalue
*v
, dpoly_r
*r
, bf_reducer
*bfr
,
1550 bfc_term_base
*t
, int k
, bfenumerator
*e
) :
1551 cumulator(factor
, v
, r
), told(t
), k(k
),
1555 virtual void add_term(const vector
<int>& powers
, evalue
*f2
);
1558 void bfe_cum::add_term(const vector
<int>& powers
, evalue
*f2
)
1560 bfr
->update_powers(powers
);
1562 bfc_term_base
* t
= bfe
->find_bfc_term(bfr
->vn
, bfr
->npowers
, bfr
->nnf
);
1563 bfe
->set_factor(f2
, bfr
->l_changes
% 2);
1564 bfe
->add_term(t
, told
->terms
[k
], bfr
->l_extra_num
);
1567 void bfenumerator::cum(bf_reducer
*bfr
, bfc_term_base
*t
, int k
,
1568 dpoly_r
*r
, barvinok_options
*options
)
1570 bfe_term
* bfet
= static_cast<bfe_term
*>(t
);
1571 bfe_cum
cum(bfet
->factors
[k
], E_num(0, bfr
->d
), r
, bfr
, t
, k
, this);
1572 cum
.cumulate(options
);
1575 void bfenumerator::base(mat_ZZ
& factors
, bfc_vec
& v
)
1577 for (int i
= 0; i
< v
.size(); ++i
) {
1578 assert(v
[i
]->terms
.NumRows() == 1);
1579 evalue
*factor
= static_cast<bfe_term
*>(v
[i
])->factors
[0];
1580 eadd(factor
, vE
[vert
]);
1585 void bfenumerator::handle(const signed_cone
& sc
, barvinok_options
*options
)
1588 assert(sc
.rays
.NumRows() == enumerator_base::dim
);
1590 bfe_term
* t
= new bfe_term(enumerator_base::dim
);
1591 vector
< bfc_term_base
* > v
;
1594 t
->factors
.resize(1);
1596 t
->terms
.SetDims(1, enumerator_base::dim
);
1597 lattice_point(V
, sc
.rays
, t
->terms
[0], E_vertex
, options
);
1599 // the elements of factors are always lexpositive
1601 int s
= setup_factors(sc
.rays
, factors
, t
, sc
.sign
);
1603 t
->factors
[0] = new evalue
;
1604 value_init(t
->factors
[0]->d
);
1605 evalue_set_si(t
->factors
[0], s
, 1);
1606 reduce(factors
, v
, options
);
1608 for (int i
= 0; i
< enumerator_base::dim
; ++i
)
1610 free_evalue_refs(E_vertex
[i
]);
1615 #ifdef HAVE_CORRECT_VERTICES
1616 static inline Param_Polyhedron
*Polyhedron2Param_SD(Polyhedron
**Din
,
1617 Polyhedron
*Cin
,int WS
,Polyhedron
**CEq
,Matrix
**CT
)
1619 if (WS
& POL_NO_DUAL
)
1621 return Polyhedron2Param_SimplifiedDomain(Din
, Cin
, WS
, CEq
, CT
);
1624 static Param_Polyhedron
*Polyhedron2Param_SD(Polyhedron
**Din
,
1625 Polyhedron
*Cin
,int WS
,Polyhedron
**CEq
,Matrix
**CT
)
1627 static char data
[] = " 1 0 0 0 0 1 -18 "
1628 " 1 0 0 -20 0 19 1 "
1629 " 1 0 1 20 0 -20 16 "
1632 " 1 4 -20 0 0 -1 23 "
1633 " 1 -4 20 0 0 1 -22 "
1634 " 1 0 1 0 20 -20 16 "
1635 " 1 0 0 0 -20 19 1 ";
1636 static int checked
= 0;
1641 Matrix
*M
= Matrix_Alloc(9, 7);
1642 for (i
= 0; i
< 9; ++i
)
1643 for (int j
= 0; j
< 7; ++j
) {
1644 sscanf(p
, "%d%n", &v
, &n
);
1646 value_set_si(M
->p
[i
][j
], v
);
1648 Polyhedron
*P
= Constraints2Polyhedron(M
, 1024);
1650 Polyhedron
*U
= Universe_Polyhedron(1);
1651 Param_Polyhedron
*PP
= Polyhedron2Param_Domain(P
, U
, 1024);
1655 for (i
= 0, V
= PP
->V
; V
; ++i
, V
= V
->next
)
1658 Param_Polyhedron_Free(PP
);
1660 fprintf(stderr
, "WARNING: results may be incorrect\n");
1662 "WARNING: use latest version of PolyLib to remove this warning\n");
1666 return Polyhedron2Param_SimplifiedDomain(Din
, Cin
, WS
, CEq
, CT
);
1670 static evalue
* barvinok_enumerate_ev_f(Polyhedron
*P
, Polyhedron
* C
,
1671 barvinok_options
*options
);
1674 static evalue
* barvinok_enumerate_cst(Polyhedron
*P
, Polyhedron
* C
,
1679 ALLOC(evalue
, eres
);
1680 value_init(eres
->d
);
1681 value_set_si(eres
->d
, 0);
1682 eres
->x
.p
= new_enode(partition
, 2, C
->Dimension
);
1683 EVALUE_SET_DOMAIN(eres
->x
.p
->arr
[0], DomainConstraintSimplify(C
, MaxRays
));
1684 value_set_si(eres
->x
.p
->arr
[1].d
, 1);
1685 value_init(eres
->x
.p
->arr
[1].x
.n
);
1687 value_set_si(eres
->x
.p
->arr
[1].x
.n
, 0);
1689 barvinok_count(P
, &eres
->x
.p
->arr
[1].x
.n
, MaxRays
);
1694 evalue
* barvinok_enumerate_with_options(Polyhedron
*P
, Polyhedron
* C
,
1695 struct barvinok_options
*options
)
1697 //P = unfringe(P, MaxRays);
1698 Polyhedron
*Corig
= C
;
1699 Polyhedron
*CEq
= NULL
, *rVD
, *CA
;
1701 unsigned nparam
= C
->Dimension
;
1705 value_init(factor
.d
);
1706 evalue_set_si(&factor
, 1, 1);
1708 CA
= align_context(C
, P
->Dimension
, options
->MaxRays
);
1709 P
= DomainIntersection(P
, CA
, options
->MaxRays
);
1710 Polyhedron_Free(CA
);
1713 POL_ENSURE_FACETS(P
);
1714 POL_ENSURE_VERTICES(P
);
1715 POL_ENSURE_FACETS(C
);
1716 POL_ENSURE_VERTICES(C
);
1718 if (C
->Dimension
== 0 || emptyQ(P
)) {
1720 eres
= barvinok_enumerate_cst(P
, CEq
? CEq
: Polyhedron_Copy(C
),
1723 emul(&factor
, eres
);
1724 reduce_evalue(eres
);
1725 free_evalue_refs(&factor
);
1732 if (Polyhedron_is_infinite_param(P
, nparam
))
1737 P
= remove_equalities_p(P
, P
->Dimension
-nparam
, &f
);
1738 mask(f
, &factor
, options
);
1741 if (P
->Dimension
== nparam
) {
1743 P
= Universe_Polyhedron(0);
1747 Polyhedron
*T
= Polyhedron_Factor(P
, nparam
, options
->MaxRays
);
1748 if (T
|| (P
->Dimension
== nparam
+1)) {
1751 for (Q
= T
? T
: P
; Q
; Q
= Q
->next
) {
1752 Polyhedron
*next
= Q
->next
;
1756 if (Q
->Dimension
!= C
->Dimension
)
1757 QC
= Polyhedron_Project(Q
, nparam
);
1760 C
= DomainIntersection(C
, QC
, options
->MaxRays
);
1762 Polyhedron_Free(C2
);
1764 Polyhedron_Free(QC
);
1772 if (T
->Dimension
== C
->Dimension
) {
1779 Polyhedron
*next
= P
->next
;
1781 eres
= barvinok_enumerate_ev_f(P
, C
, options
);
1788 for (Q
= P
->next
; Q
; Q
= Q
->next
) {
1789 Polyhedron
*next
= Q
->next
;
1792 f
= barvinok_enumerate_ev_f(Q
, C
, options
);
1794 free_evalue_refs(f
);
1804 evalue
* barvinok_enumerate_ev(Polyhedron
*P
, Polyhedron
* C
, unsigned MaxRays
)
1807 barvinok_options
*options
= barvinok_options_new_with_defaults();
1808 options
->MaxRays
= MaxRays
;
1809 E
= barvinok_enumerate_with_options(P
, C
, options
);
1814 static evalue
* barvinok_enumerate_ev_f(Polyhedron
*P
, Polyhedron
* C
,
1815 barvinok_options
*options
)
1817 unsigned nparam
= C
->Dimension
;
1819 if (P
->Dimension
- nparam
== 1)
1820 return ParamLine_Length(P
, C
, options
);
1822 Param_Polyhedron
*PP
= NULL
;
1823 Polyhedron
*CEq
= NULL
, *pVD
;
1825 Param_Domain
*D
, *next
;
1828 Polyhedron
*Porig
= P
;
1830 PP
= Polyhedron2Param_SD(&P
,C
,options
->MaxRays
,&CEq
,&CT
);
1832 if (isIdentity(CT
)) {
1836 assert(CT
->NbRows
!= CT
->NbColumns
);
1837 if (CT
->NbRows
== 1) { // no more parameters
1838 eres
= barvinok_enumerate_cst(P
, CEq
, options
->MaxRays
);
1843 Param_Polyhedron_Free(PP
);
1849 nparam
= CT
->NbRows
- 1;
1852 unsigned dim
= P
->Dimension
- nparam
;
1854 ALLOC(evalue
, eres
);
1855 value_init(eres
->d
);
1856 value_set_si(eres
->d
, 0);
1859 for (nd
= 0, D
=PP
->D
; D
; ++nd
, D
=D
->next
);
1860 struct section
{ Polyhedron
*D
; evalue E
; };
1861 section
*s
= new section
[nd
];
1862 Polyhedron
**fVD
= new Polyhedron_p
[nd
];
1864 enumerator_base
*et
= NULL
;
1869 et
= enumerator_base::create(P
, dim
, PP
->nbV
, options
);
1871 for(nd
= 0, D
=PP
->D
; D
; D
=next
) {
1874 Polyhedron
*rVD
= reduce_domain(D
->Domain
, CT
, CEq
,
1875 fVD
, nd
, options
->MaxRays
);
1879 pVD
= CT
? DomainImage(rVD
,CT
,options
->MaxRays
) : rVD
;
1881 value_init(s
[nd
].E
.d
);
1882 evalue_set_si(&s
[nd
].E
, 0, 1);
1885 FORALL_PVertex_in_ParamPolyhedron(V
,D
,PP
) // _i is internal counter
1888 et
->decompose_at(V
, _i
, options
);
1889 } catch (OrthogonalException
&e
) {
1892 for (; nd
>= 0; --nd
) {
1893 free_evalue_refs(&s
[nd
].E
);
1894 Domain_Free(s
[nd
].D
);
1895 Domain_Free(fVD
[nd
]);
1899 eadd(et
->vE
[_i
] , &s
[nd
].E
);
1900 END_FORALL_PVertex_in_ParamPolyhedron
;
1901 evalue_range_reduction_in_domain(&s
[nd
].E
, pVD
);
1904 addeliminatedparams_evalue(&s
[nd
].E
, CT
);
1912 evalue_set_si(eres
, 0, 1);
1914 eres
->x
.p
= new_enode(partition
, 2*nd
, C
->Dimension
);
1915 for (int j
= 0; j
< nd
; ++j
) {
1916 EVALUE_SET_DOMAIN(eres
->x
.p
->arr
[2*j
], s
[j
].D
);
1917 value_clear(eres
->x
.p
->arr
[2*j
+1].d
);
1918 eres
->x
.p
->arr
[2*j
+1] = s
[j
].E
;
1919 Domain_Free(fVD
[j
]);
1926 Polyhedron_Free(CEq
);
1930 Enumeration
* barvinok_enumerate(Polyhedron
*P
, Polyhedron
* C
, unsigned MaxRays
)
1932 evalue
*EP
= barvinok_enumerate_ev(P
, C
, MaxRays
);
1934 return partition2enumeration(EP
);
1937 static void SwapColumns(Value
**V
, int n
, int i
, int j
)
1939 for (int r
= 0; r
< n
; ++r
)
1940 value_swap(V
[r
][i
], V
[r
][j
]);
1943 static void SwapColumns(Polyhedron
*P
, int i
, int j
)
1945 SwapColumns(P
->Constraint
, P
->NbConstraints
, i
, j
);
1946 SwapColumns(P
->Ray
, P
->NbRays
, i
, j
);
1949 /* Construct a constraint c from constraints l and u such that if
1950 * if constraint c holds then for each value of the other variables
1951 * there is at most one value of variable pos (position pos+1 in the constraints).
1953 * Given a lower and an upper bound
1954 * n_l v_i + <c_l,x> + c_l >= 0
1955 * -n_u v_i + <c_u,x> + c_u >= 0
1956 * the constructed constraint is
1958 * -(n_l<c_u,x> + n_u<c_l,x>) + (-n_l c_u - n_u c_l + n_l n_u - 1)
1960 * which is then simplified to remove the content of the non-constant coefficients
1962 * len is the total length of the constraints.
1963 * v is a temporary variable that can be used by this procedure
1965 static void negative_test_constraint(Value
*l
, Value
*u
, Value
*c
, int pos
,
1968 value_oppose(*v
, u
[pos
+1]);
1969 Vector_Combine(l
+1, u
+1, c
+1, *v
, l
[pos
+1], len
-1);
1970 value_multiply(*v
, *v
, l
[pos
+1]);
1971 value_subtract(c
[len
-1], c
[len
-1], *v
);
1972 value_set_si(*v
, -1);
1973 Vector_Scale(c
+1, c
+1, *v
, len
-1);
1974 value_decrement(c
[len
-1], c
[len
-1]);
1975 ConstraintSimplify(c
, c
, len
, v
);
1978 static bool parallel_constraints(Value
*l
, Value
*u
, Value
*c
, int pos
,
1987 Vector_Gcd(&l
[1+pos
], len
, &g1
);
1988 Vector_Gcd(&u
[1+pos
], len
, &g2
);
1989 Vector_Combine(l
+1+pos
, u
+1+pos
, c
+1, g2
, g1
, len
);
1990 parallel
= First_Non_Zero(c
+1, len
) == -1;
1998 static void negative_test_constraint7(Value
*l
, Value
*u
, Value
*c
, int pos
,
1999 int exist
, int len
, Value
*v
)
2004 Vector_Gcd(&u
[1+pos
], exist
, v
);
2005 Vector_Gcd(&l
[1+pos
], exist
, &g
);
2006 Vector_Combine(l
+1, u
+1, c
+1, *v
, g
, len
-1);
2007 value_multiply(*v
, *v
, g
);
2008 value_subtract(c
[len
-1], c
[len
-1], *v
);
2009 value_set_si(*v
, -1);
2010 Vector_Scale(c
+1, c
+1, *v
, len
-1);
2011 value_decrement(c
[len
-1], c
[len
-1]);
2012 ConstraintSimplify(c
, c
, len
, v
);
2017 /* Turns a x + b >= 0 into a x + b <= -1
2019 * len is the total length of the constraint.
2020 * v is a temporary variable that can be used by this procedure
2022 static void oppose_constraint(Value
*c
, int len
, Value
*v
)
2024 value_set_si(*v
, -1);
2025 Vector_Scale(c
+1, c
+1, *v
, len
-1);
2026 value_decrement(c
[len
-1], c
[len
-1]);
2029 /* Split polyhedron P into two polyhedra *pos and *neg, where
2030 * existential variable i has at most one solution for each
2031 * value of the other variables in *neg.
2033 * The splitting is performed using constraints l and u.
2035 * nvar: number of set variables
2036 * row: temporary vector that can be used by this procedure
2037 * f: temporary value that can be used by this procedure
2039 static bool SplitOnConstraint(Polyhedron
*P
, int i
, int l
, int u
,
2040 int nvar
, int MaxRays
, Vector
*row
, Value
& f
,
2041 Polyhedron
**pos
, Polyhedron
**neg
)
2043 negative_test_constraint(P
->Constraint
[l
], P
->Constraint
[u
],
2044 row
->p
, nvar
+i
, P
->Dimension
+2, &f
);
2045 *neg
= AddConstraints(row
->p
, 1, P
, MaxRays
);
2047 /* We found an independent, but useless constraint
2048 * Maybe we should detect this earlier and not
2049 * mark the variable as INDEPENDENT
2051 if (emptyQ((*neg
))) {
2052 Polyhedron_Free(*neg
);
2056 oppose_constraint(row
->p
, P
->Dimension
+2, &f
);
2057 *pos
= AddConstraints(row
->p
, 1, P
, MaxRays
);
2059 if (emptyQ((*pos
))) {
2060 Polyhedron_Free(*neg
);
2061 Polyhedron_Free(*pos
);
2069 * unimodularly transform P such that constraint r is transformed
2070 * into a constraint that involves only a single (the first)
2071 * existential variable
2074 static Polyhedron
*rotate_along(Polyhedron
*P
, int r
, int nvar
, int exist
,
2080 Vector
*row
= Vector_Alloc(exist
);
2081 Vector_Copy(P
->Constraint
[r
]+1+nvar
, row
->p
, exist
);
2082 Vector_Gcd(row
->p
, exist
, &g
);
2083 if (value_notone_p(g
))
2084 Vector_AntiScale(row
->p
, row
->p
, g
, exist
);
2087 Matrix
*M
= unimodular_complete(row
);
2088 Matrix
*M2
= Matrix_Alloc(P
->Dimension
+1, P
->Dimension
+1);
2089 for (r
= 0; r
< nvar
; ++r
)
2090 value_set_si(M2
->p
[r
][r
], 1);
2091 for ( ; r
< nvar
+exist
; ++r
)
2092 Vector_Copy(M
->p
[r
-nvar
], M2
->p
[r
]+nvar
, exist
);
2093 for ( ; r
< P
->Dimension
+1; ++r
)
2094 value_set_si(M2
->p
[r
][r
], 1);
2095 Polyhedron
*T
= Polyhedron_Image(P
, M2
, MaxRays
);
2104 /* Split polyhedron P into two polyhedra *pos and *neg, where
2105 * existential variable i has at most one solution for each
2106 * value of the other variables in *neg.
2108 * If independent is set, then the two constraints on which the
2109 * split will be performed need to be independent of the other
2110 * existential variables.
2112 * Return true if an appropriate split could be performed.
2114 * nvar: number of set variables
2115 * exist: number of existential variables
2116 * row: temporary vector that can be used by this procedure
2117 * f: temporary value that can be used by this procedure
2119 static bool SplitOnVar(Polyhedron
*P
, int i
,
2120 int nvar
, int exist
, int MaxRays
,
2121 Vector
*row
, Value
& f
, bool independent
,
2122 Polyhedron
**pos
, Polyhedron
**neg
)
2126 for (int l
= P
->NbEq
; l
< P
->NbConstraints
; ++l
) {
2127 if (value_negz_p(P
->Constraint
[l
][nvar
+i
+1]))
2131 for (j
= 0; j
< exist
; ++j
)
2132 if (j
!= i
&& value_notzero_p(P
->Constraint
[l
][nvar
+j
+1]))
2138 for (int u
= P
->NbEq
; u
< P
->NbConstraints
; ++u
) {
2139 if (value_posz_p(P
->Constraint
[u
][nvar
+i
+1]))
2143 for (j
= 0; j
< exist
; ++j
)
2144 if (j
!= i
&& value_notzero_p(P
->Constraint
[u
][nvar
+j
+1]))
2150 if (SplitOnConstraint(P
, i
, l
, u
, nvar
, MaxRays
, row
, f
, pos
, neg
)) {
2153 SwapColumns(*neg
, nvar
+1, nvar
+1+i
);
2163 static bool double_bound_pair(Polyhedron
*P
, int nvar
, int exist
,
2164 int i
, int l1
, int l2
,
2165 Polyhedron
**pos
, Polyhedron
**neg
)
2169 Vector
*row
= Vector_Alloc(P
->Dimension
+2);
2170 value_set_si(row
->p
[0], 1);
2171 value_oppose(f
, P
->Constraint
[l1
][nvar
+i
+1]);
2172 Vector_Combine(P
->Constraint
[l1
]+1, P
->Constraint
[l2
]+1,
2174 P
->Constraint
[l2
][nvar
+i
+1], f
,
2176 ConstraintSimplify(row
->p
, row
->p
, P
->Dimension
+2, &f
);
2177 *pos
= AddConstraints(row
->p
, 1, P
, 0);
2178 value_set_si(f
, -1);
2179 Vector_Scale(row
->p
+1, row
->p
+1, f
, P
->Dimension
+1);
2180 value_decrement(row
->p
[P
->Dimension
+1], row
->p
[P
->Dimension
+1]);
2181 *neg
= AddConstraints(row
->p
, 1, P
, 0);
2185 return !emptyQ((*pos
)) && !emptyQ((*neg
));
2188 static bool double_bound(Polyhedron
*P
, int nvar
, int exist
,
2189 Polyhedron
**pos
, Polyhedron
**neg
)
2191 for (int i
= 0; i
< exist
; ++i
) {
2193 for (l1
= P
->NbEq
; l1
< P
->NbConstraints
; ++l1
) {
2194 if (value_negz_p(P
->Constraint
[l1
][nvar
+i
+1]))
2196 for (l2
= l1
+ 1; l2
< P
->NbConstraints
; ++l2
) {
2197 if (value_negz_p(P
->Constraint
[l2
][nvar
+i
+1]))
2199 if (double_bound_pair(P
, nvar
, exist
, i
, l1
, l2
, pos
, neg
))
2203 for (l1
= P
->NbEq
; l1
< P
->NbConstraints
; ++l1
) {
2204 if (value_posz_p(P
->Constraint
[l1
][nvar
+i
+1]))
2206 if (l1
< P
->NbConstraints
)
2207 for (l2
= l1
+ 1; l2
< P
->NbConstraints
; ++l2
) {
2208 if (value_posz_p(P
->Constraint
[l2
][nvar
+i
+1]))
2210 if (double_bound_pair(P
, nvar
, exist
, i
, l1
, l2
, pos
, neg
))
2222 INDEPENDENT
= 1 << 2,
2226 static evalue
* enumerate_or(Polyhedron
*D
,
2227 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2230 fprintf(stderr
, "\nER: Or\n");
2231 #endif /* DEBUG_ER */
2233 Polyhedron
*N
= D
->next
;
2236 barvinok_enumerate_e_with_options(D
, exist
, nparam
, options
);
2239 for (D
= N
; D
; D
= N
) {
2244 barvinok_enumerate_e_with_options(D
, exist
, nparam
, options
);
2247 free_evalue_refs(EN
);
2257 static evalue
* enumerate_sum(Polyhedron
*P
,
2258 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2260 int nvar
= P
->Dimension
- exist
- nparam
;
2261 int toswap
= nvar
< exist
? nvar
: exist
;
2262 for (int i
= 0; i
< toswap
; ++i
)
2263 SwapColumns(P
, 1 + i
, nvar
+exist
- i
);
2267 fprintf(stderr
, "\nER: Sum\n");
2268 #endif /* DEBUG_ER */
2270 evalue
*EP
= barvinok_enumerate_e_with_options(P
, exist
, nparam
, options
);
2272 for (int i
= 0; i
< /* nvar */ nparam
; ++i
) {
2273 Matrix
*C
= Matrix_Alloc(1, 1 + nparam
+ 1);
2274 value_set_si(C
->p
[0][0], 1);
2276 value_init(split
.d
);
2277 value_set_si(split
.d
, 0);
2278 split
.x
.p
= new_enode(partition
, 4, nparam
);
2279 value_set_si(C
->p
[0][1+i
], 1);
2280 Matrix
*C2
= Matrix_Copy(C
);
2281 EVALUE_SET_DOMAIN(split
.x
.p
->arr
[0],
2282 Constraints2Polyhedron(C2
, options
->MaxRays
));
2284 evalue_set_si(&split
.x
.p
->arr
[1], 1, 1);
2285 value_set_si(C
->p
[0][1+i
], -1);
2286 value_set_si(C
->p
[0][1+nparam
], -1);
2287 EVALUE_SET_DOMAIN(split
.x
.p
->arr
[2],
2288 Constraints2Polyhedron(C
, options
->MaxRays
));
2289 evalue_set_si(&split
.x
.p
->arr
[3], 1, 1);
2291 free_evalue_refs(&split
);
2295 evalue_range_reduction(EP
);
2297 evalue_frac2floor2(EP
, 1);
2299 evalue
*sum
= esum(EP
, nvar
);
2301 free_evalue_refs(EP
);
2305 evalue_range_reduction(EP
);
2310 static evalue
* split_sure(Polyhedron
*P
, Polyhedron
*S
,
2311 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2313 int nvar
= P
->Dimension
- exist
- nparam
;
2315 Matrix
*M
= Matrix_Alloc(exist
, S
->Dimension
+2);
2316 for (int i
= 0; i
< exist
; ++i
)
2317 value_set_si(M
->p
[i
][nvar
+i
+1], 1);
2319 S
= DomainAddRays(S
, M
, options
->MaxRays
);
2321 Polyhedron
*F
= DomainAddRays(P
, M
, options
->MaxRays
);
2322 Polyhedron
*D
= DomainDifference(F
, S
, options
->MaxRays
);
2324 D
= Disjoint_Domain(D
, 0, options
->MaxRays
);
2329 M
= Matrix_Alloc(P
->Dimension
+1-exist
, P
->Dimension
+1);
2330 for (int j
= 0; j
< nvar
; ++j
)
2331 value_set_si(M
->p
[j
][j
], 1);
2332 for (int j
= 0; j
< nparam
+1; ++j
)
2333 value_set_si(M
->p
[nvar
+j
][nvar
+exist
+j
], 1);
2334 Polyhedron
*T
= Polyhedron_Image(S
, M
, options
->MaxRays
);
2335 evalue
*EP
= barvinok_enumerate_e_with_options(T
, 0, nparam
, options
);
2340 for (Polyhedron
*Q
= D
; Q
; Q
= Q
->next
) {
2341 Polyhedron
*N
= Q
->next
;
2343 T
= DomainIntersection(P
, Q
, options
->MaxRays
);
2344 evalue
*E
= barvinok_enumerate_e_with_options(T
, exist
, nparam
, options
);
2346 free_evalue_refs(E
);
2355 static evalue
* enumerate_sure(Polyhedron
*P
,
2356 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2360 int nvar
= P
->Dimension
- exist
- nparam
;
2366 for (i
= 0; i
< exist
; ++i
) {
2367 Matrix
*M
= Matrix_Alloc(S
->NbConstraints
, S
->Dimension
+2);
2369 value_set_si(lcm
, 1);
2370 for (int j
= 0; j
< S
->NbConstraints
; ++j
) {
2371 if (value_negz_p(S
->Constraint
[j
][1+nvar
+i
]))
2373 if (value_one_p(S
->Constraint
[j
][1+nvar
+i
]))
2375 value_lcm(lcm
, S
->Constraint
[j
][1+nvar
+i
], &lcm
);
2378 for (int j
= 0; j
< S
->NbConstraints
; ++j
) {
2379 if (value_negz_p(S
->Constraint
[j
][1+nvar
+i
]))
2381 if (value_one_p(S
->Constraint
[j
][1+nvar
+i
]))
2383 value_division(f
, lcm
, S
->Constraint
[j
][1+nvar
+i
]);
2384 Vector_Scale(S
->Constraint
[j
], M
->p
[c
], f
, S
->Dimension
+2);
2385 value_subtract(M
->p
[c
][S
->Dimension
+1],
2386 M
->p
[c
][S
->Dimension
+1],
2388 value_increment(M
->p
[c
][S
->Dimension
+1],
2389 M
->p
[c
][S
->Dimension
+1]);
2393 S
= AddConstraints(M
->p
[0], c
, S
, options
->MaxRays
);
2408 fprintf(stderr
, "\nER: Sure\n");
2409 #endif /* DEBUG_ER */
2411 return split_sure(P
, S
, exist
, nparam
, options
);
2414 static evalue
* enumerate_sure2(Polyhedron
*P
,
2415 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2417 int nvar
= P
->Dimension
- exist
- nparam
;
2419 for (r
= 0; r
< P
->NbRays
; ++r
)
2420 if (value_one_p(P
->Ray
[r
][0]) &&
2421 value_one_p(P
->Ray
[r
][P
->Dimension
+1]))
2427 Matrix
*M
= Matrix_Alloc(nvar
+ 1 + nparam
, P
->Dimension
+2);
2428 for (int i
= 0; i
< nvar
; ++i
)
2429 value_set_si(M
->p
[i
][1+i
], 1);
2430 for (int i
= 0; i
< nparam
; ++i
)
2431 value_set_si(M
->p
[i
+nvar
][1+nvar
+exist
+i
], 1);
2432 Vector_Copy(P
->Ray
[r
]+1+nvar
, M
->p
[nvar
+nparam
]+1+nvar
, exist
);
2433 value_set_si(M
->p
[nvar
+nparam
][0], 1);
2434 value_set_si(M
->p
[nvar
+nparam
][P
->Dimension
+1], 1);
2435 Polyhedron
* F
= Rays2Polyhedron(M
, options
->MaxRays
);
2438 Polyhedron
*I
= DomainIntersection(F
, P
, options
->MaxRays
);
2442 fprintf(stderr
, "\nER: Sure2\n");
2443 #endif /* DEBUG_ER */
2445 return split_sure(P
, I
, exist
, nparam
, options
);
2448 static evalue
* enumerate_cyclic(Polyhedron
*P
,
2449 unsigned exist
, unsigned nparam
,
2450 evalue
* EP
, int r
, int p
, unsigned MaxRays
)
2452 int nvar
= P
->Dimension
- exist
- nparam
;
2454 /* If EP in its fractional maps only contains references
2455 * to the remainder parameter with appropriate coefficients
2456 * then we could in principle avoid adding existentially
2457 * quantified variables to the validity domains.
2458 * We'd have to replace the remainder by m { p/m }
2459 * and multiply with an appropriate factor that is one
2460 * only in the appropriate range.
2461 * This last multiplication can be avoided if EP
2462 * has a single validity domain with no (further)
2463 * constraints on the remainder parameter
2466 Matrix
*CT
= Matrix_Alloc(nparam
+1, nparam
+3);
2467 Matrix
*M
= Matrix_Alloc(1, 1+nparam
+3);
2468 for (int j
= 0; j
< nparam
; ++j
)
2470 value_set_si(CT
->p
[j
][j
], 1);
2471 value_set_si(CT
->p
[p
][nparam
+1], 1);
2472 value_set_si(CT
->p
[nparam
][nparam
+2], 1);
2473 value_set_si(M
->p
[0][1+p
], -1);
2474 value_absolute(M
->p
[0][1+nparam
], P
->Ray
[0][1+nvar
+exist
+p
]);
2475 value_set_si(M
->p
[0][1+nparam
+1], 1);
2476 Polyhedron
*CEq
= Constraints2Polyhedron(M
, 1);
2478 addeliminatedparams_enum(EP
, CT
, CEq
, MaxRays
, nparam
);
2479 Polyhedron_Free(CEq
);
2485 static void enumerate_vd_add_ray(evalue
*EP
, Matrix
*Rays
, unsigned MaxRays
)
2487 if (value_notzero_p(EP
->d
))
2490 assert(EP
->x
.p
->type
== partition
);
2491 assert(EP
->x
.p
->pos
== EVALUE_DOMAIN(EP
->x
.p
->arr
[0])->Dimension
);
2492 for (int i
= 0; i
< EP
->x
.p
->size
/2; ++i
) {
2493 Polyhedron
*D
= EVALUE_DOMAIN(EP
->x
.p
->arr
[2*i
]);
2494 Polyhedron
*N
= DomainAddRays(D
, Rays
, MaxRays
);
2495 EVALUE_SET_DOMAIN(EP
->x
.p
->arr
[2*i
], N
);
2500 static evalue
* enumerate_line(Polyhedron
*P
,
2501 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2507 fprintf(stderr
, "\nER: Line\n");
2508 #endif /* DEBUG_ER */
2510 int nvar
= P
->Dimension
- exist
- nparam
;
2512 for (i
= 0; i
< nparam
; ++i
)
2513 if (value_notzero_p(P
->Ray
[0][1+nvar
+exist
+i
]))
2516 for (j
= i
+1; j
< nparam
; ++j
)
2517 if (value_notzero_p(P
->Ray
[0][1+nvar
+exist
+i
]))
2519 assert(j
>= nparam
); // for now
2521 Matrix
*M
= Matrix_Alloc(2, P
->Dimension
+2);
2522 value_set_si(M
->p
[0][0], 1);
2523 value_set_si(M
->p
[0][1+nvar
+exist
+i
], 1);
2524 value_set_si(M
->p
[1][0], 1);
2525 value_set_si(M
->p
[1][1+nvar
+exist
+i
], -1);
2526 value_absolute(M
->p
[1][1+P
->Dimension
], P
->Ray
[0][1+nvar
+exist
+i
]);
2527 value_decrement(M
->p
[1][1+P
->Dimension
], M
->p
[1][1+P
->Dimension
]);
2528 Polyhedron
*S
= AddConstraints(M
->p
[0], 2, P
, options
->MaxRays
);
2529 evalue
*EP
= barvinok_enumerate_e_with_options(S
, exist
, nparam
, options
);
2533 return enumerate_cyclic(P
, exist
, nparam
, EP
, 0, i
, options
->MaxRays
);
2536 static int single_param_pos(Polyhedron
*P
, unsigned exist
, unsigned nparam
,
2539 int nvar
= P
->Dimension
- exist
- nparam
;
2540 if (First_Non_Zero(P
->Ray
[r
]+1, nvar
) != -1)
2542 int i
= First_Non_Zero(P
->Ray
[r
]+1+nvar
+exist
, nparam
);
2545 if (First_Non_Zero(P
->Ray
[r
]+1+nvar
+exist
+1, nparam
-i
-1) != -1)
2550 static evalue
* enumerate_remove_ray(Polyhedron
*P
, int r
,
2551 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2554 fprintf(stderr
, "\nER: RedundantRay\n");
2555 #endif /* DEBUG_ER */
2559 value_set_si(one
, 1);
2560 int len
= P
->NbRays
-1;
2561 Matrix
*M
= Matrix_Alloc(2 * len
, P
->Dimension
+2);
2562 Vector_Copy(P
->Ray
[0], M
->p
[0], r
* (P
->Dimension
+2));
2563 Vector_Copy(P
->Ray
[r
+1], M
->p
[r
], (len
-r
) * (P
->Dimension
+2));
2564 for (int j
= 0; j
< P
->NbRays
; ++j
) {
2567 Vector_Combine(P
->Ray
[j
], P
->Ray
[r
], M
->p
[len
+j
-(j
>r
)],
2568 one
, P
->Ray
[j
][P
->Dimension
+1], P
->Dimension
+2);
2571 P
= Rays2Polyhedron(M
, options
->MaxRays
);
2573 evalue
*EP
= barvinok_enumerate_e_with_options(P
, exist
, nparam
, options
);
2580 static evalue
* enumerate_redundant_ray(Polyhedron
*P
,
2581 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2583 assert(P
->NbBid
== 0);
2584 int nvar
= P
->Dimension
- exist
- nparam
;
2588 for (int r
= 0; r
< P
->NbRays
; ++r
) {
2589 if (value_notzero_p(P
->Ray
[r
][P
->Dimension
+1]))
2591 int i1
= single_param_pos(P
, exist
, nparam
, r
);
2594 for (int r2
= r
+1; r2
< P
->NbRays
; ++r2
) {
2595 if (value_notzero_p(P
->Ray
[r2
][P
->Dimension
+1]))
2597 int i2
= single_param_pos(P
, exist
, nparam
, r2
);
2603 value_division(m
, P
->Ray
[r
][1+nvar
+exist
+i1
],
2604 P
->Ray
[r2
][1+nvar
+exist
+i1
]);
2605 value_multiply(m
, m
, P
->Ray
[r2
][1+nvar
+exist
+i1
]);
2606 /* r2 divides r => r redundant */
2607 if (value_eq(m
, P
->Ray
[r
][1+nvar
+exist
+i1
])) {
2609 return enumerate_remove_ray(P
, r
, exist
, nparam
, options
);
2612 value_division(m
, P
->Ray
[r2
][1+nvar
+exist
+i1
],
2613 P
->Ray
[r
][1+nvar
+exist
+i1
]);
2614 value_multiply(m
, m
, P
->Ray
[r
][1+nvar
+exist
+i1
]);
2615 /* r divides r2 => r2 redundant */
2616 if (value_eq(m
, P
->Ray
[r2
][1+nvar
+exist
+i1
])) {
2618 return enumerate_remove_ray(P
, r2
, exist
, nparam
, options
);
2626 static Polyhedron
*upper_bound(Polyhedron
*P
,
2627 int pos
, Value
*max
, Polyhedron
**R
)
2636 for (Polyhedron
*Q
= P
; Q
; Q
= N
) {
2638 for (r
= 0; r
< P
->NbRays
; ++r
) {
2639 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1]) &&
2640 value_pos_p(P
->Ray
[r
][1+pos
]))
2643 if (r
< P
->NbRays
) {
2651 for (r
= 0; r
< P
->NbRays
; ++r
) {
2652 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1]))
2654 mpz_fdiv_q(v
, P
->Ray
[r
][1+pos
], P
->Ray
[r
][1+P
->Dimension
]);
2655 if ((!Q
->next
&& r
== 0) || value_gt(v
, *max
))
2656 value_assign(*max
, v
);
2663 static evalue
* enumerate_ray(Polyhedron
*P
,
2664 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2666 assert(P
->NbBid
== 0);
2667 int nvar
= P
->Dimension
- exist
- nparam
;
2670 for (r
= 0; r
< P
->NbRays
; ++r
)
2671 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1]))
2677 for (r2
= r
+1; r2
< P
->NbRays
; ++r2
)
2678 if (value_zero_p(P
->Ray
[r2
][P
->Dimension
+1]))
2680 if (r2
< P
->NbRays
) {
2682 return enumerate_sum(P
, exist
, nparam
, options
);
2686 fprintf(stderr
, "\nER: Ray\n");
2687 #endif /* DEBUG_ER */
2693 value_set_si(one
, 1);
2694 int i
= single_param_pos(P
, exist
, nparam
, r
);
2695 assert(i
!= -1); // for now;
2697 Matrix
*M
= Matrix_Alloc(P
->NbRays
, P
->Dimension
+2);
2698 for (int j
= 0; j
< P
->NbRays
; ++j
) {
2699 Vector_Combine(P
->Ray
[j
], P
->Ray
[r
], M
->p
[j
],
2700 one
, P
->Ray
[j
][P
->Dimension
+1], P
->Dimension
+2);
2702 Polyhedron
*S
= Rays2Polyhedron(M
, options
->MaxRays
);
2704 Polyhedron
*D
= DomainDifference(P
, S
, options
->MaxRays
);
2706 // Polyhedron_Print(stderr, P_VALUE_FMT, D);
2707 assert(value_pos_p(P
->Ray
[r
][1+nvar
+exist
+i
])); // for now
2709 D
= upper_bound(D
, nvar
+exist
+i
, &m
, &R
);
2713 M
= Matrix_Alloc(2, P
->Dimension
+2);
2714 value_set_si(M
->p
[0][0], 1);
2715 value_set_si(M
->p
[1][0], 1);
2716 value_set_si(M
->p
[0][1+nvar
+exist
+i
], -1);
2717 value_set_si(M
->p
[1][1+nvar
+exist
+i
], 1);
2718 value_assign(M
->p
[0][1+P
->Dimension
], m
);
2719 value_oppose(M
->p
[1][1+P
->Dimension
], m
);
2720 value_addto(M
->p
[1][1+P
->Dimension
], M
->p
[1][1+P
->Dimension
],
2721 P
->Ray
[r
][1+nvar
+exist
+i
]);
2722 value_decrement(M
->p
[1][1+P
->Dimension
], M
->p
[1][1+P
->Dimension
]);
2723 // Matrix_Print(stderr, P_VALUE_FMT, M);
2724 D
= AddConstraints(M
->p
[0], 2, P
, options
->MaxRays
);
2725 // Polyhedron_Print(stderr, P_VALUE_FMT, D);
2726 value_subtract(M
->p
[0][1+P
->Dimension
], M
->p
[0][1+P
->Dimension
],
2727 P
->Ray
[r
][1+nvar
+exist
+i
]);
2728 // Matrix_Print(stderr, P_VALUE_FMT, M);
2729 S
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
2730 // Polyhedron_Print(stderr, P_VALUE_FMT, S);
2733 evalue
*EP
= barvinok_enumerate_e_with_options(D
, exist
, nparam
, options
);
2738 if (value_notone_p(P
->Ray
[r
][1+nvar
+exist
+i
]))
2739 EP
= enumerate_cyclic(P
, exist
, nparam
, EP
, r
, i
, options
->MaxRays
);
2741 M
= Matrix_Alloc(1, nparam
+2);
2742 value_set_si(M
->p
[0][0], 1);
2743 value_set_si(M
->p
[0][1+i
], 1);
2744 enumerate_vd_add_ray(EP
, M
, options
->MaxRays
);
2749 evalue
*E
= barvinok_enumerate_e_with_options(S
, exist
, nparam
, options
);
2751 free_evalue_refs(E
);
2758 evalue
*ER
= enumerate_or(R
, exist
, nparam
, options
);
2760 free_evalue_refs(ER
);
2767 static evalue
* enumerate_vd(Polyhedron
**PA
,
2768 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2770 Polyhedron
*P
= *PA
;
2771 int nvar
= P
->Dimension
- exist
- nparam
;
2772 Param_Polyhedron
*PP
= NULL
;
2773 Polyhedron
*C
= Universe_Polyhedron(nparam
);
2777 PP
= Polyhedron2Param_SimplifiedDomain(&PR
,C
, options
->MaxRays
,&CEq
,&CT
);
2781 Param_Domain
*D
, *last
;
2784 for (nd
= 0, D
=PP
->D
; D
; D
=D
->next
, ++nd
)
2787 Polyhedron
**VD
= new Polyhedron_p
[nd
];
2788 Polyhedron
**fVD
= new Polyhedron_p
[nd
];
2789 for(nd
= 0, D
=PP
->D
; D
; D
=D
->next
) {
2790 Polyhedron
*rVD
= reduce_domain(D
->Domain
, CT
, CEq
,
2791 fVD
, nd
, options
->MaxRays
);
2804 /* This doesn't seem to have any effect */
2806 Polyhedron
*CA
= align_context(VD
[0], P
->Dimension
, options
->MaxRays
);
2808 P
= DomainIntersection(P
, CA
, options
->MaxRays
);
2811 Polyhedron_Free(CA
);
2816 if (!EP
&& CT
->NbColumns
!= CT
->NbRows
) {
2817 Polyhedron
*CEqr
= DomainImage(CEq
, CT
, options
->MaxRays
);
2818 Polyhedron
*CA
= align_context(CEqr
, PR
->Dimension
, options
->MaxRays
);
2819 Polyhedron
*I
= DomainIntersection(PR
, CA
, options
->MaxRays
);
2820 Polyhedron_Free(CEqr
);
2821 Polyhedron_Free(CA
);
2823 fprintf(stderr
, "\nER: Eliminate\n");
2824 #endif /* DEBUG_ER */
2825 nparam
-= CT
->NbColumns
- CT
->NbRows
;
2826 EP
= barvinok_enumerate_e_with_options(I
, exist
, nparam
, options
);
2827 nparam
+= CT
->NbColumns
- CT
->NbRows
;
2828 addeliminatedparams_enum(EP
, CT
, CEq
, options
->MaxRays
, nparam
);
2832 Polyhedron_Free(PR
);
2835 if (!EP
&& nd
> 1) {
2837 fprintf(stderr
, "\nER: VD\n");
2838 #endif /* DEBUG_ER */
2839 for (int i
= 0; i
< nd
; ++i
) {
2840 Polyhedron
*CA
= align_context(VD
[i
], P
->Dimension
, options
->MaxRays
);
2841 Polyhedron
*I
= DomainIntersection(P
, CA
, options
->MaxRays
);
2844 EP
= barvinok_enumerate_e_with_options(I
, exist
, nparam
, options
);
2846 evalue
*E
= barvinok_enumerate_e_with_options(I
, exist
, nparam
,
2849 free_evalue_refs(E
);
2853 Polyhedron_Free(CA
);
2857 for (int i
= 0; i
< nd
; ++i
) {
2858 Polyhedron_Free(VD
[i
]);
2859 Polyhedron_Free(fVD
[i
]);
2865 if (!EP
&& nvar
== 0) {
2868 Param_Vertices
*V
, *V2
;
2869 Matrix
* M
= Matrix_Alloc(1, P
->Dimension
+2);
2871 FORALL_PVertex_in_ParamPolyhedron(V
, last
, PP
) {
2873 FORALL_PVertex_in_ParamPolyhedron(V2
, last
, PP
) {
2880 for (int i
= 0; i
< exist
; ++i
) {
2881 value_oppose(f
, V
->Vertex
->p
[i
][nparam
+1]);
2882 Vector_Combine(V
->Vertex
->p
[i
],
2884 M
->p
[0] + 1 + nvar
+ exist
,
2885 V2
->Vertex
->p
[i
][nparam
+1],
2889 for (j
= 0; j
< nparam
; ++j
)
2890 if (value_notzero_p(M
->p
[0][1+nvar
+exist
+j
]))
2894 ConstraintSimplify(M
->p
[0], M
->p
[0],
2895 P
->Dimension
+2, &f
);
2896 value_set_si(M
->p
[0][0], 0);
2897 Polyhedron
*para
= AddConstraints(M
->p
[0], 1, P
,
2900 Polyhedron_Free(para
);
2903 Polyhedron
*pos
, *neg
;
2904 value_set_si(M
->p
[0][0], 1);
2905 value_decrement(M
->p
[0][P
->Dimension
+1],
2906 M
->p
[0][P
->Dimension
+1]);
2907 neg
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
2908 value_set_si(f
, -1);
2909 Vector_Scale(M
->p
[0]+1, M
->p
[0]+1, f
,
2911 value_decrement(M
->p
[0][P
->Dimension
+1],
2912 M
->p
[0][P
->Dimension
+1]);
2913 value_decrement(M
->p
[0][P
->Dimension
+1],
2914 M
->p
[0][P
->Dimension
+1]);
2915 pos
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
2916 if (emptyQ(neg
) && emptyQ(pos
)) {
2917 Polyhedron_Free(para
);
2918 Polyhedron_Free(pos
);
2919 Polyhedron_Free(neg
);
2923 fprintf(stderr
, "\nER: Order\n");
2924 #endif /* DEBUG_ER */
2925 EP
= barvinok_enumerate_e_with_options(para
, exist
, nparam
,
2929 E
= barvinok_enumerate_e_with_options(pos
, exist
, nparam
,
2932 free_evalue_refs(E
);
2936 E
= barvinok_enumerate_e_with_options(neg
, exist
, nparam
,
2939 free_evalue_refs(E
);
2942 Polyhedron_Free(para
);
2943 Polyhedron_Free(pos
);
2944 Polyhedron_Free(neg
);
2949 } END_FORALL_PVertex_in_ParamPolyhedron
;
2952 } END_FORALL_PVertex_in_ParamPolyhedron
;
2955 /* Search for vertex coordinate to split on */
2956 /* First look for one independent of the parameters */
2957 FORALL_PVertex_in_ParamPolyhedron(V
, last
, PP
) {
2958 for (int i
= 0; i
< exist
; ++i
) {
2960 for (j
= 0; j
< nparam
; ++j
)
2961 if (value_notzero_p(V
->Vertex
->p
[i
][j
]))
2965 value_set_si(M
->p
[0][0], 1);
2966 Vector_Set(M
->p
[0]+1, 0, nvar
+exist
);
2967 Vector_Copy(V
->Vertex
->p
[i
],
2968 M
->p
[0] + 1 + nvar
+ exist
, nparam
+1);
2969 value_oppose(M
->p
[0][1+nvar
+i
],
2970 V
->Vertex
->p
[i
][nparam
+1]);
2972 Polyhedron
*pos
, *neg
;
2973 value_set_si(M
->p
[0][0], 1);
2974 value_decrement(M
->p
[0][P
->Dimension
+1],
2975 M
->p
[0][P
->Dimension
+1]);
2976 neg
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
2977 value_set_si(f
, -1);
2978 Vector_Scale(M
->p
[0]+1, M
->p
[0]+1, f
,
2980 value_decrement(M
->p
[0][P
->Dimension
+1],
2981 M
->p
[0][P
->Dimension
+1]);
2982 value_decrement(M
->p
[0][P
->Dimension
+1],
2983 M
->p
[0][P
->Dimension
+1]);
2984 pos
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
2985 if (emptyQ(neg
) || emptyQ(pos
)) {
2986 Polyhedron_Free(pos
);
2987 Polyhedron_Free(neg
);
2990 Polyhedron_Free(pos
);
2991 value_increment(M
->p
[0][P
->Dimension
+1],
2992 M
->p
[0][P
->Dimension
+1]);
2993 pos
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
2995 fprintf(stderr
, "\nER: Vertex\n");
2996 #endif /* DEBUG_ER */
2998 EP
= enumerate_or(pos
, exist
, nparam
, options
);
3003 } END_FORALL_PVertex_in_ParamPolyhedron
;
3007 /* Search for vertex coordinate to split on */
3008 /* Now look for one that depends on the parameters */
3009 FORALL_PVertex_in_ParamPolyhedron(V
, last
, PP
) {
3010 for (int i
= 0; i
< exist
; ++i
) {
3011 value_set_si(M
->p
[0][0], 1);
3012 Vector_Set(M
->p
[0]+1, 0, nvar
+exist
);
3013 Vector_Copy(V
->Vertex
->p
[i
],
3014 M
->p
[0] + 1 + nvar
+ exist
, nparam
+1);
3015 value_oppose(M
->p
[0][1+nvar
+i
],
3016 V
->Vertex
->p
[i
][nparam
+1]);
3018 Polyhedron
*pos
, *neg
;
3019 value_set_si(M
->p
[0][0], 1);
3020 value_decrement(M
->p
[0][P
->Dimension
+1],
3021 M
->p
[0][P
->Dimension
+1]);
3022 neg
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
3023 value_set_si(f
, -1);
3024 Vector_Scale(M
->p
[0]+1, M
->p
[0]+1, f
,
3026 value_decrement(M
->p
[0][P
->Dimension
+1],
3027 M
->p
[0][P
->Dimension
+1]);
3028 value_decrement(M
->p
[0][P
->Dimension
+1],
3029 M
->p
[0][P
->Dimension
+1]);
3030 pos
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
3031 if (emptyQ(neg
) || emptyQ(pos
)) {
3032 Polyhedron_Free(pos
);
3033 Polyhedron_Free(neg
);
3036 Polyhedron_Free(pos
);
3037 value_increment(M
->p
[0][P
->Dimension
+1],
3038 M
->p
[0][P
->Dimension
+1]);
3039 pos
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
3041 fprintf(stderr
, "\nER: ParamVertex\n");
3042 #endif /* DEBUG_ER */
3044 EP
= enumerate_or(pos
, exist
, nparam
, options
);
3049 } END_FORALL_PVertex_in_ParamPolyhedron
;
3057 Polyhedron_Free(CEq
);
3061 Param_Polyhedron_Free(PP
);
3067 evalue
* barvinok_enumerate_pip(Polyhedron
*P
, unsigned exist
, unsigned nparam
,
3071 barvinok_options
*options
= barvinok_options_new_with_defaults();
3072 options
->MaxRays
= MaxRays
;
3073 E
= barvinok_enumerate_pip_with_options(P
, exist
, nparam
, options
);
3079 evalue
*barvinok_enumerate_pip_with_options(Polyhedron
*P
,
3080 unsigned exist
, unsigned nparam
, struct barvinok_options
*options
)
3085 evalue
*barvinok_enumerate_pip_with_options(Polyhedron
*P
,
3086 unsigned exist
, unsigned nparam
, struct barvinok_options
*options
)
3088 int nvar
= P
->Dimension
- exist
- nparam
;
3089 evalue
*EP
= evalue_zero();
3093 fprintf(stderr
, "\nER: PIP\n");
3094 #endif /* DEBUG_ER */
3096 Polyhedron
*D
= pip_projectout(P
, nvar
, exist
, nparam
);
3097 for (Q
= D
; Q
; Q
= N
) {
3101 exist
= Q
->Dimension
- nvar
- nparam
;
3102 E
= barvinok_enumerate_e_with_options(Q
, exist
, nparam
, options
);
3105 free_evalue_refs(E
);
3114 static bool is_single(Value
*row
, int pos
, int len
)
3116 return First_Non_Zero(row
, pos
) == -1 &&
3117 First_Non_Zero(row
+pos
+1, len
-pos
-1) == -1;
3120 static evalue
* barvinok_enumerate_e_r(Polyhedron
*P
,
3121 unsigned exist
, unsigned nparam
, barvinok_options
*options
);
3124 static int er_level
= 0;
3126 evalue
* barvinok_enumerate_e_with_options(Polyhedron
*P
,
3127 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
3129 fprintf(stderr
, "\nER: level %i\n", er_level
);
3131 Polyhedron_PrintConstraints(stderr
, P_VALUE_FMT
, P
);
3132 fprintf(stderr
, "\nE %d\nP %d\n", exist
, nparam
);
3134 P
= DomainConstraintSimplify(Polyhedron_Copy(P
), options
->MaxRays
);
3135 evalue
*EP
= barvinok_enumerate_e_r(P
, exist
, nparam
, options
);
3141 evalue
* barvinok_enumerate_e_with_options(Polyhedron
*P
,
3142 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
3144 P
= DomainConstraintSimplify(Polyhedron_Copy(P
), options
->MaxRays
);
3145 evalue
*EP
= barvinok_enumerate_e_r(P
, exist
, nparam
, options
);
3151 evalue
* barvinok_enumerate_e(Polyhedron
*P
, unsigned exist
, unsigned nparam
,
3155 barvinok_options
*options
= barvinok_options_new_with_defaults();
3156 options
->MaxRays
= MaxRays
;
3157 E
= barvinok_enumerate_e_with_options(P
, exist
, nparam
, options
);
3162 static evalue
* barvinok_enumerate_e_r(Polyhedron
*P
,
3163 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
3166 Polyhedron
*U
= Universe_Polyhedron(nparam
);
3167 evalue
*EP
= barvinok_enumerate_with_options(P
, U
, options
);
3168 //char *param_name[] = {"P", "Q", "R", "S", "T" };
3169 //print_evalue(stdout, EP, param_name);
3174 int nvar
= P
->Dimension
- exist
- nparam
;
3175 int len
= P
->Dimension
+ 2;
3178 POL_ENSURE_FACETS(P
);
3179 POL_ENSURE_VERTICES(P
);
3182 return evalue_zero();
3184 if (nvar
== 0 && nparam
== 0) {
3185 evalue
*EP
= evalue_zero();
3186 barvinok_count_with_options(P
, &EP
->x
.n
, options
);
3187 if (value_pos_p(EP
->x
.n
))
3188 value_set_si(EP
->x
.n
, 1);
3193 for (r
= 0; r
< P
->NbRays
; ++r
)
3194 if (value_zero_p(P
->Ray
[r
][0]) ||
3195 value_zero_p(P
->Ray
[r
][P
->Dimension
+1])) {
3197 for (i
= 0; i
< nvar
; ++i
)
3198 if (value_notzero_p(P
->Ray
[r
][i
+1]))
3202 for (i
= nvar
+ exist
; i
< nvar
+ exist
+ nparam
; ++i
)
3203 if (value_notzero_p(P
->Ray
[r
][i
+1]))
3205 if (i
>= nvar
+ exist
+ nparam
)
3208 if (r
< P
->NbRays
) {
3209 evalue
*EP
= evalue_zero();
3210 value_set_si(EP
->x
.n
, -1);
3215 for (r
= 0; r
< P
->NbEq
; ++r
)
3216 if ((first
= First_Non_Zero(P
->Constraint
[r
]+1+nvar
, exist
)) != -1)
3219 if (First_Non_Zero(P
->Constraint
[r
]+1+nvar
+first
+1,
3220 exist
-first
-1) != -1) {
3221 Polyhedron
*T
= rotate_along(P
, r
, nvar
, exist
, options
->MaxRays
);
3223 fprintf(stderr
, "\nER: Equality\n");
3224 #endif /* DEBUG_ER */
3225 evalue
*EP
= barvinok_enumerate_e_with_options(T
, exist
-1, nparam
,
3231 fprintf(stderr
, "\nER: Fixed\n");
3232 #endif /* DEBUG_ER */
3234 return barvinok_enumerate_e_with_options(P
, exist
-1, nparam
,
3237 Polyhedron
*T
= Polyhedron_Copy(P
);
3238 SwapColumns(T
, nvar
+1, nvar
+1+first
);
3239 evalue
*EP
= barvinok_enumerate_e_with_options(T
, exist
-1, nparam
,
3247 Vector
*row
= Vector_Alloc(len
);
3248 value_set_si(row
->p
[0], 1);
3253 enum constraint
* info
= new constraint
[exist
];
3254 for (int i
= 0; i
< exist
; ++i
) {
3256 for (int l
= P
->NbEq
; l
< P
->NbConstraints
; ++l
) {
3257 if (value_negz_p(P
->Constraint
[l
][nvar
+i
+1]))
3259 bool l_parallel
= is_single(P
->Constraint
[l
]+nvar
+1, i
, exist
);
3260 for (int u
= P
->NbEq
; u
< P
->NbConstraints
; ++u
) {
3261 if (value_posz_p(P
->Constraint
[u
][nvar
+i
+1]))
3263 bool lu_parallel
= l_parallel
||
3264 is_single(P
->Constraint
[u
]+nvar
+1, i
, exist
);
3265 value_oppose(f
, P
->Constraint
[u
][nvar
+i
+1]);
3266 Vector_Combine(P
->Constraint
[l
]+1, P
->Constraint
[u
]+1, row
->p
+1,
3267 f
, P
->Constraint
[l
][nvar
+i
+1], len
-1);
3268 if (!(info
[i
] & INDEPENDENT
)) {
3270 for (j
= 0; j
< exist
; ++j
)
3271 if (j
!= i
&& value_notzero_p(row
->p
[nvar
+j
+1]))
3274 //printf("independent: i: %d, l: %d, u: %d\n", i, l, u);
3275 info
[i
] = (constraint
)(info
[i
] | INDEPENDENT
);
3278 if (info
[i
] & ALL_POS
) {
3279 value_addto(row
->p
[len
-1], row
->p
[len
-1],
3280 P
->Constraint
[l
][nvar
+i
+1]);
3281 value_addto(row
->p
[len
-1], row
->p
[len
-1], f
);
3282 value_multiply(f
, f
, P
->Constraint
[l
][nvar
+i
+1]);
3283 value_subtract(row
->p
[len
-1], row
->p
[len
-1], f
);
3284 value_decrement(row
->p
[len
-1], row
->p
[len
-1]);
3285 ConstraintSimplify(row
->p
, row
->p
, len
, &f
);
3286 value_set_si(f
, -1);
3287 Vector_Scale(row
->p
+1, row
->p
+1, f
, len
-1);
3288 value_decrement(row
->p
[len
-1], row
->p
[len
-1]);
3289 Polyhedron
*T
= AddConstraints(row
->p
, 1, P
, options
->MaxRays
);
3291 //printf("not all_pos: i: %d, l: %d, u: %d\n", i, l, u);
3292 info
[i
] = (constraint
)(info
[i
] ^ ALL_POS
);
3294 //puts("pos remainder");
3295 //Polyhedron_Print(stdout, P_VALUE_FMT, T);
3298 if (!(info
[i
] & ONE_NEG
)) {
3300 negative_test_constraint(P
->Constraint
[l
],
3302 row
->p
, nvar
+i
, len
, &f
);
3303 oppose_constraint(row
->p
, len
, &f
);
3304 Polyhedron
*T
= AddConstraints(row
->p
, 1, P
,
3307 //printf("one_neg i: %d, l: %d, u: %d\n", i, l, u);
3308 info
[i
] = (constraint
)(info
[i
] | ONE_NEG
);
3310 //puts("neg remainder");
3311 //Polyhedron_Print(stdout, P_VALUE_FMT, T);
3313 } else if (!(info
[i
] & ROT_NEG
)) {
3314 if (parallel_constraints(P
->Constraint
[l
],
3316 row
->p
, nvar
, exist
)) {
3317 negative_test_constraint7(P
->Constraint
[l
],
3319 row
->p
, nvar
, exist
,
3321 oppose_constraint(row
->p
, len
, &f
);
3322 Polyhedron
*T
= AddConstraints(row
->p
, 1, P
,
3325 // printf("rot_neg i: %d, l: %d, u: %d\n", i, l, u);
3326 info
[i
] = (constraint
)(info
[i
] | ROT_NEG
);
3329 //puts("neg remainder");
3330 //Polyhedron_Print(stdout, P_VALUE_FMT, T);
3335 if (!(info
[i
] & ALL_POS
) && (info
[i
] & (ONE_NEG
| ROT_NEG
)))
3339 if (info
[i
] & ALL_POS
)
3346 for (int i = 0; i < exist; ++i)
3347 printf("%i: %i\n", i, info[i]);
3349 for (int i
= 0; i
< exist
; ++i
)
3350 if (info
[i
] & ALL_POS
) {
3352 fprintf(stderr
, "\nER: Positive\n");
3353 #endif /* DEBUG_ER */
3355 // Maybe we should chew off some of the fat here
3356 Matrix
*M
= Matrix_Alloc(P
->Dimension
, P
->Dimension
+1);
3357 for (int j
= 0; j
< P
->Dimension
; ++j
)
3358 value_set_si(M
->p
[j
][j
+ (j
>= i
+nvar
)], 1);
3359 Polyhedron
*T
= Polyhedron_Image(P
, M
, options
->MaxRays
);
3361 evalue
*EP
= barvinok_enumerate_e_with_options(T
, exist
-1, nparam
,
3369 for (int i
= 0; i
< exist
; ++i
)
3370 if (info
[i
] & ONE_NEG
) {
3372 fprintf(stderr
, "\nER: Negative\n");
3373 #endif /* DEBUG_ER */
3378 return barvinok_enumerate_e_with_options(P
, exist
-1, nparam
,
3381 Polyhedron
*T
= Polyhedron_Copy(P
);
3382 SwapColumns(T
, nvar
+1, nvar
+1+i
);
3383 evalue
*EP
= barvinok_enumerate_e_with_options(T
, exist
-1, nparam
,
3389 for (int i
= 0; i
< exist
; ++i
)
3390 if (info
[i
] & ROT_NEG
) {
3392 fprintf(stderr
, "\nER: Rotate\n");
3393 #endif /* DEBUG_ER */
3397 Polyhedron
*T
= rotate_along(P
, r
, nvar
, exist
, options
->MaxRays
);
3398 evalue
*EP
= barvinok_enumerate_e_with_options(T
, exist
-1, nparam
,
3403 for (int i
= 0; i
< exist
; ++i
)
3404 if (info
[i
] & INDEPENDENT
) {
3405 Polyhedron
*pos
, *neg
;
3407 /* Find constraint again and split off negative part */
3409 if (SplitOnVar(P
, i
, nvar
, exist
, options
->MaxRays
,
3410 row
, f
, true, &pos
, &neg
)) {
3412 fprintf(stderr
, "\nER: Split\n");
3413 #endif /* DEBUG_ER */
3416 barvinok_enumerate_e_with_options(neg
, exist
-1, nparam
, options
);
3418 barvinok_enumerate_e_with_options(pos
, exist
, nparam
, options
);
3420 free_evalue_refs(E
);
3422 Polyhedron_Free(neg
);
3423 Polyhedron_Free(pos
);
3437 EP
= enumerate_line(P
, exist
, nparam
, options
);
3441 EP
= barvinok_enumerate_pip_with_options(P
, exist
, nparam
, options
);
3445 EP
= enumerate_redundant_ray(P
, exist
, nparam
, options
);
3449 EP
= enumerate_sure(P
, exist
, nparam
, options
);
3453 EP
= enumerate_ray(P
, exist
, nparam
, options
);
3457 EP
= enumerate_sure2(P
, exist
, nparam
, options
);
3461 F
= unfringe(P
, options
->MaxRays
);
3462 if (!PolyhedronIncludes(F
, P
)) {
3464 fprintf(stderr
, "\nER: Fringed\n");
3465 #endif /* DEBUG_ER */
3466 EP
= barvinok_enumerate_e_with_options(F
, exist
, nparam
, options
);
3473 EP
= enumerate_vd(&P
, exist
, nparam
, options
);
3478 EP
= enumerate_sum(P
, exist
, nparam
, options
);
3485 Polyhedron
*pos
, *neg
;
3486 for (i
= 0; i
< exist
; ++i
)
3487 if (SplitOnVar(P
, i
, nvar
, exist
, options
->MaxRays
,
3488 row
, f
, false, &pos
, &neg
))
3494 EP
= enumerate_or(pos
, exist
, nparam
, options
);
3507 * remove equalities that require a "compression" of the parameters
3509 static Polyhedron
*remove_more_equalities(Polyhedron
*P
, unsigned nparam
,
3510 Matrix
**CP
, unsigned MaxRays
)
3513 remove_all_equalities(&P
, NULL
, CP
, NULL
, nparam
, MaxRays
);
3520 static gen_fun
*series(Polyhedron
*P
, unsigned nparam
, barvinok_options
*options
)
3530 assert(!Polyhedron_is_infinite_param(P
, nparam
));
3531 assert(P
->NbBid
== 0);
3532 assert(Polyhedron_has_revlex_positive_rays(P
, nparam
));
3534 P
= remove_more_equalities(P
, nparam
, &CP
, options
->MaxRays
);
3535 assert(P
->NbEq
== 0);
3537 nparam
= CP
->NbColumns
-1;
3542 barvinok_count_with_options(P
, &c
, options
);
3543 gf
= new gen_fun(c
);
3547 red
= gf_base::create(Polyhedron_Project(P
, nparam
),
3548 P
->Dimension
, nparam
, options
);
3549 POL_ENSURE_VERTICES(P
);
3550 red
->start_gf(P
, options
);
3562 gen_fun
* barvinok_series_with_options(Polyhedron
*P
, Polyhedron
* C
,
3563 barvinok_options
*options
)
3566 unsigned nparam
= C
->Dimension
;
3569 CA
= align_context(C
, P
->Dimension
, options
->MaxRays
);
3570 P
= DomainIntersection(P
, CA
, options
->MaxRays
);
3571 Polyhedron_Free(CA
);
3573 gf
= series(P
, nparam
, options
);
3578 gen_fun
* barvinok_series(Polyhedron
*P
, Polyhedron
* C
, unsigned MaxRays
)
3581 barvinok_options
*options
= barvinok_options_new_with_defaults();
3582 options
->MaxRays
= MaxRays
;
3583 gf
= barvinok_series_with_options(P
, C
, options
);
3588 static Polyhedron
*skew_into_positive_orthant(Polyhedron
*D
, unsigned nparam
,
3594 for (Polyhedron
*P
= D
; P
; P
= P
->next
) {
3595 POL_ENSURE_VERTICES(P
);
3596 assert(!Polyhedron_is_infinite_param(P
, nparam
));
3597 assert(P
->NbBid
== 0);
3598 assert(Polyhedron_has_positive_rays(P
, nparam
));
3600 for (int r
= 0; r
< P
->NbRays
; ++r
) {
3601 if (value_notzero_p(P
->Ray
[r
][P
->Dimension
+1]))
3603 for (int i
= 0; i
< nparam
; ++i
) {
3605 if (value_posz_p(P
->Ray
[r
][i
+1]))
3608 M
= Matrix_Alloc(D
->Dimension
+1, D
->Dimension
+1);
3609 for (int i
= 0; i
< D
->Dimension
+1; ++i
)
3610 value_set_si(M
->p
[i
][i
], 1);
3612 Inner_Product(P
->Ray
[r
]+1, M
->p
[i
], D
->Dimension
+1, &tmp
);
3613 if (value_posz_p(tmp
))
3616 for (j
= P
->Dimension
- nparam
; j
< P
->Dimension
; ++j
)
3617 if (value_pos_p(P
->Ray
[r
][j
+1]))
3619 assert(j
< P
->Dimension
);
3620 value_pdivision(tmp
, P
->Ray
[r
][j
+1], P
->Ray
[r
][i
+1]);
3621 value_subtract(M
->p
[i
][j
], M
->p
[i
][j
], tmp
);
3627 D
= DomainImage(D
, M
, MaxRays
);
3633 gen_fun
* barvinok_enumerate_union_series_with_options(Polyhedron
*D
, Polyhedron
* C
,
3634 barvinok_options
*options
)
3636 Polyhedron
*conv
, *D2
;
3638 gen_fun
*gf
= NULL
, *gf2
;
3639 unsigned nparam
= C
->Dimension
;
3644 CA
= align_context(C
, D
->Dimension
, options
->MaxRays
);
3645 D
= DomainIntersection(D
, CA
, options
->MaxRays
);
3646 Polyhedron_Free(CA
);
3648 D2
= skew_into_positive_orthant(D
, nparam
, options
->MaxRays
);
3649 for (Polyhedron
*P
= D2
; P
; P
= P
->next
) {
3650 assert(P
->Dimension
== D2
->Dimension
);
3653 P_gf
= series(Polyhedron_Copy(P
), nparam
, options
);
3657 gf
->add_union(P_gf
, options
);
3661 /* we actually only need the convex union of the parameter space
3662 * but the reducer classes currently expect a polyhedron in
3663 * the combined space
3665 Polyhedron_Free(gf
->context
);
3666 gf
->context
= DomainConvex(D2
, options
->MaxRays
);
3668 gf2
= gf
->summate(D2
->Dimension
- nparam
, options
);
3677 gen_fun
* barvinok_enumerate_union_series(Polyhedron
*D
, Polyhedron
* C
,
3681 barvinok_options
*options
= barvinok_options_new_with_defaults();
3682 options
->MaxRays
= MaxRays
;
3683 gf
= barvinok_enumerate_union_series_with_options(D
, C
, options
);
3688 evalue
* barvinok_enumerate_union(Polyhedron
*D
, Polyhedron
* C
, unsigned MaxRays
)
3691 gen_fun
*gf
= barvinok_enumerate_union_series(D
, C
, MaxRays
);