4 #include <NTL/mat_ZZ.h>
6 #include <barvinok/barvinok.h>
7 #include <barvinok/util.h>
8 #include "conversion.h"
9 #include "decomposer.h"
10 #include "reduce_domain.h"
20 * Returns the largest absolute value in the vector
22 static ZZ
max(vec_ZZ
& v
)
25 for (int i
= 1; i
< v
.length(); ++i
)
31 /* Remove common divisor of elements of cols of B */
32 static void normalize_cols(mat_ZZ
& B
)
35 for (int i
= 0; i
< B
.NumCols(); ++i
) {
37 for (int j
= 1 ; gcd
!= 1 && j
< B
.NumRows(); ++j
)
38 GCD(gcd
, gcd
, B
[j
][i
]);
40 for (int j
= 0; j
< B
.NumRows(); ++j
)
45 /* Remove common divisor of elements of B */
46 static void normalize_matrix(mat_ZZ
& B
)
49 for (int i
= 0; i
< B
.NumCols(); ++i
)
50 for (int j
= 0 ; j
< B
.NumRows(); ++j
) {
51 GCD(gcd
, gcd
, B
[i
][j
]);
55 for (int i
= 0; i
< B
.NumCols(); ++i
)
56 for (int j
= 0; j
< B
.NumRows(); ++j
)
62 cone(const mat_ZZ
& r
, int row
, const vec_ZZ
& w
, int s
) {
68 cone(const signed_cone
& sc
) {
74 det
= determinant(rays
);
77 bool needs_split(barvinok_options
*options
) {
81 if (options
->primal
&& index
<= options
->max_index
)
89 if (!options
->primal
&& options
->max_index
> 1) {
92 index
= abs(determinant(B2
));
93 if (index
<= options
->max_index
)
100 void short_vector(vec_ZZ
& v
, vec_ZZ
& lambda
, barvinok_options
*options
) {
103 long r
= LLL(det2
, B
, U
, options
->LLL_a
, options
->LLL_b
);
107 for (int i
= 1; i
< B
.NumRows(); ++i
) {
120 for (i
= 0; i
< lambda
.length(); ++i
)
123 if (i
== lambda
.length()) {
136 std::ostream
& operator<<(std::ostream
& os
, const cone
& c
)
138 os
<< c
.rays
<< endl
;
139 os
<< "det: " << c
.det
<< endl
;
140 os
<< "sign: " << c
.sgn
<< endl
;
144 static void decompose(const signed_cone
& sc
, signed_cone_consumer
& scc
,
145 bool primal
, barvinok_options
*options
)
147 vector
<cone
*> nonuni
;
148 cone
*c
= new cone(sc
);
149 if (c
->needs_split(options
)) {
153 options
->stats
->base_cones
++;
154 scc
.handle(signed_cone(sc
.C
, sc
.rays
, sc
.sign
, to_ulong(c
->index
)),
165 while (!nonuni
.empty()) {
168 c
->short_vector(v
, lambda
, options
);
169 for (int i
= 0; i
< c
->rays
.NumRows(); ++i
) {
172 cone
*pc
= new cone(c
->rays
, i
, v
, sign(lambda
[i
]) * c
->sgn
);
174 for (int j
= 0; j
<= i
; ++j
) {
175 if ((j
== i
&& sign(lambda
[i
]) < 0) ||
176 (j
< i
&& sign(lambda
[i
]) == sign(lambda
[j
]))) {
177 pc
->rays
[j
] = -pc
->rays
[j
];
182 if (pc
->needs_split(options
)) {
183 assert(abs(pc
->det
) < abs(c
->det
));
184 nonuni
.push_back(pc
);
187 options
->stats
->base_cones
++;
188 scc
.handle(signed_cone(pc
->rays
, pc
->sgn
,
189 to_ulong(pc
->index
)),
195 while (!nonuni
.empty()) {
208 struct polar_signed_cone_consumer
: public signed_cone_consumer
{
209 signed_cone_consumer
& scc
;
211 polar_signed_cone_consumer(signed_cone_consumer
& scc
) : scc(scc
) {}
212 virtual void handle(const signed_cone
& sc
, barvinok_options
*options
) {
213 Polyhedron
*C
= sc
.C
;
215 Matrix
*M
= Matrix_Alloc(sc
.rays
.NumRows()+1, sc
.rays
.NumCols()+2);
216 for (int i
= 0; i
< sc
.rays
.NumRows(); ++i
) {
217 zz2values(sc
.rays
[i
], M
->p
[i
]+1);
218 value_set_si(M
->p
[i
][0], 1);
220 value_set_si(M
->p
[sc
.rays
.NumRows()][0], 1);
221 value_set_si(M
->p
[sc
.rays
.NumRows()][1+sc
.rays
.NumCols()], 1);
222 C
= Rays2Polyhedron(M
, M
->NbRows
+1);
223 assert(C
->NbConstraints
== C
->NbRays
);
226 Polyhedron_Polarize(C
);
228 scc
.handle(signed_cone(C
, r
, sc
.sign
, sc
.det
), options
);
234 /* Remove common divisor of elements of rows of B */
235 static void normalize_rows(mat_ZZ
& B
)
238 for (int i
= 0; i
< B
.NumRows(); ++i
) {
240 for (int j
= 1 ; gcd
!= 1 && j
< B
.NumCols(); ++j
)
241 GCD(gcd
, gcd
, B
[i
][j
]);
243 for (int j
= 0; j
< B
.NumCols(); ++j
)
248 static void polar_decompose(Polyhedron
*cone
, signed_cone_consumer
& scc
,
249 barvinok_options
*options
)
251 POL_ENSURE_VERTICES(cone
);
252 Polyhedron_Polarize(cone
);
253 if (cone
->NbRays
- 1 != cone
->Dimension
) {
254 Polyhedron
*tmp
= cone
;
255 cone
= triangulate_cone_with_options(cone
, options
);
256 Polyhedron_Free(tmp
);
258 polar_signed_cone_consumer
pssc(scc
);
261 for (Polyhedron
*Polar
= cone
; Polar
; Polar
= Polar
->next
) {
264 decompose(signed_cone(Polar
, r
, 1), pssc
, false, options
);
273 static void primal_decompose(Polyhedron
*cone
, signed_cone_consumer
& scc
,
274 barvinok_options
*options
)
276 POL_ENSURE_VERTICES(cone
);
278 if (cone
->NbRays
- 1 == cone
->Dimension
)
281 parts
= triangulate_cone_with_options(cone
, options
);
282 Vector
*average
= NULL
;
286 average
= inner_point(cone
);
290 for (Polyhedron
*simple
= parts
; simple
; simple
= simple
->next
) {
292 Matrix
*Rays
= rays2(simple
);
293 for (int i
= 0; i
< Rays
->NbRows
; ++i
) {
294 if (simple
== cone
) {
298 for (f
= 0; f
< simple
->NbConstraints
; ++f
) {
299 Inner_Product(Rays
->p
[i
], simple
->Constraint
[f
]+1,
300 simple
->Dimension
, &tmp
);
301 if (value_notzero_p(tmp
))
304 assert(f
< simple
->NbConstraints
);
305 if (!is_internal(average
, simple
->Constraint
[f
])) {
306 Vector_Oppose(Rays
->p
[i
], Rays
->p
[i
], Rays
->NbColumns
);
311 matrix2zz(Rays
, ray
, Rays
->NbRows
, Rays
->NbColumns
);
313 decompose(signed_cone(simple
, ray
, sign
), scc
, true, options
);
319 Vector_Free(average
);
326 Vector_Free(average
);
332 void barvinok_decompose(Polyhedron
*C
, signed_cone_consumer
& scc
,
333 barvinok_options
*options
)
336 primal_decompose(C
, scc
, options
);
338 polar_decompose(C
, scc
, options
);
341 void vertex_decomposer::decompose_at_vertex(Param_Vertices
*V
, int _i
,
342 barvinok_options
*options
)
344 Polyhedron
*C
= supporting_cone_p(P
, V
);
348 barvinok_decompose(C
, scc
, options
);
351 struct posneg_collector
: public signed_cone_consumer
{
352 posneg_collector(Polyhedron
*pos
, Polyhedron
*neg
) : pos(pos
), neg(neg
) {}
353 virtual void handle(const signed_cone
& sc
, barvinok_options
*options
) {
354 Polyhedron
*p
= Polyhedron_Copy(sc
.C
);
368 * Barvinok's Decomposition of a simplicial cone
370 * Returns two lists of polyhedra
372 void barvinok_decompose(Polyhedron
*C
, Polyhedron
**ppos
, Polyhedron
**pneg
)
374 barvinok_options
*options
= barvinok_options_new_with_defaults();
375 posneg_collector
pc(*ppos
, *pneg
);
376 POL_ENSURE_VERTICES(C
);
379 decompose(signed_cone(C
, r
, 1), pc
, false, options
);
382 barvinok_options_free(options
);