1 #include <barvinok/polylib.h>
2 #include <barvinok/barvinok.h>
3 #include <barvinok/options.h>
4 #include <barvinok/util.h>
5 #include "reduce_domain.h"
9 #define ALLOC(type) (type*)malloc(sizeof(type))
10 #define ALLOCN(type,n) (type*)malloc((n) * sizeof(type))
12 /* Computes an evalue representation of a coordinate
15 static evalue
*vertex2evalue(Value
*vertex
, int nparam
)
17 return affine2evalue(vertex
, vertex
[nparam
+1], nparam
);
20 static void matrix_print(evalue
***matrix
, int dim
, int *cols
,
25 for (i
= 0; i
< dim
; ++i
)
26 for (j
= 0; j
< dim
; ++j
) {
27 int k
= cols
? cols
[j
] : j
;
28 fprintf(stderr
, "%d %d: ", i
, j
);
29 print_evalue(stderr
, matrix
[i
][k
], param_names
);
30 fprintf(stderr
, "\n");
34 /* Compute determinant using Laplace's formula.
35 * In particular, the determinant is expanded along the last row.
36 * The cols array is a list of columns that remain in the currect submatrix.
38 static evalue
*determinant_cols(evalue
***matrix
, int dim
, int *cols
)
46 evalue_copy(det
, matrix
[0][cols
[0]]);
51 evalue_set_si(&mone
, -1, 1);
54 int *newcols
= ALLOCN(int, dim
-1);
55 for (j
= 1; j
< dim
; ++j
)
56 newcols
[j
-1] = cols
[j
];
57 for (j
= 0; j
< dim
; ++j
) {
59 newcols
[j
-1] = cols
[j
-1];
60 tmp
= determinant_cols(matrix
, dim
-1, newcols
);
61 emul(matrix
[dim
-1][cols
[j
]], tmp
);
68 free_evalue_refs(tmp
);
73 free_evalue_refs(&mone
);
78 static evalue
*determinant(evalue
***matrix
, int dim
)
81 int *cols
= ALLOCN(int, dim
);
84 for (i
= 0; i
< dim
; ++i
)
87 det
= determinant_cols(matrix
, dim
, cols
);
94 /* Compute the facet of P that saturates constraint c.
96 static Polyhedron
*facet(Polyhedron
*P
, int c
, unsigned MaxRays
)
99 Vector
*row
= Vector_Alloc(1+P
->Dimension
+1);
100 Vector_Copy(P
->Constraint
[c
]+1, row
->p
+1, P
->Dimension
+1);
101 F
= AddConstraints(row
->p
, 1, P
, MaxRays
);
106 /* Compute a dummy Param_Domain that contains all vertices of Param_Domain D
107 * (which contains the vertices of P) that lie on the facet obtain by
108 * saturating constraint c of P
110 static Param_Domain
*face_vertices(Param_Polyhedron
*PP
, Param_Domain
*D
,
111 Polyhedron
*P
, int c
)
115 Param_Domain
*FD
= ALLOC(Param_Domain
);
119 nv
= (PP
->nbV
- 1)/(8*sizeof(int)) + 1;
120 FD
->F
= ALLOCN(unsigned, nv
);
121 memset(FD
->F
, 0, nv
* sizeof(unsigned));
123 FORALL_PVertex_in_ParamPolyhedron(V
, D
, PP
) /* _i, _ix, _bx internal counters */
125 unsigned char *supporting
= supporting_constraints(P
, V
, &n
);
129 END_FORALL_PVertex_in_ParamPolyhedron
;
134 /* Substitute parameters by the corresponding element in subs
136 static evalue
*evalue_substitute_new(evalue
*e
, evalue
**subs
)
142 if (value_notzero_p(e
->d
)) {
148 assert(e
->x
.p
->type
== polynomial
);
151 for (i
= e
->x
.p
->size
-1; i
> 0; --i
) {
152 c
= evalue_substitute_new(&e
->x
.p
->arr
[i
], subs
);
156 emul(subs
[e
->x
.p
->pos
-1], res
);
158 c
= evalue_substitute_new(&e
->x
.p
->arr
[0], subs
);
166 /* Plug in the parametric vertex V in the constraint constraint.
167 * The result is stored in row, with the denominator in position 0.
169 static void Param_Inner_Product(Value
*constraint
, Matrix
*Vertex
,
172 unsigned nparam
= Vertex
->NbColumns
- 2;
173 unsigned nvar
= Vertex
->NbRows
;
177 value_set_si(row
[0], 1);
178 Vector_Set(row
+1, 0, nparam
+1);
183 for (j
= 0 ; j
< nvar
; ++j
) {
184 value_set_si(tmp
, 1);
185 value_assign(tmp2
, constraint
[1+j
]);
186 if (value_ne(row
[0], Vertex
->p
[j
][nparam
+1])) {
187 value_assign(tmp
, row
[0]);
188 value_lcm(row
[0], Vertex
->p
[j
][nparam
+1], &row
[0]);
189 value_division(tmp
, row
[0], tmp
);
190 value_multiply(tmp2
, tmp2
, row
[0]);
191 value_division(tmp2
, tmp2
, Vertex
->p
[j
][nparam
+1]);
193 Vector_Combine(row
+1, Vertex
->p
[j
], row
+1, tmp
, tmp2
, nparam
+1);
195 value_set_si(tmp
, 1);
196 Vector_Combine(row
+1, constraint
+1+nvar
, row
+1, tmp
, row
[0], nparam
+1);
202 struct parameter_point
{
207 struct parameter_point
*parameter_point_new(unsigned nparam
)
209 struct parameter_point
*point
= ALLOC(struct parameter_point
);
210 point
->coord
= Vector_Alloc(nparam
+1);
215 evalue
**parameter_point_evalue(struct parameter_point
*point
)
218 unsigned nparam
= point
->coord
->Size
-1;
223 point
->e
= ALLOCN(evalue
*, nparam
);
224 for (j
= 0; j
< nparam
; ++j
) {
225 point
->e
[j
] = ALLOC(evalue
);
226 value_init(point
->e
[j
]->d
);
227 evalue_set(point
->e
[j
], point
->coord
->p
[j
], point
->coord
->p
[nparam
]);
233 void parameter_point_free(struct parameter_point
*point
)
236 unsigned nparam
= point
->coord
->Size
-1;
238 Vector_Free(point
->coord
);
241 for (i
= 0; i
< nparam
; ++i
) {
242 free_evalue_refs(point
->e
[i
]);
250 /* Computes point in pameter space where polyhedron is non-empty.
252 static struct parameter_point
*non_empty_point(Param_Domain
*D
)
254 unsigned nparam
= D
->Domain
->Dimension
;
255 struct parameter_point
*point
;
258 v
= inner_point(D
->Domain
);
259 point
= parameter_point_new(nparam
);
260 Vector_Copy(v
->p
+1, point
->coord
->p
, nparam
+1);
266 static Matrix
*barycenter(Param_Polyhedron
*PP
, Param_Domain
*D
)
269 Matrix
*center
= NULL
;
279 FORALL_PVertex_in_ParamPolyhedron(V
, D
, PP
)
282 center
= Matrix_Copy(V
->Vertex
);
283 nparam
= center
->NbColumns
- 2;
285 for (i
= 0; i
< center
->NbRows
; ++i
) {
286 value_assign(fc
, center
->p
[i
][nparam
+1]);
287 value_lcm(fc
, V
->Vertex
->p
[i
][nparam
+1],
288 ¢er
->p
[i
][nparam
+1]);
289 value_division(fc
, center
->p
[i
][nparam
+1], fc
);
290 value_division(fv
, center
->p
[i
][nparam
+1],
291 V
->Vertex
->p
[i
][nparam
+1]);
292 Vector_Combine(center
->p
[i
], V
->Vertex
->p
[i
], center
->p
[i
],
296 END_FORALL_PVertex_in_ParamPolyhedron
;
301 value_set_si(denom
, nbV
);
302 for (i
= 0; i
< center
->NbRows
; ++i
) {
303 value_multiply(center
->p
[i
][nparam
+1], center
->p
[i
][nparam
+1], denom
);
304 Vector_Normalize(center
->p
[i
], nparam
+2);
311 /* Compute dim! times the volume of polyhedron F in Param_Domain D.
312 * If F is a simplex, then the volume is computed of a recursive pyramid
313 * over F with the points already in matrix.
314 * Otherwise, the barycenter of F is added to matrix and the function
315 * is called recursively on the facets of F.
317 * The first row of matrix contain the _negative_ of the first point.
318 * The remaining rows of matrix contain the distance of the corresponding
319 * point to the first point.
321 static evalue
*volume_in_domain(Param_Polyhedron
*PP
, Param_Domain
*D
,
322 unsigned dim
, evalue
***matrix
,
323 struct parameter_point
*point
,
324 int row
, Polyhedron
*F
,
325 struct barvinok_options
*options
);
327 static evalue
*volume_triangulate(Param_Polyhedron
*PP
, Param_Domain
*D
,
328 unsigned dim
, evalue
***matrix
,
329 struct parameter_point
*point
,
330 int row
, Polyhedron
*F
,
331 struct barvinok_options
*options
)
338 unsigned cut_MaxRays
= options
->MaxRays
;
339 unsigned nparam
= D
->Domain
->Dimension
;
341 POL_UNSET(cut_MaxRays
, POL_INTEGER
);
344 evalue_set_si(&mone
, -1, 1);
346 center
= barycenter(PP
, D
);
347 for (j
= 0; j
< dim
; ++j
)
348 matrix
[row
][j
] = vertex2evalue(center
->p
[j
], center
->NbColumns
- 2);
351 for (j
= 0; j
< dim
; ++j
)
352 emul(&mone
, matrix
[row
][j
]);
354 for (j
= 0; j
< dim
; ++j
)
355 eadd(matrix
[0][j
], matrix
[row
][j
]);
359 POL_ENSURE_FACETS(F
);
360 for (j
= F
->NbEq
; j
< F
->NbConstraints
; ++j
) {
363 if (First_Non_Zero(F
->Constraint
[j
]+1, dim
) == -1)
365 FF
= facet(F
, j
, options
->MaxRays
);
366 FD
= face_vertices(PP
, D
, F
, j
);
367 tmp
= volume_in_domain(PP
, FD
, dim
, matrix
, point
,
373 free_evalue_refs(tmp
);
377 Param_Domain_Free(FD
);
382 for (j
= 0; j
< dim
; ++j
) {
383 free_evalue_refs(matrix
[row
][j
]);
384 free(matrix
[row
][j
]);
387 free_evalue_refs(&mone
);
391 static evalue
*volume_simplex(Param_Polyhedron
*PP
, Param_Domain
*D
,
392 unsigned dim
, evalue
***matrix
,
393 struct parameter_point
*point
,
394 int row
, unsigned MaxRays
)
402 evalue_set_si(&mone
, -1, 1);
405 FORALL_PVertex_in_ParamPolyhedron(V
, D
, PP
) /* _ix, _bx internal counters */
406 for (j
= 0; j
< dim
; ++j
) {
407 matrix
[i
][j
] = vertex2evalue(V
->Vertex
->p
[j
],
408 V
->Vertex
->NbColumns
- 2);
410 emul(&mone
, matrix
[i
][j
]);
412 eadd(matrix
[0][j
], matrix
[i
][j
]);
415 END_FORALL_PVertex_in_ParamPolyhedron
;
417 vol
= determinant(matrix
+1, dim
);
419 val
= evalue_substitute_new(vol
, parameter_point_evalue(point
));
421 assert(value_notzero_p(val
->d
));
422 assert(value_notzero_p(val
->x
.n
));
423 if (value_neg_p(val
->x
.n
))
426 free_evalue_refs(val
);
429 for (i
= row
; i
< dim
+1; ++i
) {
430 for (j
= 0; j
< dim
; ++j
) {
431 free_evalue_refs(matrix
[i
][j
]);
436 free_evalue_refs(&mone
);
441 static evalue
*volume_triangulate_lift(Param_Polyhedron
*PP
, Param_Domain
*D
,
442 unsigned dim
, evalue
***matrix
,
443 struct parameter_point
*point
,
444 int row
, unsigned MaxRays
)
446 const static int MAX_TRY
=10;
451 Matrix
*FixedRays
, *M
;
459 nv
= (PP
->nbV
- 1)/(8*sizeof(int)) + 1;
460 SD
.F
= ALLOCN(unsigned, nv
);
462 FixedRays
= Matrix_Alloc(PP
->nbV
+1, 1+dim
+2);
464 FORALL_PVertex_in_ParamPolyhedron(V
, D
, PP
)
465 unsigned nparam
= V
->Vertex
->NbColumns
- 2;
466 Param_Vertex_Common_Denominator(V
);
467 for (i
= 0; i
< V
->Vertex
->NbRows
; ++i
) {
468 value_multiply(FixedRays
->p
[nbV
][1+i
], V
->Vertex
->p
[i
][nparam
],
469 point
->coord
->p
[nparam
]);
470 Inner_Product(V
->Vertex
->p
[i
], point
->coord
->p
, nparam
,
471 &FixedRays
->p
[nbV
][1+dim
]);
472 value_addto(FixedRays
->p
[nbV
][1+i
], FixedRays
->p
[nbV
][1+i
],
473 FixedRays
->p
[nbV
][1+dim
]);
475 value_multiply(FixedRays
->p
[nbV
][1+dim
+1], V
->Vertex
->p
[0][nparam
+1],
476 point
->coord
->p
[nparam
]);
477 value_set_si(FixedRays
->p
[nbV
][0], 1);
479 END_FORALL_PVertex_in_ParamPolyhedron
;
480 value_set_si(FixedRays
->p
[nbV
][0], 1);
481 value_set_si(FixedRays
->p
[nbV
][1+dim
], 1);
482 FixedRays
->NbRows
= nbV
+1;
487 /* Usually vol should still be NULL */
489 free_evalue_refs(vol
);
495 assert(t
<= MAX_TRY
);
498 for (i
= 0; i
< nbV
; ++i
)
499 value_set_si(FixedRays
->p
[i
][1+dim
], random_int((t
+1)*dim
*nbV
)+1);
501 M
= Matrix_Copy(FixedRays
);
502 L
= Rays2Polyhedron(M
, MaxRays
);
505 POL_ENSURE_FACETS(L
);
506 for (i
= 0; i
< L
->NbConstraints
; ++i
) {
508 /* Ignore perpendicular facets, i.e., facets with 0 z-coordinate */
509 if (value_negz_p(L
->Constraint
[i
][1+dim
]))
512 memset(SD
.F
, 0, nv
* sizeof(unsigned));
515 FORALL_PVertex_in_ParamPolyhedron(V
, D
, PP
) /* _ix, _bx internal */
516 Inner_Product(FixedRays
->p
[nbV
]+1, L
->Constraint
[i
]+1, dim
+2, &tmp
);
517 if (value_zero_p(tmp
)) {
524 END_FORALL_PVertex_in_ParamPolyhedron
;
525 assert(r
== (dim
-row
)+1);
527 s
= volume_simplex(PP
, &SD
, dim
, matrix
, point
, row
, MaxRays
);
537 Matrix_Free(FixedRays
);
544 static evalue
*volume_in_domain(Param_Polyhedron
*PP
, Param_Domain
*D
,
545 unsigned dim
, evalue
***matrix
,
546 struct parameter_point
*point
,
547 int row
, Polyhedron
*F
,
548 struct barvinok_options
*options
)
557 FORALL_PVertex_in_ParamPolyhedron(V
, D
, PP
)
559 END_FORALL_PVertex_in_ParamPolyhedron
;
561 if (nbV
> (dim
-row
) + 1) {
562 if (options
->volume_triangulate_lift
)
563 vol
= volume_triangulate_lift(PP
, D
, dim
, matrix
, point
,
564 row
, options
->MaxRays
);
566 vol
= volume_triangulate(PP
, D
, dim
, matrix
, point
,
569 assert(nbV
== (dim
-row
) + 1);
570 vol
= volume_simplex(PP
, D
, dim
, matrix
, point
, row
, options
->MaxRays
);
576 evalue
* Param_Polyhedron_Volume(Polyhedron
*P
, Polyhedron
* C
,
577 struct barvinok_options
*options
)
580 unsigned nparam
= C
->Dimension
;
581 unsigned nvar
= P
->Dimension
- C
->Dimension
;
582 Param_Polyhedron
*PP
;
583 unsigned PP_MaxRays
= options
->MaxRays
;
589 struct section
{ Polyhedron
*D
; evalue
*E
; } *s
;
593 if (options
->polynomial_approximation
== BV_APPROX_SIGN_NONE
)
594 options
->polynomial_approximation
= BV_APPROX_SIGN_APPROX
;
596 if (options
->polynomial_approximation
!= BV_APPROX_SIGN_APPROX
) {
597 int pa
= options
->polynomial_approximation
;
598 assert(pa
== BV_APPROX_SIGN_UPPER
|| pa
== BV_APPROX_SIGN_LOWER
);
600 P
= Polyhedron_Flate(P
, nparam
, pa
== BV_APPROX_SIGN_UPPER
,
603 /* Don't deflate/inflate again (on this polytope) */
604 options
->polynomial_approximation
= BV_APPROX_SIGN_APPROX
;
605 vol
= barvinok_enumerate_with_options(P
, C
, options
);
606 options
->polynomial_approximation
= pa
;
612 TC
= true_context(P
, NULL
, C
, options
->MaxRays
);
614 if (PP_MaxRays
& POL_NO_DUAL
)
617 MaxRays
= options
->MaxRays
;
618 POL_UNSET(options
->MaxRays
, POL_INTEGER
);
621 Factorial(nvar
, &fact
);
623 PP
= Polyhedron2Param_Domain(P
, C
, PP_MaxRays
);
625 for (nd
= 0, D
= PP
->D
; D
; ++nd
, D
= D
->next
);
626 s
= ALLOCN(struct section
, nd
);
628 matrix
= ALLOCN(evalue
**, nvar
+1);
629 for (i
= 0; i
< nvar
+1; ++i
)
630 matrix
[i
] = ALLOCN(evalue
*, nvar
);
632 FORALL_REDUCED_DOMAIN(PP
, TC
, NULL
, NULL
, nd
, options
, i
, D
, rVD
)
634 struct parameter_point
*point
;
636 CA
= align_context(D
->Domain
, P
->Dimension
, MaxRays
);
637 F
= DomainIntersection(P
, CA
, options
->MaxRays
);
640 point
= non_empty_point(D
);
642 s
[i
].E
= volume_in_domain(PP
, D
, nvar
, matrix
, point
, 0, F
, options
);
644 parameter_point_free(point
);
645 evalue_div(s
[i
].E
, fact
);
646 END_FORALL_REDUCED_DOMAIN
647 options
->MaxRays
= MaxRays
;
652 value_set_si(vol
->d
, 0);
655 evalue_set_si(vol
, 0, 1);
657 vol
->x
.p
= new_enode(partition
, 2*nd
, C
->Dimension
);
658 for (i
= 0; i
< nd
; ++i
) {
659 EVALUE_SET_DOMAIN(vol
->x
.p
->arr
[2*i
], s
[i
].D
);
660 value_clear(vol
->x
.p
->arr
[2*i
+1].d
);
661 vol
->x
.p
->arr
[2*i
+1] = *s
[i
].E
;
667 for (i
= 0; i
< nvar
+1; ++i
)
670 Param_Polyhedron_Free(PP
);