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"
30 #include "bernoulli.h"
31 #include "param_util.h"
42 using std::ostringstream
;
44 #define ALLOC(t,p) p = (t*)malloc(sizeof(*p))
57 coeff
= Matrix_Alloc(d
+1, d
+1+1);
58 value_set_si(coeff
->p
[0][0], 1);
59 value_set_si(coeff
->p
[0][d
+1], 1);
60 for (int i
= 1; i
<= d
; ++i
) {
61 value_multiply(coeff
->p
[i
][0], coeff
->p
[i
-1][0], d0
);
62 Vector_Combine(coeff
->p
[i
-1], coeff
->p
[i
-1]+1, coeff
->p
[i
]+1,
64 value_set_si(coeff
->p
[i
][d
+1], i
);
65 value_multiply(coeff
->p
[i
][d
+1], coeff
->p
[i
][d
+1], coeff
->p
[i
-1][d
+1]);
66 value_decrement(d0
, d0
);
71 void div(dpoly
& d
, Vector
*count
, ZZ
& sign
) {
72 int len
= coeff
->NbRows
;
73 Matrix
* c
= Matrix_Alloc(coeff
->NbRows
, coeff
->NbColumns
);
76 for (int i
= 0; i
< len
; ++i
) {
77 Vector_Copy(coeff
->p
[i
], c
->p
[i
], len
+1);
78 for (int j
= 1; j
<= i
; ++j
) {
79 value_multiply(tmp
, d
.coeff
->p
[j
], c
->p
[i
][len
]);
80 value_oppose(tmp
, tmp
);
81 Vector_Combine(c
->p
[i
], c
->p
[i
-j
], c
->p
[i
],
82 c
->p
[i
-j
][len
], tmp
, len
);
83 value_multiply(c
->p
[i
][len
], c
->p
[i
][len
], c
->p
[i
-j
][len
]);
85 value_multiply(c
->p
[i
][len
], c
->p
[i
][len
], d
.coeff
->p
[0]);
88 value_set_si(tmp
, -1);
89 Vector_Scale(c
->p
[len
-1], count
->p
, tmp
, len
);
90 value_assign(count
->p
[len
], c
->p
[len
-1][len
]);
92 Vector_Copy(c
->p
[len
-1], count
->p
, len
+1);
93 Vector_Normalize(count
->p
, len
+1);
101 * Searches for a vector that is not orthogonal to any
102 * of the rays in rays.
104 static void nonorthog(mat_ZZ
& rays
, vec_ZZ
& lambda
)
106 int dim
= rays
.NumCols();
108 lambda
.SetLength(dim
);
112 for (int i
= 2; !found
&& i
<= 50*dim
; i
+=4) {
113 for (int j
= 0; j
< MAX_TRY
; ++j
) {
114 for (int k
= 0; k
< dim
; ++k
) {
115 int r
= random_int(i
)+2;
116 int v
= (2*(r
%2)-1) * (r
>> 1);
120 for (; k
< rays
.NumRows(); ++k
)
121 if (lambda
* rays
[k
] == 0)
123 if (k
== rays
.NumRows()) {
132 static void add_rays(mat_ZZ
& rays
, Polyhedron
*i
, int *r
, int nvar
= -1,
135 unsigned dim
= i
->Dimension
;
138 for (int k
= 0; k
< i
->NbRays
; ++k
) {
139 if (!value_zero_p(i
->Ray
[k
][dim
+1]))
141 if (!all
&& nvar
!= dim
&& First_Non_Zero(i
->Ray
[k
]+1, nvar
) == -1)
143 values2zz(i
->Ray
[k
]+1, rays
[(*r
)++], nvar
);
147 static void mask_r(Matrix
*f
, int nr
, Vector
*lcm
, int p
, Vector
*val
, evalue
*ev
)
149 unsigned nparam
= lcm
->Size
;
152 Vector
* prod
= Vector_Alloc(f
->NbRows
);
153 Matrix_Vector_Product(f
, val
->p
, prod
->p
);
155 for (int i
= 0; i
< nr
; ++i
) {
156 value_modulus(prod
->p
[i
], prod
->p
[i
], f
->p
[i
][nparam
+1]);
157 isint
&= value_zero_p(prod
->p
[i
]);
159 value_set_si(ev
->d
, 1);
161 value_set_si(ev
->x
.n
, isint
);
168 if (value_one_p(lcm
->p
[p
]))
169 mask_r(f
, nr
, lcm
, p
+1, val
, ev
);
171 value_assign(tmp
, lcm
->p
[p
]);
172 value_set_si(ev
->d
, 0);
173 ev
->x
.p
= new_enode(periodic
, VALUE_TO_INT(tmp
), p
+1);
175 value_decrement(tmp
, tmp
);
176 value_assign(val
->p
[p
], tmp
);
177 mask_r(f
, nr
, lcm
, p
+1, val
, &ev
->x
.p
->arr
[VALUE_TO_INT(tmp
)]);
178 } while (value_pos_p(tmp
));
183 static void mask_fractional(Matrix
*f
, evalue
*factor
)
185 int nr
= f
->NbRows
, nc
= f
->NbColumns
;
188 for (n
= 0; n
< nr
&& value_notzero_p(f
->p
[n
][nc
-1]); ++n
)
189 if (value_notone_p(f
->p
[n
][nc
-1]) &&
190 value_notmone_p(f
->p
[n
][nc
-1]))
204 value_set_si(EV
.x
.n
, 1);
206 for (n
= 0; n
< nr
; ++n
) {
207 value_assign(m
, f
->p
[n
][nc
-1]);
208 if (value_one_p(m
) || value_mone_p(m
))
211 int j
= normal_mod(f
->p
[n
], nc
-1, &m
);
213 free_evalue_refs(factor
);
214 value_init(factor
->d
);
215 evalue_set_si(factor
, 0, 1);
219 values2zz(f
->p
[n
], row
, nc
-1);
222 if (j
< (nc
-1)-1 && row
[j
] > g
/2) {
223 for (int k
= j
; k
< (nc
-1); ++k
)
229 value_set_si(EP
.d
, 0);
230 EP
.x
.p
= new_enode(relation
, 2, 0);
231 value_clear(EP
.x
.p
->arr
[1].d
);
232 EP
.x
.p
->arr
[1] = *factor
;
233 evalue
*ev
= &EP
.x
.p
->arr
[0];
234 value_set_si(ev
->d
, 0);
235 ev
->x
.p
= new_enode(fractional
, 3, -1);
236 evalue_set_si(&ev
->x
.p
->arr
[1], 0, 1);
237 evalue_set_si(&ev
->x
.p
->arr
[2], 1, 1);
238 evalue
*E
= multi_monom(row
);
239 value_assign(EV
.d
, m
);
241 value_clear(ev
->x
.p
->arr
[0].d
);
242 ev
->x
.p
->arr
[0] = *E
;
248 free_evalue_refs(&EV
);
254 static void mask_table(Matrix
*f
, evalue
*factor
)
256 int nr
= f
->NbRows
, nc
= f
->NbColumns
;
259 for (n
= 0; n
< nr
&& value_notzero_p(f
->p
[n
][nc
-1]); ++n
)
260 if (value_notone_p(f
->p
[n
][nc
-1]) &&
261 value_notmone_p(f
->p
[n
][nc
-1]))
269 unsigned np
= nc
- 2;
270 Vector
*lcm
= Vector_Alloc(np
);
271 Vector
*val
= Vector_Alloc(nc
);
272 Vector_Set(val
->p
, 0, nc
);
273 value_set_si(val
->p
[np
], 1);
274 Vector_Set(lcm
->p
, 1, np
);
275 for (n
= 0; n
< nr
; ++n
) {
276 if (value_one_p(f
->p
[n
][nc
-1]) ||
277 value_mone_p(f
->p
[n
][nc
-1]))
279 for (int j
= 0; j
< np
; ++j
)
280 if (value_notzero_p(f
->p
[n
][j
])) {
281 Gcd(f
->p
[n
][j
], f
->p
[n
][nc
-1], &tmp
);
282 value_division(tmp
, f
->p
[n
][nc
-1], tmp
);
283 value_lcm(tmp
, lcm
->p
[j
], &lcm
->p
[j
]);
288 mask_r(f
, nr
, lcm
, 0, val
, &EP
);
293 free_evalue_refs(&EP
);
296 static void mask(Matrix
*f
, evalue
*factor
, barvinok_options
*options
)
298 if (options
->lookup_table
)
299 mask_table(f
, factor
);
301 mask_fractional(f
, factor
);
304 struct bfe_term
: public bfc_term_base
{
305 vector
<evalue
*> factors
;
307 bfe_term(int len
) : bfc_term_base(len
) {
311 for (int i
= 0; i
< factors
.size(); ++i
) {
314 free_evalue_refs(factors
[i
]);
320 static void print_int_vector(int *v
, int len
, const char *name
)
322 cerr
<< name
<< endl
;
323 for (int j
= 0; j
< len
; ++j
) {
329 static void print_bfc_terms(mat_ZZ
& factors
, bfc_vec
& v
)
332 cerr
<< "factors" << endl
;
333 cerr
<< factors
<< endl
;
334 for (int i
= 0; i
< v
.size(); ++i
) {
335 cerr
<< "term: " << i
<< endl
;
336 print_int_vector(v
[i
]->powers
, factors
.NumRows(), "powers");
337 cerr
<< "terms" << endl
;
338 cerr
<< v
[i
]->terms
<< endl
;
339 bfc_term
* bfct
= static_cast<bfc_term
*>(v
[i
]);
340 cerr
<< bfct
->c
<< endl
;
344 static void print_bfe_terms(mat_ZZ
& factors
, bfc_vec
& v
)
347 cerr
<< "factors" << endl
;
348 cerr
<< factors
<< endl
;
349 for (int i
= 0; i
< v
.size(); ++i
) {
350 cerr
<< "term: " << i
<< endl
;
351 print_int_vector(v
[i
]->powers
, factors
.NumRows(), "powers");
352 cerr
<< "terms" << endl
;
353 cerr
<< v
[i
]->terms
<< endl
;
354 bfe_term
* bfet
= static_cast<bfe_term
*>(v
[i
]);
355 for (int j
= 0; j
< v
[i
]->terms
.NumRows(); ++j
) {
356 const char * test
[] = {"a", "b"};
357 print_evalue(stderr
, bfet
->factors
[j
], test
);
358 fprintf(stderr
, "\n");
363 struct bfcounter
: public bfcounter_base
{
367 bfcounter(unsigned dim
) : bfcounter_base(dim
) {
376 virtual void base(mat_ZZ
& factors
, bfc_vec
& v
);
377 virtual void get_count(Value
*result
) {
378 assert(value_one_p(&count
[0]._mp_den
));
379 value_assign(*result
, &count
[0]._mp_num
);
383 void bfcounter::base(mat_ZZ
& factors
, bfc_vec
& v
)
385 unsigned nf
= factors
.NumRows();
387 for (int i
= 0; i
< v
.size(); ++i
) {
388 bfc_term
* bfct
= static_cast<bfc_term
*>(v
[i
]);
390 // factor is always positive, so we always
392 for (int k
= 0; k
< nf
; ++k
)
393 total_power
+= v
[i
]->powers
[k
];
396 for (j
= 0; j
< nf
; ++j
)
397 if (v
[i
]->powers
[j
] > 0)
400 zz2value(factors
[j
][0], tz
);
401 dpoly
D(total_power
, tz
, 1);
402 for (int k
= 1; k
< v
[i
]->powers
[j
]; ++k
) {
403 zz2value(factors
[j
][0], tz
);
404 dpoly
fact(total_power
, tz
, 1);
408 for (int k
= 0; k
< v
[i
]->powers
[j
]; ++k
) {
409 zz2value(factors
[j
][0], tz
);
410 dpoly
fact(total_power
, tz
, 1);
414 for (int k
= 0; k
< v
[i
]->terms
.NumRows(); ++k
) {
415 zz2value(v
[i
]->terms
[k
][0], tz
);
416 dpoly
n(total_power
, tz
);
417 mpq_set_si(tcount
, 0, 1);
418 n
.div(D
, tcount
, one
);
420 bfct
->c
[k
].n
= -bfct
->c
[k
].n
;
421 zz2value(bfct
->c
[k
].n
, tn
);
422 zz2value(bfct
->c
[k
].d
, td
);
424 mpz_mul(mpq_numref(tcount
), mpq_numref(tcount
), tn
);
425 mpz_mul(mpq_denref(tcount
), mpq_denref(tcount
), td
);
426 mpq_canonicalize(tcount
);
427 mpq_add(count
, count
, tcount
);
434 /* Check whether the polyhedron is unbounded and if so,
435 * check whether it has any (and therefore an infinite number of)
437 * If one of the vertices is integer, then we are done.
438 * Otherwise, transform the polyhedron such that one of the rays
439 * is the first unit vector and cut it off at a height that ensures
440 * that if the whole polyhedron has any points, then the remaining part
441 * has integer points. In particular we add the largest coefficient
442 * of a ray to the highest vertex (rounded up).
444 static bool Polyhedron_is_infinite(Polyhedron
*P
, Value
* result
,
445 barvinok_options
*options
)
457 for (; r
< P
->NbRays
; ++r
)
458 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1]))
460 if (P
->NbBid
== 0 && r
== P
->NbRays
)
463 if (options
->count_sample_infinite
) {
466 sample
= Polyhedron_Sample(P
, options
);
468 value_set_si(*result
, 0);
470 value_set_si(*result
, -1);
476 for (int i
= 0; i
< P
->NbRays
; ++i
)
477 if (value_one_p(P
->Ray
[i
][1+P
->Dimension
])) {
478 value_set_si(*result
, -1);
483 M
= Matrix_Alloc(P
->Dimension
+1, P
->Dimension
+1);
484 Vector_Gcd(P
->Ray
[r
]+1, P
->Dimension
, &g
);
485 Vector_AntiScale(P
->Ray
[r
]+1, M
->p
[0], g
, P
->Dimension
+1);
486 int ok
= unimodular_complete(M
, 1);
488 value_set_si(M
->p
[P
->Dimension
][P
->Dimension
], 1);
491 P
= Polyhedron_Preimage(P
, M2
, 0);
499 value_set_si(size
, 0);
501 for (int i
= 0; i
< P
->NbBid
; ++i
) {
502 value_absolute(tmp
, P
->Ray
[i
][1]);
503 if (value_gt(tmp
, size
))
504 value_assign(size
, tmp
);
506 for (int i
= P
->NbBid
; i
< P
->NbRays
; ++i
) {
507 if (value_zero_p(P
->Ray
[i
][P
->Dimension
+1])) {
508 if (value_gt(P
->Ray
[i
][1], size
))
509 value_assign(size
, P
->Ray
[i
][1]);
512 mpz_cdiv_q(tmp
, P
->Ray
[i
][1], P
->Ray
[i
][P
->Dimension
+1]);
513 if (first
|| value_gt(tmp
, offset
)) {
514 value_assign(offset
, tmp
);
518 value_addto(offset
, offset
, size
);
522 v
= Vector_Alloc(P
->Dimension
+2);
523 value_set_si(v
->p
[0], 1);
524 value_set_si(v
->p
[1], -1);
525 value_assign(v
->p
[1+P
->Dimension
], offset
);
526 R
= AddConstraints(v
->p
, 1, P
, options
->MaxRays
);
534 barvinok_count_with_options(P
, &c
, options
);
537 value_set_si(*result
, 0);
539 value_set_si(*result
, -1);
545 typedef Polyhedron
* Polyhedron_p
;
547 static void barvinok_count_f(Polyhedron
*P
, Value
* result
,
548 barvinok_options
*options
);
550 void barvinok_count_with_options(Polyhedron
*P
, Value
* result
,
551 struct barvinok_options
*options
)
556 bool infinite
= false;
560 "barvinok_count: input is a union; only first polyhedron is counted\n");
563 value_set_si(*result
, 0);
569 P
= remove_equalities(P
, options
->MaxRays
);
570 P
= DomainConstraintSimplify(P
, options
->MaxRays
);
574 } while (!emptyQ(P
) && P
->NbEq
!= 0);
577 value_set_si(*result
, 0);
582 if (Polyhedron_is_infinite(P
, result
, options
)) {
587 if (P
->Dimension
== 0) {
588 /* Test whether the constraints are satisfied */
589 POL_ENSURE_VERTICES(P
);
590 value_set_si(*result
, !emptyQ(P
));
595 Q
= Polyhedron_Factor(P
, 0, NULL
, options
->MaxRays
);
603 barvinok_count_f(P
, result
, options
);
604 if (value_neg_p(*result
))
606 if (Q
&& P
->next
&& value_notzero_p(*result
)) {
610 for (Q
= P
->next
; Q
; Q
= Q
->next
) {
611 barvinok_count_f(Q
, &factor
, options
);
612 if (value_neg_p(factor
)) {
615 } else if (Q
->next
&& value_zero_p(factor
)) {
616 value_set_si(*result
, 0);
619 value_multiply(*result
, *result
, factor
);
628 value_set_si(*result
, -1);
631 void barvinok_count(Polyhedron
*P
, Value
* result
, unsigned NbMaxCons
)
633 barvinok_options
*options
= barvinok_options_new_with_defaults();
634 options
->MaxRays
= NbMaxCons
;
635 barvinok_count_with_options(P
, result
, options
);
636 barvinok_options_free(options
);
639 static void barvinok_count_f(Polyhedron
*P
, Value
* result
,
640 barvinok_options
*options
)
643 value_set_si(*result
, 0);
647 if (P
->Dimension
== 1)
648 return Line_Length(P
, result
);
650 int c
= P
->NbConstraints
;
651 POL_ENSURE_FACETS(P
);
652 if (c
!= P
->NbConstraints
|| P
->NbEq
!= 0) {
653 Polyhedron
*next
= P
->next
;
655 barvinok_count_with_options(P
, result
, options
);
660 POL_ENSURE_VERTICES(P
);
662 if (Polyhedron_is_infinite(P
, result
, options
))
666 if (options
->incremental_specialization
== BV_SPECIALIZATION_BF
)
667 cnt
= new bfcounter(P
->Dimension
);
668 else if (options
->incremental_specialization
== BV_SPECIALIZATION_DF
)
669 cnt
= new icounter(P
->Dimension
);
670 else if (options
->incremental_specialization
== BV_SPECIALIZATION_TODD
)
671 cnt
= new tcounter(P
->Dimension
, options
->max_index
);
673 cnt
= new counter(P
->Dimension
, options
->max_index
);
674 cnt
->start(P
, options
);
676 cnt
->get_count(result
);
680 static void uni_polynom(int param
, Vector
*c
, evalue
*EP
)
682 unsigned dim
= c
->Size
-2;
684 value_set_si(EP
->d
,0);
685 EP
->x
.p
= new_enode(polynomial
, dim
+1, param
+1);
686 for (int j
= 0; j
<= dim
; ++j
)
687 evalue_set(&EP
->x
.p
->arr
[j
], c
->p
[j
], c
->p
[dim
+1]);
690 Polyhedron
*unfringe (Polyhedron
*P
, unsigned MaxRays
)
692 int len
= P
->Dimension
+2;
693 Polyhedron
*T
, *R
= P
;
696 Vector
*row
= Vector_Alloc(len
);
697 value_set_si(row
->p
[0], 1);
699 R
= DomainConstraintSimplify(Polyhedron_Copy(P
), MaxRays
);
701 Matrix
*M
= Matrix_Alloc(2, len
-1);
702 value_set_si(M
->p
[1][len
-2], 1);
703 for (int v
= 0; v
< P
->Dimension
; ++v
) {
704 value_set_si(M
->p
[0][v
], 1);
705 Polyhedron
*I
= Polyhedron_Image(R
, M
, 2+1);
706 value_set_si(M
->p
[0][v
], 0);
707 for (int r
= 0; r
< I
->NbConstraints
; ++r
) {
708 if (value_zero_p(I
->Constraint
[r
][0]))
710 if (value_zero_p(I
->Constraint
[r
][1]))
712 if (value_one_p(I
->Constraint
[r
][1]))
714 if (value_mone_p(I
->Constraint
[r
][1]))
716 value_absolute(g
, I
->Constraint
[r
][1]);
717 Vector_Set(row
->p
+1, 0, len
-2);
718 value_division(row
->p
[1+v
], I
->Constraint
[r
][1], g
);
719 mpz_fdiv_q(row
->p
[len
-1], I
->Constraint
[r
][2], g
);
721 R
= AddConstraints(row
->p
, 1, R
, MaxRays
);
733 /* Check whether all rays point in the positive directions
736 static bool Polyhedron_has_positive_rays(Polyhedron
*P
, unsigned nparam
)
739 for (r
= 0; r
< P
->NbRays
; ++r
)
740 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1])) {
742 for (i
= P
->Dimension
- nparam
; i
< P
->Dimension
; ++i
)
743 if (value_neg_p(P
->Ray
[r
][i
+1]))
749 typedef evalue
* evalue_p
;
751 struct enumerator_base
{
755 vertex_decomposer
*vpd
;
757 enumerator_base(unsigned dim
, vertex_decomposer
*vpd
)
762 vE
= new evalue_p
[vpd
->nbV
];
763 for (int j
= 0; j
< vpd
->nbV
; ++j
)
767 evalue_set_si(&mone
, -1, 1);
770 void decompose_at(Param_Vertices
*V
, int _i
, barvinok_options
*options
) {
774 value_init(vE
[_i
]->d
);
775 evalue_set_si(vE
[_i
], 0, 1);
777 vpd
->decompose_at_vertex(V
, _i
, options
);
780 virtual ~enumerator_base() {
781 for (int j
= 0; j
< vpd
->nbV
; ++j
)
783 free_evalue_refs(vE
[j
]);
788 free_evalue_refs(&mone
);
791 static enumerator_base
*create(Polyhedron
*P
, unsigned dim
, unsigned nbV
,
792 barvinok_options
*options
);
795 struct enumerator
: public signed_cone_consumer
, public vertex_decomposer
,
796 public enumerator_base
{
805 enumerator(Polyhedron
*P
, unsigned dim
, unsigned nbV
) :
806 vertex_decomposer(P
, nbV
, *this), enumerator_base(dim
, this) {
809 randomvector(P
, lambda
, dim
);
811 c
= Vector_Alloc(dim
+2);
823 virtual void handle(const signed_cone
& sc
, barvinok_options
*options
);
826 void enumerator::handle(const signed_cone
& sc
, barvinok_options
*options
)
829 assert(sc
.rays
.NumRows() == dim
);
830 for (int k
= 0; k
< dim
; ++k
) {
831 if (lambda
* sc
.rays
[k
] == 0)
837 lattice_point(V
, sc
.rays
, lambda
, &num
, sc
.det
, sc
.closed
, options
);
838 den
= sc
.rays
* lambda
;
843 zz2value(den
[0], tz
);
845 for (int k
= 1; k
< dim
; ++k
) {
846 zz2value(den
[k
], tz
);
847 dpoly
fact(dim
, tz
, 1);
853 for (unsigned long i
= 0; i
< sc
.det
; ++i
) {
854 evalue
*EV
= evalue_polynomial(c
, num
.E
[i
]);
856 free_evalue_refs(EV
);
858 free_evalue_refs(num
.E
[i
]);
863 mpq_set_si(count
, 0, 1);
864 if (num
.constant
.length() == 1) {
865 zz2value(num
.constant
[0], tz
);
867 d
.div(n
, count
, sign
);
874 for (unsigned long i
= 0; i
< sc
.det
; ++i
) {
875 value_assign(acc
, c
->p
[dim
]);
876 zz2value(num
.constant
[i
], x
);
877 for (int j
= dim
-1; j
>= 0; --j
) {
878 value_multiply(acc
, acc
, x
);
879 value_addto(acc
, acc
, c
->p
[j
]);
881 value_addto(mpq_numref(count
), mpq_numref(count
), acc
);
883 mpz_set(mpq_denref(count
), c
->p
[dim
+1]);
889 evalue_set(&EV
, &count
[0]._mp_num
, &count
[0]._mp_den
);
891 free_evalue_refs(&EV
);
895 struct ienumerator_base
: enumerator_base
{
898 ienumerator_base(unsigned dim
, vertex_decomposer
*vpd
) :
899 enumerator_base(dim
,vpd
) {
900 E_vertex
= new evalue_p
[dim
];
903 virtual ~ienumerator_base() {
907 evalue
*E_num(int i
, int d
) {
908 return E_vertex
[i
+ (dim
-d
)];
917 cumulator(evalue
*factor
, evalue
*v
, dpoly_r
*r
) :
918 factor(factor
), v(v
), r(r
) {}
920 void cumulate(barvinok_options
*options
);
922 virtual void add_term(const vector
<int>& powers
, evalue
*f2
) = 0;
923 virtual ~cumulator() {}
926 void cumulator::cumulate(barvinok_options
*options
)
928 evalue cum
; // factor * 1 * E_num[0]/1 * (E_num[0]-1)/2 *...
930 evalue t
; // E_num[0] - (m-1)
934 if (options
->lookup_table
) {
936 evalue_set_si(&mone
, -1, 1);
940 evalue_copy(&cum
, factor
);
943 value_set_si(f
.d
, 1);
944 value_set_si(f
.x
.n
, 1);
948 if (!options
->lookup_table
) {
949 for (cst
= &t
; value_zero_p(cst
->d
); ) {
950 if (cst
->x
.p
->type
== fractional
)
951 cst
= &cst
->x
.p
->arr
[1];
953 cst
= &cst
->x
.p
->arr
[0];
957 for (int m
= 0; m
< r
->len
; ++m
) {
960 value_set_si(f
.d
, m
);
962 if (!options
->lookup_table
)
963 value_subtract(cst
->x
.n
, cst
->x
.n
, cst
->d
);
969 dpoly_r_term_list
& current
= r
->c
[r
->len
-1-m
];
970 dpoly_r_term_list::iterator j
;
971 for (j
= current
.begin(); j
!= current
.end(); ++j
) {
972 if ((*j
)->coeff
== 0)
974 evalue
*f2
= new evalue
;
977 zz2value((*j
)->coeff
, f2
->x
.n
);
978 zz2value(r
->denom
, f2
->d
);
981 add_term((*j
)->powers
, f2
);
984 free_evalue_refs(&f
);
985 free_evalue_refs(&t
);
986 free_evalue_refs(&cum
);
987 if (options
->lookup_table
)
988 free_evalue_refs(&mone
);
996 struct ie_cum
: public cumulator
{
997 vector
<E_poly_term
*> terms
;
999 ie_cum(evalue
*factor
, evalue
*v
, dpoly_r
*r
) : cumulator(factor
, v
, r
) {}
1001 virtual void add_term(const vector
<int>& powers
, evalue
*f2
);
1004 void ie_cum::add_term(const vector
<int>& powers
, evalue
*f2
)
1007 for (k
= 0; k
< terms
.size(); ++k
) {
1008 if (terms
[k
]->powers
== powers
) {
1009 eadd(f2
, terms
[k
]->E
);
1010 free_evalue_refs(f2
);
1015 if (k
>= terms
.size()) {
1016 E_poly_term
*ET
= new E_poly_term
;
1017 ET
->powers
= powers
;
1019 terms
.push_back(ET
);
1023 struct ienumerator
: public signed_cone_consumer
, public vertex_decomposer
,
1024 public ienumerator_base
{
1031 ienumerator(Polyhedron
*P
, unsigned dim
, unsigned nbV
) :
1032 vertex_decomposer(P
, nbV
, *this), ienumerator_base(dim
, this) {
1033 vertex
.SetDims(1, dim
);
1035 den
.SetDims(dim
, dim
);
1045 virtual void handle(const signed_cone
& sc
, barvinok_options
*options
);
1046 void reduce(evalue
*factor
, const mat_ZZ
& num
, const mat_ZZ
& den_f
,
1047 barvinok_options
*options
);
1050 void ienumerator::reduce(evalue
*factor
, const mat_ZZ
& num
, const mat_ZZ
& den_f
,
1051 barvinok_options
*options
)
1053 unsigned len
= den_f
.NumRows(); // number of factors in den
1054 unsigned dim
= num
.NumCols();
1055 assert(num
.NumRows() == 1);
1058 eadd(factor
, vE
[vert
]);
1067 split_one(num
, num_s
, num_p
, den_f
, den_s
, den_r
);
1070 den_p
.SetLength(len
);
1074 normalize(one
, num_s
, num_p
, den_s
, den_p
, den_r
);
1076 emul(&mone
, factor
);
1080 for (int k
= 0; k
< len
; ++k
) {
1083 else if (den_s
[k
] == 0)
1086 if (no_param
== 0) {
1087 reduce(factor
, num_p
, den_r
, options
);
1091 pden
.SetDims(only_param
, dim
-1);
1093 for (k
= 0, l
= 0; k
< len
; ++k
)
1095 pden
[l
++] = den_r
[k
];
1097 for (k
= 0; k
< len
; ++k
)
1101 zz2value(num_s
[0], tz
);
1102 dpoly
n(no_param
, tz
);
1103 zz2value(den_s
[k
], tz
);
1104 dpoly
D(no_param
, tz
, 1);
1105 for ( ; ++k
< len
; )
1106 if (den_p
[k
] == 0) {
1107 zz2value(den_s
[k
], tz
);
1108 dpoly
fact(no_param
, tz
, 1);
1113 // if no_param + only_param == len then all powers
1114 // below will be all zero
1115 if (no_param
+ only_param
== len
) {
1116 if (E_num(0, dim
) != 0)
1117 r
= new dpoly_r(n
, len
);
1119 mpq_set_si(tcount
, 0, 1);
1121 n
.div(D
, tcount
, one
);
1123 if (value_notzero_p(mpq_numref(tcount
))) {
1127 value_assign(f
.x
.n
, mpq_numref(tcount
));
1128 value_assign(f
.d
, mpq_denref(tcount
));
1130 reduce(factor
, num_p
, pden
, options
);
1131 free_evalue_refs(&f
);
1136 for (k
= 0; k
< len
; ++k
) {
1137 if (den_s
[k
] == 0 || den_p
[k
] == 0)
1140 zz2value(den_s
[k
], tz
);
1141 dpoly
pd(no_param
-1, tz
, 1);
1144 for (l
= 0; l
< k
; ++l
)
1145 if (den_r
[l
] == den_r
[k
])
1149 r
= new dpoly_r(n
, pd
, l
, len
);
1151 dpoly_r
*nr
= new dpoly_r(r
, pd
, l
, len
);
1157 dpoly_r
*rc
= r
->div(D
);
1160 if (E_num(0, dim
) == 0) {
1161 int common
= pden
.NumRows();
1162 dpoly_r_term_list
& final
= r
->c
[r
->len
-1];
1168 zz2value(r
->denom
, f
.d
);
1169 dpoly_r_term_list::iterator j
;
1170 for (j
= final
.begin(); j
!= final
.end(); ++j
) {
1171 if ((*j
)->coeff
== 0)
1174 for (int k
= 0; k
< r
->dim
; ++k
) {
1175 int n
= (*j
)->powers
[k
];
1178 pden
.SetDims(rows
+n
, pden
.NumCols());
1179 for (int l
= 0; l
< n
; ++l
)
1180 pden
[rows
+l
] = den_r
[k
];
1184 evalue_copy(&t
, factor
);
1185 zz2value((*j
)->coeff
, f
.x
.n
);
1187 reduce(&t
, num_p
, pden
, options
);
1188 free_evalue_refs(&t
);
1190 free_evalue_refs(&f
);
1192 ie_cum
cum(factor
, E_num(0, dim
), r
);
1193 cum
.cumulate(options
);
1195 int common
= pden
.NumRows();
1197 for (int j
= 0; j
< cum
.terms
.size(); ++j
) {
1199 pden
.SetDims(rows
, pden
.NumCols());
1200 for (int k
= 0; k
< r
->dim
; ++k
) {
1201 int n
= cum
.terms
[j
]->powers
[k
];
1204 pden
.SetDims(rows
+n
, pden
.NumCols());
1205 for (int l
= 0; l
< n
; ++l
)
1206 pden
[rows
+l
] = den_r
[k
];
1209 reduce(cum
.terms
[j
]->E
, num_p
, pden
, options
);
1210 free_evalue_refs(cum
.terms
[j
]->E
);
1211 delete cum
.terms
[j
]->E
;
1212 delete cum
.terms
[j
];
1219 static int type_offset(enode
*p
)
1221 return p
->type
== fractional
? 1 :
1222 p
->type
== flooring
? 1 : 0;
1225 static int edegree(evalue
*e
)
1230 if (value_notzero_p(e
->d
))
1234 int i
= type_offset(p
);
1235 if (p
->size
-i
-1 > d
)
1236 d
= p
->size
- i
- 1;
1237 for (; i
< p
->size
; i
++) {
1238 int d2
= edegree(&p
->arr
[i
]);
1245 void ienumerator::handle(const signed_cone
& sc
, barvinok_options
*options
)
1247 assert(sc
.det
== 1);
1249 assert(sc
.rays
.NumRows() == dim
);
1251 lattice_point(V
, sc
.rays
, vertex
[0], E_vertex
, options
);
1257 evalue_set_si(&one
, sc
.sign
, 1);
1258 reduce(&one
, vertex
, den
, options
);
1259 free_evalue_refs(&one
);
1261 for (int i
= 0; i
< dim
; ++i
)
1263 free_evalue_refs(E_vertex
[i
]);
1268 struct bfenumerator
: public vertex_decomposer
, public bf_base
,
1269 public ienumerator_base
{
1272 bfenumerator(Polyhedron
*P
, unsigned dim
, unsigned nbV
) :
1273 vertex_decomposer(P
, nbV
, *this),
1274 bf_base(dim
), ienumerator_base(dim
, this) {
1282 virtual void handle(const signed_cone
& sc
, barvinok_options
*options
);
1283 virtual void base(mat_ZZ
& factors
, bfc_vec
& v
);
1285 bfc_term_base
* new_bf_term(int len
) {
1286 bfe_term
* t
= new bfe_term(len
);
1290 virtual void set_factor(bfc_term_base
*t
, int k
, int change
) {
1291 bfe_term
* bfet
= static_cast<bfe_term
*>(t
);
1292 factor
= bfet
->factors
[k
];
1293 assert(factor
!= NULL
);
1294 bfet
->factors
[k
] = NULL
;
1296 emul(&mone
, factor
);
1299 virtual void set_factor(bfc_term_base
*t
, int k
, mpq_t
&q
, int change
) {
1300 bfe_term
* bfet
= static_cast<bfe_term
*>(t
);
1301 factor
= bfet
->factors
[k
];
1302 assert(factor
!= NULL
);
1303 bfet
->factors
[k
] = NULL
;
1309 value_oppose(f
.x
.n
, mpq_numref(q
));
1311 value_assign(f
.x
.n
, mpq_numref(q
));
1312 value_assign(f
.d
, mpq_denref(q
));
1314 free_evalue_refs(&f
);
1317 virtual void set_factor(bfc_term_base
*t
, int k
, const QQ
& c
, int change
) {
1318 bfe_term
* bfet
= static_cast<bfe_term
*>(t
);
1320 factor
= new evalue
;
1325 zz2value(c
.n
, f
.x
.n
);
1327 value_oppose(f
.x
.n
, f
.x
.n
);
1330 value_init(factor
->d
);
1331 evalue_copy(factor
, bfet
->factors
[k
]);
1333 free_evalue_refs(&f
);
1336 void set_factor(evalue
*f
, int change
) {
1342 virtual void insert_term(bfc_term_base
*t
, int i
) {
1343 bfe_term
* bfet
= static_cast<bfe_term
*>(t
);
1344 int len
= t
->terms
.NumRows()-1; // already increased by one
1346 bfet
->factors
.resize(len
+1);
1347 for (int j
= len
; j
> i
; --j
) {
1348 bfet
->factors
[j
] = bfet
->factors
[j
-1];
1349 t
->terms
[j
] = t
->terms
[j
-1];
1351 bfet
->factors
[i
] = factor
;
1355 virtual void update_term(bfc_term_base
*t
, int i
) {
1356 bfe_term
* bfet
= static_cast<bfe_term
*>(t
);
1358 eadd(factor
, bfet
->factors
[i
]);
1359 free_evalue_refs(factor
);
1363 virtual bool constant_vertex(int dim
) { return E_num(0, dim
) == 0; }
1365 virtual void cum(bf_reducer
*bfr
, bfc_term_base
*t
, int k
, dpoly_r
*r
,
1366 barvinok_options
*options
);
1369 enumerator_base
*enumerator_base::create(Polyhedron
*P
, unsigned dim
, unsigned nbV
,
1370 barvinok_options
*options
)
1372 enumerator_base
*eb
;
1374 if (options
->incremental_specialization
== BV_SPECIALIZATION_BF
)
1375 eb
= new bfenumerator(P
, dim
, nbV
);
1376 else if (options
->incremental_specialization
== BV_SPECIALIZATION_DF
)
1377 eb
= new ienumerator(P
, dim
, nbV
);
1379 eb
= new enumerator(P
, dim
, nbV
);
1384 struct bfe_cum
: public cumulator
{
1386 bfc_term_base
*told
;
1390 bfe_cum(evalue
*factor
, evalue
*v
, dpoly_r
*r
, bf_reducer
*bfr
,
1391 bfc_term_base
*t
, int k
, bfenumerator
*e
) :
1392 cumulator(factor
, v
, r
), told(t
), k(k
),
1396 virtual void add_term(const vector
<int>& powers
, evalue
*f2
);
1399 void bfe_cum::add_term(const vector
<int>& powers
, evalue
*f2
)
1401 bfr
->update_powers(powers
);
1403 bfc_term_base
* t
= bfe
->find_bfc_term(bfr
->vn
, bfr
->npowers
, bfr
->nnf
);
1404 bfe
->set_factor(f2
, bfr
->l_changes
% 2);
1405 bfe
->add_term(t
, told
->terms
[k
], bfr
->l_extra_num
);
1408 void bfenumerator::cum(bf_reducer
*bfr
, bfc_term_base
*t
, int k
,
1409 dpoly_r
*r
, barvinok_options
*options
)
1411 bfe_term
* bfet
= static_cast<bfe_term
*>(t
);
1412 bfe_cum
cum(bfet
->factors
[k
], E_num(0, bfr
->d
), r
, bfr
, t
, k
, this);
1413 cum
.cumulate(options
);
1416 void bfenumerator::base(mat_ZZ
& factors
, bfc_vec
& v
)
1418 for (int i
= 0; i
< v
.size(); ++i
) {
1419 assert(v
[i
]->terms
.NumRows() == 1);
1420 evalue
*factor
= static_cast<bfe_term
*>(v
[i
])->factors
[0];
1421 eadd(factor
, vE
[vert
]);
1426 void bfenumerator::handle(const signed_cone
& sc
, barvinok_options
*options
)
1428 assert(sc
.det
== 1);
1430 assert(sc
.rays
.NumRows() == enumerator_base::dim
);
1432 bfe_term
* t
= new bfe_term(enumerator_base::dim
);
1433 vector
< bfc_term_base
* > v
;
1436 t
->factors
.resize(1);
1438 t
->terms
.SetDims(1, enumerator_base::dim
);
1439 lattice_point(V
, sc
.rays
, t
->terms
[0], E_vertex
, options
);
1441 // the elements of factors are always lexpositive
1443 int s
= setup_factors(sc
.rays
, factors
, t
, sc
.sign
);
1445 t
->factors
[0] = new evalue
;
1446 value_init(t
->factors
[0]->d
);
1447 evalue_set_si(t
->factors
[0], s
, 1);
1448 reduce(factors
, v
, options
);
1450 for (int i
= 0; i
< enumerator_base::dim
; ++i
)
1452 free_evalue_refs(E_vertex
[i
]);
1457 static evalue
* barvinok_enumerate_ev_f(Polyhedron
*P
, Polyhedron
* C
,
1458 barvinok_options
*options
);
1461 static evalue
* barvinok_enumerate_cst(Polyhedron
*P
, Polyhedron
* C
,
1462 struct barvinok_options
*options
)
1466 ALLOC(evalue
, eres
);
1467 value_init(eres
->d
);
1468 value_set_si(eres
->d
, 0);
1469 eres
->x
.p
= new_enode(partition
, 2, C
->Dimension
);
1470 EVALUE_SET_DOMAIN(eres
->x
.p
->arr
[0],
1471 DomainConstraintSimplify(C
, options
->MaxRays
));
1472 value_set_si(eres
->x
.p
->arr
[1].d
, 1);
1473 value_init(eres
->x
.p
->arr
[1].x
.n
);
1475 value_set_si(eres
->x
.p
->arr
[1].x
.n
, 0);
1477 barvinok_count_with_options(P
, &eres
->x
.p
->arr
[1].x
.n
, options
);
1483 static evalue
* enumerate(Polyhedron
*P
, Polyhedron
* C
,
1484 struct barvinok_options
*options
)
1486 //P = unfringe(P, MaxRays);
1488 Polyhedron
*Corig
= C
;
1489 Polyhedron
*CEq
= NULL
, *rVD
;
1491 unsigned nparam
= C
->Dimension
;
1496 value_init(factor
.d
);
1497 evalue_set_si(&factor
, 1, 1);
1500 POL_ENSURE_FACETS(P
);
1501 POL_ENSURE_VERTICES(P
);
1502 POL_ENSURE_FACETS(C
);
1503 POL_ENSURE_VERTICES(C
);
1505 if (C
->Dimension
== 0 || emptyQ(P
)) {
1507 eres
= barvinok_enumerate_cst(P
, CEq
? CEq
: Polyhedron_Copy(C
), options
);
1510 evalue_backsubstitute(eres
, CP
, options
->MaxRays
);
1514 emul(&factor
, eres
);
1515 if (options
->approximation_method
== BV_APPROX_DROP
) {
1516 if (options
->polynomial_approximation
== BV_APPROX_SIGN_UPPER
)
1517 evalue_frac2polynomial(eres
, 1, options
->MaxRays
);
1518 if (options
->polynomial_approximation
== BV_APPROX_SIGN_LOWER
)
1519 evalue_frac2polynomial(eres
, -1, options
->MaxRays
);
1520 if (options
->polynomial_approximation
== BV_APPROX_SIGN_APPROX
)
1521 evalue_frac2polynomial(eres
, 0, options
->MaxRays
);
1523 reduce_evalue(eres
);
1524 free_evalue_refs(&factor
);
1531 if (Polyhedron_is_unbounded(P
, nparam
, options
->MaxRays
))
1536 P
= remove_equalities_p(P
, P
->Dimension
-nparam
, &f
, options
->MaxRays
);
1537 mask(f
, &factor
, options
);
1540 if (P
->Dimension
== nparam
) {
1542 P
= Universe_Polyhedron(0);
1548 remove_all_equalities(&Q
, &C
, &CP
, NULL
, nparam
, options
->MaxRays
);
1549 if (C
!= D
&& D
!= Corig
)
1551 eres
= enumerate(Q
, C
, options
);
1555 Polyhedron
*T
= Polyhedron_Factor(P
, nparam
, NULL
, options
->MaxRays
);
1556 if (T
|| (P
->Dimension
== nparam
+1)) {
1559 for (Q
= T
? T
: P
; Q
; Q
= Q
->next
) {
1560 Polyhedron
*next
= Q
->next
;
1564 if (Q
->Dimension
!= C
->Dimension
)
1565 QC
= Polyhedron_Project(Q
, nparam
);
1568 C
= DomainIntersection(C
, QC
, options
->MaxRays
);
1570 Polyhedron_Free(C2
);
1572 Polyhedron_Free(QC
);
1580 if (T
->Dimension
== C
->Dimension
) {
1589 eres
= barvinok_enumerate_ev_f(P
, C
, options
);
1596 for (Q
= P
->next
; Q
; Q
= Q
->next
) {
1597 Polyhedron
*next
= Q
->next
;
1600 f
= barvinok_enumerate_ev_f(Q
, C
, options
);
1602 free_evalue_refs(f
);
1612 evalue
* barvinok_enumerate_with_options(Polyhedron
*P
, Polyhedron
* C
,
1613 struct barvinok_options
*options
)
1615 Polyhedron
*next
, *Cnext
, *CA
;
1616 Polyhedron
*Porig
= P
;
1621 "barvinok_enumerate: input is a union; only first polyhedron is enumerated\n");
1625 "barvinok_enumerate: context is a union; only first polyhedron is considered\n");
1629 CA
= align_context(C
, P
->Dimension
, options
->MaxRays
);
1632 P
= DomainIntersection(P
, CA
, options
->MaxRays
);
1634 Polyhedron_Free(CA
);
1636 if (options
->approximation_method
== BV_APPROX_BERNOULLI
) {
1637 eres
= Bernoulli_sum(P
, C
, options
);
1640 eres
= enumerate(P
, C
, options
);
1647 evalue
* barvinok_enumerate_ev(Polyhedron
*P
, Polyhedron
* C
, unsigned MaxRays
)
1650 barvinok_options
*options
= barvinok_options_new_with_defaults();
1651 options
->MaxRays
= MaxRays
;
1652 E
= barvinok_enumerate_with_options(P
, C
, options
);
1653 barvinok_options_free(options
);
1657 evalue
*Param_Polyhedron_Enumerate(Param_Polyhedron
*PP
, Polyhedron
*P
,
1659 struct barvinok_options
*options
)
1663 unsigned nparam
= C
->Dimension
;
1664 unsigned dim
= P
->Dimension
- nparam
;
1666 ALLOC(evalue
, eres
);
1667 value_init(eres
->d
);
1668 value_set_si(eres
->d
, 0);
1671 for (nd
= 0, D
=PP
->D
; D
; ++nd
, D
=D
->next
);
1672 struct section
{ Polyhedron
*D
; evalue E
; };
1673 section
*s
= new section
[nd
];
1675 enumerator_base
*et
= NULL
;
1680 et
= enumerator_base::create(P
, dim
, PP
->nbV
, options
);
1682 Polyhedron
*TC
= true_context(P
, C
, options
->MaxRays
);
1683 FORALL_REDUCED_DOMAIN(PP
, TC
, nd
, options
, i
, D
, rVD
)
1686 value_init(s
[i
].E
.d
);
1687 evalue_set_si(&s
[i
].E
, 0, 1);
1690 FORALL_PVertex_in_ParamPolyhedron(V
,D
,PP
) // _i is internal counter
1693 et
->decompose_at(V
, _i
, options
);
1694 } catch (OrthogonalException
&e
) {
1695 FORALL_REDUCED_DOMAIN_RESET
;
1696 for (; i
>= 0; --i
) {
1697 free_evalue_refs(&s
[i
].E
);
1698 Domain_Free(s
[i
].D
);
1702 eadd(et
->vE
[_i
] , &s
[i
].E
);
1703 END_FORALL_PVertex_in_ParamPolyhedron
;
1704 evalue_range_reduction_in_domain(&s
[i
].E
, rVD
);
1705 END_FORALL_REDUCED_DOMAIN
1706 Polyhedron_Free(TC
);
1710 evalue_set_si(eres
, 0, 1);
1712 eres
->x
.p
= new_enode(partition
, 2*nd
, C
->Dimension
);
1713 for (int j
= 0; j
< nd
; ++j
) {
1714 EVALUE_SET_DOMAIN(eres
->x
.p
->arr
[2*j
], s
[j
].D
);
1715 value_clear(eres
->x
.p
->arr
[2*j
+1].d
);
1716 eres
->x
.p
->arr
[2*j
+1] = s
[j
].E
;
1724 static evalue
* barvinok_enumerate_ev_f(Polyhedron
*P
, Polyhedron
* C
,
1725 barvinok_options
*options
)
1727 unsigned nparam
= C
->Dimension
;
1728 bool do_scale
= options
->approximation_method
== BV_APPROX_SCALE
;
1730 if (options
->approximation_method
== BV_APPROX_VOLUME
)
1731 return Param_Polyhedron_Volume(P
, C
, options
);
1733 if (P
->Dimension
- nparam
== 1 && !do_scale
)
1734 return ParamLine_Length(P
, C
, options
);
1736 Param_Polyhedron
*PP
= NULL
;
1740 eres
= scale_bound(P
, C
, options
);
1745 PP
= Polyhedron2Param_Polyhedron(P
, C
, options
);
1748 eres
= scale(PP
, P
, C
, options
);
1750 eres
= Param_Polyhedron_Enumerate(PP
, P
, C
, options
);
1753 Param_Polyhedron_Free(PP
);
1758 Enumeration
* barvinok_enumerate(Polyhedron
*P
, Polyhedron
* C
, unsigned MaxRays
)
1760 evalue
*EP
= barvinok_enumerate_ev(P
, C
, MaxRays
);
1762 return partition2enumeration(EP
);
1765 static void SwapColumns(Value
**V
, int n
, int i
, int j
)
1767 for (int r
= 0; r
< n
; ++r
)
1768 value_swap(V
[r
][i
], V
[r
][j
]);
1771 static void SwapColumns(Polyhedron
*P
, int i
, int j
)
1773 SwapColumns(P
->Constraint
, P
->NbConstraints
, i
, j
);
1774 SwapColumns(P
->Ray
, P
->NbRays
, i
, j
);
1777 /* Construct a constraint c from constraints l and u such that if
1778 * if constraint c holds then for each value of the other variables
1779 * there is at most one value of variable pos (position pos+1 in the constraints).
1781 * Given a lower and an upper bound
1782 * n_l v_i + <c_l,x> + c_l >= 0
1783 * -n_u v_i + <c_u,x> + c_u >= 0
1784 * the constructed constraint is
1786 * -(n_l<c_u,x> + n_u<c_l,x>) + (-n_l c_u - n_u c_l + n_l n_u - 1)
1788 * which is then simplified to remove the content of the non-constant coefficients
1790 * len is the total length of the constraints.
1791 * v is a temporary variable that can be used by this procedure
1793 static void negative_test_constraint(Value
*l
, Value
*u
, Value
*c
, int pos
,
1796 value_oppose(*v
, u
[pos
+1]);
1797 Vector_Combine(l
+1, u
+1, c
+1, *v
, l
[pos
+1], len
-1);
1798 value_multiply(*v
, *v
, l
[pos
+1]);
1799 value_subtract(c
[len
-1], c
[len
-1], *v
);
1800 value_set_si(*v
, -1);
1801 Vector_Scale(c
+1, c
+1, *v
, len
-1);
1802 value_decrement(c
[len
-1], c
[len
-1]);
1803 ConstraintSimplify(c
, c
, len
, v
);
1806 static bool parallel_constraints(Value
*l
, Value
*u
, Value
*c
, int pos
,
1815 Vector_Gcd(&l
[1+pos
], len
, &g1
);
1816 Vector_Gcd(&u
[1+pos
], len
, &g2
);
1817 Vector_Combine(l
+1+pos
, u
+1+pos
, c
+1, g2
, g1
, len
);
1818 parallel
= First_Non_Zero(c
+1, len
) == -1;
1826 static void negative_test_constraint7(Value
*l
, Value
*u
, Value
*c
, int pos
,
1827 int exist
, int len
, Value
*v
)
1832 Vector_Gcd(&u
[1+pos
], exist
, v
);
1833 Vector_Gcd(&l
[1+pos
], exist
, &g
);
1834 Vector_Combine(l
+1, u
+1, c
+1, *v
, g
, len
-1);
1835 value_multiply(*v
, *v
, g
);
1836 value_subtract(c
[len
-1], c
[len
-1], *v
);
1837 value_set_si(*v
, -1);
1838 Vector_Scale(c
+1, c
+1, *v
, len
-1);
1839 value_decrement(c
[len
-1], c
[len
-1]);
1840 ConstraintSimplify(c
, c
, len
, v
);
1845 /* Turns a x + b >= 0 into a x + b <= -1
1847 * len is the total length of the constraint.
1848 * v is a temporary variable that can be used by this procedure
1850 static void oppose_constraint(Value
*c
, int len
, Value
*v
)
1852 value_set_si(*v
, -1);
1853 Vector_Scale(c
+1, c
+1, *v
, len
-1);
1854 value_decrement(c
[len
-1], c
[len
-1]);
1857 /* Split polyhedron P into two polyhedra *pos and *neg, where
1858 * existential variable i has at most one solution for each
1859 * value of the other variables in *neg.
1861 * The splitting is performed using constraints l and u.
1863 * nvar: number of set variables
1864 * row: temporary vector that can be used by this procedure
1865 * f: temporary value that can be used by this procedure
1867 static bool SplitOnConstraint(Polyhedron
*P
, int i
, int l
, int u
,
1868 int nvar
, int MaxRays
, Vector
*row
, Value
& f
,
1869 Polyhedron
**pos
, Polyhedron
**neg
)
1871 negative_test_constraint(P
->Constraint
[l
], P
->Constraint
[u
],
1872 row
->p
, nvar
+i
, P
->Dimension
+2, &f
);
1873 *neg
= AddConstraints(row
->p
, 1, P
, MaxRays
);
1874 POL_ENSURE_VERTICES(*neg
);
1876 /* We found an independent, but useless constraint
1877 * Maybe we should detect this earlier and not
1878 * mark the variable as INDEPENDENT
1880 if (emptyQ((*neg
))) {
1881 Polyhedron_Free(*neg
);
1885 oppose_constraint(row
->p
, P
->Dimension
+2, &f
);
1886 *pos
= AddConstraints(row
->p
, 1, P
, MaxRays
);
1887 POL_ENSURE_VERTICES(*pos
);
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 POL_ENSURE_VERTICES(*pos
);
2009 value_set_si(f
, -1);
2010 Vector_Scale(row
->p
+1, row
->p
+1, f
, P
->Dimension
+1);
2011 value_decrement(row
->p
[P
->Dimension
+1], row
->p
[P
->Dimension
+1]);
2012 *neg
= AddConstraints(row
->p
, 1, P
, 0);
2013 POL_ENSURE_VERTICES(*neg
);
2017 return !emptyQ((*pos
)) && !emptyQ((*neg
));
2020 static bool double_bound(Polyhedron
*P
, int nvar
, int exist
,
2021 Polyhedron
**pos
, Polyhedron
**neg
)
2023 for (int i
= 0; i
< exist
; ++i
) {
2025 for (l1
= P
->NbEq
; l1
< P
->NbConstraints
; ++l1
) {
2026 if (value_negz_p(P
->Constraint
[l1
][nvar
+i
+1]))
2028 for (l2
= l1
+ 1; l2
< P
->NbConstraints
; ++l2
) {
2029 if (value_negz_p(P
->Constraint
[l2
][nvar
+i
+1]))
2031 if (double_bound_pair(P
, nvar
, exist
, i
, l1
, l2
, pos
, neg
))
2035 for (l1
= P
->NbEq
; l1
< P
->NbConstraints
; ++l1
) {
2036 if (value_posz_p(P
->Constraint
[l1
][nvar
+i
+1]))
2038 if (l1
< P
->NbConstraints
)
2039 for (l2
= l1
+ 1; l2
< P
->NbConstraints
; ++l2
) {
2040 if (value_posz_p(P
->Constraint
[l2
][nvar
+i
+1]))
2042 if (double_bound_pair(P
, nvar
, exist
, i
, l1
, l2
, pos
, neg
))
2054 INDEPENDENT
= 1 << 2,
2058 static evalue
* enumerate_or(Polyhedron
*D
,
2059 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2062 fprintf(stderr
, "\nER: Or\n");
2063 #endif /* DEBUG_ER */
2065 Polyhedron
*N
= D
->next
;
2068 barvinok_enumerate_e_with_options(D
, exist
, nparam
, options
);
2071 for (D
= N
; D
; D
= N
) {
2076 barvinok_enumerate_e_with_options(D
, exist
, nparam
, options
);
2079 free_evalue_refs(EN
);
2089 static evalue
* enumerate_sum(Polyhedron
*P
,
2090 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2092 int nvar
= P
->Dimension
- exist
- nparam
;
2093 int toswap
= nvar
< exist
? nvar
: exist
;
2094 for (int i
= 0; i
< toswap
; ++i
)
2095 SwapColumns(P
, 1 + i
, nvar
+exist
- i
);
2099 fprintf(stderr
, "\nER: Sum\n");
2100 #endif /* DEBUG_ER */
2102 evalue
*EP
= barvinok_enumerate_e_with_options(P
, exist
, nparam
, options
);
2104 evalue_split_domains_into_orthants(EP
, options
->MaxRays
);
2106 evalue_range_reduction(EP
);
2108 evalue_frac2floor(EP
);
2110 evalue
*sum
= evalue_sum(EP
, nvar
, options
->MaxRays
);
2112 free_evalue_refs(EP
);
2116 evalue_range_reduction(EP
);
2121 static evalue
* split_sure(Polyhedron
*P
, Polyhedron
*S
,
2122 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2124 int nvar
= P
->Dimension
- exist
- nparam
;
2126 Matrix
*M
= Matrix_Alloc(exist
, S
->Dimension
+2);
2127 for (int i
= 0; i
< exist
; ++i
)
2128 value_set_si(M
->p
[i
][nvar
+i
+1], 1);
2130 S
= DomainAddRays(S
, M
, options
->MaxRays
);
2132 Polyhedron
*F
= DomainAddRays(P
, M
, options
->MaxRays
);
2133 Polyhedron
*D
= DomainDifference(F
, S
, options
->MaxRays
);
2135 D
= Disjoint_Domain(D
, 0, options
->MaxRays
);
2140 M
= Matrix_Alloc(P
->Dimension
+1-exist
, P
->Dimension
+1);
2141 for (int j
= 0; j
< nvar
; ++j
)
2142 value_set_si(M
->p
[j
][j
], 1);
2143 for (int j
= 0; j
< nparam
+1; ++j
)
2144 value_set_si(M
->p
[nvar
+j
][nvar
+exist
+j
], 1);
2145 Polyhedron
*T
= Polyhedron_Image(S
, M
, options
->MaxRays
);
2146 evalue
*EP
= barvinok_enumerate_e_with_options(T
, 0, nparam
, options
);
2151 for (Polyhedron
*Q
= D
; Q
; Q
= Q
->next
) {
2152 Polyhedron
*N
= Q
->next
;
2154 T
= DomainIntersection(P
, Q
, options
->MaxRays
);
2155 evalue
*E
= barvinok_enumerate_e_with_options(T
, exist
, nparam
, options
);
2157 free_evalue_refs(E
);
2166 static evalue
* enumerate_sure(Polyhedron
*P
,
2167 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2171 int nvar
= P
->Dimension
- exist
- nparam
;
2177 for (i
= 0; i
< exist
; ++i
) {
2178 Matrix
*M
= Matrix_Alloc(S
->NbConstraints
, S
->Dimension
+2);
2180 value_set_si(lcm
, 1);
2181 for (int j
= 0; j
< S
->NbConstraints
; ++j
) {
2182 if (value_negz_p(S
->Constraint
[j
][1+nvar
+i
]))
2184 if (value_one_p(S
->Constraint
[j
][1+nvar
+i
]))
2186 value_lcm(lcm
, S
->Constraint
[j
][1+nvar
+i
], &lcm
);
2189 for (int j
= 0; j
< S
->NbConstraints
; ++j
) {
2190 if (value_negz_p(S
->Constraint
[j
][1+nvar
+i
]))
2192 if (value_one_p(S
->Constraint
[j
][1+nvar
+i
]))
2194 value_division(f
, lcm
, S
->Constraint
[j
][1+nvar
+i
]);
2195 Vector_Scale(S
->Constraint
[j
], M
->p
[c
], f
, S
->Dimension
+2);
2196 value_subtract(M
->p
[c
][S
->Dimension
+1],
2197 M
->p
[c
][S
->Dimension
+1],
2199 value_increment(M
->p
[c
][S
->Dimension
+1],
2200 M
->p
[c
][S
->Dimension
+1]);
2204 S
= AddConstraints(M
->p
[0], c
, S
, options
->MaxRays
);
2219 fprintf(stderr
, "\nER: Sure\n");
2220 #endif /* DEBUG_ER */
2222 return split_sure(P
, S
, exist
, nparam
, options
);
2225 static evalue
* enumerate_sure2(Polyhedron
*P
,
2226 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2228 int nvar
= P
->Dimension
- exist
- nparam
;
2230 for (r
= 0; r
< P
->NbRays
; ++r
)
2231 if (value_one_p(P
->Ray
[r
][0]) &&
2232 value_one_p(P
->Ray
[r
][P
->Dimension
+1]))
2238 Matrix
*M
= Matrix_Alloc(nvar
+ 1 + nparam
, P
->Dimension
+2);
2239 for (int i
= 0; i
< nvar
; ++i
)
2240 value_set_si(M
->p
[i
][1+i
], 1);
2241 for (int i
= 0; i
< nparam
; ++i
)
2242 value_set_si(M
->p
[i
+nvar
][1+nvar
+exist
+i
], 1);
2243 Vector_Copy(P
->Ray
[r
]+1+nvar
, M
->p
[nvar
+nparam
]+1+nvar
, exist
);
2244 value_set_si(M
->p
[nvar
+nparam
][0], 1);
2245 value_set_si(M
->p
[nvar
+nparam
][P
->Dimension
+1], 1);
2246 Polyhedron
* F
= Rays2Polyhedron(M
, options
->MaxRays
);
2249 Polyhedron
*I
= DomainIntersection(F
, P
, options
->MaxRays
);
2253 fprintf(stderr
, "\nER: Sure2\n");
2254 #endif /* DEBUG_ER */
2256 return split_sure(P
, I
, exist
, nparam
, options
);
2259 static evalue
* enumerate_cyclic(Polyhedron
*P
,
2260 unsigned exist
, unsigned nparam
,
2261 evalue
* EP
, int r
, int p
, unsigned MaxRays
)
2263 int nvar
= P
->Dimension
- exist
- nparam
;
2265 /* If EP in its fractional maps only contains references
2266 * to the remainder parameter with appropriate coefficients
2267 * then we could in principle avoid adding existentially
2268 * quantified variables to the validity domains.
2269 * We'd have to replace the remainder by m { p/m }
2270 * and multiply with an appropriate factor that is one
2271 * only in the appropriate range.
2272 * This last multiplication can be avoided if EP
2273 * has a single validity domain with no (further)
2274 * constraints on the remainder parameter
2277 Matrix
*CT
= Matrix_Alloc(nparam
+1, nparam
+3);
2278 Matrix
*M
= Matrix_Alloc(1, 1+nparam
+3);
2279 for (int j
= 0; j
< nparam
; ++j
)
2281 value_set_si(CT
->p
[j
][j
], 1);
2282 value_set_si(CT
->p
[p
][nparam
+1], 1);
2283 value_set_si(CT
->p
[nparam
][nparam
+2], 1);
2284 value_set_si(M
->p
[0][1+p
], -1);
2285 value_absolute(M
->p
[0][1+nparam
], P
->Ray
[0][1+nvar
+exist
+p
]);
2286 value_set_si(M
->p
[0][1+nparam
+1], 1);
2287 Polyhedron
*CEq
= Constraints2Polyhedron(M
, 1);
2289 addeliminatedparams_enum(EP
, CT
, CEq
, MaxRays
, nparam
);
2290 Polyhedron_Free(CEq
);
2296 static void enumerate_vd_add_ray(evalue
*EP
, Matrix
*Rays
, unsigned MaxRays
)
2298 if (value_notzero_p(EP
->d
))
2301 assert(EP
->x
.p
->type
== partition
);
2302 assert(EP
->x
.p
->pos
== EVALUE_DOMAIN(EP
->x
.p
->arr
[0])->Dimension
);
2303 for (int i
= 0; i
< EP
->x
.p
->size
/2; ++i
) {
2304 Polyhedron
*D
= EVALUE_DOMAIN(EP
->x
.p
->arr
[2*i
]);
2305 Polyhedron
*N
= DomainAddRays(D
, Rays
, MaxRays
);
2306 EVALUE_SET_DOMAIN(EP
->x
.p
->arr
[2*i
], N
);
2311 static evalue
* enumerate_line(Polyhedron
*P
,
2312 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2318 fprintf(stderr
, "\nER: Line\n");
2319 #endif /* DEBUG_ER */
2321 int nvar
= P
->Dimension
- exist
- nparam
;
2323 for (i
= 0; i
< nparam
; ++i
)
2324 if (value_notzero_p(P
->Ray
[0][1+nvar
+exist
+i
]))
2327 for (j
= i
+1; j
< nparam
; ++j
)
2328 if (value_notzero_p(P
->Ray
[0][1+nvar
+exist
+i
]))
2330 assert(j
>= nparam
); // for now
2332 Matrix
*M
= Matrix_Alloc(2, P
->Dimension
+2);
2333 value_set_si(M
->p
[0][0], 1);
2334 value_set_si(M
->p
[0][1+nvar
+exist
+i
], 1);
2335 value_set_si(M
->p
[1][0], 1);
2336 value_set_si(M
->p
[1][1+nvar
+exist
+i
], -1);
2337 value_absolute(M
->p
[1][1+P
->Dimension
], P
->Ray
[0][1+nvar
+exist
+i
]);
2338 value_decrement(M
->p
[1][1+P
->Dimension
], M
->p
[1][1+P
->Dimension
]);
2339 Polyhedron
*S
= AddConstraints(M
->p
[0], 2, P
, options
->MaxRays
);
2340 evalue
*EP
= barvinok_enumerate_e_with_options(S
, exist
, nparam
, options
);
2344 return enumerate_cyclic(P
, exist
, nparam
, EP
, 0, i
, options
->MaxRays
);
2347 static int single_param_pos(Polyhedron
*P
, unsigned exist
, unsigned nparam
,
2350 int nvar
= P
->Dimension
- exist
- nparam
;
2351 if (First_Non_Zero(P
->Ray
[r
]+1, nvar
) != -1)
2353 int i
= First_Non_Zero(P
->Ray
[r
]+1+nvar
+exist
, nparam
);
2356 if (First_Non_Zero(P
->Ray
[r
]+1+nvar
+exist
+1, nparam
-i
-1) != -1)
2361 static evalue
* enumerate_remove_ray(Polyhedron
*P
, int r
,
2362 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2365 fprintf(stderr
, "\nER: RedundantRay\n");
2366 #endif /* DEBUG_ER */
2370 value_set_si(one
, 1);
2371 int len
= P
->NbRays
-1;
2372 Matrix
*M
= Matrix_Alloc(2 * len
, P
->Dimension
+2);
2373 Vector_Copy(P
->Ray
[0], M
->p
[0], r
* (P
->Dimension
+2));
2374 Vector_Copy(P
->Ray
[r
+1], M
->p
[r
], (len
-r
) * (P
->Dimension
+2));
2375 for (int j
= 0; j
< P
->NbRays
; ++j
) {
2378 Vector_Combine(P
->Ray
[j
], P
->Ray
[r
], M
->p
[len
+j
-(j
>r
)],
2379 one
, P
->Ray
[j
][P
->Dimension
+1], P
->Dimension
+2);
2382 P
= Rays2Polyhedron(M
, options
->MaxRays
);
2384 evalue
*EP
= barvinok_enumerate_e_with_options(P
, exist
, nparam
, options
);
2391 static evalue
* enumerate_redundant_ray(Polyhedron
*P
,
2392 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2394 assert(P
->NbBid
== 0);
2395 int nvar
= P
->Dimension
- exist
- nparam
;
2399 for (int r
= 0; r
< P
->NbRays
; ++r
) {
2400 if (value_notzero_p(P
->Ray
[r
][P
->Dimension
+1]))
2402 int i1
= single_param_pos(P
, exist
, nparam
, r
);
2405 for (int r2
= r
+1; r2
< P
->NbRays
; ++r2
) {
2406 if (value_notzero_p(P
->Ray
[r2
][P
->Dimension
+1]))
2408 int i2
= single_param_pos(P
, exist
, nparam
, r2
);
2414 value_division(m
, P
->Ray
[r
][1+nvar
+exist
+i1
],
2415 P
->Ray
[r2
][1+nvar
+exist
+i1
]);
2416 value_multiply(m
, m
, P
->Ray
[r2
][1+nvar
+exist
+i1
]);
2417 /* r2 divides r => r redundant */
2418 if (value_eq(m
, P
->Ray
[r
][1+nvar
+exist
+i1
])) {
2420 return enumerate_remove_ray(P
, r
, exist
, nparam
, options
);
2423 value_division(m
, P
->Ray
[r2
][1+nvar
+exist
+i1
],
2424 P
->Ray
[r
][1+nvar
+exist
+i1
]);
2425 value_multiply(m
, m
, P
->Ray
[r
][1+nvar
+exist
+i1
]);
2426 /* r divides r2 => r2 redundant */
2427 if (value_eq(m
, P
->Ray
[r2
][1+nvar
+exist
+i1
])) {
2429 return enumerate_remove_ray(P
, r2
, exist
, nparam
, options
);
2437 static Polyhedron
*upper_bound(Polyhedron
*P
,
2438 int pos
, Value
*max
, Polyhedron
**R
)
2447 for (Polyhedron
*Q
= P
; Q
; Q
= N
) {
2449 for (r
= 0; r
< P
->NbRays
; ++r
) {
2450 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1]) &&
2451 value_pos_p(P
->Ray
[r
][1+pos
]))
2454 if (r
< P
->NbRays
) {
2462 for (r
= 0; r
< P
->NbRays
; ++r
) {
2463 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1]))
2465 mpz_fdiv_q(v
, P
->Ray
[r
][1+pos
], P
->Ray
[r
][1+P
->Dimension
]);
2466 if ((!Q
->next
&& r
== 0) || value_gt(v
, *max
))
2467 value_assign(*max
, v
);
2474 static evalue
* enumerate_ray(Polyhedron
*P
,
2475 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2477 assert(P
->NbBid
== 0);
2478 int nvar
= P
->Dimension
- exist
- nparam
;
2481 for (r
= 0; r
< P
->NbRays
; ++r
)
2482 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1]))
2488 for (r2
= r
+1; r2
< P
->NbRays
; ++r2
)
2489 if (value_zero_p(P
->Ray
[r2
][P
->Dimension
+1]))
2491 if (r2
< P
->NbRays
) {
2493 return enumerate_sum(P
, exist
, nparam
, options
);
2497 fprintf(stderr
, "\nER: Ray\n");
2498 #endif /* DEBUG_ER */
2504 value_set_si(one
, 1);
2505 int i
= single_param_pos(P
, exist
, nparam
, r
);
2506 assert(i
!= -1); // for now;
2508 Matrix
*M
= Matrix_Alloc(P
->NbRays
, P
->Dimension
+2);
2509 for (int j
= 0; j
< P
->NbRays
; ++j
) {
2510 Vector_Combine(P
->Ray
[j
], P
->Ray
[r
], M
->p
[j
],
2511 one
, P
->Ray
[j
][P
->Dimension
+1], P
->Dimension
+2);
2513 Polyhedron
*S
= Rays2Polyhedron(M
, options
->MaxRays
);
2515 Polyhedron
*D
= DomainDifference(P
, S
, options
->MaxRays
);
2517 // Polyhedron_Print(stderr, P_VALUE_FMT, D);
2518 assert(value_pos_p(P
->Ray
[r
][1+nvar
+exist
+i
])); // for now
2520 D
= upper_bound(D
, nvar
+exist
+i
, &m
, &R
);
2524 M
= Matrix_Alloc(2, P
->Dimension
+2);
2525 value_set_si(M
->p
[0][0], 1);
2526 value_set_si(M
->p
[1][0], 1);
2527 value_set_si(M
->p
[0][1+nvar
+exist
+i
], -1);
2528 value_set_si(M
->p
[1][1+nvar
+exist
+i
], 1);
2529 value_assign(M
->p
[0][1+P
->Dimension
], m
);
2530 value_oppose(M
->p
[1][1+P
->Dimension
], m
);
2531 value_addto(M
->p
[1][1+P
->Dimension
], M
->p
[1][1+P
->Dimension
],
2532 P
->Ray
[r
][1+nvar
+exist
+i
]);
2533 value_decrement(M
->p
[1][1+P
->Dimension
], M
->p
[1][1+P
->Dimension
]);
2534 // Matrix_Print(stderr, P_VALUE_FMT, M);
2535 D
= AddConstraints(M
->p
[0], 2, P
, options
->MaxRays
);
2536 // Polyhedron_Print(stderr, P_VALUE_FMT, D);
2537 value_subtract(M
->p
[0][1+P
->Dimension
], M
->p
[0][1+P
->Dimension
],
2538 P
->Ray
[r
][1+nvar
+exist
+i
]);
2539 // Matrix_Print(stderr, P_VALUE_FMT, M);
2540 S
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
2541 // Polyhedron_Print(stderr, P_VALUE_FMT, S);
2544 evalue
*EP
= barvinok_enumerate_e_with_options(D
, exist
, nparam
, options
);
2549 if (value_notone_p(P
->Ray
[r
][1+nvar
+exist
+i
]))
2550 EP
= enumerate_cyclic(P
, exist
, nparam
, EP
, r
, i
, options
->MaxRays
);
2552 M
= Matrix_Alloc(1, nparam
+2);
2553 value_set_si(M
->p
[0][0], 1);
2554 value_set_si(M
->p
[0][1+i
], 1);
2555 enumerate_vd_add_ray(EP
, M
, options
->MaxRays
);
2560 evalue
*E
= barvinok_enumerate_e_with_options(S
, exist
, nparam
, options
);
2562 free_evalue_refs(E
);
2569 evalue
*ER
= enumerate_or(R
, exist
, nparam
, options
);
2571 free_evalue_refs(ER
);
2578 static evalue
* enumerate_vd(Polyhedron
**PA
,
2579 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2581 Polyhedron
*P
= *PA
;
2582 int nvar
= P
->Dimension
- exist
- nparam
;
2583 Param_Polyhedron
*PP
= NULL
;
2584 Polyhedron
*C
= Universe_Polyhedron(nparam
);
2588 PP
= Polyhedron2Param_Polyhedron(PR
, C
, options
);
2592 Param_Domain
*D
, *last
;
2595 for (nd
= 0, D
=PP
->D
; D
; D
=D
->next
, ++nd
)
2598 Polyhedron
**VD
= new Polyhedron_p
[nd
];
2599 Polyhedron
*TC
= true_context(P
, C
, options
->MaxRays
);
2600 FORALL_REDUCED_DOMAIN(PP
, TC
, nd
, options
, i
, D
, rVD
)
2603 END_FORALL_REDUCED_DOMAIN
2604 Polyhedron_Free(TC
);
2611 /* This doesn't seem to have any effect */
2613 Polyhedron
*CA
= align_context(VD
[0], P
->Dimension
, options
->MaxRays
);
2615 P
= DomainIntersection(P
, CA
, options
->MaxRays
);
2618 Polyhedron_Free(CA
);
2624 Polyhedron_Free(PR
);
2627 if (!EP
&& nd
> 1) {
2629 fprintf(stderr
, "\nER: VD\n");
2630 #endif /* DEBUG_ER */
2631 for (int i
= 0; i
< nd
; ++i
) {
2632 Polyhedron
*CA
= align_context(VD
[i
], P
->Dimension
, options
->MaxRays
);
2633 Polyhedron
*I
= DomainIntersection(P
, CA
, options
->MaxRays
);
2636 EP
= barvinok_enumerate_e_with_options(I
, exist
, nparam
, options
);
2638 evalue
*E
= barvinok_enumerate_e_with_options(I
, exist
, nparam
,
2641 free_evalue_refs(E
);
2645 Polyhedron_Free(CA
);
2649 for (int i
= 0; i
< nd
; ++i
)
2650 Polyhedron_Free(VD
[i
]);
2654 if (!EP
&& nvar
== 0) {
2657 Param_Vertices
*V
, *V2
;
2658 Matrix
* M
= Matrix_Alloc(1, P
->Dimension
+2);
2660 FORALL_PVertex_in_ParamPolyhedron(V
, last
, PP
) {
2662 FORALL_PVertex_in_ParamPolyhedron(V2
, last
, PP
) {
2669 for (int i
= 0; i
< exist
; ++i
) {
2670 value_oppose(f
, V
->Vertex
->p
[i
][nparam
+1]);
2671 Vector_Combine(V
->Vertex
->p
[i
],
2673 M
->p
[0] + 1 + nvar
+ exist
,
2674 V2
->Vertex
->p
[i
][nparam
+1],
2678 for (j
= 0; j
< nparam
; ++j
)
2679 if (value_notzero_p(M
->p
[0][1+nvar
+exist
+j
]))
2683 ConstraintSimplify(M
->p
[0], M
->p
[0],
2684 P
->Dimension
+2, &f
);
2685 value_set_si(M
->p
[0][0], 0);
2686 Polyhedron
*para
= AddConstraints(M
->p
[0], 1, P
,
2688 POL_ENSURE_VERTICES(para
);
2690 Polyhedron_Free(para
);
2693 Polyhedron
*pos
, *neg
;
2694 value_set_si(M
->p
[0][0], 1);
2695 value_decrement(M
->p
[0][P
->Dimension
+1],
2696 M
->p
[0][P
->Dimension
+1]);
2697 neg
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
2698 value_set_si(f
, -1);
2699 Vector_Scale(M
->p
[0]+1, M
->p
[0]+1, f
,
2701 value_decrement(M
->p
[0][P
->Dimension
+1],
2702 M
->p
[0][P
->Dimension
+1]);
2703 value_decrement(M
->p
[0][P
->Dimension
+1],
2704 M
->p
[0][P
->Dimension
+1]);
2705 pos
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
2706 POL_ENSURE_VERTICES(neg
);
2707 POL_ENSURE_VERTICES(pos
);
2708 if (emptyQ(neg
) && emptyQ(pos
)) {
2709 Polyhedron_Free(para
);
2710 Polyhedron_Free(pos
);
2711 Polyhedron_Free(neg
);
2715 fprintf(stderr
, "\nER: Order\n");
2716 #endif /* DEBUG_ER */
2717 EP
= barvinok_enumerate_e_with_options(para
, exist
, nparam
,
2721 E
= barvinok_enumerate_e_with_options(pos
, exist
, nparam
,
2724 free_evalue_refs(E
);
2728 E
= barvinok_enumerate_e_with_options(neg
, exist
, nparam
,
2731 free_evalue_refs(E
);
2734 Polyhedron_Free(para
);
2735 Polyhedron_Free(pos
);
2736 Polyhedron_Free(neg
);
2741 } END_FORALL_PVertex_in_ParamPolyhedron
;
2744 } END_FORALL_PVertex_in_ParamPolyhedron
;
2747 /* Search for vertex coordinate to split on */
2748 /* First look for one independent of the parameters */
2749 FORALL_PVertex_in_ParamPolyhedron(V
, last
, PP
) {
2750 for (int i
= 0; i
< exist
; ++i
) {
2752 for (j
= 0; j
< nparam
; ++j
)
2753 if (value_notzero_p(V
->Vertex
->p
[i
][j
]))
2757 value_set_si(M
->p
[0][0], 1);
2758 Vector_Set(M
->p
[0]+1, 0, nvar
+exist
);
2759 Vector_Copy(V
->Vertex
->p
[i
],
2760 M
->p
[0] + 1 + nvar
+ exist
, nparam
+1);
2761 value_oppose(M
->p
[0][1+nvar
+i
],
2762 V
->Vertex
->p
[i
][nparam
+1]);
2764 Polyhedron
*pos
, *neg
;
2765 value_set_si(M
->p
[0][0], 1);
2766 value_decrement(M
->p
[0][P
->Dimension
+1],
2767 M
->p
[0][P
->Dimension
+1]);
2768 neg
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
2769 value_set_si(f
, -1);
2770 Vector_Scale(M
->p
[0]+1, M
->p
[0]+1, f
,
2772 value_decrement(M
->p
[0][P
->Dimension
+1],
2773 M
->p
[0][P
->Dimension
+1]);
2774 value_decrement(M
->p
[0][P
->Dimension
+1],
2775 M
->p
[0][P
->Dimension
+1]);
2776 pos
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
2777 POL_ENSURE_VERTICES(neg
);
2778 POL_ENSURE_VERTICES(pos
);
2779 if (emptyQ(neg
) || emptyQ(pos
)) {
2780 Polyhedron_Free(pos
);
2781 Polyhedron_Free(neg
);
2784 Polyhedron_Free(pos
);
2785 value_increment(M
->p
[0][P
->Dimension
+1],
2786 M
->p
[0][P
->Dimension
+1]);
2787 pos
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
2789 fprintf(stderr
, "\nER: Vertex\n");
2790 #endif /* DEBUG_ER */
2792 EP
= enumerate_or(pos
, exist
, nparam
, options
);
2797 } END_FORALL_PVertex_in_ParamPolyhedron
;
2801 /* Search for vertex coordinate to split on */
2802 /* Now look for one that depends on the parameters */
2803 FORALL_PVertex_in_ParamPolyhedron(V
, last
, PP
) {
2804 for (int i
= 0; i
< exist
; ++i
) {
2805 value_set_si(M
->p
[0][0], 1);
2806 Vector_Set(M
->p
[0]+1, 0, nvar
+exist
);
2807 Vector_Copy(V
->Vertex
->p
[i
],
2808 M
->p
[0] + 1 + nvar
+ exist
, nparam
+1);
2809 value_oppose(M
->p
[0][1+nvar
+i
],
2810 V
->Vertex
->p
[i
][nparam
+1]);
2812 Polyhedron
*pos
, *neg
;
2813 value_set_si(M
->p
[0][0], 1);
2814 value_decrement(M
->p
[0][P
->Dimension
+1],
2815 M
->p
[0][P
->Dimension
+1]);
2816 neg
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
2817 value_set_si(f
, -1);
2818 Vector_Scale(M
->p
[0]+1, M
->p
[0]+1, f
,
2820 value_decrement(M
->p
[0][P
->Dimension
+1],
2821 M
->p
[0][P
->Dimension
+1]);
2822 value_decrement(M
->p
[0][P
->Dimension
+1],
2823 M
->p
[0][P
->Dimension
+1]);
2824 pos
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
2825 POL_ENSURE_VERTICES(neg
);
2826 POL_ENSURE_VERTICES(pos
);
2827 if (emptyQ(neg
) || emptyQ(pos
)) {
2828 Polyhedron_Free(pos
);
2829 Polyhedron_Free(neg
);
2832 Polyhedron_Free(pos
);
2833 value_increment(M
->p
[0][P
->Dimension
+1],
2834 M
->p
[0][P
->Dimension
+1]);
2835 pos
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
2837 fprintf(stderr
, "\nER: ParamVertex\n");
2838 #endif /* DEBUG_ER */
2840 EP
= enumerate_or(pos
, exist
, nparam
, options
);
2845 } END_FORALL_PVertex_in_ParamPolyhedron
;
2853 Polyhedron_Free(CEq
);
2857 Param_Polyhedron_Free(PP
);
2863 evalue
* barvinok_enumerate_pip(Polyhedron
*P
, unsigned exist
, unsigned nparam
,
2867 barvinok_options
*options
= barvinok_options_new_with_defaults();
2868 options
->MaxRays
= MaxRays
;
2869 E
= barvinok_enumerate_pip_with_options(P
, exist
, nparam
, options
);
2870 barvinok_options_free(options
);
2875 evalue
*barvinok_enumerate_pip_with_options(Polyhedron
*P
,
2876 unsigned exist
, unsigned nparam
, struct barvinok_options
*options
)
2881 evalue
*barvinok_enumerate_pip_with_options(Polyhedron
*P
,
2882 unsigned exist
, unsigned nparam
, struct barvinok_options
*options
)
2884 int nvar
= P
->Dimension
- exist
- nparam
;
2885 evalue
*EP
= evalue_zero();
2889 fprintf(stderr
, "\nER: PIP\n");
2890 #endif /* DEBUG_ER */
2892 Polyhedron
*D
= pip_projectout(P
, nvar
, exist
, nparam
);
2893 for (Q
= D
; Q
; Q
= N
) {
2897 exist
= Q
->Dimension
- nvar
- nparam
;
2898 E
= barvinok_enumerate_e_with_options(Q
, exist
, nparam
, options
);
2901 free_evalue_refs(E
);
2910 static bool is_single(Value
*row
, int pos
, int len
)
2912 return First_Non_Zero(row
, pos
) == -1 &&
2913 First_Non_Zero(row
+pos
+1, len
-pos
-1) == -1;
2916 static evalue
* barvinok_enumerate_e_r(Polyhedron
*P
,
2917 unsigned exist
, unsigned nparam
, barvinok_options
*options
);
2920 static int er_level
= 0;
2922 evalue
* barvinok_enumerate_e_with_options(Polyhedron
*P
,
2923 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2925 fprintf(stderr
, "\nER: level %i\n", er_level
);
2927 Polyhedron_PrintConstraints(stderr
, P_VALUE_FMT
, P
);
2928 fprintf(stderr
, "\nE %d\nP %d\n", exist
, nparam
);
2930 P
= DomainConstraintSimplify(Polyhedron_Copy(P
), options
->MaxRays
);
2931 evalue
*EP
= barvinok_enumerate_e_r(P
, exist
, nparam
, options
);
2937 evalue
* barvinok_enumerate_e_with_options(Polyhedron
*P
,
2938 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2940 P
= DomainConstraintSimplify(Polyhedron_Copy(P
), options
->MaxRays
);
2941 evalue
*EP
= barvinok_enumerate_e_r(P
, exist
, nparam
, options
);
2947 evalue
* barvinok_enumerate_e(Polyhedron
*P
, unsigned exist
, unsigned nparam
,
2951 barvinok_options
*options
= barvinok_options_new_with_defaults();
2952 options
->MaxRays
= MaxRays
;
2953 E
= barvinok_enumerate_e_with_options(P
, exist
, nparam
, options
);
2954 barvinok_options_free(options
);
2958 static evalue
* barvinok_enumerate_e_r(Polyhedron
*P
,
2959 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2962 Polyhedron
*U
= Universe_Polyhedron(nparam
);
2963 evalue
*EP
= barvinok_enumerate_with_options(P
, U
, options
);
2964 //char *param_name[] = {"P", "Q", "R", "S", "T" };
2965 //print_evalue(stdout, EP, param_name);
2970 int nvar
= P
->Dimension
- exist
- nparam
;
2971 int len
= P
->Dimension
+ 2;
2974 POL_ENSURE_FACETS(P
);
2975 POL_ENSURE_VERTICES(P
);
2978 return evalue_zero();
2980 if (nvar
== 0 && nparam
== 0) {
2981 evalue
*EP
= evalue_zero();
2982 barvinok_count_with_options(P
, &EP
->x
.n
, options
);
2983 if (value_pos_p(EP
->x
.n
))
2984 value_set_si(EP
->x
.n
, 1);
2989 for (r
= 0; r
< P
->NbRays
; ++r
)
2990 if (value_zero_p(P
->Ray
[r
][0]) ||
2991 value_zero_p(P
->Ray
[r
][P
->Dimension
+1])) {
2993 for (i
= 0; i
< nvar
; ++i
)
2994 if (value_notzero_p(P
->Ray
[r
][i
+1]))
2998 for (i
= nvar
+ exist
; i
< nvar
+ exist
+ nparam
; ++i
)
2999 if (value_notzero_p(P
->Ray
[r
][i
+1]))
3001 if (i
>= nvar
+ exist
+ nparam
)
3004 if (r
< P
->NbRays
) {
3005 evalue
*EP
= evalue_zero();
3006 value_set_si(EP
->x
.n
, -1);
3011 for (r
= 0; r
< P
->NbEq
; ++r
)
3012 if ((first
= First_Non_Zero(P
->Constraint
[r
]+1+nvar
, exist
)) != -1)
3015 if (First_Non_Zero(P
->Constraint
[r
]+1+nvar
+first
+1,
3016 exist
-first
-1) != -1) {
3017 Polyhedron
*T
= rotate_along(P
, r
, nvar
, exist
, options
->MaxRays
);
3019 fprintf(stderr
, "\nER: Equality\n");
3020 #endif /* DEBUG_ER */
3021 evalue
*EP
= barvinok_enumerate_e_with_options(T
, exist
-1, nparam
,
3027 fprintf(stderr
, "\nER: Fixed\n");
3028 #endif /* DEBUG_ER */
3030 return barvinok_enumerate_e_with_options(P
, exist
-1, nparam
,
3033 Polyhedron
*T
= Polyhedron_Copy(P
);
3034 SwapColumns(T
, nvar
+1, nvar
+1+first
);
3035 evalue
*EP
= barvinok_enumerate_e_with_options(T
, exist
-1, nparam
,
3043 Vector
*row
= Vector_Alloc(len
);
3044 value_set_si(row
->p
[0], 1);
3049 enum constraint
* info
= new constraint
[exist
];
3050 for (int i
= 0; i
< exist
; ++i
) {
3052 for (int l
= P
->NbEq
; l
< P
->NbConstraints
; ++l
) {
3053 if (value_negz_p(P
->Constraint
[l
][nvar
+i
+1]))
3055 bool l_parallel
= is_single(P
->Constraint
[l
]+nvar
+1, i
, exist
);
3056 for (int u
= P
->NbEq
; u
< P
->NbConstraints
; ++u
) {
3057 if (value_posz_p(P
->Constraint
[u
][nvar
+i
+1]))
3059 bool lu_parallel
= l_parallel
||
3060 is_single(P
->Constraint
[u
]+nvar
+1, i
, exist
);
3061 value_oppose(f
, P
->Constraint
[u
][nvar
+i
+1]);
3062 Vector_Combine(P
->Constraint
[l
]+1, P
->Constraint
[u
]+1, row
->p
+1,
3063 f
, P
->Constraint
[l
][nvar
+i
+1], len
-1);
3064 if (!(info
[i
] & INDEPENDENT
)) {
3066 for (j
= 0; j
< exist
; ++j
)
3067 if (j
!= i
&& value_notzero_p(row
->p
[nvar
+j
+1]))
3070 //printf("independent: i: %d, l: %d, u: %d\n", i, l, u);
3071 info
[i
] = (constraint
)(info
[i
] | INDEPENDENT
);
3074 if (info
[i
] & ALL_POS
) {
3075 value_addto(row
->p
[len
-1], row
->p
[len
-1],
3076 P
->Constraint
[l
][nvar
+i
+1]);
3077 value_addto(row
->p
[len
-1], row
->p
[len
-1], f
);
3078 value_multiply(f
, f
, P
->Constraint
[l
][nvar
+i
+1]);
3079 value_subtract(row
->p
[len
-1], row
->p
[len
-1], f
);
3080 value_decrement(row
->p
[len
-1], row
->p
[len
-1]);
3081 ConstraintSimplify(row
->p
, row
->p
, len
, &f
);
3082 value_set_si(f
, -1);
3083 Vector_Scale(row
->p
+1, row
->p
+1, f
, len
-1);
3084 value_decrement(row
->p
[len
-1], row
->p
[len
-1]);
3085 Polyhedron
*T
= AddConstraints(row
->p
, 1, P
, options
->MaxRays
);
3086 POL_ENSURE_VERTICES(T
);
3088 //printf("not all_pos: i: %d, l: %d, u: %d\n", i, l, u);
3089 info
[i
] = (constraint
)(info
[i
] ^ ALL_POS
);
3091 //puts("pos remainder");
3092 //Polyhedron_Print(stdout, P_VALUE_FMT, T);
3095 if (!(info
[i
] & ONE_NEG
)) {
3097 negative_test_constraint(P
->Constraint
[l
],
3099 row
->p
, nvar
+i
, len
, &f
);
3100 oppose_constraint(row
->p
, len
, &f
);
3101 Polyhedron
*T
= AddConstraints(row
->p
, 1, P
,
3103 POL_ENSURE_VERTICES(T
);
3105 //printf("one_neg i: %d, l: %d, u: %d\n", i, l, u);
3106 info
[i
] = (constraint
)(info
[i
] | ONE_NEG
);
3108 //puts("neg remainder");
3109 //Polyhedron_Print(stdout, P_VALUE_FMT, T);
3111 } else if (!(info
[i
] & ROT_NEG
)) {
3112 if (parallel_constraints(P
->Constraint
[l
],
3114 row
->p
, nvar
, exist
)) {
3115 negative_test_constraint7(P
->Constraint
[l
],
3117 row
->p
, nvar
, exist
,
3119 oppose_constraint(row
->p
, len
, &f
);
3120 Polyhedron
*T
= AddConstraints(row
->p
, 1, P
,
3122 POL_ENSURE_VERTICES(T
);
3124 // printf("rot_neg i: %d, l: %d, u: %d\n", i, l, u);
3125 info
[i
] = (constraint
)(info
[i
] | ROT_NEG
);
3128 //puts("neg remainder");
3129 //Polyhedron_Print(stdout, P_VALUE_FMT, T);
3134 if (!(info
[i
] & ALL_POS
) && (info
[i
] & (ONE_NEG
| ROT_NEG
)))
3138 if (info
[i
] & ALL_POS
)
3145 for (int i = 0; i < exist; ++i)
3146 printf("%i: %i\n", i, info[i]);
3148 for (int i
= 0; i
< exist
; ++i
)
3149 if (info
[i
] & ALL_POS
) {
3151 fprintf(stderr
, "\nER: Positive\n");
3152 #endif /* DEBUG_ER */
3154 // Maybe we should chew off some of the fat here
3155 Matrix
*M
= Matrix_Alloc(P
->Dimension
, P
->Dimension
+1);
3156 for (int j
= 0; j
< P
->Dimension
; ++j
)
3157 value_set_si(M
->p
[j
][j
+ (j
>= i
+nvar
)], 1);
3158 Polyhedron
*T
= Polyhedron_Image(P
, M
, options
->MaxRays
);
3160 evalue
*EP
= barvinok_enumerate_e_with_options(T
, exist
-1, nparam
,
3168 for (int i
= 0; i
< exist
; ++i
)
3169 if (info
[i
] & ONE_NEG
) {
3171 fprintf(stderr
, "\nER: Negative\n");
3172 #endif /* DEBUG_ER */
3177 return barvinok_enumerate_e_with_options(P
, exist
-1, nparam
,
3180 Polyhedron
*T
= Polyhedron_Copy(P
);
3181 SwapColumns(T
, nvar
+1, nvar
+1+i
);
3182 evalue
*EP
= barvinok_enumerate_e_with_options(T
, exist
-1, nparam
,
3188 for (int i
= 0; i
< exist
; ++i
)
3189 if (info
[i
] & ROT_NEG
) {
3191 fprintf(stderr
, "\nER: Rotate\n");
3192 #endif /* DEBUG_ER */
3196 Polyhedron
*T
= rotate_along(P
, r
, nvar
, exist
, options
->MaxRays
);
3197 evalue
*EP
= barvinok_enumerate_e_with_options(T
, exist
-1, nparam
,
3202 for (int i
= 0; i
< exist
; ++i
)
3203 if (info
[i
] & INDEPENDENT
) {
3204 Polyhedron
*pos
, *neg
;
3206 /* Find constraint again and split off negative part */
3208 if (SplitOnVar(P
, i
, nvar
, exist
, options
->MaxRays
,
3209 row
, f
, true, &pos
, &neg
)) {
3211 fprintf(stderr
, "\nER: Split\n");
3212 #endif /* DEBUG_ER */
3215 barvinok_enumerate_e_with_options(neg
, exist
-1, nparam
, options
);
3217 barvinok_enumerate_e_with_options(pos
, exist
, nparam
, options
);
3219 free_evalue_refs(E
);
3221 Polyhedron_Free(neg
);
3222 Polyhedron_Free(pos
);
3236 EP
= enumerate_line(P
, exist
, nparam
, options
);
3240 EP
= barvinok_enumerate_pip_with_options(P
, exist
, nparam
, options
);
3244 EP
= enumerate_redundant_ray(P
, exist
, nparam
, options
);
3248 EP
= enumerate_sure(P
, exist
, nparam
, options
);
3252 EP
= enumerate_ray(P
, exist
, nparam
, options
);
3256 EP
= enumerate_sure2(P
, exist
, nparam
, options
);
3260 F
= unfringe(P
, options
->MaxRays
);
3261 if (!PolyhedronIncludes(F
, P
)) {
3263 fprintf(stderr
, "\nER: Fringed\n");
3264 #endif /* DEBUG_ER */
3265 EP
= barvinok_enumerate_e_with_options(F
, exist
, nparam
, options
);
3272 EP
= enumerate_vd(&P
, exist
, nparam
, options
);
3277 EP
= enumerate_sum(P
, exist
, nparam
, options
);
3284 Polyhedron
*pos
, *neg
;
3285 for (i
= 0; i
< exist
; ++i
)
3286 if (SplitOnVar(P
, i
, nvar
, exist
, options
->MaxRays
,
3287 row
, f
, false, &pos
, &neg
))
3293 EP
= enumerate_or(pos
, exist
, nparam
, options
);
3306 * remove equalities that require a "compression" of the parameters
3308 static Polyhedron
*remove_more_equalities(Polyhedron
*P
, unsigned nparam
,
3309 Matrix
**CP
, unsigned MaxRays
)
3312 remove_all_equalities(&P
, NULL
, CP
, NULL
, nparam
, MaxRays
);
3319 static gen_fun
*series(Polyhedron
*P
, unsigned nparam
, barvinok_options
*options
)
3329 assert(!Polyhedron_is_unbounded(P
, nparam
, options
->MaxRays
));
3330 assert(P
->NbBid
== 0);
3331 assert(Polyhedron_has_revlex_positive_rays(P
, nparam
));
3333 P
= remove_more_equalities(P
, nparam
, &CP
, options
->MaxRays
);
3334 assert(P
->NbEq
== 0);
3336 nparam
= CP
->NbColumns
-1;
3341 barvinok_count_with_options(P
, &c
, options
);
3342 gf
= new gen_fun(c
);
3346 red
= gf_base::create(Polyhedron_Project(P
, nparam
),
3347 P
->Dimension
, nparam
, options
);
3348 POL_ENSURE_VERTICES(P
);
3349 red
->start_gf(P
, options
);
3361 gen_fun
* barvinok_series_with_options(Polyhedron
*P
, Polyhedron
* C
,
3362 barvinok_options
*options
)
3365 unsigned nparam
= C
->Dimension
;
3368 CA
= align_context(C
, P
->Dimension
, options
->MaxRays
);
3369 P
= DomainIntersection(P
, CA
, options
->MaxRays
);
3370 Polyhedron_Free(CA
);
3372 gf
= series(P
, nparam
, options
);
3377 gen_fun
* barvinok_series(Polyhedron
*P
, Polyhedron
* C
, unsigned MaxRays
)
3380 barvinok_options
*options
= barvinok_options_new_with_defaults();
3381 options
->MaxRays
= MaxRays
;
3382 gf
= barvinok_series_with_options(P
, C
, options
);
3383 barvinok_options_free(options
);
3387 static Polyhedron
*skew_into_positive_orthant(Polyhedron
*D
, unsigned nparam
,
3393 for (Polyhedron
*P
= D
; P
; P
= P
->next
) {
3394 POL_ENSURE_VERTICES(P
);
3395 assert(!Polyhedron_is_unbounded(P
, nparam
, MaxRays
));
3396 assert(P
->NbBid
== 0);
3397 assert(Polyhedron_has_positive_rays(P
, nparam
));
3399 for (int r
= 0; r
< P
->NbRays
; ++r
) {
3400 if (value_notzero_p(P
->Ray
[r
][P
->Dimension
+1]))
3402 for (int i
= 0; i
< nparam
; ++i
) {
3404 if (value_posz_p(P
->Ray
[r
][i
+1]))
3407 M
= Matrix_Alloc(D
->Dimension
+1, D
->Dimension
+1);
3408 for (int i
= 0; i
< D
->Dimension
+1; ++i
)
3409 value_set_si(M
->p
[i
][i
], 1);
3411 Inner_Product(P
->Ray
[r
]+1, M
->p
[i
], D
->Dimension
+1, &tmp
);
3412 if (value_posz_p(tmp
))
3415 for (j
= P
->Dimension
- nparam
; j
< P
->Dimension
; ++j
)
3416 if (value_pos_p(P
->Ray
[r
][j
+1]))
3418 assert(j
< P
->Dimension
);
3419 value_pdivision(tmp
, P
->Ray
[r
][j
+1], P
->Ray
[r
][i
+1]);
3420 value_subtract(M
->p
[i
][j
], M
->p
[i
][j
], tmp
);
3426 D
= DomainImage(D
, M
, MaxRays
);
3432 gen_fun
* barvinok_enumerate_union_series_with_options(Polyhedron
*D
, Polyhedron
* C
,
3433 barvinok_options
*options
)
3435 Polyhedron
*conv
, *D2
;
3437 gen_fun
*gf
= NULL
, *gf2
;
3438 unsigned nparam
= C
->Dimension
;
3443 CA
= align_context(C
, D
->Dimension
, options
->MaxRays
);
3444 D
= DomainIntersection(D
, CA
, options
->MaxRays
);
3445 Polyhedron_Free(CA
);
3447 D2
= skew_into_positive_orthant(D
, nparam
, options
->MaxRays
);
3448 for (Polyhedron
*P
= D2
; P
; P
= P
->next
) {
3449 assert(P
->Dimension
== D2
->Dimension
);
3452 P_gf
= series(Polyhedron_Copy(P
), P
->Dimension
, options
);
3456 gf
->add_union(P_gf
, options
);
3460 /* we actually only need the convex union of the parameter space
3461 * but the reducer classes currently expect a polyhedron in
3462 * the combined space
3464 Polyhedron_Free(gf
->context
);
3465 gf
->context
= DomainConvex(D2
, options
->MaxRays
);
3467 gf2
= gf
->summate(D2
->Dimension
- nparam
, options
);
3476 gen_fun
* barvinok_enumerate_union_series(Polyhedron
*D
, Polyhedron
* C
,
3480 barvinok_options
*options
= barvinok_options_new_with_defaults();
3481 options
->MaxRays
= MaxRays
;
3482 gf
= barvinok_enumerate_union_series_with_options(D
, C
, options
);
3483 barvinok_options_free(options
);
3487 evalue
* barvinok_enumerate_union(Polyhedron
*D
, Polyhedron
* C
, unsigned MaxRays
)
3490 gen_fun
*gf
= barvinok_enumerate_union_series(D
, C
, MaxRays
);