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(const signed_cone
& sc
) {
37 Cone
= Polyhedron_Copy(sc
.C
);
40 set_closed(sc
.closed
);
44 matrix2zz(Rays
, A
, Rays
->NbRows
- 1, Rays
->NbColumns
- 1);
47 void set_closed(int *cl
) {
50 closed
= new int[Rays
->NbRows
-1];
51 for (int i
= 0; i
< Rays
->NbRows
-1; ++i
)
56 Vector
* short_vector(vec_ZZ
& lambda
, barvinok_options
*options
) {
57 Matrix
*M
= Matrix_Copy(Rays
);
58 Matrix
*inv
= Matrix_Alloc(M
->NbRows
, M
->NbColumns
);
59 int ok
= Matrix_Inverse(M
, inv
);
66 matrix2zz(inv
, B
, inv
->NbRows
- 1, inv
->NbColumns
- 1);
67 long r
= LLL(det2
, B
, U
, options
->LLL_a
, options
->LLL_b
);
71 for (int i
= 1; i
< B
.NumRows(); ++i
) {
83 Vector
*z
= Vector_Alloc(U
[index
].length()+1);
85 zz2values(U
[index
], z
->p
);
86 value_set_si(z
->p
[U
[index
].length()], 0);
88 Polyhedron
*C
= poly();
90 for (i
= 0; i
< lambda
.length(); ++i
)
93 if (i
== lambda
.length()) {
96 value_set_si(tmp
, -1);
97 Vector_Scale(z
->p
, z
->p
, tmp
, z
->Size
-1);
104 Polyhedron_Free(Cone
);
112 Matrix
*M
= Matrix_Alloc(Rays
->NbRows
+1, Rays
->NbColumns
+1);
113 for (int i
= 0; i
< Rays
->NbRows
; ++i
) {
114 Vector_Copy(Rays
->p
[i
], M
->p
[i
]+1, Rays
->NbColumns
);
115 value_set_si(M
->p
[i
][0], 1);
117 Vector_Set(M
->p
[Rays
->NbRows
]+1, 0, Rays
->NbColumns
-1);
118 value_set_si(M
->p
[Rays
->NbRows
][0], 1);
119 value_set_si(M
->p
[Rays
->NbRows
][Rays
->NbColumns
], 1);
120 Cone
= Rays2Polyhedron(M
, M
->NbRows
+1);
121 assert(Cone
->NbConstraints
== Cone
->NbRays
);
133 static void decompose(const signed_cone
& sc
, signed_cone_consumer
& scc
,
134 barvinok_options
*options
)
136 vector
<cone
*> nonuni
;
137 cone
* c
= new cone(sc
);
145 options
->stats
.unimodular_cones
++;
155 int closed
[c
->Rays
->NbRows
-1];
156 while (!nonuni
.empty()) {
159 Vector
* v
= c
->short_vector(lambda
, options
);
160 for (int i
= 0; i
< c
->Rays
->NbRows
- 1; ++i
) {
163 Matrix
* M
= Matrix_Copy(c
->Rays
);
164 Vector_Copy(v
->p
, M
->p
[i
], v
->Size
);
165 cone
* pc
= new cone(M
);
167 bool same_sign
= sign(c
->det
) * sign(pc
->det
) > 0;
168 for (int j
= 0; j
< c
->Rays
->NbRows
- 1; ++j
) {
170 closed
[j
] = c
->closed
[j
];
173 closed
[j
] = c
->closed
[j
];
175 closed
[j
] = !c
->closed
[j
];
176 } else if (sign(lambda
[i
]) * sign(lambda
[j
]) > 0) {
177 if (c
->closed
[i
] == c
->closed
[j
])
180 closed
[j
] = c
->closed
[j
];
182 closed
[j
] = c
->closed
[i
] && c
->closed
[j
];
184 pc
->set_closed(closed
);
186 assert (pc
->det
!= 0);
187 if (abs(pc
->det
) > 1) {
188 assert(abs(pc
->det
) < abs(c
->det
));
189 nonuni
.push_back(pc
);
192 options
->stats
.unimodular_cones
++;
194 scc
.handle(signed_cone(pc
->poly(), sign(pc
->det
) * s
,
197 scc
.handle(signed_cone(pc
->poly(), sign(pc
->det
) * s
));
202 while (!nonuni
.empty()) {
219 struct polar_signed_cone_consumer
: public signed_cone_consumer
{
220 signed_cone_consumer
& scc
;
221 polar_signed_cone_consumer(signed_cone_consumer
& scc
) : scc(scc
) {}
222 virtual void handle(const signed_cone
& sc
) {
223 Polyhedron_Polarize(sc
.C
);
228 static void polar_decompose(Polyhedron
*cone
, signed_cone_consumer
& scc
,
229 barvinok_options
*options
)
231 POL_ENSURE_VERTICES(cone
);
232 Polyhedron_Polarize(cone
);
233 if (cone
->NbRays
- 1 != cone
->Dimension
) {
234 Polyhedron
*tmp
= cone
;
235 cone
= triangulate_cone(cone
, options
->MaxRays
);
236 Polyhedron_Free(tmp
);
238 polar_signed_cone_consumer
pssc(scc
);
240 for (Polyhedron
*Polar
= cone
; Polar
; Polar
= Polar
->next
)
241 decompose(signed_cone(Polar
, 1), pssc
, options
);
249 static void primal_decompose(Polyhedron
*cone
, signed_cone_consumer
& scc
,
250 barvinok_options
*options
)
252 POL_ENSURE_VERTICES(cone
);
254 if (cone
->NbRays
- 1 == cone
->Dimension
)
257 parts
= triangulate_cone(cone
, options
->MaxRays
);
258 int closed
[cone
->Dimension
];
259 Vector
*average
= NULL
;
263 average
= Vector_Alloc(cone
->Dimension
);
264 for (int i
= 0; i
< cone
->NbRays
; ++i
) {
265 if (value_notzero_p(cone
->Ray
[i
][1+cone
->Dimension
]))
267 Vector_Add(average
->p
, cone
->Ray
[i
]+1, average
->p
, cone
->Dimension
);
271 for (Polyhedron
*simple
= parts
; simple
; simple
= simple
->next
) {
272 for (int i
= 0, r
= 0; r
< simple
->NbRays
; ++r
) {
273 if (value_notzero_p(simple
->Ray
[r
][1+simple
->Dimension
]))
275 if (simple
== cone
) {
279 for (f
= 0; f
< simple
->NbConstraints
; ++f
) {
280 Inner_Product(simple
->Ray
[r
]+1, simple
->Constraint
[f
]+1,
281 simple
->Dimension
, &tmp
);
282 if (value_notzero_p(tmp
))
285 assert(f
< simple
->NbConstraints
);
286 Inner_Product(simple
->Constraint
[f
]+1, average
->p
,
287 simple
->Dimension
, &tmp
);
288 if (value_notzero_p(tmp
))
289 closed
[i
] = value_pos_p(tmp
);
291 int p
= First_Non_Zero(simple
->Constraint
[f
]+1,
293 closed
[i
] = value_pos_p(simple
->Constraint
[f
][1+p
]);
298 decompose(signed_cone(simple
, 1, closed
), scc
, options
);
304 Vector_Free(average
);
311 Vector_Free(average
);
317 void barvinok_decompose(Polyhedron
*C
, signed_cone_consumer
& scc
,
318 barvinok_options
*options
)
321 primal_decompose(C
, scc
, options
);
323 polar_decompose(C
, scc
, options
);
326 void vertex_decomposer::decompose_at_vertex(Param_Vertices
*V
, int _i
,
327 barvinok_options
*options
)
329 Polyhedron
*C
= supporting_cone_p(P
, V
);
333 barvinok_decompose(C
, scc
, options
);
336 struct posneg_collector
: public signed_cone_consumer
{
337 posneg_collector(Polyhedron
*pos
, Polyhedron
*neg
) : pos(pos
), neg(neg
) {}
338 virtual void handle(const signed_cone
& sc
) {
339 Polyhedron
*p
= Polyhedron_Copy(sc
.C
);
353 * Barvinok's Decomposition of a simplicial cone
355 * Returns two lists of polyhedra
357 void barvinok_decompose(Polyhedron
*C
, Polyhedron
**ppos
, Polyhedron
**pneg
)
359 barvinok_options
*options
= barvinok_options_new_with_defaults();
360 posneg_collector
pc(*ppos
, *pneg
);
361 POL_ENSURE_VERTICES(C
);
362 decompose(signed_cone(C
, 1), pc
, options
);