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 for (; r
< P
->NbRays
; ++r
)
1864 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1]))
1866 if (P
->NbBid
!=0 || r
< P
->NbRays
) {
1867 value_set_si(*result
, -1);
1871 P
= remove_equalities(P
);
1874 value_set_si(*result
, 0);
1879 if (P
->Dimension
== 0) {
1880 /* Test whether the constraints are satisfied */
1881 POL_ENSURE_VERTICES(P
);
1882 value_set_si(*result
, !emptyQ(P
));
1887 Q
= Polyhedron_Factor(P
, 0, NbMaxCons
);
1895 barvinok_count_f(P
, result
, NbMaxCons
);
1900 for (Q
= P
->next
; Q
; Q
= Q
->next
) {
1901 barvinok_count_f(Q
, &factor
, NbMaxCons
);
1902 if (value_neg_p(factor
)) {
1905 } else if (Q
->next
&& value_zero_p(factor
)) {
1906 value_set_si(*result
, 0);
1909 value_multiply(*result
, *result
, factor
);
1912 value_clear(factor
);
1918 value_set_si(*result
, -1);
1921 static void barvinok_count_f(Polyhedron
*P
, Value
* result
, unsigned NbMaxCons
)
1923 if (P
->Dimension
== 1)
1924 return Line_Length(P
, result
);
1926 int c
= P
->NbConstraints
;
1927 POL_ENSURE_FACETS(P
);
1928 if (c
!= P
->NbConstraints
|| P
->NbEq
!= 0)
1929 return barvinok_count(P
, result
, NbMaxCons
);
1931 POL_ENSURE_VERTICES(P
);
1933 #ifdef USE_INCREMENTAL_BF
1935 #elif defined USE_INCREMENTAL_DF
1940 cnt
.start(NbMaxCons
);
1942 assert(value_one_p(&cnt
.count
[0]._mp_den
));
1943 value_assign(*result
, &cnt
.count
[0]._mp_num
);
1946 static void uni_polynom(int param
, Vector
*c
, evalue
*EP
)
1948 unsigned dim
= c
->Size
-2;
1950 value_set_si(EP
->d
,0);
1951 EP
->x
.p
= new_enode(polynomial
, dim
+1, param
+1);
1952 for (int j
= 0; j
<= dim
; ++j
)
1953 evalue_set(&EP
->x
.p
->arr
[j
], c
->p
[j
], c
->p
[dim
+1]);
1956 static void multi_polynom(Vector
*c
, evalue
* X
, evalue
*EP
)
1958 unsigned dim
= c
->Size
-2;
1962 evalue_set(&EC
, c
->p
[dim
], c
->p
[dim
+1]);
1965 evalue_set(EP
, c
->p
[dim
], c
->p
[dim
+1]);
1967 for (int i
= dim
-1; i
>= 0; --i
) {
1969 value_assign(EC
.x
.n
, c
->p
[i
]);
1972 free_evalue_refs(&EC
);
1975 Polyhedron
*unfringe (Polyhedron
*P
, unsigned MaxRays
)
1977 int len
= P
->Dimension
+2;
1978 Polyhedron
*T
, *R
= P
;
1981 Vector
*row
= Vector_Alloc(len
);
1982 value_set_si(row
->p
[0], 1);
1984 R
= DomainConstraintSimplify(Polyhedron_Copy(P
), MaxRays
);
1986 Matrix
*M
= Matrix_Alloc(2, len
-1);
1987 value_set_si(M
->p
[1][len
-2], 1);
1988 for (int v
= 0; v
< P
->Dimension
; ++v
) {
1989 value_set_si(M
->p
[0][v
], 1);
1990 Polyhedron
*I
= Polyhedron_Image(P
, M
, 2+1);
1991 value_set_si(M
->p
[0][v
], 0);
1992 for (int r
= 0; r
< I
->NbConstraints
; ++r
) {
1993 if (value_zero_p(I
->Constraint
[r
][0]))
1995 if (value_zero_p(I
->Constraint
[r
][1]))
1997 if (value_one_p(I
->Constraint
[r
][1]))
1999 if (value_mone_p(I
->Constraint
[r
][1]))
2001 value_absolute(g
, I
->Constraint
[r
][1]);
2002 Vector_Set(row
->p
+1, 0, len
-2);
2003 value_division(row
->p
[1+v
], I
->Constraint
[r
][1], g
);
2004 mpz_fdiv_q(row
->p
[len
-1], I
->Constraint
[r
][2], g
);
2006 R
= AddConstraints(row
->p
, 1, R
, MaxRays
);
2018 /* this procedure may have false negatives */
2019 static bool Polyhedron_is_infinite(Polyhedron
*P
, unsigned nparam
)
2022 for (r
= 0; r
< P
->NbRays
; ++r
) {
2023 if (!value_zero_p(P
->Ray
[r
][0]) &&
2024 !value_zero_p(P
->Ray
[r
][P
->Dimension
+1]))
2026 if (First_Non_Zero(P
->Ray
[r
]+1+P
->Dimension
-nparam
, nparam
) == -1)
2032 /* Check whether all rays point in the positive directions
2033 * for the parameters
2035 static bool Polyhedron_has_positive_rays(Polyhedron
*P
, unsigned nparam
)
2038 for (r
= 0; r
< P
->NbRays
; ++r
)
2039 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1])) {
2041 for (i
= P
->Dimension
- nparam
; i
< P
->Dimension
; ++i
)
2042 if (value_neg_p(P
->Ray
[r
][i
+1]))
2048 typedef evalue
* evalue_p
;
2050 struct enumerator
: public polar_decomposer
{
2064 enumerator(Polyhedron
*P
, unsigned dim
, unsigned nbV
) {
2068 randomvector(P
, lambda
, dim
);
2069 rays
.SetDims(dim
, dim
);
2071 c
= Vector_Alloc(dim
+2);
2073 vE
= new evalue_p
[nbV
];
2074 for (int j
= 0; j
< nbV
; ++j
)
2080 void decompose_at(Param_Vertices
*V
, int _i
, unsigned MaxRays
) {
2081 Polyhedron
*C
= supporting_cone_p(P
, V
);
2085 vE
[_i
] = new evalue
;
2086 value_init(vE
[_i
]->d
);
2087 evalue_set_si(vE
[_i
], 0, 1);
2089 decompose(C
, MaxRays
);
2096 for (int j
= 0; j
< nbV
; ++j
)
2098 free_evalue_refs(vE
[j
]);
2104 virtual void handle_polar(Polyhedron
*P
, int sign
);
2107 void enumerator::handle_polar(Polyhedron
*C
, int s
)
2110 assert(C
->NbRays
-1 == dim
);
2111 add_rays(rays
, C
, &r
);
2112 for (int k
= 0; k
< dim
; ++k
) {
2113 if (lambda
* rays
[k
] == 0)
2119 lattice_point(V
, C
, lambda
, &num
, 0);
2120 den
= rays
* lambda
;
2121 normalize(sign
, num
.constant
, den
);
2123 dpoly
n(dim
, den
[0], 1);
2124 for (int k
= 1; k
< dim
; ++k
) {
2125 dpoly
fact(dim
, den
[k
], 1);
2128 if (num
.E
!= NULL
) {
2129 ZZ
one(INIT_VAL
, 1);
2130 dpoly_n
d(dim
, num
.constant
, one
);
2133 multi_polynom(c
, num
.E
, &EV
);
2135 free_evalue_refs(&EV
);
2136 free_evalue_refs(num
.E
);
2138 } else if (num
.pos
!= -1) {
2139 dpoly_n
d(dim
, num
.constant
, num
.coeff
);
2142 uni_polynom(num
.pos
, c
, &EV
);
2144 free_evalue_refs(&EV
);
2146 mpq_set_si(count
, 0, 1);
2147 dpoly
d(dim
, num
.constant
);
2148 d
.div(n
, count
, sign
);
2151 evalue_set(&EV
, &count
[0]._mp_num
, &count
[0]._mp_den
);
2153 free_evalue_refs(&EV
);
2157 struct enumerator_base
{
2162 vertex_decomposer
*vpd
;
2164 enumerator_base(unsigned dim
, vertex_decomposer
*vpd
)
2169 vE
= new evalue_p
[vpd
->nbV
];
2170 for (int j
= 0; j
< vpd
->nbV
; ++j
)
2173 E_vertex
= new evalue_p
[dim
];
2176 evalue_set_si(&mone
, -1, 1);
2179 void decompose_at(Param_Vertices
*V
, int _i
, unsigned MaxRays
/*, Polyhedron *pVD*/) {
2182 vE
[_i
] = new evalue
;
2183 value_init(vE
[_i
]->d
);
2184 evalue_set_si(vE
[_i
], 0, 1);
2186 vpd
->decompose_at_vertex(V
, _i
, MaxRays
);
2189 ~enumerator_base() {
2190 for (int j
= 0; j
< vpd
->nbV
; ++j
)
2192 free_evalue_refs(vE
[j
]);
2199 free_evalue_refs(&mone
);
2202 evalue
*E_num(int i
, int d
) {
2203 return E_vertex
[i
+ (dim
-d
)];
2212 cumulator(evalue
*factor
, evalue
*v
, dpoly_r
*r
) :
2213 factor(factor
), v(v
), r(r
) {}
2217 virtual void add_term(int *powers
, int len
, evalue
*f2
) = 0;
2220 void cumulator::cumulate()
2222 evalue cum
; // factor * 1 * E_num[0]/1 * (E_num[0]-1)/2 *...
2224 evalue t
; // E_num[0] - (m-1)
2230 evalue_set_si(&mone
, -1, 1);
2234 evalue_copy(&cum
, factor
);
2237 value_set_si(f
.d
, 1);
2238 value_set_si(f
.x
.n
, 1);
2243 for (cst
= &t
; value_zero_p(cst
->d
); ) {
2244 if (cst
->x
.p
->type
== fractional
)
2245 cst
= &cst
->x
.p
->arr
[1];
2247 cst
= &cst
->x
.p
->arr
[0];
2251 for (int m
= 0; m
< r
->len
; ++m
) {
2254 value_set_si(f
.d
, m
);
2257 value_subtract(cst
->x
.n
, cst
->x
.n
, cst
->d
);
2264 vector
< dpoly_r_term
* >& current
= r
->c
[r
->len
-1-m
];
2265 for (int j
= 0; j
< current
.size(); ++j
) {
2266 if (current
[j
]->coeff
== 0)
2268 evalue
*f2
= new evalue
;
2270 value_init(f2
->x
.n
);
2271 zz2value(current
[j
]->coeff
, f2
->x
.n
);
2272 zz2value(r
->denom
, f2
->d
);
2275 add_term(current
[j
]->powers
, r
->dim
, f2
);
2278 free_evalue_refs(&f
);
2279 free_evalue_refs(&t
);
2280 free_evalue_refs(&cum
);
2282 free_evalue_refs(&mone
);
2286 struct E_poly_term
{
2291 struct ie_cum
: public cumulator
{
2292 vector
<E_poly_term
*> terms
;
2294 ie_cum(evalue
*factor
, evalue
*v
, dpoly_r
*r
) : cumulator(factor
, v
, r
) {}
2296 virtual void add_term(int *powers
, int len
, evalue
*f2
);
2299 void ie_cum::add_term(int *powers
, int len
, evalue
*f2
)
2302 for (k
= 0; k
< terms
.size(); ++k
) {
2303 if (memcmp(terms
[k
]->powers
, powers
, len
* sizeof(int)) == 0) {
2304 eadd(f2
, terms
[k
]->E
);
2305 free_evalue_refs(f2
);
2310 if (k
>= terms
.size()) {
2311 E_poly_term
*ET
= new E_poly_term
;
2312 ET
->powers
= new int[len
];
2313 memcpy(ET
->powers
, powers
, len
* sizeof(int));
2315 terms
.push_back(ET
);
2319 struct ienumerator
: public polar_decomposer
, public vertex_decomposer
,
2320 public enumerator_base
{
2326 ienumerator(Polyhedron
*P
, unsigned dim
, unsigned nbV
) :
2327 vertex_decomposer(P
, nbV
, this), enumerator_base(dim
, this) {
2328 vertex
.SetLength(dim
);
2330 den
.SetDims(dim
, dim
);
2338 virtual void handle_polar(Polyhedron
*P
, int sign
);
2339 void reduce(evalue
*factor
, vec_ZZ
& num
, mat_ZZ
& den_f
);
2342 void ienumerator::reduce(
2343 evalue
*factor
, vec_ZZ
& num
, mat_ZZ
& den_f
)
2345 unsigned len
= den_f
.NumRows(); // number of factors in den
2346 unsigned dim
= num
.length();
2349 eadd(factor
, vE
[vert
]);
2354 den_s
.SetLength(len
);
2356 den_r
.SetDims(len
, dim
-1);
2360 for (r
= 0; r
< len
; ++r
) {
2361 den_s
[r
] = den_f
[r
][0];
2362 for (k
= 0; k
<= dim
-1; ++k
)
2364 den_r
[r
][k
-(k
>0)] = den_f
[r
][k
];
2369 num_p
.SetLength(dim
-1);
2370 for (k
= 0 ; k
<= dim
-1; ++k
)
2372 num_p
[k
-(k
>0)] = num
[k
];
2375 den_p
.SetLength(len
);
2379 normalize(one
, num_s
, num_p
, den_s
, den_p
, den_r
);
2381 emul(&mone
, factor
);
2385 for (int k
= 0; k
< len
; ++k
) {
2388 else if (den_s
[k
] == 0)
2391 if (no_param
== 0) {
2392 reduce(factor
, num_p
, den_r
);
2396 pden
.SetDims(only_param
, dim
-1);
2398 for (k
= 0, l
= 0; k
< len
; ++k
)
2400 pden
[l
++] = den_r
[k
];
2402 for (k
= 0; k
< len
; ++k
)
2406 dpoly
n(no_param
, num_s
);
2407 dpoly
D(no_param
, den_s
[k
], 1);
2408 for ( ; ++k
< len
; )
2409 if (den_p
[k
] == 0) {
2410 dpoly
fact(no_param
, den_s
[k
], 1);
2415 // if no_param + only_param == len then all powers
2416 // below will be all zero
2417 if (no_param
+ only_param
== len
) {
2418 if (E_num(0, dim
) != 0)
2419 r
= new dpoly_r(n
, len
);
2421 mpq_set_si(tcount
, 0, 1);
2423 n
.div(D
, tcount
, one
);
2425 if (value_notzero_p(mpq_numref(tcount
))) {
2429 value_assign(f
.x
.n
, mpq_numref(tcount
));
2430 value_assign(f
.d
, mpq_denref(tcount
));
2432 reduce(factor
, num_p
, pden
);
2433 free_evalue_refs(&f
);
2438 for (k
= 0; k
< len
; ++k
) {
2439 if (den_s
[k
] == 0 || den_p
[k
] == 0)
2442 dpoly
pd(no_param
-1, den_s
[k
], 1);
2445 for (l
= 0; l
< k
; ++l
)
2446 if (den_r
[l
] == den_r
[k
])
2450 r
= new dpoly_r(n
, pd
, l
, len
);
2452 dpoly_r
*nr
= new dpoly_r(r
, pd
, l
, len
);
2458 dpoly_r
*rc
= r
->div(D
);
2461 if (E_num(0, dim
) == 0) {
2462 int common
= pden
.NumRows();
2463 vector
< dpoly_r_term
* >& final
= r
->c
[r
->len
-1];
2469 zz2value(r
->denom
, f
.d
);
2470 for (int j
= 0; j
< final
.size(); ++j
) {
2471 if (final
[j
]->coeff
== 0)
2474 for (int k
= 0; k
< r
->dim
; ++k
) {
2475 int n
= final
[j
]->powers
[k
];
2478 pden
.SetDims(rows
+n
, pden
.NumCols());
2479 for (int l
= 0; l
< n
; ++l
)
2480 pden
[rows
+l
] = den_r
[k
];
2484 evalue_copy(&t
, factor
);
2485 zz2value(final
[j
]->coeff
, f
.x
.n
);
2487 reduce(&t
, num_p
, pden
);
2488 free_evalue_refs(&t
);
2490 free_evalue_refs(&f
);
2492 ie_cum
cum(factor
, E_num(0, dim
), r
);
2495 int common
= pden
.NumRows();
2497 for (int j
= 0; j
< cum
.terms
.size(); ++j
) {
2499 pden
.SetDims(rows
, pden
.NumCols());
2500 for (int k
= 0; k
< r
->dim
; ++k
) {
2501 int n
= cum
.terms
[j
]->powers
[k
];
2504 pden
.SetDims(rows
+n
, pden
.NumCols());
2505 for (int l
= 0; l
< n
; ++l
)
2506 pden
[rows
+l
] = den_r
[k
];
2509 reduce(cum
.terms
[j
]->E
, num_p
, pden
);
2510 free_evalue_refs(cum
.terms
[j
]->E
);
2511 delete cum
.terms
[j
]->E
;
2512 delete [] cum
.terms
[j
]->powers
;
2513 delete cum
.terms
[j
];
2520 static int type_offset(enode
*p
)
2522 return p
->type
== fractional
? 1 :
2523 p
->type
== flooring
? 1 : 0;
2526 static int edegree(evalue
*e
)
2531 if (value_notzero_p(e
->d
))
2535 int i
= type_offset(p
);
2536 if (p
->size
-i
-1 > d
)
2537 d
= p
->size
- i
- 1;
2538 for (; i
< p
->size
; i
++) {
2539 int d2
= edegree(&p
->arr
[i
]);
2546 void ienumerator::handle_polar(Polyhedron
*C
, int s
)
2548 assert(C
->NbRays
-1 == dim
);
2550 lattice_point(V
, C
, vertex
, E_vertex
);
2553 for (r
= 0; r
< dim
; ++r
)
2554 values2zz(C
->Ray
[r
]+1, den
[r
], dim
);
2558 evalue_set_si(&one
, s
, 1);
2559 reduce(&one
, vertex
, den
);
2560 free_evalue_refs(&one
);
2562 for (int i
= 0; i
< dim
; ++i
)
2564 free_evalue_refs(E_vertex
[i
]);
2569 struct bfenumerator
: public vertex_decomposer
, public bf_base
,
2570 public enumerator_base
{
2573 bfenumerator(Polyhedron
*P
, unsigned dim
, unsigned nbV
) :
2574 vertex_decomposer(P
, nbV
, this),
2575 bf_base(P
, dim
), enumerator_base(dim
, this) {
2583 virtual void handle_polar(Polyhedron
*P
, int sign
);
2584 virtual void base(mat_ZZ
& factors
, bfc_vec
& v
);
2586 bfc_term_base
* new_bf_term(int len
) {
2587 bfe_term
* t
= new bfe_term(len
);
2591 virtual void set_factor(bfc_term_base
*t
, int k
, int change
) {
2592 bfe_term
* bfet
= static_cast<bfe_term
*>(t
);
2593 factor
= bfet
->factors
[k
];
2594 assert(factor
!= NULL
);
2595 bfet
->factors
[k
] = NULL
;
2597 emul(&mone
, factor
);
2600 virtual void set_factor(bfc_term_base
*t
, int k
, mpq_t
&q
, 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
;
2610 value_oppose(f
.x
.n
, mpq_numref(q
));
2612 value_assign(f
.x
.n
, mpq_numref(q
));
2613 value_assign(f
.d
, mpq_denref(q
));
2617 virtual void set_factor(bfc_term_base
*t
, int k
, ZZ
& n
, ZZ
& d
, int change
) {
2618 bfe_term
* bfet
= static_cast<bfe_term
*>(t
);
2620 factor
= new evalue
;
2627 value_oppose(f
.x
.n
, f
.x
.n
);
2630 value_init(factor
->d
);
2631 evalue_copy(factor
, bfet
->factors
[k
]);
2635 void set_factor(evalue
*f
, int change
) {
2641 virtual void insert_term(bfc_term_base
*t
, int i
) {
2642 bfe_term
* bfet
= static_cast<bfe_term
*>(t
);
2643 int len
= t
->terms
.NumRows()-1; // already increased by one
2645 bfet
->factors
.resize(len
+1);
2646 for (int j
= len
; j
> i
; --j
) {
2647 bfet
->factors
[j
] = bfet
->factors
[j
-1];
2648 t
->terms
[j
] = t
->terms
[j
-1];
2650 bfet
->factors
[i
] = factor
;
2654 virtual void update_term(bfc_term_base
*t
, int i
) {
2655 bfe_term
* bfet
= static_cast<bfe_term
*>(t
);
2657 eadd(factor
, bfet
->factors
[i
]);
2658 free_evalue_refs(factor
);
2662 virtual bool constant_vertex(int dim
) { return E_num(0, dim
) == 0; }
2664 virtual void cum(bf_reducer
*bfr
, bfc_term_base
*t
, int k
, dpoly_r
*r
);
2667 struct bfe_cum
: public cumulator
{
2669 bfc_term_base
*told
;
2673 bfe_cum(evalue
*factor
, evalue
*v
, dpoly_r
*r
, bf_reducer
*bfr
,
2674 bfc_term_base
*t
, int k
, bfenumerator
*e
) :
2675 cumulator(factor
, v
, r
), told(t
), k(k
),
2679 virtual void add_term(int *powers
, int len
, evalue
*f2
);
2682 void bfe_cum::add_term(int *powers
, int len
, evalue
*f2
)
2684 bfr
->update_powers(powers
, len
);
2686 bfc_term_base
* t
= bfe
->find_bfc_term(bfr
->vn
, bfr
->npowers
, bfr
->nnf
);
2687 bfe
->set_factor(f2
, bfr
->l_changes
% 2);
2688 bfe
->add_term(t
, told
->terms
[k
], bfr
->l_extra_num
);
2691 void bfenumerator::cum(bf_reducer
*bfr
, bfc_term_base
*t
, int k
,
2694 bfe_term
* bfet
= static_cast<bfe_term
*>(t
);
2695 bfe_cum
cum(bfet
->factors
[k
], E_num(0, bfr
->d
), r
, bfr
, t
, k
, this);
2699 void bfenumerator::base(mat_ZZ
& factors
, bfc_vec
& v
)
2701 for (int i
= 0; i
< v
.size(); ++i
) {
2702 assert(v
[i
]->terms
.NumRows() == 1);
2703 evalue
*factor
= static_cast<bfe_term
*>(v
[i
])->factors
[0];
2704 eadd(factor
, vE
[vert
]);
2709 void bfenumerator::handle_polar(Polyhedron
*C
, int s
)
2711 assert(C
->NbRays
-1 == enumerator_base::dim
);
2713 bfe_term
* t
= new bfe_term(enumerator_base::dim
);
2714 vector
< bfc_term_base
* > v
;
2717 t
->factors
.resize(1);
2719 t
->terms
.SetDims(1, enumerator_base::dim
);
2720 lattice_point(V
, C
, t
->terms
[0], E_vertex
);
2722 // the elements of factors are always lexpositive
2724 s
= setup_factors(C
, factors
, t
, s
);
2726 t
->factors
[0] = new evalue
;
2727 value_init(t
->factors
[0]->d
);
2728 evalue_set_si(t
->factors
[0], s
, 1);
2731 for (int i
= 0; i
< enumerator_base::dim
; ++i
)
2733 free_evalue_refs(E_vertex
[i
]);
2738 #ifdef HAVE_CORRECT_VERTICES
2739 static inline Param_Polyhedron
*Polyhedron2Param_SD(Polyhedron
**Din
,
2740 Polyhedron
*Cin
,int WS
,Polyhedron
**CEq
,Matrix
**CT
)
2742 if (WS
& POL_NO_DUAL
)
2744 return Polyhedron2Param_SimplifiedDomain(Din
, Cin
, WS
, CEq
, CT
);
2747 static Param_Polyhedron
*Polyhedron2Param_SD(Polyhedron
**Din
,
2748 Polyhedron
*Cin
,int WS
,Polyhedron
**CEq
,Matrix
**CT
)
2750 static char data
[] = " 1 0 0 0 0 1 -18 "
2751 " 1 0 0 -20 0 19 1 "
2752 " 1 0 1 20 0 -20 16 "
2755 " 1 4 -20 0 0 -1 23 "
2756 " 1 -4 20 0 0 1 -22 "
2757 " 1 0 1 0 20 -20 16 "
2758 " 1 0 0 0 -20 19 1 ";
2759 static int checked
= 0;
2764 Matrix
*M
= Matrix_Alloc(9, 7);
2765 for (i
= 0; i
< 9; ++i
)
2766 for (int j
= 0; j
< 7; ++j
) {
2767 sscanf(p
, "%d%n", &v
, &n
);
2769 value_set_si(M
->p
[i
][j
], v
);
2771 Polyhedron
*P
= Constraints2Polyhedron(M
, 1024);
2773 Polyhedron
*U
= Universe_Polyhedron(1);
2774 Param_Polyhedron
*PP
= Polyhedron2Param_Domain(P
, U
, 1024);
2778 for (i
= 0, V
= PP
->V
; V
; ++i
, V
= V
->next
)
2781 Param_Polyhedron_Free(PP
);
2783 fprintf(stderr
, "WARNING: results may be incorrect\n");
2785 "WARNING: use latest version of PolyLib to remove this warning\n");
2789 return Polyhedron2Param_SimplifiedDomain(Din
, Cin
, WS
, CEq
, CT
);
2793 static evalue
* barvinok_enumerate_ev_f(Polyhedron
*P
, Polyhedron
* C
,
2797 static evalue
* barvinok_enumerate_cst(Polyhedron
*P
, Polyhedron
* C
,
2802 ALLOC(evalue
, eres
);
2803 value_init(eres
->d
);
2804 value_set_si(eres
->d
, 0);
2805 eres
->x
.p
= new_enode(partition
, 2, C
->Dimension
);
2806 EVALUE_SET_DOMAIN(eres
->x
.p
->arr
[0], DomainConstraintSimplify(C
, MaxRays
));
2807 value_set_si(eres
->x
.p
->arr
[1].d
, 1);
2808 value_init(eres
->x
.p
->arr
[1].x
.n
);
2810 value_set_si(eres
->x
.p
->arr
[1].x
.n
, 0);
2812 barvinok_count(P
, &eres
->x
.p
->arr
[1].x
.n
, MaxRays
);
2817 evalue
* barvinok_enumerate_ev(Polyhedron
*P
, Polyhedron
* C
, unsigned MaxRays
)
2819 //P = unfringe(P, MaxRays);
2820 Polyhedron
*Corig
= C
;
2821 Polyhedron
*CEq
= NULL
, *rVD
, *CA
;
2823 unsigned nparam
= C
->Dimension
;
2827 value_init(factor
.d
);
2828 evalue_set_si(&factor
, 1, 1);
2830 CA
= align_context(C
, P
->Dimension
, MaxRays
);
2831 P
= DomainIntersection(P
, CA
, MaxRays
);
2832 Polyhedron_Free(CA
);
2835 POL_ENSURE_FACETS(P
);
2836 POL_ENSURE_VERTICES(P
);
2837 POL_ENSURE_FACETS(C
);
2838 POL_ENSURE_VERTICES(C
);
2840 if (C
->Dimension
== 0 || emptyQ(P
)) {
2842 eres
= barvinok_enumerate_cst(P
, CEq
? CEq
: Polyhedron_Copy(C
),
2845 emul(&factor
, eres
);
2846 reduce_evalue(eres
);
2847 free_evalue_refs(&factor
);
2854 if (Polyhedron_is_infinite(P
, nparam
))
2859 P
= remove_equalities_p(P
, P
->Dimension
-nparam
, &f
);
2863 if (P
->Dimension
== nparam
) {
2865 P
= Universe_Polyhedron(0);
2869 Polyhedron
*T
= Polyhedron_Factor(P
, nparam
, MaxRays
);
2870 if (T
|| (P
->Dimension
== nparam
+1)) {
2873 for (Q
= T
? T
: P
; Q
; Q
= Q
->next
) {
2874 Polyhedron
*next
= Q
->next
;
2878 if (Q
->Dimension
!= C
->Dimension
)
2879 QC
= Polyhedron_Project(Q
, nparam
);
2882 C
= DomainIntersection(C
, QC
, MaxRays
);
2884 Polyhedron_Free(C2
);
2886 Polyhedron_Free(QC
);
2894 if (T
->Dimension
== C
->Dimension
) {
2901 Polyhedron
*next
= P
->next
;
2903 eres
= barvinok_enumerate_ev_f(P
, C
, MaxRays
);
2910 for (Q
= P
->next
; Q
; Q
= Q
->next
) {
2911 Polyhedron
*next
= Q
->next
;
2914 f
= barvinok_enumerate_ev_f(Q
, C
, MaxRays
);
2916 free_evalue_refs(f
);
2926 static evalue
* barvinok_enumerate_ev_f(Polyhedron
*P
, Polyhedron
* C
,
2929 unsigned nparam
= C
->Dimension
;
2931 if (P
->Dimension
- nparam
== 1)
2932 return ParamLine_Length(P
, C
, MaxRays
);
2934 Param_Polyhedron
*PP
= NULL
;
2935 Polyhedron
*CEq
= NULL
, *pVD
;
2937 Param_Domain
*D
, *next
;
2940 Polyhedron
*Porig
= P
;
2942 PP
= Polyhedron2Param_SD(&P
,C
,MaxRays
,&CEq
,&CT
);
2944 if (isIdentity(CT
)) {
2948 assert(CT
->NbRows
!= CT
->NbColumns
);
2949 if (CT
->NbRows
== 1) { // no more parameters
2950 eres
= barvinok_enumerate_cst(P
, CEq
, MaxRays
);
2955 Param_Polyhedron_Free(PP
);
2961 nparam
= CT
->NbRows
- 1;
2964 unsigned dim
= P
->Dimension
- nparam
;
2966 ALLOC(evalue
, eres
);
2967 value_init(eres
->d
);
2968 value_set_si(eres
->d
, 0);
2971 for (nd
= 0, D
=PP
->D
; D
; ++nd
, D
=D
->next
);
2972 struct section
{ Polyhedron
*D
; evalue E
; };
2973 section
*s
= new section
[nd
];
2974 Polyhedron
**fVD
= new Polyhedron_p
[nd
];
2977 #ifdef USE_INCREMENTAL_BF
2978 bfenumerator
et(P
, dim
, PP
->nbV
);
2979 #elif defined USE_INCREMENTAL_DF
2980 ienumerator
et(P
, dim
, PP
->nbV
);
2982 enumerator
et(P
, dim
, PP
->nbV
);
2985 for(nd
= 0, D
=PP
->D
; D
; D
=next
) {
2988 Polyhedron
*rVD
= reduce_domain(D
->Domain
, CT
, CEq
,
2993 pVD
= CT
? DomainImage(rVD
,CT
,MaxRays
) : rVD
;
2995 value_init(s
[nd
].E
.d
);
2996 evalue_set_si(&s
[nd
].E
, 0, 1);
2999 FORALL_PVertex_in_ParamPolyhedron(V
,D
,PP
) // _i is internal counter
3002 et
.decompose_at(V
, _i
, MaxRays
);
3003 } catch (OrthogonalException
&e
) {
3006 for (; nd
>= 0; --nd
) {
3007 free_evalue_refs(&s
[nd
].E
);
3008 Domain_Free(s
[nd
].D
);
3009 Domain_Free(fVD
[nd
]);
3013 eadd(et
.vE
[_i
] , &s
[nd
].E
);
3014 END_FORALL_PVertex_in_ParamPolyhedron
;
3015 evalue_range_reduction_in_domain(&s
[nd
].E
, pVD
);
3018 addeliminatedparams_evalue(&s
[nd
].E
, CT
);
3025 evalue_set_si(eres
, 0, 1);
3027 eres
->x
.p
= new_enode(partition
, 2*nd
, C
->Dimension
);
3028 for (int j
= 0; j
< nd
; ++j
) {
3029 EVALUE_SET_DOMAIN(eres
->x
.p
->arr
[2*j
], s
[j
].D
);
3030 value_clear(eres
->x
.p
->arr
[2*j
+1].d
);
3031 eres
->x
.p
->arr
[2*j
+1] = s
[j
].E
;
3032 Domain_Free(fVD
[j
]);
3039 Polyhedron_Free(CEq
);
3043 Enumeration
* barvinok_enumerate(Polyhedron
*P
, Polyhedron
* C
, unsigned MaxRays
)
3045 evalue
*EP
= barvinok_enumerate_ev(P
, C
, MaxRays
);
3047 return partition2enumeration(EP
);
3050 static void SwapColumns(Value
**V
, int n
, int i
, int j
)
3052 for (int r
= 0; r
< n
; ++r
)
3053 value_swap(V
[r
][i
], V
[r
][j
]);
3056 static void SwapColumns(Polyhedron
*P
, int i
, int j
)
3058 SwapColumns(P
->Constraint
, P
->NbConstraints
, i
, j
);
3059 SwapColumns(P
->Ray
, P
->NbRays
, i
, j
);
3062 /* Construct a constraint c from constraints l and u such that if
3063 * if constraint c holds then for each value of the other variables
3064 * there is at most one value of variable pos (position pos+1 in the constraints).
3066 * Given a lower and an upper bound
3067 * n_l v_i + <c_l,x> + c_l >= 0
3068 * -n_u v_i + <c_u,x> + c_u >= 0
3069 * the constructed constraint is
3071 * -(n_l<c_u,x> + n_u<c_l,x>) + (-n_l c_u - n_u c_l + n_l n_u - 1)
3073 * which is then simplified to remove the content of the non-constant coefficients
3075 * len is the total length of the constraints.
3076 * v is a temporary variable that can be used by this procedure
3078 static void negative_test_constraint(Value
*l
, Value
*u
, Value
*c
, int pos
,
3081 value_oppose(*v
, u
[pos
+1]);
3082 Vector_Combine(l
+1, u
+1, c
+1, *v
, l
[pos
+1], len
-1);
3083 value_multiply(*v
, *v
, l
[pos
+1]);
3084 value_subtract(c
[len
-1], c
[len
-1], *v
);
3085 value_set_si(*v
, -1);
3086 Vector_Scale(c
+1, c
+1, *v
, len
-1);
3087 value_decrement(c
[len
-1], c
[len
-1]);
3088 ConstraintSimplify(c
, c
, len
, v
);
3091 static bool parallel_constraints(Value
*l
, Value
*u
, Value
*c
, int pos
,
3100 Vector_Gcd(&l
[1+pos
], len
, &g1
);
3101 Vector_Gcd(&u
[1+pos
], len
, &g2
);
3102 Vector_Combine(l
+1+pos
, u
+1+pos
, c
+1, g2
, g1
, len
);
3103 parallel
= First_Non_Zero(c
+1, len
) == -1;
3111 static void negative_test_constraint7(Value
*l
, Value
*u
, Value
*c
, int pos
,
3112 int exist
, int len
, Value
*v
)
3117 Vector_Gcd(&u
[1+pos
], exist
, v
);
3118 Vector_Gcd(&l
[1+pos
], exist
, &g
);
3119 Vector_Combine(l
+1, u
+1, c
+1, *v
, g
, len
-1);
3120 value_multiply(*v
, *v
, g
);
3121 value_subtract(c
[len
-1], c
[len
-1], *v
);
3122 value_set_si(*v
, -1);
3123 Vector_Scale(c
+1, c
+1, *v
, len
-1);
3124 value_decrement(c
[len
-1], c
[len
-1]);
3125 ConstraintSimplify(c
, c
, len
, v
);
3130 /* Turns a x + b >= 0 into a x + b <= -1
3132 * len is the total length of the constraint.
3133 * v is a temporary variable that can be used by this procedure
3135 static void oppose_constraint(Value
*c
, int len
, Value
*v
)
3137 value_set_si(*v
, -1);
3138 Vector_Scale(c
+1, c
+1, *v
, len
-1);
3139 value_decrement(c
[len
-1], c
[len
-1]);
3142 /* Split polyhedron P into two polyhedra *pos and *neg, where
3143 * existential variable i has at most one solution for each
3144 * value of the other variables in *neg.
3146 * The splitting is performed using constraints l and u.
3148 * nvar: number of set variables
3149 * row: temporary vector that can be used by this procedure
3150 * f: temporary value that can be used by this procedure
3152 static bool SplitOnConstraint(Polyhedron
*P
, int i
, int l
, int u
,
3153 int nvar
, int MaxRays
, Vector
*row
, Value
& f
,
3154 Polyhedron
**pos
, Polyhedron
**neg
)
3156 negative_test_constraint(P
->Constraint
[l
], P
->Constraint
[u
],
3157 row
->p
, nvar
+i
, P
->Dimension
+2, &f
);
3158 *neg
= AddConstraints(row
->p
, 1, P
, MaxRays
);
3160 /* We found an independent, but useless constraint
3161 * Maybe we should detect this earlier and not
3162 * mark the variable as INDEPENDENT
3164 if (emptyQ((*neg
))) {
3165 Polyhedron_Free(*neg
);
3169 oppose_constraint(row
->p
, P
->Dimension
+2, &f
);
3170 *pos
= AddConstraints(row
->p
, 1, P
, MaxRays
);
3172 if (emptyQ((*pos
))) {
3173 Polyhedron_Free(*neg
);
3174 Polyhedron_Free(*pos
);
3182 * unimodularly transform P such that constraint r is transformed
3183 * into a constraint that involves only a single (the first)
3184 * existential variable
3187 static Polyhedron
*rotate_along(Polyhedron
*P
, int r
, int nvar
, int exist
,
3193 Vector
*row
= Vector_Alloc(exist
);
3194 Vector_Copy(P
->Constraint
[r
]+1+nvar
, row
->p
, exist
);
3195 Vector_Gcd(row
->p
, exist
, &g
);
3196 if (value_notone_p(g
))
3197 Vector_AntiScale(row
->p
, row
->p
, g
, exist
);
3200 Matrix
*M
= unimodular_complete(row
);
3201 Matrix
*M2
= Matrix_Alloc(P
->Dimension
+1, P
->Dimension
+1);
3202 for (r
= 0; r
< nvar
; ++r
)
3203 value_set_si(M2
->p
[r
][r
], 1);
3204 for ( ; r
< nvar
+exist
; ++r
)
3205 Vector_Copy(M
->p
[r
-nvar
], M2
->p
[r
]+nvar
, exist
);
3206 for ( ; r
< P
->Dimension
+1; ++r
)
3207 value_set_si(M2
->p
[r
][r
], 1);
3208 Polyhedron
*T
= Polyhedron_Image(P
, M2
, MaxRays
);
3217 /* Split polyhedron P into two polyhedra *pos and *neg, where
3218 * existential variable i has at most one solution for each
3219 * value of the other variables in *neg.
3221 * If independent is set, then the two constraints on which the
3222 * split will be performed need to be independent of the other
3223 * existential variables.
3225 * Return true if an appropriate split could be performed.
3227 * nvar: number of set variables
3228 * exist: number of existential variables
3229 * row: temporary vector that can be used by this procedure
3230 * f: temporary value that can be used by this procedure
3232 static bool SplitOnVar(Polyhedron
*P
, int i
,
3233 int nvar
, int exist
, int MaxRays
,
3234 Vector
*row
, Value
& f
, bool independent
,
3235 Polyhedron
**pos
, Polyhedron
**neg
)
3239 for (int l
= P
->NbEq
; l
< P
->NbConstraints
; ++l
) {
3240 if (value_negz_p(P
->Constraint
[l
][nvar
+i
+1]))
3244 for (j
= 0; j
< exist
; ++j
)
3245 if (j
!= i
&& value_notzero_p(P
->Constraint
[l
][nvar
+j
+1]))
3251 for (int u
= P
->NbEq
; u
< P
->NbConstraints
; ++u
) {
3252 if (value_posz_p(P
->Constraint
[u
][nvar
+i
+1]))
3256 for (j
= 0; j
< exist
; ++j
)
3257 if (j
!= i
&& value_notzero_p(P
->Constraint
[u
][nvar
+j
+1]))
3263 if (SplitOnConstraint(P
, i
, l
, u
, nvar
, MaxRays
, row
, f
, pos
, neg
)) {
3266 SwapColumns(*neg
, nvar
+1, nvar
+1+i
);
3276 static bool double_bound_pair(Polyhedron
*P
, int nvar
, int exist
,
3277 int i
, int l1
, int l2
,
3278 Polyhedron
**pos
, Polyhedron
**neg
)
3282 Vector
*row
= Vector_Alloc(P
->Dimension
+2);
3283 value_set_si(row
->p
[0], 1);
3284 value_oppose(f
, P
->Constraint
[l1
][nvar
+i
+1]);
3285 Vector_Combine(P
->Constraint
[l1
]+1, P
->Constraint
[l2
]+1,
3287 P
->Constraint
[l2
][nvar
+i
+1], f
,
3289 ConstraintSimplify(row
->p
, row
->p
, P
->Dimension
+2, &f
);
3290 *pos
= AddConstraints(row
->p
, 1, P
, 0);
3291 value_set_si(f
, -1);
3292 Vector_Scale(row
->p
+1, row
->p
+1, f
, P
->Dimension
+1);
3293 value_decrement(row
->p
[P
->Dimension
+1], row
->p
[P
->Dimension
+1]);
3294 *neg
= AddConstraints(row
->p
, 1, P
, 0);
3298 return !emptyQ((*pos
)) && !emptyQ((*neg
));
3301 static bool double_bound(Polyhedron
*P
, int nvar
, int exist
,
3302 Polyhedron
**pos
, Polyhedron
**neg
)
3304 for (int i
= 0; i
< exist
; ++i
) {
3306 for (l1
= P
->NbEq
; l1
< P
->NbConstraints
; ++l1
) {
3307 if (value_negz_p(P
->Constraint
[l1
][nvar
+i
+1]))
3309 for (l2
= l1
+ 1; l2
< P
->NbConstraints
; ++l2
) {
3310 if (value_negz_p(P
->Constraint
[l2
][nvar
+i
+1]))
3312 if (double_bound_pair(P
, nvar
, exist
, i
, l1
, l2
, pos
, neg
))
3316 for (l1
= P
->NbEq
; l1
< P
->NbConstraints
; ++l1
) {
3317 if (value_posz_p(P
->Constraint
[l1
][nvar
+i
+1]))
3319 if (l1
< P
->NbConstraints
)
3320 for (l2
= l1
+ 1; l2
< P
->NbConstraints
; ++l2
) {
3321 if (value_posz_p(P
->Constraint
[l2
][nvar
+i
+1]))
3323 if (double_bound_pair(P
, nvar
, exist
, i
, l1
, l2
, pos
, neg
))
3335 INDEPENDENT
= 1 << 2,
3339 static evalue
* enumerate_or(Polyhedron
*D
,
3340 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
3343 fprintf(stderr
, "\nER: Or\n");
3344 #endif /* DEBUG_ER */
3346 Polyhedron
*N
= D
->next
;
3349 barvinok_enumerate_e(D
, exist
, nparam
, MaxRays
);
3352 for (D
= N
; D
; D
= N
) {
3357 barvinok_enumerate_e(D
, exist
, nparam
, MaxRays
);
3360 free_evalue_refs(EN
);
3370 static evalue
* enumerate_sum(Polyhedron
*P
,
3371 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
3373 int nvar
= P
->Dimension
- exist
- nparam
;
3374 int toswap
= nvar
< exist
? nvar
: exist
;
3375 for (int i
= 0; i
< toswap
; ++i
)
3376 SwapColumns(P
, 1 + i
, nvar
+exist
- i
);
3380 fprintf(stderr
, "\nER: Sum\n");
3381 #endif /* DEBUG_ER */
3383 evalue
*EP
= barvinok_enumerate_e(P
, exist
, nparam
, MaxRays
);
3385 for (int i
= 0; i
< /* nvar */ nparam
; ++i
) {
3386 Matrix
*C
= Matrix_Alloc(1, 1 + nparam
+ 1);
3387 value_set_si(C
->p
[0][0], 1);
3389 value_init(split
.d
);
3390 value_set_si(split
.d
, 0);
3391 split
.x
.p
= new_enode(partition
, 4, nparam
);
3392 value_set_si(C
->p
[0][1+i
], 1);
3393 Matrix
*C2
= Matrix_Copy(C
);
3394 EVALUE_SET_DOMAIN(split
.x
.p
->arr
[0],
3395 Constraints2Polyhedron(C2
, MaxRays
));
3397 evalue_set_si(&split
.x
.p
->arr
[1], 1, 1);
3398 value_set_si(C
->p
[0][1+i
], -1);
3399 value_set_si(C
->p
[0][1+nparam
], -1);
3400 EVALUE_SET_DOMAIN(split
.x
.p
->arr
[2],
3401 Constraints2Polyhedron(C
, MaxRays
));
3402 evalue_set_si(&split
.x
.p
->arr
[3], 1, 1);
3404 free_evalue_refs(&split
);
3408 evalue_range_reduction(EP
);
3410 evalue_frac2floor(EP
);
3412 evalue
*sum
= esum(EP
, nvar
);
3414 free_evalue_refs(EP
);
3418 evalue_range_reduction(EP
);
3423 static evalue
* split_sure(Polyhedron
*P
, Polyhedron
*S
,
3424 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
3426 int nvar
= P
->Dimension
- exist
- nparam
;
3428 Matrix
*M
= Matrix_Alloc(exist
, S
->Dimension
+2);
3429 for (int i
= 0; i
< exist
; ++i
)
3430 value_set_si(M
->p
[i
][nvar
+i
+1], 1);
3432 S
= DomainAddRays(S
, M
, MaxRays
);
3434 Polyhedron
*F
= DomainAddRays(P
, M
, MaxRays
);
3435 Polyhedron
*D
= DomainDifference(F
, S
, MaxRays
);
3437 D
= Disjoint_Domain(D
, 0, MaxRays
);
3442 M
= Matrix_Alloc(P
->Dimension
+1-exist
, P
->Dimension
+1);
3443 for (int j
= 0; j
< nvar
; ++j
)
3444 value_set_si(M
->p
[j
][j
], 1);
3445 for (int j
= 0; j
< nparam
+1; ++j
)
3446 value_set_si(M
->p
[nvar
+j
][nvar
+exist
+j
], 1);
3447 Polyhedron
*T
= Polyhedron_Image(S
, M
, MaxRays
);
3448 evalue
*EP
= barvinok_enumerate_e(T
, 0, nparam
, MaxRays
);
3453 for (Polyhedron
*Q
= D
; Q
; Q
= Q
->next
) {
3454 Polyhedron
*N
= Q
->next
;
3456 T
= DomainIntersection(P
, Q
, MaxRays
);
3457 evalue
*E
= barvinok_enumerate_e(T
, exist
, nparam
, MaxRays
);
3459 free_evalue_refs(E
);
3468 static evalue
* enumerate_sure(Polyhedron
*P
,
3469 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
3473 int nvar
= P
->Dimension
- exist
- nparam
;
3479 for (i
= 0; i
< exist
; ++i
) {
3480 Matrix
*M
= Matrix_Alloc(S
->NbConstraints
, S
->Dimension
+2);
3482 value_set_si(lcm
, 1);
3483 for (int j
= 0; j
< S
->NbConstraints
; ++j
) {
3484 if (value_negz_p(S
->Constraint
[j
][1+nvar
+i
]))
3486 if (value_one_p(S
->Constraint
[j
][1+nvar
+i
]))
3488 value_lcm(lcm
, S
->Constraint
[j
][1+nvar
+i
], &lcm
);
3491 for (int j
= 0; j
< S
->NbConstraints
; ++j
) {
3492 if (value_negz_p(S
->Constraint
[j
][1+nvar
+i
]))
3494 if (value_one_p(S
->Constraint
[j
][1+nvar
+i
]))
3496 value_division(f
, lcm
, S
->Constraint
[j
][1+nvar
+i
]);
3497 Vector_Scale(S
->Constraint
[j
], M
->p
[c
], f
, S
->Dimension
+2);
3498 value_subtract(M
->p
[c
][S
->Dimension
+1],
3499 M
->p
[c
][S
->Dimension
+1],
3501 value_increment(M
->p
[c
][S
->Dimension
+1],
3502 M
->p
[c
][S
->Dimension
+1]);
3506 S
= AddConstraints(M
->p
[0], c
, S
, MaxRays
);
3521 fprintf(stderr
, "\nER: Sure\n");
3522 #endif /* DEBUG_ER */
3524 return split_sure(P
, S
, exist
, nparam
, MaxRays
);
3527 static evalue
* enumerate_sure2(Polyhedron
*P
,
3528 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
3530 int nvar
= P
->Dimension
- exist
- nparam
;
3532 for (r
= 0; r
< P
->NbRays
; ++r
)
3533 if (value_one_p(P
->Ray
[r
][0]) &&
3534 value_one_p(P
->Ray
[r
][P
->Dimension
+1]))
3540 Matrix
*M
= Matrix_Alloc(nvar
+ 1 + nparam
, P
->Dimension
+2);
3541 for (int i
= 0; i
< nvar
; ++i
)
3542 value_set_si(M
->p
[i
][1+i
], 1);
3543 for (int i
= 0; i
< nparam
; ++i
)
3544 value_set_si(M
->p
[i
+nvar
][1+nvar
+exist
+i
], 1);
3545 Vector_Copy(P
->Ray
[r
]+1+nvar
, M
->p
[nvar
+nparam
]+1+nvar
, exist
);
3546 value_set_si(M
->p
[nvar
+nparam
][0], 1);
3547 value_set_si(M
->p
[nvar
+nparam
][P
->Dimension
+1], 1);
3548 Polyhedron
* F
= Rays2Polyhedron(M
, MaxRays
);
3551 Polyhedron
*I
= DomainIntersection(F
, P
, MaxRays
);
3555 fprintf(stderr
, "\nER: Sure2\n");
3556 #endif /* DEBUG_ER */
3558 return split_sure(P
, I
, exist
, nparam
, MaxRays
);
3561 static evalue
* enumerate_cyclic(Polyhedron
*P
,
3562 unsigned exist
, unsigned nparam
,
3563 evalue
* EP
, int r
, int p
, unsigned MaxRays
)
3565 int nvar
= P
->Dimension
- exist
- nparam
;
3567 /* If EP in its fractional maps only contains references
3568 * to the remainder parameter with appropriate coefficients
3569 * then we could in principle avoid adding existentially
3570 * quantified variables to the validity domains.
3571 * We'd have to replace the remainder by m { p/m }
3572 * and multiply with an appropriate factor that is one
3573 * only in the appropriate range.
3574 * This last multiplication can be avoided if EP
3575 * has a single validity domain with no (further)
3576 * constraints on the remainder parameter
3579 Matrix
*CT
= Matrix_Alloc(nparam
+1, nparam
+3);
3580 Matrix
*M
= Matrix_Alloc(1, 1+nparam
+3);
3581 for (int j
= 0; j
< nparam
; ++j
)
3583 value_set_si(CT
->p
[j
][j
], 1);
3584 value_set_si(CT
->p
[p
][nparam
+1], 1);
3585 value_set_si(CT
->p
[nparam
][nparam
+2], 1);
3586 value_set_si(M
->p
[0][1+p
], -1);
3587 value_absolute(M
->p
[0][1+nparam
], P
->Ray
[0][1+nvar
+exist
+p
]);
3588 value_set_si(M
->p
[0][1+nparam
+1], 1);
3589 Polyhedron
*CEq
= Constraints2Polyhedron(M
, 1);
3591 addeliminatedparams_enum(EP
, CT
, CEq
, MaxRays
, nparam
);
3592 Polyhedron_Free(CEq
);
3598 static void enumerate_vd_add_ray(evalue
*EP
, Matrix
*Rays
, unsigned MaxRays
)
3600 if (value_notzero_p(EP
->d
))
3603 assert(EP
->x
.p
->type
== partition
);
3604 assert(EP
->x
.p
->pos
== EVALUE_DOMAIN(EP
->x
.p
->arr
[0])->Dimension
);
3605 for (int i
= 0; i
< EP
->x
.p
->size
/2; ++i
) {
3606 Polyhedron
*D
= EVALUE_DOMAIN(EP
->x
.p
->arr
[2*i
]);
3607 Polyhedron
*N
= DomainAddRays(D
, Rays
, MaxRays
);
3608 EVALUE_SET_DOMAIN(EP
->x
.p
->arr
[2*i
], N
);
3613 static evalue
* enumerate_line(Polyhedron
*P
,
3614 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
3620 fprintf(stderr
, "\nER: Line\n");
3621 #endif /* DEBUG_ER */
3623 int nvar
= P
->Dimension
- exist
- nparam
;
3625 for (i
= 0; i
< nparam
; ++i
)
3626 if (value_notzero_p(P
->Ray
[0][1+nvar
+exist
+i
]))
3629 for (j
= i
+1; j
< nparam
; ++j
)
3630 if (value_notzero_p(P
->Ray
[0][1+nvar
+exist
+i
]))
3632 assert(j
>= nparam
); // for now
3634 Matrix
*M
= Matrix_Alloc(2, P
->Dimension
+2);
3635 value_set_si(M
->p
[0][0], 1);
3636 value_set_si(M
->p
[0][1+nvar
+exist
+i
], 1);
3637 value_set_si(M
->p
[1][0], 1);
3638 value_set_si(M
->p
[1][1+nvar
+exist
+i
], -1);
3639 value_absolute(M
->p
[1][1+P
->Dimension
], P
->Ray
[0][1+nvar
+exist
+i
]);
3640 value_decrement(M
->p
[1][1+P
->Dimension
], M
->p
[1][1+P
->Dimension
]);
3641 Polyhedron
*S
= AddConstraints(M
->p
[0], 2, P
, MaxRays
);
3642 evalue
*EP
= barvinok_enumerate_e(S
, exist
, nparam
, MaxRays
);
3646 return enumerate_cyclic(P
, exist
, nparam
, EP
, 0, i
, MaxRays
);
3649 static int single_param_pos(Polyhedron
*P
, unsigned exist
, unsigned nparam
,
3652 int nvar
= P
->Dimension
- exist
- nparam
;
3653 if (First_Non_Zero(P
->Ray
[r
]+1, nvar
) != -1)
3655 int i
= First_Non_Zero(P
->Ray
[r
]+1+nvar
+exist
, nparam
);
3658 if (First_Non_Zero(P
->Ray
[r
]+1+nvar
+exist
+1, nparam
-i
-1) != -1)
3663 static evalue
* enumerate_remove_ray(Polyhedron
*P
, int r
,
3664 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
3667 fprintf(stderr
, "\nER: RedundantRay\n");
3668 #endif /* DEBUG_ER */
3672 value_set_si(one
, 1);
3673 int len
= P
->NbRays
-1;
3674 Matrix
*M
= Matrix_Alloc(2 * len
, P
->Dimension
+2);
3675 Vector_Copy(P
->Ray
[0], M
->p
[0], r
* (P
->Dimension
+2));
3676 Vector_Copy(P
->Ray
[r
+1], M
->p
[r
], (len
-r
) * (P
->Dimension
+2));
3677 for (int j
= 0; j
< P
->NbRays
; ++j
) {
3680 Vector_Combine(P
->Ray
[j
], P
->Ray
[r
], M
->p
[len
+j
-(j
>r
)],
3681 one
, P
->Ray
[j
][P
->Dimension
+1], P
->Dimension
+2);
3684 P
= Rays2Polyhedron(M
, MaxRays
);
3686 evalue
*EP
= barvinok_enumerate_e(P
, exist
, nparam
, MaxRays
);
3693 static evalue
* enumerate_redundant_ray(Polyhedron
*P
,
3694 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
3696 assert(P
->NbBid
== 0);
3697 int nvar
= P
->Dimension
- exist
- nparam
;
3701 for (int r
= 0; r
< P
->NbRays
; ++r
) {
3702 if (value_notzero_p(P
->Ray
[r
][P
->Dimension
+1]))
3704 int i1
= single_param_pos(P
, exist
, nparam
, r
);
3707 for (int r2
= r
+1; r2
< P
->NbRays
; ++r2
) {
3708 if (value_notzero_p(P
->Ray
[r2
][P
->Dimension
+1]))
3710 int i2
= single_param_pos(P
, exist
, nparam
, r2
);
3716 value_division(m
, P
->Ray
[r
][1+nvar
+exist
+i1
],
3717 P
->Ray
[r2
][1+nvar
+exist
+i1
]);
3718 value_multiply(m
, m
, P
->Ray
[r2
][1+nvar
+exist
+i1
]);
3719 /* r2 divides r => r redundant */
3720 if (value_eq(m
, P
->Ray
[r
][1+nvar
+exist
+i1
])) {
3722 return enumerate_remove_ray(P
, r
, exist
, nparam
, MaxRays
);
3725 value_division(m
, P
->Ray
[r2
][1+nvar
+exist
+i1
],
3726 P
->Ray
[r
][1+nvar
+exist
+i1
]);
3727 value_multiply(m
, m
, P
->Ray
[r
][1+nvar
+exist
+i1
]);
3728 /* r divides r2 => r2 redundant */
3729 if (value_eq(m
, P
->Ray
[r2
][1+nvar
+exist
+i1
])) {
3731 return enumerate_remove_ray(P
, r2
, exist
, nparam
, MaxRays
);
3739 static Polyhedron
*upper_bound(Polyhedron
*P
,
3740 int pos
, Value
*max
, Polyhedron
**R
)
3749 for (Polyhedron
*Q
= P
; Q
; Q
= N
) {
3751 for (r
= 0; r
< P
->NbRays
; ++r
) {
3752 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1]) &&
3753 value_pos_p(P
->Ray
[r
][1+pos
]))
3756 if (r
< P
->NbRays
) {
3764 for (r
= 0; r
< P
->NbRays
; ++r
) {
3765 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1]))
3767 mpz_fdiv_q(v
, P
->Ray
[r
][1+pos
], P
->Ray
[r
][1+P
->Dimension
]);
3768 if ((!Q
->next
&& r
== 0) || value_gt(v
, *max
))
3769 value_assign(*max
, v
);
3776 static evalue
* enumerate_ray(Polyhedron
*P
,
3777 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
3779 assert(P
->NbBid
== 0);
3780 int nvar
= P
->Dimension
- exist
- nparam
;
3783 for (r
= 0; r
< P
->NbRays
; ++r
)
3784 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1]))
3790 for (r2
= r
+1; r2
< P
->NbRays
; ++r2
)
3791 if (value_zero_p(P
->Ray
[r2
][P
->Dimension
+1]))
3793 if (r2
< P
->NbRays
) {
3795 return enumerate_sum(P
, exist
, nparam
, MaxRays
);
3799 fprintf(stderr
, "\nER: Ray\n");
3800 #endif /* DEBUG_ER */
3806 value_set_si(one
, 1);
3807 int i
= single_param_pos(P
, exist
, nparam
, r
);
3808 assert(i
!= -1); // for now;
3810 Matrix
*M
= Matrix_Alloc(P
->NbRays
, P
->Dimension
+2);
3811 for (int j
= 0; j
< P
->NbRays
; ++j
) {
3812 Vector_Combine(P
->Ray
[j
], P
->Ray
[r
], M
->p
[j
],
3813 one
, P
->Ray
[j
][P
->Dimension
+1], P
->Dimension
+2);
3815 Polyhedron
*S
= Rays2Polyhedron(M
, MaxRays
);
3817 Polyhedron
*D
= DomainDifference(P
, S
, MaxRays
);
3819 // Polyhedron_Print(stderr, P_VALUE_FMT, D);
3820 assert(value_pos_p(P
->Ray
[r
][1+nvar
+exist
+i
])); // for now
3822 D
= upper_bound(D
, nvar
+exist
+i
, &m
, &R
);
3826 M
= Matrix_Alloc(2, P
->Dimension
+2);
3827 value_set_si(M
->p
[0][0], 1);
3828 value_set_si(M
->p
[1][0], 1);
3829 value_set_si(M
->p
[0][1+nvar
+exist
+i
], -1);
3830 value_set_si(M
->p
[1][1+nvar
+exist
+i
], 1);
3831 value_assign(M
->p
[0][1+P
->Dimension
], m
);
3832 value_oppose(M
->p
[1][1+P
->Dimension
], m
);
3833 value_addto(M
->p
[1][1+P
->Dimension
], M
->p
[1][1+P
->Dimension
],
3834 P
->Ray
[r
][1+nvar
+exist
+i
]);
3835 value_decrement(M
->p
[1][1+P
->Dimension
], M
->p
[1][1+P
->Dimension
]);
3836 // Matrix_Print(stderr, P_VALUE_FMT, M);
3837 D
= AddConstraints(M
->p
[0], 2, P
, MaxRays
);
3838 // Polyhedron_Print(stderr, P_VALUE_FMT, D);
3839 value_subtract(M
->p
[0][1+P
->Dimension
], M
->p
[0][1+P
->Dimension
],
3840 P
->Ray
[r
][1+nvar
+exist
+i
]);
3841 // Matrix_Print(stderr, P_VALUE_FMT, M);
3842 S
= AddConstraints(M
->p
[0], 1, P
, MaxRays
);
3843 // Polyhedron_Print(stderr, P_VALUE_FMT, S);
3846 evalue
*EP
= barvinok_enumerate_e(D
, exist
, nparam
, MaxRays
);
3851 if (value_notone_p(P
->Ray
[r
][1+nvar
+exist
+i
]))
3852 EP
= enumerate_cyclic(P
, exist
, nparam
, EP
, r
, i
, MaxRays
);
3854 M
= Matrix_Alloc(1, nparam
+2);
3855 value_set_si(M
->p
[0][0], 1);
3856 value_set_si(M
->p
[0][1+i
], 1);
3857 enumerate_vd_add_ray(EP
, M
, MaxRays
);
3862 evalue
*E
= barvinok_enumerate_e(S
, exist
, nparam
, MaxRays
);
3864 free_evalue_refs(E
);
3871 evalue
*ER
= enumerate_or(R
, exist
, nparam
, MaxRays
);
3873 free_evalue_refs(ER
);
3880 static evalue
* enumerate_vd(Polyhedron
**PA
,
3881 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
3883 Polyhedron
*P
= *PA
;
3884 int nvar
= P
->Dimension
- exist
- nparam
;
3885 Param_Polyhedron
*PP
= NULL
;
3886 Polyhedron
*C
= Universe_Polyhedron(nparam
);
3890 PP
= Polyhedron2Param_SimplifiedDomain(&PR
,C
,MaxRays
,&CEq
,&CT
);
3894 Param_Domain
*D
, *last
;
3897 for (nd
= 0, D
=PP
->D
; D
; D
=D
->next
, ++nd
)
3900 Polyhedron
**VD
= new Polyhedron_p
[nd
];
3901 Polyhedron
**fVD
= new Polyhedron_p
[nd
];
3902 for(nd
= 0, D
=PP
->D
; D
; D
=D
->next
) {
3903 Polyhedron
*rVD
= reduce_domain(D
->Domain
, CT
, CEq
,
3917 /* This doesn't seem to have any effect */
3919 Polyhedron
*CA
= align_context(VD
[0], P
->Dimension
, MaxRays
);
3921 P
= DomainIntersection(P
, CA
, MaxRays
);
3924 Polyhedron_Free(CA
);
3929 if (!EP
&& CT
->NbColumns
!= CT
->NbRows
) {
3930 Polyhedron
*CEqr
= DomainImage(CEq
, CT
, MaxRays
);
3931 Polyhedron
*CA
= align_context(CEqr
, PR
->Dimension
, MaxRays
);
3932 Polyhedron
*I
= DomainIntersection(PR
, CA
, MaxRays
);
3933 Polyhedron_Free(CEqr
);
3934 Polyhedron_Free(CA
);
3936 fprintf(stderr
, "\nER: Eliminate\n");
3937 #endif /* DEBUG_ER */
3938 nparam
-= CT
->NbColumns
- CT
->NbRows
;
3939 EP
= barvinok_enumerate_e(I
, exist
, nparam
, MaxRays
);
3940 nparam
+= CT
->NbColumns
- CT
->NbRows
;
3941 addeliminatedparams_enum(EP
, CT
, CEq
, MaxRays
, nparam
);
3945 Polyhedron_Free(PR
);
3948 if (!EP
&& nd
> 1) {
3950 fprintf(stderr
, "\nER: VD\n");
3951 #endif /* DEBUG_ER */
3952 for (int i
= 0; i
< nd
; ++i
) {
3953 Polyhedron
*CA
= align_context(VD
[i
], P
->Dimension
, MaxRays
);
3954 Polyhedron
*I
= DomainIntersection(P
, CA
, MaxRays
);
3957 EP
= barvinok_enumerate_e(I
, exist
, nparam
, MaxRays
);
3959 evalue
*E
= barvinok_enumerate_e(I
, exist
, nparam
, MaxRays
);
3961 free_evalue_refs(E
);
3965 Polyhedron_Free(CA
);
3969 for (int i
= 0; i
< nd
; ++i
) {
3970 Polyhedron_Free(VD
[i
]);
3971 Polyhedron_Free(fVD
[i
]);
3977 if (!EP
&& nvar
== 0) {
3980 Param_Vertices
*V
, *V2
;
3981 Matrix
* M
= Matrix_Alloc(1, P
->Dimension
+2);
3983 FORALL_PVertex_in_ParamPolyhedron(V
, last
, PP
) {
3985 FORALL_PVertex_in_ParamPolyhedron(V2
, last
, PP
) {
3992 for (int i
= 0; i
< exist
; ++i
) {
3993 value_oppose(f
, V
->Vertex
->p
[i
][nparam
+1]);
3994 Vector_Combine(V
->Vertex
->p
[i
],
3996 M
->p
[0] + 1 + nvar
+ exist
,
3997 V2
->Vertex
->p
[i
][nparam
+1],
4001 for (j
= 0; j
< nparam
; ++j
)
4002 if (value_notzero_p(M
->p
[0][1+nvar
+exist
+j
]))
4006 ConstraintSimplify(M
->p
[0], M
->p
[0],
4007 P
->Dimension
+2, &f
);
4008 value_set_si(M
->p
[0][0], 0);
4009 Polyhedron
*para
= AddConstraints(M
->p
[0], 1, P
,
4012 Polyhedron_Free(para
);
4015 Polyhedron
*pos
, *neg
;
4016 value_set_si(M
->p
[0][0], 1);
4017 value_decrement(M
->p
[0][P
->Dimension
+1],
4018 M
->p
[0][P
->Dimension
+1]);
4019 neg
= AddConstraints(M
->p
[0], 1, P
, MaxRays
);
4020 value_set_si(f
, -1);
4021 Vector_Scale(M
->p
[0]+1, M
->p
[0]+1, f
,
4023 value_decrement(M
->p
[0][P
->Dimension
+1],
4024 M
->p
[0][P
->Dimension
+1]);
4025 value_decrement(M
->p
[0][P
->Dimension
+1],
4026 M
->p
[0][P
->Dimension
+1]);
4027 pos
= AddConstraints(M
->p
[0], 1, P
, MaxRays
);
4028 if (emptyQ(neg
) && emptyQ(pos
)) {
4029 Polyhedron_Free(para
);
4030 Polyhedron_Free(pos
);
4031 Polyhedron_Free(neg
);
4035 fprintf(stderr
, "\nER: Order\n");
4036 #endif /* DEBUG_ER */
4037 EP
= barvinok_enumerate_e(para
, exist
, nparam
, MaxRays
);
4040 E
= barvinok_enumerate_e(pos
, exist
, nparam
, MaxRays
);
4042 free_evalue_refs(E
);
4046 E
= barvinok_enumerate_e(neg
, exist
, nparam
, MaxRays
);
4048 free_evalue_refs(E
);
4051 Polyhedron_Free(para
);
4052 Polyhedron_Free(pos
);
4053 Polyhedron_Free(neg
);
4058 } END_FORALL_PVertex_in_ParamPolyhedron
;
4061 } END_FORALL_PVertex_in_ParamPolyhedron
;
4064 /* Search for vertex coordinate to split on */
4065 /* First look for one independent of the parameters */
4066 FORALL_PVertex_in_ParamPolyhedron(V
, last
, PP
) {
4067 for (int i
= 0; i
< exist
; ++i
) {
4069 for (j
= 0; j
< nparam
; ++j
)
4070 if (value_notzero_p(V
->Vertex
->p
[i
][j
]))
4074 value_set_si(M
->p
[0][0], 1);
4075 Vector_Set(M
->p
[0]+1, 0, nvar
+exist
);
4076 Vector_Copy(V
->Vertex
->p
[i
],
4077 M
->p
[0] + 1 + nvar
+ exist
, nparam
+1);
4078 value_oppose(M
->p
[0][1+nvar
+i
],
4079 V
->Vertex
->p
[i
][nparam
+1]);
4081 Polyhedron
*pos
, *neg
;
4082 value_set_si(M
->p
[0][0], 1);
4083 value_decrement(M
->p
[0][P
->Dimension
+1],
4084 M
->p
[0][P
->Dimension
+1]);
4085 neg
= AddConstraints(M
->p
[0], 1, P
, MaxRays
);
4086 value_set_si(f
, -1);
4087 Vector_Scale(M
->p
[0]+1, M
->p
[0]+1, f
,
4089 value_decrement(M
->p
[0][P
->Dimension
+1],
4090 M
->p
[0][P
->Dimension
+1]);
4091 value_decrement(M
->p
[0][P
->Dimension
+1],
4092 M
->p
[0][P
->Dimension
+1]);
4093 pos
= AddConstraints(M
->p
[0], 1, P
, MaxRays
);
4094 if (emptyQ(neg
) || emptyQ(pos
)) {
4095 Polyhedron_Free(pos
);
4096 Polyhedron_Free(neg
);
4099 Polyhedron_Free(pos
);
4100 value_increment(M
->p
[0][P
->Dimension
+1],
4101 M
->p
[0][P
->Dimension
+1]);
4102 pos
= AddConstraints(M
->p
[0], 1, P
, MaxRays
);
4104 fprintf(stderr
, "\nER: Vertex\n");
4105 #endif /* DEBUG_ER */
4107 EP
= enumerate_or(pos
, exist
, nparam
, MaxRays
);
4112 } END_FORALL_PVertex_in_ParamPolyhedron
;
4116 /* Search for vertex coordinate to split on */
4117 /* Now look for one that depends on the parameters */
4118 FORALL_PVertex_in_ParamPolyhedron(V
, last
, PP
) {
4119 for (int i
= 0; i
< exist
; ++i
) {
4120 value_set_si(M
->p
[0][0], 1);
4121 Vector_Set(M
->p
[0]+1, 0, nvar
+exist
);
4122 Vector_Copy(V
->Vertex
->p
[i
],
4123 M
->p
[0] + 1 + nvar
+ exist
, nparam
+1);
4124 value_oppose(M
->p
[0][1+nvar
+i
],
4125 V
->Vertex
->p
[i
][nparam
+1]);
4127 Polyhedron
*pos
, *neg
;
4128 value_set_si(M
->p
[0][0], 1);
4129 value_decrement(M
->p
[0][P
->Dimension
+1],
4130 M
->p
[0][P
->Dimension
+1]);
4131 neg
= AddConstraints(M
->p
[0], 1, P
, MaxRays
);
4132 value_set_si(f
, -1);
4133 Vector_Scale(M
->p
[0]+1, M
->p
[0]+1, f
,
4135 value_decrement(M
->p
[0][P
->Dimension
+1],
4136 M
->p
[0][P
->Dimension
+1]);
4137 value_decrement(M
->p
[0][P
->Dimension
+1],
4138 M
->p
[0][P
->Dimension
+1]);
4139 pos
= AddConstraints(M
->p
[0], 1, P
, MaxRays
);
4140 if (emptyQ(neg
) || emptyQ(pos
)) {
4141 Polyhedron_Free(pos
);
4142 Polyhedron_Free(neg
);
4145 Polyhedron_Free(pos
);
4146 value_increment(M
->p
[0][P
->Dimension
+1],
4147 M
->p
[0][P
->Dimension
+1]);
4148 pos
= AddConstraints(M
->p
[0], 1, P
, MaxRays
);
4150 fprintf(stderr
, "\nER: ParamVertex\n");
4151 #endif /* DEBUG_ER */
4153 EP
= enumerate_or(pos
, exist
, nparam
, MaxRays
);
4158 } END_FORALL_PVertex_in_ParamPolyhedron
;
4166 Polyhedron_Free(CEq
);
4170 Param_Polyhedron_Free(PP
);
4177 evalue
*barvinok_enumerate_pip(Polyhedron
*P
,
4178 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
4183 evalue
*barvinok_enumerate_pip(Polyhedron
*P
,
4184 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
4186 int nvar
= P
->Dimension
- exist
- nparam
;
4187 evalue
*EP
= evalue_zero();
4191 fprintf(stderr
, "\nER: PIP\n");
4192 #endif /* DEBUG_ER */
4194 Polyhedron
*D
= pip_projectout(P
, nvar
, exist
, nparam
);
4195 for (Q
= D
; Q
; Q
= N
) {
4199 exist
= Q
->Dimension
- nvar
- nparam
;
4200 E
= barvinok_enumerate_e(Q
, exist
, nparam
, MaxRays
);
4203 free_evalue_refs(E
);
4212 static bool is_single(Value
*row
, int pos
, int len
)
4214 return First_Non_Zero(row
, pos
) == -1 &&
4215 First_Non_Zero(row
+pos
+1, len
-pos
-1) == -1;
4218 static evalue
* barvinok_enumerate_e_r(Polyhedron
*P
,
4219 unsigned exist
, unsigned nparam
, unsigned MaxRays
);
4222 static int er_level
= 0;
4224 evalue
* barvinok_enumerate_e(Polyhedron
*P
,
4225 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
4227 fprintf(stderr
, "\nER: level %i\n", er_level
);
4229 Polyhedron_PrintConstraints(stderr
, P_VALUE_FMT
, P
);
4230 fprintf(stderr
, "\nE %d\nP %d\n", exist
, nparam
);
4232 P
= DomainConstraintSimplify(Polyhedron_Copy(P
), MaxRays
);
4233 evalue
*EP
= barvinok_enumerate_e_r(P
, exist
, nparam
, MaxRays
);
4239 evalue
* barvinok_enumerate_e(Polyhedron
*P
,
4240 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
4242 P
= DomainConstraintSimplify(Polyhedron_Copy(P
), MaxRays
);
4243 evalue
*EP
= barvinok_enumerate_e_r(P
, exist
, nparam
, MaxRays
);
4249 static evalue
* barvinok_enumerate_e_r(Polyhedron
*P
,
4250 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
4253 Polyhedron
*U
= Universe_Polyhedron(nparam
);
4254 evalue
*EP
= barvinok_enumerate_ev(P
, U
, MaxRays
);
4255 //char *param_name[] = {"P", "Q", "R", "S", "T" };
4256 //print_evalue(stdout, EP, param_name);
4261 int nvar
= P
->Dimension
- exist
- nparam
;
4262 int len
= P
->Dimension
+ 2;
4265 POL_ENSURE_FACETS(P
);
4266 POL_ENSURE_VERTICES(P
);
4269 return evalue_zero();
4271 if (nvar
== 0 && nparam
== 0) {
4272 evalue
*EP
= evalue_zero();
4273 barvinok_count(P
, &EP
->x
.n
, MaxRays
);
4274 if (value_pos_p(EP
->x
.n
))
4275 value_set_si(EP
->x
.n
, 1);
4280 for (r
= 0; r
< P
->NbRays
; ++r
)
4281 if (value_zero_p(P
->Ray
[r
][0]) ||
4282 value_zero_p(P
->Ray
[r
][P
->Dimension
+1])) {
4284 for (i
= 0; i
< nvar
; ++i
)
4285 if (value_notzero_p(P
->Ray
[r
][i
+1]))
4289 for (i
= nvar
+ exist
; i
< nvar
+ exist
+ nparam
; ++i
)
4290 if (value_notzero_p(P
->Ray
[r
][i
+1]))
4292 if (i
>= nvar
+ exist
+ nparam
)
4295 if (r
< P
->NbRays
) {
4296 evalue
*EP
= evalue_zero();
4297 value_set_si(EP
->x
.n
, -1);
4302 for (r
= 0; r
< P
->NbEq
; ++r
)
4303 if ((first
= First_Non_Zero(P
->Constraint
[r
]+1+nvar
, exist
)) != -1)
4306 if (First_Non_Zero(P
->Constraint
[r
]+1+nvar
+first
+1,
4307 exist
-first
-1) != -1) {
4308 Polyhedron
*T
= rotate_along(P
, r
, nvar
, exist
, MaxRays
);
4310 fprintf(stderr
, "\nER: Equality\n");
4311 #endif /* DEBUG_ER */
4312 evalue
*EP
= barvinok_enumerate_e(T
, exist
-1, nparam
, MaxRays
);
4317 fprintf(stderr
, "\nER: Fixed\n");
4318 #endif /* DEBUG_ER */
4320 return barvinok_enumerate_e(P
, exist
-1, nparam
, MaxRays
);
4322 Polyhedron
*T
= Polyhedron_Copy(P
);
4323 SwapColumns(T
, nvar
+1, nvar
+1+first
);
4324 evalue
*EP
= barvinok_enumerate_e(T
, exist
-1, nparam
, MaxRays
);
4331 Vector
*row
= Vector_Alloc(len
);
4332 value_set_si(row
->p
[0], 1);
4337 enum constraint
* info
= new constraint
[exist
];
4338 for (int i
= 0; i
< exist
; ++i
) {
4340 for (int l
= P
->NbEq
; l
< P
->NbConstraints
; ++l
) {
4341 if (value_negz_p(P
->Constraint
[l
][nvar
+i
+1]))
4343 bool l_parallel
= is_single(P
->Constraint
[l
]+nvar
+1, i
, exist
);
4344 for (int u
= P
->NbEq
; u
< P
->NbConstraints
; ++u
) {
4345 if (value_posz_p(P
->Constraint
[u
][nvar
+i
+1]))
4347 bool lu_parallel
= l_parallel
||
4348 is_single(P
->Constraint
[u
]+nvar
+1, i
, exist
);
4349 value_oppose(f
, P
->Constraint
[u
][nvar
+i
+1]);
4350 Vector_Combine(P
->Constraint
[l
]+1, P
->Constraint
[u
]+1, row
->p
+1,
4351 f
, P
->Constraint
[l
][nvar
+i
+1], len
-1);
4352 if (!(info
[i
] & INDEPENDENT
)) {
4354 for (j
= 0; j
< exist
; ++j
)
4355 if (j
!= i
&& value_notzero_p(row
->p
[nvar
+j
+1]))
4358 //printf("independent: i: %d, l: %d, u: %d\n", i, l, u);
4359 info
[i
] = (constraint
)(info
[i
] | INDEPENDENT
);
4362 if (info
[i
] & ALL_POS
) {
4363 value_addto(row
->p
[len
-1], row
->p
[len
-1],
4364 P
->Constraint
[l
][nvar
+i
+1]);
4365 value_addto(row
->p
[len
-1], row
->p
[len
-1], f
);
4366 value_multiply(f
, f
, P
->Constraint
[l
][nvar
+i
+1]);
4367 value_subtract(row
->p
[len
-1], row
->p
[len
-1], f
);
4368 value_decrement(row
->p
[len
-1], row
->p
[len
-1]);
4369 ConstraintSimplify(row
->p
, row
->p
, len
, &f
);
4370 value_set_si(f
, -1);
4371 Vector_Scale(row
->p
+1, row
->p
+1, f
, len
-1);
4372 value_decrement(row
->p
[len
-1], row
->p
[len
-1]);
4373 Polyhedron
*T
= AddConstraints(row
->p
, 1, P
, MaxRays
);
4375 //printf("not all_pos: i: %d, l: %d, u: %d\n", i, l, u);
4376 info
[i
] = (constraint
)(info
[i
] ^ ALL_POS
);
4378 //puts("pos remainder");
4379 //Polyhedron_Print(stdout, P_VALUE_FMT, T);
4382 if (!(info
[i
] & ONE_NEG
)) {
4384 negative_test_constraint(P
->Constraint
[l
],
4386 row
->p
, nvar
+i
, len
, &f
);
4387 oppose_constraint(row
->p
, len
, &f
);
4388 Polyhedron
*T
= AddConstraints(row
->p
, 1, P
, MaxRays
);
4390 //printf("one_neg i: %d, l: %d, u: %d\n", i, l, u);
4391 info
[i
] = (constraint
)(info
[i
] | ONE_NEG
);
4393 //puts("neg remainder");
4394 //Polyhedron_Print(stdout, P_VALUE_FMT, T);
4396 } else if (!(info
[i
] & ROT_NEG
)) {
4397 if (parallel_constraints(P
->Constraint
[l
],
4399 row
->p
, nvar
, exist
)) {
4400 negative_test_constraint7(P
->Constraint
[l
],
4402 row
->p
, nvar
, exist
,
4404 oppose_constraint(row
->p
, len
, &f
);
4405 Polyhedron
*T
= AddConstraints(row
->p
, 1, P
, MaxRays
);
4407 // printf("rot_neg i: %d, l: %d, u: %d\n", i, l, u);
4408 info
[i
] = (constraint
)(info
[i
] | ROT_NEG
);
4411 //puts("neg remainder");
4412 //Polyhedron_Print(stdout, P_VALUE_FMT, T);
4417 if (!(info
[i
] & ALL_POS
) && (info
[i
] & (ONE_NEG
| ROT_NEG
)))
4421 if (info
[i
] & ALL_POS
)
4428 for (int i = 0; i < exist; ++i)
4429 printf("%i: %i\n", i, info[i]);
4431 for (int i
= 0; i
< exist
; ++i
)
4432 if (info
[i
] & ALL_POS
) {
4434 fprintf(stderr
, "\nER: Positive\n");
4435 #endif /* DEBUG_ER */
4437 // Maybe we should chew off some of the fat here
4438 Matrix
*M
= Matrix_Alloc(P
->Dimension
, P
->Dimension
+1);
4439 for (int j
= 0; j
< P
->Dimension
; ++j
)
4440 value_set_si(M
->p
[j
][j
+ (j
>= i
+nvar
)], 1);
4441 Polyhedron
*T
= Polyhedron_Image(P
, M
, MaxRays
);
4443 evalue
*EP
= barvinok_enumerate_e(T
, exist
-1, nparam
, MaxRays
);
4450 for (int i
= 0; i
< exist
; ++i
)
4451 if (info
[i
] & ONE_NEG
) {
4453 fprintf(stderr
, "\nER: Negative\n");
4454 #endif /* DEBUG_ER */
4459 return barvinok_enumerate_e(P
, exist
-1, nparam
, MaxRays
);
4461 Polyhedron
*T
= Polyhedron_Copy(P
);
4462 SwapColumns(T
, nvar
+1, nvar
+1+i
);
4463 evalue
*EP
= barvinok_enumerate_e(T
, exist
-1, nparam
, MaxRays
);
4468 for (int i
= 0; i
< exist
; ++i
)
4469 if (info
[i
] & ROT_NEG
) {
4471 fprintf(stderr
, "\nER: Rotate\n");
4472 #endif /* DEBUG_ER */
4476 Polyhedron
*T
= rotate_along(P
, r
, nvar
, exist
, MaxRays
);
4477 evalue
*EP
= barvinok_enumerate_e(T
, exist
-1, nparam
, MaxRays
);
4481 for (int i
= 0; i
< exist
; ++i
)
4482 if (info
[i
] & INDEPENDENT
) {
4483 Polyhedron
*pos
, *neg
;
4485 /* Find constraint again and split off negative part */
4487 if (SplitOnVar(P
, i
, nvar
, exist
, MaxRays
,
4488 row
, f
, true, &pos
, &neg
)) {
4490 fprintf(stderr
, "\nER: Split\n");
4491 #endif /* DEBUG_ER */
4494 barvinok_enumerate_e(neg
, exist
-1, nparam
, MaxRays
);
4496 barvinok_enumerate_e(pos
, exist
, nparam
, MaxRays
);
4498 free_evalue_refs(E
);
4500 Polyhedron_Free(neg
);
4501 Polyhedron_Free(pos
);
4515 EP
= enumerate_line(P
, exist
, nparam
, MaxRays
);
4519 EP
= barvinok_enumerate_pip(P
, exist
, nparam
, MaxRays
);
4523 EP
= enumerate_redundant_ray(P
, exist
, nparam
, MaxRays
);
4527 EP
= enumerate_sure(P
, exist
, nparam
, MaxRays
);
4531 EP
= enumerate_ray(P
, exist
, nparam
, MaxRays
);
4535 EP
= enumerate_sure2(P
, exist
, nparam
, MaxRays
);
4539 F
= unfringe(P
, MaxRays
);
4540 if (!PolyhedronIncludes(F
, P
)) {
4542 fprintf(stderr
, "\nER: Fringed\n");
4543 #endif /* DEBUG_ER */
4544 EP
= barvinok_enumerate_e(F
, exist
, nparam
, MaxRays
);
4551 EP
= enumerate_vd(&P
, exist
, nparam
, MaxRays
);
4556 EP
= enumerate_sum(P
, exist
, nparam
, MaxRays
);
4563 Polyhedron
*pos
, *neg
;
4564 for (i
= 0; i
< exist
; ++i
)
4565 if (SplitOnVar(P
, i
, nvar
, exist
, MaxRays
,
4566 row
, f
, false, &pos
, &neg
))
4572 EP
= enumerate_or(pos
, exist
, nparam
, MaxRays
);
4584 static void split_param_compression(Matrix
*CP
, mat_ZZ
& map
, vec_ZZ
& offset
)
4586 Matrix
*T
= Transpose(CP
);
4587 matrix2zz(T
, map
, T
->NbRows
-1, T
->NbColumns
-1);
4588 values2zz(T
->p
[T
->NbRows
-1], offset
, T
->NbColumns
-1);
4593 * remove equalities that require a "compression" of the parameters
4595 #ifndef HAVE_COMPRESS_PARMS
4596 static Polyhedron
*remove_more_equalities(Polyhedron
*P
, unsigned nparam
,
4597 Matrix
**CP
, unsigned MaxRays
)
4602 static Polyhedron
*remove_more_equalities(Polyhedron
*P
, unsigned nparam
,
4603 Matrix
**CP
, unsigned MaxRays
)
4608 /* compress_parms doesn't like equalities that only involve parameters */
4609 for (int i
= 0; i
< P
->NbEq
; ++i
)
4610 assert(First_Non_Zero(P
->Constraint
[i
]+1, P
->Dimension
-nparam
) != -1);
4612 M
= Matrix_Alloc(P
->NbEq
, P
->Dimension
+2);
4613 Vector_Copy(P
->Constraint
[0], M
->p
[0], P
->NbEq
* (P
->Dimension
+2));
4614 *CP
= compress_parms(M
, nparam
);
4615 T
= align_matrix(*CP
, P
->Dimension
+1);
4616 Q
= Polyhedron_Preimage(P
, T
, MaxRays
);
4619 P
= remove_equalities_p(P
, P
->Dimension
-nparam
, NULL
);
4626 gen_fun
* barvinok_series(Polyhedron
*P
, Polyhedron
* C
, unsigned MaxRays
)
4630 unsigned nparam
= C
->Dimension
;
4632 CA
= align_context(C
, P
->Dimension
, MaxRays
);
4633 P
= DomainIntersection(P
, CA
, MaxRays
);
4634 Polyhedron_Free(CA
);
4641 assert(!Polyhedron_is_infinite(P
, nparam
));
4642 assert(P
->NbBid
== 0);
4643 assert(Polyhedron_has_positive_rays(P
, nparam
));
4645 P
= remove_equalities_p(P
, P
->Dimension
-nparam
, NULL
);
4647 P
= remove_more_equalities(P
, nparam
, &CP
, MaxRays
);
4648 assert(P
->NbEq
== 0);
4650 #ifdef USE_INCREMENTAL_BF
4651 partial_bfcounter
red(P
, nparam
);
4652 #elif defined USE_INCREMENTAL_DF
4653 partial_ireducer
red(P
, nparam
);
4655 partial_reducer
red(P
, nparam
);
4662 split_param_compression(CP
, map
, offset
);
4663 red
.gf
->substitute(CP
, map
, offset
);
4669 static Polyhedron
*skew_into_positive_orthant(Polyhedron
*D
, unsigned nparam
,
4675 for (Polyhedron
*P
= D
; P
; P
= P
->next
) {
4676 POL_ENSURE_VERTICES(P
);
4677 assert(!Polyhedron_is_infinite(P
, nparam
));
4678 assert(P
->NbBid
== 0);
4679 assert(Polyhedron_has_positive_rays(P
, nparam
));
4681 for (int r
= 0; r
< P
->NbRays
; ++r
) {
4682 if (value_notzero_p(P
->Ray
[r
][P
->Dimension
+1]))
4684 for (int i
= 0; i
< nparam
; ++i
) {
4686 if (value_posz_p(P
->Ray
[r
][i
+1]))
4689 M
= Matrix_Alloc(D
->Dimension
+1, D
->Dimension
+1);
4690 for (int i
= 0; i
< D
->Dimension
+1; ++i
)
4691 value_set_si(M
->p
[i
][i
], 1);
4693 Inner_Product(P
->Ray
[r
]+1, M
->p
[i
], D
->Dimension
+1, &tmp
);
4694 if (value_posz_p(tmp
))
4697 for (j
= P
->Dimension
- nparam
; j
< P
->Dimension
; ++j
)
4698 if (value_pos_p(P
->Ray
[r
][j
+1]))
4700 assert(j
< P
->Dimension
);
4701 value_pdivision(tmp
, P
->Ray
[r
][j
+1], P
->Ray
[r
][i
+1]);
4702 value_subtract(M
->p
[i
][j
], M
->p
[i
][j
], tmp
);
4708 D
= DomainImage(D
, M
, MaxRays
);
4714 evalue
* barvinok_enumerate_union(Polyhedron
*D
, Polyhedron
* C
, unsigned MaxRays
)
4717 Polyhedron
*conv
, *D2
;
4719 unsigned nparam
= C
->Dimension
;
4723 D2
= skew_into_positive_orthant(D
, nparam
, MaxRays
);
4724 for (Polyhedron
*P
= D2
; P
; P
= P
->next
) {
4725 assert(P
->Dimension
== D2
->Dimension
);
4726 POL_ENSURE_VERTICES(P
);
4727 /* it doesn't matter which reducer we use, since we don't actually
4728 * reduce anything here
4730 partial_reducer
red(P
, P
->Dimension
);
4735 gf
->add_union(red
.gf
, MaxRays
);
4739 /* we actually only need the convex union of the parameter space
4740 * but the reducer classes currently expect a polyhedron in
4741 * the combined space
4743 conv
= DomainConvex(D2
, MaxRays
);
4744 #ifdef USE_INCREMENTAL_DF
4745 partial_ireducer
red(conv
, nparam
);
4747 partial_reducer
red(conv
, nparam
);
4749 for (int i
= 0; i
< gf
->term
.size(); ++i
) {
4750 for (int j
= 0; j
< gf
->term
[i
]->n
.power
.NumRows(); ++j
) {
4751 red
.reduce(gf
->term
[i
]->n
.coeff
[j
][0], gf
->term
[i
]->n
.coeff
[j
][1],
4752 gf
->term
[i
]->n
.power
[j
], gf
->term
[i
]->d
.power
);
4758 Polyhedron_Free(conv
);