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);
217 Polyhedron
*C
= poly();
219 for (i
= 0; i
< lambda
.length(); ++i
)
222 if (i
== lambda
.length()) {
225 value_set_si(tmp
, -1);
226 Vector_Scale(z
->p
, z
->p
, tmp
, z
->Size
-1);
233 Polyhedron_Free(Cone
);
239 Matrix
*M
= Matrix_Alloc(Rays
->NbRows
+1, Rays
->NbColumns
+1);
240 for (int i
= 0; i
< Rays
->NbRows
; ++i
) {
241 Vector_Copy(Rays
->p
[i
], M
->p
[i
]+1, Rays
->NbColumns
);
242 value_set_si(M
->p
[i
][0], 1);
244 Vector_Set(M
->p
[Rays
->NbRows
]+1, 0, Rays
->NbColumns
-1);
245 value_set_si(M
->p
[Rays
->NbRows
][0], 1);
246 value_set_si(M
->p
[Rays
->NbRows
][Rays
->NbColumns
], 1);
247 Cone
= Rays2Polyhedron(M
, M
->NbRows
+1);
248 assert(Cone
->NbConstraints
== Cone
->NbRays
);
262 dpoly(int d
, ZZ
& degree
, int offset
= 0) {
263 coeff
.SetLength(d
+1);
265 int min
= d
+ offset
;
266 if (degree
>= 0 && degree
< ZZ(INIT_VAL
, min
))
267 min
= to_int(degree
);
269 ZZ c
= ZZ(INIT_VAL
, 1);
272 for (int i
= 1; i
<= min
; ++i
) {
273 c
*= (degree
-i
+ 1);
278 void operator *= (dpoly
& f
) {
279 assert(coeff
.length() == f
.coeff
.length());
281 coeff
= f
.coeff
[0] * coeff
;
282 for (int i
= 1; i
< coeff
.length(); ++i
)
283 for (int j
= 0; i
+j
< coeff
.length(); ++j
)
284 coeff
[i
+j
] += f
.coeff
[i
] * old
[j
];
286 void div(dpoly
& d
, mpq_t count
, ZZ
& sign
) {
287 int len
= coeff
.length();
290 mpq_t
* c
= new mpq_t
[coeff
.length()];
293 for (int i
= 0; i
< len
; ++i
) {
295 zz2value(coeff
[i
], tmp
);
296 mpq_set_z(c
[i
], tmp
);
298 for (int j
= 1; j
<= i
; ++j
) {
299 zz2value(d
.coeff
[j
], tmp
);
300 mpq_set_z(qtmp
, tmp
);
301 mpq_mul(qtmp
, qtmp
, c
[i
-j
]);
302 mpq_sub(c
[i
], c
[i
], qtmp
);
305 zz2value(d
.coeff
[0], tmp
);
306 mpq_set_z(qtmp
, tmp
);
307 mpq_div(c
[i
], c
[i
], qtmp
);
310 mpq_sub(count
, count
, c
[len
-1]);
312 mpq_add(count
, count
, c
[len
-1]);
316 for (int i
= 0; i
< len
; ++i
)
328 dpoly_n(int d
, ZZ
& degree_0
, ZZ
& degree_1
, int offset
= 0) {
332 zz2value(degree_0
, d0
);
333 zz2value(degree_1
, d1
);
334 coeff
= Matrix_Alloc(d
+1, d
+1+1);
335 value_set_si(coeff
->p
[0][0], 1);
336 value_set_si(coeff
->p
[0][d
+1], 1);
337 for (int i
= 1; i
<= d
; ++i
) {
338 value_multiply(coeff
->p
[i
][0], coeff
->p
[i
-1][0], d0
);
339 Vector_Combine(coeff
->p
[i
-1], coeff
->p
[i
-1]+1, coeff
->p
[i
]+1,
341 value_set_si(coeff
->p
[i
][d
+1], i
);
342 value_multiply(coeff
->p
[i
][d
+1], coeff
->p
[i
][d
+1], coeff
->p
[i
-1][d
+1]);
343 value_decrement(d0
, d0
);
348 void div(dpoly
& d
, Vector
*count
, ZZ
& sign
) {
349 int len
= coeff
->NbRows
;
350 Matrix
* c
= Matrix_Alloc(coeff
->NbRows
, coeff
->NbColumns
);
353 for (int i
= 0; i
< len
; ++i
) {
354 Vector_Copy(coeff
->p
[i
], c
->p
[i
], len
+1);
355 for (int j
= 1; j
<= i
; ++j
) {
356 zz2value(d
.coeff
[j
], tmp
);
357 value_multiply(tmp
, tmp
, c
->p
[i
][len
]);
358 value_oppose(tmp
, tmp
);
359 Vector_Combine(c
->p
[i
], c
->p
[i
-j
], c
->p
[i
],
360 c
->p
[i
-j
][len
], tmp
, len
);
361 value_multiply(c
->p
[i
][len
], c
->p
[i
][len
], c
->p
[i
-j
][len
]);
363 zz2value(d
.coeff
[0], tmp
);
364 value_multiply(c
->p
[i
][len
], c
->p
[i
][len
], tmp
);
367 value_set_si(tmp
, -1);
368 Vector_Scale(c
->p
[len
-1], count
->p
, tmp
, len
);
369 value_assign(count
->p
[len
], c
->p
[len
-1][len
]);
371 Vector_Copy(c
->p
[len
-1], count
->p
, len
+1);
372 Vector_Normalize(count
->p
, len
+1);
378 struct dpoly_r_term
{
383 /* len: number of elements in c
384 * each element in c is the coefficient of a power of t
385 * in the MacLaurin expansion
388 vector
< dpoly_r_term
* > *c
;
393 void add_term(int i
, int * powers
, ZZ
& coeff
) {
396 for (int k
= 0; k
< c
[i
].size(); ++k
) {
397 if (memcmp(c
[i
][k
]->powers
, powers
, dim
* sizeof(int)) == 0) {
398 c
[i
][k
]->coeff
+= coeff
;
402 dpoly_r_term
*t
= new dpoly_r_term
;
403 t
->powers
= new int[dim
];
404 memcpy(t
->powers
, powers
, dim
* sizeof(int));
408 dpoly_r(int len
, int dim
) {
412 c
= new vector
< dpoly_r_term
* > [len
];
414 dpoly_r(dpoly
& num
, int dim
) {
416 len
= num
.coeff
.length();
417 c
= new vector
< dpoly_r_term
* > [len
];
420 memset(powers
, 0, dim
* sizeof(int));
422 for (int i
= 0; i
< len
; ++i
) {
423 ZZ coeff
= num
.coeff
[i
];
424 add_term(i
, powers
, coeff
);
427 dpoly_r(dpoly
& num
, dpoly
& den
, int pos
, int dim
) {
429 len
= num
.coeff
.length();
430 c
= new vector
< dpoly_r_term
* > [len
];
434 for (int i
= 0; i
< len
; ++i
) {
435 ZZ coeff
= num
.coeff
[i
];
436 memset(powers
, 0, dim
* sizeof(int));
439 add_term(i
, powers
, coeff
);
441 for (int j
= 1; j
<= i
; ++j
) {
442 for (int k
= 0; k
< c
[i
-j
].size(); ++k
) {
443 memcpy(powers
, c
[i
-j
][k
]->powers
, dim
*sizeof(int));
445 coeff
= -den
.coeff
[j
-1] * c
[i
-j
][k
]->coeff
;
446 add_term(i
, powers
, coeff
);
452 dpoly_r(dpoly_r
* num
, dpoly
& den
, int pos
, int dim
) {
455 c
= new vector
< dpoly_r_term
* > [len
];
460 for (int i
= 0 ; i
< len
; ++i
) {
461 for (int k
= 0; k
< num
->c
[i
].size(); ++k
) {
462 memcpy(powers
, num
->c
[i
][k
]->powers
, dim
*sizeof(int));
464 add_term(i
, powers
, num
->c
[i
][k
]->coeff
);
467 for (int j
= 1; j
<= i
; ++j
) {
468 for (int k
= 0; k
< c
[i
-j
].size(); ++k
) {
469 memcpy(powers
, c
[i
-j
][k
]->powers
, dim
*sizeof(int));
471 coeff
= -den
.coeff
[j
-1] * c
[i
-j
][k
]->coeff
;
472 add_term(i
, powers
, coeff
);
478 for (int i
= 0 ; i
< len
; ++i
)
479 for (int k
= 0; k
< c
[i
].size(); ++k
) {
480 delete [] c
[i
][k
]->powers
;
485 dpoly_r
*div(dpoly
& d
) {
486 dpoly_r
*rc
= new dpoly_r(len
, dim
);
487 rc
->denom
= power(d
.coeff
[0], len
);
488 ZZ inv_d
= rc
->denom
/ d
.coeff
[0];
491 for (int i
= 0; i
< len
; ++i
) {
492 for (int k
= 0; k
< c
[i
].size(); ++k
) {
493 coeff
= c
[i
][k
]->coeff
* inv_d
;
494 rc
->add_term(i
, c
[i
][k
]->powers
, coeff
);
497 for (int j
= 1; j
<= i
; ++j
) {
498 for (int k
= 0; k
< rc
->c
[i
-j
].size(); ++k
) {
499 coeff
= - d
.coeff
[j
] * rc
->c
[i
-j
][k
]->coeff
/ d
.coeff
[0];
500 rc
->add_term(i
, rc
->c
[i
-j
][k
]->powers
, coeff
);
507 for (int i
= 0; i
< len
; ++i
) {
510 cout
<< c
[i
].size() << endl
;
511 for (int j
= 0; j
< c
[i
].size(); ++j
) {
512 for (int k
= 0; k
< dim
; ++k
) {
513 cout
<< c
[i
][j
]->powers
[k
] << " ";
515 cout
<< ": " << c
[i
][j
]->coeff
<< "/" << denom
<< endl
;
523 void decompose(Polyhedron
*C
);
524 virtual void handle(Polyhedron
*P
, int sign
) = 0;
527 struct polar_decomposer
: public decomposer
{
528 void decompose(Polyhedron
*C
, unsigned MaxRays
);
529 virtual void handle(Polyhedron
*P
, int sign
);
530 virtual void handle_polar(Polyhedron
*P
, int sign
) = 0;
533 void decomposer::decompose(Polyhedron
*C
)
535 vector
<cone
*> nonuni
;
536 cone
* c
= new cone(C
);
547 while (!nonuni
.empty()) {
550 Vector
* v
= c
->short_vector(lambda
);
551 for (int i
= 0; i
< c
->Rays
->NbRows
- 1; ++i
) {
554 Matrix
* M
= Matrix_Copy(c
->Rays
);
555 Vector_Copy(v
->p
, M
->p
[i
], v
->Size
);
556 cone
* pc
= new cone(M
);
557 assert (pc
->det
!= 0);
558 if (abs(pc
->det
) > 1) {
559 assert(abs(pc
->det
) < abs(c
->det
));
560 nonuni
.push_back(pc
);
562 handle(pc
->poly(), sign(pc
->det
) * s
);
572 void polar_decomposer::decompose(Polyhedron
*cone
, unsigned MaxRays
)
574 Polyhedron_Polarize(cone
);
575 if (cone
->NbRays
- 1 != cone
->Dimension
) {
576 Polyhedron
*tmp
= cone
;
577 cone
= triangularize_cone(cone
, MaxRays
);
578 Polyhedron_Free(tmp
);
580 for (Polyhedron
*Polar
= cone
; Polar
; Polar
= Polar
->next
)
581 decomposer::decompose(Polar
);
585 void polar_decomposer::handle(Polyhedron
*P
, int sign
)
587 Polyhedron_Polarize(P
);
588 handle_polar(P
, sign
);
592 * Barvinok's Decomposition of a simplicial cone
594 * Returns two lists of polyhedra
596 void barvinok_decompose(Polyhedron
*C
, Polyhedron
**ppos
, Polyhedron
**pneg
)
598 Polyhedron
*pos
= *ppos
, *neg
= *pneg
;
599 vector
<cone
*> nonuni
;
600 cone
* c
= new cone(C
);
607 Polyhedron
*p
= Polyhedron_Copy(c
->Cone
);
613 while (!nonuni
.empty()) {
616 Vector
* v
= c
->short_vector(lambda
);
617 for (int i
= 0; i
< c
->Rays
->NbRows
- 1; ++i
) {
620 Matrix
* M
= Matrix_Copy(c
->Rays
);
621 Vector_Copy(v
->p
, M
->p
[i
], v
->Size
);
622 cone
* pc
= new cone(M
);
623 assert (pc
->det
!= 0);
624 if (abs(pc
->det
) > 1) {
625 assert(abs(pc
->det
) < abs(c
->det
));
626 nonuni
.push_back(pc
);
628 Polyhedron
*p
= pc
->poly();
630 if (sign(pc
->det
) == s
) {
648 const int MAX_TRY
=10;
650 * Searches for a vector that is not orthogonal to any
651 * of the rays in rays.
653 static void nonorthog(mat_ZZ
& rays
, vec_ZZ
& lambda
)
655 int dim
= rays
.NumCols();
657 lambda
.SetLength(dim
);
661 for (int i
= 2; !found
&& i
<= 50*dim
; i
+=4) {
662 for (int j
= 0; j
< MAX_TRY
; ++j
) {
663 for (int k
= 0; k
< dim
; ++k
) {
664 int r
= random_int(i
)+2;
665 int v
= (2*(r
%2)-1) * (r
>> 1);
669 for (; k
< rays
.NumRows(); ++k
)
670 if (lambda
* rays
[k
] == 0)
672 if (k
== rays
.NumRows()) {
681 static void randomvector(Polyhedron
*P
, vec_ZZ
& lambda
, int nvar
)
685 unsigned int dim
= P
->Dimension
;
688 for (int i
= 0; i
< P
->NbRays
; ++i
) {
689 for (int j
= 1; j
<= dim
; ++j
) {
690 value_absolute(tmp
, P
->Ray
[i
][j
]);
691 int t
= VALUE_TO_LONG(tmp
) * 16;
696 for (int i
= 0; i
< P
->NbConstraints
; ++i
) {
697 for (int j
= 1; j
<= dim
; ++j
) {
698 value_absolute(tmp
, P
->Constraint
[i
][j
]);
699 int t
= VALUE_TO_LONG(tmp
) * 16;
706 lambda
.SetLength(nvar
);
707 for (int k
= 0; k
< nvar
; ++k
) {
708 int r
= random_int(max
*dim
)+2;
709 int v
= (2*(r
%2)-1) * (max
/2*dim
+ (r
>> 1));
714 static void add_rays(mat_ZZ
& rays
, Polyhedron
*i
, int *r
, int nvar
= -1,
717 unsigned dim
= i
->Dimension
;
720 for (int k
= 0; k
< i
->NbRays
; ++k
) {
721 if (!value_zero_p(i
->Ray
[k
][dim
+1]))
723 if (!all
&& nvar
!= dim
&& First_Non_Zero(i
->Ray
[k
]+1, nvar
) == -1)
725 values2zz(i
->Ray
[k
]+1, rays
[(*r
)++], nvar
);
729 void lattice_point(Value
* values
, Polyhedron
*i
, vec_ZZ
& vertex
)
731 unsigned dim
= i
->Dimension
;
732 if(!value_one_p(values
[dim
])) {
733 Matrix
* Rays
= rays(i
);
734 Matrix
*inv
= Matrix_Alloc(Rays
->NbRows
, Rays
->NbColumns
);
735 int ok
= Matrix_Inverse(Rays
, inv
);
739 Vector
*lambda
= Vector_Alloc(dim
+1);
740 Vector_Matrix_Product(values
, inv
, lambda
->p
);
742 for (int j
= 0; j
< dim
; ++j
)
743 mpz_cdiv_q(lambda
->p
[j
], lambda
->p
[j
], lambda
->p
[dim
]);
744 value_set_si(lambda
->p
[dim
], 1);
745 Vector
*A
= Vector_Alloc(dim
+1);
746 Vector_Matrix_Product(lambda
->p
, Rays
, A
->p
);
749 values2zz(A
->p
, vertex
, dim
);
752 values2zz(values
, vertex
, dim
);
755 static evalue
*term(int param
, ZZ
& c
, Value
*den
= NULL
)
757 evalue
*EP
= new evalue();
759 value_set_si(EP
->d
,0);
760 EP
->x
.p
= new_enode(polynomial
, 2, param
+ 1);
761 evalue_set_si(&EP
->x
.p
->arr
[0], 0, 1);
762 value_init(EP
->x
.p
->arr
[1].x
.n
);
764 value_set_si(EP
->x
.p
->arr
[1].d
, 1);
766 value_assign(EP
->x
.p
->arr
[1].d
, *den
);
767 zz2value(c
, EP
->x
.p
->arr
[1].x
.n
);
771 static void vertex_period(
772 Polyhedron
*i
, vec_ZZ
& lambda
, Matrix
*T
,
773 Value lcm
, int p
, Vector
*val
,
774 evalue
*E
, evalue
* ev
,
777 unsigned nparam
= T
->NbRows
- 1;
778 unsigned dim
= i
->Dimension
;
785 Vector
* values
= Vector_Alloc(dim
+ 1);
786 Vector_Matrix_Product(val
->p
, T
, values
->p
);
787 value_assign(values
->p
[dim
], lcm
);
788 lattice_point(values
->p
, i
, vertex
);
789 num
= vertex
* lambda
;
794 zz2value(num
, ev
->x
.n
);
795 value_assign(ev
->d
, lcm
);
802 values2zz(T
->p
[p
], vertex
, dim
);
803 nump
= vertex
* lambda
;
804 if (First_Non_Zero(val
->p
, p
) == -1) {
805 value_assign(tmp
, lcm
);
806 evalue
*ET
= term(p
, nump
, &tmp
);
808 free_evalue_refs(ET
);
812 value_assign(tmp
, lcm
);
813 if (First_Non_Zero(T
->p
[p
], dim
) != -1)
814 Vector_Gcd(T
->p
[p
], dim
, &tmp
);
816 if (value_lt(tmp
, lcm
)) {
819 value_division(tmp
, lcm
, tmp
);
820 value_set_si(ev
->d
, 0);
821 ev
->x
.p
= new_enode(periodic
, VALUE_TO_INT(tmp
), p
+1);
822 value2zz(tmp
, count
);
824 value_decrement(tmp
, tmp
);
826 ZZ new_offset
= offset
- count
* nump
;
827 value_assign(val
->p
[p
], tmp
);
828 vertex_period(i
, lambda
, T
, lcm
, p
+1, val
, E
,
829 &ev
->x
.p
->arr
[VALUE_TO_INT(tmp
)], new_offset
);
830 } while (value_pos_p(tmp
));
832 vertex_period(i
, lambda
, T
, lcm
, p
+1, val
, E
, ev
, offset
);
836 static void mask_r(Matrix
*f
, int nr
, Vector
*lcm
, int p
, Vector
*val
, evalue
*ev
)
838 unsigned nparam
= lcm
->Size
;
841 Vector
* prod
= Vector_Alloc(f
->NbRows
);
842 Matrix_Vector_Product(f
, val
->p
, prod
->p
);
844 for (int i
= 0; i
< nr
; ++i
) {
845 value_modulus(prod
->p
[i
], prod
->p
[i
], f
->p
[i
][nparam
+1]);
846 isint
&= value_zero_p(prod
->p
[i
]);
848 value_set_si(ev
->d
, 1);
850 value_set_si(ev
->x
.n
, isint
);
857 if (value_one_p(lcm
->p
[p
]))
858 mask_r(f
, nr
, lcm
, p
+1, val
, ev
);
860 value_assign(tmp
, lcm
->p
[p
]);
861 value_set_si(ev
->d
, 0);
862 ev
->x
.p
= new_enode(periodic
, VALUE_TO_INT(tmp
), p
+1);
864 value_decrement(tmp
, tmp
);
865 value_assign(val
->p
[p
], tmp
);
866 mask_r(f
, nr
, lcm
, p
+1, val
, &ev
->x
.p
->arr
[VALUE_TO_INT(tmp
)]);
867 } while (value_pos_p(tmp
));
872 static evalue
*multi_monom(vec_ZZ
& p
)
874 evalue
*X
= new evalue();
877 unsigned nparam
= p
.length()-1;
878 zz2value(p
[nparam
], X
->x
.n
);
879 value_set_si(X
->d
, 1);
880 for (int i
= 0; i
< nparam
; ++i
) {
883 evalue
*T
= term(i
, p
[i
]);
892 * Check whether mapping polyhedron P on the affine combination
893 * num yields a range that has a fixed quotient on integer
895 * If zero is true, then we are only interested in the quotient
896 * for the cases where the remainder is zero.
897 * Returns NULL if false and a newly allocated value if true.
899 static Value
*fixed_quotient(Polyhedron
*P
, vec_ZZ
& num
, Value d
, bool zero
)
902 int len
= num
.length();
903 Matrix
*T
= Matrix_Alloc(2, len
);
904 zz2values(num
, T
->p
[0]);
905 value_set_si(T
->p
[1][len
-1], 1);
906 Polyhedron
*I
= Polyhedron_Image(P
, T
, P
->NbConstraints
);
910 for (i
= 0; i
< I
->NbRays
; ++i
)
911 if (value_zero_p(I
->Ray
[i
][2])) {
919 int bounded
= line_minmax(I
, &min
, &max
);
923 mpz_cdiv_q(min
, min
, d
);
925 mpz_fdiv_q(min
, min
, d
);
926 mpz_fdiv_q(max
, max
, d
);
928 if (value_eq(min
, max
)) {
931 value_assign(*ret
, min
);
939 * Normalize linear expression coef modulo m
940 * Removes common factor and reduces coefficients
941 * Returns index of first non-zero coefficient or len
943 static int normal_mod(Value
*coef
, int len
, Value
*m
)
948 Vector_Gcd(coef
, len
, &gcd
);
950 Vector_AntiScale(coef
, coef
, gcd
, len
);
952 value_division(*m
, *m
, gcd
);
959 for (j
= 0; j
< len
; ++j
)
960 mpz_fdiv_r(coef
[j
], coef
[j
], *m
);
961 for (j
= 0; j
< len
; ++j
)
962 if (value_notzero_p(coef
[j
]))
969 static void mask(Matrix
*f
, evalue
*factor
)
971 int nr
= f
->NbRows
, nc
= f
->NbColumns
;
974 for (n
= 0; n
< nr
&& value_notzero_p(f
->p
[n
][nc
-1]); ++n
)
975 if (value_notone_p(f
->p
[n
][nc
-1]) &&
976 value_notmone_p(f
->p
[n
][nc
-1]))
990 value_set_si(EV
.x
.n
, 1);
992 for (n
= 0; n
< nr
; ++n
) {
993 value_assign(m
, f
->p
[n
][nc
-1]);
994 if (value_one_p(m
) || value_mone_p(m
))
997 int j
= normal_mod(f
->p
[n
], nc
-1, &m
);
999 free_evalue_refs(factor
);
1000 value_init(factor
->d
);
1001 evalue_set_si(factor
, 0, 1);
1005 values2zz(f
->p
[n
], row
, nc
-1);
1008 if (j
< (nc
-1)-1 && row
[j
] > g
/2) {
1009 for (int k
= j
; k
< (nc
-1); ++k
)
1011 row
[k
] = g
- row
[k
];
1015 value_set_si(EP
.d
, 0);
1016 EP
.x
.p
= new_enode(relation
, 2, 0);
1017 value_clear(EP
.x
.p
->arr
[1].d
);
1018 EP
.x
.p
->arr
[1] = *factor
;
1019 evalue
*ev
= &EP
.x
.p
->arr
[0];
1020 value_set_si(ev
->d
, 0);
1021 ev
->x
.p
= new_enode(fractional
, 3, -1);
1022 evalue_set_si(&ev
->x
.p
->arr
[1], 0, 1);
1023 evalue_set_si(&ev
->x
.p
->arr
[2], 1, 1);
1024 evalue
*E
= multi_monom(row
);
1025 value_assign(EV
.d
, m
);
1027 value_clear(ev
->x
.p
->arr
[0].d
);
1028 ev
->x
.p
->arr
[0] = *E
;
1034 free_evalue_refs(&EV
);
1040 static void mask(Matrix
*f
, evalue
*factor
)
1042 int nr
= f
->NbRows
, nc
= f
->NbColumns
;
1045 for (n
= 0; n
< nr
&& value_notzero_p(f
->p
[n
][nc
-1]); ++n
)
1046 if (value_notone_p(f
->p
[n
][nc
-1]) &&
1047 value_notmone_p(f
->p
[n
][nc
-1]))
1055 unsigned np
= nc
- 2;
1056 Vector
*lcm
= Vector_Alloc(np
);
1057 Vector
*val
= Vector_Alloc(nc
);
1058 Vector_Set(val
->p
, 0, nc
);
1059 value_set_si(val
->p
[np
], 1);
1060 Vector_Set(lcm
->p
, 1, np
);
1061 for (n
= 0; n
< nr
; ++n
) {
1062 if (value_one_p(f
->p
[n
][nc
-1]) ||
1063 value_mone_p(f
->p
[n
][nc
-1]))
1065 for (int j
= 0; j
< np
; ++j
)
1066 if (value_notzero_p(f
->p
[n
][j
])) {
1067 Gcd(f
->p
[n
][j
], f
->p
[n
][nc
-1], &tmp
);
1068 value_division(tmp
, f
->p
[n
][nc
-1], tmp
);
1069 value_lcm(tmp
, lcm
->p
[j
], &lcm
->p
[j
]);
1074 mask_r(f
, nr
, lcm
, 0, val
, &EP
);
1079 free_evalue_refs(&EP
);
1090 static bool mod_needed(Polyhedron
*PD
, vec_ZZ
& num
, Value d
, evalue
*E
)
1092 Value
*q
= fixed_quotient(PD
, num
, d
, false);
1097 value_oppose(*q
, *q
);
1100 value_set_si(EV
.d
, 1);
1102 value_multiply(EV
.x
.n
, *q
, d
);
1104 free_evalue_refs(&EV
);
1110 /* modifies f argument ! */
1111 static void ceil_mod(Value
*coef
, int len
, Value d
, ZZ
& f
, evalue
*EP
, Polyhedron
*PD
)
1115 value_set_si(m
, -1);
1117 Vector_Scale(coef
, coef
, m
, len
);
1120 int j
= normal_mod(coef
, len
, &m
);
1128 values2zz(coef
, num
, len
);
1135 evalue_set_si(&tmp
, 0, 1);
1139 while (j
< len
-1 && (num
[j
] == g
/2 || num
[j
] == 0))
1141 if ((j
< len
-1 && num
[j
] > g
/2) || (j
== len
-1 && num
[j
] >= (g
+1)/2)) {
1142 for (int k
= j
; k
< len
-1; ++k
)
1144 num
[k
] = g
- num
[k
];
1145 num
[len
-1] = g
- 1 - num
[len
-1];
1146 value_assign(tmp
.d
, m
);
1148 zz2value(t
, tmp
.x
.n
);
1154 ZZ t
= num
[len
-1] * f
;
1155 zz2value(t
, tmp
.x
.n
);
1156 value_assign(tmp
.d
, m
);
1159 evalue
*E
= multi_monom(num
);
1163 if (PD
&& !mod_needed(PD
, num
, m
, E
)) {
1165 zz2value(f
, EV
.x
.n
);
1166 value_assign(EV
.d
, m
);
1171 value_set_si(EV
.x
.n
, 1);
1172 value_assign(EV
.d
, m
);
1174 value_clear(EV
.x
.n
);
1175 value_set_si(EV
.d
, 0);
1176 EV
.x
.p
= new_enode(fractional
, 3, -1);
1177 evalue_copy(&EV
.x
.p
->arr
[0], E
);
1178 evalue_set_si(&EV
.x
.p
->arr
[1], 0, 1);
1179 value_init(EV
.x
.p
->arr
[2].x
.n
);
1180 zz2value(f
, EV
.x
.p
->arr
[2].x
.n
);
1181 value_set_si(EV
.x
.p
->arr
[2].d
, 1);
1186 free_evalue_refs(&EV
);
1187 free_evalue_refs(E
);
1191 free_evalue_refs(&tmp
);
1198 static void ceil(Value
*coef
, int len
, Value d
, ZZ
& f
,
1199 evalue
*EP
, Polyhedron
*PD
) {
1200 ceil_mod(coef
, len
, d
, f
, EP
, PD
);
1203 static void ceil(Value
*coef
, int len
, Value d
, ZZ
& f
,
1204 evalue
*EP
, Polyhedron
*PD
) {
1205 ceil_mod(coef
, len
, d
, f
, EP
, PD
);
1206 evalue_mod2table(EP
, len
-1);
1210 evalue
* bv_ceil3(Value
*coef
, int len
, Value d
, Polyhedron
*P
)
1212 Vector
*val
= Vector_Alloc(len
);
1216 value_set_si(t
, -1);
1217 Vector_Scale(coef
, val
->p
, t
, len
);
1218 value_absolute(t
, d
);
1221 values2zz(val
->p
, num
, len
);
1222 evalue
*EP
= multi_monom(num
);
1226 value_init(tmp
.x
.n
);
1227 value_set_si(tmp
.x
.n
, 1);
1228 value_assign(tmp
.d
, t
);
1234 ceil_mod(val
->p
, len
, t
, one
, EP
, P
);
1237 /* copy EP to malloc'ed evalue */
1243 free_evalue_refs(&tmp
);
1250 evalue
* lattice_point(
1251 Polyhedron
*i
, vec_ZZ
& lambda
, Matrix
*W
, Value lcm
, Polyhedron
*PD
)
1253 unsigned nparam
= W
->NbColumns
- 1;
1255 Matrix
* Rays
= rays2(i
);
1256 Matrix
*T
= Transpose(Rays
);
1257 Matrix
*T2
= Matrix_Copy(T
);
1258 Matrix
*inv
= Matrix_Alloc(T2
->NbRows
, T2
->NbColumns
);
1259 int ok
= Matrix_Inverse(T2
, inv
);
1264 matrix2zz(W
, vertex
, W
->NbRows
, W
->NbColumns
);
1267 num
= lambda
* vertex
;
1269 evalue
*EP
= multi_monom(num
);
1273 value_init(tmp
.x
.n
);
1274 value_set_si(tmp
.x
.n
, 1);
1275 value_assign(tmp
.d
, lcm
);
1279 Matrix
*L
= Matrix_Alloc(inv
->NbRows
, W
->NbColumns
);
1280 Matrix_Product(inv
, W
, L
);
1283 matrix2zz(T
, RT
, T
->NbRows
, T
->NbColumns
);
1286 vec_ZZ p
= lambda
* RT
;
1288 for (int i
= 0; i
< L
->NbRows
; ++i
) {
1289 ceil_mod(L
->p
[i
], nparam
+1, lcm
, p
[i
], EP
, PD
);
1295 free_evalue_refs(&tmp
);
1299 evalue
* lattice_point(
1300 Polyhedron
*i
, vec_ZZ
& lambda
, Matrix
*W
, Value lcm
, Polyhedron
*PD
)
1302 Matrix
*T
= Transpose(W
);
1303 unsigned nparam
= T
->NbRows
- 1;
1305 evalue
*EP
= new evalue();
1307 evalue_set_si(EP
, 0, 1);
1310 Vector
*val
= Vector_Alloc(nparam
+1);
1311 value_set_si(val
->p
[nparam
], 1);
1312 ZZ
offset(INIT_VAL
, 0);
1314 vertex_period(i
, lambda
, T
, lcm
, 0, val
, EP
, &ev
, offset
);
1317 free_evalue_refs(&ev
);
1328 Param_Vertices
* V
, Polyhedron
*i
, vec_ZZ
& lambda
, term_info
* term
,
1331 unsigned nparam
= V
->Vertex
->NbColumns
- 2;
1332 unsigned dim
= i
->Dimension
;
1334 vertex
.SetDims(V
->Vertex
->NbRows
, nparam
+1);
1338 value_set_si(lcm
, 1);
1339 for (int j
= 0; j
< V
->Vertex
->NbRows
; ++j
) {
1340 value_lcm(lcm
, V
->Vertex
->p
[j
][nparam
+1], &lcm
);
1342 if (value_notone_p(lcm
)) {
1343 Matrix
* mv
= Matrix_Alloc(dim
, nparam
+1);
1344 for (int j
= 0 ; j
< dim
; ++j
) {
1345 value_division(tmp
, lcm
, V
->Vertex
->p
[j
][nparam
+1]);
1346 Vector_Scale(V
->Vertex
->p
[j
], mv
->p
[j
], tmp
, nparam
+1);
1349 term
->E
= lattice_point(i
, lambda
, mv
, lcm
, PD
);
1357 for (int i
= 0; i
< V
->Vertex
->NbRows
; ++i
) {
1358 assert(value_one_p(V
->Vertex
->p
[i
][nparam
+1])); // for now
1359 values2zz(V
->Vertex
->p
[i
], vertex
[i
], nparam
+1);
1363 num
= lambda
* vertex
;
1367 for (int j
= 0; j
< nparam
; ++j
)
1373 term
->E
= multi_monom(num
);
1377 term
->constant
= num
[nparam
];
1380 term
->coeff
= num
[p
];
1387 static void normalize(ZZ
& sign
, ZZ
& num
, vec_ZZ
& den
)
1389 unsigned dim
= den
.length();
1393 for (int j
= 0; j
< den
.length(); ++j
) {
1397 den
[j
] = abs(den
[j
]);
1406 * f: the powers in the denominator for the remaining vars
1407 * each row refers to a factor
1408 * den_s: for each factor, the power of (s+1)
1410 * num_s: powers in the numerator corresponding to the summed vars
1411 * num_p: powers in the numerator corresponding to the remaining vars
1412 * number of rays in cone: "dim" = "k"
1413 * length of each ray: "dim" = "d"
1414 * for now, it is assumed: k == d
1416 * den_p: for each factor
1417 * 0: independent of remaining vars
1418 * 1: power corresponds to corresponding row in f
1420 * all inputs are subject to change
1422 static void normalize(ZZ
& sign
,
1423 ZZ
& num_s
, vec_ZZ
& num_p
, vec_ZZ
& den_s
, vec_ZZ
& den_p
,
1426 unsigned dim
= f
.NumRows();
1427 unsigned nparam
= num_p
.length();
1428 unsigned nvar
= dim
- nparam
;
1432 for (int j
= 0; j
< den_s
.length(); ++j
) {
1433 if (den_s
[j
] == 0) {
1438 for (k
= 0; k
< nparam
; ++k
)
1452 den_s
[j
] = abs(den_s
[j
]);
1461 struct counter
: public polar_decomposer
{
1473 counter(Polyhedron
*P
) {
1476 randomvector(P
, lambda
, dim
);
1477 rays
.SetDims(dim
, dim
);
1482 void start(unsigned MaxRays
);
1488 virtual void handle_polar(Polyhedron
*P
, int sign
);
1491 void counter::handle_polar(Polyhedron
*C
, int s
)
1494 assert(C
->NbRays
-1 == dim
);
1495 add_rays(rays
, C
, &r
);
1496 for (int k
= 0; k
< dim
; ++k
) {
1497 assert(lambda
* rays
[k
] != 0);
1502 lattice_point(P
->Ray
[j
]+1, C
, vertex
);
1503 num
= vertex
* lambda
;
1504 den
= rays
* lambda
;
1505 normalize(sign
, num
, den
);
1508 dpoly
n(dim
, den
[0], 1);
1509 for (int k
= 1; k
< dim
; ++k
) {
1510 dpoly
fact(dim
, den
[k
], 1);
1513 d
.div(n
, count
, sign
);
1516 void counter::start(unsigned MaxRays
)
1518 for (j
= 0; j
< P
->NbRays
; ++j
) {
1519 Polyhedron
*C
= supporting_cone(P
, j
);
1520 decompose(C
, MaxRays
);
1524 struct reducer
: public polar_decomposer
{
1536 int lower
; // call base when only this many variables is left
1537 int untouched
; // keep this many variables untouched
1539 reducer(Polyhedron
*P
) {
1542 //den.SetLength(dim);
1549 void start(unsigned MaxRays
);
1557 virtual void handle_polar(Polyhedron
*P
, int sign
);
1558 void reduce(ZZ c
, ZZ cd
, vec_ZZ
& num
, mat_ZZ
& den_f
);
1559 virtual void base(ZZ
& c
, ZZ
& cd
, vec_ZZ
& num
, mat_ZZ
& den_f
) = 0;
1562 void reducer::reduce(ZZ c
, ZZ cd
, vec_ZZ
& num
, mat_ZZ
& den_f
)
1564 unsigned len
= den_f
.NumRows(); // number of factors in den
1565 unsigned d
= num
.length()-1;
1568 base(c
, cd
, num
, den_f
);
1571 assert(num
.length() > 1);
1574 den_s
.SetLength(len
);
1576 den_r
.SetDims(len
, d
);
1578 /* Since we're working incrementally, we can look
1579 * for the "easiest" parameter first.
1580 * In particular we first handle the parameters such
1581 * that no_param + only_param == len, since that allows
1582 * us to decouple the problem and the split off part
1583 * may very well be zero
1585 * Let's just disable this for now
1586 * Using this optimization, we can't guarantee that
1587 * all the generating functions converge on the same neighbourhood
1592 for (i = 0; i < d+1-untouched; ++i) {
1593 for (r = 0; r < len; ++r) {
1594 if (den_f[r][i] != 0) {
1595 for (k = 0; k <= d; ++k)
1596 if (i != k && den_f[r][k] != 0)
1605 if (i > d-untouched)
1609 for (r
= 0; r
< len
; ++r
) {
1610 den_s
[r
] = den_f
[r
][i
];
1611 for (k
= 0; k
<= d
; ++k
)
1613 den_r
[r
][k
-(k
>i
)] = den_f
[r
][k
];
1619 for (k
= 0 ; k
<= d
; ++k
)
1621 num_p
[k
-(k
>i
)] = num
[k
];
1624 den_p
.SetLength(len
);
1626 normalize(c
, num_s
, num_p
, den_s
, den_p
, den_r
);
1630 for (int k
= 0; k
< len
; ++k
) {
1633 else if (den_s
[k
] == 0)
1636 if (no_param
== 0) {
1637 reduce(c
, cd
, num_p
, den_r
);
1641 pden
.SetDims(only_param
, d
);
1643 for (k
= 0, l
= 0; k
< len
; ++k
)
1645 pden
[l
++] = den_r
[k
];
1647 for (k
= 0; k
< len
; ++k
)
1651 dpoly
n(no_param
, num_s
);
1652 dpoly
D(no_param
, den_s
[k
], 1);
1653 for ( ; ++k
< len
; )
1654 if (den_p
[k
] == 0) {
1655 dpoly
fact(no_param
, den_s
[k
], 1);
1659 if (no_param
+ only_param
== len
) {
1660 mpq_set_si(tcount
, 0, 1);
1661 n
.div(D
, tcount
, one
);
1664 value2zz(mpq_numref(tcount
), qn
);
1665 value2zz(mpq_denref(tcount
), qd
);
1671 reduce(qn
, qd
, num_p
, pden
);
1675 for (k
= 0; k
< len
; ++k
) {
1676 if (den_s
[k
] == 0 || den_p
[k
] == 0)
1679 dpoly
pd(no_param
-1, den_s
[k
], 1);
1682 for (l
= 0; l
< k
; ++l
)
1683 if (den_r
[l
] == den_r
[k
])
1687 r
= new dpoly_r(n
, pd
, l
, len
);
1689 dpoly_r
*nr
= new dpoly_r(r
, pd
, l
, len
);
1695 dpoly_r
*rc
= r
->div(D
);
1699 int common
= pden
.NumRows();
1700 vector
< dpoly_r_term
* >& final
= rc
->c
[rc
->len
-1];
1702 for (int j
= 0; j
< final
.size(); ++j
) {
1703 if (final
[j
]->coeff
== 0)
1706 pden
.SetDims(rows
, pden
.NumCols());
1707 for (int k
= 0; k
< rc
->dim
; ++k
) {
1708 int n
= final
[j
]->powers
[k
];
1711 pden
.SetDims(rows
+n
, pden
.NumCols());
1712 for (int l
= 0; l
< n
; ++l
)
1713 pden
[rows
+l
] = den_r
[k
];
1716 final
[j
]->coeff
*= c
;
1717 reduce(final
[j
]->coeff
, rc
->denom
, num_p
, pden
);
1726 void reducer::handle_polar(Polyhedron
*C
, int s
)
1728 assert(C
->NbRays
-1 == dim
);
1732 lattice_point(P
->Ray
[j
]+1, C
, vertex
);
1735 den
.SetDims(dim
, dim
);
1738 for (r
= 0; r
< dim
; ++r
)
1739 values2zz(C
->Ray
[r
]+1, den
[r
], dim
);
1741 reduce(sgn
, one
, vertex
, den
);
1744 void reducer::start(unsigned MaxRays
)
1746 for (j
= 0; j
< P
->NbRays
; ++j
) {
1747 Polyhedron
*C
= supporting_cone(P
, j
);
1748 decompose(C
, MaxRays
);
1752 // incremental counter
1753 struct icounter
: public reducer
{
1756 icounter(Polyhedron
*P
) : reducer(P
) {
1764 virtual void base(ZZ
& c
, ZZ
& cd
, vec_ZZ
& num
, mat_ZZ
& den_f
);
1767 void icounter::base(ZZ
& c
, ZZ
& cd
, vec_ZZ
& num
, mat_ZZ
& den_f
)
1770 unsigned len
= den_f
.NumRows(); // number of factors in den
1772 den_s
.SetLength(len
);
1774 for (r
= 0; r
< len
; ++r
)
1775 den_s
[r
] = den_f
[r
][0];
1776 normalize(c
, num_s
, den_s
);
1778 dpoly
n(len
, num_s
);
1779 dpoly
D(len
, den_s
[0], 1);
1780 for (int k
= 1; k
< len
; ++k
) {
1781 dpoly
fact(len
, den_s
[k
], 1);
1784 mpq_set_si(tcount
, 0, 1);
1785 n
.div(D
, tcount
, one
);
1788 mpz_mul(mpq_numref(tcount
), mpq_numref(tcount
), tn
);
1789 mpz_mul(mpq_denref(tcount
), mpq_denref(tcount
), td
);
1790 mpq_canonicalize(tcount
);
1791 mpq_add(count
, count
, tcount
);
1794 struct partial_reducer
: public reducer
{
1797 partial_reducer(Polyhedron
*P
, unsigned nparam
) : reducer(P
) {
1798 gf
= new gen_fun(Polyhedron_Project(P
, nparam
));
1802 ~partial_reducer() {
1804 virtual void base(ZZ
& c
, ZZ
& cd
, vec_ZZ
& num
, mat_ZZ
& den_f
);
1805 void start(unsigned MaxRays
);
1808 void partial_reducer::base(ZZ
& c
, ZZ
& cd
, vec_ZZ
& num
, mat_ZZ
& den_f
)
1810 gf
->add(c
, cd
, num
, den_f
);
1813 void partial_reducer::start(unsigned MaxRays
)
1815 for (j
= 0; j
< P
->NbRays
; ++j
) {
1816 if (!value_pos_p(P
->Ray
[j
][dim
+1]))
1819 Polyhedron
*C
= supporting_cone(P
, j
);
1820 decompose(C
, MaxRays
);
1824 typedef Polyhedron
* Polyhedron_p
;
1826 void barvinok_count(Polyhedron
*P
, Value
* result
, unsigned NbMaxCons
)
1828 Polyhedron
** vcone
;
1837 value_set_si(*result
, 0);
1841 for (; r
< P
->NbRays
; ++r
)
1842 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1]))
1844 if (P
->NbBid
!=0 || r
< P
->NbRays
) {
1845 value_set_si(*result
, -1);
1849 P
= remove_equalities(P
);
1852 value_set_si(*result
, 0);
1858 value_set_si(factor
, 1);
1859 Q
= Polyhedron_Reduce(P
, &factor
);
1866 if (P
->Dimension
== 0) {
1867 value_assign(*result
, factor
);
1870 value_clear(factor
);
1874 #ifdef USE_INCREMENTAL
1879 cnt
.start(NbMaxCons
);
1881 assert(value_one_p(&cnt
.count
[0]._mp_den
));
1882 value_multiply(*result
, &cnt
.count
[0]._mp_num
, factor
);
1886 value_clear(factor
);
1889 static void uni_polynom(int param
, Vector
*c
, evalue
*EP
)
1891 unsigned dim
= c
->Size
-2;
1893 value_set_si(EP
->d
,0);
1894 EP
->x
.p
= new_enode(polynomial
, dim
+1, param
+1);
1895 for (int j
= 0; j
<= dim
; ++j
)
1896 evalue_set(&EP
->x
.p
->arr
[j
], c
->p
[j
], c
->p
[dim
+1]);
1899 static void multi_polynom(Vector
*c
, evalue
* X
, evalue
*EP
)
1901 unsigned dim
= c
->Size
-2;
1905 evalue_set(&EC
, c
->p
[dim
], c
->p
[dim
+1]);
1908 evalue_set(EP
, c
->p
[dim
], c
->p
[dim
+1]);
1910 for (int i
= dim
-1; i
>= 0; --i
) {
1912 value_assign(EC
.x
.n
, c
->p
[i
]);
1915 free_evalue_refs(&EC
);
1918 Polyhedron
*unfringe (Polyhedron
*P
, unsigned MaxRays
)
1920 int len
= P
->Dimension
+2;
1921 Polyhedron
*T
, *R
= P
;
1924 Vector
*row
= Vector_Alloc(len
);
1925 value_set_si(row
->p
[0], 1);
1927 R
= DomainConstraintSimplify(Polyhedron_Copy(P
), MaxRays
);
1929 Matrix
*M
= Matrix_Alloc(2, len
-1);
1930 value_set_si(M
->p
[1][len
-2], 1);
1931 for (int v
= 0; v
< P
->Dimension
; ++v
) {
1932 value_set_si(M
->p
[0][v
], 1);
1933 Polyhedron
*I
= Polyhedron_Image(P
, M
, 2+1);
1934 value_set_si(M
->p
[0][v
], 0);
1935 for (int r
= 0; r
< I
->NbConstraints
; ++r
) {
1936 if (value_zero_p(I
->Constraint
[r
][0]))
1938 if (value_zero_p(I
->Constraint
[r
][1]))
1940 if (value_one_p(I
->Constraint
[r
][1]))
1942 if (value_mone_p(I
->Constraint
[r
][1]))
1944 value_absolute(g
, I
->Constraint
[r
][1]);
1945 Vector_Set(row
->p
+1, 0, len
-2);
1946 value_division(row
->p
[1+v
], I
->Constraint
[r
][1], g
);
1947 mpz_fdiv_q(row
->p
[len
-1], I
->Constraint
[r
][2], g
);
1949 R
= AddConstraints(row
->p
, 1, R
, MaxRays
);
1961 static Polyhedron
*reduce_domain(Polyhedron
*D
, Matrix
*CT
, Polyhedron
*CEq
,
1962 Polyhedron
**fVD
, int nd
, unsigned MaxRays
)
1967 Dt
= CT
? DomainPreimage(D
, CT
, MaxRays
) : D
;
1968 Polyhedron
*rVD
= DomainIntersection(Dt
, CEq
, MaxRays
);
1970 /* if rVD is empty or too small in geometric dimension */
1971 if(!rVD
|| emptyQ(rVD
) ||
1972 (rVD
->Dimension
-rVD
->NbEq
< Dt
->Dimension
-Dt
->NbEq
-CEq
->NbEq
)) {
1977 return 0; /* empty validity domain */
1983 fVD
[nd
] = Domain_Copy(rVD
);
1984 for (int i
= 0 ; i
< nd
; ++i
) {
1985 Polyhedron
*I
= DomainIntersection(fVD
[nd
], fVD
[i
], MaxRays
);
1990 Polyhedron
*F
= DomainSimplify(I
, fVD
[nd
], MaxRays
);
1992 Polyhedron
*T
= rVD
;
1993 rVD
= DomainDifference(rVD
, F
, MaxRays
);
2000 rVD
= DomainConstraintSimplify(rVD
, MaxRays
);
2002 Domain_Free(fVD
[nd
]);
2009 barvinok_count(rVD
, &c
, MaxRays
);
2010 if (value_zero_p(c
)) {
2019 static bool Polyhedron_is_infinite(Polyhedron
*P
, unsigned nparam
)
2022 for (r
= 0; r
< P
->NbRays
; ++r
)
2023 if (value_zero_p(P
->Ray
[r
][0]) ||
2024 value_zero_p(P
->Ray
[r
][P
->Dimension
+1])) {
2026 for (i
= P
->Dimension
- nparam
; i
< P
->Dimension
; ++i
)
2027 if (value_notzero_p(P
->Ray
[r
][i
+1]))
2029 if (i
>= P
->Dimension
)
2032 return r
< P
->NbRays
;
2035 /* Check whether all rays point in the positive directions
2036 * for the parameters
2038 static bool Polyhedron_has_positive_rays(Polyhedron
*P
, unsigned nparam
)
2041 for (r
= 0; r
< P
->NbRays
; ++r
)
2042 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1])) {
2044 for (i
= P
->Dimension
- nparam
; i
< P
->Dimension
; ++i
)
2045 if (value_neg_p(P
->Ray
[r
][i
+1]))
2051 typedef evalue
* evalue_p
;
2053 struct enumerator
: public polar_decomposer
{
2067 enumerator(Polyhedron
*P
, unsigned dim
, unsigned nbV
) {
2071 randomvector(P
, lambda
, dim
);
2072 rays
.SetDims(dim
, dim
);
2074 c
= Vector_Alloc(dim
+2);
2076 vE
= new evalue_p
[nbV
];
2077 for (int j
= 0; j
< nbV
; ++j
)
2083 void decompose_at(Param_Vertices
*V
, int _i
, unsigned MaxRays
) {
2084 Polyhedron
*C
= supporting_cone_p(P
, V
);
2088 vE
[_i
] = new evalue
;
2089 value_init(vE
[_i
]->d
);
2090 evalue_set_si(vE
[_i
], 0, 1);
2092 decompose(C
, MaxRays
);
2099 for (int j
= 0; j
< nbV
; ++j
)
2101 free_evalue_refs(vE
[j
]);
2107 virtual void handle_polar(Polyhedron
*P
, int sign
);
2110 void enumerator::handle_polar(Polyhedron
*C
, int s
)
2113 assert(C
->NbRays
-1 == dim
);
2114 add_rays(rays
, C
, &r
);
2115 for (int k
= 0; k
< dim
; ++k
) {
2116 assert(lambda
* rays
[k
] != 0);
2121 lattice_point(V
, C
, lambda
, &num
, 0);
2122 den
= rays
* lambda
;
2123 normalize(sign
, num
.constant
, den
);
2125 dpoly
n(dim
, den
[0], 1);
2126 for (int k
= 1; k
< dim
; ++k
) {
2127 dpoly
fact(dim
, den
[k
], 1);
2130 if (num
.E
!= NULL
) {
2131 ZZ
one(INIT_VAL
, 1);
2132 dpoly_n
d(dim
, num
.constant
, one
);
2135 multi_polynom(c
, num
.E
, &EV
);
2137 free_evalue_refs(&EV
);
2138 free_evalue_refs(num
.E
);
2140 } else if (num
.pos
!= -1) {
2141 dpoly_n
d(dim
, num
.constant
, num
.coeff
);
2144 uni_polynom(num
.pos
, c
, &EV
);
2146 free_evalue_refs(&EV
);
2148 mpq_set_si(count
, 0, 1);
2149 dpoly
d(dim
, num
.constant
);
2150 d
.div(n
, count
, sign
);
2153 evalue_set(&EV
, &count
[0]._mp_num
, &count
[0]._mp_den
);
2155 free_evalue_refs(&EV
);
2159 struct ienumerator
: public polar_decomposer
{
2160 Polyhedron
*P
, *pVD
;
2171 ienumerator(Polyhedron
*P
, unsigned dim
, unsigned nbV
) {
2176 vE
= new evalue_p
[nbV
];
2177 for (int j
= 0; j
< nbV
; ++j
)
2180 E_vertex
= new evalue_p
[dim
];
2181 vertex
.SetLength(dim
);
2183 den
.SetDims(dim
, dim
);
2185 evalue_set_si(&mone
, -1, 1);
2188 void decompose_at(Param_Vertices
*V
, int _i
, unsigned MaxRays
/*, Polyhedron *pVD*/) {
2189 Polyhedron
*C
= supporting_cone_p(P
, V
);
2194 vE
[_i
] = new evalue
;
2195 value_init(vE
[_i
]->d
);
2196 evalue_set_si(vE
[_i
], 0, 1);
2198 decompose(C
, MaxRays
);
2202 for (int j
= 0; j
< nbV
; ++j
)
2204 free_evalue_refs(vE
[j
]);
2211 free_evalue_refs(&mone
);
2214 virtual void handle_polar(Polyhedron
*P
, int sign
);
2215 void reduce(evalue
*factor
, vec_ZZ
& num
, evalue
** E_num
,
2219 static evalue
* new_zero_ep()
2224 evalue_set_si(EP
, 0, 1);
2228 void lattice_point(Param_Vertices
*V
, Polyhedron
*C
, vec_ZZ
& num
,
2231 unsigned nparam
= V
->Vertex
->NbColumns
- 2;
2232 unsigned dim
= C
->Dimension
;
2234 vertex
.SetLength(nparam
+1);
2239 value_set_si(lcm
, 1);
2241 for (int j
= 0; j
< V
->Vertex
->NbRows
; ++j
) {
2242 value_lcm(lcm
, V
->Vertex
->p
[j
][nparam
+1], &lcm
);
2245 if (value_notone_p(lcm
)) {
2246 Matrix
* mv
= Matrix_Alloc(dim
, nparam
+1);
2247 for (int j
= 0 ; j
< dim
; ++j
) {
2248 value_division(tmp
, lcm
, V
->Vertex
->p
[j
][nparam
+1]);
2249 Vector_Scale(V
->Vertex
->p
[j
], mv
->p
[j
], tmp
, nparam
+1);
2252 Matrix
* Rays
= rays2(C
);
2253 Matrix
*T
= Transpose(Rays
);
2254 Matrix
*T2
= Matrix_Copy(T
);
2255 Matrix
*inv
= Matrix_Alloc(T2
->NbRows
, T2
->NbColumns
);
2256 int ok
= Matrix_Inverse(T2
, inv
);
2260 Matrix
*L
= Matrix_Alloc(inv
->NbRows
, mv
->NbColumns
);
2261 Matrix_Product(inv
, mv
, L
);
2270 evalue
*remainders
[dim
];
2271 for (int i
= 0; i
< dim
; ++i
) {
2272 remainders
[i
] = new_zero_ep();
2274 ceil(L
->p
[i
], nparam
+1, lcm
, one
, remainders
[i
], 0);
2279 for (int i
= 0; i
< V
->Vertex
->NbRows
; ++i
) {
2280 values2zz(mv
->p
[i
], vertex
, nparam
+1);
2281 E_vertex
[i
] = multi_monom(vertex
);
2284 value_set_si(f
.x
.n
, 1);
2285 value_assign(f
.d
, lcm
);
2287 emul(&f
, E_vertex
[i
]);
2289 for (int j
= 0; j
< dim
; ++j
) {
2290 if (value_zero_p(T
->p
[i
][j
]))
2294 evalue_copy(&cp
, remainders
[j
]);
2295 if (value_notone_p(T
->p
[i
][j
])) {
2296 value_set_si(f
.d
, 1);
2297 value_assign(f
.x
.n
, T
->p
[i
][j
]);
2300 eadd(&cp
, E_vertex
[i
]);
2301 free_evalue_refs(&cp
);
2304 for (int i
= 0; i
< dim
; ++i
) {
2305 free_evalue_refs(remainders
[i
]);
2306 free(remainders
[i
]);
2309 free_evalue_refs(&f
);
2320 for (int i
= 0; i
< V
->Vertex
->NbRows
; ++i
) {
2322 if (First_Non_Zero(V
->Vertex
->p
[i
], nparam
) == -1) {
2324 value2zz(V
->Vertex
->p
[i
][nparam
], num
[i
]);
2326 values2zz(V
->Vertex
->p
[i
], vertex
, nparam
+1);
2327 E_vertex
[i
] = multi_monom(vertex
);
2333 struct E_poly_term
{
2338 void ienumerator::reduce(
2339 evalue
*factor
, vec_ZZ
& num
, evalue
** E_num
,
2342 unsigned len
= den_f
.NumRows(); // number of factors in den
2343 unsigned dim
= num
.length();
2346 eadd(factor
, vE
[_i
]);
2351 den_s
.SetLength(len
);
2353 den_r
.SetDims(len
, dim
-1);
2358 for (r
= 0; r
< len
; ++r
) {
2359 den_s
[r
] = den_f
[r
][i
];
2360 for (k
= 0; k
<= dim
-1; ++k
)
2362 den_r
[r
][k
-(k
>i
)] = den_f
[r
][k
];
2367 num_p
.SetLength(dim
-1);
2368 evalue
* E_num_p
[dim
-1];
2369 for (k
= 0 ; k
<= dim
-1; ++k
)
2371 num_p
[k
-(k
>i
)] = num
[k
];
2372 E_num_p
[k
-(k
>i
)] = E_num
[k
];
2376 den_p
.SetLength(len
);
2380 normalize(one
, num_s
, num_p
, den_s
, den_p
, den_r
);
2382 emul(&mone
, factor
);
2386 for (int k
= 0; k
< len
; ++k
) {
2389 else if (den_s
[k
] == 0)
2392 if (no_param
== 0) {
2393 reduce(factor
, num_p
, E_num_p
, den_r
);
2397 pden
.SetDims(only_param
, dim
-1);
2399 for (k
= 0, l
= 0; k
< len
; ++k
)
2401 pden
[l
++] = den_r
[k
];
2403 for (k
= 0; k
< len
; ++k
)
2407 dpoly
n(no_param
, num_s
);
2408 dpoly
D(no_param
, den_s
[k
], 1);
2409 for ( ; ++k
< len
; )
2410 if (den_p
[k
] == 0) {
2411 dpoly
fact(no_param
, den_s
[k
], 1);
2416 if (no_param
+ only_param
== len
)
2417 r
= new dpoly_r(n
, len
);
2419 for (k
= 0; k
< len
; ++k
) {
2420 if (den_s
[k
] == 0 || den_p
[k
] == 0)
2423 dpoly
pd(no_param
-1, den_s
[k
], 1);
2426 for (l
= 0; l
< k
; ++l
)
2427 if (den_r
[l
] == den_r
[k
])
2431 r
= new dpoly_r(n
, pd
, l
, len
);
2433 dpoly_r
*nr
= new dpoly_r(r
, pd
, l
, len
);
2439 dpoly_r
*rc
= r
->div(D
);
2442 if (E_num
[i
] == 0) {
2443 int common
= pden
.NumRows();
2444 vector
< dpoly_r_term
* >& final
= r
->c
[r
->len
-1];
2450 zz2value(r
->denom
, f
.d
);
2451 for (int j
= 0; j
< final
.size(); ++j
) {
2452 if (final
[j
]->coeff
== 0)
2455 pden
.SetDims(rows
, pden
.NumCols());
2456 for (int k
= 0; k
< r
->dim
; ++k
) {
2457 int n
= final
[j
]->powers
[k
];
2460 pden
.SetDims(rows
+n
, pden
.NumCols());
2461 for (int l
= 0; l
< n
; ++l
)
2462 pden
[rows
+l
] = den_r
[k
];
2466 evalue_copy(&t
, factor
);
2467 zz2value(final
[j
]->coeff
, f
.x
.n
);
2469 reduce(&t
, num_p
, E_num_p
, pden
);
2470 free_evalue_refs(&t
);
2472 free_evalue_refs(&f
);
2474 evalue cum
; // factor * 1 * E_num[i]/1 * (E_num[i]-1)/2 *...
2476 evalue_copy(&cum
, factor
);
2480 value_set_si(f
.d
, 1);
2481 value_set_si(f
.x
.n
, 1);
2482 evalue t
; // E_num[i] - (m-1)
2484 evalue_copy(&t
, E_num
[i
]);
2487 for (cst
= &t
; value_zero_p(cst
->d
); ) {
2488 if (cst
->x
.p
->type
== fractional
)
2489 cst
= &cst
->x
.p
->arr
[1];
2491 cst
= &cst
->x
.p
->arr
[0];
2494 vector
<E_poly_term
*> terms
;
2495 for (int m
= 0; m
< r
->len
; ++m
) {
2498 value_set_si(f
.d
, m
);
2501 value_substract(cst
->x
.n
, cst
->x
.n
, cst
->d
);
2508 vector
< dpoly_r_term
* >& current
= r
->c
[r
->len
-1-m
];
2509 for (int j
= 0; j
< current
.size(); ++j
) {
2510 if (current
[j
]->coeff
== 0)
2512 evalue
*f2
= new evalue
;
2514 value_init(f2
->x
.n
);
2515 zz2value(current
[j
]->coeff
, f2
->x
.n
);
2516 zz2value(r
->denom
, f2
->d
);
2519 for (k
= 0; k
< terms
.size(); ++k
) {
2520 if (memcmp(terms
[k
]->powers
, current
[j
]->powers
,
2521 r
->dim
* sizeof(int)) == 0) {
2522 eadd(f2
, terms
[k
]->E
);
2523 free_evalue_refs(f2
);
2528 if (k
>= terms
.size()) {
2529 E_poly_term
*ET
= new E_poly_term
;
2530 ET
->powers
= new int[r
->dim
];
2531 memcpy(ET
->powers
, current
[j
]->powers
,
2532 r
->dim
* sizeof(int));
2534 terms
.push_back(ET
);
2538 free_evalue_refs(&f
);
2539 free_evalue_refs(&t
);
2540 free_evalue_refs(&cum
);
2542 int common
= pden
.NumRows();
2544 for (int j
= 0; j
< terms
.size(); ++j
) {
2546 pden
.SetDims(rows
, pden
.NumCols());
2547 for (int k
= 0; k
< r
->dim
; ++k
) {
2548 int n
= terms
[j
]->powers
[k
];
2551 pden
.SetDims(rows
+n
, pden
.NumCols());
2552 for (int l
= 0; l
< n
; ++l
)
2553 pden
[rows
+l
] = den_r
[k
];
2556 reduce(terms
[j
]->E
, num_p
, E_num_p
, pden
);
2557 free_evalue_refs(terms
[j
]->E
);
2559 delete [] terms
[j
]->powers
;
2567 static int type_offset(enode
*p
)
2569 return p
->type
== fractional
? 1 :
2570 p
->type
== flooring
? 1 : 0;
2573 static int edegree(evalue
*e
)
2578 if (value_notzero_p(e
->d
))
2582 int i
= type_offset(p
);
2583 if (p
->size
-i
-1 > d
)
2584 d
= p
->size
- i
- 1;
2585 for (; i
< p
->size
; i
++) {
2586 int d2
= edegree(&p
->arr
[i
]);
2593 void ienumerator::handle_polar(Polyhedron
*C
, int s
)
2595 assert(C
->NbRays
-1 == dim
);
2599 lattice_point(V
, C
, vertex
, E_vertex
);
2602 for (r
= 0; r
< dim
; ++r
)
2603 values2zz(C
->Ray
[r
]+1, den
[r
], dim
);
2607 evalue_set_si(&one
, s
, 1);
2608 reduce(&one
, vertex
, E_vertex
, den
);
2609 free_evalue_refs(&one
);
2611 for (int i
= 0; i
< dim
; ++i
)
2613 free_evalue_refs(E_vertex
[i
]);
2619 char * test[] = {"a", "b"};
2622 evalue_copy(&E, vE[_i]);
2623 frac2floor_in_domain(&E, pVD);
2624 printf("***** Curr value:");
2625 print_evalue(stdout, &E, test);
2626 fprintf(stdout, "\n");
2632 #ifdef HAVE_CORRECT_VERTICES
2633 static inline Param_Polyhedron
*Polyhedron2Param_SD(Polyhedron
**Din
,
2634 Polyhedron
*Cin
,int WS
,Polyhedron
**CEq
,Matrix
**CT
)
2636 return Polyhedron2Param_SimplifiedDomain(Din
, Cin
, WS
, CEq
, CT
);
2639 static Param_Polyhedron
*Polyhedron2Param_SD(Polyhedron
**Din
,
2640 Polyhedron
*Cin
,int WS
,Polyhedron
**CEq
,Matrix
**CT
)
2642 static char data
[] = " 1 0 0 0 0 1 -18 "
2643 " 1 0 0 -20 0 19 1 "
2644 " 1 0 1 20 0 -20 16 "
2647 " 1 4 -20 0 0 -1 23 "
2648 " 1 -4 20 0 0 1 -22 "
2649 " 1 0 1 0 20 -20 16 "
2650 " 1 0 0 0 -20 19 1 ";
2651 static int checked
= 0;
2656 Matrix
*M
= Matrix_Alloc(9, 7);
2657 for (i
= 0; i
< 9; ++i
)
2658 for (int j
= 0; j
< 7; ++j
) {
2659 sscanf(p
, "%d%n", &v
, &n
);
2661 value_set_si(M
->p
[i
][j
], v
);
2663 Polyhedron
*P
= Constraints2Polyhedron(M
, 1024);
2666 Polyhedron
*U
= Universe_Polyhedron(1);
2668 Param_Polyhedron
*PP
=
2669 Polyhedron2Param_SimplifiedDomain(&P
, U
, 1024, NULL
, NULL
);
2672 Polyhedron_Free(P2
);
2675 for (i
= 0, V
= PP
->V
; V
; ++i
, V
= V
->next
)
2678 Param_Polyhedron_Free(PP
);
2680 fprintf(stderr
, "WARNING: results may be incorrect\n");
2682 "WARNING: use latest version of PolyLib to remove this warning\n");
2686 return Polyhedron2Param_SimplifiedDomain(Din
, Cin
, WS
, CEq
, CT
);
2690 evalue
* barvinok_enumerate_ev(Polyhedron
*P
, Polyhedron
* C
, unsigned MaxRays
)
2692 //P = unfringe(P, MaxRays);
2693 Polyhedron
*CEq
= NULL
, *rVD
, *pVD
, *CA
;
2695 Param_Polyhedron
*PP
= NULL
;
2696 Param_Domain
*D
, *next
;
2699 unsigned nparam
= C
->Dimension
;
2701 ALLOC(evalue
, eres
);
2702 value_init(eres
->d
);
2703 value_set_si(eres
->d
, 0);
2706 value_init(factor
.d
);
2707 evalue_set_si(&factor
, 1, 1);
2709 CA
= align_context(C
, P
->Dimension
, MaxRays
);
2710 P
= DomainIntersection(P
, CA
, MaxRays
);
2711 Polyhedron_Free(CA
);
2713 if (C
->Dimension
== 0 || emptyQ(P
)) {
2715 eres
->x
.p
= new_enode(partition
, 2, C
->Dimension
);
2716 EVALUE_SET_DOMAIN(eres
->x
.p
->arr
[0],
2717 DomainConstraintSimplify(CEq
? CEq
: Polyhedron_Copy(C
), MaxRays
));
2718 value_set_si(eres
->x
.p
->arr
[1].d
, 1);
2719 value_init(eres
->x
.p
->arr
[1].x
.n
);
2721 value_set_si(eres
->x
.p
->arr
[1].x
.n
, 0);
2723 barvinok_count(P
, &eres
->x
.p
->arr
[1].x
.n
, MaxRays
);
2725 emul(&factor
, eres
);
2726 reduce_evalue(eres
);
2727 free_evalue_refs(&factor
);
2732 Param_Polyhedron_Free(PP
);
2736 if (Polyhedron_is_infinite(P
, nparam
))
2741 P
= remove_equalities_p(P
, P
->Dimension
-nparam
, &f
);
2745 if (P
->Dimension
== nparam
) {
2747 P
= Universe_Polyhedron(0);
2751 Polyhedron
*Q
= ParamPolyhedron_Reduce(P
, P
->Dimension
-nparam
, &factor
);
2754 if (Q
->Dimension
== nparam
) {
2756 P
= Universe_Polyhedron(0);
2761 Polyhedron
*oldP
= P
;
2762 PP
= Polyhedron2Param_SD(&P
,C
,MaxRays
,&CEq
,&CT
);
2764 Polyhedron_Free(oldP
);
2766 if (isIdentity(CT
)) {
2770 assert(CT
->NbRows
!= CT
->NbColumns
);
2771 if (CT
->NbRows
== 1) // no more parameters
2773 nparam
= CT
->NbRows
- 1;
2776 unsigned dim
= P
->Dimension
- nparam
;
2778 #ifdef USE_INCREMENTAL
2779 ienumerator
et(P
, dim
, PP
->nbV
);
2781 enumerator
et(P
, dim
, PP
->nbV
);
2785 for (nd
= 0, D
=PP
->D
; D
; ++nd
, D
=D
->next
);
2786 struct section
{ Polyhedron
*D
; evalue E
; };
2787 section
*s
= new section
[nd
];
2788 Polyhedron
**fVD
= new Polyhedron_p
[nd
];
2790 for(nd
= 0, D
=PP
->D
; D
; D
=next
) {
2793 Polyhedron
*rVD
= reduce_domain(D
->Domain
, CT
, CEq
,
2798 pVD
= CT
? DomainImage(rVD
,CT
,MaxRays
) : rVD
;
2800 value_init(s
[nd
].E
.d
);
2801 evalue_set_si(&s
[nd
].E
, 0, 1);
2803 FORALL_PVertex_in_ParamPolyhedron(V
,D
,PP
) // _i is internal counter
2805 et
.decompose_at(V
, _i
, MaxRays
);
2806 eadd(et
.vE
[_i
] , &s
[nd
].E
);
2807 END_FORALL_PVertex_in_ParamPolyhedron
;
2808 reduce_in_domain(&s
[nd
].E
, pVD
);
2811 addeliminatedparams_evalue(&s
[nd
].E
, CT
);
2819 evalue_set_si(eres
, 0, 1);
2821 eres
->x
.p
= new_enode(partition
, 2*nd
, C
->Dimension
);
2822 for (int j
= 0; j
< nd
; ++j
) {
2823 EVALUE_SET_DOMAIN(eres
->x
.p
->arr
[2*j
], s
[j
].D
);
2824 value_clear(eres
->x
.p
->arr
[2*j
+1].d
);
2825 eres
->x
.p
->arr
[2*j
+1] = s
[j
].E
;
2826 Domain_Free(fVD
[j
]);
2834 Polyhedron_Free(CEq
);
2839 Enumeration
* barvinok_enumerate(Polyhedron
*P
, Polyhedron
* C
, unsigned MaxRays
)
2841 evalue
*EP
= barvinok_enumerate_ev(P
, C
, MaxRays
);
2843 return partition2enumeration(EP
);
2846 static void SwapColumns(Value
**V
, int n
, int i
, int j
)
2848 for (int r
= 0; r
< n
; ++r
)
2849 value_swap(V
[r
][i
], V
[r
][j
]);
2852 static void SwapColumns(Polyhedron
*P
, int i
, int j
)
2854 SwapColumns(P
->Constraint
, P
->NbConstraints
, i
, j
);
2855 SwapColumns(P
->Ray
, P
->NbRays
, i
, j
);
2858 static void negative_test_constraint(Value
*l
, Value
*u
, Value
*c
, int pos
,
2861 value_oppose(*v
, u
[pos
+1]);
2862 Vector_Combine(l
+1, u
+1, c
+1, *v
, l
[pos
+1], len
-1);
2863 value_multiply(*v
, *v
, l
[pos
+1]);
2864 value_substract(c
[len
-1], c
[len
-1], *v
);
2865 value_set_si(*v
, -1);
2866 Vector_Scale(c
+1, c
+1, *v
, len
-1);
2867 value_decrement(c
[len
-1], c
[len
-1]);
2868 ConstraintSimplify(c
, c
, len
, v
);
2871 static bool parallel_constraints(Value
*l
, Value
*u
, Value
*c
, int pos
,
2880 Vector_Gcd(&l
[1+pos
], len
, &g1
);
2881 Vector_Gcd(&u
[1+pos
], len
, &g2
);
2882 Vector_Combine(l
+1+pos
, u
+1+pos
, c
+1, g2
, g1
, len
);
2883 parallel
= First_Non_Zero(c
+1, len
) == -1;
2891 static void negative_test_constraint7(Value
*l
, Value
*u
, Value
*c
, int pos
,
2892 int exist
, int len
, Value
*v
)
2897 Vector_Gcd(&u
[1+pos
], exist
, v
);
2898 Vector_Gcd(&l
[1+pos
], exist
, &g
);
2899 Vector_Combine(l
+1, u
+1, c
+1, *v
, g
, len
-1);
2900 value_multiply(*v
, *v
, g
);
2901 value_substract(c
[len
-1], c
[len
-1], *v
);
2902 value_set_si(*v
, -1);
2903 Vector_Scale(c
+1, c
+1, *v
, len
-1);
2904 value_decrement(c
[len
-1], c
[len
-1]);
2905 ConstraintSimplify(c
, c
, len
, v
);
2910 static void oppose_constraint(Value
*c
, int len
, Value
*v
)
2912 value_set_si(*v
, -1);
2913 Vector_Scale(c
+1, c
+1, *v
, len
-1);
2914 value_decrement(c
[len
-1], c
[len
-1]);
2917 static bool SplitOnConstraint(Polyhedron
*P
, int i
, int l
, int u
,
2918 int nvar
, int len
, int exist
, int MaxRays
,
2919 Vector
*row
, Value
& f
, bool independent
,
2920 Polyhedron
**pos
, Polyhedron
**neg
)
2922 negative_test_constraint(P
->Constraint
[l
], P
->Constraint
[u
],
2923 row
->p
, nvar
+i
, len
, &f
);
2924 *neg
= AddConstraints(row
->p
, 1, P
, MaxRays
);
2926 /* We found an independent, but useless constraint
2927 * Maybe we should detect this earlier and not
2928 * mark the variable as INDEPENDENT
2930 if (emptyQ((*neg
))) {
2931 Polyhedron_Free(*neg
);
2935 oppose_constraint(row
->p
, len
, &f
);
2936 *pos
= AddConstraints(row
->p
, 1, P
, MaxRays
);
2938 if (emptyQ((*pos
))) {
2939 Polyhedron_Free(*neg
);
2940 Polyhedron_Free(*pos
);
2948 * unimodularly transform P such that constraint r is transformed
2949 * into a constraint that involves only a single (the first)
2950 * existential variable
2953 static Polyhedron
*rotate_along(Polyhedron
*P
, int r
, int nvar
, int exist
,
2959 Vector
*row
= Vector_Alloc(exist
);
2960 Vector_Copy(P
->Constraint
[r
]+1+nvar
, row
->p
, exist
);
2961 Vector_Gcd(row
->p
, exist
, &g
);
2962 if (value_notone_p(g
))
2963 Vector_AntiScale(row
->p
, row
->p
, g
, exist
);
2966 Matrix
*M
= unimodular_complete(row
);
2967 Matrix
*M2
= Matrix_Alloc(P
->Dimension
+1, P
->Dimension
+1);
2968 for (r
= 0; r
< nvar
; ++r
)
2969 value_set_si(M2
->p
[r
][r
], 1);
2970 for ( ; r
< nvar
+exist
; ++r
)
2971 Vector_Copy(M
->p
[r
-nvar
], M2
->p
[r
]+nvar
, exist
);
2972 for ( ; r
< P
->Dimension
+1; ++r
)
2973 value_set_si(M2
->p
[r
][r
], 1);
2974 Polyhedron
*T
= Polyhedron_Image(P
, M2
, MaxRays
);
2983 static bool SplitOnVar(Polyhedron
*P
, int i
,
2984 int nvar
, int len
, int exist
, int MaxRays
,
2985 Vector
*row
, Value
& f
, bool independent
,
2986 Polyhedron
**pos
, Polyhedron
**neg
)
2990 for (int l
= P
->NbEq
; l
< P
->NbConstraints
; ++l
) {
2991 if (value_negz_p(P
->Constraint
[l
][nvar
+i
+1]))
2995 for (j
= 0; j
< exist
; ++j
)
2996 if (j
!= i
&& value_notzero_p(P
->Constraint
[l
][nvar
+j
+1]))
3002 for (int u
= P
->NbEq
; u
< P
->NbConstraints
; ++u
) {
3003 if (value_posz_p(P
->Constraint
[u
][nvar
+i
+1]))
3007 for (j
= 0; j
< exist
; ++j
)
3008 if (j
!= i
&& value_notzero_p(P
->Constraint
[u
][nvar
+j
+1]))
3014 if (SplitOnConstraint(P
, i
, l
, u
,
3015 nvar
, len
, exist
, MaxRays
,
3016 row
, f
, independent
,
3020 SwapColumns(*neg
, nvar
+1, nvar
+1+i
);
3030 static bool double_bound_pair(Polyhedron
*P
, int nvar
, int exist
,
3031 int i
, int l1
, int l2
,
3032 Polyhedron
**pos
, Polyhedron
**neg
)
3036 Vector
*row
= Vector_Alloc(P
->Dimension
+2);
3037 value_set_si(row
->p
[0], 1);
3038 value_oppose(f
, P
->Constraint
[l1
][nvar
+i
+1]);
3039 Vector_Combine(P
->Constraint
[l1
]+1, P
->Constraint
[l2
]+1,
3041 P
->Constraint
[l2
][nvar
+i
+1], f
,
3043 ConstraintSimplify(row
->p
, row
->p
, P
->Dimension
+2, &f
);
3044 *pos
= AddConstraints(row
->p
, 1, P
, 0);
3045 value_set_si(f
, -1);
3046 Vector_Scale(row
->p
+1, row
->p
+1, f
, P
->Dimension
+1);
3047 value_decrement(row
->p
[P
->Dimension
+1], row
->p
[P
->Dimension
+1]);
3048 *neg
= AddConstraints(row
->p
, 1, P
, 0);
3052 return !emptyQ((*pos
)) && !emptyQ((*neg
));
3055 static bool double_bound(Polyhedron
*P
, int nvar
, int exist
,
3056 Polyhedron
**pos
, Polyhedron
**neg
)
3058 for (int i
= 0; i
< exist
; ++i
) {
3060 for (l1
= P
->NbEq
; l1
< P
->NbConstraints
; ++l1
) {
3061 if (value_negz_p(P
->Constraint
[l1
][nvar
+i
+1]))
3063 for (l2
= l1
+ 1; l2
< P
->NbConstraints
; ++l2
) {
3064 if (value_negz_p(P
->Constraint
[l2
][nvar
+i
+1]))
3066 if (double_bound_pair(P
, nvar
, exist
, i
, l1
, l2
, pos
, neg
))
3070 for (l1
= P
->NbEq
; l1
< P
->NbConstraints
; ++l1
) {
3071 if (value_posz_p(P
->Constraint
[l1
][nvar
+i
+1]))
3073 if (l1
< P
->NbConstraints
)
3074 for (l2
= l1
+ 1; l2
< P
->NbConstraints
; ++l2
) {
3075 if (value_posz_p(P
->Constraint
[l2
][nvar
+i
+1]))
3077 if (double_bound_pair(P
, nvar
, exist
, i
, l1
, l2
, pos
, neg
))
3089 INDEPENDENT
= 1 << 2,
3093 static evalue
* enumerate_or(Polyhedron
*D
,
3094 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
3097 fprintf(stderr
, "\nER: Or\n");
3098 #endif /* DEBUG_ER */
3100 Polyhedron
*N
= D
->next
;
3103 barvinok_enumerate_e(D
, exist
, nparam
, MaxRays
);
3106 for (D
= N
; D
; D
= N
) {
3111 barvinok_enumerate_e(D
, exist
, nparam
, MaxRays
);
3114 free_evalue_refs(EN
);
3124 static evalue
* enumerate_sum(Polyhedron
*P
,
3125 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
3127 int nvar
= P
->Dimension
- exist
- nparam
;
3128 int toswap
= nvar
< exist
? nvar
: exist
;
3129 for (int i
= 0; i
< toswap
; ++i
)
3130 SwapColumns(P
, 1 + i
, nvar
+exist
- i
);
3134 fprintf(stderr
, "\nER: Sum\n");
3135 #endif /* DEBUG_ER */
3137 evalue
*EP
= barvinok_enumerate_e(P
, exist
, nparam
, MaxRays
);
3139 for (int i
= 0; i
< /* nvar */ nparam
; ++i
) {
3140 Matrix
*C
= Matrix_Alloc(1, 1 + nparam
+ 1);
3141 value_set_si(C
->p
[0][0], 1);
3143 value_init(split
.d
);
3144 value_set_si(split
.d
, 0);
3145 split
.x
.p
= new_enode(partition
, 4, nparam
);
3146 value_set_si(C
->p
[0][1+i
], 1);
3147 Matrix
*C2
= Matrix_Copy(C
);
3148 EVALUE_SET_DOMAIN(split
.x
.p
->arr
[0],
3149 Constraints2Polyhedron(C2
, MaxRays
));
3151 evalue_set_si(&split
.x
.p
->arr
[1], 1, 1);
3152 value_set_si(C
->p
[0][1+i
], -1);
3153 value_set_si(C
->p
[0][1+nparam
], -1);
3154 EVALUE_SET_DOMAIN(split
.x
.p
->arr
[2],
3155 Constraints2Polyhedron(C
, MaxRays
));
3156 evalue_set_si(&split
.x
.p
->arr
[3], 1, 1);
3158 free_evalue_refs(&split
);
3162 evalue_range_reduction(EP
);
3164 evalue_frac2floor(EP
);
3166 evalue
*sum
= esum(EP
, nvar
);
3168 free_evalue_refs(EP
);
3172 evalue_range_reduction(EP
);
3177 static evalue
* split_sure(Polyhedron
*P
, Polyhedron
*S
,
3178 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
3180 int nvar
= P
->Dimension
- exist
- nparam
;
3182 Matrix
*M
= Matrix_Alloc(exist
, S
->Dimension
+2);
3183 for (int i
= 0; i
< exist
; ++i
)
3184 value_set_si(M
->p
[i
][nvar
+i
+1], 1);
3186 S
= DomainAddRays(S
, M
, MaxRays
);
3188 Polyhedron
*F
= DomainAddRays(P
, M
, MaxRays
);
3189 Polyhedron
*D
= DomainDifference(F
, S
, MaxRays
);
3191 D
= Disjoint_Domain(D
, 0, MaxRays
);
3196 M
= Matrix_Alloc(P
->Dimension
+1-exist
, P
->Dimension
+1);
3197 for (int j
= 0; j
< nvar
; ++j
)
3198 value_set_si(M
->p
[j
][j
], 1);
3199 for (int j
= 0; j
< nparam
+1; ++j
)
3200 value_set_si(M
->p
[nvar
+j
][nvar
+exist
+j
], 1);
3201 Polyhedron
*T
= Polyhedron_Image(S
, M
, MaxRays
);
3202 evalue
*EP
= barvinok_enumerate_e(T
, 0, nparam
, MaxRays
);
3207 for (Polyhedron
*Q
= D
; Q
; Q
= Q
->next
) {
3208 Polyhedron
*N
= Q
->next
;
3210 T
= DomainIntersection(P
, Q
, MaxRays
);
3211 evalue
*E
= barvinok_enumerate_e(T
, exist
, nparam
, MaxRays
);
3213 free_evalue_refs(E
);
3222 static evalue
* enumerate_sure(Polyhedron
*P
,
3223 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
3227 int nvar
= P
->Dimension
- exist
- nparam
;
3233 for (i
= 0; i
< exist
; ++i
) {
3234 Matrix
*M
= Matrix_Alloc(S
->NbConstraints
, S
->Dimension
+2);
3236 value_set_si(lcm
, 1);
3237 for (int j
= 0; j
< S
->NbConstraints
; ++j
) {
3238 if (value_negz_p(S
->Constraint
[j
][1+nvar
+i
]))
3240 if (value_one_p(S
->Constraint
[j
][1+nvar
+i
]))
3242 value_lcm(lcm
, S
->Constraint
[j
][1+nvar
+i
], &lcm
);
3245 for (int j
= 0; j
< S
->NbConstraints
; ++j
) {
3246 if (value_negz_p(S
->Constraint
[j
][1+nvar
+i
]))
3248 if (value_one_p(S
->Constraint
[j
][1+nvar
+i
]))
3250 value_division(f
, lcm
, S
->Constraint
[j
][1+nvar
+i
]);
3251 Vector_Scale(S
->Constraint
[j
], M
->p
[c
], f
, S
->Dimension
+2);
3252 value_substract(M
->p
[c
][S
->Dimension
+1],
3253 M
->p
[c
][S
->Dimension
+1],
3255 value_increment(M
->p
[c
][S
->Dimension
+1],
3256 M
->p
[c
][S
->Dimension
+1]);
3260 S
= AddConstraints(M
->p
[0], c
, S
, MaxRays
);
3275 fprintf(stderr
, "\nER: Sure\n");
3276 #endif /* DEBUG_ER */
3278 return split_sure(P
, S
, exist
, nparam
, MaxRays
);
3281 static evalue
* enumerate_sure2(Polyhedron
*P
,
3282 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
3284 int nvar
= P
->Dimension
- exist
- nparam
;
3286 for (r
= 0; r
< P
->NbRays
; ++r
)
3287 if (value_one_p(P
->Ray
[r
][0]) &&
3288 value_one_p(P
->Ray
[r
][P
->Dimension
+1]))
3294 Matrix
*M
= Matrix_Alloc(nvar
+ 1 + nparam
, P
->Dimension
+2);
3295 for (int i
= 0; i
< nvar
; ++i
)
3296 value_set_si(M
->p
[i
][1+i
], 1);
3297 for (int i
= 0; i
< nparam
; ++i
)
3298 value_set_si(M
->p
[i
+nvar
][1+nvar
+exist
+i
], 1);
3299 Vector_Copy(P
->Ray
[r
]+1+nvar
, M
->p
[nvar
+nparam
]+1+nvar
, exist
);
3300 value_set_si(M
->p
[nvar
+nparam
][0], 1);
3301 value_set_si(M
->p
[nvar
+nparam
][P
->Dimension
+1], 1);
3302 Polyhedron
* F
= Rays2Polyhedron(M
, MaxRays
);
3305 Polyhedron
*I
= DomainIntersection(F
, P
, MaxRays
);
3309 fprintf(stderr
, "\nER: Sure2\n");
3310 #endif /* DEBUG_ER */
3312 return split_sure(P
, I
, exist
, nparam
, MaxRays
);
3315 static evalue
* enumerate_cyclic(Polyhedron
*P
,
3316 unsigned exist
, unsigned nparam
,
3317 evalue
* EP
, int r
, int p
, unsigned MaxRays
)
3319 int nvar
= P
->Dimension
- exist
- nparam
;
3321 /* If EP in its fractional maps only contains references
3322 * to the remainder parameter with appropriate coefficients
3323 * then we could in principle avoid adding existentially
3324 * quantified variables to the validity domains.
3325 * We'd have to replace the remainder by m { p/m }
3326 * and multiply with an appropriate factor that is one
3327 * only in the appropriate range.
3328 * This last multiplication can be avoided if EP
3329 * has a single validity domain with no (further)
3330 * constraints on the remainder parameter
3333 Matrix
*CT
= Matrix_Alloc(nparam
+1, nparam
+3);
3334 Matrix
*M
= Matrix_Alloc(1, 1+nparam
+3);
3335 for (int j
= 0; j
< nparam
; ++j
)
3337 value_set_si(CT
->p
[j
][j
], 1);
3338 value_set_si(CT
->p
[p
][nparam
+1], 1);
3339 value_set_si(CT
->p
[nparam
][nparam
+2], 1);
3340 value_set_si(M
->p
[0][1+p
], -1);
3341 value_absolute(M
->p
[0][1+nparam
], P
->Ray
[0][1+nvar
+exist
+p
]);
3342 value_set_si(M
->p
[0][1+nparam
+1], 1);
3343 Polyhedron
*CEq
= Constraints2Polyhedron(M
, 1);
3345 addeliminatedparams_enum(EP
, CT
, CEq
, MaxRays
, nparam
);
3346 Polyhedron_Free(CEq
);
3352 static void enumerate_vd_add_ray(evalue
*EP
, Matrix
*Rays
, unsigned MaxRays
)
3354 if (value_notzero_p(EP
->d
))
3357 assert(EP
->x
.p
->type
== partition
);
3358 assert(EP
->x
.p
->pos
== EVALUE_DOMAIN(EP
->x
.p
->arr
[0])->Dimension
);
3359 for (int i
= 0; i
< EP
->x
.p
->size
/2; ++i
) {
3360 Polyhedron
*D
= EVALUE_DOMAIN(EP
->x
.p
->arr
[2*i
]);
3361 Polyhedron
*N
= DomainAddRays(D
, Rays
, MaxRays
);
3362 EVALUE_SET_DOMAIN(EP
->x
.p
->arr
[2*i
], N
);
3367 static evalue
* enumerate_line(Polyhedron
*P
,
3368 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
3374 fprintf(stderr
, "\nER: Line\n");
3375 #endif /* DEBUG_ER */
3377 int nvar
= P
->Dimension
- exist
- nparam
;
3379 for (i
= 0; i
< nparam
; ++i
)
3380 if (value_notzero_p(P
->Ray
[0][1+nvar
+exist
+i
]))
3383 for (j
= i
+1; j
< nparam
; ++j
)
3384 if (value_notzero_p(P
->Ray
[0][1+nvar
+exist
+i
]))
3386 assert(j
>= nparam
); // for now
3388 Matrix
*M
= Matrix_Alloc(2, P
->Dimension
+2);
3389 value_set_si(M
->p
[0][0], 1);
3390 value_set_si(M
->p
[0][1+nvar
+exist
+i
], 1);
3391 value_set_si(M
->p
[1][0], 1);
3392 value_set_si(M
->p
[1][1+nvar
+exist
+i
], -1);
3393 value_absolute(M
->p
[1][1+P
->Dimension
], P
->Ray
[0][1+nvar
+exist
+i
]);
3394 value_decrement(M
->p
[1][1+P
->Dimension
], M
->p
[1][1+P
->Dimension
]);
3395 Polyhedron
*S
= AddConstraints(M
->p
[0], 2, P
, MaxRays
);
3396 evalue
*EP
= barvinok_enumerate_e(S
, exist
, nparam
, MaxRays
);
3400 return enumerate_cyclic(P
, exist
, nparam
, EP
, 0, i
, MaxRays
);
3403 static int single_param_pos(Polyhedron
*P
, unsigned exist
, unsigned nparam
,
3406 int nvar
= P
->Dimension
- exist
- nparam
;
3407 if (First_Non_Zero(P
->Ray
[r
]+1, nvar
) != -1)
3409 int i
= First_Non_Zero(P
->Ray
[r
]+1+nvar
+exist
, nparam
);
3412 if (First_Non_Zero(P
->Ray
[r
]+1+nvar
+exist
+1, nparam
-i
-1) != -1)
3417 static evalue
* enumerate_remove_ray(Polyhedron
*P
, int r
,
3418 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
3421 fprintf(stderr
, "\nER: RedundantRay\n");
3422 #endif /* DEBUG_ER */
3426 value_set_si(one
, 1);
3427 int len
= P
->NbRays
-1;
3428 Matrix
*M
= Matrix_Alloc(2 * len
, P
->Dimension
+2);
3429 Vector_Copy(P
->Ray
[0], M
->p
[0], r
* (P
->Dimension
+2));
3430 Vector_Copy(P
->Ray
[r
+1], M
->p
[r
], (len
-r
) * (P
->Dimension
+2));
3431 for (int j
= 0; j
< P
->NbRays
; ++j
) {
3434 Vector_Combine(P
->Ray
[j
], P
->Ray
[r
], M
->p
[len
+j
-(j
>r
)],
3435 one
, P
->Ray
[j
][P
->Dimension
+1], P
->Dimension
+2);
3438 P
= Rays2Polyhedron(M
, MaxRays
);
3440 evalue
*EP
= barvinok_enumerate_e(P
, exist
, nparam
, MaxRays
);
3447 static evalue
* enumerate_redundant_ray(Polyhedron
*P
,
3448 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
3450 assert(P
->NbBid
== 0);
3451 int nvar
= P
->Dimension
- exist
- nparam
;
3455 for (int r
= 0; r
< P
->NbRays
; ++r
) {
3456 if (value_notzero_p(P
->Ray
[r
][P
->Dimension
+1]))
3458 int i1
= single_param_pos(P
, exist
, nparam
, r
);
3461 for (int r2
= r
+1; r2
< P
->NbRays
; ++r2
) {
3462 if (value_notzero_p(P
->Ray
[r2
][P
->Dimension
+1]))
3464 int i2
= single_param_pos(P
, exist
, nparam
, r2
);
3470 value_division(m
, P
->Ray
[r
][1+nvar
+exist
+i1
],
3471 P
->Ray
[r2
][1+nvar
+exist
+i1
]);
3472 value_multiply(m
, m
, P
->Ray
[r2
][1+nvar
+exist
+i1
]);
3473 /* r2 divides r => r redundant */
3474 if (value_eq(m
, P
->Ray
[r
][1+nvar
+exist
+i1
])) {
3476 return enumerate_remove_ray(P
, r
, exist
, nparam
, MaxRays
);
3479 value_division(m
, P
->Ray
[r2
][1+nvar
+exist
+i1
],
3480 P
->Ray
[r
][1+nvar
+exist
+i1
]);
3481 value_multiply(m
, m
, P
->Ray
[r
][1+nvar
+exist
+i1
]);
3482 /* r divides r2 => r2 redundant */
3483 if (value_eq(m
, P
->Ray
[r2
][1+nvar
+exist
+i1
])) {
3485 return enumerate_remove_ray(P
, r2
, exist
, nparam
, MaxRays
);
3493 static Polyhedron
*upper_bound(Polyhedron
*P
,
3494 int pos
, Value
*max
, Polyhedron
**R
)
3503 for (Polyhedron
*Q
= P
; Q
; Q
= N
) {
3505 for (r
= 0; r
< P
->NbRays
; ++r
) {
3506 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1]) &&
3507 value_pos_p(P
->Ray
[r
][1+pos
]))
3510 if (r
< P
->NbRays
) {
3518 for (r
= 0; r
< P
->NbRays
; ++r
) {
3519 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1]))
3521 mpz_fdiv_q(v
, P
->Ray
[r
][1+pos
], P
->Ray
[r
][1+P
->Dimension
]);
3522 if ((!Q
->next
&& r
== 0) || value_gt(v
, *max
))
3523 value_assign(*max
, v
);
3530 static evalue
* enumerate_ray(Polyhedron
*P
,
3531 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
3533 assert(P
->NbBid
== 0);
3534 int nvar
= P
->Dimension
- exist
- nparam
;
3537 for (r
= 0; r
< P
->NbRays
; ++r
)
3538 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1]))
3544 for (r2
= r
+1; r2
< P
->NbRays
; ++r2
)
3545 if (value_zero_p(P
->Ray
[r2
][P
->Dimension
+1]))
3547 if (r2
< P
->NbRays
) {
3549 return enumerate_sum(P
, exist
, nparam
, MaxRays
);
3553 fprintf(stderr
, "\nER: Ray\n");
3554 #endif /* DEBUG_ER */
3560 value_set_si(one
, 1);
3561 int i
= single_param_pos(P
, exist
, nparam
, r
);
3562 assert(i
!= -1); // for now;
3564 Matrix
*M
= Matrix_Alloc(P
->NbRays
, P
->Dimension
+2);
3565 for (int j
= 0; j
< P
->NbRays
; ++j
) {
3566 Vector_Combine(P
->Ray
[j
], P
->Ray
[r
], M
->p
[j
],
3567 one
, P
->Ray
[j
][P
->Dimension
+1], P
->Dimension
+2);
3569 Polyhedron
*S
= Rays2Polyhedron(M
, MaxRays
);
3571 Polyhedron
*D
= DomainDifference(P
, S
, MaxRays
);
3573 // Polyhedron_Print(stderr, P_VALUE_FMT, D);
3574 assert(value_pos_p(P
->Ray
[r
][1+nvar
+exist
+i
])); // for now
3576 D
= upper_bound(D
, nvar
+exist
+i
, &m
, &R
);
3580 M
= Matrix_Alloc(2, P
->Dimension
+2);
3581 value_set_si(M
->p
[0][0], 1);
3582 value_set_si(M
->p
[1][0], 1);
3583 value_set_si(M
->p
[0][1+nvar
+exist
+i
], -1);
3584 value_set_si(M
->p
[1][1+nvar
+exist
+i
], 1);
3585 value_assign(M
->p
[0][1+P
->Dimension
], m
);
3586 value_oppose(M
->p
[1][1+P
->Dimension
], m
);
3587 value_addto(M
->p
[1][1+P
->Dimension
], M
->p
[1][1+P
->Dimension
],
3588 P
->Ray
[r
][1+nvar
+exist
+i
]);
3589 value_decrement(M
->p
[1][1+P
->Dimension
], M
->p
[1][1+P
->Dimension
]);
3590 // Matrix_Print(stderr, P_VALUE_FMT, M);
3591 D
= AddConstraints(M
->p
[0], 2, P
, MaxRays
);
3592 // Polyhedron_Print(stderr, P_VALUE_FMT, D);
3593 value_substract(M
->p
[0][1+P
->Dimension
], M
->p
[0][1+P
->Dimension
],
3594 P
->Ray
[r
][1+nvar
+exist
+i
]);
3595 // Matrix_Print(stderr, P_VALUE_FMT, M);
3596 S
= AddConstraints(M
->p
[0], 1, P
, MaxRays
);
3597 // Polyhedron_Print(stderr, P_VALUE_FMT, S);
3600 evalue
*EP
= barvinok_enumerate_e(D
, exist
, nparam
, MaxRays
);
3605 if (value_notone_p(P
->Ray
[r
][1+nvar
+exist
+i
]))
3606 EP
= enumerate_cyclic(P
, exist
, nparam
, EP
, r
, i
, MaxRays
);
3608 M
= Matrix_Alloc(1, nparam
+2);
3609 value_set_si(M
->p
[0][0], 1);
3610 value_set_si(M
->p
[0][1+i
], 1);
3611 enumerate_vd_add_ray(EP
, M
, MaxRays
);
3616 evalue
*E
= barvinok_enumerate_e(S
, exist
, nparam
, MaxRays
);
3618 free_evalue_refs(E
);
3625 evalue
*ER
= enumerate_or(R
, exist
, nparam
, MaxRays
);
3627 free_evalue_refs(ER
);
3634 static evalue
* enumerate_vd(Polyhedron
**PA
,
3635 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
3637 Polyhedron
*P
= *PA
;
3638 int nvar
= P
->Dimension
- exist
- nparam
;
3639 Param_Polyhedron
*PP
= NULL
;
3640 Polyhedron
*C
= Universe_Polyhedron(nparam
);
3644 PP
= Polyhedron2Param_SimplifiedDomain(&PR
,C
,MaxRays
,&CEq
,&CT
);
3648 Param_Domain
*D
, *last
;
3651 for (nd
= 0, D
=PP
->D
; D
; D
=D
->next
, ++nd
)
3654 Polyhedron
**VD
= new Polyhedron_p
[nd
];
3655 Polyhedron
**fVD
= new Polyhedron_p
[nd
];
3656 for(nd
= 0, D
=PP
->D
; D
; D
=D
->next
) {
3657 Polyhedron
*rVD
= reduce_domain(D
->Domain
, CT
, CEq
,
3671 /* This doesn't seem to have any effect */
3673 Polyhedron
*CA
= align_context(VD
[0], P
->Dimension
, MaxRays
);
3675 P
= DomainIntersection(P
, CA
, MaxRays
);
3678 Polyhedron_Free(CA
);
3683 if (!EP
&& CT
->NbColumns
!= CT
->NbRows
) {
3684 Polyhedron
*CEqr
= DomainImage(CEq
, CT
, MaxRays
);
3685 Polyhedron
*CA
= align_context(CEqr
, PR
->Dimension
, MaxRays
);
3686 Polyhedron
*I
= DomainIntersection(PR
, CA
, MaxRays
);
3687 Polyhedron_Free(CEqr
);
3688 Polyhedron_Free(CA
);
3690 fprintf(stderr
, "\nER: Eliminate\n");
3691 #endif /* DEBUG_ER */
3692 nparam
-= CT
->NbColumns
- CT
->NbRows
;
3693 EP
= barvinok_enumerate_e(I
, exist
, nparam
, MaxRays
);
3694 nparam
+= CT
->NbColumns
- CT
->NbRows
;
3695 addeliminatedparams_enum(EP
, CT
, CEq
, MaxRays
, nparam
);
3699 Polyhedron_Free(PR
);
3702 if (!EP
&& nd
> 1) {
3704 fprintf(stderr
, "\nER: VD\n");
3705 #endif /* DEBUG_ER */
3706 for (int i
= 0; i
< nd
; ++i
) {
3707 Polyhedron
*CA
= align_context(VD
[i
], P
->Dimension
, MaxRays
);
3708 Polyhedron
*I
= DomainIntersection(P
, CA
, MaxRays
);
3711 EP
= barvinok_enumerate_e(I
, exist
, nparam
, MaxRays
);
3713 evalue
*E
= barvinok_enumerate_e(I
, exist
, nparam
, MaxRays
);
3715 free_evalue_refs(E
);
3719 Polyhedron_Free(CA
);
3723 for (int i
= 0; i
< nd
; ++i
) {
3724 Polyhedron_Free(VD
[i
]);
3725 Polyhedron_Free(fVD
[i
]);
3731 if (!EP
&& nvar
== 0) {
3734 Param_Vertices
*V
, *V2
;
3735 Matrix
* M
= Matrix_Alloc(1, P
->Dimension
+2);
3737 FORALL_PVertex_in_ParamPolyhedron(V
, last
, PP
) {
3739 FORALL_PVertex_in_ParamPolyhedron(V2
, last
, PP
) {
3746 for (int i
= 0; i
< exist
; ++i
) {
3747 value_oppose(f
, V
->Vertex
->p
[i
][nparam
+1]);
3748 Vector_Combine(V
->Vertex
->p
[i
],
3750 M
->p
[0] + 1 + nvar
+ exist
,
3751 V2
->Vertex
->p
[i
][nparam
+1],
3755 for (j
= 0; j
< nparam
; ++j
)
3756 if (value_notzero_p(M
->p
[0][1+nvar
+exist
+j
]))
3760 ConstraintSimplify(M
->p
[0], M
->p
[0],
3761 P
->Dimension
+2, &f
);
3762 value_set_si(M
->p
[0][0], 0);
3763 Polyhedron
*para
= AddConstraints(M
->p
[0], 1, P
,
3766 Polyhedron_Free(para
);
3769 Polyhedron
*pos
, *neg
;
3770 value_set_si(M
->p
[0][0], 1);
3771 value_decrement(M
->p
[0][P
->Dimension
+1],
3772 M
->p
[0][P
->Dimension
+1]);
3773 neg
= AddConstraints(M
->p
[0], 1, P
, MaxRays
);
3774 value_set_si(f
, -1);
3775 Vector_Scale(M
->p
[0]+1, M
->p
[0]+1, f
,
3777 value_decrement(M
->p
[0][P
->Dimension
+1],
3778 M
->p
[0][P
->Dimension
+1]);
3779 value_decrement(M
->p
[0][P
->Dimension
+1],
3780 M
->p
[0][P
->Dimension
+1]);
3781 pos
= AddConstraints(M
->p
[0], 1, P
, MaxRays
);
3782 if (emptyQ(neg
) && emptyQ(pos
)) {
3783 Polyhedron_Free(para
);
3784 Polyhedron_Free(pos
);
3785 Polyhedron_Free(neg
);
3789 fprintf(stderr
, "\nER: Order\n");
3790 #endif /* DEBUG_ER */
3791 EP
= barvinok_enumerate_e(para
, exist
, nparam
, MaxRays
);
3794 E
= barvinok_enumerate_e(pos
, exist
, nparam
, MaxRays
);
3796 free_evalue_refs(E
);
3800 E
= barvinok_enumerate_e(neg
, exist
, nparam
, MaxRays
);
3802 free_evalue_refs(E
);
3805 Polyhedron_Free(para
);
3806 Polyhedron_Free(pos
);
3807 Polyhedron_Free(neg
);
3812 } END_FORALL_PVertex_in_ParamPolyhedron
;
3815 } END_FORALL_PVertex_in_ParamPolyhedron
;
3818 /* Search for vertex coordinate to split on */
3819 /* First look for one independent of the parameters */
3820 FORALL_PVertex_in_ParamPolyhedron(V
, last
, PP
) {
3821 for (int i
= 0; i
< exist
; ++i
) {
3823 for (j
= 0; j
< nparam
; ++j
)
3824 if (value_notzero_p(V
->Vertex
->p
[i
][j
]))
3828 value_set_si(M
->p
[0][0], 1);
3829 Vector_Set(M
->p
[0]+1, 0, nvar
+exist
);
3830 Vector_Copy(V
->Vertex
->p
[i
],
3831 M
->p
[0] + 1 + nvar
+ exist
, nparam
+1);
3832 value_oppose(M
->p
[0][1+nvar
+i
],
3833 V
->Vertex
->p
[i
][nparam
+1]);
3835 Polyhedron
*pos
, *neg
;
3836 value_set_si(M
->p
[0][0], 1);
3837 value_decrement(M
->p
[0][P
->Dimension
+1],
3838 M
->p
[0][P
->Dimension
+1]);
3839 neg
= AddConstraints(M
->p
[0], 1, P
, MaxRays
);
3840 value_set_si(f
, -1);
3841 Vector_Scale(M
->p
[0]+1, M
->p
[0]+1, f
,
3843 value_decrement(M
->p
[0][P
->Dimension
+1],
3844 M
->p
[0][P
->Dimension
+1]);
3845 value_decrement(M
->p
[0][P
->Dimension
+1],
3846 M
->p
[0][P
->Dimension
+1]);
3847 pos
= AddConstraints(M
->p
[0], 1, P
, MaxRays
);
3848 if (emptyQ(neg
) || emptyQ(pos
)) {
3849 Polyhedron_Free(pos
);
3850 Polyhedron_Free(neg
);
3853 Polyhedron_Free(pos
);
3854 value_increment(M
->p
[0][P
->Dimension
+1],
3855 M
->p
[0][P
->Dimension
+1]);
3856 pos
= AddConstraints(M
->p
[0], 1, P
, MaxRays
);
3858 fprintf(stderr
, "\nER: Vertex\n");
3859 #endif /* DEBUG_ER */
3861 EP
= enumerate_or(pos
, exist
, nparam
, MaxRays
);
3866 } END_FORALL_PVertex_in_ParamPolyhedron
;
3870 /* Search for vertex coordinate to split on */
3871 /* Now look for one that depends on the parameters */
3872 FORALL_PVertex_in_ParamPolyhedron(V
, last
, PP
) {
3873 for (int i
= 0; i
< exist
; ++i
) {
3874 value_set_si(M
->p
[0][0], 1);
3875 Vector_Set(M
->p
[0]+1, 0, nvar
+exist
);
3876 Vector_Copy(V
->Vertex
->p
[i
],
3877 M
->p
[0] + 1 + nvar
+ exist
, nparam
+1);
3878 value_oppose(M
->p
[0][1+nvar
+i
],
3879 V
->Vertex
->p
[i
][nparam
+1]);
3881 Polyhedron
*pos
, *neg
;
3882 value_set_si(M
->p
[0][0], 1);
3883 value_decrement(M
->p
[0][P
->Dimension
+1],
3884 M
->p
[0][P
->Dimension
+1]);
3885 neg
= AddConstraints(M
->p
[0], 1, P
, MaxRays
);
3886 value_set_si(f
, -1);
3887 Vector_Scale(M
->p
[0]+1, M
->p
[0]+1, f
,
3889 value_decrement(M
->p
[0][P
->Dimension
+1],
3890 M
->p
[0][P
->Dimension
+1]);
3891 value_decrement(M
->p
[0][P
->Dimension
+1],
3892 M
->p
[0][P
->Dimension
+1]);
3893 pos
= AddConstraints(M
->p
[0], 1, P
, MaxRays
);
3894 if (emptyQ(neg
) || emptyQ(pos
)) {
3895 Polyhedron_Free(pos
);
3896 Polyhedron_Free(neg
);
3899 Polyhedron_Free(pos
);
3900 value_increment(M
->p
[0][P
->Dimension
+1],
3901 M
->p
[0][P
->Dimension
+1]);
3902 pos
= AddConstraints(M
->p
[0], 1, P
, MaxRays
);
3904 fprintf(stderr
, "\nER: ParamVertex\n");
3905 #endif /* DEBUG_ER */
3907 EP
= enumerate_or(pos
, exist
, nparam
, MaxRays
);
3912 } END_FORALL_PVertex_in_ParamPolyhedron
;
3920 Polyhedron_Free(CEq
);
3924 Param_Polyhedron_Free(PP
);
3931 evalue
*barvinok_enumerate_pip(Polyhedron
*P
,
3932 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
3937 evalue
*barvinok_enumerate_pip(Polyhedron
*P
,
3938 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
3940 int nvar
= P
->Dimension
- exist
- nparam
;
3941 evalue
*EP
= new_zero_ep();
3942 Polyhedron
*Q
, *N
, *T
= 0;
3948 fprintf(stderr
, "\nER: PIP\n");
3949 #endif /* DEBUG_ER */
3951 for (int i
= 0; i
< P
->Dimension
; ++i
) {
3954 bool posray
= false;
3955 bool negray
= false;
3956 value_set_si(min
, 0);
3957 for (int j
= 0; j
< P
->NbRays
; ++j
) {
3958 if (value_pos_p(P
->Ray
[j
][1+i
])) {
3960 if (value_zero_p(P
->Ray
[j
][1+P
->Dimension
]))
3962 } else if (value_neg_p(P
->Ray
[j
][1+i
])) {
3964 if (value_zero_p(P
->Ray
[j
][1+P
->Dimension
]))
3968 P
->Ray
[j
][1+i
], P
->Ray
[j
][1+P
->Dimension
]);
3969 if (value_lt(tmp
, min
))
3970 value_assign(min
, tmp
);
3975 assert(!(posray
&& negray
));
3976 assert(!negray
); // for now
3977 Polyhedron
*O
= T
? T
: P
;
3978 /* shift by a safe amount */
3979 Matrix
*M
= Matrix_Alloc(O
->NbRays
, O
->Dimension
+2);
3980 Vector_Copy(O
->Ray
[0], M
->p
[0], O
->NbRays
* (O
->Dimension
+2));
3981 for (int j
= 0; j
< P
->NbRays
; ++j
) {
3982 if (value_notzero_p(M
->p
[j
][1+P
->Dimension
])) {
3983 value_multiply(tmp
, min
, M
->p
[j
][1+P
->Dimension
]);
3984 value_substract(M
->p
[j
][1+i
], M
->p
[j
][1+i
], tmp
);
3989 T
= Rays2Polyhedron(M
, MaxRays
);
3992 /* negating a parameter requires that we substitute in the
3993 * sign again afterwards.
3996 assert(i
< nvar
+exist
);
3998 T
= Polyhedron_Copy(P
);
3999 for (int j
= 0; j
< T
->NbRays
; ++j
)
4000 value_oppose(T
->Ray
[j
][1+i
], T
->Ray
[j
][1+i
]);
4001 for (int j
= 0; j
< T
->NbConstraints
; ++j
)
4002 value_oppose(T
->Constraint
[j
][1+i
], T
->Constraint
[j
][1+i
]);
4008 Polyhedron
*D
= pip_lexmin(T
? T
: P
, exist
, nparam
);
4009 for (Q
= D
; Q
; Q
= N
) {
4013 exist
= Q
->Dimension
- nvar
- nparam
;
4014 E
= barvinok_enumerate_e(Q
, exist
, nparam
, MaxRays
);
4017 free_evalue_refs(E
);
4029 static bool is_single(Value
*row
, int pos
, int len
)
4031 return First_Non_Zero(row
, pos
) == -1 &&
4032 First_Non_Zero(row
+pos
+1, len
-pos
-1) == -1;
4035 static evalue
* barvinok_enumerate_e_r(Polyhedron
*P
,
4036 unsigned exist
, unsigned nparam
, unsigned MaxRays
);
4039 static int er_level
= 0;
4041 evalue
* barvinok_enumerate_e(Polyhedron
*P
,
4042 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
4044 fprintf(stderr
, "\nER: level %i\n", er_level
);
4045 int nvar
= P
->Dimension
- exist
- nparam
;
4046 fprintf(stderr
, "%d %d %d\n", nvar
, exist
, nparam
);
4048 Polyhedron_Print(stderr
, P_VALUE_FMT
, P
);
4050 P
= DomainConstraintSimplify(Polyhedron_Copy(P
), MaxRays
);
4051 evalue
*EP
= barvinok_enumerate_e_r(P
, exist
, nparam
, MaxRays
);
4057 evalue
* barvinok_enumerate_e(Polyhedron
*P
,
4058 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
4060 P
= DomainConstraintSimplify(Polyhedron_Copy(P
), MaxRays
);
4061 evalue
*EP
= barvinok_enumerate_e_r(P
, exist
, nparam
, MaxRays
);
4067 static evalue
* barvinok_enumerate_e_r(Polyhedron
*P
,
4068 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
4071 Polyhedron
*U
= Universe_Polyhedron(nparam
);
4072 evalue
*EP
= barvinok_enumerate_ev(P
, U
, MaxRays
);
4073 //char *param_name[] = {"P", "Q", "R", "S", "T" };
4074 //print_evalue(stdout, EP, param_name);
4079 int nvar
= P
->Dimension
- exist
- nparam
;
4080 int len
= P
->Dimension
+ 2;
4083 return new_zero_ep();
4085 if (nvar
== 0 && nparam
== 0) {
4086 evalue
*EP
= new_zero_ep();
4087 barvinok_count(P
, &EP
->x
.n
, MaxRays
);
4088 if (value_pos_p(EP
->x
.n
))
4089 value_set_si(EP
->x
.n
, 1);
4094 for (r
= 0; r
< P
->NbRays
; ++r
)
4095 if (value_zero_p(P
->Ray
[r
][0]) ||
4096 value_zero_p(P
->Ray
[r
][P
->Dimension
+1])) {
4098 for (i
= 0; i
< nvar
; ++i
)
4099 if (value_notzero_p(P
->Ray
[r
][i
+1]))
4103 for (i
= nvar
+ exist
; i
< nvar
+ exist
+ nparam
; ++i
)
4104 if (value_notzero_p(P
->Ray
[r
][i
+1]))
4106 if (i
>= nvar
+ exist
+ nparam
)
4109 if (r
< P
->NbRays
) {
4110 evalue
*EP
= new_zero_ep();
4111 value_set_si(EP
->x
.n
, -1);
4116 for (r
= 0; r
< P
->NbEq
; ++r
)
4117 if ((first
= First_Non_Zero(P
->Constraint
[r
]+1+nvar
, exist
)) != -1)
4120 if (First_Non_Zero(P
->Constraint
[r
]+1+nvar
+first
+1,
4121 exist
-first
-1) != -1) {
4122 Polyhedron
*T
= rotate_along(P
, r
, nvar
, exist
, MaxRays
);
4124 fprintf(stderr
, "\nER: Equality\n");
4125 #endif /* DEBUG_ER */
4126 evalue
*EP
= barvinok_enumerate_e(T
, exist
-1, nparam
, MaxRays
);
4131 fprintf(stderr
, "\nER: Fixed\n");
4132 #endif /* DEBUG_ER */
4134 return barvinok_enumerate_e(P
, exist
-1, nparam
, MaxRays
);
4136 Polyhedron
*T
= Polyhedron_Copy(P
);
4137 SwapColumns(T
, nvar
+1, nvar
+1+first
);
4138 evalue
*EP
= barvinok_enumerate_e(T
, exist
-1, nparam
, MaxRays
);
4145 Vector
*row
= Vector_Alloc(len
);
4146 value_set_si(row
->p
[0], 1);
4151 enum constraint
* info
= new constraint
[exist
];
4152 for (int i
= 0; i
< exist
; ++i
) {
4154 for (int l
= P
->NbEq
; l
< P
->NbConstraints
; ++l
) {
4155 if (value_negz_p(P
->Constraint
[l
][nvar
+i
+1]))
4157 bool l_parallel
= is_single(P
->Constraint
[l
]+nvar
+1, i
, exist
);
4158 for (int u
= P
->NbEq
; u
< P
->NbConstraints
; ++u
) {
4159 if (value_posz_p(P
->Constraint
[u
][nvar
+i
+1]))
4161 bool lu_parallel
= l_parallel
||
4162 is_single(P
->Constraint
[u
]+nvar
+1, i
, exist
);
4163 value_oppose(f
, P
->Constraint
[u
][nvar
+i
+1]);
4164 Vector_Combine(P
->Constraint
[l
]+1, P
->Constraint
[u
]+1, row
->p
+1,
4165 f
, P
->Constraint
[l
][nvar
+i
+1], len
-1);
4166 if (!(info
[i
] & INDEPENDENT
)) {
4168 for (j
= 0; j
< exist
; ++j
)
4169 if (j
!= i
&& value_notzero_p(row
->p
[nvar
+j
+1]))
4172 //printf("independent: i: %d, l: %d, u: %d\n", i, l, u);
4173 info
[i
] = (constraint
)(info
[i
] | INDEPENDENT
);
4176 if (info
[i
] & ALL_POS
) {
4177 value_addto(row
->p
[len
-1], row
->p
[len
-1],
4178 P
->Constraint
[l
][nvar
+i
+1]);
4179 value_addto(row
->p
[len
-1], row
->p
[len
-1], f
);
4180 value_multiply(f
, f
, P
->Constraint
[l
][nvar
+i
+1]);
4181 value_substract(row
->p
[len
-1], row
->p
[len
-1], f
);
4182 value_decrement(row
->p
[len
-1], row
->p
[len
-1]);
4183 ConstraintSimplify(row
->p
, row
->p
, len
, &f
);
4184 value_set_si(f
, -1);
4185 Vector_Scale(row
->p
+1, row
->p
+1, f
, len
-1);
4186 value_decrement(row
->p
[len
-1], row
->p
[len
-1]);
4187 Polyhedron
*T
= AddConstraints(row
->p
, 1, P
, MaxRays
);
4189 //printf("not all_pos: i: %d, l: %d, u: %d\n", i, l, u);
4190 info
[i
] = (constraint
)(info
[i
] ^ ALL_POS
);
4192 //puts("pos remainder");
4193 //Polyhedron_Print(stdout, P_VALUE_FMT, T);
4196 if (!(info
[i
] & ONE_NEG
)) {
4198 negative_test_constraint(P
->Constraint
[l
],
4200 row
->p
, nvar
+i
, len
, &f
);
4201 oppose_constraint(row
->p
, len
, &f
);
4202 Polyhedron
*T
= AddConstraints(row
->p
, 1, P
, MaxRays
);
4204 //printf("one_neg i: %d, l: %d, u: %d\n", i, l, u);
4205 info
[i
] = (constraint
)(info
[i
] | ONE_NEG
);
4207 //puts("neg remainder");
4208 //Polyhedron_Print(stdout, P_VALUE_FMT, T);
4210 } else if (!(info
[i
] & ROT_NEG
)) {
4211 if (parallel_constraints(P
->Constraint
[l
],
4213 row
->p
, nvar
, exist
)) {
4214 negative_test_constraint7(P
->Constraint
[l
],
4216 row
->p
, nvar
, exist
,
4218 oppose_constraint(row
->p
, len
, &f
);
4219 Polyhedron
*T
= AddConstraints(row
->p
, 1, P
, MaxRays
);
4221 // printf("rot_neg i: %d, l: %d, u: %d\n", i, l, u);
4222 info
[i
] = (constraint
)(info
[i
] | ROT_NEG
);
4225 //puts("neg remainder");
4226 //Polyhedron_Print(stdout, P_VALUE_FMT, T);
4231 if (!(info
[i
] & ALL_POS
) && (info
[i
] & (ONE_NEG
| ROT_NEG
)))
4235 if (info
[i
] & ALL_POS
)
4242 for (int i = 0; i < exist; ++i)
4243 printf("%i: %i\n", i, info[i]);
4245 for (int i
= 0; i
< exist
; ++i
)
4246 if (info
[i
] & ALL_POS
) {
4248 fprintf(stderr
, "\nER: Positive\n");
4249 #endif /* DEBUG_ER */
4251 // Maybe we should chew off some of the fat here
4252 Matrix
*M
= Matrix_Alloc(P
->Dimension
, P
->Dimension
+1);
4253 for (int j
= 0; j
< P
->Dimension
; ++j
)
4254 value_set_si(M
->p
[j
][j
+ (j
>= i
+nvar
)], 1);
4255 Polyhedron
*T
= Polyhedron_Image(P
, M
, MaxRays
);
4257 evalue
*EP
= barvinok_enumerate_e(T
, exist
-1, nparam
, MaxRays
);
4264 for (int i
= 0; i
< exist
; ++i
)
4265 if (info
[i
] & ONE_NEG
) {
4267 fprintf(stderr
, "\nER: Negative\n");
4268 #endif /* DEBUG_ER */
4273 return barvinok_enumerate_e(P
, exist
-1, nparam
, MaxRays
);
4275 Polyhedron
*T
= Polyhedron_Copy(P
);
4276 SwapColumns(T
, nvar
+1, nvar
+1+i
);
4277 evalue
*EP
= barvinok_enumerate_e(T
, exist
-1, nparam
, MaxRays
);
4282 for (int i
= 0; i
< exist
; ++i
)
4283 if (info
[i
] & ROT_NEG
) {
4285 fprintf(stderr
, "\nER: Rotate\n");
4286 #endif /* DEBUG_ER */
4290 Polyhedron
*T
= rotate_along(P
, r
, nvar
, exist
, MaxRays
);
4291 evalue
*EP
= barvinok_enumerate_e(T
, exist
-1, nparam
, MaxRays
);
4295 for (int i
= 0; i
< exist
; ++i
)
4296 if (info
[i
] & INDEPENDENT
) {
4297 Polyhedron
*pos
, *neg
;
4299 /* Find constraint again and split off negative part */
4301 if (SplitOnVar(P
, i
, nvar
, len
, exist
, MaxRays
,
4302 row
, f
, true, &pos
, &neg
)) {
4304 fprintf(stderr
, "\nER: Split\n");
4305 #endif /* DEBUG_ER */
4308 barvinok_enumerate_e(neg
, exist
-1, nparam
, MaxRays
);
4310 barvinok_enumerate_e(pos
, exist
, nparam
, MaxRays
);
4312 free_evalue_refs(E
);
4314 Polyhedron_Free(neg
);
4315 Polyhedron_Free(pos
);
4329 EP
= enumerate_line(P
, exist
, nparam
, MaxRays
);
4333 EP
= barvinok_enumerate_pip(P
, exist
, nparam
, MaxRays
);
4337 EP
= enumerate_redundant_ray(P
, exist
, nparam
, MaxRays
);
4341 EP
= enumerate_sure(P
, exist
, nparam
, MaxRays
);
4345 EP
= enumerate_ray(P
, exist
, nparam
, MaxRays
);
4349 EP
= enumerate_sure2(P
, exist
, nparam
, MaxRays
);
4353 F
= unfringe(P
, MaxRays
);
4354 if (!PolyhedronIncludes(F
, P
)) {
4356 fprintf(stderr
, "\nER: Fringed\n");
4357 #endif /* DEBUG_ER */
4358 EP
= barvinok_enumerate_e(F
, exist
, nparam
, MaxRays
);
4365 EP
= enumerate_vd(&P
, exist
, nparam
, MaxRays
);
4370 EP
= enumerate_sum(P
, exist
, nparam
, MaxRays
);
4377 Polyhedron
*pos
, *neg
;
4378 for (i
= 0; i
< exist
; ++i
)
4379 if (SplitOnVar(P
, i
, nvar
, len
, exist
, MaxRays
,
4380 row
, f
, false, &pos
, &neg
))
4386 EP
= enumerate_or(pos
, exist
, nparam
, MaxRays
);
4398 gen_fun
* barvinok_series(Polyhedron
*P
, Polyhedron
* C
, unsigned MaxRays
)
4400 Polyhedron
** vcone
;
4402 unsigned nparam
= C
->Dimension
;
4406 sign
.SetLength(ncone
);
4408 CA
= align_context(C
, P
->Dimension
, MaxRays
);
4409 P
= DomainIntersection(P
, CA
, MaxRays
);
4410 Polyhedron_Free(CA
);
4412 assert(!Polyhedron_is_infinite(P
, nparam
));
4413 assert(P
->NbBid
== 0);
4414 assert(Polyhedron_has_positive_rays(P
, nparam
));
4416 P
= remove_equalities_p(P
, P
->Dimension
-nparam
, NULL
);
4417 assert(P
->NbEq
== 0);
4419 partial_reducer
red(P
, nparam
);