8 #include <NTL/mat_ZZ.h>
10 #include <barvinok/util.h>
11 #include <barvinok/evalue.h>
16 #include <barvinok/barvinok.h>
17 #include <barvinok/genfun.h>
18 #include <barvinok/options.h>
19 #include <barvinok/sample.h>
20 #include "conversion.h"
21 #include "decomposer.h"
22 #include "lattice_point.h"
23 #include "reduce_domain.h"
24 #include "genfun_constructor.h"
25 #include "remove_equalities.h"
38 using std::ostringstream
;
40 #define ALLOC(t,p) p = (t*)malloc(sizeof(*p))
48 dpoly_n(int d
, ZZ
& degree_0
, ZZ
& degree_1
, int offset
= 0) {
52 zz2value(degree_0
, d0
);
53 zz2value(degree_1
, d1
);
54 coeff
= Matrix_Alloc(d
+1, d
+1+1);
55 value_set_si(coeff
->p
[0][0], 1);
56 value_set_si(coeff
->p
[0][d
+1], 1);
57 for (int i
= 1; i
<= d
; ++i
) {
58 value_multiply(coeff
->p
[i
][0], coeff
->p
[i
-1][0], d0
);
59 Vector_Combine(coeff
->p
[i
-1], coeff
->p
[i
-1]+1, coeff
->p
[i
]+1,
61 value_set_si(coeff
->p
[i
][d
+1], i
);
62 value_multiply(coeff
->p
[i
][d
+1], coeff
->p
[i
][d
+1], coeff
->p
[i
-1][d
+1]);
63 value_decrement(d0
, d0
);
68 void div(dpoly
& d
, Vector
*count
, ZZ
& sign
) {
69 int len
= coeff
->NbRows
;
70 Matrix
* c
= Matrix_Alloc(coeff
->NbRows
, coeff
->NbColumns
);
73 for (int i
= 0; i
< len
; ++i
) {
74 Vector_Copy(coeff
->p
[i
], c
->p
[i
], len
+1);
75 for (int j
= 1; j
<= i
; ++j
) {
76 zz2value(d
.coeff
[j
], tmp
);
77 value_multiply(tmp
, tmp
, 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 zz2value(d
.coeff
[0], tmp
);
84 value_multiply(c
->p
[i
][len
], c
->p
[i
][len
], tmp
);
87 value_set_si(tmp
, -1);
88 Vector_Scale(c
->p
[len
-1], count
->p
, tmp
, len
);
89 value_assign(count
->p
[len
], c
->p
[len
-1][len
]);
91 Vector_Copy(c
->p
[len
-1], count
->p
, len
+1);
92 Vector_Normalize(count
->p
, len
+1);
100 * Searches for a vector that is not orthogonal to any
101 * of the rays in rays.
103 static void nonorthog(mat_ZZ
& rays
, vec_ZZ
& lambda
)
105 int dim
= rays
.NumCols();
107 lambda
.SetLength(dim
);
111 for (int i
= 2; !found
&& i
<= 50*dim
; i
+=4) {
112 for (int j
= 0; j
< MAX_TRY
; ++j
) {
113 for (int k
= 0; k
< dim
; ++k
) {
114 int r
= random_int(i
)+2;
115 int v
= (2*(r
%2)-1) * (r
>> 1);
119 for (; k
< rays
.NumRows(); ++k
)
120 if (lambda
* rays
[k
] == 0)
122 if (k
== rays
.NumRows()) {
131 static void add_rays(mat_ZZ
& rays
, Polyhedron
*i
, int *r
, int nvar
= -1,
134 unsigned dim
= i
->Dimension
;
137 for (int k
= 0; k
< i
->NbRays
; ++k
) {
138 if (!value_zero_p(i
->Ray
[k
][dim
+1]))
140 if (!all
&& nvar
!= dim
&& First_Non_Zero(i
->Ray
[k
]+1, nvar
) == -1)
142 values2zz(i
->Ray
[k
]+1, rays
[(*r
)++], nvar
);
146 static void mask_r(Matrix
*f
, int nr
, Vector
*lcm
, int p
, Vector
*val
, evalue
*ev
)
148 unsigned nparam
= lcm
->Size
;
151 Vector
* prod
= Vector_Alloc(f
->NbRows
);
152 Matrix_Vector_Product(f
, val
->p
, prod
->p
);
154 for (int i
= 0; i
< nr
; ++i
) {
155 value_modulus(prod
->p
[i
], prod
->p
[i
], f
->p
[i
][nparam
+1]);
156 isint
&= value_zero_p(prod
->p
[i
]);
158 value_set_si(ev
->d
, 1);
160 value_set_si(ev
->x
.n
, isint
);
167 if (value_one_p(lcm
->p
[p
]))
168 mask_r(f
, nr
, lcm
, p
+1, val
, ev
);
170 value_assign(tmp
, lcm
->p
[p
]);
171 value_set_si(ev
->d
, 0);
172 ev
->x
.p
= new_enode(periodic
, VALUE_TO_INT(tmp
), p
+1);
174 value_decrement(tmp
, tmp
);
175 value_assign(val
->p
[p
], tmp
);
176 mask_r(f
, nr
, lcm
, p
+1, val
, &ev
->x
.p
->arr
[VALUE_TO_INT(tmp
)]);
177 } while (value_pos_p(tmp
));
182 static void mask_fractional(Matrix
*f
, evalue
*factor
)
184 int nr
= f
->NbRows
, nc
= f
->NbColumns
;
187 for (n
= 0; n
< nr
&& value_notzero_p(f
->p
[n
][nc
-1]); ++n
)
188 if (value_notone_p(f
->p
[n
][nc
-1]) &&
189 value_notmone_p(f
->p
[n
][nc
-1]))
203 value_set_si(EV
.x
.n
, 1);
205 for (n
= 0; n
< nr
; ++n
) {
206 value_assign(m
, f
->p
[n
][nc
-1]);
207 if (value_one_p(m
) || value_mone_p(m
))
210 int j
= normal_mod(f
->p
[n
], nc
-1, &m
);
212 free_evalue_refs(factor
);
213 value_init(factor
->d
);
214 evalue_set_si(factor
, 0, 1);
218 values2zz(f
->p
[n
], row
, nc
-1);
221 if (j
< (nc
-1)-1 && row
[j
] > g
/2) {
222 for (int k
= j
; k
< (nc
-1); ++k
)
228 value_set_si(EP
.d
, 0);
229 EP
.x
.p
= new_enode(relation
, 2, 0);
230 value_clear(EP
.x
.p
->arr
[1].d
);
231 EP
.x
.p
->arr
[1] = *factor
;
232 evalue
*ev
= &EP
.x
.p
->arr
[0];
233 value_set_si(ev
->d
, 0);
234 ev
->x
.p
= new_enode(fractional
, 3, -1);
235 evalue_set_si(&ev
->x
.p
->arr
[1], 0, 1);
236 evalue_set_si(&ev
->x
.p
->arr
[2], 1, 1);
237 evalue
*E
= multi_monom(row
);
238 value_assign(EV
.d
, m
);
240 value_clear(ev
->x
.p
->arr
[0].d
);
241 ev
->x
.p
->arr
[0] = *E
;
247 free_evalue_refs(&EV
);
253 static void mask_table(Matrix
*f
, evalue
*factor
)
255 int nr
= f
->NbRows
, nc
= f
->NbColumns
;
258 for (n
= 0; n
< nr
&& value_notzero_p(f
->p
[n
][nc
-1]); ++n
)
259 if (value_notone_p(f
->p
[n
][nc
-1]) &&
260 value_notmone_p(f
->p
[n
][nc
-1]))
268 unsigned np
= nc
- 2;
269 Vector
*lcm
= Vector_Alloc(np
);
270 Vector
*val
= Vector_Alloc(nc
);
271 Vector_Set(val
->p
, 0, nc
);
272 value_set_si(val
->p
[np
], 1);
273 Vector_Set(lcm
->p
, 1, np
);
274 for (n
= 0; n
< nr
; ++n
) {
275 if (value_one_p(f
->p
[n
][nc
-1]) ||
276 value_mone_p(f
->p
[n
][nc
-1]))
278 for (int j
= 0; j
< np
; ++j
)
279 if (value_notzero_p(f
->p
[n
][j
])) {
280 Gcd(f
->p
[n
][j
], f
->p
[n
][nc
-1], &tmp
);
281 value_division(tmp
, f
->p
[n
][nc
-1], tmp
);
282 value_lcm(tmp
, lcm
->p
[j
], &lcm
->p
[j
]);
287 mask_r(f
, nr
, lcm
, 0, val
, &EP
);
292 free_evalue_refs(&EP
);
295 static void mask(Matrix
*f
, evalue
*factor
, barvinok_options
*options
)
297 if (options
->lookup_table
)
298 mask_table(f
, factor
);
300 mask_fractional(f
, factor
);
303 struct counter
: public np_base
{
313 counter(unsigned dim
) : np_base(dim
) {
318 virtual void init(Polyhedron
*P
) {
319 randomvector(P
, lambda
, dim
);
322 virtual void reset() {
323 mpq_set_si(count
, 0, 0);
330 virtual void handle(const mat_ZZ
& rays
, Value
*vertex
, const QQ
& c
,
331 unsigned long det
, int *closed
, barvinok_options
*options
);
332 virtual void get_count(Value
*result
) {
333 assert(value_one_p(&count
[0]._mp_den
));
334 value_assign(*result
, &count
[0]._mp_num
);
338 void counter::handle(const mat_ZZ
& rays
, Value
*V
, const QQ
& c
, unsigned long det
,
339 int *closed
, barvinok_options
*options
)
341 for (int k
= 0; k
< dim
; ++k
) {
342 if (lambda
* rays
[k
] == 0)
347 assert(c
.n
== 1 || c
.n
== -1);
350 lattice_point(V
, rays
, vertex
, det
, closed
);
351 num
= vertex
* lambda
;
354 normalize(sign
, offset
, den
);
357 dpoly
d(dim
, num
[0]);
358 for (int k
= 1; k
< num
.length(); ++k
) {
360 dpoly
term(dim
, num
[k
]);
363 dpoly
n(dim
, den
[0], 1);
364 for (int k
= 1; k
< dim
; ++k
) {
365 dpoly
fact(dim
, den
[k
], 1);
368 d
.div(n
, count
, sign
);
371 struct bfe_term
: public bfc_term_base
{
372 vector
<evalue
*> factors
;
374 bfe_term(int len
) : bfc_term_base(len
) {
378 for (int i
= 0; i
< factors
.size(); ++i
) {
381 free_evalue_refs(factors
[i
]);
387 static void print_int_vector(int *v
, int len
, char *name
)
389 cerr
<< name
<< endl
;
390 for (int j
= 0; j
< len
; ++j
) {
396 static void print_bfc_terms(mat_ZZ
& factors
, bfc_vec
& v
)
399 cerr
<< "factors" << endl
;
400 cerr
<< factors
<< endl
;
401 for (int i
= 0; i
< v
.size(); ++i
) {
402 cerr
<< "term: " << i
<< endl
;
403 print_int_vector(v
[i
]->powers
, factors
.NumRows(), "powers");
404 cerr
<< "terms" << endl
;
405 cerr
<< v
[i
]->terms
<< endl
;
406 bfc_term
* bfct
= static_cast<bfc_term
*>(v
[i
]);
407 cerr
<< bfct
->c
<< endl
;
411 static void print_bfe_terms(mat_ZZ
& factors
, bfc_vec
& v
)
414 cerr
<< "factors" << endl
;
415 cerr
<< factors
<< endl
;
416 for (int i
= 0; i
< v
.size(); ++i
) {
417 cerr
<< "term: " << i
<< endl
;
418 print_int_vector(v
[i
]->powers
, factors
.NumRows(), "powers");
419 cerr
<< "terms" << endl
;
420 cerr
<< v
[i
]->terms
<< endl
;
421 bfe_term
* bfet
= static_cast<bfe_term
*>(v
[i
]);
422 for (int j
= 0; j
< v
[i
]->terms
.NumRows(); ++j
) {
423 char * test
[] = {"a", "b"};
424 print_evalue(stderr
, bfet
->factors
[j
], test
);
425 fprintf(stderr
, "\n");
430 struct bfcounter
: public bfcounter_base
{
433 bfcounter(unsigned dim
) : bfcounter_base(dim
) {
440 virtual void base(mat_ZZ
& factors
, bfc_vec
& v
);
441 virtual void get_count(Value
*result
) {
442 assert(value_one_p(&count
[0]._mp_den
));
443 value_assign(*result
, &count
[0]._mp_num
);
447 void bfcounter::base(mat_ZZ
& factors
, bfc_vec
& v
)
449 unsigned nf
= factors
.NumRows();
451 for (int i
= 0; i
< v
.size(); ++i
) {
452 bfc_term
* bfct
= static_cast<bfc_term
*>(v
[i
]);
454 // factor is always positive, so we always
456 for (int k
= 0; k
< nf
; ++k
)
457 total_power
+= v
[i
]->powers
[k
];
460 for (j
= 0; j
< nf
; ++j
)
461 if (v
[i
]->powers
[j
] > 0)
464 dpoly
D(total_power
, factors
[j
][0], 1);
465 for (int k
= 1; k
< v
[i
]->powers
[j
]; ++k
) {
466 dpoly
fact(total_power
, factors
[j
][0], 1);
470 for (int k
= 0; k
< v
[i
]->powers
[j
]; ++k
) {
471 dpoly
fact(total_power
, factors
[j
][0], 1);
475 for (int k
= 0; k
< v
[i
]->terms
.NumRows(); ++k
) {
476 dpoly
n(total_power
, v
[i
]->terms
[k
][0]);
477 mpq_set_si(tcount
, 0, 1);
478 n
.div(D
, tcount
, one
);
480 bfct
->c
[k
].n
= -bfct
->c
[k
].n
;
481 zz2value(bfct
->c
[k
].n
, tn
);
482 zz2value(bfct
->c
[k
].d
, td
);
484 mpz_mul(mpq_numref(tcount
), mpq_numref(tcount
), tn
);
485 mpz_mul(mpq_denref(tcount
), mpq_denref(tcount
), td
);
486 mpq_canonicalize(tcount
);
487 mpq_add(count
, count
, tcount
);
494 /* Check whether the polyhedron is unbounded and if so,
495 * check whether it has any (and therefore an infinite number of)
497 * If one of the vertices is integer, then we are done.
498 * Otherwise, transform the polyhedron such that one of the rays
499 * is the first unit vector and cut it off at a height that ensures
500 * that if the whole polyhedron has any points, then the remaining part
501 * has integer points. In particular we add the largest coefficient
502 * of a ray to the highest vertex (rounded up).
504 static bool Polyhedron_is_infinite(Polyhedron
*P
, Value
* result
,
505 barvinok_options
*options
)
517 for (; r
< P
->NbRays
; ++r
)
518 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1]))
520 if (P
->NbBid
== 0 && r
== P
->NbRays
)
523 if (options
->count_sample_infinite
) {
526 sample
= Polyhedron_Sample(P
, options
);
528 value_set_si(*result
, 0);
530 value_set_si(*result
, -1);
536 for (int i
= 0; i
< P
->NbRays
; ++i
)
537 if (value_one_p(P
->Ray
[i
][1+P
->Dimension
])) {
538 value_set_si(*result
, -1);
543 M
= Matrix_Alloc(P
->Dimension
+1, P
->Dimension
+1);
544 Vector_Gcd(P
->Ray
[r
]+1, P
->Dimension
, &g
);
545 Vector_AntiScale(P
->Ray
[r
]+1, M
->p
[0], g
, P
->Dimension
+1);
546 int ok
= unimodular_complete(M
, 1);
548 value_set_si(M
->p
[P
->Dimension
][P
->Dimension
], 1);
551 P
= Polyhedron_Preimage(P
, M2
, 0);
559 value_set_si(size
, 0);
561 for (int i
= 0; i
< P
->NbBid
; ++i
) {
562 value_absolute(tmp
, P
->Ray
[i
][1]);
563 if (value_gt(tmp
, size
))
564 value_assign(size
, tmp
);
566 for (int i
= P
->NbBid
; i
< P
->NbRays
; ++i
) {
567 if (value_zero_p(P
->Ray
[i
][P
->Dimension
+1])) {
568 if (value_gt(P
->Ray
[i
][1], size
))
569 value_assign(size
, P
->Ray
[i
][1]);
572 mpz_cdiv_q(tmp
, P
->Ray
[i
][1], P
->Ray
[i
][P
->Dimension
+1]);
573 if (first
|| value_gt(tmp
, offset
)) {
574 value_assign(offset
, tmp
);
578 value_addto(offset
, offset
, size
);
582 v
= Vector_Alloc(P
->Dimension
+2);
583 value_set_si(v
->p
[0], 1);
584 value_set_si(v
->p
[1], -1);
585 value_assign(v
->p
[1+P
->Dimension
], offset
);
586 R
= AddConstraints(v
->p
, 1, P
, options
->MaxRays
);
594 barvinok_count_with_options(P
, &c
, options
);
597 value_set_si(*result
, 0);
599 value_set_si(*result
, -1);
605 typedef Polyhedron
* Polyhedron_p
;
607 static void barvinok_count_f(Polyhedron
*P
, Value
* result
,
608 barvinok_options
*options
);
610 void barvinok_count_with_options(Polyhedron
*P
, Value
* result
,
611 struct barvinok_options
*options
)
616 bool infinite
= false;
620 "barvinok_count: input is a union; only first polyhedron is counted\n");
623 value_set_si(*result
, 0);
629 P
= remove_equalities(P
, options
->MaxRays
);
630 P
= DomainConstraintSimplify(P
, options
->MaxRays
);
634 } while (!emptyQ(P
) && P
->NbEq
!= 0);
637 value_set_si(*result
, 0);
642 if (Polyhedron_is_infinite(P
, result
, options
)) {
647 if (P
->Dimension
== 0) {
648 /* Test whether the constraints are satisfied */
649 POL_ENSURE_VERTICES(P
);
650 value_set_si(*result
, !emptyQ(P
));
655 Q
= Polyhedron_Factor(P
, 0, NULL
, options
->MaxRays
);
663 barvinok_count_f(P
, result
, options
);
664 if (value_neg_p(*result
))
666 if (Q
&& P
->next
&& value_notzero_p(*result
)) {
670 for (Q
= P
->next
; Q
; Q
= Q
->next
) {
671 barvinok_count_f(Q
, &factor
, options
);
672 if (value_neg_p(factor
)) {
675 } else if (Q
->next
&& value_zero_p(factor
)) {
676 value_set_si(*result
, 0);
679 value_multiply(*result
, *result
, factor
);
688 value_set_si(*result
, -1);
691 void barvinok_count(Polyhedron
*P
, Value
* result
, unsigned NbMaxCons
)
693 barvinok_options
*options
= barvinok_options_new_with_defaults();
694 options
->MaxRays
= NbMaxCons
;
695 barvinok_count_with_options(P
, result
, options
);
696 barvinok_options_free(options
);
699 static void barvinok_count_f(Polyhedron
*P
, Value
* result
,
700 barvinok_options
*options
)
703 value_set_si(*result
, 0);
707 if (P
->Dimension
== 1)
708 return Line_Length(P
, result
);
710 int c
= P
->NbConstraints
;
711 POL_ENSURE_FACETS(P
);
712 if (c
!= P
->NbConstraints
|| P
->NbEq
!= 0) {
713 Polyhedron
*next
= P
->next
;
715 barvinok_count_with_options(P
, result
, options
);
720 POL_ENSURE_VERTICES(P
);
722 if (Polyhedron_is_infinite(P
, result
, options
))
726 if (options
->incremental_specialization
== 2)
727 cnt
= new bfcounter(P
->Dimension
);
728 else if (options
->incremental_specialization
== 1)
729 cnt
= new icounter(P
->Dimension
);
731 cnt
= new counter(P
->Dimension
);
732 cnt
->start(P
, options
);
734 cnt
->get_count(result
);
738 static void uni_polynom(int param
, Vector
*c
, evalue
*EP
)
740 unsigned dim
= c
->Size
-2;
742 value_set_si(EP
->d
,0);
743 EP
->x
.p
= new_enode(polynomial
, dim
+1, param
+1);
744 for (int j
= 0; j
<= dim
; ++j
)
745 evalue_set(&EP
->x
.p
->arr
[j
], c
->p
[j
], c
->p
[dim
+1]);
748 static void multi_polynom(Vector
*c
, evalue
* X
, evalue
*EP
)
750 unsigned dim
= c
->Size
-2;
754 evalue_set(&EC
, c
->p
[dim
], c
->p
[dim
+1]);
757 evalue_set(EP
, c
->p
[dim
], c
->p
[dim
+1]);
759 for (int i
= dim
-1; i
>= 0; --i
) {
761 value_assign(EC
.x
.n
, c
->p
[i
]);
764 free_evalue_refs(&EC
);
767 Polyhedron
*unfringe (Polyhedron
*P
, unsigned MaxRays
)
769 int len
= P
->Dimension
+2;
770 Polyhedron
*T
, *R
= P
;
773 Vector
*row
= Vector_Alloc(len
);
774 value_set_si(row
->p
[0], 1);
776 R
= DomainConstraintSimplify(Polyhedron_Copy(P
), MaxRays
);
778 Matrix
*M
= Matrix_Alloc(2, len
-1);
779 value_set_si(M
->p
[1][len
-2], 1);
780 for (int v
= 0; v
< P
->Dimension
; ++v
) {
781 value_set_si(M
->p
[0][v
], 1);
782 Polyhedron
*I
= Polyhedron_Image(R
, M
, 2+1);
783 value_set_si(M
->p
[0][v
], 0);
784 for (int r
= 0; r
< I
->NbConstraints
; ++r
) {
785 if (value_zero_p(I
->Constraint
[r
][0]))
787 if (value_zero_p(I
->Constraint
[r
][1]))
789 if (value_one_p(I
->Constraint
[r
][1]))
791 if (value_mone_p(I
->Constraint
[r
][1]))
793 value_absolute(g
, I
->Constraint
[r
][1]);
794 Vector_Set(row
->p
+1, 0, len
-2);
795 value_division(row
->p
[1+v
], I
->Constraint
[r
][1], g
);
796 mpz_fdiv_q(row
->p
[len
-1], I
->Constraint
[r
][2], g
);
798 R
= AddConstraints(row
->p
, 1, R
, MaxRays
);
810 /* Check whether all rays point in the positive directions
813 static bool Polyhedron_has_positive_rays(Polyhedron
*P
, unsigned nparam
)
816 for (r
= 0; r
< P
->NbRays
; ++r
)
817 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1])) {
819 for (i
= P
->Dimension
- nparam
; i
< P
->Dimension
; ++i
)
820 if (value_neg_p(P
->Ray
[r
][i
+1]))
826 typedef evalue
* evalue_p
;
828 struct enumerator_base
{
832 vertex_decomposer
*vpd
;
834 enumerator_base(unsigned dim
, vertex_decomposer
*vpd
)
839 vE
= new evalue_p
[vpd
->nbV
];
840 for (int j
= 0; j
< vpd
->nbV
; ++j
)
844 evalue_set_si(&mone
, -1, 1);
847 void decompose_at(Param_Vertices
*V
, int _i
, barvinok_options
*options
) {
851 value_init(vE
[_i
]->d
);
852 evalue_set_si(vE
[_i
], 0, 1);
854 vpd
->decompose_at_vertex(V
, _i
, options
);
857 virtual ~enumerator_base() {
858 for (int j
= 0; j
< vpd
->nbV
; ++j
)
860 free_evalue_refs(vE
[j
]);
865 free_evalue_refs(&mone
);
868 static enumerator_base
*create(Polyhedron
*P
, unsigned dim
, unsigned nbV
,
869 barvinok_options
*options
);
872 struct enumerator
: public signed_cone_consumer
, public vertex_decomposer
,
873 public enumerator_base
{
881 enumerator(Polyhedron
*P
, unsigned dim
, unsigned nbV
) :
882 vertex_decomposer(P
, nbV
, *this), enumerator_base(dim
, this) {
885 randomvector(P
, lambda
, dim
);
887 c
= Vector_Alloc(dim
+2);
897 virtual void handle(const signed_cone
& sc
, barvinok_options
*options
);
900 void enumerator::handle(const signed_cone
& sc
, barvinok_options
*options
)
903 assert(sc
.rays
.NumRows() == dim
);
904 for (int k
= 0; k
< dim
; ++k
) {
905 if (lambda
* sc
.rays
[k
] == 0)
911 lattice_point(V
, sc
.rays
, lambda
, &num
, sc
.det
, sc
.closed
, options
);
912 den
= sc
.rays
* lambda
;
914 normalize(sign
, offset
, den
);
916 dpoly
n(dim
, den
[0], 1);
917 for (int k
= 1; k
< dim
; ++k
) {
918 dpoly
fact(dim
, den
[k
], 1);
923 dpoly_n
d(dim
, offset
, one
);
925 for (unsigned long i
= 0; i
< sc
.det
; ++i
) {
927 multi_polynom(c
, num
.E
[i
], &EV
);
928 eadd(&EV
, vE
[vert
]);
929 free_evalue_refs(&EV
);
930 free_evalue_refs(num
.E
[i
]);
935 mpq_set_si(count
, 0, 1);
936 if (num
.constant
.length() == 1) {
937 num
.constant
[0] += offset
;
938 dpoly
d(dim
, num
.constant
[0]);
939 d
.div(n
, count
, sign
);
942 dpoly_n
d(dim
, offset
, one
);
947 for (unsigned long i
= 0; i
< sc
.det
; ++i
) {
948 value_assign(acc
, c
->p
[dim
]);
949 zz2value(num
.constant
[i
], x
);
950 for (int j
= dim
-1; j
>= 0; --j
) {
951 value_multiply(acc
, acc
, x
);
952 value_addto(acc
, acc
, c
->p
[j
]);
954 value_addto(mpq_numref(count
), mpq_numref(count
), acc
);
956 mpz_set(mpq_denref(count
), c
->p
[dim
+1]);
962 evalue_set(&EV
, &count
[0]._mp_num
, &count
[0]._mp_den
);
964 free_evalue_refs(&EV
);
968 struct ienumerator_base
: enumerator_base
{
971 ienumerator_base(unsigned dim
, vertex_decomposer
*vpd
) :
972 enumerator_base(dim
,vpd
) {
973 E_vertex
= new evalue_p
[dim
];
976 virtual ~ienumerator_base() {
980 evalue
*E_num(int i
, int d
) {
981 return E_vertex
[i
+ (dim
-d
)];
990 cumulator(evalue
*factor
, evalue
*v
, dpoly_r
*r
) :
991 factor(factor
), v(v
), r(r
) {}
993 void cumulate(barvinok_options
*options
);
995 virtual void add_term(const vector
<int>& powers
, evalue
*f2
) = 0;
996 virtual ~cumulator() {}
999 void cumulator::cumulate(barvinok_options
*options
)
1001 evalue cum
; // factor * 1 * E_num[0]/1 * (E_num[0]-1)/2 *...
1003 evalue t
; // E_num[0] - (m-1)
1007 if (options
->lookup_table
) {
1009 evalue_set_si(&mone
, -1, 1);
1013 evalue_copy(&cum
, factor
);
1016 value_set_si(f
.d
, 1);
1017 value_set_si(f
.x
.n
, 1);
1021 if (!options
->lookup_table
) {
1022 for (cst
= &t
; value_zero_p(cst
->d
); ) {
1023 if (cst
->x
.p
->type
== fractional
)
1024 cst
= &cst
->x
.p
->arr
[1];
1026 cst
= &cst
->x
.p
->arr
[0];
1030 for (int m
= 0; m
< r
->len
; ++m
) {
1033 value_set_si(f
.d
, m
);
1035 if (!options
->lookup_table
)
1036 value_subtract(cst
->x
.n
, cst
->x
.n
, cst
->d
);
1042 dpoly_r_term_list
& current
= r
->c
[r
->len
-1-m
];
1043 dpoly_r_term_list::iterator j
;
1044 for (j
= current
.begin(); j
!= current
.end(); ++j
) {
1045 if ((*j
)->coeff
== 0)
1047 evalue
*f2
= new evalue
;
1049 value_init(f2
->x
.n
);
1050 zz2value((*j
)->coeff
, f2
->x
.n
);
1051 zz2value(r
->denom
, f2
->d
);
1054 add_term((*j
)->powers
, f2
);
1057 free_evalue_refs(&f
);
1058 free_evalue_refs(&t
);
1059 free_evalue_refs(&cum
);
1060 if (options
->lookup_table
)
1061 free_evalue_refs(&mone
);
1064 struct E_poly_term
{
1069 struct ie_cum
: public cumulator
{
1070 vector
<E_poly_term
*> terms
;
1072 ie_cum(evalue
*factor
, evalue
*v
, dpoly_r
*r
) : cumulator(factor
, v
, r
) {}
1074 virtual void add_term(const vector
<int>& powers
, evalue
*f2
);
1077 void ie_cum::add_term(const vector
<int>& powers
, evalue
*f2
)
1080 for (k
= 0; k
< terms
.size(); ++k
) {
1081 if (terms
[k
]->powers
== powers
) {
1082 eadd(f2
, terms
[k
]->E
);
1083 free_evalue_refs(f2
);
1088 if (k
>= terms
.size()) {
1089 E_poly_term
*ET
= new E_poly_term
;
1090 ET
->powers
= powers
;
1092 terms
.push_back(ET
);
1096 struct ienumerator
: public signed_cone_consumer
, public vertex_decomposer
,
1097 public ienumerator_base
{
1103 ienumerator(Polyhedron
*P
, unsigned dim
, unsigned nbV
) :
1104 vertex_decomposer(P
, nbV
, *this), ienumerator_base(dim
, this) {
1105 vertex
.SetDims(1, dim
);
1107 den
.SetDims(dim
, dim
);
1115 virtual void handle(const signed_cone
& sc
, barvinok_options
*options
);
1116 void reduce(evalue
*factor
, const mat_ZZ
& num
, const mat_ZZ
& den_f
,
1117 barvinok_options
*options
);
1120 void ienumerator::reduce(evalue
*factor
, const mat_ZZ
& num
, const mat_ZZ
& den_f
,
1121 barvinok_options
*options
)
1123 unsigned len
= den_f
.NumRows(); // number of factors in den
1124 unsigned dim
= num
.NumCols();
1125 assert(num
.NumRows() == 1);
1128 eadd(factor
, vE
[vert
]);
1137 split_one(num
, num_s
, num_p
, den_f
, den_s
, den_r
);
1140 den_p
.SetLength(len
);
1144 normalize(one
, num_s
, num_p
, den_s
, den_p
, den_r
);
1146 emul(&mone
, factor
);
1150 for (int k
= 0; k
< len
; ++k
) {
1153 else if (den_s
[k
] == 0)
1156 if (no_param
== 0) {
1157 reduce(factor
, num_p
, den_r
, options
);
1161 pden
.SetDims(only_param
, dim
-1);
1163 for (k
= 0, l
= 0; k
< len
; ++k
)
1165 pden
[l
++] = den_r
[k
];
1167 for (k
= 0; k
< len
; ++k
)
1171 dpoly
n(no_param
, num_s
[0]);
1172 dpoly
D(no_param
, den_s
[k
], 1);
1173 for ( ; ++k
< len
; )
1174 if (den_p
[k
] == 0) {
1175 dpoly
fact(no_param
, den_s
[k
], 1);
1180 // if no_param + only_param == len then all powers
1181 // below will be all zero
1182 if (no_param
+ only_param
== len
) {
1183 if (E_num(0, dim
) != 0)
1184 r
= new dpoly_r(n
, len
);
1186 mpq_set_si(tcount
, 0, 1);
1188 n
.div(D
, tcount
, one
);
1190 if (value_notzero_p(mpq_numref(tcount
))) {
1194 value_assign(f
.x
.n
, mpq_numref(tcount
));
1195 value_assign(f
.d
, mpq_denref(tcount
));
1197 reduce(factor
, num_p
, pden
, options
);
1198 free_evalue_refs(&f
);
1203 for (k
= 0; k
< len
; ++k
) {
1204 if (den_s
[k
] == 0 || den_p
[k
] == 0)
1207 dpoly
pd(no_param
-1, den_s
[k
], 1);
1210 for (l
= 0; l
< k
; ++l
)
1211 if (den_r
[l
] == den_r
[k
])
1215 r
= new dpoly_r(n
, pd
, l
, len
);
1217 dpoly_r
*nr
= new dpoly_r(r
, pd
, l
, len
);
1223 dpoly_r
*rc
= r
->div(D
);
1226 if (E_num(0, dim
) == 0) {
1227 int common
= pden
.NumRows();
1228 dpoly_r_term_list
& final
= r
->c
[r
->len
-1];
1234 zz2value(r
->denom
, f
.d
);
1235 dpoly_r_term_list::iterator j
;
1236 for (j
= final
.begin(); j
!= final
.end(); ++j
) {
1237 if ((*j
)->coeff
== 0)
1240 for (int k
= 0; k
< r
->dim
; ++k
) {
1241 int n
= (*j
)->powers
[k
];
1244 pden
.SetDims(rows
+n
, pden
.NumCols());
1245 for (int l
= 0; l
< n
; ++l
)
1246 pden
[rows
+l
] = den_r
[k
];
1250 evalue_copy(&t
, factor
);
1251 zz2value((*j
)->coeff
, f
.x
.n
);
1253 reduce(&t
, num_p
, pden
, options
);
1254 free_evalue_refs(&t
);
1256 free_evalue_refs(&f
);
1258 ie_cum
cum(factor
, E_num(0, dim
), r
);
1259 cum
.cumulate(options
);
1261 int common
= pden
.NumRows();
1263 for (int j
= 0; j
< cum
.terms
.size(); ++j
) {
1265 pden
.SetDims(rows
, pden
.NumCols());
1266 for (int k
= 0; k
< r
->dim
; ++k
) {
1267 int n
= cum
.terms
[j
]->powers
[k
];
1270 pden
.SetDims(rows
+n
, pden
.NumCols());
1271 for (int l
= 0; l
< n
; ++l
)
1272 pden
[rows
+l
] = den_r
[k
];
1275 reduce(cum
.terms
[j
]->E
, num_p
, pden
, options
);
1276 free_evalue_refs(cum
.terms
[j
]->E
);
1277 delete cum
.terms
[j
]->E
;
1278 delete cum
.terms
[j
];
1285 static int type_offset(enode
*p
)
1287 return p
->type
== fractional
? 1 :
1288 p
->type
== flooring
? 1 : 0;
1291 static int edegree(evalue
*e
)
1296 if (value_notzero_p(e
->d
))
1300 int i
= type_offset(p
);
1301 if (p
->size
-i
-1 > d
)
1302 d
= p
->size
- i
- 1;
1303 for (; i
< p
->size
; i
++) {
1304 int d2
= edegree(&p
->arr
[i
]);
1311 void ienumerator::handle(const signed_cone
& sc
, barvinok_options
*options
)
1313 assert(sc
.det
== 1);
1315 assert(sc
.rays
.NumRows() == dim
);
1317 lattice_point(V
, sc
.rays
, vertex
[0], E_vertex
, options
);
1323 evalue_set_si(&one
, sc
.sign
, 1);
1324 reduce(&one
, vertex
, den
, options
);
1325 free_evalue_refs(&one
);
1327 for (int i
= 0; i
< dim
; ++i
)
1329 free_evalue_refs(E_vertex
[i
]);
1334 struct bfenumerator
: public vertex_decomposer
, public bf_base
,
1335 public ienumerator_base
{
1338 bfenumerator(Polyhedron
*P
, unsigned dim
, unsigned nbV
) :
1339 vertex_decomposer(P
, nbV
, *this),
1340 bf_base(dim
), ienumerator_base(dim
, this) {
1348 virtual void handle(const signed_cone
& sc
, barvinok_options
*options
);
1349 virtual void base(mat_ZZ
& factors
, bfc_vec
& v
);
1351 bfc_term_base
* new_bf_term(int len
) {
1352 bfe_term
* t
= new bfe_term(len
);
1356 virtual void set_factor(bfc_term_base
*t
, int k
, int change
) {
1357 bfe_term
* bfet
= static_cast<bfe_term
*>(t
);
1358 factor
= bfet
->factors
[k
];
1359 assert(factor
!= NULL
);
1360 bfet
->factors
[k
] = NULL
;
1362 emul(&mone
, factor
);
1365 virtual void set_factor(bfc_term_base
*t
, int k
, mpq_t
&q
, int change
) {
1366 bfe_term
* bfet
= static_cast<bfe_term
*>(t
);
1367 factor
= bfet
->factors
[k
];
1368 assert(factor
!= NULL
);
1369 bfet
->factors
[k
] = NULL
;
1375 value_oppose(f
.x
.n
, mpq_numref(q
));
1377 value_assign(f
.x
.n
, mpq_numref(q
));
1378 value_assign(f
.d
, mpq_denref(q
));
1380 free_evalue_refs(&f
);
1383 virtual void set_factor(bfc_term_base
*t
, int k
, const QQ
& c
, int change
) {
1384 bfe_term
* bfet
= static_cast<bfe_term
*>(t
);
1386 factor
= new evalue
;
1391 zz2value(c
.n
, f
.x
.n
);
1393 value_oppose(f
.x
.n
, f
.x
.n
);
1396 value_init(factor
->d
);
1397 evalue_copy(factor
, bfet
->factors
[k
]);
1399 free_evalue_refs(&f
);
1402 void set_factor(evalue
*f
, int change
) {
1408 virtual void insert_term(bfc_term_base
*t
, int i
) {
1409 bfe_term
* bfet
= static_cast<bfe_term
*>(t
);
1410 int len
= t
->terms
.NumRows()-1; // already increased by one
1412 bfet
->factors
.resize(len
+1);
1413 for (int j
= len
; j
> i
; --j
) {
1414 bfet
->factors
[j
] = bfet
->factors
[j
-1];
1415 t
->terms
[j
] = t
->terms
[j
-1];
1417 bfet
->factors
[i
] = factor
;
1421 virtual void update_term(bfc_term_base
*t
, int i
) {
1422 bfe_term
* bfet
= static_cast<bfe_term
*>(t
);
1424 eadd(factor
, bfet
->factors
[i
]);
1425 free_evalue_refs(factor
);
1429 virtual bool constant_vertex(int dim
) { return E_num(0, dim
) == 0; }
1431 virtual void cum(bf_reducer
*bfr
, bfc_term_base
*t
, int k
, dpoly_r
*r
,
1432 barvinok_options
*options
);
1435 enumerator_base
*enumerator_base::create(Polyhedron
*P
, unsigned dim
, unsigned nbV
,
1436 barvinok_options
*options
)
1438 enumerator_base
*eb
;
1440 if (options
->incremental_specialization
== BV_SPECIALIZATION_BF
)
1441 eb
= new bfenumerator(P
, dim
, nbV
);
1442 else if (options
->incremental_specialization
== BV_SPECIALIZATION_DF
)
1443 eb
= new ienumerator(P
, dim
, nbV
);
1445 eb
= new enumerator(P
, dim
, nbV
);
1450 struct bfe_cum
: public cumulator
{
1452 bfc_term_base
*told
;
1456 bfe_cum(evalue
*factor
, evalue
*v
, dpoly_r
*r
, bf_reducer
*bfr
,
1457 bfc_term_base
*t
, int k
, bfenumerator
*e
) :
1458 cumulator(factor
, v
, r
), told(t
), k(k
),
1462 virtual void add_term(const vector
<int>& powers
, evalue
*f2
);
1465 void bfe_cum::add_term(const vector
<int>& powers
, evalue
*f2
)
1467 bfr
->update_powers(powers
);
1469 bfc_term_base
* t
= bfe
->find_bfc_term(bfr
->vn
, bfr
->npowers
, bfr
->nnf
);
1470 bfe
->set_factor(f2
, bfr
->l_changes
% 2);
1471 bfe
->add_term(t
, told
->terms
[k
], bfr
->l_extra_num
);
1474 void bfenumerator::cum(bf_reducer
*bfr
, bfc_term_base
*t
, int k
,
1475 dpoly_r
*r
, barvinok_options
*options
)
1477 bfe_term
* bfet
= static_cast<bfe_term
*>(t
);
1478 bfe_cum
cum(bfet
->factors
[k
], E_num(0, bfr
->d
), r
, bfr
, t
, k
, this);
1479 cum
.cumulate(options
);
1482 void bfenumerator::base(mat_ZZ
& factors
, bfc_vec
& v
)
1484 for (int i
= 0; i
< v
.size(); ++i
) {
1485 assert(v
[i
]->terms
.NumRows() == 1);
1486 evalue
*factor
= static_cast<bfe_term
*>(v
[i
])->factors
[0];
1487 eadd(factor
, vE
[vert
]);
1492 void bfenumerator::handle(const signed_cone
& sc
, barvinok_options
*options
)
1494 assert(sc
.det
== 1);
1496 assert(sc
.rays
.NumRows() == enumerator_base::dim
);
1498 bfe_term
* t
= new bfe_term(enumerator_base::dim
);
1499 vector
< bfc_term_base
* > v
;
1502 t
->factors
.resize(1);
1504 t
->terms
.SetDims(1, enumerator_base::dim
);
1505 lattice_point(V
, sc
.rays
, t
->terms
[0], E_vertex
, options
);
1507 // the elements of factors are always lexpositive
1509 int s
= setup_factors(sc
.rays
, factors
, t
, sc
.sign
);
1511 t
->factors
[0] = new evalue
;
1512 value_init(t
->factors
[0]->d
);
1513 evalue_set_si(t
->factors
[0], s
, 1);
1514 reduce(factors
, v
, options
);
1516 for (int i
= 0; i
< enumerator_base::dim
; ++i
)
1518 free_evalue_refs(E_vertex
[i
]);
1523 static inline Param_Polyhedron
*Polyhedron2Param_MR(Polyhedron
*Din
,
1524 Polyhedron
*Cin
, int WS
)
1526 if (WS
& POL_NO_DUAL
)
1528 return Polyhedron2Param_Domain(Din
, Cin
, WS
);
1531 static evalue
* barvinok_enumerate_ev_f(Polyhedron
*P
, Polyhedron
* C
,
1532 barvinok_options
*options
);
1535 static evalue
* barvinok_enumerate_cst(Polyhedron
*P
, Polyhedron
* C
,
1536 struct barvinok_options
*options
)
1540 ALLOC(evalue
, eres
);
1541 value_init(eres
->d
);
1542 value_set_si(eres
->d
, 0);
1543 eres
->x
.p
= new_enode(partition
, 2, C
->Dimension
);
1544 EVALUE_SET_DOMAIN(eres
->x
.p
->arr
[0],
1545 DomainConstraintSimplify(C
, options
->MaxRays
));
1546 value_set_si(eres
->x
.p
->arr
[1].d
, 1);
1547 value_init(eres
->x
.p
->arr
[1].x
.n
);
1549 value_set_si(eres
->x
.p
->arr
[1].x
.n
, 0);
1551 barvinok_count_with_options(P
, &eres
->x
.p
->arr
[1].x
.n
, options
);
1557 static evalue
* enumerate(Polyhedron
*P
, Polyhedron
* C
,
1558 struct barvinok_options
*options
)
1560 //P = unfringe(P, MaxRays);
1562 Polyhedron
*Corig
= C
;
1563 Polyhedron
*CEq
= NULL
, *rVD
;
1565 unsigned nparam
= C
->Dimension
;
1570 value_init(factor
.d
);
1571 evalue_set_si(&factor
, 1, 1);
1574 POL_ENSURE_FACETS(P
);
1575 POL_ENSURE_VERTICES(P
);
1576 POL_ENSURE_FACETS(C
);
1577 POL_ENSURE_VERTICES(C
);
1579 if (C
->Dimension
== 0 || emptyQ(P
)) {
1581 eres
= barvinok_enumerate_cst(P
, CEq
? CEq
: Polyhedron_Copy(C
), options
);
1584 evalue_backsubstitute(eres
, CP
, options
->MaxRays
);
1588 emul(&factor
, eres
);
1589 if (options
->approximation_method
== BV_APPROX_DROP
) {
1590 if (options
->polynomial_approximation
== BV_APPROX_SIGN_UPPER
)
1591 evalue_frac2polynomial(eres
, 1, options
->MaxRays
);
1592 if (options
->polynomial_approximation
== BV_APPROX_SIGN_LOWER
)
1593 evalue_frac2polynomial(eres
, -1, options
->MaxRays
);
1594 if (options
->polynomial_approximation
== BV_APPROX_SIGN_APPROX
)
1595 evalue_frac2polynomial(eres
, 0, options
->MaxRays
);
1597 reduce_evalue(eres
);
1598 free_evalue_refs(&factor
);
1605 if (Polyhedron_is_unbounded(P
, nparam
, options
->MaxRays
))
1610 P
= remove_equalities_p(P
, P
->Dimension
-nparam
, &f
, options
->MaxRays
);
1611 mask(f
, &factor
, options
);
1614 if (P
->Dimension
== nparam
) {
1616 P
= Universe_Polyhedron(0);
1622 remove_all_equalities(&Q
, &C
, &CP
, NULL
, nparam
, options
->MaxRays
);
1623 if (C
!= D
&& D
!= Corig
)
1625 eres
= enumerate(Q
, C
, options
);
1629 Polyhedron
*T
= Polyhedron_Factor(P
, nparam
, NULL
, options
->MaxRays
);
1630 if (T
|| (P
->Dimension
== nparam
+1)) {
1633 for (Q
= T
? T
: P
; Q
; Q
= Q
->next
) {
1634 Polyhedron
*next
= Q
->next
;
1638 if (Q
->Dimension
!= C
->Dimension
)
1639 QC
= Polyhedron_Project(Q
, nparam
);
1642 C
= DomainIntersection(C
, QC
, options
->MaxRays
);
1644 Polyhedron_Free(C2
);
1646 Polyhedron_Free(QC
);
1654 if (T
->Dimension
== C
->Dimension
) {
1663 eres
= barvinok_enumerate_ev_f(P
, C
, options
);
1670 for (Q
= P
->next
; Q
; Q
= Q
->next
) {
1671 Polyhedron
*next
= Q
->next
;
1674 f
= barvinok_enumerate_ev_f(Q
, C
, options
);
1676 free_evalue_refs(f
);
1686 evalue
* barvinok_enumerate_with_options(Polyhedron
*P
, Polyhedron
* C
,
1687 struct barvinok_options
*options
)
1689 Polyhedron
*next
, *Cnext
, *CA
;
1690 Polyhedron
*Porig
= P
;
1695 "barvinok_enumerate: input is a union; only first polyhedron is enumerated\n");
1699 "barvinok_enumerate: context is a union; only first polyhedron is considered\n");
1703 CA
= align_context(C
, P
->Dimension
, options
->MaxRays
);
1706 P
= DomainIntersection(P
, CA
, options
->MaxRays
);
1708 Polyhedron_Free(CA
);
1710 eres
= enumerate(P
, C
, options
);
1717 evalue
* barvinok_enumerate_ev(Polyhedron
*P
, Polyhedron
* C
, unsigned MaxRays
)
1720 barvinok_options
*options
= barvinok_options_new_with_defaults();
1721 options
->MaxRays
= MaxRays
;
1722 E
= barvinok_enumerate_with_options(P
, C
, options
);
1723 barvinok_options_free(options
);
1727 evalue
*Param_Polyhedron_Enumerate(Param_Polyhedron
*PP
, Polyhedron
*P
,
1729 struct barvinok_options
*options
)
1733 unsigned nparam
= C
->Dimension
;
1734 unsigned dim
= P
->Dimension
- nparam
;
1736 ALLOC(evalue
, eres
);
1737 value_init(eres
->d
);
1738 value_set_si(eres
->d
, 0);
1741 for (nd
= 0, D
=PP
->D
; D
; ++nd
, D
=D
->next
);
1742 struct section
{ Polyhedron
*D
; evalue E
; };
1743 section
*s
= new section
[nd
];
1745 enumerator_base
*et
= NULL
;
1750 et
= enumerator_base::create(P
, dim
, PP
->nbV
, options
);
1752 Polyhedron
*TC
= true_context(P
, C
, options
->MaxRays
);
1753 FORALL_REDUCED_DOMAIN(PP
, TC
, nd
, options
, i
, D
, rVD
)
1756 value_init(s
[i
].E
.d
);
1757 evalue_set_si(&s
[i
].E
, 0, 1);
1760 FORALL_PVertex_in_ParamPolyhedron(V
,D
,PP
) // _i is internal counter
1763 et
->decompose_at(V
, _i
, options
);
1764 } catch (OrthogonalException
&e
) {
1765 FORALL_REDUCED_DOMAIN_RESET
;
1766 for (; i
>= 0; --i
) {
1767 free_evalue_refs(&s
[i
].E
);
1768 Domain_Free(s
[i
].D
);
1772 eadd(et
->vE
[_i
] , &s
[i
].E
);
1773 END_FORALL_PVertex_in_ParamPolyhedron
;
1774 evalue_range_reduction_in_domain(&s
[i
].E
, rVD
);
1775 END_FORALL_REDUCED_DOMAIN
1776 Polyhedron_Free(TC
);
1780 evalue_set_si(eres
, 0, 1);
1782 eres
->x
.p
= new_enode(partition
, 2*nd
, C
->Dimension
);
1783 for (int j
= 0; j
< nd
; ++j
) {
1784 EVALUE_SET_DOMAIN(eres
->x
.p
->arr
[2*j
], s
[j
].D
);
1785 value_clear(eres
->x
.p
->arr
[2*j
+1].d
);
1786 eres
->x
.p
->arr
[2*j
+1] = s
[j
].E
;
1794 static evalue
* barvinok_enumerate_ev_f(Polyhedron
*P
, Polyhedron
* C
,
1795 barvinok_options
*options
)
1797 unsigned nparam
= C
->Dimension
;
1798 bool do_scale
= options
->approximation_method
== BV_APPROX_SCALE
;
1800 if (options
->approximation_method
== BV_APPROX_VOLUME
)
1801 return Param_Polyhedron_Volume(P
, C
, options
);
1803 if (P
->Dimension
- nparam
== 1 && !do_scale
)
1804 return ParamLine_Length(P
, C
, options
);
1806 Param_Polyhedron
*PP
= NULL
;
1810 eres
= scale_bound(P
, C
, options
);
1815 PP
= Polyhedron2Param_MR(P
, C
, options
->MaxRays
);
1818 eres
= scale(PP
, P
, C
, options
);
1820 eres
= Param_Polyhedron_Enumerate(PP
, P
, C
, options
);
1823 Param_Polyhedron_Free(PP
);
1828 Enumeration
* barvinok_enumerate(Polyhedron
*P
, Polyhedron
* C
, unsigned MaxRays
)
1830 evalue
*EP
= barvinok_enumerate_ev(P
, C
, MaxRays
);
1832 return partition2enumeration(EP
);
1835 static void SwapColumns(Value
**V
, int n
, int i
, int j
)
1837 for (int r
= 0; r
< n
; ++r
)
1838 value_swap(V
[r
][i
], V
[r
][j
]);
1841 static void SwapColumns(Polyhedron
*P
, int i
, int j
)
1843 SwapColumns(P
->Constraint
, P
->NbConstraints
, i
, j
);
1844 SwapColumns(P
->Ray
, P
->NbRays
, i
, j
);
1847 /* Construct a constraint c from constraints l and u such that if
1848 * if constraint c holds then for each value of the other variables
1849 * there is at most one value of variable pos (position pos+1 in the constraints).
1851 * Given a lower and an upper bound
1852 * n_l v_i + <c_l,x> + c_l >= 0
1853 * -n_u v_i + <c_u,x> + c_u >= 0
1854 * the constructed constraint is
1856 * -(n_l<c_u,x> + n_u<c_l,x>) + (-n_l c_u - n_u c_l + n_l n_u - 1)
1858 * which is then simplified to remove the content of the non-constant coefficients
1860 * len is the total length of the constraints.
1861 * v is a temporary variable that can be used by this procedure
1863 static void negative_test_constraint(Value
*l
, Value
*u
, Value
*c
, int pos
,
1866 value_oppose(*v
, u
[pos
+1]);
1867 Vector_Combine(l
+1, u
+1, c
+1, *v
, l
[pos
+1], len
-1);
1868 value_multiply(*v
, *v
, l
[pos
+1]);
1869 value_subtract(c
[len
-1], c
[len
-1], *v
);
1870 value_set_si(*v
, -1);
1871 Vector_Scale(c
+1, c
+1, *v
, len
-1);
1872 value_decrement(c
[len
-1], c
[len
-1]);
1873 ConstraintSimplify(c
, c
, len
, v
);
1876 static bool parallel_constraints(Value
*l
, Value
*u
, Value
*c
, int pos
,
1885 Vector_Gcd(&l
[1+pos
], len
, &g1
);
1886 Vector_Gcd(&u
[1+pos
], len
, &g2
);
1887 Vector_Combine(l
+1+pos
, u
+1+pos
, c
+1, g2
, g1
, len
);
1888 parallel
= First_Non_Zero(c
+1, len
) == -1;
1896 static void negative_test_constraint7(Value
*l
, Value
*u
, Value
*c
, int pos
,
1897 int exist
, int len
, Value
*v
)
1902 Vector_Gcd(&u
[1+pos
], exist
, v
);
1903 Vector_Gcd(&l
[1+pos
], exist
, &g
);
1904 Vector_Combine(l
+1, u
+1, c
+1, *v
, g
, len
-1);
1905 value_multiply(*v
, *v
, g
);
1906 value_subtract(c
[len
-1], c
[len
-1], *v
);
1907 value_set_si(*v
, -1);
1908 Vector_Scale(c
+1, c
+1, *v
, len
-1);
1909 value_decrement(c
[len
-1], c
[len
-1]);
1910 ConstraintSimplify(c
, c
, len
, v
);
1915 /* Turns a x + b >= 0 into a x + b <= -1
1917 * len is the total length of the constraint.
1918 * v is a temporary variable that can be used by this procedure
1920 static void oppose_constraint(Value
*c
, int len
, Value
*v
)
1922 value_set_si(*v
, -1);
1923 Vector_Scale(c
+1, c
+1, *v
, len
-1);
1924 value_decrement(c
[len
-1], c
[len
-1]);
1927 /* Split polyhedron P into two polyhedra *pos and *neg, where
1928 * existential variable i has at most one solution for each
1929 * value of the other variables in *neg.
1931 * The splitting is performed using constraints l and u.
1933 * nvar: number of set variables
1934 * row: temporary vector that can be used by this procedure
1935 * f: temporary value that can be used by this procedure
1937 static bool SplitOnConstraint(Polyhedron
*P
, int i
, int l
, int u
,
1938 int nvar
, int MaxRays
, Vector
*row
, Value
& f
,
1939 Polyhedron
**pos
, Polyhedron
**neg
)
1941 negative_test_constraint(P
->Constraint
[l
], P
->Constraint
[u
],
1942 row
->p
, nvar
+i
, P
->Dimension
+2, &f
);
1943 *neg
= AddConstraints(row
->p
, 1, P
, MaxRays
);
1945 /* We found an independent, but useless constraint
1946 * Maybe we should detect this earlier and not
1947 * mark the variable as INDEPENDENT
1949 if (emptyQ((*neg
))) {
1950 Polyhedron_Free(*neg
);
1954 oppose_constraint(row
->p
, P
->Dimension
+2, &f
);
1955 *pos
= AddConstraints(row
->p
, 1, P
, MaxRays
);
1957 if (emptyQ((*pos
))) {
1958 Polyhedron_Free(*neg
);
1959 Polyhedron_Free(*pos
);
1967 * unimodularly transform P such that constraint r is transformed
1968 * into a constraint that involves only a single (the first)
1969 * existential variable
1972 static Polyhedron
*rotate_along(Polyhedron
*P
, int r
, int nvar
, int exist
,
1978 Matrix
*M
= Matrix_Alloc(exist
, exist
);
1979 Vector_Copy(P
->Constraint
[r
]+1+nvar
, M
->p
[0], exist
);
1980 Vector_Gcd(M
->p
[0], exist
, &g
);
1981 if (value_notone_p(g
))
1982 Vector_AntiScale(M
->p
[0], M
->p
[0], g
, exist
);
1985 int ok
= unimodular_complete(M
, 1);
1987 Matrix
*M2
= Matrix_Alloc(P
->Dimension
+1, P
->Dimension
+1);
1988 for (r
= 0; r
< nvar
; ++r
)
1989 value_set_si(M2
->p
[r
][r
], 1);
1990 for ( ; r
< nvar
+exist
; ++r
)
1991 Vector_Copy(M
->p
[r
-nvar
], M2
->p
[r
]+nvar
, exist
);
1992 for ( ; r
< P
->Dimension
+1; ++r
)
1993 value_set_si(M2
->p
[r
][r
], 1);
1994 Polyhedron
*T
= Polyhedron_Image(P
, M2
, MaxRays
);
2002 /* Split polyhedron P into two polyhedra *pos and *neg, where
2003 * existential variable i has at most one solution for each
2004 * value of the other variables in *neg.
2006 * If independent is set, then the two constraints on which the
2007 * split will be performed need to be independent of the other
2008 * existential variables.
2010 * Return true if an appropriate split could be performed.
2012 * nvar: number of set variables
2013 * exist: number of existential variables
2014 * row: temporary vector that can be used by this procedure
2015 * f: temporary value that can be used by this procedure
2017 static bool SplitOnVar(Polyhedron
*P
, int i
,
2018 int nvar
, int exist
, int MaxRays
,
2019 Vector
*row
, Value
& f
, bool independent
,
2020 Polyhedron
**pos
, Polyhedron
**neg
)
2024 for (int l
= P
->NbEq
; l
< P
->NbConstraints
; ++l
) {
2025 if (value_negz_p(P
->Constraint
[l
][nvar
+i
+1]))
2029 for (j
= 0; j
< exist
; ++j
)
2030 if (j
!= i
&& value_notzero_p(P
->Constraint
[l
][nvar
+j
+1]))
2036 for (int u
= P
->NbEq
; u
< P
->NbConstraints
; ++u
) {
2037 if (value_posz_p(P
->Constraint
[u
][nvar
+i
+1]))
2041 for (j
= 0; j
< exist
; ++j
)
2042 if (j
!= i
&& value_notzero_p(P
->Constraint
[u
][nvar
+j
+1]))
2048 if (SplitOnConstraint(P
, i
, l
, u
, nvar
, MaxRays
, row
, f
, pos
, neg
)) {
2051 SwapColumns(*neg
, nvar
+1, nvar
+1+i
);
2061 static bool double_bound_pair(Polyhedron
*P
, int nvar
, int exist
,
2062 int i
, int l1
, int l2
,
2063 Polyhedron
**pos
, Polyhedron
**neg
)
2067 Vector
*row
= Vector_Alloc(P
->Dimension
+2);
2068 value_set_si(row
->p
[0], 1);
2069 value_oppose(f
, P
->Constraint
[l1
][nvar
+i
+1]);
2070 Vector_Combine(P
->Constraint
[l1
]+1, P
->Constraint
[l2
]+1,
2072 P
->Constraint
[l2
][nvar
+i
+1], f
,
2074 ConstraintSimplify(row
->p
, row
->p
, P
->Dimension
+2, &f
);
2075 *pos
= AddConstraints(row
->p
, 1, P
, 0);
2076 value_set_si(f
, -1);
2077 Vector_Scale(row
->p
+1, row
->p
+1, f
, P
->Dimension
+1);
2078 value_decrement(row
->p
[P
->Dimension
+1], row
->p
[P
->Dimension
+1]);
2079 *neg
= AddConstraints(row
->p
, 1, P
, 0);
2083 return !emptyQ((*pos
)) && !emptyQ((*neg
));
2086 static bool double_bound(Polyhedron
*P
, int nvar
, int exist
,
2087 Polyhedron
**pos
, Polyhedron
**neg
)
2089 for (int i
= 0; i
< exist
; ++i
) {
2091 for (l1
= P
->NbEq
; l1
< P
->NbConstraints
; ++l1
) {
2092 if (value_negz_p(P
->Constraint
[l1
][nvar
+i
+1]))
2094 for (l2
= l1
+ 1; l2
< P
->NbConstraints
; ++l2
) {
2095 if (value_negz_p(P
->Constraint
[l2
][nvar
+i
+1]))
2097 if (double_bound_pair(P
, nvar
, exist
, i
, l1
, l2
, pos
, neg
))
2101 for (l1
= P
->NbEq
; l1
< P
->NbConstraints
; ++l1
) {
2102 if (value_posz_p(P
->Constraint
[l1
][nvar
+i
+1]))
2104 if (l1
< P
->NbConstraints
)
2105 for (l2
= l1
+ 1; l2
< P
->NbConstraints
; ++l2
) {
2106 if (value_posz_p(P
->Constraint
[l2
][nvar
+i
+1]))
2108 if (double_bound_pair(P
, nvar
, exist
, i
, l1
, l2
, pos
, neg
))
2120 INDEPENDENT
= 1 << 2,
2124 static evalue
* enumerate_or(Polyhedron
*D
,
2125 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2128 fprintf(stderr
, "\nER: Or\n");
2129 #endif /* DEBUG_ER */
2131 Polyhedron
*N
= D
->next
;
2134 barvinok_enumerate_e_with_options(D
, exist
, nparam
, options
);
2137 for (D
= N
; D
; D
= N
) {
2142 barvinok_enumerate_e_with_options(D
, exist
, nparam
, options
);
2145 free_evalue_refs(EN
);
2155 static evalue
* enumerate_sum(Polyhedron
*P
,
2156 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2158 int nvar
= P
->Dimension
- exist
- nparam
;
2159 int toswap
= nvar
< exist
? nvar
: exist
;
2160 for (int i
= 0; i
< toswap
; ++i
)
2161 SwapColumns(P
, 1 + i
, nvar
+exist
- i
);
2165 fprintf(stderr
, "\nER: Sum\n");
2166 #endif /* DEBUG_ER */
2168 evalue
*EP
= barvinok_enumerate_e_with_options(P
, exist
, nparam
, options
);
2170 evalue_split_domains_into_orthants(EP
, options
->MaxRays
);
2172 evalue_range_reduction(EP
);
2174 evalue_frac2floor2(EP
, 1);
2176 evalue
*sum
= esum(EP
, nvar
);
2178 free_evalue_refs(EP
);
2182 evalue_range_reduction(EP
);
2187 static evalue
* split_sure(Polyhedron
*P
, Polyhedron
*S
,
2188 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2190 int nvar
= P
->Dimension
- exist
- nparam
;
2192 Matrix
*M
= Matrix_Alloc(exist
, S
->Dimension
+2);
2193 for (int i
= 0; i
< exist
; ++i
)
2194 value_set_si(M
->p
[i
][nvar
+i
+1], 1);
2196 S
= DomainAddRays(S
, M
, options
->MaxRays
);
2198 Polyhedron
*F
= DomainAddRays(P
, M
, options
->MaxRays
);
2199 Polyhedron
*D
= DomainDifference(F
, S
, options
->MaxRays
);
2201 D
= Disjoint_Domain(D
, 0, options
->MaxRays
);
2206 M
= Matrix_Alloc(P
->Dimension
+1-exist
, P
->Dimension
+1);
2207 for (int j
= 0; j
< nvar
; ++j
)
2208 value_set_si(M
->p
[j
][j
], 1);
2209 for (int j
= 0; j
< nparam
+1; ++j
)
2210 value_set_si(M
->p
[nvar
+j
][nvar
+exist
+j
], 1);
2211 Polyhedron
*T
= Polyhedron_Image(S
, M
, options
->MaxRays
);
2212 evalue
*EP
= barvinok_enumerate_e_with_options(T
, 0, nparam
, options
);
2217 for (Polyhedron
*Q
= D
; Q
; Q
= Q
->next
) {
2218 Polyhedron
*N
= Q
->next
;
2220 T
= DomainIntersection(P
, Q
, options
->MaxRays
);
2221 evalue
*E
= barvinok_enumerate_e_with_options(T
, exist
, nparam
, options
);
2223 free_evalue_refs(E
);
2232 static evalue
* enumerate_sure(Polyhedron
*P
,
2233 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2237 int nvar
= P
->Dimension
- exist
- nparam
;
2243 for (i
= 0; i
< exist
; ++i
) {
2244 Matrix
*M
= Matrix_Alloc(S
->NbConstraints
, S
->Dimension
+2);
2246 value_set_si(lcm
, 1);
2247 for (int j
= 0; j
< S
->NbConstraints
; ++j
) {
2248 if (value_negz_p(S
->Constraint
[j
][1+nvar
+i
]))
2250 if (value_one_p(S
->Constraint
[j
][1+nvar
+i
]))
2252 value_lcm(lcm
, S
->Constraint
[j
][1+nvar
+i
], &lcm
);
2255 for (int j
= 0; j
< S
->NbConstraints
; ++j
) {
2256 if (value_negz_p(S
->Constraint
[j
][1+nvar
+i
]))
2258 if (value_one_p(S
->Constraint
[j
][1+nvar
+i
]))
2260 value_division(f
, lcm
, S
->Constraint
[j
][1+nvar
+i
]);
2261 Vector_Scale(S
->Constraint
[j
], M
->p
[c
], f
, S
->Dimension
+2);
2262 value_subtract(M
->p
[c
][S
->Dimension
+1],
2263 M
->p
[c
][S
->Dimension
+1],
2265 value_increment(M
->p
[c
][S
->Dimension
+1],
2266 M
->p
[c
][S
->Dimension
+1]);
2270 S
= AddConstraints(M
->p
[0], c
, S
, options
->MaxRays
);
2285 fprintf(stderr
, "\nER: Sure\n");
2286 #endif /* DEBUG_ER */
2288 return split_sure(P
, S
, exist
, nparam
, options
);
2291 static evalue
* enumerate_sure2(Polyhedron
*P
,
2292 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2294 int nvar
= P
->Dimension
- exist
- nparam
;
2296 for (r
= 0; r
< P
->NbRays
; ++r
)
2297 if (value_one_p(P
->Ray
[r
][0]) &&
2298 value_one_p(P
->Ray
[r
][P
->Dimension
+1]))
2304 Matrix
*M
= Matrix_Alloc(nvar
+ 1 + nparam
, P
->Dimension
+2);
2305 for (int i
= 0; i
< nvar
; ++i
)
2306 value_set_si(M
->p
[i
][1+i
], 1);
2307 for (int i
= 0; i
< nparam
; ++i
)
2308 value_set_si(M
->p
[i
+nvar
][1+nvar
+exist
+i
], 1);
2309 Vector_Copy(P
->Ray
[r
]+1+nvar
, M
->p
[nvar
+nparam
]+1+nvar
, exist
);
2310 value_set_si(M
->p
[nvar
+nparam
][0], 1);
2311 value_set_si(M
->p
[nvar
+nparam
][P
->Dimension
+1], 1);
2312 Polyhedron
* F
= Rays2Polyhedron(M
, options
->MaxRays
);
2315 Polyhedron
*I
= DomainIntersection(F
, P
, options
->MaxRays
);
2319 fprintf(stderr
, "\nER: Sure2\n");
2320 #endif /* DEBUG_ER */
2322 return split_sure(P
, I
, exist
, nparam
, options
);
2325 static evalue
* enumerate_cyclic(Polyhedron
*P
,
2326 unsigned exist
, unsigned nparam
,
2327 evalue
* EP
, int r
, int p
, unsigned MaxRays
)
2329 int nvar
= P
->Dimension
- exist
- nparam
;
2331 /* If EP in its fractional maps only contains references
2332 * to the remainder parameter with appropriate coefficients
2333 * then we could in principle avoid adding existentially
2334 * quantified variables to the validity domains.
2335 * We'd have to replace the remainder by m { p/m }
2336 * and multiply with an appropriate factor that is one
2337 * only in the appropriate range.
2338 * This last multiplication can be avoided if EP
2339 * has a single validity domain with no (further)
2340 * constraints on the remainder parameter
2343 Matrix
*CT
= Matrix_Alloc(nparam
+1, nparam
+3);
2344 Matrix
*M
= Matrix_Alloc(1, 1+nparam
+3);
2345 for (int j
= 0; j
< nparam
; ++j
)
2347 value_set_si(CT
->p
[j
][j
], 1);
2348 value_set_si(CT
->p
[p
][nparam
+1], 1);
2349 value_set_si(CT
->p
[nparam
][nparam
+2], 1);
2350 value_set_si(M
->p
[0][1+p
], -1);
2351 value_absolute(M
->p
[0][1+nparam
], P
->Ray
[0][1+nvar
+exist
+p
]);
2352 value_set_si(M
->p
[0][1+nparam
+1], 1);
2353 Polyhedron
*CEq
= Constraints2Polyhedron(M
, 1);
2355 addeliminatedparams_enum(EP
, CT
, CEq
, MaxRays
, nparam
);
2356 Polyhedron_Free(CEq
);
2362 static void enumerate_vd_add_ray(evalue
*EP
, Matrix
*Rays
, unsigned MaxRays
)
2364 if (value_notzero_p(EP
->d
))
2367 assert(EP
->x
.p
->type
== partition
);
2368 assert(EP
->x
.p
->pos
== EVALUE_DOMAIN(EP
->x
.p
->arr
[0])->Dimension
);
2369 for (int i
= 0; i
< EP
->x
.p
->size
/2; ++i
) {
2370 Polyhedron
*D
= EVALUE_DOMAIN(EP
->x
.p
->arr
[2*i
]);
2371 Polyhedron
*N
= DomainAddRays(D
, Rays
, MaxRays
);
2372 EVALUE_SET_DOMAIN(EP
->x
.p
->arr
[2*i
], N
);
2377 static evalue
* enumerate_line(Polyhedron
*P
,
2378 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2384 fprintf(stderr
, "\nER: Line\n");
2385 #endif /* DEBUG_ER */
2387 int nvar
= P
->Dimension
- exist
- nparam
;
2389 for (i
= 0; i
< nparam
; ++i
)
2390 if (value_notzero_p(P
->Ray
[0][1+nvar
+exist
+i
]))
2393 for (j
= i
+1; j
< nparam
; ++j
)
2394 if (value_notzero_p(P
->Ray
[0][1+nvar
+exist
+i
]))
2396 assert(j
>= nparam
); // for now
2398 Matrix
*M
= Matrix_Alloc(2, P
->Dimension
+2);
2399 value_set_si(M
->p
[0][0], 1);
2400 value_set_si(M
->p
[0][1+nvar
+exist
+i
], 1);
2401 value_set_si(M
->p
[1][0], 1);
2402 value_set_si(M
->p
[1][1+nvar
+exist
+i
], -1);
2403 value_absolute(M
->p
[1][1+P
->Dimension
], P
->Ray
[0][1+nvar
+exist
+i
]);
2404 value_decrement(M
->p
[1][1+P
->Dimension
], M
->p
[1][1+P
->Dimension
]);
2405 Polyhedron
*S
= AddConstraints(M
->p
[0], 2, P
, options
->MaxRays
);
2406 evalue
*EP
= barvinok_enumerate_e_with_options(S
, exist
, nparam
, options
);
2410 return enumerate_cyclic(P
, exist
, nparam
, EP
, 0, i
, options
->MaxRays
);
2413 static int single_param_pos(Polyhedron
*P
, unsigned exist
, unsigned nparam
,
2416 int nvar
= P
->Dimension
- exist
- nparam
;
2417 if (First_Non_Zero(P
->Ray
[r
]+1, nvar
) != -1)
2419 int i
= First_Non_Zero(P
->Ray
[r
]+1+nvar
+exist
, nparam
);
2422 if (First_Non_Zero(P
->Ray
[r
]+1+nvar
+exist
+1, nparam
-i
-1) != -1)
2427 static evalue
* enumerate_remove_ray(Polyhedron
*P
, int r
,
2428 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2431 fprintf(stderr
, "\nER: RedundantRay\n");
2432 #endif /* DEBUG_ER */
2436 value_set_si(one
, 1);
2437 int len
= P
->NbRays
-1;
2438 Matrix
*M
= Matrix_Alloc(2 * len
, P
->Dimension
+2);
2439 Vector_Copy(P
->Ray
[0], M
->p
[0], r
* (P
->Dimension
+2));
2440 Vector_Copy(P
->Ray
[r
+1], M
->p
[r
], (len
-r
) * (P
->Dimension
+2));
2441 for (int j
= 0; j
< P
->NbRays
; ++j
) {
2444 Vector_Combine(P
->Ray
[j
], P
->Ray
[r
], M
->p
[len
+j
-(j
>r
)],
2445 one
, P
->Ray
[j
][P
->Dimension
+1], P
->Dimension
+2);
2448 P
= Rays2Polyhedron(M
, options
->MaxRays
);
2450 evalue
*EP
= barvinok_enumerate_e_with_options(P
, exist
, nparam
, options
);
2457 static evalue
* enumerate_redundant_ray(Polyhedron
*P
,
2458 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2460 assert(P
->NbBid
== 0);
2461 int nvar
= P
->Dimension
- exist
- nparam
;
2465 for (int r
= 0; r
< P
->NbRays
; ++r
) {
2466 if (value_notzero_p(P
->Ray
[r
][P
->Dimension
+1]))
2468 int i1
= single_param_pos(P
, exist
, nparam
, r
);
2471 for (int r2
= r
+1; r2
< P
->NbRays
; ++r2
) {
2472 if (value_notzero_p(P
->Ray
[r2
][P
->Dimension
+1]))
2474 int i2
= single_param_pos(P
, exist
, nparam
, r2
);
2480 value_division(m
, P
->Ray
[r
][1+nvar
+exist
+i1
],
2481 P
->Ray
[r2
][1+nvar
+exist
+i1
]);
2482 value_multiply(m
, m
, P
->Ray
[r2
][1+nvar
+exist
+i1
]);
2483 /* r2 divides r => r redundant */
2484 if (value_eq(m
, P
->Ray
[r
][1+nvar
+exist
+i1
])) {
2486 return enumerate_remove_ray(P
, r
, exist
, nparam
, options
);
2489 value_division(m
, P
->Ray
[r2
][1+nvar
+exist
+i1
],
2490 P
->Ray
[r
][1+nvar
+exist
+i1
]);
2491 value_multiply(m
, m
, P
->Ray
[r
][1+nvar
+exist
+i1
]);
2492 /* r divides r2 => r2 redundant */
2493 if (value_eq(m
, P
->Ray
[r2
][1+nvar
+exist
+i1
])) {
2495 return enumerate_remove_ray(P
, r2
, exist
, nparam
, options
);
2503 static Polyhedron
*upper_bound(Polyhedron
*P
,
2504 int pos
, Value
*max
, Polyhedron
**R
)
2513 for (Polyhedron
*Q
= P
; Q
; Q
= N
) {
2515 for (r
= 0; r
< P
->NbRays
; ++r
) {
2516 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1]) &&
2517 value_pos_p(P
->Ray
[r
][1+pos
]))
2520 if (r
< P
->NbRays
) {
2528 for (r
= 0; r
< P
->NbRays
; ++r
) {
2529 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1]))
2531 mpz_fdiv_q(v
, P
->Ray
[r
][1+pos
], P
->Ray
[r
][1+P
->Dimension
]);
2532 if ((!Q
->next
&& r
== 0) || value_gt(v
, *max
))
2533 value_assign(*max
, v
);
2540 static evalue
* enumerate_ray(Polyhedron
*P
,
2541 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2543 assert(P
->NbBid
== 0);
2544 int nvar
= P
->Dimension
- exist
- nparam
;
2547 for (r
= 0; r
< P
->NbRays
; ++r
)
2548 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1]))
2554 for (r2
= r
+1; r2
< P
->NbRays
; ++r2
)
2555 if (value_zero_p(P
->Ray
[r2
][P
->Dimension
+1]))
2557 if (r2
< P
->NbRays
) {
2559 return enumerate_sum(P
, exist
, nparam
, options
);
2563 fprintf(stderr
, "\nER: Ray\n");
2564 #endif /* DEBUG_ER */
2570 value_set_si(one
, 1);
2571 int i
= single_param_pos(P
, exist
, nparam
, r
);
2572 assert(i
!= -1); // for now;
2574 Matrix
*M
= Matrix_Alloc(P
->NbRays
, P
->Dimension
+2);
2575 for (int j
= 0; j
< P
->NbRays
; ++j
) {
2576 Vector_Combine(P
->Ray
[j
], P
->Ray
[r
], M
->p
[j
],
2577 one
, P
->Ray
[j
][P
->Dimension
+1], P
->Dimension
+2);
2579 Polyhedron
*S
= Rays2Polyhedron(M
, options
->MaxRays
);
2581 Polyhedron
*D
= DomainDifference(P
, S
, options
->MaxRays
);
2583 // Polyhedron_Print(stderr, P_VALUE_FMT, D);
2584 assert(value_pos_p(P
->Ray
[r
][1+nvar
+exist
+i
])); // for now
2586 D
= upper_bound(D
, nvar
+exist
+i
, &m
, &R
);
2590 M
= Matrix_Alloc(2, P
->Dimension
+2);
2591 value_set_si(M
->p
[0][0], 1);
2592 value_set_si(M
->p
[1][0], 1);
2593 value_set_si(M
->p
[0][1+nvar
+exist
+i
], -1);
2594 value_set_si(M
->p
[1][1+nvar
+exist
+i
], 1);
2595 value_assign(M
->p
[0][1+P
->Dimension
], m
);
2596 value_oppose(M
->p
[1][1+P
->Dimension
], m
);
2597 value_addto(M
->p
[1][1+P
->Dimension
], M
->p
[1][1+P
->Dimension
],
2598 P
->Ray
[r
][1+nvar
+exist
+i
]);
2599 value_decrement(M
->p
[1][1+P
->Dimension
], M
->p
[1][1+P
->Dimension
]);
2600 // Matrix_Print(stderr, P_VALUE_FMT, M);
2601 D
= AddConstraints(M
->p
[0], 2, P
, options
->MaxRays
);
2602 // Polyhedron_Print(stderr, P_VALUE_FMT, D);
2603 value_subtract(M
->p
[0][1+P
->Dimension
], M
->p
[0][1+P
->Dimension
],
2604 P
->Ray
[r
][1+nvar
+exist
+i
]);
2605 // Matrix_Print(stderr, P_VALUE_FMT, M);
2606 S
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
2607 // Polyhedron_Print(stderr, P_VALUE_FMT, S);
2610 evalue
*EP
= barvinok_enumerate_e_with_options(D
, exist
, nparam
, options
);
2615 if (value_notone_p(P
->Ray
[r
][1+nvar
+exist
+i
]))
2616 EP
= enumerate_cyclic(P
, exist
, nparam
, EP
, r
, i
, options
->MaxRays
);
2618 M
= Matrix_Alloc(1, nparam
+2);
2619 value_set_si(M
->p
[0][0], 1);
2620 value_set_si(M
->p
[0][1+i
], 1);
2621 enumerate_vd_add_ray(EP
, M
, options
->MaxRays
);
2626 evalue
*E
= barvinok_enumerate_e_with_options(S
, exist
, nparam
, options
);
2628 free_evalue_refs(E
);
2635 evalue
*ER
= enumerate_or(R
, exist
, nparam
, options
);
2637 free_evalue_refs(ER
);
2644 static evalue
* enumerate_vd(Polyhedron
**PA
,
2645 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2647 Polyhedron
*P
= *PA
;
2648 int nvar
= P
->Dimension
- exist
- nparam
;
2649 Param_Polyhedron
*PP
= NULL
;
2650 Polyhedron
*C
= Universe_Polyhedron(nparam
);
2654 PP
= Polyhedron2Param_Domain(PR
,C
, options
->MaxRays
);
2658 Param_Domain
*D
, *last
;
2661 for (nd
= 0, D
=PP
->D
; D
; D
=D
->next
, ++nd
)
2664 Polyhedron
**VD
= new Polyhedron_p
[nd
];
2665 Polyhedron
*TC
= true_context(P
, C
, options
->MaxRays
);
2666 FORALL_REDUCED_DOMAIN(PP
, TC
, nd
, options
, i
, D
, rVD
)
2669 END_FORALL_REDUCED_DOMAIN
2670 Polyhedron_Free(TC
);
2677 /* This doesn't seem to have any effect */
2679 Polyhedron
*CA
= align_context(VD
[0], P
->Dimension
, options
->MaxRays
);
2681 P
= DomainIntersection(P
, CA
, options
->MaxRays
);
2684 Polyhedron_Free(CA
);
2690 Polyhedron_Free(PR
);
2693 if (!EP
&& nd
> 1) {
2695 fprintf(stderr
, "\nER: VD\n");
2696 #endif /* DEBUG_ER */
2697 for (int i
= 0; i
< nd
; ++i
) {
2698 Polyhedron
*CA
= align_context(VD
[i
], P
->Dimension
, options
->MaxRays
);
2699 Polyhedron
*I
= DomainIntersection(P
, CA
, options
->MaxRays
);
2702 EP
= barvinok_enumerate_e_with_options(I
, exist
, nparam
, options
);
2704 evalue
*E
= barvinok_enumerate_e_with_options(I
, exist
, nparam
,
2707 free_evalue_refs(E
);
2711 Polyhedron_Free(CA
);
2715 for (int i
= 0; i
< nd
; ++i
)
2716 Polyhedron_Free(VD
[i
]);
2720 if (!EP
&& nvar
== 0) {
2723 Param_Vertices
*V
, *V2
;
2724 Matrix
* M
= Matrix_Alloc(1, P
->Dimension
+2);
2726 FORALL_PVertex_in_ParamPolyhedron(V
, last
, PP
) {
2728 FORALL_PVertex_in_ParamPolyhedron(V2
, last
, PP
) {
2735 for (int i
= 0; i
< exist
; ++i
) {
2736 value_oppose(f
, V
->Vertex
->p
[i
][nparam
+1]);
2737 Vector_Combine(V
->Vertex
->p
[i
],
2739 M
->p
[0] + 1 + nvar
+ exist
,
2740 V2
->Vertex
->p
[i
][nparam
+1],
2744 for (j
= 0; j
< nparam
; ++j
)
2745 if (value_notzero_p(M
->p
[0][1+nvar
+exist
+j
]))
2749 ConstraintSimplify(M
->p
[0], M
->p
[0],
2750 P
->Dimension
+2, &f
);
2751 value_set_si(M
->p
[0][0], 0);
2752 Polyhedron
*para
= AddConstraints(M
->p
[0], 1, P
,
2755 Polyhedron_Free(para
);
2758 Polyhedron
*pos
, *neg
;
2759 value_set_si(M
->p
[0][0], 1);
2760 value_decrement(M
->p
[0][P
->Dimension
+1],
2761 M
->p
[0][P
->Dimension
+1]);
2762 neg
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
2763 value_set_si(f
, -1);
2764 Vector_Scale(M
->p
[0]+1, M
->p
[0]+1, f
,
2766 value_decrement(M
->p
[0][P
->Dimension
+1],
2767 M
->p
[0][P
->Dimension
+1]);
2768 value_decrement(M
->p
[0][P
->Dimension
+1],
2769 M
->p
[0][P
->Dimension
+1]);
2770 pos
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
2771 if (emptyQ(neg
) && emptyQ(pos
)) {
2772 Polyhedron_Free(para
);
2773 Polyhedron_Free(pos
);
2774 Polyhedron_Free(neg
);
2778 fprintf(stderr
, "\nER: Order\n");
2779 #endif /* DEBUG_ER */
2780 EP
= barvinok_enumerate_e_with_options(para
, exist
, nparam
,
2784 E
= barvinok_enumerate_e_with_options(pos
, exist
, nparam
,
2787 free_evalue_refs(E
);
2791 E
= barvinok_enumerate_e_with_options(neg
, exist
, nparam
,
2794 free_evalue_refs(E
);
2797 Polyhedron_Free(para
);
2798 Polyhedron_Free(pos
);
2799 Polyhedron_Free(neg
);
2804 } END_FORALL_PVertex_in_ParamPolyhedron
;
2807 } END_FORALL_PVertex_in_ParamPolyhedron
;
2810 /* Search for vertex coordinate to split on */
2811 /* First look for one independent of the parameters */
2812 FORALL_PVertex_in_ParamPolyhedron(V
, last
, PP
) {
2813 for (int i
= 0; i
< exist
; ++i
) {
2815 for (j
= 0; j
< nparam
; ++j
)
2816 if (value_notzero_p(V
->Vertex
->p
[i
][j
]))
2820 value_set_si(M
->p
[0][0], 1);
2821 Vector_Set(M
->p
[0]+1, 0, nvar
+exist
);
2822 Vector_Copy(V
->Vertex
->p
[i
],
2823 M
->p
[0] + 1 + nvar
+ exist
, nparam
+1);
2824 value_oppose(M
->p
[0][1+nvar
+i
],
2825 V
->Vertex
->p
[i
][nparam
+1]);
2827 Polyhedron
*pos
, *neg
;
2828 value_set_si(M
->p
[0][0], 1);
2829 value_decrement(M
->p
[0][P
->Dimension
+1],
2830 M
->p
[0][P
->Dimension
+1]);
2831 neg
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
2832 value_set_si(f
, -1);
2833 Vector_Scale(M
->p
[0]+1, M
->p
[0]+1, f
,
2835 value_decrement(M
->p
[0][P
->Dimension
+1],
2836 M
->p
[0][P
->Dimension
+1]);
2837 value_decrement(M
->p
[0][P
->Dimension
+1],
2838 M
->p
[0][P
->Dimension
+1]);
2839 pos
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
2840 if (emptyQ(neg
) || emptyQ(pos
)) {
2841 Polyhedron_Free(pos
);
2842 Polyhedron_Free(neg
);
2845 Polyhedron_Free(pos
);
2846 value_increment(M
->p
[0][P
->Dimension
+1],
2847 M
->p
[0][P
->Dimension
+1]);
2848 pos
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
2850 fprintf(stderr
, "\nER: Vertex\n");
2851 #endif /* DEBUG_ER */
2853 EP
= enumerate_or(pos
, exist
, nparam
, options
);
2858 } END_FORALL_PVertex_in_ParamPolyhedron
;
2862 /* Search for vertex coordinate to split on */
2863 /* Now look for one that depends on the parameters */
2864 FORALL_PVertex_in_ParamPolyhedron(V
, last
, PP
) {
2865 for (int i
= 0; i
< exist
; ++i
) {
2866 value_set_si(M
->p
[0][0], 1);
2867 Vector_Set(M
->p
[0]+1, 0, nvar
+exist
);
2868 Vector_Copy(V
->Vertex
->p
[i
],
2869 M
->p
[0] + 1 + nvar
+ exist
, nparam
+1);
2870 value_oppose(M
->p
[0][1+nvar
+i
],
2871 V
->Vertex
->p
[i
][nparam
+1]);
2873 Polyhedron
*pos
, *neg
;
2874 value_set_si(M
->p
[0][0], 1);
2875 value_decrement(M
->p
[0][P
->Dimension
+1],
2876 M
->p
[0][P
->Dimension
+1]);
2877 neg
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
2878 value_set_si(f
, -1);
2879 Vector_Scale(M
->p
[0]+1, M
->p
[0]+1, f
,
2881 value_decrement(M
->p
[0][P
->Dimension
+1],
2882 M
->p
[0][P
->Dimension
+1]);
2883 value_decrement(M
->p
[0][P
->Dimension
+1],
2884 M
->p
[0][P
->Dimension
+1]);
2885 pos
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
2886 if (emptyQ(neg
) || emptyQ(pos
)) {
2887 Polyhedron_Free(pos
);
2888 Polyhedron_Free(neg
);
2891 Polyhedron_Free(pos
);
2892 value_increment(M
->p
[0][P
->Dimension
+1],
2893 M
->p
[0][P
->Dimension
+1]);
2894 pos
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
2896 fprintf(stderr
, "\nER: ParamVertex\n");
2897 #endif /* DEBUG_ER */
2899 EP
= enumerate_or(pos
, exist
, nparam
, options
);
2904 } END_FORALL_PVertex_in_ParamPolyhedron
;
2912 Polyhedron_Free(CEq
);
2916 Param_Polyhedron_Free(PP
);
2922 evalue
* barvinok_enumerate_pip(Polyhedron
*P
, unsigned exist
, unsigned nparam
,
2926 barvinok_options
*options
= barvinok_options_new_with_defaults();
2927 options
->MaxRays
= MaxRays
;
2928 E
= barvinok_enumerate_pip_with_options(P
, exist
, nparam
, options
);
2929 barvinok_options_free(options
);
2934 evalue
*barvinok_enumerate_pip_with_options(Polyhedron
*P
,
2935 unsigned exist
, unsigned nparam
, struct barvinok_options
*options
)
2940 evalue
*barvinok_enumerate_pip_with_options(Polyhedron
*P
,
2941 unsigned exist
, unsigned nparam
, struct barvinok_options
*options
)
2943 int nvar
= P
->Dimension
- exist
- nparam
;
2944 evalue
*EP
= evalue_zero();
2948 fprintf(stderr
, "\nER: PIP\n");
2949 #endif /* DEBUG_ER */
2951 Polyhedron
*D
= pip_projectout(P
, nvar
, exist
, nparam
);
2952 for (Q
= D
; Q
; Q
= N
) {
2956 exist
= Q
->Dimension
- nvar
- nparam
;
2957 E
= barvinok_enumerate_e_with_options(Q
, exist
, nparam
, options
);
2960 free_evalue_refs(E
);
2969 static bool is_single(Value
*row
, int pos
, int len
)
2971 return First_Non_Zero(row
, pos
) == -1 &&
2972 First_Non_Zero(row
+pos
+1, len
-pos
-1) == -1;
2975 static evalue
* barvinok_enumerate_e_r(Polyhedron
*P
,
2976 unsigned exist
, unsigned nparam
, barvinok_options
*options
);
2979 static int er_level
= 0;
2981 evalue
* barvinok_enumerate_e_with_options(Polyhedron
*P
,
2982 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2984 fprintf(stderr
, "\nER: level %i\n", er_level
);
2986 Polyhedron_PrintConstraints(stderr
, P_VALUE_FMT
, P
);
2987 fprintf(stderr
, "\nE %d\nP %d\n", exist
, nparam
);
2989 P
= DomainConstraintSimplify(Polyhedron_Copy(P
), options
->MaxRays
);
2990 evalue
*EP
= barvinok_enumerate_e_r(P
, exist
, nparam
, options
);
2996 evalue
* barvinok_enumerate_e_with_options(Polyhedron
*P
,
2997 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2999 P
= DomainConstraintSimplify(Polyhedron_Copy(P
), options
->MaxRays
);
3000 evalue
*EP
= barvinok_enumerate_e_r(P
, exist
, nparam
, options
);
3006 evalue
* barvinok_enumerate_e(Polyhedron
*P
, unsigned exist
, unsigned nparam
,
3010 barvinok_options
*options
= barvinok_options_new_with_defaults();
3011 options
->MaxRays
= MaxRays
;
3012 E
= barvinok_enumerate_e_with_options(P
, exist
, nparam
, options
);
3013 barvinok_options_free(options
);
3017 static evalue
* barvinok_enumerate_e_r(Polyhedron
*P
,
3018 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
3021 Polyhedron
*U
= Universe_Polyhedron(nparam
);
3022 evalue
*EP
= barvinok_enumerate_with_options(P
, U
, options
);
3023 //char *param_name[] = {"P", "Q", "R", "S", "T" };
3024 //print_evalue(stdout, EP, param_name);
3029 int nvar
= P
->Dimension
- exist
- nparam
;
3030 int len
= P
->Dimension
+ 2;
3033 POL_ENSURE_FACETS(P
);
3034 POL_ENSURE_VERTICES(P
);
3037 return evalue_zero();
3039 if (nvar
== 0 && nparam
== 0) {
3040 evalue
*EP
= evalue_zero();
3041 barvinok_count_with_options(P
, &EP
->x
.n
, options
);
3042 if (value_pos_p(EP
->x
.n
))
3043 value_set_si(EP
->x
.n
, 1);
3048 for (r
= 0; r
< P
->NbRays
; ++r
)
3049 if (value_zero_p(P
->Ray
[r
][0]) ||
3050 value_zero_p(P
->Ray
[r
][P
->Dimension
+1])) {
3052 for (i
= 0; i
< nvar
; ++i
)
3053 if (value_notzero_p(P
->Ray
[r
][i
+1]))
3057 for (i
= nvar
+ exist
; i
< nvar
+ exist
+ nparam
; ++i
)
3058 if (value_notzero_p(P
->Ray
[r
][i
+1]))
3060 if (i
>= nvar
+ exist
+ nparam
)
3063 if (r
< P
->NbRays
) {
3064 evalue
*EP
= evalue_zero();
3065 value_set_si(EP
->x
.n
, -1);
3070 for (r
= 0; r
< P
->NbEq
; ++r
)
3071 if ((first
= First_Non_Zero(P
->Constraint
[r
]+1+nvar
, exist
)) != -1)
3074 if (First_Non_Zero(P
->Constraint
[r
]+1+nvar
+first
+1,
3075 exist
-first
-1) != -1) {
3076 Polyhedron
*T
= rotate_along(P
, r
, nvar
, exist
, options
->MaxRays
);
3078 fprintf(stderr
, "\nER: Equality\n");
3079 #endif /* DEBUG_ER */
3080 evalue
*EP
= barvinok_enumerate_e_with_options(T
, exist
-1, nparam
,
3086 fprintf(stderr
, "\nER: Fixed\n");
3087 #endif /* DEBUG_ER */
3089 return barvinok_enumerate_e_with_options(P
, exist
-1, nparam
,
3092 Polyhedron
*T
= Polyhedron_Copy(P
);
3093 SwapColumns(T
, nvar
+1, nvar
+1+first
);
3094 evalue
*EP
= barvinok_enumerate_e_with_options(T
, exist
-1, nparam
,
3102 Vector
*row
= Vector_Alloc(len
);
3103 value_set_si(row
->p
[0], 1);
3108 enum constraint
* info
= new constraint
[exist
];
3109 for (int i
= 0; i
< exist
; ++i
) {
3111 for (int l
= P
->NbEq
; l
< P
->NbConstraints
; ++l
) {
3112 if (value_negz_p(P
->Constraint
[l
][nvar
+i
+1]))
3114 bool l_parallel
= is_single(P
->Constraint
[l
]+nvar
+1, i
, exist
);
3115 for (int u
= P
->NbEq
; u
< P
->NbConstraints
; ++u
) {
3116 if (value_posz_p(P
->Constraint
[u
][nvar
+i
+1]))
3118 bool lu_parallel
= l_parallel
||
3119 is_single(P
->Constraint
[u
]+nvar
+1, i
, exist
);
3120 value_oppose(f
, P
->Constraint
[u
][nvar
+i
+1]);
3121 Vector_Combine(P
->Constraint
[l
]+1, P
->Constraint
[u
]+1, row
->p
+1,
3122 f
, P
->Constraint
[l
][nvar
+i
+1], len
-1);
3123 if (!(info
[i
] & INDEPENDENT
)) {
3125 for (j
= 0; j
< exist
; ++j
)
3126 if (j
!= i
&& value_notzero_p(row
->p
[nvar
+j
+1]))
3129 //printf("independent: i: %d, l: %d, u: %d\n", i, l, u);
3130 info
[i
] = (constraint
)(info
[i
] | INDEPENDENT
);
3133 if (info
[i
] & ALL_POS
) {
3134 value_addto(row
->p
[len
-1], row
->p
[len
-1],
3135 P
->Constraint
[l
][nvar
+i
+1]);
3136 value_addto(row
->p
[len
-1], row
->p
[len
-1], f
);
3137 value_multiply(f
, f
, P
->Constraint
[l
][nvar
+i
+1]);
3138 value_subtract(row
->p
[len
-1], row
->p
[len
-1], f
);
3139 value_decrement(row
->p
[len
-1], row
->p
[len
-1]);
3140 ConstraintSimplify(row
->p
, row
->p
, len
, &f
);
3141 value_set_si(f
, -1);
3142 Vector_Scale(row
->p
+1, row
->p
+1, f
, len
-1);
3143 value_decrement(row
->p
[len
-1], row
->p
[len
-1]);
3144 Polyhedron
*T
= AddConstraints(row
->p
, 1, P
, options
->MaxRays
);
3146 //printf("not all_pos: i: %d, l: %d, u: %d\n", i, l, u);
3147 info
[i
] = (constraint
)(info
[i
] ^ ALL_POS
);
3149 //puts("pos remainder");
3150 //Polyhedron_Print(stdout, P_VALUE_FMT, T);
3153 if (!(info
[i
] & ONE_NEG
)) {
3155 negative_test_constraint(P
->Constraint
[l
],
3157 row
->p
, nvar
+i
, len
, &f
);
3158 oppose_constraint(row
->p
, len
, &f
);
3159 Polyhedron
*T
= AddConstraints(row
->p
, 1, P
,
3162 //printf("one_neg i: %d, l: %d, u: %d\n", i, l, u);
3163 info
[i
] = (constraint
)(info
[i
] | ONE_NEG
);
3165 //puts("neg remainder");
3166 //Polyhedron_Print(stdout, P_VALUE_FMT, T);
3168 } else if (!(info
[i
] & ROT_NEG
)) {
3169 if (parallel_constraints(P
->Constraint
[l
],
3171 row
->p
, nvar
, exist
)) {
3172 negative_test_constraint7(P
->Constraint
[l
],
3174 row
->p
, nvar
, exist
,
3176 oppose_constraint(row
->p
, len
, &f
);
3177 Polyhedron
*T
= AddConstraints(row
->p
, 1, P
,
3180 // printf("rot_neg i: %d, l: %d, u: %d\n", i, l, u);
3181 info
[i
] = (constraint
)(info
[i
] | ROT_NEG
);
3184 //puts("neg remainder");
3185 //Polyhedron_Print(stdout, P_VALUE_FMT, T);
3190 if (!(info
[i
] & ALL_POS
) && (info
[i
] & (ONE_NEG
| ROT_NEG
)))
3194 if (info
[i
] & ALL_POS
)
3201 for (int i = 0; i < exist; ++i)
3202 printf("%i: %i\n", i, info[i]);
3204 for (int i
= 0; i
< exist
; ++i
)
3205 if (info
[i
] & ALL_POS
) {
3207 fprintf(stderr
, "\nER: Positive\n");
3208 #endif /* DEBUG_ER */
3210 // Maybe we should chew off some of the fat here
3211 Matrix
*M
= Matrix_Alloc(P
->Dimension
, P
->Dimension
+1);
3212 for (int j
= 0; j
< P
->Dimension
; ++j
)
3213 value_set_si(M
->p
[j
][j
+ (j
>= i
+nvar
)], 1);
3214 Polyhedron
*T
= Polyhedron_Image(P
, M
, options
->MaxRays
);
3216 evalue
*EP
= barvinok_enumerate_e_with_options(T
, exist
-1, nparam
,
3224 for (int i
= 0; i
< exist
; ++i
)
3225 if (info
[i
] & ONE_NEG
) {
3227 fprintf(stderr
, "\nER: Negative\n");
3228 #endif /* DEBUG_ER */
3233 return barvinok_enumerate_e_with_options(P
, exist
-1, nparam
,
3236 Polyhedron
*T
= Polyhedron_Copy(P
);
3237 SwapColumns(T
, nvar
+1, nvar
+1+i
);
3238 evalue
*EP
= barvinok_enumerate_e_with_options(T
, exist
-1, nparam
,
3244 for (int i
= 0; i
< exist
; ++i
)
3245 if (info
[i
] & ROT_NEG
) {
3247 fprintf(stderr
, "\nER: Rotate\n");
3248 #endif /* DEBUG_ER */
3252 Polyhedron
*T
= rotate_along(P
, r
, nvar
, exist
, options
->MaxRays
);
3253 evalue
*EP
= barvinok_enumerate_e_with_options(T
, exist
-1, nparam
,
3258 for (int i
= 0; i
< exist
; ++i
)
3259 if (info
[i
] & INDEPENDENT
) {
3260 Polyhedron
*pos
, *neg
;
3262 /* Find constraint again and split off negative part */
3264 if (SplitOnVar(P
, i
, nvar
, exist
, options
->MaxRays
,
3265 row
, f
, true, &pos
, &neg
)) {
3267 fprintf(stderr
, "\nER: Split\n");
3268 #endif /* DEBUG_ER */
3271 barvinok_enumerate_e_with_options(neg
, exist
-1, nparam
, options
);
3273 barvinok_enumerate_e_with_options(pos
, exist
, nparam
, options
);
3275 free_evalue_refs(E
);
3277 Polyhedron_Free(neg
);
3278 Polyhedron_Free(pos
);
3292 EP
= enumerate_line(P
, exist
, nparam
, options
);
3296 EP
= barvinok_enumerate_pip_with_options(P
, exist
, nparam
, options
);
3300 EP
= enumerate_redundant_ray(P
, exist
, nparam
, options
);
3304 EP
= enumerate_sure(P
, exist
, nparam
, options
);
3308 EP
= enumerate_ray(P
, exist
, nparam
, options
);
3312 EP
= enumerate_sure2(P
, exist
, nparam
, options
);
3316 F
= unfringe(P
, options
->MaxRays
);
3317 if (!PolyhedronIncludes(F
, P
)) {
3319 fprintf(stderr
, "\nER: Fringed\n");
3320 #endif /* DEBUG_ER */
3321 EP
= barvinok_enumerate_e_with_options(F
, exist
, nparam
, options
);
3328 EP
= enumerate_vd(&P
, exist
, nparam
, options
);
3333 EP
= enumerate_sum(P
, exist
, nparam
, options
);
3340 Polyhedron
*pos
, *neg
;
3341 for (i
= 0; i
< exist
; ++i
)
3342 if (SplitOnVar(P
, i
, nvar
, exist
, options
->MaxRays
,
3343 row
, f
, false, &pos
, &neg
))
3349 EP
= enumerate_or(pos
, exist
, nparam
, options
);
3362 * remove equalities that require a "compression" of the parameters
3364 static Polyhedron
*remove_more_equalities(Polyhedron
*P
, unsigned nparam
,
3365 Matrix
**CP
, unsigned MaxRays
)
3368 remove_all_equalities(&P
, NULL
, CP
, NULL
, nparam
, MaxRays
);
3375 static gen_fun
*series(Polyhedron
*P
, unsigned nparam
, barvinok_options
*options
)
3385 assert(!Polyhedron_is_unbounded(P
, nparam
, options
->MaxRays
));
3386 assert(P
->NbBid
== 0);
3387 assert(Polyhedron_has_revlex_positive_rays(P
, nparam
));
3389 P
= remove_more_equalities(P
, nparam
, &CP
, options
->MaxRays
);
3390 assert(P
->NbEq
== 0);
3392 nparam
= CP
->NbColumns
-1;
3397 barvinok_count_with_options(P
, &c
, options
);
3398 gf
= new gen_fun(c
);
3402 red
= gf_base::create(Polyhedron_Project(P
, nparam
),
3403 P
->Dimension
, nparam
, options
);
3404 POL_ENSURE_VERTICES(P
);
3405 red
->start_gf(P
, options
);
3417 gen_fun
* barvinok_series_with_options(Polyhedron
*P
, Polyhedron
* C
,
3418 barvinok_options
*options
)
3421 unsigned nparam
= C
->Dimension
;
3424 CA
= align_context(C
, P
->Dimension
, options
->MaxRays
);
3425 P
= DomainIntersection(P
, CA
, options
->MaxRays
);
3426 Polyhedron_Free(CA
);
3428 gf
= series(P
, nparam
, options
);
3433 gen_fun
* barvinok_series(Polyhedron
*P
, Polyhedron
* C
, unsigned MaxRays
)
3436 barvinok_options
*options
= barvinok_options_new_with_defaults();
3437 options
->MaxRays
= MaxRays
;
3438 gf
= barvinok_series_with_options(P
, C
, options
);
3439 barvinok_options_free(options
);
3443 static Polyhedron
*skew_into_positive_orthant(Polyhedron
*D
, unsigned nparam
,
3449 for (Polyhedron
*P
= D
; P
; P
= P
->next
) {
3450 POL_ENSURE_VERTICES(P
);
3451 assert(!Polyhedron_is_unbounded(P
, nparam
, MaxRays
));
3452 assert(P
->NbBid
== 0);
3453 assert(Polyhedron_has_positive_rays(P
, nparam
));
3455 for (int r
= 0; r
< P
->NbRays
; ++r
) {
3456 if (value_notzero_p(P
->Ray
[r
][P
->Dimension
+1]))
3458 for (int i
= 0; i
< nparam
; ++i
) {
3460 if (value_posz_p(P
->Ray
[r
][i
+1]))
3463 M
= Matrix_Alloc(D
->Dimension
+1, D
->Dimension
+1);
3464 for (int i
= 0; i
< D
->Dimension
+1; ++i
)
3465 value_set_si(M
->p
[i
][i
], 1);
3467 Inner_Product(P
->Ray
[r
]+1, M
->p
[i
], D
->Dimension
+1, &tmp
);
3468 if (value_posz_p(tmp
))
3471 for (j
= P
->Dimension
- nparam
; j
< P
->Dimension
; ++j
)
3472 if (value_pos_p(P
->Ray
[r
][j
+1]))
3474 assert(j
< P
->Dimension
);
3475 value_pdivision(tmp
, P
->Ray
[r
][j
+1], P
->Ray
[r
][i
+1]);
3476 value_subtract(M
->p
[i
][j
], M
->p
[i
][j
], tmp
);
3482 D
= DomainImage(D
, M
, MaxRays
);
3488 gen_fun
* barvinok_enumerate_union_series_with_options(Polyhedron
*D
, Polyhedron
* C
,
3489 barvinok_options
*options
)
3491 Polyhedron
*conv
, *D2
;
3493 gen_fun
*gf
= NULL
, *gf2
;
3494 unsigned nparam
= C
->Dimension
;
3499 CA
= align_context(C
, D
->Dimension
, options
->MaxRays
);
3500 D
= DomainIntersection(D
, CA
, options
->MaxRays
);
3501 Polyhedron_Free(CA
);
3503 D2
= skew_into_positive_orthant(D
, nparam
, options
->MaxRays
);
3504 for (Polyhedron
*P
= D2
; P
; P
= P
->next
) {
3505 assert(P
->Dimension
== D2
->Dimension
);
3508 P_gf
= series(Polyhedron_Copy(P
), nparam
, options
);
3512 gf
->add_union(P_gf
, options
);
3516 /* we actually only need the convex union of the parameter space
3517 * but the reducer classes currently expect a polyhedron in
3518 * the combined space
3520 Polyhedron_Free(gf
->context
);
3521 gf
->context
= DomainConvex(D2
, options
->MaxRays
);
3523 gf2
= gf
->summate(D2
->Dimension
- nparam
, options
);
3532 gen_fun
* barvinok_enumerate_union_series(Polyhedron
*D
, Polyhedron
* C
,
3536 barvinok_options
*options
= barvinok_options_new_with_defaults();
3537 options
->MaxRays
= MaxRays
;
3538 gf
= barvinok_enumerate_union_series_with_options(D
, C
, options
);
3539 barvinok_options_free(options
);
3543 evalue
* barvinok_enumerate_union(Polyhedron
*D
, Polyhedron
* C
, unsigned MaxRays
)
3546 gen_fun
*gf
= barvinok_enumerate_union_series(D
, C
, MaxRays
);