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
);
189 Vector
* short_vector(vec_ZZ
& lambda
) {
190 Matrix
*M
= Matrix_Copy(Rays
);
191 Matrix
*inv
= Matrix_Alloc(M
->NbRows
, M
->NbColumns
);
192 int ok
= Matrix_Inverse(M
, inv
);
199 matrix2zz(inv
, B
, inv
->NbRows
- 1, inv
->NbColumns
- 1);
200 long r
= LLL(det2
, B
, U
);
204 for (int i
= 1; i
< B
.NumRows(); ++i
) {
216 Vector
*z
= Vector_Alloc(U
[index
].length()+1);
218 zz2values(U
[index
], z
->p
);
219 value_set_si(z
->p
[U
[index
].length()], 0);
223 Polyhedron
*C
= poly();
225 for (i
= 0; i
< C
->NbConstraints
; ++i
) {
226 Inner_Product(z
->p
, C
->Constraint
[i
]+1, z
->Size
-1, &tmp
);
227 if (value_pos_p(tmp
))
230 if (i
== C
->NbConstraints
) {
231 value_set_si(tmp
, -1);
232 Vector_Scale(z
->p
, z
->p
, tmp
, z
->Size
-1);
239 Polyhedron_Free(Cone
);
245 Matrix
*M
= Matrix_Alloc(Rays
->NbRows
+1, Rays
->NbColumns
+1);
246 for (int i
= 0; i
< Rays
->NbRows
; ++i
) {
247 Vector_Copy(Rays
->p
[i
], M
->p
[i
]+1, Rays
->NbColumns
);
248 value_set_si(M
->p
[i
][0], 1);
250 Vector_Set(M
->p
[Rays
->NbRows
]+1, 0, Rays
->NbColumns
-1);
251 value_set_si(M
->p
[Rays
->NbRows
][0], 1);
252 value_set_si(M
->p
[Rays
->NbRows
][Rays
->NbColumns
], 1);
253 Cone
= Rays2Polyhedron(M
, M
->NbRows
+1);
254 assert(Cone
->NbConstraints
== Cone
->NbRays
);
268 dpoly(int d
, ZZ
& degree
, int offset
= 0) {
269 coeff
.SetLength(d
+1);
271 int min
= d
+ offset
;
272 if (degree
>= 0 && degree
< ZZ(INIT_VAL
, min
))
273 min
= to_int(degree
);
275 ZZ c
= ZZ(INIT_VAL
, 1);
278 for (int i
= 1; i
<= min
; ++i
) {
279 c
*= (degree
-i
+ 1);
284 void operator *= (dpoly
& f
) {
285 assert(coeff
.length() == f
.coeff
.length());
287 coeff
= f
.coeff
[0] * coeff
;
288 for (int i
= 1; i
< coeff
.length(); ++i
)
289 for (int j
= 0; i
+j
< coeff
.length(); ++j
)
290 coeff
[i
+j
] += f
.coeff
[i
] * old
[j
];
292 void div(dpoly
& d
, mpq_t count
, ZZ
& sign
) {
293 int len
= coeff
.length();
296 mpq_t
* c
= new mpq_t
[coeff
.length()];
299 for (int i
= 0; i
< len
; ++i
) {
301 zz2value(coeff
[i
], tmp
);
302 mpq_set_z(c
[i
], tmp
);
304 for (int j
= 1; j
<= i
; ++j
) {
305 zz2value(d
.coeff
[j
], tmp
);
306 mpq_set_z(qtmp
, tmp
);
307 mpq_mul(qtmp
, qtmp
, c
[i
-j
]);
308 mpq_sub(c
[i
], c
[i
], qtmp
);
311 zz2value(d
.coeff
[0], tmp
);
312 mpq_set_z(qtmp
, tmp
);
313 mpq_div(c
[i
], c
[i
], qtmp
);
316 mpq_sub(count
, count
, c
[len
-1]);
318 mpq_add(count
, count
, c
[len
-1]);
322 for (int i
= 0; i
< len
; ++i
)
334 dpoly_n(int d
, ZZ
& degree_0
, ZZ
& degree_1
, int offset
= 0) {
338 zz2value(degree_0
, d0
);
339 zz2value(degree_1
, d1
);
340 coeff
= Matrix_Alloc(d
+1, d
+1+1);
341 value_set_si(coeff
->p
[0][0], 1);
342 value_set_si(coeff
->p
[0][d
+1], 1);
343 for (int i
= 1; i
<= d
; ++i
) {
344 value_multiply(coeff
->p
[i
][0], coeff
->p
[i
-1][0], d0
);
345 Vector_Combine(coeff
->p
[i
-1], coeff
->p
[i
-1]+1, coeff
->p
[i
]+1,
347 value_set_si(coeff
->p
[i
][d
+1], i
);
348 value_multiply(coeff
->p
[i
][d
+1], coeff
->p
[i
][d
+1], coeff
->p
[i
-1][d
+1]);
349 value_decrement(d0
, d0
);
354 void div(dpoly
& d
, Vector
*count
, ZZ
& sign
) {
355 int len
= coeff
->NbRows
;
356 Matrix
* c
= Matrix_Alloc(coeff
->NbRows
, coeff
->NbColumns
);
359 for (int i
= 0; i
< len
; ++i
) {
360 Vector_Copy(coeff
->p
[i
], c
->p
[i
], len
+1);
361 for (int j
= 1; j
<= i
; ++j
) {
362 zz2value(d
.coeff
[j
], tmp
);
363 value_multiply(tmp
, tmp
, c
->p
[i
][len
]);
364 value_oppose(tmp
, tmp
);
365 Vector_Combine(c
->p
[i
], c
->p
[i
-j
], c
->p
[i
],
366 c
->p
[i
-j
][len
], tmp
, len
);
367 value_multiply(c
->p
[i
][len
], c
->p
[i
][len
], c
->p
[i
-j
][len
]);
369 zz2value(d
.coeff
[0], tmp
);
370 value_multiply(c
->p
[i
][len
], c
->p
[i
][len
], tmp
);
373 value_set_si(tmp
, -1);
374 Vector_Scale(c
->p
[len
-1], count
->p
, tmp
, len
);
375 value_assign(count
->p
[len
], c
->p
[len
-1][len
]);
377 Vector_Copy(c
->p
[len
-1], count
->p
, len
+1);
378 Vector_Normalize(count
->p
, len
+1);
384 struct dpoly_r_term
{
390 vector
< dpoly_r_term
* > *c
;
394 void add_term(int i
, int * powers
, ZZ
& coeff
) {
395 for (int k
= 0; k
< c
[i
].size(); ++k
) {
396 if (memcmp(c
[i
][k
]->powers
, powers
, dim
* sizeof(int)) == 0) {
397 c
[i
][k
]->coeff
+= coeff
;
401 dpoly_r_term
*t
= new dpoly_r_term
;
402 t
->powers
= new int[dim
];
403 memcpy(t
->powers
, powers
, dim
* sizeof(int));
407 dpoly_r(int len
, int dim
) {
410 c
= new vector
< dpoly_r_term
* > [len
];
412 dpoly_r(dpoly
& num
, dpoly
& den
, int pos
, int sign
, int dim
) {
413 len
= num
.coeff
.length();
414 c
= new vector
< dpoly_r_term
* > [len
];
418 for (int i
= 0; i
< len
; ++i
) {
419 ZZ coeff
= num
.coeff
[i
];
420 memset(powers
, 0, dim
* sizeof(int));
423 add_term(i
, powers
, coeff
);
425 for (int j
= 1; j
<= i
; ++j
) {
426 for (int k
= 0; k
< c
[i
-j
].size(); ++k
) {
427 memcpy(powers
, c
[i
-j
][k
]->powers
, dim
*sizeof(int));
429 coeff
= -den
.coeff
[j
-1] * c
[i
-j
][k
]->coeff
;
430 add_term(i
, powers
, coeff
);
436 void div(dpoly
& d
, ZZ
& sign
, gen_fun
*gf
, mat_ZZ
& pden
, mat_ZZ
& den
,
438 dpoly_r
rc(len
, dim
);
439 ZZ max_d
= power(d
.coeff
[0], len
+1);
443 for (int i
= 0; i
< len
; ++i
) {
446 for (int k
= 0; k
< c
[i
].size(); ++k
) {
447 coeff
= c
[i
][k
]->coeff
* cur_d
;
448 rc
.add_term(i
, c
[i
][k
]->powers
, coeff
);
451 for (int j
= 1; j
<= i
; ++j
) {
452 for (int k
= 0; k
< rc
.c
[i
-j
].size(); ++k
) {
453 coeff
= - d
.coeff
[j
] * rc
.c
[i
-j
][k
]->coeff
/ d
.coeff
[0];
454 rc
.add_term(i
, rc
.c
[i
-j
][k
]->powers
, coeff
);
459 int common
= pden
.NumRows();
461 vector
< dpoly_r_term
* >& final
= rc
.c
[len
-1];
463 for (int j
= 0; j
< final
.size(); ++j
) {
465 pden
.SetDims(rows
, pden
.NumCols());
466 for (int k
= 0; k
< dim
; ++k
) {
467 int n
= final
[j
]->powers
[k
];
470 int abs_n
= n
< 0 ? -n
: n
;
471 pden
.SetDims(rows
+abs_n
, pden
.NumCols());
472 for (int l
= 0; l
< abs_n
; ++l
) {
474 pden
[rows
+l
] = den
[k
];
476 pden
[rows
+l
] = -den
[k
];
480 gf
->add(final
[j
]->coeff
, max_d
, num_p
, pden
);
484 for (int i
= 0; i
< len
; ++i
) {
487 cout
<< c
[i
].size() << endl
;
488 for (int j
= 0; j
< c
[i
].size(); ++j
) {
489 for (int k
= 0; k
< dim
; ++k
) {
490 cout
<< c
[i
][j
]->powers
[k
] << " ";
492 cout
<< ": " << c
[i
][j
]->coeff
<< endl
;
500 void decompose(Polyhedron
*C
);
501 virtual void handle(Polyhedron
*P
, int sign
) = 0;
504 struct polar_decomposer
: public decomposer
{
505 void decompose(Polyhedron
*C
, unsigned MaxRays
);
506 virtual void handle(Polyhedron
*P
, int sign
);
507 virtual void handle_polar(Polyhedron
*P
, int sign
) = 0;
510 void decomposer::decompose(Polyhedron
*C
)
512 vector
<cone
*> nonuni
;
513 cone
* c
= new cone(C
);
524 while (!nonuni
.empty()) {
527 Vector
* v
= c
->short_vector(lambda
);
528 for (int i
= 0; i
< c
->Rays
->NbRows
- 1; ++i
) {
531 Matrix
* M
= Matrix_Copy(c
->Rays
);
532 Vector_Copy(v
->p
, M
->p
[i
], v
->Size
);
533 cone
* pc
= new cone(M
);
534 assert (pc
->det
!= 0);
535 if (abs(pc
->det
) > 1) {
536 assert(abs(pc
->det
) < abs(c
->det
));
537 nonuni
.push_back(pc
);
539 handle(pc
->poly(), sign(pc
->det
) * s
);
549 void polar_decomposer::decompose(Polyhedron
*cone
, unsigned MaxRays
)
551 Polyhedron_Polarize(cone
);
552 if (cone
->NbRays
- 1 != cone
->Dimension
) {
553 Polyhedron
*tmp
= cone
;
554 cone
= triangularize_cone(cone
, MaxRays
);
555 Polyhedron_Free(tmp
);
557 for (Polyhedron
*Polar
= cone
; Polar
; Polar
= Polar
->next
)
558 decomposer::decompose(Polar
);
562 void polar_decomposer::handle(Polyhedron
*P
, int sign
)
564 Polyhedron_Polarize(P
);
565 handle_polar(P
, sign
);
569 * Barvinok's Decomposition of a simplicial cone
571 * Returns two lists of polyhedra
573 void barvinok_decompose(Polyhedron
*C
, Polyhedron
**ppos
, Polyhedron
**pneg
)
575 Polyhedron
*pos
= *ppos
, *neg
= *pneg
;
576 vector
<cone
*> nonuni
;
577 cone
* c
= new cone(C
);
584 Polyhedron
*p
= Polyhedron_Copy(c
->Cone
);
590 while (!nonuni
.empty()) {
593 Vector
* v
= c
->short_vector(lambda
);
594 for (int i
= 0; i
< c
->Rays
->NbRows
- 1; ++i
) {
597 Matrix
* M
= Matrix_Copy(c
->Rays
);
598 Vector_Copy(v
->p
, M
->p
[i
], v
->Size
);
599 cone
* pc
= new cone(M
);
600 assert (pc
->det
!= 0);
601 if (abs(pc
->det
) > 1) {
602 assert(abs(pc
->det
) < abs(c
->det
));
603 nonuni
.push_back(pc
);
605 Polyhedron
*p
= pc
->poly();
607 if (sign(pc
->det
) == s
) {
626 * Returns a single list of npos "positive" cones followed by nneg
628 * The input cone is freed
630 void decompose(Polyhedron
*cone
, Polyhedron
**parts
, int *npos
, int *nneg
, unsigned MaxRays
)
632 Polyhedron_Polarize(cone
);
633 if (cone
->NbRays
- 1 != cone
->Dimension
) {
634 Polyhedron
*tmp
= cone
;
635 cone
= triangularize_cone(cone
, MaxRays
);
636 Polyhedron_Free(tmp
);
638 Polyhedron
*polpos
= NULL
, *polneg
= NULL
;
639 *npos
= 0; *nneg
= 0;
640 for (Polyhedron
*Polar
= cone
; Polar
; Polar
= Polar
->next
)
641 barvinok_decompose(Polar
, &polpos
, &polneg
);
644 for (Polyhedron
*i
= polpos
; i
; i
= i
->next
) {
645 Polyhedron_Polarize(i
);
649 for (Polyhedron
*i
= polneg
; i
; i
= i
->next
) {
650 Polyhedron_Polarize(i
);
661 const int MAX_TRY
=10;
663 * Searches for a vector that is not orthogonal to any
664 * of the rays in rays.
666 static void nonorthog(mat_ZZ
& rays
, vec_ZZ
& lambda
)
668 int dim
= rays
.NumCols();
670 lambda
.SetLength(dim
);
674 for (int i
= 2; !found
&& i
<= 50*dim
; i
+=4) {
675 for (int j
= 0; j
< MAX_TRY
; ++j
) {
676 for (int k
= 0; k
< dim
; ++k
) {
677 int r
= random_int(i
)+2;
678 int v
= (2*(r
%2)-1) * (r
>> 1);
682 for (; k
< rays
.NumRows(); ++k
)
683 if (lambda
* rays
[k
] == 0)
685 if (k
== rays
.NumRows()) {
694 static void randomvector(Polyhedron
*P
, vec_ZZ
& lambda
, int nvar
)
698 unsigned int dim
= P
->Dimension
;
701 for (int i
= 0; i
< P
->NbRays
; ++i
) {
702 for (int j
= 1; j
<= dim
; ++j
) {
703 value_absolute(tmp
, P
->Ray
[i
][j
]);
704 int t
= VALUE_TO_LONG(tmp
);
709 for (int i
= 0; i
< P
->NbConstraints
; ++i
) {
710 for (int j
= 1; j
<= dim
; ++j
) {
711 value_absolute(tmp
, P
->Constraint
[i
][j
]);
712 int t
= VALUE_TO_LONG(tmp
);
719 lambda
.SetLength(nvar
);
720 for (int k
= 0; k
< nvar
; ++k
) {
721 int r
= random_int(8*max
*dim
)+2;
722 int v
= (2*(r
%2)-1) * (4*max
*dim
+ (r
>> 1));
727 static void add_rays(mat_ZZ
& rays
, Polyhedron
*i
, int *r
, int nvar
= -1,
730 unsigned dim
= i
->Dimension
;
733 for (int k
= 0; k
< i
->NbRays
; ++k
) {
734 if (!value_zero_p(i
->Ray
[k
][dim
+1]))
736 if (!all
&& nvar
!= dim
&& First_Non_Zero(i
->Ray
[k
]+1, nvar
) == -1)
738 values2zz(i
->Ray
[k
]+1, rays
[(*r
)++], nvar
);
742 void lattice_point(Value
* values
, Polyhedron
*i
, vec_ZZ
& vertex
)
744 unsigned dim
= i
->Dimension
;
745 if(!value_one_p(values
[dim
])) {
746 Matrix
* Rays
= rays(i
);
747 Matrix
*inv
= Matrix_Alloc(Rays
->NbRows
, Rays
->NbColumns
);
748 int ok
= Matrix_Inverse(Rays
, inv
);
752 Vector
*lambda
= Vector_Alloc(dim
+1);
753 Vector_Matrix_Product(values
, inv
, lambda
->p
);
755 for (int j
= 0; j
< dim
; ++j
)
756 mpz_cdiv_q(lambda
->p
[j
], lambda
->p
[j
], lambda
->p
[dim
]);
757 value_set_si(lambda
->p
[dim
], 1);
758 Vector
*A
= Vector_Alloc(dim
+1);
759 Vector_Matrix_Product(lambda
->p
, Rays
, A
->p
);
762 values2zz(A
->p
, vertex
, dim
);
765 values2zz(values
, vertex
, dim
);
768 static evalue
*term(int param
, ZZ
& c
, Value
*den
= NULL
)
770 evalue
*EP
= new evalue();
772 value_set_si(EP
->d
,0);
773 EP
->x
.p
= new_enode(polynomial
, 2, param
+ 1);
774 evalue_set_si(&EP
->x
.p
->arr
[0], 0, 1);
775 value_init(EP
->x
.p
->arr
[1].x
.n
);
777 value_set_si(EP
->x
.p
->arr
[1].d
, 1);
779 value_assign(EP
->x
.p
->arr
[1].d
, *den
);
780 zz2value(c
, EP
->x
.p
->arr
[1].x
.n
);
784 static void vertex_period(
785 Polyhedron
*i
, vec_ZZ
& lambda
, Matrix
*T
,
786 Value lcm
, int p
, Vector
*val
,
787 evalue
*E
, evalue
* ev
,
790 unsigned nparam
= T
->NbRows
- 1;
791 unsigned dim
= i
->Dimension
;
798 Vector
* values
= Vector_Alloc(dim
+ 1);
799 Vector_Matrix_Product(val
->p
, T
, values
->p
);
800 value_assign(values
->p
[dim
], lcm
);
801 lattice_point(values
->p
, i
, vertex
);
802 num
= vertex
* lambda
;
807 zz2value(num
, ev
->x
.n
);
808 value_assign(ev
->d
, lcm
);
815 values2zz(T
->p
[p
], vertex
, dim
);
816 nump
= vertex
* lambda
;
817 if (First_Non_Zero(val
->p
, p
) == -1) {
818 value_assign(tmp
, lcm
);
819 evalue
*ET
= term(p
, nump
, &tmp
);
821 free_evalue_refs(ET
);
825 value_assign(tmp
, lcm
);
826 if (First_Non_Zero(T
->p
[p
], dim
) != -1)
827 Vector_Gcd(T
->p
[p
], dim
, &tmp
);
829 if (value_lt(tmp
, lcm
)) {
832 value_division(tmp
, lcm
, tmp
);
833 value_set_si(ev
->d
, 0);
834 ev
->x
.p
= new_enode(periodic
, VALUE_TO_INT(tmp
), p
+1);
835 value2zz(tmp
, count
);
837 value_decrement(tmp
, tmp
);
839 ZZ new_offset
= offset
- count
* nump
;
840 value_assign(val
->p
[p
], tmp
);
841 vertex_period(i
, lambda
, T
, lcm
, p
+1, val
, E
,
842 &ev
->x
.p
->arr
[VALUE_TO_INT(tmp
)], new_offset
);
843 } while (value_pos_p(tmp
));
845 vertex_period(i
, lambda
, T
, lcm
, p
+1, val
, E
, ev
, offset
);
849 static void mask_r(Matrix
*f
, int nr
, Vector
*lcm
, int p
, Vector
*val
, evalue
*ev
)
851 unsigned nparam
= lcm
->Size
;
854 Vector
* prod
= Vector_Alloc(f
->NbRows
);
855 Matrix_Vector_Product(f
, val
->p
, prod
->p
);
857 for (int i
= 0; i
< nr
; ++i
) {
858 value_modulus(prod
->p
[i
], prod
->p
[i
], f
->p
[i
][nparam
+1]);
859 isint
&= value_zero_p(prod
->p
[i
]);
861 value_set_si(ev
->d
, 1);
863 value_set_si(ev
->x
.n
, isint
);
870 if (value_one_p(lcm
->p
[p
]))
871 mask_r(f
, nr
, lcm
, p
+1, val
, ev
);
873 value_assign(tmp
, lcm
->p
[p
]);
874 value_set_si(ev
->d
, 0);
875 ev
->x
.p
= new_enode(periodic
, VALUE_TO_INT(tmp
), p
+1);
877 value_decrement(tmp
, tmp
);
878 value_assign(val
->p
[p
], tmp
);
879 mask_r(f
, nr
, lcm
, p
+1, val
, &ev
->x
.p
->arr
[VALUE_TO_INT(tmp
)]);
880 } while (value_pos_p(tmp
));
885 static evalue
*multi_monom(vec_ZZ
& p
)
887 evalue
*X
= new evalue();
890 unsigned nparam
= p
.length()-1;
891 zz2value(p
[nparam
], X
->x
.n
);
892 value_set_si(X
->d
, 1);
893 for (int i
= 0; i
< nparam
; ++i
) {
896 evalue
*T
= term(i
, p
[i
]);
905 * Check whether mapping polyhedron P on the affine combination
906 * num yields a range that has a fixed quotient on integer
908 * If zero is true, then we are only interested in the quotient
909 * for the cases where the remainder is zero.
910 * Returns NULL if false and a newly allocated value if true.
912 static Value
*fixed_quotient(Polyhedron
*P
, vec_ZZ
& num
, Value d
, bool zero
)
915 int len
= num
.length();
916 Matrix
*T
= Matrix_Alloc(2, len
);
917 zz2values(num
, T
->p
[0]);
918 value_set_si(T
->p
[1][len
-1], 1);
919 Polyhedron
*I
= Polyhedron_Image(P
, T
, P
->NbConstraints
);
923 for (i
= 0; i
< I
->NbRays
; ++i
)
924 if (value_zero_p(I
->Ray
[i
][2])) {
932 int bounded
= line_minmax(I
, &min
, &max
);
936 mpz_cdiv_q(min
, min
, d
);
938 mpz_fdiv_q(min
, min
, d
);
939 mpz_fdiv_q(max
, max
, d
);
941 if (value_eq(min
, max
)) {
944 value_assign(*ret
, min
);
952 * Normalize linear expression coef modulo m
953 * Removes common factor and reduces coefficients
954 * Returns index of first non-zero coefficient or len
956 static int normal_mod(Value
*coef
, int len
, Value
*m
)
961 Vector_Gcd(coef
, len
, &gcd
);
963 Vector_AntiScale(coef
, coef
, gcd
, len
);
965 value_division(*m
, *m
, gcd
);
972 for (j
= 0; j
< len
; ++j
)
973 mpz_fdiv_r(coef
[j
], coef
[j
], *m
);
974 for (j
= 0; j
< len
; ++j
)
975 if (value_notzero_p(coef
[j
]))
982 static void mask(Matrix
*f
, evalue
*factor
)
984 int nr
= f
->NbRows
, nc
= f
->NbColumns
;
987 for (n
= 0; n
< nr
&& value_notzero_p(f
->p
[n
][nc
-1]); ++n
)
988 if (value_notone_p(f
->p
[n
][nc
-1]) &&
989 value_notmone_p(f
->p
[n
][nc
-1]))
1003 value_set_si(EV
.x
.n
, 1);
1005 for (n
= 0; n
< nr
; ++n
) {
1006 value_assign(m
, f
->p
[n
][nc
-1]);
1007 if (value_one_p(m
) || value_mone_p(m
))
1010 int j
= normal_mod(f
->p
[n
], nc
-1, &m
);
1012 free_evalue_refs(factor
);
1013 value_init(factor
->d
);
1014 evalue_set_si(factor
, 0, 1);
1018 values2zz(f
->p
[n
], row
, nc
-1);
1021 if (j
< (nc
-1)-1 && row
[j
] > g
/2) {
1022 for (int k
= j
; k
< (nc
-1); ++k
)
1024 row
[k
] = g
- row
[k
];
1028 value_set_si(EP
.d
, 0);
1029 EP
.x
.p
= new_enode(relation
, 2, 0);
1030 value_clear(EP
.x
.p
->arr
[1].d
);
1031 EP
.x
.p
->arr
[1] = *factor
;
1032 evalue
*ev
= &EP
.x
.p
->arr
[0];
1033 value_set_si(ev
->d
, 0);
1034 ev
->x
.p
= new_enode(fractional
, 3, -1);
1035 evalue_set_si(&ev
->x
.p
->arr
[1], 0, 1);
1036 evalue_set_si(&ev
->x
.p
->arr
[2], 1, 1);
1037 evalue
*E
= multi_monom(row
);
1038 value_assign(EV
.d
, m
);
1040 value_clear(ev
->x
.p
->arr
[0].d
);
1041 ev
->x
.p
->arr
[0] = *E
;
1047 free_evalue_refs(&EV
);
1053 static void mask(Matrix
*f
, evalue
*factor
)
1055 int nr
= f
->NbRows
, nc
= f
->NbColumns
;
1058 for (n
= 0; n
< nr
&& value_notzero_p(f
->p
[n
][nc
-1]); ++n
)
1059 if (value_notone_p(f
->p
[n
][nc
-1]) &&
1060 value_notmone_p(f
->p
[n
][nc
-1]))
1068 unsigned np
= nc
- 2;
1069 Vector
*lcm
= Vector_Alloc(np
);
1070 Vector
*val
= Vector_Alloc(nc
);
1071 Vector_Set(val
->p
, 0, nc
);
1072 value_set_si(val
->p
[np
], 1);
1073 Vector_Set(lcm
->p
, 1, np
);
1074 for (n
= 0; n
< nr
; ++n
) {
1075 if (value_one_p(f
->p
[n
][nc
-1]) ||
1076 value_mone_p(f
->p
[n
][nc
-1]))
1078 for (int j
= 0; j
< np
; ++j
)
1079 if (value_notzero_p(f
->p
[n
][j
])) {
1080 Gcd(f
->p
[n
][j
], f
->p
[n
][nc
-1], &tmp
);
1081 value_division(tmp
, f
->p
[n
][nc
-1], tmp
);
1082 value_lcm(tmp
, lcm
->p
[j
], &lcm
->p
[j
]);
1087 mask_r(f
, nr
, lcm
, 0, val
, &EP
);
1092 free_evalue_refs(&EP
);
1103 static bool mod_needed(Polyhedron
*PD
, vec_ZZ
& num
, Value d
, evalue
*E
)
1105 Value
*q
= fixed_quotient(PD
, num
, d
, false);
1110 value_oppose(*q
, *q
);
1113 value_set_si(EV
.d
, 1);
1115 value_multiply(EV
.x
.n
, *q
, d
);
1117 free_evalue_refs(&EV
);
1123 static void ceil_mod(Value
*coef
, int len
, Value d
, ZZ
& f
, evalue
*EP
, Polyhedron
*PD
)
1127 value_set_si(m
, -1);
1129 Vector_Scale(coef
, coef
, m
, len
);
1132 int j
= normal_mod(coef
, len
, &m
);
1140 values2zz(coef
, num
, len
);
1147 evalue_set_si(&tmp
, 0, 1);
1151 while (j
< len
-1 && (num
[j
] == g
/2 || num
[j
] == 0))
1153 if ((j
< len
-1 && num
[j
] > g
/2) || (j
== len
-1 && num
[j
] >= (g
+1)/2)) {
1154 for (int k
= j
; k
< len
-1; ++k
)
1156 num
[k
] = g
- num
[k
];
1157 num
[len
-1] = g
- 1 - num
[len
-1];
1158 value_assign(tmp
.d
, m
);
1160 zz2value(t
, tmp
.x
.n
);
1166 ZZ t
= num
[len
-1] * f
;
1167 zz2value(t
, tmp
.x
.n
);
1168 value_assign(tmp
.d
, m
);
1171 evalue
*E
= multi_monom(num
);
1175 if (PD
&& !mod_needed(PD
, num
, m
, E
)) {
1177 zz2value(f
, EV
.x
.n
);
1178 value_assign(EV
.d
, m
);
1183 value_set_si(EV
.x
.n
, 1);
1184 value_assign(EV
.d
, m
);
1186 value_clear(EV
.x
.n
);
1187 value_set_si(EV
.d
, 0);
1188 EV
.x
.p
= new_enode(fractional
, 3, -1);
1189 evalue_copy(&EV
.x
.p
->arr
[0], E
);
1190 evalue_set_si(&EV
.x
.p
->arr
[1], 0, 1);
1191 value_init(EV
.x
.p
->arr
[2].x
.n
);
1192 zz2value(f
, EV
.x
.p
->arr
[2].x
.n
);
1193 value_set_si(EV
.x
.p
->arr
[2].d
, 1);
1198 free_evalue_refs(&EV
);
1199 free_evalue_refs(E
);
1203 free_evalue_refs(&tmp
);
1209 evalue
* bv_ceil3(Value
*coef
, int len
, Value d
, Polyhedron
*P
)
1211 Vector
*val
= Vector_Alloc(len
);
1215 value_set_si(t
, -1);
1216 Vector_Scale(coef
, val
->p
, t
, len
);
1217 value_absolute(t
, d
);
1220 values2zz(val
->p
, num
, len
);
1221 evalue
*EP
= multi_monom(num
);
1225 value_init(tmp
.x
.n
);
1226 value_set_si(tmp
.x
.n
, 1);
1227 value_assign(tmp
.d
, t
);
1233 ceil_mod(val
->p
, len
, t
, one
, EP
, P
);
1236 /* copy EP to malloc'ed evalue */
1242 free_evalue_refs(&tmp
);
1249 evalue
* lattice_point(
1250 Polyhedron
*i
, vec_ZZ
& lambda
, Matrix
*W
, Value lcm
, Polyhedron
*PD
)
1252 unsigned nparam
= W
->NbColumns
- 1;
1254 Matrix
* Rays
= rays2(i
);
1255 Matrix
*T
= Transpose(Rays
);
1256 Matrix
*T2
= Matrix_Copy(T
);
1257 Matrix
*inv
= Matrix_Alloc(T2
->NbRows
, T2
->NbColumns
);
1258 int ok
= Matrix_Inverse(T2
, inv
);
1263 matrix2zz(W
, vertex
, W
->NbRows
, W
->NbColumns
);
1266 num
= lambda
* vertex
;
1268 evalue
*EP
= multi_monom(num
);
1272 value_init(tmp
.x
.n
);
1273 value_set_si(tmp
.x
.n
, 1);
1274 value_assign(tmp
.d
, lcm
);
1278 Matrix
*L
= Matrix_Alloc(inv
->NbRows
, W
->NbColumns
);
1279 Matrix_Product(inv
, W
, L
);
1282 matrix2zz(T
, RT
, T
->NbRows
, T
->NbColumns
);
1285 vec_ZZ p
= lambda
* RT
;
1287 for (int i
= 0; i
< L
->NbRows
; ++i
) {
1288 ceil_mod(L
->p
[i
], nparam
+1, lcm
, p
[i
], EP
, PD
);
1294 free_evalue_refs(&tmp
);
1298 evalue
* lattice_point(
1299 Polyhedron
*i
, vec_ZZ
& lambda
, Matrix
*W
, Value lcm
, Polyhedron
*PD
)
1301 Matrix
*T
= Transpose(W
);
1302 unsigned nparam
= T
->NbRows
- 1;
1304 evalue
*EP
= new evalue();
1306 evalue_set_si(EP
, 0, 1);
1309 Vector
*val
= Vector_Alloc(nparam
+1);
1310 value_set_si(val
->p
[nparam
], 1);
1311 ZZ
offset(INIT_VAL
, 0);
1313 vertex_period(i
, lambda
, T
, lcm
, 0, val
, EP
, &ev
, offset
);
1316 free_evalue_refs(&ev
);
1327 Param_Vertices
* V
, Polyhedron
*i
, vec_ZZ
& lambda
, term_info
* term
,
1330 unsigned nparam
= V
->Vertex
->NbColumns
- 2;
1331 unsigned dim
= i
->Dimension
;
1333 vertex
.SetDims(V
->Vertex
->NbRows
, nparam
+1);
1337 value_set_si(lcm
, 1);
1338 for (int j
= 0; j
< V
->Vertex
->NbRows
; ++j
) {
1339 value_lcm(lcm
, V
->Vertex
->p
[j
][nparam
+1], &lcm
);
1341 if (value_notone_p(lcm
)) {
1342 Matrix
* mv
= Matrix_Alloc(dim
, nparam
+1);
1343 for (int j
= 0 ; j
< dim
; ++j
) {
1344 value_division(tmp
, lcm
, V
->Vertex
->p
[j
][nparam
+1]);
1345 Vector_Scale(V
->Vertex
->p
[j
], mv
->p
[j
], tmp
, nparam
+1);
1348 term
->E
= lattice_point(i
, lambda
, mv
, lcm
, PD
);
1356 for (int i
= 0; i
< V
->Vertex
->NbRows
; ++i
) {
1357 assert(value_one_p(V
->Vertex
->p
[i
][nparam
+1])); // for now
1358 values2zz(V
->Vertex
->p
[i
], vertex
[i
], nparam
+1);
1362 num
= lambda
* vertex
;
1366 for (int j
= 0; j
< nparam
; ++j
)
1372 term
->E
= multi_monom(num
);
1376 term
->constant
= num
[nparam
];
1379 term
->coeff
= num
[p
];
1386 void normalize(Polyhedron
*i
, vec_ZZ
& lambda
, ZZ
& sign
, ZZ
& num
, vec_ZZ
& den
)
1388 unsigned dim
= i
->Dimension
;
1392 rays
.SetDims(dim
, dim
);
1393 add_rays(rays
, i
, &r
);
1394 den
= rays
* lambda
;
1397 for (int j
= 0; j
< den
.length(); ++j
) {
1401 den
[j
] = abs(den
[j
]);
1409 struct counter
: public polar_decomposer
{
1421 counter(Polyhedron
*P
) {
1424 randomvector(P
, lambda
, dim
);
1425 rays
.SetDims(dim
, dim
);
1430 void start(unsigned MaxRays
);
1436 virtual void handle_polar(Polyhedron
*P
, int sign
);
1439 void counter::handle_polar(Polyhedron
*C
, int s
)
1442 assert(C
->NbRays
-1 == dim
);
1443 add_rays(rays
, C
, &r
);
1444 for (int k
= 0; k
< dim
; ++k
) {
1445 assert(lambda
* rays
[k
] != 0);
1450 lattice_point(P
->Ray
[j
]+1, C
, vertex
);
1451 num
= vertex
* lambda
;
1452 normalize(C
, lambda
, sign
, num
, den
);
1455 dpoly
n(dim
, den
[0], 1);
1456 for (int k
= 1; k
< dim
; ++k
) {
1457 dpoly
fact(dim
, den
[k
], 1);
1460 d
.div(n
, count
, sign
);
1463 void counter::start(unsigned MaxRays
)
1465 for (j
= 0; j
< P
->NbRays
; ++j
) {
1466 Polyhedron
*C
= supporting_cone(P
, j
);
1467 decompose(C
, MaxRays
);
1471 typedef Polyhedron
* Polyhedron_p
;
1473 void barvinok_count(Polyhedron
*P
, Value
* result
, unsigned NbMaxCons
)
1475 Polyhedron
** vcone
;
1484 value_set_si(*result
, 0);
1488 for (; r
< P
->NbRays
; ++r
)
1489 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1]))
1491 if (P
->NbBid
!=0 || r
< P
->NbRays
) {
1492 value_set_si(*result
, -1);
1496 P
= remove_equalities(P
);
1499 value_set_si(*result
, 0);
1505 value_set_si(factor
, 1);
1506 Q
= Polyhedron_Reduce(P
, &factor
);
1513 if (P
->Dimension
== 0) {
1514 value_assign(*result
, factor
);
1517 value_clear(factor
);
1522 cnt
.start(NbMaxCons
);
1524 assert(value_one_p(&cnt
.count
[0]._mp_den
));
1525 value_multiply(*result
, &cnt
.count
[0]._mp_num
, factor
);
1529 value_clear(factor
);
1532 static void uni_polynom(int param
, Vector
*c
, evalue
*EP
)
1534 unsigned dim
= c
->Size
-2;
1536 value_set_si(EP
->d
,0);
1537 EP
->x
.p
= new_enode(polynomial
, dim
+1, param
+1);
1538 for (int j
= 0; j
<= dim
; ++j
)
1539 evalue_set(&EP
->x
.p
->arr
[j
], c
->p
[j
], c
->p
[dim
+1]);
1542 static void multi_polynom(Vector
*c
, evalue
* X
, evalue
*EP
)
1544 unsigned dim
= c
->Size
-2;
1548 evalue_set(&EC
, c
->p
[dim
], c
->p
[dim
+1]);
1551 evalue_set(EP
, c
->p
[dim
], c
->p
[dim
+1]);
1553 for (int i
= dim
-1; i
>= 0; --i
) {
1555 value_assign(EC
.x
.n
, c
->p
[i
]);
1558 free_evalue_refs(&EC
);
1561 Polyhedron
*unfringe (Polyhedron
*P
, unsigned MaxRays
)
1563 int len
= P
->Dimension
+2;
1564 Polyhedron
*T
, *R
= P
;
1567 Vector
*row
= Vector_Alloc(len
);
1568 value_set_si(row
->p
[0], 1);
1570 R
= DomainConstraintSimplify(Polyhedron_Copy(P
), MaxRays
);
1572 Matrix
*M
= Matrix_Alloc(2, len
-1);
1573 value_set_si(M
->p
[1][len
-2], 1);
1574 for (int v
= 0; v
< P
->Dimension
; ++v
) {
1575 value_set_si(M
->p
[0][v
], 1);
1576 Polyhedron
*I
= Polyhedron_Image(P
, M
, 2+1);
1577 value_set_si(M
->p
[0][v
], 0);
1578 for (int r
= 0; r
< I
->NbConstraints
; ++r
) {
1579 if (value_zero_p(I
->Constraint
[r
][0]))
1581 if (value_zero_p(I
->Constraint
[r
][1]))
1583 if (value_one_p(I
->Constraint
[r
][1]))
1585 if (value_mone_p(I
->Constraint
[r
][1]))
1587 value_absolute(g
, I
->Constraint
[r
][1]);
1588 Vector_Set(row
->p
+1, 0, len
-2);
1589 value_division(row
->p
[1+v
], I
->Constraint
[r
][1], g
);
1590 mpz_fdiv_q(row
->p
[len
-1], I
->Constraint
[r
][2], g
);
1592 R
= AddConstraints(row
->p
, 1, R
, MaxRays
);
1604 static Polyhedron
*reduce_domain(Polyhedron
*D
, Matrix
*CT
, Polyhedron
*CEq
,
1605 Polyhedron
**fVD
, int nd
, unsigned MaxRays
)
1610 Dt
= CT
? DomainPreimage(D
, CT
, MaxRays
) : D
;
1611 Polyhedron
*rVD
= DomainIntersection(Dt
, CEq
, MaxRays
);
1613 /* if rVD is empty or too small in geometric dimension */
1614 if(!rVD
|| emptyQ(rVD
) ||
1615 (rVD
->Dimension
-rVD
->NbEq
< Dt
->Dimension
-Dt
->NbEq
-CEq
->NbEq
)) {
1620 return 0; /* empty validity domain */
1626 fVD
[nd
] = Domain_Copy(rVD
);
1627 for (int i
= 0 ; i
< nd
; ++i
) {
1628 Polyhedron
*I
= DomainIntersection(fVD
[nd
], fVD
[i
], MaxRays
);
1633 Polyhedron
*F
= DomainSimplify(I
, fVD
[nd
], MaxRays
);
1635 Polyhedron
*T
= rVD
;
1636 rVD
= DomainDifference(rVD
, F
, MaxRays
);
1643 rVD
= DomainConstraintSimplify(rVD
, MaxRays
);
1645 Domain_Free(fVD
[nd
]);
1652 barvinok_count(rVD
, &c
, MaxRays
);
1653 if (value_zero_p(c
)) {
1662 static bool Polyhedron_is_infinite(Polyhedron
*P
, unsigned nparam
)
1665 for (r
= 0; r
< P
->NbRays
; ++r
)
1666 if (value_zero_p(P
->Ray
[r
][0]) ||
1667 value_zero_p(P
->Ray
[r
][P
->Dimension
+1])) {
1669 for (i
= P
->Dimension
- nparam
; i
< P
->Dimension
; ++i
)
1670 if (value_notzero_p(P
->Ray
[r
][i
+1]))
1672 if (i
>= P
->Dimension
)
1675 return r
< P
->NbRays
;
1678 /* Check whether all rays point in the positive directions
1679 * for the parameters
1681 static bool Polyhedron_has_positive_rays(Polyhedron
*P
, unsigned nparam
)
1684 for (r
= 0; r
< P
->NbRays
; ++r
)
1685 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1])) {
1687 for (i
= P
->Dimension
- nparam
; i
< P
->Dimension
; ++i
)
1688 if (value_neg_p(P
->Ray
[r
][i
+1]))
1694 typedef evalue
* evalue_p
;
1696 evalue
* barvinok_enumerate_ev(Polyhedron
*P
, Polyhedron
* C
, unsigned MaxRays
)
1698 //P = unfringe(P, MaxRays);
1699 Polyhedron
*CEq
= NULL
, *rVD
, *pVD
, *CA
;
1701 Param_Polyhedron
*PP
= NULL
;
1702 Param_Domain
*D
, *next
;
1705 unsigned nparam
= C
->Dimension
;
1707 ALLOC(evalue
, eres
);
1708 value_init(eres
->d
);
1709 value_set_si(eres
->d
, 0);
1712 value_init(factor
.d
);
1713 evalue_set_si(&factor
, 1, 1);
1715 CA
= align_context(C
, P
->Dimension
, MaxRays
);
1716 P
= DomainIntersection(P
, CA
, MaxRays
);
1717 Polyhedron_Free(CA
);
1719 if (C
->Dimension
== 0 || emptyQ(P
)) {
1721 eres
->x
.p
= new_enode(partition
, 2, C
->Dimension
);
1722 EVALUE_SET_DOMAIN(eres
->x
.p
->arr
[0],
1723 DomainConstraintSimplify(CEq
? CEq
: Polyhedron_Copy(C
), MaxRays
));
1724 value_set_si(eres
->x
.p
->arr
[1].d
, 1);
1725 value_init(eres
->x
.p
->arr
[1].x
.n
);
1727 value_set_si(eres
->x
.p
->arr
[1].x
.n
, 0);
1729 barvinok_count(P
, &eres
->x
.p
->arr
[1].x
.n
, MaxRays
);
1731 emul(&factor
, eres
);
1732 reduce_evalue(eres
);
1733 free_evalue_refs(&factor
);
1738 Param_Polyhedron_Free(PP
);
1742 if (Polyhedron_is_infinite(P
, nparam
))
1747 P
= remove_equalities_p(P
, P
->Dimension
-nparam
, &f
);
1751 if (P
->Dimension
== nparam
) {
1753 P
= Universe_Polyhedron(0);
1757 Polyhedron
*Q
= ParamPolyhedron_Reduce(P
, P
->Dimension
-nparam
, &factor
);
1760 if (Q
->Dimension
== nparam
) {
1762 P
= Universe_Polyhedron(0);
1767 Polyhedron
*oldP
= P
;
1768 PP
= Polyhedron2Param_SimplifiedDomain(&P
,C
,MaxRays
,&CEq
,&CT
);
1770 Polyhedron_Free(oldP
);
1772 if (isIdentity(CT
)) {
1776 assert(CT
->NbRows
!= CT
->NbColumns
);
1777 if (CT
->NbRows
== 1) // no more parameters
1779 nparam
= CT
->NbRows
- 1;
1782 unsigned dim
= P
->Dimension
- nparam
;
1783 evalue
** vE
= new evalue_p
[PP
->nbV
];
1784 for (int j
= 0; j
< PP
->nbV
; ++j
)
1789 //nonorthog(rays, lambda);
1790 randomvector(P
, lambda
, dim
);
1792 Vector
*c
= Vector_Alloc(dim
+2);
1795 for (nd
= 0, D
=PP
->D
; D
; ++nd
, D
=D
->next
);
1796 struct section
{ Polyhedron
*D
; evalue E
; };
1797 section
*s
= new section
[nd
];
1798 Polyhedron
**fVD
= new Polyhedron_p
[nd
];
1805 rays
.SetDims(dim
, dim
);
1810 for(nd
= 0, D
=PP
->D
; D
; D
=next
) {
1813 Polyhedron
*rVD
= reduce_domain(D
->Domain
, CT
, CEq
,
1818 pVD
= CT
? DomainImage(rVD
,CT
,MaxRays
) : rVD
;
1820 value_init(s
[nd
].E
.d
);
1821 evalue_set_si(&s
[nd
].E
, 0, 1);
1823 FORALL_PVertex_in_ParamPolyhedron(V
,D
,PP
) // _i is internal counter
1826 Polyhedron
*C
= supporting_cone_p(P
, V
);
1830 decompose(C
, &vcone
, &npos
, &nneg
, MaxRays
);
1832 vE
[_i
] = new evalue
;
1833 value_init(vE
[_i
]->d
);
1834 evalue_set_si(vE
[_i
], 0, 1);
1836 for (Polyhedron
*i
= vcone
; i
; i
= i
->next
) {
1838 assert(i
->NbRays
-1 == dim
);
1839 add_rays(rays
, i
, &r
);
1840 for (int k
= 0; k
< dim
; ++k
) {
1841 assert(lambda
* rays
[k
] != 0);
1844 sign
= f
< npos
? 1 : -1;
1845 lattice_point(V
, i
, lambda
, &num
, 0);
1846 normalize(i
, lambda
, sign
, num
.constant
, den
);
1848 dpoly
n(dim
, den
[0], 1);
1849 for (int k
= 1; k
< dim
; ++k
) {
1850 dpoly
fact(dim
, den
[k
], 1);
1853 if (num
.E
!= NULL
) {
1854 ZZ
one(INIT_VAL
, 1);
1855 dpoly_n
d(dim
, num
.constant
, one
);
1858 multi_polynom(c
, num
.E
, &EV
);
1860 free_evalue_refs(&EV
);
1861 free_evalue_refs(num
.E
);
1863 } else if (num
.pos
!= -1) {
1864 dpoly_n
d(dim
, num
.constant
, num
.coeff
);
1867 uni_polynom(num
.pos
, c
, &EV
);
1869 free_evalue_refs(&EV
);
1871 mpq_set_si(count
, 0, 1);
1872 dpoly
d(dim
, num
.constant
);
1873 d
.div(n
, count
, sign
);
1876 evalue_set(&EV
, &count
[0]._mp_num
, &count
[0]._mp_den
);
1878 free_evalue_refs(&EV
);
1885 eadd(vE
[_i
] , &s
[nd
].E
);
1886 END_FORALL_PVertex_in_ParamPolyhedron
;
1887 reduce_in_domain(&s
[nd
].E
, pVD
);
1890 addeliminatedparams_evalue(&s
[nd
].E
, CT
);
1900 evalue_set_si(eres
, 0, 1);
1902 eres
->x
.p
= new_enode(partition
, 2*nd
, C
->Dimension
);
1903 for (int j
= 0; j
< nd
; ++j
) {
1904 EVALUE_SET_DOMAIN(eres
->x
.p
->arr
[2*j
], s
[j
].D
);
1905 value_clear(eres
->x
.p
->arr
[2*j
+1].d
);
1906 eres
->x
.p
->arr
[2*j
+1] = s
[j
].E
;
1907 Domain_Free(fVD
[j
]);
1915 for (int j
= 0; j
< PP
->nbV
; ++j
) {
1917 free_evalue_refs(vE
[j
]);
1924 Polyhedron_Free(CEq
);
1929 Enumeration
* barvinok_enumerate(Polyhedron
*P
, Polyhedron
* C
, unsigned MaxRays
)
1931 evalue
*EP
= barvinok_enumerate_ev(P
, C
, MaxRays
);
1933 return partition2enumeration(EP
);
1936 static void SwapColumns(Value
**V
, int n
, int i
, int j
)
1938 for (int r
= 0; r
< n
; ++r
)
1939 value_swap(V
[r
][i
], V
[r
][j
]);
1942 static void SwapColumns(Polyhedron
*P
, int i
, int j
)
1944 SwapColumns(P
->Constraint
, P
->NbConstraints
, i
, j
);
1945 SwapColumns(P
->Ray
, P
->NbRays
, i
, j
);
1948 static void negative_test_constraint(Value
*l
, Value
*u
, Value
*c
, int pos
,
1951 value_oppose(*v
, u
[pos
+1]);
1952 Vector_Combine(l
+1, u
+1, c
+1, *v
, l
[pos
+1], len
-1);
1953 value_multiply(*v
, *v
, l
[pos
+1]);
1954 value_substract(c
[len
-1], c
[len
-1], *v
);
1955 value_set_si(*v
, -1);
1956 Vector_Scale(c
+1, c
+1, *v
, len
-1);
1957 value_decrement(c
[len
-1], c
[len
-1]);
1958 ConstraintSimplify(c
, c
, len
, v
);
1961 static void oppose_constraint(Value
*c
, int len
, Value
*v
)
1963 value_set_si(*v
, -1);
1964 Vector_Scale(c
+1, c
+1, *v
, len
-1);
1965 value_decrement(c
[len
-1], c
[len
-1]);
1968 static bool SplitOnConstraint(Polyhedron
*P
, int i
, int l
, int u
,
1969 int nvar
, int len
, int exist
, int MaxRays
,
1970 Vector
*row
, Value
& f
, bool independent
,
1971 Polyhedron
**pos
, Polyhedron
**neg
)
1973 negative_test_constraint(P
->Constraint
[l
], P
->Constraint
[u
],
1974 row
->p
, nvar
+i
, len
, &f
);
1975 *neg
= AddConstraints(row
->p
, 1, P
, MaxRays
);
1977 /* We found an independent, but useless constraint
1978 * Maybe we should detect this earlier and not
1979 * mark the variable as INDEPENDENT
1981 if (emptyQ((*neg
))) {
1982 Polyhedron_Free(*neg
);
1986 oppose_constraint(row
->p
, len
, &f
);
1987 *pos
= AddConstraints(row
->p
, 1, P
, MaxRays
);
1989 if (emptyQ((*pos
))) {
1990 Polyhedron_Free(*neg
);
1991 Polyhedron_Free(*pos
);
1999 * unimodularly transform P such that constraint r is transformed
2000 * into a constraint that involves only a single (the first)
2001 * existential variable
2004 static Polyhedron
*rotate_along(Polyhedron
*P
, int r
, int nvar
, int exist
,
2010 Vector
*row
= Vector_Alloc(exist
);
2011 Vector_Copy(P
->Constraint
[r
]+1+nvar
, row
->p
, exist
);
2012 Vector_Gcd(row
->p
, exist
, &g
);
2013 if (value_notone_p(g
))
2014 Vector_AntiScale(row
->p
, row
->p
, g
, exist
);
2017 Matrix
*M
= unimodular_complete(row
);
2018 Matrix
*M2
= Matrix_Alloc(P
->Dimension
+1, P
->Dimension
+1);
2019 for (r
= 0; r
< nvar
; ++r
)
2020 value_set_si(M2
->p
[r
][r
], 1);
2021 for ( ; r
< nvar
+exist
; ++r
)
2022 Vector_Copy(M
->p
[r
-nvar
], M2
->p
[r
]+nvar
, exist
);
2023 for ( ; r
< P
->Dimension
+1; ++r
)
2024 value_set_si(M2
->p
[r
][r
], 1);
2025 Polyhedron
*T
= Polyhedron_Image(P
, M2
, MaxRays
);
2034 static bool SplitOnVar(Polyhedron
*P
, int i
,
2035 int nvar
, int len
, int exist
, int MaxRays
,
2036 Vector
*row
, Value
& f
, bool independent
,
2037 Polyhedron
**pos
, Polyhedron
**neg
)
2041 for (int l
= P
->NbEq
; l
< P
->NbConstraints
; ++l
) {
2042 if (value_negz_p(P
->Constraint
[l
][nvar
+i
+1]))
2046 for (j
= 0; j
< exist
; ++j
)
2047 if (j
!= i
&& value_notzero_p(P
->Constraint
[l
][nvar
+j
+1]))
2053 for (int u
= P
->NbEq
; u
< P
->NbConstraints
; ++u
) {
2054 if (value_posz_p(P
->Constraint
[u
][nvar
+i
+1]))
2058 for (j
= 0; j
< exist
; ++j
)
2059 if (j
!= i
&& value_notzero_p(P
->Constraint
[u
][nvar
+j
+1]))
2065 if (SplitOnConstraint(P
, i
, l
, u
,
2066 nvar
, len
, exist
, MaxRays
,
2067 row
, f
, independent
,
2071 SwapColumns(*neg
, nvar
+1, nvar
+1+i
);
2081 static bool double_bound_pair(Polyhedron
*P
, int nvar
, int exist
,
2082 int i
, int l1
, int l2
,
2083 Polyhedron
**pos
, Polyhedron
**neg
)
2087 Vector
*row
= Vector_Alloc(P
->Dimension
+2);
2088 value_set_si(row
->p
[0], 1);
2089 value_oppose(f
, P
->Constraint
[l1
][nvar
+i
+1]);
2090 Vector_Combine(P
->Constraint
[l1
]+1, P
->Constraint
[l2
]+1,
2092 P
->Constraint
[l2
][nvar
+i
+1], f
,
2094 ConstraintSimplify(row
->p
, row
->p
, P
->Dimension
+2, &f
);
2095 *pos
= AddConstraints(row
->p
, 1, P
, 0);
2096 value_set_si(f
, -1);
2097 Vector_Scale(row
->p
+1, row
->p
+1, f
, P
->Dimension
+1);
2098 value_decrement(row
->p
[P
->Dimension
+1], row
->p
[P
->Dimension
+1]);
2099 *neg
= AddConstraints(row
->p
, 1, P
, 0);
2103 return !emptyQ((*pos
)) && !emptyQ((*neg
));
2106 static bool double_bound(Polyhedron
*P
, int nvar
, int exist
,
2107 Polyhedron
**pos
, Polyhedron
**neg
)
2109 for (int i
= 0; i
< exist
; ++i
) {
2111 for (l1
= P
->NbEq
; l1
< P
->NbConstraints
; ++l1
) {
2112 if (value_negz_p(P
->Constraint
[l1
][nvar
+i
+1]))
2114 for (l2
= l1
+ 1; l2
< P
->NbConstraints
; ++l2
) {
2115 if (value_negz_p(P
->Constraint
[l2
][nvar
+i
+1]))
2117 if (double_bound_pair(P
, nvar
, exist
, i
, l1
, l2
, pos
, neg
))
2121 for (l1
= P
->NbEq
; l1
< P
->NbConstraints
; ++l1
) {
2122 if (value_posz_p(P
->Constraint
[l1
][nvar
+i
+1]))
2124 if (l1
< P
->NbConstraints
)
2125 for (l2
= l1
+ 1; l2
< P
->NbConstraints
; ++l2
) {
2126 if (value_posz_p(P
->Constraint
[l2
][nvar
+i
+1]))
2128 if (double_bound_pair(P
, nvar
, exist
, i
, l1
, l2
, pos
, neg
))
2140 INDEPENDENT
= 1 << 2
2143 static evalue
* enumerate_or(Polyhedron
*D
,
2144 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
2147 fprintf(stderr
, "\nER: Or\n");
2148 #endif /* DEBUG_ER */
2150 Polyhedron
*N
= D
->next
;
2153 barvinok_enumerate_e(D
, exist
, nparam
, MaxRays
);
2156 for (D
= N
; D
; D
= N
) {
2161 barvinok_enumerate_e(D
, exist
, nparam
, MaxRays
);
2164 free_evalue_refs(EN
);
2174 static evalue
* enumerate_sum(Polyhedron
*P
,
2175 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
2177 int nvar
= P
->Dimension
- exist
- nparam
;
2178 int toswap
= nvar
< exist
? nvar
: exist
;
2179 for (int i
= 0; i
< toswap
; ++i
)
2180 SwapColumns(P
, 1 + i
, nvar
+exist
- i
);
2184 fprintf(stderr
, "\nER: Sum\n");
2185 #endif /* DEBUG_ER */
2187 evalue
*EP
= barvinok_enumerate_e(P
, exist
, nparam
, MaxRays
);
2189 for (int i
= 0; i
< /* nvar */ nparam
; ++i
) {
2190 Matrix
*C
= Matrix_Alloc(1, 1 + nparam
+ 1);
2191 value_set_si(C
->p
[0][0], 1);
2193 value_init(split
.d
);
2194 value_set_si(split
.d
, 0);
2195 split
.x
.p
= new_enode(partition
, 4, nparam
);
2196 value_set_si(C
->p
[0][1+i
], 1);
2197 Matrix
*C2
= Matrix_Copy(C
);
2198 EVALUE_SET_DOMAIN(split
.x
.p
->arr
[0],
2199 Constraints2Polyhedron(C2
, MaxRays
));
2201 evalue_set_si(&split
.x
.p
->arr
[1], 1, 1);
2202 value_set_si(C
->p
[0][1+i
], -1);
2203 value_set_si(C
->p
[0][1+nparam
], -1);
2204 EVALUE_SET_DOMAIN(split
.x
.p
->arr
[2],
2205 Constraints2Polyhedron(C
, MaxRays
));
2206 evalue_set_si(&split
.x
.p
->arr
[3], 1, 1);
2208 free_evalue_refs(&split
);
2212 evalue_range_reduction(EP
);
2214 evalue_frac2floor(EP
);
2216 evalue
*sum
= esum(EP
, nvar
);
2218 free_evalue_refs(EP
);
2222 evalue_range_reduction(EP
);
2227 static evalue
* split_sure(Polyhedron
*P
, Polyhedron
*S
,
2228 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
2230 int nvar
= P
->Dimension
- exist
- nparam
;
2232 Matrix
*M
= Matrix_Alloc(exist
, S
->Dimension
+2);
2233 for (int i
= 0; i
< exist
; ++i
)
2234 value_set_si(M
->p
[i
][nvar
+i
+1], 1);
2236 S
= DomainAddRays(S
, M
, MaxRays
);
2238 Polyhedron
*F
= DomainAddRays(P
, M
, MaxRays
);
2239 Polyhedron
*D
= DomainDifference(F
, S
, MaxRays
);
2241 D
= Disjoint_Domain(D
, 0, MaxRays
);
2246 M
= Matrix_Alloc(P
->Dimension
+1-exist
, P
->Dimension
+1);
2247 for (int j
= 0; j
< nvar
; ++j
)
2248 value_set_si(M
->p
[j
][j
], 1);
2249 for (int j
= 0; j
< nparam
+1; ++j
)
2250 value_set_si(M
->p
[nvar
+j
][nvar
+exist
+j
], 1);
2251 Polyhedron
*T
= Polyhedron_Image(S
, M
, MaxRays
);
2252 evalue
*EP
= barvinok_enumerate_e(T
, 0, nparam
, MaxRays
);
2257 for (Polyhedron
*Q
= D
; Q
; Q
= Q
->next
) {
2258 Polyhedron
*N
= Q
->next
;
2260 T
= DomainIntersection(P
, Q
, MaxRays
);
2261 evalue
*E
= barvinok_enumerate_e(T
, exist
, nparam
, MaxRays
);
2263 free_evalue_refs(E
);
2272 static evalue
* enumerate_sure(Polyhedron
*P
,
2273 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
2277 int nvar
= P
->Dimension
- exist
- nparam
;
2283 for (i
= 0; i
< exist
; ++i
) {
2284 Matrix
*M
= Matrix_Alloc(S
->NbConstraints
, S
->Dimension
+2);
2286 value_set_si(lcm
, 1);
2287 for (int j
= 0; j
< S
->NbConstraints
; ++j
) {
2288 if (value_negz_p(S
->Constraint
[j
][1+nvar
+i
]))
2290 if (value_one_p(S
->Constraint
[j
][1+nvar
+i
]))
2292 value_lcm(lcm
, S
->Constraint
[j
][1+nvar
+i
], &lcm
);
2295 for (int j
= 0; j
< S
->NbConstraints
; ++j
) {
2296 if (value_negz_p(S
->Constraint
[j
][1+nvar
+i
]))
2298 if (value_one_p(S
->Constraint
[j
][1+nvar
+i
]))
2300 value_division(f
, lcm
, S
->Constraint
[j
][1+nvar
+i
]);
2301 Vector_Scale(S
->Constraint
[j
], M
->p
[c
], f
, S
->Dimension
+2);
2302 value_substract(M
->p
[c
][S
->Dimension
+1],
2303 M
->p
[c
][S
->Dimension
+1],
2305 value_increment(M
->p
[c
][S
->Dimension
+1],
2306 M
->p
[c
][S
->Dimension
+1]);
2310 S
= AddConstraints(M
->p
[0], c
, S
, MaxRays
);
2325 fprintf(stderr
, "\nER: Sure\n");
2326 #endif /* DEBUG_ER */
2328 return split_sure(P
, S
, exist
, nparam
, MaxRays
);
2331 static evalue
* enumerate_sure2(Polyhedron
*P
,
2332 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
2334 int nvar
= P
->Dimension
- exist
- nparam
;
2336 for (r
= 0; r
< P
->NbRays
; ++r
)
2337 if (value_one_p(P
->Ray
[r
][0]) &&
2338 value_one_p(P
->Ray
[r
][P
->Dimension
+1]))
2344 Matrix
*M
= Matrix_Alloc(nvar
+ 1 + nparam
, P
->Dimension
+2);
2345 for (int i
= 0; i
< nvar
; ++i
)
2346 value_set_si(M
->p
[i
][1+i
], 1);
2347 for (int i
= 0; i
< nparam
; ++i
)
2348 value_set_si(M
->p
[i
+nvar
][1+nvar
+exist
+i
], 1);
2349 Vector_Copy(P
->Ray
[r
]+1+nvar
, M
->p
[nvar
+nparam
]+1+nvar
, exist
);
2350 value_set_si(M
->p
[nvar
+nparam
][0], 1);
2351 value_set_si(M
->p
[nvar
+nparam
][P
->Dimension
+1], 1);
2352 Polyhedron
* F
= Rays2Polyhedron(M
, MaxRays
);
2355 Polyhedron
*I
= DomainIntersection(F
, P
, MaxRays
);
2359 fprintf(stderr
, "\nER: Sure2\n");
2360 #endif /* DEBUG_ER */
2362 return split_sure(P
, I
, exist
, nparam
, MaxRays
);
2365 static evalue
* enumerate_cyclic(Polyhedron
*P
,
2366 unsigned exist
, unsigned nparam
,
2367 evalue
* EP
, int r
, int p
, unsigned MaxRays
)
2369 int nvar
= P
->Dimension
- exist
- nparam
;
2371 /* If EP in its fractional maps only contains references
2372 * to the remainder parameter with appropriate coefficients
2373 * then we could in principle avoid adding existentially
2374 * quantified variables to the validity domains.
2375 * We'd have to replace the remainder by m { p/m }
2376 * and multiply with an appropriate factor that is one
2377 * only in the appropriate range.
2378 * This last multiplication can be avoided if EP
2379 * has a single validity domain with no (further)
2380 * constraints on the remainder parameter
2383 Matrix
*CT
= Matrix_Alloc(nparam
+1, nparam
+3);
2384 Matrix
*M
= Matrix_Alloc(1, 1+nparam
+3);
2385 for (int j
= 0; j
< nparam
; ++j
)
2387 value_set_si(CT
->p
[j
][j
], 1);
2388 value_set_si(CT
->p
[p
][nparam
+1], 1);
2389 value_set_si(CT
->p
[nparam
][nparam
+2], 1);
2390 value_set_si(M
->p
[0][1+p
], -1);
2391 value_absolute(M
->p
[0][1+nparam
], P
->Ray
[0][1+nvar
+exist
+p
]);
2392 value_set_si(M
->p
[0][1+nparam
+1], 1);
2393 Polyhedron
*CEq
= Constraints2Polyhedron(M
, 1);
2395 addeliminatedparams_enum(EP
, CT
, CEq
, MaxRays
, nparam
);
2396 Polyhedron_Free(CEq
);
2402 static void enumerate_vd_add_ray(evalue
*EP
, Matrix
*Rays
, unsigned MaxRays
)
2404 if (value_notzero_p(EP
->d
))
2407 assert(EP
->x
.p
->type
== partition
);
2408 assert(EP
->x
.p
->pos
== EVALUE_DOMAIN(EP
->x
.p
->arr
[0])->Dimension
);
2409 for (int i
= 0; i
< EP
->x
.p
->size
/2; ++i
) {
2410 Polyhedron
*D
= EVALUE_DOMAIN(EP
->x
.p
->arr
[2*i
]);
2411 Polyhedron
*N
= DomainAddRays(D
, Rays
, MaxRays
);
2412 EVALUE_SET_DOMAIN(EP
->x
.p
->arr
[2*i
], N
);
2417 static evalue
* enumerate_line(Polyhedron
*P
,
2418 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
2424 fprintf(stderr
, "\nER: Line\n");
2425 #endif /* DEBUG_ER */
2427 int nvar
= P
->Dimension
- exist
- nparam
;
2429 for (i
= 0; i
< nparam
; ++i
)
2430 if (value_notzero_p(P
->Ray
[0][1+nvar
+exist
+i
]))
2433 for (j
= i
+1; j
< nparam
; ++j
)
2434 if (value_notzero_p(P
->Ray
[0][1+nvar
+exist
+i
]))
2436 assert(j
>= nparam
); // for now
2438 Matrix
*M
= Matrix_Alloc(2, P
->Dimension
+2);
2439 value_set_si(M
->p
[0][0], 1);
2440 value_set_si(M
->p
[0][1+nvar
+exist
+i
], 1);
2441 value_set_si(M
->p
[1][0], 1);
2442 value_set_si(M
->p
[1][1+nvar
+exist
+i
], -1);
2443 value_absolute(M
->p
[1][1+P
->Dimension
], P
->Ray
[0][1+nvar
+exist
+i
]);
2444 value_decrement(M
->p
[1][1+P
->Dimension
], M
->p
[1][1+P
->Dimension
]);
2445 Polyhedron
*S
= AddConstraints(M
->p
[0], 2, P
, MaxRays
);
2446 evalue
*EP
= barvinok_enumerate_e(S
, exist
, nparam
, MaxRays
);
2450 return enumerate_cyclic(P
, exist
, nparam
, EP
, 0, i
, MaxRays
);
2453 static int single_param_pos(Polyhedron
*P
, unsigned exist
, unsigned nparam
,
2456 int nvar
= P
->Dimension
- exist
- nparam
;
2457 if (First_Non_Zero(P
->Ray
[r
]+1, nvar
) != -1)
2459 int i
= First_Non_Zero(P
->Ray
[r
]+1+nvar
+exist
, nparam
);
2462 if (First_Non_Zero(P
->Ray
[r
]+1+nvar
+exist
+1, nparam
-i
-1) != -1)
2467 static evalue
* enumerate_remove_ray(Polyhedron
*P
, int r
,
2468 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
2471 fprintf(stderr
, "\nER: RedundantRay\n");
2472 #endif /* DEBUG_ER */
2476 value_set_si(one
, 1);
2477 int len
= P
->NbRays
-1;
2478 Matrix
*M
= Matrix_Alloc(2 * len
, P
->Dimension
+2);
2479 Vector_Copy(P
->Ray
[0], M
->p
[0], r
* (P
->Dimension
+2));
2480 Vector_Copy(P
->Ray
[r
+1], M
->p
[r
], (len
-r
) * (P
->Dimension
+2));
2481 for (int j
= 0; j
< P
->NbRays
; ++j
) {
2484 Vector_Combine(P
->Ray
[j
], P
->Ray
[r
], M
->p
[len
+j
-(j
>r
)],
2485 one
, P
->Ray
[j
][P
->Dimension
+1], P
->Dimension
+2);
2488 P
= Rays2Polyhedron(M
, MaxRays
);
2490 evalue
*EP
= barvinok_enumerate_e(P
, exist
, nparam
, MaxRays
);
2497 static evalue
* enumerate_redundant_ray(Polyhedron
*P
,
2498 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
2500 assert(P
->NbBid
== 0);
2501 int nvar
= P
->Dimension
- exist
- nparam
;
2505 for (int r
= 0; r
< P
->NbRays
; ++r
) {
2506 if (value_notzero_p(P
->Ray
[r
][P
->Dimension
+1]))
2508 int i1
= single_param_pos(P
, exist
, nparam
, r
);
2511 for (int r2
= r
+1; r2
< P
->NbRays
; ++r2
) {
2512 if (value_notzero_p(P
->Ray
[r2
][P
->Dimension
+1]))
2514 int i2
= single_param_pos(P
, exist
, nparam
, r2
);
2520 value_division(m
, P
->Ray
[r
][1+nvar
+exist
+i1
],
2521 P
->Ray
[r2
][1+nvar
+exist
+i1
]);
2522 value_multiply(m
, m
, P
->Ray
[r2
][1+nvar
+exist
+i1
]);
2523 /* r2 divides r => r redundant */
2524 if (value_eq(m
, P
->Ray
[r
][1+nvar
+exist
+i1
])) {
2526 return enumerate_remove_ray(P
, r
, exist
, nparam
, MaxRays
);
2529 value_division(m
, P
->Ray
[r2
][1+nvar
+exist
+i1
],
2530 P
->Ray
[r
][1+nvar
+exist
+i1
]);
2531 value_multiply(m
, m
, P
->Ray
[r
][1+nvar
+exist
+i1
]);
2532 /* r divides r2 => r2 redundant */
2533 if (value_eq(m
, P
->Ray
[r2
][1+nvar
+exist
+i1
])) {
2535 return enumerate_remove_ray(P
, r2
, exist
, nparam
, MaxRays
);
2543 static Polyhedron
*upper_bound(Polyhedron
*P
,
2544 int pos
, Value
*max
, Polyhedron
**R
)
2553 for (Polyhedron
*Q
= P
; Q
; Q
= N
) {
2555 for (r
= 0; r
< P
->NbRays
; ++r
) {
2556 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1]) &&
2557 value_pos_p(P
->Ray
[r
][1+pos
]))
2560 if (r
< P
->NbRays
) {
2568 for (r
= 0; r
< P
->NbRays
; ++r
) {
2569 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1]))
2571 mpz_fdiv_q(v
, P
->Ray
[r
][1+pos
], P
->Ray
[r
][1+P
->Dimension
]);
2572 if ((!Q
->next
&& r
== 0) || value_gt(v
, *max
))
2573 value_assign(*max
, v
);
2580 static evalue
* enumerate_ray(Polyhedron
*P
,
2581 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
2583 assert(P
->NbBid
== 0);
2584 int nvar
= P
->Dimension
- exist
- nparam
;
2587 for (r
= 0; r
< P
->NbRays
; ++r
)
2588 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1]))
2594 for (r2
= r
+1; r2
< P
->NbRays
; ++r2
)
2595 if (value_zero_p(P
->Ray
[r2
][P
->Dimension
+1]))
2597 if (r2
< P
->NbRays
) {
2599 return enumerate_sum(P
, exist
, nparam
, MaxRays
);
2603 fprintf(stderr
, "\nER: Ray\n");
2604 #endif /* DEBUG_ER */
2610 value_set_si(one
, 1);
2611 int i
= single_param_pos(P
, exist
, nparam
, r
);
2612 assert(i
!= -1); // for now;
2614 Matrix
*M
= Matrix_Alloc(P
->NbRays
, P
->Dimension
+2);
2615 for (int j
= 0; j
< P
->NbRays
; ++j
) {
2616 Vector_Combine(P
->Ray
[j
], P
->Ray
[r
], M
->p
[j
],
2617 one
, P
->Ray
[j
][P
->Dimension
+1], P
->Dimension
+2);
2619 Polyhedron
*S
= Rays2Polyhedron(M
, MaxRays
);
2621 Polyhedron
*D
= DomainDifference(P
, S
, MaxRays
);
2623 // Polyhedron_Print(stderr, P_VALUE_FMT, D);
2624 assert(value_pos_p(P
->Ray
[r
][1+nvar
+exist
+i
])); // for now
2626 D
= upper_bound(D
, nvar
+exist
+i
, &m
, &R
);
2630 M
= Matrix_Alloc(2, P
->Dimension
+2);
2631 value_set_si(M
->p
[0][0], 1);
2632 value_set_si(M
->p
[1][0], 1);
2633 value_set_si(M
->p
[0][1+nvar
+exist
+i
], -1);
2634 value_set_si(M
->p
[1][1+nvar
+exist
+i
], 1);
2635 value_assign(M
->p
[0][1+P
->Dimension
], m
);
2636 value_oppose(M
->p
[1][1+P
->Dimension
], m
);
2637 value_addto(M
->p
[1][1+P
->Dimension
], M
->p
[1][1+P
->Dimension
],
2638 P
->Ray
[r
][1+nvar
+exist
+i
]);
2639 value_decrement(M
->p
[1][1+P
->Dimension
], M
->p
[1][1+P
->Dimension
]);
2640 // Matrix_Print(stderr, P_VALUE_FMT, M);
2641 D
= AddConstraints(M
->p
[0], 2, P
, MaxRays
);
2642 // Polyhedron_Print(stderr, P_VALUE_FMT, D);
2643 value_substract(M
->p
[0][1+P
->Dimension
], M
->p
[0][1+P
->Dimension
],
2644 P
->Ray
[r
][1+nvar
+exist
+i
]);
2645 // Matrix_Print(stderr, P_VALUE_FMT, M);
2646 S
= AddConstraints(M
->p
[0], 1, P
, MaxRays
);
2647 // Polyhedron_Print(stderr, P_VALUE_FMT, S);
2650 evalue
*EP
= barvinok_enumerate_e(D
, exist
, nparam
, MaxRays
);
2655 if (value_notone_p(P
->Ray
[r
][1+nvar
+exist
+i
]))
2656 EP
= enumerate_cyclic(P
, exist
, nparam
, EP
, r
, i
, MaxRays
);
2658 M
= Matrix_Alloc(1, nparam
+2);
2659 value_set_si(M
->p
[0][0], 1);
2660 value_set_si(M
->p
[0][1+i
], 1);
2661 enumerate_vd_add_ray(EP
, M
, MaxRays
);
2666 evalue
*E
= barvinok_enumerate_e(S
, exist
, nparam
, MaxRays
);
2668 free_evalue_refs(E
);
2675 evalue
*ER
= enumerate_or(R
, exist
, nparam
, MaxRays
);
2677 free_evalue_refs(ER
);
2684 static evalue
* new_zero_ep()
2689 evalue_set_si(EP
, 0, 1);
2693 static evalue
* enumerate_vd(Polyhedron
**PA
,
2694 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
2696 Polyhedron
*P
= *PA
;
2697 int nvar
= P
->Dimension
- exist
- nparam
;
2698 Param_Polyhedron
*PP
= NULL
;
2699 Polyhedron
*C
= Universe_Polyhedron(nparam
);
2703 PP
= Polyhedron2Param_SimplifiedDomain(&PR
,C
,MaxRays
,&CEq
,&CT
);
2707 Param_Domain
*D
, *last
;
2710 for (nd
= 0, D
=PP
->D
; D
; D
=D
->next
, ++nd
)
2713 Polyhedron
**VD
= new Polyhedron_p
[nd
];
2714 Polyhedron
**fVD
= new Polyhedron_p
[nd
];
2715 for(nd
= 0, D
=PP
->D
; D
; D
=D
->next
) {
2716 Polyhedron
*rVD
= reduce_domain(D
->Domain
, CT
, CEq
,
2730 /* This doesn't seem to have any effect */
2732 Polyhedron
*CA
= align_context(VD
[0], P
->Dimension
, MaxRays
);
2734 P
= DomainIntersection(P
, CA
, MaxRays
);
2737 Polyhedron_Free(CA
);
2742 if (!EP
&& CT
->NbColumns
!= CT
->NbRows
) {
2743 Polyhedron
*CEqr
= DomainImage(CEq
, CT
, MaxRays
);
2744 Polyhedron
*CA
= align_context(CEqr
, PR
->Dimension
, MaxRays
);
2745 Polyhedron
*I
= DomainIntersection(PR
, CA
, MaxRays
);
2746 Polyhedron_Free(CEqr
);
2747 Polyhedron_Free(CA
);
2749 fprintf(stderr
, "\nER: Eliminate\n");
2750 #endif /* DEBUG_ER */
2751 nparam
-= CT
->NbColumns
- CT
->NbRows
;
2752 EP
= barvinok_enumerate_e(I
, exist
, nparam
, MaxRays
);
2753 nparam
+= CT
->NbColumns
- CT
->NbRows
;
2754 addeliminatedparams_enum(EP
, CT
, CEq
, MaxRays
, nparam
);
2758 Polyhedron_Free(PR
);
2761 if (!EP
&& nd
> 1) {
2763 fprintf(stderr
, "\nER: VD\n");
2764 #endif /* DEBUG_ER */
2765 for (int i
= 0; i
< nd
; ++i
) {
2766 Polyhedron
*CA
= align_context(VD
[i
], P
->Dimension
, MaxRays
);
2767 Polyhedron
*I
= DomainIntersection(P
, CA
, MaxRays
);
2770 EP
= barvinok_enumerate_e(I
, exist
, nparam
, MaxRays
);
2772 evalue
*E
= barvinok_enumerate_e(I
, exist
, nparam
, MaxRays
);
2774 free_evalue_refs(E
);
2778 Polyhedron_Free(CA
);
2782 for (int i
= 0; i
< nd
; ++i
) {
2783 Polyhedron_Free(VD
[i
]);
2784 Polyhedron_Free(fVD
[i
]);
2790 if (!EP
&& nvar
== 0) {
2793 Param_Vertices
*V
, *V2
;
2794 Matrix
* M
= Matrix_Alloc(1, P
->Dimension
+2);
2796 FORALL_PVertex_in_ParamPolyhedron(V
, last
, PP
) {
2798 FORALL_PVertex_in_ParamPolyhedron(V2
, last
, PP
) {
2805 for (int i
= 0; i
< exist
; ++i
) {
2806 value_oppose(f
, V
->Vertex
->p
[i
][nparam
+1]);
2807 Vector_Combine(V
->Vertex
->p
[i
],
2809 M
->p
[0] + 1 + nvar
+ exist
,
2810 V2
->Vertex
->p
[i
][nparam
+1],
2814 for (j
= 0; j
< nparam
; ++j
)
2815 if (value_notzero_p(M
->p
[0][1+nvar
+exist
+j
]))
2819 ConstraintSimplify(M
->p
[0], M
->p
[0],
2820 P
->Dimension
+2, &f
);
2821 value_set_si(M
->p
[0][0], 0);
2822 Polyhedron
*para
= AddConstraints(M
->p
[0], 1, P
,
2825 Polyhedron_Free(para
);
2828 Polyhedron
*pos
, *neg
;
2829 value_set_si(M
->p
[0][0], 1);
2830 value_decrement(M
->p
[0][P
->Dimension
+1],
2831 M
->p
[0][P
->Dimension
+1]);
2832 neg
= AddConstraints(M
->p
[0], 1, P
, MaxRays
);
2833 value_set_si(f
, -1);
2834 Vector_Scale(M
->p
[0]+1, M
->p
[0]+1, f
,
2836 value_decrement(M
->p
[0][P
->Dimension
+1],
2837 M
->p
[0][P
->Dimension
+1]);
2838 value_decrement(M
->p
[0][P
->Dimension
+1],
2839 M
->p
[0][P
->Dimension
+1]);
2840 pos
= AddConstraints(M
->p
[0], 1, P
, MaxRays
);
2841 if (emptyQ(neg
) && emptyQ(pos
)) {
2842 Polyhedron_Free(para
);
2843 Polyhedron_Free(pos
);
2844 Polyhedron_Free(neg
);
2848 fprintf(stderr
, "\nER: Order\n");
2849 #endif /* DEBUG_ER */
2850 EP
= barvinok_enumerate_e(para
, exist
, nparam
, MaxRays
);
2853 E
= barvinok_enumerate_e(pos
, exist
, nparam
, MaxRays
);
2855 free_evalue_refs(E
);
2859 E
= barvinok_enumerate_e(neg
, exist
, nparam
, MaxRays
);
2861 free_evalue_refs(E
);
2864 Polyhedron_Free(para
);
2865 Polyhedron_Free(pos
);
2866 Polyhedron_Free(neg
);
2871 } END_FORALL_PVertex_in_ParamPolyhedron
;
2874 } END_FORALL_PVertex_in_ParamPolyhedron
;
2877 /* Search for vertex coordinate to split on */
2878 /* First look for one independent of the parameters */
2879 FORALL_PVertex_in_ParamPolyhedron(V
, last
, PP
) {
2880 for (int i
= 0; i
< exist
; ++i
) {
2882 for (j
= 0; j
< nparam
; ++j
)
2883 if (value_notzero_p(V
->Vertex
->p
[i
][j
]))
2887 value_set_si(M
->p
[0][0], 1);
2888 Vector_Set(M
->p
[0]+1, 0, nvar
+exist
);
2889 Vector_Copy(V
->Vertex
->p
[i
],
2890 M
->p
[0] + 1 + nvar
+ exist
, nparam
+1);
2891 value_oppose(M
->p
[0][1+nvar
+i
],
2892 V
->Vertex
->p
[i
][nparam
+1]);
2894 Polyhedron
*pos
, *neg
;
2895 value_set_si(M
->p
[0][0], 1);
2896 value_decrement(M
->p
[0][P
->Dimension
+1],
2897 M
->p
[0][P
->Dimension
+1]);
2898 neg
= AddConstraints(M
->p
[0], 1, P
, MaxRays
);
2899 value_set_si(f
, -1);
2900 Vector_Scale(M
->p
[0]+1, M
->p
[0]+1, f
,
2902 value_decrement(M
->p
[0][P
->Dimension
+1],
2903 M
->p
[0][P
->Dimension
+1]);
2904 value_decrement(M
->p
[0][P
->Dimension
+1],
2905 M
->p
[0][P
->Dimension
+1]);
2906 pos
= AddConstraints(M
->p
[0], 1, P
, MaxRays
);
2907 if (emptyQ(neg
) || emptyQ(pos
)) {
2908 Polyhedron_Free(pos
);
2909 Polyhedron_Free(neg
);
2912 Polyhedron_Free(pos
);
2913 value_increment(M
->p
[0][P
->Dimension
+1],
2914 M
->p
[0][P
->Dimension
+1]);
2915 pos
= AddConstraints(M
->p
[0], 1, P
, MaxRays
);
2917 fprintf(stderr
, "\nER: Vertex\n");
2918 #endif /* DEBUG_ER */
2920 EP
= enumerate_or(pos
, exist
, nparam
, MaxRays
);
2925 } END_FORALL_PVertex_in_ParamPolyhedron
;
2929 /* Search for vertex coordinate to split on */
2930 /* Now look for one that depends on the parameters */
2931 FORALL_PVertex_in_ParamPolyhedron(V
, last
, PP
) {
2932 for (int i
= 0; i
< exist
; ++i
) {
2933 value_set_si(M
->p
[0][0], 1);
2934 Vector_Set(M
->p
[0]+1, 0, nvar
+exist
);
2935 Vector_Copy(V
->Vertex
->p
[i
],
2936 M
->p
[0] + 1 + nvar
+ exist
, nparam
+1);
2937 value_oppose(M
->p
[0][1+nvar
+i
],
2938 V
->Vertex
->p
[i
][nparam
+1]);
2940 Polyhedron
*pos
, *neg
;
2941 value_set_si(M
->p
[0][0], 1);
2942 value_decrement(M
->p
[0][P
->Dimension
+1],
2943 M
->p
[0][P
->Dimension
+1]);
2944 neg
= AddConstraints(M
->p
[0], 1, P
, MaxRays
);
2945 value_set_si(f
, -1);
2946 Vector_Scale(M
->p
[0]+1, M
->p
[0]+1, f
,
2948 value_decrement(M
->p
[0][P
->Dimension
+1],
2949 M
->p
[0][P
->Dimension
+1]);
2950 value_decrement(M
->p
[0][P
->Dimension
+1],
2951 M
->p
[0][P
->Dimension
+1]);
2952 pos
= AddConstraints(M
->p
[0], 1, P
, MaxRays
);
2953 if (emptyQ(neg
) || emptyQ(pos
)) {
2954 Polyhedron_Free(pos
);
2955 Polyhedron_Free(neg
);
2958 Polyhedron_Free(pos
);
2959 value_increment(M
->p
[0][P
->Dimension
+1],
2960 M
->p
[0][P
->Dimension
+1]);
2961 pos
= AddConstraints(M
->p
[0], 1, P
, MaxRays
);
2963 fprintf(stderr
, "\nER: ParamVertex\n");
2964 #endif /* DEBUG_ER */
2966 EP
= enumerate_or(pos
, exist
, nparam
, MaxRays
);
2971 } END_FORALL_PVertex_in_ParamPolyhedron
;
2979 Polyhedron_Free(CEq
);
2983 Param_Polyhedron_Free(PP
);
2990 evalue
*barvinok_enumerate_pip(Polyhedron
*P
,
2991 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
2996 evalue
*barvinok_enumerate_pip(Polyhedron
*P
,
2997 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
2999 int nvar
= P
->Dimension
- exist
- nparam
;
3000 evalue
*EP
= new_zero_ep();
3001 Polyhedron
*Q
, *N
, *T
= 0;
3007 fprintf(stderr
, "\nER: PIP\n");
3008 #endif /* DEBUG_ER */
3010 for (int i
= 0; i
< P
->Dimension
; ++i
) {
3013 bool posray
= false;
3014 bool negray
= false;
3015 value_set_si(min
, 0);
3016 for (int j
= 0; j
< P
->NbRays
; ++j
) {
3017 if (value_pos_p(P
->Ray
[j
][1+i
])) {
3019 if (value_zero_p(P
->Ray
[j
][1+P
->Dimension
]))
3021 } else if (value_neg_p(P
->Ray
[j
][1+i
])) {
3023 if (value_zero_p(P
->Ray
[j
][1+P
->Dimension
]))
3027 P
->Ray
[j
][1+i
], P
->Ray
[j
][1+P
->Dimension
]);
3028 if (value_lt(tmp
, min
))
3029 value_assign(min
, tmp
);
3034 assert(!(posray
&& negray
));
3035 assert(!negray
); // for now
3036 Polyhedron
*O
= T
? T
: P
;
3037 /* shift by a safe amount */
3038 Matrix
*M
= Matrix_Alloc(O
->NbRays
, O
->Dimension
+2);
3039 Vector_Copy(O
->Ray
[0], M
->p
[0], O
->NbRays
* (O
->Dimension
+2));
3040 for (int j
= 0; j
< P
->NbRays
; ++j
) {
3041 if (value_notzero_p(M
->p
[j
][1+P
->Dimension
])) {
3042 value_multiply(tmp
, min
, M
->p
[j
][1+P
->Dimension
]);
3043 value_substract(M
->p
[j
][1+i
], M
->p
[j
][1+i
], tmp
);
3048 T
= Rays2Polyhedron(M
, MaxRays
);
3051 /* negating a parameter requires that we substitute in the
3052 * sign again afterwards.
3055 assert(i
< nvar
+exist
);
3057 T
= Polyhedron_Copy(P
);
3058 for (int j
= 0; j
< T
->NbRays
; ++j
)
3059 value_oppose(T
->Ray
[j
][1+i
], T
->Ray
[j
][1+i
]);
3060 for (int j
= 0; j
< T
->NbConstraints
; ++j
)
3061 value_oppose(T
->Constraint
[j
][1+i
], T
->Constraint
[j
][1+i
]);
3067 Polyhedron
*D
= pip_lexmin(T
? T
: P
, exist
, nparam
);
3068 for (Q
= D
; Q
; Q
= N
) {
3072 exist
= Q
->Dimension
- nvar
- nparam
;
3073 E
= barvinok_enumerate_e(Q
, exist
, nparam
, MaxRays
);
3076 free_evalue_refs(E
);
3088 static bool is_single(Value
*row
, int pos
, int len
)
3090 return First_Non_Zero(row
, pos
) == -1 &&
3091 First_Non_Zero(row
+pos
+1, len
-pos
-1) == -1;
3094 static evalue
* barvinok_enumerate_e_r(Polyhedron
*P
,
3095 unsigned exist
, unsigned nparam
, unsigned MaxRays
);
3098 static int er_level
= 0;
3100 evalue
* barvinok_enumerate_e(Polyhedron
*P
,
3101 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
3103 fprintf(stderr
, "\nER: level %i\n", er_level
);
3104 int nvar
= P
->Dimension
- exist
- nparam
;
3105 fprintf(stderr
, "%d %d %d\n", nvar
, exist
, nparam
);
3107 Polyhedron_Print(stderr
, P_VALUE_FMT
, P
);
3109 P
= DomainConstraintSimplify(Polyhedron_Copy(P
), MaxRays
);
3110 evalue
*EP
= barvinok_enumerate_e_r(P
, exist
, nparam
, MaxRays
);
3116 evalue
* barvinok_enumerate_e(Polyhedron
*P
,
3117 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
3119 P
= DomainConstraintSimplify(Polyhedron_Copy(P
), MaxRays
);
3120 evalue
*EP
= barvinok_enumerate_e_r(P
, exist
, nparam
, MaxRays
);
3126 static evalue
* barvinok_enumerate_e_r(Polyhedron
*P
,
3127 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
3130 Polyhedron
*U
= Universe_Polyhedron(nparam
);
3131 evalue
*EP
= barvinok_enumerate_ev(P
, U
, MaxRays
);
3132 //char *param_name[] = {"P", "Q", "R", "S", "T" };
3133 //print_evalue(stdout, EP, param_name);
3138 int nvar
= P
->Dimension
- exist
- nparam
;
3139 int len
= P
->Dimension
+ 2;
3142 return new_zero_ep();
3144 if (nvar
== 0 && nparam
== 0) {
3145 evalue
*EP
= new_zero_ep();
3146 barvinok_count(P
, &EP
->x
.n
, MaxRays
);
3147 if (value_pos_p(EP
->x
.n
))
3148 value_set_si(EP
->x
.n
, 1);
3153 for (r
= 0; r
< P
->NbRays
; ++r
)
3154 if (value_zero_p(P
->Ray
[r
][0]) ||
3155 value_zero_p(P
->Ray
[r
][P
->Dimension
+1])) {
3157 for (i
= 0; i
< nvar
; ++i
)
3158 if (value_notzero_p(P
->Ray
[r
][i
+1]))
3162 for (i
= nvar
+ exist
; i
< nvar
+ exist
+ nparam
; ++i
)
3163 if (value_notzero_p(P
->Ray
[r
][i
+1]))
3165 if (i
>= nvar
+ exist
+ nparam
)
3168 if (r
< P
->NbRays
) {
3169 evalue
*EP
= new_zero_ep();
3170 value_set_si(EP
->x
.n
, -1);
3175 for (r
= 0; r
< P
->NbEq
; ++r
)
3176 if ((first
= First_Non_Zero(P
->Constraint
[r
]+1+nvar
, exist
)) != -1)
3179 if (First_Non_Zero(P
->Constraint
[r
]+1+nvar
+first
+1,
3180 exist
-first
-1) != -1) {
3181 Polyhedron
*T
= rotate_along(P
, r
, nvar
, exist
, MaxRays
);
3183 fprintf(stderr
, "\nER: Equality\n");
3184 #endif /* DEBUG_ER */
3185 evalue
*EP
= barvinok_enumerate_e(T
, exist
-1, nparam
, MaxRays
);
3190 fprintf(stderr
, "\nER: Fixed\n");
3191 #endif /* DEBUG_ER */
3193 return barvinok_enumerate_e(P
, exist
-1, nparam
, MaxRays
);
3195 Polyhedron
*T
= Polyhedron_Copy(P
);
3196 SwapColumns(T
, nvar
+1, nvar
+1+first
);
3197 evalue
*EP
= barvinok_enumerate_e(T
, exist
-1, nparam
, MaxRays
);
3204 Vector
*row
= Vector_Alloc(len
);
3205 value_set_si(row
->p
[0], 1);
3210 enum constraint
* info
= new constraint
[exist
];
3211 for (int i
= 0; i
< exist
; ++i
) {
3213 for (int l
= P
->NbEq
; l
< P
->NbConstraints
; ++l
) {
3214 if (value_negz_p(P
->Constraint
[l
][nvar
+i
+1]))
3216 bool l_parallel
= is_single(P
->Constraint
[l
]+nvar
+1, i
, exist
);
3217 for (int u
= P
->NbEq
; u
< P
->NbConstraints
; ++u
) {
3218 if (value_posz_p(P
->Constraint
[u
][nvar
+i
+1]))
3220 bool lu_parallel
= l_parallel
||
3221 is_single(P
->Constraint
[u
]+nvar
+1, i
, exist
);
3222 value_oppose(f
, P
->Constraint
[u
][nvar
+i
+1]);
3223 Vector_Combine(P
->Constraint
[l
]+1, P
->Constraint
[u
]+1, row
->p
+1,
3224 f
, P
->Constraint
[l
][nvar
+i
+1], len
-1);
3225 if (!(info
[i
] & INDEPENDENT
)) {
3227 for (j
= 0; j
< exist
; ++j
)
3228 if (j
!= i
&& value_notzero_p(row
->p
[nvar
+j
+1]))
3231 //printf("independent: i: %d, l: %d, u: %d\n", i, l, u);
3232 info
[i
] = (constraint
)(info
[i
] | INDEPENDENT
);
3235 if (info
[i
] & ALL_POS
) {
3236 value_addto(row
->p
[len
-1], row
->p
[len
-1],
3237 P
->Constraint
[l
][nvar
+i
+1]);
3238 value_addto(row
->p
[len
-1], row
->p
[len
-1], f
);
3239 value_multiply(f
, f
, P
->Constraint
[l
][nvar
+i
+1]);
3240 value_substract(row
->p
[len
-1], row
->p
[len
-1], f
);
3241 value_decrement(row
->p
[len
-1], row
->p
[len
-1]);
3242 ConstraintSimplify(row
->p
, row
->p
, len
, &f
);
3243 value_set_si(f
, -1);
3244 Vector_Scale(row
->p
+1, row
->p
+1, f
, len
-1);
3245 value_decrement(row
->p
[len
-1], row
->p
[len
-1]);
3246 Polyhedron
*T
= AddConstraints(row
->p
, 1, P
, MaxRays
);
3248 //printf("not all_pos: i: %d, l: %d, u: %d\n", i, l, u);
3249 info
[i
] = (constraint
)(info
[i
] ^ ALL_POS
);
3251 //puts("pos remainder");
3252 //Polyhedron_Print(stdout, P_VALUE_FMT, T);
3255 if (!(info
[i
] & ONE_NEG
)) {
3257 negative_test_constraint(P
->Constraint
[l
],
3259 row
->p
, nvar
+i
, len
, &f
);
3260 oppose_constraint(row
->p
, len
, &f
);
3261 Polyhedron
*T
= AddConstraints(row
->p
, 1, P
, MaxRays
);
3263 //printf("one_neg i: %d, l: %d, u: %d\n", i, l, u);
3264 info
[i
] = (constraint
)(info
[i
] | ONE_NEG
);
3266 //puts("neg remainder");
3267 //Polyhedron_Print(stdout, P_VALUE_FMT, T);
3271 if (!(info
[i
] & ALL_POS
) && (info
[i
] & ONE_NEG
))
3275 if (info
[i
] & ALL_POS
)
3282 for (int i = 0; i < exist; ++i)
3283 printf("%i: %i\n", i, info[i]);
3285 for (int i
= 0; i
< exist
; ++i
)
3286 if (info
[i
] & ALL_POS
) {
3288 fprintf(stderr
, "\nER: Positive\n");
3289 #endif /* DEBUG_ER */
3291 // Maybe we should chew off some of the fat here
3292 Matrix
*M
= Matrix_Alloc(P
->Dimension
, P
->Dimension
+1);
3293 for (int j
= 0; j
< P
->Dimension
; ++j
)
3294 value_set_si(M
->p
[j
][j
+ (j
>= i
+nvar
)], 1);
3295 Polyhedron
*T
= Polyhedron_Image(P
, M
, MaxRays
);
3297 evalue
*EP
= barvinok_enumerate_e(T
, exist
-1, nparam
, MaxRays
);
3304 for (int i
= 0; i
< exist
; ++i
)
3305 if (info
[i
] & ONE_NEG
) {
3307 fprintf(stderr
, "\nER: Negative\n");
3308 #endif /* DEBUG_ER */
3313 return barvinok_enumerate_e(P
, exist
-1, nparam
, MaxRays
);
3315 Polyhedron
*T
= Polyhedron_Copy(P
);
3316 SwapColumns(T
, nvar
+1, nvar
+1+i
);
3317 evalue
*EP
= barvinok_enumerate_e(T
, exist
-1, nparam
, MaxRays
);
3322 for (int i
= 0; i
< exist
; ++i
)
3323 if (info
[i
] & INDEPENDENT
) {
3324 Polyhedron
*pos
, *neg
;
3326 /* Find constraint again and split off negative part */
3328 if (SplitOnVar(P
, i
, nvar
, len
, exist
, MaxRays
,
3329 row
, f
, true, &pos
, &neg
)) {
3331 fprintf(stderr
, "\nER: Split\n");
3332 #endif /* DEBUG_ER */
3335 barvinok_enumerate_e(neg
, exist
-1, nparam
, MaxRays
);
3337 barvinok_enumerate_e(pos
, exist
, nparam
, MaxRays
);
3339 free_evalue_refs(E
);
3341 Polyhedron_Free(neg
);
3342 Polyhedron_Free(pos
);
3356 EP
= enumerate_line(P
, exist
, nparam
, MaxRays
);
3360 EP
= barvinok_enumerate_pip(P
, exist
, nparam
, MaxRays
);
3364 EP
= enumerate_redundant_ray(P
, exist
, nparam
, MaxRays
);
3368 EP
= enumerate_sure(P
, exist
, nparam
, MaxRays
);
3372 EP
= enumerate_ray(P
, exist
, nparam
, MaxRays
);
3376 EP
= enumerate_sure2(P
, exist
, nparam
, MaxRays
);
3380 F
= unfringe(P
, MaxRays
);
3381 if (!PolyhedronIncludes(F
, P
)) {
3383 fprintf(stderr
, "\nER: Fringed\n");
3384 #endif /* DEBUG_ER */
3385 EP
= barvinok_enumerate_e(F
, exist
, nparam
, MaxRays
);
3392 EP
= enumerate_vd(&P
, exist
, nparam
, MaxRays
);
3397 EP
= enumerate_sum(P
, exist
, nparam
, MaxRays
);
3404 Polyhedron
*pos
, *neg
;
3405 for (i
= 0; i
< exist
; ++i
)
3406 if (SplitOnVar(P
, i
, nvar
, len
, exist
, MaxRays
,
3407 row
, f
, false, &pos
, &neg
))
3413 EP
= enumerate_or(pos
, exist
, nparam
, MaxRays
);
3425 static void normalize(Polyhedron
*i
, vec_ZZ
& lambda
, ZZ
& sign
,
3426 ZZ
& num_s
, vec_ZZ
& num_p
, vec_ZZ
& den_s
, vec_ZZ
& den_p
,
3429 unsigned dim
= i
->Dimension
;
3430 unsigned nparam
= num_p
.length();
3431 unsigned nvar
= dim
- nparam
;
3435 rays
.SetDims(dim
, nvar
);
3436 add_rays(rays
, i
, &r
, nvar
, true);
3437 den_s
= rays
* lambda
;
3441 for (int j
= 0; j
< den_s
.length(); ++j
) {
3442 values2zz(i
->Ray
[j
]+1+nvar
, f
[j
], nparam
);
3443 if (den_s
[j
] == 0) {
3447 if (First_Non_Zero(i
->Ray
[j
]+1+nvar
, nparam
) != -1) {
3458 den_s
[j
] = abs(den_s
[j
]);
3467 gen_fun
* barvinok_series(Polyhedron
*P
, Polyhedron
* C
, unsigned MaxRays
)
3469 Polyhedron
** vcone
;
3471 unsigned nparam
= C
->Dimension
;
3475 sign
.SetLength(ncone
);
3477 CA
= align_context(C
, P
->Dimension
, MaxRays
);
3478 P
= DomainIntersection(P
, CA
, MaxRays
);
3479 Polyhedron_Free(CA
);
3481 assert(!Polyhedron_is_infinite(P
, nparam
));
3482 assert(P
->NbBid
== 0);
3483 assert(Polyhedron_has_positive_rays(P
, nparam
));
3484 assert(P
->NbEq
== 0);
3487 nvar
= dim
- nparam
;
3488 vcone
= new Polyhedron_p
[P
->NbRays
];
3490 for (int j
= 0; j
< P
->NbRays
; ++j
) {
3491 if (!value_pos_p(P
->Ray
[j
][dim
+1]))
3495 Polyhedron
*C
= supporting_cone(P
, j
);
3496 decompose(C
, &vcone
[j
], &npos
, &nneg
, MaxRays
);
3497 ncone
+= npos
+ nneg
;
3498 sign
.SetLength(ncone
);
3499 for (int k
= 0; k
< npos
; ++k
)
3500 sign
[ncone
-nneg
-k
-1] = 1;
3501 for (int k
= 0; k
< nneg
; ++k
)
3502 sign
[ncone
-k
-1] = -1;
3506 rays
.SetDims(ncone
* dim
, nvar
);
3508 for (int j
= 0; j
< P
->NbRays
; ++j
) {
3509 if (!value_pos_p(P
->Ray
[j
][dim
+1]))
3512 for (Polyhedron
*i
= vcone
[j
]; i
; i
= i
->next
) {
3513 add_rays(rays
, i
, &r
, nvar
);
3516 rays
.SetDims(r
, nvar
);
3518 nonorthog(rays
, lambda
);
3519 //randomvector(P, lambda, nvar);
3522 cout << "rays: " << rays;
3523 cout << "lambda: " << lambda;
3529 num_p
.SetLength(nparam
);
3532 den_s
.SetLength(dim
);
3534 den_p
.SetLength(dim
);
3536 den
.SetDims(dim
, nparam
);
3542 gen_fun
* gf
= new gen_fun
;
3544 for (int j
= 0; j
< P
->NbRays
; ++j
) {
3545 if (!value_pos_p(P
->Ray
[j
][dim
+1]))
3548 for (Polyhedron
*i
= vcone
[j
]; i
; i
= i
->next
, ++f
) {
3549 lattice_point(P
->Ray
[j
]+1, i
, vertex
);
3552 for ( ; k
< nvar
; ++k
)
3553 num_s
+= vertex
[k
] * lambda
[k
];
3554 for ( ; k
< dim
; ++k
)
3555 num_p
[k
-nvar
] = vertex
[k
];
3556 normalize(i
, lambda
, sign
[f
], num_s
, num_p
,
3561 for (int k
= 0; k
< dim
; ++k
) {
3564 else if (den_s
[k
] == 0)
3567 if (no_param
== 0) {
3568 for (int k
= 0; k
< dim
; ++k
)
3571 gf
->add(sign
[f
], one
, num_p
, den
);
3572 } else if (no_param
+ only_param
== dim
) {
3575 pden
.SetDims(only_param
, nparam
);
3577 for (k
= 0, l
= 0; k
< dim
; ++k
)
3581 for (k
= 0; k
< dim
; ++k
)
3585 dpoly
n(no_param
, num_s
);
3586 dpoly
d(no_param
, den_s
[k
], 1);
3587 for ( ; k
< dim
; ++k
)
3588 if (den_s
[k
] != 0) {
3589 dpoly
fact(no_param
, den_s
[k
], 1);
3593 mpq_set_si(count
, 0, 1);
3594 n
.div(d
, count
, sign
[f
]);
3597 value2zz(mpq_numref(count
), qn
);
3598 value2zz(mpq_denref(count
), qd
);
3600 gf
->add(qn
, qd
, num_p
, pden
);
3605 pden
.SetDims(only_param
, nparam
);
3607 for (k
= 0, l
= 0; k
< dim
; ++k
)
3611 for (k
= 0; k
< dim
; ++k
)
3615 dpoly
n(no_param
, num_s
);
3616 dpoly
d(no_param
, den_s
[k
], 1);
3617 for ( ; k
< dim
; ++k
)
3618 if (den_p
[k
] == 0) {
3619 dpoly
fact(no_param
, den_s
[k
], 1);
3623 for (k
= 0; k
< dim
; ++k
) {
3624 if (den_s
[k
] == 0 || den_p
[k
] == 0)
3627 dpoly
pd(no_param
-1, den_s
[k
], 1);
3628 int s
= den_p
[k
] < 0 ? -1 : 1;
3631 r
= new dpoly_r(n
, pd
, k
, s
, dim
);
3633 assert(0); // for now
3636 r
->div(d
, sign
[f
], gf
, pden
, den
, num_p
);
3640 cout << "sign: " << sign[f];
3641 cout << "num_s: " << num_s;
3642 cout << "num_p: " << num_p;
3643 cout << "den_s: " << den_s;
3644 cout << "den_p: " << den_p;
3645 cout << "den: " << den;
3646 cout << "only_param: " << only_param;
3647 cout << "no_param: " << no_param;