8 #include <NTL/mat_ZZ.h>
12 #include <polylib/polylibgmp.h>
13 #include "ev_operations.h"
27 using std::ostringstream
;
29 #define ALLOC(p) (((long *) (p))[0])
30 #define SIZE(p) (((long *) (p))[1])
31 #define DATA(p) ((mp_limb_t *) (((long *) (p)) + 2))
33 static void value2zz(Value v
, ZZ
& z
)
35 int sa
= v
[0]._mp_size
;
36 int abs_sa
= sa
< 0 ? -sa
: sa
;
38 _ntl_gsetlength(&z
.rep
, abs_sa
);
39 mp_limb_t
* adata
= DATA(z
.rep
);
40 for (int i
= 0; i
< abs_sa
; ++i
)
41 adata
[i
] = v
[0]._mp_d
[i
];
45 static void zz2value(ZZ
& z
, Value
& v
)
53 int abs_sa
= sa
< 0 ? -sa
: sa
;
55 mp_limb_t
* adata
= DATA(z
.rep
);
56 _mpz_realloc(v
, abs_sa
);
57 for (int i
= 0; i
< abs_sa
; ++i
)
58 v
[0]._mp_d
[i
] = adata
[i
];
63 #define ALLOC(t,p) p = (t*)malloc(sizeof(*p))
66 * We just ignore the last column and row
67 * If the final element is not equal to one
68 * then the result will actually be a multiple of the input
70 static void matrix2zz(Matrix
*M
, mat_ZZ
& m
, unsigned nr
, unsigned nc
)
74 for (int i
= 0; i
< nr
; ++i
) {
75 // assert(value_one_p(M->p[i][M->NbColumns - 1]));
76 for (int j
= 0; j
< nc
; ++j
) {
77 value2zz(M
->p
[i
][j
], m
[i
][j
]);
82 static void values2zz(Value
*p
, vec_ZZ
& v
, int len
)
86 for (int i
= 0; i
< len
; ++i
) {
93 static void zz2values(vec_ZZ
& v
, Value
*p
)
95 for (int i
= 0; i
< v
.length(); ++i
)
99 static void rays(mat_ZZ
& r
, Polyhedron
*C
)
101 unsigned dim
= C
->NbRays
- 1; /* don't count zero vertex */
102 assert(C
->NbRays
- 1 == C
->Dimension
);
107 for (i
= 0, c
= 0; i
< dim
; ++i
)
108 if (value_zero_p(C
->Ray
[i
][dim
+1])) {
109 for (int j
= 0; j
< dim
; ++j
) {
110 value2zz(C
->Ray
[i
][j
+1], tmp
);
117 static Matrix
* rays(Polyhedron
*C
)
119 unsigned dim
= C
->NbRays
- 1; /* don't count zero vertex */
120 assert(C
->NbRays
- 1 == C
->Dimension
);
122 Matrix
*M
= Matrix_Alloc(dim
+1, dim
+1);
126 for (i
= 0, c
= 0; i
<= dim
&& c
< dim
; ++i
)
127 if (value_zero_p(C
->Ray
[i
][dim
+1])) {
128 Vector_Copy(C
->Ray
[i
] + 1, M
->p
[c
], dim
);
129 value_set_si(M
->p
[c
++][dim
], 0);
132 value_set_si(M
->p
[dim
][dim
], 1);
137 static Matrix
* rays2(Polyhedron
*C
)
139 unsigned dim
= C
->NbRays
- 1; /* don't count zero vertex */
140 assert(C
->NbRays
- 1 == C
->Dimension
);
142 Matrix
*M
= Matrix_Alloc(dim
, dim
);
146 for (i
= 0, c
= 0; i
<= dim
&& c
< dim
; ++i
)
147 if (value_zero_p(C
->Ray
[i
][dim
+1]))
148 Vector_Copy(C
->Ray
[i
] + 1, M
->p
[c
++], dim
);
155 * Returns the largest absolute value in the vector
157 static ZZ
max(vec_ZZ
& v
)
160 for (int i
= 1; i
< v
.length(); ++i
)
170 Rays
= Matrix_Copy(M
);
173 cone(Polyhedron
*C
) {
174 Cone
= Polyhedron_Copy(C
);
180 matrix2zz(Rays
, A
, Rays
->NbRows
- 1, Rays
->NbColumns
- 1);
181 det
= determinant(A
);
188 Vector
* short_vector(vec_ZZ
& lambda
) {
189 Matrix
*M
= Matrix_Copy(Rays
);
190 Matrix
*inv
= Matrix_Alloc(M
->NbRows
, M
->NbColumns
);
191 int ok
= Matrix_Inverse(M
, inv
);
198 matrix2zz(inv
, B
, inv
->NbRows
- 1, inv
->NbColumns
- 1);
199 long r
= LLL(det2
, B
, U
);
203 for (int i
= 1; i
< B
.NumRows(); ++i
) {
215 Vector
*z
= Vector_Alloc(U
[index
].length()+1);
217 zz2values(U
[index
], z
->p
);
218 value_set_si(z
->p
[U
[index
].length()], 0);
222 Polyhedron
*C
= poly();
224 for (i
= 0; i
< C
->NbConstraints
; ++i
) {
225 Inner_Product(z
->p
, C
->Constraint
[i
]+1, z
->Size
-1, &tmp
);
226 if (value_pos_p(tmp
))
229 if (i
== C
->NbConstraints
) {
230 value_set_si(tmp
, -1);
231 Vector_Scale(z
->p
, z
->p
, tmp
, z
->Size
-1);
238 Polyhedron_Free(Cone
);
244 Matrix
*M
= Matrix_Alloc(Rays
->NbRows
+1, Rays
->NbColumns
+1);
245 for (int i
= 0; i
< Rays
->NbRows
; ++i
) {
246 Vector_Copy(Rays
->p
[i
], M
->p
[i
]+1, Rays
->NbColumns
);
247 value_set_si(M
->p
[i
][0], 1);
249 Vector_Set(M
->p
[Rays
->NbRows
]+1, 0, Rays
->NbColumns
-1);
250 value_set_si(M
->p
[Rays
->NbRows
][0], 1);
251 value_set_si(M
->p
[Rays
->NbRows
][Rays
->NbColumns
], 1);
252 Cone
= Rays2Polyhedron(M
, M
->NbRows
+1);
253 assert(Cone
->NbConstraints
== Cone
->NbRays
);
267 dpoly(int d
, ZZ
& degree
, int offset
= 0) {
268 coeff
.SetLength(d
+1);
270 int min
= d
+ offset
;
271 if (degree
>= 0 && degree
< ZZ(INIT_VAL
, min
))
272 min
= to_int(degree
);
274 ZZ c
= ZZ(INIT_VAL
, 1);
277 for (int i
= 1; i
<= min
; ++i
) {
278 c
*= (degree
-i
+ 1);
283 void operator *= (dpoly
& f
) {
284 assert(coeff
.length() == f
.coeff
.length());
286 coeff
= f
.coeff
[0] * coeff
;
287 for (int i
= 1; i
< coeff
.length(); ++i
)
288 for (int j
= 0; i
+j
< coeff
.length(); ++j
)
289 coeff
[i
+j
] += f
.coeff
[i
] * old
[j
];
291 void div(dpoly
& d
, mpq_t count
, ZZ
& sign
) {
292 int len
= coeff
.length();
295 mpq_t
* c
= new mpq_t
[coeff
.length()];
298 for (int i
= 0; i
< len
; ++i
) {
300 zz2value(coeff
[i
], tmp
);
301 mpq_set_z(c
[i
], tmp
);
303 for (int j
= 1; j
<= i
; ++j
) {
304 zz2value(d
.coeff
[j
], tmp
);
305 mpq_set_z(qtmp
, tmp
);
306 mpq_mul(qtmp
, qtmp
, c
[i
-j
]);
307 mpq_sub(c
[i
], c
[i
], qtmp
);
310 zz2value(d
.coeff
[0], tmp
);
311 mpq_set_z(qtmp
, tmp
);
312 mpq_div(c
[i
], c
[i
], qtmp
);
315 mpq_sub(count
, count
, c
[len
-1]);
317 mpq_add(count
, count
, c
[len
-1]);
321 for (int i
= 0; i
< len
; ++i
)
333 dpoly_n(int d
, ZZ
& degree_0
, ZZ
& degree_1
, int offset
= 0) {
337 zz2value(degree_0
, d0
);
338 zz2value(degree_1
, d1
);
339 coeff
= Matrix_Alloc(d
+1, d
+1+1);
340 value_set_si(coeff
->p
[0][0], 1);
341 value_set_si(coeff
->p
[0][d
+1], 1);
342 for (int i
= 1; i
<= d
; ++i
) {
343 value_multiply(coeff
->p
[i
][0], coeff
->p
[i
-1][0], d0
);
344 Vector_Combine(coeff
->p
[i
-1], coeff
->p
[i
-1]+1, coeff
->p
[i
]+1,
346 value_set_si(coeff
->p
[i
][d
+1], i
);
347 value_multiply(coeff
->p
[i
][d
+1], coeff
->p
[i
][d
+1], coeff
->p
[i
-1][d
+1]);
348 value_decrement(d0
, d0
);
353 void div(dpoly
& d
, Vector
*count
, ZZ
& sign
) {
354 int len
= coeff
->NbRows
;
355 Matrix
* c
= Matrix_Alloc(coeff
->NbRows
, coeff
->NbColumns
);
358 for (int i
= 0; i
< len
; ++i
) {
359 Vector_Copy(coeff
->p
[i
], c
->p
[i
], len
+1);
360 for (int j
= 1; j
<= i
; ++j
) {
361 zz2value(d
.coeff
[j
], tmp
);
362 value_multiply(tmp
, tmp
, c
->p
[i
][len
]);
363 value_oppose(tmp
, tmp
);
364 Vector_Combine(c
->p
[i
], c
->p
[i
-j
], c
->p
[i
],
365 c
->p
[i
-j
][len
], tmp
, len
);
366 value_multiply(c
->p
[i
][len
], c
->p
[i
][len
], c
->p
[i
-j
][len
]);
368 zz2value(d
.coeff
[0], tmp
);
369 value_multiply(c
->p
[i
][len
], c
->p
[i
][len
], tmp
);
372 value_set_si(tmp
, -1);
373 Vector_Scale(c
->p
[len
-1], count
->p
, tmp
, len
);
374 value_assign(count
->p
[len
], c
->p
[len
-1][len
]);
376 Vector_Copy(c
->p
[len
-1], count
->p
, len
+1);
377 Vector_Normalize(count
->p
, len
+1);
384 * Barvinok's Decomposition of a simplicial cone
386 * Returns two lists of polyhedra
388 void barvinok_decompose(Polyhedron
*C
, Polyhedron
**ppos
, Polyhedron
**pneg
)
390 Polyhedron
*pos
= *ppos
, *neg
= *pneg
;
391 vector
<cone
*> nonuni
;
392 cone
* c
= new cone(C
);
399 Polyhedron
*p
= Polyhedron_Copy(c
->Cone
);
405 while (!nonuni
.empty()) {
408 Vector
* v
= c
->short_vector(lambda
);
409 for (int i
= 0; i
< c
->Rays
->NbRows
- 1; ++i
) {
412 Matrix
* M
= Matrix_Copy(c
->Rays
);
413 Vector_Copy(v
->p
, M
->p
[i
], v
->Size
);
414 cone
* pc
= new cone(M
);
415 assert (pc
->det
!= 0);
416 if (abs(pc
->det
) > 1) {
417 assert(abs(pc
->det
) < abs(c
->det
));
418 nonuni
.push_back(pc
);
420 Polyhedron
*p
= pc
->poly();
422 if (sign(pc
->det
) == s
) {
441 * Returns a single list of npos "positive" cones followed by nneg
443 * The input cone is freed
445 void decompose(Polyhedron
*cone
, Polyhedron
**parts
, int *npos
, int *nneg
, unsigned MaxRays
)
447 Polyhedron_Polarize(cone
);
448 if (cone
->NbRays
- 1 != cone
->Dimension
) {
449 Polyhedron
*tmp
= cone
;
450 cone
= triangularize_cone(cone
, MaxRays
);
451 Polyhedron_Free(tmp
);
453 Polyhedron
*polpos
= NULL
, *polneg
= NULL
;
454 *npos
= 0; *nneg
= 0;
455 for (Polyhedron
*Polar
= cone
; Polar
; Polar
= Polar
->next
)
456 barvinok_decompose(Polar
, &polpos
, &polneg
);
459 for (Polyhedron
*i
= polpos
; i
; i
= i
->next
) {
460 Polyhedron_Polarize(i
);
464 for (Polyhedron
*i
= polneg
; i
; i
= i
->next
) {
465 Polyhedron_Polarize(i
);
476 const int MAX_TRY
=10;
478 * Searches for a vector that is not othogonal to any
479 * of the rays in rays.
481 static void nonorthog(mat_ZZ
& rays
, vec_ZZ
& lambda
)
483 int dim
= rays
.NumCols();
485 lambda
.SetLength(dim
);
486 for (int i
= 2; !found
&& i
<= 50*dim
; i
+=4) {
487 for (int j
= 0; j
< MAX_TRY
; ++j
) {
488 for (int k
= 0; k
< dim
; ++k
) {
489 int r
= random_int(i
)+2;
490 int v
= (2*(r
%2)-1) * (r
>> 1);
494 for (; k
< rays
.NumRows(); ++k
)
495 if (lambda
* rays
[k
] == 0)
497 if (k
== rays
.NumRows()) {
506 static void add_rays(mat_ZZ
& rays
, Polyhedron
*i
, int *r
)
508 unsigned dim
= i
->Dimension
;
509 for (int k
= 0; k
< i
->NbRays
; ++k
) {
510 if (!value_zero_p(i
->Ray
[k
][dim
+1]))
512 values2zz(i
->Ray
[k
]+1, rays
[(*r
)++], dim
);
516 void lattice_point(Value
* values
, Polyhedron
*i
, vec_ZZ
& vertex
)
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
);
542 static evalue
*term(int param
, ZZ
& c
, Value
*den
= NULL
)
544 evalue
*EP
= new evalue();
546 value_set_si(EP
->d
,0);
547 EP
->x
.p
= new_enode(polynomial
, 2, param
+ 1);
548 evalue_set_si(&EP
->x
.p
->arr
[0], 0, 1);
549 value_init(EP
->x
.p
->arr
[1].x
.n
);
551 value_set_si(EP
->x
.p
->arr
[1].d
, 1);
553 value_assign(EP
->x
.p
->arr
[1].d
, *den
);
554 zz2value(c
, EP
->x
.p
->arr
[1].x
.n
);
558 static void vertex_period(
559 Polyhedron
*i
, vec_ZZ
& lambda
, Matrix
*T
,
560 Value lcm
, int p
, Vector
*val
,
561 evalue
*E
, evalue
* ev
,
564 unsigned nparam
= T
->NbRows
- 1;
565 unsigned dim
= i
->Dimension
;
572 Vector
* values
= Vector_Alloc(dim
+ 1);
573 Vector_Matrix_Product(val
->p
, T
, values
->p
);
574 value_assign(values
->p
[dim
], lcm
);
575 lattice_point(values
->p
, i
, vertex
);
576 num
= vertex
* lambda
;
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 int bounded
= line_minmax(I
, &min
, &max
);
710 mpz_cdiv_q(min
, min
, d
);
712 mpz_fdiv_q(min
, min
, d
);
713 mpz_fdiv_q(max
, max
, d
);
715 if (value_eq(min
, max
)) {
718 value_assign(*ret
, min
);
726 * Normalize linear expression coef modulo m
727 * Removes common factor and reduces coefficients
728 * Returns index of first non-zero coefficient or len
730 static int normal_mod(Value
*coef
, int len
, Value
*m
)
735 Vector_Gcd(coef
, len
, &gcd
);
737 Vector_AntiScale(coef
, coef
, gcd
, len
);
739 value_division(*m
, *m
, gcd
);
746 for (j
= 0; j
< len
; ++j
)
747 mpz_fdiv_r(coef
[j
], coef
[j
], *m
);
748 for (j
= 0; j
< len
; ++j
)
749 if (value_notzero_p(coef
[j
]))
756 static void mask(Matrix
*f
, evalue
*factor
)
758 int nr
= f
->NbRows
, nc
= f
->NbColumns
;
761 for (n
= 0; n
< nr
&& value_notzero_p(f
->p
[n
][nc
-1]); ++n
)
762 if (value_notone_p(f
->p
[n
][nc
-1]) &&
763 value_notmone_p(f
->p
[n
][nc
-1]))
777 value_set_si(EV
.x
.n
, 1);
779 for (n
= 0; n
< nr
; ++n
) {
780 value_assign(m
, f
->p
[n
][nc
-1]);
781 if (value_one_p(m
) || value_mone_p(m
))
784 int j
= normal_mod(f
->p
[n
], nc
-1, &m
);
786 free_evalue_refs(factor
);
787 value_init(factor
->d
);
788 evalue_set_si(factor
, 0, 1);
792 values2zz(f
->p
[n
], row
, nc
-1);
795 if (j
< (nc
-1)-1 && row
[j
] > g
/2) {
796 for (int k
= j
; k
< (nc
-1); ++k
)
802 value_set_si(EP
.d
, 0);
803 EP
.x
.p
= new_enode(relation
, 2, 0);
804 value_clear(EP
.x
.p
->arr
[1].d
);
805 EP
.x
.p
->arr
[1] = *factor
;
806 evalue
*ev
= &EP
.x
.p
->arr
[0];
807 value_set_si(ev
->d
, 0);
808 ev
->x
.p
= new_enode(fractional
, 3, -1);
809 evalue_set_si(&ev
->x
.p
->arr
[1], 0, 1);
810 evalue_set_si(&ev
->x
.p
->arr
[2], 1, 1);
811 evalue
*E
= multi_monom(row
);
812 value_assign(EV
.d
, m
);
814 value_clear(ev
->x
.p
->arr
[0].d
);
815 ev
->x
.p
->arr
[0] = *E
;
821 free_evalue_refs(&EV
);
827 static void mask(Matrix
*f
, evalue
*factor
)
829 int nr
= f
->NbRows
, nc
= f
->NbColumns
;
832 for (n
= 0; n
< nr
&& value_notzero_p(f
->p
[n
][nc
-1]); ++n
)
833 if (value_notone_p(f
->p
[n
][nc
-1]) &&
834 value_notmone_p(f
->p
[n
][nc
-1]))
842 unsigned np
= nc
- 2;
843 Vector
*lcm
= Vector_Alloc(np
);
844 Vector
*val
= Vector_Alloc(nc
);
845 Vector_Set(val
->p
, 0, nc
);
846 value_set_si(val
->p
[np
], 1);
847 Vector_Set(lcm
->p
, 1, np
);
848 for (n
= 0; n
< nr
; ++n
) {
849 if (value_one_p(f
->p
[n
][nc
-1]) ||
850 value_mone_p(f
->p
[n
][nc
-1]))
852 for (int j
= 0; j
< np
; ++j
)
853 if (value_notzero_p(f
->p
[n
][j
])) {
854 Gcd(f
->p
[n
][j
], f
->p
[n
][nc
-1], &tmp
);
855 value_division(tmp
, f
->p
[n
][nc
-1], tmp
);
856 value_lcm(tmp
, lcm
->p
[j
], &lcm
->p
[j
]);
861 mask_r(f
, nr
, lcm
, 0, val
, &EP
);
866 free_evalue_refs(&EP
);
877 static bool mod_needed(Polyhedron
*PD
, vec_ZZ
& num
, Value d
, evalue
*E
)
879 Value
*q
= fixed_quotient(PD
, num
, d
, false);
884 value_oppose(*q
, *q
);
887 value_set_si(EV
.d
, 1);
889 value_multiply(EV
.x
.n
, *q
, d
);
891 free_evalue_refs(&EV
);
897 static void ceil_mod(Value
*coef
, int len
, Value d
, ZZ
& f
, evalue
*EP
, Polyhedron
*PD
)
903 Vector_Scale(coef
, coef
, m
, len
);
906 int j
= normal_mod(coef
, len
, &m
);
914 values2zz(coef
, num
, len
);
921 evalue_set_si(&tmp
, 0, 1);
925 while (j
< len
-1 && (num
[j
] == g
/2 || num
[j
] == 0))
927 if ((j
< len
-1 && num
[j
] > g
/2) || (j
== len
-1 && num
[j
] >= (g
+1)/2)) {
928 for (int k
= j
; k
< len
-1; ++k
)
931 num
[len
-1] = g
- 1 - num
[len
-1];
932 value_assign(tmp
.d
, m
);
934 zz2value(t
, tmp
.x
.n
);
940 ZZ t
= num
[len
-1] * f
;
941 zz2value(t
, tmp
.x
.n
);
942 value_assign(tmp
.d
, m
);
945 evalue
*E
= multi_monom(num
);
949 if (PD
&& !mod_needed(PD
, num
, m
, E
)) {
952 value_assign(EV
.d
, m
);
957 value_set_si(EV
.x
.n
, 1);
958 value_assign(EV
.d
, m
);
961 value_set_si(EV
.d
, 0);
962 EV
.x
.p
= new_enode(fractional
, 3, -1);
963 evalue_copy(&EV
.x
.p
->arr
[0], E
);
964 evalue_set_si(&EV
.x
.p
->arr
[1], 0, 1);
965 value_init(EV
.x
.p
->arr
[2].x
.n
);
966 zz2value(f
, EV
.x
.p
->arr
[2].x
.n
);
967 value_set_si(EV
.x
.p
->arr
[2].d
, 1);
972 free_evalue_refs(&EV
);
977 free_evalue_refs(&tmp
);
983 evalue
* bv_ceil3(Value
*coef
, int len
, Value d
, Polyhedron
*P
)
985 Vector
*val
= Vector_Alloc(len
);
990 Vector_Scale(coef
, val
->p
, t
, len
);
991 value_absolute(t
, d
);
994 values2zz(val
->p
, num
, len
);
995 evalue
*EP
= multi_monom(num
);
1000 value_set_si(tmp
.x
.n
, 1);
1001 value_assign(tmp
.d
, t
);
1007 ceil_mod(val
->p
, len
, t
, one
, EP
, P
);
1010 /* copy EP to malloc'ed evalue */
1016 free_evalue_refs(&tmp
);
1023 evalue
* lattice_point(
1024 Polyhedron
*i
, vec_ZZ
& lambda
, Matrix
*W
, Value lcm
, Polyhedron
*PD
)
1026 unsigned nparam
= W
->NbColumns
- 1;
1028 Matrix
* Rays
= rays2(i
);
1029 Matrix
*T
= Transpose(Rays
);
1030 Matrix
*T2
= Matrix_Copy(T
);
1031 Matrix
*inv
= Matrix_Alloc(T2
->NbRows
, T2
->NbColumns
);
1032 int ok
= Matrix_Inverse(T2
, inv
);
1037 matrix2zz(W
, vertex
, W
->NbRows
, W
->NbColumns
);
1040 num
= lambda
* vertex
;
1042 evalue
*EP
= multi_monom(num
);
1046 value_init(tmp
.x
.n
);
1047 value_set_si(tmp
.x
.n
, 1);
1048 value_assign(tmp
.d
, lcm
);
1052 Matrix
*L
= Matrix_Alloc(inv
->NbRows
, W
->NbColumns
);
1053 Matrix_Product(inv
, W
, L
);
1056 matrix2zz(T
, RT
, T
->NbRows
, T
->NbColumns
);
1059 vec_ZZ p
= lambda
* RT
;
1061 for (int i
= 0; i
< L
->NbRows
; ++i
) {
1062 ceil_mod(L
->p
[i
], nparam
+1, lcm
, p
[i
], EP
, PD
);
1068 free_evalue_refs(&tmp
);
1072 evalue
* lattice_point(
1073 Polyhedron
*i
, vec_ZZ
& lambda
, Matrix
*W
, Value lcm
, Polyhedron
*PD
)
1075 Matrix
*T
= Transpose(W
);
1076 unsigned nparam
= T
->NbRows
- 1;
1078 evalue
*EP
= new evalue();
1080 evalue_set_si(EP
, 0, 1);
1083 Vector
*val
= Vector_Alloc(nparam
+1);
1084 value_set_si(val
->p
[nparam
], 1);
1085 ZZ
offset(INIT_VAL
, 0);
1087 vertex_period(i
, lambda
, T
, lcm
, 0, val
, EP
, &ev
, offset
);
1090 free_evalue_refs(&ev
);
1101 Param_Vertices
* V
, Polyhedron
*i
, vec_ZZ
& lambda
, term_info
* term
,
1104 unsigned nparam
= V
->Vertex
->NbColumns
- 2;
1105 unsigned dim
= i
->Dimension
;
1107 vertex
.SetDims(V
->Vertex
->NbRows
, nparam
+1);
1111 value_set_si(lcm
, 1);
1112 for (int j
= 0; j
< V
->Vertex
->NbRows
; ++j
) {
1113 value_lcm(lcm
, V
->Vertex
->p
[j
][nparam
+1], &lcm
);
1115 if (value_notone_p(lcm
)) {
1116 Matrix
* mv
= Matrix_Alloc(dim
, nparam
+1);
1117 for (int j
= 0 ; j
< dim
; ++j
) {
1118 value_division(tmp
, lcm
, V
->Vertex
->p
[j
][nparam
+1]);
1119 Vector_Scale(V
->Vertex
->p
[j
], mv
->p
[j
], tmp
, nparam
+1);
1122 term
->E
= lattice_point(i
, lambda
, mv
, lcm
, PD
);
1130 for (int i
= 0; i
< V
->Vertex
->NbRows
; ++i
) {
1131 assert(value_one_p(V
->Vertex
->p
[i
][nparam
+1])); // for now
1132 values2zz(V
->Vertex
->p
[i
], vertex
[i
], nparam
+1);
1136 num
= lambda
* vertex
;
1140 for (int j
= 0; j
< nparam
; ++j
)
1146 term
->E
= multi_monom(num
);
1150 term
->constant
= num
[nparam
];
1153 term
->coeff
= num
[p
];
1160 void normalize(Polyhedron
*i
, vec_ZZ
& lambda
, ZZ
& sign
, ZZ
& num
, vec_ZZ
& den
)
1162 unsigned dim
= i
->Dimension
;
1166 rays
.SetDims(dim
, dim
);
1167 add_rays(rays
, i
, &r
);
1168 den
= rays
* lambda
;
1171 for (int j
= 0; j
< den
.length(); ++j
) {
1175 den
[j
] = abs(den
[j
]);
1183 typedef Polyhedron
* Polyhedron_p
;
1185 void barvinok_count(Polyhedron
*P
, Value
* result
, unsigned NbMaxCons
)
1187 Polyhedron
** vcone
;
1190 sign
.SetLength(ncone
);
1198 value_set_si(*result
, 0);
1202 for (; r
< P
->NbRays
; ++r
)
1203 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1]))
1205 if (P
->NbBid
!=0 || r
< P
->NbRays
) {
1206 value_set_si(*result
, -1);
1210 P
= remove_equalities(P
);
1213 value_set_si(*result
, 0);
1219 value_set_si(factor
, 1);
1220 Q
= Polyhedron_Reduce(P
, &factor
);
1227 if (P
->Dimension
== 0) {
1228 value_assign(*result
, factor
);
1231 value_clear(factor
);
1236 vcone
= new Polyhedron_p
[P
->NbRays
];
1238 for (int j
= 0; j
< P
->NbRays
; ++j
) {
1240 Polyhedron
*C
= supporting_cone(P
, j
);
1241 decompose(C
, &vcone
[j
], &npos
, &nneg
, NbMaxCons
);
1242 ncone
+= npos
+ nneg
;
1243 sign
.SetLength(ncone
);
1244 for (int k
= 0; k
< npos
; ++k
)
1245 sign
[ncone
-nneg
-k
-1] = 1;
1246 for (int k
= 0; k
< nneg
; ++k
)
1247 sign
[ncone
-k
-1] = -1;
1251 rays
.SetDims(ncone
* dim
, dim
);
1253 for (int j
= 0; j
< P
->NbRays
; ++j
) {
1254 for (Polyhedron
*i
= vcone
[j
]; i
; i
= i
->next
) {
1255 assert(i
->NbRays
-1 == dim
);
1256 add_rays(rays
, i
, &r
);
1260 nonorthog(rays
, lambda
);
1270 for (int j
= 0; j
< P
->NbRays
; ++j
) {
1271 for (Polyhedron
*i
= vcone
[j
]; i
; i
= i
->next
) {
1272 lattice_point(P
->Ray
[j
]+1, i
, vertex
);
1273 num
= vertex
* lambda
;
1274 normalize(i
, lambda
, sign
[f
], num
, den
);
1277 dpoly
n(dim
, den
[0], 1);
1278 for (int k
= 1; k
< dim
; ++k
) {
1279 dpoly
fact(dim
, den
[k
], 1);
1282 d
.div(n
, count
, sign
[f
]);
1286 Domain_Free(vcone
[j
]);
1289 assert(value_one_p(&count
[0]._mp_den
));
1290 value_multiply(*result
, &count
[0]._mp_num
, factor
);
1297 value_clear(factor
);
1300 static void uni_polynom(int param
, Vector
*c
, evalue
*EP
)
1302 unsigned dim
= c
->Size
-2;
1304 value_set_si(EP
->d
,0);
1305 EP
->x
.p
= new_enode(polynomial
, dim
+1, param
+1);
1306 for (int j
= 0; j
<= dim
; ++j
)
1307 evalue_set(&EP
->x
.p
->arr
[j
], c
->p
[j
], c
->p
[dim
+1]);
1310 static void multi_polynom(Vector
*c
, evalue
* X
, evalue
*EP
)
1312 unsigned dim
= c
->Size
-2;
1316 evalue_set(&EC
, c
->p
[dim
], c
->p
[dim
+1]);
1319 evalue_set(EP
, c
->p
[dim
], c
->p
[dim
+1]);
1321 for (int i
= dim
-1; i
>= 0; --i
) {
1323 value_assign(EC
.x
.n
, c
->p
[i
]);
1326 free_evalue_refs(&EC
);
1329 Polyhedron
*unfringe (Polyhedron
*P
, unsigned MaxRays
)
1331 int len
= P
->Dimension
+2;
1332 Polyhedron
*T
, *R
= P
;
1335 Vector
*row
= Vector_Alloc(len
);
1336 value_set_si(row
->p
[0], 1);
1338 R
= DomainConstraintSimplify(Polyhedron_Copy(P
), MaxRays
);
1340 Matrix
*M
= Matrix_Alloc(2, len
-1);
1341 value_set_si(M
->p
[1][len
-2], 1);
1342 for (int v
= 0; v
< P
->Dimension
; ++v
) {
1343 value_set_si(M
->p
[0][v
], 1);
1344 Polyhedron
*I
= Polyhedron_Image(P
, M
, 2+1);
1345 value_set_si(M
->p
[0][v
], 0);
1346 for (int r
= 0; r
< I
->NbConstraints
; ++r
) {
1347 if (value_zero_p(I
->Constraint
[r
][0]))
1349 if (value_zero_p(I
->Constraint
[r
][1]))
1351 if (value_one_p(I
->Constraint
[r
][1]))
1353 if (value_mone_p(I
->Constraint
[r
][1]))
1355 value_absolute(g
, I
->Constraint
[r
][1]);
1356 Vector_Set(row
->p
+1, 0, len
-2);
1357 value_division(row
->p
[1+v
], I
->Constraint
[r
][1], g
);
1358 mpz_fdiv_q(row
->p
[len
-1], I
->Constraint
[r
][2], g
);
1360 R
= AddConstraints(row
->p
, 1, R
, MaxRays
);
1372 static Polyhedron
*reduce_domain(Polyhedron
*D
, Matrix
*CT
, Polyhedron
*CEq
,
1373 Polyhedron
**fVD
, int nd
, unsigned MaxRays
)
1378 Dt
= CT
? DomainPreimage(D
, CT
, MaxRays
) : D
;
1379 Polyhedron
*rVD
= DomainIntersection(Dt
, CEq
, MaxRays
);
1381 /* if rVD is empty or too small in geometric dimension */
1382 if(!rVD
|| emptyQ(rVD
) ||
1383 (rVD
->Dimension
-rVD
->NbEq
< Dt
->Dimension
-Dt
->NbEq
-CEq
->NbEq
)) {
1388 return 0; /* empty validity domain */
1394 fVD
[nd
] = Domain_Copy(rVD
);
1395 for (int i
= 0 ; i
< nd
; ++i
) {
1396 Polyhedron
*I
= DomainIntersection(fVD
[nd
], fVD
[i
], MaxRays
);
1401 Polyhedron
*F
= DomainSimplify(I
, fVD
[nd
], MaxRays
);
1403 Polyhedron
*T
= rVD
;
1404 rVD
= DomainDifference(rVD
, F
, MaxRays
);
1411 rVD
= DomainConstraintSimplify(rVD
, MaxRays
);
1413 Domain_Free(fVD
[nd
]);
1420 barvinok_count(rVD
, &c
, MaxRays
);
1421 if (value_zero_p(c
)) {
1430 evalue
* barvinok_enumerate_ev(Polyhedron
*P
, Polyhedron
* C
, unsigned MaxRays
)
1432 //P = unfringe(P, MaxRays);
1433 Polyhedron
*CEq
= NULL
, *rVD
, *pVD
, *CA
;
1435 Param_Polyhedron
*PP
= NULL
;
1436 Param_Domain
*D
, *next
;
1439 unsigned nparam
= C
->Dimension
;
1441 ALLOC(evalue
, eres
);
1442 value_init(eres
->d
);
1443 value_set_si(eres
->d
, 0);
1446 value_init(factor
.d
);
1447 evalue_set_si(&factor
, 1, 1);
1449 CA
= align_context(C
, P
->Dimension
, MaxRays
);
1450 P
= DomainIntersection(P
, CA
, MaxRays
);
1451 Polyhedron_Free(CA
);
1453 if (C
->Dimension
== 0 || emptyQ(P
)) {
1455 eres
->x
.p
= new_enode(partition
, 2, C
->Dimension
);
1456 EVALUE_SET_DOMAIN(eres
->x
.p
->arr
[0],
1457 DomainConstraintSimplify(CEq
? CEq
: Polyhedron_Copy(C
), MaxRays
));
1458 value_set_si(eres
->x
.p
->arr
[1].d
, 1);
1459 value_init(eres
->x
.p
->arr
[1].x
.n
);
1461 value_set_si(eres
->x
.p
->arr
[1].x
.n
, 0);
1463 barvinok_count(P
, &eres
->x
.p
->arr
[1].x
.n
, MaxRays
);
1465 emul(&factor
, eres
);
1466 reduce_evalue(eres
);
1467 free_evalue_refs(&factor
);
1472 Param_Polyhedron_Free(PP
);
1476 for (r
= 0; r
< P
->NbRays
; ++r
)
1477 if (value_zero_p(P
->Ray
[r
][0]) ||
1478 value_zero_p(P
->Ray
[r
][P
->Dimension
+1])) {
1480 for (i
= P
->Dimension
- nparam
; i
< P
->Dimension
; ++i
)
1481 if (value_notzero_p(P
->Ray
[r
][i
+1]))
1483 if (i
>= P
->Dimension
)
1491 P
= remove_equalities_p(P
, P
->Dimension
-nparam
, &f
);
1495 if (P
->Dimension
== nparam
) {
1497 P
= Universe_Polyhedron(0);
1501 Polyhedron
*Q
= ParamPolyhedron_Reduce(P
, P
->Dimension
-nparam
, &factor
);
1504 if (Q
->Dimension
== nparam
) {
1506 P
= Universe_Polyhedron(0);
1511 Polyhedron
*oldP
= P
;
1512 PP
= Polyhedron2Param_SimplifiedDomain(&P
,C
,MaxRays
,&CEq
,&CT
);
1514 Polyhedron_Free(oldP
);
1516 if (isIdentity(CT
)) {
1520 assert(CT
->NbRows
!= CT
->NbColumns
);
1521 if (CT
->NbRows
== 1) // no more parameters
1523 nparam
= CT
->NbRows
- 1;
1526 unsigned dim
= P
->Dimension
- nparam
;
1527 Polyhedron
** vcone
= new Polyhedron_p
[PP
->nbV
];
1528 int * npos
= new int[PP
->nbV
];
1529 int * nneg
= new int[PP
->nbV
];
1533 for (i
= 0, V
= PP
->V
; V
; ++i
, V
= V
->next
) {
1534 Polyhedron
*C
= supporting_cone_p(P
, V
);
1535 decompose(C
, &vcone
[i
], &npos
[i
], &nneg
[i
], MaxRays
);
1538 Vector
*c
= Vector_Alloc(dim
+2);
1541 for (nd
= 0, D
=PP
->D
; D
; ++nd
, D
=D
->next
);
1542 struct section
{ Polyhedron
*D
; evalue E
; };
1543 section
*s
= new section
[nd
];
1544 Polyhedron
**fVD
= new Polyhedron_p
[nd
];
1546 for(nd
= 0, D
=PP
->D
; D
; D
=next
) {
1549 Polyhedron
*rVD
= reduce_domain(D
->Domain
, CT
, CEq
,
1554 pVD
= CT
? DomainImage(rVD
,CT
,MaxRays
) : rVD
;
1557 FORALL_PVertex_in_ParamPolyhedron(V
,D
,PP
) // _i is internal counter
1558 ncone
+= npos
[_i
] + nneg
[_i
];
1559 END_FORALL_PVertex_in_ParamPolyhedron
;
1562 rays
.SetDims(ncone
* dim
, dim
);
1564 FORALL_PVertex_in_ParamPolyhedron(V
,D
,PP
) // _i is internal counter
1565 for (Polyhedron
*i
= vcone
[_i
]; i
; i
= i
->next
) {
1566 assert(i
->NbRays
-1 == dim
);
1567 add_rays(rays
, i
, &r
);
1569 END_FORALL_PVertex_in_ParamPolyhedron
;
1571 nonorthog(rays
, lambda
);
1577 value_init(s
[nd
].E
.d
);
1578 evalue_set_si(&s
[nd
].E
, 0, 1);
1581 FORALL_PVertex_in_ParamPolyhedron(V
,D
,PP
)
1583 for (Polyhedron
*i
= vcone
[_i
]; i
; i
= i
->next
) {
1584 sign
= f
< npos
[_i
] ? 1 : -1;
1585 lattice_point(V
, i
, lambda
, &num
, pVD
);
1586 normalize(i
, lambda
, sign
, num
.constant
, den
);
1588 dpoly
n(dim
, den
[0], 1);
1589 for (int k
= 1; k
< dim
; ++k
) {
1590 dpoly
fact(dim
, den
[k
], 1);
1593 if (num
.E
!= NULL
) {
1594 ZZ
one(INIT_VAL
, 1);
1595 dpoly_n
d(dim
, num
.constant
, one
);
1598 multi_polynom(c
, num
.E
, &EV
);
1599 eadd(&EV
, &s
[nd
].E
);
1600 free_evalue_refs(&EV
);
1601 free_evalue_refs(num
.E
);
1603 } else if (num
.pos
!= -1) {
1604 dpoly_n
d(dim
, num
.constant
, num
.coeff
);
1607 uni_polynom(num
.pos
, c
, &EV
);
1608 eadd(&EV
, &s
[nd
].E
);
1609 free_evalue_refs(&EV
);
1611 mpq_set_si(count
, 0, 1);
1612 dpoly
d(dim
, num
.constant
);
1613 d
.div(n
, count
, sign
);
1616 evalue_set(&EV
, &count
[0]._mp_num
, &count
[0]._mp_den
);
1617 eadd(&EV
, &s
[nd
].E
);
1618 free_evalue_refs(&EV
);
1622 END_FORALL_PVertex_in_ParamPolyhedron
;
1627 addeliminatedparams_evalue(&s
[nd
].E
, CT
);
1635 evalue_set_si(eres
, 0, 1);
1637 eres
->x
.p
= new_enode(partition
, 2*nd
, C
->Dimension
);
1638 for (int j
= 0; j
< nd
; ++j
) {
1639 EVALUE_SET_DOMAIN(eres
->x
.p
->arr
[2*j
], s
[j
].D
);
1640 value_clear(eres
->x
.p
->arr
[2*j
+1].d
);
1641 eres
->x
.p
->arr
[2*j
+1] = s
[j
].E
;
1642 Domain_Free(fVD
[j
]);
1650 for (int j
= 0; j
< PP
->nbV
; ++j
)
1651 Domain_Free(vcone
[j
]);
1657 Polyhedron_Free(CEq
);
1662 Enumeration
* barvinok_enumerate(Polyhedron
*P
, Polyhedron
* C
, unsigned MaxRays
)
1664 evalue
*EP
= barvinok_enumerate_ev(P
, C
, MaxRays
);
1666 return partition2enumeration(EP
);
1669 static void SwapColumns(Value
**V
, int n
, int i
, int j
)
1671 for (int r
= 0; r
< n
; ++r
)
1672 value_swap(V
[r
][i
], V
[r
][j
]);
1675 static void SwapColumns(Polyhedron
*P
, int i
, int j
)
1677 SwapColumns(P
->Constraint
, P
->NbConstraints
, i
, j
);
1678 SwapColumns(P
->Ray
, P
->NbRays
, i
, j
);
1681 static void negative_test_constraint(Value
*l
, Value
*u
, Value
*c
, int pos
,
1684 value_oppose(*v
, u
[pos
+1]);
1685 Vector_Combine(l
+1, u
+1, c
+1, *v
, l
[pos
+1], len
-1);
1686 value_multiply(*v
, *v
, l
[pos
+1]);
1687 value_substract(c
[len
-1], c
[len
-1], *v
);
1688 value_set_si(*v
, -1);
1689 Vector_Scale(c
+1, c
+1, *v
, len
-1);
1690 value_decrement(c
[len
-1], c
[len
-1]);
1691 ConstraintSimplify(c
, c
, len
, v
);
1694 static void oppose_constraint(Value
*c
, int len
, Value
*v
)
1696 value_set_si(*v
, -1);
1697 Vector_Scale(c
+1, c
+1, *v
, len
-1);
1698 value_decrement(c
[len
-1], c
[len
-1]);
1701 static bool SplitOnConstraint(Polyhedron
*P
, int i
, int l
, int u
,
1702 int nvar
, int len
, int exist
, int MaxRays
,
1703 Vector
*row
, Value
& f
, bool independent
,
1704 Polyhedron
**pos
, Polyhedron
**neg
)
1706 negative_test_constraint(P
->Constraint
[l
], P
->Constraint
[u
],
1707 row
->p
, nvar
+i
, len
, &f
);
1708 *neg
= AddConstraints(row
->p
, 1, P
, MaxRays
);
1710 /* We found an independent, but useless constraint
1711 * Maybe we should detect this earlier and not
1712 * mark the variable as INDEPENDENT
1714 if (emptyQ((*neg
))) {
1715 Polyhedron_Free(*neg
);
1719 oppose_constraint(row
->p
, len
, &f
);
1720 *pos
= AddConstraints(row
->p
, 1, P
, MaxRays
);
1722 if (emptyQ((*pos
))) {
1723 Polyhedron_Free(*neg
);
1724 Polyhedron_Free(*pos
);
1732 * unimodularly transform P such that constraint r is transformed
1733 * into a constraint that involves only a single (the first)
1734 * existential variable
1737 static Polyhedron
*rotate_along(Polyhedron
*P
, int r
, int nvar
, int exist
,
1743 Vector
*row
= Vector_Alloc(exist
);
1744 Vector_Copy(P
->Constraint
[r
]+1+nvar
, row
->p
, exist
);
1745 Vector_Gcd(row
->p
, exist
, &g
);
1746 if (value_notone_p(g
))
1747 Vector_AntiScale(row
->p
, row
->p
, g
, exist
);
1750 Matrix
*M
= unimodular_complete(row
);
1751 Matrix
*M2
= Matrix_Alloc(P
->Dimension
+1, P
->Dimension
+1);
1752 for (r
= 0; r
< nvar
; ++r
)
1753 value_set_si(M2
->p
[r
][r
], 1);
1754 for ( ; r
< nvar
+exist
; ++r
)
1755 Vector_Copy(M
->p
[r
-nvar
], M2
->p
[r
]+nvar
, exist
);
1756 for ( ; r
< P
->Dimension
+1; ++r
)
1757 value_set_si(M2
->p
[r
][r
], 1);
1758 Polyhedron
*T
= Polyhedron_Image(P
, M2
, MaxRays
);
1767 static bool SplitOnVar(Polyhedron
*P
, int i
,
1768 int nvar
, int len
, int exist
, int MaxRays
,
1769 Vector
*row
, Value
& f
, bool independent
,
1770 Polyhedron
**pos
, Polyhedron
**neg
)
1774 for (int l
= P
->NbEq
; l
< P
->NbConstraints
; ++l
) {
1775 if (value_negz_p(P
->Constraint
[l
][nvar
+i
+1]))
1779 for (j
= 0; j
< exist
; ++j
)
1780 if (j
!= i
&& value_notzero_p(P
->Constraint
[l
][nvar
+j
+1]))
1786 for (int u
= P
->NbEq
; u
< P
->NbConstraints
; ++u
) {
1787 if (value_posz_p(P
->Constraint
[u
][nvar
+i
+1]))
1791 for (j
= 0; j
< exist
; ++j
)
1792 if (j
!= i
&& value_notzero_p(P
->Constraint
[u
][nvar
+j
+1]))
1798 if (SplitOnConstraint(P
, i
, l
, u
,
1799 nvar
, len
, exist
, MaxRays
,
1800 row
, f
, independent
,
1804 SwapColumns(*neg
, nvar
+1, nvar
+1+i
);
1814 static bool double_bound_pair(Polyhedron
*P
, int nvar
, int exist
,
1815 int i
, int l1
, int l2
,
1816 Polyhedron
**pos
, Polyhedron
**neg
)
1820 Vector
*row
= Vector_Alloc(P
->Dimension
+2);
1821 value_set_si(row
->p
[0], 1);
1822 value_oppose(f
, P
->Constraint
[l1
][nvar
+i
+1]);
1823 Vector_Combine(P
->Constraint
[l1
]+1, P
->Constraint
[l2
]+1,
1825 P
->Constraint
[l2
][nvar
+i
+1], f
,
1827 ConstraintSimplify(row
->p
, row
->p
, P
->Dimension
+2, &f
);
1828 *pos
= AddConstraints(row
->p
, 1, P
, 0);
1829 value_set_si(f
, -1);
1830 Vector_Scale(row
->p
+1, row
->p
+1, f
, P
->Dimension
+1);
1831 value_decrement(row
->p
[P
->Dimension
+1], row
->p
[P
->Dimension
+1]);
1832 *neg
= AddConstraints(row
->p
, 1, P
, 0);
1836 return !emptyQ((*pos
)) && !emptyQ((*neg
));
1839 static bool double_bound(Polyhedron
*P
, int nvar
, int exist
,
1840 Polyhedron
**pos
, Polyhedron
**neg
)
1842 for (int i
= 0; i
< exist
; ++i
) {
1844 for (l1
= P
->NbEq
; l1
< P
->NbConstraints
; ++l1
) {
1845 if (value_negz_p(P
->Constraint
[l1
][nvar
+i
+1]))
1847 for (l2
= l1
+ 1; l2
< P
->NbConstraints
; ++l2
) {
1848 if (value_negz_p(P
->Constraint
[l2
][nvar
+i
+1]))
1850 if (double_bound_pair(P
, nvar
, exist
, i
, l1
, l2
, pos
, neg
))
1854 for (l1
= P
->NbEq
; l1
< P
->NbConstraints
; ++l1
) {
1855 if (value_posz_p(P
->Constraint
[l1
][nvar
+i
+1]))
1857 if (l1
< P
->NbConstraints
)
1858 for (l2
= l1
+ 1; l2
< P
->NbConstraints
; ++l2
) {
1859 if (value_posz_p(P
->Constraint
[l2
][nvar
+i
+1]))
1861 if (double_bound_pair(P
, nvar
, exist
, i
, l1
, l2
, pos
, neg
))
1873 INDEPENDENT
= 1 << 2
1876 static evalue
* enumerate_or(Polyhedron
*D
,
1877 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
1880 fprintf(stderr
, "\nER: Or\n");
1881 #endif /* DEBUG_ER */
1883 Polyhedron
*N
= D
->next
;
1886 barvinok_enumerate_e(D
, exist
, nparam
, MaxRays
);
1889 for (D
= N
; D
; D
= N
) {
1894 barvinok_enumerate_e(D
, exist
, nparam
, MaxRays
);
1897 free_evalue_refs(EN
);
1907 static evalue
* enumerate_sum(Polyhedron
*P
,
1908 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
1910 int nvar
= P
->Dimension
- exist
- nparam
;
1911 int toswap
= nvar
< exist
? nvar
: exist
;
1912 for (int i
= 0; i
< toswap
; ++i
)
1913 SwapColumns(P
, 1 + i
, nvar
+exist
- i
);
1917 fprintf(stderr
, "\nER: Sum\n");
1918 #endif /* DEBUG_ER */
1920 evalue
*EP
= barvinok_enumerate_e(P
, exist
, nparam
, MaxRays
);
1922 for (int i
= 0; i
< /* nvar */ nparam
; ++i
) {
1923 Matrix
*C
= Matrix_Alloc(1, 1 + nparam
+ 1);
1924 value_set_si(C
->p
[0][0], 1);
1926 value_init(split
.d
);
1927 value_set_si(split
.d
, 0);
1928 split
.x
.p
= new_enode(partition
, 4, nparam
);
1929 value_set_si(C
->p
[0][1+i
], 1);
1930 Matrix
*C2
= Matrix_Copy(C
);
1931 EVALUE_SET_DOMAIN(split
.x
.p
->arr
[0],
1932 Constraints2Polyhedron(C2
, MaxRays
));
1934 evalue_set_si(&split
.x
.p
->arr
[1], 1, 1);
1935 value_set_si(C
->p
[0][1+i
], -1);
1936 value_set_si(C
->p
[0][1+nparam
], -1);
1937 EVALUE_SET_DOMAIN(split
.x
.p
->arr
[2],
1938 Constraints2Polyhedron(C
, MaxRays
));
1939 evalue_set_si(&split
.x
.p
->arr
[3], 1, 1);
1941 free_evalue_refs(&split
);
1945 evalue_range_reduction(EP
);
1947 evalue_frac2floor(EP
);
1949 evalue
*sum
= esum(EP
, nvar
);
1951 free_evalue_refs(EP
);
1955 evalue_range_reduction(EP
);
1960 static evalue
* split_sure(Polyhedron
*P
, Polyhedron
*S
,
1961 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
1963 int nvar
= P
->Dimension
- exist
- nparam
;
1965 Matrix
*M
= Matrix_Alloc(exist
, S
->Dimension
+2);
1966 for (int i
= 0; i
< exist
; ++i
)
1967 value_set_si(M
->p
[i
][nvar
+i
+1], 1);
1969 S
= DomainAddRays(S
, M
, MaxRays
);
1971 Polyhedron
*F
= DomainAddRays(P
, M
, MaxRays
);
1972 Polyhedron
*D
= DomainDifference(F
, S
, MaxRays
);
1974 D
= Disjoint_Domain(D
, 0, MaxRays
);
1979 M
= Matrix_Alloc(P
->Dimension
+1-exist
, P
->Dimension
+1);
1980 for (int j
= 0; j
< nvar
; ++j
)
1981 value_set_si(M
->p
[j
][j
], 1);
1982 for (int j
= 0; j
< nparam
+1; ++j
)
1983 value_set_si(M
->p
[nvar
+j
][nvar
+exist
+j
], 1);
1984 Polyhedron
*T
= Polyhedron_Image(S
, M
, MaxRays
);
1985 evalue
*EP
= barvinok_enumerate_e(T
, 0, nparam
, MaxRays
);
1990 for (Polyhedron
*Q
= D
; Q
; Q
= Q
->next
) {
1991 Polyhedron
*N
= Q
->next
;
1993 T
= DomainIntersection(P
, Q
, MaxRays
);
1994 evalue
*E
= barvinok_enumerate_e(T
, exist
, nparam
, MaxRays
);
1996 free_evalue_refs(E
);
2005 static evalue
* enumerate_sure(Polyhedron
*P
,
2006 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
2010 int nvar
= P
->Dimension
- exist
- nparam
;
2016 for (i
= 0; i
< exist
; ++i
) {
2017 Matrix
*M
= Matrix_Alloc(S
->NbConstraints
, S
->Dimension
+2);
2019 value_set_si(lcm
, 1);
2020 for (int j
= 0; j
< S
->NbConstraints
; ++j
) {
2021 if (value_negz_p(S
->Constraint
[j
][1+nvar
+i
]))
2023 if (value_one_p(S
->Constraint
[j
][1+nvar
+i
]))
2025 value_lcm(lcm
, S
->Constraint
[j
][1+nvar
+i
], &lcm
);
2028 for (int j
= 0; j
< S
->NbConstraints
; ++j
) {
2029 if (value_negz_p(S
->Constraint
[j
][1+nvar
+i
]))
2031 if (value_one_p(S
->Constraint
[j
][1+nvar
+i
]))
2033 value_division(f
, lcm
, S
->Constraint
[j
][1+nvar
+i
]);
2034 Vector_Scale(S
->Constraint
[j
], M
->p
[c
], f
, S
->Dimension
+2);
2035 value_substract(M
->p
[c
][S
->Dimension
+1],
2036 M
->p
[c
][S
->Dimension
+1],
2038 value_increment(M
->p
[c
][S
->Dimension
+1],
2039 M
->p
[c
][S
->Dimension
+1]);
2043 S
= AddConstraints(M
->p
[0], c
, S
, MaxRays
);
2058 fprintf(stderr
, "\nER: Sure\n");
2059 #endif /* DEBUG_ER */
2061 return split_sure(P
, S
, exist
, nparam
, MaxRays
);
2064 static evalue
* enumerate_sure2(Polyhedron
*P
,
2065 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
2067 int nvar
= P
->Dimension
- exist
- nparam
;
2069 for (r
= 0; r
< P
->NbRays
; ++r
)
2070 if (value_one_p(P
->Ray
[r
][0]) &&
2071 value_one_p(P
->Ray
[r
][P
->Dimension
+1]))
2077 Matrix
*M
= Matrix_Alloc(nvar
+ 1 + nparam
, P
->Dimension
+2);
2078 for (int i
= 0; i
< nvar
; ++i
)
2079 value_set_si(M
->p
[i
][1+i
], 1);
2080 for (int i
= 0; i
< nparam
; ++i
)
2081 value_set_si(M
->p
[i
+nvar
][1+nvar
+exist
+i
], 1);
2082 Vector_Copy(P
->Ray
[r
]+1+nvar
, M
->p
[nvar
+nparam
]+1+nvar
, exist
);
2083 value_set_si(M
->p
[nvar
+nparam
][0], 1);
2084 value_set_si(M
->p
[nvar
+nparam
][P
->Dimension
+1], 1);
2085 Polyhedron
* F
= Rays2Polyhedron(M
, MaxRays
);
2088 Polyhedron
*I
= DomainIntersection(F
, P
, MaxRays
);
2092 fprintf(stderr
, "\nER: Sure2\n");
2093 #endif /* DEBUG_ER */
2095 return split_sure(P
, I
, exist
, nparam
, MaxRays
);
2098 static evalue
* enumerate_cyclic(Polyhedron
*P
,
2099 unsigned exist
, unsigned nparam
,
2100 evalue
* EP
, int r
, int p
, unsigned MaxRays
)
2102 int nvar
= P
->Dimension
- exist
- nparam
;
2104 /* If EP in its fractional maps only contains references
2105 * to the remainder parameter with appropriate coefficients
2106 * then we could in principle avoid adding existentially
2107 * quantified variables to the validity domains.
2108 * We'd have to replace the remainder by m { p/m }
2109 * and multiply with an appropriate factor that is one
2110 * only in the appropriate range.
2111 * This last multiplication can be avoided if EP
2112 * has a single validity domain with no (further)
2113 * constraints on the remainder parameter
2116 Matrix
*CT
= Matrix_Alloc(nparam
+1, nparam
+3);
2117 Matrix
*M
= Matrix_Alloc(1, 1+nparam
+3);
2118 for (int j
= 0; j
< nparam
; ++j
)
2120 value_set_si(CT
->p
[j
][j
], 1);
2121 value_set_si(CT
->p
[p
][nparam
+1], 1);
2122 value_set_si(CT
->p
[nparam
][nparam
+2], 1);
2123 value_set_si(M
->p
[0][1+p
], -1);
2124 value_absolute(M
->p
[0][1+nparam
], P
->Ray
[0][1+nvar
+exist
+p
]);
2125 value_set_si(M
->p
[0][1+nparam
+1], 1);
2126 Polyhedron
*CEq
= Constraints2Polyhedron(M
, 1);
2128 addeliminatedparams_enum(EP
, CT
, CEq
, MaxRays
, nparam
);
2129 Polyhedron_Free(CEq
);
2135 static void enumerate_vd_add_ray(evalue
*EP
, Matrix
*Rays
, unsigned MaxRays
)
2137 if (value_notzero_p(EP
->d
))
2140 assert(EP
->x
.p
->type
== partition
);
2141 assert(EP
->x
.p
->pos
== EVALUE_DOMAIN(EP
->x
.p
->arr
[0])->Dimension
);
2142 for (int i
= 0; i
< EP
->x
.p
->size
/2; ++i
) {
2143 Polyhedron
*D
= EVALUE_DOMAIN(EP
->x
.p
->arr
[2*i
]);
2144 Polyhedron
*N
= DomainAddRays(D
, Rays
, MaxRays
);
2145 EVALUE_SET_DOMAIN(EP
->x
.p
->arr
[2*i
], N
);
2150 static evalue
* enumerate_line(Polyhedron
*P
,
2151 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
2157 fprintf(stderr
, "\nER: Line\n");
2158 #endif /* DEBUG_ER */
2160 int nvar
= P
->Dimension
- exist
- nparam
;
2162 for (i
= 0; i
< nparam
; ++i
)
2163 if (value_notzero_p(P
->Ray
[0][1+nvar
+exist
+i
]))
2166 for (j
= i
+1; j
< nparam
; ++j
)
2167 if (value_notzero_p(P
->Ray
[0][1+nvar
+exist
+i
]))
2169 assert(j
>= nparam
); // for now
2171 Matrix
*M
= Matrix_Alloc(2, P
->Dimension
+2);
2172 value_set_si(M
->p
[0][0], 1);
2173 value_set_si(M
->p
[0][1+nvar
+exist
+i
], 1);
2174 value_set_si(M
->p
[1][0], 1);
2175 value_set_si(M
->p
[1][1+nvar
+exist
+i
], -1);
2176 value_absolute(M
->p
[1][1+P
->Dimension
], P
->Ray
[0][1+nvar
+exist
+i
]);
2177 value_decrement(M
->p
[1][1+P
->Dimension
], M
->p
[1][1+P
->Dimension
]);
2178 Polyhedron
*S
= AddConstraints(M
->p
[0], 2, P
, MaxRays
);
2179 evalue
*EP
= barvinok_enumerate_e(S
, exist
, nparam
, MaxRays
);
2183 return enumerate_cyclic(P
, exist
, nparam
, EP
, 0, i
, MaxRays
);
2186 static int single_param_pos(Polyhedron
*P
, unsigned exist
, unsigned nparam
,
2189 int nvar
= P
->Dimension
- exist
- nparam
;
2190 if (First_Non_Zero(P
->Ray
[r
]+1, nvar
) != -1)
2192 int i
= First_Non_Zero(P
->Ray
[r
]+1+nvar
+exist
, nparam
);
2195 if (First_Non_Zero(P
->Ray
[r
]+1+nvar
+exist
+1, nparam
-i
-1) != -1)
2200 static evalue
* enumerate_remove_ray(Polyhedron
*P
, int r
,
2201 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
2204 fprintf(stderr
, "\nER: RedundantRay\n");
2205 #endif /* DEBUG_ER */
2209 value_set_si(one
, 1);
2210 int len
= P
->NbRays
-1;
2211 Matrix
*M
= Matrix_Alloc(2 * len
, P
->Dimension
+2);
2212 Vector_Copy(P
->Ray
[0], M
->p
[0], r
* (P
->Dimension
+2));
2213 Vector_Copy(P
->Ray
[r
+1], M
->p
[r
], (len
-r
) * (P
->Dimension
+2));
2214 for (int j
= 0; j
< P
->NbRays
; ++j
) {
2217 Vector_Combine(P
->Ray
[j
], P
->Ray
[r
], M
->p
[len
+j
-(j
>r
)],
2218 one
, P
->Ray
[j
][P
->Dimension
+1], P
->Dimension
+2);
2221 P
= Rays2Polyhedron(M
, MaxRays
);
2223 evalue
*EP
= barvinok_enumerate_e(P
, exist
, nparam
, MaxRays
);
2230 static evalue
* enumerate_redundant_ray(Polyhedron
*P
,
2231 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
2233 assert(P
->NbBid
== 0);
2234 int nvar
= P
->Dimension
- exist
- nparam
;
2238 for (int r
= 0; r
< P
->NbRays
; ++r
) {
2239 if (value_notzero_p(P
->Ray
[r
][P
->Dimension
+1]))
2241 int i1
= single_param_pos(P
, exist
, nparam
, r
);
2244 for (int r2
= r
+1; r2
< P
->NbRays
; ++r2
) {
2245 if (value_notzero_p(P
->Ray
[r2
][P
->Dimension
+1]))
2247 int i2
= single_param_pos(P
, exist
, nparam
, r2
);
2253 value_division(m
, P
->Ray
[r
][1+nvar
+exist
+i1
],
2254 P
->Ray
[r2
][1+nvar
+exist
+i1
]);
2255 value_multiply(m
, m
, P
->Ray
[r2
][1+nvar
+exist
+i1
]);
2256 /* r2 divides r => r redundant */
2257 if (value_eq(m
, P
->Ray
[r
][1+nvar
+exist
+i1
])) {
2259 return enumerate_remove_ray(P
, r
, exist
, nparam
, MaxRays
);
2262 value_division(m
, P
->Ray
[r2
][1+nvar
+exist
+i1
],
2263 P
->Ray
[r
][1+nvar
+exist
+i1
]);
2264 value_multiply(m
, m
, P
->Ray
[r
][1+nvar
+exist
+i1
]);
2265 /* r divides r2 => r2 redundant */
2266 if (value_eq(m
, P
->Ray
[r2
][1+nvar
+exist
+i1
])) {
2268 return enumerate_remove_ray(P
, r2
, exist
, nparam
, MaxRays
);
2276 static Polyhedron
*upper_bound(Polyhedron
*P
,
2277 int pos
, Value
*max
, Polyhedron
**R
)
2286 for (Polyhedron
*Q
= P
; Q
; Q
= N
) {
2288 for (r
= 0; r
< P
->NbRays
; ++r
) {
2289 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1]) &&
2290 value_pos_p(P
->Ray
[r
][1+pos
]))
2293 if (r
< P
->NbRays
) {
2301 for (r
= 0; r
< P
->NbRays
; ++r
) {
2302 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1]))
2304 mpz_fdiv_q(v
, P
->Ray
[r
][1+pos
], P
->Ray
[r
][1+P
->Dimension
]);
2305 if ((!Q
->next
&& r
== 0) || value_gt(v
, *max
))
2306 value_assign(*max
, v
);
2313 static evalue
* enumerate_ray(Polyhedron
*P
,
2314 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
2316 assert(P
->NbBid
== 0);
2317 int nvar
= P
->Dimension
- exist
- nparam
;
2320 for (r
= 0; r
< P
->NbRays
; ++r
)
2321 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1]))
2327 for (r2
= r
+1; r2
< P
->NbRays
; ++r2
)
2328 if (value_zero_p(P
->Ray
[r2
][P
->Dimension
+1]))
2330 if (r2
< P
->NbRays
) {
2332 return enumerate_sum(P
, exist
, nparam
, MaxRays
);
2336 fprintf(stderr
, "\nER: Ray\n");
2337 #endif /* DEBUG_ER */
2343 value_set_si(one
, 1);
2344 int i
= single_param_pos(P
, exist
, nparam
, r
);
2345 assert(i
!= -1); // for now;
2347 Matrix
*M
= Matrix_Alloc(P
->NbRays
, P
->Dimension
+2);
2348 for (int j
= 0; j
< P
->NbRays
; ++j
) {
2349 Vector_Combine(P
->Ray
[j
], P
->Ray
[r
], M
->p
[j
],
2350 one
, P
->Ray
[j
][P
->Dimension
+1], P
->Dimension
+2);
2352 Polyhedron
*S
= Rays2Polyhedron(M
, MaxRays
);
2354 Polyhedron
*D
= DomainDifference(P
, S
, MaxRays
);
2356 // Polyhedron_Print(stderr, P_VALUE_FMT, D);
2357 assert(value_pos_p(P
->Ray
[r
][1+nvar
+exist
+i
])); // for now
2359 D
= upper_bound(D
, nvar
+exist
+i
, &m
, &R
);
2363 M
= Matrix_Alloc(2, P
->Dimension
+2);
2364 value_set_si(M
->p
[0][0], 1);
2365 value_set_si(M
->p
[1][0], 1);
2366 value_set_si(M
->p
[0][1+nvar
+exist
+i
], -1);
2367 value_set_si(M
->p
[1][1+nvar
+exist
+i
], 1);
2368 value_assign(M
->p
[0][1+P
->Dimension
], m
);
2369 value_oppose(M
->p
[1][1+P
->Dimension
], m
);
2370 value_addto(M
->p
[1][1+P
->Dimension
], M
->p
[1][1+P
->Dimension
],
2371 P
->Ray
[r
][1+nvar
+exist
+i
]);
2372 value_decrement(M
->p
[1][1+P
->Dimension
], M
->p
[1][1+P
->Dimension
]);
2373 // Matrix_Print(stderr, P_VALUE_FMT, M);
2374 D
= AddConstraints(M
->p
[0], 2, P
, MaxRays
);
2375 // Polyhedron_Print(stderr, P_VALUE_FMT, D);
2376 value_substract(M
->p
[0][1+P
->Dimension
], M
->p
[0][1+P
->Dimension
],
2377 P
->Ray
[r
][1+nvar
+exist
+i
]);
2378 // Matrix_Print(stderr, P_VALUE_FMT, M);
2379 S
= AddConstraints(M
->p
[0], 1, P
, MaxRays
);
2380 // Polyhedron_Print(stderr, P_VALUE_FMT, S);
2383 evalue
*EP
= barvinok_enumerate_e(D
, exist
, nparam
, MaxRays
);
2388 if (value_notone_p(P
->Ray
[r
][1+nvar
+exist
+i
]))
2389 EP
= enumerate_cyclic(P
, exist
, nparam
, EP
, r
, i
, MaxRays
);
2391 M
= Matrix_Alloc(1, nparam
+2);
2392 value_set_si(M
->p
[0][0], 1);
2393 value_set_si(M
->p
[0][1+i
], 1);
2394 enumerate_vd_add_ray(EP
, M
, MaxRays
);
2399 evalue
*E
= barvinok_enumerate_e(S
, exist
, nparam
, MaxRays
);
2401 free_evalue_refs(E
);
2408 evalue
*ER
= enumerate_or(R
, exist
, nparam
, MaxRays
);
2410 free_evalue_refs(ER
);
2417 static evalue
* new_zero_ep()
2422 evalue_set_si(EP
, 0, 1);
2426 static evalue
* enumerate_vd(Polyhedron
**PA
,
2427 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
2429 Polyhedron
*P
= *PA
;
2430 int nvar
= P
->Dimension
- exist
- nparam
;
2431 Param_Polyhedron
*PP
= NULL
;
2432 Polyhedron
*C
= Universe_Polyhedron(nparam
);
2436 PP
= Polyhedron2Param_SimplifiedDomain(&PR
,C
,MaxRays
,&CEq
,&CT
);
2440 Param_Domain
*D
, *last
;
2443 for (nd
= 0, D
=PP
->D
; D
; D
=D
->next
, ++nd
)
2446 Polyhedron
**VD
= new Polyhedron_p
[nd
];
2447 Polyhedron
**fVD
= new Polyhedron_p
[nd
];
2448 for(nd
= 0, D
=PP
->D
; D
; D
=D
->next
) {
2449 Polyhedron
*rVD
= reduce_domain(D
->Domain
, CT
, CEq
,
2463 /* This doesn't seem to have any effect */
2465 Polyhedron
*CA
= align_context(VD
[0], P
->Dimension
, MaxRays
);
2467 P
= DomainIntersection(P
, CA
, MaxRays
);
2470 Polyhedron_Free(CA
);
2475 if (!EP
&& CT
->NbColumns
!= CT
->NbRows
) {
2476 Polyhedron
*CEqr
= DomainImage(CEq
, CT
, MaxRays
);
2477 Polyhedron
*CA
= align_context(CEqr
, PR
->Dimension
, MaxRays
);
2478 Polyhedron
*I
= DomainIntersection(PR
, CA
, MaxRays
);
2479 Polyhedron_Free(CEqr
);
2480 Polyhedron_Free(CA
);
2482 fprintf(stderr
, "\nER: Eliminate\n");
2483 #endif /* DEBUG_ER */
2484 nparam
-= CT
->NbColumns
- CT
->NbRows
;
2485 EP
= barvinok_enumerate_e(I
, exist
, nparam
, MaxRays
);
2486 nparam
+= CT
->NbColumns
- CT
->NbRows
;
2487 addeliminatedparams_enum(EP
, CT
, CEq
, MaxRays
, nparam
);
2491 Polyhedron_Free(PR
);
2494 if (!EP
&& nd
> 1) {
2496 fprintf(stderr
, "\nER: VD\n");
2497 #endif /* DEBUG_ER */
2498 for (int i
= 0; i
< nd
; ++i
) {
2499 Polyhedron
*CA
= align_context(VD
[i
], P
->Dimension
, MaxRays
);
2500 Polyhedron
*I
= DomainIntersection(P
, CA
, MaxRays
);
2503 EP
= barvinok_enumerate_e(I
, exist
, nparam
, MaxRays
);
2505 evalue
*E
= barvinok_enumerate_e(I
, exist
, nparam
, MaxRays
);
2507 free_evalue_refs(E
);
2511 Polyhedron_Free(CA
);
2515 for (int i
= 0; i
< nd
; ++i
) {
2516 Polyhedron_Free(VD
[i
]);
2517 Polyhedron_Free(fVD
[i
]);
2523 if (!EP
&& nvar
== 0) {
2526 Param_Vertices
*V
, *V2
;
2527 Matrix
* M
= Matrix_Alloc(1, P
->Dimension
+2);
2529 FORALL_PVertex_in_ParamPolyhedron(V
, last
, PP
) {
2531 FORALL_PVertex_in_ParamPolyhedron(V2
, last
, PP
) {
2538 for (int i
= 0; i
< exist
; ++i
) {
2539 value_oppose(f
, V
->Vertex
->p
[i
][nparam
+1]);
2540 Vector_Combine(V
->Vertex
->p
[i
],
2542 M
->p
[0] + 1 + nvar
+ exist
,
2543 V2
->Vertex
->p
[i
][nparam
+1],
2547 for (j
= 0; j
< nparam
; ++j
)
2548 if (value_notzero_p(M
->p
[0][1+nvar
+exist
+j
]))
2552 ConstraintSimplify(M
->p
[0], M
->p
[0],
2553 P
->Dimension
+2, &f
);
2554 value_set_si(M
->p
[0][0], 0);
2555 Polyhedron
*para
= AddConstraints(M
->p
[0], 1, P
,
2558 Polyhedron_Free(para
);
2561 Polyhedron
*pos
, *neg
;
2562 value_set_si(M
->p
[0][0], 1);
2563 value_decrement(M
->p
[0][P
->Dimension
+1],
2564 M
->p
[0][P
->Dimension
+1]);
2565 neg
= AddConstraints(M
->p
[0], 1, P
, MaxRays
);
2566 value_set_si(f
, -1);
2567 Vector_Scale(M
->p
[0]+1, M
->p
[0]+1, f
,
2569 value_decrement(M
->p
[0][P
->Dimension
+1],
2570 M
->p
[0][P
->Dimension
+1]);
2571 value_decrement(M
->p
[0][P
->Dimension
+1],
2572 M
->p
[0][P
->Dimension
+1]);
2573 pos
= AddConstraints(M
->p
[0], 1, P
, MaxRays
);
2574 if (emptyQ(neg
) && emptyQ(pos
)) {
2575 Polyhedron_Free(para
);
2576 Polyhedron_Free(pos
);
2577 Polyhedron_Free(neg
);
2581 fprintf(stderr
, "\nER: Order\n");
2582 #endif /* DEBUG_ER */
2583 EP
= barvinok_enumerate_e(para
, exist
, nparam
, MaxRays
);
2586 E
= barvinok_enumerate_e(pos
, exist
, nparam
, MaxRays
);
2588 free_evalue_refs(E
);
2592 E
= barvinok_enumerate_e(neg
, exist
, nparam
, MaxRays
);
2594 free_evalue_refs(E
);
2597 Polyhedron_Free(para
);
2598 Polyhedron_Free(pos
);
2599 Polyhedron_Free(neg
);
2604 } END_FORALL_PVertex_in_ParamPolyhedron
;
2607 } END_FORALL_PVertex_in_ParamPolyhedron
;
2610 /* Search for vertex coordinate to split on */
2611 /* First look for one independent of the parameters */
2612 FORALL_PVertex_in_ParamPolyhedron(V
, last
, PP
) {
2613 for (int i
= 0; i
< exist
; ++i
) {
2615 for (j
= 0; j
< nparam
; ++j
)
2616 if (value_notzero_p(V
->Vertex
->p
[i
][j
]))
2620 value_set_si(M
->p
[0][0], 1);
2621 Vector_Set(M
->p
[0]+1, 0, nvar
+exist
);
2622 Vector_Copy(V
->Vertex
->p
[i
],
2623 M
->p
[0] + 1 + nvar
+ exist
, nparam
+1);
2624 value_oppose(M
->p
[0][1+nvar
+i
],
2625 V
->Vertex
->p
[i
][nparam
+1]);
2627 Polyhedron
*pos
, *neg
;
2628 value_set_si(M
->p
[0][0], 1);
2629 value_decrement(M
->p
[0][P
->Dimension
+1],
2630 M
->p
[0][P
->Dimension
+1]);
2631 neg
= AddConstraints(M
->p
[0], 1, P
, MaxRays
);
2632 value_set_si(f
, -1);
2633 Vector_Scale(M
->p
[0]+1, M
->p
[0]+1, f
,
2635 value_decrement(M
->p
[0][P
->Dimension
+1],
2636 M
->p
[0][P
->Dimension
+1]);
2637 value_decrement(M
->p
[0][P
->Dimension
+1],
2638 M
->p
[0][P
->Dimension
+1]);
2639 pos
= AddConstraints(M
->p
[0], 1, P
, MaxRays
);
2640 if (emptyQ(neg
) || emptyQ(pos
)) {
2641 Polyhedron_Free(pos
);
2642 Polyhedron_Free(neg
);
2645 Polyhedron_Free(pos
);
2646 value_increment(M
->p
[0][P
->Dimension
+1],
2647 M
->p
[0][P
->Dimension
+1]);
2648 pos
= AddConstraints(M
->p
[0], 1, P
, MaxRays
);
2650 fprintf(stderr
, "\nER: Vertex\n");
2651 #endif /* DEBUG_ER */
2653 EP
= enumerate_or(pos
, exist
, nparam
, MaxRays
);
2658 } END_FORALL_PVertex_in_ParamPolyhedron
;
2662 /* Search for vertex coordinate to split on */
2663 /* Now look for one that depends on the parameters */
2664 FORALL_PVertex_in_ParamPolyhedron(V
, last
, PP
) {
2665 for (int i
= 0; i
< exist
; ++i
) {
2666 value_set_si(M
->p
[0][0], 1);
2667 Vector_Set(M
->p
[0]+1, 0, nvar
+exist
);
2668 Vector_Copy(V
->Vertex
->p
[i
],
2669 M
->p
[0] + 1 + nvar
+ exist
, nparam
+1);
2670 value_oppose(M
->p
[0][1+nvar
+i
],
2671 V
->Vertex
->p
[i
][nparam
+1]);
2673 Polyhedron
*pos
, *neg
;
2674 value_set_si(M
->p
[0][0], 1);
2675 value_decrement(M
->p
[0][P
->Dimension
+1],
2676 M
->p
[0][P
->Dimension
+1]);
2677 neg
= AddConstraints(M
->p
[0], 1, P
, MaxRays
);
2678 value_set_si(f
, -1);
2679 Vector_Scale(M
->p
[0]+1, M
->p
[0]+1, f
,
2681 value_decrement(M
->p
[0][P
->Dimension
+1],
2682 M
->p
[0][P
->Dimension
+1]);
2683 value_decrement(M
->p
[0][P
->Dimension
+1],
2684 M
->p
[0][P
->Dimension
+1]);
2685 pos
= AddConstraints(M
->p
[0], 1, P
, MaxRays
);
2686 if (emptyQ(neg
) || emptyQ(pos
)) {
2687 Polyhedron_Free(pos
);
2688 Polyhedron_Free(neg
);
2691 Polyhedron_Free(pos
);
2692 value_increment(M
->p
[0][P
->Dimension
+1],
2693 M
->p
[0][P
->Dimension
+1]);
2694 pos
= AddConstraints(M
->p
[0], 1, P
, MaxRays
);
2696 fprintf(stderr
, "\nER: ParamVertex\n");
2697 #endif /* DEBUG_ER */
2699 EP
= enumerate_or(pos
, exist
, nparam
, MaxRays
);
2704 } END_FORALL_PVertex_in_ParamPolyhedron
;
2712 Polyhedron_Free(CEq
);
2716 Param_Polyhedron_Free(PP
);
2723 evalue
*barvinok_enumerate_pip(Polyhedron
*P
,
2724 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
2729 evalue
*barvinok_enumerate_pip(Polyhedron
*P
,
2730 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
2732 int nvar
= P
->Dimension
- exist
- nparam
;
2733 evalue
*EP
= new_zero_ep();
2734 Polyhedron
*Q
, *N
, *T
= 0;
2740 fprintf(stderr
, "\nER: PIP\n");
2741 #endif /* DEBUG_ER */
2743 for (int i
= 0; i
< P
->Dimension
; ++i
) {
2746 bool posray
= false;
2747 bool negray
= false;
2748 value_set_si(min
, 0);
2749 for (int j
= 0; j
< P
->NbRays
; ++j
) {
2750 if (value_pos_p(P
->Ray
[j
][1+i
])) {
2752 if (value_zero_p(P
->Ray
[j
][1+P
->Dimension
]))
2754 } else if (value_neg_p(P
->Ray
[j
][1+i
])) {
2756 if (value_zero_p(P
->Ray
[j
][1+P
->Dimension
]))
2760 P
->Ray
[j
][1+i
], P
->Ray
[j
][1+P
->Dimension
]);
2761 if (value_lt(tmp
, min
))
2762 value_assign(min
, tmp
);
2767 assert(!(posray
&& negray
));
2768 assert(!negray
); // for now
2769 Polyhedron
*O
= T
? T
: P
;
2770 /* shift by a safe amount */
2771 Matrix
*M
= Matrix_Alloc(O
->NbRays
, O
->Dimension
+2);
2772 Vector_Copy(O
->Ray
[0], M
->p
[0], O
->NbRays
* (O
->Dimension
+2));
2773 for (int j
= 0; j
< P
->NbRays
; ++j
) {
2774 if (value_notzero_p(M
->p
[j
][1+P
->Dimension
])) {
2775 value_multiply(tmp
, min
, M
->p
[j
][1+P
->Dimension
]);
2776 value_substract(M
->p
[j
][1+i
], M
->p
[j
][1+i
], tmp
);
2781 T
= Rays2Polyhedron(M
, MaxRays
);
2784 /* negating a parameter requires that we substitute in the
2785 * sign again afterwards.
2788 assert(i
< nvar
+exist
);
2790 T
= Polyhedron_Copy(P
);
2791 for (int j
= 0; j
< T
->NbRays
; ++j
)
2792 value_oppose(T
->Ray
[j
][1+i
], T
->Ray
[j
][1+i
]);
2793 for (int j
= 0; j
< T
->NbConstraints
; ++j
)
2794 value_oppose(T
->Constraint
[j
][1+i
], T
->Constraint
[j
][1+i
]);
2800 Polyhedron
*D
= pip_lexmin(T
? T
: P
, exist
, nparam
);
2801 for (Q
= D
; Q
; Q
= N
) {
2805 exist
= Q
->Dimension
- nvar
- nparam
;
2806 E
= barvinok_enumerate_e(Q
, exist
, nparam
, MaxRays
);
2809 free_evalue_refs(E
);
2821 static bool is_single(Value
*row
, int pos
, int len
)
2823 return First_Non_Zero(row
, pos
) == -1 &&
2824 First_Non_Zero(row
+pos
+1, len
-pos
-1) == -1;
2827 static evalue
* barvinok_enumerate_e_r(Polyhedron
*P
,
2828 unsigned exist
, unsigned nparam
, unsigned MaxRays
);
2831 static int er_level
= 0;
2833 evalue
* barvinok_enumerate_e(Polyhedron
*P
,
2834 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
2836 fprintf(stderr
, "\nER: level %i\n", er_level
);
2837 int nvar
= P
->Dimension
- exist
- nparam
;
2838 fprintf(stderr
, "%d %d %d\n", nvar
, exist
, nparam
);
2840 Polyhedron_Print(stderr
, P_VALUE_FMT
, P
);
2842 P
= DomainConstraintSimplify(Polyhedron_Copy(P
), MaxRays
);
2843 evalue
*EP
= barvinok_enumerate_e_r(P
, exist
, nparam
, MaxRays
);
2849 evalue
* barvinok_enumerate_e(Polyhedron
*P
,
2850 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
2852 P
= DomainConstraintSimplify(Polyhedron_Copy(P
), MaxRays
);
2853 evalue
*EP
= barvinok_enumerate_e_r(P
, exist
, nparam
, MaxRays
);
2859 static evalue
* barvinok_enumerate_e_r(Polyhedron
*P
,
2860 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
2863 Polyhedron
*U
= Universe_Polyhedron(nparam
);
2864 evalue
*EP
= barvinok_enumerate_ev(P
, U
, MaxRays
);
2865 //char *param_name[] = {"P", "Q", "R", "S", "T" };
2866 //print_evalue(stdout, EP, param_name);
2871 int nvar
= P
->Dimension
- exist
- nparam
;
2872 int len
= P
->Dimension
+ 2;
2875 return new_zero_ep();
2877 if (nvar
== 0 && nparam
== 0) {
2878 evalue
*EP
= new_zero_ep();
2879 barvinok_count(P
, &EP
->x
.n
, MaxRays
);
2880 if (value_pos_p(EP
->x
.n
))
2881 value_set_si(EP
->x
.n
, 1);
2886 for (r
= 0; r
< P
->NbRays
; ++r
)
2887 if (value_zero_p(P
->Ray
[r
][0]) ||
2888 value_zero_p(P
->Ray
[r
][P
->Dimension
+1])) {
2890 for (i
= 0; i
< nvar
; ++i
)
2891 if (value_notzero_p(P
->Ray
[r
][i
+1]))
2895 for (i
= nvar
+ exist
; i
< nvar
+ exist
+ nparam
; ++i
)
2896 if (value_notzero_p(P
->Ray
[r
][i
+1]))
2898 if (i
>= nvar
+ exist
+ nparam
)
2901 if (r
< P
->NbRays
) {
2902 evalue
*EP
= new_zero_ep();
2903 value_set_si(EP
->x
.n
, -1);
2908 for (r
= 0; r
< P
->NbEq
; ++r
)
2909 if ((first
= First_Non_Zero(P
->Constraint
[r
]+1+nvar
, exist
)) != -1)
2912 if (First_Non_Zero(P
->Constraint
[r
]+1+nvar
+first
+1,
2913 exist
-first
-1) != -1) {
2914 Polyhedron
*T
= rotate_along(P
, r
, nvar
, exist
, MaxRays
);
2916 fprintf(stderr
, "\nER: Equality\n");
2917 #endif /* DEBUG_ER */
2918 evalue
*EP
= barvinok_enumerate_e(T
, exist
-1, nparam
, MaxRays
);
2923 fprintf(stderr
, "\nER: Fixed\n");
2924 #endif /* DEBUG_ER */
2926 return barvinok_enumerate_e(P
, exist
-1, nparam
, MaxRays
);
2928 Polyhedron
*T
= Polyhedron_Copy(P
);
2929 SwapColumns(T
, nvar
+1, nvar
+1+first
);
2930 evalue
*EP
= barvinok_enumerate_e(T
, exist
-1, nparam
, MaxRays
);
2937 Vector
*row
= Vector_Alloc(len
);
2938 value_set_si(row
->p
[0], 1);
2943 enum constraint
* info
= new constraint
[exist
];
2944 for (int i
= 0; i
< exist
; ++i
) {
2946 for (int l
= P
->NbEq
; l
< P
->NbConstraints
; ++l
) {
2947 if (value_negz_p(P
->Constraint
[l
][nvar
+i
+1]))
2949 bool l_parallel
= is_single(P
->Constraint
[l
]+nvar
+1, i
, exist
);
2950 for (int u
= P
->NbEq
; u
< P
->NbConstraints
; ++u
) {
2951 if (value_posz_p(P
->Constraint
[u
][nvar
+i
+1]))
2953 bool lu_parallel
= l_parallel
||
2954 is_single(P
->Constraint
[u
]+nvar
+1, i
, exist
);
2955 value_oppose(f
, P
->Constraint
[u
][nvar
+i
+1]);
2956 Vector_Combine(P
->Constraint
[l
]+1, P
->Constraint
[u
]+1, row
->p
+1,
2957 f
, P
->Constraint
[l
][nvar
+i
+1], len
-1);
2958 if (!(info
[i
] & INDEPENDENT
)) {
2960 for (j
= 0; j
< exist
; ++j
)
2961 if (j
!= i
&& value_notzero_p(row
->p
[nvar
+j
+1]))
2964 //printf("independent: i: %d, l: %d, u: %d\n", i, l, u);
2965 info
[i
] = (constraint
)(info
[i
] | INDEPENDENT
);
2968 if (info
[i
] & ALL_POS
) {
2969 value_addto(row
->p
[len
-1], row
->p
[len
-1],
2970 P
->Constraint
[l
][nvar
+i
+1]);
2971 value_addto(row
->p
[len
-1], row
->p
[len
-1], f
);
2972 value_multiply(f
, f
, P
->Constraint
[l
][nvar
+i
+1]);
2973 value_substract(row
->p
[len
-1], row
->p
[len
-1], f
);
2974 value_decrement(row
->p
[len
-1], row
->p
[len
-1]);
2975 ConstraintSimplify(row
->p
, row
->p
, len
, &f
);
2976 value_set_si(f
, -1);
2977 Vector_Scale(row
->p
+1, row
->p
+1, f
, len
-1);
2978 value_decrement(row
->p
[len
-1], row
->p
[len
-1]);
2979 Polyhedron
*T
= AddConstraints(row
->p
, 1, P
, MaxRays
);
2981 //printf("not all_pos: i: %d, l: %d, u: %d\n", i, l, u);
2982 info
[i
] = (constraint
)(info
[i
] ^ ALL_POS
);
2984 //puts("pos remainder");
2985 //Polyhedron_Print(stdout, P_VALUE_FMT, T);
2988 if (!(info
[i
] & ONE_NEG
)) {
2990 negative_test_constraint(P
->Constraint
[l
],
2992 row
->p
, nvar
+i
, len
, &f
);
2993 oppose_constraint(row
->p
, len
, &f
);
2994 Polyhedron
*T
= AddConstraints(row
->p
, 1, P
, MaxRays
);
2996 //printf("one_neg i: %d, l: %d, u: %d\n", i, l, u);
2997 info
[i
] = (constraint
)(info
[i
] | ONE_NEG
);
2999 //puts("neg remainder");
3000 //Polyhedron_Print(stdout, P_VALUE_FMT, T);
3004 if (!(info
[i
] & ALL_POS
) && (info
[i
] & ONE_NEG
))
3008 if (info
[i
] & ALL_POS
)
3015 for (int i = 0; i < exist; ++i)
3016 printf("%i: %i\n", i, info[i]);
3018 for (int i
= 0; i
< exist
; ++i
)
3019 if (info
[i
] & ALL_POS
) {
3021 fprintf(stderr
, "\nER: Positive\n");
3022 #endif /* DEBUG_ER */
3024 // Maybe we should chew off some of the fat here
3025 Matrix
*M
= Matrix_Alloc(P
->Dimension
, P
->Dimension
+1);
3026 for (int j
= 0; j
< P
->Dimension
; ++j
)
3027 value_set_si(M
->p
[j
][j
+ (j
>= i
+nvar
)], 1);
3028 Polyhedron
*T
= Polyhedron_Image(P
, M
, MaxRays
);
3030 evalue
*EP
= barvinok_enumerate_e(T
, exist
-1, nparam
, MaxRays
);
3037 for (int i
= 0; i
< exist
; ++i
)
3038 if (info
[i
] & ONE_NEG
) {
3040 fprintf(stderr
, "\nER: Negative\n");
3041 #endif /* DEBUG_ER */
3046 return barvinok_enumerate_e(P
, exist
-1, nparam
, MaxRays
);
3048 Polyhedron
*T
= Polyhedron_Copy(P
);
3049 SwapColumns(T
, nvar
+1, nvar
+1+i
);
3050 evalue
*EP
= barvinok_enumerate_e(T
, exist
-1, nparam
, MaxRays
);
3055 for (int i
= 0; i
< exist
; ++i
)
3056 if (info
[i
] & INDEPENDENT
) {
3057 Polyhedron
*pos
, *neg
;
3059 /* Find constraint again and split off negative part */
3061 if (SplitOnVar(P
, i
, nvar
, len
, exist
, MaxRays
,
3062 row
, f
, true, &pos
, &neg
)) {
3064 fprintf(stderr
, "\nER: Split\n");
3065 #endif /* DEBUG_ER */
3068 barvinok_enumerate_e(neg
, exist
-1, nparam
, MaxRays
);
3070 barvinok_enumerate_e(pos
, exist
, nparam
, MaxRays
);
3072 free_evalue_refs(E
);
3074 Polyhedron_Free(neg
);
3075 Polyhedron_Free(pos
);
3089 EP
= enumerate_line(P
, exist
, nparam
, MaxRays
);
3093 EP
= barvinok_enumerate_pip(P
, exist
, nparam
, MaxRays
);
3097 EP
= enumerate_redundant_ray(P
, exist
, nparam
, MaxRays
);
3101 EP
= enumerate_sure(P
, exist
, nparam
, MaxRays
);
3105 EP
= enumerate_ray(P
, exist
, nparam
, MaxRays
);
3109 EP
= enumerate_sure2(P
, exist
, nparam
, MaxRays
);
3113 F
= unfringe(P
, MaxRays
);
3114 if (!PolyhedronIncludes(F
, P
)) {
3116 fprintf(stderr
, "\nER: Fringed\n");
3117 #endif /* DEBUG_ER */
3118 EP
= barvinok_enumerate_e(F
, exist
, nparam
, MaxRays
);
3125 EP
= enumerate_vd(&P
, exist
, nparam
, MaxRays
);
3130 EP
= enumerate_sum(P
, exist
, nparam
, MaxRays
);
3137 Polyhedron
*pos
, *neg
;
3138 for (i
= 0; i
< exist
; ++i
)
3139 if (SplitOnVar(P
, i
, nvar
, len
, exist
, MaxRays
,
3140 row
, f
, false, &pos
, &neg
))
3146 EP
= enumerate_or(pos
, exist
, nparam
, MaxRays
);