8 #include <NTL/mat_ZZ.h>
10 #include <barvinok/util.h>
11 #include <barvinok/evalue.h>
16 #include <barvinok/barvinok.h>
17 #include <barvinok/genfun.h>
18 #include <barvinok/options.h>
19 #include <barvinok/sample.h>
20 #include "conversion.h"
21 #include "decomposer.h"
22 #include "lattice_point.h"
23 #include "reduce_domain.h"
24 #include "genfun_constructor.h"
25 #include "remove_equalities.h"
36 using std::ostringstream
;
38 #define ALLOC(t,p) p = (t*)malloc(sizeof(*p))
46 dpoly_n(int d
, ZZ
& degree_0
, ZZ
& degree_1
, int offset
= 0) {
50 zz2value(degree_0
, d0
);
51 zz2value(degree_1
, d1
);
52 coeff
= Matrix_Alloc(d
+1, d
+1+1);
53 value_set_si(coeff
->p
[0][0], 1);
54 value_set_si(coeff
->p
[0][d
+1], 1);
55 for (int i
= 1; i
<= d
; ++i
) {
56 value_multiply(coeff
->p
[i
][0], coeff
->p
[i
-1][0], d0
);
57 Vector_Combine(coeff
->p
[i
-1], coeff
->p
[i
-1]+1, coeff
->p
[i
]+1,
59 value_set_si(coeff
->p
[i
][d
+1], i
);
60 value_multiply(coeff
->p
[i
][d
+1], coeff
->p
[i
][d
+1], coeff
->p
[i
-1][d
+1]);
61 value_decrement(d0
, d0
);
66 void div(dpoly
& d
, Vector
*count
, ZZ
& sign
) {
67 int len
= coeff
->NbRows
;
68 Matrix
* c
= Matrix_Alloc(coeff
->NbRows
, coeff
->NbColumns
);
71 for (int i
= 0; i
< len
; ++i
) {
72 Vector_Copy(coeff
->p
[i
], c
->p
[i
], len
+1);
73 for (int j
= 1; j
<= i
; ++j
) {
74 zz2value(d
.coeff
[j
], tmp
);
75 value_multiply(tmp
, tmp
, c
->p
[i
][len
]);
76 value_oppose(tmp
, tmp
);
77 Vector_Combine(c
->p
[i
], c
->p
[i
-j
], c
->p
[i
],
78 c
->p
[i
-j
][len
], tmp
, len
);
79 value_multiply(c
->p
[i
][len
], c
->p
[i
][len
], c
->p
[i
-j
][len
]);
81 zz2value(d
.coeff
[0], tmp
);
82 value_multiply(c
->p
[i
][len
], c
->p
[i
][len
], tmp
);
85 value_set_si(tmp
, -1);
86 Vector_Scale(c
->p
[len
-1], count
->p
, tmp
, len
);
87 value_assign(count
->p
[len
], c
->p
[len
-1][len
]);
89 Vector_Copy(c
->p
[len
-1], count
->p
, len
+1);
90 Vector_Normalize(count
->p
, len
+1);
98 * Searches for a vector that is not orthogonal to any
99 * of the rays in rays.
101 static void nonorthog(mat_ZZ
& rays
, vec_ZZ
& lambda
)
103 int dim
= rays
.NumCols();
105 lambda
.SetLength(dim
);
109 for (int i
= 2; !found
&& i
<= 50*dim
; i
+=4) {
110 for (int j
= 0; j
< MAX_TRY
; ++j
) {
111 for (int k
= 0; k
< dim
; ++k
) {
112 int r
= random_int(i
)+2;
113 int v
= (2*(r
%2)-1) * (r
>> 1);
117 for (; k
< rays
.NumRows(); ++k
)
118 if (lambda
* rays
[k
] == 0)
120 if (k
== rays
.NumRows()) {
129 static void add_rays(mat_ZZ
& rays
, Polyhedron
*i
, int *r
, int nvar
= -1,
132 unsigned dim
= i
->Dimension
;
135 for (int k
= 0; k
< i
->NbRays
; ++k
) {
136 if (!value_zero_p(i
->Ray
[k
][dim
+1]))
138 if (!all
&& nvar
!= dim
&& First_Non_Zero(i
->Ray
[k
]+1, nvar
) == -1)
140 values2zz(i
->Ray
[k
]+1, rays
[(*r
)++], nvar
);
144 static void mask_r(Matrix
*f
, int nr
, Vector
*lcm
, int p
, Vector
*val
, evalue
*ev
)
146 unsigned nparam
= lcm
->Size
;
149 Vector
* prod
= Vector_Alloc(f
->NbRows
);
150 Matrix_Vector_Product(f
, val
->p
, prod
->p
);
152 for (int i
= 0; i
< nr
; ++i
) {
153 value_modulus(prod
->p
[i
], prod
->p
[i
], f
->p
[i
][nparam
+1]);
154 isint
&= value_zero_p(prod
->p
[i
]);
156 value_set_si(ev
->d
, 1);
158 value_set_si(ev
->x
.n
, isint
);
165 if (value_one_p(lcm
->p
[p
]))
166 mask_r(f
, nr
, lcm
, p
+1, val
, ev
);
168 value_assign(tmp
, lcm
->p
[p
]);
169 value_set_si(ev
->d
, 0);
170 ev
->x
.p
= new_enode(periodic
, VALUE_TO_INT(tmp
), p
+1);
172 value_decrement(tmp
, tmp
);
173 value_assign(val
->p
[p
], tmp
);
174 mask_r(f
, nr
, lcm
, p
+1, val
, &ev
->x
.p
->arr
[VALUE_TO_INT(tmp
)]);
175 } while (value_pos_p(tmp
));
180 static void mask_fractional(Matrix
*f
, evalue
*factor
)
182 int nr
= f
->NbRows
, nc
= f
->NbColumns
;
185 for (n
= 0; n
< nr
&& value_notzero_p(f
->p
[n
][nc
-1]); ++n
)
186 if (value_notone_p(f
->p
[n
][nc
-1]) &&
187 value_notmone_p(f
->p
[n
][nc
-1]))
201 value_set_si(EV
.x
.n
, 1);
203 for (n
= 0; n
< nr
; ++n
) {
204 value_assign(m
, f
->p
[n
][nc
-1]);
205 if (value_one_p(m
) || value_mone_p(m
))
208 int j
= normal_mod(f
->p
[n
], nc
-1, &m
);
210 free_evalue_refs(factor
);
211 value_init(factor
->d
);
212 evalue_set_si(factor
, 0, 1);
216 values2zz(f
->p
[n
], row
, nc
-1);
219 if (j
< (nc
-1)-1 && row
[j
] > g
/2) {
220 for (int k
= j
; k
< (nc
-1); ++k
)
226 value_set_si(EP
.d
, 0);
227 EP
.x
.p
= new_enode(relation
, 2, 0);
228 value_clear(EP
.x
.p
->arr
[1].d
);
229 EP
.x
.p
->arr
[1] = *factor
;
230 evalue
*ev
= &EP
.x
.p
->arr
[0];
231 value_set_si(ev
->d
, 0);
232 ev
->x
.p
= new_enode(fractional
, 3, -1);
233 evalue_set_si(&ev
->x
.p
->arr
[1], 0, 1);
234 evalue_set_si(&ev
->x
.p
->arr
[2], 1, 1);
235 evalue
*E
= multi_monom(row
);
236 value_assign(EV
.d
, m
);
238 value_clear(ev
->x
.p
->arr
[0].d
);
239 ev
->x
.p
->arr
[0] = *E
;
245 free_evalue_refs(&EV
);
251 static void mask_table(Matrix
*f
, evalue
*factor
)
253 int nr
= f
->NbRows
, nc
= f
->NbColumns
;
256 for (n
= 0; n
< nr
&& value_notzero_p(f
->p
[n
][nc
-1]); ++n
)
257 if (value_notone_p(f
->p
[n
][nc
-1]) &&
258 value_notmone_p(f
->p
[n
][nc
-1]))
266 unsigned np
= nc
- 2;
267 Vector
*lcm
= Vector_Alloc(np
);
268 Vector
*val
= Vector_Alloc(nc
);
269 Vector_Set(val
->p
, 0, nc
);
270 value_set_si(val
->p
[np
], 1);
271 Vector_Set(lcm
->p
, 1, np
);
272 for (n
= 0; n
< nr
; ++n
) {
273 if (value_one_p(f
->p
[n
][nc
-1]) ||
274 value_mone_p(f
->p
[n
][nc
-1]))
276 for (int j
= 0; j
< np
; ++j
)
277 if (value_notzero_p(f
->p
[n
][j
])) {
278 Gcd(f
->p
[n
][j
], f
->p
[n
][nc
-1], &tmp
);
279 value_division(tmp
, f
->p
[n
][nc
-1], tmp
);
280 value_lcm(tmp
, lcm
->p
[j
], &lcm
->p
[j
]);
285 mask_r(f
, nr
, lcm
, 0, val
, &EP
);
290 free_evalue_refs(&EP
);
293 static void mask(Matrix
*f
, evalue
*factor
, barvinok_options
*options
)
295 if (options
->lookup_table
)
296 mask_table(f
, factor
);
298 mask_fractional(f
, factor
);
301 /* This structure encodes the power of the term in a rational generating function.
303 * Either E == NULL or constant = 0
304 * If E != NULL, then the power is E
305 * If E == NULL, then the power is coeff * param[pos] + constant
314 /* Returns the power of (t+1) in the term of a rational generating function,
315 * i.e., the scalar product of the actual lattice point and lambda.
316 * The lattice point is the unique lattice point in the fundamental parallelepiped
317 * of the unimodual cone i shifted to the parametric vertex V.
319 * PD is the parameter domain, which, if != NULL, may be used to simply the
320 * resulting expression.
322 * The result is returned in term.
324 void lattice_point(Param_Vertices
* V
, const mat_ZZ
& rays
, vec_ZZ
& lambda
,
325 term_info
* term
, Polyhedron
*PD
, barvinok_options
*options
)
327 unsigned nparam
= V
->Vertex
->NbColumns
- 2;
328 unsigned dim
= rays
.NumCols();
330 vertex
.SetDims(V
->Vertex
->NbRows
, nparam
+1);
334 value_set_si(lcm
, 1);
335 for (int j
= 0; j
< V
->Vertex
->NbRows
; ++j
) {
336 value_lcm(lcm
, V
->Vertex
->p
[j
][nparam
+1], &lcm
);
338 if (value_notone_p(lcm
)) {
339 Matrix
* mv
= Matrix_Alloc(dim
, nparam
+1);
340 for (int j
= 0 ; j
< dim
; ++j
) {
341 value_division(tmp
, lcm
, V
->Vertex
->p
[j
][nparam
+1]);
342 Vector_Scale(V
->Vertex
->p
[j
], mv
->p
[j
], tmp
, nparam
+1);
345 term
->E
= lattice_point(rays
, lambda
, mv
, lcm
, PD
, options
);
353 for (int i
= 0; i
< V
->Vertex
->NbRows
; ++i
) {
354 assert(value_one_p(V
->Vertex
->p
[i
][nparam
+1])); // for now
355 values2zz(V
->Vertex
->p
[i
], vertex
[i
], nparam
+1);
359 num
= lambda
* vertex
;
363 for (int j
= 0; j
< nparam
; ++j
)
369 term
->E
= multi_monom(num
);
373 term
->constant
= num
[nparam
];
376 term
->coeff
= num
[p
];
384 struct counter
: public np_base
{
394 counter(unsigned dim
) : np_base(dim
) {
399 virtual void init(Polyhedron
*P
) {
400 randomvector(P
, lambda
, dim
);
403 virtual void reset() {
404 mpq_set_si(count
, 0, 0);
411 virtual void handle(const mat_ZZ
& rays
, Value
*vertex
, const QQ
& c
,
412 unsigned long det
, int *closed
, barvinok_options
*options
);
413 virtual void get_count(Value
*result
) {
414 assert(value_one_p(&count
[0]._mp_den
));
415 value_assign(*result
, &count
[0]._mp_num
);
419 void counter::handle(const mat_ZZ
& rays
, Value
*V
, const QQ
& c
, unsigned long det
,
420 int *closed
, barvinok_options
*options
)
422 for (int k
= 0; k
< dim
; ++k
) {
423 if (lambda
* rays
[k
] == 0)
428 assert(c
.n
== 1 || c
.n
== -1);
431 lattice_point(V
, rays
, vertex
, det
, closed
);
432 num
= vertex
* lambda
;
435 normalize(sign
, offset
, den
);
438 dpoly
d(dim
, num
[0]);
439 for (int k
= 1; k
< num
.length(); ++k
) {
441 dpoly
term(dim
, num
[k
]);
444 dpoly
n(dim
, den
[0], 1);
445 for (int k
= 1; k
< dim
; ++k
) {
446 dpoly
fact(dim
, den
[k
], 1);
449 d
.div(n
, count
, sign
);
452 struct bfe_term
: public bfc_term_base
{
453 vector
<evalue
*> factors
;
455 bfe_term(int len
) : bfc_term_base(len
) {
459 for (int i
= 0; i
< factors
.size(); ++i
) {
462 free_evalue_refs(factors
[i
]);
468 static void print_int_vector(int *v
, int len
, char *name
)
470 cerr
<< name
<< endl
;
471 for (int j
= 0; j
< len
; ++j
) {
477 static void print_bfc_terms(mat_ZZ
& factors
, bfc_vec
& v
)
480 cerr
<< "factors" << endl
;
481 cerr
<< factors
<< endl
;
482 for (int i
= 0; i
< v
.size(); ++i
) {
483 cerr
<< "term: " << i
<< endl
;
484 print_int_vector(v
[i
]->powers
, factors
.NumRows(), "powers");
485 cerr
<< "terms" << endl
;
486 cerr
<< v
[i
]->terms
<< endl
;
487 bfc_term
* bfct
= static_cast<bfc_term
*>(v
[i
]);
488 cerr
<< bfct
->c
<< endl
;
492 static void print_bfe_terms(mat_ZZ
& factors
, bfc_vec
& v
)
495 cerr
<< "factors" << endl
;
496 cerr
<< factors
<< endl
;
497 for (int i
= 0; i
< v
.size(); ++i
) {
498 cerr
<< "term: " << i
<< endl
;
499 print_int_vector(v
[i
]->powers
, factors
.NumRows(), "powers");
500 cerr
<< "terms" << endl
;
501 cerr
<< v
[i
]->terms
<< endl
;
502 bfe_term
* bfet
= static_cast<bfe_term
*>(v
[i
]);
503 for (int j
= 0; j
< v
[i
]->terms
.NumRows(); ++j
) {
504 char * test
[] = {"a", "b"};
505 print_evalue(stderr
, bfet
->factors
[j
], test
);
506 fprintf(stderr
, "\n");
511 struct bfcounter
: public bfcounter_base
{
514 bfcounter(unsigned dim
) : bfcounter_base(dim
) {
521 virtual void base(mat_ZZ
& factors
, bfc_vec
& v
);
522 virtual void get_count(Value
*result
) {
523 assert(value_one_p(&count
[0]._mp_den
));
524 value_assign(*result
, &count
[0]._mp_num
);
528 void bfcounter::base(mat_ZZ
& factors
, bfc_vec
& v
)
530 unsigned nf
= factors
.NumRows();
532 for (int i
= 0; i
< v
.size(); ++i
) {
533 bfc_term
* bfct
= static_cast<bfc_term
*>(v
[i
]);
535 // factor is always positive, so we always
537 for (int k
= 0; k
< nf
; ++k
)
538 total_power
+= v
[i
]->powers
[k
];
541 for (j
= 0; j
< nf
; ++j
)
542 if (v
[i
]->powers
[j
] > 0)
545 dpoly
D(total_power
, factors
[j
][0], 1);
546 for (int k
= 1; k
< v
[i
]->powers
[j
]; ++k
) {
547 dpoly
fact(total_power
, factors
[j
][0], 1);
551 for (int k
= 0; k
< v
[i
]->powers
[j
]; ++k
) {
552 dpoly
fact(total_power
, factors
[j
][0], 1);
556 for (int k
= 0; k
< v
[i
]->terms
.NumRows(); ++k
) {
557 dpoly
n(total_power
, v
[i
]->terms
[k
][0]);
558 mpq_set_si(tcount
, 0, 1);
559 n
.div(D
, tcount
, one
);
561 bfct
->c
[k
].n
= -bfct
->c
[k
].n
;
562 zz2value(bfct
->c
[k
].n
, tn
);
563 zz2value(bfct
->c
[k
].d
, td
);
565 mpz_mul(mpq_numref(tcount
), mpq_numref(tcount
), tn
);
566 mpz_mul(mpq_denref(tcount
), mpq_denref(tcount
), td
);
567 mpq_canonicalize(tcount
);
568 mpq_add(count
, count
, tcount
);
575 /* Check whether the polyhedron is unbounded and if so,
576 * check whether it has any (and therefore an infinite number of)
578 * If one of the vertices is integer, then we are done.
579 * Otherwise, transform the polyhedron such that one of the rays
580 * is the first unit vector and cut it off at a height that ensures
581 * that if the whole polyhedron has any points, then the remaining part
582 * has integer points. In particular we add the largest coefficient
583 * of a ray to the highest vertex (rounded up).
585 static bool Polyhedron_is_infinite(Polyhedron
*P
, Value
* result
,
586 barvinok_options
*options
)
598 for (; r
< P
->NbRays
; ++r
)
599 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1]))
601 if (P
->NbBid
== 0 && r
== P
->NbRays
)
604 if (options
->count_sample_infinite
) {
607 sample
= Polyhedron_Sample(P
, options
);
609 value_set_si(*result
, 0);
611 value_set_si(*result
, -1);
617 for (int i
= 0; i
< P
->NbRays
; ++i
)
618 if (value_one_p(P
->Ray
[i
][1+P
->Dimension
])) {
619 value_set_si(*result
, -1);
624 v
= Vector_Alloc(P
->Dimension
+1);
625 Vector_Gcd(P
->Ray
[r
]+1, P
->Dimension
, &g
);
626 Vector_AntiScale(P
->Ray
[r
]+1, v
->p
, g
, P
->Dimension
+1);
627 M
= unimodular_complete(v
);
628 value_set_si(M
->p
[P
->Dimension
][P
->Dimension
], 1);
631 P
= Polyhedron_Preimage(P
, M2
, 0);
640 value_set_si(size
, 0);
642 for (int i
= 0; i
< P
->NbBid
; ++i
) {
643 value_absolute(tmp
, P
->Ray
[i
][1]);
644 if (value_gt(tmp
, size
))
645 value_assign(size
, tmp
);
647 for (int i
= P
->NbBid
; i
< P
->NbRays
; ++i
) {
648 if (value_zero_p(P
->Ray
[i
][P
->Dimension
+1])) {
649 if (value_gt(P
->Ray
[i
][1], size
))
650 value_assign(size
, P
->Ray
[i
][1]);
653 mpz_cdiv_q(tmp
, P
->Ray
[i
][1], P
->Ray
[i
][P
->Dimension
+1]);
654 if (first
|| value_gt(tmp
, offset
)) {
655 value_assign(offset
, tmp
);
659 value_addto(offset
, offset
, size
);
663 v
= Vector_Alloc(P
->Dimension
+2);
664 value_set_si(v
->p
[0], 1);
665 value_set_si(v
->p
[1], -1);
666 value_assign(v
->p
[1+P
->Dimension
], offset
);
667 R
= AddConstraints(v
->p
, 1, P
, options
->MaxRays
);
675 barvinok_count_with_options(P
, &c
, options
);
678 value_set_si(*result
, 0);
680 value_set_si(*result
, -1);
686 typedef Polyhedron
* Polyhedron_p
;
688 static void barvinok_count_f(Polyhedron
*P
, Value
* result
,
689 barvinok_options
*options
);
691 void barvinok_count_with_options(Polyhedron
*P
, Value
* result
,
692 struct barvinok_options
*options
)
697 bool infinite
= false;
700 value_set_si(*result
, 0);
706 P
= remove_equalities(P
);
707 P
= DomainConstraintSimplify(P
, options
->MaxRays
);
711 } while (!emptyQ(P
) && P
->NbEq
!= 0);
714 value_set_si(*result
, 0);
719 if (Polyhedron_is_infinite(P
, result
, options
)) {
724 if (P
->Dimension
== 0) {
725 /* Test whether the constraints are satisfied */
726 POL_ENSURE_VERTICES(P
);
727 value_set_si(*result
, !emptyQ(P
));
732 Q
= Polyhedron_Factor(P
, 0, options
->MaxRays
);
740 barvinok_count_f(P
, result
, options
);
741 if (value_neg_p(*result
))
743 if (Q
&& P
->next
&& value_notzero_p(*result
)) {
747 for (Q
= P
->next
; Q
; Q
= Q
->next
) {
748 barvinok_count_f(Q
, &factor
, options
);
749 if (value_neg_p(factor
)) {
752 } else if (Q
->next
&& value_zero_p(factor
)) {
753 value_set_si(*result
, 0);
756 value_multiply(*result
, *result
, factor
);
765 value_set_si(*result
, -1);
768 void barvinok_count(Polyhedron
*P
, Value
* result
, unsigned NbMaxCons
)
770 barvinok_options
*options
= barvinok_options_new_with_defaults();
771 options
->MaxRays
= NbMaxCons
;
772 barvinok_count_with_options(P
, result
, options
);
773 barvinok_options_free(options
);
776 static void barvinok_count_f(Polyhedron
*P
, Value
* result
,
777 barvinok_options
*options
)
780 value_set_si(*result
, 0);
784 if (P
->Dimension
== 1)
785 return Line_Length(P
, result
);
787 int c
= P
->NbConstraints
;
788 POL_ENSURE_FACETS(P
);
789 if (c
!= P
->NbConstraints
|| P
->NbEq
!= 0)
790 return barvinok_count_with_options(P
, result
, options
);
792 POL_ENSURE_VERTICES(P
);
794 if (Polyhedron_is_infinite(P
, result
, options
))
798 if (options
->incremental_specialization
== 2)
799 cnt
= new bfcounter(P
->Dimension
);
800 else if (options
->incremental_specialization
== 1)
801 cnt
= new icounter(P
->Dimension
);
803 cnt
= new counter(P
->Dimension
);
804 cnt
->start(P
, options
);
806 cnt
->get_count(result
);
810 static void uni_polynom(int param
, Vector
*c
, evalue
*EP
)
812 unsigned dim
= c
->Size
-2;
814 value_set_si(EP
->d
,0);
815 EP
->x
.p
= new_enode(polynomial
, dim
+1, param
+1);
816 for (int j
= 0; j
<= dim
; ++j
)
817 evalue_set(&EP
->x
.p
->arr
[j
], c
->p
[j
], c
->p
[dim
+1]);
820 static void multi_polynom(Vector
*c
, evalue
* X
, evalue
*EP
)
822 unsigned dim
= c
->Size
-2;
826 evalue_set(&EC
, c
->p
[dim
], c
->p
[dim
+1]);
829 evalue_set(EP
, c
->p
[dim
], c
->p
[dim
+1]);
831 for (int i
= dim
-1; i
>= 0; --i
) {
833 value_assign(EC
.x
.n
, c
->p
[i
]);
836 free_evalue_refs(&EC
);
839 Polyhedron
*unfringe (Polyhedron
*P
, unsigned MaxRays
)
841 int len
= P
->Dimension
+2;
842 Polyhedron
*T
, *R
= P
;
845 Vector
*row
= Vector_Alloc(len
);
846 value_set_si(row
->p
[0], 1);
848 R
= DomainConstraintSimplify(Polyhedron_Copy(P
), MaxRays
);
850 Matrix
*M
= Matrix_Alloc(2, len
-1);
851 value_set_si(M
->p
[1][len
-2], 1);
852 for (int v
= 0; v
< P
->Dimension
; ++v
) {
853 value_set_si(M
->p
[0][v
], 1);
854 Polyhedron
*I
= Polyhedron_Image(R
, M
, 2+1);
855 value_set_si(M
->p
[0][v
], 0);
856 for (int r
= 0; r
< I
->NbConstraints
; ++r
) {
857 if (value_zero_p(I
->Constraint
[r
][0]))
859 if (value_zero_p(I
->Constraint
[r
][1]))
861 if (value_one_p(I
->Constraint
[r
][1]))
863 if (value_mone_p(I
->Constraint
[r
][1]))
865 value_absolute(g
, I
->Constraint
[r
][1]);
866 Vector_Set(row
->p
+1, 0, len
-2);
867 value_division(row
->p
[1+v
], I
->Constraint
[r
][1], g
);
868 mpz_fdiv_q(row
->p
[len
-1], I
->Constraint
[r
][2], g
);
870 R
= AddConstraints(row
->p
, 1, R
, MaxRays
);
882 /* this procedure may have false negatives */
883 static bool Polyhedron_is_infinite_param(Polyhedron
*P
, unsigned nparam
)
886 for (r
= 0; r
< P
->NbRays
; ++r
) {
887 if (!value_zero_p(P
->Ray
[r
][0]) &&
888 !value_zero_p(P
->Ray
[r
][P
->Dimension
+1]))
890 if (First_Non_Zero(P
->Ray
[r
]+1+P
->Dimension
-nparam
, nparam
) == -1)
896 /* Check whether all rays point in the positive directions
899 static bool Polyhedron_has_positive_rays(Polyhedron
*P
, unsigned nparam
)
902 for (r
= 0; r
< P
->NbRays
; ++r
)
903 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1])) {
905 for (i
= P
->Dimension
- nparam
; i
< P
->Dimension
; ++i
)
906 if (value_neg_p(P
->Ray
[r
][i
+1]))
912 /* Check whether all rays are revlex positive in the parameters
914 static bool Polyhedron_has_revlex_positive_rays(Polyhedron
*P
, unsigned nparam
)
917 for (r
= 0; r
< P
->NbRays
; ++r
) {
918 if (value_notzero_p(P
->Ray
[r
][P
->Dimension
+1]))
921 for (i
= P
->Dimension
-1; i
>= P
->Dimension
-nparam
; --i
) {
922 if (value_neg_p(P
->Ray
[r
][i
+1]))
924 if (value_pos_p(P
->Ray
[r
][i
+1]))
927 /* A ray independent of the parameters */
928 if (i
< P
->Dimension
-nparam
)
934 typedef evalue
* evalue_p
;
936 struct enumerator_base
{
940 vertex_decomposer
*vpd
;
942 enumerator_base(unsigned dim
, vertex_decomposer
*vpd
)
947 vE
= new evalue_p
[vpd
->nbV
];
948 for (int j
= 0; j
< vpd
->nbV
; ++j
)
952 evalue_set_si(&mone
, -1, 1);
955 void decompose_at(Param_Vertices
*V
, int _i
, barvinok_options
*options
) {
959 value_init(vE
[_i
]->d
);
960 evalue_set_si(vE
[_i
], 0, 1);
962 vpd
->decompose_at_vertex(V
, _i
, options
);
965 virtual ~enumerator_base() {
966 for (int j
= 0; j
< vpd
->nbV
; ++j
)
968 free_evalue_refs(vE
[j
]);
973 free_evalue_refs(&mone
);
976 static enumerator_base
*create(Polyhedron
*P
, unsigned dim
, unsigned nbV
,
977 barvinok_options
*options
);
980 struct enumerator
: public signed_cone_consumer
, public vertex_decomposer
,
981 public enumerator_base
{
989 enumerator(Polyhedron
*P
, unsigned dim
, unsigned nbV
) :
990 vertex_decomposer(P
, nbV
, *this), enumerator_base(dim
, this) {
993 randomvector(P
, lambda
, dim
);
995 c
= Vector_Alloc(dim
+2);
1005 virtual void handle(const signed_cone
& sc
, barvinok_options
*options
);
1008 void enumerator::handle(const signed_cone
& sc
, barvinok_options
*options
)
1010 assert(sc
.det
== 1);
1013 assert(sc
.rays
.NumRows() == dim
);
1014 for (int k
= 0; k
< dim
; ++k
) {
1015 if (lambda
* sc
.rays
[k
] == 0)
1021 lattice_point(V
, sc
.rays
, lambda
, &num
, 0, options
);
1022 den
= sc
.rays
* lambda
;
1023 normalize(sign
, num
.constant
, den
);
1025 dpoly
n(dim
, den
[0], 1);
1026 for (int k
= 1; k
< dim
; ++k
) {
1027 dpoly
fact(dim
, den
[k
], 1);
1030 if (num
.E
!= NULL
) {
1031 ZZ
one(INIT_VAL
, 1);
1032 dpoly_n
d(dim
, num
.constant
, one
);
1035 multi_polynom(c
, num
.E
, &EV
);
1036 eadd(&EV
, vE
[vert
]);
1037 free_evalue_refs(&EV
);
1038 free_evalue_refs(num
.E
);
1040 } else if (num
.pos
!= -1) {
1041 dpoly_n
d(dim
, num
.constant
, num
.coeff
);
1044 uni_polynom(num
.pos
, c
, &EV
);
1045 eadd(&EV
, vE
[vert
]);
1046 free_evalue_refs(&EV
);
1048 mpq_set_si(count
, 0, 1);
1049 dpoly
d(dim
, num
.constant
);
1050 d
.div(n
, count
, sign
);
1053 evalue_set(&EV
, &count
[0]._mp_num
, &count
[0]._mp_den
);
1054 eadd(&EV
, vE
[vert
]);
1055 free_evalue_refs(&EV
);
1059 struct ienumerator_base
: enumerator_base
{
1062 ienumerator_base(unsigned dim
, vertex_decomposer
*vpd
) :
1063 enumerator_base(dim
,vpd
) {
1064 E_vertex
= new evalue_p
[dim
];
1067 virtual ~ienumerator_base() {
1071 evalue
*E_num(int i
, int d
) {
1072 return E_vertex
[i
+ (dim
-d
)];
1081 cumulator(evalue
*factor
, evalue
*v
, dpoly_r
*r
) :
1082 factor(factor
), v(v
), r(r
) {}
1084 void cumulate(barvinok_options
*options
);
1086 virtual void add_term(const vector
<int>& powers
, evalue
*f2
) = 0;
1089 void cumulator::cumulate(barvinok_options
*options
)
1091 evalue cum
; // factor * 1 * E_num[0]/1 * (E_num[0]-1)/2 *...
1093 evalue t
; // E_num[0] - (m-1)
1097 if (options
->lookup_table
) {
1099 evalue_set_si(&mone
, -1, 1);
1103 evalue_copy(&cum
, factor
);
1106 value_set_si(f
.d
, 1);
1107 value_set_si(f
.x
.n
, 1);
1111 if (!options
->lookup_table
) {
1112 for (cst
= &t
; value_zero_p(cst
->d
); ) {
1113 if (cst
->x
.p
->type
== fractional
)
1114 cst
= &cst
->x
.p
->arr
[1];
1116 cst
= &cst
->x
.p
->arr
[0];
1120 for (int m
= 0; m
< r
->len
; ++m
) {
1123 value_set_si(f
.d
, m
);
1125 if (!options
->lookup_table
)
1126 value_subtract(cst
->x
.n
, cst
->x
.n
, cst
->d
);
1132 dpoly_r_term_list
& current
= r
->c
[r
->len
-1-m
];
1133 dpoly_r_term_list::iterator j
;
1134 for (j
= current
.begin(); j
!= current
.end(); ++j
) {
1135 if ((*j
)->coeff
== 0)
1137 evalue
*f2
= new evalue
;
1139 value_init(f2
->x
.n
);
1140 zz2value((*j
)->coeff
, f2
->x
.n
);
1141 zz2value(r
->denom
, f2
->d
);
1144 add_term((*j
)->powers
, f2
);
1147 free_evalue_refs(&f
);
1148 free_evalue_refs(&t
);
1149 free_evalue_refs(&cum
);
1150 if (options
->lookup_table
)
1151 free_evalue_refs(&mone
);
1154 struct E_poly_term
{
1159 struct ie_cum
: public cumulator
{
1160 vector
<E_poly_term
*> terms
;
1162 ie_cum(evalue
*factor
, evalue
*v
, dpoly_r
*r
) : cumulator(factor
, v
, r
) {}
1164 virtual void add_term(const vector
<int>& powers
, evalue
*f2
);
1167 void ie_cum::add_term(const vector
<int>& powers
, evalue
*f2
)
1170 for (k
= 0; k
< terms
.size(); ++k
) {
1171 if (terms
[k
]->powers
== powers
) {
1172 eadd(f2
, terms
[k
]->E
);
1173 free_evalue_refs(f2
);
1178 if (k
>= terms
.size()) {
1179 E_poly_term
*ET
= new E_poly_term
;
1180 ET
->powers
= powers
;
1182 terms
.push_back(ET
);
1186 struct ienumerator
: public signed_cone_consumer
, public vertex_decomposer
,
1187 public ienumerator_base
{
1193 ienumerator(Polyhedron
*P
, unsigned dim
, unsigned nbV
) :
1194 vertex_decomposer(P
, nbV
, *this), ienumerator_base(dim
, this) {
1195 vertex
.SetDims(1, dim
);
1197 den
.SetDims(dim
, dim
);
1205 virtual void handle(const signed_cone
& sc
, barvinok_options
*options
);
1206 void reduce(evalue
*factor
, const mat_ZZ
& num
, const mat_ZZ
& den_f
,
1207 barvinok_options
*options
);
1210 void ienumerator::reduce(evalue
*factor
, const mat_ZZ
& num
, const mat_ZZ
& den_f
,
1211 barvinok_options
*options
)
1213 unsigned len
= den_f
.NumRows(); // number of factors in den
1214 unsigned dim
= num
.NumCols();
1215 assert(num
.NumRows() == 1);
1218 eadd(factor
, vE
[vert
]);
1227 split_one(num
, num_s
, num_p
, den_f
, den_s
, den_r
);
1230 den_p
.SetLength(len
);
1234 normalize(one
, num_s
, num_p
, den_s
, den_p
, den_r
);
1236 emul(&mone
, factor
);
1240 for (int k
= 0; k
< len
; ++k
) {
1243 else if (den_s
[k
] == 0)
1246 if (no_param
== 0) {
1247 reduce(factor
, num_p
, den_r
, options
);
1251 pden
.SetDims(only_param
, dim
-1);
1253 for (k
= 0, l
= 0; k
< len
; ++k
)
1255 pden
[l
++] = den_r
[k
];
1257 for (k
= 0; k
< len
; ++k
)
1261 dpoly
n(no_param
, num_s
[0]);
1262 dpoly
D(no_param
, den_s
[k
], 1);
1263 for ( ; ++k
< len
; )
1264 if (den_p
[k
] == 0) {
1265 dpoly
fact(no_param
, den_s
[k
], 1);
1270 // if no_param + only_param == len then all powers
1271 // below will be all zero
1272 if (no_param
+ only_param
== len
) {
1273 if (E_num(0, dim
) != 0)
1274 r
= new dpoly_r(n
, len
);
1276 mpq_set_si(tcount
, 0, 1);
1278 n
.div(D
, tcount
, one
);
1280 if (value_notzero_p(mpq_numref(tcount
))) {
1284 value_assign(f
.x
.n
, mpq_numref(tcount
));
1285 value_assign(f
.d
, mpq_denref(tcount
));
1287 reduce(factor
, num_p
, pden
, options
);
1288 free_evalue_refs(&f
);
1293 for (k
= 0; k
< len
; ++k
) {
1294 if (den_s
[k
] == 0 || den_p
[k
] == 0)
1297 dpoly
pd(no_param
-1, den_s
[k
], 1);
1300 for (l
= 0; l
< k
; ++l
)
1301 if (den_r
[l
] == den_r
[k
])
1305 r
= new dpoly_r(n
, pd
, l
, len
);
1307 dpoly_r
*nr
= new dpoly_r(r
, pd
, l
, len
);
1313 dpoly_r
*rc
= r
->div(D
);
1316 if (E_num(0, dim
) == 0) {
1317 int common
= pden
.NumRows();
1318 dpoly_r_term_list
& final
= r
->c
[r
->len
-1];
1324 zz2value(r
->denom
, f
.d
);
1325 dpoly_r_term_list::iterator j
;
1326 for (j
= final
.begin(); j
!= final
.end(); ++j
) {
1327 if ((*j
)->coeff
== 0)
1330 for (int k
= 0; k
< r
->dim
; ++k
) {
1331 int n
= (*j
)->powers
[k
];
1334 pden
.SetDims(rows
+n
, pden
.NumCols());
1335 for (int l
= 0; l
< n
; ++l
)
1336 pden
[rows
+l
] = den_r
[k
];
1340 evalue_copy(&t
, factor
);
1341 zz2value((*j
)->coeff
, f
.x
.n
);
1343 reduce(&t
, num_p
, pden
, options
);
1344 free_evalue_refs(&t
);
1346 free_evalue_refs(&f
);
1348 ie_cum
cum(factor
, E_num(0, dim
), r
);
1349 cum
.cumulate(options
);
1351 int common
= pden
.NumRows();
1353 for (int j
= 0; j
< cum
.terms
.size(); ++j
) {
1355 pden
.SetDims(rows
, pden
.NumCols());
1356 for (int k
= 0; k
< r
->dim
; ++k
) {
1357 int n
= cum
.terms
[j
]->powers
[k
];
1360 pden
.SetDims(rows
+n
, pden
.NumCols());
1361 for (int l
= 0; l
< n
; ++l
)
1362 pden
[rows
+l
] = den_r
[k
];
1365 reduce(cum
.terms
[j
]->E
, num_p
, pden
, options
);
1366 free_evalue_refs(cum
.terms
[j
]->E
);
1367 delete cum
.terms
[j
]->E
;
1368 delete cum
.terms
[j
];
1375 static int type_offset(enode
*p
)
1377 return p
->type
== fractional
? 1 :
1378 p
->type
== flooring
? 1 : 0;
1381 static int edegree(evalue
*e
)
1386 if (value_notzero_p(e
->d
))
1390 int i
= type_offset(p
);
1391 if (p
->size
-i
-1 > d
)
1392 d
= p
->size
- i
- 1;
1393 for (; i
< p
->size
; i
++) {
1394 int d2
= edegree(&p
->arr
[i
]);
1401 void ienumerator::handle(const signed_cone
& sc
, barvinok_options
*options
)
1403 assert(sc
.det
== 1);
1405 assert(sc
.rays
.NumRows() == dim
);
1407 lattice_point(V
, sc
.rays
, vertex
[0], E_vertex
, options
);
1413 evalue_set_si(&one
, sc
.sign
, 1);
1414 reduce(&one
, vertex
, den
, options
);
1415 free_evalue_refs(&one
);
1417 for (int i
= 0; i
< dim
; ++i
)
1419 free_evalue_refs(E_vertex
[i
]);
1424 struct bfenumerator
: public vertex_decomposer
, public bf_base
,
1425 public ienumerator_base
{
1428 bfenumerator(Polyhedron
*P
, unsigned dim
, unsigned nbV
) :
1429 vertex_decomposer(P
, nbV
, *this),
1430 bf_base(dim
), ienumerator_base(dim
, this) {
1438 virtual void handle(const signed_cone
& sc
, barvinok_options
*options
);
1439 virtual void base(mat_ZZ
& factors
, bfc_vec
& v
);
1441 bfc_term_base
* new_bf_term(int len
) {
1442 bfe_term
* t
= new bfe_term(len
);
1446 virtual void set_factor(bfc_term_base
*t
, int k
, int change
) {
1447 bfe_term
* bfet
= static_cast<bfe_term
*>(t
);
1448 factor
= bfet
->factors
[k
];
1449 assert(factor
!= NULL
);
1450 bfet
->factors
[k
] = NULL
;
1452 emul(&mone
, factor
);
1455 virtual void set_factor(bfc_term_base
*t
, int k
, mpq_t
&q
, int change
) {
1456 bfe_term
* bfet
= static_cast<bfe_term
*>(t
);
1457 factor
= bfet
->factors
[k
];
1458 assert(factor
!= NULL
);
1459 bfet
->factors
[k
] = NULL
;
1465 value_oppose(f
.x
.n
, mpq_numref(q
));
1467 value_assign(f
.x
.n
, mpq_numref(q
));
1468 value_assign(f
.d
, mpq_denref(q
));
1470 free_evalue_refs(&f
);
1473 virtual void set_factor(bfc_term_base
*t
, int k
, const QQ
& c
, int change
) {
1474 bfe_term
* bfet
= static_cast<bfe_term
*>(t
);
1476 factor
= new evalue
;
1481 zz2value(c
.n
, f
.x
.n
);
1483 value_oppose(f
.x
.n
, f
.x
.n
);
1486 value_init(factor
->d
);
1487 evalue_copy(factor
, bfet
->factors
[k
]);
1489 free_evalue_refs(&f
);
1492 void set_factor(evalue
*f
, int change
) {
1498 virtual void insert_term(bfc_term_base
*t
, int i
) {
1499 bfe_term
* bfet
= static_cast<bfe_term
*>(t
);
1500 int len
= t
->terms
.NumRows()-1; // already increased by one
1502 bfet
->factors
.resize(len
+1);
1503 for (int j
= len
; j
> i
; --j
) {
1504 bfet
->factors
[j
] = bfet
->factors
[j
-1];
1505 t
->terms
[j
] = t
->terms
[j
-1];
1507 bfet
->factors
[i
] = factor
;
1511 virtual void update_term(bfc_term_base
*t
, int i
) {
1512 bfe_term
* bfet
= static_cast<bfe_term
*>(t
);
1514 eadd(factor
, bfet
->factors
[i
]);
1515 free_evalue_refs(factor
);
1519 virtual bool constant_vertex(int dim
) { return E_num(0, dim
) == 0; }
1521 virtual void cum(bf_reducer
*bfr
, bfc_term_base
*t
, int k
, dpoly_r
*r
,
1522 barvinok_options
*options
);
1525 enumerator_base
*enumerator_base::create(Polyhedron
*P
, unsigned dim
, unsigned nbV
,
1526 barvinok_options
*options
)
1528 enumerator_base
*eb
;
1530 if (options
->incremental_specialization
== BV_SPECIALIZATION_BF
)
1531 eb
= new bfenumerator(P
, dim
, nbV
);
1532 else if (options
->incremental_specialization
== BV_SPECIALIZATION_DF
)
1533 eb
= new ienumerator(P
, dim
, nbV
);
1535 eb
= new enumerator(P
, dim
, nbV
);
1540 struct bfe_cum
: public cumulator
{
1542 bfc_term_base
*told
;
1546 bfe_cum(evalue
*factor
, evalue
*v
, dpoly_r
*r
, bf_reducer
*bfr
,
1547 bfc_term_base
*t
, int k
, bfenumerator
*e
) :
1548 cumulator(factor
, v
, r
), told(t
), k(k
),
1552 virtual void add_term(const vector
<int>& powers
, evalue
*f2
);
1555 void bfe_cum::add_term(const vector
<int>& powers
, evalue
*f2
)
1557 bfr
->update_powers(powers
);
1559 bfc_term_base
* t
= bfe
->find_bfc_term(bfr
->vn
, bfr
->npowers
, bfr
->nnf
);
1560 bfe
->set_factor(f2
, bfr
->l_changes
% 2);
1561 bfe
->add_term(t
, told
->terms
[k
], bfr
->l_extra_num
);
1564 void bfenumerator::cum(bf_reducer
*bfr
, bfc_term_base
*t
, int k
,
1565 dpoly_r
*r
, barvinok_options
*options
)
1567 bfe_term
* bfet
= static_cast<bfe_term
*>(t
);
1568 bfe_cum
cum(bfet
->factors
[k
], E_num(0, bfr
->d
), r
, bfr
, t
, k
, this);
1569 cum
.cumulate(options
);
1572 void bfenumerator::base(mat_ZZ
& factors
, bfc_vec
& v
)
1574 for (int i
= 0; i
< v
.size(); ++i
) {
1575 assert(v
[i
]->terms
.NumRows() == 1);
1576 evalue
*factor
= static_cast<bfe_term
*>(v
[i
])->factors
[0];
1577 eadd(factor
, vE
[vert
]);
1582 void bfenumerator::handle(const signed_cone
& sc
, barvinok_options
*options
)
1584 assert(sc
.det
== 1);
1586 assert(sc
.rays
.NumRows() == enumerator_base::dim
);
1588 bfe_term
* t
= new bfe_term(enumerator_base::dim
);
1589 vector
< bfc_term_base
* > v
;
1592 t
->factors
.resize(1);
1594 t
->terms
.SetDims(1, enumerator_base::dim
);
1595 lattice_point(V
, sc
.rays
, t
->terms
[0], E_vertex
, options
);
1597 // the elements of factors are always lexpositive
1599 int s
= setup_factors(sc
.rays
, factors
, t
, sc
.sign
);
1601 t
->factors
[0] = new evalue
;
1602 value_init(t
->factors
[0]->d
);
1603 evalue_set_si(t
->factors
[0], s
, 1);
1604 reduce(factors
, v
, options
);
1606 for (int i
= 0; i
< enumerator_base::dim
; ++i
)
1608 free_evalue_refs(E_vertex
[i
]);
1613 #ifdef HAVE_CORRECT_VERTICES
1614 static inline Param_Polyhedron
*Polyhedron2Param_SD(Polyhedron
**Din
,
1615 Polyhedron
*Cin
,int WS
,Polyhedron
**CEq
,Matrix
**CT
)
1617 if (WS
& POL_NO_DUAL
)
1619 return Polyhedron2Param_SimplifiedDomain(Din
, Cin
, WS
, CEq
, CT
);
1622 static Param_Polyhedron
*Polyhedron2Param_SD(Polyhedron
**Din
,
1623 Polyhedron
*Cin
,int WS
,Polyhedron
**CEq
,Matrix
**CT
)
1625 static char data
[] = " 1 0 0 0 0 1 -18 "
1626 " 1 0 0 -20 0 19 1 "
1627 " 1 0 1 20 0 -20 16 "
1630 " 1 4 -20 0 0 -1 23 "
1631 " 1 -4 20 0 0 1 -22 "
1632 " 1 0 1 0 20 -20 16 "
1633 " 1 0 0 0 -20 19 1 ";
1634 static int checked
= 0;
1639 Matrix
*M
= Matrix_Alloc(9, 7);
1640 for (i
= 0; i
< 9; ++i
)
1641 for (int j
= 0; j
< 7; ++j
) {
1642 sscanf(p
, "%d%n", &v
, &n
);
1644 value_set_si(M
->p
[i
][j
], v
);
1646 Polyhedron
*P
= Constraints2Polyhedron(M
, 1024);
1648 Polyhedron
*U
= Universe_Polyhedron(1);
1649 Param_Polyhedron
*PP
= Polyhedron2Param_Domain(P
, U
, 1024);
1653 for (i
= 0, V
= PP
->V
; V
; ++i
, V
= V
->next
)
1656 Param_Polyhedron_Free(PP
);
1658 fprintf(stderr
, "WARNING: results may be incorrect\n");
1660 "WARNING: use latest version of PolyLib to remove this warning\n");
1664 return Polyhedron2Param_SimplifiedDomain(Din
, Cin
, WS
, CEq
, CT
);
1668 static evalue
* barvinok_enumerate_ev_f(Polyhedron
*P
, Polyhedron
* C
,
1669 barvinok_options
*options
);
1672 static evalue
* barvinok_enumerate_cst(Polyhedron
*P
, Polyhedron
* C
,
1677 ALLOC(evalue
, eres
);
1678 value_init(eres
->d
);
1679 value_set_si(eres
->d
, 0);
1680 eres
->x
.p
= new_enode(partition
, 2, C
->Dimension
);
1681 EVALUE_SET_DOMAIN(eres
->x
.p
->arr
[0], DomainConstraintSimplify(C
, MaxRays
));
1682 value_set_si(eres
->x
.p
->arr
[1].d
, 1);
1683 value_init(eres
->x
.p
->arr
[1].x
.n
);
1685 value_set_si(eres
->x
.p
->arr
[1].x
.n
, 0);
1687 barvinok_count(P
, &eres
->x
.p
->arr
[1].x
.n
, MaxRays
);
1692 evalue
* barvinok_enumerate_with_options(Polyhedron
*P
, Polyhedron
* C
,
1693 struct barvinok_options
*options
)
1695 //P = unfringe(P, MaxRays);
1696 Polyhedron
*Corig
= C
;
1697 Polyhedron
*CEq
= NULL
, *rVD
, *CA
;
1699 unsigned nparam
= C
->Dimension
;
1703 value_init(factor
.d
);
1704 evalue_set_si(&factor
, 1, 1);
1706 CA
= align_context(C
, P
->Dimension
, options
->MaxRays
);
1707 P
= DomainIntersection(P
, CA
, options
->MaxRays
);
1708 Polyhedron_Free(CA
);
1711 POL_ENSURE_FACETS(P
);
1712 POL_ENSURE_VERTICES(P
);
1713 POL_ENSURE_FACETS(C
);
1714 POL_ENSURE_VERTICES(C
);
1716 if (C
->Dimension
== 0 || emptyQ(P
)) {
1718 eres
= barvinok_enumerate_cst(P
, CEq
? CEq
: Polyhedron_Copy(C
),
1721 emul(&factor
, eres
);
1722 reduce_evalue(eres
);
1723 free_evalue_refs(&factor
);
1730 if (Polyhedron_is_infinite_param(P
, nparam
))
1735 P
= remove_equalities_p(P
, P
->Dimension
-nparam
, &f
);
1736 mask(f
, &factor
, options
);
1739 if (P
->Dimension
== nparam
) {
1741 P
= Universe_Polyhedron(0);
1745 Polyhedron
*T
= Polyhedron_Factor(P
, nparam
, options
->MaxRays
);
1746 if (T
|| (P
->Dimension
== nparam
+1)) {
1749 for (Q
= T
? T
: P
; Q
; Q
= Q
->next
) {
1750 Polyhedron
*next
= Q
->next
;
1754 if (Q
->Dimension
!= C
->Dimension
)
1755 QC
= Polyhedron_Project(Q
, nparam
);
1758 C
= DomainIntersection(C
, QC
, options
->MaxRays
);
1760 Polyhedron_Free(C2
);
1762 Polyhedron_Free(QC
);
1770 if (T
->Dimension
== C
->Dimension
) {
1777 Polyhedron
*next
= P
->next
;
1779 eres
= barvinok_enumerate_ev_f(P
, C
, options
);
1786 for (Q
= P
->next
; Q
; Q
= Q
->next
) {
1787 Polyhedron
*next
= Q
->next
;
1790 f
= barvinok_enumerate_ev_f(Q
, C
, options
);
1792 free_evalue_refs(f
);
1802 evalue
* barvinok_enumerate_ev(Polyhedron
*P
, Polyhedron
* C
, unsigned MaxRays
)
1805 barvinok_options
*options
= barvinok_options_new_with_defaults();
1806 options
->MaxRays
= MaxRays
;
1807 E
= barvinok_enumerate_with_options(P
, C
, options
);
1808 barvinok_options_free(options
);
1812 static evalue
* barvinok_enumerate_ev_f(Polyhedron
*P
, Polyhedron
* C
,
1813 barvinok_options
*options
)
1815 unsigned nparam
= C
->Dimension
;
1817 if (P
->Dimension
- nparam
== 1)
1818 return ParamLine_Length(P
, C
, options
);
1820 Param_Polyhedron
*PP
= NULL
;
1821 Polyhedron
*CEq
= NULL
, *pVD
;
1823 Param_Domain
*D
, *next
;
1826 Polyhedron
*Porig
= P
;
1828 PP
= Polyhedron2Param_SD(&P
,C
,options
->MaxRays
,&CEq
,&CT
);
1830 if (isIdentity(CT
)) {
1834 assert(CT
->NbRows
!= CT
->NbColumns
);
1835 if (CT
->NbRows
== 1) { // no more parameters
1836 eres
= barvinok_enumerate_cst(P
, CEq
, options
->MaxRays
);
1841 Param_Polyhedron_Free(PP
);
1847 nparam
= CT
->NbRows
- 1;
1850 unsigned dim
= P
->Dimension
- nparam
;
1852 ALLOC(evalue
, eres
);
1853 value_init(eres
->d
);
1854 value_set_si(eres
->d
, 0);
1857 for (nd
= 0, D
=PP
->D
; D
; ++nd
, D
=D
->next
);
1858 struct section
{ Polyhedron
*D
; evalue E
; };
1859 section
*s
= new section
[nd
];
1860 Polyhedron
**fVD
= new Polyhedron_p
[nd
];
1862 enumerator_base
*et
= NULL
;
1867 et
= enumerator_base::create(P
, dim
, PP
->nbV
, options
);
1869 for(nd
= 0, D
=PP
->D
; D
; D
=next
) {
1872 Polyhedron
*rVD
= reduce_domain(D
->Domain
, CT
, CEq
,
1873 fVD
, nd
, options
->MaxRays
);
1877 pVD
= CT
? DomainImage(rVD
,CT
,options
->MaxRays
) : rVD
;
1879 value_init(s
[nd
].E
.d
);
1880 evalue_set_si(&s
[nd
].E
, 0, 1);
1883 FORALL_PVertex_in_ParamPolyhedron(V
,D
,PP
) // _i is internal counter
1886 et
->decompose_at(V
, _i
, options
);
1887 } catch (OrthogonalException
&e
) {
1890 for (; nd
>= 0; --nd
) {
1891 free_evalue_refs(&s
[nd
].E
);
1892 Domain_Free(s
[nd
].D
);
1893 Domain_Free(fVD
[nd
]);
1897 eadd(et
->vE
[_i
] , &s
[nd
].E
);
1898 END_FORALL_PVertex_in_ParamPolyhedron
;
1899 evalue_range_reduction_in_domain(&s
[nd
].E
, pVD
);
1902 addeliminatedparams_evalue(&s
[nd
].E
, CT
);
1910 evalue_set_si(eres
, 0, 1);
1912 eres
->x
.p
= new_enode(partition
, 2*nd
, C
->Dimension
);
1913 for (int j
= 0; j
< nd
; ++j
) {
1914 EVALUE_SET_DOMAIN(eres
->x
.p
->arr
[2*j
], s
[j
].D
);
1915 value_clear(eres
->x
.p
->arr
[2*j
+1].d
);
1916 eres
->x
.p
->arr
[2*j
+1] = s
[j
].E
;
1917 Domain_Free(fVD
[j
]);
1924 Polyhedron_Free(CEq
);
1928 Enumeration
* barvinok_enumerate(Polyhedron
*P
, Polyhedron
* C
, unsigned MaxRays
)
1930 evalue
*EP
= barvinok_enumerate_ev(P
, C
, MaxRays
);
1932 return partition2enumeration(EP
);
1935 static void SwapColumns(Value
**V
, int n
, int i
, int j
)
1937 for (int r
= 0; r
< n
; ++r
)
1938 value_swap(V
[r
][i
], V
[r
][j
]);
1941 static void SwapColumns(Polyhedron
*P
, int i
, int j
)
1943 SwapColumns(P
->Constraint
, P
->NbConstraints
, i
, j
);
1944 SwapColumns(P
->Ray
, P
->NbRays
, i
, j
);
1947 /* Construct a constraint c from constraints l and u such that if
1948 * if constraint c holds then for each value of the other variables
1949 * there is at most one value of variable pos (position pos+1 in the constraints).
1951 * Given a lower and an upper bound
1952 * n_l v_i + <c_l,x> + c_l >= 0
1953 * -n_u v_i + <c_u,x> + c_u >= 0
1954 * the constructed constraint is
1956 * -(n_l<c_u,x> + n_u<c_l,x>) + (-n_l c_u - n_u c_l + n_l n_u - 1)
1958 * which is then simplified to remove the content of the non-constant coefficients
1960 * len is the total length of the constraints.
1961 * v is a temporary variable that can be used by this procedure
1963 static void negative_test_constraint(Value
*l
, Value
*u
, Value
*c
, int pos
,
1966 value_oppose(*v
, u
[pos
+1]);
1967 Vector_Combine(l
+1, u
+1, c
+1, *v
, l
[pos
+1], len
-1);
1968 value_multiply(*v
, *v
, l
[pos
+1]);
1969 value_subtract(c
[len
-1], c
[len
-1], *v
);
1970 value_set_si(*v
, -1);
1971 Vector_Scale(c
+1, c
+1, *v
, len
-1);
1972 value_decrement(c
[len
-1], c
[len
-1]);
1973 ConstraintSimplify(c
, c
, len
, v
);
1976 static bool parallel_constraints(Value
*l
, Value
*u
, Value
*c
, int pos
,
1985 Vector_Gcd(&l
[1+pos
], len
, &g1
);
1986 Vector_Gcd(&u
[1+pos
], len
, &g2
);
1987 Vector_Combine(l
+1+pos
, u
+1+pos
, c
+1, g2
, g1
, len
);
1988 parallel
= First_Non_Zero(c
+1, len
) == -1;
1996 static void negative_test_constraint7(Value
*l
, Value
*u
, Value
*c
, int pos
,
1997 int exist
, int len
, Value
*v
)
2002 Vector_Gcd(&u
[1+pos
], exist
, v
);
2003 Vector_Gcd(&l
[1+pos
], exist
, &g
);
2004 Vector_Combine(l
+1, u
+1, c
+1, *v
, g
, len
-1);
2005 value_multiply(*v
, *v
, g
);
2006 value_subtract(c
[len
-1], c
[len
-1], *v
);
2007 value_set_si(*v
, -1);
2008 Vector_Scale(c
+1, c
+1, *v
, len
-1);
2009 value_decrement(c
[len
-1], c
[len
-1]);
2010 ConstraintSimplify(c
, c
, len
, v
);
2015 /* Turns a x + b >= 0 into a x + b <= -1
2017 * len is the total length of the constraint.
2018 * v is a temporary variable that can be used by this procedure
2020 static void oppose_constraint(Value
*c
, int len
, Value
*v
)
2022 value_set_si(*v
, -1);
2023 Vector_Scale(c
+1, c
+1, *v
, len
-1);
2024 value_decrement(c
[len
-1], c
[len
-1]);
2027 /* Split polyhedron P into two polyhedra *pos and *neg, where
2028 * existential variable i has at most one solution for each
2029 * value of the other variables in *neg.
2031 * The splitting is performed using constraints l and u.
2033 * nvar: number of set variables
2034 * row: temporary vector that can be used by this procedure
2035 * f: temporary value that can be used by this procedure
2037 static bool SplitOnConstraint(Polyhedron
*P
, int i
, int l
, int u
,
2038 int nvar
, int MaxRays
, Vector
*row
, Value
& f
,
2039 Polyhedron
**pos
, Polyhedron
**neg
)
2041 negative_test_constraint(P
->Constraint
[l
], P
->Constraint
[u
],
2042 row
->p
, nvar
+i
, P
->Dimension
+2, &f
);
2043 *neg
= AddConstraints(row
->p
, 1, P
, MaxRays
);
2045 /* We found an independent, but useless constraint
2046 * Maybe we should detect this earlier and not
2047 * mark the variable as INDEPENDENT
2049 if (emptyQ((*neg
))) {
2050 Polyhedron_Free(*neg
);
2054 oppose_constraint(row
->p
, P
->Dimension
+2, &f
);
2055 *pos
= AddConstraints(row
->p
, 1, P
, MaxRays
);
2057 if (emptyQ((*pos
))) {
2058 Polyhedron_Free(*neg
);
2059 Polyhedron_Free(*pos
);
2067 * unimodularly transform P such that constraint r is transformed
2068 * into a constraint that involves only a single (the first)
2069 * existential variable
2072 static Polyhedron
*rotate_along(Polyhedron
*P
, int r
, int nvar
, int exist
,
2078 Vector
*row
= Vector_Alloc(exist
);
2079 Vector_Copy(P
->Constraint
[r
]+1+nvar
, row
->p
, exist
);
2080 Vector_Gcd(row
->p
, exist
, &g
);
2081 if (value_notone_p(g
))
2082 Vector_AntiScale(row
->p
, row
->p
, g
, exist
);
2085 Matrix
*M
= unimodular_complete(row
);
2086 Matrix
*M2
= Matrix_Alloc(P
->Dimension
+1, P
->Dimension
+1);
2087 for (r
= 0; r
< nvar
; ++r
)
2088 value_set_si(M2
->p
[r
][r
], 1);
2089 for ( ; r
< nvar
+exist
; ++r
)
2090 Vector_Copy(M
->p
[r
-nvar
], M2
->p
[r
]+nvar
, exist
);
2091 for ( ; r
< P
->Dimension
+1; ++r
)
2092 value_set_si(M2
->p
[r
][r
], 1);
2093 Polyhedron
*T
= Polyhedron_Image(P
, M2
, MaxRays
);
2102 /* Split polyhedron P into two polyhedra *pos and *neg, where
2103 * existential variable i has at most one solution for each
2104 * value of the other variables in *neg.
2106 * If independent is set, then the two constraints on which the
2107 * split will be performed need to be independent of the other
2108 * existential variables.
2110 * Return true if an appropriate split could be performed.
2112 * nvar: number of set variables
2113 * exist: number of existential variables
2114 * row: temporary vector that can be used by this procedure
2115 * f: temporary value that can be used by this procedure
2117 static bool SplitOnVar(Polyhedron
*P
, int i
,
2118 int nvar
, int exist
, int MaxRays
,
2119 Vector
*row
, Value
& f
, bool independent
,
2120 Polyhedron
**pos
, Polyhedron
**neg
)
2124 for (int l
= P
->NbEq
; l
< P
->NbConstraints
; ++l
) {
2125 if (value_negz_p(P
->Constraint
[l
][nvar
+i
+1]))
2129 for (j
= 0; j
< exist
; ++j
)
2130 if (j
!= i
&& value_notzero_p(P
->Constraint
[l
][nvar
+j
+1]))
2136 for (int u
= P
->NbEq
; u
< P
->NbConstraints
; ++u
) {
2137 if (value_posz_p(P
->Constraint
[u
][nvar
+i
+1]))
2141 for (j
= 0; j
< exist
; ++j
)
2142 if (j
!= i
&& value_notzero_p(P
->Constraint
[u
][nvar
+j
+1]))
2148 if (SplitOnConstraint(P
, i
, l
, u
, nvar
, MaxRays
, row
, f
, pos
, neg
)) {
2151 SwapColumns(*neg
, nvar
+1, nvar
+1+i
);
2161 static bool double_bound_pair(Polyhedron
*P
, int nvar
, int exist
,
2162 int i
, int l1
, int l2
,
2163 Polyhedron
**pos
, Polyhedron
**neg
)
2167 Vector
*row
= Vector_Alloc(P
->Dimension
+2);
2168 value_set_si(row
->p
[0], 1);
2169 value_oppose(f
, P
->Constraint
[l1
][nvar
+i
+1]);
2170 Vector_Combine(P
->Constraint
[l1
]+1, P
->Constraint
[l2
]+1,
2172 P
->Constraint
[l2
][nvar
+i
+1], f
,
2174 ConstraintSimplify(row
->p
, row
->p
, P
->Dimension
+2, &f
);
2175 *pos
= AddConstraints(row
->p
, 1, P
, 0);
2176 value_set_si(f
, -1);
2177 Vector_Scale(row
->p
+1, row
->p
+1, f
, P
->Dimension
+1);
2178 value_decrement(row
->p
[P
->Dimension
+1], row
->p
[P
->Dimension
+1]);
2179 *neg
= AddConstraints(row
->p
, 1, P
, 0);
2183 return !emptyQ((*pos
)) && !emptyQ((*neg
));
2186 static bool double_bound(Polyhedron
*P
, int nvar
, int exist
,
2187 Polyhedron
**pos
, Polyhedron
**neg
)
2189 for (int i
= 0; i
< exist
; ++i
) {
2191 for (l1
= P
->NbEq
; l1
< P
->NbConstraints
; ++l1
) {
2192 if (value_negz_p(P
->Constraint
[l1
][nvar
+i
+1]))
2194 for (l2
= l1
+ 1; l2
< P
->NbConstraints
; ++l2
) {
2195 if (value_negz_p(P
->Constraint
[l2
][nvar
+i
+1]))
2197 if (double_bound_pair(P
, nvar
, exist
, i
, l1
, l2
, pos
, neg
))
2201 for (l1
= P
->NbEq
; l1
< P
->NbConstraints
; ++l1
) {
2202 if (value_posz_p(P
->Constraint
[l1
][nvar
+i
+1]))
2204 if (l1
< P
->NbConstraints
)
2205 for (l2
= l1
+ 1; l2
< P
->NbConstraints
; ++l2
) {
2206 if (value_posz_p(P
->Constraint
[l2
][nvar
+i
+1]))
2208 if (double_bound_pair(P
, nvar
, exist
, i
, l1
, l2
, pos
, neg
))
2220 INDEPENDENT
= 1 << 2,
2224 static evalue
* enumerate_or(Polyhedron
*D
,
2225 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2228 fprintf(stderr
, "\nER: Or\n");
2229 #endif /* DEBUG_ER */
2231 Polyhedron
*N
= D
->next
;
2234 barvinok_enumerate_e_with_options(D
, exist
, nparam
, options
);
2237 for (D
= N
; D
; D
= N
) {
2242 barvinok_enumerate_e_with_options(D
, exist
, nparam
, options
);
2245 free_evalue_refs(EN
);
2255 static evalue
* enumerate_sum(Polyhedron
*P
,
2256 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2258 int nvar
= P
->Dimension
- exist
- nparam
;
2259 int toswap
= nvar
< exist
? nvar
: exist
;
2260 for (int i
= 0; i
< toswap
; ++i
)
2261 SwapColumns(P
, 1 + i
, nvar
+exist
- i
);
2265 fprintf(stderr
, "\nER: Sum\n");
2266 #endif /* DEBUG_ER */
2268 evalue
*EP
= barvinok_enumerate_e_with_options(P
, exist
, nparam
, options
);
2270 for (int i
= 0; i
< /* nvar */ nparam
; ++i
) {
2271 Matrix
*C
= Matrix_Alloc(1, 1 + nparam
+ 1);
2272 value_set_si(C
->p
[0][0], 1);
2274 value_init(split
.d
);
2275 value_set_si(split
.d
, 0);
2276 split
.x
.p
= new_enode(partition
, 4, nparam
);
2277 value_set_si(C
->p
[0][1+i
], 1);
2278 Matrix
*C2
= Matrix_Copy(C
);
2279 EVALUE_SET_DOMAIN(split
.x
.p
->arr
[0],
2280 Constraints2Polyhedron(C2
, options
->MaxRays
));
2282 evalue_set_si(&split
.x
.p
->arr
[1], 1, 1);
2283 value_set_si(C
->p
[0][1+i
], -1);
2284 value_set_si(C
->p
[0][1+nparam
], -1);
2285 EVALUE_SET_DOMAIN(split
.x
.p
->arr
[2],
2286 Constraints2Polyhedron(C
, options
->MaxRays
));
2287 evalue_set_si(&split
.x
.p
->arr
[3], 1, 1);
2289 free_evalue_refs(&split
);
2293 evalue_range_reduction(EP
);
2295 evalue_frac2floor2(EP
, 1);
2297 evalue
*sum
= esum(EP
, nvar
);
2299 free_evalue_refs(EP
);
2303 evalue_range_reduction(EP
);
2308 static evalue
* split_sure(Polyhedron
*P
, Polyhedron
*S
,
2309 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2311 int nvar
= P
->Dimension
- exist
- nparam
;
2313 Matrix
*M
= Matrix_Alloc(exist
, S
->Dimension
+2);
2314 for (int i
= 0; i
< exist
; ++i
)
2315 value_set_si(M
->p
[i
][nvar
+i
+1], 1);
2317 S
= DomainAddRays(S
, M
, options
->MaxRays
);
2319 Polyhedron
*F
= DomainAddRays(P
, M
, options
->MaxRays
);
2320 Polyhedron
*D
= DomainDifference(F
, S
, options
->MaxRays
);
2322 D
= Disjoint_Domain(D
, 0, options
->MaxRays
);
2327 M
= Matrix_Alloc(P
->Dimension
+1-exist
, P
->Dimension
+1);
2328 for (int j
= 0; j
< nvar
; ++j
)
2329 value_set_si(M
->p
[j
][j
], 1);
2330 for (int j
= 0; j
< nparam
+1; ++j
)
2331 value_set_si(M
->p
[nvar
+j
][nvar
+exist
+j
], 1);
2332 Polyhedron
*T
= Polyhedron_Image(S
, M
, options
->MaxRays
);
2333 evalue
*EP
= barvinok_enumerate_e_with_options(T
, 0, nparam
, options
);
2338 for (Polyhedron
*Q
= D
; Q
; Q
= Q
->next
) {
2339 Polyhedron
*N
= Q
->next
;
2341 T
= DomainIntersection(P
, Q
, options
->MaxRays
);
2342 evalue
*E
= barvinok_enumerate_e_with_options(T
, exist
, nparam
, options
);
2344 free_evalue_refs(E
);
2353 static evalue
* enumerate_sure(Polyhedron
*P
,
2354 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2358 int nvar
= P
->Dimension
- exist
- nparam
;
2364 for (i
= 0; i
< exist
; ++i
) {
2365 Matrix
*M
= Matrix_Alloc(S
->NbConstraints
, S
->Dimension
+2);
2367 value_set_si(lcm
, 1);
2368 for (int j
= 0; j
< S
->NbConstraints
; ++j
) {
2369 if (value_negz_p(S
->Constraint
[j
][1+nvar
+i
]))
2371 if (value_one_p(S
->Constraint
[j
][1+nvar
+i
]))
2373 value_lcm(lcm
, S
->Constraint
[j
][1+nvar
+i
], &lcm
);
2376 for (int j
= 0; j
< S
->NbConstraints
; ++j
) {
2377 if (value_negz_p(S
->Constraint
[j
][1+nvar
+i
]))
2379 if (value_one_p(S
->Constraint
[j
][1+nvar
+i
]))
2381 value_division(f
, lcm
, S
->Constraint
[j
][1+nvar
+i
]);
2382 Vector_Scale(S
->Constraint
[j
], M
->p
[c
], f
, S
->Dimension
+2);
2383 value_subtract(M
->p
[c
][S
->Dimension
+1],
2384 M
->p
[c
][S
->Dimension
+1],
2386 value_increment(M
->p
[c
][S
->Dimension
+1],
2387 M
->p
[c
][S
->Dimension
+1]);
2391 S
= AddConstraints(M
->p
[0], c
, S
, options
->MaxRays
);
2406 fprintf(stderr
, "\nER: Sure\n");
2407 #endif /* DEBUG_ER */
2409 return split_sure(P
, S
, exist
, nparam
, options
);
2412 static evalue
* enumerate_sure2(Polyhedron
*P
,
2413 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2415 int nvar
= P
->Dimension
- exist
- nparam
;
2417 for (r
= 0; r
< P
->NbRays
; ++r
)
2418 if (value_one_p(P
->Ray
[r
][0]) &&
2419 value_one_p(P
->Ray
[r
][P
->Dimension
+1]))
2425 Matrix
*M
= Matrix_Alloc(nvar
+ 1 + nparam
, P
->Dimension
+2);
2426 for (int i
= 0; i
< nvar
; ++i
)
2427 value_set_si(M
->p
[i
][1+i
], 1);
2428 for (int i
= 0; i
< nparam
; ++i
)
2429 value_set_si(M
->p
[i
+nvar
][1+nvar
+exist
+i
], 1);
2430 Vector_Copy(P
->Ray
[r
]+1+nvar
, M
->p
[nvar
+nparam
]+1+nvar
, exist
);
2431 value_set_si(M
->p
[nvar
+nparam
][0], 1);
2432 value_set_si(M
->p
[nvar
+nparam
][P
->Dimension
+1], 1);
2433 Polyhedron
* F
= Rays2Polyhedron(M
, options
->MaxRays
);
2436 Polyhedron
*I
= DomainIntersection(F
, P
, options
->MaxRays
);
2440 fprintf(stderr
, "\nER: Sure2\n");
2441 #endif /* DEBUG_ER */
2443 return split_sure(P
, I
, exist
, nparam
, options
);
2446 static evalue
* enumerate_cyclic(Polyhedron
*P
,
2447 unsigned exist
, unsigned nparam
,
2448 evalue
* EP
, int r
, int p
, unsigned MaxRays
)
2450 int nvar
= P
->Dimension
- exist
- nparam
;
2452 /* If EP in its fractional maps only contains references
2453 * to the remainder parameter with appropriate coefficients
2454 * then we could in principle avoid adding existentially
2455 * quantified variables to the validity domains.
2456 * We'd have to replace the remainder by m { p/m }
2457 * and multiply with an appropriate factor that is one
2458 * only in the appropriate range.
2459 * This last multiplication can be avoided if EP
2460 * has a single validity domain with no (further)
2461 * constraints on the remainder parameter
2464 Matrix
*CT
= Matrix_Alloc(nparam
+1, nparam
+3);
2465 Matrix
*M
= Matrix_Alloc(1, 1+nparam
+3);
2466 for (int j
= 0; j
< nparam
; ++j
)
2468 value_set_si(CT
->p
[j
][j
], 1);
2469 value_set_si(CT
->p
[p
][nparam
+1], 1);
2470 value_set_si(CT
->p
[nparam
][nparam
+2], 1);
2471 value_set_si(M
->p
[0][1+p
], -1);
2472 value_absolute(M
->p
[0][1+nparam
], P
->Ray
[0][1+nvar
+exist
+p
]);
2473 value_set_si(M
->p
[0][1+nparam
+1], 1);
2474 Polyhedron
*CEq
= Constraints2Polyhedron(M
, 1);
2476 addeliminatedparams_enum(EP
, CT
, CEq
, MaxRays
, nparam
);
2477 Polyhedron_Free(CEq
);
2483 static void enumerate_vd_add_ray(evalue
*EP
, Matrix
*Rays
, unsigned MaxRays
)
2485 if (value_notzero_p(EP
->d
))
2488 assert(EP
->x
.p
->type
== partition
);
2489 assert(EP
->x
.p
->pos
== EVALUE_DOMAIN(EP
->x
.p
->arr
[0])->Dimension
);
2490 for (int i
= 0; i
< EP
->x
.p
->size
/2; ++i
) {
2491 Polyhedron
*D
= EVALUE_DOMAIN(EP
->x
.p
->arr
[2*i
]);
2492 Polyhedron
*N
= DomainAddRays(D
, Rays
, MaxRays
);
2493 EVALUE_SET_DOMAIN(EP
->x
.p
->arr
[2*i
], N
);
2498 static evalue
* enumerate_line(Polyhedron
*P
,
2499 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2505 fprintf(stderr
, "\nER: Line\n");
2506 #endif /* DEBUG_ER */
2508 int nvar
= P
->Dimension
- exist
- nparam
;
2510 for (i
= 0; i
< nparam
; ++i
)
2511 if (value_notzero_p(P
->Ray
[0][1+nvar
+exist
+i
]))
2514 for (j
= i
+1; j
< nparam
; ++j
)
2515 if (value_notzero_p(P
->Ray
[0][1+nvar
+exist
+i
]))
2517 assert(j
>= nparam
); // for now
2519 Matrix
*M
= Matrix_Alloc(2, P
->Dimension
+2);
2520 value_set_si(M
->p
[0][0], 1);
2521 value_set_si(M
->p
[0][1+nvar
+exist
+i
], 1);
2522 value_set_si(M
->p
[1][0], 1);
2523 value_set_si(M
->p
[1][1+nvar
+exist
+i
], -1);
2524 value_absolute(M
->p
[1][1+P
->Dimension
], P
->Ray
[0][1+nvar
+exist
+i
]);
2525 value_decrement(M
->p
[1][1+P
->Dimension
], M
->p
[1][1+P
->Dimension
]);
2526 Polyhedron
*S
= AddConstraints(M
->p
[0], 2, P
, options
->MaxRays
);
2527 evalue
*EP
= barvinok_enumerate_e_with_options(S
, exist
, nparam
, options
);
2531 return enumerate_cyclic(P
, exist
, nparam
, EP
, 0, i
, options
->MaxRays
);
2534 static int single_param_pos(Polyhedron
*P
, unsigned exist
, unsigned nparam
,
2537 int nvar
= P
->Dimension
- exist
- nparam
;
2538 if (First_Non_Zero(P
->Ray
[r
]+1, nvar
) != -1)
2540 int i
= First_Non_Zero(P
->Ray
[r
]+1+nvar
+exist
, nparam
);
2543 if (First_Non_Zero(P
->Ray
[r
]+1+nvar
+exist
+1, nparam
-i
-1) != -1)
2548 static evalue
* enumerate_remove_ray(Polyhedron
*P
, int r
,
2549 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2552 fprintf(stderr
, "\nER: RedundantRay\n");
2553 #endif /* DEBUG_ER */
2557 value_set_si(one
, 1);
2558 int len
= P
->NbRays
-1;
2559 Matrix
*M
= Matrix_Alloc(2 * len
, P
->Dimension
+2);
2560 Vector_Copy(P
->Ray
[0], M
->p
[0], r
* (P
->Dimension
+2));
2561 Vector_Copy(P
->Ray
[r
+1], M
->p
[r
], (len
-r
) * (P
->Dimension
+2));
2562 for (int j
= 0; j
< P
->NbRays
; ++j
) {
2565 Vector_Combine(P
->Ray
[j
], P
->Ray
[r
], M
->p
[len
+j
-(j
>r
)],
2566 one
, P
->Ray
[j
][P
->Dimension
+1], P
->Dimension
+2);
2569 P
= Rays2Polyhedron(M
, options
->MaxRays
);
2571 evalue
*EP
= barvinok_enumerate_e_with_options(P
, exist
, nparam
, options
);
2578 static evalue
* enumerate_redundant_ray(Polyhedron
*P
,
2579 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2581 assert(P
->NbBid
== 0);
2582 int nvar
= P
->Dimension
- exist
- nparam
;
2586 for (int r
= 0; r
< P
->NbRays
; ++r
) {
2587 if (value_notzero_p(P
->Ray
[r
][P
->Dimension
+1]))
2589 int i1
= single_param_pos(P
, exist
, nparam
, r
);
2592 for (int r2
= r
+1; r2
< P
->NbRays
; ++r2
) {
2593 if (value_notzero_p(P
->Ray
[r2
][P
->Dimension
+1]))
2595 int i2
= single_param_pos(P
, exist
, nparam
, r2
);
2601 value_division(m
, P
->Ray
[r
][1+nvar
+exist
+i1
],
2602 P
->Ray
[r2
][1+nvar
+exist
+i1
]);
2603 value_multiply(m
, m
, P
->Ray
[r2
][1+nvar
+exist
+i1
]);
2604 /* r2 divides r => r redundant */
2605 if (value_eq(m
, P
->Ray
[r
][1+nvar
+exist
+i1
])) {
2607 return enumerate_remove_ray(P
, r
, exist
, nparam
, options
);
2610 value_division(m
, P
->Ray
[r2
][1+nvar
+exist
+i1
],
2611 P
->Ray
[r
][1+nvar
+exist
+i1
]);
2612 value_multiply(m
, m
, P
->Ray
[r
][1+nvar
+exist
+i1
]);
2613 /* r divides r2 => r2 redundant */
2614 if (value_eq(m
, P
->Ray
[r2
][1+nvar
+exist
+i1
])) {
2616 return enumerate_remove_ray(P
, r2
, exist
, nparam
, options
);
2624 static Polyhedron
*upper_bound(Polyhedron
*P
,
2625 int pos
, Value
*max
, Polyhedron
**R
)
2634 for (Polyhedron
*Q
= P
; Q
; Q
= N
) {
2636 for (r
= 0; r
< P
->NbRays
; ++r
) {
2637 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1]) &&
2638 value_pos_p(P
->Ray
[r
][1+pos
]))
2641 if (r
< P
->NbRays
) {
2649 for (r
= 0; r
< P
->NbRays
; ++r
) {
2650 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1]))
2652 mpz_fdiv_q(v
, P
->Ray
[r
][1+pos
], P
->Ray
[r
][1+P
->Dimension
]);
2653 if ((!Q
->next
&& r
== 0) || value_gt(v
, *max
))
2654 value_assign(*max
, v
);
2661 static evalue
* enumerate_ray(Polyhedron
*P
,
2662 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2664 assert(P
->NbBid
== 0);
2665 int nvar
= P
->Dimension
- exist
- nparam
;
2668 for (r
= 0; r
< P
->NbRays
; ++r
)
2669 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1]))
2675 for (r2
= r
+1; r2
< P
->NbRays
; ++r2
)
2676 if (value_zero_p(P
->Ray
[r2
][P
->Dimension
+1]))
2678 if (r2
< P
->NbRays
) {
2680 return enumerate_sum(P
, exist
, nparam
, options
);
2684 fprintf(stderr
, "\nER: Ray\n");
2685 #endif /* DEBUG_ER */
2691 value_set_si(one
, 1);
2692 int i
= single_param_pos(P
, exist
, nparam
, r
);
2693 assert(i
!= -1); // for now;
2695 Matrix
*M
= Matrix_Alloc(P
->NbRays
, P
->Dimension
+2);
2696 for (int j
= 0; j
< P
->NbRays
; ++j
) {
2697 Vector_Combine(P
->Ray
[j
], P
->Ray
[r
], M
->p
[j
],
2698 one
, P
->Ray
[j
][P
->Dimension
+1], P
->Dimension
+2);
2700 Polyhedron
*S
= Rays2Polyhedron(M
, options
->MaxRays
);
2702 Polyhedron
*D
= DomainDifference(P
, S
, options
->MaxRays
);
2704 // Polyhedron_Print(stderr, P_VALUE_FMT, D);
2705 assert(value_pos_p(P
->Ray
[r
][1+nvar
+exist
+i
])); // for now
2707 D
= upper_bound(D
, nvar
+exist
+i
, &m
, &R
);
2711 M
= Matrix_Alloc(2, P
->Dimension
+2);
2712 value_set_si(M
->p
[0][0], 1);
2713 value_set_si(M
->p
[1][0], 1);
2714 value_set_si(M
->p
[0][1+nvar
+exist
+i
], -1);
2715 value_set_si(M
->p
[1][1+nvar
+exist
+i
], 1);
2716 value_assign(M
->p
[0][1+P
->Dimension
], m
);
2717 value_oppose(M
->p
[1][1+P
->Dimension
], m
);
2718 value_addto(M
->p
[1][1+P
->Dimension
], M
->p
[1][1+P
->Dimension
],
2719 P
->Ray
[r
][1+nvar
+exist
+i
]);
2720 value_decrement(M
->p
[1][1+P
->Dimension
], M
->p
[1][1+P
->Dimension
]);
2721 // Matrix_Print(stderr, P_VALUE_FMT, M);
2722 D
= AddConstraints(M
->p
[0], 2, P
, options
->MaxRays
);
2723 // Polyhedron_Print(stderr, P_VALUE_FMT, D);
2724 value_subtract(M
->p
[0][1+P
->Dimension
], M
->p
[0][1+P
->Dimension
],
2725 P
->Ray
[r
][1+nvar
+exist
+i
]);
2726 // Matrix_Print(stderr, P_VALUE_FMT, M);
2727 S
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
2728 // Polyhedron_Print(stderr, P_VALUE_FMT, S);
2731 evalue
*EP
= barvinok_enumerate_e_with_options(D
, exist
, nparam
, options
);
2736 if (value_notone_p(P
->Ray
[r
][1+nvar
+exist
+i
]))
2737 EP
= enumerate_cyclic(P
, exist
, nparam
, EP
, r
, i
, options
->MaxRays
);
2739 M
= Matrix_Alloc(1, nparam
+2);
2740 value_set_si(M
->p
[0][0], 1);
2741 value_set_si(M
->p
[0][1+i
], 1);
2742 enumerate_vd_add_ray(EP
, M
, options
->MaxRays
);
2747 evalue
*E
= barvinok_enumerate_e_with_options(S
, exist
, nparam
, options
);
2749 free_evalue_refs(E
);
2756 evalue
*ER
= enumerate_or(R
, exist
, nparam
, options
);
2758 free_evalue_refs(ER
);
2765 static evalue
* enumerate_vd(Polyhedron
**PA
,
2766 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2768 Polyhedron
*P
= *PA
;
2769 int nvar
= P
->Dimension
- exist
- nparam
;
2770 Param_Polyhedron
*PP
= NULL
;
2771 Polyhedron
*C
= Universe_Polyhedron(nparam
);
2775 PP
= Polyhedron2Param_SimplifiedDomain(&PR
,C
, options
->MaxRays
,&CEq
,&CT
);
2779 Param_Domain
*D
, *last
;
2782 for (nd
= 0, D
=PP
->D
; D
; D
=D
->next
, ++nd
)
2785 Polyhedron
**VD
= new Polyhedron_p
[nd
];
2786 Polyhedron
**fVD
= new Polyhedron_p
[nd
];
2787 for(nd
= 0, D
=PP
->D
; D
; D
=D
->next
) {
2788 Polyhedron
*rVD
= reduce_domain(D
->Domain
, CT
, CEq
,
2789 fVD
, nd
, options
->MaxRays
);
2802 /* This doesn't seem to have any effect */
2804 Polyhedron
*CA
= align_context(VD
[0], P
->Dimension
, options
->MaxRays
);
2806 P
= DomainIntersection(P
, CA
, options
->MaxRays
);
2809 Polyhedron_Free(CA
);
2814 if (!EP
&& CT
->NbColumns
!= CT
->NbRows
) {
2815 Polyhedron
*CEqr
= DomainImage(CEq
, CT
, options
->MaxRays
);
2816 Polyhedron
*CA
= align_context(CEqr
, PR
->Dimension
, options
->MaxRays
);
2817 Polyhedron
*I
= DomainIntersection(PR
, CA
, options
->MaxRays
);
2818 Polyhedron_Free(CEqr
);
2819 Polyhedron_Free(CA
);
2821 fprintf(stderr
, "\nER: Eliminate\n");
2822 #endif /* DEBUG_ER */
2823 nparam
-= CT
->NbColumns
- CT
->NbRows
;
2824 EP
= barvinok_enumerate_e_with_options(I
, exist
, nparam
, options
);
2825 nparam
+= CT
->NbColumns
- CT
->NbRows
;
2826 addeliminatedparams_enum(EP
, CT
, CEq
, options
->MaxRays
, nparam
);
2830 Polyhedron_Free(PR
);
2833 if (!EP
&& nd
> 1) {
2835 fprintf(stderr
, "\nER: VD\n");
2836 #endif /* DEBUG_ER */
2837 for (int i
= 0; i
< nd
; ++i
) {
2838 Polyhedron
*CA
= align_context(VD
[i
], P
->Dimension
, options
->MaxRays
);
2839 Polyhedron
*I
= DomainIntersection(P
, CA
, options
->MaxRays
);
2842 EP
= barvinok_enumerate_e_with_options(I
, exist
, nparam
, options
);
2844 evalue
*E
= barvinok_enumerate_e_with_options(I
, exist
, nparam
,
2847 free_evalue_refs(E
);
2851 Polyhedron_Free(CA
);
2855 for (int i
= 0; i
< nd
; ++i
) {
2856 Polyhedron_Free(VD
[i
]);
2857 Polyhedron_Free(fVD
[i
]);
2863 if (!EP
&& nvar
== 0) {
2866 Param_Vertices
*V
, *V2
;
2867 Matrix
* M
= Matrix_Alloc(1, P
->Dimension
+2);
2869 FORALL_PVertex_in_ParamPolyhedron(V
, last
, PP
) {
2871 FORALL_PVertex_in_ParamPolyhedron(V2
, last
, PP
) {
2878 for (int i
= 0; i
< exist
; ++i
) {
2879 value_oppose(f
, V
->Vertex
->p
[i
][nparam
+1]);
2880 Vector_Combine(V
->Vertex
->p
[i
],
2882 M
->p
[0] + 1 + nvar
+ exist
,
2883 V2
->Vertex
->p
[i
][nparam
+1],
2887 for (j
= 0; j
< nparam
; ++j
)
2888 if (value_notzero_p(M
->p
[0][1+nvar
+exist
+j
]))
2892 ConstraintSimplify(M
->p
[0], M
->p
[0],
2893 P
->Dimension
+2, &f
);
2894 value_set_si(M
->p
[0][0], 0);
2895 Polyhedron
*para
= AddConstraints(M
->p
[0], 1, P
,
2898 Polyhedron_Free(para
);
2901 Polyhedron
*pos
, *neg
;
2902 value_set_si(M
->p
[0][0], 1);
2903 value_decrement(M
->p
[0][P
->Dimension
+1],
2904 M
->p
[0][P
->Dimension
+1]);
2905 neg
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
2906 value_set_si(f
, -1);
2907 Vector_Scale(M
->p
[0]+1, M
->p
[0]+1, f
,
2909 value_decrement(M
->p
[0][P
->Dimension
+1],
2910 M
->p
[0][P
->Dimension
+1]);
2911 value_decrement(M
->p
[0][P
->Dimension
+1],
2912 M
->p
[0][P
->Dimension
+1]);
2913 pos
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
2914 if (emptyQ(neg
) && emptyQ(pos
)) {
2915 Polyhedron_Free(para
);
2916 Polyhedron_Free(pos
);
2917 Polyhedron_Free(neg
);
2921 fprintf(stderr
, "\nER: Order\n");
2922 #endif /* DEBUG_ER */
2923 EP
= barvinok_enumerate_e_with_options(para
, exist
, nparam
,
2927 E
= barvinok_enumerate_e_with_options(pos
, exist
, nparam
,
2930 free_evalue_refs(E
);
2934 E
= barvinok_enumerate_e_with_options(neg
, exist
, nparam
,
2937 free_evalue_refs(E
);
2940 Polyhedron_Free(para
);
2941 Polyhedron_Free(pos
);
2942 Polyhedron_Free(neg
);
2947 } END_FORALL_PVertex_in_ParamPolyhedron
;
2950 } END_FORALL_PVertex_in_ParamPolyhedron
;
2953 /* Search for vertex coordinate to split on */
2954 /* First look for one independent of the parameters */
2955 FORALL_PVertex_in_ParamPolyhedron(V
, last
, PP
) {
2956 for (int i
= 0; i
< exist
; ++i
) {
2958 for (j
= 0; j
< nparam
; ++j
)
2959 if (value_notzero_p(V
->Vertex
->p
[i
][j
]))
2963 value_set_si(M
->p
[0][0], 1);
2964 Vector_Set(M
->p
[0]+1, 0, nvar
+exist
);
2965 Vector_Copy(V
->Vertex
->p
[i
],
2966 M
->p
[0] + 1 + nvar
+ exist
, nparam
+1);
2967 value_oppose(M
->p
[0][1+nvar
+i
],
2968 V
->Vertex
->p
[i
][nparam
+1]);
2970 Polyhedron
*pos
, *neg
;
2971 value_set_si(M
->p
[0][0], 1);
2972 value_decrement(M
->p
[0][P
->Dimension
+1],
2973 M
->p
[0][P
->Dimension
+1]);
2974 neg
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
2975 value_set_si(f
, -1);
2976 Vector_Scale(M
->p
[0]+1, M
->p
[0]+1, f
,
2978 value_decrement(M
->p
[0][P
->Dimension
+1],
2979 M
->p
[0][P
->Dimension
+1]);
2980 value_decrement(M
->p
[0][P
->Dimension
+1],
2981 M
->p
[0][P
->Dimension
+1]);
2982 pos
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
2983 if (emptyQ(neg
) || emptyQ(pos
)) {
2984 Polyhedron_Free(pos
);
2985 Polyhedron_Free(neg
);
2988 Polyhedron_Free(pos
);
2989 value_increment(M
->p
[0][P
->Dimension
+1],
2990 M
->p
[0][P
->Dimension
+1]);
2991 pos
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
2993 fprintf(stderr
, "\nER: Vertex\n");
2994 #endif /* DEBUG_ER */
2996 EP
= enumerate_or(pos
, exist
, nparam
, options
);
3001 } END_FORALL_PVertex_in_ParamPolyhedron
;
3005 /* Search for vertex coordinate to split on */
3006 /* Now look for one that depends on the parameters */
3007 FORALL_PVertex_in_ParamPolyhedron(V
, last
, PP
) {
3008 for (int i
= 0; i
< exist
; ++i
) {
3009 value_set_si(M
->p
[0][0], 1);
3010 Vector_Set(M
->p
[0]+1, 0, nvar
+exist
);
3011 Vector_Copy(V
->Vertex
->p
[i
],
3012 M
->p
[0] + 1 + nvar
+ exist
, nparam
+1);
3013 value_oppose(M
->p
[0][1+nvar
+i
],
3014 V
->Vertex
->p
[i
][nparam
+1]);
3016 Polyhedron
*pos
, *neg
;
3017 value_set_si(M
->p
[0][0], 1);
3018 value_decrement(M
->p
[0][P
->Dimension
+1],
3019 M
->p
[0][P
->Dimension
+1]);
3020 neg
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
3021 value_set_si(f
, -1);
3022 Vector_Scale(M
->p
[0]+1, M
->p
[0]+1, f
,
3024 value_decrement(M
->p
[0][P
->Dimension
+1],
3025 M
->p
[0][P
->Dimension
+1]);
3026 value_decrement(M
->p
[0][P
->Dimension
+1],
3027 M
->p
[0][P
->Dimension
+1]);
3028 pos
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
3029 if (emptyQ(neg
) || emptyQ(pos
)) {
3030 Polyhedron_Free(pos
);
3031 Polyhedron_Free(neg
);
3034 Polyhedron_Free(pos
);
3035 value_increment(M
->p
[0][P
->Dimension
+1],
3036 M
->p
[0][P
->Dimension
+1]);
3037 pos
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
3039 fprintf(stderr
, "\nER: ParamVertex\n");
3040 #endif /* DEBUG_ER */
3042 EP
= enumerate_or(pos
, exist
, nparam
, options
);
3047 } END_FORALL_PVertex_in_ParamPolyhedron
;
3055 Polyhedron_Free(CEq
);
3059 Param_Polyhedron_Free(PP
);
3065 evalue
* barvinok_enumerate_pip(Polyhedron
*P
, unsigned exist
, unsigned nparam
,
3069 barvinok_options
*options
= barvinok_options_new_with_defaults();
3070 options
->MaxRays
= MaxRays
;
3071 E
= barvinok_enumerate_pip_with_options(P
, exist
, nparam
, options
);
3072 barvinok_options_free(options
);
3077 evalue
*barvinok_enumerate_pip_with_options(Polyhedron
*P
,
3078 unsigned exist
, unsigned nparam
, struct barvinok_options
*options
)
3083 evalue
*barvinok_enumerate_pip_with_options(Polyhedron
*P
,
3084 unsigned exist
, unsigned nparam
, struct barvinok_options
*options
)
3086 int nvar
= P
->Dimension
- exist
- nparam
;
3087 evalue
*EP
= evalue_zero();
3091 fprintf(stderr
, "\nER: PIP\n");
3092 #endif /* DEBUG_ER */
3094 Polyhedron
*D
= pip_projectout(P
, nvar
, exist
, nparam
);
3095 for (Q
= D
; Q
; Q
= N
) {
3099 exist
= Q
->Dimension
- nvar
- nparam
;
3100 E
= barvinok_enumerate_e_with_options(Q
, exist
, nparam
, options
);
3103 free_evalue_refs(E
);
3112 static bool is_single(Value
*row
, int pos
, int len
)
3114 return First_Non_Zero(row
, pos
) == -1 &&
3115 First_Non_Zero(row
+pos
+1, len
-pos
-1) == -1;
3118 static evalue
* barvinok_enumerate_e_r(Polyhedron
*P
,
3119 unsigned exist
, unsigned nparam
, barvinok_options
*options
);
3122 static int er_level
= 0;
3124 evalue
* barvinok_enumerate_e_with_options(Polyhedron
*P
,
3125 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
3127 fprintf(stderr
, "\nER: level %i\n", er_level
);
3129 Polyhedron_PrintConstraints(stderr
, P_VALUE_FMT
, P
);
3130 fprintf(stderr
, "\nE %d\nP %d\n", exist
, nparam
);
3132 P
= DomainConstraintSimplify(Polyhedron_Copy(P
), options
->MaxRays
);
3133 evalue
*EP
= barvinok_enumerate_e_r(P
, exist
, nparam
, options
);
3139 evalue
* barvinok_enumerate_e_with_options(Polyhedron
*P
,
3140 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
3142 P
= DomainConstraintSimplify(Polyhedron_Copy(P
), options
->MaxRays
);
3143 evalue
*EP
= barvinok_enumerate_e_r(P
, exist
, nparam
, options
);
3149 evalue
* barvinok_enumerate_e(Polyhedron
*P
, unsigned exist
, unsigned nparam
,
3153 barvinok_options
*options
= barvinok_options_new_with_defaults();
3154 options
->MaxRays
= MaxRays
;
3155 E
= barvinok_enumerate_e_with_options(P
, exist
, nparam
, options
);
3156 barvinok_options_free(options
);
3160 static evalue
* barvinok_enumerate_e_r(Polyhedron
*P
,
3161 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
3164 Polyhedron
*U
= Universe_Polyhedron(nparam
);
3165 evalue
*EP
= barvinok_enumerate_with_options(P
, U
, options
);
3166 //char *param_name[] = {"P", "Q", "R", "S", "T" };
3167 //print_evalue(stdout, EP, param_name);
3172 int nvar
= P
->Dimension
- exist
- nparam
;
3173 int len
= P
->Dimension
+ 2;
3176 POL_ENSURE_FACETS(P
);
3177 POL_ENSURE_VERTICES(P
);
3180 return evalue_zero();
3182 if (nvar
== 0 && nparam
== 0) {
3183 evalue
*EP
= evalue_zero();
3184 barvinok_count_with_options(P
, &EP
->x
.n
, options
);
3185 if (value_pos_p(EP
->x
.n
))
3186 value_set_si(EP
->x
.n
, 1);
3191 for (r
= 0; r
< P
->NbRays
; ++r
)
3192 if (value_zero_p(P
->Ray
[r
][0]) ||
3193 value_zero_p(P
->Ray
[r
][P
->Dimension
+1])) {
3195 for (i
= 0; i
< nvar
; ++i
)
3196 if (value_notzero_p(P
->Ray
[r
][i
+1]))
3200 for (i
= nvar
+ exist
; i
< nvar
+ exist
+ nparam
; ++i
)
3201 if (value_notzero_p(P
->Ray
[r
][i
+1]))
3203 if (i
>= nvar
+ exist
+ nparam
)
3206 if (r
< P
->NbRays
) {
3207 evalue
*EP
= evalue_zero();
3208 value_set_si(EP
->x
.n
, -1);
3213 for (r
= 0; r
< P
->NbEq
; ++r
)
3214 if ((first
= First_Non_Zero(P
->Constraint
[r
]+1+nvar
, exist
)) != -1)
3217 if (First_Non_Zero(P
->Constraint
[r
]+1+nvar
+first
+1,
3218 exist
-first
-1) != -1) {
3219 Polyhedron
*T
= rotate_along(P
, r
, nvar
, exist
, options
->MaxRays
);
3221 fprintf(stderr
, "\nER: Equality\n");
3222 #endif /* DEBUG_ER */
3223 evalue
*EP
= barvinok_enumerate_e_with_options(T
, exist
-1, nparam
,
3229 fprintf(stderr
, "\nER: Fixed\n");
3230 #endif /* DEBUG_ER */
3232 return barvinok_enumerate_e_with_options(P
, exist
-1, nparam
,
3235 Polyhedron
*T
= Polyhedron_Copy(P
);
3236 SwapColumns(T
, nvar
+1, nvar
+1+first
);
3237 evalue
*EP
= barvinok_enumerate_e_with_options(T
, exist
-1, nparam
,
3245 Vector
*row
= Vector_Alloc(len
);
3246 value_set_si(row
->p
[0], 1);
3251 enum constraint
* info
= new constraint
[exist
];
3252 for (int i
= 0; i
< exist
; ++i
) {
3254 for (int l
= P
->NbEq
; l
< P
->NbConstraints
; ++l
) {
3255 if (value_negz_p(P
->Constraint
[l
][nvar
+i
+1]))
3257 bool l_parallel
= is_single(P
->Constraint
[l
]+nvar
+1, i
, exist
);
3258 for (int u
= P
->NbEq
; u
< P
->NbConstraints
; ++u
) {
3259 if (value_posz_p(P
->Constraint
[u
][nvar
+i
+1]))
3261 bool lu_parallel
= l_parallel
||
3262 is_single(P
->Constraint
[u
]+nvar
+1, i
, exist
);
3263 value_oppose(f
, P
->Constraint
[u
][nvar
+i
+1]);
3264 Vector_Combine(P
->Constraint
[l
]+1, P
->Constraint
[u
]+1, row
->p
+1,
3265 f
, P
->Constraint
[l
][nvar
+i
+1], len
-1);
3266 if (!(info
[i
] & INDEPENDENT
)) {
3268 for (j
= 0; j
< exist
; ++j
)
3269 if (j
!= i
&& value_notzero_p(row
->p
[nvar
+j
+1]))
3272 //printf("independent: i: %d, l: %d, u: %d\n", i, l, u);
3273 info
[i
] = (constraint
)(info
[i
] | INDEPENDENT
);
3276 if (info
[i
] & ALL_POS
) {
3277 value_addto(row
->p
[len
-1], row
->p
[len
-1],
3278 P
->Constraint
[l
][nvar
+i
+1]);
3279 value_addto(row
->p
[len
-1], row
->p
[len
-1], f
);
3280 value_multiply(f
, f
, P
->Constraint
[l
][nvar
+i
+1]);
3281 value_subtract(row
->p
[len
-1], row
->p
[len
-1], f
);
3282 value_decrement(row
->p
[len
-1], row
->p
[len
-1]);
3283 ConstraintSimplify(row
->p
, row
->p
, len
, &f
);
3284 value_set_si(f
, -1);
3285 Vector_Scale(row
->p
+1, row
->p
+1, f
, len
-1);
3286 value_decrement(row
->p
[len
-1], row
->p
[len
-1]);
3287 Polyhedron
*T
= AddConstraints(row
->p
, 1, P
, options
->MaxRays
);
3289 //printf("not all_pos: i: %d, l: %d, u: %d\n", i, l, u);
3290 info
[i
] = (constraint
)(info
[i
] ^ ALL_POS
);
3292 //puts("pos remainder");
3293 //Polyhedron_Print(stdout, P_VALUE_FMT, T);
3296 if (!(info
[i
] & ONE_NEG
)) {
3298 negative_test_constraint(P
->Constraint
[l
],
3300 row
->p
, nvar
+i
, len
, &f
);
3301 oppose_constraint(row
->p
, len
, &f
);
3302 Polyhedron
*T
= AddConstraints(row
->p
, 1, P
,
3305 //printf("one_neg i: %d, l: %d, u: %d\n", i, l, u);
3306 info
[i
] = (constraint
)(info
[i
] | ONE_NEG
);
3308 //puts("neg remainder");
3309 //Polyhedron_Print(stdout, P_VALUE_FMT, T);
3311 } else if (!(info
[i
] & ROT_NEG
)) {
3312 if (parallel_constraints(P
->Constraint
[l
],
3314 row
->p
, nvar
, exist
)) {
3315 negative_test_constraint7(P
->Constraint
[l
],
3317 row
->p
, nvar
, exist
,
3319 oppose_constraint(row
->p
, len
, &f
);
3320 Polyhedron
*T
= AddConstraints(row
->p
, 1, P
,
3323 // printf("rot_neg i: %d, l: %d, u: %d\n", i, l, u);
3324 info
[i
] = (constraint
)(info
[i
] | ROT_NEG
);
3327 //puts("neg remainder");
3328 //Polyhedron_Print(stdout, P_VALUE_FMT, T);
3333 if (!(info
[i
] & ALL_POS
) && (info
[i
] & (ONE_NEG
| ROT_NEG
)))
3337 if (info
[i
] & ALL_POS
)
3344 for (int i = 0; i < exist; ++i)
3345 printf("%i: %i\n", i, info[i]);
3347 for (int i
= 0; i
< exist
; ++i
)
3348 if (info
[i
] & ALL_POS
) {
3350 fprintf(stderr
, "\nER: Positive\n");
3351 #endif /* DEBUG_ER */
3353 // Maybe we should chew off some of the fat here
3354 Matrix
*M
= Matrix_Alloc(P
->Dimension
, P
->Dimension
+1);
3355 for (int j
= 0; j
< P
->Dimension
; ++j
)
3356 value_set_si(M
->p
[j
][j
+ (j
>= i
+nvar
)], 1);
3357 Polyhedron
*T
= Polyhedron_Image(P
, M
, options
->MaxRays
);
3359 evalue
*EP
= barvinok_enumerate_e_with_options(T
, exist
-1, nparam
,
3367 for (int i
= 0; i
< exist
; ++i
)
3368 if (info
[i
] & ONE_NEG
) {
3370 fprintf(stderr
, "\nER: Negative\n");
3371 #endif /* DEBUG_ER */
3376 return barvinok_enumerate_e_with_options(P
, exist
-1, nparam
,
3379 Polyhedron
*T
= Polyhedron_Copy(P
);
3380 SwapColumns(T
, nvar
+1, nvar
+1+i
);
3381 evalue
*EP
= barvinok_enumerate_e_with_options(T
, exist
-1, nparam
,
3387 for (int i
= 0; i
< exist
; ++i
)
3388 if (info
[i
] & ROT_NEG
) {
3390 fprintf(stderr
, "\nER: Rotate\n");
3391 #endif /* DEBUG_ER */
3395 Polyhedron
*T
= rotate_along(P
, r
, nvar
, exist
, options
->MaxRays
);
3396 evalue
*EP
= barvinok_enumerate_e_with_options(T
, exist
-1, nparam
,
3401 for (int i
= 0; i
< exist
; ++i
)
3402 if (info
[i
] & INDEPENDENT
) {
3403 Polyhedron
*pos
, *neg
;
3405 /* Find constraint again and split off negative part */
3407 if (SplitOnVar(P
, i
, nvar
, exist
, options
->MaxRays
,
3408 row
, f
, true, &pos
, &neg
)) {
3410 fprintf(stderr
, "\nER: Split\n");
3411 #endif /* DEBUG_ER */
3414 barvinok_enumerate_e_with_options(neg
, exist
-1, nparam
, options
);
3416 barvinok_enumerate_e_with_options(pos
, exist
, nparam
, options
);
3418 free_evalue_refs(E
);
3420 Polyhedron_Free(neg
);
3421 Polyhedron_Free(pos
);
3435 EP
= enumerate_line(P
, exist
, nparam
, options
);
3439 EP
= barvinok_enumerate_pip_with_options(P
, exist
, nparam
, options
);
3443 EP
= enumerate_redundant_ray(P
, exist
, nparam
, options
);
3447 EP
= enumerate_sure(P
, exist
, nparam
, options
);
3451 EP
= enumerate_ray(P
, exist
, nparam
, options
);
3455 EP
= enumerate_sure2(P
, exist
, nparam
, options
);
3459 F
= unfringe(P
, options
->MaxRays
);
3460 if (!PolyhedronIncludes(F
, P
)) {
3462 fprintf(stderr
, "\nER: Fringed\n");
3463 #endif /* DEBUG_ER */
3464 EP
= barvinok_enumerate_e_with_options(F
, exist
, nparam
, options
);
3471 EP
= enumerate_vd(&P
, exist
, nparam
, options
);
3476 EP
= enumerate_sum(P
, exist
, nparam
, options
);
3483 Polyhedron
*pos
, *neg
;
3484 for (i
= 0; i
< exist
; ++i
)
3485 if (SplitOnVar(P
, i
, nvar
, exist
, options
->MaxRays
,
3486 row
, f
, false, &pos
, &neg
))
3492 EP
= enumerate_or(pos
, exist
, nparam
, options
);
3505 * remove equalities that require a "compression" of the parameters
3507 static Polyhedron
*remove_more_equalities(Polyhedron
*P
, unsigned nparam
,
3508 Matrix
**CP
, unsigned MaxRays
)
3511 remove_all_equalities(&P
, NULL
, CP
, NULL
, nparam
, MaxRays
);
3518 static gen_fun
*series(Polyhedron
*P
, unsigned nparam
, barvinok_options
*options
)
3528 assert(!Polyhedron_is_infinite_param(P
, nparam
));
3529 assert(P
->NbBid
== 0);
3530 assert(Polyhedron_has_revlex_positive_rays(P
, nparam
));
3532 P
= remove_more_equalities(P
, nparam
, &CP
, options
->MaxRays
);
3533 assert(P
->NbEq
== 0);
3535 nparam
= CP
->NbColumns
-1;
3540 barvinok_count_with_options(P
, &c
, options
);
3541 gf
= new gen_fun(c
);
3545 red
= gf_base::create(Polyhedron_Project(P
, nparam
),
3546 P
->Dimension
, nparam
, options
);
3547 POL_ENSURE_VERTICES(P
);
3548 red
->start_gf(P
, options
);
3560 gen_fun
* barvinok_series_with_options(Polyhedron
*P
, Polyhedron
* C
,
3561 barvinok_options
*options
)
3564 unsigned nparam
= C
->Dimension
;
3567 CA
= align_context(C
, P
->Dimension
, options
->MaxRays
);
3568 P
= DomainIntersection(P
, CA
, options
->MaxRays
);
3569 Polyhedron_Free(CA
);
3571 gf
= series(P
, nparam
, options
);
3576 gen_fun
* barvinok_series(Polyhedron
*P
, Polyhedron
* C
, unsigned MaxRays
)
3579 barvinok_options
*options
= barvinok_options_new_with_defaults();
3580 options
->MaxRays
= MaxRays
;
3581 gf
= barvinok_series_with_options(P
, C
, options
);
3582 barvinok_options_free(options
);
3586 static Polyhedron
*skew_into_positive_orthant(Polyhedron
*D
, unsigned nparam
,
3592 for (Polyhedron
*P
= D
; P
; P
= P
->next
) {
3593 POL_ENSURE_VERTICES(P
);
3594 assert(!Polyhedron_is_infinite_param(P
, nparam
));
3595 assert(P
->NbBid
== 0);
3596 assert(Polyhedron_has_positive_rays(P
, nparam
));
3598 for (int r
= 0; r
< P
->NbRays
; ++r
) {
3599 if (value_notzero_p(P
->Ray
[r
][P
->Dimension
+1]))
3601 for (int i
= 0; i
< nparam
; ++i
) {
3603 if (value_posz_p(P
->Ray
[r
][i
+1]))
3606 M
= Matrix_Alloc(D
->Dimension
+1, D
->Dimension
+1);
3607 for (int i
= 0; i
< D
->Dimension
+1; ++i
)
3608 value_set_si(M
->p
[i
][i
], 1);
3610 Inner_Product(P
->Ray
[r
]+1, M
->p
[i
], D
->Dimension
+1, &tmp
);
3611 if (value_posz_p(tmp
))
3614 for (j
= P
->Dimension
- nparam
; j
< P
->Dimension
; ++j
)
3615 if (value_pos_p(P
->Ray
[r
][j
+1]))
3617 assert(j
< P
->Dimension
);
3618 value_pdivision(tmp
, P
->Ray
[r
][j
+1], P
->Ray
[r
][i
+1]);
3619 value_subtract(M
->p
[i
][j
], M
->p
[i
][j
], tmp
);
3625 D
= DomainImage(D
, M
, MaxRays
);
3631 gen_fun
* barvinok_enumerate_union_series_with_options(Polyhedron
*D
, Polyhedron
* C
,
3632 barvinok_options
*options
)
3634 Polyhedron
*conv
, *D2
;
3636 gen_fun
*gf
= NULL
, *gf2
;
3637 unsigned nparam
= C
->Dimension
;
3642 CA
= align_context(C
, D
->Dimension
, options
->MaxRays
);
3643 D
= DomainIntersection(D
, CA
, options
->MaxRays
);
3644 Polyhedron_Free(CA
);
3646 D2
= skew_into_positive_orthant(D
, nparam
, options
->MaxRays
);
3647 for (Polyhedron
*P
= D2
; P
; P
= P
->next
) {
3648 assert(P
->Dimension
== D2
->Dimension
);
3651 P_gf
= series(Polyhedron_Copy(P
), nparam
, options
);
3655 gf
->add_union(P_gf
, options
);
3659 /* we actually only need the convex union of the parameter space
3660 * but the reducer classes currently expect a polyhedron in
3661 * the combined space
3663 Polyhedron_Free(gf
->context
);
3664 gf
->context
= DomainConvex(D2
, options
->MaxRays
);
3666 gf2
= gf
->summate(D2
->Dimension
- nparam
, options
);
3675 gen_fun
* barvinok_enumerate_union_series(Polyhedron
*D
, Polyhedron
* C
,
3679 barvinok_options
*options
= barvinok_options_new_with_defaults();
3680 options
->MaxRays
= MaxRays
;
3681 gf
= barvinok_enumerate_union_series_with_options(D
, C
, options
);
3682 barvinok_options_free(options
);
3686 evalue
* barvinok_enumerate_union(Polyhedron
*D
, Polyhedron
* C
, unsigned MaxRays
)
3689 gen_fun
*gf
= barvinok_enumerate_union_series(D
, C
, MaxRays
);