8 #include <NTL/mat_ZZ.h>
12 #include <polylib/polylibgmp.h>
13 #include "ev_operations.h"
25 using std::ostringstream
;
27 #define ALLOC(p) (((long *) (p))[0])
28 #define SIZE(p) (((long *) (p))[1])
29 #define DATA(p) ((mp_limb_t *) (((long *) (p)) + 2))
31 static void value2zz(Value v
, ZZ
& z
)
33 int sa
= v
[0]._mp_size
;
34 int abs_sa
= sa
< 0 ? -sa
: sa
;
36 _ntl_gsetlength(&z
.rep
, abs_sa
);
37 mp_limb_t
* adata
= DATA(z
.rep
);
38 for (int i
= 0; i
< abs_sa
; ++i
)
39 adata
[i
] = v
[0]._mp_d
[i
];
43 static void zz2value(ZZ
& z
, Value
& v
)
51 int abs_sa
= sa
< 0 ? -sa
: sa
;
53 mp_limb_t
* adata
= DATA(z
.rep
);
54 mpz_realloc2(v
, __GMP_BITS_PER_MP_LIMB
* abs_sa
);
55 for (int i
= 0; i
< abs_sa
; ++i
)
56 v
[0]._mp_d
[i
] = adata
[i
];
61 * We just ignore the last column and row
62 * If the final element is not equal to one
63 * then the result will actually be a multiple of the input
65 static void matrix2zz(Matrix
*M
, mat_ZZ
& m
, unsigned nr
, unsigned nc
)
69 for (int i
= 0; i
< nr
; ++i
) {
70 // assert(value_one_p(M->p[i][M->NbColumns - 1]));
71 for (int j
= 0; j
< nc
; ++j
) {
72 value2zz(M
->p
[i
][j
], m
[i
][j
]);
77 static void values2zz(Value
*p
, vec_ZZ
& v
, int len
)
81 for (int i
= 0; i
< len
; ++i
) {
87 * We add a 0 at the end, because we need it afterwards
89 static Vector
* zz2vector(vec_ZZ
& v
)
91 Vector
*vec
= Vector_Alloc(v
.length()+1);
93 for (int i
= 0; i
< v
.length(); ++i
)
94 zz2value(v
[i
], vec
->p
[i
]);
96 value_set_si(vec
->p
[v
.length()], 0);
101 static void rays(mat_ZZ
& r
, Polyhedron
*C
)
103 unsigned dim
= C
->NbRays
- 1; /* don't count zero vertex */
104 assert(C
->NbRays
- 1 == C
->Dimension
);
109 for (i
= 0, c
= 0; i
< dim
; ++i
)
110 if (value_zero_p(C
->Ray
[i
][dim
+1])) {
111 for (int j
= 0; j
< dim
; ++j
) {
112 value2zz(C
->Ray
[i
][j
+1], tmp
);
119 static Matrix
* rays(Polyhedron
*C
)
121 unsigned dim
= C
->NbRays
- 1; /* don't count zero vertex */
122 assert(C
->NbRays
- 1 == C
->Dimension
);
124 Matrix
*M
= Matrix_Alloc(dim
+1, dim
+1);
128 for (i
= 0, c
= 0; i
<= dim
&& c
< dim
; ++i
)
129 if (value_zero_p(C
->Ray
[i
][dim
+1])) {
130 Vector_Copy(C
->Ray
[i
] + 1, M
->p
[c
], dim
);
131 value_set_si(M
->p
[c
++][dim
], 0);
134 value_set_si(M
->p
[dim
][dim
], 1);
140 * Returns the largest absolute value in the vector
142 static ZZ
max(vec_ZZ
& v
)
145 for (int i
= 1; i
< v
.length(); ++i
)
155 Rays
= Matrix_Copy(M
);
158 cone(Polyhedron
*C
) {
159 Cone
= Polyhedron_Copy(C
);
165 matrix2zz(Rays
, A
, Rays
->NbRows
- 1, Rays
->NbColumns
- 1);
166 det
= determinant(A
);
173 Vector
* short_vector(vec_ZZ
& lambda
) {
174 Matrix
*M
= Matrix_Copy(Rays
);
175 Matrix
*inv
= Matrix_Alloc(M
->NbRows
, M
->NbColumns
);
176 int ok
= Matrix_Inverse(M
, inv
);
183 matrix2zz(inv
, B
, inv
->NbRows
- 1, inv
->NbColumns
- 1);
184 long r
= LLL(det2
, B
, U
);
188 for (int i
= 1; i
< B
.NumRows(); ++i
) {
200 Vector
*z
= zz2vector(U
[index
]);
203 Polyhedron
*C
= poly();
205 for (i
= 0; i
< C
->NbConstraints
; ++i
) {
206 Inner_Product(z
->p
, C
->Constraint
[i
]+1, z
->Size
-1, &tmp
);
207 if (value_pos_p(tmp
))
210 if (i
== C
->NbConstraints
) {
211 value_set_si(tmp
, -1);
212 Vector_Scale(z
->p
, z
->p
, tmp
, z
->Size
-1);
219 Polyhedron_Free(Cone
);
225 Matrix
*M
= Matrix_Alloc(Rays
->NbRows
+1, Rays
->NbColumns
+1);
226 for (int i
= 0; i
< Rays
->NbRows
; ++i
) {
227 Vector_Copy(Rays
->p
[i
], M
->p
[i
]+1, Rays
->NbColumns
);
228 value_set_si(M
->p
[i
][0], 1);
230 Vector_Set(M
->p
[Rays
->NbRows
]+1, 0, Rays
->NbColumns
-1);
231 value_set_si(M
->p
[Rays
->NbRows
][0], 1);
232 value_set_si(M
->p
[Rays
->NbRows
][Rays
->NbColumns
], 1);
233 Cone
= Rays2Polyhedron(M
, M
->NbRows
+1);
234 assert(Cone
->NbConstraints
== Cone
->NbRays
);
248 dpoly(int d
, ZZ
& degree
, int offset
= 0) {
249 coeff
.SetLength(d
+1);
251 int min
= d
+ offset
;
252 if (degree
< ZZ(INIT_VAL
, min
))
253 min
= to_int(degree
);
255 ZZ c
= ZZ(INIT_VAL
, 1);
258 for (int i
= 1; i
<= min
; ++i
) {
259 c
*= (degree
-i
+ 1);
264 void operator *= (dpoly
& f
) {
265 assert(coeff
.length() == f
.coeff
.length());
267 coeff
= f
.coeff
[0] * coeff
;
268 for (int i
= 1; i
< coeff
.length(); ++i
)
269 for (int j
= 0; i
+j
< coeff
.length(); ++j
)
270 coeff
[i
+j
] += f
.coeff
[i
] * old
[j
];
272 void div(dpoly
& d
, mpq_t count
, ZZ
& sign
) {
273 int len
= coeff
.length();
276 mpq_t
* c
= new mpq_t
[coeff
.length()];
279 for (int i
= 0; i
< len
; ++i
) {
281 zz2value(coeff
[i
], tmp
);
282 mpq_set_z(c
[i
], tmp
);
284 for (int j
= 1; j
<= i
; ++j
) {
285 zz2value(d
.coeff
[j
], tmp
);
286 mpq_set_z(qtmp
, tmp
);
287 mpq_mul(qtmp
, qtmp
, c
[i
-j
]);
288 mpq_sub(c
[i
], c
[i
], qtmp
);
291 zz2value(d
.coeff
[0], tmp
);
292 mpq_set_z(qtmp
, tmp
);
293 mpq_div(c
[i
], c
[i
], qtmp
);
296 mpq_sub(count
, count
, c
[len
-1]);
298 mpq_add(count
, count
, c
[len
-1]);
302 for (int i
= 0; i
< len
; ++i
)
314 dpoly_n(int d
, ZZ
& degree_0
, ZZ
& degree_1
, int offset
= 0) {
318 zz2value(degree_0
, d0
);
319 zz2value(degree_1
, d1
);
320 coeff
= Matrix_Alloc(d
+1, d
+1+1);
321 value_set_si(coeff
->p
[0][0], 1);
322 value_set_si(coeff
->p
[0][d
+1], 1);
323 for (int i
= 1; i
<= d
; ++i
) {
324 value_multiply(coeff
->p
[i
][0], coeff
->p
[i
-1][0], d0
);
325 Vector_Combine(coeff
->p
[i
-1], coeff
->p
[i
-1]+1, coeff
->p
[i
]+1,
327 value_set_si(coeff
->p
[i
][d
+1], i
);
328 value_multiply(coeff
->p
[i
][d
+1], coeff
->p
[i
][d
+1], coeff
->p
[i
-1][d
+1]);
329 value_decrement(d0
, d0
);
334 void div(dpoly
& d
, Vector
*count
, ZZ
& sign
) {
335 int len
= coeff
->NbRows
;
336 Matrix
* c
= Matrix_Alloc(coeff
->NbRows
, coeff
->NbColumns
);
339 for (int i
= 0; i
< len
; ++i
) {
340 Vector_Copy(coeff
->p
[i
], c
->p
[i
], len
+1);
341 for (int j
= 1; j
<= i
; ++j
) {
342 zz2value(d
.coeff
[j
], tmp
);
343 value_multiply(tmp
, tmp
, c
->p
[i
][len
]);
344 value_oppose(tmp
, tmp
);
345 Vector_Combine(c
->p
[i
], c
->p
[i
-j
], c
->p
[i
],
346 c
->p
[i
-j
][len
], tmp
, len
);
347 value_multiply(c
->p
[i
][len
], c
->p
[i
][len
], c
->p
[i
-j
][len
]);
349 zz2value(d
.coeff
[0], tmp
);
350 value_multiply(c
->p
[i
][len
], c
->p
[i
][len
], tmp
);
353 value_set_si(tmp
, -1);
354 Vector_Scale(c
->p
[len
-1], count
->p
, tmp
, len
);
355 value_assign(count
->p
[len
], c
->p
[len
-1][len
]);
357 Vector_Copy(c
->p
[len
-1], count
->p
, len
+1);
358 Vector_Normalize(count
->p
, len
+1);
365 * Barvinok's Decomposition of a simplicial cone
367 * Returns two lists of polyhedra
369 void barvinok_decompose(Polyhedron
*C
, Polyhedron
**ppos
, Polyhedron
**pneg
)
371 Polyhedron
*pos
= *ppos
, *neg
= *pneg
;
372 vector
<cone
*> nonuni
;
373 cone
* c
= new cone(C
);
380 Polyhedron
*p
= Polyhedron_Copy(c
->Cone
);
386 while (!nonuni
.empty()) {
389 Vector
* v
= c
->short_vector(lambda
);
390 for (int i
= 0; i
< c
->Rays
->NbRows
- 1; ++i
) {
393 Matrix
* M
= Matrix_Copy(c
->Rays
);
394 Vector_Copy(v
->p
, M
->p
[i
], v
->Size
);
395 cone
* pc
= new cone(M
);
396 assert (pc
->det
!= 0);
397 if (abs(pc
->det
) > 1) {
398 assert(abs(pc
->det
) < abs(c
->det
));
399 nonuni
.push_back(pc
);
401 Polyhedron
*p
= pc
->poly();
403 if (sign(pc
->det
) == s
) {
422 * Returns a single list of npos "positive" cones followed by nneg
424 * The input cone is freed
426 void decompose(Polyhedron
*cone
, Polyhedron
**parts
, int *npos
, int *nneg
, unsigned MaxRays
)
428 Polyhedron_Polarize(cone
);
429 if (cone
->NbRays
- 1 != cone
->Dimension
) {
430 Polyhedron
*tmp
= cone
;
431 cone
= triangularize_cone(cone
, MaxRays
);
432 Polyhedron_Free(tmp
);
434 Polyhedron
*polpos
= NULL
, *polneg
= NULL
;
435 *npos
= 0; *nneg
= 0;
436 for (Polyhedron
*Polar
= cone
; Polar
; Polar
= Polar
->next
)
437 barvinok_decompose(Polar
, &polpos
, &polneg
);
440 for (Polyhedron
*i
= polpos
; i
; i
= i
->next
) {
441 Polyhedron_Polarize(i
);
445 for (Polyhedron
*i
= polneg
; i
; i
= i
->next
) {
446 Polyhedron_Polarize(i
);
457 const int MAX_TRY
=10;
459 * Searches for a vector that is not othogonal to any
460 * of the rays in rays.
462 static void nonorthog(mat_ZZ
& rays
, vec_ZZ
& lambda
)
464 int dim
= rays
.NumCols();
466 lambda
.SetLength(dim
);
467 for (int i
= 2; !found
&& i
<= 50*dim
; i
+=4) {
468 for (int j
= 0; j
< MAX_TRY
; ++j
) {
469 for (int k
= 0; k
< dim
; ++k
) {
470 int r
= random_int(i
)+2;
471 int v
= (2*(r
%2)-1) * (r
>> 1);
475 for (; k
< rays
.NumRows(); ++k
)
476 if (lambda
* rays
[k
] == 0)
478 if (k
== rays
.NumRows()) {
487 static void add_rays(mat_ZZ
& rays
, Polyhedron
*i
, int *r
)
489 unsigned dim
= i
->Dimension
;
490 for (int k
= 0; k
< i
->NbRays
; ++k
) {
491 if (!value_zero_p(i
->Ray
[k
][dim
+1]))
493 values2zz(i
->Ray
[k
]+1, rays
[(*r
)++], dim
);
497 void lattice_point(Value
* values
, Polyhedron
*i
, vec_ZZ
& lambda
, ZZ
& num
)
500 unsigned dim
= i
->Dimension
;
501 if(!value_one_p(values
[dim
])) {
502 Matrix
* Rays
= rays(i
);
503 Matrix
*inv
= Matrix_Alloc(Rays
->NbRows
, Rays
->NbColumns
);
504 int ok
= Matrix_Inverse(Rays
, inv
);
508 Vector
*lambda
= Vector_Alloc(dim
+1);
509 Vector_Matrix_Product(values
, inv
, lambda
->p
);
511 for (int j
= 0; j
< dim
; ++j
)
512 mpz_cdiv_q(lambda
->p
[j
], lambda
->p
[j
], lambda
->p
[dim
]);
513 value_set_si(lambda
->p
[dim
], 1);
514 Vector
*A
= Vector_Alloc(dim
+1);
515 Vector_Matrix_Product(lambda
->p
, Rays
, A
->p
);
518 values2zz(A
->p
, vertex
, dim
);
521 values2zz(values
, vertex
, dim
);
523 num
= vertex
* lambda
;
526 static evalue
*term(int param
, ZZ
& c
, Value
*den
= NULL
)
528 evalue
*EP
= new evalue();
530 value_set_si(EP
->d
,0);
531 EP
->x
.p
= new_enode(polynomial
, 2, param
+ 1);
532 evalue_set_si(&EP
->x
.p
->arr
[0], 0, 1);
533 value_init(EP
->x
.p
->arr
[1].x
.n
);
535 value_set_si(EP
->x
.p
->arr
[1].d
, 1);
537 value_assign(EP
->x
.p
->arr
[1].d
, *den
);
538 zz2value(c
, EP
->x
.p
->arr
[1].x
.n
);
542 static void vertex_period(
543 Polyhedron
*i
, vec_ZZ
& lambda
, Matrix
*T
,
544 Value lcm
, int p
, Vector
*val
,
545 evalue
*E
, evalue
* ev
,
548 unsigned nparam
= T
->NbRows
- 1;
549 unsigned dim
= i
->Dimension
;
555 Vector
* values
= Vector_Alloc(dim
+ 1);
556 Vector_Matrix_Product(val
->p
, T
, values
->p
);
557 value_assign(values
->p
[dim
], lcm
);
558 lattice_point(values
->p
, i
, lambda
, num
);
563 zz2value(num
, ev
->x
.n
);
564 value_assign(ev
->d
, lcm
);
571 values2zz(T
->p
[p
], vertex
, dim
);
572 nump
= vertex
* lambda
;
573 if (First_Non_Zero(val
->p
, p
) == -1) {
574 value_assign(tmp
, lcm
);
575 evalue
*ET
= term(p
, nump
, &tmp
);
577 free_evalue_refs(ET
);
581 value_assign(tmp
, lcm
);
582 if (First_Non_Zero(T
->p
[p
], dim
) != -1)
583 Vector_Gcd(T
->p
[p
], dim
, &tmp
);
585 if (value_lt(tmp
, lcm
)) {
588 value_division(tmp
, lcm
, tmp
);
589 value_set_si(ev
->d
, 0);
590 ev
->x
.p
= new_enode(periodic
, VALUE_TO_INT(tmp
), p
+1);
591 value2zz(tmp
, count
);
593 value_decrement(tmp
, tmp
);
595 ZZ new_offset
= offset
- count
* nump
;
596 value_assign(val
->p
[p
], tmp
);
597 vertex_period(i
, lambda
, T
, lcm
, p
+1, val
, E
,
598 &ev
->x
.p
->arr
[VALUE_TO_INT(tmp
)], new_offset
);
599 } while (value_pos_p(tmp
));
601 vertex_period(i
, lambda
, T
, lcm
, p
+1, val
, E
, ev
, offset
);
605 static void mask_r(Matrix
*f
, int nr
, Vector
*lcm
, int p
, Vector
*val
, evalue
*ev
)
607 unsigned nparam
= lcm
->Size
;
610 Vector
* prod
= Vector_Alloc(f
->NbRows
);
611 Matrix_Vector_Product(f
, val
->p
, prod
->p
);
613 for (int i
= 0; i
< nr
; ++i
) {
614 value_modulus(prod
->p
[i
], prod
->p
[i
], f
->p
[i
][nparam
+1]);
615 isint
&= value_zero_p(prod
->p
[i
]);
617 value_set_si(ev
->d
, 1);
619 value_set_si(ev
->x
.n
, isint
);
626 if (value_one_p(lcm
->p
[p
]))
627 mask_r(f
, nr
, lcm
, p
+1, val
, ev
);
629 value_assign(tmp
, lcm
->p
[p
]);
630 value_set_si(ev
->d
, 0);
631 ev
->x
.p
= new_enode(periodic
, VALUE_TO_INT(tmp
), p
+1);
633 value_decrement(tmp
, tmp
);
634 value_assign(val
->p
[p
], tmp
);
635 mask_r(f
, nr
, lcm
, p
+1, val
, &ev
->x
.p
->arr
[VALUE_TO_INT(tmp
)]);
636 } while (value_pos_p(tmp
));
644 static void mask(Matrix
*f
, evalue
*factor
)
646 int nr
= f
->NbRows
, nc
= f
->NbColumns
;
649 for (n
= 0; n
< nr
&& value_notzero_p(f
->p
[n
][nc
-1]); ++n
)
650 if (value_notone_p(f
->p
[n
][nc
-1]) &&
651 value_notmone_p(f
->p
[n
][nc
-1]))
659 unsigned np
= nc
- 2;
660 Vector
*lcm
= Vector_Alloc(np
);
661 Vector
*val
= Vector_Alloc(nc
);
662 Vector_Set(val
->p
, 0, nc
);
663 value_set_si(val
->p
[np
], 1);
664 Vector_Set(lcm
->p
, 1, np
);
665 for (n
= 0; n
< nr
; ++n
) {
666 if (value_one_p(f
->p
[n
][nc
-1]) ||
667 value_mone_p(f
->p
[n
][nc
-1]))
669 for (int j
= 0; j
< np
; ++j
)
670 if (value_notzero_p(f
->p
[n
][j
])) {
671 Gcd(f
->p
[n
][j
], f
->p
[n
][nc
-1], &tmp
);
672 value_division(tmp
, f
->p
[n
][nc
-1], tmp
);
673 value_lcm(tmp
, lcm
->p
[j
], &lcm
->p
[j
]);
678 mask_r(f
, nr
, lcm
, 0, val
, &EP
);
683 free_evalue_refs(&EP
);
686 static evalue
*multi_mononom(vec_ZZ
& p
)
688 evalue
*X
= new evalue();
690 evalue_set_si(X
, 0, 1);
691 unsigned nparam
= p
.length()-1;
692 for (int i
= 0; i
< nparam
; ++i
) {
693 evalue
*T
= term(i
, p
[i
]);
709 Param_Vertices
* V
, Polyhedron
*i
, vec_ZZ
& lambda
, term_info
* term
)
711 unsigned nparam
= V
->Vertex
->NbColumns
- 2;
712 unsigned dim
= i
->Dimension
;
714 vertex
.SetDims(V
->Vertex
->NbRows
, nparam
+1);
718 value_set_si(lcm
, 1);
719 for (int j
= 0; j
< V
->Vertex
->NbRows
; ++j
) {
720 value_lcm(lcm
, V
->Vertex
->p
[j
][nparam
+1], &lcm
);
722 if (value_notone_p(lcm
)) {
723 Matrix
* Rays
= rays(i
);
724 Matrix
*inv
= Matrix_Alloc(Rays
->NbRows
, Rays
->NbColumns
);
725 int ok
= Matrix_Inverse(Rays
, inv
);
730 Matrix
* mv
= Matrix_Alloc(dim
, nparam
+1);
731 for (int j
= 0 ; j
< dim
; ++j
) {
732 value_division(tmp
, lcm
, V
->Vertex
->p
[j
][nparam
+1]);
733 Vector_Scale(V
->Vertex
->p
[j
], mv
->p
[j
], tmp
, nparam
+1);
735 Matrix
*T
= Transpose(mv
);
737 evalue
*EP
= new evalue();
739 evalue_set_si(EP
, 0, 1);
741 Vector
*val
= Vector_Alloc(nparam
+1);
742 value_set_si(val
->p
[nparam
], 1);
743 ZZ
offset(INIT_VAL
, 0);
745 vertex_period(i
, lambda
, T
, lcm
, 0, val
, EP
, &ev
, offset
);
748 free_evalue_refs(&ev
);
761 for (int i
= 0; i
< V
->Vertex
->NbRows
; ++i
) {
762 assert(value_one_p(V
->Vertex
->p
[i
][nparam
+1])); // for now
763 values2zz(V
->Vertex
->p
[i
], vertex
[i
], nparam
+1);
767 num
= lambda
* vertex
;
771 for (int j
= 0; j
< nparam
; ++j
)
777 term
->E
= multi_mononom(num
);
778 term
->constant
= num
[nparam
];
781 term
->constant
= num
[nparam
];
784 term
->coeff
= num
[p
];
791 void normalize(Polyhedron
*i
, vec_ZZ
& lambda
, ZZ
& sign
, ZZ
& num
, vec_ZZ
& den
)
793 unsigned dim
= i
->Dimension
;
797 rays
.SetDims(dim
, dim
);
798 add_rays(rays
, i
, &r
);
802 for (int j
= 0; j
< den
.length(); ++j
) {
806 den
[j
] = abs(den
[j
]);
814 void barvinok_count(Polyhedron
*P
, Value
* result
, unsigned NbMaxCons
)
819 sign
.SetLength(ncone
);
827 value_set_si(*result
, 0);
831 for (; r
< P
->NbRays
; ++r
)
832 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1]))
834 if (P
->NbBid
!=0 || r
< P
->NbRays
) {
835 value_set_si(*result
, -1);
839 P
= remove_equalities(P
);
842 value_set_si(*result
, 0);
848 value_set_si(factor
, 1);
849 Q
= Polyhedron_Reduce(P
, &factor
);
856 if (P
->Dimension
== 0) {
857 value_assign(*result
, factor
);
865 vcone
= new (Polyhedron
*)[P
->NbRays
];
867 for (int j
= 0; j
< P
->NbRays
; ++j
) {
869 Polyhedron
*C
= supporting_cone(P
, j
);
870 decompose(C
, &vcone
[j
], &npos
, &nneg
, NbMaxCons
);
871 ncone
+= npos
+ nneg
;
872 sign
.SetLength(ncone
);
873 for (int k
= 0; k
< npos
; ++k
)
874 sign
[ncone
-nneg
-k
-1] = 1;
875 for (int k
= 0; k
< nneg
; ++k
)
876 sign
[ncone
-k
-1] = -1;
880 rays
.SetDims(ncone
* dim
, dim
);
882 for (int j
= 0; j
< P
->NbRays
; ++j
) {
883 for (Polyhedron
*i
= vcone
[j
]; i
; i
= i
->next
) {
884 assert(i
->NbRays
-1 == dim
);
885 add_rays(rays
, i
, &r
);
889 nonorthog(rays
, lambda
);
893 num
.SetLength(ncone
);
894 den
.SetDims(ncone
,dim
);
897 for (int j
= 0; j
< P
->NbRays
; ++j
) {
898 for (Polyhedron
*i
= vcone
[j
]; i
; i
= i
->next
) {
899 lattice_point(P
->Ray
[j
]+1, i
, lambda
, num
[f
]);
900 normalize(i
, lambda
, sign
[f
], num
[f
], den
[f
]);
905 for (int j
= 1; j
< num
.length(); ++j
)
908 for (int j
= 0; j
< num
.length(); ++j
)
914 for (int j
= 0; j
< P
->NbRays
; ++j
) {
915 for (Polyhedron
*i
= vcone
[j
]; i
; i
= i
->next
) {
916 dpoly
d(dim
, num
[f
]);
917 dpoly
n(dim
, den
[f
][0], 1);
918 for (int k
= 1; k
< dim
; ++k
) {
919 dpoly
fact(dim
, den
[f
][k
], 1);
922 d
.div(n
, count
, sign
[f
]);
926 assert(value_one_p(&count
[0]._mp_den
));
927 value_multiply(*result
, &count
[0]._mp_num
, factor
);
930 for (int j
= 0; j
< P
->NbRays
; ++j
)
931 Domain_Free(vcone
[j
]);
940 static void uni_polynom(int param
, Vector
*c
, evalue
*EP
)
942 unsigned dim
= c
->Size
-2;
944 value_set_si(EP
->d
,0);
945 EP
->x
.p
= new_enode(polynomial
, dim
+1, param
+1);
946 for (int j
= 0; j
<= dim
; ++j
)
947 evalue_set(&EP
->x
.p
->arr
[j
], c
->p
[j
], c
->p
[dim
+1]);
950 static void multi_polynom(Vector
*c
, evalue
* X
, evalue
*EP
)
952 unsigned dim
= c
->Size
-2;
956 evalue_set(&EC
, c
->p
[dim
], c
->p
[dim
+1]);
959 evalue_set(EP
, c
->p
[dim
], c
->p
[dim
+1]);
961 for (int i
= dim
-1; i
>= 0; --i
) {
963 value_assign(EC
.x
.n
, c
->p
[i
]);
966 free_evalue_refs(&EC
);
970 Enumeration
* barvinok_enumerate(Polyhedron
*P
, Polyhedron
* C
, unsigned MaxRays
)
972 Polyhedron
*CEq
= NULL
, *rVD
, *CA
;
974 Param_Polyhedron
*PP
= NULL
;
975 Param_Domain
*D
, *next
;
977 Enumeration
*en
, *res
;
979 unsigned nparam
= C
->Dimension
;
981 value_init(factor
.d
);
982 evalue_set_si(&factor
, 1, 1);
986 CA
= align_context(C
, P
->Dimension
, MaxRays
);
987 P
= DomainIntersection(P
, CA
, MaxRays
);
990 if (C
->Dimension
== 0 || emptyQ(P
)) {
992 res
= (Enumeration
*)malloc(sizeof(Enumeration
));
993 res
->ValidityDomain
= CEq
? CEq
: Polyhedron_Copy(C
);
995 value_init(res
->EP
.d
);
996 value_set_si(res
->EP
.d
, 1);
997 value_init(res
->EP
.x
.n
);
999 value_set_si(res
->EP
.x
.n
, 0);
1001 barvinok_count(P
, &res
->EP
.x
.n
, MaxRays
);
1002 emul(&factor
, &res
->EP
);
1004 free_evalue_refs(&factor
);
1009 Param_Polyhedron_Free(PP
);
1016 P
= remove_equalities_p(P
, P
->Dimension
-nparam
, &f
);
1019 if (P
->Dimension
== nparam
) {
1021 P
= Universe_Polyhedron(0);
1025 Polyhedron
*oldP
= P
;
1026 PP
= Polyhedron2Param_SimplifiedDomain(&P
,C
,MaxRays
,&CEq
,&CT
);
1028 Polyhedron_Free(oldP
);
1030 if (isIdentity(CT
)) {
1034 assert(CT
->NbRows
!= CT
->NbColumns
);
1035 if (CT
->NbRows
== 1) // no more parameters
1037 nparam
= CT
->NbRows
- 1;
1040 unsigned dim
= P
->Dimension
- nparam
;
1041 Polyhedron
** vcone
= new (Polyhedron
*)[PP
->nbV
];
1042 int * npos
= new int[PP
->nbV
];
1043 int * nneg
= new int[PP
->nbV
];
1047 for (i
= 0, V
= PP
->V
; V
; ++i
, V
= V
->next
) {
1048 Polyhedron
*C
= supporting_cone_p(P
, V
);
1049 decompose(C
, &vcone
[i
], &npos
[i
], &nneg
[i
], MaxRays
);
1052 Vector
*c
= Vector_Alloc(dim
+2);
1054 for(D
=PP
->D
; D
; D
=next
) {
1061 Dt
= CT
? Polyhedron_Preimage(D
->Domain
,CT
,MaxRays
) : D
->Domain
;
1062 rVD
= DomainIntersection(Dt
,CEq
,MaxRays
);
1064 /* if rVD is empty or too small in geometric dimension */
1065 if(!rVD
|| emptyQ(rVD
) ||
1066 (rVD
->Dimension
-rVD
->NbEq
< Dt
->Dimension
-Dt
->NbEq
-CEq
->NbEq
)) {
1068 Polyhedron_Free(rVD
);
1070 Polyhedron_Free(Dt
);
1071 continue; /* empty validity domain */
1074 Polyhedron_Free(Dt
);
1077 sign
.SetLength(ncone
);
1078 FORALL_PVertex_in_ParamPolyhedron(V
,D
,PP
) // _i is internal counter
1079 ncone
+= npos
[_i
] + nneg
[_i
];
1080 sign
.SetLength(ncone
);
1081 for (int k
= 0; k
< npos
[_i
]; ++k
)
1082 sign
[ncone
-nneg
[_i
]-k
-1] = 1;
1083 for (int k
= 0; k
< nneg
[_i
]; ++k
)
1084 sign
[ncone
-k
-1] = -1;
1085 END_FORALL_PVertex_in_ParamPolyhedron
;
1088 rays
.SetDims(ncone
* dim
, dim
);
1090 FORALL_PVertex_in_ParamPolyhedron(V
,D
,PP
) // _i is internal counter
1091 for (Polyhedron
*i
= vcone
[_i
]; i
; i
= i
->next
) {
1092 assert(i
->NbRays
-1 == dim
);
1093 add_rays(rays
, i
, &r
);
1095 END_FORALL_PVertex_in_ParamPolyhedron
;
1097 nonorthog(rays
, lambda
);
1100 den
.SetDims(ncone
,dim
);
1101 term_info
*num
= new term_info
[ncone
];
1104 FORALL_PVertex_in_ParamPolyhedron(V
,D
,PP
)
1105 for (Polyhedron
*i
= vcone
[_i
]; i
; i
= i
->next
) {
1106 lattice_point(V
, i
, lambda
, &num
[f
]);
1107 normalize(i
, lambda
, sign
[f
], num
[f
].constant
, den
[f
]);
1110 END_FORALL_PVertex_in_ParamPolyhedron
;
1111 ZZ min
= num
[0].constant
;
1112 for (int j
= 1; j
< ncone
; ++j
)
1113 if (num
[j
].constant
< min
)
1114 min
= num
[j
].constant
;
1115 for (int j
= 0; j
< ncone
; ++j
)
1116 num
[j
].constant
-= min
;
1120 evalue_set_si(&EP
, 0, 1);
1123 FORALL_PVertex_in_ParamPolyhedron(V
,D
,PP
)
1124 for (Polyhedron
*i
= vcone
[_i
]; i
; i
= i
->next
) {
1125 dpoly
n(dim
, den
[f
][0], 1);
1126 for (int k
= 1; k
< dim
; ++k
) {
1127 dpoly
fact(dim
, den
[f
][k
], 1);
1130 if (num
[f
].E
!= NULL
) {
1131 ZZ
one(INIT_VAL
, 1);
1132 dpoly_n
d(dim
, num
[f
].constant
, one
);
1133 d
.div(n
, c
, sign
[f
]);
1135 multi_polynom(c
, num
[f
].E
, &EV
);
1137 free_evalue_refs(&EV
);
1138 free_evalue_refs(num
[f
].E
);
1140 } else if (num
[f
].pos
!= -1) {
1141 dpoly_n
d(dim
, num
[f
].constant
, num
[f
].coeff
);
1142 d
.div(n
, c
, sign
[f
]);
1144 uni_polynom(num
[f
].pos
, c
, &EV
);
1146 free_evalue_refs(&EV
);
1148 mpq_set_si(count
, 0, 1);
1149 dpoly
d(dim
, num
[f
].constant
);
1150 d
.div(n
, count
, sign
[f
]);
1153 evalue_set(&EV
, &count
[0]._mp_num
, &count
[0]._mp_den
);
1155 free_evalue_refs(&EV
);
1159 END_FORALL_PVertex_in_ParamPolyhedron
;
1164 en
= (Enumeration
*)malloc(sizeof(Enumeration
));
1167 res
->ValidityDomain
= rVD
;
1169 addeliminatedparams_evalue(&EP
, CT
);
1172 reduce_evalue(&res
->EP
);
1177 for (int j
= 0; j
< PP
->nbV
; ++j
)
1178 Domain_Free(vcone
[j
]);
1184 Polyhedron_Free(CEq
);