2 #include <barvinok/polylib.h>
3 #include <barvinok/barvinok.h>
4 #include <barvinok/options.h>
5 #include <barvinok/util.h>
6 #include "reduce_domain.h"
7 #include "param_util.h"
11 #define ALLOC(type) (type*)malloc(sizeof(type))
12 #define ALLOCN(type,n) (type*)malloc((n) * sizeof(type))
14 /* Computes an evalue representation of a coordinate
17 static evalue
*vertex2evalue(Value
*vertex
, int nparam
)
19 return affine2evalue(vertex
, vertex
[nparam
+1], nparam
);
22 static void matrix_print(evalue
***matrix
, int dim
, int *cols
,
23 const char **param_names
) __attribute__((unused
));
24 static void matrix_print(evalue
***matrix
, int dim
, int *cols
,
25 const char **param_names
)
29 for (i
= 0; i
< dim
; ++i
)
30 for (j
= 0; j
< dim
; ++j
) {
31 int k
= cols
? cols
[j
] : j
;
32 fprintf(stderr
, "%d %d: ", i
, j
);
33 print_evalue(stderr
, matrix
[i
][k
], param_names
);
34 fprintf(stderr
, "\n");
38 /* Compute determinant using Laplace's formula.
39 * In particular, the determinant is expanded along the last row.
40 * The cols array is a list of columns that remain in the currect submatrix.
42 static evalue
*determinant_cols(evalue
***matrix
, int dim
, int *cols
)
52 evalue_copy(det
, matrix
[0][cols
[0]]);
57 evalue_set_si(&mone
, -1, 1);
59 newcols
= ALLOCN(int, dim
-1);
60 for (j
= 1; j
< dim
; ++j
)
61 newcols
[j
-1] = cols
[j
];
62 for (j
= 0; j
< dim
; ++j
) {
64 newcols
[j
-1] = cols
[j
-1];
65 tmp
= determinant_cols(matrix
, dim
-1, newcols
);
66 emul(matrix
[dim
-1][cols
[j
]], tmp
);
77 free_evalue_refs(&mone
);
82 static evalue
*determinant(evalue
***matrix
, int dim
)
85 int *cols
= ALLOCN(int, dim
);
88 for (i
= 0; i
< dim
; ++i
)
91 det
= determinant_cols(matrix
, dim
, cols
);
98 /* Compute the facet of P that saturates constraint c.
100 static Polyhedron
*facet(Polyhedron
*P
, int c
, unsigned MaxRays
)
103 Vector
*row
= Vector_Alloc(1+P
->Dimension
+1);
104 Vector_Copy(P
->Constraint
[c
]+1, row
->p
+1, P
->Dimension
+1);
105 F
= AddConstraints(row
->p
, 1, P
, MaxRays
);
110 /* Substitute parameters by the corresponding element in subs
112 static evalue
*evalue_substitute_new(evalue
*e
, evalue
**subs
)
118 if (value_notzero_p(e
->d
)) {
124 assert(e
->x
.p
->type
== polynomial
);
127 for (i
= e
->x
.p
->size
-1; i
> 0; --i
) {
128 c
= evalue_substitute_new(&e
->x
.p
->arr
[i
], subs
);
131 emul(subs
[e
->x
.p
->pos
-1], res
);
133 c
= evalue_substitute_new(&e
->x
.p
->arr
[0], subs
);
140 struct parameter_point
{
145 struct parameter_point
*parameter_point_new(unsigned nparam
)
147 struct parameter_point
*point
= ALLOC(struct parameter_point
);
148 point
->coord
= Vector_Alloc(nparam
+1);
153 evalue
**parameter_point_evalue(struct parameter_point
*point
)
156 unsigned nparam
= point
->coord
->Size
-1;
161 point
->e
= ALLOCN(evalue
*, nparam
);
162 for (j
= 0; j
< nparam
; ++j
) {
163 point
->e
[j
] = ALLOC(evalue
);
164 value_init(point
->e
[j
]->d
);
165 evalue_set(point
->e
[j
], point
->coord
->p
[j
], point
->coord
->p
[nparam
]);
171 void parameter_point_free(struct parameter_point
*point
)
174 unsigned nparam
= point
->coord
->Size
-1;
176 Vector_Free(point
->coord
);
179 for (i
= 0; i
< nparam
; ++i
)
180 evalue_free(point
->e
[i
]);
186 /* Computes point in pameter space where polyhedron is non-empty.
188 static struct parameter_point
*non_empty_point(Param_Domain
*D
)
190 unsigned nparam
= D
->Domain
->Dimension
;
191 struct parameter_point
*point
;
194 v
= inner_point(D
->Domain
);
195 point
= parameter_point_new(nparam
);
196 Vector_Copy(v
->p
+1, point
->coord
->p
, nparam
+1);
202 static Matrix
*barycenter(Param_Polyhedron
*PP
, Param_Domain
*D
)
205 Matrix
*center
= NULL
;
215 FORALL_PVertex_in_ParamPolyhedron(V
, D
, PP
)
218 center
= Matrix_Copy(V
->Vertex
);
219 nparam
= center
->NbColumns
- 2;
221 for (i
= 0; i
< center
->NbRows
; ++i
) {
222 value_assign(fc
, center
->p
[i
][nparam
+1]);
223 value_lcm(center
->p
[i
][nparam
+1],
224 fc
, V
->Vertex
->p
[i
][nparam
+1]);
225 value_division(fc
, center
->p
[i
][nparam
+1], fc
);
226 value_division(fv
, center
->p
[i
][nparam
+1],
227 V
->Vertex
->p
[i
][nparam
+1]);
228 Vector_Combine(center
->p
[i
], V
->Vertex
->p
[i
], center
->p
[i
],
232 END_FORALL_PVertex_in_ParamPolyhedron
;
237 value_set_si(denom
, nbV
);
238 for (i
= 0; i
< center
->NbRows
; ++i
) {
239 value_multiply(center
->p
[i
][nparam
+1], center
->p
[i
][nparam
+1], denom
);
240 Vector_Normalize(center
->p
[i
], nparam
+2);
247 static Matrix
*triangulation_vertex(Param_Polyhedron
*PP
, Param_Domain
*D
,
252 FORALL_PVertex_in_ParamPolyhedron(V
, D
, PP
)
254 END_FORALL_PVertex_in_ParamPolyhedron
;
260 /* Compute dim! times the volume of polyhedron F in Param_Domain D.
261 * If F is a simplex, then the volume is computed of a recursive pyramid
262 * over F with the points already in matrix.
263 * Otherwise, the barycenter of F is added to matrix and the function
264 * is called recursively on the facets of F.
266 * The first row of matrix contain the _negative_ of the first point.
267 * The remaining rows of matrix contain the distance of the corresponding
268 * point to the first point.
270 static evalue
*volume_in_domain(Param_Polyhedron
*PP
, Param_Domain
*D
,
271 unsigned dim
, evalue
***matrix
,
272 struct parameter_point
*point
,
273 int row
, Polyhedron
*F
,
274 struct barvinok_options
*options
);
276 static evalue
*volume_triangulate(Param_Polyhedron
*PP
, Param_Domain
*D
,
277 unsigned dim
, evalue
***matrix
,
278 struct parameter_point
*point
,
279 int row
, Polyhedron
*F
,
280 struct barvinok_options
*options
)
287 unsigned cut_MaxRays
= options
->MaxRays
;
288 unsigned nparam
= PP
->V
->Vertex
->NbColumns
-2;
291 POL_UNSET(cut_MaxRays
, POL_INTEGER
);
294 evalue_set_si(&mone
, -1, 1);
296 if (options
->approx
->volume_triangulate
== BV_VOL_BARYCENTER
)
297 center
= barycenter(PP
, D
);
299 center
= triangulation_vertex(PP
, D
, F
);
300 for (j
= 0; j
< dim
; ++j
)
301 matrix
[row
][j
] = vertex2evalue(center
->p
[j
], center
->NbColumns
- 2);
302 if (options
->approx
->volume_triangulate
== BV_VOL_BARYCENTER
)
305 v
= Vector_Alloc(1+nparam
+1);
308 for (j
= 0; j
< dim
; ++j
)
309 emul(&mone
, matrix
[row
][j
]);
311 for (j
= 0; j
< dim
; ++j
)
312 eadd(matrix
[0][j
], matrix
[row
][j
]);
316 POL_ENSURE_FACETS(F
);
317 for (j
= F
->NbEq
; j
< F
->NbConstraints
; ++j
) {
320 if (First_Non_Zero(F
->Constraint
[j
]+1, dim
) == -1)
322 if (options
->approx
->volume_triangulate
!= BV_VOL_BARYCENTER
) {
323 Param_Inner_Product(F
->Constraint
[j
], center
, v
->p
);
324 if (First_Non_Zero(v
->p
+1, nparam
+1) == -1)
327 FF
= facet(F
, j
, options
->MaxRays
);
328 FD
= Param_Polyhedron_Facet(PP
, D
, F
->Constraint
[j
]);
329 tmp
= volume_in_domain(PP
, FD
, dim
, matrix
, point
,
338 Param_Domain_Free(FD
);
341 if (options
->approx
->volume_triangulate
!= BV_VOL_BARYCENTER
)
344 for (j
= 0; j
< dim
; ++j
)
345 evalue_free(matrix
[row
][j
]);
347 free_evalue_refs(&mone
);
351 static evalue
*volume_simplex(Param_Polyhedron
*PP
, Param_Domain
*D
,
352 unsigned dim
, evalue
***matrix
,
353 struct parameter_point
*point
,
354 int row
, struct barvinok_options
*options
)
361 options
->stats
->volume_simplices
++;
364 evalue_set_si(&mone
, -1, 1);
367 FORALL_PVertex_in_ParamPolyhedron(V
, D
, PP
) /* _ix, _bx internal counters */
368 for (j
= 0; j
< dim
; ++j
) {
369 matrix
[i
][j
] = vertex2evalue(V
->Vertex
->p
[j
],
370 V
->Vertex
->NbColumns
- 2);
372 emul(&mone
, matrix
[i
][j
]);
374 eadd(matrix
[0][j
], matrix
[i
][j
]);
377 END_FORALL_PVertex_in_ParamPolyhedron
;
379 vol
= determinant(matrix
+1, dim
);
381 val
= evalue_substitute_new(vol
, parameter_point_evalue(point
));
383 assert(value_notzero_p(val
->d
));
384 assert(value_notzero_p(val
->x
.n
));
385 if (value_neg_p(val
->x
.n
))
390 for (i
= row
; i
< dim
+1; ++i
)
391 for (j
= 0; j
< dim
; ++j
)
392 evalue_free(matrix
[i
][j
]);
394 free_evalue_refs(&mone
);
399 static evalue
*volume_triangulate_lift(Param_Polyhedron
*PP
, Param_Domain
*D
,
400 unsigned dim
, evalue
***matrix
,
401 struct parameter_point
*point
,
402 int row
, struct barvinok_options
*options
)
404 const static int MAX_TRY
=10;
409 Matrix
*FixedRays
, *M
;
417 nv
= (PP
->nbV
- 1)/(8*sizeof(int)) + 1;
418 SD
.F
= ALLOCN(unsigned, nv
);
420 FixedRays
= Matrix_Alloc(PP
->nbV
+1, 1+dim
+2);
422 FORALL_PVertex_in_ParamPolyhedron(V
, D
, PP
)
423 unsigned nparam
= V
->Vertex
->NbColumns
- 2;
424 Param_Vertex_Common_Denominator(V
);
425 for (i
= 0; i
< V
->Vertex
->NbRows
; ++i
) {
426 value_multiply(FixedRays
->p
[nbV
][1+i
], V
->Vertex
->p
[i
][nparam
],
427 point
->coord
->p
[nparam
]);
428 Inner_Product(V
->Vertex
->p
[i
], point
->coord
->p
, nparam
,
429 &FixedRays
->p
[nbV
][1+dim
]);
430 value_addto(FixedRays
->p
[nbV
][1+i
], FixedRays
->p
[nbV
][1+i
],
431 FixedRays
->p
[nbV
][1+dim
]);
433 value_multiply(FixedRays
->p
[nbV
][1+dim
+1], V
->Vertex
->p
[0][nparam
+1],
434 point
->coord
->p
[nparam
]);
435 value_set_si(FixedRays
->p
[nbV
][0], 1);
437 END_FORALL_PVertex_in_ParamPolyhedron
;
438 value_set_si(FixedRays
->p
[nbV
][0], 1);
439 value_set_si(FixedRays
->p
[nbV
][1+dim
], 1);
440 FixedRays
->NbRows
= nbV
+1;
445 /* Usually vol should still be NULL */
451 assert(t
<= MAX_TRY
);
454 for (i
= 0; i
< nbV
; ++i
)
455 value_set_si(FixedRays
->p
[i
][1+dim
], random_int((t
+1)*dim
*nbV
)+1);
457 M
= Matrix_Copy(FixedRays
);
458 L
= Rays2Polyhedron(M
, options
->MaxRays
);
461 POL_ENSURE_FACETS(L
);
462 for (i
= 0; i
< L
->NbConstraints
; ++i
) {
464 /* Ignore perpendicular facets, i.e., facets with 0 z-coordinate */
465 if (value_negz_p(L
->Constraint
[i
][1+dim
]))
468 memset(SD
.F
, 0, nv
* sizeof(unsigned));
471 FORALL_PVertex_in_ParamPolyhedron(V
, D
, PP
) /* _ix, _bx internal */
472 Inner_Product(FixedRays
->p
[nbV
]+1, L
->Constraint
[i
]+1, dim
+2, &tmp
);
473 if (value_zero_p(tmp
)) {
480 END_FORALL_PVertex_in_ParamPolyhedron
;
481 assert(r
== (dim
-row
)+1);
483 s
= volume_simplex(PP
, &SD
, dim
, matrix
, point
, row
, options
);
492 Matrix_Free(FixedRays
);
499 static evalue
*volume_in_domain(Param_Polyhedron
*PP
, Param_Domain
*D
,
500 unsigned dim
, evalue
***matrix
,
501 struct parameter_point
*point
,
502 int row
, Polyhedron
*F
,
503 struct barvinok_options
*options
)
512 FORALL_PVertex_in_ParamPolyhedron(V
, D
, PP
)
514 END_FORALL_PVertex_in_ParamPolyhedron
;
516 if (nbV
> (dim
-row
) + 1) {
517 if (options
->approx
->volume_triangulate
== BV_VOL_LIFT
)
518 vol
= volume_triangulate_lift(PP
, D
, dim
, matrix
, point
,
521 vol
= volume_triangulate(PP
, D
, dim
, matrix
, point
,
524 assert(nbV
== (dim
-row
) + 1);
525 vol
= volume_simplex(PP
, D
, dim
, matrix
, point
, row
, options
);
531 evalue
* Param_Polyhedron_Volume(Polyhedron
*P
, Polyhedron
* C
,
532 struct barvinok_options
*options
)
535 unsigned nparam
= C
->Dimension
;
536 unsigned nvar
= P
->Dimension
- C
->Dimension
;
537 Param_Polyhedron
*PP
;
543 struct evalue_section
*s
;
547 if (options
->approx
->approximation
== BV_APPROX_SIGN_NONE
)
550 if (options
->approx
->approximation
!= BV_APPROX_SIGN_APPROX
) {
551 int pa
= options
->approx
->approximation
;
552 assert(pa
== BV_APPROX_SIGN_UPPER
|| pa
== BV_APPROX_SIGN_LOWER
);
554 P
= Polyhedron_Flate(P
, nparam
, pa
== BV_APPROX_SIGN_UPPER
,
557 /* Don't deflate/inflate again (on this polytope) */
558 options
->approx
->approximation
= BV_APPROX_SIGN_APPROX
;
559 vol
= barvinok_enumerate_with_options(P
, C
, options
);
560 options
->approx
->approximation
= pa
;
566 TC
= true_context(P
, C
, options
->MaxRays
);
568 MaxRays
= options
->MaxRays
;
569 POL_UNSET(options
->MaxRays
, POL_INTEGER
);
572 Factorial(nvar
, &fact
);
574 PP
= Polyhedron2Param_Polyhedron(P
, C
, options
);
576 for (nd
= 0, D
= PP
->D
; D
; ++nd
, D
= D
->next
);
577 s
= ALLOCN(struct evalue_section
, nd
);
579 matrix
= ALLOCN(evalue
**, nvar
+1);
580 for (i
= 0; i
< nvar
+1; ++i
)
581 matrix
[i
] = ALLOCN(evalue
*, nvar
);
583 FORALL_REDUCED_DOMAIN(PP
, TC
, nd
, options
, i
, D
, rVD
)
585 struct parameter_point
*point
;
587 CA
= align_context(D
->Domain
, P
->Dimension
, MaxRays
);
588 F
= DomainIntersection(P
, CA
, options
->MaxRays
);
591 point
= non_empty_point(D
);
593 s
[i
].E
= volume_in_domain(PP
, D
, nvar
, matrix
, point
, 0, F
, options
);
595 parameter_point_free(point
);
596 evalue_div(s
[i
].E
, fact
);
597 END_FORALL_REDUCED_DOMAIN
598 options
->MaxRays
= MaxRays
;
601 vol
= evalue_from_section_array(s
, nd
);
604 for (i
= 0; i
< nvar
+1; ++i
)
607 Param_Polyhedron_Free(PP
);