8 #include <NTL/mat_ZZ.h>
12 #include <polylib/polylibgmp.h>
13 #include "ev_operations.h"
26 using std::ostringstream
;
28 #define ALLOC(p) (((long *) (p))[0])
29 #define SIZE(p) (((long *) (p))[1])
30 #define DATA(p) ((mp_limb_t *) (((long *) (p)) + 2))
32 static void value2zz(Value v
, ZZ
& z
)
34 int sa
= v
[0]._mp_size
;
35 int abs_sa
= sa
< 0 ? -sa
: sa
;
37 _ntl_gsetlength(&z
.rep
, abs_sa
);
38 mp_limb_t
* adata
= DATA(z
.rep
);
39 for (int i
= 0; i
< abs_sa
; ++i
)
40 adata
[i
] = v
[0]._mp_d
[i
];
44 static void zz2value(ZZ
& z
, Value
& v
)
52 int abs_sa
= sa
< 0 ? -sa
: sa
;
54 mp_limb_t
* adata
= DATA(z
.rep
);
55 mpz_realloc2(v
, __GMP_BITS_PER_MP_LIMB
* abs_sa
);
56 for (int i
= 0; i
< abs_sa
; ++i
)
57 v
[0]._mp_d
[i
] = adata
[i
];
62 #define ALLOC(p) p = (typeof(p))malloc(sizeof(*p))
65 * We just ignore the last column and row
66 * If the final element is not equal to one
67 * then the result will actually be a multiple of the input
69 static void matrix2zz(Matrix
*M
, mat_ZZ
& m
, unsigned nr
, unsigned nc
)
73 for (int i
= 0; i
< nr
; ++i
) {
74 // assert(value_one_p(M->p[i][M->NbColumns - 1]));
75 for (int j
= 0; j
< nc
; ++j
) {
76 value2zz(M
->p
[i
][j
], m
[i
][j
]);
81 static void values2zz(Value
*p
, vec_ZZ
& v
, int len
)
85 for (int i
= 0; i
< len
; ++i
) {
92 static void zz2values(vec_ZZ
& v
, Value
*p
)
94 for (int i
= 0; i
< v
.length(); ++i
)
98 static void rays(mat_ZZ
& r
, Polyhedron
*C
)
100 unsigned dim
= C
->NbRays
- 1; /* don't count zero vertex */
101 assert(C
->NbRays
- 1 == C
->Dimension
);
106 for (i
= 0, c
= 0; i
< dim
; ++i
)
107 if (value_zero_p(C
->Ray
[i
][dim
+1])) {
108 for (int j
= 0; j
< dim
; ++j
) {
109 value2zz(C
->Ray
[i
][j
+1], tmp
);
116 static Matrix
* rays(Polyhedron
*C
)
118 unsigned dim
= C
->NbRays
- 1; /* don't count zero vertex */
119 assert(C
->NbRays
- 1 == C
->Dimension
);
121 Matrix
*M
= Matrix_Alloc(dim
+1, dim
+1);
125 for (i
= 0, c
= 0; i
<= dim
&& c
< dim
; ++i
)
126 if (value_zero_p(C
->Ray
[i
][dim
+1])) {
127 Vector_Copy(C
->Ray
[i
] + 1, M
->p
[c
], dim
);
128 value_set_si(M
->p
[c
++][dim
], 0);
131 value_set_si(M
->p
[dim
][dim
], 1);
136 static Matrix
* rays2(Polyhedron
*C
)
138 unsigned dim
= C
->NbRays
- 1; /* don't count zero vertex */
139 assert(C
->NbRays
- 1 == C
->Dimension
);
141 Matrix
*M
= Matrix_Alloc(dim
, dim
);
145 for (i
= 0, c
= 0; i
<= dim
&& c
< dim
; ++i
)
146 if (value_zero_p(C
->Ray
[i
][dim
+1]))
147 Vector_Copy(C
->Ray
[i
] + 1, M
->p
[c
++], dim
);
154 * Returns the largest absolute value in the vector
156 static ZZ
max(vec_ZZ
& v
)
159 for (int i
= 1; i
< v
.length(); ++i
)
169 Rays
= Matrix_Copy(M
);
172 cone(Polyhedron
*C
) {
173 Cone
= Polyhedron_Copy(C
);
179 matrix2zz(Rays
, A
, Rays
->NbRows
- 1, Rays
->NbColumns
- 1);
180 det
= determinant(A
);
187 Vector
* short_vector(vec_ZZ
& lambda
) {
188 Matrix
*M
= Matrix_Copy(Rays
);
189 Matrix
*inv
= Matrix_Alloc(M
->NbRows
, M
->NbColumns
);
190 int ok
= Matrix_Inverse(M
, inv
);
197 matrix2zz(inv
, B
, inv
->NbRows
- 1, inv
->NbColumns
- 1);
198 long r
= LLL(det2
, B
, U
);
202 for (int i
= 1; i
< B
.NumRows(); ++i
) {
214 Vector
*z
= Vector_Alloc(U
[index
].length()+1);
216 zz2values(U
[index
], z
->p
);
217 value_set_si(z
->p
[U
[index
].length()], 0);
221 Polyhedron
*C
= poly();
223 for (i
= 0; i
< C
->NbConstraints
; ++i
) {
224 Inner_Product(z
->p
, C
->Constraint
[i
]+1, z
->Size
-1, &tmp
);
225 if (value_pos_p(tmp
))
228 if (i
== C
->NbConstraints
) {
229 value_set_si(tmp
, -1);
230 Vector_Scale(z
->p
, z
->p
, tmp
, z
->Size
-1);
237 Polyhedron_Free(Cone
);
243 Matrix
*M
= Matrix_Alloc(Rays
->NbRows
+1, Rays
->NbColumns
+1);
244 for (int i
= 0; i
< Rays
->NbRows
; ++i
) {
245 Vector_Copy(Rays
->p
[i
], M
->p
[i
]+1, Rays
->NbColumns
);
246 value_set_si(M
->p
[i
][0], 1);
248 Vector_Set(M
->p
[Rays
->NbRows
]+1, 0, Rays
->NbColumns
-1);
249 value_set_si(M
->p
[Rays
->NbRows
][0], 1);
250 value_set_si(M
->p
[Rays
->NbRows
][Rays
->NbColumns
], 1);
251 Cone
= Rays2Polyhedron(M
, M
->NbRows
+1);
252 assert(Cone
->NbConstraints
== Cone
->NbRays
);
266 dpoly(int d
, ZZ
& degree
, int offset
= 0) {
267 coeff
.SetLength(d
+1);
269 int min
= d
+ offset
;
270 if (degree
< ZZ(INIT_VAL
, min
))
271 min
= to_int(degree
);
273 ZZ c
= ZZ(INIT_VAL
, 1);
276 for (int i
= 1; i
<= min
; ++i
) {
277 c
*= (degree
-i
+ 1);
282 void operator *= (dpoly
& f
) {
283 assert(coeff
.length() == f
.coeff
.length());
285 coeff
= f
.coeff
[0] * coeff
;
286 for (int i
= 1; i
< coeff
.length(); ++i
)
287 for (int j
= 0; i
+j
< coeff
.length(); ++j
)
288 coeff
[i
+j
] += f
.coeff
[i
] * old
[j
];
290 void div(dpoly
& d
, mpq_t count
, ZZ
& sign
) {
291 int len
= coeff
.length();
294 mpq_t
* c
= new mpq_t
[coeff
.length()];
297 for (int i
= 0; i
< len
; ++i
) {
299 zz2value(coeff
[i
], tmp
);
300 mpq_set_z(c
[i
], tmp
);
302 for (int j
= 1; j
<= i
; ++j
) {
303 zz2value(d
.coeff
[j
], tmp
);
304 mpq_set_z(qtmp
, tmp
);
305 mpq_mul(qtmp
, qtmp
, c
[i
-j
]);
306 mpq_sub(c
[i
], c
[i
], qtmp
);
309 zz2value(d
.coeff
[0], tmp
);
310 mpq_set_z(qtmp
, tmp
);
311 mpq_div(c
[i
], c
[i
], qtmp
);
314 mpq_sub(count
, count
, c
[len
-1]);
316 mpq_add(count
, count
, c
[len
-1]);
320 for (int i
= 0; i
< len
; ++i
)
332 dpoly_n(int d
, ZZ
& degree_0
, ZZ
& degree_1
, int offset
= 0) {
336 zz2value(degree_0
, d0
);
337 zz2value(degree_1
, d1
);
338 coeff
= Matrix_Alloc(d
+1, d
+1+1);
339 value_set_si(coeff
->p
[0][0], 1);
340 value_set_si(coeff
->p
[0][d
+1], 1);
341 for (int i
= 1; i
<= d
; ++i
) {
342 value_multiply(coeff
->p
[i
][0], coeff
->p
[i
-1][0], d0
);
343 Vector_Combine(coeff
->p
[i
-1], coeff
->p
[i
-1]+1, coeff
->p
[i
]+1,
345 value_set_si(coeff
->p
[i
][d
+1], i
);
346 value_multiply(coeff
->p
[i
][d
+1], coeff
->p
[i
][d
+1], coeff
->p
[i
-1][d
+1]);
347 value_decrement(d0
, d0
);
352 void div(dpoly
& d
, Vector
*count
, ZZ
& sign
) {
353 int len
= coeff
->NbRows
;
354 Matrix
* c
= Matrix_Alloc(coeff
->NbRows
, coeff
->NbColumns
);
357 for (int i
= 0; i
< len
; ++i
) {
358 Vector_Copy(coeff
->p
[i
], c
->p
[i
], len
+1);
359 for (int j
= 1; j
<= i
; ++j
) {
360 zz2value(d
.coeff
[j
], tmp
);
361 value_multiply(tmp
, tmp
, c
->p
[i
][len
]);
362 value_oppose(tmp
, tmp
);
363 Vector_Combine(c
->p
[i
], c
->p
[i
-j
], c
->p
[i
],
364 c
->p
[i
-j
][len
], tmp
, len
);
365 value_multiply(c
->p
[i
][len
], c
->p
[i
][len
], c
->p
[i
-j
][len
]);
367 zz2value(d
.coeff
[0], tmp
);
368 value_multiply(c
->p
[i
][len
], c
->p
[i
][len
], tmp
);
371 value_set_si(tmp
, -1);
372 Vector_Scale(c
->p
[len
-1], count
->p
, tmp
, len
);
373 value_assign(count
->p
[len
], c
->p
[len
-1][len
]);
375 Vector_Copy(c
->p
[len
-1], count
->p
, len
+1);
376 Vector_Normalize(count
->p
, len
+1);
383 * Barvinok's Decomposition of a simplicial cone
385 * Returns two lists of polyhedra
387 void barvinok_decompose(Polyhedron
*C
, Polyhedron
**ppos
, Polyhedron
**pneg
)
389 Polyhedron
*pos
= *ppos
, *neg
= *pneg
;
390 vector
<cone
*> nonuni
;
391 cone
* c
= new cone(C
);
398 Polyhedron
*p
= Polyhedron_Copy(c
->Cone
);
404 while (!nonuni
.empty()) {
407 Vector
* v
= c
->short_vector(lambda
);
408 for (int i
= 0; i
< c
->Rays
->NbRows
- 1; ++i
) {
411 Matrix
* M
= Matrix_Copy(c
->Rays
);
412 Vector_Copy(v
->p
, M
->p
[i
], v
->Size
);
413 cone
* pc
= new cone(M
);
414 assert (pc
->det
!= 0);
415 if (abs(pc
->det
) > 1) {
416 assert(abs(pc
->det
) < abs(c
->det
));
417 nonuni
.push_back(pc
);
419 Polyhedron
*p
= pc
->poly();
421 if (sign(pc
->det
) == s
) {
440 * Returns a single list of npos "positive" cones followed by nneg
442 * The input cone is freed
444 void decompose(Polyhedron
*cone
, Polyhedron
**parts
, int *npos
, int *nneg
, unsigned MaxRays
)
446 Polyhedron_Polarize(cone
);
447 if (cone
->NbRays
- 1 != cone
->Dimension
) {
448 Polyhedron
*tmp
= cone
;
449 cone
= triangularize_cone(cone
, MaxRays
);
450 Polyhedron_Free(tmp
);
452 Polyhedron
*polpos
= NULL
, *polneg
= NULL
;
453 *npos
= 0; *nneg
= 0;
454 for (Polyhedron
*Polar
= cone
; Polar
; Polar
= Polar
->next
)
455 barvinok_decompose(Polar
, &polpos
, &polneg
);
458 for (Polyhedron
*i
= polpos
; i
; i
= i
->next
) {
459 Polyhedron_Polarize(i
);
463 for (Polyhedron
*i
= polneg
; i
; i
= i
->next
) {
464 Polyhedron_Polarize(i
);
475 const int MAX_TRY
=10;
477 * Searches for a vector that is not othogonal to any
478 * of the rays in rays.
480 static void nonorthog(mat_ZZ
& rays
, vec_ZZ
& lambda
)
482 int dim
= rays
.NumCols();
484 lambda
.SetLength(dim
);
485 for (int i
= 2; !found
&& i
<= 50*dim
; i
+=4) {
486 for (int j
= 0; j
< MAX_TRY
; ++j
) {
487 for (int k
= 0; k
< dim
; ++k
) {
488 int r
= random_int(i
)+2;
489 int v
= (2*(r
%2)-1) * (r
>> 1);
493 for (; k
< rays
.NumRows(); ++k
)
494 if (lambda
* rays
[k
] == 0)
496 if (k
== rays
.NumRows()) {
505 static void add_rays(mat_ZZ
& rays
, Polyhedron
*i
, int *r
)
507 unsigned dim
= i
->Dimension
;
508 for (int k
= 0; k
< i
->NbRays
; ++k
) {
509 if (!value_zero_p(i
->Ray
[k
][dim
+1]))
511 values2zz(i
->Ray
[k
]+1, rays
[(*r
)++], dim
);
515 void lattice_point(Value
* values
, Polyhedron
*i
, vec_ZZ
& lambda
, ZZ
& num
)
518 unsigned dim
= i
->Dimension
;
519 if(!value_one_p(values
[dim
])) {
520 Matrix
* Rays
= rays(i
);
521 Matrix
*inv
= Matrix_Alloc(Rays
->NbRows
, Rays
->NbColumns
);
522 int ok
= Matrix_Inverse(Rays
, inv
);
526 Vector
*lambda
= Vector_Alloc(dim
+1);
527 Vector_Matrix_Product(values
, inv
, lambda
->p
);
529 for (int j
= 0; j
< dim
; ++j
)
530 mpz_cdiv_q(lambda
->p
[j
], lambda
->p
[j
], lambda
->p
[dim
]);
531 value_set_si(lambda
->p
[dim
], 1);
532 Vector
*A
= Vector_Alloc(dim
+1);
533 Vector_Matrix_Product(lambda
->p
, Rays
, A
->p
);
536 values2zz(A
->p
, vertex
, dim
);
539 values2zz(values
, vertex
, dim
);
541 num
= vertex
* lambda
;
544 static evalue
*term(int param
, ZZ
& c
, Value
*den
= NULL
)
546 evalue
*EP
= new evalue();
548 value_set_si(EP
->d
,0);
549 EP
->x
.p
= new_enode(polynomial
, 2, param
+ 1);
550 evalue_set_si(&EP
->x
.p
->arr
[0], 0, 1);
551 value_init(EP
->x
.p
->arr
[1].x
.n
);
553 value_set_si(EP
->x
.p
->arr
[1].d
, 1);
555 value_assign(EP
->x
.p
->arr
[1].d
, *den
);
556 zz2value(c
, EP
->x
.p
->arr
[1].x
.n
);
560 static void vertex_period(
561 Polyhedron
*i
, vec_ZZ
& lambda
, Matrix
*T
,
562 Value lcm
, int p
, Vector
*val
,
563 evalue
*E
, evalue
* ev
,
566 unsigned nparam
= T
->NbRows
- 1;
567 unsigned dim
= i
->Dimension
;
573 Vector
* values
= Vector_Alloc(dim
+ 1);
574 Vector_Matrix_Product(val
->p
, T
, values
->p
);
575 value_assign(values
->p
[dim
], lcm
);
576 lattice_point(values
->p
, i
, lambda
, num
);
581 zz2value(num
, ev
->x
.n
);
582 value_assign(ev
->d
, lcm
);
589 values2zz(T
->p
[p
], vertex
, dim
);
590 nump
= vertex
* lambda
;
591 if (First_Non_Zero(val
->p
, p
) == -1) {
592 value_assign(tmp
, lcm
);
593 evalue
*ET
= term(p
, nump
, &tmp
);
595 free_evalue_refs(ET
);
599 value_assign(tmp
, lcm
);
600 if (First_Non_Zero(T
->p
[p
], dim
) != -1)
601 Vector_Gcd(T
->p
[p
], dim
, &tmp
);
603 if (value_lt(tmp
, lcm
)) {
606 value_division(tmp
, lcm
, tmp
);
607 value_set_si(ev
->d
, 0);
608 ev
->x
.p
= new_enode(periodic
, VALUE_TO_INT(tmp
), p
+1);
609 value2zz(tmp
, count
);
611 value_decrement(tmp
, tmp
);
613 ZZ new_offset
= offset
- count
* nump
;
614 value_assign(val
->p
[p
], tmp
);
615 vertex_period(i
, lambda
, T
, lcm
, p
+1, val
, E
,
616 &ev
->x
.p
->arr
[VALUE_TO_INT(tmp
)], new_offset
);
617 } while (value_pos_p(tmp
));
619 vertex_period(i
, lambda
, T
, lcm
, p
+1, val
, E
, ev
, offset
);
623 static void mask_r(Matrix
*f
, int nr
, Vector
*lcm
, int p
, Vector
*val
, evalue
*ev
)
625 unsigned nparam
= lcm
->Size
;
628 Vector
* prod
= Vector_Alloc(f
->NbRows
);
629 Matrix_Vector_Product(f
, val
->p
, prod
->p
);
631 for (int i
= 0; i
< nr
; ++i
) {
632 value_modulus(prod
->p
[i
], prod
->p
[i
], f
->p
[i
][nparam
+1]);
633 isint
&= value_zero_p(prod
->p
[i
]);
635 value_set_si(ev
->d
, 1);
637 value_set_si(ev
->x
.n
, isint
);
644 if (value_one_p(lcm
->p
[p
]))
645 mask_r(f
, nr
, lcm
, p
+1, val
, ev
);
647 value_assign(tmp
, lcm
->p
[p
]);
648 value_set_si(ev
->d
, 0);
649 ev
->x
.p
= new_enode(periodic
, VALUE_TO_INT(tmp
), p
+1);
651 value_decrement(tmp
, tmp
);
652 value_assign(val
->p
[p
], tmp
);
653 mask_r(f
, nr
, lcm
, p
+1, val
, &ev
->x
.p
->arr
[VALUE_TO_INT(tmp
)]);
654 } while (value_pos_p(tmp
));
659 static evalue
*multi_monom(vec_ZZ
& p
)
661 evalue
*X
= new evalue();
664 unsigned nparam
= p
.length()-1;
665 zz2value(p
[nparam
], X
->x
.n
);
666 value_set_si(X
->d
, 1);
667 for (int i
= 0; i
< nparam
; ++i
) {
670 evalue
*T
= term(i
, p
[i
]);
679 * Check whether mapping polyhedron P on the affine combination
680 * num yields a range that has a fixed quotient on integer
682 * If zero is true, then we are only interested in the quotient
683 * for the cases where the remainder is zero.
684 * Returns NULL if false and a newly allocated value if true.
686 static Value
*fixed_quotient(Polyhedron
*P
, vec_ZZ
& num
, Value d
, bool zero
)
689 int len
= num
.length();
690 Matrix
*T
= Matrix_Alloc(2, len
);
691 zz2values(num
, T
->p
[0]);
692 value_set_si(T
->p
[1][len
-1], 1);
693 Polyhedron
*I
= Polyhedron_Image(P
, T
, P
->NbConstraints
);
697 for (i
= 0; i
< I
->NbRays
; ++i
)
698 if (value_zero_p(I
->Ray
[i
][2])) {
706 line_minmax(I
, &min
, &max
);
709 mpz_cdiv_q(min
, min
, d
);
711 mpz_fdiv_q(min
, min
, d
);
712 mpz_fdiv_q(max
, max
, d
);
714 if (value_eq(min
, max
)) {
717 value_assign(*ret
, min
);
725 * Normalize linear expression coef modulo m
726 * Removes common factor and reduces coefficients
727 * Returns index of first non-zero coefficient or len
729 static int normal_mod(Value
*coef
, int len
, Value
*m
)
734 Vector_Gcd(coef
, len
, &gcd
);
736 Vector_AntiScale(coef
, coef
, gcd
, len
);
738 value_division(*m
, *m
, gcd
);
745 for (j
= 0; j
< len
; ++j
)
746 mpz_fdiv_r(coef
[j
], coef
[j
], *m
);
747 for (j
= 0; j
< len
; ++j
)
748 if (value_notzero_p(coef
[j
]))
755 static void mask(Matrix
*f
, evalue
*factor
)
757 int nr
= f
->NbRows
, nc
= f
->NbColumns
;
760 for (n
= 0; n
< nr
&& value_notzero_p(f
->p
[n
][nc
-1]); ++n
)
761 if (value_notone_p(f
->p
[n
][nc
-1]) &&
762 value_notmone_p(f
->p
[n
][nc
-1]))
776 value_set_si(EV
.x
.n
, 1);
778 for (n
= 0; n
< nr
; ++n
) {
779 value_assign(m
, f
->p
[n
][nc
-1]);
780 if (value_one_p(m
) || value_mone_p(m
))
783 int j
= normal_mod(f
->p
[n
], nc
-1, &m
);
785 free_evalue_refs(factor
);
786 value_init(factor
->d
);
787 evalue_set_si(factor
, 0, 1);
791 values2zz(f
->p
[n
], row
, nc
-1);
794 if (j
< (nc
-1)-1 && row
[j
] > g
/2) {
795 for (int k
= j
; k
< (nc
-1); ++k
)
801 value_set_si(EP
.d
, 0);
802 EP
.x
.p
= new_enode(relation
, 2, 0);
803 value_clear(EP
.x
.p
->arr
[1].d
);
804 EP
.x
.p
->arr
[1] = *factor
;
805 evalue
*ev
= &EP
.x
.p
->arr
[0];
806 value_set_si(ev
->d
, 0);
807 ev
->x
.p
= new_enode(fractional
, 3, -1);
808 evalue_set_si(&ev
->x
.p
->arr
[1], 0, 1);
809 evalue_set_si(&ev
->x
.p
->arr
[2], 1, 1);
810 evalue
*E
= multi_monom(row
);
811 value_assign(EV
.d
, m
);
813 value_clear(ev
->x
.p
->arr
[0].d
);
814 ev
->x
.p
->arr
[0] = *E
;
820 free_evalue_refs(&EV
);
826 static void mask(Matrix
*f
, evalue
*factor
)
828 int nr
= f
->NbRows
, nc
= f
->NbColumns
;
831 for (n
= 0; n
< nr
&& value_notzero_p(f
->p
[n
][nc
-1]); ++n
)
832 if (value_notone_p(f
->p
[n
][nc
-1]) &&
833 value_notmone_p(f
->p
[n
][nc
-1]))
841 unsigned np
= nc
- 2;
842 Vector
*lcm
= Vector_Alloc(np
);
843 Vector
*val
= Vector_Alloc(nc
);
844 Vector_Set(val
->p
, 0, nc
);
845 value_set_si(val
->p
[np
], 1);
846 Vector_Set(lcm
->p
, 1, np
);
847 for (n
= 0; n
< nr
; ++n
) {
848 if (value_one_p(f
->p
[n
][nc
-1]) ||
849 value_mone_p(f
->p
[n
][nc
-1]))
851 for (int j
= 0; j
< np
; ++j
)
852 if (value_notzero_p(f
->p
[n
][j
])) {
853 Gcd(f
->p
[n
][j
], f
->p
[n
][nc
-1], &tmp
);
854 value_division(tmp
, f
->p
[n
][nc
-1], tmp
);
855 value_lcm(tmp
, lcm
->p
[j
], &lcm
->p
[j
]);
860 mask_r(f
, nr
, lcm
, 0, val
, &EP
);
865 free_evalue_refs(&EP
);
876 static bool mod_needed(Polyhedron
*PD
, vec_ZZ
& num
, Value d
, evalue
*E
)
878 Value
*q
= fixed_quotient(PD
, num
, d
, false);
883 value_oppose(*q
, *q
);
886 value_set_si(EV
.d
, 1);
888 value_multiply(EV
.x
.n
, *q
, d
);
890 free_evalue_refs(&EV
);
896 static void ceil_mod(Value
*coef
, int len
, Value d
, ZZ
& f
, evalue
*EP
, Polyhedron
*PD
)
902 Vector_Scale(coef
, coef
, m
, len
);
905 int j
= normal_mod(coef
, len
, &m
);
913 values2zz(coef
, num
, len
);
920 evalue_set_si(&tmp
, 0, 1);
924 while (j
< len
-1 && (num
[j
] == g
/2 || num
[j
] == 0))
926 if ((j
< len
-1 && num
[j
] > g
/2) || (j
== len
-1 && num
[j
] >= (g
+1)/2)) {
927 for (int k
= j
; k
< len
-1; ++k
)
930 num
[len
-1] = g
- 1 - num
[len
-1];
931 value_assign(tmp
.d
, m
);
933 zz2value(t
, tmp
.x
.n
);
939 ZZ t
= num
[len
-1] * f
;
940 zz2value(t
, tmp
.x
.n
);
941 value_assign(tmp
.d
, m
);
944 evalue
*E
= multi_monom(num
);
948 if (PD
&& !mod_needed(PD
, num
, m
, E
)) {
951 value_assign(EV
.d
, m
);
956 value_set_si(EV
.x
.n
, 1);
957 value_assign(EV
.d
, m
);
960 value_set_si(EV
.d
, 0);
961 EV
.x
.p
= new_enode(fractional
, 3, -1);
962 evalue_copy(&EV
.x
.p
->arr
[0], E
);
963 evalue_set_si(&EV
.x
.p
->arr
[1], 0, 1);
964 value_init(EV
.x
.p
->arr
[2].x
.n
);
965 zz2value(f
, EV
.x
.p
->arr
[2].x
.n
);
966 value_set_si(EV
.x
.p
->arr
[2].d
, 1);
971 free_evalue_refs(&EV
);
976 free_evalue_refs(&tmp
);
982 evalue
* bv_ceil3(Value
*coef
, int len
, Value d
, Polyhedron
*P
)
984 Vector
*val
= Vector_Alloc(len
);
989 Vector_Scale(coef
, val
->p
, t
, len
);
990 value_absolute(t
, d
);
993 values2zz(val
->p
, num
, len
);
994 evalue
*EP
= multi_monom(num
);
999 value_set_si(tmp
.x
.n
, 1);
1000 value_assign(tmp
.d
, t
);
1006 ceil_mod(val
->p
, len
, t
, one
, EP
, P
);
1009 /* copy EP to malloc'ed evalue */
1015 free_evalue_refs(&tmp
);
1022 evalue
* lattice_point(
1023 Polyhedron
*i
, vec_ZZ
& lambda
, Matrix
*W
, Value lcm
, Polyhedron
*PD
)
1025 unsigned nparam
= W
->NbColumns
- 1;
1027 Matrix
* Rays
= rays2(i
);
1028 Matrix
*T
= Transpose(Rays
);
1029 Matrix
*T2
= Matrix_Copy(T
);
1030 Matrix
*inv
= Matrix_Alloc(T2
->NbRows
, T2
->NbColumns
);
1031 int ok
= Matrix_Inverse(T2
, inv
);
1036 matrix2zz(W
, vertex
, W
->NbRows
, W
->NbColumns
);
1039 num
= lambda
* vertex
;
1041 evalue
*EP
= multi_monom(num
);
1045 value_init(tmp
.x
.n
);
1046 value_set_si(tmp
.x
.n
, 1);
1047 value_assign(tmp
.d
, lcm
);
1051 Matrix
*L
= Matrix_Alloc(inv
->NbRows
, W
->NbColumns
);
1052 Matrix_Product(inv
, W
, L
);
1055 matrix2zz(T
, RT
, T
->NbRows
, T
->NbColumns
);
1058 vec_ZZ p
= lambda
* RT
;
1060 for (int i
= 0; i
< L
->NbRows
; ++i
) {
1061 ceil_mod(L
->p
[i
], nparam
+1, lcm
, p
[i
], EP
, PD
);
1067 free_evalue_refs(&tmp
);
1071 evalue
* lattice_point(
1072 Polyhedron
*i
, vec_ZZ
& lambda
, Matrix
*W
, Value lcm
, Polyhedron
*PD
)
1074 Matrix
*T
= Transpose(W
);
1075 unsigned nparam
= T
->NbRows
- 1;
1077 evalue
*EP
= new evalue();
1079 evalue_set_si(EP
, 0, 1);
1082 Vector
*val
= Vector_Alloc(nparam
+1);
1083 value_set_si(val
->p
[nparam
], 1);
1084 ZZ
offset(INIT_VAL
, 0);
1086 vertex_period(i
, lambda
, T
, lcm
, 0, val
, EP
, &ev
, offset
);
1089 free_evalue_refs(&ev
);
1100 Param_Vertices
* V
, Polyhedron
*i
, vec_ZZ
& lambda
, term_info
* term
,
1103 unsigned nparam
= V
->Vertex
->NbColumns
- 2;
1104 unsigned dim
= i
->Dimension
;
1106 vertex
.SetDims(V
->Vertex
->NbRows
, nparam
+1);
1110 value_set_si(lcm
, 1);
1111 for (int j
= 0; j
< V
->Vertex
->NbRows
; ++j
) {
1112 value_lcm(lcm
, V
->Vertex
->p
[j
][nparam
+1], &lcm
);
1114 if (value_notone_p(lcm
)) {
1115 Matrix
* mv
= Matrix_Alloc(dim
, nparam
+1);
1116 for (int j
= 0 ; j
< dim
; ++j
) {
1117 value_division(tmp
, lcm
, V
->Vertex
->p
[j
][nparam
+1]);
1118 Vector_Scale(V
->Vertex
->p
[j
], mv
->p
[j
], tmp
, nparam
+1);
1121 term
->E
= lattice_point(i
, lambda
, mv
, lcm
, PD
);
1129 for (int i
= 0; i
< V
->Vertex
->NbRows
; ++i
) {
1130 assert(value_one_p(V
->Vertex
->p
[i
][nparam
+1])); // for now
1131 values2zz(V
->Vertex
->p
[i
], vertex
[i
], nparam
+1);
1135 num
= lambda
* vertex
;
1139 for (int j
= 0; j
< nparam
; ++j
)
1145 term
->E
= multi_monom(num
);
1149 term
->constant
= num
[nparam
];
1152 term
->coeff
= num
[p
];
1159 void normalize(Polyhedron
*i
, vec_ZZ
& lambda
, ZZ
& sign
, ZZ
& num
, vec_ZZ
& den
)
1161 unsigned dim
= i
->Dimension
;
1165 rays
.SetDims(dim
, dim
);
1166 add_rays(rays
, i
, &r
);
1167 den
= rays
* lambda
;
1170 for (int j
= 0; j
< den
.length(); ++j
) {
1174 den
[j
] = abs(den
[j
]);
1182 void barvinok_count(Polyhedron
*P
, Value
* result
, unsigned NbMaxCons
)
1184 Polyhedron
** vcone
;
1187 sign
.SetLength(ncone
);
1195 value_set_si(*result
, 0);
1199 for (; r
< P
->NbRays
; ++r
)
1200 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1]))
1202 if (P
->NbBid
!=0 || r
< P
->NbRays
) {
1203 value_set_si(*result
, -1);
1207 P
= remove_equalities(P
);
1210 value_set_si(*result
, 0);
1216 value_set_si(factor
, 1);
1217 Q
= Polyhedron_Reduce(P
, &factor
);
1224 if (P
->Dimension
== 0) {
1225 value_assign(*result
, factor
);
1228 value_clear(factor
);
1233 vcone
= new (Polyhedron
*)[P
->NbRays
];
1235 for (int j
= 0; j
< P
->NbRays
; ++j
) {
1237 Polyhedron
*C
= supporting_cone(P
, j
);
1238 decompose(C
, &vcone
[j
], &npos
, &nneg
, NbMaxCons
);
1239 ncone
+= npos
+ nneg
;
1240 sign
.SetLength(ncone
);
1241 for (int k
= 0; k
< npos
; ++k
)
1242 sign
[ncone
-nneg
-k
-1] = 1;
1243 for (int k
= 0; k
< nneg
; ++k
)
1244 sign
[ncone
-k
-1] = -1;
1248 rays
.SetDims(ncone
* dim
, dim
);
1250 for (int j
= 0; j
< P
->NbRays
; ++j
) {
1251 for (Polyhedron
*i
= vcone
[j
]; i
; i
= i
->next
) {
1252 assert(i
->NbRays
-1 == dim
);
1253 add_rays(rays
, i
, &r
);
1257 nonorthog(rays
, lambda
);
1261 num
.SetLength(ncone
);
1262 den
.SetDims(ncone
,dim
);
1265 for (int j
= 0; j
< P
->NbRays
; ++j
) {
1266 for (Polyhedron
*i
= vcone
[j
]; i
; i
= i
->next
) {
1267 lattice_point(P
->Ray
[j
]+1, i
, lambda
, num
[f
]);
1268 normalize(i
, lambda
, sign
[f
], num
[f
], den
[f
]);
1273 for (int j
= 1; j
< num
.length(); ++j
)
1276 for (int j
= 0; j
< num
.length(); ++j
)
1282 for (int j
= 0; j
< P
->NbRays
; ++j
) {
1283 for (Polyhedron
*i
= vcone
[j
]; i
; i
= i
->next
) {
1284 dpoly
d(dim
, num
[f
]);
1285 dpoly
n(dim
, den
[f
][0], 1);
1286 for (int k
= 1; k
< dim
; ++k
) {
1287 dpoly
fact(dim
, den
[f
][k
], 1);
1290 d
.div(n
, count
, sign
[f
]);
1294 assert(value_one_p(&count
[0]._mp_den
));
1295 value_multiply(*result
, &count
[0]._mp_num
, factor
);
1298 for (int j
= 0; j
< P
->NbRays
; ++j
)
1299 Domain_Free(vcone
[j
]);
1305 value_clear(factor
);
1308 static void uni_polynom(int param
, Vector
*c
, evalue
*EP
)
1310 unsigned dim
= c
->Size
-2;
1312 value_set_si(EP
->d
,0);
1313 EP
->x
.p
= new_enode(polynomial
, dim
+1, param
+1);
1314 for (int j
= 0; j
<= dim
; ++j
)
1315 evalue_set(&EP
->x
.p
->arr
[j
], c
->p
[j
], c
->p
[dim
+1]);
1318 static void multi_polynom(Vector
*c
, evalue
* X
, evalue
*EP
)
1320 unsigned dim
= c
->Size
-2;
1324 evalue_set(&EC
, c
->p
[dim
], c
->p
[dim
+1]);
1327 evalue_set(EP
, c
->p
[dim
], c
->p
[dim
+1]);
1329 for (int i
= dim
-1; i
>= 0; --i
) {
1331 value_assign(EC
.x
.n
, c
->p
[i
]);
1334 free_evalue_refs(&EC
);
1337 Polyhedron
*unfringe (Polyhedron
*P
, unsigned MaxRays
)
1339 int len
= P
->Dimension
+2;
1340 Polyhedron
*T
, *R
= P
;
1343 Vector
*row
= Vector_Alloc(len
);
1344 value_set_si(row
->p
[0], 1);
1346 R
= DomainConstraintSimplify(Polyhedron_Copy(P
), MaxRays
);
1348 Matrix
*M
= Matrix_Alloc(2, len
-1);
1349 value_set_si(M
->p
[1][len
-2], 1);
1350 for (int v
= 0; v
< P
->Dimension
; ++v
) {
1351 value_set_si(M
->p
[0][v
], 1);
1352 Polyhedron
*I
= Polyhedron_Image(P
, M
, 2+1);
1353 value_set_si(M
->p
[0][v
], 0);
1354 for (int r
= 0; r
< I
->NbConstraints
; ++r
) {
1355 if (value_zero_p(I
->Constraint
[r
][0]))
1357 if (value_zero_p(I
->Constraint
[r
][1]))
1359 if (value_one_p(I
->Constraint
[r
][1]))
1361 if (value_mone_p(I
->Constraint
[r
][1]))
1363 value_absolute(g
, I
->Constraint
[r
][1]);
1364 Vector_Set(row
->p
+1, 0, len
-2);
1365 value_division(row
->p
[1+v
], I
->Constraint
[r
][1], g
);
1366 mpz_fdiv_q(row
->p
[len
-1], I
->Constraint
[r
][2], g
);
1368 R
= AddConstraints(row
->p
, 1, R
, MaxRays
);
1377 static Polyhedron
*reduce_domain(Polyhedron
*D
, Matrix
*CT
, Polyhedron
*CEq
,
1378 Polyhedron
**fVD
, int nd
, unsigned MaxRays
)
1383 Dt
= CT
? DomainPreimage(D
, CT
, MaxRays
) : D
;
1384 Polyhedron
*rVD
= DomainIntersection(Dt
, CEq
, MaxRays
);
1386 /* if rVD is empty or too small in geometric dimension */
1387 if(!rVD
|| emptyQ(rVD
) ||
1388 (rVD
->Dimension
-rVD
->NbEq
< Dt
->Dimension
-Dt
->NbEq
-CEq
->NbEq
)) {
1393 return 0; /* empty validity domain */
1399 fVD
[nd
] = Domain_Copy(rVD
);
1400 for (int i
= 0 ; i
< nd
; ++i
) {
1401 Polyhedron
*I
= DomainIntersection(fVD
[nd
], fVD
[i
], MaxRays
);
1406 Polyhedron
*F
= DomainSimplify(I
, fVD
[nd
], MaxRays
);
1408 Polyhedron
*T
= rVD
;
1409 rVD
= DomainDifference(rVD
, F
, MaxRays
);
1416 rVD
= DomainConstraintSimplify(rVD
, MaxRays
);
1424 barvinok_count(rVD
, &c
, MaxRays
);
1425 if (value_zero_p(c
)) {
1434 evalue
* barvinok_enumerate_ev(Polyhedron
*P
, Polyhedron
* C
, unsigned MaxRays
)
1436 //P = unfringe(P, MaxRays);
1437 Polyhedron
*CEq
= NULL
, *rVD
, *pVD
, *CA
;
1439 Param_Polyhedron
*PP
= NULL
;
1440 Param_Domain
*D
, *next
;
1443 unsigned nparam
= C
->Dimension
;
1446 value_init(eres
->d
);
1447 value_set_si(eres
->d
, 0);
1450 value_init(factor
.d
);
1451 evalue_set_si(&factor
, 1, 1);
1453 CA
= align_context(C
, P
->Dimension
, MaxRays
);
1454 P
= DomainIntersection(P
, CA
, MaxRays
);
1455 Polyhedron_Free(CA
);
1457 if (C
->Dimension
== 0 || emptyQ(P
)) {
1459 eres
->x
.p
= new_enode(partition
, 2, -1);
1460 EVALUE_SET_DOMAIN(eres
->x
.p
->arr
[0],
1461 DomainConstraintSimplify(CEq
? CEq
: Polyhedron_Copy(C
), MaxRays
));
1462 value_set_si(eres
->x
.p
->arr
[1].d
, 1);
1463 value_init(eres
->x
.p
->arr
[1].x
.n
);
1465 value_set_si(eres
->x
.p
->arr
[1].x
.n
, 0);
1467 barvinok_count(P
, &eres
->x
.p
->arr
[1].x
.n
, MaxRays
);
1469 emul(&factor
, eres
);
1470 reduce_evalue(eres
);
1471 free_evalue_refs(&factor
);
1476 Param_Polyhedron_Free(PP
);
1480 for (r
= 0; r
< P
->NbRays
; ++r
)
1481 if (value_zero_p(P
->Ray
[r
][0]) ||
1482 value_zero_p(P
->Ray
[r
][P
->Dimension
+1])) {
1484 for (i
= P
->Dimension
- nparam
; i
< P
->Dimension
; ++i
)
1485 if (value_notzero_p(P
->Ray
[r
][i
+1]))
1487 if (i
>= P
->Dimension
)
1495 P
= remove_equalities_p(P
, P
->Dimension
-nparam
, &f
);
1499 if (P
->Dimension
== nparam
) {
1501 P
= Universe_Polyhedron(0);
1505 Polyhedron
*Q
= ParamPolyhedron_Reduce(P
, P
->Dimension
-nparam
, &factor
);
1508 if (Q
->Dimension
== nparam
) {
1510 P
= Universe_Polyhedron(0);
1515 Polyhedron
*oldP
= P
;
1516 PP
= Polyhedron2Param_SimplifiedDomain(&P
,C
,MaxRays
,&CEq
,&CT
);
1518 Polyhedron_Free(oldP
);
1520 if (isIdentity(CT
)) {
1524 assert(CT
->NbRows
!= CT
->NbColumns
);
1525 if (CT
->NbRows
== 1) // no more parameters
1527 nparam
= CT
->NbRows
- 1;
1530 unsigned dim
= P
->Dimension
- nparam
;
1531 Polyhedron
** vcone
= new (Polyhedron
*)[PP
->nbV
];
1532 int * npos
= new int[PP
->nbV
];
1533 int * nneg
= new int[PP
->nbV
];
1537 for (i
= 0, V
= PP
->V
; V
; ++i
, V
= V
->next
) {
1538 Polyhedron
*C
= supporting_cone_p(P
, V
);
1539 decompose(C
, &vcone
[i
], &npos
[i
], &nneg
[i
], MaxRays
);
1542 Vector
*c
= Vector_Alloc(dim
+2);
1545 for (nd
= 0, D
=PP
->D
; D
; ++nd
, D
=D
->next
);
1546 struct section
{ Polyhedron
*D
; evalue E
; };
1547 section
*s
= new section
[nd
];
1548 Polyhedron
**fVD
= new (Polyhedron
*)[nd
];
1550 for(nd
= 0, D
=PP
->D
; D
; D
=next
) {
1553 Polyhedron
*rVD
= reduce_domain(D
->Domain
, CT
, CEq
,
1558 pVD
= CT
? DomainImage(rVD
,CT
,MaxRays
) : rVD
;
1561 sign
.SetLength(ncone
);
1562 FORALL_PVertex_in_ParamPolyhedron(V
,D
,PP
) // _i is internal counter
1563 ncone
+= npos
[_i
] + nneg
[_i
];
1564 sign
.SetLength(ncone
);
1565 for (int k
= 0; k
< npos
[_i
]; ++k
)
1566 sign
[ncone
-nneg
[_i
]-k
-1] = 1;
1567 for (int k
= 0; k
< nneg
[_i
]; ++k
)
1568 sign
[ncone
-k
-1] = -1;
1569 END_FORALL_PVertex_in_ParamPolyhedron
;
1572 rays
.SetDims(ncone
* dim
, dim
);
1574 FORALL_PVertex_in_ParamPolyhedron(V
,D
,PP
) // _i is internal counter
1575 for (Polyhedron
*i
= vcone
[_i
]; i
; i
= i
->next
) {
1576 assert(i
->NbRays
-1 == dim
);
1577 add_rays(rays
, i
, &r
);
1579 END_FORALL_PVertex_in_ParamPolyhedron
;
1581 nonorthog(rays
, lambda
);
1584 den
.SetDims(ncone
,dim
);
1585 term_info
*num
= new term_info
[ncone
];
1588 FORALL_PVertex_in_ParamPolyhedron(V
,D
,PP
)
1589 for (Polyhedron
*i
= vcone
[_i
]; i
; i
= i
->next
) {
1590 lattice_point(V
, i
, lambda
, &num
[f
], pVD
);
1591 normalize(i
, lambda
, sign
[f
], num
[f
].constant
, den
[f
]);
1594 END_FORALL_PVertex_in_ParamPolyhedron
;
1595 ZZ min
= num
[0].constant
;
1596 for (int j
= 1; j
< ncone
; ++j
)
1597 if (num
[j
].constant
< min
)
1598 min
= num
[j
].constant
;
1599 for (int j
= 0; j
< ncone
; ++j
)
1600 num
[j
].constant
-= min
;
1602 value_init(s
[nd
].E
.d
);
1603 evalue_set_si(&s
[nd
].E
, 0, 1);
1606 FORALL_PVertex_in_ParamPolyhedron(V
,D
,PP
)
1607 for (Polyhedron
*i
= vcone
[_i
]; i
; i
= i
->next
) {
1608 dpoly
n(dim
, den
[f
][0], 1);
1609 for (int k
= 1; k
< dim
; ++k
) {
1610 dpoly
fact(dim
, den
[f
][k
], 1);
1613 if (num
[f
].E
!= NULL
) {
1614 ZZ
one(INIT_VAL
, 1);
1615 dpoly_n
d(dim
, num
[f
].constant
, one
);
1616 d
.div(n
, c
, sign
[f
]);
1618 multi_polynom(c
, num
[f
].E
, &EV
);
1619 eadd(&EV
, &s
[nd
].E
);
1620 free_evalue_refs(&EV
);
1621 free_evalue_refs(num
[f
].E
);
1623 } else if (num
[f
].pos
!= -1) {
1624 dpoly_n
d(dim
, num
[f
].constant
, num
[f
].coeff
);
1625 d
.div(n
, c
, sign
[f
]);
1627 uni_polynom(num
[f
].pos
, c
, &EV
);
1628 eadd(&EV
, &s
[nd
].E
);
1629 free_evalue_refs(&EV
);
1631 mpq_set_si(count
, 0, 1);
1632 dpoly
d(dim
, num
[f
].constant
);
1633 d
.div(n
, count
, sign
[f
]);
1636 evalue_set(&EV
, &count
[0]._mp_num
, &count
[0]._mp_den
);
1637 eadd(&EV
, &s
[nd
].E
);
1638 free_evalue_refs(&EV
);
1642 END_FORALL_PVertex_in_ParamPolyhedron
;
1648 addeliminatedparams_evalue(&s
[nd
].E
, CT
);
1656 evalue_set_si(eres
, 0, 1);
1658 eres
->x
.p
= new_enode(partition
, 2*nd
, -1);
1659 for (int j
= 0; j
< nd
; ++j
) {
1660 EVALUE_SET_DOMAIN(eres
->x
.p
->arr
[2*j
], s
[j
].D
);
1661 value_clear(eres
->x
.p
->arr
[2*j
+1].d
);
1662 eres
->x
.p
->arr
[2*j
+1] = s
[j
].E
;
1663 Domain_Free(fVD
[j
]);
1671 for (int j
= 0; j
< PP
->nbV
; ++j
)
1672 Domain_Free(vcone
[j
]);
1678 Polyhedron_Free(CEq
);
1683 Enumeration
* barvinok_enumerate(Polyhedron
*P
, Polyhedron
* C
, unsigned MaxRays
)
1685 evalue
*EP
= barvinok_enumerate_ev(P
, C
, MaxRays
);
1687 return partition2enumeration(EP
);
1690 static void SwapColumns(Value
**V
, int n
, int i
, int j
)
1692 for (int r
= 0; r
< n
; ++r
)
1693 value_swap(V
[r
][i
], V
[r
][j
]);
1696 static void SwapColumns(Polyhedron
*P
, int i
, int j
)
1698 SwapColumns(P
->Constraint
, P
->NbConstraints
, i
, j
);
1699 SwapColumns(P
->Ray
, P
->NbRays
, i
, j
);
1702 static bool SplitOnConstraint(Polyhedron
*P
, int i
, int l
, int u
,
1703 int nvar
, int len
, int exist
, int MaxRays
,
1704 Vector
*row
, Value
& f
, bool independent
,
1705 Polyhedron
**pos
, Polyhedron
**neg
)
1707 value_oppose(f
, P
->Constraint
[u
][nvar
+i
+1]);
1708 Vector_Combine(P
->Constraint
[l
]+1, P
->Constraint
[u
]+1,
1710 f
, P
->Constraint
[l
][nvar
+i
+1], len
-1);
1712 //printf("l: %d, u: %d\n", l, u);
1713 value_multiply(f
, f
, P
->Constraint
[l
][nvar
+i
+1]);
1714 value_substract(row
->p
[len
-1], row
->p
[len
-1], f
);
1715 value_set_si(f
, -1);
1716 Vector_Scale(row
->p
+1, row
->p
+1, f
, len
-1);
1717 value_decrement(row
->p
[len
-1], row
->p
[len
-1]);
1718 Vector_Gcd(row
->p
+1, len
- 2, &f
);
1719 if (value_notone_p(f
)) {
1720 Vector_AntiScale(row
->p
+1, row
->p
+1, f
, len
-2);
1721 mpz_fdiv_q(row
->p
[len
-1], row
->p
[len
-1], f
);
1723 *neg
= AddConstraints(row
->p
, 1, P
, MaxRays
);
1725 /* We found an independent, but useless constraint
1726 * Maybe we should detect this earlier and not
1727 * mark the variable as INDEPENDENT
1729 if (emptyQ((*neg
))) {
1730 Polyhedron_Free(*neg
);
1734 value_set_si(f
, -1);
1735 Vector_Scale(row
->p
+1, row
->p
+1, f
, len
-1);
1736 value_decrement(row
->p
[len
-1], row
->p
[len
-1]);
1737 *pos
= AddConstraints(row
->p
, 1, P
, MaxRays
);
1739 if (emptyQ((*pos
))) {
1740 Polyhedron_Free(*neg
);
1741 Polyhedron_Free(*pos
);
1749 * unimodularly transform P such that constraint r is transformed
1750 * into a constraint that involves only a single (the first)
1751 * existential variable
1754 static Polyhedron
*rotate_along(Polyhedron
*P
, int r
, int nvar
, int exist
,
1760 Vector
*row
= Vector_Alloc(exist
);
1761 Vector_Copy(P
->Constraint
[r
]+1+nvar
, row
->p
, exist
);
1762 Vector_Gcd(row
->p
, exist
, &g
);
1763 if (value_notone_p(g
))
1764 Vector_AntiScale(row
->p
, row
->p
, g
, exist
);
1767 Matrix
*M
= unimodular_complete(row
);
1768 Matrix
*M2
= Matrix_Alloc(P
->Dimension
+1, P
->Dimension
+1);
1769 for (r
= 0; r
< nvar
; ++r
)
1770 value_set_si(M2
->p
[r
][r
], 1);
1771 for ( ; r
< nvar
+exist
; ++r
)
1772 Vector_Copy(M
->p
[r
-nvar
], M2
->p
[r
]+nvar
, exist
);
1773 for ( ; r
< P
->Dimension
+1; ++r
)
1774 value_set_si(M2
->p
[r
][r
], 1);
1775 Polyhedron
*T
= Polyhedron_Image(P
, M2
, MaxRays
);
1784 static bool SplitOnVar(Polyhedron
*P
, int i
,
1785 int nvar
, int len
, int exist
, int MaxRays
,
1786 Vector
*row
, Value
& f
, bool independent
,
1787 Polyhedron
**pos
, Polyhedron
**neg
)
1791 for (int l
= P
->NbEq
; l
< P
->NbConstraints
; ++l
) {
1792 if (value_negz_p(P
->Constraint
[l
][nvar
+i
+1]))
1796 for (j
= 0; j
< exist
; ++j
)
1797 if (j
!= i
&& value_notzero_p(P
->Constraint
[l
][nvar
+j
+1]))
1803 for (int u
= P
->NbEq
; u
< P
->NbConstraints
; ++u
) {
1804 if (value_posz_p(P
->Constraint
[u
][nvar
+i
+1]))
1808 for (j
= 0; j
< exist
; ++j
)
1809 if (j
!= i
&& value_notzero_p(P
->Constraint
[u
][nvar
+j
+1]))
1815 if (SplitOnConstraint(P
, i
, l
, u
,
1816 nvar
, len
, exist
, MaxRays
,
1817 row
, f
, independent
,
1821 SwapColumns(*neg
, nvar
+1, nvar
+1+i
);
1831 static bool double_bound_pair(Polyhedron
*P
, int nvar
, int exist
,
1832 int i
, int l1
, int l2
,
1833 Polyhedron
**pos
, Polyhedron
**neg
)
1837 Vector
*row
= Vector_Alloc(P
->Dimension
+2);
1838 value_set_si(row
->p
[0], 1);
1839 value_oppose(f
, P
->Constraint
[l1
][nvar
+i
+1]);
1840 Vector_Combine(P
->Constraint
[l1
]+1, P
->Constraint
[l2
]+1,
1842 P
->Constraint
[l2
][nvar
+i
+1], f
,
1844 ConstraintSimplify(row
->p
, row
->p
, P
->Dimension
+2, &f
);
1845 *pos
= AddConstraints(row
->p
, 1, P
, 0);
1846 value_set_si(f
, -1);
1847 Vector_Scale(row
->p
+1, row
->p
+1, f
, P
->Dimension
+1);
1848 value_decrement(row
->p
[P
->Dimension
+1], row
->p
[P
->Dimension
+1]);
1849 *neg
= AddConstraints(row
->p
, 1, P
, 0);
1853 return !emptyQ((*pos
)) && !emptyQ((*neg
));
1856 static bool double_bound(Polyhedron
*P
, int nvar
, int exist
,
1857 Polyhedron
**pos
, Polyhedron
**neg
)
1859 for (int i
= 0; i
< exist
; ++i
) {
1861 for (l1
= P
->NbEq
; l1
< P
->NbConstraints
; ++l1
) {
1862 if (value_negz_p(P
->Constraint
[l1
][nvar
+i
+1]))
1864 for (l2
= l1
+ 1; l2
< P
->NbConstraints
; ++l2
) {
1865 if (value_negz_p(P
->Constraint
[l2
][nvar
+i
+1]))
1867 if (double_bound_pair(P
, nvar
, exist
, i
, l1
, l2
, pos
, neg
))
1871 for (l1
= P
->NbEq
; l1
< P
->NbConstraints
; ++l1
) {
1872 if (value_posz_p(P
->Constraint
[l1
][nvar
+i
+1]))
1874 if (l1
< P
->NbConstraints
)
1875 for (l2
= l1
+ 1; l2
< P
->NbConstraints
; ++l2
) {
1876 if (value_posz_p(P
->Constraint
[l2
][nvar
+i
+1]))
1878 if (double_bound_pair(P
, nvar
, exist
, i
, l1
, l2
, pos
, neg
))
1890 INDEPENDENT
= 1 << 2,
1893 static evalue
* enumerate_or(Polyhedron
*pos
, Polyhedron
*neg
,
1894 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
1897 fprintf(stderr
, "\nER: Or\n");
1898 #endif /* DEBUG_ER */
1901 barvinok_enumerate_e(neg
, exist
, nparam
, MaxRays
);
1903 barvinok_enumerate_e(pos
, exist
, nparam
, MaxRays
);
1906 evalue_copy(&E
, EP
);
1909 free_evalue_refs(EN
);
1911 evalue_set_si(EN
, -1, 1);
1915 free_evalue_refs(EN
);
1917 free_evalue_refs(&E
);
1918 Polyhedron_Free(neg
);
1919 Polyhedron_Free(pos
);
1926 static evalue
* enumerate_sum(Polyhedron
*P
,
1927 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
1929 int nvar
= P
->Dimension
- exist
- nparam
;
1930 int toswap
= nvar
< exist
? nvar
: exist
;
1931 for (int i
= 0; i
< toswap
; ++i
)
1932 SwapColumns(P
, 1 + i
, nvar
+exist
- i
);
1936 fprintf(stderr
, "\nER: Sum\n");
1937 #endif /* DEBUG_ER */
1939 evalue
*EP
= barvinok_enumerate_e(P
, exist
, nparam
, MaxRays
);
1941 for (int i
= 0; i
< /* nvar */ nparam
; ++i
) {
1942 Matrix
*C
= Matrix_Alloc(1, 1 + nparam
+ 1);
1943 value_set_si(C
->p
[0][0], 1);
1945 value_init(split
.d
);
1946 value_set_si(split
.d
, 0);
1947 split
.x
.p
= new_enode(partition
, 4, -1);
1948 value_set_si(C
->p
[0][1+i
], 1);
1949 Matrix
*C2
= Matrix_Copy(C
);
1950 EVALUE_SET_DOMAIN(split
.x
.p
->arr
[0],
1951 Constraints2Polyhedron(C2
, MaxRays
));
1953 evalue_set_si(&split
.x
.p
->arr
[1], 1, 1);
1954 value_set_si(C
->p
[0][1+i
], -1);
1955 value_set_si(C
->p
[0][1+nparam
], -1);
1956 EVALUE_SET_DOMAIN(split
.x
.p
->arr
[2],
1957 Constraints2Polyhedron(C
, MaxRays
));
1958 evalue_set_si(&split
.x
.p
->arr
[3], 1, 1);
1960 free_evalue_refs(&split
);
1964 evalue_range_reduction(EP
);
1966 evalue_frac2floor(EP
);
1968 evalue
*sum
= esum(EP
, nvar
);
1970 free_evalue_refs(EP
);
1974 evalue_range_reduction(EP
);
1979 static evalue
* split_sure(Polyhedron
*P
, Polyhedron
*S
,
1980 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
1982 int nvar
= P
->Dimension
- exist
- nparam
;
1984 Matrix
*M
= Matrix_Alloc(exist
, S
->Dimension
+2);
1985 for (int i
= 0; i
< exist
; ++i
)
1986 value_set_si(M
->p
[i
][nvar
+i
+1], 1);
1988 S
= DomainAddRays(S
, M
, MaxRays
);
1990 Polyhedron
*F
= DomainAddRays(P
, M
, MaxRays
);
1991 Polyhedron
*D
= DomainDifference(F
, S
, MaxRays
);
1993 D
= Disjoint_Domain(D
, 0, MaxRays
);
1998 M
= Matrix_Alloc(P
->Dimension
+1-exist
, P
->Dimension
+1);
1999 for (int j
= 0; j
< nvar
; ++j
)
2000 value_set_si(M
->p
[j
][j
], 1);
2001 for (int j
= 0; j
< nparam
+1; ++j
)
2002 value_set_si(M
->p
[nvar
+j
][nvar
+exist
+j
], 1);
2003 Polyhedron
*T
= Polyhedron_Image(S
, M
, MaxRays
);
2004 evalue
*EP
= barvinok_enumerate_e(T
, 0, nparam
, MaxRays
);
2009 for (Polyhedron
*Q
= D
; Q
; Q
= Q
->next
) {
2010 Polyhedron
*N
= Q
->next
;
2012 T
= DomainIntersection(P
, Q
, MaxRays
);
2013 evalue
*E
= barvinok_enumerate_e(T
, exist
, nparam
, MaxRays
);
2015 free_evalue_refs(E
);
2024 static evalue
* enumerate_sure(Polyhedron
*P
,
2025 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
2029 int nvar
= P
->Dimension
- exist
- nparam
;
2031 for (i
= 0; i
< exist
; ++i
) {
2032 Matrix
*M
= Matrix_Alloc(S
->NbConstraints
, S
->Dimension
+2);
2034 for (int j
= 0; j
< S
->NbConstraints
; ++j
) {
2035 if (value_negz_p(S
->Constraint
[j
][1+nvar
+i
]))
2037 if (value_one_p(S
->Constraint
[j
][1+nvar
+i
]))
2039 Vector_Copy(S
->Constraint
[j
], M
->p
[c
], S
->Dimension
+2);
2040 value_substract(M
->p
[c
][S
->Dimension
+1],
2041 M
->p
[c
][S
->Dimension
+1],
2042 S
->Constraint
[j
][1+nvar
+i
]);
2043 value_increment(M
->p
[c
][S
->Dimension
+1],
2044 M
->p
[c
][S
->Dimension
+1]);
2048 S
= AddConstraints(M
->p
[0], c
, S
, MaxRays
);
2059 fprintf(stderr
, "\nER: Sure\n");
2060 #endif /* DEBUG_ER */
2062 return split_sure(P
, S
, exist
, nparam
, MaxRays
);
2065 static evalue
* enumerate_sure2(Polyhedron
*P
,
2066 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
2068 int nvar
= P
->Dimension
- exist
- nparam
;
2070 for (r
= 0; r
< P
->NbRays
; ++r
)
2071 if (value_one_p(P
->Ray
[r
][0]) &&
2072 value_one_p(P
->Ray
[r
][P
->Dimension
+1]))
2078 Matrix
*M
= Matrix_Alloc(nvar
+ 1 + nparam
, P
->Dimension
+2);
2079 for (int i
= 0; i
< nvar
; ++i
)
2080 value_set_si(M
->p
[i
][1+i
], 1);
2081 for (int i
= 0; i
< nparam
; ++i
)
2082 value_set_si(M
->p
[i
+nvar
][1+nvar
+exist
+i
], 1);
2083 Vector_Copy(P
->Ray
[r
]+1+nvar
, M
->p
[nvar
+nparam
]+1+nvar
, exist
);
2084 value_set_si(M
->p
[nvar
+nparam
][0], 1);
2085 value_set_si(M
->p
[nvar
+nparam
][P
->Dimension
+1], 1);
2086 Polyhedron
* F
= Rays2Polyhedron(M
, MaxRays
);
2089 Polyhedron
*I
= DomainIntersection(F
, P
, MaxRays
);
2093 fprintf(stderr
, "\nER: Sure2\n");
2094 #endif /* DEBUG_ER */
2096 return split_sure(P
, I
, exist
, nparam
, MaxRays
);
2099 static evalue
* new_zero_ep()
2104 evalue_set_si(EP
, 0, 1);
2108 static evalue
* enumerate_vd(Polyhedron
**PA
,
2109 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
2111 Polyhedron
*P
= *PA
;
2112 int nvar
= P
->Dimension
- exist
- nparam
;
2113 Param_Polyhedron
*PP
= NULL
;
2114 Polyhedron
*C
= Universe_Polyhedron(nparam
);
2118 PP
= Polyhedron2Param_SimplifiedDomain(&PR
,C
,MaxRays
,&CEq
,&CT
);
2122 Param_Domain
*D
, *last
;
2125 for (nd
= 0, D
=PP
->D
; D
; D
=D
->next
, ++nd
)
2128 Polyhedron
**VD
= new (Polyhedron
*)[nd
];
2129 Polyhedron
**fVD
= new (Polyhedron
*)[nd
];
2130 for(nd
= 0, D
=PP
->D
; D
; D
=D
->next
) {
2131 Polyhedron
*rVD
= reduce_domain(D
->Domain
, CT
, CEq
,
2145 /* This doesn't seem to have any effect */
2147 Polyhedron
*CA
= align_context(VD
[0], P
->Dimension
, MaxRays
);
2149 P
= DomainIntersection(P
, CA
, MaxRays
);
2152 Polyhedron_Free(CA
);
2157 if (!EP
&& CT
->NbColumns
!= CT
->NbRows
) {
2158 Polyhedron
*CEqr
= DomainImage(CEq
, CT
, MaxRays
);
2159 Polyhedron
*CA
= align_context(CEqr
, PR
->Dimension
, MaxRays
);
2160 Polyhedron
*I
= DomainIntersection(PR
, CA
, MaxRays
);
2161 Polyhedron_Free(CEqr
);
2162 Polyhedron_Free(CA
);
2164 fprintf(stderr
, "\nER: Eliminate\n");
2165 #endif /* DEBUG_ER */
2166 nparam
-= CT
->NbColumns
- CT
->NbRows
;
2167 EP
= barvinok_enumerate_e(I
, exist
, nparam
, MaxRays
);
2168 addeliminatedparams_enum(EP
, CT
, CEq
, MaxRays
);
2173 if (!EP
&& nd
> 1) {
2175 fprintf(stderr
, "\nER: VD\n");
2176 #endif /* DEBUG_ER */
2177 for (int i
= 0; i
< nd
; ++i
) {
2178 Polyhedron
*CA
= align_context(VD
[i
], P
->Dimension
, MaxRays
);
2179 Polyhedron
*I
= DomainIntersection(P
, CA
, MaxRays
);
2182 EP
= barvinok_enumerate_e(I
, exist
, nparam
, MaxRays
);
2184 evalue
*E
= barvinok_enumerate_e(I
, exist
, nparam
, MaxRays
);
2186 free_evalue_refs(E
);
2190 Polyhedron_Free(CA
);
2194 for (int i
= 0; i
< nd
; ++i
) {
2195 Polyhedron_Free(VD
[i
]);
2196 Polyhedron_Free(fVD
[i
]);
2202 if (!EP
&& nvar
== 0) {
2205 Param_Vertices
*V
, *V2
;
2206 Matrix
* M
= Matrix_Alloc(1, P
->Dimension
+2);
2208 FORALL_PVertex_in_ParamPolyhedron(V
, last
, PP
) {
2210 FORALL_PVertex_in_ParamPolyhedron(V2
, last
, PP
) {
2217 for (int i
= 0; i
< exist
; ++i
) {
2218 value_oppose(f
, V
->Vertex
->p
[i
][nparam
+1]);
2219 Vector_Combine(V
->Vertex
->p
[i
],
2221 M
->p
[0] + 1 + nvar
+ exist
,
2222 V2
->Vertex
->p
[i
][nparam
+1],
2226 for (j
= 0; j
< nparam
; ++j
)
2227 if (value_notzero_p(M
->p
[0][1+nvar
+exist
+j
]))
2231 ConstraintSimplify(M
->p
[0], M
->p
[0],
2232 P
->Dimension
+2, &f
);
2233 value_set_si(M
->p
[0][0], 0);
2234 Polyhedron
*para
= AddConstraints(M
->p
[0], 1, P
,
2237 Polyhedron_Free(para
);
2240 Polyhedron
*pos
, *neg
;
2241 value_set_si(M
->p
[0][0], 1);
2242 value_decrement(M
->p
[0][P
->Dimension
+1],
2243 M
->p
[0][P
->Dimension
+1]);
2244 neg
= AddConstraints(M
->p
[0], 1, P
, MaxRays
);
2245 value_set_si(f
, -1);
2246 Vector_Scale(M
->p
[0]+1, M
->p
[0]+1, f
,
2248 value_decrement(M
->p
[0][P
->Dimension
+1],
2249 M
->p
[0][P
->Dimension
+1]);
2250 value_decrement(M
->p
[0][P
->Dimension
+1],
2251 M
->p
[0][P
->Dimension
+1]);
2252 pos
= AddConstraints(M
->p
[0], 1, P
, MaxRays
);
2253 if (emptyQ(neg
) && emptyQ(pos
)) {
2254 Polyhedron_Free(para
);
2255 Polyhedron_Free(pos
);
2256 Polyhedron_Free(neg
);
2260 fprintf(stderr
, "\nER: Order\n");
2261 #endif /* DEBUG_ER */
2262 EP
= barvinok_enumerate_e(para
, exist
, nparam
, MaxRays
);
2265 E
= barvinok_enumerate_e(pos
, exist
, nparam
, MaxRays
);
2267 free_evalue_refs(E
);
2271 E
= barvinok_enumerate_e(neg
, exist
, nparam
, MaxRays
);
2273 free_evalue_refs(E
);
2280 } END_FORALL_PVertex_in_ParamPolyhedron
;
2283 } END_FORALL_PVertex_in_ParamPolyhedron
;
2286 /* Search for vertex coordinate to split on */
2287 /* First look for one independent of the parameters */
2288 FORALL_PVertex_in_ParamPolyhedron(V
, last
, PP
) {
2289 for (int i
= 0; i
< exist
; ++i
) {
2291 for (j
= 0; j
< nparam
; ++j
)
2292 if (value_notzero_p(V
->Vertex
->p
[i
][j
]))
2296 value_set_si(M
->p
[0][0], 1);
2297 Vector_Set(M
->p
[0]+1, 0, nvar
+exist
);
2298 Vector_Copy(V
->Vertex
->p
[i
],
2299 M
->p
[0] + 1 + nvar
+ exist
, nparam
+1);
2300 value_oppose(M
->p
[0][1+nvar
+i
],
2301 V
->Vertex
->p
[i
][nparam
+1]);
2303 Polyhedron
*pos
, *neg
;
2304 value_set_si(M
->p
[0][0], 1);
2305 value_decrement(M
->p
[0][P
->Dimension
+1],
2306 M
->p
[0][P
->Dimension
+1]);
2307 neg
= AddConstraints(M
->p
[0], 1, P
, MaxRays
);
2308 value_set_si(f
, -1);
2309 Vector_Scale(M
->p
[0]+1, M
->p
[0]+1, f
,
2311 value_decrement(M
->p
[0][P
->Dimension
+1],
2312 M
->p
[0][P
->Dimension
+1]);
2313 value_decrement(M
->p
[0][P
->Dimension
+1],
2314 M
->p
[0][P
->Dimension
+1]);
2315 pos
= AddConstraints(M
->p
[0], 1, P
, MaxRays
);
2316 if (emptyQ(neg
) || emptyQ(pos
)) {
2317 Polyhedron_Free(pos
);
2318 Polyhedron_Free(neg
);
2321 Polyhedron_Free(pos
);
2322 value_increment(M
->p
[0][P
->Dimension
+1],
2323 M
->p
[0][P
->Dimension
+1]);
2324 pos
= AddConstraints(M
->p
[0], 1, P
, MaxRays
);
2326 fprintf(stderr
, "\nER: Vertex\n");
2327 #endif /* DEBUG_ER */
2328 EP
= enumerate_or(pos
, neg
, exist
, nparam
, MaxRays
);
2333 } END_FORALL_PVertex_in_ParamPolyhedron
;
2337 /* Search for vertex coordinate to split on */
2338 /* Now look for one that depends on the parameters */
2339 FORALL_PVertex_in_ParamPolyhedron(V
, last
, PP
) {
2340 for (int i
= 0; i
< exist
; ++i
) {
2341 value_set_si(M
->p
[0][0], 1);
2342 Vector_Set(M
->p
[0]+1, 0, nvar
+exist
);
2343 Vector_Copy(V
->Vertex
->p
[i
],
2344 M
->p
[0] + 1 + nvar
+ exist
, nparam
+1);
2345 value_oppose(M
->p
[0][1+nvar
+i
],
2346 V
->Vertex
->p
[i
][nparam
+1]);
2348 Polyhedron
*pos
, *neg
;
2349 value_set_si(M
->p
[0][0], 1);
2350 value_decrement(M
->p
[0][P
->Dimension
+1],
2351 M
->p
[0][P
->Dimension
+1]);
2352 neg
= AddConstraints(M
->p
[0], 1, P
, MaxRays
);
2353 value_set_si(f
, -1);
2354 Vector_Scale(M
->p
[0]+1, M
->p
[0]+1, f
,
2356 value_decrement(M
->p
[0][P
->Dimension
+1],
2357 M
->p
[0][P
->Dimension
+1]);
2358 value_decrement(M
->p
[0][P
->Dimension
+1],
2359 M
->p
[0][P
->Dimension
+1]);
2360 pos
= AddConstraints(M
->p
[0], 1, P
, MaxRays
);
2361 if (emptyQ(neg
) || emptyQ(pos
)) {
2362 Polyhedron_Free(pos
);
2363 Polyhedron_Free(neg
);
2366 Polyhedron_Free(pos
);
2367 value_increment(M
->p
[0][P
->Dimension
+1],
2368 M
->p
[0][P
->Dimension
+1]);
2369 pos
= AddConstraints(M
->p
[0], 1, P
, MaxRays
);
2371 fprintf(stderr
, "\nER: ParamVertex\n");
2372 #endif /* DEBUG_ER */
2373 EP
= enumerate_or(pos
, neg
, exist
, nparam
, MaxRays
);
2378 } END_FORALL_PVertex_in_ParamPolyhedron
;
2386 Polyhedron_Free(CEq
);
2390 Param_Polyhedron_Free(PP
);
2396 static evalue
* barvinok_enumerate_e_r(Polyhedron
*P
,
2397 unsigned exist
, unsigned nparam
, unsigned MaxRays
);
2400 static int er_level
= 0;
2402 evalue
* barvinok_enumerate_e(Polyhedron
*P
,
2403 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
2405 fprintf(stderr
, "\nER: level %i\n", er_level
);
2406 int nvar
= P
->Dimension
- exist
- nparam
;
2407 fprintf(stderr
, "%d %d %d\n", nvar
, exist
, nparam
);
2409 Polyhedron_Print(stderr
, P_VALUE_FMT
, P
);
2411 P
= DomainConstraintSimplify(Polyhedron_Copy(P
), MaxRays
);
2412 evalue
*EP
= barvinok_enumerate_e_r(P
, exist
, nparam
, MaxRays
);
2418 evalue
* barvinok_enumerate_e(Polyhedron
*P
,
2419 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
2421 P
= DomainConstraintSimplify(Polyhedron_Copy(P
), MaxRays
);
2422 evalue
*EP
= barvinok_enumerate_e_r(P
, exist
, nparam
, MaxRays
);
2428 static evalue
* barvinok_enumerate_e_r(Polyhedron
*P
,
2429 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
2432 Polyhedron
*U
= Universe_Polyhedron(nparam
);
2433 evalue
*EP
= barvinok_enumerate_ev(P
, U
, MaxRays
);
2434 //char *param_name[] = {"P", "Q", "R", "S", "T" };
2435 //print_evalue(stdout, EP, param_name);
2440 int nvar
= P
->Dimension
- exist
- nparam
;
2441 int len
= P
->Dimension
+ 2;
2444 return new_zero_ep();
2446 if (nvar
== 0 && nparam
== 0) {
2447 evalue
*EP
= new_zero_ep();
2448 barvinok_count(P
, &EP
->x
.n
, MaxRays
);
2449 if (value_pos_p(EP
->x
.n
))
2450 value_set_si(EP
->x
.n
, 1);
2455 for (r
= 0; r
< P
->NbRays
; ++r
)
2456 if (value_zero_p(P
->Ray
[r
][0]) ||
2457 value_zero_p(P
->Ray
[r
][P
->Dimension
+1])) {
2459 for (i
= 0; i
< nvar
; ++i
)
2460 if (value_notzero_p(P
->Ray
[r
][i
+1]))
2464 for (i
= nvar
; i
< nvar
+ exist
; ++i
)
2465 if (value_notzero_p(P
->Ray
[r
][i
+1]))
2467 if (i
>= nvar
+ exist
)
2470 if (r
< P
->NbRays
) {
2471 evalue
*EP
= new_zero_ep();
2472 value_set_si(EP
->x
.n
, -1);
2477 for (r
= 0; r
< P
->NbEq
; ++r
)
2478 if ((first
= First_Non_Zero(P
->Constraint
[r
]+1+nvar
, exist
)) != -1)
2481 if (First_Non_Zero(P
->Constraint
[r
]+1+nvar
+first
+1,
2482 exist
-first
-1) != -1) {
2483 Polyhedron
*T
= rotate_along(P
, r
, nvar
, exist
, MaxRays
);
2485 fprintf(stderr
, "\nER: Equality\n");
2486 #endif /* DEBUG_ER */
2487 evalue
*EP
= barvinok_enumerate_e(T
, exist
-1, nparam
, MaxRays
);
2492 fprintf(stderr
, "\nER: Fixed\n");
2493 #endif /* DEBUG_ER */
2495 return barvinok_enumerate_e(P
, exist
-1, nparam
, MaxRays
);
2497 Polyhedron
*T
= Polyhedron_Copy(P
);
2498 SwapColumns(T
, nvar
+1, nvar
+1+first
);
2499 evalue
*EP
= barvinok_enumerate_e(T
, exist
-1, nparam
, MaxRays
);
2506 Vector
*row
= Vector_Alloc(len
);
2507 value_set_si(row
->p
[0], 1);
2512 enum constraint info
[exist
];
2513 for (int i
= 0; i
< exist
; ++i
) {
2515 for (int l
= P
->NbEq
; l
< P
->NbConstraints
; ++l
) {
2516 if (value_negz_p(P
->Constraint
[l
][nvar
+i
+1]))
2518 for (int u
= P
->NbEq
; u
< P
->NbConstraints
; ++u
) {
2519 if (value_posz_p(P
->Constraint
[u
][nvar
+i
+1]))
2521 value_oppose(f
, P
->Constraint
[u
][nvar
+i
+1]);
2522 Vector_Combine(P
->Constraint
[l
]+1, P
->Constraint
[u
]+1, row
->p
+1,
2523 f
, P
->Constraint
[l
][nvar
+i
+1], len
-1);
2524 if (!(info
[i
] & INDEPENDENT
)) {
2526 for (j
= 0; j
< exist
; ++j
)
2527 if (j
!= i
&& value_notzero_p(row
->p
[nvar
+j
+1]))
2530 //printf("independent: i: %d, l: %d, u: %d\n", i, l, u);
2531 info
[i
] = (constraint
)(info
[i
] | INDEPENDENT
);
2534 if (info
[i
] & ALL_POS
) {
2535 value_addto(row
->p
[len
-1], row
->p
[len
-1],
2536 P
->Constraint
[l
][nvar
+i
+1]);
2537 value_addto(row
->p
[len
-1], row
->p
[len
-1], f
);
2538 value_multiply(f
, f
, P
->Constraint
[l
][nvar
+i
+1]);
2539 value_substract(row
->p
[len
-1], row
->p
[len
-1], f
);
2540 value_decrement(row
->p
[len
-1], row
->p
[len
-1]);
2541 Vector_Gcd(row
->p
+1, len
- 2, &f
);
2542 if (value_notone_p(f
)) {
2543 Vector_AntiScale(row
->p
+1, row
->p
+1, f
, len
-2);
2544 mpz_fdiv_q(row
->p
[len
-1], row
->p
[len
-1], f
);
2546 value_set_si(f
, -1);
2547 Vector_Scale(row
->p
+1, row
->p
+1, f
, len
-1);
2548 value_decrement(row
->p
[len
-1], row
->p
[len
-1]);
2549 Polyhedron
*T
= AddConstraints(row
->p
, 1, P
, MaxRays
);
2551 //printf("not all_pos: i: %d, l: %d, u: %d\n", i, l, u);
2552 info
[i
] = (constraint
)(info
[i
] ^ ALL_POS
);
2554 //puts("pos remainder");
2555 //Polyhedron_Print(stdout, P_VALUE_FMT, T);
2558 if (!(info
[i
] & ONE_NEG
)) {
2560 for (j
= 0; j
< exist
; ++j
)
2562 value_notzero_p(P
->Constraint
[l
][nvar
+j
+1]))
2565 for (j
= 0; j
< exist
; ++j
)
2567 value_notzero_p(P
->Constraint
[u
][nvar
+j
+1]))
2570 /* recalculate constant */
2571 /* We actually recalculate the whole row for
2572 * now, because it may have already been scaled
2574 value_oppose(f
, P
->Constraint
[u
][nvar
+i
+1]);
2575 Vector_Combine(P
->Constraint
[l
]+1, P
->Constraint
[u
]+1,
2577 f
, P
->Constraint
[l
][nvar
+i
+1], len
-1);
2579 Vector_Combine(P->Constraint[l]+len-1,
2580 P->Constraint[u]+len-1, row->p+len-1,
2581 f, P->Constraint[l][nvar+i+1], 1);
2583 value_multiply(f
, f
, P
->Constraint
[l
][nvar
+i
+1]);
2584 value_substract(row
->p
[len
-1], row
->p
[len
-1], f
);
2585 value_set_si(f
, -1);
2586 Vector_Scale(row
->p
+1, row
->p
+1, f
, len
-1);
2587 value_decrement(row
->p
[len
-1], row
->p
[len
-1]);
2588 Vector_Gcd(row
->p
+1, len
- 2, &f
);
2589 if (value_notone_p(f
)) {
2590 Vector_AntiScale(row
->p
+1, row
->p
+1, f
, len
-2);
2591 mpz_fdiv_q(row
->p
[len
-1], row
->p
[len
-1], f
);
2593 value_set_si(f
, -1);
2594 Vector_Scale(row
->p
+1, row
->p
+1, f
, len
-1);
2595 value_decrement(row
->p
[len
-1], row
->p
[len
-1]);
2597 //Vector_Print(stdout, P_VALUE_FMT, row);
2598 Polyhedron
*T
= AddConstraints(row
->p
, 1, P
, MaxRays
);
2600 //printf("one_neg i: %d, l: %d, u: %d\n", i, l, u);
2601 info
[i
] = (constraint
)(info
[i
] | ONE_NEG
);
2603 //puts("neg remainder");
2604 //Polyhedron_Print(stdout, P_VALUE_FMT, T);
2608 if (!(info
[i
] & ALL_POS
) && (info
[i
] & ONE_NEG
))
2612 if (info
[i
] & ALL_POS
)
2619 for (int i = 0; i < exist; ++i)
2620 printf("%i: %i\n", i, info[i]);
2622 for (int i
= 0; i
< exist
; ++i
)
2623 if (info
[i
] & ALL_POS
) {
2625 fprintf(stderr
, "\nER: Positive\n");
2626 #endif /* DEBUG_ER */
2628 // Maybe we should chew off some of the fat here
2629 Matrix
*M
= Matrix_Alloc(P
->Dimension
, P
->Dimension
+1);
2630 for (int j
= 0; j
< P
->Dimension
; ++j
)
2631 value_set_si(M
->p
[j
][j
+ (j
>= i
+nvar
)], 1);
2632 Polyhedron
*T
= Polyhedron_Image(P
, M
, MaxRays
);
2634 evalue
*EP
= barvinok_enumerate_e(T
, exist
-1, nparam
, MaxRays
);
2640 for (int i
= 0; i
< exist
; ++i
)
2641 if (info
[i
] & ONE_NEG
) {
2643 fprintf(stderr
, "\nER: Negative\n");
2644 #endif /* DEBUG_ER */
2648 return barvinok_enumerate_e(P
, exist
-1, nparam
, MaxRays
);
2650 Polyhedron
*T
= Polyhedron_Copy(P
);
2651 SwapColumns(T
, nvar
+1, nvar
+1+i
);
2652 evalue
*EP
= barvinok_enumerate_e(T
, exist
-1, nparam
, MaxRays
);
2657 for (int i
= 0; i
< exist
; ++i
)
2658 if (info
[i
] & INDEPENDENT
) {
2659 Polyhedron
*pos
, *neg
;
2661 /* Find constraint again and split off negative part */
2663 if (SplitOnVar(P
, i
, nvar
, len
, exist
, MaxRays
,
2664 row
, f
, true, &pos
, &neg
)) {
2666 fprintf(stderr
, "\nER: Split\n");
2667 #endif /* DEBUG_ER */
2670 barvinok_enumerate_e(neg
, exist
-1, nparam
, MaxRays
);
2672 barvinok_enumerate_e(pos
, exist
, nparam
, MaxRays
);
2674 free_evalue_refs(E
);
2676 Polyhedron_Free(neg
);
2677 Polyhedron_Free(pos
);
2685 EP
= enumerate_sure(P
, exist
, nparam
, MaxRays
);
2689 EP
= enumerate_sure2(P
, exist
, nparam
, MaxRays
);
2693 Polyhedron
*F
= unfringe(P
, MaxRays
);
2694 if (!PolyhedronIncludes(F
, P
)) {
2696 fprintf(stderr
, "\nER: Fringed\n");
2697 #endif /* DEBUG_ER */
2698 EP
= barvinok_enumerate_e(F
, exist
, nparam
, MaxRays
);
2706 EP
= enumerate_vd(&P
, exist
, nparam
, MaxRays
);
2714 EP
= enumerate_sum(P
, exist
, nparam
, MaxRays
);
2723 Polyhedron
*pos
, *neg
;
2724 for (i
= 0; i
< exist
; ++i
)
2725 if (SplitOnVar(P
, i
, nvar
, len
, exist
, MaxRays
,
2726 row
, f
, false, &pos
, &neg
))
2731 EP
= enumerate_or(pos
, neg
, exist
, nparam
, MaxRays
);