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 "conversion.h"
20 #include "decomposer.h"
21 #include "lattice_point.h"
22 #include "reduce_domain.h"
33 using std::ostringstream
;
35 #define ALLOC(t,p) p = (t*)malloc(sizeof(*p))
37 static void rays(mat_ZZ
& r
, Polyhedron
*C
)
39 unsigned dim
= C
->NbRays
- 1; /* don't count zero vertex */
40 assert(C
->NbRays
- 1 == C
->Dimension
);
45 for (i
= 0, c
= 0; i
< dim
; ++i
)
46 if (value_zero_p(C
->Ray
[i
][dim
+1])) {
47 for (int j
= 0; j
< dim
; ++j
) {
48 value2zz(C
->Ray
[i
][j
+1], tmp
);
58 dpoly(int d
, ZZ
& degree
, int offset
= 0) {
62 if (degree
>= 0 && degree
< ZZ(INIT_VAL
, min
))
65 ZZ c
= ZZ(INIT_VAL
, 1);
68 for (int i
= 1; i
<= min
; ++i
) {
74 void operator *= (dpoly
& f
) {
75 assert(coeff
.length() == f
.coeff
.length());
77 coeff
= f
.coeff
[0] * coeff
;
78 for (int i
= 1; i
< coeff
.length(); ++i
)
79 for (int j
= 0; i
+j
< coeff
.length(); ++j
)
80 coeff
[i
+j
] += f
.coeff
[i
] * old
[j
];
82 void div(dpoly
& d
, mpq_t count
, ZZ
& sign
) {
83 int len
= coeff
.length();
86 mpq_t
* c
= new mpq_t
[coeff
.length()];
89 for (int i
= 0; i
< len
; ++i
) {
91 zz2value(coeff
[i
], tmp
);
94 for (int j
= 1; j
<= i
; ++j
) {
95 zz2value(d
.coeff
[j
], tmp
);
97 mpq_mul(qtmp
, qtmp
, c
[i
-j
]);
98 mpq_sub(c
[i
], c
[i
], qtmp
);
101 zz2value(d
.coeff
[0], tmp
);
102 mpq_set_z(qtmp
, tmp
);
103 mpq_div(c
[i
], c
[i
], qtmp
);
106 mpq_sub(count
, count
, c
[len
-1]);
108 mpq_add(count
, count
, c
[len
-1]);
112 for (int i
= 0; i
< len
; ++i
)
124 dpoly_n(int d
, ZZ
& degree_0
, ZZ
& degree_1
, int offset
= 0) {
128 zz2value(degree_0
, d0
);
129 zz2value(degree_1
, d1
);
130 coeff
= Matrix_Alloc(d
+1, d
+1+1);
131 value_set_si(coeff
->p
[0][0], 1);
132 value_set_si(coeff
->p
[0][d
+1], 1);
133 for (int i
= 1; i
<= d
; ++i
) {
134 value_multiply(coeff
->p
[i
][0], coeff
->p
[i
-1][0], d0
);
135 Vector_Combine(coeff
->p
[i
-1], coeff
->p
[i
-1]+1, coeff
->p
[i
]+1,
137 value_set_si(coeff
->p
[i
][d
+1], i
);
138 value_multiply(coeff
->p
[i
][d
+1], coeff
->p
[i
][d
+1], coeff
->p
[i
-1][d
+1]);
139 value_decrement(d0
, d0
);
144 void div(dpoly
& d
, Vector
*count
, ZZ
& sign
) {
145 int len
= coeff
->NbRows
;
146 Matrix
* c
= Matrix_Alloc(coeff
->NbRows
, coeff
->NbColumns
);
149 for (int i
= 0; i
< len
; ++i
) {
150 Vector_Copy(coeff
->p
[i
], c
->p
[i
], len
+1);
151 for (int j
= 1; j
<= i
; ++j
) {
152 zz2value(d
.coeff
[j
], tmp
);
153 value_multiply(tmp
, tmp
, c
->p
[i
][len
]);
154 value_oppose(tmp
, tmp
);
155 Vector_Combine(c
->p
[i
], c
->p
[i
-j
], c
->p
[i
],
156 c
->p
[i
-j
][len
], tmp
, len
);
157 value_multiply(c
->p
[i
][len
], c
->p
[i
][len
], c
->p
[i
-j
][len
]);
159 zz2value(d
.coeff
[0], tmp
);
160 value_multiply(c
->p
[i
][len
], c
->p
[i
][len
], tmp
);
163 value_set_si(tmp
, -1);
164 Vector_Scale(c
->p
[len
-1], count
->p
, tmp
, len
);
165 value_assign(count
->p
[len
], c
->p
[len
-1][len
]);
167 Vector_Copy(c
->p
[len
-1], count
->p
, len
+1);
168 Vector_Normalize(count
->p
, len
+1);
174 struct dpoly_r_term
{
179 /* len: number of elements in c
180 * each element in c is the coefficient of a power of t
181 * in the MacLaurin expansion
184 vector
< dpoly_r_term
* > *c
;
189 void add_term(int i
, int * powers
, ZZ
& coeff
) {
192 for (int k
= 0; k
< c
[i
].size(); ++k
) {
193 if (memcmp(c
[i
][k
]->powers
, powers
, dim
* sizeof(int)) == 0) {
194 c
[i
][k
]->coeff
+= coeff
;
198 dpoly_r_term
*t
= new dpoly_r_term
;
199 t
->powers
= new int[dim
];
200 memcpy(t
->powers
, powers
, dim
* sizeof(int));
204 dpoly_r(int len
, int dim
) {
208 c
= new vector
< dpoly_r_term
* > [len
];
210 dpoly_r(dpoly
& num
, int dim
) {
212 len
= num
.coeff
.length();
213 c
= new vector
< dpoly_r_term
* > [len
];
216 memset(powers
, 0, dim
* sizeof(int));
218 for (int i
= 0; i
< len
; ++i
) {
219 ZZ coeff
= num
.coeff
[i
];
220 add_term(i
, powers
, coeff
);
223 dpoly_r(dpoly
& num
, dpoly
& den
, int pos
, int dim
) {
225 len
= num
.coeff
.length();
226 c
= new vector
< dpoly_r_term
* > [len
];
230 for (int i
= 0; i
< len
; ++i
) {
231 ZZ coeff
= num
.coeff
[i
];
232 memset(powers
, 0, dim
* sizeof(int));
235 add_term(i
, powers
, coeff
);
237 for (int j
= 1; j
<= i
; ++j
) {
238 for (int k
= 0; k
< c
[i
-j
].size(); ++k
) {
239 memcpy(powers
, c
[i
-j
][k
]->powers
, dim
*sizeof(int));
241 coeff
= -den
.coeff
[j
-1] * c
[i
-j
][k
]->coeff
;
242 add_term(i
, powers
, coeff
);
248 dpoly_r(dpoly_r
* num
, dpoly
& den
, int pos
, int dim
) {
251 c
= new vector
< dpoly_r_term
* > [len
];
256 for (int i
= 0 ; i
< len
; ++i
) {
257 for (int k
= 0; k
< num
->c
[i
].size(); ++k
) {
258 memcpy(powers
, num
->c
[i
][k
]->powers
, dim
*sizeof(int));
260 add_term(i
, powers
, num
->c
[i
][k
]->coeff
);
263 for (int j
= 1; j
<= i
; ++j
) {
264 for (int k
= 0; k
< c
[i
-j
].size(); ++k
) {
265 memcpy(powers
, c
[i
-j
][k
]->powers
, dim
*sizeof(int));
267 coeff
= -den
.coeff
[j
-1] * c
[i
-j
][k
]->coeff
;
268 add_term(i
, powers
, coeff
);
274 for (int i
= 0 ; i
< len
; ++i
)
275 for (int k
= 0; k
< c
[i
].size(); ++k
) {
276 delete [] c
[i
][k
]->powers
;
281 dpoly_r
*div(dpoly
& d
) {
282 dpoly_r
*rc
= new dpoly_r(len
, dim
);
283 rc
->denom
= power(d
.coeff
[0], len
);
284 ZZ inv_d
= rc
->denom
/ d
.coeff
[0];
287 for (int i
= 0; i
< len
; ++i
) {
288 for (int k
= 0; k
< c
[i
].size(); ++k
) {
289 coeff
= c
[i
][k
]->coeff
* inv_d
;
290 rc
->add_term(i
, c
[i
][k
]->powers
, coeff
);
293 for (int j
= 1; j
<= i
; ++j
) {
294 for (int k
= 0; k
< rc
->c
[i
-j
].size(); ++k
) {
295 coeff
= - d
.coeff
[j
] * rc
->c
[i
-j
][k
]->coeff
/ d
.coeff
[0];
296 rc
->add_term(i
, rc
->c
[i
-j
][k
]->powers
, coeff
);
303 for (int i
= 0; i
< len
; ++i
) {
306 cerr
<< c
[i
].size() << endl
;
307 for (int j
= 0; j
< c
[i
].size(); ++j
) {
308 for (int k
= 0; k
< dim
; ++k
) {
309 cerr
<< c
[i
][j
]->powers
[k
] << " ";
311 cerr
<< ": " << c
[i
][j
]->coeff
<< "/" << denom
<< endl
;
318 const int MAX_TRY
=10;
320 * Searches for a vector that is not orthogonal to any
321 * of the rays in rays.
323 static void nonorthog(mat_ZZ
& rays
, vec_ZZ
& lambda
)
325 int dim
= rays
.NumCols();
327 lambda
.SetLength(dim
);
331 for (int i
= 2; !found
&& i
<= 50*dim
; i
+=4) {
332 for (int j
= 0; j
< MAX_TRY
; ++j
) {
333 for (int k
= 0; k
< dim
; ++k
) {
334 int r
= random_int(i
)+2;
335 int v
= (2*(r
%2)-1) * (r
>> 1);
339 for (; k
< rays
.NumRows(); ++k
)
340 if (lambda
* rays
[k
] == 0)
342 if (k
== rays
.NumRows()) {
351 static void randomvector(Polyhedron
*P
, vec_ZZ
& lambda
, int nvar
)
355 unsigned int dim
= P
->Dimension
;
358 for (int i
= 0; i
< P
->NbRays
; ++i
) {
359 for (int j
= 1; j
<= dim
; ++j
) {
360 value_absolute(tmp
, P
->Ray
[i
][j
]);
361 int t
= VALUE_TO_LONG(tmp
) * 16;
366 for (int i
= 0; i
< P
->NbConstraints
; ++i
) {
367 for (int j
= 1; j
<= dim
; ++j
) {
368 value_absolute(tmp
, P
->Constraint
[i
][j
]);
369 int t
= VALUE_TO_LONG(tmp
) * 16;
376 lambda
.SetLength(nvar
);
377 for (int k
= 0; k
< nvar
; ++k
) {
378 int r
= random_int(max
*dim
)+2;
379 int v
= (2*(r
%2)-1) * (max
/2*dim
+ (r
>> 1));
384 static void add_rays(mat_ZZ
& rays
, Polyhedron
*i
, int *r
, int nvar
= -1,
387 unsigned dim
= i
->Dimension
;
390 for (int k
= 0; k
< i
->NbRays
; ++k
) {
391 if (!value_zero_p(i
->Ray
[k
][dim
+1]))
393 if (!all
&& nvar
!= dim
&& First_Non_Zero(i
->Ray
[k
]+1, nvar
) == -1)
395 values2zz(i
->Ray
[k
]+1, rays
[(*r
)++], nvar
);
399 static void mask_r(Matrix
*f
, int nr
, Vector
*lcm
, int p
, Vector
*val
, evalue
*ev
)
401 unsigned nparam
= lcm
->Size
;
404 Vector
* prod
= Vector_Alloc(f
->NbRows
);
405 Matrix_Vector_Product(f
, val
->p
, prod
->p
);
407 for (int i
= 0; i
< nr
; ++i
) {
408 value_modulus(prod
->p
[i
], prod
->p
[i
], f
->p
[i
][nparam
+1]);
409 isint
&= value_zero_p(prod
->p
[i
]);
411 value_set_si(ev
->d
, 1);
413 value_set_si(ev
->x
.n
, isint
);
420 if (value_one_p(lcm
->p
[p
]))
421 mask_r(f
, nr
, lcm
, p
+1, val
, ev
);
423 value_assign(tmp
, lcm
->p
[p
]);
424 value_set_si(ev
->d
, 0);
425 ev
->x
.p
= new_enode(periodic
, VALUE_TO_INT(tmp
), p
+1);
427 value_decrement(tmp
, tmp
);
428 value_assign(val
->p
[p
], tmp
);
429 mask_r(f
, nr
, lcm
, p
+1, val
, &ev
->x
.p
->arr
[VALUE_TO_INT(tmp
)]);
430 } while (value_pos_p(tmp
));
436 static void mask(Matrix
*f
, evalue
*factor
)
438 int nr
= f
->NbRows
, nc
= f
->NbColumns
;
441 for (n
= 0; n
< nr
&& value_notzero_p(f
->p
[n
][nc
-1]); ++n
)
442 if (value_notone_p(f
->p
[n
][nc
-1]) &&
443 value_notmone_p(f
->p
[n
][nc
-1]))
457 value_set_si(EV
.x
.n
, 1);
459 for (n
= 0; n
< nr
; ++n
) {
460 value_assign(m
, f
->p
[n
][nc
-1]);
461 if (value_one_p(m
) || value_mone_p(m
))
464 int j
= normal_mod(f
->p
[n
], nc
-1, &m
);
466 free_evalue_refs(factor
);
467 value_init(factor
->d
);
468 evalue_set_si(factor
, 0, 1);
472 values2zz(f
->p
[n
], row
, nc
-1);
475 if (j
< (nc
-1)-1 && row
[j
] > g
/2) {
476 for (int k
= j
; k
< (nc
-1); ++k
)
482 value_set_si(EP
.d
, 0);
483 EP
.x
.p
= new_enode(relation
, 2, 0);
484 value_clear(EP
.x
.p
->arr
[1].d
);
485 EP
.x
.p
->arr
[1] = *factor
;
486 evalue
*ev
= &EP
.x
.p
->arr
[0];
487 value_set_si(ev
->d
, 0);
488 ev
->x
.p
= new_enode(fractional
, 3, -1);
489 evalue_set_si(&ev
->x
.p
->arr
[1], 0, 1);
490 evalue_set_si(&ev
->x
.p
->arr
[2], 1, 1);
491 evalue
*E
= multi_monom(row
);
492 value_assign(EV
.d
, m
);
494 value_clear(ev
->x
.p
->arr
[0].d
);
495 ev
->x
.p
->arr
[0] = *E
;
501 free_evalue_refs(&EV
);
507 static void mask(Matrix
*f
, evalue
*factor
)
509 int nr
= f
->NbRows
, nc
= f
->NbColumns
;
512 for (n
= 0; n
< nr
&& value_notzero_p(f
->p
[n
][nc
-1]); ++n
)
513 if (value_notone_p(f
->p
[n
][nc
-1]) &&
514 value_notmone_p(f
->p
[n
][nc
-1]))
522 unsigned np
= nc
- 2;
523 Vector
*lcm
= Vector_Alloc(np
);
524 Vector
*val
= Vector_Alloc(nc
);
525 Vector_Set(val
->p
, 0, nc
);
526 value_set_si(val
->p
[np
], 1);
527 Vector_Set(lcm
->p
, 1, np
);
528 for (n
= 0; n
< nr
; ++n
) {
529 if (value_one_p(f
->p
[n
][nc
-1]) ||
530 value_mone_p(f
->p
[n
][nc
-1]))
532 for (int j
= 0; j
< np
; ++j
)
533 if (value_notzero_p(f
->p
[n
][j
])) {
534 Gcd(f
->p
[n
][j
], f
->p
[n
][nc
-1], &tmp
);
535 value_division(tmp
, f
->p
[n
][nc
-1], tmp
);
536 value_lcm(tmp
, lcm
->p
[j
], &lcm
->p
[j
]);
541 mask_r(f
, nr
, lcm
, 0, val
, &EP
);
546 free_evalue_refs(&EP
);
550 /* This structure encodes the power of the term in a rational generating function.
552 * Either E == NULL or constant = 0
553 * If E != NULL, then the power is E
554 * If E == NULL, then the power is coeff * param[pos] + constant
563 /* Returns the power of (t+1) in the term of a rational generating function,
564 * i.e., the scalar product of the actual lattice point and lambda.
565 * The lattice point is the unique lattice point in the fundamental parallelepiped
566 * of the unimodual cone i shifted to the parametric vertex V.
568 * PD is the parameter domain, which, if != NULL, may be used to simply the
569 * resulting expression.
571 * The result is returned in term.
574 Param_Vertices
* V
, Polyhedron
*i
, vec_ZZ
& lambda
, term_info
* term
,
577 unsigned nparam
= V
->Vertex
->NbColumns
- 2;
578 unsigned dim
= i
->Dimension
;
580 vertex
.SetDims(V
->Vertex
->NbRows
, nparam
+1);
584 value_set_si(lcm
, 1);
585 for (int j
= 0; j
< V
->Vertex
->NbRows
; ++j
) {
586 value_lcm(lcm
, V
->Vertex
->p
[j
][nparam
+1], &lcm
);
588 if (value_notone_p(lcm
)) {
589 Matrix
* mv
= Matrix_Alloc(dim
, nparam
+1);
590 for (int j
= 0 ; j
< dim
; ++j
) {
591 value_division(tmp
, lcm
, V
->Vertex
->p
[j
][nparam
+1]);
592 Vector_Scale(V
->Vertex
->p
[j
], mv
->p
[j
], tmp
, nparam
+1);
595 term
->E
= lattice_point(i
, lambda
, mv
, lcm
, PD
);
603 for (int i
= 0; i
< V
->Vertex
->NbRows
; ++i
) {
604 assert(value_one_p(V
->Vertex
->p
[i
][nparam
+1])); // for now
605 values2zz(V
->Vertex
->p
[i
], vertex
[i
], nparam
+1);
609 num
= lambda
* vertex
;
613 for (int j
= 0; j
< nparam
; ++j
)
619 term
->E
= multi_monom(num
);
623 term
->constant
= num
[nparam
];
626 term
->coeff
= num
[p
];
633 static void normalize(ZZ
& sign
, ZZ
& num
, vec_ZZ
& den
)
635 unsigned dim
= den
.length();
639 for (int j
= 0; j
< den
.length(); ++j
) {
643 den
[j
] = abs(den
[j
]);
652 * f: the powers in the denominator for the remaining vars
653 * each row refers to a factor
654 * den_s: for each factor, the power of (s+1)
656 * num_s: powers in the numerator corresponding to the summed vars
657 * num_p: powers in the numerator corresponding to the remaining vars
658 * number of rays in cone: "dim" = "k"
659 * length of each ray: "dim" = "d"
660 * for now, it is assumed: k == d
662 * den_p: for each factor
663 * 0: independent of remaining vars
664 * 1: power corresponds to corresponding row in f
666 * all inputs are subject to change
668 static void normalize(ZZ
& sign
,
669 ZZ
& num_s
, vec_ZZ
& num_p
, vec_ZZ
& den_s
, vec_ZZ
& den_p
,
672 unsigned dim
= f
.NumRows();
673 unsigned nparam
= num_p
.length();
674 unsigned nvar
= dim
- nparam
;
678 for (int j
= 0; j
< den_s
.length(); ++j
) {
684 for (k
= 0; k
< nparam
; ++k
)
698 den_s
[j
] = abs(den_s
[j
]);
707 struct counter
: public polar_decomposer
{
719 counter(Polyhedron
*P
) {
722 rays
.SetDims(dim
, dim
);
727 void start(unsigned MaxRays
);
733 virtual void handle_polar(Polyhedron
*P
, int sign
);
736 struct OrthogonalException
{} Orthogonal
;
738 void counter::handle_polar(Polyhedron
*C
, int s
)
741 assert(C
->NbRays
-1 == dim
);
742 add_rays(rays
, C
, &r
);
743 for (int k
= 0; k
< dim
; ++k
) {
744 if (lambda
* rays
[k
] == 0)
750 lattice_point(P
->Ray
[j
]+1, C
, vertex
);
751 num
= vertex
* lambda
;
753 normalize(sign
, num
, den
);
756 dpoly
n(dim
, den
[0], 1);
757 for (int k
= 1; k
< dim
; ++k
) {
758 dpoly
fact(dim
, den
[k
], 1);
761 d
.div(n
, count
, sign
);
764 void counter::start(unsigned MaxRays
)
768 randomvector(P
, lambda
, dim
);
769 for (j
= 0; j
< P
->NbRays
; ++j
) {
770 Polyhedron
*C
= supporting_cone(P
, j
);
771 decompose(C
, MaxRays
);
774 } catch (OrthogonalException
&e
) {
775 mpq_set_si(count
, 0, 0);
780 /* base for non-parametric counting */
781 struct np_base
: public polar_decomposer
{
786 np_base(Polyhedron
*P
, unsigned dim
) {
792 struct reducer
: public np_base
{
801 int lower
; // call base when only this many variables is left
803 reducer(Polyhedron
*P
) : np_base(P
, P
->Dimension
) {
804 //den.SetLength(dim);
811 void start(unsigned MaxRays
);
819 virtual void handle_polar(Polyhedron
*P
, int sign
);
820 void reduce(ZZ c
, ZZ cd
, vec_ZZ
& num
, mat_ZZ
& den_f
);
821 virtual void base(ZZ
& c
, ZZ
& cd
, vec_ZZ
& num
, mat_ZZ
& den_f
) = 0;
822 virtual void split(vec_ZZ
& num
, ZZ
& num_s
, vec_ZZ
& num_p
,
823 mat_ZZ
& den_f
, vec_ZZ
& den_s
, mat_ZZ
& den_r
) = 0;
826 void reducer::reduce(ZZ c
, ZZ cd
, vec_ZZ
& num
, mat_ZZ
& den_f
)
828 unsigned len
= den_f
.NumRows(); // number of factors in den
830 if (num
.length() == lower
) {
831 base(c
, cd
, num
, den_f
);
834 assert(num
.length() > 1);
841 split(num
, num_s
, num_p
, den_f
, den_s
, den_r
);
844 den_p
.SetLength(len
);
846 normalize(c
, num_s
, num_p
, den_s
, den_p
, den_r
);
848 int only_param
= 0; // k-r-s from text
849 int no_param
= 0; // r from text
850 for (int k
= 0; k
< len
; ++k
) {
853 else if (den_s
[k
] == 0)
857 reduce(c
, cd
, num_p
, den_r
);
861 pden
.SetDims(only_param
, den_r
.NumCols());
863 for (k
= 0, l
= 0; k
< len
; ++k
)
865 pden
[l
++] = den_r
[k
];
867 for (k
= 0; k
< len
; ++k
)
871 dpoly
n(no_param
, num_s
);
872 dpoly
D(no_param
, den_s
[k
], 1);
875 dpoly
fact(no_param
, den_s
[k
], 1);
879 if (no_param
+ only_param
== len
) {
880 mpq_set_si(tcount
, 0, 1);
881 n
.div(D
, tcount
, one
);
884 value2zz(mpq_numref(tcount
), qn
);
885 value2zz(mpq_denref(tcount
), qd
);
891 reduce(qn
, qd
, num_p
, pden
);
895 for (k
= 0; k
< len
; ++k
) {
896 if (den_s
[k
] == 0 || den_p
[k
] == 0)
899 dpoly
pd(no_param
-1, den_s
[k
], 1);
902 for (l
= 0; l
< k
; ++l
)
903 if (den_r
[l
] == den_r
[k
])
907 r
= new dpoly_r(n
, pd
, l
, len
);
909 dpoly_r
*nr
= new dpoly_r(r
, pd
, l
, len
);
915 dpoly_r
*rc
= r
->div(D
);
919 int common
= pden
.NumRows();
920 vector
< dpoly_r_term
* >& final
= rc
->c
[rc
->len
-1];
922 for (int j
= 0; j
< final
.size(); ++j
) {
923 if (final
[j
]->coeff
== 0)
926 pden
.SetDims(rows
, pden
.NumCols());
927 for (int k
= 0; k
< rc
->dim
; ++k
) {
928 int n
= final
[j
]->powers
[k
];
931 pden
.SetDims(rows
+n
, pden
.NumCols());
932 for (int l
= 0; l
< n
; ++l
)
933 pden
[rows
+l
] = den_r
[k
];
936 final
[j
]->coeff
*= c
;
937 reduce(final
[j
]->coeff
, rc
->denom
, num_p
, pden
);
946 void reducer::handle_polar(Polyhedron
*C
, int s
)
948 assert(C
->NbRays
-1 == dim
);
952 lattice_point(P
->Ray
[current_vertex
]+1, C
, vertex
);
955 den
.SetDims(dim
, dim
);
958 for (r
= 0; r
< dim
; ++r
)
959 values2zz(C
->Ray
[r
]+1, den
[r
], dim
);
961 reduce(sgn
, one
, vertex
, den
);
964 void reducer::start(unsigned MaxRays
)
966 for (current_vertex
= 0; current_vertex
< P
->NbRays
; ++current_vertex
) {
967 Polyhedron
*C
= supporting_cone(P
, current_vertex
);
968 decompose(C
, MaxRays
);
972 struct ireducer
: public reducer
{
973 ireducer(Polyhedron
*P
) : reducer(P
) {}
975 virtual void split(vec_ZZ
& num
, ZZ
& num_s
, vec_ZZ
& num_p
,
976 mat_ZZ
& den_f
, vec_ZZ
& den_s
, mat_ZZ
& den_r
) {
977 unsigned len
= den_f
.NumRows(); // number of factors in den
978 unsigned d
= num
.length() - 1;
980 den_s
.SetLength(len
);
981 den_r
.SetDims(len
, d
);
983 for (int r
= 0; r
< len
; ++r
) {
984 den_s
[r
] = den_f
[r
][0];
985 for (int k
= 1; k
<= d
; ++k
)
986 den_r
[r
][k
-1] = den_f
[r
][k
];
991 for (int k
= 1 ; k
<= d
; ++k
)
996 // incremental counter
997 struct icounter
: public ireducer
{
1000 icounter(Polyhedron
*P
) : ireducer(P
) {
1007 virtual void base(ZZ
& c
, ZZ
& cd
, vec_ZZ
& num
, mat_ZZ
& den_f
);
1010 void icounter::base(ZZ
& c
, ZZ
& cd
, vec_ZZ
& num
, mat_ZZ
& den_f
)
1013 unsigned len
= den_f
.NumRows(); // number of factors in den
1015 den_s
.SetLength(len
);
1017 for (r
= 0; r
< len
; ++r
)
1018 den_s
[r
] = den_f
[r
][0];
1019 normalize(c
, num_s
, den_s
);
1021 dpoly
n(len
, num_s
);
1022 dpoly
D(len
, den_s
[0], 1);
1023 for (int k
= 1; k
< len
; ++k
) {
1024 dpoly
fact(len
, den_s
[k
], 1);
1027 mpq_set_si(tcount
, 0, 1);
1028 n
.div(D
, tcount
, one
);
1031 mpz_mul(mpq_numref(tcount
), mpq_numref(tcount
), tn
);
1032 mpz_mul(mpq_denref(tcount
), mpq_denref(tcount
), td
);
1033 mpq_canonicalize(tcount
);
1034 mpq_add(count
, count
, tcount
);
1037 /* base for generating function counting */
1042 gf_base(np_base
*npb
, unsigned nparam
) : base(npb
) {
1043 gf
= new gen_fun(Polyhedron_Project(base
->P
, nparam
));
1045 void start(unsigned MaxRays
);
1048 void gf_base::start(unsigned MaxRays
)
1050 for (int i
= 0; i
< base
->P
->NbRays
; ++i
) {
1051 if (!value_pos_p(base
->P
->Ray
[i
][base
->dim
+1]))
1054 Polyhedron
*C
= supporting_cone(base
->P
, i
);
1055 base
->current_vertex
= i
;
1056 base
->decompose(C
, MaxRays
);
1060 struct partial_ireducer
: public ireducer
, public gf_base
{
1061 partial_ireducer(Polyhedron
*P
, unsigned nparam
) :
1062 ireducer(P
), gf_base(this, nparam
) {
1065 ~partial_ireducer() {
1067 virtual void base(ZZ
& c
, ZZ
& cd
, vec_ZZ
& num
, mat_ZZ
& den_f
);
1068 /* we want to override the start method from reducer with the one from gf_base */
1069 void start(unsigned MaxRays
) {
1070 gf_base::start(MaxRays
);
1074 void partial_ireducer::base(ZZ
& c
, ZZ
& cd
, vec_ZZ
& num
, mat_ZZ
& den_f
)
1076 gf
->add(c
, cd
, num
, den_f
);
1079 struct partial_reducer
: public reducer
, public gf_base
{
1083 partial_reducer(Polyhedron
*P
, unsigned nparam
) :
1084 reducer(P
), gf_base(this, nparam
) {
1087 tmp
.SetLength(dim
- nparam
);
1088 randomvector(P
, lambda
, dim
- nparam
);
1090 ~partial_reducer() {
1092 virtual void base(ZZ
& c
, ZZ
& cd
, vec_ZZ
& num
, mat_ZZ
& den_f
);
1093 /* we want to override the start method from reducer with the one from gf_base */
1094 void start(unsigned MaxRays
) {
1095 gf_base::start(MaxRays
);
1098 virtual void split(vec_ZZ
& num
, ZZ
& num_s
, vec_ZZ
& num_p
,
1099 mat_ZZ
& den_f
, vec_ZZ
& den_s
, mat_ZZ
& den_r
) {
1100 unsigned len
= den_f
.NumRows(); // number of factors in den
1101 unsigned nvar
= tmp
.length();
1103 den_s
.SetLength(len
);
1104 den_r
.SetDims(len
, lower
);
1106 for (int r
= 0; r
< len
; ++r
) {
1107 for (int k
= 0; k
< nvar
; ++k
)
1108 tmp
[k
] = den_f
[r
][k
];
1109 den_s
[r
] = tmp
* lambda
;
1111 for (int k
= nvar
; k
< dim
; ++k
)
1112 den_r
[r
][k
-nvar
] = den_f
[r
][k
];
1115 for (int k
= 0; k
< nvar
; ++k
)
1117 num_s
= tmp
*lambda
;
1118 num_p
.SetLength(lower
);
1119 for (int k
= nvar
; k
< dim
; ++k
)
1120 num_p
[k
-nvar
] = num
[k
];
1124 void partial_reducer::base(ZZ
& c
, ZZ
& cd
, vec_ZZ
& num
, mat_ZZ
& den_f
)
1126 gf
->add(c
, cd
, num
, den_f
);
1129 struct bfc_term_base
{
1130 // the number of times a given factor appears in the denominator
1134 bfc_term_base(int len
) {
1135 powers
= new int[len
];
1138 virtual ~bfc_term_base() {
1143 struct bfc_term
: public bfc_term_base
{
1147 bfc_term(int len
) : bfc_term_base(len
) {}
1150 struct bfe_term
: public bfc_term_base
{
1151 vector
<evalue
*> factors
;
1153 bfe_term(int len
) : bfc_term_base(len
) {
1157 for (int i
= 0; i
< factors
.size(); ++i
) {
1160 free_evalue_refs(factors
[i
]);
1166 typedef vector
< bfc_term_base
* > bfc_vec
;
1170 struct bf_base
: public np_base
{
1175 int lower
; // call base when only this many variables is left
1177 bf_base(Polyhedron
*P
, unsigned dim
) : np_base(P
, dim
) {
1190 void start(unsigned MaxRays
);
1191 virtual void handle_polar(Polyhedron
*P
, int sign
);
1192 int setup_factors(Polyhedron
*P
, mat_ZZ
& factors
, bfc_term_base
* t
, int s
);
1194 bfc_term_base
* find_bfc_term(bfc_vec
& v
, int *powers
, int len
);
1195 void add_term(bfc_term_base
*t
, vec_ZZ
& num1
, vec_ZZ
& num
);
1196 void add_term(bfc_term_base
*t
, vec_ZZ
& num
);
1198 void reduce(mat_ZZ
& factors
, bfc_vec
& v
);
1199 virtual void base(mat_ZZ
& factors
, bfc_vec
& v
) = 0;
1201 virtual bfc_term_base
* new_bf_term(int len
) = 0;
1202 virtual void set_factor(bfc_term_base
*t
, int k
, int change
) = 0;
1203 virtual void set_factor(bfc_term_base
*t
, int k
, mpq_t
&f
, int change
) = 0;
1204 virtual void set_factor(bfc_term_base
*t
, int k
, ZZ
& n
, ZZ
& d
, int change
) = 0;
1205 virtual void update_term(bfc_term_base
*t
, int i
) = 0;
1206 virtual void insert_term(bfc_term_base
*t
, int i
) = 0;
1207 virtual bool constant_vertex(int dim
) = 0;
1208 virtual void cum(bf_reducer
*bfr
, bfc_term_base
*t
, int k
,
1212 static int lex_cmp(vec_ZZ
& a
, vec_ZZ
& b
)
1214 assert(a
.length() == b
.length());
1216 for (int j
= 0; j
< a
.length(); ++j
)
1218 return a
[j
] < b
[j
] ? -1 : 1;
1222 void bf_base::add_term(bfc_term_base
*t
, vec_ZZ
& num_orig
, vec_ZZ
& extra_num
)
1225 int d
= num_orig
.length();
1227 for (int l
= 0; l
< d
-1; ++l
)
1228 num
[l
] = num_orig
[l
+1] + extra_num
[l
];
1233 void bf_base::add_term(bfc_term_base
*t
, vec_ZZ
& num
)
1235 int len
= t
->terms
.NumRows();
1237 for (i
= 0; i
< len
; ++i
) {
1238 r
= lex_cmp(t
->terms
[i
], num
);
1242 if (i
== len
|| r
> 0) {
1243 t
->terms
.SetDims(len
+1, num
.length());
1247 // i < len && r == 0
1252 static void print_int_vector(int *v
, int len
, char *name
)
1254 cerr
<< name
<< endl
;
1255 for (int j
= 0; j
< len
; ++j
) {
1256 cerr
<< v
[j
] << " ";
1261 static void print_bfc_terms(mat_ZZ
& factors
, bfc_vec
& v
)
1264 cerr
<< "factors" << endl
;
1265 cerr
<< factors
<< endl
;
1266 for (int i
= 0; i
< v
.size(); ++i
) {
1267 cerr
<< "term: " << i
<< endl
;
1268 print_int_vector(v
[i
]->powers
, factors
.NumRows(), "powers");
1269 cerr
<< "terms" << endl
;
1270 cerr
<< v
[i
]->terms
<< endl
;
1271 bfc_term
* bfct
= static_cast<bfc_term
*>(v
[i
]);
1272 cerr
<< bfct
->cn
<< endl
;
1273 cerr
<< bfct
->cd
<< endl
;
1277 static void print_bfe_terms(mat_ZZ
& factors
, bfc_vec
& v
)
1280 cerr
<< "factors" << endl
;
1281 cerr
<< factors
<< endl
;
1282 for (int i
= 0; i
< v
.size(); ++i
) {
1283 cerr
<< "term: " << i
<< endl
;
1284 print_int_vector(v
[i
]->powers
, factors
.NumRows(), "powers");
1285 cerr
<< "terms" << endl
;
1286 cerr
<< v
[i
]->terms
<< endl
;
1287 bfe_term
* bfet
= static_cast<bfe_term
*>(v
[i
]);
1288 for (int j
= 0; j
< v
[i
]->terms
.NumRows(); ++j
) {
1289 char * test
[] = {"a", "b"};
1290 print_evalue(stderr
, bfet
->factors
[j
], test
);
1291 fprintf(stderr
, "\n");
1296 bfc_term_base
* bf_base::find_bfc_term(bfc_vec
& v
, int *powers
, int len
)
1298 bfc_vec::iterator i
;
1299 for (i
= v
.begin(); i
!= v
.end(); ++i
) {
1301 for (j
= 0; j
< len
; ++j
)
1302 if ((*i
)->powers
[j
] != powers
[j
])
1306 if ((*i
)->powers
[j
] > powers
[j
])
1310 bfc_term_base
* t
= new_bf_term(len
);
1312 memcpy(t
->powers
, powers
, len
* sizeof(int));
1333 int no_param
; // r from text
1334 int only_param
; // k-r-s from text
1335 int total_power
; // k from text
1337 // created in compute_reduced_factors
1339 // set in update_powers
1344 bf_reducer(mat_ZZ
& factors
, bfc_vec
& v
, bf_base
*bf
)
1345 : factors(factors
), v(v
), bf(bf
) {
1346 nf
= factors
.NumRows();
1347 d
= factors
.NumCols();
1348 old2new
= new int[nf
];
1351 extra_num
.SetLength(d
-1);
1360 void compute_reduced_factors();
1361 void compute_extra_num(int i
);
1365 void update_powers(int *powers
, int len
);
1368 void bf_reducer::compute_extra_num(int i
)
1372 no_param
= 0; // r from text
1373 only_param
= 0; // k-r-s from text
1374 total_power
= 0; // k from text
1376 for (int j
= 0; j
< nf
; ++j
) {
1377 if (v
[i
]->powers
[j
] == 0)
1380 total_power
+= v
[i
]->powers
[j
];
1381 if (factors
[j
][0] == 0) {
1382 only_param
+= v
[i
]->powers
[j
];
1386 if (old2new
[j
] == -1)
1387 no_param
+= v
[i
]->powers
[j
];
1389 extra_num
+= -sign
[j
] * v
[i
]->powers
[j
] * nfactors
[old2new
[j
]];
1390 changes
+= v
[i
]->powers
[j
];
1394 void bf_reducer::update_powers(int *powers
, int len
)
1396 for (int l
= 0; l
< nnf
; ++l
)
1397 npowers
[l
] = bpowers
[l
];
1399 l_extra_num
= extra_num
;
1400 l_changes
= changes
;
1402 for (int l
= 0; l
< len
; ++l
) {
1406 assert(old2new
[l
] != -1);
1408 npowers
[old2new
[l
]] += n
;
1409 // interpretation of sign has been inverted
1410 // since we inverted the power for specialization
1412 l_extra_num
+= n
* nfactors
[old2new
[l
]];
1419 void bf_reducer::compute_reduced_factors()
1421 unsigned nf
= factors
.NumRows();
1422 unsigned d
= factors
.NumCols();
1424 nfactors
.SetDims(nnf
, d
-1);
1426 for (int i
= 0; i
< nf
; ++i
) {
1429 for (j
= 0; j
< nnf
; ++j
) {
1431 for (k
= 1; k
< d
; ++k
)
1432 if (factors
[i
][k
] != 0 || nfactors
[j
][k
-1] != 0)
1434 if (k
< d
&& factors
[i
][k
] == -nfactors
[j
][k
-1])
1437 if (factors
[i
][k
] != s
* nfactors
[j
][k
-1])
1445 for (k
= 1; k
< d
; ++k
)
1446 if (factors
[i
][k
] != 0)
1449 if (factors
[i
][k
] < 0)
1451 nfactors
.SetDims(++nnf
, d
-1);
1452 for (int k
= 1; k
< d
; ++k
)
1453 nfactors
[j
][k
-1] = s
* factors
[i
][k
];
1459 npowers
= new int[nnf
];
1460 bpowers
= new int[nnf
];
1463 void bf_reducer::reduce()
1465 compute_reduced_factors();
1467 for (int i
= 0; i
< v
.size(); ++i
) {
1468 compute_extra_num(i
);
1470 if (no_param
== 0) {
1472 extra_num
.SetLength(d
-1);
1475 for (int k
= 0; k
< nnf
; ++k
)
1477 for (int k
= 0; k
< nf
; ++k
) {
1478 assert(old2new
[k
] != -1);
1479 npowers
[old2new
[k
]] += v
[i
]->powers
[k
];
1480 if (sign
[k
] == -1) {
1481 extra_num
+= v
[i
]->powers
[k
] * nfactors
[old2new
[k
]];
1482 changes
+= v
[i
]->powers
[k
];
1486 bfc_term_base
* t
= bf
->find_bfc_term(vn
, npowers
, nnf
);
1487 for (int k
= 0; k
< v
[i
]->terms
.NumRows(); ++k
) {
1488 bf
->set_factor(v
[i
], k
, changes
% 2);
1489 bf
->add_term(t
, v
[i
]->terms
[k
], extra_num
);
1492 // powers of "constant" part
1493 for (int k
= 0; k
< nnf
; ++k
)
1495 for (int k
= 0; k
< nf
; ++k
) {
1496 if (factors
[k
][0] != 0)
1498 assert(old2new
[k
] != -1);
1499 bpowers
[old2new
[k
]] += v
[i
]->powers
[k
];
1500 if (sign
[k
] == -1) {
1501 extra_num
+= v
[i
]->powers
[k
] * nfactors
[old2new
[k
]];
1502 changes
+= v
[i
]->powers
[k
];
1507 for (j
= 0; j
< nf
; ++j
)
1508 if (old2new
[j
] == -1 && v
[i
]->powers
[j
] > 0)
1511 dpoly
D(no_param
, factors
[j
][0], 1);
1512 for (int k
= 1; k
< v
[i
]->powers
[j
]; ++k
) {
1513 dpoly
fact(no_param
, factors
[j
][0], 1);
1517 if (old2new
[j
] == -1)
1518 for (int k
= 0; k
< v
[i
]->powers
[j
]; ++k
) {
1519 dpoly
fact(no_param
, factors
[j
][0], 1);
1523 if (no_param
+ only_param
== total_power
&&
1524 bf
->constant_vertex(d
)) {
1525 bfc_term_base
* t
= NULL
;
1530 for (int k
= 0; k
< v
[i
]->terms
.NumRows(); ++k
) {
1531 dpoly
n(no_param
, v
[i
]->terms
[k
][0]);
1532 mpq_set_si(bf
->tcount
, 0, 1);
1533 n
.div(D
, bf
->tcount
, bf
->one
);
1535 if (value_zero_p(mpq_numref(bf
->tcount
)))
1539 t
= bf
->find_bfc_term(vn
, bpowers
, nnf
);
1540 bf
->set_factor(v
[i
], k
, bf
->tcount
, changes
% 2);
1541 bf
->add_term(t
, v
[i
]->terms
[k
], extra_num
);
1544 for (int j
= 0; j
< v
[i
]->terms
.NumRows(); ++j
) {
1545 dpoly
n(no_param
, v
[i
]->terms
[j
][0]);
1548 if (no_param
+ only_param
== total_power
)
1549 r
= new dpoly_r(n
, nf
);
1551 for (int k
= 0; k
< nf
; ++k
) {
1552 if (v
[i
]->powers
[k
] == 0)
1554 if (factors
[k
][0] == 0 || old2new
[k
] == -1)
1557 dpoly
pd(no_param
-1, factors
[k
][0], 1);
1559 for (int l
= 0; l
< v
[i
]->powers
[k
]; ++l
) {
1561 for (q
= 0; q
< k
; ++q
)
1562 if (old2new
[q
] == old2new
[k
] &&
1567 r
= new dpoly_r(n
, pd
, q
, nf
);
1569 dpoly_r
*nr
= new dpoly_r(r
, pd
, q
, nf
);
1576 dpoly_r
*rc
= r
->div(D
);
1579 if (bf
->constant_vertex(d
)) {
1580 vector
< dpoly_r_term
* >& final
= rc
->c
[rc
->len
-1];
1582 for (int k
= 0; k
< final
.size(); ++k
) {
1583 if (final
[k
]->coeff
== 0)
1586 update_powers(final
[k
]->powers
, rc
->dim
);
1588 bfc_term_base
* t
= bf
->find_bfc_term(vn
, npowers
, nnf
);
1589 bf
->set_factor(v
[i
], j
, final
[k
]->coeff
, rc
->denom
, l_changes
% 2);
1590 bf
->add_term(t
, v
[i
]->terms
[j
], l_extra_num
);
1593 bf
->cum(this, v
[i
], j
, rc
);
1604 void bf_base::reduce(mat_ZZ
& factors
, bfc_vec
& v
)
1606 assert(v
.size() > 0);
1607 unsigned nf
= factors
.NumRows();
1608 unsigned d
= factors
.NumCols();
1611 return base(factors
, v
);
1613 bf_reducer
bfr(factors
, v
, this);
1617 if (bfr
.vn
.size() > 0)
1618 reduce(bfr
.nfactors
, bfr
.vn
);
1621 int bf_base::setup_factors(Polyhedron
*C
, mat_ZZ
& factors
,
1622 bfc_term_base
* t
, int s
)
1624 factors
.SetDims(dim
, dim
);
1628 for (r
= 0; r
< dim
; ++r
)
1631 for (r
= 0; r
< dim
; ++r
) {
1632 values2zz(C
->Ray
[r
]+1, factors
[r
], dim
);
1634 for (k
= 0; k
< dim
; ++k
)
1635 if (factors
[r
][k
] != 0)
1637 if (factors
[r
][k
] < 0) {
1638 factors
[r
] = -factors
[r
];
1639 t
->terms
[0] += factors
[r
];
1647 void bf_base::handle_polar(Polyhedron
*C
, int s
)
1649 bfc_term
* t
= new bfc_term(dim
);
1650 vector
< bfc_term_base
* > v
;
1653 assert(C
->NbRays
-1 == dim
);
1658 t
->terms
.SetDims(1, dim
);
1659 lattice_point(P
->Ray
[current_vertex
]+1, C
, t
->terms
[0]);
1661 // the elements of factors are always lexpositive
1663 s
= setup_factors(C
, factors
, t
, s
);
1671 void bf_base::start(unsigned MaxRays
)
1673 for (current_vertex
= 0; current_vertex
< P
->NbRays
; ++current_vertex
) {
1674 Polyhedron
*C
= supporting_cone(P
, current_vertex
);
1675 decompose(C
, MaxRays
);
1679 struct bfcounter_base
: public bf_base
{
1683 bfcounter_base(Polyhedron
*P
) : bf_base(P
, P
->Dimension
) {
1686 bfc_term_base
* new_bf_term(int len
) {
1687 bfc_term
* t
= new bfc_term(len
);
1693 virtual void set_factor(bfc_term_base
*t
, int k
, int change
) {
1694 bfc_term
* bfct
= static_cast<bfc_term
*>(t
);
1701 virtual void set_factor(bfc_term_base
*t
, int k
, mpq_t
&f
, int change
) {
1702 bfc_term
* bfct
= static_cast<bfc_term
*>(t
);
1703 value2zz(mpq_numref(f
), cn
);
1704 value2zz(mpq_denref(f
), cd
);
1711 virtual void set_factor(bfc_term_base
*t
, int k
, ZZ
& n
, ZZ
& d
, int change
) {
1712 bfc_term
* bfct
= static_cast<bfc_term
*>(t
);
1713 cn
= bfct
->cn
[k
] * n
;
1716 cd
= bfct
->cd
[k
] * d
;
1719 virtual void insert_term(bfc_term_base
*t
, int i
) {
1720 bfc_term
* bfct
= static_cast<bfc_term
*>(t
);
1721 int len
= t
->terms
.NumRows()-1; // already increased by one
1723 bfct
->cn
.SetLength(len
+1);
1724 bfct
->cd
.SetLength(len
+1);
1725 for (int j
= len
; j
> i
; --j
) {
1726 bfct
->cn
[j
] = bfct
->cn
[j
-1];
1727 bfct
->cd
[j
] = bfct
->cd
[j
-1];
1728 t
->terms
[j
] = t
->terms
[j
-1];
1734 virtual void update_term(bfc_term_base
*t
, int i
) {
1735 bfc_term
* bfct
= static_cast<bfc_term
*>(t
);
1737 ZZ g
= GCD(bfct
->cd
[i
], cd
);
1738 ZZ n
= cn
* bfct
->cd
[i
]/g
+ bfct
->cn
[i
] * cd
/g
;
1739 ZZ d
= bfct
->cd
[i
] * cd
/ g
;
1744 virtual bool constant_vertex(int dim
) { return true; }
1745 virtual void cum(bf_reducer
*bfr
, bfc_term_base
*t
, int k
, dpoly_r
*r
) {
1750 struct bfcounter
: public bfcounter_base
{
1753 bfcounter(Polyhedron
*P
) : bfcounter_base(P
) {
1760 virtual void base(mat_ZZ
& factors
, bfc_vec
& v
);
1763 void bfcounter::base(mat_ZZ
& factors
, bfc_vec
& v
)
1765 unsigned nf
= factors
.NumRows();
1767 for (int i
= 0; i
< v
.size(); ++i
) {
1768 bfc_term
* bfct
= static_cast<bfc_term
*>(v
[i
]);
1769 int total_power
= 0;
1770 // factor is always positive, so we always
1772 for (int k
= 0; k
< nf
; ++k
)
1773 total_power
+= v
[i
]->powers
[k
];
1776 for (j
= 0; j
< nf
; ++j
)
1777 if (v
[i
]->powers
[j
] > 0)
1780 dpoly
D(total_power
, factors
[j
][0], 1);
1781 for (int k
= 1; k
< v
[i
]->powers
[j
]; ++k
) {
1782 dpoly
fact(total_power
, factors
[j
][0], 1);
1786 for (int k
= 0; k
< v
[i
]->powers
[j
]; ++k
) {
1787 dpoly
fact(total_power
, factors
[j
][0], 1);
1791 for (int k
= 0; k
< v
[i
]->terms
.NumRows(); ++k
) {
1792 dpoly
n(total_power
, v
[i
]->terms
[k
][0]);
1793 mpq_set_si(tcount
, 0, 1);
1794 n
.div(D
, tcount
, one
);
1795 if (total_power
% 2)
1796 bfct
->cn
[k
] = -bfct
->cn
[k
];
1797 zz2value(bfct
->cn
[k
], tn
);
1798 zz2value(bfct
->cd
[k
], td
);
1800 mpz_mul(mpq_numref(tcount
), mpq_numref(tcount
), tn
);
1801 mpz_mul(mpq_denref(tcount
), mpq_denref(tcount
), td
);
1802 mpq_canonicalize(tcount
);
1803 mpq_add(count
, count
, tcount
);
1809 struct partial_bfcounter
: public bfcounter_base
, public gf_base
{
1810 partial_bfcounter(Polyhedron
*P
, unsigned nparam
) :
1811 bfcounter_base(P
), gf_base(this, nparam
) {
1814 ~partial_bfcounter() {
1816 virtual void base(mat_ZZ
& factors
, bfc_vec
& v
);
1817 /* we want to override the start method from bf_base with the one from gf_base */
1818 void start(unsigned MaxRays
) {
1819 gf_base::start(MaxRays
);
1823 void partial_bfcounter::base(mat_ZZ
& factors
, bfc_vec
& v
)
1826 unsigned nf
= factors
.NumRows();
1828 for (int i
= 0; i
< v
.size(); ++i
) {
1829 bfc_term
* bfct
= static_cast<bfc_term
*>(v
[i
]);
1830 den
.SetDims(0, lower
);
1831 int total_power
= 0;
1833 for (int j
= 0; j
< nf
; ++j
) {
1834 total_power
+= v
[i
]->powers
[j
];
1835 den
.SetDims(total_power
, lower
);
1836 for (int k
= 0; k
< v
[i
]->powers
[j
]; ++k
)
1837 den
[p
++] = factors
[j
];
1839 for (int j
= 0; j
< v
[i
]->terms
.NumRows(); ++j
)
1840 gf
->add(bfct
->cn
[j
], bfct
->cd
[j
], v
[i
]->terms
[j
], den
);
1846 typedef Polyhedron
* Polyhedron_p
;
1848 static void barvinok_count_f(Polyhedron
*P
, Value
* result
, unsigned NbMaxCons
);
1850 void barvinok_count(Polyhedron
*P
, Value
* result
, unsigned NbMaxCons
)
1856 bool infinite
= false;
1859 value_set_si(*result
, 0);
1863 P
= remove_equalities(P
);
1866 value_set_si(*result
, 0);
1872 for (; r
< P
->NbRays
; ++r
)
1873 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1]))
1875 if (P
->NbBid
!= 0 || r
< P
->NbRays
) {
1876 value_set_si(*result
, -1);
1881 if (P
->Dimension
== 0) {
1882 /* Test whether the constraints are satisfied */
1883 POL_ENSURE_VERTICES(P
);
1884 value_set_si(*result
, !emptyQ(P
));
1889 Q
= Polyhedron_Factor(P
, 0, NbMaxCons
);
1897 barvinok_count_f(P
, result
, NbMaxCons
);
1898 if (value_neg_p(*result
))
1900 if (Q
&& P
->next
&& value_notzero_p(*result
)) {
1904 for (Q
= P
->next
; Q
; Q
= Q
->next
) {
1905 barvinok_count_f(Q
, &factor
, NbMaxCons
);
1906 if (value_neg_p(factor
)) {
1909 } else if (Q
->next
&& value_zero_p(factor
)) {
1910 value_set_si(*result
, 0);
1913 value_multiply(*result
, *result
, factor
);
1916 value_clear(factor
);
1922 value_set_si(*result
, -1);
1925 static void barvinok_count_f(Polyhedron
*P
, Value
* result
, unsigned NbMaxCons
)
1928 value_set_si(*result
, 0);
1932 if (P
->Dimension
== 1)
1933 return Line_Length(P
, result
);
1935 int c
= P
->NbConstraints
;
1936 POL_ENSURE_FACETS(P
);
1937 if (c
!= P
->NbConstraints
|| P
->NbEq
!= 0)
1938 return barvinok_count(P
, result
, NbMaxCons
);
1940 POL_ENSURE_VERTICES(P
);
1942 #ifdef USE_INCREMENTAL_BF
1944 #elif defined USE_INCREMENTAL_DF
1949 cnt
.start(NbMaxCons
);
1951 assert(value_one_p(&cnt
.count
[0]._mp_den
));
1952 value_assign(*result
, &cnt
.count
[0]._mp_num
);
1955 static void uni_polynom(int param
, Vector
*c
, evalue
*EP
)
1957 unsigned dim
= c
->Size
-2;
1959 value_set_si(EP
->d
,0);
1960 EP
->x
.p
= new_enode(polynomial
, dim
+1, param
+1);
1961 for (int j
= 0; j
<= dim
; ++j
)
1962 evalue_set(&EP
->x
.p
->arr
[j
], c
->p
[j
], c
->p
[dim
+1]);
1965 static void multi_polynom(Vector
*c
, evalue
* X
, evalue
*EP
)
1967 unsigned dim
= c
->Size
-2;
1971 evalue_set(&EC
, c
->p
[dim
], c
->p
[dim
+1]);
1974 evalue_set(EP
, c
->p
[dim
], c
->p
[dim
+1]);
1976 for (int i
= dim
-1; i
>= 0; --i
) {
1978 value_assign(EC
.x
.n
, c
->p
[i
]);
1981 free_evalue_refs(&EC
);
1984 Polyhedron
*unfringe (Polyhedron
*P
, unsigned MaxRays
)
1986 int len
= P
->Dimension
+2;
1987 Polyhedron
*T
, *R
= P
;
1990 Vector
*row
= Vector_Alloc(len
);
1991 value_set_si(row
->p
[0], 1);
1993 R
= DomainConstraintSimplify(Polyhedron_Copy(P
), MaxRays
);
1995 Matrix
*M
= Matrix_Alloc(2, len
-1);
1996 value_set_si(M
->p
[1][len
-2], 1);
1997 for (int v
= 0; v
< P
->Dimension
; ++v
) {
1998 value_set_si(M
->p
[0][v
], 1);
1999 Polyhedron
*I
= Polyhedron_Image(P
, M
, 2+1);
2000 value_set_si(M
->p
[0][v
], 0);
2001 for (int r
= 0; r
< I
->NbConstraints
; ++r
) {
2002 if (value_zero_p(I
->Constraint
[r
][0]))
2004 if (value_zero_p(I
->Constraint
[r
][1]))
2006 if (value_one_p(I
->Constraint
[r
][1]))
2008 if (value_mone_p(I
->Constraint
[r
][1]))
2010 value_absolute(g
, I
->Constraint
[r
][1]);
2011 Vector_Set(row
->p
+1, 0, len
-2);
2012 value_division(row
->p
[1+v
], I
->Constraint
[r
][1], g
);
2013 mpz_fdiv_q(row
->p
[len
-1], I
->Constraint
[r
][2], g
);
2015 R
= AddConstraints(row
->p
, 1, R
, MaxRays
);
2027 /* this procedure may have false negatives */
2028 static bool Polyhedron_is_infinite(Polyhedron
*P
, unsigned nparam
)
2031 for (r
= 0; r
< P
->NbRays
; ++r
) {
2032 if (!value_zero_p(P
->Ray
[r
][0]) &&
2033 !value_zero_p(P
->Ray
[r
][P
->Dimension
+1]))
2035 if (First_Non_Zero(P
->Ray
[r
]+1+P
->Dimension
-nparam
, nparam
) == -1)
2041 /* Check whether all rays point in the positive directions
2042 * for the parameters
2044 static bool Polyhedron_has_positive_rays(Polyhedron
*P
, unsigned nparam
)
2047 for (r
= 0; r
< P
->NbRays
; ++r
)
2048 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1])) {
2050 for (i
= P
->Dimension
- nparam
; i
< P
->Dimension
; ++i
)
2051 if (value_neg_p(P
->Ray
[r
][i
+1]))
2057 typedef evalue
* evalue_p
;
2059 struct enumerator
: public polar_decomposer
{
2073 enumerator(Polyhedron
*P
, unsigned dim
, unsigned nbV
) {
2077 randomvector(P
, lambda
, dim
);
2078 rays
.SetDims(dim
, dim
);
2080 c
= Vector_Alloc(dim
+2);
2082 vE
= new evalue_p
[nbV
];
2083 for (int j
= 0; j
< nbV
; ++j
)
2089 void decompose_at(Param_Vertices
*V
, int _i
, unsigned MaxRays
) {
2090 Polyhedron
*C
= supporting_cone_p(P
, V
);
2094 vE
[_i
] = new evalue
;
2095 value_init(vE
[_i
]->d
);
2096 evalue_set_si(vE
[_i
], 0, 1);
2098 decompose(C
, MaxRays
);
2105 for (int j
= 0; j
< nbV
; ++j
)
2107 free_evalue_refs(vE
[j
]);
2113 virtual void handle_polar(Polyhedron
*P
, int sign
);
2116 void enumerator::handle_polar(Polyhedron
*C
, int s
)
2119 assert(C
->NbRays
-1 == dim
);
2120 add_rays(rays
, C
, &r
);
2121 for (int k
= 0; k
< dim
; ++k
) {
2122 if (lambda
* rays
[k
] == 0)
2128 lattice_point(V
, C
, lambda
, &num
, 0);
2129 den
= rays
* lambda
;
2130 normalize(sign
, num
.constant
, den
);
2132 dpoly
n(dim
, den
[0], 1);
2133 for (int k
= 1; k
< dim
; ++k
) {
2134 dpoly
fact(dim
, den
[k
], 1);
2137 if (num
.E
!= NULL
) {
2138 ZZ
one(INIT_VAL
, 1);
2139 dpoly_n
d(dim
, num
.constant
, one
);
2142 multi_polynom(c
, num
.E
, &EV
);
2144 free_evalue_refs(&EV
);
2145 free_evalue_refs(num
.E
);
2147 } else if (num
.pos
!= -1) {
2148 dpoly_n
d(dim
, num
.constant
, num
.coeff
);
2151 uni_polynom(num
.pos
, c
, &EV
);
2153 free_evalue_refs(&EV
);
2155 mpq_set_si(count
, 0, 1);
2156 dpoly
d(dim
, num
.constant
);
2157 d
.div(n
, count
, sign
);
2160 evalue_set(&EV
, &count
[0]._mp_num
, &count
[0]._mp_den
);
2162 free_evalue_refs(&EV
);
2166 struct enumerator_base
{
2171 vertex_decomposer
*vpd
;
2173 enumerator_base(unsigned dim
, vertex_decomposer
*vpd
)
2178 vE
= new evalue_p
[vpd
->nbV
];
2179 for (int j
= 0; j
< vpd
->nbV
; ++j
)
2182 E_vertex
= new evalue_p
[dim
];
2185 evalue_set_si(&mone
, -1, 1);
2188 void decompose_at(Param_Vertices
*V
, int _i
, unsigned MaxRays
/*, Polyhedron *pVD*/) {
2191 vE
[_i
] = new evalue
;
2192 value_init(vE
[_i
]->d
);
2193 evalue_set_si(vE
[_i
], 0, 1);
2195 vpd
->decompose_at_vertex(V
, _i
, MaxRays
);
2198 ~enumerator_base() {
2199 for (int j
= 0; j
< vpd
->nbV
; ++j
)
2201 free_evalue_refs(vE
[j
]);
2208 free_evalue_refs(&mone
);
2211 evalue
*E_num(int i
, int d
) {
2212 return E_vertex
[i
+ (dim
-d
)];
2221 cumulator(evalue
*factor
, evalue
*v
, dpoly_r
*r
) :
2222 factor(factor
), v(v
), r(r
) {}
2226 virtual void add_term(int *powers
, int len
, evalue
*f2
) = 0;
2229 void cumulator::cumulate()
2231 evalue cum
; // factor * 1 * E_num[0]/1 * (E_num[0]-1)/2 *...
2233 evalue t
; // E_num[0] - (m-1)
2239 evalue_set_si(&mone
, -1, 1);
2243 evalue_copy(&cum
, factor
);
2246 value_set_si(f
.d
, 1);
2247 value_set_si(f
.x
.n
, 1);
2252 for (cst
= &t
; value_zero_p(cst
->d
); ) {
2253 if (cst
->x
.p
->type
== fractional
)
2254 cst
= &cst
->x
.p
->arr
[1];
2256 cst
= &cst
->x
.p
->arr
[0];
2260 for (int m
= 0; m
< r
->len
; ++m
) {
2263 value_set_si(f
.d
, m
);
2266 value_subtract(cst
->x
.n
, cst
->x
.n
, cst
->d
);
2273 vector
< dpoly_r_term
* >& current
= r
->c
[r
->len
-1-m
];
2274 for (int j
= 0; j
< current
.size(); ++j
) {
2275 if (current
[j
]->coeff
== 0)
2277 evalue
*f2
= new evalue
;
2279 value_init(f2
->x
.n
);
2280 zz2value(current
[j
]->coeff
, f2
->x
.n
);
2281 zz2value(r
->denom
, f2
->d
);
2284 add_term(current
[j
]->powers
, r
->dim
, f2
);
2287 free_evalue_refs(&f
);
2288 free_evalue_refs(&t
);
2289 free_evalue_refs(&cum
);
2291 free_evalue_refs(&mone
);
2295 struct E_poly_term
{
2300 struct ie_cum
: public cumulator
{
2301 vector
<E_poly_term
*> terms
;
2303 ie_cum(evalue
*factor
, evalue
*v
, dpoly_r
*r
) : cumulator(factor
, v
, r
) {}
2305 virtual void add_term(int *powers
, int len
, evalue
*f2
);
2308 void ie_cum::add_term(int *powers
, int len
, evalue
*f2
)
2311 for (k
= 0; k
< terms
.size(); ++k
) {
2312 if (memcmp(terms
[k
]->powers
, powers
, len
* sizeof(int)) == 0) {
2313 eadd(f2
, terms
[k
]->E
);
2314 free_evalue_refs(f2
);
2319 if (k
>= terms
.size()) {
2320 E_poly_term
*ET
= new E_poly_term
;
2321 ET
->powers
= new int[len
];
2322 memcpy(ET
->powers
, powers
, len
* sizeof(int));
2324 terms
.push_back(ET
);
2328 struct ienumerator
: public polar_decomposer
, public vertex_decomposer
,
2329 public enumerator_base
{
2335 ienumerator(Polyhedron
*P
, unsigned dim
, unsigned nbV
) :
2336 vertex_decomposer(P
, nbV
, this), enumerator_base(dim
, this) {
2337 vertex
.SetLength(dim
);
2339 den
.SetDims(dim
, dim
);
2347 virtual void handle_polar(Polyhedron
*P
, int sign
);
2348 void reduce(evalue
*factor
, vec_ZZ
& num
, mat_ZZ
& den_f
);
2351 void ienumerator::reduce(
2352 evalue
*factor
, vec_ZZ
& num
, mat_ZZ
& den_f
)
2354 unsigned len
= den_f
.NumRows(); // number of factors in den
2355 unsigned dim
= num
.length();
2358 eadd(factor
, vE
[vert
]);
2363 den_s
.SetLength(len
);
2365 den_r
.SetDims(len
, dim
-1);
2369 for (r
= 0; r
< len
; ++r
) {
2370 den_s
[r
] = den_f
[r
][0];
2371 for (k
= 0; k
<= dim
-1; ++k
)
2373 den_r
[r
][k
-(k
>0)] = den_f
[r
][k
];
2378 num_p
.SetLength(dim
-1);
2379 for (k
= 0 ; k
<= dim
-1; ++k
)
2381 num_p
[k
-(k
>0)] = num
[k
];
2384 den_p
.SetLength(len
);
2388 normalize(one
, num_s
, num_p
, den_s
, den_p
, den_r
);
2390 emul(&mone
, factor
);
2394 for (int k
= 0; k
< len
; ++k
) {
2397 else if (den_s
[k
] == 0)
2400 if (no_param
== 0) {
2401 reduce(factor
, num_p
, den_r
);
2405 pden
.SetDims(only_param
, dim
-1);
2407 for (k
= 0, l
= 0; k
< len
; ++k
)
2409 pden
[l
++] = den_r
[k
];
2411 for (k
= 0; k
< len
; ++k
)
2415 dpoly
n(no_param
, num_s
);
2416 dpoly
D(no_param
, den_s
[k
], 1);
2417 for ( ; ++k
< len
; )
2418 if (den_p
[k
] == 0) {
2419 dpoly
fact(no_param
, den_s
[k
], 1);
2424 // if no_param + only_param == len then all powers
2425 // below will be all zero
2426 if (no_param
+ only_param
== len
) {
2427 if (E_num(0, dim
) != 0)
2428 r
= new dpoly_r(n
, len
);
2430 mpq_set_si(tcount
, 0, 1);
2432 n
.div(D
, tcount
, one
);
2434 if (value_notzero_p(mpq_numref(tcount
))) {
2438 value_assign(f
.x
.n
, mpq_numref(tcount
));
2439 value_assign(f
.d
, mpq_denref(tcount
));
2441 reduce(factor
, num_p
, pden
);
2442 free_evalue_refs(&f
);
2447 for (k
= 0; k
< len
; ++k
) {
2448 if (den_s
[k
] == 0 || den_p
[k
] == 0)
2451 dpoly
pd(no_param
-1, den_s
[k
], 1);
2454 for (l
= 0; l
< k
; ++l
)
2455 if (den_r
[l
] == den_r
[k
])
2459 r
= new dpoly_r(n
, pd
, l
, len
);
2461 dpoly_r
*nr
= new dpoly_r(r
, pd
, l
, len
);
2467 dpoly_r
*rc
= r
->div(D
);
2470 if (E_num(0, dim
) == 0) {
2471 int common
= pden
.NumRows();
2472 vector
< dpoly_r_term
* >& final
= r
->c
[r
->len
-1];
2478 zz2value(r
->denom
, f
.d
);
2479 for (int j
= 0; j
< final
.size(); ++j
) {
2480 if (final
[j
]->coeff
== 0)
2483 for (int k
= 0; k
< r
->dim
; ++k
) {
2484 int n
= final
[j
]->powers
[k
];
2487 pden
.SetDims(rows
+n
, pden
.NumCols());
2488 for (int l
= 0; l
< n
; ++l
)
2489 pden
[rows
+l
] = den_r
[k
];
2493 evalue_copy(&t
, factor
);
2494 zz2value(final
[j
]->coeff
, f
.x
.n
);
2496 reduce(&t
, num_p
, pden
);
2497 free_evalue_refs(&t
);
2499 free_evalue_refs(&f
);
2501 ie_cum
cum(factor
, E_num(0, dim
), r
);
2504 int common
= pden
.NumRows();
2506 for (int j
= 0; j
< cum
.terms
.size(); ++j
) {
2508 pden
.SetDims(rows
, pden
.NumCols());
2509 for (int k
= 0; k
< r
->dim
; ++k
) {
2510 int n
= cum
.terms
[j
]->powers
[k
];
2513 pden
.SetDims(rows
+n
, pden
.NumCols());
2514 for (int l
= 0; l
< n
; ++l
)
2515 pden
[rows
+l
] = den_r
[k
];
2518 reduce(cum
.terms
[j
]->E
, num_p
, pden
);
2519 free_evalue_refs(cum
.terms
[j
]->E
);
2520 delete cum
.terms
[j
]->E
;
2521 delete [] cum
.terms
[j
]->powers
;
2522 delete cum
.terms
[j
];
2529 static int type_offset(enode
*p
)
2531 return p
->type
== fractional
? 1 :
2532 p
->type
== flooring
? 1 : 0;
2535 static int edegree(evalue
*e
)
2540 if (value_notzero_p(e
->d
))
2544 int i
= type_offset(p
);
2545 if (p
->size
-i
-1 > d
)
2546 d
= p
->size
- i
- 1;
2547 for (; i
< p
->size
; i
++) {
2548 int d2
= edegree(&p
->arr
[i
]);
2555 void ienumerator::handle_polar(Polyhedron
*C
, int s
)
2557 assert(C
->NbRays
-1 == dim
);
2559 lattice_point(V
, C
, vertex
, E_vertex
);
2562 for (r
= 0; r
< dim
; ++r
)
2563 values2zz(C
->Ray
[r
]+1, den
[r
], dim
);
2567 evalue_set_si(&one
, s
, 1);
2568 reduce(&one
, vertex
, den
);
2569 free_evalue_refs(&one
);
2571 for (int i
= 0; i
< dim
; ++i
)
2573 free_evalue_refs(E_vertex
[i
]);
2578 struct bfenumerator
: public vertex_decomposer
, public bf_base
,
2579 public enumerator_base
{
2582 bfenumerator(Polyhedron
*P
, unsigned dim
, unsigned nbV
) :
2583 vertex_decomposer(P
, nbV
, this),
2584 bf_base(P
, dim
), enumerator_base(dim
, this) {
2592 virtual void handle_polar(Polyhedron
*P
, int sign
);
2593 virtual void base(mat_ZZ
& factors
, bfc_vec
& v
);
2595 bfc_term_base
* new_bf_term(int len
) {
2596 bfe_term
* t
= new bfe_term(len
);
2600 virtual void set_factor(bfc_term_base
*t
, int k
, int change
) {
2601 bfe_term
* bfet
= static_cast<bfe_term
*>(t
);
2602 factor
= bfet
->factors
[k
];
2603 assert(factor
!= NULL
);
2604 bfet
->factors
[k
] = NULL
;
2606 emul(&mone
, factor
);
2609 virtual void set_factor(bfc_term_base
*t
, int k
, mpq_t
&q
, int change
) {
2610 bfe_term
* bfet
= static_cast<bfe_term
*>(t
);
2611 factor
= bfet
->factors
[k
];
2612 assert(factor
!= NULL
);
2613 bfet
->factors
[k
] = NULL
;
2619 value_oppose(f
.x
.n
, mpq_numref(q
));
2621 value_assign(f
.x
.n
, mpq_numref(q
));
2622 value_assign(f
.d
, mpq_denref(q
));
2626 virtual void set_factor(bfc_term_base
*t
, int k
, ZZ
& n
, ZZ
& d
, int change
) {
2627 bfe_term
* bfet
= static_cast<bfe_term
*>(t
);
2629 factor
= new evalue
;
2636 value_oppose(f
.x
.n
, f
.x
.n
);
2639 value_init(factor
->d
);
2640 evalue_copy(factor
, bfet
->factors
[k
]);
2644 void set_factor(evalue
*f
, int change
) {
2650 virtual void insert_term(bfc_term_base
*t
, int i
) {
2651 bfe_term
* bfet
= static_cast<bfe_term
*>(t
);
2652 int len
= t
->terms
.NumRows()-1; // already increased by one
2654 bfet
->factors
.resize(len
+1);
2655 for (int j
= len
; j
> i
; --j
) {
2656 bfet
->factors
[j
] = bfet
->factors
[j
-1];
2657 t
->terms
[j
] = t
->terms
[j
-1];
2659 bfet
->factors
[i
] = factor
;
2663 virtual void update_term(bfc_term_base
*t
, int i
) {
2664 bfe_term
* bfet
= static_cast<bfe_term
*>(t
);
2666 eadd(factor
, bfet
->factors
[i
]);
2667 free_evalue_refs(factor
);
2671 virtual bool constant_vertex(int dim
) { return E_num(0, dim
) == 0; }
2673 virtual void cum(bf_reducer
*bfr
, bfc_term_base
*t
, int k
, dpoly_r
*r
);
2676 struct bfe_cum
: public cumulator
{
2678 bfc_term_base
*told
;
2682 bfe_cum(evalue
*factor
, evalue
*v
, dpoly_r
*r
, bf_reducer
*bfr
,
2683 bfc_term_base
*t
, int k
, bfenumerator
*e
) :
2684 cumulator(factor
, v
, r
), told(t
), k(k
),
2688 virtual void add_term(int *powers
, int len
, evalue
*f2
);
2691 void bfe_cum::add_term(int *powers
, int len
, evalue
*f2
)
2693 bfr
->update_powers(powers
, len
);
2695 bfc_term_base
* t
= bfe
->find_bfc_term(bfr
->vn
, bfr
->npowers
, bfr
->nnf
);
2696 bfe
->set_factor(f2
, bfr
->l_changes
% 2);
2697 bfe
->add_term(t
, told
->terms
[k
], bfr
->l_extra_num
);
2700 void bfenumerator::cum(bf_reducer
*bfr
, bfc_term_base
*t
, int k
,
2703 bfe_term
* bfet
= static_cast<bfe_term
*>(t
);
2704 bfe_cum
cum(bfet
->factors
[k
], E_num(0, bfr
->d
), r
, bfr
, t
, k
, this);
2708 void bfenumerator::base(mat_ZZ
& factors
, bfc_vec
& v
)
2710 for (int i
= 0; i
< v
.size(); ++i
) {
2711 assert(v
[i
]->terms
.NumRows() == 1);
2712 evalue
*factor
= static_cast<bfe_term
*>(v
[i
])->factors
[0];
2713 eadd(factor
, vE
[vert
]);
2718 void bfenumerator::handle_polar(Polyhedron
*C
, int s
)
2720 assert(C
->NbRays
-1 == enumerator_base::dim
);
2722 bfe_term
* t
= new bfe_term(enumerator_base::dim
);
2723 vector
< bfc_term_base
* > v
;
2726 t
->factors
.resize(1);
2728 t
->terms
.SetDims(1, enumerator_base::dim
);
2729 lattice_point(V
, C
, t
->terms
[0], E_vertex
);
2731 // the elements of factors are always lexpositive
2733 s
= setup_factors(C
, factors
, t
, s
);
2735 t
->factors
[0] = new evalue
;
2736 value_init(t
->factors
[0]->d
);
2737 evalue_set_si(t
->factors
[0], s
, 1);
2740 for (int i
= 0; i
< enumerator_base::dim
; ++i
)
2742 free_evalue_refs(E_vertex
[i
]);
2747 #ifdef HAVE_CORRECT_VERTICES
2748 static inline Param_Polyhedron
*Polyhedron2Param_SD(Polyhedron
**Din
,
2749 Polyhedron
*Cin
,int WS
,Polyhedron
**CEq
,Matrix
**CT
)
2751 if (WS
& POL_NO_DUAL
)
2753 return Polyhedron2Param_SimplifiedDomain(Din
, Cin
, WS
, CEq
, CT
);
2756 static Param_Polyhedron
*Polyhedron2Param_SD(Polyhedron
**Din
,
2757 Polyhedron
*Cin
,int WS
,Polyhedron
**CEq
,Matrix
**CT
)
2759 static char data
[] = " 1 0 0 0 0 1 -18 "
2760 " 1 0 0 -20 0 19 1 "
2761 " 1 0 1 20 0 -20 16 "
2764 " 1 4 -20 0 0 -1 23 "
2765 " 1 -4 20 0 0 1 -22 "
2766 " 1 0 1 0 20 -20 16 "
2767 " 1 0 0 0 -20 19 1 ";
2768 static int checked
= 0;
2773 Matrix
*M
= Matrix_Alloc(9, 7);
2774 for (i
= 0; i
< 9; ++i
)
2775 for (int j
= 0; j
< 7; ++j
) {
2776 sscanf(p
, "%d%n", &v
, &n
);
2778 value_set_si(M
->p
[i
][j
], v
);
2780 Polyhedron
*P
= Constraints2Polyhedron(M
, 1024);
2782 Polyhedron
*U
= Universe_Polyhedron(1);
2783 Param_Polyhedron
*PP
= Polyhedron2Param_Domain(P
, U
, 1024);
2787 for (i
= 0, V
= PP
->V
; V
; ++i
, V
= V
->next
)
2790 Param_Polyhedron_Free(PP
);
2792 fprintf(stderr
, "WARNING: results may be incorrect\n");
2794 "WARNING: use latest version of PolyLib to remove this warning\n");
2798 return Polyhedron2Param_SimplifiedDomain(Din
, Cin
, WS
, CEq
, CT
);
2802 static evalue
* barvinok_enumerate_ev_f(Polyhedron
*P
, Polyhedron
* C
,
2806 static evalue
* barvinok_enumerate_cst(Polyhedron
*P
, Polyhedron
* C
,
2811 ALLOC(evalue
, eres
);
2812 value_init(eres
->d
);
2813 value_set_si(eres
->d
, 0);
2814 eres
->x
.p
= new_enode(partition
, 2, C
->Dimension
);
2815 EVALUE_SET_DOMAIN(eres
->x
.p
->arr
[0], DomainConstraintSimplify(C
, MaxRays
));
2816 value_set_si(eres
->x
.p
->arr
[1].d
, 1);
2817 value_init(eres
->x
.p
->arr
[1].x
.n
);
2819 value_set_si(eres
->x
.p
->arr
[1].x
.n
, 0);
2821 barvinok_count(P
, &eres
->x
.p
->arr
[1].x
.n
, MaxRays
);
2826 evalue
* barvinok_enumerate_ev(Polyhedron
*P
, Polyhedron
* C
, unsigned MaxRays
)
2828 //P = unfringe(P, MaxRays);
2829 Polyhedron
*Corig
= C
;
2830 Polyhedron
*CEq
= NULL
, *rVD
, *CA
;
2832 unsigned nparam
= C
->Dimension
;
2836 value_init(factor
.d
);
2837 evalue_set_si(&factor
, 1, 1);
2839 CA
= align_context(C
, P
->Dimension
, MaxRays
);
2840 P
= DomainIntersection(P
, CA
, MaxRays
);
2841 Polyhedron_Free(CA
);
2844 POL_ENSURE_FACETS(P
);
2845 POL_ENSURE_VERTICES(P
);
2846 POL_ENSURE_FACETS(C
);
2847 POL_ENSURE_VERTICES(C
);
2849 if (C
->Dimension
== 0 || emptyQ(P
)) {
2851 eres
= barvinok_enumerate_cst(P
, CEq
? CEq
: Polyhedron_Copy(C
),
2854 emul(&factor
, eres
);
2855 reduce_evalue(eres
);
2856 free_evalue_refs(&factor
);
2863 if (Polyhedron_is_infinite(P
, nparam
))
2868 P
= remove_equalities_p(P
, P
->Dimension
-nparam
, &f
);
2872 if (P
->Dimension
== nparam
) {
2874 P
= Universe_Polyhedron(0);
2878 Polyhedron
*T
= Polyhedron_Factor(P
, nparam
, MaxRays
);
2879 if (T
|| (P
->Dimension
== nparam
+1)) {
2882 for (Q
= T
? T
: P
; Q
; Q
= Q
->next
) {
2883 Polyhedron
*next
= Q
->next
;
2887 if (Q
->Dimension
!= C
->Dimension
)
2888 QC
= Polyhedron_Project(Q
, nparam
);
2891 C
= DomainIntersection(C
, QC
, MaxRays
);
2893 Polyhedron_Free(C2
);
2895 Polyhedron_Free(QC
);
2903 if (T
->Dimension
== C
->Dimension
) {
2910 Polyhedron
*next
= P
->next
;
2912 eres
= barvinok_enumerate_ev_f(P
, C
, MaxRays
);
2919 for (Q
= P
->next
; Q
; Q
= Q
->next
) {
2920 Polyhedron
*next
= Q
->next
;
2923 f
= barvinok_enumerate_ev_f(Q
, C
, MaxRays
);
2925 free_evalue_refs(f
);
2935 static evalue
* barvinok_enumerate_ev_f(Polyhedron
*P
, Polyhedron
* C
,
2938 unsigned nparam
= C
->Dimension
;
2940 if (P
->Dimension
- nparam
== 1)
2941 return ParamLine_Length(P
, C
, MaxRays
);
2943 Param_Polyhedron
*PP
= NULL
;
2944 Polyhedron
*CEq
= NULL
, *pVD
;
2946 Param_Domain
*D
, *next
;
2949 Polyhedron
*Porig
= P
;
2951 PP
= Polyhedron2Param_SD(&P
,C
,MaxRays
,&CEq
,&CT
);
2953 if (isIdentity(CT
)) {
2957 assert(CT
->NbRows
!= CT
->NbColumns
);
2958 if (CT
->NbRows
== 1) { // no more parameters
2959 eres
= barvinok_enumerate_cst(P
, CEq
, MaxRays
);
2964 Param_Polyhedron_Free(PP
);
2970 nparam
= CT
->NbRows
- 1;
2973 unsigned dim
= P
->Dimension
- nparam
;
2975 ALLOC(evalue
, eres
);
2976 value_init(eres
->d
);
2977 value_set_si(eres
->d
, 0);
2980 for (nd
= 0, D
=PP
->D
; D
; ++nd
, D
=D
->next
);
2981 struct section
{ Polyhedron
*D
; evalue E
; };
2982 section
*s
= new section
[nd
];
2983 Polyhedron
**fVD
= new Polyhedron_p
[nd
];
2986 #ifdef USE_INCREMENTAL_BF
2987 bfenumerator
et(P
, dim
, PP
->nbV
);
2988 #elif defined USE_INCREMENTAL_DF
2989 ienumerator
et(P
, dim
, PP
->nbV
);
2991 enumerator
et(P
, dim
, PP
->nbV
);
2994 for(nd
= 0, D
=PP
->D
; D
; D
=next
) {
2997 Polyhedron
*rVD
= reduce_domain(D
->Domain
, CT
, CEq
,
3002 pVD
= CT
? DomainImage(rVD
,CT
,MaxRays
) : rVD
;
3004 value_init(s
[nd
].E
.d
);
3005 evalue_set_si(&s
[nd
].E
, 0, 1);
3008 FORALL_PVertex_in_ParamPolyhedron(V
,D
,PP
) // _i is internal counter
3011 et
.decompose_at(V
, _i
, MaxRays
);
3012 } catch (OrthogonalException
&e
) {
3015 for (; nd
>= 0; --nd
) {
3016 free_evalue_refs(&s
[nd
].E
);
3017 Domain_Free(s
[nd
].D
);
3018 Domain_Free(fVD
[nd
]);
3022 eadd(et
.vE
[_i
] , &s
[nd
].E
);
3023 END_FORALL_PVertex_in_ParamPolyhedron
;
3024 evalue_range_reduction_in_domain(&s
[nd
].E
, pVD
);
3027 addeliminatedparams_evalue(&s
[nd
].E
, CT
);
3034 evalue_set_si(eres
, 0, 1);
3036 eres
->x
.p
= new_enode(partition
, 2*nd
, C
->Dimension
);
3037 for (int j
= 0; j
< nd
; ++j
) {
3038 EVALUE_SET_DOMAIN(eres
->x
.p
->arr
[2*j
], s
[j
].D
);
3039 value_clear(eres
->x
.p
->arr
[2*j
+1].d
);
3040 eres
->x
.p
->arr
[2*j
+1] = s
[j
].E
;
3041 Domain_Free(fVD
[j
]);
3048 Polyhedron_Free(CEq
);
3052 Enumeration
* barvinok_enumerate(Polyhedron
*P
, Polyhedron
* C
, unsigned MaxRays
)
3054 evalue
*EP
= barvinok_enumerate_ev(P
, C
, MaxRays
);
3056 return partition2enumeration(EP
);
3059 static void SwapColumns(Value
**V
, int n
, int i
, int j
)
3061 for (int r
= 0; r
< n
; ++r
)
3062 value_swap(V
[r
][i
], V
[r
][j
]);
3065 static void SwapColumns(Polyhedron
*P
, int i
, int j
)
3067 SwapColumns(P
->Constraint
, P
->NbConstraints
, i
, j
);
3068 SwapColumns(P
->Ray
, P
->NbRays
, i
, j
);
3071 /* Construct a constraint c from constraints l and u such that if
3072 * if constraint c holds then for each value of the other variables
3073 * there is at most one value of variable pos (position pos+1 in the constraints).
3075 * Given a lower and an upper bound
3076 * n_l v_i + <c_l,x> + c_l >= 0
3077 * -n_u v_i + <c_u,x> + c_u >= 0
3078 * the constructed constraint is
3080 * -(n_l<c_u,x> + n_u<c_l,x>) + (-n_l c_u - n_u c_l + n_l n_u - 1)
3082 * which is then simplified to remove the content of the non-constant coefficients
3084 * len is the total length of the constraints.
3085 * v is a temporary variable that can be used by this procedure
3087 static void negative_test_constraint(Value
*l
, Value
*u
, Value
*c
, int pos
,
3090 value_oppose(*v
, u
[pos
+1]);
3091 Vector_Combine(l
+1, u
+1, c
+1, *v
, l
[pos
+1], len
-1);
3092 value_multiply(*v
, *v
, l
[pos
+1]);
3093 value_subtract(c
[len
-1], c
[len
-1], *v
);
3094 value_set_si(*v
, -1);
3095 Vector_Scale(c
+1, c
+1, *v
, len
-1);
3096 value_decrement(c
[len
-1], c
[len
-1]);
3097 ConstraintSimplify(c
, c
, len
, v
);
3100 static bool parallel_constraints(Value
*l
, Value
*u
, Value
*c
, int pos
,
3109 Vector_Gcd(&l
[1+pos
], len
, &g1
);
3110 Vector_Gcd(&u
[1+pos
], len
, &g2
);
3111 Vector_Combine(l
+1+pos
, u
+1+pos
, c
+1, g2
, g1
, len
);
3112 parallel
= First_Non_Zero(c
+1, len
) == -1;
3120 static void negative_test_constraint7(Value
*l
, Value
*u
, Value
*c
, int pos
,
3121 int exist
, int len
, Value
*v
)
3126 Vector_Gcd(&u
[1+pos
], exist
, v
);
3127 Vector_Gcd(&l
[1+pos
], exist
, &g
);
3128 Vector_Combine(l
+1, u
+1, c
+1, *v
, g
, len
-1);
3129 value_multiply(*v
, *v
, g
);
3130 value_subtract(c
[len
-1], c
[len
-1], *v
);
3131 value_set_si(*v
, -1);
3132 Vector_Scale(c
+1, c
+1, *v
, len
-1);
3133 value_decrement(c
[len
-1], c
[len
-1]);
3134 ConstraintSimplify(c
, c
, len
, v
);
3139 /* Turns a x + b >= 0 into a x + b <= -1
3141 * len is the total length of the constraint.
3142 * v is a temporary variable that can be used by this procedure
3144 static void oppose_constraint(Value
*c
, int len
, Value
*v
)
3146 value_set_si(*v
, -1);
3147 Vector_Scale(c
+1, c
+1, *v
, len
-1);
3148 value_decrement(c
[len
-1], c
[len
-1]);
3151 /* Split polyhedron P into two polyhedra *pos and *neg, where
3152 * existential variable i has at most one solution for each
3153 * value of the other variables in *neg.
3155 * The splitting is performed using constraints l and u.
3157 * nvar: number of set variables
3158 * row: temporary vector that can be used by this procedure
3159 * f: temporary value that can be used by this procedure
3161 static bool SplitOnConstraint(Polyhedron
*P
, int i
, int l
, int u
,
3162 int nvar
, int MaxRays
, Vector
*row
, Value
& f
,
3163 Polyhedron
**pos
, Polyhedron
**neg
)
3165 negative_test_constraint(P
->Constraint
[l
], P
->Constraint
[u
],
3166 row
->p
, nvar
+i
, P
->Dimension
+2, &f
);
3167 *neg
= AddConstraints(row
->p
, 1, P
, MaxRays
);
3169 /* We found an independent, but useless constraint
3170 * Maybe we should detect this earlier and not
3171 * mark the variable as INDEPENDENT
3173 if (emptyQ((*neg
))) {
3174 Polyhedron_Free(*neg
);
3178 oppose_constraint(row
->p
, P
->Dimension
+2, &f
);
3179 *pos
= AddConstraints(row
->p
, 1, P
, MaxRays
);
3181 if (emptyQ((*pos
))) {
3182 Polyhedron_Free(*neg
);
3183 Polyhedron_Free(*pos
);
3191 * unimodularly transform P such that constraint r is transformed
3192 * into a constraint that involves only a single (the first)
3193 * existential variable
3196 static Polyhedron
*rotate_along(Polyhedron
*P
, int r
, int nvar
, int exist
,
3202 Vector
*row
= Vector_Alloc(exist
);
3203 Vector_Copy(P
->Constraint
[r
]+1+nvar
, row
->p
, exist
);
3204 Vector_Gcd(row
->p
, exist
, &g
);
3205 if (value_notone_p(g
))
3206 Vector_AntiScale(row
->p
, row
->p
, g
, exist
);
3209 Matrix
*M
= unimodular_complete(row
);
3210 Matrix
*M2
= Matrix_Alloc(P
->Dimension
+1, P
->Dimension
+1);
3211 for (r
= 0; r
< nvar
; ++r
)
3212 value_set_si(M2
->p
[r
][r
], 1);
3213 for ( ; r
< nvar
+exist
; ++r
)
3214 Vector_Copy(M
->p
[r
-nvar
], M2
->p
[r
]+nvar
, exist
);
3215 for ( ; r
< P
->Dimension
+1; ++r
)
3216 value_set_si(M2
->p
[r
][r
], 1);
3217 Polyhedron
*T
= Polyhedron_Image(P
, M2
, MaxRays
);
3226 /* Split polyhedron P into two polyhedra *pos and *neg, where
3227 * existential variable i has at most one solution for each
3228 * value of the other variables in *neg.
3230 * If independent is set, then the two constraints on which the
3231 * split will be performed need to be independent of the other
3232 * existential variables.
3234 * Return true if an appropriate split could be performed.
3236 * nvar: number of set variables
3237 * exist: number of existential variables
3238 * row: temporary vector that can be used by this procedure
3239 * f: temporary value that can be used by this procedure
3241 static bool SplitOnVar(Polyhedron
*P
, int i
,
3242 int nvar
, int exist
, int MaxRays
,
3243 Vector
*row
, Value
& f
, bool independent
,
3244 Polyhedron
**pos
, Polyhedron
**neg
)
3248 for (int l
= P
->NbEq
; l
< P
->NbConstraints
; ++l
) {
3249 if (value_negz_p(P
->Constraint
[l
][nvar
+i
+1]))
3253 for (j
= 0; j
< exist
; ++j
)
3254 if (j
!= i
&& value_notzero_p(P
->Constraint
[l
][nvar
+j
+1]))
3260 for (int u
= P
->NbEq
; u
< P
->NbConstraints
; ++u
) {
3261 if (value_posz_p(P
->Constraint
[u
][nvar
+i
+1]))
3265 for (j
= 0; j
< exist
; ++j
)
3266 if (j
!= i
&& value_notzero_p(P
->Constraint
[u
][nvar
+j
+1]))
3272 if (SplitOnConstraint(P
, i
, l
, u
, nvar
, MaxRays
, row
, f
, pos
, neg
)) {
3275 SwapColumns(*neg
, nvar
+1, nvar
+1+i
);
3285 static bool double_bound_pair(Polyhedron
*P
, int nvar
, int exist
,
3286 int i
, int l1
, int l2
,
3287 Polyhedron
**pos
, Polyhedron
**neg
)
3291 Vector
*row
= Vector_Alloc(P
->Dimension
+2);
3292 value_set_si(row
->p
[0], 1);
3293 value_oppose(f
, P
->Constraint
[l1
][nvar
+i
+1]);
3294 Vector_Combine(P
->Constraint
[l1
]+1, P
->Constraint
[l2
]+1,
3296 P
->Constraint
[l2
][nvar
+i
+1], f
,
3298 ConstraintSimplify(row
->p
, row
->p
, P
->Dimension
+2, &f
);
3299 *pos
= AddConstraints(row
->p
, 1, P
, 0);
3300 value_set_si(f
, -1);
3301 Vector_Scale(row
->p
+1, row
->p
+1, f
, P
->Dimension
+1);
3302 value_decrement(row
->p
[P
->Dimension
+1], row
->p
[P
->Dimension
+1]);
3303 *neg
= AddConstraints(row
->p
, 1, P
, 0);
3307 return !emptyQ((*pos
)) && !emptyQ((*neg
));
3310 static bool double_bound(Polyhedron
*P
, int nvar
, int exist
,
3311 Polyhedron
**pos
, Polyhedron
**neg
)
3313 for (int i
= 0; i
< exist
; ++i
) {
3315 for (l1
= P
->NbEq
; l1
< P
->NbConstraints
; ++l1
) {
3316 if (value_negz_p(P
->Constraint
[l1
][nvar
+i
+1]))
3318 for (l2
= l1
+ 1; l2
< P
->NbConstraints
; ++l2
) {
3319 if (value_negz_p(P
->Constraint
[l2
][nvar
+i
+1]))
3321 if (double_bound_pair(P
, nvar
, exist
, i
, l1
, l2
, pos
, neg
))
3325 for (l1
= P
->NbEq
; l1
< P
->NbConstraints
; ++l1
) {
3326 if (value_posz_p(P
->Constraint
[l1
][nvar
+i
+1]))
3328 if (l1
< P
->NbConstraints
)
3329 for (l2
= l1
+ 1; l2
< P
->NbConstraints
; ++l2
) {
3330 if (value_posz_p(P
->Constraint
[l2
][nvar
+i
+1]))
3332 if (double_bound_pair(P
, nvar
, exist
, i
, l1
, l2
, pos
, neg
))
3344 INDEPENDENT
= 1 << 2,
3348 static evalue
* enumerate_or(Polyhedron
*D
,
3349 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
3352 fprintf(stderr
, "\nER: Or\n");
3353 #endif /* DEBUG_ER */
3355 Polyhedron
*N
= D
->next
;
3358 barvinok_enumerate_e(D
, exist
, nparam
, MaxRays
);
3361 for (D
= N
; D
; D
= N
) {
3366 barvinok_enumerate_e(D
, exist
, nparam
, MaxRays
);
3369 free_evalue_refs(EN
);
3379 static evalue
* enumerate_sum(Polyhedron
*P
,
3380 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
3382 int nvar
= P
->Dimension
- exist
- nparam
;
3383 int toswap
= nvar
< exist
? nvar
: exist
;
3384 for (int i
= 0; i
< toswap
; ++i
)
3385 SwapColumns(P
, 1 + i
, nvar
+exist
- i
);
3389 fprintf(stderr
, "\nER: Sum\n");
3390 #endif /* DEBUG_ER */
3392 evalue
*EP
= barvinok_enumerate_e(P
, exist
, nparam
, MaxRays
);
3394 for (int i
= 0; i
< /* nvar */ nparam
; ++i
) {
3395 Matrix
*C
= Matrix_Alloc(1, 1 + nparam
+ 1);
3396 value_set_si(C
->p
[0][0], 1);
3398 value_init(split
.d
);
3399 value_set_si(split
.d
, 0);
3400 split
.x
.p
= new_enode(partition
, 4, nparam
);
3401 value_set_si(C
->p
[0][1+i
], 1);
3402 Matrix
*C2
= Matrix_Copy(C
);
3403 EVALUE_SET_DOMAIN(split
.x
.p
->arr
[0],
3404 Constraints2Polyhedron(C2
, MaxRays
));
3406 evalue_set_si(&split
.x
.p
->arr
[1], 1, 1);
3407 value_set_si(C
->p
[0][1+i
], -1);
3408 value_set_si(C
->p
[0][1+nparam
], -1);
3409 EVALUE_SET_DOMAIN(split
.x
.p
->arr
[2],
3410 Constraints2Polyhedron(C
, MaxRays
));
3411 evalue_set_si(&split
.x
.p
->arr
[3], 1, 1);
3413 free_evalue_refs(&split
);
3417 evalue_range_reduction(EP
);
3419 evalue_frac2floor(EP
);
3421 evalue
*sum
= esum(EP
, nvar
);
3423 free_evalue_refs(EP
);
3427 evalue_range_reduction(EP
);
3432 static evalue
* split_sure(Polyhedron
*P
, Polyhedron
*S
,
3433 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
3435 int nvar
= P
->Dimension
- exist
- nparam
;
3437 Matrix
*M
= Matrix_Alloc(exist
, S
->Dimension
+2);
3438 for (int i
= 0; i
< exist
; ++i
)
3439 value_set_si(M
->p
[i
][nvar
+i
+1], 1);
3441 S
= DomainAddRays(S
, M
, MaxRays
);
3443 Polyhedron
*F
= DomainAddRays(P
, M
, MaxRays
);
3444 Polyhedron
*D
= DomainDifference(F
, S
, MaxRays
);
3446 D
= Disjoint_Domain(D
, 0, MaxRays
);
3451 M
= Matrix_Alloc(P
->Dimension
+1-exist
, P
->Dimension
+1);
3452 for (int j
= 0; j
< nvar
; ++j
)
3453 value_set_si(M
->p
[j
][j
], 1);
3454 for (int j
= 0; j
< nparam
+1; ++j
)
3455 value_set_si(M
->p
[nvar
+j
][nvar
+exist
+j
], 1);
3456 Polyhedron
*T
= Polyhedron_Image(S
, M
, MaxRays
);
3457 evalue
*EP
= barvinok_enumerate_e(T
, 0, nparam
, MaxRays
);
3462 for (Polyhedron
*Q
= D
; Q
; Q
= Q
->next
) {
3463 Polyhedron
*N
= Q
->next
;
3465 T
= DomainIntersection(P
, Q
, MaxRays
);
3466 evalue
*E
= barvinok_enumerate_e(T
, exist
, nparam
, MaxRays
);
3468 free_evalue_refs(E
);
3477 static evalue
* enumerate_sure(Polyhedron
*P
,
3478 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
3482 int nvar
= P
->Dimension
- exist
- nparam
;
3488 for (i
= 0; i
< exist
; ++i
) {
3489 Matrix
*M
= Matrix_Alloc(S
->NbConstraints
, S
->Dimension
+2);
3491 value_set_si(lcm
, 1);
3492 for (int j
= 0; j
< S
->NbConstraints
; ++j
) {
3493 if (value_negz_p(S
->Constraint
[j
][1+nvar
+i
]))
3495 if (value_one_p(S
->Constraint
[j
][1+nvar
+i
]))
3497 value_lcm(lcm
, S
->Constraint
[j
][1+nvar
+i
], &lcm
);
3500 for (int j
= 0; j
< S
->NbConstraints
; ++j
) {
3501 if (value_negz_p(S
->Constraint
[j
][1+nvar
+i
]))
3503 if (value_one_p(S
->Constraint
[j
][1+nvar
+i
]))
3505 value_division(f
, lcm
, S
->Constraint
[j
][1+nvar
+i
]);
3506 Vector_Scale(S
->Constraint
[j
], M
->p
[c
], f
, S
->Dimension
+2);
3507 value_subtract(M
->p
[c
][S
->Dimension
+1],
3508 M
->p
[c
][S
->Dimension
+1],
3510 value_increment(M
->p
[c
][S
->Dimension
+1],
3511 M
->p
[c
][S
->Dimension
+1]);
3515 S
= AddConstraints(M
->p
[0], c
, S
, MaxRays
);
3530 fprintf(stderr
, "\nER: Sure\n");
3531 #endif /* DEBUG_ER */
3533 return split_sure(P
, S
, exist
, nparam
, MaxRays
);
3536 static evalue
* enumerate_sure2(Polyhedron
*P
,
3537 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
3539 int nvar
= P
->Dimension
- exist
- nparam
;
3541 for (r
= 0; r
< P
->NbRays
; ++r
)
3542 if (value_one_p(P
->Ray
[r
][0]) &&
3543 value_one_p(P
->Ray
[r
][P
->Dimension
+1]))
3549 Matrix
*M
= Matrix_Alloc(nvar
+ 1 + nparam
, P
->Dimension
+2);
3550 for (int i
= 0; i
< nvar
; ++i
)
3551 value_set_si(M
->p
[i
][1+i
], 1);
3552 for (int i
= 0; i
< nparam
; ++i
)
3553 value_set_si(M
->p
[i
+nvar
][1+nvar
+exist
+i
], 1);
3554 Vector_Copy(P
->Ray
[r
]+1+nvar
, M
->p
[nvar
+nparam
]+1+nvar
, exist
);
3555 value_set_si(M
->p
[nvar
+nparam
][0], 1);
3556 value_set_si(M
->p
[nvar
+nparam
][P
->Dimension
+1], 1);
3557 Polyhedron
* F
= Rays2Polyhedron(M
, MaxRays
);
3560 Polyhedron
*I
= DomainIntersection(F
, P
, MaxRays
);
3564 fprintf(stderr
, "\nER: Sure2\n");
3565 #endif /* DEBUG_ER */
3567 return split_sure(P
, I
, exist
, nparam
, MaxRays
);
3570 static evalue
* enumerate_cyclic(Polyhedron
*P
,
3571 unsigned exist
, unsigned nparam
,
3572 evalue
* EP
, int r
, int p
, unsigned MaxRays
)
3574 int nvar
= P
->Dimension
- exist
- nparam
;
3576 /* If EP in its fractional maps only contains references
3577 * to the remainder parameter with appropriate coefficients
3578 * then we could in principle avoid adding existentially
3579 * quantified variables to the validity domains.
3580 * We'd have to replace the remainder by m { p/m }
3581 * and multiply with an appropriate factor that is one
3582 * only in the appropriate range.
3583 * This last multiplication can be avoided if EP
3584 * has a single validity domain with no (further)
3585 * constraints on the remainder parameter
3588 Matrix
*CT
= Matrix_Alloc(nparam
+1, nparam
+3);
3589 Matrix
*M
= Matrix_Alloc(1, 1+nparam
+3);
3590 for (int j
= 0; j
< nparam
; ++j
)
3592 value_set_si(CT
->p
[j
][j
], 1);
3593 value_set_si(CT
->p
[p
][nparam
+1], 1);
3594 value_set_si(CT
->p
[nparam
][nparam
+2], 1);
3595 value_set_si(M
->p
[0][1+p
], -1);
3596 value_absolute(M
->p
[0][1+nparam
], P
->Ray
[0][1+nvar
+exist
+p
]);
3597 value_set_si(M
->p
[0][1+nparam
+1], 1);
3598 Polyhedron
*CEq
= Constraints2Polyhedron(M
, 1);
3600 addeliminatedparams_enum(EP
, CT
, CEq
, MaxRays
, nparam
);
3601 Polyhedron_Free(CEq
);
3607 static void enumerate_vd_add_ray(evalue
*EP
, Matrix
*Rays
, unsigned MaxRays
)
3609 if (value_notzero_p(EP
->d
))
3612 assert(EP
->x
.p
->type
== partition
);
3613 assert(EP
->x
.p
->pos
== EVALUE_DOMAIN(EP
->x
.p
->arr
[0])->Dimension
);
3614 for (int i
= 0; i
< EP
->x
.p
->size
/2; ++i
) {
3615 Polyhedron
*D
= EVALUE_DOMAIN(EP
->x
.p
->arr
[2*i
]);
3616 Polyhedron
*N
= DomainAddRays(D
, Rays
, MaxRays
);
3617 EVALUE_SET_DOMAIN(EP
->x
.p
->arr
[2*i
], N
);
3622 static evalue
* enumerate_line(Polyhedron
*P
,
3623 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
3629 fprintf(stderr
, "\nER: Line\n");
3630 #endif /* DEBUG_ER */
3632 int nvar
= P
->Dimension
- exist
- nparam
;
3634 for (i
= 0; i
< nparam
; ++i
)
3635 if (value_notzero_p(P
->Ray
[0][1+nvar
+exist
+i
]))
3638 for (j
= i
+1; j
< nparam
; ++j
)
3639 if (value_notzero_p(P
->Ray
[0][1+nvar
+exist
+i
]))
3641 assert(j
>= nparam
); // for now
3643 Matrix
*M
= Matrix_Alloc(2, P
->Dimension
+2);
3644 value_set_si(M
->p
[0][0], 1);
3645 value_set_si(M
->p
[0][1+nvar
+exist
+i
], 1);
3646 value_set_si(M
->p
[1][0], 1);
3647 value_set_si(M
->p
[1][1+nvar
+exist
+i
], -1);
3648 value_absolute(M
->p
[1][1+P
->Dimension
], P
->Ray
[0][1+nvar
+exist
+i
]);
3649 value_decrement(M
->p
[1][1+P
->Dimension
], M
->p
[1][1+P
->Dimension
]);
3650 Polyhedron
*S
= AddConstraints(M
->p
[0], 2, P
, MaxRays
);
3651 evalue
*EP
= barvinok_enumerate_e(S
, exist
, nparam
, MaxRays
);
3655 return enumerate_cyclic(P
, exist
, nparam
, EP
, 0, i
, MaxRays
);
3658 static int single_param_pos(Polyhedron
*P
, unsigned exist
, unsigned nparam
,
3661 int nvar
= P
->Dimension
- exist
- nparam
;
3662 if (First_Non_Zero(P
->Ray
[r
]+1, nvar
) != -1)
3664 int i
= First_Non_Zero(P
->Ray
[r
]+1+nvar
+exist
, nparam
);
3667 if (First_Non_Zero(P
->Ray
[r
]+1+nvar
+exist
+1, nparam
-i
-1) != -1)
3672 static evalue
* enumerate_remove_ray(Polyhedron
*P
, int r
,
3673 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
3676 fprintf(stderr
, "\nER: RedundantRay\n");
3677 #endif /* DEBUG_ER */
3681 value_set_si(one
, 1);
3682 int len
= P
->NbRays
-1;
3683 Matrix
*M
= Matrix_Alloc(2 * len
, P
->Dimension
+2);
3684 Vector_Copy(P
->Ray
[0], M
->p
[0], r
* (P
->Dimension
+2));
3685 Vector_Copy(P
->Ray
[r
+1], M
->p
[r
], (len
-r
) * (P
->Dimension
+2));
3686 for (int j
= 0; j
< P
->NbRays
; ++j
) {
3689 Vector_Combine(P
->Ray
[j
], P
->Ray
[r
], M
->p
[len
+j
-(j
>r
)],
3690 one
, P
->Ray
[j
][P
->Dimension
+1], P
->Dimension
+2);
3693 P
= Rays2Polyhedron(M
, MaxRays
);
3695 evalue
*EP
= barvinok_enumerate_e(P
, exist
, nparam
, MaxRays
);
3702 static evalue
* enumerate_redundant_ray(Polyhedron
*P
,
3703 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
3705 assert(P
->NbBid
== 0);
3706 int nvar
= P
->Dimension
- exist
- nparam
;
3710 for (int r
= 0; r
< P
->NbRays
; ++r
) {
3711 if (value_notzero_p(P
->Ray
[r
][P
->Dimension
+1]))
3713 int i1
= single_param_pos(P
, exist
, nparam
, r
);
3716 for (int r2
= r
+1; r2
< P
->NbRays
; ++r2
) {
3717 if (value_notzero_p(P
->Ray
[r2
][P
->Dimension
+1]))
3719 int i2
= single_param_pos(P
, exist
, nparam
, r2
);
3725 value_division(m
, P
->Ray
[r
][1+nvar
+exist
+i1
],
3726 P
->Ray
[r2
][1+nvar
+exist
+i1
]);
3727 value_multiply(m
, m
, P
->Ray
[r2
][1+nvar
+exist
+i1
]);
3728 /* r2 divides r => r redundant */
3729 if (value_eq(m
, P
->Ray
[r
][1+nvar
+exist
+i1
])) {
3731 return enumerate_remove_ray(P
, r
, exist
, nparam
, MaxRays
);
3734 value_division(m
, P
->Ray
[r2
][1+nvar
+exist
+i1
],
3735 P
->Ray
[r
][1+nvar
+exist
+i1
]);
3736 value_multiply(m
, m
, P
->Ray
[r
][1+nvar
+exist
+i1
]);
3737 /* r divides r2 => r2 redundant */
3738 if (value_eq(m
, P
->Ray
[r2
][1+nvar
+exist
+i1
])) {
3740 return enumerate_remove_ray(P
, r2
, exist
, nparam
, MaxRays
);
3748 static Polyhedron
*upper_bound(Polyhedron
*P
,
3749 int pos
, Value
*max
, Polyhedron
**R
)
3758 for (Polyhedron
*Q
= P
; Q
; Q
= N
) {
3760 for (r
= 0; r
< P
->NbRays
; ++r
) {
3761 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1]) &&
3762 value_pos_p(P
->Ray
[r
][1+pos
]))
3765 if (r
< P
->NbRays
) {
3773 for (r
= 0; r
< P
->NbRays
; ++r
) {
3774 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1]))
3776 mpz_fdiv_q(v
, P
->Ray
[r
][1+pos
], P
->Ray
[r
][1+P
->Dimension
]);
3777 if ((!Q
->next
&& r
== 0) || value_gt(v
, *max
))
3778 value_assign(*max
, v
);
3785 static evalue
* enumerate_ray(Polyhedron
*P
,
3786 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
3788 assert(P
->NbBid
== 0);
3789 int nvar
= P
->Dimension
- exist
- nparam
;
3792 for (r
= 0; r
< P
->NbRays
; ++r
)
3793 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1]))
3799 for (r2
= r
+1; r2
< P
->NbRays
; ++r2
)
3800 if (value_zero_p(P
->Ray
[r2
][P
->Dimension
+1]))
3802 if (r2
< P
->NbRays
) {
3804 return enumerate_sum(P
, exist
, nparam
, MaxRays
);
3808 fprintf(stderr
, "\nER: Ray\n");
3809 #endif /* DEBUG_ER */
3815 value_set_si(one
, 1);
3816 int i
= single_param_pos(P
, exist
, nparam
, r
);
3817 assert(i
!= -1); // for now;
3819 Matrix
*M
= Matrix_Alloc(P
->NbRays
, P
->Dimension
+2);
3820 for (int j
= 0; j
< P
->NbRays
; ++j
) {
3821 Vector_Combine(P
->Ray
[j
], P
->Ray
[r
], M
->p
[j
],
3822 one
, P
->Ray
[j
][P
->Dimension
+1], P
->Dimension
+2);
3824 Polyhedron
*S
= Rays2Polyhedron(M
, MaxRays
);
3826 Polyhedron
*D
= DomainDifference(P
, S
, MaxRays
);
3828 // Polyhedron_Print(stderr, P_VALUE_FMT, D);
3829 assert(value_pos_p(P
->Ray
[r
][1+nvar
+exist
+i
])); // for now
3831 D
= upper_bound(D
, nvar
+exist
+i
, &m
, &R
);
3835 M
= Matrix_Alloc(2, P
->Dimension
+2);
3836 value_set_si(M
->p
[0][0], 1);
3837 value_set_si(M
->p
[1][0], 1);
3838 value_set_si(M
->p
[0][1+nvar
+exist
+i
], -1);
3839 value_set_si(M
->p
[1][1+nvar
+exist
+i
], 1);
3840 value_assign(M
->p
[0][1+P
->Dimension
], m
);
3841 value_oppose(M
->p
[1][1+P
->Dimension
], m
);
3842 value_addto(M
->p
[1][1+P
->Dimension
], M
->p
[1][1+P
->Dimension
],
3843 P
->Ray
[r
][1+nvar
+exist
+i
]);
3844 value_decrement(M
->p
[1][1+P
->Dimension
], M
->p
[1][1+P
->Dimension
]);
3845 // Matrix_Print(stderr, P_VALUE_FMT, M);
3846 D
= AddConstraints(M
->p
[0], 2, P
, MaxRays
);
3847 // Polyhedron_Print(stderr, P_VALUE_FMT, D);
3848 value_subtract(M
->p
[0][1+P
->Dimension
], M
->p
[0][1+P
->Dimension
],
3849 P
->Ray
[r
][1+nvar
+exist
+i
]);
3850 // Matrix_Print(stderr, P_VALUE_FMT, M);
3851 S
= AddConstraints(M
->p
[0], 1, P
, MaxRays
);
3852 // Polyhedron_Print(stderr, P_VALUE_FMT, S);
3855 evalue
*EP
= barvinok_enumerate_e(D
, exist
, nparam
, MaxRays
);
3860 if (value_notone_p(P
->Ray
[r
][1+nvar
+exist
+i
]))
3861 EP
= enumerate_cyclic(P
, exist
, nparam
, EP
, r
, i
, MaxRays
);
3863 M
= Matrix_Alloc(1, nparam
+2);
3864 value_set_si(M
->p
[0][0], 1);
3865 value_set_si(M
->p
[0][1+i
], 1);
3866 enumerate_vd_add_ray(EP
, M
, MaxRays
);
3871 evalue
*E
= barvinok_enumerate_e(S
, exist
, nparam
, MaxRays
);
3873 free_evalue_refs(E
);
3880 evalue
*ER
= enumerate_or(R
, exist
, nparam
, MaxRays
);
3882 free_evalue_refs(ER
);
3889 static evalue
* enumerate_vd(Polyhedron
**PA
,
3890 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
3892 Polyhedron
*P
= *PA
;
3893 int nvar
= P
->Dimension
- exist
- nparam
;
3894 Param_Polyhedron
*PP
= NULL
;
3895 Polyhedron
*C
= Universe_Polyhedron(nparam
);
3899 PP
= Polyhedron2Param_SimplifiedDomain(&PR
,C
,MaxRays
,&CEq
,&CT
);
3903 Param_Domain
*D
, *last
;
3906 for (nd
= 0, D
=PP
->D
; D
; D
=D
->next
, ++nd
)
3909 Polyhedron
**VD
= new Polyhedron_p
[nd
];
3910 Polyhedron
**fVD
= new Polyhedron_p
[nd
];
3911 for(nd
= 0, D
=PP
->D
; D
; D
=D
->next
) {
3912 Polyhedron
*rVD
= reduce_domain(D
->Domain
, CT
, CEq
,
3926 /* This doesn't seem to have any effect */
3928 Polyhedron
*CA
= align_context(VD
[0], P
->Dimension
, MaxRays
);
3930 P
= DomainIntersection(P
, CA
, MaxRays
);
3933 Polyhedron_Free(CA
);
3938 if (!EP
&& CT
->NbColumns
!= CT
->NbRows
) {
3939 Polyhedron
*CEqr
= DomainImage(CEq
, CT
, MaxRays
);
3940 Polyhedron
*CA
= align_context(CEqr
, PR
->Dimension
, MaxRays
);
3941 Polyhedron
*I
= DomainIntersection(PR
, CA
, MaxRays
);
3942 Polyhedron_Free(CEqr
);
3943 Polyhedron_Free(CA
);
3945 fprintf(stderr
, "\nER: Eliminate\n");
3946 #endif /* DEBUG_ER */
3947 nparam
-= CT
->NbColumns
- CT
->NbRows
;
3948 EP
= barvinok_enumerate_e(I
, exist
, nparam
, MaxRays
);
3949 nparam
+= CT
->NbColumns
- CT
->NbRows
;
3950 addeliminatedparams_enum(EP
, CT
, CEq
, MaxRays
, nparam
);
3954 Polyhedron_Free(PR
);
3957 if (!EP
&& nd
> 1) {
3959 fprintf(stderr
, "\nER: VD\n");
3960 #endif /* DEBUG_ER */
3961 for (int i
= 0; i
< nd
; ++i
) {
3962 Polyhedron
*CA
= align_context(VD
[i
], P
->Dimension
, MaxRays
);
3963 Polyhedron
*I
= DomainIntersection(P
, CA
, MaxRays
);
3966 EP
= barvinok_enumerate_e(I
, exist
, nparam
, MaxRays
);
3968 evalue
*E
= barvinok_enumerate_e(I
, exist
, nparam
, MaxRays
);
3970 free_evalue_refs(E
);
3974 Polyhedron_Free(CA
);
3978 for (int i
= 0; i
< nd
; ++i
) {
3979 Polyhedron_Free(VD
[i
]);
3980 Polyhedron_Free(fVD
[i
]);
3986 if (!EP
&& nvar
== 0) {
3989 Param_Vertices
*V
, *V2
;
3990 Matrix
* M
= Matrix_Alloc(1, P
->Dimension
+2);
3992 FORALL_PVertex_in_ParamPolyhedron(V
, last
, PP
) {
3994 FORALL_PVertex_in_ParamPolyhedron(V2
, last
, PP
) {
4001 for (int i
= 0; i
< exist
; ++i
) {
4002 value_oppose(f
, V
->Vertex
->p
[i
][nparam
+1]);
4003 Vector_Combine(V
->Vertex
->p
[i
],
4005 M
->p
[0] + 1 + nvar
+ exist
,
4006 V2
->Vertex
->p
[i
][nparam
+1],
4010 for (j
= 0; j
< nparam
; ++j
)
4011 if (value_notzero_p(M
->p
[0][1+nvar
+exist
+j
]))
4015 ConstraintSimplify(M
->p
[0], M
->p
[0],
4016 P
->Dimension
+2, &f
);
4017 value_set_si(M
->p
[0][0], 0);
4018 Polyhedron
*para
= AddConstraints(M
->p
[0], 1, P
,
4021 Polyhedron_Free(para
);
4024 Polyhedron
*pos
, *neg
;
4025 value_set_si(M
->p
[0][0], 1);
4026 value_decrement(M
->p
[0][P
->Dimension
+1],
4027 M
->p
[0][P
->Dimension
+1]);
4028 neg
= AddConstraints(M
->p
[0], 1, P
, MaxRays
);
4029 value_set_si(f
, -1);
4030 Vector_Scale(M
->p
[0]+1, M
->p
[0]+1, f
,
4032 value_decrement(M
->p
[0][P
->Dimension
+1],
4033 M
->p
[0][P
->Dimension
+1]);
4034 value_decrement(M
->p
[0][P
->Dimension
+1],
4035 M
->p
[0][P
->Dimension
+1]);
4036 pos
= AddConstraints(M
->p
[0], 1, P
, MaxRays
);
4037 if (emptyQ(neg
) && emptyQ(pos
)) {
4038 Polyhedron_Free(para
);
4039 Polyhedron_Free(pos
);
4040 Polyhedron_Free(neg
);
4044 fprintf(stderr
, "\nER: Order\n");
4045 #endif /* DEBUG_ER */
4046 EP
= barvinok_enumerate_e(para
, exist
, nparam
, MaxRays
);
4049 E
= barvinok_enumerate_e(pos
, exist
, nparam
, MaxRays
);
4051 free_evalue_refs(E
);
4055 E
= barvinok_enumerate_e(neg
, exist
, nparam
, MaxRays
);
4057 free_evalue_refs(E
);
4060 Polyhedron_Free(para
);
4061 Polyhedron_Free(pos
);
4062 Polyhedron_Free(neg
);
4067 } END_FORALL_PVertex_in_ParamPolyhedron
;
4070 } END_FORALL_PVertex_in_ParamPolyhedron
;
4073 /* Search for vertex coordinate to split on */
4074 /* First look for one independent of the parameters */
4075 FORALL_PVertex_in_ParamPolyhedron(V
, last
, PP
) {
4076 for (int i
= 0; i
< exist
; ++i
) {
4078 for (j
= 0; j
< nparam
; ++j
)
4079 if (value_notzero_p(V
->Vertex
->p
[i
][j
]))
4083 value_set_si(M
->p
[0][0], 1);
4084 Vector_Set(M
->p
[0]+1, 0, nvar
+exist
);
4085 Vector_Copy(V
->Vertex
->p
[i
],
4086 M
->p
[0] + 1 + nvar
+ exist
, nparam
+1);
4087 value_oppose(M
->p
[0][1+nvar
+i
],
4088 V
->Vertex
->p
[i
][nparam
+1]);
4090 Polyhedron
*pos
, *neg
;
4091 value_set_si(M
->p
[0][0], 1);
4092 value_decrement(M
->p
[0][P
->Dimension
+1],
4093 M
->p
[0][P
->Dimension
+1]);
4094 neg
= AddConstraints(M
->p
[0], 1, P
, MaxRays
);
4095 value_set_si(f
, -1);
4096 Vector_Scale(M
->p
[0]+1, M
->p
[0]+1, f
,
4098 value_decrement(M
->p
[0][P
->Dimension
+1],
4099 M
->p
[0][P
->Dimension
+1]);
4100 value_decrement(M
->p
[0][P
->Dimension
+1],
4101 M
->p
[0][P
->Dimension
+1]);
4102 pos
= AddConstraints(M
->p
[0], 1, P
, MaxRays
);
4103 if (emptyQ(neg
) || emptyQ(pos
)) {
4104 Polyhedron_Free(pos
);
4105 Polyhedron_Free(neg
);
4108 Polyhedron_Free(pos
);
4109 value_increment(M
->p
[0][P
->Dimension
+1],
4110 M
->p
[0][P
->Dimension
+1]);
4111 pos
= AddConstraints(M
->p
[0], 1, P
, MaxRays
);
4113 fprintf(stderr
, "\nER: Vertex\n");
4114 #endif /* DEBUG_ER */
4116 EP
= enumerate_or(pos
, exist
, nparam
, MaxRays
);
4121 } END_FORALL_PVertex_in_ParamPolyhedron
;
4125 /* Search for vertex coordinate to split on */
4126 /* Now look for one that depends on the parameters */
4127 FORALL_PVertex_in_ParamPolyhedron(V
, last
, PP
) {
4128 for (int i
= 0; i
< exist
; ++i
) {
4129 value_set_si(M
->p
[0][0], 1);
4130 Vector_Set(M
->p
[0]+1, 0, nvar
+exist
);
4131 Vector_Copy(V
->Vertex
->p
[i
],
4132 M
->p
[0] + 1 + nvar
+ exist
, nparam
+1);
4133 value_oppose(M
->p
[0][1+nvar
+i
],
4134 V
->Vertex
->p
[i
][nparam
+1]);
4136 Polyhedron
*pos
, *neg
;
4137 value_set_si(M
->p
[0][0], 1);
4138 value_decrement(M
->p
[0][P
->Dimension
+1],
4139 M
->p
[0][P
->Dimension
+1]);
4140 neg
= AddConstraints(M
->p
[0], 1, P
, MaxRays
);
4141 value_set_si(f
, -1);
4142 Vector_Scale(M
->p
[0]+1, M
->p
[0]+1, f
,
4144 value_decrement(M
->p
[0][P
->Dimension
+1],
4145 M
->p
[0][P
->Dimension
+1]);
4146 value_decrement(M
->p
[0][P
->Dimension
+1],
4147 M
->p
[0][P
->Dimension
+1]);
4148 pos
= AddConstraints(M
->p
[0], 1, P
, MaxRays
);
4149 if (emptyQ(neg
) || emptyQ(pos
)) {
4150 Polyhedron_Free(pos
);
4151 Polyhedron_Free(neg
);
4154 Polyhedron_Free(pos
);
4155 value_increment(M
->p
[0][P
->Dimension
+1],
4156 M
->p
[0][P
->Dimension
+1]);
4157 pos
= AddConstraints(M
->p
[0], 1, P
, MaxRays
);
4159 fprintf(stderr
, "\nER: ParamVertex\n");
4160 #endif /* DEBUG_ER */
4162 EP
= enumerate_or(pos
, exist
, nparam
, MaxRays
);
4167 } END_FORALL_PVertex_in_ParamPolyhedron
;
4175 Polyhedron_Free(CEq
);
4179 Param_Polyhedron_Free(PP
);
4186 evalue
*barvinok_enumerate_pip(Polyhedron
*P
,
4187 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
4192 evalue
*barvinok_enumerate_pip(Polyhedron
*P
,
4193 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
4195 int nvar
= P
->Dimension
- exist
- nparam
;
4196 evalue
*EP
= evalue_zero();
4200 fprintf(stderr
, "\nER: PIP\n");
4201 #endif /* DEBUG_ER */
4203 Polyhedron
*D
= pip_projectout(P
, nvar
, exist
, nparam
);
4204 for (Q
= D
; Q
; Q
= N
) {
4208 exist
= Q
->Dimension
- nvar
- nparam
;
4209 E
= barvinok_enumerate_e(Q
, exist
, nparam
, MaxRays
);
4212 free_evalue_refs(E
);
4221 static bool is_single(Value
*row
, int pos
, int len
)
4223 return First_Non_Zero(row
, pos
) == -1 &&
4224 First_Non_Zero(row
+pos
+1, len
-pos
-1) == -1;
4227 static evalue
* barvinok_enumerate_e_r(Polyhedron
*P
,
4228 unsigned exist
, unsigned nparam
, unsigned MaxRays
);
4231 static int er_level
= 0;
4233 evalue
* barvinok_enumerate_e(Polyhedron
*P
,
4234 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
4236 fprintf(stderr
, "\nER: level %i\n", er_level
);
4238 Polyhedron_PrintConstraints(stderr
, P_VALUE_FMT
, P
);
4239 fprintf(stderr
, "\nE %d\nP %d\n", exist
, nparam
);
4241 P
= DomainConstraintSimplify(Polyhedron_Copy(P
), MaxRays
);
4242 evalue
*EP
= barvinok_enumerate_e_r(P
, exist
, nparam
, MaxRays
);
4248 evalue
* barvinok_enumerate_e(Polyhedron
*P
,
4249 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
4251 P
= DomainConstraintSimplify(Polyhedron_Copy(P
), MaxRays
);
4252 evalue
*EP
= barvinok_enumerate_e_r(P
, exist
, nparam
, MaxRays
);
4258 static evalue
* barvinok_enumerate_e_r(Polyhedron
*P
,
4259 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
4262 Polyhedron
*U
= Universe_Polyhedron(nparam
);
4263 evalue
*EP
= barvinok_enumerate_ev(P
, U
, MaxRays
);
4264 //char *param_name[] = {"P", "Q", "R", "S", "T" };
4265 //print_evalue(stdout, EP, param_name);
4270 int nvar
= P
->Dimension
- exist
- nparam
;
4271 int len
= P
->Dimension
+ 2;
4274 POL_ENSURE_FACETS(P
);
4275 POL_ENSURE_VERTICES(P
);
4278 return evalue_zero();
4280 if (nvar
== 0 && nparam
== 0) {
4281 evalue
*EP
= evalue_zero();
4282 barvinok_count(P
, &EP
->x
.n
, MaxRays
);
4283 if (value_pos_p(EP
->x
.n
))
4284 value_set_si(EP
->x
.n
, 1);
4289 for (r
= 0; r
< P
->NbRays
; ++r
)
4290 if (value_zero_p(P
->Ray
[r
][0]) ||
4291 value_zero_p(P
->Ray
[r
][P
->Dimension
+1])) {
4293 for (i
= 0; i
< nvar
; ++i
)
4294 if (value_notzero_p(P
->Ray
[r
][i
+1]))
4298 for (i
= nvar
+ exist
; i
< nvar
+ exist
+ nparam
; ++i
)
4299 if (value_notzero_p(P
->Ray
[r
][i
+1]))
4301 if (i
>= nvar
+ exist
+ nparam
)
4304 if (r
< P
->NbRays
) {
4305 evalue
*EP
= evalue_zero();
4306 value_set_si(EP
->x
.n
, -1);
4311 for (r
= 0; r
< P
->NbEq
; ++r
)
4312 if ((first
= First_Non_Zero(P
->Constraint
[r
]+1+nvar
, exist
)) != -1)
4315 if (First_Non_Zero(P
->Constraint
[r
]+1+nvar
+first
+1,
4316 exist
-first
-1) != -1) {
4317 Polyhedron
*T
= rotate_along(P
, r
, nvar
, exist
, MaxRays
);
4319 fprintf(stderr
, "\nER: Equality\n");
4320 #endif /* DEBUG_ER */
4321 evalue
*EP
= barvinok_enumerate_e(T
, exist
-1, nparam
, MaxRays
);
4326 fprintf(stderr
, "\nER: Fixed\n");
4327 #endif /* DEBUG_ER */
4329 return barvinok_enumerate_e(P
, exist
-1, nparam
, MaxRays
);
4331 Polyhedron
*T
= Polyhedron_Copy(P
);
4332 SwapColumns(T
, nvar
+1, nvar
+1+first
);
4333 evalue
*EP
= barvinok_enumerate_e(T
, exist
-1, nparam
, MaxRays
);
4340 Vector
*row
= Vector_Alloc(len
);
4341 value_set_si(row
->p
[0], 1);
4346 enum constraint
* info
= new constraint
[exist
];
4347 for (int i
= 0; i
< exist
; ++i
) {
4349 for (int l
= P
->NbEq
; l
< P
->NbConstraints
; ++l
) {
4350 if (value_negz_p(P
->Constraint
[l
][nvar
+i
+1]))
4352 bool l_parallel
= is_single(P
->Constraint
[l
]+nvar
+1, i
, exist
);
4353 for (int u
= P
->NbEq
; u
< P
->NbConstraints
; ++u
) {
4354 if (value_posz_p(P
->Constraint
[u
][nvar
+i
+1]))
4356 bool lu_parallel
= l_parallel
||
4357 is_single(P
->Constraint
[u
]+nvar
+1, i
, exist
);
4358 value_oppose(f
, P
->Constraint
[u
][nvar
+i
+1]);
4359 Vector_Combine(P
->Constraint
[l
]+1, P
->Constraint
[u
]+1, row
->p
+1,
4360 f
, P
->Constraint
[l
][nvar
+i
+1], len
-1);
4361 if (!(info
[i
] & INDEPENDENT
)) {
4363 for (j
= 0; j
< exist
; ++j
)
4364 if (j
!= i
&& value_notzero_p(row
->p
[nvar
+j
+1]))
4367 //printf("independent: i: %d, l: %d, u: %d\n", i, l, u);
4368 info
[i
] = (constraint
)(info
[i
] | INDEPENDENT
);
4371 if (info
[i
] & ALL_POS
) {
4372 value_addto(row
->p
[len
-1], row
->p
[len
-1],
4373 P
->Constraint
[l
][nvar
+i
+1]);
4374 value_addto(row
->p
[len
-1], row
->p
[len
-1], f
);
4375 value_multiply(f
, f
, P
->Constraint
[l
][nvar
+i
+1]);
4376 value_subtract(row
->p
[len
-1], row
->p
[len
-1], f
);
4377 value_decrement(row
->p
[len
-1], row
->p
[len
-1]);
4378 ConstraintSimplify(row
->p
, row
->p
, len
, &f
);
4379 value_set_si(f
, -1);
4380 Vector_Scale(row
->p
+1, row
->p
+1, f
, len
-1);
4381 value_decrement(row
->p
[len
-1], row
->p
[len
-1]);
4382 Polyhedron
*T
= AddConstraints(row
->p
, 1, P
, MaxRays
);
4384 //printf("not all_pos: i: %d, l: %d, u: %d\n", i, l, u);
4385 info
[i
] = (constraint
)(info
[i
] ^ ALL_POS
);
4387 //puts("pos remainder");
4388 //Polyhedron_Print(stdout, P_VALUE_FMT, T);
4391 if (!(info
[i
] & ONE_NEG
)) {
4393 negative_test_constraint(P
->Constraint
[l
],
4395 row
->p
, nvar
+i
, len
, &f
);
4396 oppose_constraint(row
->p
, len
, &f
);
4397 Polyhedron
*T
= AddConstraints(row
->p
, 1, P
, MaxRays
);
4399 //printf("one_neg i: %d, l: %d, u: %d\n", i, l, u);
4400 info
[i
] = (constraint
)(info
[i
] | ONE_NEG
);
4402 //puts("neg remainder");
4403 //Polyhedron_Print(stdout, P_VALUE_FMT, T);
4405 } else if (!(info
[i
] & ROT_NEG
)) {
4406 if (parallel_constraints(P
->Constraint
[l
],
4408 row
->p
, nvar
, exist
)) {
4409 negative_test_constraint7(P
->Constraint
[l
],
4411 row
->p
, nvar
, exist
,
4413 oppose_constraint(row
->p
, len
, &f
);
4414 Polyhedron
*T
= AddConstraints(row
->p
, 1, P
, MaxRays
);
4416 // printf("rot_neg i: %d, l: %d, u: %d\n", i, l, u);
4417 info
[i
] = (constraint
)(info
[i
] | ROT_NEG
);
4420 //puts("neg remainder");
4421 //Polyhedron_Print(stdout, P_VALUE_FMT, T);
4426 if (!(info
[i
] & ALL_POS
) && (info
[i
] & (ONE_NEG
| ROT_NEG
)))
4430 if (info
[i
] & ALL_POS
)
4437 for (int i = 0; i < exist; ++i)
4438 printf("%i: %i\n", i, info[i]);
4440 for (int i
= 0; i
< exist
; ++i
)
4441 if (info
[i
] & ALL_POS
) {
4443 fprintf(stderr
, "\nER: Positive\n");
4444 #endif /* DEBUG_ER */
4446 // Maybe we should chew off some of the fat here
4447 Matrix
*M
= Matrix_Alloc(P
->Dimension
, P
->Dimension
+1);
4448 for (int j
= 0; j
< P
->Dimension
; ++j
)
4449 value_set_si(M
->p
[j
][j
+ (j
>= i
+nvar
)], 1);
4450 Polyhedron
*T
= Polyhedron_Image(P
, M
, MaxRays
);
4452 evalue
*EP
= barvinok_enumerate_e(T
, exist
-1, nparam
, MaxRays
);
4459 for (int i
= 0; i
< exist
; ++i
)
4460 if (info
[i
] & ONE_NEG
) {
4462 fprintf(stderr
, "\nER: Negative\n");
4463 #endif /* DEBUG_ER */
4468 return barvinok_enumerate_e(P
, exist
-1, nparam
, MaxRays
);
4470 Polyhedron
*T
= Polyhedron_Copy(P
);
4471 SwapColumns(T
, nvar
+1, nvar
+1+i
);
4472 evalue
*EP
= barvinok_enumerate_e(T
, exist
-1, nparam
, MaxRays
);
4477 for (int i
= 0; i
< exist
; ++i
)
4478 if (info
[i
] & ROT_NEG
) {
4480 fprintf(stderr
, "\nER: Rotate\n");
4481 #endif /* DEBUG_ER */
4485 Polyhedron
*T
= rotate_along(P
, r
, nvar
, exist
, MaxRays
);
4486 evalue
*EP
= barvinok_enumerate_e(T
, exist
-1, nparam
, MaxRays
);
4490 for (int i
= 0; i
< exist
; ++i
)
4491 if (info
[i
] & INDEPENDENT
) {
4492 Polyhedron
*pos
, *neg
;
4494 /* Find constraint again and split off negative part */
4496 if (SplitOnVar(P
, i
, nvar
, exist
, MaxRays
,
4497 row
, f
, true, &pos
, &neg
)) {
4499 fprintf(stderr
, "\nER: Split\n");
4500 #endif /* DEBUG_ER */
4503 barvinok_enumerate_e(neg
, exist
-1, nparam
, MaxRays
);
4505 barvinok_enumerate_e(pos
, exist
, nparam
, MaxRays
);
4507 free_evalue_refs(E
);
4509 Polyhedron_Free(neg
);
4510 Polyhedron_Free(pos
);
4524 EP
= enumerate_line(P
, exist
, nparam
, MaxRays
);
4528 EP
= barvinok_enumerate_pip(P
, exist
, nparam
, MaxRays
);
4532 EP
= enumerate_redundant_ray(P
, exist
, nparam
, MaxRays
);
4536 EP
= enumerate_sure(P
, exist
, nparam
, MaxRays
);
4540 EP
= enumerate_ray(P
, exist
, nparam
, MaxRays
);
4544 EP
= enumerate_sure2(P
, exist
, nparam
, MaxRays
);
4548 F
= unfringe(P
, MaxRays
);
4549 if (!PolyhedronIncludes(F
, P
)) {
4551 fprintf(stderr
, "\nER: Fringed\n");
4552 #endif /* DEBUG_ER */
4553 EP
= barvinok_enumerate_e(F
, exist
, nparam
, MaxRays
);
4560 EP
= enumerate_vd(&P
, exist
, nparam
, MaxRays
);
4565 EP
= enumerate_sum(P
, exist
, nparam
, MaxRays
);
4572 Polyhedron
*pos
, *neg
;
4573 for (i
= 0; i
< exist
; ++i
)
4574 if (SplitOnVar(P
, i
, nvar
, exist
, MaxRays
,
4575 row
, f
, false, &pos
, &neg
))
4581 EP
= enumerate_or(pos
, exist
, nparam
, MaxRays
);
4593 static void split_param_compression(Matrix
*CP
, mat_ZZ
& map
, vec_ZZ
& offset
)
4595 Matrix
*T
= Transpose(CP
);
4596 matrix2zz(T
, map
, T
->NbRows
-1, T
->NbColumns
-1);
4597 values2zz(T
->p
[T
->NbRows
-1], offset
, T
->NbColumns
-1);
4602 * remove equalities that require a "compression" of the parameters
4604 #ifndef HAVE_COMPRESS_PARMS
4605 static Polyhedron
*remove_more_equalities(Polyhedron
*P
, unsigned nparam
,
4606 Matrix
**CP
, unsigned MaxRays
)
4611 static Polyhedron
*remove_more_equalities(Polyhedron
*P
, unsigned nparam
,
4612 Matrix
**CP
, unsigned MaxRays
)
4617 /* compress_parms doesn't like equalities that only involve parameters */
4618 for (int i
= 0; i
< P
->NbEq
; ++i
)
4619 assert(First_Non_Zero(P
->Constraint
[i
]+1, P
->Dimension
-nparam
) != -1);
4621 M
= Matrix_Alloc(P
->NbEq
, P
->Dimension
+2);
4622 Vector_Copy(P
->Constraint
[0], M
->p
[0], P
->NbEq
* (P
->Dimension
+2));
4623 *CP
= compress_parms(M
, nparam
);
4624 T
= align_matrix(*CP
, P
->Dimension
+1);
4625 Q
= Polyhedron_Preimage(P
, T
, MaxRays
);
4628 P
= remove_equalities_p(P
, P
->Dimension
-nparam
, NULL
);
4635 gen_fun
* barvinok_series(Polyhedron
*P
, Polyhedron
* C
, unsigned MaxRays
)
4639 unsigned nparam
= C
->Dimension
;
4641 CA
= align_context(C
, P
->Dimension
, MaxRays
);
4642 P
= DomainIntersection(P
, CA
, MaxRays
);
4643 Polyhedron_Free(CA
);
4650 assert(!Polyhedron_is_infinite(P
, nparam
));
4651 assert(P
->NbBid
== 0);
4652 assert(Polyhedron_has_positive_rays(P
, nparam
));
4654 P
= remove_equalities_p(P
, P
->Dimension
-nparam
, NULL
);
4656 P
= remove_more_equalities(P
, nparam
, &CP
, MaxRays
);
4657 assert(P
->NbEq
== 0);
4659 #ifdef USE_INCREMENTAL_BF
4660 partial_bfcounter
red(P
, nparam
);
4661 #elif defined USE_INCREMENTAL_DF
4662 partial_ireducer
red(P
, nparam
);
4664 partial_reducer
red(P
, nparam
);
4671 split_param_compression(CP
, map
, offset
);
4672 red
.gf
->substitute(CP
, map
, offset
);
4678 static Polyhedron
*skew_into_positive_orthant(Polyhedron
*D
, unsigned nparam
,
4684 for (Polyhedron
*P
= D
; P
; P
= P
->next
) {
4685 POL_ENSURE_VERTICES(P
);
4686 assert(!Polyhedron_is_infinite(P
, nparam
));
4687 assert(P
->NbBid
== 0);
4688 assert(Polyhedron_has_positive_rays(P
, nparam
));
4690 for (int r
= 0; r
< P
->NbRays
; ++r
) {
4691 if (value_notzero_p(P
->Ray
[r
][P
->Dimension
+1]))
4693 for (int i
= 0; i
< nparam
; ++i
) {
4695 if (value_posz_p(P
->Ray
[r
][i
+1]))
4698 M
= Matrix_Alloc(D
->Dimension
+1, D
->Dimension
+1);
4699 for (int i
= 0; i
< D
->Dimension
+1; ++i
)
4700 value_set_si(M
->p
[i
][i
], 1);
4702 Inner_Product(P
->Ray
[r
]+1, M
->p
[i
], D
->Dimension
+1, &tmp
);
4703 if (value_posz_p(tmp
))
4706 for (j
= P
->Dimension
- nparam
; j
< P
->Dimension
; ++j
)
4707 if (value_pos_p(P
->Ray
[r
][j
+1]))
4709 assert(j
< P
->Dimension
);
4710 value_pdivision(tmp
, P
->Ray
[r
][j
+1], P
->Ray
[r
][i
+1]);
4711 value_subtract(M
->p
[i
][j
], M
->p
[i
][j
], tmp
);
4717 D
= DomainImage(D
, M
, MaxRays
);
4723 gen_fun
* barvinok_enumerate_union_series(Polyhedron
*D
, Polyhedron
* C
,
4726 Polyhedron
*conv
, *D2
;
4728 unsigned nparam
= C
->Dimension
;
4732 D2
= skew_into_positive_orthant(D
, nparam
, MaxRays
);
4733 for (Polyhedron
*P
= D2
; P
; P
= P
->next
) {
4734 assert(P
->Dimension
== D2
->Dimension
);
4735 POL_ENSURE_VERTICES(P
);
4736 /* it doesn't matter which reducer we use, since we don't actually
4737 * reduce anything here
4739 partial_reducer
red(P
, P
->Dimension
);
4744 gf
->add_union(red
.gf
, MaxRays
);
4748 /* we actually only need the convex union of the parameter space
4749 * but the reducer classes currently expect a polyhedron in
4750 * the combined space
4752 conv
= DomainConvex(D2
, MaxRays
);
4753 #ifdef USE_INCREMENTAL_DF
4754 partial_ireducer
red(conv
, nparam
);
4756 partial_reducer
red(conv
, nparam
);
4758 for (int i
= 0; i
< gf
->term
.size(); ++i
) {
4759 for (int j
= 0; j
< gf
->term
[i
]->n
.power
.NumRows(); ++j
) {
4760 red
.reduce(gf
->term
[i
]->n
.coeff
[j
][0], gf
->term
[i
]->n
.coeff
[j
][1],
4761 gf
->term
[i
]->n
.power
[j
], gf
->term
[i
]->d
.power
);
4767 Polyhedron_Free(conv
);
4771 evalue
* barvinok_enumerate_union(Polyhedron
*D
, Polyhedron
* C
, unsigned MaxRays
)
4774 gen_fun
*gf
= barvinok_enumerate_union_series(D
, C
, MaxRays
);