2 * Sum polynomial over integer points in polytope using local
3 * Euler-Maclaurin formula by Berline and Vergne.
7 #include <barvinok/options.h>
8 #include <barvinok/util.h>
10 #include "conversion.h"
11 #include "decomposer.h"
13 #include "lattice_point.h"
14 #include "param_util.h"
15 #include "reduce_domain.h"
17 /* Compute total degree in first nvar variables */
18 static unsigned total_degree(const evalue
*e
, unsigned nvar
)
23 if (value_notzero_p(e
->d
))
25 assert(e
->x
.p
->type
== polynomial
);
26 if (e
->x
.p
->pos
-1 >= nvar
)
29 max_degree
= total_degree(&e
->x
.p
->arr
[0], nvar
);
30 for (i
= 1; i
< e
->x
.p
->size
; ++i
) {
31 unsigned degree
= i
+ total_degree(&e
->x
.p
->arr
[i
], nvar
);
32 if (degree
> max_degree
)
45 Value
*factorial(unsigned n
)
51 int size
= 3*(n
+ 5)/2;
53 fact
.fact
= (Value
*)realloc(fact
.fact
, size
*sizeof(Value
));
56 for (int i
= fact
.n
; i
<= n
; ++i
) {
57 value_init(fact
.fact
[i
]);
59 value_set_si(fact
.fact
[0], 1);
61 mpz_mul_ui(fact
.fact
[i
], fact
.fact
[i
-1], i
);
74 Value
*binomial(unsigned n
, unsigned k
)
77 return &binom
.binom
[n
]->p
[k
];
79 if (n
>= binom
.size
) {
80 int size
= 3*(n
+ 5)/2;
82 binom
.binom
= (Vector
**)realloc(binom
.binom
, size
*sizeof(Vector
*));
85 for (int i
= binom
.n
; i
<= n
; ++i
) {
86 binom
.binom
[i
] = Vector_Alloc(i
+1);
88 value_set_si(binom
.binom
[0]->p
[0], 1);
90 value_set_si(binom
.binom
[i
]->p
[0], 1);
91 value_set_si(binom
.binom
[i
]->p
[i
], 1);
92 for (int j
= 1; j
< i
; ++j
)
93 value_addto(binom
.binom
[i
]->p
[j
],
94 binom
.binom
[i
-1]->p
[j
-1], binom
.binom
[i
-1]->p
[j
]);
98 return &binom
.binom
[n
]->p
[k
];
105 power(Value v
, int max
) {
106 powers
= Vector_Alloc(max
+1);
108 value_set_si(powers
->p
[0], 1);
110 value_assign(powers
->p
[1], v
);
116 Value
*operator[](int exp
) {
118 assert(exp
< powers
->Size
);
119 for (; n
<= exp
; ++n
)
120 value_multiply(powers
->p
[n
], powers
->p
[n
-1], powers
->p
[1]);
121 return &powers
->p
[exp
];
126 * Computes the coefficients of
128 * \mu(-t + R_+)(\xi) = \sum_{n=0)^\infty -b(n+1, t)/(n+1)! \xi^n
130 * where b(n, t) is the Bernoulli polynomial of degree n in t
131 * and t(p) is an expression (a fractional part) of the parameters p
132 * such that 0 <= t(p) < 1 for all values of the parameters.
133 * The coefficients are computed on demand up to (and including)
134 * the maximal degree max_degree.
138 evalue
**coefficients
;
141 mu_1d(unsigned max_degree
, evalue
*t
) : max_degree(max_degree
), t(t
) {
142 coefficients
= new evalue
*[max_degree
+1];
143 for (int i
= 0; i
< max_degree
+1; ++i
)
144 coefficients
[i
] = NULL
;
147 for (int i
= 0; i
< max_degree
+1; ++i
)
149 evalue_free(coefficients
[i
]);
150 delete [] coefficients
;
152 void compute_coefficient(unsigned n
);
153 const evalue
*coefficient(unsigned n
) {
154 if (!coefficients
[n
])
155 compute_coefficient(n
);
156 return coefficients
[n
];
160 void mu_1d::compute_coefficient(unsigned n
)
162 struct poly_list
*bernoulli
= bernoulli_compute(n
+1);
163 evalue
*c
= evalue_polynomial(bernoulli
->poly
[n
+1], t
);
165 evalue_div(c
, *factorial(n
+1));
171 * Computes the coefficients of
173 * \mu(a)(y) = \sum_{n_1} \sum_{n_2} c_{n_1,n_2} y^{n_1} y^{n_2}
175 * with c_{n1,n2} given by
177 * b(n1+1,t1)/(n1+1)! b(n2+1,t2)/(n2+1)!
178 * - b(n1+n2+2,t2)/(n1+n2+2)! (-c1)^{n1+1}
179 * - b(n1+n2+2,t1)/(n1+n2+2)! (-c2)^{n2+1}
181 * where b(n, t) is the Bernoulli polynomial of degree n in t,
182 * t1(p) and t2(p) are expressions (fractional parts) of the parameters p
183 * such that 0 <= t1(p), t2(p) < 1 for all values of the parameters
184 * and c1 = cn/c1d and c2 = cn/c2d.
185 * The coefficients are computed on demand up to (and including)
186 * the maximal degree (n1,n2) = (max_degree,max_degree).
188 * bernoulli_t[i][j] contains b(j+1, t_i)/(j+1)!
192 evalue
***coefficients
;
193 /* bernoulli_t[i][n] stores b(n+1, t_i)/(n+1)! */
194 evalue
**bernoulli_t
[2];
195 /* stores the powers of -cn */
196 struct power
*power_cn
;
197 struct power
*power_c1d
;
198 struct power
*power_c2d
;
201 mu_2d(unsigned max_degree
, evalue
*t1
, evalue
*t2
,
202 Value cn
, Value c1d
, Value c2d
) : max_degree(max_degree
) {
205 coefficients
= new evalue
**[max_degree
+1];
206 for (int i
= 0; i
< max_degree
+1; ++i
) {
207 coefficients
[i
] = new evalue
*[max_degree
+1];
208 for (int j
= 0; j
< max_degree
+1; ++j
)
209 coefficients
[i
][j
] = NULL
;
211 for (int i
= 0; i
< 2; ++i
) {
212 bernoulli_t
[i
] = new evalue
*[max_degree
+2];
213 for (int j
= 0; j
< max_degree
+2; ++j
)
214 bernoulli_t
[i
][j
] = NULL
;
216 value_oppose(cn
, cn
);
217 power_cn
= new struct power(cn
, max_degree
+1);
218 value_oppose(cn
, cn
);
219 power_c1d
= new struct power(c1d
, max_degree
+1);
220 power_c2d
= new struct power(c2d
, max_degree
+1);
223 for (int i
= 0; i
< max_degree
+1; ++i
) {
224 for (int j
= 0; j
< max_degree
+1; ++j
)
225 if (coefficients
[i
][j
])
226 evalue_free(coefficients
[i
][j
]);
227 delete [] coefficients
[i
];
229 delete [] coefficients
;
230 for (int i
= 0; i
< 2; ++i
)
231 for (int j
= 0; j
< max_degree
+2; ++j
)
232 if (bernoulli_t
[i
][j
])
233 evalue_free(bernoulli_t
[i
][j
]);
234 for (int i
= 0; i
< 2; ++i
)
235 delete [] bernoulli_t
[i
];
240 const evalue
*get_bernoulli(int n
, int i
);
242 void compute_coefficient(unsigned n1
, unsigned n2
);
243 const evalue
*coefficient(unsigned n1
, unsigned n2
) {
244 if (!coefficients
[n1
][n2
])
245 compute_coefficient(n1
, n2
);
246 return coefficients
[n1
][n2
];
251 * Returns b(n, t_i)/n!
253 const evalue
*mu_2d::get_bernoulli(int n
, int i
)
255 if (!bernoulli_t
[i
][n
-1]) {
256 struct poly_list
*bernoulli
= bernoulli_compute(n
);
257 bernoulli_t
[i
][n
-1] = evalue_polynomial(bernoulli
->poly
[n
], t
[i
]);
258 evalue_div(bernoulli_t
[i
][n
-1], *factorial(n
));
260 return bernoulli_t
[i
][n
-1];
263 void mu_2d::compute_coefficient(unsigned n1
, unsigned n2
)
265 evalue
*c
= evalue_dup(get_bernoulli(n1
+1, 0));
266 emul(get_bernoulli(n2
+1, 1), c
);
268 if (value_notzero_p(*(*power_cn
)[1])) {
272 value_init(neg_power
);
274 t
= evalue_dup(get_bernoulli(n1
+n2
+2, 1));
275 value_multiply(neg_power
,
276 *(*power_cn
)[n1
+1], *binomial(n1
+n2
+1, n1
+1));
277 value_oppose(neg_power
, neg_power
);
278 evalue_mul_div(t
, neg_power
, *(*power_c1d
)[n1
+1]);
282 t
= evalue_dup(get_bernoulli(n1
+n2
+2, 0));
283 value_multiply(neg_power
,
284 *(*power_cn
)[n2
+1], *binomial(n1
+n2
+1, n2
+1));
285 value_oppose(neg_power
, neg_power
);
286 evalue_mul_div(t
, neg_power
, *(*power_c2d
)[n2
+1]);
290 value_clear(neg_power
);
293 coefficients
[n1
][n2
] = c
;
296 /* Later: avoid recomputation of mu of faces that appear in
297 * more than one domain.
299 struct summator_2d
: public signed_cone_consumer
, public vertex_decomposer
{
300 const evalue
*polynomial
;
304 /* substitutions to use when result is 0-dimensional (only parameters) */
306 /* substitutions to use when result is 1-dimensional */
310 summator_2d(evalue
*e
, Param_Polyhedron
*PP
, Value
*inner
,
312 polynomial(e
), vertex_decomposer(PP
, *this),
313 inner(inner
), nparam(nparam
) {
316 subs_0d
= new evalue
*[2+nparam
];
317 subs_1d
= new evalue
*[2+nparam
];
322 for (int i
= 0; i
< nparam
; ++i
) {
323 subs_0d
[2+i
] = evalue_var(i
);
324 subs_1d
[2+i
] = evalue_var(1+i
);
328 for (int i
= 0; i
< nparam
; ++i
) {
329 evalue_free(subs_0d
[2+i
]);
330 evalue_free(subs_1d
[2+i
]);
335 evalue
*summate_over_pdomain(Param_Polyhedron
*PP
, unsigned *facets
,
337 struct barvinok_options
*options
);
338 void handle_facet(Param_Polyhedron
*PP
, Param_Domain
*FD
, Value
*normal
);
339 void integrate(Param_Polyhedron
*PP
, unsigned *facets
, Param_Domain
*PD
);
340 virtual void handle(const signed_cone
& sc
, barvinok_options
*options
);
343 /* Replaces poly by its derivative along variable var */
344 static void evalue_derive(evalue
*poly
, int var
)
346 if (value_notzero_p(poly
->d
)) {
347 value_set_si(poly
->x
.n
, 0);
348 value_set_si(poly
->d
, 1);
351 assert(poly
->x
.p
->type
== polynomial
);
352 if (poly
->x
.p
->pos
-1 > var
) {
353 free_evalue_refs(poly
);
355 evalue_set_si(poly
, 0, 1);
359 if (poly
->x
.p
->pos
-1 < var
) {
360 for (int i
= 0; i
< poly
->x
.p
->size
; ++i
)
361 evalue_derive(&poly
->x
.p
->arr
[i
], var
);
366 assert(poly
->x
.p
->size
>= 1);
367 enode
*p
= poly
->x
.p
;
368 free_evalue_refs(&p
->arr
[0]);
371 evalue_set_si(poly
, 0, 1);
375 value_clear(poly
->d
);
384 for (int i
= 0; i
< p
->size
; ++i
) {
385 value_set_si(factor
, i
+1);
386 p
->arr
[i
] = p
->arr
[i
+1];
387 evalue_mul(&p
->arr
[i
], factor
);
392 /* Check whether e is constant with respect to variable var */
393 static int evalue_is_constant(evalue
*e
, int var
)
397 if (value_notzero_p(e
->d
))
399 if (e
->x
.p
->type
== polynomial
&& e
->x
.p
->pos
-1 == var
)
401 assert(e
->x
.p
->type
== polynomial
||
402 e
->x
.p
->type
== fractional
||
403 e
->x
.p
->type
== flooring
);
404 for (i
= 0; i
< e
->x
.p
->size
; ++i
)
405 if (!evalue_is_constant(&e
->x
.p
->arr
[i
], var
))
410 /* Replaces poly by its anti-derivative with constant 0 along variable var */
411 static void evalue_anti_derive(evalue
*poly
, int var
)
415 if (value_zero_p(poly
->d
) &&
416 poly
->x
.p
->type
== polynomial
&& poly
->x
.p
->pos
-1 < var
) {
417 for (int i
= 0; i
< poly
->x
.p
->size
; ++i
)
418 evalue_anti_derive(&poly
->x
.p
->arr
[i
], var
);
423 if (evalue_is_constant(poly
, var
)) {
424 p
= new_enode(polynomial
, 2, 1+var
);
425 evalue_set_si(&p
->arr
[0], 0, 1);
426 value_clear(p
->arr
[1].d
);
432 assert(poly
->x
.p
->type
== polynomial
);
434 p
= new_enode(polynomial
, poly
->x
.p
->size
+1, 1+var
);
435 evalue_set_si(&p
->arr
[0], 0, 1);
436 for (int i
= 0; i
< poly
->x
.p
->size
; ++i
) {
437 value_clear(p
->arr
[1+i
].d
);
438 p
->arr
[1+i
] = poly
->x
.p
->arr
[i
];
439 value_set_si(poly
->d
, 1+i
);
440 evalue_div(&p
->arr
[1+i
], poly
->d
);
444 value_set_si(poly
->d
, 0);
447 /* Computes offsets in the basis given by the rays of the cone
448 * to the integer point in the fundamental parallelepiped of
450 * The resulting evalues contain only the parameters as variables.
452 evalue
**offsets_to_integer_point(Matrix
*Rays
, Matrix
*vertex
)
454 unsigned nparam
= vertex
->NbColumns
-2;
455 evalue
**t
= new evalue
*[2];
457 if (value_one_p(vertex
->p
[0][nparam
+1])) {
458 t
[0] = evalue_zero();
459 t
[1] = evalue_zero();
461 Matrix
*R2
= Matrix_Copy(Rays
);
462 Matrix_Transposition(R2
);
463 Matrix
*inv
= Matrix_Alloc(Rays
->NbColumns
, Rays
->NbRows
);
464 int ok
= Matrix_Inverse(R2
, inv
);
468 /* We want the fractional part of the negative relative coordinates */
469 Vector_Oppose(inv
->p
[0], inv
->p
[0], inv
->NbColumns
);
470 Vector_Oppose(inv
->p
[1], inv
->p
[1], inv
->NbColumns
);
472 Matrix
*neg_rel
= Matrix_Alloc(2, nparam
+1);
474 Matrix_Product(inv
, vertex
, neg_rel
);
477 t
[0] = fractional_part(neg_rel
->p
[0], vertex
->p
[0][nparam
+1],
479 t
[1] = fractional_part(neg_rel
->p
[1], vertex
->p
[0][nparam
+1],
481 Matrix_Free(neg_rel
);
488 * Called from decompose_at_vertex.
490 * Handles a cone in the signed decomposition of the supporting
491 * cone of a vertex. The cone is assumed to be unimodular.
493 void summator_2d::handle(const signed_cone
& sc
, barvinok_options
*options
)
496 unsigned degree
= total_degree(polynomial
, 2);
498 subs_0d
[0] = affine2evalue(V
->Vertex
->p
[0],
499 V
->Vertex
->p
[0][nparam
+1], nparam
);
500 subs_0d
[1] = affine2evalue(V
->Vertex
->p
[1],
501 V
->Vertex
->p
[1][nparam
+1], nparam
);
505 assert(V
->Vertex
->NbRows
> 0);
506 Param_Vertex_Common_Denominator(V
);
508 Matrix
*Rays
= zz2matrix(sc
.rays
);
510 t
= offsets_to_integer_point(Rays
, V
->Vertex
);
512 Vector
*c
= Vector_Alloc(3);
513 Inner_Product(Rays
->p
[0], Rays
->p
[1], 2, &c
->p
[0]);
514 Inner_Product(Rays
->p
[0], Rays
->p
[0], 2, &c
->p
[1]);
515 Inner_Product(Rays
->p
[1], Rays
->p
[1], 2, &c
->p
[2]);
517 mu_2d
mu(degree
, t
[0], t
[1], c
->p
[0], c
->p
[1], c
->p
[2]);
520 struct power
power_r00(Rays
->p
[0][0], degree
);
521 struct power
power_r01(Rays
->p
[0][1], degree
);
522 struct power
power_r10(Rays
->p
[1][0], degree
);
523 struct power
power_r11(Rays
->p
[1][1], degree
);
525 Value factor
, tmp1
, tmp2
;
529 evalue
*res
= evalue_zero();
530 evalue
*dx1
= evalue_dup(polynomial
);
531 for (int i
= 0; !EVALUE_IS_ZERO(*dx1
); ++i
) {
532 evalue
*dx2
= evalue_dup(dx1
);
533 for (int j
= 0; !EVALUE_IS_ZERO(*dx2
); ++j
) {
534 evalue
*cij
= evalue_zero();
535 for (int n1
= 0; n1
<= i
+j
; ++n1
) {
537 value_set_si(factor
, 0);
538 for (int k
= max(0, i
-n2
); k
<= i
&& k
<= n1
; ++k
) {
540 value_multiply(tmp1
, *power_r00
[k
], *power_r01
[n1
-k
]);
541 value_multiply(tmp1
, tmp1
, *power_r10
[l
]);
542 value_multiply(tmp1
, tmp1
, *power_r11
[n2
-l
]);
543 value_multiply(tmp1
, tmp1
, *binomial(n1
, k
));
544 value_multiply(tmp1
, tmp1
, *binomial(n2
, l
));
545 value_addto(factor
, factor
, tmp1
);
547 if (value_zero_p(factor
))
550 evalue
*c
= evalue_dup(mu
.coefficient(n1
, n2
));
551 evalue_mul(c
, factor
);
555 evalue
*d
= evalue_dup(dx2
);
556 evalue_substitute(d
, subs_0d
);
561 evalue_derive(dx2
, 1);
564 evalue_derive(dx1
, 0);
567 for (int i
= 0; i
< 2; ++i
) {
568 evalue_free(subs_0d
[i
]);
584 evalue
*summator_2d::summate_over_pdomain(Param_Polyhedron
*PP
,
585 unsigned *facets
, Param_Domain
*PD
,
586 struct barvinok_options
*options
)
592 assert(PP
->V
->Vertex
->NbRows
== 2);
594 FORALL_PVertex_in_ParamPolyhedron(V
, PD
, PP
) // _i internal counter
595 decompose_at_vertex(V
, _i
, options
);
596 END_FORALL_PVertex_in_ParamPolyhedron
;
598 Vector
*normal
= Vector_Alloc(2);
599 for (i
= 0, ix
= 0, bx
= MSB
; i
< PP
->Constraints
->NbRows
; ++i
) {
602 if (!(facets
[ix
] & bx
)) {
607 Vector_Copy(PP
->Constraints
->p
[i
]+1, normal
->p
, 2);
608 if (value_zero_p(normal
->p
[0]) && value_zero_p(normal
->p
[1]))
611 FD
= Param_Polyhedron_Facet(PP
, PD
, PP
->Constraints
->p
[i
]);
612 Vector_Normalize(normal
->p
, 2);
613 handle_facet(PP
, FD
, normal
->p
);
614 Param_Domain_Free(FD
);
619 integrate(PP
, facets
, PD
);
624 void summator_2d::handle_facet(Param_Polyhedron
*PP
, Param_Domain
*FD
,
629 Param_Vertices
*vertex
[2];
631 unsigned degree
= total_degree(polynomial
, 2);
633 FORALL_PVertex_in_ParamPolyhedron(V
, FD
, PP
)
635 END_FORALL_PVertex_in_ParamPolyhedron
;
639 /* We can take either vertex[0] or vertex[1];
640 * the result should be the same
642 Param_Vertex_Common_Denominator(vertex
[0]);
644 /* The extra variable in front is the coordinate along the facet. */
645 Vector
*coef_normal
= Vector_Alloc(1 + nparam
+ 2);
646 Vector_Combine(vertex
[0]->Vertex
->p
[0], vertex
[0]->Vertex
->p
[1],
647 coef_normal
->p
+1, normal
[0], normal
[1], nparam
+1);
648 value_assign(coef_normal
->p
[1+nparam
+1], vertex
[0]->Vertex
->p
[0][nparam
+1]);
649 Vector_Normalize(coef_normal
->p
, coef_normal
->Size
);
651 Vector
*base
= Vector_Alloc(2);
652 value_oppose(base
->p
[0], normal
[1]);
653 value_assign(base
->p
[1], normal
[0]);
656 Inner_Product(normal
, normal
, 2, &det
);
658 Vector
*s
= Vector_Alloc(1+nparam
+2);
659 value_multiply(s
->p
[1+nparam
+1], coef_normal
->p
[1+nparam
+1], det
);
660 value_multiply(s
->p
[0], base
->p
[0], s
->p
[1+nparam
+1]);
661 Vector_Scale(coef_normal
->p
+1, s
->p
+1, normal
[0], nparam
+1);
662 subs_1d
[0] = affine2evalue(s
->p
, s
->p
[1+nparam
+1], 1+nparam
);
663 value_multiply(s
->p
[0], base
->p
[1], s
->p
[1+nparam
+1]);
664 Vector_Scale(coef_normal
->p
+1, s
->p
+1, normal
[1], nparam
+1);
665 subs_1d
[1] = affine2evalue(s
->p
, s
->p
[1+nparam
+1], 1+nparam
);
669 if (value_one_p(coef_normal
->p
[coef_normal
->Size
-1]))
672 Vector_Oppose(coef_normal
->p
+1, coef_normal
->p
+1, nparam
+1);
673 t
= fractional_part(coef_normal
->p
,
674 coef_normal
->p
[coef_normal
->Size
-1],
677 Vector_Free(coef_normal
);
681 struct power
power_normal0(normal
[0], degree
);
682 struct power
power_normal1(normal
[1], degree
);
683 struct power
power_det(det
, degree
);
687 evalue
*res
= evalue_zero();
688 evalue
*dx1
= evalue_dup(polynomial
);
689 for (int i
= 0; !EVALUE_IS_ZERO(*dx1
); ++i
) {
690 evalue
*dx2
= evalue_dup(dx1
);
691 for (int j
= 0; !EVALUE_IS_ZERO(*dx2
); ++j
) {
692 value_multiply(factor
, *power_normal0
[i
], *power_normal1
[j
]);
693 if (value_notzero_p(factor
)) {
694 value_multiply(factor
, factor
, *binomial(i
+j
, i
));
696 evalue
*c
= evalue_dup(mu
.coefficient(i
+j
));
697 evalue_mul_div(c
, factor
, *power_det
[i
+j
]);
699 evalue
*d
= evalue_dup(dx2
);
700 evalue_substitute(d
, subs_1d
);
706 evalue_derive(dx2
, 1);
709 evalue_derive(dx1
, 0);
713 for (int i
= 0; i
< 2; ++i
) {
714 evalue_free(subs_1d
[i
]);
718 evalue_anti_derive(res
, 0);
720 Matrix
*z
= Matrix_Alloc(2, nparam
+2);
721 Vector
*fixed_z
= Vector_Alloc(2);
722 for (int i
= 0; i
< 2; ++i
) {
723 Vector_Combine(vertex
[i
]->Vertex
->p
[0], vertex
[i
]->Vertex
->p
[1],
724 z
->p
[i
], base
->p
[0], base
->p
[1], nparam
+1);
725 value_multiply(z
->p
[i
][nparam
+1],
726 det
, vertex
[i
]->Vertex
->p
[0][nparam
+1]);
727 Inner_Product(z
->p
[i
], inner
, nparam
+1, &fixed_z
->p
[i
]);
730 /* Put on a common denominator */
731 value_multiply(fixed_z
->p
[0], fixed_z
->p
[0], z
->p
[1][nparam
+1]);
732 value_multiply(fixed_z
->p
[1], fixed_z
->p
[1], z
->p
[0][nparam
+1]);
733 /* Make sure z->p[0] is smaller than z->p[1] (for an internal
734 * point of the chamber and hence for all parameter values in
735 * the chamber), to ensure we integrate in the right direction.
737 if (value_lt(fixed_z
->p
[1], fixed_z
->p
[0]))
738 Vector_Exchange(z
->p
[0], z
->p
[1], nparam
+2);
739 Vector_Free(fixed_z
);
742 subs_0d
[1] = affine2evalue(z
->p
[1], z
->p
[1][nparam
+1], nparam
);
743 evalue
*up
= evalue_dup(res
);
744 evalue_substitute(up
, subs_0d
+1);
745 evalue_free(subs_0d
[1]);
747 subs_0d
[1] = affine2evalue(z
->p
[0], z
->p
[0][nparam
+1], nparam
);
748 evalue_substitute(res
, subs_0d
+1);
751 evalue_free(subs_0d
[1]);
761 /* Integrate the polynomial over the whole polygon using
762 * the Green-Stokes theorem.
764 void summator_2d::integrate(Param_Polyhedron
*PP
, unsigned *facets
,
768 evalue
*res
= evalue_zero();
772 evalue
*I
= evalue_dup(polynomial
);
773 evalue_anti_derive(I
, 0);
776 Vector
*normal
= Vector_Alloc(2);
777 Vector
*dir
= Vector_Alloc(2);
778 Matrix
*v0v1
= Matrix_Alloc(2, nparam
+2);
779 Vector
*f_v0v1
= Vector_Alloc(2);
780 Vector
*s
= Vector_Alloc(1+nparam
+2);
781 for (i
= 0, ix
= 0, bx
= MSB
; i
< PP
->Constraints
->NbRows
; ++i
) {
784 Param_Vertices
*vertex
[2];
786 if (!(facets
[ix
] & bx
)) {
791 Vector_Copy(PP
->Constraints
->p
[i
]+1, normal
->p
, 2);
793 if (value_zero_p(normal
->p
[0]))
796 Vector_Normalize(normal
->p
, 2);
797 value_assign(dir
->p
[0], normal
->p
[1]);
798 value_oppose(dir
->p
[1], normal
->p
[0]);
800 FD
= Param_Polyhedron_Facet(PP
, PD
, PP
->Constraints
->p
[i
]);
802 FORALL_PVertex_in_ParamPolyhedron(V
, FD
, PP
)
804 END_FORALL_PVertex_in_ParamPolyhedron
;
808 Param_Vertex_Common_Denominator(vertex
[0]);
809 Param_Vertex_Common_Denominator(vertex
[1]);
811 value_oppose(tmp
, vertex
[1]->Vertex
->p
[0][nparam
+1]);
812 for (int i
= 0; i
< 2; ++i
)
813 Vector_Combine(vertex
[1]->Vertex
->p
[i
],
814 vertex
[0]->Vertex
->p
[i
],
816 vertex
[0]->Vertex
->p
[0][nparam
+1], tmp
, nparam
+1);
817 value_multiply(v0v1
->p
[0][nparam
+1],
818 vertex
[0]->Vertex
->p
[0][nparam
+1],
819 vertex
[1]->Vertex
->p
[0][nparam
+1]);
820 value_assign(v0v1
->p
[1][nparam
+1], v0v1
->p
[0][nparam
+1]);
822 /* Order vertices to ensure we integrate in the right
823 * direction, i.e., with the polytope "on the left".
825 for (int i
= 0; i
< 2; ++i
)
826 Inner_Product(v0v1
->p
[i
], inner
, nparam
+1, &f_v0v1
->p
[i
]);
828 Inner_Product(dir
->p
, f_v0v1
->p
, 2, &tmp
);
829 if (value_neg_p(tmp
)) {
830 Param_Vertices
*PV
= vertex
[0];
831 vertex
[0] = vertex
[1];
833 for (int i
= 0; i
< 2; ++i
)
834 Vector_Oppose(v0v1
->p
[i
], v0v1
->p
[i
], nparam
+1);
836 value_oppose(tmp
, normal
->p
[0]);
837 if (value_neg_p(tmp
)) {
838 value_oppose(tmp
, tmp
);
839 Vector_Oppose(v0v1
->p
[1], v0v1
->p
[1], nparam
+1);
841 value_multiply(tmp
, tmp
, v0v1
->p
[1][nparam
+1]);
842 evalue
*top
= affine2evalue(v0v1
->p
[1], tmp
, nparam
);
844 value_multiply(s
->p
[0], normal
->p
[1], vertex
[0]->Vertex
->p
[0][nparam
+1]);
845 Vector_Copy(vertex
[0]->Vertex
->p
[0], s
->p
+1, nparam
+2);
846 subs_1d
[0] = affine2evalue(s
->p
, s
->p
[1+nparam
+1], 1+nparam
);
847 value_multiply(s
->p
[0], normal
->p
[0], vertex
[0]->Vertex
->p
[0][nparam
+1]);
848 value_oppose(s
->p
[0], s
->p
[0]);
849 Vector_Copy(vertex
[0]->Vertex
->p
[1], s
->p
+1, nparam
+2);
850 subs_1d
[1] = affine2evalue(s
->p
, s
->p
[1+nparam
+1], 1+nparam
);
852 evalue
*d
= evalue_dup(I
);
853 evalue_substitute(d
, subs_1d
);
854 evalue_anti_derive(d
, 0);
856 evalue_free(subs_1d
[0]);
857 evalue_free(subs_1d
[1]);
860 evalue_substitute(d
, subs_0d
+1);
861 evalue_mul(d
, dir
->p
[1]);
862 evalue_free(subs_0d
[1]);
867 Param_Domain_Free(FD
);
882 evalue
*summate_over_1d_pdomain(evalue
*e
,
883 Param_Polyhedron
*PP
, Param_Domain
*PD
,
885 struct barvinok_options
*options
)
889 Param_Vertices
*vertex
[2];
890 unsigned nparam
= PP
->V
->Vertex
->NbColumns
-2;
891 evalue
*subs_0d
[1+nparam
];
894 unsigned degree
= total_degree(e
, 1);
896 for (int i
= 0; i
< nparam
; ++i
)
897 subs_0d
[1+i
] = evalue_var(i
);
899 FORALL_PVertex_in_ParamPolyhedron(V
, PD
, PP
)
901 END_FORALL_PVertex_in_ParamPolyhedron
;
904 Vector
*fixed
= Vector_Alloc(2);
905 for (int i
= 0; i
< 2; ++i
) {
906 Inner_Product(vertex
[i
]->Vertex
->p
[0], inner
, nparam
+1, &fixed
->p
[i
]);
907 value_multiply(fixed
->p
[i
],
908 fixed
->p
[i
], vertex
[1-i
]->Vertex
->p
[0][nparam
+1]);
910 if (value_lt(fixed
->p
[1], fixed
->p
[0])) {
912 vertex
[0] = vertex
[1];
917 Vector
*coef
= Vector_Alloc(nparam
+1);
918 for (int i
= 0; i
< 2; ++i
)
919 a
[i
] = affine2evalue(vertex
[i
]->Vertex
->p
[0],
920 vertex
[i
]->Vertex
->p
[0][nparam
+1], nparam
);
921 if (value_one_p(vertex
[0]->Vertex
->p
[0][nparam
+1]))
922 t
[0] = evalue_zero();
924 Vector_Oppose(vertex
[0]->Vertex
->p
[0], coef
->p
, nparam
+1);
925 t
[0] = fractional_part(coef
->p
, vertex
[0]->Vertex
->p
[0][nparam
+1],
928 if (value_one_p(vertex
[1]->Vertex
->p
[0][nparam
+1]))
929 t
[1] = evalue_zero();
931 Vector_Copy(vertex
[1]->Vertex
->p
[0], coef
->p
, nparam
+1);
932 t
[1] = fractional_part(coef
->p
, vertex
[1]->Vertex
->p
[0][nparam
+1],
937 evalue
*I
= evalue_dup(e
);
938 evalue_anti_derive(I
, 0);
939 evalue
*up
= evalue_dup(I
);
941 evalue_substitute(up
, subs_0d
);
944 evalue_substitute(I
, subs_0d
);
951 mu_1d
mu0(degree
, t
[0]);
952 mu_1d
mu1(degree
, t
[1]);
954 evalue
*dx
= evalue_dup(e
);
955 for (int n
= 0; !EVALUE_IS_ZERO(*dx
); ++n
) {
960 evalue_substitute(d
, subs_0d
);
961 emul(mu0
.coefficient(n
), d
);
967 evalue_substitute(d
, subs_0d
);
968 emul(mu1
.coefficient(n
), d
);
974 evalue_derive(dx
, 0);
978 for (int i
= 0; i
< nparam
; ++i
)
979 evalue_free(subs_0d
[1+i
]);
981 for (int i
= 0; i
< 2; ++i
) {
989 #define INT_BITS (sizeof(unsigned) * 8)
991 static unsigned *active_constraints(Param_Polyhedron
*PP
, Param_Domain
*D
)
993 int len
= (PP
->Constraints
->NbRows
+INT_BITS
-1)/INT_BITS
;
994 unsigned *facets
= (unsigned *)calloc(len
, sizeof(unsigned));
997 FORALL_PVertex_in_ParamPolyhedron(V
, D
, PP
)
999 Param_Vertex_Set_Facets(PP
, V
);
1000 for (int i
= 0; i
< len
; ++i
)
1001 facets
[i
] |= V
->Facets
[i
];
1002 END_FORALL_PVertex_in_ParamPolyhedron
;
1007 static evalue
*summate_over_domain(evalue
*e
, int nvar
, Polyhedron
*D
,
1008 struct barvinok_options
*options
)
1011 Param_Polyhedron
*PP
;
1015 struct evalue_section
*s
;
1018 MaxRays
= options
->MaxRays
;
1019 POL_UNSET(options
->MaxRays
, POL_INTEGER
);
1021 U
= Universe_Polyhedron(D
->Dimension
- nvar
);
1022 PP
= Polyhedron2Param_Polyhedron(D
, U
, options
);
1024 for (nd
= 0, PD
= PP
->D
; PD
; ++nd
, PD
= PD
->next
);
1025 s
= ALLOCN(struct evalue_section
, nd
);
1027 Polyhedron
*TC
= true_context(D
, U
, MaxRays
);
1028 FORALL_REDUCED_DOMAIN(PP
, TC
, nd
, options
, i
, PD
, rVD
)
1031 facets
= active_constraints(PP
, PD
);
1033 Vector
*inner
= inner_point(rVD
);
1037 s
[i
].E
= summate_over_1d_pdomain(e
, PP
, PD
, inner
->p
+1, options
);
1038 } else if (nvar
== 2) {
1039 summator_2d
s2d(e
, PP
, inner
->p
+1, rVD
->Dimension
);
1041 s
[i
].E
= s2d
.summate_over_pdomain(PP
, facets
, PD
, options
);
1046 END_FORALL_REDUCED_DOMAIN
1047 Polyhedron_Free(TC
);
1049 Param_Polyhedron_Free(PP
);
1051 options
->MaxRays
= MaxRays
;
1053 res
= evalue_from_section_array(s
, nd
);
1059 evalue
*euler_summate(evalue
*e
, unsigned nvar
,
1060 struct barvinok_options
*options
)
1068 if (nvar
== 0 || EVALUE_IS_ZERO(*e
))
1069 return evalue_dup(e
);
1071 assert(value_zero_p(e
->d
));
1072 assert(e
->x
.p
->type
== partition
);
1074 res
= evalue_zero();
1076 for (i
= 0; i
< e
->x
.p
->size
/2; ++i
) {
1078 t
= summate_over_domain(&e
->x
.p
->arr
[2*i
+1], nvar
,
1079 EVALUE_DOMAIN(e
->x
.p
->arr
[2*i
]), options
);