2 #include <barvinok/barvinok.h>
3 #include <barvinok/util.h>
4 #include "genfun_constructor.h"
5 #include "remove_equalities.h"
7 /* Check whether all rays point in the positive directions
10 static bool Polyhedron_has_positive_rays(Polyhedron
*P
, unsigned nparam
)
13 for (r
= 0; r
< P
->NbRays
; ++r
)
14 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1])) {
16 for (i
= P
->Dimension
- nparam
; i
< P
->Dimension
; ++i
)
17 if (value_neg_p(P
->Ray
[r
][i
+1]))
24 * remove equalities that require a "compression" of the parameters
26 static Polyhedron
*remove_more_equalities(Polyhedron
*P
, unsigned nparam
,
27 Matrix
**CP
, unsigned MaxRays
)
30 remove_all_equalities(&P
, NULL
, CP
, NULL
, nparam
, MaxRays
);
37 static gen_fun
*series(Polyhedron
*P
, unsigned nparam
, barvinok_options
*options
)
47 assert(!Polyhedron_is_unbounded(P
, nparam
, options
->MaxRays
));
48 assert(P
->NbBid
== 0);
49 assert(Polyhedron_has_revlex_positive_rays(P
, nparam
));
51 P
= remove_more_equalities(P
, nparam
, &CP
, options
->MaxRays
);
54 nparam
= CP
->NbColumns
-1;
59 barvinok_count_with_options(P
, &c
, options
);
64 red
= gf_base::create(Polyhedron_Project(P
, nparam
),
65 P
->Dimension
, nparam
, options
);
66 POL_ENSURE_VERTICES(P
);
67 red
->start_gf(P
, options
);
79 gen_fun
* barvinok_series_with_options(Polyhedron
*P
, Polyhedron
* C
,
80 barvinok_options
*options
)
83 unsigned nparam
= C
->Dimension
;
86 CA
= align_context(C
, P
->Dimension
, options
->MaxRays
);
87 P
= DomainIntersection(P
, CA
, options
->MaxRays
);
90 gf
= series(P
, nparam
, options
);
95 gen_fun
* barvinok_series(Polyhedron
*P
, Polyhedron
* C
, unsigned MaxRays
)
98 barvinok_options
*options
= barvinok_options_new_with_defaults();
99 options
->MaxRays
= MaxRays
;
100 gf
= barvinok_series_with_options(P
, C
, options
);
101 barvinok_options_free(options
);
105 static Polyhedron
*skew_into_positive_orthant(Polyhedron
*D
, unsigned nparam
,
111 for (Polyhedron
*P
= D
; P
; P
= P
->next
) {
112 POL_ENSURE_VERTICES(P
);
113 assert(!Polyhedron_is_unbounded(P
, nparam
, MaxRays
));
114 assert(P
->NbBid
== 0);
115 assert(Polyhedron_has_positive_rays(P
, nparam
));
117 for (int r
= 0; r
< P
->NbRays
; ++r
) {
118 if (value_notzero_p(P
->Ray
[r
][P
->Dimension
+1]))
120 for (int i
= 0; i
< nparam
; ++i
) {
122 if (value_posz_p(P
->Ray
[r
][i
+1]))
125 M
= Matrix_Alloc(D
->Dimension
+1, D
->Dimension
+1);
126 for (int i
= 0; i
< D
->Dimension
+1; ++i
)
127 value_set_si(M
->p
[i
][i
], 1);
129 Inner_Product(P
->Ray
[r
]+1, M
->p
[i
], D
->Dimension
+1, &tmp
);
130 if (value_posz_p(tmp
))
133 for (j
= P
->Dimension
- nparam
; j
< P
->Dimension
; ++j
)
134 if (value_pos_p(P
->Ray
[r
][j
+1]))
136 assert(j
< P
->Dimension
);
137 value_pdivision(tmp
, P
->Ray
[r
][j
+1], P
->Ray
[r
][i
+1]);
138 value_subtract(M
->p
[i
][j
], M
->p
[i
][j
], tmp
);
144 D
= DomainImage(D
, M
, MaxRays
);
150 gen_fun
* barvinok_enumerate_union_series_with_options(Polyhedron
*D
, Polyhedron
* C
,
151 barvinok_options
*options
)
153 Polyhedron
*conv
, *D2
;
155 gen_fun
*gf
= NULL
, *gf2
;
156 unsigned nparam
= C
->Dimension
;
161 CA
= align_context(C
, D
->Dimension
, options
->MaxRays
);
162 D
= DomainIntersection(D
, CA
, options
->MaxRays
);
165 D2
= skew_into_positive_orthant(D
, nparam
, options
->MaxRays
);
166 for (Polyhedron
*P
= D2
; P
; P
= P
->next
) {
167 assert(P
->Dimension
== D2
->Dimension
);
170 P_gf
= series(Polyhedron_Copy(P
), P
->Dimension
, options
);
174 gf
->add_union(P_gf
, options
);
178 /* we actually only need the convex union of the parameter space
179 * but the reducer classes currently expect a polyhedron in
182 Polyhedron_Free(gf
->context
);
183 gf
->context
= DomainConvex(D2
, options
->MaxRays
);
185 gf2
= gf
->summate(D2
->Dimension
- nparam
, options
);
194 gen_fun
* barvinok_enumerate_union_series(Polyhedron
*D
, Polyhedron
* C
,
198 barvinok_options
*options
= barvinok_options_new_with_defaults();
199 options
->MaxRays
= MaxRays
;
200 gf
= barvinok_enumerate_union_series_with_options(D
, C
, options
);
201 barvinok_options_free(options
);