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
) {
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
, dpoly
& den
, int pos
, int sign
, int dim
) {
416 len
= num
.coeff
.length();
417 c
= new vector
< dpoly_r_term
* > [len
];
421 for (int i
= 0; i
< len
; ++i
) {
422 ZZ coeff
= num
.coeff
[i
];
423 memset(powers
, 0, dim
* sizeof(int));
426 add_term(i
, powers
, coeff
);
428 for (int j
= 1; j
<= i
; ++j
) {
429 for (int k
= 0; k
< c
[i
-j
].size(); ++k
) {
430 memcpy(powers
, c
[i
-j
][k
]->powers
, dim
*sizeof(int));
432 coeff
= -den
.coeff
[j
-1] * c
[i
-j
][k
]->coeff
;
433 add_term(i
, powers
, coeff
);
439 dpoly_r
*div(dpoly
& d
) {
440 dpoly_r
*rc
= new dpoly_r(len
, dim
);
441 rc
->denom
= power(d
.coeff
[0], len
);
442 ZZ inv_d
= rc
->denom
/ d
.coeff
[0];
445 for (int i
= 0; i
< len
; ++i
) {
446 for (int k
= 0; k
< c
[i
].size(); ++k
) {
447 coeff
= c
[i
][k
]->coeff
* inv_d
;
448 rc
->add_term(i
, c
[i
][k
]->powers
, coeff
);
451 for (int j
= 1; j
<= i
; ++j
) {
452 for (int k
= 0; k
< rc
->c
[i
-j
].size(); ++k
) {
453 coeff
= - d
.coeff
[j
] * rc
->c
[i
-j
][k
]->coeff
/ d
.coeff
[0];
454 rc
->add_term(i
, rc
->c
[i
-j
][k
]->powers
, coeff
);
460 void div(dpoly
& d
, ZZ
& sign
, gen_fun
*gf
, mat_ZZ
& pden
, mat_ZZ
& den
,
462 dpoly_r
* rc
= div(d
);
464 int common
= pden
.NumRows();
466 vector
< dpoly_r_term
* >& final
= rc
->c
[len
-1];
468 for (int j
= 0; j
< final
.size(); ++j
) {
470 pden
.SetDims(rows
, pden
.NumCols());
471 for (int k
= 0; k
< dim
; ++k
) {
472 int n
= final
[j
]->powers
[k
];
475 int abs_n
= n
< 0 ? -n
: n
;
476 pden
.SetDims(rows
+abs_n
, pden
.NumCols());
477 for (int l
= 0; l
< abs_n
; ++l
) {
479 pden
[rows
+l
] = den
[k
];
481 pden
[rows
+l
] = -den
[k
];
485 final
[j
]->coeff
*= sign
;
486 gf
->add(final
[j
]->coeff
, rc
->denom
, num_p
, pden
);
491 for (int i
= 0; i
< len
; ++i
) {
494 cout
<< c
[i
].size() << endl
;
495 for (int j
= 0; j
< c
[i
].size(); ++j
) {
496 for (int k
= 0; k
< dim
; ++k
) {
497 cout
<< c
[i
][j
]->powers
[k
] << " ";
499 cout
<< ": " << c
[i
][j
]->coeff
<< "/" << denom
<< endl
;
507 void decompose(Polyhedron
*C
);
508 virtual void handle(Polyhedron
*P
, int sign
) = 0;
511 struct polar_decomposer
: public decomposer
{
512 void decompose(Polyhedron
*C
, unsigned MaxRays
);
513 virtual void handle(Polyhedron
*P
, int sign
);
514 virtual void handle_polar(Polyhedron
*P
, int sign
) = 0;
517 void decomposer::decompose(Polyhedron
*C
)
519 vector
<cone
*> nonuni
;
520 cone
* c
= new cone(C
);
531 while (!nonuni
.empty()) {
534 Vector
* v
= c
->short_vector(lambda
);
535 for (int i
= 0; i
< c
->Rays
->NbRows
- 1; ++i
) {
538 Matrix
* M
= Matrix_Copy(c
->Rays
);
539 Vector_Copy(v
->p
, M
->p
[i
], v
->Size
);
540 cone
* pc
= new cone(M
);
541 assert (pc
->det
!= 0);
542 if (abs(pc
->det
) > 1) {
543 assert(abs(pc
->det
) < abs(c
->det
));
544 nonuni
.push_back(pc
);
546 handle(pc
->poly(), sign(pc
->det
) * s
);
556 void polar_decomposer::decompose(Polyhedron
*cone
, unsigned MaxRays
)
558 Polyhedron_Polarize(cone
);
559 if (cone
->NbRays
- 1 != cone
->Dimension
) {
560 Polyhedron
*tmp
= cone
;
561 cone
= triangularize_cone(cone
, MaxRays
);
562 Polyhedron_Free(tmp
);
564 for (Polyhedron
*Polar
= cone
; Polar
; Polar
= Polar
->next
)
565 decomposer::decompose(Polar
);
569 void polar_decomposer::handle(Polyhedron
*P
, int sign
)
571 Polyhedron_Polarize(P
);
572 handle_polar(P
, sign
);
576 * Barvinok's Decomposition of a simplicial cone
578 * Returns two lists of polyhedra
580 void barvinok_decompose(Polyhedron
*C
, Polyhedron
**ppos
, Polyhedron
**pneg
)
582 Polyhedron
*pos
= *ppos
, *neg
= *pneg
;
583 vector
<cone
*> nonuni
;
584 cone
* c
= new cone(C
);
591 Polyhedron
*p
= Polyhedron_Copy(c
->Cone
);
597 while (!nonuni
.empty()) {
600 Vector
* v
= c
->short_vector(lambda
);
601 for (int i
= 0; i
< c
->Rays
->NbRows
- 1; ++i
) {
604 Matrix
* M
= Matrix_Copy(c
->Rays
);
605 Vector_Copy(v
->p
, M
->p
[i
], v
->Size
);
606 cone
* pc
= new cone(M
);
607 assert (pc
->det
!= 0);
608 if (abs(pc
->det
) > 1) {
609 assert(abs(pc
->det
) < abs(c
->det
));
610 nonuni
.push_back(pc
);
612 Polyhedron
*p
= pc
->poly();
614 if (sign(pc
->det
) == s
) {
633 * Returns a single list of npos "positive" cones followed by nneg
635 * The input cone is freed
637 void decompose(Polyhedron
*cone
, Polyhedron
**parts
, int *npos
, int *nneg
, unsigned MaxRays
)
639 Polyhedron_Polarize(cone
);
640 if (cone
->NbRays
- 1 != cone
->Dimension
) {
641 Polyhedron
*tmp
= cone
;
642 cone
= triangularize_cone(cone
, MaxRays
);
643 Polyhedron_Free(tmp
);
645 Polyhedron
*polpos
= NULL
, *polneg
= NULL
;
646 *npos
= 0; *nneg
= 0;
647 for (Polyhedron
*Polar
= cone
; Polar
; Polar
= Polar
->next
)
648 barvinok_decompose(Polar
, &polpos
, &polneg
);
651 for (Polyhedron
*i
= polpos
; i
; i
= i
->next
) {
652 Polyhedron_Polarize(i
);
656 for (Polyhedron
*i
= polneg
; i
; i
= i
->next
) {
657 Polyhedron_Polarize(i
);
668 const int MAX_TRY
=10;
670 * Searches for a vector that is not orthogonal to any
671 * of the rays in rays.
673 static void nonorthog(mat_ZZ
& rays
, vec_ZZ
& lambda
)
675 int dim
= rays
.NumCols();
677 lambda
.SetLength(dim
);
681 for (int i
= 2; !found
&& i
<= 50*dim
; i
+=4) {
682 for (int j
= 0; j
< MAX_TRY
; ++j
) {
683 for (int k
= 0; k
< dim
; ++k
) {
684 int r
= random_int(i
)+2;
685 int v
= (2*(r
%2)-1) * (r
>> 1);
689 for (; k
< rays
.NumRows(); ++k
)
690 if (lambda
* rays
[k
] == 0)
692 if (k
== rays
.NumRows()) {
701 static void randomvector(Polyhedron
*P
, vec_ZZ
& lambda
, int nvar
)
705 unsigned int dim
= P
->Dimension
;
708 for (int i
= 0; i
< P
->NbRays
; ++i
) {
709 for (int j
= 1; j
<= dim
; ++j
) {
710 value_absolute(tmp
, P
->Ray
[i
][j
]);
711 int t
= VALUE_TO_LONG(tmp
) * 16;
716 for (int i
= 0; i
< P
->NbConstraints
; ++i
) {
717 for (int j
= 1; j
<= dim
; ++j
) {
718 value_absolute(tmp
, P
->Constraint
[i
][j
]);
719 int t
= VALUE_TO_LONG(tmp
) * 16;
726 lambda
.SetLength(nvar
);
727 for (int k
= 0; k
< nvar
; ++k
) {
728 int r
= random_int(max
*dim
)+2;
729 int v
= (2*(r
%2)-1) * (max
/2*dim
+ (r
>> 1));
734 static void add_rays(mat_ZZ
& rays
, Polyhedron
*i
, int *r
, int nvar
= -1,
737 unsigned dim
= i
->Dimension
;
740 for (int k
= 0; k
< i
->NbRays
; ++k
) {
741 if (!value_zero_p(i
->Ray
[k
][dim
+1]))
743 if (!all
&& nvar
!= dim
&& First_Non_Zero(i
->Ray
[k
]+1, nvar
) == -1)
745 values2zz(i
->Ray
[k
]+1, rays
[(*r
)++], nvar
);
749 void lattice_point(Value
* values
, Polyhedron
*i
, vec_ZZ
& vertex
)
751 unsigned dim
= i
->Dimension
;
752 if(!value_one_p(values
[dim
])) {
753 Matrix
* Rays
= rays(i
);
754 Matrix
*inv
= Matrix_Alloc(Rays
->NbRows
, Rays
->NbColumns
);
755 int ok
= Matrix_Inverse(Rays
, inv
);
759 Vector
*lambda
= Vector_Alloc(dim
+1);
760 Vector_Matrix_Product(values
, inv
, lambda
->p
);
762 for (int j
= 0; j
< dim
; ++j
)
763 mpz_cdiv_q(lambda
->p
[j
], lambda
->p
[j
], lambda
->p
[dim
]);
764 value_set_si(lambda
->p
[dim
], 1);
765 Vector
*A
= Vector_Alloc(dim
+1);
766 Vector_Matrix_Product(lambda
->p
, Rays
, A
->p
);
769 values2zz(A
->p
, vertex
, dim
);
772 values2zz(values
, vertex
, dim
);
775 static evalue
*term(int param
, ZZ
& c
, Value
*den
= NULL
)
777 evalue
*EP
= new evalue();
779 value_set_si(EP
->d
,0);
780 EP
->x
.p
= new_enode(polynomial
, 2, param
+ 1);
781 evalue_set_si(&EP
->x
.p
->arr
[0], 0, 1);
782 value_init(EP
->x
.p
->arr
[1].x
.n
);
784 value_set_si(EP
->x
.p
->arr
[1].d
, 1);
786 value_assign(EP
->x
.p
->arr
[1].d
, *den
);
787 zz2value(c
, EP
->x
.p
->arr
[1].x
.n
);
791 static void vertex_period(
792 Polyhedron
*i
, vec_ZZ
& lambda
, Matrix
*T
,
793 Value lcm
, int p
, Vector
*val
,
794 evalue
*E
, evalue
* ev
,
797 unsigned nparam
= T
->NbRows
- 1;
798 unsigned dim
= i
->Dimension
;
805 Vector
* values
= Vector_Alloc(dim
+ 1);
806 Vector_Matrix_Product(val
->p
, T
, values
->p
);
807 value_assign(values
->p
[dim
], lcm
);
808 lattice_point(values
->p
, i
, vertex
);
809 num
= vertex
* lambda
;
814 zz2value(num
, ev
->x
.n
);
815 value_assign(ev
->d
, lcm
);
822 values2zz(T
->p
[p
], vertex
, dim
);
823 nump
= vertex
* lambda
;
824 if (First_Non_Zero(val
->p
, p
) == -1) {
825 value_assign(tmp
, lcm
);
826 evalue
*ET
= term(p
, nump
, &tmp
);
828 free_evalue_refs(ET
);
832 value_assign(tmp
, lcm
);
833 if (First_Non_Zero(T
->p
[p
], dim
) != -1)
834 Vector_Gcd(T
->p
[p
], dim
, &tmp
);
836 if (value_lt(tmp
, lcm
)) {
839 value_division(tmp
, lcm
, tmp
);
840 value_set_si(ev
->d
, 0);
841 ev
->x
.p
= new_enode(periodic
, VALUE_TO_INT(tmp
), p
+1);
842 value2zz(tmp
, count
);
844 value_decrement(tmp
, tmp
);
846 ZZ new_offset
= offset
- count
* nump
;
847 value_assign(val
->p
[p
], tmp
);
848 vertex_period(i
, lambda
, T
, lcm
, p
+1, val
, E
,
849 &ev
->x
.p
->arr
[VALUE_TO_INT(tmp
)], new_offset
);
850 } while (value_pos_p(tmp
));
852 vertex_period(i
, lambda
, T
, lcm
, p
+1, val
, E
, ev
, offset
);
856 static void mask_r(Matrix
*f
, int nr
, Vector
*lcm
, int p
, Vector
*val
, evalue
*ev
)
858 unsigned nparam
= lcm
->Size
;
861 Vector
* prod
= Vector_Alloc(f
->NbRows
);
862 Matrix_Vector_Product(f
, val
->p
, prod
->p
);
864 for (int i
= 0; i
< nr
; ++i
) {
865 value_modulus(prod
->p
[i
], prod
->p
[i
], f
->p
[i
][nparam
+1]);
866 isint
&= value_zero_p(prod
->p
[i
]);
868 value_set_si(ev
->d
, 1);
870 value_set_si(ev
->x
.n
, isint
);
877 if (value_one_p(lcm
->p
[p
]))
878 mask_r(f
, nr
, lcm
, p
+1, val
, ev
);
880 value_assign(tmp
, lcm
->p
[p
]);
881 value_set_si(ev
->d
, 0);
882 ev
->x
.p
= new_enode(periodic
, VALUE_TO_INT(tmp
), p
+1);
884 value_decrement(tmp
, tmp
);
885 value_assign(val
->p
[p
], tmp
);
886 mask_r(f
, nr
, lcm
, p
+1, val
, &ev
->x
.p
->arr
[VALUE_TO_INT(tmp
)]);
887 } while (value_pos_p(tmp
));
892 static evalue
*multi_monom(vec_ZZ
& p
)
894 evalue
*X
= new evalue();
897 unsigned nparam
= p
.length()-1;
898 zz2value(p
[nparam
], X
->x
.n
);
899 value_set_si(X
->d
, 1);
900 for (int i
= 0; i
< nparam
; ++i
) {
903 evalue
*T
= term(i
, p
[i
]);
912 * Check whether mapping polyhedron P on the affine combination
913 * num yields a range that has a fixed quotient on integer
915 * If zero is true, then we are only interested in the quotient
916 * for the cases where the remainder is zero.
917 * Returns NULL if false and a newly allocated value if true.
919 static Value
*fixed_quotient(Polyhedron
*P
, vec_ZZ
& num
, Value d
, bool zero
)
922 int len
= num
.length();
923 Matrix
*T
= Matrix_Alloc(2, len
);
924 zz2values(num
, T
->p
[0]);
925 value_set_si(T
->p
[1][len
-1], 1);
926 Polyhedron
*I
= Polyhedron_Image(P
, T
, P
->NbConstraints
);
930 for (i
= 0; i
< I
->NbRays
; ++i
)
931 if (value_zero_p(I
->Ray
[i
][2])) {
939 int bounded
= line_minmax(I
, &min
, &max
);
943 mpz_cdiv_q(min
, min
, d
);
945 mpz_fdiv_q(min
, min
, d
);
946 mpz_fdiv_q(max
, max
, d
);
948 if (value_eq(min
, max
)) {
951 value_assign(*ret
, min
);
959 * Normalize linear expression coef modulo m
960 * Removes common factor and reduces coefficients
961 * Returns index of first non-zero coefficient or len
963 static int normal_mod(Value
*coef
, int len
, Value
*m
)
968 Vector_Gcd(coef
, len
, &gcd
);
970 Vector_AntiScale(coef
, coef
, gcd
, len
);
972 value_division(*m
, *m
, gcd
);
979 for (j
= 0; j
< len
; ++j
)
980 mpz_fdiv_r(coef
[j
], coef
[j
], *m
);
981 for (j
= 0; j
< len
; ++j
)
982 if (value_notzero_p(coef
[j
]))
989 static void mask(Matrix
*f
, evalue
*factor
)
991 int nr
= f
->NbRows
, nc
= f
->NbColumns
;
994 for (n
= 0; n
< nr
&& value_notzero_p(f
->p
[n
][nc
-1]); ++n
)
995 if (value_notone_p(f
->p
[n
][nc
-1]) &&
996 value_notmone_p(f
->p
[n
][nc
-1]))
1010 value_set_si(EV
.x
.n
, 1);
1012 for (n
= 0; n
< nr
; ++n
) {
1013 value_assign(m
, f
->p
[n
][nc
-1]);
1014 if (value_one_p(m
) || value_mone_p(m
))
1017 int j
= normal_mod(f
->p
[n
], nc
-1, &m
);
1019 free_evalue_refs(factor
);
1020 value_init(factor
->d
);
1021 evalue_set_si(factor
, 0, 1);
1025 values2zz(f
->p
[n
], row
, nc
-1);
1028 if (j
< (nc
-1)-1 && row
[j
] > g
/2) {
1029 for (int k
= j
; k
< (nc
-1); ++k
)
1031 row
[k
] = g
- row
[k
];
1035 value_set_si(EP
.d
, 0);
1036 EP
.x
.p
= new_enode(relation
, 2, 0);
1037 value_clear(EP
.x
.p
->arr
[1].d
);
1038 EP
.x
.p
->arr
[1] = *factor
;
1039 evalue
*ev
= &EP
.x
.p
->arr
[0];
1040 value_set_si(ev
->d
, 0);
1041 ev
->x
.p
= new_enode(fractional
, 3, -1);
1042 evalue_set_si(&ev
->x
.p
->arr
[1], 0, 1);
1043 evalue_set_si(&ev
->x
.p
->arr
[2], 1, 1);
1044 evalue
*E
= multi_monom(row
);
1045 value_assign(EV
.d
, m
);
1047 value_clear(ev
->x
.p
->arr
[0].d
);
1048 ev
->x
.p
->arr
[0] = *E
;
1054 free_evalue_refs(&EV
);
1060 static void mask(Matrix
*f
, evalue
*factor
)
1062 int nr
= f
->NbRows
, nc
= f
->NbColumns
;
1065 for (n
= 0; n
< nr
&& value_notzero_p(f
->p
[n
][nc
-1]); ++n
)
1066 if (value_notone_p(f
->p
[n
][nc
-1]) &&
1067 value_notmone_p(f
->p
[n
][nc
-1]))
1075 unsigned np
= nc
- 2;
1076 Vector
*lcm
= Vector_Alloc(np
);
1077 Vector
*val
= Vector_Alloc(nc
);
1078 Vector_Set(val
->p
, 0, nc
);
1079 value_set_si(val
->p
[np
], 1);
1080 Vector_Set(lcm
->p
, 1, np
);
1081 for (n
= 0; n
< nr
; ++n
) {
1082 if (value_one_p(f
->p
[n
][nc
-1]) ||
1083 value_mone_p(f
->p
[n
][nc
-1]))
1085 for (int j
= 0; j
< np
; ++j
)
1086 if (value_notzero_p(f
->p
[n
][j
])) {
1087 Gcd(f
->p
[n
][j
], f
->p
[n
][nc
-1], &tmp
);
1088 value_division(tmp
, f
->p
[n
][nc
-1], tmp
);
1089 value_lcm(tmp
, lcm
->p
[j
], &lcm
->p
[j
]);
1094 mask_r(f
, nr
, lcm
, 0, val
, &EP
);
1099 free_evalue_refs(&EP
);
1110 static bool mod_needed(Polyhedron
*PD
, vec_ZZ
& num
, Value d
, evalue
*E
)
1112 Value
*q
= fixed_quotient(PD
, num
, d
, false);
1117 value_oppose(*q
, *q
);
1120 value_set_si(EV
.d
, 1);
1122 value_multiply(EV
.x
.n
, *q
, d
);
1124 free_evalue_refs(&EV
);
1130 static void ceil_mod(Value
*coef
, int len
, Value d
, ZZ
& f
, evalue
*EP
, Polyhedron
*PD
)
1134 value_set_si(m
, -1);
1136 Vector_Scale(coef
, coef
, m
, len
);
1139 int j
= normal_mod(coef
, len
, &m
);
1147 values2zz(coef
, num
, len
);
1154 evalue_set_si(&tmp
, 0, 1);
1158 while (j
< len
-1 && (num
[j
] == g
/2 || num
[j
] == 0))
1160 if ((j
< len
-1 && num
[j
] > g
/2) || (j
== len
-1 && num
[j
] >= (g
+1)/2)) {
1161 for (int k
= j
; k
< len
-1; ++k
)
1163 num
[k
] = g
- num
[k
];
1164 num
[len
-1] = g
- 1 - num
[len
-1];
1165 value_assign(tmp
.d
, m
);
1167 zz2value(t
, tmp
.x
.n
);
1173 ZZ t
= num
[len
-1] * f
;
1174 zz2value(t
, tmp
.x
.n
);
1175 value_assign(tmp
.d
, m
);
1178 evalue
*E
= multi_monom(num
);
1182 if (PD
&& !mod_needed(PD
, num
, m
, E
)) {
1184 zz2value(f
, EV
.x
.n
);
1185 value_assign(EV
.d
, m
);
1190 value_set_si(EV
.x
.n
, 1);
1191 value_assign(EV
.d
, m
);
1193 value_clear(EV
.x
.n
);
1194 value_set_si(EV
.d
, 0);
1195 EV
.x
.p
= new_enode(fractional
, 3, -1);
1196 evalue_copy(&EV
.x
.p
->arr
[0], E
);
1197 evalue_set_si(&EV
.x
.p
->arr
[1], 0, 1);
1198 value_init(EV
.x
.p
->arr
[2].x
.n
);
1199 zz2value(f
, EV
.x
.p
->arr
[2].x
.n
);
1200 value_set_si(EV
.x
.p
->arr
[2].d
, 1);
1205 free_evalue_refs(&EV
);
1206 free_evalue_refs(E
);
1210 free_evalue_refs(&tmp
);
1216 evalue
* bv_ceil3(Value
*coef
, int len
, Value d
, Polyhedron
*P
)
1218 Vector
*val
= Vector_Alloc(len
);
1222 value_set_si(t
, -1);
1223 Vector_Scale(coef
, val
->p
, t
, len
);
1224 value_absolute(t
, d
);
1227 values2zz(val
->p
, num
, len
);
1228 evalue
*EP
= multi_monom(num
);
1232 value_init(tmp
.x
.n
);
1233 value_set_si(tmp
.x
.n
, 1);
1234 value_assign(tmp
.d
, t
);
1240 ceil_mod(val
->p
, len
, t
, one
, EP
, P
);
1243 /* copy EP to malloc'ed evalue */
1249 free_evalue_refs(&tmp
);
1256 evalue
* lattice_point(
1257 Polyhedron
*i
, vec_ZZ
& lambda
, Matrix
*W
, Value lcm
, Polyhedron
*PD
)
1259 unsigned nparam
= W
->NbColumns
- 1;
1261 Matrix
* Rays
= rays2(i
);
1262 Matrix
*T
= Transpose(Rays
);
1263 Matrix
*T2
= Matrix_Copy(T
);
1264 Matrix
*inv
= Matrix_Alloc(T2
->NbRows
, T2
->NbColumns
);
1265 int ok
= Matrix_Inverse(T2
, inv
);
1270 matrix2zz(W
, vertex
, W
->NbRows
, W
->NbColumns
);
1273 num
= lambda
* vertex
;
1275 evalue
*EP
= multi_monom(num
);
1279 value_init(tmp
.x
.n
);
1280 value_set_si(tmp
.x
.n
, 1);
1281 value_assign(tmp
.d
, lcm
);
1285 Matrix
*L
= Matrix_Alloc(inv
->NbRows
, W
->NbColumns
);
1286 Matrix_Product(inv
, W
, L
);
1289 matrix2zz(T
, RT
, T
->NbRows
, T
->NbColumns
);
1292 vec_ZZ p
= lambda
* RT
;
1294 for (int i
= 0; i
< L
->NbRows
; ++i
) {
1295 ceil_mod(L
->p
[i
], nparam
+1, lcm
, p
[i
], EP
, PD
);
1301 free_evalue_refs(&tmp
);
1305 evalue
* lattice_point(
1306 Polyhedron
*i
, vec_ZZ
& lambda
, Matrix
*W
, Value lcm
, Polyhedron
*PD
)
1308 Matrix
*T
= Transpose(W
);
1309 unsigned nparam
= T
->NbRows
- 1;
1311 evalue
*EP
= new evalue();
1313 evalue_set_si(EP
, 0, 1);
1316 Vector
*val
= Vector_Alloc(nparam
+1);
1317 value_set_si(val
->p
[nparam
], 1);
1318 ZZ
offset(INIT_VAL
, 0);
1320 vertex_period(i
, lambda
, T
, lcm
, 0, val
, EP
, &ev
, offset
);
1323 free_evalue_refs(&ev
);
1334 Param_Vertices
* V
, Polyhedron
*i
, vec_ZZ
& lambda
, term_info
* term
,
1337 unsigned nparam
= V
->Vertex
->NbColumns
- 2;
1338 unsigned dim
= i
->Dimension
;
1340 vertex
.SetDims(V
->Vertex
->NbRows
, nparam
+1);
1344 value_set_si(lcm
, 1);
1345 for (int j
= 0; j
< V
->Vertex
->NbRows
; ++j
) {
1346 value_lcm(lcm
, V
->Vertex
->p
[j
][nparam
+1], &lcm
);
1348 if (value_notone_p(lcm
)) {
1349 Matrix
* mv
= Matrix_Alloc(dim
, nparam
+1);
1350 for (int j
= 0 ; j
< dim
; ++j
) {
1351 value_division(tmp
, lcm
, V
->Vertex
->p
[j
][nparam
+1]);
1352 Vector_Scale(V
->Vertex
->p
[j
], mv
->p
[j
], tmp
, nparam
+1);
1355 term
->E
= lattice_point(i
, lambda
, mv
, lcm
, PD
);
1363 for (int i
= 0; i
< V
->Vertex
->NbRows
; ++i
) {
1364 assert(value_one_p(V
->Vertex
->p
[i
][nparam
+1])); // for now
1365 values2zz(V
->Vertex
->p
[i
], vertex
[i
], nparam
+1);
1369 num
= lambda
* vertex
;
1373 for (int j
= 0; j
< nparam
; ++j
)
1379 term
->E
= multi_monom(num
);
1383 term
->constant
= num
[nparam
];
1386 term
->coeff
= num
[p
];
1393 static void normalize(ZZ
& sign
, ZZ
& num
, vec_ZZ
& den
)
1395 unsigned dim
= den
.length();
1399 for (int j
= 0; j
< den
.length(); ++j
) {
1403 den
[j
] = abs(den
[j
]);
1412 * f: the powers in the denominator for the remaining vars
1413 * each row refers to a factor
1414 * den_s: for each factor, the power of (s+1)
1416 * num_s: powers in the numerator corresponding to the summed vars
1417 * num_p: powers in the numerator corresponidng to the remaining vars
1418 * number of rays in cone: "dim" = "k"
1419 * length of each ray: "dim" = "d"
1420 * for now, it is assume: k == d
1422 * den_p: for each factor
1423 * 0: independent of remaining vars
1424 * 1: power corresponds to corresponding row in f
1425 * -1: power is inverse of corresponding row in f
1427 static void normalize(ZZ
& sign
,
1428 ZZ
& num_s
, vec_ZZ
& num_p
, vec_ZZ
& den_s
, vec_ZZ
& den_p
,
1431 unsigned dim
= f
.NumRows();
1432 unsigned nparam
= num_p
.length();
1433 unsigned nvar
= dim
- nparam
;
1437 for (int j
= 0; j
< den_s
.length(); ++j
) {
1438 if (den_s
[j
] == 0) {
1443 for (k
= 0; k
< nparam
; ++k
)
1457 den_s
[j
] = abs(den_s
[j
]);
1466 struct counter
: public polar_decomposer
{
1478 counter(Polyhedron
*P
) {
1481 randomvector(P
, lambda
, dim
);
1482 rays
.SetDims(dim
, dim
);
1487 void start(unsigned MaxRays
);
1493 virtual void handle_polar(Polyhedron
*P
, int sign
);
1496 void counter::handle_polar(Polyhedron
*C
, int s
)
1499 assert(C
->NbRays
-1 == dim
);
1500 add_rays(rays
, C
, &r
);
1501 for (int k
= 0; k
< dim
; ++k
) {
1502 assert(lambda
* rays
[k
] != 0);
1507 lattice_point(P
->Ray
[j
]+1, C
, vertex
);
1508 num
= vertex
* lambda
;
1509 den
= rays
* lambda
;
1510 normalize(sign
, num
, den
);
1513 dpoly
n(dim
, den
[0], 1);
1514 for (int k
= 1; k
< dim
; ++k
) {
1515 dpoly
fact(dim
, den
[k
], 1);
1518 d
.div(n
, count
, sign
);
1521 void counter::start(unsigned MaxRays
)
1523 for (j
= 0; j
< P
->NbRays
; ++j
) {
1524 Polyhedron
*C
= supporting_cone(P
, j
);
1525 decompose(C
, MaxRays
);
1529 typedef Polyhedron
* Polyhedron_p
;
1531 void barvinok_count(Polyhedron
*P
, Value
* result
, unsigned NbMaxCons
)
1533 Polyhedron
** vcone
;
1542 value_set_si(*result
, 0);
1546 for (; r
< P
->NbRays
; ++r
)
1547 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1]))
1549 if (P
->NbBid
!=0 || r
< P
->NbRays
) {
1550 value_set_si(*result
, -1);
1554 P
= remove_equalities(P
);
1557 value_set_si(*result
, 0);
1563 value_set_si(factor
, 1);
1564 Q
= Polyhedron_Reduce(P
, &factor
);
1571 if (P
->Dimension
== 0) {
1572 value_assign(*result
, factor
);
1575 value_clear(factor
);
1580 cnt
.start(NbMaxCons
);
1582 assert(value_one_p(&cnt
.count
[0]._mp_den
));
1583 value_multiply(*result
, &cnt
.count
[0]._mp_num
, factor
);
1587 value_clear(factor
);
1590 static void uni_polynom(int param
, Vector
*c
, evalue
*EP
)
1592 unsigned dim
= c
->Size
-2;
1594 value_set_si(EP
->d
,0);
1595 EP
->x
.p
= new_enode(polynomial
, dim
+1, param
+1);
1596 for (int j
= 0; j
<= dim
; ++j
)
1597 evalue_set(&EP
->x
.p
->arr
[j
], c
->p
[j
], c
->p
[dim
+1]);
1600 static void multi_polynom(Vector
*c
, evalue
* X
, evalue
*EP
)
1602 unsigned dim
= c
->Size
-2;
1606 evalue_set(&EC
, c
->p
[dim
], c
->p
[dim
+1]);
1609 evalue_set(EP
, c
->p
[dim
], c
->p
[dim
+1]);
1611 for (int i
= dim
-1; i
>= 0; --i
) {
1613 value_assign(EC
.x
.n
, c
->p
[i
]);
1616 free_evalue_refs(&EC
);
1619 Polyhedron
*unfringe (Polyhedron
*P
, unsigned MaxRays
)
1621 int len
= P
->Dimension
+2;
1622 Polyhedron
*T
, *R
= P
;
1625 Vector
*row
= Vector_Alloc(len
);
1626 value_set_si(row
->p
[0], 1);
1628 R
= DomainConstraintSimplify(Polyhedron_Copy(P
), MaxRays
);
1630 Matrix
*M
= Matrix_Alloc(2, len
-1);
1631 value_set_si(M
->p
[1][len
-2], 1);
1632 for (int v
= 0; v
< P
->Dimension
; ++v
) {
1633 value_set_si(M
->p
[0][v
], 1);
1634 Polyhedron
*I
= Polyhedron_Image(P
, M
, 2+1);
1635 value_set_si(M
->p
[0][v
], 0);
1636 for (int r
= 0; r
< I
->NbConstraints
; ++r
) {
1637 if (value_zero_p(I
->Constraint
[r
][0]))
1639 if (value_zero_p(I
->Constraint
[r
][1]))
1641 if (value_one_p(I
->Constraint
[r
][1]))
1643 if (value_mone_p(I
->Constraint
[r
][1]))
1645 value_absolute(g
, I
->Constraint
[r
][1]);
1646 Vector_Set(row
->p
+1, 0, len
-2);
1647 value_division(row
->p
[1+v
], I
->Constraint
[r
][1], g
);
1648 mpz_fdiv_q(row
->p
[len
-1], I
->Constraint
[r
][2], g
);
1650 R
= AddConstraints(row
->p
, 1, R
, MaxRays
);
1662 static Polyhedron
*reduce_domain(Polyhedron
*D
, Matrix
*CT
, Polyhedron
*CEq
,
1663 Polyhedron
**fVD
, int nd
, unsigned MaxRays
)
1668 Dt
= CT
? DomainPreimage(D
, CT
, MaxRays
) : D
;
1669 Polyhedron
*rVD
= DomainIntersection(Dt
, CEq
, MaxRays
);
1671 /* if rVD is empty or too small in geometric dimension */
1672 if(!rVD
|| emptyQ(rVD
) ||
1673 (rVD
->Dimension
-rVD
->NbEq
< Dt
->Dimension
-Dt
->NbEq
-CEq
->NbEq
)) {
1678 return 0; /* empty validity domain */
1684 fVD
[nd
] = Domain_Copy(rVD
);
1685 for (int i
= 0 ; i
< nd
; ++i
) {
1686 Polyhedron
*I
= DomainIntersection(fVD
[nd
], fVD
[i
], MaxRays
);
1691 Polyhedron
*F
= DomainSimplify(I
, fVD
[nd
], MaxRays
);
1693 Polyhedron
*T
= rVD
;
1694 rVD
= DomainDifference(rVD
, F
, MaxRays
);
1701 rVD
= DomainConstraintSimplify(rVD
, MaxRays
);
1703 Domain_Free(fVD
[nd
]);
1710 barvinok_count(rVD
, &c
, MaxRays
);
1711 if (value_zero_p(c
)) {
1720 static bool Polyhedron_is_infinite(Polyhedron
*P
, unsigned nparam
)
1723 for (r
= 0; r
< P
->NbRays
; ++r
)
1724 if (value_zero_p(P
->Ray
[r
][0]) ||
1725 value_zero_p(P
->Ray
[r
][P
->Dimension
+1])) {
1727 for (i
= P
->Dimension
- nparam
; i
< P
->Dimension
; ++i
)
1728 if (value_notzero_p(P
->Ray
[r
][i
+1]))
1730 if (i
>= P
->Dimension
)
1733 return r
< P
->NbRays
;
1736 /* Check whether all rays point in the positive directions
1737 * for the parameters
1739 static bool Polyhedron_has_positive_rays(Polyhedron
*P
, unsigned nparam
)
1742 for (r
= 0; r
< P
->NbRays
; ++r
)
1743 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1])) {
1745 for (i
= P
->Dimension
- nparam
; i
< P
->Dimension
; ++i
)
1746 if (value_neg_p(P
->Ray
[r
][i
+1]))
1752 typedef evalue
* evalue_p
;
1754 struct enumerator
: public polar_decomposer
{
1768 enumerator(Polyhedron
*P
, unsigned dim
, unsigned nbV
) {
1772 randomvector(P
, lambda
, dim
);
1773 rays
.SetDims(dim
, dim
);
1775 c
= Vector_Alloc(dim
+2);
1777 vE
= new evalue_p
[nbV
];
1778 for (int j
= 0; j
< nbV
; ++j
)
1784 void decompose_at(Param_Vertices
*V
, int _i
, unsigned MaxRays
) {
1785 Polyhedron
*C
= supporting_cone_p(P
, V
);
1789 vE
[_i
] = new evalue
;
1790 value_init(vE
[_i
]->d
);
1791 evalue_set_si(vE
[_i
], 0, 1);
1793 decompose(C
, MaxRays
);
1800 for (int j
= 0; j
< nbV
; ++j
)
1802 free_evalue_refs(vE
[j
]);
1808 virtual void handle_polar(Polyhedron
*P
, int sign
);
1811 void enumerator::handle_polar(Polyhedron
*C
, int s
)
1814 assert(C
->NbRays
-1 == dim
);
1815 add_rays(rays
, C
, &r
);
1816 for (int k
= 0; k
< dim
; ++k
) {
1817 assert(lambda
* rays
[k
] != 0);
1822 lattice_point(V
, C
, lambda
, &num
, 0);
1823 den
= rays
* lambda
;
1824 normalize(sign
, num
.constant
, den
);
1826 dpoly
n(dim
, den
[0], 1);
1827 for (int k
= 1; k
< dim
; ++k
) {
1828 dpoly
fact(dim
, den
[k
], 1);
1831 if (num
.E
!= NULL
) {
1832 ZZ
one(INIT_VAL
, 1);
1833 dpoly_n
d(dim
, num
.constant
, one
);
1836 multi_polynom(c
, num
.E
, &EV
);
1838 free_evalue_refs(&EV
);
1839 free_evalue_refs(num
.E
);
1841 } else if (num
.pos
!= -1) {
1842 dpoly_n
d(dim
, num
.constant
, num
.coeff
);
1845 uni_polynom(num
.pos
, c
, &EV
);
1847 free_evalue_refs(&EV
);
1849 mpq_set_si(count
, 0, 1);
1850 dpoly
d(dim
, num
.constant
);
1851 d
.div(n
, count
, sign
);
1854 evalue_set(&EV
, &count
[0]._mp_num
, &count
[0]._mp_den
);
1856 free_evalue_refs(&EV
);
1860 evalue
* barvinok_enumerate_ev(Polyhedron
*P
, Polyhedron
* C
, unsigned MaxRays
)
1862 //P = unfringe(P, MaxRays);
1863 Polyhedron
*CEq
= NULL
, *rVD
, *pVD
, *CA
;
1865 Param_Polyhedron
*PP
= NULL
;
1866 Param_Domain
*D
, *next
;
1869 unsigned nparam
= C
->Dimension
;
1871 ALLOC(evalue
, eres
);
1872 value_init(eres
->d
);
1873 value_set_si(eres
->d
, 0);
1876 value_init(factor
.d
);
1877 evalue_set_si(&factor
, 1, 1);
1879 CA
= align_context(C
, P
->Dimension
, MaxRays
);
1880 P
= DomainIntersection(P
, CA
, MaxRays
);
1881 Polyhedron_Free(CA
);
1883 if (C
->Dimension
== 0 || emptyQ(P
)) {
1885 eres
->x
.p
= new_enode(partition
, 2, C
->Dimension
);
1886 EVALUE_SET_DOMAIN(eres
->x
.p
->arr
[0],
1887 DomainConstraintSimplify(CEq
? CEq
: Polyhedron_Copy(C
), MaxRays
));
1888 value_set_si(eres
->x
.p
->arr
[1].d
, 1);
1889 value_init(eres
->x
.p
->arr
[1].x
.n
);
1891 value_set_si(eres
->x
.p
->arr
[1].x
.n
, 0);
1893 barvinok_count(P
, &eres
->x
.p
->arr
[1].x
.n
, MaxRays
);
1895 emul(&factor
, eres
);
1896 reduce_evalue(eres
);
1897 free_evalue_refs(&factor
);
1902 Param_Polyhedron_Free(PP
);
1906 if (Polyhedron_is_infinite(P
, nparam
))
1911 P
= remove_equalities_p(P
, P
->Dimension
-nparam
, &f
);
1915 if (P
->Dimension
== nparam
) {
1917 P
= Universe_Polyhedron(0);
1921 Polyhedron
*Q
= ParamPolyhedron_Reduce(P
, P
->Dimension
-nparam
, &factor
);
1924 if (Q
->Dimension
== nparam
) {
1926 P
= Universe_Polyhedron(0);
1931 Polyhedron
*oldP
= P
;
1932 PP
= Polyhedron2Param_SimplifiedDomain(&P
,C
,MaxRays
,&CEq
,&CT
);
1934 Polyhedron_Free(oldP
);
1936 if (isIdentity(CT
)) {
1940 assert(CT
->NbRows
!= CT
->NbColumns
);
1941 if (CT
->NbRows
== 1) // no more parameters
1943 nparam
= CT
->NbRows
- 1;
1946 unsigned dim
= P
->Dimension
- nparam
;
1948 enumerator
et(P
, dim
, PP
->nbV
);
1951 for (nd
= 0, D
=PP
->D
; D
; ++nd
, D
=D
->next
);
1952 struct section
{ Polyhedron
*D
; evalue E
; };
1953 section
*s
= new section
[nd
];
1954 Polyhedron
**fVD
= new Polyhedron_p
[nd
];
1956 for(nd
= 0, D
=PP
->D
; D
; D
=next
) {
1959 Polyhedron
*rVD
= reduce_domain(D
->Domain
, CT
, CEq
,
1964 pVD
= CT
? DomainImage(rVD
,CT
,MaxRays
) : rVD
;
1966 value_init(s
[nd
].E
.d
);
1967 evalue_set_si(&s
[nd
].E
, 0, 1);
1969 FORALL_PVertex_in_ParamPolyhedron(V
,D
,PP
) // _i is internal counter
1971 et
.decompose_at(V
, _i
, MaxRays
);
1972 eadd(et
.vE
[_i
] , &s
[nd
].E
);
1973 END_FORALL_PVertex_in_ParamPolyhedron
;
1974 reduce_in_domain(&s
[nd
].E
, pVD
);
1977 addeliminatedparams_evalue(&s
[nd
].E
, CT
);
1985 evalue_set_si(eres
, 0, 1);
1987 eres
->x
.p
= new_enode(partition
, 2*nd
, C
->Dimension
);
1988 for (int j
= 0; j
< nd
; ++j
) {
1989 EVALUE_SET_DOMAIN(eres
->x
.p
->arr
[2*j
], s
[j
].D
);
1990 value_clear(eres
->x
.p
->arr
[2*j
+1].d
);
1991 eres
->x
.p
->arr
[2*j
+1] = s
[j
].E
;
1992 Domain_Free(fVD
[j
]);
2000 Polyhedron_Free(CEq
);
2005 Enumeration
* barvinok_enumerate(Polyhedron
*P
, Polyhedron
* C
, unsigned MaxRays
)
2007 evalue
*EP
= barvinok_enumerate_ev(P
, C
, MaxRays
);
2009 return partition2enumeration(EP
);
2012 static void SwapColumns(Value
**V
, int n
, int i
, int j
)
2014 for (int r
= 0; r
< n
; ++r
)
2015 value_swap(V
[r
][i
], V
[r
][j
]);
2018 static void SwapColumns(Polyhedron
*P
, int i
, int j
)
2020 SwapColumns(P
->Constraint
, P
->NbConstraints
, i
, j
);
2021 SwapColumns(P
->Ray
, P
->NbRays
, i
, j
);
2024 static void negative_test_constraint(Value
*l
, Value
*u
, Value
*c
, int pos
,
2027 value_oppose(*v
, u
[pos
+1]);
2028 Vector_Combine(l
+1, u
+1, c
+1, *v
, l
[pos
+1], len
-1);
2029 value_multiply(*v
, *v
, l
[pos
+1]);
2030 value_substract(c
[len
-1], c
[len
-1], *v
);
2031 value_set_si(*v
, -1);
2032 Vector_Scale(c
+1, c
+1, *v
, len
-1);
2033 value_decrement(c
[len
-1], c
[len
-1]);
2034 ConstraintSimplify(c
, c
, len
, v
);
2037 static bool parallel_constraints(Value
*l
, Value
*u
, Value
*c
, int pos
,
2046 Vector_Gcd(&l
[1+pos
], len
, &g1
);
2047 Vector_Gcd(&u
[1+pos
], len
, &g2
);
2048 Vector_Combine(l
+1+pos
, u
+1+pos
, c
+1, g2
, g1
, len
);
2049 parallel
= First_Non_Zero(c
+1, len
) == -1;
2057 static void negative_test_constraint7(Value
*l
, Value
*u
, Value
*c
, int pos
,
2058 int exist
, int len
, Value
*v
)
2063 Vector_Gcd(&u
[1+pos
], exist
, v
);
2064 Vector_Gcd(&l
[1+pos
], exist
, &g
);
2065 Vector_Combine(l
+1, u
+1, c
+1, *v
, g
, len
-1);
2066 value_multiply(*v
, *v
, g
);
2067 value_substract(c
[len
-1], c
[len
-1], *v
);
2068 value_set_si(*v
, -1);
2069 Vector_Scale(c
+1, c
+1, *v
, len
-1);
2070 value_decrement(c
[len
-1], c
[len
-1]);
2071 ConstraintSimplify(c
, c
, len
, v
);
2076 static void oppose_constraint(Value
*c
, int len
, Value
*v
)
2078 value_set_si(*v
, -1);
2079 Vector_Scale(c
+1, c
+1, *v
, len
-1);
2080 value_decrement(c
[len
-1], c
[len
-1]);
2083 static bool SplitOnConstraint(Polyhedron
*P
, int i
, int l
, int u
,
2084 int nvar
, int len
, int exist
, int MaxRays
,
2085 Vector
*row
, Value
& f
, bool independent
,
2086 Polyhedron
**pos
, Polyhedron
**neg
)
2088 negative_test_constraint(P
->Constraint
[l
], P
->Constraint
[u
],
2089 row
->p
, nvar
+i
, len
, &f
);
2090 *neg
= AddConstraints(row
->p
, 1, P
, MaxRays
);
2092 /* We found an independent, but useless constraint
2093 * Maybe we should detect this earlier and not
2094 * mark the variable as INDEPENDENT
2096 if (emptyQ((*neg
))) {
2097 Polyhedron_Free(*neg
);
2101 oppose_constraint(row
->p
, len
, &f
);
2102 *pos
= AddConstraints(row
->p
, 1, P
, MaxRays
);
2104 if (emptyQ((*pos
))) {
2105 Polyhedron_Free(*neg
);
2106 Polyhedron_Free(*pos
);
2114 * unimodularly transform P such that constraint r is transformed
2115 * into a constraint that involves only a single (the first)
2116 * existential variable
2119 static Polyhedron
*rotate_along(Polyhedron
*P
, int r
, int nvar
, int exist
,
2125 Vector
*row
= Vector_Alloc(exist
);
2126 Vector_Copy(P
->Constraint
[r
]+1+nvar
, row
->p
, exist
);
2127 Vector_Gcd(row
->p
, exist
, &g
);
2128 if (value_notone_p(g
))
2129 Vector_AntiScale(row
->p
, row
->p
, g
, exist
);
2132 Matrix
*M
= unimodular_complete(row
);
2133 Matrix
*M2
= Matrix_Alloc(P
->Dimension
+1, P
->Dimension
+1);
2134 for (r
= 0; r
< nvar
; ++r
)
2135 value_set_si(M2
->p
[r
][r
], 1);
2136 for ( ; r
< nvar
+exist
; ++r
)
2137 Vector_Copy(M
->p
[r
-nvar
], M2
->p
[r
]+nvar
, exist
);
2138 for ( ; r
< P
->Dimension
+1; ++r
)
2139 value_set_si(M2
->p
[r
][r
], 1);
2140 Polyhedron
*T
= Polyhedron_Image(P
, M2
, MaxRays
);
2149 static bool SplitOnVar(Polyhedron
*P
, int i
,
2150 int nvar
, int len
, int exist
, int MaxRays
,
2151 Vector
*row
, Value
& f
, bool independent
,
2152 Polyhedron
**pos
, Polyhedron
**neg
)
2156 for (int l
= P
->NbEq
; l
< P
->NbConstraints
; ++l
) {
2157 if (value_negz_p(P
->Constraint
[l
][nvar
+i
+1]))
2161 for (j
= 0; j
< exist
; ++j
)
2162 if (j
!= i
&& value_notzero_p(P
->Constraint
[l
][nvar
+j
+1]))
2168 for (int u
= P
->NbEq
; u
< P
->NbConstraints
; ++u
) {
2169 if (value_posz_p(P
->Constraint
[u
][nvar
+i
+1]))
2173 for (j
= 0; j
< exist
; ++j
)
2174 if (j
!= i
&& value_notzero_p(P
->Constraint
[u
][nvar
+j
+1]))
2180 if (SplitOnConstraint(P
, i
, l
, u
,
2181 nvar
, len
, exist
, MaxRays
,
2182 row
, f
, independent
,
2186 SwapColumns(*neg
, nvar
+1, nvar
+1+i
);
2196 static bool double_bound_pair(Polyhedron
*P
, int nvar
, int exist
,
2197 int i
, int l1
, int l2
,
2198 Polyhedron
**pos
, Polyhedron
**neg
)
2202 Vector
*row
= Vector_Alloc(P
->Dimension
+2);
2203 value_set_si(row
->p
[0], 1);
2204 value_oppose(f
, P
->Constraint
[l1
][nvar
+i
+1]);
2205 Vector_Combine(P
->Constraint
[l1
]+1, P
->Constraint
[l2
]+1,
2207 P
->Constraint
[l2
][nvar
+i
+1], f
,
2209 ConstraintSimplify(row
->p
, row
->p
, P
->Dimension
+2, &f
);
2210 *pos
= AddConstraints(row
->p
, 1, P
, 0);
2211 value_set_si(f
, -1);
2212 Vector_Scale(row
->p
+1, row
->p
+1, f
, P
->Dimension
+1);
2213 value_decrement(row
->p
[P
->Dimension
+1], row
->p
[P
->Dimension
+1]);
2214 *neg
= AddConstraints(row
->p
, 1, P
, 0);
2218 return !emptyQ((*pos
)) && !emptyQ((*neg
));
2221 static bool double_bound(Polyhedron
*P
, int nvar
, int exist
,
2222 Polyhedron
**pos
, Polyhedron
**neg
)
2224 for (int i
= 0; i
< exist
; ++i
) {
2226 for (l1
= P
->NbEq
; l1
< P
->NbConstraints
; ++l1
) {
2227 if (value_negz_p(P
->Constraint
[l1
][nvar
+i
+1]))
2229 for (l2
= l1
+ 1; l2
< P
->NbConstraints
; ++l2
) {
2230 if (value_negz_p(P
->Constraint
[l2
][nvar
+i
+1]))
2232 if (double_bound_pair(P
, nvar
, exist
, i
, l1
, l2
, pos
, neg
))
2236 for (l1
= P
->NbEq
; l1
< P
->NbConstraints
; ++l1
) {
2237 if (value_posz_p(P
->Constraint
[l1
][nvar
+i
+1]))
2239 if (l1
< P
->NbConstraints
)
2240 for (l2
= l1
+ 1; l2
< P
->NbConstraints
; ++l2
) {
2241 if (value_posz_p(P
->Constraint
[l2
][nvar
+i
+1]))
2243 if (double_bound_pair(P
, nvar
, exist
, i
, l1
, l2
, pos
, neg
))
2255 INDEPENDENT
= 1 << 2,
2259 static evalue
* enumerate_or(Polyhedron
*D
,
2260 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
2263 fprintf(stderr
, "\nER: Or\n");
2264 #endif /* DEBUG_ER */
2266 Polyhedron
*N
= D
->next
;
2269 barvinok_enumerate_e(D
, exist
, nparam
, MaxRays
);
2272 for (D
= N
; D
; D
= N
) {
2277 barvinok_enumerate_e(D
, exist
, nparam
, MaxRays
);
2280 free_evalue_refs(EN
);
2290 static evalue
* enumerate_sum(Polyhedron
*P
,
2291 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
2293 int nvar
= P
->Dimension
- exist
- nparam
;
2294 int toswap
= nvar
< exist
? nvar
: exist
;
2295 for (int i
= 0; i
< toswap
; ++i
)
2296 SwapColumns(P
, 1 + i
, nvar
+exist
- i
);
2300 fprintf(stderr
, "\nER: Sum\n");
2301 #endif /* DEBUG_ER */
2303 evalue
*EP
= barvinok_enumerate_e(P
, exist
, nparam
, MaxRays
);
2305 for (int i
= 0; i
< /* nvar */ nparam
; ++i
) {
2306 Matrix
*C
= Matrix_Alloc(1, 1 + nparam
+ 1);
2307 value_set_si(C
->p
[0][0], 1);
2309 value_init(split
.d
);
2310 value_set_si(split
.d
, 0);
2311 split
.x
.p
= new_enode(partition
, 4, nparam
);
2312 value_set_si(C
->p
[0][1+i
], 1);
2313 Matrix
*C2
= Matrix_Copy(C
);
2314 EVALUE_SET_DOMAIN(split
.x
.p
->arr
[0],
2315 Constraints2Polyhedron(C2
, MaxRays
));
2317 evalue_set_si(&split
.x
.p
->arr
[1], 1, 1);
2318 value_set_si(C
->p
[0][1+i
], -1);
2319 value_set_si(C
->p
[0][1+nparam
], -1);
2320 EVALUE_SET_DOMAIN(split
.x
.p
->arr
[2],
2321 Constraints2Polyhedron(C
, MaxRays
));
2322 evalue_set_si(&split
.x
.p
->arr
[3], 1, 1);
2324 free_evalue_refs(&split
);
2328 evalue_range_reduction(EP
);
2330 evalue_frac2floor(EP
);
2332 evalue
*sum
= esum(EP
, nvar
);
2334 free_evalue_refs(EP
);
2338 evalue_range_reduction(EP
);
2343 static evalue
* split_sure(Polyhedron
*P
, Polyhedron
*S
,
2344 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
2346 int nvar
= P
->Dimension
- exist
- nparam
;
2348 Matrix
*M
= Matrix_Alloc(exist
, S
->Dimension
+2);
2349 for (int i
= 0; i
< exist
; ++i
)
2350 value_set_si(M
->p
[i
][nvar
+i
+1], 1);
2352 S
= DomainAddRays(S
, M
, MaxRays
);
2354 Polyhedron
*F
= DomainAddRays(P
, M
, MaxRays
);
2355 Polyhedron
*D
= DomainDifference(F
, S
, MaxRays
);
2357 D
= Disjoint_Domain(D
, 0, MaxRays
);
2362 M
= Matrix_Alloc(P
->Dimension
+1-exist
, P
->Dimension
+1);
2363 for (int j
= 0; j
< nvar
; ++j
)
2364 value_set_si(M
->p
[j
][j
], 1);
2365 for (int j
= 0; j
< nparam
+1; ++j
)
2366 value_set_si(M
->p
[nvar
+j
][nvar
+exist
+j
], 1);
2367 Polyhedron
*T
= Polyhedron_Image(S
, M
, MaxRays
);
2368 evalue
*EP
= barvinok_enumerate_e(T
, 0, nparam
, MaxRays
);
2373 for (Polyhedron
*Q
= D
; Q
; Q
= Q
->next
) {
2374 Polyhedron
*N
= Q
->next
;
2376 T
= DomainIntersection(P
, Q
, MaxRays
);
2377 evalue
*E
= barvinok_enumerate_e(T
, exist
, nparam
, MaxRays
);
2379 free_evalue_refs(E
);
2388 static evalue
* enumerate_sure(Polyhedron
*P
,
2389 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
2393 int nvar
= P
->Dimension
- exist
- nparam
;
2399 for (i
= 0; i
< exist
; ++i
) {
2400 Matrix
*M
= Matrix_Alloc(S
->NbConstraints
, S
->Dimension
+2);
2402 value_set_si(lcm
, 1);
2403 for (int j
= 0; j
< S
->NbConstraints
; ++j
) {
2404 if (value_negz_p(S
->Constraint
[j
][1+nvar
+i
]))
2406 if (value_one_p(S
->Constraint
[j
][1+nvar
+i
]))
2408 value_lcm(lcm
, S
->Constraint
[j
][1+nvar
+i
], &lcm
);
2411 for (int j
= 0; j
< S
->NbConstraints
; ++j
) {
2412 if (value_negz_p(S
->Constraint
[j
][1+nvar
+i
]))
2414 if (value_one_p(S
->Constraint
[j
][1+nvar
+i
]))
2416 value_division(f
, lcm
, S
->Constraint
[j
][1+nvar
+i
]);
2417 Vector_Scale(S
->Constraint
[j
], M
->p
[c
], f
, S
->Dimension
+2);
2418 value_substract(M
->p
[c
][S
->Dimension
+1],
2419 M
->p
[c
][S
->Dimension
+1],
2421 value_increment(M
->p
[c
][S
->Dimension
+1],
2422 M
->p
[c
][S
->Dimension
+1]);
2426 S
= AddConstraints(M
->p
[0], c
, S
, MaxRays
);
2441 fprintf(stderr
, "\nER: Sure\n");
2442 #endif /* DEBUG_ER */
2444 return split_sure(P
, S
, exist
, nparam
, MaxRays
);
2447 static evalue
* enumerate_sure2(Polyhedron
*P
,
2448 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
2450 int nvar
= P
->Dimension
- exist
- nparam
;
2452 for (r
= 0; r
< P
->NbRays
; ++r
)
2453 if (value_one_p(P
->Ray
[r
][0]) &&
2454 value_one_p(P
->Ray
[r
][P
->Dimension
+1]))
2460 Matrix
*M
= Matrix_Alloc(nvar
+ 1 + nparam
, P
->Dimension
+2);
2461 for (int i
= 0; i
< nvar
; ++i
)
2462 value_set_si(M
->p
[i
][1+i
], 1);
2463 for (int i
= 0; i
< nparam
; ++i
)
2464 value_set_si(M
->p
[i
+nvar
][1+nvar
+exist
+i
], 1);
2465 Vector_Copy(P
->Ray
[r
]+1+nvar
, M
->p
[nvar
+nparam
]+1+nvar
, exist
);
2466 value_set_si(M
->p
[nvar
+nparam
][0], 1);
2467 value_set_si(M
->p
[nvar
+nparam
][P
->Dimension
+1], 1);
2468 Polyhedron
* F
= Rays2Polyhedron(M
, MaxRays
);
2471 Polyhedron
*I
= DomainIntersection(F
, P
, MaxRays
);
2475 fprintf(stderr
, "\nER: Sure2\n");
2476 #endif /* DEBUG_ER */
2478 return split_sure(P
, I
, exist
, nparam
, MaxRays
);
2481 static evalue
* enumerate_cyclic(Polyhedron
*P
,
2482 unsigned exist
, unsigned nparam
,
2483 evalue
* EP
, int r
, int p
, unsigned MaxRays
)
2485 int nvar
= P
->Dimension
- exist
- nparam
;
2487 /* If EP in its fractional maps only contains references
2488 * to the remainder parameter with appropriate coefficients
2489 * then we could in principle avoid adding existentially
2490 * quantified variables to the validity domains.
2491 * We'd have to replace the remainder by m { p/m }
2492 * and multiply with an appropriate factor that is one
2493 * only in the appropriate range.
2494 * This last multiplication can be avoided if EP
2495 * has a single validity domain with no (further)
2496 * constraints on the remainder parameter
2499 Matrix
*CT
= Matrix_Alloc(nparam
+1, nparam
+3);
2500 Matrix
*M
= Matrix_Alloc(1, 1+nparam
+3);
2501 for (int j
= 0; j
< nparam
; ++j
)
2503 value_set_si(CT
->p
[j
][j
], 1);
2504 value_set_si(CT
->p
[p
][nparam
+1], 1);
2505 value_set_si(CT
->p
[nparam
][nparam
+2], 1);
2506 value_set_si(M
->p
[0][1+p
], -1);
2507 value_absolute(M
->p
[0][1+nparam
], P
->Ray
[0][1+nvar
+exist
+p
]);
2508 value_set_si(M
->p
[0][1+nparam
+1], 1);
2509 Polyhedron
*CEq
= Constraints2Polyhedron(M
, 1);
2511 addeliminatedparams_enum(EP
, CT
, CEq
, MaxRays
, nparam
);
2512 Polyhedron_Free(CEq
);
2518 static void enumerate_vd_add_ray(evalue
*EP
, Matrix
*Rays
, unsigned MaxRays
)
2520 if (value_notzero_p(EP
->d
))
2523 assert(EP
->x
.p
->type
== partition
);
2524 assert(EP
->x
.p
->pos
== EVALUE_DOMAIN(EP
->x
.p
->arr
[0])->Dimension
);
2525 for (int i
= 0; i
< EP
->x
.p
->size
/2; ++i
) {
2526 Polyhedron
*D
= EVALUE_DOMAIN(EP
->x
.p
->arr
[2*i
]);
2527 Polyhedron
*N
= DomainAddRays(D
, Rays
, MaxRays
);
2528 EVALUE_SET_DOMAIN(EP
->x
.p
->arr
[2*i
], N
);
2533 static evalue
* enumerate_line(Polyhedron
*P
,
2534 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
2540 fprintf(stderr
, "\nER: Line\n");
2541 #endif /* DEBUG_ER */
2543 int nvar
= P
->Dimension
- exist
- nparam
;
2545 for (i
= 0; i
< nparam
; ++i
)
2546 if (value_notzero_p(P
->Ray
[0][1+nvar
+exist
+i
]))
2549 for (j
= i
+1; j
< nparam
; ++j
)
2550 if (value_notzero_p(P
->Ray
[0][1+nvar
+exist
+i
]))
2552 assert(j
>= nparam
); // for now
2554 Matrix
*M
= Matrix_Alloc(2, P
->Dimension
+2);
2555 value_set_si(M
->p
[0][0], 1);
2556 value_set_si(M
->p
[0][1+nvar
+exist
+i
], 1);
2557 value_set_si(M
->p
[1][0], 1);
2558 value_set_si(M
->p
[1][1+nvar
+exist
+i
], -1);
2559 value_absolute(M
->p
[1][1+P
->Dimension
], P
->Ray
[0][1+nvar
+exist
+i
]);
2560 value_decrement(M
->p
[1][1+P
->Dimension
], M
->p
[1][1+P
->Dimension
]);
2561 Polyhedron
*S
= AddConstraints(M
->p
[0], 2, P
, MaxRays
);
2562 evalue
*EP
= barvinok_enumerate_e(S
, exist
, nparam
, MaxRays
);
2566 return enumerate_cyclic(P
, exist
, nparam
, EP
, 0, i
, MaxRays
);
2569 static int single_param_pos(Polyhedron
*P
, unsigned exist
, unsigned nparam
,
2572 int nvar
= P
->Dimension
- exist
- nparam
;
2573 if (First_Non_Zero(P
->Ray
[r
]+1, nvar
) != -1)
2575 int i
= First_Non_Zero(P
->Ray
[r
]+1+nvar
+exist
, nparam
);
2578 if (First_Non_Zero(P
->Ray
[r
]+1+nvar
+exist
+1, nparam
-i
-1) != -1)
2583 static evalue
* enumerate_remove_ray(Polyhedron
*P
, int r
,
2584 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
2587 fprintf(stderr
, "\nER: RedundantRay\n");
2588 #endif /* DEBUG_ER */
2592 value_set_si(one
, 1);
2593 int len
= P
->NbRays
-1;
2594 Matrix
*M
= Matrix_Alloc(2 * len
, P
->Dimension
+2);
2595 Vector_Copy(P
->Ray
[0], M
->p
[0], r
* (P
->Dimension
+2));
2596 Vector_Copy(P
->Ray
[r
+1], M
->p
[r
], (len
-r
) * (P
->Dimension
+2));
2597 for (int j
= 0; j
< P
->NbRays
; ++j
) {
2600 Vector_Combine(P
->Ray
[j
], P
->Ray
[r
], M
->p
[len
+j
-(j
>r
)],
2601 one
, P
->Ray
[j
][P
->Dimension
+1], P
->Dimension
+2);
2604 P
= Rays2Polyhedron(M
, MaxRays
);
2606 evalue
*EP
= barvinok_enumerate_e(P
, exist
, nparam
, MaxRays
);
2613 static evalue
* enumerate_redundant_ray(Polyhedron
*P
,
2614 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
2616 assert(P
->NbBid
== 0);
2617 int nvar
= P
->Dimension
- exist
- nparam
;
2621 for (int r
= 0; r
< P
->NbRays
; ++r
) {
2622 if (value_notzero_p(P
->Ray
[r
][P
->Dimension
+1]))
2624 int i1
= single_param_pos(P
, exist
, nparam
, r
);
2627 for (int r2
= r
+1; r2
< P
->NbRays
; ++r2
) {
2628 if (value_notzero_p(P
->Ray
[r2
][P
->Dimension
+1]))
2630 int i2
= single_param_pos(P
, exist
, nparam
, r2
);
2636 value_division(m
, P
->Ray
[r
][1+nvar
+exist
+i1
],
2637 P
->Ray
[r2
][1+nvar
+exist
+i1
]);
2638 value_multiply(m
, m
, P
->Ray
[r2
][1+nvar
+exist
+i1
]);
2639 /* r2 divides r => r redundant */
2640 if (value_eq(m
, P
->Ray
[r
][1+nvar
+exist
+i1
])) {
2642 return enumerate_remove_ray(P
, r
, exist
, nparam
, MaxRays
);
2645 value_division(m
, P
->Ray
[r2
][1+nvar
+exist
+i1
],
2646 P
->Ray
[r
][1+nvar
+exist
+i1
]);
2647 value_multiply(m
, m
, P
->Ray
[r
][1+nvar
+exist
+i1
]);
2648 /* r divides r2 => r2 redundant */
2649 if (value_eq(m
, P
->Ray
[r2
][1+nvar
+exist
+i1
])) {
2651 return enumerate_remove_ray(P
, r2
, exist
, nparam
, MaxRays
);
2659 static Polyhedron
*upper_bound(Polyhedron
*P
,
2660 int pos
, Value
*max
, Polyhedron
**R
)
2669 for (Polyhedron
*Q
= P
; Q
; Q
= N
) {
2671 for (r
= 0; r
< P
->NbRays
; ++r
) {
2672 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1]) &&
2673 value_pos_p(P
->Ray
[r
][1+pos
]))
2676 if (r
< P
->NbRays
) {
2684 for (r
= 0; r
< P
->NbRays
; ++r
) {
2685 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1]))
2687 mpz_fdiv_q(v
, P
->Ray
[r
][1+pos
], P
->Ray
[r
][1+P
->Dimension
]);
2688 if ((!Q
->next
&& r
== 0) || value_gt(v
, *max
))
2689 value_assign(*max
, v
);
2696 static evalue
* enumerate_ray(Polyhedron
*P
,
2697 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
2699 assert(P
->NbBid
== 0);
2700 int nvar
= P
->Dimension
- exist
- nparam
;
2703 for (r
= 0; r
< P
->NbRays
; ++r
)
2704 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1]))
2710 for (r2
= r
+1; r2
< P
->NbRays
; ++r2
)
2711 if (value_zero_p(P
->Ray
[r2
][P
->Dimension
+1]))
2713 if (r2
< P
->NbRays
) {
2715 return enumerate_sum(P
, exist
, nparam
, MaxRays
);
2719 fprintf(stderr
, "\nER: Ray\n");
2720 #endif /* DEBUG_ER */
2726 value_set_si(one
, 1);
2727 int i
= single_param_pos(P
, exist
, nparam
, r
);
2728 assert(i
!= -1); // for now;
2730 Matrix
*M
= Matrix_Alloc(P
->NbRays
, P
->Dimension
+2);
2731 for (int j
= 0; j
< P
->NbRays
; ++j
) {
2732 Vector_Combine(P
->Ray
[j
], P
->Ray
[r
], M
->p
[j
],
2733 one
, P
->Ray
[j
][P
->Dimension
+1], P
->Dimension
+2);
2735 Polyhedron
*S
= Rays2Polyhedron(M
, MaxRays
);
2737 Polyhedron
*D
= DomainDifference(P
, S
, MaxRays
);
2739 // Polyhedron_Print(stderr, P_VALUE_FMT, D);
2740 assert(value_pos_p(P
->Ray
[r
][1+nvar
+exist
+i
])); // for now
2742 D
= upper_bound(D
, nvar
+exist
+i
, &m
, &R
);
2746 M
= Matrix_Alloc(2, P
->Dimension
+2);
2747 value_set_si(M
->p
[0][0], 1);
2748 value_set_si(M
->p
[1][0], 1);
2749 value_set_si(M
->p
[0][1+nvar
+exist
+i
], -1);
2750 value_set_si(M
->p
[1][1+nvar
+exist
+i
], 1);
2751 value_assign(M
->p
[0][1+P
->Dimension
], m
);
2752 value_oppose(M
->p
[1][1+P
->Dimension
], m
);
2753 value_addto(M
->p
[1][1+P
->Dimension
], M
->p
[1][1+P
->Dimension
],
2754 P
->Ray
[r
][1+nvar
+exist
+i
]);
2755 value_decrement(M
->p
[1][1+P
->Dimension
], M
->p
[1][1+P
->Dimension
]);
2756 // Matrix_Print(stderr, P_VALUE_FMT, M);
2757 D
= AddConstraints(M
->p
[0], 2, P
, MaxRays
);
2758 // Polyhedron_Print(stderr, P_VALUE_FMT, D);
2759 value_substract(M
->p
[0][1+P
->Dimension
], M
->p
[0][1+P
->Dimension
],
2760 P
->Ray
[r
][1+nvar
+exist
+i
]);
2761 // Matrix_Print(stderr, P_VALUE_FMT, M);
2762 S
= AddConstraints(M
->p
[0], 1, P
, MaxRays
);
2763 // Polyhedron_Print(stderr, P_VALUE_FMT, S);
2766 evalue
*EP
= barvinok_enumerate_e(D
, exist
, nparam
, MaxRays
);
2771 if (value_notone_p(P
->Ray
[r
][1+nvar
+exist
+i
]))
2772 EP
= enumerate_cyclic(P
, exist
, nparam
, EP
, r
, i
, MaxRays
);
2774 M
= Matrix_Alloc(1, nparam
+2);
2775 value_set_si(M
->p
[0][0], 1);
2776 value_set_si(M
->p
[0][1+i
], 1);
2777 enumerate_vd_add_ray(EP
, M
, MaxRays
);
2782 evalue
*E
= barvinok_enumerate_e(S
, exist
, nparam
, MaxRays
);
2784 free_evalue_refs(E
);
2791 evalue
*ER
= enumerate_or(R
, exist
, nparam
, MaxRays
);
2793 free_evalue_refs(ER
);
2800 static evalue
* new_zero_ep()
2805 evalue_set_si(EP
, 0, 1);
2809 static evalue
* enumerate_vd(Polyhedron
**PA
,
2810 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
2812 Polyhedron
*P
= *PA
;
2813 int nvar
= P
->Dimension
- exist
- nparam
;
2814 Param_Polyhedron
*PP
= NULL
;
2815 Polyhedron
*C
= Universe_Polyhedron(nparam
);
2819 PP
= Polyhedron2Param_SimplifiedDomain(&PR
,C
,MaxRays
,&CEq
,&CT
);
2823 Param_Domain
*D
, *last
;
2826 for (nd
= 0, D
=PP
->D
; D
; D
=D
->next
, ++nd
)
2829 Polyhedron
**VD
= new Polyhedron_p
[nd
];
2830 Polyhedron
**fVD
= new Polyhedron_p
[nd
];
2831 for(nd
= 0, D
=PP
->D
; D
; D
=D
->next
) {
2832 Polyhedron
*rVD
= reduce_domain(D
->Domain
, CT
, CEq
,
2846 /* This doesn't seem to have any effect */
2848 Polyhedron
*CA
= align_context(VD
[0], P
->Dimension
, MaxRays
);
2850 P
= DomainIntersection(P
, CA
, MaxRays
);
2853 Polyhedron_Free(CA
);
2858 if (!EP
&& CT
->NbColumns
!= CT
->NbRows
) {
2859 Polyhedron
*CEqr
= DomainImage(CEq
, CT
, MaxRays
);
2860 Polyhedron
*CA
= align_context(CEqr
, PR
->Dimension
, MaxRays
);
2861 Polyhedron
*I
= DomainIntersection(PR
, CA
, MaxRays
);
2862 Polyhedron_Free(CEqr
);
2863 Polyhedron_Free(CA
);
2865 fprintf(stderr
, "\nER: Eliminate\n");
2866 #endif /* DEBUG_ER */
2867 nparam
-= CT
->NbColumns
- CT
->NbRows
;
2868 EP
= barvinok_enumerate_e(I
, exist
, nparam
, MaxRays
);
2869 nparam
+= CT
->NbColumns
- CT
->NbRows
;
2870 addeliminatedparams_enum(EP
, CT
, CEq
, MaxRays
, nparam
);
2874 Polyhedron_Free(PR
);
2877 if (!EP
&& nd
> 1) {
2879 fprintf(stderr
, "\nER: VD\n");
2880 #endif /* DEBUG_ER */
2881 for (int i
= 0; i
< nd
; ++i
) {
2882 Polyhedron
*CA
= align_context(VD
[i
], P
->Dimension
, MaxRays
);
2883 Polyhedron
*I
= DomainIntersection(P
, CA
, MaxRays
);
2886 EP
= barvinok_enumerate_e(I
, exist
, nparam
, MaxRays
);
2888 evalue
*E
= barvinok_enumerate_e(I
, exist
, nparam
, MaxRays
);
2890 free_evalue_refs(E
);
2894 Polyhedron_Free(CA
);
2898 for (int i
= 0; i
< nd
; ++i
) {
2899 Polyhedron_Free(VD
[i
]);
2900 Polyhedron_Free(fVD
[i
]);
2906 if (!EP
&& nvar
== 0) {
2909 Param_Vertices
*V
, *V2
;
2910 Matrix
* M
= Matrix_Alloc(1, P
->Dimension
+2);
2912 FORALL_PVertex_in_ParamPolyhedron(V
, last
, PP
) {
2914 FORALL_PVertex_in_ParamPolyhedron(V2
, last
, PP
) {
2921 for (int i
= 0; i
< exist
; ++i
) {
2922 value_oppose(f
, V
->Vertex
->p
[i
][nparam
+1]);
2923 Vector_Combine(V
->Vertex
->p
[i
],
2925 M
->p
[0] + 1 + nvar
+ exist
,
2926 V2
->Vertex
->p
[i
][nparam
+1],
2930 for (j
= 0; j
< nparam
; ++j
)
2931 if (value_notzero_p(M
->p
[0][1+nvar
+exist
+j
]))
2935 ConstraintSimplify(M
->p
[0], M
->p
[0],
2936 P
->Dimension
+2, &f
);
2937 value_set_si(M
->p
[0][0], 0);
2938 Polyhedron
*para
= AddConstraints(M
->p
[0], 1, P
,
2941 Polyhedron_Free(para
);
2944 Polyhedron
*pos
, *neg
;
2945 value_set_si(M
->p
[0][0], 1);
2946 value_decrement(M
->p
[0][P
->Dimension
+1],
2947 M
->p
[0][P
->Dimension
+1]);
2948 neg
= AddConstraints(M
->p
[0], 1, P
, MaxRays
);
2949 value_set_si(f
, -1);
2950 Vector_Scale(M
->p
[0]+1, M
->p
[0]+1, f
,
2952 value_decrement(M
->p
[0][P
->Dimension
+1],
2953 M
->p
[0][P
->Dimension
+1]);
2954 value_decrement(M
->p
[0][P
->Dimension
+1],
2955 M
->p
[0][P
->Dimension
+1]);
2956 pos
= AddConstraints(M
->p
[0], 1, P
, MaxRays
);
2957 if (emptyQ(neg
) && emptyQ(pos
)) {
2958 Polyhedron_Free(para
);
2959 Polyhedron_Free(pos
);
2960 Polyhedron_Free(neg
);
2964 fprintf(stderr
, "\nER: Order\n");
2965 #endif /* DEBUG_ER */
2966 EP
= barvinok_enumerate_e(para
, exist
, nparam
, MaxRays
);
2969 E
= barvinok_enumerate_e(pos
, exist
, nparam
, MaxRays
);
2971 free_evalue_refs(E
);
2975 E
= barvinok_enumerate_e(neg
, exist
, nparam
, MaxRays
);
2977 free_evalue_refs(E
);
2980 Polyhedron_Free(para
);
2981 Polyhedron_Free(pos
);
2982 Polyhedron_Free(neg
);
2987 } END_FORALL_PVertex_in_ParamPolyhedron
;
2990 } END_FORALL_PVertex_in_ParamPolyhedron
;
2993 /* Search for vertex coordinate to split on */
2994 /* First look for one independent of the parameters */
2995 FORALL_PVertex_in_ParamPolyhedron(V
, last
, PP
) {
2996 for (int i
= 0; i
< exist
; ++i
) {
2998 for (j
= 0; j
< nparam
; ++j
)
2999 if (value_notzero_p(V
->Vertex
->p
[i
][j
]))
3003 value_set_si(M
->p
[0][0], 1);
3004 Vector_Set(M
->p
[0]+1, 0, nvar
+exist
);
3005 Vector_Copy(V
->Vertex
->p
[i
],
3006 M
->p
[0] + 1 + nvar
+ exist
, nparam
+1);
3007 value_oppose(M
->p
[0][1+nvar
+i
],
3008 V
->Vertex
->p
[i
][nparam
+1]);
3010 Polyhedron
*pos
, *neg
;
3011 value_set_si(M
->p
[0][0], 1);
3012 value_decrement(M
->p
[0][P
->Dimension
+1],
3013 M
->p
[0][P
->Dimension
+1]);
3014 neg
= AddConstraints(M
->p
[0], 1, P
, MaxRays
);
3015 value_set_si(f
, -1);
3016 Vector_Scale(M
->p
[0]+1, M
->p
[0]+1, f
,
3018 value_decrement(M
->p
[0][P
->Dimension
+1],
3019 M
->p
[0][P
->Dimension
+1]);
3020 value_decrement(M
->p
[0][P
->Dimension
+1],
3021 M
->p
[0][P
->Dimension
+1]);
3022 pos
= AddConstraints(M
->p
[0], 1, P
, MaxRays
);
3023 if (emptyQ(neg
) || emptyQ(pos
)) {
3024 Polyhedron_Free(pos
);
3025 Polyhedron_Free(neg
);
3028 Polyhedron_Free(pos
);
3029 value_increment(M
->p
[0][P
->Dimension
+1],
3030 M
->p
[0][P
->Dimension
+1]);
3031 pos
= AddConstraints(M
->p
[0], 1, P
, MaxRays
);
3033 fprintf(stderr
, "\nER: Vertex\n");
3034 #endif /* DEBUG_ER */
3036 EP
= enumerate_or(pos
, exist
, nparam
, MaxRays
);
3041 } END_FORALL_PVertex_in_ParamPolyhedron
;
3045 /* Search for vertex coordinate to split on */
3046 /* Now look for one that depends on the parameters */
3047 FORALL_PVertex_in_ParamPolyhedron(V
, last
, PP
) {
3048 for (int i
= 0; i
< exist
; ++i
) {
3049 value_set_si(M
->p
[0][0], 1);
3050 Vector_Set(M
->p
[0]+1, 0, nvar
+exist
);
3051 Vector_Copy(V
->Vertex
->p
[i
],
3052 M
->p
[0] + 1 + nvar
+ exist
, nparam
+1);
3053 value_oppose(M
->p
[0][1+nvar
+i
],
3054 V
->Vertex
->p
[i
][nparam
+1]);
3056 Polyhedron
*pos
, *neg
;
3057 value_set_si(M
->p
[0][0], 1);
3058 value_decrement(M
->p
[0][P
->Dimension
+1],
3059 M
->p
[0][P
->Dimension
+1]);
3060 neg
= AddConstraints(M
->p
[0], 1, P
, MaxRays
);
3061 value_set_si(f
, -1);
3062 Vector_Scale(M
->p
[0]+1, M
->p
[0]+1, f
,
3064 value_decrement(M
->p
[0][P
->Dimension
+1],
3065 M
->p
[0][P
->Dimension
+1]);
3066 value_decrement(M
->p
[0][P
->Dimension
+1],
3067 M
->p
[0][P
->Dimension
+1]);
3068 pos
= AddConstraints(M
->p
[0], 1, P
, MaxRays
);
3069 if (emptyQ(neg
) || emptyQ(pos
)) {
3070 Polyhedron_Free(pos
);
3071 Polyhedron_Free(neg
);
3074 Polyhedron_Free(pos
);
3075 value_increment(M
->p
[0][P
->Dimension
+1],
3076 M
->p
[0][P
->Dimension
+1]);
3077 pos
= AddConstraints(M
->p
[0], 1, P
, MaxRays
);
3079 fprintf(stderr
, "\nER: ParamVertex\n");
3080 #endif /* DEBUG_ER */
3082 EP
= enumerate_or(pos
, exist
, nparam
, MaxRays
);
3087 } END_FORALL_PVertex_in_ParamPolyhedron
;
3095 Polyhedron_Free(CEq
);
3099 Param_Polyhedron_Free(PP
);
3106 evalue
*barvinok_enumerate_pip(Polyhedron
*P
,
3107 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
3112 evalue
*barvinok_enumerate_pip(Polyhedron
*P
,
3113 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
3115 int nvar
= P
->Dimension
- exist
- nparam
;
3116 evalue
*EP
= new_zero_ep();
3117 Polyhedron
*Q
, *N
, *T
= 0;
3123 fprintf(stderr
, "\nER: PIP\n");
3124 #endif /* DEBUG_ER */
3126 for (int i
= 0; i
< P
->Dimension
; ++i
) {
3129 bool posray
= false;
3130 bool negray
= false;
3131 value_set_si(min
, 0);
3132 for (int j
= 0; j
< P
->NbRays
; ++j
) {
3133 if (value_pos_p(P
->Ray
[j
][1+i
])) {
3135 if (value_zero_p(P
->Ray
[j
][1+P
->Dimension
]))
3137 } else if (value_neg_p(P
->Ray
[j
][1+i
])) {
3139 if (value_zero_p(P
->Ray
[j
][1+P
->Dimension
]))
3143 P
->Ray
[j
][1+i
], P
->Ray
[j
][1+P
->Dimension
]);
3144 if (value_lt(tmp
, min
))
3145 value_assign(min
, tmp
);
3150 assert(!(posray
&& negray
));
3151 assert(!negray
); // for now
3152 Polyhedron
*O
= T
? T
: P
;
3153 /* shift by a safe amount */
3154 Matrix
*M
= Matrix_Alloc(O
->NbRays
, O
->Dimension
+2);
3155 Vector_Copy(O
->Ray
[0], M
->p
[0], O
->NbRays
* (O
->Dimension
+2));
3156 for (int j
= 0; j
< P
->NbRays
; ++j
) {
3157 if (value_notzero_p(M
->p
[j
][1+P
->Dimension
])) {
3158 value_multiply(tmp
, min
, M
->p
[j
][1+P
->Dimension
]);
3159 value_substract(M
->p
[j
][1+i
], M
->p
[j
][1+i
], tmp
);
3164 T
= Rays2Polyhedron(M
, MaxRays
);
3167 /* negating a parameter requires that we substitute in the
3168 * sign again afterwards.
3171 assert(i
< nvar
+exist
);
3173 T
= Polyhedron_Copy(P
);
3174 for (int j
= 0; j
< T
->NbRays
; ++j
)
3175 value_oppose(T
->Ray
[j
][1+i
], T
->Ray
[j
][1+i
]);
3176 for (int j
= 0; j
< T
->NbConstraints
; ++j
)
3177 value_oppose(T
->Constraint
[j
][1+i
], T
->Constraint
[j
][1+i
]);
3183 Polyhedron
*D
= pip_lexmin(T
? T
: P
, exist
, nparam
);
3184 for (Q
= D
; Q
; Q
= N
) {
3188 exist
= Q
->Dimension
- nvar
- nparam
;
3189 E
= barvinok_enumerate_e(Q
, exist
, nparam
, MaxRays
);
3192 free_evalue_refs(E
);
3204 static bool is_single(Value
*row
, int pos
, int len
)
3206 return First_Non_Zero(row
, pos
) == -1 &&
3207 First_Non_Zero(row
+pos
+1, len
-pos
-1) == -1;
3210 static evalue
* barvinok_enumerate_e_r(Polyhedron
*P
,
3211 unsigned exist
, unsigned nparam
, unsigned MaxRays
);
3214 static int er_level
= 0;
3216 evalue
* barvinok_enumerate_e(Polyhedron
*P
,
3217 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
3219 fprintf(stderr
, "\nER: level %i\n", er_level
);
3220 int nvar
= P
->Dimension
- exist
- nparam
;
3221 fprintf(stderr
, "%d %d %d\n", nvar
, exist
, nparam
);
3223 Polyhedron_Print(stderr
, P_VALUE_FMT
, P
);
3225 P
= DomainConstraintSimplify(Polyhedron_Copy(P
), MaxRays
);
3226 evalue
*EP
= barvinok_enumerate_e_r(P
, exist
, nparam
, MaxRays
);
3232 evalue
* barvinok_enumerate_e(Polyhedron
*P
,
3233 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
3235 P
= DomainConstraintSimplify(Polyhedron_Copy(P
), MaxRays
);
3236 evalue
*EP
= barvinok_enumerate_e_r(P
, exist
, nparam
, MaxRays
);
3242 static evalue
* barvinok_enumerate_e_r(Polyhedron
*P
,
3243 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
3246 Polyhedron
*U
= Universe_Polyhedron(nparam
);
3247 evalue
*EP
= barvinok_enumerate_ev(P
, U
, MaxRays
);
3248 //char *param_name[] = {"P", "Q", "R", "S", "T" };
3249 //print_evalue(stdout, EP, param_name);
3254 int nvar
= P
->Dimension
- exist
- nparam
;
3255 int len
= P
->Dimension
+ 2;
3258 return new_zero_ep();
3260 if (nvar
== 0 && nparam
== 0) {
3261 evalue
*EP
= new_zero_ep();
3262 barvinok_count(P
, &EP
->x
.n
, MaxRays
);
3263 if (value_pos_p(EP
->x
.n
))
3264 value_set_si(EP
->x
.n
, 1);
3269 for (r
= 0; r
< P
->NbRays
; ++r
)
3270 if (value_zero_p(P
->Ray
[r
][0]) ||
3271 value_zero_p(P
->Ray
[r
][P
->Dimension
+1])) {
3273 for (i
= 0; i
< nvar
; ++i
)
3274 if (value_notzero_p(P
->Ray
[r
][i
+1]))
3278 for (i
= nvar
+ exist
; i
< nvar
+ exist
+ nparam
; ++i
)
3279 if (value_notzero_p(P
->Ray
[r
][i
+1]))
3281 if (i
>= nvar
+ exist
+ nparam
)
3284 if (r
< P
->NbRays
) {
3285 evalue
*EP
= new_zero_ep();
3286 value_set_si(EP
->x
.n
, -1);
3291 for (r
= 0; r
< P
->NbEq
; ++r
)
3292 if ((first
= First_Non_Zero(P
->Constraint
[r
]+1+nvar
, exist
)) != -1)
3295 if (First_Non_Zero(P
->Constraint
[r
]+1+nvar
+first
+1,
3296 exist
-first
-1) != -1) {
3297 Polyhedron
*T
= rotate_along(P
, r
, nvar
, exist
, MaxRays
);
3299 fprintf(stderr
, "\nER: Equality\n");
3300 #endif /* DEBUG_ER */
3301 evalue
*EP
= barvinok_enumerate_e(T
, exist
-1, nparam
, MaxRays
);
3306 fprintf(stderr
, "\nER: Fixed\n");
3307 #endif /* DEBUG_ER */
3309 return barvinok_enumerate_e(P
, exist
-1, nparam
, MaxRays
);
3311 Polyhedron
*T
= Polyhedron_Copy(P
);
3312 SwapColumns(T
, nvar
+1, nvar
+1+first
);
3313 evalue
*EP
= barvinok_enumerate_e(T
, exist
-1, nparam
, MaxRays
);
3320 Vector
*row
= Vector_Alloc(len
);
3321 value_set_si(row
->p
[0], 1);
3326 enum constraint
* info
= new constraint
[exist
];
3327 for (int i
= 0; i
< exist
; ++i
) {
3329 for (int l
= P
->NbEq
; l
< P
->NbConstraints
; ++l
) {
3330 if (value_negz_p(P
->Constraint
[l
][nvar
+i
+1]))
3332 bool l_parallel
= is_single(P
->Constraint
[l
]+nvar
+1, i
, exist
);
3333 for (int u
= P
->NbEq
; u
< P
->NbConstraints
; ++u
) {
3334 if (value_posz_p(P
->Constraint
[u
][nvar
+i
+1]))
3336 bool lu_parallel
= l_parallel
||
3337 is_single(P
->Constraint
[u
]+nvar
+1, i
, exist
);
3338 value_oppose(f
, P
->Constraint
[u
][nvar
+i
+1]);
3339 Vector_Combine(P
->Constraint
[l
]+1, P
->Constraint
[u
]+1, row
->p
+1,
3340 f
, P
->Constraint
[l
][nvar
+i
+1], len
-1);
3341 if (!(info
[i
] & INDEPENDENT
)) {
3343 for (j
= 0; j
< exist
; ++j
)
3344 if (j
!= i
&& value_notzero_p(row
->p
[nvar
+j
+1]))
3347 //printf("independent: i: %d, l: %d, u: %d\n", i, l, u);
3348 info
[i
] = (constraint
)(info
[i
] | INDEPENDENT
);
3351 if (info
[i
] & ALL_POS
) {
3352 value_addto(row
->p
[len
-1], row
->p
[len
-1],
3353 P
->Constraint
[l
][nvar
+i
+1]);
3354 value_addto(row
->p
[len
-1], row
->p
[len
-1], f
);
3355 value_multiply(f
, f
, P
->Constraint
[l
][nvar
+i
+1]);
3356 value_substract(row
->p
[len
-1], row
->p
[len
-1], f
);
3357 value_decrement(row
->p
[len
-1], row
->p
[len
-1]);
3358 ConstraintSimplify(row
->p
, row
->p
, len
, &f
);
3359 value_set_si(f
, -1);
3360 Vector_Scale(row
->p
+1, row
->p
+1, f
, len
-1);
3361 value_decrement(row
->p
[len
-1], row
->p
[len
-1]);
3362 Polyhedron
*T
= AddConstraints(row
->p
, 1, P
, MaxRays
);
3364 //printf("not all_pos: i: %d, l: %d, u: %d\n", i, l, u);
3365 info
[i
] = (constraint
)(info
[i
] ^ ALL_POS
);
3367 //puts("pos remainder");
3368 //Polyhedron_Print(stdout, P_VALUE_FMT, T);
3371 if (!(info
[i
] & ONE_NEG
)) {
3373 negative_test_constraint(P
->Constraint
[l
],
3375 row
->p
, nvar
+i
, len
, &f
);
3376 oppose_constraint(row
->p
, len
, &f
);
3377 Polyhedron
*T
= AddConstraints(row
->p
, 1, P
, MaxRays
);
3379 //printf("one_neg i: %d, l: %d, u: %d\n", i, l, u);
3380 info
[i
] = (constraint
)(info
[i
] | ONE_NEG
);
3382 //puts("neg remainder");
3383 //Polyhedron_Print(stdout, P_VALUE_FMT, T);
3385 } else if (!(info
[i
] & ROT_NEG
)) {
3386 if (parallel_constraints(P
->Constraint
[l
],
3388 row
->p
, nvar
, exist
)) {
3389 negative_test_constraint7(P
->Constraint
[l
],
3391 row
->p
, nvar
, exist
,
3393 oppose_constraint(row
->p
, len
, &f
);
3394 Polyhedron
*T
= AddConstraints(row
->p
, 1, P
, MaxRays
);
3396 // printf("rot_neg i: %d, l: %d, u: %d\n", i, l, u);
3397 info
[i
] = (constraint
)(info
[i
] | ROT_NEG
);
3400 //puts("neg remainder");
3401 //Polyhedron_Print(stdout, P_VALUE_FMT, T);
3406 if (!(info
[i
] & ALL_POS
) && (info
[i
] & (ONE_NEG
| ROT_NEG
)))
3410 if (info
[i
] & ALL_POS
)
3417 for (int i = 0; i < exist; ++i)
3418 printf("%i: %i\n", i, info[i]);
3420 for (int i
= 0; i
< exist
; ++i
)
3421 if (info
[i
] & ALL_POS
) {
3423 fprintf(stderr
, "\nER: Positive\n");
3424 #endif /* DEBUG_ER */
3426 // Maybe we should chew off some of the fat here
3427 Matrix
*M
= Matrix_Alloc(P
->Dimension
, P
->Dimension
+1);
3428 for (int j
= 0; j
< P
->Dimension
; ++j
)
3429 value_set_si(M
->p
[j
][j
+ (j
>= i
+nvar
)], 1);
3430 Polyhedron
*T
= Polyhedron_Image(P
, M
, MaxRays
);
3432 evalue
*EP
= barvinok_enumerate_e(T
, exist
-1, nparam
, MaxRays
);
3439 for (int i
= 0; i
< exist
; ++i
)
3440 if (info
[i
] & ONE_NEG
) {
3442 fprintf(stderr
, "\nER: Negative\n");
3443 #endif /* DEBUG_ER */
3448 return barvinok_enumerate_e(P
, exist
-1, nparam
, MaxRays
);
3450 Polyhedron
*T
= Polyhedron_Copy(P
);
3451 SwapColumns(T
, nvar
+1, nvar
+1+i
);
3452 evalue
*EP
= barvinok_enumerate_e(T
, exist
-1, nparam
, MaxRays
);
3457 for (int i
= 0; i
< exist
; ++i
)
3458 if (info
[i
] & ROT_NEG
) {
3460 fprintf(stderr
, "\nER: Rotate\n");
3461 #endif /* DEBUG_ER */
3465 Polyhedron
*T
= rotate_along(P
, r
, nvar
, exist
, MaxRays
);
3466 evalue
*EP
= barvinok_enumerate_e(T
, exist
-1, nparam
, MaxRays
);
3470 for (int i
= 0; i
< exist
; ++i
)
3471 if (info
[i
] & INDEPENDENT
) {
3472 Polyhedron
*pos
, *neg
;
3474 /* Find constraint again and split off negative part */
3476 if (SplitOnVar(P
, i
, nvar
, len
, exist
, MaxRays
,
3477 row
, f
, true, &pos
, &neg
)) {
3479 fprintf(stderr
, "\nER: Split\n");
3480 #endif /* DEBUG_ER */
3483 barvinok_enumerate_e(neg
, exist
-1, nparam
, MaxRays
);
3485 barvinok_enumerate_e(pos
, exist
, nparam
, MaxRays
);
3487 free_evalue_refs(E
);
3489 Polyhedron_Free(neg
);
3490 Polyhedron_Free(pos
);
3504 EP
= enumerate_line(P
, exist
, nparam
, MaxRays
);
3508 EP
= barvinok_enumerate_pip(P
, exist
, nparam
, MaxRays
);
3512 EP
= enumerate_redundant_ray(P
, exist
, nparam
, MaxRays
);
3516 EP
= enumerate_sure(P
, exist
, nparam
, MaxRays
);
3520 EP
= enumerate_ray(P
, exist
, nparam
, MaxRays
);
3524 EP
= enumerate_sure2(P
, exist
, nparam
, MaxRays
);
3528 F
= unfringe(P
, MaxRays
);
3529 if (!PolyhedronIncludes(F
, P
)) {
3531 fprintf(stderr
, "\nER: Fringed\n");
3532 #endif /* DEBUG_ER */
3533 EP
= barvinok_enumerate_e(F
, exist
, nparam
, MaxRays
);
3540 EP
= enumerate_vd(&P
, exist
, nparam
, MaxRays
);
3545 EP
= enumerate_sum(P
, exist
, nparam
, MaxRays
);
3552 Polyhedron
*pos
, *neg
;
3553 for (i
= 0; i
< exist
; ++i
)
3554 if (SplitOnVar(P
, i
, nvar
, len
, exist
, MaxRays
,
3555 row
, f
, false, &pos
, &neg
))
3561 EP
= enumerate_or(pos
, exist
, nparam
, MaxRays
);
3573 gen_fun
* barvinok_series(Polyhedron
*P
, Polyhedron
* C
, unsigned MaxRays
)
3575 Polyhedron
** vcone
;
3577 unsigned nparam
= C
->Dimension
;
3581 sign
.SetLength(ncone
);
3583 CA
= align_context(C
, P
->Dimension
, MaxRays
);
3584 P
= DomainIntersection(P
, CA
, MaxRays
);
3585 Polyhedron_Free(CA
);
3587 assert(!Polyhedron_is_infinite(P
, nparam
));
3588 assert(P
->NbBid
== 0);
3589 assert(Polyhedron_has_positive_rays(P
, nparam
));
3590 assert(P
->NbEq
== 0);
3593 nvar
= dim
- nparam
;
3594 vcone
= new Polyhedron_p
[P
->NbRays
];
3596 for (int j
= 0; j
< P
->NbRays
; ++j
) {
3597 if (!value_pos_p(P
->Ray
[j
][dim
+1]))
3601 Polyhedron
*C
= supporting_cone(P
, j
);
3602 decompose(C
, &vcone
[j
], &npos
, &nneg
, MaxRays
);
3603 ncone
+= npos
+ nneg
;
3604 sign
.SetLength(ncone
);
3605 for (int k
= 0; k
< npos
; ++k
)
3606 sign
[ncone
-nneg
-k
-1] = 1;
3607 for (int k
= 0; k
< nneg
; ++k
)
3608 sign
[ncone
-k
-1] = -1;
3612 rays
.SetDims(ncone
* dim
, nvar
);
3614 for (int j
= 0; j
< P
->NbRays
; ++j
) {
3615 if (!value_pos_p(P
->Ray
[j
][dim
+1]))
3618 for (Polyhedron
*i
= vcone
[j
]; i
; i
= i
->next
) {
3619 add_rays(rays
, i
, &r
, nvar
);
3622 rays
.SetDims(r
, nvar
);
3624 nonorthog(rays
, lambda
);
3625 //randomvector(P, lambda, nvar);
3628 cout << "rays: " << rays;
3629 cout << "lambda: " << lambda;
3635 num_p
.SetLength(nparam
);
3638 den_s
.SetLength(dim
);
3640 den_p
.SetLength(dim
);
3642 den
.SetDims(dim
, nparam
);
3648 gen_fun
* gf
= new gen_fun
;
3650 rays
.SetDims(dim
, nvar
);
3652 for (int j
= 0; j
< P
->NbRays
; ++j
) {
3653 if (!value_pos_p(P
->Ray
[j
][dim
+1]))
3656 for (Polyhedron
*i
= vcone
[j
]; i
; i
= i
->next
, ++f
) {
3657 lattice_point(P
->Ray
[j
]+1, i
, vertex
);
3660 for ( ; k
< nvar
; ++k
)
3661 num_s
+= vertex
[k
] * lambda
[k
];
3662 for ( ; k
< dim
; ++k
)
3663 num_p
[k
-nvar
] = vertex
[k
];
3666 add_rays(rays
, i
, &r
, nvar
, true);
3667 for (r
= 0; r
< dim
; ++r
)
3668 values2zz(i
->Ray
[r
]+1+nvar
, den
[r
], nparam
);
3669 den_s
= rays
* lambda
;
3671 normalize(sign
[f
], num_s
, num_p
, den_s
, den_p
, den
);
3675 for (int k
= 0; k
< dim
; ++k
) {
3678 else if (den_s
[k
] == 0)
3681 if (no_param
== 0) {
3682 for (int k
= 0; k
< dim
; ++k
)
3685 gf
->add(sign
[f
], one
, num_p
, den
);
3686 } else if (no_param
+ only_param
== dim
) {
3689 pden
.SetDims(only_param
, nparam
);
3691 for (k
= 0, l
= 0; k
< dim
; ++k
)
3695 for (k
= 0; k
< dim
; ++k
)
3699 dpoly
n(no_param
, num_s
);
3700 dpoly
d(no_param
, den_s
[k
], 1);
3701 for ( ; ++k
< dim
; k
)
3702 if (den_s
[k
] != 0) {
3703 dpoly
fact(no_param
, den_s
[k
], 1);
3707 mpq_set_si(count
, 0, 1);
3708 n
.div(d
, count
, sign
[f
]);
3711 value2zz(mpq_numref(count
), qn
);
3712 value2zz(mpq_denref(count
), qd
);
3714 gf
->add(qn
, qd
, num_p
, pden
);
3719 pden
.SetDims(only_param
, nparam
);
3721 for (k
= 0, l
= 0; k
< dim
; ++k
)
3725 for (k
= 0; k
< dim
; ++k
)
3729 dpoly
n(no_param
, num_s
);
3730 dpoly
d(no_param
, den_s
[k
], 1);
3731 for ( ; ++k
< dim
; )
3732 if (den_p
[k
] == 0) {
3733 dpoly
fact(no_param
, den_s
[k
], 1);
3737 for (k
= 0; k
< dim
; ++k
) {
3738 if (den_s
[k
] == 0 || den_p
[k
] == 0)
3741 dpoly
pd(no_param
-1, den_s
[k
], 1);
3742 int s
= den_p
[k
] < 0 ? -1 : 1;
3745 r
= new dpoly_r(n
, pd
, k
, s
, dim
);
3747 assert(0); // for now
3750 r
->div(d
, sign
[f
], gf
, pden
, den
, num_p
);
3754 cout << "sign: " << sign[f];
3755 cout << "num_s: " << num_s;
3756 cout << "num_p: " << num_p;
3757 cout << "den_s: " << den_s;
3758 cout << "den_p: " << den_p;
3759 cout << "den: " << den;
3760 cout << "only_param: " << only_param;
3761 cout << "no_param: " << no_param;