6 #include <NTL/vec_ZZ.h>
7 #include <NTL/mat_ZZ.h>
9 #include <barvinok/barvinok.h>
10 #include <barvinok/util.h>
11 #include "conversion.h"
12 #include "decomposer.h"
13 #include "param_util.h"
14 #include "reduce_domain.h"
22 * Returns the largest absolute value in the vector
24 static ZZ
max(vec_ZZ
& v
)
27 for (int i
= 1; i
< v
.length(); ++i
)
33 /* Remove common divisor of elements of cols of B */
34 static void normalize_cols(mat_ZZ
& B
)
37 for (int i
= 0; i
< B
.NumCols(); ++i
) {
39 for (int j
= 1 ; gcd
!= 1 && j
< B
.NumRows(); ++j
)
40 GCD(gcd
, gcd
, B
[j
][i
]);
42 for (int j
= 0; j
< B
.NumRows(); ++j
)
47 /* Remove common divisor of elements of B */
48 static void normalize_matrix(mat_ZZ
& B
)
51 for (int i
= 0; i
< B
.NumCols(); ++i
)
52 for (int j
= 0 ; j
< B
.NumRows(); ++j
) {
53 GCD(gcd
, gcd
, B
[i
][j
]);
57 for (int i
= 0; i
< B
.NumCols(); ++i
)
58 for (int j
= 0; j
< B
.NumRows(); ++j
)
64 cone(const mat_ZZ
& r
, int row
, const vec_ZZ
& w
, int s
) {
70 cone(const signed_cone
& sc
) {
76 det
= determinant(rays
);
79 bool needs_split(barvinok_options
*options
) {
83 if (options
->primal
&& index
<= options
->max_index
)
91 if (!options
->primal
&& options
->max_index
> 1) {
94 index
= abs(determinant(B2
));
95 if (index
<= options
->max_index
)
102 void short_vector(vec_ZZ
& v
, vec_ZZ
& lambda
, barvinok_options
*options
) {
106 LLL(det2
, B
, U
, options
->LLL_a
, options
->LLL_b
);
110 for (int i
= 1; i
< B
.NumRows(); ++i
) {
123 for (i
= 0; i
< lambda
.length(); ++i
)
126 if (i
== lambda
.length()) {
139 std::ostream
& operator<<(std::ostream
& os
, const cone
& c
)
141 os
<< c
.rays
<< endl
;
142 os
<< "det: " << c
.det
<< endl
;
143 os
<< "sign: " << c
.sgn
<< endl
;
147 static void decompose(const signed_cone
& sc
, signed_cone_consumer
& scc
,
148 bool primal
, barvinok_options
*options
)
150 vector
<cone
*> nonuni
;
151 cone
*c
= new cone(sc
);
152 if (c
->needs_split(options
)) {
156 options
->stats
->base_cones
++;
157 scc
.handle(signed_cone(sc
.C
, sc
.rays
, sc
.sign
, to_ulong(c
->index
)),
168 while (!nonuni
.empty()) {
171 c
->short_vector(v
, lambda
, options
);
172 for (int i
= 0; i
< c
->rays
.NumRows(); ++i
) {
175 cone
*pc
= new cone(c
->rays
, i
, v
, sign(lambda
[i
]) * c
->sgn
);
177 for (int j
= 0; j
<= i
; ++j
) {
178 if ((j
== i
&& sign(lambda
[i
]) < 0) ||
179 (j
< i
&& sign(lambda
[i
]) == sign(lambda
[j
]))) {
180 pc
->rays
[j
] = -pc
->rays
[j
];
185 if (pc
->needs_split(options
)) {
186 assert(abs(pc
->det
) < abs(c
->det
));
187 nonuni
.push_back(pc
);
190 options
->stats
->base_cones
++;
191 scc
.handle(signed_cone(pc
->rays
, pc
->sgn
,
192 to_ulong(pc
->index
)),
198 while (!nonuni
.empty()) {
211 struct polar_signed_cone_consumer
: public signed_cone_consumer
{
212 signed_cone_consumer
& scc
;
214 polar_signed_cone_consumer(signed_cone_consumer
& scc
) : scc(scc
) {}
215 virtual void handle(const signed_cone
& sc
, barvinok_options
*options
) {
216 Polyhedron
*C
= sc
.C
;
218 Matrix
*M
= Matrix_Alloc(sc
.rays
.NumRows()+1, sc
.rays
.NumCols()+2);
219 for (int i
= 0; i
< sc
.rays
.NumRows(); ++i
) {
220 zz2values(sc
.rays
[i
], M
->p
[i
]+1);
221 value_set_si(M
->p
[i
][0], 1);
223 value_set_si(M
->p
[sc
.rays
.NumRows()][0], 1);
224 value_set_si(M
->p
[sc
.rays
.NumRows()][1+sc
.rays
.NumCols()], 1);
225 C
= Rays2Polyhedron(M
, M
->NbRows
+1);
226 assert(C
->NbConstraints
== C
->NbRays
);
229 Polyhedron_Polarize(C
);
232 scc
.handle(signed_cone(C
, r
, sc
.sign
, sc
.det
), options
);
243 /* Remove common divisor of elements of rows of B */
244 static void normalize_rows(mat_ZZ
& B
)
247 for (int i
= 0; i
< B
.NumRows(); ++i
) {
249 for (int j
= 1 ; gcd
!= 1 && j
< B
.NumCols(); ++j
)
250 GCD(gcd
, gcd
, B
[i
][j
]);
252 for (int j
= 0; j
< B
.NumCols(); ++j
)
257 static void polar_decompose(Polyhedron
*cone
, signed_cone_consumer
& scc
,
258 barvinok_options
*options
)
260 POL_ENSURE_VERTICES(cone
);
261 Polyhedron_Polarize(cone
);
262 if (cone
->NbRays
- 1 != cone
->Dimension
) {
263 Polyhedron
*tmp
= cone
;
264 cone
= triangulate_cone_with_options(cone
, options
);
265 Polyhedron_Free(tmp
);
267 polar_signed_cone_consumer
pssc(scc
);
270 for (Polyhedron
*Polar
= cone
; Polar
; Polar
= Polar
->next
) {
273 decompose(signed_cone(Polar
, r
, 1), pssc
, false, options
);
282 static void primal_decompose(Polyhedron
*cone
, signed_cone_consumer
& scc
,
283 barvinok_options
*options
)
285 POL_ENSURE_VERTICES(cone
);
287 if (cone
->NbRays
- 1 == cone
->Dimension
)
290 parts
= triangulate_cone_with_options(cone
, options
);
291 Vector
*average
= NULL
;
295 average
= inner_point(cone
);
299 for (Polyhedron
*simple
= parts
; simple
; simple
= simple
->next
) {
301 Matrix
*Rays
= rays2(simple
);
302 for (int i
= 0; i
< Rays
->NbRows
; ++i
) {
303 if (simple
== cone
) {
307 for (f
= 0; f
< simple
->NbConstraints
; ++f
) {
308 Inner_Product(Rays
->p
[i
], simple
->Constraint
[f
]+1,
309 simple
->Dimension
, &tmp
);
310 if (value_notzero_p(tmp
))
313 assert(f
< simple
->NbConstraints
);
314 if (!is_internal(average
, simple
->Constraint
[f
])) {
315 Vector_Oppose(Rays
->p
[i
], Rays
->p
[i
], Rays
->NbColumns
);
320 matrix2zz(Rays
, ray
, Rays
->NbRows
, Rays
->NbColumns
);
322 decompose(signed_cone(simple
, ray
, sign
), scc
, true, options
);
328 Vector_Free(average
);
335 Vector_Free(average
);
341 void barvinok_decompose(Polyhedron
*C
, signed_cone_consumer
& scc
,
342 barvinok_options
*options
)
344 POL_ENSURE_VERTICES(C
);
346 primal_decompose(C
, scc
, options
);
348 polar_decompose(C
, scc
, options
);
351 void vertex_decomposer::decompose_at_vertex(Param_Vertices
*V
, int _i
,
352 barvinok_options
*options
)
354 Polyhedron
*C
= Param_Vertex_Cone(PP
, V
, options
);
358 barvinok_decompose(C
, scc
, options
);
361 struct posneg_collector
: public signed_cone_consumer
{
362 posneg_collector(Polyhedron
*pos
, Polyhedron
*neg
) : pos(pos
), neg(neg
) {}
363 virtual void handle(const signed_cone
& sc
, barvinok_options
*options
) {
364 Polyhedron
*p
= Polyhedron_Copy(sc
.C
);
378 * Barvinok's Decomposition of a simplicial cone
380 * Returns two lists of polyhedra
382 void barvinok_decompose(Polyhedron
*C
, Polyhedron
**ppos
, Polyhedron
**pneg
)
384 barvinok_options
*options
= barvinok_options_new_with_defaults();
385 posneg_collector
pc(*ppos
, *pneg
);
386 POL_ENSURE_VERTICES(C
);
389 decompose(signed_cone(C
, r
, 1), pc
, false, options
);
392 barvinok_options_free(options
);