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, NULL
, 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 /* Check whether all rays point in the positive directions
894 static bool Polyhedron_has_positive_rays(Polyhedron
*P
, unsigned nparam
)
897 for (r
= 0; r
< P
->NbRays
; ++r
)
898 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1])) {
900 for (i
= P
->Dimension
- nparam
; i
< P
->Dimension
; ++i
)
901 if (value_neg_p(P
->Ray
[r
][i
+1]))
907 typedef evalue
* evalue_p
;
909 struct enumerator_base
{
913 vertex_decomposer
*vpd
;
915 enumerator_base(unsigned dim
, vertex_decomposer
*vpd
)
920 vE
= new evalue_p
[vpd
->nbV
];
921 for (int j
= 0; j
< vpd
->nbV
; ++j
)
925 evalue_set_si(&mone
, -1, 1);
928 void decompose_at(Param_Vertices
*V
, int _i
, barvinok_options
*options
) {
932 value_init(vE
[_i
]->d
);
933 evalue_set_si(vE
[_i
], 0, 1);
935 vpd
->decompose_at_vertex(V
, _i
, options
);
938 virtual ~enumerator_base() {
939 for (int j
= 0; j
< vpd
->nbV
; ++j
)
941 free_evalue_refs(vE
[j
]);
946 free_evalue_refs(&mone
);
949 static enumerator_base
*create(Polyhedron
*P
, unsigned dim
, unsigned nbV
,
950 barvinok_options
*options
);
953 struct enumerator
: public signed_cone_consumer
, public vertex_decomposer
,
954 public enumerator_base
{
962 enumerator(Polyhedron
*P
, unsigned dim
, unsigned nbV
) :
963 vertex_decomposer(P
, nbV
, *this), enumerator_base(dim
, this) {
966 randomvector(P
, lambda
, dim
);
968 c
= Vector_Alloc(dim
+2);
978 virtual void handle(const signed_cone
& sc
, barvinok_options
*options
);
981 void enumerator::handle(const signed_cone
& sc
, barvinok_options
*options
)
986 assert(sc
.rays
.NumRows() == dim
);
987 for (int k
= 0; k
< dim
; ++k
) {
988 if (lambda
* sc
.rays
[k
] == 0)
994 lattice_point(V
, sc
.rays
, lambda
, &num
, 0, options
);
995 den
= sc
.rays
* lambda
;
996 normalize(sign
, num
.constant
, den
);
998 dpoly
n(dim
, den
[0], 1);
999 for (int k
= 1; k
< dim
; ++k
) {
1000 dpoly
fact(dim
, den
[k
], 1);
1003 if (num
.E
!= NULL
) {
1004 ZZ
one(INIT_VAL
, 1);
1005 dpoly_n
d(dim
, num
.constant
, one
);
1008 multi_polynom(c
, num
.E
, &EV
);
1009 eadd(&EV
, vE
[vert
]);
1010 free_evalue_refs(&EV
);
1011 free_evalue_refs(num
.E
);
1013 } else if (num
.pos
!= -1) {
1014 dpoly_n
d(dim
, num
.constant
, num
.coeff
);
1017 uni_polynom(num
.pos
, c
, &EV
);
1018 eadd(&EV
, vE
[vert
]);
1019 free_evalue_refs(&EV
);
1021 mpq_set_si(count
, 0, 1);
1022 dpoly
d(dim
, num
.constant
);
1023 d
.div(n
, count
, sign
);
1026 evalue_set(&EV
, &count
[0]._mp_num
, &count
[0]._mp_den
);
1027 eadd(&EV
, vE
[vert
]);
1028 free_evalue_refs(&EV
);
1032 struct ienumerator_base
: enumerator_base
{
1035 ienumerator_base(unsigned dim
, vertex_decomposer
*vpd
) :
1036 enumerator_base(dim
,vpd
) {
1037 E_vertex
= new evalue_p
[dim
];
1040 virtual ~ienumerator_base() {
1044 evalue
*E_num(int i
, int d
) {
1045 return E_vertex
[i
+ (dim
-d
)];
1054 cumulator(evalue
*factor
, evalue
*v
, dpoly_r
*r
) :
1055 factor(factor
), v(v
), r(r
) {}
1057 void cumulate(barvinok_options
*options
);
1059 virtual void add_term(const vector
<int>& powers
, evalue
*f2
) = 0;
1060 virtual ~cumulator() {}
1063 void cumulator::cumulate(barvinok_options
*options
)
1065 evalue cum
; // factor * 1 * E_num[0]/1 * (E_num[0]-1)/2 *...
1067 evalue t
; // E_num[0] - (m-1)
1071 if (options
->lookup_table
) {
1073 evalue_set_si(&mone
, -1, 1);
1077 evalue_copy(&cum
, factor
);
1080 value_set_si(f
.d
, 1);
1081 value_set_si(f
.x
.n
, 1);
1085 if (!options
->lookup_table
) {
1086 for (cst
= &t
; value_zero_p(cst
->d
); ) {
1087 if (cst
->x
.p
->type
== fractional
)
1088 cst
= &cst
->x
.p
->arr
[1];
1090 cst
= &cst
->x
.p
->arr
[0];
1094 for (int m
= 0; m
< r
->len
; ++m
) {
1097 value_set_si(f
.d
, m
);
1099 if (!options
->lookup_table
)
1100 value_subtract(cst
->x
.n
, cst
->x
.n
, cst
->d
);
1106 dpoly_r_term_list
& current
= r
->c
[r
->len
-1-m
];
1107 dpoly_r_term_list::iterator j
;
1108 for (j
= current
.begin(); j
!= current
.end(); ++j
) {
1109 if ((*j
)->coeff
== 0)
1111 evalue
*f2
= new evalue
;
1113 value_init(f2
->x
.n
);
1114 zz2value((*j
)->coeff
, f2
->x
.n
);
1115 zz2value(r
->denom
, f2
->d
);
1118 add_term((*j
)->powers
, f2
);
1121 free_evalue_refs(&f
);
1122 free_evalue_refs(&t
);
1123 free_evalue_refs(&cum
);
1124 if (options
->lookup_table
)
1125 free_evalue_refs(&mone
);
1128 struct E_poly_term
{
1133 struct ie_cum
: public cumulator
{
1134 vector
<E_poly_term
*> terms
;
1136 ie_cum(evalue
*factor
, evalue
*v
, dpoly_r
*r
) : cumulator(factor
, v
, r
) {}
1138 virtual void add_term(const vector
<int>& powers
, evalue
*f2
);
1141 void ie_cum::add_term(const vector
<int>& powers
, evalue
*f2
)
1144 for (k
= 0; k
< terms
.size(); ++k
) {
1145 if (terms
[k
]->powers
== powers
) {
1146 eadd(f2
, terms
[k
]->E
);
1147 free_evalue_refs(f2
);
1152 if (k
>= terms
.size()) {
1153 E_poly_term
*ET
= new E_poly_term
;
1154 ET
->powers
= powers
;
1156 terms
.push_back(ET
);
1160 struct ienumerator
: public signed_cone_consumer
, public vertex_decomposer
,
1161 public ienumerator_base
{
1167 ienumerator(Polyhedron
*P
, unsigned dim
, unsigned nbV
) :
1168 vertex_decomposer(P
, nbV
, *this), ienumerator_base(dim
, this) {
1169 vertex
.SetDims(1, dim
);
1171 den
.SetDims(dim
, dim
);
1179 virtual void handle(const signed_cone
& sc
, barvinok_options
*options
);
1180 void reduce(evalue
*factor
, const mat_ZZ
& num
, const mat_ZZ
& den_f
,
1181 barvinok_options
*options
);
1184 void ienumerator::reduce(evalue
*factor
, const mat_ZZ
& num
, const mat_ZZ
& den_f
,
1185 barvinok_options
*options
)
1187 unsigned len
= den_f
.NumRows(); // number of factors in den
1188 unsigned dim
= num
.NumCols();
1189 assert(num
.NumRows() == 1);
1192 eadd(factor
, vE
[vert
]);
1201 split_one(num
, num_s
, num_p
, den_f
, den_s
, den_r
);
1204 den_p
.SetLength(len
);
1208 normalize(one
, num_s
, num_p
, den_s
, den_p
, den_r
);
1210 emul(&mone
, factor
);
1214 for (int k
= 0; k
< len
; ++k
) {
1217 else if (den_s
[k
] == 0)
1220 if (no_param
== 0) {
1221 reduce(factor
, num_p
, den_r
, options
);
1225 pden
.SetDims(only_param
, dim
-1);
1227 for (k
= 0, l
= 0; k
< len
; ++k
)
1229 pden
[l
++] = den_r
[k
];
1231 for (k
= 0; k
< len
; ++k
)
1235 dpoly
n(no_param
, num_s
[0]);
1236 dpoly
D(no_param
, den_s
[k
], 1);
1237 for ( ; ++k
< len
; )
1238 if (den_p
[k
] == 0) {
1239 dpoly
fact(no_param
, den_s
[k
], 1);
1244 // if no_param + only_param == len then all powers
1245 // below will be all zero
1246 if (no_param
+ only_param
== len
) {
1247 if (E_num(0, dim
) != 0)
1248 r
= new dpoly_r(n
, len
);
1250 mpq_set_si(tcount
, 0, 1);
1252 n
.div(D
, tcount
, one
);
1254 if (value_notzero_p(mpq_numref(tcount
))) {
1258 value_assign(f
.x
.n
, mpq_numref(tcount
));
1259 value_assign(f
.d
, mpq_denref(tcount
));
1261 reduce(factor
, num_p
, pden
, options
);
1262 free_evalue_refs(&f
);
1267 for (k
= 0; k
< len
; ++k
) {
1268 if (den_s
[k
] == 0 || den_p
[k
] == 0)
1271 dpoly
pd(no_param
-1, den_s
[k
], 1);
1274 for (l
= 0; l
< k
; ++l
)
1275 if (den_r
[l
] == den_r
[k
])
1279 r
= new dpoly_r(n
, pd
, l
, len
);
1281 dpoly_r
*nr
= new dpoly_r(r
, pd
, l
, len
);
1287 dpoly_r
*rc
= r
->div(D
);
1290 if (E_num(0, dim
) == 0) {
1291 int common
= pden
.NumRows();
1292 dpoly_r_term_list
& final
= r
->c
[r
->len
-1];
1298 zz2value(r
->denom
, f
.d
);
1299 dpoly_r_term_list::iterator j
;
1300 for (j
= final
.begin(); j
!= final
.end(); ++j
) {
1301 if ((*j
)->coeff
== 0)
1304 for (int k
= 0; k
< r
->dim
; ++k
) {
1305 int n
= (*j
)->powers
[k
];
1308 pden
.SetDims(rows
+n
, pden
.NumCols());
1309 for (int l
= 0; l
< n
; ++l
)
1310 pden
[rows
+l
] = den_r
[k
];
1314 evalue_copy(&t
, factor
);
1315 zz2value((*j
)->coeff
, f
.x
.n
);
1317 reduce(&t
, num_p
, pden
, options
);
1318 free_evalue_refs(&t
);
1320 free_evalue_refs(&f
);
1322 ie_cum
cum(factor
, E_num(0, dim
), r
);
1323 cum
.cumulate(options
);
1325 int common
= pden
.NumRows();
1327 for (int j
= 0; j
< cum
.terms
.size(); ++j
) {
1329 pden
.SetDims(rows
, pden
.NumCols());
1330 for (int k
= 0; k
< r
->dim
; ++k
) {
1331 int n
= cum
.terms
[j
]->powers
[k
];
1334 pden
.SetDims(rows
+n
, pden
.NumCols());
1335 for (int l
= 0; l
< n
; ++l
)
1336 pden
[rows
+l
] = den_r
[k
];
1339 reduce(cum
.terms
[j
]->E
, num_p
, pden
, options
);
1340 free_evalue_refs(cum
.terms
[j
]->E
);
1341 delete cum
.terms
[j
]->E
;
1342 delete cum
.terms
[j
];
1349 static int type_offset(enode
*p
)
1351 return p
->type
== fractional
? 1 :
1352 p
->type
== flooring
? 1 : 0;
1355 static int edegree(evalue
*e
)
1360 if (value_notzero_p(e
->d
))
1364 int i
= type_offset(p
);
1365 if (p
->size
-i
-1 > d
)
1366 d
= p
->size
- i
- 1;
1367 for (; i
< p
->size
; i
++) {
1368 int d2
= edegree(&p
->arr
[i
]);
1375 void ienumerator::handle(const signed_cone
& sc
, barvinok_options
*options
)
1377 assert(sc
.det
== 1);
1379 assert(sc
.rays
.NumRows() == dim
);
1381 lattice_point(V
, sc
.rays
, vertex
[0], E_vertex
, options
);
1387 evalue_set_si(&one
, sc
.sign
, 1);
1388 reduce(&one
, vertex
, den
, options
);
1389 free_evalue_refs(&one
);
1391 for (int i
= 0; i
< dim
; ++i
)
1393 free_evalue_refs(E_vertex
[i
]);
1398 struct bfenumerator
: public vertex_decomposer
, public bf_base
,
1399 public ienumerator_base
{
1402 bfenumerator(Polyhedron
*P
, unsigned dim
, unsigned nbV
) :
1403 vertex_decomposer(P
, nbV
, *this),
1404 bf_base(dim
), ienumerator_base(dim
, this) {
1412 virtual void handle(const signed_cone
& sc
, barvinok_options
*options
);
1413 virtual void base(mat_ZZ
& factors
, bfc_vec
& v
);
1415 bfc_term_base
* new_bf_term(int len
) {
1416 bfe_term
* t
= new bfe_term(len
);
1420 virtual void set_factor(bfc_term_base
*t
, int k
, int change
) {
1421 bfe_term
* bfet
= static_cast<bfe_term
*>(t
);
1422 factor
= bfet
->factors
[k
];
1423 assert(factor
!= NULL
);
1424 bfet
->factors
[k
] = NULL
;
1426 emul(&mone
, factor
);
1429 virtual void set_factor(bfc_term_base
*t
, int k
, mpq_t
&q
, int change
) {
1430 bfe_term
* bfet
= static_cast<bfe_term
*>(t
);
1431 factor
= bfet
->factors
[k
];
1432 assert(factor
!= NULL
);
1433 bfet
->factors
[k
] = NULL
;
1439 value_oppose(f
.x
.n
, mpq_numref(q
));
1441 value_assign(f
.x
.n
, mpq_numref(q
));
1442 value_assign(f
.d
, mpq_denref(q
));
1444 free_evalue_refs(&f
);
1447 virtual void set_factor(bfc_term_base
*t
, int k
, const QQ
& c
, int change
) {
1448 bfe_term
* bfet
= static_cast<bfe_term
*>(t
);
1450 factor
= new evalue
;
1455 zz2value(c
.n
, f
.x
.n
);
1457 value_oppose(f
.x
.n
, f
.x
.n
);
1460 value_init(factor
->d
);
1461 evalue_copy(factor
, bfet
->factors
[k
]);
1463 free_evalue_refs(&f
);
1466 void set_factor(evalue
*f
, int change
) {
1472 virtual void insert_term(bfc_term_base
*t
, int i
) {
1473 bfe_term
* bfet
= static_cast<bfe_term
*>(t
);
1474 int len
= t
->terms
.NumRows()-1; // already increased by one
1476 bfet
->factors
.resize(len
+1);
1477 for (int j
= len
; j
> i
; --j
) {
1478 bfet
->factors
[j
] = bfet
->factors
[j
-1];
1479 t
->terms
[j
] = t
->terms
[j
-1];
1481 bfet
->factors
[i
] = factor
;
1485 virtual void update_term(bfc_term_base
*t
, int i
) {
1486 bfe_term
* bfet
= static_cast<bfe_term
*>(t
);
1488 eadd(factor
, bfet
->factors
[i
]);
1489 free_evalue_refs(factor
);
1493 virtual bool constant_vertex(int dim
) { return E_num(0, dim
) == 0; }
1495 virtual void cum(bf_reducer
*bfr
, bfc_term_base
*t
, int k
, dpoly_r
*r
,
1496 barvinok_options
*options
);
1499 enumerator_base
*enumerator_base::create(Polyhedron
*P
, unsigned dim
, unsigned nbV
,
1500 barvinok_options
*options
)
1502 enumerator_base
*eb
;
1504 if (options
->incremental_specialization
== BV_SPECIALIZATION_BF
)
1505 eb
= new bfenumerator(P
, dim
, nbV
);
1506 else if (options
->incremental_specialization
== BV_SPECIALIZATION_DF
)
1507 eb
= new ienumerator(P
, dim
, nbV
);
1509 eb
= new enumerator(P
, dim
, nbV
);
1514 struct bfe_cum
: public cumulator
{
1516 bfc_term_base
*told
;
1520 bfe_cum(evalue
*factor
, evalue
*v
, dpoly_r
*r
, bf_reducer
*bfr
,
1521 bfc_term_base
*t
, int k
, bfenumerator
*e
) :
1522 cumulator(factor
, v
, r
), told(t
), k(k
),
1526 virtual void add_term(const vector
<int>& powers
, evalue
*f2
);
1529 void bfe_cum::add_term(const vector
<int>& powers
, evalue
*f2
)
1531 bfr
->update_powers(powers
);
1533 bfc_term_base
* t
= bfe
->find_bfc_term(bfr
->vn
, bfr
->npowers
, bfr
->nnf
);
1534 bfe
->set_factor(f2
, bfr
->l_changes
% 2);
1535 bfe
->add_term(t
, told
->terms
[k
], bfr
->l_extra_num
);
1538 void bfenumerator::cum(bf_reducer
*bfr
, bfc_term_base
*t
, int k
,
1539 dpoly_r
*r
, barvinok_options
*options
)
1541 bfe_term
* bfet
= static_cast<bfe_term
*>(t
);
1542 bfe_cum
cum(bfet
->factors
[k
], E_num(0, bfr
->d
), r
, bfr
, t
, k
, this);
1543 cum
.cumulate(options
);
1546 void bfenumerator::base(mat_ZZ
& factors
, bfc_vec
& v
)
1548 for (int i
= 0; i
< v
.size(); ++i
) {
1549 assert(v
[i
]->terms
.NumRows() == 1);
1550 evalue
*factor
= static_cast<bfe_term
*>(v
[i
])->factors
[0];
1551 eadd(factor
, vE
[vert
]);
1556 void bfenumerator::handle(const signed_cone
& sc
, barvinok_options
*options
)
1558 assert(sc
.det
== 1);
1560 assert(sc
.rays
.NumRows() == enumerator_base::dim
);
1562 bfe_term
* t
= new bfe_term(enumerator_base::dim
);
1563 vector
< bfc_term_base
* > v
;
1566 t
->factors
.resize(1);
1568 t
->terms
.SetDims(1, enumerator_base::dim
);
1569 lattice_point(V
, sc
.rays
, t
->terms
[0], E_vertex
, options
);
1571 // the elements of factors are always lexpositive
1573 int s
= setup_factors(sc
.rays
, factors
, t
, sc
.sign
);
1575 t
->factors
[0] = new evalue
;
1576 value_init(t
->factors
[0]->d
);
1577 evalue_set_si(t
->factors
[0], s
, 1);
1578 reduce(factors
, v
, options
);
1580 for (int i
= 0; i
< enumerator_base::dim
; ++i
)
1582 free_evalue_refs(E_vertex
[i
]);
1587 #ifdef HAVE_CORRECT_VERTICES
1588 static inline Param_Polyhedron
*Polyhedron2Param_SD(Polyhedron
**Din
,
1589 Polyhedron
*Cin
,int WS
,Polyhedron
**CEq
,Matrix
**CT
)
1591 if (WS
& POL_NO_DUAL
)
1593 return Polyhedron2Param_SimplifiedDomain(Din
, Cin
, WS
, CEq
, CT
);
1596 static Param_Polyhedron
*Polyhedron2Param_SD(Polyhedron
**Din
,
1597 Polyhedron
*Cin
,int WS
,Polyhedron
**CEq
,Matrix
**CT
)
1599 static char data
[] = " 1 0 0 0 0 1 -18 "
1600 " 1 0 0 -20 0 19 1 "
1601 " 1 0 1 20 0 -20 16 "
1604 " 1 4 -20 0 0 -1 23 "
1605 " 1 -4 20 0 0 1 -22 "
1606 " 1 0 1 0 20 -20 16 "
1607 " 1 0 0 0 -20 19 1 ";
1608 static int checked
= 0;
1613 Matrix
*M
= Matrix_Alloc(9, 7);
1614 for (i
= 0; i
< 9; ++i
)
1615 for (int j
= 0; j
< 7; ++j
) {
1616 sscanf(p
, "%d%n", &v
, &n
);
1618 value_set_si(M
->p
[i
][j
], v
);
1620 Polyhedron
*P
= Constraints2Polyhedron(M
, 1024);
1622 Polyhedron
*U
= Universe_Polyhedron(1);
1623 Param_Polyhedron
*PP
= Polyhedron2Param_Domain(P
, U
, 1024);
1627 for (i
= 0, V
= PP
->V
; V
; ++i
, V
= V
->next
)
1630 Param_Polyhedron_Free(PP
);
1632 fprintf(stderr
, "WARNING: results may be incorrect\n");
1634 "WARNING: use latest version of PolyLib to remove this warning\n");
1638 return Polyhedron2Param_SimplifiedDomain(Din
, Cin
, WS
, CEq
, CT
);
1642 static evalue
* barvinok_enumerate_ev_f(Polyhedron
*P
, Polyhedron
* C
,
1643 barvinok_options
*options
);
1646 static evalue
* barvinok_enumerate_cst(Polyhedron
*P
, Polyhedron
* C
,
1647 struct barvinok_options
*options
)
1651 ALLOC(evalue
, eres
);
1652 value_init(eres
->d
);
1653 value_set_si(eres
->d
, 0);
1654 eres
->x
.p
= new_enode(partition
, 2, C
->Dimension
);
1655 EVALUE_SET_DOMAIN(eres
->x
.p
->arr
[0],
1656 DomainConstraintSimplify(C
, options
->MaxRays
));
1657 value_set_si(eres
->x
.p
->arr
[1].d
, 1);
1658 value_init(eres
->x
.p
->arr
[1].x
.n
);
1660 value_set_si(eres
->x
.p
->arr
[1].x
.n
, 0);
1662 barvinok_count_with_options(P
, &eres
->x
.p
->arr
[1].x
.n
, options
);
1667 evalue
* barvinok_enumerate_with_options(Polyhedron
*P
, Polyhedron
* C
,
1668 struct barvinok_options
*options
)
1670 //P = unfringe(P, MaxRays);
1671 Polyhedron
*next
, *Cnext
;
1672 Polyhedron
*Corig
= C
;
1673 Polyhedron
*Porig
= P
;
1674 Polyhedron
*CEq
= NULL
, *rVD
, *CA
;
1676 unsigned nparam
= C
->Dimension
;
1681 "barvinok_enumerate: input is a union; only first polyhedron is enumerated\n");
1685 "barvinok_enumerate: context is a union; only first polyhedron is considered\n");
1688 value_init(factor
.d
);
1689 evalue_set_si(&factor
, 1, 1);
1693 CA
= align_context(C
, P
->Dimension
, options
->MaxRays
);
1696 P
= DomainIntersection(P
, CA
, options
->MaxRays
);
1698 Polyhedron_Free(CA
);
1701 POL_ENSURE_FACETS(P
);
1702 POL_ENSURE_VERTICES(P
);
1703 POL_ENSURE_FACETS(C
);
1704 POL_ENSURE_VERTICES(C
);
1706 if (C
->Dimension
== 0 || emptyQ(P
)) {
1708 eres
= barvinok_enumerate_cst(P
, CEq
? CEq
: Polyhedron_Copy(C
), options
);
1710 emul(&factor
, eres
);
1711 if (options
->polynomial_approximation
== BV_POLAPPROX_UPPER
)
1712 evalue_frac2polynomial(eres
, 1, options
->MaxRays
);
1713 if (options
->polynomial_approximation
== BV_POLAPPROX_LOWER
)
1714 evalue_frac2polynomial(eres
, -1, options
->MaxRays
);
1715 reduce_evalue(eres
);
1716 free_evalue_refs(&factor
);
1721 Corig
->next
= Cnext
;
1724 if (Polyhedron_is_unbounded(P
, nparam
, options
->MaxRays
))
1729 P
= remove_equalities_p(P
, P
->Dimension
-nparam
, &f
);
1730 mask(f
, &factor
, options
);
1733 if (P
->Dimension
== nparam
) {
1735 P
= Universe_Polyhedron(0);
1739 Polyhedron
*T
= Polyhedron_Factor(P
, nparam
, NULL
, options
->MaxRays
);
1740 if (T
|| (P
->Dimension
== nparam
+1)) {
1743 for (Q
= T
? T
: P
; Q
; Q
= Q
->next
) {
1744 Polyhedron
*next
= Q
->next
;
1748 if (Q
->Dimension
!= C
->Dimension
)
1749 QC
= Polyhedron_Project(Q
, nparam
);
1752 C
= DomainIntersection(C
, QC
, options
->MaxRays
);
1754 Polyhedron_Free(C2
);
1756 Polyhedron_Free(QC
);
1764 if (T
->Dimension
== C
->Dimension
) {
1773 eres
= barvinok_enumerate_ev_f(P
, C
, options
);
1780 for (Q
= P
->next
; Q
; Q
= Q
->next
) {
1781 Polyhedron
*next
= Q
->next
;
1784 f
= barvinok_enumerate_ev_f(Q
, C
, options
);
1786 free_evalue_refs(f
);
1796 evalue
* barvinok_enumerate_ev(Polyhedron
*P
, Polyhedron
* C
, unsigned MaxRays
)
1799 barvinok_options
*options
= barvinok_options_new_with_defaults();
1800 options
->MaxRays
= MaxRays
;
1801 E
= barvinok_enumerate_with_options(P
, C
, options
);
1802 barvinok_options_free(options
);
1806 /* adapted from mpolyhedron_inflate in PolyLib */
1807 static Polyhedron
*Polyhedron_Inflate(Polyhedron
*P
, unsigned nparam
,
1811 int nvar
= P
->Dimension
- nparam
;
1812 Matrix
*C
= Polyhedron2Constraints(P
);
1816 /* subtract the sum of the negative coefficients of each inequality */
1817 for (int i
= 0; i
< C
->NbRows
; ++i
) {
1818 value_set_si(sum
, 0);
1819 for (int j
= 0; j
< nvar
; ++j
)
1820 if (value_neg_p(C
->p
[i
][1+j
]))
1821 value_addto(sum
, sum
, C
->p
[i
][1+j
]);
1822 value_subtract(C
->p
[i
][1+P
->Dimension
], C
->p
[i
][1+P
->Dimension
], sum
);
1825 P2
= Constraints2Polyhedron(C
, MaxRays
);
1830 /* adapted from mpolyhedron_deflate in PolyLib */
1831 static Polyhedron
*Polyhedron_Deflate(Polyhedron
*P
, unsigned nparam
,
1835 int nvar
= P
->Dimension
- nparam
;
1836 Matrix
*C
= Polyhedron2Constraints(P
);
1840 /* subtract the sum of the positive coefficients of each inequality */
1841 for (int i
= 0; i
< C
->NbRows
; ++i
) {
1842 value_set_si(sum
, 0);
1843 for (int j
= 0; j
< nvar
; ++j
)
1844 if (value_pos_p(C
->p
[i
][1+j
]))
1845 value_addto(sum
, sum
, C
->p
[i
][1+j
]);
1846 value_subtract(C
->p
[i
][1+P
->Dimension
], C
->p
[i
][1+P
->Dimension
], sum
);
1849 P2
= Constraints2Polyhedron(C
, MaxRays
);
1854 static evalue
* barvinok_enumerate_ev_f(Polyhedron
*P
, Polyhedron
* C
,
1855 barvinok_options
*options
)
1857 unsigned nparam
= C
->Dimension
;
1858 bool pre_approx
= options
->polynomial_approximation
>= BV_POLAPPROX_PRE_LOWER
&&
1859 options
->polynomial_approximation
<= BV_POLAPPROX_PRE_APPROX
;
1861 if (P
->Dimension
- nparam
== 1 && !pre_approx
)
1862 return ParamLine_Length(P
, C
, options
);
1864 Param_Polyhedron
*PP
= NULL
;
1865 Polyhedron
*CEq
= NULL
, *pVD
;
1867 Param_Domain
*D
, *next
;
1870 Polyhedron
*Porig
= P
;
1874 if (options
->polynomial_approximation
== BV_POLAPPROX_PRE_UPPER
)
1875 P
= Polyhedron_Inflate(P
, nparam
, options
->MaxRays
);
1876 if (options
->polynomial_approximation
== BV_POLAPPROX_PRE_LOWER
) {
1877 P
= Polyhedron_Deflate(P
, nparam
, options
->MaxRays
);
1878 POL_ENSURE_VERTICES(P
);
1880 eres
= barvinok_enumerate_cst(P
, Polyhedron_Copy(C
), options
);
1887 PP
= Polyhedron2Param_SD(&T
, C
, options
->MaxRays
, &CEq
, &CT
);
1888 if (T
!= P
&& P
!= Porig
)
1892 if (isIdentity(CT
)) {
1896 assert(CT
->NbRows
!= CT
->NbColumns
);
1897 if (CT
->NbRows
== 1) { // no more parameters
1898 eres
= barvinok_enumerate_cst(P
, CEq
, options
);
1903 Param_Polyhedron_Free(PP
);
1909 nparam
= CT
->NbRows
- 1;
1915 Param_Polyhedron_Scale_Integer(PP
, &T
, &det
, options
->MaxRays
);
1921 unsigned dim
= P
->Dimension
- nparam
;
1923 ALLOC(evalue
, eres
);
1924 value_init(eres
->d
);
1925 value_set_si(eres
->d
, 0);
1928 for (nd
= 0, D
=PP
->D
; D
; ++nd
, D
=D
->next
);
1929 struct section
{ Polyhedron
*D
; evalue E
; };
1930 section
*s
= new section
[nd
];
1931 Polyhedron
**fVD
= new Polyhedron_p
[nd
];
1933 enumerator_base
*et
= NULL
;
1938 et
= enumerator_base::create(P
, dim
, PP
->nbV
, options
);
1940 for(nd
= 0, D
=PP
->D
; D
; D
=next
) {
1943 Polyhedron
*rVD
= reduce_domain(D
->Domain
, CT
, CEq
, fVD
, nd
, options
);
1947 pVD
= CT
? DomainImage(rVD
,CT
,options
->MaxRays
) : rVD
;
1949 value_init(s
[nd
].E
.d
);
1950 evalue_set_si(&s
[nd
].E
, 0, 1);
1953 FORALL_PVertex_in_ParamPolyhedron(V
,D
,PP
) // _i is internal counter
1956 et
->decompose_at(V
, _i
, options
);
1957 } catch (OrthogonalException
&e
) {
1960 for (; nd
>= 0; --nd
) {
1961 free_evalue_refs(&s
[nd
].E
);
1962 Domain_Free(s
[nd
].D
);
1963 Domain_Free(fVD
[nd
]);
1967 eadd(et
->vE
[_i
] , &s
[nd
].E
);
1968 END_FORALL_PVertex_in_ParamPolyhedron
;
1969 evalue_range_reduction_in_domain(&s
[nd
].E
, pVD
);
1972 addeliminatedparams_evalue(&s
[nd
].E
, CT
);
1980 evalue_set_si(eres
, 0, 1);
1982 eres
->x
.p
= new_enode(partition
, 2*nd
, C
->Dimension
);
1983 for (int j
= 0; j
< nd
; ++j
) {
1984 EVALUE_SET_DOMAIN(eres
->x
.p
->arr
[2*j
], s
[j
].D
);
1985 value_clear(eres
->x
.p
->arr
[2*j
+1].d
);
1986 eres
->x
.p
->arr
[2*j
+1] = s
[j
].E
;
1987 Domain_Free(fVD
[j
]);
1994 evalue_div(eres
, det
);
1999 Polyhedron_Free(CEq
);
2003 Enumeration
* barvinok_enumerate(Polyhedron
*P
, Polyhedron
* C
, unsigned MaxRays
)
2005 evalue
*EP
= barvinok_enumerate_ev(P
, C
, MaxRays
);
2007 return partition2enumeration(EP
);
2010 static void SwapColumns(Value
**V
, int n
, int i
, int j
)
2012 for (int r
= 0; r
< n
; ++r
)
2013 value_swap(V
[r
][i
], V
[r
][j
]);
2016 static void SwapColumns(Polyhedron
*P
, int i
, int j
)
2018 SwapColumns(P
->Constraint
, P
->NbConstraints
, i
, j
);
2019 SwapColumns(P
->Ray
, P
->NbRays
, i
, j
);
2022 /* Construct a constraint c from constraints l and u such that if
2023 * if constraint c holds then for each value of the other variables
2024 * there is at most one value of variable pos (position pos+1 in the constraints).
2026 * Given a lower and an upper bound
2027 * n_l v_i + <c_l,x> + c_l >= 0
2028 * -n_u v_i + <c_u,x> + c_u >= 0
2029 * the constructed constraint is
2031 * -(n_l<c_u,x> + n_u<c_l,x>) + (-n_l c_u - n_u c_l + n_l n_u - 1)
2033 * which is then simplified to remove the content of the non-constant coefficients
2035 * len is the total length of the constraints.
2036 * v is a temporary variable that can be used by this procedure
2038 static void negative_test_constraint(Value
*l
, Value
*u
, Value
*c
, int pos
,
2041 value_oppose(*v
, u
[pos
+1]);
2042 Vector_Combine(l
+1, u
+1, c
+1, *v
, l
[pos
+1], len
-1);
2043 value_multiply(*v
, *v
, l
[pos
+1]);
2044 value_subtract(c
[len
-1], c
[len
-1], *v
);
2045 value_set_si(*v
, -1);
2046 Vector_Scale(c
+1, c
+1, *v
, len
-1);
2047 value_decrement(c
[len
-1], c
[len
-1]);
2048 ConstraintSimplify(c
, c
, len
, v
);
2051 static bool parallel_constraints(Value
*l
, Value
*u
, Value
*c
, int pos
,
2060 Vector_Gcd(&l
[1+pos
], len
, &g1
);
2061 Vector_Gcd(&u
[1+pos
], len
, &g2
);
2062 Vector_Combine(l
+1+pos
, u
+1+pos
, c
+1, g2
, g1
, len
);
2063 parallel
= First_Non_Zero(c
+1, len
) == -1;
2071 static void negative_test_constraint7(Value
*l
, Value
*u
, Value
*c
, int pos
,
2072 int exist
, int len
, Value
*v
)
2077 Vector_Gcd(&u
[1+pos
], exist
, v
);
2078 Vector_Gcd(&l
[1+pos
], exist
, &g
);
2079 Vector_Combine(l
+1, u
+1, c
+1, *v
, g
, len
-1);
2080 value_multiply(*v
, *v
, g
);
2081 value_subtract(c
[len
-1], c
[len
-1], *v
);
2082 value_set_si(*v
, -1);
2083 Vector_Scale(c
+1, c
+1, *v
, len
-1);
2084 value_decrement(c
[len
-1], c
[len
-1]);
2085 ConstraintSimplify(c
, c
, len
, v
);
2090 /* Turns a x + b >= 0 into a x + b <= -1
2092 * len is the total length of the constraint.
2093 * v is a temporary variable that can be used by this procedure
2095 static void oppose_constraint(Value
*c
, int len
, Value
*v
)
2097 value_set_si(*v
, -1);
2098 Vector_Scale(c
+1, c
+1, *v
, len
-1);
2099 value_decrement(c
[len
-1], c
[len
-1]);
2102 /* Split polyhedron P into two polyhedra *pos and *neg, where
2103 * existential variable i has at most one solution for each
2104 * value of the other variables in *neg.
2106 * The splitting is performed using constraints l and u.
2108 * nvar: number of set variables
2109 * row: temporary vector that can be used by this procedure
2110 * f: temporary value that can be used by this procedure
2112 static bool SplitOnConstraint(Polyhedron
*P
, int i
, int l
, int u
,
2113 int nvar
, int MaxRays
, Vector
*row
, Value
& f
,
2114 Polyhedron
**pos
, Polyhedron
**neg
)
2116 negative_test_constraint(P
->Constraint
[l
], P
->Constraint
[u
],
2117 row
->p
, nvar
+i
, P
->Dimension
+2, &f
);
2118 *neg
= AddConstraints(row
->p
, 1, P
, MaxRays
);
2120 /* We found an independent, but useless constraint
2121 * Maybe we should detect this earlier and not
2122 * mark the variable as INDEPENDENT
2124 if (emptyQ((*neg
))) {
2125 Polyhedron_Free(*neg
);
2129 oppose_constraint(row
->p
, P
->Dimension
+2, &f
);
2130 *pos
= AddConstraints(row
->p
, 1, P
, MaxRays
);
2132 if (emptyQ((*pos
))) {
2133 Polyhedron_Free(*neg
);
2134 Polyhedron_Free(*pos
);
2142 * unimodularly transform P such that constraint r is transformed
2143 * into a constraint that involves only a single (the first)
2144 * existential variable
2147 static Polyhedron
*rotate_along(Polyhedron
*P
, int r
, int nvar
, int exist
,
2153 Vector
*row
= Vector_Alloc(exist
);
2154 Vector_Copy(P
->Constraint
[r
]+1+nvar
, row
->p
, exist
);
2155 Vector_Gcd(row
->p
, exist
, &g
);
2156 if (value_notone_p(g
))
2157 Vector_AntiScale(row
->p
, row
->p
, g
, exist
);
2160 Matrix
*M
= unimodular_complete(row
);
2161 Matrix
*M2
= Matrix_Alloc(P
->Dimension
+1, P
->Dimension
+1);
2162 for (r
= 0; r
< nvar
; ++r
)
2163 value_set_si(M2
->p
[r
][r
], 1);
2164 for ( ; r
< nvar
+exist
; ++r
)
2165 Vector_Copy(M
->p
[r
-nvar
], M2
->p
[r
]+nvar
, exist
);
2166 for ( ; r
< P
->Dimension
+1; ++r
)
2167 value_set_si(M2
->p
[r
][r
], 1);
2168 Polyhedron
*T
= Polyhedron_Image(P
, M2
, MaxRays
);
2177 /* Split polyhedron P into two polyhedra *pos and *neg, where
2178 * existential variable i has at most one solution for each
2179 * value of the other variables in *neg.
2181 * If independent is set, then the two constraints on which the
2182 * split will be performed need to be independent of the other
2183 * existential variables.
2185 * Return true if an appropriate split could be performed.
2187 * nvar: number of set variables
2188 * exist: number of existential variables
2189 * row: temporary vector that can be used by this procedure
2190 * f: temporary value that can be used by this procedure
2192 static bool SplitOnVar(Polyhedron
*P
, int i
,
2193 int nvar
, int exist
, int MaxRays
,
2194 Vector
*row
, Value
& f
, bool independent
,
2195 Polyhedron
**pos
, Polyhedron
**neg
)
2199 for (int l
= P
->NbEq
; l
< P
->NbConstraints
; ++l
) {
2200 if (value_negz_p(P
->Constraint
[l
][nvar
+i
+1]))
2204 for (j
= 0; j
< exist
; ++j
)
2205 if (j
!= i
&& value_notzero_p(P
->Constraint
[l
][nvar
+j
+1]))
2211 for (int u
= P
->NbEq
; u
< P
->NbConstraints
; ++u
) {
2212 if (value_posz_p(P
->Constraint
[u
][nvar
+i
+1]))
2216 for (j
= 0; j
< exist
; ++j
)
2217 if (j
!= i
&& value_notzero_p(P
->Constraint
[u
][nvar
+j
+1]))
2223 if (SplitOnConstraint(P
, i
, l
, u
, nvar
, MaxRays
, row
, f
, pos
, neg
)) {
2226 SwapColumns(*neg
, nvar
+1, nvar
+1+i
);
2236 static bool double_bound_pair(Polyhedron
*P
, int nvar
, int exist
,
2237 int i
, int l1
, int l2
,
2238 Polyhedron
**pos
, Polyhedron
**neg
)
2242 Vector
*row
= Vector_Alloc(P
->Dimension
+2);
2243 value_set_si(row
->p
[0], 1);
2244 value_oppose(f
, P
->Constraint
[l1
][nvar
+i
+1]);
2245 Vector_Combine(P
->Constraint
[l1
]+1, P
->Constraint
[l2
]+1,
2247 P
->Constraint
[l2
][nvar
+i
+1], f
,
2249 ConstraintSimplify(row
->p
, row
->p
, P
->Dimension
+2, &f
);
2250 *pos
= AddConstraints(row
->p
, 1, P
, 0);
2251 value_set_si(f
, -1);
2252 Vector_Scale(row
->p
+1, row
->p
+1, f
, P
->Dimension
+1);
2253 value_decrement(row
->p
[P
->Dimension
+1], row
->p
[P
->Dimension
+1]);
2254 *neg
= AddConstraints(row
->p
, 1, P
, 0);
2258 return !emptyQ((*pos
)) && !emptyQ((*neg
));
2261 static bool double_bound(Polyhedron
*P
, int nvar
, int exist
,
2262 Polyhedron
**pos
, Polyhedron
**neg
)
2264 for (int i
= 0; i
< exist
; ++i
) {
2266 for (l1
= P
->NbEq
; l1
< P
->NbConstraints
; ++l1
) {
2267 if (value_negz_p(P
->Constraint
[l1
][nvar
+i
+1]))
2269 for (l2
= l1
+ 1; l2
< P
->NbConstraints
; ++l2
) {
2270 if (value_negz_p(P
->Constraint
[l2
][nvar
+i
+1]))
2272 if (double_bound_pair(P
, nvar
, exist
, i
, l1
, l2
, pos
, neg
))
2276 for (l1
= P
->NbEq
; l1
< P
->NbConstraints
; ++l1
) {
2277 if (value_posz_p(P
->Constraint
[l1
][nvar
+i
+1]))
2279 if (l1
< P
->NbConstraints
)
2280 for (l2
= l1
+ 1; l2
< P
->NbConstraints
; ++l2
) {
2281 if (value_posz_p(P
->Constraint
[l2
][nvar
+i
+1]))
2283 if (double_bound_pair(P
, nvar
, exist
, i
, l1
, l2
, pos
, neg
))
2295 INDEPENDENT
= 1 << 2,
2299 static evalue
* enumerate_or(Polyhedron
*D
,
2300 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2303 fprintf(stderr
, "\nER: Or\n");
2304 #endif /* DEBUG_ER */
2306 Polyhedron
*N
= D
->next
;
2309 barvinok_enumerate_e_with_options(D
, exist
, nparam
, options
);
2312 for (D
= N
; D
; D
= N
) {
2317 barvinok_enumerate_e_with_options(D
, exist
, nparam
, options
);
2320 free_evalue_refs(EN
);
2330 static evalue
* enumerate_sum(Polyhedron
*P
,
2331 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2333 int nvar
= P
->Dimension
- exist
- nparam
;
2334 int toswap
= nvar
< exist
? nvar
: exist
;
2335 for (int i
= 0; i
< toswap
; ++i
)
2336 SwapColumns(P
, 1 + i
, nvar
+exist
- i
);
2340 fprintf(stderr
, "\nER: Sum\n");
2341 #endif /* DEBUG_ER */
2343 evalue
*EP
= barvinok_enumerate_e_with_options(P
, exist
, nparam
, options
);
2345 evalue_split_domains_into_orthants(EP
, options
->MaxRays
);
2347 evalue_range_reduction(EP
);
2349 evalue_frac2floor2(EP
, 1);
2351 evalue
*sum
= esum(EP
, nvar
);
2353 free_evalue_refs(EP
);
2357 evalue_range_reduction(EP
);
2362 static evalue
* split_sure(Polyhedron
*P
, Polyhedron
*S
,
2363 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2365 int nvar
= P
->Dimension
- exist
- nparam
;
2367 Matrix
*M
= Matrix_Alloc(exist
, S
->Dimension
+2);
2368 for (int i
= 0; i
< exist
; ++i
)
2369 value_set_si(M
->p
[i
][nvar
+i
+1], 1);
2371 S
= DomainAddRays(S
, M
, options
->MaxRays
);
2373 Polyhedron
*F
= DomainAddRays(P
, M
, options
->MaxRays
);
2374 Polyhedron
*D
= DomainDifference(F
, S
, options
->MaxRays
);
2376 D
= Disjoint_Domain(D
, 0, options
->MaxRays
);
2381 M
= Matrix_Alloc(P
->Dimension
+1-exist
, P
->Dimension
+1);
2382 for (int j
= 0; j
< nvar
; ++j
)
2383 value_set_si(M
->p
[j
][j
], 1);
2384 for (int j
= 0; j
< nparam
+1; ++j
)
2385 value_set_si(M
->p
[nvar
+j
][nvar
+exist
+j
], 1);
2386 Polyhedron
*T
= Polyhedron_Image(S
, M
, options
->MaxRays
);
2387 evalue
*EP
= barvinok_enumerate_e_with_options(T
, 0, nparam
, options
);
2392 for (Polyhedron
*Q
= D
; Q
; Q
= Q
->next
) {
2393 Polyhedron
*N
= Q
->next
;
2395 T
= DomainIntersection(P
, Q
, options
->MaxRays
);
2396 evalue
*E
= barvinok_enumerate_e_with_options(T
, exist
, nparam
, options
);
2398 free_evalue_refs(E
);
2407 static evalue
* enumerate_sure(Polyhedron
*P
,
2408 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2412 int nvar
= P
->Dimension
- exist
- nparam
;
2418 for (i
= 0; i
< exist
; ++i
) {
2419 Matrix
*M
= Matrix_Alloc(S
->NbConstraints
, S
->Dimension
+2);
2421 value_set_si(lcm
, 1);
2422 for (int j
= 0; j
< S
->NbConstraints
; ++j
) {
2423 if (value_negz_p(S
->Constraint
[j
][1+nvar
+i
]))
2425 if (value_one_p(S
->Constraint
[j
][1+nvar
+i
]))
2427 value_lcm(lcm
, S
->Constraint
[j
][1+nvar
+i
], &lcm
);
2430 for (int j
= 0; j
< S
->NbConstraints
; ++j
) {
2431 if (value_negz_p(S
->Constraint
[j
][1+nvar
+i
]))
2433 if (value_one_p(S
->Constraint
[j
][1+nvar
+i
]))
2435 value_division(f
, lcm
, S
->Constraint
[j
][1+nvar
+i
]);
2436 Vector_Scale(S
->Constraint
[j
], M
->p
[c
], f
, S
->Dimension
+2);
2437 value_subtract(M
->p
[c
][S
->Dimension
+1],
2438 M
->p
[c
][S
->Dimension
+1],
2440 value_increment(M
->p
[c
][S
->Dimension
+1],
2441 M
->p
[c
][S
->Dimension
+1]);
2445 S
= AddConstraints(M
->p
[0], c
, S
, options
->MaxRays
);
2460 fprintf(stderr
, "\nER: Sure\n");
2461 #endif /* DEBUG_ER */
2463 return split_sure(P
, S
, exist
, nparam
, options
);
2466 static evalue
* enumerate_sure2(Polyhedron
*P
,
2467 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2469 int nvar
= P
->Dimension
- exist
- nparam
;
2471 for (r
= 0; r
< P
->NbRays
; ++r
)
2472 if (value_one_p(P
->Ray
[r
][0]) &&
2473 value_one_p(P
->Ray
[r
][P
->Dimension
+1]))
2479 Matrix
*M
= Matrix_Alloc(nvar
+ 1 + nparam
, P
->Dimension
+2);
2480 for (int i
= 0; i
< nvar
; ++i
)
2481 value_set_si(M
->p
[i
][1+i
], 1);
2482 for (int i
= 0; i
< nparam
; ++i
)
2483 value_set_si(M
->p
[i
+nvar
][1+nvar
+exist
+i
], 1);
2484 Vector_Copy(P
->Ray
[r
]+1+nvar
, M
->p
[nvar
+nparam
]+1+nvar
, exist
);
2485 value_set_si(M
->p
[nvar
+nparam
][0], 1);
2486 value_set_si(M
->p
[nvar
+nparam
][P
->Dimension
+1], 1);
2487 Polyhedron
* F
= Rays2Polyhedron(M
, options
->MaxRays
);
2490 Polyhedron
*I
= DomainIntersection(F
, P
, options
->MaxRays
);
2494 fprintf(stderr
, "\nER: Sure2\n");
2495 #endif /* DEBUG_ER */
2497 return split_sure(P
, I
, exist
, nparam
, options
);
2500 static evalue
* enumerate_cyclic(Polyhedron
*P
,
2501 unsigned exist
, unsigned nparam
,
2502 evalue
* EP
, int r
, int p
, unsigned MaxRays
)
2504 int nvar
= P
->Dimension
- exist
- nparam
;
2506 /* If EP in its fractional maps only contains references
2507 * to the remainder parameter with appropriate coefficients
2508 * then we could in principle avoid adding existentially
2509 * quantified variables to the validity domains.
2510 * We'd have to replace the remainder by m { p/m }
2511 * and multiply with an appropriate factor that is one
2512 * only in the appropriate range.
2513 * This last multiplication can be avoided if EP
2514 * has a single validity domain with no (further)
2515 * constraints on the remainder parameter
2518 Matrix
*CT
= Matrix_Alloc(nparam
+1, nparam
+3);
2519 Matrix
*M
= Matrix_Alloc(1, 1+nparam
+3);
2520 for (int j
= 0; j
< nparam
; ++j
)
2522 value_set_si(CT
->p
[j
][j
], 1);
2523 value_set_si(CT
->p
[p
][nparam
+1], 1);
2524 value_set_si(CT
->p
[nparam
][nparam
+2], 1);
2525 value_set_si(M
->p
[0][1+p
], -1);
2526 value_absolute(M
->p
[0][1+nparam
], P
->Ray
[0][1+nvar
+exist
+p
]);
2527 value_set_si(M
->p
[0][1+nparam
+1], 1);
2528 Polyhedron
*CEq
= Constraints2Polyhedron(M
, 1);
2530 addeliminatedparams_enum(EP
, CT
, CEq
, MaxRays
, nparam
);
2531 Polyhedron_Free(CEq
);
2537 static void enumerate_vd_add_ray(evalue
*EP
, Matrix
*Rays
, unsigned MaxRays
)
2539 if (value_notzero_p(EP
->d
))
2542 assert(EP
->x
.p
->type
== partition
);
2543 assert(EP
->x
.p
->pos
== EVALUE_DOMAIN(EP
->x
.p
->arr
[0])->Dimension
);
2544 for (int i
= 0; i
< EP
->x
.p
->size
/2; ++i
) {
2545 Polyhedron
*D
= EVALUE_DOMAIN(EP
->x
.p
->arr
[2*i
]);
2546 Polyhedron
*N
= DomainAddRays(D
, Rays
, MaxRays
);
2547 EVALUE_SET_DOMAIN(EP
->x
.p
->arr
[2*i
], N
);
2552 static evalue
* enumerate_line(Polyhedron
*P
,
2553 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2559 fprintf(stderr
, "\nER: Line\n");
2560 #endif /* DEBUG_ER */
2562 int nvar
= P
->Dimension
- exist
- nparam
;
2564 for (i
= 0; i
< nparam
; ++i
)
2565 if (value_notzero_p(P
->Ray
[0][1+nvar
+exist
+i
]))
2568 for (j
= i
+1; j
< nparam
; ++j
)
2569 if (value_notzero_p(P
->Ray
[0][1+nvar
+exist
+i
]))
2571 assert(j
>= nparam
); // for now
2573 Matrix
*M
= Matrix_Alloc(2, P
->Dimension
+2);
2574 value_set_si(M
->p
[0][0], 1);
2575 value_set_si(M
->p
[0][1+nvar
+exist
+i
], 1);
2576 value_set_si(M
->p
[1][0], 1);
2577 value_set_si(M
->p
[1][1+nvar
+exist
+i
], -1);
2578 value_absolute(M
->p
[1][1+P
->Dimension
], P
->Ray
[0][1+nvar
+exist
+i
]);
2579 value_decrement(M
->p
[1][1+P
->Dimension
], M
->p
[1][1+P
->Dimension
]);
2580 Polyhedron
*S
= AddConstraints(M
->p
[0], 2, P
, options
->MaxRays
);
2581 evalue
*EP
= barvinok_enumerate_e_with_options(S
, exist
, nparam
, options
);
2585 return enumerate_cyclic(P
, exist
, nparam
, EP
, 0, i
, options
->MaxRays
);
2588 static int single_param_pos(Polyhedron
*P
, unsigned exist
, unsigned nparam
,
2591 int nvar
= P
->Dimension
- exist
- nparam
;
2592 if (First_Non_Zero(P
->Ray
[r
]+1, nvar
) != -1)
2594 int i
= First_Non_Zero(P
->Ray
[r
]+1+nvar
+exist
, nparam
);
2597 if (First_Non_Zero(P
->Ray
[r
]+1+nvar
+exist
+1, nparam
-i
-1) != -1)
2602 static evalue
* enumerate_remove_ray(Polyhedron
*P
, int r
,
2603 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2606 fprintf(stderr
, "\nER: RedundantRay\n");
2607 #endif /* DEBUG_ER */
2611 value_set_si(one
, 1);
2612 int len
= P
->NbRays
-1;
2613 Matrix
*M
= Matrix_Alloc(2 * len
, P
->Dimension
+2);
2614 Vector_Copy(P
->Ray
[0], M
->p
[0], r
* (P
->Dimension
+2));
2615 Vector_Copy(P
->Ray
[r
+1], M
->p
[r
], (len
-r
) * (P
->Dimension
+2));
2616 for (int j
= 0; j
< P
->NbRays
; ++j
) {
2619 Vector_Combine(P
->Ray
[j
], P
->Ray
[r
], M
->p
[len
+j
-(j
>r
)],
2620 one
, P
->Ray
[j
][P
->Dimension
+1], P
->Dimension
+2);
2623 P
= Rays2Polyhedron(M
, options
->MaxRays
);
2625 evalue
*EP
= barvinok_enumerate_e_with_options(P
, exist
, nparam
, options
);
2632 static evalue
* enumerate_redundant_ray(Polyhedron
*P
,
2633 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2635 assert(P
->NbBid
== 0);
2636 int nvar
= P
->Dimension
- exist
- nparam
;
2640 for (int r
= 0; r
< P
->NbRays
; ++r
) {
2641 if (value_notzero_p(P
->Ray
[r
][P
->Dimension
+1]))
2643 int i1
= single_param_pos(P
, exist
, nparam
, r
);
2646 for (int r2
= r
+1; r2
< P
->NbRays
; ++r2
) {
2647 if (value_notzero_p(P
->Ray
[r2
][P
->Dimension
+1]))
2649 int i2
= single_param_pos(P
, exist
, nparam
, r2
);
2655 value_division(m
, P
->Ray
[r
][1+nvar
+exist
+i1
],
2656 P
->Ray
[r2
][1+nvar
+exist
+i1
]);
2657 value_multiply(m
, m
, P
->Ray
[r2
][1+nvar
+exist
+i1
]);
2658 /* r2 divides r => r redundant */
2659 if (value_eq(m
, P
->Ray
[r
][1+nvar
+exist
+i1
])) {
2661 return enumerate_remove_ray(P
, r
, exist
, nparam
, options
);
2664 value_division(m
, P
->Ray
[r2
][1+nvar
+exist
+i1
],
2665 P
->Ray
[r
][1+nvar
+exist
+i1
]);
2666 value_multiply(m
, m
, P
->Ray
[r
][1+nvar
+exist
+i1
]);
2667 /* r divides r2 => r2 redundant */
2668 if (value_eq(m
, P
->Ray
[r2
][1+nvar
+exist
+i1
])) {
2670 return enumerate_remove_ray(P
, r2
, exist
, nparam
, options
);
2678 static Polyhedron
*upper_bound(Polyhedron
*P
,
2679 int pos
, Value
*max
, Polyhedron
**R
)
2688 for (Polyhedron
*Q
= P
; Q
; Q
= N
) {
2690 for (r
= 0; r
< P
->NbRays
; ++r
) {
2691 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1]) &&
2692 value_pos_p(P
->Ray
[r
][1+pos
]))
2695 if (r
< P
->NbRays
) {
2703 for (r
= 0; r
< P
->NbRays
; ++r
) {
2704 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1]))
2706 mpz_fdiv_q(v
, P
->Ray
[r
][1+pos
], P
->Ray
[r
][1+P
->Dimension
]);
2707 if ((!Q
->next
&& r
== 0) || value_gt(v
, *max
))
2708 value_assign(*max
, v
);
2715 static evalue
* enumerate_ray(Polyhedron
*P
,
2716 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2718 assert(P
->NbBid
== 0);
2719 int nvar
= P
->Dimension
- exist
- nparam
;
2722 for (r
= 0; r
< P
->NbRays
; ++r
)
2723 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1]))
2729 for (r2
= r
+1; r2
< P
->NbRays
; ++r2
)
2730 if (value_zero_p(P
->Ray
[r2
][P
->Dimension
+1]))
2732 if (r2
< P
->NbRays
) {
2734 return enumerate_sum(P
, exist
, nparam
, options
);
2738 fprintf(stderr
, "\nER: Ray\n");
2739 #endif /* DEBUG_ER */
2745 value_set_si(one
, 1);
2746 int i
= single_param_pos(P
, exist
, nparam
, r
);
2747 assert(i
!= -1); // for now;
2749 Matrix
*M
= Matrix_Alloc(P
->NbRays
, P
->Dimension
+2);
2750 for (int j
= 0; j
< P
->NbRays
; ++j
) {
2751 Vector_Combine(P
->Ray
[j
], P
->Ray
[r
], M
->p
[j
],
2752 one
, P
->Ray
[j
][P
->Dimension
+1], P
->Dimension
+2);
2754 Polyhedron
*S
= Rays2Polyhedron(M
, options
->MaxRays
);
2756 Polyhedron
*D
= DomainDifference(P
, S
, options
->MaxRays
);
2758 // Polyhedron_Print(stderr, P_VALUE_FMT, D);
2759 assert(value_pos_p(P
->Ray
[r
][1+nvar
+exist
+i
])); // for now
2761 D
= upper_bound(D
, nvar
+exist
+i
, &m
, &R
);
2765 M
= Matrix_Alloc(2, P
->Dimension
+2);
2766 value_set_si(M
->p
[0][0], 1);
2767 value_set_si(M
->p
[1][0], 1);
2768 value_set_si(M
->p
[0][1+nvar
+exist
+i
], -1);
2769 value_set_si(M
->p
[1][1+nvar
+exist
+i
], 1);
2770 value_assign(M
->p
[0][1+P
->Dimension
], m
);
2771 value_oppose(M
->p
[1][1+P
->Dimension
], m
);
2772 value_addto(M
->p
[1][1+P
->Dimension
], M
->p
[1][1+P
->Dimension
],
2773 P
->Ray
[r
][1+nvar
+exist
+i
]);
2774 value_decrement(M
->p
[1][1+P
->Dimension
], M
->p
[1][1+P
->Dimension
]);
2775 // Matrix_Print(stderr, P_VALUE_FMT, M);
2776 D
= AddConstraints(M
->p
[0], 2, P
, options
->MaxRays
);
2777 // Polyhedron_Print(stderr, P_VALUE_FMT, D);
2778 value_subtract(M
->p
[0][1+P
->Dimension
], M
->p
[0][1+P
->Dimension
],
2779 P
->Ray
[r
][1+nvar
+exist
+i
]);
2780 // Matrix_Print(stderr, P_VALUE_FMT, M);
2781 S
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
2782 // Polyhedron_Print(stderr, P_VALUE_FMT, S);
2785 evalue
*EP
= barvinok_enumerate_e_with_options(D
, exist
, nparam
, options
);
2790 if (value_notone_p(P
->Ray
[r
][1+nvar
+exist
+i
]))
2791 EP
= enumerate_cyclic(P
, exist
, nparam
, EP
, r
, i
, options
->MaxRays
);
2793 M
= Matrix_Alloc(1, nparam
+2);
2794 value_set_si(M
->p
[0][0], 1);
2795 value_set_si(M
->p
[0][1+i
], 1);
2796 enumerate_vd_add_ray(EP
, M
, options
->MaxRays
);
2801 evalue
*E
= barvinok_enumerate_e_with_options(S
, exist
, nparam
, options
);
2803 free_evalue_refs(E
);
2810 evalue
*ER
= enumerate_or(R
, exist
, nparam
, options
);
2812 free_evalue_refs(ER
);
2819 static evalue
* enumerate_vd(Polyhedron
**PA
,
2820 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2822 Polyhedron
*P
= *PA
;
2823 int nvar
= P
->Dimension
- exist
- nparam
;
2824 Param_Polyhedron
*PP
= NULL
;
2825 Polyhedron
*C
= Universe_Polyhedron(nparam
);
2829 PP
= Polyhedron2Param_SimplifiedDomain(&PR
,C
, options
->MaxRays
,&CEq
,&CT
);
2833 Param_Domain
*D
, *last
;
2836 for (nd
= 0, D
=PP
->D
; D
; D
=D
->next
, ++nd
)
2839 Polyhedron
**VD
= new Polyhedron_p
[nd
];
2840 Polyhedron
**fVD
= new Polyhedron_p
[nd
];
2841 for(nd
= 0, D
=PP
->D
; D
; D
=D
->next
) {
2842 Polyhedron
*rVD
= reduce_domain(D
->Domain
, CT
, CEq
, fVD
, nd
, options
);
2855 /* This doesn't seem to have any effect */
2857 Polyhedron
*CA
= align_context(VD
[0], P
->Dimension
, options
->MaxRays
);
2859 P
= DomainIntersection(P
, CA
, options
->MaxRays
);
2862 Polyhedron_Free(CA
);
2867 if (!EP
&& CT
->NbColumns
!= CT
->NbRows
) {
2868 Polyhedron
*CEqr
= DomainImage(CEq
, CT
, options
->MaxRays
);
2869 Polyhedron
*CA
= align_context(CEqr
, PR
->Dimension
, options
->MaxRays
);
2870 Polyhedron
*I
= DomainIntersection(PR
, CA
, options
->MaxRays
);
2871 Polyhedron_Free(CEqr
);
2872 Polyhedron_Free(CA
);
2874 fprintf(stderr
, "\nER: Eliminate\n");
2875 #endif /* DEBUG_ER */
2876 nparam
-= CT
->NbColumns
- CT
->NbRows
;
2877 EP
= barvinok_enumerate_e_with_options(I
, exist
, nparam
, options
);
2878 nparam
+= CT
->NbColumns
- CT
->NbRows
;
2879 addeliminatedparams_enum(EP
, CT
, CEq
, options
->MaxRays
, nparam
);
2883 Polyhedron_Free(PR
);
2886 if (!EP
&& nd
> 1) {
2888 fprintf(stderr
, "\nER: VD\n");
2889 #endif /* DEBUG_ER */
2890 for (int i
= 0; i
< nd
; ++i
) {
2891 Polyhedron
*CA
= align_context(VD
[i
], P
->Dimension
, options
->MaxRays
);
2892 Polyhedron
*I
= DomainIntersection(P
, CA
, options
->MaxRays
);
2895 EP
= barvinok_enumerate_e_with_options(I
, exist
, nparam
, options
);
2897 evalue
*E
= barvinok_enumerate_e_with_options(I
, exist
, nparam
,
2900 free_evalue_refs(E
);
2904 Polyhedron_Free(CA
);
2908 for (int i
= 0; i
< nd
; ++i
) {
2909 Polyhedron_Free(VD
[i
]);
2910 Polyhedron_Free(fVD
[i
]);
2916 if (!EP
&& nvar
== 0) {
2919 Param_Vertices
*V
, *V2
;
2920 Matrix
* M
= Matrix_Alloc(1, P
->Dimension
+2);
2922 FORALL_PVertex_in_ParamPolyhedron(V
, last
, PP
) {
2924 FORALL_PVertex_in_ParamPolyhedron(V2
, last
, PP
) {
2931 for (int i
= 0; i
< exist
; ++i
) {
2932 value_oppose(f
, V
->Vertex
->p
[i
][nparam
+1]);
2933 Vector_Combine(V
->Vertex
->p
[i
],
2935 M
->p
[0] + 1 + nvar
+ exist
,
2936 V2
->Vertex
->p
[i
][nparam
+1],
2940 for (j
= 0; j
< nparam
; ++j
)
2941 if (value_notzero_p(M
->p
[0][1+nvar
+exist
+j
]))
2945 ConstraintSimplify(M
->p
[0], M
->p
[0],
2946 P
->Dimension
+2, &f
);
2947 value_set_si(M
->p
[0][0], 0);
2948 Polyhedron
*para
= AddConstraints(M
->p
[0], 1, P
,
2951 Polyhedron_Free(para
);
2954 Polyhedron
*pos
, *neg
;
2955 value_set_si(M
->p
[0][0], 1);
2956 value_decrement(M
->p
[0][P
->Dimension
+1],
2957 M
->p
[0][P
->Dimension
+1]);
2958 neg
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
2959 value_set_si(f
, -1);
2960 Vector_Scale(M
->p
[0]+1, M
->p
[0]+1, f
,
2962 value_decrement(M
->p
[0][P
->Dimension
+1],
2963 M
->p
[0][P
->Dimension
+1]);
2964 value_decrement(M
->p
[0][P
->Dimension
+1],
2965 M
->p
[0][P
->Dimension
+1]);
2966 pos
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
2967 if (emptyQ(neg
) && emptyQ(pos
)) {
2968 Polyhedron_Free(para
);
2969 Polyhedron_Free(pos
);
2970 Polyhedron_Free(neg
);
2974 fprintf(stderr
, "\nER: Order\n");
2975 #endif /* DEBUG_ER */
2976 EP
= barvinok_enumerate_e_with_options(para
, exist
, nparam
,
2980 E
= barvinok_enumerate_e_with_options(pos
, exist
, nparam
,
2983 free_evalue_refs(E
);
2987 E
= barvinok_enumerate_e_with_options(neg
, exist
, nparam
,
2990 free_evalue_refs(E
);
2993 Polyhedron_Free(para
);
2994 Polyhedron_Free(pos
);
2995 Polyhedron_Free(neg
);
3000 } END_FORALL_PVertex_in_ParamPolyhedron
;
3003 } END_FORALL_PVertex_in_ParamPolyhedron
;
3006 /* Search for vertex coordinate to split on */
3007 /* First look for one independent of the parameters */
3008 FORALL_PVertex_in_ParamPolyhedron(V
, last
, PP
) {
3009 for (int i
= 0; i
< exist
; ++i
) {
3011 for (j
= 0; j
< nparam
; ++j
)
3012 if (value_notzero_p(V
->Vertex
->p
[i
][j
]))
3016 value_set_si(M
->p
[0][0], 1);
3017 Vector_Set(M
->p
[0]+1, 0, nvar
+exist
);
3018 Vector_Copy(V
->Vertex
->p
[i
],
3019 M
->p
[0] + 1 + nvar
+ exist
, nparam
+1);
3020 value_oppose(M
->p
[0][1+nvar
+i
],
3021 V
->Vertex
->p
[i
][nparam
+1]);
3023 Polyhedron
*pos
, *neg
;
3024 value_set_si(M
->p
[0][0], 1);
3025 value_decrement(M
->p
[0][P
->Dimension
+1],
3026 M
->p
[0][P
->Dimension
+1]);
3027 neg
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
3028 value_set_si(f
, -1);
3029 Vector_Scale(M
->p
[0]+1, M
->p
[0]+1, f
,
3031 value_decrement(M
->p
[0][P
->Dimension
+1],
3032 M
->p
[0][P
->Dimension
+1]);
3033 value_decrement(M
->p
[0][P
->Dimension
+1],
3034 M
->p
[0][P
->Dimension
+1]);
3035 pos
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
3036 if (emptyQ(neg
) || emptyQ(pos
)) {
3037 Polyhedron_Free(pos
);
3038 Polyhedron_Free(neg
);
3041 Polyhedron_Free(pos
);
3042 value_increment(M
->p
[0][P
->Dimension
+1],
3043 M
->p
[0][P
->Dimension
+1]);
3044 pos
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
3046 fprintf(stderr
, "\nER: Vertex\n");
3047 #endif /* DEBUG_ER */
3049 EP
= enumerate_or(pos
, exist
, nparam
, options
);
3054 } END_FORALL_PVertex_in_ParamPolyhedron
;
3058 /* Search for vertex coordinate to split on */
3059 /* Now look for one that depends on the parameters */
3060 FORALL_PVertex_in_ParamPolyhedron(V
, last
, PP
) {
3061 for (int i
= 0; i
< exist
; ++i
) {
3062 value_set_si(M
->p
[0][0], 1);
3063 Vector_Set(M
->p
[0]+1, 0, nvar
+exist
);
3064 Vector_Copy(V
->Vertex
->p
[i
],
3065 M
->p
[0] + 1 + nvar
+ exist
, nparam
+1);
3066 value_oppose(M
->p
[0][1+nvar
+i
],
3067 V
->Vertex
->p
[i
][nparam
+1]);
3069 Polyhedron
*pos
, *neg
;
3070 value_set_si(M
->p
[0][0], 1);
3071 value_decrement(M
->p
[0][P
->Dimension
+1],
3072 M
->p
[0][P
->Dimension
+1]);
3073 neg
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
3074 value_set_si(f
, -1);
3075 Vector_Scale(M
->p
[0]+1, M
->p
[0]+1, f
,
3077 value_decrement(M
->p
[0][P
->Dimension
+1],
3078 M
->p
[0][P
->Dimension
+1]);
3079 value_decrement(M
->p
[0][P
->Dimension
+1],
3080 M
->p
[0][P
->Dimension
+1]);
3081 pos
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
3082 if (emptyQ(neg
) || emptyQ(pos
)) {
3083 Polyhedron_Free(pos
);
3084 Polyhedron_Free(neg
);
3087 Polyhedron_Free(pos
);
3088 value_increment(M
->p
[0][P
->Dimension
+1],
3089 M
->p
[0][P
->Dimension
+1]);
3090 pos
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
3092 fprintf(stderr
, "\nER: ParamVertex\n");
3093 #endif /* DEBUG_ER */
3095 EP
= enumerate_or(pos
, exist
, nparam
, options
);
3100 } END_FORALL_PVertex_in_ParamPolyhedron
;
3108 Polyhedron_Free(CEq
);
3112 Param_Polyhedron_Free(PP
);
3118 evalue
* barvinok_enumerate_pip(Polyhedron
*P
, unsigned exist
, unsigned nparam
,
3122 barvinok_options
*options
= barvinok_options_new_with_defaults();
3123 options
->MaxRays
= MaxRays
;
3124 E
= barvinok_enumerate_pip_with_options(P
, exist
, nparam
, options
);
3125 barvinok_options_free(options
);
3130 evalue
*barvinok_enumerate_pip_with_options(Polyhedron
*P
,
3131 unsigned exist
, unsigned nparam
, struct barvinok_options
*options
)
3136 evalue
*barvinok_enumerate_pip_with_options(Polyhedron
*P
,
3137 unsigned exist
, unsigned nparam
, struct barvinok_options
*options
)
3139 int nvar
= P
->Dimension
- exist
- nparam
;
3140 evalue
*EP
= evalue_zero();
3144 fprintf(stderr
, "\nER: PIP\n");
3145 #endif /* DEBUG_ER */
3147 Polyhedron
*D
= pip_projectout(P
, nvar
, exist
, nparam
);
3148 for (Q
= D
; Q
; Q
= N
) {
3152 exist
= Q
->Dimension
- nvar
- nparam
;
3153 E
= barvinok_enumerate_e_with_options(Q
, exist
, nparam
, options
);
3156 free_evalue_refs(E
);
3165 static bool is_single(Value
*row
, int pos
, int len
)
3167 return First_Non_Zero(row
, pos
) == -1 &&
3168 First_Non_Zero(row
+pos
+1, len
-pos
-1) == -1;
3171 static evalue
* barvinok_enumerate_e_r(Polyhedron
*P
,
3172 unsigned exist
, unsigned nparam
, barvinok_options
*options
);
3175 static int er_level
= 0;
3177 evalue
* barvinok_enumerate_e_with_options(Polyhedron
*P
,
3178 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
3180 fprintf(stderr
, "\nER: level %i\n", er_level
);
3182 Polyhedron_PrintConstraints(stderr
, P_VALUE_FMT
, P
);
3183 fprintf(stderr
, "\nE %d\nP %d\n", exist
, nparam
);
3185 P
= DomainConstraintSimplify(Polyhedron_Copy(P
), options
->MaxRays
);
3186 evalue
*EP
= barvinok_enumerate_e_r(P
, exist
, nparam
, options
);
3192 evalue
* barvinok_enumerate_e_with_options(Polyhedron
*P
,
3193 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
3195 P
= DomainConstraintSimplify(Polyhedron_Copy(P
), options
->MaxRays
);
3196 evalue
*EP
= barvinok_enumerate_e_r(P
, exist
, nparam
, options
);
3202 evalue
* barvinok_enumerate_e(Polyhedron
*P
, unsigned exist
, unsigned nparam
,
3206 barvinok_options
*options
= barvinok_options_new_with_defaults();
3207 options
->MaxRays
= MaxRays
;
3208 E
= barvinok_enumerate_e_with_options(P
, exist
, nparam
, options
);
3209 barvinok_options_free(options
);
3213 static evalue
* barvinok_enumerate_e_r(Polyhedron
*P
,
3214 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
3217 Polyhedron
*U
= Universe_Polyhedron(nparam
);
3218 evalue
*EP
= barvinok_enumerate_with_options(P
, U
, options
);
3219 //char *param_name[] = {"P", "Q", "R", "S", "T" };
3220 //print_evalue(stdout, EP, param_name);
3225 int nvar
= P
->Dimension
- exist
- nparam
;
3226 int len
= P
->Dimension
+ 2;
3229 POL_ENSURE_FACETS(P
);
3230 POL_ENSURE_VERTICES(P
);
3233 return evalue_zero();
3235 if (nvar
== 0 && nparam
== 0) {
3236 evalue
*EP
= evalue_zero();
3237 barvinok_count_with_options(P
, &EP
->x
.n
, options
);
3238 if (value_pos_p(EP
->x
.n
))
3239 value_set_si(EP
->x
.n
, 1);
3244 for (r
= 0; r
< P
->NbRays
; ++r
)
3245 if (value_zero_p(P
->Ray
[r
][0]) ||
3246 value_zero_p(P
->Ray
[r
][P
->Dimension
+1])) {
3248 for (i
= 0; i
< nvar
; ++i
)
3249 if (value_notzero_p(P
->Ray
[r
][i
+1]))
3253 for (i
= nvar
+ exist
; i
< nvar
+ exist
+ nparam
; ++i
)
3254 if (value_notzero_p(P
->Ray
[r
][i
+1]))
3256 if (i
>= nvar
+ exist
+ nparam
)
3259 if (r
< P
->NbRays
) {
3260 evalue
*EP
= evalue_zero();
3261 value_set_si(EP
->x
.n
, -1);
3266 for (r
= 0; r
< P
->NbEq
; ++r
)
3267 if ((first
= First_Non_Zero(P
->Constraint
[r
]+1+nvar
, exist
)) != -1)
3270 if (First_Non_Zero(P
->Constraint
[r
]+1+nvar
+first
+1,
3271 exist
-first
-1) != -1) {
3272 Polyhedron
*T
= rotate_along(P
, r
, nvar
, exist
, options
->MaxRays
);
3274 fprintf(stderr
, "\nER: Equality\n");
3275 #endif /* DEBUG_ER */
3276 evalue
*EP
= barvinok_enumerate_e_with_options(T
, exist
-1, nparam
,
3282 fprintf(stderr
, "\nER: Fixed\n");
3283 #endif /* DEBUG_ER */
3285 return barvinok_enumerate_e_with_options(P
, exist
-1, nparam
,
3288 Polyhedron
*T
= Polyhedron_Copy(P
);
3289 SwapColumns(T
, nvar
+1, nvar
+1+first
);
3290 evalue
*EP
= barvinok_enumerate_e_with_options(T
, exist
-1, nparam
,
3298 Vector
*row
= Vector_Alloc(len
);
3299 value_set_si(row
->p
[0], 1);
3304 enum constraint
* info
= new constraint
[exist
];
3305 for (int i
= 0; i
< exist
; ++i
) {
3307 for (int l
= P
->NbEq
; l
< P
->NbConstraints
; ++l
) {
3308 if (value_negz_p(P
->Constraint
[l
][nvar
+i
+1]))
3310 bool l_parallel
= is_single(P
->Constraint
[l
]+nvar
+1, i
, exist
);
3311 for (int u
= P
->NbEq
; u
< P
->NbConstraints
; ++u
) {
3312 if (value_posz_p(P
->Constraint
[u
][nvar
+i
+1]))
3314 bool lu_parallel
= l_parallel
||
3315 is_single(P
->Constraint
[u
]+nvar
+1, i
, exist
);
3316 value_oppose(f
, P
->Constraint
[u
][nvar
+i
+1]);
3317 Vector_Combine(P
->Constraint
[l
]+1, P
->Constraint
[u
]+1, row
->p
+1,
3318 f
, P
->Constraint
[l
][nvar
+i
+1], len
-1);
3319 if (!(info
[i
] & INDEPENDENT
)) {
3321 for (j
= 0; j
< exist
; ++j
)
3322 if (j
!= i
&& value_notzero_p(row
->p
[nvar
+j
+1]))
3325 //printf("independent: i: %d, l: %d, u: %d\n", i, l, u);
3326 info
[i
] = (constraint
)(info
[i
] | INDEPENDENT
);
3329 if (info
[i
] & ALL_POS
) {
3330 value_addto(row
->p
[len
-1], row
->p
[len
-1],
3331 P
->Constraint
[l
][nvar
+i
+1]);
3332 value_addto(row
->p
[len
-1], row
->p
[len
-1], f
);
3333 value_multiply(f
, f
, P
->Constraint
[l
][nvar
+i
+1]);
3334 value_subtract(row
->p
[len
-1], row
->p
[len
-1], f
);
3335 value_decrement(row
->p
[len
-1], row
->p
[len
-1]);
3336 ConstraintSimplify(row
->p
, row
->p
, len
, &f
);
3337 value_set_si(f
, -1);
3338 Vector_Scale(row
->p
+1, row
->p
+1, f
, len
-1);
3339 value_decrement(row
->p
[len
-1], row
->p
[len
-1]);
3340 Polyhedron
*T
= AddConstraints(row
->p
, 1, P
, options
->MaxRays
);
3342 //printf("not all_pos: i: %d, l: %d, u: %d\n", i, l, u);
3343 info
[i
] = (constraint
)(info
[i
] ^ ALL_POS
);
3345 //puts("pos remainder");
3346 //Polyhedron_Print(stdout, P_VALUE_FMT, T);
3349 if (!(info
[i
] & ONE_NEG
)) {
3351 negative_test_constraint(P
->Constraint
[l
],
3353 row
->p
, nvar
+i
, len
, &f
);
3354 oppose_constraint(row
->p
, len
, &f
);
3355 Polyhedron
*T
= AddConstraints(row
->p
, 1, P
,
3358 //printf("one_neg i: %d, l: %d, u: %d\n", i, l, u);
3359 info
[i
] = (constraint
)(info
[i
] | ONE_NEG
);
3361 //puts("neg remainder");
3362 //Polyhedron_Print(stdout, P_VALUE_FMT, T);
3364 } else if (!(info
[i
] & ROT_NEG
)) {
3365 if (parallel_constraints(P
->Constraint
[l
],
3367 row
->p
, nvar
, exist
)) {
3368 negative_test_constraint7(P
->Constraint
[l
],
3370 row
->p
, nvar
, exist
,
3372 oppose_constraint(row
->p
, len
, &f
);
3373 Polyhedron
*T
= AddConstraints(row
->p
, 1, P
,
3376 // printf("rot_neg i: %d, l: %d, u: %d\n", i, l, u);
3377 info
[i
] = (constraint
)(info
[i
] | ROT_NEG
);
3380 //puts("neg remainder");
3381 //Polyhedron_Print(stdout, P_VALUE_FMT, T);
3386 if (!(info
[i
] & ALL_POS
) && (info
[i
] & (ONE_NEG
| ROT_NEG
)))
3390 if (info
[i
] & ALL_POS
)
3397 for (int i = 0; i < exist; ++i)
3398 printf("%i: %i\n", i, info[i]);
3400 for (int i
= 0; i
< exist
; ++i
)
3401 if (info
[i
] & ALL_POS
) {
3403 fprintf(stderr
, "\nER: Positive\n");
3404 #endif /* DEBUG_ER */
3406 // Maybe we should chew off some of the fat here
3407 Matrix
*M
= Matrix_Alloc(P
->Dimension
, P
->Dimension
+1);
3408 for (int j
= 0; j
< P
->Dimension
; ++j
)
3409 value_set_si(M
->p
[j
][j
+ (j
>= i
+nvar
)], 1);
3410 Polyhedron
*T
= Polyhedron_Image(P
, M
, options
->MaxRays
);
3412 evalue
*EP
= barvinok_enumerate_e_with_options(T
, exist
-1, nparam
,
3420 for (int i
= 0; i
< exist
; ++i
)
3421 if (info
[i
] & ONE_NEG
) {
3423 fprintf(stderr
, "\nER: Negative\n");
3424 #endif /* DEBUG_ER */
3429 return barvinok_enumerate_e_with_options(P
, exist
-1, nparam
,
3432 Polyhedron
*T
= Polyhedron_Copy(P
);
3433 SwapColumns(T
, nvar
+1, nvar
+1+i
);
3434 evalue
*EP
= barvinok_enumerate_e_with_options(T
, exist
-1, nparam
,
3440 for (int i
= 0; i
< exist
; ++i
)
3441 if (info
[i
] & ROT_NEG
) {
3443 fprintf(stderr
, "\nER: Rotate\n");
3444 #endif /* DEBUG_ER */
3448 Polyhedron
*T
= rotate_along(P
, r
, nvar
, exist
, options
->MaxRays
);
3449 evalue
*EP
= barvinok_enumerate_e_with_options(T
, exist
-1, nparam
,
3454 for (int i
= 0; i
< exist
; ++i
)
3455 if (info
[i
] & INDEPENDENT
) {
3456 Polyhedron
*pos
, *neg
;
3458 /* Find constraint again and split off negative part */
3460 if (SplitOnVar(P
, i
, nvar
, exist
, options
->MaxRays
,
3461 row
, f
, true, &pos
, &neg
)) {
3463 fprintf(stderr
, "\nER: Split\n");
3464 #endif /* DEBUG_ER */
3467 barvinok_enumerate_e_with_options(neg
, exist
-1, nparam
, options
);
3469 barvinok_enumerate_e_with_options(pos
, exist
, nparam
, options
);
3471 free_evalue_refs(E
);
3473 Polyhedron_Free(neg
);
3474 Polyhedron_Free(pos
);
3488 EP
= enumerate_line(P
, exist
, nparam
, options
);
3492 EP
= barvinok_enumerate_pip_with_options(P
, exist
, nparam
, options
);
3496 EP
= enumerate_redundant_ray(P
, exist
, nparam
, options
);
3500 EP
= enumerate_sure(P
, exist
, nparam
, options
);
3504 EP
= enumerate_ray(P
, exist
, nparam
, options
);
3508 EP
= enumerate_sure2(P
, exist
, nparam
, options
);
3512 F
= unfringe(P
, options
->MaxRays
);
3513 if (!PolyhedronIncludes(F
, P
)) {
3515 fprintf(stderr
, "\nER: Fringed\n");
3516 #endif /* DEBUG_ER */
3517 EP
= barvinok_enumerate_e_with_options(F
, exist
, nparam
, options
);
3524 EP
= enumerate_vd(&P
, exist
, nparam
, options
);
3529 EP
= enumerate_sum(P
, exist
, nparam
, options
);
3536 Polyhedron
*pos
, *neg
;
3537 for (i
= 0; i
< exist
; ++i
)
3538 if (SplitOnVar(P
, i
, nvar
, exist
, options
->MaxRays
,
3539 row
, f
, false, &pos
, &neg
))
3545 EP
= enumerate_or(pos
, exist
, nparam
, options
);
3558 * remove equalities that require a "compression" of the parameters
3560 static Polyhedron
*remove_more_equalities(Polyhedron
*P
, unsigned nparam
,
3561 Matrix
**CP
, unsigned MaxRays
)
3564 remove_all_equalities(&P
, NULL
, CP
, NULL
, nparam
, MaxRays
);
3571 static gen_fun
*series(Polyhedron
*P
, unsigned nparam
, barvinok_options
*options
)
3581 assert(!Polyhedron_is_unbounded(P
, nparam
, options
->MaxRays
));
3582 assert(P
->NbBid
== 0);
3583 assert(Polyhedron_has_revlex_positive_rays(P
, nparam
));
3585 P
= remove_more_equalities(P
, nparam
, &CP
, options
->MaxRays
);
3586 assert(P
->NbEq
== 0);
3588 nparam
= CP
->NbColumns
-1;
3593 barvinok_count_with_options(P
, &c
, options
);
3594 gf
= new gen_fun(c
);
3598 red
= gf_base::create(Polyhedron_Project(P
, nparam
),
3599 P
->Dimension
, nparam
, options
);
3600 POL_ENSURE_VERTICES(P
);
3601 red
->start_gf(P
, options
);
3613 gen_fun
* barvinok_series_with_options(Polyhedron
*P
, Polyhedron
* C
,
3614 barvinok_options
*options
)
3617 unsigned nparam
= C
->Dimension
;
3620 CA
= align_context(C
, P
->Dimension
, options
->MaxRays
);
3621 P
= DomainIntersection(P
, CA
, options
->MaxRays
);
3622 Polyhedron_Free(CA
);
3624 gf
= series(P
, nparam
, options
);
3629 gen_fun
* barvinok_series(Polyhedron
*P
, Polyhedron
* C
, unsigned MaxRays
)
3632 barvinok_options
*options
= barvinok_options_new_with_defaults();
3633 options
->MaxRays
= MaxRays
;
3634 gf
= barvinok_series_with_options(P
, C
, options
);
3635 barvinok_options_free(options
);
3639 static Polyhedron
*skew_into_positive_orthant(Polyhedron
*D
, unsigned nparam
,
3645 for (Polyhedron
*P
= D
; P
; P
= P
->next
) {
3646 POL_ENSURE_VERTICES(P
);
3647 assert(!Polyhedron_is_unbounded(P
, nparam
, MaxRays
));
3648 assert(P
->NbBid
== 0);
3649 assert(Polyhedron_has_positive_rays(P
, nparam
));
3651 for (int r
= 0; r
< P
->NbRays
; ++r
) {
3652 if (value_notzero_p(P
->Ray
[r
][P
->Dimension
+1]))
3654 for (int i
= 0; i
< nparam
; ++i
) {
3656 if (value_posz_p(P
->Ray
[r
][i
+1]))
3659 M
= Matrix_Alloc(D
->Dimension
+1, D
->Dimension
+1);
3660 for (int i
= 0; i
< D
->Dimension
+1; ++i
)
3661 value_set_si(M
->p
[i
][i
], 1);
3663 Inner_Product(P
->Ray
[r
]+1, M
->p
[i
], D
->Dimension
+1, &tmp
);
3664 if (value_posz_p(tmp
))
3667 for (j
= P
->Dimension
- nparam
; j
< P
->Dimension
; ++j
)
3668 if (value_pos_p(P
->Ray
[r
][j
+1]))
3670 assert(j
< P
->Dimension
);
3671 value_pdivision(tmp
, P
->Ray
[r
][j
+1], P
->Ray
[r
][i
+1]);
3672 value_subtract(M
->p
[i
][j
], M
->p
[i
][j
], tmp
);
3678 D
= DomainImage(D
, M
, MaxRays
);
3684 gen_fun
* barvinok_enumerate_union_series_with_options(Polyhedron
*D
, Polyhedron
* C
,
3685 barvinok_options
*options
)
3687 Polyhedron
*conv
, *D2
;
3689 gen_fun
*gf
= NULL
, *gf2
;
3690 unsigned nparam
= C
->Dimension
;
3695 CA
= align_context(C
, D
->Dimension
, options
->MaxRays
);
3696 D
= DomainIntersection(D
, CA
, options
->MaxRays
);
3697 Polyhedron_Free(CA
);
3699 D2
= skew_into_positive_orthant(D
, nparam
, options
->MaxRays
);
3700 for (Polyhedron
*P
= D2
; P
; P
= P
->next
) {
3701 assert(P
->Dimension
== D2
->Dimension
);
3704 P_gf
= series(Polyhedron_Copy(P
), nparam
, options
);
3708 gf
->add_union(P_gf
, options
);
3712 /* we actually only need the convex union of the parameter space
3713 * but the reducer classes currently expect a polyhedron in
3714 * the combined space
3716 Polyhedron_Free(gf
->context
);
3717 gf
->context
= DomainConvex(D2
, options
->MaxRays
);
3719 gf2
= gf
->summate(D2
->Dimension
- nparam
, options
);
3728 gen_fun
* barvinok_enumerate_union_series(Polyhedron
*D
, Polyhedron
* C
,
3732 barvinok_options
*options
= barvinok_options_new_with_defaults();
3733 options
->MaxRays
= MaxRays
;
3734 gf
= barvinok_enumerate_union_series_with_options(D
, C
, options
);
3735 barvinok_options_free(options
);
3739 evalue
* barvinok_enumerate_union(Polyhedron
*D
, Polyhedron
* C
, unsigned MaxRays
)
3742 gen_fun
*gf
= barvinok_enumerate_union_series(D
, C
, MaxRays
);