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"
37 using std::ostringstream
;
39 #define ALLOC(t,p) p = (t*)malloc(sizeof(*p))
47 dpoly_n(int d
, ZZ
& degree_0
, ZZ
& degree_1
, int offset
= 0) {
51 zz2value(degree_0
, d0
);
52 zz2value(degree_1
, d1
);
53 coeff
= Matrix_Alloc(d
+1, d
+1+1);
54 value_set_si(coeff
->p
[0][0], 1);
55 value_set_si(coeff
->p
[0][d
+1], 1);
56 for (int i
= 1; i
<= d
; ++i
) {
57 value_multiply(coeff
->p
[i
][0], coeff
->p
[i
-1][0], d0
);
58 Vector_Combine(coeff
->p
[i
-1], coeff
->p
[i
-1]+1, coeff
->p
[i
]+1,
60 value_set_si(coeff
->p
[i
][d
+1], i
);
61 value_multiply(coeff
->p
[i
][d
+1], coeff
->p
[i
][d
+1], coeff
->p
[i
-1][d
+1]);
62 value_decrement(d0
, d0
);
67 void div(dpoly
& d
, Vector
*count
, ZZ
& sign
) {
68 int len
= coeff
->NbRows
;
69 Matrix
* c
= Matrix_Alloc(coeff
->NbRows
, coeff
->NbColumns
);
72 for (int i
= 0; i
< len
; ++i
) {
73 Vector_Copy(coeff
->p
[i
], c
->p
[i
], len
+1);
74 for (int j
= 1; j
<= i
; ++j
) {
75 zz2value(d
.coeff
[j
], tmp
);
76 value_multiply(tmp
, tmp
, c
->p
[i
][len
]);
77 value_oppose(tmp
, tmp
);
78 Vector_Combine(c
->p
[i
], c
->p
[i
-j
], c
->p
[i
],
79 c
->p
[i
-j
][len
], tmp
, len
);
80 value_multiply(c
->p
[i
][len
], c
->p
[i
][len
], c
->p
[i
-j
][len
]);
82 zz2value(d
.coeff
[0], tmp
);
83 value_multiply(c
->p
[i
][len
], c
->p
[i
][len
], tmp
);
86 value_set_si(tmp
, -1);
87 Vector_Scale(c
->p
[len
-1], count
->p
, tmp
, len
);
88 value_assign(count
->p
[len
], c
->p
[len
-1][len
]);
90 Vector_Copy(c
->p
[len
-1], count
->p
, len
+1);
91 Vector_Normalize(count
->p
, len
+1);
99 * Searches for a vector that is not orthogonal to any
100 * of the rays in rays.
102 static void nonorthog(mat_ZZ
& rays
, vec_ZZ
& lambda
)
104 int dim
= rays
.NumCols();
106 lambda
.SetLength(dim
);
110 for (int i
= 2; !found
&& i
<= 50*dim
; i
+=4) {
111 for (int j
= 0; j
< MAX_TRY
; ++j
) {
112 for (int k
= 0; k
< dim
; ++k
) {
113 int r
= random_int(i
)+2;
114 int v
= (2*(r
%2)-1) * (r
>> 1);
118 for (; k
< rays
.NumRows(); ++k
)
119 if (lambda
* rays
[k
] == 0)
121 if (k
== rays
.NumRows()) {
130 static void add_rays(mat_ZZ
& rays
, Polyhedron
*i
, int *r
, int nvar
= -1,
133 unsigned dim
= i
->Dimension
;
136 for (int k
= 0; k
< i
->NbRays
; ++k
) {
137 if (!value_zero_p(i
->Ray
[k
][dim
+1]))
139 if (!all
&& nvar
!= dim
&& First_Non_Zero(i
->Ray
[k
]+1, nvar
) == -1)
141 values2zz(i
->Ray
[k
]+1, rays
[(*r
)++], nvar
);
145 static void mask_r(Matrix
*f
, int nr
, Vector
*lcm
, int p
, Vector
*val
, evalue
*ev
)
147 unsigned nparam
= lcm
->Size
;
150 Vector
* prod
= Vector_Alloc(f
->NbRows
);
151 Matrix_Vector_Product(f
, val
->p
, prod
->p
);
153 for (int i
= 0; i
< nr
; ++i
) {
154 value_modulus(prod
->p
[i
], prod
->p
[i
], f
->p
[i
][nparam
+1]);
155 isint
&= value_zero_p(prod
->p
[i
]);
157 value_set_si(ev
->d
, 1);
159 value_set_si(ev
->x
.n
, isint
);
166 if (value_one_p(lcm
->p
[p
]))
167 mask_r(f
, nr
, lcm
, p
+1, val
, ev
);
169 value_assign(tmp
, lcm
->p
[p
]);
170 value_set_si(ev
->d
, 0);
171 ev
->x
.p
= new_enode(periodic
, VALUE_TO_INT(tmp
), p
+1);
173 value_decrement(tmp
, tmp
);
174 value_assign(val
->p
[p
], tmp
);
175 mask_r(f
, nr
, lcm
, p
+1, val
, &ev
->x
.p
->arr
[VALUE_TO_INT(tmp
)]);
176 } while (value_pos_p(tmp
));
181 static void mask_fractional(Matrix
*f
, evalue
*factor
)
183 int nr
= f
->NbRows
, nc
= f
->NbColumns
;
186 for (n
= 0; n
< nr
&& value_notzero_p(f
->p
[n
][nc
-1]); ++n
)
187 if (value_notone_p(f
->p
[n
][nc
-1]) &&
188 value_notmone_p(f
->p
[n
][nc
-1]))
202 value_set_si(EV
.x
.n
, 1);
204 for (n
= 0; n
< nr
; ++n
) {
205 value_assign(m
, f
->p
[n
][nc
-1]);
206 if (value_one_p(m
) || value_mone_p(m
))
209 int j
= normal_mod(f
->p
[n
], nc
-1, &m
);
211 free_evalue_refs(factor
);
212 value_init(factor
->d
);
213 evalue_set_si(factor
, 0, 1);
217 values2zz(f
->p
[n
], row
, nc
-1);
220 if (j
< (nc
-1)-1 && row
[j
] > g
/2) {
221 for (int k
= j
; k
< (nc
-1); ++k
)
227 value_set_si(EP
.d
, 0);
228 EP
.x
.p
= new_enode(relation
, 2, 0);
229 value_clear(EP
.x
.p
->arr
[1].d
);
230 EP
.x
.p
->arr
[1] = *factor
;
231 evalue
*ev
= &EP
.x
.p
->arr
[0];
232 value_set_si(ev
->d
, 0);
233 ev
->x
.p
= new_enode(fractional
, 3, -1);
234 evalue_set_si(&ev
->x
.p
->arr
[1], 0, 1);
235 evalue_set_si(&ev
->x
.p
->arr
[2], 1, 1);
236 evalue
*E
= multi_monom(row
);
237 value_assign(EV
.d
, m
);
239 value_clear(ev
->x
.p
->arr
[0].d
);
240 ev
->x
.p
->arr
[0] = *E
;
246 free_evalue_refs(&EV
);
252 static void mask_table(Matrix
*f
, evalue
*factor
)
254 int nr
= f
->NbRows
, nc
= f
->NbColumns
;
257 for (n
= 0; n
< nr
&& value_notzero_p(f
->p
[n
][nc
-1]); ++n
)
258 if (value_notone_p(f
->p
[n
][nc
-1]) &&
259 value_notmone_p(f
->p
[n
][nc
-1]))
267 unsigned np
= nc
- 2;
268 Vector
*lcm
= Vector_Alloc(np
);
269 Vector
*val
= Vector_Alloc(nc
);
270 Vector_Set(val
->p
, 0, nc
);
271 value_set_si(val
->p
[np
], 1);
272 Vector_Set(lcm
->p
, 1, np
);
273 for (n
= 0; n
< nr
; ++n
) {
274 if (value_one_p(f
->p
[n
][nc
-1]) ||
275 value_mone_p(f
->p
[n
][nc
-1]))
277 for (int j
= 0; j
< np
; ++j
)
278 if (value_notzero_p(f
->p
[n
][j
])) {
279 Gcd(f
->p
[n
][j
], f
->p
[n
][nc
-1], &tmp
);
280 value_division(tmp
, f
->p
[n
][nc
-1], tmp
);
281 value_lcm(tmp
, lcm
->p
[j
], &lcm
->p
[j
]);
286 mask_r(f
, nr
, lcm
, 0, val
, &EP
);
291 free_evalue_refs(&EP
);
294 static void mask(Matrix
*f
, evalue
*factor
, barvinok_options
*options
)
296 if (options
->lookup_table
)
297 mask_table(f
, factor
);
299 mask_fractional(f
, factor
);
302 /* This structure encodes the power of the term in a rational generating function.
304 * Either E == NULL or constant = 0
305 * If E != NULL, then the power is E
306 * If E == NULL, then the power is coeff * param[pos] + constant
315 /* Returns the power of (t+1) in the term of a rational generating function,
316 * i.e., the scalar product of the actual lattice point and lambda.
317 * The lattice point is the unique lattice point in the fundamental parallelepiped
318 * of the unimodual cone i shifted to the parametric vertex V.
320 * PD is the parameter domain, which, if != NULL, may be used to simply the
321 * resulting expression.
323 * The result is returned in term.
325 void lattice_point(Param_Vertices
* V
, const mat_ZZ
& rays
, vec_ZZ
& lambda
,
326 term_info
* term
, Polyhedron
*PD
, barvinok_options
*options
)
328 unsigned nparam
= V
->Vertex
->NbColumns
- 2;
329 unsigned dim
= rays
.NumCols();
331 vertex
.SetDims(V
->Vertex
->NbRows
, nparam
+1);
335 value_set_si(lcm
, 1);
336 for (int j
= 0; j
< V
->Vertex
->NbRows
; ++j
) {
337 value_lcm(lcm
, V
->Vertex
->p
[j
][nparam
+1], &lcm
);
339 if (value_notone_p(lcm
)) {
340 Matrix
* mv
= Matrix_Alloc(dim
, nparam
+1);
341 for (int j
= 0 ; j
< dim
; ++j
) {
342 value_division(tmp
, lcm
, V
->Vertex
->p
[j
][nparam
+1]);
343 Vector_Scale(V
->Vertex
->p
[j
], mv
->p
[j
], tmp
, nparam
+1);
346 term
->E
= lattice_point(rays
, lambda
, mv
, lcm
, PD
, options
);
354 for (int i
= 0; i
< V
->Vertex
->NbRows
; ++i
) {
355 assert(value_one_p(V
->Vertex
->p
[i
][nparam
+1])); // for now
356 values2zz(V
->Vertex
->p
[i
], vertex
[i
], nparam
+1);
360 num
= lambda
* vertex
;
364 for (int j
= 0; j
< nparam
; ++j
)
370 term
->E
= multi_monom(num
);
374 term
->constant
= num
[nparam
];
377 term
->coeff
= num
[p
];
385 struct counter
: public np_base
{
395 counter(unsigned dim
) : np_base(dim
) {
400 virtual void init(Polyhedron
*P
) {
401 randomvector(P
, lambda
, dim
);
404 virtual void reset() {
405 mpq_set_si(count
, 0, 0);
412 virtual void handle(const mat_ZZ
& rays
, Value
*vertex
, const QQ
& c
,
413 unsigned long det
, int *closed
, barvinok_options
*options
);
414 virtual void get_count(Value
*result
) {
415 assert(value_one_p(&count
[0]._mp_den
));
416 value_assign(*result
, &count
[0]._mp_num
);
420 void counter::handle(const mat_ZZ
& rays
, Value
*V
, const QQ
& c
, unsigned long det
,
421 int *closed
, barvinok_options
*options
)
423 for (int k
= 0; k
< dim
; ++k
) {
424 if (lambda
* rays
[k
] == 0)
429 assert(c
.n
== 1 || c
.n
== -1);
432 lattice_point(V
, rays
, vertex
, det
, closed
);
433 num
= vertex
* lambda
;
436 normalize(sign
, offset
, den
);
439 dpoly
d(dim
, num
[0]);
440 for (int k
= 1; k
< num
.length(); ++k
) {
442 dpoly
term(dim
, num
[k
]);
445 dpoly
n(dim
, den
[0], 1);
446 for (int k
= 1; k
< dim
; ++k
) {
447 dpoly
fact(dim
, den
[k
], 1);
450 d
.div(n
, count
, sign
);
453 struct bfe_term
: public bfc_term_base
{
454 vector
<evalue
*> factors
;
456 bfe_term(int len
) : bfc_term_base(len
) {
460 for (int i
= 0; i
< factors
.size(); ++i
) {
463 free_evalue_refs(factors
[i
]);
469 static void print_int_vector(int *v
, int len
, char *name
)
471 cerr
<< name
<< endl
;
472 for (int j
= 0; j
< len
; ++j
) {
478 static void print_bfc_terms(mat_ZZ
& factors
, bfc_vec
& v
)
481 cerr
<< "factors" << endl
;
482 cerr
<< factors
<< endl
;
483 for (int i
= 0; i
< v
.size(); ++i
) {
484 cerr
<< "term: " << i
<< endl
;
485 print_int_vector(v
[i
]->powers
, factors
.NumRows(), "powers");
486 cerr
<< "terms" << endl
;
487 cerr
<< v
[i
]->terms
<< endl
;
488 bfc_term
* bfct
= static_cast<bfc_term
*>(v
[i
]);
489 cerr
<< bfct
->c
<< endl
;
493 static void print_bfe_terms(mat_ZZ
& factors
, bfc_vec
& v
)
496 cerr
<< "factors" << endl
;
497 cerr
<< factors
<< endl
;
498 for (int i
= 0; i
< v
.size(); ++i
) {
499 cerr
<< "term: " << i
<< endl
;
500 print_int_vector(v
[i
]->powers
, factors
.NumRows(), "powers");
501 cerr
<< "terms" << endl
;
502 cerr
<< v
[i
]->terms
<< endl
;
503 bfe_term
* bfet
= static_cast<bfe_term
*>(v
[i
]);
504 for (int j
= 0; j
< v
[i
]->terms
.NumRows(); ++j
) {
505 char * test
[] = {"a", "b"};
506 print_evalue(stderr
, bfet
->factors
[j
], test
);
507 fprintf(stderr
, "\n");
512 struct bfcounter
: public bfcounter_base
{
515 bfcounter(unsigned dim
) : bfcounter_base(dim
) {
522 virtual void base(mat_ZZ
& factors
, bfc_vec
& v
);
523 virtual void get_count(Value
*result
) {
524 assert(value_one_p(&count
[0]._mp_den
));
525 value_assign(*result
, &count
[0]._mp_num
);
529 void bfcounter::base(mat_ZZ
& factors
, bfc_vec
& v
)
531 unsigned nf
= factors
.NumRows();
533 for (int i
= 0; i
< v
.size(); ++i
) {
534 bfc_term
* bfct
= static_cast<bfc_term
*>(v
[i
]);
536 // factor is always positive, so we always
538 for (int k
= 0; k
< nf
; ++k
)
539 total_power
+= v
[i
]->powers
[k
];
542 for (j
= 0; j
< nf
; ++j
)
543 if (v
[i
]->powers
[j
] > 0)
546 dpoly
D(total_power
, factors
[j
][0], 1);
547 for (int k
= 1; k
< v
[i
]->powers
[j
]; ++k
) {
548 dpoly
fact(total_power
, factors
[j
][0], 1);
552 for (int k
= 0; k
< v
[i
]->powers
[j
]; ++k
) {
553 dpoly
fact(total_power
, factors
[j
][0], 1);
557 for (int k
= 0; k
< v
[i
]->terms
.NumRows(); ++k
) {
558 dpoly
n(total_power
, v
[i
]->terms
[k
][0]);
559 mpq_set_si(tcount
, 0, 1);
560 n
.div(D
, tcount
, one
);
562 bfct
->c
[k
].n
= -bfct
->c
[k
].n
;
563 zz2value(bfct
->c
[k
].n
, tn
);
564 zz2value(bfct
->c
[k
].d
, td
);
566 mpz_mul(mpq_numref(tcount
), mpq_numref(tcount
), tn
);
567 mpz_mul(mpq_denref(tcount
), mpq_denref(tcount
), td
);
568 mpq_canonicalize(tcount
);
569 mpq_add(count
, count
, tcount
);
576 /* Check whether the polyhedron is unbounded and if so,
577 * check whether it has any (and therefore an infinite number of)
579 * If one of the vertices is integer, then we are done.
580 * Otherwise, transform the polyhedron such that one of the rays
581 * is the first unit vector and cut it off at a height that ensures
582 * that if the whole polyhedron has any points, then the remaining part
583 * has integer points. In particular we add the largest coefficient
584 * of a ray to the highest vertex (rounded up).
586 static bool Polyhedron_is_infinite(Polyhedron
*P
, Value
* result
,
587 barvinok_options
*options
)
599 for (; r
< P
->NbRays
; ++r
)
600 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1]))
602 if (P
->NbBid
== 0 && r
== P
->NbRays
)
605 if (options
->count_sample_infinite
) {
608 sample
= Polyhedron_Sample(P
, options
);
610 value_set_si(*result
, 0);
612 value_set_si(*result
, -1);
618 for (int i
= 0; i
< P
->NbRays
; ++i
)
619 if (value_one_p(P
->Ray
[i
][1+P
->Dimension
])) {
620 value_set_si(*result
, -1);
625 v
= Vector_Alloc(P
->Dimension
+1);
626 Vector_Gcd(P
->Ray
[r
]+1, P
->Dimension
, &g
);
627 Vector_AntiScale(P
->Ray
[r
]+1, v
->p
, g
, P
->Dimension
+1);
628 M
= unimodular_complete(v
);
629 value_set_si(M
->p
[P
->Dimension
][P
->Dimension
], 1);
632 P
= Polyhedron_Preimage(P
, M2
, 0);
641 value_set_si(size
, 0);
643 for (int i
= 0; i
< P
->NbBid
; ++i
) {
644 value_absolute(tmp
, P
->Ray
[i
][1]);
645 if (value_gt(tmp
, size
))
646 value_assign(size
, tmp
);
648 for (int i
= P
->NbBid
; i
< P
->NbRays
; ++i
) {
649 if (value_zero_p(P
->Ray
[i
][P
->Dimension
+1])) {
650 if (value_gt(P
->Ray
[i
][1], size
))
651 value_assign(size
, P
->Ray
[i
][1]);
654 mpz_cdiv_q(tmp
, P
->Ray
[i
][1], P
->Ray
[i
][P
->Dimension
+1]);
655 if (first
|| value_gt(tmp
, offset
)) {
656 value_assign(offset
, tmp
);
660 value_addto(offset
, offset
, size
);
664 v
= Vector_Alloc(P
->Dimension
+2);
665 value_set_si(v
->p
[0], 1);
666 value_set_si(v
->p
[1], -1);
667 value_assign(v
->p
[1+P
->Dimension
], offset
);
668 R
= AddConstraints(v
->p
, 1, P
, options
->MaxRays
);
676 barvinok_count_with_options(P
, &c
, options
);
679 value_set_si(*result
, 0);
681 value_set_si(*result
, -1);
687 typedef Polyhedron
* Polyhedron_p
;
689 static void barvinok_count_f(Polyhedron
*P
, Value
* result
,
690 barvinok_options
*options
);
692 void barvinok_count_with_options(Polyhedron
*P
, Value
* result
,
693 struct barvinok_options
*options
)
698 bool infinite
= false;
702 "barvinok_count: input is a union; only first polyhedron is counted\n");
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, NULL
, 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 /* Check whether all rays point in the positive directions
890 static bool Polyhedron_has_positive_rays(Polyhedron
*P
, unsigned nparam
)
893 for (r
= 0; r
< P
->NbRays
; ++r
)
894 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1])) {
896 for (i
= P
->Dimension
- nparam
; i
< P
->Dimension
; ++i
)
897 if (value_neg_p(P
->Ray
[r
][i
+1]))
903 typedef evalue
* evalue_p
;
905 struct enumerator_base
{
909 vertex_decomposer
*vpd
;
911 enumerator_base(unsigned dim
, vertex_decomposer
*vpd
)
916 vE
= new evalue_p
[vpd
->nbV
];
917 for (int j
= 0; j
< vpd
->nbV
; ++j
)
921 evalue_set_si(&mone
, -1, 1);
924 void decompose_at(Param_Vertices
*V
, int _i
, barvinok_options
*options
) {
928 value_init(vE
[_i
]->d
);
929 evalue_set_si(vE
[_i
], 0, 1);
931 vpd
->decompose_at_vertex(V
, _i
, options
);
934 virtual ~enumerator_base() {
935 for (int j
= 0; j
< vpd
->nbV
; ++j
)
937 free_evalue_refs(vE
[j
]);
942 free_evalue_refs(&mone
);
945 static enumerator_base
*create(Polyhedron
*P
, unsigned dim
, unsigned nbV
,
946 barvinok_options
*options
);
949 struct enumerator
: public signed_cone_consumer
, public vertex_decomposer
,
950 public enumerator_base
{
958 enumerator(Polyhedron
*P
, unsigned dim
, unsigned nbV
) :
959 vertex_decomposer(P
, nbV
, *this), enumerator_base(dim
, this) {
962 randomvector(P
, lambda
, dim
);
964 c
= Vector_Alloc(dim
+2);
974 virtual void handle(const signed_cone
& sc
, barvinok_options
*options
);
977 void enumerator::handle(const signed_cone
& sc
, barvinok_options
*options
)
982 assert(sc
.rays
.NumRows() == dim
);
983 for (int k
= 0; k
< dim
; ++k
) {
984 if (lambda
* sc
.rays
[k
] == 0)
990 lattice_point(V
, sc
.rays
, lambda
, &num
, 0, options
);
991 den
= sc
.rays
* lambda
;
992 normalize(sign
, num
.constant
, den
);
994 dpoly
n(dim
, den
[0], 1);
995 for (int k
= 1; k
< dim
; ++k
) {
996 dpoly
fact(dim
, den
[k
], 1);
1000 ZZ
one(INIT_VAL
, 1);
1001 dpoly_n
d(dim
, num
.constant
, one
);
1004 multi_polynom(c
, num
.E
, &EV
);
1005 eadd(&EV
, vE
[vert
]);
1006 free_evalue_refs(&EV
);
1007 free_evalue_refs(num
.E
);
1009 } else if (num
.pos
!= -1) {
1010 dpoly_n
d(dim
, num
.constant
, num
.coeff
);
1013 uni_polynom(num
.pos
, c
, &EV
);
1014 eadd(&EV
, vE
[vert
]);
1015 free_evalue_refs(&EV
);
1017 mpq_set_si(count
, 0, 1);
1018 dpoly
d(dim
, num
.constant
);
1019 d
.div(n
, count
, sign
);
1022 evalue_set(&EV
, &count
[0]._mp_num
, &count
[0]._mp_den
);
1023 eadd(&EV
, vE
[vert
]);
1024 free_evalue_refs(&EV
);
1028 struct ienumerator_base
: enumerator_base
{
1031 ienumerator_base(unsigned dim
, vertex_decomposer
*vpd
) :
1032 enumerator_base(dim
,vpd
) {
1033 E_vertex
= new evalue_p
[dim
];
1036 virtual ~ienumerator_base() {
1040 evalue
*E_num(int i
, int d
) {
1041 return E_vertex
[i
+ (dim
-d
)];
1050 cumulator(evalue
*factor
, evalue
*v
, dpoly_r
*r
) :
1051 factor(factor
), v(v
), r(r
) {}
1053 void cumulate(barvinok_options
*options
);
1055 virtual void add_term(const vector
<int>& powers
, evalue
*f2
) = 0;
1056 virtual ~cumulator() {}
1059 void cumulator::cumulate(barvinok_options
*options
)
1061 evalue cum
; // factor * 1 * E_num[0]/1 * (E_num[0]-1)/2 *...
1063 evalue t
; // E_num[0] - (m-1)
1067 if (options
->lookup_table
) {
1069 evalue_set_si(&mone
, -1, 1);
1073 evalue_copy(&cum
, factor
);
1076 value_set_si(f
.d
, 1);
1077 value_set_si(f
.x
.n
, 1);
1081 if (!options
->lookup_table
) {
1082 for (cst
= &t
; value_zero_p(cst
->d
); ) {
1083 if (cst
->x
.p
->type
== fractional
)
1084 cst
= &cst
->x
.p
->arr
[1];
1086 cst
= &cst
->x
.p
->arr
[0];
1090 for (int m
= 0; m
< r
->len
; ++m
) {
1093 value_set_si(f
.d
, m
);
1095 if (!options
->lookup_table
)
1096 value_subtract(cst
->x
.n
, cst
->x
.n
, cst
->d
);
1102 dpoly_r_term_list
& current
= r
->c
[r
->len
-1-m
];
1103 dpoly_r_term_list::iterator j
;
1104 for (j
= current
.begin(); j
!= current
.end(); ++j
) {
1105 if ((*j
)->coeff
== 0)
1107 evalue
*f2
= new evalue
;
1109 value_init(f2
->x
.n
);
1110 zz2value((*j
)->coeff
, f2
->x
.n
);
1111 zz2value(r
->denom
, f2
->d
);
1114 add_term((*j
)->powers
, f2
);
1117 free_evalue_refs(&f
);
1118 free_evalue_refs(&t
);
1119 free_evalue_refs(&cum
);
1120 if (options
->lookup_table
)
1121 free_evalue_refs(&mone
);
1124 struct E_poly_term
{
1129 struct ie_cum
: public cumulator
{
1130 vector
<E_poly_term
*> terms
;
1132 ie_cum(evalue
*factor
, evalue
*v
, dpoly_r
*r
) : cumulator(factor
, v
, r
) {}
1134 virtual void add_term(const vector
<int>& powers
, evalue
*f2
);
1137 void ie_cum::add_term(const vector
<int>& powers
, evalue
*f2
)
1140 for (k
= 0; k
< terms
.size(); ++k
) {
1141 if (terms
[k
]->powers
== powers
) {
1142 eadd(f2
, terms
[k
]->E
);
1143 free_evalue_refs(f2
);
1148 if (k
>= terms
.size()) {
1149 E_poly_term
*ET
= new E_poly_term
;
1150 ET
->powers
= powers
;
1152 terms
.push_back(ET
);
1156 struct ienumerator
: public signed_cone_consumer
, public vertex_decomposer
,
1157 public ienumerator_base
{
1163 ienumerator(Polyhedron
*P
, unsigned dim
, unsigned nbV
) :
1164 vertex_decomposer(P
, nbV
, *this), ienumerator_base(dim
, this) {
1165 vertex
.SetDims(1, dim
);
1167 den
.SetDims(dim
, dim
);
1175 virtual void handle(const signed_cone
& sc
, barvinok_options
*options
);
1176 void reduce(evalue
*factor
, const mat_ZZ
& num
, const mat_ZZ
& den_f
,
1177 barvinok_options
*options
);
1180 void ienumerator::reduce(evalue
*factor
, const mat_ZZ
& num
, const mat_ZZ
& den_f
,
1181 barvinok_options
*options
)
1183 unsigned len
= den_f
.NumRows(); // number of factors in den
1184 unsigned dim
= num
.NumCols();
1185 assert(num
.NumRows() == 1);
1188 eadd(factor
, vE
[vert
]);
1197 split_one(num
, num_s
, num_p
, den_f
, den_s
, den_r
);
1200 den_p
.SetLength(len
);
1204 normalize(one
, num_s
, num_p
, den_s
, den_p
, den_r
);
1206 emul(&mone
, factor
);
1210 for (int k
= 0; k
< len
; ++k
) {
1213 else if (den_s
[k
] == 0)
1216 if (no_param
== 0) {
1217 reduce(factor
, num_p
, den_r
, options
);
1221 pden
.SetDims(only_param
, dim
-1);
1223 for (k
= 0, l
= 0; k
< len
; ++k
)
1225 pden
[l
++] = den_r
[k
];
1227 for (k
= 0; k
< len
; ++k
)
1231 dpoly
n(no_param
, num_s
[0]);
1232 dpoly
D(no_param
, den_s
[k
], 1);
1233 for ( ; ++k
< len
; )
1234 if (den_p
[k
] == 0) {
1235 dpoly
fact(no_param
, den_s
[k
], 1);
1240 // if no_param + only_param == len then all powers
1241 // below will be all zero
1242 if (no_param
+ only_param
== len
) {
1243 if (E_num(0, dim
) != 0)
1244 r
= new dpoly_r(n
, len
);
1246 mpq_set_si(tcount
, 0, 1);
1248 n
.div(D
, tcount
, one
);
1250 if (value_notzero_p(mpq_numref(tcount
))) {
1254 value_assign(f
.x
.n
, mpq_numref(tcount
));
1255 value_assign(f
.d
, mpq_denref(tcount
));
1257 reduce(factor
, num_p
, pden
, options
);
1258 free_evalue_refs(&f
);
1263 for (k
= 0; k
< len
; ++k
) {
1264 if (den_s
[k
] == 0 || den_p
[k
] == 0)
1267 dpoly
pd(no_param
-1, den_s
[k
], 1);
1270 for (l
= 0; l
< k
; ++l
)
1271 if (den_r
[l
] == den_r
[k
])
1275 r
= new dpoly_r(n
, pd
, l
, len
);
1277 dpoly_r
*nr
= new dpoly_r(r
, pd
, l
, len
);
1283 dpoly_r
*rc
= r
->div(D
);
1286 if (E_num(0, dim
) == 0) {
1287 int common
= pden
.NumRows();
1288 dpoly_r_term_list
& final
= r
->c
[r
->len
-1];
1294 zz2value(r
->denom
, f
.d
);
1295 dpoly_r_term_list::iterator j
;
1296 for (j
= final
.begin(); j
!= final
.end(); ++j
) {
1297 if ((*j
)->coeff
== 0)
1300 for (int k
= 0; k
< r
->dim
; ++k
) {
1301 int n
= (*j
)->powers
[k
];
1304 pden
.SetDims(rows
+n
, pden
.NumCols());
1305 for (int l
= 0; l
< n
; ++l
)
1306 pden
[rows
+l
] = den_r
[k
];
1310 evalue_copy(&t
, factor
);
1311 zz2value((*j
)->coeff
, f
.x
.n
);
1313 reduce(&t
, num_p
, pden
, options
);
1314 free_evalue_refs(&t
);
1316 free_evalue_refs(&f
);
1318 ie_cum
cum(factor
, E_num(0, dim
), r
);
1319 cum
.cumulate(options
);
1321 int common
= pden
.NumRows();
1323 for (int j
= 0; j
< cum
.terms
.size(); ++j
) {
1325 pden
.SetDims(rows
, pden
.NumCols());
1326 for (int k
= 0; k
< r
->dim
; ++k
) {
1327 int n
= cum
.terms
[j
]->powers
[k
];
1330 pden
.SetDims(rows
+n
, pden
.NumCols());
1331 for (int l
= 0; l
< n
; ++l
)
1332 pden
[rows
+l
] = den_r
[k
];
1335 reduce(cum
.terms
[j
]->E
, num_p
, pden
, options
);
1336 free_evalue_refs(cum
.terms
[j
]->E
);
1337 delete cum
.terms
[j
]->E
;
1338 delete cum
.terms
[j
];
1345 static int type_offset(enode
*p
)
1347 return p
->type
== fractional
? 1 :
1348 p
->type
== flooring
? 1 : 0;
1351 static int edegree(evalue
*e
)
1356 if (value_notzero_p(e
->d
))
1360 int i
= type_offset(p
);
1361 if (p
->size
-i
-1 > d
)
1362 d
= p
->size
- i
- 1;
1363 for (; i
< p
->size
; i
++) {
1364 int d2
= edegree(&p
->arr
[i
]);
1371 void ienumerator::handle(const signed_cone
& sc
, barvinok_options
*options
)
1373 assert(sc
.det
== 1);
1375 assert(sc
.rays
.NumRows() == dim
);
1377 lattice_point(V
, sc
.rays
, vertex
[0], E_vertex
, options
);
1383 evalue_set_si(&one
, sc
.sign
, 1);
1384 reduce(&one
, vertex
, den
, options
);
1385 free_evalue_refs(&one
);
1387 for (int i
= 0; i
< dim
; ++i
)
1389 free_evalue_refs(E_vertex
[i
]);
1394 struct bfenumerator
: public vertex_decomposer
, public bf_base
,
1395 public ienumerator_base
{
1398 bfenumerator(Polyhedron
*P
, unsigned dim
, unsigned nbV
) :
1399 vertex_decomposer(P
, nbV
, *this),
1400 bf_base(dim
), ienumerator_base(dim
, this) {
1408 virtual void handle(const signed_cone
& sc
, barvinok_options
*options
);
1409 virtual void base(mat_ZZ
& factors
, bfc_vec
& v
);
1411 bfc_term_base
* new_bf_term(int len
) {
1412 bfe_term
* t
= new bfe_term(len
);
1416 virtual void set_factor(bfc_term_base
*t
, int k
, int change
) {
1417 bfe_term
* bfet
= static_cast<bfe_term
*>(t
);
1418 factor
= bfet
->factors
[k
];
1419 assert(factor
!= NULL
);
1420 bfet
->factors
[k
] = NULL
;
1422 emul(&mone
, factor
);
1425 virtual void set_factor(bfc_term_base
*t
, int k
, mpq_t
&q
, int change
) {
1426 bfe_term
* bfet
= static_cast<bfe_term
*>(t
);
1427 factor
= bfet
->factors
[k
];
1428 assert(factor
!= NULL
);
1429 bfet
->factors
[k
] = NULL
;
1435 value_oppose(f
.x
.n
, mpq_numref(q
));
1437 value_assign(f
.x
.n
, mpq_numref(q
));
1438 value_assign(f
.d
, mpq_denref(q
));
1440 free_evalue_refs(&f
);
1443 virtual void set_factor(bfc_term_base
*t
, int k
, const QQ
& c
, int change
) {
1444 bfe_term
* bfet
= static_cast<bfe_term
*>(t
);
1446 factor
= new evalue
;
1451 zz2value(c
.n
, f
.x
.n
);
1453 value_oppose(f
.x
.n
, f
.x
.n
);
1456 value_init(factor
->d
);
1457 evalue_copy(factor
, bfet
->factors
[k
]);
1459 free_evalue_refs(&f
);
1462 void set_factor(evalue
*f
, int change
) {
1468 virtual void insert_term(bfc_term_base
*t
, int i
) {
1469 bfe_term
* bfet
= static_cast<bfe_term
*>(t
);
1470 int len
= t
->terms
.NumRows()-1; // already increased by one
1472 bfet
->factors
.resize(len
+1);
1473 for (int j
= len
; j
> i
; --j
) {
1474 bfet
->factors
[j
] = bfet
->factors
[j
-1];
1475 t
->terms
[j
] = t
->terms
[j
-1];
1477 bfet
->factors
[i
] = factor
;
1481 virtual void update_term(bfc_term_base
*t
, int i
) {
1482 bfe_term
* bfet
= static_cast<bfe_term
*>(t
);
1484 eadd(factor
, bfet
->factors
[i
]);
1485 free_evalue_refs(factor
);
1489 virtual bool constant_vertex(int dim
) { return E_num(0, dim
) == 0; }
1491 virtual void cum(bf_reducer
*bfr
, bfc_term_base
*t
, int k
, dpoly_r
*r
,
1492 barvinok_options
*options
);
1495 enumerator_base
*enumerator_base::create(Polyhedron
*P
, unsigned dim
, unsigned nbV
,
1496 barvinok_options
*options
)
1498 enumerator_base
*eb
;
1500 if (options
->incremental_specialization
== BV_SPECIALIZATION_BF
)
1501 eb
= new bfenumerator(P
, dim
, nbV
);
1502 else if (options
->incremental_specialization
== BV_SPECIALIZATION_DF
)
1503 eb
= new ienumerator(P
, dim
, nbV
);
1505 eb
= new enumerator(P
, dim
, nbV
);
1510 struct bfe_cum
: public cumulator
{
1512 bfc_term_base
*told
;
1516 bfe_cum(evalue
*factor
, evalue
*v
, dpoly_r
*r
, bf_reducer
*bfr
,
1517 bfc_term_base
*t
, int k
, bfenumerator
*e
) :
1518 cumulator(factor
, v
, r
), told(t
), k(k
),
1522 virtual void add_term(const vector
<int>& powers
, evalue
*f2
);
1525 void bfe_cum::add_term(const vector
<int>& powers
, evalue
*f2
)
1527 bfr
->update_powers(powers
);
1529 bfc_term_base
* t
= bfe
->find_bfc_term(bfr
->vn
, bfr
->npowers
, bfr
->nnf
);
1530 bfe
->set_factor(f2
, bfr
->l_changes
% 2);
1531 bfe
->add_term(t
, told
->terms
[k
], bfr
->l_extra_num
);
1534 void bfenumerator::cum(bf_reducer
*bfr
, bfc_term_base
*t
, int k
,
1535 dpoly_r
*r
, barvinok_options
*options
)
1537 bfe_term
* bfet
= static_cast<bfe_term
*>(t
);
1538 bfe_cum
cum(bfet
->factors
[k
], E_num(0, bfr
->d
), r
, bfr
, t
, k
, this);
1539 cum
.cumulate(options
);
1542 void bfenumerator::base(mat_ZZ
& factors
, bfc_vec
& v
)
1544 for (int i
= 0; i
< v
.size(); ++i
) {
1545 assert(v
[i
]->terms
.NumRows() == 1);
1546 evalue
*factor
= static_cast<bfe_term
*>(v
[i
])->factors
[0];
1547 eadd(factor
, vE
[vert
]);
1552 void bfenumerator::handle(const signed_cone
& sc
, barvinok_options
*options
)
1554 assert(sc
.det
== 1);
1556 assert(sc
.rays
.NumRows() == enumerator_base::dim
);
1558 bfe_term
* t
= new bfe_term(enumerator_base::dim
);
1559 vector
< bfc_term_base
* > v
;
1562 t
->factors
.resize(1);
1564 t
->terms
.SetDims(1, enumerator_base::dim
);
1565 lattice_point(V
, sc
.rays
, t
->terms
[0], E_vertex
, options
);
1567 // the elements of factors are always lexpositive
1569 int s
= setup_factors(sc
.rays
, factors
, t
, sc
.sign
);
1571 t
->factors
[0] = new evalue
;
1572 value_init(t
->factors
[0]->d
);
1573 evalue_set_si(t
->factors
[0], s
, 1);
1574 reduce(factors
, v
, options
);
1576 for (int i
= 0; i
< enumerator_base::dim
; ++i
)
1578 free_evalue_refs(E_vertex
[i
]);
1583 #ifdef HAVE_CORRECT_VERTICES
1584 static inline Param_Polyhedron
*Polyhedron2Param_SD(Polyhedron
**Din
,
1585 Polyhedron
*Cin
,int WS
,Polyhedron
**CEq
,Matrix
**CT
)
1587 if (WS
& POL_NO_DUAL
)
1589 return Polyhedron2Param_SimplifiedDomain(Din
, Cin
, WS
, CEq
, CT
);
1592 static Param_Polyhedron
*Polyhedron2Param_SD(Polyhedron
**Din
,
1593 Polyhedron
*Cin
,int WS
,Polyhedron
**CEq
,Matrix
**CT
)
1595 static char data
[] = " 1 0 0 0 0 1 -18 "
1596 " 1 0 0 -20 0 19 1 "
1597 " 1 0 1 20 0 -20 16 "
1600 " 1 4 -20 0 0 -1 23 "
1601 " 1 -4 20 0 0 1 -22 "
1602 " 1 0 1 0 20 -20 16 "
1603 " 1 0 0 0 -20 19 1 ";
1604 static int checked
= 0;
1609 Matrix
*M
= Matrix_Alloc(9, 7);
1610 for (i
= 0; i
< 9; ++i
)
1611 for (int j
= 0; j
< 7; ++j
) {
1612 sscanf(p
, "%d%n", &v
, &n
);
1614 value_set_si(M
->p
[i
][j
], v
);
1616 Polyhedron
*P
= Constraints2Polyhedron(M
, 1024);
1618 Polyhedron
*U
= Universe_Polyhedron(1);
1619 Param_Polyhedron
*PP
= Polyhedron2Param_Domain(P
, U
, 1024);
1623 for (i
= 0, V
= PP
->V
; V
; ++i
, V
= V
->next
)
1626 Param_Polyhedron_Free(PP
);
1628 fprintf(stderr
, "WARNING: results may be incorrect\n");
1630 "WARNING: use latest version of PolyLib to remove this warning\n");
1634 return Polyhedron2Param_SimplifiedDomain(Din
, Cin
, WS
, CEq
, CT
);
1638 static evalue
* barvinok_enumerate_ev_f(Polyhedron
*P
, Polyhedron
* C
,
1639 barvinok_options
*options
);
1642 static evalue
* barvinok_enumerate_cst(Polyhedron
*P
, Polyhedron
* C
,
1643 struct barvinok_options
*options
)
1647 ALLOC(evalue
, eres
);
1648 value_init(eres
->d
);
1649 value_set_si(eres
->d
, 0);
1650 eres
->x
.p
= new_enode(partition
, 2, C
->Dimension
);
1651 EVALUE_SET_DOMAIN(eres
->x
.p
->arr
[0],
1652 DomainConstraintSimplify(C
, options
->MaxRays
));
1653 value_set_si(eres
->x
.p
->arr
[1].d
, 1);
1654 value_init(eres
->x
.p
->arr
[1].x
.n
);
1656 value_set_si(eres
->x
.p
->arr
[1].x
.n
, 0);
1658 barvinok_count_with_options(P
, &eres
->x
.p
->arr
[1].x
.n
, options
);
1663 evalue
* barvinok_enumerate_with_options(Polyhedron
*P
, Polyhedron
* C
,
1664 struct barvinok_options
*options
)
1666 //P = unfringe(P, MaxRays);
1667 Polyhedron
*next
, *Cnext
;
1668 Polyhedron
*Corig
= C
;
1669 Polyhedron
*Porig
= P
;
1670 Polyhedron
*CEq
= NULL
, *rVD
, *CA
;
1672 unsigned nparam
= C
->Dimension
;
1677 "barvinok_enumerate: input is a union; only first polyhedron is enumerated\n");
1681 "barvinok_enumerate: context is a union; only first polyhedron is considered\n");
1684 value_init(factor
.d
);
1685 evalue_set_si(&factor
, 1, 1);
1689 CA
= align_context(C
, P
->Dimension
, options
->MaxRays
);
1692 P
= DomainIntersection(P
, CA
, options
->MaxRays
);
1694 Polyhedron_Free(CA
);
1697 POL_ENSURE_FACETS(P
);
1698 POL_ENSURE_VERTICES(P
);
1699 POL_ENSURE_FACETS(C
);
1700 POL_ENSURE_VERTICES(C
);
1702 if (C
->Dimension
== 0 || emptyQ(P
)) {
1704 eres
= barvinok_enumerate_cst(P
, CEq
? CEq
: Polyhedron_Copy(C
), options
);
1706 emul(&factor
, eres
);
1707 if (options
->approximation_method
== BV_APPROX_DROP
) {
1708 if (options
->polynomial_approximation
== BV_APPROX_SIGN_UPPER
)
1709 evalue_frac2polynomial(eres
, 1, options
->MaxRays
);
1710 if (options
->polynomial_approximation
== BV_APPROX_SIGN_LOWER
)
1711 evalue_frac2polynomial(eres
, -1, options
->MaxRays
);
1713 reduce_evalue(eres
);
1714 free_evalue_refs(&factor
);
1719 Corig
->next
= Cnext
;
1722 if (Polyhedron_is_unbounded(P
, nparam
, options
->MaxRays
))
1727 P
= remove_equalities_p(P
, P
->Dimension
-nparam
, &f
);
1728 mask(f
, &factor
, options
);
1731 if (P
->Dimension
== nparam
) {
1733 P
= Universe_Polyhedron(0);
1737 Polyhedron
*T
= Polyhedron_Factor(P
, nparam
, NULL
, options
->MaxRays
);
1738 if (T
|| (P
->Dimension
== nparam
+1)) {
1741 for (Q
= T
? T
: P
; Q
; Q
= Q
->next
) {
1742 Polyhedron
*next
= Q
->next
;
1746 if (Q
->Dimension
!= C
->Dimension
)
1747 QC
= Polyhedron_Project(Q
, nparam
);
1750 C
= DomainIntersection(C
, QC
, options
->MaxRays
);
1752 Polyhedron_Free(C2
);
1754 Polyhedron_Free(QC
);
1762 if (T
->Dimension
== C
->Dimension
) {
1771 eres
= barvinok_enumerate_ev_f(P
, C
, options
);
1778 for (Q
= P
->next
; Q
; Q
= Q
->next
) {
1779 Polyhedron
*next
= Q
->next
;
1782 f
= barvinok_enumerate_ev_f(Q
, C
, options
);
1784 free_evalue_refs(f
);
1794 evalue
* barvinok_enumerate_ev(Polyhedron
*P
, Polyhedron
* C
, unsigned MaxRays
)
1797 barvinok_options
*options
= barvinok_options_new_with_defaults();
1798 options
->MaxRays
= MaxRays
;
1799 E
= barvinok_enumerate_with_options(P
, C
, options
);
1800 barvinok_options_free(options
);
1804 static evalue
* barvinok_enumerate_ev_f(Polyhedron
*P
, Polyhedron
* C
,
1805 barvinok_options
*options
)
1807 unsigned nparam
= C
->Dimension
;
1808 bool scale_fast
= options
->approximation_method
== BV_APPROX_SCALE_FAST
;
1809 bool do_scale
= options
->approximation_method
== BV_APPROX_SCALE
;
1811 if (P
->Dimension
- nparam
== 1 && !scale_fast
&& !do_scale
)
1812 return ParamLine_Length(P
, C
, options
);
1814 Param_Polyhedron
*PP
= NULL
;
1815 Polyhedron
*CEq
= NULL
, *pVD
;
1817 Param_Domain
*D
, *next
;
1820 Polyhedron
*Porig
= P
;
1824 if (do_scale
|| scale_fast
) {
1825 P
= scale_init(P
, C
, &scaling
, options
);
1827 eres
= barvinok_enumerate_with_options(P
, C
, options
);
1829 scale_finish(eres
, &scaling
, options
);
1835 PP
= Polyhedron2Param_SD(&T
, C
, options
->MaxRays
, &CEq
, &CT
);
1836 if (T
!= P
&& P
!= Porig
)
1840 if (isIdentity(CT
)) {
1844 assert(CT
->NbRows
!= CT
->NbColumns
);
1845 if (CT
->NbRows
== 1) { // no more parameters
1846 eres
= barvinok_enumerate_cst(P
, CEq
, options
);
1851 Param_Polyhedron_Free(PP
);
1857 nparam
= CT
->NbRows
- 1;
1860 if (do_scale
|| scale_fast
)
1861 P
= scale(PP
, P
, &scaling
, P
!= Porig
, options
);
1863 unsigned dim
= P
->Dimension
- nparam
;
1865 ALLOC(evalue
, eres
);
1866 value_init(eres
->d
);
1867 value_set_si(eres
->d
, 0);
1870 for (nd
= 0, D
=PP
->D
; D
; ++nd
, D
=D
->next
);
1871 struct section
{ Polyhedron
*D
; evalue E
; };
1872 section
*s
= new section
[nd
];
1873 Polyhedron
**fVD
= new Polyhedron_p
[nd
];
1875 enumerator_base
*et
= NULL
;
1880 et
= enumerator_base::create(P
, dim
, PP
->nbV
, options
);
1882 for(nd
= 0, D
=PP
->D
; D
; D
=next
) {
1885 Polyhedron
*rVD
= reduce_domain(D
->Domain
, CT
, CEq
, fVD
, nd
, options
);
1889 pVD
= CT
? DomainImage(rVD
,CT
,options
->MaxRays
) : rVD
;
1891 value_init(s
[nd
].E
.d
);
1892 evalue_set_si(&s
[nd
].E
, 0, 1);
1895 FORALL_PVertex_in_ParamPolyhedron(V
,D
,PP
) // _i is internal counter
1898 et
->decompose_at(V
, _i
, options
);
1899 } catch (OrthogonalException
&e
) {
1902 for (; nd
>= 0; --nd
) {
1903 free_evalue_refs(&s
[nd
].E
);
1904 Domain_Free(s
[nd
].D
);
1905 Domain_Free(fVD
[nd
]);
1909 eadd(et
->vE
[_i
] , &s
[nd
].E
);
1910 END_FORALL_PVertex_in_ParamPolyhedron
;
1911 evalue_range_reduction_in_domain(&s
[nd
].E
, pVD
);
1914 addeliminatedparams_evalue(&s
[nd
].E
, CT
);
1922 evalue_set_si(eres
, 0, 1);
1924 eres
->x
.p
= new_enode(partition
, 2*nd
, C
->Dimension
);
1925 for (int j
= 0; j
< nd
; ++j
) {
1926 EVALUE_SET_DOMAIN(eres
->x
.p
->arr
[2*j
], s
[j
].D
);
1927 value_clear(eres
->x
.p
->arr
[2*j
+1].d
);
1928 eres
->x
.p
->arr
[2*j
+1] = s
[j
].E
;
1929 Domain_Free(fVD
[j
]);
1935 if (do_scale
|| scale_fast
)
1936 scale_finish(eres
, &scaling
, options
);
1939 Polyhedron_Free(CEq
);
1943 Enumeration
* barvinok_enumerate(Polyhedron
*P
, Polyhedron
* C
, unsigned MaxRays
)
1945 evalue
*EP
= barvinok_enumerate_ev(P
, C
, MaxRays
);
1947 return partition2enumeration(EP
);
1950 static void SwapColumns(Value
**V
, int n
, int i
, int j
)
1952 for (int r
= 0; r
< n
; ++r
)
1953 value_swap(V
[r
][i
], V
[r
][j
]);
1956 static void SwapColumns(Polyhedron
*P
, int i
, int j
)
1958 SwapColumns(P
->Constraint
, P
->NbConstraints
, i
, j
);
1959 SwapColumns(P
->Ray
, P
->NbRays
, i
, j
);
1962 /* Construct a constraint c from constraints l and u such that if
1963 * if constraint c holds then for each value of the other variables
1964 * there is at most one value of variable pos (position pos+1 in the constraints).
1966 * Given a lower and an upper bound
1967 * n_l v_i + <c_l,x> + c_l >= 0
1968 * -n_u v_i + <c_u,x> + c_u >= 0
1969 * the constructed constraint is
1971 * -(n_l<c_u,x> + n_u<c_l,x>) + (-n_l c_u - n_u c_l + n_l n_u - 1)
1973 * which is then simplified to remove the content of the non-constant coefficients
1975 * len is the total length of the constraints.
1976 * v is a temporary variable that can be used by this procedure
1978 static void negative_test_constraint(Value
*l
, Value
*u
, Value
*c
, int pos
,
1981 value_oppose(*v
, u
[pos
+1]);
1982 Vector_Combine(l
+1, u
+1, c
+1, *v
, l
[pos
+1], len
-1);
1983 value_multiply(*v
, *v
, l
[pos
+1]);
1984 value_subtract(c
[len
-1], c
[len
-1], *v
);
1985 value_set_si(*v
, -1);
1986 Vector_Scale(c
+1, c
+1, *v
, len
-1);
1987 value_decrement(c
[len
-1], c
[len
-1]);
1988 ConstraintSimplify(c
, c
, len
, v
);
1991 static bool parallel_constraints(Value
*l
, Value
*u
, Value
*c
, int pos
,
2000 Vector_Gcd(&l
[1+pos
], len
, &g1
);
2001 Vector_Gcd(&u
[1+pos
], len
, &g2
);
2002 Vector_Combine(l
+1+pos
, u
+1+pos
, c
+1, g2
, g1
, len
);
2003 parallel
= First_Non_Zero(c
+1, len
) == -1;
2011 static void negative_test_constraint7(Value
*l
, Value
*u
, Value
*c
, int pos
,
2012 int exist
, int len
, Value
*v
)
2017 Vector_Gcd(&u
[1+pos
], exist
, v
);
2018 Vector_Gcd(&l
[1+pos
], exist
, &g
);
2019 Vector_Combine(l
+1, u
+1, c
+1, *v
, g
, len
-1);
2020 value_multiply(*v
, *v
, g
);
2021 value_subtract(c
[len
-1], c
[len
-1], *v
);
2022 value_set_si(*v
, -1);
2023 Vector_Scale(c
+1, c
+1, *v
, len
-1);
2024 value_decrement(c
[len
-1], c
[len
-1]);
2025 ConstraintSimplify(c
, c
, len
, v
);
2030 /* Turns a x + b >= 0 into a x + b <= -1
2032 * len is the total length of the constraint.
2033 * v is a temporary variable that can be used by this procedure
2035 static void oppose_constraint(Value
*c
, int len
, Value
*v
)
2037 value_set_si(*v
, -1);
2038 Vector_Scale(c
+1, c
+1, *v
, len
-1);
2039 value_decrement(c
[len
-1], c
[len
-1]);
2042 /* Split polyhedron P into two polyhedra *pos and *neg, where
2043 * existential variable i has at most one solution for each
2044 * value of the other variables in *neg.
2046 * The splitting is performed using constraints l and u.
2048 * nvar: number of set variables
2049 * row: temporary vector that can be used by this procedure
2050 * f: temporary value that can be used by this procedure
2052 static bool SplitOnConstraint(Polyhedron
*P
, int i
, int l
, int u
,
2053 int nvar
, int MaxRays
, Vector
*row
, Value
& f
,
2054 Polyhedron
**pos
, Polyhedron
**neg
)
2056 negative_test_constraint(P
->Constraint
[l
], P
->Constraint
[u
],
2057 row
->p
, nvar
+i
, P
->Dimension
+2, &f
);
2058 *neg
= AddConstraints(row
->p
, 1, P
, MaxRays
);
2060 /* We found an independent, but useless constraint
2061 * Maybe we should detect this earlier and not
2062 * mark the variable as INDEPENDENT
2064 if (emptyQ((*neg
))) {
2065 Polyhedron_Free(*neg
);
2069 oppose_constraint(row
->p
, P
->Dimension
+2, &f
);
2070 *pos
= AddConstraints(row
->p
, 1, P
, MaxRays
);
2072 if (emptyQ((*pos
))) {
2073 Polyhedron_Free(*neg
);
2074 Polyhedron_Free(*pos
);
2082 * unimodularly transform P such that constraint r is transformed
2083 * into a constraint that involves only a single (the first)
2084 * existential variable
2087 static Polyhedron
*rotate_along(Polyhedron
*P
, int r
, int nvar
, int exist
,
2093 Vector
*row
= Vector_Alloc(exist
);
2094 Vector_Copy(P
->Constraint
[r
]+1+nvar
, row
->p
, exist
);
2095 Vector_Gcd(row
->p
, exist
, &g
);
2096 if (value_notone_p(g
))
2097 Vector_AntiScale(row
->p
, row
->p
, g
, exist
);
2100 Matrix
*M
= unimodular_complete(row
);
2101 Matrix
*M2
= Matrix_Alloc(P
->Dimension
+1, P
->Dimension
+1);
2102 for (r
= 0; r
< nvar
; ++r
)
2103 value_set_si(M2
->p
[r
][r
], 1);
2104 for ( ; r
< nvar
+exist
; ++r
)
2105 Vector_Copy(M
->p
[r
-nvar
], M2
->p
[r
]+nvar
, exist
);
2106 for ( ; r
< P
->Dimension
+1; ++r
)
2107 value_set_si(M2
->p
[r
][r
], 1);
2108 Polyhedron
*T
= Polyhedron_Image(P
, M2
, MaxRays
);
2117 /* Split polyhedron P into two polyhedra *pos and *neg, where
2118 * existential variable i has at most one solution for each
2119 * value of the other variables in *neg.
2121 * If independent is set, then the two constraints on which the
2122 * split will be performed need to be independent of the other
2123 * existential variables.
2125 * Return true if an appropriate split could be performed.
2127 * nvar: number of set variables
2128 * exist: number of existential variables
2129 * row: temporary vector that can be used by this procedure
2130 * f: temporary value that can be used by this procedure
2132 static bool SplitOnVar(Polyhedron
*P
, int i
,
2133 int nvar
, int exist
, int MaxRays
,
2134 Vector
*row
, Value
& f
, bool independent
,
2135 Polyhedron
**pos
, Polyhedron
**neg
)
2139 for (int l
= P
->NbEq
; l
< P
->NbConstraints
; ++l
) {
2140 if (value_negz_p(P
->Constraint
[l
][nvar
+i
+1]))
2144 for (j
= 0; j
< exist
; ++j
)
2145 if (j
!= i
&& value_notzero_p(P
->Constraint
[l
][nvar
+j
+1]))
2151 for (int u
= P
->NbEq
; u
< P
->NbConstraints
; ++u
) {
2152 if (value_posz_p(P
->Constraint
[u
][nvar
+i
+1]))
2156 for (j
= 0; j
< exist
; ++j
)
2157 if (j
!= i
&& value_notzero_p(P
->Constraint
[u
][nvar
+j
+1]))
2163 if (SplitOnConstraint(P
, i
, l
, u
, nvar
, MaxRays
, row
, f
, pos
, neg
)) {
2166 SwapColumns(*neg
, nvar
+1, nvar
+1+i
);
2176 static bool double_bound_pair(Polyhedron
*P
, int nvar
, int exist
,
2177 int i
, int l1
, int l2
,
2178 Polyhedron
**pos
, Polyhedron
**neg
)
2182 Vector
*row
= Vector_Alloc(P
->Dimension
+2);
2183 value_set_si(row
->p
[0], 1);
2184 value_oppose(f
, P
->Constraint
[l1
][nvar
+i
+1]);
2185 Vector_Combine(P
->Constraint
[l1
]+1, P
->Constraint
[l2
]+1,
2187 P
->Constraint
[l2
][nvar
+i
+1], f
,
2189 ConstraintSimplify(row
->p
, row
->p
, P
->Dimension
+2, &f
);
2190 *pos
= AddConstraints(row
->p
, 1, P
, 0);
2191 value_set_si(f
, -1);
2192 Vector_Scale(row
->p
+1, row
->p
+1, f
, P
->Dimension
+1);
2193 value_decrement(row
->p
[P
->Dimension
+1], row
->p
[P
->Dimension
+1]);
2194 *neg
= AddConstraints(row
->p
, 1, P
, 0);
2198 return !emptyQ((*pos
)) && !emptyQ((*neg
));
2201 static bool double_bound(Polyhedron
*P
, int nvar
, int exist
,
2202 Polyhedron
**pos
, Polyhedron
**neg
)
2204 for (int i
= 0; i
< exist
; ++i
) {
2206 for (l1
= P
->NbEq
; l1
< P
->NbConstraints
; ++l1
) {
2207 if (value_negz_p(P
->Constraint
[l1
][nvar
+i
+1]))
2209 for (l2
= l1
+ 1; l2
< P
->NbConstraints
; ++l2
) {
2210 if (value_negz_p(P
->Constraint
[l2
][nvar
+i
+1]))
2212 if (double_bound_pair(P
, nvar
, exist
, i
, l1
, l2
, pos
, neg
))
2216 for (l1
= P
->NbEq
; l1
< P
->NbConstraints
; ++l1
) {
2217 if (value_posz_p(P
->Constraint
[l1
][nvar
+i
+1]))
2219 if (l1
< P
->NbConstraints
)
2220 for (l2
= l1
+ 1; l2
< P
->NbConstraints
; ++l2
) {
2221 if (value_posz_p(P
->Constraint
[l2
][nvar
+i
+1]))
2223 if (double_bound_pair(P
, nvar
, exist
, i
, l1
, l2
, pos
, neg
))
2235 INDEPENDENT
= 1 << 2,
2239 static evalue
* enumerate_or(Polyhedron
*D
,
2240 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2243 fprintf(stderr
, "\nER: Or\n");
2244 #endif /* DEBUG_ER */
2246 Polyhedron
*N
= D
->next
;
2249 barvinok_enumerate_e_with_options(D
, exist
, nparam
, options
);
2252 for (D
= N
; D
; D
= N
) {
2257 barvinok_enumerate_e_with_options(D
, exist
, nparam
, options
);
2260 free_evalue_refs(EN
);
2270 static evalue
* enumerate_sum(Polyhedron
*P
,
2271 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2273 int nvar
= P
->Dimension
- exist
- nparam
;
2274 int toswap
= nvar
< exist
? nvar
: exist
;
2275 for (int i
= 0; i
< toswap
; ++i
)
2276 SwapColumns(P
, 1 + i
, nvar
+exist
- i
);
2280 fprintf(stderr
, "\nER: Sum\n");
2281 #endif /* DEBUG_ER */
2283 evalue
*EP
= barvinok_enumerate_e_with_options(P
, exist
, nparam
, options
);
2285 evalue_split_domains_into_orthants(EP
, options
->MaxRays
);
2287 evalue_range_reduction(EP
);
2289 evalue_frac2floor2(EP
, 1);
2291 evalue
*sum
= esum(EP
, nvar
);
2293 free_evalue_refs(EP
);
2297 evalue_range_reduction(EP
);
2302 static evalue
* split_sure(Polyhedron
*P
, Polyhedron
*S
,
2303 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2305 int nvar
= P
->Dimension
- exist
- nparam
;
2307 Matrix
*M
= Matrix_Alloc(exist
, S
->Dimension
+2);
2308 for (int i
= 0; i
< exist
; ++i
)
2309 value_set_si(M
->p
[i
][nvar
+i
+1], 1);
2311 S
= DomainAddRays(S
, M
, options
->MaxRays
);
2313 Polyhedron
*F
= DomainAddRays(P
, M
, options
->MaxRays
);
2314 Polyhedron
*D
= DomainDifference(F
, S
, options
->MaxRays
);
2316 D
= Disjoint_Domain(D
, 0, options
->MaxRays
);
2321 M
= Matrix_Alloc(P
->Dimension
+1-exist
, P
->Dimension
+1);
2322 for (int j
= 0; j
< nvar
; ++j
)
2323 value_set_si(M
->p
[j
][j
], 1);
2324 for (int j
= 0; j
< nparam
+1; ++j
)
2325 value_set_si(M
->p
[nvar
+j
][nvar
+exist
+j
], 1);
2326 Polyhedron
*T
= Polyhedron_Image(S
, M
, options
->MaxRays
);
2327 evalue
*EP
= barvinok_enumerate_e_with_options(T
, 0, nparam
, options
);
2332 for (Polyhedron
*Q
= D
; Q
; Q
= Q
->next
) {
2333 Polyhedron
*N
= Q
->next
;
2335 T
= DomainIntersection(P
, Q
, options
->MaxRays
);
2336 evalue
*E
= barvinok_enumerate_e_with_options(T
, exist
, nparam
, options
);
2338 free_evalue_refs(E
);
2347 static evalue
* enumerate_sure(Polyhedron
*P
,
2348 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2352 int nvar
= P
->Dimension
- exist
- nparam
;
2358 for (i
= 0; i
< exist
; ++i
) {
2359 Matrix
*M
= Matrix_Alloc(S
->NbConstraints
, S
->Dimension
+2);
2361 value_set_si(lcm
, 1);
2362 for (int j
= 0; j
< S
->NbConstraints
; ++j
) {
2363 if (value_negz_p(S
->Constraint
[j
][1+nvar
+i
]))
2365 if (value_one_p(S
->Constraint
[j
][1+nvar
+i
]))
2367 value_lcm(lcm
, S
->Constraint
[j
][1+nvar
+i
], &lcm
);
2370 for (int j
= 0; j
< S
->NbConstraints
; ++j
) {
2371 if (value_negz_p(S
->Constraint
[j
][1+nvar
+i
]))
2373 if (value_one_p(S
->Constraint
[j
][1+nvar
+i
]))
2375 value_division(f
, lcm
, S
->Constraint
[j
][1+nvar
+i
]);
2376 Vector_Scale(S
->Constraint
[j
], M
->p
[c
], f
, S
->Dimension
+2);
2377 value_subtract(M
->p
[c
][S
->Dimension
+1],
2378 M
->p
[c
][S
->Dimension
+1],
2380 value_increment(M
->p
[c
][S
->Dimension
+1],
2381 M
->p
[c
][S
->Dimension
+1]);
2385 S
= AddConstraints(M
->p
[0], c
, S
, options
->MaxRays
);
2400 fprintf(stderr
, "\nER: Sure\n");
2401 #endif /* DEBUG_ER */
2403 return split_sure(P
, S
, exist
, nparam
, options
);
2406 static evalue
* enumerate_sure2(Polyhedron
*P
,
2407 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2409 int nvar
= P
->Dimension
- exist
- nparam
;
2411 for (r
= 0; r
< P
->NbRays
; ++r
)
2412 if (value_one_p(P
->Ray
[r
][0]) &&
2413 value_one_p(P
->Ray
[r
][P
->Dimension
+1]))
2419 Matrix
*M
= Matrix_Alloc(nvar
+ 1 + nparam
, P
->Dimension
+2);
2420 for (int i
= 0; i
< nvar
; ++i
)
2421 value_set_si(M
->p
[i
][1+i
], 1);
2422 for (int i
= 0; i
< nparam
; ++i
)
2423 value_set_si(M
->p
[i
+nvar
][1+nvar
+exist
+i
], 1);
2424 Vector_Copy(P
->Ray
[r
]+1+nvar
, M
->p
[nvar
+nparam
]+1+nvar
, exist
);
2425 value_set_si(M
->p
[nvar
+nparam
][0], 1);
2426 value_set_si(M
->p
[nvar
+nparam
][P
->Dimension
+1], 1);
2427 Polyhedron
* F
= Rays2Polyhedron(M
, options
->MaxRays
);
2430 Polyhedron
*I
= DomainIntersection(F
, P
, options
->MaxRays
);
2434 fprintf(stderr
, "\nER: Sure2\n");
2435 #endif /* DEBUG_ER */
2437 return split_sure(P
, I
, exist
, nparam
, options
);
2440 static evalue
* enumerate_cyclic(Polyhedron
*P
,
2441 unsigned exist
, unsigned nparam
,
2442 evalue
* EP
, int r
, int p
, unsigned MaxRays
)
2444 int nvar
= P
->Dimension
- exist
- nparam
;
2446 /* If EP in its fractional maps only contains references
2447 * to the remainder parameter with appropriate coefficients
2448 * then we could in principle avoid adding existentially
2449 * quantified variables to the validity domains.
2450 * We'd have to replace the remainder by m { p/m }
2451 * and multiply with an appropriate factor that is one
2452 * only in the appropriate range.
2453 * This last multiplication can be avoided if EP
2454 * has a single validity domain with no (further)
2455 * constraints on the remainder parameter
2458 Matrix
*CT
= Matrix_Alloc(nparam
+1, nparam
+3);
2459 Matrix
*M
= Matrix_Alloc(1, 1+nparam
+3);
2460 for (int j
= 0; j
< nparam
; ++j
)
2462 value_set_si(CT
->p
[j
][j
], 1);
2463 value_set_si(CT
->p
[p
][nparam
+1], 1);
2464 value_set_si(CT
->p
[nparam
][nparam
+2], 1);
2465 value_set_si(M
->p
[0][1+p
], -1);
2466 value_absolute(M
->p
[0][1+nparam
], P
->Ray
[0][1+nvar
+exist
+p
]);
2467 value_set_si(M
->p
[0][1+nparam
+1], 1);
2468 Polyhedron
*CEq
= Constraints2Polyhedron(M
, 1);
2470 addeliminatedparams_enum(EP
, CT
, CEq
, MaxRays
, nparam
);
2471 Polyhedron_Free(CEq
);
2477 static void enumerate_vd_add_ray(evalue
*EP
, Matrix
*Rays
, unsigned MaxRays
)
2479 if (value_notzero_p(EP
->d
))
2482 assert(EP
->x
.p
->type
== partition
);
2483 assert(EP
->x
.p
->pos
== EVALUE_DOMAIN(EP
->x
.p
->arr
[0])->Dimension
);
2484 for (int i
= 0; i
< EP
->x
.p
->size
/2; ++i
) {
2485 Polyhedron
*D
= EVALUE_DOMAIN(EP
->x
.p
->arr
[2*i
]);
2486 Polyhedron
*N
= DomainAddRays(D
, Rays
, MaxRays
);
2487 EVALUE_SET_DOMAIN(EP
->x
.p
->arr
[2*i
], N
);
2492 static evalue
* enumerate_line(Polyhedron
*P
,
2493 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2499 fprintf(stderr
, "\nER: Line\n");
2500 #endif /* DEBUG_ER */
2502 int nvar
= P
->Dimension
- exist
- nparam
;
2504 for (i
= 0; i
< nparam
; ++i
)
2505 if (value_notzero_p(P
->Ray
[0][1+nvar
+exist
+i
]))
2508 for (j
= i
+1; j
< nparam
; ++j
)
2509 if (value_notzero_p(P
->Ray
[0][1+nvar
+exist
+i
]))
2511 assert(j
>= nparam
); // for now
2513 Matrix
*M
= Matrix_Alloc(2, P
->Dimension
+2);
2514 value_set_si(M
->p
[0][0], 1);
2515 value_set_si(M
->p
[0][1+nvar
+exist
+i
], 1);
2516 value_set_si(M
->p
[1][0], 1);
2517 value_set_si(M
->p
[1][1+nvar
+exist
+i
], -1);
2518 value_absolute(M
->p
[1][1+P
->Dimension
], P
->Ray
[0][1+nvar
+exist
+i
]);
2519 value_decrement(M
->p
[1][1+P
->Dimension
], M
->p
[1][1+P
->Dimension
]);
2520 Polyhedron
*S
= AddConstraints(M
->p
[0], 2, P
, options
->MaxRays
);
2521 evalue
*EP
= barvinok_enumerate_e_with_options(S
, exist
, nparam
, options
);
2525 return enumerate_cyclic(P
, exist
, nparam
, EP
, 0, i
, options
->MaxRays
);
2528 static int single_param_pos(Polyhedron
*P
, unsigned exist
, unsigned nparam
,
2531 int nvar
= P
->Dimension
- exist
- nparam
;
2532 if (First_Non_Zero(P
->Ray
[r
]+1, nvar
) != -1)
2534 int i
= First_Non_Zero(P
->Ray
[r
]+1+nvar
+exist
, nparam
);
2537 if (First_Non_Zero(P
->Ray
[r
]+1+nvar
+exist
+1, nparam
-i
-1) != -1)
2542 static evalue
* enumerate_remove_ray(Polyhedron
*P
, int r
,
2543 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2546 fprintf(stderr
, "\nER: RedundantRay\n");
2547 #endif /* DEBUG_ER */
2551 value_set_si(one
, 1);
2552 int len
= P
->NbRays
-1;
2553 Matrix
*M
= Matrix_Alloc(2 * len
, P
->Dimension
+2);
2554 Vector_Copy(P
->Ray
[0], M
->p
[0], r
* (P
->Dimension
+2));
2555 Vector_Copy(P
->Ray
[r
+1], M
->p
[r
], (len
-r
) * (P
->Dimension
+2));
2556 for (int j
= 0; j
< P
->NbRays
; ++j
) {
2559 Vector_Combine(P
->Ray
[j
], P
->Ray
[r
], M
->p
[len
+j
-(j
>r
)],
2560 one
, P
->Ray
[j
][P
->Dimension
+1], P
->Dimension
+2);
2563 P
= Rays2Polyhedron(M
, options
->MaxRays
);
2565 evalue
*EP
= barvinok_enumerate_e_with_options(P
, exist
, nparam
, options
);
2572 static evalue
* enumerate_redundant_ray(Polyhedron
*P
,
2573 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2575 assert(P
->NbBid
== 0);
2576 int nvar
= P
->Dimension
- exist
- nparam
;
2580 for (int r
= 0; r
< P
->NbRays
; ++r
) {
2581 if (value_notzero_p(P
->Ray
[r
][P
->Dimension
+1]))
2583 int i1
= single_param_pos(P
, exist
, nparam
, r
);
2586 for (int r2
= r
+1; r2
< P
->NbRays
; ++r2
) {
2587 if (value_notzero_p(P
->Ray
[r2
][P
->Dimension
+1]))
2589 int i2
= single_param_pos(P
, exist
, nparam
, r2
);
2595 value_division(m
, P
->Ray
[r
][1+nvar
+exist
+i1
],
2596 P
->Ray
[r2
][1+nvar
+exist
+i1
]);
2597 value_multiply(m
, m
, P
->Ray
[r2
][1+nvar
+exist
+i1
]);
2598 /* r2 divides r => r redundant */
2599 if (value_eq(m
, P
->Ray
[r
][1+nvar
+exist
+i1
])) {
2601 return enumerate_remove_ray(P
, r
, exist
, nparam
, options
);
2604 value_division(m
, P
->Ray
[r2
][1+nvar
+exist
+i1
],
2605 P
->Ray
[r
][1+nvar
+exist
+i1
]);
2606 value_multiply(m
, m
, P
->Ray
[r
][1+nvar
+exist
+i1
]);
2607 /* r divides r2 => r2 redundant */
2608 if (value_eq(m
, P
->Ray
[r2
][1+nvar
+exist
+i1
])) {
2610 return enumerate_remove_ray(P
, r2
, exist
, nparam
, options
);
2618 static Polyhedron
*upper_bound(Polyhedron
*P
,
2619 int pos
, Value
*max
, Polyhedron
**R
)
2628 for (Polyhedron
*Q
= P
; Q
; Q
= N
) {
2630 for (r
= 0; r
< P
->NbRays
; ++r
) {
2631 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1]) &&
2632 value_pos_p(P
->Ray
[r
][1+pos
]))
2635 if (r
< P
->NbRays
) {
2643 for (r
= 0; r
< P
->NbRays
; ++r
) {
2644 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1]))
2646 mpz_fdiv_q(v
, P
->Ray
[r
][1+pos
], P
->Ray
[r
][1+P
->Dimension
]);
2647 if ((!Q
->next
&& r
== 0) || value_gt(v
, *max
))
2648 value_assign(*max
, v
);
2655 static evalue
* enumerate_ray(Polyhedron
*P
,
2656 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2658 assert(P
->NbBid
== 0);
2659 int nvar
= P
->Dimension
- exist
- nparam
;
2662 for (r
= 0; r
< P
->NbRays
; ++r
)
2663 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1]))
2669 for (r2
= r
+1; r2
< P
->NbRays
; ++r2
)
2670 if (value_zero_p(P
->Ray
[r2
][P
->Dimension
+1]))
2672 if (r2
< P
->NbRays
) {
2674 return enumerate_sum(P
, exist
, nparam
, options
);
2678 fprintf(stderr
, "\nER: Ray\n");
2679 #endif /* DEBUG_ER */
2685 value_set_si(one
, 1);
2686 int i
= single_param_pos(P
, exist
, nparam
, r
);
2687 assert(i
!= -1); // for now;
2689 Matrix
*M
= Matrix_Alloc(P
->NbRays
, P
->Dimension
+2);
2690 for (int j
= 0; j
< P
->NbRays
; ++j
) {
2691 Vector_Combine(P
->Ray
[j
], P
->Ray
[r
], M
->p
[j
],
2692 one
, P
->Ray
[j
][P
->Dimension
+1], P
->Dimension
+2);
2694 Polyhedron
*S
= Rays2Polyhedron(M
, options
->MaxRays
);
2696 Polyhedron
*D
= DomainDifference(P
, S
, options
->MaxRays
);
2698 // Polyhedron_Print(stderr, P_VALUE_FMT, D);
2699 assert(value_pos_p(P
->Ray
[r
][1+nvar
+exist
+i
])); // for now
2701 D
= upper_bound(D
, nvar
+exist
+i
, &m
, &R
);
2705 M
= Matrix_Alloc(2, P
->Dimension
+2);
2706 value_set_si(M
->p
[0][0], 1);
2707 value_set_si(M
->p
[1][0], 1);
2708 value_set_si(M
->p
[0][1+nvar
+exist
+i
], -1);
2709 value_set_si(M
->p
[1][1+nvar
+exist
+i
], 1);
2710 value_assign(M
->p
[0][1+P
->Dimension
], m
);
2711 value_oppose(M
->p
[1][1+P
->Dimension
], m
);
2712 value_addto(M
->p
[1][1+P
->Dimension
], M
->p
[1][1+P
->Dimension
],
2713 P
->Ray
[r
][1+nvar
+exist
+i
]);
2714 value_decrement(M
->p
[1][1+P
->Dimension
], M
->p
[1][1+P
->Dimension
]);
2715 // Matrix_Print(stderr, P_VALUE_FMT, M);
2716 D
= AddConstraints(M
->p
[0], 2, P
, options
->MaxRays
);
2717 // Polyhedron_Print(stderr, P_VALUE_FMT, D);
2718 value_subtract(M
->p
[0][1+P
->Dimension
], M
->p
[0][1+P
->Dimension
],
2719 P
->Ray
[r
][1+nvar
+exist
+i
]);
2720 // Matrix_Print(stderr, P_VALUE_FMT, M);
2721 S
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
2722 // Polyhedron_Print(stderr, P_VALUE_FMT, S);
2725 evalue
*EP
= barvinok_enumerate_e_with_options(D
, exist
, nparam
, options
);
2730 if (value_notone_p(P
->Ray
[r
][1+nvar
+exist
+i
]))
2731 EP
= enumerate_cyclic(P
, exist
, nparam
, EP
, r
, i
, options
->MaxRays
);
2733 M
= Matrix_Alloc(1, nparam
+2);
2734 value_set_si(M
->p
[0][0], 1);
2735 value_set_si(M
->p
[0][1+i
], 1);
2736 enumerate_vd_add_ray(EP
, M
, options
->MaxRays
);
2741 evalue
*E
= barvinok_enumerate_e_with_options(S
, exist
, nparam
, options
);
2743 free_evalue_refs(E
);
2750 evalue
*ER
= enumerate_or(R
, exist
, nparam
, options
);
2752 free_evalue_refs(ER
);
2759 static evalue
* enumerate_vd(Polyhedron
**PA
,
2760 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2762 Polyhedron
*P
= *PA
;
2763 int nvar
= P
->Dimension
- exist
- nparam
;
2764 Param_Polyhedron
*PP
= NULL
;
2765 Polyhedron
*C
= Universe_Polyhedron(nparam
);
2769 PP
= Polyhedron2Param_SimplifiedDomain(&PR
,C
, options
->MaxRays
,&CEq
,&CT
);
2773 Param_Domain
*D
, *last
;
2776 for (nd
= 0, D
=PP
->D
; D
; D
=D
->next
, ++nd
)
2779 Polyhedron
**VD
= new Polyhedron_p
[nd
];
2780 Polyhedron
**fVD
= new Polyhedron_p
[nd
];
2781 for(nd
= 0, D
=PP
->D
; D
; D
=D
->next
) {
2782 Polyhedron
*rVD
= reduce_domain(D
->Domain
, CT
, CEq
, fVD
, nd
, options
);
2795 /* This doesn't seem to have any effect */
2797 Polyhedron
*CA
= align_context(VD
[0], P
->Dimension
, options
->MaxRays
);
2799 P
= DomainIntersection(P
, CA
, options
->MaxRays
);
2802 Polyhedron_Free(CA
);
2807 if (!EP
&& CT
->NbColumns
!= CT
->NbRows
) {
2808 Polyhedron
*CEqr
= DomainImage(CEq
, CT
, options
->MaxRays
);
2809 Polyhedron
*CA
= align_context(CEqr
, PR
->Dimension
, options
->MaxRays
);
2810 Polyhedron
*I
= DomainIntersection(PR
, CA
, options
->MaxRays
);
2811 Polyhedron_Free(CEqr
);
2812 Polyhedron_Free(CA
);
2814 fprintf(stderr
, "\nER: Eliminate\n");
2815 #endif /* DEBUG_ER */
2816 nparam
-= CT
->NbColumns
- CT
->NbRows
;
2817 EP
= barvinok_enumerate_e_with_options(I
, exist
, nparam
, options
);
2818 nparam
+= CT
->NbColumns
- CT
->NbRows
;
2819 addeliminatedparams_enum(EP
, CT
, CEq
, options
->MaxRays
, nparam
);
2823 Polyhedron_Free(PR
);
2826 if (!EP
&& nd
> 1) {
2828 fprintf(stderr
, "\nER: VD\n");
2829 #endif /* DEBUG_ER */
2830 for (int i
= 0; i
< nd
; ++i
) {
2831 Polyhedron
*CA
= align_context(VD
[i
], P
->Dimension
, options
->MaxRays
);
2832 Polyhedron
*I
= DomainIntersection(P
, CA
, options
->MaxRays
);
2835 EP
= barvinok_enumerate_e_with_options(I
, exist
, nparam
, options
);
2837 evalue
*E
= barvinok_enumerate_e_with_options(I
, exist
, nparam
,
2840 free_evalue_refs(E
);
2844 Polyhedron_Free(CA
);
2848 for (int i
= 0; i
< nd
; ++i
) {
2849 Polyhedron_Free(VD
[i
]);
2850 Polyhedron_Free(fVD
[i
]);
2856 if (!EP
&& nvar
== 0) {
2859 Param_Vertices
*V
, *V2
;
2860 Matrix
* M
= Matrix_Alloc(1, P
->Dimension
+2);
2862 FORALL_PVertex_in_ParamPolyhedron(V
, last
, PP
) {
2864 FORALL_PVertex_in_ParamPolyhedron(V2
, last
, PP
) {
2871 for (int i
= 0; i
< exist
; ++i
) {
2872 value_oppose(f
, V
->Vertex
->p
[i
][nparam
+1]);
2873 Vector_Combine(V
->Vertex
->p
[i
],
2875 M
->p
[0] + 1 + nvar
+ exist
,
2876 V2
->Vertex
->p
[i
][nparam
+1],
2880 for (j
= 0; j
< nparam
; ++j
)
2881 if (value_notzero_p(M
->p
[0][1+nvar
+exist
+j
]))
2885 ConstraintSimplify(M
->p
[0], M
->p
[0],
2886 P
->Dimension
+2, &f
);
2887 value_set_si(M
->p
[0][0], 0);
2888 Polyhedron
*para
= AddConstraints(M
->p
[0], 1, P
,
2891 Polyhedron_Free(para
);
2894 Polyhedron
*pos
, *neg
;
2895 value_set_si(M
->p
[0][0], 1);
2896 value_decrement(M
->p
[0][P
->Dimension
+1],
2897 M
->p
[0][P
->Dimension
+1]);
2898 neg
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
2899 value_set_si(f
, -1);
2900 Vector_Scale(M
->p
[0]+1, M
->p
[0]+1, f
,
2902 value_decrement(M
->p
[0][P
->Dimension
+1],
2903 M
->p
[0][P
->Dimension
+1]);
2904 value_decrement(M
->p
[0][P
->Dimension
+1],
2905 M
->p
[0][P
->Dimension
+1]);
2906 pos
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
2907 if (emptyQ(neg
) && emptyQ(pos
)) {
2908 Polyhedron_Free(para
);
2909 Polyhedron_Free(pos
);
2910 Polyhedron_Free(neg
);
2914 fprintf(stderr
, "\nER: Order\n");
2915 #endif /* DEBUG_ER */
2916 EP
= barvinok_enumerate_e_with_options(para
, exist
, nparam
,
2920 E
= barvinok_enumerate_e_with_options(pos
, exist
, nparam
,
2923 free_evalue_refs(E
);
2927 E
= barvinok_enumerate_e_with_options(neg
, exist
, nparam
,
2930 free_evalue_refs(E
);
2933 Polyhedron_Free(para
);
2934 Polyhedron_Free(pos
);
2935 Polyhedron_Free(neg
);
2940 } END_FORALL_PVertex_in_ParamPolyhedron
;
2943 } END_FORALL_PVertex_in_ParamPolyhedron
;
2946 /* Search for vertex coordinate to split on */
2947 /* First look for one independent of the parameters */
2948 FORALL_PVertex_in_ParamPolyhedron(V
, last
, PP
) {
2949 for (int i
= 0; i
< exist
; ++i
) {
2951 for (j
= 0; j
< nparam
; ++j
)
2952 if (value_notzero_p(V
->Vertex
->p
[i
][j
]))
2956 value_set_si(M
->p
[0][0], 1);
2957 Vector_Set(M
->p
[0]+1, 0, nvar
+exist
);
2958 Vector_Copy(V
->Vertex
->p
[i
],
2959 M
->p
[0] + 1 + nvar
+ exist
, nparam
+1);
2960 value_oppose(M
->p
[0][1+nvar
+i
],
2961 V
->Vertex
->p
[i
][nparam
+1]);
2963 Polyhedron
*pos
, *neg
;
2964 value_set_si(M
->p
[0][0], 1);
2965 value_decrement(M
->p
[0][P
->Dimension
+1],
2966 M
->p
[0][P
->Dimension
+1]);
2967 neg
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
2968 value_set_si(f
, -1);
2969 Vector_Scale(M
->p
[0]+1, M
->p
[0]+1, f
,
2971 value_decrement(M
->p
[0][P
->Dimension
+1],
2972 M
->p
[0][P
->Dimension
+1]);
2973 value_decrement(M
->p
[0][P
->Dimension
+1],
2974 M
->p
[0][P
->Dimension
+1]);
2975 pos
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
2976 if (emptyQ(neg
) || emptyQ(pos
)) {
2977 Polyhedron_Free(pos
);
2978 Polyhedron_Free(neg
);
2981 Polyhedron_Free(pos
);
2982 value_increment(M
->p
[0][P
->Dimension
+1],
2983 M
->p
[0][P
->Dimension
+1]);
2984 pos
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
2986 fprintf(stderr
, "\nER: Vertex\n");
2987 #endif /* DEBUG_ER */
2989 EP
= enumerate_or(pos
, exist
, nparam
, options
);
2994 } END_FORALL_PVertex_in_ParamPolyhedron
;
2998 /* Search for vertex coordinate to split on */
2999 /* Now look for one that depends on the parameters */
3000 FORALL_PVertex_in_ParamPolyhedron(V
, last
, PP
) {
3001 for (int i
= 0; i
< exist
; ++i
) {
3002 value_set_si(M
->p
[0][0], 1);
3003 Vector_Set(M
->p
[0]+1, 0, nvar
+exist
);
3004 Vector_Copy(V
->Vertex
->p
[i
],
3005 M
->p
[0] + 1 + nvar
+ exist
, nparam
+1);
3006 value_oppose(M
->p
[0][1+nvar
+i
],
3007 V
->Vertex
->p
[i
][nparam
+1]);
3009 Polyhedron
*pos
, *neg
;
3010 value_set_si(M
->p
[0][0], 1);
3011 value_decrement(M
->p
[0][P
->Dimension
+1],
3012 M
->p
[0][P
->Dimension
+1]);
3013 neg
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
3014 value_set_si(f
, -1);
3015 Vector_Scale(M
->p
[0]+1, M
->p
[0]+1, f
,
3017 value_decrement(M
->p
[0][P
->Dimension
+1],
3018 M
->p
[0][P
->Dimension
+1]);
3019 value_decrement(M
->p
[0][P
->Dimension
+1],
3020 M
->p
[0][P
->Dimension
+1]);
3021 pos
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
3022 if (emptyQ(neg
) || emptyQ(pos
)) {
3023 Polyhedron_Free(pos
);
3024 Polyhedron_Free(neg
);
3027 Polyhedron_Free(pos
);
3028 value_increment(M
->p
[0][P
->Dimension
+1],
3029 M
->p
[0][P
->Dimension
+1]);
3030 pos
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
3032 fprintf(stderr
, "\nER: ParamVertex\n");
3033 #endif /* DEBUG_ER */
3035 EP
= enumerate_or(pos
, exist
, nparam
, options
);
3040 } END_FORALL_PVertex_in_ParamPolyhedron
;
3048 Polyhedron_Free(CEq
);
3052 Param_Polyhedron_Free(PP
);
3058 evalue
* barvinok_enumerate_pip(Polyhedron
*P
, unsigned exist
, unsigned nparam
,
3062 barvinok_options
*options
= barvinok_options_new_with_defaults();
3063 options
->MaxRays
= MaxRays
;
3064 E
= barvinok_enumerate_pip_with_options(P
, exist
, nparam
, options
);
3065 barvinok_options_free(options
);
3070 evalue
*barvinok_enumerate_pip_with_options(Polyhedron
*P
,
3071 unsigned exist
, unsigned nparam
, struct barvinok_options
*options
)
3076 evalue
*barvinok_enumerate_pip_with_options(Polyhedron
*P
,
3077 unsigned exist
, unsigned nparam
, struct barvinok_options
*options
)
3079 int nvar
= P
->Dimension
- exist
- nparam
;
3080 evalue
*EP
= evalue_zero();
3084 fprintf(stderr
, "\nER: PIP\n");
3085 #endif /* DEBUG_ER */
3087 Polyhedron
*D
= pip_projectout(P
, nvar
, exist
, nparam
);
3088 for (Q
= D
; Q
; Q
= N
) {
3092 exist
= Q
->Dimension
- nvar
- nparam
;
3093 E
= barvinok_enumerate_e_with_options(Q
, exist
, nparam
, options
);
3096 free_evalue_refs(E
);
3105 static bool is_single(Value
*row
, int pos
, int len
)
3107 return First_Non_Zero(row
, pos
) == -1 &&
3108 First_Non_Zero(row
+pos
+1, len
-pos
-1) == -1;
3111 static evalue
* barvinok_enumerate_e_r(Polyhedron
*P
,
3112 unsigned exist
, unsigned nparam
, barvinok_options
*options
);
3115 static int er_level
= 0;
3117 evalue
* barvinok_enumerate_e_with_options(Polyhedron
*P
,
3118 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
3120 fprintf(stderr
, "\nER: level %i\n", er_level
);
3122 Polyhedron_PrintConstraints(stderr
, P_VALUE_FMT
, P
);
3123 fprintf(stderr
, "\nE %d\nP %d\n", exist
, nparam
);
3125 P
= DomainConstraintSimplify(Polyhedron_Copy(P
), options
->MaxRays
);
3126 evalue
*EP
= barvinok_enumerate_e_r(P
, exist
, nparam
, options
);
3132 evalue
* barvinok_enumerate_e_with_options(Polyhedron
*P
,
3133 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
3135 P
= DomainConstraintSimplify(Polyhedron_Copy(P
), options
->MaxRays
);
3136 evalue
*EP
= barvinok_enumerate_e_r(P
, exist
, nparam
, options
);
3142 evalue
* barvinok_enumerate_e(Polyhedron
*P
, unsigned exist
, unsigned nparam
,
3146 barvinok_options
*options
= barvinok_options_new_with_defaults();
3147 options
->MaxRays
= MaxRays
;
3148 E
= barvinok_enumerate_e_with_options(P
, exist
, nparam
, options
);
3149 barvinok_options_free(options
);
3153 static evalue
* barvinok_enumerate_e_r(Polyhedron
*P
,
3154 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
3157 Polyhedron
*U
= Universe_Polyhedron(nparam
);
3158 evalue
*EP
= barvinok_enumerate_with_options(P
, U
, options
);
3159 //char *param_name[] = {"P", "Q", "R", "S", "T" };
3160 //print_evalue(stdout, EP, param_name);
3165 int nvar
= P
->Dimension
- exist
- nparam
;
3166 int len
= P
->Dimension
+ 2;
3169 POL_ENSURE_FACETS(P
);
3170 POL_ENSURE_VERTICES(P
);
3173 return evalue_zero();
3175 if (nvar
== 0 && nparam
== 0) {
3176 evalue
*EP
= evalue_zero();
3177 barvinok_count_with_options(P
, &EP
->x
.n
, options
);
3178 if (value_pos_p(EP
->x
.n
))
3179 value_set_si(EP
->x
.n
, 1);
3184 for (r
= 0; r
< P
->NbRays
; ++r
)
3185 if (value_zero_p(P
->Ray
[r
][0]) ||
3186 value_zero_p(P
->Ray
[r
][P
->Dimension
+1])) {
3188 for (i
= 0; i
< nvar
; ++i
)
3189 if (value_notzero_p(P
->Ray
[r
][i
+1]))
3193 for (i
= nvar
+ exist
; i
< nvar
+ exist
+ nparam
; ++i
)
3194 if (value_notzero_p(P
->Ray
[r
][i
+1]))
3196 if (i
>= nvar
+ exist
+ nparam
)
3199 if (r
< P
->NbRays
) {
3200 evalue
*EP
= evalue_zero();
3201 value_set_si(EP
->x
.n
, -1);
3206 for (r
= 0; r
< P
->NbEq
; ++r
)
3207 if ((first
= First_Non_Zero(P
->Constraint
[r
]+1+nvar
, exist
)) != -1)
3210 if (First_Non_Zero(P
->Constraint
[r
]+1+nvar
+first
+1,
3211 exist
-first
-1) != -1) {
3212 Polyhedron
*T
= rotate_along(P
, r
, nvar
, exist
, options
->MaxRays
);
3214 fprintf(stderr
, "\nER: Equality\n");
3215 #endif /* DEBUG_ER */
3216 evalue
*EP
= barvinok_enumerate_e_with_options(T
, exist
-1, nparam
,
3222 fprintf(stderr
, "\nER: Fixed\n");
3223 #endif /* DEBUG_ER */
3225 return barvinok_enumerate_e_with_options(P
, exist
-1, nparam
,
3228 Polyhedron
*T
= Polyhedron_Copy(P
);
3229 SwapColumns(T
, nvar
+1, nvar
+1+first
);
3230 evalue
*EP
= barvinok_enumerate_e_with_options(T
, exist
-1, nparam
,
3238 Vector
*row
= Vector_Alloc(len
);
3239 value_set_si(row
->p
[0], 1);
3244 enum constraint
* info
= new constraint
[exist
];
3245 for (int i
= 0; i
< exist
; ++i
) {
3247 for (int l
= P
->NbEq
; l
< P
->NbConstraints
; ++l
) {
3248 if (value_negz_p(P
->Constraint
[l
][nvar
+i
+1]))
3250 bool l_parallel
= is_single(P
->Constraint
[l
]+nvar
+1, i
, exist
);
3251 for (int u
= P
->NbEq
; u
< P
->NbConstraints
; ++u
) {
3252 if (value_posz_p(P
->Constraint
[u
][nvar
+i
+1]))
3254 bool lu_parallel
= l_parallel
||
3255 is_single(P
->Constraint
[u
]+nvar
+1, i
, exist
);
3256 value_oppose(f
, P
->Constraint
[u
][nvar
+i
+1]);
3257 Vector_Combine(P
->Constraint
[l
]+1, P
->Constraint
[u
]+1, row
->p
+1,
3258 f
, P
->Constraint
[l
][nvar
+i
+1], len
-1);
3259 if (!(info
[i
] & INDEPENDENT
)) {
3261 for (j
= 0; j
< exist
; ++j
)
3262 if (j
!= i
&& value_notzero_p(row
->p
[nvar
+j
+1]))
3265 //printf("independent: i: %d, l: %d, u: %d\n", i, l, u);
3266 info
[i
] = (constraint
)(info
[i
] | INDEPENDENT
);
3269 if (info
[i
] & ALL_POS
) {
3270 value_addto(row
->p
[len
-1], row
->p
[len
-1],
3271 P
->Constraint
[l
][nvar
+i
+1]);
3272 value_addto(row
->p
[len
-1], row
->p
[len
-1], f
);
3273 value_multiply(f
, f
, P
->Constraint
[l
][nvar
+i
+1]);
3274 value_subtract(row
->p
[len
-1], row
->p
[len
-1], f
);
3275 value_decrement(row
->p
[len
-1], row
->p
[len
-1]);
3276 ConstraintSimplify(row
->p
, row
->p
, len
, &f
);
3277 value_set_si(f
, -1);
3278 Vector_Scale(row
->p
+1, row
->p
+1, f
, len
-1);
3279 value_decrement(row
->p
[len
-1], row
->p
[len
-1]);
3280 Polyhedron
*T
= AddConstraints(row
->p
, 1, P
, options
->MaxRays
);
3282 //printf("not all_pos: i: %d, l: %d, u: %d\n", i, l, u);
3283 info
[i
] = (constraint
)(info
[i
] ^ ALL_POS
);
3285 //puts("pos remainder");
3286 //Polyhedron_Print(stdout, P_VALUE_FMT, T);
3289 if (!(info
[i
] & ONE_NEG
)) {
3291 negative_test_constraint(P
->Constraint
[l
],
3293 row
->p
, nvar
+i
, len
, &f
);
3294 oppose_constraint(row
->p
, len
, &f
);
3295 Polyhedron
*T
= AddConstraints(row
->p
, 1, P
,
3298 //printf("one_neg i: %d, l: %d, u: %d\n", i, l, u);
3299 info
[i
] = (constraint
)(info
[i
] | ONE_NEG
);
3301 //puts("neg remainder");
3302 //Polyhedron_Print(stdout, P_VALUE_FMT, T);
3304 } else if (!(info
[i
] & ROT_NEG
)) {
3305 if (parallel_constraints(P
->Constraint
[l
],
3307 row
->p
, nvar
, exist
)) {
3308 negative_test_constraint7(P
->Constraint
[l
],
3310 row
->p
, nvar
, exist
,
3312 oppose_constraint(row
->p
, len
, &f
);
3313 Polyhedron
*T
= AddConstraints(row
->p
, 1, P
,
3316 // printf("rot_neg i: %d, l: %d, u: %d\n", i, l, u);
3317 info
[i
] = (constraint
)(info
[i
] | ROT_NEG
);
3320 //puts("neg remainder");
3321 //Polyhedron_Print(stdout, P_VALUE_FMT, T);
3326 if (!(info
[i
] & ALL_POS
) && (info
[i
] & (ONE_NEG
| ROT_NEG
)))
3330 if (info
[i
] & ALL_POS
)
3337 for (int i = 0; i < exist; ++i)
3338 printf("%i: %i\n", i, info[i]);
3340 for (int i
= 0; i
< exist
; ++i
)
3341 if (info
[i
] & ALL_POS
) {
3343 fprintf(stderr
, "\nER: Positive\n");
3344 #endif /* DEBUG_ER */
3346 // Maybe we should chew off some of the fat here
3347 Matrix
*M
= Matrix_Alloc(P
->Dimension
, P
->Dimension
+1);
3348 for (int j
= 0; j
< P
->Dimension
; ++j
)
3349 value_set_si(M
->p
[j
][j
+ (j
>= i
+nvar
)], 1);
3350 Polyhedron
*T
= Polyhedron_Image(P
, M
, options
->MaxRays
);
3352 evalue
*EP
= barvinok_enumerate_e_with_options(T
, exist
-1, nparam
,
3360 for (int i
= 0; i
< exist
; ++i
)
3361 if (info
[i
] & ONE_NEG
) {
3363 fprintf(stderr
, "\nER: Negative\n");
3364 #endif /* DEBUG_ER */
3369 return barvinok_enumerate_e_with_options(P
, exist
-1, nparam
,
3372 Polyhedron
*T
= Polyhedron_Copy(P
);
3373 SwapColumns(T
, nvar
+1, nvar
+1+i
);
3374 evalue
*EP
= barvinok_enumerate_e_with_options(T
, exist
-1, nparam
,
3380 for (int i
= 0; i
< exist
; ++i
)
3381 if (info
[i
] & ROT_NEG
) {
3383 fprintf(stderr
, "\nER: Rotate\n");
3384 #endif /* DEBUG_ER */
3388 Polyhedron
*T
= rotate_along(P
, r
, nvar
, exist
, options
->MaxRays
);
3389 evalue
*EP
= barvinok_enumerate_e_with_options(T
, exist
-1, nparam
,
3394 for (int i
= 0; i
< exist
; ++i
)
3395 if (info
[i
] & INDEPENDENT
) {
3396 Polyhedron
*pos
, *neg
;
3398 /* Find constraint again and split off negative part */
3400 if (SplitOnVar(P
, i
, nvar
, exist
, options
->MaxRays
,
3401 row
, f
, true, &pos
, &neg
)) {
3403 fprintf(stderr
, "\nER: Split\n");
3404 #endif /* DEBUG_ER */
3407 barvinok_enumerate_e_with_options(neg
, exist
-1, nparam
, options
);
3409 barvinok_enumerate_e_with_options(pos
, exist
, nparam
, options
);
3411 free_evalue_refs(E
);
3413 Polyhedron_Free(neg
);
3414 Polyhedron_Free(pos
);
3428 EP
= enumerate_line(P
, exist
, nparam
, options
);
3432 EP
= barvinok_enumerate_pip_with_options(P
, exist
, nparam
, options
);
3436 EP
= enumerate_redundant_ray(P
, exist
, nparam
, options
);
3440 EP
= enumerate_sure(P
, exist
, nparam
, options
);
3444 EP
= enumerate_ray(P
, exist
, nparam
, options
);
3448 EP
= enumerate_sure2(P
, exist
, nparam
, options
);
3452 F
= unfringe(P
, options
->MaxRays
);
3453 if (!PolyhedronIncludes(F
, P
)) {
3455 fprintf(stderr
, "\nER: Fringed\n");
3456 #endif /* DEBUG_ER */
3457 EP
= barvinok_enumerate_e_with_options(F
, exist
, nparam
, options
);
3464 EP
= enumerate_vd(&P
, exist
, nparam
, options
);
3469 EP
= enumerate_sum(P
, exist
, nparam
, options
);
3476 Polyhedron
*pos
, *neg
;
3477 for (i
= 0; i
< exist
; ++i
)
3478 if (SplitOnVar(P
, i
, nvar
, exist
, options
->MaxRays
,
3479 row
, f
, false, &pos
, &neg
))
3485 EP
= enumerate_or(pos
, exist
, nparam
, options
);
3498 * remove equalities that require a "compression" of the parameters
3500 static Polyhedron
*remove_more_equalities(Polyhedron
*P
, unsigned nparam
,
3501 Matrix
**CP
, unsigned MaxRays
)
3504 remove_all_equalities(&P
, NULL
, CP
, NULL
, nparam
, MaxRays
);
3511 static gen_fun
*series(Polyhedron
*P
, unsigned nparam
, barvinok_options
*options
)
3521 assert(!Polyhedron_is_unbounded(P
, nparam
, options
->MaxRays
));
3522 assert(P
->NbBid
== 0);
3523 assert(Polyhedron_has_revlex_positive_rays(P
, nparam
));
3525 P
= remove_more_equalities(P
, nparam
, &CP
, options
->MaxRays
);
3526 assert(P
->NbEq
== 0);
3528 nparam
= CP
->NbColumns
-1;
3533 barvinok_count_with_options(P
, &c
, options
);
3534 gf
= new gen_fun(c
);
3538 red
= gf_base::create(Polyhedron_Project(P
, nparam
),
3539 P
->Dimension
, nparam
, options
);
3540 POL_ENSURE_VERTICES(P
);
3541 red
->start_gf(P
, options
);
3553 gen_fun
* barvinok_series_with_options(Polyhedron
*P
, Polyhedron
* C
,
3554 barvinok_options
*options
)
3557 unsigned nparam
= C
->Dimension
;
3560 CA
= align_context(C
, P
->Dimension
, options
->MaxRays
);
3561 P
= DomainIntersection(P
, CA
, options
->MaxRays
);
3562 Polyhedron_Free(CA
);
3564 gf
= series(P
, nparam
, options
);
3569 gen_fun
* barvinok_series(Polyhedron
*P
, Polyhedron
* C
, unsigned MaxRays
)
3572 barvinok_options
*options
= barvinok_options_new_with_defaults();
3573 options
->MaxRays
= MaxRays
;
3574 gf
= barvinok_series_with_options(P
, C
, options
);
3575 barvinok_options_free(options
);
3579 static Polyhedron
*skew_into_positive_orthant(Polyhedron
*D
, unsigned nparam
,
3585 for (Polyhedron
*P
= D
; P
; P
= P
->next
) {
3586 POL_ENSURE_VERTICES(P
);
3587 assert(!Polyhedron_is_unbounded(P
, nparam
, MaxRays
));
3588 assert(P
->NbBid
== 0);
3589 assert(Polyhedron_has_positive_rays(P
, nparam
));
3591 for (int r
= 0; r
< P
->NbRays
; ++r
) {
3592 if (value_notzero_p(P
->Ray
[r
][P
->Dimension
+1]))
3594 for (int i
= 0; i
< nparam
; ++i
) {
3596 if (value_posz_p(P
->Ray
[r
][i
+1]))
3599 M
= Matrix_Alloc(D
->Dimension
+1, D
->Dimension
+1);
3600 for (int i
= 0; i
< D
->Dimension
+1; ++i
)
3601 value_set_si(M
->p
[i
][i
], 1);
3603 Inner_Product(P
->Ray
[r
]+1, M
->p
[i
], D
->Dimension
+1, &tmp
);
3604 if (value_posz_p(tmp
))
3607 for (j
= P
->Dimension
- nparam
; j
< P
->Dimension
; ++j
)
3608 if (value_pos_p(P
->Ray
[r
][j
+1]))
3610 assert(j
< P
->Dimension
);
3611 value_pdivision(tmp
, P
->Ray
[r
][j
+1], P
->Ray
[r
][i
+1]);
3612 value_subtract(M
->p
[i
][j
], M
->p
[i
][j
], tmp
);
3618 D
= DomainImage(D
, M
, MaxRays
);
3624 gen_fun
* barvinok_enumerate_union_series_with_options(Polyhedron
*D
, Polyhedron
* C
,
3625 barvinok_options
*options
)
3627 Polyhedron
*conv
, *D2
;
3629 gen_fun
*gf
= NULL
, *gf2
;
3630 unsigned nparam
= C
->Dimension
;
3635 CA
= align_context(C
, D
->Dimension
, options
->MaxRays
);
3636 D
= DomainIntersection(D
, CA
, options
->MaxRays
);
3637 Polyhedron_Free(CA
);
3639 D2
= skew_into_positive_orthant(D
, nparam
, options
->MaxRays
);
3640 for (Polyhedron
*P
= D2
; P
; P
= P
->next
) {
3641 assert(P
->Dimension
== D2
->Dimension
);
3644 P_gf
= series(Polyhedron_Copy(P
), nparam
, options
);
3648 gf
->add_union(P_gf
, options
);
3652 /* we actually only need the convex union of the parameter space
3653 * but the reducer classes currently expect a polyhedron in
3654 * the combined space
3656 Polyhedron_Free(gf
->context
);
3657 gf
->context
= DomainConvex(D2
, options
->MaxRays
);
3659 gf2
= gf
->summate(D2
->Dimension
- nparam
, options
);
3668 gen_fun
* barvinok_enumerate_union_series(Polyhedron
*D
, Polyhedron
* C
,
3672 barvinok_options
*options
= barvinok_options_new_with_defaults();
3673 options
->MaxRays
= MaxRays
;
3674 gf
= barvinok_enumerate_union_series_with_options(D
, C
, options
);
3675 barvinok_options_free(options
);
3679 evalue
* barvinok_enumerate_union(Polyhedron
*D
, Polyhedron
* C
, unsigned MaxRays
)
3682 gen_fun
*gf
= barvinok_enumerate_union_series(D
, C
, MaxRays
);