2 #include <barvinok/barvinok.h>
3 #include <barvinok/options.h>
4 #include <barvinok/util.h>
6 #include "lattice_point.h"
7 #include "section_array.h"
9 #define ALLOC(type) (type*)malloc(sizeof(type))
10 #define ALLOCN(type,n) (type*)malloc((n) * sizeof(type))
12 static struct bernoulli_coef bernoulli_coef
;
13 static struct poly_list bernoulli
;
14 static struct poly_list faulhaber
;
16 struct bernoulli_coef
*bernoulli_coef_compute(int n
)
21 if (n
< bernoulli_coef
.n
)
22 return &bernoulli_coef
;
24 if (n
>= bernoulli_coef
.size
) {
25 int size
= 3*(n
+ 5)/2;
28 b
= Vector_Alloc(size
);
29 if (bernoulli_coef
.n
) {
30 Vector_Copy(bernoulli_coef
.num
->p
, b
->p
, bernoulli_coef
.n
);
31 Vector_Free(bernoulli_coef
.num
);
33 bernoulli_coef
.num
= b
;
34 b
= Vector_Alloc(size
);
35 if (bernoulli_coef
.n
) {
36 Vector_Copy(bernoulli_coef
.den
->p
, b
->p
, bernoulli_coef
.n
);
37 Vector_Free(bernoulli_coef
.den
);
39 bernoulli_coef
.den
= b
;
40 b
= Vector_Alloc(size
);
41 if (bernoulli_coef
.n
) {
42 Vector_Copy(bernoulli_coef
.lcm
->p
, b
->p
, bernoulli_coef
.n
);
43 Vector_Free(bernoulli_coef
.lcm
);
45 bernoulli_coef
.lcm
= b
;
47 bernoulli_coef
.size
= size
;
51 for (i
= bernoulli_coef
.n
; i
<= n
; ++i
) {
53 value_set_si(bernoulli_coef
.num
->p
[0], 1);
54 value_set_si(bernoulli_coef
.den
->p
[0], 1);
55 value_set_si(bernoulli_coef
.lcm
->p
[0], 1);
58 value_set_si(bernoulli_coef
.num
->p
[i
], 0);
59 value_set_si(factor
, -(i
+1));
60 for (j
= i
-1; j
>= 0; --j
) {
61 mpz_mul_ui(factor
, factor
, j
+1);
62 mpz_divexact_ui(factor
, factor
, i
+1-j
);
63 value_division(tmp
, bernoulli_coef
.lcm
->p
[i
-1],
64 bernoulli_coef
.den
->p
[j
]);
65 value_multiply(tmp
, tmp
, bernoulli_coef
.num
->p
[j
]);
66 value_multiply(tmp
, tmp
, factor
);
67 value_addto(bernoulli_coef
.num
->p
[i
], bernoulli_coef
.num
->p
[i
], tmp
);
69 mpz_mul_ui(bernoulli_coef
.den
->p
[i
], bernoulli_coef
.lcm
->p
[i
-1], i
+1);
70 value_gcd(tmp
, bernoulli_coef
.num
->p
[i
], bernoulli_coef
.den
->p
[i
]);
71 if (value_notone_p(tmp
)) {
72 value_division(bernoulli_coef
.num
->p
[i
],
73 bernoulli_coef
.num
->p
[i
], tmp
);
74 value_division(bernoulli_coef
.den
->p
[i
],
75 bernoulli_coef
.den
->p
[i
], tmp
);
77 value_lcm(bernoulli_coef
.lcm
->p
[i
],
78 bernoulli_coef
.lcm
->p
[i
-1], bernoulli_coef
.den
->p
[i
]);
80 bernoulli_coef
.n
= n
+1;
84 return &bernoulli_coef
;
88 * Compute either Bernoulli B_n or Faulhaber F_n polynomials.
90 * B_n = sum_{k=0}^n { n \choose k } b_k x^{n-k}
91 * F_n = 1/(n+1) sum_{k=0}^n { n+1 \choose k } b_k x^{n+1-k}
93 static struct poly_list
*bernoulli_faulhaber_compute(int n
, struct poly_list
*pl
,
98 struct bernoulli_coef
*bc
;
104 int size
= 3*(n
+ 5)/2;
107 poly
= ALLOCN(Vector
*, size
);
108 for (i
= 0; i
< pl
->n
; ++i
)
109 poly
[i
] = pl
->poly
[i
];
116 bc
= bernoulli_coef_compute(n
);
119 for (i
= pl
->n
; i
<= n
; ++i
) {
120 pl
->poly
[i
] = Vector_Alloc(i
+faulhaber
+2);
121 value_assign(pl
->poly
[i
]->p
[i
+faulhaber
], bc
->lcm
->p
[i
]);
123 mpz_mul_ui(pl
->poly
[i
]->p
[i
+2], bc
->lcm
->p
[i
], i
+1);
125 value_assign(pl
->poly
[i
]->p
[i
+1], bc
->lcm
->p
[i
]);
126 value_set_si(factor
, 1);
127 for (j
= 1; j
<= i
; ++j
) {
128 mpz_mul_ui(factor
, factor
, i
+faulhaber
+1-j
);
129 mpz_divexact_ui(factor
, factor
, j
);
130 value_division(pl
->poly
[i
]->p
[i
+faulhaber
-j
],
131 bc
->lcm
->p
[i
], bc
->den
->p
[j
]);
132 value_multiply(pl
->poly
[i
]->p
[i
+faulhaber
-j
],
133 pl
->poly
[i
]->p
[i
+faulhaber
-j
], bc
->num
->p
[j
]);
134 value_multiply(pl
->poly
[i
]->p
[i
+faulhaber
-j
],
135 pl
->poly
[i
]->p
[i
+faulhaber
-j
], factor
);
137 Vector_Normalize(pl
->poly
[i
]->p
, i
+faulhaber
+2);
145 struct poly_list
*bernoulli_compute(int n
)
147 return bernoulli_faulhaber_compute(n
, &bernoulli
, 0);
150 struct poly_list
*faulhaber_compute(int n
)
152 return bernoulli_faulhaber_compute(n
, &faulhaber
, 1);
155 static evalue
*shifted_copy(const evalue
*src
)
157 evalue
*e
= ALLOC(evalue
);
160 evalue_shift_variables(e
, -1);
164 /* Computes the argument for the Faulhaber polynomial.
165 * If we are computing a polynomial approximation (exact == 0),
166 * then this is simply arg/denom.
167 * Otherwise, if neg == 0, then we are dealing with an upper bound
168 * and we need to compute floor(arg/denom) = arg/denom - { arg/denom }
169 * If neg == 1, then we are dealing with a lower bound
170 * and we need to compute ceil(arg/denom) = arg/denom + { -arg/denom }
172 * Modifies arg (if exact == 1).
174 static evalue
*power_sums_base(Vector
*arg
, Value denom
, int neg
, int exact
)
177 evalue
*base
= affine2evalue(arg
->p
, denom
, arg
->Size
-1);
179 if (!exact
|| value_one_p(denom
))
183 Vector_Oppose(arg
->p
, arg
->p
, arg
->Size
);
185 fract
= fractional_part(arg
->p
, denom
, arg
->Size
-1, NULL
);
187 value_set_si(arg
->p
[0], -1);
188 evalue_mul(fract
, arg
->p
[0]);
196 static evalue
*power_sums(struct poly_list
*faulhaber
, const evalue
*poly
,
197 Vector
*arg
, Value denom
, int neg
, int alt_neg
,
201 evalue
*base
= power_sums_base(arg
, denom
, neg
, exact
);
202 evalue
*sum
= evalue_zero();
204 for (i
= 1; i
< poly
->x
.p
->size
; ++i
) {
205 evalue
*term
= evalue_polynomial(faulhaber
->poly
[i
], base
);
206 evalue
*factor
= shifted_copy(&poly
->x
.p
->arr
[i
]);
208 if (alt_neg
&& (i
% 2))
221 /* Given a constraint (cst_affine) a x + b y + c >= 0, compate a constraint (c)
222 * +- (b y + c) +- a >=,> 0
225 * sign_affine sign_cst
227 static void bound_constraint(Value
*c
, unsigned dim
,
229 int sign_affine
, int sign_cst
, int strict
)
231 if (sign_affine
== -1)
232 Vector_Oppose(cst_affine
+1, c
, dim
+1);
234 Vector_Copy(cst_affine
+1, c
, dim
+1);
237 value_subtract(c
[dim
], c
[dim
], cst_affine
[0]);
238 else if (sign_cst
== 1)
239 value_addto(c
[dim
], c
[dim
], cst_affine
[0]);
242 value_decrement(c
[dim
], c
[dim
]);
245 struct Bernoulli_data
{
246 struct barvinok_options
*options
;
247 struct evalue_section_array
*sections
;
251 static evalue
*compute_poly_u(evalue
*poly_u
, Value
*upper
, Vector
*row
,
252 unsigned dim
, Value tmp
,
253 struct poly_list
*faulhaber
,
254 struct Bernoulli_data
*data
)
256 int exact
= data
->options
->approximation_method
== BV_APPROX_NONE
;
259 Vector_Copy(upper
+2, row
->p
, dim
+1);
260 value_oppose(tmp
, upper
[1]);
261 value_addto(row
->p
[dim
], row
->p
[dim
], tmp
);
262 return power_sums(faulhaber
, data
->e
, row
, tmp
, 0, 0, exact
);
265 static evalue
*compute_poly_l(evalue
*poly_l
, Value
*lower
, Vector
*row
,
267 struct poly_list
*faulhaber
,
268 struct Bernoulli_data
*data
)
270 int exact
= data
->options
->approximation_method
== BV_APPROX_NONE
;
273 Vector_Copy(lower
+2, row
->p
, dim
+1);
274 value_addto(row
->p
[dim
], row
->p
[dim
], lower
[1]);
275 return power_sums(faulhaber
, data
->e
, row
, lower
[1], 0, 1, exact
);
278 /* Compute sum of constant term.
280 static evalue
*linear_term(const evalue
*cst
, Value
*lower
, Value
*upper
,
281 Vector
*row
, Value tmp
, int exact
)
284 unsigned dim
= row
->Size
- 1;
286 if (EVALUE_IS_ZERO(*cst
))
287 return evalue_zero();
290 value_absolute(tmp
, upper
[1]);
292 Vector_Combine(lower
+2, upper
+2, row
->p
, tmp
, lower
[1], dim
+1);
293 value_multiply(tmp
, tmp
, lower
[1]);
294 /* upper - lower + 1 */
295 value_addto(row
->p
[dim
], row
->p
[dim
], tmp
);
297 linear
= affine2evalue(row
->p
, tmp
, dim
);
301 value_absolute(tmp
, upper
[1]);
302 Vector_Copy(upper
+2, row
->p
, dim
+1);
303 value_addto(row
->p
[dim
], row
->p
[dim
], tmp
);
305 linear
= power_sums_base(row
, tmp
, 0, 1);
307 Vector_Copy(lower
+2, row
->p
, dim
+1);
309 l
= power_sums_base(row
, lower
[1], 0, 1);
311 /* floor(upper+1) + floor(-lower) = floor(upper) - ceil(lower) + 1 */
320 static void Bernoulli_init(unsigned n
, void *cb_data
)
322 struct Bernoulli_data
*data
= (struct Bernoulli_data
*)cb_data
;
323 int exact
= data
->options
->approximation_method
== BV_APPROX_NONE
;
324 int cases
= exact
? 3 : 5;
326 evalue_section_array_ensure(data
->sections
, cases
* n
);
329 static void Bernoulli_cb(Matrix
*M
, Value
*lower
, Value
*upper
, void *cb_data
);
332 * This function requires that either the lower or the upper bound
333 * represented by the constraints "lower" and "upper" is an integer
335 * An affine substitution is performed to make this bound exactly
336 * zero, ensuring that in the recursive call to Bernoulli_cb,
337 * only one of the "cases" will apply.
339 static void transform_to_single_case(Matrix
*M
, Value
*lower
, Value
*upper
,
340 struct Bernoulli_data
*data
)
342 unsigned dim
= M
->NbColumns
-2;
346 const evalue
*e
= data
->e
;
352 subs
= ALLOCN(evalue
*, dim
+1);
353 for (i
= 0; i
< dim
; ++i
)
354 subs
[1+i
] = evalue_var(1+i
);
355 shadow
= Vector_Alloc(2 * (2+dim
+1));
356 if (value_one_p(lower
[1])) {
357 /* Replace i by i + l' when b = 1 */
358 value_set_si(shadow
->p
[0], 1);
359 Vector_Oppose(lower
+2, shadow
->p
+1, dim
+1);
360 subs
[0] = affine2evalue(shadow
->p
, shadow
->p
[0], dim
+1);
364 * (-a i + u') + a (-l') >= 0
366 value_assign(shadow
->p
[2+dim
+1+1], upper
[1]);
367 value_oppose(a
, upper
[1]);
369 Vector_Combine(upper
+2, lower
+2, shadow
->p
+2+dim
+1+2,
371 upper
= shadow
->p
+2+dim
+1;
373 value_set_si(lower
[1], 1);
374 Vector_Set(lower
+2, 0, dim
+1);
376 /* Replace i by i + u' when a = 1 */
377 value_set_si(shadow
->p
[0], 1);
378 Vector_Copy(upper
+2, shadow
->p
+1, dim
+1);
379 subs
[0] = affine2evalue(shadow
->p
, shadow
->p
[0], dim
+1);
381 * (b i - l') + b u' >= 0
385 value_assign(shadow
->p
[1], lower
[1]);
387 value_assign(b
, lower
[1]);
388 Vector_Combine(upper
+2, lower
+2, shadow
->p
+2,
390 upper
= shadow
->p
+2+dim
+1;
392 value_set_si(upper
[1], -1);
393 Vector_Set(upper
+2, 0, dim
+1);
398 t
= evalue_dup(data
->e
);
399 evalue_substitute(t
, subs
);
402 for (i
= 0; i
< dim
+1; ++i
)
403 evalue_free(subs
[i
]);
406 Bernoulli_cb(M
, lower
, upper
, data
);
413 static void Bernoulli_cb(Matrix
*M
, Value
*lower
, Value
*upper
, void *cb_data
)
415 struct Bernoulli_data
*data
= (struct Bernoulli_data
*)cb_data
;
416 struct evalue_section_array
*sections
= data
->sections
;
419 const evalue
*factor
= NULL
;
420 evalue
*linear
= NULL
;
423 unsigned dim
= M
->NbColumns
-2;
425 int exact
= data
->options
->approximation_method
== BV_APPROX_NONE
;
426 int cases
= exact
? 3 : 5;
430 evalue_section_array_ensure(sections
, sections
->ns
+ cases
);
433 T
= Constraints2Polyhedron(M2
, data
->options
->MaxRays
);
436 POL_ENSURE_VERTICES(T
);
442 constant
= value_notzero_p(data
->e
->d
) ||
443 data
->e
->x
.p
->type
!= polynomial
||
444 data
->e
->x
.p
->pos
!= 1;
445 if (!constant
&& (value_one_p(lower
[1]) || value_mone_p(upper
[1]))) {
447 int lower_cst
, upper_cst
;
449 lower_cst
= First_Non_Zero(lower
+2, dim
) == -1;
450 upper_cst
= First_Non_Zero(upper
+2, dim
) == -1;
452 (lower_cst
&& value_negz_p(lower
[2+dim
])) ||
453 (upper_cst
&& value_negz_p(upper
[2+dim
])) ||
454 (lower_cst
&& upper_cst
&&
455 value_posz_p(lower
[2+dim
]) && value_posz_p(upper
[2+dim
]));
457 transform_to_single_case(M
, lower
, upper
, data
);
463 assert(lower
!= upper
);
465 row
= Vector_Alloc(dim
+1);
467 if (value_notzero_p(data
->e
->d
)) {
471 if (data
->e
->x
.p
->type
== polynomial
&& data
->e
->x
.p
->pos
== 1)
472 factor
= shifted_copy(&data
->e
->x
.p
->arr
[0]);
474 factor
= shifted_copy(data
->e
);
478 linear
= linear_term(factor
, lower
, upper
, row
, tmp
, exact
);
481 evalue_section_array_add(sections
, T
, linear
);
482 data
->options
->stats
->bernoulli_sums
++;
484 evalue
*poly_u
= NULL
, *poly_l
= NULL
;
486 struct poly_list
*faulhaber
;
487 assert(data
->e
->x
.p
->type
== polynomial
);
488 assert(data
->e
->x
.p
->pos
== 1);
489 faulhaber
= faulhaber_compute(data
->e
->x
.p
->size
-1);
490 /* lower is the constraint
491 * b i - l' >= 0 i >= l'/b = l
492 * upper is the constraint
493 * -a i + u' >= 0 i <= u'/a = u
495 M2
= Matrix_Alloc(3, 2+T
->Dimension
);
496 value_set_si(M2
->p
[0][0], 1);
497 value_set_si(M2
->p
[1][0], 1);
498 value_set_si(M2
->p
[2][0], 1);
501 * 0 < l l' - 1 >= 0 if exact
504 bound_constraint(M2
->p
[0]+1, T
->Dimension
, lower
+1, -1, 0, 1);
506 bound_constraint(M2
->p
[0]+1, T
->Dimension
, lower
+1, -1, -1, 0);
507 D
= AddConstraints(M2
->p_Init
, 1, T
, data
->options
->MaxRays
);
508 POL_ENSURE_VERTICES(D
);
513 poly_u
= compute_poly_u(poly_u
, upper
, row
, dim
, tmp
,
515 Vector_Oppose(lower
+2, row
->p
, dim
+1);
516 extra
= power_sums(faulhaber
, data
->e
, row
, lower
[1], 1, 0, exact
);
520 evalue_section_array_add(sections
, D
, extra
);
521 data
->options
->stats
->bernoulli_sums
++;
525 * 1 <= -u -u' - a >= 0
526 * 0 < -u -u' - 1 >= 0 if exact
529 bound_constraint(M2
->p
[0]+1, T
->Dimension
, upper
+1, -1, 0, 1);
531 bound_constraint(M2
->p
[0]+1, T
->Dimension
, upper
+1, -1, 1, 0);
532 D
= AddConstraints(M2
->p_Init
, 1, T
, data
->options
->MaxRays
);
533 POL_ENSURE_VERTICES(D
);
538 poly_l
= compute_poly_l(poly_l
, lower
, row
, dim
, faulhaber
, data
);
539 Vector_Oppose(upper
+2, row
->p
, dim
+1);
540 value_oppose(tmp
, upper
[1]);
541 extra
= power_sums(faulhaber
, data
->e
, row
, tmp
, 1, 1, exact
);
545 evalue_section_array_add(sections
, D
, extra
);
546 data
->options
->stats
->bernoulli_sums
++;
553 bound_constraint(M2
->p
[0]+1, T
->Dimension
, upper
+1, 1, 0, 0);
554 bound_constraint(M2
->p
[1]+1, T
->Dimension
, lower
+1, 1, 0, 0);
555 D
= AddConstraints(M2
->p_Init
, 2, T
, data
->options
->MaxRays
);
556 POL_ENSURE_VERTICES(D
);
561 poly_l
= compute_poly_l(poly_l
, lower
, row
, dim
, faulhaber
, data
);
562 poly_u
= compute_poly_u(poly_u
, upper
, row
, dim
, tmp
,
567 evalue_copy(e
, poly_u
);
570 evalue_section_array_add(sections
, D
, e
);
571 data
->options
->stats
->bernoulli_sums
++;
576 * l < 1 -l' + b - 1 >= 0
580 bound_constraint(M2
->p
[0]+1, T
->Dimension
, lower
+1, 1, 1, 1);
581 bound_constraint(M2
->p
[1]+1, T
->Dimension
, lower
+1, -1, 0, 1);
582 bound_constraint(M2
->p
[2]+1, T
->Dimension
, upper
+1, 1, 1, 0);
583 D
= AddConstraints(M2
->p_Init
, 3, T
, data
->options
->MaxRays
);
584 POL_ENSURE_VERTICES(D
);
588 poly_u
= compute_poly_u(poly_u
, upper
, row
, dim
, tmp
,
590 eadd(linear
, poly_u
);
591 evalue_section_array_add(sections
, D
, poly_u
);
593 data
->options
->stats
->bernoulli_sums
++;
597 * -u < 1 u' + a - 1 >= 0
598 * 0 < -u -u' - 1 >= 0
599 * l <= -1 -l' - b >= 0
601 bound_constraint(M2
->p
[0]+1, T
->Dimension
, upper
+1, 1, -1, 1);
602 bound_constraint(M2
->p
[1]+1, T
->Dimension
, upper
+1, -1, 0, 1);
603 bound_constraint(M2
->p
[2]+1, T
->Dimension
, lower
+1, 1, -1, 0);
604 D
= AddConstraints(M2
->p_Init
, 3, T
, data
->options
->MaxRays
);
605 POL_ENSURE_VERTICES(D
);
609 poly_l
= compute_poly_l(poly_l
, lower
, row
, dim
,
611 eadd(linear
, poly_l
);
612 evalue_section_array_add(sections
, D
, poly_l
);
614 data
->options
->stats
->bernoulli_sums
++;
626 if (factor
!= data
->e
)
627 evalue_free((evalue
*)factor
);
633 * Move the variable at position pos to the front by exchanging
634 * that variable with the first variable.
636 static void more_var_to_front(Polyhedron
**P_p
, evalue
**E_p
, int pos
)
638 Polyhedron
*P
= *P_p
;
640 unsigned dim
= P
->Dimension
;
644 P
= Polyhedron_Copy(P
);
645 Polyhedron_ExchangeColumns(P
, 1, 1+pos
);
648 if (value_zero_p(E
->d
)) {
652 subs
= ALLOCN(evalue
*, dim
);
653 for (j
= 0; j
< dim
; ++j
)
654 subs
[j
] = evalue_var(j
);
658 E
= evalue_dup(*E_p
);
659 evalue_substitute(E
, subs
);
660 for (j
= 0; j
< dim
; ++j
)
661 evalue_free(subs
[j
]);
667 static int type_offset(enode
*p
)
669 return p
->type
== fractional
? 1 :
670 p
->type
== flooring
? 1 : 0;
673 static void adjust_periods(evalue
*e
, unsigned nvar
, Vector
*periods
)
675 for (; value_zero_p(e
->d
); e
= &e
->x
.p
->arr
[0]) {
677 assert(e
->x
.p
->type
== polynomial
);
678 assert(e
->x
.p
->size
== 2);
679 assert(value_notzero_p(e
->x
.p
->arr
[1].d
));
681 pos
= e
->x
.p
->pos
- 1;
685 value_lcm(periods
->p
[pos
], periods
->p
[pos
], e
->x
.p
->arr
[1].d
);
689 static void fractional_periods_r(evalue
*e
, unsigned nvar
, Vector
*periods
)
693 if (value_notzero_p(e
->d
))
696 assert(e
->x
.p
->type
== polynomial
|| e
->x
.p
->type
== fractional
);
698 if (e
->x
.p
->type
== fractional
)
699 adjust_periods(&e
->x
.p
->arr
[0], nvar
, periods
);
701 for (i
= type_offset(e
->x
.p
); i
< e
->x
.p
->size
; ++i
)
702 fractional_periods_r(&e
->x
.p
->arr
[i
], nvar
, periods
);
706 * For each of the nvar variables, compute the lcm of the
707 * denominators of the coefficients of that variable in
708 * any of the fractional parts.
710 static Vector
*fractional_periods(evalue
*e
, unsigned nvar
)
713 Vector
*periods
= Vector_Alloc(nvar
);
715 for (i
= 0; i
< nvar
; ++i
)
716 value_set_si(periods
->p
[i
], 1);
718 fractional_periods_r(e
, nvar
, periods
);
723 /* Move "best" variable to sum over into the first position,
724 * possibly changing *P_p and *E_p.
726 * If there are any fractional parts (period != NULL), then we
727 * first look for a variable with the smallest lcm of denominators
728 * inside a fractional part. This denominator is assigned to period
729 * and corresponds to the number of "splinters" we need to construct
732 * Of those with this denominator (all if period == NULL or if there
733 * are no fractional parts), we select the variable with the smallest
734 * maximal coefficient, as this coefficient will become a denominator
735 * in new fractional parts (for an exact computation), which may
736 * lead to splintering in the next step.
738 static void move_best_to_front(Polyhedron
**P_p
, evalue
**E_p
, unsigned nvar
,
741 Polyhedron
*P
= *P_p
;
743 int i
, j
, best_i
= -1;
750 periods
= fractional_periods(E
, nvar
);
751 value_assign(*period
, periods
->p
[0]);
752 for (i
= 1; i
< nvar
; ++i
)
753 if (value_lt(periods
->p
[i
], *period
))
754 value_assign(*period
, periods
->p
[i
]);
760 for (i
= 0; i
< nvar
; ++i
) {
761 if (period
&& value_ne(*period
, periods
->p
[i
]))
764 value_set_si(max
, 0);
766 for (j
= 0; j
< P
->NbConstraints
; ++j
) {
767 if (value_zero_p(P
->Constraint
[j
][1+i
]))
769 if (First_Non_Zero(P
->Constraint
[j
]+1, i
) == -1 &&
770 First_Non_Zero(P
->Constraint
[j
]+1+i
+1, nvar
-i
-1) == -1)
772 if (value_abs_gt(P
->Constraint
[j
][1+i
], max
))
773 value_absolute(max
, P
->Constraint
[j
][1+i
]);
776 if (best_i
== -1 || value_lt(max
, best
)) {
777 value_assign(best
, max
);
786 Vector_Free(periods
);
789 more_var_to_front(P_p
, E_p
, best_i
);
794 static evalue
*sum_over_polytope_base(Polyhedron
*P
, evalue
*E
, unsigned nvar
,
795 struct evalue_section_array
*sections
,
796 struct barvinok_options
*options
)
799 struct Bernoulli_data data
;
801 assert(P
->NbEq
== 0);
804 data
.options
= options
;
805 data
.sections
= sections
;
808 for_each_lower_upper_bound(P
, Bernoulli_init
, Bernoulli_cb
, &data
);
810 res
= evalue_from_section_array(sections
->s
, sections
->ns
);
813 evalue
*tmp
= barvinok_summate(res
, nvar
-1, options
);
821 static evalue
*sum_over_polytope_with_equalities(Polyhedron
*P
, evalue
*E
,
823 struct evalue_section_array
*sections
,
824 struct barvinok_options
*options
)
826 unsigned dim
= P
->Dimension
;
827 unsigned new_dim
, new_nparam
;
828 Matrix
*T
= NULL
, *CP
= NULL
;
834 return evalue_zero();
838 remove_all_equalities(&P
, NULL
, &CP
, &T
, dim
-nvar
, options
->MaxRays
);
842 return evalue_zero();
845 new_nparam
= CP
? CP
->NbColumns
-1 : dim
- nvar
;
846 new_dim
= T
? T
->NbColumns
-1 : nvar
+ new_nparam
;
848 /* We can avoid these substitutions if E is a constant */
849 subs
= ALLOCN(evalue
*, dim
);
850 for (j
= 0; j
< nvar
; ++j
) {
852 subs
[j
] = affine2evalue(T
->p
[j
], T
->p
[nvar
+new_nparam
][new_dim
],
855 subs
[j
] = evalue_var(j
);
857 for (j
= 0; j
< dim
-nvar
; ++j
) {
859 subs
[nvar
+j
] = affine2evalue(CP
->p
[j
], CP
->p
[dim
-nvar
][new_nparam
],
862 subs
[nvar
+j
] = evalue_var(j
);
863 evalue_shift_variables(subs
[nvar
+j
], new_dim
-new_nparam
);
867 evalue_substitute(E
, subs
);
870 for (j
= 0; j
< dim
; ++j
)
871 evalue_free(subs
[j
]);
874 if (new_dim
-new_nparam
> 0) {
875 sum
= bernoulli_summate(P
, E
, new_dim
-new_nparam
, sections
, options
);
881 sum
->x
.p
= new_enode(partition
, 2, new_dim
);
882 EVALUE_SET_DOMAIN(sum
->x
.p
->arr
[0], P
);
883 value_clear(sum
->x
.p
->arr
[1].d
);
884 sum
->x
.p
->arr
[1] = *E
;
889 evalue_backsubstitute(sum
, CP
, options
->MaxRays
);
899 /* Splinter P into period slices along the first variable x = period y + c,
900 * 0 <= c < perdiod, * ensuring no fractional part contains the first variable,
901 * and sum over all slices.
903 static evalue
*sum_over_polytope_slices(Polyhedron
*P
, evalue
*E
,
906 struct evalue_section_array
*sections
,
907 struct barvinok_options
*options
)
909 evalue
*sum
= evalue_zero();
911 unsigned dim
= P
->Dimension
;
919 value_set_si(one
, 1);
921 subs
= ALLOCN(evalue
*, dim
);
923 T
= Matrix_Alloc(dim
+1, dim
+1);
924 value_assign(T
->p
[0][0], period
);
925 for (j
= 1; j
< dim
+1; ++j
)
926 value_set_si(T
->p
[j
][j
], 1);
928 for (j
= 0; j
< dim
; ++j
)
929 subs
[j
] = evalue_var(j
);
930 evalue_mul(subs
[0], period
);
932 for (value_set_si(i
, 0); value_lt(i
, period
); value_increment(i
, i
)) {
934 Polyhedron
*S
= DomainPreimage(P
, T
, options
->MaxRays
);
935 evalue
*e
= evalue_dup(E
);
936 evalue_substitute(e
, subs
);
940 tmp
= sum_over_polytope_with_equalities(S
, e
, nvar
, sections
, options
);
942 tmp
= sum_over_polytope_base(S
, e
, nvar
, sections
, options
);
951 value_increment(T
->p
[0][dim
], T
->p
[0][dim
]);
952 evalue_add_constant(subs
[0], one
);
958 for (j
= 0; j
< dim
; ++j
)
959 evalue_free(subs
[j
]);
966 evalue
*bernoulli_summate(Polyhedron
*P
, evalue
*E
, unsigned nvar
,
967 struct evalue_section_array
*sections
,
968 struct barvinok_options
*options
)
970 Polyhedron
*P_orig
= P
;
974 int exact
= options
->approximation_method
== BV_APPROX_NONE
;
977 return sum_over_polytope_with_equalities(P
, E
, nvar
, sections
, options
);
981 move_best_to_front(&P
, &E
, nvar
, exact
? &period
: NULL
);
982 if (exact
&& value_notone_p(period
))
983 sum
= sum_over_polytope_slices(P
, E
, nvar
, period
, sections
, options
);
985 sum
= sum_over_polytope_base(P
, E
, nvar
, sections
, options
);