3 #include <NTL/mat_ZZ.h>
6 #include <polylib/polylibgmp.h>
8 #include <barvinok/barvinok.h>
9 #include <barvinok/util.h>
10 #include "conversion.h"
11 #include "decomposer.h"
19 * Returns the largest absolute value in the vector
21 static ZZ
max(vec_ZZ
& v
)
24 for (int i
= 1; i
< v
.length(); ++i
)
34 Rays
= Matrix_Copy(M
);
38 Cone
= Polyhedron_Copy(C
);
44 matrix2zz(Rays
, A
, Rays
->NbRows
- 1, Rays
->NbColumns
- 1);
48 Vector
* short_vector(vec_ZZ
& lambda
) {
49 Matrix
*M
= Matrix_Copy(Rays
);
50 Matrix
*inv
= Matrix_Alloc(M
->NbRows
, M
->NbColumns
);
51 int ok
= Matrix_Inverse(M
, inv
);
58 matrix2zz(inv
, B
, inv
->NbRows
- 1, inv
->NbColumns
- 1);
59 long r
= LLL(det2
, B
, U
);
63 for (int i
= 1; i
< B
.NumRows(); ++i
) {
75 Vector
*z
= Vector_Alloc(U
[index
].length()+1);
77 zz2values(U
[index
], z
->p
);
78 value_set_si(z
->p
[U
[index
].length()], 0);
80 Polyhedron
*C
= poly();
82 for (i
= 0; i
< lambda
.length(); ++i
)
85 if (i
== lambda
.length()) {
88 value_set_si(tmp
, -1);
89 Vector_Scale(z
->p
, z
->p
, tmp
, z
->Size
-1);
96 Polyhedron_Free(Cone
);
102 Matrix
*M
= Matrix_Alloc(Rays
->NbRows
+1, Rays
->NbColumns
+1);
103 for (int i
= 0; i
< Rays
->NbRows
; ++i
) {
104 Vector_Copy(Rays
->p
[i
], M
->p
[i
]+1, Rays
->NbColumns
);
105 value_set_si(M
->p
[i
][0], 1);
107 Vector_Set(M
->p
[Rays
->NbRows
]+1, 0, Rays
->NbColumns
-1);
108 value_set_si(M
->p
[Rays
->NbRows
][0], 1);
109 value_set_si(M
->p
[Rays
->NbRows
][Rays
->NbColumns
], 1);
110 Cone
= Rays2Polyhedron(M
, M
->NbRows
+1);
111 assert(Cone
->NbConstraints
== Cone
->NbRays
);
122 void decomposer::decompose(Polyhedron
*C
)
124 vector
<cone
*> nonuni
;
125 cone
* c
= new cone(C
);
141 while (!nonuni
.empty()) {
144 Vector
* v
= c
->short_vector(lambda
);
145 for (int i
= 0; i
< c
->Rays
->NbRows
- 1; ++i
) {
148 Matrix
* M
= Matrix_Copy(c
->Rays
);
149 Vector_Copy(v
->p
, M
->p
[i
], v
->Size
);
150 cone
* pc
= new cone(M
);
151 assert (pc
->det
!= 0);
152 if (abs(pc
->det
) > 1) {
153 assert(abs(pc
->det
) < abs(c
->det
));
154 nonuni
.push_back(pc
);
157 handle(pc
->poly(), sign(pc
->det
) * s
);
162 while (!nonuni
.empty()) {
179 void polar_decomposer::decompose(Polyhedron
*cone
, unsigned MaxRays
)
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
, MaxRays
);
186 Polyhedron_Free(tmp
);
189 for (Polyhedron
*Polar
= cone
; Polar
; Polar
= Polar
->next
)
190 decomposer::decompose(Polar
);
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
,
207 Polyhedron
*C
= supporting_cone_p(P
, V
);
211 pd
->decompose(C
, MaxRays
);
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 Polyhedron
*pos
= *ppos
, *neg
= *pneg
;
222 vector
<cone
*> nonuni
;
223 cone
* c
= new cone(C
);
230 Polyhedron
*p
= Polyhedron_Copy(c
->Cone
);
236 while (!nonuni
.empty()) {
239 Vector
* v
= c
->short_vector(lambda
);
240 for (int i
= 0; i
< c
->Rays
->NbRows
- 1; ++i
) {
243 Matrix
* M
= Matrix_Copy(c
->Rays
);
244 Vector_Copy(v
->p
, M
->p
[i
], v
->Size
);
245 cone
* pc
= new cone(M
);
246 assert (pc
->det
!= 0);
247 if (abs(pc
->det
) > 1) {
248 assert(abs(pc
->det
) < abs(c
->det
));
249 nonuni
.push_back(pc
);
251 Polyhedron
*p
= pc
->poly();
253 if (sign(pc
->det
) == s
) {