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"
27 #ifndef HAVE_PARAM_POLYHEDRON_SCALE_INTEGER
28 extern "C" void Param_Polyhedron_Scale_Integer(Param_Polyhedron
*PP
, Polyhedron
**P
,
29 Value
*det
, unsigned MaxRays
);
41 using std::ostringstream
;
43 #define ALLOC(t,p) p = (t*)malloc(sizeof(*p))
51 dpoly_n(int d
, ZZ
& degree_0
, ZZ
& degree_1
, int offset
= 0) {
55 zz2value(degree_0
, d0
);
56 zz2value(degree_1
, d1
);
57 coeff
= Matrix_Alloc(d
+1, d
+1+1);
58 value_set_si(coeff
->p
[0][0], 1);
59 value_set_si(coeff
->p
[0][d
+1], 1);
60 for (int i
= 1; i
<= d
; ++i
) {
61 value_multiply(coeff
->p
[i
][0], coeff
->p
[i
-1][0], d0
);
62 Vector_Combine(coeff
->p
[i
-1], coeff
->p
[i
-1]+1, coeff
->p
[i
]+1,
64 value_set_si(coeff
->p
[i
][d
+1], i
);
65 value_multiply(coeff
->p
[i
][d
+1], coeff
->p
[i
][d
+1], coeff
->p
[i
-1][d
+1]);
66 value_decrement(d0
, d0
);
71 void div(dpoly
& d
, Vector
*count
, ZZ
& sign
) {
72 int len
= coeff
->NbRows
;
73 Matrix
* c
= Matrix_Alloc(coeff
->NbRows
, coeff
->NbColumns
);
76 for (int i
= 0; i
< len
; ++i
) {
77 Vector_Copy(coeff
->p
[i
], c
->p
[i
], len
+1);
78 for (int j
= 1; j
<= i
; ++j
) {
79 zz2value(d
.coeff
[j
], tmp
);
80 value_multiply(tmp
, tmp
, c
->p
[i
][len
]);
81 value_oppose(tmp
, tmp
);
82 Vector_Combine(c
->p
[i
], c
->p
[i
-j
], c
->p
[i
],
83 c
->p
[i
-j
][len
], tmp
, len
);
84 value_multiply(c
->p
[i
][len
], c
->p
[i
][len
], c
->p
[i
-j
][len
]);
86 zz2value(d
.coeff
[0], tmp
);
87 value_multiply(c
->p
[i
][len
], c
->p
[i
][len
], tmp
);
90 value_set_si(tmp
, -1);
91 Vector_Scale(c
->p
[len
-1], count
->p
, tmp
, len
);
92 value_assign(count
->p
[len
], c
->p
[len
-1][len
]);
94 Vector_Copy(c
->p
[len
-1], count
->p
, len
+1);
95 Vector_Normalize(count
->p
, len
+1);
101 const int MAX_TRY
=10;
103 * Searches for a vector that is not orthogonal to any
104 * of the rays in rays.
106 static void nonorthog(mat_ZZ
& rays
, vec_ZZ
& lambda
)
108 int dim
= rays
.NumCols();
110 lambda
.SetLength(dim
);
114 for (int i
= 2; !found
&& i
<= 50*dim
; i
+=4) {
115 for (int j
= 0; j
< MAX_TRY
; ++j
) {
116 for (int k
= 0; k
< dim
; ++k
) {
117 int r
= random_int(i
)+2;
118 int v
= (2*(r
%2)-1) * (r
>> 1);
122 for (; k
< rays
.NumRows(); ++k
)
123 if (lambda
* rays
[k
] == 0)
125 if (k
== rays
.NumRows()) {
134 static void add_rays(mat_ZZ
& rays
, Polyhedron
*i
, int *r
, int nvar
= -1,
137 unsigned dim
= i
->Dimension
;
140 for (int k
= 0; k
< i
->NbRays
; ++k
) {
141 if (!value_zero_p(i
->Ray
[k
][dim
+1]))
143 if (!all
&& nvar
!= dim
&& First_Non_Zero(i
->Ray
[k
]+1, nvar
) == -1)
145 values2zz(i
->Ray
[k
]+1, rays
[(*r
)++], nvar
);
149 static void mask_r(Matrix
*f
, int nr
, Vector
*lcm
, int p
, Vector
*val
, evalue
*ev
)
151 unsigned nparam
= lcm
->Size
;
154 Vector
* prod
= Vector_Alloc(f
->NbRows
);
155 Matrix_Vector_Product(f
, val
->p
, prod
->p
);
157 for (int i
= 0; i
< nr
; ++i
) {
158 value_modulus(prod
->p
[i
], prod
->p
[i
], f
->p
[i
][nparam
+1]);
159 isint
&= value_zero_p(prod
->p
[i
]);
161 value_set_si(ev
->d
, 1);
163 value_set_si(ev
->x
.n
, isint
);
170 if (value_one_p(lcm
->p
[p
]))
171 mask_r(f
, nr
, lcm
, p
+1, val
, ev
);
173 value_assign(tmp
, lcm
->p
[p
]);
174 value_set_si(ev
->d
, 0);
175 ev
->x
.p
= new_enode(periodic
, VALUE_TO_INT(tmp
), p
+1);
177 value_decrement(tmp
, tmp
);
178 value_assign(val
->p
[p
], tmp
);
179 mask_r(f
, nr
, lcm
, p
+1, val
, &ev
->x
.p
->arr
[VALUE_TO_INT(tmp
)]);
180 } while (value_pos_p(tmp
));
185 static void mask_fractional(Matrix
*f
, evalue
*factor
)
187 int nr
= f
->NbRows
, nc
= f
->NbColumns
;
190 for (n
= 0; n
< nr
&& value_notzero_p(f
->p
[n
][nc
-1]); ++n
)
191 if (value_notone_p(f
->p
[n
][nc
-1]) &&
192 value_notmone_p(f
->p
[n
][nc
-1]))
206 value_set_si(EV
.x
.n
, 1);
208 for (n
= 0; n
< nr
; ++n
) {
209 value_assign(m
, f
->p
[n
][nc
-1]);
210 if (value_one_p(m
) || value_mone_p(m
))
213 int j
= normal_mod(f
->p
[n
], nc
-1, &m
);
215 free_evalue_refs(factor
);
216 value_init(factor
->d
);
217 evalue_set_si(factor
, 0, 1);
221 values2zz(f
->p
[n
], row
, nc
-1);
224 if (j
< (nc
-1)-1 && row
[j
] > g
/2) {
225 for (int k
= j
; k
< (nc
-1); ++k
)
231 value_set_si(EP
.d
, 0);
232 EP
.x
.p
= new_enode(relation
, 2, 0);
233 value_clear(EP
.x
.p
->arr
[1].d
);
234 EP
.x
.p
->arr
[1] = *factor
;
235 evalue
*ev
= &EP
.x
.p
->arr
[0];
236 value_set_si(ev
->d
, 0);
237 ev
->x
.p
= new_enode(fractional
, 3, -1);
238 evalue_set_si(&ev
->x
.p
->arr
[1], 0, 1);
239 evalue_set_si(&ev
->x
.p
->arr
[2], 1, 1);
240 evalue
*E
= multi_monom(row
);
241 value_assign(EV
.d
, m
);
243 value_clear(ev
->x
.p
->arr
[0].d
);
244 ev
->x
.p
->arr
[0] = *E
;
250 free_evalue_refs(&EV
);
256 static void mask_table(Matrix
*f
, evalue
*factor
)
258 int nr
= f
->NbRows
, nc
= f
->NbColumns
;
261 for (n
= 0; n
< nr
&& value_notzero_p(f
->p
[n
][nc
-1]); ++n
)
262 if (value_notone_p(f
->p
[n
][nc
-1]) &&
263 value_notmone_p(f
->p
[n
][nc
-1]))
271 unsigned np
= nc
- 2;
272 Vector
*lcm
= Vector_Alloc(np
);
273 Vector
*val
= Vector_Alloc(nc
);
274 Vector_Set(val
->p
, 0, nc
);
275 value_set_si(val
->p
[np
], 1);
276 Vector_Set(lcm
->p
, 1, np
);
277 for (n
= 0; n
< nr
; ++n
) {
278 if (value_one_p(f
->p
[n
][nc
-1]) ||
279 value_mone_p(f
->p
[n
][nc
-1]))
281 for (int j
= 0; j
< np
; ++j
)
282 if (value_notzero_p(f
->p
[n
][j
])) {
283 Gcd(f
->p
[n
][j
], f
->p
[n
][nc
-1], &tmp
);
284 value_division(tmp
, f
->p
[n
][nc
-1], tmp
);
285 value_lcm(tmp
, lcm
->p
[j
], &lcm
->p
[j
]);
290 mask_r(f
, nr
, lcm
, 0, val
, &EP
);
295 free_evalue_refs(&EP
);
298 static void mask(Matrix
*f
, evalue
*factor
, barvinok_options
*options
)
300 if (options
->lookup_table
)
301 mask_table(f
, factor
);
303 mask_fractional(f
, factor
);
306 /* This structure encodes the power of the term in a rational generating function.
308 * Either E == NULL or constant = 0
309 * If E != NULL, then the power is E
310 * If E == NULL, then the power is coeff * param[pos] + constant
319 /* Returns the power of (t+1) in the term of a rational generating function,
320 * i.e., the scalar product of the actual lattice point and lambda.
321 * The lattice point is the unique lattice point in the fundamental parallelepiped
322 * of the unimodual cone i shifted to the parametric vertex V.
324 * PD is the parameter domain, which, if != NULL, may be used to simply the
325 * resulting expression.
327 * The result is returned in term.
329 void lattice_point(Param_Vertices
* V
, const mat_ZZ
& rays
, vec_ZZ
& lambda
,
330 term_info
* term
, Polyhedron
*PD
, barvinok_options
*options
)
332 unsigned nparam
= V
->Vertex
->NbColumns
- 2;
333 unsigned dim
= rays
.NumCols();
335 vertex
.SetDims(V
->Vertex
->NbRows
, nparam
+1);
339 value_set_si(lcm
, 1);
340 for (int j
= 0; j
< V
->Vertex
->NbRows
; ++j
) {
341 value_lcm(lcm
, V
->Vertex
->p
[j
][nparam
+1], &lcm
);
343 if (value_notone_p(lcm
)) {
344 Matrix
* mv
= Matrix_Alloc(dim
, nparam
+1);
345 for (int j
= 0 ; j
< dim
; ++j
) {
346 value_division(tmp
, lcm
, V
->Vertex
->p
[j
][nparam
+1]);
347 Vector_Scale(V
->Vertex
->p
[j
], mv
->p
[j
], tmp
, nparam
+1);
350 term
->E
= lattice_point(rays
, lambda
, mv
, lcm
, PD
, options
);
358 for (int i
= 0; i
< V
->Vertex
->NbRows
; ++i
) {
359 assert(value_one_p(V
->Vertex
->p
[i
][nparam
+1])); // for now
360 values2zz(V
->Vertex
->p
[i
], vertex
[i
], nparam
+1);
364 num
= lambda
* vertex
;
368 for (int j
= 0; j
< nparam
; ++j
)
374 term
->E
= multi_monom(num
);
378 term
->constant
= num
[nparam
];
381 term
->coeff
= num
[p
];
389 struct counter
: public np_base
{
399 counter(unsigned dim
) : np_base(dim
) {
404 virtual void init(Polyhedron
*P
) {
405 randomvector(P
, lambda
, dim
);
408 virtual void reset() {
409 mpq_set_si(count
, 0, 0);
416 virtual void handle(const mat_ZZ
& rays
, Value
*vertex
, const QQ
& c
,
417 unsigned long det
, int *closed
, barvinok_options
*options
);
418 virtual void get_count(Value
*result
) {
419 assert(value_one_p(&count
[0]._mp_den
));
420 value_assign(*result
, &count
[0]._mp_num
);
424 void counter::handle(const mat_ZZ
& rays
, Value
*V
, const QQ
& c
, unsigned long det
,
425 int *closed
, barvinok_options
*options
)
427 for (int k
= 0; k
< dim
; ++k
) {
428 if (lambda
* rays
[k
] == 0)
433 assert(c
.n
== 1 || c
.n
== -1);
436 lattice_point(V
, rays
, vertex
, det
, closed
);
437 num
= vertex
* lambda
;
440 normalize(sign
, offset
, den
);
443 dpoly
d(dim
, num
[0]);
444 for (int k
= 1; k
< num
.length(); ++k
) {
446 dpoly
term(dim
, num
[k
]);
449 dpoly
n(dim
, den
[0], 1);
450 for (int k
= 1; k
< dim
; ++k
) {
451 dpoly
fact(dim
, den
[k
], 1);
454 d
.div(n
, count
, sign
);
457 struct bfe_term
: public bfc_term_base
{
458 vector
<evalue
*> factors
;
460 bfe_term(int len
) : bfc_term_base(len
) {
464 for (int i
= 0; i
< factors
.size(); ++i
) {
467 free_evalue_refs(factors
[i
]);
473 static void print_int_vector(int *v
, int len
, char *name
)
475 cerr
<< name
<< endl
;
476 for (int j
= 0; j
< len
; ++j
) {
482 static void print_bfc_terms(mat_ZZ
& factors
, bfc_vec
& v
)
485 cerr
<< "factors" << endl
;
486 cerr
<< factors
<< endl
;
487 for (int i
= 0; i
< v
.size(); ++i
) {
488 cerr
<< "term: " << i
<< endl
;
489 print_int_vector(v
[i
]->powers
, factors
.NumRows(), "powers");
490 cerr
<< "terms" << endl
;
491 cerr
<< v
[i
]->terms
<< endl
;
492 bfc_term
* bfct
= static_cast<bfc_term
*>(v
[i
]);
493 cerr
<< bfct
->c
<< endl
;
497 static void print_bfe_terms(mat_ZZ
& factors
, bfc_vec
& v
)
500 cerr
<< "factors" << endl
;
501 cerr
<< factors
<< endl
;
502 for (int i
= 0; i
< v
.size(); ++i
) {
503 cerr
<< "term: " << i
<< endl
;
504 print_int_vector(v
[i
]->powers
, factors
.NumRows(), "powers");
505 cerr
<< "terms" << endl
;
506 cerr
<< v
[i
]->terms
<< endl
;
507 bfe_term
* bfet
= static_cast<bfe_term
*>(v
[i
]);
508 for (int j
= 0; j
< v
[i
]->terms
.NumRows(); ++j
) {
509 char * test
[] = {"a", "b"};
510 print_evalue(stderr
, bfet
->factors
[j
], test
);
511 fprintf(stderr
, "\n");
516 struct bfcounter
: public bfcounter_base
{
519 bfcounter(unsigned dim
) : bfcounter_base(dim
) {
526 virtual void base(mat_ZZ
& factors
, bfc_vec
& v
);
527 virtual void get_count(Value
*result
) {
528 assert(value_one_p(&count
[0]._mp_den
));
529 value_assign(*result
, &count
[0]._mp_num
);
533 void bfcounter::base(mat_ZZ
& factors
, bfc_vec
& v
)
535 unsigned nf
= factors
.NumRows();
537 for (int i
= 0; i
< v
.size(); ++i
) {
538 bfc_term
* bfct
= static_cast<bfc_term
*>(v
[i
]);
540 // factor is always positive, so we always
542 for (int k
= 0; k
< nf
; ++k
)
543 total_power
+= v
[i
]->powers
[k
];
546 for (j
= 0; j
< nf
; ++j
)
547 if (v
[i
]->powers
[j
] > 0)
550 dpoly
D(total_power
, factors
[j
][0], 1);
551 for (int k
= 1; k
< v
[i
]->powers
[j
]; ++k
) {
552 dpoly
fact(total_power
, factors
[j
][0], 1);
556 for (int k
= 0; k
< v
[i
]->powers
[j
]; ++k
) {
557 dpoly
fact(total_power
, factors
[j
][0], 1);
561 for (int k
= 0; k
< v
[i
]->terms
.NumRows(); ++k
) {
562 dpoly
n(total_power
, v
[i
]->terms
[k
][0]);
563 mpq_set_si(tcount
, 0, 1);
564 n
.div(D
, tcount
, one
);
566 bfct
->c
[k
].n
= -bfct
->c
[k
].n
;
567 zz2value(bfct
->c
[k
].n
, tn
);
568 zz2value(bfct
->c
[k
].d
, td
);
570 mpz_mul(mpq_numref(tcount
), mpq_numref(tcount
), tn
);
571 mpz_mul(mpq_denref(tcount
), mpq_denref(tcount
), td
);
572 mpq_canonicalize(tcount
);
573 mpq_add(count
, count
, tcount
);
580 /* Check whether the polyhedron is unbounded and if so,
581 * check whether it has any (and therefore an infinite number of)
583 * If one of the vertices is integer, then we are done.
584 * Otherwise, transform the polyhedron such that one of the rays
585 * is the first unit vector and cut it off at a height that ensures
586 * that if the whole polyhedron has any points, then the remaining part
587 * has integer points. In particular we add the largest coefficient
588 * of a ray to the highest vertex (rounded up).
590 static bool Polyhedron_is_infinite(Polyhedron
*P
, Value
* result
,
591 barvinok_options
*options
)
603 for (; r
< P
->NbRays
; ++r
)
604 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1]))
606 if (P
->NbBid
== 0 && r
== P
->NbRays
)
609 if (options
->count_sample_infinite
) {
612 sample
= Polyhedron_Sample(P
, options
);
614 value_set_si(*result
, 0);
616 value_set_si(*result
, -1);
622 for (int i
= 0; i
< P
->NbRays
; ++i
)
623 if (value_one_p(P
->Ray
[i
][1+P
->Dimension
])) {
624 value_set_si(*result
, -1);
629 v
= Vector_Alloc(P
->Dimension
+1);
630 Vector_Gcd(P
->Ray
[r
]+1, P
->Dimension
, &g
);
631 Vector_AntiScale(P
->Ray
[r
]+1, v
->p
, g
, P
->Dimension
+1);
632 M
= unimodular_complete(v
);
633 value_set_si(M
->p
[P
->Dimension
][P
->Dimension
], 1);
636 P
= Polyhedron_Preimage(P
, M2
, 0);
645 value_set_si(size
, 0);
647 for (int i
= 0; i
< P
->NbBid
; ++i
) {
648 value_absolute(tmp
, P
->Ray
[i
][1]);
649 if (value_gt(tmp
, size
))
650 value_assign(size
, tmp
);
652 for (int i
= P
->NbBid
; i
< P
->NbRays
; ++i
) {
653 if (value_zero_p(P
->Ray
[i
][P
->Dimension
+1])) {
654 if (value_gt(P
->Ray
[i
][1], size
))
655 value_assign(size
, P
->Ray
[i
][1]);
658 mpz_cdiv_q(tmp
, P
->Ray
[i
][1], P
->Ray
[i
][P
->Dimension
+1]);
659 if (first
|| value_gt(tmp
, offset
)) {
660 value_assign(offset
, tmp
);
664 value_addto(offset
, offset
, size
);
668 v
= Vector_Alloc(P
->Dimension
+2);
669 value_set_si(v
->p
[0], 1);
670 value_set_si(v
->p
[1], -1);
671 value_assign(v
->p
[1+P
->Dimension
], offset
);
672 R
= AddConstraints(v
->p
, 1, P
, options
->MaxRays
);
680 barvinok_count_with_options(P
, &c
, options
);
683 value_set_si(*result
, 0);
685 value_set_si(*result
, -1);
691 typedef Polyhedron
* Polyhedron_p
;
693 static void barvinok_count_f(Polyhedron
*P
, Value
* result
,
694 barvinok_options
*options
);
696 void barvinok_count_with_options(Polyhedron
*P
, Value
* result
,
697 struct barvinok_options
*options
)
702 bool infinite
= false;
706 "barvinok_count: input is a union; only first polyhedron is counted\n");
709 value_set_si(*result
, 0);
715 P
= remove_equalities(P
);
716 P
= DomainConstraintSimplify(P
, options
->MaxRays
);
720 } while (!emptyQ(P
) && P
->NbEq
!= 0);
723 value_set_si(*result
, 0);
728 if (Polyhedron_is_infinite(P
, result
, options
)) {
733 if (P
->Dimension
== 0) {
734 /* Test whether the constraints are satisfied */
735 POL_ENSURE_VERTICES(P
);
736 value_set_si(*result
, !emptyQ(P
));
741 Q
= Polyhedron_Factor(P
, 0, options
->MaxRays
);
749 barvinok_count_f(P
, result
, options
);
750 if (value_neg_p(*result
))
752 if (Q
&& P
->next
&& value_notzero_p(*result
)) {
756 for (Q
= P
->next
; Q
; Q
= Q
->next
) {
757 barvinok_count_f(Q
, &factor
, options
);
758 if (value_neg_p(factor
)) {
761 } else if (Q
->next
&& value_zero_p(factor
)) {
762 value_set_si(*result
, 0);
765 value_multiply(*result
, *result
, factor
);
774 value_set_si(*result
, -1);
777 void barvinok_count(Polyhedron
*P
, Value
* result
, unsigned NbMaxCons
)
779 barvinok_options
*options
= barvinok_options_new_with_defaults();
780 options
->MaxRays
= NbMaxCons
;
781 barvinok_count_with_options(P
, result
, options
);
782 barvinok_options_free(options
);
785 static void barvinok_count_f(Polyhedron
*P
, Value
* result
,
786 barvinok_options
*options
)
789 value_set_si(*result
, 0);
793 if (P
->Dimension
== 1)
794 return Line_Length(P
, result
);
796 int c
= P
->NbConstraints
;
797 POL_ENSURE_FACETS(P
);
798 if (c
!= P
->NbConstraints
|| P
->NbEq
!= 0)
799 return barvinok_count_with_options(P
, result
, options
);
801 POL_ENSURE_VERTICES(P
);
803 if (Polyhedron_is_infinite(P
, result
, options
))
807 if (options
->incremental_specialization
== 2)
808 cnt
= new bfcounter(P
->Dimension
);
809 else if (options
->incremental_specialization
== 1)
810 cnt
= new icounter(P
->Dimension
);
812 cnt
= new counter(P
->Dimension
);
813 cnt
->start(P
, options
);
815 cnt
->get_count(result
);
819 static void uni_polynom(int param
, Vector
*c
, evalue
*EP
)
821 unsigned dim
= c
->Size
-2;
823 value_set_si(EP
->d
,0);
824 EP
->x
.p
= new_enode(polynomial
, dim
+1, param
+1);
825 for (int j
= 0; j
<= dim
; ++j
)
826 evalue_set(&EP
->x
.p
->arr
[j
], c
->p
[j
], c
->p
[dim
+1]);
829 static void multi_polynom(Vector
*c
, evalue
* X
, evalue
*EP
)
831 unsigned dim
= c
->Size
-2;
835 evalue_set(&EC
, c
->p
[dim
], c
->p
[dim
+1]);
838 evalue_set(EP
, c
->p
[dim
], c
->p
[dim
+1]);
840 for (int i
= dim
-1; i
>= 0; --i
) {
842 value_assign(EC
.x
.n
, c
->p
[i
]);
845 free_evalue_refs(&EC
);
848 Polyhedron
*unfringe (Polyhedron
*P
, unsigned MaxRays
)
850 int len
= P
->Dimension
+2;
851 Polyhedron
*T
, *R
= P
;
854 Vector
*row
= Vector_Alloc(len
);
855 value_set_si(row
->p
[0], 1);
857 R
= DomainConstraintSimplify(Polyhedron_Copy(P
), MaxRays
);
859 Matrix
*M
= Matrix_Alloc(2, len
-1);
860 value_set_si(M
->p
[1][len
-2], 1);
861 for (int v
= 0; v
< P
->Dimension
; ++v
) {
862 value_set_si(M
->p
[0][v
], 1);
863 Polyhedron
*I
= Polyhedron_Image(R
, M
, 2+1);
864 value_set_si(M
->p
[0][v
], 0);
865 for (int r
= 0; r
< I
->NbConstraints
; ++r
) {
866 if (value_zero_p(I
->Constraint
[r
][0]))
868 if (value_zero_p(I
->Constraint
[r
][1]))
870 if (value_one_p(I
->Constraint
[r
][1]))
872 if (value_mone_p(I
->Constraint
[r
][1]))
874 value_absolute(g
, I
->Constraint
[r
][1]);
875 Vector_Set(row
->p
+1, 0, len
-2);
876 value_division(row
->p
[1+v
], I
->Constraint
[r
][1], g
);
877 mpz_fdiv_q(row
->p
[len
-1], I
->Constraint
[r
][2], g
);
879 R
= AddConstraints(row
->p
, 1, R
, MaxRays
);
891 /* this procedure may have false negatives */
892 static bool Polyhedron_is_infinite_param(Polyhedron
*P
, unsigned nparam
)
895 for (r
= 0; r
< P
->NbRays
; ++r
) {
896 if (!value_zero_p(P
->Ray
[r
][0]) &&
897 !value_zero_p(P
->Ray
[r
][P
->Dimension
+1]))
899 if (First_Non_Zero(P
->Ray
[r
]+1+P
->Dimension
-nparam
, nparam
) == -1)
905 /* Check whether all rays point in the positive directions
908 static bool Polyhedron_has_positive_rays(Polyhedron
*P
, unsigned nparam
)
911 for (r
= 0; r
< P
->NbRays
; ++r
)
912 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1])) {
914 for (i
= P
->Dimension
- nparam
; i
< P
->Dimension
; ++i
)
915 if (value_neg_p(P
->Ray
[r
][i
+1]))
921 typedef evalue
* evalue_p
;
923 struct enumerator_base
{
927 vertex_decomposer
*vpd
;
929 enumerator_base(unsigned dim
, vertex_decomposer
*vpd
)
934 vE
= new evalue_p
[vpd
->nbV
];
935 for (int j
= 0; j
< vpd
->nbV
; ++j
)
939 evalue_set_si(&mone
, -1, 1);
942 void decompose_at(Param_Vertices
*V
, int _i
, barvinok_options
*options
) {
946 value_init(vE
[_i
]->d
);
947 evalue_set_si(vE
[_i
], 0, 1);
949 vpd
->decompose_at_vertex(V
, _i
, options
);
952 virtual ~enumerator_base() {
953 for (int j
= 0; j
< vpd
->nbV
; ++j
)
955 free_evalue_refs(vE
[j
]);
960 free_evalue_refs(&mone
);
963 static enumerator_base
*create(Polyhedron
*P
, unsigned dim
, unsigned nbV
,
964 barvinok_options
*options
);
967 struct enumerator
: public signed_cone_consumer
, public vertex_decomposer
,
968 public enumerator_base
{
976 enumerator(Polyhedron
*P
, unsigned dim
, unsigned nbV
) :
977 vertex_decomposer(P
, nbV
, *this), enumerator_base(dim
, this) {
980 randomvector(P
, lambda
, dim
);
982 c
= Vector_Alloc(dim
+2);
992 virtual void handle(const signed_cone
& sc
, barvinok_options
*options
);
995 void enumerator::handle(const signed_cone
& sc
, barvinok_options
*options
)
1000 assert(sc
.rays
.NumRows() == dim
);
1001 for (int k
= 0; k
< dim
; ++k
) {
1002 if (lambda
* sc
.rays
[k
] == 0)
1008 lattice_point(V
, sc
.rays
, lambda
, &num
, 0, options
);
1009 den
= sc
.rays
* lambda
;
1010 normalize(sign
, num
.constant
, den
);
1012 dpoly
n(dim
, den
[0], 1);
1013 for (int k
= 1; k
< dim
; ++k
) {
1014 dpoly
fact(dim
, den
[k
], 1);
1017 if (num
.E
!= NULL
) {
1018 ZZ
one(INIT_VAL
, 1);
1019 dpoly_n
d(dim
, num
.constant
, one
);
1022 multi_polynom(c
, num
.E
, &EV
);
1023 eadd(&EV
, vE
[vert
]);
1024 free_evalue_refs(&EV
);
1025 free_evalue_refs(num
.E
);
1027 } else if (num
.pos
!= -1) {
1028 dpoly_n
d(dim
, num
.constant
, num
.coeff
);
1031 uni_polynom(num
.pos
, c
, &EV
);
1032 eadd(&EV
, vE
[vert
]);
1033 free_evalue_refs(&EV
);
1035 mpq_set_si(count
, 0, 1);
1036 dpoly
d(dim
, num
.constant
);
1037 d
.div(n
, count
, sign
);
1040 evalue_set(&EV
, &count
[0]._mp_num
, &count
[0]._mp_den
);
1041 eadd(&EV
, vE
[vert
]);
1042 free_evalue_refs(&EV
);
1046 struct ienumerator_base
: enumerator_base
{
1049 ienumerator_base(unsigned dim
, vertex_decomposer
*vpd
) :
1050 enumerator_base(dim
,vpd
) {
1051 E_vertex
= new evalue_p
[dim
];
1054 virtual ~ienumerator_base() {
1058 evalue
*E_num(int i
, int d
) {
1059 return E_vertex
[i
+ (dim
-d
)];
1068 cumulator(evalue
*factor
, evalue
*v
, dpoly_r
*r
) :
1069 factor(factor
), v(v
), r(r
) {}
1071 void cumulate(barvinok_options
*options
);
1073 virtual void add_term(const vector
<int>& powers
, evalue
*f2
) = 0;
1074 virtual ~cumulator() {}
1077 void cumulator::cumulate(barvinok_options
*options
)
1079 evalue cum
; // factor * 1 * E_num[0]/1 * (E_num[0]-1)/2 *...
1081 evalue t
; // E_num[0] - (m-1)
1085 if (options
->lookup_table
) {
1087 evalue_set_si(&mone
, -1, 1);
1091 evalue_copy(&cum
, factor
);
1094 value_set_si(f
.d
, 1);
1095 value_set_si(f
.x
.n
, 1);
1099 if (!options
->lookup_table
) {
1100 for (cst
= &t
; value_zero_p(cst
->d
); ) {
1101 if (cst
->x
.p
->type
== fractional
)
1102 cst
= &cst
->x
.p
->arr
[1];
1104 cst
= &cst
->x
.p
->arr
[0];
1108 for (int m
= 0; m
< r
->len
; ++m
) {
1111 value_set_si(f
.d
, m
);
1113 if (!options
->lookup_table
)
1114 value_subtract(cst
->x
.n
, cst
->x
.n
, cst
->d
);
1120 dpoly_r_term_list
& current
= r
->c
[r
->len
-1-m
];
1121 dpoly_r_term_list::iterator j
;
1122 for (j
= current
.begin(); j
!= current
.end(); ++j
) {
1123 if ((*j
)->coeff
== 0)
1125 evalue
*f2
= new evalue
;
1127 value_init(f2
->x
.n
);
1128 zz2value((*j
)->coeff
, f2
->x
.n
);
1129 zz2value(r
->denom
, f2
->d
);
1132 add_term((*j
)->powers
, f2
);
1135 free_evalue_refs(&f
);
1136 free_evalue_refs(&t
);
1137 free_evalue_refs(&cum
);
1138 if (options
->lookup_table
)
1139 free_evalue_refs(&mone
);
1142 struct E_poly_term
{
1147 struct ie_cum
: public cumulator
{
1148 vector
<E_poly_term
*> terms
;
1150 ie_cum(evalue
*factor
, evalue
*v
, dpoly_r
*r
) : cumulator(factor
, v
, r
) {}
1152 virtual void add_term(const vector
<int>& powers
, evalue
*f2
);
1155 void ie_cum::add_term(const vector
<int>& powers
, evalue
*f2
)
1158 for (k
= 0; k
< terms
.size(); ++k
) {
1159 if (terms
[k
]->powers
== powers
) {
1160 eadd(f2
, terms
[k
]->E
);
1161 free_evalue_refs(f2
);
1166 if (k
>= terms
.size()) {
1167 E_poly_term
*ET
= new E_poly_term
;
1168 ET
->powers
= powers
;
1170 terms
.push_back(ET
);
1174 struct ienumerator
: public signed_cone_consumer
, public vertex_decomposer
,
1175 public ienumerator_base
{
1181 ienumerator(Polyhedron
*P
, unsigned dim
, unsigned nbV
) :
1182 vertex_decomposer(P
, nbV
, *this), ienumerator_base(dim
, this) {
1183 vertex
.SetDims(1, dim
);
1185 den
.SetDims(dim
, dim
);
1193 virtual void handle(const signed_cone
& sc
, barvinok_options
*options
);
1194 void reduce(evalue
*factor
, const mat_ZZ
& num
, const mat_ZZ
& den_f
,
1195 barvinok_options
*options
);
1198 void ienumerator::reduce(evalue
*factor
, const mat_ZZ
& num
, const mat_ZZ
& den_f
,
1199 barvinok_options
*options
)
1201 unsigned len
= den_f
.NumRows(); // number of factors in den
1202 unsigned dim
= num
.NumCols();
1203 assert(num
.NumRows() == 1);
1206 eadd(factor
, vE
[vert
]);
1215 split_one(num
, num_s
, num_p
, den_f
, den_s
, den_r
);
1218 den_p
.SetLength(len
);
1222 normalize(one
, num_s
, num_p
, den_s
, den_p
, den_r
);
1224 emul(&mone
, factor
);
1228 for (int k
= 0; k
< len
; ++k
) {
1231 else if (den_s
[k
] == 0)
1234 if (no_param
== 0) {
1235 reduce(factor
, num_p
, den_r
, options
);
1239 pden
.SetDims(only_param
, dim
-1);
1241 for (k
= 0, l
= 0; k
< len
; ++k
)
1243 pden
[l
++] = den_r
[k
];
1245 for (k
= 0; k
< len
; ++k
)
1249 dpoly
n(no_param
, num_s
[0]);
1250 dpoly
D(no_param
, den_s
[k
], 1);
1251 for ( ; ++k
< len
; )
1252 if (den_p
[k
] == 0) {
1253 dpoly
fact(no_param
, den_s
[k
], 1);
1258 // if no_param + only_param == len then all powers
1259 // below will be all zero
1260 if (no_param
+ only_param
== len
) {
1261 if (E_num(0, dim
) != 0)
1262 r
= new dpoly_r(n
, len
);
1264 mpq_set_si(tcount
, 0, 1);
1266 n
.div(D
, tcount
, one
);
1268 if (value_notzero_p(mpq_numref(tcount
))) {
1272 value_assign(f
.x
.n
, mpq_numref(tcount
));
1273 value_assign(f
.d
, mpq_denref(tcount
));
1275 reduce(factor
, num_p
, pden
, options
);
1276 free_evalue_refs(&f
);
1281 for (k
= 0; k
< len
; ++k
) {
1282 if (den_s
[k
] == 0 || den_p
[k
] == 0)
1285 dpoly
pd(no_param
-1, den_s
[k
], 1);
1288 for (l
= 0; l
< k
; ++l
)
1289 if (den_r
[l
] == den_r
[k
])
1293 r
= new dpoly_r(n
, pd
, l
, len
);
1295 dpoly_r
*nr
= new dpoly_r(r
, pd
, l
, len
);
1301 dpoly_r
*rc
= r
->div(D
);
1304 if (E_num(0, dim
) == 0) {
1305 int common
= pden
.NumRows();
1306 dpoly_r_term_list
& final
= r
->c
[r
->len
-1];
1312 zz2value(r
->denom
, f
.d
);
1313 dpoly_r_term_list::iterator j
;
1314 for (j
= final
.begin(); j
!= final
.end(); ++j
) {
1315 if ((*j
)->coeff
== 0)
1318 for (int k
= 0; k
< r
->dim
; ++k
) {
1319 int n
= (*j
)->powers
[k
];
1322 pden
.SetDims(rows
+n
, pden
.NumCols());
1323 for (int l
= 0; l
< n
; ++l
)
1324 pden
[rows
+l
] = den_r
[k
];
1328 evalue_copy(&t
, factor
);
1329 zz2value((*j
)->coeff
, f
.x
.n
);
1331 reduce(&t
, num_p
, pden
, options
);
1332 free_evalue_refs(&t
);
1334 free_evalue_refs(&f
);
1336 ie_cum
cum(factor
, E_num(0, dim
), r
);
1337 cum
.cumulate(options
);
1339 int common
= pden
.NumRows();
1341 for (int j
= 0; j
< cum
.terms
.size(); ++j
) {
1343 pden
.SetDims(rows
, pden
.NumCols());
1344 for (int k
= 0; k
< r
->dim
; ++k
) {
1345 int n
= cum
.terms
[j
]->powers
[k
];
1348 pden
.SetDims(rows
+n
, pden
.NumCols());
1349 for (int l
= 0; l
< n
; ++l
)
1350 pden
[rows
+l
] = den_r
[k
];
1353 reduce(cum
.terms
[j
]->E
, num_p
, pden
, options
);
1354 free_evalue_refs(cum
.terms
[j
]->E
);
1355 delete cum
.terms
[j
]->E
;
1356 delete cum
.terms
[j
];
1363 static int type_offset(enode
*p
)
1365 return p
->type
== fractional
? 1 :
1366 p
->type
== flooring
? 1 : 0;
1369 static int edegree(evalue
*e
)
1374 if (value_notzero_p(e
->d
))
1378 int i
= type_offset(p
);
1379 if (p
->size
-i
-1 > d
)
1380 d
= p
->size
- i
- 1;
1381 for (; i
< p
->size
; i
++) {
1382 int d2
= edegree(&p
->arr
[i
]);
1389 void ienumerator::handle(const signed_cone
& sc
, barvinok_options
*options
)
1391 assert(sc
.det
== 1);
1393 assert(sc
.rays
.NumRows() == dim
);
1395 lattice_point(V
, sc
.rays
, vertex
[0], E_vertex
, options
);
1401 evalue_set_si(&one
, sc
.sign
, 1);
1402 reduce(&one
, vertex
, den
, options
);
1403 free_evalue_refs(&one
);
1405 for (int i
= 0; i
< dim
; ++i
)
1407 free_evalue_refs(E_vertex
[i
]);
1412 struct bfenumerator
: public vertex_decomposer
, public bf_base
,
1413 public ienumerator_base
{
1416 bfenumerator(Polyhedron
*P
, unsigned dim
, unsigned nbV
) :
1417 vertex_decomposer(P
, nbV
, *this),
1418 bf_base(dim
), ienumerator_base(dim
, this) {
1426 virtual void handle(const signed_cone
& sc
, barvinok_options
*options
);
1427 virtual void base(mat_ZZ
& factors
, bfc_vec
& v
);
1429 bfc_term_base
* new_bf_term(int len
) {
1430 bfe_term
* t
= new bfe_term(len
);
1434 virtual void set_factor(bfc_term_base
*t
, int k
, int change
) {
1435 bfe_term
* bfet
= static_cast<bfe_term
*>(t
);
1436 factor
= bfet
->factors
[k
];
1437 assert(factor
!= NULL
);
1438 bfet
->factors
[k
] = NULL
;
1440 emul(&mone
, factor
);
1443 virtual void set_factor(bfc_term_base
*t
, int k
, mpq_t
&q
, int change
) {
1444 bfe_term
* bfet
= static_cast<bfe_term
*>(t
);
1445 factor
= bfet
->factors
[k
];
1446 assert(factor
!= NULL
);
1447 bfet
->factors
[k
] = NULL
;
1453 value_oppose(f
.x
.n
, mpq_numref(q
));
1455 value_assign(f
.x
.n
, mpq_numref(q
));
1456 value_assign(f
.d
, mpq_denref(q
));
1458 free_evalue_refs(&f
);
1461 virtual void set_factor(bfc_term_base
*t
, int k
, const QQ
& c
, int change
) {
1462 bfe_term
* bfet
= static_cast<bfe_term
*>(t
);
1464 factor
= new evalue
;
1469 zz2value(c
.n
, f
.x
.n
);
1471 value_oppose(f
.x
.n
, f
.x
.n
);
1474 value_init(factor
->d
);
1475 evalue_copy(factor
, bfet
->factors
[k
]);
1477 free_evalue_refs(&f
);
1480 void set_factor(evalue
*f
, int change
) {
1486 virtual void insert_term(bfc_term_base
*t
, int i
) {
1487 bfe_term
* bfet
= static_cast<bfe_term
*>(t
);
1488 int len
= t
->terms
.NumRows()-1; // already increased by one
1490 bfet
->factors
.resize(len
+1);
1491 for (int j
= len
; j
> i
; --j
) {
1492 bfet
->factors
[j
] = bfet
->factors
[j
-1];
1493 t
->terms
[j
] = t
->terms
[j
-1];
1495 bfet
->factors
[i
] = factor
;
1499 virtual void update_term(bfc_term_base
*t
, int i
) {
1500 bfe_term
* bfet
= static_cast<bfe_term
*>(t
);
1502 eadd(factor
, bfet
->factors
[i
]);
1503 free_evalue_refs(factor
);
1507 virtual bool constant_vertex(int dim
) { return E_num(0, dim
) == 0; }
1509 virtual void cum(bf_reducer
*bfr
, bfc_term_base
*t
, int k
, dpoly_r
*r
,
1510 barvinok_options
*options
);
1513 enumerator_base
*enumerator_base::create(Polyhedron
*P
, unsigned dim
, unsigned nbV
,
1514 barvinok_options
*options
)
1516 enumerator_base
*eb
;
1518 if (options
->incremental_specialization
== BV_SPECIALIZATION_BF
)
1519 eb
= new bfenumerator(P
, dim
, nbV
);
1520 else if (options
->incremental_specialization
== BV_SPECIALIZATION_DF
)
1521 eb
= new ienumerator(P
, dim
, nbV
);
1523 eb
= new enumerator(P
, dim
, nbV
);
1528 struct bfe_cum
: public cumulator
{
1530 bfc_term_base
*told
;
1534 bfe_cum(evalue
*factor
, evalue
*v
, dpoly_r
*r
, bf_reducer
*bfr
,
1535 bfc_term_base
*t
, int k
, bfenumerator
*e
) :
1536 cumulator(factor
, v
, r
), told(t
), k(k
),
1540 virtual void add_term(const vector
<int>& powers
, evalue
*f2
);
1543 void bfe_cum::add_term(const vector
<int>& powers
, evalue
*f2
)
1545 bfr
->update_powers(powers
);
1547 bfc_term_base
* t
= bfe
->find_bfc_term(bfr
->vn
, bfr
->npowers
, bfr
->nnf
);
1548 bfe
->set_factor(f2
, bfr
->l_changes
% 2);
1549 bfe
->add_term(t
, told
->terms
[k
], bfr
->l_extra_num
);
1552 void bfenumerator::cum(bf_reducer
*bfr
, bfc_term_base
*t
, int k
,
1553 dpoly_r
*r
, barvinok_options
*options
)
1555 bfe_term
* bfet
= static_cast<bfe_term
*>(t
);
1556 bfe_cum
cum(bfet
->factors
[k
], E_num(0, bfr
->d
), r
, bfr
, t
, k
, this);
1557 cum
.cumulate(options
);
1560 void bfenumerator::base(mat_ZZ
& factors
, bfc_vec
& v
)
1562 for (int i
= 0; i
< v
.size(); ++i
) {
1563 assert(v
[i
]->terms
.NumRows() == 1);
1564 evalue
*factor
= static_cast<bfe_term
*>(v
[i
])->factors
[0];
1565 eadd(factor
, vE
[vert
]);
1570 void bfenumerator::handle(const signed_cone
& sc
, barvinok_options
*options
)
1572 assert(sc
.det
== 1);
1574 assert(sc
.rays
.NumRows() == enumerator_base::dim
);
1576 bfe_term
* t
= new bfe_term(enumerator_base::dim
);
1577 vector
< bfc_term_base
* > v
;
1580 t
->factors
.resize(1);
1582 t
->terms
.SetDims(1, enumerator_base::dim
);
1583 lattice_point(V
, sc
.rays
, t
->terms
[0], E_vertex
, options
);
1585 // the elements of factors are always lexpositive
1587 int s
= setup_factors(sc
.rays
, factors
, t
, sc
.sign
);
1589 t
->factors
[0] = new evalue
;
1590 value_init(t
->factors
[0]->d
);
1591 evalue_set_si(t
->factors
[0], s
, 1);
1592 reduce(factors
, v
, options
);
1594 for (int i
= 0; i
< enumerator_base::dim
; ++i
)
1596 free_evalue_refs(E_vertex
[i
]);
1601 #ifdef HAVE_CORRECT_VERTICES
1602 static inline Param_Polyhedron
*Polyhedron2Param_SD(Polyhedron
**Din
,
1603 Polyhedron
*Cin
,int WS
,Polyhedron
**CEq
,Matrix
**CT
)
1605 if (WS
& POL_NO_DUAL
)
1607 return Polyhedron2Param_SimplifiedDomain(Din
, Cin
, WS
, CEq
, CT
);
1610 static Param_Polyhedron
*Polyhedron2Param_SD(Polyhedron
**Din
,
1611 Polyhedron
*Cin
,int WS
,Polyhedron
**CEq
,Matrix
**CT
)
1613 static char data
[] = " 1 0 0 0 0 1 -18 "
1614 " 1 0 0 -20 0 19 1 "
1615 " 1 0 1 20 0 -20 16 "
1618 " 1 4 -20 0 0 -1 23 "
1619 " 1 -4 20 0 0 1 -22 "
1620 " 1 0 1 0 20 -20 16 "
1621 " 1 0 0 0 -20 19 1 ";
1622 static int checked
= 0;
1627 Matrix
*M
= Matrix_Alloc(9, 7);
1628 for (i
= 0; i
< 9; ++i
)
1629 for (int j
= 0; j
< 7; ++j
) {
1630 sscanf(p
, "%d%n", &v
, &n
);
1632 value_set_si(M
->p
[i
][j
], v
);
1634 Polyhedron
*P
= Constraints2Polyhedron(M
, 1024);
1636 Polyhedron
*U
= Universe_Polyhedron(1);
1637 Param_Polyhedron
*PP
= Polyhedron2Param_Domain(P
, U
, 1024);
1641 for (i
= 0, V
= PP
->V
; V
; ++i
, V
= V
->next
)
1644 Param_Polyhedron_Free(PP
);
1646 fprintf(stderr
, "WARNING: results may be incorrect\n");
1648 "WARNING: use latest version of PolyLib to remove this warning\n");
1652 return Polyhedron2Param_SimplifiedDomain(Din
, Cin
, WS
, CEq
, CT
);
1656 static evalue
* barvinok_enumerate_ev_f(Polyhedron
*P
, Polyhedron
* C
,
1657 barvinok_options
*options
);
1660 static evalue
* barvinok_enumerate_cst(Polyhedron
*P
, Polyhedron
* C
,
1661 struct barvinok_options
*options
)
1665 ALLOC(evalue
, eres
);
1666 value_init(eres
->d
);
1667 value_set_si(eres
->d
, 0);
1668 eres
->x
.p
= new_enode(partition
, 2, C
->Dimension
);
1669 EVALUE_SET_DOMAIN(eres
->x
.p
->arr
[0],
1670 DomainConstraintSimplify(C
, options
->MaxRays
));
1671 value_set_si(eres
->x
.p
->arr
[1].d
, 1);
1672 value_init(eres
->x
.p
->arr
[1].x
.n
);
1674 value_set_si(eres
->x
.p
->arr
[1].x
.n
, 0);
1676 barvinok_count_with_options(P
, &eres
->x
.p
->arr
[1].x
.n
, options
);
1681 evalue
* barvinok_enumerate_with_options(Polyhedron
*P
, Polyhedron
* C
,
1682 struct barvinok_options
*options
)
1684 //P = unfringe(P, MaxRays);
1685 Polyhedron
*next
, *Cnext
;
1686 Polyhedron
*Corig
= C
;
1687 Polyhedron
*Porig
= P
;
1688 Polyhedron
*CEq
= NULL
, *rVD
, *CA
;
1690 unsigned nparam
= C
->Dimension
;
1695 "barvinok_enumerate: input is a union; only first polyhedron is enumerated\n");
1699 "barvinok_enumerate: context is a union; only first polyhedron is considered\n");
1702 value_init(factor
.d
);
1703 evalue_set_si(&factor
, 1, 1);
1707 CA
= align_context(C
, P
->Dimension
, options
->MaxRays
);
1710 P
= DomainIntersection(P
, CA
, options
->MaxRays
);
1712 Polyhedron_Free(CA
);
1715 POL_ENSURE_FACETS(P
);
1716 POL_ENSURE_VERTICES(P
);
1717 POL_ENSURE_FACETS(C
);
1718 POL_ENSURE_VERTICES(C
);
1720 if (C
->Dimension
== 0 || emptyQ(P
)) {
1722 eres
= barvinok_enumerate_cst(P
, CEq
? CEq
: Polyhedron_Copy(C
), options
);
1724 emul(&factor
, eres
);
1725 if (options
->polynomial_approximation
== BV_POLAPPROX_UPPER
)
1726 evalue_frac2polynomial(eres
, 1, options
->MaxRays
);
1727 if (options
->polynomial_approximation
== BV_POLAPPROX_LOWER
)
1728 evalue_frac2polynomial(eres
, 0, options
->MaxRays
);
1729 reduce_evalue(eres
);
1730 free_evalue_refs(&factor
);
1735 Corig
->next
= Cnext
;
1738 if (Polyhedron_is_infinite_param(P
, nparam
))
1743 P
= remove_equalities_p(P
, P
->Dimension
-nparam
, &f
);
1744 mask(f
, &factor
, options
);
1747 if (P
->Dimension
== nparam
) {
1749 P
= Universe_Polyhedron(0);
1753 Polyhedron
*T
= Polyhedron_Factor(P
, nparam
, options
->MaxRays
);
1754 if (T
|| (P
->Dimension
== nparam
+1)) {
1757 for (Q
= T
? T
: P
; Q
; Q
= Q
->next
) {
1758 Polyhedron
*next
= Q
->next
;
1762 if (Q
->Dimension
!= C
->Dimension
)
1763 QC
= Polyhedron_Project(Q
, nparam
);
1766 C
= DomainIntersection(C
, QC
, options
->MaxRays
);
1768 Polyhedron_Free(C2
);
1770 Polyhedron_Free(QC
);
1778 if (T
->Dimension
== C
->Dimension
) {
1787 eres
= barvinok_enumerate_ev_f(P
, C
, options
);
1794 for (Q
= P
->next
; Q
; Q
= Q
->next
) {
1795 Polyhedron
*next
= Q
->next
;
1798 f
= barvinok_enumerate_ev_f(Q
, C
, options
);
1800 free_evalue_refs(f
);
1810 evalue
* barvinok_enumerate_ev(Polyhedron
*P
, Polyhedron
* C
, unsigned MaxRays
)
1813 barvinok_options
*options
= barvinok_options_new_with_defaults();
1814 options
->MaxRays
= MaxRays
;
1815 E
= barvinok_enumerate_with_options(P
, C
, options
);
1816 barvinok_options_free(options
);
1820 /* adapted from mpolyhedron_inflate in PolyLib */
1821 static Polyhedron
*Polyhedron_Inflate(Polyhedron
*P
, unsigned nparam
,
1825 int nvar
= P
->Dimension
- nparam
;
1826 Matrix
*C
= Polyhedron2Constraints(P
);
1830 /* subtract the sum of the negative coefficients of each inequality */
1831 for (int i
= 0; i
< C
->NbRows
; ++i
) {
1832 value_set_si(sum
, 0);
1833 for (int j
= 0; j
< nvar
; ++j
)
1834 if (value_neg_p(C
->p
[i
][1+j
]))
1835 value_addto(sum
, sum
, C
->p
[i
][1+j
]);
1836 value_subtract(C
->p
[i
][1+P
->Dimension
], C
->p
[i
][1+P
->Dimension
], sum
);
1839 P2
= Constraints2Polyhedron(C
, MaxRays
);
1844 /* adapted from mpolyhedron_deflate in PolyLib */
1845 static Polyhedron
*Polyhedron_Deflate(Polyhedron
*P
, unsigned nparam
,
1849 int nvar
= P
->Dimension
- nparam
;
1850 Matrix
*C
= Polyhedron2Constraints(P
);
1854 /* subtract the sum of the positive coefficients of each inequality */
1855 for (int i
= 0; i
< C
->NbRows
; ++i
) {
1856 value_set_si(sum
, 0);
1857 for (int j
= 0; j
< nvar
; ++j
)
1858 if (value_pos_p(C
->p
[i
][1+j
]))
1859 value_addto(sum
, sum
, C
->p
[i
][1+j
]);
1860 value_subtract(C
->p
[i
][1+P
->Dimension
], C
->p
[i
][1+P
->Dimension
], sum
);
1863 P2
= Constraints2Polyhedron(C
, MaxRays
);
1868 static evalue
* barvinok_enumerate_ev_f(Polyhedron
*P
, Polyhedron
* C
,
1869 barvinok_options
*options
)
1871 unsigned nparam
= C
->Dimension
;
1872 bool pre_approx
= options
->polynomial_approximation
>= BV_POLAPPROX_PRE_LOWER
&&
1873 options
->polynomial_approximation
<= BV_POLAPPROX_PRE_APPROX
;
1875 if (P
->Dimension
- nparam
== 1 && !pre_approx
)
1876 return ParamLine_Length(P
, C
, options
);
1878 Param_Polyhedron
*PP
= NULL
;
1879 Polyhedron
*CEq
= NULL
, *pVD
;
1881 Param_Domain
*D
, *next
;
1884 Polyhedron
*Porig
= P
;
1888 if (options
->polynomial_approximation
== BV_POLAPPROX_PRE_UPPER
)
1889 P
= Polyhedron_Inflate(P
, nparam
, options
->MaxRays
);
1890 if (options
->polynomial_approximation
== BV_POLAPPROX_PRE_LOWER
)
1891 P
= Polyhedron_Deflate(P
, nparam
, options
->MaxRays
);
1894 PP
= Polyhedron2Param_SD(&T
, C
, options
->MaxRays
, &CEq
, &CT
);
1895 if (T
!= P
&& P
!= Porig
)
1899 if (isIdentity(CT
)) {
1903 assert(CT
->NbRows
!= CT
->NbColumns
);
1904 if (CT
->NbRows
== 1) { // no more parameters
1905 eres
= barvinok_enumerate_cst(P
, CEq
, options
);
1910 Param_Polyhedron_Free(PP
);
1916 nparam
= CT
->NbRows
- 1;
1922 Param_Polyhedron_Scale_Integer(PP
, &T
, &det
, options
->MaxRays
);
1928 unsigned dim
= P
->Dimension
- nparam
;
1930 ALLOC(evalue
, eres
);
1931 value_init(eres
->d
);
1932 value_set_si(eres
->d
, 0);
1935 for (nd
= 0, D
=PP
->D
; D
; ++nd
, D
=D
->next
);
1936 struct section
{ Polyhedron
*D
; evalue E
; };
1937 section
*s
= new section
[nd
];
1938 Polyhedron
**fVD
= new Polyhedron_p
[nd
];
1940 enumerator_base
*et
= NULL
;
1945 et
= enumerator_base::create(P
, dim
, PP
->nbV
, options
);
1947 for(nd
= 0, D
=PP
->D
; D
; D
=next
) {
1950 Polyhedron
*rVD
= reduce_domain(D
->Domain
, CT
, CEq
, fVD
, nd
, options
);
1954 pVD
= CT
? DomainImage(rVD
,CT
,options
->MaxRays
) : rVD
;
1956 value_init(s
[nd
].E
.d
);
1957 evalue_set_si(&s
[nd
].E
, 0, 1);
1960 FORALL_PVertex_in_ParamPolyhedron(V
,D
,PP
) // _i is internal counter
1963 et
->decompose_at(V
, _i
, options
);
1964 } catch (OrthogonalException
&e
) {
1967 for (; nd
>= 0; --nd
) {
1968 free_evalue_refs(&s
[nd
].E
);
1969 Domain_Free(s
[nd
].D
);
1970 Domain_Free(fVD
[nd
]);
1974 eadd(et
->vE
[_i
] , &s
[nd
].E
);
1975 END_FORALL_PVertex_in_ParamPolyhedron
;
1976 evalue_range_reduction_in_domain(&s
[nd
].E
, pVD
);
1979 addeliminatedparams_evalue(&s
[nd
].E
, CT
);
1987 evalue_set_si(eres
, 0, 1);
1989 eres
->x
.p
= new_enode(partition
, 2*nd
, C
->Dimension
);
1990 for (int j
= 0; j
< nd
; ++j
) {
1991 EVALUE_SET_DOMAIN(eres
->x
.p
->arr
[2*j
], s
[j
].D
);
1992 value_clear(eres
->x
.p
->arr
[2*j
+1].d
);
1993 eres
->x
.p
->arr
[2*j
+1] = s
[j
].E
;
1994 Domain_Free(fVD
[j
]);
2001 evalue_div(eres
, det
);
2006 Polyhedron_Free(CEq
);
2010 Enumeration
* barvinok_enumerate(Polyhedron
*P
, Polyhedron
* C
, unsigned MaxRays
)
2012 evalue
*EP
= barvinok_enumerate_ev(P
, C
, MaxRays
);
2014 return partition2enumeration(EP
);
2017 static void SwapColumns(Value
**V
, int n
, int i
, int j
)
2019 for (int r
= 0; r
< n
; ++r
)
2020 value_swap(V
[r
][i
], V
[r
][j
]);
2023 static void SwapColumns(Polyhedron
*P
, int i
, int j
)
2025 SwapColumns(P
->Constraint
, P
->NbConstraints
, i
, j
);
2026 SwapColumns(P
->Ray
, P
->NbRays
, i
, j
);
2029 /* Construct a constraint c from constraints l and u such that if
2030 * if constraint c holds then for each value of the other variables
2031 * there is at most one value of variable pos (position pos+1 in the constraints).
2033 * Given a lower and an upper bound
2034 * n_l v_i + <c_l,x> + c_l >= 0
2035 * -n_u v_i + <c_u,x> + c_u >= 0
2036 * the constructed constraint is
2038 * -(n_l<c_u,x> + n_u<c_l,x>) + (-n_l c_u - n_u c_l + n_l n_u - 1)
2040 * which is then simplified to remove the content of the non-constant coefficients
2042 * len is the total length of the constraints.
2043 * v is a temporary variable that can be used by this procedure
2045 static void negative_test_constraint(Value
*l
, Value
*u
, Value
*c
, int pos
,
2048 value_oppose(*v
, u
[pos
+1]);
2049 Vector_Combine(l
+1, u
+1, c
+1, *v
, l
[pos
+1], len
-1);
2050 value_multiply(*v
, *v
, l
[pos
+1]);
2051 value_subtract(c
[len
-1], c
[len
-1], *v
);
2052 value_set_si(*v
, -1);
2053 Vector_Scale(c
+1, c
+1, *v
, len
-1);
2054 value_decrement(c
[len
-1], c
[len
-1]);
2055 ConstraintSimplify(c
, c
, len
, v
);
2058 static bool parallel_constraints(Value
*l
, Value
*u
, Value
*c
, int pos
,
2067 Vector_Gcd(&l
[1+pos
], len
, &g1
);
2068 Vector_Gcd(&u
[1+pos
], len
, &g2
);
2069 Vector_Combine(l
+1+pos
, u
+1+pos
, c
+1, g2
, g1
, len
);
2070 parallel
= First_Non_Zero(c
+1, len
) == -1;
2078 static void negative_test_constraint7(Value
*l
, Value
*u
, Value
*c
, int pos
,
2079 int exist
, int len
, Value
*v
)
2084 Vector_Gcd(&u
[1+pos
], exist
, v
);
2085 Vector_Gcd(&l
[1+pos
], exist
, &g
);
2086 Vector_Combine(l
+1, u
+1, c
+1, *v
, g
, len
-1);
2087 value_multiply(*v
, *v
, g
);
2088 value_subtract(c
[len
-1], c
[len
-1], *v
);
2089 value_set_si(*v
, -1);
2090 Vector_Scale(c
+1, c
+1, *v
, len
-1);
2091 value_decrement(c
[len
-1], c
[len
-1]);
2092 ConstraintSimplify(c
, c
, len
, v
);
2097 /* Turns a x + b >= 0 into a x + b <= -1
2099 * len is the total length of the constraint.
2100 * v is a temporary variable that can be used by this procedure
2102 static void oppose_constraint(Value
*c
, int len
, Value
*v
)
2104 value_set_si(*v
, -1);
2105 Vector_Scale(c
+1, c
+1, *v
, len
-1);
2106 value_decrement(c
[len
-1], c
[len
-1]);
2109 /* Split polyhedron P into two polyhedra *pos and *neg, where
2110 * existential variable i has at most one solution for each
2111 * value of the other variables in *neg.
2113 * The splitting is performed using constraints l and u.
2115 * nvar: number of set variables
2116 * row: temporary vector that can be used by this procedure
2117 * f: temporary value that can be used by this procedure
2119 static bool SplitOnConstraint(Polyhedron
*P
, int i
, int l
, int u
,
2120 int nvar
, int MaxRays
, Vector
*row
, Value
& f
,
2121 Polyhedron
**pos
, Polyhedron
**neg
)
2123 negative_test_constraint(P
->Constraint
[l
], P
->Constraint
[u
],
2124 row
->p
, nvar
+i
, P
->Dimension
+2, &f
);
2125 *neg
= AddConstraints(row
->p
, 1, P
, MaxRays
);
2127 /* We found an independent, but useless constraint
2128 * Maybe we should detect this earlier and not
2129 * mark the variable as INDEPENDENT
2131 if (emptyQ((*neg
))) {
2132 Polyhedron_Free(*neg
);
2136 oppose_constraint(row
->p
, P
->Dimension
+2, &f
);
2137 *pos
= AddConstraints(row
->p
, 1, P
, MaxRays
);
2139 if (emptyQ((*pos
))) {
2140 Polyhedron_Free(*neg
);
2141 Polyhedron_Free(*pos
);
2149 * unimodularly transform P such that constraint r is transformed
2150 * into a constraint that involves only a single (the first)
2151 * existential variable
2154 static Polyhedron
*rotate_along(Polyhedron
*P
, int r
, int nvar
, int exist
,
2160 Vector
*row
= Vector_Alloc(exist
);
2161 Vector_Copy(P
->Constraint
[r
]+1+nvar
, row
->p
, exist
);
2162 Vector_Gcd(row
->p
, exist
, &g
);
2163 if (value_notone_p(g
))
2164 Vector_AntiScale(row
->p
, row
->p
, g
, exist
);
2167 Matrix
*M
= unimodular_complete(row
);
2168 Matrix
*M2
= Matrix_Alloc(P
->Dimension
+1, P
->Dimension
+1);
2169 for (r
= 0; r
< nvar
; ++r
)
2170 value_set_si(M2
->p
[r
][r
], 1);
2171 for ( ; r
< nvar
+exist
; ++r
)
2172 Vector_Copy(M
->p
[r
-nvar
], M2
->p
[r
]+nvar
, exist
);
2173 for ( ; r
< P
->Dimension
+1; ++r
)
2174 value_set_si(M2
->p
[r
][r
], 1);
2175 Polyhedron
*T
= Polyhedron_Image(P
, M2
, MaxRays
);
2184 /* Split polyhedron P into two polyhedra *pos and *neg, where
2185 * existential variable i has at most one solution for each
2186 * value of the other variables in *neg.
2188 * If independent is set, then the two constraints on which the
2189 * split will be performed need to be independent of the other
2190 * existential variables.
2192 * Return true if an appropriate split could be performed.
2194 * nvar: number of set variables
2195 * exist: number of existential variables
2196 * row: temporary vector that can be used by this procedure
2197 * f: temporary value that can be used by this procedure
2199 static bool SplitOnVar(Polyhedron
*P
, int i
,
2200 int nvar
, int exist
, int MaxRays
,
2201 Vector
*row
, Value
& f
, bool independent
,
2202 Polyhedron
**pos
, Polyhedron
**neg
)
2206 for (int l
= P
->NbEq
; l
< P
->NbConstraints
; ++l
) {
2207 if (value_negz_p(P
->Constraint
[l
][nvar
+i
+1]))
2211 for (j
= 0; j
< exist
; ++j
)
2212 if (j
!= i
&& value_notzero_p(P
->Constraint
[l
][nvar
+j
+1]))
2218 for (int u
= P
->NbEq
; u
< P
->NbConstraints
; ++u
) {
2219 if (value_posz_p(P
->Constraint
[u
][nvar
+i
+1]))
2223 for (j
= 0; j
< exist
; ++j
)
2224 if (j
!= i
&& value_notzero_p(P
->Constraint
[u
][nvar
+j
+1]))
2230 if (SplitOnConstraint(P
, i
, l
, u
, nvar
, MaxRays
, row
, f
, pos
, neg
)) {
2233 SwapColumns(*neg
, nvar
+1, nvar
+1+i
);
2243 static bool double_bound_pair(Polyhedron
*P
, int nvar
, int exist
,
2244 int i
, int l1
, int l2
,
2245 Polyhedron
**pos
, Polyhedron
**neg
)
2249 Vector
*row
= Vector_Alloc(P
->Dimension
+2);
2250 value_set_si(row
->p
[0], 1);
2251 value_oppose(f
, P
->Constraint
[l1
][nvar
+i
+1]);
2252 Vector_Combine(P
->Constraint
[l1
]+1, P
->Constraint
[l2
]+1,
2254 P
->Constraint
[l2
][nvar
+i
+1], f
,
2256 ConstraintSimplify(row
->p
, row
->p
, P
->Dimension
+2, &f
);
2257 *pos
= AddConstraints(row
->p
, 1, P
, 0);
2258 value_set_si(f
, -1);
2259 Vector_Scale(row
->p
+1, row
->p
+1, f
, P
->Dimension
+1);
2260 value_decrement(row
->p
[P
->Dimension
+1], row
->p
[P
->Dimension
+1]);
2261 *neg
= AddConstraints(row
->p
, 1, P
, 0);
2265 return !emptyQ((*pos
)) && !emptyQ((*neg
));
2268 static bool double_bound(Polyhedron
*P
, int nvar
, int exist
,
2269 Polyhedron
**pos
, Polyhedron
**neg
)
2271 for (int i
= 0; i
< exist
; ++i
) {
2273 for (l1
= P
->NbEq
; l1
< P
->NbConstraints
; ++l1
) {
2274 if (value_negz_p(P
->Constraint
[l1
][nvar
+i
+1]))
2276 for (l2
= l1
+ 1; l2
< P
->NbConstraints
; ++l2
) {
2277 if (value_negz_p(P
->Constraint
[l2
][nvar
+i
+1]))
2279 if (double_bound_pair(P
, nvar
, exist
, i
, l1
, l2
, pos
, neg
))
2283 for (l1
= P
->NbEq
; l1
< P
->NbConstraints
; ++l1
) {
2284 if (value_posz_p(P
->Constraint
[l1
][nvar
+i
+1]))
2286 if (l1
< P
->NbConstraints
)
2287 for (l2
= l1
+ 1; l2
< P
->NbConstraints
; ++l2
) {
2288 if (value_posz_p(P
->Constraint
[l2
][nvar
+i
+1]))
2290 if (double_bound_pair(P
, nvar
, exist
, i
, l1
, l2
, pos
, neg
))
2302 INDEPENDENT
= 1 << 2,
2306 static evalue
* enumerate_or(Polyhedron
*D
,
2307 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2310 fprintf(stderr
, "\nER: Or\n");
2311 #endif /* DEBUG_ER */
2313 Polyhedron
*N
= D
->next
;
2316 barvinok_enumerate_e_with_options(D
, exist
, nparam
, options
);
2319 for (D
= N
; D
; D
= N
) {
2324 barvinok_enumerate_e_with_options(D
, exist
, nparam
, options
);
2327 free_evalue_refs(EN
);
2337 static evalue
* enumerate_sum(Polyhedron
*P
,
2338 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2340 int nvar
= P
->Dimension
- exist
- nparam
;
2341 int toswap
= nvar
< exist
? nvar
: exist
;
2342 for (int i
= 0; i
< toswap
; ++i
)
2343 SwapColumns(P
, 1 + i
, nvar
+exist
- i
);
2347 fprintf(stderr
, "\nER: Sum\n");
2348 #endif /* DEBUG_ER */
2350 evalue
*EP
= barvinok_enumerate_e_with_options(P
, exist
, nparam
, options
);
2352 evalue_split_domains_into_orthants(EP
, options
->MaxRays
);
2354 evalue_range_reduction(EP
);
2356 evalue_frac2floor2(EP
, 1);
2358 evalue
*sum
= esum(EP
, nvar
);
2360 free_evalue_refs(EP
);
2364 evalue_range_reduction(EP
);
2369 static evalue
* split_sure(Polyhedron
*P
, Polyhedron
*S
,
2370 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2372 int nvar
= P
->Dimension
- exist
- nparam
;
2374 Matrix
*M
= Matrix_Alloc(exist
, S
->Dimension
+2);
2375 for (int i
= 0; i
< exist
; ++i
)
2376 value_set_si(M
->p
[i
][nvar
+i
+1], 1);
2378 S
= DomainAddRays(S
, M
, options
->MaxRays
);
2380 Polyhedron
*F
= DomainAddRays(P
, M
, options
->MaxRays
);
2381 Polyhedron
*D
= DomainDifference(F
, S
, options
->MaxRays
);
2383 D
= Disjoint_Domain(D
, 0, options
->MaxRays
);
2388 M
= Matrix_Alloc(P
->Dimension
+1-exist
, P
->Dimension
+1);
2389 for (int j
= 0; j
< nvar
; ++j
)
2390 value_set_si(M
->p
[j
][j
], 1);
2391 for (int j
= 0; j
< nparam
+1; ++j
)
2392 value_set_si(M
->p
[nvar
+j
][nvar
+exist
+j
], 1);
2393 Polyhedron
*T
= Polyhedron_Image(S
, M
, options
->MaxRays
);
2394 evalue
*EP
= barvinok_enumerate_e_with_options(T
, 0, nparam
, options
);
2399 for (Polyhedron
*Q
= D
; Q
; Q
= Q
->next
) {
2400 Polyhedron
*N
= Q
->next
;
2402 T
= DomainIntersection(P
, Q
, options
->MaxRays
);
2403 evalue
*E
= barvinok_enumerate_e_with_options(T
, exist
, nparam
, options
);
2405 free_evalue_refs(E
);
2414 static evalue
* enumerate_sure(Polyhedron
*P
,
2415 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2419 int nvar
= P
->Dimension
- exist
- nparam
;
2425 for (i
= 0; i
< exist
; ++i
) {
2426 Matrix
*M
= Matrix_Alloc(S
->NbConstraints
, S
->Dimension
+2);
2428 value_set_si(lcm
, 1);
2429 for (int j
= 0; j
< S
->NbConstraints
; ++j
) {
2430 if (value_negz_p(S
->Constraint
[j
][1+nvar
+i
]))
2432 if (value_one_p(S
->Constraint
[j
][1+nvar
+i
]))
2434 value_lcm(lcm
, S
->Constraint
[j
][1+nvar
+i
], &lcm
);
2437 for (int j
= 0; j
< S
->NbConstraints
; ++j
) {
2438 if (value_negz_p(S
->Constraint
[j
][1+nvar
+i
]))
2440 if (value_one_p(S
->Constraint
[j
][1+nvar
+i
]))
2442 value_division(f
, lcm
, S
->Constraint
[j
][1+nvar
+i
]);
2443 Vector_Scale(S
->Constraint
[j
], M
->p
[c
], f
, S
->Dimension
+2);
2444 value_subtract(M
->p
[c
][S
->Dimension
+1],
2445 M
->p
[c
][S
->Dimension
+1],
2447 value_increment(M
->p
[c
][S
->Dimension
+1],
2448 M
->p
[c
][S
->Dimension
+1]);
2452 S
= AddConstraints(M
->p
[0], c
, S
, options
->MaxRays
);
2467 fprintf(stderr
, "\nER: Sure\n");
2468 #endif /* DEBUG_ER */
2470 return split_sure(P
, S
, exist
, nparam
, options
);
2473 static evalue
* enumerate_sure2(Polyhedron
*P
,
2474 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2476 int nvar
= P
->Dimension
- exist
- nparam
;
2478 for (r
= 0; r
< P
->NbRays
; ++r
)
2479 if (value_one_p(P
->Ray
[r
][0]) &&
2480 value_one_p(P
->Ray
[r
][P
->Dimension
+1]))
2486 Matrix
*M
= Matrix_Alloc(nvar
+ 1 + nparam
, P
->Dimension
+2);
2487 for (int i
= 0; i
< nvar
; ++i
)
2488 value_set_si(M
->p
[i
][1+i
], 1);
2489 for (int i
= 0; i
< nparam
; ++i
)
2490 value_set_si(M
->p
[i
+nvar
][1+nvar
+exist
+i
], 1);
2491 Vector_Copy(P
->Ray
[r
]+1+nvar
, M
->p
[nvar
+nparam
]+1+nvar
, exist
);
2492 value_set_si(M
->p
[nvar
+nparam
][0], 1);
2493 value_set_si(M
->p
[nvar
+nparam
][P
->Dimension
+1], 1);
2494 Polyhedron
* F
= Rays2Polyhedron(M
, options
->MaxRays
);
2497 Polyhedron
*I
= DomainIntersection(F
, P
, options
->MaxRays
);
2501 fprintf(stderr
, "\nER: Sure2\n");
2502 #endif /* DEBUG_ER */
2504 return split_sure(P
, I
, exist
, nparam
, options
);
2507 static evalue
* enumerate_cyclic(Polyhedron
*P
,
2508 unsigned exist
, unsigned nparam
,
2509 evalue
* EP
, int r
, int p
, unsigned MaxRays
)
2511 int nvar
= P
->Dimension
- exist
- nparam
;
2513 /* If EP in its fractional maps only contains references
2514 * to the remainder parameter with appropriate coefficients
2515 * then we could in principle avoid adding existentially
2516 * quantified variables to the validity domains.
2517 * We'd have to replace the remainder by m { p/m }
2518 * and multiply with an appropriate factor that is one
2519 * only in the appropriate range.
2520 * This last multiplication can be avoided if EP
2521 * has a single validity domain with no (further)
2522 * constraints on the remainder parameter
2525 Matrix
*CT
= Matrix_Alloc(nparam
+1, nparam
+3);
2526 Matrix
*M
= Matrix_Alloc(1, 1+nparam
+3);
2527 for (int j
= 0; j
< nparam
; ++j
)
2529 value_set_si(CT
->p
[j
][j
], 1);
2530 value_set_si(CT
->p
[p
][nparam
+1], 1);
2531 value_set_si(CT
->p
[nparam
][nparam
+2], 1);
2532 value_set_si(M
->p
[0][1+p
], -1);
2533 value_absolute(M
->p
[0][1+nparam
], P
->Ray
[0][1+nvar
+exist
+p
]);
2534 value_set_si(M
->p
[0][1+nparam
+1], 1);
2535 Polyhedron
*CEq
= Constraints2Polyhedron(M
, 1);
2537 addeliminatedparams_enum(EP
, CT
, CEq
, MaxRays
, nparam
);
2538 Polyhedron_Free(CEq
);
2544 static void enumerate_vd_add_ray(evalue
*EP
, Matrix
*Rays
, unsigned MaxRays
)
2546 if (value_notzero_p(EP
->d
))
2549 assert(EP
->x
.p
->type
== partition
);
2550 assert(EP
->x
.p
->pos
== EVALUE_DOMAIN(EP
->x
.p
->arr
[0])->Dimension
);
2551 for (int i
= 0; i
< EP
->x
.p
->size
/2; ++i
) {
2552 Polyhedron
*D
= EVALUE_DOMAIN(EP
->x
.p
->arr
[2*i
]);
2553 Polyhedron
*N
= DomainAddRays(D
, Rays
, MaxRays
);
2554 EVALUE_SET_DOMAIN(EP
->x
.p
->arr
[2*i
], N
);
2559 static evalue
* enumerate_line(Polyhedron
*P
,
2560 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2566 fprintf(stderr
, "\nER: Line\n");
2567 #endif /* DEBUG_ER */
2569 int nvar
= P
->Dimension
- exist
- nparam
;
2571 for (i
= 0; i
< nparam
; ++i
)
2572 if (value_notzero_p(P
->Ray
[0][1+nvar
+exist
+i
]))
2575 for (j
= i
+1; j
< nparam
; ++j
)
2576 if (value_notzero_p(P
->Ray
[0][1+nvar
+exist
+i
]))
2578 assert(j
>= nparam
); // for now
2580 Matrix
*M
= Matrix_Alloc(2, P
->Dimension
+2);
2581 value_set_si(M
->p
[0][0], 1);
2582 value_set_si(M
->p
[0][1+nvar
+exist
+i
], 1);
2583 value_set_si(M
->p
[1][0], 1);
2584 value_set_si(M
->p
[1][1+nvar
+exist
+i
], -1);
2585 value_absolute(M
->p
[1][1+P
->Dimension
], P
->Ray
[0][1+nvar
+exist
+i
]);
2586 value_decrement(M
->p
[1][1+P
->Dimension
], M
->p
[1][1+P
->Dimension
]);
2587 Polyhedron
*S
= AddConstraints(M
->p
[0], 2, P
, options
->MaxRays
);
2588 evalue
*EP
= barvinok_enumerate_e_with_options(S
, exist
, nparam
, options
);
2592 return enumerate_cyclic(P
, exist
, nparam
, EP
, 0, i
, options
->MaxRays
);
2595 static int single_param_pos(Polyhedron
*P
, unsigned exist
, unsigned nparam
,
2598 int nvar
= P
->Dimension
- exist
- nparam
;
2599 if (First_Non_Zero(P
->Ray
[r
]+1, nvar
) != -1)
2601 int i
= First_Non_Zero(P
->Ray
[r
]+1+nvar
+exist
, nparam
);
2604 if (First_Non_Zero(P
->Ray
[r
]+1+nvar
+exist
+1, nparam
-i
-1) != -1)
2609 static evalue
* enumerate_remove_ray(Polyhedron
*P
, int r
,
2610 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2613 fprintf(stderr
, "\nER: RedundantRay\n");
2614 #endif /* DEBUG_ER */
2618 value_set_si(one
, 1);
2619 int len
= P
->NbRays
-1;
2620 Matrix
*M
= Matrix_Alloc(2 * len
, P
->Dimension
+2);
2621 Vector_Copy(P
->Ray
[0], M
->p
[0], r
* (P
->Dimension
+2));
2622 Vector_Copy(P
->Ray
[r
+1], M
->p
[r
], (len
-r
) * (P
->Dimension
+2));
2623 for (int j
= 0; j
< P
->NbRays
; ++j
) {
2626 Vector_Combine(P
->Ray
[j
], P
->Ray
[r
], M
->p
[len
+j
-(j
>r
)],
2627 one
, P
->Ray
[j
][P
->Dimension
+1], P
->Dimension
+2);
2630 P
= Rays2Polyhedron(M
, options
->MaxRays
);
2632 evalue
*EP
= barvinok_enumerate_e_with_options(P
, exist
, nparam
, options
);
2639 static evalue
* enumerate_redundant_ray(Polyhedron
*P
,
2640 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2642 assert(P
->NbBid
== 0);
2643 int nvar
= P
->Dimension
- exist
- nparam
;
2647 for (int r
= 0; r
< P
->NbRays
; ++r
) {
2648 if (value_notzero_p(P
->Ray
[r
][P
->Dimension
+1]))
2650 int i1
= single_param_pos(P
, exist
, nparam
, r
);
2653 for (int r2
= r
+1; r2
< P
->NbRays
; ++r2
) {
2654 if (value_notzero_p(P
->Ray
[r2
][P
->Dimension
+1]))
2656 int i2
= single_param_pos(P
, exist
, nparam
, r2
);
2662 value_division(m
, P
->Ray
[r
][1+nvar
+exist
+i1
],
2663 P
->Ray
[r2
][1+nvar
+exist
+i1
]);
2664 value_multiply(m
, m
, P
->Ray
[r2
][1+nvar
+exist
+i1
]);
2665 /* r2 divides r => r redundant */
2666 if (value_eq(m
, P
->Ray
[r
][1+nvar
+exist
+i1
])) {
2668 return enumerate_remove_ray(P
, r
, exist
, nparam
, options
);
2671 value_division(m
, P
->Ray
[r2
][1+nvar
+exist
+i1
],
2672 P
->Ray
[r
][1+nvar
+exist
+i1
]);
2673 value_multiply(m
, m
, P
->Ray
[r
][1+nvar
+exist
+i1
]);
2674 /* r divides r2 => r2 redundant */
2675 if (value_eq(m
, P
->Ray
[r2
][1+nvar
+exist
+i1
])) {
2677 return enumerate_remove_ray(P
, r2
, exist
, nparam
, options
);
2685 static Polyhedron
*upper_bound(Polyhedron
*P
,
2686 int pos
, Value
*max
, Polyhedron
**R
)
2695 for (Polyhedron
*Q
= P
; Q
; Q
= N
) {
2697 for (r
= 0; r
< P
->NbRays
; ++r
) {
2698 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1]) &&
2699 value_pos_p(P
->Ray
[r
][1+pos
]))
2702 if (r
< P
->NbRays
) {
2710 for (r
= 0; r
< P
->NbRays
; ++r
) {
2711 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1]))
2713 mpz_fdiv_q(v
, P
->Ray
[r
][1+pos
], P
->Ray
[r
][1+P
->Dimension
]);
2714 if ((!Q
->next
&& r
== 0) || value_gt(v
, *max
))
2715 value_assign(*max
, v
);
2722 static evalue
* enumerate_ray(Polyhedron
*P
,
2723 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2725 assert(P
->NbBid
== 0);
2726 int nvar
= P
->Dimension
- exist
- nparam
;
2729 for (r
= 0; r
< P
->NbRays
; ++r
)
2730 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1]))
2736 for (r2
= r
+1; r2
< P
->NbRays
; ++r2
)
2737 if (value_zero_p(P
->Ray
[r2
][P
->Dimension
+1]))
2739 if (r2
< P
->NbRays
) {
2741 return enumerate_sum(P
, exist
, nparam
, options
);
2745 fprintf(stderr
, "\nER: Ray\n");
2746 #endif /* DEBUG_ER */
2752 value_set_si(one
, 1);
2753 int i
= single_param_pos(P
, exist
, nparam
, r
);
2754 assert(i
!= -1); // for now;
2756 Matrix
*M
= Matrix_Alloc(P
->NbRays
, P
->Dimension
+2);
2757 for (int j
= 0; j
< P
->NbRays
; ++j
) {
2758 Vector_Combine(P
->Ray
[j
], P
->Ray
[r
], M
->p
[j
],
2759 one
, P
->Ray
[j
][P
->Dimension
+1], P
->Dimension
+2);
2761 Polyhedron
*S
= Rays2Polyhedron(M
, options
->MaxRays
);
2763 Polyhedron
*D
= DomainDifference(P
, S
, options
->MaxRays
);
2765 // Polyhedron_Print(stderr, P_VALUE_FMT, D);
2766 assert(value_pos_p(P
->Ray
[r
][1+nvar
+exist
+i
])); // for now
2768 D
= upper_bound(D
, nvar
+exist
+i
, &m
, &R
);
2772 M
= Matrix_Alloc(2, P
->Dimension
+2);
2773 value_set_si(M
->p
[0][0], 1);
2774 value_set_si(M
->p
[1][0], 1);
2775 value_set_si(M
->p
[0][1+nvar
+exist
+i
], -1);
2776 value_set_si(M
->p
[1][1+nvar
+exist
+i
], 1);
2777 value_assign(M
->p
[0][1+P
->Dimension
], m
);
2778 value_oppose(M
->p
[1][1+P
->Dimension
], m
);
2779 value_addto(M
->p
[1][1+P
->Dimension
], M
->p
[1][1+P
->Dimension
],
2780 P
->Ray
[r
][1+nvar
+exist
+i
]);
2781 value_decrement(M
->p
[1][1+P
->Dimension
], M
->p
[1][1+P
->Dimension
]);
2782 // Matrix_Print(stderr, P_VALUE_FMT, M);
2783 D
= AddConstraints(M
->p
[0], 2, P
, options
->MaxRays
);
2784 // Polyhedron_Print(stderr, P_VALUE_FMT, D);
2785 value_subtract(M
->p
[0][1+P
->Dimension
], M
->p
[0][1+P
->Dimension
],
2786 P
->Ray
[r
][1+nvar
+exist
+i
]);
2787 // Matrix_Print(stderr, P_VALUE_FMT, M);
2788 S
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
2789 // Polyhedron_Print(stderr, P_VALUE_FMT, S);
2792 evalue
*EP
= barvinok_enumerate_e_with_options(D
, exist
, nparam
, options
);
2797 if (value_notone_p(P
->Ray
[r
][1+nvar
+exist
+i
]))
2798 EP
= enumerate_cyclic(P
, exist
, nparam
, EP
, r
, i
, options
->MaxRays
);
2800 M
= Matrix_Alloc(1, nparam
+2);
2801 value_set_si(M
->p
[0][0], 1);
2802 value_set_si(M
->p
[0][1+i
], 1);
2803 enumerate_vd_add_ray(EP
, M
, options
->MaxRays
);
2808 evalue
*E
= barvinok_enumerate_e_with_options(S
, exist
, nparam
, options
);
2810 free_evalue_refs(E
);
2817 evalue
*ER
= enumerate_or(R
, exist
, nparam
, options
);
2819 free_evalue_refs(ER
);
2826 static evalue
* enumerate_vd(Polyhedron
**PA
,
2827 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2829 Polyhedron
*P
= *PA
;
2830 int nvar
= P
->Dimension
- exist
- nparam
;
2831 Param_Polyhedron
*PP
= NULL
;
2832 Polyhedron
*C
= Universe_Polyhedron(nparam
);
2836 PP
= Polyhedron2Param_SimplifiedDomain(&PR
,C
, options
->MaxRays
,&CEq
,&CT
);
2840 Param_Domain
*D
, *last
;
2843 for (nd
= 0, D
=PP
->D
; D
; D
=D
->next
, ++nd
)
2846 Polyhedron
**VD
= new Polyhedron_p
[nd
];
2847 Polyhedron
**fVD
= new Polyhedron_p
[nd
];
2848 for(nd
= 0, D
=PP
->D
; D
; D
=D
->next
) {
2849 Polyhedron
*rVD
= reduce_domain(D
->Domain
, CT
, CEq
, fVD
, nd
, options
);
2862 /* This doesn't seem to have any effect */
2864 Polyhedron
*CA
= align_context(VD
[0], P
->Dimension
, options
->MaxRays
);
2866 P
= DomainIntersection(P
, CA
, options
->MaxRays
);
2869 Polyhedron_Free(CA
);
2874 if (!EP
&& CT
->NbColumns
!= CT
->NbRows
) {
2875 Polyhedron
*CEqr
= DomainImage(CEq
, CT
, options
->MaxRays
);
2876 Polyhedron
*CA
= align_context(CEqr
, PR
->Dimension
, options
->MaxRays
);
2877 Polyhedron
*I
= DomainIntersection(PR
, CA
, options
->MaxRays
);
2878 Polyhedron_Free(CEqr
);
2879 Polyhedron_Free(CA
);
2881 fprintf(stderr
, "\nER: Eliminate\n");
2882 #endif /* DEBUG_ER */
2883 nparam
-= CT
->NbColumns
- CT
->NbRows
;
2884 EP
= barvinok_enumerate_e_with_options(I
, exist
, nparam
, options
);
2885 nparam
+= CT
->NbColumns
- CT
->NbRows
;
2886 addeliminatedparams_enum(EP
, CT
, CEq
, options
->MaxRays
, nparam
);
2890 Polyhedron_Free(PR
);
2893 if (!EP
&& nd
> 1) {
2895 fprintf(stderr
, "\nER: VD\n");
2896 #endif /* DEBUG_ER */
2897 for (int i
= 0; i
< nd
; ++i
) {
2898 Polyhedron
*CA
= align_context(VD
[i
], P
->Dimension
, options
->MaxRays
);
2899 Polyhedron
*I
= DomainIntersection(P
, CA
, options
->MaxRays
);
2902 EP
= barvinok_enumerate_e_with_options(I
, exist
, nparam
, options
);
2904 evalue
*E
= barvinok_enumerate_e_with_options(I
, exist
, nparam
,
2907 free_evalue_refs(E
);
2911 Polyhedron_Free(CA
);
2915 for (int i
= 0; i
< nd
; ++i
) {
2916 Polyhedron_Free(VD
[i
]);
2917 Polyhedron_Free(fVD
[i
]);
2923 if (!EP
&& nvar
== 0) {
2926 Param_Vertices
*V
, *V2
;
2927 Matrix
* M
= Matrix_Alloc(1, P
->Dimension
+2);
2929 FORALL_PVertex_in_ParamPolyhedron(V
, last
, PP
) {
2931 FORALL_PVertex_in_ParamPolyhedron(V2
, last
, PP
) {
2938 for (int i
= 0; i
< exist
; ++i
) {
2939 value_oppose(f
, V
->Vertex
->p
[i
][nparam
+1]);
2940 Vector_Combine(V
->Vertex
->p
[i
],
2942 M
->p
[0] + 1 + nvar
+ exist
,
2943 V2
->Vertex
->p
[i
][nparam
+1],
2947 for (j
= 0; j
< nparam
; ++j
)
2948 if (value_notzero_p(M
->p
[0][1+nvar
+exist
+j
]))
2952 ConstraintSimplify(M
->p
[0], M
->p
[0],
2953 P
->Dimension
+2, &f
);
2954 value_set_si(M
->p
[0][0], 0);
2955 Polyhedron
*para
= AddConstraints(M
->p
[0], 1, P
,
2958 Polyhedron_Free(para
);
2961 Polyhedron
*pos
, *neg
;
2962 value_set_si(M
->p
[0][0], 1);
2963 value_decrement(M
->p
[0][P
->Dimension
+1],
2964 M
->p
[0][P
->Dimension
+1]);
2965 neg
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
2966 value_set_si(f
, -1);
2967 Vector_Scale(M
->p
[0]+1, M
->p
[0]+1, f
,
2969 value_decrement(M
->p
[0][P
->Dimension
+1],
2970 M
->p
[0][P
->Dimension
+1]);
2971 value_decrement(M
->p
[0][P
->Dimension
+1],
2972 M
->p
[0][P
->Dimension
+1]);
2973 pos
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
2974 if (emptyQ(neg
) && emptyQ(pos
)) {
2975 Polyhedron_Free(para
);
2976 Polyhedron_Free(pos
);
2977 Polyhedron_Free(neg
);
2981 fprintf(stderr
, "\nER: Order\n");
2982 #endif /* DEBUG_ER */
2983 EP
= barvinok_enumerate_e_with_options(para
, exist
, nparam
,
2987 E
= barvinok_enumerate_e_with_options(pos
, exist
, nparam
,
2990 free_evalue_refs(E
);
2994 E
= barvinok_enumerate_e_with_options(neg
, exist
, nparam
,
2997 free_evalue_refs(E
);
3000 Polyhedron_Free(para
);
3001 Polyhedron_Free(pos
);
3002 Polyhedron_Free(neg
);
3007 } END_FORALL_PVertex_in_ParamPolyhedron
;
3010 } END_FORALL_PVertex_in_ParamPolyhedron
;
3013 /* Search for vertex coordinate to split on */
3014 /* First look for one independent of the parameters */
3015 FORALL_PVertex_in_ParamPolyhedron(V
, last
, PP
) {
3016 for (int i
= 0; i
< exist
; ++i
) {
3018 for (j
= 0; j
< nparam
; ++j
)
3019 if (value_notzero_p(V
->Vertex
->p
[i
][j
]))
3023 value_set_si(M
->p
[0][0], 1);
3024 Vector_Set(M
->p
[0]+1, 0, nvar
+exist
);
3025 Vector_Copy(V
->Vertex
->p
[i
],
3026 M
->p
[0] + 1 + nvar
+ exist
, nparam
+1);
3027 value_oppose(M
->p
[0][1+nvar
+i
],
3028 V
->Vertex
->p
[i
][nparam
+1]);
3030 Polyhedron
*pos
, *neg
;
3031 value_set_si(M
->p
[0][0], 1);
3032 value_decrement(M
->p
[0][P
->Dimension
+1],
3033 M
->p
[0][P
->Dimension
+1]);
3034 neg
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
3035 value_set_si(f
, -1);
3036 Vector_Scale(M
->p
[0]+1, M
->p
[0]+1, f
,
3038 value_decrement(M
->p
[0][P
->Dimension
+1],
3039 M
->p
[0][P
->Dimension
+1]);
3040 value_decrement(M
->p
[0][P
->Dimension
+1],
3041 M
->p
[0][P
->Dimension
+1]);
3042 pos
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
3043 if (emptyQ(neg
) || emptyQ(pos
)) {
3044 Polyhedron_Free(pos
);
3045 Polyhedron_Free(neg
);
3048 Polyhedron_Free(pos
);
3049 value_increment(M
->p
[0][P
->Dimension
+1],
3050 M
->p
[0][P
->Dimension
+1]);
3051 pos
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
3053 fprintf(stderr
, "\nER: Vertex\n");
3054 #endif /* DEBUG_ER */
3056 EP
= enumerate_or(pos
, exist
, nparam
, options
);
3061 } END_FORALL_PVertex_in_ParamPolyhedron
;
3065 /* Search for vertex coordinate to split on */
3066 /* Now look for one that depends on the parameters */
3067 FORALL_PVertex_in_ParamPolyhedron(V
, last
, PP
) {
3068 for (int i
= 0; i
< exist
; ++i
) {
3069 value_set_si(M
->p
[0][0], 1);
3070 Vector_Set(M
->p
[0]+1, 0, nvar
+exist
);
3071 Vector_Copy(V
->Vertex
->p
[i
],
3072 M
->p
[0] + 1 + nvar
+ exist
, nparam
+1);
3073 value_oppose(M
->p
[0][1+nvar
+i
],
3074 V
->Vertex
->p
[i
][nparam
+1]);
3076 Polyhedron
*pos
, *neg
;
3077 value_set_si(M
->p
[0][0], 1);
3078 value_decrement(M
->p
[0][P
->Dimension
+1],
3079 M
->p
[0][P
->Dimension
+1]);
3080 neg
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
3081 value_set_si(f
, -1);
3082 Vector_Scale(M
->p
[0]+1, M
->p
[0]+1, f
,
3084 value_decrement(M
->p
[0][P
->Dimension
+1],
3085 M
->p
[0][P
->Dimension
+1]);
3086 value_decrement(M
->p
[0][P
->Dimension
+1],
3087 M
->p
[0][P
->Dimension
+1]);
3088 pos
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
3089 if (emptyQ(neg
) || emptyQ(pos
)) {
3090 Polyhedron_Free(pos
);
3091 Polyhedron_Free(neg
);
3094 Polyhedron_Free(pos
);
3095 value_increment(M
->p
[0][P
->Dimension
+1],
3096 M
->p
[0][P
->Dimension
+1]);
3097 pos
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
3099 fprintf(stderr
, "\nER: ParamVertex\n");
3100 #endif /* DEBUG_ER */
3102 EP
= enumerate_or(pos
, exist
, nparam
, options
);
3107 } END_FORALL_PVertex_in_ParamPolyhedron
;
3115 Polyhedron_Free(CEq
);
3119 Param_Polyhedron_Free(PP
);
3125 evalue
* barvinok_enumerate_pip(Polyhedron
*P
, unsigned exist
, unsigned nparam
,
3129 barvinok_options
*options
= barvinok_options_new_with_defaults();
3130 options
->MaxRays
= MaxRays
;
3131 E
= barvinok_enumerate_pip_with_options(P
, exist
, nparam
, options
);
3132 barvinok_options_free(options
);
3137 evalue
*barvinok_enumerate_pip_with_options(Polyhedron
*P
,
3138 unsigned exist
, unsigned nparam
, struct barvinok_options
*options
)
3143 evalue
*barvinok_enumerate_pip_with_options(Polyhedron
*P
,
3144 unsigned exist
, unsigned nparam
, struct barvinok_options
*options
)
3146 int nvar
= P
->Dimension
- exist
- nparam
;
3147 evalue
*EP
= evalue_zero();
3151 fprintf(stderr
, "\nER: PIP\n");
3152 #endif /* DEBUG_ER */
3154 Polyhedron
*D
= pip_projectout(P
, nvar
, exist
, nparam
);
3155 for (Q
= D
; Q
; Q
= N
) {
3159 exist
= Q
->Dimension
- nvar
- nparam
;
3160 E
= barvinok_enumerate_e_with_options(Q
, exist
, nparam
, options
);
3163 free_evalue_refs(E
);
3172 static bool is_single(Value
*row
, int pos
, int len
)
3174 return First_Non_Zero(row
, pos
) == -1 &&
3175 First_Non_Zero(row
+pos
+1, len
-pos
-1) == -1;
3178 static evalue
* barvinok_enumerate_e_r(Polyhedron
*P
,
3179 unsigned exist
, unsigned nparam
, barvinok_options
*options
);
3182 static int er_level
= 0;
3184 evalue
* barvinok_enumerate_e_with_options(Polyhedron
*P
,
3185 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
3187 fprintf(stderr
, "\nER: level %i\n", er_level
);
3189 Polyhedron_PrintConstraints(stderr
, P_VALUE_FMT
, P
);
3190 fprintf(stderr
, "\nE %d\nP %d\n", exist
, nparam
);
3192 P
= DomainConstraintSimplify(Polyhedron_Copy(P
), options
->MaxRays
);
3193 evalue
*EP
= barvinok_enumerate_e_r(P
, exist
, nparam
, options
);
3199 evalue
* barvinok_enumerate_e_with_options(Polyhedron
*P
,
3200 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
3202 P
= DomainConstraintSimplify(Polyhedron_Copy(P
), options
->MaxRays
);
3203 evalue
*EP
= barvinok_enumerate_e_r(P
, exist
, nparam
, options
);
3209 evalue
* barvinok_enumerate_e(Polyhedron
*P
, unsigned exist
, unsigned nparam
,
3213 barvinok_options
*options
= barvinok_options_new_with_defaults();
3214 options
->MaxRays
= MaxRays
;
3215 E
= barvinok_enumerate_e_with_options(P
, exist
, nparam
, options
);
3216 barvinok_options_free(options
);
3220 static evalue
* barvinok_enumerate_e_r(Polyhedron
*P
,
3221 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
3224 Polyhedron
*U
= Universe_Polyhedron(nparam
);
3225 evalue
*EP
= barvinok_enumerate_with_options(P
, U
, options
);
3226 //char *param_name[] = {"P", "Q", "R", "S", "T" };
3227 //print_evalue(stdout, EP, param_name);
3232 int nvar
= P
->Dimension
- exist
- nparam
;
3233 int len
= P
->Dimension
+ 2;
3236 POL_ENSURE_FACETS(P
);
3237 POL_ENSURE_VERTICES(P
);
3240 return evalue_zero();
3242 if (nvar
== 0 && nparam
== 0) {
3243 evalue
*EP
= evalue_zero();
3244 barvinok_count_with_options(P
, &EP
->x
.n
, options
);
3245 if (value_pos_p(EP
->x
.n
))
3246 value_set_si(EP
->x
.n
, 1);
3251 for (r
= 0; r
< P
->NbRays
; ++r
)
3252 if (value_zero_p(P
->Ray
[r
][0]) ||
3253 value_zero_p(P
->Ray
[r
][P
->Dimension
+1])) {
3255 for (i
= 0; i
< nvar
; ++i
)
3256 if (value_notzero_p(P
->Ray
[r
][i
+1]))
3260 for (i
= nvar
+ exist
; i
< nvar
+ exist
+ nparam
; ++i
)
3261 if (value_notzero_p(P
->Ray
[r
][i
+1]))
3263 if (i
>= nvar
+ exist
+ nparam
)
3266 if (r
< P
->NbRays
) {
3267 evalue
*EP
= evalue_zero();
3268 value_set_si(EP
->x
.n
, -1);
3273 for (r
= 0; r
< P
->NbEq
; ++r
)
3274 if ((first
= First_Non_Zero(P
->Constraint
[r
]+1+nvar
, exist
)) != -1)
3277 if (First_Non_Zero(P
->Constraint
[r
]+1+nvar
+first
+1,
3278 exist
-first
-1) != -1) {
3279 Polyhedron
*T
= rotate_along(P
, r
, nvar
, exist
, options
->MaxRays
);
3281 fprintf(stderr
, "\nER: Equality\n");
3282 #endif /* DEBUG_ER */
3283 evalue
*EP
= barvinok_enumerate_e_with_options(T
, exist
-1, nparam
,
3289 fprintf(stderr
, "\nER: Fixed\n");
3290 #endif /* DEBUG_ER */
3292 return barvinok_enumerate_e_with_options(P
, exist
-1, nparam
,
3295 Polyhedron
*T
= Polyhedron_Copy(P
);
3296 SwapColumns(T
, nvar
+1, nvar
+1+first
);
3297 evalue
*EP
= barvinok_enumerate_e_with_options(T
, exist
-1, nparam
,
3305 Vector
*row
= Vector_Alloc(len
);
3306 value_set_si(row
->p
[0], 1);
3311 enum constraint
* info
= new constraint
[exist
];
3312 for (int i
= 0; i
< exist
; ++i
) {
3314 for (int l
= P
->NbEq
; l
< P
->NbConstraints
; ++l
) {
3315 if (value_negz_p(P
->Constraint
[l
][nvar
+i
+1]))
3317 bool l_parallel
= is_single(P
->Constraint
[l
]+nvar
+1, i
, exist
);
3318 for (int u
= P
->NbEq
; u
< P
->NbConstraints
; ++u
) {
3319 if (value_posz_p(P
->Constraint
[u
][nvar
+i
+1]))
3321 bool lu_parallel
= l_parallel
||
3322 is_single(P
->Constraint
[u
]+nvar
+1, i
, exist
);
3323 value_oppose(f
, P
->Constraint
[u
][nvar
+i
+1]);
3324 Vector_Combine(P
->Constraint
[l
]+1, P
->Constraint
[u
]+1, row
->p
+1,
3325 f
, P
->Constraint
[l
][nvar
+i
+1], len
-1);
3326 if (!(info
[i
] & INDEPENDENT
)) {
3328 for (j
= 0; j
< exist
; ++j
)
3329 if (j
!= i
&& value_notzero_p(row
->p
[nvar
+j
+1]))
3332 //printf("independent: i: %d, l: %d, u: %d\n", i, l, u);
3333 info
[i
] = (constraint
)(info
[i
] | INDEPENDENT
);
3336 if (info
[i
] & ALL_POS
) {
3337 value_addto(row
->p
[len
-1], row
->p
[len
-1],
3338 P
->Constraint
[l
][nvar
+i
+1]);
3339 value_addto(row
->p
[len
-1], row
->p
[len
-1], f
);
3340 value_multiply(f
, f
, P
->Constraint
[l
][nvar
+i
+1]);
3341 value_subtract(row
->p
[len
-1], row
->p
[len
-1], f
);
3342 value_decrement(row
->p
[len
-1], row
->p
[len
-1]);
3343 ConstraintSimplify(row
->p
, row
->p
, len
, &f
);
3344 value_set_si(f
, -1);
3345 Vector_Scale(row
->p
+1, row
->p
+1, f
, len
-1);
3346 value_decrement(row
->p
[len
-1], row
->p
[len
-1]);
3347 Polyhedron
*T
= AddConstraints(row
->p
, 1, P
, options
->MaxRays
);
3349 //printf("not all_pos: i: %d, l: %d, u: %d\n", i, l, u);
3350 info
[i
] = (constraint
)(info
[i
] ^ ALL_POS
);
3352 //puts("pos remainder");
3353 //Polyhedron_Print(stdout, P_VALUE_FMT, T);
3356 if (!(info
[i
] & ONE_NEG
)) {
3358 negative_test_constraint(P
->Constraint
[l
],
3360 row
->p
, nvar
+i
, len
, &f
);
3361 oppose_constraint(row
->p
, len
, &f
);
3362 Polyhedron
*T
= AddConstraints(row
->p
, 1, P
,
3365 //printf("one_neg i: %d, l: %d, u: %d\n", i, l, u);
3366 info
[i
] = (constraint
)(info
[i
] | ONE_NEG
);
3368 //puts("neg remainder");
3369 //Polyhedron_Print(stdout, P_VALUE_FMT, T);
3371 } else if (!(info
[i
] & ROT_NEG
)) {
3372 if (parallel_constraints(P
->Constraint
[l
],
3374 row
->p
, nvar
, exist
)) {
3375 negative_test_constraint7(P
->Constraint
[l
],
3377 row
->p
, nvar
, exist
,
3379 oppose_constraint(row
->p
, len
, &f
);
3380 Polyhedron
*T
= AddConstraints(row
->p
, 1, P
,
3383 // printf("rot_neg i: %d, l: %d, u: %d\n", i, l, u);
3384 info
[i
] = (constraint
)(info
[i
] | ROT_NEG
);
3387 //puts("neg remainder");
3388 //Polyhedron_Print(stdout, P_VALUE_FMT, T);
3393 if (!(info
[i
] & ALL_POS
) && (info
[i
] & (ONE_NEG
| ROT_NEG
)))
3397 if (info
[i
] & ALL_POS
)
3404 for (int i = 0; i < exist; ++i)
3405 printf("%i: %i\n", i, info[i]);
3407 for (int i
= 0; i
< exist
; ++i
)
3408 if (info
[i
] & ALL_POS
) {
3410 fprintf(stderr
, "\nER: Positive\n");
3411 #endif /* DEBUG_ER */
3413 // Maybe we should chew off some of the fat here
3414 Matrix
*M
= Matrix_Alloc(P
->Dimension
, P
->Dimension
+1);
3415 for (int j
= 0; j
< P
->Dimension
; ++j
)
3416 value_set_si(M
->p
[j
][j
+ (j
>= i
+nvar
)], 1);
3417 Polyhedron
*T
= Polyhedron_Image(P
, M
, options
->MaxRays
);
3419 evalue
*EP
= barvinok_enumerate_e_with_options(T
, exist
-1, nparam
,
3427 for (int i
= 0; i
< exist
; ++i
)
3428 if (info
[i
] & ONE_NEG
) {
3430 fprintf(stderr
, "\nER: Negative\n");
3431 #endif /* DEBUG_ER */
3436 return barvinok_enumerate_e_with_options(P
, exist
-1, nparam
,
3439 Polyhedron
*T
= Polyhedron_Copy(P
);
3440 SwapColumns(T
, nvar
+1, nvar
+1+i
);
3441 evalue
*EP
= barvinok_enumerate_e_with_options(T
, exist
-1, nparam
,
3447 for (int i
= 0; i
< exist
; ++i
)
3448 if (info
[i
] & ROT_NEG
) {
3450 fprintf(stderr
, "\nER: Rotate\n");
3451 #endif /* DEBUG_ER */
3455 Polyhedron
*T
= rotate_along(P
, r
, nvar
, exist
, options
->MaxRays
);
3456 evalue
*EP
= barvinok_enumerate_e_with_options(T
, exist
-1, nparam
,
3461 for (int i
= 0; i
< exist
; ++i
)
3462 if (info
[i
] & INDEPENDENT
) {
3463 Polyhedron
*pos
, *neg
;
3465 /* Find constraint again and split off negative part */
3467 if (SplitOnVar(P
, i
, nvar
, exist
, options
->MaxRays
,
3468 row
, f
, true, &pos
, &neg
)) {
3470 fprintf(stderr
, "\nER: Split\n");
3471 #endif /* DEBUG_ER */
3474 barvinok_enumerate_e_with_options(neg
, exist
-1, nparam
, options
);
3476 barvinok_enumerate_e_with_options(pos
, exist
, nparam
, options
);
3478 free_evalue_refs(E
);
3480 Polyhedron_Free(neg
);
3481 Polyhedron_Free(pos
);
3495 EP
= enumerate_line(P
, exist
, nparam
, options
);
3499 EP
= barvinok_enumerate_pip_with_options(P
, exist
, nparam
, options
);
3503 EP
= enumerate_redundant_ray(P
, exist
, nparam
, options
);
3507 EP
= enumerate_sure(P
, exist
, nparam
, options
);
3511 EP
= enumerate_ray(P
, exist
, nparam
, options
);
3515 EP
= enumerate_sure2(P
, exist
, nparam
, options
);
3519 F
= unfringe(P
, options
->MaxRays
);
3520 if (!PolyhedronIncludes(F
, P
)) {
3522 fprintf(stderr
, "\nER: Fringed\n");
3523 #endif /* DEBUG_ER */
3524 EP
= barvinok_enumerate_e_with_options(F
, exist
, nparam
, options
);
3531 EP
= enumerate_vd(&P
, exist
, nparam
, options
);
3536 EP
= enumerate_sum(P
, exist
, nparam
, options
);
3543 Polyhedron
*pos
, *neg
;
3544 for (i
= 0; i
< exist
; ++i
)
3545 if (SplitOnVar(P
, i
, nvar
, exist
, options
->MaxRays
,
3546 row
, f
, false, &pos
, &neg
))
3552 EP
= enumerate_or(pos
, exist
, nparam
, options
);
3565 * remove equalities that require a "compression" of the parameters
3567 static Polyhedron
*remove_more_equalities(Polyhedron
*P
, unsigned nparam
,
3568 Matrix
**CP
, unsigned MaxRays
)
3571 remove_all_equalities(&P
, NULL
, CP
, NULL
, nparam
, MaxRays
);
3578 static gen_fun
*series(Polyhedron
*P
, unsigned nparam
, barvinok_options
*options
)
3588 assert(!Polyhedron_is_infinite_param(P
, nparam
));
3589 assert(P
->NbBid
== 0);
3590 assert(Polyhedron_has_revlex_positive_rays(P
, nparam
));
3592 P
= remove_more_equalities(P
, nparam
, &CP
, options
->MaxRays
);
3593 assert(P
->NbEq
== 0);
3595 nparam
= CP
->NbColumns
-1;
3600 barvinok_count_with_options(P
, &c
, options
);
3601 gf
= new gen_fun(c
);
3605 red
= gf_base::create(Polyhedron_Project(P
, nparam
),
3606 P
->Dimension
, nparam
, options
);
3607 POL_ENSURE_VERTICES(P
);
3608 red
->start_gf(P
, options
);
3620 gen_fun
* barvinok_series_with_options(Polyhedron
*P
, Polyhedron
* C
,
3621 barvinok_options
*options
)
3624 unsigned nparam
= C
->Dimension
;
3627 CA
= align_context(C
, P
->Dimension
, options
->MaxRays
);
3628 P
= DomainIntersection(P
, CA
, options
->MaxRays
);
3629 Polyhedron_Free(CA
);
3631 gf
= series(P
, nparam
, options
);
3636 gen_fun
* barvinok_series(Polyhedron
*P
, Polyhedron
* C
, unsigned MaxRays
)
3639 barvinok_options
*options
= barvinok_options_new_with_defaults();
3640 options
->MaxRays
= MaxRays
;
3641 gf
= barvinok_series_with_options(P
, C
, options
);
3642 barvinok_options_free(options
);
3646 static Polyhedron
*skew_into_positive_orthant(Polyhedron
*D
, unsigned nparam
,
3652 for (Polyhedron
*P
= D
; P
; P
= P
->next
) {
3653 POL_ENSURE_VERTICES(P
);
3654 assert(!Polyhedron_is_infinite_param(P
, nparam
));
3655 assert(P
->NbBid
== 0);
3656 assert(Polyhedron_has_positive_rays(P
, nparam
));
3658 for (int r
= 0; r
< P
->NbRays
; ++r
) {
3659 if (value_notzero_p(P
->Ray
[r
][P
->Dimension
+1]))
3661 for (int i
= 0; i
< nparam
; ++i
) {
3663 if (value_posz_p(P
->Ray
[r
][i
+1]))
3666 M
= Matrix_Alloc(D
->Dimension
+1, D
->Dimension
+1);
3667 for (int i
= 0; i
< D
->Dimension
+1; ++i
)
3668 value_set_si(M
->p
[i
][i
], 1);
3670 Inner_Product(P
->Ray
[r
]+1, M
->p
[i
], D
->Dimension
+1, &tmp
);
3671 if (value_posz_p(tmp
))
3674 for (j
= P
->Dimension
- nparam
; j
< P
->Dimension
; ++j
)
3675 if (value_pos_p(P
->Ray
[r
][j
+1]))
3677 assert(j
< P
->Dimension
);
3678 value_pdivision(tmp
, P
->Ray
[r
][j
+1], P
->Ray
[r
][i
+1]);
3679 value_subtract(M
->p
[i
][j
], M
->p
[i
][j
], tmp
);
3685 D
= DomainImage(D
, M
, MaxRays
);
3691 gen_fun
* barvinok_enumerate_union_series_with_options(Polyhedron
*D
, Polyhedron
* C
,
3692 barvinok_options
*options
)
3694 Polyhedron
*conv
, *D2
;
3696 gen_fun
*gf
= NULL
, *gf2
;
3697 unsigned nparam
= C
->Dimension
;
3702 CA
= align_context(C
, D
->Dimension
, options
->MaxRays
);
3703 D
= DomainIntersection(D
, CA
, options
->MaxRays
);
3704 Polyhedron_Free(CA
);
3706 D2
= skew_into_positive_orthant(D
, nparam
, options
->MaxRays
);
3707 for (Polyhedron
*P
= D2
; P
; P
= P
->next
) {
3708 assert(P
->Dimension
== D2
->Dimension
);
3711 P_gf
= series(Polyhedron_Copy(P
), nparam
, options
);
3715 gf
->add_union(P_gf
, options
);
3719 /* we actually only need the convex union of the parameter space
3720 * but the reducer classes currently expect a polyhedron in
3721 * the combined space
3723 Polyhedron_Free(gf
->context
);
3724 gf
->context
= DomainConvex(D2
, options
->MaxRays
);
3726 gf2
= gf
->summate(D2
->Dimension
- nparam
, options
);
3735 gen_fun
* barvinok_enumerate_union_series(Polyhedron
*D
, Polyhedron
* C
,
3739 barvinok_options
*options
= barvinok_options_new_with_defaults();
3740 options
->MaxRays
= MaxRays
;
3741 gf
= barvinok_enumerate_union_series_with_options(D
, C
, options
);
3742 barvinok_options_free(options
);
3746 evalue
* barvinok_enumerate_union(Polyhedron
*D
, Polyhedron
* C
, unsigned MaxRays
)
3749 gen_fun
*gf
= barvinok_enumerate_union_series(D
, C
, MaxRays
);