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 typedef evalue
* evalue_p
;
914 struct enumerator_base
{
918 vertex_decomposer
*vpd
;
920 enumerator_base(unsigned dim
, vertex_decomposer
*vpd
)
925 vE
= new evalue_p
[vpd
->nbV
];
926 for (int j
= 0; j
< vpd
->nbV
; ++j
)
930 evalue_set_si(&mone
, -1, 1);
933 void decompose_at(Param_Vertices
*V
, int _i
, barvinok_options
*options
) {
937 value_init(vE
[_i
]->d
);
938 evalue_set_si(vE
[_i
], 0, 1);
940 vpd
->decompose_at_vertex(V
, _i
, options
);
943 virtual ~enumerator_base() {
944 for (int j
= 0; j
< vpd
->nbV
; ++j
)
946 free_evalue_refs(vE
[j
]);
951 free_evalue_refs(&mone
);
954 static enumerator_base
*create(Polyhedron
*P
, unsigned dim
, unsigned nbV
,
955 barvinok_options
*options
);
958 struct enumerator
: public signed_cone_consumer
, public vertex_decomposer
,
959 public enumerator_base
{
967 enumerator(Polyhedron
*P
, unsigned dim
, unsigned nbV
) :
968 vertex_decomposer(P
, nbV
, *this), enumerator_base(dim
, this) {
971 randomvector(P
, lambda
, dim
);
973 c
= Vector_Alloc(dim
+2);
983 virtual void handle(const signed_cone
& sc
, barvinok_options
*options
);
986 void enumerator::handle(const signed_cone
& sc
, barvinok_options
*options
)
991 assert(sc
.rays
.NumRows() == dim
);
992 for (int k
= 0; k
< dim
; ++k
) {
993 if (lambda
* sc
.rays
[k
] == 0)
999 lattice_point(V
, sc
.rays
, lambda
, &num
, 0, options
);
1000 den
= sc
.rays
* lambda
;
1001 normalize(sign
, num
.constant
, den
);
1003 dpoly
n(dim
, den
[0], 1);
1004 for (int k
= 1; k
< dim
; ++k
) {
1005 dpoly
fact(dim
, den
[k
], 1);
1008 if (num
.E
!= NULL
) {
1009 ZZ
one(INIT_VAL
, 1);
1010 dpoly_n
d(dim
, num
.constant
, one
);
1013 multi_polynom(c
, num
.E
, &EV
);
1014 eadd(&EV
, vE
[vert
]);
1015 free_evalue_refs(&EV
);
1016 free_evalue_refs(num
.E
);
1018 } else if (num
.pos
!= -1) {
1019 dpoly_n
d(dim
, num
.constant
, num
.coeff
);
1022 uni_polynom(num
.pos
, c
, &EV
);
1023 eadd(&EV
, vE
[vert
]);
1024 free_evalue_refs(&EV
);
1026 mpq_set_si(count
, 0, 1);
1027 dpoly
d(dim
, num
.constant
);
1028 d
.div(n
, count
, sign
);
1031 evalue_set(&EV
, &count
[0]._mp_num
, &count
[0]._mp_den
);
1032 eadd(&EV
, vE
[vert
]);
1033 free_evalue_refs(&EV
);
1037 struct ienumerator_base
: enumerator_base
{
1040 ienumerator_base(unsigned dim
, vertex_decomposer
*vpd
) :
1041 enumerator_base(dim
,vpd
) {
1042 E_vertex
= new evalue_p
[dim
];
1045 virtual ~ienumerator_base() {
1049 evalue
*E_num(int i
, int d
) {
1050 return E_vertex
[i
+ (dim
-d
)];
1059 cumulator(evalue
*factor
, evalue
*v
, dpoly_r
*r
) :
1060 factor(factor
), v(v
), r(r
) {}
1062 void cumulate(barvinok_options
*options
);
1064 virtual void add_term(const vector
<int>& powers
, evalue
*f2
) = 0;
1067 void cumulator::cumulate(barvinok_options
*options
)
1069 evalue cum
; // factor * 1 * E_num[0]/1 * (E_num[0]-1)/2 *...
1071 evalue t
; // E_num[0] - (m-1)
1075 if (options
->lookup_table
) {
1077 evalue_set_si(&mone
, -1, 1);
1081 evalue_copy(&cum
, factor
);
1084 value_set_si(f
.d
, 1);
1085 value_set_si(f
.x
.n
, 1);
1089 if (!options
->lookup_table
) {
1090 for (cst
= &t
; value_zero_p(cst
->d
); ) {
1091 if (cst
->x
.p
->type
== fractional
)
1092 cst
= &cst
->x
.p
->arr
[1];
1094 cst
= &cst
->x
.p
->arr
[0];
1098 for (int m
= 0; m
< r
->len
; ++m
) {
1101 value_set_si(f
.d
, m
);
1103 if (!options
->lookup_table
)
1104 value_subtract(cst
->x
.n
, cst
->x
.n
, cst
->d
);
1110 dpoly_r_term_list
& current
= r
->c
[r
->len
-1-m
];
1111 dpoly_r_term_list::iterator j
;
1112 for (j
= current
.begin(); j
!= current
.end(); ++j
) {
1113 if ((*j
)->coeff
== 0)
1115 evalue
*f2
= new evalue
;
1117 value_init(f2
->x
.n
);
1118 zz2value((*j
)->coeff
, f2
->x
.n
);
1119 zz2value(r
->denom
, f2
->d
);
1122 add_term((*j
)->powers
, f2
);
1125 free_evalue_refs(&f
);
1126 free_evalue_refs(&t
);
1127 free_evalue_refs(&cum
);
1128 if (options
->lookup_table
)
1129 free_evalue_refs(&mone
);
1132 struct E_poly_term
{
1137 struct ie_cum
: public cumulator
{
1138 vector
<E_poly_term
*> terms
;
1140 ie_cum(evalue
*factor
, evalue
*v
, dpoly_r
*r
) : cumulator(factor
, v
, r
) {}
1142 virtual void add_term(const vector
<int>& powers
, evalue
*f2
);
1145 void ie_cum::add_term(const vector
<int>& powers
, evalue
*f2
)
1148 for (k
= 0; k
< terms
.size(); ++k
) {
1149 if (terms
[k
]->powers
== powers
) {
1150 eadd(f2
, terms
[k
]->E
);
1151 free_evalue_refs(f2
);
1156 if (k
>= terms
.size()) {
1157 E_poly_term
*ET
= new E_poly_term
;
1158 ET
->powers
= powers
;
1160 terms
.push_back(ET
);
1164 struct ienumerator
: public signed_cone_consumer
, public vertex_decomposer
,
1165 public ienumerator_base
{
1171 ienumerator(Polyhedron
*P
, unsigned dim
, unsigned nbV
) :
1172 vertex_decomposer(P
, nbV
, *this), ienumerator_base(dim
, this) {
1173 vertex
.SetDims(1, dim
);
1175 den
.SetDims(dim
, dim
);
1183 virtual void handle(const signed_cone
& sc
, barvinok_options
*options
);
1184 void reduce(evalue
*factor
, const mat_ZZ
& num
, const mat_ZZ
& den_f
,
1185 barvinok_options
*options
);
1188 void ienumerator::reduce(evalue
*factor
, const mat_ZZ
& num
, const mat_ZZ
& den_f
,
1189 barvinok_options
*options
)
1191 unsigned len
= den_f
.NumRows(); // number of factors in den
1192 unsigned dim
= num
.NumCols();
1193 assert(num
.NumRows() == 1);
1196 eadd(factor
, vE
[vert
]);
1205 split_one(num
, num_s
, num_p
, den_f
, den_s
, den_r
);
1208 den_p
.SetLength(len
);
1212 normalize(one
, num_s
, num_p
, den_s
, den_p
, den_r
);
1214 emul(&mone
, factor
);
1218 for (int k
= 0; k
< len
; ++k
) {
1221 else if (den_s
[k
] == 0)
1224 if (no_param
== 0) {
1225 reduce(factor
, num_p
, den_r
, options
);
1229 pden
.SetDims(only_param
, dim
-1);
1231 for (k
= 0, l
= 0; k
< len
; ++k
)
1233 pden
[l
++] = den_r
[k
];
1235 for (k
= 0; k
< len
; ++k
)
1239 dpoly
n(no_param
, num_s
[0]);
1240 dpoly
D(no_param
, den_s
[k
], 1);
1241 for ( ; ++k
< len
; )
1242 if (den_p
[k
] == 0) {
1243 dpoly
fact(no_param
, den_s
[k
], 1);
1248 // if no_param + only_param == len then all powers
1249 // below will be all zero
1250 if (no_param
+ only_param
== len
) {
1251 if (E_num(0, dim
) != 0)
1252 r
= new dpoly_r(n
, len
);
1254 mpq_set_si(tcount
, 0, 1);
1256 n
.div(D
, tcount
, one
);
1258 if (value_notzero_p(mpq_numref(tcount
))) {
1262 value_assign(f
.x
.n
, mpq_numref(tcount
));
1263 value_assign(f
.d
, mpq_denref(tcount
));
1265 reduce(factor
, num_p
, pden
, options
);
1266 free_evalue_refs(&f
);
1271 for (k
= 0; k
< len
; ++k
) {
1272 if (den_s
[k
] == 0 || den_p
[k
] == 0)
1275 dpoly
pd(no_param
-1, den_s
[k
], 1);
1278 for (l
= 0; l
< k
; ++l
)
1279 if (den_r
[l
] == den_r
[k
])
1283 r
= new dpoly_r(n
, pd
, l
, len
);
1285 dpoly_r
*nr
= new dpoly_r(r
, pd
, l
, len
);
1291 dpoly_r
*rc
= r
->div(D
);
1294 if (E_num(0, dim
) == 0) {
1295 int common
= pden
.NumRows();
1296 dpoly_r_term_list
& final
= r
->c
[r
->len
-1];
1302 zz2value(r
->denom
, f
.d
);
1303 dpoly_r_term_list::iterator j
;
1304 for (j
= final
.begin(); j
!= final
.end(); ++j
) {
1305 if ((*j
)->coeff
== 0)
1308 for (int k
= 0; k
< r
->dim
; ++k
) {
1309 int n
= (*j
)->powers
[k
];
1312 pden
.SetDims(rows
+n
, pden
.NumCols());
1313 for (int l
= 0; l
< n
; ++l
)
1314 pden
[rows
+l
] = den_r
[k
];
1318 evalue_copy(&t
, factor
);
1319 zz2value((*j
)->coeff
, f
.x
.n
);
1321 reduce(&t
, num_p
, pden
, options
);
1322 free_evalue_refs(&t
);
1324 free_evalue_refs(&f
);
1326 ie_cum
cum(factor
, E_num(0, dim
), r
);
1327 cum
.cumulate(options
);
1329 int common
= pden
.NumRows();
1331 for (int j
= 0; j
< cum
.terms
.size(); ++j
) {
1333 pden
.SetDims(rows
, pden
.NumCols());
1334 for (int k
= 0; k
< r
->dim
; ++k
) {
1335 int n
= cum
.terms
[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
];
1343 reduce(cum
.terms
[j
]->E
, num_p
, pden
, options
);
1344 free_evalue_refs(cum
.terms
[j
]->E
);
1345 delete cum
.terms
[j
]->E
;
1346 delete cum
.terms
[j
];
1353 static int type_offset(enode
*p
)
1355 return p
->type
== fractional
? 1 :
1356 p
->type
== flooring
? 1 : 0;
1359 static int edegree(evalue
*e
)
1364 if (value_notzero_p(e
->d
))
1368 int i
= type_offset(p
);
1369 if (p
->size
-i
-1 > d
)
1370 d
= p
->size
- i
- 1;
1371 for (; i
< p
->size
; i
++) {
1372 int d2
= edegree(&p
->arr
[i
]);
1379 void ienumerator::handle(const signed_cone
& sc
, barvinok_options
*options
)
1381 assert(sc
.det
== 1);
1383 assert(sc
.rays
.NumRows() == dim
);
1385 lattice_point(V
, sc
.rays
, vertex
[0], E_vertex
, options
);
1391 evalue_set_si(&one
, sc
.sign
, 1);
1392 reduce(&one
, vertex
, den
, options
);
1393 free_evalue_refs(&one
);
1395 for (int i
= 0; i
< dim
; ++i
)
1397 free_evalue_refs(E_vertex
[i
]);
1402 struct bfenumerator
: public vertex_decomposer
, public bf_base
,
1403 public ienumerator_base
{
1406 bfenumerator(Polyhedron
*P
, unsigned dim
, unsigned nbV
) :
1407 vertex_decomposer(P
, nbV
, *this),
1408 bf_base(dim
), ienumerator_base(dim
, this) {
1416 virtual void handle(const signed_cone
& sc
, barvinok_options
*options
);
1417 virtual void base(mat_ZZ
& factors
, bfc_vec
& v
);
1419 bfc_term_base
* new_bf_term(int len
) {
1420 bfe_term
* t
= new bfe_term(len
);
1424 virtual void set_factor(bfc_term_base
*t
, int k
, int change
) {
1425 bfe_term
* bfet
= static_cast<bfe_term
*>(t
);
1426 factor
= bfet
->factors
[k
];
1427 assert(factor
!= NULL
);
1428 bfet
->factors
[k
] = NULL
;
1430 emul(&mone
, factor
);
1433 virtual void set_factor(bfc_term_base
*t
, int k
, mpq_t
&q
, int change
) {
1434 bfe_term
* bfet
= static_cast<bfe_term
*>(t
);
1435 factor
= bfet
->factors
[k
];
1436 assert(factor
!= NULL
);
1437 bfet
->factors
[k
] = NULL
;
1443 value_oppose(f
.x
.n
, mpq_numref(q
));
1445 value_assign(f
.x
.n
, mpq_numref(q
));
1446 value_assign(f
.d
, mpq_denref(q
));
1448 free_evalue_refs(&f
);
1451 virtual void set_factor(bfc_term_base
*t
, int k
, const QQ
& c
, int change
) {
1452 bfe_term
* bfet
= static_cast<bfe_term
*>(t
);
1454 factor
= new evalue
;
1459 zz2value(c
.n
, f
.x
.n
);
1461 value_oppose(f
.x
.n
, f
.x
.n
);
1464 value_init(factor
->d
);
1465 evalue_copy(factor
, bfet
->factors
[k
]);
1467 free_evalue_refs(&f
);
1470 void set_factor(evalue
*f
, int change
) {
1476 virtual void insert_term(bfc_term_base
*t
, int i
) {
1477 bfe_term
* bfet
= static_cast<bfe_term
*>(t
);
1478 int len
= t
->terms
.NumRows()-1; // already increased by one
1480 bfet
->factors
.resize(len
+1);
1481 for (int j
= len
; j
> i
; --j
) {
1482 bfet
->factors
[j
] = bfet
->factors
[j
-1];
1483 t
->terms
[j
] = t
->terms
[j
-1];
1485 bfet
->factors
[i
] = factor
;
1489 virtual void update_term(bfc_term_base
*t
, int i
) {
1490 bfe_term
* bfet
= static_cast<bfe_term
*>(t
);
1492 eadd(factor
, bfet
->factors
[i
]);
1493 free_evalue_refs(factor
);
1497 virtual bool constant_vertex(int dim
) { return E_num(0, dim
) == 0; }
1499 virtual void cum(bf_reducer
*bfr
, bfc_term_base
*t
, int k
, dpoly_r
*r
,
1500 barvinok_options
*options
);
1503 enumerator_base
*enumerator_base::create(Polyhedron
*P
, unsigned dim
, unsigned nbV
,
1504 barvinok_options
*options
)
1506 enumerator_base
*eb
;
1508 if (options
->incremental_specialization
== BV_SPECIALIZATION_BF
)
1509 eb
= new bfenumerator(P
, dim
, nbV
);
1510 else if (options
->incremental_specialization
== BV_SPECIALIZATION_DF
)
1511 eb
= new ienumerator(P
, dim
, nbV
);
1513 eb
= new enumerator(P
, dim
, nbV
);
1518 struct bfe_cum
: public cumulator
{
1520 bfc_term_base
*told
;
1524 bfe_cum(evalue
*factor
, evalue
*v
, dpoly_r
*r
, bf_reducer
*bfr
,
1525 bfc_term_base
*t
, int k
, bfenumerator
*e
) :
1526 cumulator(factor
, v
, r
), told(t
), k(k
),
1530 virtual void add_term(const vector
<int>& powers
, evalue
*f2
);
1533 void bfe_cum::add_term(const vector
<int>& powers
, evalue
*f2
)
1535 bfr
->update_powers(powers
);
1537 bfc_term_base
* t
= bfe
->find_bfc_term(bfr
->vn
, bfr
->npowers
, bfr
->nnf
);
1538 bfe
->set_factor(f2
, bfr
->l_changes
% 2);
1539 bfe
->add_term(t
, told
->terms
[k
], bfr
->l_extra_num
);
1542 void bfenumerator::cum(bf_reducer
*bfr
, bfc_term_base
*t
, int k
,
1543 dpoly_r
*r
, barvinok_options
*options
)
1545 bfe_term
* bfet
= static_cast<bfe_term
*>(t
);
1546 bfe_cum
cum(bfet
->factors
[k
], E_num(0, bfr
->d
), r
, bfr
, t
, k
, this);
1547 cum
.cumulate(options
);
1550 void bfenumerator::base(mat_ZZ
& factors
, bfc_vec
& v
)
1552 for (int i
= 0; i
< v
.size(); ++i
) {
1553 assert(v
[i
]->terms
.NumRows() == 1);
1554 evalue
*factor
= static_cast<bfe_term
*>(v
[i
])->factors
[0];
1555 eadd(factor
, vE
[vert
]);
1560 void bfenumerator::handle(const signed_cone
& sc
, barvinok_options
*options
)
1562 assert(sc
.det
== 1);
1564 assert(sc
.rays
.NumRows() == enumerator_base::dim
);
1566 bfe_term
* t
= new bfe_term(enumerator_base::dim
);
1567 vector
< bfc_term_base
* > v
;
1570 t
->factors
.resize(1);
1572 t
->terms
.SetDims(1, enumerator_base::dim
);
1573 lattice_point(V
, sc
.rays
, t
->terms
[0], E_vertex
, options
);
1575 // the elements of factors are always lexpositive
1577 int s
= setup_factors(sc
.rays
, factors
, t
, sc
.sign
);
1579 t
->factors
[0] = new evalue
;
1580 value_init(t
->factors
[0]->d
);
1581 evalue_set_si(t
->factors
[0], s
, 1);
1582 reduce(factors
, v
, options
);
1584 for (int i
= 0; i
< enumerator_base::dim
; ++i
)
1586 free_evalue_refs(E_vertex
[i
]);
1591 #ifdef HAVE_CORRECT_VERTICES
1592 static inline Param_Polyhedron
*Polyhedron2Param_SD(Polyhedron
**Din
,
1593 Polyhedron
*Cin
,int WS
,Polyhedron
**CEq
,Matrix
**CT
)
1595 if (WS
& POL_NO_DUAL
)
1597 return Polyhedron2Param_SimplifiedDomain(Din
, Cin
, WS
, CEq
, CT
);
1600 static Param_Polyhedron
*Polyhedron2Param_SD(Polyhedron
**Din
,
1601 Polyhedron
*Cin
,int WS
,Polyhedron
**CEq
,Matrix
**CT
)
1603 static char data
[] = " 1 0 0 0 0 1 -18 "
1604 " 1 0 0 -20 0 19 1 "
1605 " 1 0 1 20 0 -20 16 "
1608 " 1 4 -20 0 0 -1 23 "
1609 " 1 -4 20 0 0 1 -22 "
1610 " 1 0 1 0 20 -20 16 "
1611 " 1 0 0 0 -20 19 1 ";
1612 static int checked
= 0;
1617 Matrix
*M
= Matrix_Alloc(9, 7);
1618 for (i
= 0; i
< 9; ++i
)
1619 for (int j
= 0; j
< 7; ++j
) {
1620 sscanf(p
, "%d%n", &v
, &n
);
1622 value_set_si(M
->p
[i
][j
], v
);
1624 Polyhedron
*P
= Constraints2Polyhedron(M
, 1024);
1626 Polyhedron
*U
= Universe_Polyhedron(1);
1627 Param_Polyhedron
*PP
= Polyhedron2Param_Domain(P
, U
, 1024);
1631 for (i
= 0, V
= PP
->V
; V
; ++i
, V
= V
->next
)
1634 Param_Polyhedron_Free(PP
);
1636 fprintf(stderr
, "WARNING: results may be incorrect\n");
1638 "WARNING: use latest version of PolyLib to remove this warning\n");
1642 return Polyhedron2Param_SimplifiedDomain(Din
, Cin
, WS
, CEq
, CT
);
1646 static evalue
* barvinok_enumerate_ev_f(Polyhedron
*P
, Polyhedron
* C
,
1647 barvinok_options
*options
);
1650 static evalue
* barvinok_enumerate_cst(Polyhedron
*P
, Polyhedron
* C
,
1651 struct barvinok_options
*options
)
1655 ALLOC(evalue
, eres
);
1656 value_init(eres
->d
);
1657 value_set_si(eres
->d
, 0);
1658 eres
->x
.p
= new_enode(partition
, 2, C
->Dimension
);
1659 EVALUE_SET_DOMAIN(eres
->x
.p
->arr
[0],
1660 DomainConstraintSimplify(C
, options
->MaxRays
));
1661 value_set_si(eres
->x
.p
->arr
[1].d
, 1);
1662 value_init(eres
->x
.p
->arr
[1].x
.n
);
1664 value_set_si(eres
->x
.p
->arr
[1].x
.n
, 0);
1666 barvinok_count_with_options(P
, &eres
->x
.p
->arr
[1].x
.n
, options
);
1671 evalue
* barvinok_enumerate_with_options(Polyhedron
*P
, Polyhedron
* C
,
1672 struct barvinok_options
*options
)
1674 //P = unfringe(P, MaxRays);
1675 Polyhedron
*Corig
= C
;
1676 Polyhedron
*CEq
= NULL
, *rVD
, *CA
;
1678 unsigned nparam
= C
->Dimension
;
1682 value_init(factor
.d
);
1683 evalue_set_si(&factor
, 1, 1);
1685 CA
= align_context(C
, P
->Dimension
, options
->MaxRays
);
1686 P
= DomainIntersection(P
, CA
, options
->MaxRays
);
1687 Polyhedron_Free(CA
);
1690 POL_ENSURE_FACETS(P
);
1691 POL_ENSURE_VERTICES(P
);
1692 POL_ENSURE_FACETS(C
);
1693 POL_ENSURE_VERTICES(C
);
1695 if (C
->Dimension
== 0 || emptyQ(P
)) {
1697 eres
= barvinok_enumerate_cst(P
, CEq
? CEq
: Polyhedron_Copy(C
), options
);
1699 emul(&factor
, eres
);
1700 reduce_evalue(eres
);
1701 free_evalue_refs(&factor
);
1708 if (Polyhedron_is_infinite_param(P
, nparam
))
1713 P
= remove_equalities_p(P
, P
->Dimension
-nparam
, &f
);
1714 mask(f
, &factor
, options
);
1717 if (P
->Dimension
== nparam
) {
1719 P
= Universe_Polyhedron(0);
1723 Polyhedron
*T
= Polyhedron_Factor(P
, nparam
, options
->MaxRays
);
1724 if (T
|| (P
->Dimension
== nparam
+1)) {
1727 for (Q
= T
? T
: P
; Q
; Q
= Q
->next
) {
1728 Polyhedron
*next
= Q
->next
;
1732 if (Q
->Dimension
!= C
->Dimension
)
1733 QC
= Polyhedron_Project(Q
, nparam
);
1736 C
= DomainIntersection(C
, QC
, options
->MaxRays
);
1738 Polyhedron_Free(C2
);
1740 Polyhedron_Free(QC
);
1748 if (T
->Dimension
== C
->Dimension
) {
1755 Polyhedron
*next
= P
->next
;
1757 eres
= barvinok_enumerate_ev_f(P
, C
, options
);
1764 for (Q
= P
->next
; Q
; Q
= Q
->next
) {
1765 Polyhedron
*next
= Q
->next
;
1768 f
= barvinok_enumerate_ev_f(Q
, C
, options
);
1770 free_evalue_refs(f
);
1780 evalue
* barvinok_enumerate_ev(Polyhedron
*P
, Polyhedron
* C
, unsigned MaxRays
)
1783 barvinok_options
*options
= barvinok_options_new_with_defaults();
1784 options
->MaxRays
= MaxRays
;
1785 E
= barvinok_enumerate_with_options(P
, C
, options
);
1786 barvinok_options_free(options
);
1790 static evalue
* barvinok_enumerate_ev_f(Polyhedron
*P
, Polyhedron
* C
,
1791 barvinok_options
*options
)
1793 unsigned nparam
= C
->Dimension
;
1795 if (P
->Dimension
- nparam
== 1)
1796 return ParamLine_Length(P
, C
, options
);
1798 Param_Polyhedron
*PP
= NULL
;
1799 Polyhedron
*CEq
= NULL
, *pVD
;
1801 Param_Domain
*D
, *next
;
1804 Polyhedron
*Porig
= P
;
1806 PP
= Polyhedron2Param_SD(&P
,C
,options
->MaxRays
,&CEq
,&CT
);
1808 if (isIdentity(CT
)) {
1812 assert(CT
->NbRows
!= CT
->NbColumns
);
1813 if (CT
->NbRows
== 1) { // no more parameters
1814 eres
= barvinok_enumerate_cst(P
, CEq
, options
);
1819 Param_Polyhedron_Free(PP
);
1825 nparam
= CT
->NbRows
- 1;
1828 unsigned dim
= P
->Dimension
- nparam
;
1830 ALLOC(evalue
, eres
);
1831 value_init(eres
->d
);
1832 value_set_si(eres
->d
, 0);
1835 for (nd
= 0, D
=PP
->D
; D
; ++nd
, D
=D
->next
);
1836 struct section
{ Polyhedron
*D
; evalue E
; };
1837 section
*s
= new section
[nd
];
1838 Polyhedron
**fVD
= new Polyhedron_p
[nd
];
1840 enumerator_base
*et
= NULL
;
1845 et
= enumerator_base::create(P
, dim
, PP
->nbV
, options
);
1847 for(nd
= 0, D
=PP
->D
; D
; D
=next
) {
1850 Polyhedron
*rVD
= reduce_domain(D
->Domain
, CT
, CEq
, fVD
, nd
, options
);
1854 pVD
= CT
? DomainImage(rVD
,CT
,options
->MaxRays
) : rVD
;
1856 value_init(s
[nd
].E
.d
);
1857 evalue_set_si(&s
[nd
].E
, 0, 1);
1860 FORALL_PVertex_in_ParamPolyhedron(V
,D
,PP
) // _i is internal counter
1863 et
->decompose_at(V
, _i
, options
);
1864 } catch (OrthogonalException
&e
) {
1867 for (; nd
>= 0; --nd
) {
1868 free_evalue_refs(&s
[nd
].E
);
1869 Domain_Free(s
[nd
].D
);
1870 Domain_Free(fVD
[nd
]);
1874 eadd(et
->vE
[_i
] , &s
[nd
].E
);
1875 END_FORALL_PVertex_in_ParamPolyhedron
;
1876 evalue_range_reduction_in_domain(&s
[nd
].E
, pVD
);
1879 addeliminatedparams_evalue(&s
[nd
].E
, CT
);
1887 evalue_set_si(eres
, 0, 1);
1889 eres
->x
.p
= new_enode(partition
, 2*nd
, C
->Dimension
);
1890 for (int j
= 0; j
< nd
; ++j
) {
1891 EVALUE_SET_DOMAIN(eres
->x
.p
->arr
[2*j
], s
[j
].D
);
1892 value_clear(eres
->x
.p
->arr
[2*j
+1].d
);
1893 eres
->x
.p
->arr
[2*j
+1] = s
[j
].E
;
1894 Domain_Free(fVD
[j
]);
1901 Polyhedron_Free(CEq
);
1905 Enumeration
* barvinok_enumerate(Polyhedron
*P
, Polyhedron
* C
, unsigned MaxRays
)
1907 evalue
*EP
= barvinok_enumerate_ev(P
, C
, MaxRays
);
1909 return partition2enumeration(EP
);
1912 static void SwapColumns(Value
**V
, int n
, int i
, int j
)
1914 for (int r
= 0; r
< n
; ++r
)
1915 value_swap(V
[r
][i
], V
[r
][j
]);
1918 static void SwapColumns(Polyhedron
*P
, int i
, int j
)
1920 SwapColumns(P
->Constraint
, P
->NbConstraints
, i
, j
);
1921 SwapColumns(P
->Ray
, P
->NbRays
, i
, j
);
1924 /* Construct a constraint c from constraints l and u such that if
1925 * if constraint c holds then for each value of the other variables
1926 * there is at most one value of variable pos (position pos+1 in the constraints).
1928 * Given a lower and an upper bound
1929 * n_l v_i + <c_l,x> + c_l >= 0
1930 * -n_u v_i + <c_u,x> + c_u >= 0
1931 * the constructed constraint is
1933 * -(n_l<c_u,x> + n_u<c_l,x>) + (-n_l c_u - n_u c_l + n_l n_u - 1)
1935 * which is then simplified to remove the content of the non-constant coefficients
1937 * len is the total length of the constraints.
1938 * v is a temporary variable that can be used by this procedure
1940 static void negative_test_constraint(Value
*l
, Value
*u
, Value
*c
, int pos
,
1943 value_oppose(*v
, u
[pos
+1]);
1944 Vector_Combine(l
+1, u
+1, c
+1, *v
, l
[pos
+1], len
-1);
1945 value_multiply(*v
, *v
, l
[pos
+1]);
1946 value_subtract(c
[len
-1], c
[len
-1], *v
);
1947 value_set_si(*v
, -1);
1948 Vector_Scale(c
+1, c
+1, *v
, len
-1);
1949 value_decrement(c
[len
-1], c
[len
-1]);
1950 ConstraintSimplify(c
, c
, len
, v
);
1953 static bool parallel_constraints(Value
*l
, Value
*u
, Value
*c
, int pos
,
1962 Vector_Gcd(&l
[1+pos
], len
, &g1
);
1963 Vector_Gcd(&u
[1+pos
], len
, &g2
);
1964 Vector_Combine(l
+1+pos
, u
+1+pos
, c
+1, g2
, g1
, len
);
1965 parallel
= First_Non_Zero(c
+1, len
) == -1;
1973 static void negative_test_constraint7(Value
*l
, Value
*u
, Value
*c
, int pos
,
1974 int exist
, int len
, Value
*v
)
1979 Vector_Gcd(&u
[1+pos
], exist
, v
);
1980 Vector_Gcd(&l
[1+pos
], exist
, &g
);
1981 Vector_Combine(l
+1, u
+1, c
+1, *v
, g
, len
-1);
1982 value_multiply(*v
, *v
, g
);
1983 value_subtract(c
[len
-1], c
[len
-1], *v
);
1984 value_set_si(*v
, -1);
1985 Vector_Scale(c
+1, c
+1, *v
, len
-1);
1986 value_decrement(c
[len
-1], c
[len
-1]);
1987 ConstraintSimplify(c
, c
, len
, v
);
1992 /* Turns a x + b >= 0 into a x + b <= -1
1994 * len is the total length of the constraint.
1995 * v is a temporary variable that can be used by this procedure
1997 static void oppose_constraint(Value
*c
, int len
, Value
*v
)
1999 value_set_si(*v
, -1);
2000 Vector_Scale(c
+1, c
+1, *v
, len
-1);
2001 value_decrement(c
[len
-1], c
[len
-1]);
2004 /* Split polyhedron P into two polyhedra *pos and *neg, where
2005 * existential variable i has at most one solution for each
2006 * value of the other variables in *neg.
2008 * The splitting is performed using constraints l and u.
2010 * nvar: number of set variables
2011 * row: temporary vector that can be used by this procedure
2012 * f: temporary value that can be used by this procedure
2014 static bool SplitOnConstraint(Polyhedron
*P
, int i
, int l
, int u
,
2015 int nvar
, int MaxRays
, Vector
*row
, Value
& f
,
2016 Polyhedron
**pos
, Polyhedron
**neg
)
2018 negative_test_constraint(P
->Constraint
[l
], P
->Constraint
[u
],
2019 row
->p
, nvar
+i
, P
->Dimension
+2, &f
);
2020 *neg
= AddConstraints(row
->p
, 1, P
, MaxRays
);
2022 /* We found an independent, but useless constraint
2023 * Maybe we should detect this earlier and not
2024 * mark the variable as INDEPENDENT
2026 if (emptyQ((*neg
))) {
2027 Polyhedron_Free(*neg
);
2031 oppose_constraint(row
->p
, P
->Dimension
+2, &f
);
2032 *pos
= AddConstraints(row
->p
, 1, P
, MaxRays
);
2034 if (emptyQ((*pos
))) {
2035 Polyhedron_Free(*neg
);
2036 Polyhedron_Free(*pos
);
2044 * unimodularly transform P such that constraint r is transformed
2045 * into a constraint that involves only a single (the first)
2046 * existential variable
2049 static Polyhedron
*rotate_along(Polyhedron
*P
, int r
, int nvar
, int exist
,
2055 Vector
*row
= Vector_Alloc(exist
);
2056 Vector_Copy(P
->Constraint
[r
]+1+nvar
, row
->p
, exist
);
2057 Vector_Gcd(row
->p
, exist
, &g
);
2058 if (value_notone_p(g
))
2059 Vector_AntiScale(row
->p
, row
->p
, g
, exist
);
2062 Matrix
*M
= unimodular_complete(row
);
2063 Matrix
*M2
= Matrix_Alloc(P
->Dimension
+1, P
->Dimension
+1);
2064 for (r
= 0; r
< nvar
; ++r
)
2065 value_set_si(M2
->p
[r
][r
], 1);
2066 for ( ; r
< nvar
+exist
; ++r
)
2067 Vector_Copy(M
->p
[r
-nvar
], M2
->p
[r
]+nvar
, exist
);
2068 for ( ; r
< P
->Dimension
+1; ++r
)
2069 value_set_si(M2
->p
[r
][r
], 1);
2070 Polyhedron
*T
= Polyhedron_Image(P
, M2
, MaxRays
);
2079 /* Split polyhedron P into two polyhedra *pos and *neg, where
2080 * existential variable i has at most one solution for each
2081 * value of the other variables in *neg.
2083 * If independent is set, then the two constraints on which the
2084 * split will be performed need to be independent of the other
2085 * existential variables.
2087 * Return true if an appropriate split could be performed.
2089 * nvar: number of set variables
2090 * exist: number of existential variables
2091 * row: temporary vector that can be used by this procedure
2092 * f: temporary value that can be used by this procedure
2094 static bool SplitOnVar(Polyhedron
*P
, int i
,
2095 int nvar
, int exist
, int MaxRays
,
2096 Vector
*row
, Value
& f
, bool independent
,
2097 Polyhedron
**pos
, Polyhedron
**neg
)
2101 for (int l
= P
->NbEq
; l
< P
->NbConstraints
; ++l
) {
2102 if (value_negz_p(P
->Constraint
[l
][nvar
+i
+1]))
2106 for (j
= 0; j
< exist
; ++j
)
2107 if (j
!= i
&& value_notzero_p(P
->Constraint
[l
][nvar
+j
+1]))
2113 for (int u
= P
->NbEq
; u
< P
->NbConstraints
; ++u
) {
2114 if (value_posz_p(P
->Constraint
[u
][nvar
+i
+1]))
2118 for (j
= 0; j
< exist
; ++j
)
2119 if (j
!= i
&& value_notzero_p(P
->Constraint
[u
][nvar
+j
+1]))
2125 if (SplitOnConstraint(P
, i
, l
, u
, nvar
, MaxRays
, row
, f
, pos
, neg
)) {
2128 SwapColumns(*neg
, nvar
+1, nvar
+1+i
);
2138 static bool double_bound_pair(Polyhedron
*P
, int nvar
, int exist
,
2139 int i
, int l1
, int l2
,
2140 Polyhedron
**pos
, Polyhedron
**neg
)
2144 Vector
*row
= Vector_Alloc(P
->Dimension
+2);
2145 value_set_si(row
->p
[0], 1);
2146 value_oppose(f
, P
->Constraint
[l1
][nvar
+i
+1]);
2147 Vector_Combine(P
->Constraint
[l1
]+1, P
->Constraint
[l2
]+1,
2149 P
->Constraint
[l2
][nvar
+i
+1], f
,
2151 ConstraintSimplify(row
->p
, row
->p
, P
->Dimension
+2, &f
);
2152 *pos
= AddConstraints(row
->p
, 1, P
, 0);
2153 value_set_si(f
, -1);
2154 Vector_Scale(row
->p
+1, row
->p
+1, f
, P
->Dimension
+1);
2155 value_decrement(row
->p
[P
->Dimension
+1], row
->p
[P
->Dimension
+1]);
2156 *neg
= AddConstraints(row
->p
, 1, P
, 0);
2160 return !emptyQ((*pos
)) && !emptyQ((*neg
));
2163 static bool double_bound(Polyhedron
*P
, int nvar
, int exist
,
2164 Polyhedron
**pos
, Polyhedron
**neg
)
2166 for (int i
= 0; i
< exist
; ++i
) {
2168 for (l1
= P
->NbEq
; l1
< P
->NbConstraints
; ++l1
) {
2169 if (value_negz_p(P
->Constraint
[l1
][nvar
+i
+1]))
2171 for (l2
= l1
+ 1; l2
< P
->NbConstraints
; ++l2
) {
2172 if (value_negz_p(P
->Constraint
[l2
][nvar
+i
+1]))
2174 if (double_bound_pair(P
, nvar
, exist
, i
, l1
, l2
, pos
, neg
))
2178 for (l1
= P
->NbEq
; l1
< P
->NbConstraints
; ++l1
) {
2179 if (value_posz_p(P
->Constraint
[l1
][nvar
+i
+1]))
2181 if (l1
< P
->NbConstraints
)
2182 for (l2
= l1
+ 1; l2
< P
->NbConstraints
; ++l2
) {
2183 if (value_posz_p(P
->Constraint
[l2
][nvar
+i
+1]))
2185 if (double_bound_pair(P
, nvar
, exist
, i
, l1
, l2
, pos
, neg
))
2197 INDEPENDENT
= 1 << 2,
2201 static evalue
* enumerate_or(Polyhedron
*D
,
2202 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2205 fprintf(stderr
, "\nER: Or\n");
2206 #endif /* DEBUG_ER */
2208 Polyhedron
*N
= D
->next
;
2211 barvinok_enumerate_e_with_options(D
, exist
, nparam
, options
);
2214 for (D
= N
; D
; D
= N
) {
2219 barvinok_enumerate_e_with_options(D
, exist
, nparam
, options
);
2222 free_evalue_refs(EN
);
2232 static evalue
* enumerate_sum(Polyhedron
*P
,
2233 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2235 int nvar
= P
->Dimension
- exist
- nparam
;
2236 int toswap
= nvar
< exist
? nvar
: exist
;
2237 for (int i
= 0; i
< toswap
; ++i
)
2238 SwapColumns(P
, 1 + i
, nvar
+exist
- i
);
2242 fprintf(stderr
, "\nER: Sum\n");
2243 #endif /* DEBUG_ER */
2245 evalue
*EP
= barvinok_enumerate_e_with_options(P
, exist
, nparam
, options
);
2247 for (int i
= 0; i
< /* nvar */ nparam
; ++i
) {
2248 Matrix
*C
= Matrix_Alloc(1, 1 + nparam
+ 1);
2249 value_set_si(C
->p
[0][0], 1);
2251 value_init(split
.d
);
2252 value_set_si(split
.d
, 0);
2253 split
.x
.p
= new_enode(partition
, 4, nparam
);
2254 value_set_si(C
->p
[0][1+i
], 1);
2255 Matrix
*C2
= Matrix_Copy(C
);
2256 EVALUE_SET_DOMAIN(split
.x
.p
->arr
[0],
2257 Constraints2Polyhedron(C2
, options
->MaxRays
));
2259 evalue_set_si(&split
.x
.p
->arr
[1], 1, 1);
2260 value_set_si(C
->p
[0][1+i
], -1);
2261 value_set_si(C
->p
[0][1+nparam
], -1);
2262 EVALUE_SET_DOMAIN(split
.x
.p
->arr
[2],
2263 Constraints2Polyhedron(C
, options
->MaxRays
));
2264 evalue_set_si(&split
.x
.p
->arr
[3], 1, 1);
2266 free_evalue_refs(&split
);
2270 evalue_range_reduction(EP
);
2272 evalue_frac2floor2(EP
, 1);
2274 evalue
*sum
= esum(EP
, nvar
);
2276 free_evalue_refs(EP
);
2280 evalue_range_reduction(EP
);
2285 static evalue
* split_sure(Polyhedron
*P
, Polyhedron
*S
,
2286 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2288 int nvar
= P
->Dimension
- exist
- nparam
;
2290 Matrix
*M
= Matrix_Alloc(exist
, S
->Dimension
+2);
2291 for (int i
= 0; i
< exist
; ++i
)
2292 value_set_si(M
->p
[i
][nvar
+i
+1], 1);
2294 S
= DomainAddRays(S
, M
, options
->MaxRays
);
2296 Polyhedron
*F
= DomainAddRays(P
, M
, options
->MaxRays
);
2297 Polyhedron
*D
= DomainDifference(F
, S
, options
->MaxRays
);
2299 D
= Disjoint_Domain(D
, 0, options
->MaxRays
);
2304 M
= Matrix_Alloc(P
->Dimension
+1-exist
, P
->Dimension
+1);
2305 for (int j
= 0; j
< nvar
; ++j
)
2306 value_set_si(M
->p
[j
][j
], 1);
2307 for (int j
= 0; j
< nparam
+1; ++j
)
2308 value_set_si(M
->p
[nvar
+j
][nvar
+exist
+j
], 1);
2309 Polyhedron
*T
= Polyhedron_Image(S
, M
, options
->MaxRays
);
2310 evalue
*EP
= barvinok_enumerate_e_with_options(T
, 0, nparam
, options
);
2315 for (Polyhedron
*Q
= D
; Q
; Q
= Q
->next
) {
2316 Polyhedron
*N
= Q
->next
;
2318 T
= DomainIntersection(P
, Q
, options
->MaxRays
);
2319 evalue
*E
= barvinok_enumerate_e_with_options(T
, exist
, nparam
, options
);
2321 free_evalue_refs(E
);
2330 static evalue
* enumerate_sure(Polyhedron
*P
,
2331 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2335 int nvar
= P
->Dimension
- exist
- nparam
;
2341 for (i
= 0; i
< exist
; ++i
) {
2342 Matrix
*M
= Matrix_Alloc(S
->NbConstraints
, S
->Dimension
+2);
2344 value_set_si(lcm
, 1);
2345 for (int j
= 0; j
< S
->NbConstraints
; ++j
) {
2346 if (value_negz_p(S
->Constraint
[j
][1+nvar
+i
]))
2348 if (value_one_p(S
->Constraint
[j
][1+nvar
+i
]))
2350 value_lcm(lcm
, S
->Constraint
[j
][1+nvar
+i
], &lcm
);
2353 for (int j
= 0; j
< S
->NbConstraints
; ++j
) {
2354 if (value_negz_p(S
->Constraint
[j
][1+nvar
+i
]))
2356 if (value_one_p(S
->Constraint
[j
][1+nvar
+i
]))
2358 value_division(f
, lcm
, S
->Constraint
[j
][1+nvar
+i
]);
2359 Vector_Scale(S
->Constraint
[j
], M
->p
[c
], f
, S
->Dimension
+2);
2360 value_subtract(M
->p
[c
][S
->Dimension
+1],
2361 M
->p
[c
][S
->Dimension
+1],
2363 value_increment(M
->p
[c
][S
->Dimension
+1],
2364 M
->p
[c
][S
->Dimension
+1]);
2368 S
= AddConstraints(M
->p
[0], c
, S
, options
->MaxRays
);
2383 fprintf(stderr
, "\nER: Sure\n");
2384 #endif /* DEBUG_ER */
2386 return split_sure(P
, S
, exist
, nparam
, options
);
2389 static evalue
* enumerate_sure2(Polyhedron
*P
,
2390 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2392 int nvar
= P
->Dimension
- exist
- nparam
;
2394 for (r
= 0; r
< P
->NbRays
; ++r
)
2395 if (value_one_p(P
->Ray
[r
][0]) &&
2396 value_one_p(P
->Ray
[r
][P
->Dimension
+1]))
2402 Matrix
*M
= Matrix_Alloc(nvar
+ 1 + nparam
, P
->Dimension
+2);
2403 for (int i
= 0; i
< nvar
; ++i
)
2404 value_set_si(M
->p
[i
][1+i
], 1);
2405 for (int i
= 0; i
< nparam
; ++i
)
2406 value_set_si(M
->p
[i
+nvar
][1+nvar
+exist
+i
], 1);
2407 Vector_Copy(P
->Ray
[r
]+1+nvar
, M
->p
[nvar
+nparam
]+1+nvar
, exist
);
2408 value_set_si(M
->p
[nvar
+nparam
][0], 1);
2409 value_set_si(M
->p
[nvar
+nparam
][P
->Dimension
+1], 1);
2410 Polyhedron
* F
= Rays2Polyhedron(M
, options
->MaxRays
);
2413 Polyhedron
*I
= DomainIntersection(F
, P
, options
->MaxRays
);
2417 fprintf(stderr
, "\nER: Sure2\n");
2418 #endif /* DEBUG_ER */
2420 return split_sure(P
, I
, exist
, nparam
, options
);
2423 static evalue
* enumerate_cyclic(Polyhedron
*P
,
2424 unsigned exist
, unsigned nparam
,
2425 evalue
* EP
, int r
, int p
, unsigned MaxRays
)
2427 int nvar
= P
->Dimension
- exist
- nparam
;
2429 /* If EP in its fractional maps only contains references
2430 * to the remainder parameter with appropriate coefficients
2431 * then we could in principle avoid adding existentially
2432 * quantified variables to the validity domains.
2433 * We'd have to replace the remainder by m { p/m }
2434 * and multiply with an appropriate factor that is one
2435 * only in the appropriate range.
2436 * This last multiplication can be avoided if EP
2437 * has a single validity domain with no (further)
2438 * constraints on the remainder parameter
2441 Matrix
*CT
= Matrix_Alloc(nparam
+1, nparam
+3);
2442 Matrix
*M
= Matrix_Alloc(1, 1+nparam
+3);
2443 for (int j
= 0; j
< nparam
; ++j
)
2445 value_set_si(CT
->p
[j
][j
], 1);
2446 value_set_si(CT
->p
[p
][nparam
+1], 1);
2447 value_set_si(CT
->p
[nparam
][nparam
+2], 1);
2448 value_set_si(M
->p
[0][1+p
], -1);
2449 value_absolute(M
->p
[0][1+nparam
], P
->Ray
[0][1+nvar
+exist
+p
]);
2450 value_set_si(M
->p
[0][1+nparam
+1], 1);
2451 Polyhedron
*CEq
= Constraints2Polyhedron(M
, 1);
2453 addeliminatedparams_enum(EP
, CT
, CEq
, MaxRays
, nparam
);
2454 Polyhedron_Free(CEq
);
2460 static void enumerate_vd_add_ray(evalue
*EP
, Matrix
*Rays
, unsigned MaxRays
)
2462 if (value_notzero_p(EP
->d
))
2465 assert(EP
->x
.p
->type
== partition
);
2466 assert(EP
->x
.p
->pos
== EVALUE_DOMAIN(EP
->x
.p
->arr
[0])->Dimension
);
2467 for (int i
= 0; i
< EP
->x
.p
->size
/2; ++i
) {
2468 Polyhedron
*D
= EVALUE_DOMAIN(EP
->x
.p
->arr
[2*i
]);
2469 Polyhedron
*N
= DomainAddRays(D
, Rays
, MaxRays
);
2470 EVALUE_SET_DOMAIN(EP
->x
.p
->arr
[2*i
], N
);
2475 static evalue
* enumerate_line(Polyhedron
*P
,
2476 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2482 fprintf(stderr
, "\nER: Line\n");
2483 #endif /* DEBUG_ER */
2485 int nvar
= P
->Dimension
- exist
- nparam
;
2487 for (i
= 0; i
< nparam
; ++i
)
2488 if (value_notzero_p(P
->Ray
[0][1+nvar
+exist
+i
]))
2491 for (j
= i
+1; j
< nparam
; ++j
)
2492 if (value_notzero_p(P
->Ray
[0][1+nvar
+exist
+i
]))
2494 assert(j
>= nparam
); // for now
2496 Matrix
*M
= Matrix_Alloc(2, P
->Dimension
+2);
2497 value_set_si(M
->p
[0][0], 1);
2498 value_set_si(M
->p
[0][1+nvar
+exist
+i
], 1);
2499 value_set_si(M
->p
[1][0], 1);
2500 value_set_si(M
->p
[1][1+nvar
+exist
+i
], -1);
2501 value_absolute(M
->p
[1][1+P
->Dimension
], P
->Ray
[0][1+nvar
+exist
+i
]);
2502 value_decrement(M
->p
[1][1+P
->Dimension
], M
->p
[1][1+P
->Dimension
]);
2503 Polyhedron
*S
= AddConstraints(M
->p
[0], 2, P
, options
->MaxRays
);
2504 evalue
*EP
= barvinok_enumerate_e_with_options(S
, exist
, nparam
, options
);
2508 return enumerate_cyclic(P
, exist
, nparam
, EP
, 0, i
, options
->MaxRays
);
2511 static int single_param_pos(Polyhedron
*P
, unsigned exist
, unsigned nparam
,
2514 int nvar
= P
->Dimension
- exist
- nparam
;
2515 if (First_Non_Zero(P
->Ray
[r
]+1, nvar
) != -1)
2517 int i
= First_Non_Zero(P
->Ray
[r
]+1+nvar
+exist
, nparam
);
2520 if (First_Non_Zero(P
->Ray
[r
]+1+nvar
+exist
+1, nparam
-i
-1) != -1)
2525 static evalue
* enumerate_remove_ray(Polyhedron
*P
, int r
,
2526 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2529 fprintf(stderr
, "\nER: RedundantRay\n");
2530 #endif /* DEBUG_ER */
2534 value_set_si(one
, 1);
2535 int len
= P
->NbRays
-1;
2536 Matrix
*M
= Matrix_Alloc(2 * len
, P
->Dimension
+2);
2537 Vector_Copy(P
->Ray
[0], M
->p
[0], r
* (P
->Dimension
+2));
2538 Vector_Copy(P
->Ray
[r
+1], M
->p
[r
], (len
-r
) * (P
->Dimension
+2));
2539 for (int j
= 0; j
< P
->NbRays
; ++j
) {
2542 Vector_Combine(P
->Ray
[j
], P
->Ray
[r
], M
->p
[len
+j
-(j
>r
)],
2543 one
, P
->Ray
[j
][P
->Dimension
+1], P
->Dimension
+2);
2546 P
= Rays2Polyhedron(M
, options
->MaxRays
);
2548 evalue
*EP
= barvinok_enumerate_e_with_options(P
, exist
, nparam
, options
);
2555 static evalue
* enumerate_redundant_ray(Polyhedron
*P
,
2556 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2558 assert(P
->NbBid
== 0);
2559 int nvar
= P
->Dimension
- exist
- nparam
;
2563 for (int r
= 0; r
< P
->NbRays
; ++r
) {
2564 if (value_notzero_p(P
->Ray
[r
][P
->Dimension
+1]))
2566 int i1
= single_param_pos(P
, exist
, nparam
, r
);
2569 for (int r2
= r
+1; r2
< P
->NbRays
; ++r2
) {
2570 if (value_notzero_p(P
->Ray
[r2
][P
->Dimension
+1]))
2572 int i2
= single_param_pos(P
, exist
, nparam
, r2
);
2578 value_division(m
, P
->Ray
[r
][1+nvar
+exist
+i1
],
2579 P
->Ray
[r2
][1+nvar
+exist
+i1
]);
2580 value_multiply(m
, m
, P
->Ray
[r2
][1+nvar
+exist
+i1
]);
2581 /* r2 divides r => r redundant */
2582 if (value_eq(m
, P
->Ray
[r
][1+nvar
+exist
+i1
])) {
2584 return enumerate_remove_ray(P
, r
, exist
, nparam
, options
);
2587 value_division(m
, P
->Ray
[r2
][1+nvar
+exist
+i1
],
2588 P
->Ray
[r
][1+nvar
+exist
+i1
]);
2589 value_multiply(m
, m
, P
->Ray
[r
][1+nvar
+exist
+i1
]);
2590 /* r divides r2 => r2 redundant */
2591 if (value_eq(m
, P
->Ray
[r2
][1+nvar
+exist
+i1
])) {
2593 return enumerate_remove_ray(P
, r2
, exist
, nparam
, options
);
2601 static Polyhedron
*upper_bound(Polyhedron
*P
,
2602 int pos
, Value
*max
, Polyhedron
**R
)
2611 for (Polyhedron
*Q
= P
; Q
; Q
= N
) {
2613 for (r
= 0; r
< P
->NbRays
; ++r
) {
2614 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1]) &&
2615 value_pos_p(P
->Ray
[r
][1+pos
]))
2618 if (r
< P
->NbRays
) {
2626 for (r
= 0; r
< P
->NbRays
; ++r
) {
2627 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1]))
2629 mpz_fdiv_q(v
, P
->Ray
[r
][1+pos
], P
->Ray
[r
][1+P
->Dimension
]);
2630 if ((!Q
->next
&& r
== 0) || value_gt(v
, *max
))
2631 value_assign(*max
, v
);
2638 static evalue
* enumerate_ray(Polyhedron
*P
,
2639 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2641 assert(P
->NbBid
== 0);
2642 int nvar
= P
->Dimension
- exist
- nparam
;
2645 for (r
= 0; r
< P
->NbRays
; ++r
)
2646 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1]))
2652 for (r2
= r
+1; r2
< P
->NbRays
; ++r2
)
2653 if (value_zero_p(P
->Ray
[r2
][P
->Dimension
+1]))
2655 if (r2
< P
->NbRays
) {
2657 return enumerate_sum(P
, exist
, nparam
, options
);
2661 fprintf(stderr
, "\nER: Ray\n");
2662 #endif /* DEBUG_ER */
2668 value_set_si(one
, 1);
2669 int i
= single_param_pos(P
, exist
, nparam
, r
);
2670 assert(i
!= -1); // for now;
2672 Matrix
*M
= Matrix_Alloc(P
->NbRays
, P
->Dimension
+2);
2673 for (int j
= 0; j
< P
->NbRays
; ++j
) {
2674 Vector_Combine(P
->Ray
[j
], P
->Ray
[r
], M
->p
[j
],
2675 one
, P
->Ray
[j
][P
->Dimension
+1], P
->Dimension
+2);
2677 Polyhedron
*S
= Rays2Polyhedron(M
, options
->MaxRays
);
2679 Polyhedron
*D
= DomainDifference(P
, S
, options
->MaxRays
);
2681 // Polyhedron_Print(stderr, P_VALUE_FMT, D);
2682 assert(value_pos_p(P
->Ray
[r
][1+nvar
+exist
+i
])); // for now
2684 D
= upper_bound(D
, nvar
+exist
+i
, &m
, &R
);
2688 M
= Matrix_Alloc(2, P
->Dimension
+2);
2689 value_set_si(M
->p
[0][0], 1);
2690 value_set_si(M
->p
[1][0], 1);
2691 value_set_si(M
->p
[0][1+nvar
+exist
+i
], -1);
2692 value_set_si(M
->p
[1][1+nvar
+exist
+i
], 1);
2693 value_assign(M
->p
[0][1+P
->Dimension
], m
);
2694 value_oppose(M
->p
[1][1+P
->Dimension
], m
);
2695 value_addto(M
->p
[1][1+P
->Dimension
], M
->p
[1][1+P
->Dimension
],
2696 P
->Ray
[r
][1+nvar
+exist
+i
]);
2697 value_decrement(M
->p
[1][1+P
->Dimension
], M
->p
[1][1+P
->Dimension
]);
2698 // Matrix_Print(stderr, P_VALUE_FMT, M);
2699 D
= AddConstraints(M
->p
[0], 2, P
, options
->MaxRays
);
2700 // Polyhedron_Print(stderr, P_VALUE_FMT, D);
2701 value_subtract(M
->p
[0][1+P
->Dimension
], M
->p
[0][1+P
->Dimension
],
2702 P
->Ray
[r
][1+nvar
+exist
+i
]);
2703 // Matrix_Print(stderr, P_VALUE_FMT, M);
2704 S
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
2705 // Polyhedron_Print(stderr, P_VALUE_FMT, S);
2708 evalue
*EP
= barvinok_enumerate_e_with_options(D
, exist
, nparam
, options
);
2713 if (value_notone_p(P
->Ray
[r
][1+nvar
+exist
+i
]))
2714 EP
= enumerate_cyclic(P
, exist
, nparam
, EP
, r
, i
, options
->MaxRays
);
2716 M
= Matrix_Alloc(1, nparam
+2);
2717 value_set_si(M
->p
[0][0], 1);
2718 value_set_si(M
->p
[0][1+i
], 1);
2719 enumerate_vd_add_ray(EP
, M
, options
->MaxRays
);
2724 evalue
*E
= barvinok_enumerate_e_with_options(S
, exist
, nparam
, options
);
2726 free_evalue_refs(E
);
2733 evalue
*ER
= enumerate_or(R
, exist
, nparam
, options
);
2735 free_evalue_refs(ER
);
2742 static evalue
* enumerate_vd(Polyhedron
**PA
,
2743 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2745 Polyhedron
*P
= *PA
;
2746 int nvar
= P
->Dimension
- exist
- nparam
;
2747 Param_Polyhedron
*PP
= NULL
;
2748 Polyhedron
*C
= Universe_Polyhedron(nparam
);
2752 PP
= Polyhedron2Param_SimplifiedDomain(&PR
,C
, options
->MaxRays
,&CEq
,&CT
);
2756 Param_Domain
*D
, *last
;
2759 for (nd
= 0, D
=PP
->D
; D
; D
=D
->next
, ++nd
)
2762 Polyhedron
**VD
= new Polyhedron_p
[nd
];
2763 Polyhedron
**fVD
= new Polyhedron_p
[nd
];
2764 for(nd
= 0, D
=PP
->D
; D
; D
=D
->next
) {
2765 Polyhedron
*rVD
= reduce_domain(D
->Domain
, CT
, CEq
, fVD
, nd
, options
);
2778 /* This doesn't seem to have any effect */
2780 Polyhedron
*CA
= align_context(VD
[0], P
->Dimension
, options
->MaxRays
);
2782 P
= DomainIntersection(P
, CA
, options
->MaxRays
);
2785 Polyhedron_Free(CA
);
2790 if (!EP
&& CT
->NbColumns
!= CT
->NbRows
) {
2791 Polyhedron
*CEqr
= DomainImage(CEq
, CT
, options
->MaxRays
);
2792 Polyhedron
*CA
= align_context(CEqr
, PR
->Dimension
, options
->MaxRays
);
2793 Polyhedron
*I
= DomainIntersection(PR
, CA
, options
->MaxRays
);
2794 Polyhedron_Free(CEqr
);
2795 Polyhedron_Free(CA
);
2797 fprintf(stderr
, "\nER: Eliminate\n");
2798 #endif /* DEBUG_ER */
2799 nparam
-= CT
->NbColumns
- CT
->NbRows
;
2800 EP
= barvinok_enumerate_e_with_options(I
, exist
, nparam
, options
);
2801 nparam
+= CT
->NbColumns
- CT
->NbRows
;
2802 addeliminatedparams_enum(EP
, CT
, CEq
, options
->MaxRays
, nparam
);
2806 Polyhedron_Free(PR
);
2809 if (!EP
&& nd
> 1) {
2811 fprintf(stderr
, "\nER: VD\n");
2812 #endif /* DEBUG_ER */
2813 for (int i
= 0; i
< nd
; ++i
) {
2814 Polyhedron
*CA
= align_context(VD
[i
], P
->Dimension
, options
->MaxRays
);
2815 Polyhedron
*I
= DomainIntersection(P
, CA
, options
->MaxRays
);
2818 EP
= barvinok_enumerate_e_with_options(I
, exist
, nparam
, options
);
2820 evalue
*E
= barvinok_enumerate_e_with_options(I
, exist
, nparam
,
2823 free_evalue_refs(E
);
2827 Polyhedron_Free(CA
);
2831 for (int i
= 0; i
< nd
; ++i
) {
2832 Polyhedron_Free(VD
[i
]);
2833 Polyhedron_Free(fVD
[i
]);
2839 if (!EP
&& nvar
== 0) {
2842 Param_Vertices
*V
, *V2
;
2843 Matrix
* M
= Matrix_Alloc(1, P
->Dimension
+2);
2845 FORALL_PVertex_in_ParamPolyhedron(V
, last
, PP
) {
2847 FORALL_PVertex_in_ParamPolyhedron(V2
, last
, PP
) {
2854 for (int i
= 0; i
< exist
; ++i
) {
2855 value_oppose(f
, V
->Vertex
->p
[i
][nparam
+1]);
2856 Vector_Combine(V
->Vertex
->p
[i
],
2858 M
->p
[0] + 1 + nvar
+ exist
,
2859 V2
->Vertex
->p
[i
][nparam
+1],
2863 for (j
= 0; j
< nparam
; ++j
)
2864 if (value_notzero_p(M
->p
[0][1+nvar
+exist
+j
]))
2868 ConstraintSimplify(M
->p
[0], M
->p
[0],
2869 P
->Dimension
+2, &f
);
2870 value_set_si(M
->p
[0][0], 0);
2871 Polyhedron
*para
= AddConstraints(M
->p
[0], 1, P
,
2874 Polyhedron_Free(para
);
2877 Polyhedron
*pos
, *neg
;
2878 value_set_si(M
->p
[0][0], 1);
2879 value_decrement(M
->p
[0][P
->Dimension
+1],
2880 M
->p
[0][P
->Dimension
+1]);
2881 neg
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
2882 value_set_si(f
, -1);
2883 Vector_Scale(M
->p
[0]+1, M
->p
[0]+1, f
,
2885 value_decrement(M
->p
[0][P
->Dimension
+1],
2886 M
->p
[0][P
->Dimension
+1]);
2887 value_decrement(M
->p
[0][P
->Dimension
+1],
2888 M
->p
[0][P
->Dimension
+1]);
2889 pos
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
2890 if (emptyQ(neg
) && emptyQ(pos
)) {
2891 Polyhedron_Free(para
);
2892 Polyhedron_Free(pos
);
2893 Polyhedron_Free(neg
);
2897 fprintf(stderr
, "\nER: Order\n");
2898 #endif /* DEBUG_ER */
2899 EP
= barvinok_enumerate_e_with_options(para
, exist
, nparam
,
2903 E
= barvinok_enumerate_e_with_options(pos
, exist
, nparam
,
2906 free_evalue_refs(E
);
2910 E
= barvinok_enumerate_e_with_options(neg
, exist
, nparam
,
2913 free_evalue_refs(E
);
2916 Polyhedron_Free(para
);
2917 Polyhedron_Free(pos
);
2918 Polyhedron_Free(neg
);
2923 } END_FORALL_PVertex_in_ParamPolyhedron
;
2926 } END_FORALL_PVertex_in_ParamPolyhedron
;
2929 /* Search for vertex coordinate to split on */
2930 /* First look for one independent of the parameters */
2931 FORALL_PVertex_in_ParamPolyhedron(V
, last
, PP
) {
2932 for (int i
= 0; i
< exist
; ++i
) {
2934 for (j
= 0; j
< nparam
; ++j
)
2935 if (value_notzero_p(V
->Vertex
->p
[i
][j
]))
2939 value_set_si(M
->p
[0][0], 1);
2940 Vector_Set(M
->p
[0]+1, 0, nvar
+exist
);
2941 Vector_Copy(V
->Vertex
->p
[i
],
2942 M
->p
[0] + 1 + nvar
+ exist
, nparam
+1);
2943 value_oppose(M
->p
[0][1+nvar
+i
],
2944 V
->Vertex
->p
[i
][nparam
+1]);
2946 Polyhedron
*pos
, *neg
;
2947 value_set_si(M
->p
[0][0], 1);
2948 value_decrement(M
->p
[0][P
->Dimension
+1],
2949 M
->p
[0][P
->Dimension
+1]);
2950 neg
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
2951 value_set_si(f
, -1);
2952 Vector_Scale(M
->p
[0]+1, M
->p
[0]+1, f
,
2954 value_decrement(M
->p
[0][P
->Dimension
+1],
2955 M
->p
[0][P
->Dimension
+1]);
2956 value_decrement(M
->p
[0][P
->Dimension
+1],
2957 M
->p
[0][P
->Dimension
+1]);
2958 pos
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
2959 if (emptyQ(neg
) || emptyQ(pos
)) {
2960 Polyhedron_Free(pos
);
2961 Polyhedron_Free(neg
);
2964 Polyhedron_Free(pos
);
2965 value_increment(M
->p
[0][P
->Dimension
+1],
2966 M
->p
[0][P
->Dimension
+1]);
2967 pos
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
2969 fprintf(stderr
, "\nER: Vertex\n");
2970 #endif /* DEBUG_ER */
2972 EP
= enumerate_or(pos
, exist
, nparam
, options
);
2977 } END_FORALL_PVertex_in_ParamPolyhedron
;
2981 /* Search for vertex coordinate to split on */
2982 /* Now look for one that depends on the parameters */
2983 FORALL_PVertex_in_ParamPolyhedron(V
, last
, PP
) {
2984 for (int i
= 0; i
< exist
; ++i
) {
2985 value_set_si(M
->p
[0][0], 1);
2986 Vector_Set(M
->p
[0]+1, 0, nvar
+exist
);
2987 Vector_Copy(V
->Vertex
->p
[i
],
2988 M
->p
[0] + 1 + nvar
+ exist
, nparam
+1);
2989 value_oppose(M
->p
[0][1+nvar
+i
],
2990 V
->Vertex
->p
[i
][nparam
+1]);
2992 Polyhedron
*pos
, *neg
;
2993 value_set_si(M
->p
[0][0], 1);
2994 value_decrement(M
->p
[0][P
->Dimension
+1],
2995 M
->p
[0][P
->Dimension
+1]);
2996 neg
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
2997 value_set_si(f
, -1);
2998 Vector_Scale(M
->p
[0]+1, M
->p
[0]+1, f
,
3000 value_decrement(M
->p
[0][P
->Dimension
+1],
3001 M
->p
[0][P
->Dimension
+1]);
3002 value_decrement(M
->p
[0][P
->Dimension
+1],
3003 M
->p
[0][P
->Dimension
+1]);
3004 pos
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
3005 if (emptyQ(neg
) || emptyQ(pos
)) {
3006 Polyhedron_Free(pos
);
3007 Polyhedron_Free(neg
);
3010 Polyhedron_Free(pos
);
3011 value_increment(M
->p
[0][P
->Dimension
+1],
3012 M
->p
[0][P
->Dimension
+1]);
3013 pos
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
3015 fprintf(stderr
, "\nER: ParamVertex\n");
3016 #endif /* DEBUG_ER */
3018 EP
= enumerate_or(pos
, exist
, nparam
, options
);
3023 } END_FORALL_PVertex_in_ParamPolyhedron
;
3031 Polyhedron_Free(CEq
);
3035 Param_Polyhedron_Free(PP
);
3041 evalue
* barvinok_enumerate_pip(Polyhedron
*P
, unsigned exist
, unsigned nparam
,
3045 barvinok_options
*options
= barvinok_options_new_with_defaults();
3046 options
->MaxRays
= MaxRays
;
3047 E
= barvinok_enumerate_pip_with_options(P
, exist
, nparam
, options
);
3048 barvinok_options_free(options
);
3053 evalue
*barvinok_enumerate_pip_with_options(Polyhedron
*P
,
3054 unsigned exist
, unsigned nparam
, struct barvinok_options
*options
)
3059 evalue
*barvinok_enumerate_pip_with_options(Polyhedron
*P
,
3060 unsigned exist
, unsigned nparam
, struct barvinok_options
*options
)
3062 int nvar
= P
->Dimension
- exist
- nparam
;
3063 evalue
*EP
= evalue_zero();
3067 fprintf(stderr
, "\nER: PIP\n");
3068 #endif /* DEBUG_ER */
3070 Polyhedron
*D
= pip_projectout(P
, nvar
, exist
, nparam
);
3071 for (Q
= D
; Q
; Q
= N
) {
3075 exist
= Q
->Dimension
- nvar
- nparam
;
3076 E
= barvinok_enumerate_e_with_options(Q
, exist
, nparam
, options
);
3079 free_evalue_refs(E
);
3088 static bool is_single(Value
*row
, int pos
, int len
)
3090 return First_Non_Zero(row
, pos
) == -1 &&
3091 First_Non_Zero(row
+pos
+1, len
-pos
-1) == -1;
3094 static evalue
* barvinok_enumerate_e_r(Polyhedron
*P
,
3095 unsigned exist
, unsigned nparam
, barvinok_options
*options
);
3098 static int er_level
= 0;
3100 evalue
* barvinok_enumerate_e_with_options(Polyhedron
*P
,
3101 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
3103 fprintf(stderr
, "\nER: level %i\n", er_level
);
3105 Polyhedron_PrintConstraints(stderr
, P_VALUE_FMT
, P
);
3106 fprintf(stderr
, "\nE %d\nP %d\n", exist
, nparam
);
3108 P
= DomainConstraintSimplify(Polyhedron_Copy(P
), options
->MaxRays
);
3109 evalue
*EP
= barvinok_enumerate_e_r(P
, exist
, nparam
, options
);
3115 evalue
* barvinok_enumerate_e_with_options(Polyhedron
*P
,
3116 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
3118 P
= DomainConstraintSimplify(Polyhedron_Copy(P
), options
->MaxRays
);
3119 evalue
*EP
= barvinok_enumerate_e_r(P
, exist
, nparam
, options
);
3125 evalue
* barvinok_enumerate_e(Polyhedron
*P
, unsigned exist
, unsigned nparam
,
3129 barvinok_options
*options
= barvinok_options_new_with_defaults();
3130 options
->MaxRays
= MaxRays
;
3131 E
= barvinok_enumerate_e_with_options(P
, exist
, nparam
, options
);
3132 barvinok_options_free(options
);
3136 static evalue
* barvinok_enumerate_e_r(Polyhedron
*P
,
3137 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
3140 Polyhedron
*U
= Universe_Polyhedron(nparam
);
3141 evalue
*EP
= barvinok_enumerate_with_options(P
, U
, options
);
3142 //char *param_name[] = {"P", "Q", "R", "S", "T" };
3143 //print_evalue(stdout, EP, param_name);
3148 int nvar
= P
->Dimension
- exist
- nparam
;
3149 int len
= P
->Dimension
+ 2;
3152 POL_ENSURE_FACETS(P
);
3153 POL_ENSURE_VERTICES(P
);
3156 return evalue_zero();
3158 if (nvar
== 0 && nparam
== 0) {
3159 evalue
*EP
= evalue_zero();
3160 barvinok_count_with_options(P
, &EP
->x
.n
, options
);
3161 if (value_pos_p(EP
->x
.n
))
3162 value_set_si(EP
->x
.n
, 1);
3167 for (r
= 0; r
< P
->NbRays
; ++r
)
3168 if (value_zero_p(P
->Ray
[r
][0]) ||
3169 value_zero_p(P
->Ray
[r
][P
->Dimension
+1])) {
3171 for (i
= 0; i
< nvar
; ++i
)
3172 if (value_notzero_p(P
->Ray
[r
][i
+1]))
3176 for (i
= nvar
+ exist
; i
< nvar
+ exist
+ nparam
; ++i
)
3177 if (value_notzero_p(P
->Ray
[r
][i
+1]))
3179 if (i
>= nvar
+ exist
+ nparam
)
3182 if (r
< P
->NbRays
) {
3183 evalue
*EP
= evalue_zero();
3184 value_set_si(EP
->x
.n
, -1);
3189 for (r
= 0; r
< P
->NbEq
; ++r
)
3190 if ((first
= First_Non_Zero(P
->Constraint
[r
]+1+nvar
, exist
)) != -1)
3193 if (First_Non_Zero(P
->Constraint
[r
]+1+nvar
+first
+1,
3194 exist
-first
-1) != -1) {
3195 Polyhedron
*T
= rotate_along(P
, r
, nvar
, exist
, options
->MaxRays
);
3197 fprintf(stderr
, "\nER: Equality\n");
3198 #endif /* DEBUG_ER */
3199 evalue
*EP
= barvinok_enumerate_e_with_options(T
, exist
-1, nparam
,
3205 fprintf(stderr
, "\nER: Fixed\n");
3206 #endif /* DEBUG_ER */
3208 return barvinok_enumerate_e_with_options(P
, exist
-1, nparam
,
3211 Polyhedron
*T
= Polyhedron_Copy(P
);
3212 SwapColumns(T
, nvar
+1, nvar
+1+first
);
3213 evalue
*EP
= barvinok_enumerate_e_with_options(T
, exist
-1, nparam
,
3221 Vector
*row
= Vector_Alloc(len
);
3222 value_set_si(row
->p
[0], 1);
3227 enum constraint
* info
= new constraint
[exist
];
3228 for (int i
= 0; i
< exist
; ++i
) {
3230 for (int l
= P
->NbEq
; l
< P
->NbConstraints
; ++l
) {
3231 if (value_negz_p(P
->Constraint
[l
][nvar
+i
+1]))
3233 bool l_parallel
= is_single(P
->Constraint
[l
]+nvar
+1, i
, exist
);
3234 for (int u
= P
->NbEq
; u
< P
->NbConstraints
; ++u
) {
3235 if (value_posz_p(P
->Constraint
[u
][nvar
+i
+1]))
3237 bool lu_parallel
= l_parallel
||
3238 is_single(P
->Constraint
[u
]+nvar
+1, i
, exist
);
3239 value_oppose(f
, P
->Constraint
[u
][nvar
+i
+1]);
3240 Vector_Combine(P
->Constraint
[l
]+1, P
->Constraint
[u
]+1, row
->p
+1,
3241 f
, P
->Constraint
[l
][nvar
+i
+1], len
-1);
3242 if (!(info
[i
] & INDEPENDENT
)) {
3244 for (j
= 0; j
< exist
; ++j
)
3245 if (j
!= i
&& value_notzero_p(row
->p
[nvar
+j
+1]))
3248 //printf("independent: i: %d, l: %d, u: %d\n", i, l, u);
3249 info
[i
] = (constraint
)(info
[i
] | INDEPENDENT
);
3252 if (info
[i
] & ALL_POS
) {
3253 value_addto(row
->p
[len
-1], row
->p
[len
-1],
3254 P
->Constraint
[l
][nvar
+i
+1]);
3255 value_addto(row
->p
[len
-1], row
->p
[len
-1], f
);
3256 value_multiply(f
, f
, P
->Constraint
[l
][nvar
+i
+1]);
3257 value_subtract(row
->p
[len
-1], row
->p
[len
-1], f
);
3258 value_decrement(row
->p
[len
-1], row
->p
[len
-1]);
3259 ConstraintSimplify(row
->p
, row
->p
, len
, &f
);
3260 value_set_si(f
, -1);
3261 Vector_Scale(row
->p
+1, row
->p
+1, f
, len
-1);
3262 value_decrement(row
->p
[len
-1], row
->p
[len
-1]);
3263 Polyhedron
*T
= AddConstraints(row
->p
, 1, P
, options
->MaxRays
);
3265 //printf("not all_pos: i: %d, l: %d, u: %d\n", i, l, u);
3266 info
[i
] = (constraint
)(info
[i
] ^ ALL_POS
);
3268 //puts("pos remainder");
3269 //Polyhedron_Print(stdout, P_VALUE_FMT, T);
3272 if (!(info
[i
] & ONE_NEG
)) {
3274 negative_test_constraint(P
->Constraint
[l
],
3276 row
->p
, nvar
+i
, len
, &f
);
3277 oppose_constraint(row
->p
, len
, &f
);
3278 Polyhedron
*T
= AddConstraints(row
->p
, 1, P
,
3281 //printf("one_neg i: %d, l: %d, u: %d\n", i, l, u);
3282 info
[i
] = (constraint
)(info
[i
] | ONE_NEG
);
3284 //puts("neg remainder");
3285 //Polyhedron_Print(stdout, P_VALUE_FMT, T);
3287 } else if (!(info
[i
] & ROT_NEG
)) {
3288 if (parallel_constraints(P
->Constraint
[l
],
3290 row
->p
, nvar
, exist
)) {
3291 negative_test_constraint7(P
->Constraint
[l
],
3293 row
->p
, nvar
, exist
,
3295 oppose_constraint(row
->p
, len
, &f
);
3296 Polyhedron
*T
= AddConstraints(row
->p
, 1, P
,
3299 // printf("rot_neg i: %d, l: %d, u: %d\n", i, l, u);
3300 info
[i
] = (constraint
)(info
[i
] | ROT_NEG
);
3303 //puts("neg remainder");
3304 //Polyhedron_Print(stdout, P_VALUE_FMT, T);
3309 if (!(info
[i
] & ALL_POS
) && (info
[i
] & (ONE_NEG
| ROT_NEG
)))
3313 if (info
[i
] & ALL_POS
)
3320 for (int i = 0; i < exist; ++i)
3321 printf("%i: %i\n", i, info[i]);
3323 for (int i
= 0; i
< exist
; ++i
)
3324 if (info
[i
] & ALL_POS
) {
3326 fprintf(stderr
, "\nER: Positive\n");
3327 #endif /* DEBUG_ER */
3329 // Maybe we should chew off some of the fat here
3330 Matrix
*M
= Matrix_Alloc(P
->Dimension
, P
->Dimension
+1);
3331 for (int j
= 0; j
< P
->Dimension
; ++j
)
3332 value_set_si(M
->p
[j
][j
+ (j
>= i
+nvar
)], 1);
3333 Polyhedron
*T
= Polyhedron_Image(P
, M
, options
->MaxRays
);
3335 evalue
*EP
= barvinok_enumerate_e_with_options(T
, exist
-1, nparam
,
3343 for (int i
= 0; i
< exist
; ++i
)
3344 if (info
[i
] & ONE_NEG
) {
3346 fprintf(stderr
, "\nER: Negative\n");
3347 #endif /* DEBUG_ER */
3352 return barvinok_enumerate_e_with_options(P
, exist
-1, nparam
,
3355 Polyhedron
*T
= Polyhedron_Copy(P
);
3356 SwapColumns(T
, nvar
+1, nvar
+1+i
);
3357 evalue
*EP
= barvinok_enumerate_e_with_options(T
, exist
-1, nparam
,
3363 for (int i
= 0; i
< exist
; ++i
)
3364 if (info
[i
] & ROT_NEG
) {
3366 fprintf(stderr
, "\nER: Rotate\n");
3367 #endif /* DEBUG_ER */
3371 Polyhedron
*T
= rotate_along(P
, r
, nvar
, exist
, options
->MaxRays
);
3372 evalue
*EP
= barvinok_enumerate_e_with_options(T
, exist
-1, nparam
,
3377 for (int i
= 0; i
< exist
; ++i
)
3378 if (info
[i
] & INDEPENDENT
) {
3379 Polyhedron
*pos
, *neg
;
3381 /* Find constraint again and split off negative part */
3383 if (SplitOnVar(P
, i
, nvar
, exist
, options
->MaxRays
,
3384 row
, f
, true, &pos
, &neg
)) {
3386 fprintf(stderr
, "\nER: Split\n");
3387 #endif /* DEBUG_ER */
3390 barvinok_enumerate_e_with_options(neg
, exist
-1, nparam
, options
);
3392 barvinok_enumerate_e_with_options(pos
, exist
, nparam
, options
);
3394 free_evalue_refs(E
);
3396 Polyhedron_Free(neg
);
3397 Polyhedron_Free(pos
);
3411 EP
= enumerate_line(P
, exist
, nparam
, options
);
3415 EP
= barvinok_enumerate_pip_with_options(P
, exist
, nparam
, options
);
3419 EP
= enumerate_redundant_ray(P
, exist
, nparam
, options
);
3423 EP
= enumerate_sure(P
, exist
, nparam
, options
);
3427 EP
= enumerate_ray(P
, exist
, nparam
, options
);
3431 EP
= enumerate_sure2(P
, exist
, nparam
, options
);
3435 F
= unfringe(P
, options
->MaxRays
);
3436 if (!PolyhedronIncludes(F
, P
)) {
3438 fprintf(stderr
, "\nER: Fringed\n");
3439 #endif /* DEBUG_ER */
3440 EP
= barvinok_enumerate_e_with_options(F
, exist
, nparam
, options
);
3447 EP
= enumerate_vd(&P
, exist
, nparam
, options
);
3452 EP
= enumerate_sum(P
, exist
, nparam
, options
);
3459 Polyhedron
*pos
, *neg
;
3460 for (i
= 0; i
< exist
; ++i
)
3461 if (SplitOnVar(P
, i
, nvar
, exist
, options
->MaxRays
,
3462 row
, f
, false, &pos
, &neg
))
3468 EP
= enumerate_or(pos
, exist
, nparam
, options
);
3481 * remove equalities that require a "compression" of the parameters
3483 static Polyhedron
*remove_more_equalities(Polyhedron
*P
, unsigned nparam
,
3484 Matrix
**CP
, unsigned MaxRays
)
3487 remove_all_equalities(&P
, NULL
, CP
, NULL
, nparam
, MaxRays
);
3494 static gen_fun
*series(Polyhedron
*P
, unsigned nparam
, barvinok_options
*options
)
3504 assert(!Polyhedron_is_infinite_param(P
, nparam
));
3505 assert(P
->NbBid
== 0);
3506 assert(Polyhedron_has_revlex_positive_rays(P
, nparam
));
3508 P
= remove_more_equalities(P
, nparam
, &CP
, options
->MaxRays
);
3509 assert(P
->NbEq
== 0);
3511 nparam
= CP
->NbColumns
-1;
3516 barvinok_count_with_options(P
, &c
, options
);
3517 gf
= new gen_fun(c
);
3521 red
= gf_base::create(Polyhedron_Project(P
, nparam
),
3522 P
->Dimension
, nparam
, options
);
3523 POL_ENSURE_VERTICES(P
);
3524 red
->start_gf(P
, options
);
3536 gen_fun
* barvinok_series_with_options(Polyhedron
*P
, Polyhedron
* C
,
3537 barvinok_options
*options
)
3540 unsigned nparam
= C
->Dimension
;
3543 CA
= align_context(C
, P
->Dimension
, options
->MaxRays
);
3544 P
= DomainIntersection(P
, CA
, options
->MaxRays
);
3545 Polyhedron_Free(CA
);
3547 gf
= series(P
, nparam
, options
);
3552 gen_fun
* barvinok_series(Polyhedron
*P
, Polyhedron
* C
, unsigned MaxRays
)
3555 barvinok_options
*options
= barvinok_options_new_with_defaults();
3556 options
->MaxRays
= MaxRays
;
3557 gf
= barvinok_series_with_options(P
, C
, options
);
3558 barvinok_options_free(options
);
3562 static Polyhedron
*skew_into_positive_orthant(Polyhedron
*D
, unsigned nparam
,
3568 for (Polyhedron
*P
= D
; P
; P
= P
->next
) {
3569 POL_ENSURE_VERTICES(P
);
3570 assert(!Polyhedron_is_infinite_param(P
, nparam
));
3571 assert(P
->NbBid
== 0);
3572 assert(Polyhedron_has_positive_rays(P
, nparam
));
3574 for (int r
= 0; r
< P
->NbRays
; ++r
) {
3575 if (value_notzero_p(P
->Ray
[r
][P
->Dimension
+1]))
3577 for (int i
= 0; i
< nparam
; ++i
) {
3579 if (value_posz_p(P
->Ray
[r
][i
+1]))
3582 M
= Matrix_Alloc(D
->Dimension
+1, D
->Dimension
+1);
3583 for (int i
= 0; i
< D
->Dimension
+1; ++i
)
3584 value_set_si(M
->p
[i
][i
], 1);
3586 Inner_Product(P
->Ray
[r
]+1, M
->p
[i
], D
->Dimension
+1, &tmp
);
3587 if (value_posz_p(tmp
))
3590 for (j
= P
->Dimension
- nparam
; j
< P
->Dimension
; ++j
)
3591 if (value_pos_p(P
->Ray
[r
][j
+1]))
3593 assert(j
< P
->Dimension
);
3594 value_pdivision(tmp
, P
->Ray
[r
][j
+1], P
->Ray
[r
][i
+1]);
3595 value_subtract(M
->p
[i
][j
], M
->p
[i
][j
], tmp
);
3601 D
= DomainImage(D
, M
, MaxRays
);
3607 gen_fun
* barvinok_enumerate_union_series_with_options(Polyhedron
*D
, Polyhedron
* C
,
3608 barvinok_options
*options
)
3610 Polyhedron
*conv
, *D2
;
3612 gen_fun
*gf
= NULL
, *gf2
;
3613 unsigned nparam
= C
->Dimension
;
3618 CA
= align_context(C
, D
->Dimension
, options
->MaxRays
);
3619 D
= DomainIntersection(D
, CA
, options
->MaxRays
);
3620 Polyhedron_Free(CA
);
3622 D2
= skew_into_positive_orthant(D
, nparam
, options
->MaxRays
);
3623 for (Polyhedron
*P
= D2
; P
; P
= P
->next
) {
3624 assert(P
->Dimension
== D2
->Dimension
);
3627 P_gf
= series(Polyhedron_Copy(P
), nparam
, options
);
3631 gf
->add_union(P_gf
, options
);
3635 /* we actually only need the convex union of the parameter space
3636 * but the reducer classes currently expect a polyhedron in
3637 * the combined space
3639 Polyhedron_Free(gf
->context
);
3640 gf
->context
= DomainConvex(D2
, options
->MaxRays
);
3642 gf2
= gf
->summate(D2
->Dimension
- nparam
, options
);
3651 gen_fun
* barvinok_enumerate_union_series(Polyhedron
*D
, Polyhedron
* C
,
3655 barvinok_options
*options
= barvinok_options_new_with_defaults();
3656 options
->MaxRays
= MaxRays
;
3657 gf
= barvinok_enumerate_union_series_with_options(D
, C
, options
);
3658 barvinok_options_free(options
);
3662 evalue
* barvinok_enumerate_union(Polyhedron
*D
, Polyhedron
* C
, unsigned MaxRays
)
3665 gen_fun
*gf
= barvinok_enumerate_union_series(D
, C
, MaxRays
);