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"
23 #include "decomposer.h"
24 #include "lattice_point.h"
25 #include "reduce_domain.h"
26 #include "genfun_constructor.h"
27 #include "remove_equalities.h"
40 using std::ostringstream
;
42 #define ALLOC(t,p) p = (t*)malloc(sizeof(*p))
55 coeff
= Matrix_Alloc(d
+1, d
+1+1);
56 value_set_si(coeff
->p
[0][0], 1);
57 value_set_si(coeff
->p
[0][d
+1], 1);
58 for (int i
= 1; i
<= d
; ++i
) {
59 value_multiply(coeff
->p
[i
][0], coeff
->p
[i
-1][0], d0
);
60 Vector_Combine(coeff
->p
[i
-1], coeff
->p
[i
-1]+1, coeff
->p
[i
]+1,
62 value_set_si(coeff
->p
[i
][d
+1], i
);
63 value_multiply(coeff
->p
[i
][d
+1], coeff
->p
[i
][d
+1], coeff
->p
[i
-1][d
+1]);
64 value_decrement(d0
, d0
);
69 void div(dpoly
& d
, Vector
*count
, ZZ
& sign
) {
70 int len
= coeff
->NbRows
;
71 Matrix
* c
= Matrix_Alloc(coeff
->NbRows
, coeff
->NbColumns
);
74 for (int i
= 0; i
< len
; ++i
) {
75 Vector_Copy(coeff
->p
[i
], c
->p
[i
], len
+1);
76 for (int j
= 1; j
<= i
; ++j
) {
77 value_multiply(tmp
, d
.coeff
->p
[j
], c
->p
[i
][len
]);
78 value_oppose(tmp
, tmp
);
79 Vector_Combine(c
->p
[i
], c
->p
[i
-j
], c
->p
[i
],
80 c
->p
[i
-j
][len
], tmp
, len
);
81 value_multiply(c
->p
[i
][len
], c
->p
[i
][len
], c
->p
[i
-j
][len
]);
83 value_multiply(c
->p
[i
][len
], c
->p
[i
][len
], d
.coeff
->p
[0]);
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 struct bfe_term
: public bfc_term_base
{
303 vector
<evalue
*> factors
;
305 bfe_term(int len
) : bfc_term_base(len
) {
309 for (int i
= 0; i
< factors
.size(); ++i
) {
312 free_evalue_refs(factors
[i
]);
318 static void print_int_vector(int *v
, int len
, char *name
)
320 cerr
<< name
<< endl
;
321 for (int j
= 0; j
< len
; ++j
) {
327 static void print_bfc_terms(mat_ZZ
& factors
, bfc_vec
& v
)
330 cerr
<< "factors" << endl
;
331 cerr
<< factors
<< endl
;
332 for (int i
= 0; i
< v
.size(); ++i
) {
333 cerr
<< "term: " << i
<< endl
;
334 print_int_vector(v
[i
]->powers
, factors
.NumRows(), "powers");
335 cerr
<< "terms" << endl
;
336 cerr
<< v
[i
]->terms
<< endl
;
337 bfc_term
* bfct
= static_cast<bfc_term
*>(v
[i
]);
338 cerr
<< bfct
->c
<< endl
;
342 static void print_bfe_terms(mat_ZZ
& factors
, bfc_vec
& v
)
345 cerr
<< "factors" << endl
;
346 cerr
<< factors
<< endl
;
347 for (int i
= 0; i
< v
.size(); ++i
) {
348 cerr
<< "term: " << i
<< endl
;
349 print_int_vector(v
[i
]->powers
, factors
.NumRows(), "powers");
350 cerr
<< "terms" << endl
;
351 cerr
<< v
[i
]->terms
<< endl
;
352 bfe_term
* bfet
= static_cast<bfe_term
*>(v
[i
]);
353 for (int j
= 0; j
< v
[i
]->terms
.NumRows(); ++j
) {
354 char * test
[] = {"a", "b"};
355 print_evalue(stderr
, bfet
->factors
[j
], test
);
356 fprintf(stderr
, "\n");
361 struct bfcounter
: public bfcounter_base
{
365 bfcounter(unsigned dim
) : bfcounter_base(dim
) {
374 virtual void base(mat_ZZ
& factors
, bfc_vec
& v
);
375 virtual void get_count(Value
*result
) {
376 assert(value_one_p(&count
[0]._mp_den
));
377 value_assign(*result
, &count
[0]._mp_num
);
381 void bfcounter::base(mat_ZZ
& factors
, bfc_vec
& v
)
383 unsigned nf
= factors
.NumRows();
385 for (int i
= 0; i
< v
.size(); ++i
) {
386 bfc_term
* bfct
= static_cast<bfc_term
*>(v
[i
]);
388 // factor is always positive, so we always
390 for (int k
= 0; k
< nf
; ++k
)
391 total_power
+= v
[i
]->powers
[k
];
394 for (j
= 0; j
< nf
; ++j
)
395 if (v
[i
]->powers
[j
] > 0)
398 zz2value(factors
[j
][0], tz
);
399 dpoly
D(total_power
, tz
, 1);
400 for (int k
= 1; k
< v
[i
]->powers
[j
]; ++k
) {
401 zz2value(factors
[j
][0], tz
);
402 dpoly
fact(total_power
, tz
, 1);
406 for (int k
= 0; k
< v
[i
]->powers
[j
]; ++k
) {
407 zz2value(factors
[j
][0], tz
);
408 dpoly
fact(total_power
, tz
, 1);
412 for (int k
= 0; k
< v
[i
]->terms
.NumRows(); ++k
) {
413 zz2value(v
[i
]->terms
[k
][0], tz
);
414 dpoly
n(total_power
, tz
);
415 mpq_set_si(tcount
, 0, 1);
416 n
.div(D
, tcount
, one
);
418 bfct
->c
[k
].n
= -bfct
->c
[k
].n
;
419 zz2value(bfct
->c
[k
].n
, tn
);
420 zz2value(bfct
->c
[k
].d
, td
);
422 mpz_mul(mpq_numref(tcount
), mpq_numref(tcount
), tn
);
423 mpz_mul(mpq_denref(tcount
), mpq_denref(tcount
), td
);
424 mpq_canonicalize(tcount
);
425 mpq_add(count
, count
, tcount
);
432 /* Check whether the polyhedron is unbounded and if so,
433 * check whether it has any (and therefore an infinite number of)
435 * If one of the vertices is integer, then we are done.
436 * Otherwise, transform the polyhedron such that one of the rays
437 * is the first unit vector and cut it off at a height that ensures
438 * that if the whole polyhedron has any points, then the remaining part
439 * has integer points. In particular we add the largest coefficient
440 * of a ray to the highest vertex (rounded up).
442 static bool Polyhedron_is_infinite(Polyhedron
*P
, Value
* result
,
443 barvinok_options
*options
)
455 for (; r
< P
->NbRays
; ++r
)
456 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1]))
458 if (P
->NbBid
== 0 && r
== P
->NbRays
)
461 if (options
->count_sample_infinite
) {
464 sample
= Polyhedron_Sample(P
, options
);
466 value_set_si(*result
, 0);
468 value_set_si(*result
, -1);
474 for (int i
= 0; i
< P
->NbRays
; ++i
)
475 if (value_one_p(P
->Ray
[i
][1+P
->Dimension
])) {
476 value_set_si(*result
, -1);
481 M
= Matrix_Alloc(P
->Dimension
+1, P
->Dimension
+1);
482 Vector_Gcd(P
->Ray
[r
]+1, P
->Dimension
, &g
);
483 Vector_AntiScale(P
->Ray
[r
]+1, M
->p
[0], g
, P
->Dimension
+1);
484 int ok
= unimodular_complete(M
, 1);
486 value_set_si(M
->p
[P
->Dimension
][P
->Dimension
], 1);
489 P
= Polyhedron_Preimage(P
, M2
, 0);
497 value_set_si(size
, 0);
499 for (int i
= 0; i
< P
->NbBid
; ++i
) {
500 value_absolute(tmp
, P
->Ray
[i
][1]);
501 if (value_gt(tmp
, size
))
502 value_assign(size
, tmp
);
504 for (int i
= P
->NbBid
; i
< P
->NbRays
; ++i
) {
505 if (value_zero_p(P
->Ray
[i
][P
->Dimension
+1])) {
506 if (value_gt(P
->Ray
[i
][1], size
))
507 value_assign(size
, P
->Ray
[i
][1]);
510 mpz_cdiv_q(tmp
, P
->Ray
[i
][1], P
->Ray
[i
][P
->Dimension
+1]);
511 if (first
|| value_gt(tmp
, offset
)) {
512 value_assign(offset
, tmp
);
516 value_addto(offset
, offset
, size
);
520 v
= Vector_Alloc(P
->Dimension
+2);
521 value_set_si(v
->p
[0], 1);
522 value_set_si(v
->p
[1], -1);
523 value_assign(v
->p
[1+P
->Dimension
], offset
);
524 R
= AddConstraints(v
->p
, 1, P
, options
->MaxRays
);
532 barvinok_count_with_options(P
, &c
, options
);
535 value_set_si(*result
, 0);
537 value_set_si(*result
, -1);
543 typedef Polyhedron
* Polyhedron_p
;
545 static void barvinok_count_f(Polyhedron
*P
, Value
* result
,
546 barvinok_options
*options
);
548 void barvinok_count_with_options(Polyhedron
*P
, Value
* result
,
549 struct barvinok_options
*options
)
554 bool infinite
= false;
558 "barvinok_count: input is a union; only first polyhedron is counted\n");
561 value_set_si(*result
, 0);
567 P
= remove_equalities(P
, options
->MaxRays
);
568 P
= DomainConstraintSimplify(P
, options
->MaxRays
);
572 } while (!emptyQ(P
) && P
->NbEq
!= 0);
575 value_set_si(*result
, 0);
580 if (Polyhedron_is_infinite(P
, result
, options
)) {
585 if (P
->Dimension
== 0) {
586 /* Test whether the constraints are satisfied */
587 POL_ENSURE_VERTICES(P
);
588 value_set_si(*result
, !emptyQ(P
));
593 Q
= Polyhedron_Factor(P
, 0, NULL
, options
->MaxRays
);
601 barvinok_count_f(P
, result
, options
);
602 if (value_neg_p(*result
))
604 if (Q
&& P
->next
&& value_notzero_p(*result
)) {
608 for (Q
= P
->next
; Q
; Q
= Q
->next
) {
609 barvinok_count_f(Q
, &factor
, options
);
610 if (value_neg_p(factor
)) {
613 } else if (Q
->next
&& value_zero_p(factor
)) {
614 value_set_si(*result
, 0);
617 value_multiply(*result
, *result
, factor
);
626 value_set_si(*result
, -1);
629 void barvinok_count(Polyhedron
*P
, Value
* result
, unsigned NbMaxCons
)
631 barvinok_options
*options
= barvinok_options_new_with_defaults();
632 options
->MaxRays
= NbMaxCons
;
633 barvinok_count_with_options(P
, result
, options
);
634 barvinok_options_free(options
);
637 static void barvinok_count_f(Polyhedron
*P
, Value
* result
,
638 barvinok_options
*options
)
641 value_set_si(*result
, 0);
645 if (P
->Dimension
== 1)
646 return Line_Length(P
, result
);
648 int c
= P
->NbConstraints
;
649 POL_ENSURE_FACETS(P
);
650 if (c
!= P
->NbConstraints
|| P
->NbEq
!= 0) {
651 Polyhedron
*next
= P
->next
;
653 barvinok_count_with_options(P
, result
, options
);
658 POL_ENSURE_VERTICES(P
);
660 if (Polyhedron_is_infinite(P
, result
, options
))
664 if (options
->incremental_specialization
== BV_SPECIALIZATION_BF
)
665 cnt
= new bfcounter(P
->Dimension
);
666 else if (options
->incremental_specialization
== BV_SPECIALIZATION_DF
)
667 cnt
= new icounter(P
->Dimension
);
668 else if (options
->incremental_specialization
== BV_SPECIALIZATION_TODD
)
669 cnt
= new tcounter(P
->Dimension
, options
->max_index
);
671 cnt
= new counter(P
->Dimension
, options
->max_index
);
672 cnt
->start(P
, options
);
674 cnt
->get_count(result
);
678 static void uni_polynom(int param
, Vector
*c
, evalue
*EP
)
680 unsigned dim
= c
->Size
-2;
682 value_set_si(EP
->d
,0);
683 EP
->x
.p
= new_enode(polynomial
, dim
+1, param
+1);
684 for (int j
= 0; j
<= dim
; ++j
)
685 evalue_set(&EP
->x
.p
->arr
[j
], c
->p
[j
], c
->p
[dim
+1]);
688 Polyhedron
*unfringe (Polyhedron
*P
, unsigned MaxRays
)
690 int len
= P
->Dimension
+2;
691 Polyhedron
*T
, *R
= P
;
694 Vector
*row
= Vector_Alloc(len
);
695 value_set_si(row
->p
[0], 1);
697 R
= DomainConstraintSimplify(Polyhedron_Copy(P
), MaxRays
);
699 Matrix
*M
= Matrix_Alloc(2, len
-1);
700 value_set_si(M
->p
[1][len
-2], 1);
701 for (int v
= 0; v
< P
->Dimension
; ++v
) {
702 value_set_si(M
->p
[0][v
], 1);
703 Polyhedron
*I
= Polyhedron_Image(R
, M
, 2+1);
704 value_set_si(M
->p
[0][v
], 0);
705 for (int r
= 0; r
< I
->NbConstraints
; ++r
) {
706 if (value_zero_p(I
->Constraint
[r
][0]))
708 if (value_zero_p(I
->Constraint
[r
][1]))
710 if (value_one_p(I
->Constraint
[r
][1]))
712 if (value_mone_p(I
->Constraint
[r
][1]))
714 value_absolute(g
, I
->Constraint
[r
][1]);
715 Vector_Set(row
->p
+1, 0, len
-2);
716 value_division(row
->p
[1+v
], I
->Constraint
[r
][1], g
);
717 mpz_fdiv_q(row
->p
[len
-1], I
->Constraint
[r
][2], g
);
719 R
= AddConstraints(row
->p
, 1, R
, MaxRays
);
731 /* Check whether all rays point in the positive directions
734 static bool Polyhedron_has_positive_rays(Polyhedron
*P
, unsigned nparam
)
737 for (r
= 0; r
< P
->NbRays
; ++r
)
738 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1])) {
740 for (i
= P
->Dimension
- nparam
; i
< P
->Dimension
; ++i
)
741 if (value_neg_p(P
->Ray
[r
][i
+1]))
747 typedef evalue
* evalue_p
;
749 struct enumerator_base
{
753 vertex_decomposer
*vpd
;
755 enumerator_base(unsigned dim
, vertex_decomposer
*vpd
)
760 vE
= new evalue_p
[vpd
->nbV
];
761 for (int j
= 0; j
< vpd
->nbV
; ++j
)
765 evalue_set_si(&mone
, -1, 1);
768 void decompose_at(Param_Vertices
*V
, int _i
, barvinok_options
*options
) {
772 value_init(vE
[_i
]->d
);
773 evalue_set_si(vE
[_i
], 0, 1);
775 vpd
->decompose_at_vertex(V
, _i
, options
);
778 virtual ~enumerator_base() {
779 for (int j
= 0; j
< vpd
->nbV
; ++j
)
781 free_evalue_refs(vE
[j
]);
786 free_evalue_refs(&mone
);
789 static enumerator_base
*create(Polyhedron
*P
, unsigned dim
, unsigned nbV
,
790 barvinok_options
*options
);
793 struct enumerator
: public signed_cone_consumer
, public vertex_decomposer
,
794 public enumerator_base
{
803 enumerator(Polyhedron
*P
, unsigned dim
, unsigned nbV
) :
804 vertex_decomposer(P
, nbV
, *this), enumerator_base(dim
, this) {
807 randomvector(P
, lambda
, dim
);
809 c
= Vector_Alloc(dim
+2);
821 virtual void handle(const signed_cone
& sc
, barvinok_options
*options
);
824 void enumerator::handle(const signed_cone
& sc
, barvinok_options
*options
)
827 assert(sc
.rays
.NumRows() == dim
);
828 for (int k
= 0; k
< dim
; ++k
) {
829 if (lambda
* sc
.rays
[k
] == 0)
835 lattice_point(V
, sc
.rays
, lambda
, &num
, sc
.det
, sc
.closed
, options
);
836 den
= sc
.rays
* lambda
;
841 zz2value(den
[0], tz
);
843 for (int k
= 1; k
< dim
; ++k
) {
844 zz2value(den
[k
], tz
);
845 dpoly
fact(dim
, tz
, 1);
851 for (unsigned long i
= 0; i
< sc
.det
; ++i
) {
852 evalue
*EV
= evalue_polynomial(c
, num
.E
[i
]);
854 free_evalue_refs(EV
);
856 free_evalue_refs(num
.E
[i
]);
861 mpq_set_si(count
, 0, 1);
862 if (num
.constant
.length() == 1) {
863 zz2value(num
.constant
[0], tz
);
865 d
.div(n
, count
, sign
);
872 for (unsigned long i
= 0; i
< sc
.det
; ++i
) {
873 value_assign(acc
, c
->p
[dim
]);
874 zz2value(num
.constant
[i
], x
);
875 for (int j
= dim
-1; j
>= 0; --j
) {
876 value_multiply(acc
, acc
, x
);
877 value_addto(acc
, acc
, c
->p
[j
]);
879 value_addto(mpq_numref(count
), mpq_numref(count
), acc
);
881 mpz_set(mpq_denref(count
), c
->p
[dim
+1]);
887 evalue_set(&EV
, &count
[0]._mp_num
, &count
[0]._mp_den
);
889 free_evalue_refs(&EV
);
893 struct ienumerator_base
: enumerator_base
{
896 ienumerator_base(unsigned dim
, vertex_decomposer
*vpd
) :
897 enumerator_base(dim
,vpd
) {
898 E_vertex
= new evalue_p
[dim
];
901 virtual ~ienumerator_base() {
905 evalue
*E_num(int i
, int d
) {
906 return E_vertex
[i
+ (dim
-d
)];
915 cumulator(evalue
*factor
, evalue
*v
, dpoly_r
*r
) :
916 factor(factor
), v(v
), r(r
) {}
918 void cumulate(barvinok_options
*options
);
920 virtual void add_term(const vector
<int>& powers
, evalue
*f2
) = 0;
921 virtual ~cumulator() {}
924 void cumulator::cumulate(barvinok_options
*options
)
926 evalue cum
; // factor * 1 * E_num[0]/1 * (E_num[0]-1)/2 *...
928 evalue t
; // E_num[0] - (m-1)
932 if (options
->lookup_table
) {
934 evalue_set_si(&mone
, -1, 1);
938 evalue_copy(&cum
, factor
);
941 value_set_si(f
.d
, 1);
942 value_set_si(f
.x
.n
, 1);
946 if (!options
->lookup_table
) {
947 for (cst
= &t
; value_zero_p(cst
->d
); ) {
948 if (cst
->x
.p
->type
== fractional
)
949 cst
= &cst
->x
.p
->arr
[1];
951 cst
= &cst
->x
.p
->arr
[0];
955 for (int m
= 0; m
< r
->len
; ++m
) {
958 value_set_si(f
.d
, m
);
960 if (!options
->lookup_table
)
961 value_subtract(cst
->x
.n
, cst
->x
.n
, cst
->d
);
967 dpoly_r_term_list
& current
= r
->c
[r
->len
-1-m
];
968 dpoly_r_term_list::iterator j
;
969 for (j
= current
.begin(); j
!= current
.end(); ++j
) {
970 if ((*j
)->coeff
== 0)
972 evalue
*f2
= new evalue
;
975 zz2value((*j
)->coeff
, f2
->x
.n
);
976 zz2value(r
->denom
, f2
->d
);
979 add_term((*j
)->powers
, f2
);
982 free_evalue_refs(&f
);
983 free_evalue_refs(&t
);
984 free_evalue_refs(&cum
);
985 if (options
->lookup_table
)
986 free_evalue_refs(&mone
);
994 struct ie_cum
: public cumulator
{
995 vector
<E_poly_term
*> terms
;
997 ie_cum(evalue
*factor
, evalue
*v
, dpoly_r
*r
) : cumulator(factor
, v
, r
) {}
999 virtual void add_term(const vector
<int>& powers
, evalue
*f2
);
1002 void ie_cum::add_term(const vector
<int>& powers
, evalue
*f2
)
1005 for (k
= 0; k
< terms
.size(); ++k
) {
1006 if (terms
[k
]->powers
== powers
) {
1007 eadd(f2
, terms
[k
]->E
);
1008 free_evalue_refs(f2
);
1013 if (k
>= terms
.size()) {
1014 E_poly_term
*ET
= new E_poly_term
;
1015 ET
->powers
= powers
;
1017 terms
.push_back(ET
);
1021 struct ienumerator
: public signed_cone_consumer
, public vertex_decomposer
,
1022 public ienumerator_base
{
1029 ienumerator(Polyhedron
*P
, unsigned dim
, unsigned nbV
) :
1030 vertex_decomposer(P
, nbV
, *this), ienumerator_base(dim
, this) {
1031 vertex
.SetDims(1, dim
);
1033 den
.SetDims(dim
, dim
);
1043 virtual void handle(const signed_cone
& sc
, barvinok_options
*options
);
1044 void reduce(evalue
*factor
, const mat_ZZ
& num
, const mat_ZZ
& den_f
,
1045 barvinok_options
*options
);
1048 void ienumerator::reduce(evalue
*factor
, const mat_ZZ
& num
, const mat_ZZ
& den_f
,
1049 barvinok_options
*options
)
1051 unsigned len
= den_f
.NumRows(); // number of factors in den
1052 unsigned dim
= num
.NumCols();
1053 assert(num
.NumRows() == 1);
1056 eadd(factor
, vE
[vert
]);
1065 split_one(num
, num_s
, num_p
, den_f
, den_s
, den_r
);
1068 den_p
.SetLength(len
);
1072 normalize(one
, num_s
, num_p
, den_s
, den_p
, den_r
);
1074 emul(&mone
, factor
);
1078 for (int k
= 0; k
< len
; ++k
) {
1081 else if (den_s
[k
] == 0)
1084 if (no_param
== 0) {
1085 reduce(factor
, num_p
, den_r
, options
);
1089 pden
.SetDims(only_param
, dim
-1);
1091 for (k
= 0, l
= 0; k
< len
; ++k
)
1093 pden
[l
++] = den_r
[k
];
1095 for (k
= 0; k
< len
; ++k
)
1099 zz2value(num_s
[0], tz
);
1100 dpoly
n(no_param
, tz
);
1101 zz2value(den_s
[k
], tz
);
1102 dpoly
D(no_param
, tz
, 1);
1103 for ( ; ++k
< len
; )
1104 if (den_p
[k
] == 0) {
1105 zz2value(den_s
[k
], tz
);
1106 dpoly
fact(no_param
, tz
, 1);
1111 // if no_param + only_param == len then all powers
1112 // below will be all zero
1113 if (no_param
+ only_param
== len
) {
1114 if (E_num(0, dim
) != 0)
1115 r
= new dpoly_r(n
, len
);
1117 mpq_set_si(tcount
, 0, 1);
1119 n
.div(D
, tcount
, one
);
1121 if (value_notzero_p(mpq_numref(tcount
))) {
1125 value_assign(f
.x
.n
, mpq_numref(tcount
));
1126 value_assign(f
.d
, mpq_denref(tcount
));
1128 reduce(factor
, num_p
, pden
, options
);
1129 free_evalue_refs(&f
);
1134 for (k
= 0; k
< len
; ++k
) {
1135 if (den_s
[k
] == 0 || den_p
[k
] == 0)
1138 zz2value(den_s
[k
], tz
);
1139 dpoly
pd(no_param
-1, tz
, 1);
1142 for (l
= 0; l
< k
; ++l
)
1143 if (den_r
[l
] == den_r
[k
])
1147 r
= new dpoly_r(n
, pd
, l
, len
);
1149 dpoly_r
*nr
= new dpoly_r(r
, pd
, l
, len
);
1155 dpoly_r
*rc
= r
->div(D
);
1158 if (E_num(0, dim
) == 0) {
1159 int common
= pden
.NumRows();
1160 dpoly_r_term_list
& final
= r
->c
[r
->len
-1];
1166 zz2value(r
->denom
, f
.d
);
1167 dpoly_r_term_list::iterator j
;
1168 for (j
= final
.begin(); j
!= final
.end(); ++j
) {
1169 if ((*j
)->coeff
== 0)
1172 for (int k
= 0; k
< r
->dim
; ++k
) {
1173 int n
= (*j
)->powers
[k
];
1176 pden
.SetDims(rows
+n
, pden
.NumCols());
1177 for (int l
= 0; l
< n
; ++l
)
1178 pden
[rows
+l
] = den_r
[k
];
1182 evalue_copy(&t
, factor
);
1183 zz2value((*j
)->coeff
, f
.x
.n
);
1185 reduce(&t
, num_p
, pden
, options
);
1186 free_evalue_refs(&t
);
1188 free_evalue_refs(&f
);
1190 ie_cum
cum(factor
, E_num(0, dim
), r
);
1191 cum
.cumulate(options
);
1193 int common
= pden
.NumRows();
1195 for (int j
= 0; j
< cum
.terms
.size(); ++j
) {
1197 pden
.SetDims(rows
, pden
.NumCols());
1198 for (int k
= 0; k
< r
->dim
; ++k
) {
1199 int n
= cum
.terms
[j
]->powers
[k
];
1202 pden
.SetDims(rows
+n
, pden
.NumCols());
1203 for (int l
= 0; l
< n
; ++l
)
1204 pden
[rows
+l
] = den_r
[k
];
1207 reduce(cum
.terms
[j
]->E
, num_p
, pden
, options
);
1208 free_evalue_refs(cum
.terms
[j
]->E
);
1209 delete cum
.terms
[j
]->E
;
1210 delete cum
.terms
[j
];
1217 static int type_offset(enode
*p
)
1219 return p
->type
== fractional
? 1 :
1220 p
->type
== flooring
? 1 : 0;
1223 static int edegree(evalue
*e
)
1228 if (value_notzero_p(e
->d
))
1232 int i
= type_offset(p
);
1233 if (p
->size
-i
-1 > d
)
1234 d
= p
->size
- i
- 1;
1235 for (; i
< p
->size
; i
++) {
1236 int d2
= edegree(&p
->arr
[i
]);
1243 void ienumerator::handle(const signed_cone
& sc
, barvinok_options
*options
)
1245 assert(sc
.det
== 1);
1247 assert(sc
.rays
.NumRows() == dim
);
1249 lattice_point(V
, sc
.rays
, vertex
[0], E_vertex
, options
);
1255 evalue_set_si(&one
, sc
.sign
, 1);
1256 reduce(&one
, vertex
, den
, options
);
1257 free_evalue_refs(&one
);
1259 for (int i
= 0; i
< dim
; ++i
)
1261 free_evalue_refs(E_vertex
[i
]);
1266 struct bfenumerator
: public vertex_decomposer
, public bf_base
,
1267 public ienumerator_base
{
1270 bfenumerator(Polyhedron
*P
, unsigned dim
, unsigned nbV
) :
1271 vertex_decomposer(P
, nbV
, *this),
1272 bf_base(dim
), ienumerator_base(dim
, this) {
1280 virtual void handle(const signed_cone
& sc
, barvinok_options
*options
);
1281 virtual void base(mat_ZZ
& factors
, bfc_vec
& v
);
1283 bfc_term_base
* new_bf_term(int len
) {
1284 bfe_term
* t
= new bfe_term(len
);
1288 virtual void set_factor(bfc_term_base
*t
, int k
, int change
) {
1289 bfe_term
* bfet
= static_cast<bfe_term
*>(t
);
1290 factor
= bfet
->factors
[k
];
1291 assert(factor
!= NULL
);
1292 bfet
->factors
[k
] = NULL
;
1294 emul(&mone
, factor
);
1297 virtual void set_factor(bfc_term_base
*t
, int k
, mpq_t
&q
, int change
) {
1298 bfe_term
* bfet
= static_cast<bfe_term
*>(t
);
1299 factor
= bfet
->factors
[k
];
1300 assert(factor
!= NULL
);
1301 bfet
->factors
[k
] = NULL
;
1307 value_oppose(f
.x
.n
, mpq_numref(q
));
1309 value_assign(f
.x
.n
, mpq_numref(q
));
1310 value_assign(f
.d
, mpq_denref(q
));
1312 free_evalue_refs(&f
);
1315 virtual void set_factor(bfc_term_base
*t
, int k
, const QQ
& c
, int change
) {
1316 bfe_term
* bfet
= static_cast<bfe_term
*>(t
);
1318 factor
= new evalue
;
1323 zz2value(c
.n
, f
.x
.n
);
1325 value_oppose(f
.x
.n
, f
.x
.n
);
1328 value_init(factor
->d
);
1329 evalue_copy(factor
, bfet
->factors
[k
]);
1331 free_evalue_refs(&f
);
1334 void set_factor(evalue
*f
, int change
) {
1340 virtual void insert_term(bfc_term_base
*t
, int i
) {
1341 bfe_term
* bfet
= static_cast<bfe_term
*>(t
);
1342 int len
= t
->terms
.NumRows()-1; // already increased by one
1344 bfet
->factors
.resize(len
+1);
1345 for (int j
= len
; j
> i
; --j
) {
1346 bfet
->factors
[j
] = bfet
->factors
[j
-1];
1347 t
->terms
[j
] = t
->terms
[j
-1];
1349 bfet
->factors
[i
] = factor
;
1353 virtual void update_term(bfc_term_base
*t
, int i
) {
1354 bfe_term
* bfet
= static_cast<bfe_term
*>(t
);
1356 eadd(factor
, bfet
->factors
[i
]);
1357 free_evalue_refs(factor
);
1361 virtual bool constant_vertex(int dim
) { return E_num(0, dim
) == 0; }
1363 virtual void cum(bf_reducer
*bfr
, bfc_term_base
*t
, int k
, dpoly_r
*r
,
1364 barvinok_options
*options
);
1367 enumerator_base
*enumerator_base::create(Polyhedron
*P
, unsigned dim
, unsigned nbV
,
1368 barvinok_options
*options
)
1370 enumerator_base
*eb
;
1372 if (options
->incremental_specialization
== BV_SPECIALIZATION_BF
)
1373 eb
= new bfenumerator(P
, dim
, nbV
);
1374 else if (options
->incremental_specialization
== BV_SPECIALIZATION_DF
)
1375 eb
= new ienumerator(P
, dim
, nbV
);
1377 eb
= new enumerator(P
, dim
, nbV
);
1382 struct bfe_cum
: public cumulator
{
1384 bfc_term_base
*told
;
1388 bfe_cum(evalue
*factor
, evalue
*v
, dpoly_r
*r
, bf_reducer
*bfr
,
1389 bfc_term_base
*t
, int k
, bfenumerator
*e
) :
1390 cumulator(factor
, v
, r
), told(t
), k(k
),
1394 virtual void add_term(const vector
<int>& powers
, evalue
*f2
);
1397 void bfe_cum::add_term(const vector
<int>& powers
, evalue
*f2
)
1399 bfr
->update_powers(powers
);
1401 bfc_term_base
* t
= bfe
->find_bfc_term(bfr
->vn
, bfr
->npowers
, bfr
->nnf
);
1402 bfe
->set_factor(f2
, bfr
->l_changes
% 2);
1403 bfe
->add_term(t
, told
->terms
[k
], bfr
->l_extra_num
);
1406 void bfenumerator::cum(bf_reducer
*bfr
, bfc_term_base
*t
, int k
,
1407 dpoly_r
*r
, barvinok_options
*options
)
1409 bfe_term
* bfet
= static_cast<bfe_term
*>(t
);
1410 bfe_cum
cum(bfet
->factors
[k
], E_num(0, bfr
->d
), r
, bfr
, t
, k
, this);
1411 cum
.cumulate(options
);
1414 void bfenumerator::base(mat_ZZ
& factors
, bfc_vec
& v
)
1416 for (int i
= 0; i
< v
.size(); ++i
) {
1417 assert(v
[i
]->terms
.NumRows() == 1);
1418 evalue
*factor
= static_cast<bfe_term
*>(v
[i
])->factors
[0];
1419 eadd(factor
, vE
[vert
]);
1424 void bfenumerator::handle(const signed_cone
& sc
, barvinok_options
*options
)
1426 assert(sc
.det
== 1);
1428 assert(sc
.rays
.NumRows() == enumerator_base::dim
);
1430 bfe_term
* t
= new bfe_term(enumerator_base::dim
);
1431 vector
< bfc_term_base
* > v
;
1434 t
->factors
.resize(1);
1436 t
->terms
.SetDims(1, enumerator_base::dim
);
1437 lattice_point(V
, sc
.rays
, t
->terms
[0], E_vertex
, options
);
1439 // the elements of factors are always lexpositive
1441 int s
= setup_factors(sc
.rays
, factors
, t
, sc
.sign
);
1443 t
->factors
[0] = new evalue
;
1444 value_init(t
->factors
[0]->d
);
1445 evalue_set_si(t
->factors
[0], s
, 1);
1446 reduce(factors
, v
, options
);
1448 for (int i
= 0; i
< enumerator_base::dim
; ++i
)
1450 free_evalue_refs(E_vertex
[i
]);
1455 static inline Param_Polyhedron
*Polyhedron2Param_MR(Polyhedron
*Din
,
1456 Polyhedron
*Cin
, int WS
)
1458 if (WS
& POL_NO_DUAL
)
1460 return Polyhedron2Param_Domain(Din
, Cin
, WS
);
1463 static evalue
* barvinok_enumerate_ev_f(Polyhedron
*P
, Polyhedron
* C
,
1464 barvinok_options
*options
);
1467 static evalue
* barvinok_enumerate_cst(Polyhedron
*P
, Polyhedron
* C
,
1468 struct barvinok_options
*options
)
1472 ALLOC(evalue
, eres
);
1473 value_init(eres
->d
);
1474 value_set_si(eres
->d
, 0);
1475 eres
->x
.p
= new_enode(partition
, 2, C
->Dimension
);
1476 EVALUE_SET_DOMAIN(eres
->x
.p
->arr
[0],
1477 DomainConstraintSimplify(C
, options
->MaxRays
));
1478 value_set_si(eres
->x
.p
->arr
[1].d
, 1);
1479 value_init(eres
->x
.p
->arr
[1].x
.n
);
1481 value_set_si(eres
->x
.p
->arr
[1].x
.n
, 0);
1483 barvinok_count_with_options(P
, &eres
->x
.p
->arr
[1].x
.n
, options
);
1489 static evalue
* enumerate(Polyhedron
*P
, Polyhedron
* C
,
1490 struct barvinok_options
*options
)
1492 //P = unfringe(P, MaxRays);
1494 Polyhedron
*Corig
= C
;
1495 Polyhedron
*CEq
= NULL
, *rVD
;
1497 unsigned nparam
= C
->Dimension
;
1502 value_init(factor
.d
);
1503 evalue_set_si(&factor
, 1, 1);
1506 POL_ENSURE_FACETS(P
);
1507 POL_ENSURE_VERTICES(P
);
1508 POL_ENSURE_FACETS(C
);
1509 POL_ENSURE_VERTICES(C
);
1511 if (C
->Dimension
== 0 || emptyQ(P
)) {
1513 eres
= barvinok_enumerate_cst(P
, CEq
? CEq
: Polyhedron_Copy(C
), options
);
1516 evalue_backsubstitute(eres
, CP
, options
->MaxRays
);
1520 emul(&factor
, eres
);
1521 if (options
->approximation_method
== BV_APPROX_DROP
) {
1522 if (options
->polynomial_approximation
== BV_APPROX_SIGN_UPPER
)
1523 evalue_frac2polynomial(eres
, 1, options
->MaxRays
);
1524 if (options
->polynomial_approximation
== BV_APPROX_SIGN_LOWER
)
1525 evalue_frac2polynomial(eres
, -1, options
->MaxRays
);
1526 if (options
->polynomial_approximation
== BV_APPROX_SIGN_APPROX
)
1527 evalue_frac2polynomial(eres
, 0, options
->MaxRays
);
1529 reduce_evalue(eres
);
1530 free_evalue_refs(&factor
);
1537 if (Polyhedron_is_unbounded(P
, nparam
, options
->MaxRays
))
1542 P
= remove_equalities_p(P
, P
->Dimension
-nparam
, &f
, options
->MaxRays
);
1543 mask(f
, &factor
, options
);
1546 if (P
->Dimension
== nparam
) {
1548 P
= Universe_Polyhedron(0);
1554 remove_all_equalities(&Q
, &C
, &CP
, NULL
, nparam
, options
->MaxRays
);
1555 if (C
!= D
&& D
!= Corig
)
1557 eres
= enumerate(Q
, C
, options
);
1561 Polyhedron
*T
= Polyhedron_Factor(P
, nparam
, NULL
, options
->MaxRays
);
1562 if (T
|| (P
->Dimension
== nparam
+1)) {
1565 for (Q
= T
? T
: P
; Q
; Q
= Q
->next
) {
1566 Polyhedron
*next
= Q
->next
;
1570 if (Q
->Dimension
!= C
->Dimension
)
1571 QC
= Polyhedron_Project(Q
, nparam
);
1574 C
= DomainIntersection(C
, QC
, options
->MaxRays
);
1576 Polyhedron_Free(C2
);
1578 Polyhedron_Free(QC
);
1586 if (T
->Dimension
== C
->Dimension
) {
1595 eres
= barvinok_enumerate_ev_f(P
, C
, options
);
1602 for (Q
= P
->next
; Q
; Q
= Q
->next
) {
1603 Polyhedron
*next
= Q
->next
;
1606 f
= barvinok_enumerate_ev_f(Q
, C
, options
);
1608 free_evalue_refs(f
);
1618 evalue
* barvinok_enumerate_with_options(Polyhedron
*P
, Polyhedron
* C
,
1619 struct barvinok_options
*options
)
1621 Polyhedron
*next
, *Cnext
, *CA
;
1622 Polyhedron
*Porig
= P
;
1627 "barvinok_enumerate: input is a union; only first polyhedron is enumerated\n");
1631 "barvinok_enumerate: context is a union; only first polyhedron is considered\n");
1635 CA
= align_context(C
, P
->Dimension
, options
->MaxRays
);
1638 P
= DomainIntersection(P
, CA
, options
->MaxRays
);
1640 Polyhedron_Free(CA
);
1642 eres
= enumerate(P
, C
, options
);
1649 evalue
* barvinok_enumerate_ev(Polyhedron
*P
, Polyhedron
* C
, unsigned MaxRays
)
1652 barvinok_options
*options
= barvinok_options_new_with_defaults();
1653 options
->MaxRays
= MaxRays
;
1654 E
= barvinok_enumerate_with_options(P
, C
, options
);
1655 barvinok_options_free(options
);
1659 evalue
*Param_Polyhedron_Enumerate(Param_Polyhedron
*PP
, Polyhedron
*P
,
1661 struct barvinok_options
*options
)
1665 unsigned nparam
= C
->Dimension
;
1666 unsigned dim
= P
->Dimension
- nparam
;
1668 ALLOC(evalue
, eres
);
1669 value_init(eres
->d
);
1670 value_set_si(eres
->d
, 0);
1673 for (nd
= 0, D
=PP
->D
; D
; ++nd
, D
=D
->next
);
1674 struct section
{ Polyhedron
*D
; evalue E
; };
1675 section
*s
= new section
[nd
];
1677 enumerator_base
*et
= NULL
;
1682 et
= enumerator_base::create(P
, dim
, PP
->nbV
, options
);
1684 Polyhedron
*TC
= true_context(P
, C
, options
->MaxRays
);
1685 FORALL_REDUCED_DOMAIN(PP
, TC
, nd
, options
, i
, D
, rVD
)
1688 value_init(s
[i
].E
.d
);
1689 evalue_set_si(&s
[i
].E
, 0, 1);
1692 FORALL_PVertex_in_ParamPolyhedron(V
,D
,PP
) // _i is internal counter
1695 et
->decompose_at(V
, _i
, options
);
1696 } catch (OrthogonalException
&e
) {
1697 FORALL_REDUCED_DOMAIN_RESET
;
1698 for (; i
>= 0; --i
) {
1699 free_evalue_refs(&s
[i
].E
);
1700 Domain_Free(s
[i
].D
);
1704 eadd(et
->vE
[_i
] , &s
[i
].E
);
1705 END_FORALL_PVertex_in_ParamPolyhedron
;
1706 evalue_range_reduction_in_domain(&s
[i
].E
, rVD
);
1707 END_FORALL_REDUCED_DOMAIN
1708 Polyhedron_Free(TC
);
1712 evalue_set_si(eres
, 0, 1);
1714 eres
->x
.p
= new_enode(partition
, 2*nd
, C
->Dimension
);
1715 for (int j
= 0; j
< nd
; ++j
) {
1716 EVALUE_SET_DOMAIN(eres
->x
.p
->arr
[2*j
], s
[j
].D
);
1717 value_clear(eres
->x
.p
->arr
[2*j
+1].d
);
1718 eres
->x
.p
->arr
[2*j
+1] = s
[j
].E
;
1726 static evalue
* barvinok_enumerate_ev_f(Polyhedron
*P
, Polyhedron
* C
,
1727 barvinok_options
*options
)
1729 unsigned nparam
= C
->Dimension
;
1730 bool do_scale
= options
->approximation_method
== BV_APPROX_SCALE
;
1732 if (options
->approximation_method
== BV_APPROX_VOLUME
)
1733 return Param_Polyhedron_Volume(P
, C
, options
);
1735 if (P
->Dimension
- nparam
== 1 && !do_scale
)
1736 return ParamLine_Length(P
, C
, options
);
1738 Param_Polyhedron
*PP
= NULL
;
1742 eres
= scale_bound(P
, C
, options
);
1747 PP
= Polyhedron2Param_MR(P
, C
, options
->MaxRays
);
1750 eres
= scale(PP
, P
, C
, options
);
1752 eres
= Param_Polyhedron_Enumerate(PP
, P
, C
, options
);
1755 Param_Polyhedron_Free(PP
);
1760 Enumeration
* barvinok_enumerate(Polyhedron
*P
, Polyhedron
* C
, unsigned MaxRays
)
1762 evalue
*EP
= barvinok_enumerate_ev(P
, C
, MaxRays
);
1764 return partition2enumeration(EP
);
1767 static void SwapColumns(Value
**V
, int n
, int i
, int j
)
1769 for (int r
= 0; r
< n
; ++r
)
1770 value_swap(V
[r
][i
], V
[r
][j
]);
1773 static void SwapColumns(Polyhedron
*P
, int i
, int j
)
1775 SwapColumns(P
->Constraint
, P
->NbConstraints
, i
, j
);
1776 SwapColumns(P
->Ray
, P
->NbRays
, i
, j
);
1779 /* Construct a constraint c from constraints l and u such that if
1780 * if constraint c holds then for each value of the other variables
1781 * there is at most one value of variable pos (position pos+1 in the constraints).
1783 * Given a lower and an upper bound
1784 * n_l v_i + <c_l,x> + c_l >= 0
1785 * -n_u v_i + <c_u,x> + c_u >= 0
1786 * the constructed constraint is
1788 * -(n_l<c_u,x> + n_u<c_l,x>) + (-n_l c_u - n_u c_l + n_l n_u - 1)
1790 * which is then simplified to remove the content of the non-constant coefficients
1792 * len is the total length of the constraints.
1793 * v is a temporary variable that can be used by this procedure
1795 static void negative_test_constraint(Value
*l
, Value
*u
, Value
*c
, int pos
,
1798 value_oppose(*v
, u
[pos
+1]);
1799 Vector_Combine(l
+1, u
+1, c
+1, *v
, l
[pos
+1], len
-1);
1800 value_multiply(*v
, *v
, l
[pos
+1]);
1801 value_subtract(c
[len
-1], c
[len
-1], *v
);
1802 value_set_si(*v
, -1);
1803 Vector_Scale(c
+1, c
+1, *v
, len
-1);
1804 value_decrement(c
[len
-1], c
[len
-1]);
1805 ConstraintSimplify(c
, c
, len
, v
);
1808 static bool parallel_constraints(Value
*l
, Value
*u
, Value
*c
, int pos
,
1817 Vector_Gcd(&l
[1+pos
], len
, &g1
);
1818 Vector_Gcd(&u
[1+pos
], len
, &g2
);
1819 Vector_Combine(l
+1+pos
, u
+1+pos
, c
+1, g2
, g1
, len
);
1820 parallel
= First_Non_Zero(c
+1, len
) == -1;
1828 static void negative_test_constraint7(Value
*l
, Value
*u
, Value
*c
, int pos
,
1829 int exist
, int len
, Value
*v
)
1834 Vector_Gcd(&u
[1+pos
], exist
, v
);
1835 Vector_Gcd(&l
[1+pos
], exist
, &g
);
1836 Vector_Combine(l
+1, u
+1, c
+1, *v
, g
, len
-1);
1837 value_multiply(*v
, *v
, g
);
1838 value_subtract(c
[len
-1], c
[len
-1], *v
);
1839 value_set_si(*v
, -1);
1840 Vector_Scale(c
+1, c
+1, *v
, len
-1);
1841 value_decrement(c
[len
-1], c
[len
-1]);
1842 ConstraintSimplify(c
, c
, len
, v
);
1847 /* Turns a x + b >= 0 into a x + b <= -1
1849 * len is the total length of the constraint.
1850 * v is a temporary variable that can be used by this procedure
1852 static void oppose_constraint(Value
*c
, int len
, Value
*v
)
1854 value_set_si(*v
, -1);
1855 Vector_Scale(c
+1, c
+1, *v
, len
-1);
1856 value_decrement(c
[len
-1], c
[len
-1]);
1859 /* Split polyhedron P into two polyhedra *pos and *neg, where
1860 * existential variable i has at most one solution for each
1861 * value of the other variables in *neg.
1863 * The splitting is performed using constraints l and u.
1865 * nvar: number of set variables
1866 * row: temporary vector that can be used by this procedure
1867 * f: temporary value that can be used by this procedure
1869 static bool SplitOnConstraint(Polyhedron
*P
, int i
, int l
, int u
,
1870 int nvar
, int MaxRays
, Vector
*row
, Value
& f
,
1871 Polyhedron
**pos
, Polyhedron
**neg
)
1873 negative_test_constraint(P
->Constraint
[l
], P
->Constraint
[u
],
1874 row
->p
, nvar
+i
, P
->Dimension
+2, &f
);
1875 *neg
= AddConstraints(row
->p
, 1, P
, MaxRays
);
1877 /* We found an independent, but useless constraint
1878 * Maybe we should detect this earlier and not
1879 * mark the variable as INDEPENDENT
1881 if (emptyQ((*neg
))) {
1882 Polyhedron_Free(*neg
);
1886 oppose_constraint(row
->p
, P
->Dimension
+2, &f
);
1887 *pos
= AddConstraints(row
->p
, 1, P
, MaxRays
);
1889 if (emptyQ((*pos
))) {
1890 Polyhedron_Free(*neg
);
1891 Polyhedron_Free(*pos
);
1899 * unimodularly transform P such that constraint r is transformed
1900 * into a constraint that involves only a single (the first)
1901 * existential variable
1904 static Polyhedron
*rotate_along(Polyhedron
*P
, int r
, int nvar
, int exist
,
1910 Matrix
*M
= Matrix_Alloc(exist
, exist
);
1911 Vector_Copy(P
->Constraint
[r
]+1+nvar
, M
->p
[0], exist
);
1912 Vector_Gcd(M
->p
[0], exist
, &g
);
1913 if (value_notone_p(g
))
1914 Vector_AntiScale(M
->p
[0], M
->p
[0], g
, exist
);
1917 int ok
= unimodular_complete(M
, 1);
1919 Matrix
*M2
= Matrix_Alloc(P
->Dimension
+1, P
->Dimension
+1);
1920 for (r
= 0; r
< nvar
; ++r
)
1921 value_set_si(M2
->p
[r
][r
], 1);
1922 for ( ; r
< nvar
+exist
; ++r
)
1923 Vector_Copy(M
->p
[r
-nvar
], M2
->p
[r
]+nvar
, exist
);
1924 for ( ; r
< P
->Dimension
+1; ++r
)
1925 value_set_si(M2
->p
[r
][r
], 1);
1926 Polyhedron
*T
= Polyhedron_Image(P
, M2
, MaxRays
);
1934 /* Split polyhedron P into two polyhedra *pos and *neg, where
1935 * existential variable i has at most one solution for each
1936 * value of the other variables in *neg.
1938 * If independent is set, then the two constraints on which the
1939 * split will be performed need to be independent of the other
1940 * existential variables.
1942 * Return true if an appropriate split could be performed.
1944 * nvar: number of set variables
1945 * exist: number of existential variables
1946 * row: temporary vector that can be used by this procedure
1947 * f: temporary value that can be used by this procedure
1949 static bool SplitOnVar(Polyhedron
*P
, int i
,
1950 int nvar
, int exist
, int MaxRays
,
1951 Vector
*row
, Value
& f
, bool independent
,
1952 Polyhedron
**pos
, Polyhedron
**neg
)
1956 for (int l
= P
->NbEq
; l
< P
->NbConstraints
; ++l
) {
1957 if (value_negz_p(P
->Constraint
[l
][nvar
+i
+1]))
1961 for (j
= 0; j
< exist
; ++j
)
1962 if (j
!= i
&& value_notzero_p(P
->Constraint
[l
][nvar
+j
+1]))
1968 for (int u
= P
->NbEq
; u
< P
->NbConstraints
; ++u
) {
1969 if (value_posz_p(P
->Constraint
[u
][nvar
+i
+1]))
1973 for (j
= 0; j
< exist
; ++j
)
1974 if (j
!= i
&& value_notzero_p(P
->Constraint
[u
][nvar
+j
+1]))
1980 if (SplitOnConstraint(P
, i
, l
, u
, nvar
, MaxRays
, row
, f
, pos
, neg
)) {
1983 SwapColumns(*neg
, nvar
+1, nvar
+1+i
);
1993 static bool double_bound_pair(Polyhedron
*P
, int nvar
, int exist
,
1994 int i
, int l1
, int l2
,
1995 Polyhedron
**pos
, Polyhedron
**neg
)
1999 Vector
*row
= Vector_Alloc(P
->Dimension
+2);
2000 value_set_si(row
->p
[0], 1);
2001 value_oppose(f
, P
->Constraint
[l1
][nvar
+i
+1]);
2002 Vector_Combine(P
->Constraint
[l1
]+1, P
->Constraint
[l2
]+1,
2004 P
->Constraint
[l2
][nvar
+i
+1], f
,
2006 ConstraintSimplify(row
->p
, row
->p
, P
->Dimension
+2, &f
);
2007 *pos
= AddConstraints(row
->p
, 1, P
, 0);
2008 value_set_si(f
, -1);
2009 Vector_Scale(row
->p
+1, row
->p
+1, f
, P
->Dimension
+1);
2010 value_decrement(row
->p
[P
->Dimension
+1], row
->p
[P
->Dimension
+1]);
2011 *neg
= AddConstraints(row
->p
, 1, P
, 0);
2015 return !emptyQ((*pos
)) && !emptyQ((*neg
));
2018 static bool double_bound(Polyhedron
*P
, int nvar
, int exist
,
2019 Polyhedron
**pos
, Polyhedron
**neg
)
2021 for (int i
= 0; i
< exist
; ++i
) {
2023 for (l1
= P
->NbEq
; l1
< P
->NbConstraints
; ++l1
) {
2024 if (value_negz_p(P
->Constraint
[l1
][nvar
+i
+1]))
2026 for (l2
= l1
+ 1; l2
< P
->NbConstraints
; ++l2
) {
2027 if (value_negz_p(P
->Constraint
[l2
][nvar
+i
+1]))
2029 if (double_bound_pair(P
, nvar
, exist
, i
, l1
, l2
, pos
, neg
))
2033 for (l1
= P
->NbEq
; l1
< P
->NbConstraints
; ++l1
) {
2034 if (value_posz_p(P
->Constraint
[l1
][nvar
+i
+1]))
2036 if (l1
< P
->NbConstraints
)
2037 for (l2
= l1
+ 1; l2
< P
->NbConstraints
; ++l2
) {
2038 if (value_posz_p(P
->Constraint
[l2
][nvar
+i
+1]))
2040 if (double_bound_pair(P
, nvar
, exist
, i
, l1
, l2
, pos
, neg
))
2052 INDEPENDENT
= 1 << 2,
2056 static evalue
* enumerate_or(Polyhedron
*D
,
2057 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2060 fprintf(stderr
, "\nER: Or\n");
2061 #endif /* DEBUG_ER */
2063 Polyhedron
*N
= D
->next
;
2066 barvinok_enumerate_e_with_options(D
, exist
, nparam
, options
);
2069 for (D
= N
; D
; D
= N
) {
2074 barvinok_enumerate_e_with_options(D
, exist
, nparam
, options
);
2077 free_evalue_refs(EN
);
2087 static evalue
* enumerate_sum(Polyhedron
*P
,
2088 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2090 int nvar
= P
->Dimension
- exist
- nparam
;
2091 int toswap
= nvar
< exist
? nvar
: exist
;
2092 for (int i
= 0; i
< toswap
; ++i
)
2093 SwapColumns(P
, 1 + i
, nvar
+exist
- i
);
2097 fprintf(stderr
, "\nER: Sum\n");
2098 #endif /* DEBUG_ER */
2100 evalue
*EP
= barvinok_enumerate_e_with_options(P
, exist
, nparam
, options
);
2102 evalue_split_domains_into_orthants(EP
, options
->MaxRays
);
2104 evalue_range_reduction(EP
);
2106 evalue_frac2floor2(EP
, 1);
2108 evalue
*sum
= esum(EP
, nvar
);
2110 free_evalue_refs(EP
);
2114 evalue_range_reduction(EP
);
2119 static evalue
* split_sure(Polyhedron
*P
, Polyhedron
*S
,
2120 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2122 int nvar
= P
->Dimension
- exist
- nparam
;
2124 Matrix
*M
= Matrix_Alloc(exist
, S
->Dimension
+2);
2125 for (int i
= 0; i
< exist
; ++i
)
2126 value_set_si(M
->p
[i
][nvar
+i
+1], 1);
2128 S
= DomainAddRays(S
, M
, options
->MaxRays
);
2130 Polyhedron
*F
= DomainAddRays(P
, M
, options
->MaxRays
);
2131 Polyhedron
*D
= DomainDifference(F
, S
, options
->MaxRays
);
2133 D
= Disjoint_Domain(D
, 0, options
->MaxRays
);
2138 M
= Matrix_Alloc(P
->Dimension
+1-exist
, P
->Dimension
+1);
2139 for (int j
= 0; j
< nvar
; ++j
)
2140 value_set_si(M
->p
[j
][j
], 1);
2141 for (int j
= 0; j
< nparam
+1; ++j
)
2142 value_set_si(M
->p
[nvar
+j
][nvar
+exist
+j
], 1);
2143 Polyhedron
*T
= Polyhedron_Image(S
, M
, options
->MaxRays
);
2144 evalue
*EP
= barvinok_enumerate_e_with_options(T
, 0, nparam
, options
);
2149 for (Polyhedron
*Q
= D
; Q
; Q
= Q
->next
) {
2150 Polyhedron
*N
= Q
->next
;
2152 T
= DomainIntersection(P
, Q
, options
->MaxRays
);
2153 evalue
*E
= barvinok_enumerate_e_with_options(T
, exist
, nparam
, options
);
2155 free_evalue_refs(E
);
2164 static evalue
* enumerate_sure(Polyhedron
*P
,
2165 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2169 int nvar
= P
->Dimension
- exist
- nparam
;
2175 for (i
= 0; i
< exist
; ++i
) {
2176 Matrix
*M
= Matrix_Alloc(S
->NbConstraints
, S
->Dimension
+2);
2178 value_set_si(lcm
, 1);
2179 for (int j
= 0; j
< S
->NbConstraints
; ++j
) {
2180 if (value_negz_p(S
->Constraint
[j
][1+nvar
+i
]))
2182 if (value_one_p(S
->Constraint
[j
][1+nvar
+i
]))
2184 value_lcm(lcm
, S
->Constraint
[j
][1+nvar
+i
], &lcm
);
2187 for (int j
= 0; j
< S
->NbConstraints
; ++j
) {
2188 if (value_negz_p(S
->Constraint
[j
][1+nvar
+i
]))
2190 if (value_one_p(S
->Constraint
[j
][1+nvar
+i
]))
2192 value_division(f
, lcm
, S
->Constraint
[j
][1+nvar
+i
]);
2193 Vector_Scale(S
->Constraint
[j
], M
->p
[c
], f
, S
->Dimension
+2);
2194 value_subtract(M
->p
[c
][S
->Dimension
+1],
2195 M
->p
[c
][S
->Dimension
+1],
2197 value_increment(M
->p
[c
][S
->Dimension
+1],
2198 M
->p
[c
][S
->Dimension
+1]);
2202 S
= AddConstraints(M
->p
[0], c
, S
, options
->MaxRays
);
2217 fprintf(stderr
, "\nER: Sure\n");
2218 #endif /* DEBUG_ER */
2220 return split_sure(P
, S
, exist
, nparam
, options
);
2223 static evalue
* enumerate_sure2(Polyhedron
*P
,
2224 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2226 int nvar
= P
->Dimension
- exist
- nparam
;
2228 for (r
= 0; r
< P
->NbRays
; ++r
)
2229 if (value_one_p(P
->Ray
[r
][0]) &&
2230 value_one_p(P
->Ray
[r
][P
->Dimension
+1]))
2236 Matrix
*M
= Matrix_Alloc(nvar
+ 1 + nparam
, P
->Dimension
+2);
2237 for (int i
= 0; i
< nvar
; ++i
)
2238 value_set_si(M
->p
[i
][1+i
], 1);
2239 for (int i
= 0; i
< nparam
; ++i
)
2240 value_set_si(M
->p
[i
+nvar
][1+nvar
+exist
+i
], 1);
2241 Vector_Copy(P
->Ray
[r
]+1+nvar
, M
->p
[nvar
+nparam
]+1+nvar
, exist
);
2242 value_set_si(M
->p
[nvar
+nparam
][0], 1);
2243 value_set_si(M
->p
[nvar
+nparam
][P
->Dimension
+1], 1);
2244 Polyhedron
* F
= Rays2Polyhedron(M
, options
->MaxRays
);
2247 Polyhedron
*I
= DomainIntersection(F
, P
, options
->MaxRays
);
2251 fprintf(stderr
, "\nER: Sure2\n");
2252 #endif /* DEBUG_ER */
2254 return split_sure(P
, I
, exist
, nparam
, options
);
2257 static evalue
* enumerate_cyclic(Polyhedron
*P
,
2258 unsigned exist
, unsigned nparam
,
2259 evalue
* EP
, int r
, int p
, unsigned MaxRays
)
2261 int nvar
= P
->Dimension
- exist
- nparam
;
2263 /* If EP in its fractional maps only contains references
2264 * to the remainder parameter with appropriate coefficients
2265 * then we could in principle avoid adding existentially
2266 * quantified variables to the validity domains.
2267 * We'd have to replace the remainder by m { p/m }
2268 * and multiply with an appropriate factor that is one
2269 * only in the appropriate range.
2270 * This last multiplication can be avoided if EP
2271 * has a single validity domain with no (further)
2272 * constraints on the remainder parameter
2275 Matrix
*CT
= Matrix_Alloc(nparam
+1, nparam
+3);
2276 Matrix
*M
= Matrix_Alloc(1, 1+nparam
+3);
2277 for (int j
= 0; j
< nparam
; ++j
)
2279 value_set_si(CT
->p
[j
][j
], 1);
2280 value_set_si(CT
->p
[p
][nparam
+1], 1);
2281 value_set_si(CT
->p
[nparam
][nparam
+2], 1);
2282 value_set_si(M
->p
[0][1+p
], -1);
2283 value_absolute(M
->p
[0][1+nparam
], P
->Ray
[0][1+nvar
+exist
+p
]);
2284 value_set_si(M
->p
[0][1+nparam
+1], 1);
2285 Polyhedron
*CEq
= Constraints2Polyhedron(M
, 1);
2287 addeliminatedparams_enum(EP
, CT
, CEq
, MaxRays
, nparam
);
2288 Polyhedron_Free(CEq
);
2294 static void enumerate_vd_add_ray(evalue
*EP
, Matrix
*Rays
, unsigned MaxRays
)
2296 if (value_notzero_p(EP
->d
))
2299 assert(EP
->x
.p
->type
== partition
);
2300 assert(EP
->x
.p
->pos
== EVALUE_DOMAIN(EP
->x
.p
->arr
[0])->Dimension
);
2301 for (int i
= 0; i
< EP
->x
.p
->size
/2; ++i
) {
2302 Polyhedron
*D
= EVALUE_DOMAIN(EP
->x
.p
->arr
[2*i
]);
2303 Polyhedron
*N
= DomainAddRays(D
, Rays
, MaxRays
);
2304 EVALUE_SET_DOMAIN(EP
->x
.p
->arr
[2*i
], N
);
2309 static evalue
* enumerate_line(Polyhedron
*P
,
2310 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2316 fprintf(stderr
, "\nER: Line\n");
2317 #endif /* DEBUG_ER */
2319 int nvar
= P
->Dimension
- exist
- nparam
;
2321 for (i
= 0; i
< nparam
; ++i
)
2322 if (value_notzero_p(P
->Ray
[0][1+nvar
+exist
+i
]))
2325 for (j
= i
+1; j
< nparam
; ++j
)
2326 if (value_notzero_p(P
->Ray
[0][1+nvar
+exist
+i
]))
2328 assert(j
>= nparam
); // for now
2330 Matrix
*M
= Matrix_Alloc(2, P
->Dimension
+2);
2331 value_set_si(M
->p
[0][0], 1);
2332 value_set_si(M
->p
[0][1+nvar
+exist
+i
], 1);
2333 value_set_si(M
->p
[1][0], 1);
2334 value_set_si(M
->p
[1][1+nvar
+exist
+i
], -1);
2335 value_absolute(M
->p
[1][1+P
->Dimension
], P
->Ray
[0][1+nvar
+exist
+i
]);
2336 value_decrement(M
->p
[1][1+P
->Dimension
], M
->p
[1][1+P
->Dimension
]);
2337 Polyhedron
*S
= AddConstraints(M
->p
[0], 2, P
, options
->MaxRays
);
2338 evalue
*EP
= barvinok_enumerate_e_with_options(S
, exist
, nparam
, options
);
2342 return enumerate_cyclic(P
, exist
, nparam
, EP
, 0, i
, options
->MaxRays
);
2345 static int single_param_pos(Polyhedron
*P
, unsigned exist
, unsigned nparam
,
2348 int nvar
= P
->Dimension
- exist
- nparam
;
2349 if (First_Non_Zero(P
->Ray
[r
]+1, nvar
) != -1)
2351 int i
= First_Non_Zero(P
->Ray
[r
]+1+nvar
+exist
, nparam
);
2354 if (First_Non_Zero(P
->Ray
[r
]+1+nvar
+exist
+1, nparam
-i
-1) != -1)
2359 static evalue
* enumerate_remove_ray(Polyhedron
*P
, int r
,
2360 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2363 fprintf(stderr
, "\nER: RedundantRay\n");
2364 #endif /* DEBUG_ER */
2368 value_set_si(one
, 1);
2369 int len
= P
->NbRays
-1;
2370 Matrix
*M
= Matrix_Alloc(2 * len
, P
->Dimension
+2);
2371 Vector_Copy(P
->Ray
[0], M
->p
[0], r
* (P
->Dimension
+2));
2372 Vector_Copy(P
->Ray
[r
+1], M
->p
[r
], (len
-r
) * (P
->Dimension
+2));
2373 for (int j
= 0; j
< P
->NbRays
; ++j
) {
2376 Vector_Combine(P
->Ray
[j
], P
->Ray
[r
], M
->p
[len
+j
-(j
>r
)],
2377 one
, P
->Ray
[j
][P
->Dimension
+1], P
->Dimension
+2);
2380 P
= Rays2Polyhedron(M
, options
->MaxRays
);
2382 evalue
*EP
= barvinok_enumerate_e_with_options(P
, exist
, nparam
, options
);
2389 static evalue
* enumerate_redundant_ray(Polyhedron
*P
,
2390 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2392 assert(P
->NbBid
== 0);
2393 int nvar
= P
->Dimension
- exist
- nparam
;
2397 for (int r
= 0; r
< P
->NbRays
; ++r
) {
2398 if (value_notzero_p(P
->Ray
[r
][P
->Dimension
+1]))
2400 int i1
= single_param_pos(P
, exist
, nparam
, r
);
2403 for (int r2
= r
+1; r2
< P
->NbRays
; ++r2
) {
2404 if (value_notzero_p(P
->Ray
[r2
][P
->Dimension
+1]))
2406 int i2
= single_param_pos(P
, exist
, nparam
, r2
);
2412 value_division(m
, P
->Ray
[r
][1+nvar
+exist
+i1
],
2413 P
->Ray
[r2
][1+nvar
+exist
+i1
]);
2414 value_multiply(m
, m
, P
->Ray
[r2
][1+nvar
+exist
+i1
]);
2415 /* r2 divides r => r redundant */
2416 if (value_eq(m
, P
->Ray
[r
][1+nvar
+exist
+i1
])) {
2418 return enumerate_remove_ray(P
, r
, exist
, nparam
, options
);
2421 value_division(m
, P
->Ray
[r2
][1+nvar
+exist
+i1
],
2422 P
->Ray
[r
][1+nvar
+exist
+i1
]);
2423 value_multiply(m
, m
, P
->Ray
[r
][1+nvar
+exist
+i1
]);
2424 /* r divides r2 => r2 redundant */
2425 if (value_eq(m
, P
->Ray
[r2
][1+nvar
+exist
+i1
])) {
2427 return enumerate_remove_ray(P
, r2
, exist
, nparam
, options
);
2435 static Polyhedron
*upper_bound(Polyhedron
*P
,
2436 int pos
, Value
*max
, Polyhedron
**R
)
2445 for (Polyhedron
*Q
= P
; Q
; Q
= N
) {
2447 for (r
= 0; r
< P
->NbRays
; ++r
) {
2448 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1]) &&
2449 value_pos_p(P
->Ray
[r
][1+pos
]))
2452 if (r
< P
->NbRays
) {
2460 for (r
= 0; r
< P
->NbRays
; ++r
) {
2461 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1]))
2463 mpz_fdiv_q(v
, P
->Ray
[r
][1+pos
], P
->Ray
[r
][1+P
->Dimension
]);
2464 if ((!Q
->next
&& r
== 0) || value_gt(v
, *max
))
2465 value_assign(*max
, v
);
2472 static evalue
* enumerate_ray(Polyhedron
*P
,
2473 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2475 assert(P
->NbBid
== 0);
2476 int nvar
= P
->Dimension
- exist
- nparam
;
2479 for (r
= 0; r
< P
->NbRays
; ++r
)
2480 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1]))
2486 for (r2
= r
+1; r2
< P
->NbRays
; ++r2
)
2487 if (value_zero_p(P
->Ray
[r2
][P
->Dimension
+1]))
2489 if (r2
< P
->NbRays
) {
2491 return enumerate_sum(P
, exist
, nparam
, options
);
2495 fprintf(stderr
, "\nER: Ray\n");
2496 #endif /* DEBUG_ER */
2502 value_set_si(one
, 1);
2503 int i
= single_param_pos(P
, exist
, nparam
, r
);
2504 assert(i
!= -1); // for now;
2506 Matrix
*M
= Matrix_Alloc(P
->NbRays
, P
->Dimension
+2);
2507 for (int j
= 0; j
< P
->NbRays
; ++j
) {
2508 Vector_Combine(P
->Ray
[j
], P
->Ray
[r
], M
->p
[j
],
2509 one
, P
->Ray
[j
][P
->Dimension
+1], P
->Dimension
+2);
2511 Polyhedron
*S
= Rays2Polyhedron(M
, options
->MaxRays
);
2513 Polyhedron
*D
= DomainDifference(P
, S
, options
->MaxRays
);
2515 // Polyhedron_Print(stderr, P_VALUE_FMT, D);
2516 assert(value_pos_p(P
->Ray
[r
][1+nvar
+exist
+i
])); // for now
2518 D
= upper_bound(D
, nvar
+exist
+i
, &m
, &R
);
2522 M
= Matrix_Alloc(2, P
->Dimension
+2);
2523 value_set_si(M
->p
[0][0], 1);
2524 value_set_si(M
->p
[1][0], 1);
2525 value_set_si(M
->p
[0][1+nvar
+exist
+i
], -1);
2526 value_set_si(M
->p
[1][1+nvar
+exist
+i
], 1);
2527 value_assign(M
->p
[0][1+P
->Dimension
], m
);
2528 value_oppose(M
->p
[1][1+P
->Dimension
], m
);
2529 value_addto(M
->p
[1][1+P
->Dimension
], M
->p
[1][1+P
->Dimension
],
2530 P
->Ray
[r
][1+nvar
+exist
+i
]);
2531 value_decrement(M
->p
[1][1+P
->Dimension
], M
->p
[1][1+P
->Dimension
]);
2532 // Matrix_Print(stderr, P_VALUE_FMT, M);
2533 D
= AddConstraints(M
->p
[0], 2, P
, options
->MaxRays
);
2534 // Polyhedron_Print(stderr, P_VALUE_FMT, D);
2535 value_subtract(M
->p
[0][1+P
->Dimension
], M
->p
[0][1+P
->Dimension
],
2536 P
->Ray
[r
][1+nvar
+exist
+i
]);
2537 // Matrix_Print(stderr, P_VALUE_FMT, M);
2538 S
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
2539 // Polyhedron_Print(stderr, P_VALUE_FMT, S);
2542 evalue
*EP
= barvinok_enumerate_e_with_options(D
, exist
, nparam
, options
);
2547 if (value_notone_p(P
->Ray
[r
][1+nvar
+exist
+i
]))
2548 EP
= enumerate_cyclic(P
, exist
, nparam
, EP
, r
, i
, options
->MaxRays
);
2550 M
= Matrix_Alloc(1, nparam
+2);
2551 value_set_si(M
->p
[0][0], 1);
2552 value_set_si(M
->p
[0][1+i
], 1);
2553 enumerate_vd_add_ray(EP
, M
, options
->MaxRays
);
2558 evalue
*E
= barvinok_enumerate_e_with_options(S
, exist
, nparam
, options
);
2560 free_evalue_refs(E
);
2567 evalue
*ER
= enumerate_or(R
, exist
, nparam
, options
);
2569 free_evalue_refs(ER
);
2576 static evalue
* enumerate_vd(Polyhedron
**PA
,
2577 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2579 Polyhedron
*P
= *PA
;
2580 int nvar
= P
->Dimension
- exist
- nparam
;
2581 Param_Polyhedron
*PP
= NULL
;
2582 Polyhedron
*C
= Universe_Polyhedron(nparam
);
2586 PP
= Polyhedron2Param_Domain(PR
,C
, options
->MaxRays
);
2590 Param_Domain
*D
, *last
;
2593 for (nd
= 0, D
=PP
->D
; D
; D
=D
->next
, ++nd
)
2596 Polyhedron
**VD
= new Polyhedron_p
[nd
];
2597 Polyhedron
*TC
= true_context(P
, C
, options
->MaxRays
);
2598 FORALL_REDUCED_DOMAIN(PP
, TC
, nd
, options
, i
, D
, rVD
)
2601 END_FORALL_REDUCED_DOMAIN
2602 Polyhedron_Free(TC
);
2609 /* This doesn't seem to have any effect */
2611 Polyhedron
*CA
= align_context(VD
[0], P
->Dimension
, options
->MaxRays
);
2613 P
= DomainIntersection(P
, CA
, options
->MaxRays
);
2616 Polyhedron_Free(CA
);
2622 Polyhedron_Free(PR
);
2625 if (!EP
&& nd
> 1) {
2627 fprintf(stderr
, "\nER: VD\n");
2628 #endif /* DEBUG_ER */
2629 for (int i
= 0; i
< nd
; ++i
) {
2630 Polyhedron
*CA
= align_context(VD
[i
], P
->Dimension
, options
->MaxRays
);
2631 Polyhedron
*I
= DomainIntersection(P
, CA
, options
->MaxRays
);
2634 EP
= barvinok_enumerate_e_with_options(I
, exist
, nparam
, options
);
2636 evalue
*E
= barvinok_enumerate_e_with_options(I
, exist
, nparam
,
2639 free_evalue_refs(E
);
2643 Polyhedron_Free(CA
);
2647 for (int i
= 0; i
< nd
; ++i
)
2648 Polyhedron_Free(VD
[i
]);
2652 if (!EP
&& nvar
== 0) {
2655 Param_Vertices
*V
, *V2
;
2656 Matrix
* M
= Matrix_Alloc(1, P
->Dimension
+2);
2658 FORALL_PVertex_in_ParamPolyhedron(V
, last
, PP
) {
2660 FORALL_PVertex_in_ParamPolyhedron(V2
, last
, PP
) {
2667 for (int i
= 0; i
< exist
; ++i
) {
2668 value_oppose(f
, V
->Vertex
->p
[i
][nparam
+1]);
2669 Vector_Combine(V
->Vertex
->p
[i
],
2671 M
->p
[0] + 1 + nvar
+ exist
,
2672 V2
->Vertex
->p
[i
][nparam
+1],
2676 for (j
= 0; j
< nparam
; ++j
)
2677 if (value_notzero_p(M
->p
[0][1+nvar
+exist
+j
]))
2681 ConstraintSimplify(M
->p
[0], M
->p
[0],
2682 P
->Dimension
+2, &f
);
2683 value_set_si(M
->p
[0][0], 0);
2684 Polyhedron
*para
= AddConstraints(M
->p
[0], 1, P
,
2687 Polyhedron_Free(para
);
2690 Polyhedron
*pos
, *neg
;
2691 value_set_si(M
->p
[0][0], 1);
2692 value_decrement(M
->p
[0][P
->Dimension
+1],
2693 M
->p
[0][P
->Dimension
+1]);
2694 neg
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
2695 value_set_si(f
, -1);
2696 Vector_Scale(M
->p
[0]+1, M
->p
[0]+1, f
,
2698 value_decrement(M
->p
[0][P
->Dimension
+1],
2699 M
->p
[0][P
->Dimension
+1]);
2700 value_decrement(M
->p
[0][P
->Dimension
+1],
2701 M
->p
[0][P
->Dimension
+1]);
2702 pos
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
2703 if (emptyQ(neg
) && emptyQ(pos
)) {
2704 Polyhedron_Free(para
);
2705 Polyhedron_Free(pos
);
2706 Polyhedron_Free(neg
);
2710 fprintf(stderr
, "\nER: Order\n");
2711 #endif /* DEBUG_ER */
2712 EP
= barvinok_enumerate_e_with_options(para
, exist
, nparam
,
2716 E
= barvinok_enumerate_e_with_options(pos
, exist
, nparam
,
2719 free_evalue_refs(E
);
2723 E
= barvinok_enumerate_e_with_options(neg
, exist
, nparam
,
2726 free_evalue_refs(E
);
2729 Polyhedron_Free(para
);
2730 Polyhedron_Free(pos
);
2731 Polyhedron_Free(neg
);
2736 } END_FORALL_PVertex_in_ParamPolyhedron
;
2739 } END_FORALL_PVertex_in_ParamPolyhedron
;
2742 /* Search for vertex coordinate to split on */
2743 /* First look for one independent of the parameters */
2744 FORALL_PVertex_in_ParamPolyhedron(V
, last
, PP
) {
2745 for (int i
= 0; i
< exist
; ++i
) {
2747 for (j
= 0; j
< nparam
; ++j
)
2748 if (value_notzero_p(V
->Vertex
->p
[i
][j
]))
2752 value_set_si(M
->p
[0][0], 1);
2753 Vector_Set(M
->p
[0]+1, 0, nvar
+exist
);
2754 Vector_Copy(V
->Vertex
->p
[i
],
2755 M
->p
[0] + 1 + nvar
+ exist
, nparam
+1);
2756 value_oppose(M
->p
[0][1+nvar
+i
],
2757 V
->Vertex
->p
[i
][nparam
+1]);
2759 Polyhedron
*pos
, *neg
;
2760 value_set_si(M
->p
[0][0], 1);
2761 value_decrement(M
->p
[0][P
->Dimension
+1],
2762 M
->p
[0][P
->Dimension
+1]);
2763 neg
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
2764 value_set_si(f
, -1);
2765 Vector_Scale(M
->p
[0]+1, M
->p
[0]+1, f
,
2767 value_decrement(M
->p
[0][P
->Dimension
+1],
2768 M
->p
[0][P
->Dimension
+1]);
2769 value_decrement(M
->p
[0][P
->Dimension
+1],
2770 M
->p
[0][P
->Dimension
+1]);
2771 pos
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
2772 if (emptyQ(neg
) || emptyQ(pos
)) {
2773 Polyhedron_Free(pos
);
2774 Polyhedron_Free(neg
);
2777 Polyhedron_Free(pos
);
2778 value_increment(M
->p
[0][P
->Dimension
+1],
2779 M
->p
[0][P
->Dimension
+1]);
2780 pos
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
2782 fprintf(stderr
, "\nER: Vertex\n");
2783 #endif /* DEBUG_ER */
2785 EP
= enumerate_or(pos
, exist
, nparam
, options
);
2790 } END_FORALL_PVertex_in_ParamPolyhedron
;
2794 /* Search for vertex coordinate to split on */
2795 /* Now look for one that depends on the parameters */
2796 FORALL_PVertex_in_ParamPolyhedron(V
, last
, PP
) {
2797 for (int i
= 0; i
< exist
; ++i
) {
2798 value_set_si(M
->p
[0][0], 1);
2799 Vector_Set(M
->p
[0]+1, 0, nvar
+exist
);
2800 Vector_Copy(V
->Vertex
->p
[i
],
2801 M
->p
[0] + 1 + nvar
+ exist
, nparam
+1);
2802 value_oppose(M
->p
[0][1+nvar
+i
],
2803 V
->Vertex
->p
[i
][nparam
+1]);
2805 Polyhedron
*pos
, *neg
;
2806 value_set_si(M
->p
[0][0], 1);
2807 value_decrement(M
->p
[0][P
->Dimension
+1],
2808 M
->p
[0][P
->Dimension
+1]);
2809 neg
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
2810 value_set_si(f
, -1);
2811 Vector_Scale(M
->p
[0]+1, M
->p
[0]+1, f
,
2813 value_decrement(M
->p
[0][P
->Dimension
+1],
2814 M
->p
[0][P
->Dimension
+1]);
2815 value_decrement(M
->p
[0][P
->Dimension
+1],
2816 M
->p
[0][P
->Dimension
+1]);
2817 pos
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
2818 if (emptyQ(neg
) || emptyQ(pos
)) {
2819 Polyhedron_Free(pos
);
2820 Polyhedron_Free(neg
);
2823 Polyhedron_Free(pos
);
2824 value_increment(M
->p
[0][P
->Dimension
+1],
2825 M
->p
[0][P
->Dimension
+1]);
2826 pos
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
2828 fprintf(stderr
, "\nER: ParamVertex\n");
2829 #endif /* DEBUG_ER */
2831 EP
= enumerate_or(pos
, exist
, nparam
, options
);
2836 } END_FORALL_PVertex_in_ParamPolyhedron
;
2844 Polyhedron_Free(CEq
);
2848 Param_Polyhedron_Free(PP
);
2854 evalue
* barvinok_enumerate_pip(Polyhedron
*P
, unsigned exist
, unsigned nparam
,
2858 barvinok_options
*options
= barvinok_options_new_with_defaults();
2859 options
->MaxRays
= MaxRays
;
2860 E
= barvinok_enumerate_pip_with_options(P
, exist
, nparam
, options
);
2861 barvinok_options_free(options
);
2866 evalue
*barvinok_enumerate_pip_with_options(Polyhedron
*P
,
2867 unsigned exist
, unsigned nparam
, struct barvinok_options
*options
)
2872 evalue
*barvinok_enumerate_pip_with_options(Polyhedron
*P
,
2873 unsigned exist
, unsigned nparam
, struct barvinok_options
*options
)
2875 int nvar
= P
->Dimension
- exist
- nparam
;
2876 evalue
*EP
= evalue_zero();
2880 fprintf(stderr
, "\nER: PIP\n");
2881 #endif /* DEBUG_ER */
2883 Polyhedron
*D
= pip_projectout(P
, nvar
, exist
, nparam
);
2884 for (Q
= D
; Q
; Q
= N
) {
2888 exist
= Q
->Dimension
- nvar
- nparam
;
2889 E
= barvinok_enumerate_e_with_options(Q
, exist
, nparam
, options
);
2892 free_evalue_refs(E
);
2901 static bool is_single(Value
*row
, int pos
, int len
)
2903 return First_Non_Zero(row
, pos
) == -1 &&
2904 First_Non_Zero(row
+pos
+1, len
-pos
-1) == -1;
2907 static evalue
* barvinok_enumerate_e_r(Polyhedron
*P
,
2908 unsigned exist
, unsigned nparam
, barvinok_options
*options
);
2911 static int er_level
= 0;
2913 evalue
* barvinok_enumerate_e_with_options(Polyhedron
*P
,
2914 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2916 fprintf(stderr
, "\nER: level %i\n", er_level
);
2918 Polyhedron_PrintConstraints(stderr
, P_VALUE_FMT
, P
);
2919 fprintf(stderr
, "\nE %d\nP %d\n", exist
, nparam
);
2921 P
= DomainConstraintSimplify(Polyhedron_Copy(P
), options
->MaxRays
);
2922 evalue
*EP
= barvinok_enumerate_e_r(P
, exist
, nparam
, options
);
2928 evalue
* barvinok_enumerate_e_with_options(Polyhedron
*P
,
2929 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2931 P
= DomainConstraintSimplify(Polyhedron_Copy(P
), options
->MaxRays
);
2932 evalue
*EP
= barvinok_enumerate_e_r(P
, exist
, nparam
, options
);
2938 evalue
* barvinok_enumerate_e(Polyhedron
*P
, unsigned exist
, unsigned nparam
,
2942 barvinok_options
*options
= barvinok_options_new_with_defaults();
2943 options
->MaxRays
= MaxRays
;
2944 E
= barvinok_enumerate_e_with_options(P
, exist
, nparam
, options
);
2945 barvinok_options_free(options
);
2949 static evalue
* barvinok_enumerate_e_r(Polyhedron
*P
,
2950 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2953 Polyhedron
*U
= Universe_Polyhedron(nparam
);
2954 evalue
*EP
= barvinok_enumerate_with_options(P
, U
, options
);
2955 //char *param_name[] = {"P", "Q", "R", "S", "T" };
2956 //print_evalue(stdout, EP, param_name);
2961 int nvar
= P
->Dimension
- exist
- nparam
;
2962 int len
= P
->Dimension
+ 2;
2965 POL_ENSURE_FACETS(P
);
2966 POL_ENSURE_VERTICES(P
);
2969 return evalue_zero();
2971 if (nvar
== 0 && nparam
== 0) {
2972 evalue
*EP
= evalue_zero();
2973 barvinok_count_with_options(P
, &EP
->x
.n
, options
);
2974 if (value_pos_p(EP
->x
.n
))
2975 value_set_si(EP
->x
.n
, 1);
2980 for (r
= 0; r
< P
->NbRays
; ++r
)
2981 if (value_zero_p(P
->Ray
[r
][0]) ||
2982 value_zero_p(P
->Ray
[r
][P
->Dimension
+1])) {
2984 for (i
= 0; i
< nvar
; ++i
)
2985 if (value_notzero_p(P
->Ray
[r
][i
+1]))
2989 for (i
= nvar
+ exist
; i
< nvar
+ exist
+ nparam
; ++i
)
2990 if (value_notzero_p(P
->Ray
[r
][i
+1]))
2992 if (i
>= nvar
+ exist
+ nparam
)
2995 if (r
< P
->NbRays
) {
2996 evalue
*EP
= evalue_zero();
2997 value_set_si(EP
->x
.n
, -1);
3002 for (r
= 0; r
< P
->NbEq
; ++r
)
3003 if ((first
= First_Non_Zero(P
->Constraint
[r
]+1+nvar
, exist
)) != -1)
3006 if (First_Non_Zero(P
->Constraint
[r
]+1+nvar
+first
+1,
3007 exist
-first
-1) != -1) {
3008 Polyhedron
*T
= rotate_along(P
, r
, nvar
, exist
, options
->MaxRays
);
3010 fprintf(stderr
, "\nER: Equality\n");
3011 #endif /* DEBUG_ER */
3012 evalue
*EP
= barvinok_enumerate_e_with_options(T
, exist
-1, nparam
,
3018 fprintf(stderr
, "\nER: Fixed\n");
3019 #endif /* DEBUG_ER */
3021 return barvinok_enumerate_e_with_options(P
, exist
-1, nparam
,
3024 Polyhedron
*T
= Polyhedron_Copy(P
);
3025 SwapColumns(T
, nvar
+1, nvar
+1+first
);
3026 evalue
*EP
= barvinok_enumerate_e_with_options(T
, exist
-1, nparam
,
3034 Vector
*row
= Vector_Alloc(len
);
3035 value_set_si(row
->p
[0], 1);
3040 enum constraint
* info
= new constraint
[exist
];
3041 for (int i
= 0; i
< exist
; ++i
) {
3043 for (int l
= P
->NbEq
; l
< P
->NbConstraints
; ++l
) {
3044 if (value_negz_p(P
->Constraint
[l
][nvar
+i
+1]))
3046 bool l_parallel
= is_single(P
->Constraint
[l
]+nvar
+1, i
, exist
);
3047 for (int u
= P
->NbEq
; u
< P
->NbConstraints
; ++u
) {
3048 if (value_posz_p(P
->Constraint
[u
][nvar
+i
+1]))
3050 bool lu_parallel
= l_parallel
||
3051 is_single(P
->Constraint
[u
]+nvar
+1, i
, exist
);
3052 value_oppose(f
, P
->Constraint
[u
][nvar
+i
+1]);
3053 Vector_Combine(P
->Constraint
[l
]+1, P
->Constraint
[u
]+1, row
->p
+1,
3054 f
, P
->Constraint
[l
][nvar
+i
+1], len
-1);
3055 if (!(info
[i
] & INDEPENDENT
)) {
3057 for (j
= 0; j
< exist
; ++j
)
3058 if (j
!= i
&& value_notzero_p(row
->p
[nvar
+j
+1]))
3061 //printf("independent: i: %d, l: %d, u: %d\n", i, l, u);
3062 info
[i
] = (constraint
)(info
[i
] | INDEPENDENT
);
3065 if (info
[i
] & ALL_POS
) {
3066 value_addto(row
->p
[len
-1], row
->p
[len
-1],
3067 P
->Constraint
[l
][nvar
+i
+1]);
3068 value_addto(row
->p
[len
-1], row
->p
[len
-1], f
);
3069 value_multiply(f
, f
, P
->Constraint
[l
][nvar
+i
+1]);
3070 value_subtract(row
->p
[len
-1], row
->p
[len
-1], f
);
3071 value_decrement(row
->p
[len
-1], row
->p
[len
-1]);
3072 ConstraintSimplify(row
->p
, row
->p
, len
, &f
);
3073 value_set_si(f
, -1);
3074 Vector_Scale(row
->p
+1, row
->p
+1, f
, len
-1);
3075 value_decrement(row
->p
[len
-1], row
->p
[len
-1]);
3076 Polyhedron
*T
= AddConstraints(row
->p
, 1, P
, options
->MaxRays
);
3078 //printf("not all_pos: i: %d, l: %d, u: %d\n", i, l, u);
3079 info
[i
] = (constraint
)(info
[i
] ^ ALL_POS
);
3081 //puts("pos remainder");
3082 //Polyhedron_Print(stdout, P_VALUE_FMT, T);
3085 if (!(info
[i
] & ONE_NEG
)) {
3087 negative_test_constraint(P
->Constraint
[l
],
3089 row
->p
, nvar
+i
, len
, &f
);
3090 oppose_constraint(row
->p
, len
, &f
);
3091 Polyhedron
*T
= AddConstraints(row
->p
, 1, P
,
3094 //printf("one_neg i: %d, l: %d, u: %d\n", i, l, u);
3095 info
[i
] = (constraint
)(info
[i
] | ONE_NEG
);
3097 //puts("neg remainder");
3098 //Polyhedron_Print(stdout, P_VALUE_FMT, T);
3100 } else if (!(info
[i
] & ROT_NEG
)) {
3101 if (parallel_constraints(P
->Constraint
[l
],
3103 row
->p
, nvar
, exist
)) {
3104 negative_test_constraint7(P
->Constraint
[l
],
3106 row
->p
, nvar
, exist
,
3108 oppose_constraint(row
->p
, len
, &f
);
3109 Polyhedron
*T
= AddConstraints(row
->p
, 1, P
,
3112 // printf("rot_neg i: %d, l: %d, u: %d\n", i, l, u);
3113 info
[i
] = (constraint
)(info
[i
] | ROT_NEG
);
3116 //puts("neg remainder");
3117 //Polyhedron_Print(stdout, P_VALUE_FMT, T);
3122 if (!(info
[i
] & ALL_POS
) && (info
[i
] & (ONE_NEG
| ROT_NEG
)))
3126 if (info
[i
] & ALL_POS
)
3133 for (int i = 0; i < exist; ++i)
3134 printf("%i: %i\n", i, info[i]);
3136 for (int i
= 0; i
< exist
; ++i
)
3137 if (info
[i
] & ALL_POS
) {
3139 fprintf(stderr
, "\nER: Positive\n");
3140 #endif /* DEBUG_ER */
3142 // Maybe we should chew off some of the fat here
3143 Matrix
*M
= Matrix_Alloc(P
->Dimension
, P
->Dimension
+1);
3144 for (int j
= 0; j
< P
->Dimension
; ++j
)
3145 value_set_si(M
->p
[j
][j
+ (j
>= i
+nvar
)], 1);
3146 Polyhedron
*T
= Polyhedron_Image(P
, M
, options
->MaxRays
);
3148 evalue
*EP
= barvinok_enumerate_e_with_options(T
, exist
-1, nparam
,
3156 for (int i
= 0; i
< exist
; ++i
)
3157 if (info
[i
] & ONE_NEG
) {
3159 fprintf(stderr
, "\nER: Negative\n");
3160 #endif /* DEBUG_ER */
3165 return barvinok_enumerate_e_with_options(P
, exist
-1, nparam
,
3168 Polyhedron
*T
= Polyhedron_Copy(P
);
3169 SwapColumns(T
, nvar
+1, nvar
+1+i
);
3170 evalue
*EP
= barvinok_enumerate_e_with_options(T
, exist
-1, nparam
,
3176 for (int i
= 0; i
< exist
; ++i
)
3177 if (info
[i
] & ROT_NEG
) {
3179 fprintf(stderr
, "\nER: Rotate\n");
3180 #endif /* DEBUG_ER */
3184 Polyhedron
*T
= rotate_along(P
, r
, nvar
, exist
, options
->MaxRays
);
3185 evalue
*EP
= barvinok_enumerate_e_with_options(T
, exist
-1, nparam
,
3190 for (int i
= 0; i
< exist
; ++i
)
3191 if (info
[i
] & INDEPENDENT
) {
3192 Polyhedron
*pos
, *neg
;
3194 /* Find constraint again and split off negative part */
3196 if (SplitOnVar(P
, i
, nvar
, exist
, options
->MaxRays
,
3197 row
, f
, true, &pos
, &neg
)) {
3199 fprintf(stderr
, "\nER: Split\n");
3200 #endif /* DEBUG_ER */
3203 barvinok_enumerate_e_with_options(neg
, exist
-1, nparam
, options
);
3205 barvinok_enumerate_e_with_options(pos
, exist
, nparam
, options
);
3207 free_evalue_refs(E
);
3209 Polyhedron_Free(neg
);
3210 Polyhedron_Free(pos
);
3224 EP
= enumerate_line(P
, exist
, nparam
, options
);
3228 EP
= barvinok_enumerate_pip_with_options(P
, exist
, nparam
, options
);
3232 EP
= enumerate_redundant_ray(P
, exist
, nparam
, options
);
3236 EP
= enumerate_sure(P
, exist
, nparam
, options
);
3240 EP
= enumerate_ray(P
, exist
, nparam
, options
);
3244 EP
= enumerate_sure2(P
, exist
, nparam
, options
);
3248 F
= unfringe(P
, options
->MaxRays
);
3249 if (!PolyhedronIncludes(F
, P
)) {
3251 fprintf(stderr
, "\nER: Fringed\n");
3252 #endif /* DEBUG_ER */
3253 EP
= barvinok_enumerate_e_with_options(F
, exist
, nparam
, options
);
3260 EP
= enumerate_vd(&P
, exist
, nparam
, options
);
3265 EP
= enumerate_sum(P
, exist
, nparam
, options
);
3272 Polyhedron
*pos
, *neg
;
3273 for (i
= 0; i
< exist
; ++i
)
3274 if (SplitOnVar(P
, i
, nvar
, exist
, options
->MaxRays
,
3275 row
, f
, false, &pos
, &neg
))
3281 EP
= enumerate_or(pos
, exist
, nparam
, options
);
3294 * remove equalities that require a "compression" of the parameters
3296 static Polyhedron
*remove_more_equalities(Polyhedron
*P
, unsigned nparam
,
3297 Matrix
**CP
, unsigned MaxRays
)
3300 remove_all_equalities(&P
, NULL
, CP
, NULL
, nparam
, MaxRays
);
3307 static gen_fun
*series(Polyhedron
*P
, unsigned nparam
, barvinok_options
*options
)
3317 assert(!Polyhedron_is_unbounded(P
, nparam
, options
->MaxRays
));
3318 assert(P
->NbBid
== 0);
3319 assert(Polyhedron_has_revlex_positive_rays(P
, nparam
));
3321 P
= remove_more_equalities(P
, nparam
, &CP
, options
->MaxRays
);
3322 assert(P
->NbEq
== 0);
3324 nparam
= CP
->NbColumns
-1;
3329 barvinok_count_with_options(P
, &c
, options
);
3330 gf
= new gen_fun(c
);
3334 red
= gf_base::create(Polyhedron_Project(P
, nparam
),
3335 P
->Dimension
, nparam
, options
);
3336 POL_ENSURE_VERTICES(P
);
3337 red
->start_gf(P
, options
);
3349 gen_fun
* barvinok_series_with_options(Polyhedron
*P
, Polyhedron
* C
,
3350 barvinok_options
*options
)
3353 unsigned nparam
= C
->Dimension
;
3356 CA
= align_context(C
, P
->Dimension
, options
->MaxRays
);
3357 P
= DomainIntersection(P
, CA
, options
->MaxRays
);
3358 Polyhedron_Free(CA
);
3360 gf
= series(P
, nparam
, options
);
3365 gen_fun
* barvinok_series(Polyhedron
*P
, Polyhedron
* C
, unsigned MaxRays
)
3368 barvinok_options
*options
= barvinok_options_new_with_defaults();
3369 options
->MaxRays
= MaxRays
;
3370 gf
= barvinok_series_with_options(P
, C
, options
);
3371 barvinok_options_free(options
);
3375 static Polyhedron
*skew_into_positive_orthant(Polyhedron
*D
, unsigned nparam
,
3381 for (Polyhedron
*P
= D
; P
; P
= P
->next
) {
3382 POL_ENSURE_VERTICES(P
);
3383 assert(!Polyhedron_is_unbounded(P
, nparam
, MaxRays
));
3384 assert(P
->NbBid
== 0);
3385 assert(Polyhedron_has_positive_rays(P
, nparam
));
3387 for (int r
= 0; r
< P
->NbRays
; ++r
) {
3388 if (value_notzero_p(P
->Ray
[r
][P
->Dimension
+1]))
3390 for (int i
= 0; i
< nparam
; ++i
) {
3392 if (value_posz_p(P
->Ray
[r
][i
+1]))
3395 M
= Matrix_Alloc(D
->Dimension
+1, D
->Dimension
+1);
3396 for (int i
= 0; i
< D
->Dimension
+1; ++i
)
3397 value_set_si(M
->p
[i
][i
], 1);
3399 Inner_Product(P
->Ray
[r
]+1, M
->p
[i
], D
->Dimension
+1, &tmp
);
3400 if (value_posz_p(tmp
))
3403 for (j
= P
->Dimension
- nparam
; j
< P
->Dimension
; ++j
)
3404 if (value_pos_p(P
->Ray
[r
][j
+1]))
3406 assert(j
< P
->Dimension
);
3407 value_pdivision(tmp
, P
->Ray
[r
][j
+1], P
->Ray
[r
][i
+1]);
3408 value_subtract(M
->p
[i
][j
], M
->p
[i
][j
], tmp
);
3414 D
= DomainImage(D
, M
, MaxRays
);
3420 gen_fun
* barvinok_enumerate_union_series_with_options(Polyhedron
*D
, Polyhedron
* C
,
3421 barvinok_options
*options
)
3423 Polyhedron
*conv
, *D2
;
3425 gen_fun
*gf
= NULL
, *gf2
;
3426 unsigned nparam
= C
->Dimension
;
3431 CA
= align_context(C
, D
->Dimension
, options
->MaxRays
);
3432 D
= DomainIntersection(D
, CA
, options
->MaxRays
);
3433 Polyhedron_Free(CA
);
3435 D2
= skew_into_positive_orthant(D
, nparam
, options
->MaxRays
);
3436 for (Polyhedron
*P
= D2
; P
; P
= P
->next
) {
3437 assert(P
->Dimension
== D2
->Dimension
);
3440 P_gf
= series(Polyhedron_Copy(P
), nparam
, options
);
3444 gf
->add_union(P_gf
, options
);
3448 /* we actually only need the convex union of the parameter space
3449 * but the reducer classes currently expect a polyhedron in
3450 * the combined space
3452 Polyhedron_Free(gf
->context
);
3453 gf
->context
= DomainConvex(D2
, options
->MaxRays
);
3455 gf2
= gf
->summate(D2
->Dimension
- nparam
, options
);
3464 gen_fun
* barvinok_enumerate_union_series(Polyhedron
*D
, Polyhedron
* C
,
3468 barvinok_options
*options
= barvinok_options_new_with_defaults();
3469 options
->MaxRays
= MaxRays
;
3470 gf
= barvinok_enumerate_union_series_with_options(D
, C
, options
);
3471 barvinok_options_free(options
);
3475 evalue
* barvinok_enumerate_union(Polyhedron
*D
, Polyhedron
* C
, unsigned MaxRays
)
3478 gen_fun
*gf
= barvinok_enumerate_union_series(D
, C
, MaxRays
);