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 * Barvinok's Decomposition of a simplicial cone
502 * Returns two lists of polyhedra
504 void barvinok_decompose(Polyhedron
*C
, Polyhedron
**ppos
, Polyhedron
**pneg
)
506 Polyhedron
*pos
= *ppos
, *neg
= *pneg
;
507 vector
<cone
*> nonuni
;
508 cone
* c
= new cone(C
);
515 Polyhedron
*p
= Polyhedron_Copy(c
->Cone
);
521 while (!nonuni
.empty()) {
524 Vector
* v
= c
->short_vector(lambda
);
525 for (int i
= 0; i
< c
->Rays
->NbRows
- 1; ++i
) {
528 Matrix
* M
= Matrix_Copy(c
->Rays
);
529 Vector_Copy(v
->p
, M
->p
[i
], v
->Size
);
530 cone
* pc
= new cone(M
);
531 assert (pc
->det
!= 0);
532 if (abs(pc
->det
) > 1) {
533 assert(abs(pc
->det
) < abs(c
->det
));
534 nonuni
.push_back(pc
);
536 Polyhedron
*p
= pc
->poly();
538 if (sign(pc
->det
) == s
) {
557 * Returns a single list of npos "positive" cones followed by nneg
559 * The input cone is freed
561 void decompose(Polyhedron
*cone
, Polyhedron
**parts
, int *npos
, int *nneg
, unsigned MaxRays
)
563 Polyhedron_Polarize(cone
);
564 if (cone
->NbRays
- 1 != cone
->Dimension
) {
565 Polyhedron
*tmp
= cone
;
566 cone
= triangularize_cone(cone
, MaxRays
);
567 Polyhedron_Free(tmp
);
569 Polyhedron
*polpos
= NULL
, *polneg
= NULL
;
570 *npos
= 0; *nneg
= 0;
571 for (Polyhedron
*Polar
= cone
; Polar
; Polar
= Polar
->next
)
572 barvinok_decompose(Polar
, &polpos
, &polneg
);
575 for (Polyhedron
*i
= polpos
; i
; i
= i
->next
) {
576 Polyhedron_Polarize(i
);
580 for (Polyhedron
*i
= polneg
; i
; i
= i
->next
) {
581 Polyhedron_Polarize(i
);
592 const int MAX_TRY
=10;
594 * Searches for a vector that is not orthogonal to any
595 * of the rays in rays.
597 static void nonorthog(mat_ZZ
& rays
, vec_ZZ
& lambda
)
599 int dim
= rays
.NumCols();
601 lambda
.SetLength(dim
);
605 for (int i
= 2; !found
&& i
<= 50*dim
; i
+=4) {
606 for (int j
= 0; j
< MAX_TRY
; ++j
) {
607 for (int k
= 0; k
< dim
; ++k
) {
608 int r
= random_int(i
)+2;
609 int v
= (2*(r
%2)-1) * (r
>> 1);
613 for (; k
< rays
.NumRows(); ++k
)
614 if (lambda
* rays
[k
] == 0)
616 if (k
== rays
.NumRows()) {
625 static void randomvector(Polyhedron
*P
, vec_ZZ
& lambda
, int nvar
)
629 unsigned int dim
= P
->Dimension
;
632 for (int i
= 0; i
< P
->NbRays
; ++i
) {
633 for (int j
= 1; j
<= dim
; ++j
) {
634 value_absolute(tmp
, P
->Ray
[i
][j
]);
635 int t
= VALUE_TO_LONG(tmp
);
640 for (int i
= 0; i
< P
->NbConstraints
; ++i
) {
641 for (int j
= 1; j
<= dim
; ++j
) {
642 value_absolute(tmp
, P
->Constraint
[i
][j
]);
643 int t
= VALUE_TO_LONG(tmp
);
650 lambda
.SetLength(nvar
);
651 for (int k
= 0; k
< nvar
; ++k
) {
652 int r
= random_int(8*max
*dim
)+2;
653 int v
= (2*(r
%2)-1) * (4*max
*dim
+ (r
>> 1));
658 static void add_rays(mat_ZZ
& rays
, Polyhedron
*i
, int *r
, int nvar
= -1,
661 unsigned dim
= i
->Dimension
;
664 for (int k
= 0; k
< i
->NbRays
; ++k
) {
665 if (!value_zero_p(i
->Ray
[k
][dim
+1]))
667 if (!all
&& nvar
!= dim
&& First_Non_Zero(i
->Ray
[k
]+1, nvar
) == -1)
669 values2zz(i
->Ray
[k
]+1, rays
[(*r
)++], nvar
);
673 void lattice_point(Value
* values
, Polyhedron
*i
, vec_ZZ
& vertex
)
675 unsigned dim
= i
->Dimension
;
676 if(!value_one_p(values
[dim
])) {
677 Matrix
* Rays
= rays(i
);
678 Matrix
*inv
= Matrix_Alloc(Rays
->NbRows
, Rays
->NbColumns
);
679 int ok
= Matrix_Inverse(Rays
, inv
);
683 Vector
*lambda
= Vector_Alloc(dim
+1);
684 Vector_Matrix_Product(values
, inv
, lambda
->p
);
686 for (int j
= 0; j
< dim
; ++j
)
687 mpz_cdiv_q(lambda
->p
[j
], lambda
->p
[j
], lambda
->p
[dim
]);
688 value_set_si(lambda
->p
[dim
], 1);
689 Vector
*A
= Vector_Alloc(dim
+1);
690 Vector_Matrix_Product(lambda
->p
, Rays
, A
->p
);
693 values2zz(A
->p
, vertex
, dim
);
696 values2zz(values
, vertex
, dim
);
699 static evalue
*term(int param
, ZZ
& c
, Value
*den
= NULL
)
701 evalue
*EP
= new evalue();
703 value_set_si(EP
->d
,0);
704 EP
->x
.p
= new_enode(polynomial
, 2, param
+ 1);
705 evalue_set_si(&EP
->x
.p
->arr
[0], 0, 1);
706 value_init(EP
->x
.p
->arr
[1].x
.n
);
708 value_set_si(EP
->x
.p
->arr
[1].d
, 1);
710 value_assign(EP
->x
.p
->arr
[1].d
, *den
);
711 zz2value(c
, EP
->x
.p
->arr
[1].x
.n
);
715 static void vertex_period(
716 Polyhedron
*i
, vec_ZZ
& lambda
, Matrix
*T
,
717 Value lcm
, int p
, Vector
*val
,
718 evalue
*E
, evalue
* ev
,
721 unsigned nparam
= T
->NbRows
- 1;
722 unsigned dim
= i
->Dimension
;
729 Vector
* values
= Vector_Alloc(dim
+ 1);
730 Vector_Matrix_Product(val
->p
, T
, values
->p
);
731 value_assign(values
->p
[dim
], lcm
);
732 lattice_point(values
->p
, i
, vertex
);
733 num
= vertex
* lambda
;
738 zz2value(num
, ev
->x
.n
);
739 value_assign(ev
->d
, lcm
);
746 values2zz(T
->p
[p
], vertex
, dim
);
747 nump
= vertex
* lambda
;
748 if (First_Non_Zero(val
->p
, p
) == -1) {
749 value_assign(tmp
, lcm
);
750 evalue
*ET
= term(p
, nump
, &tmp
);
752 free_evalue_refs(ET
);
756 value_assign(tmp
, lcm
);
757 if (First_Non_Zero(T
->p
[p
], dim
) != -1)
758 Vector_Gcd(T
->p
[p
], dim
, &tmp
);
760 if (value_lt(tmp
, lcm
)) {
763 value_division(tmp
, lcm
, tmp
);
764 value_set_si(ev
->d
, 0);
765 ev
->x
.p
= new_enode(periodic
, VALUE_TO_INT(tmp
), p
+1);
766 value2zz(tmp
, count
);
768 value_decrement(tmp
, tmp
);
770 ZZ new_offset
= offset
- count
* nump
;
771 value_assign(val
->p
[p
], tmp
);
772 vertex_period(i
, lambda
, T
, lcm
, p
+1, val
, E
,
773 &ev
->x
.p
->arr
[VALUE_TO_INT(tmp
)], new_offset
);
774 } while (value_pos_p(tmp
));
776 vertex_period(i
, lambda
, T
, lcm
, p
+1, val
, E
, ev
, offset
);
780 static void mask_r(Matrix
*f
, int nr
, Vector
*lcm
, int p
, Vector
*val
, evalue
*ev
)
782 unsigned nparam
= lcm
->Size
;
785 Vector
* prod
= Vector_Alloc(f
->NbRows
);
786 Matrix_Vector_Product(f
, val
->p
, prod
->p
);
788 for (int i
= 0; i
< nr
; ++i
) {
789 value_modulus(prod
->p
[i
], prod
->p
[i
], f
->p
[i
][nparam
+1]);
790 isint
&= value_zero_p(prod
->p
[i
]);
792 value_set_si(ev
->d
, 1);
794 value_set_si(ev
->x
.n
, isint
);
801 if (value_one_p(lcm
->p
[p
]))
802 mask_r(f
, nr
, lcm
, p
+1, val
, ev
);
804 value_assign(tmp
, lcm
->p
[p
]);
805 value_set_si(ev
->d
, 0);
806 ev
->x
.p
= new_enode(periodic
, VALUE_TO_INT(tmp
), p
+1);
808 value_decrement(tmp
, tmp
);
809 value_assign(val
->p
[p
], tmp
);
810 mask_r(f
, nr
, lcm
, p
+1, val
, &ev
->x
.p
->arr
[VALUE_TO_INT(tmp
)]);
811 } while (value_pos_p(tmp
));
816 static evalue
*multi_monom(vec_ZZ
& p
)
818 evalue
*X
= new evalue();
821 unsigned nparam
= p
.length()-1;
822 zz2value(p
[nparam
], X
->x
.n
);
823 value_set_si(X
->d
, 1);
824 for (int i
= 0; i
< nparam
; ++i
) {
827 evalue
*T
= term(i
, p
[i
]);
836 * Check whether mapping polyhedron P on the affine combination
837 * num yields a range that has a fixed quotient on integer
839 * If zero is true, then we are only interested in the quotient
840 * for the cases where the remainder is zero.
841 * Returns NULL if false and a newly allocated value if true.
843 static Value
*fixed_quotient(Polyhedron
*P
, vec_ZZ
& num
, Value d
, bool zero
)
846 int len
= num
.length();
847 Matrix
*T
= Matrix_Alloc(2, len
);
848 zz2values(num
, T
->p
[0]);
849 value_set_si(T
->p
[1][len
-1], 1);
850 Polyhedron
*I
= Polyhedron_Image(P
, T
, P
->NbConstraints
);
854 for (i
= 0; i
< I
->NbRays
; ++i
)
855 if (value_zero_p(I
->Ray
[i
][2])) {
863 int bounded
= line_minmax(I
, &min
, &max
);
867 mpz_cdiv_q(min
, min
, d
);
869 mpz_fdiv_q(min
, min
, d
);
870 mpz_fdiv_q(max
, max
, d
);
872 if (value_eq(min
, max
)) {
875 value_assign(*ret
, min
);
883 * Normalize linear expression coef modulo m
884 * Removes common factor and reduces coefficients
885 * Returns index of first non-zero coefficient or len
887 static int normal_mod(Value
*coef
, int len
, Value
*m
)
892 Vector_Gcd(coef
, len
, &gcd
);
894 Vector_AntiScale(coef
, coef
, gcd
, len
);
896 value_division(*m
, *m
, gcd
);
903 for (j
= 0; j
< len
; ++j
)
904 mpz_fdiv_r(coef
[j
], coef
[j
], *m
);
905 for (j
= 0; j
< len
; ++j
)
906 if (value_notzero_p(coef
[j
]))
913 static void mask(Matrix
*f
, evalue
*factor
)
915 int nr
= f
->NbRows
, nc
= f
->NbColumns
;
918 for (n
= 0; n
< nr
&& value_notzero_p(f
->p
[n
][nc
-1]); ++n
)
919 if (value_notone_p(f
->p
[n
][nc
-1]) &&
920 value_notmone_p(f
->p
[n
][nc
-1]))
934 value_set_si(EV
.x
.n
, 1);
936 for (n
= 0; n
< nr
; ++n
) {
937 value_assign(m
, f
->p
[n
][nc
-1]);
938 if (value_one_p(m
) || value_mone_p(m
))
941 int j
= normal_mod(f
->p
[n
], nc
-1, &m
);
943 free_evalue_refs(factor
);
944 value_init(factor
->d
);
945 evalue_set_si(factor
, 0, 1);
949 values2zz(f
->p
[n
], row
, nc
-1);
952 if (j
< (nc
-1)-1 && row
[j
] > g
/2) {
953 for (int k
= j
; k
< (nc
-1); ++k
)
959 value_set_si(EP
.d
, 0);
960 EP
.x
.p
= new_enode(relation
, 2, 0);
961 value_clear(EP
.x
.p
->arr
[1].d
);
962 EP
.x
.p
->arr
[1] = *factor
;
963 evalue
*ev
= &EP
.x
.p
->arr
[0];
964 value_set_si(ev
->d
, 0);
965 ev
->x
.p
= new_enode(fractional
, 3, -1);
966 evalue_set_si(&ev
->x
.p
->arr
[1], 0, 1);
967 evalue_set_si(&ev
->x
.p
->arr
[2], 1, 1);
968 evalue
*E
= multi_monom(row
);
969 value_assign(EV
.d
, m
);
971 value_clear(ev
->x
.p
->arr
[0].d
);
972 ev
->x
.p
->arr
[0] = *E
;
978 free_evalue_refs(&EV
);
984 static void mask(Matrix
*f
, evalue
*factor
)
986 int nr
= f
->NbRows
, nc
= f
->NbColumns
;
989 for (n
= 0; n
< nr
&& value_notzero_p(f
->p
[n
][nc
-1]); ++n
)
990 if (value_notone_p(f
->p
[n
][nc
-1]) &&
991 value_notmone_p(f
->p
[n
][nc
-1]))
999 unsigned np
= nc
- 2;
1000 Vector
*lcm
= Vector_Alloc(np
);
1001 Vector
*val
= Vector_Alloc(nc
);
1002 Vector_Set(val
->p
, 0, nc
);
1003 value_set_si(val
->p
[np
], 1);
1004 Vector_Set(lcm
->p
, 1, np
);
1005 for (n
= 0; n
< nr
; ++n
) {
1006 if (value_one_p(f
->p
[n
][nc
-1]) ||
1007 value_mone_p(f
->p
[n
][nc
-1]))
1009 for (int j
= 0; j
< np
; ++j
)
1010 if (value_notzero_p(f
->p
[n
][j
])) {
1011 Gcd(f
->p
[n
][j
], f
->p
[n
][nc
-1], &tmp
);
1012 value_division(tmp
, f
->p
[n
][nc
-1], tmp
);
1013 value_lcm(tmp
, lcm
->p
[j
], &lcm
->p
[j
]);
1018 mask_r(f
, nr
, lcm
, 0, val
, &EP
);
1023 free_evalue_refs(&EP
);
1034 static bool mod_needed(Polyhedron
*PD
, vec_ZZ
& num
, Value d
, evalue
*E
)
1036 Value
*q
= fixed_quotient(PD
, num
, d
, false);
1041 value_oppose(*q
, *q
);
1044 value_set_si(EV
.d
, 1);
1046 value_multiply(EV
.x
.n
, *q
, d
);
1048 free_evalue_refs(&EV
);
1054 static void ceil_mod(Value
*coef
, int len
, Value d
, ZZ
& f
, evalue
*EP
, Polyhedron
*PD
)
1058 value_set_si(m
, -1);
1060 Vector_Scale(coef
, coef
, m
, len
);
1063 int j
= normal_mod(coef
, len
, &m
);
1071 values2zz(coef
, num
, len
);
1078 evalue_set_si(&tmp
, 0, 1);
1082 while (j
< len
-1 && (num
[j
] == g
/2 || num
[j
] == 0))
1084 if ((j
< len
-1 && num
[j
] > g
/2) || (j
== len
-1 && num
[j
] >= (g
+1)/2)) {
1085 for (int k
= j
; k
< len
-1; ++k
)
1087 num
[k
] = g
- num
[k
];
1088 num
[len
-1] = g
- 1 - num
[len
-1];
1089 value_assign(tmp
.d
, m
);
1091 zz2value(t
, tmp
.x
.n
);
1097 ZZ t
= num
[len
-1] * f
;
1098 zz2value(t
, tmp
.x
.n
);
1099 value_assign(tmp
.d
, m
);
1102 evalue
*E
= multi_monom(num
);
1106 if (PD
&& !mod_needed(PD
, num
, m
, E
)) {
1108 zz2value(f
, EV
.x
.n
);
1109 value_assign(EV
.d
, m
);
1114 value_set_si(EV
.x
.n
, 1);
1115 value_assign(EV
.d
, m
);
1117 value_clear(EV
.x
.n
);
1118 value_set_si(EV
.d
, 0);
1119 EV
.x
.p
= new_enode(fractional
, 3, -1);
1120 evalue_copy(&EV
.x
.p
->arr
[0], E
);
1121 evalue_set_si(&EV
.x
.p
->arr
[1], 0, 1);
1122 value_init(EV
.x
.p
->arr
[2].x
.n
);
1123 zz2value(f
, EV
.x
.p
->arr
[2].x
.n
);
1124 value_set_si(EV
.x
.p
->arr
[2].d
, 1);
1129 free_evalue_refs(&EV
);
1130 free_evalue_refs(E
);
1134 free_evalue_refs(&tmp
);
1140 evalue
* bv_ceil3(Value
*coef
, int len
, Value d
, Polyhedron
*P
)
1142 Vector
*val
= Vector_Alloc(len
);
1146 value_set_si(t
, -1);
1147 Vector_Scale(coef
, val
->p
, t
, len
);
1148 value_absolute(t
, d
);
1151 values2zz(val
->p
, num
, len
);
1152 evalue
*EP
= multi_monom(num
);
1156 value_init(tmp
.x
.n
);
1157 value_set_si(tmp
.x
.n
, 1);
1158 value_assign(tmp
.d
, t
);
1164 ceil_mod(val
->p
, len
, t
, one
, EP
, P
);
1167 /* copy EP to malloc'ed evalue */
1173 free_evalue_refs(&tmp
);
1180 evalue
* lattice_point(
1181 Polyhedron
*i
, vec_ZZ
& lambda
, Matrix
*W
, Value lcm
, Polyhedron
*PD
)
1183 unsigned nparam
= W
->NbColumns
- 1;
1185 Matrix
* Rays
= rays2(i
);
1186 Matrix
*T
= Transpose(Rays
);
1187 Matrix
*T2
= Matrix_Copy(T
);
1188 Matrix
*inv
= Matrix_Alloc(T2
->NbRows
, T2
->NbColumns
);
1189 int ok
= Matrix_Inverse(T2
, inv
);
1194 matrix2zz(W
, vertex
, W
->NbRows
, W
->NbColumns
);
1197 num
= lambda
* vertex
;
1199 evalue
*EP
= multi_monom(num
);
1203 value_init(tmp
.x
.n
);
1204 value_set_si(tmp
.x
.n
, 1);
1205 value_assign(tmp
.d
, lcm
);
1209 Matrix
*L
= Matrix_Alloc(inv
->NbRows
, W
->NbColumns
);
1210 Matrix_Product(inv
, W
, L
);
1213 matrix2zz(T
, RT
, T
->NbRows
, T
->NbColumns
);
1216 vec_ZZ p
= lambda
* RT
;
1218 for (int i
= 0; i
< L
->NbRows
; ++i
) {
1219 ceil_mod(L
->p
[i
], nparam
+1, lcm
, p
[i
], EP
, PD
);
1225 free_evalue_refs(&tmp
);
1229 evalue
* lattice_point(
1230 Polyhedron
*i
, vec_ZZ
& lambda
, Matrix
*W
, Value lcm
, Polyhedron
*PD
)
1232 Matrix
*T
= Transpose(W
);
1233 unsigned nparam
= T
->NbRows
- 1;
1235 evalue
*EP
= new evalue();
1237 evalue_set_si(EP
, 0, 1);
1240 Vector
*val
= Vector_Alloc(nparam
+1);
1241 value_set_si(val
->p
[nparam
], 1);
1242 ZZ
offset(INIT_VAL
, 0);
1244 vertex_period(i
, lambda
, T
, lcm
, 0, val
, EP
, &ev
, offset
);
1247 free_evalue_refs(&ev
);
1258 Param_Vertices
* V
, Polyhedron
*i
, vec_ZZ
& lambda
, term_info
* term
,
1261 unsigned nparam
= V
->Vertex
->NbColumns
- 2;
1262 unsigned dim
= i
->Dimension
;
1264 vertex
.SetDims(V
->Vertex
->NbRows
, nparam
+1);
1268 value_set_si(lcm
, 1);
1269 for (int j
= 0; j
< V
->Vertex
->NbRows
; ++j
) {
1270 value_lcm(lcm
, V
->Vertex
->p
[j
][nparam
+1], &lcm
);
1272 if (value_notone_p(lcm
)) {
1273 Matrix
* mv
= Matrix_Alloc(dim
, nparam
+1);
1274 for (int j
= 0 ; j
< dim
; ++j
) {
1275 value_division(tmp
, lcm
, V
->Vertex
->p
[j
][nparam
+1]);
1276 Vector_Scale(V
->Vertex
->p
[j
], mv
->p
[j
], tmp
, nparam
+1);
1279 term
->E
= lattice_point(i
, lambda
, mv
, lcm
, PD
);
1287 for (int i
= 0; i
< V
->Vertex
->NbRows
; ++i
) {
1288 assert(value_one_p(V
->Vertex
->p
[i
][nparam
+1])); // for now
1289 values2zz(V
->Vertex
->p
[i
], vertex
[i
], nparam
+1);
1293 num
= lambda
* vertex
;
1297 for (int j
= 0; j
< nparam
; ++j
)
1303 term
->E
= multi_monom(num
);
1307 term
->constant
= num
[nparam
];
1310 term
->coeff
= num
[p
];
1317 void normalize(Polyhedron
*i
, vec_ZZ
& lambda
, ZZ
& sign
, ZZ
& num
, vec_ZZ
& den
)
1319 unsigned dim
= i
->Dimension
;
1323 rays
.SetDims(dim
, dim
);
1324 add_rays(rays
, i
, &r
);
1325 den
= rays
* lambda
;
1328 for (int j
= 0; j
< den
.length(); ++j
) {
1332 den
[j
] = abs(den
[j
]);
1340 typedef Polyhedron
* Polyhedron_p
;
1342 void barvinok_count(Polyhedron
*P
, Value
* result
, unsigned NbMaxCons
)
1344 Polyhedron
** vcone
;
1347 sign
.SetLength(ncone
);
1355 value_set_si(*result
, 0);
1359 for (; r
< P
->NbRays
; ++r
)
1360 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1]))
1362 if (P
->NbBid
!=0 || r
< P
->NbRays
) {
1363 value_set_si(*result
, -1);
1367 P
= remove_equalities(P
);
1370 value_set_si(*result
, 0);
1376 value_set_si(factor
, 1);
1377 Q
= Polyhedron_Reduce(P
, &factor
);
1384 if (P
->Dimension
== 0) {
1385 value_assign(*result
, factor
);
1388 value_clear(factor
);
1393 vcone
= new Polyhedron_p
[P
->NbRays
];
1396 //nonorthog(rays, lambda);
1397 randomvector(P
, lambda
, dim
);
1398 //cout << "lambda: " << lambda << endl;
1401 rays
.SetDims(dim
, dim
);
1412 for (int j
= 0; j
< P
->NbRays
; ++j
) {
1414 Polyhedron
*C
= supporting_cone(P
, j
);
1415 decompose(C
, &vcone
[j
], &npos
, &nneg
, NbMaxCons
);
1416 ncone
+= npos
+ nneg
;
1417 sign
.SetLength(ncone
);
1418 for (int k
= 0; k
< npos
; ++k
)
1419 sign
[ncone
-nneg
-k
-1] = 1;
1420 for (int k
= 0; k
< nneg
; ++k
)
1421 sign
[ncone
-k
-1] = -1;
1422 for (Polyhedron
*i
= vcone
[j
]; i
; i
= i
->next
) {
1424 assert(i
->NbRays
-1 == dim
);
1425 add_rays(rays
, i
, &r
);
1426 for (int k
= 0; k
< dim
; ++k
) {
1427 assert(lambda
* rays
[k
] != 0);
1430 lattice_point(P
->Ray
[j
]+1, i
, vertex
);
1431 num
= vertex
* lambda
;
1432 normalize(i
, lambda
, sign
[f
], num
, den
);
1435 dpoly
n(dim
, den
[0], 1);
1436 for (int k
= 1; k
< dim
; ++k
) {
1437 dpoly
fact(dim
, den
[k
], 1);
1440 d
.div(n
, count
, sign
[f
]);
1444 Domain_Free(vcone
[j
]);
1447 assert(value_one_p(&count
[0]._mp_den
));
1448 value_multiply(*result
, &count
[0]._mp_num
, factor
);
1455 value_clear(factor
);
1458 static void uni_polynom(int param
, Vector
*c
, evalue
*EP
)
1460 unsigned dim
= c
->Size
-2;
1462 value_set_si(EP
->d
,0);
1463 EP
->x
.p
= new_enode(polynomial
, dim
+1, param
+1);
1464 for (int j
= 0; j
<= dim
; ++j
)
1465 evalue_set(&EP
->x
.p
->arr
[j
], c
->p
[j
], c
->p
[dim
+1]);
1468 static void multi_polynom(Vector
*c
, evalue
* X
, evalue
*EP
)
1470 unsigned dim
= c
->Size
-2;
1474 evalue_set(&EC
, c
->p
[dim
], c
->p
[dim
+1]);
1477 evalue_set(EP
, c
->p
[dim
], c
->p
[dim
+1]);
1479 for (int i
= dim
-1; i
>= 0; --i
) {
1481 value_assign(EC
.x
.n
, c
->p
[i
]);
1484 free_evalue_refs(&EC
);
1487 Polyhedron
*unfringe (Polyhedron
*P
, unsigned MaxRays
)
1489 int len
= P
->Dimension
+2;
1490 Polyhedron
*T
, *R
= P
;
1493 Vector
*row
= Vector_Alloc(len
);
1494 value_set_si(row
->p
[0], 1);
1496 R
= DomainConstraintSimplify(Polyhedron_Copy(P
), MaxRays
);
1498 Matrix
*M
= Matrix_Alloc(2, len
-1);
1499 value_set_si(M
->p
[1][len
-2], 1);
1500 for (int v
= 0; v
< P
->Dimension
; ++v
) {
1501 value_set_si(M
->p
[0][v
], 1);
1502 Polyhedron
*I
= Polyhedron_Image(P
, M
, 2+1);
1503 value_set_si(M
->p
[0][v
], 0);
1504 for (int r
= 0; r
< I
->NbConstraints
; ++r
) {
1505 if (value_zero_p(I
->Constraint
[r
][0]))
1507 if (value_zero_p(I
->Constraint
[r
][1]))
1509 if (value_one_p(I
->Constraint
[r
][1]))
1511 if (value_mone_p(I
->Constraint
[r
][1]))
1513 value_absolute(g
, I
->Constraint
[r
][1]);
1514 Vector_Set(row
->p
+1, 0, len
-2);
1515 value_division(row
->p
[1+v
], I
->Constraint
[r
][1], g
);
1516 mpz_fdiv_q(row
->p
[len
-1], I
->Constraint
[r
][2], g
);
1518 R
= AddConstraints(row
->p
, 1, R
, MaxRays
);
1530 static Polyhedron
*reduce_domain(Polyhedron
*D
, Matrix
*CT
, Polyhedron
*CEq
,
1531 Polyhedron
**fVD
, int nd
, unsigned MaxRays
)
1536 Dt
= CT
? DomainPreimage(D
, CT
, MaxRays
) : D
;
1537 Polyhedron
*rVD
= DomainIntersection(Dt
, CEq
, MaxRays
);
1539 /* if rVD is empty or too small in geometric dimension */
1540 if(!rVD
|| emptyQ(rVD
) ||
1541 (rVD
->Dimension
-rVD
->NbEq
< Dt
->Dimension
-Dt
->NbEq
-CEq
->NbEq
)) {
1546 return 0; /* empty validity domain */
1552 fVD
[nd
] = Domain_Copy(rVD
);
1553 for (int i
= 0 ; i
< nd
; ++i
) {
1554 Polyhedron
*I
= DomainIntersection(fVD
[nd
], fVD
[i
], MaxRays
);
1559 Polyhedron
*F
= DomainSimplify(I
, fVD
[nd
], MaxRays
);
1561 Polyhedron
*T
= rVD
;
1562 rVD
= DomainDifference(rVD
, F
, MaxRays
);
1569 rVD
= DomainConstraintSimplify(rVD
, MaxRays
);
1571 Domain_Free(fVD
[nd
]);
1578 barvinok_count(rVD
, &c
, MaxRays
);
1579 if (value_zero_p(c
)) {
1588 static bool Polyhedron_is_infinite(Polyhedron
*P
, unsigned nparam
)
1591 for (r
= 0; r
< P
->NbRays
; ++r
)
1592 if (value_zero_p(P
->Ray
[r
][0]) ||
1593 value_zero_p(P
->Ray
[r
][P
->Dimension
+1])) {
1595 for (i
= P
->Dimension
- nparam
; i
< P
->Dimension
; ++i
)
1596 if (value_notzero_p(P
->Ray
[r
][i
+1]))
1598 if (i
>= P
->Dimension
)
1601 return r
< P
->NbRays
;
1604 /* Check whether all rays point in the positive directions
1605 * for the parameters
1607 static bool Polyhedron_has_positive_rays(Polyhedron
*P
, unsigned nparam
)
1610 for (r
= 0; r
< P
->NbRays
; ++r
)
1611 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1])) {
1613 for (i
= P
->Dimension
- nparam
; i
< P
->Dimension
; ++i
)
1614 if (value_neg_p(P
->Ray
[r
][i
+1]))
1620 evalue
* barvinok_enumerate_ev(Polyhedron
*P
, Polyhedron
* C
, unsigned MaxRays
)
1622 //P = unfringe(P, MaxRays);
1623 Polyhedron
*CEq
= NULL
, *rVD
, *pVD
, *CA
;
1625 Param_Polyhedron
*PP
= NULL
;
1626 Param_Domain
*D
, *next
;
1629 unsigned nparam
= C
->Dimension
;
1631 ALLOC(evalue
, eres
);
1632 value_init(eres
->d
);
1633 value_set_si(eres
->d
, 0);
1636 value_init(factor
.d
);
1637 evalue_set_si(&factor
, 1, 1);
1639 CA
= align_context(C
, P
->Dimension
, MaxRays
);
1640 P
= DomainIntersection(P
, CA
, MaxRays
);
1641 Polyhedron_Free(CA
);
1643 if (C
->Dimension
== 0 || emptyQ(P
)) {
1645 eres
->x
.p
= new_enode(partition
, 2, C
->Dimension
);
1646 EVALUE_SET_DOMAIN(eres
->x
.p
->arr
[0],
1647 DomainConstraintSimplify(CEq
? CEq
: Polyhedron_Copy(C
), MaxRays
));
1648 value_set_si(eres
->x
.p
->arr
[1].d
, 1);
1649 value_init(eres
->x
.p
->arr
[1].x
.n
);
1651 value_set_si(eres
->x
.p
->arr
[1].x
.n
, 0);
1653 barvinok_count(P
, &eres
->x
.p
->arr
[1].x
.n
, MaxRays
);
1655 emul(&factor
, eres
);
1656 reduce_evalue(eres
);
1657 free_evalue_refs(&factor
);
1662 Param_Polyhedron_Free(PP
);
1666 if (Polyhedron_is_infinite(P
, nparam
))
1671 P
= remove_equalities_p(P
, P
->Dimension
-nparam
, &f
);
1675 if (P
->Dimension
== nparam
) {
1677 P
= Universe_Polyhedron(0);
1681 Polyhedron
*Q
= ParamPolyhedron_Reduce(P
, P
->Dimension
-nparam
, &factor
);
1684 if (Q
->Dimension
== nparam
) {
1686 P
= Universe_Polyhedron(0);
1691 Polyhedron
*oldP
= P
;
1692 PP
= Polyhedron2Param_SimplifiedDomain(&P
,C
,MaxRays
,&CEq
,&CT
);
1694 Polyhedron_Free(oldP
);
1696 if (isIdentity(CT
)) {
1700 assert(CT
->NbRows
!= CT
->NbColumns
);
1701 if (CT
->NbRows
== 1) // no more parameters
1703 nparam
= CT
->NbRows
- 1;
1706 unsigned dim
= P
->Dimension
- nparam
;
1707 Polyhedron
** vcone
= new Polyhedron_p
[PP
->nbV
];
1708 int * npos
= new int[PP
->nbV
];
1709 int * nneg
= new int[PP
->nbV
];
1713 for (i
= 0, V
= PP
->V
; V
; ++i
, V
= V
->next
) {
1714 Polyhedron
*C
= supporting_cone_p(P
, V
);
1715 decompose(C
, &vcone
[i
], &npos
[i
], &nneg
[i
], MaxRays
);
1718 Vector
*c
= Vector_Alloc(dim
+2);
1721 for (nd
= 0, D
=PP
->D
; D
; ++nd
, D
=D
->next
);
1722 struct section
{ Polyhedron
*D
; evalue E
; };
1723 section
*s
= new section
[nd
];
1724 Polyhedron
**fVD
= new Polyhedron_p
[nd
];
1726 for(nd
= 0, D
=PP
->D
; D
; D
=next
) {
1729 Polyhedron
*rVD
= reduce_domain(D
->Domain
, CT
, CEq
,
1734 pVD
= CT
? DomainImage(rVD
,CT
,MaxRays
) : rVD
;
1737 FORALL_PVertex_in_ParamPolyhedron(V
,D
,PP
) // _i is internal counter
1738 ncone
+= npos
[_i
] + nneg
[_i
];
1739 END_FORALL_PVertex_in_ParamPolyhedron
;
1742 rays
.SetDims(ncone
* dim
, dim
);
1744 FORALL_PVertex_in_ParamPolyhedron(V
,D
,PP
) // _i is internal counter
1745 for (Polyhedron
*i
= vcone
[_i
]; i
; i
= i
->next
) {
1746 assert(i
->NbRays
-1 == dim
);
1747 add_rays(rays
, i
, &r
);
1749 END_FORALL_PVertex_in_ParamPolyhedron
;
1751 nonorthog(rays
, lambda
);
1757 value_init(s
[nd
].E
.d
);
1758 evalue_set_si(&s
[nd
].E
, 0, 1);
1761 FORALL_PVertex_in_ParamPolyhedron(V
,D
,PP
)
1763 for (Polyhedron
*i
= vcone
[_i
]; i
; i
= i
->next
) {
1764 sign
= f
< npos
[_i
] ? 1 : -1;
1765 lattice_point(V
, i
, lambda
, &num
, pVD
);
1766 normalize(i
, lambda
, sign
, num
.constant
, den
);
1768 dpoly
n(dim
, den
[0], 1);
1769 for (int k
= 1; k
< dim
; ++k
) {
1770 dpoly
fact(dim
, den
[k
], 1);
1773 if (num
.E
!= NULL
) {
1774 ZZ
one(INIT_VAL
, 1);
1775 dpoly_n
d(dim
, num
.constant
, one
);
1778 multi_polynom(c
, num
.E
, &EV
);
1779 eadd(&EV
, &s
[nd
].E
);
1780 free_evalue_refs(&EV
);
1781 free_evalue_refs(num
.E
);
1783 } else if (num
.pos
!= -1) {
1784 dpoly_n
d(dim
, num
.constant
, num
.coeff
);
1787 uni_polynom(num
.pos
, c
, &EV
);
1788 eadd(&EV
, &s
[nd
].E
);
1789 free_evalue_refs(&EV
);
1791 mpq_set_si(count
, 0, 1);
1792 dpoly
d(dim
, num
.constant
);
1793 d
.div(n
, count
, sign
);
1796 evalue_set(&EV
, &count
[0]._mp_num
, &count
[0]._mp_den
);
1797 eadd(&EV
, &s
[nd
].E
);
1798 free_evalue_refs(&EV
);
1802 END_FORALL_PVertex_in_ParamPolyhedron
;
1807 addeliminatedparams_evalue(&s
[nd
].E
, CT
);
1815 evalue_set_si(eres
, 0, 1);
1817 eres
->x
.p
= new_enode(partition
, 2*nd
, C
->Dimension
);
1818 for (int j
= 0; j
< nd
; ++j
) {
1819 EVALUE_SET_DOMAIN(eres
->x
.p
->arr
[2*j
], s
[j
].D
);
1820 value_clear(eres
->x
.p
->arr
[2*j
+1].d
);
1821 eres
->x
.p
->arr
[2*j
+1] = s
[j
].E
;
1822 Domain_Free(fVD
[j
]);
1830 for (int j
= 0; j
< PP
->nbV
; ++j
)
1831 Domain_Free(vcone
[j
]);
1837 Polyhedron_Free(CEq
);
1842 Enumeration
* barvinok_enumerate(Polyhedron
*P
, Polyhedron
* C
, unsigned MaxRays
)
1844 evalue
*EP
= barvinok_enumerate_ev(P
, C
, MaxRays
);
1846 return partition2enumeration(EP
);
1849 static void SwapColumns(Value
**V
, int n
, int i
, int j
)
1851 for (int r
= 0; r
< n
; ++r
)
1852 value_swap(V
[r
][i
], V
[r
][j
]);
1855 static void SwapColumns(Polyhedron
*P
, int i
, int j
)
1857 SwapColumns(P
->Constraint
, P
->NbConstraints
, i
, j
);
1858 SwapColumns(P
->Ray
, P
->NbRays
, i
, j
);
1861 static void negative_test_constraint(Value
*l
, Value
*u
, Value
*c
, int pos
,
1864 value_oppose(*v
, u
[pos
+1]);
1865 Vector_Combine(l
+1, u
+1, c
+1, *v
, l
[pos
+1], len
-1);
1866 value_multiply(*v
, *v
, l
[pos
+1]);
1867 value_substract(c
[len
-1], c
[len
-1], *v
);
1868 value_set_si(*v
, -1);
1869 Vector_Scale(c
+1, c
+1, *v
, len
-1);
1870 value_decrement(c
[len
-1], c
[len
-1]);
1871 ConstraintSimplify(c
, c
, len
, v
);
1874 static void oppose_constraint(Value
*c
, int len
, Value
*v
)
1876 value_set_si(*v
, -1);
1877 Vector_Scale(c
+1, c
+1, *v
, len
-1);
1878 value_decrement(c
[len
-1], c
[len
-1]);
1881 static bool SplitOnConstraint(Polyhedron
*P
, int i
, int l
, int u
,
1882 int nvar
, int len
, int exist
, int MaxRays
,
1883 Vector
*row
, Value
& f
, bool independent
,
1884 Polyhedron
**pos
, Polyhedron
**neg
)
1886 negative_test_constraint(P
->Constraint
[l
], P
->Constraint
[u
],
1887 row
->p
, nvar
+i
, len
, &f
);
1888 *neg
= AddConstraints(row
->p
, 1, P
, MaxRays
);
1890 /* We found an independent, but useless constraint
1891 * Maybe we should detect this earlier and not
1892 * mark the variable as INDEPENDENT
1894 if (emptyQ((*neg
))) {
1895 Polyhedron_Free(*neg
);
1899 oppose_constraint(row
->p
, len
, &f
);
1900 *pos
= AddConstraints(row
->p
, 1, P
, MaxRays
);
1902 if (emptyQ((*pos
))) {
1903 Polyhedron_Free(*neg
);
1904 Polyhedron_Free(*pos
);
1912 * unimodularly transform P such that constraint r is transformed
1913 * into a constraint that involves only a single (the first)
1914 * existential variable
1917 static Polyhedron
*rotate_along(Polyhedron
*P
, int r
, int nvar
, int exist
,
1923 Vector
*row
= Vector_Alloc(exist
);
1924 Vector_Copy(P
->Constraint
[r
]+1+nvar
, row
->p
, exist
);
1925 Vector_Gcd(row
->p
, exist
, &g
);
1926 if (value_notone_p(g
))
1927 Vector_AntiScale(row
->p
, row
->p
, g
, exist
);
1930 Matrix
*M
= unimodular_complete(row
);
1931 Matrix
*M2
= Matrix_Alloc(P
->Dimension
+1, P
->Dimension
+1);
1932 for (r
= 0; r
< nvar
; ++r
)
1933 value_set_si(M2
->p
[r
][r
], 1);
1934 for ( ; r
< nvar
+exist
; ++r
)
1935 Vector_Copy(M
->p
[r
-nvar
], M2
->p
[r
]+nvar
, exist
);
1936 for ( ; r
< P
->Dimension
+1; ++r
)
1937 value_set_si(M2
->p
[r
][r
], 1);
1938 Polyhedron
*T
= Polyhedron_Image(P
, M2
, MaxRays
);
1947 static bool SplitOnVar(Polyhedron
*P
, int i
,
1948 int nvar
, int len
, int exist
, int MaxRays
,
1949 Vector
*row
, Value
& f
, bool independent
,
1950 Polyhedron
**pos
, Polyhedron
**neg
)
1954 for (int l
= P
->NbEq
; l
< P
->NbConstraints
; ++l
) {
1955 if (value_negz_p(P
->Constraint
[l
][nvar
+i
+1]))
1959 for (j
= 0; j
< exist
; ++j
)
1960 if (j
!= i
&& value_notzero_p(P
->Constraint
[l
][nvar
+j
+1]))
1966 for (int u
= P
->NbEq
; u
< P
->NbConstraints
; ++u
) {
1967 if (value_posz_p(P
->Constraint
[u
][nvar
+i
+1]))
1971 for (j
= 0; j
< exist
; ++j
)
1972 if (j
!= i
&& value_notzero_p(P
->Constraint
[u
][nvar
+j
+1]))
1978 if (SplitOnConstraint(P
, i
, l
, u
,
1979 nvar
, len
, exist
, MaxRays
,
1980 row
, f
, independent
,
1984 SwapColumns(*neg
, nvar
+1, nvar
+1+i
);
1994 static bool double_bound_pair(Polyhedron
*P
, int nvar
, int exist
,
1995 int i
, int l1
, int l2
,
1996 Polyhedron
**pos
, Polyhedron
**neg
)
2000 Vector
*row
= Vector_Alloc(P
->Dimension
+2);
2001 value_set_si(row
->p
[0], 1);
2002 value_oppose(f
, P
->Constraint
[l1
][nvar
+i
+1]);
2003 Vector_Combine(P
->Constraint
[l1
]+1, P
->Constraint
[l2
]+1,
2005 P
->Constraint
[l2
][nvar
+i
+1], f
,
2007 ConstraintSimplify(row
->p
, row
->p
, P
->Dimension
+2, &f
);
2008 *pos
= AddConstraints(row
->p
, 1, P
, 0);
2009 value_set_si(f
, -1);
2010 Vector_Scale(row
->p
+1, row
->p
+1, f
, P
->Dimension
+1);
2011 value_decrement(row
->p
[P
->Dimension
+1], row
->p
[P
->Dimension
+1]);
2012 *neg
= AddConstraints(row
->p
, 1, P
, 0);
2016 return !emptyQ((*pos
)) && !emptyQ((*neg
));
2019 static bool double_bound(Polyhedron
*P
, int nvar
, int exist
,
2020 Polyhedron
**pos
, Polyhedron
**neg
)
2022 for (int i
= 0; i
< exist
; ++i
) {
2024 for (l1
= P
->NbEq
; l1
< P
->NbConstraints
; ++l1
) {
2025 if (value_negz_p(P
->Constraint
[l1
][nvar
+i
+1]))
2027 for (l2
= l1
+ 1; l2
< P
->NbConstraints
; ++l2
) {
2028 if (value_negz_p(P
->Constraint
[l2
][nvar
+i
+1]))
2030 if (double_bound_pair(P
, nvar
, exist
, i
, l1
, l2
, pos
, neg
))
2034 for (l1
= P
->NbEq
; l1
< P
->NbConstraints
; ++l1
) {
2035 if (value_posz_p(P
->Constraint
[l1
][nvar
+i
+1]))
2037 if (l1
< P
->NbConstraints
)
2038 for (l2
= l1
+ 1; l2
< P
->NbConstraints
; ++l2
) {
2039 if (value_posz_p(P
->Constraint
[l2
][nvar
+i
+1]))
2041 if (double_bound_pair(P
, nvar
, exist
, i
, l1
, l2
, pos
, neg
))
2053 INDEPENDENT
= 1 << 2
2056 static evalue
* enumerate_or(Polyhedron
*D
,
2057 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
2060 fprintf(stderr
, "\nER: Or\n");
2061 #endif /* DEBUG_ER */
2063 Polyhedron
*N
= D
->next
;
2066 barvinok_enumerate_e(D
, exist
, nparam
, MaxRays
);
2069 for (D
= N
; D
; D
= N
) {
2074 barvinok_enumerate_e(D
, exist
, nparam
, MaxRays
);
2077 free_evalue_refs(EN
);
2087 static evalue
* enumerate_sum(Polyhedron
*P
,
2088 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
2090 int nvar
= P
->Dimension
- exist
- nparam
;
2091 int toswap
= nvar
< exist
? nvar
: exist
;
2092 for (int i
= 0; i
< toswap
; ++i
)
2093 SwapColumns(P
, 1 + i
, nvar
+exist
- i
);
2097 fprintf(stderr
, "\nER: Sum\n");
2098 #endif /* DEBUG_ER */
2100 evalue
*EP
= barvinok_enumerate_e(P
, exist
, nparam
, MaxRays
);
2102 for (int i
= 0; i
< /* nvar */ nparam
; ++i
) {
2103 Matrix
*C
= Matrix_Alloc(1, 1 + nparam
+ 1);
2104 value_set_si(C
->p
[0][0], 1);
2106 value_init(split
.d
);
2107 value_set_si(split
.d
, 0);
2108 split
.x
.p
= new_enode(partition
, 4, nparam
);
2109 value_set_si(C
->p
[0][1+i
], 1);
2110 Matrix
*C2
= Matrix_Copy(C
);
2111 EVALUE_SET_DOMAIN(split
.x
.p
->arr
[0],
2112 Constraints2Polyhedron(C2
, MaxRays
));
2114 evalue_set_si(&split
.x
.p
->arr
[1], 1, 1);
2115 value_set_si(C
->p
[0][1+i
], -1);
2116 value_set_si(C
->p
[0][1+nparam
], -1);
2117 EVALUE_SET_DOMAIN(split
.x
.p
->arr
[2],
2118 Constraints2Polyhedron(C
, MaxRays
));
2119 evalue_set_si(&split
.x
.p
->arr
[3], 1, 1);
2121 free_evalue_refs(&split
);
2125 evalue_range_reduction(EP
);
2127 evalue_frac2floor(EP
);
2129 evalue
*sum
= esum(EP
, nvar
);
2131 free_evalue_refs(EP
);
2135 evalue_range_reduction(EP
);
2140 static evalue
* split_sure(Polyhedron
*P
, Polyhedron
*S
,
2141 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
2143 int nvar
= P
->Dimension
- exist
- nparam
;
2145 Matrix
*M
= Matrix_Alloc(exist
, S
->Dimension
+2);
2146 for (int i
= 0; i
< exist
; ++i
)
2147 value_set_si(M
->p
[i
][nvar
+i
+1], 1);
2149 S
= DomainAddRays(S
, M
, MaxRays
);
2151 Polyhedron
*F
= DomainAddRays(P
, M
, MaxRays
);
2152 Polyhedron
*D
= DomainDifference(F
, S
, MaxRays
);
2154 D
= Disjoint_Domain(D
, 0, MaxRays
);
2159 M
= Matrix_Alloc(P
->Dimension
+1-exist
, P
->Dimension
+1);
2160 for (int j
= 0; j
< nvar
; ++j
)
2161 value_set_si(M
->p
[j
][j
], 1);
2162 for (int j
= 0; j
< nparam
+1; ++j
)
2163 value_set_si(M
->p
[nvar
+j
][nvar
+exist
+j
], 1);
2164 Polyhedron
*T
= Polyhedron_Image(S
, M
, MaxRays
);
2165 evalue
*EP
= barvinok_enumerate_e(T
, 0, nparam
, MaxRays
);
2170 for (Polyhedron
*Q
= D
; Q
; Q
= Q
->next
) {
2171 Polyhedron
*N
= Q
->next
;
2173 T
= DomainIntersection(P
, Q
, MaxRays
);
2174 evalue
*E
= barvinok_enumerate_e(T
, exist
, nparam
, MaxRays
);
2176 free_evalue_refs(E
);
2185 static evalue
* enumerate_sure(Polyhedron
*P
,
2186 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
2190 int nvar
= P
->Dimension
- exist
- nparam
;
2196 for (i
= 0; i
< exist
; ++i
) {
2197 Matrix
*M
= Matrix_Alloc(S
->NbConstraints
, S
->Dimension
+2);
2199 value_set_si(lcm
, 1);
2200 for (int j
= 0; j
< S
->NbConstraints
; ++j
) {
2201 if (value_negz_p(S
->Constraint
[j
][1+nvar
+i
]))
2203 if (value_one_p(S
->Constraint
[j
][1+nvar
+i
]))
2205 value_lcm(lcm
, S
->Constraint
[j
][1+nvar
+i
], &lcm
);
2208 for (int j
= 0; j
< S
->NbConstraints
; ++j
) {
2209 if (value_negz_p(S
->Constraint
[j
][1+nvar
+i
]))
2211 if (value_one_p(S
->Constraint
[j
][1+nvar
+i
]))
2213 value_division(f
, lcm
, S
->Constraint
[j
][1+nvar
+i
]);
2214 Vector_Scale(S
->Constraint
[j
], M
->p
[c
], f
, S
->Dimension
+2);
2215 value_substract(M
->p
[c
][S
->Dimension
+1],
2216 M
->p
[c
][S
->Dimension
+1],
2218 value_increment(M
->p
[c
][S
->Dimension
+1],
2219 M
->p
[c
][S
->Dimension
+1]);
2223 S
= AddConstraints(M
->p
[0], c
, S
, MaxRays
);
2238 fprintf(stderr
, "\nER: Sure\n");
2239 #endif /* DEBUG_ER */
2241 return split_sure(P
, S
, exist
, nparam
, MaxRays
);
2244 static evalue
* enumerate_sure2(Polyhedron
*P
,
2245 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
2247 int nvar
= P
->Dimension
- exist
- nparam
;
2249 for (r
= 0; r
< P
->NbRays
; ++r
)
2250 if (value_one_p(P
->Ray
[r
][0]) &&
2251 value_one_p(P
->Ray
[r
][P
->Dimension
+1]))
2257 Matrix
*M
= Matrix_Alloc(nvar
+ 1 + nparam
, P
->Dimension
+2);
2258 for (int i
= 0; i
< nvar
; ++i
)
2259 value_set_si(M
->p
[i
][1+i
], 1);
2260 for (int i
= 0; i
< nparam
; ++i
)
2261 value_set_si(M
->p
[i
+nvar
][1+nvar
+exist
+i
], 1);
2262 Vector_Copy(P
->Ray
[r
]+1+nvar
, M
->p
[nvar
+nparam
]+1+nvar
, exist
);
2263 value_set_si(M
->p
[nvar
+nparam
][0], 1);
2264 value_set_si(M
->p
[nvar
+nparam
][P
->Dimension
+1], 1);
2265 Polyhedron
* F
= Rays2Polyhedron(M
, MaxRays
);
2268 Polyhedron
*I
= DomainIntersection(F
, P
, MaxRays
);
2272 fprintf(stderr
, "\nER: Sure2\n");
2273 #endif /* DEBUG_ER */
2275 return split_sure(P
, I
, exist
, nparam
, MaxRays
);
2278 static evalue
* enumerate_cyclic(Polyhedron
*P
,
2279 unsigned exist
, unsigned nparam
,
2280 evalue
* EP
, int r
, int p
, unsigned MaxRays
)
2282 int nvar
= P
->Dimension
- exist
- nparam
;
2284 /* If EP in its fractional maps only contains references
2285 * to the remainder parameter with appropriate coefficients
2286 * then we could in principle avoid adding existentially
2287 * quantified variables to the validity domains.
2288 * We'd have to replace the remainder by m { p/m }
2289 * and multiply with an appropriate factor that is one
2290 * only in the appropriate range.
2291 * This last multiplication can be avoided if EP
2292 * has a single validity domain with no (further)
2293 * constraints on the remainder parameter
2296 Matrix
*CT
= Matrix_Alloc(nparam
+1, nparam
+3);
2297 Matrix
*M
= Matrix_Alloc(1, 1+nparam
+3);
2298 for (int j
= 0; j
< nparam
; ++j
)
2300 value_set_si(CT
->p
[j
][j
], 1);
2301 value_set_si(CT
->p
[p
][nparam
+1], 1);
2302 value_set_si(CT
->p
[nparam
][nparam
+2], 1);
2303 value_set_si(M
->p
[0][1+p
], -1);
2304 value_absolute(M
->p
[0][1+nparam
], P
->Ray
[0][1+nvar
+exist
+p
]);
2305 value_set_si(M
->p
[0][1+nparam
+1], 1);
2306 Polyhedron
*CEq
= Constraints2Polyhedron(M
, 1);
2308 addeliminatedparams_enum(EP
, CT
, CEq
, MaxRays
, nparam
);
2309 Polyhedron_Free(CEq
);
2315 static void enumerate_vd_add_ray(evalue
*EP
, Matrix
*Rays
, unsigned MaxRays
)
2317 if (value_notzero_p(EP
->d
))
2320 assert(EP
->x
.p
->type
== partition
);
2321 assert(EP
->x
.p
->pos
== EVALUE_DOMAIN(EP
->x
.p
->arr
[0])->Dimension
);
2322 for (int i
= 0; i
< EP
->x
.p
->size
/2; ++i
) {
2323 Polyhedron
*D
= EVALUE_DOMAIN(EP
->x
.p
->arr
[2*i
]);
2324 Polyhedron
*N
= DomainAddRays(D
, Rays
, MaxRays
);
2325 EVALUE_SET_DOMAIN(EP
->x
.p
->arr
[2*i
], N
);
2330 static evalue
* enumerate_line(Polyhedron
*P
,
2331 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
2337 fprintf(stderr
, "\nER: Line\n");
2338 #endif /* DEBUG_ER */
2340 int nvar
= P
->Dimension
- exist
- nparam
;
2342 for (i
= 0; i
< nparam
; ++i
)
2343 if (value_notzero_p(P
->Ray
[0][1+nvar
+exist
+i
]))
2346 for (j
= i
+1; j
< nparam
; ++j
)
2347 if (value_notzero_p(P
->Ray
[0][1+nvar
+exist
+i
]))
2349 assert(j
>= nparam
); // for now
2351 Matrix
*M
= Matrix_Alloc(2, P
->Dimension
+2);
2352 value_set_si(M
->p
[0][0], 1);
2353 value_set_si(M
->p
[0][1+nvar
+exist
+i
], 1);
2354 value_set_si(M
->p
[1][0], 1);
2355 value_set_si(M
->p
[1][1+nvar
+exist
+i
], -1);
2356 value_absolute(M
->p
[1][1+P
->Dimension
], P
->Ray
[0][1+nvar
+exist
+i
]);
2357 value_decrement(M
->p
[1][1+P
->Dimension
], M
->p
[1][1+P
->Dimension
]);
2358 Polyhedron
*S
= AddConstraints(M
->p
[0], 2, P
, MaxRays
);
2359 evalue
*EP
= barvinok_enumerate_e(S
, exist
, nparam
, MaxRays
);
2363 return enumerate_cyclic(P
, exist
, nparam
, EP
, 0, i
, MaxRays
);
2366 static int single_param_pos(Polyhedron
*P
, unsigned exist
, unsigned nparam
,
2369 int nvar
= P
->Dimension
- exist
- nparam
;
2370 if (First_Non_Zero(P
->Ray
[r
]+1, nvar
) != -1)
2372 int i
= First_Non_Zero(P
->Ray
[r
]+1+nvar
+exist
, nparam
);
2375 if (First_Non_Zero(P
->Ray
[r
]+1+nvar
+exist
+1, nparam
-i
-1) != -1)
2380 static evalue
* enumerate_remove_ray(Polyhedron
*P
, int r
,
2381 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
2384 fprintf(stderr
, "\nER: RedundantRay\n");
2385 #endif /* DEBUG_ER */
2389 value_set_si(one
, 1);
2390 int len
= P
->NbRays
-1;
2391 Matrix
*M
= Matrix_Alloc(2 * len
, P
->Dimension
+2);
2392 Vector_Copy(P
->Ray
[0], M
->p
[0], r
* (P
->Dimension
+2));
2393 Vector_Copy(P
->Ray
[r
+1], M
->p
[r
], (len
-r
) * (P
->Dimension
+2));
2394 for (int j
= 0; j
< P
->NbRays
; ++j
) {
2397 Vector_Combine(P
->Ray
[j
], P
->Ray
[r
], M
->p
[len
+j
-(j
>r
)],
2398 one
, P
->Ray
[j
][P
->Dimension
+1], P
->Dimension
+2);
2401 P
= Rays2Polyhedron(M
, MaxRays
);
2403 evalue
*EP
= barvinok_enumerate_e(P
, exist
, nparam
, MaxRays
);
2410 static evalue
* enumerate_redundant_ray(Polyhedron
*P
,
2411 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
2413 assert(P
->NbBid
== 0);
2414 int nvar
= P
->Dimension
- exist
- nparam
;
2418 for (int r
= 0; r
< P
->NbRays
; ++r
) {
2419 if (value_notzero_p(P
->Ray
[r
][P
->Dimension
+1]))
2421 int i1
= single_param_pos(P
, exist
, nparam
, r
);
2424 for (int r2
= r
+1; r2
< P
->NbRays
; ++r2
) {
2425 if (value_notzero_p(P
->Ray
[r2
][P
->Dimension
+1]))
2427 int i2
= single_param_pos(P
, exist
, nparam
, r2
);
2433 value_division(m
, P
->Ray
[r
][1+nvar
+exist
+i1
],
2434 P
->Ray
[r2
][1+nvar
+exist
+i1
]);
2435 value_multiply(m
, m
, P
->Ray
[r2
][1+nvar
+exist
+i1
]);
2436 /* r2 divides r => r redundant */
2437 if (value_eq(m
, P
->Ray
[r
][1+nvar
+exist
+i1
])) {
2439 return enumerate_remove_ray(P
, r
, exist
, nparam
, MaxRays
);
2442 value_division(m
, P
->Ray
[r2
][1+nvar
+exist
+i1
],
2443 P
->Ray
[r
][1+nvar
+exist
+i1
]);
2444 value_multiply(m
, m
, P
->Ray
[r
][1+nvar
+exist
+i1
]);
2445 /* r divides r2 => r2 redundant */
2446 if (value_eq(m
, P
->Ray
[r2
][1+nvar
+exist
+i1
])) {
2448 return enumerate_remove_ray(P
, r2
, exist
, nparam
, MaxRays
);
2456 static Polyhedron
*upper_bound(Polyhedron
*P
,
2457 int pos
, Value
*max
, Polyhedron
**R
)
2466 for (Polyhedron
*Q
= P
; Q
; Q
= N
) {
2468 for (r
= 0; r
< P
->NbRays
; ++r
) {
2469 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1]) &&
2470 value_pos_p(P
->Ray
[r
][1+pos
]))
2473 if (r
< P
->NbRays
) {
2481 for (r
= 0; r
< P
->NbRays
; ++r
) {
2482 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1]))
2484 mpz_fdiv_q(v
, P
->Ray
[r
][1+pos
], P
->Ray
[r
][1+P
->Dimension
]);
2485 if ((!Q
->next
&& r
== 0) || value_gt(v
, *max
))
2486 value_assign(*max
, v
);
2493 static evalue
* enumerate_ray(Polyhedron
*P
,
2494 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
2496 assert(P
->NbBid
== 0);
2497 int nvar
= P
->Dimension
- exist
- nparam
;
2500 for (r
= 0; r
< P
->NbRays
; ++r
)
2501 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1]))
2507 for (r2
= r
+1; r2
< P
->NbRays
; ++r2
)
2508 if (value_zero_p(P
->Ray
[r2
][P
->Dimension
+1]))
2510 if (r2
< P
->NbRays
) {
2512 return enumerate_sum(P
, exist
, nparam
, MaxRays
);
2516 fprintf(stderr
, "\nER: Ray\n");
2517 #endif /* DEBUG_ER */
2523 value_set_si(one
, 1);
2524 int i
= single_param_pos(P
, exist
, nparam
, r
);
2525 assert(i
!= -1); // for now;
2527 Matrix
*M
= Matrix_Alloc(P
->NbRays
, P
->Dimension
+2);
2528 for (int j
= 0; j
< P
->NbRays
; ++j
) {
2529 Vector_Combine(P
->Ray
[j
], P
->Ray
[r
], M
->p
[j
],
2530 one
, P
->Ray
[j
][P
->Dimension
+1], P
->Dimension
+2);
2532 Polyhedron
*S
= Rays2Polyhedron(M
, MaxRays
);
2534 Polyhedron
*D
= DomainDifference(P
, S
, MaxRays
);
2536 // Polyhedron_Print(stderr, P_VALUE_FMT, D);
2537 assert(value_pos_p(P
->Ray
[r
][1+nvar
+exist
+i
])); // for now
2539 D
= upper_bound(D
, nvar
+exist
+i
, &m
, &R
);
2543 M
= Matrix_Alloc(2, P
->Dimension
+2);
2544 value_set_si(M
->p
[0][0], 1);
2545 value_set_si(M
->p
[1][0], 1);
2546 value_set_si(M
->p
[0][1+nvar
+exist
+i
], -1);
2547 value_set_si(M
->p
[1][1+nvar
+exist
+i
], 1);
2548 value_assign(M
->p
[0][1+P
->Dimension
], m
);
2549 value_oppose(M
->p
[1][1+P
->Dimension
], m
);
2550 value_addto(M
->p
[1][1+P
->Dimension
], M
->p
[1][1+P
->Dimension
],
2551 P
->Ray
[r
][1+nvar
+exist
+i
]);
2552 value_decrement(M
->p
[1][1+P
->Dimension
], M
->p
[1][1+P
->Dimension
]);
2553 // Matrix_Print(stderr, P_VALUE_FMT, M);
2554 D
= AddConstraints(M
->p
[0], 2, P
, MaxRays
);
2555 // Polyhedron_Print(stderr, P_VALUE_FMT, D);
2556 value_substract(M
->p
[0][1+P
->Dimension
], M
->p
[0][1+P
->Dimension
],
2557 P
->Ray
[r
][1+nvar
+exist
+i
]);
2558 // Matrix_Print(stderr, P_VALUE_FMT, M);
2559 S
= AddConstraints(M
->p
[0], 1, P
, MaxRays
);
2560 // Polyhedron_Print(stderr, P_VALUE_FMT, S);
2563 evalue
*EP
= barvinok_enumerate_e(D
, exist
, nparam
, MaxRays
);
2568 if (value_notone_p(P
->Ray
[r
][1+nvar
+exist
+i
]))
2569 EP
= enumerate_cyclic(P
, exist
, nparam
, EP
, r
, i
, MaxRays
);
2571 M
= Matrix_Alloc(1, nparam
+2);
2572 value_set_si(M
->p
[0][0], 1);
2573 value_set_si(M
->p
[0][1+i
], 1);
2574 enumerate_vd_add_ray(EP
, M
, MaxRays
);
2579 evalue
*E
= barvinok_enumerate_e(S
, exist
, nparam
, MaxRays
);
2581 free_evalue_refs(E
);
2588 evalue
*ER
= enumerate_or(R
, exist
, nparam
, MaxRays
);
2590 free_evalue_refs(ER
);
2597 static evalue
* new_zero_ep()
2602 evalue_set_si(EP
, 0, 1);
2606 static evalue
* enumerate_vd(Polyhedron
**PA
,
2607 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
2609 Polyhedron
*P
= *PA
;
2610 int nvar
= P
->Dimension
- exist
- nparam
;
2611 Param_Polyhedron
*PP
= NULL
;
2612 Polyhedron
*C
= Universe_Polyhedron(nparam
);
2616 PP
= Polyhedron2Param_SimplifiedDomain(&PR
,C
,MaxRays
,&CEq
,&CT
);
2620 Param_Domain
*D
, *last
;
2623 for (nd
= 0, D
=PP
->D
; D
; D
=D
->next
, ++nd
)
2626 Polyhedron
**VD
= new Polyhedron_p
[nd
];
2627 Polyhedron
**fVD
= new Polyhedron_p
[nd
];
2628 for(nd
= 0, D
=PP
->D
; D
; D
=D
->next
) {
2629 Polyhedron
*rVD
= reduce_domain(D
->Domain
, CT
, CEq
,
2643 /* This doesn't seem to have any effect */
2645 Polyhedron
*CA
= align_context(VD
[0], P
->Dimension
, MaxRays
);
2647 P
= DomainIntersection(P
, CA
, MaxRays
);
2650 Polyhedron_Free(CA
);
2655 if (!EP
&& CT
->NbColumns
!= CT
->NbRows
) {
2656 Polyhedron
*CEqr
= DomainImage(CEq
, CT
, MaxRays
);
2657 Polyhedron
*CA
= align_context(CEqr
, PR
->Dimension
, MaxRays
);
2658 Polyhedron
*I
= DomainIntersection(PR
, CA
, MaxRays
);
2659 Polyhedron_Free(CEqr
);
2660 Polyhedron_Free(CA
);
2662 fprintf(stderr
, "\nER: Eliminate\n");
2663 #endif /* DEBUG_ER */
2664 nparam
-= CT
->NbColumns
- CT
->NbRows
;
2665 EP
= barvinok_enumerate_e(I
, exist
, nparam
, MaxRays
);
2666 nparam
+= CT
->NbColumns
- CT
->NbRows
;
2667 addeliminatedparams_enum(EP
, CT
, CEq
, MaxRays
, nparam
);
2671 Polyhedron_Free(PR
);
2674 if (!EP
&& nd
> 1) {
2676 fprintf(stderr
, "\nER: VD\n");
2677 #endif /* DEBUG_ER */
2678 for (int i
= 0; i
< nd
; ++i
) {
2679 Polyhedron
*CA
= align_context(VD
[i
], P
->Dimension
, MaxRays
);
2680 Polyhedron
*I
= DomainIntersection(P
, CA
, MaxRays
);
2683 EP
= barvinok_enumerate_e(I
, exist
, nparam
, MaxRays
);
2685 evalue
*E
= barvinok_enumerate_e(I
, exist
, nparam
, MaxRays
);
2687 free_evalue_refs(E
);
2691 Polyhedron_Free(CA
);
2695 for (int i
= 0; i
< nd
; ++i
) {
2696 Polyhedron_Free(VD
[i
]);
2697 Polyhedron_Free(fVD
[i
]);
2703 if (!EP
&& nvar
== 0) {
2706 Param_Vertices
*V
, *V2
;
2707 Matrix
* M
= Matrix_Alloc(1, P
->Dimension
+2);
2709 FORALL_PVertex_in_ParamPolyhedron(V
, last
, PP
) {
2711 FORALL_PVertex_in_ParamPolyhedron(V2
, last
, PP
) {
2718 for (int i
= 0; i
< exist
; ++i
) {
2719 value_oppose(f
, V
->Vertex
->p
[i
][nparam
+1]);
2720 Vector_Combine(V
->Vertex
->p
[i
],
2722 M
->p
[0] + 1 + nvar
+ exist
,
2723 V2
->Vertex
->p
[i
][nparam
+1],
2727 for (j
= 0; j
< nparam
; ++j
)
2728 if (value_notzero_p(M
->p
[0][1+nvar
+exist
+j
]))
2732 ConstraintSimplify(M
->p
[0], M
->p
[0],
2733 P
->Dimension
+2, &f
);
2734 value_set_si(M
->p
[0][0], 0);
2735 Polyhedron
*para
= AddConstraints(M
->p
[0], 1, P
,
2738 Polyhedron_Free(para
);
2741 Polyhedron
*pos
, *neg
;
2742 value_set_si(M
->p
[0][0], 1);
2743 value_decrement(M
->p
[0][P
->Dimension
+1],
2744 M
->p
[0][P
->Dimension
+1]);
2745 neg
= AddConstraints(M
->p
[0], 1, P
, MaxRays
);
2746 value_set_si(f
, -1);
2747 Vector_Scale(M
->p
[0]+1, M
->p
[0]+1, f
,
2749 value_decrement(M
->p
[0][P
->Dimension
+1],
2750 M
->p
[0][P
->Dimension
+1]);
2751 value_decrement(M
->p
[0][P
->Dimension
+1],
2752 M
->p
[0][P
->Dimension
+1]);
2753 pos
= AddConstraints(M
->p
[0], 1, P
, MaxRays
);
2754 if (emptyQ(neg
) && emptyQ(pos
)) {
2755 Polyhedron_Free(para
);
2756 Polyhedron_Free(pos
);
2757 Polyhedron_Free(neg
);
2761 fprintf(stderr
, "\nER: Order\n");
2762 #endif /* DEBUG_ER */
2763 EP
= barvinok_enumerate_e(para
, exist
, nparam
, MaxRays
);
2766 E
= barvinok_enumerate_e(pos
, exist
, nparam
, MaxRays
);
2768 free_evalue_refs(E
);
2772 E
= barvinok_enumerate_e(neg
, exist
, nparam
, MaxRays
);
2774 free_evalue_refs(E
);
2777 Polyhedron_Free(para
);
2778 Polyhedron_Free(pos
);
2779 Polyhedron_Free(neg
);
2784 } END_FORALL_PVertex_in_ParamPolyhedron
;
2787 } END_FORALL_PVertex_in_ParamPolyhedron
;
2790 /* Search for vertex coordinate to split on */
2791 /* First look for one independent of the parameters */
2792 FORALL_PVertex_in_ParamPolyhedron(V
, last
, PP
) {
2793 for (int i
= 0; i
< exist
; ++i
) {
2795 for (j
= 0; j
< nparam
; ++j
)
2796 if (value_notzero_p(V
->Vertex
->p
[i
][j
]))
2800 value_set_si(M
->p
[0][0], 1);
2801 Vector_Set(M
->p
[0]+1, 0, nvar
+exist
);
2802 Vector_Copy(V
->Vertex
->p
[i
],
2803 M
->p
[0] + 1 + nvar
+ exist
, nparam
+1);
2804 value_oppose(M
->p
[0][1+nvar
+i
],
2805 V
->Vertex
->p
[i
][nparam
+1]);
2807 Polyhedron
*pos
, *neg
;
2808 value_set_si(M
->p
[0][0], 1);
2809 value_decrement(M
->p
[0][P
->Dimension
+1],
2810 M
->p
[0][P
->Dimension
+1]);
2811 neg
= AddConstraints(M
->p
[0], 1, P
, MaxRays
);
2812 value_set_si(f
, -1);
2813 Vector_Scale(M
->p
[0]+1, M
->p
[0]+1, f
,
2815 value_decrement(M
->p
[0][P
->Dimension
+1],
2816 M
->p
[0][P
->Dimension
+1]);
2817 value_decrement(M
->p
[0][P
->Dimension
+1],
2818 M
->p
[0][P
->Dimension
+1]);
2819 pos
= AddConstraints(M
->p
[0], 1, P
, MaxRays
);
2820 if (emptyQ(neg
) || emptyQ(pos
)) {
2821 Polyhedron_Free(pos
);
2822 Polyhedron_Free(neg
);
2825 Polyhedron_Free(pos
);
2826 value_increment(M
->p
[0][P
->Dimension
+1],
2827 M
->p
[0][P
->Dimension
+1]);
2828 pos
= AddConstraints(M
->p
[0], 1, P
, MaxRays
);
2830 fprintf(stderr
, "\nER: Vertex\n");
2831 #endif /* DEBUG_ER */
2833 EP
= enumerate_or(pos
, exist
, nparam
, MaxRays
);
2838 } END_FORALL_PVertex_in_ParamPolyhedron
;
2842 /* Search for vertex coordinate to split on */
2843 /* Now look for one that depends on the parameters */
2844 FORALL_PVertex_in_ParamPolyhedron(V
, last
, PP
) {
2845 for (int i
= 0; i
< exist
; ++i
) {
2846 value_set_si(M
->p
[0][0], 1);
2847 Vector_Set(M
->p
[0]+1, 0, nvar
+exist
);
2848 Vector_Copy(V
->Vertex
->p
[i
],
2849 M
->p
[0] + 1 + nvar
+ exist
, nparam
+1);
2850 value_oppose(M
->p
[0][1+nvar
+i
],
2851 V
->Vertex
->p
[i
][nparam
+1]);
2853 Polyhedron
*pos
, *neg
;
2854 value_set_si(M
->p
[0][0], 1);
2855 value_decrement(M
->p
[0][P
->Dimension
+1],
2856 M
->p
[0][P
->Dimension
+1]);
2857 neg
= AddConstraints(M
->p
[0], 1, P
, MaxRays
);
2858 value_set_si(f
, -1);
2859 Vector_Scale(M
->p
[0]+1, M
->p
[0]+1, f
,
2861 value_decrement(M
->p
[0][P
->Dimension
+1],
2862 M
->p
[0][P
->Dimension
+1]);
2863 value_decrement(M
->p
[0][P
->Dimension
+1],
2864 M
->p
[0][P
->Dimension
+1]);
2865 pos
= AddConstraints(M
->p
[0], 1, P
, MaxRays
);
2866 if (emptyQ(neg
) || emptyQ(pos
)) {
2867 Polyhedron_Free(pos
);
2868 Polyhedron_Free(neg
);
2871 Polyhedron_Free(pos
);
2872 value_increment(M
->p
[0][P
->Dimension
+1],
2873 M
->p
[0][P
->Dimension
+1]);
2874 pos
= AddConstraints(M
->p
[0], 1, P
, MaxRays
);
2876 fprintf(stderr
, "\nER: ParamVertex\n");
2877 #endif /* DEBUG_ER */
2879 EP
= enumerate_or(pos
, exist
, nparam
, MaxRays
);
2884 } END_FORALL_PVertex_in_ParamPolyhedron
;
2892 Polyhedron_Free(CEq
);
2896 Param_Polyhedron_Free(PP
);
2903 evalue
*barvinok_enumerate_pip(Polyhedron
*P
,
2904 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
2909 evalue
*barvinok_enumerate_pip(Polyhedron
*P
,
2910 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
2912 int nvar
= P
->Dimension
- exist
- nparam
;
2913 evalue
*EP
= new_zero_ep();
2914 Polyhedron
*Q
, *N
, *T
= 0;
2920 fprintf(stderr
, "\nER: PIP\n");
2921 #endif /* DEBUG_ER */
2923 for (int i
= 0; i
< P
->Dimension
; ++i
) {
2926 bool posray
= false;
2927 bool negray
= false;
2928 value_set_si(min
, 0);
2929 for (int j
= 0; j
< P
->NbRays
; ++j
) {
2930 if (value_pos_p(P
->Ray
[j
][1+i
])) {
2932 if (value_zero_p(P
->Ray
[j
][1+P
->Dimension
]))
2934 } else if (value_neg_p(P
->Ray
[j
][1+i
])) {
2936 if (value_zero_p(P
->Ray
[j
][1+P
->Dimension
]))
2940 P
->Ray
[j
][1+i
], P
->Ray
[j
][1+P
->Dimension
]);
2941 if (value_lt(tmp
, min
))
2942 value_assign(min
, tmp
);
2947 assert(!(posray
&& negray
));
2948 assert(!negray
); // for now
2949 Polyhedron
*O
= T
? T
: P
;
2950 /* shift by a safe amount */
2951 Matrix
*M
= Matrix_Alloc(O
->NbRays
, O
->Dimension
+2);
2952 Vector_Copy(O
->Ray
[0], M
->p
[0], O
->NbRays
* (O
->Dimension
+2));
2953 for (int j
= 0; j
< P
->NbRays
; ++j
) {
2954 if (value_notzero_p(M
->p
[j
][1+P
->Dimension
])) {
2955 value_multiply(tmp
, min
, M
->p
[j
][1+P
->Dimension
]);
2956 value_substract(M
->p
[j
][1+i
], M
->p
[j
][1+i
], tmp
);
2961 T
= Rays2Polyhedron(M
, MaxRays
);
2964 /* negating a parameter requires that we substitute in the
2965 * sign again afterwards.
2968 assert(i
< nvar
+exist
);
2970 T
= Polyhedron_Copy(P
);
2971 for (int j
= 0; j
< T
->NbRays
; ++j
)
2972 value_oppose(T
->Ray
[j
][1+i
], T
->Ray
[j
][1+i
]);
2973 for (int j
= 0; j
< T
->NbConstraints
; ++j
)
2974 value_oppose(T
->Constraint
[j
][1+i
], T
->Constraint
[j
][1+i
]);
2980 Polyhedron
*D
= pip_lexmin(T
? T
: P
, exist
, nparam
);
2981 for (Q
= D
; Q
; Q
= N
) {
2985 exist
= Q
->Dimension
- nvar
- nparam
;
2986 E
= barvinok_enumerate_e(Q
, exist
, nparam
, MaxRays
);
2989 free_evalue_refs(E
);
3001 static bool is_single(Value
*row
, int pos
, int len
)
3003 return First_Non_Zero(row
, pos
) == -1 &&
3004 First_Non_Zero(row
+pos
+1, len
-pos
-1) == -1;
3007 static evalue
* barvinok_enumerate_e_r(Polyhedron
*P
,
3008 unsigned exist
, unsigned nparam
, unsigned MaxRays
);
3011 static int er_level
= 0;
3013 evalue
* barvinok_enumerate_e(Polyhedron
*P
,
3014 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
3016 fprintf(stderr
, "\nER: level %i\n", er_level
);
3017 int nvar
= P
->Dimension
- exist
- nparam
;
3018 fprintf(stderr
, "%d %d %d\n", nvar
, exist
, nparam
);
3020 Polyhedron_Print(stderr
, P_VALUE_FMT
, P
);
3022 P
= DomainConstraintSimplify(Polyhedron_Copy(P
), MaxRays
);
3023 evalue
*EP
= barvinok_enumerate_e_r(P
, exist
, nparam
, MaxRays
);
3029 evalue
* barvinok_enumerate_e(Polyhedron
*P
,
3030 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
3032 P
= DomainConstraintSimplify(Polyhedron_Copy(P
), MaxRays
);
3033 evalue
*EP
= barvinok_enumerate_e_r(P
, exist
, nparam
, MaxRays
);
3039 static evalue
* barvinok_enumerate_e_r(Polyhedron
*P
,
3040 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
3043 Polyhedron
*U
= Universe_Polyhedron(nparam
);
3044 evalue
*EP
= barvinok_enumerate_ev(P
, U
, MaxRays
);
3045 //char *param_name[] = {"P", "Q", "R", "S", "T" };
3046 //print_evalue(stdout, EP, param_name);
3051 int nvar
= P
->Dimension
- exist
- nparam
;
3052 int len
= P
->Dimension
+ 2;
3055 return new_zero_ep();
3057 if (nvar
== 0 && nparam
== 0) {
3058 evalue
*EP
= new_zero_ep();
3059 barvinok_count(P
, &EP
->x
.n
, MaxRays
);
3060 if (value_pos_p(EP
->x
.n
))
3061 value_set_si(EP
->x
.n
, 1);
3066 for (r
= 0; r
< P
->NbRays
; ++r
)
3067 if (value_zero_p(P
->Ray
[r
][0]) ||
3068 value_zero_p(P
->Ray
[r
][P
->Dimension
+1])) {
3070 for (i
= 0; i
< nvar
; ++i
)
3071 if (value_notzero_p(P
->Ray
[r
][i
+1]))
3075 for (i
= nvar
+ exist
; i
< nvar
+ exist
+ nparam
; ++i
)
3076 if (value_notzero_p(P
->Ray
[r
][i
+1]))
3078 if (i
>= nvar
+ exist
+ nparam
)
3081 if (r
< P
->NbRays
) {
3082 evalue
*EP
= new_zero_ep();
3083 value_set_si(EP
->x
.n
, -1);
3088 for (r
= 0; r
< P
->NbEq
; ++r
)
3089 if ((first
= First_Non_Zero(P
->Constraint
[r
]+1+nvar
, exist
)) != -1)
3092 if (First_Non_Zero(P
->Constraint
[r
]+1+nvar
+first
+1,
3093 exist
-first
-1) != -1) {
3094 Polyhedron
*T
= rotate_along(P
, r
, nvar
, exist
, MaxRays
);
3096 fprintf(stderr
, "\nER: Equality\n");
3097 #endif /* DEBUG_ER */
3098 evalue
*EP
= barvinok_enumerate_e(T
, exist
-1, nparam
, MaxRays
);
3103 fprintf(stderr
, "\nER: Fixed\n");
3104 #endif /* DEBUG_ER */
3106 return barvinok_enumerate_e(P
, exist
-1, nparam
, MaxRays
);
3108 Polyhedron
*T
= Polyhedron_Copy(P
);
3109 SwapColumns(T
, nvar
+1, nvar
+1+first
);
3110 evalue
*EP
= barvinok_enumerate_e(T
, exist
-1, nparam
, MaxRays
);
3117 Vector
*row
= Vector_Alloc(len
);
3118 value_set_si(row
->p
[0], 1);
3123 enum constraint
* info
= new constraint
[exist
];
3124 for (int i
= 0; i
< exist
; ++i
) {
3126 for (int l
= P
->NbEq
; l
< P
->NbConstraints
; ++l
) {
3127 if (value_negz_p(P
->Constraint
[l
][nvar
+i
+1]))
3129 bool l_parallel
= is_single(P
->Constraint
[l
]+nvar
+1, i
, exist
);
3130 for (int u
= P
->NbEq
; u
< P
->NbConstraints
; ++u
) {
3131 if (value_posz_p(P
->Constraint
[u
][nvar
+i
+1]))
3133 bool lu_parallel
= l_parallel
||
3134 is_single(P
->Constraint
[u
]+nvar
+1, i
, exist
);
3135 value_oppose(f
, P
->Constraint
[u
][nvar
+i
+1]);
3136 Vector_Combine(P
->Constraint
[l
]+1, P
->Constraint
[u
]+1, row
->p
+1,
3137 f
, P
->Constraint
[l
][nvar
+i
+1], len
-1);
3138 if (!(info
[i
] & INDEPENDENT
)) {
3140 for (j
= 0; j
< exist
; ++j
)
3141 if (j
!= i
&& value_notzero_p(row
->p
[nvar
+j
+1]))
3144 //printf("independent: i: %d, l: %d, u: %d\n", i, l, u);
3145 info
[i
] = (constraint
)(info
[i
] | INDEPENDENT
);
3148 if (info
[i
] & ALL_POS
) {
3149 value_addto(row
->p
[len
-1], row
->p
[len
-1],
3150 P
->Constraint
[l
][nvar
+i
+1]);
3151 value_addto(row
->p
[len
-1], row
->p
[len
-1], f
);
3152 value_multiply(f
, f
, P
->Constraint
[l
][nvar
+i
+1]);
3153 value_substract(row
->p
[len
-1], row
->p
[len
-1], f
);
3154 value_decrement(row
->p
[len
-1], row
->p
[len
-1]);
3155 ConstraintSimplify(row
->p
, row
->p
, len
, &f
);
3156 value_set_si(f
, -1);
3157 Vector_Scale(row
->p
+1, row
->p
+1, f
, len
-1);
3158 value_decrement(row
->p
[len
-1], row
->p
[len
-1]);
3159 Polyhedron
*T
= AddConstraints(row
->p
, 1, P
, MaxRays
);
3161 //printf("not all_pos: i: %d, l: %d, u: %d\n", i, l, u);
3162 info
[i
] = (constraint
)(info
[i
] ^ ALL_POS
);
3164 //puts("pos remainder");
3165 //Polyhedron_Print(stdout, P_VALUE_FMT, T);
3168 if (!(info
[i
] & ONE_NEG
)) {
3170 negative_test_constraint(P
->Constraint
[l
],
3172 row
->p
, nvar
+i
, len
, &f
);
3173 oppose_constraint(row
->p
, len
, &f
);
3174 Polyhedron
*T
= AddConstraints(row
->p
, 1, P
, MaxRays
);
3176 //printf("one_neg i: %d, l: %d, u: %d\n", i, l, u);
3177 info
[i
] = (constraint
)(info
[i
] | ONE_NEG
);
3179 //puts("neg remainder");
3180 //Polyhedron_Print(stdout, P_VALUE_FMT, T);
3184 if (!(info
[i
] & ALL_POS
) && (info
[i
] & ONE_NEG
))
3188 if (info
[i
] & ALL_POS
)
3195 for (int i = 0; i < exist; ++i)
3196 printf("%i: %i\n", i, info[i]);
3198 for (int i
= 0; i
< exist
; ++i
)
3199 if (info
[i
] & ALL_POS
) {
3201 fprintf(stderr
, "\nER: Positive\n");
3202 #endif /* DEBUG_ER */
3204 // Maybe we should chew off some of the fat here
3205 Matrix
*M
= Matrix_Alloc(P
->Dimension
, P
->Dimension
+1);
3206 for (int j
= 0; j
< P
->Dimension
; ++j
)
3207 value_set_si(M
->p
[j
][j
+ (j
>= i
+nvar
)], 1);
3208 Polyhedron
*T
= Polyhedron_Image(P
, M
, MaxRays
);
3210 evalue
*EP
= barvinok_enumerate_e(T
, exist
-1, nparam
, MaxRays
);
3217 for (int i
= 0; i
< exist
; ++i
)
3218 if (info
[i
] & ONE_NEG
) {
3220 fprintf(stderr
, "\nER: Negative\n");
3221 #endif /* DEBUG_ER */
3226 return barvinok_enumerate_e(P
, exist
-1, nparam
, MaxRays
);
3228 Polyhedron
*T
= Polyhedron_Copy(P
);
3229 SwapColumns(T
, nvar
+1, nvar
+1+i
);
3230 evalue
*EP
= barvinok_enumerate_e(T
, exist
-1, nparam
, MaxRays
);
3235 for (int i
= 0; i
< exist
; ++i
)
3236 if (info
[i
] & INDEPENDENT
) {
3237 Polyhedron
*pos
, *neg
;
3239 /* Find constraint again and split off negative part */
3241 if (SplitOnVar(P
, i
, nvar
, len
, exist
, MaxRays
,
3242 row
, f
, true, &pos
, &neg
)) {
3244 fprintf(stderr
, "\nER: Split\n");
3245 #endif /* DEBUG_ER */
3248 barvinok_enumerate_e(neg
, exist
-1, nparam
, MaxRays
);
3250 barvinok_enumerate_e(pos
, exist
, nparam
, MaxRays
);
3252 free_evalue_refs(E
);
3254 Polyhedron_Free(neg
);
3255 Polyhedron_Free(pos
);
3269 EP
= enumerate_line(P
, exist
, nparam
, MaxRays
);
3273 EP
= barvinok_enumerate_pip(P
, exist
, nparam
, MaxRays
);
3277 EP
= enumerate_redundant_ray(P
, exist
, nparam
, MaxRays
);
3281 EP
= enumerate_sure(P
, exist
, nparam
, MaxRays
);
3285 EP
= enumerate_ray(P
, exist
, nparam
, MaxRays
);
3289 EP
= enumerate_sure2(P
, exist
, nparam
, MaxRays
);
3293 F
= unfringe(P
, MaxRays
);
3294 if (!PolyhedronIncludes(F
, P
)) {
3296 fprintf(stderr
, "\nER: Fringed\n");
3297 #endif /* DEBUG_ER */
3298 EP
= barvinok_enumerate_e(F
, exist
, nparam
, MaxRays
);
3305 EP
= enumerate_vd(&P
, exist
, nparam
, MaxRays
);
3310 EP
= enumerate_sum(P
, exist
, nparam
, MaxRays
);
3317 Polyhedron
*pos
, *neg
;
3318 for (i
= 0; i
< exist
; ++i
)
3319 if (SplitOnVar(P
, i
, nvar
, len
, exist
, MaxRays
,
3320 row
, f
, false, &pos
, &neg
))
3326 EP
= enumerate_or(pos
, exist
, nparam
, MaxRays
);
3338 static void normalize(Polyhedron
*i
, vec_ZZ
& lambda
, ZZ
& sign
,
3339 ZZ
& num_s
, vec_ZZ
& num_p
, vec_ZZ
& den_s
, vec_ZZ
& den_p
,
3342 unsigned dim
= i
->Dimension
;
3343 unsigned nparam
= num_p
.length();
3344 unsigned nvar
= dim
- nparam
;
3348 rays
.SetDims(dim
, nvar
);
3349 add_rays(rays
, i
, &r
, nvar
, true);
3350 den_s
= rays
* lambda
;
3354 for (int j
= 0; j
< den_s
.length(); ++j
) {
3355 values2zz(i
->Ray
[j
]+1+nvar
, f
[j
], nparam
);
3356 if (den_s
[j
] == 0) {
3360 if (First_Non_Zero(i
->Ray
[j
]+1+nvar
, nparam
) != -1) {
3371 den_s
[j
] = abs(den_s
[j
]);
3380 gen_fun
* barvinok_series(Polyhedron
*P
, Polyhedron
* C
, unsigned MaxRays
)
3382 Polyhedron
** vcone
;
3384 unsigned nparam
= C
->Dimension
;
3388 sign
.SetLength(ncone
);
3390 CA
= align_context(C
, P
->Dimension
, MaxRays
);
3391 P
= DomainIntersection(P
, CA
, MaxRays
);
3392 Polyhedron_Free(CA
);
3394 assert(!Polyhedron_is_infinite(P
, nparam
));
3395 assert(P
->NbBid
== 0);
3396 assert(Polyhedron_has_positive_rays(P
, nparam
));
3397 assert(P
->NbEq
== 0);
3400 nvar
= dim
- nparam
;
3401 vcone
= new Polyhedron_p
[P
->NbRays
];
3403 for (int j
= 0; j
< P
->NbRays
; ++j
) {
3404 if (!value_pos_p(P
->Ray
[j
][dim
+1]))
3408 Polyhedron
*C
= supporting_cone(P
, j
);
3409 decompose(C
, &vcone
[j
], &npos
, &nneg
, MaxRays
);
3410 ncone
+= npos
+ nneg
;
3411 sign
.SetLength(ncone
);
3412 for (int k
= 0; k
< npos
; ++k
)
3413 sign
[ncone
-nneg
-k
-1] = 1;
3414 for (int k
= 0; k
< nneg
; ++k
)
3415 sign
[ncone
-k
-1] = -1;
3419 rays
.SetDims(ncone
* dim
, nvar
);
3421 for (int j
= 0; j
< P
->NbRays
; ++j
) {
3422 if (!value_pos_p(P
->Ray
[j
][dim
+1]))
3425 for (Polyhedron
*i
= vcone
[j
]; i
; i
= i
->next
) {
3426 add_rays(rays
, i
, &r
, nvar
);
3429 rays
.SetDims(r
, nvar
);
3431 nonorthog(rays
, lambda
);
3432 //randomvector(P, lambda, nvar);
3435 cout << "rays: " << rays;
3436 cout << "lambda: " << lambda;
3442 num_p
.SetLength(nparam
);
3445 den_s
.SetLength(dim
);
3447 den_p
.SetLength(dim
);
3449 den
.SetDims(dim
, nparam
);
3455 gen_fun
* gf
= new gen_fun
;
3457 for (int j
= 0; j
< P
->NbRays
; ++j
) {
3458 if (!value_pos_p(P
->Ray
[j
][dim
+1]))
3461 for (Polyhedron
*i
= vcone
[j
]; i
; i
= i
->next
, ++f
) {
3462 lattice_point(P
->Ray
[j
]+1, i
, vertex
);
3465 for ( ; k
< nvar
; ++k
)
3466 num_s
+= vertex
[k
] * lambda
[k
];
3467 for ( ; k
< dim
; ++k
)
3468 num_p
[k
-nvar
] = vertex
[k
];
3469 normalize(i
, lambda
, sign
[f
], num_s
, num_p
,
3474 for (int k
= 0; k
< dim
; ++k
) {
3477 else if (den_s
[k
] == 0)
3480 if (no_param
== 0) {
3481 for (int k
= 0; k
< dim
; ++k
)
3484 gf
->add(sign
[f
], one
, num_p
, den
);
3485 } else if (no_param
+ only_param
== dim
) {
3488 pden
.SetDims(only_param
, nparam
);
3490 for (k
= 0, l
= 0; k
< dim
; ++k
)
3494 for (k
= 0; k
< dim
; ++k
)
3498 dpoly
n(no_param
, num_s
);
3499 dpoly
d(no_param
, den_s
[k
], 1);
3500 for ( ; k
< dim
; ++k
)
3501 if (den_s
[k
] != 0) {
3502 dpoly
fact(no_param
, den_s
[k
], 1);
3506 mpq_set_si(count
, 0, 1);
3507 n
.div(d
, count
, sign
[f
]);
3510 value2zz(mpq_numref(count
), qn
);
3511 value2zz(mpq_denref(count
), qd
);
3513 gf
->add(qn
, qd
, num_p
, pden
);
3518 pden
.SetDims(only_param
, nparam
);
3520 for (k
= 0, l
= 0; k
< dim
; ++k
)
3524 for (k
= 0; k
< dim
; ++k
)
3528 dpoly
n(no_param
, num_s
);
3529 dpoly
d(no_param
, den_s
[k
], 1);
3530 for ( ; k
< dim
; ++k
)
3531 if (den_p
[k
] == 0) {
3532 dpoly
fact(no_param
, den_s
[k
], 1);
3536 for (k
= 0; k
< dim
; ++k
) {
3537 if (den_s
[k
] == 0 || den_p
[k
] == 0)
3540 dpoly
pd(no_param
-1, den_s
[k
], 1);
3541 int s
= den_p
[k
] < 0 ? -1 : 1;
3544 r
= new dpoly_r(n
, pd
, k
, s
, dim
);
3546 assert(0); // for now
3549 r
->div(d
, sign
[f
], gf
, pden
, den
, num_p
);
3553 cout << "sign: " << sign[f];
3554 cout << "num_s: " << num_s;
3555 cout << "num_p: " << num_p;
3556 cout << "den_s: " << den_s;
3557 cout << "den_p: " << den_p;
3558 cout << "den: " << den;
3559 cout << "only_param: " << only_param;
3560 cout << "no_param: " << no_param;