8 #include <NTL/mat_ZZ.h>
10 #include <barvinok/util.h>
11 #include <barvinok/evalue.h>
16 #include <barvinok/barvinok.h>
17 #include <barvinok/genfun.h>
18 #include <barvinok/options.h>
19 #include <barvinok/sample.h>
20 #include "conversion.h"
21 #include "decomposer.h"
22 #include "lattice_point.h"
23 #include "reduce_domain.h"
24 #include "genfun_constructor.h"
25 #include "remove_equalities.h"
36 using std::ostringstream
;
38 #define ALLOC(t,p) p = (t*)malloc(sizeof(*p))
46 dpoly_n(int d
, ZZ
& degree_0
, ZZ
& degree_1
, int offset
= 0) {
50 zz2value(degree_0
, d0
);
51 zz2value(degree_1
, d1
);
52 coeff
= Matrix_Alloc(d
+1, d
+1+1);
53 value_set_si(coeff
->p
[0][0], 1);
54 value_set_si(coeff
->p
[0][d
+1], 1);
55 for (int i
= 1; i
<= d
; ++i
) {
56 value_multiply(coeff
->p
[i
][0], coeff
->p
[i
-1][0], d0
);
57 Vector_Combine(coeff
->p
[i
-1], coeff
->p
[i
-1]+1, coeff
->p
[i
]+1,
59 value_set_si(coeff
->p
[i
][d
+1], i
);
60 value_multiply(coeff
->p
[i
][d
+1], coeff
->p
[i
][d
+1], coeff
->p
[i
-1][d
+1]);
61 value_decrement(d0
, d0
);
66 void div(dpoly
& d
, Vector
*count
, ZZ
& sign
) {
67 int len
= coeff
->NbRows
;
68 Matrix
* c
= Matrix_Alloc(coeff
->NbRows
, coeff
->NbColumns
);
71 for (int i
= 0; i
< len
; ++i
) {
72 Vector_Copy(coeff
->p
[i
], c
->p
[i
], len
+1);
73 for (int j
= 1; j
<= i
; ++j
) {
74 zz2value(d
.coeff
[j
], tmp
);
75 value_multiply(tmp
, tmp
, c
->p
[i
][len
]);
76 value_oppose(tmp
, tmp
);
77 Vector_Combine(c
->p
[i
], c
->p
[i
-j
], c
->p
[i
],
78 c
->p
[i
-j
][len
], tmp
, len
);
79 value_multiply(c
->p
[i
][len
], c
->p
[i
][len
], c
->p
[i
-j
][len
]);
81 zz2value(d
.coeff
[0], tmp
);
82 value_multiply(c
->p
[i
][len
], c
->p
[i
][len
], tmp
);
85 value_set_si(tmp
, -1);
86 Vector_Scale(c
->p
[len
-1], count
->p
, tmp
, len
);
87 value_assign(count
->p
[len
], c
->p
[len
-1][len
]);
89 Vector_Copy(c
->p
[len
-1], count
->p
, len
+1);
90 Vector_Normalize(count
->p
, len
+1);
98 * Searches for a vector that is not orthogonal to any
99 * of the rays in rays.
101 static void nonorthog(mat_ZZ
& rays
, vec_ZZ
& lambda
)
103 int dim
= rays
.NumCols();
105 lambda
.SetLength(dim
);
109 for (int i
= 2; !found
&& i
<= 50*dim
; i
+=4) {
110 for (int j
= 0; j
< MAX_TRY
; ++j
) {
111 for (int k
= 0; k
< dim
; ++k
) {
112 int r
= random_int(i
)+2;
113 int v
= (2*(r
%2)-1) * (r
>> 1);
117 for (; k
< rays
.NumRows(); ++k
)
118 if (lambda
* rays
[k
] == 0)
120 if (k
== rays
.NumRows()) {
129 static void add_rays(mat_ZZ
& rays
, Polyhedron
*i
, int *r
, int nvar
= -1,
132 unsigned dim
= i
->Dimension
;
135 for (int k
= 0; k
< i
->NbRays
; ++k
) {
136 if (!value_zero_p(i
->Ray
[k
][dim
+1]))
138 if (!all
&& nvar
!= dim
&& First_Non_Zero(i
->Ray
[k
]+1, nvar
) == -1)
140 values2zz(i
->Ray
[k
]+1, rays
[(*r
)++], nvar
);
144 static void mask_r(Matrix
*f
, int nr
, Vector
*lcm
, int p
, Vector
*val
, evalue
*ev
)
146 unsigned nparam
= lcm
->Size
;
149 Vector
* prod
= Vector_Alloc(f
->NbRows
);
150 Matrix_Vector_Product(f
, val
->p
, prod
->p
);
152 for (int i
= 0; i
< nr
; ++i
) {
153 value_modulus(prod
->p
[i
], prod
->p
[i
], f
->p
[i
][nparam
+1]);
154 isint
&= value_zero_p(prod
->p
[i
]);
156 value_set_si(ev
->d
, 1);
158 value_set_si(ev
->x
.n
, isint
);
165 if (value_one_p(lcm
->p
[p
]))
166 mask_r(f
, nr
, lcm
, p
+1, val
, ev
);
168 value_assign(tmp
, lcm
->p
[p
]);
169 value_set_si(ev
->d
, 0);
170 ev
->x
.p
= new_enode(periodic
, VALUE_TO_INT(tmp
), p
+1);
172 value_decrement(tmp
, tmp
);
173 value_assign(val
->p
[p
], tmp
);
174 mask_r(f
, nr
, lcm
, p
+1, val
, &ev
->x
.p
->arr
[VALUE_TO_INT(tmp
)]);
175 } while (value_pos_p(tmp
));
180 static void mask_fractional(Matrix
*f
, evalue
*factor
)
182 int nr
= f
->NbRows
, nc
= f
->NbColumns
;
185 for (n
= 0; n
< nr
&& value_notzero_p(f
->p
[n
][nc
-1]); ++n
)
186 if (value_notone_p(f
->p
[n
][nc
-1]) &&
187 value_notmone_p(f
->p
[n
][nc
-1]))
201 value_set_si(EV
.x
.n
, 1);
203 for (n
= 0; n
< nr
; ++n
) {
204 value_assign(m
, f
->p
[n
][nc
-1]);
205 if (value_one_p(m
) || value_mone_p(m
))
208 int j
= normal_mod(f
->p
[n
], nc
-1, &m
);
210 free_evalue_refs(factor
);
211 value_init(factor
->d
);
212 evalue_set_si(factor
, 0, 1);
216 values2zz(f
->p
[n
], row
, nc
-1);
219 if (j
< (nc
-1)-1 && row
[j
] > g
/2) {
220 for (int k
= j
; k
< (nc
-1); ++k
)
226 value_set_si(EP
.d
, 0);
227 EP
.x
.p
= new_enode(relation
, 2, 0);
228 value_clear(EP
.x
.p
->arr
[1].d
);
229 EP
.x
.p
->arr
[1] = *factor
;
230 evalue
*ev
= &EP
.x
.p
->arr
[0];
231 value_set_si(ev
->d
, 0);
232 ev
->x
.p
= new_enode(fractional
, 3, -1);
233 evalue_set_si(&ev
->x
.p
->arr
[1], 0, 1);
234 evalue_set_si(&ev
->x
.p
->arr
[2], 1, 1);
235 evalue
*E
= multi_monom(row
);
236 value_assign(EV
.d
, m
);
238 value_clear(ev
->x
.p
->arr
[0].d
);
239 ev
->x
.p
->arr
[0] = *E
;
245 free_evalue_refs(&EV
);
251 static void mask_table(Matrix
*f
, evalue
*factor
)
253 int nr
= f
->NbRows
, nc
= f
->NbColumns
;
256 for (n
= 0; n
< nr
&& value_notzero_p(f
->p
[n
][nc
-1]); ++n
)
257 if (value_notone_p(f
->p
[n
][nc
-1]) &&
258 value_notmone_p(f
->p
[n
][nc
-1]))
266 unsigned np
= nc
- 2;
267 Vector
*lcm
= Vector_Alloc(np
);
268 Vector
*val
= Vector_Alloc(nc
);
269 Vector_Set(val
->p
, 0, nc
);
270 value_set_si(val
->p
[np
], 1);
271 Vector_Set(lcm
->p
, 1, np
);
272 for (n
= 0; n
< nr
; ++n
) {
273 if (value_one_p(f
->p
[n
][nc
-1]) ||
274 value_mone_p(f
->p
[n
][nc
-1]))
276 for (int j
= 0; j
< np
; ++j
)
277 if (value_notzero_p(f
->p
[n
][j
])) {
278 Gcd(f
->p
[n
][j
], f
->p
[n
][nc
-1], &tmp
);
279 value_division(tmp
, f
->p
[n
][nc
-1], tmp
);
280 value_lcm(tmp
, lcm
->p
[j
], &lcm
->p
[j
]);
285 mask_r(f
, nr
, lcm
, 0, val
, &EP
);
290 free_evalue_refs(&EP
);
293 static void mask(Matrix
*f
, evalue
*factor
, barvinok_options
*options
)
295 if (options
->lookup_table
)
296 mask_table(f
, factor
);
298 mask_fractional(f
, factor
);
301 /* This structure encodes the power of the term in a rational generating function.
303 * Either E == NULL or constant = 0
304 * If E != NULL, then the power is E
305 * If E == NULL, then the power is coeff * param[pos] + constant
314 /* Returns the power of (t+1) in the term of a rational generating function,
315 * i.e., the scalar product of the actual lattice point and lambda.
316 * The lattice point is the unique lattice point in the fundamental parallelepiped
317 * of the unimodual cone i shifted to the parametric vertex V.
319 * PD is the parameter domain, which, if != NULL, may be used to simply the
320 * resulting expression.
322 * The result is returned in term.
324 void lattice_point(Param_Vertices
* V
, const mat_ZZ
& rays
, vec_ZZ
& lambda
,
325 term_info
* term
, Polyhedron
*PD
, barvinok_options
*options
)
327 unsigned nparam
= V
->Vertex
->NbColumns
- 2;
328 unsigned dim
= rays
.NumCols();
330 vertex
.SetDims(V
->Vertex
->NbRows
, nparam
+1);
334 value_set_si(lcm
, 1);
335 for (int j
= 0; j
< V
->Vertex
->NbRows
; ++j
) {
336 value_lcm(lcm
, V
->Vertex
->p
[j
][nparam
+1], &lcm
);
338 if (value_notone_p(lcm
)) {
339 Matrix
* mv
= Matrix_Alloc(dim
, nparam
+1);
340 for (int j
= 0 ; j
< dim
; ++j
) {
341 value_division(tmp
, lcm
, V
->Vertex
->p
[j
][nparam
+1]);
342 Vector_Scale(V
->Vertex
->p
[j
], mv
->p
[j
], tmp
, nparam
+1);
345 term
->E
= lattice_point(rays
, lambda
, mv
, lcm
, PD
, options
);
353 for (int i
= 0; i
< V
->Vertex
->NbRows
; ++i
) {
354 assert(value_one_p(V
->Vertex
->p
[i
][nparam
+1])); // for now
355 values2zz(V
->Vertex
->p
[i
], vertex
[i
], nparam
+1);
359 num
= lambda
* vertex
;
363 for (int j
= 0; j
< nparam
; ++j
)
369 term
->E
= multi_monom(num
);
373 term
->constant
= num
[nparam
];
376 term
->coeff
= num
[p
];
384 struct counter
: public np_base
{
393 counter(unsigned dim
) : np_base(dim
) {
398 virtual void start(Polyhedron
*P
, barvinok_options
*options
);
404 virtual void handle(const mat_ZZ
& rays
, Value
*vertex
, QQ c
, int *closed
,
405 barvinok_options
*options
);
406 virtual void get_count(Value
*result
) {
407 assert(value_one_p(&count
[0]._mp_den
));
408 value_assign(*result
, &count
[0]._mp_num
);
412 struct OrthogonalException
{} Orthogonal
;
414 void counter::handle(const mat_ZZ
& rays
, Value
*V
, QQ c
, int *closed
,
415 barvinok_options
*options
)
417 for (int k
= 0; k
< dim
; ++k
) {
418 if (lambda
* rays
[k
] == 0)
423 assert(c
.n
== 1 || c
.n
== -1);
426 lattice_point(V
, rays
, vertex
, closed
);
427 num
= vertex
* lambda
;
429 normalize(sign
, num
, den
);
432 dpoly
n(dim
, den
[0], 1);
433 for (int k
= 1; k
< dim
; ++k
) {
434 dpoly
fact(dim
, den
[k
], 1);
437 d
.div(n
, count
, sign
);
440 void counter::start(Polyhedron
*P
, barvinok_options
*options
)
444 randomvector(P
, lambda
, dim
);
445 np_base::start(P
, options
);
447 } catch (OrthogonalException
&e
) {
448 mpq_set_si(count
, 0, 0);
453 struct bfe_term
: public bfc_term_base
{
454 vector
<evalue
*> factors
;
456 bfe_term(int len
) : bfc_term_base(len
) {
460 for (int i
= 0; i
< factors
.size(); ++i
) {
463 free_evalue_refs(factors
[i
]);
469 static void print_int_vector(int *v
, int len
, char *name
)
471 cerr
<< name
<< endl
;
472 for (int j
= 0; j
< len
; ++j
) {
478 static void print_bfc_terms(mat_ZZ
& factors
, bfc_vec
& v
)
481 cerr
<< "factors" << endl
;
482 cerr
<< factors
<< endl
;
483 for (int i
= 0; i
< v
.size(); ++i
) {
484 cerr
<< "term: " << i
<< endl
;
485 print_int_vector(v
[i
]->powers
, factors
.NumRows(), "powers");
486 cerr
<< "terms" << endl
;
487 cerr
<< v
[i
]->terms
<< endl
;
488 bfc_term
* bfct
= static_cast<bfc_term
*>(v
[i
]);
489 cerr
<< bfct
->c
<< endl
;
493 static void print_bfe_terms(mat_ZZ
& factors
, bfc_vec
& v
)
496 cerr
<< "factors" << endl
;
497 cerr
<< factors
<< endl
;
498 for (int i
= 0; i
< v
.size(); ++i
) {
499 cerr
<< "term: " << i
<< endl
;
500 print_int_vector(v
[i
]->powers
, factors
.NumRows(), "powers");
501 cerr
<< "terms" << endl
;
502 cerr
<< v
[i
]->terms
<< endl
;
503 bfe_term
* bfet
= static_cast<bfe_term
*>(v
[i
]);
504 for (int j
= 0; j
< v
[i
]->terms
.NumRows(); ++j
) {
505 char * test
[] = {"a", "b"};
506 print_evalue(stderr
, bfet
->factors
[j
], test
);
507 fprintf(stderr
, "\n");
512 struct bfcounter
: public bfcounter_base
{
515 bfcounter(unsigned dim
) : bfcounter_base(dim
) {
522 virtual void base(mat_ZZ
& factors
, bfc_vec
& v
);
523 virtual void get_count(Value
*result
) {
524 assert(value_one_p(&count
[0]._mp_den
));
525 value_assign(*result
, &count
[0]._mp_num
);
529 void bfcounter::base(mat_ZZ
& factors
, bfc_vec
& v
)
531 unsigned nf
= factors
.NumRows();
533 for (int i
= 0; i
< v
.size(); ++i
) {
534 bfc_term
* bfct
= static_cast<bfc_term
*>(v
[i
]);
536 // factor is always positive, so we always
538 for (int k
= 0; k
< nf
; ++k
)
539 total_power
+= v
[i
]->powers
[k
];
542 for (j
= 0; j
< nf
; ++j
)
543 if (v
[i
]->powers
[j
] > 0)
546 dpoly
D(total_power
, factors
[j
][0], 1);
547 for (int k
= 1; k
< v
[i
]->powers
[j
]; ++k
) {
548 dpoly
fact(total_power
, factors
[j
][0], 1);
552 for (int k
= 0; k
< v
[i
]->powers
[j
]; ++k
) {
553 dpoly
fact(total_power
, factors
[j
][0], 1);
557 for (int k
= 0; k
< v
[i
]->terms
.NumRows(); ++k
) {
558 dpoly
n(total_power
, v
[i
]->terms
[k
][0]);
559 mpq_set_si(tcount
, 0, 1);
560 n
.div(D
, tcount
, one
);
562 bfct
->c
[k
].n
= -bfct
->c
[k
].n
;
563 zz2value(bfct
->c
[k
].n
, tn
);
564 zz2value(bfct
->c
[k
].d
, td
);
566 mpz_mul(mpq_numref(tcount
), mpq_numref(tcount
), tn
);
567 mpz_mul(mpq_denref(tcount
), mpq_denref(tcount
), td
);
568 mpq_canonicalize(tcount
);
569 mpq_add(count
, count
, tcount
);
576 /* Check whether the polyhedron is unbounded and if so,
577 * check whether it has any (and therefore an infinite number of)
579 * If one of the vertices is integer, then we are done.
580 * Otherwise, transform the polyhedron such that one of the rays
581 * is the first unit vector and cut it off at a height that ensures
582 * that if the whole polyhedron has any points, then the remaining part
583 * has integer points. In particular we add the largest coefficient
584 * of a ray to the highest vertex (rounded up).
586 static bool Polyhedron_is_infinite(Polyhedron
*P
, Value
* result
,
587 barvinok_options
*options
)
599 for (; r
< P
->NbRays
; ++r
)
600 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1]))
602 if (P
->NbBid
== 0 && r
== P
->NbRays
)
605 if (options
->count_sample_infinite
) {
608 sample
= Polyhedron_Sample(P
, options
);
610 value_set_si(*result
, 0);
612 value_set_si(*result
, -1);
618 for (int i
= 0; i
< P
->NbRays
; ++i
)
619 if (value_one_p(P
->Ray
[i
][1+P
->Dimension
])) {
620 value_set_si(*result
, -1);
625 v
= Vector_Alloc(P
->Dimension
+1);
626 Vector_Gcd(P
->Ray
[r
]+1, P
->Dimension
, &g
);
627 Vector_AntiScale(P
->Ray
[r
]+1, v
->p
, g
, P
->Dimension
+1);
628 M
= unimodular_complete(v
);
629 value_set_si(M
->p
[P
->Dimension
][P
->Dimension
], 1);
632 P
= Polyhedron_Preimage(P
, M2
, 0);
641 value_set_si(size
, 0);
643 for (int i
= 0; i
< P
->NbBid
; ++i
) {
644 value_absolute(tmp
, P
->Ray
[i
][1]);
645 if (value_gt(tmp
, size
))
646 value_assign(size
, tmp
);
648 for (int i
= P
->NbBid
; i
< P
->NbRays
; ++i
) {
649 if (value_zero_p(P
->Ray
[i
][P
->Dimension
+1])) {
650 if (value_gt(P
->Ray
[i
][1], size
))
651 value_assign(size
, P
->Ray
[i
][1]);
654 mpz_cdiv_q(tmp
, P
->Ray
[i
][1], P
->Ray
[i
][P
->Dimension
+1]);
655 if (first
|| value_gt(tmp
, offset
)) {
656 value_assign(offset
, tmp
);
660 value_addto(offset
, offset
, size
);
664 v
= Vector_Alloc(P
->Dimension
+2);
665 value_set_si(v
->p
[0], 1);
666 value_set_si(v
->p
[1], -1);
667 value_assign(v
->p
[1+P
->Dimension
], offset
);
668 R
= AddConstraints(v
->p
, 1, P
, options
->MaxRays
);
676 barvinok_count_with_options(P
, &c
, options
);
679 value_set_si(*result
, 0);
681 value_set_si(*result
, -1);
687 typedef Polyhedron
* Polyhedron_p
;
689 static void barvinok_count_f(Polyhedron
*P
, Value
* result
,
690 barvinok_options
*options
);
692 void barvinok_count_with_options(Polyhedron
*P
, Value
* result
,
693 struct barvinok_options
*options
)
698 bool infinite
= false;
701 value_set_si(*result
, 0);
707 P
= remove_equalities(P
);
708 P
= DomainConstraintSimplify(P
, options
->MaxRays
);
712 } while (!emptyQ(P
) && P
->NbEq
!= 0);
715 value_set_si(*result
, 0);
720 if (Polyhedron_is_infinite(P
, result
, options
)) {
725 if (P
->Dimension
== 0) {
726 /* Test whether the constraints are satisfied */
727 POL_ENSURE_VERTICES(P
);
728 value_set_si(*result
, !emptyQ(P
));
733 Q
= Polyhedron_Factor(P
, 0, options
->MaxRays
);
741 barvinok_count_f(P
, result
, options
);
742 if (value_neg_p(*result
))
744 if (Q
&& P
->next
&& value_notzero_p(*result
)) {
748 for (Q
= P
->next
; Q
; Q
= Q
->next
) {
749 barvinok_count_f(Q
, &factor
, options
);
750 if (value_neg_p(factor
)) {
753 } else if (Q
->next
&& value_zero_p(factor
)) {
754 value_set_si(*result
, 0);
757 value_multiply(*result
, *result
, factor
);
766 value_set_si(*result
, -1);
769 void barvinok_count(Polyhedron
*P
, Value
* result
, unsigned NbMaxCons
)
771 barvinok_options
*options
= barvinok_options_new_with_defaults();
772 options
->MaxRays
= NbMaxCons
;
773 barvinok_count_with_options(P
, result
, options
);
777 static void barvinok_count_f(Polyhedron
*P
, Value
* result
,
778 barvinok_options
*options
)
781 value_set_si(*result
, 0);
785 if (P
->Dimension
== 1)
786 return Line_Length(P
, result
);
788 int c
= P
->NbConstraints
;
789 POL_ENSURE_FACETS(P
);
790 if (c
!= P
->NbConstraints
|| P
->NbEq
!= 0)
791 return barvinok_count_with_options(P
, result
, options
);
793 POL_ENSURE_VERTICES(P
);
795 if (Polyhedron_is_infinite(P
, result
, options
))
799 if (options
->incremental_specialization
== 2)
800 cnt
= new bfcounter(P
->Dimension
);
801 else if (options
->incremental_specialization
== 1)
802 cnt
= new icounter(P
->Dimension
);
804 cnt
= new counter(P
->Dimension
);
805 cnt
->start(P
, options
);
807 cnt
->get_count(result
);
811 static void uni_polynom(int param
, Vector
*c
, evalue
*EP
)
813 unsigned dim
= c
->Size
-2;
815 value_set_si(EP
->d
,0);
816 EP
->x
.p
= new_enode(polynomial
, dim
+1, param
+1);
817 for (int j
= 0; j
<= dim
; ++j
)
818 evalue_set(&EP
->x
.p
->arr
[j
], c
->p
[j
], c
->p
[dim
+1]);
821 static void multi_polynom(Vector
*c
, evalue
* X
, evalue
*EP
)
823 unsigned dim
= c
->Size
-2;
827 evalue_set(&EC
, c
->p
[dim
], c
->p
[dim
+1]);
830 evalue_set(EP
, c
->p
[dim
], c
->p
[dim
+1]);
832 for (int i
= dim
-1; i
>= 0; --i
) {
834 value_assign(EC
.x
.n
, c
->p
[i
]);
837 free_evalue_refs(&EC
);
840 Polyhedron
*unfringe (Polyhedron
*P
, unsigned MaxRays
)
842 int len
= P
->Dimension
+2;
843 Polyhedron
*T
, *R
= P
;
846 Vector
*row
= Vector_Alloc(len
);
847 value_set_si(row
->p
[0], 1);
849 R
= DomainConstraintSimplify(Polyhedron_Copy(P
), MaxRays
);
851 Matrix
*M
= Matrix_Alloc(2, len
-1);
852 value_set_si(M
->p
[1][len
-2], 1);
853 for (int v
= 0; v
< P
->Dimension
; ++v
) {
854 value_set_si(M
->p
[0][v
], 1);
855 Polyhedron
*I
= Polyhedron_Image(R
, M
, 2+1);
856 value_set_si(M
->p
[0][v
], 0);
857 for (int r
= 0; r
< I
->NbConstraints
; ++r
) {
858 if (value_zero_p(I
->Constraint
[r
][0]))
860 if (value_zero_p(I
->Constraint
[r
][1]))
862 if (value_one_p(I
->Constraint
[r
][1]))
864 if (value_mone_p(I
->Constraint
[r
][1]))
866 value_absolute(g
, I
->Constraint
[r
][1]);
867 Vector_Set(row
->p
+1, 0, len
-2);
868 value_division(row
->p
[1+v
], I
->Constraint
[r
][1], g
);
869 mpz_fdiv_q(row
->p
[len
-1], I
->Constraint
[r
][2], g
);
871 R
= AddConstraints(row
->p
, 1, R
, MaxRays
);
883 /* this procedure may have false negatives */
884 static bool Polyhedron_is_infinite_param(Polyhedron
*P
, unsigned nparam
)
887 for (r
= 0; r
< P
->NbRays
; ++r
) {
888 if (!value_zero_p(P
->Ray
[r
][0]) &&
889 !value_zero_p(P
->Ray
[r
][P
->Dimension
+1]))
891 if (First_Non_Zero(P
->Ray
[r
]+1+P
->Dimension
-nparam
, nparam
) == -1)
897 /* Check whether all rays point in the positive directions
900 static bool Polyhedron_has_positive_rays(Polyhedron
*P
, unsigned nparam
)
903 for (r
= 0; r
< P
->NbRays
; ++r
)
904 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1])) {
906 for (i
= P
->Dimension
- nparam
; i
< P
->Dimension
; ++i
)
907 if (value_neg_p(P
->Ray
[r
][i
+1]))
913 /* Check whether all rays are revlex positive in the parameters
915 static bool Polyhedron_has_revlex_positive_rays(Polyhedron
*P
, unsigned nparam
)
918 for (r
= 0; r
< P
->NbRays
; ++r
) {
919 if (value_notzero_p(P
->Ray
[r
][P
->Dimension
+1]))
922 for (i
= P
->Dimension
-1; i
>= P
->Dimension
-nparam
; --i
) {
923 if (value_neg_p(P
->Ray
[r
][i
+1]))
925 if (value_pos_p(P
->Ray
[r
][i
+1]))
928 /* A ray independent of the parameters */
929 if (i
< P
->Dimension
-nparam
)
935 typedef evalue
* evalue_p
;
937 struct enumerator_base
{
941 vertex_decomposer
*vpd
;
943 enumerator_base(unsigned dim
, vertex_decomposer
*vpd
)
948 vE
= new evalue_p
[vpd
->nbV
];
949 for (int j
= 0; j
< vpd
->nbV
; ++j
)
953 evalue_set_si(&mone
, -1, 1);
956 void decompose_at(Param_Vertices
*V
, int _i
, barvinok_options
*options
) {
960 value_init(vE
[_i
]->d
);
961 evalue_set_si(vE
[_i
], 0, 1);
963 vpd
->decompose_at_vertex(V
, _i
, options
);
966 virtual ~enumerator_base() {
967 for (int j
= 0; j
< vpd
->nbV
; ++j
)
969 free_evalue_refs(vE
[j
]);
974 free_evalue_refs(&mone
);
977 static enumerator_base
*create(Polyhedron
*P
, unsigned dim
, unsigned nbV
,
978 barvinok_options
*options
);
981 struct enumerator
: public signed_cone_consumer
, public vertex_decomposer
,
982 public enumerator_base
{
990 enumerator(Polyhedron
*P
, unsigned dim
, unsigned nbV
) :
991 vertex_decomposer(P
, nbV
, *this), enumerator_base(dim
, this) {
994 randomvector(P
, lambda
, dim
);
996 c
= Vector_Alloc(dim
+2);
1006 virtual void handle(const signed_cone
& sc
, barvinok_options
*options
);
1009 void enumerator::handle(const signed_cone
& sc
, barvinok_options
*options
)
1013 assert(sc
.rays
.NumRows() == dim
);
1014 for (int k
= 0; k
< dim
; ++k
) {
1015 if (lambda
* sc
.rays
[k
] == 0)
1021 lattice_point(V
, sc
.rays
, lambda
, &num
, 0, options
);
1022 den
= sc
.rays
* lambda
;
1023 normalize(sign
, num
.constant
, den
);
1025 dpoly
n(dim
, den
[0], 1);
1026 for (int k
= 1; k
< dim
; ++k
) {
1027 dpoly
fact(dim
, den
[k
], 1);
1030 if (num
.E
!= NULL
) {
1031 ZZ
one(INIT_VAL
, 1);
1032 dpoly_n
d(dim
, num
.constant
, one
);
1035 multi_polynom(c
, num
.E
, &EV
);
1036 eadd(&EV
, vE
[vert
]);
1037 free_evalue_refs(&EV
);
1038 free_evalue_refs(num
.E
);
1040 } else if (num
.pos
!= -1) {
1041 dpoly_n
d(dim
, num
.constant
, num
.coeff
);
1044 uni_polynom(num
.pos
, c
, &EV
);
1045 eadd(&EV
, vE
[vert
]);
1046 free_evalue_refs(&EV
);
1048 mpq_set_si(count
, 0, 1);
1049 dpoly
d(dim
, num
.constant
);
1050 d
.div(n
, count
, sign
);
1053 evalue_set(&EV
, &count
[0]._mp_num
, &count
[0]._mp_den
);
1054 eadd(&EV
, vE
[vert
]);
1055 free_evalue_refs(&EV
);
1059 struct ienumerator_base
: enumerator_base
{
1062 ienumerator_base(unsigned dim
, vertex_decomposer
*vpd
) :
1063 enumerator_base(dim
,vpd
) {
1064 E_vertex
= new evalue_p
[dim
];
1067 virtual ~ienumerator_base() {
1071 evalue
*E_num(int i
, int d
) {
1072 return E_vertex
[i
+ (dim
-d
)];
1081 cumulator(evalue
*factor
, evalue
*v
, dpoly_r
*r
) :
1082 factor(factor
), v(v
), r(r
) {}
1084 void cumulate(barvinok_options
*options
);
1086 virtual void add_term(const vector
<int>& powers
, evalue
*f2
) = 0;
1089 void cumulator::cumulate(barvinok_options
*options
)
1091 evalue cum
; // factor * 1 * E_num[0]/1 * (E_num[0]-1)/2 *...
1093 evalue t
; // E_num[0] - (m-1)
1097 if (options
->lookup_table
) {
1099 evalue_set_si(&mone
, -1, 1);
1103 evalue_copy(&cum
, factor
);
1106 value_set_si(f
.d
, 1);
1107 value_set_si(f
.x
.n
, 1);
1111 if (!options
->lookup_table
) {
1112 for (cst
= &t
; value_zero_p(cst
->d
); ) {
1113 if (cst
->x
.p
->type
== fractional
)
1114 cst
= &cst
->x
.p
->arr
[1];
1116 cst
= &cst
->x
.p
->arr
[0];
1120 for (int m
= 0; m
< r
->len
; ++m
) {
1123 value_set_si(f
.d
, m
);
1125 if (!options
->lookup_table
)
1126 value_subtract(cst
->x
.n
, cst
->x
.n
, cst
->d
);
1132 dpoly_r_term_list
& current
= r
->c
[r
->len
-1-m
];
1133 dpoly_r_term_list::iterator j
;
1134 for (j
= current
.begin(); j
!= current
.end(); ++j
) {
1135 if ((*j
)->coeff
== 0)
1137 evalue
*f2
= new evalue
;
1139 value_init(f2
->x
.n
);
1140 zz2value((*j
)->coeff
, f2
->x
.n
);
1141 zz2value(r
->denom
, f2
->d
);
1144 add_term((*j
)->powers
, f2
);
1147 free_evalue_refs(&f
);
1148 free_evalue_refs(&t
);
1149 free_evalue_refs(&cum
);
1150 if (options
->lookup_table
)
1151 free_evalue_refs(&mone
);
1154 struct E_poly_term
{
1159 struct ie_cum
: public cumulator
{
1160 vector
<E_poly_term
*> terms
;
1162 ie_cum(evalue
*factor
, evalue
*v
, dpoly_r
*r
) : cumulator(factor
, v
, r
) {}
1164 virtual void add_term(const vector
<int>& powers
, evalue
*f2
);
1167 void ie_cum::add_term(const vector
<int>& powers
, evalue
*f2
)
1170 for (k
= 0; k
< terms
.size(); ++k
) {
1171 if (terms
[k
]->powers
== powers
) {
1172 eadd(f2
, terms
[k
]->E
);
1173 free_evalue_refs(f2
);
1178 if (k
>= terms
.size()) {
1179 E_poly_term
*ET
= new E_poly_term
;
1180 ET
->powers
= powers
;
1182 terms
.push_back(ET
);
1186 struct ienumerator
: public signed_cone_consumer
, public vertex_decomposer
,
1187 public ienumerator_base
{
1193 ienumerator(Polyhedron
*P
, unsigned dim
, unsigned nbV
) :
1194 vertex_decomposer(P
, nbV
, *this), ienumerator_base(dim
, this) {
1195 vertex
.SetLength(dim
);
1197 den
.SetDims(dim
, dim
);
1205 virtual void handle(const signed_cone
& sc
, barvinok_options
*options
);
1206 void reduce(evalue
*factor
, vec_ZZ
& num
, mat_ZZ
& den_f
,
1207 barvinok_options
*options
);
1210 void ienumerator::reduce(evalue
*factor
, vec_ZZ
& num
, mat_ZZ
& den_f
,
1211 barvinok_options
*options
)
1213 unsigned len
= den_f
.NumRows(); // number of factors in den
1214 unsigned dim
= num
.length();
1217 eadd(factor
, vE
[vert
]);
1222 den_s
.SetLength(len
);
1224 den_r
.SetDims(len
, dim
-1);
1228 for (r
= 0; r
< len
; ++r
) {
1229 den_s
[r
] = den_f
[r
][0];
1230 for (k
= 0; k
<= dim
-1; ++k
)
1232 den_r
[r
][k
-(k
>0)] = den_f
[r
][k
];
1237 num_p
.SetLength(dim
-1);
1238 for (k
= 0 ; k
<= dim
-1; ++k
)
1240 num_p
[k
-(k
>0)] = num
[k
];
1243 den_p
.SetLength(len
);
1247 normalize(one
, num_s
, num_p
, den_s
, den_p
, den_r
);
1249 emul(&mone
, factor
);
1253 for (int k
= 0; k
< len
; ++k
) {
1256 else if (den_s
[k
] == 0)
1259 if (no_param
== 0) {
1260 reduce(factor
, num_p
, den_r
, options
);
1264 pden
.SetDims(only_param
, dim
-1);
1266 for (k
= 0, l
= 0; k
< len
; ++k
)
1268 pden
[l
++] = den_r
[k
];
1270 for (k
= 0; k
< len
; ++k
)
1274 dpoly
n(no_param
, num_s
);
1275 dpoly
D(no_param
, den_s
[k
], 1);
1276 for ( ; ++k
< len
; )
1277 if (den_p
[k
] == 0) {
1278 dpoly
fact(no_param
, den_s
[k
], 1);
1283 // if no_param + only_param == len then all powers
1284 // below will be all zero
1285 if (no_param
+ only_param
== len
) {
1286 if (E_num(0, dim
) != 0)
1287 r
= new dpoly_r(n
, len
);
1289 mpq_set_si(tcount
, 0, 1);
1291 n
.div(D
, tcount
, one
);
1293 if (value_notzero_p(mpq_numref(tcount
))) {
1297 value_assign(f
.x
.n
, mpq_numref(tcount
));
1298 value_assign(f
.d
, mpq_denref(tcount
));
1300 reduce(factor
, num_p
, pden
, options
);
1301 free_evalue_refs(&f
);
1306 for (k
= 0; k
< len
; ++k
) {
1307 if (den_s
[k
] == 0 || den_p
[k
] == 0)
1310 dpoly
pd(no_param
-1, den_s
[k
], 1);
1313 for (l
= 0; l
< k
; ++l
)
1314 if (den_r
[l
] == den_r
[k
])
1318 r
= new dpoly_r(n
, pd
, l
, len
);
1320 dpoly_r
*nr
= new dpoly_r(r
, pd
, l
, len
);
1326 dpoly_r
*rc
= r
->div(D
);
1329 if (E_num(0, dim
) == 0) {
1330 int common
= pden
.NumRows();
1331 dpoly_r_term_list
& final
= r
->c
[r
->len
-1];
1337 zz2value(r
->denom
, f
.d
);
1338 dpoly_r_term_list::iterator j
;
1339 for (j
= final
.begin(); j
!= final
.end(); ++j
) {
1340 if ((*j
)->coeff
== 0)
1343 for (int k
= 0; k
< r
->dim
; ++k
) {
1344 int n
= (*j
)->powers
[k
];
1347 pden
.SetDims(rows
+n
, pden
.NumCols());
1348 for (int l
= 0; l
< n
; ++l
)
1349 pden
[rows
+l
] = den_r
[k
];
1353 evalue_copy(&t
, factor
);
1354 zz2value((*j
)->coeff
, f
.x
.n
);
1356 reduce(&t
, num_p
, pden
, options
);
1357 free_evalue_refs(&t
);
1359 free_evalue_refs(&f
);
1361 ie_cum
cum(factor
, E_num(0, dim
), r
);
1362 cum
.cumulate(options
);
1364 int common
= pden
.NumRows();
1366 for (int j
= 0; j
< cum
.terms
.size(); ++j
) {
1368 pden
.SetDims(rows
, pden
.NumCols());
1369 for (int k
= 0; k
< r
->dim
; ++k
) {
1370 int n
= cum
.terms
[j
]->powers
[k
];
1373 pden
.SetDims(rows
+n
, pden
.NumCols());
1374 for (int l
= 0; l
< n
; ++l
)
1375 pden
[rows
+l
] = den_r
[k
];
1378 reduce(cum
.terms
[j
]->E
, num_p
, pden
, options
);
1379 free_evalue_refs(cum
.terms
[j
]->E
);
1380 delete cum
.terms
[j
]->E
;
1381 delete cum
.terms
[j
];
1388 static int type_offset(enode
*p
)
1390 return p
->type
== fractional
? 1 :
1391 p
->type
== flooring
? 1 : 0;
1394 static int edegree(evalue
*e
)
1399 if (value_notzero_p(e
->d
))
1403 int i
= type_offset(p
);
1404 if (p
->size
-i
-1 > d
)
1405 d
= p
->size
- i
- 1;
1406 for (; i
< p
->size
; i
++) {
1407 int d2
= edegree(&p
->arr
[i
]);
1414 void ienumerator::handle(const signed_cone
& sc
, barvinok_options
*options
)
1417 assert(sc
.rays
.NumRows() == dim
);
1419 lattice_point(V
, sc
.rays
, vertex
, E_vertex
, options
);
1425 evalue_set_si(&one
, sc
.sign
, 1);
1426 reduce(&one
, vertex
, den
, options
);
1427 free_evalue_refs(&one
);
1429 for (int i
= 0; i
< dim
; ++i
)
1431 free_evalue_refs(E_vertex
[i
]);
1436 struct bfenumerator
: public vertex_decomposer
, public bf_base
,
1437 public ienumerator_base
{
1440 bfenumerator(Polyhedron
*P
, unsigned dim
, unsigned nbV
) :
1441 vertex_decomposer(P
, nbV
, *this),
1442 bf_base(dim
), ienumerator_base(dim
, this) {
1450 virtual void handle(const signed_cone
& sc
, barvinok_options
*options
);
1451 virtual void base(mat_ZZ
& factors
, bfc_vec
& v
);
1453 bfc_term_base
* new_bf_term(int len
) {
1454 bfe_term
* t
= new bfe_term(len
);
1458 virtual void set_factor(bfc_term_base
*t
, int k
, int change
) {
1459 bfe_term
* bfet
= static_cast<bfe_term
*>(t
);
1460 factor
= bfet
->factors
[k
];
1461 assert(factor
!= NULL
);
1462 bfet
->factors
[k
] = NULL
;
1464 emul(&mone
, factor
);
1467 virtual void set_factor(bfc_term_base
*t
, int k
, mpq_t
&q
, int change
) {
1468 bfe_term
* bfet
= static_cast<bfe_term
*>(t
);
1469 factor
= bfet
->factors
[k
];
1470 assert(factor
!= NULL
);
1471 bfet
->factors
[k
] = NULL
;
1477 value_oppose(f
.x
.n
, mpq_numref(q
));
1479 value_assign(f
.x
.n
, mpq_numref(q
));
1480 value_assign(f
.d
, mpq_denref(q
));
1482 free_evalue_refs(&f
);
1485 virtual void set_factor(bfc_term_base
*t
, int k
, const QQ
& c
, int change
) {
1486 bfe_term
* bfet
= static_cast<bfe_term
*>(t
);
1488 factor
= new evalue
;
1493 zz2value(c
.n
, f
.x
.n
);
1495 value_oppose(f
.x
.n
, f
.x
.n
);
1498 value_init(factor
->d
);
1499 evalue_copy(factor
, bfet
->factors
[k
]);
1501 free_evalue_refs(&f
);
1504 void set_factor(evalue
*f
, int change
) {
1510 virtual void insert_term(bfc_term_base
*t
, int i
) {
1511 bfe_term
* bfet
= static_cast<bfe_term
*>(t
);
1512 int len
= t
->terms
.NumRows()-1; // already increased by one
1514 bfet
->factors
.resize(len
+1);
1515 for (int j
= len
; j
> i
; --j
) {
1516 bfet
->factors
[j
] = bfet
->factors
[j
-1];
1517 t
->terms
[j
] = t
->terms
[j
-1];
1519 bfet
->factors
[i
] = factor
;
1523 virtual void update_term(bfc_term_base
*t
, int i
) {
1524 bfe_term
* bfet
= static_cast<bfe_term
*>(t
);
1526 eadd(factor
, bfet
->factors
[i
]);
1527 free_evalue_refs(factor
);
1531 virtual bool constant_vertex(int dim
) { return E_num(0, dim
) == 0; }
1533 virtual void cum(bf_reducer
*bfr
, bfc_term_base
*t
, int k
, dpoly_r
*r
,
1534 barvinok_options
*options
);
1537 enumerator_base
*enumerator_base::create(Polyhedron
*P
, unsigned dim
, unsigned nbV
,
1538 barvinok_options
*options
)
1540 enumerator_base
*eb
;
1542 if (options
->incremental_specialization
== BV_SPECIALIZATION_BF
)
1543 eb
= new bfenumerator(P
, dim
, nbV
);
1544 else if (options
->incremental_specialization
== BV_SPECIALIZATION_DF
)
1545 eb
= new ienumerator(P
, dim
, nbV
);
1547 eb
= new enumerator(P
, dim
, nbV
);
1552 struct bfe_cum
: public cumulator
{
1554 bfc_term_base
*told
;
1558 bfe_cum(evalue
*factor
, evalue
*v
, dpoly_r
*r
, bf_reducer
*bfr
,
1559 bfc_term_base
*t
, int k
, bfenumerator
*e
) :
1560 cumulator(factor
, v
, r
), told(t
), k(k
),
1564 virtual void add_term(const vector
<int>& powers
, evalue
*f2
);
1567 void bfe_cum::add_term(const vector
<int>& powers
, evalue
*f2
)
1569 bfr
->update_powers(powers
);
1571 bfc_term_base
* t
= bfe
->find_bfc_term(bfr
->vn
, bfr
->npowers
, bfr
->nnf
);
1572 bfe
->set_factor(f2
, bfr
->l_changes
% 2);
1573 bfe
->add_term(t
, told
->terms
[k
], bfr
->l_extra_num
);
1576 void bfenumerator::cum(bf_reducer
*bfr
, bfc_term_base
*t
, int k
,
1577 dpoly_r
*r
, barvinok_options
*options
)
1579 bfe_term
* bfet
= static_cast<bfe_term
*>(t
);
1580 bfe_cum
cum(bfet
->factors
[k
], E_num(0, bfr
->d
), r
, bfr
, t
, k
, this);
1581 cum
.cumulate(options
);
1584 void bfenumerator::base(mat_ZZ
& factors
, bfc_vec
& v
)
1586 for (int i
= 0; i
< v
.size(); ++i
) {
1587 assert(v
[i
]->terms
.NumRows() == 1);
1588 evalue
*factor
= static_cast<bfe_term
*>(v
[i
])->factors
[0];
1589 eadd(factor
, vE
[vert
]);
1594 void bfenumerator::handle(const signed_cone
& sc
, barvinok_options
*options
)
1597 assert(sc
.rays
.NumRows() == enumerator_base::dim
);
1599 bfe_term
* t
= new bfe_term(enumerator_base::dim
);
1600 vector
< bfc_term_base
* > v
;
1603 t
->factors
.resize(1);
1605 t
->terms
.SetDims(1, enumerator_base::dim
);
1606 lattice_point(V
, sc
.rays
, t
->terms
[0], E_vertex
, options
);
1608 // the elements of factors are always lexpositive
1610 int s
= setup_factors(sc
.rays
, factors
, t
, sc
.sign
);
1612 t
->factors
[0] = new evalue
;
1613 value_init(t
->factors
[0]->d
);
1614 evalue_set_si(t
->factors
[0], s
, 1);
1615 reduce(factors
, v
, options
);
1617 for (int i
= 0; i
< enumerator_base::dim
; ++i
)
1619 free_evalue_refs(E_vertex
[i
]);
1624 #ifdef HAVE_CORRECT_VERTICES
1625 static inline Param_Polyhedron
*Polyhedron2Param_SD(Polyhedron
**Din
,
1626 Polyhedron
*Cin
,int WS
,Polyhedron
**CEq
,Matrix
**CT
)
1628 if (WS
& POL_NO_DUAL
)
1630 return Polyhedron2Param_SimplifiedDomain(Din
, Cin
, WS
, CEq
, CT
);
1633 static Param_Polyhedron
*Polyhedron2Param_SD(Polyhedron
**Din
,
1634 Polyhedron
*Cin
,int WS
,Polyhedron
**CEq
,Matrix
**CT
)
1636 static char data
[] = " 1 0 0 0 0 1 -18 "
1637 " 1 0 0 -20 0 19 1 "
1638 " 1 0 1 20 0 -20 16 "
1641 " 1 4 -20 0 0 -1 23 "
1642 " 1 -4 20 0 0 1 -22 "
1643 " 1 0 1 0 20 -20 16 "
1644 " 1 0 0 0 -20 19 1 ";
1645 static int checked
= 0;
1650 Matrix
*M
= Matrix_Alloc(9, 7);
1651 for (i
= 0; i
< 9; ++i
)
1652 for (int j
= 0; j
< 7; ++j
) {
1653 sscanf(p
, "%d%n", &v
, &n
);
1655 value_set_si(M
->p
[i
][j
], v
);
1657 Polyhedron
*P
= Constraints2Polyhedron(M
, 1024);
1659 Polyhedron
*U
= Universe_Polyhedron(1);
1660 Param_Polyhedron
*PP
= Polyhedron2Param_Domain(P
, U
, 1024);
1664 for (i
= 0, V
= PP
->V
; V
; ++i
, V
= V
->next
)
1667 Param_Polyhedron_Free(PP
);
1669 fprintf(stderr
, "WARNING: results may be incorrect\n");
1671 "WARNING: use latest version of PolyLib to remove this warning\n");
1675 return Polyhedron2Param_SimplifiedDomain(Din
, Cin
, WS
, CEq
, CT
);
1679 static evalue
* barvinok_enumerate_ev_f(Polyhedron
*P
, Polyhedron
* C
,
1680 barvinok_options
*options
);
1683 static evalue
* barvinok_enumerate_cst(Polyhedron
*P
, Polyhedron
* C
,
1688 ALLOC(evalue
, eres
);
1689 value_init(eres
->d
);
1690 value_set_si(eres
->d
, 0);
1691 eres
->x
.p
= new_enode(partition
, 2, C
->Dimension
);
1692 EVALUE_SET_DOMAIN(eres
->x
.p
->arr
[0], DomainConstraintSimplify(C
, MaxRays
));
1693 value_set_si(eres
->x
.p
->arr
[1].d
, 1);
1694 value_init(eres
->x
.p
->arr
[1].x
.n
);
1696 value_set_si(eres
->x
.p
->arr
[1].x
.n
, 0);
1698 barvinok_count(P
, &eres
->x
.p
->arr
[1].x
.n
, MaxRays
);
1703 evalue
* barvinok_enumerate_with_options(Polyhedron
*P
, Polyhedron
* C
,
1704 struct barvinok_options
*options
)
1706 //P = unfringe(P, MaxRays);
1707 Polyhedron
*Corig
= C
;
1708 Polyhedron
*CEq
= NULL
, *rVD
, *CA
;
1710 unsigned nparam
= C
->Dimension
;
1714 value_init(factor
.d
);
1715 evalue_set_si(&factor
, 1, 1);
1717 CA
= align_context(C
, P
->Dimension
, options
->MaxRays
);
1718 P
= DomainIntersection(P
, CA
, options
->MaxRays
);
1719 Polyhedron_Free(CA
);
1722 POL_ENSURE_FACETS(P
);
1723 POL_ENSURE_VERTICES(P
);
1724 POL_ENSURE_FACETS(C
);
1725 POL_ENSURE_VERTICES(C
);
1727 if (C
->Dimension
== 0 || emptyQ(P
)) {
1729 eres
= barvinok_enumerate_cst(P
, CEq
? CEq
: Polyhedron_Copy(C
),
1732 emul(&factor
, eres
);
1733 reduce_evalue(eres
);
1734 free_evalue_refs(&factor
);
1741 if (Polyhedron_is_infinite_param(P
, nparam
))
1746 P
= remove_equalities_p(P
, P
->Dimension
-nparam
, &f
);
1747 mask(f
, &factor
, options
);
1750 if (P
->Dimension
== nparam
) {
1752 P
= Universe_Polyhedron(0);
1756 Polyhedron
*T
= Polyhedron_Factor(P
, nparam
, options
->MaxRays
);
1757 if (T
|| (P
->Dimension
== nparam
+1)) {
1760 for (Q
= T
? T
: P
; Q
; Q
= Q
->next
) {
1761 Polyhedron
*next
= Q
->next
;
1765 if (Q
->Dimension
!= C
->Dimension
)
1766 QC
= Polyhedron_Project(Q
, nparam
);
1769 C
= DomainIntersection(C
, QC
, options
->MaxRays
);
1771 Polyhedron_Free(C2
);
1773 Polyhedron_Free(QC
);
1781 if (T
->Dimension
== C
->Dimension
) {
1788 Polyhedron
*next
= P
->next
;
1790 eres
= barvinok_enumerate_ev_f(P
, C
, options
);
1797 for (Q
= P
->next
; Q
; Q
= Q
->next
) {
1798 Polyhedron
*next
= Q
->next
;
1801 f
= barvinok_enumerate_ev_f(Q
, C
, options
);
1803 free_evalue_refs(f
);
1813 evalue
* barvinok_enumerate_ev(Polyhedron
*P
, Polyhedron
* C
, unsigned MaxRays
)
1816 barvinok_options
*options
= barvinok_options_new_with_defaults();
1817 options
->MaxRays
= MaxRays
;
1818 E
= barvinok_enumerate_with_options(P
, C
, options
);
1823 static evalue
* barvinok_enumerate_ev_f(Polyhedron
*P
, Polyhedron
* C
,
1824 barvinok_options
*options
)
1826 unsigned nparam
= C
->Dimension
;
1828 if (P
->Dimension
- nparam
== 1)
1829 return ParamLine_Length(P
, C
, options
);
1831 Param_Polyhedron
*PP
= NULL
;
1832 Polyhedron
*CEq
= NULL
, *pVD
;
1834 Param_Domain
*D
, *next
;
1837 Polyhedron
*Porig
= P
;
1839 PP
= Polyhedron2Param_SD(&P
,C
,options
->MaxRays
,&CEq
,&CT
);
1841 if (isIdentity(CT
)) {
1845 assert(CT
->NbRows
!= CT
->NbColumns
);
1846 if (CT
->NbRows
== 1) { // no more parameters
1847 eres
= barvinok_enumerate_cst(P
, CEq
, options
->MaxRays
);
1852 Param_Polyhedron_Free(PP
);
1858 nparam
= CT
->NbRows
- 1;
1861 unsigned dim
= P
->Dimension
- nparam
;
1863 ALLOC(evalue
, eres
);
1864 value_init(eres
->d
);
1865 value_set_si(eres
->d
, 0);
1868 for (nd
= 0, D
=PP
->D
; D
; ++nd
, D
=D
->next
);
1869 struct section
{ Polyhedron
*D
; evalue E
; };
1870 section
*s
= new section
[nd
];
1871 Polyhedron
**fVD
= new Polyhedron_p
[nd
];
1873 enumerator_base
*et
= NULL
;
1878 et
= enumerator_base::create(P
, dim
, PP
->nbV
, options
);
1880 for(nd
= 0, D
=PP
->D
; D
; D
=next
) {
1883 Polyhedron
*rVD
= reduce_domain(D
->Domain
, CT
, CEq
,
1884 fVD
, nd
, options
->MaxRays
);
1888 pVD
= CT
? DomainImage(rVD
,CT
,options
->MaxRays
) : rVD
;
1890 value_init(s
[nd
].E
.d
);
1891 evalue_set_si(&s
[nd
].E
, 0, 1);
1894 FORALL_PVertex_in_ParamPolyhedron(V
,D
,PP
) // _i is internal counter
1897 et
->decompose_at(V
, _i
, options
);
1898 } catch (OrthogonalException
&e
) {
1901 for (; nd
>= 0; --nd
) {
1902 free_evalue_refs(&s
[nd
].E
);
1903 Domain_Free(s
[nd
].D
);
1904 Domain_Free(fVD
[nd
]);
1908 eadd(et
->vE
[_i
] , &s
[nd
].E
);
1909 END_FORALL_PVertex_in_ParamPolyhedron
;
1910 evalue_range_reduction_in_domain(&s
[nd
].E
, pVD
);
1913 addeliminatedparams_evalue(&s
[nd
].E
, CT
);
1921 evalue_set_si(eres
, 0, 1);
1923 eres
->x
.p
= new_enode(partition
, 2*nd
, C
->Dimension
);
1924 for (int j
= 0; j
< nd
; ++j
) {
1925 EVALUE_SET_DOMAIN(eres
->x
.p
->arr
[2*j
], s
[j
].D
);
1926 value_clear(eres
->x
.p
->arr
[2*j
+1].d
);
1927 eres
->x
.p
->arr
[2*j
+1] = s
[j
].E
;
1928 Domain_Free(fVD
[j
]);
1935 Polyhedron_Free(CEq
);
1939 Enumeration
* barvinok_enumerate(Polyhedron
*P
, Polyhedron
* C
, unsigned MaxRays
)
1941 evalue
*EP
= barvinok_enumerate_ev(P
, C
, MaxRays
);
1943 return partition2enumeration(EP
);
1946 static void SwapColumns(Value
**V
, int n
, int i
, int j
)
1948 for (int r
= 0; r
< n
; ++r
)
1949 value_swap(V
[r
][i
], V
[r
][j
]);
1952 static void SwapColumns(Polyhedron
*P
, int i
, int j
)
1954 SwapColumns(P
->Constraint
, P
->NbConstraints
, i
, j
);
1955 SwapColumns(P
->Ray
, P
->NbRays
, i
, j
);
1958 /* Construct a constraint c from constraints l and u such that if
1959 * if constraint c holds then for each value of the other variables
1960 * there is at most one value of variable pos (position pos+1 in the constraints).
1962 * Given a lower and an upper bound
1963 * n_l v_i + <c_l,x> + c_l >= 0
1964 * -n_u v_i + <c_u,x> + c_u >= 0
1965 * the constructed constraint is
1967 * -(n_l<c_u,x> + n_u<c_l,x>) + (-n_l c_u - n_u c_l + n_l n_u - 1)
1969 * which is then simplified to remove the content of the non-constant coefficients
1971 * len is the total length of the constraints.
1972 * v is a temporary variable that can be used by this procedure
1974 static void negative_test_constraint(Value
*l
, Value
*u
, Value
*c
, int pos
,
1977 value_oppose(*v
, u
[pos
+1]);
1978 Vector_Combine(l
+1, u
+1, c
+1, *v
, l
[pos
+1], len
-1);
1979 value_multiply(*v
, *v
, l
[pos
+1]);
1980 value_subtract(c
[len
-1], c
[len
-1], *v
);
1981 value_set_si(*v
, -1);
1982 Vector_Scale(c
+1, c
+1, *v
, len
-1);
1983 value_decrement(c
[len
-1], c
[len
-1]);
1984 ConstraintSimplify(c
, c
, len
, v
);
1987 static bool parallel_constraints(Value
*l
, Value
*u
, Value
*c
, int pos
,
1996 Vector_Gcd(&l
[1+pos
], len
, &g1
);
1997 Vector_Gcd(&u
[1+pos
], len
, &g2
);
1998 Vector_Combine(l
+1+pos
, u
+1+pos
, c
+1, g2
, g1
, len
);
1999 parallel
= First_Non_Zero(c
+1, len
) == -1;
2007 static void negative_test_constraint7(Value
*l
, Value
*u
, Value
*c
, int pos
,
2008 int exist
, int len
, Value
*v
)
2013 Vector_Gcd(&u
[1+pos
], exist
, v
);
2014 Vector_Gcd(&l
[1+pos
], exist
, &g
);
2015 Vector_Combine(l
+1, u
+1, c
+1, *v
, g
, len
-1);
2016 value_multiply(*v
, *v
, g
);
2017 value_subtract(c
[len
-1], c
[len
-1], *v
);
2018 value_set_si(*v
, -1);
2019 Vector_Scale(c
+1, c
+1, *v
, len
-1);
2020 value_decrement(c
[len
-1], c
[len
-1]);
2021 ConstraintSimplify(c
, c
, len
, v
);
2026 /* Turns a x + b >= 0 into a x + b <= -1
2028 * len is the total length of the constraint.
2029 * v is a temporary variable that can be used by this procedure
2031 static void oppose_constraint(Value
*c
, int len
, Value
*v
)
2033 value_set_si(*v
, -1);
2034 Vector_Scale(c
+1, c
+1, *v
, len
-1);
2035 value_decrement(c
[len
-1], c
[len
-1]);
2038 /* Split polyhedron P into two polyhedra *pos and *neg, where
2039 * existential variable i has at most one solution for each
2040 * value of the other variables in *neg.
2042 * The splitting is performed using constraints l and u.
2044 * nvar: number of set variables
2045 * row: temporary vector that can be used by this procedure
2046 * f: temporary value that can be used by this procedure
2048 static bool SplitOnConstraint(Polyhedron
*P
, int i
, int l
, int u
,
2049 int nvar
, int MaxRays
, Vector
*row
, Value
& f
,
2050 Polyhedron
**pos
, Polyhedron
**neg
)
2052 negative_test_constraint(P
->Constraint
[l
], P
->Constraint
[u
],
2053 row
->p
, nvar
+i
, P
->Dimension
+2, &f
);
2054 *neg
= AddConstraints(row
->p
, 1, P
, MaxRays
);
2056 /* We found an independent, but useless constraint
2057 * Maybe we should detect this earlier and not
2058 * mark the variable as INDEPENDENT
2060 if (emptyQ((*neg
))) {
2061 Polyhedron_Free(*neg
);
2065 oppose_constraint(row
->p
, P
->Dimension
+2, &f
);
2066 *pos
= AddConstraints(row
->p
, 1, P
, MaxRays
);
2068 if (emptyQ((*pos
))) {
2069 Polyhedron_Free(*neg
);
2070 Polyhedron_Free(*pos
);
2078 * unimodularly transform P such that constraint r is transformed
2079 * into a constraint that involves only a single (the first)
2080 * existential variable
2083 static Polyhedron
*rotate_along(Polyhedron
*P
, int r
, int nvar
, int exist
,
2089 Vector
*row
= Vector_Alloc(exist
);
2090 Vector_Copy(P
->Constraint
[r
]+1+nvar
, row
->p
, exist
);
2091 Vector_Gcd(row
->p
, exist
, &g
);
2092 if (value_notone_p(g
))
2093 Vector_AntiScale(row
->p
, row
->p
, g
, exist
);
2096 Matrix
*M
= unimodular_complete(row
);
2097 Matrix
*M2
= Matrix_Alloc(P
->Dimension
+1, P
->Dimension
+1);
2098 for (r
= 0; r
< nvar
; ++r
)
2099 value_set_si(M2
->p
[r
][r
], 1);
2100 for ( ; r
< nvar
+exist
; ++r
)
2101 Vector_Copy(M
->p
[r
-nvar
], M2
->p
[r
]+nvar
, exist
);
2102 for ( ; r
< P
->Dimension
+1; ++r
)
2103 value_set_si(M2
->p
[r
][r
], 1);
2104 Polyhedron
*T
= Polyhedron_Image(P
, M2
, MaxRays
);
2113 /* Split polyhedron P into two polyhedra *pos and *neg, where
2114 * existential variable i has at most one solution for each
2115 * value of the other variables in *neg.
2117 * If independent is set, then the two constraints on which the
2118 * split will be performed need to be independent of the other
2119 * existential variables.
2121 * Return true if an appropriate split could be performed.
2123 * nvar: number of set variables
2124 * exist: number of existential variables
2125 * row: temporary vector that can be used by this procedure
2126 * f: temporary value that can be used by this procedure
2128 static bool SplitOnVar(Polyhedron
*P
, int i
,
2129 int nvar
, int exist
, int MaxRays
,
2130 Vector
*row
, Value
& f
, bool independent
,
2131 Polyhedron
**pos
, Polyhedron
**neg
)
2135 for (int l
= P
->NbEq
; l
< P
->NbConstraints
; ++l
) {
2136 if (value_negz_p(P
->Constraint
[l
][nvar
+i
+1]))
2140 for (j
= 0; j
< exist
; ++j
)
2141 if (j
!= i
&& value_notzero_p(P
->Constraint
[l
][nvar
+j
+1]))
2147 for (int u
= P
->NbEq
; u
< P
->NbConstraints
; ++u
) {
2148 if (value_posz_p(P
->Constraint
[u
][nvar
+i
+1]))
2152 for (j
= 0; j
< exist
; ++j
)
2153 if (j
!= i
&& value_notzero_p(P
->Constraint
[u
][nvar
+j
+1]))
2159 if (SplitOnConstraint(P
, i
, l
, u
, nvar
, MaxRays
, row
, f
, pos
, neg
)) {
2162 SwapColumns(*neg
, nvar
+1, nvar
+1+i
);
2172 static bool double_bound_pair(Polyhedron
*P
, int nvar
, int exist
,
2173 int i
, int l1
, int l2
,
2174 Polyhedron
**pos
, Polyhedron
**neg
)
2178 Vector
*row
= Vector_Alloc(P
->Dimension
+2);
2179 value_set_si(row
->p
[0], 1);
2180 value_oppose(f
, P
->Constraint
[l1
][nvar
+i
+1]);
2181 Vector_Combine(P
->Constraint
[l1
]+1, P
->Constraint
[l2
]+1,
2183 P
->Constraint
[l2
][nvar
+i
+1], f
,
2185 ConstraintSimplify(row
->p
, row
->p
, P
->Dimension
+2, &f
);
2186 *pos
= AddConstraints(row
->p
, 1, P
, 0);
2187 value_set_si(f
, -1);
2188 Vector_Scale(row
->p
+1, row
->p
+1, f
, P
->Dimension
+1);
2189 value_decrement(row
->p
[P
->Dimension
+1], row
->p
[P
->Dimension
+1]);
2190 *neg
= AddConstraints(row
->p
, 1, P
, 0);
2194 return !emptyQ((*pos
)) && !emptyQ((*neg
));
2197 static bool double_bound(Polyhedron
*P
, int nvar
, int exist
,
2198 Polyhedron
**pos
, Polyhedron
**neg
)
2200 for (int i
= 0; i
< exist
; ++i
) {
2202 for (l1
= P
->NbEq
; l1
< P
->NbConstraints
; ++l1
) {
2203 if (value_negz_p(P
->Constraint
[l1
][nvar
+i
+1]))
2205 for (l2
= l1
+ 1; l2
< P
->NbConstraints
; ++l2
) {
2206 if (value_negz_p(P
->Constraint
[l2
][nvar
+i
+1]))
2208 if (double_bound_pair(P
, nvar
, exist
, i
, l1
, l2
, pos
, neg
))
2212 for (l1
= P
->NbEq
; l1
< P
->NbConstraints
; ++l1
) {
2213 if (value_posz_p(P
->Constraint
[l1
][nvar
+i
+1]))
2215 if (l1
< P
->NbConstraints
)
2216 for (l2
= l1
+ 1; l2
< P
->NbConstraints
; ++l2
) {
2217 if (value_posz_p(P
->Constraint
[l2
][nvar
+i
+1]))
2219 if (double_bound_pair(P
, nvar
, exist
, i
, l1
, l2
, pos
, neg
))
2231 INDEPENDENT
= 1 << 2,
2235 static evalue
* enumerate_or(Polyhedron
*D
,
2236 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2239 fprintf(stderr
, "\nER: Or\n");
2240 #endif /* DEBUG_ER */
2242 Polyhedron
*N
= D
->next
;
2245 barvinok_enumerate_e_with_options(D
, exist
, nparam
, options
);
2248 for (D
= N
; D
; D
= N
) {
2253 barvinok_enumerate_e_with_options(D
, exist
, nparam
, options
);
2256 free_evalue_refs(EN
);
2266 static evalue
* enumerate_sum(Polyhedron
*P
,
2267 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2269 int nvar
= P
->Dimension
- exist
- nparam
;
2270 int toswap
= nvar
< exist
? nvar
: exist
;
2271 for (int i
= 0; i
< toswap
; ++i
)
2272 SwapColumns(P
, 1 + i
, nvar
+exist
- i
);
2276 fprintf(stderr
, "\nER: Sum\n");
2277 #endif /* DEBUG_ER */
2279 evalue
*EP
= barvinok_enumerate_e_with_options(P
, exist
, nparam
, options
);
2281 for (int i
= 0; i
< /* nvar */ nparam
; ++i
) {
2282 Matrix
*C
= Matrix_Alloc(1, 1 + nparam
+ 1);
2283 value_set_si(C
->p
[0][0], 1);
2285 value_init(split
.d
);
2286 value_set_si(split
.d
, 0);
2287 split
.x
.p
= new_enode(partition
, 4, nparam
);
2288 value_set_si(C
->p
[0][1+i
], 1);
2289 Matrix
*C2
= Matrix_Copy(C
);
2290 EVALUE_SET_DOMAIN(split
.x
.p
->arr
[0],
2291 Constraints2Polyhedron(C2
, options
->MaxRays
));
2293 evalue_set_si(&split
.x
.p
->arr
[1], 1, 1);
2294 value_set_si(C
->p
[0][1+i
], -1);
2295 value_set_si(C
->p
[0][1+nparam
], -1);
2296 EVALUE_SET_DOMAIN(split
.x
.p
->arr
[2],
2297 Constraints2Polyhedron(C
, options
->MaxRays
));
2298 evalue_set_si(&split
.x
.p
->arr
[3], 1, 1);
2300 free_evalue_refs(&split
);
2304 evalue_range_reduction(EP
);
2306 evalue_frac2floor2(EP
, 1);
2308 evalue
*sum
= esum(EP
, nvar
);
2310 free_evalue_refs(EP
);
2314 evalue_range_reduction(EP
);
2319 static evalue
* split_sure(Polyhedron
*P
, Polyhedron
*S
,
2320 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2322 int nvar
= P
->Dimension
- exist
- nparam
;
2324 Matrix
*M
= Matrix_Alloc(exist
, S
->Dimension
+2);
2325 for (int i
= 0; i
< exist
; ++i
)
2326 value_set_si(M
->p
[i
][nvar
+i
+1], 1);
2328 S
= DomainAddRays(S
, M
, options
->MaxRays
);
2330 Polyhedron
*F
= DomainAddRays(P
, M
, options
->MaxRays
);
2331 Polyhedron
*D
= DomainDifference(F
, S
, options
->MaxRays
);
2333 D
= Disjoint_Domain(D
, 0, options
->MaxRays
);
2338 M
= Matrix_Alloc(P
->Dimension
+1-exist
, P
->Dimension
+1);
2339 for (int j
= 0; j
< nvar
; ++j
)
2340 value_set_si(M
->p
[j
][j
], 1);
2341 for (int j
= 0; j
< nparam
+1; ++j
)
2342 value_set_si(M
->p
[nvar
+j
][nvar
+exist
+j
], 1);
2343 Polyhedron
*T
= Polyhedron_Image(S
, M
, options
->MaxRays
);
2344 evalue
*EP
= barvinok_enumerate_e_with_options(T
, 0, nparam
, options
);
2349 for (Polyhedron
*Q
= D
; Q
; Q
= Q
->next
) {
2350 Polyhedron
*N
= Q
->next
;
2352 T
= DomainIntersection(P
, Q
, options
->MaxRays
);
2353 evalue
*E
= barvinok_enumerate_e_with_options(T
, exist
, nparam
, options
);
2355 free_evalue_refs(E
);
2364 static evalue
* enumerate_sure(Polyhedron
*P
,
2365 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2369 int nvar
= P
->Dimension
- exist
- nparam
;
2375 for (i
= 0; i
< exist
; ++i
) {
2376 Matrix
*M
= Matrix_Alloc(S
->NbConstraints
, S
->Dimension
+2);
2378 value_set_si(lcm
, 1);
2379 for (int j
= 0; j
< S
->NbConstraints
; ++j
) {
2380 if (value_negz_p(S
->Constraint
[j
][1+nvar
+i
]))
2382 if (value_one_p(S
->Constraint
[j
][1+nvar
+i
]))
2384 value_lcm(lcm
, S
->Constraint
[j
][1+nvar
+i
], &lcm
);
2387 for (int j
= 0; j
< S
->NbConstraints
; ++j
) {
2388 if (value_negz_p(S
->Constraint
[j
][1+nvar
+i
]))
2390 if (value_one_p(S
->Constraint
[j
][1+nvar
+i
]))
2392 value_division(f
, lcm
, S
->Constraint
[j
][1+nvar
+i
]);
2393 Vector_Scale(S
->Constraint
[j
], M
->p
[c
], f
, S
->Dimension
+2);
2394 value_subtract(M
->p
[c
][S
->Dimension
+1],
2395 M
->p
[c
][S
->Dimension
+1],
2397 value_increment(M
->p
[c
][S
->Dimension
+1],
2398 M
->p
[c
][S
->Dimension
+1]);
2402 S
= AddConstraints(M
->p
[0], c
, S
, options
->MaxRays
);
2417 fprintf(stderr
, "\nER: Sure\n");
2418 #endif /* DEBUG_ER */
2420 return split_sure(P
, S
, exist
, nparam
, options
);
2423 static evalue
* enumerate_sure2(Polyhedron
*P
,
2424 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2426 int nvar
= P
->Dimension
- exist
- nparam
;
2428 for (r
= 0; r
< P
->NbRays
; ++r
)
2429 if (value_one_p(P
->Ray
[r
][0]) &&
2430 value_one_p(P
->Ray
[r
][P
->Dimension
+1]))
2436 Matrix
*M
= Matrix_Alloc(nvar
+ 1 + nparam
, P
->Dimension
+2);
2437 for (int i
= 0; i
< nvar
; ++i
)
2438 value_set_si(M
->p
[i
][1+i
], 1);
2439 for (int i
= 0; i
< nparam
; ++i
)
2440 value_set_si(M
->p
[i
+nvar
][1+nvar
+exist
+i
], 1);
2441 Vector_Copy(P
->Ray
[r
]+1+nvar
, M
->p
[nvar
+nparam
]+1+nvar
, exist
);
2442 value_set_si(M
->p
[nvar
+nparam
][0], 1);
2443 value_set_si(M
->p
[nvar
+nparam
][P
->Dimension
+1], 1);
2444 Polyhedron
* F
= Rays2Polyhedron(M
, options
->MaxRays
);
2447 Polyhedron
*I
= DomainIntersection(F
, P
, options
->MaxRays
);
2451 fprintf(stderr
, "\nER: Sure2\n");
2452 #endif /* DEBUG_ER */
2454 return split_sure(P
, I
, exist
, nparam
, options
);
2457 static evalue
* enumerate_cyclic(Polyhedron
*P
,
2458 unsigned exist
, unsigned nparam
,
2459 evalue
* EP
, int r
, int p
, unsigned MaxRays
)
2461 int nvar
= P
->Dimension
- exist
- nparam
;
2463 /* If EP in its fractional maps only contains references
2464 * to the remainder parameter with appropriate coefficients
2465 * then we could in principle avoid adding existentially
2466 * quantified variables to the validity domains.
2467 * We'd have to replace the remainder by m { p/m }
2468 * and multiply with an appropriate factor that is one
2469 * only in the appropriate range.
2470 * This last multiplication can be avoided if EP
2471 * has a single validity domain with no (further)
2472 * constraints on the remainder parameter
2475 Matrix
*CT
= Matrix_Alloc(nparam
+1, nparam
+3);
2476 Matrix
*M
= Matrix_Alloc(1, 1+nparam
+3);
2477 for (int j
= 0; j
< nparam
; ++j
)
2479 value_set_si(CT
->p
[j
][j
], 1);
2480 value_set_si(CT
->p
[p
][nparam
+1], 1);
2481 value_set_si(CT
->p
[nparam
][nparam
+2], 1);
2482 value_set_si(M
->p
[0][1+p
], -1);
2483 value_absolute(M
->p
[0][1+nparam
], P
->Ray
[0][1+nvar
+exist
+p
]);
2484 value_set_si(M
->p
[0][1+nparam
+1], 1);
2485 Polyhedron
*CEq
= Constraints2Polyhedron(M
, 1);
2487 addeliminatedparams_enum(EP
, CT
, CEq
, MaxRays
, nparam
);
2488 Polyhedron_Free(CEq
);
2494 static void enumerate_vd_add_ray(evalue
*EP
, Matrix
*Rays
, unsigned MaxRays
)
2496 if (value_notzero_p(EP
->d
))
2499 assert(EP
->x
.p
->type
== partition
);
2500 assert(EP
->x
.p
->pos
== EVALUE_DOMAIN(EP
->x
.p
->arr
[0])->Dimension
);
2501 for (int i
= 0; i
< EP
->x
.p
->size
/2; ++i
) {
2502 Polyhedron
*D
= EVALUE_DOMAIN(EP
->x
.p
->arr
[2*i
]);
2503 Polyhedron
*N
= DomainAddRays(D
, Rays
, MaxRays
);
2504 EVALUE_SET_DOMAIN(EP
->x
.p
->arr
[2*i
], N
);
2509 static evalue
* enumerate_line(Polyhedron
*P
,
2510 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2516 fprintf(stderr
, "\nER: Line\n");
2517 #endif /* DEBUG_ER */
2519 int nvar
= P
->Dimension
- exist
- nparam
;
2521 for (i
= 0; i
< nparam
; ++i
)
2522 if (value_notzero_p(P
->Ray
[0][1+nvar
+exist
+i
]))
2525 for (j
= i
+1; j
< nparam
; ++j
)
2526 if (value_notzero_p(P
->Ray
[0][1+nvar
+exist
+i
]))
2528 assert(j
>= nparam
); // for now
2530 Matrix
*M
= Matrix_Alloc(2, P
->Dimension
+2);
2531 value_set_si(M
->p
[0][0], 1);
2532 value_set_si(M
->p
[0][1+nvar
+exist
+i
], 1);
2533 value_set_si(M
->p
[1][0], 1);
2534 value_set_si(M
->p
[1][1+nvar
+exist
+i
], -1);
2535 value_absolute(M
->p
[1][1+P
->Dimension
], P
->Ray
[0][1+nvar
+exist
+i
]);
2536 value_decrement(M
->p
[1][1+P
->Dimension
], M
->p
[1][1+P
->Dimension
]);
2537 Polyhedron
*S
= AddConstraints(M
->p
[0], 2, P
, options
->MaxRays
);
2538 evalue
*EP
= barvinok_enumerate_e_with_options(S
, exist
, nparam
, options
);
2542 return enumerate_cyclic(P
, exist
, nparam
, EP
, 0, i
, options
->MaxRays
);
2545 static int single_param_pos(Polyhedron
*P
, unsigned exist
, unsigned nparam
,
2548 int nvar
= P
->Dimension
- exist
- nparam
;
2549 if (First_Non_Zero(P
->Ray
[r
]+1, nvar
) != -1)
2551 int i
= First_Non_Zero(P
->Ray
[r
]+1+nvar
+exist
, nparam
);
2554 if (First_Non_Zero(P
->Ray
[r
]+1+nvar
+exist
+1, nparam
-i
-1) != -1)
2559 static evalue
* enumerate_remove_ray(Polyhedron
*P
, int r
,
2560 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2563 fprintf(stderr
, "\nER: RedundantRay\n");
2564 #endif /* DEBUG_ER */
2568 value_set_si(one
, 1);
2569 int len
= P
->NbRays
-1;
2570 Matrix
*M
= Matrix_Alloc(2 * len
, P
->Dimension
+2);
2571 Vector_Copy(P
->Ray
[0], M
->p
[0], r
* (P
->Dimension
+2));
2572 Vector_Copy(P
->Ray
[r
+1], M
->p
[r
], (len
-r
) * (P
->Dimension
+2));
2573 for (int j
= 0; j
< P
->NbRays
; ++j
) {
2576 Vector_Combine(P
->Ray
[j
], P
->Ray
[r
], M
->p
[len
+j
-(j
>r
)],
2577 one
, P
->Ray
[j
][P
->Dimension
+1], P
->Dimension
+2);
2580 P
= Rays2Polyhedron(M
, options
->MaxRays
);
2582 evalue
*EP
= barvinok_enumerate_e_with_options(P
, exist
, nparam
, options
);
2589 static evalue
* enumerate_redundant_ray(Polyhedron
*P
,
2590 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2592 assert(P
->NbBid
== 0);
2593 int nvar
= P
->Dimension
- exist
- nparam
;
2597 for (int r
= 0; r
< P
->NbRays
; ++r
) {
2598 if (value_notzero_p(P
->Ray
[r
][P
->Dimension
+1]))
2600 int i1
= single_param_pos(P
, exist
, nparam
, r
);
2603 for (int r2
= r
+1; r2
< P
->NbRays
; ++r2
) {
2604 if (value_notzero_p(P
->Ray
[r2
][P
->Dimension
+1]))
2606 int i2
= single_param_pos(P
, exist
, nparam
, r2
);
2612 value_division(m
, P
->Ray
[r
][1+nvar
+exist
+i1
],
2613 P
->Ray
[r2
][1+nvar
+exist
+i1
]);
2614 value_multiply(m
, m
, P
->Ray
[r2
][1+nvar
+exist
+i1
]);
2615 /* r2 divides r => r redundant */
2616 if (value_eq(m
, P
->Ray
[r
][1+nvar
+exist
+i1
])) {
2618 return enumerate_remove_ray(P
, r
, exist
, nparam
, options
);
2621 value_division(m
, P
->Ray
[r2
][1+nvar
+exist
+i1
],
2622 P
->Ray
[r
][1+nvar
+exist
+i1
]);
2623 value_multiply(m
, m
, P
->Ray
[r
][1+nvar
+exist
+i1
]);
2624 /* r divides r2 => r2 redundant */
2625 if (value_eq(m
, P
->Ray
[r2
][1+nvar
+exist
+i1
])) {
2627 return enumerate_remove_ray(P
, r2
, exist
, nparam
, options
);
2635 static Polyhedron
*upper_bound(Polyhedron
*P
,
2636 int pos
, Value
*max
, Polyhedron
**R
)
2645 for (Polyhedron
*Q
= P
; Q
; Q
= N
) {
2647 for (r
= 0; r
< P
->NbRays
; ++r
) {
2648 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1]) &&
2649 value_pos_p(P
->Ray
[r
][1+pos
]))
2652 if (r
< P
->NbRays
) {
2660 for (r
= 0; r
< P
->NbRays
; ++r
) {
2661 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1]))
2663 mpz_fdiv_q(v
, P
->Ray
[r
][1+pos
], P
->Ray
[r
][1+P
->Dimension
]);
2664 if ((!Q
->next
&& r
== 0) || value_gt(v
, *max
))
2665 value_assign(*max
, v
);
2672 static evalue
* enumerate_ray(Polyhedron
*P
,
2673 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2675 assert(P
->NbBid
== 0);
2676 int nvar
= P
->Dimension
- exist
- nparam
;
2679 for (r
= 0; r
< P
->NbRays
; ++r
)
2680 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1]))
2686 for (r2
= r
+1; r2
< P
->NbRays
; ++r2
)
2687 if (value_zero_p(P
->Ray
[r2
][P
->Dimension
+1]))
2689 if (r2
< P
->NbRays
) {
2691 return enumerate_sum(P
, exist
, nparam
, options
);
2695 fprintf(stderr
, "\nER: Ray\n");
2696 #endif /* DEBUG_ER */
2702 value_set_si(one
, 1);
2703 int i
= single_param_pos(P
, exist
, nparam
, r
);
2704 assert(i
!= -1); // for now;
2706 Matrix
*M
= Matrix_Alloc(P
->NbRays
, P
->Dimension
+2);
2707 for (int j
= 0; j
< P
->NbRays
; ++j
) {
2708 Vector_Combine(P
->Ray
[j
], P
->Ray
[r
], M
->p
[j
],
2709 one
, P
->Ray
[j
][P
->Dimension
+1], P
->Dimension
+2);
2711 Polyhedron
*S
= Rays2Polyhedron(M
, options
->MaxRays
);
2713 Polyhedron
*D
= DomainDifference(P
, S
, options
->MaxRays
);
2715 // Polyhedron_Print(stderr, P_VALUE_FMT, D);
2716 assert(value_pos_p(P
->Ray
[r
][1+nvar
+exist
+i
])); // for now
2718 D
= upper_bound(D
, nvar
+exist
+i
, &m
, &R
);
2722 M
= Matrix_Alloc(2, P
->Dimension
+2);
2723 value_set_si(M
->p
[0][0], 1);
2724 value_set_si(M
->p
[1][0], 1);
2725 value_set_si(M
->p
[0][1+nvar
+exist
+i
], -1);
2726 value_set_si(M
->p
[1][1+nvar
+exist
+i
], 1);
2727 value_assign(M
->p
[0][1+P
->Dimension
], m
);
2728 value_oppose(M
->p
[1][1+P
->Dimension
], m
);
2729 value_addto(M
->p
[1][1+P
->Dimension
], M
->p
[1][1+P
->Dimension
],
2730 P
->Ray
[r
][1+nvar
+exist
+i
]);
2731 value_decrement(M
->p
[1][1+P
->Dimension
], M
->p
[1][1+P
->Dimension
]);
2732 // Matrix_Print(stderr, P_VALUE_FMT, M);
2733 D
= AddConstraints(M
->p
[0], 2, P
, options
->MaxRays
);
2734 // Polyhedron_Print(stderr, P_VALUE_FMT, D);
2735 value_subtract(M
->p
[0][1+P
->Dimension
], M
->p
[0][1+P
->Dimension
],
2736 P
->Ray
[r
][1+nvar
+exist
+i
]);
2737 // Matrix_Print(stderr, P_VALUE_FMT, M);
2738 S
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
2739 // Polyhedron_Print(stderr, P_VALUE_FMT, S);
2742 evalue
*EP
= barvinok_enumerate_e_with_options(D
, exist
, nparam
, options
);
2747 if (value_notone_p(P
->Ray
[r
][1+nvar
+exist
+i
]))
2748 EP
= enumerate_cyclic(P
, exist
, nparam
, EP
, r
, i
, options
->MaxRays
);
2750 M
= Matrix_Alloc(1, nparam
+2);
2751 value_set_si(M
->p
[0][0], 1);
2752 value_set_si(M
->p
[0][1+i
], 1);
2753 enumerate_vd_add_ray(EP
, M
, options
->MaxRays
);
2758 evalue
*E
= barvinok_enumerate_e_with_options(S
, exist
, nparam
, options
);
2760 free_evalue_refs(E
);
2767 evalue
*ER
= enumerate_or(R
, exist
, nparam
, options
);
2769 free_evalue_refs(ER
);
2776 static evalue
* enumerate_vd(Polyhedron
**PA
,
2777 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2779 Polyhedron
*P
= *PA
;
2780 int nvar
= P
->Dimension
- exist
- nparam
;
2781 Param_Polyhedron
*PP
= NULL
;
2782 Polyhedron
*C
= Universe_Polyhedron(nparam
);
2786 PP
= Polyhedron2Param_SimplifiedDomain(&PR
,C
, options
->MaxRays
,&CEq
,&CT
);
2790 Param_Domain
*D
, *last
;
2793 for (nd
= 0, D
=PP
->D
; D
; D
=D
->next
, ++nd
)
2796 Polyhedron
**VD
= new Polyhedron_p
[nd
];
2797 Polyhedron
**fVD
= new Polyhedron_p
[nd
];
2798 for(nd
= 0, D
=PP
->D
; D
; D
=D
->next
) {
2799 Polyhedron
*rVD
= reduce_domain(D
->Domain
, CT
, CEq
,
2800 fVD
, nd
, options
->MaxRays
);
2813 /* This doesn't seem to have any effect */
2815 Polyhedron
*CA
= align_context(VD
[0], P
->Dimension
, options
->MaxRays
);
2817 P
= DomainIntersection(P
, CA
, options
->MaxRays
);
2820 Polyhedron_Free(CA
);
2825 if (!EP
&& CT
->NbColumns
!= CT
->NbRows
) {
2826 Polyhedron
*CEqr
= DomainImage(CEq
, CT
, options
->MaxRays
);
2827 Polyhedron
*CA
= align_context(CEqr
, PR
->Dimension
, options
->MaxRays
);
2828 Polyhedron
*I
= DomainIntersection(PR
, CA
, options
->MaxRays
);
2829 Polyhedron_Free(CEqr
);
2830 Polyhedron_Free(CA
);
2832 fprintf(stderr
, "\nER: Eliminate\n");
2833 #endif /* DEBUG_ER */
2834 nparam
-= CT
->NbColumns
- CT
->NbRows
;
2835 EP
= barvinok_enumerate_e_with_options(I
, exist
, nparam
, options
);
2836 nparam
+= CT
->NbColumns
- CT
->NbRows
;
2837 addeliminatedparams_enum(EP
, CT
, CEq
, options
->MaxRays
, nparam
);
2841 Polyhedron_Free(PR
);
2844 if (!EP
&& nd
> 1) {
2846 fprintf(stderr
, "\nER: VD\n");
2847 #endif /* DEBUG_ER */
2848 for (int i
= 0; i
< nd
; ++i
) {
2849 Polyhedron
*CA
= align_context(VD
[i
], P
->Dimension
, options
->MaxRays
);
2850 Polyhedron
*I
= DomainIntersection(P
, CA
, options
->MaxRays
);
2853 EP
= barvinok_enumerate_e_with_options(I
, exist
, nparam
, options
);
2855 evalue
*E
= barvinok_enumerate_e_with_options(I
, exist
, nparam
,
2858 free_evalue_refs(E
);
2862 Polyhedron_Free(CA
);
2866 for (int i
= 0; i
< nd
; ++i
) {
2867 Polyhedron_Free(VD
[i
]);
2868 Polyhedron_Free(fVD
[i
]);
2874 if (!EP
&& nvar
== 0) {
2877 Param_Vertices
*V
, *V2
;
2878 Matrix
* M
= Matrix_Alloc(1, P
->Dimension
+2);
2880 FORALL_PVertex_in_ParamPolyhedron(V
, last
, PP
) {
2882 FORALL_PVertex_in_ParamPolyhedron(V2
, last
, PP
) {
2889 for (int i
= 0; i
< exist
; ++i
) {
2890 value_oppose(f
, V
->Vertex
->p
[i
][nparam
+1]);
2891 Vector_Combine(V
->Vertex
->p
[i
],
2893 M
->p
[0] + 1 + nvar
+ exist
,
2894 V2
->Vertex
->p
[i
][nparam
+1],
2898 for (j
= 0; j
< nparam
; ++j
)
2899 if (value_notzero_p(M
->p
[0][1+nvar
+exist
+j
]))
2903 ConstraintSimplify(M
->p
[0], M
->p
[0],
2904 P
->Dimension
+2, &f
);
2905 value_set_si(M
->p
[0][0], 0);
2906 Polyhedron
*para
= AddConstraints(M
->p
[0], 1, P
,
2909 Polyhedron_Free(para
);
2912 Polyhedron
*pos
, *neg
;
2913 value_set_si(M
->p
[0][0], 1);
2914 value_decrement(M
->p
[0][P
->Dimension
+1],
2915 M
->p
[0][P
->Dimension
+1]);
2916 neg
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
2917 value_set_si(f
, -1);
2918 Vector_Scale(M
->p
[0]+1, M
->p
[0]+1, f
,
2920 value_decrement(M
->p
[0][P
->Dimension
+1],
2921 M
->p
[0][P
->Dimension
+1]);
2922 value_decrement(M
->p
[0][P
->Dimension
+1],
2923 M
->p
[0][P
->Dimension
+1]);
2924 pos
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
2925 if (emptyQ(neg
) && emptyQ(pos
)) {
2926 Polyhedron_Free(para
);
2927 Polyhedron_Free(pos
);
2928 Polyhedron_Free(neg
);
2932 fprintf(stderr
, "\nER: Order\n");
2933 #endif /* DEBUG_ER */
2934 EP
= barvinok_enumerate_e_with_options(para
, exist
, nparam
,
2938 E
= barvinok_enumerate_e_with_options(pos
, exist
, nparam
,
2941 free_evalue_refs(E
);
2945 E
= barvinok_enumerate_e_with_options(neg
, exist
, nparam
,
2948 free_evalue_refs(E
);
2951 Polyhedron_Free(para
);
2952 Polyhedron_Free(pos
);
2953 Polyhedron_Free(neg
);
2958 } END_FORALL_PVertex_in_ParamPolyhedron
;
2961 } END_FORALL_PVertex_in_ParamPolyhedron
;
2964 /* Search for vertex coordinate to split on */
2965 /* First look for one independent of the parameters */
2966 FORALL_PVertex_in_ParamPolyhedron(V
, last
, PP
) {
2967 for (int i
= 0; i
< exist
; ++i
) {
2969 for (j
= 0; j
< nparam
; ++j
)
2970 if (value_notzero_p(V
->Vertex
->p
[i
][j
]))
2974 value_set_si(M
->p
[0][0], 1);
2975 Vector_Set(M
->p
[0]+1, 0, nvar
+exist
);
2976 Vector_Copy(V
->Vertex
->p
[i
],
2977 M
->p
[0] + 1 + nvar
+ exist
, nparam
+1);
2978 value_oppose(M
->p
[0][1+nvar
+i
],
2979 V
->Vertex
->p
[i
][nparam
+1]);
2981 Polyhedron
*pos
, *neg
;
2982 value_set_si(M
->p
[0][0], 1);
2983 value_decrement(M
->p
[0][P
->Dimension
+1],
2984 M
->p
[0][P
->Dimension
+1]);
2985 neg
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
2986 value_set_si(f
, -1);
2987 Vector_Scale(M
->p
[0]+1, M
->p
[0]+1, f
,
2989 value_decrement(M
->p
[0][P
->Dimension
+1],
2990 M
->p
[0][P
->Dimension
+1]);
2991 value_decrement(M
->p
[0][P
->Dimension
+1],
2992 M
->p
[0][P
->Dimension
+1]);
2993 pos
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
2994 if (emptyQ(neg
) || emptyQ(pos
)) {
2995 Polyhedron_Free(pos
);
2996 Polyhedron_Free(neg
);
2999 Polyhedron_Free(pos
);
3000 value_increment(M
->p
[0][P
->Dimension
+1],
3001 M
->p
[0][P
->Dimension
+1]);
3002 pos
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
3004 fprintf(stderr
, "\nER: Vertex\n");
3005 #endif /* DEBUG_ER */
3007 EP
= enumerate_or(pos
, exist
, nparam
, options
);
3012 } END_FORALL_PVertex_in_ParamPolyhedron
;
3016 /* Search for vertex coordinate to split on */
3017 /* Now look for one that depends on the parameters */
3018 FORALL_PVertex_in_ParamPolyhedron(V
, last
, PP
) {
3019 for (int i
= 0; i
< exist
; ++i
) {
3020 value_set_si(M
->p
[0][0], 1);
3021 Vector_Set(M
->p
[0]+1, 0, nvar
+exist
);
3022 Vector_Copy(V
->Vertex
->p
[i
],
3023 M
->p
[0] + 1 + nvar
+ exist
, nparam
+1);
3024 value_oppose(M
->p
[0][1+nvar
+i
],
3025 V
->Vertex
->p
[i
][nparam
+1]);
3027 Polyhedron
*pos
, *neg
;
3028 value_set_si(M
->p
[0][0], 1);
3029 value_decrement(M
->p
[0][P
->Dimension
+1],
3030 M
->p
[0][P
->Dimension
+1]);
3031 neg
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
3032 value_set_si(f
, -1);
3033 Vector_Scale(M
->p
[0]+1, M
->p
[0]+1, f
,
3035 value_decrement(M
->p
[0][P
->Dimension
+1],
3036 M
->p
[0][P
->Dimension
+1]);
3037 value_decrement(M
->p
[0][P
->Dimension
+1],
3038 M
->p
[0][P
->Dimension
+1]);
3039 pos
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
3040 if (emptyQ(neg
) || emptyQ(pos
)) {
3041 Polyhedron_Free(pos
);
3042 Polyhedron_Free(neg
);
3045 Polyhedron_Free(pos
);
3046 value_increment(M
->p
[0][P
->Dimension
+1],
3047 M
->p
[0][P
->Dimension
+1]);
3048 pos
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
3050 fprintf(stderr
, "\nER: ParamVertex\n");
3051 #endif /* DEBUG_ER */
3053 EP
= enumerate_or(pos
, exist
, nparam
, options
);
3058 } END_FORALL_PVertex_in_ParamPolyhedron
;
3066 Polyhedron_Free(CEq
);
3070 Param_Polyhedron_Free(PP
);
3076 evalue
* barvinok_enumerate_pip(Polyhedron
*P
, unsigned exist
, unsigned nparam
,
3080 barvinok_options
*options
= barvinok_options_new_with_defaults();
3081 options
->MaxRays
= MaxRays
;
3082 E
= barvinok_enumerate_pip_with_options(P
, exist
, nparam
, options
);
3088 evalue
*barvinok_enumerate_pip_with_options(Polyhedron
*P
,
3089 unsigned exist
, unsigned nparam
, struct barvinok_options
*options
)
3094 evalue
*barvinok_enumerate_pip_with_options(Polyhedron
*P
,
3095 unsigned exist
, unsigned nparam
, struct barvinok_options
*options
)
3097 int nvar
= P
->Dimension
- exist
- nparam
;
3098 evalue
*EP
= evalue_zero();
3102 fprintf(stderr
, "\nER: PIP\n");
3103 #endif /* DEBUG_ER */
3105 Polyhedron
*D
= pip_projectout(P
, nvar
, exist
, nparam
);
3106 for (Q
= D
; Q
; Q
= N
) {
3110 exist
= Q
->Dimension
- nvar
- nparam
;
3111 E
= barvinok_enumerate_e_with_options(Q
, exist
, nparam
, options
);
3114 free_evalue_refs(E
);
3123 static bool is_single(Value
*row
, int pos
, int len
)
3125 return First_Non_Zero(row
, pos
) == -1 &&
3126 First_Non_Zero(row
+pos
+1, len
-pos
-1) == -1;
3129 static evalue
* barvinok_enumerate_e_r(Polyhedron
*P
,
3130 unsigned exist
, unsigned nparam
, barvinok_options
*options
);
3133 static int er_level
= 0;
3135 evalue
* barvinok_enumerate_e_with_options(Polyhedron
*P
,
3136 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
3138 fprintf(stderr
, "\nER: level %i\n", er_level
);
3140 Polyhedron_PrintConstraints(stderr
, P_VALUE_FMT
, P
);
3141 fprintf(stderr
, "\nE %d\nP %d\n", exist
, nparam
);
3143 P
= DomainConstraintSimplify(Polyhedron_Copy(P
), options
->MaxRays
);
3144 evalue
*EP
= barvinok_enumerate_e_r(P
, exist
, nparam
, options
);
3150 evalue
* barvinok_enumerate_e_with_options(Polyhedron
*P
,
3151 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
3153 P
= DomainConstraintSimplify(Polyhedron_Copy(P
), options
->MaxRays
);
3154 evalue
*EP
= barvinok_enumerate_e_r(P
, exist
, nparam
, options
);
3160 evalue
* barvinok_enumerate_e(Polyhedron
*P
, unsigned exist
, unsigned nparam
,
3164 barvinok_options
*options
= barvinok_options_new_with_defaults();
3165 options
->MaxRays
= MaxRays
;
3166 E
= barvinok_enumerate_e_with_options(P
, exist
, nparam
, options
);
3171 static evalue
* barvinok_enumerate_e_r(Polyhedron
*P
,
3172 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
3175 Polyhedron
*U
= Universe_Polyhedron(nparam
);
3176 evalue
*EP
= barvinok_enumerate_with_options(P
, U
, options
);
3177 //char *param_name[] = {"P", "Q", "R", "S", "T" };
3178 //print_evalue(stdout, EP, param_name);
3183 int nvar
= P
->Dimension
- exist
- nparam
;
3184 int len
= P
->Dimension
+ 2;
3187 POL_ENSURE_FACETS(P
);
3188 POL_ENSURE_VERTICES(P
);
3191 return evalue_zero();
3193 if (nvar
== 0 && nparam
== 0) {
3194 evalue
*EP
= evalue_zero();
3195 barvinok_count_with_options(P
, &EP
->x
.n
, options
);
3196 if (value_pos_p(EP
->x
.n
))
3197 value_set_si(EP
->x
.n
, 1);
3202 for (r
= 0; r
< P
->NbRays
; ++r
)
3203 if (value_zero_p(P
->Ray
[r
][0]) ||
3204 value_zero_p(P
->Ray
[r
][P
->Dimension
+1])) {
3206 for (i
= 0; i
< nvar
; ++i
)
3207 if (value_notzero_p(P
->Ray
[r
][i
+1]))
3211 for (i
= nvar
+ exist
; i
< nvar
+ exist
+ nparam
; ++i
)
3212 if (value_notzero_p(P
->Ray
[r
][i
+1]))
3214 if (i
>= nvar
+ exist
+ nparam
)
3217 if (r
< P
->NbRays
) {
3218 evalue
*EP
= evalue_zero();
3219 value_set_si(EP
->x
.n
, -1);
3224 for (r
= 0; r
< P
->NbEq
; ++r
)
3225 if ((first
= First_Non_Zero(P
->Constraint
[r
]+1+nvar
, exist
)) != -1)
3228 if (First_Non_Zero(P
->Constraint
[r
]+1+nvar
+first
+1,
3229 exist
-first
-1) != -1) {
3230 Polyhedron
*T
= rotate_along(P
, r
, nvar
, exist
, options
->MaxRays
);
3232 fprintf(stderr
, "\nER: Equality\n");
3233 #endif /* DEBUG_ER */
3234 evalue
*EP
= barvinok_enumerate_e_with_options(T
, exist
-1, nparam
,
3240 fprintf(stderr
, "\nER: Fixed\n");
3241 #endif /* DEBUG_ER */
3243 return barvinok_enumerate_e_with_options(P
, exist
-1, nparam
,
3246 Polyhedron
*T
= Polyhedron_Copy(P
);
3247 SwapColumns(T
, nvar
+1, nvar
+1+first
);
3248 evalue
*EP
= barvinok_enumerate_e_with_options(T
, exist
-1, nparam
,
3256 Vector
*row
= Vector_Alloc(len
);
3257 value_set_si(row
->p
[0], 1);
3262 enum constraint
* info
= new constraint
[exist
];
3263 for (int i
= 0; i
< exist
; ++i
) {
3265 for (int l
= P
->NbEq
; l
< P
->NbConstraints
; ++l
) {
3266 if (value_negz_p(P
->Constraint
[l
][nvar
+i
+1]))
3268 bool l_parallel
= is_single(P
->Constraint
[l
]+nvar
+1, i
, exist
);
3269 for (int u
= P
->NbEq
; u
< P
->NbConstraints
; ++u
) {
3270 if (value_posz_p(P
->Constraint
[u
][nvar
+i
+1]))
3272 bool lu_parallel
= l_parallel
||
3273 is_single(P
->Constraint
[u
]+nvar
+1, i
, exist
);
3274 value_oppose(f
, P
->Constraint
[u
][nvar
+i
+1]);
3275 Vector_Combine(P
->Constraint
[l
]+1, P
->Constraint
[u
]+1, row
->p
+1,
3276 f
, P
->Constraint
[l
][nvar
+i
+1], len
-1);
3277 if (!(info
[i
] & INDEPENDENT
)) {
3279 for (j
= 0; j
< exist
; ++j
)
3280 if (j
!= i
&& value_notzero_p(row
->p
[nvar
+j
+1]))
3283 //printf("independent: i: %d, l: %d, u: %d\n", i, l, u);
3284 info
[i
] = (constraint
)(info
[i
] | INDEPENDENT
);
3287 if (info
[i
] & ALL_POS
) {
3288 value_addto(row
->p
[len
-1], row
->p
[len
-1],
3289 P
->Constraint
[l
][nvar
+i
+1]);
3290 value_addto(row
->p
[len
-1], row
->p
[len
-1], f
);
3291 value_multiply(f
, f
, P
->Constraint
[l
][nvar
+i
+1]);
3292 value_subtract(row
->p
[len
-1], row
->p
[len
-1], f
);
3293 value_decrement(row
->p
[len
-1], row
->p
[len
-1]);
3294 ConstraintSimplify(row
->p
, row
->p
, len
, &f
);
3295 value_set_si(f
, -1);
3296 Vector_Scale(row
->p
+1, row
->p
+1, f
, len
-1);
3297 value_decrement(row
->p
[len
-1], row
->p
[len
-1]);
3298 Polyhedron
*T
= AddConstraints(row
->p
, 1, P
, options
->MaxRays
);
3300 //printf("not all_pos: i: %d, l: %d, u: %d\n", i, l, u);
3301 info
[i
] = (constraint
)(info
[i
] ^ ALL_POS
);
3303 //puts("pos remainder");
3304 //Polyhedron_Print(stdout, P_VALUE_FMT, T);
3307 if (!(info
[i
] & ONE_NEG
)) {
3309 negative_test_constraint(P
->Constraint
[l
],
3311 row
->p
, nvar
+i
, len
, &f
);
3312 oppose_constraint(row
->p
, len
, &f
);
3313 Polyhedron
*T
= AddConstraints(row
->p
, 1, P
,
3316 //printf("one_neg i: %d, l: %d, u: %d\n", i, l, u);
3317 info
[i
] = (constraint
)(info
[i
] | ONE_NEG
);
3319 //puts("neg remainder");
3320 //Polyhedron_Print(stdout, P_VALUE_FMT, T);
3322 } else if (!(info
[i
] & ROT_NEG
)) {
3323 if (parallel_constraints(P
->Constraint
[l
],
3325 row
->p
, nvar
, exist
)) {
3326 negative_test_constraint7(P
->Constraint
[l
],
3328 row
->p
, nvar
, exist
,
3330 oppose_constraint(row
->p
, len
, &f
);
3331 Polyhedron
*T
= AddConstraints(row
->p
, 1, P
,
3334 // printf("rot_neg i: %d, l: %d, u: %d\n", i, l, u);
3335 info
[i
] = (constraint
)(info
[i
] | ROT_NEG
);
3338 //puts("neg remainder");
3339 //Polyhedron_Print(stdout, P_VALUE_FMT, T);
3344 if (!(info
[i
] & ALL_POS
) && (info
[i
] & (ONE_NEG
| ROT_NEG
)))
3348 if (info
[i
] & ALL_POS
)
3355 for (int i = 0; i < exist; ++i)
3356 printf("%i: %i\n", i, info[i]);
3358 for (int i
= 0; i
< exist
; ++i
)
3359 if (info
[i
] & ALL_POS
) {
3361 fprintf(stderr
, "\nER: Positive\n");
3362 #endif /* DEBUG_ER */
3364 // Maybe we should chew off some of the fat here
3365 Matrix
*M
= Matrix_Alloc(P
->Dimension
, P
->Dimension
+1);
3366 for (int j
= 0; j
< P
->Dimension
; ++j
)
3367 value_set_si(M
->p
[j
][j
+ (j
>= i
+nvar
)], 1);
3368 Polyhedron
*T
= Polyhedron_Image(P
, M
, options
->MaxRays
);
3370 evalue
*EP
= barvinok_enumerate_e_with_options(T
, exist
-1, nparam
,
3378 for (int i
= 0; i
< exist
; ++i
)
3379 if (info
[i
] & ONE_NEG
) {
3381 fprintf(stderr
, "\nER: Negative\n");
3382 #endif /* DEBUG_ER */
3387 return barvinok_enumerate_e_with_options(P
, exist
-1, nparam
,
3390 Polyhedron
*T
= Polyhedron_Copy(P
);
3391 SwapColumns(T
, nvar
+1, nvar
+1+i
);
3392 evalue
*EP
= barvinok_enumerate_e_with_options(T
, exist
-1, nparam
,
3398 for (int i
= 0; i
< exist
; ++i
)
3399 if (info
[i
] & ROT_NEG
) {
3401 fprintf(stderr
, "\nER: Rotate\n");
3402 #endif /* DEBUG_ER */
3406 Polyhedron
*T
= rotate_along(P
, r
, nvar
, exist
, options
->MaxRays
);
3407 evalue
*EP
= barvinok_enumerate_e_with_options(T
, exist
-1, nparam
,
3412 for (int i
= 0; i
< exist
; ++i
)
3413 if (info
[i
] & INDEPENDENT
) {
3414 Polyhedron
*pos
, *neg
;
3416 /* Find constraint again and split off negative part */
3418 if (SplitOnVar(P
, i
, nvar
, exist
, options
->MaxRays
,
3419 row
, f
, true, &pos
, &neg
)) {
3421 fprintf(stderr
, "\nER: Split\n");
3422 #endif /* DEBUG_ER */
3425 barvinok_enumerate_e_with_options(neg
, exist
-1, nparam
, options
);
3427 barvinok_enumerate_e_with_options(pos
, exist
, nparam
, options
);
3429 free_evalue_refs(E
);
3431 Polyhedron_Free(neg
);
3432 Polyhedron_Free(pos
);
3446 EP
= enumerate_line(P
, exist
, nparam
, options
);
3450 EP
= barvinok_enumerate_pip_with_options(P
, exist
, nparam
, options
);
3454 EP
= enumerate_redundant_ray(P
, exist
, nparam
, options
);
3458 EP
= enumerate_sure(P
, exist
, nparam
, options
);
3462 EP
= enumerate_ray(P
, exist
, nparam
, options
);
3466 EP
= enumerate_sure2(P
, exist
, nparam
, options
);
3470 F
= unfringe(P
, options
->MaxRays
);
3471 if (!PolyhedronIncludes(F
, P
)) {
3473 fprintf(stderr
, "\nER: Fringed\n");
3474 #endif /* DEBUG_ER */
3475 EP
= barvinok_enumerate_e_with_options(F
, exist
, nparam
, options
);
3482 EP
= enumerate_vd(&P
, exist
, nparam
, options
);
3487 EP
= enumerate_sum(P
, exist
, nparam
, options
);
3494 Polyhedron
*pos
, *neg
;
3495 for (i
= 0; i
< exist
; ++i
)
3496 if (SplitOnVar(P
, i
, nvar
, exist
, options
->MaxRays
,
3497 row
, f
, false, &pos
, &neg
))
3503 EP
= enumerate_or(pos
, exist
, nparam
, options
);
3516 * remove equalities that require a "compression" of the parameters
3518 static Polyhedron
*remove_more_equalities(Polyhedron
*P
, unsigned nparam
,
3519 Matrix
**CP
, unsigned MaxRays
)
3522 remove_all_equalities(&P
, NULL
, CP
, NULL
, nparam
, MaxRays
);
3529 static gen_fun
*series(Polyhedron
*P
, unsigned nparam
, barvinok_options
*options
)
3539 assert(!Polyhedron_is_infinite_param(P
, nparam
));
3540 assert(P
->NbBid
== 0);
3541 assert(Polyhedron_has_revlex_positive_rays(P
, nparam
));
3543 P
= remove_more_equalities(P
, nparam
, &CP
, options
->MaxRays
);
3544 assert(P
->NbEq
== 0);
3546 nparam
= CP
->NbColumns
-1;
3551 barvinok_count_with_options(P
, &c
, options
);
3552 gf
= new gen_fun(c
);
3556 red
= gf_base::create(Polyhedron_Project(P
, nparam
),
3557 P
->Dimension
, nparam
, options
);
3558 POL_ENSURE_VERTICES(P
);
3559 red
->start_gf(P
, options
);
3571 gen_fun
* barvinok_series_with_options(Polyhedron
*P
, Polyhedron
* C
,
3572 barvinok_options
*options
)
3575 unsigned nparam
= C
->Dimension
;
3578 CA
= align_context(C
, P
->Dimension
, options
->MaxRays
);
3579 P
= DomainIntersection(P
, CA
, options
->MaxRays
);
3580 Polyhedron_Free(CA
);
3582 gf
= series(P
, nparam
, options
);
3587 gen_fun
* barvinok_series(Polyhedron
*P
, Polyhedron
* C
, unsigned MaxRays
)
3590 barvinok_options
*options
= barvinok_options_new_with_defaults();
3591 options
->MaxRays
= MaxRays
;
3592 gf
= barvinok_series_with_options(P
, C
, options
);
3597 static Polyhedron
*skew_into_positive_orthant(Polyhedron
*D
, unsigned nparam
,
3603 for (Polyhedron
*P
= D
; P
; P
= P
->next
) {
3604 POL_ENSURE_VERTICES(P
);
3605 assert(!Polyhedron_is_infinite_param(P
, nparam
));
3606 assert(P
->NbBid
== 0);
3607 assert(Polyhedron_has_positive_rays(P
, nparam
));
3609 for (int r
= 0; r
< P
->NbRays
; ++r
) {
3610 if (value_notzero_p(P
->Ray
[r
][P
->Dimension
+1]))
3612 for (int i
= 0; i
< nparam
; ++i
) {
3614 if (value_posz_p(P
->Ray
[r
][i
+1]))
3617 M
= Matrix_Alloc(D
->Dimension
+1, D
->Dimension
+1);
3618 for (int i
= 0; i
< D
->Dimension
+1; ++i
)
3619 value_set_si(M
->p
[i
][i
], 1);
3621 Inner_Product(P
->Ray
[r
]+1, M
->p
[i
], D
->Dimension
+1, &tmp
);
3622 if (value_posz_p(tmp
))
3625 for (j
= P
->Dimension
- nparam
; j
< P
->Dimension
; ++j
)
3626 if (value_pos_p(P
->Ray
[r
][j
+1]))
3628 assert(j
< P
->Dimension
);
3629 value_pdivision(tmp
, P
->Ray
[r
][j
+1], P
->Ray
[r
][i
+1]);
3630 value_subtract(M
->p
[i
][j
], M
->p
[i
][j
], tmp
);
3636 D
= DomainImage(D
, M
, MaxRays
);
3642 gen_fun
* barvinok_enumerate_union_series_with_options(Polyhedron
*D
, Polyhedron
* C
,
3643 barvinok_options
*options
)
3645 Polyhedron
*conv
, *D2
;
3647 gen_fun
*gf
= NULL
, *gf2
;
3648 unsigned nparam
= C
->Dimension
;
3653 CA
= align_context(C
, D
->Dimension
, options
->MaxRays
);
3654 D
= DomainIntersection(D
, CA
, options
->MaxRays
);
3655 Polyhedron_Free(CA
);
3657 D2
= skew_into_positive_orthant(D
, nparam
, options
->MaxRays
);
3658 for (Polyhedron
*P
= D2
; P
; P
= P
->next
) {
3659 assert(P
->Dimension
== D2
->Dimension
);
3662 P_gf
= series(Polyhedron_Copy(P
), nparam
, options
);
3666 gf
->add_union(P_gf
, options
);
3670 /* we actually only need the convex union of the parameter space
3671 * but the reducer classes currently expect a polyhedron in
3672 * the combined space
3674 Polyhedron_Free(gf
->context
);
3675 gf
->context
= DomainConvex(D2
, options
->MaxRays
);
3677 gf2
= gf
->summate(D2
->Dimension
- nparam
, options
);
3686 gen_fun
* barvinok_enumerate_union_series(Polyhedron
*D
, Polyhedron
* C
,
3690 barvinok_options
*options
= barvinok_options_new_with_defaults();
3691 options
->MaxRays
= MaxRays
;
3692 gf
= barvinok_enumerate_union_series_with_options(D
, C
, options
);
3697 evalue
* barvinok_enumerate_union(Polyhedron
*D
, Polyhedron
* C
, unsigned MaxRays
)
3700 gen_fun
*gf
= barvinok_enumerate_union_series(D
, C
, MaxRays
);