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
+1);
442 ZZ cur_d
= rc
->denom
;
445 for (int i
= 0; i
< len
; ++i
) {
448 for (int k
= 0; k
< c
[i
].size(); ++k
) {
449 coeff
= c
[i
][k
]->coeff
* cur_d
;
450 rc
->add_term(i
, c
[i
][k
]->powers
, coeff
);
453 for (int j
= 1; j
<= i
; ++j
) {
454 for (int k
= 0; k
< rc
->c
[i
-j
].size(); ++k
) {
455 coeff
= - d
.coeff
[j
] * rc
->c
[i
-j
][k
]->coeff
/ d
.coeff
[0];
456 rc
->add_term(i
, rc
->c
[i
-j
][k
]->powers
, coeff
);
462 void div(dpoly
& d
, ZZ
& sign
, gen_fun
*gf
, mat_ZZ
& pden
, mat_ZZ
& den
,
464 dpoly_r
* rc
= div(d
);
466 int common
= pden
.NumRows();
468 vector
< dpoly_r_term
* >& final
= rc
->c
[len
-1];
470 for (int j
= 0; j
< final
.size(); ++j
) {
472 pden
.SetDims(rows
, pden
.NumCols());
473 for (int k
= 0; k
< dim
; ++k
) {
474 int n
= final
[j
]->powers
[k
];
477 int abs_n
= n
< 0 ? -n
: n
;
478 pden
.SetDims(rows
+abs_n
, pden
.NumCols());
479 for (int l
= 0; l
< abs_n
; ++l
) {
481 pden
[rows
+l
] = den
[k
];
483 pden
[rows
+l
] = -den
[k
];
487 gf
->add(final
[j
]->coeff
, rc
->denom
, num_p
, pden
);
492 for (int i
= 0; i
< len
; ++i
) {
495 cout
<< c
[i
].size() << endl
;
496 for (int j
= 0; j
< c
[i
].size(); ++j
) {
497 for (int k
= 0; k
< dim
; ++k
) {
498 cout
<< c
[i
][j
]->powers
[k
] << " ";
500 cout
<< ": " << c
[i
][j
]->coeff
<< endl
;
508 void decompose(Polyhedron
*C
);
509 virtual void handle(Polyhedron
*P
, int sign
) = 0;
512 struct polar_decomposer
: public decomposer
{
513 void decompose(Polyhedron
*C
, unsigned MaxRays
);
514 virtual void handle(Polyhedron
*P
, int sign
);
515 virtual void handle_polar(Polyhedron
*P
, int sign
) = 0;
518 void decomposer::decompose(Polyhedron
*C
)
520 vector
<cone
*> nonuni
;
521 cone
* c
= new cone(C
);
532 while (!nonuni
.empty()) {
535 Vector
* v
= c
->short_vector(lambda
);
536 for (int i
= 0; i
< c
->Rays
->NbRows
- 1; ++i
) {
539 Matrix
* M
= Matrix_Copy(c
->Rays
);
540 Vector_Copy(v
->p
, M
->p
[i
], v
->Size
);
541 cone
* pc
= new cone(M
);
542 assert (pc
->det
!= 0);
543 if (abs(pc
->det
) > 1) {
544 assert(abs(pc
->det
) < abs(c
->det
));
545 nonuni
.push_back(pc
);
547 handle(pc
->poly(), sign(pc
->det
) * s
);
557 void polar_decomposer::decompose(Polyhedron
*cone
, unsigned MaxRays
)
559 Polyhedron_Polarize(cone
);
560 if (cone
->NbRays
- 1 != cone
->Dimension
) {
561 Polyhedron
*tmp
= cone
;
562 cone
= triangularize_cone(cone
, MaxRays
);
563 Polyhedron_Free(tmp
);
565 for (Polyhedron
*Polar
= cone
; Polar
; Polar
= Polar
->next
)
566 decomposer::decompose(Polar
);
570 void polar_decomposer::handle(Polyhedron
*P
, int sign
)
572 Polyhedron_Polarize(P
);
573 handle_polar(P
, sign
);
577 * Barvinok's Decomposition of a simplicial cone
579 * Returns two lists of polyhedra
581 void barvinok_decompose(Polyhedron
*C
, Polyhedron
**ppos
, Polyhedron
**pneg
)
583 Polyhedron
*pos
= *ppos
, *neg
= *pneg
;
584 vector
<cone
*> nonuni
;
585 cone
* c
= new cone(C
);
592 Polyhedron
*p
= Polyhedron_Copy(c
->Cone
);
598 while (!nonuni
.empty()) {
601 Vector
* v
= c
->short_vector(lambda
);
602 for (int i
= 0; i
< c
->Rays
->NbRows
- 1; ++i
) {
605 Matrix
* M
= Matrix_Copy(c
->Rays
);
606 Vector_Copy(v
->p
, M
->p
[i
], v
->Size
);
607 cone
* pc
= new cone(M
);
608 assert (pc
->det
!= 0);
609 if (abs(pc
->det
) > 1) {
610 assert(abs(pc
->det
) < abs(c
->det
));
611 nonuni
.push_back(pc
);
613 Polyhedron
*p
= pc
->poly();
615 if (sign(pc
->det
) == s
) {
634 * Returns a single list of npos "positive" cones followed by nneg
636 * The input cone is freed
638 void decompose(Polyhedron
*cone
, Polyhedron
**parts
, int *npos
, int *nneg
, unsigned MaxRays
)
640 Polyhedron_Polarize(cone
);
641 if (cone
->NbRays
- 1 != cone
->Dimension
) {
642 Polyhedron
*tmp
= cone
;
643 cone
= triangularize_cone(cone
, MaxRays
);
644 Polyhedron_Free(tmp
);
646 Polyhedron
*polpos
= NULL
, *polneg
= NULL
;
647 *npos
= 0; *nneg
= 0;
648 for (Polyhedron
*Polar
= cone
; Polar
; Polar
= Polar
->next
)
649 barvinok_decompose(Polar
, &polpos
, &polneg
);
652 for (Polyhedron
*i
= polpos
; i
; i
= i
->next
) {
653 Polyhedron_Polarize(i
);
657 for (Polyhedron
*i
= polneg
; i
; i
= i
->next
) {
658 Polyhedron_Polarize(i
);
669 const int MAX_TRY
=10;
671 * Searches for a vector that is not orthogonal to any
672 * of the rays in rays.
674 static void nonorthog(mat_ZZ
& rays
, vec_ZZ
& lambda
)
676 int dim
= rays
.NumCols();
678 lambda
.SetLength(dim
);
682 for (int i
= 2; !found
&& i
<= 50*dim
; i
+=4) {
683 for (int j
= 0; j
< MAX_TRY
; ++j
) {
684 for (int k
= 0; k
< dim
; ++k
) {
685 int r
= random_int(i
)+2;
686 int v
= (2*(r
%2)-1) * (r
>> 1);
690 for (; k
< rays
.NumRows(); ++k
)
691 if (lambda
* rays
[k
] == 0)
693 if (k
== rays
.NumRows()) {
702 static void randomvector(Polyhedron
*P
, vec_ZZ
& lambda
, int nvar
)
706 unsigned int dim
= P
->Dimension
;
709 for (int i
= 0; i
< P
->NbRays
; ++i
) {
710 for (int j
= 1; j
<= dim
; ++j
) {
711 value_absolute(tmp
, P
->Ray
[i
][j
]);
712 int t
= VALUE_TO_LONG(tmp
) * 16;
717 for (int i
= 0; i
< P
->NbConstraints
; ++i
) {
718 for (int j
= 1; j
<= dim
; ++j
) {
719 value_absolute(tmp
, P
->Constraint
[i
][j
]);
720 int t
= VALUE_TO_LONG(tmp
) * 16;
727 lambda
.SetLength(nvar
);
728 for (int k
= 0; k
< nvar
; ++k
) {
729 int r
= random_int(max
*dim
)+2;
730 int v
= (2*(r
%2)-1) * (max
/2*dim
+ (r
>> 1));
735 static void add_rays(mat_ZZ
& rays
, Polyhedron
*i
, int *r
, int nvar
= -1,
738 unsigned dim
= i
->Dimension
;
741 for (int k
= 0; k
< i
->NbRays
; ++k
) {
742 if (!value_zero_p(i
->Ray
[k
][dim
+1]))
744 if (!all
&& nvar
!= dim
&& First_Non_Zero(i
->Ray
[k
]+1, nvar
) == -1)
746 values2zz(i
->Ray
[k
]+1, rays
[(*r
)++], nvar
);
750 void lattice_point(Value
* values
, Polyhedron
*i
, vec_ZZ
& vertex
)
752 unsigned dim
= i
->Dimension
;
753 if(!value_one_p(values
[dim
])) {
754 Matrix
* Rays
= rays(i
);
755 Matrix
*inv
= Matrix_Alloc(Rays
->NbRows
, Rays
->NbColumns
);
756 int ok
= Matrix_Inverse(Rays
, inv
);
760 Vector
*lambda
= Vector_Alloc(dim
+1);
761 Vector_Matrix_Product(values
, inv
, lambda
->p
);
763 for (int j
= 0; j
< dim
; ++j
)
764 mpz_cdiv_q(lambda
->p
[j
], lambda
->p
[j
], lambda
->p
[dim
]);
765 value_set_si(lambda
->p
[dim
], 1);
766 Vector
*A
= Vector_Alloc(dim
+1);
767 Vector_Matrix_Product(lambda
->p
, Rays
, A
->p
);
770 values2zz(A
->p
, vertex
, dim
);
773 values2zz(values
, vertex
, dim
);
776 static evalue
*term(int param
, ZZ
& c
, Value
*den
= NULL
)
778 evalue
*EP
= new evalue();
780 value_set_si(EP
->d
,0);
781 EP
->x
.p
= new_enode(polynomial
, 2, param
+ 1);
782 evalue_set_si(&EP
->x
.p
->arr
[0], 0, 1);
783 value_init(EP
->x
.p
->arr
[1].x
.n
);
785 value_set_si(EP
->x
.p
->arr
[1].d
, 1);
787 value_assign(EP
->x
.p
->arr
[1].d
, *den
);
788 zz2value(c
, EP
->x
.p
->arr
[1].x
.n
);
792 static void vertex_period(
793 Polyhedron
*i
, vec_ZZ
& lambda
, Matrix
*T
,
794 Value lcm
, int p
, Vector
*val
,
795 evalue
*E
, evalue
* ev
,
798 unsigned nparam
= T
->NbRows
- 1;
799 unsigned dim
= i
->Dimension
;
806 Vector
* values
= Vector_Alloc(dim
+ 1);
807 Vector_Matrix_Product(val
->p
, T
, values
->p
);
808 value_assign(values
->p
[dim
], lcm
);
809 lattice_point(values
->p
, i
, vertex
);
810 num
= vertex
* lambda
;
815 zz2value(num
, ev
->x
.n
);
816 value_assign(ev
->d
, lcm
);
823 values2zz(T
->p
[p
], vertex
, dim
);
824 nump
= vertex
* lambda
;
825 if (First_Non_Zero(val
->p
, p
) == -1) {
826 value_assign(tmp
, lcm
);
827 evalue
*ET
= term(p
, nump
, &tmp
);
829 free_evalue_refs(ET
);
833 value_assign(tmp
, lcm
);
834 if (First_Non_Zero(T
->p
[p
], dim
) != -1)
835 Vector_Gcd(T
->p
[p
], dim
, &tmp
);
837 if (value_lt(tmp
, lcm
)) {
840 value_division(tmp
, lcm
, tmp
);
841 value_set_si(ev
->d
, 0);
842 ev
->x
.p
= new_enode(periodic
, VALUE_TO_INT(tmp
), p
+1);
843 value2zz(tmp
, count
);
845 value_decrement(tmp
, tmp
);
847 ZZ new_offset
= offset
- count
* nump
;
848 value_assign(val
->p
[p
], tmp
);
849 vertex_period(i
, lambda
, T
, lcm
, p
+1, val
, E
,
850 &ev
->x
.p
->arr
[VALUE_TO_INT(tmp
)], new_offset
);
851 } while (value_pos_p(tmp
));
853 vertex_period(i
, lambda
, T
, lcm
, p
+1, val
, E
, ev
, offset
);
857 static void mask_r(Matrix
*f
, int nr
, Vector
*lcm
, int p
, Vector
*val
, evalue
*ev
)
859 unsigned nparam
= lcm
->Size
;
862 Vector
* prod
= Vector_Alloc(f
->NbRows
);
863 Matrix_Vector_Product(f
, val
->p
, prod
->p
);
865 for (int i
= 0; i
< nr
; ++i
) {
866 value_modulus(prod
->p
[i
], prod
->p
[i
], f
->p
[i
][nparam
+1]);
867 isint
&= value_zero_p(prod
->p
[i
]);
869 value_set_si(ev
->d
, 1);
871 value_set_si(ev
->x
.n
, isint
);
878 if (value_one_p(lcm
->p
[p
]))
879 mask_r(f
, nr
, lcm
, p
+1, val
, ev
);
881 value_assign(tmp
, lcm
->p
[p
]);
882 value_set_si(ev
->d
, 0);
883 ev
->x
.p
= new_enode(periodic
, VALUE_TO_INT(tmp
), p
+1);
885 value_decrement(tmp
, tmp
);
886 value_assign(val
->p
[p
], tmp
);
887 mask_r(f
, nr
, lcm
, p
+1, val
, &ev
->x
.p
->arr
[VALUE_TO_INT(tmp
)]);
888 } while (value_pos_p(tmp
));
893 static evalue
*multi_monom(vec_ZZ
& p
)
895 evalue
*X
= new evalue();
898 unsigned nparam
= p
.length()-1;
899 zz2value(p
[nparam
], X
->x
.n
);
900 value_set_si(X
->d
, 1);
901 for (int i
= 0; i
< nparam
; ++i
) {
904 evalue
*T
= term(i
, p
[i
]);
913 * Check whether mapping polyhedron P on the affine combination
914 * num yields a range that has a fixed quotient on integer
916 * If zero is true, then we are only interested in the quotient
917 * for the cases where the remainder is zero.
918 * Returns NULL if false and a newly allocated value if true.
920 static Value
*fixed_quotient(Polyhedron
*P
, vec_ZZ
& num
, Value d
, bool zero
)
923 int len
= num
.length();
924 Matrix
*T
= Matrix_Alloc(2, len
);
925 zz2values(num
, T
->p
[0]);
926 value_set_si(T
->p
[1][len
-1], 1);
927 Polyhedron
*I
= Polyhedron_Image(P
, T
, P
->NbConstraints
);
931 for (i
= 0; i
< I
->NbRays
; ++i
)
932 if (value_zero_p(I
->Ray
[i
][2])) {
940 int bounded
= line_minmax(I
, &min
, &max
);
944 mpz_cdiv_q(min
, min
, d
);
946 mpz_fdiv_q(min
, min
, d
);
947 mpz_fdiv_q(max
, max
, d
);
949 if (value_eq(min
, max
)) {
952 value_assign(*ret
, min
);
960 * Normalize linear expression coef modulo m
961 * Removes common factor and reduces coefficients
962 * Returns index of first non-zero coefficient or len
964 static int normal_mod(Value
*coef
, int len
, Value
*m
)
969 Vector_Gcd(coef
, len
, &gcd
);
971 Vector_AntiScale(coef
, coef
, gcd
, len
);
973 value_division(*m
, *m
, gcd
);
980 for (j
= 0; j
< len
; ++j
)
981 mpz_fdiv_r(coef
[j
], coef
[j
], *m
);
982 for (j
= 0; j
< len
; ++j
)
983 if (value_notzero_p(coef
[j
]))
990 static void mask(Matrix
*f
, evalue
*factor
)
992 int nr
= f
->NbRows
, nc
= f
->NbColumns
;
995 for (n
= 0; n
< nr
&& value_notzero_p(f
->p
[n
][nc
-1]); ++n
)
996 if (value_notone_p(f
->p
[n
][nc
-1]) &&
997 value_notmone_p(f
->p
[n
][nc
-1]))
1011 value_set_si(EV
.x
.n
, 1);
1013 for (n
= 0; n
< nr
; ++n
) {
1014 value_assign(m
, f
->p
[n
][nc
-1]);
1015 if (value_one_p(m
) || value_mone_p(m
))
1018 int j
= normal_mod(f
->p
[n
], nc
-1, &m
);
1020 free_evalue_refs(factor
);
1021 value_init(factor
->d
);
1022 evalue_set_si(factor
, 0, 1);
1026 values2zz(f
->p
[n
], row
, nc
-1);
1029 if (j
< (nc
-1)-1 && row
[j
] > g
/2) {
1030 for (int k
= j
; k
< (nc
-1); ++k
)
1032 row
[k
] = g
- row
[k
];
1036 value_set_si(EP
.d
, 0);
1037 EP
.x
.p
= new_enode(relation
, 2, 0);
1038 value_clear(EP
.x
.p
->arr
[1].d
);
1039 EP
.x
.p
->arr
[1] = *factor
;
1040 evalue
*ev
= &EP
.x
.p
->arr
[0];
1041 value_set_si(ev
->d
, 0);
1042 ev
->x
.p
= new_enode(fractional
, 3, -1);
1043 evalue_set_si(&ev
->x
.p
->arr
[1], 0, 1);
1044 evalue_set_si(&ev
->x
.p
->arr
[2], 1, 1);
1045 evalue
*E
= multi_monom(row
);
1046 value_assign(EV
.d
, m
);
1048 value_clear(ev
->x
.p
->arr
[0].d
);
1049 ev
->x
.p
->arr
[0] = *E
;
1055 free_evalue_refs(&EV
);
1061 static void mask(Matrix
*f
, evalue
*factor
)
1063 int nr
= f
->NbRows
, nc
= f
->NbColumns
;
1066 for (n
= 0; n
< nr
&& value_notzero_p(f
->p
[n
][nc
-1]); ++n
)
1067 if (value_notone_p(f
->p
[n
][nc
-1]) &&
1068 value_notmone_p(f
->p
[n
][nc
-1]))
1076 unsigned np
= nc
- 2;
1077 Vector
*lcm
= Vector_Alloc(np
);
1078 Vector
*val
= Vector_Alloc(nc
);
1079 Vector_Set(val
->p
, 0, nc
);
1080 value_set_si(val
->p
[np
], 1);
1081 Vector_Set(lcm
->p
, 1, np
);
1082 for (n
= 0; n
< nr
; ++n
) {
1083 if (value_one_p(f
->p
[n
][nc
-1]) ||
1084 value_mone_p(f
->p
[n
][nc
-1]))
1086 for (int j
= 0; j
< np
; ++j
)
1087 if (value_notzero_p(f
->p
[n
][j
])) {
1088 Gcd(f
->p
[n
][j
], f
->p
[n
][nc
-1], &tmp
);
1089 value_division(tmp
, f
->p
[n
][nc
-1], tmp
);
1090 value_lcm(tmp
, lcm
->p
[j
], &lcm
->p
[j
]);
1095 mask_r(f
, nr
, lcm
, 0, val
, &EP
);
1100 free_evalue_refs(&EP
);
1111 static bool mod_needed(Polyhedron
*PD
, vec_ZZ
& num
, Value d
, evalue
*E
)
1113 Value
*q
= fixed_quotient(PD
, num
, d
, false);
1118 value_oppose(*q
, *q
);
1121 value_set_si(EV
.d
, 1);
1123 value_multiply(EV
.x
.n
, *q
, d
);
1125 free_evalue_refs(&EV
);
1131 static void ceil_mod(Value
*coef
, int len
, Value d
, ZZ
& f
, evalue
*EP
, Polyhedron
*PD
)
1135 value_set_si(m
, -1);
1137 Vector_Scale(coef
, coef
, m
, len
);
1140 int j
= normal_mod(coef
, len
, &m
);
1148 values2zz(coef
, num
, len
);
1155 evalue_set_si(&tmp
, 0, 1);
1159 while (j
< len
-1 && (num
[j
] == g
/2 || num
[j
] == 0))
1161 if ((j
< len
-1 && num
[j
] > g
/2) || (j
== len
-1 && num
[j
] >= (g
+1)/2)) {
1162 for (int k
= j
; k
< len
-1; ++k
)
1164 num
[k
] = g
- num
[k
];
1165 num
[len
-1] = g
- 1 - num
[len
-1];
1166 value_assign(tmp
.d
, m
);
1168 zz2value(t
, tmp
.x
.n
);
1174 ZZ t
= num
[len
-1] * f
;
1175 zz2value(t
, tmp
.x
.n
);
1176 value_assign(tmp
.d
, m
);
1179 evalue
*E
= multi_monom(num
);
1183 if (PD
&& !mod_needed(PD
, num
, m
, E
)) {
1185 zz2value(f
, EV
.x
.n
);
1186 value_assign(EV
.d
, m
);
1191 value_set_si(EV
.x
.n
, 1);
1192 value_assign(EV
.d
, m
);
1194 value_clear(EV
.x
.n
);
1195 value_set_si(EV
.d
, 0);
1196 EV
.x
.p
= new_enode(fractional
, 3, -1);
1197 evalue_copy(&EV
.x
.p
->arr
[0], E
);
1198 evalue_set_si(&EV
.x
.p
->arr
[1], 0, 1);
1199 value_init(EV
.x
.p
->arr
[2].x
.n
);
1200 zz2value(f
, EV
.x
.p
->arr
[2].x
.n
);
1201 value_set_si(EV
.x
.p
->arr
[2].d
, 1);
1206 free_evalue_refs(&EV
);
1207 free_evalue_refs(E
);
1211 free_evalue_refs(&tmp
);
1217 evalue
* bv_ceil3(Value
*coef
, int len
, Value d
, Polyhedron
*P
)
1219 Vector
*val
= Vector_Alloc(len
);
1223 value_set_si(t
, -1);
1224 Vector_Scale(coef
, val
->p
, t
, len
);
1225 value_absolute(t
, d
);
1228 values2zz(val
->p
, num
, len
);
1229 evalue
*EP
= multi_monom(num
);
1233 value_init(tmp
.x
.n
);
1234 value_set_si(tmp
.x
.n
, 1);
1235 value_assign(tmp
.d
, t
);
1241 ceil_mod(val
->p
, len
, t
, one
, EP
, P
);
1244 /* copy EP to malloc'ed evalue */
1250 free_evalue_refs(&tmp
);
1257 evalue
* lattice_point(
1258 Polyhedron
*i
, vec_ZZ
& lambda
, Matrix
*W
, Value lcm
, Polyhedron
*PD
)
1260 unsigned nparam
= W
->NbColumns
- 1;
1262 Matrix
* Rays
= rays2(i
);
1263 Matrix
*T
= Transpose(Rays
);
1264 Matrix
*T2
= Matrix_Copy(T
);
1265 Matrix
*inv
= Matrix_Alloc(T2
->NbRows
, T2
->NbColumns
);
1266 int ok
= Matrix_Inverse(T2
, inv
);
1271 matrix2zz(W
, vertex
, W
->NbRows
, W
->NbColumns
);
1274 num
= lambda
* vertex
;
1276 evalue
*EP
= multi_monom(num
);
1280 value_init(tmp
.x
.n
);
1281 value_set_si(tmp
.x
.n
, 1);
1282 value_assign(tmp
.d
, lcm
);
1286 Matrix
*L
= Matrix_Alloc(inv
->NbRows
, W
->NbColumns
);
1287 Matrix_Product(inv
, W
, L
);
1290 matrix2zz(T
, RT
, T
->NbRows
, T
->NbColumns
);
1293 vec_ZZ p
= lambda
* RT
;
1295 for (int i
= 0; i
< L
->NbRows
; ++i
) {
1296 ceil_mod(L
->p
[i
], nparam
+1, lcm
, p
[i
], EP
, PD
);
1302 free_evalue_refs(&tmp
);
1306 evalue
* lattice_point(
1307 Polyhedron
*i
, vec_ZZ
& lambda
, Matrix
*W
, Value lcm
, Polyhedron
*PD
)
1309 Matrix
*T
= Transpose(W
);
1310 unsigned nparam
= T
->NbRows
- 1;
1312 evalue
*EP
= new evalue();
1314 evalue_set_si(EP
, 0, 1);
1317 Vector
*val
= Vector_Alloc(nparam
+1);
1318 value_set_si(val
->p
[nparam
], 1);
1319 ZZ
offset(INIT_VAL
, 0);
1321 vertex_period(i
, lambda
, T
, lcm
, 0, val
, EP
, &ev
, offset
);
1324 free_evalue_refs(&ev
);
1335 Param_Vertices
* V
, Polyhedron
*i
, vec_ZZ
& lambda
, term_info
* term
,
1338 unsigned nparam
= V
->Vertex
->NbColumns
- 2;
1339 unsigned dim
= i
->Dimension
;
1341 vertex
.SetDims(V
->Vertex
->NbRows
, nparam
+1);
1345 value_set_si(lcm
, 1);
1346 for (int j
= 0; j
< V
->Vertex
->NbRows
; ++j
) {
1347 value_lcm(lcm
, V
->Vertex
->p
[j
][nparam
+1], &lcm
);
1349 if (value_notone_p(lcm
)) {
1350 Matrix
* mv
= Matrix_Alloc(dim
, nparam
+1);
1351 for (int j
= 0 ; j
< dim
; ++j
) {
1352 value_division(tmp
, lcm
, V
->Vertex
->p
[j
][nparam
+1]);
1353 Vector_Scale(V
->Vertex
->p
[j
], mv
->p
[j
], tmp
, nparam
+1);
1356 term
->E
= lattice_point(i
, lambda
, mv
, lcm
, PD
);
1364 for (int i
= 0; i
< V
->Vertex
->NbRows
; ++i
) {
1365 assert(value_one_p(V
->Vertex
->p
[i
][nparam
+1])); // for now
1366 values2zz(V
->Vertex
->p
[i
], vertex
[i
], nparam
+1);
1370 num
= lambda
* vertex
;
1374 for (int j
= 0; j
< nparam
; ++j
)
1380 term
->E
= multi_monom(num
);
1384 term
->constant
= num
[nparam
];
1387 term
->coeff
= num
[p
];
1394 static void normalize(ZZ
& sign
, ZZ
& num
, vec_ZZ
& den
)
1396 unsigned dim
= den
.length();
1400 for (int j
= 0; j
< den
.length(); ++j
) {
1404 den
[j
] = abs(den
[j
]);
1413 * f: the powers in the denominator for the remaining vars
1414 * each row refers to a factor
1415 * den_s: for each factor, the power of (s+1)
1417 * num_s: powers in the numerator corresponding to the summed vars
1418 * num_p: powers in the numerator corresponidng to the remaining vars
1419 * number of rays in cone: "dim" = "k"
1420 * length of each ray: "dim" = "d"
1421 * for now, it is assume: k == d
1423 * den_p: for each factor
1424 * 0: independent of remaining vars
1425 * 1: power corresponds to corresponding row in f
1426 * -1: power is inverse of corresponding row in f
1428 static void normalize(ZZ
& sign
,
1429 ZZ
& num_s
, vec_ZZ
& num_p
, vec_ZZ
& den_s
, vec_ZZ
& den_p
,
1432 unsigned dim
= f
.NumRows();
1433 unsigned nparam
= num_p
.length();
1434 unsigned nvar
= dim
- nparam
;
1438 for (int j
= 0; j
< den_s
.length(); ++j
) {
1439 if (den_s
[j
] == 0) {
1444 for (k
= 0; k
< nparam
; ++k
)
1458 den_s
[j
] = abs(den_s
[j
]);
1467 struct counter
: public polar_decomposer
{
1479 counter(Polyhedron
*P
) {
1482 randomvector(P
, lambda
, dim
);
1483 rays
.SetDims(dim
, dim
);
1488 void start(unsigned MaxRays
);
1494 virtual void handle_polar(Polyhedron
*P
, int sign
);
1497 void counter::handle_polar(Polyhedron
*C
, int s
)
1500 assert(C
->NbRays
-1 == dim
);
1501 add_rays(rays
, C
, &r
);
1502 for (int k
= 0; k
< dim
; ++k
) {
1503 assert(lambda
* rays
[k
] != 0);
1508 lattice_point(P
->Ray
[j
]+1, C
, vertex
);
1509 num
= vertex
* lambda
;
1510 den
= rays
* lambda
;
1511 normalize(sign
, num
, den
);
1514 dpoly
n(dim
, den
[0], 1);
1515 for (int k
= 1; k
< dim
; ++k
) {
1516 dpoly
fact(dim
, den
[k
], 1);
1519 d
.div(n
, count
, sign
);
1522 void counter::start(unsigned MaxRays
)
1524 for (j
= 0; j
< P
->NbRays
; ++j
) {
1525 Polyhedron
*C
= supporting_cone(P
, j
);
1526 decompose(C
, MaxRays
);
1530 typedef Polyhedron
* Polyhedron_p
;
1532 void barvinok_count(Polyhedron
*P
, Value
* result
, unsigned NbMaxCons
)
1534 Polyhedron
** vcone
;
1543 value_set_si(*result
, 0);
1547 for (; r
< P
->NbRays
; ++r
)
1548 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1]))
1550 if (P
->NbBid
!=0 || r
< P
->NbRays
) {
1551 value_set_si(*result
, -1);
1555 P
= remove_equalities(P
);
1558 value_set_si(*result
, 0);
1564 value_set_si(factor
, 1);
1565 Q
= Polyhedron_Reduce(P
, &factor
);
1572 if (P
->Dimension
== 0) {
1573 value_assign(*result
, factor
);
1576 value_clear(factor
);
1581 cnt
.start(NbMaxCons
);
1583 assert(value_one_p(&cnt
.count
[0]._mp_den
));
1584 value_multiply(*result
, &cnt
.count
[0]._mp_num
, factor
);
1588 value_clear(factor
);
1591 static void uni_polynom(int param
, Vector
*c
, evalue
*EP
)
1593 unsigned dim
= c
->Size
-2;
1595 value_set_si(EP
->d
,0);
1596 EP
->x
.p
= new_enode(polynomial
, dim
+1, param
+1);
1597 for (int j
= 0; j
<= dim
; ++j
)
1598 evalue_set(&EP
->x
.p
->arr
[j
], c
->p
[j
], c
->p
[dim
+1]);
1601 static void multi_polynom(Vector
*c
, evalue
* X
, evalue
*EP
)
1603 unsigned dim
= c
->Size
-2;
1607 evalue_set(&EC
, c
->p
[dim
], c
->p
[dim
+1]);
1610 evalue_set(EP
, c
->p
[dim
], c
->p
[dim
+1]);
1612 for (int i
= dim
-1; i
>= 0; --i
) {
1614 value_assign(EC
.x
.n
, c
->p
[i
]);
1617 free_evalue_refs(&EC
);
1620 Polyhedron
*unfringe (Polyhedron
*P
, unsigned MaxRays
)
1622 int len
= P
->Dimension
+2;
1623 Polyhedron
*T
, *R
= P
;
1626 Vector
*row
= Vector_Alloc(len
);
1627 value_set_si(row
->p
[0], 1);
1629 R
= DomainConstraintSimplify(Polyhedron_Copy(P
), MaxRays
);
1631 Matrix
*M
= Matrix_Alloc(2, len
-1);
1632 value_set_si(M
->p
[1][len
-2], 1);
1633 for (int v
= 0; v
< P
->Dimension
; ++v
) {
1634 value_set_si(M
->p
[0][v
], 1);
1635 Polyhedron
*I
= Polyhedron_Image(P
, M
, 2+1);
1636 value_set_si(M
->p
[0][v
], 0);
1637 for (int r
= 0; r
< I
->NbConstraints
; ++r
) {
1638 if (value_zero_p(I
->Constraint
[r
][0]))
1640 if (value_zero_p(I
->Constraint
[r
][1]))
1642 if (value_one_p(I
->Constraint
[r
][1]))
1644 if (value_mone_p(I
->Constraint
[r
][1]))
1646 value_absolute(g
, I
->Constraint
[r
][1]);
1647 Vector_Set(row
->p
+1, 0, len
-2);
1648 value_division(row
->p
[1+v
], I
->Constraint
[r
][1], g
);
1649 mpz_fdiv_q(row
->p
[len
-1], I
->Constraint
[r
][2], g
);
1651 R
= AddConstraints(row
->p
, 1, R
, MaxRays
);
1663 static Polyhedron
*reduce_domain(Polyhedron
*D
, Matrix
*CT
, Polyhedron
*CEq
,
1664 Polyhedron
**fVD
, int nd
, unsigned MaxRays
)
1669 Dt
= CT
? DomainPreimage(D
, CT
, MaxRays
) : D
;
1670 Polyhedron
*rVD
= DomainIntersection(Dt
, CEq
, MaxRays
);
1672 /* if rVD is empty or too small in geometric dimension */
1673 if(!rVD
|| emptyQ(rVD
) ||
1674 (rVD
->Dimension
-rVD
->NbEq
< Dt
->Dimension
-Dt
->NbEq
-CEq
->NbEq
)) {
1679 return 0; /* empty validity domain */
1685 fVD
[nd
] = Domain_Copy(rVD
);
1686 for (int i
= 0 ; i
< nd
; ++i
) {
1687 Polyhedron
*I
= DomainIntersection(fVD
[nd
], fVD
[i
], MaxRays
);
1692 Polyhedron
*F
= DomainSimplify(I
, fVD
[nd
], MaxRays
);
1694 Polyhedron
*T
= rVD
;
1695 rVD
= DomainDifference(rVD
, F
, MaxRays
);
1702 rVD
= DomainConstraintSimplify(rVD
, MaxRays
);
1704 Domain_Free(fVD
[nd
]);
1711 barvinok_count(rVD
, &c
, MaxRays
);
1712 if (value_zero_p(c
)) {
1721 static bool Polyhedron_is_infinite(Polyhedron
*P
, unsigned nparam
)
1724 for (r
= 0; r
< P
->NbRays
; ++r
)
1725 if (value_zero_p(P
->Ray
[r
][0]) ||
1726 value_zero_p(P
->Ray
[r
][P
->Dimension
+1])) {
1728 for (i
= P
->Dimension
- nparam
; i
< P
->Dimension
; ++i
)
1729 if (value_notzero_p(P
->Ray
[r
][i
+1]))
1731 if (i
>= P
->Dimension
)
1734 return r
< P
->NbRays
;
1737 /* Check whether all rays point in the positive directions
1738 * for the parameters
1740 static bool Polyhedron_has_positive_rays(Polyhedron
*P
, unsigned nparam
)
1743 for (r
= 0; r
< P
->NbRays
; ++r
)
1744 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1])) {
1746 for (i
= P
->Dimension
- nparam
; i
< P
->Dimension
; ++i
)
1747 if (value_neg_p(P
->Ray
[r
][i
+1]))
1753 typedef evalue
* evalue_p
;
1755 struct enumerator
: public polar_decomposer
{
1769 enumerator(Polyhedron
*P
, unsigned dim
, unsigned nbV
) {
1773 randomvector(P
, lambda
, dim
);
1774 rays
.SetDims(dim
, dim
);
1776 c
= Vector_Alloc(dim
+2);
1778 vE
= new evalue_p
[nbV
];
1779 for (int j
= 0; j
< nbV
; ++j
)
1785 void decompose_at(Param_Vertices
*V
, int _i
, unsigned MaxRays
) {
1786 Polyhedron
*C
= supporting_cone_p(P
, V
);
1790 vE
[_i
] = new evalue
;
1791 value_init(vE
[_i
]->d
);
1792 evalue_set_si(vE
[_i
], 0, 1);
1794 decompose(C
, MaxRays
);
1801 for (int j
= 0; j
< nbV
; ++j
)
1803 free_evalue_refs(vE
[j
]);
1809 virtual void handle_polar(Polyhedron
*P
, int sign
);
1812 void enumerator::handle_polar(Polyhedron
*C
, int s
)
1815 assert(C
->NbRays
-1 == dim
);
1816 add_rays(rays
, C
, &r
);
1817 for (int k
= 0; k
< dim
; ++k
) {
1818 assert(lambda
* rays
[k
] != 0);
1823 lattice_point(V
, C
, lambda
, &num
, 0);
1824 den
= rays
* lambda
;
1825 normalize(sign
, num
.constant
, den
);
1827 dpoly
n(dim
, den
[0], 1);
1828 for (int k
= 1; k
< dim
; ++k
) {
1829 dpoly
fact(dim
, den
[k
], 1);
1832 if (num
.E
!= NULL
) {
1833 ZZ
one(INIT_VAL
, 1);
1834 dpoly_n
d(dim
, num
.constant
, one
);
1837 multi_polynom(c
, num
.E
, &EV
);
1839 free_evalue_refs(&EV
);
1840 free_evalue_refs(num
.E
);
1842 } else if (num
.pos
!= -1) {
1843 dpoly_n
d(dim
, num
.constant
, num
.coeff
);
1846 uni_polynom(num
.pos
, c
, &EV
);
1848 free_evalue_refs(&EV
);
1850 mpq_set_si(count
, 0, 1);
1851 dpoly
d(dim
, num
.constant
);
1852 d
.div(n
, count
, sign
);
1855 evalue_set(&EV
, &count
[0]._mp_num
, &count
[0]._mp_den
);
1857 free_evalue_refs(&EV
);
1861 evalue
* barvinok_enumerate_ev(Polyhedron
*P
, Polyhedron
* C
, unsigned MaxRays
)
1863 //P = unfringe(P, MaxRays);
1864 Polyhedron
*CEq
= NULL
, *rVD
, *pVD
, *CA
;
1866 Param_Polyhedron
*PP
= NULL
;
1867 Param_Domain
*D
, *next
;
1870 unsigned nparam
= C
->Dimension
;
1872 ALLOC(evalue
, eres
);
1873 value_init(eres
->d
);
1874 value_set_si(eres
->d
, 0);
1877 value_init(factor
.d
);
1878 evalue_set_si(&factor
, 1, 1);
1880 CA
= align_context(C
, P
->Dimension
, MaxRays
);
1881 P
= DomainIntersection(P
, CA
, MaxRays
);
1882 Polyhedron_Free(CA
);
1884 if (C
->Dimension
== 0 || emptyQ(P
)) {
1886 eres
->x
.p
= new_enode(partition
, 2, C
->Dimension
);
1887 EVALUE_SET_DOMAIN(eres
->x
.p
->arr
[0],
1888 DomainConstraintSimplify(CEq
? CEq
: Polyhedron_Copy(C
), MaxRays
));
1889 value_set_si(eres
->x
.p
->arr
[1].d
, 1);
1890 value_init(eres
->x
.p
->arr
[1].x
.n
);
1892 value_set_si(eres
->x
.p
->arr
[1].x
.n
, 0);
1894 barvinok_count(P
, &eres
->x
.p
->arr
[1].x
.n
, MaxRays
);
1896 emul(&factor
, eres
);
1897 reduce_evalue(eres
);
1898 free_evalue_refs(&factor
);
1903 Param_Polyhedron_Free(PP
);
1907 if (Polyhedron_is_infinite(P
, nparam
))
1912 P
= remove_equalities_p(P
, P
->Dimension
-nparam
, &f
);
1916 if (P
->Dimension
== nparam
) {
1918 P
= Universe_Polyhedron(0);
1922 Polyhedron
*Q
= ParamPolyhedron_Reduce(P
, P
->Dimension
-nparam
, &factor
);
1925 if (Q
->Dimension
== nparam
) {
1927 P
= Universe_Polyhedron(0);
1932 Polyhedron
*oldP
= P
;
1933 PP
= Polyhedron2Param_SimplifiedDomain(&P
,C
,MaxRays
,&CEq
,&CT
);
1935 Polyhedron_Free(oldP
);
1937 if (isIdentity(CT
)) {
1941 assert(CT
->NbRows
!= CT
->NbColumns
);
1942 if (CT
->NbRows
== 1) // no more parameters
1944 nparam
= CT
->NbRows
- 1;
1947 unsigned dim
= P
->Dimension
- nparam
;
1949 enumerator
et(P
, dim
, PP
->nbV
);
1952 for (nd
= 0, D
=PP
->D
; D
; ++nd
, D
=D
->next
);
1953 struct section
{ Polyhedron
*D
; evalue E
; };
1954 section
*s
= new section
[nd
];
1955 Polyhedron
**fVD
= new Polyhedron_p
[nd
];
1957 for(nd
= 0, D
=PP
->D
; D
; D
=next
) {
1960 Polyhedron
*rVD
= reduce_domain(D
->Domain
, CT
, CEq
,
1965 pVD
= CT
? DomainImage(rVD
,CT
,MaxRays
) : rVD
;
1967 value_init(s
[nd
].E
.d
);
1968 evalue_set_si(&s
[nd
].E
, 0, 1);
1970 FORALL_PVertex_in_ParamPolyhedron(V
,D
,PP
) // _i is internal counter
1972 et
.decompose_at(V
, _i
, MaxRays
);
1973 eadd(et
.vE
[_i
] , &s
[nd
].E
);
1974 END_FORALL_PVertex_in_ParamPolyhedron
;
1975 reduce_in_domain(&s
[nd
].E
, pVD
);
1978 addeliminatedparams_evalue(&s
[nd
].E
, CT
);
1986 evalue_set_si(eres
, 0, 1);
1988 eres
->x
.p
= new_enode(partition
, 2*nd
, C
->Dimension
);
1989 for (int j
= 0; j
< nd
; ++j
) {
1990 EVALUE_SET_DOMAIN(eres
->x
.p
->arr
[2*j
], s
[j
].D
);
1991 value_clear(eres
->x
.p
->arr
[2*j
+1].d
);
1992 eres
->x
.p
->arr
[2*j
+1] = s
[j
].E
;
1993 Domain_Free(fVD
[j
]);
2001 Polyhedron_Free(CEq
);
2006 Enumeration
* barvinok_enumerate(Polyhedron
*P
, Polyhedron
* C
, unsigned MaxRays
)
2008 evalue
*EP
= barvinok_enumerate_ev(P
, C
, MaxRays
);
2010 return partition2enumeration(EP
);
2013 static void SwapColumns(Value
**V
, int n
, int i
, int j
)
2015 for (int r
= 0; r
< n
; ++r
)
2016 value_swap(V
[r
][i
], V
[r
][j
]);
2019 static void SwapColumns(Polyhedron
*P
, int i
, int j
)
2021 SwapColumns(P
->Constraint
, P
->NbConstraints
, i
, j
);
2022 SwapColumns(P
->Ray
, P
->NbRays
, i
, j
);
2025 static void negative_test_constraint(Value
*l
, Value
*u
, Value
*c
, int pos
,
2028 value_oppose(*v
, u
[pos
+1]);
2029 Vector_Combine(l
+1, u
+1, c
+1, *v
, l
[pos
+1], len
-1);
2030 value_multiply(*v
, *v
, l
[pos
+1]);
2031 value_substract(c
[len
-1], c
[len
-1], *v
);
2032 value_set_si(*v
, -1);
2033 Vector_Scale(c
+1, c
+1, *v
, len
-1);
2034 value_decrement(c
[len
-1], c
[len
-1]);
2035 ConstraintSimplify(c
, c
, len
, v
);
2038 static bool parallel_constraints(Value
*l
, Value
*u
, Value
*c
, int pos
,
2047 Vector_Gcd(&l
[1+pos
], len
, &g1
);
2048 Vector_Gcd(&u
[1+pos
], len
, &g2
);
2049 Vector_Combine(l
+1+pos
, u
+1+pos
, c
+1, g2
, g1
, len
);
2050 parallel
= First_Non_Zero(c
+1, len
) == -1;
2058 static void negative_test_constraint7(Value
*l
, Value
*u
, Value
*c
, int pos
,
2059 int exist
, int len
, Value
*v
)
2064 Vector_Gcd(&u
[1+pos
], exist
, v
);
2065 Vector_Gcd(&l
[1+pos
], exist
, &g
);
2066 Vector_Combine(l
+1, u
+1, c
+1, *v
, g
, len
-1);
2067 value_multiply(*v
, *v
, g
);
2068 value_substract(c
[len
-1], c
[len
-1], *v
);
2069 value_set_si(*v
, -1);
2070 Vector_Scale(c
+1, c
+1, *v
, len
-1);
2071 value_decrement(c
[len
-1], c
[len
-1]);
2072 ConstraintSimplify(c
, c
, len
, v
);
2077 static void oppose_constraint(Value
*c
, int len
, Value
*v
)
2079 value_set_si(*v
, -1);
2080 Vector_Scale(c
+1, c
+1, *v
, len
-1);
2081 value_decrement(c
[len
-1], c
[len
-1]);
2084 static bool SplitOnConstraint(Polyhedron
*P
, int i
, int l
, int u
,
2085 int nvar
, int len
, int exist
, int MaxRays
,
2086 Vector
*row
, Value
& f
, bool independent
,
2087 Polyhedron
**pos
, Polyhedron
**neg
)
2089 negative_test_constraint(P
->Constraint
[l
], P
->Constraint
[u
],
2090 row
->p
, nvar
+i
, len
, &f
);
2091 *neg
= AddConstraints(row
->p
, 1, P
, MaxRays
);
2093 /* We found an independent, but useless constraint
2094 * Maybe we should detect this earlier and not
2095 * mark the variable as INDEPENDENT
2097 if (emptyQ((*neg
))) {
2098 Polyhedron_Free(*neg
);
2102 oppose_constraint(row
->p
, len
, &f
);
2103 *pos
= AddConstraints(row
->p
, 1, P
, MaxRays
);
2105 if (emptyQ((*pos
))) {
2106 Polyhedron_Free(*neg
);
2107 Polyhedron_Free(*pos
);
2115 * unimodularly transform P such that constraint r is transformed
2116 * into a constraint that involves only a single (the first)
2117 * existential variable
2120 static Polyhedron
*rotate_along(Polyhedron
*P
, int r
, int nvar
, int exist
,
2126 Vector
*row
= Vector_Alloc(exist
);
2127 Vector_Copy(P
->Constraint
[r
]+1+nvar
, row
->p
, exist
);
2128 Vector_Gcd(row
->p
, exist
, &g
);
2129 if (value_notone_p(g
))
2130 Vector_AntiScale(row
->p
, row
->p
, g
, exist
);
2133 Matrix
*M
= unimodular_complete(row
);
2134 Matrix
*M2
= Matrix_Alloc(P
->Dimension
+1, P
->Dimension
+1);
2135 for (r
= 0; r
< nvar
; ++r
)
2136 value_set_si(M2
->p
[r
][r
], 1);
2137 for ( ; r
< nvar
+exist
; ++r
)
2138 Vector_Copy(M
->p
[r
-nvar
], M2
->p
[r
]+nvar
, exist
);
2139 for ( ; r
< P
->Dimension
+1; ++r
)
2140 value_set_si(M2
->p
[r
][r
], 1);
2141 Polyhedron
*T
= Polyhedron_Image(P
, M2
, MaxRays
);
2150 static bool SplitOnVar(Polyhedron
*P
, int i
,
2151 int nvar
, int len
, int exist
, int MaxRays
,
2152 Vector
*row
, Value
& f
, bool independent
,
2153 Polyhedron
**pos
, Polyhedron
**neg
)
2157 for (int l
= P
->NbEq
; l
< P
->NbConstraints
; ++l
) {
2158 if (value_negz_p(P
->Constraint
[l
][nvar
+i
+1]))
2162 for (j
= 0; j
< exist
; ++j
)
2163 if (j
!= i
&& value_notzero_p(P
->Constraint
[l
][nvar
+j
+1]))
2169 for (int u
= P
->NbEq
; u
< P
->NbConstraints
; ++u
) {
2170 if (value_posz_p(P
->Constraint
[u
][nvar
+i
+1]))
2174 for (j
= 0; j
< exist
; ++j
)
2175 if (j
!= i
&& value_notzero_p(P
->Constraint
[u
][nvar
+j
+1]))
2181 if (SplitOnConstraint(P
, i
, l
, u
,
2182 nvar
, len
, exist
, MaxRays
,
2183 row
, f
, independent
,
2187 SwapColumns(*neg
, nvar
+1, nvar
+1+i
);
2197 static bool double_bound_pair(Polyhedron
*P
, int nvar
, int exist
,
2198 int i
, int l1
, int l2
,
2199 Polyhedron
**pos
, Polyhedron
**neg
)
2203 Vector
*row
= Vector_Alloc(P
->Dimension
+2);
2204 value_set_si(row
->p
[0], 1);
2205 value_oppose(f
, P
->Constraint
[l1
][nvar
+i
+1]);
2206 Vector_Combine(P
->Constraint
[l1
]+1, P
->Constraint
[l2
]+1,
2208 P
->Constraint
[l2
][nvar
+i
+1], f
,
2210 ConstraintSimplify(row
->p
, row
->p
, P
->Dimension
+2, &f
);
2211 *pos
= AddConstraints(row
->p
, 1, P
, 0);
2212 value_set_si(f
, -1);
2213 Vector_Scale(row
->p
+1, row
->p
+1, f
, P
->Dimension
+1);
2214 value_decrement(row
->p
[P
->Dimension
+1], row
->p
[P
->Dimension
+1]);
2215 *neg
= AddConstraints(row
->p
, 1, P
, 0);
2219 return !emptyQ((*pos
)) && !emptyQ((*neg
));
2222 static bool double_bound(Polyhedron
*P
, int nvar
, int exist
,
2223 Polyhedron
**pos
, Polyhedron
**neg
)
2225 for (int i
= 0; i
< exist
; ++i
) {
2227 for (l1
= P
->NbEq
; l1
< P
->NbConstraints
; ++l1
) {
2228 if (value_negz_p(P
->Constraint
[l1
][nvar
+i
+1]))
2230 for (l2
= l1
+ 1; l2
< P
->NbConstraints
; ++l2
) {
2231 if (value_negz_p(P
->Constraint
[l2
][nvar
+i
+1]))
2233 if (double_bound_pair(P
, nvar
, exist
, i
, l1
, l2
, pos
, neg
))
2237 for (l1
= P
->NbEq
; l1
< P
->NbConstraints
; ++l1
) {
2238 if (value_posz_p(P
->Constraint
[l1
][nvar
+i
+1]))
2240 if (l1
< P
->NbConstraints
)
2241 for (l2
= l1
+ 1; l2
< P
->NbConstraints
; ++l2
) {
2242 if (value_posz_p(P
->Constraint
[l2
][nvar
+i
+1]))
2244 if (double_bound_pair(P
, nvar
, exist
, i
, l1
, l2
, pos
, neg
))
2256 INDEPENDENT
= 1 << 2,
2260 static evalue
* enumerate_or(Polyhedron
*D
,
2261 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
2264 fprintf(stderr
, "\nER: Or\n");
2265 #endif /* DEBUG_ER */
2267 Polyhedron
*N
= D
->next
;
2270 barvinok_enumerate_e(D
, exist
, nparam
, MaxRays
);
2273 for (D
= N
; D
; D
= N
) {
2278 barvinok_enumerate_e(D
, exist
, nparam
, MaxRays
);
2281 free_evalue_refs(EN
);
2291 static evalue
* enumerate_sum(Polyhedron
*P
,
2292 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
2294 int nvar
= P
->Dimension
- exist
- nparam
;
2295 int toswap
= nvar
< exist
? nvar
: exist
;
2296 for (int i
= 0; i
< toswap
; ++i
)
2297 SwapColumns(P
, 1 + i
, nvar
+exist
- i
);
2301 fprintf(stderr
, "\nER: Sum\n");
2302 #endif /* DEBUG_ER */
2304 evalue
*EP
= barvinok_enumerate_e(P
, exist
, nparam
, MaxRays
);
2306 for (int i
= 0; i
< /* nvar */ nparam
; ++i
) {
2307 Matrix
*C
= Matrix_Alloc(1, 1 + nparam
+ 1);
2308 value_set_si(C
->p
[0][0], 1);
2310 value_init(split
.d
);
2311 value_set_si(split
.d
, 0);
2312 split
.x
.p
= new_enode(partition
, 4, nparam
);
2313 value_set_si(C
->p
[0][1+i
], 1);
2314 Matrix
*C2
= Matrix_Copy(C
);
2315 EVALUE_SET_DOMAIN(split
.x
.p
->arr
[0],
2316 Constraints2Polyhedron(C2
, MaxRays
));
2318 evalue_set_si(&split
.x
.p
->arr
[1], 1, 1);
2319 value_set_si(C
->p
[0][1+i
], -1);
2320 value_set_si(C
->p
[0][1+nparam
], -1);
2321 EVALUE_SET_DOMAIN(split
.x
.p
->arr
[2],
2322 Constraints2Polyhedron(C
, MaxRays
));
2323 evalue_set_si(&split
.x
.p
->arr
[3], 1, 1);
2325 free_evalue_refs(&split
);
2329 evalue_range_reduction(EP
);
2331 evalue_frac2floor(EP
);
2333 evalue
*sum
= esum(EP
, nvar
);
2335 free_evalue_refs(EP
);
2339 evalue_range_reduction(EP
);
2344 static evalue
* split_sure(Polyhedron
*P
, Polyhedron
*S
,
2345 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
2347 int nvar
= P
->Dimension
- exist
- nparam
;
2349 Matrix
*M
= Matrix_Alloc(exist
, S
->Dimension
+2);
2350 for (int i
= 0; i
< exist
; ++i
)
2351 value_set_si(M
->p
[i
][nvar
+i
+1], 1);
2353 S
= DomainAddRays(S
, M
, MaxRays
);
2355 Polyhedron
*F
= DomainAddRays(P
, M
, MaxRays
);
2356 Polyhedron
*D
= DomainDifference(F
, S
, MaxRays
);
2358 D
= Disjoint_Domain(D
, 0, MaxRays
);
2363 M
= Matrix_Alloc(P
->Dimension
+1-exist
, P
->Dimension
+1);
2364 for (int j
= 0; j
< nvar
; ++j
)
2365 value_set_si(M
->p
[j
][j
], 1);
2366 for (int j
= 0; j
< nparam
+1; ++j
)
2367 value_set_si(M
->p
[nvar
+j
][nvar
+exist
+j
], 1);
2368 Polyhedron
*T
= Polyhedron_Image(S
, M
, MaxRays
);
2369 evalue
*EP
= barvinok_enumerate_e(T
, 0, nparam
, MaxRays
);
2374 for (Polyhedron
*Q
= D
; Q
; Q
= Q
->next
) {
2375 Polyhedron
*N
= Q
->next
;
2377 T
= DomainIntersection(P
, Q
, MaxRays
);
2378 evalue
*E
= barvinok_enumerate_e(T
, exist
, nparam
, MaxRays
);
2380 free_evalue_refs(E
);
2389 static evalue
* enumerate_sure(Polyhedron
*P
,
2390 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
2394 int nvar
= P
->Dimension
- exist
- nparam
;
2400 for (i
= 0; i
< exist
; ++i
) {
2401 Matrix
*M
= Matrix_Alloc(S
->NbConstraints
, S
->Dimension
+2);
2403 value_set_si(lcm
, 1);
2404 for (int j
= 0; j
< S
->NbConstraints
; ++j
) {
2405 if (value_negz_p(S
->Constraint
[j
][1+nvar
+i
]))
2407 if (value_one_p(S
->Constraint
[j
][1+nvar
+i
]))
2409 value_lcm(lcm
, S
->Constraint
[j
][1+nvar
+i
], &lcm
);
2412 for (int j
= 0; j
< S
->NbConstraints
; ++j
) {
2413 if (value_negz_p(S
->Constraint
[j
][1+nvar
+i
]))
2415 if (value_one_p(S
->Constraint
[j
][1+nvar
+i
]))
2417 value_division(f
, lcm
, S
->Constraint
[j
][1+nvar
+i
]);
2418 Vector_Scale(S
->Constraint
[j
], M
->p
[c
], f
, S
->Dimension
+2);
2419 value_substract(M
->p
[c
][S
->Dimension
+1],
2420 M
->p
[c
][S
->Dimension
+1],
2422 value_increment(M
->p
[c
][S
->Dimension
+1],
2423 M
->p
[c
][S
->Dimension
+1]);
2427 S
= AddConstraints(M
->p
[0], c
, S
, MaxRays
);
2442 fprintf(stderr
, "\nER: Sure\n");
2443 #endif /* DEBUG_ER */
2445 return split_sure(P
, S
, exist
, nparam
, MaxRays
);
2448 static evalue
* enumerate_sure2(Polyhedron
*P
,
2449 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
2451 int nvar
= P
->Dimension
- exist
- nparam
;
2453 for (r
= 0; r
< P
->NbRays
; ++r
)
2454 if (value_one_p(P
->Ray
[r
][0]) &&
2455 value_one_p(P
->Ray
[r
][P
->Dimension
+1]))
2461 Matrix
*M
= Matrix_Alloc(nvar
+ 1 + nparam
, P
->Dimension
+2);
2462 for (int i
= 0; i
< nvar
; ++i
)
2463 value_set_si(M
->p
[i
][1+i
], 1);
2464 for (int i
= 0; i
< nparam
; ++i
)
2465 value_set_si(M
->p
[i
+nvar
][1+nvar
+exist
+i
], 1);
2466 Vector_Copy(P
->Ray
[r
]+1+nvar
, M
->p
[nvar
+nparam
]+1+nvar
, exist
);
2467 value_set_si(M
->p
[nvar
+nparam
][0], 1);
2468 value_set_si(M
->p
[nvar
+nparam
][P
->Dimension
+1], 1);
2469 Polyhedron
* F
= Rays2Polyhedron(M
, MaxRays
);
2472 Polyhedron
*I
= DomainIntersection(F
, P
, MaxRays
);
2476 fprintf(stderr
, "\nER: Sure2\n");
2477 #endif /* DEBUG_ER */
2479 return split_sure(P
, I
, exist
, nparam
, MaxRays
);
2482 static evalue
* enumerate_cyclic(Polyhedron
*P
,
2483 unsigned exist
, unsigned nparam
,
2484 evalue
* EP
, int r
, int p
, unsigned MaxRays
)
2486 int nvar
= P
->Dimension
- exist
- nparam
;
2488 /* If EP in its fractional maps only contains references
2489 * to the remainder parameter with appropriate coefficients
2490 * then we could in principle avoid adding existentially
2491 * quantified variables to the validity domains.
2492 * We'd have to replace the remainder by m { p/m }
2493 * and multiply with an appropriate factor that is one
2494 * only in the appropriate range.
2495 * This last multiplication can be avoided if EP
2496 * has a single validity domain with no (further)
2497 * constraints on the remainder parameter
2500 Matrix
*CT
= Matrix_Alloc(nparam
+1, nparam
+3);
2501 Matrix
*M
= Matrix_Alloc(1, 1+nparam
+3);
2502 for (int j
= 0; j
< nparam
; ++j
)
2504 value_set_si(CT
->p
[j
][j
], 1);
2505 value_set_si(CT
->p
[p
][nparam
+1], 1);
2506 value_set_si(CT
->p
[nparam
][nparam
+2], 1);
2507 value_set_si(M
->p
[0][1+p
], -1);
2508 value_absolute(M
->p
[0][1+nparam
], P
->Ray
[0][1+nvar
+exist
+p
]);
2509 value_set_si(M
->p
[0][1+nparam
+1], 1);
2510 Polyhedron
*CEq
= Constraints2Polyhedron(M
, 1);
2512 addeliminatedparams_enum(EP
, CT
, CEq
, MaxRays
, nparam
);
2513 Polyhedron_Free(CEq
);
2519 static void enumerate_vd_add_ray(evalue
*EP
, Matrix
*Rays
, unsigned MaxRays
)
2521 if (value_notzero_p(EP
->d
))
2524 assert(EP
->x
.p
->type
== partition
);
2525 assert(EP
->x
.p
->pos
== EVALUE_DOMAIN(EP
->x
.p
->arr
[0])->Dimension
);
2526 for (int i
= 0; i
< EP
->x
.p
->size
/2; ++i
) {
2527 Polyhedron
*D
= EVALUE_DOMAIN(EP
->x
.p
->arr
[2*i
]);
2528 Polyhedron
*N
= DomainAddRays(D
, Rays
, MaxRays
);
2529 EVALUE_SET_DOMAIN(EP
->x
.p
->arr
[2*i
], N
);
2534 static evalue
* enumerate_line(Polyhedron
*P
,
2535 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
2541 fprintf(stderr
, "\nER: Line\n");
2542 #endif /* DEBUG_ER */
2544 int nvar
= P
->Dimension
- exist
- nparam
;
2546 for (i
= 0; i
< nparam
; ++i
)
2547 if (value_notzero_p(P
->Ray
[0][1+nvar
+exist
+i
]))
2550 for (j
= i
+1; j
< nparam
; ++j
)
2551 if (value_notzero_p(P
->Ray
[0][1+nvar
+exist
+i
]))
2553 assert(j
>= nparam
); // for now
2555 Matrix
*M
= Matrix_Alloc(2, P
->Dimension
+2);
2556 value_set_si(M
->p
[0][0], 1);
2557 value_set_si(M
->p
[0][1+nvar
+exist
+i
], 1);
2558 value_set_si(M
->p
[1][0], 1);
2559 value_set_si(M
->p
[1][1+nvar
+exist
+i
], -1);
2560 value_absolute(M
->p
[1][1+P
->Dimension
], P
->Ray
[0][1+nvar
+exist
+i
]);
2561 value_decrement(M
->p
[1][1+P
->Dimension
], M
->p
[1][1+P
->Dimension
]);
2562 Polyhedron
*S
= AddConstraints(M
->p
[0], 2, P
, MaxRays
);
2563 evalue
*EP
= barvinok_enumerate_e(S
, exist
, nparam
, MaxRays
);
2567 return enumerate_cyclic(P
, exist
, nparam
, EP
, 0, i
, MaxRays
);
2570 static int single_param_pos(Polyhedron
*P
, unsigned exist
, unsigned nparam
,
2573 int nvar
= P
->Dimension
- exist
- nparam
;
2574 if (First_Non_Zero(P
->Ray
[r
]+1, nvar
) != -1)
2576 int i
= First_Non_Zero(P
->Ray
[r
]+1+nvar
+exist
, nparam
);
2579 if (First_Non_Zero(P
->Ray
[r
]+1+nvar
+exist
+1, nparam
-i
-1) != -1)
2584 static evalue
* enumerate_remove_ray(Polyhedron
*P
, int r
,
2585 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
2588 fprintf(stderr
, "\nER: RedundantRay\n");
2589 #endif /* DEBUG_ER */
2593 value_set_si(one
, 1);
2594 int len
= P
->NbRays
-1;
2595 Matrix
*M
= Matrix_Alloc(2 * len
, P
->Dimension
+2);
2596 Vector_Copy(P
->Ray
[0], M
->p
[0], r
* (P
->Dimension
+2));
2597 Vector_Copy(P
->Ray
[r
+1], M
->p
[r
], (len
-r
) * (P
->Dimension
+2));
2598 for (int j
= 0; j
< P
->NbRays
; ++j
) {
2601 Vector_Combine(P
->Ray
[j
], P
->Ray
[r
], M
->p
[len
+j
-(j
>r
)],
2602 one
, P
->Ray
[j
][P
->Dimension
+1], P
->Dimension
+2);
2605 P
= Rays2Polyhedron(M
, MaxRays
);
2607 evalue
*EP
= barvinok_enumerate_e(P
, exist
, nparam
, MaxRays
);
2614 static evalue
* enumerate_redundant_ray(Polyhedron
*P
,
2615 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
2617 assert(P
->NbBid
== 0);
2618 int nvar
= P
->Dimension
- exist
- nparam
;
2622 for (int r
= 0; r
< P
->NbRays
; ++r
) {
2623 if (value_notzero_p(P
->Ray
[r
][P
->Dimension
+1]))
2625 int i1
= single_param_pos(P
, exist
, nparam
, r
);
2628 for (int r2
= r
+1; r2
< P
->NbRays
; ++r2
) {
2629 if (value_notzero_p(P
->Ray
[r2
][P
->Dimension
+1]))
2631 int i2
= single_param_pos(P
, exist
, nparam
, r2
);
2637 value_division(m
, P
->Ray
[r
][1+nvar
+exist
+i1
],
2638 P
->Ray
[r2
][1+nvar
+exist
+i1
]);
2639 value_multiply(m
, m
, P
->Ray
[r2
][1+nvar
+exist
+i1
]);
2640 /* r2 divides r => r redundant */
2641 if (value_eq(m
, P
->Ray
[r
][1+nvar
+exist
+i1
])) {
2643 return enumerate_remove_ray(P
, r
, exist
, nparam
, MaxRays
);
2646 value_division(m
, P
->Ray
[r2
][1+nvar
+exist
+i1
],
2647 P
->Ray
[r
][1+nvar
+exist
+i1
]);
2648 value_multiply(m
, m
, P
->Ray
[r
][1+nvar
+exist
+i1
]);
2649 /* r divides r2 => r2 redundant */
2650 if (value_eq(m
, P
->Ray
[r2
][1+nvar
+exist
+i1
])) {
2652 return enumerate_remove_ray(P
, r2
, exist
, nparam
, MaxRays
);
2660 static Polyhedron
*upper_bound(Polyhedron
*P
,
2661 int pos
, Value
*max
, Polyhedron
**R
)
2670 for (Polyhedron
*Q
= P
; Q
; Q
= N
) {
2672 for (r
= 0; r
< P
->NbRays
; ++r
) {
2673 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1]) &&
2674 value_pos_p(P
->Ray
[r
][1+pos
]))
2677 if (r
< P
->NbRays
) {
2685 for (r
= 0; r
< P
->NbRays
; ++r
) {
2686 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1]))
2688 mpz_fdiv_q(v
, P
->Ray
[r
][1+pos
], P
->Ray
[r
][1+P
->Dimension
]);
2689 if ((!Q
->next
&& r
== 0) || value_gt(v
, *max
))
2690 value_assign(*max
, v
);
2697 static evalue
* enumerate_ray(Polyhedron
*P
,
2698 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
2700 assert(P
->NbBid
== 0);
2701 int nvar
= P
->Dimension
- exist
- nparam
;
2704 for (r
= 0; r
< P
->NbRays
; ++r
)
2705 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1]))
2711 for (r2
= r
+1; r2
< P
->NbRays
; ++r2
)
2712 if (value_zero_p(P
->Ray
[r2
][P
->Dimension
+1]))
2714 if (r2
< P
->NbRays
) {
2716 return enumerate_sum(P
, exist
, nparam
, MaxRays
);
2720 fprintf(stderr
, "\nER: Ray\n");
2721 #endif /* DEBUG_ER */
2727 value_set_si(one
, 1);
2728 int i
= single_param_pos(P
, exist
, nparam
, r
);
2729 assert(i
!= -1); // for now;
2731 Matrix
*M
= Matrix_Alloc(P
->NbRays
, P
->Dimension
+2);
2732 for (int j
= 0; j
< P
->NbRays
; ++j
) {
2733 Vector_Combine(P
->Ray
[j
], P
->Ray
[r
], M
->p
[j
],
2734 one
, P
->Ray
[j
][P
->Dimension
+1], P
->Dimension
+2);
2736 Polyhedron
*S
= Rays2Polyhedron(M
, MaxRays
);
2738 Polyhedron
*D
= DomainDifference(P
, S
, MaxRays
);
2740 // Polyhedron_Print(stderr, P_VALUE_FMT, D);
2741 assert(value_pos_p(P
->Ray
[r
][1+nvar
+exist
+i
])); // for now
2743 D
= upper_bound(D
, nvar
+exist
+i
, &m
, &R
);
2747 M
= Matrix_Alloc(2, P
->Dimension
+2);
2748 value_set_si(M
->p
[0][0], 1);
2749 value_set_si(M
->p
[1][0], 1);
2750 value_set_si(M
->p
[0][1+nvar
+exist
+i
], -1);
2751 value_set_si(M
->p
[1][1+nvar
+exist
+i
], 1);
2752 value_assign(M
->p
[0][1+P
->Dimension
], m
);
2753 value_oppose(M
->p
[1][1+P
->Dimension
], m
);
2754 value_addto(M
->p
[1][1+P
->Dimension
], M
->p
[1][1+P
->Dimension
],
2755 P
->Ray
[r
][1+nvar
+exist
+i
]);
2756 value_decrement(M
->p
[1][1+P
->Dimension
], M
->p
[1][1+P
->Dimension
]);
2757 // Matrix_Print(stderr, P_VALUE_FMT, M);
2758 D
= AddConstraints(M
->p
[0], 2, P
, MaxRays
);
2759 // Polyhedron_Print(stderr, P_VALUE_FMT, D);
2760 value_substract(M
->p
[0][1+P
->Dimension
], M
->p
[0][1+P
->Dimension
],
2761 P
->Ray
[r
][1+nvar
+exist
+i
]);
2762 // Matrix_Print(stderr, P_VALUE_FMT, M);
2763 S
= AddConstraints(M
->p
[0], 1, P
, MaxRays
);
2764 // Polyhedron_Print(stderr, P_VALUE_FMT, S);
2767 evalue
*EP
= barvinok_enumerate_e(D
, exist
, nparam
, MaxRays
);
2772 if (value_notone_p(P
->Ray
[r
][1+nvar
+exist
+i
]))
2773 EP
= enumerate_cyclic(P
, exist
, nparam
, EP
, r
, i
, MaxRays
);
2775 M
= Matrix_Alloc(1, nparam
+2);
2776 value_set_si(M
->p
[0][0], 1);
2777 value_set_si(M
->p
[0][1+i
], 1);
2778 enumerate_vd_add_ray(EP
, M
, MaxRays
);
2783 evalue
*E
= barvinok_enumerate_e(S
, exist
, nparam
, MaxRays
);
2785 free_evalue_refs(E
);
2792 evalue
*ER
= enumerate_or(R
, exist
, nparam
, MaxRays
);
2794 free_evalue_refs(ER
);
2801 static evalue
* new_zero_ep()
2806 evalue_set_si(EP
, 0, 1);
2810 static evalue
* enumerate_vd(Polyhedron
**PA
,
2811 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
2813 Polyhedron
*P
= *PA
;
2814 int nvar
= P
->Dimension
- exist
- nparam
;
2815 Param_Polyhedron
*PP
= NULL
;
2816 Polyhedron
*C
= Universe_Polyhedron(nparam
);
2820 PP
= Polyhedron2Param_SimplifiedDomain(&PR
,C
,MaxRays
,&CEq
,&CT
);
2824 Param_Domain
*D
, *last
;
2827 for (nd
= 0, D
=PP
->D
; D
; D
=D
->next
, ++nd
)
2830 Polyhedron
**VD
= new Polyhedron_p
[nd
];
2831 Polyhedron
**fVD
= new Polyhedron_p
[nd
];
2832 for(nd
= 0, D
=PP
->D
; D
; D
=D
->next
) {
2833 Polyhedron
*rVD
= reduce_domain(D
->Domain
, CT
, CEq
,
2847 /* This doesn't seem to have any effect */
2849 Polyhedron
*CA
= align_context(VD
[0], P
->Dimension
, MaxRays
);
2851 P
= DomainIntersection(P
, CA
, MaxRays
);
2854 Polyhedron_Free(CA
);
2859 if (!EP
&& CT
->NbColumns
!= CT
->NbRows
) {
2860 Polyhedron
*CEqr
= DomainImage(CEq
, CT
, MaxRays
);
2861 Polyhedron
*CA
= align_context(CEqr
, PR
->Dimension
, MaxRays
);
2862 Polyhedron
*I
= DomainIntersection(PR
, CA
, MaxRays
);
2863 Polyhedron_Free(CEqr
);
2864 Polyhedron_Free(CA
);
2866 fprintf(stderr
, "\nER: Eliminate\n");
2867 #endif /* DEBUG_ER */
2868 nparam
-= CT
->NbColumns
- CT
->NbRows
;
2869 EP
= barvinok_enumerate_e(I
, exist
, nparam
, MaxRays
);
2870 nparam
+= CT
->NbColumns
- CT
->NbRows
;
2871 addeliminatedparams_enum(EP
, CT
, CEq
, MaxRays
, nparam
);
2875 Polyhedron_Free(PR
);
2878 if (!EP
&& nd
> 1) {
2880 fprintf(stderr
, "\nER: VD\n");
2881 #endif /* DEBUG_ER */
2882 for (int i
= 0; i
< nd
; ++i
) {
2883 Polyhedron
*CA
= align_context(VD
[i
], P
->Dimension
, MaxRays
);
2884 Polyhedron
*I
= DomainIntersection(P
, CA
, MaxRays
);
2887 EP
= barvinok_enumerate_e(I
, exist
, nparam
, MaxRays
);
2889 evalue
*E
= barvinok_enumerate_e(I
, exist
, nparam
, MaxRays
);
2891 free_evalue_refs(E
);
2895 Polyhedron_Free(CA
);
2899 for (int i
= 0; i
< nd
; ++i
) {
2900 Polyhedron_Free(VD
[i
]);
2901 Polyhedron_Free(fVD
[i
]);
2907 if (!EP
&& nvar
== 0) {
2910 Param_Vertices
*V
, *V2
;
2911 Matrix
* M
= Matrix_Alloc(1, P
->Dimension
+2);
2913 FORALL_PVertex_in_ParamPolyhedron(V
, last
, PP
) {
2915 FORALL_PVertex_in_ParamPolyhedron(V2
, last
, PP
) {
2922 for (int i
= 0; i
< exist
; ++i
) {
2923 value_oppose(f
, V
->Vertex
->p
[i
][nparam
+1]);
2924 Vector_Combine(V
->Vertex
->p
[i
],
2926 M
->p
[0] + 1 + nvar
+ exist
,
2927 V2
->Vertex
->p
[i
][nparam
+1],
2931 for (j
= 0; j
< nparam
; ++j
)
2932 if (value_notzero_p(M
->p
[0][1+nvar
+exist
+j
]))
2936 ConstraintSimplify(M
->p
[0], M
->p
[0],
2937 P
->Dimension
+2, &f
);
2938 value_set_si(M
->p
[0][0], 0);
2939 Polyhedron
*para
= AddConstraints(M
->p
[0], 1, P
,
2942 Polyhedron_Free(para
);
2945 Polyhedron
*pos
, *neg
;
2946 value_set_si(M
->p
[0][0], 1);
2947 value_decrement(M
->p
[0][P
->Dimension
+1],
2948 M
->p
[0][P
->Dimension
+1]);
2949 neg
= AddConstraints(M
->p
[0], 1, P
, MaxRays
);
2950 value_set_si(f
, -1);
2951 Vector_Scale(M
->p
[0]+1, M
->p
[0]+1, f
,
2953 value_decrement(M
->p
[0][P
->Dimension
+1],
2954 M
->p
[0][P
->Dimension
+1]);
2955 value_decrement(M
->p
[0][P
->Dimension
+1],
2956 M
->p
[0][P
->Dimension
+1]);
2957 pos
= AddConstraints(M
->p
[0], 1, P
, MaxRays
);
2958 if (emptyQ(neg
) && emptyQ(pos
)) {
2959 Polyhedron_Free(para
);
2960 Polyhedron_Free(pos
);
2961 Polyhedron_Free(neg
);
2965 fprintf(stderr
, "\nER: Order\n");
2966 #endif /* DEBUG_ER */
2967 EP
= barvinok_enumerate_e(para
, exist
, nparam
, MaxRays
);
2970 E
= barvinok_enumerate_e(pos
, exist
, nparam
, MaxRays
);
2972 free_evalue_refs(E
);
2976 E
= barvinok_enumerate_e(neg
, exist
, nparam
, MaxRays
);
2978 free_evalue_refs(E
);
2981 Polyhedron_Free(para
);
2982 Polyhedron_Free(pos
);
2983 Polyhedron_Free(neg
);
2988 } END_FORALL_PVertex_in_ParamPolyhedron
;
2991 } END_FORALL_PVertex_in_ParamPolyhedron
;
2994 /* Search for vertex coordinate to split on */
2995 /* First look for one independent of the parameters */
2996 FORALL_PVertex_in_ParamPolyhedron(V
, last
, PP
) {
2997 for (int i
= 0; i
< exist
; ++i
) {
2999 for (j
= 0; j
< nparam
; ++j
)
3000 if (value_notzero_p(V
->Vertex
->p
[i
][j
]))
3004 value_set_si(M
->p
[0][0], 1);
3005 Vector_Set(M
->p
[0]+1, 0, nvar
+exist
);
3006 Vector_Copy(V
->Vertex
->p
[i
],
3007 M
->p
[0] + 1 + nvar
+ exist
, nparam
+1);
3008 value_oppose(M
->p
[0][1+nvar
+i
],
3009 V
->Vertex
->p
[i
][nparam
+1]);
3011 Polyhedron
*pos
, *neg
;
3012 value_set_si(M
->p
[0][0], 1);
3013 value_decrement(M
->p
[0][P
->Dimension
+1],
3014 M
->p
[0][P
->Dimension
+1]);
3015 neg
= AddConstraints(M
->p
[0], 1, P
, MaxRays
);
3016 value_set_si(f
, -1);
3017 Vector_Scale(M
->p
[0]+1, M
->p
[0]+1, f
,
3019 value_decrement(M
->p
[0][P
->Dimension
+1],
3020 M
->p
[0][P
->Dimension
+1]);
3021 value_decrement(M
->p
[0][P
->Dimension
+1],
3022 M
->p
[0][P
->Dimension
+1]);
3023 pos
= AddConstraints(M
->p
[0], 1, P
, MaxRays
);
3024 if (emptyQ(neg
) || emptyQ(pos
)) {
3025 Polyhedron_Free(pos
);
3026 Polyhedron_Free(neg
);
3029 Polyhedron_Free(pos
);
3030 value_increment(M
->p
[0][P
->Dimension
+1],
3031 M
->p
[0][P
->Dimension
+1]);
3032 pos
= AddConstraints(M
->p
[0], 1, P
, MaxRays
);
3034 fprintf(stderr
, "\nER: Vertex\n");
3035 #endif /* DEBUG_ER */
3037 EP
= enumerate_or(pos
, exist
, nparam
, MaxRays
);
3042 } END_FORALL_PVertex_in_ParamPolyhedron
;
3046 /* Search for vertex coordinate to split on */
3047 /* Now look for one that depends on the parameters */
3048 FORALL_PVertex_in_ParamPolyhedron(V
, last
, PP
) {
3049 for (int i
= 0; i
< exist
; ++i
) {
3050 value_set_si(M
->p
[0][0], 1);
3051 Vector_Set(M
->p
[0]+1, 0, nvar
+exist
);
3052 Vector_Copy(V
->Vertex
->p
[i
],
3053 M
->p
[0] + 1 + nvar
+ exist
, nparam
+1);
3054 value_oppose(M
->p
[0][1+nvar
+i
],
3055 V
->Vertex
->p
[i
][nparam
+1]);
3057 Polyhedron
*pos
, *neg
;
3058 value_set_si(M
->p
[0][0], 1);
3059 value_decrement(M
->p
[0][P
->Dimension
+1],
3060 M
->p
[0][P
->Dimension
+1]);
3061 neg
= AddConstraints(M
->p
[0], 1, P
, MaxRays
);
3062 value_set_si(f
, -1);
3063 Vector_Scale(M
->p
[0]+1, M
->p
[0]+1, f
,
3065 value_decrement(M
->p
[0][P
->Dimension
+1],
3066 M
->p
[0][P
->Dimension
+1]);
3067 value_decrement(M
->p
[0][P
->Dimension
+1],
3068 M
->p
[0][P
->Dimension
+1]);
3069 pos
= AddConstraints(M
->p
[0], 1, P
, MaxRays
);
3070 if (emptyQ(neg
) || emptyQ(pos
)) {
3071 Polyhedron_Free(pos
);
3072 Polyhedron_Free(neg
);
3075 Polyhedron_Free(pos
);
3076 value_increment(M
->p
[0][P
->Dimension
+1],
3077 M
->p
[0][P
->Dimension
+1]);
3078 pos
= AddConstraints(M
->p
[0], 1, P
, MaxRays
);
3080 fprintf(stderr
, "\nER: ParamVertex\n");
3081 #endif /* DEBUG_ER */
3083 EP
= enumerate_or(pos
, exist
, nparam
, MaxRays
);
3088 } END_FORALL_PVertex_in_ParamPolyhedron
;
3096 Polyhedron_Free(CEq
);
3100 Param_Polyhedron_Free(PP
);
3107 evalue
*barvinok_enumerate_pip(Polyhedron
*P
,
3108 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
3113 evalue
*barvinok_enumerate_pip(Polyhedron
*P
,
3114 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
3116 int nvar
= P
->Dimension
- exist
- nparam
;
3117 evalue
*EP
= new_zero_ep();
3118 Polyhedron
*Q
, *N
, *T
= 0;
3124 fprintf(stderr
, "\nER: PIP\n");
3125 #endif /* DEBUG_ER */
3127 for (int i
= 0; i
< P
->Dimension
; ++i
) {
3130 bool posray
= false;
3131 bool negray
= false;
3132 value_set_si(min
, 0);
3133 for (int j
= 0; j
< P
->NbRays
; ++j
) {
3134 if (value_pos_p(P
->Ray
[j
][1+i
])) {
3136 if (value_zero_p(P
->Ray
[j
][1+P
->Dimension
]))
3138 } else if (value_neg_p(P
->Ray
[j
][1+i
])) {
3140 if (value_zero_p(P
->Ray
[j
][1+P
->Dimension
]))
3144 P
->Ray
[j
][1+i
], P
->Ray
[j
][1+P
->Dimension
]);
3145 if (value_lt(tmp
, min
))
3146 value_assign(min
, tmp
);
3151 assert(!(posray
&& negray
));
3152 assert(!negray
); // for now
3153 Polyhedron
*O
= T
? T
: P
;
3154 /* shift by a safe amount */
3155 Matrix
*M
= Matrix_Alloc(O
->NbRays
, O
->Dimension
+2);
3156 Vector_Copy(O
->Ray
[0], M
->p
[0], O
->NbRays
* (O
->Dimension
+2));
3157 for (int j
= 0; j
< P
->NbRays
; ++j
) {
3158 if (value_notzero_p(M
->p
[j
][1+P
->Dimension
])) {
3159 value_multiply(tmp
, min
, M
->p
[j
][1+P
->Dimension
]);
3160 value_substract(M
->p
[j
][1+i
], M
->p
[j
][1+i
], tmp
);
3165 T
= Rays2Polyhedron(M
, MaxRays
);
3168 /* negating a parameter requires that we substitute in the
3169 * sign again afterwards.
3172 assert(i
< nvar
+exist
);
3174 T
= Polyhedron_Copy(P
);
3175 for (int j
= 0; j
< T
->NbRays
; ++j
)
3176 value_oppose(T
->Ray
[j
][1+i
], T
->Ray
[j
][1+i
]);
3177 for (int j
= 0; j
< T
->NbConstraints
; ++j
)
3178 value_oppose(T
->Constraint
[j
][1+i
], T
->Constraint
[j
][1+i
]);
3184 Polyhedron
*D
= pip_lexmin(T
? T
: P
, exist
, nparam
);
3185 for (Q
= D
; Q
; Q
= N
) {
3189 exist
= Q
->Dimension
- nvar
- nparam
;
3190 E
= barvinok_enumerate_e(Q
, exist
, nparam
, MaxRays
);
3193 free_evalue_refs(E
);
3205 static bool is_single(Value
*row
, int pos
, int len
)
3207 return First_Non_Zero(row
, pos
) == -1 &&
3208 First_Non_Zero(row
+pos
+1, len
-pos
-1) == -1;
3211 static evalue
* barvinok_enumerate_e_r(Polyhedron
*P
,
3212 unsigned exist
, unsigned nparam
, unsigned MaxRays
);
3215 static int er_level
= 0;
3217 evalue
* barvinok_enumerate_e(Polyhedron
*P
,
3218 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
3220 fprintf(stderr
, "\nER: level %i\n", er_level
);
3221 int nvar
= P
->Dimension
- exist
- nparam
;
3222 fprintf(stderr
, "%d %d %d\n", nvar
, exist
, nparam
);
3224 Polyhedron_Print(stderr
, P_VALUE_FMT
, P
);
3226 P
= DomainConstraintSimplify(Polyhedron_Copy(P
), MaxRays
);
3227 evalue
*EP
= barvinok_enumerate_e_r(P
, exist
, nparam
, MaxRays
);
3233 evalue
* barvinok_enumerate_e(Polyhedron
*P
,
3234 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
3236 P
= DomainConstraintSimplify(Polyhedron_Copy(P
), MaxRays
);
3237 evalue
*EP
= barvinok_enumerate_e_r(P
, exist
, nparam
, MaxRays
);
3243 static evalue
* barvinok_enumerate_e_r(Polyhedron
*P
,
3244 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
3247 Polyhedron
*U
= Universe_Polyhedron(nparam
);
3248 evalue
*EP
= barvinok_enumerate_ev(P
, U
, MaxRays
);
3249 //char *param_name[] = {"P", "Q", "R", "S", "T" };
3250 //print_evalue(stdout, EP, param_name);
3255 int nvar
= P
->Dimension
- exist
- nparam
;
3256 int len
= P
->Dimension
+ 2;
3259 return new_zero_ep();
3261 if (nvar
== 0 && nparam
== 0) {
3262 evalue
*EP
= new_zero_ep();
3263 barvinok_count(P
, &EP
->x
.n
, MaxRays
);
3264 if (value_pos_p(EP
->x
.n
))
3265 value_set_si(EP
->x
.n
, 1);
3270 for (r
= 0; r
< P
->NbRays
; ++r
)
3271 if (value_zero_p(P
->Ray
[r
][0]) ||
3272 value_zero_p(P
->Ray
[r
][P
->Dimension
+1])) {
3274 for (i
= 0; i
< nvar
; ++i
)
3275 if (value_notzero_p(P
->Ray
[r
][i
+1]))
3279 for (i
= nvar
+ exist
; i
< nvar
+ exist
+ nparam
; ++i
)
3280 if (value_notzero_p(P
->Ray
[r
][i
+1]))
3282 if (i
>= nvar
+ exist
+ nparam
)
3285 if (r
< P
->NbRays
) {
3286 evalue
*EP
= new_zero_ep();
3287 value_set_si(EP
->x
.n
, -1);
3292 for (r
= 0; r
< P
->NbEq
; ++r
)
3293 if ((first
= First_Non_Zero(P
->Constraint
[r
]+1+nvar
, exist
)) != -1)
3296 if (First_Non_Zero(P
->Constraint
[r
]+1+nvar
+first
+1,
3297 exist
-first
-1) != -1) {
3298 Polyhedron
*T
= rotate_along(P
, r
, nvar
, exist
, MaxRays
);
3300 fprintf(stderr
, "\nER: Equality\n");
3301 #endif /* DEBUG_ER */
3302 evalue
*EP
= barvinok_enumerate_e(T
, exist
-1, nparam
, MaxRays
);
3307 fprintf(stderr
, "\nER: Fixed\n");
3308 #endif /* DEBUG_ER */
3310 return barvinok_enumerate_e(P
, exist
-1, nparam
, MaxRays
);
3312 Polyhedron
*T
= Polyhedron_Copy(P
);
3313 SwapColumns(T
, nvar
+1, nvar
+1+first
);
3314 evalue
*EP
= barvinok_enumerate_e(T
, exist
-1, nparam
, MaxRays
);
3321 Vector
*row
= Vector_Alloc(len
);
3322 value_set_si(row
->p
[0], 1);
3327 enum constraint
* info
= new constraint
[exist
];
3328 for (int i
= 0; i
< exist
; ++i
) {
3330 for (int l
= P
->NbEq
; l
< P
->NbConstraints
; ++l
) {
3331 if (value_negz_p(P
->Constraint
[l
][nvar
+i
+1]))
3333 bool l_parallel
= is_single(P
->Constraint
[l
]+nvar
+1, i
, exist
);
3334 for (int u
= P
->NbEq
; u
< P
->NbConstraints
; ++u
) {
3335 if (value_posz_p(P
->Constraint
[u
][nvar
+i
+1]))
3337 bool lu_parallel
= l_parallel
||
3338 is_single(P
->Constraint
[u
]+nvar
+1, i
, exist
);
3339 value_oppose(f
, P
->Constraint
[u
][nvar
+i
+1]);
3340 Vector_Combine(P
->Constraint
[l
]+1, P
->Constraint
[u
]+1, row
->p
+1,
3341 f
, P
->Constraint
[l
][nvar
+i
+1], len
-1);
3342 if (!(info
[i
] & INDEPENDENT
)) {
3344 for (j
= 0; j
< exist
; ++j
)
3345 if (j
!= i
&& value_notzero_p(row
->p
[nvar
+j
+1]))
3348 //printf("independent: i: %d, l: %d, u: %d\n", i, l, u);
3349 info
[i
] = (constraint
)(info
[i
] | INDEPENDENT
);
3352 if (info
[i
] & ALL_POS
) {
3353 value_addto(row
->p
[len
-1], row
->p
[len
-1],
3354 P
->Constraint
[l
][nvar
+i
+1]);
3355 value_addto(row
->p
[len
-1], row
->p
[len
-1], f
);
3356 value_multiply(f
, f
, P
->Constraint
[l
][nvar
+i
+1]);
3357 value_substract(row
->p
[len
-1], row
->p
[len
-1], f
);
3358 value_decrement(row
->p
[len
-1], row
->p
[len
-1]);
3359 ConstraintSimplify(row
->p
, row
->p
, len
, &f
);
3360 value_set_si(f
, -1);
3361 Vector_Scale(row
->p
+1, row
->p
+1, f
, len
-1);
3362 value_decrement(row
->p
[len
-1], row
->p
[len
-1]);
3363 Polyhedron
*T
= AddConstraints(row
->p
, 1, P
, MaxRays
);
3365 //printf("not all_pos: i: %d, l: %d, u: %d\n", i, l, u);
3366 info
[i
] = (constraint
)(info
[i
] ^ ALL_POS
);
3368 //puts("pos remainder");
3369 //Polyhedron_Print(stdout, P_VALUE_FMT, T);
3372 if (!(info
[i
] & ONE_NEG
)) {
3374 negative_test_constraint(P
->Constraint
[l
],
3376 row
->p
, nvar
+i
, len
, &f
);
3377 oppose_constraint(row
->p
, len
, &f
);
3378 Polyhedron
*T
= AddConstraints(row
->p
, 1, P
, MaxRays
);
3380 //printf("one_neg i: %d, l: %d, u: %d\n", i, l, u);
3381 info
[i
] = (constraint
)(info
[i
] | ONE_NEG
);
3383 //puts("neg remainder");
3384 //Polyhedron_Print(stdout, P_VALUE_FMT, T);
3386 } else if (!(info
[i
] & ROT_NEG
)) {
3387 if (parallel_constraints(P
->Constraint
[l
],
3389 row
->p
, nvar
, exist
)) {
3390 negative_test_constraint7(P
->Constraint
[l
],
3392 row
->p
, nvar
, exist
,
3394 oppose_constraint(row
->p
, len
, &f
);
3395 Polyhedron
*T
= AddConstraints(row
->p
, 1, P
, MaxRays
);
3397 // printf("rot_neg i: %d, l: %d, u: %d\n", i, l, u);
3398 info
[i
] = (constraint
)(info
[i
] | ROT_NEG
);
3401 //puts("neg remainder");
3402 //Polyhedron_Print(stdout, P_VALUE_FMT, T);
3407 if (!(info
[i
] & ALL_POS
) && (info
[i
] & (ONE_NEG
| ROT_NEG
)))
3411 if (info
[i
] & ALL_POS
)
3418 for (int i = 0; i < exist; ++i)
3419 printf("%i: %i\n", i, info[i]);
3421 for (int i
= 0; i
< exist
; ++i
)
3422 if (info
[i
] & ALL_POS
) {
3424 fprintf(stderr
, "\nER: Positive\n");
3425 #endif /* DEBUG_ER */
3427 // Maybe we should chew off some of the fat here
3428 Matrix
*M
= Matrix_Alloc(P
->Dimension
, P
->Dimension
+1);
3429 for (int j
= 0; j
< P
->Dimension
; ++j
)
3430 value_set_si(M
->p
[j
][j
+ (j
>= i
+nvar
)], 1);
3431 Polyhedron
*T
= Polyhedron_Image(P
, M
, MaxRays
);
3433 evalue
*EP
= barvinok_enumerate_e(T
, exist
-1, nparam
, MaxRays
);
3440 for (int i
= 0; i
< exist
; ++i
)
3441 if (info
[i
] & ONE_NEG
) {
3443 fprintf(stderr
, "\nER: Negative\n");
3444 #endif /* DEBUG_ER */
3449 return barvinok_enumerate_e(P
, exist
-1, nparam
, MaxRays
);
3451 Polyhedron
*T
= Polyhedron_Copy(P
);
3452 SwapColumns(T
, nvar
+1, nvar
+1+i
);
3453 evalue
*EP
= barvinok_enumerate_e(T
, exist
-1, nparam
, MaxRays
);
3458 for (int i
= 0; i
< exist
; ++i
)
3459 if (info
[i
] & ROT_NEG
) {
3461 fprintf(stderr
, "\nER: Rotate\n");
3462 #endif /* DEBUG_ER */
3466 Polyhedron
*T
= rotate_along(P
, r
, nvar
, exist
, MaxRays
);
3467 evalue
*EP
= barvinok_enumerate_e(T
, exist
-1, nparam
, MaxRays
);
3471 for (int i
= 0; i
< exist
; ++i
)
3472 if (info
[i
] & INDEPENDENT
) {
3473 Polyhedron
*pos
, *neg
;
3475 /* Find constraint again and split off negative part */
3477 if (SplitOnVar(P
, i
, nvar
, len
, exist
, MaxRays
,
3478 row
, f
, true, &pos
, &neg
)) {
3480 fprintf(stderr
, "\nER: Split\n");
3481 #endif /* DEBUG_ER */
3484 barvinok_enumerate_e(neg
, exist
-1, nparam
, MaxRays
);
3486 barvinok_enumerate_e(pos
, exist
, nparam
, MaxRays
);
3488 free_evalue_refs(E
);
3490 Polyhedron_Free(neg
);
3491 Polyhedron_Free(pos
);
3505 EP
= enumerate_line(P
, exist
, nparam
, MaxRays
);
3509 EP
= barvinok_enumerate_pip(P
, exist
, nparam
, MaxRays
);
3513 EP
= enumerate_redundant_ray(P
, exist
, nparam
, MaxRays
);
3517 EP
= enumerate_sure(P
, exist
, nparam
, MaxRays
);
3521 EP
= enumerate_ray(P
, exist
, nparam
, MaxRays
);
3525 EP
= enumerate_sure2(P
, exist
, nparam
, MaxRays
);
3529 F
= unfringe(P
, MaxRays
);
3530 if (!PolyhedronIncludes(F
, P
)) {
3532 fprintf(stderr
, "\nER: Fringed\n");
3533 #endif /* DEBUG_ER */
3534 EP
= barvinok_enumerate_e(F
, exist
, nparam
, MaxRays
);
3541 EP
= enumerate_vd(&P
, exist
, nparam
, MaxRays
);
3546 EP
= enumerate_sum(P
, exist
, nparam
, MaxRays
);
3553 Polyhedron
*pos
, *neg
;
3554 for (i
= 0; i
< exist
; ++i
)
3555 if (SplitOnVar(P
, i
, nvar
, len
, exist
, MaxRays
,
3556 row
, f
, false, &pos
, &neg
))
3562 EP
= enumerate_or(pos
, exist
, nparam
, MaxRays
);
3574 gen_fun
* barvinok_series(Polyhedron
*P
, Polyhedron
* C
, unsigned MaxRays
)
3576 Polyhedron
** vcone
;
3578 unsigned nparam
= C
->Dimension
;
3582 sign
.SetLength(ncone
);
3584 CA
= align_context(C
, P
->Dimension
, MaxRays
);
3585 P
= DomainIntersection(P
, CA
, MaxRays
);
3586 Polyhedron_Free(CA
);
3588 assert(!Polyhedron_is_infinite(P
, nparam
));
3589 assert(P
->NbBid
== 0);
3590 assert(Polyhedron_has_positive_rays(P
, nparam
));
3591 assert(P
->NbEq
== 0);
3594 nvar
= dim
- nparam
;
3595 vcone
= new Polyhedron_p
[P
->NbRays
];
3597 for (int j
= 0; j
< P
->NbRays
; ++j
) {
3598 if (!value_pos_p(P
->Ray
[j
][dim
+1]))
3602 Polyhedron
*C
= supporting_cone(P
, j
);
3603 decompose(C
, &vcone
[j
], &npos
, &nneg
, MaxRays
);
3604 ncone
+= npos
+ nneg
;
3605 sign
.SetLength(ncone
);
3606 for (int k
= 0; k
< npos
; ++k
)
3607 sign
[ncone
-nneg
-k
-1] = 1;
3608 for (int k
= 0; k
< nneg
; ++k
)
3609 sign
[ncone
-k
-1] = -1;
3613 rays
.SetDims(ncone
* dim
, nvar
);
3615 for (int j
= 0; j
< P
->NbRays
; ++j
) {
3616 if (!value_pos_p(P
->Ray
[j
][dim
+1]))
3619 for (Polyhedron
*i
= vcone
[j
]; i
; i
= i
->next
) {
3620 add_rays(rays
, i
, &r
, nvar
);
3623 rays
.SetDims(r
, nvar
);
3625 nonorthog(rays
, lambda
);
3626 //randomvector(P, lambda, nvar);
3629 cout << "rays: " << rays;
3630 cout << "lambda: " << lambda;
3636 num_p
.SetLength(nparam
);
3639 den_s
.SetLength(dim
);
3641 den_p
.SetLength(dim
);
3643 den
.SetDims(dim
, nparam
);
3649 gen_fun
* gf
= new gen_fun
;
3651 rays
.SetDims(dim
, nvar
);
3653 for (int j
= 0; j
< P
->NbRays
; ++j
) {
3654 if (!value_pos_p(P
->Ray
[j
][dim
+1]))
3657 for (Polyhedron
*i
= vcone
[j
]; i
; i
= i
->next
, ++f
) {
3658 lattice_point(P
->Ray
[j
]+1, i
, vertex
);
3661 for ( ; k
< nvar
; ++k
)
3662 num_s
+= vertex
[k
] * lambda
[k
];
3663 for ( ; k
< dim
; ++k
)
3664 num_p
[k
-nvar
] = vertex
[k
];
3667 add_rays(rays
, i
, &r
, nvar
, true);
3668 for (r
= 0; r
< dim
; ++r
)
3669 values2zz(i
->Ray
[r
]+1+nvar
, den
[r
], nparam
);
3670 den_s
= rays
* lambda
;
3672 normalize(sign
[f
], num_s
, num_p
, den_s
, den_p
, den
);
3676 for (int k
= 0; k
< dim
; ++k
) {
3679 else if (den_s
[k
] == 0)
3682 if (no_param
== 0) {
3683 for (int k
= 0; k
< dim
; ++k
)
3686 gf
->add(sign
[f
], one
, num_p
, den
);
3687 } else if (no_param
+ only_param
== dim
) {
3690 pden
.SetDims(only_param
, nparam
);
3692 for (k
= 0, l
= 0; k
< dim
; ++k
)
3696 for (k
= 0; k
< dim
; ++k
)
3700 dpoly
n(no_param
, num_s
);
3701 dpoly
d(no_param
, den_s
[k
], 1);
3702 for ( ; k
< dim
; ++k
)
3703 if (den_s
[k
] != 0) {
3704 dpoly
fact(no_param
, den_s
[k
], 1);
3708 mpq_set_si(count
, 0, 1);
3709 n
.div(d
, count
, sign
[f
]);
3712 value2zz(mpq_numref(count
), qn
);
3713 value2zz(mpq_denref(count
), qd
);
3715 gf
->add(qn
, qd
, num_p
, pden
);
3720 pden
.SetDims(only_param
, nparam
);
3722 for (k
= 0, l
= 0; k
< dim
; ++k
)
3726 for (k
= 0; k
< dim
; ++k
)
3730 dpoly
n(no_param
, num_s
);
3731 dpoly
d(no_param
, den_s
[k
], 1);
3732 for ( ; k
< dim
; ++k
)
3733 if (den_p
[k
] == 0) {
3734 dpoly
fact(no_param
, den_s
[k
], 1);
3738 for (k
= 0; k
< dim
; ++k
) {
3739 if (den_s
[k
] == 0 || den_p
[k
] == 0)
3742 dpoly
pd(no_param
-1, den_s
[k
], 1);
3743 int s
= den_p
[k
] < 0 ? -1 : 1;
3746 r
= new dpoly_r(n
, pd
, k
, s
, dim
);
3748 assert(0); // for now
3751 r
->div(d
, sign
[f
], gf
, pden
, den
, num_p
);
3755 cout << "sign: " << sign[f];
3756 cout << "num_s: " << num_s;
3757 cout << "num_p: " << num_p;
3758 cout << "den_s: " << den_s;
3759 cout << "den_p: " << den_p;
3760 cout << "den: " << den;
3761 cout << "only_param: " << only_param;
3762 cout << "no_param: " << no_param;