4 #include <NTL/mat_ZZ.h>
6 #include <barvinok/barvinok.h>
7 #include <barvinok/util.h>
8 #include "conversion.h"
9 #include "decomposer.h"
17 * Returns the largest absolute value in the vector
19 static ZZ
max(vec_ZZ
& v
)
22 for (int i
= 1; i
< v
.length(); ++i
)
32 Rays
= Matrix_Copy(M
);
36 Cone
= Polyhedron_Copy(C
);
42 matrix2zz(Rays
, A
, Rays
->NbRows
- 1, Rays
->NbColumns
- 1);
46 Vector
* short_vector(vec_ZZ
& lambda
, barvinok_options
*options
) {
47 Matrix
*M
= Matrix_Copy(Rays
);
48 Matrix
*inv
= Matrix_Alloc(M
->NbRows
, M
->NbColumns
);
49 int ok
= Matrix_Inverse(M
, inv
);
56 matrix2zz(inv
, B
, inv
->NbRows
- 1, inv
->NbColumns
- 1);
57 long r
= LLL(det2
, B
, U
, options
->LLL_a
, options
->LLL_b
);
61 for (int i
= 1; i
< B
.NumRows(); ++i
) {
73 Vector
*z
= Vector_Alloc(U
[index
].length()+1);
75 zz2values(U
[index
], z
->p
);
76 value_set_si(z
->p
[U
[index
].length()], 0);
78 Polyhedron
*C
= poly();
80 for (i
= 0; i
< lambda
.length(); ++i
)
83 if (i
== lambda
.length()) {
86 value_set_si(tmp
, -1);
87 Vector_Scale(z
->p
, z
->p
, tmp
, z
->Size
-1);
94 Polyhedron_Free(Cone
);
100 Matrix
*M
= Matrix_Alloc(Rays
->NbRows
+1, Rays
->NbColumns
+1);
101 for (int i
= 0; i
< Rays
->NbRows
; ++i
) {
102 Vector_Copy(Rays
->p
[i
], M
->p
[i
]+1, Rays
->NbColumns
);
103 value_set_si(M
->p
[i
][0], 1);
105 Vector_Set(M
->p
[Rays
->NbRows
]+1, 0, Rays
->NbColumns
-1);
106 value_set_si(M
->p
[Rays
->NbRows
][0], 1);
107 value_set_si(M
->p
[Rays
->NbRows
][Rays
->NbColumns
], 1);
108 Cone
= Rays2Polyhedron(M
, M
->NbRows
+1);
109 assert(Cone
->NbConstraints
== Cone
->NbRays
);
120 void decomposer::decompose(Polyhedron
*C
, barvinok_options
*options
)
122 vector
<cone
*> nonuni
;
123 cone
* c
= new cone(C
);
131 options
->stats
.unimodular_cones
++;
140 while (!nonuni
.empty()) {
143 Vector
* v
= c
->short_vector(lambda
, options
);
144 for (int i
= 0; i
< c
->Rays
->NbRows
- 1; ++i
) {
147 Matrix
* M
= Matrix_Copy(c
->Rays
);
148 Vector_Copy(v
->p
, M
->p
[i
], v
->Size
);
149 cone
* pc
= new cone(M
);
150 assert (pc
->det
!= 0);
151 if (abs(pc
->det
) > 1) {
152 assert(abs(pc
->det
) < abs(c
->det
));
153 nonuni
.push_back(pc
);
156 options
->stats
.unimodular_cones
++;
157 handle(pc
->poly(), sign(pc
->det
) * s
);
162 while (!nonuni
.empty()) {
179 void polar_decomposer::decompose(Polyhedron
*cone
, barvinok_options
*options
)
181 POL_ENSURE_VERTICES(cone
);
182 Polyhedron_Polarize(cone
);
183 if (cone
->NbRays
- 1 != cone
->Dimension
) {
184 Polyhedron
*tmp
= cone
;
185 cone
= triangulate_cone(cone
, options
->MaxRays
);
186 Polyhedron_Free(tmp
);
189 for (Polyhedron
*Polar
= cone
; Polar
; Polar
= Polar
->next
)
190 decomposer::decompose(Polar
, options
);
198 void polar_decomposer::handle(Polyhedron
*P
, int sign
)
200 Polyhedron_Polarize(P
);
201 handle_polar(P
, sign
);
204 void vertex_decomposer::decompose_at_vertex(Param_Vertices
*V
, int _i
,
205 barvinok_options
*options
)
207 Polyhedron
*C
= supporting_cone_p(P
, V
);
211 pd
->decompose(C
, options
);
215 * Barvinok's Decomposition of a simplicial cone
217 * Returns two lists of polyhedra
219 void barvinok_decompose(Polyhedron
*C
, Polyhedron
**ppos
, Polyhedron
**pneg
)
221 barvinok_options
*options
= barvinok_options_new_with_defaults();
222 Polyhedron
*pos
= *ppos
, *neg
= *pneg
;
223 vector
<cone
*> nonuni
;
224 cone
* c
= new cone(C
);
231 Polyhedron
*p
= Polyhedron_Copy(c
->Cone
);
237 while (!nonuni
.empty()) {
240 Vector
* v
= c
->short_vector(lambda
, options
);
241 for (int i
= 0; i
< c
->Rays
->NbRows
- 1; ++i
) {
244 Matrix
* M
= Matrix_Copy(c
->Rays
);
245 Vector_Copy(v
->p
, M
->p
[i
], v
->Size
);
246 cone
* pc
= new cone(M
);
247 assert (pc
->det
!= 0);
248 if (abs(pc
->det
) > 1) {
249 assert(abs(pc
->det
) < abs(c
->det
));
250 nonuni
.push_back(pc
);
252 Polyhedron
*p
= pc
->poly();
254 if (sign(pc
->det
) == s
) {