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 "param_util.h"
11 #include "reduce_domain.h"
21 * Returns the largest absolute value in the vector
23 static ZZ
max(vec_ZZ
& v
)
26 for (int i
= 1; i
< v
.length(); ++i
)
32 /* Remove common divisor of elements of cols of B */
33 static void normalize_cols(mat_ZZ
& B
)
36 for (int i
= 0; i
< B
.NumCols(); ++i
) {
38 for (int j
= 1 ; gcd
!= 1 && j
< B
.NumRows(); ++j
)
39 GCD(gcd
, gcd
, B
[j
][i
]);
41 for (int j
= 0; j
< B
.NumRows(); ++j
)
46 /* Remove common divisor of elements of B */
47 static void normalize_matrix(mat_ZZ
& B
)
50 for (int i
= 0; i
< B
.NumCols(); ++i
)
51 for (int j
= 0 ; j
< B
.NumRows(); ++j
) {
52 GCD(gcd
, gcd
, B
[i
][j
]);
56 for (int i
= 0; i
< B
.NumCols(); ++i
)
57 for (int j
= 0; j
< B
.NumRows(); ++j
)
63 cone(const mat_ZZ
& r
, int row
, const vec_ZZ
& w
, int s
) {
69 cone(const signed_cone
& sc
) {
75 det
= determinant(rays
);
78 bool needs_split(barvinok_options
*options
) {
82 if (options
->primal
&& index
<= options
->max_index
)
90 if (!options
->primal
&& options
->max_index
> 1) {
93 index
= abs(determinant(B2
));
94 if (index
<= options
->max_index
)
101 void short_vector(vec_ZZ
& v
, vec_ZZ
& lambda
, barvinok_options
*options
) {
104 long r
= LLL(det2
, B
, U
, options
->LLL_a
, options
->LLL_b
);
108 for (int i
= 1; i
< B
.NumRows(); ++i
) {
121 for (i
= 0; i
< lambda
.length(); ++i
)
124 if (i
== lambda
.length()) {
137 std::ostream
& operator<<(std::ostream
& os
, const cone
& c
)
139 os
<< c
.rays
<< endl
;
140 os
<< "det: " << c
.det
<< endl
;
141 os
<< "sign: " << c
.sgn
<< endl
;
145 static void decompose(const signed_cone
& sc
, signed_cone_consumer
& scc
,
146 bool primal
, barvinok_options
*options
)
148 vector
<cone
*> nonuni
;
149 cone
*c
= new cone(sc
);
150 if (c
->needs_split(options
)) {
154 options
->stats
->base_cones
++;
155 scc
.handle(signed_cone(sc
.C
, sc
.rays
, sc
.sign
, to_ulong(c
->index
)),
166 while (!nonuni
.empty()) {
169 c
->short_vector(v
, lambda
, options
);
170 for (int i
= 0; i
< c
->rays
.NumRows(); ++i
) {
173 cone
*pc
= new cone(c
->rays
, i
, v
, sign(lambda
[i
]) * c
->sgn
);
175 for (int j
= 0; j
<= i
; ++j
) {
176 if ((j
== i
&& sign(lambda
[i
]) < 0) ||
177 (j
< i
&& sign(lambda
[i
]) == sign(lambda
[j
]))) {
178 pc
->rays
[j
] = -pc
->rays
[j
];
183 if (pc
->needs_split(options
)) {
184 assert(abs(pc
->det
) < abs(c
->det
));
185 nonuni
.push_back(pc
);
188 options
->stats
->base_cones
++;
189 scc
.handle(signed_cone(pc
->rays
, pc
->sgn
,
190 to_ulong(pc
->index
)),
196 while (!nonuni
.empty()) {
209 struct polar_signed_cone_consumer
: public signed_cone_consumer
{
210 signed_cone_consumer
& scc
;
212 polar_signed_cone_consumer(signed_cone_consumer
& scc
) : scc(scc
) {}
213 virtual void handle(const signed_cone
& sc
, barvinok_options
*options
) {
214 Polyhedron
*C
= sc
.C
;
216 Matrix
*M
= Matrix_Alloc(sc
.rays
.NumRows()+1, sc
.rays
.NumCols()+2);
217 for (int i
= 0; i
< sc
.rays
.NumRows(); ++i
) {
218 zz2values(sc
.rays
[i
], M
->p
[i
]+1);
219 value_set_si(M
->p
[i
][0], 1);
221 value_set_si(M
->p
[sc
.rays
.NumRows()][0], 1);
222 value_set_si(M
->p
[sc
.rays
.NumRows()][1+sc
.rays
.NumCols()], 1);
223 C
= Rays2Polyhedron(M
, M
->NbRows
+1);
224 assert(C
->NbConstraints
== C
->NbRays
);
227 Polyhedron_Polarize(C
);
230 scc
.handle(signed_cone(C
, r
, sc
.sign
, sc
.det
), options
);
241 /* Remove common divisor of elements of rows of B */
242 static void normalize_rows(mat_ZZ
& B
)
245 for (int i
= 0; i
< B
.NumRows(); ++i
) {
247 for (int j
= 1 ; gcd
!= 1 && j
< B
.NumCols(); ++j
)
248 GCD(gcd
, gcd
, B
[i
][j
]);
250 for (int j
= 0; j
< B
.NumCols(); ++j
)
255 static void polar_decompose(Polyhedron
*cone
, signed_cone_consumer
& scc
,
256 barvinok_options
*options
)
258 POL_ENSURE_VERTICES(cone
);
259 Polyhedron_Polarize(cone
);
260 if (cone
->NbRays
- 1 != cone
->Dimension
) {
261 Polyhedron
*tmp
= cone
;
262 cone
= triangulate_cone_with_options(cone
, options
);
263 Polyhedron_Free(tmp
);
265 polar_signed_cone_consumer
pssc(scc
);
268 for (Polyhedron
*Polar
= cone
; Polar
; Polar
= Polar
->next
) {
271 decompose(signed_cone(Polar
, r
, 1), pssc
, false, options
);
280 static void primal_decompose(Polyhedron
*cone
, signed_cone_consumer
& scc
,
281 barvinok_options
*options
)
283 POL_ENSURE_VERTICES(cone
);
285 if (cone
->NbRays
- 1 == cone
->Dimension
)
288 parts
= triangulate_cone_with_options(cone
, options
);
289 Vector
*average
= NULL
;
293 average
= inner_point(cone
);
297 for (Polyhedron
*simple
= parts
; simple
; simple
= simple
->next
) {
299 Matrix
*Rays
= rays2(simple
);
300 for (int i
= 0; i
< Rays
->NbRows
; ++i
) {
301 if (simple
== cone
) {
305 for (f
= 0; f
< simple
->NbConstraints
; ++f
) {
306 Inner_Product(Rays
->p
[i
], simple
->Constraint
[f
]+1,
307 simple
->Dimension
, &tmp
);
308 if (value_notzero_p(tmp
))
311 assert(f
< simple
->NbConstraints
);
312 if (!is_internal(average
, simple
->Constraint
[f
])) {
313 Vector_Oppose(Rays
->p
[i
], Rays
->p
[i
], Rays
->NbColumns
);
318 matrix2zz(Rays
, ray
, Rays
->NbRows
, Rays
->NbColumns
);
320 decompose(signed_cone(simple
, ray
, sign
), scc
, true, options
);
326 Vector_Free(average
);
333 Vector_Free(average
);
339 void barvinok_decompose(Polyhedron
*C
, signed_cone_consumer
& scc
,
340 barvinok_options
*options
)
342 POL_ENSURE_VERTICES(C
);
344 primal_decompose(C
, scc
, options
);
346 polar_decompose(C
, scc
, options
);
349 void vertex_decomposer::decompose_at_vertex(Param_Vertices
*V
, int _i
,
350 barvinok_options
*options
)
352 Polyhedron
*C
= Param_Vertex_Cone(PP
, V
, options
);
356 barvinok_decompose(C
, scc
, options
);
359 struct posneg_collector
: public signed_cone_consumer
{
360 posneg_collector(Polyhedron
*pos
, Polyhedron
*neg
) : pos(pos
), neg(neg
) {}
361 virtual void handle(const signed_cone
& sc
, barvinok_options
*options
) {
362 Polyhedron
*p
= Polyhedron_Copy(sc
.C
);
376 * Barvinok's Decomposition of a simplicial cone
378 * Returns two lists of polyhedra
380 void barvinok_decompose(Polyhedron
*C
, Polyhedron
**ppos
, Polyhedron
**pneg
)
382 barvinok_options
*options
= barvinok_options_new_with_defaults();
383 posneg_collector
pc(*ppos
, *pneg
);
384 POL_ENSURE_VERTICES(C
);
387 decompose(signed_cone(C
, r
, 1), pc
, false, options
);
390 barvinok_options_free(options
);