8 #include <NTL/mat_ZZ.h>
12 #include <polylib/polylibgmp.h>
13 #include "ev_operations.h"
28 using std::ostringstream
;
30 #define ALLOC(p) (((long *) (p))[0])
31 #define SIZE(p) (((long *) (p))[1])
32 #define DATA(p) ((mp_limb_t *) (((long *) (p)) + 2))
34 static void value2zz(Value v
, ZZ
& z
)
36 int sa
= v
[0]._mp_size
;
37 int abs_sa
= sa
< 0 ? -sa
: sa
;
39 _ntl_gsetlength(&z
.rep
, abs_sa
);
40 mp_limb_t
* adata
= DATA(z
.rep
);
41 for (int i
= 0; i
< abs_sa
; ++i
)
42 adata
[i
] = v
[0]._mp_d
[i
];
46 void zz2value(ZZ
& z
, Value
& v
)
54 int abs_sa
= sa
< 0 ? -sa
: sa
;
56 mp_limb_t
* adata
= DATA(z
.rep
);
57 _mpz_realloc(v
, abs_sa
);
58 for (int i
= 0; i
< abs_sa
; ++i
)
59 v
[0]._mp_d
[i
] = adata
[i
];
64 #define ALLOC(t,p) p = (t*)malloc(sizeof(*p))
67 * We just ignore the last column and row
68 * If the final element is not equal to one
69 * then the result will actually be a multiple of the input
71 static void matrix2zz(Matrix
*M
, mat_ZZ
& m
, unsigned nr
, unsigned nc
)
75 for (int i
= 0; i
< nr
; ++i
) {
76 // assert(value_one_p(M->p[i][M->NbColumns - 1]));
77 for (int j
= 0; j
< nc
; ++j
) {
78 value2zz(M
->p
[i
][j
], m
[i
][j
]);
83 static void values2zz(Value
*p
, vec_ZZ
& v
, int len
)
87 for (int i
= 0; i
< len
; ++i
) {
94 static void zz2values(vec_ZZ
& v
, Value
*p
)
96 for (int i
= 0; i
< v
.length(); ++i
)
100 static void rays(mat_ZZ
& r
, Polyhedron
*C
)
102 unsigned dim
= C
->NbRays
- 1; /* don't count zero vertex */
103 assert(C
->NbRays
- 1 == C
->Dimension
);
108 for (i
= 0, c
= 0; i
< dim
; ++i
)
109 if (value_zero_p(C
->Ray
[i
][dim
+1])) {
110 for (int j
= 0; j
< dim
; ++j
) {
111 value2zz(C
->Ray
[i
][j
+1], tmp
);
118 static Matrix
* rays(Polyhedron
*C
)
120 unsigned dim
= C
->NbRays
- 1; /* don't count zero vertex */
121 assert(C
->NbRays
- 1 == C
->Dimension
);
123 Matrix
*M
= Matrix_Alloc(dim
+1, dim
+1);
127 for (i
= 0, c
= 0; i
<= dim
&& c
< dim
; ++i
)
128 if (value_zero_p(C
->Ray
[i
][dim
+1])) {
129 Vector_Copy(C
->Ray
[i
] + 1, M
->p
[c
], dim
);
130 value_set_si(M
->p
[c
++][dim
], 0);
133 value_set_si(M
->p
[dim
][dim
], 1);
138 static Matrix
* rays2(Polyhedron
*C
)
140 unsigned dim
= C
->NbRays
- 1; /* don't count zero vertex */
141 assert(C
->NbRays
- 1 == C
->Dimension
);
143 Matrix
*M
= Matrix_Alloc(dim
, dim
);
147 for (i
= 0, c
= 0; i
<= dim
&& c
< dim
; ++i
)
148 if (value_zero_p(C
->Ray
[i
][dim
+1]))
149 Vector_Copy(C
->Ray
[i
] + 1, M
->p
[c
++], dim
);
156 * Returns the largest absolute value in the vector
158 static ZZ
max(vec_ZZ
& v
)
161 for (int i
= 1; i
< v
.length(); ++i
)
171 Rays
= Matrix_Copy(M
);
174 cone(Polyhedron
*C
) {
175 Cone
= Polyhedron_Copy(C
);
181 matrix2zz(Rays
, A
, Rays
->NbRows
- 1, Rays
->NbColumns
- 1);
182 det
= determinant(A
);
185 Vector
* short_vector(vec_ZZ
& lambda
) {
186 Matrix
*M
= Matrix_Copy(Rays
);
187 Matrix
*inv
= Matrix_Alloc(M
->NbRows
, M
->NbColumns
);
188 int ok
= Matrix_Inverse(M
, inv
);
195 matrix2zz(inv
, B
, inv
->NbRows
- 1, inv
->NbColumns
- 1);
196 long r
= LLL(det2
, B
, U
);
200 for (int i
= 1; i
< B
.NumRows(); ++i
) {
212 Vector
*z
= Vector_Alloc(U
[index
].length()+1);
214 zz2values(U
[index
], z
->p
);
215 value_set_si(z
->p
[U
[index
].length()], 0);
219 Polyhedron
*C
= poly();
221 for (i
= 0; i
< C
->NbConstraints
; ++i
) {
222 Inner_Product(z
->p
, C
->Constraint
[i
]+1, z
->Size
-1, &tmp
);
223 if (value_pos_p(tmp
))
226 if (i
== C
->NbConstraints
) {
227 value_set_si(tmp
, -1);
228 Vector_Scale(z
->p
, z
->p
, tmp
, z
->Size
-1);
235 Polyhedron_Free(Cone
);
241 Matrix
*M
= Matrix_Alloc(Rays
->NbRows
+1, Rays
->NbColumns
+1);
242 for (int i
= 0; i
< Rays
->NbRows
; ++i
) {
243 Vector_Copy(Rays
->p
[i
], M
->p
[i
]+1, Rays
->NbColumns
);
244 value_set_si(M
->p
[i
][0], 1);
246 Vector_Set(M
->p
[Rays
->NbRows
]+1, 0, Rays
->NbColumns
-1);
247 value_set_si(M
->p
[Rays
->NbRows
][0], 1);
248 value_set_si(M
->p
[Rays
->NbRows
][Rays
->NbColumns
], 1);
249 Cone
= Rays2Polyhedron(M
, M
->NbRows
+1);
250 assert(Cone
->NbConstraints
== Cone
->NbRays
);
264 dpoly(int d
, ZZ
& degree
, int offset
= 0) {
265 coeff
.SetLength(d
+1);
267 int min
= d
+ offset
;
268 if (degree
>= 0 && degree
< ZZ(INIT_VAL
, min
))
269 min
= to_int(degree
);
271 ZZ c
= ZZ(INIT_VAL
, 1);
274 for (int i
= 1; i
<= min
; ++i
) {
275 c
*= (degree
-i
+ 1);
280 void operator *= (dpoly
& f
) {
281 assert(coeff
.length() == f
.coeff
.length());
283 coeff
= f
.coeff
[0] * coeff
;
284 for (int i
= 1; i
< coeff
.length(); ++i
)
285 for (int j
= 0; i
+j
< coeff
.length(); ++j
)
286 coeff
[i
+j
] += f
.coeff
[i
] * old
[j
];
288 void div(dpoly
& d
, mpq_t count
, ZZ
& sign
) {
289 int len
= coeff
.length();
292 mpq_t
* c
= new mpq_t
[coeff
.length()];
295 for (int i
= 0; i
< len
; ++i
) {
297 zz2value(coeff
[i
], tmp
);
298 mpq_set_z(c
[i
], tmp
);
300 for (int j
= 1; j
<= i
; ++j
) {
301 zz2value(d
.coeff
[j
], tmp
);
302 mpq_set_z(qtmp
, tmp
);
303 mpq_mul(qtmp
, qtmp
, c
[i
-j
]);
304 mpq_sub(c
[i
], c
[i
], qtmp
);
307 zz2value(d
.coeff
[0], tmp
);
308 mpq_set_z(qtmp
, tmp
);
309 mpq_div(c
[i
], c
[i
], qtmp
);
312 mpq_sub(count
, count
, c
[len
-1]);
314 mpq_add(count
, count
, c
[len
-1]);
318 for (int i
= 0; i
< len
; ++i
)
330 dpoly_n(int d
, ZZ
& degree_0
, ZZ
& degree_1
, int offset
= 0) {
334 zz2value(degree_0
, d0
);
335 zz2value(degree_1
, d1
);
336 coeff
= Matrix_Alloc(d
+1, d
+1+1);
337 value_set_si(coeff
->p
[0][0], 1);
338 value_set_si(coeff
->p
[0][d
+1], 1);
339 for (int i
= 1; i
<= d
; ++i
) {
340 value_multiply(coeff
->p
[i
][0], coeff
->p
[i
-1][0], d0
);
341 Vector_Combine(coeff
->p
[i
-1], coeff
->p
[i
-1]+1, coeff
->p
[i
]+1,
343 value_set_si(coeff
->p
[i
][d
+1], i
);
344 value_multiply(coeff
->p
[i
][d
+1], coeff
->p
[i
][d
+1], coeff
->p
[i
-1][d
+1]);
345 value_decrement(d0
, d0
);
350 void div(dpoly
& d
, Vector
*count
, ZZ
& sign
) {
351 int len
= coeff
->NbRows
;
352 Matrix
* c
= Matrix_Alloc(coeff
->NbRows
, coeff
->NbColumns
);
355 for (int i
= 0; i
< len
; ++i
) {
356 Vector_Copy(coeff
->p
[i
], c
->p
[i
], len
+1);
357 for (int j
= 1; j
<= i
; ++j
) {
358 zz2value(d
.coeff
[j
], tmp
);
359 value_multiply(tmp
, tmp
, c
->p
[i
][len
]);
360 value_oppose(tmp
, tmp
);
361 Vector_Combine(c
->p
[i
], c
->p
[i
-j
], c
->p
[i
],
362 c
->p
[i
-j
][len
], tmp
, len
);
363 value_multiply(c
->p
[i
][len
], c
->p
[i
][len
], c
->p
[i
-j
][len
]);
365 zz2value(d
.coeff
[0], tmp
);
366 value_multiply(c
->p
[i
][len
], c
->p
[i
][len
], tmp
);
369 value_set_si(tmp
, -1);
370 Vector_Scale(c
->p
[len
-1], count
->p
, tmp
, len
);
371 value_assign(count
->p
[len
], c
->p
[len
-1][len
]);
373 Vector_Copy(c
->p
[len
-1], count
->p
, len
+1);
374 Vector_Normalize(count
->p
, len
+1);
380 struct dpoly_r_term
{
385 /* len: number of elements in c
386 * each element in c is the coefficient of a power of t
387 * in the MacLaurin expansion
390 vector
< dpoly_r_term
* > *c
;
395 void add_term(int i
, int * powers
, ZZ
& coeff
) {
398 for (int k
= 0; k
< c
[i
].size(); ++k
) {
399 if (memcmp(c
[i
][k
]->powers
, powers
, dim
* sizeof(int)) == 0) {
400 c
[i
][k
]->coeff
+= coeff
;
404 dpoly_r_term
*t
= new dpoly_r_term
;
405 t
->powers
= new int[dim
];
406 memcpy(t
->powers
, powers
, dim
* sizeof(int));
410 dpoly_r(int len
, int dim
) {
414 c
= new vector
< dpoly_r_term
* > [len
];
416 dpoly_r(dpoly
& num
, dpoly
& den
, int pos
, int sign
, int dim
) {
418 len
= num
.coeff
.length();
419 c
= new vector
< dpoly_r_term
* > [len
];
423 for (int i
= 0; i
< len
; ++i
) {
424 ZZ coeff
= num
.coeff
[i
];
425 memset(powers
, 0, dim
* sizeof(int));
428 add_term(i
, powers
, coeff
);
430 for (int j
= 1; j
<= i
; ++j
) {
431 for (int k
= 0; k
< c
[i
-j
].size(); ++k
) {
432 memcpy(powers
, c
[i
-j
][k
]->powers
, dim
*sizeof(int));
434 coeff
= -den
.coeff
[j
-1] * c
[i
-j
][k
]->coeff
;
435 add_term(i
, powers
, coeff
);
441 dpoly_r(dpoly_r
* num
, dpoly
& den
, int pos
, int sign
, int dim
) {
444 c
= new vector
< dpoly_r_term
* > [len
];
449 for (int i
= 0 ; i
< len
; ++i
) {
450 for (int k
= 0; k
< num
->c
[i
].size(); ++k
) {
451 memcpy(powers
, num
->c
[i
][k
]->powers
, dim
*sizeof(int));
453 add_term(i
, powers
, num
->c
[i
][k
]->coeff
);
456 for (int j
= 1; j
<= i
; ++j
) {
457 for (int k
= 0; k
< c
[i
-j
].size(); ++k
) {
458 memcpy(powers
, c
[i
-j
][k
]->powers
, dim
*sizeof(int));
460 coeff
= -den
.coeff
[j
-1] * c
[i
-j
][k
]->coeff
;
461 add_term(i
, powers
, coeff
);
467 for (int i
= 0 ; i
< len
; ++i
)
468 for (int k
= 0; k
< c
[i
].size(); ++k
) {
469 delete [] c
[i
][k
]->powers
;
474 dpoly_r
*div(dpoly
& d
) {
475 dpoly_r
*rc
= new dpoly_r(len
, dim
);
476 rc
->denom
= power(d
.coeff
[0], len
);
477 ZZ inv_d
= rc
->denom
/ d
.coeff
[0];
480 for (int i
= 0; i
< len
; ++i
) {
481 for (int k
= 0; k
< c
[i
].size(); ++k
) {
482 coeff
= c
[i
][k
]->coeff
* inv_d
;
483 rc
->add_term(i
, c
[i
][k
]->powers
, coeff
);
486 for (int j
= 1; j
<= i
; ++j
) {
487 for (int k
= 0; k
< rc
->c
[i
-j
].size(); ++k
) {
488 coeff
= - d
.coeff
[j
] * rc
->c
[i
-j
][k
]->coeff
/ d
.coeff
[0];
489 rc
->add_term(i
, rc
->c
[i
-j
][k
]->powers
, coeff
);
496 for (int i
= 0; i
< len
; ++i
) {
499 cout
<< c
[i
].size() << endl
;
500 for (int j
= 0; j
< c
[i
].size(); ++j
) {
501 for (int k
= 0; k
< dim
; ++k
) {
502 cout
<< c
[i
][j
]->powers
[k
] << " ";
504 cout
<< ": " << c
[i
][j
]->coeff
<< "/" << denom
<< endl
;
512 void decompose(Polyhedron
*C
);
513 virtual void handle(Polyhedron
*P
, int sign
) = 0;
516 struct polar_decomposer
: public decomposer
{
517 void decompose(Polyhedron
*C
, unsigned MaxRays
);
518 virtual void handle(Polyhedron
*P
, int sign
);
519 virtual void handle_polar(Polyhedron
*P
, int sign
) = 0;
522 void decomposer::decompose(Polyhedron
*C
)
524 vector
<cone
*> nonuni
;
525 cone
* c
= new cone(C
);
536 while (!nonuni
.empty()) {
539 Vector
* v
= c
->short_vector(lambda
);
540 for (int i
= 0; i
< c
->Rays
->NbRows
- 1; ++i
) {
543 Matrix
* M
= Matrix_Copy(c
->Rays
);
544 Vector_Copy(v
->p
, M
->p
[i
], v
->Size
);
545 cone
* pc
= new cone(M
);
546 assert (pc
->det
!= 0);
547 if (abs(pc
->det
) > 1) {
548 assert(abs(pc
->det
) < abs(c
->det
));
549 nonuni
.push_back(pc
);
551 handle(pc
->poly(), sign(pc
->det
) * s
);
561 void polar_decomposer::decompose(Polyhedron
*cone
, unsigned MaxRays
)
563 Polyhedron_Polarize(cone
);
564 if (cone
->NbRays
- 1 != cone
->Dimension
) {
565 Polyhedron
*tmp
= cone
;
566 cone
= triangularize_cone(cone
, MaxRays
);
567 Polyhedron_Free(tmp
);
569 for (Polyhedron
*Polar
= cone
; Polar
; Polar
= Polar
->next
)
570 decomposer::decompose(Polar
);
574 void polar_decomposer::handle(Polyhedron
*P
, int sign
)
576 Polyhedron_Polarize(P
);
577 handle_polar(P
, sign
);
581 * Barvinok's Decomposition of a simplicial cone
583 * Returns two lists of polyhedra
585 void barvinok_decompose(Polyhedron
*C
, Polyhedron
**ppos
, Polyhedron
**pneg
)
587 Polyhedron
*pos
= *ppos
, *neg
= *pneg
;
588 vector
<cone
*> nonuni
;
589 cone
* c
= new cone(C
);
596 Polyhedron
*p
= Polyhedron_Copy(c
->Cone
);
602 while (!nonuni
.empty()) {
605 Vector
* v
= c
->short_vector(lambda
);
606 for (int i
= 0; i
< c
->Rays
->NbRows
- 1; ++i
) {
609 Matrix
* M
= Matrix_Copy(c
->Rays
);
610 Vector_Copy(v
->p
, M
->p
[i
], v
->Size
);
611 cone
* pc
= new cone(M
);
612 assert (pc
->det
!= 0);
613 if (abs(pc
->det
) > 1) {
614 assert(abs(pc
->det
) < abs(c
->det
));
615 nonuni
.push_back(pc
);
617 Polyhedron
*p
= pc
->poly();
619 if (sign(pc
->det
) == s
) {
637 const int MAX_TRY
=10;
639 * Searches for a vector that is not orthogonal to any
640 * of the rays in rays.
642 static void nonorthog(mat_ZZ
& rays
, vec_ZZ
& lambda
)
644 int dim
= rays
.NumCols();
646 lambda
.SetLength(dim
);
650 for (int i
= 2; !found
&& i
<= 50*dim
; i
+=4) {
651 for (int j
= 0; j
< MAX_TRY
; ++j
) {
652 for (int k
= 0; k
< dim
; ++k
) {
653 int r
= random_int(i
)+2;
654 int v
= (2*(r
%2)-1) * (r
>> 1);
658 for (; k
< rays
.NumRows(); ++k
)
659 if (lambda
* rays
[k
] == 0)
661 if (k
== rays
.NumRows()) {
670 static void randomvector(Polyhedron
*P
, vec_ZZ
& lambda
, int nvar
)
674 unsigned int dim
= P
->Dimension
;
677 for (int i
= 0; i
< P
->NbRays
; ++i
) {
678 for (int j
= 1; j
<= dim
; ++j
) {
679 value_absolute(tmp
, P
->Ray
[i
][j
]);
680 int t
= VALUE_TO_LONG(tmp
) * 16;
685 for (int i
= 0; i
< P
->NbConstraints
; ++i
) {
686 for (int j
= 1; j
<= dim
; ++j
) {
687 value_absolute(tmp
, P
->Constraint
[i
][j
]);
688 int t
= VALUE_TO_LONG(tmp
) * 16;
695 lambda
.SetLength(nvar
);
696 for (int k
= 0; k
< nvar
; ++k
) {
697 int r
= random_int(max
*dim
)+2;
698 int v
= (2*(r
%2)-1) * (max
/2*dim
+ (r
>> 1));
703 static void add_rays(mat_ZZ
& rays
, Polyhedron
*i
, int *r
, int nvar
= -1,
706 unsigned dim
= i
->Dimension
;
709 for (int k
= 0; k
< i
->NbRays
; ++k
) {
710 if (!value_zero_p(i
->Ray
[k
][dim
+1]))
712 if (!all
&& nvar
!= dim
&& First_Non_Zero(i
->Ray
[k
]+1, nvar
) == -1)
714 values2zz(i
->Ray
[k
]+1, rays
[(*r
)++], nvar
);
718 void lattice_point(Value
* values
, Polyhedron
*i
, vec_ZZ
& vertex
)
720 unsigned dim
= i
->Dimension
;
721 if(!value_one_p(values
[dim
])) {
722 Matrix
* Rays
= rays(i
);
723 Matrix
*inv
= Matrix_Alloc(Rays
->NbRows
, Rays
->NbColumns
);
724 int ok
= Matrix_Inverse(Rays
, inv
);
728 Vector
*lambda
= Vector_Alloc(dim
+1);
729 Vector_Matrix_Product(values
, inv
, lambda
->p
);
731 for (int j
= 0; j
< dim
; ++j
)
732 mpz_cdiv_q(lambda
->p
[j
], lambda
->p
[j
], lambda
->p
[dim
]);
733 value_set_si(lambda
->p
[dim
], 1);
734 Vector
*A
= Vector_Alloc(dim
+1);
735 Vector_Matrix_Product(lambda
->p
, Rays
, A
->p
);
738 values2zz(A
->p
, vertex
, dim
);
741 values2zz(values
, vertex
, dim
);
744 static evalue
*term(int param
, ZZ
& c
, Value
*den
= NULL
)
746 evalue
*EP
= new evalue();
748 value_set_si(EP
->d
,0);
749 EP
->x
.p
= new_enode(polynomial
, 2, param
+ 1);
750 evalue_set_si(&EP
->x
.p
->arr
[0], 0, 1);
751 value_init(EP
->x
.p
->arr
[1].x
.n
);
753 value_set_si(EP
->x
.p
->arr
[1].d
, 1);
755 value_assign(EP
->x
.p
->arr
[1].d
, *den
);
756 zz2value(c
, EP
->x
.p
->arr
[1].x
.n
);
760 static void vertex_period(
761 Polyhedron
*i
, vec_ZZ
& lambda
, Matrix
*T
,
762 Value lcm
, int p
, Vector
*val
,
763 evalue
*E
, evalue
* ev
,
766 unsigned nparam
= T
->NbRows
- 1;
767 unsigned dim
= i
->Dimension
;
774 Vector
* values
= Vector_Alloc(dim
+ 1);
775 Vector_Matrix_Product(val
->p
, T
, values
->p
);
776 value_assign(values
->p
[dim
], lcm
);
777 lattice_point(values
->p
, i
, vertex
);
778 num
= vertex
* lambda
;
783 zz2value(num
, ev
->x
.n
);
784 value_assign(ev
->d
, lcm
);
791 values2zz(T
->p
[p
], vertex
, dim
);
792 nump
= vertex
* lambda
;
793 if (First_Non_Zero(val
->p
, p
) == -1) {
794 value_assign(tmp
, lcm
);
795 evalue
*ET
= term(p
, nump
, &tmp
);
797 free_evalue_refs(ET
);
801 value_assign(tmp
, lcm
);
802 if (First_Non_Zero(T
->p
[p
], dim
) != -1)
803 Vector_Gcd(T
->p
[p
], dim
, &tmp
);
805 if (value_lt(tmp
, lcm
)) {
808 value_division(tmp
, lcm
, tmp
);
809 value_set_si(ev
->d
, 0);
810 ev
->x
.p
= new_enode(periodic
, VALUE_TO_INT(tmp
), p
+1);
811 value2zz(tmp
, count
);
813 value_decrement(tmp
, tmp
);
815 ZZ new_offset
= offset
- count
* nump
;
816 value_assign(val
->p
[p
], tmp
);
817 vertex_period(i
, lambda
, T
, lcm
, p
+1, val
, E
,
818 &ev
->x
.p
->arr
[VALUE_TO_INT(tmp
)], new_offset
);
819 } while (value_pos_p(tmp
));
821 vertex_period(i
, lambda
, T
, lcm
, p
+1, val
, E
, ev
, offset
);
825 static void mask_r(Matrix
*f
, int nr
, Vector
*lcm
, int p
, Vector
*val
, evalue
*ev
)
827 unsigned nparam
= lcm
->Size
;
830 Vector
* prod
= Vector_Alloc(f
->NbRows
);
831 Matrix_Vector_Product(f
, val
->p
, prod
->p
);
833 for (int i
= 0; i
< nr
; ++i
) {
834 value_modulus(prod
->p
[i
], prod
->p
[i
], f
->p
[i
][nparam
+1]);
835 isint
&= value_zero_p(prod
->p
[i
]);
837 value_set_si(ev
->d
, 1);
839 value_set_si(ev
->x
.n
, isint
);
846 if (value_one_p(lcm
->p
[p
]))
847 mask_r(f
, nr
, lcm
, p
+1, val
, ev
);
849 value_assign(tmp
, lcm
->p
[p
]);
850 value_set_si(ev
->d
, 0);
851 ev
->x
.p
= new_enode(periodic
, VALUE_TO_INT(tmp
), p
+1);
853 value_decrement(tmp
, tmp
);
854 value_assign(val
->p
[p
], tmp
);
855 mask_r(f
, nr
, lcm
, p
+1, val
, &ev
->x
.p
->arr
[VALUE_TO_INT(tmp
)]);
856 } while (value_pos_p(tmp
));
861 static evalue
*multi_monom(vec_ZZ
& p
)
863 evalue
*X
= new evalue();
866 unsigned nparam
= p
.length()-1;
867 zz2value(p
[nparam
], X
->x
.n
);
868 value_set_si(X
->d
, 1);
869 for (int i
= 0; i
< nparam
; ++i
) {
872 evalue
*T
= term(i
, p
[i
]);
881 * Check whether mapping polyhedron P on the affine combination
882 * num yields a range that has a fixed quotient on integer
884 * If zero is true, then we are only interested in the quotient
885 * for the cases where the remainder is zero.
886 * Returns NULL if false and a newly allocated value if true.
888 static Value
*fixed_quotient(Polyhedron
*P
, vec_ZZ
& num
, Value d
, bool zero
)
891 int len
= num
.length();
892 Matrix
*T
= Matrix_Alloc(2, len
);
893 zz2values(num
, T
->p
[0]);
894 value_set_si(T
->p
[1][len
-1], 1);
895 Polyhedron
*I
= Polyhedron_Image(P
, T
, P
->NbConstraints
);
899 for (i
= 0; i
< I
->NbRays
; ++i
)
900 if (value_zero_p(I
->Ray
[i
][2])) {
908 int bounded
= line_minmax(I
, &min
, &max
);
912 mpz_cdiv_q(min
, min
, d
);
914 mpz_fdiv_q(min
, min
, d
);
915 mpz_fdiv_q(max
, max
, d
);
917 if (value_eq(min
, max
)) {
920 value_assign(*ret
, min
);
928 * Normalize linear expression coef modulo m
929 * Removes common factor and reduces coefficients
930 * Returns index of first non-zero coefficient or len
932 static int normal_mod(Value
*coef
, int len
, Value
*m
)
937 Vector_Gcd(coef
, len
, &gcd
);
939 Vector_AntiScale(coef
, coef
, gcd
, len
);
941 value_division(*m
, *m
, gcd
);
948 for (j
= 0; j
< len
; ++j
)
949 mpz_fdiv_r(coef
[j
], coef
[j
], *m
);
950 for (j
= 0; j
< len
; ++j
)
951 if (value_notzero_p(coef
[j
]))
958 static void mask(Matrix
*f
, evalue
*factor
)
960 int nr
= f
->NbRows
, nc
= f
->NbColumns
;
963 for (n
= 0; n
< nr
&& value_notzero_p(f
->p
[n
][nc
-1]); ++n
)
964 if (value_notone_p(f
->p
[n
][nc
-1]) &&
965 value_notmone_p(f
->p
[n
][nc
-1]))
979 value_set_si(EV
.x
.n
, 1);
981 for (n
= 0; n
< nr
; ++n
) {
982 value_assign(m
, f
->p
[n
][nc
-1]);
983 if (value_one_p(m
) || value_mone_p(m
))
986 int j
= normal_mod(f
->p
[n
], nc
-1, &m
);
988 free_evalue_refs(factor
);
989 value_init(factor
->d
);
990 evalue_set_si(factor
, 0, 1);
994 values2zz(f
->p
[n
], row
, nc
-1);
997 if (j
< (nc
-1)-1 && row
[j
] > g
/2) {
998 for (int k
= j
; k
< (nc
-1); ++k
)
1000 row
[k
] = g
- row
[k
];
1004 value_set_si(EP
.d
, 0);
1005 EP
.x
.p
= new_enode(relation
, 2, 0);
1006 value_clear(EP
.x
.p
->arr
[1].d
);
1007 EP
.x
.p
->arr
[1] = *factor
;
1008 evalue
*ev
= &EP
.x
.p
->arr
[0];
1009 value_set_si(ev
->d
, 0);
1010 ev
->x
.p
= new_enode(fractional
, 3, -1);
1011 evalue_set_si(&ev
->x
.p
->arr
[1], 0, 1);
1012 evalue_set_si(&ev
->x
.p
->arr
[2], 1, 1);
1013 evalue
*E
= multi_monom(row
);
1014 value_assign(EV
.d
, m
);
1016 value_clear(ev
->x
.p
->arr
[0].d
);
1017 ev
->x
.p
->arr
[0] = *E
;
1023 free_evalue_refs(&EV
);
1029 static void mask(Matrix
*f
, evalue
*factor
)
1031 int nr
= f
->NbRows
, nc
= f
->NbColumns
;
1034 for (n
= 0; n
< nr
&& value_notzero_p(f
->p
[n
][nc
-1]); ++n
)
1035 if (value_notone_p(f
->p
[n
][nc
-1]) &&
1036 value_notmone_p(f
->p
[n
][nc
-1]))
1044 unsigned np
= nc
- 2;
1045 Vector
*lcm
= Vector_Alloc(np
);
1046 Vector
*val
= Vector_Alloc(nc
);
1047 Vector_Set(val
->p
, 0, nc
);
1048 value_set_si(val
->p
[np
], 1);
1049 Vector_Set(lcm
->p
, 1, np
);
1050 for (n
= 0; n
< nr
; ++n
) {
1051 if (value_one_p(f
->p
[n
][nc
-1]) ||
1052 value_mone_p(f
->p
[n
][nc
-1]))
1054 for (int j
= 0; j
< np
; ++j
)
1055 if (value_notzero_p(f
->p
[n
][j
])) {
1056 Gcd(f
->p
[n
][j
], f
->p
[n
][nc
-1], &tmp
);
1057 value_division(tmp
, f
->p
[n
][nc
-1], tmp
);
1058 value_lcm(tmp
, lcm
->p
[j
], &lcm
->p
[j
]);
1063 mask_r(f
, nr
, lcm
, 0, val
, &EP
);
1068 free_evalue_refs(&EP
);
1079 static bool mod_needed(Polyhedron
*PD
, vec_ZZ
& num
, Value d
, evalue
*E
)
1081 Value
*q
= fixed_quotient(PD
, num
, d
, false);
1086 value_oppose(*q
, *q
);
1089 value_set_si(EV
.d
, 1);
1091 value_multiply(EV
.x
.n
, *q
, d
);
1093 free_evalue_refs(&EV
);
1099 static void ceil_mod(Value
*coef
, int len
, Value d
, ZZ
& f
, evalue
*EP
, Polyhedron
*PD
)
1103 value_set_si(m
, -1);
1105 Vector_Scale(coef
, coef
, m
, len
);
1108 int j
= normal_mod(coef
, len
, &m
);
1116 values2zz(coef
, num
, len
);
1123 evalue_set_si(&tmp
, 0, 1);
1127 while (j
< len
-1 && (num
[j
] == g
/2 || num
[j
] == 0))
1129 if ((j
< len
-1 && num
[j
] > g
/2) || (j
== len
-1 && num
[j
] >= (g
+1)/2)) {
1130 for (int k
= j
; k
< len
-1; ++k
)
1132 num
[k
] = g
- num
[k
];
1133 num
[len
-1] = g
- 1 - num
[len
-1];
1134 value_assign(tmp
.d
, m
);
1136 zz2value(t
, tmp
.x
.n
);
1142 ZZ t
= num
[len
-1] * f
;
1143 zz2value(t
, tmp
.x
.n
);
1144 value_assign(tmp
.d
, m
);
1147 evalue
*E
= multi_monom(num
);
1151 if (PD
&& !mod_needed(PD
, num
, m
, E
)) {
1153 zz2value(f
, EV
.x
.n
);
1154 value_assign(EV
.d
, m
);
1159 value_set_si(EV
.x
.n
, 1);
1160 value_assign(EV
.d
, m
);
1162 value_clear(EV
.x
.n
);
1163 value_set_si(EV
.d
, 0);
1164 EV
.x
.p
= new_enode(fractional
, 3, -1);
1165 evalue_copy(&EV
.x
.p
->arr
[0], E
);
1166 evalue_set_si(&EV
.x
.p
->arr
[1], 0, 1);
1167 value_init(EV
.x
.p
->arr
[2].x
.n
);
1168 zz2value(f
, EV
.x
.p
->arr
[2].x
.n
);
1169 value_set_si(EV
.x
.p
->arr
[2].d
, 1);
1174 free_evalue_refs(&EV
);
1175 free_evalue_refs(E
);
1179 free_evalue_refs(&tmp
);
1185 evalue
* bv_ceil3(Value
*coef
, int len
, Value d
, Polyhedron
*P
)
1187 Vector
*val
= Vector_Alloc(len
);
1191 value_set_si(t
, -1);
1192 Vector_Scale(coef
, val
->p
, t
, len
);
1193 value_absolute(t
, d
);
1196 values2zz(val
->p
, num
, len
);
1197 evalue
*EP
= multi_monom(num
);
1201 value_init(tmp
.x
.n
);
1202 value_set_si(tmp
.x
.n
, 1);
1203 value_assign(tmp
.d
, t
);
1209 ceil_mod(val
->p
, len
, t
, one
, EP
, P
);
1212 /* copy EP to malloc'ed evalue */
1218 free_evalue_refs(&tmp
);
1225 evalue
* lattice_point(
1226 Polyhedron
*i
, vec_ZZ
& lambda
, Matrix
*W
, Value lcm
, Polyhedron
*PD
)
1228 unsigned nparam
= W
->NbColumns
- 1;
1230 Matrix
* Rays
= rays2(i
);
1231 Matrix
*T
= Transpose(Rays
);
1232 Matrix
*T2
= Matrix_Copy(T
);
1233 Matrix
*inv
= Matrix_Alloc(T2
->NbRows
, T2
->NbColumns
);
1234 int ok
= Matrix_Inverse(T2
, inv
);
1239 matrix2zz(W
, vertex
, W
->NbRows
, W
->NbColumns
);
1242 num
= lambda
* vertex
;
1244 evalue
*EP
= multi_monom(num
);
1248 value_init(tmp
.x
.n
);
1249 value_set_si(tmp
.x
.n
, 1);
1250 value_assign(tmp
.d
, lcm
);
1254 Matrix
*L
= Matrix_Alloc(inv
->NbRows
, W
->NbColumns
);
1255 Matrix_Product(inv
, W
, L
);
1258 matrix2zz(T
, RT
, T
->NbRows
, T
->NbColumns
);
1261 vec_ZZ p
= lambda
* RT
;
1263 for (int i
= 0; i
< L
->NbRows
; ++i
) {
1264 ceil_mod(L
->p
[i
], nparam
+1, lcm
, p
[i
], EP
, PD
);
1270 free_evalue_refs(&tmp
);
1274 evalue
* lattice_point(
1275 Polyhedron
*i
, vec_ZZ
& lambda
, Matrix
*W
, Value lcm
, Polyhedron
*PD
)
1277 Matrix
*T
= Transpose(W
);
1278 unsigned nparam
= T
->NbRows
- 1;
1280 evalue
*EP
= new evalue();
1282 evalue_set_si(EP
, 0, 1);
1285 Vector
*val
= Vector_Alloc(nparam
+1);
1286 value_set_si(val
->p
[nparam
], 1);
1287 ZZ
offset(INIT_VAL
, 0);
1289 vertex_period(i
, lambda
, T
, lcm
, 0, val
, EP
, &ev
, offset
);
1292 free_evalue_refs(&ev
);
1303 Param_Vertices
* V
, Polyhedron
*i
, vec_ZZ
& lambda
, term_info
* term
,
1306 unsigned nparam
= V
->Vertex
->NbColumns
- 2;
1307 unsigned dim
= i
->Dimension
;
1309 vertex
.SetDims(V
->Vertex
->NbRows
, nparam
+1);
1313 value_set_si(lcm
, 1);
1314 for (int j
= 0; j
< V
->Vertex
->NbRows
; ++j
) {
1315 value_lcm(lcm
, V
->Vertex
->p
[j
][nparam
+1], &lcm
);
1317 if (value_notone_p(lcm
)) {
1318 Matrix
* mv
= Matrix_Alloc(dim
, nparam
+1);
1319 for (int j
= 0 ; j
< dim
; ++j
) {
1320 value_division(tmp
, lcm
, V
->Vertex
->p
[j
][nparam
+1]);
1321 Vector_Scale(V
->Vertex
->p
[j
], mv
->p
[j
], tmp
, nparam
+1);
1324 term
->E
= lattice_point(i
, lambda
, mv
, lcm
, PD
);
1332 for (int i
= 0; i
< V
->Vertex
->NbRows
; ++i
) {
1333 assert(value_one_p(V
->Vertex
->p
[i
][nparam
+1])); // for now
1334 values2zz(V
->Vertex
->p
[i
], vertex
[i
], nparam
+1);
1338 num
= lambda
* vertex
;
1342 for (int j
= 0; j
< nparam
; ++j
)
1348 term
->E
= multi_monom(num
);
1352 term
->constant
= num
[nparam
];
1355 term
->coeff
= num
[p
];
1362 static void normalize(ZZ
& sign
, ZZ
& num
, vec_ZZ
& den
)
1364 unsigned dim
= den
.length();
1368 for (int j
= 0; j
< den
.length(); ++j
) {
1372 den
[j
] = abs(den
[j
]);
1381 * f: the powers in the denominator for the remaining vars
1382 * each row refers to a factor
1383 * den_s: for each factor, the power of (s+1)
1385 * num_s: powers in the numerator corresponding to the summed vars
1386 * num_p: powers in the numerator corresponidng to the remaining vars
1387 * number of rays in cone: "dim" = "k"
1388 * length of each ray: "dim" = "d"
1389 * for now, it is assume: k == d
1391 * den_p: for each factor
1392 * 0: independent of remaining vars
1393 * 1: power corresponds to corresponding row in f
1394 * -1: power is inverse of corresponding row in f
1396 static void normalize(ZZ
& sign
,
1397 ZZ
& num_s
, vec_ZZ
& num_p
, vec_ZZ
& den_s
, vec_ZZ
& den_p
,
1400 unsigned dim
= f
.NumRows();
1401 unsigned nparam
= num_p
.length();
1402 unsigned nvar
= dim
- nparam
;
1406 for (int j
= 0; j
< den_s
.length(); ++j
) {
1407 if (den_s
[j
] == 0) {
1412 for (k
= 0; k
< nparam
; ++k
)
1426 den_s
[j
] = abs(den_s
[j
]);
1435 struct counter
: public polar_decomposer
{
1447 counter(Polyhedron
*P
) {
1450 randomvector(P
, lambda
, dim
);
1451 rays
.SetDims(dim
, dim
);
1456 void start(unsigned MaxRays
);
1462 virtual void handle_polar(Polyhedron
*P
, int sign
);
1465 void counter::handle_polar(Polyhedron
*C
, int s
)
1468 assert(C
->NbRays
-1 == dim
);
1469 add_rays(rays
, C
, &r
);
1470 for (int k
= 0; k
< dim
; ++k
) {
1471 assert(lambda
* rays
[k
] != 0);
1476 lattice_point(P
->Ray
[j
]+1, C
, vertex
);
1477 num
= vertex
* lambda
;
1478 den
= rays
* lambda
;
1479 normalize(sign
, num
, den
);
1482 dpoly
n(dim
, den
[0], 1);
1483 for (int k
= 1; k
< dim
; ++k
) {
1484 dpoly
fact(dim
, den
[k
], 1);
1487 d
.div(n
, count
, sign
);
1490 void counter::start(unsigned MaxRays
)
1492 for (j
= 0; j
< P
->NbRays
; ++j
) {
1493 Polyhedron
*C
= supporting_cone(P
, j
);
1494 decompose(C
, MaxRays
);
1498 struct reducer
: public polar_decomposer
{
1510 int lower
; // call base when only this many variables is left
1511 int untouched
; // keep this many variables untouched
1513 reducer(Polyhedron
*P
) {
1516 //den.SetLength(dim);
1523 void start(unsigned MaxRays
);
1531 virtual void handle_polar(Polyhedron
*P
, int sign
);
1532 void reduce(ZZ c
, ZZ cd
, vec_ZZ
& num
, mat_ZZ
& den_f
);
1533 virtual void base(ZZ
& c
, ZZ
& cd
, vec_ZZ
& num
, mat_ZZ
& den_f
) = 0;
1536 void reducer::reduce(ZZ c
, ZZ cd
, vec_ZZ
& num
, mat_ZZ
& den_f
)
1538 unsigned len
= den_f
.NumRows(); // number of factors in den
1539 unsigned d
= num
.length()-1;
1542 base(c
, cd
, num
, den_f
);
1545 assert(num
.length() > 1);
1548 den_s
.SetLength(len
);
1550 den_r
.SetDims(len
, d
);
1552 /* Since we're working incrementally, we can look
1553 * for the "easiest" parameter first.
1554 * In particular we first handle the parameters such
1555 * that no_param + only_param == len, since that allows
1556 * us to decouple the problem and the split off part
1557 * may very well be zero
1561 for (i
= 0; i
< d
+1-untouched
; ++i
) {
1562 for (r
= 0; r
< len
; ++r
) {
1563 if (den_f
[r
][i
] != 0) {
1564 for (k
= 0; k
<= d
; ++k
)
1565 if (i
!= k
&& den_f
[r
][k
] != 0)
1574 if (i
> d
-untouched
)
1577 for (r
= 0; r
< len
; ++r
) {
1578 den_s
[r
] = den_f
[r
][i
];
1579 for (k
= 0; k
<= d
; ++k
)
1581 den_r
[r
][k
-(k
>i
)] = den_f
[r
][k
];
1587 for (k
= 0 ; k
<= d
; ++k
)
1589 num_p
[k
-(k
>i
)] = num
[k
];
1592 den_p
.SetLength(len
);
1594 normalize(c
, num_s
, num_p
, den_s
, den_p
, den_r
);
1598 for (int k
= 0; k
< len
; ++k
) {
1601 else if (den_s
[k
] == 0)
1604 if (no_param
== 0) {
1605 for (int k
= 0; k
< len
; ++k
)
1607 den_r
[k
] = -den_r
[k
];
1608 reduce(c
, cd
, num_p
, den_r
);
1612 pden
.SetDims(only_param
, d
);
1614 for (k
= 0, l
= 0; k
< len
; ++k
)
1616 pden
[l
++] = den_r
[k
];
1618 for (k
= 0; k
< len
; ++k
)
1622 dpoly
n(no_param
, num_s
);
1623 dpoly
D(no_param
, den_s
[k
], 1);
1624 for ( ; ++k
< len
; )
1625 if (den_p
[k
] == 0) {
1626 dpoly
fact(no_param
, den_s
[k
], 1);
1630 if (no_param
+ only_param
== len
) {
1631 mpq_set_si(tcount
, 0, 1);
1632 n
.div(D
, tcount
, one
);
1635 value2zz(mpq_numref(tcount
), qn
);
1636 value2zz(mpq_denref(tcount
), qd
);
1642 reduce(qn
, qd
, num_p
, pden
);
1646 for (k
= 0; k
< len
; ++k
) {
1647 if (den_s
[k
] == 0 || den_p
[k
] == 0)
1650 dpoly
pd(no_param
-1, den_s
[k
], 1);
1651 int s
= den_p
[k
] < 0 ? -1 : 1;
1654 r
= new dpoly_r(n
, pd
, k
, s
, len
);
1656 dpoly_r
*nr
= new dpoly_r(r
, pd
, k
, s
, len
);
1662 dpoly_r
*rc
= r
->div(D
);
1666 int common
= pden
.NumRows();
1667 vector
< dpoly_r_term
* >& final
= rc
->c
[rc
->len
-1];
1669 for (int j
= 0; j
< final
.size(); ++j
) {
1670 if (final
[j
]->coeff
== 0)
1673 pden
.SetDims(rows
, pden
.NumCols());
1674 for (int k
= 0; k
< rc
->dim
; ++k
) {
1675 int n
= final
[j
]->powers
[k
];
1678 int abs_n
= n
< 0 ? -n
: n
;
1679 pden
.SetDims(rows
+abs_n
, pden
.NumCols());
1680 for (int l
= 0; l
< abs_n
; ++l
) {
1682 pden
[rows
+l
] = den_r
[k
];
1684 pden
[rows
+l
] = -den_r
[k
];
1688 final
[j
]->coeff
*= c
;
1689 reduce(final
[j
]->coeff
, rc
->denom
, num_p
, pden
);
1698 void reducer::handle_polar(Polyhedron
*C
, int s
)
1700 assert(C
->NbRays
-1 == dim
);
1704 lattice_point(P
->Ray
[j
]+1, C
, vertex
);
1707 den
.SetDims(dim
, dim
);
1710 for (r
= 0; r
< dim
; ++r
)
1711 values2zz(C
->Ray
[r
]+1, den
[r
], dim
);
1713 reduce(sgn
, one
, vertex
, den
);
1716 void reducer::start(unsigned MaxRays
)
1718 for (j
= 0; j
< P
->NbRays
; ++j
) {
1719 Polyhedron
*C
= supporting_cone(P
, j
);
1720 decompose(C
, MaxRays
);
1724 // incremental counter
1725 struct icounter
: public reducer
{
1728 icounter(Polyhedron
*P
) : reducer(P
) {
1736 virtual void base(ZZ
& c
, ZZ
& cd
, vec_ZZ
& num
, mat_ZZ
& den_f
);
1739 void icounter::base(ZZ
& c
, ZZ
& cd
, vec_ZZ
& num
, mat_ZZ
& den_f
)
1742 unsigned len
= den_f
.NumRows(); // number of factors in den
1744 den_s
.SetLength(len
);
1746 for (r
= 0; r
< len
; ++r
)
1747 den_s
[r
] = den_f
[r
][0];
1748 normalize(c
, num_s
, den_s
);
1750 dpoly
n(len
, num_s
);
1751 dpoly
D(len
, den_s
[0], 1);
1752 for (int k
= 1; k
< len
; ++k
) {
1753 dpoly
fact(len
, den_s
[k
], 1);
1756 mpq_set_si(tcount
, 0, 1);
1757 n
.div(D
, tcount
, one
);
1760 mpz_mul(mpq_numref(tcount
), mpq_numref(tcount
), tn
);
1761 mpz_mul(mpq_denref(tcount
), mpq_denref(tcount
), td
);
1762 mpq_canonicalize(tcount
);
1763 mpq_add(count
, count
, tcount
);
1766 struct partial_reducer
: public reducer
{
1769 partial_reducer(Polyhedron
*P
, unsigned nparam
) : reducer(P
) {
1774 ~partial_reducer() {
1776 virtual void base(ZZ
& c
, ZZ
& cd
, vec_ZZ
& num
, mat_ZZ
& den_f
);
1777 void start(unsigned MaxRays
);
1780 void partial_reducer::base(ZZ
& c
, ZZ
& cd
, vec_ZZ
& num
, mat_ZZ
& den_f
)
1782 gf
->add(c
, cd
, num
, den_f
);
1785 void partial_reducer::start(unsigned MaxRays
)
1787 for (j
= 0; j
< P
->NbRays
; ++j
) {
1788 if (!value_pos_p(P
->Ray
[j
][dim
+1]))
1791 Polyhedron
*C
= supporting_cone(P
, j
);
1792 decompose(C
, MaxRays
);
1796 typedef Polyhedron
* Polyhedron_p
;
1798 void barvinok_count(Polyhedron
*P
, Value
* result
, unsigned NbMaxCons
)
1800 Polyhedron
** vcone
;
1809 value_set_si(*result
, 0);
1813 for (; r
< P
->NbRays
; ++r
)
1814 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1]))
1816 if (P
->NbBid
!=0 || r
< P
->NbRays
) {
1817 value_set_si(*result
, -1);
1821 P
= remove_equalities(P
);
1824 value_set_si(*result
, 0);
1830 value_set_si(factor
, 1);
1831 Q
= Polyhedron_Reduce(P
, &factor
);
1838 if (P
->Dimension
== 0) {
1839 value_assign(*result
, factor
);
1842 value_clear(factor
);
1847 cnt
.start(NbMaxCons
);
1849 assert(value_one_p(&cnt
.count
[0]._mp_den
));
1850 value_multiply(*result
, &cnt
.count
[0]._mp_num
, factor
);
1854 value_clear(factor
);
1857 static void uni_polynom(int param
, Vector
*c
, evalue
*EP
)
1859 unsigned dim
= c
->Size
-2;
1861 value_set_si(EP
->d
,0);
1862 EP
->x
.p
= new_enode(polynomial
, dim
+1, param
+1);
1863 for (int j
= 0; j
<= dim
; ++j
)
1864 evalue_set(&EP
->x
.p
->arr
[j
], c
->p
[j
], c
->p
[dim
+1]);
1867 static void multi_polynom(Vector
*c
, evalue
* X
, evalue
*EP
)
1869 unsigned dim
= c
->Size
-2;
1873 evalue_set(&EC
, c
->p
[dim
], c
->p
[dim
+1]);
1876 evalue_set(EP
, c
->p
[dim
], c
->p
[dim
+1]);
1878 for (int i
= dim
-1; i
>= 0; --i
) {
1880 value_assign(EC
.x
.n
, c
->p
[i
]);
1883 free_evalue_refs(&EC
);
1886 Polyhedron
*unfringe (Polyhedron
*P
, unsigned MaxRays
)
1888 int len
= P
->Dimension
+2;
1889 Polyhedron
*T
, *R
= P
;
1892 Vector
*row
= Vector_Alloc(len
);
1893 value_set_si(row
->p
[0], 1);
1895 R
= DomainConstraintSimplify(Polyhedron_Copy(P
), MaxRays
);
1897 Matrix
*M
= Matrix_Alloc(2, len
-1);
1898 value_set_si(M
->p
[1][len
-2], 1);
1899 for (int v
= 0; v
< P
->Dimension
; ++v
) {
1900 value_set_si(M
->p
[0][v
], 1);
1901 Polyhedron
*I
= Polyhedron_Image(P
, M
, 2+1);
1902 value_set_si(M
->p
[0][v
], 0);
1903 for (int r
= 0; r
< I
->NbConstraints
; ++r
) {
1904 if (value_zero_p(I
->Constraint
[r
][0]))
1906 if (value_zero_p(I
->Constraint
[r
][1]))
1908 if (value_one_p(I
->Constraint
[r
][1]))
1910 if (value_mone_p(I
->Constraint
[r
][1]))
1912 value_absolute(g
, I
->Constraint
[r
][1]);
1913 Vector_Set(row
->p
+1, 0, len
-2);
1914 value_division(row
->p
[1+v
], I
->Constraint
[r
][1], g
);
1915 mpz_fdiv_q(row
->p
[len
-1], I
->Constraint
[r
][2], g
);
1917 R
= AddConstraints(row
->p
, 1, R
, MaxRays
);
1929 static Polyhedron
*reduce_domain(Polyhedron
*D
, Matrix
*CT
, Polyhedron
*CEq
,
1930 Polyhedron
**fVD
, int nd
, unsigned MaxRays
)
1935 Dt
= CT
? DomainPreimage(D
, CT
, MaxRays
) : D
;
1936 Polyhedron
*rVD
= DomainIntersection(Dt
, CEq
, MaxRays
);
1938 /* if rVD is empty or too small in geometric dimension */
1939 if(!rVD
|| emptyQ(rVD
) ||
1940 (rVD
->Dimension
-rVD
->NbEq
< Dt
->Dimension
-Dt
->NbEq
-CEq
->NbEq
)) {
1945 return 0; /* empty validity domain */
1951 fVD
[nd
] = Domain_Copy(rVD
);
1952 for (int i
= 0 ; i
< nd
; ++i
) {
1953 Polyhedron
*I
= DomainIntersection(fVD
[nd
], fVD
[i
], MaxRays
);
1958 Polyhedron
*F
= DomainSimplify(I
, fVD
[nd
], MaxRays
);
1960 Polyhedron
*T
= rVD
;
1961 rVD
= DomainDifference(rVD
, F
, MaxRays
);
1968 rVD
= DomainConstraintSimplify(rVD
, MaxRays
);
1970 Domain_Free(fVD
[nd
]);
1977 barvinok_count(rVD
, &c
, MaxRays
);
1978 if (value_zero_p(c
)) {
1987 static bool Polyhedron_is_infinite(Polyhedron
*P
, unsigned nparam
)
1990 for (r
= 0; r
< P
->NbRays
; ++r
)
1991 if (value_zero_p(P
->Ray
[r
][0]) ||
1992 value_zero_p(P
->Ray
[r
][P
->Dimension
+1])) {
1994 for (i
= P
->Dimension
- nparam
; i
< P
->Dimension
; ++i
)
1995 if (value_notzero_p(P
->Ray
[r
][i
+1]))
1997 if (i
>= P
->Dimension
)
2000 return r
< P
->NbRays
;
2003 /* Check whether all rays point in the positive directions
2004 * for the parameters
2006 static bool Polyhedron_has_positive_rays(Polyhedron
*P
, unsigned nparam
)
2009 for (r
= 0; r
< P
->NbRays
; ++r
)
2010 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1])) {
2012 for (i
= P
->Dimension
- nparam
; i
< P
->Dimension
; ++i
)
2013 if (value_neg_p(P
->Ray
[r
][i
+1]))
2019 typedef evalue
* evalue_p
;
2021 struct enumerator
: public polar_decomposer
{
2035 enumerator(Polyhedron
*P
, unsigned dim
, unsigned nbV
) {
2039 randomvector(P
, lambda
, dim
);
2040 rays
.SetDims(dim
, dim
);
2042 c
= Vector_Alloc(dim
+2);
2044 vE
= new evalue_p
[nbV
];
2045 for (int j
= 0; j
< nbV
; ++j
)
2051 void decompose_at(Param_Vertices
*V
, int _i
, unsigned MaxRays
) {
2052 Polyhedron
*C
= supporting_cone_p(P
, V
);
2056 vE
[_i
] = new evalue
;
2057 value_init(vE
[_i
]->d
);
2058 evalue_set_si(vE
[_i
], 0, 1);
2060 decompose(C
, MaxRays
);
2067 for (int j
= 0; j
< nbV
; ++j
)
2069 free_evalue_refs(vE
[j
]);
2075 virtual void handle_polar(Polyhedron
*P
, int sign
);
2078 void enumerator::handle_polar(Polyhedron
*C
, int s
)
2081 assert(C
->NbRays
-1 == dim
);
2082 add_rays(rays
, C
, &r
);
2083 for (int k
= 0; k
< dim
; ++k
) {
2084 assert(lambda
* rays
[k
] != 0);
2089 lattice_point(V
, C
, lambda
, &num
, 0);
2090 den
= rays
* lambda
;
2091 normalize(sign
, num
.constant
, den
);
2093 dpoly
n(dim
, den
[0], 1);
2094 for (int k
= 1; k
< dim
; ++k
) {
2095 dpoly
fact(dim
, den
[k
], 1);
2098 if (num
.E
!= NULL
) {
2099 ZZ
one(INIT_VAL
, 1);
2100 dpoly_n
d(dim
, num
.constant
, one
);
2103 multi_polynom(c
, num
.E
, &EV
);
2105 free_evalue_refs(&EV
);
2106 free_evalue_refs(num
.E
);
2108 } else if (num
.pos
!= -1) {
2109 dpoly_n
d(dim
, num
.constant
, num
.coeff
);
2112 uni_polynom(num
.pos
, c
, &EV
);
2114 free_evalue_refs(&EV
);
2116 mpq_set_si(count
, 0, 1);
2117 dpoly
d(dim
, num
.constant
);
2118 d
.div(n
, count
, sign
);
2121 evalue_set(&EV
, &count
[0]._mp_num
, &count
[0]._mp_den
);
2123 free_evalue_refs(&EV
);
2127 evalue
* barvinok_enumerate_ev(Polyhedron
*P
, Polyhedron
* C
, unsigned MaxRays
)
2129 //P = unfringe(P, MaxRays);
2130 Polyhedron
*CEq
= NULL
, *rVD
, *pVD
, *CA
;
2132 Param_Polyhedron
*PP
= NULL
;
2133 Param_Domain
*D
, *next
;
2136 unsigned nparam
= C
->Dimension
;
2138 ALLOC(evalue
, eres
);
2139 value_init(eres
->d
);
2140 value_set_si(eres
->d
, 0);
2143 value_init(factor
.d
);
2144 evalue_set_si(&factor
, 1, 1);
2146 CA
= align_context(C
, P
->Dimension
, MaxRays
);
2147 P
= DomainIntersection(P
, CA
, MaxRays
);
2148 Polyhedron_Free(CA
);
2150 if (C
->Dimension
== 0 || emptyQ(P
)) {
2152 eres
->x
.p
= new_enode(partition
, 2, C
->Dimension
);
2153 EVALUE_SET_DOMAIN(eres
->x
.p
->arr
[0],
2154 DomainConstraintSimplify(CEq
? CEq
: Polyhedron_Copy(C
), MaxRays
));
2155 value_set_si(eres
->x
.p
->arr
[1].d
, 1);
2156 value_init(eres
->x
.p
->arr
[1].x
.n
);
2158 value_set_si(eres
->x
.p
->arr
[1].x
.n
, 0);
2160 barvinok_count(P
, &eres
->x
.p
->arr
[1].x
.n
, MaxRays
);
2162 emul(&factor
, eres
);
2163 reduce_evalue(eres
);
2164 free_evalue_refs(&factor
);
2169 Param_Polyhedron_Free(PP
);
2173 if (Polyhedron_is_infinite(P
, nparam
))
2178 P
= remove_equalities_p(P
, P
->Dimension
-nparam
, &f
);
2182 if (P
->Dimension
== nparam
) {
2184 P
= Universe_Polyhedron(0);
2188 Polyhedron
*Q
= ParamPolyhedron_Reduce(P
, P
->Dimension
-nparam
, &factor
);
2191 if (Q
->Dimension
== nparam
) {
2193 P
= Universe_Polyhedron(0);
2198 Polyhedron
*oldP
= P
;
2199 PP
= Polyhedron2Param_SimplifiedDomain(&P
,C
,MaxRays
,&CEq
,&CT
);
2201 Polyhedron_Free(oldP
);
2203 if (isIdentity(CT
)) {
2207 assert(CT
->NbRows
!= CT
->NbColumns
);
2208 if (CT
->NbRows
== 1) // no more parameters
2210 nparam
= CT
->NbRows
- 1;
2213 unsigned dim
= P
->Dimension
- nparam
;
2215 enumerator
et(P
, dim
, PP
->nbV
);
2218 for (nd
= 0, D
=PP
->D
; D
; ++nd
, D
=D
->next
);
2219 struct section
{ Polyhedron
*D
; evalue E
; };
2220 section
*s
= new section
[nd
];
2221 Polyhedron
**fVD
= new Polyhedron_p
[nd
];
2223 for(nd
= 0, D
=PP
->D
; D
; D
=next
) {
2226 Polyhedron
*rVD
= reduce_domain(D
->Domain
, CT
, CEq
,
2231 pVD
= CT
? DomainImage(rVD
,CT
,MaxRays
) : rVD
;
2233 value_init(s
[nd
].E
.d
);
2234 evalue_set_si(&s
[nd
].E
, 0, 1);
2236 FORALL_PVertex_in_ParamPolyhedron(V
,D
,PP
) // _i is internal counter
2238 et
.decompose_at(V
, _i
, MaxRays
);
2239 eadd(et
.vE
[_i
] , &s
[nd
].E
);
2240 END_FORALL_PVertex_in_ParamPolyhedron
;
2241 reduce_in_domain(&s
[nd
].E
, pVD
);
2244 addeliminatedparams_evalue(&s
[nd
].E
, CT
);
2252 evalue_set_si(eres
, 0, 1);
2254 eres
->x
.p
= new_enode(partition
, 2*nd
, C
->Dimension
);
2255 for (int j
= 0; j
< nd
; ++j
) {
2256 EVALUE_SET_DOMAIN(eres
->x
.p
->arr
[2*j
], s
[j
].D
);
2257 value_clear(eres
->x
.p
->arr
[2*j
+1].d
);
2258 eres
->x
.p
->arr
[2*j
+1] = s
[j
].E
;
2259 Domain_Free(fVD
[j
]);
2267 Polyhedron_Free(CEq
);
2272 Enumeration
* barvinok_enumerate(Polyhedron
*P
, Polyhedron
* C
, unsigned MaxRays
)
2274 evalue
*EP
= barvinok_enumerate_ev(P
, C
, MaxRays
);
2276 return partition2enumeration(EP
);
2279 static void SwapColumns(Value
**V
, int n
, int i
, int j
)
2281 for (int r
= 0; r
< n
; ++r
)
2282 value_swap(V
[r
][i
], V
[r
][j
]);
2285 static void SwapColumns(Polyhedron
*P
, int i
, int j
)
2287 SwapColumns(P
->Constraint
, P
->NbConstraints
, i
, j
);
2288 SwapColumns(P
->Ray
, P
->NbRays
, i
, j
);
2291 static void negative_test_constraint(Value
*l
, Value
*u
, Value
*c
, int pos
,
2294 value_oppose(*v
, u
[pos
+1]);
2295 Vector_Combine(l
+1, u
+1, c
+1, *v
, l
[pos
+1], len
-1);
2296 value_multiply(*v
, *v
, l
[pos
+1]);
2297 value_substract(c
[len
-1], c
[len
-1], *v
);
2298 value_set_si(*v
, -1);
2299 Vector_Scale(c
+1, c
+1, *v
, len
-1);
2300 value_decrement(c
[len
-1], c
[len
-1]);
2301 ConstraintSimplify(c
, c
, len
, v
);
2304 static bool parallel_constraints(Value
*l
, Value
*u
, Value
*c
, int pos
,
2313 Vector_Gcd(&l
[1+pos
], len
, &g1
);
2314 Vector_Gcd(&u
[1+pos
], len
, &g2
);
2315 Vector_Combine(l
+1+pos
, u
+1+pos
, c
+1, g2
, g1
, len
);
2316 parallel
= First_Non_Zero(c
+1, len
) == -1;
2324 static void negative_test_constraint7(Value
*l
, Value
*u
, Value
*c
, int pos
,
2325 int exist
, int len
, Value
*v
)
2330 Vector_Gcd(&u
[1+pos
], exist
, v
);
2331 Vector_Gcd(&l
[1+pos
], exist
, &g
);
2332 Vector_Combine(l
+1, u
+1, c
+1, *v
, g
, len
-1);
2333 value_multiply(*v
, *v
, g
);
2334 value_substract(c
[len
-1], c
[len
-1], *v
);
2335 value_set_si(*v
, -1);
2336 Vector_Scale(c
+1, c
+1, *v
, len
-1);
2337 value_decrement(c
[len
-1], c
[len
-1]);
2338 ConstraintSimplify(c
, c
, len
, v
);
2343 static void oppose_constraint(Value
*c
, int len
, Value
*v
)
2345 value_set_si(*v
, -1);
2346 Vector_Scale(c
+1, c
+1, *v
, len
-1);
2347 value_decrement(c
[len
-1], c
[len
-1]);
2350 static bool SplitOnConstraint(Polyhedron
*P
, int i
, int l
, int u
,
2351 int nvar
, int len
, int exist
, int MaxRays
,
2352 Vector
*row
, Value
& f
, bool independent
,
2353 Polyhedron
**pos
, Polyhedron
**neg
)
2355 negative_test_constraint(P
->Constraint
[l
], P
->Constraint
[u
],
2356 row
->p
, nvar
+i
, len
, &f
);
2357 *neg
= AddConstraints(row
->p
, 1, P
, MaxRays
);
2359 /* We found an independent, but useless constraint
2360 * Maybe we should detect this earlier and not
2361 * mark the variable as INDEPENDENT
2363 if (emptyQ((*neg
))) {
2364 Polyhedron_Free(*neg
);
2368 oppose_constraint(row
->p
, len
, &f
);
2369 *pos
= AddConstraints(row
->p
, 1, P
, MaxRays
);
2371 if (emptyQ((*pos
))) {
2372 Polyhedron_Free(*neg
);
2373 Polyhedron_Free(*pos
);
2381 * unimodularly transform P such that constraint r is transformed
2382 * into a constraint that involves only a single (the first)
2383 * existential variable
2386 static Polyhedron
*rotate_along(Polyhedron
*P
, int r
, int nvar
, int exist
,
2392 Vector
*row
= Vector_Alloc(exist
);
2393 Vector_Copy(P
->Constraint
[r
]+1+nvar
, row
->p
, exist
);
2394 Vector_Gcd(row
->p
, exist
, &g
);
2395 if (value_notone_p(g
))
2396 Vector_AntiScale(row
->p
, row
->p
, g
, exist
);
2399 Matrix
*M
= unimodular_complete(row
);
2400 Matrix
*M2
= Matrix_Alloc(P
->Dimension
+1, P
->Dimension
+1);
2401 for (r
= 0; r
< nvar
; ++r
)
2402 value_set_si(M2
->p
[r
][r
], 1);
2403 for ( ; r
< nvar
+exist
; ++r
)
2404 Vector_Copy(M
->p
[r
-nvar
], M2
->p
[r
]+nvar
, exist
);
2405 for ( ; r
< P
->Dimension
+1; ++r
)
2406 value_set_si(M2
->p
[r
][r
], 1);
2407 Polyhedron
*T
= Polyhedron_Image(P
, M2
, MaxRays
);
2416 static bool SplitOnVar(Polyhedron
*P
, int i
,
2417 int nvar
, int len
, int exist
, int MaxRays
,
2418 Vector
*row
, Value
& f
, bool independent
,
2419 Polyhedron
**pos
, Polyhedron
**neg
)
2423 for (int l
= P
->NbEq
; l
< P
->NbConstraints
; ++l
) {
2424 if (value_negz_p(P
->Constraint
[l
][nvar
+i
+1]))
2428 for (j
= 0; j
< exist
; ++j
)
2429 if (j
!= i
&& value_notzero_p(P
->Constraint
[l
][nvar
+j
+1]))
2435 for (int u
= P
->NbEq
; u
< P
->NbConstraints
; ++u
) {
2436 if (value_posz_p(P
->Constraint
[u
][nvar
+i
+1]))
2440 for (j
= 0; j
< exist
; ++j
)
2441 if (j
!= i
&& value_notzero_p(P
->Constraint
[u
][nvar
+j
+1]))
2447 if (SplitOnConstraint(P
, i
, l
, u
,
2448 nvar
, len
, exist
, MaxRays
,
2449 row
, f
, independent
,
2453 SwapColumns(*neg
, nvar
+1, nvar
+1+i
);
2463 static bool double_bound_pair(Polyhedron
*P
, int nvar
, int exist
,
2464 int i
, int l1
, int l2
,
2465 Polyhedron
**pos
, Polyhedron
**neg
)
2469 Vector
*row
= Vector_Alloc(P
->Dimension
+2);
2470 value_set_si(row
->p
[0], 1);
2471 value_oppose(f
, P
->Constraint
[l1
][nvar
+i
+1]);
2472 Vector_Combine(P
->Constraint
[l1
]+1, P
->Constraint
[l2
]+1,
2474 P
->Constraint
[l2
][nvar
+i
+1], f
,
2476 ConstraintSimplify(row
->p
, row
->p
, P
->Dimension
+2, &f
);
2477 *pos
= AddConstraints(row
->p
, 1, P
, 0);
2478 value_set_si(f
, -1);
2479 Vector_Scale(row
->p
+1, row
->p
+1, f
, P
->Dimension
+1);
2480 value_decrement(row
->p
[P
->Dimension
+1], row
->p
[P
->Dimension
+1]);
2481 *neg
= AddConstraints(row
->p
, 1, P
, 0);
2485 return !emptyQ((*pos
)) && !emptyQ((*neg
));
2488 static bool double_bound(Polyhedron
*P
, int nvar
, int exist
,
2489 Polyhedron
**pos
, Polyhedron
**neg
)
2491 for (int i
= 0; i
< exist
; ++i
) {
2493 for (l1
= P
->NbEq
; l1
< P
->NbConstraints
; ++l1
) {
2494 if (value_negz_p(P
->Constraint
[l1
][nvar
+i
+1]))
2496 for (l2
= l1
+ 1; l2
< P
->NbConstraints
; ++l2
) {
2497 if (value_negz_p(P
->Constraint
[l2
][nvar
+i
+1]))
2499 if (double_bound_pair(P
, nvar
, exist
, i
, l1
, l2
, pos
, neg
))
2503 for (l1
= P
->NbEq
; l1
< P
->NbConstraints
; ++l1
) {
2504 if (value_posz_p(P
->Constraint
[l1
][nvar
+i
+1]))
2506 if (l1
< P
->NbConstraints
)
2507 for (l2
= l1
+ 1; l2
< P
->NbConstraints
; ++l2
) {
2508 if (value_posz_p(P
->Constraint
[l2
][nvar
+i
+1]))
2510 if (double_bound_pair(P
, nvar
, exist
, i
, l1
, l2
, pos
, neg
))
2522 INDEPENDENT
= 1 << 2,
2526 static evalue
* enumerate_or(Polyhedron
*D
,
2527 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
2530 fprintf(stderr
, "\nER: Or\n");
2531 #endif /* DEBUG_ER */
2533 Polyhedron
*N
= D
->next
;
2536 barvinok_enumerate_e(D
, exist
, nparam
, MaxRays
);
2539 for (D
= N
; D
; D
= N
) {
2544 barvinok_enumerate_e(D
, exist
, nparam
, MaxRays
);
2547 free_evalue_refs(EN
);
2557 static evalue
* enumerate_sum(Polyhedron
*P
,
2558 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
2560 int nvar
= P
->Dimension
- exist
- nparam
;
2561 int toswap
= nvar
< exist
? nvar
: exist
;
2562 for (int i
= 0; i
< toswap
; ++i
)
2563 SwapColumns(P
, 1 + i
, nvar
+exist
- i
);
2567 fprintf(stderr
, "\nER: Sum\n");
2568 #endif /* DEBUG_ER */
2570 evalue
*EP
= barvinok_enumerate_e(P
, exist
, nparam
, MaxRays
);
2572 for (int i
= 0; i
< /* nvar */ nparam
; ++i
) {
2573 Matrix
*C
= Matrix_Alloc(1, 1 + nparam
+ 1);
2574 value_set_si(C
->p
[0][0], 1);
2576 value_init(split
.d
);
2577 value_set_si(split
.d
, 0);
2578 split
.x
.p
= new_enode(partition
, 4, nparam
);
2579 value_set_si(C
->p
[0][1+i
], 1);
2580 Matrix
*C2
= Matrix_Copy(C
);
2581 EVALUE_SET_DOMAIN(split
.x
.p
->arr
[0],
2582 Constraints2Polyhedron(C2
, MaxRays
));
2584 evalue_set_si(&split
.x
.p
->arr
[1], 1, 1);
2585 value_set_si(C
->p
[0][1+i
], -1);
2586 value_set_si(C
->p
[0][1+nparam
], -1);
2587 EVALUE_SET_DOMAIN(split
.x
.p
->arr
[2],
2588 Constraints2Polyhedron(C
, MaxRays
));
2589 evalue_set_si(&split
.x
.p
->arr
[3], 1, 1);
2591 free_evalue_refs(&split
);
2595 evalue_range_reduction(EP
);
2597 evalue_frac2floor(EP
);
2599 evalue
*sum
= esum(EP
, nvar
);
2601 free_evalue_refs(EP
);
2605 evalue_range_reduction(EP
);
2610 static evalue
* split_sure(Polyhedron
*P
, Polyhedron
*S
,
2611 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
2613 int nvar
= P
->Dimension
- exist
- nparam
;
2615 Matrix
*M
= Matrix_Alloc(exist
, S
->Dimension
+2);
2616 for (int i
= 0; i
< exist
; ++i
)
2617 value_set_si(M
->p
[i
][nvar
+i
+1], 1);
2619 S
= DomainAddRays(S
, M
, MaxRays
);
2621 Polyhedron
*F
= DomainAddRays(P
, M
, MaxRays
);
2622 Polyhedron
*D
= DomainDifference(F
, S
, MaxRays
);
2624 D
= Disjoint_Domain(D
, 0, MaxRays
);
2629 M
= Matrix_Alloc(P
->Dimension
+1-exist
, P
->Dimension
+1);
2630 for (int j
= 0; j
< nvar
; ++j
)
2631 value_set_si(M
->p
[j
][j
], 1);
2632 for (int j
= 0; j
< nparam
+1; ++j
)
2633 value_set_si(M
->p
[nvar
+j
][nvar
+exist
+j
], 1);
2634 Polyhedron
*T
= Polyhedron_Image(S
, M
, MaxRays
);
2635 evalue
*EP
= barvinok_enumerate_e(T
, 0, nparam
, MaxRays
);
2640 for (Polyhedron
*Q
= D
; Q
; Q
= Q
->next
) {
2641 Polyhedron
*N
= Q
->next
;
2643 T
= DomainIntersection(P
, Q
, MaxRays
);
2644 evalue
*E
= barvinok_enumerate_e(T
, exist
, nparam
, MaxRays
);
2646 free_evalue_refs(E
);
2655 static evalue
* enumerate_sure(Polyhedron
*P
,
2656 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
2660 int nvar
= P
->Dimension
- exist
- nparam
;
2666 for (i
= 0; i
< exist
; ++i
) {
2667 Matrix
*M
= Matrix_Alloc(S
->NbConstraints
, S
->Dimension
+2);
2669 value_set_si(lcm
, 1);
2670 for (int j
= 0; j
< S
->NbConstraints
; ++j
) {
2671 if (value_negz_p(S
->Constraint
[j
][1+nvar
+i
]))
2673 if (value_one_p(S
->Constraint
[j
][1+nvar
+i
]))
2675 value_lcm(lcm
, S
->Constraint
[j
][1+nvar
+i
], &lcm
);
2678 for (int j
= 0; j
< S
->NbConstraints
; ++j
) {
2679 if (value_negz_p(S
->Constraint
[j
][1+nvar
+i
]))
2681 if (value_one_p(S
->Constraint
[j
][1+nvar
+i
]))
2683 value_division(f
, lcm
, S
->Constraint
[j
][1+nvar
+i
]);
2684 Vector_Scale(S
->Constraint
[j
], M
->p
[c
], f
, S
->Dimension
+2);
2685 value_substract(M
->p
[c
][S
->Dimension
+1],
2686 M
->p
[c
][S
->Dimension
+1],
2688 value_increment(M
->p
[c
][S
->Dimension
+1],
2689 M
->p
[c
][S
->Dimension
+1]);
2693 S
= AddConstraints(M
->p
[0], c
, S
, MaxRays
);
2708 fprintf(stderr
, "\nER: Sure\n");
2709 #endif /* DEBUG_ER */
2711 return split_sure(P
, S
, exist
, nparam
, MaxRays
);
2714 static evalue
* enumerate_sure2(Polyhedron
*P
,
2715 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
2717 int nvar
= P
->Dimension
- exist
- nparam
;
2719 for (r
= 0; r
< P
->NbRays
; ++r
)
2720 if (value_one_p(P
->Ray
[r
][0]) &&
2721 value_one_p(P
->Ray
[r
][P
->Dimension
+1]))
2727 Matrix
*M
= Matrix_Alloc(nvar
+ 1 + nparam
, P
->Dimension
+2);
2728 for (int i
= 0; i
< nvar
; ++i
)
2729 value_set_si(M
->p
[i
][1+i
], 1);
2730 for (int i
= 0; i
< nparam
; ++i
)
2731 value_set_si(M
->p
[i
+nvar
][1+nvar
+exist
+i
], 1);
2732 Vector_Copy(P
->Ray
[r
]+1+nvar
, M
->p
[nvar
+nparam
]+1+nvar
, exist
);
2733 value_set_si(M
->p
[nvar
+nparam
][0], 1);
2734 value_set_si(M
->p
[nvar
+nparam
][P
->Dimension
+1], 1);
2735 Polyhedron
* F
= Rays2Polyhedron(M
, MaxRays
);
2738 Polyhedron
*I
= DomainIntersection(F
, P
, MaxRays
);
2742 fprintf(stderr
, "\nER: Sure2\n");
2743 #endif /* DEBUG_ER */
2745 return split_sure(P
, I
, exist
, nparam
, MaxRays
);
2748 static evalue
* enumerate_cyclic(Polyhedron
*P
,
2749 unsigned exist
, unsigned nparam
,
2750 evalue
* EP
, int r
, int p
, unsigned MaxRays
)
2752 int nvar
= P
->Dimension
- exist
- nparam
;
2754 /* If EP in its fractional maps only contains references
2755 * to the remainder parameter with appropriate coefficients
2756 * then we could in principle avoid adding existentially
2757 * quantified variables to the validity domains.
2758 * We'd have to replace the remainder by m { p/m }
2759 * and multiply with an appropriate factor that is one
2760 * only in the appropriate range.
2761 * This last multiplication can be avoided if EP
2762 * has a single validity domain with no (further)
2763 * constraints on the remainder parameter
2766 Matrix
*CT
= Matrix_Alloc(nparam
+1, nparam
+3);
2767 Matrix
*M
= Matrix_Alloc(1, 1+nparam
+3);
2768 for (int j
= 0; j
< nparam
; ++j
)
2770 value_set_si(CT
->p
[j
][j
], 1);
2771 value_set_si(CT
->p
[p
][nparam
+1], 1);
2772 value_set_si(CT
->p
[nparam
][nparam
+2], 1);
2773 value_set_si(M
->p
[0][1+p
], -1);
2774 value_absolute(M
->p
[0][1+nparam
], P
->Ray
[0][1+nvar
+exist
+p
]);
2775 value_set_si(M
->p
[0][1+nparam
+1], 1);
2776 Polyhedron
*CEq
= Constraints2Polyhedron(M
, 1);
2778 addeliminatedparams_enum(EP
, CT
, CEq
, MaxRays
, nparam
);
2779 Polyhedron_Free(CEq
);
2785 static void enumerate_vd_add_ray(evalue
*EP
, Matrix
*Rays
, unsigned MaxRays
)
2787 if (value_notzero_p(EP
->d
))
2790 assert(EP
->x
.p
->type
== partition
);
2791 assert(EP
->x
.p
->pos
== EVALUE_DOMAIN(EP
->x
.p
->arr
[0])->Dimension
);
2792 for (int i
= 0; i
< EP
->x
.p
->size
/2; ++i
) {
2793 Polyhedron
*D
= EVALUE_DOMAIN(EP
->x
.p
->arr
[2*i
]);
2794 Polyhedron
*N
= DomainAddRays(D
, Rays
, MaxRays
);
2795 EVALUE_SET_DOMAIN(EP
->x
.p
->arr
[2*i
], N
);
2800 static evalue
* enumerate_line(Polyhedron
*P
,
2801 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
2807 fprintf(stderr
, "\nER: Line\n");
2808 #endif /* DEBUG_ER */
2810 int nvar
= P
->Dimension
- exist
- nparam
;
2812 for (i
= 0; i
< nparam
; ++i
)
2813 if (value_notzero_p(P
->Ray
[0][1+nvar
+exist
+i
]))
2816 for (j
= i
+1; j
< nparam
; ++j
)
2817 if (value_notzero_p(P
->Ray
[0][1+nvar
+exist
+i
]))
2819 assert(j
>= nparam
); // for now
2821 Matrix
*M
= Matrix_Alloc(2, P
->Dimension
+2);
2822 value_set_si(M
->p
[0][0], 1);
2823 value_set_si(M
->p
[0][1+nvar
+exist
+i
], 1);
2824 value_set_si(M
->p
[1][0], 1);
2825 value_set_si(M
->p
[1][1+nvar
+exist
+i
], -1);
2826 value_absolute(M
->p
[1][1+P
->Dimension
], P
->Ray
[0][1+nvar
+exist
+i
]);
2827 value_decrement(M
->p
[1][1+P
->Dimension
], M
->p
[1][1+P
->Dimension
]);
2828 Polyhedron
*S
= AddConstraints(M
->p
[0], 2, P
, MaxRays
);
2829 evalue
*EP
= barvinok_enumerate_e(S
, exist
, nparam
, MaxRays
);
2833 return enumerate_cyclic(P
, exist
, nparam
, EP
, 0, i
, MaxRays
);
2836 static int single_param_pos(Polyhedron
*P
, unsigned exist
, unsigned nparam
,
2839 int nvar
= P
->Dimension
- exist
- nparam
;
2840 if (First_Non_Zero(P
->Ray
[r
]+1, nvar
) != -1)
2842 int i
= First_Non_Zero(P
->Ray
[r
]+1+nvar
+exist
, nparam
);
2845 if (First_Non_Zero(P
->Ray
[r
]+1+nvar
+exist
+1, nparam
-i
-1) != -1)
2850 static evalue
* enumerate_remove_ray(Polyhedron
*P
, int r
,
2851 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
2854 fprintf(stderr
, "\nER: RedundantRay\n");
2855 #endif /* DEBUG_ER */
2859 value_set_si(one
, 1);
2860 int len
= P
->NbRays
-1;
2861 Matrix
*M
= Matrix_Alloc(2 * len
, P
->Dimension
+2);
2862 Vector_Copy(P
->Ray
[0], M
->p
[0], r
* (P
->Dimension
+2));
2863 Vector_Copy(P
->Ray
[r
+1], M
->p
[r
], (len
-r
) * (P
->Dimension
+2));
2864 for (int j
= 0; j
< P
->NbRays
; ++j
) {
2867 Vector_Combine(P
->Ray
[j
], P
->Ray
[r
], M
->p
[len
+j
-(j
>r
)],
2868 one
, P
->Ray
[j
][P
->Dimension
+1], P
->Dimension
+2);
2871 P
= Rays2Polyhedron(M
, MaxRays
);
2873 evalue
*EP
= barvinok_enumerate_e(P
, exist
, nparam
, MaxRays
);
2880 static evalue
* enumerate_redundant_ray(Polyhedron
*P
,
2881 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
2883 assert(P
->NbBid
== 0);
2884 int nvar
= P
->Dimension
- exist
- nparam
;
2888 for (int r
= 0; r
< P
->NbRays
; ++r
) {
2889 if (value_notzero_p(P
->Ray
[r
][P
->Dimension
+1]))
2891 int i1
= single_param_pos(P
, exist
, nparam
, r
);
2894 for (int r2
= r
+1; r2
< P
->NbRays
; ++r2
) {
2895 if (value_notzero_p(P
->Ray
[r2
][P
->Dimension
+1]))
2897 int i2
= single_param_pos(P
, exist
, nparam
, r2
);
2903 value_division(m
, P
->Ray
[r
][1+nvar
+exist
+i1
],
2904 P
->Ray
[r2
][1+nvar
+exist
+i1
]);
2905 value_multiply(m
, m
, P
->Ray
[r2
][1+nvar
+exist
+i1
]);
2906 /* r2 divides r => r redundant */
2907 if (value_eq(m
, P
->Ray
[r
][1+nvar
+exist
+i1
])) {
2909 return enumerate_remove_ray(P
, r
, exist
, nparam
, MaxRays
);
2912 value_division(m
, P
->Ray
[r2
][1+nvar
+exist
+i1
],
2913 P
->Ray
[r
][1+nvar
+exist
+i1
]);
2914 value_multiply(m
, m
, P
->Ray
[r
][1+nvar
+exist
+i1
]);
2915 /* r divides r2 => r2 redundant */
2916 if (value_eq(m
, P
->Ray
[r2
][1+nvar
+exist
+i1
])) {
2918 return enumerate_remove_ray(P
, r2
, exist
, nparam
, MaxRays
);
2926 static Polyhedron
*upper_bound(Polyhedron
*P
,
2927 int pos
, Value
*max
, Polyhedron
**R
)
2936 for (Polyhedron
*Q
= P
; Q
; Q
= N
) {
2938 for (r
= 0; r
< P
->NbRays
; ++r
) {
2939 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1]) &&
2940 value_pos_p(P
->Ray
[r
][1+pos
]))
2943 if (r
< P
->NbRays
) {
2951 for (r
= 0; r
< P
->NbRays
; ++r
) {
2952 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1]))
2954 mpz_fdiv_q(v
, P
->Ray
[r
][1+pos
], P
->Ray
[r
][1+P
->Dimension
]);
2955 if ((!Q
->next
&& r
== 0) || value_gt(v
, *max
))
2956 value_assign(*max
, v
);
2963 static evalue
* enumerate_ray(Polyhedron
*P
,
2964 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
2966 assert(P
->NbBid
== 0);
2967 int nvar
= P
->Dimension
- exist
- nparam
;
2970 for (r
= 0; r
< P
->NbRays
; ++r
)
2971 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1]))
2977 for (r2
= r
+1; r2
< P
->NbRays
; ++r2
)
2978 if (value_zero_p(P
->Ray
[r2
][P
->Dimension
+1]))
2980 if (r2
< P
->NbRays
) {
2982 return enumerate_sum(P
, exist
, nparam
, MaxRays
);
2986 fprintf(stderr
, "\nER: Ray\n");
2987 #endif /* DEBUG_ER */
2993 value_set_si(one
, 1);
2994 int i
= single_param_pos(P
, exist
, nparam
, r
);
2995 assert(i
!= -1); // for now;
2997 Matrix
*M
= Matrix_Alloc(P
->NbRays
, P
->Dimension
+2);
2998 for (int j
= 0; j
< P
->NbRays
; ++j
) {
2999 Vector_Combine(P
->Ray
[j
], P
->Ray
[r
], M
->p
[j
],
3000 one
, P
->Ray
[j
][P
->Dimension
+1], P
->Dimension
+2);
3002 Polyhedron
*S
= Rays2Polyhedron(M
, MaxRays
);
3004 Polyhedron
*D
= DomainDifference(P
, S
, MaxRays
);
3006 // Polyhedron_Print(stderr, P_VALUE_FMT, D);
3007 assert(value_pos_p(P
->Ray
[r
][1+nvar
+exist
+i
])); // for now
3009 D
= upper_bound(D
, nvar
+exist
+i
, &m
, &R
);
3013 M
= Matrix_Alloc(2, P
->Dimension
+2);
3014 value_set_si(M
->p
[0][0], 1);
3015 value_set_si(M
->p
[1][0], 1);
3016 value_set_si(M
->p
[0][1+nvar
+exist
+i
], -1);
3017 value_set_si(M
->p
[1][1+nvar
+exist
+i
], 1);
3018 value_assign(M
->p
[0][1+P
->Dimension
], m
);
3019 value_oppose(M
->p
[1][1+P
->Dimension
], m
);
3020 value_addto(M
->p
[1][1+P
->Dimension
], M
->p
[1][1+P
->Dimension
],
3021 P
->Ray
[r
][1+nvar
+exist
+i
]);
3022 value_decrement(M
->p
[1][1+P
->Dimension
], M
->p
[1][1+P
->Dimension
]);
3023 // Matrix_Print(stderr, P_VALUE_FMT, M);
3024 D
= AddConstraints(M
->p
[0], 2, P
, MaxRays
);
3025 // Polyhedron_Print(stderr, P_VALUE_FMT, D);
3026 value_substract(M
->p
[0][1+P
->Dimension
], M
->p
[0][1+P
->Dimension
],
3027 P
->Ray
[r
][1+nvar
+exist
+i
]);
3028 // Matrix_Print(stderr, P_VALUE_FMT, M);
3029 S
= AddConstraints(M
->p
[0], 1, P
, MaxRays
);
3030 // Polyhedron_Print(stderr, P_VALUE_FMT, S);
3033 evalue
*EP
= barvinok_enumerate_e(D
, exist
, nparam
, MaxRays
);
3038 if (value_notone_p(P
->Ray
[r
][1+nvar
+exist
+i
]))
3039 EP
= enumerate_cyclic(P
, exist
, nparam
, EP
, r
, i
, MaxRays
);
3041 M
= Matrix_Alloc(1, nparam
+2);
3042 value_set_si(M
->p
[0][0], 1);
3043 value_set_si(M
->p
[0][1+i
], 1);
3044 enumerate_vd_add_ray(EP
, M
, MaxRays
);
3049 evalue
*E
= barvinok_enumerate_e(S
, exist
, nparam
, MaxRays
);
3051 free_evalue_refs(E
);
3058 evalue
*ER
= enumerate_or(R
, exist
, nparam
, MaxRays
);
3060 free_evalue_refs(ER
);
3067 static evalue
* new_zero_ep()
3072 evalue_set_si(EP
, 0, 1);
3076 static evalue
* enumerate_vd(Polyhedron
**PA
,
3077 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
3079 Polyhedron
*P
= *PA
;
3080 int nvar
= P
->Dimension
- exist
- nparam
;
3081 Param_Polyhedron
*PP
= NULL
;
3082 Polyhedron
*C
= Universe_Polyhedron(nparam
);
3086 PP
= Polyhedron2Param_SimplifiedDomain(&PR
,C
,MaxRays
,&CEq
,&CT
);
3090 Param_Domain
*D
, *last
;
3093 for (nd
= 0, D
=PP
->D
; D
; D
=D
->next
, ++nd
)
3096 Polyhedron
**VD
= new Polyhedron_p
[nd
];
3097 Polyhedron
**fVD
= new Polyhedron_p
[nd
];
3098 for(nd
= 0, D
=PP
->D
; D
; D
=D
->next
) {
3099 Polyhedron
*rVD
= reduce_domain(D
->Domain
, CT
, CEq
,
3113 /* This doesn't seem to have any effect */
3115 Polyhedron
*CA
= align_context(VD
[0], P
->Dimension
, MaxRays
);
3117 P
= DomainIntersection(P
, CA
, MaxRays
);
3120 Polyhedron_Free(CA
);
3125 if (!EP
&& CT
->NbColumns
!= CT
->NbRows
) {
3126 Polyhedron
*CEqr
= DomainImage(CEq
, CT
, MaxRays
);
3127 Polyhedron
*CA
= align_context(CEqr
, PR
->Dimension
, MaxRays
);
3128 Polyhedron
*I
= DomainIntersection(PR
, CA
, MaxRays
);
3129 Polyhedron_Free(CEqr
);
3130 Polyhedron_Free(CA
);
3132 fprintf(stderr
, "\nER: Eliminate\n");
3133 #endif /* DEBUG_ER */
3134 nparam
-= CT
->NbColumns
- CT
->NbRows
;
3135 EP
= barvinok_enumerate_e(I
, exist
, nparam
, MaxRays
);
3136 nparam
+= CT
->NbColumns
- CT
->NbRows
;
3137 addeliminatedparams_enum(EP
, CT
, CEq
, MaxRays
, nparam
);
3141 Polyhedron_Free(PR
);
3144 if (!EP
&& nd
> 1) {
3146 fprintf(stderr
, "\nER: VD\n");
3147 #endif /* DEBUG_ER */
3148 for (int i
= 0; i
< nd
; ++i
) {
3149 Polyhedron
*CA
= align_context(VD
[i
], P
->Dimension
, MaxRays
);
3150 Polyhedron
*I
= DomainIntersection(P
, CA
, MaxRays
);
3153 EP
= barvinok_enumerate_e(I
, exist
, nparam
, MaxRays
);
3155 evalue
*E
= barvinok_enumerate_e(I
, exist
, nparam
, MaxRays
);
3157 free_evalue_refs(E
);
3161 Polyhedron_Free(CA
);
3165 for (int i
= 0; i
< nd
; ++i
) {
3166 Polyhedron_Free(VD
[i
]);
3167 Polyhedron_Free(fVD
[i
]);
3173 if (!EP
&& nvar
== 0) {
3176 Param_Vertices
*V
, *V2
;
3177 Matrix
* M
= Matrix_Alloc(1, P
->Dimension
+2);
3179 FORALL_PVertex_in_ParamPolyhedron(V
, last
, PP
) {
3181 FORALL_PVertex_in_ParamPolyhedron(V2
, last
, PP
) {
3188 for (int i
= 0; i
< exist
; ++i
) {
3189 value_oppose(f
, V
->Vertex
->p
[i
][nparam
+1]);
3190 Vector_Combine(V
->Vertex
->p
[i
],
3192 M
->p
[0] + 1 + nvar
+ exist
,
3193 V2
->Vertex
->p
[i
][nparam
+1],
3197 for (j
= 0; j
< nparam
; ++j
)
3198 if (value_notzero_p(M
->p
[0][1+nvar
+exist
+j
]))
3202 ConstraintSimplify(M
->p
[0], M
->p
[0],
3203 P
->Dimension
+2, &f
);
3204 value_set_si(M
->p
[0][0], 0);
3205 Polyhedron
*para
= AddConstraints(M
->p
[0], 1, P
,
3208 Polyhedron_Free(para
);
3211 Polyhedron
*pos
, *neg
;
3212 value_set_si(M
->p
[0][0], 1);
3213 value_decrement(M
->p
[0][P
->Dimension
+1],
3214 M
->p
[0][P
->Dimension
+1]);
3215 neg
= AddConstraints(M
->p
[0], 1, P
, MaxRays
);
3216 value_set_si(f
, -1);
3217 Vector_Scale(M
->p
[0]+1, M
->p
[0]+1, f
,
3219 value_decrement(M
->p
[0][P
->Dimension
+1],
3220 M
->p
[0][P
->Dimension
+1]);
3221 value_decrement(M
->p
[0][P
->Dimension
+1],
3222 M
->p
[0][P
->Dimension
+1]);
3223 pos
= AddConstraints(M
->p
[0], 1, P
, MaxRays
);
3224 if (emptyQ(neg
) && emptyQ(pos
)) {
3225 Polyhedron_Free(para
);
3226 Polyhedron_Free(pos
);
3227 Polyhedron_Free(neg
);
3231 fprintf(stderr
, "\nER: Order\n");
3232 #endif /* DEBUG_ER */
3233 EP
= barvinok_enumerate_e(para
, exist
, nparam
, MaxRays
);
3236 E
= barvinok_enumerate_e(pos
, exist
, nparam
, MaxRays
);
3238 free_evalue_refs(E
);
3242 E
= barvinok_enumerate_e(neg
, exist
, nparam
, MaxRays
);
3244 free_evalue_refs(E
);
3247 Polyhedron_Free(para
);
3248 Polyhedron_Free(pos
);
3249 Polyhedron_Free(neg
);
3254 } END_FORALL_PVertex_in_ParamPolyhedron
;
3257 } END_FORALL_PVertex_in_ParamPolyhedron
;
3260 /* Search for vertex coordinate to split on */
3261 /* First look for one independent of the parameters */
3262 FORALL_PVertex_in_ParamPolyhedron(V
, last
, PP
) {
3263 for (int i
= 0; i
< exist
; ++i
) {
3265 for (j
= 0; j
< nparam
; ++j
)
3266 if (value_notzero_p(V
->Vertex
->p
[i
][j
]))
3270 value_set_si(M
->p
[0][0], 1);
3271 Vector_Set(M
->p
[0]+1, 0, nvar
+exist
);
3272 Vector_Copy(V
->Vertex
->p
[i
],
3273 M
->p
[0] + 1 + nvar
+ exist
, nparam
+1);
3274 value_oppose(M
->p
[0][1+nvar
+i
],
3275 V
->Vertex
->p
[i
][nparam
+1]);
3277 Polyhedron
*pos
, *neg
;
3278 value_set_si(M
->p
[0][0], 1);
3279 value_decrement(M
->p
[0][P
->Dimension
+1],
3280 M
->p
[0][P
->Dimension
+1]);
3281 neg
= AddConstraints(M
->p
[0], 1, P
, MaxRays
);
3282 value_set_si(f
, -1);
3283 Vector_Scale(M
->p
[0]+1, M
->p
[0]+1, f
,
3285 value_decrement(M
->p
[0][P
->Dimension
+1],
3286 M
->p
[0][P
->Dimension
+1]);
3287 value_decrement(M
->p
[0][P
->Dimension
+1],
3288 M
->p
[0][P
->Dimension
+1]);
3289 pos
= AddConstraints(M
->p
[0], 1, P
, MaxRays
);
3290 if (emptyQ(neg
) || emptyQ(pos
)) {
3291 Polyhedron_Free(pos
);
3292 Polyhedron_Free(neg
);
3295 Polyhedron_Free(pos
);
3296 value_increment(M
->p
[0][P
->Dimension
+1],
3297 M
->p
[0][P
->Dimension
+1]);
3298 pos
= AddConstraints(M
->p
[0], 1, P
, MaxRays
);
3300 fprintf(stderr
, "\nER: Vertex\n");
3301 #endif /* DEBUG_ER */
3303 EP
= enumerate_or(pos
, exist
, nparam
, MaxRays
);
3308 } END_FORALL_PVertex_in_ParamPolyhedron
;
3312 /* Search for vertex coordinate to split on */
3313 /* Now look for one that depends on the parameters */
3314 FORALL_PVertex_in_ParamPolyhedron(V
, last
, PP
) {
3315 for (int i
= 0; i
< exist
; ++i
) {
3316 value_set_si(M
->p
[0][0], 1);
3317 Vector_Set(M
->p
[0]+1, 0, nvar
+exist
);
3318 Vector_Copy(V
->Vertex
->p
[i
],
3319 M
->p
[0] + 1 + nvar
+ exist
, nparam
+1);
3320 value_oppose(M
->p
[0][1+nvar
+i
],
3321 V
->Vertex
->p
[i
][nparam
+1]);
3323 Polyhedron
*pos
, *neg
;
3324 value_set_si(M
->p
[0][0], 1);
3325 value_decrement(M
->p
[0][P
->Dimension
+1],
3326 M
->p
[0][P
->Dimension
+1]);
3327 neg
= AddConstraints(M
->p
[0], 1, P
, MaxRays
);
3328 value_set_si(f
, -1);
3329 Vector_Scale(M
->p
[0]+1, M
->p
[0]+1, f
,
3331 value_decrement(M
->p
[0][P
->Dimension
+1],
3332 M
->p
[0][P
->Dimension
+1]);
3333 value_decrement(M
->p
[0][P
->Dimension
+1],
3334 M
->p
[0][P
->Dimension
+1]);
3335 pos
= AddConstraints(M
->p
[0], 1, P
, MaxRays
);
3336 if (emptyQ(neg
) || emptyQ(pos
)) {
3337 Polyhedron_Free(pos
);
3338 Polyhedron_Free(neg
);
3341 Polyhedron_Free(pos
);
3342 value_increment(M
->p
[0][P
->Dimension
+1],
3343 M
->p
[0][P
->Dimension
+1]);
3344 pos
= AddConstraints(M
->p
[0], 1, P
, MaxRays
);
3346 fprintf(stderr
, "\nER: ParamVertex\n");
3347 #endif /* DEBUG_ER */
3349 EP
= enumerate_or(pos
, exist
, nparam
, MaxRays
);
3354 } END_FORALL_PVertex_in_ParamPolyhedron
;
3362 Polyhedron_Free(CEq
);
3366 Param_Polyhedron_Free(PP
);
3373 evalue
*barvinok_enumerate_pip(Polyhedron
*P
,
3374 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
3379 evalue
*barvinok_enumerate_pip(Polyhedron
*P
,
3380 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
3382 int nvar
= P
->Dimension
- exist
- nparam
;
3383 evalue
*EP
= new_zero_ep();
3384 Polyhedron
*Q
, *N
, *T
= 0;
3390 fprintf(stderr
, "\nER: PIP\n");
3391 #endif /* DEBUG_ER */
3393 for (int i
= 0; i
< P
->Dimension
; ++i
) {
3396 bool posray
= false;
3397 bool negray
= false;
3398 value_set_si(min
, 0);
3399 for (int j
= 0; j
< P
->NbRays
; ++j
) {
3400 if (value_pos_p(P
->Ray
[j
][1+i
])) {
3402 if (value_zero_p(P
->Ray
[j
][1+P
->Dimension
]))
3404 } else if (value_neg_p(P
->Ray
[j
][1+i
])) {
3406 if (value_zero_p(P
->Ray
[j
][1+P
->Dimension
]))
3410 P
->Ray
[j
][1+i
], P
->Ray
[j
][1+P
->Dimension
]);
3411 if (value_lt(tmp
, min
))
3412 value_assign(min
, tmp
);
3417 assert(!(posray
&& negray
));
3418 assert(!negray
); // for now
3419 Polyhedron
*O
= T
? T
: P
;
3420 /* shift by a safe amount */
3421 Matrix
*M
= Matrix_Alloc(O
->NbRays
, O
->Dimension
+2);
3422 Vector_Copy(O
->Ray
[0], M
->p
[0], O
->NbRays
* (O
->Dimension
+2));
3423 for (int j
= 0; j
< P
->NbRays
; ++j
) {
3424 if (value_notzero_p(M
->p
[j
][1+P
->Dimension
])) {
3425 value_multiply(tmp
, min
, M
->p
[j
][1+P
->Dimension
]);
3426 value_substract(M
->p
[j
][1+i
], M
->p
[j
][1+i
], tmp
);
3431 T
= Rays2Polyhedron(M
, MaxRays
);
3434 /* negating a parameter requires that we substitute in the
3435 * sign again afterwards.
3438 assert(i
< nvar
+exist
);
3440 T
= Polyhedron_Copy(P
);
3441 for (int j
= 0; j
< T
->NbRays
; ++j
)
3442 value_oppose(T
->Ray
[j
][1+i
], T
->Ray
[j
][1+i
]);
3443 for (int j
= 0; j
< T
->NbConstraints
; ++j
)
3444 value_oppose(T
->Constraint
[j
][1+i
], T
->Constraint
[j
][1+i
]);
3450 Polyhedron
*D
= pip_lexmin(T
? T
: P
, exist
, nparam
);
3451 for (Q
= D
; Q
; Q
= N
) {
3455 exist
= Q
->Dimension
- nvar
- nparam
;
3456 E
= barvinok_enumerate_e(Q
, exist
, nparam
, MaxRays
);
3459 free_evalue_refs(E
);
3471 static bool is_single(Value
*row
, int pos
, int len
)
3473 return First_Non_Zero(row
, pos
) == -1 &&
3474 First_Non_Zero(row
+pos
+1, len
-pos
-1) == -1;
3477 static evalue
* barvinok_enumerate_e_r(Polyhedron
*P
,
3478 unsigned exist
, unsigned nparam
, unsigned MaxRays
);
3481 static int er_level
= 0;
3483 evalue
* barvinok_enumerate_e(Polyhedron
*P
,
3484 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
3486 fprintf(stderr
, "\nER: level %i\n", er_level
);
3487 int nvar
= P
->Dimension
- exist
- nparam
;
3488 fprintf(stderr
, "%d %d %d\n", nvar
, exist
, nparam
);
3490 Polyhedron_Print(stderr
, P_VALUE_FMT
, P
);
3492 P
= DomainConstraintSimplify(Polyhedron_Copy(P
), MaxRays
);
3493 evalue
*EP
= barvinok_enumerate_e_r(P
, exist
, nparam
, MaxRays
);
3499 evalue
* barvinok_enumerate_e(Polyhedron
*P
,
3500 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
3502 P
= DomainConstraintSimplify(Polyhedron_Copy(P
), MaxRays
);
3503 evalue
*EP
= barvinok_enumerate_e_r(P
, exist
, nparam
, MaxRays
);
3509 static evalue
* barvinok_enumerate_e_r(Polyhedron
*P
,
3510 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
3513 Polyhedron
*U
= Universe_Polyhedron(nparam
);
3514 evalue
*EP
= barvinok_enumerate_ev(P
, U
, MaxRays
);
3515 //char *param_name[] = {"P", "Q", "R", "S", "T" };
3516 //print_evalue(stdout, EP, param_name);
3521 int nvar
= P
->Dimension
- exist
- nparam
;
3522 int len
= P
->Dimension
+ 2;
3525 return new_zero_ep();
3527 if (nvar
== 0 && nparam
== 0) {
3528 evalue
*EP
= new_zero_ep();
3529 barvinok_count(P
, &EP
->x
.n
, MaxRays
);
3530 if (value_pos_p(EP
->x
.n
))
3531 value_set_si(EP
->x
.n
, 1);
3536 for (r
= 0; r
< P
->NbRays
; ++r
)
3537 if (value_zero_p(P
->Ray
[r
][0]) ||
3538 value_zero_p(P
->Ray
[r
][P
->Dimension
+1])) {
3540 for (i
= 0; i
< nvar
; ++i
)
3541 if (value_notzero_p(P
->Ray
[r
][i
+1]))
3545 for (i
= nvar
+ exist
; i
< nvar
+ exist
+ nparam
; ++i
)
3546 if (value_notzero_p(P
->Ray
[r
][i
+1]))
3548 if (i
>= nvar
+ exist
+ nparam
)
3551 if (r
< P
->NbRays
) {
3552 evalue
*EP
= new_zero_ep();
3553 value_set_si(EP
->x
.n
, -1);
3558 for (r
= 0; r
< P
->NbEq
; ++r
)
3559 if ((first
= First_Non_Zero(P
->Constraint
[r
]+1+nvar
, exist
)) != -1)
3562 if (First_Non_Zero(P
->Constraint
[r
]+1+nvar
+first
+1,
3563 exist
-first
-1) != -1) {
3564 Polyhedron
*T
= rotate_along(P
, r
, nvar
, exist
, MaxRays
);
3566 fprintf(stderr
, "\nER: Equality\n");
3567 #endif /* DEBUG_ER */
3568 evalue
*EP
= barvinok_enumerate_e(T
, exist
-1, nparam
, MaxRays
);
3573 fprintf(stderr
, "\nER: Fixed\n");
3574 #endif /* DEBUG_ER */
3576 return barvinok_enumerate_e(P
, exist
-1, nparam
, MaxRays
);
3578 Polyhedron
*T
= Polyhedron_Copy(P
);
3579 SwapColumns(T
, nvar
+1, nvar
+1+first
);
3580 evalue
*EP
= barvinok_enumerate_e(T
, exist
-1, nparam
, MaxRays
);
3587 Vector
*row
= Vector_Alloc(len
);
3588 value_set_si(row
->p
[0], 1);
3593 enum constraint
* info
= new constraint
[exist
];
3594 for (int i
= 0; i
< exist
; ++i
) {
3596 for (int l
= P
->NbEq
; l
< P
->NbConstraints
; ++l
) {
3597 if (value_negz_p(P
->Constraint
[l
][nvar
+i
+1]))
3599 bool l_parallel
= is_single(P
->Constraint
[l
]+nvar
+1, i
, exist
);
3600 for (int u
= P
->NbEq
; u
< P
->NbConstraints
; ++u
) {
3601 if (value_posz_p(P
->Constraint
[u
][nvar
+i
+1]))
3603 bool lu_parallel
= l_parallel
||
3604 is_single(P
->Constraint
[u
]+nvar
+1, i
, exist
);
3605 value_oppose(f
, P
->Constraint
[u
][nvar
+i
+1]);
3606 Vector_Combine(P
->Constraint
[l
]+1, P
->Constraint
[u
]+1, row
->p
+1,
3607 f
, P
->Constraint
[l
][nvar
+i
+1], len
-1);
3608 if (!(info
[i
] & INDEPENDENT
)) {
3610 for (j
= 0; j
< exist
; ++j
)
3611 if (j
!= i
&& value_notzero_p(row
->p
[nvar
+j
+1]))
3614 //printf("independent: i: %d, l: %d, u: %d\n", i, l, u);
3615 info
[i
] = (constraint
)(info
[i
] | INDEPENDENT
);
3618 if (info
[i
] & ALL_POS
) {
3619 value_addto(row
->p
[len
-1], row
->p
[len
-1],
3620 P
->Constraint
[l
][nvar
+i
+1]);
3621 value_addto(row
->p
[len
-1], row
->p
[len
-1], f
);
3622 value_multiply(f
, f
, P
->Constraint
[l
][nvar
+i
+1]);
3623 value_substract(row
->p
[len
-1], row
->p
[len
-1], f
);
3624 value_decrement(row
->p
[len
-1], row
->p
[len
-1]);
3625 ConstraintSimplify(row
->p
, row
->p
, len
, &f
);
3626 value_set_si(f
, -1);
3627 Vector_Scale(row
->p
+1, row
->p
+1, f
, len
-1);
3628 value_decrement(row
->p
[len
-1], row
->p
[len
-1]);
3629 Polyhedron
*T
= AddConstraints(row
->p
, 1, P
, MaxRays
);
3631 //printf("not all_pos: i: %d, l: %d, u: %d\n", i, l, u);
3632 info
[i
] = (constraint
)(info
[i
] ^ ALL_POS
);
3634 //puts("pos remainder");
3635 //Polyhedron_Print(stdout, P_VALUE_FMT, T);
3638 if (!(info
[i
] & ONE_NEG
)) {
3640 negative_test_constraint(P
->Constraint
[l
],
3642 row
->p
, nvar
+i
, len
, &f
);
3643 oppose_constraint(row
->p
, len
, &f
);
3644 Polyhedron
*T
= AddConstraints(row
->p
, 1, P
, MaxRays
);
3646 //printf("one_neg i: %d, l: %d, u: %d\n", i, l, u);
3647 info
[i
] = (constraint
)(info
[i
] | ONE_NEG
);
3649 //puts("neg remainder");
3650 //Polyhedron_Print(stdout, P_VALUE_FMT, T);
3652 } else if (!(info
[i
] & ROT_NEG
)) {
3653 if (parallel_constraints(P
->Constraint
[l
],
3655 row
->p
, nvar
, exist
)) {
3656 negative_test_constraint7(P
->Constraint
[l
],
3658 row
->p
, nvar
, exist
,
3660 oppose_constraint(row
->p
, len
, &f
);
3661 Polyhedron
*T
= AddConstraints(row
->p
, 1, P
, MaxRays
);
3663 // printf("rot_neg i: %d, l: %d, u: %d\n", i, l, u);
3664 info
[i
] = (constraint
)(info
[i
] | ROT_NEG
);
3667 //puts("neg remainder");
3668 //Polyhedron_Print(stdout, P_VALUE_FMT, T);
3673 if (!(info
[i
] & ALL_POS
) && (info
[i
] & (ONE_NEG
| ROT_NEG
)))
3677 if (info
[i
] & ALL_POS
)
3684 for (int i = 0; i < exist; ++i)
3685 printf("%i: %i\n", i, info[i]);
3687 for (int i
= 0; i
< exist
; ++i
)
3688 if (info
[i
] & ALL_POS
) {
3690 fprintf(stderr
, "\nER: Positive\n");
3691 #endif /* DEBUG_ER */
3693 // Maybe we should chew off some of the fat here
3694 Matrix
*M
= Matrix_Alloc(P
->Dimension
, P
->Dimension
+1);
3695 for (int j
= 0; j
< P
->Dimension
; ++j
)
3696 value_set_si(M
->p
[j
][j
+ (j
>= i
+nvar
)], 1);
3697 Polyhedron
*T
= Polyhedron_Image(P
, M
, MaxRays
);
3699 evalue
*EP
= barvinok_enumerate_e(T
, exist
-1, nparam
, MaxRays
);
3706 for (int i
= 0; i
< exist
; ++i
)
3707 if (info
[i
] & ONE_NEG
) {
3709 fprintf(stderr
, "\nER: Negative\n");
3710 #endif /* DEBUG_ER */
3715 return barvinok_enumerate_e(P
, exist
-1, nparam
, MaxRays
);
3717 Polyhedron
*T
= Polyhedron_Copy(P
);
3718 SwapColumns(T
, nvar
+1, nvar
+1+i
);
3719 evalue
*EP
= barvinok_enumerate_e(T
, exist
-1, nparam
, MaxRays
);
3724 for (int i
= 0; i
< exist
; ++i
)
3725 if (info
[i
] & ROT_NEG
) {
3727 fprintf(stderr
, "\nER: Rotate\n");
3728 #endif /* DEBUG_ER */
3732 Polyhedron
*T
= rotate_along(P
, r
, nvar
, exist
, MaxRays
);
3733 evalue
*EP
= barvinok_enumerate_e(T
, exist
-1, nparam
, MaxRays
);
3737 for (int i
= 0; i
< exist
; ++i
)
3738 if (info
[i
] & INDEPENDENT
) {
3739 Polyhedron
*pos
, *neg
;
3741 /* Find constraint again and split off negative part */
3743 if (SplitOnVar(P
, i
, nvar
, len
, exist
, MaxRays
,
3744 row
, f
, true, &pos
, &neg
)) {
3746 fprintf(stderr
, "\nER: Split\n");
3747 #endif /* DEBUG_ER */
3750 barvinok_enumerate_e(neg
, exist
-1, nparam
, MaxRays
);
3752 barvinok_enumerate_e(pos
, exist
, nparam
, MaxRays
);
3754 free_evalue_refs(E
);
3756 Polyhedron_Free(neg
);
3757 Polyhedron_Free(pos
);
3771 EP
= enumerate_line(P
, exist
, nparam
, MaxRays
);
3775 EP
= barvinok_enumerate_pip(P
, exist
, nparam
, MaxRays
);
3779 EP
= enumerate_redundant_ray(P
, exist
, nparam
, MaxRays
);
3783 EP
= enumerate_sure(P
, exist
, nparam
, MaxRays
);
3787 EP
= enumerate_ray(P
, exist
, nparam
, MaxRays
);
3791 EP
= enumerate_sure2(P
, exist
, nparam
, MaxRays
);
3795 F
= unfringe(P
, MaxRays
);
3796 if (!PolyhedronIncludes(F
, P
)) {
3798 fprintf(stderr
, "\nER: Fringed\n");
3799 #endif /* DEBUG_ER */
3800 EP
= barvinok_enumerate_e(F
, exist
, nparam
, MaxRays
);
3807 EP
= enumerate_vd(&P
, exist
, nparam
, MaxRays
);
3812 EP
= enumerate_sum(P
, exist
, nparam
, MaxRays
);
3819 Polyhedron
*pos
, *neg
;
3820 for (i
= 0; i
< exist
; ++i
)
3821 if (SplitOnVar(P
, i
, nvar
, len
, exist
, MaxRays
,
3822 row
, f
, false, &pos
, &neg
))
3828 EP
= enumerate_or(pos
, exist
, nparam
, MaxRays
);
3840 gen_fun
* barvinok_series(Polyhedron
*P
, Polyhedron
* C
, unsigned MaxRays
)
3842 Polyhedron
** vcone
;
3844 unsigned nparam
= C
->Dimension
;
3848 sign
.SetLength(ncone
);
3850 CA
= align_context(C
, P
->Dimension
, MaxRays
);
3851 P
= DomainIntersection(P
, CA
, MaxRays
);
3852 Polyhedron_Free(CA
);
3854 assert(!Polyhedron_is_infinite(P
, nparam
));
3855 assert(P
->NbBid
== 0);
3856 assert(Polyhedron_has_positive_rays(P
, nparam
));
3857 assert(P
->NbEq
== 0);
3859 partial_reducer
red(P
, nparam
);