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 /* Plug in the parametric vertex V in the constraint constraint.
107 * The result is stored in row, with the denominator in position 0.
109 static void Param_Inner_Product(Value
*constraint
, Matrix
*Vertex
,
112 unsigned nparam
= Vertex
->NbColumns
- 2;
113 unsigned nvar
= Vertex
->NbRows
;
117 value_set_si(row
[0], 1);
118 Vector_Set(row
+1, 0, nparam
+1);
123 for (j
= 0 ; j
< nvar
; ++j
) {
124 value_set_si(tmp
, 1);
125 value_assign(tmp2
, constraint
[1+j
]);
126 if (value_ne(row
[0], Vertex
->p
[j
][nparam
+1])) {
127 value_assign(tmp
, row
[0]);
128 value_lcm(row
[0], Vertex
->p
[j
][nparam
+1], &row
[0]);
129 value_division(tmp
, row
[0], tmp
);
130 value_multiply(tmp2
, tmp2
, row
[0]);
131 value_division(tmp2
, tmp2
, Vertex
->p
[j
][nparam
+1]);
133 Vector_Combine(row
+1, Vertex
->p
[j
], row
+1, tmp
, tmp2
, nparam
+1);
135 value_set_si(tmp
, 1);
136 Vector_Combine(row
+1, constraint
+1+nvar
, row
+1, tmp
, row
[0], nparam
+1);
142 /* Compute a dummy Param_Domain that contains all vertices of Param_Domain D
143 * (which contains the vertices of P) that lie on the facet obtain by
144 * saturating constraint c of P
146 static Param_Domain
*face_vertices(Param_Polyhedron
*PP
, Param_Domain
*D
,
147 Polyhedron
*P
, int c
)
151 Param_Domain
*FD
= ALLOC(Param_Domain
);
154 unsigned nparam
= PP
->V
->Vertex
->NbColumns
-2;
155 Vector
*row
= Vector_Alloc(1+nparam
+1);
157 nv
= (PP
->nbV
- 1)/(8*sizeof(int)) + 1;
158 FD
->F
= ALLOCN(unsigned, nv
);
159 memset(FD
->F
, 0, nv
* sizeof(unsigned));
161 FORALL_PVertex_in_ParamPolyhedron(V
, D
, PP
) /* _i, _ix, _bx internal counters */
163 Param_Inner_Product(P
->Constraint
[c
], V
->Vertex
, row
->p
);
164 if (First_Non_Zero(row
->p
+1, nparam
+1) == -1)
166 END_FORALL_PVertex_in_ParamPolyhedron
;
173 /* Substitute parameters by the corresponding element in subs
175 static evalue
*evalue_substitute_new(evalue
*e
, evalue
**subs
)
181 if (value_notzero_p(e
->d
)) {
187 assert(e
->x
.p
->type
== polynomial
);
190 for (i
= e
->x
.p
->size
-1; i
> 0; --i
) {
191 c
= evalue_substitute_new(&e
->x
.p
->arr
[i
], subs
);
195 emul(subs
[e
->x
.p
->pos
-1], res
);
197 c
= evalue_substitute_new(&e
->x
.p
->arr
[0], subs
);
205 struct parameter_point
{
210 struct parameter_point
*parameter_point_new(unsigned nparam
)
212 struct parameter_point
*point
= ALLOC(struct parameter_point
);
213 point
->coord
= Vector_Alloc(nparam
+1);
218 evalue
**parameter_point_evalue(struct parameter_point
*point
)
221 unsigned nparam
= point
->coord
->Size
-1;
226 point
->e
= ALLOCN(evalue
*, nparam
);
227 for (j
= 0; j
< nparam
; ++j
) {
228 point
->e
[j
] = ALLOC(evalue
);
229 value_init(point
->e
[j
]->d
);
230 evalue_set(point
->e
[j
], point
->coord
->p
[j
], point
->coord
->p
[nparam
]);
236 void parameter_point_free(struct parameter_point
*point
)
239 unsigned nparam
= point
->coord
->Size
-1;
241 Vector_Free(point
->coord
);
244 for (i
= 0; i
< nparam
; ++i
) {
245 free_evalue_refs(point
->e
[i
]);
253 /* Computes point in pameter space where polyhedron is non-empty.
255 static struct parameter_point
*non_empty_point(Param_Domain
*D
)
257 unsigned nparam
= D
->Domain
->Dimension
;
258 struct parameter_point
*point
;
261 v
= inner_point(D
->Domain
);
262 point
= parameter_point_new(nparam
);
263 Vector_Copy(v
->p
+1, point
->coord
->p
, nparam
+1);
269 static Matrix
*barycenter(Param_Polyhedron
*PP
, Param_Domain
*D
)
272 Matrix
*center
= NULL
;
282 FORALL_PVertex_in_ParamPolyhedron(V
, D
, PP
)
285 center
= Matrix_Copy(V
->Vertex
);
286 nparam
= center
->NbColumns
- 2;
288 for (i
= 0; i
< center
->NbRows
; ++i
) {
289 value_assign(fc
, center
->p
[i
][nparam
+1]);
290 value_lcm(fc
, V
->Vertex
->p
[i
][nparam
+1],
291 ¢er
->p
[i
][nparam
+1]);
292 value_division(fc
, center
->p
[i
][nparam
+1], fc
);
293 value_division(fv
, center
->p
[i
][nparam
+1],
294 V
->Vertex
->p
[i
][nparam
+1]);
295 Vector_Combine(center
->p
[i
], V
->Vertex
->p
[i
], center
->p
[i
],
299 END_FORALL_PVertex_in_ParamPolyhedron
;
304 value_set_si(denom
, nbV
);
305 for (i
= 0; i
< center
->NbRows
; ++i
) {
306 value_multiply(center
->p
[i
][nparam
+1], center
->p
[i
][nparam
+1], denom
);
307 Vector_Normalize(center
->p
[i
], nparam
+2);
314 static Matrix
*triangulation_vertex(Param_Polyhedron
*PP
, Param_Domain
*D
,
319 FORALL_PVertex_in_ParamPolyhedron(V
, D
, PP
)
321 END_FORALL_PVertex_in_ParamPolyhedron
;
327 /* Compute dim! times the volume of polyhedron F in Param_Domain D.
328 * If F is a simplex, then the volume is computed of a recursive pyramid
329 * over F with the points already in matrix.
330 * Otherwise, the barycenter of F is added to matrix and the function
331 * is called recursively on the facets of F.
333 * The first row of matrix contain the _negative_ of the first point.
334 * The remaining rows of matrix contain the distance of the corresponding
335 * point to the first point.
337 static evalue
*volume_in_domain(Param_Polyhedron
*PP
, Param_Domain
*D
,
338 unsigned dim
, evalue
***matrix
,
339 struct parameter_point
*point
,
340 int row
, Polyhedron
*F
,
341 struct barvinok_options
*options
);
343 static evalue
*volume_triangulate(Param_Polyhedron
*PP
, Param_Domain
*D
,
344 unsigned dim
, evalue
***matrix
,
345 struct parameter_point
*point
,
346 int row
, Polyhedron
*F
,
347 struct barvinok_options
*options
)
354 unsigned cut_MaxRays
= options
->MaxRays
;
355 unsigned nparam
= PP
->V
->Vertex
->NbColumns
-2;
358 POL_UNSET(cut_MaxRays
, POL_INTEGER
);
361 evalue_set_si(&mone
, -1, 1);
363 if (options
->volume_triangulate
== BV_VOL_BARYCENTER
)
364 center
= barycenter(PP
, D
);
366 center
= triangulation_vertex(PP
, D
, F
);
367 for (j
= 0; j
< dim
; ++j
)
368 matrix
[row
][j
] = vertex2evalue(center
->p
[j
], center
->NbColumns
- 2);
369 if (options
->volume_triangulate
== BV_VOL_BARYCENTER
)
372 v
= Vector_Alloc(1+nparam
+1);
375 for (j
= 0; j
< dim
; ++j
)
376 emul(&mone
, matrix
[row
][j
]);
378 for (j
= 0; j
< dim
; ++j
)
379 eadd(matrix
[0][j
], matrix
[row
][j
]);
383 POL_ENSURE_FACETS(F
);
384 for (j
= F
->NbEq
; j
< F
->NbConstraints
; ++j
) {
387 if (First_Non_Zero(F
->Constraint
[j
]+1, dim
) == -1)
389 if (options
->volume_triangulate
!= BV_VOL_BARYCENTER
) {
390 Param_Inner_Product(F
->Constraint
[j
], center
, v
->p
);
391 if (First_Non_Zero(v
->p
+1, nparam
+1) == -1)
394 FF
= facet(F
, j
, options
->MaxRays
);
395 FD
= face_vertices(PP
, D
, F
, j
);
396 tmp
= volume_in_domain(PP
, FD
, dim
, matrix
, point
,
402 free_evalue_refs(tmp
);
406 Param_Domain_Free(FD
);
409 if (options
->volume_triangulate
!= BV_VOL_BARYCENTER
)
412 for (j
= 0; j
< dim
; ++j
) {
413 free_evalue_refs(matrix
[row
][j
]);
414 free(matrix
[row
][j
]);
417 free_evalue_refs(&mone
);
421 static evalue
*volume_simplex(Param_Polyhedron
*PP
, Param_Domain
*D
,
422 unsigned dim
, evalue
***matrix
,
423 struct parameter_point
*point
,
424 int row
, struct barvinok_options
*options
)
431 options
->stats
->volume_simplices
++;
434 evalue_set_si(&mone
, -1, 1);
437 FORALL_PVertex_in_ParamPolyhedron(V
, D
, PP
) /* _ix, _bx internal counters */
438 for (j
= 0; j
< dim
; ++j
) {
439 matrix
[i
][j
] = vertex2evalue(V
->Vertex
->p
[j
],
440 V
->Vertex
->NbColumns
- 2);
442 emul(&mone
, matrix
[i
][j
]);
444 eadd(matrix
[0][j
], matrix
[i
][j
]);
447 END_FORALL_PVertex_in_ParamPolyhedron
;
449 vol
= determinant(matrix
+1, dim
);
451 val
= evalue_substitute_new(vol
, parameter_point_evalue(point
));
453 assert(value_notzero_p(val
->d
));
454 assert(value_notzero_p(val
->x
.n
));
455 if (value_neg_p(val
->x
.n
))
458 free_evalue_refs(val
);
461 for (i
= row
; i
< dim
+1; ++i
) {
462 for (j
= 0; j
< dim
; ++j
) {
463 free_evalue_refs(matrix
[i
][j
]);
468 free_evalue_refs(&mone
);
473 static evalue
*volume_triangulate_lift(Param_Polyhedron
*PP
, Param_Domain
*D
,
474 unsigned dim
, evalue
***matrix
,
475 struct parameter_point
*point
,
476 int row
, struct barvinok_options
*options
)
478 const static int MAX_TRY
=10;
483 Matrix
*FixedRays
, *M
;
491 nv
= (PP
->nbV
- 1)/(8*sizeof(int)) + 1;
492 SD
.F
= ALLOCN(unsigned, nv
);
494 FixedRays
= Matrix_Alloc(PP
->nbV
+1, 1+dim
+2);
496 FORALL_PVertex_in_ParamPolyhedron(V
, D
, PP
)
497 unsigned nparam
= V
->Vertex
->NbColumns
- 2;
498 Param_Vertex_Common_Denominator(V
);
499 for (i
= 0; i
< V
->Vertex
->NbRows
; ++i
) {
500 value_multiply(FixedRays
->p
[nbV
][1+i
], V
->Vertex
->p
[i
][nparam
],
501 point
->coord
->p
[nparam
]);
502 Inner_Product(V
->Vertex
->p
[i
], point
->coord
->p
, nparam
,
503 &FixedRays
->p
[nbV
][1+dim
]);
504 value_addto(FixedRays
->p
[nbV
][1+i
], FixedRays
->p
[nbV
][1+i
],
505 FixedRays
->p
[nbV
][1+dim
]);
507 value_multiply(FixedRays
->p
[nbV
][1+dim
+1], V
->Vertex
->p
[0][nparam
+1],
508 point
->coord
->p
[nparam
]);
509 value_set_si(FixedRays
->p
[nbV
][0], 1);
511 END_FORALL_PVertex_in_ParamPolyhedron
;
512 value_set_si(FixedRays
->p
[nbV
][0], 1);
513 value_set_si(FixedRays
->p
[nbV
][1+dim
], 1);
514 FixedRays
->NbRows
= nbV
+1;
519 /* Usually vol should still be NULL */
521 free_evalue_refs(vol
);
527 assert(t
<= MAX_TRY
);
530 for (i
= 0; i
< nbV
; ++i
)
531 value_set_si(FixedRays
->p
[i
][1+dim
], random_int((t
+1)*dim
*nbV
)+1);
533 M
= Matrix_Copy(FixedRays
);
534 L
= Rays2Polyhedron(M
, options
->MaxRays
);
537 POL_ENSURE_FACETS(L
);
538 for (i
= 0; i
< L
->NbConstraints
; ++i
) {
540 /* Ignore perpendicular facets, i.e., facets with 0 z-coordinate */
541 if (value_negz_p(L
->Constraint
[i
][1+dim
]))
544 memset(SD
.F
, 0, nv
* sizeof(unsigned));
547 FORALL_PVertex_in_ParamPolyhedron(V
, D
, PP
) /* _ix, _bx internal */
548 Inner_Product(FixedRays
->p
[nbV
]+1, L
->Constraint
[i
]+1, dim
+2, &tmp
);
549 if (value_zero_p(tmp
)) {
556 END_FORALL_PVertex_in_ParamPolyhedron
;
557 assert(r
== (dim
-row
)+1);
559 s
= volume_simplex(PP
, &SD
, dim
, matrix
, point
, row
, options
);
569 Matrix_Free(FixedRays
);
576 static evalue
*volume_in_domain(Param_Polyhedron
*PP
, Param_Domain
*D
,
577 unsigned dim
, evalue
***matrix
,
578 struct parameter_point
*point
,
579 int row
, Polyhedron
*F
,
580 struct barvinok_options
*options
)
589 FORALL_PVertex_in_ParamPolyhedron(V
, D
, PP
)
591 END_FORALL_PVertex_in_ParamPolyhedron
;
593 if (nbV
> (dim
-row
) + 1) {
594 if (options
->volume_triangulate
== BV_VOL_LIFT
)
595 vol
= volume_triangulate_lift(PP
, D
, dim
, matrix
, point
,
598 vol
= volume_triangulate(PP
, D
, dim
, matrix
, point
,
601 assert(nbV
== (dim
-row
) + 1);
602 vol
= volume_simplex(PP
, D
, dim
, matrix
, point
, row
, options
);
608 evalue
* Param_Polyhedron_Volume(Polyhedron
*P
, Polyhedron
* C
,
609 struct barvinok_options
*options
)
612 unsigned nparam
= C
->Dimension
;
613 unsigned nvar
= P
->Dimension
- C
->Dimension
;
614 Param_Polyhedron
*PP
;
615 unsigned PP_MaxRays
= options
->MaxRays
;
621 struct section
{ Polyhedron
*D
; evalue
*E
; } *s
;
625 if (options
->polynomial_approximation
== BV_APPROX_SIGN_NONE
)
626 options
->polynomial_approximation
= BV_APPROX_SIGN_APPROX
;
628 if (options
->polynomial_approximation
!= BV_APPROX_SIGN_APPROX
) {
629 int pa
= options
->polynomial_approximation
;
630 assert(pa
== BV_APPROX_SIGN_UPPER
|| pa
== BV_APPROX_SIGN_LOWER
);
632 P
= Polyhedron_Flate(P
, nparam
, pa
== BV_APPROX_SIGN_UPPER
,
635 /* Don't deflate/inflate again (on this polytope) */
636 options
->polynomial_approximation
= BV_APPROX_SIGN_APPROX
;
637 vol
= barvinok_enumerate_with_options(P
, C
, options
);
638 options
->polynomial_approximation
= pa
;
644 TC
= true_context(P
, C
, options
->MaxRays
);
646 if (PP_MaxRays
& POL_NO_DUAL
)
649 MaxRays
= options
->MaxRays
;
650 POL_UNSET(options
->MaxRays
, POL_INTEGER
);
653 Factorial(nvar
, &fact
);
655 PP
= Polyhedron2Param_Domain(P
, C
, PP_MaxRays
);
657 for (nd
= 0, D
= PP
->D
; D
; ++nd
, D
= D
->next
);
658 s
= ALLOCN(struct section
, nd
);
660 matrix
= ALLOCN(evalue
**, nvar
+1);
661 for (i
= 0; i
< nvar
+1; ++i
)
662 matrix
[i
] = ALLOCN(evalue
*, nvar
);
664 FORALL_REDUCED_DOMAIN(PP
, TC
, nd
, options
, i
, D
, rVD
)
666 struct parameter_point
*point
;
668 CA
= align_context(D
->Domain
, P
->Dimension
, MaxRays
);
669 F
= DomainIntersection(P
, CA
, options
->MaxRays
);
672 point
= non_empty_point(D
);
674 s
[i
].E
= volume_in_domain(PP
, D
, nvar
, matrix
, point
, 0, F
, options
);
676 parameter_point_free(point
);
677 evalue_div(s
[i
].E
, fact
);
678 END_FORALL_REDUCED_DOMAIN
679 options
->MaxRays
= MaxRays
;
684 value_set_si(vol
->d
, 0);
687 evalue_set_si(vol
, 0, 1);
689 vol
->x
.p
= new_enode(partition
, 2*nd
, C
->Dimension
);
690 for (i
= 0; i
< nd
; ++i
) {
691 EVALUE_SET_DOMAIN(vol
->x
.p
->arr
[2*i
], s
[i
].D
);
692 value_clear(vol
->x
.p
->arr
[2*i
+1].d
);
693 vol
->x
.p
->arr
[2*i
+1] = *s
[i
].E
;
699 for (i
= 0; i
< nvar
+1; ++i
)
702 Param_Polyhedron_Free(PP
);