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"
38 using std::ostringstream
;
40 #define ALLOC(t,p) p = (t*)malloc(sizeof(*p))
48 dpoly_n(int d
, ZZ
& degree_0
, ZZ
& degree_1
, int offset
= 0) {
52 zz2value(degree_0
, d0
);
53 zz2value(degree_1
, d1
);
54 coeff
= Matrix_Alloc(d
+1, d
+1+1);
55 value_set_si(coeff
->p
[0][0], 1);
56 value_set_si(coeff
->p
[0][d
+1], 1);
57 for (int i
= 1; i
<= d
; ++i
) {
58 value_multiply(coeff
->p
[i
][0], coeff
->p
[i
-1][0], d0
);
59 Vector_Combine(coeff
->p
[i
-1], coeff
->p
[i
-1]+1, coeff
->p
[i
]+1,
61 value_set_si(coeff
->p
[i
][d
+1], i
);
62 value_multiply(coeff
->p
[i
][d
+1], coeff
->p
[i
][d
+1], coeff
->p
[i
-1][d
+1]);
63 value_decrement(d0
, d0
);
68 void div(dpoly
& d
, Vector
*count
, ZZ
& sign
) {
69 int len
= coeff
->NbRows
;
70 Matrix
* c
= Matrix_Alloc(coeff
->NbRows
, coeff
->NbColumns
);
73 for (int i
= 0; i
< len
; ++i
) {
74 Vector_Copy(coeff
->p
[i
], c
->p
[i
], len
+1);
75 for (int j
= 1; j
<= i
; ++j
) {
76 zz2value(d
.coeff
[j
], tmp
);
77 value_multiply(tmp
, tmp
, c
->p
[i
][len
]);
78 value_oppose(tmp
, tmp
);
79 Vector_Combine(c
->p
[i
], c
->p
[i
-j
], c
->p
[i
],
80 c
->p
[i
-j
][len
], tmp
, len
);
81 value_multiply(c
->p
[i
][len
], c
->p
[i
][len
], c
->p
[i
-j
][len
]);
83 zz2value(d
.coeff
[0], tmp
);
84 value_multiply(c
->p
[i
][len
], c
->p
[i
][len
], tmp
);
87 value_set_si(tmp
, -1);
88 Vector_Scale(c
->p
[len
-1], count
->p
, tmp
, len
);
89 value_assign(count
->p
[len
], c
->p
[len
-1][len
]);
91 Vector_Copy(c
->p
[len
-1], count
->p
, len
+1);
92 Vector_Normalize(count
->p
, len
+1);
100 * Searches for a vector that is not orthogonal to any
101 * of the rays in rays.
103 static void nonorthog(mat_ZZ
& rays
, vec_ZZ
& lambda
)
105 int dim
= rays
.NumCols();
107 lambda
.SetLength(dim
);
111 for (int i
= 2; !found
&& i
<= 50*dim
; i
+=4) {
112 for (int j
= 0; j
< MAX_TRY
; ++j
) {
113 for (int k
= 0; k
< dim
; ++k
) {
114 int r
= random_int(i
)+2;
115 int v
= (2*(r
%2)-1) * (r
>> 1);
119 for (; k
< rays
.NumRows(); ++k
)
120 if (lambda
* rays
[k
] == 0)
122 if (k
== rays
.NumRows()) {
131 static void add_rays(mat_ZZ
& rays
, Polyhedron
*i
, int *r
, int nvar
= -1,
134 unsigned dim
= i
->Dimension
;
137 for (int k
= 0; k
< i
->NbRays
; ++k
) {
138 if (!value_zero_p(i
->Ray
[k
][dim
+1]))
140 if (!all
&& nvar
!= dim
&& First_Non_Zero(i
->Ray
[k
]+1, nvar
) == -1)
142 values2zz(i
->Ray
[k
]+1, rays
[(*r
)++], nvar
);
146 static void mask_r(Matrix
*f
, int nr
, Vector
*lcm
, int p
, Vector
*val
, evalue
*ev
)
148 unsigned nparam
= lcm
->Size
;
151 Vector
* prod
= Vector_Alloc(f
->NbRows
);
152 Matrix_Vector_Product(f
, val
->p
, prod
->p
);
154 for (int i
= 0; i
< nr
; ++i
) {
155 value_modulus(prod
->p
[i
], prod
->p
[i
], f
->p
[i
][nparam
+1]);
156 isint
&= value_zero_p(prod
->p
[i
]);
158 value_set_si(ev
->d
, 1);
160 value_set_si(ev
->x
.n
, isint
);
167 if (value_one_p(lcm
->p
[p
]))
168 mask_r(f
, nr
, lcm
, p
+1, val
, ev
);
170 value_assign(tmp
, lcm
->p
[p
]);
171 value_set_si(ev
->d
, 0);
172 ev
->x
.p
= new_enode(periodic
, VALUE_TO_INT(tmp
), p
+1);
174 value_decrement(tmp
, tmp
);
175 value_assign(val
->p
[p
], tmp
);
176 mask_r(f
, nr
, lcm
, p
+1, val
, &ev
->x
.p
->arr
[VALUE_TO_INT(tmp
)]);
177 } while (value_pos_p(tmp
));
182 static void mask_fractional(Matrix
*f
, evalue
*factor
)
184 int nr
= f
->NbRows
, nc
= f
->NbColumns
;
187 for (n
= 0; n
< nr
&& value_notzero_p(f
->p
[n
][nc
-1]); ++n
)
188 if (value_notone_p(f
->p
[n
][nc
-1]) &&
189 value_notmone_p(f
->p
[n
][nc
-1]))
203 value_set_si(EV
.x
.n
, 1);
205 for (n
= 0; n
< nr
; ++n
) {
206 value_assign(m
, f
->p
[n
][nc
-1]);
207 if (value_one_p(m
) || value_mone_p(m
))
210 int j
= normal_mod(f
->p
[n
], nc
-1, &m
);
212 free_evalue_refs(factor
);
213 value_init(factor
->d
);
214 evalue_set_si(factor
, 0, 1);
218 values2zz(f
->p
[n
], row
, nc
-1);
221 if (j
< (nc
-1)-1 && row
[j
] > g
/2) {
222 for (int k
= j
; k
< (nc
-1); ++k
)
228 value_set_si(EP
.d
, 0);
229 EP
.x
.p
= new_enode(relation
, 2, 0);
230 value_clear(EP
.x
.p
->arr
[1].d
);
231 EP
.x
.p
->arr
[1] = *factor
;
232 evalue
*ev
= &EP
.x
.p
->arr
[0];
233 value_set_si(ev
->d
, 0);
234 ev
->x
.p
= new_enode(fractional
, 3, -1);
235 evalue_set_si(&ev
->x
.p
->arr
[1], 0, 1);
236 evalue_set_si(&ev
->x
.p
->arr
[2], 1, 1);
237 evalue
*E
= multi_monom(row
);
238 value_assign(EV
.d
, m
);
240 value_clear(ev
->x
.p
->arr
[0].d
);
241 ev
->x
.p
->arr
[0] = *E
;
247 free_evalue_refs(&EV
);
253 static void mask_table(Matrix
*f
, evalue
*factor
)
255 int nr
= f
->NbRows
, nc
= f
->NbColumns
;
258 for (n
= 0; n
< nr
&& value_notzero_p(f
->p
[n
][nc
-1]); ++n
)
259 if (value_notone_p(f
->p
[n
][nc
-1]) &&
260 value_notmone_p(f
->p
[n
][nc
-1]))
268 unsigned np
= nc
- 2;
269 Vector
*lcm
= Vector_Alloc(np
);
270 Vector
*val
= Vector_Alloc(nc
);
271 Vector_Set(val
->p
, 0, nc
);
272 value_set_si(val
->p
[np
], 1);
273 Vector_Set(lcm
->p
, 1, np
);
274 for (n
= 0; n
< nr
; ++n
) {
275 if (value_one_p(f
->p
[n
][nc
-1]) ||
276 value_mone_p(f
->p
[n
][nc
-1]))
278 for (int j
= 0; j
< np
; ++j
)
279 if (value_notzero_p(f
->p
[n
][j
])) {
280 Gcd(f
->p
[n
][j
], f
->p
[n
][nc
-1], &tmp
);
281 value_division(tmp
, f
->p
[n
][nc
-1], tmp
);
282 value_lcm(tmp
, lcm
->p
[j
], &lcm
->p
[j
]);
287 mask_r(f
, nr
, lcm
, 0, val
, &EP
);
292 free_evalue_refs(&EP
);
295 static void mask(Matrix
*f
, evalue
*factor
, barvinok_options
*options
)
297 if (options
->lookup_table
)
298 mask_table(f
, factor
);
300 mask_fractional(f
, factor
);
303 /* This structure encodes the power of the term in a rational generating function.
305 * Either E == NULL or constant = 0
306 * If E != NULL, then the power is E
307 * If E == NULL, then the power is coeff * param[pos] + constant
316 /* Returns the power of (t+1) in the term of a rational generating function,
317 * i.e., the scalar product of the actual lattice point and lambda.
318 * The lattice point is the unique lattice point in the fundamental parallelepiped
319 * of the unimodual cone i shifted to the parametric vertex V.
321 * PD is the parameter domain, which, if != NULL, may be used to simply the
322 * resulting expression.
324 * The result is returned in term.
326 void lattice_point(Param_Vertices
* V
, const mat_ZZ
& rays
, vec_ZZ
& lambda
,
327 term_info
* term
, Polyhedron
*PD
, barvinok_options
*options
)
329 unsigned nparam
= V
->Vertex
->NbColumns
- 2;
330 unsigned dim
= rays
.NumCols();
332 vertex
.SetDims(V
->Vertex
->NbRows
, nparam
+1);
336 value_set_si(lcm
, 1);
337 for (int j
= 0; j
< V
->Vertex
->NbRows
; ++j
) {
338 value_lcm(lcm
, V
->Vertex
->p
[j
][nparam
+1], &lcm
);
340 if (value_notone_p(lcm
)) {
341 Matrix
* mv
= Matrix_Alloc(dim
, nparam
+1);
342 for (int j
= 0 ; j
< dim
; ++j
) {
343 value_division(tmp
, lcm
, V
->Vertex
->p
[j
][nparam
+1]);
344 Vector_Scale(V
->Vertex
->p
[j
], mv
->p
[j
], tmp
, nparam
+1);
347 term
->E
= lattice_point(rays
, lambda
, mv
, lcm
, PD
, options
);
355 for (int i
= 0; i
< V
->Vertex
->NbRows
; ++i
) {
356 assert(value_one_p(V
->Vertex
->p
[i
][nparam
+1])); // for now
357 values2zz(V
->Vertex
->p
[i
], vertex
[i
], nparam
+1);
361 num
= lambda
* vertex
;
365 for (int j
= 0; j
< nparam
; ++j
)
371 term
->E
= multi_monom(num
);
375 term
->constant
= num
[nparam
];
378 term
->coeff
= num
[p
];
386 struct counter
: public np_base
{
396 counter(unsigned dim
) : np_base(dim
) {
401 virtual void init(Polyhedron
*P
) {
402 randomvector(P
, lambda
, dim
);
405 virtual void reset() {
406 mpq_set_si(count
, 0, 0);
413 virtual void handle(const mat_ZZ
& rays
, Value
*vertex
, const QQ
& c
,
414 unsigned long det
, int *closed
, barvinok_options
*options
);
415 virtual void get_count(Value
*result
) {
416 assert(value_one_p(&count
[0]._mp_den
));
417 value_assign(*result
, &count
[0]._mp_num
);
421 void counter::handle(const mat_ZZ
& rays
, Value
*V
, const QQ
& c
, unsigned long det
,
422 int *closed
, barvinok_options
*options
)
424 for (int k
= 0; k
< dim
; ++k
) {
425 if (lambda
* rays
[k
] == 0)
430 assert(c
.n
== 1 || c
.n
== -1);
433 lattice_point(V
, rays
, vertex
, det
, closed
);
434 num
= vertex
* lambda
;
437 normalize(sign
, offset
, den
);
440 dpoly
d(dim
, num
[0]);
441 for (int k
= 1; k
< num
.length(); ++k
) {
443 dpoly
term(dim
, num
[k
]);
446 dpoly
n(dim
, den
[0], 1);
447 for (int k
= 1; k
< dim
; ++k
) {
448 dpoly
fact(dim
, den
[k
], 1);
451 d
.div(n
, count
, sign
);
454 struct bfe_term
: public bfc_term_base
{
455 vector
<evalue
*> factors
;
457 bfe_term(int len
) : bfc_term_base(len
) {
461 for (int i
= 0; i
< factors
.size(); ++i
) {
464 free_evalue_refs(factors
[i
]);
470 static void print_int_vector(int *v
, int len
, char *name
)
472 cerr
<< name
<< endl
;
473 for (int j
= 0; j
< len
; ++j
) {
479 static void print_bfc_terms(mat_ZZ
& factors
, bfc_vec
& v
)
482 cerr
<< "factors" << endl
;
483 cerr
<< factors
<< endl
;
484 for (int i
= 0; i
< v
.size(); ++i
) {
485 cerr
<< "term: " << i
<< endl
;
486 print_int_vector(v
[i
]->powers
, factors
.NumRows(), "powers");
487 cerr
<< "terms" << endl
;
488 cerr
<< v
[i
]->terms
<< endl
;
489 bfc_term
* bfct
= static_cast<bfc_term
*>(v
[i
]);
490 cerr
<< bfct
->c
<< endl
;
494 static void print_bfe_terms(mat_ZZ
& factors
, bfc_vec
& v
)
497 cerr
<< "factors" << endl
;
498 cerr
<< factors
<< endl
;
499 for (int i
= 0; i
< v
.size(); ++i
) {
500 cerr
<< "term: " << i
<< endl
;
501 print_int_vector(v
[i
]->powers
, factors
.NumRows(), "powers");
502 cerr
<< "terms" << endl
;
503 cerr
<< v
[i
]->terms
<< endl
;
504 bfe_term
* bfet
= static_cast<bfe_term
*>(v
[i
]);
505 for (int j
= 0; j
< v
[i
]->terms
.NumRows(); ++j
) {
506 char * test
[] = {"a", "b"};
507 print_evalue(stderr
, bfet
->factors
[j
], test
);
508 fprintf(stderr
, "\n");
513 struct bfcounter
: public bfcounter_base
{
516 bfcounter(unsigned dim
) : bfcounter_base(dim
) {
523 virtual void base(mat_ZZ
& factors
, bfc_vec
& v
);
524 virtual void get_count(Value
*result
) {
525 assert(value_one_p(&count
[0]._mp_den
));
526 value_assign(*result
, &count
[0]._mp_num
);
530 void bfcounter::base(mat_ZZ
& factors
, bfc_vec
& v
)
532 unsigned nf
= factors
.NumRows();
534 for (int i
= 0; i
< v
.size(); ++i
) {
535 bfc_term
* bfct
= static_cast<bfc_term
*>(v
[i
]);
537 // factor is always positive, so we always
539 for (int k
= 0; k
< nf
; ++k
)
540 total_power
+= v
[i
]->powers
[k
];
543 for (j
= 0; j
< nf
; ++j
)
544 if (v
[i
]->powers
[j
] > 0)
547 dpoly
D(total_power
, factors
[j
][0], 1);
548 for (int k
= 1; k
< v
[i
]->powers
[j
]; ++k
) {
549 dpoly
fact(total_power
, factors
[j
][0], 1);
553 for (int k
= 0; k
< v
[i
]->powers
[j
]; ++k
) {
554 dpoly
fact(total_power
, factors
[j
][0], 1);
558 for (int k
= 0; k
< v
[i
]->terms
.NumRows(); ++k
) {
559 dpoly
n(total_power
, v
[i
]->terms
[k
][0]);
560 mpq_set_si(tcount
, 0, 1);
561 n
.div(D
, tcount
, one
);
563 bfct
->c
[k
].n
= -bfct
->c
[k
].n
;
564 zz2value(bfct
->c
[k
].n
, tn
);
565 zz2value(bfct
->c
[k
].d
, td
);
567 mpz_mul(mpq_numref(tcount
), mpq_numref(tcount
), tn
);
568 mpz_mul(mpq_denref(tcount
), mpq_denref(tcount
), td
);
569 mpq_canonicalize(tcount
);
570 mpq_add(count
, count
, tcount
);
577 /* Check whether the polyhedron is unbounded and if so,
578 * check whether it has any (and therefore an infinite number of)
580 * If one of the vertices is integer, then we are done.
581 * Otherwise, transform the polyhedron such that one of the rays
582 * is the first unit vector and cut it off at a height that ensures
583 * that if the whole polyhedron has any points, then the remaining part
584 * has integer points. In particular we add the largest coefficient
585 * of a ray to the highest vertex (rounded up).
587 static bool Polyhedron_is_infinite(Polyhedron
*P
, Value
* result
,
588 barvinok_options
*options
)
600 for (; r
< P
->NbRays
; ++r
)
601 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1]))
603 if (P
->NbBid
== 0 && r
== P
->NbRays
)
606 if (options
->count_sample_infinite
) {
609 sample
= Polyhedron_Sample(P
, options
);
611 value_set_si(*result
, 0);
613 value_set_si(*result
, -1);
619 for (int i
= 0; i
< P
->NbRays
; ++i
)
620 if (value_one_p(P
->Ray
[i
][1+P
->Dimension
])) {
621 value_set_si(*result
, -1);
626 v
= Vector_Alloc(P
->Dimension
+1);
627 Vector_Gcd(P
->Ray
[r
]+1, P
->Dimension
, &g
);
628 Vector_AntiScale(P
->Ray
[r
]+1, v
->p
, g
, P
->Dimension
+1);
629 M
= unimodular_complete(v
);
630 value_set_si(M
->p
[P
->Dimension
][P
->Dimension
], 1);
633 P
= Polyhedron_Preimage(P
, M2
, 0);
642 value_set_si(size
, 0);
644 for (int i
= 0; i
< P
->NbBid
; ++i
) {
645 value_absolute(tmp
, P
->Ray
[i
][1]);
646 if (value_gt(tmp
, size
))
647 value_assign(size
, tmp
);
649 for (int i
= P
->NbBid
; i
< P
->NbRays
; ++i
) {
650 if (value_zero_p(P
->Ray
[i
][P
->Dimension
+1])) {
651 if (value_gt(P
->Ray
[i
][1], size
))
652 value_assign(size
, P
->Ray
[i
][1]);
655 mpz_cdiv_q(tmp
, P
->Ray
[i
][1], P
->Ray
[i
][P
->Dimension
+1]);
656 if (first
|| value_gt(tmp
, offset
)) {
657 value_assign(offset
, tmp
);
661 value_addto(offset
, offset
, size
);
665 v
= Vector_Alloc(P
->Dimension
+2);
666 value_set_si(v
->p
[0], 1);
667 value_set_si(v
->p
[1], -1);
668 value_assign(v
->p
[1+P
->Dimension
], offset
);
669 R
= AddConstraints(v
->p
, 1, P
, options
->MaxRays
);
677 barvinok_count_with_options(P
, &c
, options
);
680 value_set_si(*result
, 0);
682 value_set_si(*result
, -1);
688 typedef Polyhedron
* Polyhedron_p
;
690 static void barvinok_count_f(Polyhedron
*P
, Value
* result
,
691 barvinok_options
*options
);
693 void barvinok_count_with_options(Polyhedron
*P
, Value
* result
,
694 struct barvinok_options
*options
)
699 bool infinite
= false;
703 "barvinok_count: input is a union; only first polyhedron is counted\n");
706 value_set_si(*result
, 0);
712 P
= remove_equalities(P
);
713 P
= DomainConstraintSimplify(P
, options
->MaxRays
);
717 } while (!emptyQ(P
) && P
->NbEq
!= 0);
720 value_set_si(*result
, 0);
725 if (Polyhedron_is_infinite(P
, result
, options
)) {
730 if (P
->Dimension
== 0) {
731 /* Test whether the constraints are satisfied */
732 POL_ENSURE_VERTICES(P
);
733 value_set_si(*result
, !emptyQ(P
));
738 Q
= Polyhedron_Factor(P
, 0, NULL
, options
->MaxRays
);
746 barvinok_count_f(P
, result
, options
);
747 if (value_neg_p(*result
))
749 if (Q
&& P
->next
&& value_notzero_p(*result
)) {
753 for (Q
= P
->next
; Q
; Q
= Q
->next
) {
754 barvinok_count_f(Q
, &factor
, options
);
755 if (value_neg_p(factor
)) {
758 } else if (Q
->next
&& value_zero_p(factor
)) {
759 value_set_si(*result
, 0);
762 value_multiply(*result
, *result
, factor
);
771 value_set_si(*result
, -1);
774 void barvinok_count(Polyhedron
*P
, Value
* result
, unsigned NbMaxCons
)
776 barvinok_options
*options
= barvinok_options_new_with_defaults();
777 options
->MaxRays
= NbMaxCons
;
778 barvinok_count_with_options(P
, result
, options
);
779 barvinok_options_free(options
);
782 static void barvinok_count_f(Polyhedron
*P
, Value
* result
,
783 barvinok_options
*options
)
786 value_set_si(*result
, 0);
790 if (P
->Dimension
== 1)
791 return Line_Length(P
, result
);
793 int c
= P
->NbConstraints
;
794 POL_ENSURE_FACETS(P
);
795 if (c
!= P
->NbConstraints
|| P
->NbEq
!= 0)
796 return barvinok_count_with_options(P
, result
, options
);
798 POL_ENSURE_VERTICES(P
);
800 if (Polyhedron_is_infinite(P
, result
, options
))
804 if (options
->incremental_specialization
== 2)
805 cnt
= new bfcounter(P
->Dimension
);
806 else if (options
->incremental_specialization
== 1)
807 cnt
= new icounter(P
->Dimension
);
809 cnt
= new counter(P
->Dimension
);
810 cnt
->start(P
, options
);
812 cnt
->get_count(result
);
816 static void uni_polynom(int param
, Vector
*c
, evalue
*EP
)
818 unsigned dim
= c
->Size
-2;
820 value_set_si(EP
->d
,0);
821 EP
->x
.p
= new_enode(polynomial
, dim
+1, param
+1);
822 for (int j
= 0; j
<= dim
; ++j
)
823 evalue_set(&EP
->x
.p
->arr
[j
], c
->p
[j
], c
->p
[dim
+1]);
826 static void multi_polynom(Vector
*c
, evalue
* X
, evalue
*EP
)
828 unsigned dim
= c
->Size
-2;
832 evalue_set(&EC
, c
->p
[dim
], c
->p
[dim
+1]);
835 evalue_set(EP
, c
->p
[dim
], c
->p
[dim
+1]);
837 for (int i
= dim
-1; i
>= 0; --i
) {
839 value_assign(EC
.x
.n
, c
->p
[i
]);
842 free_evalue_refs(&EC
);
845 Polyhedron
*unfringe (Polyhedron
*P
, unsigned MaxRays
)
847 int len
= P
->Dimension
+2;
848 Polyhedron
*T
, *R
= P
;
851 Vector
*row
= Vector_Alloc(len
);
852 value_set_si(row
->p
[0], 1);
854 R
= DomainConstraintSimplify(Polyhedron_Copy(P
), MaxRays
);
856 Matrix
*M
= Matrix_Alloc(2, len
-1);
857 value_set_si(M
->p
[1][len
-2], 1);
858 for (int v
= 0; v
< P
->Dimension
; ++v
) {
859 value_set_si(M
->p
[0][v
], 1);
860 Polyhedron
*I
= Polyhedron_Image(R
, M
, 2+1);
861 value_set_si(M
->p
[0][v
], 0);
862 for (int r
= 0; r
< I
->NbConstraints
; ++r
) {
863 if (value_zero_p(I
->Constraint
[r
][0]))
865 if (value_zero_p(I
->Constraint
[r
][1]))
867 if (value_one_p(I
->Constraint
[r
][1]))
869 if (value_mone_p(I
->Constraint
[r
][1]))
871 value_absolute(g
, I
->Constraint
[r
][1]);
872 Vector_Set(row
->p
+1, 0, len
-2);
873 value_division(row
->p
[1+v
], I
->Constraint
[r
][1], g
);
874 mpz_fdiv_q(row
->p
[len
-1], I
->Constraint
[r
][2], g
);
876 R
= AddConstraints(row
->p
, 1, R
, MaxRays
);
888 /* Check whether all rays point in the positive directions
891 static bool Polyhedron_has_positive_rays(Polyhedron
*P
, unsigned nparam
)
894 for (r
= 0; r
< P
->NbRays
; ++r
)
895 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1])) {
897 for (i
= P
->Dimension
- nparam
; i
< P
->Dimension
; ++i
)
898 if (value_neg_p(P
->Ray
[r
][i
+1]))
904 typedef evalue
* evalue_p
;
906 struct enumerator_base
{
910 vertex_decomposer
*vpd
;
912 enumerator_base(unsigned dim
, vertex_decomposer
*vpd
)
917 vE
= new evalue_p
[vpd
->nbV
];
918 for (int j
= 0; j
< vpd
->nbV
; ++j
)
922 evalue_set_si(&mone
, -1, 1);
925 void decompose_at(Param_Vertices
*V
, int _i
, barvinok_options
*options
) {
929 value_init(vE
[_i
]->d
);
930 evalue_set_si(vE
[_i
], 0, 1);
932 vpd
->decompose_at_vertex(V
, _i
, options
);
935 virtual ~enumerator_base() {
936 for (int j
= 0; j
< vpd
->nbV
; ++j
)
938 free_evalue_refs(vE
[j
]);
943 free_evalue_refs(&mone
);
946 static enumerator_base
*create(Polyhedron
*P
, unsigned dim
, unsigned nbV
,
947 barvinok_options
*options
);
950 struct enumerator
: public signed_cone_consumer
, public vertex_decomposer
,
951 public enumerator_base
{
959 enumerator(Polyhedron
*P
, unsigned dim
, unsigned nbV
) :
960 vertex_decomposer(P
, nbV
, *this), enumerator_base(dim
, this) {
963 randomvector(P
, lambda
, dim
);
965 c
= Vector_Alloc(dim
+2);
975 virtual void handle(const signed_cone
& sc
, barvinok_options
*options
);
978 void enumerator::handle(const signed_cone
& sc
, barvinok_options
*options
)
983 assert(sc
.rays
.NumRows() == dim
);
984 for (int k
= 0; k
< dim
; ++k
) {
985 if (lambda
* sc
.rays
[k
] == 0)
991 lattice_point(V
, sc
.rays
, lambda
, &num
, 0, options
);
992 den
= sc
.rays
* lambda
;
993 normalize(sign
, num
.constant
, den
);
995 dpoly
n(dim
, den
[0], 1);
996 for (int k
= 1; k
< dim
; ++k
) {
997 dpoly
fact(dim
, den
[k
], 1);
1000 if (num
.E
!= NULL
) {
1001 ZZ
one(INIT_VAL
, 1);
1002 dpoly_n
d(dim
, num
.constant
, one
);
1005 multi_polynom(c
, num
.E
, &EV
);
1006 eadd(&EV
, vE
[vert
]);
1007 free_evalue_refs(&EV
);
1008 free_evalue_refs(num
.E
);
1010 } else if (num
.pos
!= -1) {
1011 dpoly_n
d(dim
, num
.constant
, num
.coeff
);
1014 uni_polynom(num
.pos
, c
, &EV
);
1015 eadd(&EV
, vE
[vert
]);
1016 free_evalue_refs(&EV
);
1018 mpq_set_si(count
, 0, 1);
1019 dpoly
d(dim
, num
.constant
);
1020 d
.div(n
, count
, sign
);
1023 evalue_set(&EV
, &count
[0]._mp_num
, &count
[0]._mp_den
);
1024 eadd(&EV
, vE
[vert
]);
1025 free_evalue_refs(&EV
);
1029 struct ienumerator_base
: enumerator_base
{
1032 ienumerator_base(unsigned dim
, vertex_decomposer
*vpd
) :
1033 enumerator_base(dim
,vpd
) {
1034 E_vertex
= new evalue_p
[dim
];
1037 virtual ~ienumerator_base() {
1041 evalue
*E_num(int i
, int d
) {
1042 return E_vertex
[i
+ (dim
-d
)];
1051 cumulator(evalue
*factor
, evalue
*v
, dpoly_r
*r
) :
1052 factor(factor
), v(v
), r(r
) {}
1054 void cumulate(barvinok_options
*options
);
1056 virtual void add_term(const vector
<int>& powers
, evalue
*f2
) = 0;
1057 virtual ~cumulator() {}
1060 void cumulator::cumulate(barvinok_options
*options
)
1062 evalue cum
; // factor * 1 * E_num[0]/1 * (E_num[0]-1)/2 *...
1064 evalue t
; // E_num[0] - (m-1)
1068 if (options
->lookup_table
) {
1070 evalue_set_si(&mone
, -1, 1);
1074 evalue_copy(&cum
, factor
);
1077 value_set_si(f
.d
, 1);
1078 value_set_si(f
.x
.n
, 1);
1082 if (!options
->lookup_table
) {
1083 for (cst
= &t
; value_zero_p(cst
->d
); ) {
1084 if (cst
->x
.p
->type
== fractional
)
1085 cst
= &cst
->x
.p
->arr
[1];
1087 cst
= &cst
->x
.p
->arr
[0];
1091 for (int m
= 0; m
< r
->len
; ++m
) {
1094 value_set_si(f
.d
, m
);
1096 if (!options
->lookup_table
)
1097 value_subtract(cst
->x
.n
, cst
->x
.n
, cst
->d
);
1103 dpoly_r_term_list
& current
= r
->c
[r
->len
-1-m
];
1104 dpoly_r_term_list::iterator j
;
1105 for (j
= current
.begin(); j
!= current
.end(); ++j
) {
1106 if ((*j
)->coeff
== 0)
1108 evalue
*f2
= new evalue
;
1110 value_init(f2
->x
.n
);
1111 zz2value((*j
)->coeff
, f2
->x
.n
);
1112 zz2value(r
->denom
, f2
->d
);
1115 add_term((*j
)->powers
, f2
);
1118 free_evalue_refs(&f
);
1119 free_evalue_refs(&t
);
1120 free_evalue_refs(&cum
);
1121 if (options
->lookup_table
)
1122 free_evalue_refs(&mone
);
1125 struct E_poly_term
{
1130 struct ie_cum
: public cumulator
{
1131 vector
<E_poly_term
*> terms
;
1133 ie_cum(evalue
*factor
, evalue
*v
, dpoly_r
*r
) : cumulator(factor
, v
, r
) {}
1135 virtual void add_term(const vector
<int>& powers
, evalue
*f2
);
1138 void ie_cum::add_term(const vector
<int>& powers
, evalue
*f2
)
1141 for (k
= 0; k
< terms
.size(); ++k
) {
1142 if (terms
[k
]->powers
== powers
) {
1143 eadd(f2
, terms
[k
]->E
);
1144 free_evalue_refs(f2
);
1149 if (k
>= terms
.size()) {
1150 E_poly_term
*ET
= new E_poly_term
;
1151 ET
->powers
= powers
;
1153 terms
.push_back(ET
);
1157 struct ienumerator
: public signed_cone_consumer
, public vertex_decomposer
,
1158 public ienumerator_base
{
1164 ienumerator(Polyhedron
*P
, unsigned dim
, unsigned nbV
) :
1165 vertex_decomposer(P
, nbV
, *this), ienumerator_base(dim
, this) {
1166 vertex
.SetDims(1, dim
);
1168 den
.SetDims(dim
, dim
);
1176 virtual void handle(const signed_cone
& sc
, barvinok_options
*options
);
1177 void reduce(evalue
*factor
, const mat_ZZ
& num
, const mat_ZZ
& den_f
,
1178 barvinok_options
*options
);
1181 void ienumerator::reduce(evalue
*factor
, const mat_ZZ
& num
, const mat_ZZ
& den_f
,
1182 barvinok_options
*options
)
1184 unsigned len
= den_f
.NumRows(); // number of factors in den
1185 unsigned dim
= num
.NumCols();
1186 assert(num
.NumRows() == 1);
1189 eadd(factor
, vE
[vert
]);
1198 split_one(num
, num_s
, num_p
, den_f
, den_s
, den_r
);
1201 den_p
.SetLength(len
);
1205 normalize(one
, num_s
, num_p
, den_s
, den_p
, den_r
);
1207 emul(&mone
, factor
);
1211 for (int k
= 0; k
< len
; ++k
) {
1214 else if (den_s
[k
] == 0)
1217 if (no_param
== 0) {
1218 reduce(factor
, num_p
, den_r
, options
);
1222 pden
.SetDims(only_param
, dim
-1);
1224 for (k
= 0, l
= 0; k
< len
; ++k
)
1226 pden
[l
++] = den_r
[k
];
1228 for (k
= 0; k
< len
; ++k
)
1232 dpoly
n(no_param
, num_s
[0]);
1233 dpoly
D(no_param
, den_s
[k
], 1);
1234 for ( ; ++k
< len
; )
1235 if (den_p
[k
] == 0) {
1236 dpoly
fact(no_param
, den_s
[k
], 1);
1241 // if no_param + only_param == len then all powers
1242 // below will be all zero
1243 if (no_param
+ only_param
== len
) {
1244 if (E_num(0, dim
) != 0)
1245 r
= new dpoly_r(n
, len
);
1247 mpq_set_si(tcount
, 0, 1);
1249 n
.div(D
, tcount
, one
);
1251 if (value_notzero_p(mpq_numref(tcount
))) {
1255 value_assign(f
.x
.n
, mpq_numref(tcount
));
1256 value_assign(f
.d
, mpq_denref(tcount
));
1258 reduce(factor
, num_p
, pden
, options
);
1259 free_evalue_refs(&f
);
1264 for (k
= 0; k
< len
; ++k
) {
1265 if (den_s
[k
] == 0 || den_p
[k
] == 0)
1268 dpoly
pd(no_param
-1, den_s
[k
], 1);
1271 for (l
= 0; l
< k
; ++l
)
1272 if (den_r
[l
] == den_r
[k
])
1276 r
= new dpoly_r(n
, pd
, l
, len
);
1278 dpoly_r
*nr
= new dpoly_r(r
, pd
, l
, len
);
1284 dpoly_r
*rc
= r
->div(D
);
1287 if (E_num(0, dim
) == 0) {
1288 int common
= pden
.NumRows();
1289 dpoly_r_term_list
& final
= r
->c
[r
->len
-1];
1295 zz2value(r
->denom
, f
.d
);
1296 dpoly_r_term_list::iterator j
;
1297 for (j
= final
.begin(); j
!= final
.end(); ++j
) {
1298 if ((*j
)->coeff
== 0)
1301 for (int k
= 0; k
< r
->dim
; ++k
) {
1302 int n
= (*j
)->powers
[k
];
1305 pden
.SetDims(rows
+n
, pden
.NumCols());
1306 for (int l
= 0; l
< n
; ++l
)
1307 pden
[rows
+l
] = den_r
[k
];
1311 evalue_copy(&t
, factor
);
1312 zz2value((*j
)->coeff
, f
.x
.n
);
1314 reduce(&t
, num_p
, pden
, options
);
1315 free_evalue_refs(&t
);
1317 free_evalue_refs(&f
);
1319 ie_cum
cum(factor
, E_num(0, dim
), r
);
1320 cum
.cumulate(options
);
1322 int common
= pden
.NumRows();
1324 for (int j
= 0; j
< cum
.terms
.size(); ++j
) {
1326 pden
.SetDims(rows
, pden
.NumCols());
1327 for (int k
= 0; k
< r
->dim
; ++k
) {
1328 int n
= cum
.terms
[j
]->powers
[k
];
1331 pden
.SetDims(rows
+n
, pden
.NumCols());
1332 for (int l
= 0; l
< n
; ++l
)
1333 pden
[rows
+l
] = den_r
[k
];
1336 reduce(cum
.terms
[j
]->E
, num_p
, pden
, options
);
1337 free_evalue_refs(cum
.terms
[j
]->E
);
1338 delete cum
.terms
[j
]->E
;
1339 delete cum
.terms
[j
];
1346 static int type_offset(enode
*p
)
1348 return p
->type
== fractional
? 1 :
1349 p
->type
== flooring
? 1 : 0;
1352 static int edegree(evalue
*e
)
1357 if (value_notzero_p(e
->d
))
1361 int i
= type_offset(p
);
1362 if (p
->size
-i
-1 > d
)
1363 d
= p
->size
- i
- 1;
1364 for (; i
< p
->size
; i
++) {
1365 int d2
= edegree(&p
->arr
[i
]);
1372 void ienumerator::handle(const signed_cone
& sc
, barvinok_options
*options
)
1374 assert(sc
.det
== 1);
1376 assert(sc
.rays
.NumRows() == dim
);
1378 lattice_point(V
, sc
.rays
, vertex
[0], E_vertex
, options
);
1384 evalue_set_si(&one
, sc
.sign
, 1);
1385 reduce(&one
, vertex
, den
, options
);
1386 free_evalue_refs(&one
);
1388 for (int i
= 0; i
< dim
; ++i
)
1390 free_evalue_refs(E_vertex
[i
]);
1395 struct bfenumerator
: public vertex_decomposer
, public bf_base
,
1396 public ienumerator_base
{
1399 bfenumerator(Polyhedron
*P
, unsigned dim
, unsigned nbV
) :
1400 vertex_decomposer(P
, nbV
, *this),
1401 bf_base(dim
), ienumerator_base(dim
, this) {
1409 virtual void handle(const signed_cone
& sc
, barvinok_options
*options
);
1410 virtual void base(mat_ZZ
& factors
, bfc_vec
& v
);
1412 bfc_term_base
* new_bf_term(int len
) {
1413 bfe_term
* t
= new bfe_term(len
);
1417 virtual void set_factor(bfc_term_base
*t
, int k
, int change
) {
1418 bfe_term
* bfet
= static_cast<bfe_term
*>(t
);
1419 factor
= bfet
->factors
[k
];
1420 assert(factor
!= NULL
);
1421 bfet
->factors
[k
] = NULL
;
1423 emul(&mone
, factor
);
1426 virtual void set_factor(bfc_term_base
*t
, int k
, mpq_t
&q
, int change
) {
1427 bfe_term
* bfet
= static_cast<bfe_term
*>(t
);
1428 factor
= bfet
->factors
[k
];
1429 assert(factor
!= NULL
);
1430 bfet
->factors
[k
] = NULL
;
1436 value_oppose(f
.x
.n
, mpq_numref(q
));
1438 value_assign(f
.x
.n
, mpq_numref(q
));
1439 value_assign(f
.d
, mpq_denref(q
));
1441 free_evalue_refs(&f
);
1444 virtual void set_factor(bfc_term_base
*t
, int k
, const QQ
& c
, int change
) {
1445 bfe_term
* bfet
= static_cast<bfe_term
*>(t
);
1447 factor
= new evalue
;
1452 zz2value(c
.n
, f
.x
.n
);
1454 value_oppose(f
.x
.n
, f
.x
.n
);
1457 value_init(factor
->d
);
1458 evalue_copy(factor
, bfet
->factors
[k
]);
1460 free_evalue_refs(&f
);
1463 void set_factor(evalue
*f
, int change
) {
1469 virtual void insert_term(bfc_term_base
*t
, int i
) {
1470 bfe_term
* bfet
= static_cast<bfe_term
*>(t
);
1471 int len
= t
->terms
.NumRows()-1; // already increased by one
1473 bfet
->factors
.resize(len
+1);
1474 for (int j
= len
; j
> i
; --j
) {
1475 bfet
->factors
[j
] = bfet
->factors
[j
-1];
1476 t
->terms
[j
] = t
->terms
[j
-1];
1478 bfet
->factors
[i
] = factor
;
1482 virtual void update_term(bfc_term_base
*t
, int i
) {
1483 bfe_term
* bfet
= static_cast<bfe_term
*>(t
);
1485 eadd(factor
, bfet
->factors
[i
]);
1486 free_evalue_refs(factor
);
1490 virtual bool constant_vertex(int dim
) { return E_num(0, dim
) == 0; }
1492 virtual void cum(bf_reducer
*bfr
, bfc_term_base
*t
, int k
, dpoly_r
*r
,
1493 barvinok_options
*options
);
1496 enumerator_base
*enumerator_base::create(Polyhedron
*P
, unsigned dim
, unsigned nbV
,
1497 barvinok_options
*options
)
1499 enumerator_base
*eb
;
1501 if (options
->incremental_specialization
== BV_SPECIALIZATION_BF
)
1502 eb
= new bfenumerator(P
, dim
, nbV
);
1503 else if (options
->incremental_specialization
== BV_SPECIALIZATION_DF
)
1504 eb
= new ienumerator(P
, dim
, nbV
);
1506 eb
= new enumerator(P
, dim
, nbV
);
1511 struct bfe_cum
: public cumulator
{
1513 bfc_term_base
*told
;
1517 bfe_cum(evalue
*factor
, evalue
*v
, dpoly_r
*r
, bf_reducer
*bfr
,
1518 bfc_term_base
*t
, int k
, bfenumerator
*e
) :
1519 cumulator(factor
, v
, r
), told(t
), k(k
),
1523 virtual void add_term(const vector
<int>& powers
, evalue
*f2
);
1526 void bfe_cum::add_term(const vector
<int>& powers
, evalue
*f2
)
1528 bfr
->update_powers(powers
);
1530 bfc_term_base
* t
= bfe
->find_bfc_term(bfr
->vn
, bfr
->npowers
, bfr
->nnf
);
1531 bfe
->set_factor(f2
, bfr
->l_changes
% 2);
1532 bfe
->add_term(t
, told
->terms
[k
], bfr
->l_extra_num
);
1535 void bfenumerator::cum(bf_reducer
*bfr
, bfc_term_base
*t
, int k
,
1536 dpoly_r
*r
, barvinok_options
*options
)
1538 bfe_term
* bfet
= static_cast<bfe_term
*>(t
);
1539 bfe_cum
cum(bfet
->factors
[k
], E_num(0, bfr
->d
), r
, bfr
, t
, k
, this);
1540 cum
.cumulate(options
);
1543 void bfenumerator::base(mat_ZZ
& factors
, bfc_vec
& v
)
1545 for (int i
= 0; i
< v
.size(); ++i
) {
1546 assert(v
[i
]->terms
.NumRows() == 1);
1547 evalue
*factor
= static_cast<bfe_term
*>(v
[i
])->factors
[0];
1548 eadd(factor
, vE
[vert
]);
1553 void bfenumerator::handle(const signed_cone
& sc
, barvinok_options
*options
)
1555 assert(sc
.det
== 1);
1557 assert(sc
.rays
.NumRows() == enumerator_base::dim
);
1559 bfe_term
* t
= new bfe_term(enumerator_base::dim
);
1560 vector
< bfc_term_base
* > v
;
1563 t
->factors
.resize(1);
1565 t
->terms
.SetDims(1, enumerator_base::dim
);
1566 lattice_point(V
, sc
.rays
, t
->terms
[0], E_vertex
, options
);
1568 // the elements of factors are always lexpositive
1570 int s
= setup_factors(sc
.rays
, factors
, t
, sc
.sign
);
1572 t
->factors
[0] = new evalue
;
1573 value_init(t
->factors
[0]->d
);
1574 evalue_set_si(t
->factors
[0], s
, 1);
1575 reduce(factors
, v
, options
);
1577 for (int i
= 0; i
< enumerator_base::dim
; ++i
)
1579 free_evalue_refs(E_vertex
[i
]);
1584 #ifdef HAVE_CORRECT_VERTICES
1585 static inline Param_Polyhedron
*Polyhedron2Param_SD(Polyhedron
**Din
,
1586 Polyhedron
*Cin
,int WS
,Polyhedron
**CEq
,Matrix
**CT
)
1588 if (WS
& POL_NO_DUAL
)
1590 return Polyhedron2Param_SimplifiedDomain(Din
, Cin
, WS
, CEq
, CT
);
1593 static Param_Polyhedron
*Polyhedron2Param_SD(Polyhedron
**Din
,
1594 Polyhedron
*Cin
,int WS
,Polyhedron
**CEq
,Matrix
**CT
)
1596 static char data
[] = " 1 0 0 0 0 1 -18 "
1597 " 1 0 0 -20 0 19 1 "
1598 " 1 0 1 20 0 -20 16 "
1601 " 1 4 -20 0 0 -1 23 "
1602 " 1 -4 20 0 0 1 -22 "
1603 " 1 0 1 0 20 -20 16 "
1604 " 1 0 0 0 -20 19 1 ";
1605 static int checked
= 0;
1610 Matrix
*M
= Matrix_Alloc(9, 7);
1611 for (i
= 0; i
< 9; ++i
)
1612 for (int j
= 0; j
< 7; ++j
) {
1613 sscanf(p
, "%d%n", &v
, &n
);
1615 value_set_si(M
->p
[i
][j
], v
);
1617 Polyhedron
*P
= Constraints2Polyhedron(M
, 1024);
1619 Polyhedron
*U
= Universe_Polyhedron(1);
1620 Param_Polyhedron
*PP
= Polyhedron2Param_Domain(P
, U
, 1024);
1624 for (i
= 0, V
= PP
->V
; V
; ++i
, V
= V
->next
)
1627 Param_Polyhedron_Free(PP
);
1629 fprintf(stderr
, "WARNING: results may be incorrect\n");
1631 "WARNING: use latest version of PolyLib to remove this warning\n");
1635 return Polyhedron2Param_SimplifiedDomain(Din
, Cin
, WS
, CEq
, CT
);
1639 static evalue
* barvinok_enumerate_ev_f(Polyhedron
*P
, Polyhedron
* C
,
1640 barvinok_options
*options
);
1643 static evalue
* barvinok_enumerate_cst(Polyhedron
*P
, Polyhedron
* C
,
1644 struct barvinok_options
*options
)
1648 ALLOC(evalue
, eres
);
1649 value_init(eres
->d
);
1650 value_set_si(eres
->d
, 0);
1651 eres
->x
.p
= new_enode(partition
, 2, C
->Dimension
);
1652 EVALUE_SET_DOMAIN(eres
->x
.p
->arr
[0],
1653 DomainConstraintSimplify(C
, options
->MaxRays
));
1654 value_set_si(eres
->x
.p
->arr
[1].d
, 1);
1655 value_init(eres
->x
.p
->arr
[1].x
.n
);
1657 value_set_si(eres
->x
.p
->arr
[1].x
.n
, 0);
1659 barvinok_count_with_options(P
, &eres
->x
.p
->arr
[1].x
.n
, options
);
1664 evalue
* barvinok_enumerate_with_options(Polyhedron
*P
, Polyhedron
* C
,
1665 struct barvinok_options
*options
)
1667 //P = unfringe(P, MaxRays);
1668 Polyhedron
*next
, *Cnext
;
1669 Polyhedron
*Corig
= C
;
1670 Polyhedron
*Porig
= P
;
1671 Polyhedron
*CEq
= NULL
, *rVD
, *CA
;
1673 unsigned nparam
= C
->Dimension
;
1679 "barvinok_enumerate: input is a union; only first polyhedron is enumerated\n");
1683 "barvinok_enumerate: context is a union; only first polyhedron is considered\n");
1686 value_init(factor
.d
);
1687 evalue_set_si(&factor
, 1, 1);
1691 CA
= align_context(C
, P
->Dimension
, options
->MaxRays
);
1694 P
= DomainIntersection(P
, CA
, options
->MaxRays
);
1696 Polyhedron_Free(CA
);
1699 POL_ENSURE_FACETS(P
);
1700 POL_ENSURE_VERTICES(P
);
1701 POL_ENSURE_FACETS(C
);
1702 POL_ENSURE_VERTICES(C
);
1704 if (C
->Dimension
== 0 || emptyQ(P
)) {
1706 eres
= barvinok_enumerate_cst(P
, CEq
? CEq
: Polyhedron_Copy(C
), options
);
1709 evalue_backsubstitute(eres
, CP
, options
->MaxRays
);
1713 emul(&factor
, eres
);
1714 if (options
->approximation_method
== BV_APPROX_DROP
) {
1715 if (options
->polynomial_approximation
== BV_APPROX_SIGN_UPPER
)
1716 evalue_frac2polynomial(eres
, 1, options
->MaxRays
);
1717 if (options
->polynomial_approximation
== BV_APPROX_SIGN_LOWER
)
1718 evalue_frac2polynomial(eres
, -1, options
->MaxRays
);
1719 if (options
->polynomial_approximation
== BV_APPROX_SIGN_APPROX
)
1720 evalue_frac2polynomial(eres
, 0, options
->MaxRays
);
1722 reduce_evalue(eres
);
1723 free_evalue_refs(&factor
);
1728 Corig
->next
= Cnext
;
1731 if (Polyhedron_is_unbounded(P
, nparam
, options
->MaxRays
))
1736 P
= remove_equalities_p(P
, P
->Dimension
-nparam
, &f
);
1737 mask(f
, &factor
, options
);
1740 if (P
->Dimension
== nparam
) {
1742 P
= Universe_Polyhedron(0);
1748 remove_all_equalities(&P
, &C
, &CP
, NULL
, nparam
, options
->MaxRays
);
1749 if (P
!= Q
&& Q
!= Porig
)
1751 if (C
!= D
&& D
!= Corig
)
1755 nparam
= C
->Dimension
;
1760 Polyhedron
*T
= Polyhedron_Factor(P
, nparam
, NULL
, options
->MaxRays
);
1761 if (T
|| (P
->Dimension
== nparam
+1)) {
1764 for (Q
= T
? T
: P
; Q
; Q
= Q
->next
) {
1765 Polyhedron
*next
= Q
->next
;
1769 if (Q
->Dimension
!= C
->Dimension
)
1770 QC
= Polyhedron_Project(Q
, nparam
);
1773 C
= DomainIntersection(C
, QC
, options
->MaxRays
);
1775 Polyhedron_Free(C2
);
1777 Polyhedron_Free(QC
);
1785 if (T
->Dimension
== C
->Dimension
) {
1794 eres
= barvinok_enumerate_ev_f(P
, C
, options
);
1801 for (Q
= P
->next
; Q
; Q
= Q
->next
) {
1802 Polyhedron
*next
= Q
->next
;
1805 f
= barvinok_enumerate_ev_f(Q
, C
, options
);
1807 free_evalue_refs(f
);
1817 evalue
* barvinok_enumerate_ev(Polyhedron
*P
, Polyhedron
* C
, unsigned MaxRays
)
1820 barvinok_options
*options
= barvinok_options_new_with_defaults();
1821 options
->MaxRays
= MaxRays
;
1822 E
= barvinok_enumerate_with_options(P
, C
, options
);
1823 barvinok_options_free(options
);
1827 evalue
*Param_Polyhedron_Enumerate(Param_Polyhedron
*PP
, Polyhedron
*P
,
1829 Polyhedron
*CEq
, Matrix
*CT
,
1830 struct barvinok_options
*options
)
1834 unsigned nparam
= CT
? CT
->NbRows
- 1 : C
->Dimension
;
1835 unsigned dim
= P
->Dimension
- nparam
;
1837 ALLOC(evalue
, eres
);
1838 value_init(eres
->d
);
1839 value_set_si(eres
->d
, 0);
1842 for (nd
= 0, D
=PP
->D
; D
; ++nd
, D
=D
->next
);
1843 struct section
{ Polyhedron
*D
; evalue E
; };
1844 section
*s
= new section
[nd
];
1846 enumerator_base
*et
= NULL
;
1851 et
= enumerator_base::create(P
, dim
, PP
->nbV
, options
);
1853 Polyhedron
*TC
= true_context(P
, CT
, CEq
? CEq
: C
, options
->MaxRays
);
1854 FORALL_REDUCED_DOMAIN(PP
, TC
, CT
, CEq
, nd
, options
, i
, D
, rVD
)
1858 pVD
= CT
? DomainImage(rVD
,CT
,options
->MaxRays
) : rVD
;
1860 value_init(s
[i
].E
.d
);
1861 evalue_set_si(&s
[i
].E
, 0, 1);
1864 FORALL_PVertex_in_ParamPolyhedron(V
,D
,PP
) // _i is internal counter
1867 et
->decompose_at(V
, _i
, options
);
1868 } catch (OrthogonalException
&e
) {
1871 FORALL_REDUCED_DOMAIN_RESET
;
1872 for (; i
>= 0; --i
) {
1873 free_evalue_refs(&s
[i
].E
);
1874 Domain_Free(s
[i
].D
);
1878 eadd(et
->vE
[_i
] , &s
[i
].E
);
1879 END_FORALL_PVertex_in_ParamPolyhedron
;
1880 evalue_range_reduction_in_domain(&s
[i
].E
, pVD
);
1883 addeliminatedparams_evalue(&s
[i
].E
, CT
);
1886 END_FORALL_REDUCED_DOMAIN
1887 Polyhedron_Free(TC
);
1891 evalue_set_si(eres
, 0, 1);
1893 eres
->x
.p
= new_enode(partition
, 2*nd
, C
->Dimension
);
1894 for (int j
= 0; j
< nd
; ++j
) {
1895 EVALUE_SET_DOMAIN(eres
->x
.p
->arr
[2*j
], s
[j
].D
);
1896 value_clear(eres
->x
.p
->arr
[2*j
+1].d
);
1897 eres
->x
.p
->arr
[2*j
+1] = s
[j
].E
;
1905 static evalue
* barvinok_enumerate_ev_f(Polyhedron
*P
, Polyhedron
* C
,
1906 barvinok_options
*options
)
1908 unsigned nparam
= C
->Dimension
;
1909 bool do_scale
= options
->approximation_method
== BV_APPROX_SCALE
;
1911 if (options
->approximation_method
== BV_APPROX_VOLUME
)
1912 return Param_Polyhedron_Volume(P
, C
, options
);
1914 if (P
->Dimension
- nparam
== 1 && !do_scale
)
1915 return ParamLine_Length(P
, C
, options
);
1917 Param_Polyhedron
*PP
= NULL
;
1918 Polyhedron
*CEq
= NULL
;
1921 Polyhedron
*Porig
= P
;
1925 eres
= scale_bound(P
, C
, options
);
1931 PP
= Polyhedron2Param_SD(&T
, C
, options
->MaxRays
, &CEq
, &CT
);
1932 if (T
!= P
&& P
!= Porig
)
1936 if (isIdentity(CT
)) {
1941 if (CT
&& CT
->NbRows
== 1)
1942 eres
= barvinok_enumerate_cst(P
, CEq
, options
);
1945 eres
= scale(PP
, P
, C
, CEq
, CT
, options
);
1947 eres
= Param_Polyhedron_Enumerate(PP
, P
, C
, CEq
, CT
, options
);
1949 Polyhedron_Free(CEq
);
1955 Param_Polyhedron_Free(PP
);
1962 Enumeration
* barvinok_enumerate(Polyhedron
*P
, Polyhedron
* C
, unsigned MaxRays
)
1964 evalue
*EP
= barvinok_enumerate_ev(P
, C
, MaxRays
);
1966 return partition2enumeration(EP
);
1969 static void SwapColumns(Value
**V
, int n
, int i
, int j
)
1971 for (int r
= 0; r
< n
; ++r
)
1972 value_swap(V
[r
][i
], V
[r
][j
]);
1975 static void SwapColumns(Polyhedron
*P
, int i
, int j
)
1977 SwapColumns(P
->Constraint
, P
->NbConstraints
, i
, j
);
1978 SwapColumns(P
->Ray
, P
->NbRays
, i
, j
);
1981 /* Construct a constraint c from constraints l and u such that if
1982 * if constraint c holds then for each value of the other variables
1983 * there is at most one value of variable pos (position pos+1 in the constraints).
1985 * Given a lower and an upper bound
1986 * n_l v_i + <c_l,x> + c_l >= 0
1987 * -n_u v_i + <c_u,x> + c_u >= 0
1988 * the constructed constraint is
1990 * -(n_l<c_u,x> + n_u<c_l,x>) + (-n_l c_u - n_u c_l + n_l n_u - 1)
1992 * which is then simplified to remove the content of the non-constant coefficients
1994 * len is the total length of the constraints.
1995 * v is a temporary variable that can be used by this procedure
1997 static void negative_test_constraint(Value
*l
, Value
*u
, Value
*c
, int pos
,
2000 value_oppose(*v
, u
[pos
+1]);
2001 Vector_Combine(l
+1, u
+1, c
+1, *v
, l
[pos
+1], len
-1);
2002 value_multiply(*v
, *v
, l
[pos
+1]);
2003 value_subtract(c
[len
-1], c
[len
-1], *v
);
2004 value_set_si(*v
, -1);
2005 Vector_Scale(c
+1, c
+1, *v
, len
-1);
2006 value_decrement(c
[len
-1], c
[len
-1]);
2007 ConstraintSimplify(c
, c
, len
, v
);
2010 static bool parallel_constraints(Value
*l
, Value
*u
, Value
*c
, int pos
,
2019 Vector_Gcd(&l
[1+pos
], len
, &g1
);
2020 Vector_Gcd(&u
[1+pos
], len
, &g2
);
2021 Vector_Combine(l
+1+pos
, u
+1+pos
, c
+1, g2
, g1
, len
);
2022 parallel
= First_Non_Zero(c
+1, len
) == -1;
2030 static void negative_test_constraint7(Value
*l
, Value
*u
, Value
*c
, int pos
,
2031 int exist
, int len
, Value
*v
)
2036 Vector_Gcd(&u
[1+pos
], exist
, v
);
2037 Vector_Gcd(&l
[1+pos
], exist
, &g
);
2038 Vector_Combine(l
+1, u
+1, c
+1, *v
, g
, len
-1);
2039 value_multiply(*v
, *v
, g
);
2040 value_subtract(c
[len
-1], c
[len
-1], *v
);
2041 value_set_si(*v
, -1);
2042 Vector_Scale(c
+1, c
+1, *v
, len
-1);
2043 value_decrement(c
[len
-1], c
[len
-1]);
2044 ConstraintSimplify(c
, c
, len
, v
);
2049 /* Turns a x + b >= 0 into a x + b <= -1
2051 * len is the total length of the constraint.
2052 * v is a temporary variable that can be used by this procedure
2054 static void oppose_constraint(Value
*c
, int len
, Value
*v
)
2056 value_set_si(*v
, -1);
2057 Vector_Scale(c
+1, c
+1, *v
, len
-1);
2058 value_decrement(c
[len
-1], c
[len
-1]);
2061 /* Split polyhedron P into two polyhedra *pos and *neg, where
2062 * existential variable i has at most one solution for each
2063 * value of the other variables in *neg.
2065 * The splitting is performed using constraints l and u.
2067 * nvar: number of set variables
2068 * row: temporary vector that can be used by this procedure
2069 * f: temporary value that can be used by this procedure
2071 static bool SplitOnConstraint(Polyhedron
*P
, int i
, int l
, int u
,
2072 int nvar
, int MaxRays
, Vector
*row
, Value
& f
,
2073 Polyhedron
**pos
, Polyhedron
**neg
)
2075 negative_test_constraint(P
->Constraint
[l
], P
->Constraint
[u
],
2076 row
->p
, nvar
+i
, P
->Dimension
+2, &f
);
2077 *neg
= AddConstraints(row
->p
, 1, P
, MaxRays
);
2079 /* We found an independent, but useless constraint
2080 * Maybe we should detect this earlier and not
2081 * mark the variable as INDEPENDENT
2083 if (emptyQ((*neg
))) {
2084 Polyhedron_Free(*neg
);
2088 oppose_constraint(row
->p
, P
->Dimension
+2, &f
);
2089 *pos
= AddConstraints(row
->p
, 1, P
, MaxRays
);
2091 if (emptyQ((*pos
))) {
2092 Polyhedron_Free(*neg
);
2093 Polyhedron_Free(*pos
);
2101 * unimodularly transform P such that constraint r is transformed
2102 * into a constraint that involves only a single (the first)
2103 * existential variable
2106 static Polyhedron
*rotate_along(Polyhedron
*P
, int r
, int nvar
, int exist
,
2112 Vector
*row
= Vector_Alloc(exist
);
2113 Vector_Copy(P
->Constraint
[r
]+1+nvar
, row
->p
, exist
);
2114 Vector_Gcd(row
->p
, exist
, &g
);
2115 if (value_notone_p(g
))
2116 Vector_AntiScale(row
->p
, row
->p
, g
, exist
);
2119 Matrix
*M
= unimodular_complete(row
);
2120 Matrix
*M2
= Matrix_Alloc(P
->Dimension
+1, P
->Dimension
+1);
2121 for (r
= 0; r
< nvar
; ++r
)
2122 value_set_si(M2
->p
[r
][r
], 1);
2123 for ( ; r
< nvar
+exist
; ++r
)
2124 Vector_Copy(M
->p
[r
-nvar
], M2
->p
[r
]+nvar
, exist
);
2125 for ( ; r
< P
->Dimension
+1; ++r
)
2126 value_set_si(M2
->p
[r
][r
], 1);
2127 Polyhedron
*T
= Polyhedron_Image(P
, M2
, MaxRays
);
2136 /* Split polyhedron P into two polyhedra *pos and *neg, where
2137 * existential variable i has at most one solution for each
2138 * value of the other variables in *neg.
2140 * If independent is set, then the two constraints on which the
2141 * split will be performed need to be independent of the other
2142 * existential variables.
2144 * Return true if an appropriate split could be performed.
2146 * nvar: number of set variables
2147 * exist: number of existential variables
2148 * row: temporary vector that can be used by this procedure
2149 * f: temporary value that can be used by this procedure
2151 static bool SplitOnVar(Polyhedron
*P
, int i
,
2152 int nvar
, int exist
, int MaxRays
,
2153 Vector
*row
, Value
& f
, bool independent
,
2154 Polyhedron
**pos
, Polyhedron
**neg
)
2158 for (int l
= P
->NbEq
; l
< P
->NbConstraints
; ++l
) {
2159 if (value_negz_p(P
->Constraint
[l
][nvar
+i
+1]))
2163 for (j
= 0; j
< exist
; ++j
)
2164 if (j
!= i
&& value_notzero_p(P
->Constraint
[l
][nvar
+j
+1]))
2170 for (int u
= P
->NbEq
; u
< P
->NbConstraints
; ++u
) {
2171 if (value_posz_p(P
->Constraint
[u
][nvar
+i
+1]))
2175 for (j
= 0; j
< exist
; ++j
)
2176 if (j
!= i
&& value_notzero_p(P
->Constraint
[u
][nvar
+j
+1]))
2182 if (SplitOnConstraint(P
, i
, l
, u
, nvar
, MaxRays
, row
, f
, pos
, neg
)) {
2185 SwapColumns(*neg
, nvar
+1, nvar
+1+i
);
2195 static bool double_bound_pair(Polyhedron
*P
, int nvar
, int exist
,
2196 int i
, int l1
, int l2
,
2197 Polyhedron
**pos
, Polyhedron
**neg
)
2201 Vector
*row
= Vector_Alloc(P
->Dimension
+2);
2202 value_set_si(row
->p
[0], 1);
2203 value_oppose(f
, P
->Constraint
[l1
][nvar
+i
+1]);
2204 Vector_Combine(P
->Constraint
[l1
]+1, P
->Constraint
[l2
]+1,
2206 P
->Constraint
[l2
][nvar
+i
+1], f
,
2208 ConstraintSimplify(row
->p
, row
->p
, P
->Dimension
+2, &f
);
2209 *pos
= AddConstraints(row
->p
, 1, P
, 0);
2210 value_set_si(f
, -1);
2211 Vector_Scale(row
->p
+1, row
->p
+1, f
, P
->Dimension
+1);
2212 value_decrement(row
->p
[P
->Dimension
+1], row
->p
[P
->Dimension
+1]);
2213 *neg
= AddConstraints(row
->p
, 1, P
, 0);
2217 return !emptyQ((*pos
)) && !emptyQ((*neg
));
2220 static bool double_bound(Polyhedron
*P
, int nvar
, int exist
,
2221 Polyhedron
**pos
, Polyhedron
**neg
)
2223 for (int i
= 0; i
< exist
; ++i
) {
2225 for (l1
= P
->NbEq
; l1
< P
->NbConstraints
; ++l1
) {
2226 if (value_negz_p(P
->Constraint
[l1
][nvar
+i
+1]))
2228 for (l2
= l1
+ 1; l2
< P
->NbConstraints
; ++l2
) {
2229 if (value_negz_p(P
->Constraint
[l2
][nvar
+i
+1]))
2231 if (double_bound_pair(P
, nvar
, exist
, i
, l1
, l2
, pos
, neg
))
2235 for (l1
= P
->NbEq
; l1
< P
->NbConstraints
; ++l1
) {
2236 if (value_posz_p(P
->Constraint
[l1
][nvar
+i
+1]))
2238 if (l1
< P
->NbConstraints
)
2239 for (l2
= l1
+ 1; l2
< P
->NbConstraints
; ++l2
) {
2240 if (value_posz_p(P
->Constraint
[l2
][nvar
+i
+1]))
2242 if (double_bound_pair(P
, nvar
, exist
, i
, l1
, l2
, pos
, neg
))
2254 INDEPENDENT
= 1 << 2,
2258 static evalue
* enumerate_or(Polyhedron
*D
,
2259 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2262 fprintf(stderr
, "\nER: Or\n");
2263 #endif /* DEBUG_ER */
2265 Polyhedron
*N
= D
->next
;
2268 barvinok_enumerate_e_with_options(D
, exist
, nparam
, options
);
2271 for (D
= N
; D
; D
= N
) {
2276 barvinok_enumerate_e_with_options(D
, exist
, nparam
, options
);
2279 free_evalue_refs(EN
);
2289 static evalue
* enumerate_sum(Polyhedron
*P
,
2290 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2292 int nvar
= P
->Dimension
- exist
- nparam
;
2293 int toswap
= nvar
< exist
? nvar
: exist
;
2294 for (int i
= 0; i
< toswap
; ++i
)
2295 SwapColumns(P
, 1 + i
, nvar
+exist
- i
);
2299 fprintf(stderr
, "\nER: Sum\n");
2300 #endif /* DEBUG_ER */
2302 evalue
*EP
= barvinok_enumerate_e_with_options(P
, exist
, nparam
, options
);
2304 evalue_split_domains_into_orthants(EP
, options
->MaxRays
);
2306 evalue_range_reduction(EP
);
2308 evalue_frac2floor2(EP
, 1);
2310 evalue
*sum
= esum(EP
, nvar
);
2312 free_evalue_refs(EP
);
2316 evalue_range_reduction(EP
);
2321 static evalue
* split_sure(Polyhedron
*P
, Polyhedron
*S
,
2322 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2324 int nvar
= P
->Dimension
- exist
- nparam
;
2326 Matrix
*M
= Matrix_Alloc(exist
, S
->Dimension
+2);
2327 for (int i
= 0; i
< exist
; ++i
)
2328 value_set_si(M
->p
[i
][nvar
+i
+1], 1);
2330 S
= DomainAddRays(S
, M
, options
->MaxRays
);
2332 Polyhedron
*F
= DomainAddRays(P
, M
, options
->MaxRays
);
2333 Polyhedron
*D
= DomainDifference(F
, S
, options
->MaxRays
);
2335 D
= Disjoint_Domain(D
, 0, options
->MaxRays
);
2340 M
= Matrix_Alloc(P
->Dimension
+1-exist
, P
->Dimension
+1);
2341 for (int j
= 0; j
< nvar
; ++j
)
2342 value_set_si(M
->p
[j
][j
], 1);
2343 for (int j
= 0; j
< nparam
+1; ++j
)
2344 value_set_si(M
->p
[nvar
+j
][nvar
+exist
+j
], 1);
2345 Polyhedron
*T
= Polyhedron_Image(S
, M
, options
->MaxRays
);
2346 evalue
*EP
= barvinok_enumerate_e_with_options(T
, 0, nparam
, options
);
2351 for (Polyhedron
*Q
= D
; Q
; Q
= Q
->next
) {
2352 Polyhedron
*N
= Q
->next
;
2354 T
= DomainIntersection(P
, Q
, options
->MaxRays
);
2355 evalue
*E
= barvinok_enumerate_e_with_options(T
, exist
, nparam
, options
);
2357 free_evalue_refs(E
);
2366 static evalue
* enumerate_sure(Polyhedron
*P
,
2367 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2371 int nvar
= P
->Dimension
- exist
- nparam
;
2377 for (i
= 0; i
< exist
; ++i
) {
2378 Matrix
*M
= Matrix_Alloc(S
->NbConstraints
, S
->Dimension
+2);
2380 value_set_si(lcm
, 1);
2381 for (int j
= 0; j
< S
->NbConstraints
; ++j
) {
2382 if (value_negz_p(S
->Constraint
[j
][1+nvar
+i
]))
2384 if (value_one_p(S
->Constraint
[j
][1+nvar
+i
]))
2386 value_lcm(lcm
, S
->Constraint
[j
][1+nvar
+i
], &lcm
);
2389 for (int j
= 0; j
< S
->NbConstraints
; ++j
) {
2390 if (value_negz_p(S
->Constraint
[j
][1+nvar
+i
]))
2392 if (value_one_p(S
->Constraint
[j
][1+nvar
+i
]))
2394 value_division(f
, lcm
, S
->Constraint
[j
][1+nvar
+i
]);
2395 Vector_Scale(S
->Constraint
[j
], M
->p
[c
], f
, S
->Dimension
+2);
2396 value_subtract(M
->p
[c
][S
->Dimension
+1],
2397 M
->p
[c
][S
->Dimension
+1],
2399 value_increment(M
->p
[c
][S
->Dimension
+1],
2400 M
->p
[c
][S
->Dimension
+1]);
2404 S
= AddConstraints(M
->p
[0], c
, S
, options
->MaxRays
);
2419 fprintf(stderr
, "\nER: Sure\n");
2420 #endif /* DEBUG_ER */
2422 return split_sure(P
, S
, exist
, nparam
, options
);
2425 static evalue
* enumerate_sure2(Polyhedron
*P
,
2426 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2428 int nvar
= P
->Dimension
- exist
- nparam
;
2430 for (r
= 0; r
< P
->NbRays
; ++r
)
2431 if (value_one_p(P
->Ray
[r
][0]) &&
2432 value_one_p(P
->Ray
[r
][P
->Dimension
+1]))
2438 Matrix
*M
= Matrix_Alloc(nvar
+ 1 + nparam
, P
->Dimension
+2);
2439 for (int i
= 0; i
< nvar
; ++i
)
2440 value_set_si(M
->p
[i
][1+i
], 1);
2441 for (int i
= 0; i
< nparam
; ++i
)
2442 value_set_si(M
->p
[i
+nvar
][1+nvar
+exist
+i
], 1);
2443 Vector_Copy(P
->Ray
[r
]+1+nvar
, M
->p
[nvar
+nparam
]+1+nvar
, exist
);
2444 value_set_si(M
->p
[nvar
+nparam
][0], 1);
2445 value_set_si(M
->p
[nvar
+nparam
][P
->Dimension
+1], 1);
2446 Polyhedron
* F
= Rays2Polyhedron(M
, options
->MaxRays
);
2449 Polyhedron
*I
= DomainIntersection(F
, P
, options
->MaxRays
);
2453 fprintf(stderr
, "\nER: Sure2\n");
2454 #endif /* DEBUG_ER */
2456 return split_sure(P
, I
, exist
, nparam
, options
);
2459 static evalue
* enumerate_cyclic(Polyhedron
*P
,
2460 unsigned exist
, unsigned nparam
,
2461 evalue
* EP
, int r
, int p
, unsigned MaxRays
)
2463 int nvar
= P
->Dimension
- exist
- nparam
;
2465 /* If EP in its fractional maps only contains references
2466 * to the remainder parameter with appropriate coefficients
2467 * then we could in principle avoid adding existentially
2468 * quantified variables to the validity domains.
2469 * We'd have to replace the remainder by m { p/m }
2470 * and multiply with an appropriate factor that is one
2471 * only in the appropriate range.
2472 * This last multiplication can be avoided if EP
2473 * has a single validity domain with no (further)
2474 * constraints on the remainder parameter
2477 Matrix
*CT
= Matrix_Alloc(nparam
+1, nparam
+3);
2478 Matrix
*M
= Matrix_Alloc(1, 1+nparam
+3);
2479 for (int j
= 0; j
< nparam
; ++j
)
2481 value_set_si(CT
->p
[j
][j
], 1);
2482 value_set_si(CT
->p
[p
][nparam
+1], 1);
2483 value_set_si(CT
->p
[nparam
][nparam
+2], 1);
2484 value_set_si(M
->p
[0][1+p
], -1);
2485 value_absolute(M
->p
[0][1+nparam
], P
->Ray
[0][1+nvar
+exist
+p
]);
2486 value_set_si(M
->p
[0][1+nparam
+1], 1);
2487 Polyhedron
*CEq
= Constraints2Polyhedron(M
, 1);
2489 addeliminatedparams_enum(EP
, CT
, CEq
, MaxRays
, nparam
);
2490 Polyhedron_Free(CEq
);
2496 static void enumerate_vd_add_ray(evalue
*EP
, Matrix
*Rays
, unsigned MaxRays
)
2498 if (value_notzero_p(EP
->d
))
2501 assert(EP
->x
.p
->type
== partition
);
2502 assert(EP
->x
.p
->pos
== EVALUE_DOMAIN(EP
->x
.p
->arr
[0])->Dimension
);
2503 for (int i
= 0; i
< EP
->x
.p
->size
/2; ++i
) {
2504 Polyhedron
*D
= EVALUE_DOMAIN(EP
->x
.p
->arr
[2*i
]);
2505 Polyhedron
*N
= DomainAddRays(D
, Rays
, MaxRays
);
2506 EVALUE_SET_DOMAIN(EP
->x
.p
->arr
[2*i
], N
);
2511 static evalue
* enumerate_line(Polyhedron
*P
,
2512 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2518 fprintf(stderr
, "\nER: Line\n");
2519 #endif /* DEBUG_ER */
2521 int nvar
= P
->Dimension
- exist
- nparam
;
2523 for (i
= 0; i
< nparam
; ++i
)
2524 if (value_notzero_p(P
->Ray
[0][1+nvar
+exist
+i
]))
2527 for (j
= i
+1; j
< nparam
; ++j
)
2528 if (value_notzero_p(P
->Ray
[0][1+nvar
+exist
+i
]))
2530 assert(j
>= nparam
); // for now
2532 Matrix
*M
= Matrix_Alloc(2, P
->Dimension
+2);
2533 value_set_si(M
->p
[0][0], 1);
2534 value_set_si(M
->p
[0][1+nvar
+exist
+i
], 1);
2535 value_set_si(M
->p
[1][0], 1);
2536 value_set_si(M
->p
[1][1+nvar
+exist
+i
], -1);
2537 value_absolute(M
->p
[1][1+P
->Dimension
], P
->Ray
[0][1+nvar
+exist
+i
]);
2538 value_decrement(M
->p
[1][1+P
->Dimension
], M
->p
[1][1+P
->Dimension
]);
2539 Polyhedron
*S
= AddConstraints(M
->p
[0], 2, P
, options
->MaxRays
);
2540 evalue
*EP
= barvinok_enumerate_e_with_options(S
, exist
, nparam
, options
);
2544 return enumerate_cyclic(P
, exist
, nparam
, EP
, 0, i
, options
->MaxRays
);
2547 static int single_param_pos(Polyhedron
*P
, unsigned exist
, unsigned nparam
,
2550 int nvar
= P
->Dimension
- exist
- nparam
;
2551 if (First_Non_Zero(P
->Ray
[r
]+1, nvar
) != -1)
2553 int i
= First_Non_Zero(P
->Ray
[r
]+1+nvar
+exist
, nparam
);
2556 if (First_Non_Zero(P
->Ray
[r
]+1+nvar
+exist
+1, nparam
-i
-1) != -1)
2561 static evalue
* enumerate_remove_ray(Polyhedron
*P
, int r
,
2562 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2565 fprintf(stderr
, "\nER: RedundantRay\n");
2566 #endif /* DEBUG_ER */
2570 value_set_si(one
, 1);
2571 int len
= P
->NbRays
-1;
2572 Matrix
*M
= Matrix_Alloc(2 * len
, P
->Dimension
+2);
2573 Vector_Copy(P
->Ray
[0], M
->p
[0], r
* (P
->Dimension
+2));
2574 Vector_Copy(P
->Ray
[r
+1], M
->p
[r
], (len
-r
) * (P
->Dimension
+2));
2575 for (int j
= 0; j
< P
->NbRays
; ++j
) {
2578 Vector_Combine(P
->Ray
[j
], P
->Ray
[r
], M
->p
[len
+j
-(j
>r
)],
2579 one
, P
->Ray
[j
][P
->Dimension
+1], P
->Dimension
+2);
2582 P
= Rays2Polyhedron(M
, options
->MaxRays
);
2584 evalue
*EP
= barvinok_enumerate_e_with_options(P
, exist
, nparam
, options
);
2591 static evalue
* enumerate_redundant_ray(Polyhedron
*P
,
2592 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2594 assert(P
->NbBid
== 0);
2595 int nvar
= P
->Dimension
- exist
- nparam
;
2599 for (int r
= 0; r
< P
->NbRays
; ++r
) {
2600 if (value_notzero_p(P
->Ray
[r
][P
->Dimension
+1]))
2602 int i1
= single_param_pos(P
, exist
, nparam
, r
);
2605 for (int r2
= r
+1; r2
< P
->NbRays
; ++r2
) {
2606 if (value_notzero_p(P
->Ray
[r2
][P
->Dimension
+1]))
2608 int i2
= single_param_pos(P
, exist
, nparam
, r2
);
2614 value_division(m
, P
->Ray
[r
][1+nvar
+exist
+i1
],
2615 P
->Ray
[r2
][1+nvar
+exist
+i1
]);
2616 value_multiply(m
, m
, P
->Ray
[r2
][1+nvar
+exist
+i1
]);
2617 /* r2 divides r => r redundant */
2618 if (value_eq(m
, P
->Ray
[r
][1+nvar
+exist
+i1
])) {
2620 return enumerate_remove_ray(P
, r
, exist
, nparam
, options
);
2623 value_division(m
, P
->Ray
[r2
][1+nvar
+exist
+i1
],
2624 P
->Ray
[r
][1+nvar
+exist
+i1
]);
2625 value_multiply(m
, m
, P
->Ray
[r
][1+nvar
+exist
+i1
]);
2626 /* r divides r2 => r2 redundant */
2627 if (value_eq(m
, P
->Ray
[r2
][1+nvar
+exist
+i1
])) {
2629 return enumerate_remove_ray(P
, r2
, exist
, nparam
, options
);
2637 static Polyhedron
*upper_bound(Polyhedron
*P
,
2638 int pos
, Value
*max
, Polyhedron
**R
)
2647 for (Polyhedron
*Q
= P
; Q
; Q
= N
) {
2649 for (r
= 0; r
< P
->NbRays
; ++r
) {
2650 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1]) &&
2651 value_pos_p(P
->Ray
[r
][1+pos
]))
2654 if (r
< P
->NbRays
) {
2662 for (r
= 0; r
< P
->NbRays
; ++r
) {
2663 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1]))
2665 mpz_fdiv_q(v
, P
->Ray
[r
][1+pos
], P
->Ray
[r
][1+P
->Dimension
]);
2666 if ((!Q
->next
&& r
== 0) || value_gt(v
, *max
))
2667 value_assign(*max
, v
);
2674 static evalue
* enumerate_ray(Polyhedron
*P
,
2675 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2677 assert(P
->NbBid
== 0);
2678 int nvar
= P
->Dimension
- exist
- nparam
;
2681 for (r
= 0; r
< P
->NbRays
; ++r
)
2682 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1]))
2688 for (r2
= r
+1; r2
< P
->NbRays
; ++r2
)
2689 if (value_zero_p(P
->Ray
[r2
][P
->Dimension
+1]))
2691 if (r2
< P
->NbRays
) {
2693 return enumerate_sum(P
, exist
, nparam
, options
);
2697 fprintf(stderr
, "\nER: Ray\n");
2698 #endif /* DEBUG_ER */
2704 value_set_si(one
, 1);
2705 int i
= single_param_pos(P
, exist
, nparam
, r
);
2706 assert(i
!= -1); // for now;
2708 Matrix
*M
= Matrix_Alloc(P
->NbRays
, P
->Dimension
+2);
2709 for (int j
= 0; j
< P
->NbRays
; ++j
) {
2710 Vector_Combine(P
->Ray
[j
], P
->Ray
[r
], M
->p
[j
],
2711 one
, P
->Ray
[j
][P
->Dimension
+1], P
->Dimension
+2);
2713 Polyhedron
*S
= Rays2Polyhedron(M
, options
->MaxRays
);
2715 Polyhedron
*D
= DomainDifference(P
, S
, options
->MaxRays
);
2717 // Polyhedron_Print(stderr, P_VALUE_FMT, D);
2718 assert(value_pos_p(P
->Ray
[r
][1+nvar
+exist
+i
])); // for now
2720 D
= upper_bound(D
, nvar
+exist
+i
, &m
, &R
);
2724 M
= Matrix_Alloc(2, P
->Dimension
+2);
2725 value_set_si(M
->p
[0][0], 1);
2726 value_set_si(M
->p
[1][0], 1);
2727 value_set_si(M
->p
[0][1+nvar
+exist
+i
], -1);
2728 value_set_si(M
->p
[1][1+nvar
+exist
+i
], 1);
2729 value_assign(M
->p
[0][1+P
->Dimension
], m
);
2730 value_oppose(M
->p
[1][1+P
->Dimension
], m
);
2731 value_addto(M
->p
[1][1+P
->Dimension
], M
->p
[1][1+P
->Dimension
],
2732 P
->Ray
[r
][1+nvar
+exist
+i
]);
2733 value_decrement(M
->p
[1][1+P
->Dimension
], M
->p
[1][1+P
->Dimension
]);
2734 // Matrix_Print(stderr, P_VALUE_FMT, M);
2735 D
= AddConstraints(M
->p
[0], 2, P
, options
->MaxRays
);
2736 // Polyhedron_Print(stderr, P_VALUE_FMT, D);
2737 value_subtract(M
->p
[0][1+P
->Dimension
], M
->p
[0][1+P
->Dimension
],
2738 P
->Ray
[r
][1+nvar
+exist
+i
]);
2739 // Matrix_Print(stderr, P_VALUE_FMT, M);
2740 S
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
2741 // Polyhedron_Print(stderr, P_VALUE_FMT, S);
2744 evalue
*EP
= barvinok_enumerate_e_with_options(D
, exist
, nparam
, options
);
2749 if (value_notone_p(P
->Ray
[r
][1+nvar
+exist
+i
]))
2750 EP
= enumerate_cyclic(P
, exist
, nparam
, EP
, r
, i
, options
->MaxRays
);
2752 M
= Matrix_Alloc(1, nparam
+2);
2753 value_set_si(M
->p
[0][0], 1);
2754 value_set_si(M
->p
[0][1+i
], 1);
2755 enumerate_vd_add_ray(EP
, M
, options
->MaxRays
);
2760 evalue
*E
= barvinok_enumerate_e_with_options(S
, exist
, nparam
, options
);
2762 free_evalue_refs(E
);
2769 evalue
*ER
= enumerate_or(R
, exist
, nparam
, options
);
2771 free_evalue_refs(ER
);
2778 static evalue
* enumerate_vd(Polyhedron
**PA
,
2779 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2781 Polyhedron
*P
= *PA
;
2782 int nvar
= P
->Dimension
- exist
- nparam
;
2783 Param_Polyhedron
*PP
= NULL
;
2784 Polyhedron
*C
= Universe_Polyhedron(nparam
);
2788 PP
= Polyhedron2Param_SimplifiedDomain(&PR
,C
, options
->MaxRays
,&CEq
,&CT
);
2792 Param_Domain
*D
, *last
;
2795 for (nd
= 0, D
=PP
->D
; D
; D
=D
->next
, ++nd
)
2798 Polyhedron
**VD
= new Polyhedron_p
[nd
];
2799 Polyhedron
*TC
= true_context(P
, CT
, CEq
? CEq
: C
, options
->MaxRays
);
2800 FORALL_REDUCED_DOMAIN(PP
, TC
, CT
, CEq
, nd
, options
, i
, D
, rVD
)
2803 END_FORALL_REDUCED_DOMAIN
2804 Polyhedron_Free(TC
);
2811 /* This doesn't seem to have any effect */
2813 Polyhedron
*CA
= align_context(VD
[0], P
->Dimension
, options
->MaxRays
);
2815 P
= DomainIntersection(P
, CA
, options
->MaxRays
);
2818 Polyhedron_Free(CA
);
2823 if (!EP
&& CT
->NbColumns
!= CT
->NbRows
) {
2824 Polyhedron
*CEqr
= DomainImage(CEq
, CT
, options
->MaxRays
);
2825 Polyhedron
*CA
= align_context(CEqr
, PR
->Dimension
, options
->MaxRays
);
2826 Polyhedron
*I
= DomainIntersection(PR
, CA
, options
->MaxRays
);
2827 Polyhedron_Free(CEqr
);
2828 Polyhedron_Free(CA
);
2830 fprintf(stderr
, "\nER: Eliminate\n");
2831 #endif /* DEBUG_ER */
2832 nparam
-= CT
->NbColumns
- CT
->NbRows
;
2833 EP
= barvinok_enumerate_e_with_options(I
, exist
, nparam
, options
);
2834 nparam
+= CT
->NbColumns
- CT
->NbRows
;
2835 addeliminatedparams_enum(EP
, CT
, CEq
, options
->MaxRays
, nparam
);
2839 Polyhedron_Free(PR
);
2842 if (!EP
&& nd
> 1) {
2844 fprintf(stderr
, "\nER: VD\n");
2845 #endif /* DEBUG_ER */
2846 for (int i
= 0; i
< nd
; ++i
) {
2847 Polyhedron
*CA
= align_context(VD
[i
], P
->Dimension
, options
->MaxRays
);
2848 Polyhedron
*I
= DomainIntersection(P
, CA
, options
->MaxRays
);
2851 EP
= barvinok_enumerate_e_with_options(I
, exist
, nparam
, options
);
2853 evalue
*E
= barvinok_enumerate_e_with_options(I
, exist
, nparam
,
2856 free_evalue_refs(E
);
2860 Polyhedron_Free(CA
);
2864 for (int i
= 0; i
< nd
; ++i
)
2865 Polyhedron_Free(VD
[i
]);
2869 if (!EP
&& nvar
== 0) {
2872 Param_Vertices
*V
, *V2
;
2873 Matrix
* M
= Matrix_Alloc(1, P
->Dimension
+2);
2875 FORALL_PVertex_in_ParamPolyhedron(V
, last
, PP
) {
2877 FORALL_PVertex_in_ParamPolyhedron(V2
, last
, PP
) {
2884 for (int i
= 0; i
< exist
; ++i
) {
2885 value_oppose(f
, V
->Vertex
->p
[i
][nparam
+1]);
2886 Vector_Combine(V
->Vertex
->p
[i
],
2888 M
->p
[0] + 1 + nvar
+ exist
,
2889 V2
->Vertex
->p
[i
][nparam
+1],
2893 for (j
= 0; j
< nparam
; ++j
)
2894 if (value_notzero_p(M
->p
[0][1+nvar
+exist
+j
]))
2898 ConstraintSimplify(M
->p
[0], M
->p
[0],
2899 P
->Dimension
+2, &f
);
2900 value_set_si(M
->p
[0][0], 0);
2901 Polyhedron
*para
= AddConstraints(M
->p
[0], 1, P
,
2904 Polyhedron_Free(para
);
2907 Polyhedron
*pos
, *neg
;
2908 value_set_si(M
->p
[0][0], 1);
2909 value_decrement(M
->p
[0][P
->Dimension
+1],
2910 M
->p
[0][P
->Dimension
+1]);
2911 neg
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
2912 value_set_si(f
, -1);
2913 Vector_Scale(M
->p
[0]+1, M
->p
[0]+1, f
,
2915 value_decrement(M
->p
[0][P
->Dimension
+1],
2916 M
->p
[0][P
->Dimension
+1]);
2917 value_decrement(M
->p
[0][P
->Dimension
+1],
2918 M
->p
[0][P
->Dimension
+1]);
2919 pos
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
2920 if (emptyQ(neg
) && emptyQ(pos
)) {
2921 Polyhedron_Free(para
);
2922 Polyhedron_Free(pos
);
2923 Polyhedron_Free(neg
);
2927 fprintf(stderr
, "\nER: Order\n");
2928 #endif /* DEBUG_ER */
2929 EP
= barvinok_enumerate_e_with_options(para
, exist
, nparam
,
2933 E
= barvinok_enumerate_e_with_options(pos
, exist
, nparam
,
2936 free_evalue_refs(E
);
2940 E
= barvinok_enumerate_e_with_options(neg
, exist
, nparam
,
2943 free_evalue_refs(E
);
2946 Polyhedron_Free(para
);
2947 Polyhedron_Free(pos
);
2948 Polyhedron_Free(neg
);
2953 } END_FORALL_PVertex_in_ParamPolyhedron
;
2956 } END_FORALL_PVertex_in_ParamPolyhedron
;
2959 /* Search for vertex coordinate to split on */
2960 /* First look for one independent of the parameters */
2961 FORALL_PVertex_in_ParamPolyhedron(V
, last
, PP
) {
2962 for (int i
= 0; i
< exist
; ++i
) {
2964 for (j
= 0; j
< nparam
; ++j
)
2965 if (value_notzero_p(V
->Vertex
->p
[i
][j
]))
2969 value_set_si(M
->p
[0][0], 1);
2970 Vector_Set(M
->p
[0]+1, 0, nvar
+exist
);
2971 Vector_Copy(V
->Vertex
->p
[i
],
2972 M
->p
[0] + 1 + nvar
+ exist
, nparam
+1);
2973 value_oppose(M
->p
[0][1+nvar
+i
],
2974 V
->Vertex
->p
[i
][nparam
+1]);
2976 Polyhedron
*pos
, *neg
;
2977 value_set_si(M
->p
[0][0], 1);
2978 value_decrement(M
->p
[0][P
->Dimension
+1],
2979 M
->p
[0][P
->Dimension
+1]);
2980 neg
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
2981 value_set_si(f
, -1);
2982 Vector_Scale(M
->p
[0]+1, M
->p
[0]+1, f
,
2984 value_decrement(M
->p
[0][P
->Dimension
+1],
2985 M
->p
[0][P
->Dimension
+1]);
2986 value_decrement(M
->p
[0][P
->Dimension
+1],
2987 M
->p
[0][P
->Dimension
+1]);
2988 pos
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
2989 if (emptyQ(neg
) || emptyQ(pos
)) {
2990 Polyhedron_Free(pos
);
2991 Polyhedron_Free(neg
);
2994 Polyhedron_Free(pos
);
2995 value_increment(M
->p
[0][P
->Dimension
+1],
2996 M
->p
[0][P
->Dimension
+1]);
2997 pos
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
2999 fprintf(stderr
, "\nER: Vertex\n");
3000 #endif /* DEBUG_ER */
3002 EP
= enumerate_or(pos
, exist
, nparam
, options
);
3007 } END_FORALL_PVertex_in_ParamPolyhedron
;
3011 /* Search for vertex coordinate to split on */
3012 /* Now look for one that depends on the parameters */
3013 FORALL_PVertex_in_ParamPolyhedron(V
, last
, PP
) {
3014 for (int i
= 0; i
< exist
; ++i
) {
3015 value_set_si(M
->p
[0][0], 1);
3016 Vector_Set(M
->p
[0]+1, 0, nvar
+exist
);
3017 Vector_Copy(V
->Vertex
->p
[i
],
3018 M
->p
[0] + 1 + nvar
+ exist
, nparam
+1);
3019 value_oppose(M
->p
[0][1+nvar
+i
],
3020 V
->Vertex
->p
[i
][nparam
+1]);
3022 Polyhedron
*pos
, *neg
;
3023 value_set_si(M
->p
[0][0], 1);
3024 value_decrement(M
->p
[0][P
->Dimension
+1],
3025 M
->p
[0][P
->Dimension
+1]);
3026 neg
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
3027 value_set_si(f
, -1);
3028 Vector_Scale(M
->p
[0]+1, M
->p
[0]+1, f
,
3030 value_decrement(M
->p
[0][P
->Dimension
+1],
3031 M
->p
[0][P
->Dimension
+1]);
3032 value_decrement(M
->p
[0][P
->Dimension
+1],
3033 M
->p
[0][P
->Dimension
+1]);
3034 pos
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
3035 if (emptyQ(neg
) || emptyQ(pos
)) {
3036 Polyhedron_Free(pos
);
3037 Polyhedron_Free(neg
);
3040 Polyhedron_Free(pos
);
3041 value_increment(M
->p
[0][P
->Dimension
+1],
3042 M
->p
[0][P
->Dimension
+1]);
3043 pos
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
3045 fprintf(stderr
, "\nER: ParamVertex\n");
3046 #endif /* DEBUG_ER */
3048 EP
= enumerate_or(pos
, exist
, nparam
, options
);
3053 } END_FORALL_PVertex_in_ParamPolyhedron
;
3061 Polyhedron_Free(CEq
);
3065 Param_Polyhedron_Free(PP
);
3071 evalue
* barvinok_enumerate_pip(Polyhedron
*P
, unsigned exist
, unsigned nparam
,
3075 barvinok_options
*options
= barvinok_options_new_with_defaults();
3076 options
->MaxRays
= MaxRays
;
3077 E
= barvinok_enumerate_pip_with_options(P
, exist
, nparam
, options
);
3078 barvinok_options_free(options
);
3083 evalue
*barvinok_enumerate_pip_with_options(Polyhedron
*P
,
3084 unsigned exist
, unsigned nparam
, struct barvinok_options
*options
)
3089 evalue
*barvinok_enumerate_pip_with_options(Polyhedron
*P
,
3090 unsigned exist
, unsigned nparam
, struct barvinok_options
*options
)
3092 int nvar
= P
->Dimension
- exist
- nparam
;
3093 evalue
*EP
= evalue_zero();
3097 fprintf(stderr
, "\nER: PIP\n");
3098 #endif /* DEBUG_ER */
3100 Polyhedron
*D
= pip_projectout(P
, nvar
, exist
, nparam
);
3101 for (Q
= D
; Q
; Q
= N
) {
3105 exist
= Q
->Dimension
- nvar
- nparam
;
3106 E
= barvinok_enumerate_e_with_options(Q
, exist
, nparam
, options
);
3109 free_evalue_refs(E
);
3118 static bool is_single(Value
*row
, int pos
, int len
)
3120 return First_Non_Zero(row
, pos
) == -1 &&
3121 First_Non_Zero(row
+pos
+1, len
-pos
-1) == -1;
3124 static evalue
* barvinok_enumerate_e_r(Polyhedron
*P
,
3125 unsigned exist
, unsigned nparam
, barvinok_options
*options
);
3128 static int er_level
= 0;
3130 evalue
* barvinok_enumerate_e_with_options(Polyhedron
*P
,
3131 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
3133 fprintf(stderr
, "\nER: level %i\n", er_level
);
3135 Polyhedron_PrintConstraints(stderr
, P_VALUE_FMT
, P
);
3136 fprintf(stderr
, "\nE %d\nP %d\n", exist
, nparam
);
3138 P
= DomainConstraintSimplify(Polyhedron_Copy(P
), options
->MaxRays
);
3139 evalue
*EP
= barvinok_enumerate_e_r(P
, exist
, nparam
, options
);
3145 evalue
* barvinok_enumerate_e_with_options(Polyhedron
*P
,
3146 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
3148 P
= DomainConstraintSimplify(Polyhedron_Copy(P
), options
->MaxRays
);
3149 evalue
*EP
= barvinok_enumerate_e_r(P
, exist
, nparam
, options
);
3155 evalue
* barvinok_enumerate_e(Polyhedron
*P
, unsigned exist
, unsigned nparam
,
3159 barvinok_options
*options
= barvinok_options_new_with_defaults();
3160 options
->MaxRays
= MaxRays
;
3161 E
= barvinok_enumerate_e_with_options(P
, exist
, nparam
, options
);
3162 barvinok_options_free(options
);
3166 static evalue
* barvinok_enumerate_e_r(Polyhedron
*P
,
3167 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
3170 Polyhedron
*U
= Universe_Polyhedron(nparam
);
3171 evalue
*EP
= barvinok_enumerate_with_options(P
, U
, options
);
3172 //char *param_name[] = {"P", "Q", "R", "S", "T" };
3173 //print_evalue(stdout, EP, param_name);
3178 int nvar
= P
->Dimension
- exist
- nparam
;
3179 int len
= P
->Dimension
+ 2;
3182 POL_ENSURE_FACETS(P
);
3183 POL_ENSURE_VERTICES(P
);
3186 return evalue_zero();
3188 if (nvar
== 0 && nparam
== 0) {
3189 evalue
*EP
= evalue_zero();
3190 barvinok_count_with_options(P
, &EP
->x
.n
, options
);
3191 if (value_pos_p(EP
->x
.n
))
3192 value_set_si(EP
->x
.n
, 1);
3197 for (r
= 0; r
< P
->NbRays
; ++r
)
3198 if (value_zero_p(P
->Ray
[r
][0]) ||
3199 value_zero_p(P
->Ray
[r
][P
->Dimension
+1])) {
3201 for (i
= 0; i
< nvar
; ++i
)
3202 if (value_notzero_p(P
->Ray
[r
][i
+1]))
3206 for (i
= nvar
+ exist
; i
< nvar
+ exist
+ nparam
; ++i
)
3207 if (value_notzero_p(P
->Ray
[r
][i
+1]))
3209 if (i
>= nvar
+ exist
+ nparam
)
3212 if (r
< P
->NbRays
) {
3213 evalue
*EP
= evalue_zero();
3214 value_set_si(EP
->x
.n
, -1);
3219 for (r
= 0; r
< P
->NbEq
; ++r
)
3220 if ((first
= First_Non_Zero(P
->Constraint
[r
]+1+nvar
, exist
)) != -1)
3223 if (First_Non_Zero(P
->Constraint
[r
]+1+nvar
+first
+1,
3224 exist
-first
-1) != -1) {
3225 Polyhedron
*T
= rotate_along(P
, r
, nvar
, exist
, options
->MaxRays
);
3227 fprintf(stderr
, "\nER: Equality\n");
3228 #endif /* DEBUG_ER */
3229 evalue
*EP
= barvinok_enumerate_e_with_options(T
, exist
-1, nparam
,
3235 fprintf(stderr
, "\nER: Fixed\n");
3236 #endif /* DEBUG_ER */
3238 return barvinok_enumerate_e_with_options(P
, exist
-1, nparam
,
3241 Polyhedron
*T
= Polyhedron_Copy(P
);
3242 SwapColumns(T
, nvar
+1, nvar
+1+first
);
3243 evalue
*EP
= barvinok_enumerate_e_with_options(T
, exist
-1, nparam
,
3251 Vector
*row
= Vector_Alloc(len
);
3252 value_set_si(row
->p
[0], 1);
3257 enum constraint
* info
= new constraint
[exist
];
3258 for (int i
= 0; i
< exist
; ++i
) {
3260 for (int l
= P
->NbEq
; l
< P
->NbConstraints
; ++l
) {
3261 if (value_negz_p(P
->Constraint
[l
][nvar
+i
+1]))
3263 bool l_parallel
= is_single(P
->Constraint
[l
]+nvar
+1, i
, exist
);
3264 for (int u
= P
->NbEq
; u
< P
->NbConstraints
; ++u
) {
3265 if (value_posz_p(P
->Constraint
[u
][nvar
+i
+1]))
3267 bool lu_parallel
= l_parallel
||
3268 is_single(P
->Constraint
[u
]+nvar
+1, i
, exist
);
3269 value_oppose(f
, P
->Constraint
[u
][nvar
+i
+1]);
3270 Vector_Combine(P
->Constraint
[l
]+1, P
->Constraint
[u
]+1, row
->p
+1,
3271 f
, P
->Constraint
[l
][nvar
+i
+1], len
-1);
3272 if (!(info
[i
] & INDEPENDENT
)) {
3274 for (j
= 0; j
< exist
; ++j
)
3275 if (j
!= i
&& value_notzero_p(row
->p
[nvar
+j
+1]))
3278 //printf("independent: i: %d, l: %d, u: %d\n", i, l, u);
3279 info
[i
] = (constraint
)(info
[i
] | INDEPENDENT
);
3282 if (info
[i
] & ALL_POS
) {
3283 value_addto(row
->p
[len
-1], row
->p
[len
-1],
3284 P
->Constraint
[l
][nvar
+i
+1]);
3285 value_addto(row
->p
[len
-1], row
->p
[len
-1], f
);
3286 value_multiply(f
, f
, P
->Constraint
[l
][nvar
+i
+1]);
3287 value_subtract(row
->p
[len
-1], row
->p
[len
-1], f
);
3288 value_decrement(row
->p
[len
-1], row
->p
[len
-1]);
3289 ConstraintSimplify(row
->p
, row
->p
, len
, &f
);
3290 value_set_si(f
, -1);
3291 Vector_Scale(row
->p
+1, row
->p
+1, f
, len
-1);
3292 value_decrement(row
->p
[len
-1], row
->p
[len
-1]);
3293 Polyhedron
*T
= AddConstraints(row
->p
, 1, P
, options
->MaxRays
);
3295 //printf("not all_pos: i: %d, l: %d, u: %d\n", i, l, u);
3296 info
[i
] = (constraint
)(info
[i
] ^ ALL_POS
);
3298 //puts("pos remainder");
3299 //Polyhedron_Print(stdout, P_VALUE_FMT, T);
3302 if (!(info
[i
] & ONE_NEG
)) {
3304 negative_test_constraint(P
->Constraint
[l
],
3306 row
->p
, nvar
+i
, len
, &f
);
3307 oppose_constraint(row
->p
, len
, &f
);
3308 Polyhedron
*T
= AddConstraints(row
->p
, 1, P
,
3311 //printf("one_neg i: %d, l: %d, u: %d\n", i, l, u);
3312 info
[i
] = (constraint
)(info
[i
] | ONE_NEG
);
3314 //puts("neg remainder");
3315 //Polyhedron_Print(stdout, P_VALUE_FMT, T);
3317 } else if (!(info
[i
] & ROT_NEG
)) {
3318 if (parallel_constraints(P
->Constraint
[l
],
3320 row
->p
, nvar
, exist
)) {
3321 negative_test_constraint7(P
->Constraint
[l
],
3323 row
->p
, nvar
, exist
,
3325 oppose_constraint(row
->p
, len
, &f
);
3326 Polyhedron
*T
= AddConstraints(row
->p
, 1, P
,
3329 // printf("rot_neg i: %d, l: %d, u: %d\n", i, l, u);
3330 info
[i
] = (constraint
)(info
[i
] | ROT_NEG
);
3333 //puts("neg remainder");
3334 //Polyhedron_Print(stdout, P_VALUE_FMT, T);
3339 if (!(info
[i
] & ALL_POS
) && (info
[i
] & (ONE_NEG
| ROT_NEG
)))
3343 if (info
[i
] & ALL_POS
)
3350 for (int i = 0; i < exist; ++i)
3351 printf("%i: %i\n", i, info[i]);
3353 for (int i
= 0; i
< exist
; ++i
)
3354 if (info
[i
] & ALL_POS
) {
3356 fprintf(stderr
, "\nER: Positive\n");
3357 #endif /* DEBUG_ER */
3359 // Maybe we should chew off some of the fat here
3360 Matrix
*M
= Matrix_Alloc(P
->Dimension
, P
->Dimension
+1);
3361 for (int j
= 0; j
< P
->Dimension
; ++j
)
3362 value_set_si(M
->p
[j
][j
+ (j
>= i
+nvar
)], 1);
3363 Polyhedron
*T
= Polyhedron_Image(P
, M
, options
->MaxRays
);
3365 evalue
*EP
= barvinok_enumerate_e_with_options(T
, exist
-1, nparam
,
3373 for (int i
= 0; i
< exist
; ++i
)
3374 if (info
[i
] & ONE_NEG
) {
3376 fprintf(stderr
, "\nER: Negative\n");
3377 #endif /* DEBUG_ER */
3382 return barvinok_enumerate_e_with_options(P
, exist
-1, nparam
,
3385 Polyhedron
*T
= Polyhedron_Copy(P
);
3386 SwapColumns(T
, nvar
+1, nvar
+1+i
);
3387 evalue
*EP
= barvinok_enumerate_e_with_options(T
, exist
-1, nparam
,
3393 for (int i
= 0; i
< exist
; ++i
)
3394 if (info
[i
] & ROT_NEG
) {
3396 fprintf(stderr
, "\nER: Rotate\n");
3397 #endif /* DEBUG_ER */
3401 Polyhedron
*T
= rotate_along(P
, r
, nvar
, exist
, options
->MaxRays
);
3402 evalue
*EP
= barvinok_enumerate_e_with_options(T
, exist
-1, nparam
,
3407 for (int i
= 0; i
< exist
; ++i
)
3408 if (info
[i
] & INDEPENDENT
) {
3409 Polyhedron
*pos
, *neg
;
3411 /* Find constraint again and split off negative part */
3413 if (SplitOnVar(P
, i
, nvar
, exist
, options
->MaxRays
,
3414 row
, f
, true, &pos
, &neg
)) {
3416 fprintf(stderr
, "\nER: Split\n");
3417 #endif /* DEBUG_ER */
3420 barvinok_enumerate_e_with_options(neg
, exist
-1, nparam
, options
);
3422 barvinok_enumerate_e_with_options(pos
, exist
, nparam
, options
);
3424 free_evalue_refs(E
);
3426 Polyhedron_Free(neg
);
3427 Polyhedron_Free(pos
);
3441 EP
= enumerate_line(P
, exist
, nparam
, options
);
3445 EP
= barvinok_enumerate_pip_with_options(P
, exist
, nparam
, options
);
3449 EP
= enumerate_redundant_ray(P
, exist
, nparam
, options
);
3453 EP
= enumerate_sure(P
, exist
, nparam
, options
);
3457 EP
= enumerate_ray(P
, exist
, nparam
, options
);
3461 EP
= enumerate_sure2(P
, exist
, nparam
, options
);
3465 F
= unfringe(P
, options
->MaxRays
);
3466 if (!PolyhedronIncludes(F
, P
)) {
3468 fprintf(stderr
, "\nER: Fringed\n");
3469 #endif /* DEBUG_ER */
3470 EP
= barvinok_enumerate_e_with_options(F
, exist
, nparam
, options
);
3477 EP
= enumerate_vd(&P
, exist
, nparam
, options
);
3482 EP
= enumerate_sum(P
, exist
, nparam
, options
);
3489 Polyhedron
*pos
, *neg
;
3490 for (i
= 0; i
< exist
; ++i
)
3491 if (SplitOnVar(P
, i
, nvar
, exist
, options
->MaxRays
,
3492 row
, f
, false, &pos
, &neg
))
3498 EP
= enumerate_or(pos
, exist
, nparam
, options
);
3511 * remove equalities that require a "compression" of the parameters
3513 static Polyhedron
*remove_more_equalities(Polyhedron
*P
, unsigned nparam
,
3514 Matrix
**CP
, unsigned MaxRays
)
3517 remove_all_equalities(&P
, NULL
, CP
, NULL
, nparam
, MaxRays
);
3524 static gen_fun
*series(Polyhedron
*P
, unsigned nparam
, barvinok_options
*options
)
3534 assert(!Polyhedron_is_unbounded(P
, nparam
, options
->MaxRays
));
3535 assert(P
->NbBid
== 0);
3536 assert(Polyhedron_has_revlex_positive_rays(P
, nparam
));
3538 P
= remove_more_equalities(P
, nparam
, &CP
, options
->MaxRays
);
3539 assert(P
->NbEq
== 0);
3541 nparam
= CP
->NbColumns
-1;
3546 barvinok_count_with_options(P
, &c
, options
);
3547 gf
= new gen_fun(c
);
3551 red
= gf_base::create(Polyhedron_Project(P
, nparam
),
3552 P
->Dimension
, nparam
, options
);
3553 POL_ENSURE_VERTICES(P
);
3554 red
->start_gf(P
, options
);
3566 gen_fun
* barvinok_series_with_options(Polyhedron
*P
, Polyhedron
* C
,
3567 barvinok_options
*options
)
3570 unsigned nparam
= C
->Dimension
;
3573 CA
= align_context(C
, P
->Dimension
, options
->MaxRays
);
3574 P
= DomainIntersection(P
, CA
, options
->MaxRays
);
3575 Polyhedron_Free(CA
);
3577 gf
= series(P
, nparam
, options
);
3582 gen_fun
* barvinok_series(Polyhedron
*P
, Polyhedron
* C
, unsigned MaxRays
)
3585 barvinok_options
*options
= barvinok_options_new_with_defaults();
3586 options
->MaxRays
= MaxRays
;
3587 gf
= barvinok_series_with_options(P
, C
, options
);
3588 barvinok_options_free(options
);
3592 static Polyhedron
*skew_into_positive_orthant(Polyhedron
*D
, unsigned nparam
,
3598 for (Polyhedron
*P
= D
; P
; P
= P
->next
) {
3599 POL_ENSURE_VERTICES(P
);
3600 assert(!Polyhedron_is_unbounded(P
, nparam
, MaxRays
));
3601 assert(P
->NbBid
== 0);
3602 assert(Polyhedron_has_positive_rays(P
, nparam
));
3604 for (int r
= 0; r
< P
->NbRays
; ++r
) {
3605 if (value_notzero_p(P
->Ray
[r
][P
->Dimension
+1]))
3607 for (int i
= 0; i
< nparam
; ++i
) {
3609 if (value_posz_p(P
->Ray
[r
][i
+1]))
3612 M
= Matrix_Alloc(D
->Dimension
+1, D
->Dimension
+1);
3613 for (int i
= 0; i
< D
->Dimension
+1; ++i
)
3614 value_set_si(M
->p
[i
][i
], 1);
3616 Inner_Product(P
->Ray
[r
]+1, M
->p
[i
], D
->Dimension
+1, &tmp
);
3617 if (value_posz_p(tmp
))
3620 for (j
= P
->Dimension
- nparam
; j
< P
->Dimension
; ++j
)
3621 if (value_pos_p(P
->Ray
[r
][j
+1]))
3623 assert(j
< P
->Dimension
);
3624 value_pdivision(tmp
, P
->Ray
[r
][j
+1], P
->Ray
[r
][i
+1]);
3625 value_subtract(M
->p
[i
][j
], M
->p
[i
][j
], tmp
);
3631 D
= DomainImage(D
, M
, MaxRays
);
3637 gen_fun
* barvinok_enumerate_union_series_with_options(Polyhedron
*D
, Polyhedron
* C
,
3638 barvinok_options
*options
)
3640 Polyhedron
*conv
, *D2
;
3642 gen_fun
*gf
= NULL
, *gf2
;
3643 unsigned nparam
= C
->Dimension
;
3648 CA
= align_context(C
, D
->Dimension
, options
->MaxRays
);
3649 D
= DomainIntersection(D
, CA
, options
->MaxRays
);
3650 Polyhedron_Free(CA
);
3652 D2
= skew_into_positive_orthant(D
, nparam
, options
->MaxRays
);
3653 for (Polyhedron
*P
= D2
; P
; P
= P
->next
) {
3654 assert(P
->Dimension
== D2
->Dimension
);
3657 P_gf
= series(Polyhedron_Copy(P
), nparam
, options
);
3661 gf
->add_union(P_gf
, options
);
3665 /* we actually only need the convex union of the parameter space
3666 * but the reducer classes currently expect a polyhedron in
3667 * the combined space
3669 Polyhedron_Free(gf
->context
);
3670 gf
->context
= DomainConvex(D2
, options
->MaxRays
);
3672 gf2
= gf
->summate(D2
->Dimension
- nparam
, options
);
3681 gen_fun
* barvinok_enumerate_union_series(Polyhedron
*D
, Polyhedron
* C
,
3685 barvinok_options
*options
= barvinok_options_new_with_defaults();
3686 options
->MaxRays
= MaxRays
;
3687 gf
= barvinok_enumerate_union_series_with_options(D
, C
, options
);
3688 barvinok_options_free(options
);
3692 evalue
* barvinok_enumerate_union(Polyhedron
*D
, Polyhedron
* C
, unsigned MaxRays
)
3695 gen_fun
*gf
= barvinok_enumerate_union_series(D
, C
, MaxRays
);