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"
28 #ifndef HAVE_PARAM_POLYHEDRON_SCALE_INTEGER
29 extern "C" void Param_Polyhedron_Scale_Integer(Param_Polyhedron
*PP
, Polyhedron
**P
,
30 Value
*det
, unsigned MaxRays
);
42 using std::ostringstream
;
44 #define ALLOC(t,p) p = (t*)malloc(sizeof(*p))
52 dpoly_n(int d
, ZZ
& degree_0
, ZZ
& degree_1
, int offset
= 0) {
56 zz2value(degree_0
, d0
);
57 zz2value(degree_1
, d1
);
58 coeff
= Matrix_Alloc(d
+1, d
+1+1);
59 value_set_si(coeff
->p
[0][0], 1);
60 value_set_si(coeff
->p
[0][d
+1], 1);
61 for (int i
= 1; i
<= d
; ++i
) {
62 value_multiply(coeff
->p
[i
][0], coeff
->p
[i
-1][0], d0
);
63 Vector_Combine(coeff
->p
[i
-1], coeff
->p
[i
-1]+1, coeff
->p
[i
]+1,
65 value_set_si(coeff
->p
[i
][d
+1], i
);
66 value_multiply(coeff
->p
[i
][d
+1], coeff
->p
[i
][d
+1], coeff
->p
[i
-1][d
+1]);
67 value_decrement(d0
, d0
);
72 void div(dpoly
& d
, Vector
*count
, ZZ
& sign
) {
73 int len
= coeff
->NbRows
;
74 Matrix
* c
= Matrix_Alloc(coeff
->NbRows
, coeff
->NbColumns
);
77 for (int i
= 0; i
< len
; ++i
) {
78 Vector_Copy(coeff
->p
[i
], c
->p
[i
], len
+1);
79 for (int j
= 1; j
<= i
; ++j
) {
80 zz2value(d
.coeff
[j
], tmp
);
81 value_multiply(tmp
, tmp
, c
->p
[i
][len
]);
82 value_oppose(tmp
, tmp
);
83 Vector_Combine(c
->p
[i
], c
->p
[i
-j
], c
->p
[i
],
84 c
->p
[i
-j
][len
], tmp
, len
);
85 value_multiply(c
->p
[i
][len
], c
->p
[i
][len
], c
->p
[i
-j
][len
]);
87 zz2value(d
.coeff
[0], tmp
);
88 value_multiply(c
->p
[i
][len
], c
->p
[i
][len
], tmp
);
91 value_set_si(tmp
, -1);
92 Vector_Scale(c
->p
[len
-1], count
->p
, tmp
, len
);
93 value_assign(count
->p
[len
], c
->p
[len
-1][len
]);
95 Vector_Copy(c
->p
[len
-1], count
->p
, len
+1);
96 Vector_Normalize(count
->p
, len
+1);
102 const int MAX_TRY
=10;
104 * Searches for a vector that is not orthogonal to any
105 * of the rays in rays.
107 static void nonorthog(mat_ZZ
& rays
, vec_ZZ
& lambda
)
109 int dim
= rays
.NumCols();
111 lambda
.SetLength(dim
);
115 for (int i
= 2; !found
&& i
<= 50*dim
; i
+=4) {
116 for (int j
= 0; j
< MAX_TRY
; ++j
) {
117 for (int k
= 0; k
< dim
; ++k
) {
118 int r
= random_int(i
)+2;
119 int v
= (2*(r
%2)-1) * (r
>> 1);
123 for (; k
< rays
.NumRows(); ++k
)
124 if (lambda
* rays
[k
] == 0)
126 if (k
== rays
.NumRows()) {
135 static void add_rays(mat_ZZ
& rays
, Polyhedron
*i
, int *r
, int nvar
= -1,
138 unsigned dim
= i
->Dimension
;
141 for (int k
= 0; k
< i
->NbRays
; ++k
) {
142 if (!value_zero_p(i
->Ray
[k
][dim
+1]))
144 if (!all
&& nvar
!= dim
&& First_Non_Zero(i
->Ray
[k
]+1, nvar
) == -1)
146 values2zz(i
->Ray
[k
]+1, rays
[(*r
)++], nvar
);
150 static void mask_r(Matrix
*f
, int nr
, Vector
*lcm
, int p
, Vector
*val
, evalue
*ev
)
152 unsigned nparam
= lcm
->Size
;
155 Vector
* prod
= Vector_Alloc(f
->NbRows
);
156 Matrix_Vector_Product(f
, val
->p
, prod
->p
);
158 for (int i
= 0; i
< nr
; ++i
) {
159 value_modulus(prod
->p
[i
], prod
->p
[i
], f
->p
[i
][nparam
+1]);
160 isint
&= value_zero_p(prod
->p
[i
]);
162 value_set_si(ev
->d
, 1);
164 value_set_si(ev
->x
.n
, isint
);
171 if (value_one_p(lcm
->p
[p
]))
172 mask_r(f
, nr
, lcm
, p
+1, val
, ev
);
174 value_assign(tmp
, lcm
->p
[p
]);
175 value_set_si(ev
->d
, 0);
176 ev
->x
.p
= new_enode(periodic
, VALUE_TO_INT(tmp
), p
+1);
178 value_decrement(tmp
, tmp
);
179 value_assign(val
->p
[p
], tmp
);
180 mask_r(f
, nr
, lcm
, p
+1, val
, &ev
->x
.p
->arr
[VALUE_TO_INT(tmp
)]);
181 } while (value_pos_p(tmp
));
186 static void mask_fractional(Matrix
*f
, evalue
*factor
)
188 int nr
= f
->NbRows
, nc
= f
->NbColumns
;
191 for (n
= 0; n
< nr
&& value_notzero_p(f
->p
[n
][nc
-1]); ++n
)
192 if (value_notone_p(f
->p
[n
][nc
-1]) &&
193 value_notmone_p(f
->p
[n
][nc
-1]))
207 value_set_si(EV
.x
.n
, 1);
209 for (n
= 0; n
< nr
; ++n
) {
210 value_assign(m
, f
->p
[n
][nc
-1]);
211 if (value_one_p(m
) || value_mone_p(m
))
214 int j
= normal_mod(f
->p
[n
], nc
-1, &m
);
216 free_evalue_refs(factor
);
217 value_init(factor
->d
);
218 evalue_set_si(factor
, 0, 1);
222 values2zz(f
->p
[n
], row
, nc
-1);
225 if (j
< (nc
-1)-1 && row
[j
] > g
/2) {
226 for (int k
= j
; k
< (nc
-1); ++k
)
232 value_set_si(EP
.d
, 0);
233 EP
.x
.p
= new_enode(relation
, 2, 0);
234 value_clear(EP
.x
.p
->arr
[1].d
);
235 EP
.x
.p
->arr
[1] = *factor
;
236 evalue
*ev
= &EP
.x
.p
->arr
[0];
237 value_set_si(ev
->d
, 0);
238 ev
->x
.p
= new_enode(fractional
, 3, -1);
239 evalue_set_si(&ev
->x
.p
->arr
[1], 0, 1);
240 evalue_set_si(&ev
->x
.p
->arr
[2], 1, 1);
241 evalue
*E
= multi_monom(row
);
242 value_assign(EV
.d
, m
);
244 value_clear(ev
->x
.p
->arr
[0].d
);
245 ev
->x
.p
->arr
[0] = *E
;
251 free_evalue_refs(&EV
);
257 static void mask_table(Matrix
*f
, evalue
*factor
)
259 int nr
= f
->NbRows
, nc
= f
->NbColumns
;
262 for (n
= 0; n
< nr
&& value_notzero_p(f
->p
[n
][nc
-1]); ++n
)
263 if (value_notone_p(f
->p
[n
][nc
-1]) &&
264 value_notmone_p(f
->p
[n
][nc
-1]))
272 unsigned np
= nc
- 2;
273 Vector
*lcm
= Vector_Alloc(np
);
274 Vector
*val
= Vector_Alloc(nc
);
275 Vector_Set(val
->p
, 0, nc
);
276 value_set_si(val
->p
[np
], 1);
277 Vector_Set(lcm
->p
, 1, np
);
278 for (n
= 0; n
< nr
; ++n
) {
279 if (value_one_p(f
->p
[n
][nc
-1]) ||
280 value_mone_p(f
->p
[n
][nc
-1]))
282 for (int j
= 0; j
< np
; ++j
)
283 if (value_notzero_p(f
->p
[n
][j
])) {
284 Gcd(f
->p
[n
][j
], f
->p
[n
][nc
-1], &tmp
);
285 value_division(tmp
, f
->p
[n
][nc
-1], tmp
);
286 value_lcm(tmp
, lcm
->p
[j
], &lcm
->p
[j
]);
291 mask_r(f
, nr
, lcm
, 0, val
, &EP
);
296 free_evalue_refs(&EP
);
299 static void mask(Matrix
*f
, evalue
*factor
, barvinok_options
*options
)
301 if (options
->lookup_table
)
302 mask_table(f
, factor
);
304 mask_fractional(f
, factor
);
307 /* This structure encodes the power of the term in a rational generating function.
309 * Either E == NULL or constant = 0
310 * If E != NULL, then the power is E
311 * If E == NULL, then the power is coeff * param[pos] + constant
320 /* Returns the power of (t+1) in the term of a rational generating function,
321 * i.e., the scalar product of the actual lattice point and lambda.
322 * The lattice point is the unique lattice point in the fundamental parallelepiped
323 * of the unimodual cone i shifted to the parametric vertex V.
325 * PD is the parameter domain, which, if != NULL, may be used to simply the
326 * resulting expression.
328 * The result is returned in term.
330 void lattice_point(Param_Vertices
* V
, const mat_ZZ
& rays
, vec_ZZ
& lambda
,
331 term_info
* term
, Polyhedron
*PD
, barvinok_options
*options
)
333 unsigned nparam
= V
->Vertex
->NbColumns
- 2;
334 unsigned dim
= rays
.NumCols();
336 vertex
.SetDims(V
->Vertex
->NbRows
, nparam
+1);
340 value_set_si(lcm
, 1);
341 for (int j
= 0; j
< V
->Vertex
->NbRows
; ++j
) {
342 value_lcm(lcm
, V
->Vertex
->p
[j
][nparam
+1], &lcm
);
344 if (value_notone_p(lcm
)) {
345 Matrix
* mv
= Matrix_Alloc(dim
, nparam
+1);
346 for (int j
= 0 ; j
< dim
; ++j
) {
347 value_division(tmp
, lcm
, V
->Vertex
->p
[j
][nparam
+1]);
348 Vector_Scale(V
->Vertex
->p
[j
], mv
->p
[j
], tmp
, nparam
+1);
351 term
->E
= lattice_point(rays
, lambda
, mv
, lcm
, PD
, options
);
359 for (int i
= 0; i
< V
->Vertex
->NbRows
; ++i
) {
360 assert(value_one_p(V
->Vertex
->p
[i
][nparam
+1])); // for now
361 values2zz(V
->Vertex
->p
[i
], vertex
[i
], nparam
+1);
365 num
= lambda
* vertex
;
369 for (int j
= 0; j
< nparam
; ++j
)
375 term
->E
= multi_monom(num
);
379 term
->constant
= num
[nparam
];
382 term
->coeff
= num
[p
];
390 struct counter
: public np_base
{
400 counter(unsigned dim
) : np_base(dim
) {
405 virtual void init(Polyhedron
*P
) {
406 randomvector(P
, lambda
, dim
);
409 virtual void reset() {
410 mpq_set_si(count
, 0, 0);
417 virtual void handle(const mat_ZZ
& rays
, Value
*vertex
, const QQ
& c
,
418 unsigned long det
, int *closed
, barvinok_options
*options
);
419 virtual void get_count(Value
*result
) {
420 assert(value_one_p(&count
[0]._mp_den
));
421 value_assign(*result
, &count
[0]._mp_num
);
425 void counter::handle(const mat_ZZ
& rays
, Value
*V
, const QQ
& c
, unsigned long det
,
426 int *closed
, barvinok_options
*options
)
428 for (int k
= 0; k
< dim
; ++k
) {
429 if (lambda
* rays
[k
] == 0)
434 assert(c
.n
== 1 || c
.n
== -1);
437 lattice_point(V
, rays
, vertex
, det
, closed
);
438 num
= vertex
* lambda
;
441 normalize(sign
, offset
, den
);
444 dpoly
d(dim
, num
[0]);
445 for (int k
= 1; k
< num
.length(); ++k
) {
447 dpoly
term(dim
, num
[k
]);
450 dpoly
n(dim
, den
[0], 1);
451 for (int k
= 1; k
< dim
; ++k
) {
452 dpoly
fact(dim
, den
[k
], 1);
455 d
.div(n
, count
, sign
);
458 struct bfe_term
: public bfc_term_base
{
459 vector
<evalue
*> factors
;
461 bfe_term(int len
) : bfc_term_base(len
) {
465 for (int i
= 0; i
< factors
.size(); ++i
) {
468 free_evalue_refs(factors
[i
]);
474 static void print_int_vector(int *v
, int len
, char *name
)
476 cerr
<< name
<< endl
;
477 for (int j
= 0; j
< len
; ++j
) {
483 static void print_bfc_terms(mat_ZZ
& factors
, bfc_vec
& v
)
486 cerr
<< "factors" << endl
;
487 cerr
<< factors
<< endl
;
488 for (int i
= 0; i
< v
.size(); ++i
) {
489 cerr
<< "term: " << i
<< endl
;
490 print_int_vector(v
[i
]->powers
, factors
.NumRows(), "powers");
491 cerr
<< "terms" << endl
;
492 cerr
<< v
[i
]->terms
<< endl
;
493 bfc_term
* bfct
= static_cast<bfc_term
*>(v
[i
]);
494 cerr
<< bfct
->c
<< endl
;
498 static void print_bfe_terms(mat_ZZ
& factors
, bfc_vec
& v
)
501 cerr
<< "factors" << endl
;
502 cerr
<< factors
<< endl
;
503 for (int i
= 0; i
< v
.size(); ++i
) {
504 cerr
<< "term: " << i
<< endl
;
505 print_int_vector(v
[i
]->powers
, factors
.NumRows(), "powers");
506 cerr
<< "terms" << endl
;
507 cerr
<< v
[i
]->terms
<< endl
;
508 bfe_term
* bfet
= static_cast<bfe_term
*>(v
[i
]);
509 for (int j
= 0; j
< v
[i
]->terms
.NumRows(); ++j
) {
510 char * test
[] = {"a", "b"};
511 print_evalue(stderr
, bfet
->factors
[j
], test
);
512 fprintf(stderr
, "\n");
517 struct bfcounter
: public bfcounter_base
{
520 bfcounter(unsigned dim
) : bfcounter_base(dim
) {
527 virtual void base(mat_ZZ
& factors
, bfc_vec
& v
);
528 virtual void get_count(Value
*result
) {
529 assert(value_one_p(&count
[0]._mp_den
));
530 value_assign(*result
, &count
[0]._mp_num
);
534 void bfcounter::base(mat_ZZ
& factors
, bfc_vec
& v
)
536 unsigned nf
= factors
.NumRows();
538 for (int i
= 0; i
< v
.size(); ++i
) {
539 bfc_term
* bfct
= static_cast<bfc_term
*>(v
[i
]);
541 // factor is always positive, so we always
543 for (int k
= 0; k
< nf
; ++k
)
544 total_power
+= v
[i
]->powers
[k
];
547 for (j
= 0; j
< nf
; ++j
)
548 if (v
[i
]->powers
[j
] > 0)
551 dpoly
D(total_power
, factors
[j
][0], 1);
552 for (int k
= 1; k
< v
[i
]->powers
[j
]; ++k
) {
553 dpoly
fact(total_power
, factors
[j
][0], 1);
557 for (int k
= 0; k
< v
[i
]->powers
[j
]; ++k
) {
558 dpoly
fact(total_power
, factors
[j
][0], 1);
562 for (int k
= 0; k
< v
[i
]->terms
.NumRows(); ++k
) {
563 dpoly
n(total_power
, v
[i
]->terms
[k
][0]);
564 mpq_set_si(tcount
, 0, 1);
565 n
.div(D
, tcount
, one
);
567 bfct
->c
[k
].n
= -bfct
->c
[k
].n
;
568 zz2value(bfct
->c
[k
].n
, tn
);
569 zz2value(bfct
->c
[k
].d
, td
);
571 mpz_mul(mpq_numref(tcount
), mpq_numref(tcount
), tn
);
572 mpz_mul(mpq_denref(tcount
), mpq_denref(tcount
), td
);
573 mpq_canonicalize(tcount
);
574 mpq_add(count
, count
, tcount
);
581 /* Check whether the polyhedron is unbounded and if so,
582 * check whether it has any (and therefore an infinite number of)
584 * If one of the vertices is integer, then we are done.
585 * Otherwise, transform the polyhedron such that one of the rays
586 * is the first unit vector and cut it off at a height that ensures
587 * that if the whole polyhedron has any points, then the remaining part
588 * has integer points. In particular we add the largest coefficient
589 * of a ray to the highest vertex (rounded up).
591 static bool Polyhedron_is_infinite(Polyhedron
*P
, Value
* result
,
592 barvinok_options
*options
)
604 for (; r
< P
->NbRays
; ++r
)
605 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1]))
607 if (P
->NbBid
== 0 && r
== P
->NbRays
)
610 if (options
->count_sample_infinite
) {
613 sample
= Polyhedron_Sample(P
, options
);
615 value_set_si(*result
, 0);
617 value_set_si(*result
, -1);
623 for (int i
= 0; i
< P
->NbRays
; ++i
)
624 if (value_one_p(P
->Ray
[i
][1+P
->Dimension
])) {
625 value_set_si(*result
, -1);
630 v
= Vector_Alloc(P
->Dimension
+1);
631 Vector_Gcd(P
->Ray
[r
]+1, P
->Dimension
, &g
);
632 Vector_AntiScale(P
->Ray
[r
]+1, v
->p
, g
, P
->Dimension
+1);
633 M
= unimodular_complete(v
);
634 value_set_si(M
->p
[P
->Dimension
][P
->Dimension
], 1);
637 P
= Polyhedron_Preimage(P
, M2
, 0);
646 value_set_si(size
, 0);
648 for (int i
= 0; i
< P
->NbBid
; ++i
) {
649 value_absolute(tmp
, P
->Ray
[i
][1]);
650 if (value_gt(tmp
, size
))
651 value_assign(size
, tmp
);
653 for (int i
= P
->NbBid
; i
< P
->NbRays
; ++i
) {
654 if (value_zero_p(P
->Ray
[i
][P
->Dimension
+1])) {
655 if (value_gt(P
->Ray
[i
][1], size
))
656 value_assign(size
, P
->Ray
[i
][1]);
659 mpz_cdiv_q(tmp
, P
->Ray
[i
][1], P
->Ray
[i
][P
->Dimension
+1]);
660 if (first
|| value_gt(tmp
, offset
)) {
661 value_assign(offset
, tmp
);
665 value_addto(offset
, offset
, size
);
669 v
= Vector_Alloc(P
->Dimension
+2);
670 value_set_si(v
->p
[0], 1);
671 value_set_si(v
->p
[1], -1);
672 value_assign(v
->p
[1+P
->Dimension
], offset
);
673 R
= AddConstraints(v
->p
, 1, P
, options
->MaxRays
);
681 barvinok_count_with_options(P
, &c
, options
);
684 value_set_si(*result
, 0);
686 value_set_si(*result
, -1);
692 typedef Polyhedron
* Polyhedron_p
;
694 static void barvinok_count_f(Polyhedron
*P
, Value
* result
,
695 barvinok_options
*options
);
697 void barvinok_count_with_options(Polyhedron
*P
, Value
* result
,
698 struct barvinok_options
*options
)
703 bool infinite
= false;
707 "barvinok_count: input is a union; only first polyhedron is counted\n");
710 value_set_si(*result
, 0);
716 P
= remove_equalities(P
);
717 P
= DomainConstraintSimplify(P
, options
->MaxRays
);
721 } while (!emptyQ(P
) && P
->NbEq
!= 0);
724 value_set_si(*result
, 0);
729 if (Polyhedron_is_infinite(P
, result
, options
)) {
734 if (P
->Dimension
== 0) {
735 /* Test whether the constraints are satisfied */
736 POL_ENSURE_VERTICES(P
);
737 value_set_si(*result
, !emptyQ(P
));
742 Q
= Polyhedron_Factor(P
, 0, NULL
, options
->MaxRays
);
750 barvinok_count_f(P
, result
, options
);
751 if (value_neg_p(*result
))
753 if (Q
&& P
->next
&& value_notzero_p(*result
)) {
757 for (Q
= P
->next
; Q
; Q
= Q
->next
) {
758 barvinok_count_f(Q
, &factor
, options
);
759 if (value_neg_p(factor
)) {
762 } else if (Q
->next
&& value_zero_p(factor
)) {
763 value_set_si(*result
, 0);
766 value_multiply(*result
, *result
, factor
);
775 value_set_si(*result
, -1);
778 void barvinok_count(Polyhedron
*P
, Value
* result
, unsigned NbMaxCons
)
780 barvinok_options
*options
= barvinok_options_new_with_defaults();
781 options
->MaxRays
= NbMaxCons
;
782 barvinok_count_with_options(P
, result
, options
);
783 barvinok_options_free(options
);
786 static void barvinok_count_f(Polyhedron
*P
, Value
* result
,
787 barvinok_options
*options
)
790 value_set_si(*result
, 0);
794 if (P
->Dimension
== 1)
795 return Line_Length(P
, result
);
797 int c
= P
->NbConstraints
;
798 POL_ENSURE_FACETS(P
);
799 if (c
!= P
->NbConstraints
|| P
->NbEq
!= 0)
800 return barvinok_count_with_options(P
, result
, options
);
802 POL_ENSURE_VERTICES(P
);
804 if (Polyhedron_is_infinite(P
, result
, options
))
808 if (options
->incremental_specialization
== 2)
809 cnt
= new bfcounter(P
->Dimension
);
810 else if (options
->incremental_specialization
== 1)
811 cnt
= new icounter(P
->Dimension
);
813 cnt
= new counter(P
->Dimension
);
814 cnt
->start(P
, options
);
816 cnt
->get_count(result
);
820 static void uni_polynom(int param
, Vector
*c
, evalue
*EP
)
822 unsigned dim
= c
->Size
-2;
824 value_set_si(EP
->d
,0);
825 EP
->x
.p
= new_enode(polynomial
, dim
+1, param
+1);
826 for (int j
= 0; j
<= dim
; ++j
)
827 evalue_set(&EP
->x
.p
->arr
[j
], c
->p
[j
], c
->p
[dim
+1]);
830 static void multi_polynom(Vector
*c
, evalue
* X
, evalue
*EP
)
832 unsigned dim
= c
->Size
-2;
836 evalue_set(&EC
, c
->p
[dim
], c
->p
[dim
+1]);
839 evalue_set(EP
, c
->p
[dim
], c
->p
[dim
+1]);
841 for (int i
= dim
-1; i
>= 0; --i
) {
843 value_assign(EC
.x
.n
, c
->p
[i
]);
846 free_evalue_refs(&EC
);
849 Polyhedron
*unfringe (Polyhedron
*P
, unsigned MaxRays
)
851 int len
= P
->Dimension
+2;
852 Polyhedron
*T
, *R
= P
;
855 Vector
*row
= Vector_Alloc(len
);
856 value_set_si(row
->p
[0], 1);
858 R
= DomainConstraintSimplify(Polyhedron_Copy(P
), MaxRays
);
860 Matrix
*M
= Matrix_Alloc(2, len
-1);
861 value_set_si(M
->p
[1][len
-2], 1);
862 for (int v
= 0; v
< P
->Dimension
; ++v
) {
863 value_set_si(M
->p
[0][v
], 1);
864 Polyhedron
*I
= Polyhedron_Image(R
, M
, 2+1);
865 value_set_si(M
->p
[0][v
], 0);
866 for (int r
= 0; r
< I
->NbConstraints
; ++r
) {
867 if (value_zero_p(I
->Constraint
[r
][0]))
869 if (value_zero_p(I
->Constraint
[r
][1]))
871 if (value_one_p(I
->Constraint
[r
][1]))
873 if (value_mone_p(I
->Constraint
[r
][1]))
875 value_absolute(g
, I
->Constraint
[r
][1]);
876 Vector_Set(row
->p
+1, 0, len
-2);
877 value_division(row
->p
[1+v
], I
->Constraint
[r
][1], g
);
878 mpz_fdiv_q(row
->p
[len
-1], I
->Constraint
[r
][2], g
);
880 R
= AddConstraints(row
->p
, 1, R
, MaxRays
);
892 /* Check whether all rays point in the positive directions
895 static bool Polyhedron_has_positive_rays(Polyhedron
*P
, unsigned nparam
)
898 for (r
= 0; r
< P
->NbRays
; ++r
)
899 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1])) {
901 for (i
= P
->Dimension
- nparam
; i
< P
->Dimension
; ++i
)
902 if (value_neg_p(P
->Ray
[r
][i
+1]))
908 typedef evalue
* evalue_p
;
910 struct enumerator_base
{
914 vertex_decomposer
*vpd
;
916 enumerator_base(unsigned dim
, vertex_decomposer
*vpd
)
921 vE
= new evalue_p
[vpd
->nbV
];
922 for (int j
= 0; j
< vpd
->nbV
; ++j
)
926 evalue_set_si(&mone
, -1, 1);
929 void decompose_at(Param_Vertices
*V
, int _i
, barvinok_options
*options
) {
933 value_init(vE
[_i
]->d
);
934 evalue_set_si(vE
[_i
], 0, 1);
936 vpd
->decompose_at_vertex(V
, _i
, options
);
939 virtual ~enumerator_base() {
940 for (int j
= 0; j
< vpd
->nbV
; ++j
)
942 free_evalue_refs(vE
[j
]);
947 free_evalue_refs(&mone
);
950 static enumerator_base
*create(Polyhedron
*P
, unsigned dim
, unsigned nbV
,
951 barvinok_options
*options
);
954 struct enumerator
: public signed_cone_consumer
, public vertex_decomposer
,
955 public enumerator_base
{
963 enumerator(Polyhedron
*P
, unsigned dim
, unsigned nbV
) :
964 vertex_decomposer(P
, nbV
, *this), enumerator_base(dim
, this) {
967 randomvector(P
, lambda
, dim
);
969 c
= Vector_Alloc(dim
+2);
979 virtual void handle(const signed_cone
& sc
, barvinok_options
*options
);
982 void enumerator::handle(const signed_cone
& sc
, barvinok_options
*options
)
987 assert(sc
.rays
.NumRows() == dim
);
988 for (int k
= 0; k
< dim
; ++k
) {
989 if (lambda
* sc
.rays
[k
] == 0)
995 lattice_point(V
, sc
.rays
, lambda
, &num
, 0, options
);
996 den
= sc
.rays
* lambda
;
997 normalize(sign
, num
.constant
, den
);
999 dpoly
n(dim
, den
[0], 1);
1000 for (int k
= 1; k
< dim
; ++k
) {
1001 dpoly
fact(dim
, den
[k
], 1);
1004 if (num
.E
!= NULL
) {
1005 ZZ
one(INIT_VAL
, 1);
1006 dpoly_n
d(dim
, num
.constant
, one
);
1009 multi_polynom(c
, num
.E
, &EV
);
1010 eadd(&EV
, vE
[vert
]);
1011 free_evalue_refs(&EV
);
1012 free_evalue_refs(num
.E
);
1014 } else if (num
.pos
!= -1) {
1015 dpoly_n
d(dim
, num
.constant
, num
.coeff
);
1018 uni_polynom(num
.pos
, c
, &EV
);
1019 eadd(&EV
, vE
[vert
]);
1020 free_evalue_refs(&EV
);
1022 mpq_set_si(count
, 0, 1);
1023 dpoly
d(dim
, num
.constant
);
1024 d
.div(n
, count
, sign
);
1027 evalue_set(&EV
, &count
[0]._mp_num
, &count
[0]._mp_den
);
1028 eadd(&EV
, vE
[vert
]);
1029 free_evalue_refs(&EV
);
1033 struct ienumerator_base
: enumerator_base
{
1036 ienumerator_base(unsigned dim
, vertex_decomposer
*vpd
) :
1037 enumerator_base(dim
,vpd
) {
1038 E_vertex
= new evalue_p
[dim
];
1041 virtual ~ienumerator_base() {
1045 evalue
*E_num(int i
, int d
) {
1046 return E_vertex
[i
+ (dim
-d
)];
1055 cumulator(evalue
*factor
, evalue
*v
, dpoly_r
*r
) :
1056 factor(factor
), v(v
), r(r
) {}
1058 void cumulate(barvinok_options
*options
);
1060 virtual void add_term(const vector
<int>& powers
, evalue
*f2
) = 0;
1061 virtual ~cumulator() {}
1064 void cumulator::cumulate(barvinok_options
*options
)
1066 evalue cum
; // factor * 1 * E_num[0]/1 * (E_num[0]-1)/2 *...
1068 evalue t
; // E_num[0] - (m-1)
1072 if (options
->lookup_table
) {
1074 evalue_set_si(&mone
, -1, 1);
1078 evalue_copy(&cum
, factor
);
1081 value_set_si(f
.d
, 1);
1082 value_set_si(f
.x
.n
, 1);
1086 if (!options
->lookup_table
) {
1087 for (cst
= &t
; value_zero_p(cst
->d
); ) {
1088 if (cst
->x
.p
->type
== fractional
)
1089 cst
= &cst
->x
.p
->arr
[1];
1091 cst
= &cst
->x
.p
->arr
[0];
1095 for (int m
= 0; m
< r
->len
; ++m
) {
1098 value_set_si(f
.d
, m
);
1100 if (!options
->lookup_table
)
1101 value_subtract(cst
->x
.n
, cst
->x
.n
, cst
->d
);
1107 dpoly_r_term_list
& current
= r
->c
[r
->len
-1-m
];
1108 dpoly_r_term_list::iterator j
;
1109 for (j
= current
.begin(); j
!= current
.end(); ++j
) {
1110 if ((*j
)->coeff
== 0)
1112 evalue
*f2
= new evalue
;
1114 value_init(f2
->x
.n
);
1115 zz2value((*j
)->coeff
, f2
->x
.n
);
1116 zz2value(r
->denom
, f2
->d
);
1119 add_term((*j
)->powers
, f2
);
1122 free_evalue_refs(&f
);
1123 free_evalue_refs(&t
);
1124 free_evalue_refs(&cum
);
1125 if (options
->lookup_table
)
1126 free_evalue_refs(&mone
);
1129 struct E_poly_term
{
1134 struct ie_cum
: public cumulator
{
1135 vector
<E_poly_term
*> terms
;
1137 ie_cum(evalue
*factor
, evalue
*v
, dpoly_r
*r
) : cumulator(factor
, v
, r
) {}
1139 virtual void add_term(const vector
<int>& powers
, evalue
*f2
);
1142 void ie_cum::add_term(const vector
<int>& powers
, evalue
*f2
)
1145 for (k
= 0; k
< terms
.size(); ++k
) {
1146 if (terms
[k
]->powers
== powers
) {
1147 eadd(f2
, terms
[k
]->E
);
1148 free_evalue_refs(f2
);
1153 if (k
>= terms
.size()) {
1154 E_poly_term
*ET
= new E_poly_term
;
1155 ET
->powers
= powers
;
1157 terms
.push_back(ET
);
1161 struct ienumerator
: public signed_cone_consumer
, public vertex_decomposer
,
1162 public ienumerator_base
{
1168 ienumerator(Polyhedron
*P
, unsigned dim
, unsigned nbV
) :
1169 vertex_decomposer(P
, nbV
, *this), ienumerator_base(dim
, this) {
1170 vertex
.SetDims(1, dim
);
1172 den
.SetDims(dim
, dim
);
1180 virtual void handle(const signed_cone
& sc
, barvinok_options
*options
);
1181 void reduce(evalue
*factor
, const mat_ZZ
& num
, const mat_ZZ
& den_f
,
1182 barvinok_options
*options
);
1185 void ienumerator::reduce(evalue
*factor
, const mat_ZZ
& num
, const mat_ZZ
& den_f
,
1186 barvinok_options
*options
)
1188 unsigned len
= den_f
.NumRows(); // number of factors in den
1189 unsigned dim
= num
.NumCols();
1190 assert(num
.NumRows() == 1);
1193 eadd(factor
, vE
[vert
]);
1202 split_one(num
, num_s
, num_p
, den_f
, den_s
, den_r
);
1205 den_p
.SetLength(len
);
1209 normalize(one
, num_s
, num_p
, den_s
, den_p
, den_r
);
1211 emul(&mone
, factor
);
1215 for (int k
= 0; k
< len
; ++k
) {
1218 else if (den_s
[k
] == 0)
1221 if (no_param
== 0) {
1222 reduce(factor
, num_p
, den_r
, options
);
1226 pden
.SetDims(only_param
, dim
-1);
1228 for (k
= 0, l
= 0; k
< len
; ++k
)
1230 pden
[l
++] = den_r
[k
];
1232 for (k
= 0; k
< len
; ++k
)
1236 dpoly
n(no_param
, num_s
[0]);
1237 dpoly
D(no_param
, den_s
[k
], 1);
1238 for ( ; ++k
< len
; )
1239 if (den_p
[k
] == 0) {
1240 dpoly
fact(no_param
, den_s
[k
], 1);
1245 // if no_param + only_param == len then all powers
1246 // below will be all zero
1247 if (no_param
+ only_param
== len
) {
1248 if (E_num(0, dim
) != 0)
1249 r
= new dpoly_r(n
, len
);
1251 mpq_set_si(tcount
, 0, 1);
1253 n
.div(D
, tcount
, one
);
1255 if (value_notzero_p(mpq_numref(tcount
))) {
1259 value_assign(f
.x
.n
, mpq_numref(tcount
));
1260 value_assign(f
.d
, mpq_denref(tcount
));
1262 reduce(factor
, num_p
, pden
, options
);
1263 free_evalue_refs(&f
);
1268 for (k
= 0; k
< len
; ++k
) {
1269 if (den_s
[k
] == 0 || den_p
[k
] == 0)
1272 dpoly
pd(no_param
-1, den_s
[k
], 1);
1275 for (l
= 0; l
< k
; ++l
)
1276 if (den_r
[l
] == den_r
[k
])
1280 r
= new dpoly_r(n
, pd
, l
, len
);
1282 dpoly_r
*nr
= new dpoly_r(r
, pd
, l
, len
);
1288 dpoly_r
*rc
= r
->div(D
);
1291 if (E_num(0, dim
) == 0) {
1292 int common
= pden
.NumRows();
1293 dpoly_r_term_list
& final
= r
->c
[r
->len
-1];
1299 zz2value(r
->denom
, f
.d
);
1300 dpoly_r_term_list::iterator j
;
1301 for (j
= final
.begin(); j
!= final
.end(); ++j
) {
1302 if ((*j
)->coeff
== 0)
1305 for (int k
= 0; k
< r
->dim
; ++k
) {
1306 int n
= (*j
)->powers
[k
];
1309 pden
.SetDims(rows
+n
, pden
.NumCols());
1310 for (int l
= 0; l
< n
; ++l
)
1311 pden
[rows
+l
] = den_r
[k
];
1315 evalue_copy(&t
, factor
);
1316 zz2value((*j
)->coeff
, f
.x
.n
);
1318 reduce(&t
, num_p
, pden
, options
);
1319 free_evalue_refs(&t
);
1321 free_evalue_refs(&f
);
1323 ie_cum
cum(factor
, E_num(0, dim
), r
);
1324 cum
.cumulate(options
);
1326 int common
= pden
.NumRows();
1328 for (int j
= 0; j
< cum
.terms
.size(); ++j
) {
1330 pden
.SetDims(rows
, pden
.NumCols());
1331 for (int k
= 0; k
< r
->dim
; ++k
) {
1332 int n
= cum
.terms
[j
]->powers
[k
];
1335 pden
.SetDims(rows
+n
, pden
.NumCols());
1336 for (int l
= 0; l
< n
; ++l
)
1337 pden
[rows
+l
] = den_r
[k
];
1340 reduce(cum
.terms
[j
]->E
, num_p
, pden
, options
);
1341 free_evalue_refs(cum
.terms
[j
]->E
);
1342 delete cum
.terms
[j
]->E
;
1343 delete cum
.terms
[j
];
1350 static int type_offset(enode
*p
)
1352 return p
->type
== fractional
? 1 :
1353 p
->type
== flooring
? 1 : 0;
1356 static int edegree(evalue
*e
)
1361 if (value_notzero_p(e
->d
))
1365 int i
= type_offset(p
);
1366 if (p
->size
-i
-1 > d
)
1367 d
= p
->size
- i
- 1;
1368 for (; i
< p
->size
; i
++) {
1369 int d2
= edegree(&p
->arr
[i
]);
1376 void ienumerator::handle(const signed_cone
& sc
, barvinok_options
*options
)
1378 assert(sc
.det
== 1);
1380 assert(sc
.rays
.NumRows() == dim
);
1382 lattice_point(V
, sc
.rays
, vertex
[0], E_vertex
, options
);
1388 evalue_set_si(&one
, sc
.sign
, 1);
1389 reduce(&one
, vertex
, den
, options
);
1390 free_evalue_refs(&one
);
1392 for (int i
= 0; i
< dim
; ++i
)
1394 free_evalue_refs(E_vertex
[i
]);
1399 struct bfenumerator
: public vertex_decomposer
, public bf_base
,
1400 public ienumerator_base
{
1403 bfenumerator(Polyhedron
*P
, unsigned dim
, unsigned nbV
) :
1404 vertex_decomposer(P
, nbV
, *this),
1405 bf_base(dim
), ienumerator_base(dim
, this) {
1413 virtual void handle(const signed_cone
& sc
, barvinok_options
*options
);
1414 virtual void base(mat_ZZ
& factors
, bfc_vec
& v
);
1416 bfc_term_base
* new_bf_term(int len
) {
1417 bfe_term
* t
= new bfe_term(len
);
1421 virtual void set_factor(bfc_term_base
*t
, int k
, int change
) {
1422 bfe_term
* bfet
= static_cast<bfe_term
*>(t
);
1423 factor
= bfet
->factors
[k
];
1424 assert(factor
!= NULL
);
1425 bfet
->factors
[k
] = NULL
;
1427 emul(&mone
, factor
);
1430 virtual void set_factor(bfc_term_base
*t
, int k
, mpq_t
&q
, int change
) {
1431 bfe_term
* bfet
= static_cast<bfe_term
*>(t
);
1432 factor
= bfet
->factors
[k
];
1433 assert(factor
!= NULL
);
1434 bfet
->factors
[k
] = NULL
;
1440 value_oppose(f
.x
.n
, mpq_numref(q
));
1442 value_assign(f
.x
.n
, mpq_numref(q
));
1443 value_assign(f
.d
, mpq_denref(q
));
1445 free_evalue_refs(&f
);
1448 virtual void set_factor(bfc_term_base
*t
, int k
, const QQ
& c
, int change
) {
1449 bfe_term
* bfet
= static_cast<bfe_term
*>(t
);
1451 factor
= new evalue
;
1456 zz2value(c
.n
, f
.x
.n
);
1458 value_oppose(f
.x
.n
, f
.x
.n
);
1461 value_init(factor
->d
);
1462 evalue_copy(factor
, bfet
->factors
[k
]);
1464 free_evalue_refs(&f
);
1467 void set_factor(evalue
*f
, int change
) {
1473 virtual void insert_term(bfc_term_base
*t
, int i
) {
1474 bfe_term
* bfet
= static_cast<bfe_term
*>(t
);
1475 int len
= t
->terms
.NumRows()-1; // already increased by one
1477 bfet
->factors
.resize(len
+1);
1478 for (int j
= len
; j
> i
; --j
) {
1479 bfet
->factors
[j
] = bfet
->factors
[j
-1];
1480 t
->terms
[j
] = t
->terms
[j
-1];
1482 bfet
->factors
[i
] = factor
;
1486 virtual void update_term(bfc_term_base
*t
, int i
) {
1487 bfe_term
* bfet
= static_cast<bfe_term
*>(t
);
1489 eadd(factor
, bfet
->factors
[i
]);
1490 free_evalue_refs(factor
);
1494 virtual bool constant_vertex(int dim
) { return E_num(0, dim
) == 0; }
1496 virtual void cum(bf_reducer
*bfr
, bfc_term_base
*t
, int k
, dpoly_r
*r
,
1497 barvinok_options
*options
);
1500 enumerator_base
*enumerator_base::create(Polyhedron
*P
, unsigned dim
, unsigned nbV
,
1501 barvinok_options
*options
)
1503 enumerator_base
*eb
;
1505 if (options
->incremental_specialization
== BV_SPECIALIZATION_BF
)
1506 eb
= new bfenumerator(P
, dim
, nbV
);
1507 else if (options
->incremental_specialization
== BV_SPECIALIZATION_DF
)
1508 eb
= new ienumerator(P
, dim
, nbV
);
1510 eb
= new enumerator(P
, dim
, nbV
);
1515 struct bfe_cum
: public cumulator
{
1517 bfc_term_base
*told
;
1521 bfe_cum(evalue
*factor
, evalue
*v
, dpoly_r
*r
, bf_reducer
*bfr
,
1522 bfc_term_base
*t
, int k
, bfenumerator
*e
) :
1523 cumulator(factor
, v
, r
), told(t
), k(k
),
1527 virtual void add_term(const vector
<int>& powers
, evalue
*f2
);
1530 void bfe_cum::add_term(const vector
<int>& powers
, evalue
*f2
)
1532 bfr
->update_powers(powers
);
1534 bfc_term_base
* t
= bfe
->find_bfc_term(bfr
->vn
, bfr
->npowers
, bfr
->nnf
);
1535 bfe
->set_factor(f2
, bfr
->l_changes
% 2);
1536 bfe
->add_term(t
, told
->terms
[k
], bfr
->l_extra_num
);
1539 void bfenumerator::cum(bf_reducer
*bfr
, bfc_term_base
*t
, int k
,
1540 dpoly_r
*r
, barvinok_options
*options
)
1542 bfe_term
* bfet
= static_cast<bfe_term
*>(t
);
1543 bfe_cum
cum(bfet
->factors
[k
], E_num(0, bfr
->d
), r
, bfr
, t
, k
, this);
1544 cum
.cumulate(options
);
1547 void bfenumerator::base(mat_ZZ
& factors
, bfc_vec
& v
)
1549 for (int i
= 0; i
< v
.size(); ++i
) {
1550 assert(v
[i
]->terms
.NumRows() == 1);
1551 evalue
*factor
= static_cast<bfe_term
*>(v
[i
])->factors
[0];
1552 eadd(factor
, vE
[vert
]);
1557 void bfenumerator::handle(const signed_cone
& sc
, barvinok_options
*options
)
1559 assert(sc
.det
== 1);
1561 assert(sc
.rays
.NumRows() == enumerator_base::dim
);
1563 bfe_term
* t
= new bfe_term(enumerator_base::dim
);
1564 vector
< bfc_term_base
* > v
;
1567 t
->factors
.resize(1);
1569 t
->terms
.SetDims(1, enumerator_base::dim
);
1570 lattice_point(V
, sc
.rays
, t
->terms
[0], E_vertex
, options
);
1572 // the elements of factors are always lexpositive
1574 int s
= setup_factors(sc
.rays
, factors
, t
, sc
.sign
);
1576 t
->factors
[0] = new evalue
;
1577 value_init(t
->factors
[0]->d
);
1578 evalue_set_si(t
->factors
[0], s
, 1);
1579 reduce(factors
, v
, options
);
1581 for (int i
= 0; i
< enumerator_base::dim
; ++i
)
1583 free_evalue_refs(E_vertex
[i
]);
1588 #ifdef HAVE_CORRECT_VERTICES
1589 static inline Param_Polyhedron
*Polyhedron2Param_SD(Polyhedron
**Din
,
1590 Polyhedron
*Cin
,int WS
,Polyhedron
**CEq
,Matrix
**CT
)
1592 if (WS
& POL_NO_DUAL
)
1594 return Polyhedron2Param_SimplifiedDomain(Din
, Cin
, WS
, CEq
, CT
);
1597 static Param_Polyhedron
*Polyhedron2Param_SD(Polyhedron
**Din
,
1598 Polyhedron
*Cin
,int WS
,Polyhedron
**CEq
,Matrix
**CT
)
1600 static char data
[] = " 1 0 0 0 0 1 -18 "
1601 " 1 0 0 -20 0 19 1 "
1602 " 1 0 1 20 0 -20 16 "
1605 " 1 4 -20 0 0 -1 23 "
1606 " 1 -4 20 0 0 1 -22 "
1607 " 1 0 1 0 20 -20 16 "
1608 " 1 0 0 0 -20 19 1 ";
1609 static int checked
= 0;
1614 Matrix
*M
= Matrix_Alloc(9, 7);
1615 for (i
= 0; i
< 9; ++i
)
1616 for (int j
= 0; j
< 7; ++j
) {
1617 sscanf(p
, "%d%n", &v
, &n
);
1619 value_set_si(M
->p
[i
][j
], v
);
1621 Polyhedron
*P
= Constraints2Polyhedron(M
, 1024);
1623 Polyhedron
*U
= Universe_Polyhedron(1);
1624 Param_Polyhedron
*PP
= Polyhedron2Param_Domain(P
, U
, 1024);
1628 for (i
= 0, V
= PP
->V
; V
; ++i
, V
= V
->next
)
1631 Param_Polyhedron_Free(PP
);
1633 fprintf(stderr
, "WARNING: results may be incorrect\n");
1635 "WARNING: use latest version of PolyLib to remove this warning\n");
1639 return Polyhedron2Param_SimplifiedDomain(Din
, Cin
, WS
, CEq
, CT
);
1643 static evalue
* barvinok_enumerate_ev_f(Polyhedron
*P
, Polyhedron
* C
,
1644 barvinok_options
*options
);
1647 static evalue
* barvinok_enumerate_cst(Polyhedron
*P
, Polyhedron
* C
,
1648 struct barvinok_options
*options
)
1652 ALLOC(evalue
, eres
);
1653 value_init(eres
->d
);
1654 value_set_si(eres
->d
, 0);
1655 eres
->x
.p
= new_enode(partition
, 2, C
->Dimension
);
1656 EVALUE_SET_DOMAIN(eres
->x
.p
->arr
[0],
1657 DomainConstraintSimplify(C
, options
->MaxRays
));
1658 value_set_si(eres
->x
.p
->arr
[1].d
, 1);
1659 value_init(eres
->x
.p
->arr
[1].x
.n
);
1661 value_set_si(eres
->x
.p
->arr
[1].x
.n
, 0);
1663 barvinok_count_with_options(P
, &eres
->x
.p
->arr
[1].x
.n
, options
);
1668 evalue
* barvinok_enumerate_with_options(Polyhedron
*P
, Polyhedron
* C
,
1669 struct barvinok_options
*options
)
1671 //P = unfringe(P, MaxRays);
1672 Polyhedron
*next
, *Cnext
;
1673 Polyhedron
*Corig
= C
;
1674 Polyhedron
*Porig
= P
;
1675 Polyhedron
*CEq
= NULL
, *rVD
, *CA
;
1677 unsigned nparam
= C
->Dimension
;
1682 "barvinok_enumerate: input is a union; only first polyhedron is enumerated\n");
1686 "barvinok_enumerate: context is a union; only first polyhedron is considered\n");
1689 value_init(factor
.d
);
1690 evalue_set_si(&factor
, 1, 1);
1694 CA
= align_context(C
, P
->Dimension
, options
->MaxRays
);
1697 P
= DomainIntersection(P
, CA
, options
->MaxRays
);
1699 Polyhedron_Free(CA
);
1702 POL_ENSURE_FACETS(P
);
1703 POL_ENSURE_VERTICES(P
);
1704 POL_ENSURE_FACETS(C
);
1705 POL_ENSURE_VERTICES(C
);
1707 if (C
->Dimension
== 0 || emptyQ(P
)) {
1709 eres
= barvinok_enumerate_cst(P
, CEq
? CEq
: Polyhedron_Copy(C
), options
);
1711 emul(&factor
, eres
);
1712 if (options
->approximation_method
== BV_APPROX_DROP
) {
1713 if (options
->polynomial_approximation
== BV_APPROX_SIGN_UPPER
)
1714 evalue_frac2polynomial(eres
, 1, options
->MaxRays
);
1715 if (options
->polynomial_approximation
== BV_APPROX_SIGN_LOWER
)
1716 evalue_frac2polynomial(eres
, -1, options
->MaxRays
);
1718 reduce_evalue(eres
);
1719 free_evalue_refs(&factor
);
1724 Corig
->next
= Cnext
;
1727 if (Polyhedron_is_unbounded(P
, nparam
, options
->MaxRays
))
1732 P
= remove_equalities_p(P
, P
->Dimension
-nparam
, &f
);
1733 mask(f
, &factor
, options
);
1736 if (P
->Dimension
== nparam
) {
1738 P
= Universe_Polyhedron(0);
1742 Polyhedron
*T
= Polyhedron_Factor(P
, nparam
, NULL
, options
->MaxRays
);
1743 if (T
|| (P
->Dimension
== nparam
+1)) {
1746 for (Q
= T
? T
: P
; Q
; Q
= Q
->next
) {
1747 Polyhedron
*next
= Q
->next
;
1751 if (Q
->Dimension
!= C
->Dimension
)
1752 QC
= Polyhedron_Project(Q
, nparam
);
1755 C
= DomainIntersection(C
, QC
, options
->MaxRays
);
1757 Polyhedron_Free(C2
);
1759 Polyhedron_Free(QC
);
1767 if (T
->Dimension
== C
->Dimension
) {
1776 eres
= barvinok_enumerate_ev_f(P
, C
, options
);
1783 for (Q
= P
->next
; Q
; Q
= Q
->next
) {
1784 Polyhedron
*next
= Q
->next
;
1787 f
= barvinok_enumerate_ev_f(Q
, C
, options
);
1789 free_evalue_refs(f
);
1799 evalue
* barvinok_enumerate_ev(Polyhedron
*P
, Polyhedron
* C
, unsigned MaxRays
)
1802 barvinok_options
*options
= barvinok_options_new_with_defaults();
1803 options
->MaxRays
= MaxRays
;
1804 E
= barvinok_enumerate_with_options(P
, C
, options
);
1805 barvinok_options_free(options
);
1809 /* adapted from mpolyhedron_inflate in PolyLib */
1810 static Polyhedron
*Polyhedron_Inflate(Polyhedron
*P
, unsigned nparam
,
1814 int nvar
= P
->Dimension
- nparam
;
1815 Matrix
*C
= Polyhedron2Constraints(P
);
1819 /* subtract the sum of the negative coefficients of each inequality */
1820 for (int i
= 0; i
< C
->NbRows
; ++i
) {
1821 value_set_si(sum
, 0);
1822 for (int j
= 0; j
< nvar
; ++j
)
1823 if (value_neg_p(C
->p
[i
][1+j
]))
1824 value_addto(sum
, sum
, C
->p
[i
][1+j
]);
1825 value_subtract(C
->p
[i
][1+P
->Dimension
], C
->p
[i
][1+P
->Dimension
], sum
);
1828 P2
= Constraints2Polyhedron(C
, MaxRays
);
1833 /* adapted from mpolyhedron_deflate in PolyLib */
1834 static Polyhedron
*Polyhedron_Deflate(Polyhedron
*P
, unsigned nparam
,
1838 int nvar
= P
->Dimension
- nparam
;
1839 Matrix
*C
= Polyhedron2Constraints(P
);
1843 /* subtract the sum of the positive coefficients of each inequality */
1844 for (int i
= 0; i
< C
->NbRows
; ++i
) {
1845 value_set_si(sum
, 0);
1846 for (int j
= 0; j
< nvar
; ++j
)
1847 if (value_pos_p(C
->p
[i
][1+j
]))
1848 value_addto(sum
, sum
, C
->p
[i
][1+j
]);
1849 value_subtract(C
->p
[i
][1+P
->Dimension
], C
->p
[i
][1+P
->Dimension
], sum
);
1852 P2
= Constraints2Polyhedron(C
, MaxRays
);
1857 static evalue
* barvinok_enumerate_ev_f(Polyhedron
*P
, Polyhedron
* C
,
1858 barvinok_options
*options
)
1860 unsigned nparam
= C
->Dimension
;
1861 bool scale_fast
= options
->approximation_method
== BV_APPROX_SCALE_FAST
;
1862 bool scale
= options
->approximation_method
== BV_APPROX_SCALE
;
1864 if (P
->Dimension
- nparam
== 1 && !scale_fast
)
1865 return ParamLine_Length(P
, C
, options
);
1867 Param_Polyhedron
*PP
= NULL
;
1868 Polyhedron
*CEq
= NULL
, *pVD
;
1870 Param_Domain
*D
, *next
;
1873 Polyhedron
*Porig
= P
;
1877 if (scale
|| scale_fast
) {
1878 if (options
->polynomial_approximation
== BV_APPROX_SIGN_UPPER
)
1879 P
= Polyhedron_Inflate(P
, nparam
, options
->MaxRays
);
1880 if (options
->polynomial_approximation
== BV_APPROX_SIGN_LOWER
) {
1881 P
= Polyhedron_Deflate(P
, nparam
, options
->MaxRays
);
1882 POL_ENSURE_VERTICES(P
);
1884 eres
= barvinok_enumerate_cst(P
, Polyhedron_Copy(C
), options
);
1892 PP
= Polyhedron2Param_SD(&T
, C
, options
->MaxRays
, &CEq
, &CT
);
1893 if (T
!= P
&& P
!= Porig
)
1897 if (isIdentity(CT
)) {
1901 assert(CT
->NbRows
!= CT
->NbColumns
);
1902 if (CT
->NbRows
== 1) { // no more parameters
1903 eres
= barvinok_enumerate_cst(P
, CEq
, options
);
1908 Param_Polyhedron_Free(PP
);
1914 nparam
= CT
->NbRows
- 1;
1917 if (scale
|| scale_fast
) {
1921 Param_Polyhedron_Scale_Integer(PP
, &T
, &det
, options
->MaxRays
);
1923 Param_Polyhedron_Scale_Integer_Slow(PP
, &T
, &det
, options
->MaxRays
);
1929 unsigned dim
= P
->Dimension
- nparam
;
1931 ALLOC(evalue
, eres
);
1932 value_init(eres
->d
);
1933 value_set_si(eres
->d
, 0);
1936 for (nd
= 0, D
=PP
->D
; D
; ++nd
, D
=D
->next
);
1937 struct section
{ Polyhedron
*D
; evalue E
; };
1938 section
*s
= new section
[nd
];
1939 Polyhedron
**fVD
= new Polyhedron_p
[nd
];
1941 enumerator_base
*et
= NULL
;
1946 et
= enumerator_base::create(P
, dim
, PP
->nbV
, options
);
1948 for(nd
= 0, D
=PP
->D
; D
; D
=next
) {
1951 Polyhedron
*rVD
= reduce_domain(D
->Domain
, CT
, CEq
, fVD
, nd
, options
);
1955 pVD
= CT
? DomainImage(rVD
,CT
,options
->MaxRays
) : rVD
;
1957 value_init(s
[nd
].E
.d
);
1958 evalue_set_si(&s
[nd
].E
, 0, 1);
1961 FORALL_PVertex_in_ParamPolyhedron(V
,D
,PP
) // _i is internal counter
1964 et
->decompose_at(V
, _i
, options
);
1965 } catch (OrthogonalException
&e
) {
1968 for (; nd
>= 0; --nd
) {
1969 free_evalue_refs(&s
[nd
].E
);
1970 Domain_Free(s
[nd
].D
);
1971 Domain_Free(fVD
[nd
]);
1975 eadd(et
->vE
[_i
] , &s
[nd
].E
);
1976 END_FORALL_PVertex_in_ParamPolyhedron
;
1977 evalue_range_reduction_in_domain(&s
[nd
].E
, pVD
);
1980 addeliminatedparams_evalue(&s
[nd
].E
, CT
);
1988 evalue_set_si(eres
, 0, 1);
1990 eres
->x
.p
= new_enode(partition
, 2*nd
, C
->Dimension
);
1991 for (int j
= 0; j
< nd
; ++j
) {
1992 EVALUE_SET_DOMAIN(eres
->x
.p
->arr
[2*j
], s
[j
].D
);
1993 value_clear(eres
->x
.p
->arr
[2*j
+1].d
);
1994 eres
->x
.p
->arr
[2*j
+1] = s
[j
].E
;
1995 Domain_Free(fVD
[j
]);
2001 if (scale
|| scale_fast
) {
2002 evalue_div(eres
, det
);
2007 Polyhedron_Free(CEq
);
2011 Enumeration
* barvinok_enumerate(Polyhedron
*P
, Polyhedron
* C
, unsigned MaxRays
)
2013 evalue
*EP
= barvinok_enumerate_ev(P
, C
, MaxRays
);
2015 return partition2enumeration(EP
);
2018 static void SwapColumns(Value
**V
, int n
, int i
, int j
)
2020 for (int r
= 0; r
< n
; ++r
)
2021 value_swap(V
[r
][i
], V
[r
][j
]);
2024 static void SwapColumns(Polyhedron
*P
, int i
, int j
)
2026 SwapColumns(P
->Constraint
, P
->NbConstraints
, i
, j
);
2027 SwapColumns(P
->Ray
, P
->NbRays
, i
, j
);
2030 /* Construct a constraint c from constraints l and u such that if
2031 * if constraint c holds then for each value of the other variables
2032 * there is at most one value of variable pos (position pos+1 in the constraints).
2034 * Given a lower and an upper bound
2035 * n_l v_i + <c_l,x> + c_l >= 0
2036 * -n_u v_i + <c_u,x> + c_u >= 0
2037 * the constructed constraint is
2039 * -(n_l<c_u,x> + n_u<c_l,x>) + (-n_l c_u - n_u c_l + n_l n_u - 1)
2041 * which is then simplified to remove the content of the non-constant coefficients
2043 * len is the total length of the constraints.
2044 * v is a temporary variable that can be used by this procedure
2046 static void negative_test_constraint(Value
*l
, Value
*u
, Value
*c
, int pos
,
2049 value_oppose(*v
, u
[pos
+1]);
2050 Vector_Combine(l
+1, u
+1, c
+1, *v
, l
[pos
+1], len
-1);
2051 value_multiply(*v
, *v
, l
[pos
+1]);
2052 value_subtract(c
[len
-1], c
[len
-1], *v
);
2053 value_set_si(*v
, -1);
2054 Vector_Scale(c
+1, c
+1, *v
, len
-1);
2055 value_decrement(c
[len
-1], c
[len
-1]);
2056 ConstraintSimplify(c
, c
, len
, v
);
2059 static bool parallel_constraints(Value
*l
, Value
*u
, Value
*c
, int pos
,
2068 Vector_Gcd(&l
[1+pos
], len
, &g1
);
2069 Vector_Gcd(&u
[1+pos
], len
, &g2
);
2070 Vector_Combine(l
+1+pos
, u
+1+pos
, c
+1, g2
, g1
, len
);
2071 parallel
= First_Non_Zero(c
+1, len
) == -1;
2079 static void negative_test_constraint7(Value
*l
, Value
*u
, Value
*c
, int pos
,
2080 int exist
, int len
, Value
*v
)
2085 Vector_Gcd(&u
[1+pos
], exist
, v
);
2086 Vector_Gcd(&l
[1+pos
], exist
, &g
);
2087 Vector_Combine(l
+1, u
+1, c
+1, *v
, g
, len
-1);
2088 value_multiply(*v
, *v
, g
);
2089 value_subtract(c
[len
-1], c
[len
-1], *v
);
2090 value_set_si(*v
, -1);
2091 Vector_Scale(c
+1, c
+1, *v
, len
-1);
2092 value_decrement(c
[len
-1], c
[len
-1]);
2093 ConstraintSimplify(c
, c
, len
, v
);
2098 /* Turns a x + b >= 0 into a x + b <= -1
2100 * len is the total length of the constraint.
2101 * v is a temporary variable that can be used by this procedure
2103 static void oppose_constraint(Value
*c
, int len
, Value
*v
)
2105 value_set_si(*v
, -1);
2106 Vector_Scale(c
+1, c
+1, *v
, len
-1);
2107 value_decrement(c
[len
-1], c
[len
-1]);
2110 /* Split polyhedron P into two polyhedra *pos and *neg, where
2111 * existential variable i has at most one solution for each
2112 * value of the other variables in *neg.
2114 * The splitting is performed using constraints l and u.
2116 * nvar: number of set variables
2117 * row: temporary vector that can be used by this procedure
2118 * f: temporary value that can be used by this procedure
2120 static bool SplitOnConstraint(Polyhedron
*P
, int i
, int l
, int u
,
2121 int nvar
, int MaxRays
, Vector
*row
, Value
& f
,
2122 Polyhedron
**pos
, Polyhedron
**neg
)
2124 negative_test_constraint(P
->Constraint
[l
], P
->Constraint
[u
],
2125 row
->p
, nvar
+i
, P
->Dimension
+2, &f
);
2126 *neg
= AddConstraints(row
->p
, 1, P
, MaxRays
);
2128 /* We found an independent, but useless constraint
2129 * Maybe we should detect this earlier and not
2130 * mark the variable as INDEPENDENT
2132 if (emptyQ((*neg
))) {
2133 Polyhedron_Free(*neg
);
2137 oppose_constraint(row
->p
, P
->Dimension
+2, &f
);
2138 *pos
= AddConstraints(row
->p
, 1, P
, MaxRays
);
2140 if (emptyQ((*pos
))) {
2141 Polyhedron_Free(*neg
);
2142 Polyhedron_Free(*pos
);
2150 * unimodularly transform P such that constraint r is transformed
2151 * into a constraint that involves only a single (the first)
2152 * existential variable
2155 static Polyhedron
*rotate_along(Polyhedron
*P
, int r
, int nvar
, int exist
,
2161 Vector
*row
= Vector_Alloc(exist
);
2162 Vector_Copy(P
->Constraint
[r
]+1+nvar
, row
->p
, exist
);
2163 Vector_Gcd(row
->p
, exist
, &g
);
2164 if (value_notone_p(g
))
2165 Vector_AntiScale(row
->p
, row
->p
, g
, exist
);
2168 Matrix
*M
= unimodular_complete(row
);
2169 Matrix
*M2
= Matrix_Alloc(P
->Dimension
+1, P
->Dimension
+1);
2170 for (r
= 0; r
< nvar
; ++r
)
2171 value_set_si(M2
->p
[r
][r
], 1);
2172 for ( ; r
< nvar
+exist
; ++r
)
2173 Vector_Copy(M
->p
[r
-nvar
], M2
->p
[r
]+nvar
, exist
);
2174 for ( ; r
< P
->Dimension
+1; ++r
)
2175 value_set_si(M2
->p
[r
][r
], 1);
2176 Polyhedron
*T
= Polyhedron_Image(P
, M2
, MaxRays
);
2185 /* Split polyhedron P into two polyhedra *pos and *neg, where
2186 * existential variable i has at most one solution for each
2187 * value of the other variables in *neg.
2189 * If independent is set, then the two constraints on which the
2190 * split will be performed need to be independent of the other
2191 * existential variables.
2193 * Return true if an appropriate split could be performed.
2195 * nvar: number of set variables
2196 * exist: number of existential variables
2197 * row: temporary vector that can be used by this procedure
2198 * f: temporary value that can be used by this procedure
2200 static bool SplitOnVar(Polyhedron
*P
, int i
,
2201 int nvar
, int exist
, int MaxRays
,
2202 Vector
*row
, Value
& f
, bool independent
,
2203 Polyhedron
**pos
, Polyhedron
**neg
)
2207 for (int l
= P
->NbEq
; l
< P
->NbConstraints
; ++l
) {
2208 if (value_negz_p(P
->Constraint
[l
][nvar
+i
+1]))
2212 for (j
= 0; j
< exist
; ++j
)
2213 if (j
!= i
&& value_notzero_p(P
->Constraint
[l
][nvar
+j
+1]))
2219 for (int u
= P
->NbEq
; u
< P
->NbConstraints
; ++u
) {
2220 if (value_posz_p(P
->Constraint
[u
][nvar
+i
+1]))
2224 for (j
= 0; j
< exist
; ++j
)
2225 if (j
!= i
&& value_notzero_p(P
->Constraint
[u
][nvar
+j
+1]))
2231 if (SplitOnConstraint(P
, i
, l
, u
, nvar
, MaxRays
, row
, f
, pos
, neg
)) {
2234 SwapColumns(*neg
, nvar
+1, nvar
+1+i
);
2244 static bool double_bound_pair(Polyhedron
*P
, int nvar
, int exist
,
2245 int i
, int l1
, int l2
,
2246 Polyhedron
**pos
, Polyhedron
**neg
)
2250 Vector
*row
= Vector_Alloc(P
->Dimension
+2);
2251 value_set_si(row
->p
[0], 1);
2252 value_oppose(f
, P
->Constraint
[l1
][nvar
+i
+1]);
2253 Vector_Combine(P
->Constraint
[l1
]+1, P
->Constraint
[l2
]+1,
2255 P
->Constraint
[l2
][nvar
+i
+1], f
,
2257 ConstraintSimplify(row
->p
, row
->p
, P
->Dimension
+2, &f
);
2258 *pos
= AddConstraints(row
->p
, 1, P
, 0);
2259 value_set_si(f
, -1);
2260 Vector_Scale(row
->p
+1, row
->p
+1, f
, P
->Dimension
+1);
2261 value_decrement(row
->p
[P
->Dimension
+1], row
->p
[P
->Dimension
+1]);
2262 *neg
= AddConstraints(row
->p
, 1, P
, 0);
2266 return !emptyQ((*pos
)) && !emptyQ((*neg
));
2269 static bool double_bound(Polyhedron
*P
, int nvar
, int exist
,
2270 Polyhedron
**pos
, Polyhedron
**neg
)
2272 for (int i
= 0; i
< exist
; ++i
) {
2274 for (l1
= P
->NbEq
; l1
< P
->NbConstraints
; ++l1
) {
2275 if (value_negz_p(P
->Constraint
[l1
][nvar
+i
+1]))
2277 for (l2
= l1
+ 1; l2
< P
->NbConstraints
; ++l2
) {
2278 if (value_negz_p(P
->Constraint
[l2
][nvar
+i
+1]))
2280 if (double_bound_pair(P
, nvar
, exist
, i
, l1
, l2
, pos
, neg
))
2284 for (l1
= P
->NbEq
; l1
< P
->NbConstraints
; ++l1
) {
2285 if (value_posz_p(P
->Constraint
[l1
][nvar
+i
+1]))
2287 if (l1
< P
->NbConstraints
)
2288 for (l2
= l1
+ 1; l2
< P
->NbConstraints
; ++l2
) {
2289 if (value_posz_p(P
->Constraint
[l2
][nvar
+i
+1]))
2291 if (double_bound_pair(P
, nvar
, exist
, i
, l1
, l2
, pos
, neg
))
2303 INDEPENDENT
= 1 << 2,
2307 static evalue
* enumerate_or(Polyhedron
*D
,
2308 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2311 fprintf(stderr
, "\nER: Or\n");
2312 #endif /* DEBUG_ER */
2314 Polyhedron
*N
= D
->next
;
2317 barvinok_enumerate_e_with_options(D
, exist
, nparam
, options
);
2320 for (D
= N
; D
; D
= N
) {
2325 barvinok_enumerate_e_with_options(D
, exist
, nparam
, options
);
2328 free_evalue_refs(EN
);
2338 static evalue
* enumerate_sum(Polyhedron
*P
,
2339 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2341 int nvar
= P
->Dimension
- exist
- nparam
;
2342 int toswap
= nvar
< exist
? nvar
: exist
;
2343 for (int i
= 0; i
< toswap
; ++i
)
2344 SwapColumns(P
, 1 + i
, nvar
+exist
- i
);
2348 fprintf(stderr
, "\nER: Sum\n");
2349 #endif /* DEBUG_ER */
2351 evalue
*EP
= barvinok_enumerate_e_with_options(P
, exist
, nparam
, options
);
2353 evalue_split_domains_into_orthants(EP
, options
->MaxRays
);
2355 evalue_range_reduction(EP
);
2357 evalue_frac2floor2(EP
, 1);
2359 evalue
*sum
= esum(EP
, nvar
);
2361 free_evalue_refs(EP
);
2365 evalue_range_reduction(EP
);
2370 static evalue
* split_sure(Polyhedron
*P
, Polyhedron
*S
,
2371 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2373 int nvar
= P
->Dimension
- exist
- nparam
;
2375 Matrix
*M
= Matrix_Alloc(exist
, S
->Dimension
+2);
2376 for (int i
= 0; i
< exist
; ++i
)
2377 value_set_si(M
->p
[i
][nvar
+i
+1], 1);
2379 S
= DomainAddRays(S
, M
, options
->MaxRays
);
2381 Polyhedron
*F
= DomainAddRays(P
, M
, options
->MaxRays
);
2382 Polyhedron
*D
= DomainDifference(F
, S
, options
->MaxRays
);
2384 D
= Disjoint_Domain(D
, 0, options
->MaxRays
);
2389 M
= Matrix_Alloc(P
->Dimension
+1-exist
, P
->Dimension
+1);
2390 for (int j
= 0; j
< nvar
; ++j
)
2391 value_set_si(M
->p
[j
][j
], 1);
2392 for (int j
= 0; j
< nparam
+1; ++j
)
2393 value_set_si(M
->p
[nvar
+j
][nvar
+exist
+j
], 1);
2394 Polyhedron
*T
= Polyhedron_Image(S
, M
, options
->MaxRays
);
2395 evalue
*EP
= barvinok_enumerate_e_with_options(T
, 0, nparam
, options
);
2400 for (Polyhedron
*Q
= D
; Q
; Q
= Q
->next
) {
2401 Polyhedron
*N
= Q
->next
;
2403 T
= DomainIntersection(P
, Q
, options
->MaxRays
);
2404 evalue
*E
= barvinok_enumerate_e_with_options(T
, exist
, nparam
, options
);
2406 free_evalue_refs(E
);
2415 static evalue
* enumerate_sure(Polyhedron
*P
,
2416 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2420 int nvar
= P
->Dimension
- exist
- nparam
;
2426 for (i
= 0; i
< exist
; ++i
) {
2427 Matrix
*M
= Matrix_Alloc(S
->NbConstraints
, S
->Dimension
+2);
2429 value_set_si(lcm
, 1);
2430 for (int j
= 0; j
< S
->NbConstraints
; ++j
) {
2431 if (value_negz_p(S
->Constraint
[j
][1+nvar
+i
]))
2433 if (value_one_p(S
->Constraint
[j
][1+nvar
+i
]))
2435 value_lcm(lcm
, S
->Constraint
[j
][1+nvar
+i
], &lcm
);
2438 for (int j
= 0; j
< S
->NbConstraints
; ++j
) {
2439 if (value_negz_p(S
->Constraint
[j
][1+nvar
+i
]))
2441 if (value_one_p(S
->Constraint
[j
][1+nvar
+i
]))
2443 value_division(f
, lcm
, S
->Constraint
[j
][1+nvar
+i
]);
2444 Vector_Scale(S
->Constraint
[j
], M
->p
[c
], f
, S
->Dimension
+2);
2445 value_subtract(M
->p
[c
][S
->Dimension
+1],
2446 M
->p
[c
][S
->Dimension
+1],
2448 value_increment(M
->p
[c
][S
->Dimension
+1],
2449 M
->p
[c
][S
->Dimension
+1]);
2453 S
= AddConstraints(M
->p
[0], c
, S
, options
->MaxRays
);
2468 fprintf(stderr
, "\nER: Sure\n");
2469 #endif /* DEBUG_ER */
2471 return split_sure(P
, S
, exist
, nparam
, options
);
2474 static evalue
* enumerate_sure2(Polyhedron
*P
,
2475 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2477 int nvar
= P
->Dimension
- exist
- nparam
;
2479 for (r
= 0; r
< P
->NbRays
; ++r
)
2480 if (value_one_p(P
->Ray
[r
][0]) &&
2481 value_one_p(P
->Ray
[r
][P
->Dimension
+1]))
2487 Matrix
*M
= Matrix_Alloc(nvar
+ 1 + nparam
, P
->Dimension
+2);
2488 for (int i
= 0; i
< nvar
; ++i
)
2489 value_set_si(M
->p
[i
][1+i
], 1);
2490 for (int i
= 0; i
< nparam
; ++i
)
2491 value_set_si(M
->p
[i
+nvar
][1+nvar
+exist
+i
], 1);
2492 Vector_Copy(P
->Ray
[r
]+1+nvar
, M
->p
[nvar
+nparam
]+1+nvar
, exist
);
2493 value_set_si(M
->p
[nvar
+nparam
][0], 1);
2494 value_set_si(M
->p
[nvar
+nparam
][P
->Dimension
+1], 1);
2495 Polyhedron
* F
= Rays2Polyhedron(M
, options
->MaxRays
);
2498 Polyhedron
*I
= DomainIntersection(F
, P
, options
->MaxRays
);
2502 fprintf(stderr
, "\nER: Sure2\n");
2503 #endif /* DEBUG_ER */
2505 return split_sure(P
, I
, exist
, nparam
, options
);
2508 static evalue
* enumerate_cyclic(Polyhedron
*P
,
2509 unsigned exist
, unsigned nparam
,
2510 evalue
* EP
, int r
, int p
, unsigned MaxRays
)
2512 int nvar
= P
->Dimension
- exist
- nparam
;
2514 /* If EP in its fractional maps only contains references
2515 * to the remainder parameter with appropriate coefficients
2516 * then we could in principle avoid adding existentially
2517 * quantified variables to the validity domains.
2518 * We'd have to replace the remainder by m { p/m }
2519 * and multiply with an appropriate factor that is one
2520 * only in the appropriate range.
2521 * This last multiplication can be avoided if EP
2522 * has a single validity domain with no (further)
2523 * constraints on the remainder parameter
2526 Matrix
*CT
= Matrix_Alloc(nparam
+1, nparam
+3);
2527 Matrix
*M
= Matrix_Alloc(1, 1+nparam
+3);
2528 for (int j
= 0; j
< nparam
; ++j
)
2530 value_set_si(CT
->p
[j
][j
], 1);
2531 value_set_si(CT
->p
[p
][nparam
+1], 1);
2532 value_set_si(CT
->p
[nparam
][nparam
+2], 1);
2533 value_set_si(M
->p
[0][1+p
], -1);
2534 value_absolute(M
->p
[0][1+nparam
], P
->Ray
[0][1+nvar
+exist
+p
]);
2535 value_set_si(M
->p
[0][1+nparam
+1], 1);
2536 Polyhedron
*CEq
= Constraints2Polyhedron(M
, 1);
2538 addeliminatedparams_enum(EP
, CT
, CEq
, MaxRays
, nparam
);
2539 Polyhedron_Free(CEq
);
2545 static void enumerate_vd_add_ray(evalue
*EP
, Matrix
*Rays
, unsigned MaxRays
)
2547 if (value_notzero_p(EP
->d
))
2550 assert(EP
->x
.p
->type
== partition
);
2551 assert(EP
->x
.p
->pos
== EVALUE_DOMAIN(EP
->x
.p
->arr
[0])->Dimension
);
2552 for (int i
= 0; i
< EP
->x
.p
->size
/2; ++i
) {
2553 Polyhedron
*D
= EVALUE_DOMAIN(EP
->x
.p
->arr
[2*i
]);
2554 Polyhedron
*N
= DomainAddRays(D
, Rays
, MaxRays
);
2555 EVALUE_SET_DOMAIN(EP
->x
.p
->arr
[2*i
], N
);
2560 static evalue
* enumerate_line(Polyhedron
*P
,
2561 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2567 fprintf(stderr
, "\nER: Line\n");
2568 #endif /* DEBUG_ER */
2570 int nvar
= P
->Dimension
- exist
- nparam
;
2572 for (i
= 0; i
< nparam
; ++i
)
2573 if (value_notzero_p(P
->Ray
[0][1+nvar
+exist
+i
]))
2576 for (j
= i
+1; j
< nparam
; ++j
)
2577 if (value_notzero_p(P
->Ray
[0][1+nvar
+exist
+i
]))
2579 assert(j
>= nparam
); // for now
2581 Matrix
*M
= Matrix_Alloc(2, P
->Dimension
+2);
2582 value_set_si(M
->p
[0][0], 1);
2583 value_set_si(M
->p
[0][1+nvar
+exist
+i
], 1);
2584 value_set_si(M
->p
[1][0], 1);
2585 value_set_si(M
->p
[1][1+nvar
+exist
+i
], -1);
2586 value_absolute(M
->p
[1][1+P
->Dimension
], P
->Ray
[0][1+nvar
+exist
+i
]);
2587 value_decrement(M
->p
[1][1+P
->Dimension
], M
->p
[1][1+P
->Dimension
]);
2588 Polyhedron
*S
= AddConstraints(M
->p
[0], 2, P
, options
->MaxRays
);
2589 evalue
*EP
= barvinok_enumerate_e_with_options(S
, exist
, nparam
, options
);
2593 return enumerate_cyclic(P
, exist
, nparam
, EP
, 0, i
, options
->MaxRays
);
2596 static int single_param_pos(Polyhedron
*P
, unsigned exist
, unsigned nparam
,
2599 int nvar
= P
->Dimension
- exist
- nparam
;
2600 if (First_Non_Zero(P
->Ray
[r
]+1, nvar
) != -1)
2602 int i
= First_Non_Zero(P
->Ray
[r
]+1+nvar
+exist
, nparam
);
2605 if (First_Non_Zero(P
->Ray
[r
]+1+nvar
+exist
+1, nparam
-i
-1) != -1)
2610 static evalue
* enumerate_remove_ray(Polyhedron
*P
, int r
,
2611 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2614 fprintf(stderr
, "\nER: RedundantRay\n");
2615 #endif /* DEBUG_ER */
2619 value_set_si(one
, 1);
2620 int len
= P
->NbRays
-1;
2621 Matrix
*M
= Matrix_Alloc(2 * len
, P
->Dimension
+2);
2622 Vector_Copy(P
->Ray
[0], M
->p
[0], r
* (P
->Dimension
+2));
2623 Vector_Copy(P
->Ray
[r
+1], M
->p
[r
], (len
-r
) * (P
->Dimension
+2));
2624 for (int j
= 0; j
< P
->NbRays
; ++j
) {
2627 Vector_Combine(P
->Ray
[j
], P
->Ray
[r
], M
->p
[len
+j
-(j
>r
)],
2628 one
, P
->Ray
[j
][P
->Dimension
+1], P
->Dimension
+2);
2631 P
= Rays2Polyhedron(M
, options
->MaxRays
);
2633 evalue
*EP
= barvinok_enumerate_e_with_options(P
, exist
, nparam
, options
);
2640 static evalue
* enumerate_redundant_ray(Polyhedron
*P
,
2641 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2643 assert(P
->NbBid
== 0);
2644 int nvar
= P
->Dimension
- exist
- nparam
;
2648 for (int r
= 0; r
< P
->NbRays
; ++r
) {
2649 if (value_notzero_p(P
->Ray
[r
][P
->Dimension
+1]))
2651 int i1
= single_param_pos(P
, exist
, nparam
, r
);
2654 for (int r2
= r
+1; r2
< P
->NbRays
; ++r2
) {
2655 if (value_notzero_p(P
->Ray
[r2
][P
->Dimension
+1]))
2657 int i2
= single_param_pos(P
, exist
, nparam
, r2
);
2663 value_division(m
, P
->Ray
[r
][1+nvar
+exist
+i1
],
2664 P
->Ray
[r2
][1+nvar
+exist
+i1
]);
2665 value_multiply(m
, m
, P
->Ray
[r2
][1+nvar
+exist
+i1
]);
2666 /* r2 divides r => r redundant */
2667 if (value_eq(m
, P
->Ray
[r
][1+nvar
+exist
+i1
])) {
2669 return enumerate_remove_ray(P
, r
, exist
, nparam
, options
);
2672 value_division(m
, P
->Ray
[r2
][1+nvar
+exist
+i1
],
2673 P
->Ray
[r
][1+nvar
+exist
+i1
]);
2674 value_multiply(m
, m
, P
->Ray
[r
][1+nvar
+exist
+i1
]);
2675 /* r divides r2 => r2 redundant */
2676 if (value_eq(m
, P
->Ray
[r2
][1+nvar
+exist
+i1
])) {
2678 return enumerate_remove_ray(P
, r2
, exist
, nparam
, options
);
2686 static Polyhedron
*upper_bound(Polyhedron
*P
,
2687 int pos
, Value
*max
, Polyhedron
**R
)
2696 for (Polyhedron
*Q
= P
; Q
; Q
= N
) {
2698 for (r
= 0; r
< P
->NbRays
; ++r
) {
2699 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1]) &&
2700 value_pos_p(P
->Ray
[r
][1+pos
]))
2703 if (r
< P
->NbRays
) {
2711 for (r
= 0; r
< P
->NbRays
; ++r
) {
2712 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1]))
2714 mpz_fdiv_q(v
, P
->Ray
[r
][1+pos
], P
->Ray
[r
][1+P
->Dimension
]);
2715 if ((!Q
->next
&& r
== 0) || value_gt(v
, *max
))
2716 value_assign(*max
, v
);
2723 static evalue
* enumerate_ray(Polyhedron
*P
,
2724 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2726 assert(P
->NbBid
== 0);
2727 int nvar
= P
->Dimension
- exist
- nparam
;
2730 for (r
= 0; r
< P
->NbRays
; ++r
)
2731 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1]))
2737 for (r2
= r
+1; r2
< P
->NbRays
; ++r2
)
2738 if (value_zero_p(P
->Ray
[r2
][P
->Dimension
+1]))
2740 if (r2
< P
->NbRays
) {
2742 return enumerate_sum(P
, exist
, nparam
, options
);
2746 fprintf(stderr
, "\nER: Ray\n");
2747 #endif /* DEBUG_ER */
2753 value_set_si(one
, 1);
2754 int i
= single_param_pos(P
, exist
, nparam
, r
);
2755 assert(i
!= -1); // for now;
2757 Matrix
*M
= Matrix_Alloc(P
->NbRays
, P
->Dimension
+2);
2758 for (int j
= 0; j
< P
->NbRays
; ++j
) {
2759 Vector_Combine(P
->Ray
[j
], P
->Ray
[r
], M
->p
[j
],
2760 one
, P
->Ray
[j
][P
->Dimension
+1], P
->Dimension
+2);
2762 Polyhedron
*S
= Rays2Polyhedron(M
, options
->MaxRays
);
2764 Polyhedron
*D
= DomainDifference(P
, S
, options
->MaxRays
);
2766 // Polyhedron_Print(stderr, P_VALUE_FMT, D);
2767 assert(value_pos_p(P
->Ray
[r
][1+nvar
+exist
+i
])); // for now
2769 D
= upper_bound(D
, nvar
+exist
+i
, &m
, &R
);
2773 M
= Matrix_Alloc(2, P
->Dimension
+2);
2774 value_set_si(M
->p
[0][0], 1);
2775 value_set_si(M
->p
[1][0], 1);
2776 value_set_si(M
->p
[0][1+nvar
+exist
+i
], -1);
2777 value_set_si(M
->p
[1][1+nvar
+exist
+i
], 1);
2778 value_assign(M
->p
[0][1+P
->Dimension
], m
);
2779 value_oppose(M
->p
[1][1+P
->Dimension
], m
);
2780 value_addto(M
->p
[1][1+P
->Dimension
], M
->p
[1][1+P
->Dimension
],
2781 P
->Ray
[r
][1+nvar
+exist
+i
]);
2782 value_decrement(M
->p
[1][1+P
->Dimension
], M
->p
[1][1+P
->Dimension
]);
2783 // Matrix_Print(stderr, P_VALUE_FMT, M);
2784 D
= AddConstraints(M
->p
[0], 2, P
, options
->MaxRays
);
2785 // Polyhedron_Print(stderr, P_VALUE_FMT, D);
2786 value_subtract(M
->p
[0][1+P
->Dimension
], M
->p
[0][1+P
->Dimension
],
2787 P
->Ray
[r
][1+nvar
+exist
+i
]);
2788 // Matrix_Print(stderr, P_VALUE_FMT, M);
2789 S
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
2790 // Polyhedron_Print(stderr, P_VALUE_FMT, S);
2793 evalue
*EP
= barvinok_enumerate_e_with_options(D
, exist
, nparam
, options
);
2798 if (value_notone_p(P
->Ray
[r
][1+nvar
+exist
+i
]))
2799 EP
= enumerate_cyclic(P
, exist
, nparam
, EP
, r
, i
, options
->MaxRays
);
2801 M
= Matrix_Alloc(1, nparam
+2);
2802 value_set_si(M
->p
[0][0], 1);
2803 value_set_si(M
->p
[0][1+i
], 1);
2804 enumerate_vd_add_ray(EP
, M
, options
->MaxRays
);
2809 evalue
*E
= barvinok_enumerate_e_with_options(S
, exist
, nparam
, options
);
2811 free_evalue_refs(E
);
2818 evalue
*ER
= enumerate_or(R
, exist
, nparam
, options
);
2820 free_evalue_refs(ER
);
2827 static evalue
* enumerate_vd(Polyhedron
**PA
,
2828 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2830 Polyhedron
*P
= *PA
;
2831 int nvar
= P
->Dimension
- exist
- nparam
;
2832 Param_Polyhedron
*PP
= NULL
;
2833 Polyhedron
*C
= Universe_Polyhedron(nparam
);
2837 PP
= Polyhedron2Param_SimplifiedDomain(&PR
,C
, options
->MaxRays
,&CEq
,&CT
);
2841 Param_Domain
*D
, *last
;
2844 for (nd
= 0, D
=PP
->D
; D
; D
=D
->next
, ++nd
)
2847 Polyhedron
**VD
= new Polyhedron_p
[nd
];
2848 Polyhedron
**fVD
= new Polyhedron_p
[nd
];
2849 for(nd
= 0, D
=PP
->D
; D
; D
=D
->next
) {
2850 Polyhedron
*rVD
= reduce_domain(D
->Domain
, CT
, CEq
, fVD
, nd
, options
);
2863 /* This doesn't seem to have any effect */
2865 Polyhedron
*CA
= align_context(VD
[0], P
->Dimension
, options
->MaxRays
);
2867 P
= DomainIntersection(P
, CA
, options
->MaxRays
);
2870 Polyhedron_Free(CA
);
2875 if (!EP
&& CT
->NbColumns
!= CT
->NbRows
) {
2876 Polyhedron
*CEqr
= DomainImage(CEq
, CT
, options
->MaxRays
);
2877 Polyhedron
*CA
= align_context(CEqr
, PR
->Dimension
, options
->MaxRays
);
2878 Polyhedron
*I
= DomainIntersection(PR
, CA
, options
->MaxRays
);
2879 Polyhedron_Free(CEqr
);
2880 Polyhedron_Free(CA
);
2882 fprintf(stderr
, "\nER: Eliminate\n");
2883 #endif /* DEBUG_ER */
2884 nparam
-= CT
->NbColumns
- CT
->NbRows
;
2885 EP
= barvinok_enumerate_e_with_options(I
, exist
, nparam
, options
);
2886 nparam
+= CT
->NbColumns
- CT
->NbRows
;
2887 addeliminatedparams_enum(EP
, CT
, CEq
, options
->MaxRays
, nparam
);
2891 Polyhedron_Free(PR
);
2894 if (!EP
&& nd
> 1) {
2896 fprintf(stderr
, "\nER: VD\n");
2897 #endif /* DEBUG_ER */
2898 for (int i
= 0; i
< nd
; ++i
) {
2899 Polyhedron
*CA
= align_context(VD
[i
], P
->Dimension
, options
->MaxRays
);
2900 Polyhedron
*I
= DomainIntersection(P
, CA
, options
->MaxRays
);
2903 EP
= barvinok_enumerate_e_with_options(I
, exist
, nparam
, options
);
2905 evalue
*E
= barvinok_enumerate_e_with_options(I
, exist
, nparam
,
2908 free_evalue_refs(E
);
2912 Polyhedron_Free(CA
);
2916 for (int i
= 0; i
< nd
; ++i
) {
2917 Polyhedron_Free(VD
[i
]);
2918 Polyhedron_Free(fVD
[i
]);
2924 if (!EP
&& nvar
== 0) {
2927 Param_Vertices
*V
, *V2
;
2928 Matrix
* M
= Matrix_Alloc(1, P
->Dimension
+2);
2930 FORALL_PVertex_in_ParamPolyhedron(V
, last
, PP
) {
2932 FORALL_PVertex_in_ParamPolyhedron(V2
, last
, PP
) {
2939 for (int i
= 0; i
< exist
; ++i
) {
2940 value_oppose(f
, V
->Vertex
->p
[i
][nparam
+1]);
2941 Vector_Combine(V
->Vertex
->p
[i
],
2943 M
->p
[0] + 1 + nvar
+ exist
,
2944 V2
->Vertex
->p
[i
][nparam
+1],
2948 for (j
= 0; j
< nparam
; ++j
)
2949 if (value_notzero_p(M
->p
[0][1+nvar
+exist
+j
]))
2953 ConstraintSimplify(M
->p
[0], M
->p
[0],
2954 P
->Dimension
+2, &f
);
2955 value_set_si(M
->p
[0][0], 0);
2956 Polyhedron
*para
= AddConstraints(M
->p
[0], 1, P
,
2959 Polyhedron_Free(para
);
2962 Polyhedron
*pos
, *neg
;
2963 value_set_si(M
->p
[0][0], 1);
2964 value_decrement(M
->p
[0][P
->Dimension
+1],
2965 M
->p
[0][P
->Dimension
+1]);
2966 neg
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
2967 value_set_si(f
, -1);
2968 Vector_Scale(M
->p
[0]+1, M
->p
[0]+1, f
,
2970 value_decrement(M
->p
[0][P
->Dimension
+1],
2971 M
->p
[0][P
->Dimension
+1]);
2972 value_decrement(M
->p
[0][P
->Dimension
+1],
2973 M
->p
[0][P
->Dimension
+1]);
2974 pos
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
2975 if (emptyQ(neg
) && emptyQ(pos
)) {
2976 Polyhedron_Free(para
);
2977 Polyhedron_Free(pos
);
2978 Polyhedron_Free(neg
);
2982 fprintf(stderr
, "\nER: Order\n");
2983 #endif /* DEBUG_ER */
2984 EP
= barvinok_enumerate_e_with_options(para
, exist
, nparam
,
2988 E
= barvinok_enumerate_e_with_options(pos
, exist
, nparam
,
2991 free_evalue_refs(E
);
2995 E
= barvinok_enumerate_e_with_options(neg
, exist
, nparam
,
2998 free_evalue_refs(E
);
3001 Polyhedron_Free(para
);
3002 Polyhedron_Free(pos
);
3003 Polyhedron_Free(neg
);
3008 } END_FORALL_PVertex_in_ParamPolyhedron
;
3011 } END_FORALL_PVertex_in_ParamPolyhedron
;
3014 /* Search for vertex coordinate to split on */
3015 /* First look for one independent of the parameters */
3016 FORALL_PVertex_in_ParamPolyhedron(V
, last
, PP
) {
3017 for (int i
= 0; i
< exist
; ++i
) {
3019 for (j
= 0; j
< nparam
; ++j
)
3020 if (value_notzero_p(V
->Vertex
->p
[i
][j
]))
3024 value_set_si(M
->p
[0][0], 1);
3025 Vector_Set(M
->p
[0]+1, 0, nvar
+exist
);
3026 Vector_Copy(V
->Vertex
->p
[i
],
3027 M
->p
[0] + 1 + nvar
+ exist
, nparam
+1);
3028 value_oppose(M
->p
[0][1+nvar
+i
],
3029 V
->Vertex
->p
[i
][nparam
+1]);
3031 Polyhedron
*pos
, *neg
;
3032 value_set_si(M
->p
[0][0], 1);
3033 value_decrement(M
->p
[0][P
->Dimension
+1],
3034 M
->p
[0][P
->Dimension
+1]);
3035 neg
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
3036 value_set_si(f
, -1);
3037 Vector_Scale(M
->p
[0]+1, M
->p
[0]+1, f
,
3039 value_decrement(M
->p
[0][P
->Dimension
+1],
3040 M
->p
[0][P
->Dimension
+1]);
3041 value_decrement(M
->p
[0][P
->Dimension
+1],
3042 M
->p
[0][P
->Dimension
+1]);
3043 pos
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
3044 if (emptyQ(neg
) || emptyQ(pos
)) {
3045 Polyhedron_Free(pos
);
3046 Polyhedron_Free(neg
);
3049 Polyhedron_Free(pos
);
3050 value_increment(M
->p
[0][P
->Dimension
+1],
3051 M
->p
[0][P
->Dimension
+1]);
3052 pos
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
3054 fprintf(stderr
, "\nER: Vertex\n");
3055 #endif /* DEBUG_ER */
3057 EP
= enumerate_or(pos
, exist
, nparam
, options
);
3062 } END_FORALL_PVertex_in_ParamPolyhedron
;
3066 /* Search for vertex coordinate to split on */
3067 /* Now look for one that depends on the parameters */
3068 FORALL_PVertex_in_ParamPolyhedron(V
, last
, PP
) {
3069 for (int i
= 0; i
< exist
; ++i
) {
3070 value_set_si(M
->p
[0][0], 1);
3071 Vector_Set(M
->p
[0]+1, 0, nvar
+exist
);
3072 Vector_Copy(V
->Vertex
->p
[i
],
3073 M
->p
[0] + 1 + nvar
+ exist
, nparam
+1);
3074 value_oppose(M
->p
[0][1+nvar
+i
],
3075 V
->Vertex
->p
[i
][nparam
+1]);
3077 Polyhedron
*pos
, *neg
;
3078 value_set_si(M
->p
[0][0], 1);
3079 value_decrement(M
->p
[0][P
->Dimension
+1],
3080 M
->p
[0][P
->Dimension
+1]);
3081 neg
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
3082 value_set_si(f
, -1);
3083 Vector_Scale(M
->p
[0]+1, M
->p
[0]+1, f
,
3085 value_decrement(M
->p
[0][P
->Dimension
+1],
3086 M
->p
[0][P
->Dimension
+1]);
3087 value_decrement(M
->p
[0][P
->Dimension
+1],
3088 M
->p
[0][P
->Dimension
+1]);
3089 pos
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
3090 if (emptyQ(neg
) || emptyQ(pos
)) {
3091 Polyhedron_Free(pos
);
3092 Polyhedron_Free(neg
);
3095 Polyhedron_Free(pos
);
3096 value_increment(M
->p
[0][P
->Dimension
+1],
3097 M
->p
[0][P
->Dimension
+1]);
3098 pos
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
3100 fprintf(stderr
, "\nER: ParamVertex\n");
3101 #endif /* DEBUG_ER */
3103 EP
= enumerate_or(pos
, exist
, nparam
, options
);
3108 } END_FORALL_PVertex_in_ParamPolyhedron
;
3116 Polyhedron_Free(CEq
);
3120 Param_Polyhedron_Free(PP
);
3126 evalue
* barvinok_enumerate_pip(Polyhedron
*P
, unsigned exist
, unsigned nparam
,
3130 barvinok_options
*options
= barvinok_options_new_with_defaults();
3131 options
->MaxRays
= MaxRays
;
3132 E
= barvinok_enumerate_pip_with_options(P
, exist
, nparam
, options
);
3133 barvinok_options_free(options
);
3138 evalue
*barvinok_enumerate_pip_with_options(Polyhedron
*P
,
3139 unsigned exist
, unsigned nparam
, struct barvinok_options
*options
)
3144 evalue
*barvinok_enumerate_pip_with_options(Polyhedron
*P
,
3145 unsigned exist
, unsigned nparam
, struct barvinok_options
*options
)
3147 int nvar
= P
->Dimension
- exist
- nparam
;
3148 evalue
*EP
= evalue_zero();
3152 fprintf(stderr
, "\nER: PIP\n");
3153 #endif /* DEBUG_ER */
3155 Polyhedron
*D
= pip_projectout(P
, nvar
, exist
, nparam
);
3156 for (Q
= D
; Q
; Q
= N
) {
3160 exist
= Q
->Dimension
- nvar
- nparam
;
3161 E
= barvinok_enumerate_e_with_options(Q
, exist
, nparam
, options
);
3164 free_evalue_refs(E
);
3173 static bool is_single(Value
*row
, int pos
, int len
)
3175 return First_Non_Zero(row
, pos
) == -1 &&
3176 First_Non_Zero(row
+pos
+1, len
-pos
-1) == -1;
3179 static evalue
* barvinok_enumerate_e_r(Polyhedron
*P
,
3180 unsigned exist
, unsigned nparam
, barvinok_options
*options
);
3183 static int er_level
= 0;
3185 evalue
* barvinok_enumerate_e_with_options(Polyhedron
*P
,
3186 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
3188 fprintf(stderr
, "\nER: level %i\n", er_level
);
3190 Polyhedron_PrintConstraints(stderr
, P_VALUE_FMT
, P
);
3191 fprintf(stderr
, "\nE %d\nP %d\n", exist
, nparam
);
3193 P
= DomainConstraintSimplify(Polyhedron_Copy(P
), options
->MaxRays
);
3194 evalue
*EP
= barvinok_enumerate_e_r(P
, exist
, nparam
, options
);
3200 evalue
* barvinok_enumerate_e_with_options(Polyhedron
*P
,
3201 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
3203 P
= DomainConstraintSimplify(Polyhedron_Copy(P
), options
->MaxRays
);
3204 evalue
*EP
= barvinok_enumerate_e_r(P
, exist
, nparam
, options
);
3210 evalue
* barvinok_enumerate_e(Polyhedron
*P
, unsigned exist
, unsigned nparam
,
3214 barvinok_options
*options
= barvinok_options_new_with_defaults();
3215 options
->MaxRays
= MaxRays
;
3216 E
= barvinok_enumerate_e_with_options(P
, exist
, nparam
, options
);
3217 barvinok_options_free(options
);
3221 static evalue
* barvinok_enumerate_e_r(Polyhedron
*P
,
3222 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
3225 Polyhedron
*U
= Universe_Polyhedron(nparam
);
3226 evalue
*EP
= barvinok_enumerate_with_options(P
, U
, options
);
3227 //char *param_name[] = {"P", "Q", "R", "S", "T" };
3228 //print_evalue(stdout, EP, param_name);
3233 int nvar
= P
->Dimension
- exist
- nparam
;
3234 int len
= P
->Dimension
+ 2;
3237 POL_ENSURE_FACETS(P
);
3238 POL_ENSURE_VERTICES(P
);
3241 return evalue_zero();
3243 if (nvar
== 0 && nparam
== 0) {
3244 evalue
*EP
= evalue_zero();
3245 barvinok_count_with_options(P
, &EP
->x
.n
, options
);
3246 if (value_pos_p(EP
->x
.n
))
3247 value_set_si(EP
->x
.n
, 1);
3252 for (r
= 0; r
< P
->NbRays
; ++r
)
3253 if (value_zero_p(P
->Ray
[r
][0]) ||
3254 value_zero_p(P
->Ray
[r
][P
->Dimension
+1])) {
3256 for (i
= 0; i
< nvar
; ++i
)
3257 if (value_notzero_p(P
->Ray
[r
][i
+1]))
3261 for (i
= nvar
+ exist
; i
< nvar
+ exist
+ nparam
; ++i
)
3262 if (value_notzero_p(P
->Ray
[r
][i
+1]))
3264 if (i
>= nvar
+ exist
+ nparam
)
3267 if (r
< P
->NbRays
) {
3268 evalue
*EP
= evalue_zero();
3269 value_set_si(EP
->x
.n
, -1);
3274 for (r
= 0; r
< P
->NbEq
; ++r
)
3275 if ((first
= First_Non_Zero(P
->Constraint
[r
]+1+nvar
, exist
)) != -1)
3278 if (First_Non_Zero(P
->Constraint
[r
]+1+nvar
+first
+1,
3279 exist
-first
-1) != -1) {
3280 Polyhedron
*T
= rotate_along(P
, r
, nvar
, exist
, options
->MaxRays
);
3282 fprintf(stderr
, "\nER: Equality\n");
3283 #endif /* DEBUG_ER */
3284 evalue
*EP
= barvinok_enumerate_e_with_options(T
, exist
-1, nparam
,
3290 fprintf(stderr
, "\nER: Fixed\n");
3291 #endif /* DEBUG_ER */
3293 return barvinok_enumerate_e_with_options(P
, exist
-1, nparam
,
3296 Polyhedron
*T
= Polyhedron_Copy(P
);
3297 SwapColumns(T
, nvar
+1, nvar
+1+first
);
3298 evalue
*EP
= barvinok_enumerate_e_with_options(T
, exist
-1, nparam
,
3306 Vector
*row
= Vector_Alloc(len
);
3307 value_set_si(row
->p
[0], 1);
3312 enum constraint
* info
= new constraint
[exist
];
3313 for (int i
= 0; i
< exist
; ++i
) {
3315 for (int l
= P
->NbEq
; l
< P
->NbConstraints
; ++l
) {
3316 if (value_negz_p(P
->Constraint
[l
][nvar
+i
+1]))
3318 bool l_parallel
= is_single(P
->Constraint
[l
]+nvar
+1, i
, exist
);
3319 for (int u
= P
->NbEq
; u
< P
->NbConstraints
; ++u
) {
3320 if (value_posz_p(P
->Constraint
[u
][nvar
+i
+1]))
3322 bool lu_parallel
= l_parallel
||
3323 is_single(P
->Constraint
[u
]+nvar
+1, i
, exist
);
3324 value_oppose(f
, P
->Constraint
[u
][nvar
+i
+1]);
3325 Vector_Combine(P
->Constraint
[l
]+1, P
->Constraint
[u
]+1, row
->p
+1,
3326 f
, P
->Constraint
[l
][nvar
+i
+1], len
-1);
3327 if (!(info
[i
] & INDEPENDENT
)) {
3329 for (j
= 0; j
< exist
; ++j
)
3330 if (j
!= i
&& value_notzero_p(row
->p
[nvar
+j
+1]))
3333 //printf("independent: i: %d, l: %d, u: %d\n", i, l, u);
3334 info
[i
] = (constraint
)(info
[i
] | INDEPENDENT
);
3337 if (info
[i
] & ALL_POS
) {
3338 value_addto(row
->p
[len
-1], row
->p
[len
-1],
3339 P
->Constraint
[l
][nvar
+i
+1]);
3340 value_addto(row
->p
[len
-1], row
->p
[len
-1], f
);
3341 value_multiply(f
, f
, P
->Constraint
[l
][nvar
+i
+1]);
3342 value_subtract(row
->p
[len
-1], row
->p
[len
-1], f
);
3343 value_decrement(row
->p
[len
-1], row
->p
[len
-1]);
3344 ConstraintSimplify(row
->p
, row
->p
, len
, &f
);
3345 value_set_si(f
, -1);
3346 Vector_Scale(row
->p
+1, row
->p
+1, f
, len
-1);
3347 value_decrement(row
->p
[len
-1], row
->p
[len
-1]);
3348 Polyhedron
*T
= AddConstraints(row
->p
, 1, P
, options
->MaxRays
);
3350 //printf("not all_pos: i: %d, l: %d, u: %d\n", i, l, u);
3351 info
[i
] = (constraint
)(info
[i
] ^ ALL_POS
);
3353 //puts("pos remainder");
3354 //Polyhedron_Print(stdout, P_VALUE_FMT, T);
3357 if (!(info
[i
] & ONE_NEG
)) {
3359 negative_test_constraint(P
->Constraint
[l
],
3361 row
->p
, nvar
+i
, len
, &f
);
3362 oppose_constraint(row
->p
, len
, &f
);
3363 Polyhedron
*T
= AddConstraints(row
->p
, 1, P
,
3366 //printf("one_neg i: %d, l: %d, u: %d\n", i, l, u);
3367 info
[i
] = (constraint
)(info
[i
] | ONE_NEG
);
3369 //puts("neg remainder");
3370 //Polyhedron_Print(stdout, P_VALUE_FMT, T);
3372 } else if (!(info
[i
] & ROT_NEG
)) {
3373 if (parallel_constraints(P
->Constraint
[l
],
3375 row
->p
, nvar
, exist
)) {
3376 negative_test_constraint7(P
->Constraint
[l
],
3378 row
->p
, nvar
, exist
,
3380 oppose_constraint(row
->p
, len
, &f
);
3381 Polyhedron
*T
= AddConstraints(row
->p
, 1, P
,
3384 // printf("rot_neg i: %d, l: %d, u: %d\n", i, l, u);
3385 info
[i
] = (constraint
)(info
[i
] | ROT_NEG
);
3388 //puts("neg remainder");
3389 //Polyhedron_Print(stdout, P_VALUE_FMT, T);
3394 if (!(info
[i
] & ALL_POS
) && (info
[i
] & (ONE_NEG
| ROT_NEG
)))
3398 if (info
[i
] & ALL_POS
)
3405 for (int i = 0; i < exist; ++i)
3406 printf("%i: %i\n", i, info[i]);
3408 for (int i
= 0; i
< exist
; ++i
)
3409 if (info
[i
] & ALL_POS
) {
3411 fprintf(stderr
, "\nER: Positive\n");
3412 #endif /* DEBUG_ER */
3414 // Maybe we should chew off some of the fat here
3415 Matrix
*M
= Matrix_Alloc(P
->Dimension
, P
->Dimension
+1);
3416 for (int j
= 0; j
< P
->Dimension
; ++j
)
3417 value_set_si(M
->p
[j
][j
+ (j
>= i
+nvar
)], 1);
3418 Polyhedron
*T
= Polyhedron_Image(P
, M
, options
->MaxRays
);
3420 evalue
*EP
= barvinok_enumerate_e_with_options(T
, exist
-1, nparam
,
3428 for (int i
= 0; i
< exist
; ++i
)
3429 if (info
[i
] & ONE_NEG
) {
3431 fprintf(stderr
, "\nER: Negative\n");
3432 #endif /* DEBUG_ER */
3437 return barvinok_enumerate_e_with_options(P
, exist
-1, nparam
,
3440 Polyhedron
*T
= Polyhedron_Copy(P
);
3441 SwapColumns(T
, nvar
+1, nvar
+1+i
);
3442 evalue
*EP
= barvinok_enumerate_e_with_options(T
, exist
-1, nparam
,
3448 for (int i
= 0; i
< exist
; ++i
)
3449 if (info
[i
] & ROT_NEG
) {
3451 fprintf(stderr
, "\nER: Rotate\n");
3452 #endif /* DEBUG_ER */
3456 Polyhedron
*T
= rotate_along(P
, r
, nvar
, exist
, options
->MaxRays
);
3457 evalue
*EP
= barvinok_enumerate_e_with_options(T
, exist
-1, nparam
,
3462 for (int i
= 0; i
< exist
; ++i
)
3463 if (info
[i
] & INDEPENDENT
) {
3464 Polyhedron
*pos
, *neg
;
3466 /* Find constraint again and split off negative part */
3468 if (SplitOnVar(P
, i
, nvar
, exist
, options
->MaxRays
,
3469 row
, f
, true, &pos
, &neg
)) {
3471 fprintf(stderr
, "\nER: Split\n");
3472 #endif /* DEBUG_ER */
3475 barvinok_enumerate_e_with_options(neg
, exist
-1, nparam
, options
);
3477 barvinok_enumerate_e_with_options(pos
, exist
, nparam
, options
);
3479 free_evalue_refs(E
);
3481 Polyhedron_Free(neg
);
3482 Polyhedron_Free(pos
);
3496 EP
= enumerate_line(P
, exist
, nparam
, options
);
3500 EP
= barvinok_enumerate_pip_with_options(P
, exist
, nparam
, options
);
3504 EP
= enumerate_redundant_ray(P
, exist
, nparam
, options
);
3508 EP
= enumerate_sure(P
, exist
, nparam
, options
);
3512 EP
= enumerate_ray(P
, exist
, nparam
, options
);
3516 EP
= enumerate_sure2(P
, exist
, nparam
, options
);
3520 F
= unfringe(P
, options
->MaxRays
);
3521 if (!PolyhedronIncludes(F
, P
)) {
3523 fprintf(stderr
, "\nER: Fringed\n");
3524 #endif /* DEBUG_ER */
3525 EP
= barvinok_enumerate_e_with_options(F
, exist
, nparam
, options
);
3532 EP
= enumerate_vd(&P
, exist
, nparam
, options
);
3537 EP
= enumerate_sum(P
, exist
, nparam
, options
);
3544 Polyhedron
*pos
, *neg
;
3545 for (i
= 0; i
< exist
; ++i
)
3546 if (SplitOnVar(P
, i
, nvar
, exist
, options
->MaxRays
,
3547 row
, f
, false, &pos
, &neg
))
3553 EP
= enumerate_or(pos
, exist
, nparam
, options
);
3566 * remove equalities that require a "compression" of the parameters
3568 static Polyhedron
*remove_more_equalities(Polyhedron
*P
, unsigned nparam
,
3569 Matrix
**CP
, unsigned MaxRays
)
3572 remove_all_equalities(&P
, NULL
, CP
, NULL
, nparam
, MaxRays
);
3579 static gen_fun
*series(Polyhedron
*P
, unsigned nparam
, barvinok_options
*options
)
3589 assert(!Polyhedron_is_unbounded(P
, nparam
, options
->MaxRays
));
3590 assert(P
->NbBid
== 0);
3591 assert(Polyhedron_has_revlex_positive_rays(P
, nparam
));
3593 P
= remove_more_equalities(P
, nparam
, &CP
, options
->MaxRays
);
3594 assert(P
->NbEq
== 0);
3596 nparam
= CP
->NbColumns
-1;
3601 barvinok_count_with_options(P
, &c
, options
);
3602 gf
= new gen_fun(c
);
3606 red
= gf_base::create(Polyhedron_Project(P
, nparam
),
3607 P
->Dimension
, nparam
, options
);
3608 POL_ENSURE_VERTICES(P
);
3609 red
->start_gf(P
, options
);
3621 gen_fun
* barvinok_series_with_options(Polyhedron
*P
, Polyhedron
* C
,
3622 barvinok_options
*options
)
3625 unsigned nparam
= C
->Dimension
;
3628 CA
= align_context(C
, P
->Dimension
, options
->MaxRays
);
3629 P
= DomainIntersection(P
, CA
, options
->MaxRays
);
3630 Polyhedron_Free(CA
);
3632 gf
= series(P
, nparam
, options
);
3637 gen_fun
* barvinok_series(Polyhedron
*P
, Polyhedron
* C
, unsigned MaxRays
)
3640 barvinok_options
*options
= barvinok_options_new_with_defaults();
3641 options
->MaxRays
= MaxRays
;
3642 gf
= barvinok_series_with_options(P
, C
, options
);
3643 barvinok_options_free(options
);
3647 static Polyhedron
*skew_into_positive_orthant(Polyhedron
*D
, unsigned nparam
,
3653 for (Polyhedron
*P
= D
; P
; P
= P
->next
) {
3654 POL_ENSURE_VERTICES(P
);
3655 assert(!Polyhedron_is_unbounded(P
, nparam
, MaxRays
));
3656 assert(P
->NbBid
== 0);
3657 assert(Polyhedron_has_positive_rays(P
, nparam
));
3659 for (int r
= 0; r
< P
->NbRays
; ++r
) {
3660 if (value_notzero_p(P
->Ray
[r
][P
->Dimension
+1]))
3662 for (int i
= 0; i
< nparam
; ++i
) {
3664 if (value_posz_p(P
->Ray
[r
][i
+1]))
3667 M
= Matrix_Alloc(D
->Dimension
+1, D
->Dimension
+1);
3668 for (int i
= 0; i
< D
->Dimension
+1; ++i
)
3669 value_set_si(M
->p
[i
][i
], 1);
3671 Inner_Product(P
->Ray
[r
]+1, M
->p
[i
], D
->Dimension
+1, &tmp
);
3672 if (value_posz_p(tmp
))
3675 for (j
= P
->Dimension
- nparam
; j
< P
->Dimension
; ++j
)
3676 if (value_pos_p(P
->Ray
[r
][j
+1]))
3678 assert(j
< P
->Dimension
);
3679 value_pdivision(tmp
, P
->Ray
[r
][j
+1], P
->Ray
[r
][i
+1]);
3680 value_subtract(M
->p
[i
][j
], M
->p
[i
][j
], tmp
);
3686 D
= DomainImage(D
, M
, MaxRays
);
3692 gen_fun
* barvinok_enumerate_union_series_with_options(Polyhedron
*D
, Polyhedron
* C
,
3693 barvinok_options
*options
)
3695 Polyhedron
*conv
, *D2
;
3697 gen_fun
*gf
= NULL
, *gf2
;
3698 unsigned nparam
= C
->Dimension
;
3703 CA
= align_context(C
, D
->Dimension
, options
->MaxRays
);
3704 D
= DomainIntersection(D
, CA
, options
->MaxRays
);
3705 Polyhedron_Free(CA
);
3707 D2
= skew_into_positive_orthant(D
, nparam
, options
->MaxRays
);
3708 for (Polyhedron
*P
= D2
; P
; P
= P
->next
) {
3709 assert(P
->Dimension
== D2
->Dimension
);
3712 P_gf
= series(Polyhedron_Copy(P
), nparam
, options
);
3716 gf
->add_union(P_gf
, options
);
3720 /* we actually only need the convex union of the parameter space
3721 * but the reducer classes currently expect a polyhedron in
3722 * the combined space
3724 Polyhedron_Free(gf
->context
);
3725 gf
->context
= DomainConvex(D2
, options
->MaxRays
);
3727 gf2
= gf
->summate(D2
->Dimension
- nparam
, options
);
3736 gen_fun
* barvinok_enumerate_union_series(Polyhedron
*D
, Polyhedron
* C
,
3740 barvinok_options
*options
= barvinok_options_new_with_defaults();
3741 options
->MaxRays
= MaxRays
;
3742 gf
= barvinok_enumerate_union_series_with_options(D
, C
, options
);
3743 barvinok_options_free(options
);
3747 evalue
* barvinok_enumerate_union(Polyhedron
*D
, Polyhedron
* C
, unsigned MaxRays
)
3750 gen_fun
*gf
= barvinok_enumerate_union_series(D
, C
, MaxRays
);