2 #include <barvinok/options.h>
3 #include <barvinok/util.h>
6 #define ALLOC(type) (type*)malloc(sizeof(type))
7 #define ALLOCN(type,n) (type*)malloc((n) * sizeof(type))
9 static struct bernoulli_coef bernoulli_coef
;
10 static struct poly_list bernoulli
;
11 static struct poly_list faulhaber
;
13 struct bernoulli_coef
*bernoulli_coef_compute(int n
)
18 if (n
< bernoulli_coef
.n
)
19 return &bernoulli_coef
;
21 if (n
>= bernoulli_coef
.size
) {
22 int size
= 3*(n
+ 5)/2;
25 b
= Vector_Alloc(size
);
26 if (bernoulli_coef
.n
) {
27 Vector_Copy(bernoulli_coef
.num
->p
, b
->p
, bernoulli_coef
.n
);
28 Vector_Free(bernoulli_coef
.num
);
30 bernoulli_coef
.num
= b
;
31 b
= Vector_Alloc(size
);
32 if (bernoulli_coef
.n
) {
33 Vector_Copy(bernoulli_coef
.den
->p
, b
->p
, bernoulli_coef
.n
);
34 Vector_Free(bernoulli_coef
.den
);
36 bernoulli_coef
.den
= b
;
37 b
= Vector_Alloc(size
);
38 if (bernoulli_coef
.n
) {
39 Vector_Copy(bernoulli_coef
.lcm
->p
, b
->p
, bernoulli_coef
.n
);
40 Vector_Free(bernoulli_coef
.lcm
);
42 bernoulli_coef
.lcm
= b
;
44 bernoulli_coef
.size
= size
;
48 for (i
= bernoulli_coef
.n
; i
<= n
; ++i
) {
50 value_set_si(bernoulli_coef
.num
->p
[0], 1);
51 value_set_si(bernoulli_coef
.den
->p
[0], 1);
52 value_set_si(bernoulli_coef
.lcm
->p
[0], 1);
55 value_set_si(bernoulli_coef
.num
->p
[i
], 0);
56 value_set_si(factor
, -(i
+1));
57 for (j
= i
-1; j
>= 0; --j
) {
58 mpz_mul_ui(factor
, factor
, j
+1);
59 mpz_divexact_ui(factor
, factor
, i
+1-j
);
60 value_division(tmp
, bernoulli_coef
.lcm
->p
[i
-1],
61 bernoulli_coef
.den
->p
[j
]);
62 value_multiply(tmp
, tmp
, bernoulli_coef
.num
->p
[j
]);
63 value_multiply(tmp
, tmp
, factor
);
64 value_addto(bernoulli_coef
.num
->p
[i
], bernoulli_coef
.num
->p
[i
], tmp
);
66 mpz_mul_ui(bernoulli_coef
.den
->p
[i
], bernoulli_coef
.lcm
->p
[i
-1], i
+1);
67 value_gcd(tmp
, bernoulli_coef
.num
->p
[i
], bernoulli_coef
.den
->p
[i
]);
68 if (value_notone_p(tmp
)) {
69 value_division(bernoulli_coef
.num
->p
[i
],
70 bernoulli_coef
.num
->p
[i
], tmp
);
71 value_division(bernoulli_coef
.den
->p
[i
],
72 bernoulli_coef
.den
->p
[i
], tmp
);
74 value_lcm(bernoulli_coef
.lcm
->p
[i
],
75 bernoulli_coef
.lcm
->p
[i
-1], bernoulli_coef
.den
->p
[i
]);
77 bernoulli_coef
.n
= n
+1;
81 return &bernoulli_coef
;
85 * Compute either Bernoulli B_n or Faulhaber F_n polynomials.
87 * B_n = sum_{k=0}^n { n \choose k } b_k x^{n-k}
88 * F_n = 1/(n+1) sum_{k=0}^n { n+1 \choose k } b_k x^{n+1-k}
90 static struct poly_list
*bernoulli_faulhaber_compute(int n
, struct poly_list
*pl
,
95 struct bernoulli_coef
*bc
;
101 int size
= 3*(n
+ 5)/2;
104 poly
= ALLOCN(Vector
*, size
);
105 for (i
= 0; i
< pl
->n
; ++i
)
106 poly
[i
] = pl
->poly
[i
];
113 bc
= bernoulli_coef_compute(n
);
116 for (i
= pl
->n
; i
<= n
; ++i
) {
117 pl
->poly
[i
] = Vector_Alloc(i
+faulhaber
+2);
118 value_assign(pl
->poly
[i
]->p
[i
+faulhaber
], bc
->lcm
->p
[i
]);
120 mpz_mul_ui(pl
->poly
[i
]->p
[i
+2], bc
->lcm
->p
[i
], i
+1);
122 value_assign(pl
->poly
[i
]->p
[i
+1], bc
->lcm
->p
[i
]);
123 value_set_si(factor
, 1);
124 for (j
= 1; j
<= i
; ++j
) {
125 mpz_mul_ui(factor
, factor
, i
+faulhaber
+1-j
);
126 mpz_divexact_ui(factor
, factor
, j
);
127 value_division(pl
->poly
[i
]->p
[i
+faulhaber
-j
],
128 bc
->lcm
->p
[i
], bc
->den
->p
[j
]);
129 value_multiply(pl
->poly
[i
]->p
[i
+faulhaber
-j
],
130 pl
->poly
[i
]->p
[i
+faulhaber
-j
], bc
->num
->p
[j
]);
131 value_multiply(pl
->poly
[i
]->p
[i
+faulhaber
-j
],
132 pl
->poly
[i
]->p
[i
+faulhaber
-j
], factor
);
134 Vector_Normalize(pl
->poly
[i
]->p
, i
+faulhaber
+2);
142 struct poly_list
*bernoulli_compute(int n
)
144 return bernoulli_faulhaber_compute(n
, &bernoulli
, 0);
147 struct poly_list
*faulhaber_compute(int n
)
149 return bernoulli_faulhaber_compute(n
, &faulhaber
, 1);
152 /* shift variables in polynomial one down */
153 static void shift(evalue
*e
)
156 if (value_notzero_p(e
->d
))
158 assert(e
->x
.p
->type
== polynomial
);
159 assert(e
->x
.p
->pos
> 1);
161 for (i
= 0; i
< e
->x
.p
->size
; ++i
)
162 shift(&e
->x
.p
->arr
[i
]);
165 static evalue
*shifted_copy(evalue
*src
)
167 evalue
*e
= ALLOC(evalue
);
174 static evalue
*power_sums(struct poly_list
*faulhaber
, evalue
*poly
,
175 Vector
*arg
, Value denom
, int neg
, int alt_neg
)
178 evalue
*base
= affine2evalue(arg
->p
, denom
, arg
->Size
-1);
179 evalue
*sum
= evalue_zero();
181 for (i
= 1; i
< poly
->x
.p
->size
; ++i
) {
182 evalue
*term
= evalue_polynomial(faulhaber
->poly
[i
], base
);
183 evalue
*factor
= shifted_copy(&poly
->x
.p
->arr
[i
]);
185 if (alt_neg
&& (i
% 2))
198 struct Bernoulli_data
{
200 struct evalue_section
*s
;
206 static void Bernoulli_cb(Matrix
*M
, Value
*lower
, Value
*upper
, void *cb_data
)
208 struct Bernoulli_data
*data
= (struct Bernoulli_data
*)cb_data
;
211 evalue
*factor
= NULL
;
212 evalue
*linear
= NULL
;
215 unsigned dim
= M
->NbColumns
-2;
218 assert(data
->ns
< data
->size
);
221 T
= Constraints2Polyhedron(M2
, data
->MaxRays
);
224 POL_ENSURE_VERTICES(T
);
230 assert(lower
!= upper
);
232 row
= Vector_Alloc(dim
+1);
234 if (value_notzero_p(data
->e
->d
)) {
238 assert(data
->e
->x
.p
->type
== polynomial
);
239 if (data
->e
->x
.p
->pos
> 1) {
240 factor
= shifted_copy(data
->e
);
243 factor
= shifted_copy(&data
->e
->x
.p
->arr
[0]);
245 if (!EVALUE_IS_ZERO(*factor
)) {
246 value_absolute(tmp
, upper
[1]);
248 Vector_Combine(lower
+2, upper
+2, row
->p
, tmp
, lower
[1], dim
+1);
249 value_multiply(tmp
, tmp
, lower
[1]);
250 /* upper - lower + 1 */
251 value_addto(row
->p
[dim
], row
->p
[dim
], tmp
);
253 linear
= affine2evalue(row
->p
, tmp
, dim
);
254 emul(factor
, linear
);
256 linear
= evalue_zero();
259 data
->s
[data
->ns
].E
= linear
;
260 data
->s
[data
->ns
].D
= T
;
263 evalue
*poly_u
= NULL
, *poly_l
= NULL
;
265 struct poly_list
*faulhaber
;
266 assert(data
->e
->x
.p
->type
== polynomial
);
267 assert(data
->e
->x
.p
->pos
== 1);
268 faulhaber
= faulhaber_compute(data
->e
->x
.p
->size
-1);
269 /* lower is the constraint
270 * b i - l' >= 0 i >= l'/b = l
271 * upper is the constraint
272 * -a i + u' >= 0 i <= u'/a = u
274 M2
= Matrix_Alloc(3, 2+T
->Dimension
);
275 value_set_si(M2
->p
[0][0], 1);
276 value_set_si(M2
->p
[1][0], 1);
277 value_set_si(M2
->p
[2][0], 1);
281 Vector_Oppose(lower
+2, M2
->p
[0]+1, T
->Dimension
+1);
282 value_subtract(M2
->p
[0][1+T
->Dimension
], M2
->p
[0][1+T
->Dimension
],
284 D
= AddConstraints(M2
->p_Init
, 1, T
, data
->MaxRays
);
290 Vector_Copy(upper
+2, row
->p
, dim
+1);
291 value_oppose(tmp
, upper
[1]);
292 value_addto(row
->p
[dim
], row
->p
[dim
], tmp
);
293 poly_u
= power_sums(faulhaber
, data
->e
, row
, tmp
, 0, 0);
295 Vector_Oppose(lower
+2, row
->p
, dim
+1);
296 extra
= power_sums(faulhaber
, data
->e
, row
, lower
[1], 1, 0);
300 data
->s
[data
->ns
].E
= extra
;
301 data
->s
[data
->ns
].D
= D
;
306 * 1 <= -u -u' - a >= 0
308 Vector_Oppose(upper
+2, M2
->p
[0]+1, T
->Dimension
+1);
309 value_addto(M2
->p
[0][1+T
->Dimension
], M2
->p
[0][1+T
->Dimension
],
311 D
= AddConstraints(M2
->p_Init
, 1, T
, data
->MaxRays
);
317 Vector_Copy(lower
+2, row
->p
, dim
+1);
318 value_addto(row
->p
[dim
], row
->p
[dim
], lower
[1]);
319 poly_l
= power_sums(faulhaber
, data
->e
, row
, lower
[1], 0, 1);
321 Vector_Oppose(upper
+2, row
->p
, dim
+1);
322 value_oppose(tmp
, upper
[1]);
323 extra
= power_sums(faulhaber
, data
->e
, row
, tmp
, 1, 1);
327 data
->s
[data
->ns
].E
= extra
;
328 data
->s
[data
->ns
].D
= D
;
336 Vector_Copy(upper
+2, M2
->p
[0]+1, T
->Dimension
+1);
337 Vector_Copy(lower
+2, M2
->p
[1]+1, T
->Dimension
+1);
338 D
= AddConstraints(M2
->p_Init
, 2, T
, data
->MaxRays
);
343 Vector_Copy(lower
+2, row
->p
, dim
+1);
344 value_addto(row
->p
[dim
], row
->p
[dim
], lower
[1]);
345 poly_l
= power_sums(faulhaber
, data
->e
, row
, lower
[1], 0, 1);
348 Vector_Copy(upper
+2, row
->p
, dim
+1);
349 value_oppose(tmp
, upper
[1]);
350 value_addto(row
->p
[dim
], row
->p
[dim
], tmp
);
351 poly_u
= power_sums(faulhaber
, data
->e
, row
, tmp
, 0, 0);
354 data
->s
[data
->ns
].E
= ALLOC(evalue
);
355 value_init(data
->s
[data
->ns
].E
->d
);
356 evalue_copy(data
->s
[data
->ns
].E
, poly_u
);
357 eadd(poly_l
, data
->s
[data
->ns
].E
);
358 eadd(linear
, data
->s
[data
->ns
].E
);
359 data
->s
[data
->ns
].D
= D
;
364 * l < 1 -l' + b - 1 >= 0
367 Vector_Copy(lower
+2, M2
->p
[0]+1, T
->Dimension
+1);
368 value_addto(M2
->p
[0][1+T
->Dimension
], M2
->p
[0][1+T
->Dimension
], lower
[1]);
369 value_decrement(M2
->p
[0][1+T
->Dimension
], M2
->p
[0][1+T
->Dimension
]);
370 Vector_Oppose(lower
+2, M2
->p
[1]+1, T
->Dimension
+1);
371 value_decrement(M2
->p
[1][1+T
->Dimension
], M2
->p
[1][1+T
->Dimension
]);
372 D
= AddConstraints(M2
->p_Init
, 2, T
, data
->MaxRays
);
377 Vector_Copy(upper
+2, row
->p
, dim
+1);
378 value_oppose(tmp
, upper
[1]);
379 value_addto(row
->p
[dim
], row
->p
[dim
], tmp
);
380 poly_u
= power_sums(faulhaber
, data
->e
, row
, tmp
, 0, 0);
383 eadd(linear
, poly_u
);
384 data
->s
[data
->ns
].E
= poly_u
;
386 data
->s
[data
->ns
].D
= D
;
391 * -u < 1 u' + a - 1 >= 0
392 * 0 < -u -u' - 1 >= 0
395 Vector_Copy(upper
+2, M2
->p
[0]+1, T
->Dimension
+1);
396 value_subtract(M2
->p
[0][1+T
->Dimension
], M2
->p
[0][1+T
->Dimension
],
398 value_decrement(M2
->p
[0][1+T
->Dimension
], M2
->p
[0][1+T
->Dimension
]);
399 Vector_Oppose(upper
+2, M2
->p
[1]+1, T
->Dimension
+1);
400 value_decrement(M2
->p
[1][1+T
->Dimension
], M2
->p
[1][1+T
->Dimension
]);
401 Vector_Copy(lower
+2, M2
->p
[2]+1, T
->Dimension
+1);
402 D
= AddConstraints(M2
->p_Init
, 3, T
, data
->MaxRays
);
407 Vector_Copy(lower
+2, row
->p
, dim
+1);
408 value_addto(row
->p
[dim
], row
->p
[dim
], lower
[1]);
409 poly_l
= power_sums(faulhaber
, data
->e
, row
, lower
[1], 0, 1);
412 eadd(linear
, poly_l
);
413 data
->s
[data
->ns
].E
= poly_l
;
415 data
->s
[data
->ns
].D
= D
;
427 if (factor
!= data
->e
)
433 /* Looks for variable with integer bounds, i.e., with coefficients 0, 1 or -1.
434 * Returns 1 if such a variable is found and puts it in the first position,
435 * possibly changing *P_p and *E_p.
437 static int find_integer_bounds(Polyhedron
**P_p
, evalue
**E_p
, unsigned nvar
)
439 Polyhedron
*P
= *P_p
;
441 unsigned dim
= P
->Dimension
;
444 for (i
= 0; i
< nvar
; ++i
) {
445 for (j
= 0; j
< P
->NbConstraints
; ++j
) {
446 if (value_zero_p(P
->Constraint
[j
][1+i
]))
448 if (value_one_p(P
->Constraint
[j
][1+i
]))
450 if (value_mone_p(P
->Constraint
[j
][1+i
]))
454 if (j
== P
->NbConstraints
)
461 P
= Polyhedron_Copy(P
);
462 Polyhedron_ExchangeColumns(P
, 1, 1+i
);
465 if (value_zero_p(E
->d
)) {
467 subs
= ALLOCN(evalue
*, dim
);
468 for (j
= 0; j
< dim
; ++j
)
469 subs
[j
] = evalue_var(j
);
473 E
= evalue_dup(*E_p
);
474 evalue_substitute(E
, subs
);
475 for (j
= 0; j
< dim
; ++j
)
476 evalue_free(subs
[j
]);
484 static evalue
*sum_over_polytope(Polyhedron
*P
, evalue
*E
, unsigned nvar
,
485 struct Bernoulli_data
*data
,
486 struct barvinok_options
*options
)
488 unsigned dim
= P
->Dimension
- 1;
491 if (value_zero_p(P
->Constraint
[0][0]) &&
492 value_notzero_p(P
->Constraint
[0][1])) {
495 value_set_si(res
->d
, 0);
496 res
->x
.p
= new_enode(partition
, 2, dim
);
497 EVALUE_SET_DOMAIN(res
->x
.p
->arr
[0], Polyhedron_Project(P
, dim
));
498 evalue_copy(&res
->x
.p
->arr
[1], E
);
499 reduce_evalue_in_domain(&res
->x
.p
->arr
[1], P
);
500 shift(&res
->x
.p
->arr
[1]);
505 for_each_lower_upper_bound(P
, Bernoulli_cb
, data
);
507 res
= evalue_from_section_array(data
->s
, data
->ns
);
511 evalue
*tmp
= Bernoulli_sum_evalue(res
, nvar
-1, options
);
519 evalue
*Bernoulli_sum_evalue(evalue
*e
, unsigned nvar
,
520 struct barvinok_options
*options
)
522 struct Bernoulli_data data
;
524 evalue
*sum
= evalue_zero();
526 if (EVALUE_IS_ZERO(*e
))
534 assert(value_zero_p(e
->d
));
535 assert(e
->x
.p
->type
== partition
);
537 data
.size
= e
->x
.p
->size
* 2 + 16;
538 data
.s
= ALLOCN(struct evalue_section
, data
.size
);
539 data
.MaxRays
= options
->MaxRays
;
541 for (i
= 0; i
< e
->x
.p
->size
/2; ++i
) {
543 for (D
= EVALUE_DOMAIN(e
->x
.p
->arr
[2*i
]); D
; D
= D
->next
) {
544 evalue
*E
= &e
->x
.p
->arr
[2*i
+1];
546 Polyhedron
*next
= D
->next
;
552 integer_bounds
= find_integer_bounds(&P
, &E
, nvar
);
553 if (options
->approximation_method
== BV_APPROX_NONE
&&
558 evalue
*tmp
= sum_over_polytope(P
, E
, nvar
, &data
, options
);
565 if (E
!= &e
->x
.p
->arr
[2*i
+1])
585 evalue
*Bernoulli_sum(Polyhedron
*P
, Polyhedron
*C
,
586 struct barvinok_options
*options
)
592 e
.x
.p
= new_enode(partition
, 2, P
->Dimension
);
593 EVALUE_SET_DOMAIN(e
.x
.p
->arr
[0], Polyhedron_Copy(P
));
594 evalue_set_si(&e
.x
.p
->arr
[1], 1, 1);
595 sum
= Bernoulli_sum_evalue(&e
, P
->Dimension
- C
->Dimension
, options
);
596 free_evalue_refs(&e
);