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 static 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
{
386 vector
< dpoly_r_term
* > *c
;
390 void add_term(int i
, int * powers
, ZZ
& coeff
) {
391 for (int k
= 0; k
< c
[i
].size(); ++k
) {
392 if (memcmp(c
[i
][k
]->powers
, powers
, dim
* sizeof(int)) == 0) {
393 c
[i
][k
]->coeff
+= coeff
;
397 dpoly_r_term
*t
= new dpoly_r_term
;
398 t
->powers
= new int[dim
];
399 memcpy(t
->powers
, powers
, dim
* sizeof(int));
403 dpoly_r(int len
, int dim
) {
406 c
= new vector
< dpoly_r_term
* > [len
];
408 dpoly_r(dpoly
& num
, dpoly
& den
, int pos
, int sign
, int dim
) {
409 len
= num
.coeff
.length();
410 c
= new vector
< dpoly_r_term
* > [len
];
414 for (int i
= 0; i
< len
; ++i
) {
415 ZZ coeff
= num
.coeff
[i
];
416 memset(powers
, 0, dim
* sizeof(int));
419 add_term(i
, powers
, coeff
);
421 for (int j
= 1; j
<= i
; ++j
) {
422 for (int k
= 0; k
< c
[i
-j
].size(); ++k
) {
423 memcpy(powers
, c
[i
-j
][k
]->powers
, dim
*sizeof(int));
425 coeff
= -den
.coeff
[j
-1] * c
[i
-j
][k
]->coeff
;
426 add_term(i
, powers
, coeff
);
432 void div(dpoly
& d
, ZZ
& sign
, gen_fun
*gf
, mat_ZZ
& pden
, mat_ZZ
& den
,
434 dpoly_r
rc(len
, dim
);
435 ZZ max_d
= power(d
.coeff
[0], len
+1);
439 for (int i
= 0; i
< len
; ++i
) {
442 for (int k
= 0; k
< c
[i
].size(); ++k
) {
443 coeff
= c
[i
][k
]->coeff
* cur_d
;
444 rc
.add_term(i
, c
[i
][k
]->powers
, coeff
);
447 for (int j
= 1; j
<= i
; ++j
) {
448 for (int k
= 0; k
< rc
.c
[i
-j
].size(); ++k
) {
449 coeff
= - d
.coeff
[j
] * rc
.c
[i
-j
][k
]->coeff
/ d
.coeff
[0];
450 rc
.add_term(i
, rc
.c
[i
-j
][k
]->powers
, coeff
);
455 int common
= pden
.NumRows();
457 vector
< dpoly_r_term
* >& final
= rc
.c
[len
-1];
459 for (int j
= 0; j
< final
.size(); ++j
) {
461 pden
.SetDims(rows
, pden
.NumCols());
462 for (int k
= 0; k
< dim
; ++k
) {
463 int n
= final
[j
]->powers
[k
];
466 int abs_n
= n
< 0 ? -n
: n
;
467 pden
.SetDims(rows
+abs_n
, pden
.NumCols());
468 for (int l
= 0; l
< abs_n
; ++l
) {
470 pden
[rows
+l
] = den
[k
];
472 pden
[rows
+l
] = -den
[k
];
476 gf
->add(final
[j
]->coeff
, max_d
, num_p
, pden
);
480 for (int i
= 0; i
< len
; ++i
) {
483 cout
<< c
[i
].size() << endl
;
484 for (int j
= 0; j
< c
[i
].size(); ++j
) {
485 for (int k
= 0; k
< dim
; ++k
) {
486 cout
<< c
[i
][j
]->powers
[k
] << " ";
488 cout
<< ": " << c
[i
][j
]->coeff
<< endl
;
496 void decompose(Polyhedron
*C
);
497 virtual void handle(Polyhedron
*P
, int sign
) = 0;
500 struct polar_decomposer
: public decomposer
{
501 void decompose(Polyhedron
*C
, unsigned MaxRays
);
502 virtual void handle(Polyhedron
*P
, int sign
);
503 virtual void handle_polar(Polyhedron
*P
, int sign
) = 0;
506 void decomposer::decompose(Polyhedron
*C
)
508 vector
<cone
*> nonuni
;
509 cone
* c
= new cone(C
);
520 while (!nonuni
.empty()) {
523 Vector
* v
= c
->short_vector(lambda
);
524 for (int i
= 0; i
< c
->Rays
->NbRows
- 1; ++i
) {
527 Matrix
* M
= Matrix_Copy(c
->Rays
);
528 Vector_Copy(v
->p
, M
->p
[i
], v
->Size
);
529 cone
* pc
= new cone(M
);
530 assert (pc
->det
!= 0);
531 if (abs(pc
->det
) > 1) {
532 assert(abs(pc
->det
) < abs(c
->det
));
533 nonuni
.push_back(pc
);
535 handle(pc
->poly(), sign(pc
->det
) * s
);
545 void polar_decomposer::decompose(Polyhedron
*cone
, unsigned MaxRays
)
547 Polyhedron_Polarize(cone
);
548 if (cone
->NbRays
- 1 != cone
->Dimension
) {
549 Polyhedron
*tmp
= cone
;
550 cone
= triangularize_cone(cone
, MaxRays
);
551 Polyhedron_Free(tmp
);
553 for (Polyhedron
*Polar
= cone
; Polar
; Polar
= Polar
->next
)
554 decomposer::decompose(Polar
);
558 void polar_decomposer::handle(Polyhedron
*P
, int sign
)
560 Polyhedron_Polarize(P
);
561 handle_polar(P
, sign
);
565 * Barvinok's Decomposition of a simplicial cone
567 * Returns two lists of polyhedra
569 void barvinok_decompose(Polyhedron
*C
, Polyhedron
**ppos
, Polyhedron
**pneg
)
571 Polyhedron
*pos
= *ppos
, *neg
= *pneg
;
572 vector
<cone
*> nonuni
;
573 cone
* c
= new cone(C
);
580 Polyhedron
*p
= Polyhedron_Copy(c
->Cone
);
586 while (!nonuni
.empty()) {
589 Vector
* v
= c
->short_vector(lambda
);
590 for (int i
= 0; i
< c
->Rays
->NbRows
- 1; ++i
) {
593 Matrix
* M
= Matrix_Copy(c
->Rays
);
594 Vector_Copy(v
->p
, M
->p
[i
], v
->Size
);
595 cone
* pc
= new cone(M
);
596 assert (pc
->det
!= 0);
597 if (abs(pc
->det
) > 1) {
598 assert(abs(pc
->det
) < abs(c
->det
));
599 nonuni
.push_back(pc
);
601 Polyhedron
*p
= pc
->poly();
603 if (sign(pc
->det
) == s
) {
622 * Returns a single list of npos "positive" cones followed by nneg
624 * The input cone is freed
626 void decompose(Polyhedron
*cone
, Polyhedron
**parts
, int *npos
, int *nneg
, unsigned MaxRays
)
628 Polyhedron_Polarize(cone
);
629 if (cone
->NbRays
- 1 != cone
->Dimension
) {
630 Polyhedron
*tmp
= cone
;
631 cone
= triangularize_cone(cone
, MaxRays
);
632 Polyhedron_Free(tmp
);
634 Polyhedron
*polpos
= NULL
, *polneg
= NULL
;
635 *npos
= 0; *nneg
= 0;
636 for (Polyhedron
*Polar
= cone
; Polar
; Polar
= Polar
->next
)
637 barvinok_decompose(Polar
, &polpos
, &polneg
);
640 for (Polyhedron
*i
= polpos
; i
; i
= i
->next
) {
641 Polyhedron_Polarize(i
);
645 for (Polyhedron
*i
= polneg
; i
; i
= i
->next
) {
646 Polyhedron_Polarize(i
);
657 const int MAX_TRY
=10;
659 * Searches for a vector that is not orthogonal to any
660 * of the rays in rays.
662 static void nonorthog(mat_ZZ
& rays
, vec_ZZ
& lambda
)
664 int dim
= rays
.NumCols();
666 lambda
.SetLength(dim
);
670 for (int i
= 2; !found
&& i
<= 50*dim
; i
+=4) {
671 for (int j
= 0; j
< MAX_TRY
; ++j
) {
672 for (int k
= 0; k
< dim
; ++k
) {
673 int r
= random_int(i
)+2;
674 int v
= (2*(r
%2)-1) * (r
>> 1);
678 for (; k
< rays
.NumRows(); ++k
)
679 if (lambda
* rays
[k
] == 0)
681 if (k
== rays
.NumRows()) {
690 static void randomvector(Polyhedron
*P
, vec_ZZ
& lambda
, int nvar
)
694 unsigned int dim
= P
->Dimension
;
697 for (int i
= 0; i
< P
->NbRays
; ++i
) {
698 for (int j
= 1; j
<= dim
; ++j
) {
699 value_absolute(tmp
, P
->Ray
[i
][j
]);
700 int t
= VALUE_TO_LONG(tmp
) * 16;
705 for (int i
= 0; i
< P
->NbConstraints
; ++i
) {
706 for (int j
= 1; j
<= dim
; ++j
) {
707 value_absolute(tmp
, P
->Constraint
[i
][j
]);
708 int t
= VALUE_TO_LONG(tmp
) * 16;
715 lambda
.SetLength(nvar
);
716 for (int k
= 0; k
< nvar
; ++k
) {
717 int r
= random_int(max
*dim
)+2;
718 int v
= (2*(r
%2)-1) * (max
/2*dim
+ (r
>> 1));
723 static void add_rays(mat_ZZ
& rays
, Polyhedron
*i
, int *r
, int nvar
= -1,
726 unsigned dim
= i
->Dimension
;
729 for (int k
= 0; k
< i
->NbRays
; ++k
) {
730 if (!value_zero_p(i
->Ray
[k
][dim
+1]))
732 if (!all
&& nvar
!= dim
&& First_Non_Zero(i
->Ray
[k
]+1, nvar
) == -1)
734 values2zz(i
->Ray
[k
]+1, rays
[(*r
)++], nvar
);
738 void lattice_point(Value
* values
, Polyhedron
*i
, vec_ZZ
& vertex
)
740 unsigned dim
= i
->Dimension
;
741 if(!value_one_p(values
[dim
])) {
742 Matrix
* Rays
= rays(i
);
743 Matrix
*inv
= Matrix_Alloc(Rays
->NbRows
, Rays
->NbColumns
);
744 int ok
= Matrix_Inverse(Rays
, inv
);
748 Vector
*lambda
= Vector_Alloc(dim
+1);
749 Vector_Matrix_Product(values
, inv
, lambda
->p
);
751 for (int j
= 0; j
< dim
; ++j
)
752 mpz_cdiv_q(lambda
->p
[j
], lambda
->p
[j
], lambda
->p
[dim
]);
753 value_set_si(lambda
->p
[dim
], 1);
754 Vector
*A
= Vector_Alloc(dim
+1);
755 Vector_Matrix_Product(lambda
->p
, Rays
, A
->p
);
758 values2zz(A
->p
, vertex
, dim
);
761 values2zz(values
, vertex
, dim
);
764 static evalue
*term(int param
, ZZ
& c
, Value
*den
= NULL
)
766 evalue
*EP
= new evalue();
768 value_set_si(EP
->d
,0);
769 EP
->x
.p
= new_enode(polynomial
, 2, param
+ 1);
770 evalue_set_si(&EP
->x
.p
->arr
[0], 0, 1);
771 value_init(EP
->x
.p
->arr
[1].x
.n
);
773 value_set_si(EP
->x
.p
->arr
[1].d
, 1);
775 value_assign(EP
->x
.p
->arr
[1].d
, *den
);
776 zz2value(c
, EP
->x
.p
->arr
[1].x
.n
);
780 static void vertex_period(
781 Polyhedron
*i
, vec_ZZ
& lambda
, Matrix
*T
,
782 Value lcm
, int p
, Vector
*val
,
783 evalue
*E
, evalue
* ev
,
786 unsigned nparam
= T
->NbRows
- 1;
787 unsigned dim
= i
->Dimension
;
794 Vector
* values
= Vector_Alloc(dim
+ 1);
795 Vector_Matrix_Product(val
->p
, T
, values
->p
);
796 value_assign(values
->p
[dim
], lcm
);
797 lattice_point(values
->p
, i
, vertex
);
798 num
= vertex
* lambda
;
803 zz2value(num
, ev
->x
.n
);
804 value_assign(ev
->d
, lcm
);
811 values2zz(T
->p
[p
], vertex
, dim
);
812 nump
= vertex
* lambda
;
813 if (First_Non_Zero(val
->p
, p
) == -1) {
814 value_assign(tmp
, lcm
);
815 evalue
*ET
= term(p
, nump
, &tmp
);
817 free_evalue_refs(ET
);
821 value_assign(tmp
, lcm
);
822 if (First_Non_Zero(T
->p
[p
], dim
) != -1)
823 Vector_Gcd(T
->p
[p
], dim
, &tmp
);
825 if (value_lt(tmp
, lcm
)) {
828 value_division(tmp
, lcm
, tmp
);
829 value_set_si(ev
->d
, 0);
830 ev
->x
.p
= new_enode(periodic
, VALUE_TO_INT(tmp
), p
+1);
831 value2zz(tmp
, count
);
833 value_decrement(tmp
, tmp
);
835 ZZ new_offset
= offset
- count
* nump
;
836 value_assign(val
->p
[p
], tmp
);
837 vertex_period(i
, lambda
, T
, lcm
, p
+1, val
, E
,
838 &ev
->x
.p
->arr
[VALUE_TO_INT(tmp
)], new_offset
);
839 } while (value_pos_p(tmp
));
841 vertex_period(i
, lambda
, T
, lcm
, p
+1, val
, E
, ev
, offset
);
845 static void mask_r(Matrix
*f
, int nr
, Vector
*lcm
, int p
, Vector
*val
, evalue
*ev
)
847 unsigned nparam
= lcm
->Size
;
850 Vector
* prod
= Vector_Alloc(f
->NbRows
);
851 Matrix_Vector_Product(f
, val
->p
, prod
->p
);
853 for (int i
= 0; i
< nr
; ++i
) {
854 value_modulus(prod
->p
[i
], prod
->p
[i
], f
->p
[i
][nparam
+1]);
855 isint
&= value_zero_p(prod
->p
[i
]);
857 value_set_si(ev
->d
, 1);
859 value_set_si(ev
->x
.n
, isint
);
866 if (value_one_p(lcm
->p
[p
]))
867 mask_r(f
, nr
, lcm
, p
+1, val
, ev
);
869 value_assign(tmp
, lcm
->p
[p
]);
870 value_set_si(ev
->d
, 0);
871 ev
->x
.p
= new_enode(periodic
, VALUE_TO_INT(tmp
), p
+1);
873 value_decrement(tmp
, tmp
);
874 value_assign(val
->p
[p
], tmp
);
875 mask_r(f
, nr
, lcm
, p
+1, val
, &ev
->x
.p
->arr
[VALUE_TO_INT(tmp
)]);
876 } while (value_pos_p(tmp
));
881 static evalue
*multi_monom(vec_ZZ
& p
)
883 evalue
*X
= new evalue();
886 unsigned nparam
= p
.length()-1;
887 zz2value(p
[nparam
], X
->x
.n
);
888 value_set_si(X
->d
, 1);
889 for (int i
= 0; i
< nparam
; ++i
) {
892 evalue
*T
= term(i
, p
[i
]);
901 * Check whether mapping polyhedron P on the affine combination
902 * num yields a range that has a fixed quotient on integer
904 * If zero is true, then we are only interested in the quotient
905 * for the cases where the remainder is zero.
906 * Returns NULL if false and a newly allocated value if true.
908 static Value
*fixed_quotient(Polyhedron
*P
, vec_ZZ
& num
, Value d
, bool zero
)
911 int len
= num
.length();
912 Matrix
*T
= Matrix_Alloc(2, len
);
913 zz2values(num
, T
->p
[0]);
914 value_set_si(T
->p
[1][len
-1], 1);
915 Polyhedron
*I
= Polyhedron_Image(P
, T
, P
->NbConstraints
);
919 for (i
= 0; i
< I
->NbRays
; ++i
)
920 if (value_zero_p(I
->Ray
[i
][2])) {
928 int bounded
= line_minmax(I
, &min
, &max
);
932 mpz_cdiv_q(min
, min
, d
);
934 mpz_fdiv_q(min
, min
, d
);
935 mpz_fdiv_q(max
, max
, d
);
937 if (value_eq(min
, max
)) {
940 value_assign(*ret
, min
);
948 * Normalize linear expression coef modulo m
949 * Removes common factor and reduces coefficients
950 * Returns index of first non-zero coefficient or len
952 static int normal_mod(Value
*coef
, int len
, Value
*m
)
957 Vector_Gcd(coef
, len
, &gcd
);
959 Vector_AntiScale(coef
, coef
, gcd
, len
);
961 value_division(*m
, *m
, gcd
);
968 for (j
= 0; j
< len
; ++j
)
969 mpz_fdiv_r(coef
[j
], coef
[j
], *m
);
970 for (j
= 0; j
< len
; ++j
)
971 if (value_notzero_p(coef
[j
]))
978 static void mask(Matrix
*f
, evalue
*factor
)
980 int nr
= f
->NbRows
, nc
= f
->NbColumns
;
983 for (n
= 0; n
< nr
&& value_notzero_p(f
->p
[n
][nc
-1]); ++n
)
984 if (value_notone_p(f
->p
[n
][nc
-1]) &&
985 value_notmone_p(f
->p
[n
][nc
-1]))
999 value_set_si(EV
.x
.n
, 1);
1001 for (n
= 0; n
< nr
; ++n
) {
1002 value_assign(m
, f
->p
[n
][nc
-1]);
1003 if (value_one_p(m
) || value_mone_p(m
))
1006 int j
= normal_mod(f
->p
[n
], nc
-1, &m
);
1008 free_evalue_refs(factor
);
1009 value_init(factor
->d
);
1010 evalue_set_si(factor
, 0, 1);
1014 values2zz(f
->p
[n
], row
, nc
-1);
1017 if (j
< (nc
-1)-1 && row
[j
] > g
/2) {
1018 for (int k
= j
; k
< (nc
-1); ++k
)
1020 row
[k
] = g
- row
[k
];
1024 value_set_si(EP
.d
, 0);
1025 EP
.x
.p
= new_enode(relation
, 2, 0);
1026 value_clear(EP
.x
.p
->arr
[1].d
);
1027 EP
.x
.p
->arr
[1] = *factor
;
1028 evalue
*ev
= &EP
.x
.p
->arr
[0];
1029 value_set_si(ev
->d
, 0);
1030 ev
->x
.p
= new_enode(fractional
, 3, -1);
1031 evalue_set_si(&ev
->x
.p
->arr
[1], 0, 1);
1032 evalue_set_si(&ev
->x
.p
->arr
[2], 1, 1);
1033 evalue
*E
= multi_monom(row
);
1034 value_assign(EV
.d
, m
);
1036 value_clear(ev
->x
.p
->arr
[0].d
);
1037 ev
->x
.p
->arr
[0] = *E
;
1043 free_evalue_refs(&EV
);
1049 static void mask(Matrix
*f
, evalue
*factor
)
1051 int nr
= f
->NbRows
, nc
= f
->NbColumns
;
1054 for (n
= 0; n
< nr
&& value_notzero_p(f
->p
[n
][nc
-1]); ++n
)
1055 if (value_notone_p(f
->p
[n
][nc
-1]) &&
1056 value_notmone_p(f
->p
[n
][nc
-1]))
1064 unsigned np
= nc
- 2;
1065 Vector
*lcm
= Vector_Alloc(np
);
1066 Vector
*val
= Vector_Alloc(nc
);
1067 Vector_Set(val
->p
, 0, nc
);
1068 value_set_si(val
->p
[np
], 1);
1069 Vector_Set(lcm
->p
, 1, np
);
1070 for (n
= 0; n
< nr
; ++n
) {
1071 if (value_one_p(f
->p
[n
][nc
-1]) ||
1072 value_mone_p(f
->p
[n
][nc
-1]))
1074 for (int j
= 0; j
< np
; ++j
)
1075 if (value_notzero_p(f
->p
[n
][j
])) {
1076 Gcd(f
->p
[n
][j
], f
->p
[n
][nc
-1], &tmp
);
1077 value_division(tmp
, f
->p
[n
][nc
-1], tmp
);
1078 value_lcm(tmp
, lcm
->p
[j
], &lcm
->p
[j
]);
1083 mask_r(f
, nr
, lcm
, 0, val
, &EP
);
1088 free_evalue_refs(&EP
);
1099 static bool mod_needed(Polyhedron
*PD
, vec_ZZ
& num
, Value d
, evalue
*E
)
1101 Value
*q
= fixed_quotient(PD
, num
, d
, false);
1106 value_oppose(*q
, *q
);
1109 value_set_si(EV
.d
, 1);
1111 value_multiply(EV
.x
.n
, *q
, d
);
1113 free_evalue_refs(&EV
);
1119 static void ceil_mod(Value
*coef
, int len
, Value d
, ZZ
& f
, evalue
*EP
, Polyhedron
*PD
)
1123 value_set_si(m
, -1);
1125 Vector_Scale(coef
, coef
, m
, len
);
1128 int j
= normal_mod(coef
, len
, &m
);
1136 values2zz(coef
, num
, len
);
1143 evalue_set_si(&tmp
, 0, 1);
1147 while (j
< len
-1 && (num
[j
] == g
/2 || num
[j
] == 0))
1149 if ((j
< len
-1 && num
[j
] > g
/2) || (j
== len
-1 && num
[j
] >= (g
+1)/2)) {
1150 for (int k
= j
; k
< len
-1; ++k
)
1152 num
[k
] = g
- num
[k
];
1153 num
[len
-1] = g
- 1 - num
[len
-1];
1154 value_assign(tmp
.d
, m
);
1156 zz2value(t
, tmp
.x
.n
);
1162 ZZ t
= num
[len
-1] * f
;
1163 zz2value(t
, tmp
.x
.n
);
1164 value_assign(tmp
.d
, m
);
1167 evalue
*E
= multi_monom(num
);
1171 if (PD
&& !mod_needed(PD
, num
, m
, E
)) {
1173 zz2value(f
, EV
.x
.n
);
1174 value_assign(EV
.d
, m
);
1179 value_set_si(EV
.x
.n
, 1);
1180 value_assign(EV
.d
, m
);
1182 value_clear(EV
.x
.n
);
1183 value_set_si(EV
.d
, 0);
1184 EV
.x
.p
= new_enode(fractional
, 3, -1);
1185 evalue_copy(&EV
.x
.p
->arr
[0], E
);
1186 evalue_set_si(&EV
.x
.p
->arr
[1], 0, 1);
1187 value_init(EV
.x
.p
->arr
[2].x
.n
);
1188 zz2value(f
, EV
.x
.p
->arr
[2].x
.n
);
1189 value_set_si(EV
.x
.p
->arr
[2].d
, 1);
1194 free_evalue_refs(&EV
);
1195 free_evalue_refs(E
);
1199 free_evalue_refs(&tmp
);
1205 evalue
* bv_ceil3(Value
*coef
, int len
, Value d
, Polyhedron
*P
)
1207 Vector
*val
= Vector_Alloc(len
);
1211 value_set_si(t
, -1);
1212 Vector_Scale(coef
, val
->p
, t
, len
);
1213 value_absolute(t
, d
);
1216 values2zz(val
->p
, num
, len
);
1217 evalue
*EP
= multi_monom(num
);
1221 value_init(tmp
.x
.n
);
1222 value_set_si(tmp
.x
.n
, 1);
1223 value_assign(tmp
.d
, t
);
1229 ceil_mod(val
->p
, len
, t
, one
, EP
, P
);
1232 /* copy EP to malloc'ed evalue */
1238 free_evalue_refs(&tmp
);
1245 evalue
* lattice_point(
1246 Polyhedron
*i
, vec_ZZ
& lambda
, Matrix
*W
, Value lcm
, Polyhedron
*PD
)
1248 unsigned nparam
= W
->NbColumns
- 1;
1250 Matrix
* Rays
= rays2(i
);
1251 Matrix
*T
= Transpose(Rays
);
1252 Matrix
*T2
= Matrix_Copy(T
);
1253 Matrix
*inv
= Matrix_Alloc(T2
->NbRows
, T2
->NbColumns
);
1254 int ok
= Matrix_Inverse(T2
, inv
);
1259 matrix2zz(W
, vertex
, W
->NbRows
, W
->NbColumns
);
1262 num
= lambda
* vertex
;
1264 evalue
*EP
= multi_monom(num
);
1268 value_init(tmp
.x
.n
);
1269 value_set_si(tmp
.x
.n
, 1);
1270 value_assign(tmp
.d
, lcm
);
1274 Matrix
*L
= Matrix_Alloc(inv
->NbRows
, W
->NbColumns
);
1275 Matrix_Product(inv
, W
, L
);
1278 matrix2zz(T
, RT
, T
->NbRows
, T
->NbColumns
);
1281 vec_ZZ p
= lambda
* RT
;
1283 for (int i
= 0; i
< L
->NbRows
; ++i
) {
1284 ceil_mod(L
->p
[i
], nparam
+1, lcm
, p
[i
], EP
, PD
);
1290 free_evalue_refs(&tmp
);
1294 evalue
* lattice_point(
1295 Polyhedron
*i
, vec_ZZ
& lambda
, Matrix
*W
, Value lcm
, Polyhedron
*PD
)
1297 Matrix
*T
= Transpose(W
);
1298 unsigned nparam
= T
->NbRows
- 1;
1300 evalue
*EP
= new evalue();
1302 evalue_set_si(EP
, 0, 1);
1305 Vector
*val
= Vector_Alloc(nparam
+1);
1306 value_set_si(val
->p
[nparam
], 1);
1307 ZZ
offset(INIT_VAL
, 0);
1309 vertex_period(i
, lambda
, T
, lcm
, 0, val
, EP
, &ev
, offset
);
1312 free_evalue_refs(&ev
);
1323 Param_Vertices
* V
, Polyhedron
*i
, vec_ZZ
& lambda
, term_info
* term
,
1326 unsigned nparam
= V
->Vertex
->NbColumns
- 2;
1327 unsigned dim
= i
->Dimension
;
1329 vertex
.SetDims(V
->Vertex
->NbRows
, nparam
+1);
1333 value_set_si(lcm
, 1);
1334 for (int j
= 0; j
< V
->Vertex
->NbRows
; ++j
) {
1335 value_lcm(lcm
, V
->Vertex
->p
[j
][nparam
+1], &lcm
);
1337 if (value_notone_p(lcm
)) {
1338 Matrix
* mv
= Matrix_Alloc(dim
, nparam
+1);
1339 for (int j
= 0 ; j
< dim
; ++j
) {
1340 value_division(tmp
, lcm
, V
->Vertex
->p
[j
][nparam
+1]);
1341 Vector_Scale(V
->Vertex
->p
[j
], mv
->p
[j
], tmp
, nparam
+1);
1344 term
->E
= lattice_point(i
, lambda
, mv
, lcm
, PD
);
1352 for (int i
= 0; i
< V
->Vertex
->NbRows
; ++i
) {
1353 assert(value_one_p(V
->Vertex
->p
[i
][nparam
+1])); // for now
1354 values2zz(V
->Vertex
->p
[i
], vertex
[i
], nparam
+1);
1358 num
= lambda
* vertex
;
1362 for (int j
= 0; j
< nparam
; ++j
)
1368 term
->E
= multi_monom(num
);
1372 term
->constant
= num
[nparam
];
1375 term
->coeff
= num
[p
];
1382 void normalize(Polyhedron
*i
, vec_ZZ
& lambda
, ZZ
& sign
, ZZ
& num
, vec_ZZ
& den
)
1384 unsigned dim
= i
->Dimension
;
1388 rays
.SetDims(dim
, dim
);
1389 add_rays(rays
, i
, &r
);
1390 den
= rays
* lambda
;
1393 for (int j
= 0; j
< den
.length(); ++j
) {
1397 den
[j
] = abs(den
[j
]);
1405 struct counter
: public polar_decomposer
{
1417 counter(Polyhedron
*P
) {
1420 randomvector(P
, lambda
, dim
);
1421 rays
.SetDims(dim
, dim
);
1426 void start(unsigned MaxRays
);
1432 virtual void handle_polar(Polyhedron
*P
, int sign
);
1435 void counter::handle_polar(Polyhedron
*C
, int s
)
1438 assert(C
->NbRays
-1 == dim
);
1439 add_rays(rays
, C
, &r
);
1440 for (int k
= 0; k
< dim
; ++k
) {
1441 assert(lambda
* rays
[k
] != 0);
1446 lattice_point(P
->Ray
[j
]+1, C
, vertex
);
1447 num
= vertex
* lambda
;
1448 normalize(C
, lambda
, sign
, num
, den
);
1451 dpoly
n(dim
, den
[0], 1);
1452 for (int k
= 1; k
< dim
; ++k
) {
1453 dpoly
fact(dim
, den
[k
], 1);
1456 d
.div(n
, count
, sign
);
1459 void counter::start(unsigned MaxRays
)
1461 for (j
= 0; j
< P
->NbRays
; ++j
) {
1462 Polyhedron
*C
= supporting_cone(P
, j
);
1463 decompose(C
, MaxRays
);
1467 typedef Polyhedron
* Polyhedron_p
;
1469 void barvinok_count(Polyhedron
*P
, Value
* result
, unsigned NbMaxCons
)
1471 Polyhedron
** vcone
;
1480 value_set_si(*result
, 0);
1484 for (; r
< P
->NbRays
; ++r
)
1485 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1]))
1487 if (P
->NbBid
!=0 || r
< P
->NbRays
) {
1488 value_set_si(*result
, -1);
1492 P
= remove_equalities(P
);
1495 value_set_si(*result
, 0);
1501 value_set_si(factor
, 1);
1502 Q
= Polyhedron_Reduce(P
, &factor
);
1509 if (P
->Dimension
== 0) {
1510 value_assign(*result
, factor
);
1513 value_clear(factor
);
1518 cnt
.start(NbMaxCons
);
1520 assert(value_one_p(&cnt
.count
[0]._mp_den
));
1521 value_multiply(*result
, &cnt
.count
[0]._mp_num
, factor
);
1525 value_clear(factor
);
1528 static void uni_polynom(int param
, Vector
*c
, evalue
*EP
)
1530 unsigned dim
= c
->Size
-2;
1532 value_set_si(EP
->d
,0);
1533 EP
->x
.p
= new_enode(polynomial
, dim
+1, param
+1);
1534 for (int j
= 0; j
<= dim
; ++j
)
1535 evalue_set(&EP
->x
.p
->arr
[j
], c
->p
[j
], c
->p
[dim
+1]);
1538 static void multi_polynom(Vector
*c
, evalue
* X
, evalue
*EP
)
1540 unsigned dim
= c
->Size
-2;
1544 evalue_set(&EC
, c
->p
[dim
], c
->p
[dim
+1]);
1547 evalue_set(EP
, c
->p
[dim
], c
->p
[dim
+1]);
1549 for (int i
= dim
-1; i
>= 0; --i
) {
1551 value_assign(EC
.x
.n
, c
->p
[i
]);
1554 free_evalue_refs(&EC
);
1557 Polyhedron
*unfringe (Polyhedron
*P
, unsigned MaxRays
)
1559 int len
= P
->Dimension
+2;
1560 Polyhedron
*T
, *R
= P
;
1563 Vector
*row
= Vector_Alloc(len
);
1564 value_set_si(row
->p
[0], 1);
1566 R
= DomainConstraintSimplify(Polyhedron_Copy(P
), MaxRays
);
1568 Matrix
*M
= Matrix_Alloc(2, len
-1);
1569 value_set_si(M
->p
[1][len
-2], 1);
1570 for (int v
= 0; v
< P
->Dimension
; ++v
) {
1571 value_set_si(M
->p
[0][v
], 1);
1572 Polyhedron
*I
= Polyhedron_Image(P
, M
, 2+1);
1573 value_set_si(M
->p
[0][v
], 0);
1574 for (int r
= 0; r
< I
->NbConstraints
; ++r
) {
1575 if (value_zero_p(I
->Constraint
[r
][0]))
1577 if (value_zero_p(I
->Constraint
[r
][1]))
1579 if (value_one_p(I
->Constraint
[r
][1]))
1581 if (value_mone_p(I
->Constraint
[r
][1]))
1583 value_absolute(g
, I
->Constraint
[r
][1]);
1584 Vector_Set(row
->p
+1, 0, len
-2);
1585 value_division(row
->p
[1+v
], I
->Constraint
[r
][1], g
);
1586 mpz_fdiv_q(row
->p
[len
-1], I
->Constraint
[r
][2], g
);
1588 R
= AddConstraints(row
->p
, 1, R
, MaxRays
);
1600 static Polyhedron
*reduce_domain(Polyhedron
*D
, Matrix
*CT
, Polyhedron
*CEq
,
1601 Polyhedron
**fVD
, int nd
, unsigned MaxRays
)
1606 Dt
= CT
? DomainPreimage(D
, CT
, MaxRays
) : D
;
1607 Polyhedron
*rVD
= DomainIntersection(Dt
, CEq
, MaxRays
);
1609 /* if rVD is empty or too small in geometric dimension */
1610 if(!rVD
|| emptyQ(rVD
) ||
1611 (rVD
->Dimension
-rVD
->NbEq
< Dt
->Dimension
-Dt
->NbEq
-CEq
->NbEq
)) {
1616 return 0; /* empty validity domain */
1622 fVD
[nd
] = Domain_Copy(rVD
);
1623 for (int i
= 0 ; i
< nd
; ++i
) {
1624 Polyhedron
*I
= DomainIntersection(fVD
[nd
], fVD
[i
], MaxRays
);
1629 Polyhedron
*F
= DomainSimplify(I
, fVD
[nd
], MaxRays
);
1631 Polyhedron
*T
= rVD
;
1632 rVD
= DomainDifference(rVD
, F
, MaxRays
);
1639 rVD
= DomainConstraintSimplify(rVD
, MaxRays
);
1641 Domain_Free(fVD
[nd
]);
1648 barvinok_count(rVD
, &c
, MaxRays
);
1649 if (value_zero_p(c
)) {
1658 static bool Polyhedron_is_infinite(Polyhedron
*P
, unsigned nparam
)
1661 for (r
= 0; r
< P
->NbRays
; ++r
)
1662 if (value_zero_p(P
->Ray
[r
][0]) ||
1663 value_zero_p(P
->Ray
[r
][P
->Dimension
+1])) {
1665 for (i
= P
->Dimension
- nparam
; i
< P
->Dimension
; ++i
)
1666 if (value_notzero_p(P
->Ray
[r
][i
+1]))
1668 if (i
>= P
->Dimension
)
1671 return r
< P
->NbRays
;
1674 /* Check whether all rays point in the positive directions
1675 * for the parameters
1677 static bool Polyhedron_has_positive_rays(Polyhedron
*P
, unsigned nparam
)
1680 for (r
= 0; r
< P
->NbRays
; ++r
)
1681 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1])) {
1683 for (i
= P
->Dimension
- nparam
; i
< P
->Dimension
; ++i
)
1684 if (value_neg_p(P
->Ray
[r
][i
+1]))
1690 typedef evalue
* evalue_p
;
1692 struct enumerator
: public polar_decomposer
{
1706 enumerator(Polyhedron
*P
, unsigned dim
, unsigned nbV
) {
1710 randomvector(P
, lambda
, dim
);
1711 rays
.SetDims(dim
, dim
);
1713 c
= Vector_Alloc(dim
+2);
1715 vE
= new evalue_p
[nbV
];
1716 for (int j
= 0; j
< nbV
; ++j
)
1722 void decompose_at(Param_Vertices
*V
, int _i
, unsigned MaxRays
) {
1723 Polyhedron
*C
= supporting_cone_p(P
, V
);
1727 vE
[_i
] = new evalue
;
1728 value_init(vE
[_i
]->d
);
1729 evalue_set_si(vE
[_i
], 0, 1);
1731 decompose(C
, MaxRays
);
1738 for (int j
= 0; j
< nbV
; ++j
)
1740 free_evalue_refs(vE
[j
]);
1746 virtual void handle_polar(Polyhedron
*P
, int sign
);
1749 void enumerator::handle_polar(Polyhedron
*C
, int s
)
1752 assert(C
->NbRays
-1 == dim
);
1753 add_rays(rays
, C
, &r
);
1754 for (int k
= 0; k
< dim
; ++k
) {
1755 assert(lambda
* rays
[k
] != 0);
1760 lattice_point(V
, C
, lambda
, &num
, 0);
1761 normalize(C
, lambda
, sign
, num
.constant
, den
);
1763 dpoly
n(dim
, den
[0], 1);
1764 for (int k
= 1; k
< dim
; ++k
) {
1765 dpoly
fact(dim
, den
[k
], 1);
1768 if (num
.E
!= NULL
) {
1769 ZZ
one(INIT_VAL
, 1);
1770 dpoly_n
d(dim
, num
.constant
, one
);
1773 multi_polynom(c
, num
.E
, &EV
);
1775 free_evalue_refs(&EV
);
1776 free_evalue_refs(num
.E
);
1778 } else if (num
.pos
!= -1) {
1779 dpoly_n
d(dim
, num
.constant
, num
.coeff
);
1782 uni_polynom(num
.pos
, c
, &EV
);
1784 free_evalue_refs(&EV
);
1786 mpq_set_si(count
, 0, 1);
1787 dpoly
d(dim
, num
.constant
);
1788 d
.div(n
, count
, sign
);
1791 evalue_set(&EV
, &count
[0]._mp_num
, &count
[0]._mp_den
);
1793 free_evalue_refs(&EV
);
1797 evalue
* barvinok_enumerate_ev(Polyhedron
*P
, Polyhedron
* C
, unsigned MaxRays
)
1799 //P = unfringe(P, MaxRays);
1800 Polyhedron
*CEq
= NULL
, *rVD
, *pVD
, *CA
;
1802 Param_Polyhedron
*PP
= NULL
;
1803 Param_Domain
*D
, *next
;
1806 unsigned nparam
= C
->Dimension
;
1808 ALLOC(evalue
, eres
);
1809 value_init(eres
->d
);
1810 value_set_si(eres
->d
, 0);
1813 value_init(factor
.d
);
1814 evalue_set_si(&factor
, 1, 1);
1816 CA
= align_context(C
, P
->Dimension
, MaxRays
);
1817 P
= DomainIntersection(P
, CA
, MaxRays
);
1818 Polyhedron_Free(CA
);
1820 if (C
->Dimension
== 0 || emptyQ(P
)) {
1822 eres
->x
.p
= new_enode(partition
, 2, C
->Dimension
);
1823 EVALUE_SET_DOMAIN(eres
->x
.p
->arr
[0],
1824 DomainConstraintSimplify(CEq
? CEq
: Polyhedron_Copy(C
), MaxRays
));
1825 value_set_si(eres
->x
.p
->arr
[1].d
, 1);
1826 value_init(eres
->x
.p
->arr
[1].x
.n
);
1828 value_set_si(eres
->x
.p
->arr
[1].x
.n
, 0);
1830 barvinok_count(P
, &eres
->x
.p
->arr
[1].x
.n
, MaxRays
);
1832 emul(&factor
, eres
);
1833 reduce_evalue(eres
);
1834 free_evalue_refs(&factor
);
1839 Param_Polyhedron_Free(PP
);
1843 if (Polyhedron_is_infinite(P
, nparam
))
1848 P
= remove_equalities_p(P
, P
->Dimension
-nparam
, &f
);
1852 if (P
->Dimension
== nparam
) {
1854 P
= Universe_Polyhedron(0);
1858 Polyhedron
*Q
= ParamPolyhedron_Reduce(P
, P
->Dimension
-nparam
, &factor
);
1861 if (Q
->Dimension
== nparam
) {
1863 P
= Universe_Polyhedron(0);
1868 Polyhedron
*oldP
= P
;
1869 PP
= Polyhedron2Param_SimplifiedDomain(&P
,C
,MaxRays
,&CEq
,&CT
);
1871 Polyhedron_Free(oldP
);
1873 if (isIdentity(CT
)) {
1877 assert(CT
->NbRows
!= CT
->NbColumns
);
1878 if (CT
->NbRows
== 1) // no more parameters
1880 nparam
= CT
->NbRows
- 1;
1883 unsigned dim
= P
->Dimension
- nparam
;
1885 enumerator
et(P
, dim
, PP
->nbV
);
1888 for (nd
= 0, D
=PP
->D
; D
; ++nd
, D
=D
->next
);
1889 struct section
{ Polyhedron
*D
; evalue E
; };
1890 section
*s
= new section
[nd
];
1891 Polyhedron
**fVD
= new Polyhedron_p
[nd
];
1893 for(nd
= 0, D
=PP
->D
; D
; D
=next
) {
1896 Polyhedron
*rVD
= reduce_domain(D
->Domain
, CT
, CEq
,
1901 pVD
= CT
? DomainImage(rVD
,CT
,MaxRays
) : rVD
;
1903 value_init(s
[nd
].E
.d
);
1904 evalue_set_si(&s
[nd
].E
, 0, 1);
1906 FORALL_PVertex_in_ParamPolyhedron(V
,D
,PP
) // _i is internal counter
1908 et
.decompose_at(V
, _i
, MaxRays
);
1909 eadd(et
.vE
[_i
] , &s
[nd
].E
);
1910 END_FORALL_PVertex_in_ParamPolyhedron
;
1911 reduce_in_domain(&s
[nd
].E
, pVD
);
1914 addeliminatedparams_evalue(&s
[nd
].E
, CT
);
1922 evalue_set_si(eres
, 0, 1);
1924 eres
->x
.p
= new_enode(partition
, 2*nd
, C
->Dimension
);
1925 for (int j
= 0; j
< nd
; ++j
) {
1926 EVALUE_SET_DOMAIN(eres
->x
.p
->arr
[2*j
], s
[j
].D
);
1927 value_clear(eres
->x
.p
->arr
[2*j
+1].d
);
1928 eres
->x
.p
->arr
[2*j
+1] = s
[j
].E
;
1929 Domain_Free(fVD
[j
]);
1937 Polyhedron_Free(CEq
);
1942 Enumeration
* barvinok_enumerate(Polyhedron
*P
, Polyhedron
* C
, unsigned MaxRays
)
1944 evalue
*EP
= barvinok_enumerate_ev(P
, C
, MaxRays
);
1946 return partition2enumeration(EP
);
1949 static void SwapColumns(Value
**V
, int n
, int i
, int j
)
1951 for (int r
= 0; r
< n
; ++r
)
1952 value_swap(V
[r
][i
], V
[r
][j
]);
1955 static void SwapColumns(Polyhedron
*P
, int i
, int j
)
1957 SwapColumns(P
->Constraint
, P
->NbConstraints
, i
, j
);
1958 SwapColumns(P
->Ray
, P
->NbRays
, i
, j
);
1961 static void negative_test_constraint(Value
*l
, Value
*u
, Value
*c
, int pos
,
1964 value_oppose(*v
, u
[pos
+1]);
1965 Vector_Combine(l
+1, u
+1, c
+1, *v
, l
[pos
+1], len
-1);
1966 value_multiply(*v
, *v
, l
[pos
+1]);
1967 value_substract(c
[len
-1], c
[len
-1], *v
);
1968 value_set_si(*v
, -1);
1969 Vector_Scale(c
+1, c
+1, *v
, len
-1);
1970 value_decrement(c
[len
-1], c
[len
-1]);
1971 ConstraintSimplify(c
, c
, len
, v
);
1974 static bool parallel_constraints(Value
*l
, Value
*u
, Value
*c
, int pos
,
1983 Vector_Gcd(&l
[1+pos
], len
, &g1
);
1984 Vector_Gcd(&u
[1+pos
], len
, &g2
);
1985 Vector_Combine(l
+1+pos
, u
+1+pos
, c
+1, g2
, g1
, len
);
1986 parallel
= First_Non_Zero(c
+1, len
) == -1;
1994 static void negative_test_constraint7(Value
*l
, Value
*u
, Value
*c
, int pos
,
1995 int exist
, int len
, Value
*v
)
2000 Vector_Gcd(&u
[1+pos
], exist
, v
);
2001 Vector_Gcd(&l
[1+pos
], exist
, &g
);
2002 Vector_Combine(l
+1, u
+1, c
+1, *v
, g
, len
-1);
2003 value_multiply(*v
, *v
, g
);
2004 value_substract(c
[len
-1], c
[len
-1], *v
);
2005 value_set_si(*v
, -1);
2006 Vector_Scale(c
+1, c
+1, *v
, len
-1);
2007 value_decrement(c
[len
-1], c
[len
-1]);
2008 ConstraintSimplify(c
, c
, len
, v
);
2013 static void oppose_constraint(Value
*c
, int len
, Value
*v
)
2015 value_set_si(*v
, -1);
2016 Vector_Scale(c
+1, c
+1, *v
, len
-1);
2017 value_decrement(c
[len
-1], c
[len
-1]);
2020 static bool SplitOnConstraint(Polyhedron
*P
, int i
, int l
, int u
,
2021 int nvar
, int len
, int exist
, int MaxRays
,
2022 Vector
*row
, Value
& f
, bool independent
,
2023 Polyhedron
**pos
, Polyhedron
**neg
)
2025 negative_test_constraint(P
->Constraint
[l
], P
->Constraint
[u
],
2026 row
->p
, nvar
+i
, len
, &f
);
2027 *neg
= AddConstraints(row
->p
, 1, P
, MaxRays
);
2029 /* We found an independent, but useless constraint
2030 * Maybe we should detect this earlier and not
2031 * mark the variable as INDEPENDENT
2033 if (emptyQ((*neg
))) {
2034 Polyhedron_Free(*neg
);
2038 oppose_constraint(row
->p
, len
, &f
);
2039 *pos
= AddConstraints(row
->p
, 1, P
, MaxRays
);
2041 if (emptyQ((*pos
))) {
2042 Polyhedron_Free(*neg
);
2043 Polyhedron_Free(*pos
);
2051 * unimodularly transform P such that constraint r is transformed
2052 * into a constraint that involves only a single (the first)
2053 * existential variable
2056 static Polyhedron
*rotate_along(Polyhedron
*P
, int r
, int nvar
, int exist
,
2062 Vector
*row
= Vector_Alloc(exist
);
2063 Vector_Copy(P
->Constraint
[r
]+1+nvar
, row
->p
, exist
);
2064 Vector_Gcd(row
->p
, exist
, &g
);
2065 if (value_notone_p(g
))
2066 Vector_AntiScale(row
->p
, row
->p
, g
, exist
);
2069 Matrix
*M
= unimodular_complete(row
);
2070 Matrix
*M2
= Matrix_Alloc(P
->Dimension
+1, P
->Dimension
+1);
2071 for (r
= 0; r
< nvar
; ++r
)
2072 value_set_si(M2
->p
[r
][r
], 1);
2073 for ( ; r
< nvar
+exist
; ++r
)
2074 Vector_Copy(M
->p
[r
-nvar
], M2
->p
[r
]+nvar
, exist
);
2075 for ( ; r
< P
->Dimension
+1; ++r
)
2076 value_set_si(M2
->p
[r
][r
], 1);
2077 Polyhedron
*T
= Polyhedron_Image(P
, M2
, MaxRays
);
2086 static bool SplitOnVar(Polyhedron
*P
, int i
,
2087 int nvar
, int len
, int exist
, int MaxRays
,
2088 Vector
*row
, Value
& f
, bool independent
,
2089 Polyhedron
**pos
, Polyhedron
**neg
)
2093 for (int l
= P
->NbEq
; l
< P
->NbConstraints
; ++l
) {
2094 if (value_negz_p(P
->Constraint
[l
][nvar
+i
+1]))
2098 for (j
= 0; j
< exist
; ++j
)
2099 if (j
!= i
&& value_notzero_p(P
->Constraint
[l
][nvar
+j
+1]))
2105 for (int u
= P
->NbEq
; u
< P
->NbConstraints
; ++u
) {
2106 if (value_posz_p(P
->Constraint
[u
][nvar
+i
+1]))
2110 for (j
= 0; j
< exist
; ++j
)
2111 if (j
!= i
&& value_notzero_p(P
->Constraint
[u
][nvar
+j
+1]))
2117 if (SplitOnConstraint(P
, i
, l
, u
,
2118 nvar
, len
, exist
, MaxRays
,
2119 row
, f
, independent
,
2123 SwapColumns(*neg
, nvar
+1, nvar
+1+i
);
2133 static bool double_bound_pair(Polyhedron
*P
, int nvar
, int exist
,
2134 int i
, int l1
, int l2
,
2135 Polyhedron
**pos
, Polyhedron
**neg
)
2139 Vector
*row
= Vector_Alloc(P
->Dimension
+2);
2140 value_set_si(row
->p
[0], 1);
2141 value_oppose(f
, P
->Constraint
[l1
][nvar
+i
+1]);
2142 Vector_Combine(P
->Constraint
[l1
]+1, P
->Constraint
[l2
]+1,
2144 P
->Constraint
[l2
][nvar
+i
+1], f
,
2146 ConstraintSimplify(row
->p
, row
->p
, P
->Dimension
+2, &f
);
2147 *pos
= AddConstraints(row
->p
, 1, P
, 0);
2148 value_set_si(f
, -1);
2149 Vector_Scale(row
->p
+1, row
->p
+1, f
, P
->Dimension
+1);
2150 value_decrement(row
->p
[P
->Dimension
+1], row
->p
[P
->Dimension
+1]);
2151 *neg
= AddConstraints(row
->p
, 1, P
, 0);
2155 return !emptyQ((*pos
)) && !emptyQ((*neg
));
2158 static bool double_bound(Polyhedron
*P
, int nvar
, int exist
,
2159 Polyhedron
**pos
, Polyhedron
**neg
)
2161 for (int i
= 0; i
< exist
; ++i
) {
2163 for (l1
= P
->NbEq
; l1
< P
->NbConstraints
; ++l1
) {
2164 if (value_negz_p(P
->Constraint
[l1
][nvar
+i
+1]))
2166 for (l2
= l1
+ 1; l2
< P
->NbConstraints
; ++l2
) {
2167 if (value_negz_p(P
->Constraint
[l2
][nvar
+i
+1]))
2169 if (double_bound_pair(P
, nvar
, exist
, i
, l1
, l2
, pos
, neg
))
2173 for (l1
= P
->NbEq
; l1
< P
->NbConstraints
; ++l1
) {
2174 if (value_posz_p(P
->Constraint
[l1
][nvar
+i
+1]))
2176 if (l1
< P
->NbConstraints
)
2177 for (l2
= l1
+ 1; l2
< P
->NbConstraints
; ++l2
) {
2178 if (value_posz_p(P
->Constraint
[l2
][nvar
+i
+1]))
2180 if (double_bound_pair(P
, nvar
, exist
, i
, l1
, l2
, pos
, neg
))
2192 INDEPENDENT
= 1 << 2,
2196 static evalue
* enumerate_or(Polyhedron
*D
,
2197 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
2200 fprintf(stderr
, "\nER: Or\n");
2201 #endif /* DEBUG_ER */
2203 Polyhedron
*N
= D
->next
;
2206 barvinok_enumerate_e(D
, exist
, nparam
, MaxRays
);
2209 for (D
= N
; D
; D
= N
) {
2214 barvinok_enumerate_e(D
, exist
, nparam
, MaxRays
);
2217 free_evalue_refs(EN
);
2227 static evalue
* enumerate_sum(Polyhedron
*P
,
2228 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
2230 int nvar
= P
->Dimension
- exist
- nparam
;
2231 int toswap
= nvar
< exist
? nvar
: exist
;
2232 for (int i
= 0; i
< toswap
; ++i
)
2233 SwapColumns(P
, 1 + i
, nvar
+exist
- i
);
2237 fprintf(stderr
, "\nER: Sum\n");
2238 #endif /* DEBUG_ER */
2240 evalue
*EP
= barvinok_enumerate_e(P
, exist
, nparam
, MaxRays
);
2242 for (int i
= 0; i
< /* nvar */ nparam
; ++i
) {
2243 Matrix
*C
= Matrix_Alloc(1, 1 + nparam
+ 1);
2244 value_set_si(C
->p
[0][0], 1);
2246 value_init(split
.d
);
2247 value_set_si(split
.d
, 0);
2248 split
.x
.p
= new_enode(partition
, 4, nparam
);
2249 value_set_si(C
->p
[0][1+i
], 1);
2250 Matrix
*C2
= Matrix_Copy(C
);
2251 EVALUE_SET_DOMAIN(split
.x
.p
->arr
[0],
2252 Constraints2Polyhedron(C2
, MaxRays
));
2254 evalue_set_si(&split
.x
.p
->arr
[1], 1, 1);
2255 value_set_si(C
->p
[0][1+i
], -1);
2256 value_set_si(C
->p
[0][1+nparam
], -1);
2257 EVALUE_SET_DOMAIN(split
.x
.p
->arr
[2],
2258 Constraints2Polyhedron(C
, MaxRays
));
2259 evalue_set_si(&split
.x
.p
->arr
[3], 1, 1);
2261 free_evalue_refs(&split
);
2265 evalue_range_reduction(EP
);
2267 evalue_frac2floor(EP
);
2269 evalue
*sum
= esum(EP
, nvar
);
2271 free_evalue_refs(EP
);
2275 evalue_range_reduction(EP
);
2280 static evalue
* split_sure(Polyhedron
*P
, Polyhedron
*S
,
2281 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
2283 int nvar
= P
->Dimension
- exist
- nparam
;
2285 Matrix
*M
= Matrix_Alloc(exist
, S
->Dimension
+2);
2286 for (int i
= 0; i
< exist
; ++i
)
2287 value_set_si(M
->p
[i
][nvar
+i
+1], 1);
2289 S
= DomainAddRays(S
, M
, MaxRays
);
2291 Polyhedron
*F
= DomainAddRays(P
, M
, MaxRays
);
2292 Polyhedron
*D
= DomainDifference(F
, S
, MaxRays
);
2294 D
= Disjoint_Domain(D
, 0, MaxRays
);
2299 M
= Matrix_Alloc(P
->Dimension
+1-exist
, P
->Dimension
+1);
2300 for (int j
= 0; j
< nvar
; ++j
)
2301 value_set_si(M
->p
[j
][j
], 1);
2302 for (int j
= 0; j
< nparam
+1; ++j
)
2303 value_set_si(M
->p
[nvar
+j
][nvar
+exist
+j
], 1);
2304 Polyhedron
*T
= Polyhedron_Image(S
, M
, MaxRays
);
2305 evalue
*EP
= barvinok_enumerate_e(T
, 0, nparam
, MaxRays
);
2310 for (Polyhedron
*Q
= D
; Q
; Q
= Q
->next
) {
2311 Polyhedron
*N
= Q
->next
;
2313 T
= DomainIntersection(P
, Q
, MaxRays
);
2314 evalue
*E
= barvinok_enumerate_e(T
, exist
, nparam
, MaxRays
);
2316 free_evalue_refs(E
);
2325 static evalue
* enumerate_sure(Polyhedron
*P
,
2326 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
2330 int nvar
= P
->Dimension
- exist
- nparam
;
2336 for (i
= 0; i
< exist
; ++i
) {
2337 Matrix
*M
= Matrix_Alloc(S
->NbConstraints
, S
->Dimension
+2);
2339 value_set_si(lcm
, 1);
2340 for (int j
= 0; j
< S
->NbConstraints
; ++j
) {
2341 if (value_negz_p(S
->Constraint
[j
][1+nvar
+i
]))
2343 if (value_one_p(S
->Constraint
[j
][1+nvar
+i
]))
2345 value_lcm(lcm
, S
->Constraint
[j
][1+nvar
+i
], &lcm
);
2348 for (int j
= 0; j
< S
->NbConstraints
; ++j
) {
2349 if (value_negz_p(S
->Constraint
[j
][1+nvar
+i
]))
2351 if (value_one_p(S
->Constraint
[j
][1+nvar
+i
]))
2353 value_division(f
, lcm
, S
->Constraint
[j
][1+nvar
+i
]);
2354 Vector_Scale(S
->Constraint
[j
], M
->p
[c
], f
, S
->Dimension
+2);
2355 value_substract(M
->p
[c
][S
->Dimension
+1],
2356 M
->p
[c
][S
->Dimension
+1],
2358 value_increment(M
->p
[c
][S
->Dimension
+1],
2359 M
->p
[c
][S
->Dimension
+1]);
2363 S
= AddConstraints(M
->p
[0], c
, S
, MaxRays
);
2378 fprintf(stderr
, "\nER: Sure\n");
2379 #endif /* DEBUG_ER */
2381 return split_sure(P
, S
, exist
, nparam
, MaxRays
);
2384 static evalue
* enumerate_sure2(Polyhedron
*P
,
2385 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
2387 int nvar
= P
->Dimension
- exist
- nparam
;
2389 for (r
= 0; r
< P
->NbRays
; ++r
)
2390 if (value_one_p(P
->Ray
[r
][0]) &&
2391 value_one_p(P
->Ray
[r
][P
->Dimension
+1]))
2397 Matrix
*M
= Matrix_Alloc(nvar
+ 1 + nparam
, P
->Dimension
+2);
2398 for (int i
= 0; i
< nvar
; ++i
)
2399 value_set_si(M
->p
[i
][1+i
], 1);
2400 for (int i
= 0; i
< nparam
; ++i
)
2401 value_set_si(M
->p
[i
+nvar
][1+nvar
+exist
+i
], 1);
2402 Vector_Copy(P
->Ray
[r
]+1+nvar
, M
->p
[nvar
+nparam
]+1+nvar
, exist
);
2403 value_set_si(M
->p
[nvar
+nparam
][0], 1);
2404 value_set_si(M
->p
[nvar
+nparam
][P
->Dimension
+1], 1);
2405 Polyhedron
* F
= Rays2Polyhedron(M
, MaxRays
);
2408 Polyhedron
*I
= DomainIntersection(F
, P
, MaxRays
);
2412 fprintf(stderr
, "\nER: Sure2\n");
2413 #endif /* DEBUG_ER */
2415 return split_sure(P
, I
, exist
, nparam
, MaxRays
);
2418 static evalue
* enumerate_cyclic(Polyhedron
*P
,
2419 unsigned exist
, unsigned nparam
,
2420 evalue
* EP
, int r
, int p
, unsigned MaxRays
)
2422 int nvar
= P
->Dimension
- exist
- nparam
;
2424 /* If EP in its fractional maps only contains references
2425 * to the remainder parameter with appropriate coefficients
2426 * then we could in principle avoid adding existentially
2427 * quantified variables to the validity domains.
2428 * We'd have to replace the remainder by m { p/m }
2429 * and multiply with an appropriate factor that is one
2430 * only in the appropriate range.
2431 * This last multiplication can be avoided if EP
2432 * has a single validity domain with no (further)
2433 * constraints on the remainder parameter
2436 Matrix
*CT
= Matrix_Alloc(nparam
+1, nparam
+3);
2437 Matrix
*M
= Matrix_Alloc(1, 1+nparam
+3);
2438 for (int j
= 0; j
< nparam
; ++j
)
2440 value_set_si(CT
->p
[j
][j
], 1);
2441 value_set_si(CT
->p
[p
][nparam
+1], 1);
2442 value_set_si(CT
->p
[nparam
][nparam
+2], 1);
2443 value_set_si(M
->p
[0][1+p
], -1);
2444 value_absolute(M
->p
[0][1+nparam
], P
->Ray
[0][1+nvar
+exist
+p
]);
2445 value_set_si(M
->p
[0][1+nparam
+1], 1);
2446 Polyhedron
*CEq
= Constraints2Polyhedron(M
, 1);
2448 addeliminatedparams_enum(EP
, CT
, CEq
, MaxRays
, nparam
);
2449 Polyhedron_Free(CEq
);
2455 static void enumerate_vd_add_ray(evalue
*EP
, Matrix
*Rays
, unsigned MaxRays
)
2457 if (value_notzero_p(EP
->d
))
2460 assert(EP
->x
.p
->type
== partition
);
2461 assert(EP
->x
.p
->pos
== EVALUE_DOMAIN(EP
->x
.p
->arr
[0])->Dimension
);
2462 for (int i
= 0; i
< EP
->x
.p
->size
/2; ++i
) {
2463 Polyhedron
*D
= EVALUE_DOMAIN(EP
->x
.p
->arr
[2*i
]);
2464 Polyhedron
*N
= DomainAddRays(D
, Rays
, MaxRays
);
2465 EVALUE_SET_DOMAIN(EP
->x
.p
->arr
[2*i
], N
);
2470 static evalue
* enumerate_line(Polyhedron
*P
,
2471 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
2477 fprintf(stderr
, "\nER: Line\n");
2478 #endif /* DEBUG_ER */
2480 int nvar
= P
->Dimension
- exist
- nparam
;
2482 for (i
= 0; i
< nparam
; ++i
)
2483 if (value_notzero_p(P
->Ray
[0][1+nvar
+exist
+i
]))
2486 for (j
= i
+1; j
< nparam
; ++j
)
2487 if (value_notzero_p(P
->Ray
[0][1+nvar
+exist
+i
]))
2489 assert(j
>= nparam
); // for now
2491 Matrix
*M
= Matrix_Alloc(2, P
->Dimension
+2);
2492 value_set_si(M
->p
[0][0], 1);
2493 value_set_si(M
->p
[0][1+nvar
+exist
+i
], 1);
2494 value_set_si(M
->p
[1][0], 1);
2495 value_set_si(M
->p
[1][1+nvar
+exist
+i
], -1);
2496 value_absolute(M
->p
[1][1+P
->Dimension
], P
->Ray
[0][1+nvar
+exist
+i
]);
2497 value_decrement(M
->p
[1][1+P
->Dimension
], M
->p
[1][1+P
->Dimension
]);
2498 Polyhedron
*S
= AddConstraints(M
->p
[0], 2, P
, MaxRays
);
2499 evalue
*EP
= barvinok_enumerate_e(S
, exist
, nparam
, MaxRays
);
2503 return enumerate_cyclic(P
, exist
, nparam
, EP
, 0, i
, MaxRays
);
2506 static int single_param_pos(Polyhedron
*P
, unsigned exist
, unsigned nparam
,
2509 int nvar
= P
->Dimension
- exist
- nparam
;
2510 if (First_Non_Zero(P
->Ray
[r
]+1, nvar
) != -1)
2512 int i
= First_Non_Zero(P
->Ray
[r
]+1+nvar
+exist
, nparam
);
2515 if (First_Non_Zero(P
->Ray
[r
]+1+nvar
+exist
+1, nparam
-i
-1) != -1)
2520 static evalue
* enumerate_remove_ray(Polyhedron
*P
, int r
,
2521 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
2524 fprintf(stderr
, "\nER: RedundantRay\n");
2525 #endif /* DEBUG_ER */
2529 value_set_si(one
, 1);
2530 int len
= P
->NbRays
-1;
2531 Matrix
*M
= Matrix_Alloc(2 * len
, P
->Dimension
+2);
2532 Vector_Copy(P
->Ray
[0], M
->p
[0], r
* (P
->Dimension
+2));
2533 Vector_Copy(P
->Ray
[r
+1], M
->p
[r
], (len
-r
) * (P
->Dimension
+2));
2534 for (int j
= 0; j
< P
->NbRays
; ++j
) {
2537 Vector_Combine(P
->Ray
[j
], P
->Ray
[r
], M
->p
[len
+j
-(j
>r
)],
2538 one
, P
->Ray
[j
][P
->Dimension
+1], P
->Dimension
+2);
2541 P
= Rays2Polyhedron(M
, MaxRays
);
2543 evalue
*EP
= barvinok_enumerate_e(P
, exist
, nparam
, MaxRays
);
2550 static evalue
* enumerate_redundant_ray(Polyhedron
*P
,
2551 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
2553 assert(P
->NbBid
== 0);
2554 int nvar
= P
->Dimension
- exist
- nparam
;
2558 for (int r
= 0; r
< P
->NbRays
; ++r
) {
2559 if (value_notzero_p(P
->Ray
[r
][P
->Dimension
+1]))
2561 int i1
= single_param_pos(P
, exist
, nparam
, r
);
2564 for (int r2
= r
+1; r2
< P
->NbRays
; ++r2
) {
2565 if (value_notzero_p(P
->Ray
[r2
][P
->Dimension
+1]))
2567 int i2
= single_param_pos(P
, exist
, nparam
, r2
);
2573 value_division(m
, P
->Ray
[r
][1+nvar
+exist
+i1
],
2574 P
->Ray
[r2
][1+nvar
+exist
+i1
]);
2575 value_multiply(m
, m
, P
->Ray
[r2
][1+nvar
+exist
+i1
]);
2576 /* r2 divides r => r redundant */
2577 if (value_eq(m
, P
->Ray
[r
][1+nvar
+exist
+i1
])) {
2579 return enumerate_remove_ray(P
, r
, exist
, nparam
, MaxRays
);
2582 value_division(m
, P
->Ray
[r2
][1+nvar
+exist
+i1
],
2583 P
->Ray
[r
][1+nvar
+exist
+i1
]);
2584 value_multiply(m
, m
, P
->Ray
[r
][1+nvar
+exist
+i1
]);
2585 /* r divides r2 => r2 redundant */
2586 if (value_eq(m
, P
->Ray
[r2
][1+nvar
+exist
+i1
])) {
2588 return enumerate_remove_ray(P
, r2
, exist
, nparam
, MaxRays
);
2596 static Polyhedron
*upper_bound(Polyhedron
*P
,
2597 int pos
, Value
*max
, Polyhedron
**R
)
2606 for (Polyhedron
*Q
= P
; Q
; Q
= N
) {
2608 for (r
= 0; r
< P
->NbRays
; ++r
) {
2609 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1]) &&
2610 value_pos_p(P
->Ray
[r
][1+pos
]))
2613 if (r
< P
->NbRays
) {
2621 for (r
= 0; r
< P
->NbRays
; ++r
) {
2622 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1]))
2624 mpz_fdiv_q(v
, P
->Ray
[r
][1+pos
], P
->Ray
[r
][1+P
->Dimension
]);
2625 if ((!Q
->next
&& r
== 0) || value_gt(v
, *max
))
2626 value_assign(*max
, v
);
2633 static evalue
* enumerate_ray(Polyhedron
*P
,
2634 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
2636 assert(P
->NbBid
== 0);
2637 int nvar
= P
->Dimension
- exist
- nparam
;
2640 for (r
= 0; r
< P
->NbRays
; ++r
)
2641 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1]))
2647 for (r2
= r
+1; r2
< P
->NbRays
; ++r2
)
2648 if (value_zero_p(P
->Ray
[r2
][P
->Dimension
+1]))
2650 if (r2
< P
->NbRays
) {
2652 return enumerate_sum(P
, exist
, nparam
, MaxRays
);
2656 fprintf(stderr
, "\nER: Ray\n");
2657 #endif /* DEBUG_ER */
2663 value_set_si(one
, 1);
2664 int i
= single_param_pos(P
, exist
, nparam
, r
);
2665 assert(i
!= -1); // for now;
2667 Matrix
*M
= Matrix_Alloc(P
->NbRays
, P
->Dimension
+2);
2668 for (int j
= 0; j
< P
->NbRays
; ++j
) {
2669 Vector_Combine(P
->Ray
[j
], P
->Ray
[r
], M
->p
[j
],
2670 one
, P
->Ray
[j
][P
->Dimension
+1], P
->Dimension
+2);
2672 Polyhedron
*S
= Rays2Polyhedron(M
, MaxRays
);
2674 Polyhedron
*D
= DomainDifference(P
, S
, MaxRays
);
2676 // Polyhedron_Print(stderr, P_VALUE_FMT, D);
2677 assert(value_pos_p(P
->Ray
[r
][1+nvar
+exist
+i
])); // for now
2679 D
= upper_bound(D
, nvar
+exist
+i
, &m
, &R
);
2683 M
= Matrix_Alloc(2, P
->Dimension
+2);
2684 value_set_si(M
->p
[0][0], 1);
2685 value_set_si(M
->p
[1][0], 1);
2686 value_set_si(M
->p
[0][1+nvar
+exist
+i
], -1);
2687 value_set_si(M
->p
[1][1+nvar
+exist
+i
], 1);
2688 value_assign(M
->p
[0][1+P
->Dimension
], m
);
2689 value_oppose(M
->p
[1][1+P
->Dimension
], m
);
2690 value_addto(M
->p
[1][1+P
->Dimension
], M
->p
[1][1+P
->Dimension
],
2691 P
->Ray
[r
][1+nvar
+exist
+i
]);
2692 value_decrement(M
->p
[1][1+P
->Dimension
], M
->p
[1][1+P
->Dimension
]);
2693 // Matrix_Print(stderr, P_VALUE_FMT, M);
2694 D
= AddConstraints(M
->p
[0], 2, P
, MaxRays
);
2695 // Polyhedron_Print(stderr, P_VALUE_FMT, D);
2696 value_substract(M
->p
[0][1+P
->Dimension
], M
->p
[0][1+P
->Dimension
],
2697 P
->Ray
[r
][1+nvar
+exist
+i
]);
2698 // Matrix_Print(stderr, P_VALUE_FMT, M);
2699 S
= AddConstraints(M
->p
[0], 1, P
, MaxRays
);
2700 // Polyhedron_Print(stderr, P_VALUE_FMT, S);
2703 evalue
*EP
= barvinok_enumerate_e(D
, exist
, nparam
, MaxRays
);
2708 if (value_notone_p(P
->Ray
[r
][1+nvar
+exist
+i
]))
2709 EP
= enumerate_cyclic(P
, exist
, nparam
, EP
, r
, i
, MaxRays
);
2711 M
= Matrix_Alloc(1, nparam
+2);
2712 value_set_si(M
->p
[0][0], 1);
2713 value_set_si(M
->p
[0][1+i
], 1);
2714 enumerate_vd_add_ray(EP
, M
, MaxRays
);
2719 evalue
*E
= barvinok_enumerate_e(S
, exist
, nparam
, MaxRays
);
2721 free_evalue_refs(E
);
2728 evalue
*ER
= enumerate_or(R
, exist
, nparam
, MaxRays
);
2730 free_evalue_refs(ER
);
2737 static evalue
* new_zero_ep()
2742 evalue_set_si(EP
, 0, 1);
2746 static evalue
* enumerate_vd(Polyhedron
**PA
,
2747 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
2749 Polyhedron
*P
= *PA
;
2750 int nvar
= P
->Dimension
- exist
- nparam
;
2751 Param_Polyhedron
*PP
= NULL
;
2752 Polyhedron
*C
= Universe_Polyhedron(nparam
);
2756 PP
= Polyhedron2Param_SimplifiedDomain(&PR
,C
,MaxRays
,&CEq
,&CT
);
2760 Param_Domain
*D
, *last
;
2763 for (nd
= 0, D
=PP
->D
; D
; D
=D
->next
, ++nd
)
2766 Polyhedron
**VD
= new Polyhedron_p
[nd
];
2767 Polyhedron
**fVD
= new Polyhedron_p
[nd
];
2768 for(nd
= 0, D
=PP
->D
; D
; D
=D
->next
) {
2769 Polyhedron
*rVD
= reduce_domain(D
->Domain
, CT
, CEq
,
2783 /* This doesn't seem to have any effect */
2785 Polyhedron
*CA
= align_context(VD
[0], P
->Dimension
, MaxRays
);
2787 P
= DomainIntersection(P
, CA
, MaxRays
);
2790 Polyhedron_Free(CA
);
2795 if (!EP
&& CT
->NbColumns
!= CT
->NbRows
) {
2796 Polyhedron
*CEqr
= DomainImage(CEq
, CT
, MaxRays
);
2797 Polyhedron
*CA
= align_context(CEqr
, PR
->Dimension
, MaxRays
);
2798 Polyhedron
*I
= DomainIntersection(PR
, CA
, MaxRays
);
2799 Polyhedron_Free(CEqr
);
2800 Polyhedron_Free(CA
);
2802 fprintf(stderr
, "\nER: Eliminate\n");
2803 #endif /* DEBUG_ER */
2804 nparam
-= CT
->NbColumns
- CT
->NbRows
;
2805 EP
= barvinok_enumerate_e(I
, exist
, nparam
, MaxRays
);
2806 nparam
+= CT
->NbColumns
- CT
->NbRows
;
2807 addeliminatedparams_enum(EP
, CT
, CEq
, MaxRays
, nparam
);
2811 Polyhedron_Free(PR
);
2814 if (!EP
&& nd
> 1) {
2816 fprintf(stderr
, "\nER: VD\n");
2817 #endif /* DEBUG_ER */
2818 for (int i
= 0; i
< nd
; ++i
) {
2819 Polyhedron
*CA
= align_context(VD
[i
], P
->Dimension
, MaxRays
);
2820 Polyhedron
*I
= DomainIntersection(P
, CA
, MaxRays
);
2823 EP
= barvinok_enumerate_e(I
, exist
, nparam
, MaxRays
);
2825 evalue
*E
= barvinok_enumerate_e(I
, exist
, nparam
, MaxRays
);
2827 free_evalue_refs(E
);
2831 Polyhedron_Free(CA
);
2835 for (int i
= 0; i
< nd
; ++i
) {
2836 Polyhedron_Free(VD
[i
]);
2837 Polyhedron_Free(fVD
[i
]);
2843 if (!EP
&& nvar
== 0) {
2846 Param_Vertices
*V
, *V2
;
2847 Matrix
* M
= Matrix_Alloc(1, P
->Dimension
+2);
2849 FORALL_PVertex_in_ParamPolyhedron(V
, last
, PP
) {
2851 FORALL_PVertex_in_ParamPolyhedron(V2
, last
, PP
) {
2858 for (int i
= 0; i
< exist
; ++i
) {
2859 value_oppose(f
, V
->Vertex
->p
[i
][nparam
+1]);
2860 Vector_Combine(V
->Vertex
->p
[i
],
2862 M
->p
[0] + 1 + nvar
+ exist
,
2863 V2
->Vertex
->p
[i
][nparam
+1],
2867 for (j
= 0; j
< nparam
; ++j
)
2868 if (value_notzero_p(M
->p
[0][1+nvar
+exist
+j
]))
2872 ConstraintSimplify(M
->p
[0], M
->p
[0],
2873 P
->Dimension
+2, &f
);
2874 value_set_si(M
->p
[0][0], 0);
2875 Polyhedron
*para
= AddConstraints(M
->p
[0], 1, P
,
2878 Polyhedron_Free(para
);
2881 Polyhedron
*pos
, *neg
;
2882 value_set_si(M
->p
[0][0], 1);
2883 value_decrement(M
->p
[0][P
->Dimension
+1],
2884 M
->p
[0][P
->Dimension
+1]);
2885 neg
= AddConstraints(M
->p
[0], 1, P
, MaxRays
);
2886 value_set_si(f
, -1);
2887 Vector_Scale(M
->p
[0]+1, M
->p
[0]+1, f
,
2889 value_decrement(M
->p
[0][P
->Dimension
+1],
2890 M
->p
[0][P
->Dimension
+1]);
2891 value_decrement(M
->p
[0][P
->Dimension
+1],
2892 M
->p
[0][P
->Dimension
+1]);
2893 pos
= AddConstraints(M
->p
[0], 1, P
, MaxRays
);
2894 if (emptyQ(neg
) && emptyQ(pos
)) {
2895 Polyhedron_Free(para
);
2896 Polyhedron_Free(pos
);
2897 Polyhedron_Free(neg
);
2901 fprintf(stderr
, "\nER: Order\n");
2902 #endif /* DEBUG_ER */
2903 EP
= barvinok_enumerate_e(para
, exist
, nparam
, MaxRays
);
2906 E
= barvinok_enumerate_e(pos
, exist
, nparam
, MaxRays
);
2908 free_evalue_refs(E
);
2912 E
= barvinok_enumerate_e(neg
, exist
, nparam
, MaxRays
);
2914 free_evalue_refs(E
);
2917 Polyhedron_Free(para
);
2918 Polyhedron_Free(pos
);
2919 Polyhedron_Free(neg
);
2924 } END_FORALL_PVertex_in_ParamPolyhedron
;
2927 } END_FORALL_PVertex_in_ParamPolyhedron
;
2930 /* Search for vertex coordinate to split on */
2931 /* First look for one independent of the parameters */
2932 FORALL_PVertex_in_ParamPolyhedron(V
, last
, PP
) {
2933 for (int i
= 0; i
< exist
; ++i
) {
2935 for (j
= 0; j
< nparam
; ++j
)
2936 if (value_notzero_p(V
->Vertex
->p
[i
][j
]))
2940 value_set_si(M
->p
[0][0], 1);
2941 Vector_Set(M
->p
[0]+1, 0, nvar
+exist
);
2942 Vector_Copy(V
->Vertex
->p
[i
],
2943 M
->p
[0] + 1 + nvar
+ exist
, nparam
+1);
2944 value_oppose(M
->p
[0][1+nvar
+i
],
2945 V
->Vertex
->p
[i
][nparam
+1]);
2947 Polyhedron
*pos
, *neg
;
2948 value_set_si(M
->p
[0][0], 1);
2949 value_decrement(M
->p
[0][P
->Dimension
+1],
2950 M
->p
[0][P
->Dimension
+1]);
2951 neg
= AddConstraints(M
->p
[0], 1, P
, MaxRays
);
2952 value_set_si(f
, -1);
2953 Vector_Scale(M
->p
[0]+1, M
->p
[0]+1, f
,
2955 value_decrement(M
->p
[0][P
->Dimension
+1],
2956 M
->p
[0][P
->Dimension
+1]);
2957 value_decrement(M
->p
[0][P
->Dimension
+1],
2958 M
->p
[0][P
->Dimension
+1]);
2959 pos
= AddConstraints(M
->p
[0], 1, P
, MaxRays
);
2960 if (emptyQ(neg
) || emptyQ(pos
)) {
2961 Polyhedron_Free(pos
);
2962 Polyhedron_Free(neg
);
2965 Polyhedron_Free(pos
);
2966 value_increment(M
->p
[0][P
->Dimension
+1],
2967 M
->p
[0][P
->Dimension
+1]);
2968 pos
= AddConstraints(M
->p
[0], 1, P
, MaxRays
);
2970 fprintf(stderr
, "\nER: Vertex\n");
2971 #endif /* DEBUG_ER */
2973 EP
= enumerate_or(pos
, exist
, nparam
, MaxRays
);
2978 } END_FORALL_PVertex_in_ParamPolyhedron
;
2982 /* Search for vertex coordinate to split on */
2983 /* Now look for one that depends on the parameters */
2984 FORALL_PVertex_in_ParamPolyhedron(V
, last
, PP
) {
2985 for (int i
= 0; i
< exist
; ++i
) {
2986 value_set_si(M
->p
[0][0], 1);
2987 Vector_Set(M
->p
[0]+1, 0, nvar
+exist
);
2988 Vector_Copy(V
->Vertex
->p
[i
],
2989 M
->p
[0] + 1 + nvar
+ exist
, nparam
+1);
2990 value_oppose(M
->p
[0][1+nvar
+i
],
2991 V
->Vertex
->p
[i
][nparam
+1]);
2993 Polyhedron
*pos
, *neg
;
2994 value_set_si(M
->p
[0][0], 1);
2995 value_decrement(M
->p
[0][P
->Dimension
+1],
2996 M
->p
[0][P
->Dimension
+1]);
2997 neg
= AddConstraints(M
->p
[0], 1, P
, MaxRays
);
2998 value_set_si(f
, -1);
2999 Vector_Scale(M
->p
[0]+1, M
->p
[0]+1, f
,
3001 value_decrement(M
->p
[0][P
->Dimension
+1],
3002 M
->p
[0][P
->Dimension
+1]);
3003 value_decrement(M
->p
[0][P
->Dimension
+1],
3004 M
->p
[0][P
->Dimension
+1]);
3005 pos
= AddConstraints(M
->p
[0], 1, P
, MaxRays
);
3006 if (emptyQ(neg
) || emptyQ(pos
)) {
3007 Polyhedron_Free(pos
);
3008 Polyhedron_Free(neg
);
3011 Polyhedron_Free(pos
);
3012 value_increment(M
->p
[0][P
->Dimension
+1],
3013 M
->p
[0][P
->Dimension
+1]);
3014 pos
= AddConstraints(M
->p
[0], 1, P
, MaxRays
);
3016 fprintf(stderr
, "\nER: ParamVertex\n");
3017 #endif /* DEBUG_ER */
3019 EP
= enumerate_or(pos
, exist
, nparam
, MaxRays
);
3024 } END_FORALL_PVertex_in_ParamPolyhedron
;
3032 Polyhedron_Free(CEq
);
3036 Param_Polyhedron_Free(PP
);
3043 evalue
*barvinok_enumerate_pip(Polyhedron
*P
,
3044 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
3049 evalue
*barvinok_enumerate_pip(Polyhedron
*P
,
3050 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
3052 int nvar
= P
->Dimension
- exist
- nparam
;
3053 evalue
*EP
= new_zero_ep();
3054 Polyhedron
*Q
, *N
, *T
= 0;
3060 fprintf(stderr
, "\nER: PIP\n");
3061 #endif /* DEBUG_ER */
3063 for (int i
= 0; i
< P
->Dimension
; ++i
) {
3066 bool posray
= false;
3067 bool negray
= false;
3068 value_set_si(min
, 0);
3069 for (int j
= 0; j
< P
->NbRays
; ++j
) {
3070 if (value_pos_p(P
->Ray
[j
][1+i
])) {
3072 if (value_zero_p(P
->Ray
[j
][1+P
->Dimension
]))
3074 } else if (value_neg_p(P
->Ray
[j
][1+i
])) {
3076 if (value_zero_p(P
->Ray
[j
][1+P
->Dimension
]))
3080 P
->Ray
[j
][1+i
], P
->Ray
[j
][1+P
->Dimension
]);
3081 if (value_lt(tmp
, min
))
3082 value_assign(min
, tmp
);
3087 assert(!(posray
&& negray
));
3088 assert(!negray
); // for now
3089 Polyhedron
*O
= T
? T
: P
;
3090 /* shift by a safe amount */
3091 Matrix
*M
= Matrix_Alloc(O
->NbRays
, O
->Dimension
+2);
3092 Vector_Copy(O
->Ray
[0], M
->p
[0], O
->NbRays
* (O
->Dimension
+2));
3093 for (int j
= 0; j
< P
->NbRays
; ++j
) {
3094 if (value_notzero_p(M
->p
[j
][1+P
->Dimension
])) {
3095 value_multiply(tmp
, min
, M
->p
[j
][1+P
->Dimension
]);
3096 value_substract(M
->p
[j
][1+i
], M
->p
[j
][1+i
], tmp
);
3101 T
= Rays2Polyhedron(M
, MaxRays
);
3104 /* negating a parameter requires that we substitute in the
3105 * sign again afterwards.
3108 assert(i
< nvar
+exist
);
3110 T
= Polyhedron_Copy(P
);
3111 for (int j
= 0; j
< T
->NbRays
; ++j
)
3112 value_oppose(T
->Ray
[j
][1+i
], T
->Ray
[j
][1+i
]);
3113 for (int j
= 0; j
< T
->NbConstraints
; ++j
)
3114 value_oppose(T
->Constraint
[j
][1+i
], T
->Constraint
[j
][1+i
]);
3120 Polyhedron
*D
= pip_lexmin(T
? T
: P
, exist
, nparam
);
3121 for (Q
= D
; Q
; Q
= N
) {
3125 exist
= Q
->Dimension
- nvar
- nparam
;
3126 E
= barvinok_enumerate_e(Q
, exist
, nparam
, MaxRays
);
3129 free_evalue_refs(E
);
3141 static bool is_single(Value
*row
, int pos
, int len
)
3143 return First_Non_Zero(row
, pos
) == -1 &&
3144 First_Non_Zero(row
+pos
+1, len
-pos
-1) == -1;
3147 static evalue
* barvinok_enumerate_e_r(Polyhedron
*P
,
3148 unsigned exist
, unsigned nparam
, unsigned MaxRays
);
3151 static int er_level
= 0;
3153 evalue
* barvinok_enumerate_e(Polyhedron
*P
,
3154 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
3156 fprintf(stderr
, "\nER: level %i\n", er_level
);
3157 int nvar
= P
->Dimension
- exist
- nparam
;
3158 fprintf(stderr
, "%d %d %d\n", nvar
, exist
, nparam
);
3160 Polyhedron_Print(stderr
, P_VALUE_FMT
, P
);
3162 P
= DomainConstraintSimplify(Polyhedron_Copy(P
), MaxRays
);
3163 evalue
*EP
= barvinok_enumerate_e_r(P
, exist
, nparam
, MaxRays
);
3169 evalue
* barvinok_enumerate_e(Polyhedron
*P
,
3170 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
3172 P
= DomainConstraintSimplify(Polyhedron_Copy(P
), MaxRays
);
3173 evalue
*EP
= barvinok_enumerate_e_r(P
, exist
, nparam
, MaxRays
);
3179 static evalue
* barvinok_enumerate_e_r(Polyhedron
*P
,
3180 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
3183 Polyhedron
*U
= Universe_Polyhedron(nparam
);
3184 evalue
*EP
= barvinok_enumerate_ev(P
, U
, MaxRays
);
3185 //char *param_name[] = {"P", "Q", "R", "S", "T" };
3186 //print_evalue(stdout, EP, param_name);
3191 int nvar
= P
->Dimension
- exist
- nparam
;
3192 int len
= P
->Dimension
+ 2;
3195 return new_zero_ep();
3197 if (nvar
== 0 && nparam
== 0) {
3198 evalue
*EP
= new_zero_ep();
3199 barvinok_count(P
, &EP
->x
.n
, MaxRays
);
3200 if (value_pos_p(EP
->x
.n
))
3201 value_set_si(EP
->x
.n
, 1);
3206 for (r
= 0; r
< P
->NbRays
; ++r
)
3207 if (value_zero_p(P
->Ray
[r
][0]) ||
3208 value_zero_p(P
->Ray
[r
][P
->Dimension
+1])) {
3210 for (i
= 0; i
< nvar
; ++i
)
3211 if (value_notzero_p(P
->Ray
[r
][i
+1]))
3215 for (i
= nvar
+ exist
; i
< nvar
+ exist
+ nparam
; ++i
)
3216 if (value_notzero_p(P
->Ray
[r
][i
+1]))
3218 if (i
>= nvar
+ exist
+ nparam
)
3221 if (r
< P
->NbRays
) {
3222 evalue
*EP
= new_zero_ep();
3223 value_set_si(EP
->x
.n
, -1);
3228 for (r
= 0; r
< P
->NbEq
; ++r
)
3229 if ((first
= First_Non_Zero(P
->Constraint
[r
]+1+nvar
, exist
)) != -1)
3232 if (First_Non_Zero(P
->Constraint
[r
]+1+nvar
+first
+1,
3233 exist
-first
-1) != -1) {
3234 Polyhedron
*T
= rotate_along(P
, r
, nvar
, exist
, MaxRays
);
3236 fprintf(stderr
, "\nER: Equality\n");
3237 #endif /* DEBUG_ER */
3238 evalue
*EP
= barvinok_enumerate_e(T
, exist
-1, nparam
, MaxRays
);
3243 fprintf(stderr
, "\nER: Fixed\n");
3244 #endif /* DEBUG_ER */
3246 return barvinok_enumerate_e(P
, exist
-1, nparam
, MaxRays
);
3248 Polyhedron
*T
= Polyhedron_Copy(P
);
3249 SwapColumns(T
, nvar
+1, nvar
+1+first
);
3250 evalue
*EP
= barvinok_enumerate_e(T
, exist
-1, nparam
, MaxRays
);
3257 Vector
*row
= Vector_Alloc(len
);
3258 value_set_si(row
->p
[0], 1);
3263 enum constraint
* info
= new constraint
[exist
];
3264 for (int i
= 0; i
< exist
; ++i
) {
3266 for (int l
= P
->NbEq
; l
< P
->NbConstraints
; ++l
) {
3267 if (value_negz_p(P
->Constraint
[l
][nvar
+i
+1]))
3269 bool l_parallel
= is_single(P
->Constraint
[l
]+nvar
+1, i
, exist
);
3270 for (int u
= P
->NbEq
; u
< P
->NbConstraints
; ++u
) {
3271 if (value_posz_p(P
->Constraint
[u
][nvar
+i
+1]))
3273 bool lu_parallel
= l_parallel
||
3274 is_single(P
->Constraint
[u
]+nvar
+1, i
, exist
);
3275 value_oppose(f
, P
->Constraint
[u
][nvar
+i
+1]);
3276 Vector_Combine(P
->Constraint
[l
]+1, P
->Constraint
[u
]+1, row
->p
+1,
3277 f
, P
->Constraint
[l
][nvar
+i
+1], len
-1);
3278 if (!(info
[i
] & INDEPENDENT
)) {
3280 for (j
= 0; j
< exist
; ++j
)
3281 if (j
!= i
&& value_notzero_p(row
->p
[nvar
+j
+1]))
3284 //printf("independent: i: %d, l: %d, u: %d\n", i, l, u);
3285 info
[i
] = (constraint
)(info
[i
] | INDEPENDENT
);
3288 if (info
[i
] & ALL_POS
) {
3289 value_addto(row
->p
[len
-1], row
->p
[len
-1],
3290 P
->Constraint
[l
][nvar
+i
+1]);
3291 value_addto(row
->p
[len
-1], row
->p
[len
-1], f
);
3292 value_multiply(f
, f
, P
->Constraint
[l
][nvar
+i
+1]);
3293 value_substract(row
->p
[len
-1], row
->p
[len
-1], f
);
3294 value_decrement(row
->p
[len
-1], row
->p
[len
-1]);
3295 ConstraintSimplify(row
->p
, row
->p
, len
, &f
);
3296 value_set_si(f
, -1);
3297 Vector_Scale(row
->p
+1, row
->p
+1, f
, len
-1);
3298 value_decrement(row
->p
[len
-1], row
->p
[len
-1]);
3299 Polyhedron
*T
= AddConstraints(row
->p
, 1, P
, MaxRays
);
3301 //printf("not all_pos: i: %d, l: %d, u: %d\n", i, l, u);
3302 info
[i
] = (constraint
)(info
[i
] ^ ALL_POS
);
3304 //puts("pos remainder");
3305 //Polyhedron_Print(stdout, P_VALUE_FMT, T);
3308 if (!(info
[i
] & ONE_NEG
)) {
3310 negative_test_constraint(P
->Constraint
[l
],
3312 row
->p
, nvar
+i
, len
, &f
);
3313 oppose_constraint(row
->p
, len
, &f
);
3314 Polyhedron
*T
= AddConstraints(row
->p
, 1, P
, MaxRays
);
3316 //printf("one_neg i: %d, l: %d, u: %d\n", i, l, u);
3317 info
[i
] = (constraint
)(info
[i
] | ONE_NEG
);
3319 //puts("neg remainder");
3320 //Polyhedron_Print(stdout, P_VALUE_FMT, T);
3322 } else if (!(info
[i
] & ROT_NEG
)) {
3323 if (parallel_constraints(P
->Constraint
[l
],
3325 row
->p
, nvar
, exist
)) {
3326 negative_test_constraint7(P
->Constraint
[l
],
3328 row
->p
, nvar
, exist
,
3330 oppose_constraint(row
->p
, len
, &f
);
3331 Polyhedron
*T
= AddConstraints(row
->p
, 1, P
, MaxRays
);
3333 // printf("rot_neg i: %d, l: %d, u: %d\n", i, l, u);
3334 info
[i
] = (constraint
)(info
[i
] | ROT_NEG
);
3337 //puts("neg remainder");
3338 //Polyhedron_Print(stdout, P_VALUE_FMT, T);
3343 if (!(info
[i
] & ALL_POS
) && (info
[i
] & (ONE_NEG
| ROT_NEG
)))
3347 if (info
[i
] & ALL_POS
)
3354 for (int i = 0; i < exist; ++i)
3355 printf("%i: %i\n", i, info[i]);
3357 for (int i
= 0; i
< exist
; ++i
)
3358 if (info
[i
] & ALL_POS
) {
3360 fprintf(stderr
, "\nER: Positive\n");
3361 #endif /* DEBUG_ER */
3363 // Maybe we should chew off some of the fat here
3364 Matrix
*M
= Matrix_Alloc(P
->Dimension
, P
->Dimension
+1);
3365 for (int j
= 0; j
< P
->Dimension
; ++j
)
3366 value_set_si(M
->p
[j
][j
+ (j
>= i
+nvar
)], 1);
3367 Polyhedron
*T
= Polyhedron_Image(P
, M
, MaxRays
);
3369 evalue
*EP
= barvinok_enumerate_e(T
, exist
-1, nparam
, MaxRays
);
3376 for (int i
= 0; i
< exist
; ++i
)
3377 if (info
[i
] & ONE_NEG
) {
3379 fprintf(stderr
, "\nER: Negative\n");
3380 #endif /* DEBUG_ER */
3385 return barvinok_enumerate_e(P
, exist
-1, nparam
, MaxRays
);
3387 Polyhedron
*T
= Polyhedron_Copy(P
);
3388 SwapColumns(T
, nvar
+1, nvar
+1+i
);
3389 evalue
*EP
= barvinok_enumerate_e(T
, exist
-1, nparam
, MaxRays
);
3394 for (int i
= 0; i
< exist
; ++i
)
3395 if (info
[i
] & ROT_NEG
) {
3397 fprintf(stderr
, "\nER: Rotate\n");
3398 #endif /* DEBUG_ER */
3402 Polyhedron
*T
= rotate_along(P
, r
, nvar
, exist
, MaxRays
);
3403 evalue
*EP
= barvinok_enumerate_e(T
, exist
-1, nparam
, MaxRays
);
3407 for (int i
= 0; i
< exist
; ++i
)
3408 if (info
[i
] & INDEPENDENT
) {
3409 Polyhedron
*pos
, *neg
;
3411 /* Find constraint again and split off negative part */
3413 if (SplitOnVar(P
, i
, nvar
, len
, exist
, MaxRays
,
3414 row
, f
, true, &pos
, &neg
)) {
3416 fprintf(stderr
, "\nER: Split\n");
3417 #endif /* DEBUG_ER */
3420 barvinok_enumerate_e(neg
, exist
-1, nparam
, MaxRays
);
3422 barvinok_enumerate_e(pos
, exist
, nparam
, MaxRays
);
3424 free_evalue_refs(E
);
3426 Polyhedron_Free(neg
);
3427 Polyhedron_Free(pos
);
3441 EP
= enumerate_line(P
, exist
, nparam
, MaxRays
);
3445 EP
= barvinok_enumerate_pip(P
, exist
, nparam
, MaxRays
);
3449 EP
= enumerate_redundant_ray(P
, exist
, nparam
, MaxRays
);
3453 EP
= enumerate_sure(P
, exist
, nparam
, MaxRays
);
3457 EP
= enumerate_ray(P
, exist
, nparam
, MaxRays
);
3461 EP
= enumerate_sure2(P
, exist
, nparam
, MaxRays
);
3465 F
= unfringe(P
, MaxRays
);
3466 if (!PolyhedronIncludes(F
, P
)) {
3468 fprintf(stderr
, "\nER: Fringed\n");
3469 #endif /* DEBUG_ER */
3470 EP
= barvinok_enumerate_e(F
, exist
, nparam
, MaxRays
);
3477 EP
= enumerate_vd(&P
, exist
, nparam
, MaxRays
);
3482 EP
= enumerate_sum(P
, exist
, nparam
, MaxRays
);
3489 Polyhedron
*pos
, *neg
;
3490 for (i
= 0; i
< exist
; ++i
)
3491 if (SplitOnVar(P
, i
, nvar
, len
, exist
, MaxRays
,
3492 row
, f
, false, &pos
, &neg
))
3498 EP
= enumerate_or(pos
, exist
, nparam
, MaxRays
);
3510 static void normalize(Polyhedron
*i
, vec_ZZ
& lambda
, ZZ
& sign
,
3511 ZZ
& num_s
, vec_ZZ
& num_p
, vec_ZZ
& den_s
, vec_ZZ
& den_p
,
3514 unsigned dim
= i
->Dimension
;
3515 unsigned nparam
= num_p
.length();
3516 unsigned nvar
= dim
- nparam
;
3520 rays
.SetDims(dim
, nvar
);
3521 add_rays(rays
, i
, &r
, nvar
, true);
3522 den_s
= rays
* lambda
;
3526 for (int j
= 0; j
< den_s
.length(); ++j
) {
3527 values2zz(i
->Ray
[j
]+1+nvar
, f
[j
], nparam
);
3528 if (den_s
[j
] == 0) {
3532 if (First_Non_Zero(i
->Ray
[j
]+1+nvar
, nparam
) != -1) {
3543 den_s
[j
] = abs(den_s
[j
]);
3552 gen_fun
* barvinok_series(Polyhedron
*P
, Polyhedron
* C
, unsigned MaxRays
)
3554 Polyhedron
** vcone
;
3556 unsigned nparam
= C
->Dimension
;
3560 sign
.SetLength(ncone
);
3562 CA
= align_context(C
, P
->Dimension
, MaxRays
);
3563 P
= DomainIntersection(P
, CA
, MaxRays
);
3564 Polyhedron_Free(CA
);
3566 assert(!Polyhedron_is_infinite(P
, nparam
));
3567 assert(P
->NbBid
== 0);
3568 assert(Polyhedron_has_positive_rays(P
, nparam
));
3569 assert(P
->NbEq
== 0);
3572 nvar
= dim
- nparam
;
3573 vcone
= new Polyhedron_p
[P
->NbRays
];
3575 for (int j
= 0; j
< P
->NbRays
; ++j
) {
3576 if (!value_pos_p(P
->Ray
[j
][dim
+1]))
3580 Polyhedron
*C
= supporting_cone(P
, j
);
3581 decompose(C
, &vcone
[j
], &npos
, &nneg
, MaxRays
);
3582 ncone
+= npos
+ nneg
;
3583 sign
.SetLength(ncone
);
3584 for (int k
= 0; k
< npos
; ++k
)
3585 sign
[ncone
-nneg
-k
-1] = 1;
3586 for (int k
= 0; k
< nneg
; ++k
)
3587 sign
[ncone
-k
-1] = -1;
3591 rays
.SetDims(ncone
* dim
, nvar
);
3593 for (int j
= 0; j
< P
->NbRays
; ++j
) {
3594 if (!value_pos_p(P
->Ray
[j
][dim
+1]))
3597 for (Polyhedron
*i
= vcone
[j
]; i
; i
= i
->next
) {
3598 add_rays(rays
, i
, &r
, nvar
);
3601 rays
.SetDims(r
, nvar
);
3603 nonorthog(rays
, lambda
);
3604 //randomvector(P, lambda, nvar);
3607 cout << "rays: " << rays;
3608 cout << "lambda: " << lambda;
3614 num_p
.SetLength(nparam
);
3617 den_s
.SetLength(dim
);
3619 den_p
.SetLength(dim
);
3621 den
.SetDims(dim
, nparam
);
3627 gen_fun
* gf
= new gen_fun
;
3629 for (int j
= 0; j
< P
->NbRays
; ++j
) {
3630 if (!value_pos_p(P
->Ray
[j
][dim
+1]))
3633 for (Polyhedron
*i
= vcone
[j
]; i
; i
= i
->next
, ++f
) {
3634 lattice_point(P
->Ray
[j
]+1, i
, vertex
);
3637 for ( ; k
< nvar
; ++k
)
3638 num_s
+= vertex
[k
] * lambda
[k
];
3639 for ( ; k
< dim
; ++k
)
3640 num_p
[k
-nvar
] = vertex
[k
];
3641 normalize(i
, lambda
, sign
[f
], num_s
, num_p
,
3646 for (int k
= 0; k
< dim
; ++k
) {
3649 else if (den_s
[k
] == 0)
3652 if (no_param
== 0) {
3653 for (int k
= 0; k
< dim
; ++k
)
3656 gf
->add(sign
[f
], one
, num_p
, den
);
3657 } else if (no_param
+ only_param
== dim
) {
3660 pden
.SetDims(only_param
, nparam
);
3662 for (k
= 0, l
= 0; k
< dim
; ++k
)
3666 for (k
= 0; k
< dim
; ++k
)
3670 dpoly
n(no_param
, num_s
);
3671 dpoly
d(no_param
, den_s
[k
], 1);
3672 for ( ; k
< dim
; ++k
)
3673 if (den_s
[k
] != 0) {
3674 dpoly
fact(no_param
, den_s
[k
], 1);
3678 mpq_set_si(count
, 0, 1);
3679 n
.div(d
, count
, sign
[f
]);
3682 value2zz(mpq_numref(count
), qn
);
3683 value2zz(mpq_denref(count
), qd
);
3685 gf
->add(qn
, qd
, num_p
, pden
);
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_p
[k
] == 0) {
3704 dpoly
fact(no_param
, den_s
[k
], 1);
3708 for (k
= 0; k
< dim
; ++k
) {
3709 if (den_s
[k
] == 0 || den_p
[k
] == 0)
3712 dpoly
pd(no_param
-1, den_s
[k
], 1);
3713 int s
= den_p
[k
] < 0 ? -1 : 1;
3716 r
= new dpoly_r(n
, pd
, k
, s
, dim
);
3718 assert(0); // for now
3721 r
->div(d
, sign
[f
], gf
, pden
, den
, num_p
);
3725 cout << "sign: " << sign[f];
3726 cout << "num_s: " << num_s;
3727 cout << "num_p: " << num_p;
3728 cout << "den_s: " << den_s;
3729 cout << "den_p: " << den_p;
3730 cout << "den: " << den;
3731 cout << "only_param: " << only_param;
3732 cout << "no_param: " << no_param;