2 #include <barvinok/barvinok.h>
3 #include <barvinok/util.h>
4 #include "genfun_constructor.h"
5 #include "lattice_width.h"
6 #include "remove_equalities.h"
11 /* Check whether all rays point in the positive directions
14 static bool Polyhedron_has_positive_rays(Polyhedron
*P
, unsigned nparam
)
17 for (r
= 0; r
< P
->NbRays
; ++r
)
18 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1])) {
20 for (i
= P
->Dimension
- nparam
; i
< P
->Dimension
; ++i
)
21 if (value_neg_p(P
->Ray
[r
][i
+1]))
27 static gen_fun
*enumerate_series(Polyhedron
*P
, unsigned nparam
,
28 barvinok_options
*options
)
32 Polyhedron
*P_orig
= P
;
35 return new gen_fun(Empty_Polyhedron(nparam
));
38 remove_all_equalities(&P
, NULL
, &CP
, NULL
, nparam
, options
->MaxRays
);
39 assert(emptyQ2(P
) || P
->NbEq
== 0);
41 nparam
= CP
->NbColumns
-1;
46 barvinok_count_with_options(P
, &c
, options
);
50 POL_ENSURE_VERTICES(P
);
52 gf
= enumerate_series(P
, nparam
, options
);
55 red
= gf_base::create(Polyhedron_Project(P
, nparam
),
56 P
->Dimension
, nparam
, options
);
57 red
->start_gf(P
, options
);
71 gen_fun
*barvinok_enumerate_series(Polyhedron
*P
, unsigned nparam
,
72 barvinok_options
*options
)
75 return new gen_fun(Empty_Polyhedron(nparam
));
77 assert(!Polyhedron_is_unbounded(P
, nparam
, options
->MaxRays
));
78 assert(P
->NbBid
== 0);
79 assert(Polyhedron_has_revlex_positive_rays(P
, nparam
));
80 return enumerate_series(P
, nparam
, options
);
83 gen_fun
* barvinok_series_with_options(Polyhedron
*P
, Polyhedron
* C
,
84 barvinok_options
*options
)
87 unsigned nparam
= C
->Dimension
;
90 CA
= align_context(C
, P
->Dimension
, options
->MaxRays
);
91 P
= DomainIntersection(P
, CA
, options
->MaxRays
);
94 gf
= barvinok_enumerate_series(P
, nparam
, options
);
100 gen_fun
* barvinok_series(Polyhedron
*P
, Polyhedron
* C
, unsigned MaxRays
)
103 barvinok_options
*options
= barvinok_options_new_with_defaults();
104 options
->MaxRays
= MaxRays
;
105 gf
= barvinok_series_with_options(P
, C
, options
);
106 barvinok_options_free(options
);
110 static Polyhedron
*skew_into_positive_orthant(Polyhedron
*D
, unsigned nparam
,
116 for (Polyhedron
*P
= D
; P
; P
= P
->next
) {
117 POL_ENSURE_VERTICES(P
);
118 assert(!Polyhedron_is_unbounded(P
, nparam
, MaxRays
));
119 assert(P
->NbBid
== 0);
120 assert(Polyhedron_has_positive_rays(P
, nparam
));
122 for (int r
= 0; r
< P
->NbRays
; ++r
) {
123 if (value_notzero_p(P
->Ray
[r
][P
->Dimension
+1]))
125 for (int i
= 0; i
< nparam
; ++i
) {
127 if (value_posz_p(P
->Ray
[r
][i
+1]))
130 M
= Matrix_Alloc(D
->Dimension
+1, D
->Dimension
+1);
131 for (int i
= 0; i
< D
->Dimension
+1; ++i
)
132 value_set_si(M
->p
[i
][i
], 1);
134 Inner_Product(P
->Ray
[r
]+1, M
->p
[i
], D
->Dimension
+1, &tmp
);
135 if (value_posz_p(tmp
))
138 for (j
= P
->Dimension
- nparam
; j
< P
->Dimension
; ++j
)
139 if (value_pos_p(P
->Ray
[r
][j
+1]))
141 assert(j
< P
->Dimension
);
142 value_pdivision(tmp
, P
->Ray
[r
][j
+1], P
->Ray
[r
][i
+1]);
143 value_subtract(M
->p
[i
][j
], M
->p
[i
][j
], tmp
);
149 D
= DomainImage(D
, M
, MaxRays
);
155 gen_fun
* barvinok_enumerate_union_series_with_options(Polyhedron
*D
, Polyhedron
* C
,
156 barvinok_options
*options
)
158 Polyhedron
*conv
, *D2
;
160 gen_fun
*gf
= NULL
, *gf2
;
161 unsigned nparam
= C
->Dimension
;
166 CA
= align_context(C
, D
->Dimension
, options
->MaxRays
);
167 D
= DomainIntersection(D
, CA
, options
->MaxRays
);
170 D2
= skew_into_positive_orthant(D
, nparam
, options
->MaxRays
);
171 for (Polyhedron
*P
= D2
; P
; P
= P
->next
) {
172 assert(P
->Dimension
== D2
->Dimension
);
175 P_gf
= barvinok_enumerate_series(P
, P
->Dimension
, options
);
179 gf
->add_union(P_gf
, options
);
183 /* we actually only need the convex union of the parameter space
184 * but the reducer classes currently expect a polyhedron in
187 Polyhedron_Free(gf
->context
);
188 gf
->context
= DomainConvex(D2
, options
->MaxRays
);
190 gf2
= gf
->summate(D2
->Dimension
- nparam
, options
);
199 gen_fun
* barvinok_enumerate_union_series(Polyhedron
*D
, Polyhedron
* C
,
203 barvinok_options
*options
= barvinok_options_new_with_defaults();
204 options
->MaxRays
= MaxRays
;
205 gf
= barvinok_enumerate_union_series_with_options(D
, C
, options
);
206 barvinok_options_free(options
);
210 /* Unimodularly transform the polyhedron P, such that
211 * the direction specified by dir corresponds to the last
212 * variable in the transformed polyhedron.
213 * The number of variables is given by the length of dir.
215 static Polyhedron
*put_direction_last(Polyhedron
*P
, Vector
*dir
,
222 T
= Matrix_Alloc(P
->Dimension
+1, P
->Dimension
+1);
223 T
->NbColumns
= T
->NbRows
= n
;
224 Vector_Copy(dir
->p
, T
->p
[0], n
);
225 unimodular_complete(T
, 1);
226 Vector_Exchange(T
->p
[0], T
->p
[n
-1], n
);
227 T
->NbColumns
= T
->NbRows
= P
->Dimension
+1;
228 for (int j
= n
; j
< P
->Dimension
+1; ++j
)
229 value_set_si(T
->p
[j
][j
], 1);
231 R
= Polyhedron_Image(P
, T
, MaxRays
);
237 /* Do we need to continue shifting and subtracting?
238 * i is the number of times we shifted so far
239 * n is the number of coordinates projected out
241 static bool more_shifts_needed(int j
, int n
,
242 gen_fun
*S
, gen_fun
*S_divide
, const vec_ZZ
& up
,
243 barvinok_options
*options
)
248 /* For the 2-dimensional case, we need to subtract at most once */
254 /* Assume that we have to subtract at least once */
258 hp
= S
->Hadamard_product(S_divide
, options
);
260 empty
= hp
->is_zero();
266 static gen_fun
*project(Polyhedron
*P
, unsigned n
, barvinok_options
*options
,
269 /* Return gf of P projected on last dim(P)-n coordinates, i.e.,
270 * project out the first n coordinates.
272 * Assumes P has no equalities.
274 static gen_fun
*project_full_dim(Polyhedron
*P
, unsigned n
,
275 barvinok_options
*options
)
281 struct width_direction_array
*dirs
;
285 return barvinok_enumerate_series(P
, P
->Dimension
, options
);
287 up
.SetLength(P
->Dimension
- (n
-1));
289 for (int i
= 1; i
< P
->Dimension
- (n
-1); ++i
)
293 gen_fun
*S
, *S_shift
, *hp
;
295 S
= barvinok_enumerate_series(P
, P
->Dimension
, options
);
296 S_shift
= new gen_fun(S
);
298 hp
= S
->Hadamard_product(S_shift
, options
);
299 S
->add(mone
, hp
, options
);
303 gf
= S
->summate(1, options
);
309 U
= Universe_Polyhedron(P
->Dimension
- n
);
310 dirs
= Polyhedron_Lattice_Width_Directions(P
, U
, options
);
313 for (int i
= 0; i
< dirs
->n
; ++i
) {
316 gen_fun
*S
, *S_shift
, *S_divide
, *sum
;
318 CA
= align_context(dirs
->wd
[i
].domain
, P
->Dimension
, options
->MaxRays
);
319 R
= DomainIntersection(P
, CA
, options
->MaxRays
);
321 assert(dirs
->wd
[i
].dir
->Size
== n
);
322 Pi
= put_direction_last(R
, dirs
->wd
[i
].dir
, options
->MaxRays
);
325 S
= project(Pi
, n
-1, options
, 1);
327 S_shift
= new gen_fun(S
);
328 S_divide
= new gen_fun(S
);
329 S_divide
->divide(up
);
331 for (int j
= 0; more_shifts_needed(j
, n
, S
, S_divide
, up
, options
); ++j
) {
335 hp
= S
->Hadamard_product(S_shift
, options
);
336 S
->add(mone
, hp
, options
);
341 sum
= S
->summate(1, options
);
350 gf
->add(one
, sum
, options
);
354 free_width_direction_array(dirs
);
359 /* Return gf of P projected on last dim(P)-n coordinates, i.e.,
360 * project out the first n coordinates.
362 static gen_fun
*project(Polyhedron
*P
, unsigned n
, barvinok_options
*options
,
367 unsigned nparam
= P
->Dimension
- n
;
371 remove_all_equalities(&P
, NULL
, &CP
, NULL
, nparam
, options
->MaxRays
);
373 nparam
= CP
->NbColumns
- 1;
374 n
= P
->Dimension
- nparam
;
381 proj
= new gen_fun(Empty_Polyhedron(nparam
));
383 proj
= project_full_dim(P
, n
, options
);
385 proj
->substitute(CP
);
395 gen_fun
*barvinok_enumerate_e_series(Polyhedron
*P
,
396 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
398 Polyhedron
*P_orig
= P
;
400 unsigned nvar
= P
->Dimension
- exist
- nparam
;
403 return barvinok_enumerate_series(P
, nparam
, options
);
406 return new gen_fun(Empty_Polyhedron(nparam
));
408 assert(!Polyhedron_is_unbounded(P
, nparam
, options
->MaxRays
));
409 assert(P
->NbBid
== 0);
410 assert(Polyhedron_has_revlex_positive_rays(P
, nparam
));
412 /* Move existentially quantified variables to the front.*/
415 T
= Matrix_Alloc(exist
+nvar
+nparam
+1, nvar
+exist
+nparam
+1);
416 for (int i
= 0; i
< exist
; ++i
)
417 value_set_si(T
->p
[i
][nvar
+i
], 1);
418 for (int i
= 0; i
< nvar
; ++i
)
419 value_set_si(T
->p
[exist
+i
][i
], 1);
420 for (int i
= 0; i
< nparam
+1; ++i
)
421 value_set_si(T
->p
[exist
+nvar
+i
][nvar
+exist
+i
], 1);
422 P
= Polyhedron_Image(P
, T
, options
->MaxRays
);
425 proj
= project(P
, exist
, options
, P
!= P_orig
);
430 gf
= proj
->summate(nvar
, options
);