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;
705 value_set_si(*result
, 0);
711 P
= remove_equalities(P
);
712 P
= DomainConstraintSimplify(P
, options
->MaxRays
);
716 } while (!emptyQ(P
) && P
->NbEq
!= 0);
719 value_set_si(*result
, 0);
724 if (Polyhedron_is_infinite(P
, result
, options
)) {
729 if (P
->Dimension
== 0) {
730 /* Test whether the constraints are satisfied */
731 POL_ENSURE_VERTICES(P
);
732 value_set_si(*result
, !emptyQ(P
));
737 Q
= Polyhedron_Factor(P
, 0, options
->MaxRays
);
745 barvinok_count_f(P
, result
, options
);
746 if (value_neg_p(*result
))
748 if (Q
&& P
->next
&& value_notzero_p(*result
)) {
752 for (Q
= P
->next
; Q
; Q
= Q
->next
) {
753 barvinok_count_f(Q
, &factor
, options
);
754 if (value_neg_p(factor
)) {
757 } else if (Q
->next
&& value_zero_p(factor
)) {
758 value_set_si(*result
, 0);
761 value_multiply(*result
, *result
, factor
);
770 value_set_si(*result
, -1);
773 void barvinok_count(Polyhedron
*P
, Value
* result
, unsigned NbMaxCons
)
775 barvinok_options
*options
= barvinok_options_new_with_defaults();
776 options
->MaxRays
= NbMaxCons
;
777 barvinok_count_with_options(P
, result
, options
);
778 barvinok_options_free(options
);
781 static void barvinok_count_f(Polyhedron
*P
, Value
* result
,
782 barvinok_options
*options
)
785 value_set_si(*result
, 0);
789 if (P
->Dimension
== 1)
790 return Line_Length(P
, result
);
792 int c
= P
->NbConstraints
;
793 POL_ENSURE_FACETS(P
);
794 if (c
!= P
->NbConstraints
|| P
->NbEq
!= 0)
795 return barvinok_count_with_options(P
, result
, options
);
797 POL_ENSURE_VERTICES(P
);
799 if (Polyhedron_is_infinite(P
, result
, options
))
803 if (options
->incremental_specialization
== 2)
804 cnt
= new bfcounter(P
->Dimension
);
805 else if (options
->incremental_specialization
== 1)
806 cnt
= new icounter(P
->Dimension
);
808 cnt
= new counter(P
->Dimension
);
809 cnt
->start(P
, options
);
811 cnt
->get_count(result
);
815 static void uni_polynom(int param
, Vector
*c
, evalue
*EP
)
817 unsigned dim
= c
->Size
-2;
819 value_set_si(EP
->d
,0);
820 EP
->x
.p
= new_enode(polynomial
, dim
+1, param
+1);
821 for (int j
= 0; j
<= dim
; ++j
)
822 evalue_set(&EP
->x
.p
->arr
[j
], c
->p
[j
], c
->p
[dim
+1]);
825 static void multi_polynom(Vector
*c
, evalue
* X
, evalue
*EP
)
827 unsigned dim
= c
->Size
-2;
831 evalue_set(&EC
, c
->p
[dim
], c
->p
[dim
+1]);
834 evalue_set(EP
, c
->p
[dim
], c
->p
[dim
+1]);
836 for (int i
= dim
-1; i
>= 0; --i
) {
838 value_assign(EC
.x
.n
, c
->p
[i
]);
841 free_evalue_refs(&EC
);
844 Polyhedron
*unfringe (Polyhedron
*P
, unsigned MaxRays
)
846 int len
= P
->Dimension
+2;
847 Polyhedron
*T
, *R
= P
;
850 Vector
*row
= Vector_Alloc(len
);
851 value_set_si(row
->p
[0], 1);
853 R
= DomainConstraintSimplify(Polyhedron_Copy(P
), MaxRays
);
855 Matrix
*M
= Matrix_Alloc(2, len
-1);
856 value_set_si(M
->p
[1][len
-2], 1);
857 for (int v
= 0; v
< P
->Dimension
; ++v
) {
858 value_set_si(M
->p
[0][v
], 1);
859 Polyhedron
*I
= Polyhedron_Image(R
, M
, 2+1);
860 value_set_si(M
->p
[0][v
], 0);
861 for (int r
= 0; r
< I
->NbConstraints
; ++r
) {
862 if (value_zero_p(I
->Constraint
[r
][0]))
864 if (value_zero_p(I
->Constraint
[r
][1]))
866 if (value_one_p(I
->Constraint
[r
][1]))
868 if (value_mone_p(I
->Constraint
[r
][1]))
870 value_absolute(g
, I
->Constraint
[r
][1]);
871 Vector_Set(row
->p
+1, 0, len
-2);
872 value_division(row
->p
[1+v
], I
->Constraint
[r
][1], g
);
873 mpz_fdiv_q(row
->p
[len
-1], I
->Constraint
[r
][2], g
);
875 R
= AddConstraints(row
->p
, 1, R
, MaxRays
);
887 /* this procedure may have false negatives */
888 static bool Polyhedron_is_infinite_param(Polyhedron
*P
, unsigned nparam
)
891 for (r
= 0; r
< P
->NbRays
; ++r
) {
892 if (!value_zero_p(P
->Ray
[r
][0]) &&
893 !value_zero_p(P
->Ray
[r
][P
->Dimension
+1]))
895 if (First_Non_Zero(P
->Ray
[r
]+1+P
->Dimension
-nparam
, nparam
) == -1)
901 /* Check whether all rays point in the positive directions
904 static bool Polyhedron_has_positive_rays(Polyhedron
*P
, unsigned nparam
)
907 for (r
= 0; r
< P
->NbRays
; ++r
)
908 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1])) {
910 for (i
= P
->Dimension
- nparam
; i
< P
->Dimension
; ++i
)
911 if (value_neg_p(P
->Ray
[r
][i
+1]))
917 typedef evalue
* evalue_p
;
919 struct enumerator_base
{
923 vertex_decomposer
*vpd
;
925 enumerator_base(unsigned dim
, vertex_decomposer
*vpd
)
930 vE
= new evalue_p
[vpd
->nbV
];
931 for (int j
= 0; j
< vpd
->nbV
; ++j
)
935 evalue_set_si(&mone
, -1, 1);
938 void decompose_at(Param_Vertices
*V
, int _i
, barvinok_options
*options
) {
942 value_init(vE
[_i
]->d
);
943 evalue_set_si(vE
[_i
], 0, 1);
945 vpd
->decompose_at_vertex(V
, _i
, options
);
948 virtual ~enumerator_base() {
949 for (int j
= 0; j
< vpd
->nbV
; ++j
)
951 free_evalue_refs(vE
[j
]);
956 free_evalue_refs(&mone
);
959 static enumerator_base
*create(Polyhedron
*P
, unsigned dim
, unsigned nbV
,
960 barvinok_options
*options
);
963 struct enumerator
: public signed_cone_consumer
, public vertex_decomposer
,
964 public enumerator_base
{
972 enumerator(Polyhedron
*P
, unsigned dim
, unsigned nbV
) :
973 vertex_decomposer(P
, nbV
, *this), enumerator_base(dim
, this) {
976 randomvector(P
, lambda
, dim
);
978 c
= Vector_Alloc(dim
+2);
988 virtual void handle(const signed_cone
& sc
, barvinok_options
*options
);
991 void enumerator::handle(const signed_cone
& sc
, barvinok_options
*options
)
996 assert(sc
.rays
.NumRows() == dim
);
997 for (int k
= 0; k
< dim
; ++k
) {
998 if (lambda
* sc
.rays
[k
] == 0)
1004 lattice_point(V
, sc
.rays
, lambda
, &num
, 0, options
);
1005 den
= sc
.rays
* lambda
;
1006 normalize(sign
, num
.constant
, den
);
1008 dpoly
n(dim
, den
[0], 1);
1009 for (int k
= 1; k
< dim
; ++k
) {
1010 dpoly
fact(dim
, den
[k
], 1);
1013 if (num
.E
!= NULL
) {
1014 ZZ
one(INIT_VAL
, 1);
1015 dpoly_n
d(dim
, num
.constant
, one
);
1018 multi_polynom(c
, num
.E
, &EV
);
1019 eadd(&EV
, vE
[vert
]);
1020 free_evalue_refs(&EV
);
1021 free_evalue_refs(num
.E
);
1023 } else if (num
.pos
!= -1) {
1024 dpoly_n
d(dim
, num
.constant
, num
.coeff
);
1027 uni_polynom(num
.pos
, c
, &EV
);
1028 eadd(&EV
, vE
[vert
]);
1029 free_evalue_refs(&EV
);
1031 mpq_set_si(count
, 0, 1);
1032 dpoly
d(dim
, num
.constant
);
1033 d
.div(n
, count
, sign
);
1036 evalue_set(&EV
, &count
[0]._mp_num
, &count
[0]._mp_den
);
1037 eadd(&EV
, vE
[vert
]);
1038 free_evalue_refs(&EV
);
1042 struct ienumerator_base
: enumerator_base
{
1045 ienumerator_base(unsigned dim
, vertex_decomposer
*vpd
) :
1046 enumerator_base(dim
,vpd
) {
1047 E_vertex
= new evalue_p
[dim
];
1050 virtual ~ienumerator_base() {
1054 evalue
*E_num(int i
, int d
) {
1055 return E_vertex
[i
+ (dim
-d
)];
1064 cumulator(evalue
*factor
, evalue
*v
, dpoly_r
*r
) :
1065 factor(factor
), v(v
), r(r
) {}
1067 void cumulate(barvinok_options
*options
);
1069 virtual void add_term(const vector
<int>& powers
, evalue
*f2
) = 0;
1072 void cumulator::cumulate(barvinok_options
*options
)
1074 evalue cum
; // factor * 1 * E_num[0]/1 * (E_num[0]-1)/2 *...
1076 evalue t
; // E_num[0] - (m-1)
1080 if (options
->lookup_table
) {
1082 evalue_set_si(&mone
, -1, 1);
1086 evalue_copy(&cum
, factor
);
1089 value_set_si(f
.d
, 1);
1090 value_set_si(f
.x
.n
, 1);
1094 if (!options
->lookup_table
) {
1095 for (cst
= &t
; value_zero_p(cst
->d
); ) {
1096 if (cst
->x
.p
->type
== fractional
)
1097 cst
= &cst
->x
.p
->arr
[1];
1099 cst
= &cst
->x
.p
->arr
[0];
1103 for (int m
= 0; m
< r
->len
; ++m
) {
1106 value_set_si(f
.d
, m
);
1108 if (!options
->lookup_table
)
1109 value_subtract(cst
->x
.n
, cst
->x
.n
, cst
->d
);
1115 dpoly_r_term_list
& current
= r
->c
[r
->len
-1-m
];
1116 dpoly_r_term_list::iterator j
;
1117 for (j
= current
.begin(); j
!= current
.end(); ++j
) {
1118 if ((*j
)->coeff
== 0)
1120 evalue
*f2
= new evalue
;
1122 value_init(f2
->x
.n
);
1123 zz2value((*j
)->coeff
, f2
->x
.n
);
1124 zz2value(r
->denom
, f2
->d
);
1127 add_term((*j
)->powers
, f2
);
1130 free_evalue_refs(&f
);
1131 free_evalue_refs(&t
);
1132 free_evalue_refs(&cum
);
1133 if (options
->lookup_table
)
1134 free_evalue_refs(&mone
);
1137 struct E_poly_term
{
1142 struct ie_cum
: public cumulator
{
1143 vector
<E_poly_term
*> terms
;
1145 ie_cum(evalue
*factor
, evalue
*v
, dpoly_r
*r
) : cumulator(factor
, v
, r
) {}
1147 virtual void add_term(const vector
<int>& powers
, evalue
*f2
);
1150 void ie_cum::add_term(const vector
<int>& powers
, evalue
*f2
)
1153 for (k
= 0; k
< terms
.size(); ++k
) {
1154 if (terms
[k
]->powers
== powers
) {
1155 eadd(f2
, terms
[k
]->E
);
1156 free_evalue_refs(f2
);
1161 if (k
>= terms
.size()) {
1162 E_poly_term
*ET
= new E_poly_term
;
1163 ET
->powers
= powers
;
1165 terms
.push_back(ET
);
1169 struct ienumerator
: public signed_cone_consumer
, public vertex_decomposer
,
1170 public ienumerator_base
{
1176 ienumerator(Polyhedron
*P
, unsigned dim
, unsigned nbV
) :
1177 vertex_decomposer(P
, nbV
, *this), ienumerator_base(dim
, this) {
1178 vertex
.SetDims(1, dim
);
1180 den
.SetDims(dim
, dim
);
1188 virtual void handle(const signed_cone
& sc
, barvinok_options
*options
);
1189 void reduce(evalue
*factor
, const mat_ZZ
& num
, const mat_ZZ
& den_f
,
1190 barvinok_options
*options
);
1193 void ienumerator::reduce(evalue
*factor
, const mat_ZZ
& num
, const mat_ZZ
& den_f
,
1194 barvinok_options
*options
)
1196 unsigned len
= den_f
.NumRows(); // number of factors in den
1197 unsigned dim
= num
.NumCols();
1198 assert(num
.NumRows() == 1);
1201 eadd(factor
, vE
[vert
]);
1210 split_one(num
, num_s
, num_p
, den_f
, den_s
, den_r
);
1213 den_p
.SetLength(len
);
1217 normalize(one
, num_s
, num_p
, den_s
, den_p
, den_r
);
1219 emul(&mone
, factor
);
1223 for (int k
= 0; k
< len
; ++k
) {
1226 else if (den_s
[k
] == 0)
1229 if (no_param
== 0) {
1230 reduce(factor
, num_p
, den_r
, options
);
1234 pden
.SetDims(only_param
, dim
-1);
1236 for (k
= 0, l
= 0; k
< len
; ++k
)
1238 pden
[l
++] = den_r
[k
];
1240 for (k
= 0; k
< len
; ++k
)
1244 dpoly
n(no_param
, num_s
[0]);
1245 dpoly
D(no_param
, den_s
[k
], 1);
1246 for ( ; ++k
< len
; )
1247 if (den_p
[k
] == 0) {
1248 dpoly
fact(no_param
, den_s
[k
], 1);
1253 // if no_param + only_param == len then all powers
1254 // below will be all zero
1255 if (no_param
+ only_param
== len
) {
1256 if (E_num(0, dim
) != 0)
1257 r
= new dpoly_r(n
, len
);
1259 mpq_set_si(tcount
, 0, 1);
1261 n
.div(D
, tcount
, one
);
1263 if (value_notzero_p(mpq_numref(tcount
))) {
1267 value_assign(f
.x
.n
, mpq_numref(tcount
));
1268 value_assign(f
.d
, mpq_denref(tcount
));
1270 reduce(factor
, num_p
, pden
, options
);
1271 free_evalue_refs(&f
);
1276 for (k
= 0; k
< len
; ++k
) {
1277 if (den_s
[k
] == 0 || den_p
[k
] == 0)
1280 dpoly
pd(no_param
-1, den_s
[k
], 1);
1283 for (l
= 0; l
< k
; ++l
)
1284 if (den_r
[l
] == den_r
[k
])
1288 r
= new dpoly_r(n
, pd
, l
, len
);
1290 dpoly_r
*nr
= new dpoly_r(r
, pd
, l
, len
);
1296 dpoly_r
*rc
= r
->div(D
);
1299 if (E_num(0, dim
) == 0) {
1300 int common
= pden
.NumRows();
1301 dpoly_r_term_list
& final
= r
->c
[r
->len
-1];
1307 zz2value(r
->denom
, f
.d
);
1308 dpoly_r_term_list::iterator j
;
1309 for (j
= final
.begin(); j
!= final
.end(); ++j
) {
1310 if ((*j
)->coeff
== 0)
1313 for (int k
= 0; k
< r
->dim
; ++k
) {
1314 int n
= (*j
)->powers
[k
];
1317 pden
.SetDims(rows
+n
, pden
.NumCols());
1318 for (int l
= 0; l
< n
; ++l
)
1319 pden
[rows
+l
] = den_r
[k
];
1323 evalue_copy(&t
, factor
);
1324 zz2value((*j
)->coeff
, f
.x
.n
);
1326 reduce(&t
, num_p
, pden
, options
);
1327 free_evalue_refs(&t
);
1329 free_evalue_refs(&f
);
1331 ie_cum
cum(factor
, E_num(0, dim
), r
);
1332 cum
.cumulate(options
);
1334 int common
= pden
.NumRows();
1336 for (int j
= 0; j
< cum
.terms
.size(); ++j
) {
1338 pden
.SetDims(rows
, pden
.NumCols());
1339 for (int k
= 0; k
< r
->dim
; ++k
) {
1340 int n
= cum
.terms
[j
]->powers
[k
];
1343 pden
.SetDims(rows
+n
, pden
.NumCols());
1344 for (int l
= 0; l
< n
; ++l
)
1345 pden
[rows
+l
] = den_r
[k
];
1348 reduce(cum
.terms
[j
]->E
, num_p
, pden
, options
);
1349 free_evalue_refs(cum
.terms
[j
]->E
);
1350 delete cum
.terms
[j
]->E
;
1351 delete cum
.terms
[j
];
1358 static int type_offset(enode
*p
)
1360 return p
->type
== fractional
? 1 :
1361 p
->type
== flooring
? 1 : 0;
1364 static int edegree(evalue
*e
)
1369 if (value_notzero_p(e
->d
))
1373 int i
= type_offset(p
);
1374 if (p
->size
-i
-1 > d
)
1375 d
= p
->size
- i
- 1;
1376 for (; i
< p
->size
; i
++) {
1377 int d2
= edegree(&p
->arr
[i
]);
1384 void ienumerator::handle(const signed_cone
& sc
, barvinok_options
*options
)
1386 assert(sc
.det
== 1);
1388 assert(sc
.rays
.NumRows() == dim
);
1390 lattice_point(V
, sc
.rays
, vertex
[0], E_vertex
, options
);
1396 evalue_set_si(&one
, sc
.sign
, 1);
1397 reduce(&one
, vertex
, den
, options
);
1398 free_evalue_refs(&one
);
1400 for (int i
= 0; i
< dim
; ++i
)
1402 free_evalue_refs(E_vertex
[i
]);
1407 struct bfenumerator
: public vertex_decomposer
, public bf_base
,
1408 public ienumerator_base
{
1411 bfenumerator(Polyhedron
*P
, unsigned dim
, unsigned nbV
) :
1412 vertex_decomposer(P
, nbV
, *this),
1413 bf_base(dim
), ienumerator_base(dim
, this) {
1421 virtual void handle(const signed_cone
& sc
, barvinok_options
*options
);
1422 virtual void base(mat_ZZ
& factors
, bfc_vec
& v
);
1424 bfc_term_base
* new_bf_term(int len
) {
1425 bfe_term
* t
= new bfe_term(len
);
1429 virtual void set_factor(bfc_term_base
*t
, int k
, 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
;
1435 emul(&mone
, factor
);
1438 virtual void set_factor(bfc_term_base
*t
, int k
, mpq_t
&q
, int change
) {
1439 bfe_term
* bfet
= static_cast<bfe_term
*>(t
);
1440 factor
= bfet
->factors
[k
];
1441 assert(factor
!= NULL
);
1442 bfet
->factors
[k
] = NULL
;
1448 value_oppose(f
.x
.n
, mpq_numref(q
));
1450 value_assign(f
.x
.n
, mpq_numref(q
));
1451 value_assign(f
.d
, mpq_denref(q
));
1453 free_evalue_refs(&f
);
1456 virtual void set_factor(bfc_term_base
*t
, int k
, const QQ
& c
, int change
) {
1457 bfe_term
* bfet
= static_cast<bfe_term
*>(t
);
1459 factor
= new evalue
;
1464 zz2value(c
.n
, f
.x
.n
);
1466 value_oppose(f
.x
.n
, f
.x
.n
);
1469 value_init(factor
->d
);
1470 evalue_copy(factor
, bfet
->factors
[k
]);
1472 free_evalue_refs(&f
);
1475 void set_factor(evalue
*f
, int change
) {
1481 virtual void insert_term(bfc_term_base
*t
, int i
) {
1482 bfe_term
* bfet
= static_cast<bfe_term
*>(t
);
1483 int len
= t
->terms
.NumRows()-1; // already increased by one
1485 bfet
->factors
.resize(len
+1);
1486 for (int j
= len
; j
> i
; --j
) {
1487 bfet
->factors
[j
] = bfet
->factors
[j
-1];
1488 t
->terms
[j
] = t
->terms
[j
-1];
1490 bfet
->factors
[i
] = factor
;
1494 virtual void update_term(bfc_term_base
*t
, int i
) {
1495 bfe_term
* bfet
= static_cast<bfe_term
*>(t
);
1497 eadd(factor
, bfet
->factors
[i
]);
1498 free_evalue_refs(factor
);
1502 virtual bool constant_vertex(int dim
) { return E_num(0, dim
) == 0; }
1504 virtual void cum(bf_reducer
*bfr
, bfc_term_base
*t
, int k
, dpoly_r
*r
,
1505 barvinok_options
*options
);
1508 enumerator_base
*enumerator_base::create(Polyhedron
*P
, unsigned dim
, unsigned nbV
,
1509 barvinok_options
*options
)
1511 enumerator_base
*eb
;
1513 if (options
->incremental_specialization
== BV_SPECIALIZATION_BF
)
1514 eb
= new bfenumerator(P
, dim
, nbV
);
1515 else if (options
->incremental_specialization
== BV_SPECIALIZATION_DF
)
1516 eb
= new ienumerator(P
, dim
, nbV
);
1518 eb
= new enumerator(P
, dim
, nbV
);
1523 struct bfe_cum
: public cumulator
{
1525 bfc_term_base
*told
;
1529 bfe_cum(evalue
*factor
, evalue
*v
, dpoly_r
*r
, bf_reducer
*bfr
,
1530 bfc_term_base
*t
, int k
, bfenumerator
*e
) :
1531 cumulator(factor
, v
, r
), told(t
), k(k
),
1535 virtual void add_term(const vector
<int>& powers
, evalue
*f2
);
1538 void bfe_cum::add_term(const vector
<int>& powers
, evalue
*f2
)
1540 bfr
->update_powers(powers
);
1542 bfc_term_base
* t
= bfe
->find_bfc_term(bfr
->vn
, bfr
->npowers
, bfr
->nnf
);
1543 bfe
->set_factor(f2
, bfr
->l_changes
% 2);
1544 bfe
->add_term(t
, told
->terms
[k
], bfr
->l_extra_num
);
1547 void bfenumerator::cum(bf_reducer
*bfr
, bfc_term_base
*t
, int k
,
1548 dpoly_r
*r
, barvinok_options
*options
)
1550 bfe_term
* bfet
= static_cast<bfe_term
*>(t
);
1551 bfe_cum
cum(bfet
->factors
[k
], E_num(0, bfr
->d
), r
, bfr
, t
, k
, this);
1552 cum
.cumulate(options
);
1555 void bfenumerator::base(mat_ZZ
& factors
, bfc_vec
& v
)
1557 for (int i
= 0; i
< v
.size(); ++i
) {
1558 assert(v
[i
]->terms
.NumRows() == 1);
1559 evalue
*factor
= static_cast<bfe_term
*>(v
[i
])->factors
[0];
1560 eadd(factor
, vE
[vert
]);
1565 void bfenumerator::handle(const signed_cone
& sc
, barvinok_options
*options
)
1567 assert(sc
.det
== 1);
1569 assert(sc
.rays
.NumRows() == enumerator_base::dim
);
1571 bfe_term
* t
= new bfe_term(enumerator_base::dim
);
1572 vector
< bfc_term_base
* > v
;
1575 t
->factors
.resize(1);
1577 t
->terms
.SetDims(1, enumerator_base::dim
);
1578 lattice_point(V
, sc
.rays
, t
->terms
[0], E_vertex
, options
);
1580 // the elements of factors are always lexpositive
1582 int s
= setup_factors(sc
.rays
, factors
, t
, sc
.sign
);
1584 t
->factors
[0] = new evalue
;
1585 value_init(t
->factors
[0]->d
);
1586 evalue_set_si(t
->factors
[0], s
, 1);
1587 reduce(factors
, v
, options
);
1589 for (int i
= 0; i
< enumerator_base::dim
; ++i
)
1591 free_evalue_refs(E_vertex
[i
]);
1596 #ifdef HAVE_CORRECT_VERTICES
1597 static inline Param_Polyhedron
*Polyhedron2Param_SD(Polyhedron
**Din
,
1598 Polyhedron
*Cin
,int WS
,Polyhedron
**CEq
,Matrix
**CT
)
1600 if (WS
& POL_NO_DUAL
)
1602 return Polyhedron2Param_SimplifiedDomain(Din
, Cin
, WS
, CEq
, CT
);
1605 static Param_Polyhedron
*Polyhedron2Param_SD(Polyhedron
**Din
,
1606 Polyhedron
*Cin
,int WS
,Polyhedron
**CEq
,Matrix
**CT
)
1608 static char data
[] = " 1 0 0 0 0 1 -18 "
1609 " 1 0 0 -20 0 19 1 "
1610 " 1 0 1 20 0 -20 16 "
1613 " 1 4 -20 0 0 -1 23 "
1614 " 1 -4 20 0 0 1 -22 "
1615 " 1 0 1 0 20 -20 16 "
1616 " 1 0 0 0 -20 19 1 ";
1617 static int checked
= 0;
1622 Matrix
*M
= Matrix_Alloc(9, 7);
1623 for (i
= 0; i
< 9; ++i
)
1624 for (int j
= 0; j
< 7; ++j
) {
1625 sscanf(p
, "%d%n", &v
, &n
);
1627 value_set_si(M
->p
[i
][j
], v
);
1629 Polyhedron
*P
= Constraints2Polyhedron(M
, 1024);
1631 Polyhedron
*U
= Universe_Polyhedron(1);
1632 Param_Polyhedron
*PP
= Polyhedron2Param_Domain(P
, U
, 1024);
1636 for (i
= 0, V
= PP
->V
; V
; ++i
, V
= V
->next
)
1639 Param_Polyhedron_Free(PP
);
1641 fprintf(stderr
, "WARNING: results may be incorrect\n");
1643 "WARNING: use latest version of PolyLib to remove this warning\n");
1647 return Polyhedron2Param_SimplifiedDomain(Din
, Cin
, WS
, CEq
, CT
);
1651 static evalue
* barvinok_enumerate_ev_f(Polyhedron
*P
, Polyhedron
* C
,
1652 barvinok_options
*options
);
1655 static evalue
* barvinok_enumerate_cst(Polyhedron
*P
, Polyhedron
* C
,
1656 struct barvinok_options
*options
)
1660 ALLOC(evalue
, eres
);
1661 value_init(eres
->d
);
1662 value_set_si(eres
->d
, 0);
1663 eres
->x
.p
= new_enode(partition
, 2, C
->Dimension
);
1664 EVALUE_SET_DOMAIN(eres
->x
.p
->arr
[0],
1665 DomainConstraintSimplify(C
, options
->MaxRays
));
1666 value_set_si(eres
->x
.p
->arr
[1].d
, 1);
1667 value_init(eres
->x
.p
->arr
[1].x
.n
);
1669 value_set_si(eres
->x
.p
->arr
[1].x
.n
, 0);
1671 barvinok_count_with_options(P
, &eres
->x
.p
->arr
[1].x
.n
, options
);
1676 evalue
* barvinok_enumerate_with_options(Polyhedron
*P
, Polyhedron
* C
,
1677 struct barvinok_options
*options
)
1679 //P = unfringe(P, MaxRays);
1680 Polyhedron
*Corig
= C
;
1681 Polyhedron
*CEq
= NULL
, *rVD
, *CA
;
1683 unsigned nparam
= C
->Dimension
;
1687 value_init(factor
.d
);
1688 evalue_set_si(&factor
, 1, 1);
1690 CA
= align_context(C
, P
->Dimension
, options
->MaxRays
);
1691 P
= DomainIntersection(P
, CA
, options
->MaxRays
);
1692 Polyhedron_Free(CA
);
1695 POL_ENSURE_FACETS(P
);
1696 POL_ENSURE_VERTICES(P
);
1697 POL_ENSURE_FACETS(C
);
1698 POL_ENSURE_VERTICES(C
);
1700 if (C
->Dimension
== 0 || emptyQ(P
)) {
1702 eres
= barvinok_enumerate_cst(P
, CEq
? CEq
: Polyhedron_Copy(C
), options
);
1704 emul(&factor
, eres
);
1705 reduce_evalue(eres
);
1706 free_evalue_refs(&factor
);
1713 if (Polyhedron_is_infinite_param(P
, nparam
))
1718 P
= remove_equalities_p(P
, P
->Dimension
-nparam
, &f
);
1719 mask(f
, &factor
, options
);
1722 if (P
->Dimension
== nparam
) {
1724 P
= Universe_Polyhedron(0);
1728 Polyhedron
*T
= Polyhedron_Factor(P
, nparam
, options
->MaxRays
);
1729 if (T
|| (P
->Dimension
== nparam
+1)) {
1732 for (Q
= T
? T
: P
; Q
; Q
= Q
->next
) {
1733 Polyhedron
*next
= Q
->next
;
1737 if (Q
->Dimension
!= C
->Dimension
)
1738 QC
= Polyhedron_Project(Q
, nparam
);
1741 C
= DomainIntersection(C
, QC
, options
->MaxRays
);
1743 Polyhedron_Free(C2
);
1745 Polyhedron_Free(QC
);
1753 if (T
->Dimension
== C
->Dimension
) {
1760 Polyhedron
*next
= P
->next
;
1762 eres
= barvinok_enumerate_ev_f(P
, C
, options
);
1769 for (Q
= P
->next
; Q
; Q
= Q
->next
) {
1770 Polyhedron
*next
= Q
->next
;
1773 f
= barvinok_enumerate_ev_f(Q
, C
, options
);
1775 free_evalue_refs(f
);
1785 evalue
* barvinok_enumerate_ev(Polyhedron
*P
, Polyhedron
* C
, unsigned MaxRays
)
1788 barvinok_options
*options
= barvinok_options_new_with_defaults();
1789 options
->MaxRays
= MaxRays
;
1790 E
= barvinok_enumerate_with_options(P
, C
, options
);
1791 barvinok_options_free(options
);
1795 /* adapted from mpolyhedron_inflate in PolyLib */
1796 static Polyhedron
*Polyhedron_Inflate(Polyhedron
*P
, unsigned nparam
,
1800 int nvar
= P
->Dimension
- nparam
;
1801 Matrix
*C
= Polyhedron2Constraints(P
);
1805 /* subtract the sum of the negative coefficients of each inequality */
1806 for (int i
= 0; i
< C
->NbRows
; ++i
) {
1807 value_set_si(sum
, 0);
1808 for (int j
= 0; j
< nvar
; ++j
)
1809 if (value_neg_p(C
->p
[i
][1+j
]))
1810 value_addto(sum
, sum
, C
->p
[i
][1+j
]);
1811 value_subtract(C
->p
[i
][1+P
->Dimension
], C
->p
[i
][1+P
->Dimension
], sum
);
1814 P2
= Constraints2Polyhedron(C
, MaxRays
);
1819 /* adapted from mpolyhedron_deflate in PolyLib */
1820 static Polyhedron
*Polyhedron_Deflate(Polyhedron
*P
, unsigned nparam
,
1824 int nvar
= P
->Dimension
- nparam
;
1825 Matrix
*C
= Polyhedron2Constraints(P
);
1829 /* subtract the sum of the positive coefficients of each inequality */
1830 for (int i
= 0; i
< C
->NbRows
; ++i
) {
1831 value_set_si(sum
, 0);
1832 for (int j
= 0; j
< nvar
; ++j
)
1833 if (value_pos_p(C
->p
[i
][1+j
]))
1834 value_addto(sum
, sum
, C
->p
[i
][1+j
]);
1835 value_subtract(C
->p
[i
][1+P
->Dimension
], C
->p
[i
][1+P
->Dimension
], sum
);
1838 P2
= Constraints2Polyhedron(C
, MaxRays
);
1843 static evalue
* barvinok_enumerate_ev_f(Polyhedron
*P
, Polyhedron
* C
,
1844 barvinok_options
*options
)
1846 unsigned nparam
= C
->Dimension
;
1847 bool pre_approx
= options
->polynomial_approximation
>= BV_POLAPPROX_PRE_LOWER
&&
1848 options
->polynomial_approximation
<= BV_POLAPPROX_PRE_APPROX
;
1850 if (P
->Dimension
- nparam
== 1 && !pre_approx
)
1851 return ParamLine_Length(P
, C
, options
);
1853 Param_Polyhedron
*PP
= NULL
;
1854 Polyhedron
*CEq
= NULL
, *pVD
;
1856 Param_Domain
*D
, *next
;
1859 Polyhedron
*Porig
= P
;
1863 if (options
->polynomial_approximation
== BV_POLAPPROX_PRE_UPPER
)
1864 P
= Polyhedron_Inflate(P
, nparam
, options
->MaxRays
);
1865 if (options
->polynomial_approximation
== BV_POLAPPROX_PRE_LOWER
)
1866 P
= Polyhedron_Deflate(P
, nparam
, options
->MaxRays
);
1869 PP
= Polyhedron2Param_SD(&T
, C
, options
->MaxRays
, &CEq
, &CT
);
1870 if (T
!= P
&& P
!= Porig
)
1874 if (isIdentity(CT
)) {
1878 assert(CT
->NbRows
!= CT
->NbColumns
);
1879 if (CT
->NbRows
== 1) { // no more parameters
1880 eres
= barvinok_enumerate_cst(P
, CEq
, options
);
1885 Param_Polyhedron_Free(PP
);
1891 nparam
= CT
->NbRows
- 1;
1897 Param_Polyhedron_Scale_Integer(PP
, &T
, &det
, options
->MaxRays
);
1903 unsigned dim
= P
->Dimension
- nparam
;
1905 ALLOC(evalue
, eres
);
1906 value_init(eres
->d
);
1907 value_set_si(eres
->d
, 0);
1910 for (nd
= 0, D
=PP
->D
; D
; ++nd
, D
=D
->next
);
1911 struct section
{ Polyhedron
*D
; evalue E
; };
1912 section
*s
= new section
[nd
];
1913 Polyhedron
**fVD
= new Polyhedron_p
[nd
];
1915 enumerator_base
*et
= NULL
;
1920 et
= enumerator_base::create(P
, dim
, PP
->nbV
, options
);
1922 for(nd
= 0, D
=PP
->D
; D
; D
=next
) {
1925 Polyhedron
*rVD
= reduce_domain(D
->Domain
, CT
, CEq
, fVD
, nd
, options
);
1929 pVD
= CT
? DomainImage(rVD
,CT
,options
->MaxRays
) : rVD
;
1931 value_init(s
[nd
].E
.d
);
1932 evalue_set_si(&s
[nd
].E
, 0, 1);
1935 FORALL_PVertex_in_ParamPolyhedron(V
,D
,PP
) // _i is internal counter
1938 et
->decompose_at(V
, _i
, options
);
1939 } catch (OrthogonalException
&e
) {
1942 for (; nd
>= 0; --nd
) {
1943 free_evalue_refs(&s
[nd
].E
);
1944 Domain_Free(s
[nd
].D
);
1945 Domain_Free(fVD
[nd
]);
1949 eadd(et
->vE
[_i
] , &s
[nd
].E
);
1950 END_FORALL_PVertex_in_ParamPolyhedron
;
1951 evalue_range_reduction_in_domain(&s
[nd
].E
, pVD
);
1954 addeliminatedparams_evalue(&s
[nd
].E
, CT
);
1962 evalue_set_si(eres
, 0, 1);
1964 eres
->x
.p
= new_enode(partition
, 2*nd
, C
->Dimension
);
1965 for (int j
= 0; j
< nd
; ++j
) {
1966 EVALUE_SET_DOMAIN(eres
->x
.p
->arr
[2*j
], s
[j
].D
);
1967 value_clear(eres
->x
.p
->arr
[2*j
+1].d
);
1968 eres
->x
.p
->arr
[2*j
+1] = s
[j
].E
;
1969 Domain_Free(fVD
[j
]);
1976 evalue_div(eres
, det
);
1981 Polyhedron_Free(CEq
);
1985 Enumeration
* barvinok_enumerate(Polyhedron
*P
, Polyhedron
* C
, unsigned MaxRays
)
1987 evalue
*EP
= barvinok_enumerate_ev(P
, C
, MaxRays
);
1989 return partition2enumeration(EP
);
1992 static void SwapColumns(Value
**V
, int n
, int i
, int j
)
1994 for (int r
= 0; r
< n
; ++r
)
1995 value_swap(V
[r
][i
], V
[r
][j
]);
1998 static void SwapColumns(Polyhedron
*P
, int i
, int j
)
2000 SwapColumns(P
->Constraint
, P
->NbConstraints
, i
, j
);
2001 SwapColumns(P
->Ray
, P
->NbRays
, i
, j
);
2004 /* Construct a constraint c from constraints l and u such that if
2005 * if constraint c holds then for each value of the other variables
2006 * there is at most one value of variable pos (position pos+1 in the constraints).
2008 * Given a lower and an upper bound
2009 * n_l v_i + <c_l,x> + c_l >= 0
2010 * -n_u v_i + <c_u,x> + c_u >= 0
2011 * the constructed constraint is
2013 * -(n_l<c_u,x> + n_u<c_l,x>) + (-n_l c_u - n_u c_l + n_l n_u - 1)
2015 * which is then simplified to remove the content of the non-constant coefficients
2017 * len is the total length of the constraints.
2018 * v is a temporary variable that can be used by this procedure
2020 static void negative_test_constraint(Value
*l
, Value
*u
, Value
*c
, int pos
,
2023 value_oppose(*v
, u
[pos
+1]);
2024 Vector_Combine(l
+1, u
+1, c
+1, *v
, l
[pos
+1], len
-1);
2025 value_multiply(*v
, *v
, l
[pos
+1]);
2026 value_subtract(c
[len
-1], c
[len
-1], *v
);
2027 value_set_si(*v
, -1);
2028 Vector_Scale(c
+1, c
+1, *v
, len
-1);
2029 value_decrement(c
[len
-1], c
[len
-1]);
2030 ConstraintSimplify(c
, c
, len
, v
);
2033 static bool parallel_constraints(Value
*l
, Value
*u
, Value
*c
, int pos
,
2042 Vector_Gcd(&l
[1+pos
], len
, &g1
);
2043 Vector_Gcd(&u
[1+pos
], len
, &g2
);
2044 Vector_Combine(l
+1+pos
, u
+1+pos
, c
+1, g2
, g1
, len
);
2045 parallel
= First_Non_Zero(c
+1, len
) == -1;
2053 static void negative_test_constraint7(Value
*l
, Value
*u
, Value
*c
, int pos
,
2054 int exist
, int len
, Value
*v
)
2059 Vector_Gcd(&u
[1+pos
], exist
, v
);
2060 Vector_Gcd(&l
[1+pos
], exist
, &g
);
2061 Vector_Combine(l
+1, u
+1, c
+1, *v
, g
, len
-1);
2062 value_multiply(*v
, *v
, g
);
2063 value_subtract(c
[len
-1], c
[len
-1], *v
);
2064 value_set_si(*v
, -1);
2065 Vector_Scale(c
+1, c
+1, *v
, len
-1);
2066 value_decrement(c
[len
-1], c
[len
-1]);
2067 ConstraintSimplify(c
, c
, len
, v
);
2072 /* Turns a x + b >= 0 into a x + b <= -1
2074 * len is the total length of the constraint.
2075 * v is a temporary variable that can be used by this procedure
2077 static void oppose_constraint(Value
*c
, int len
, Value
*v
)
2079 value_set_si(*v
, -1);
2080 Vector_Scale(c
+1, c
+1, *v
, len
-1);
2081 value_decrement(c
[len
-1], c
[len
-1]);
2084 /* Split polyhedron P into two polyhedra *pos and *neg, where
2085 * existential variable i has at most one solution for each
2086 * value of the other variables in *neg.
2088 * The splitting is performed using constraints l and u.
2090 * nvar: number of set variables
2091 * row: temporary vector that can be used by this procedure
2092 * f: temporary value that can be used by this procedure
2094 static bool SplitOnConstraint(Polyhedron
*P
, int i
, int l
, int u
,
2095 int nvar
, int MaxRays
, Vector
*row
, Value
& f
,
2096 Polyhedron
**pos
, Polyhedron
**neg
)
2098 negative_test_constraint(P
->Constraint
[l
], P
->Constraint
[u
],
2099 row
->p
, nvar
+i
, P
->Dimension
+2, &f
);
2100 *neg
= AddConstraints(row
->p
, 1, P
, MaxRays
);
2102 /* We found an independent, but useless constraint
2103 * Maybe we should detect this earlier and not
2104 * mark the variable as INDEPENDENT
2106 if (emptyQ((*neg
))) {
2107 Polyhedron_Free(*neg
);
2111 oppose_constraint(row
->p
, P
->Dimension
+2, &f
);
2112 *pos
= AddConstraints(row
->p
, 1, P
, MaxRays
);
2114 if (emptyQ((*pos
))) {
2115 Polyhedron_Free(*neg
);
2116 Polyhedron_Free(*pos
);
2124 * unimodularly transform P such that constraint r is transformed
2125 * into a constraint that involves only a single (the first)
2126 * existential variable
2129 static Polyhedron
*rotate_along(Polyhedron
*P
, int r
, int nvar
, int exist
,
2135 Vector
*row
= Vector_Alloc(exist
);
2136 Vector_Copy(P
->Constraint
[r
]+1+nvar
, row
->p
, exist
);
2137 Vector_Gcd(row
->p
, exist
, &g
);
2138 if (value_notone_p(g
))
2139 Vector_AntiScale(row
->p
, row
->p
, g
, exist
);
2142 Matrix
*M
= unimodular_complete(row
);
2143 Matrix
*M2
= Matrix_Alloc(P
->Dimension
+1, P
->Dimension
+1);
2144 for (r
= 0; r
< nvar
; ++r
)
2145 value_set_si(M2
->p
[r
][r
], 1);
2146 for ( ; r
< nvar
+exist
; ++r
)
2147 Vector_Copy(M
->p
[r
-nvar
], M2
->p
[r
]+nvar
, exist
);
2148 for ( ; r
< P
->Dimension
+1; ++r
)
2149 value_set_si(M2
->p
[r
][r
], 1);
2150 Polyhedron
*T
= Polyhedron_Image(P
, M2
, MaxRays
);
2159 /* Split polyhedron P into two polyhedra *pos and *neg, where
2160 * existential variable i has at most one solution for each
2161 * value of the other variables in *neg.
2163 * If independent is set, then the two constraints on which the
2164 * split will be performed need to be independent of the other
2165 * existential variables.
2167 * Return true if an appropriate split could be performed.
2169 * nvar: number of set variables
2170 * exist: number of existential variables
2171 * row: temporary vector that can be used by this procedure
2172 * f: temporary value that can be used by this procedure
2174 static bool SplitOnVar(Polyhedron
*P
, int i
,
2175 int nvar
, int exist
, int MaxRays
,
2176 Vector
*row
, Value
& f
, bool independent
,
2177 Polyhedron
**pos
, Polyhedron
**neg
)
2181 for (int l
= P
->NbEq
; l
< P
->NbConstraints
; ++l
) {
2182 if (value_negz_p(P
->Constraint
[l
][nvar
+i
+1]))
2186 for (j
= 0; j
< exist
; ++j
)
2187 if (j
!= i
&& value_notzero_p(P
->Constraint
[l
][nvar
+j
+1]))
2193 for (int u
= P
->NbEq
; u
< P
->NbConstraints
; ++u
) {
2194 if (value_posz_p(P
->Constraint
[u
][nvar
+i
+1]))
2198 for (j
= 0; j
< exist
; ++j
)
2199 if (j
!= i
&& value_notzero_p(P
->Constraint
[u
][nvar
+j
+1]))
2205 if (SplitOnConstraint(P
, i
, l
, u
, nvar
, MaxRays
, row
, f
, pos
, neg
)) {
2208 SwapColumns(*neg
, nvar
+1, nvar
+1+i
);
2218 static bool double_bound_pair(Polyhedron
*P
, int nvar
, int exist
,
2219 int i
, int l1
, int l2
,
2220 Polyhedron
**pos
, Polyhedron
**neg
)
2224 Vector
*row
= Vector_Alloc(P
->Dimension
+2);
2225 value_set_si(row
->p
[0], 1);
2226 value_oppose(f
, P
->Constraint
[l1
][nvar
+i
+1]);
2227 Vector_Combine(P
->Constraint
[l1
]+1, P
->Constraint
[l2
]+1,
2229 P
->Constraint
[l2
][nvar
+i
+1], f
,
2231 ConstraintSimplify(row
->p
, row
->p
, P
->Dimension
+2, &f
);
2232 *pos
= AddConstraints(row
->p
, 1, P
, 0);
2233 value_set_si(f
, -1);
2234 Vector_Scale(row
->p
+1, row
->p
+1, f
, P
->Dimension
+1);
2235 value_decrement(row
->p
[P
->Dimension
+1], row
->p
[P
->Dimension
+1]);
2236 *neg
= AddConstraints(row
->p
, 1, P
, 0);
2240 return !emptyQ((*pos
)) && !emptyQ((*neg
));
2243 static bool double_bound(Polyhedron
*P
, int nvar
, int exist
,
2244 Polyhedron
**pos
, Polyhedron
**neg
)
2246 for (int i
= 0; i
< exist
; ++i
) {
2248 for (l1
= P
->NbEq
; l1
< P
->NbConstraints
; ++l1
) {
2249 if (value_negz_p(P
->Constraint
[l1
][nvar
+i
+1]))
2251 for (l2
= l1
+ 1; l2
< P
->NbConstraints
; ++l2
) {
2252 if (value_negz_p(P
->Constraint
[l2
][nvar
+i
+1]))
2254 if (double_bound_pair(P
, nvar
, exist
, i
, l1
, l2
, pos
, neg
))
2258 for (l1
= P
->NbEq
; l1
< P
->NbConstraints
; ++l1
) {
2259 if (value_posz_p(P
->Constraint
[l1
][nvar
+i
+1]))
2261 if (l1
< P
->NbConstraints
)
2262 for (l2
= l1
+ 1; l2
< P
->NbConstraints
; ++l2
) {
2263 if (value_posz_p(P
->Constraint
[l2
][nvar
+i
+1]))
2265 if (double_bound_pair(P
, nvar
, exist
, i
, l1
, l2
, pos
, neg
))
2277 INDEPENDENT
= 1 << 2,
2281 static evalue
* enumerate_or(Polyhedron
*D
,
2282 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2285 fprintf(stderr
, "\nER: Or\n");
2286 #endif /* DEBUG_ER */
2288 Polyhedron
*N
= D
->next
;
2291 barvinok_enumerate_e_with_options(D
, exist
, nparam
, options
);
2294 for (D
= N
; D
; D
= N
) {
2299 barvinok_enumerate_e_with_options(D
, exist
, nparam
, options
);
2302 free_evalue_refs(EN
);
2312 static evalue
* enumerate_sum(Polyhedron
*P
,
2313 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2315 int nvar
= P
->Dimension
- exist
- nparam
;
2316 int toswap
= nvar
< exist
? nvar
: exist
;
2317 for (int i
= 0; i
< toswap
; ++i
)
2318 SwapColumns(P
, 1 + i
, nvar
+exist
- i
);
2322 fprintf(stderr
, "\nER: Sum\n");
2323 #endif /* DEBUG_ER */
2325 evalue
*EP
= barvinok_enumerate_e_with_options(P
, exist
, nparam
, options
);
2327 evalue_split_domains_into_orthants(EP
, options
->MaxRays
);
2329 evalue_range_reduction(EP
);
2331 evalue_frac2floor2(EP
, 1);
2333 evalue
*sum
= esum(EP
, nvar
);
2335 free_evalue_refs(EP
);
2339 evalue_range_reduction(EP
);
2344 static evalue
* split_sure(Polyhedron
*P
, Polyhedron
*S
,
2345 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2347 int nvar
= P
->Dimension
- exist
- nparam
;
2349 Matrix
*M
= Matrix_Alloc(exist
, S
->Dimension
+2);
2350 for (int i
= 0; i
< exist
; ++i
)
2351 value_set_si(M
->p
[i
][nvar
+i
+1], 1);
2353 S
= DomainAddRays(S
, M
, options
->MaxRays
);
2355 Polyhedron
*F
= DomainAddRays(P
, M
, options
->MaxRays
);
2356 Polyhedron
*D
= DomainDifference(F
, S
, options
->MaxRays
);
2358 D
= Disjoint_Domain(D
, 0, options
->MaxRays
);
2363 M
= Matrix_Alloc(P
->Dimension
+1-exist
, P
->Dimension
+1);
2364 for (int j
= 0; j
< nvar
; ++j
)
2365 value_set_si(M
->p
[j
][j
], 1);
2366 for (int j
= 0; j
< nparam
+1; ++j
)
2367 value_set_si(M
->p
[nvar
+j
][nvar
+exist
+j
], 1);
2368 Polyhedron
*T
= Polyhedron_Image(S
, M
, options
->MaxRays
);
2369 evalue
*EP
= barvinok_enumerate_e_with_options(T
, 0, nparam
, options
);
2374 for (Polyhedron
*Q
= D
; Q
; Q
= Q
->next
) {
2375 Polyhedron
*N
= Q
->next
;
2377 T
= DomainIntersection(P
, Q
, options
->MaxRays
);
2378 evalue
*E
= barvinok_enumerate_e_with_options(T
, exist
, nparam
, options
);
2380 free_evalue_refs(E
);
2389 static evalue
* enumerate_sure(Polyhedron
*P
,
2390 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2394 int nvar
= P
->Dimension
- exist
- nparam
;
2400 for (i
= 0; i
< exist
; ++i
) {
2401 Matrix
*M
= Matrix_Alloc(S
->NbConstraints
, S
->Dimension
+2);
2403 value_set_si(lcm
, 1);
2404 for (int j
= 0; j
< S
->NbConstraints
; ++j
) {
2405 if (value_negz_p(S
->Constraint
[j
][1+nvar
+i
]))
2407 if (value_one_p(S
->Constraint
[j
][1+nvar
+i
]))
2409 value_lcm(lcm
, S
->Constraint
[j
][1+nvar
+i
], &lcm
);
2412 for (int j
= 0; j
< S
->NbConstraints
; ++j
) {
2413 if (value_negz_p(S
->Constraint
[j
][1+nvar
+i
]))
2415 if (value_one_p(S
->Constraint
[j
][1+nvar
+i
]))
2417 value_division(f
, lcm
, S
->Constraint
[j
][1+nvar
+i
]);
2418 Vector_Scale(S
->Constraint
[j
], M
->p
[c
], f
, S
->Dimension
+2);
2419 value_subtract(M
->p
[c
][S
->Dimension
+1],
2420 M
->p
[c
][S
->Dimension
+1],
2422 value_increment(M
->p
[c
][S
->Dimension
+1],
2423 M
->p
[c
][S
->Dimension
+1]);
2427 S
= AddConstraints(M
->p
[0], c
, S
, options
->MaxRays
);
2442 fprintf(stderr
, "\nER: Sure\n");
2443 #endif /* DEBUG_ER */
2445 return split_sure(P
, S
, exist
, nparam
, options
);
2448 static evalue
* enumerate_sure2(Polyhedron
*P
,
2449 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2451 int nvar
= P
->Dimension
- exist
- nparam
;
2453 for (r
= 0; r
< P
->NbRays
; ++r
)
2454 if (value_one_p(P
->Ray
[r
][0]) &&
2455 value_one_p(P
->Ray
[r
][P
->Dimension
+1]))
2461 Matrix
*M
= Matrix_Alloc(nvar
+ 1 + nparam
, P
->Dimension
+2);
2462 for (int i
= 0; i
< nvar
; ++i
)
2463 value_set_si(M
->p
[i
][1+i
], 1);
2464 for (int i
= 0; i
< nparam
; ++i
)
2465 value_set_si(M
->p
[i
+nvar
][1+nvar
+exist
+i
], 1);
2466 Vector_Copy(P
->Ray
[r
]+1+nvar
, M
->p
[nvar
+nparam
]+1+nvar
, exist
);
2467 value_set_si(M
->p
[nvar
+nparam
][0], 1);
2468 value_set_si(M
->p
[nvar
+nparam
][P
->Dimension
+1], 1);
2469 Polyhedron
* F
= Rays2Polyhedron(M
, options
->MaxRays
);
2472 Polyhedron
*I
= DomainIntersection(F
, P
, options
->MaxRays
);
2476 fprintf(stderr
, "\nER: Sure2\n");
2477 #endif /* DEBUG_ER */
2479 return split_sure(P
, I
, exist
, nparam
, options
);
2482 static evalue
* enumerate_cyclic(Polyhedron
*P
,
2483 unsigned exist
, unsigned nparam
,
2484 evalue
* EP
, int r
, int p
, unsigned MaxRays
)
2486 int nvar
= P
->Dimension
- exist
- nparam
;
2488 /* If EP in its fractional maps only contains references
2489 * to the remainder parameter with appropriate coefficients
2490 * then we could in principle avoid adding existentially
2491 * quantified variables to the validity domains.
2492 * We'd have to replace the remainder by m { p/m }
2493 * and multiply with an appropriate factor that is one
2494 * only in the appropriate range.
2495 * This last multiplication can be avoided if EP
2496 * has a single validity domain with no (further)
2497 * constraints on the remainder parameter
2500 Matrix
*CT
= Matrix_Alloc(nparam
+1, nparam
+3);
2501 Matrix
*M
= Matrix_Alloc(1, 1+nparam
+3);
2502 for (int j
= 0; j
< nparam
; ++j
)
2504 value_set_si(CT
->p
[j
][j
], 1);
2505 value_set_si(CT
->p
[p
][nparam
+1], 1);
2506 value_set_si(CT
->p
[nparam
][nparam
+2], 1);
2507 value_set_si(M
->p
[0][1+p
], -1);
2508 value_absolute(M
->p
[0][1+nparam
], P
->Ray
[0][1+nvar
+exist
+p
]);
2509 value_set_si(M
->p
[0][1+nparam
+1], 1);
2510 Polyhedron
*CEq
= Constraints2Polyhedron(M
, 1);
2512 addeliminatedparams_enum(EP
, CT
, CEq
, MaxRays
, nparam
);
2513 Polyhedron_Free(CEq
);
2519 static void enumerate_vd_add_ray(evalue
*EP
, Matrix
*Rays
, unsigned MaxRays
)
2521 if (value_notzero_p(EP
->d
))
2524 assert(EP
->x
.p
->type
== partition
);
2525 assert(EP
->x
.p
->pos
== EVALUE_DOMAIN(EP
->x
.p
->arr
[0])->Dimension
);
2526 for (int i
= 0; i
< EP
->x
.p
->size
/2; ++i
) {
2527 Polyhedron
*D
= EVALUE_DOMAIN(EP
->x
.p
->arr
[2*i
]);
2528 Polyhedron
*N
= DomainAddRays(D
, Rays
, MaxRays
);
2529 EVALUE_SET_DOMAIN(EP
->x
.p
->arr
[2*i
], N
);
2534 static evalue
* enumerate_line(Polyhedron
*P
,
2535 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2541 fprintf(stderr
, "\nER: Line\n");
2542 #endif /* DEBUG_ER */
2544 int nvar
= P
->Dimension
- exist
- nparam
;
2546 for (i
= 0; i
< nparam
; ++i
)
2547 if (value_notzero_p(P
->Ray
[0][1+nvar
+exist
+i
]))
2550 for (j
= i
+1; j
< nparam
; ++j
)
2551 if (value_notzero_p(P
->Ray
[0][1+nvar
+exist
+i
]))
2553 assert(j
>= nparam
); // for now
2555 Matrix
*M
= Matrix_Alloc(2, P
->Dimension
+2);
2556 value_set_si(M
->p
[0][0], 1);
2557 value_set_si(M
->p
[0][1+nvar
+exist
+i
], 1);
2558 value_set_si(M
->p
[1][0], 1);
2559 value_set_si(M
->p
[1][1+nvar
+exist
+i
], -1);
2560 value_absolute(M
->p
[1][1+P
->Dimension
], P
->Ray
[0][1+nvar
+exist
+i
]);
2561 value_decrement(M
->p
[1][1+P
->Dimension
], M
->p
[1][1+P
->Dimension
]);
2562 Polyhedron
*S
= AddConstraints(M
->p
[0], 2, P
, options
->MaxRays
);
2563 evalue
*EP
= barvinok_enumerate_e_with_options(S
, exist
, nparam
, options
);
2567 return enumerate_cyclic(P
, exist
, nparam
, EP
, 0, i
, options
->MaxRays
);
2570 static int single_param_pos(Polyhedron
*P
, unsigned exist
, unsigned nparam
,
2573 int nvar
= P
->Dimension
- exist
- nparam
;
2574 if (First_Non_Zero(P
->Ray
[r
]+1, nvar
) != -1)
2576 int i
= First_Non_Zero(P
->Ray
[r
]+1+nvar
+exist
, nparam
);
2579 if (First_Non_Zero(P
->Ray
[r
]+1+nvar
+exist
+1, nparam
-i
-1) != -1)
2584 static evalue
* enumerate_remove_ray(Polyhedron
*P
, int r
,
2585 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2588 fprintf(stderr
, "\nER: RedundantRay\n");
2589 #endif /* DEBUG_ER */
2593 value_set_si(one
, 1);
2594 int len
= P
->NbRays
-1;
2595 Matrix
*M
= Matrix_Alloc(2 * len
, P
->Dimension
+2);
2596 Vector_Copy(P
->Ray
[0], M
->p
[0], r
* (P
->Dimension
+2));
2597 Vector_Copy(P
->Ray
[r
+1], M
->p
[r
], (len
-r
) * (P
->Dimension
+2));
2598 for (int j
= 0; j
< P
->NbRays
; ++j
) {
2601 Vector_Combine(P
->Ray
[j
], P
->Ray
[r
], M
->p
[len
+j
-(j
>r
)],
2602 one
, P
->Ray
[j
][P
->Dimension
+1], P
->Dimension
+2);
2605 P
= Rays2Polyhedron(M
, options
->MaxRays
);
2607 evalue
*EP
= barvinok_enumerate_e_with_options(P
, exist
, nparam
, options
);
2614 static evalue
* enumerate_redundant_ray(Polyhedron
*P
,
2615 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2617 assert(P
->NbBid
== 0);
2618 int nvar
= P
->Dimension
- exist
- nparam
;
2622 for (int r
= 0; r
< P
->NbRays
; ++r
) {
2623 if (value_notzero_p(P
->Ray
[r
][P
->Dimension
+1]))
2625 int i1
= single_param_pos(P
, exist
, nparam
, r
);
2628 for (int r2
= r
+1; r2
< P
->NbRays
; ++r2
) {
2629 if (value_notzero_p(P
->Ray
[r2
][P
->Dimension
+1]))
2631 int i2
= single_param_pos(P
, exist
, nparam
, r2
);
2637 value_division(m
, P
->Ray
[r
][1+nvar
+exist
+i1
],
2638 P
->Ray
[r2
][1+nvar
+exist
+i1
]);
2639 value_multiply(m
, m
, P
->Ray
[r2
][1+nvar
+exist
+i1
]);
2640 /* r2 divides r => r redundant */
2641 if (value_eq(m
, P
->Ray
[r
][1+nvar
+exist
+i1
])) {
2643 return enumerate_remove_ray(P
, r
, exist
, nparam
, options
);
2646 value_division(m
, P
->Ray
[r2
][1+nvar
+exist
+i1
],
2647 P
->Ray
[r
][1+nvar
+exist
+i1
]);
2648 value_multiply(m
, m
, P
->Ray
[r
][1+nvar
+exist
+i1
]);
2649 /* r divides r2 => r2 redundant */
2650 if (value_eq(m
, P
->Ray
[r2
][1+nvar
+exist
+i1
])) {
2652 return enumerate_remove_ray(P
, r2
, exist
, nparam
, options
);
2660 static Polyhedron
*upper_bound(Polyhedron
*P
,
2661 int pos
, Value
*max
, Polyhedron
**R
)
2670 for (Polyhedron
*Q
= P
; Q
; Q
= N
) {
2672 for (r
= 0; r
< P
->NbRays
; ++r
) {
2673 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1]) &&
2674 value_pos_p(P
->Ray
[r
][1+pos
]))
2677 if (r
< P
->NbRays
) {
2685 for (r
= 0; r
< P
->NbRays
; ++r
) {
2686 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1]))
2688 mpz_fdiv_q(v
, P
->Ray
[r
][1+pos
], P
->Ray
[r
][1+P
->Dimension
]);
2689 if ((!Q
->next
&& r
== 0) || value_gt(v
, *max
))
2690 value_assign(*max
, v
);
2697 static evalue
* enumerate_ray(Polyhedron
*P
,
2698 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2700 assert(P
->NbBid
== 0);
2701 int nvar
= P
->Dimension
- exist
- nparam
;
2704 for (r
= 0; r
< P
->NbRays
; ++r
)
2705 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1]))
2711 for (r2
= r
+1; r2
< P
->NbRays
; ++r2
)
2712 if (value_zero_p(P
->Ray
[r2
][P
->Dimension
+1]))
2714 if (r2
< P
->NbRays
) {
2716 return enumerate_sum(P
, exist
, nparam
, options
);
2720 fprintf(stderr
, "\nER: Ray\n");
2721 #endif /* DEBUG_ER */
2727 value_set_si(one
, 1);
2728 int i
= single_param_pos(P
, exist
, nparam
, r
);
2729 assert(i
!= -1); // for now;
2731 Matrix
*M
= Matrix_Alloc(P
->NbRays
, P
->Dimension
+2);
2732 for (int j
= 0; j
< P
->NbRays
; ++j
) {
2733 Vector_Combine(P
->Ray
[j
], P
->Ray
[r
], M
->p
[j
],
2734 one
, P
->Ray
[j
][P
->Dimension
+1], P
->Dimension
+2);
2736 Polyhedron
*S
= Rays2Polyhedron(M
, options
->MaxRays
);
2738 Polyhedron
*D
= DomainDifference(P
, S
, options
->MaxRays
);
2740 // Polyhedron_Print(stderr, P_VALUE_FMT, D);
2741 assert(value_pos_p(P
->Ray
[r
][1+nvar
+exist
+i
])); // for now
2743 D
= upper_bound(D
, nvar
+exist
+i
, &m
, &R
);
2747 M
= Matrix_Alloc(2, P
->Dimension
+2);
2748 value_set_si(M
->p
[0][0], 1);
2749 value_set_si(M
->p
[1][0], 1);
2750 value_set_si(M
->p
[0][1+nvar
+exist
+i
], -1);
2751 value_set_si(M
->p
[1][1+nvar
+exist
+i
], 1);
2752 value_assign(M
->p
[0][1+P
->Dimension
], m
);
2753 value_oppose(M
->p
[1][1+P
->Dimension
], m
);
2754 value_addto(M
->p
[1][1+P
->Dimension
], M
->p
[1][1+P
->Dimension
],
2755 P
->Ray
[r
][1+nvar
+exist
+i
]);
2756 value_decrement(M
->p
[1][1+P
->Dimension
], M
->p
[1][1+P
->Dimension
]);
2757 // Matrix_Print(stderr, P_VALUE_FMT, M);
2758 D
= AddConstraints(M
->p
[0], 2, P
, options
->MaxRays
);
2759 // Polyhedron_Print(stderr, P_VALUE_FMT, D);
2760 value_subtract(M
->p
[0][1+P
->Dimension
], M
->p
[0][1+P
->Dimension
],
2761 P
->Ray
[r
][1+nvar
+exist
+i
]);
2762 // Matrix_Print(stderr, P_VALUE_FMT, M);
2763 S
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
2764 // Polyhedron_Print(stderr, P_VALUE_FMT, S);
2767 evalue
*EP
= barvinok_enumerate_e_with_options(D
, exist
, nparam
, options
);
2772 if (value_notone_p(P
->Ray
[r
][1+nvar
+exist
+i
]))
2773 EP
= enumerate_cyclic(P
, exist
, nparam
, EP
, r
, i
, options
->MaxRays
);
2775 M
= Matrix_Alloc(1, nparam
+2);
2776 value_set_si(M
->p
[0][0], 1);
2777 value_set_si(M
->p
[0][1+i
], 1);
2778 enumerate_vd_add_ray(EP
, M
, options
->MaxRays
);
2783 evalue
*E
= barvinok_enumerate_e_with_options(S
, exist
, nparam
, options
);
2785 free_evalue_refs(E
);
2792 evalue
*ER
= enumerate_or(R
, exist
, nparam
, options
);
2794 free_evalue_refs(ER
);
2801 static evalue
* enumerate_vd(Polyhedron
**PA
,
2802 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2804 Polyhedron
*P
= *PA
;
2805 int nvar
= P
->Dimension
- exist
- nparam
;
2806 Param_Polyhedron
*PP
= NULL
;
2807 Polyhedron
*C
= Universe_Polyhedron(nparam
);
2811 PP
= Polyhedron2Param_SimplifiedDomain(&PR
,C
, options
->MaxRays
,&CEq
,&CT
);
2815 Param_Domain
*D
, *last
;
2818 for (nd
= 0, D
=PP
->D
; D
; D
=D
->next
, ++nd
)
2821 Polyhedron
**VD
= new Polyhedron_p
[nd
];
2822 Polyhedron
**fVD
= new Polyhedron_p
[nd
];
2823 for(nd
= 0, D
=PP
->D
; D
; D
=D
->next
) {
2824 Polyhedron
*rVD
= reduce_domain(D
->Domain
, CT
, CEq
, fVD
, nd
, options
);
2837 /* This doesn't seem to have any effect */
2839 Polyhedron
*CA
= align_context(VD
[0], P
->Dimension
, options
->MaxRays
);
2841 P
= DomainIntersection(P
, CA
, options
->MaxRays
);
2844 Polyhedron_Free(CA
);
2849 if (!EP
&& CT
->NbColumns
!= CT
->NbRows
) {
2850 Polyhedron
*CEqr
= DomainImage(CEq
, CT
, options
->MaxRays
);
2851 Polyhedron
*CA
= align_context(CEqr
, PR
->Dimension
, options
->MaxRays
);
2852 Polyhedron
*I
= DomainIntersection(PR
, CA
, options
->MaxRays
);
2853 Polyhedron_Free(CEqr
);
2854 Polyhedron_Free(CA
);
2856 fprintf(stderr
, "\nER: Eliminate\n");
2857 #endif /* DEBUG_ER */
2858 nparam
-= CT
->NbColumns
- CT
->NbRows
;
2859 EP
= barvinok_enumerate_e_with_options(I
, exist
, nparam
, options
);
2860 nparam
+= CT
->NbColumns
- CT
->NbRows
;
2861 addeliminatedparams_enum(EP
, CT
, CEq
, options
->MaxRays
, nparam
);
2865 Polyhedron_Free(PR
);
2868 if (!EP
&& nd
> 1) {
2870 fprintf(stderr
, "\nER: VD\n");
2871 #endif /* DEBUG_ER */
2872 for (int i
= 0; i
< nd
; ++i
) {
2873 Polyhedron
*CA
= align_context(VD
[i
], P
->Dimension
, options
->MaxRays
);
2874 Polyhedron
*I
= DomainIntersection(P
, CA
, options
->MaxRays
);
2877 EP
= barvinok_enumerate_e_with_options(I
, exist
, nparam
, options
);
2879 evalue
*E
= barvinok_enumerate_e_with_options(I
, exist
, nparam
,
2882 free_evalue_refs(E
);
2886 Polyhedron_Free(CA
);
2890 for (int i
= 0; i
< nd
; ++i
) {
2891 Polyhedron_Free(VD
[i
]);
2892 Polyhedron_Free(fVD
[i
]);
2898 if (!EP
&& nvar
== 0) {
2901 Param_Vertices
*V
, *V2
;
2902 Matrix
* M
= Matrix_Alloc(1, P
->Dimension
+2);
2904 FORALL_PVertex_in_ParamPolyhedron(V
, last
, PP
) {
2906 FORALL_PVertex_in_ParamPolyhedron(V2
, last
, PP
) {
2913 for (int i
= 0; i
< exist
; ++i
) {
2914 value_oppose(f
, V
->Vertex
->p
[i
][nparam
+1]);
2915 Vector_Combine(V
->Vertex
->p
[i
],
2917 M
->p
[0] + 1 + nvar
+ exist
,
2918 V2
->Vertex
->p
[i
][nparam
+1],
2922 for (j
= 0; j
< nparam
; ++j
)
2923 if (value_notzero_p(M
->p
[0][1+nvar
+exist
+j
]))
2927 ConstraintSimplify(M
->p
[0], M
->p
[0],
2928 P
->Dimension
+2, &f
);
2929 value_set_si(M
->p
[0][0], 0);
2930 Polyhedron
*para
= AddConstraints(M
->p
[0], 1, P
,
2933 Polyhedron_Free(para
);
2936 Polyhedron
*pos
, *neg
;
2937 value_set_si(M
->p
[0][0], 1);
2938 value_decrement(M
->p
[0][P
->Dimension
+1],
2939 M
->p
[0][P
->Dimension
+1]);
2940 neg
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
2941 value_set_si(f
, -1);
2942 Vector_Scale(M
->p
[0]+1, M
->p
[0]+1, f
,
2944 value_decrement(M
->p
[0][P
->Dimension
+1],
2945 M
->p
[0][P
->Dimension
+1]);
2946 value_decrement(M
->p
[0][P
->Dimension
+1],
2947 M
->p
[0][P
->Dimension
+1]);
2948 pos
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
2949 if (emptyQ(neg
) && emptyQ(pos
)) {
2950 Polyhedron_Free(para
);
2951 Polyhedron_Free(pos
);
2952 Polyhedron_Free(neg
);
2956 fprintf(stderr
, "\nER: Order\n");
2957 #endif /* DEBUG_ER */
2958 EP
= barvinok_enumerate_e_with_options(para
, exist
, nparam
,
2962 E
= barvinok_enumerate_e_with_options(pos
, exist
, nparam
,
2965 free_evalue_refs(E
);
2969 E
= barvinok_enumerate_e_with_options(neg
, exist
, nparam
,
2972 free_evalue_refs(E
);
2975 Polyhedron_Free(para
);
2976 Polyhedron_Free(pos
);
2977 Polyhedron_Free(neg
);
2982 } END_FORALL_PVertex_in_ParamPolyhedron
;
2985 } END_FORALL_PVertex_in_ParamPolyhedron
;
2988 /* Search for vertex coordinate to split on */
2989 /* First look for one independent of the parameters */
2990 FORALL_PVertex_in_ParamPolyhedron(V
, last
, PP
) {
2991 for (int i
= 0; i
< exist
; ++i
) {
2993 for (j
= 0; j
< nparam
; ++j
)
2994 if (value_notzero_p(V
->Vertex
->p
[i
][j
]))
2998 value_set_si(M
->p
[0][0], 1);
2999 Vector_Set(M
->p
[0]+1, 0, nvar
+exist
);
3000 Vector_Copy(V
->Vertex
->p
[i
],
3001 M
->p
[0] + 1 + nvar
+ exist
, nparam
+1);
3002 value_oppose(M
->p
[0][1+nvar
+i
],
3003 V
->Vertex
->p
[i
][nparam
+1]);
3005 Polyhedron
*pos
, *neg
;
3006 value_set_si(M
->p
[0][0], 1);
3007 value_decrement(M
->p
[0][P
->Dimension
+1],
3008 M
->p
[0][P
->Dimension
+1]);
3009 neg
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
3010 value_set_si(f
, -1);
3011 Vector_Scale(M
->p
[0]+1, M
->p
[0]+1, f
,
3013 value_decrement(M
->p
[0][P
->Dimension
+1],
3014 M
->p
[0][P
->Dimension
+1]);
3015 value_decrement(M
->p
[0][P
->Dimension
+1],
3016 M
->p
[0][P
->Dimension
+1]);
3017 pos
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
3018 if (emptyQ(neg
) || emptyQ(pos
)) {
3019 Polyhedron_Free(pos
);
3020 Polyhedron_Free(neg
);
3023 Polyhedron_Free(pos
);
3024 value_increment(M
->p
[0][P
->Dimension
+1],
3025 M
->p
[0][P
->Dimension
+1]);
3026 pos
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
3028 fprintf(stderr
, "\nER: Vertex\n");
3029 #endif /* DEBUG_ER */
3031 EP
= enumerate_or(pos
, exist
, nparam
, options
);
3036 } END_FORALL_PVertex_in_ParamPolyhedron
;
3040 /* Search for vertex coordinate to split on */
3041 /* Now look for one that depends on the parameters */
3042 FORALL_PVertex_in_ParamPolyhedron(V
, last
, PP
) {
3043 for (int i
= 0; i
< exist
; ++i
) {
3044 value_set_si(M
->p
[0][0], 1);
3045 Vector_Set(M
->p
[0]+1, 0, nvar
+exist
);
3046 Vector_Copy(V
->Vertex
->p
[i
],
3047 M
->p
[0] + 1 + nvar
+ exist
, nparam
+1);
3048 value_oppose(M
->p
[0][1+nvar
+i
],
3049 V
->Vertex
->p
[i
][nparam
+1]);
3051 Polyhedron
*pos
, *neg
;
3052 value_set_si(M
->p
[0][0], 1);
3053 value_decrement(M
->p
[0][P
->Dimension
+1],
3054 M
->p
[0][P
->Dimension
+1]);
3055 neg
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
3056 value_set_si(f
, -1);
3057 Vector_Scale(M
->p
[0]+1, M
->p
[0]+1, f
,
3059 value_decrement(M
->p
[0][P
->Dimension
+1],
3060 M
->p
[0][P
->Dimension
+1]);
3061 value_decrement(M
->p
[0][P
->Dimension
+1],
3062 M
->p
[0][P
->Dimension
+1]);
3063 pos
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
3064 if (emptyQ(neg
) || emptyQ(pos
)) {
3065 Polyhedron_Free(pos
);
3066 Polyhedron_Free(neg
);
3069 Polyhedron_Free(pos
);
3070 value_increment(M
->p
[0][P
->Dimension
+1],
3071 M
->p
[0][P
->Dimension
+1]);
3072 pos
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
3074 fprintf(stderr
, "\nER: ParamVertex\n");
3075 #endif /* DEBUG_ER */
3077 EP
= enumerate_or(pos
, exist
, nparam
, options
);
3082 } END_FORALL_PVertex_in_ParamPolyhedron
;
3090 Polyhedron_Free(CEq
);
3094 Param_Polyhedron_Free(PP
);
3100 evalue
* barvinok_enumerate_pip(Polyhedron
*P
, unsigned exist
, unsigned nparam
,
3104 barvinok_options
*options
= barvinok_options_new_with_defaults();
3105 options
->MaxRays
= MaxRays
;
3106 E
= barvinok_enumerate_pip_with_options(P
, exist
, nparam
, options
);
3107 barvinok_options_free(options
);
3112 evalue
*barvinok_enumerate_pip_with_options(Polyhedron
*P
,
3113 unsigned exist
, unsigned nparam
, struct barvinok_options
*options
)
3118 evalue
*barvinok_enumerate_pip_with_options(Polyhedron
*P
,
3119 unsigned exist
, unsigned nparam
, struct barvinok_options
*options
)
3121 int nvar
= P
->Dimension
- exist
- nparam
;
3122 evalue
*EP
= evalue_zero();
3126 fprintf(stderr
, "\nER: PIP\n");
3127 #endif /* DEBUG_ER */
3129 Polyhedron
*D
= pip_projectout(P
, nvar
, exist
, nparam
);
3130 for (Q
= D
; Q
; Q
= N
) {
3134 exist
= Q
->Dimension
- nvar
- nparam
;
3135 E
= barvinok_enumerate_e_with_options(Q
, exist
, nparam
, options
);
3138 free_evalue_refs(E
);
3147 static bool is_single(Value
*row
, int pos
, int len
)
3149 return First_Non_Zero(row
, pos
) == -1 &&
3150 First_Non_Zero(row
+pos
+1, len
-pos
-1) == -1;
3153 static evalue
* barvinok_enumerate_e_r(Polyhedron
*P
,
3154 unsigned exist
, unsigned nparam
, barvinok_options
*options
);
3157 static int er_level
= 0;
3159 evalue
* barvinok_enumerate_e_with_options(Polyhedron
*P
,
3160 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
3162 fprintf(stderr
, "\nER: level %i\n", er_level
);
3164 Polyhedron_PrintConstraints(stderr
, P_VALUE_FMT
, P
);
3165 fprintf(stderr
, "\nE %d\nP %d\n", exist
, nparam
);
3167 P
= DomainConstraintSimplify(Polyhedron_Copy(P
), options
->MaxRays
);
3168 evalue
*EP
= barvinok_enumerate_e_r(P
, exist
, nparam
, options
);
3174 evalue
* barvinok_enumerate_e_with_options(Polyhedron
*P
,
3175 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
3177 P
= DomainConstraintSimplify(Polyhedron_Copy(P
), options
->MaxRays
);
3178 evalue
*EP
= barvinok_enumerate_e_r(P
, exist
, nparam
, options
);
3184 evalue
* barvinok_enumerate_e(Polyhedron
*P
, unsigned exist
, unsigned nparam
,
3188 barvinok_options
*options
= barvinok_options_new_with_defaults();
3189 options
->MaxRays
= MaxRays
;
3190 E
= barvinok_enumerate_e_with_options(P
, exist
, nparam
, options
);
3191 barvinok_options_free(options
);
3195 static evalue
* barvinok_enumerate_e_r(Polyhedron
*P
,
3196 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
3199 Polyhedron
*U
= Universe_Polyhedron(nparam
);
3200 evalue
*EP
= barvinok_enumerate_with_options(P
, U
, options
);
3201 //char *param_name[] = {"P", "Q", "R", "S", "T" };
3202 //print_evalue(stdout, EP, param_name);
3207 int nvar
= P
->Dimension
- exist
- nparam
;
3208 int len
= P
->Dimension
+ 2;
3211 POL_ENSURE_FACETS(P
);
3212 POL_ENSURE_VERTICES(P
);
3215 return evalue_zero();
3217 if (nvar
== 0 && nparam
== 0) {
3218 evalue
*EP
= evalue_zero();
3219 barvinok_count_with_options(P
, &EP
->x
.n
, options
);
3220 if (value_pos_p(EP
->x
.n
))
3221 value_set_si(EP
->x
.n
, 1);
3226 for (r
= 0; r
< P
->NbRays
; ++r
)
3227 if (value_zero_p(P
->Ray
[r
][0]) ||
3228 value_zero_p(P
->Ray
[r
][P
->Dimension
+1])) {
3230 for (i
= 0; i
< nvar
; ++i
)
3231 if (value_notzero_p(P
->Ray
[r
][i
+1]))
3235 for (i
= nvar
+ exist
; i
< nvar
+ exist
+ nparam
; ++i
)
3236 if (value_notzero_p(P
->Ray
[r
][i
+1]))
3238 if (i
>= nvar
+ exist
+ nparam
)
3241 if (r
< P
->NbRays
) {
3242 evalue
*EP
= evalue_zero();
3243 value_set_si(EP
->x
.n
, -1);
3248 for (r
= 0; r
< P
->NbEq
; ++r
)
3249 if ((first
= First_Non_Zero(P
->Constraint
[r
]+1+nvar
, exist
)) != -1)
3252 if (First_Non_Zero(P
->Constraint
[r
]+1+nvar
+first
+1,
3253 exist
-first
-1) != -1) {
3254 Polyhedron
*T
= rotate_along(P
, r
, nvar
, exist
, options
->MaxRays
);
3256 fprintf(stderr
, "\nER: Equality\n");
3257 #endif /* DEBUG_ER */
3258 evalue
*EP
= barvinok_enumerate_e_with_options(T
, exist
-1, nparam
,
3264 fprintf(stderr
, "\nER: Fixed\n");
3265 #endif /* DEBUG_ER */
3267 return barvinok_enumerate_e_with_options(P
, exist
-1, nparam
,
3270 Polyhedron
*T
= Polyhedron_Copy(P
);
3271 SwapColumns(T
, nvar
+1, nvar
+1+first
);
3272 evalue
*EP
= barvinok_enumerate_e_with_options(T
, exist
-1, nparam
,
3280 Vector
*row
= Vector_Alloc(len
);
3281 value_set_si(row
->p
[0], 1);
3286 enum constraint
* info
= new constraint
[exist
];
3287 for (int i
= 0; i
< exist
; ++i
) {
3289 for (int l
= P
->NbEq
; l
< P
->NbConstraints
; ++l
) {
3290 if (value_negz_p(P
->Constraint
[l
][nvar
+i
+1]))
3292 bool l_parallel
= is_single(P
->Constraint
[l
]+nvar
+1, i
, exist
);
3293 for (int u
= P
->NbEq
; u
< P
->NbConstraints
; ++u
) {
3294 if (value_posz_p(P
->Constraint
[u
][nvar
+i
+1]))
3296 bool lu_parallel
= l_parallel
||
3297 is_single(P
->Constraint
[u
]+nvar
+1, i
, exist
);
3298 value_oppose(f
, P
->Constraint
[u
][nvar
+i
+1]);
3299 Vector_Combine(P
->Constraint
[l
]+1, P
->Constraint
[u
]+1, row
->p
+1,
3300 f
, P
->Constraint
[l
][nvar
+i
+1], len
-1);
3301 if (!(info
[i
] & INDEPENDENT
)) {
3303 for (j
= 0; j
< exist
; ++j
)
3304 if (j
!= i
&& value_notzero_p(row
->p
[nvar
+j
+1]))
3307 //printf("independent: i: %d, l: %d, u: %d\n", i, l, u);
3308 info
[i
] = (constraint
)(info
[i
] | INDEPENDENT
);
3311 if (info
[i
] & ALL_POS
) {
3312 value_addto(row
->p
[len
-1], row
->p
[len
-1],
3313 P
->Constraint
[l
][nvar
+i
+1]);
3314 value_addto(row
->p
[len
-1], row
->p
[len
-1], f
);
3315 value_multiply(f
, f
, P
->Constraint
[l
][nvar
+i
+1]);
3316 value_subtract(row
->p
[len
-1], row
->p
[len
-1], f
);
3317 value_decrement(row
->p
[len
-1], row
->p
[len
-1]);
3318 ConstraintSimplify(row
->p
, row
->p
, len
, &f
);
3319 value_set_si(f
, -1);
3320 Vector_Scale(row
->p
+1, row
->p
+1, f
, len
-1);
3321 value_decrement(row
->p
[len
-1], row
->p
[len
-1]);
3322 Polyhedron
*T
= AddConstraints(row
->p
, 1, P
, options
->MaxRays
);
3324 //printf("not all_pos: i: %d, l: %d, u: %d\n", i, l, u);
3325 info
[i
] = (constraint
)(info
[i
] ^ ALL_POS
);
3327 //puts("pos remainder");
3328 //Polyhedron_Print(stdout, P_VALUE_FMT, T);
3331 if (!(info
[i
] & ONE_NEG
)) {
3333 negative_test_constraint(P
->Constraint
[l
],
3335 row
->p
, nvar
+i
, len
, &f
);
3336 oppose_constraint(row
->p
, len
, &f
);
3337 Polyhedron
*T
= AddConstraints(row
->p
, 1, P
,
3340 //printf("one_neg i: %d, l: %d, u: %d\n", i, l, u);
3341 info
[i
] = (constraint
)(info
[i
] | ONE_NEG
);
3343 //puts("neg remainder");
3344 //Polyhedron_Print(stdout, P_VALUE_FMT, T);
3346 } else if (!(info
[i
] & ROT_NEG
)) {
3347 if (parallel_constraints(P
->Constraint
[l
],
3349 row
->p
, nvar
, exist
)) {
3350 negative_test_constraint7(P
->Constraint
[l
],
3352 row
->p
, nvar
, exist
,
3354 oppose_constraint(row
->p
, len
, &f
);
3355 Polyhedron
*T
= AddConstraints(row
->p
, 1, P
,
3358 // printf("rot_neg i: %d, l: %d, u: %d\n", i, l, u);
3359 info
[i
] = (constraint
)(info
[i
] | ROT_NEG
);
3362 //puts("neg remainder");
3363 //Polyhedron_Print(stdout, P_VALUE_FMT, T);
3368 if (!(info
[i
] & ALL_POS
) && (info
[i
] & (ONE_NEG
| ROT_NEG
)))
3372 if (info
[i
] & ALL_POS
)
3379 for (int i = 0; i < exist; ++i)
3380 printf("%i: %i\n", i, info[i]);
3382 for (int i
= 0; i
< exist
; ++i
)
3383 if (info
[i
] & ALL_POS
) {
3385 fprintf(stderr
, "\nER: Positive\n");
3386 #endif /* DEBUG_ER */
3388 // Maybe we should chew off some of the fat here
3389 Matrix
*M
= Matrix_Alloc(P
->Dimension
, P
->Dimension
+1);
3390 for (int j
= 0; j
< P
->Dimension
; ++j
)
3391 value_set_si(M
->p
[j
][j
+ (j
>= i
+nvar
)], 1);
3392 Polyhedron
*T
= Polyhedron_Image(P
, M
, options
->MaxRays
);
3394 evalue
*EP
= barvinok_enumerate_e_with_options(T
, exist
-1, nparam
,
3402 for (int i
= 0; i
< exist
; ++i
)
3403 if (info
[i
] & ONE_NEG
) {
3405 fprintf(stderr
, "\nER: Negative\n");
3406 #endif /* DEBUG_ER */
3411 return barvinok_enumerate_e_with_options(P
, exist
-1, nparam
,
3414 Polyhedron
*T
= Polyhedron_Copy(P
);
3415 SwapColumns(T
, nvar
+1, nvar
+1+i
);
3416 evalue
*EP
= barvinok_enumerate_e_with_options(T
, exist
-1, nparam
,
3422 for (int i
= 0; i
< exist
; ++i
)
3423 if (info
[i
] & ROT_NEG
) {
3425 fprintf(stderr
, "\nER: Rotate\n");
3426 #endif /* DEBUG_ER */
3430 Polyhedron
*T
= rotate_along(P
, r
, nvar
, exist
, options
->MaxRays
);
3431 evalue
*EP
= barvinok_enumerate_e_with_options(T
, exist
-1, nparam
,
3436 for (int i
= 0; i
< exist
; ++i
)
3437 if (info
[i
] & INDEPENDENT
) {
3438 Polyhedron
*pos
, *neg
;
3440 /* Find constraint again and split off negative part */
3442 if (SplitOnVar(P
, i
, nvar
, exist
, options
->MaxRays
,
3443 row
, f
, true, &pos
, &neg
)) {
3445 fprintf(stderr
, "\nER: Split\n");
3446 #endif /* DEBUG_ER */
3449 barvinok_enumerate_e_with_options(neg
, exist
-1, nparam
, options
);
3451 barvinok_enumerate_e_with_options(pos
, exist
, nparam
, options
);
3453 free_evalue_refs(E
);
3455 Polyhedron_Free(neg
);
3456 Polyhedron_Free(pos
);
3470 EP
= enumerate_line(P
, exist
, nparam
, options
);
3474 EP
= barvinok_enumerate_pip_with_options(P
, exist
, nparam
, options
);
3478 EP
= enumerate_redundant_ray(P
, exist
, nparam
, options
);
3482 EP
= enumerate_sure(P
, exist
, nparam
, options
);
3486 EP
= enumerate_ray(P
, exist
, nparam
, options
);
3490 EP
= enumerate_sure2(P
, exist
, nparam
, options
);
3494 F
= unfringe(P
, options
->MaxRays
);
3495 if (!PolyhedronIncludes(F
, P
)) {
3497 fprintf(stderr
, "\nER: Fringed\n");
3498 #endif /* DEBUG_ER */
3499 EP
= barvinok_enumerate_e_with_options(F
, exist
, nparam
, options
);
3506 EP
= enumerate_vd(&P
, exist
, nparam
, options
);
3511 EP
= enumerate_sum(P
, exist
, nparam
, options
);
3518 Polyhedron
*pos
, *neg
;
3519 for (i
= 0; i
< exist
; ++i
)
3520 if (SplitOnVar(P
, i
, nvar
, exist
, options
->MaxRays
,
3521 row
, f
, false, &pos
, &neg
))
3527 EP
= enumerate_or(pos
, exist
, nparam
, options
);
3540 * remove equalities that require a "compression" of the parameters
3542 static Polyhedron
*remove_more_equalities(Polyhedron
*P
, unsigned nparam
,
3543 Matrix
**CP
, unsigned MaxRays
)
3546 remove_all_equalities(&P
, NULL
, CP
, NULL
, nparam
, MaxRays
);
3553 static gen_fun
*series(Polyhedron
*P
, unsigned nparam
, barvinok_options
*options
)
3563 assert(!Polyhedron_is_infinite_param(P
, nparam
));
3564 assert(P
->NbBid
== 0);
3565 assert(Polyhedron_has_revlex_positive_rays(P
, nparam
));
3567 P
= remove_more_equalities(P
, nparam
, &CP
, options
->MaxRays
);
3568 assert(P
->NbEq
== 0);
3570 nparam
= CP
->NbColumns
-1;
3575 barvinok_count_with_options(P
, &c
, options
);
3576 gf
= new gen_fun(c
);
3580 red
= gf_base::create(Polyhedron_Project(P
, nparam
),
3581 P
->Dimension
, nparam
, options
);
3582 POL_ENSURE_VERTICES(P
);
3583 red
->start_gf(P
, options
);
3595 gen_fun
* barvinok_series_with_options(Polyhedron
*P
, Polyhedron
* C
,
3596 barvinok_options
*options
)
3599 unsigned nparam
= C
->Dimension
;
3602 CA
= align_context(C
, P
->Dimension
, options
->MaxRays
);
3603 P
= DomainIntersection(P
, CA
, options
->MaxRays
);
3604 Polyhedron_Free(CA
);
3606 gf
= series(P
, nparam
, options
);
3611 gen_fun
* barvinok_series(Polyhedron
*P
, Polyhedron
* C
, unsigned MaxRays
)
3614 barvinok_options
*options
= barvinok_options_new_with_defaults();
3615 options
->MaxRays
= MaxRays
;
3616 gf
= barvinok_series_with_options(P
, C
, options
);
3617 barvinok_options_free(options
);
3621 static Polyhedron
*skew_into_positive_orthant(Polyhedron
*D
, unsigned nparam
,
3627 for (Polyhedron
*P
= D
; P
; P
= P
->next
) {
3628 POL_ENSURE_VERTICES(P
);
3629 assert(!Polyhedron_is_infinite_param(P
, nparam
));
3630 assert(P
->NbBid
== 0);
3631 assert(Polyhedron_has_positive_rays(P
, nparam
));
3633 for (int r
= 0; r
< P
->NbRays
; ++r
) {
3634 if (value_notzero_p(P
->Ray
[r
][P
->Dimension
+1]))
3636 for (int i
= 0; i
< nparam
; ++i
) {
3638 if (value_posz_p(P
->Ray
[r
][i
+1]))
3641 M
= Matrix_Alloc(D
->Dimension
+1, D
->Dimension
+1);
3642 for (int i
= 0; i
< D
->Dimension
+1; ++i
)
3643 value_set_si(M
->p
[i
][i
], 1);
3645 Inner_Product(P
->Ray
[r
]+1, M
->p
[i
], D
->Dimension
+1, &tmp
);
3646 if (value_posz_p(tmp
))
3649 for (j
= P
->Dimension
- nparam
; j
< P
->Dimension
; ++j
)
3650 if (value_pos_p(P
->Ray
[r
][j
+1]))
3652 assert(j
< P
->Dimension
);
3653 value_pdivision(tmp
, P
->Ray
[r
][j
+1], P
->Ray
[r
][i
+1]);
3654 value_subtract(M
->p
[i
][j
], M
->p
[i
][j
], tmp
);
3660 D
= DomainImage(D
, M
, MaxRays
);
3666 gen_fun
* barvinok_enumerate_union_series_with_options(Polyhedron
*D
, Polyhedron
* C
,
3667 barvinok_options
*options
)
3669 Polyhedron
*conv
, *D2
;
3671 gen_fun
*gf
= NULL
, *gf2
;
3672 unsigned nparam
= C
->Dimension
;
3677 CA
= align_context(C
, D
->Dimension
, options
->MaxRays
);
3678 D
= DomainIntersection(D
, CA
, options
->MaxRays
);
3679 Polyhedron_Free(CA
);
3681 D2
= skew_into_positive_orthant(D
, nparam
, options
->MaxRays
);
3682 for (Polyhedron
*P
= D2
; P
; P
= P
->next
) {
3683 assert(P
->Dimension
== D2
->Dimension
);
3686 P_gf
= series(Polyhedron_Copy(P
), nparam
, options
);
3690 gf
->add_union(P_gf
, options
);
3694 /* we actually only need the convex union of the parameter space
3695 * but the reducer classes currently expect a polyhedron in
3696 * the combined space
3698 Polyhedron_Free(gf
->context
);
3699 gf
->context
= DomainConvex(D2
, options
->MaxRays
);
3701 gf2
= gf
->summate(D2
->Dimension
- nparam
, options
);
3710 gen_fun
* barvinok_enumerate_union_series(Polyhedron
*D
, Polyhedron
* C
,
3714 barvinok_options
*options
= barvinok_options_new_with_defaults();
3715 options
->MaxRays
= MaxRays
;
3716 gf
= barvinok_enumerate_union_series_with_options(D
, C
, options
);
3717 barvinok_options_free(options
);
3721 evalue
* barvinok_enumerate_union(Polyhedron
*D
, Polyhedron
* C
, unsigned MaxRays
)
3724 gen_fun
*gf
= barvinok_enumerate_union_series(D
, C
, MaxRays
);