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
*series(Polyhedron
*P
, unsigned nparam
, barvinok_options
*options
)
31 Polyhedron
*P_orig
= P
;
34 return new gen_fun(Empty_Polyhedron(nparam
));
36 assert(!Polyhedron_is_unbounded(P
, nparam
, options
->MaxRays
));
37 assert(P
->NbBid
== 0);
38 assert(Polyhedron_has_revlex_positive_rays(P
, nparam
));
40 remove_all_equalities(&P
, NULL
, &CP
, NULL
, nparam
, options
->MaxRays
);
41 assert(emptyQ2(P
) || P
->NbEq
== 0);
43 nparam
= CP
->NbColumns
-1;
48 barvinok_count_with_options(P
, &c
, options
);
52 POL_ENSURE_VERTICES(P
);
54 gf
= series(P
, nparam
, options
);
57 red
= gf_base::create(Polyhedron_Project(P
, nparam
),
58 P
->Dimension
, nparam
, options
);
59 red
->start_gf(P
, options
);
73 gen_fun
* barvinok_series_with_options(Polyhedron
*P
, Polyhedron
* C
,
74 barvinok_options
*options
)
77 unsigned nparam
= C
->Dimension
;
80 CA
= align_context(C
, P
->Dimension
, options
->MaxRays
);
81 P
= DomainIntersection(P
, CA
, options
->MaxRays
);
84 gf
= series(P
, nparam
, options
);
90 gen_fun
* barvinok_series(Polyhedron
*P
, Polyhedron
* C
, unsigned MaxRays
)
93 barvinok_options
*options
= barvinok_options_new_with_defaults();
94 options
->MaxRays
= MaxRays
;
95 gf
= barvinok_series_with_options(P
, C
, options
);
96 barvinok_options_free(options
);
100 static Polyhedron
*skew_into_positive_orthant(Polyhedron
*D
, unsigned nparam
,
106 for (Polyhedron
*P
= D
; P
; P
= P
->next
) {
107 POL_ENSURE_VERTICES(P
);
108 assert(!Polyhedron_is_unbounded(P
, nparam
, MaxRays
));
109 assert(P
->NbBid
== 0);
110 assert(Polyhedron_has_positive_rays(P
, nparam
));
112 for (int r
= 0; r
< P
->NbRays
; ++r
) {
113 if (value_notzero_p(P
->Ray
[r
][P
->Dimension
+1]))
115 for (int i
= 0; i
< nparam
; ++i
) {
117 if (value_posz_p(P
->Ray
[r
][i
+1]))
120 M
= Matrix_Alloc(D
->Dimension
+1, D
->Dimension
+1);
121 for (int i
= 0; i
< D
->Dimension
+1; ++i
)
122 value_set_si(M
->p
[i
][i
], 1);
124 Inner_Product(P
->Ray
[r
]+1, M
->p
[i
], D
->Dimension
+1, &tmp
);
125 if (value_posz_p(tmp
))
128 for (j
= P
->Dimension
- nparam
; j
< P
->Dimension
; ++j
)
129 if (value_pos_p(P
->Ray
[r
][j
+1]))
131 assert(j
< P
->Dimension
);
132 value_pdivision(tmp
, P
->Ray
[r
][j
+1], P
->Ray
[r
][i
+1]);
133 value_subtract(M
->p
[i
][j
], M
->p
[i
][j
], tmp
);
139 D
= DomainImage(D
, M
, MaxRays
);
145 gen_fun
* barvinok_enumerate_union_series_with_options(Polyhedron
*D
, Polyhedron
* C
,
146 barvinok_options
*options
)
148 Polyhedron
*conv
, *D2
;
150 gen_fun
*gf
= NULL
, *gf2
;
151 unsigned nparam
= C
->Dimension
;
156 CA
= align_context(C
, D
->Dimension
, options
->MaxRays
);
157 D
= DomainIntersection(D
, CA
, options
->MaxRays
);
160 D2
= skew_into_positive_orthant(D
, nparam
, options
->MaxRays
);
161 for (Polyhedron
*P
= D2
; P
; P
= P
->next
) {
162 assert(P
->Dimension
== D2
->Dimension
);
165 P_gf
= series(P
, P
->Dimension
, options
);
169 gf
->add_union(P_gf
, options
);
173 /* we actually only need the convex union of the parameter space
174 * but the reducer classes currently expect a polyhedron in
177 Polyhedron_Free(gf
->context
);
178 gf
->context
= DomainConvex(D2
, options
->MaxRays
);
180 gf2
= gf
->summate(D2
->Dimension
- nparam
, options
);
189 gen_fun
* barvinok_enumerate_union_series(Polyhedron
*D
, Polyhedron
* C
,
193 barvinok_options
*options
= barvinok_options_new_with_defaults();
194 options
->MaxRays
= MaxRays
;
195 gf
= barvinok_enumerate_union_series_with_options(D
, C
, options
);
196 barvinok_options_free(options
);
200 /* Unimodularly transform the polyhedron P, such that
201 * the direction specified by dir corresponds to the last
202 * variable in the transformed polyhedron.
203 * The number of variables is given by the length of dir.
205 static Polyhedron
*put_direction_last(Polyhedron
*P
, Vector
*dir
,
212 T
= Matrix_Alloc(P
->Dimension
+1, P
->Dimension
+1);
213 T
->NbColumns
= T
->NbRows
= n
;
214 Vector_Copy(dir
->p
, T
->p
[0], n
);
215 unimodular_complete(T
, 1);
216 Vector_Exchange(T
->p
[0], T
->p
[n
-1], n
);
217 T
->NbColumns
= T
->NbRows
= P
->Dimension
+1;
218 for (int j
= n
; j
< P
->Dimension
+1; ++j
)
219 value_set_si(T
->p
[j
][j
], 1);
221 R
= Polyhedron_Image(P
, T
, MaxRays
);
227 /* Do we need to continue shifting and subtracting?
228 * i is the number of times we shifted so far
229 * n is the number of coordinates projected out
231 static bool more_shifts_needed(int j
, int n
,
232 gen_fun
*S
, gen_fun
*S_divide
, const vec_ZZ
& up
,
233 barvinok_options
*options
)
239 /* For the 2-dimensional case, we need to subtract at most once */
245 /* Assume that we have to subtract at least once */
249 hp
= S
->Hadamard_product(S_divide
, options
);
253 empty
= hp
->summate(&c
) && value_zero_p(c
);
261 /* Return gf of P projected on last dim(P)-n coordinates, i.e.,
262 * project out the first n coordinates.
264 gen_fun
*project(Polyhedron
*P
, unsigned n
, barvinok_options
*options
)
270 struct width_direction_array
*dirs
;
273 up
.SetLength(P
->Dimension
- (n
-1));
275 for (int i
= 1; i
< P
->Dimension
- (n
-1); ++i
)
279 gen_fun
*S
, *S_shift
, *hp
;
281 S
= series(P
, P
->Dimension
, options
);
282 S_shift
= new gen_fun(S
);
284 hp
= S
->Hadamard_product(S_shift
, options
);
285 S
->add(mone
, hp
, options
);
289 gf
= S
->summate(1, options
);
295 U
= Universe_Polyhedron(P
->Dimension
- n
);
296 dirs
= Polyhedron_Lattice_Width_Directions(P
, U
, options
);
299 for (int i
= 0; i
< dirs
->n
; ++i
) {
302 gen_fun
*S
, *S_shift
, *S_divide
, *sum
;
304 CA
= align_context(dirs
->wd
[i
].domain
, P
->Dimension
, options
->MaxRays
);
305 R
= DomainIntersection(P
, CA
, options
->MaxRays
);
307 assert(dirs
->wd
[i
].dir
->Size
== n
);
308 Pi
= put_direction_last(R
, dirs
->wd
[i
].dir
, options
->MaxRays
);
311 S
= project(Pi
, n
-1, options
);
314 S_shift
= new gen_fun(S
);
315 S_divide
= new gen_fun(S
);
316 S_divide
->divide(up
);
318 for (int j
= 0; more_shifts_needed(j
, n
, S
, S_divide
, up
, options
); ++j
) {
322 hp
= S
->Hadamard_product(S_shift
, options
);
323 S
->add(mone
, hp
, options
);
328 sum
= S
->summate(1, options
);
337 gf
->add(one
, sum
, options
);
341 free_width_direction_array(dirs
);
346 gen_fun
*barvinok_enumerate_e_series(Polyhedron
*P
,
347 unsigned exist
, unsigned nparam
, barvinok_options
*options
)
350 Polyhedron
*P_orig
= P
;
352 unsigned nvar
= P
->Dimension
- exist
- nparam
;
355 return series(P
, nparam
, options
);
358 return new gen_fun(Empty_Polyhedron(nparam
));
360 assert(!Polyhedron_is_unbounded(P
, nparam
, options
->MaxRays
));
361 assert(P
->NbBid
== 0);
362 assert(Polyhedron_has_revlex_positive_rays(P
, nparam
));
364 /* Move existentially quantified variables to the front.*/
367 T
= Matrix_Alloc(exist
+nvar
+nparam
+1, nvar
+exist
+nparam
+1);
368 for (int i
= 0; i
< exist
; ++i
)
369 value_set_si(T
->p
[i
][nvar
+i
], 1);
370 for (int i
= 0; i
< nvar
; ++i
)
371 value_set_si(T
->p
[exist
+i
][i
], 1);
372 for (int i
= 0; i
< nparam
+1; ++i
)
373 value_set_si(T
->p
[exist
+nvar
+i
][nvar
+exist
+i
], 1);
374 P
= Polyhedron_Image(P
, T
, options
->MaxRays
);
379 remove_all_equalities(&P
, NULL
, &CP
, NULL
, nvar
+nparam
,
381 exist
= P
->Dimension
- (CP
->NbColumns
-1);
386 proj
= project(P
, exist
, options
);
388 proj
->substitute(CP
);
398 gf
= proj
->summate(nvar
, options
);