8 #include <NTL/mat_ZZ.h>
10 #include <barvinok/util.h>
12 #include <polylib/polylibgmp.h>
13 #include <barvinok/evalue.h>
17 #include <barvinok/barvinok.h>
18 #include <barvinok/genfun.h>
19 #include <barvinok/options.h>
20 #include <barvinok/sample.h>
21 #include "conversion.h"
22 #include "decomposer.h"
23 #include "lattice_point.h"
24 #include "reduce_domain.h"
25 #include "genfun_constructor.h"
26 #include "remove_equalities.h"
37 using std::ostringstream
;
39 #define ALLOC(t,p) p = (t*)malloc(sizeof(*p))
41 static void rays(mat_ZZ
& r
, Polyhedron
*C
)
43 unsigned dim
= C
->NbRays
- 1; /* don't count zero vertex */
44 assert(C
->NbRays
- 1 == C
->Dimension
);
49 for (i
= 0, c
= 0; i
< dim
; ++i
)
50 if (value_zero_p(C
->Ray
[i
][dim
+1])) {
51 for (int j
= 0; j
< dim
; ++j
) {
52 value2zz(C
->Ray
[i
][j
+1], tmp
);
65 dpoly_n(int d
, ZZ
& degree_0
, ZZ
& degree_1
, int offset
= 0) {
69 zz2value(degree_0
, d0
);
70 zz2value(degree_1
, d1
);
71 coeff
= Matrix_Alloc(d
+1, d
+1+1);
72 value_set_si(coeff
->p
[0][0], 1);
73 value_set_si(coeff
->p
[0][d
+1], 1);
74 for (int i
= 1; i
<= d
; ++i
) {
75 value_multiply(coeff
->p
[i
][0], coeff
->p
[i
-1][0], d0
);
76 Vector_Combine(coeff
->p
[i
-1], coeff
->p
[i
-1]+1, coeff
->p
[i
]+1,
78 value_set_si(coeff
->p
[i
][d
+1], i
);
79 value_multiply(coeff
->p
[i
][d
+1], coeff
->p
[i
][d
+1], coeff
->p
[i
-1][d
+1]);
80 value_decrement(d0
, d0
);
85 void div(dpoly
& d
, Vector
*count
, ZZ
& sign
) {
86 int len
= coeff
->NbRows
;
87 Matrix
* c
= Matrix_Alloc(coeff
->NbRows
, coeff
->NbColumns
);
90 for (int i
= 0; i
< len
; ++i
) {
91 Vector_Copy(coeff
->p
[i
], c
->p
[i
], len
+1);
92 for (int j
= 1; j
<= i
; ++j
) {
93 zz2value(d
.coeff
[j
], tmp
);
94 value_multiply(tmp
, tmp
, c
->p
[i
][len
]);
95 value_oppose(tmp
, tmp
);
96 Vector_Combine(c
->p
[i
], c
->p
[i
-j
], c
->p
[i
],
97 c
->p
[i
-j
][len
], tmp
, len
);
98 value_multiply(c
->p
[i
][len
], c
->p
[i
][len
], c
->p
[i
-j
][len
]);
100 zz2value(d
.coeff
[0], tmp
);
101 value_multiply(c
->p
[i
][len
], c
->p
[i
][len
], tmp
);
104 value_set_si(tmp
, -1);
105 Vector_Scale(c
->p
[len
-1], count
->p
, tmp
, len
);
106 value_assign(count
->p
[len
], c
->p
[len
-1][len
]);
108 Vector_Copy(c
->p
[len
-1], count
->p
, len
+1);
109 Vector_Normalize(count
->p
, len
+1);
115 const int MAX_TRY
=10;
117 * Searches for a vector that is not orthogonal to any
118 * of the rays in rays.
120 static void nonorthog(mat_ZZ
& rays
, vec_ZZ
& lambda
)
122 int dim
= rays
.NumCols();
124 lambda
.SetLength(dim
);
128 for (int i
= 2; !found
&& i
<= 50*dim
; i
+=4) {
129 for (int j
= 0; j
< MAX_TRY
; ++j
) {
130 for (int k
= 0; k
< dim
; ++k
) {
131 int r
= random_int(i
)+2;
132 int v
= (2*(r
%2)-1) * (r
>> 1);
136 for (; k
< rays
.NumRows(); ++k
)
137 if (lambda
* rays
[k
] == 0)
139 if (k
== rays
.NumRows()) {
148 static void add_rays(mat_ZZ
& rays
, Polyhedron
*i
, int *r
, int nvar
= -1,
151 unsigned dim
= i
->Dimension
;
154 for (int k
= 0; k
< i
->NbRays
; ++k
) {
155 if (!value_zero_p(i
->Ray
[k
][dim
+1]))
157 if (!all
&& nvar
!= dim
&& First_Non_Zero(i
->Ray
[k
]+1, nvar
) == -1)
159 values2zz(i
->Ray
[k
]+1, rays
[(*r
)++], nvar
);
163 static void mask_r(Matrix
*f
, int nr
, Vector
*lcm
, int p
, Vector
*val
, evalue
*ev
)
165 unsigned nparam
= lcm
->Size
;
168 Vector
* prod
= Vector_Alloc(f
->NbRows
);
169 Matrix_Vector_Product(f
, val
->p
, prod
->p
);
171 for (int i
= 0; i
< nr
; ++i
) {
172 value_modulus(prod
->p
[i
], prod
->p
[i
], f
->p
[i
][nparam
+1]);
173 isint
&= value_zero_p(prod
->p
[i
]);
175 value_set_si(ev
->d
, 1);
177 value_set_si(ev
->x
.n
, isint
);
184 if (value_one_p(lcm
->p
[p
]))
185 mask_r(f
, nr
, lcm
, p
+1, val
, ev
);
187 value_assign(tmp
, lcm
->p
[p
]);
188 value_set_si(ev
->d
, 0);
189 ev
->x
.p
= new_enode(periodic
, VALUE_TO_INT(tmp
), p
+1);
191 value_decrement(tmp
, tmp
);
192 value_assign(val
->p
[p
], tmp
);
193 mask_r(f
, nr
, lcm
, p
+1, val
, &ev
->x
.p
->arr
[VALUE_TO_INT(tmp
)]);
194 } while (value_pos_p(tmp
));
200 static void mask(Matrix
*f
, evalue
*factor
)
202 int nr
= f
->NbRows
, nc
= f
->NbColumns
;
205 for (n
= 0; n
< nr
&& value_notzero_p(f
->p
[n
][nc
-1]); ++n
)
206 if (value_notone_p(f
->p
[n
][nc
-1]) &&
207 value_notmone_p(f
->p
[n
][nc
-1]))
221 value_set_si(EV
.x
.n
, 1);
223 for (n
= 0; n
< nr
; ++n
) {
224 value_assign(m
, f
->p
[n
][nc
-1]);
225 if (value_one_p(m
) || value_mone_p(m
))
228 int j
= normal_mod(f
->p
[n
], nc
-1, &m
);
230 free_evalue_refs(factor
);
231 value_init(factor
->d
);
232 evalue_set_si(factor
, 0, 1);
236 values2zz(f
->p
[n
], row
, nc
-1);
239 if (j
< (nc
-1)-1 && row
[j
] > g
/2) {
240 for (int k
= j
; k
< (nc
-1); ++k
)
246 value_set_si(EP
.d
, 0);
247 EP
.x
.p
= new_enode(relation
, 2, 0);
248 value_clear(EP
.x
.p
->arr
[1].d
);
249 EP
.x
.p
->arr
[1] = *factor
;
250 evalue
*ev
= &EP
.x
.p
->arr
[0];
251 value_set_si(ev
->d
, 0);
252 ev
->x
.p
= new_enode(fractional
, 3, -1);
253 evalue_set_si(&ev
->x
.p
->arr
[1], 0, 1);
254 evalue_set_si(&ev
->x
.p
->arr
[2], 1, 1);
255 evalue
*E
= multi_monom(row
);
256 value_assign(EV
.d
, m
);
258 value_clear(ev
->x
.p
->arr
[0].d
);
259 ev
->x
.p
->arr
[0] = *E
;
265 free_evalue_refs(&EV
);
271 static void mask(Matrix
*f
, evalue
*factor
)
273 int nr
= f
->NbRows
, nc
= f
->NbColumns
;
276 for (n
= 0; n
< nr
&& value_notzero_p(f
->p
[n
][nc
-1]); ++n
)
277 if (value_notone_p(f
->p
[n
][nc
-1]) &&
278 value_notmone_p(f
->p
[n
][nc
-1]))
286 unsigned np
= nc
- 2;
287 Vector
*lcm
= Vector_Alloc(np
);
288 Vector
*val
= Vector_Alloc(nc
);
289 Vector_Set(val
->p
, 0, nc
);
290 value_set_si(val
->p
[np
], 1);
291 Vector_Set(lcm
->p
, 1, np
);
292 for (n
= 0; n
< nr
; ++n
) {
293 if (value_one_p(f
->p
[n
][nc
-1]) ||
294 value_mone_p(f
->p
[n
][nc
-1]))
296 for (int j
= 0; j
< np
; ++j
)
297 if (value_notzero_p(f
->p
[n
][j
])) {
298 Gcd(f
->p
[n
][j
], f
->p
[n
][nc
-1], &tmp
);
299 value_division(tmp
, f
->p
[n
][nc
-1], tmp
);
300 value_lcm(tmp
, lcm
->p
[j
], &lcm
->p
[j
]);
305 mask_r(f
, nr
, lcm
, 0, val
, &EP
);
310 free_evalue_refs(&EP
);
314 /* This structure encodes the power of the term in a rational generating function.
316 * Either E == NULL or constant = 0
317 * If E != NULL, then the power is E
318 * If E == NULL, then the power is coeff * param[pos] + constant
327 /* Returns the power of (t+1) in the term of a rational generating function,
328 * i.e., the scalar product of the actual lattice point and lambda.
329 * The lattice point is the unique lattice point in the fundamental parallelepiped
330 * of the unimodual cone i shifted to the parametric vertex V.
332 * PD is the parameter domain, which, if != NULL, may be used to simply the
333 * resulting expression.
335 * The result is returned in term.
338 Param_Vertices
* V
, Polyhedron
*i
, vec_ZZ
& lambda
, term_info
* term
,
341 unsigned nparam
= V
->Vertex
->NbColumns
- 2;
342 unsigned dim
= i
->Dimension
;
344 vertex
.SetDims(V
->Vertex
->NbRows
, nparam
+1);
348 value_set_si(lcm
, 1);
349 for (int j
= 0; j
< V
->Vertex
->NbRows
; ++j
) {
350 value_lcm(lcm
, V
->Vertex
->p
[j
][nparam
+1], &lcm
);
352 if (value_notone_p(lcm
)) {
353 Matrix
* mv
= Matrix_Alloc(dim
, nparam
+1);
354 for (int j
= 0 ; j
< dim
; ++j
) {
355 value_division(tmp
, lcm
, V
->Vertex
->p
[j
][nparam
+1]);
356 Vector_Scale(V
->Vertex
->p
[j
], mv
->p
[j
], tmp
, nparam
+1);
359 term
->E
= lattice_point(i
, lambda
, mv
, lcm
, PD
);
367 for (int i
= 0; i
< V
->Vertex
->NbRows
; ++i
) {
368 assert(value_one_p(V
->Vertex
->p
[i
][nparam
+1])); // for now
369 values2zz(V
->Vertex
->p
[i
], vertex
[i
], nparam
+1);
373 num
= lambda
* vertex
;
377 for (int j
= 0; j
< nparam
; ++j
)
383 term
->E
= multi_monom(num
);
387 term
->constant
= num
[nparam
];
390 term
->coeff
= num
[p
];
398 struct counter
: public np_base
{
408 counter(unsigned dim
) : np_base(dim
) {
409 rays
.SetDims(dim
, dim
);
414 virtual void start(Polyhedron
*P
, barvinok_options
*options
);
420 virtual void handle_polar(Polyhedron
*C
, Value
*vertex
, QQ c
);
421 virtual void get_count(Value
*result
) {
422 assert(value_one_p(&count
[0]._mp_den
));
423 value_assign(*result
, &count
[0]._mp_num
);
427 struct OrthogonalException
{} Orthogonal
;
429 void counter::handle_polar(Polyhedron
*C
, Value
*V
, QQ c
)
432 add_rays(rays
, C
, &r
);
433 for (int k
= 0; k
< dim
; ++k
) {
434 if (lambda
* rays
[k
] == 0)
439 assert(c
.n
== 1 || c
.n
== -1);
442 lattice_point(V
, C
, vertex
);
443 num
= vertex
* lambda
;
445 normalize(sign
, num
, den
);
448 dpoly
n(dim
, den
[0], 1);
449 for (int k
= 1; k
< dim
; ++k
) {
450 dpoly
fact(dim
, den
[k
], 1);
453 d
.div(n
, count
, sign
);
456 void counter::start(Polyhedron
*P
, barvinok_options
*options
)
460 randomvector(P
, lambda
, dim
);
461 np_base::start(P
, options
);
463 } catch (OrthogonalException
&e
) {
464 mpq_set_si(count
, 0, 0);
469 struct bfe_term
: public bfc_term_base
{
470 vector
<evalue
*> factors
;
472 bfe_term(int len
) : bfc_term_base(len
) {
476 for (int i
= 0; i
< factors
.size(); ++i
) {
479 free_evalue_refs(factors
[i
]);
485 static void print_int_vector(int *v
, int len
, char *name
)
487 cerr
<< name
<< endl
;
488 for (int j
= 0; j
< len
; ++j
) {
494 static void print_bfc_terms(mat_ZZ
& factors
, bfc_vec
& v
)
497 cerr
<< "factors" << endl
;
498 cerr
<< factors
<< endl
;
499 for (int i
= 0; i
< v
.size(); ++i
) {
500 cerr
<< "term: " << i
<< endl
;
501 print_int_vector(v
[i
]->powers
, factors
.NumRows(), "powers");
502 cerr
<< "terms" << endl
;
503 cerr
<< v
[i
]->terms
<< endl
;
504 bfc_term
* bfct
= static_cast<bfc_term
*>(v
[i
]);
505 cerr
<< bfct
->c
<< endl
;
509 static void print_bfe_terms(mat_ZZ
& factors
, bfc_vec
& v
)
512 cerr
<< "factors" << endl
;
513 cerr
<< factors
<< endl
;
514 for (int i
= 0; i
< v
.size(); ++i
) {
515 cerr
<< "term: " << i
<< endl
;
516 print_int_vector(v
[i
]->powers
, factors
.NumRows(), "powers");
517 cerr
<< "terms" << endl
;
518 cerr
<< v
[i
]->terms
<< endl
;
519 bfe_term
* bfet
= static_cast<bfe_term
*>(v
[i
]);
520 for (int j
= 0; j
< v
[i
]->terms
.NumRows(); ++j
) {
521 char * test
[] = {"a", "b"};
522 print_evalue(stderr
, bfet
->factors
[j
], test
);
523 fprintf(stderr
, "\n");
528 struct bfcounter
: public bfcounter_base
{
531 bfcounter(unsigned dim
) : bfcounter_base(dim
) {
538 virtual void base(mat_ZZ
& factors
, bfc_vec
& v
);
539 virtual void get_count(Value
*result
) {
540 assert(value_one_p(&count
[0]._mp_den
));
541 value_assign(*result
, &count
[0]._mp_num
);
545 void bfcounter::base(mat_ZZ
& factors
, bfc_vec
& v
)
547 unsigned nf
= factors
.NumRows();
549 for (int i
= 0; i
< v
.size(); ++i
) {
550 bfc_term
* bfct
= static_cast<bfc_term
*>(v
[i
]);
552 // factor is always positive, so we always
554 for (int k
= 0; k
< nf
; ++k
)
555 total_power
+= v
[i
]->powers
[k
];
558 for (j
= 0; j
< nf
; ++j
)
559 if (v
[i
]->powers
[j
] > 0)
562 dpoly
D(total_power
, factors
[j
][0], 1);
563 for (int k
= 1; k
< v
[i
]->powers
[j
]; ++k
) {
564 dpoly
fact(total_power
, factors
[j
][0], 1);
568 for (int k
= 0; k
< v
[i
]->powers
[j
]; ++k
) {
569 dpoly
fact(total_power
, factors
[j
][0], 1);
573 for (int k
= 0; k
< v
[i
]->terms
.NumRows(); ++k
) {
574 dpoly
n(total_power
, v
[i
]->terms
[k
][0]);
575 mpq_set_si(tcount
, 0, 1);
576 n
.div(D
, tcount
, one
);
578 bfct
->c
[k
].n
= -bfct
->c
[k
].n
;
579 zz2value(bfct
->c
[k
].n
, tn
);
580 zz2value(bfct
->c
[k
].d
, td
);
582 mpz_mul(mpq_numref(tcount
), mpq_numref(tcount
), tn
);
583 mpz_mul(mpq_denref(tcount
), mpq_denref(tcount
), td
);
584 mpq_canonicalize(tcount
);
585 mpq_add(count
, count
, tcount
);
592 /* Check whether the polyhedron is unbounded and if so,
593 * check whether it has any (and therefore an infinite number of)
595 * If one of the vertices is integer, then we are done.
596 * Otherwise, transform the polyhedron such that one of the rays
597 * is the first unit vector and cut it off at a height that ensures
598 * that if the whole polyhedron has any points, then the remaining part
599 * has integer points. In particular we add the largest coefficient
600 * of a ray to the highest vertex (rounded up).
602 static bool Polyhedron_is_infinite(Polyhedron
*P
, Value
* result
,
603 barvinok_options
*options
)
615 for (; r
< P
->NbRays
; ++r
)
616 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1]))
618 if (P
->NbBid
== 0 && r
== P
->NbRays
)
624 sample
= Polyhedron_Sample(P
, options
);
626 value_set_si(*result
, 0);
628 value_set_si(*result
, -1);
634 for (int i
= 0; i
< P
->NbRays
; ++i
)
635 if (value_one_p(P
->Ray
[i
][1+P
->Dimension
])) {
636 value_set_si(*result
, -1);
641 v
= Vector_Alloc(P
->Dimension
+1);
642 Vector_Gcd(P
->Ray
[r
]+1, P
->Dimension
, &g
);
643 Vector_AntiScale(P
->Ray
[r
]+1, v
->p
, g
, P
->Dimension
+1);
644 M
= unimodular_complete(v
);
645 value_set_si(M
->p
[P
->Dimension
][P
->Dimension
], 1);
648 P
= Polyhedron_Preimage(P
, M2
, 0);
657 value_set_si(size
, 0);
659 for (int i
= 0; i
< P
->NbBid
; ++i
) {
660 value_absolute(tmp
, P
->Ray
[i
][1]);
661 if (value_gt(tmp
, size
))
662 value_assign(size
, tmp
);
664 for (int i
= P
->NbBid
; i
< P
->NbRays
; ++i
) {
665 if (value_zero_p(P
->Ray
[i
][P
->Dimension
+1])) {
666 if (value_gt(P
->Ray
[i
][1], size
))
667 value_assign(size
, P
->Ray
[i
][1]);
670 mpz_cdiv_q(tmp
, P
->Ray
[i
][1], P
->Ray
[i
][P
->Dimension
+1]);
671 if (first
|| value_gt(tmp
, offset
)) {
672 value_assign(offset
, tmp
);
676 value_addto(offset
, offset
, size
);
680 v
= Vector_Alloc(P
->Dimension
+2);
681 value_set_si(v
->p
[0], 1);
682 value_set_si(v
->p
[1], -1);
683 value_assign(v
->p
[1+P
->Dimension
], offset
);
684 R
= AddConstraints(v
->p
, 1, P
, options
->MaxRays
);
692 barvinok_count_with_options(P
, &c
, options
);
695 value_set_si(*result
, 0);
697 value_set_si(*result
, -1);
703 typedef Polyhedron
* Polyhedron_p
;
705 static void barvinok_count_f(Polyhedron
*P
, Value
* result
,
706 barvinok_options
*options
);
708 void barvinok_count_with_options(Polyhedron
*P
, Value
* result
,
709 struct barvinok_options
*options
)
714 bool infinite
= false;
717 value_set_si(*result
, 0);
723 P
= remove_equalities(P
);
724 P
= DomainConstraintSimplify(P
, options
->MaxRays
);
728 } while (!emptyQ(P
) && P
->NbEq
!= 0);
731 value_set_si(*result
, 0);
736 if (Polyhedron_is_infinite(P
, result
, options
)) {
741 if (P
->Dimension
== 0) {
742 /* Test whether the constraints are satisfied */
743 POL_ENSURE_VERTICES(P
);
744 value_set_si(*result
, !emptyQ(P
));
749 Q
= Polyhedron_Factor(P
, 0, options
->MaxRays
);
757 barvinok_count_f(P
, result
, options
);
758 if (value_neg_p(*result
))
760 if (Q
&& P
->next
&& value_notzero_p(*result
)) {
764 for (Q
= P
->next
; Q
; Q
= Q
->next
) {
765 barvinok_count_f(Q
, &factor
, options
);
766 if (value_neg_p(factor
)) {
769 } else if (Q
->next
&& value_zero_p(factor
)) {
770 value_set_si(*result
, 0);
773 value_multiply(*result
, *result
, factor
);
782 value_set_si(*result
, -1);
785 void barvinok_count(Polyhedron
*P
, Value
* result
, unsigned NbMaxCons
)
787 barvinok_options
*options
= barvinok_options_new_with_defaults();
788 options
->MaxRays
= NbMaxCons
;
789 barvinok_count_with_options(P
, result
, options
);
793 static void barvinok_count_f(Polyhedron
*P
, Value
* result
,
794 barvinok_options
*options
)
797 value_set_si(*result
, 0);
801 if (P
->Dimension
== 1)
802 return Line_Length(P
, result
);
804 int c
= P
->NbConstraints
;
805 POL_ENSURE_FACETS(P
);
806 if (c
!= P
->NbConstraints
|| P
->NbEq
!= 0)
807 return barvinok_count_with_options(P
, result
, options
);
809 POL_ENSURE_VERTICES(P
);
811 if (Polyhedron_is_infinite(P
, result
, options
))
815 if (options
->incremental_specialization
== 2)
816 cnt
= new bfcounter(P
->Dimension
);
817 else if (options
->incremental_specialization
== 1)
818 cnt
= new icounter(P
->Dimension
);
820 cnt
= new counter(P
->Dimension
);
821 cnt
->start(P
, options
);
823 cnt
->get_count(result
);
827 static void uni_polynom(int param
, Vector
*c
, evalue
*EP
)
829 unsigned dim
= c
->Size
-2;
831 value_set_si(EP
->d
,0);
832 EP
->x
.p
= new_enode(polynomial
, dim
+1, param
+1);
833 for (int j
= 0; j
<= dim
; ++j
)
834 evalue_set(&EP
->x
.p
->arr
[j
], c
->p
[j
], c
->p
[dim
+1]);
837 static void multi_polynom(Vector
*c
, evalue
* X
, evalue
*EP
)
839 unsigned dim
= c
->Size
-2;
843 evalue_set(&EC
, c
->p
[dim
], c
->p
[dim
+1]);
846 evalue_set(EP
, c
->p
[dim
], c
->p
[dim
+1]);
848 for (int i
= dim
-1; i
>= 0; --i
) {
850 value_assign(EC
.x
.n
, c
->p
[i
]);
853 free_evalue_refs(&EC
);
856 Polyhedron
*unfringe (Polyhedron
*P
, unsigned MaxRays
)
858 int len
= P
->Dimension
+2;
859 Polyhedron
*T
, *R
= P
;
862 Vector
*row
= Vector_Alloc(len
);
863 value_set_si(row
->p
[0], 1);
865 R
= DomainConstraintSimplify(Polyhedron_Copy(P
), MaxRays
);
867 Matrix
*M
= Matrix_Alloc(2, len
-1);
868 value_set_si(M
->p
[1][len
-2], 1);
869 for (int v
= 0; v
< P
->Dimension
; ++v
) {
870 value_set_si(M
->p
[0][v
], 1);
871 Polyhedron
*I
= Polyhedron_Image(R
, M
, 2+1);
872 value_set_si(M
->p
[0][v
], 0);
873 for (int r
= 0; r
< I
->NbConstraints
; ++r
) {
874 if (value_zero_p(I
->Constraint
[r
][0]))
876 if (value_zero_p(I
->Constraint
[r
][1]))
878 if (value_one_p(I
->Constraint
[r
][1]))
880 if (value_mone_p(I
->Constraint
[r
][1]))
882 value_absolute(g
, I
->Constraint
[r
][1]);
883 Vector_Set(row
->p
+1, 0, len
-2);
884 value_division(row
->p
[1+v
], I
->Constraint
[r
][1], g
);
885 mpz_fdiv_q(row
->p
[len
-1], I
->Constraint
[r
][2], g
);
887 R
= AddConstraints(row
->p
, 1, R
, MaxRays
);
899 /* this procedure may have false negatives */
900 static bool Polyhedron_is_infinite_param(Polyhedron
*P
, unsigned nparam
)
903 for (r
= 0; r
< P
->NbRays
; ++r
) {
904 if (!value_zero_p(P
->Ray
[r
][0]) &&
905 !value_zero_p(P
->Ray
[r
][P
->Dimension
+1]))
907 if (First_Non_Zero(P
->Ray
[r
]+1+P
->Dimension
-nparam
, nparam
) == -1)
913 /* Check whether all rays point in the positive directions
916 static bool Polyhedron_has_positive_rays(Polyhedron
*P
, unsigned nparam
)
919 for (r
= 0; r
< P
->NbRays
; ++r
)
920 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1])) {
922 for (i
= P
->Dimension
- nparam
; i
< P
->Dimension
; ++i
)
923 if (value_neg_p(P
->Ray
[r
][i
+1]))
929 typedef evalue
* evalue_p
;
931 struct enumerator
: public polar_decomposer
{
945 enumerator(Polyhedron
*P
, unsigned dim
, unsigned nbV
) {
949 randomvector(P
, lambda
, dim
);
950 rays
.SetDims(dim
, dim
);
952 c
= Vector_Alloc(dim
+2);
954 vE
= new evalue_p
[nbV
];
955 for (int j
= 0; j
< nbV
; ++j
)
961 void decompose_at(Param_Vertices
*V
, int _i
, barvinok_options
*options
) {
962 Polyhedron
*C
= supporting_cone_p(P
, V
);
967 value_init(vE
[_i
]->d
);
968 evalue_set_si(vE
[_i
], 0, 1);
970 decompose(C
, options
);
977 for (int j
= 0; j
< nbV
; ++j
)
979 free_evalue_refs(vE
[j
]);
985 virtual void handle_polar(Polyhedron
*P
, int sign
);
988 void enumerator::handle_polar(Polyhedron
*C
, int s
)
991 assert(C
->NbRays
-1 == dim
);
992 add_rays(rays
, C
, &r
);
993 for (int k
= 0; k
< dim
; ++k
) {
994 if (lambda
* rays
[k
] == 0)
1000 lattice_point(V
, C
, lambda
, &num
, 0);
1001 den
= rays
* lambda
;
1002 normalize(sign
, num
.constant
, den
);
1004 dpoly
n(dim
, den
[0], 1);
1005 for (int k
= 1; k
< dim
; ++k
) {
1006 dpoly
fact(dim
, den
[k
], 1);
1009 if (num
.E
!= NULL
) {
1010 ZZ
one(INIT_VAL
, 1);
1011 dpoly_n
d(dim
, num
.constant
, one
);
1014 multi_polynom(c
, num
.E
, &EV
);
1016 free_evalue_refs(&EV
);
1017 free_evalue_refs(num
.E
);
1019 } else if (num
.pos
!= -1) {
1020 dpoly_n
d(dim
, num
.constant
, num
.coeff
);
1023 uni_polynom(num
.pos
, c
, &EV
);
1025 free_evalue_refs(&EV
);
1027 mpq_set_si(count
, 0, 1);
1028 dpoly
d(dim
, num
.constant
);
1029 d
.div(n
, count
, sign
);
1032 evalue_set(&EV
, &count
[0]._mp_num
, &count
[0]._mp_den
);
1034 free_evalue_refs(&EV
);
1038 struct enumerator_base
{
1043 vertex_decomposer
*vpd
;
1045 enumerator_base(unsigned dim
, vertex_decomposer
*vpd
)
1050 vE
= new evalue_p
[vpd
->nbV
];
1051 for (int j
= 0; j
< vpd
->nbV
; ++j
)
1054 E_vertex
= new evalue_p
[dim
];
1057 evalue_set_si(&mone
, -1, 1);
1060 void decompose_at(Param_Vertices
*V
, int _i
, barvinok_options
*options
) {
1063 vE
[_i
] = new evalue
;
1064 value_init(vE
[_i
]->d
);
1065 evalue_set_si(vE
[_i
], 0, 1);
1067 vpd
->decompose_at_vertex(V
, _i
, options
);
1070 ~enumerator_base() {
1071 for (int j
= 0; j
< vpd
->nbV
; ++j
)
1073 free_evalue_refs(vE
[j
]);
1080 free_evalue_refs(&mone
);
1083 evalue
*E_num(int i
, int d
) {
1084 return E_vertex
[i
+ (dim
-d
)];
1093 cumulator(evalue
*factor
, evalue
*v
, dpoly_r
*r
) :
1094 factor(factor
), v(v
), r(r
) {}
1098 virtual void add_term(int *powers
, int len
, evalue
*f2
) = 0;
1101 void cumulator::cumulate()
1103 evalue cum
; // factor * 1 * E_num[0]/1 * (E_num[0]-1)/2 *...
1105 evalue t
; // E_num[0] - (m-1)
1111 evalue_set_si(&mone
, -1, 1);
1115 evalue_copy(&cum
, factor
);
1118 value_set_si(f
.d
, 1);
1119 value_set_si(f
.x
.n
, 1);
1124 for (cst
= &t
; value_zero_p(cst
->d
); ) {
1125 if (cst
->x
.p
->type
== fractional
)
1126 cst
= &cst
->x
.p
->arr
[1];
1128 cst
= &cst
->x
.p
->arr
[0];
1132 for (int m
= 0; m
< r
->len
; ++m
) {
1135 value_set_si(f
.d
, m
);
1138 value_subtract(cst
->x
.n
, cst
->x
.n
, cst
->d
);
1145 vector
< dpoly_r_term
* >& current
= r
->c
[r
->len
-1-m
];
1146 for (int j
= 0; j
< current
.size(); ++j
) {
1147 if (current
[j
]->coeff
== 0)
1149 evalue
*f2
= new evalue
;
1151 value_init(f2
->x
.n
);
1152 zz2value(current
[j
]->coeff
, f2
->x
.n
);
1153 zz2value(r
->denom
, f2
->d
);
1156 add_term(current
[j
]->powers
, r
->dim
, f2
);
1159 free_evalue_refs(&f
);
1160 free_evalue_refs(&t
);
1161 free_evalue_refs(&cum
);
1163 free_evalue_refs(&mone
);
1167 struct E_poly_term
{
1172 struct ie_cum
: public cumulator
{
1173 vector
<E_poly_term
*> terms
;
1175 ie_cum(evalue
*factor
, evalue
*v
, dpoly_r
*r
) : cumulator(factor
, v
, r
) {}
1177 virtual void add_term(int *powers
, int len
, evalue
*f2
);
1180 void ie_cum::add_term(int *powers
, int len
, evalue
*f2
)
1183 for (k
= 0; k
< terms
.size(); ++k
) {
1184 if (memcmp(terms
[k
]->powers
, powers
, len
* sizeof(int)) == 0) {
1185 eadd(f2
, terms
[k
]->E
);
1186 free_evalue_refs(f2
);
1191 if (k
>= terms
.size()) {
1192 E_poly_term
*ET
= new E_poly_term
;
1193 ET
->powers
= new int[len
];
1194 memcpy(ET
->powers
, powers
, len
* sizeof(int));
1196 terms
.push_back(ET
);
1200 struct ienumerator
: public polar_decomposer
, public vertex_decomposer
,
1201 public enumerator_base
{
1207 ienumerator(Polyhedron
*P
, unsigned dim
, unsigned nbV
) :
1208 vertex_decomposer(P
, nbV
, this), enumerator_base(dim
, this) {
1209 vertex
.SetLength(dim
);
1211 den
.SetDims(dim
, dim
);
1219 virtual void handle_polar(Polyhedron
*P
, int sign
);
1220 void reduce(evalue
*factor
, vec_ZZ
& num
, mat_ZZ
& den_f
);
1223 void ienumerator::reduce(
1224 evalue
*factor
, vec_ZZ
& num
, mat_ZZ
& den_f
)
1226 unsigned len
= den_f
.NumRows(); // number of factors in den
1227 unsigned dim
= num
.length();
1230 eadd(factor
, vE
[vert
]);
1235 den_s
.SetLength(len
);
1237 den_r
.SetDims(len
, dim
-1);
1241 for (r
= 0; r
< len
; ++r
) {
1242 den_s
[r
] = den_f
[r
][0];
1243 for (k
= 0; k
<= dim
-1; ++k
)
1245 den_r
[r
][k
-(k
>0)] = den_f
[r
][k
];
1250 num_p
.SetLength(dim
-1);
1251 for (k
= 0 ; k
<= dim
-1; ++k
)
1253 num_p
[k
-(k
>0)] = num
[k
];
1256 den_p
.SetLength(len
);
1260 normalize(one
, num_s
, num_p
, den_s
, den_p
, den_r
);
1262 emul(&mone
, factor
);
1266 for (int k
= 0; k
< len
; ++k
) {
1269 else if (den_s
[k
] == 0)
1272 if (no_param
== 0) {
1273 reduce(factor
, num_p
, den_r
);
1277 pden
.SetDims(only_param
, dim
-1);
1279 for (k
= 0, l
= 0; k
< len
; ++k
)
1281 pden
[l
++] = den_r
[k
];
1283 for (k
= 0; k
< len
; ++k
)
1287 dpoly
n(no_param
, num_s
);
1288 dpoly
D(no_param
, den_s
[k
], 1);
1289 for ( ; ++k
< len
; )
1290 if (den_p
[k
] == 0) {
1291 dpoly
fact(no_param
, den_s
[k
], 1);
1296 // if no_param + only_param == len then all powers
1297 // below will be all zero
1298 if (no_param
+ only_param
== len
) {
1299 if (E_num(0, dim
) != 0)
1300 r
= new dpoly_r(n
, len
);
1302 mpq_set_si(tcount
, 0, 1);
1304 n
.div(D
, tcount
, one
);
1306 if (value_notzero_p(mpq_numref(tcount
))) {
1310 value_assign(f
.x
.n
, mpq_numref(tcount
));
1311 value_assign(f
.d
, mpq_denref(tcount
));
1313 reduce(factor
, num_p
, pden
);
1314 free_evalue_refs(&f
);
1319 for (k
= 0; k
< len
; ++k
) {
1320 if (den_s
[k
] == 0 || den_p
[k
] == 0)
1323 dpoly
pd(no_param
-1, den_s
[k
], 1);
1326 for (l
= 0; l
< k
; ++l
)
1327 if (den_r
[l
] == den_r
[k
])
1331 r
= new dpoly_r(n
, pd
, l
, len
);
1333 dpoly_r
*nr
= new dpoly_r(r
, pd
, l
, len
);
1339 dpoly_r
*rc
= r
->div(D
);
1342 if (E_num(0, dim
) == 0) {
1343 int common
= pden
.NumRows();
1344 vector
< dpoly_r_term
* >& final
= r
->c
[r
->len
-1];
1350 zz2value(r
->denom
, f
.d
);
1351 for (int j
= 0; j
< final
.size(); ++j
) {
1352 if (final
[j
]->coeff
== 0)
1355 for (int k
= 0; k
< r
->dim
; ++k
) {
1356 int n
= final
[j
]->powers
[k
];
1359 pden
.SetDims(rows
+n
, pden
.NumCols());
1360 for (int l
= 0; l
< n
; ++l
)
1361 pden
[rows
+l
] = den_r
[k
];
1365 evalue_copy(&t
, factor
);
1366 zz2value(final
[j
]->coeff
, f
.x
.n
);
1368 reduce(&t
, num_p
, pden
);
1369 free_evalue_refs(&t
);
1371 free_evalue_refs(&f
);
1373 ie_cum
cum(factor
, E_num(0, dim
), r
);
1376 int common
= pden
.NumRows();
1378 for (int j
= 0; j
< cum
.terms
.size(); ++j
) {
1380 pden
.SetDims(rows
, pden
.NumCols());
1381 for (int k
= 0; k
< r
->dim
; ++k
) {
1382 int n
= cum
.terms
[j
]->powers
[k
];
1385 pden
.SetDims(rows
+n
, pden
.NumCols());
1386 for (int l
= 0; l
< n
; ++l
)
1387 pden
[rows
+l
] = den_r
[k
];
1390 reduce(cum
.terms
[j
]->E
, num_p
, pden
);
1391 free_evalue_refs(cum
.terms
[j
]->E
);
1392 delete cum
.terms
[j
]->E
;
1393 delete [] cum
.terms
[j
]->powers
;
1394 delete cum
.terms
[j
];
1401 static int type_offset(enode
*p
)
1403 return p
->type
== fractional
? 1 :
1404 p
->type
== flooring
? 1 : 0;
1407 static int edegree(evalue
*e
)
1412 if (value_notzero_p(e
->d
))
1416 int i
= type_offset(p
);
1417 if (p
->size
-i
-1 > d
)
1418 d
= p
->size
- i
- 1;
1419 for (; i
< p
->size
; i
++) {
1420 int d2
= edegree(&p
->arr
[i
]);
1427 void ienumerator::handle_polar(Polyhedron
*C
, int s
)
1429 assert(C
->NbRays
-1 == dim
);
1431 lattice_point(V
, C
, vertex
, E_vertex
);
1434 for (r
= 0; r
< dim
; ++r
)
1435 values2zz(C
->Ray
[r
]+1, den
[r
], dim
);
1439 evalue_set_si(&one
, s
, 1);
1440 reduce(&one
, vertex
, den
);
1441 free_evalue_refs(&one
);
1443 for (int i
= 0; i
< dim
; ++i
)
1445 free_evalue_refs(E_vertex
[i
]);
1450 struct bfenumerator
: public vertex_decomposer
, public bf_base
,
1451 public enumerator_base
{
1454 bfenumerator(Polyhedron
*P
, unsigned dim
, unsigned nbV
) :
1455 vertex_decomposer(P
, nbV
, this),
1456 bf_base(dim
), enumerator_base(dim
, this) {
1464 virtual void handle_polar(Polyhedron
*P
, int sign
);
1465 virtual void base(mat_ZZ
& factors
, bfc_vec
& v
);
1467 bfc_term_base
* new_bf_term(int len
) {
1468 bfe_term
* t
= new bfe_term(len
);
1472 virtual void set_factor(bfc_term_base
*t
, int k
, int change
) {
1473 bfe_term
* bfet
= static_cast<bfe_term
*>(t
);
1474 factor
= bfet
->factors
[k
];
1475 assert(factor
!= NULL
);
1476 bfet
->factors
[k
] = NULL
;
1478 emul(&mone
, factor
);
1481 virtual void set_factor(bfc_term_base
*t
, int k
, mpq_t
&q
, int change
) {
1482 bfe_term
* bfet
= static_cast<bfe_term
*>(t
);
1483 factor
= bfet
->factors
[k
];
1484 assert(factor
!= NULL
);
1485 bfet
->factors
[k
] = NULL
;
1491 value_oppose(f
.x
.n
, mpq_numref(q
));
1493 value_assign(f
.x
.n
, mpq_numref(q
));
1494 value_assign(f
.d
, mpq_denref(q
));
1496 free_evalue_refs(&f
);
1499 virtual void set_factor(bfc_term_base
*t
, int k
, const QQ
& c
, int change
) {
1500 bfe_term
* bfet
= static_cast<bfe_term
*>(t
);
1502 factor
= new evalue
;
1507 zz2value(c
.n
, f
.x
.n
);
1509 value_oppose(f
.x
.n
, f
.x
.n
);
1512 value_init(factor
->d
);
1513 evalue_copy(factor
, bfet
->factors
[k
]);
1515 free_evalue_refs(&f
);
1518 void set_factor(evalue
*f
, int change
) {
1524 virtual void insert_term(bfc_term_base
*t
, int i
) {
1525 bfe_term
* bfet
= static_cast<bfe_term
*>(t
);
1526 int len
= t
->terms
.NumRows()-1; // already increased by one
1528 bfet
->factors
.resize(len
+1);
1529 for (int j
= len
; j
> i
; --j
) {
1530 bfet
->factors
[j
] = bfet
->factors
[j
-1];
1531 t
->terms
[j
] = t
->terms
[j
-1];
1533 bfet
->factors
[i
] = factor
;
1537 virtual void update_term(bfc_term_base
*t
, int i
) {
1538 bfe_term
* bfet
= static_cast<bfe_term
*>(t
);
1540 eadd(factor
, bfet
->factors
[i
]);
1541 free_evalue_refs(factor
);
1545 virtual bool constant_vertex(int dim
) { return E_num(0, dim
) == 0; }
1547 virtual void cum(bf_reducer
*bfr
, bfc_term_base
*t
, int k
, dpoly_r
*r
);
1550 struct bfe_cum
: public cumulator
{
1552 bfc_term_base
*told
;
1556 bfe_cum(evalue
*factor
, evalue
*v
, dpoly_r
*r
, bf_reducer
*bfr
,
1557 bfc_term_base
*t
, int k
, bfenumerator
*e
) :
1558 cumulator(factor
, v
, r
), told(t
), k(k
),
1562 virtual void add_term(int *powers
, int len
, evalue
*f2
);
1565 void bfe_cum::add_term(int *powers
, int len
, evalue
*f2
)
1567 bfr
->update_powers(powers
, len
);
1569 bfc_term_base
* t
= bfe
->find_bfc_term(bfr
->vn
, bfr
->npowers
, bfr
->nnf
);
1570 bfe
->set_factor(f2
, bfr
->l_changes
% 2);
1571 bfe
->add_term(t
, told
->terms
[k
], bfr
->l_extra_num
);
1574 void bfenumerator::cum(bf_reducer
*bfr
, bfc_term_base
*t
, int k
,
1577 bfe_term
* bfet
= static_cast<bfe_term
*>(t
);
1578 bfe_cum
cum(bfet
->factors
[k
], E_num(0, bfr
->d
), r
, bfr
, t
, k
, this);
1582 void bfenumerator::base(mat_ZZ
& factors
, bfc_vec
& v
)
1584 for (int i
= 0; i
< v
.size(); ++i
) {
1585 assert(v
[i
]->terms
.NumRows() == 1);
1586 evalue
*factor
= static_cast<bfe_term
*>(v
[i
])->factors
[0];
1587 eadd(factor
, vE
[vert
]);
1592 void bfenumerator::handle_polar(Polyhedron
*C
, int s
)
1594 assert(C
->NbRays
-1 == enumerator_base::dim
);
1596 bfe_term
* t
= new bfe_term(enumerator_base::dim
);
1597 vector
< bfc_term_base
* > v
;
1600 t
->factors
.resize(1);
1602 t
->terms
.SetDims(1, enumerator_base::dim
);
1603 lattice_point(V
, C
, t
->terms
[0], E_vertex
);
1605 // the elements of factors are always lexpositive
1607 s
= setup_factors(C
, factors
, t
, s
);
1609 t
->factors
[0] = new evalue
;
1610 value_init(t
->factors
[0]->d
);
1611 evalue_set_si(t
->factors
[0], s
, 1);
1614 for (int i
= 0; i
< enumerator_base::dim
; ++i
)
1616 free_evalue_refs(E_vertex
[i
]);
1621 #ifdef HAVE_CORRECT_VERTICES
1622 static inline Param_Polyhedron
*Polyhedron2Param_SD(Polyhedron
**Din
,
1623 Polyhedron
*Cin
,int WS
,Polyhedron
**CEq
,Matrix
**CT
)
1625 if (WS
& POL_NO_DUAL
)
1627 return Polyhedron2Param_SimplifiedDomain(Din
, Cin
, WS
, CEq
, CT
);
1630 static Param_Polyhedron
*Polyhedron2Param_SD(Polyhedron
**Din
,
1631 Polyhedron
*Cin
,int WS
,Polyhedron
**CEq
,Matrix
**CT
)
1633 static char data
[] = " 1 0 0 0 0 1 -18 "
1634 " 1 0 0 -20 0 19 1 "
1635 " 1 0 1 20 0 -20 16 "
1638 " 1 4 -20 0 0 -1 23 "
1639 " 1 -4 20 0 0 1 -22 "
1640 " 1 0 1 0 20 -20 16 "
1641 " 1 0 0 0 -20 19 1 ";
1642 static int checked
= 0;
1647 Matrix
*M
= Matrix_Alloc(9, 7);
1648 for (i
= 0; i
< 9; ++i
)
1649 for (int j
= 0; j
< 7; ++j
) {
1650 sscanf(p
, "%d%n", &v
, &n
);
1652 value_set_si(M
->p
[i
][j
], v
);
1654 Polyhedron
*P
= Constraints2Polyhedron(M
, 1024);
1656 Polyhedron
*U
= Universe_Polyhedron(1);
1657 Param_Polyhedron
*PP
= Polyhedron2Param_Domain(P
, U
, 1024);
1661 for (i
= 0, V
= PP
->V
; V
; ++i
, V
= V
->next
)
1664 Param_Polyhedron_Free(PP
);
1666 fprintf(stderr
, "WARNING: results may be incorrect\n");
1668 "WARNING: use latest version of PolyLib to remove this warning\n");
1672 return Polyhedron2Param_SimplifiedDomain(Din
, Cin
, WS
, CEq
, CT
);
1676 static evalue
* barvinok_enumerate_ev_f(Polyhedron
*P
, Polyhedron
* C
,
1677 barvinok_options
*options
);
1680 static evalue
* barvinok_enumerate_cst(Polyhedron
*P
, Polyhedron
* C
,
1685 ALLOC(evalue
, eres
);
1686 value_init(eres
->d
);
1687 value_set_si(eres
->d
, 0);
1688 eres
->x
.p
= new_enode(partition
, 2, C
->Dimension
);
1689 EVALUE_SET_DOMAIN(eres
->x
.p
->arr
[0], DomainConstraintSimplify(C
, MaxRays
));
1690 value_set_si(eres
->x
.p
->arr
[1].d
, 1);
1691 value_init(eres
->x
.p
->arr
[1].x
.n
);
1693 value_set_si(eres
->x
.p
->arr
[1].x
.n
, 0);
1695 barvinok_count(P
, &eres
->x
.p
->arr
[1].x
.n
, MaxRays
);
1700 evalue
* barvinok_enumerate_with_options(Polyhedron
*P
, Polyhedron
* C
,
1701 struct barvinok_options
*options
)
1703 //P = unfringe(P, MaxRays);
1704 Polyhedron
*Corig
= C
;
1705 Polyhedron
*CEq
= NULL
, *rVD
, *CA
;
1707 unsigned nparam
= C
->Dimension
;
1711 value_init(factor
.d
);
1712 evalue_set_si(&factor
, 1, 1);
1714 CA
= align_context(C
, P
->Dimension
, options
->MaxRays
);
1715 P
= DomainIntersection(P
, CA
, options
->MaxRays
);
1716 Polyhedron_Free(CA
);
1719 POL_ENSURE_FACETS(P
);
1720 POL_ENSURE_VERTICES(P
);
1721 POL_ENSURE_FACETS(C
);
1722 POL_ENSURE_VERTICES(C
);
1724 if (C
->Dimension
== 0 || emptyQ(P
)) {
1726 eres
= barvinok_enumerate_cst(P
, CEq
? CEq
: Polyhedron_Copy(C
),
1729 emul(&factor
, eres
);
1730 reduce_evalue(eres
);
1731 free_evalue_refs(&factor
);
1738 if (Polyhedron_is_infinite_param(P
, nparam
))
1743 P
= remove_equalities_p(P
, P
->Dimension
-nparam
, &f
);
1747 if (P
->Dimension
== nparam
) {
1749 P
= Universe_Polyhedron(0);
1753 Polyhedron
*T
= Polyhedron_Factor(P
, nparam
, options
->MaxRays
);
1754 if (T
|| (P
->Dimension
== nparam
+1)) {
1757 for (Q
= T
? T
: P
; Q
; Q
= Q
->next
) {
1758 Polyhedron
*next
= Q
->next
;
1762 if (Q
->Dimension
!= C
->Dimension
)
1763 QC
= Polyhedron_Project(Q
, nparam
);
1766 C
= DomainIntersection(C
, QC
, options
->MaxRays
);
1768 Polyhedron_Free(C2
);
1770 Polyhedron_Free(QC
);
1778 if (T
->Dimension
== C
->Dimension
) {
1785 Polyhedron
*next
= P
->next
;
1787 eres
= barvinok_enumerate_ev_f(P
, C
, options
);
1794 for (Q
= P
->next
; Q
; Q
= Q
->next
) {
1795 Polyhedron
*next
= Q
->next
;
1798 f
= barvinok_enumerate_ev_f(Q
, C
, options
);
1800 free_evalue_refs(f
);
1810 evalue
* barvinok_enumerate_ev(Polyhedron
*P
, Polyhedron
* C
, unsigned MaxRays
)
1813 barvinok_options
*options
= barvinok_options_new_with_defaults();
1814 options
->MaxRays
= MaxRays
;
1815 E
= barvinok_enumerate_with_options(P
, C
, options
);
1820 static evalue
* barvinok_enumerate_ev_f(Polyhedron
*P
, Polyhedron
* C
,
1821 barvinok_options
*options
)
1823 unsigned nparam
= C
->Dimension
;
1825 if (P
->Dimension
- nparam
== 1)
1826 return ParamLine_Length(P
, C
, options
->MaxRays
);
1828 Param_Polyhedron
*PP
= NULL
;
1829 Polyhedron
*CEq
= NULL
, *pVD
;
1831 Param_Domain
*D
, *next
;
1834 Polyhedron
*Porig
= P
;
1836 PP
= Polyhedron2Param_SD(&P
,C
,options
->MaxRays
,&CEq
,&CT
);
1838 if (isIdentity(CT
)) {
1842 assert(CT
->NbRows
!= CT
->NbColumns
);
1843 if (CT
->NbRows
== 1) { // no more parameters
1844 eres
= barvinok_enumerate_cst(P
, CEq
, options
->MaxRays
);
1849 Param_Polyhedron_Free(PP
);
1855 nparam
= CT
->NbRows
- 1;
1858 unsigned dim
= P
->Dimension
- nparam
;
1860 ALLOC(evalue
, eres
);
1861 value_init(eres
->d
);
1862 value_set_si(eres
->d
, 0);
1865 for (nd
= 0, D
=PP
->D
; D
; ++nd
, D
=D
->next
);
1866 struct section
{ Polyhedron
*D
; evalue E
; };
1867 section
*s
= new section
[nd
];
1868 Polyhedron
**fVD
= new Polyhedron_p
[nd
];
1871 #ifdef USE_INCREMENTAL_BF
1872 bfenumerator
et(P
, dim
, PP
->nbV
);
1873 #elif defined USE_INCREMENTAL_DF
1874 ienumerator
et(P
, dim
, PP
->nbV
);
1876 enumerator
et(P
, dim
, PP
->nbV
);
1879 for(nd
= 0, D
=PP
->D
; D
; D
=next
) {
1882 Polyhedron
*rVD
= reduce_domain(D
->Domain
, CT
, CEq
,
1883 fVD
, nd
, options
->MaxRays
);
1887 pVD
= CT
? DomainImage(rVD
,CT
,options
->MaxRays
) : rVD
;
1889 value_init(s
[nd
].E
.d
);
1890 evalue_set_si(&s
[nd
].E
, 0, 1);
1893 FORALL_PVertex_in_ParamPolyhedron(V
,D
,PP
) // _i is internal counter
1896 et
.decompose_at(V
, _i
, options
);
1897 } catch (OrthogonalException
&e
) {
1900 for (; nd
>= 0; --nd
) {
1901 free_evalue_refs(&s
[nd
].E
);
1902 Domain_Free(s
[nd
].D
);
1903 Domain_Free(fVD
[nd
]);
1907 eadd(et
.vE
[_i
] , &s
[nd
].E
);
1908 END_FORALL_PVertex_in_ParamPolyhedron
;
1909 evalue_range_reduction_in_domain(&s
[nd
].E
, pVD
);
1912 addeliminatedparams_evalue(&s
[nd
].E
, CT
);
1919 evalue_set_si(eres
, 0, 1);
1921 eres
->x
.p
= new_enode(partition
, 2*nd
, C
->Dimension
);
1922 for (int j
= 0; j
< nd
; ++j
) {
1923 EVALUE_SET_DOMAIN(eres
->x
.p
->arr
[2*j
], s
[j
].D
);
1924 value_clear(eres
->x
.p
->arr
[2*j
+1].d
);
1925 eres
->x
.p
->arr
[2*j
+1] = s
[j
].E
;
1926 Domain_Free(fVD
[j
]);
1933 Polyhedron_Free(CEq
);
1937 Enumeration
* barvinok_enumerate(Polyhedron
*P
, Polyhedron
* C
, unsigned MaxRays
)
1939 evalue
*EP
= barvinok_enumerate_ev(P
, C
, MaxRays
);
1941 return partition2enumeration(EP
);
1944 static void SwapColumns(Value
**V
, int n
, int i
, int j
)
1946 for (int r
= 0; r
< n
; ++r
)
1947 value_swap(V
[r
][i
], V
[r
][j
]);
1950 static void SwapColumns(Polyhedron
*P
, int i
, int j
)
1952 SwapColumns(P
->Constraint
, P
->NbConstraints
, i
, j
);
1953 SwapColumns(P
->Ray
, P
->NbRays
, i
, j
);
1956 /* Construct a constraint c from constraints l and u such that if
1957 * if constraint c holds then for each value of the other variables
1958 * there is at most one value of variable pos (position pos+1 in the constraints).
1960 * Given a lower and an upper bound
1961 * n_l v_i + <c_l,x> + c_l >= 0
1962 * -n_u v_i + <c_u,x> + c_u >= 0
1963 * the constructed constraint is
1965 * -(n_l<c_u,x> + n_u<c_l,x>) + (-n_l c_u - n_u c_l + n_l n_u - 1)
1967 * which is then simplified to remove the content of the non-constant coefficients
1969 * len is the total length of the constraints.
1970 * v is a temporary variable that can be used by this procedure
1972 static void negative_test_constraint(Value
*l
, Value
*u
, Value
*c
, int pos
,
1975 value_oppose(*v
, u
[pos
+1]);
1976 Vector_Combine(l
+1, u
+1, c
+1, *v
, l
[pos
+1], len
-1);
1977 value_multiply(*v
, *v
, l
[pos
+1]);
1978 value_subtract(c
[len
-1], c
[len
-1], *v
);
1979 value_set_si(*v
, -1);
1980 Vector_Scale(c
+1, c
+1, *v
, len
-1);
1981 value_decrement(c
[len
-1], c
[len
-1]);
1982 ConstraintSimplify(c
, c
, len
, v
);
1985 static bool parallel_constraints(Value
*l
, Value
*u
, Value
*c
, int pos
,
1994 Vector_Gcd(&l
[1+pos
], len
, &g1
);
1995 Vector_Gcd(&u
[1+pos
], len
, &g2
);
1996 Vector_Combine(l
+1+pos
, u
+1+pos
, c
+1, g2
, g1
, len
);
1997 parallel
= First_Non_Zero(c
+1, len
) == -1;
2005 static void negative_test_constraint7(Value
*l
, Value
*u
, Value
*c
, int pos
,
2006 int exist
, int len
, Value
*v
)
2011 Vector_Gcd(&u
[1+pos
], exist
, v
);
2012 Vector_Gcd(&l
[1+pos
], exist
, &g
);
2013 Vector_Combine(l
+1, u
+1, c
+1, *v
, g
, len
-1);
2014 value_multiply(*v
, *v
, g
);
2015 value_subtract(c
[len
-1], c
[len
-1], *v
);
2016 value_set_si(*v
, -1);
2017 Vector_Scale(c
+1, c
+1, *v
, len
-1);
2018 value_decrement(c
[len
-1], c
[len
-1]);
2019 ConstraintSimplify(c
, c
, len
, v
);
2024 /* Turns a x + b >= 0 into a x + b <= -1
2026 * len is the total length of the constraint.
2027 * v is a temporary variable that can be used by this procedure
2029 static void oppose_constraint(Value
*c
, int len
, Value
*v
)
2031 value_set_si(*v
, -1);
2032 Vector_Scale(c
+1, c
+1, *v
, len
-1);
2033 value_decrement(c
[len
-1], c
[len
-1]);
2036 /* Split polyhedron P into two polyhedra *pos and *neg, where
2037 * existential variable i has at most one solution for each
2038 * value of the other variables in *neg.
2040 * The splitting is performed using constraints l and u.
2042 * nvar: number of set variables
2043 * row: temporary vector that can be used by this procedure
2044 * f: temporary value that can be used by this procedure
2046 static bool SplitOnConstraint(Polyhedron
*P
, int i
, int l
, int u
,
2047 int nvar
, int MaxRays
, Vector
*row
, Value
& f
,
2048 Polyhedron
**pos
, Polyhedron
**neg
)
2050 negative_test_constraint(P
->Constraint
[l
], P
->Constraint
[u
],
2051 row
->p
, nvar
+i
, P
->Dimension
+2, &f
);
2052 *neg
= AddConstraints(row
->p
, 1, P
, MaxRays
);
2054 /* We found an independent, but useless constraint
2055 * Maybe we should detect this earlier and not
2056 * mark the variable as INDEPENDENT
2058 if (emptyQ((*neg
))) {
2059 Polyhedron_Free(*neg
);
2063 oppose_constraint(row
->p
, P
->Dimension
+2, &f
);
2064 *pos
= AddConstraints(row
->p
, 1, P
, MaxRays
);
2066 if (emptyQ((*pos
))) {
2067 Polyhedron_Free(*neg
);
2068 Polyhedron_Free(*pos
);
2076 * unimodularly transform P such that constraint r is transformed
2077 * into a constraint that involves only a single (the first)
2078 * existential variable
2081 static Polyhedron
*rotate_along(Polyhedron
*P
, int r
, int nvar
, int exist
,
2087 Vector
*row
= Vector_Alloc(exist
);
2088 Vector_Copy(P
->Constraint
[r
]+1+nvar
, row
->p
, exist
);
2089 Vector_Gcd(row
->p
, exist
, &g
);
2090 if (value_notone_p(g
))
2091 Vector_AntiScale(row
->p
, row
->p
, g
, exist
);
2094 Matrix
*M
= unimodular_complete(row
);
2095 Matrix
*M2
= Matrix_Alloc(P
->Dimension
+1, P
->Dimension
+1);
2096 for (r
= 0; r
< nvar
; ++r
)
2097 value_set_si(M2
->p
[r
][r
], 1);
2098 for ( ; r
< nvar
+exist
; ++r
)
2099 Vector_Copy(M
->p
[r
-nvar
], M2
->p
[r
]+nvar
, exist
);
2100 for ( ; r
< P
->Dimension
+1; ++r
)
2101 value_set_si(M2
->p
[r
][r
], 1);
2102 Polyhedron
*T
= Polyhedron_Image(P
, M2
, MaxRays
);
2111 /* Split polyhedron P into two polyhedra *pos and *neg, where
2112 * existential variable i has at most one solution for each
2113 * value of the other variables in *neg.
2115 * If independent is set, then the two constraints on which the
2116 * split will be performed need to be independent of the other
2117 * existential variables.
2119 * Return true if an appropriate split could be performed.
2121 * nvar: number of set variables
2122 * exist: number of existential variables
2123 * row: temporary vector that can be used by this procedure
2124 * f: temporary value that can be used by this procedure
2126 static bool SplitOnVar(Polyhedron
*P
, int i
,
2127 int nvar
, int exist
, int MaxRays
,
2128 Vector
*row
, Value
& f
, bool independent
,
2129 Polyhedron
**pos
, Polyhedron
**neg
)
2133 for (int l
= P
->NbEq
; l
< P
->NbConstraints
; ++l
) {
2134 if (value_negz_p(P
->Constraint
[l
][nvar
+i
+1]))
2138 for (j
= 0; j
< exist
; ++j
)
2139 if (j
!= i
&& value_notzero_p(P
->Constraint
[l
][nvar
+j
+1]))
2145 for (int u
= P
->NbEq
; u
< P
->NbConstraints
; ++u
) {
2146 if (value_posz_p(P
->Constraint
[u
][nvar
+i
+1]))
2150 for (j
= 0; j
< exist
; ++j
)
2151 if (j
!= i
&& value_notzero_p(P
->Constraint
[u
][nvar
+j
+1]))
2157 if (SplitOnConstraint(P
, i
, l
, u
, nvar
, MaxRays
, row
, f
, pos
, neg
)) {
2160 SwapColumns(*neg
, nvar
+1, nvar
+1+i
);
2170 static bool double_bound_pair(Polyhedron
*P
, int nvar
, int exist
,
2171 int i
, int l1
, int l2
,
2172 Polyhedron
**pos
, Polyhedron
**neg
)
2176 Vector
*row
= Vector_Alloc(P
->Dimension
+2);
2177 value_set_si(row
->p
[0], 1);
2178 value_oppose(f
, P
->Constraint
[l1
][nvar
+i
+1]);
2179 Vector_Combine(P
->Constraint
[l1
]+1, P
->Constraint
[l2
]+1,
2181 P
->Constraint
[l2
][nvar
+i
+1], f
,
2183 ConstraintSimplify(row
->p
, row
->p
, P
->Dimension
+2, &f
);
2184 *pos
= AddConstraints(row
->p
, 1, P
, 0);
2185 value_set_si(f
, -1);
2186 Vector_Scale(row
->p
+1, row
->p
+1, f
, P
->Dimension
+1);
2187 value_decrement(row
->p
[P
->Dimension
+1], row
->p
[P
->Dimension
+1]);
2188 *neg
= AddConstraints(row
->p
, 1, P
, 0);
2192 return !emptyQ((*pos
)) && !emptyQ((*neg
));
2195 static bool double_bound(Polyhedron
*P
, int nvar
, int exist
,
2196 Polyhedron
**pos
, Polyhedron
**neg
)
2198 for (int i
= 0; i
< exist
; ++i
) {
2200 for (l1
= P
->NbEq
; l1
< P
->NbConstraints
; ++l1
) {
2201 if (value_negz_p(P
->Constraint
[l1
][nvar
+i
+1]))
2203 for (l2
= l1
+ 1; l2
< P
->NbConstraints
; ++l2
) {
2204 if (value_negz_p(P
->Constraint
[l2
][nvar
+i
+1]))
2206 if (double_bound_pair(P
, nvar
, exist
, i
, l1
, l2
, pos
, neg
))
2210 for (l1
= P
->NbEq
; l1
< P
->NbConstraints
; ++l1
) {
2211 if (value_posz_p(P
->Constraint
[l1
][nvar
+i
+1]))
2213 if (l1
< P
->NbConstraints
)
2214 for (l2
= l1
+ 1; l2
< P
->NbConstraints
; ++l2
) {
2215 if (value_posz_p(P
->Constraint
[l2
][nvar
+i
+1]))
2217 if (double_bound_pair(P
, nvar
, exist
, i
, l1
, l2
, pos
, neg
))
2229 INDEPENDENT
= 1 << 2,
2233 static evalue
* enumerate_or(Polyhedron
*D
,
2234 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2237 fprintf(stderr
, "\nER: Or\n");
2238 #endif /* DEBUG_ER */
2240 Polyhedron
*N
= D
->next
;
2243 barvinok_enumerate_e_with_options(D
, exist
, nparam
, options
);
2246 for (D
= N
; D
; D
= N
) {
2251 barvinok_enumerate_e_with_options(D
, exist
, nparam
, options
);
2254 free_evalue_refs(EN
);
2264 static evalue
* enumerate_sum(Polyhedron
*P
,
2265 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2267 int nvar
= P
->Dimension
- exist
- nparam
;
2268 int toswap
= nvar
< exist
? nvar
: exist
;
2269 for (int i
= 0; i
< toswap
; ++i
)
2270 SwapColumns(P
, 1 + i
, nvar
+exist
- i
);
2274 fprintf(stderr
, "\nER: Sum\n");
2275 #endif /* DEBUG_ER */
2277 evalue
*EP
= barvinok_enumerate_e_with_options(P
, exist
, nparam
, options
);
2279 for (int i
= 0; i
< /* nvar */ nparam
; ++i
) {
2280 Matrix
*C
= Matrix_Alloc(1, 1 + nparam
+ 1);
2281 value_set_si(C
->p
[0][0], 1);
2283 value_init(split
.d
);
2284 value_set_si(split
.d
, 0);
2285 split
.x
.p
= new_enode(partition
, 4, nparam
);
2286 value_set_si(C
->p
[0][1+i
], 1);
2287 Matrix
*C2
= Matrix_Copy(C
);
2288 EVALUE_SET_DOMAIN(split
.x
.p
->arr
[0],
2289 Constraints2Polyhedron(C2
, options
->MaxRays
));
2291 evalue_set_si(&split
.x
.p
->arr
[1], 1, 1);
2292 value_set_si(C
->p
[0][1+i
], -1);
2293 value_set_si(C
->p
[0][1+nparam
], -1);
2294 EVALUE_SET_DOMAIN(split
.x
.p
->arr
[2],
2295 Constraints2Polyhedron(C
, options
->MaxRays
));
2296 evalue_set_si(&split
.x
.p
->arr
[3], 1, 1);
2298 free_evalue_refs(&split
);
2302 evalue_range_reduction(EP
);
2304 evalue_frac2floor(EP
);
2306 evalue
*sum
= esum(EP
, nvar
);
2308 free_evalue_refs(EP
);
2312 evalue_range_reduction(EP
);
2317 static evalue
* split_sure(Polyhedron
*P
, Polyhedron
*S
,
2318 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2320 int nvar
= P
->Dimension
- exist
- nparam
;
2322 Matrix
*M
= Matrix_Alloc(exist
, S
->Dimension
+2);
2323 for (int i
= 0; i
< exist
; ++i
)
2324 value_set_si(M
->p
[i
][nvar
+i
+1], 1);
2326 S
= DomainAddRays(S
, M
, options
->MaxRays
);
2328 Polyhedron
*F
= DomainAddRays(P
, M
, options
->MaxRays
);
2329 Polyhedron
*D
= DomainDifference(F
, S
, options
->MaxRays
);
2331 D
= Disjoint_Domain(D
, 0, options
->MaxRays
);
2336 M
= Matrix_Alloc(P
->Dimension
+1-exist
, P
->Dimension
+1);
2337 for (int j
= 0; j
< nvar
; ++j
)
2338 value_set_si(M
->p
[j
][j
], 1);
2339 for (int j
= 0; j
< nparam
+1; ++j
)
2340 value_set_si(M
->p
[nvar
+j
][nvar
+exist
+j
], 1);
2341 Polyhedron
*T
= Polyhedron_Image(S
, M
, options
->MaxRays
);
2342 evalue
*EP
= barvinok_enumerate_e_with_options(T
, 0, nparam
, options
);
2347 for (Polyhedron
*Q
= D
; Q
; Q
= Q
->next
) {
2348 Polyhedron
*N
= Q
->next
;
2350 T
= DomainIntersection(P
, Q
, options
->MaxRays
);
2351 evalue
*E
= barvinok_enumerate_e_with_options(T
, exist
, nparam
, options
);
2353 free_evalue_refs(E
);
2362 static evalue
* enumerate_sure(Polyhedron
*P
,
2363 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2367 int nvar
= P
->Dimension
- exist
- nparam
;
2373 for (i
= 0; i
< exist
; ++i
) {
2374 Matrix
*M
= Matrix_Alloc(S
->NbConstraints
, S
->Dimension
+2);
2376 value_set_si(lcm
, 1);
2377 for (int j
= 0; j
< S
->NbConstraints
; ++j
) {
2378 if (value_negz_p(S
->Constraint
[j
][1+nvar
+i
]))
2380 if (value_one_p(S
->Constraint
[j
][1+nvar
+i
]))
2382 value_lcm(lcm
, S
->Constraint
[j
][1+nvar
+i
], &lcm
);
2385 for (int j
= 0; j
< S
->NbConstraints
; ++j
) {
2386 if (value_negz_p(S
->Constraint
[j
][1+nvar
+i
]))
2388 if (value_one_p(S
->Constraint
[j
][1+nvar
+i
]))
2390 value_division(f
, lcm
, S
->Constraint
[j
][1+nvar
+i
]);
2391 Vector_Scale(S
->Constraint
[j
], M
->p
[c
], f
, S
->Dimension
+2);
2392 value_subtract(M
->p
[c
][S
->Dimension
+1],
2393 M
->p
[c
][S
->Dimension
+1],
2395 value_increment(M
->p
[c
][S
->Dimension
+1],
2396 M
->p
[c
][S
->Dimension
+1]);
2400 S
= AddConstraints(M
->p
[0], c
, S
, options
->MaxRays
);
2415 fprintf(stderr
, "\nER: Sure\n");
2416 #endif /* DEBUG_ER */
2418 return split_sure(P
, S
, exist
, nparam
, options
);
2421 static evalue
* enumerate_sure2(Polyhedron
*P
,
2422 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2424 int nvar
= P
->Dimension
- exist
- nparam
;
2426 for (r
= 0; r
< P
->NbRays
; ++r
)
2427 if (value_one_p(P
->Ray
[r
][0]) &&
2428 value_one_p(P
->Ray
[r
][P
->Dimension
+1]))
2434 Matrix
*M
= Matrix_Alloc(nvar
+ 1 + nparam
, P
->Dimension
+2);
2435 for (int i
= 0; i
< nvar
; ++i
)
2436 value_set_si(M
->p
[i
][1+i
], 1);
2437 for (int i
= 0; i
< nparam
; ++i
)
2438 value_set_si(M
->p
[i
+nvar
][1+nvar
+exist
+i
], 1);
2439 Vector_Copy(P
->Ray
[r
]+1+nvar
, M
->p
[nvar
+nparam
]+1+nvar
, exist
);
2440 value_set_si(M
->p
[nvar
+nparam
][0], 1);
2441 value_set_si(M
->p
[nvar
+nparam
][P
->Dimension
+1], 1);
2442 Polyhedron
* F
= Rays2Polyhedron(M
, options
->MaxRays
);
2445 Polyhedron
*I
= DomainIntersection(F
, P
, options
->MaxRays
);
2449 fprintf(stderr
, "\nER: Sure2\n");
2450 #endif /* DEBUG_ER */
2452 return split_sure(P
, I
, exist
, nparam
, options
);
2455 static evalue
* enumerate_cyclic(Polyhedron
*P
,
2456 unsigned exist
, unsigned nparam
,
2457 evalue
* EP
, int r
, int p
, unsigned MaxRays
)
2459 int nvar
= P
->Dimension
- exist
- nparam
;
2461 /* If EP in its fractional maps only contains references
2462 * to the remainder parameter with appropriate coefficients
2463 * then we could in principle avoid adding existentially
2464 * quantified variables to the validity domains.
2465 * We'd have to replace the remainder by m { p/m }
2466 * and multiply with an appropriate factor that is one
2467 * only in the appropriate range.
2468 * This last multiplication can be avoided if EP
2469 * has a single validity domain with no (further)
2470 * constraints on the remainder parameter
2473 Matrix
*CT
= Matrix_Alloc(nparam
+1, nparam
+3);
2474 Matrix
*M
= Matrix_Alloc(1, 1+nparam
+3);
2475 for (int j
= 0; j
< nparam
; ++j
)
2477 value_set_si(CT
->p
[j
][j
], 1);
2478 value_set_si(CT
->p
[p
][nparam
+1], 1);
2479 value_set_si(CT
->p
[nparam
][nparam
+2], 1);
2480 value_set_si(M
->p
[0][1+p
], -1);
2481 value_absolute(M
->p
[0][1+nparam
], P
->Ray
[0][1+nvar
+exist
+p
]);
2482 value_set_si(M
->p
[0][1+nparam
+1], 1);
2483 Polyhedron
*CEq
= Constraints2Polyhedron(M
, 1);
2485 addeliminatedparams_enum(EP
, CT
, CEq
, MaxRays
, nparam
);
2486 Polyhedron_Free(CEq
);
2492 static void enumerate_vd_add_ray(evalue
*EP
, Matrix
*Rays
, unsigned MaxRays
)
2494 if (value_notzero_p(EP
->d
))
2497 assert(EP
->x
.p
->type
== partition
);
2498 assert(EP
->x
.p
->pos
== EVALUE_DOMAIN(EP
->x
.p
->arr
[0])->Dimension
);
2499 for (int i
= 0; i
< EP
->x
.p
->size
/2; ++i
) {
2500 Polyhedron
*D
= EVALUE_DOMAIN(EP
->x
.p
->arr
[2*i
]);
2501 Polyhedron
*N
= DomainAddRays(D
, Rays
, MaxRays
);
2502 EVALUE_SET_DOMAIN(EP
->x
.p
->arr
[2*i
], N
);
2507 static evalue
* enumerate_line(Polyhedron
*P
,
2508 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2514 fprintf(stderr
, "\nER: Line\n");
2515 #endif /* DEBUG_ER */
2517 int nvar
= P
->Dimension
- exist
- nparam
;
2519 for (i
= 0; i
< nparam
; ++i
)
2520 if (value_notzero_p(P
->Ray
[0][1+nvar
+exist
+i
]))
2523 for (j
= i
+1; j
< nparam
; ++j
)
2524 if (value_notzero_p(P
->Ray
[0][1+nvar
+exist
+i
]))
2526 assert(j
>= nparam
); // for now
2528 Matrix
*M
= Matrix_Alloc(2, P
->Dimension
+2);
2529 value_set_si(M
->p
[0][0], 1);
2530 value_set_si(M
->p
[0][1+nvar
+exist
+i
], 1);
2531 value_set_si(M
->p
[1][0], 1);
2532 value_set_si(M
->p
[1][1+nvar
+exist
+i
], -1);
2533 value_absolute(M
->p
[1][1+P
->Dimension
], P
->Ray
[0][1+nvar
+exist
+i
]);
2534 value_decrement(M
->p
[1][1+P
->Dimension
], M
->p
[1][1+P
->Dimension
]);
2535 Polyhedron
*S
= AddConstraints(M
->p
[0], 2, P
, options
->MaxRays
);
2536 evalue
*EP
= barvinok_enumerate_e_with_options(S
, exist
, nparam
, options
);
2540 return enumerate_cyclic(P
, exist
, nparam
, EP
, 0, i
, options
->MaxRays
);
2543 static int single_param_pos(Polyhedron
*P
, unsigned exist
, unsigned nparam
,
2546 int nvar
= P
->Dimension
- exist
- nparam
;
2547 if (First_Non_Zero(P
->Ray
[r
]+1, nvar
) != -1)
2549 int i
= First_Non_Zero(P
->Ray
[r
]+1+nvar
+exist
, nparam
);
2552 if (First_Non_Zero(P
->Ray
[r
]+1+nvar
+exist
+1, nparam
-i
-1) != -1)
2557 static evalue
* enumerate_remove_ray(Polyhedron
*P
, int r
,
2558 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2561 fprintf(stderr
, "\nER: RedundantRay\n");
2562 #endif /* DEBUG_ER */
2566 value_set_si(one
, 1);
2567 int len
= P
->NbRays
-1;
2568 Matrix
*M
= Matrix_Alloc(2 * len
, P
->Dimension
+2);
2569 Vector_Copy(P
->Ray
[0], M
->p
[0], r
* (P
->Dimension
+2));
2570 Vector_Copy(P
->Ray
[r
+1], M
->p
[r
], (len
-r
) * (P
->Dimension
+2));
2571 for (int j
= 0; j
< P
->NbRays
; ++j
) {
2574 Vector_Combine(P
->Ray
[j
], P
->Ray
[r
], M
->p
[len
+j
-(j
>r
)],
2575 one
, P
->Ray
[j
][P
->Dimension
+1], P
->Dimension
+2);
2578 P
= Rays2Polyhedron(M
, options
->MaxRays
);
2580 evalue
*EP
= barvinok_enumerate_e_with_options(P
, exist
, nparam
, options
);
2587 static evalue
* enumerate_redundant_ray(Polyhedron
*P
,
2588 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2590 assert(P
->NbBid
== 0);
2591 int nvar
= P
->Dimension
- exist
- nparam
;
2595 for (int r
= 0; r
< P
->NbRays
; ++r
) {
2596 if (value_notzero_p(P
->Ray
[r
][P
->Dimension
+1]))
2598 int i1
= single_param_pos(P
, exist
, nparam
, r
);
2601 for (int r2
= r
+1; r2
< P
->NbRays
; ++r2
) {
2602 if (value_notzero_p(P
->Ray
[r2
][P
->Dimension
+1]))
2604 int i2
= single_param_pos(P
, exist
, nparam
, r2
);
2610 value_division(m
, P
->Ray
[r
][1+nvar
+exist
+i1
],
2611 P
->Ray
[r2
][1+nvar
+exist
+i1
]);
2612 value_multiply(m
, m
, P
->Ray
[r2
][1+nvar
+exist
+i1
]);
2613 /* r2 divides r => r redundant */
2614 if (value_eq(m
, P
->Ray
[r
][1+nvar
+exist
+i1
])) {
2616 return enumerate_remove_ray(P
, r
, exist
, nparam
, options
);
2619 value_division(m
, P
->Ray
[r2
][1+nvar
+exist
+i1
],
2620 P
->Ray
[r
][1+nvar
+exist
+i1
]);
2621 value_multiply(m
, m
, P
->Ray
[r
][1+nvar
+exist
+i1
]);
2622 /* r divides r2 => r2 redundant */
2623 if (value_eq(m
, P
->Ray
[r2
][1+nvar
+exist
+i1
])) {
2625 return enumerate_remove_ray(P
, r2
, exist
, nparam
, options
);
2633 static Polyhedron
*upper_bound(Polyhedron
*P
,
2634 int pos
, Value
*max
, Polyhedron
**R
)
2643 for (Polyhedron
*Q
= P
; Q
; Q
= N
) {
2645 for (r
= 0; r
< P
->NbRays
; ++r
) {
2646 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1]) &&
2647 value_pos_p(P
->Ray
[r
][1+pos
]))
2650 if (r
< P
->NbRays
) {
2658 for (r
= 0; r
< P
->NbRays
; ++r
) {
2659 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1]))
2661 mpz_fdiv_q(v
, P
->Ray
[r
][1+pos
], P
->Ray
[r
][1+P
->Dimension
]);
2662 if ((!Q
->next
&& r
== 0) || value_gt(v
, *max
))
2663 value_assign(*max
, v
);
2670 static evalue
* enumerate_ray(Polyhedron
*P
,
2671 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2673 assert(P
->NbBid
== 0);
2674 int nvar
= P
->Dimension
- exist
- nparam
;
2677 for (r
= 0; r
< P
->NbRays
; ++r
)
2678 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1]))
2684 for (r2
= r
+1; r2
< P
->NbRays
; ++r2
)
2685 if (value_zero_p(P
->Ray
[r2
][P
->Dimension
+1]))
2687 if (r2
< P
->NbRays
) {
2689 return enumerate_sum(P
, exist
, nparam
, options
);
2693 fprintf(stderr
, "\nER: Ray\n");
2694 #endif /* DEBUG_ER */
2700 value_set_si(one
, 1);
2701 int i
= single_param_pos(P
, exist
, nparam
, r
);
2702 assert(i
!= -1); // for now;
2704 Matrix
*M
= Matrix_Alloc(P
->NbRays
, P
->Dimension
+2);
2705 for (int j
= 0; j
< P
->NbRays
; ++j
) {
2706 Vector_Combine(P
->Ray
[j
], P
->Ray
[r
], M
->p
[j
],
2707 one
, P
->Ray
[j
][P
->Dimension
+1], P
->Dimension
+2);
2709 Polyhedron
*S
= Rays2Polyhedron(M
, options
->MaxRays
);
2711 Polyhedron
*D
= DomainDifference(P
, S
, options
->MaxRays
);
2713 // Polyhedron_Print(stderr, P_VALUE_FMT, D);
2714 assert(value_pos_p(P
->Ray
[r
][1+nvar
+exist
+i
])); // for now
2716 D
= upper_bound(D
, nvar
+exist
+i
, &m
, &R
);
2720 M
= Matrix_Alloc(2, P
->Dimension
+2);
2721 value_set_si(M
->p
[0][0], 1);
2722 value_set_si(M
->p
[1][0], 1);
2723 value_set_si(M
->p
[0][1+nvar
+exist
+i
], -1);
2724 value_set_si(M
->p
[1][1+nvar
+exist
+i
], 1);
2725 value_assign(M
->p
[0][1+P
->Dimension
], m
);
2726 value_oppose(M
->p
[1][1+P
->Dimension
], m
);
2727 value_addto(M
->p
[1][1+P
->Dimension
], M
->p
[1][1+P
->Dimension
],
2728 P
->Ray
[r
][1+nvar
+exist
+i
]);
2729 value_decrement(M
->p
[1][1+P
->Dimension
], M
->p
[1][1+P
->Dimension
]);
2730 // Matrix_Print(stderr, P_VALUE_FMT, M);
2731 D
= AddConstraints(M
->p
[0], 2, P
, options
->MaxRays
);
2732 // Polyhedron_Print(stderr, P_VALUE_FMT, D);
2733 value_subtract(M
->p
[0][1+P
->Dimension
], M
->p
[0][1+P
->Dimension
],
2734 P
->Ray
[r
][1+nvar
+exist
+i
]);
2735 // Matrix_Print(stderr, P_VALUE_FMT, M);
2736 S
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
2737 // Polyhedron_Print(stderr, P_VALUE_FMT, S);
2740 evalue
*EP
= barvinok_enumerate_e_with_options(D
, exist
, nparam
, options
);
2745 if (value_notone_p(P
->Ray
[r
][1+nvar
+exist
+i
]))
2746 EP
= enumerate_cyclic(P
, exist
, nparam
, EP
, r
, i
, options
->MaxRays
);
2748 M
= Matrix_Alloc(1, nparam
+2);
2749 value_set_si(M
->p
[0][0], 1);
2750 value_set_si(M
->p
[0][1+i
], 1);
2751 enumerate_vd_add_ray(EP
, M
, options
->MaxRays
);
2756 evalue
*E
= barvinok_enumerate_e_with_options(S
, exist
, nparam
, options
);
2758 free_evalue_refs(E
);
2765 evalue
*ER
= enumerate_or(R
, exist
, nparam
, options
);
2767 free_evalue_refs(ER
);
2774 static evalue
* enumerate_vd(Polyhedron
**PA
,
2775 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
2777 Polyhedron
*P
= *PA
;
2778 int nvar
= P
->Dimension
- exist
- nparam
;
2779 Param_Polyhedron
*PP
= NULL
;
2780 Polyhedron
*C
= Universe_Polyhedron(nparam
);
2784 PP
= Polyhedron2Param_SimplifiedDomain(&PR
,C
, options
->MaxRays
,&CEq
,&CT
);
2788 Param_Domain
*D
, *last
;
2791 for (nd
= 0, D
=PP
->D
; D
; D
=D
->next
, ++nd
)
2794 Polyhedron
**VD
= new Polyhedron_p
[nd
];
2795 Polyhedron
**fVD
= new Polyhedron_p
[nd
];
2796 for(nd
= 0, D
=PP
->D
; D
; D
=D
->next
) {
2797 Polyhedron
*rVD
= reduce_domain(D
->Domain
, CT
, CEq
,
2798 fVD
, nd
, options
->MaxRays
);
2811 /* This doesn't seem to have any effect */
2813 Polyhedron
*CA
= align_context(VD
[0], P
->Dimension
, options
->MaxRays
);
2815 P
= DomainIntersection(P
, CA
, options
->MaxRays
);
2818 Polyhedron_Free(CA
);
2823 if (!EP
&& CT
->NbColumns
!= CT
->NbRows
) {
2824 Polyhedron
*CEqr
= DomainImage(CEq
, CT
, options
->MaxRays
);
2825 Polyhedron
*CA
= align_context(CEqr
, PR
->Dimension
, options
->MaxRays
);
2826 Polyhedron
*I
= DomainIntersection(PR
, CA
, options
->MaxRays
);
2827 Polyhedron_Free(CEqr
);
2828 Polyhedron_Free(CA
);
2830 fprintf(stderr
, "\nER: Eliminate\n");
2831 #endif /* DEBUG_ER */
2832 nparam
-= CT
->NbColumns
- CT
->NbRows
;
2833 EP
= barvinok_enumerate_e_with_options(I
, exist
, nparam
, options
);
2834 nparam
+= CT
->NbColumns
- CT
->NbRows
;
2835 addeliminatedparams_enum(EP
, CT
, CEq
, options
->MaxRays
, nparam
);
2839 Polyhedron_Free(PR
);
2842 if (!EP
&& nd
> 1) {
2844 fprintf(stderr
, "\nER: VD\n");
2845 #endif /* DEBUG_ER */
2846 for (int i
= 0; i
< nd
; ++i
) {
2847 Polyhedron
*CA
= align_context(VD
[i
], P
->Dimension
, options
->MaxRays
);
2848 Polyhedron
*I
= DomainIntersection(P
, CA
, options
->MaxRays
);
2851 EP
= barvinok_enumerate_e_with_options(I
, exist
, nparam
, options
);
2853 evalue
*E
= barvinok_enumerate_e_with_options(I
, exist
, nparam
,
2856 free_evalue_refs(E
);
2860 Polyhedron_Free(CA
);
2864 for (int i
= 0; i
< nd
; ++i
) {
2865 Polyhedron_Free(VD
[i
]);
2866 Polyhedron_Free(fVD
[i
]);
2872 if (!EP
&& nvar
== 0) {
2875 Param_Vertices
*V
, *V2
;
2876 Matrix
* M
= Matrix_Alloc(1, P
->Dimension
+2);
2878 FORALL_PVertex_in_ParamPolyhedron(V
, last
, PP
) {
2880 FORALL_PVertex_in_ParamPolyhedron(V2
, last
, PP
) {
2887 for (int i
= 0; i
< exist
; ++i
) {
2888 value_oppose(f
, V
->Vertex
->p
[i
][nparam
+1]);
2889 Vector_Combine(V
->Vertex
->p
[i
],
2891 M
->p
[0] + 1 + nvar
+ exist
,
2892 V2
->Vertex
->p
[i
][nparam
+1],
2896 for (j
= 0; j
< nparam
; ++j
)
2897 if (value_notzero_p(M
->p
[0][1+nvar
+exist
+j
]))
2901 ConstraintSimplify(M
->p
[0], M
->p
[0],
2902 P
->Dimension
+2, &f
);
2903 value_set_si(M
->p
[0][0], 0);
2904 Polyhedron
*para
= AddConstraints(M
->p
[0], 1, P
,
2907 Polyhedron_Free(para
);
2910 Polyhedron
*pos
, *neg
;
2911 value_set_si(M
->p
[0][0], 1);
2912 value_decrement(M
->p
[0][P
->Dimension
+1],
2913 M
->p
[0][P
->Dimension
+1]);
2914 neg
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
2915 value_set_si(f
, -1);
2916 Vector_Scale(M
->p
[0]+1, M
->p
[0]+1, f
,
2918 value_decrement(M
->p
[0][P
->Dimension
+1],
2919 M
->p
[0][P
->Dimension
+1]);
2920 value_decrement(M
->p
[0][P
->Dimension
+1],
2921 M
->p
[0][P
->Dimension
+1]);
2922 pos
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
2923 if (emptyQ(neg
) && emptyQ(pos
)) {
2924 Polyhedron_Free(para
);
2925 Polyhedron_Free(pos
);
2926 Polyhedron_Free(neg
);
2930 fprintf(stderr
, "\nER: Order\n");
2931 #endif /* DEBUG_ER */
2932 EP
= barvinok_enumerate_e_with_options(para
, exist
, nparam
,
2936 E
= barvinok_enumerate_e_with_options(pos
, exist
, nparam
,
2939 free_evalue_refs(E
);
2943 E
= barvinok_enumerate_e_with_options(neg
, exist
, nparam
,
2946 free_evalue_refs(E
);
2949 Polyhedron_Free(para
);
2950 Polyhedron_Free(pos
);
2951 Polyhedron_Free(neg
);
2956 } END_FORALL_PVertex_in_ParamPolyhedron
;
2959 } END_FORALL_PVertex_in_ParamPolyhedron
;
2962 /* Search for vertex coordinate to split on */
2963 /* First look for one independent of the parameters */
2964 FORALL_PVertex_in_ParamPolyhedron(V
, last
, PP
) {
2965 for (int i
= 0; i
< exist
; ++i
) {
2967 for (j
= 0; j
< nparam
; ++j
)
2968 if (value_notzero_p(V
->Vertex
->p
[i
][j
]))
2972 value_set_si(M
->p
[0][0], 1);
2973 Vector_Set(M
->p
[0]+1, 0, nvar
+exist
);
2974 Vector_Copy(V
->Vertex
->p
[i
],
2975 M
->p
[0] + 1 + nvar
+ exist
, nparam
+1);
2976 value_oppose(M
->p
[0][1+nvar
+i
],
2977 V
->Vertex
->p
[i
][nparam
+1]);
2979 Polyhedron
*pos
, *neg
;
2980 value_set_si(M
->p
[0][0], 1);
2981 value_decrement(M
->p
[0][P
->Dimension
+1],
2982 M
->p
[0][P
->Dimension
+1]);
2983 neg
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
2984 value_set_si(f
, -1);
2985 Vector_Scale(M
->p
[0]+1, M
->p
[0]+1, f
,
2987 value_decrement(M
->p
[0][P
->Dimension
+1],
2988 M
->p
[0][P
->Dimension
+1]);
2989 value_decrement(M
->p
[0][P
->Dimension
+1],
2990 M
->p
[0][P
->Dimension
+1]);
2991 pos
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
2992 if (emptyQ(neg
) || emptyQ(pos
)) {
2993 Polyhedron_Free(pos
);
2994 Polyhedron_Free(neg
);
2997 Polyhedron_Free(pos
);
2998 value_increment(M
->p
[0][P
->Dimension
+1],
2999 M
->p
[0][P
->Dimension
+1]);
3000 pos
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
3002 fprintf(stderr
, "\nER: Vertex\n");
3003 #endif /* DEBUG_ER */
3005 EP
= enumerate_or(pos
, exist
, nparam
, options
);
3010 } END_FORALL_PVertex_in_ParamPolyhedron
;
3014 /* Search for vertex coordinate to split on */
3015 /* Now look for one that depends on the parameters */
3016 FORALL_PVertex_in_ParamPolyhedron(V
, last
, PP
) {
3017 for (int i
= 0; i
< exist
; ++i
) {
3018 value_set_si(M
->p
[0][0], 1);
3019 Vector_Set(M
->p
[0]+1, 0, nvar
+exist
);
3020 Vector_Copy(V
->Vertex
->p
[i
],
3021 M
->p
[0] + 1 + nvar
+ exist
, nparam
+1);
3022 value_oppose(M
->p
[0][1+nvar
+i
],
3023 V
->Vertex
->p
[i
][nparam
+1]);
3025 Polyhedron
*pos
, *neg
;
3026 value_set_si(M
->p
[0][0], 1);
3027 value_decrement(M
->p
[0][P
->Dimension
+1],
3028 M
->p
[0][P
->Dimension
+1]);
3029 neg
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
3030 value_set_si(f
, -1);
3031 Vector_Scale(M
->p
[0]+1, M
->p
[0]+1, f
,
3033 value_decrement(M
->p
[0][P
->Dimension
+1],
3034 M
->p
[0][P
->Dimension
+1]);
3035 value_decrement(M
->p
[0][P
->Dimension
+1],
3036 M
->p
[0][P
->Dimension
+1]);
3037 pos
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
3038 if (emptyQ(neg
) || emptyQ(pos
)) {
3039 Polyhedron_Free(pos
);
3040 Polyhedron_Free(neg
);
3043 Polyhedron_Free(pos
);
3044 value_increment(M
->p
[0][P
->Dimension
+1],
3045 M
->p
[0][P
->Dimension
+1]);
3046 pos
= AddConstraints(M
->p
[0], 1, P
, options
->MaxRays
);
3048 fprintf(stderr
, "\nER: ParamVertex\n");
3049 #endif /* DEBUG_ER */
3051 EP
= enumerate_or(pos
, exist
, nparam
, options
);
3056 } END_FORALL_PVertex_in_ParamPolyhedron
;
3064 Polyhedron_Free(CEq
);
3068 Param_Polyhedron_Free(PP
);
3075 evalue
*barvinok_enumerate_pip(Polyhedron
*P
,
3076 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
3081 evalue
*barvinok_enumerate_pip(Polyhedron
*P
,
3082 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
3084 int nvar
= P
->Dimension
- exist
- nparam
;
3085 evalue
*EP
= evalue_zero();
3089 fprintf(stderr
, "\nER: PIP\n");
3090 #endif /* DEBUG_ER */
3092 Polyhedron
*D
= pip_projectout(P
, nvar
, exist
, nparam
);
3093 for (Q
= D
; Q
; Q
= N
) {
3097 exist
= Q
->Dimension
- nvar
- nparam
;
3098 E
= barvinok_enumerate_e(Q
, exist
, nparam
, MaxRays
);
3101 free_evalue_refs(E
);
3110 static bool is_single(Value
*row
, int pos
, int len
)
3112 return First_Non_Zero(row
, pos
) == -1 &&
3113 First_Non_Zero(row
+pos
+1, len
-pos
-1) == -1;
3116 static evalue
* barvinok_enumerate_e_r(Polyhedron
*P
,
3117 unsigned exist
, unsigned nparam
, barvinok_options
*options
);
3120 static int er_level
= 0;
3122 evalue
* barvinok_enumerate_e_with_options(Polyhedron
*P
,
3123 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
3125 fprintf(stderr
, "\nER: level %i\n", er_level
);
3127 Polyhedron_PrintConstraints(stderr
, P_VALUE_FMT
, P
);
3128 fprintf(stderr
, "\nE %d\nP %d\n", exist
, nparam
);
3130 P
= DomainConstraintSimplify(Polyhedron_Copy(P
), options
->MaxRays
);
3131 evalue
*EP
= barvinok_enumerate_e_r(P
, exist
, nparam
, options
);
3137 evalue
* barvinok_enumerate_e_with_options(Polyhedron
*P
,
3138 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
3140 P
= DomainConstraintSimplify(Polyhedron_Copy(P
), options
->MaxRays
);
3141 evalue
*EP
= barvinok_enumerate_e_r(P
, exist
, nparam
, options
);
3147 evalue
* barvinok_enumerate_e(Polyhedron
*P
, unsigned exist
, unsigned nparam
,
3151 barvinok_options
*options
= barvinok_options_new_with_defaults();
3152 options
->MaxRays
= MaxRays
;
3153 E
= barvinok_enumerate_e_with_options(P
, exist
, nparam
, options
);
3158 static evalue
* barvinok_enumerate_e_r(Polyhedron
*P
,
3159 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
3162 Polyhedron
*U
= Universe_Polyhedron(nparam
);
3163 evalue
*EP
= barvinok_enumerate_with_options(P
, U
, options
);
3164 //char *param_name[] = {"P", "Q", "R", "S", "T" };
3165 //print_evalue(stdout, EP, param_name);
3170 int nvar
= P
->Dimension
- exist
- nparam
;
3171 int len
= P
->Dimension
+ 2;
3174 POL_ENSURE_FACETS(P
);
3175 POL_ENSURE_VERTICES(P
);
3178 return evalue_zero();
3180 if (nvar
== 0 && nparam
== 0) {
3181 evalue
*EP
= evalue_zero();
3182 barvinok_count_with_options(P
, &EP
->x
.n
, options
);
3183 if (value_pos_p(EP
->x
.n
))
3184 value_set_si(EP
->x
.n
, 1);
3189 for (r
= 0; r
< P
->NbRays
; ++r
)
3190 if (value_zero_p(P
->Ray
[r
][0]) ||
3191 value_zero_p(P
->Ray
[r
][P
->Dimension
+1])) {
3193 for (i
= 0; i
< nvar
; ++i
)
3194 if (value_notzero_p(P
->Ray
[r
][i
+1]))
3198 for (i
= nvar
+ exist
; i
< nvar
+ exist
+ nparam
; ++i
)
3199 if (value_notzero_p(P
->Ray
[r
][i
+1]))
3201 if (i
>= nvar
+ exist
+ nparam
)
3204 if (r
< P
->NbRays
) {
3205 evalue
*EP
= evalue_zero();
3206 value_set_si(EP
->x
.n
, -1);
3211 for (r
= 0; r
< P
->NbEq
; ++r
)
3212 if ((first
= First_Non_Zero(P
->Constraint
[r
]+1+nvar
, exist
)) != -1)
3215 if (First_Non_Zero(P
->Constraint
[r
]+1+nvar
+first
+1,
3216 exist
-first
-1) != -1) {
3217 Polyhedron
*T
= rotate_along(P
, r
, nvar
, exist
, options
->MaxRays
);
3219 fprintf(stderr
, "\nER: Equality\n");
3220 #endif /* DEBUG_ER */
3221 evalue
*EP
= barvinok_enumerate_e_with_options(T
, exist
-1, nparam
,
3227 fprintf(stderr
, "\nER: Fixed\n");
3228 #endif /* DEBUG_ER */
3230 return barvinok_enumerate_e_with_options(P
, exist
-1, nparam
,
3233 Polyhedron
*T
= Polyhedron_Copy(P
);
3234 SwapColumns(T
, nvar
+1, nvar
+1+first
);
3235 evalue
*EP
= barvinok_enumerate_e_with_options(T
, exist
-1, nparam
,
3243 Vector
*row
= Vector_Alloc(len
);
3244 value_set_si(row
->p
[0], 1);
3249 enum constraint
* info
= new constraint
[exist
];
3250 for (int i
= 0; i
< exist
; ++i
) {
3252 for (int l
= P
->NbEq
; l
< P
->NbConstraints
; ++l
) {
3253 if (value_negz_p(P
->Constraint
[l
][nvar
+i
+1]))
3255 bool l_parallel
= is_single(P
->Constraint
[l
]+nvar
+1, i
, exist
);
3256 for (int u
= P
->NbEq
; u
< P
->NbConstraints
; ++u
) {
3257 if (value_posz_p(P
->Constraint
[u
][nvar
+i
+1]))
3259 bool lu_parallel
= l_parallel
||
3260 is_single(P
->Constraint
[u
]+nvar
+1, i
, exist
);
3261 value_oppose(f
, P
->Constraint
[u
][nvar
+i
+1]);
3262 Vector_Combine(P
->Constraint
[l
]+1, P
->Constraint
[u
]+1, row
->p
+1,
3263 f
, P
->Constraint
[l
][nvar
+i
+1], len
-1);
3264 if (!(info
[i
] & INDEPENDENT
)) {
3266 for (j
= 0; j
< exist
; ++j
)
3267 if (j
!= i
&& value_notzero_p(row
->p
[nvar
+j
+1]))
3270 //printf("independent: i: %d, l: %d, u: %d\n", i, l, u);
3271 info
[i
] = (constraint
)(info
[i
] | INDEPENDENT
);
3274 if (info
[i
] & ALL_POS
) {
3275 value_addto(row
->p
[len
-1], row
->p
[len
-1],
3276 P
->Constraint
[l
][nvar
+i
+1]);
3277 value_addto(row
->p
[len
-1], row
->p
[len
-1], f
);
3278 value_multiply(f
, f
, P
->Constraint
[l
][nvar
+i
+1]);
3279 value_subtract(row
->p
[len
-1], row
->p
[len
-1], f
);
3280 value_decrement(row
->p
[len
-1], row
->p
[len
-1]);
3281 ConstraintSimplify(row
->p
, row
->p
, len
, &f
);
3282 value_set_si(f
, -1);
3283 Vector_Scale(row
->p
+1, row
->p
+1, f
, len
-1);
3284 value_decrement(row
->p
[len
-1], row
->p
[len
-1]);
3285 Polyhedron
*T
= AddConstraints(row
->p
, 1, P
, options
->MaxRays
);
3287 //printf("not all_pos: i: %d, l: %d, u: %d\n", i, l, u);
3288 info
[i
] = (constraint
)(info
[i
] ^ ALL_POS
);
3290 //puts("pos remainder");
3291 //Polyhedron_Print(stdout, P_VALUE_FMT, T);
3294 if (!(info
[i
] & ONE_NEG
)) {
3296 negative_test_constraint(P
->Constraint
[l
],
3298 row
->p
, nvar
+i
, len
, &f
);
3299 oppose_constraint(row
->p
, len
, &f
);
3300 Polyhedron
*T
= AddConstraints(row
->p
, 1, P
,
3303 //printf("one_neg i: %d, l: %d, u: %d\n", i, l, u);
3304 info
[i
] = (constraint
)(info
[i
] | ONE_NEG
);
3306 //puts("neg remainder");
3307 //Polyhedron_Print(stdout, P_VALUE_FMT, T);
3309 } else if (!(info
[i
] & ROT_NEG
)) {
3310 if (parallel_constraints(P
->Constraint
[l
],
3312 row
->p
, nvar
, exist
)) {
3313 negative_test_constraint7(P
->Constraint
[l
],
3315 row
->p
, nvar
, exist
,
3317 oppose_constraint(row
->p
, len
, &f
);
3318 Polyhedron
*T
= AddConstraints(row
->p
, 1, P
,
3321 // printf("rot_neg i: %d, l: %d, u: %d\n", i, l, u);
3322 info
[i
] = (constraint
)(info
[i
] | ROT_NEG
);
3325 //puts("neg remainder");
3326 //Polyhedron_Print(stdout, P_VALUE_FMT, T);
3331 if (!(info
[i
] & ALL_POS
) && (info
[i
] & (ONE_NEG
| ROT_NEG
)))
3335 if (info
[i
] & ALL_POS
)
3342 for (int i = 0; i < exist; ++i)
3343 printf("%i: %i\n", i, info[i]);
3345 for (int i
= 0; i
< exist
; ++i
)
3346 if (info
[i
] & ALL_POS
) {
3348 fprintf(stderr
, "\nER: Positive\n");
3349 #endif /* DEBUG_ER */
3351 // Maybe we should chew off some of the fat here
3352 Matrix
*M
= Matrix_Alloc(P
->Dimension
, P
->Dimension
+1);
3353 for (int j
= 0; j
< P
->Dimension
; ++j
)
3354 value_set_si(M
->p
[j
][j
+ (j
>= i
+nvar
)], 1);
3355 Polyhedron
*T
= Polyhedron_Image(P
, M
, options
->MaxRays
);
3357 evalue
*EP
= barvinok_enumerate_e_with_options(T
, exist
-1, nparam
,
3365 for (int i
= 0; i
< exist
; ++i
)
3366 if (info
[i
] & ONE_NEG
) {
3368 fprintf(stderr
, "\nER: Negative\n");
3369 #endif /* DEBUG_ER */
3374 return barvinok_enumerate_e_with_options(P
, exist
-1, nparam
,
3377 Polyhedron
*T
= Polyhedron_Copy(P
);
3378 SwapColumns(T
, nvar
+1, nvar
+1+i
);
3379 evalue
*EP
= barvinok_enumerate_e_with_options(T
, exist
-1, nparam
,
3385 for (int i
= 0; i
< exist
; ++i
)
3386 if (info
[i
] & ROT_NEG
) {
3388 fprintf(stderr
, "\nER: Rotate\n");
3389 #endif /* DEBUG_ER */
3393 Polyhedron
*T
= rotate_along(P
, r
, nvar
, exist
, options
->MaxRays
);
3394 evalue
*EP
= barvinok_enumerate_e_with_options(T
, exist
-1, nparam
,
3399 for (int i
= 0; i
< exist
; ++i
)
3400 if (info
[i
] & INDEPENDENT
) {
3401 Polyhedron
*pos
, *neg
;
3403 /* Find constraint again and split off negative part */
3405 if (SplitOnVar(P
, i
, nvar
, exist
, options
->MaxRays
,
3406 row
, f
, true, &pos
, &neg
)) {
3408 fprintf(stderr
, "\nER: Split\n");
3409 #endif /* DEBUG_ER */
3412 barvinok_enumerate_e_with_options(neg
, exist
-1, nparam
, options
);
3414 barvinok_enumerate_e_with_options(pos
, exist
, nparam
, options
);
3416 free_evalue_refs(E
);
3418 Polyhedron_Free(neg
);
3419 Polyhedron_Free(pos
);
3433 EP
= enumerate_line(P
, exist
, nparam
, options
);
3437 EP
= barvinok_enumerate_pip(P
, exist
, nparam
, options
->MaxRays
);
3441 EP
= enumerate_redundant_ray(P
, exist
, nparam
, options
);
3445 EP
= enumerate_sure(P
, exist
, nparam
, options
);
3449 EP
= enumerate_ray(P
, exist
, nparam
, options
);
3453 EP
= enumerate_sure2(P
, exist
, nparam
, options
);
3457 F
= unfringe(P
, options
->MaxRays
);
3458 if (!PolyhedronIncludes(F
, P
)) {
3460 fprintf(stderr
, "\nER: Fringed\n");
3461 #endif /* DEBUG_ER */
3462 EP
= barvinok_enumerate_e_with_options(F
, exist
, nparam
, options
);
3469 EP
= enumerate_vd(&P
, exist
, nparam
, options
);
3474 EP
= enumerate_sum(P
, exist
, nparam
, options
);
3481 Polyhedron
*pos
, *neg
;
3482 for (i
= 0; i
< exist
; ++i
)
3483 if (SplitOnVar(P
, i
, nvar
, exist
, options
->MaxRays
,
3484 row
, f
, false, &pos
, &neg
))
3490 EP
= enumerate_or(pos
, exist
, nparam
, options
);
3503 * remove equalities that require a "compression" of the parameters
3505 static Polyhedron
*remove_more_equalities(Polyhedron
*P
, unsigned nparam
,
3506 Matrix
**CP
, unsigned MaxRays
)
3509 remove_all_equalities(&P
, NULL
, CP
, NULL
, nparam
, MaxRays
);
3516 static gen_fun
*series(Polyhedron
*P
, unsigned nparam
, barvinok_options
*options
)
3526 assert(!Polyhedron_is_infinite_param(P
, nparam
));
3527 assert(P
->NbBid
== 0);
3528 assert(Polyhedron_has_positive_rays(P
, nparam
));
3530 P
= remove_more_equalities(P
, nparam
, &CP
, options
->MaxRays
);
3531 assert(P
->NbEq
== 0);
3533 nparam
= CP
->NbColumns
-1;
3538 barvinok_count(P
, &c
, options
->MaxRays
);
3539 gf
= new gen_fun(c
);
3543 red
= gf_base::create(Polyhedron_Project(P
, nparam
),
3544 P
->Dimension
, nparam
, options
);
3545 POL_ENSURE_VERTICES(P
);
3546 red
->start_gf(P
, options
);
3558 gen_fun
* barvinok_series_with_options(Polyhedron
*P
, Polyhedron
* C
,
3559 barvinok_options
*options
)
3562 unsigned nparam
= C
->Dimension
;
3565 CA
= align_context(C
, P
->Dimension
, options
->MaxRays
);
3566 P
= DomainIntersection(P
, CA
, options
->MaxRays
);
3567 Polyhedron_Free(CA
);
3569 gf
= series(P
, nparam
, options
);
3574 gen_fun
* barvinok_series(Polyhedron
*P
, Polyhedron
* C
, unsigned MaxRays
)
3577 barvinok_options
*options
= barvinok_options_new_with_defaults();
3578 options
->MaxRays
= MaxRays
;
3579 gf
= barvinok_series_with_options(P
, C
, options
);
3584 static Polyhedron
*skew_into_positive_orthant(Polyhedron
*D
, unsigned nparam
,
3590 for (Polyhedron
*P
= D
; P
; P
= P
->next
) {
3591 POL_ENSURE_VERTICES(P
);
3592 assert(!Polyhedron_is_infinite_param(P
, nparam
));
3593 assert(P
->NbBid
== 0);
3594 assert(Polyhedron_has_positive_rays(P
, nparam
));
3596 for (int r
= 0; r
< P
->NbRays
; ++r
) {
3597 if (value_notzero_p(P
->Ray
[r
][P
->Dimension
+1]))
3599 for (int i
= 0; i
< nparam
; ++i
) {
3601 if (value_posz_p(P
->Ray
[r
][i
+1]))
3604 M
= Matrix_Alloc(D
->Dimension
+1, D
->Dimension
+1);
3605 for (int i
= 0; i
< D
->Dimension
+1; ++i
)
3606 value_set_si(M
->p
[i
][i
], 1);
3608 Inner_Product(P
->Ray
[r
]+1, M
->p
[i
], D
->Dimension
+1, &tmp
);
3609 if (value_posz_p(tmp
))
3612 for (j
= P
->Dimension
- nparam
; j
< P
->Dimension
; ++j
)
3613 if (value_pos_p(P
->Ray
[r
][j
+1]))
3615 assert(j
< P
->Dimension
);
3616 value_pdivision(tmp
, P
->Ray
[r
][j
+1], P
->Ray
[r
][i
+1]);
3617 value_subtract(M
->p
[i
][j
], M
->p
[i
][j
], tmp
);
3623 D
= DomainImage(D
, M
, MaxRays
);
3629 gen_fun
* barvinok_enumerate_union_series_with_options(Polyhedron
*D
, Polyhedron
* C
,
3630 barvinok_options
*options
)
3632 Polyhedron
*conv
, *D2
;
3634 gen_fun
*gf
= NULL
, *gf2
;
3635 unsigned nparam
= C
->Dimension
;
3640 CA
= align_context(C
, D
->Dimension
, options
->MaxRays
);
3641 D
= DomainIntersection(D
, CA
, options
->MaxRays
);
3642 Polyhedron_Free(CA
);
3644 D2
= skew_into_positive_orthant(D
, nparam
, options
->MaxRays
);
3645 for (Polyhedron
*P
= D2
; P
; P
= P
->next
) {
3646 assert(P
->Dimension
== D2
->Dimension
);
3649 P_gf
= series(Polyhedron_Copy(P
), nparam
, options
);
3653 gf
->add_union(P_gf
, options
);
3657 /* we actually only need the convex union of the parameter space
3658 * but the reducer classes currently expect a polyhedron in
3659 * the combined space
3661 Polyhedron_Free(gf
->context
);
3662 gf
->context
= DomainConvex(D2
, options
->MaxRays
);
3664 gf2
= gf
->summate(D2
->Dimension
- nparam
, options
);
3673 gen_fun
* barvinok_enumerate_union_series(Polyhedron
*D
, Polyhedron
* C
,
3677 barvinok_options
*options
= barvinok_options_new_with_defaults();
3678 options
->MaxRays
= MaxRays
;
3679 gf
= barvinok_enumerate_union_series_with_options(D
, C
, options
);
3684 evalue
* barvinok_enumerate_union(Polyhedron
*D
, Polyhedron
* C
, unsigned MaxRays
)
3687 gen_fun
*gf
= barvinok_enumerate_union_series(D
, C
, MaxRays
);