8 #include <NTL/mat_ZZ.h>
10 #include "ehrhartpolynom.h"
14 #include <polylib/polylibgmp.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 EhrhartPolynom
*term(string param
, ZZ
& c
, Value
*den
= NULL
)
529 deque
<string
> params
;
531 value_set_si(EP
.d
,0);
532 EP
.x
.p
= new_enode(polynomial
, 2, 1);
533 value_set_si(EP
.x
.p
->arr
[0].d
, 1);
534 value_set_si(EP
.x
.p
->arr
[0].x
.n
, 0);
536 value_set_si(EP
.x
.p
->arr
[1].d
, 1);
538 value_assign(EP
.x
.p
->arr
[1].d
, *den
);
539 zz2value(c
, EP
.x
.p
->arr
[1].x
.n
);
540 params
.push_back(param
);
541 EhrhartPolynom
* ret
= new EhrhartPolynom(&EP
, params
);
542 free_evalue_refs(&EP
);
546 static void vertex_period(deque
<string
>& params
,
547 Polyhedron
*i
, vec_ZZ
& lambda
, Matrix
*T
,
548 Value lcm
, int p
, Vector
*val
,
549 EhrhartPolynom
*E
, evalue
* ev
,
552 unsigned nparam
= T
->NbRows
- 1;
553 unsigned dim
= i
->Dimension
;
560 Vector
* values
= Vector_Alloc(dim
+ 1);
561 Vector_Matrix_Product(val
->p
, T
, values
->p
);
562 value_assign(values
->p
[dim
], lcm
);
563 lattice_point(values
->p
, i
, lambda
, num
);
567 zz2value(num
, ev
->x
.n
);
568 value_assign(ev
->d
, lcm
);
574 values2zz(T
->p
[p
], vertex
, dim
);
575 nump
= vertex
* lambda
;
576 if (First_Non_Zero(val
->p
, p
) == -1) {
577 value_assign(tmp
, lcm
);
578 EhrhartPolynom
* ET
= term(params
[p
], nump
, &tmp
);
583 value_assign(tmp
, lcm
);
584 if (First_Non_Zero(T
->p
[p
], dim
) != -1)
585 Vector_Gcd(T
->p
[p
], dim
, &tmp
);
586 if (value_lt(tmp
, lcm
)) {
589 value_division(tmp
, lcm
, tmp
);
590 value_set_si(ev
->d
, 0);
591 ev
->x
.p
= new_enode(periodic
, VALUE_TO_INT(tmp
), p
+1);
592 value2zz(tmp
, count
);
594 value_decrement(tmp
, tmp
);
596 ZZ new_offset
= offset
- count
* nump
;
597 value_assign(val
->p
[p
], tmp
);
598 vertex_period(params
, i
, lambda
, T
, lcm
, p
+1, val
, E
,
599 &ev
->x
.p
->arr
[VALUE_TO_INT(tmp
)], new_offset
);
600 } while (value_pos_p(tmp
));
602 vertex_period(params
, i
, lambda
, T
, lcm
, p
+1, val
, E
, ev
, offset
);
606 static EhrhartPolynom
*multi_mononom(deque
<string
>& params
, vec_ZZ
& p
)
608 EhrhartPolynom
*X
= new EhrhartPolynom();
609 unsigned nparam
= p
.length()-1;
610 for (int i
= 0; i
< nparam
; ++i
) {
611 EhrhartPolynom
*T
= term(params
[i
], p
[i
]);
625 void lattice_point(deque
<string
>& params
,
626 Param_Vertices
* V
, Polyhedron
*i
, vec_ZZ
& lambda
, term_info
* term
)
628 unsigned nparam
= V
->Vertex
->NbColumns
- 2;
629 unsigned dim
= i
->Dimension
;
631 vertex
.SetDims(V
->Vertex
->NbRows
, nparam
+1);
635 value_set_si(lcm
, 1);
636 for (int j
= 0; j
< V
->Vertex
->NbRows
; ++j
) {
637 value_lcm(lcm
, V
->Vertex
->p
[j
][nparam
+1], &lcm
);
639 if (value_notone_p(lcm
)) {
640 Matrix
* Rays
= rays(i
);
641 Matrix
*inv
= Matrix_Alloc(Rays
->NbRows
, Rays
->NbColumns
);
642 int ok
= Matrix_Inverse(Rays
, inv
);
647 Matrix
* mv
= Matrix_Alloc(dim
, nparam
+1);
648 for (int j
= 0 ; j
< dim
; ++j
) {
649 value_division(tmp
, lcm
, V
->Vertex
->p
[j
][nparam
+1]);
650 Vector_Scale(V
->Vertex
->p
[j
], mv
->p
[j
], tmp
, nparam
+1);
652 Matrix
*T
= Transpose(mv
);
654 EhrhartPolynom
* EP
= new EhrhartPolynom();
656 Vector
*val
= Vector_Alloc(nparam
+1);
657 value_set_si(val
->p
[nparam
], 1);
658 ZZ
offset(INIT_VAL
, 0);
659 vertex_period(params
, i
, lambda
, T
, lcm
, 0, val
, EP
, &ev
, offset
);
662 *EP
+= EhrhartPolynom(&ev
, params
);
669 for (int i
= 0; i
< V
->Vertex
->NbRows
; ++i
) {
670 assert(value_one_p(V
->Vertex
->p
[i
][nparam
+1])); // for now
671 values2zz(V
->Vertex
->p
[i
], vertex
[i
], nparam
+1);
675 num
= lambda
* vertex
;
679 for (int j
= 0; j
< nparam
; ++j
)
685 term
->E
= multi_mononom(params
, num
);
686 term
->constant
= num
[nparam
];
689 term
->constant
= num
[nparam
];
692 term
->coeff
= num
[p
];
699 void normalize(Polyhedron
*i
, vec_ZZ
& lambda
, ZZ
& sign
, ZZ
& num
, vec_ZZ
& den
)
701 unsigned dim
= i
->Dimension
;
705 rays
.SetDims(dim
, dim
);
706 add_rays(rays
, i
, &r
);
710 for (int j
= 0; j
< den
.length(); ++j
) {
714 den
[j
] = abs(den
[j
]);
722 void barvinok_count(Polyhedron
*P
, Value
* result
, unsigned NbMaxCons
)
727 sign
.SetLength(ncone
);
735 value_set_si(*result
, 0);
739 for (; r
< P
->NbRays
; ++r
)
740 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1]))
742 if (P
->NbBid
!=0 || r
< P
->NbRays
) {
743 value_set_si(*result
, -1);
747 P
= remove_equalities(P
);
750 value_set_si(*result
, 0);
756 value_set_si(factor
, 1);
757 Q
= Polyhedron_Reduce(P
, &factor
);
764 if (P
->Dimension
== 0) {
765 value_assign(*result
, factor
);
773 vcone
= new (Polyhedron
*)[P
->NbRays
];
775 for (int j
= 0; j
< P
->NbRays
; ++j
) {
777 Polyhedron
*C
= supporting_cone(P
, j
);
778 decompose(C
, &vcone
[j
], &npos
, &nneg
, NbMaxCons
);
779 ncone
+= npos
+ nneg
;
780 sign
.SetLength(ncone
);
781 for (int k
= 0; k
< npos
; ++k
)
782 sign
[ncone
-nneg
-k
-1] = 1;
783 for (int k
= 0; k
< nneg
; ++k
)
784 sign
[ncone
-k
-1] = -1;
788 rays
.SetDims(ncone
* dim
, dim
);
790 for (int j
= 0; j
< P
->NbRays
; ++j
) {
791 for (Polyhedron
*i
= vcone
[j
]; i
; i
= i
->next
) {
792 assert(i
->NbRays
-1 == dim
);
793 add_rays(rays
, i
, &r
);
797 nonorthog(rays
, lambda
);
801 num
.SetLength(ncone
);
802 den
.SetDims(ncone
,dim
);
805 for (int j
= 0; j
< P
->NbRays
; ++j
) {
806 for (Polyhedron
*i
= vcone
[j
]; i
; i
= i
->next
) {
807 lattice_point(P
->Ray
[j
]+1, i
, lambda
, num
[f
]);
808 normalize(i
, lambda
, sign
[f
], num
[f
], den
[f
]);
813 for (int j
= 1; j
< num
.length(); ++j
)
816 for (int j
= 0; j
< num
.length(); ++j
)
822 for (int j
= 0; j
< P
->NbRays
; ++j
) {
823 for (Polyhedron
*i
= vcone
[j
]; i
; i
= i
->next
) {
824 dpoly
d(dim
, num
[f
]);
825 dpoly
n(dim
, den
[f
][0], 1);
826 for (int k
= 1; k
< dim
; ++k
) {
827 dpoly
fact(dim
, den
[f
][k
], 1);
830 d
.div(n
, count
, sign
[f
]);
834 assert(value_one_p(&count
[0]._mp_den
));
835 value_multiply(*result
, &count
[0]._mp_num
, factor
);
838 for (int j
= 0; j
< P
->NbRays
; ++j
)
839 Domain_Free(vcone
[j
]);
848 static void default_params(deque
<string
>& params
, int n
)
850 for (int i
= 1; i
<= n
; ++i
) {
853 params
.push_back(s
.str());
857 static EhrhartPolynom
*uni_polynom(string param
, Vector
*c
)
860 deque
<string
> params
;
861 unsigned dim
= c
->Size
-2;
863 value_set_si(EP
.d
,0);
864 EP
.x
.p
= new_enode(polynomial
, dim
+1, 1);
865 for (int j
= 0; j
<= dim
; ++j
) {
866 value_assign(EP
.x
.p
->arr
[j
].d
, c
->p
[dim
+1]);
867 value_assign(EP
.x
.p
->arr
[j
].x
.n
, c
->p
[j
]);
869 params
.push_back(param
);
870 EhrhartPolynom
* ret
= new EhrhartPolynom(&EP
, params
);
871 free_evalue_refs(&EP
);
875 static EhrhartPolynom
*multi_polynom(deque
<string
>& params
, Vector
*c
, EhrhartPolynom
& X
)
877 unsigned dim
= c
->Size
-2;
881 value_assign(EC
.d
, c
->p
[dim
+1]);
883 EhrhartPolynom
*res
= new EhrhartPolynom();
884 value_assign(EC
.x
.n
, c
->p
[dim
]);
885 *res
+= EhrhartPolynom(&EC
, params
);
886 for (int i
= dim
-1; i
>= 0; --i
) {
888 value_assign(EC
.x
.n
, c
->p
[i
]);
889 *res
+= EhrhartPolynom(&EC
, params
);
891 free_evalue_refs(&EC
);
895 static EhrhartPolynom
*constant(mpq_t c
)
898 deque
<string
> params
;
901 value_assign(EP
.d
, &c
[0]._mp_den
);
902 value_assign(EP
.x
.n
, &c
[0]._mp_num
);
903 EhrhartPolynom
* ret
= new EhrhartPolynom(&EP
, params
);
904 free_evalue_refs(&EP
);
908 Enumeration
* barvinok_enumerate(Polyhedron
*P
, Polyhedron
* C
, unsigned MaxRays
)
912 Param_Polyhedron
*PP
;
915 Enumeration
*en
, *res
;
917 unsigned nparam
= C
->Dimension
;
923 P
= remove_equalities_p(P
, P
->Dimension
-nparam
, &f
);
928 assert(C
->Dimension
!= 0); // assume that there are parameters for now
929 PP
= Polyhedron2Param_SimplifiedDomain(&P
,C
,MaxRays
,&CEq
,&CT
);
930 assert(isIdentity(CT
)); // assume for now
932 deque
<string
> params
;
933 default_params(params
, nparam
);
934 unsigned dim
= P
->Dimension
- nparam
;
935 Polyhedron
** vcone
= new (Polyhedron
*)[PP
->nbV
];
936 int * npos
= new int[PP
->nbV
];
937 int * nneg
= new int[PP
->nbV
];
941 for (i
= 0, V
= PP
->V
; V
; ++i
, V
= V
->next
) {
942 Polyhedron
*C
= supporting_cone_p(P
, V
);
943 decompose(C
, &vcone
[i
], &npos
[i
], &nneg
[i
], MaxRays
);
946 Vector
*c
= Vector_Alloc(dim
+2);
948 for(D
=PP
->D
;D
;D
=D
->next
) {
950 sign
.SetLength(ncone
);
951 FORALL_PVertex_in_ParamPolyhedron(V
,D
,PP
) // _i is internal counter
952 ncone
+= npos
[_i
] + nneg
[_i
];
953 sign
.SetLength(ncone
);
954 for (int k
= 0; k
< npos
[_i
]; ++k
)
955 sign
[ncone
-nneg
[_i
]-k
-1] = 1;
956 for (int k
= 0; k
< nneg
[_i
]; ++k
)
957 sign
[ncone
-k
-1] = -1;
958 END_FORALL_PVertex_in_ParamPolyhedron
;
961 rays
.SetDims(ncone
* dim
, dim
);
963 FORALL_PVertex_in_ParamPolyhedron(V
,D
,PP
) // _i is internal counter
964 for (Polyhedron
*i
= vcone
[_i
]; i
; i
= i
->next
) {
965 assert(i
->NbRays
-1 == dim
);
966 add_rays(rays
, i
, &r
);
968 END_FORALL_PVertex_in_ParamPolyhedron
;
970 nonorthog(rays
, lambda
);
973 den
.SetDims(ncone
,dim
);
974 term_info
*num
= new term_info
[ncone
];
977 FORALL_PVertex_in_ParamPolyhedron(V
,D
,PP
)
978 for (Polyhedron
*i
= vcone
[_i
]; i
; i
= i
->next
) {
979 lattice_point(params
, V
, i
, lambda
, &num
[f
]);
980 normalize(i
, lambda
, sign
[f
], num
[f
].constant
, den
[f
]);
983 END_FORALL_PVertex_in_ParamPolyhedron
;
984 ZZ min
= num
[0].constant
;
985 for (int j
= 1; j
< ncone
; ++j
)
986 if (num
[j
].constant
< min
)
987 min
= num
[j
].constant
;
988 for (int j
= 0; j
< ncone
; ++j
)
989 num
[j
].constant
-= min
;
994 FORALL_PVertex_in_ParamPolyhedron(V
,D
,PP
)
995 for (Polyhedron
*i
= vcone
[_i
]; i
; i
= i
->next
) {
996 dpoly
n(dim
, den
[f
][0], 1);
997 for (int k
= 1; k
< dim
; ++k
) {
998 dpoly
fact(dim
, den
[f
][k
], 1);
1001 if (num
[f
].E
!= NULL
) {
1002 ZZ
one(INIT_VAL
, 1);
1003 dpoly_n
d(dim
, num
[f
].constant
, one
);
1004 d
.div(n
, c
, sign
[f
]);
1005 EhrhartPolynom
*ET
= multi_polynom(params
, c
, *num
[f
].E
);
1009 } else if (num
[f
].pos
!= -1) {
1010 dpoly_n
d(dim
, num
[f
].constant
, num
[f
].coeff
);
1011 d
.div(n
, c
, sign
[f
]);
1012 EhrhartPolynom
*E
= uni_polynom(params
[num
[f
].pos
], c
);
1016 mpq_set_si(count
, 0, 1);
1017 dpoly
d(dim
, num
[f
].constant
);
1018 d
.div(n
, count
, sign
[f
]);
1019 EhrhartPolynom
*E
= constant(count
);
1025 END_FORALL_PVertex_in_ParamPolyhedron
;
1030 en
= (Enumeration
*)malloc(sizeof(Enumeration
));
1033 res
->ValidityDomain
= D
->Domain
;
1034 res
->EP
= EP
.to_evalue(params
);
1035 reduce_evalue(&res
->EP
);
1040 for (int j
= 0; j
< PP
->nbV
; ++j
)
1041 Domain_Free(vcone
[j
]);