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 othogonal 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 add_rays(mat_ZZ
& rays
, Polyhedron
*i
, int *r
, int nvar
= -1,
628 unsigned dim
= i
->Dimension
;
631 for (int k
= 0; k
< i
->NbRays
; ++k
) {
632 if (!value_zero_p(i
->Ray
[k
][dim
+1]))
634 if (!all
&& nvar
!= dim
&& First_Non_Zero(i
->Ray
[k
]+1, nvar
) == -1)
636 values2zz(i
->Ray
[k
]+1, rays
[(*r
)++], nvar
);
640 void lattice_point(Value
* values
, Polyhedron
*i
, vec_ZZ
& vertex
)
642 unsigned dim
= i
->Dimension
;
643 if(!value_one_p(values
[dim
])) {
644 Matrix
* Rays
= rays(i
);
645 Matrix
*inv
= Matrix_Alloc(Rays
->NbRows
, Rays
->NbColumns
);
646 int ok
= Matrix_Inverse(Rays
, inv
);
650 Vector
*lambda
= Vector_Alloc(dim
+1);
651 Vector_Matrix_Product(values
, inv
, lambda
->p
);
653 for (int j
= 0; j
< dim
; ++j
)
654 mpz_cdiv_q(lambda
->p
[j
], lambda
->p
[j
], lambda
->p
[dim
]);
655 value_set_si(lambda
->p
[dim
], 1);
656 Vector
*A
= Vector_Alloc(dim
+1);
657 Vector_Matrix_Product(lambda
->p
, Rays
, A
->p
);
660 values2zz(A
->p
, vertex
, dim
);
663 values2zz(values
, vertex
, dim
);
666 static evalue
*term(int param
, ZZ
& c
, Value
*den
= NULL
)
668 evalue
*EP
= new evalue();
670 value_set_si(EP
->d
,0);
671 EP
->x
.p
= new_enode(polynomial
, 2, param
+ 1);
672 evalue_set_si(&EP
->x
.p
->arr
[0], 0, 1);
673 value_init(EP
->x
.p
->arr
[1].x
.n
);
675 value_set_si(EP
->x
.p
->arr
[1].d
, 1);
677 value_assign(EP
->x
.p
->arr
[1].d
, *den
);
678 zz2value(c
, EP
->x
.p
->arr
[1].x
.n
);
682 static void vertex_period(
683 Polyhedron
*i
, vec_ZZ
& lambda
, Matrix
*T
,
684 Value lcm
, int p
, Vector
*val
,
685 evalue
*E
, evalue
* ev
,
688 unsigned nparam
= T
->NbRows
- 1;
689 unsigned dim
= i
->Dimension
;
696 Vector
* values
= Vector_Alloc(dim
+ 1);
697 Vector_Matrix_Product(val
->p
, T
, values
->p
);
698 value_assign(values
->p
[dim
], lcm
);
699 lattice_point(values
->p
, i
, vertex
);
700 num
= vertex
* lambda
;
705 zz2value(num
, ev
->x
.n
);
706 value_assign(ev
->d
, lcm
);
713 values2zz(T
->p
[p
], vertex
, dim
);
714 nump
= vertex
* lambda
;
715 if (First_Non_Zero(val
->p
, p
) == -1) {
716 value_assign(tmp
, lcm
);
717 evalue
*ET
= term(p
, nump
, &tmp
);
719 free_evalue_refs(ET
);
723 value_assign(tmp
, lcm
);
724 if (First_Non_Zero(T
->p
[p
], dim
) != -1)
725 Vector_Gcd(T
->p
[p
], dim
, &tmp
);
727 if (value_lt(tmp
, lcm
)) {
730 value_division(tmp
, lcm
, tmp
);
731 value_set_si(ev
->d
, 0);
732 ev
->x
.p
= new_enode(periodic
, VALUE_TO_INT(tmp
), p
+1);
733 value2zz(tmp
, count
);
735 value_decrement(tmp
, tmp
);
737 ZZ new_offset
= offset
- count
* nump
;
738 value_assign(val
->p
[p
], tmp
);
739 vertex_period(i
, lambda
, T
, lcm
, p
+1, val
, E
,
740 &ev
->x
.p
->arr
[VALUE_TO_INT(tmp
)], new_offset
);
741 } while (value_pos_p(tmp
));
743 vertex_period(i
, lambda
, T
, lcm
, p
+1, val
, E
, ev
, offset
);
747 static void mask_r(Matrix
*f
, int nr
, Vector
*lcm
, int p
, Vector
*val
, evalue
*ev
)
749 unsigned nparam
= lcm
->Size
;
752 Vector
* prod
= Vector_Alloc(f
->NbRows
);
753 Matrix_Vector_Product(f
, val
->p
, prod
->p
);
755 for (int i
= 0; i
< nr
; ++i
) {
756 value_modulus(prod
->p
[i
], prod
->p
[i
], f
->p
[i
][nparam
+1]);
757 isint
&= value_zero_p(prod
->p
[i
]);
759 value_set_si(ev
->d
, 1);
761 value_set_si(ev
->x
.n
, isint
);
768 if (value_one_p(lcm
->p
[p
]))
769 mask_r(f
, nr
, lcm
, p
+1, val
, ev
);
771 value_assign(tmp
, lcm
->p
[p
]);
772 value_set_si(ev
->d
, 0);
773 ev
->x
.p
= new_enode(periodic
, VALUE_TO_INT(tmp
), p
+1);
775 value_decrement(tmp
, tmp
);
776 value_assign(val
->p
[p
], tmp
);
777 mask_r(f
, nr
, lcm
, p
+1, val
, &ev
->x
.p
->arr
[VALUE_TO_INT(tmp
)]);
778 } while (value_pos_p(tmp
));
783 static evalue
*multi_monom(vec_ZZ
& p
)
785 evalue
*X
= new evalue();
788 unsigned nparam
= p
.length()-1;
789 zz2value(p
[nparam
], X
->x
.n
);
790 value_set_si(X
->d
, 1);
791 for (int i
= 0; i
< nparam
; ++i
) {
794 evalue
*T
= term(i
, p
[i
]);
803 * Check whether mapping polyhedron P on the affine combination
804 * num yields a range that has a fixed quotient on integer
806 * If zero is true, then we are only interested in the quotient
807 * for the cases where the remainder is zero.
808 * Returns NULL if false and a newly allocated value if true.
810 static Value
*fixed_quotient(Polyhedron
*P
, vec_ZZ
& num
, Value d
, bool zero
)
813 int len
= num
.length();
814 Matrix
*T
= Matrix_Alloc(2, len
);
815 zz2values(num
, T
->p
[0]);
816 value_set_si(T
->p
[1][len
-1], 1);
817 Polyhedron
*I
= Polyhedron_Image(P
, T
, P
->NbConstraints
);
821 for (i
= 0; i
< I
->NbRays
; ++i
)
822 if (value_zero_p(I
->Ray
[i
][2])) {
830 int bounded
= line_minmax(I
, &min
, &max
);
834 mpz_cdiv_q(min
, min
, d
);
836 mpz_fdiv_q(min
, min
, d
);
837 mpz_fdiv_q(max
, max
, d
);
839 if (value_eq(min
, max
)) {
842 value_assign(*ret
, min
);
850 * Normalize linear expression coef modulo m
851 * Removes common factor and reduces coefficients
852 * Returns index of first non-zero coefficient or len
854 static int normal_mod(Value
*coef
, int len
, Value
*m
)
859 Vector_Gcd(coef
, len
, &gcd
);
861 Vector_AntiScale(coef
, coef
, gcd
, len
);
863 value_division(*m
, *m
, gcd
);
870 for (j
= 0; j
< len
; ++j
)
871 mpz_fdiv_r(coef
[j
], coef
[j
], *m
);
872 for (j
= 0; j
< len
; ++j
)
873 if (value_notzero_p(coef
[j
]))
880 static void mask(Matrix
*f
, evalue
*factor
)
882 int nr
= f
->NbRows
, nc
= f
->NbColumns
;
885 for (n
= 0; n
< nr
&& value_notzero_p(f
->p
[n
][nc
-1]); ++n
)
886 if (value_notone_p(f
->p
[n
][nc
-1]) &&
887 value_notmone_p(f
->p
[n
][nc
-1]))
901 value_set_si(EV
.x
.n
, 1);
903 for (n
= 0; n
< nr
; ++n
) {
904 value_assign(m
, f
->p
[n
][nc
-1]);
905 if (value_one_p(m
) || value_mone_p(m
))
908 int j
= normal_mod(f
->p
[n
], nc
-1, &m
);
910 free_evalue_refs(factor
);
911 value_init(factor
->d
);
912 evalue_set_si(factor
, 0, 1);
916 values2zz(f
->p
[n
], row
, nc
-1);
919 if (j
< (nc
-1)-1 && row
[j
] > g
/2) {
920 for (int k
= j
; k
< (nc
-1); ++k
)
926 value_set_si(EP
.d
, 0);
927 EP
.x
.p
= new_enode(relation
, 2, 0);
928 value_clear(EP
.x
.p
->arr
[1].d
);
929 EP
.x
.p
->arr
[1] = *factor
;
930 evalue
*ev
= &EP
.x
.p
->arr
[0];
931 value_set_si(ev
->d
, 0);
932 ev
->x
.p
= new_enode(fractional
, 3, -1);
933 evalue_set_si(&ev
->x
.p
->arr
[1], 0, 1);
934 evalue_set_si(&ev
->x
.p
->arr
[2], 1, 1);
935 evalue
*E
= multi_monom(row
);
936 value_assign(EV
.d
, m
);
938 value_clear(ev
->x
.p
->arr
[0].d
);
939 ev
->x
.p
->arr
[0] = *E
;
945 free_evalue_refs(&EV
);
951 static void mask(Matrix
*f
, evalue
*factor
)
953 int nr
= f
->NbRows
, nc
= f
->NbColumns
;
956 for (n
= 0; n
< nr
&& value_notzero_p(f
->p
[n
][nc
-1]); ++n
)
957 if (value_notone_p(f
->p
[n
][nc
-1]) &&
958 value_notmone_p(f
->p
[n
][nc
-1]))
966 unsigned np
= nc
- 2;
967 Vector
*lcm
= Vector_Alloc(np
);
968 Vector
*val
= Vector_Alloc(nc
);
969 Vector_Set(val
->p
, 0, nc
);
970 value_set_si(val
->p
[np
], 1);
971 Vector_Set(lcm
->p
, 1, np
);
972 for (n
= 0; n
< nr
; ++n
) {
973 if (value_one_p(f
->p
[n
][nc
-1]) ||
974 value_mone_p(f
->p
[n
][nc
-1]))
976 for (int j
= 0; j
< np
; ++j
)
977 if (value_notzero_p(f
->p
[n
][j
])) {
978 Gcd(f
->p
[n
][j
], f
->p
[n
][nc
-1], &tmp
);
979 value_division(tmp
, f
->p
[n
][nc
-1], tmp
);
980 value_lcm(tmp
, lcm
->p
[j
], &lcm
->p
[j
]);
985 mask_r(f
, nr
, lcm
, 0, val
, &EP
);
990 free_evalue_refs(&EP
);
1001 static bool mod_needed(Polyhedron
*PD
, vec_ZZ
& num
, Value d
, evalue
*E
)
1003 Value
*q
= fixed_quotient(PD
, num
, d
, false);
1008 value_oppose(*q
, *q
);
1011 value_set_si(EV
.d
, 1);
1013 value_multiply(EV
.x
.n
, *q
, d
);
1015 free_evalue_refs(&EV
);
1021 static void ceil_mod(Value
*coef
, int len
, Value d
, ZZ
& f
, evalue
*EP
, Polyhedron
*PD
)
1025 value_set_si(m
, -1);
1027 Vector_Scale(coef
, coef
, m
, len
);
1030 int j
= normal_mod(coef
, len
, &m
);
1038 values2zz(coef
, num
, len
);
1045 evalue_set_si(&tmp
, 0, 1);
1049 while (j
< len
-1 && (num
[j
] == g
/2 || num
[j
] == 0))
1051 if ((j
< len
-1 && num
[j
] > g
/2) || (j
== len
-1 && num
[j
] >= (g
+1)/2)) {
1052 for (int k
= j
; k
< len
-1; ++k
)
1054 num
[k
] = g
- num
[k
];
1055 num
[len
-1] = g
- 1 - num
[len
-1];
1056 value_assign(tmp
.d
, m
);
1058 zz2value(t
, tmp
.x
.n
);
1064 ZZ t
= num
[len
-1] * f
;
1065 zz2value(t
, tmp
.x
.n
);
1066 value_assign(tmp
.d
, m
);
1069 evalue
*E
= multi_monom(num
);
1073 if (PD
&& !mod_needed(PD
, num
, m
, E
)) {
1075 zz2value(f
, EV
.x
.n
);
1076 value_assign(EV
.d
, m
);
1081 value_set_si(EV
.x
.n
, 1);
1082 value_assign(EV
.d
, m
);
1084 value_clear(EV
.x
.n
);
1085 value_set_si(EV
.d
, 0);
1086 EV
.x
.p
= new_enode(fractional
, 3, -1);
1087 evalue_copy(&EV
.x
.p
->arr
[0], E
);
1088 evalue_set_si(&EV
.x
.p
->arr
[1], 0, 1);
1089 value_init(EV
.x
.p
->arr
[2].x
.n
);
1090 zz2value(f
, EV
.x
.p
->arr
[2].x
.n
);
1091 value_set_si(EV
.x
.p
->arr
[2].d
, 1);
1096 free_evalue_refs(&EV
);
1097 free_evalue_refs(E
);
1101 free_evalue_refs(&tmp
);
1107 evalue
* bv_ceil3(Value
*coef
, int len
, Value d
, Polyhedron
*P
)
1109 Vector
*val
= Vector_Alloc(len
);
1113 value_set_si(t
, -1);
1114 Vector_Scale(coef
, val
->p
, t
, len
);
1115 value_absolute(t
, d
);
1118 values2zz(val
->p
, num
, len
);
1119 evalue
*EP
= multi_monom(num
);
1123 value_init(tmp
.x
.n
);
1124 value_set_si(tmp
.x
.n
, 1);
1125 value_assign(tmp
.d
, t
);
1131 ceil_mod(val
->p
, len
, t
, one
, EP
, P
);
1134 /* copy EP to malloc'ed evalue */
1140 free_evalue_refs(&tmp
);
1147 evalue
* lattice_point(
1148 Polyhedron
*i
, vec_ZZ
& lambda
, Matrix
*W
, Value lcm
, Polyhedron
*PD
)
1150 unsigned nparam
= W
->NbColumns
- 1;
1152 Matrix
* Rays
= rays2(i
);
1153 Matrix
*T
= Transpose(Rays
);
1154 Matrix
*T2
= Matrix_Copy(T
);
1155 Matrix
*inv
= Matrix_Alloc(T2
->NbRows
, T2
->NbColumns
);
1156 int ok
= Matrix_Inverse(T2
, inv
);
1161 matrix2zz(W
, vertex
, W
->NbRows
, W
->NbColumns
);
1164 num
= lambda
* vertex
;
1166 evalue
*EP
= multi_monom(num
);
1170 value_init(tmp
.x
.n
);
1171 value_set_si(tmp
.x
.n
, 1);
1172 value_assign(tmp
.d
, lcm
);
1176 Matrix
*L
= Matrix_Alloc(inv
->NbRows
, W
->NbColumns
);
1177 Matrix_Product(inv
, W
, L
);
1180 matrix2zz(T
, RT
, T
->NbRows
, T
->NbColumns
);
1183 vec_ZZ p
= lambda
* RT
;
1185 for (int i
= 0; i
< L
->NbRows
; ++i
) {
1186 ceil_mod(L
->p
[i
], nparam
+1, lcm
, p
[i
], EP
, PD
);
1192 free_evalue_refs(&tmp
);
1196 evalue
* lattice_point(
1197 Polyhedron
*i
, vec_ZZ
& lambda
, Matrix
*W
, Value lcm
, Polyhedron
*PD
)
1199 Matrix
*T
= Transpose(W
);
1200 unsigned nparam
= T
->NbRows
- 1;
1202 evalue
*EP
= new evalue();
1204 evalue_set_si(EP
, 0, 1);
1207 Vector
*val
= Vector_Alloc(nparam
+1);
1208 value_set_si(val
->p
[nparam
], 1);
1209 ZZ
offset(INIT_VAL
, 0);
1211 vertex_period(i
, lambda
, T
, lcm
, 0, val
, EP
, &ev
, offset
);
1214 free_evalue_refs(&ev
);
1225 Param_Vertices
* V
, Polyhedron
*i
, vec_ZZ
& lambda
, term_info
* term
,
1228 unsigned nparam
= V
->Vertex
->NbColumns
- 2;
1229 unsigned dim
= i
->Dimension
;
1231 vertex
.SetDims(V
->Vertex
->NbRows
, nparam
+1);
1235 value_set_si(lcm
, 1);
1236 for (int j
= 0; j
< V
->Vertex
->NbRows
; ++j
) {
1237 value_lcm(lcm
, V
->Vertex
->p
[j
][nparam
+1], &lcm
);
1239 if (value_notone_p(lcm
)) {
1240 Matrix
* mv
= Matrix_Alloc(dim
, nparam
+1);
1241 for (int j
= 0 ; j
< dim
; ++j
) {
1242 value_division(tmp
, lcm
, V
->Vertex
->p
[j
][nparam
+1]);
1243 Vector_Scale(V
->Vertex
->p
[j
], mv
->p
[j
], tmp
, nparam
+1);
1246 term
->E
= lattice_point(i
, lambda
, mv
, lcm
, PD
);
1254 for (int i
= 0; i
< V
->Vertex
->NbRows
; ++i
) {
1255 assert(value_one_p(V
->Vertex
->p
[i
][nparam
+1])); // for now
1256 values2zz(V
->Vertex
->p
[i
], vertex
[i
], nparam
+1);
1260 num
= lambda
* vertex
;
1264 for (int j
= 0; j
< nparam
; ++j
)
1270 term
->E
= multi_monom(num
);
1274 term
->constant
= num
[nparam
];
1277 term
->coeff
= num
[p
];
1284 void normalize(Polyhedron
*i
, vec_ZZ
& lambda
, ZZ
& sign
, ZZ
& num
, vec_ZZ
& den
)
1286 unsigned dim
= i
->Dimension
;
1290 rays
.SetDims(dim
, dim
);
1291 add_rays(rays
, i
, &r
);
1292 den
= rays
* lambda
;
1295 for (int j
= 0; j
< den
.length(); ++j
) {
1299 den
[j
] = abs(den
[j
]);
1307 typedef Polyhedron
* Polyhedron_p
;
1309 void barvinok_count(Polyhedron
*P
, Value
* result
, unsigned NbMaxCons
)
1311 Polyhedron
** vcone
;
1314 sign
.SetLength(ncone
);
1322 value_set_si(*result
, 0);
1326 for (; r
< P
->NbRays
; ++r
)
1327 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1]))
1329 if (P
->NbBid
!=0 || r
< P
->NbRays
) {
1330 value_set_si(*result
, -1);
1334 P
= remove_equalities(P
);
1337 value_set_si(*result
, 0);
1343 value_set_si(factor
, 1);
1344 Q
= Polyhedron_Reduce(P
, &factor
);
1351 if (P
->Dimension
== 0) {
1352 value_assign(*result
, factor
);
1355 value_clear(factor
);
1360 vcone
= new Polyhedron_p
[P
->NbRays
];
1362 for (int j
= 0; j
< P
->NbRays
; ++j
) {
1364 Polyhedron
*C
= supporting_cone(P
, j
);
1365 decompose(C
, &vcone
[j
], &npos
, &nneg
, NbMaxCons
);
1366 ncone
+= npos
+ nneg
;
1367 sign
.SetLength(ncone
);
1368 for (int k
= 0; k
< npos
; ++k
)
1369 sign
[ncone
-nneg
-k
-1] = 1;
1370 for (int k
= 0; k
< nneg
; ++k
)
1371 sign
[ncone
-k
-1] = -1;
1375 rays
.SetDims(ncone
* dim
, dim
);
1377 for (int j
= 0; j
< P
->NbRays
; ++j
) {
1378 for (Polyhedron
*i
= vcone
[j
]; i
; i
= i
->next
) {
1379 assert(i
->NbRays
-1 == dim
);
1380 add_rays(rays
, i
, &r
);
1384 nonorthog(rays
, lambda
);
1394 for (int j
= 0; j
< P
->NbRays
; ++j
) {
1395 for (Polyhedron
*i
= vcone
[j
]; i
; i
= i
->next
) {
1396 lattice_point(P
->Ray
[j
]+1, i
, vertex
);
1397 num
= vertex
* lambda
;
1398 normalize(i
, lambda
, sign
[f
], num
, den
);
1401 dpoly
n(dim
, den
[0], 1);
1402 for (int k
= 1; k
< dim
; ++k
) {
1403 dpoly
fact(dim
, den
[k
], 1);
1406 d
.div(n
, count
, sign
[f
]);
1410 Domain_Free(vcone
[j
]);
1413 assert(value_one_p(&count
[0]._mp_den
));
1414 value_multiply(*result
, &count
[0]._mp_num
, factor
);
1421 value_clear(factor
);
1424 static void uni_polynom(int param
, Vector
*c
, evalue
*EP
)
1426 unsigned dim
= c
->Size
-2;
1428 value_set_si(EP
->d
,0);
1429 EP
->x
.p
= new_enode(polynomial
, dim
+1, param
+1);
1430 for (int j
= 0; j
<= dim
; ++j
)
1431 evalue_set(&EP
->x
.p
->arr
[j
], c
->p
[j
], c
->p
[dim
+1]);
1434 static void multi_polynom(Vector
*c
, evalue
* X
, evalue
*EP
)
1436 unsigned dim
= c
->Size
-2;
1440 evalue_set(&EC
, c
->p
[dim
], c
->p
[dim
+1]);
1443 evalue_set(EP
, c
->p
[dim
], c
->p
[dim
+1]);
1445 for (int i
= dim
-1; i
>= 0; --i
) {
1447 value_assign(EC
.x
.n
, c
->p
[i
]);
1450 free_evalue_refs(&EC
);
1453 Polyhedron
*unfringe (Polyhedron
*P
, unsigned MaxRays
)
1455 int len
= P
->Dimension
+2;
1456 Polyhedron
*T
, *R
= P
;
1459 Vector
*row
= Vector_Alloc(len
);
1460 value_set_si(row
->p
[0], 1);
1462 R
= DomainConstraintSimplify(Polyhedron_Copy(P
), MaxRays
);
1464 Matrix
*M
= Matrix_Alloc(2, len
-1);
1465 value_set_si(M
->p
[1][len
-2], 1);
1466 for (int v
= 0; v
< P
->Dimension
; ++v
) {
1467 value_set_si(M
->p
[0][v
], 1);
1468 Polyhedron
*I
= Polyhedron_Image(P
, M
, 2+1);
1469 value_set_si(M
->p
[0][v
], 0);
1470 for (int r
= 0; r
< I
->NbConstraints
; ++r
) {
1471 if (value_zero_p(I
->Constraint
[r
][0]))
1473 if (value_zero_p(I
->Constraint
[r
][1]))
1475 if (value_one_p(I
->Constraint
[r
][1]))
1477 if (value_mone_p(I
->Constraint
[r
][1]))
1479 value_absolute(g
, I
->Constraint
[r
][1]);
1480 Vector_Set(row
->p
+1, 0, len
-2);
1481 value_division(row
->p
[1+v
], I
->Constraint
[r
][1], g
);
1482 mpz_fdiv_q(row
->p
[len
-1], I
->Constraint
[r
][2], g
);
1484 R
= AddConstraints(row
->p
, 1, R
, MaxRays
);
1496 static Polyhedron
*reduce_domain(Polyhedron
*D
, Matrix
*CT
, Polyhedron
*CEq
,
1497 Polyhedron
**fVD
, int nd
, unsigned MaxRays
)
1502 Dt
= CT
? DomainPreimage(D
, CT
, MaxRays
) : D
;
1503 Polyhedron
*rVD
= DomainIntersection(Dt
, CEq
, MaxRays
);
1505 /* if rVD is empty or too small in geometric dimension */
1506 if(!rVD
|| emptyQ(rVD
) ||
1507 (rVD
->Dimension
-rVD
->NbEq
< Dt
->Dimension
-Dt
->NbEq
-CEq
->NbEq
)) {
1512 return 0; /* empty validity domain */
1518 fVD
[nd
] = Domain_Copy(rVD
);
1519 for (int i
= 0 ; i
< nd
; ++i
) {
1520 Polyhedron
*I
= DomainIntersection(fVD
[nd
], fVD
[i
], MaxRays
);
1525 Polyhedron
*F
= DomainSimplify(I
, fVD
[nd
], MaxRays
);
1527 Polyhedron
*T
= rVD
;
1528 rVD
= DomainDifference(rVD
, F
, MaxRays
);
1535 rVD
= DomainConstraintSimplify(rVD
, MaxRays
);
1537 Domain_Free(fVD
[nd
]);
1544 barvinok_count(rVD
, &c
, MaxRays
);
1545 if (value_zero_p(c
)) {
1554 static bool Polyhedron_is_infinite(Polyhedron
*P
, unsigned nparam
)
1557 for (r
= 0; r
< P
->NbRays
; ++r
)
1558 if (value_zero_p(P
->Ray
[r
][0]) ||
1559 value_zero_p(P
->Ray
[r
][P
->Dimension
+1])) {
1561 for (i
= P
->Dimension
- nparam
; i
< P
->Dimension
; ++i
)
1562 if (value_notzero_p(P
->Ray
[r
][i
+1]))
1564 if (i
>= P
->Dimension
)
1567 return r
< P
->NbRays
;
1570 evalue
* barvinok_enumerate_ev(Polyhedron
*P
, Polyhedron
* C
, unsigned MaxRays
)
1572 //P = unfringe(P, MaxRays);
1573 Polyhedron
*CEq
= NULL
, *rVD
, *pVD
, *CA
;
1575 Param_Polyhedron
*PP
= NULL
;
1576 Param_Domain
*D
, *next
;
1579 unsigned nparam
= C
->Dimension
;
1581 ALLOC(evalue
, eres
);
1582 value_init(eres
->d
);
1583 value_set_si(eres
->d
, 0);
1586 value_init(factor
.d
);
1587 evalue_set_si(&factor
, 1, 1);
1589 CA
= align_context(C
, P
->Dimension
, MaxRays
);
1590 P
= DomainIntersection(P
, CA
, MaxRays
);
1591 Polyhedron_Free(CA
);
1593 if (C
->Dimension
== 0 || emptyQ(P
)) {
1595 eres
->x
.p
= new_enode(partition
, 2, C
->Dimension
);
1596 EVALUE_SET_DOMAIN(eres
->x
.p
->arr
[0],
1597 DomainConstraintSimplify(CEq
? CEq
: Polyhedron_Copy(C
), MaxRays
));
1598 value_set_si(eres
->x
.p
->arr
[1].d
, 1);
1599 value_init(eres
->x
.p
->arr
[1].x
.n
);
1601 value_set_si(eres
->x
.p
->arr
[1].x
.n
, 0);
1603 barvinok_count(P
, &eres
->x
.p
->arr
[1].x
.n
, MaxRays
);
1605 emul(&factor
, eres
);
1606 reduce_evalue(eres
);
1607 free_evalue_refs(&factor
);
1612 Param_Polyhedron_Free(PP
);
1616 if (Polyhedron_is_infinite(P
, nparam
))
1621 P
= remove_equalities_p(P
, P
->Dimension
-nparam
, &f
);
1625 if (P
->Dimension
== nparam
) {
1627 P
= Universe_Polyhedron(0);
1631 Polyhedron
*Q
= ParamPolyhedron_Reduce(P
, P
->Dimension
-nparam
, &factor
);
1634 if (Q
->Dimension
== nparam
) {
1636 P
= Universe_Polyhedron(0);
1641 Polyhedron
*oldP
= P
;
1642 PP
= Polyhedron2Param_SimplifiedDomain(&P
,C
,MaxRays
,&CEq
,&CT
);
1644 Polyhedron_Free(oldP
);
1646 if (isIdentity(CT
)) {
1650 assert(CT
->NbRows
!= CT
->NbColumns
);
1651 if (CT
->NbRows
== 1) // no more parameters
1653 nparam
= CT
->NbRows
- 1;
1656 unsigned dim
= P
->Dimension
- nparam
;
1657 Polyhedron
** vcone
= new Polyhedron_p
[PP
->nbV
];
1658 int * npos
= new int[PP
->nbV
];
1659 int * nneg
= new int[PP
->nbV
];
1663 for (i
= 0, V
= PP
->V
; V
; ++i
, V
= V
->next
) {
1664 Polyhedron
*C
= supporting_cone_p(P
, V
);
1665 decompose(C
, &vcone
[i
], &npos
[i
], &nneg
[i
], MaxRays
);
1668 Vector
*c
= Vector_Alloc(dim
+2);
1671 for (nd
= 0, D
=PP
->D
; D
; ++nd
, D
=D
->next
);
1672 struct section
{ Polyhedron
*D
; evalue E
; };
1673 section
*s
= new section
[nd
];
1674 Polyhedron
**fVD
= new Polyhedron_p
[nd
];
1676 for(nd
= 0, D
=PP
->D
; D
; D
=next
) {
1679 Polyhedron
*rVD
= reduce_domain(D
->Domain
, CT
, CEq
,
1684 pVD
= CT
? DomainImage(rVD
,CT
,MaxRays
) : rVD
;
1687 FORALL_PVertex_in_ParamPolyhedron(V
,D
,PP
) // _i is internal counter
1688 ncone
+= npos
[_i
] + nneg
[_i
];
1689 END_FORALL_PVertex_in_ParamPolyhedron
;
1692 rays
.SetDims(ncone
* dim
, dim
);
1694 FORALL_PVertex_in_ParamPolyhedron(V
,D
,PP
) // _i is internal counter
1695 for (Polyhedron
*i
= vcone
[_i
]; i
; i
= i
->next
) {
1696 assert(i
->NbRays
-1 == dim
);
1697 add_rays(rays
, i
, &r
);
1699 END_FORALL_PVertex_in_ParamPolyhedron
;
1701 nonorthog(rays
, lambda
);
1707 value_init(s
[nd
].E
.d
);
1708 evalue_set_si(&s
[nd
].E
, 0, 1);
1711 FORALL_PVertex_in_ParamPolyhedron(V
,D
,PP
)
1713 for (Polyhedron
*i
= vcone
[_i
]; i
; i
= i
->next
) {
1714 sign
= f
< npos
[_i
] ? 1 : -1;
1715 lattice_point(V
, i
, lambda
, &num
, pVD
);
1716 normalize(i
, lambda
, sign
, num
.constant
, den
);
1718 dpoly
n(dim
, den
[0], 1);
1719 for (int k
= 1; k
< dim
; ++k
) {
1720 dpoly
fact(dim
, den
[k
], 1);
1723 if (num
.E
!= NULL
) {
1724 ZZ
one(INIT_VAL
, 1);
1725 dpoly_n
d(dim
, num
.constant
, one
);
1728 multi_polynom(c
, num
.E
, &EV
);
1729 eadd(&EV
, &s
[nd
].E
);
1730 free_evalue_refs(&EV
);
1731 free_evalue_refs(num
.E
);
1733 } else if (num
.pos
!= -1) {
1734 dpoly_n
d(dim
, num
.constant
, num
.coeff
);
1737 uni_polynom(num
.pos
, c
, &EV
);
1738 eadd(&EV
, &s
[nd
].E
);
1739 free_evalue_refs(&EV
);
1741 mpq_set_si(count
, 0, 1);
1742 dpoly
d(dim
, num
.constant
);
1743 d
.div(n
, count
, sign
);
1746 evalue_set(&EV
, &count
[0]._mp_num
, &count
[0]._mp_den
);
1747 eadd(&EV
, &s
[nd
].E
);
1748 free_evalue_refs(&EV
);
1752 END_FORALL_PVertex_in_ParamPolyhedron
;
1757 addeliminatedparams_evalue(&s
[nd
].E
, CT
);
1765 evalue_set_si(eres
, 0, 1);
1767 eres
->x
.p
= new_enode(partition
, 2*nd
, C
->Dimension
);
1768 for (int j
= 0; j
< nd
; ++j
) {
1769 EVALUE_SET_DOMAIN(eres
->x
.p
->arr
[2*j
], s
[j
].D
);
1770 value_clear(eres
->x
.p
->arr
[2*j
+1].d
);
1771 eres
->x
.p
->arr
[2*j
+1] = s
[j
].E
;
1772 Domain_Free(fVD
[j
]);
1780 for (int j
= 0; j
< PP
->nbV
; ++j
)
1781 Domain_Free(vcone
[j
]);
1787 Polyhedron_Free(CEq
);
1792 Enumeration
* barvinok_enumerate(Polyhedron
*P
, Polyhedron
* C
, unsigned MaxRays
)
1794 evalue
*EP
= barvinok_enumerate_ev(P
, C
, MaxRays
);
1796 return partition2enumeration(EP
);
1799 static void SwapColumns(Value
**V
, int n
, int i
, int j
)
1801 for (int r
= 0; r
< n
; ++r
)
1802 value_swap(V
[r
][i
], V
[r
][j
]);
1805 static void SwapColumns(Polyhedron
*P
, int i
, int j
)
1807 SwapColumns(P
->Constraint
, P
->NbConstraints
, i
, j
);
1808 SwapColumns(P
->Ray
, P
->NbRays
, i
, j
);
1811 static void negative_test_constraint(Value
*l
, Value
*u
, Value
*c
, int pos
,
1814 value_oppose(*v
, u
[pos
+1]);
1815 Vector_Combine(l
+1, u
+1, c
+1, *v
, l
[pos
+1], len
-1);
1816 value_multiply(*v
, *v
, l
[pos
+1]);
1817 value_substract(c
[len
-1], c
[len
-1], *v
);
1818 value_set_si(*v
, -1);
1819 Vector_Scale(c
+1, c
+1, *v
, len
-1);
1820 value_decrement(c
[len
-1], c
[len
-1]);
1821 ConstraintSimplify(c
, c
, len
, v
);
1824 static void oppose_constraint(Value
*c
, int len
, Value
*v
)
1826 value_set_si(*v
, -1);
1827 Vector_Scale(c
+1, c
+1, *v
, len
-1);
1828 value_decrement(c
[len
-1], c
[len
-1]);
1831 static bool SplitOnConstraint(Polyhedron
*P
, int i
, int l
, int u
,
1832 int nvar
, int len
, int exist
, int MaxRays
,
1833 Vector
*row
, Value
& f
, bool independent
,
1834 Polyhedron
**pos
, Polyhedron
**neg
)
1836 negative_test_constraint(P
->Constraint
[l
], P
->Constraint
[u
],
1837 row
->p
, nvar
+i
, len
, &f
);
1838 *neg
= AddConstraints(row
->p
, 1, P
, MaxRays
);
1840 /* We found an independent, but useless constraint
1841 * Maybe we should detect this earlier and not
1842 * mark the variable as INDEPENDENT
1844 if (emptyQ((*neg
))) {
1845 Polyhedron_Free(*neg
);
1849 oppose_constraint(row
->p
, len
, &f
);
1850 *pos
= AddConstraints(row
->p
, 1, P
, MaxRays
);
1852 if (emptyQ((*pos
))) {
1853 Polyhedron_Free(*neg
);
1854 Polyhedron_Free(*pos
);
1862 * unimodularly transform P such that constraint r is transformed
1863 * into a constraint that involves only a single (the first)
1864 * existential variable
1867 static Polyhedron
*rotate_along(Polyhedron
*P
, int r
, int nvar
, int exist
,
1873 Vector
*row
= Vector_Alloc(exist
);
1874 Vector_Copy(P
->Constraint
[r
]+1+nvar
, row
->p
, exist
);
1875 Vector_Gcd(row
->p
, exist
, &g
);
1876 if (value_notone_p(g
))
1877 Vector_AntiScale(row
->p
, row
->p
, g
, exist
);
1880 Matrix
*M
= unimodular_complete(row
);
1881 Matrix
*M2
= Matrix_Alloc(P
->Dimension
+1, P
->Dimension
+1);
1882 for (r
= 0; r
< nvar
; ++r
)
1883 value_set_si(M2
->p
[r
][r
], 1);
1884 for ( ; r
< nvar
+exist
; ++r
)
1885 Vector_Copy(M
->p
[r
-nvar
], M2
->p
[r
]+nvar
, exist
);
1886 for ( ; r
< P
->Dimension
+1; ++r
)
1887 value_set_si(M2
->p
[r
][r
], 1);
1888 Polyhedron
*T
= Polyhedron_Image(P
, M2
, MaxRays
);
1897 static bool SplitOnVar(Polyhedron
*P
, int i
,
1898 int nvar
, int len
, int exist
, int MaxRays
,
1899 Vector
*row
, Value
& f
, bool independent
,
1900 Polyhedron
**pos
, Polyhedron
**neg
)
1904 for (int l
= P
->NbEq
; l
< P
->NbConstraints
; ++l
) {
1905 if (value_negz_p(P
->Constraint
[l
][nvar
+i
+1]))
1909 for (j
= 0; j
< exist
; ++j
)
1910 if (j
!= i
&& value_notzero_p(P
->Constraint
[l
][nvar
+j
+1]))
1916 for (int u
= P
->NbEq
; u
< P
->NbConstraints
; ++u
) {
1917 if (value_posz_p(P
->Constraint
[u
][nvar
+i
+1]))
1921 for (j
= 0; j
< exist
; ++j
)
1922 if (j
!= i
&& value_notzero_p(P
->Constraint
[u
][nvar
+j
+1]))
1928 if (SplitOnConstraint(P
, i
, l
, u
,
1929 nvar
, len
, exist
, MaxRays
,
1930 row
, f
, independent
,
1934 SwapColumns(*neg
, nvar
+1, nvar
+1+i
);
1944 static bool double_bound_pair(Polyhedron
*P
, int nvar
, int exist
,
1945 int i
, int l1
, int l2
,
1946 Polyhedron
**pos
, Polyhedron
**neg
)
1950 Vector
*row
= Vector_Alloc(P
->Dimension
+2);
1951 value_set_si(row
->p
[0], 1);
1952 value_oppose(f
, P
->Constraint
[l1
][nvar
+i
+1]);
1953 Vector_Combine(P
->Constraint
[l1
]+1, P
->Constraint
[l2
]+1,
1955 P
->Constraint
[l2
][nvar
+i
+1], f
,
1957 ConstraintSimplify(row
->p
, row
->p
, P
->Dimension
+2, &f
);
1958 *pos
= AddConstraints(row
->p
, 1, P
, 0);
1959 value_set_si(f
, -1);
1960 Vector_Scale(row
->p
+1, row
->p
+1, f
, P
->Dimension
+1);
1961 value_decrement(row
->p
[P
->Dimension
+1], row
->p
[P
->Dimension
+1]);
1962 *neg
= AddConstraints(row
->p
, 1, P
, 0);
1966 return !emptyQ((*pos
)) && !emptyQ((*neg
));
1969 static bool double_bound(Polyhedron
*P
, int nvar
, int exist
,
1970 Polyhedron
**pos
, Polyhedron
**neg
)
1972 for (int i
= 0; i
< exist
; ++i
) {
1974 for (l1
= P
->NbEq
; l1
< P
->NbConstraints
; ++l1
) {
1975 if (value_negz_p(P
->Constraint
[l1
][nvar
+i
+1]))
1977 for (l2
= l1
+ 1; l2
< P
->NbConstraints
; ++l2
) {
1978 if (value_negz_p(P
->Constraint
[l2
][nvar
+i
+1]))
1980 if (double_bound_pair(P
, nvar
, exist
, i
, l1
, l2
, pos
, neg
))
1984 for (l1
= P
->NbEq
; l1
< P
->NbConstraints
; ++l1
) {
1985 if (value_posz_p(P
->Constraint
[l1
][nvar
+i
+1]))
1987 if (l1
< P
->NbConstraints
)
1988 for (l2
= l1
+ 1; l2
< P
->NbConstraints
; ++l2
) {
1989 if (value_posz_p(P
->Constraint
[l2
][nvar
+i
+1]))
1991 if (double_bound_pair(P
, nvar
, exist
, i
, l1
, l2
, pos
, neg
))
2003 INDEPENDENT
= 1 << 2
2006 static evalue
* enumerate_or(Polyhedron
*D
,
2007 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
2010 fprintf(stderr
, "\nER: Or\n");
2011 #endif /* DEBUG_ER */
2013 Polyhedron
*N
= D
->next
;
2016 barvinok_enumerate_e(D
, exist
, nparam
, MaxRays
);
2019 for (D
= N
; D
; D
= N
) {
2024 barvinok_enumerate_e(D
, exist
, nparam
, MaxRays
);
2027 free_evalue_refs(EN
);
2037 static evalue
* enumerate_sum(Polyhedron
*P
,
2038 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
2040 int nvar
= P
->Dimension
- exist
- nparam
;
2041 int toswap
= nvar
< exist
? nvar
: exist
;
2042 for (int i
= 0; i
< toswap
; ++i
)
2043 SwapColumns(P
, 1 + i
, nvar
+exist
- i
);
2047 fprintf(stderr
, "\nER: Sum\n");
2048 #endif /* DEBUG_ER */
2050 evalue
*EP
= barvinok_enumerate_e(P
, exist
, nparam
, MaxRays
);
2052 for (int i
= 0; i
< /* nvar */ nparam
; ++i
) {
2053 Matrix
*C
= Matrix_Alloc(1, 1 + nparam
+ 1);
2054 value_set_si(C
->p
[0][0], 1);
2056 value_init(split
.d
);
2057 value_set_si(split
.d
, 0);
2058 split
.x
.p
= new_enode(partition
, 4, nparam
);
2059 value_set_si(C
->p
[0][1+i
], 1);
2060 Matrix
*C2
= Matrix_Copy(C
);
2061 EVALUE_SET_DOMAIN(split
.x
.p
->arr
[0],
2062 Constraints2Polyhedron(C2
, MaxRays
));
2064 evalue_set_si(&split
.x
.p
->arr
[1], 1, 1);
2065 value_set_si(C
->p
[0][1+i
], -1);
2066 value_set_si(C
->p
[0][1+nparam
], -1);
2067 EVALUE_SET_DOMAIN(split
.x
.p
->arr
[2],
2068 Constraints2Polyhedron(C
, MaxRays
));
2069 evalue_set_si(&split
.x
.p
->arr
[3], 1, 1);
2071 free_evalue_refs(&split
);
2075 evalue_range_reduction(EP
);
2077 evalue_frac2floor(EP
);
2079 evalue
*sum
= esum(EP
, nvar
);
2081 free_evalue_refs(EP
);
2085 evalue_range_reduction(EP
);
2090 static evalue
* split_sure(Polyhedron
*P
, Polyhedron
*S
,
2091 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
2093 int nvar
= P
->Dimension
- exist
- nparam
;
2095 Matrix
*M
= Matrix_Alloc(exist
, S
->Dimension
+2);
2096 for (int i
= 0; i
< exist
; ++i
)
2097 value_set_si(M
->p
[i
][nvar
+i
+1], 1);
2099 S
= DomainAddRays(S
, M
, MaxRays
);
2101 Polyhedron
*F
= DomainAddRays(P
, M
, MaxRays
);
2102 Polyhedron
*D
= DomainDifference(F
, S
, MaxRays
);
2104 D
= Disjoint_Domain(D
, 0, MaxRays
);
2109 M
= Matrix_Alloc(P
->Dimension
+1-exist
, P
->Dimension
+1);
2110 for (int j
= 0; j
< nvar
; ++j
)
2111 value_set_si(M
->p
[j
][j
], 1);
2112 for (int j
= 0; j
< nparam
+1; ++j
)
2113 value_set_si(M
->p
[nvar
+j
][nvar
+exist
+j
], 1);
2114 Polyhedron
*T
= Polyhedron_Image(S
, M
, MaxRays
);
2115 evalue
*EP
= barvinok_enumerate_e(T
, 0, nparam
, MaxRays
);
2120 for (Polyhedron
*Q
= D
; Q
; Q
= Q
->next
) {
2121 Polyhedron
*N
= Q
->next
;
2123 T
= DomainIntersection(P
, Q
, MaxRays
);
2124 evalue
*E
= barvinok_enumerate_e(T
, exist
, nparam
, MaxRays
);
2126 free_evalue_refs(E
);
2135 static evalue
* enumerate_sure(Polyhedron
*P
,
2136 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
2140 int nvar
= P
->Dimension
- exist
- nparam
;
2146 for (i
= 0; i
< exist
; ++i
) {
2147 Matrix
*M
= Matrix_Alloc(S
->NbConstraints
, S
->Dimension
+2);
2149 value_set_si(lcm
, 1);
2150 for (int j
= 0; j
< S
->NbConstraints
; ++j
) {
2151 if (value_negz_p(S
->Constraint
[j
][1+nvar
+i
]))
2153 if (value_one_p(S
->Constraint
[j
][1+nvar
+i
]))
2155 value_lcm(lcm
, S
->Constraint
[j
][1+nvar
+i
], &lcm
);
2158 for (int j
= 0; j
< S
->NbConstraints
; ++j
) {
2159 if (value_negz_p(S
->Constraint
[j
][1+nvar
+i
]))
2161 if (value_one_p(S
->Constraint
[j
][1+nvar
+i
]))
2163 value_division(f
, lcm
, S
->Constraint
[j
][1+nvar
+i
]);
2164 Vector_Scale(S
->Constraint
[j
], M
->p
[c
], f
, S
->Dimension
+2);
2165 value_substract(M
->p
[c
][S
->Dimension
+1],
2166 M
->p
[c
][S
->Dimension
+1],
2168 value_increment(M
->p
[c
][S
->Dimension
+1],
2169 M
->p
[c
][S
->Dimension
+1]);
2173 S
= AddConstraints(M
->p
[0], c
, S
, MaxRays
);
2188 fprintf(stderr
, "\nER: Sure\n");
2189 #endif /* DEBUG_ER */
2191 return split_sure(P
, S
, exist
, nparam
, MaxRays
);
2194 static evalue
* enumerate_sure2(Polyhedron
*P
,
2195 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
2197 int nvar
= P
->Dimension
- exist
- nparam
;
2199 for (r
= 0; r
< P
->NbRays
; ++r
)
2200 if (value_one_p(P
->Ray
[r
][0]) &&
2201 value_one_p(P
->Ray
[r
][P
->Dimension
+1]))
2207 Matrix
*M
= Matrix_Alloc(nvar
+ 1 + nparam
, P
->Dimension
+2);
2208 for (int i
= 0; i
< nvar
; ++i
)
2209 value_set_si(M
->p
[i
][1+i
], 1);
2210 for (int i
= 0; i
< nparam
; ++i
)
2211 value_set_si(M
->p
[i
+nvar
][1+nvar
+exist
+i
], 1);
2212 Vector_Copy(P
->Ray
[r
]+1+nvar
, M
->p
[nvar
+nparam
]+1+nvar
, exist
);
2213 value_set_si(M
->p
[nvar
+nparam
][0], 1);
2214 value_set_si(M
->p
[nvar
+nparam
][P
->Dimension
+1], 1);
2215 Polyhedron
* F
= Rays2Polyhedron(M
, MaxRays
);
2218 Polyhedron
*I
= DomainIntersection(F
, P
, MaxRays
);
2222 fprintf(stderr
, "\nER: Sure2\n");
2223 #endif /* DEBUG_ER */
2225 return split_sure(P
, I
, exist
, nparam
, MaxRays
);
2228 static evalue
* enumerate_cyclic(Polyhedron
*P
,
2229 unsigned exist
, unsigned nparam
,
2230 evalue
* EP
, int r
, int p
, unsigned MaxRays
)
2232 int nvar
= P
->Dimension
- exist
- nparam
;
2234 /* If EP in its fractional maps only contains references
2235 * to the remainder parameter with appropriate coefficients
2236 * then we could in principle avoid adding existentially
2237 * quantified variables to the validity domains.
2238 * We'd have to replace the remainder by m { p/m }
2239 * and multiply with an appropriate factor that is one
2240 * only in the appropriate range.
2241 * This last multiplication can be avoided if EP
2242 * has a single validity domain with no (further)
2243 * constraints on the remainder parameter
2246 Matrix
*CT
= Matrix_Alloc(nparam
+1, nparam
+3);
2247 Matrix
*M
= Matrix_Alloc(1, 1+nparam
+3);
2248 for (int j
= 0; j
< nparam
; ++j
)
2250 value_set_si(CT
->p
[j
][j
], 1);
2251 value_set_si(CT
->p
[p
][nparam
+1], 1);
2252 value_set_si(CT
->p
[nparam
][nparam
+2], 1);
2253 value_set_si(M
->p
[0][1+p
], -1);
2254 value_absolute(M
->p
[0][1+nparam
], P
->Ray
[0][1+nvar
+exist
+p
]);
2255 value_set_si(M
->p
[0][1+nparam
+1], 1);
2256 Polyhedron
*CEq
= Constraints2Polyhedron(M
, 1);
2258 addeliminatedparams_enum(EP
, CT
, CEq
, MaxRays
, nparam
);
2259 Polyhedron_Free(CEq
);
2265 static void enumerate_vd_add_ray(evalue
*EP
, Matrix
*Rays
, unsigned MaxRays
)
2267 if (value_notzero_p(EP
->d
))
2270 assert(EP
->x
.p
->type
== partition
);
2271 assert(EP
->x
.p
->pos
== EVALUE_DOMAIN(EP
->x
.p
->arr
[0])->Dimension
);
2272 for (int i
= 0; i
< EP
->x
.p
->size
/2; ++i
) {
2273 Polyhedron
*D
= EVALUE_DOMAIN(EP
->x
.p
->arr
[2*i
]);
2274 Polyhedron
*N
= DomainAddRays(D
, Rays
, MaxRays
);
2275 EVALUE_SET_DOMAIN(EP
->x
.p
->arr
[2*i
], N
);
2280 static evalue
* enumerate_line(Polyhedron
*P
,
2281 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
2287 fprintf(stderr
, "\nER: Line\n");
2288 #endif /* DEBUG_ER */
2290 int nvar
= P
->Dimension
- exist
- nparam
;
2292 for (i
= 0; i
< nparam
; ++i
)
2293 if (value_notzero_p(P
->Ray
[0][1+nvar
+exist
+i
]))
2296 for (j
= i
+1; j
< nparam
; ++j
)
2297 if (value_notzero_p(P
->Ray
[0][1+nvar
+exist
+i
]))
2299 assert(j
>= nparam
); // for now
2301 Matrix
*M
= Matrix_Alloc(2, P
->Dimension
+2);
2302 value_set_si(M
->p
[0][0], 1);
2303 value_set_si(M
->p
[0][1+nvar
+exist
+i
], 1);
2304 value_set_si(M
->p
[1][0], 1);
2305 value_set_si(M
->p
[1][1+nvar
+exist
+i
], -1);
2306 value_absolute(M
->p
[1][1+P
->Dimension
], P
->Ray
[0][1+nvar
+exist
+i
]);
2307 value_decrement(M
->p
[1][1+P
->Dimension
], M
->p
[1][1+P
->Dimension
]);
2308 Polyhedron
*S
= AddConstraints(M
->p
[0], 2, P
, MaxRays
);
2309 evalue
*EP
= barvinok_enumerate_e(S
, exist
, nparam
, MaxRays
);
2313 return enumerate_cyclic(P
, exist
, nparam
, EP
, 0, i
, MaxRays
);
2316 static int single_param_pos(Polyhedron
*P
, unsigned exist
, unsigned nparam
,
2319 int nvar
= P
->Dimension
- exist
- nparam
;
2320 if (First_Non_Zero(P
->Ray
[r
]+1, nvar
) != -1)
2322 int i
= First_Non_Zero(P
->Ray
[r
]+1+nvar
+exist
, nparam
);
2325 if (First_Non_Zero(P
->Ray
[r
]+1+nvar
+exist
+1, nparam
-i
-1) != -1)
2330 static evalue
* enumerate_remove_ray(Polyhedron
*P
, int r
,
2331 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
2334 fprintf(stderr
, "\nER: RedundantRay\n");
2335 #endif /* DEBUG_ER */
2339 value_set_si(one
, 1);
2340 int len
= P
->NbRays
-1;
2341 Matrix
*M
= Matrix_Alloc(2 * len
, P
->Dimension
+2);
2342 Vector_Copy(P
->Ray
[0], M
->p
[0], r
* (P
->Dimension
+2));
2343 Vector_Copy(P
->Ray
[r
+1], M
->p
[r
], (len
-r
) * (P
->Dimension
+2));
2344 for (int j
= 0; j
< P
->NbRays
; ++j
) {
2347 Vector_Combine(P
->Ray
[j
], P
->Ray
[r
], M
->p
[len
+j
-(j
>r
)],
2348 one
, P
->Ray
[j
][P
->Dimension
+1], P
->Dimension
+2);
2351 P
= Rays2Polyhedron(M
, MaxRays
);
2353 evalue
*EP
= barvinok_enumerate_e(P
, exist
, nparam
, MaxRays
);
2360 static evalue
* enumerate_redundant_ray(Polyhedron
*P
,
2361 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
2363 assert(P
->NbBid
== 0);
2364 int nvar
= P
->Dimension
- exist
- nparam
;
2368 for (int r
= 0; r
< P
->NbRays
; ++r
) {
2369 if (value_notzero_p(P
->Ray
[r
][P
->Dimension
+1]))
2371 int i1
= single_param_pos(P
, exist
, nparam
, r
);
2374 for (int r2
= r
+1; r2
< P
->NbRays
; ++r2
) {
2375 if (value_notzero_p(P
->Ray
[r2
][P
->Dimension
+1]))
2377 int i2
= single_param_pos(P
, exist
, nparam
, r2
);
2383 value_division(m
, P
->Ray
[r
][1+nvar
+exist
+i1
],
2384 P
->Ray
[r2
][1+nvar
+exist
+i1
]);
2385 value_multiply(m
, m
, P
->Ray
[r2
][1+nvar
+exist
+i1
]);
2386 /* r2 divides r => r redundant */
2387 if (value_eq(m
, P
->Ray
[r
][1+nvar
+exist
+i1
])) {
2389 return enumerate_remove_ray(P
, r
, exist
, nparam
, MaxRays
);
2392 value_division(m
, P
->Ray
[r2
][1+nvar
+exist
+i1
],
2393 P
->Ray
[r
][1+nvar
+exist
+i1
]);
2394 value_multiply(m
, m
, P
->Ray
[r
][1+nvar
+exist
+i1
]);
2395 /* r divides r2 => r2 redundant */
2396 if (value_eq(m
, P
->Ray
[r2
][1+nvar
+exist
+i1
])) {
2398 return enumerate_remove_ray(P
, r2
, exist
, nparam
, MaxRays
);
2406 static Polyhedron
*upper_bound(Polyhedron
*P
,
2407 int pos
, Value
*max
, Polyhedron
**R
)
2416 for (Polyhedron
*Q
= P
; Q
; Q
= N
) {
2418 for (r
= 0; r
< P
->NbRays
; ++r
) {
2419 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1]) &&
2420 value_pos_p(P
->Ray
[r
][1+pos
]))
2423 if (r
< P
->NbRays
) {
2431 for (r
= 0; r
< P
->NbRays
; ++r
) {
2432 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1]))
2434 mpz_fdiv_q(v
, P
->Ray
[r
][1+pos
], P
->Ray
[r
][1+P
->Dimension
]);
2435 if ((!Q
->next
&& r
== 0) || value_gt(v
, *max
))
2436 value_assign(*max
, v
);
2443 static evalue
* enumerate_ray(Polyhedron
*P
,
2444 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
2446 assert(P
->NbBid
== 0);
2447 int nvar
= P
->Dimension
- exist
- nparam
;
2450 for (r
= 0; r
< P
->NbRays
; ++r
)
2451 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1]))
2457 for (r2
= r
+1; r2
< P
->NbRays
; ++r2
)
2458 if (value_zero_p(P
->Ray
[r2
][P
->Dimension
+1]))
2460 if (r2
< P
->NbRays
) {
2462 return enumerate_sum(P
, exist
, nparam
, MaxRays
);
2466 fprintf(stderr
, "\nER: Ray\n");
2467 #endif /* DEBUG_ER */
2473 value_set_si(one
, 1);
2474 int i
= single_param_pos(P
, exist
, nparam
, r
);
2475 assert(i
!= -1); // for now;
2477 Matrix
*M
= Matrix_Alloc(P
->NbRays
, P
->Dimension
+2);
2478 for (int j
= 0; j
< P
->NbRays
; ++j
) {
2479 Vector_Combine(P
->Ray
[j
], P
->Ray
[r
], M
->p
[j
],
2480 one
, P
->Ray
[j
][P
->Dimension
+1], P
->Dimension
+2);
2482 Polyhedron
*S
= Rays2Polyhedron(M
, MaxRays
);
2484 Polyhedron
*D
= DomainDifference(P
, S
, MaxRays
);
2486 // Polyhedron_Print(stderr, P_VALUE_FMT, D);
2487 assert(value_pos_p(P
->Ray
[r
][1+nvar
+exist
+i
])); // for now
2489 D
= upper_bound(D
, nvar
+exist
+i
, &m
, &R
);
2493 M
= Matrix_Alloc(2, P
->Dimension
+2);
2494 value_set_si(M
->p
[0][0], 1);
2495 value_set_si(M
->p
[1][0], 1);
2496 value_set_si(M
->p
[0][1+nvar
+exist
+i
], -1);
2497 value_set_si(M
->p
[1][1+nvar
+exist
+i
], 1);
2498 value_assign(M
->p
[0][1+P
->Dimension
], m
);
2499 value_oppose(M
->p
[1][1+P
->Dimension
], m
);
2500 value_addto(M
->p
[1][1+P
->Dimension
], M
->p
[1][1+P
->Dimension
],
2501 P
->Ray
[r
][1+nvar
+exist
+i
]);
2502 value_decrement(M
->p
[1][1+P
->Dimension
], M
->p
[1][1+P
->Dimension
]);
2503 // Matrix_Print(stderr, P_VALUE_FMT, M);
2504 D
= AddConstraints(M
->p
[0], 2, P
, MaxRays
);
2505 // Polyhedron_Print(stderr, P_VALUE_FMT, D);
2506 value_substract(M
->p
[0][1+P
->Dimension
], M
->p
[0][1+P
->Dimension
],
2507 P
->Ray
[r
][1+nvar
+exist
+i
]);
2508 // Matrix_Print(stderr, P_VALUE_FMT, M);
2509 S
= AddConstraints(M
->p
[0], 1, P
, MaxRays
);
2510 // Polyhedron_Print(stderr, P_VALUE_FMT, S);
2513 evalue
*EP
= barvinok_enumerate_e(D
, exist
, nparam
, MaxRays
);
2518 if (value_notone_p(P
->Ray
[r
][1+nvar
+exist
+i
]))
2519 EP
= enumerate_cyclic(P
, exist
, nparam
, EP
, r
, i
, MaxRays
);
2521 M
= Matrix_Alloc(1, nparam
+2);
2522 value_set_si(M
->p
[0][0], 1);
2523 value_set_si(M
->p
[0][1+i
], 1);
2524 enumerate_vd_add_ray(EP
, M
, MaxRays
);
2529 evalue
*E
= barvinok_enumerate_e(S
, exist
, nparam
, MaxRays
);
2531 free_evalue_refs(E
);
2538 evalue
*ER
= enumerate_or(R
, exist
, nparam
, MaxRays
);
2540 free_evalue_refs(ER
);
2547 static evalue
* new_zero_ep()
2552 evalue_set_si(EP
, 0, 1);
2556 static evalue
* enumerate_vd(Polyhedron
**PA
,
2557 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
2559 Polyhedron
*P
= *PA
;
2560 int nvar
= P
->Dimension
- exist
- nparam
;
2561 Param_Polyhedron
*PP
= NULL
;
2562 Polyhedron
*C
= Universe_Polyhedron(nparam
);
2566 PP
= Polyhedron2Param_SimplifiedDomain(&PR
,C
,MaxRays
,&CEq
,&CT
);
2570 Param_Domain
*D
, *last
;
2573 for (nd
= 0, D
=PP
->D
; D
; D
=D
->next
, ++nd
)
2576 Polyhedron
**VD
= new Polyhedron_p
[nd
];
2577 Polyhedron
**fVD
= new Polyhedron_p
[nd
];
2578 for(nd
= 0, D
=PP
->D
; D
; D
=D
->next
) {
2579 Polyhedron
*rVD
= reduce_domain(D
->Domain
, CT
, CEq
,
2593 /* This doesn't seem to have any effect */
2595 Polyhedron
*CA
= align_context(VD
[0], P
->Dimension
, MaxRays
);
2597 P
= DomainIntersection(P
, CA
, MaxRays
);
2600 Polyhedron_Free(CA
);
2605 if (!EP
&& CT
->NbColumns
!= CT
->NbRows
) {
2606 Polyhedron
*CEqr
= DomainImage(CEq
, CT
, MaxRays
);
2607 Polyhedron
*CA
= align_context(CEqr
, PR
->Dimension
, MaxRays
);
2608 Polyhedron
*I
= DomainIntersection(PR
, CA
, MaxRays
);
2609 Polyhedron_Free(CEqr
);
2610 Polyhedron_Free(CA
);
2612 fprintf(stderr
, "\nER: Eliminate\n");
2613 #endif /* DEBUG_ER */
2614 nparam
-= CT
->NbColumns
- CT
->NbRows
;
2615 EP
= barvinok_enumerate_e(I
, exist
, nparam
, MaxRays
);
2616 nparam
+= CT
->NbColumns
- CT
->NbRows
;
2617 addeliminatedparams_enum(EP
, CT
, CEq
, MaxRays
, nparam
);
2621 Polyhedron_Free(PR
);
2624 if (!EP
&& nd
> 1) {
2626 fprintf(stderr
, "\nER: VD\n");
2627 #endif /* DEBUG_ER */
2628 for (int i
= 0; i
< nd
; ++i
) {
2629 Polyhedron
*CA
= align_context(VD
[i
], P
->Dimension
, MaxRays
);
2630 Polyhedron
*I
= DomainIntersection(P
, CA
, MaxRays
);
2633 EP
= barvinok_enumerate_e(I
, exist
, nparam
, MaxRays
);
2635 evalue
*E
= barvinok_enumerate_e(I
, exist
, nparam
, MaxRays
);
2637 free_evalue_refs(E
);
2641 Polyhedron_Free(CA
);
2645 for (int i
= 0; i
< nd
; ++i
) {
2646 Polyhedron_Free(VD
[i
]);
2647 Polyhedron_Free(fVD
[i
]);
2653 if (!EP
&& nvar
== 0) {
2656 Param_Vertices
*V
, *V2
;
2657 Matrix
* M
= Matrix_Alloc(1, P
->Dimension
+2);
2659 FORALL_PVertex_in_ParamPolyhedron(V
, last
, PP
) {
2661 FORALL_PVertex_in_ParamPolyhedron(V2
, last
, PP
) {
2668 for (int i
= 0; i
< exist
; ++i
) {
2669 value_oppose(f
, V
->Vertex
->p
[i
][nparam
+1]);
2670 Vector_Combine(V
->Vertex
->p
[i
],
2672 M
->p
[0] + 1 + nvar
+ exist
,
2673 V2
->Vertex
->p
[i
][nparam
+1],
2677 for (j
= 0; j
< nparam
; ++j
)
2678 if (value_notzero_p(M
->p
[0][1+nvar
+exist
+j
]))
2682 ConstraintSimplify(M
->p
[0], M
->p
[0],
2683 P
->Dimension
+2, &f
);
2684 value_set_si(M
->p
[0][0], 0);
2685 Polyhedron
*para
= AddConstraints(M
->p
[0], 1, P
,
2688 Polyhedron_Free(para
);
2691 Polyhedron
*pos
, *neg
;
2692 value_set_si(M
->p
[0][0], 1);
2693 value_decrement(M
->p
[0][P
->Dimension
+1],
2694 M
->p
[0][P
->Dimension
+1]);
2695 neg
= AddConstraints(M
->p
[0], 1, P
, MaxRays
);
2696 value_set_si(f
, -1);
2697 Vector_Scale(M
->p
[0]+1, M
->p
[0]+1, f
,
2699 value_decrement(M
->p
[0][P
->Dimension
+1],
2700 M
->p
[0][P
->Dimension
+1]);
2701 value_decrement(M
->p
[0][P
->Dimension
+1],
2702 M
->p
[0][P
->Dimension
+1]);
2703 pos
= AddConstraints(M
->p
[0], 1, P
, MaxRays
);
2704 if (emptyQ(neg
) && emptyQ(pos
)) {
2705 Polyhedron_Free(para
);
2706 Polyhedron_Free(pos
);
2707 Polyhedron_Free(neg
);
2711 fprintf(stderr
, "\nER: Order\n");
2712 #endif /* DEBUG_ER */
2713 EP
= barvinok_enumerate_e(para
, exist
, nparam
, MaxRays
);
2716 E
= barvinok_enumerate_e(pos
, exist
, nparam
, MaxRays
);
2718 free_evalue_refs(E
);
2722 E
= barvinok_enumerate_e(neg
, exist
, nparam
, MaxRays
);
2724 free_evalue_refs(E
);
2727 Polyhedron_Free(para
);
2728 Polyhedron_Free(pos
);
2729 Polyhedron_Free(neg
);
2734 } END_FORALL_PVertex_in_ParamPolyhedron
;
2737 } END_FORALL_PVertex_in_ParamPolyhedron
;
2740 /* Search for vertex coordinate to split on */
2741 /* First look for one independent of the parameters */
2742 FORALL_PVertex_in_ParamPolyhedron(V
, last
, PP
) {
2743 for (int i
= 0; i
< exist
; ++i
) {
2745 for (j
= 0; j
< nparam
; ++j
)
2746 if (value_notzero_p(V
->Vertex
->p
[i
][j
]))
2750 value_set_si(M
->p
[0][0], 1);
2751 Vector_Set(M
->p
[0]+1, 0, nvar
+exist
);
2752 Vector_Copy(V
->Vertex
->p
[i
],
2753 M
->p
[0] + 1 + nvar
+ exist
, nparam
+1);
2754 value_oppose(M
->p
[0][1+nvar
+i
],
2755 V
->Vertex
->p
[i
][nparam
+1]);
2757 Polyhedron
*pos
, *neg
;
2758 value_set_si(M
->p
[0][0], 1);
2759 value_decrement(M
->p
[0][P
->Dimension
+1],
2760 M
->p
[0][P
->Dimension
+1]);
2761 neg
= AddConstraints(M
->p
[0], 1, P
, MaxRays
);
2762 value_set_si(f
, -1);
2763 Vector_Scale(M
->p
[0]+1, M
->p
[0]+1, f
,
2765 value_decrement(M
->p
[0][P
->Dimension
+1],
2766 M
->p
[0][P
->Dimension
+1]);
2767 value_decrement(M
->p
[0][P
->Dimension
+1],
2768 M
->p
[0][P
->Dimension
+1]);
2769 pos
= AddConstraints(M
->p
[0], 1, P
, MaxRays
);
2770 if (emptyQ(neg
) || emptyQ(pos
)) {
2771 Polyhedron_Free(pos
);
2772 Polyhedron_Free(neg
);
2775 Polyhedron_Free(pos
);
2776 value_increment(M
->p
[0][P
->Dimension
+1],
2777 M
->p
[0][P
->Dimension
+1]);
2778 pos
= AddConstraints(M
->p
[0], 1, P
, MaxRays
);
2780 fprintf(stderr
, "\nER: Vertex\n");
2781 #endif /* DEBUG_ER */
2783 EP
= enumerate_or(pos
, exist
, nparam
, MaxRays
);
2788 } END_FORALL_PVertex_in_ParamPolyhedron
;
2792 /* Search for vertex coordinate to split on */
2793 /* Now look for one that depends on the parameters */
2794 FORALL_PVertex_in_ParamPolyhedron(V
, last
, PP
) {
2795 for (int i
= 0; i
< exist
; ++i
) {
2796 value_set_si(M
->p
[0][0], 1);
2797 Vector_Set(M
->p
[0]+1, 0, nvar
+exist
);
2798 Vector_Copy(V
->Vertex
->p
[i
],
2799 M
->p
[0] + 1 + nvar
+ exist
, nparam
+1);
2800 value_oppose(M
->p
[0][1+nvar
+i
],
2801 V
->Vertex
->p
[i
][nparam
+1]);
2803 Polyhedron
*pos
, *neg
;
2804 value_set_si(M
->p
[0][0], 1);
2805 value_decrement(M
->p
[0][P
->Dimension
+1],
2806 M
->p
[0][P
->Dimension
+1]);
2807 neg
= AddConstraints(M
->p
[0], 1, P
, MaxRays
);
2808 value_set_si(f
, -1);
2809 Vector_Scale(M
->p
[0]+1, M
->p
[0]+1, f
,
2811 value_decrement(M
->p
[0][P
->Dimension
+1],
2812 M
->p
[0][P
->Dimension
+1]);
2813 value_decrement(M
->p
[0][P
->Dimension
+1],
2814 M
->p
[0][P
->Dimension
+1]);
2815 pos
= AddConstraints(M
->p
[0], 1, P
, MaxRays
);
2816 if (emptyQ(neg
) || emptyQ(pos
)) {
2817 Polyhedron_Free(pos
);
2818 Polyhedron_Free(neg
);
2821 Polyhedron_Free(pos
);
2822 value_increment(M
->p
[0][P
->Dimension
+1],
2823 M
->p
[0][P
->Dimension
+1]);
2824 pos
= AddConstraints(M
->p
[0], 1, P
, MaxRays
);
2826 fprintf(stderr
, "\nER: ParamVertex\n");
2827 #endif /* DEBUG_ER */
2829 EP
= enumerate_or(pos
, exist
, nparam
, MaxRays
);
2834 } END_FORALL_PVertex_in_ParamPolyhedron
;
2842 Polyhedron_Free(CEq
);
2846 Param_Polyhedron_Free(PP
);
2853 evalue
*barvinok_enumerate_pip(Polyhedron
*P
,
2854 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
2859 evalue
*barvinok_enumerate_pip(Polyhedron
*P
,
2860 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
2862 int nvar
= P
->Dimension
- exist
- nparam
;
2863 evalue
*EP
= new_zero_ep();
2864 Polyhedron
*Q
, *N
, *T
= 0;
2870 fprintf(stderr
, "\nER: PIP\n");
2871 #endif /* DEBUG_ER */
2873 for (int i
= 0; i
< P
->Dimension
; ++i
) {
2876 bool posray
= false;
2877 bool negray
= false;
2878 value_set_si(min
, 0);
2879 for (int j
= 0; j
< P
->NbRays
; ++j
) {
2880 if (value_pos_p(P
->Ray
[j
][1+i
])) {
2882 if (value_zero_p(P
->Ray
[j
][1+P
->Dimension
]))
2884 } else if (value_neg_p(P
->Ray
[j
][1+i
])) {
2886 if (value_zero_p(P
->Ray
[j
][1+P
->Dimension
]))
2890 P
->Ray
[j
][1+i
], P
->Ray
[j
][1+P
->Dimension
]);
2891 if (value_lt(tmp
, min
))
2892 value_assign(min
, tmp
);
2897 assert(!(posray
&& negray
));
2898 assert(!negray
); // for now
2899 Polyhedron
*O
= T
? T
: P
;
2900 /* shift by a safe amount */
2901 Matrix
*M
= Matrix_Alloc(O
->NbRays
, O
->Dimension
+2);
2902 Vector_Copy(O
->Ray
[0], M
->p
[0], O
->NbRays
* (O
->Dimension
+2));
2903 for (int j
= 0; j
< P
->NbRays
; ++j
) {
2904 if (value_notzero_p(M
->p
[j
][1+P
->Dimension
])) {
2905 value_multiply(tmp
, min
, M
->p
[j
][1+P
->Dimension
]);
2906 value_substract(M
->p
[j
][1+i
], M
->p
[j
][1+i
], tmp
);
2911 T
= Rays2Polyhedron(M
, MaxRays
);
2914 /* negating a parameter requires that we substitute in the
2915 * sign again afterwards.
2918 assert(i
< nvar
+exist
);
2920 T
= Polyhedron_Copy(P
);
2921 for (int j
= 0; j
< T
->NbRays
; ++j
)
2922 value_oppose(T
->Ray
[j
][1+i
], T
->Ray
[j
][1+i
]);
2923 for (int j
= 0; j
< T
->NbConstraints
; ++j
)
2924 value_oppose(T
->Constraint
[j
][1+i
], T
->Constraint
[j
][1+i
]);
2930 Polyhedron
*D
= pip_lexmin(T
? T
: P
, exist
, nparam
);
2931 for (Q
= D
; Q
; Q
= N
) {
2935 exist
= Q
->Dimension
- nvar
- nparam
;
2936 E
= barvinok_enumerate_e(Q
, exist
, nparam
, MaxRays
);
2939 free_evalue_refs(E
);
2951 static bool is_single(Value
*row
, int pos
, int len
)
2953 return First_Non_Zero(row
, pos
) == -1 &&
2954 First_Non_Zero(row
+pos
+1, len
-pos
-1) == -1;
2957 static evalue
* barvinok_enumerate_e_r(Polyhedron
*P
,
2958 unsigned exist
, unsigned nparam
, unsigned MaxRays
);
2961 static int er_level
= 0;
2963 evalue
* barvinok_enumerate_e(Polyhedron
*P
,
2964 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
2966 fprintf(stderr
, "\nER: level %i\n", er_level
);
2967 int nvar
= P
->Dimension
- exist
- nparam
;
2968 fprintf(stderr
, "%d %d %d\n", nvar
, exist
, nparam
);
2970 Polyhedron_Print(stderr
, P_VALUE_FMT
, P
);
2972 P
= DomainConstraintSimplify(Polyhedron_Copy(P
), MaxRays
);
2973 evalue
*EP
= barvinok_enumerate_e_r(P
, exist
, nparam
, MaxRays
);
2979 evalue
* barvinok_enumerate_e(Polyhedron
*P
,
2980 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
2982 P
= DomainConstraintSimplify(Polyhedron_Copy(P
), MaxRays
);
2983 evalue
*EP
= barvinok_enumerate_e_r(P
, exist
, nparam
, MaxRays
);
2989 static evalue
* barvinok_enumerate_e_r(Polyhedron
*P
,
2990 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
2993 Polyhedron
*U
= Universe_Polyhedron(nparam
);
2994 evalue
*EP
= barvinok_enumerate_ev(P
, U
, MaxRays
);
2995 //char *param_name[] = {"P", "Q", "R", "S", "T" };
2996 //print_evalue(stdout, EP, param_name);
3001 int nvar
= P
->Dimension
- exist
- nparam
;
3002 int len
= P
->Dimension
+ 2;
3005 return new_zero_ep();
3007 if (nvar
== 0 && nparam
== 0) {
3008 evalue
*EP
= new_zero_ep();
3009 barvinok_count(P
, &EP
->x
.n
, MaxRays
);
3010 if (value_pos_p(EP
->x
.n
))
3011 value_set_si(EP
->x
.n
, 1);
3016 for (r
= 0; r
< P
->NbRays
; ++r
)
3017 if (value_zero_p(P
->Ray
[r
][0]) ||
3018 value_zero_p(P
->Ray
[r
][P
->Dimension
+1])) {
3020 for (i
= 0; i
< nvar
; ++i
)
3021 if (value_notzero_p(P
->Ray
[r
][i
+1]))
3025 for (i
= nvar
+ exist
; i
< nvar
+ exist
+ nparam
; ++i
)
3026 if (value_notzero_p(P
->Ray
[r
][i
+1]))
3028 if (i
>= nvar
+ exist
+ nparam
)
3031 if (r
< P
->NbRays
) {
3032 evalue
*EP
= new_zero_ep();
3033 value_set_si(EP
->x
.n
, -1);
3038 for (r
= 0; r
< P
->NbEq
; ++r
)
3039 if ((first
= First_Non_Zero(P
->Constraint
[r
]+1+nvar
, exist
)) != -1)
3042 if (First_Non_Zero(P
->Constraint
[r
]+1+nvar
+first
+1,
3043 exist
-first
-1) != -1) {
3044 Polyhedron
*T
= rotate_along(P
, r
, nvar
, exist
, MaxRays
);
3046 fprintf(stderr
, "\nER: Equality\n");
3047 #endif /* DEBUG_ER */
3048 evalue
*EP
= barvinok_enumerate_e(T
, exist
-1, nparam
, MaxRays
);
3053 fprintf(stderr
, "\nER: Fixed\n");
3054 #endif /* DEBUG_ER */
3056 return barvinok_enumerate_e(P
, exist
-1, nparam
, MaxRays
);
3058 Polyhedron
*T
= Polyhedron_Copy(P
);
3059 SwapColumns(T
, nvar
+1, nvar
+1+first
);
3060 evalue
*EP
= barvinok_enumerate_e(T
, exist
-1, nparam
, MaxRays
);
3067 Vector
*row
= Vector_Alloc(len
);
3068 value_set_si(row
->p
[0], 1);
3073 enum constraint
* info
= new constraint
[exist
];
3074 for (int i
= 0; i
< exist
; ++i
) {
3076 for (int l
= P
->NbEq
; l
< P
->NbConstraints
; ++l
) {
3077 if (value_negz_p(P
->Constraint
[l
][nvar
+i
+1]))
3079 bool l_parallel
= is_single(P
->Constraint
[l
]+nvar
+1, i
, exist
);
3080 for (int u
= P
->NbEq
; u
< P
->NbConstraints
; ++u
) {
3081 if (value_posz_p(P
->Constraint
[u
][nvar
+i
+1]))
3083 bool lu_parallel
= l_parallel
||
3084 is_single(P
->Constraint
[u
]+nvar
+1, i
, exist
);
3085 value_oppose(f
, P
->Constraint
[u
][nvar
+i
+1]);
3086 Vector_Combine(P
->Constraint
[l
]+1, P
->Constraint
[u
]+1, row
->p
+1,
3087 f
, P
->Constraint
[l
][nvar
+i
+1], len
-1);
3088 if (!(info
[i
] & INDEPENDENT
)) {
3090 for (j
= 0; j
< exist
; ++j
)
3091 if (j
!= i
&& value_notzero_p(row
->p
[nvar
+j
+1]))
3094 //printf("independent: i: %d, l: %d, u: %d\n", i, l, u);
3095 info
[i
] = (constraint
)(info
[i
] | INDEPENDENT
);
3098 if (info
[i
] & ALL_POS
) {
3099 value_addto(row
->p
[len
-1], row
->p
[len
-1],
3100 P
->Constraint
[l
][nvar
+i
+1]);
3101 value_addto(row
->p
[len
-1], row
->p
[len
-1], f
);
3102 value_multiply(f
, f
, P
->Constraint
[l
][nvar
+i
+1]);
3103 value_substract(row
->p
[len
-1], row
->p
[len
-1], f
);
3104 value_decrement(row
->p
[len
-1], row
->p
[len
-1]);
3105 ConstraintSimplify(row
->p
, row
->p
, len
, &f
);
3106 value_set_si(f
, -1);
3107 Vector_Scale(row
->p
+1, row
->p
+1, f
, len
-1);
3108 value_decrement(row
->p
[len
-1], row
->p
[len
-1]);
3109 Polyhedron
*T
= AddConstraints(row
->p
, 1, P
, MaxRays
);
3111 //printf("not all_pos: i: %d, l: %d, u: %d\n", i, l, u);
3112 info
[i
] = (constraint
)(info
[i
] ^ ALL_POS
);
3114 //puts("pos remainder");
3115 //Polyhedron_Print(stdout, P_VALUE_FMT, T);
3118 if (!(info
[i
] & ONE_NEG
)) {
3120 negative_test_constraint(P
->Constraint
[l
],
3122 row
->p
, nvar
+i
, len
, &f
);
3123 oppose_constraint(row
->p
, len
, &f
);
3124 Polyhedron
*T
= AddConstraints(row
->p
, 1, P
, MaxRays
);
3126 //printf("one_neg i: %d, l: %d, u: %d\n", i, l, u);
3127 info
[i
] = (constraint
)(info
[i
] | ONE_NEG
);
3129 //puts("neg remainder");
3130 //Polyhedron_Print(stdout, P_VALUE_FMT, T);
3134 if (!(info
[i
] & ALL_POS
) && (info
[i
] & ONE_NEG
))
3138 if (info
[i
] & ALL_POS
)
3145 for (int i = 0; i < exist; ++i)
3146 printf("%i: %i\n", i, info[i]);
3148 for (int i
= 0; i
< exist
; ++i
)
3149 if (info
[i
] & ALL_POS
) {
3151 fprintf(stderr
, "\nER: Positive\n");
3152 #endif /* DEBUG_ER */
3154 // Maybe we should chew off some of the fat here
3155 Matrix
*M
= Matrix_Alloc(P
->Dimension
, P
->Dimension
+1);
3156 for (int j
= 0; j
< P
->Dimension
; ++j
)
3157 value_set_si(M
->p
[j
][j
+ (j
>= i
+nvar
)], 1);
3158 Polyhedron
*T
= Polyhedron_Image(P
, M
, MaxRays
);
3160 evalue
*EP
= barvinok_enumerate_e(T
, exist
-1, nparam
, MaxRays
);
3167 for (int i
= 0; i
< exist
; ++i
)
3168 if (info
[i
] & ONE_NEG
) {
3170 fprintf(stderr
, "\nER: Negative\n");
3171 #endif /* DEBUG_ER */
3176 return barvinok_enumerate_e(P
, exist
-1, nparam
, MaxRays
);
3178 Polyhedron
*T
= Polyhedron_Copy(P
);
3179 SwapColumns(T
, nvar
+1, nvar
+1+i
);
3180 evalue
*EP
= barvinok_enumerate_e(T
, exist
-1, nparam
, MaxRays
);
3185 for (int i
= 0; i
< exist
; ++i
)
3186 if (info
[i
] & INDEPENDENT
) {
3187 Polyhedron
*pos
, *neg
;
3189 /* Find constraint again and split off negative part */
3191 if (SplitOnVar(P
, i
, nvar
, len
, exist
, MaxRays
,
3192 row
, f
, true, &pos
, &neg
)) {
3194 fprintf(stderr
, "\nER: Split\n");
3195 #endif /* DEBUG_ER */
3198 barvinok_enumerate_e(neg
, exist
-1, nparam
, MaxRays
);
3200 barvinok_enumerate_e(pos
, exist
, nparam
, MaxRays
);
3202 free_evalue_refs(E
);
3204 Polyhedron_Free(neg
);
3205 Polyhedron_Free(pos
);
3219 EP
= enumerate_line(P
, exist
, nparam
, MaxRays
);
3223 EP
= barvinok_enumerate_pip(P
, exist
, nparam
, MaxRays
);
3227 EP
= enumerate_redundant_ray(P
, exist
, nparam
, MaxRays
);
3231 EP
= enumerate_sure(P
, exist
, nparam
, MaxRays
);
3235 EP
= enumerate_ray(P
, exist
, nparam
, MaxRays
);
3239 EP
= enumerate_sure2(P
, exist
, nparam
, MaxRays
);
3243 F
= unfringe(P
, MaxRays
);
3244 if (!PolyhedronIncludes(F
, P
)) {
3246 fprintf(stderr
, "\nER: Fringed\n");
3247 #endif /* DEBUG_ER */
3248 EP
= barvinok_enumerate_e(F
, exist
, nparam
, MaxRays
);
3255 EP
= enumerate_vd(&P
, exist
, nparam
, MaxRays
);
3260 EP
= enumerate_sum(P
, exist
, nparam
, MaxRays
);
3267 Polyhedron
*pos
, *neg
;
3268 for (i
= 0; i
< exist
; ++i
)
3269 if (SplitOnVar(P
, i
, nvar
, len
, exist
, MaxRays
,
3270 row
, f
, false, &pos
, &neg
))
3276 EP
= enumerate_or(pos
, exist
, nparam
, MaxRays
);
3288 static void normalize(Polyhedron
*i
, vec_ZZ
& lambda
, ZZ
& sign
,
3289 ZZ
& num_s
, vec_ZZ
& num_p
, vec_ZZ
& den_s
, vec_ZZ
& den_p
,
3292 unsigned dim
= i
->Dimension
;
3293 unsigned nparam
= num_p
.length();
3294 unsigned nvar
= dim
- nparam
;
3298 rays
.SetDims(dim
, nvar
);
3299 add_rays(rays
, i
, &r
, nvar
, true);
3300 den_s
= rays
* lambda
;
3304 for (int j
= 0; j
< den_s
.length(); ++j
) {
3305 values2zz(i
->Ray
[j
]+1+nvar
, f
[j
], nparam
);
3306 if (den_s
[j
] == 0) {
3310 if (First_Non_Zero(i
->Ray
[j
]+1+nvar
, nparam
) != -1) {
3321 den_s
[j
] = abs(den_s
[j
]);
3330 gen_fun
* barvinok_series(Polyhedron
*P
, Polyhedron
* C
, unsigned MaxRays
)
3332 Polyhedron
** vcone
;
3334 unsigned nparam
= C
->Dimension
;
3338 sign
.SetLength(ncone
);
3340 CA
= align_context(C
, P
->Dimension
, MaxRays
);
3341 P
= DomainIntersection(P
, CA
, MaxRays
);
3342 Polyhedron_Free(CA
);
3344 assert(!Polyhedron_is_infinite(P
, nparam
));
3345 assert(P
->NbBid
== 0);
3346 assert(P
->NbEq
== 0);
3349 nvar
= dim
- nparam
;
3350 vcone
= new Polyhedron_p
[P
->NbRays
];
3352 for (int j
= 0; j
< P
->NbRays
; ++j
) {
3354 Polyhedron
*C
= supporting_cone(P
, j
);
3355 decompose(C
, &vcone
[j
], &npos
, &nneg
, MaxRays
);
3356 ncone
+= npos
+ nneg
;
3357 sign
.SetLength(ncone
);
3358 for (int k
= 0; k
< npos
; ++k
)
3359 sign
[ncone
-nneg
-k
-1] = 1;
3360 for (int k
= 0; k
< nneg
; ++k
)
3361 sign
[ncone
-k
-1] = -1;
3365 rays
.SetDims(ncone
* dim
, nvar
);
3367 for (int j
= 0; j
< P
->NbRays
; ++j
) {
3368 for (Polyhedron
*i
= vcone
[j
]; i
; i
= i
->next
) {
3369 add_rays(rays
, i
, &r
, nvar
);
3372 rays
.SetDims(r
, nvar
);
3374 nonorthog(rays
, lambda
);
3377 cout << "rays: " << rays;
3378 cout << "lambda: " << lambda;
3384 num_p
.SetLength(nparam
);
3387 den_s
.SetLength(dim
);
3389 den_p
.SetLength(dim
);
3391 den
.SetDims(dim
, nparam
);
3397 gen_fun
* gf
= new gen_fun
;
3399 for (int j
= 0; j
< P
->NbRays
; ++j
) {
3400 for (Polyhedron
*i
= vcone
[j
]; i
; i
= i
->next
, ++f
) {
3401 lattice_point(P
->Ray
[j
]+1, i
, vertex
);
3404 for ( ; k
< nvar
; ++k
)
3405 num_s
+= vertex
[k
] * lambda
[k
];
3406 for ( ; k
< dim
; ++k
)
3407 num_p
[k
-nvar
] = vertex
[k
];
3408 normalize(i
, lambda
, sign
[f
], num_s
, num_p
,
3413 for (int k
= 0; k
< dim
; ++k
) {
3416 else if (den_s
[k
] == 0)
3419 if (no_param
== 0) {
3420 for (int k
= 0; k
< dim
; ++k
)
3423 gf
->add(sign
[f
], one
, num_p
, den
);
3424 } else if (no_param
+ only_param
== dim
) {
3427 pden
.SetDims(only_param
, nparam
);
3429 for (k
= 0, l
= 0; k
< dim
; ++k
)
3433 for (k
= 0; k
< dim
; ++k
)
3437 dpoly
n(no_param
, num_s
);
3438 dpoly
d(no_param
, den_s
[k
], 1);
3439 for ( ; k
< dim
; ++k
)
3440 if (den_s
[k
] != 0) {
3441 dpoly
fact(no_param
, den_s
[k
], 1);
3445 mpq_set_si(count
, 0, 1);
3446 n
.div(d
, count
, sign
[f
]);
3449 value2zz(mpq_numref(count
), qn
);
3450 value2zz(mpq_denref(count
), qd
);
3452 gf
->add(qn
, qd
, num_p
, pden
);
3457 pden
.SetDims(only_param
, nparam
);
3459 for (k
= 0, l
= 0; k
< dim
; ++k
)
3463 for (k
= 0; k
< dim
; ++k
)
3467 dpoly
n(no_param
, num_s
);
3468 dpoly
d(no_param
, den_s
[k
], 1);
3469 for ( ; k
< dim
; ++k
)
3470 if (den_p
[k
] == 0) {
3471 dpoly
fact(no_param
, den_s
[k
], 1);
3475 for (k
= 0; k
< dim
; ++k
) {
3476 if (den_s
[k
] == 0 || den_p
[k
] == 0)
3479 dpoly
pd(no_param
-1, den_s
[k
], 1);
3480 int s
= den_p
[k
] < 0 ? -1 : 1;
3483 r
= new dpoly_r(n
, pd
, k
, s
, dim
);
3485 assert(0); // for now
3488 r
->div(d
, sign
[f
], gf
, pden
, den
, num_p
);
3492 cout << "sign: " << sign[f];
3493 cout << "num_s: " << num_s;
3494 cout << "num_p: " << num_p;
3495 cout << "den_s: " << den_s;
3496 cout << "den_p: " << den_p;
3497 cout << "den: " << den;
3498 cout << "only_param: " << only_param;
3499 cout << "no_param: " << no_param;