8 #include <NTL/mat_ZZ.h>
12 #include <polylib/polylibgmp.h>
13 #include "ev_operations.h"
26 using std::ostringstream
;
28 #define ALLOC(p) (((long *) (p))[0])
29 #define SIZE(p) (((long *) (p))[1])
30 #define DATA(p) ((mp_limb_t *) (((long *) (p)) + 2))
32 static void value2zz(Value v
, ZZ
& z
)
34 int sa
= v
[0]._mp_size
;
35 int abs_sa
= sa
< 0 ? -sa
: sa
;
37 _ntl_gsetlength(&z
.rep
, abs_sa
);
38 mp_limb_t
* adata
= DATA(z
.rep
);
39 for (int i
= 0; i
< abs_sa
; ++i
)
40 adata
[i
] = v
[0]._mp_d
[i
];
44 static void zz2value(ZZ
& z
, Value
& v
)
52 int abs_sa
= sa
< 0 ? -sa
: sa
;
54 mp_limb_t
* adata
= DATA(z
.rep
);
55 mpz_realloc2(v
, __GMP_BITS_PER_MP_LIMB
* abs_sa
);
56 for (int i
= 0; i
< abs_sa
; ++i
)
57 v
[0]._mp_d
[i
] = adata
[i
];
62 #define ALLOC(p) p = (typeof(p))malloc(sizeof(*p))
65 * We just ignore the last column and row
66 * If the final element is not equal to one
67 * then the result will actually be a multiple of the input
69 static void matrix2zz(Matrix
*M
, mat_ZZ
& m
, unsigned nr
, unsigned nc
)
73 for (int i
= 0; i
< nr
; ++i
) {
74 // assert(value_one_p(M->p[i][M->NbColumns - 1]));
75 for (int j
= 0; j
< nc
; ++j
) {
76 value2zz(M
->p
[i
][j
], m
[i
][j
]);
81 static void values2zz(Value
*p
, vec_ZZ
& v
, int len
)
85 for (int i
= 0; i
< len
; ++i
) {
91 * We add a 0 at the end, because we need it afterwards
93 static Vector
* zz2vector(vec_ZZ
& v
)
95 Vector
*vec
= Vector_Alloc(v
.length()+1);
97 for (int i
= 0; i
< v
.length(); ++i
)
98 zz2value(v
[i
], vec
->p
[i
]);
100 value_set_si(vec
->p
[v
.length()], 0);
105 static void rays(mat_ZZ
& r
, Polyhedron
*C
)
107 unsigned dim
= C
->NbRays
- 1; /* don't count zero vertex */
108 assert(C
->NbRays
- 1 == C
->Dimension
);
113 for (i
= 0, c
= 0; i
< dim
; ++i
)
114 if (value_zero_p(C
->Ray
[i
][dim
+1])) {
115 for (int j
= 0; j
< dim
; ++j
) {
116 value2zz(C
->Ray
[i
][j
+1], tmp
);
123 static Matrix
* rays(Polyhedron
*C
)
125 unsigned dim
= C
->NbRays
- 1; /* don't count zero vertex */
126 assert(C
->NbRays
- 1 == C
->Dimension
);
128 Matrix
*M
= Matrix_Alloc(dim
+1, dim
+1);
132 for (i
= 0, c
= 0; i
<= dim
&& c
< dim
; ++i
)
133 if (value_zero_p(C
->Ray
[i
][dim
+1])) {
134 Vector_Copy(C
->Ray
[i
] + 1, M
->p
[c
], dim
);
135 value_set_si(M
->p
[c
++][dim
], 0);
138 value_set_si(M
->p
[dim
][dim
], 1);
143 static Matrix
* rays2(Polyhedron
*C
)
145 unsigned dim
= C
->NbRays
- 1; /* don't count zero vertex */
146 assert(C
->NbRays
- 1 == C
->Dimension
);
148 Matrix
*M
= Matrix_Alloc(dim
, dim
);
152 for (i
= 0, c
= 0; i
<= dim
&& c
< dim
; ++i
)
153 if (value_zero_p(C
->Ray
[i
][dim
+1]))
154 Vector_Copy(C
->Ray
[i
] + 1, M
->p
[c
++], dim
);
161 * Returns the largest absolute value in the vector
163 static ZZ
max(vec_ZZ
& v
)
166 for (int i
= 1; i
< v
.length(); ++i
)
176 Rays
= Matrix_Copy(M
);
179 cone(Polyhedron
*C
) {
180 Cone
= Polyhedron_Copy(C
);
186 matrix2zz(Rays
, A
, Rays
->NbRows
- 1, Rays
->NbColumns
- 1);
187 det
= determinant(A
);
194 Vector
* short_vector(vec_ZZ
& lambda
) {
195 Matrix
*M
= Matrix_Copy(Rays
);
196 Matrix
*inv
= Matrix_Alloc(M
->NbRows
, M
->NbColumns
);
197 int ok
= Matrix_Inverse(M
, inv
);
204 matrix2zz(inv
, B
, inv
->NbRows
- 1, inv
->NbColumns
- 1);
205 long r
= LLL(det2
, B
, U
);
209 for (int i
= 1; i
< B
.NumRows(); ++i
) {
221 Vector
*z
= zz2vector(U
[index
]);
224 Polyhedron
*C
= poly();
226 for (i
= 0; i
< C
->NbConstraints
; ++i
) {
227 Inner_Product(z
->p
, C
->Constraint
[i
]+1, z
->Size
-1, &tmp
);
228 if (value_pos_p(tmp
))
231 if (i
== C
->NbConstraints
) {
232 value_set_si(tmp
, -1);
233 Vector_Scale(z
->p
, z
->p
, tmp
, z
->Size
-1);
240 Polyhedron_Free(Cone
);
246 Matrix
*M
= Matrix_Alloc(Rays
->NbRows
+1, Rays
->NbColumns
+1);
247 for (int i
= 0; i
< Rays
->NbRows
; ++i
) {
248 Vector_Copy(Rays
->p
[i
], M
->p
[i
]+1, Rays
->NbColumns
);
249 value_set_si(M
->p
[i
][0], 1);
251 Vector_Set(M
->p
[Rays
->NbRows
]+1, 0, Rays
->NbColumns
-1);
252 value_set_si(M
->p
[Rays
->NbRows
][0], 1);
253 value_set_si(M
->p
[Rays
->NbRows
][Rays
->NbColumns
], 1);
254 Cone
= Rays2Polyhedron(M
, M
->NbRows
+1);
255 assert(Cone
->NbConstraints
== Cone
->NbRays
);
269 dpoly(int d
, ZZ
& degree
, int offset
= 0) {
270 coeff
.SetLength(d
+1);
272 int min
= d
+ offset
;
273 if (degree
< ZZ(INIT_VAL
, min
))
274 min
= to_int(degree
);
276 ZZ c
= ZZ(INIT_VAL
, 1);
279 for (int i
= 1; i
<= min
; ++i
) {
280 c
*= (degree
-i
+ 1);
285 void operator *= (dpoly
& f
) {
286 assert(coeff
.length() == f
.coeff
.length());
288 coeff
= f
.coeff
[0] * coeff
;
289 for (int i
= 1; i
< coeff
.length(); ++i
)
290 for (int j
= 0; i
+j
< coeff
.length(); ++j
)
291 coeff
[i
+j
] += f
.coeff
[i
] * old
[j
];
293 void div(dpoly
& d
, mpq_t count
, ZZ
& sign
) {
294 int len
= coeff
.length();
297 mpq_t
* c
= new mpq_t
[coeff
.length()];
300 for (int i
= 0; i
< len
; ++i
) {
302 zz2value(coeff
[i
], tmp
);
303 mpq_set_z(c
[i
], tmp
);
305 for (int j
= 1; j
<= i
; ++j
) {
306 zz2value(d
.coeff
[j
], tmp
);
307 mpq_set_z(qtmp
, tmp
);
308 mpq_mul(qtmp
, qtmp
, c
[i
-j
]);
309 mpq_sub(c
[i
], c
[i
], qtmp
);
312 zz2value(d
.coeff
[0], tmp
);
313 mpq_set_z(qtmp
, tmp
);
314 mpq_div(c
[i
], c
[i
], qtmp
);
317 mpq_sub(count
, count
, c
[len
-1]);
319 mpq_add(count
, count
, c
[len
-1]);
323 for (int i
= 0; i
< len
; ++i
)
335 dpoly_n(int d
, ZZ
& degree_0
, ZZ
& degree_1
, int offset
= 0) {
339 zz2value(degree_0
, d0
);
340 zz2value(degree_1
, d1
);
341 coeff
= Matrix_Alloc(d
+1, d
+1+1);
342 value_set_si(coeff
->p
[0][0], 1);
343 value_set_si(coeff
->p
[0][d
+1], 1);
344 for (int i
= 1; i
<= d
; ++i
) {
345 value_multiply(coeff
->p
[i
][0], coeff
->p
[i
-1][0], d0
);
346 Vector_Combine(coeff
->p
[i
-1], coeff
->p
[i
-1]+1, coeff
->p
[i
]+1,
348 value_set_si(coeff
->p
[i
][d
+1], i
);
349 value_multiply(coeff
->p
[i
][d
+1], coeff
->p
[i
][d
+1], coeff
->p
[i
-1][d
+1]);
350 value_decrement(d0
, d0
);
355 void div(dpoly
& d
, Vector
*count
, ZZ
& sign
) {
356 int len
= coeff
->NbRows
;
357 Matrix
* c
= Matrix_Alloc(coeff
->NbRows
, coeff
->NbColumns
);
360 for (int i
= 0; i
< len
; ++i
) {
361 Vector_Copy(coeff
->p
[i
], c
->p
[i
], len
+1);
362 for (int j
= 1; j
<= i
; ++j
) {
363 zz2value(d
.coeff
[j
], tmp
);
364 value_multiply(tmp
, tmp
, c
->p
[i
][len
]);
365 value_oppose(tmp
, tmp
);
366 Vector_Combine(c
->p
[i
], c
->p
[i
-j
], c
->p
[i
],
367 c
->p
[i
-j
][len
], tmp
, len
);
368 value_multiply(c
->p
[i
][len
], c
->p
[i
][len
], c
->p
[i
-j
][len
]);
370 zz2value(d
.coeff
[0], tmp
);
371 value_multiply(c
->p
[i
][len
], c
->p
[i
][len
], tmp
);
374 value_set_si(tmp
, -1);
375 Vector_Scale(c
->p
[len
-1], count
->p
, tmp
, len
);
376 value_assign(count
->p
[len
], c
->p
[len
-1][len
]);
378 Vector_Copy(c
->p
[len
-1], count
->p
, len
+1);
379 Vector_Normalize(count
->p
, len
+1);
386 * Barvinok's Decomposition of a simplicial cone
388 * Returns two lists of polyhedra
390 void barvinok_decompose(Polyhedron
*C
, Polyhedron
**ppos
, Polyhedron
**pneg
)
392 Polyhedron
*pos
= *ppos
, *neg
= *pneg
;
393 vector
<cone
*> nonuni
;
394 cone
* c
= new cone(C
);
401 Polyhedron
*p
= Polyhedron_Copy(c
->Cone
);
407 while (!nonuni
.empty()) {
410 Vector
* v
= c
->short_vector(lambda
);
411 for (int i
= 0; i
< c
->Rays
->NbRows
- 1; ++i
) {
414 Matrix
* M
= Matrix_Copy(c
->Rays
);
415 Vector_Copy(v
->p
, M
->p
[i
], v
->Size
);
416 cone
* pc
= new cone(M
);
417 assert (pc
->det
!= 0);
418 if (abs(pc
->det
) > 1) {
419 assert(abs(pc
->det
) < abs(c
->det
));
420 nonuni
.push_back(pc
);
422 Polyhedron
*p
= pc
->poly();
424 if (sign(pc
->det
) == s
) {
443 * Returns a single list of npos "positive" cones followed by nneg
445 * The input cone is freed
447 void decompose(Polyhedron
*cone
, Polyhedron
**parts
, int *npos
, int *nneg
, unsigned MaxRays
)
449 Polyhedron_Polarize(cone
);
450 if (cone
->NbRays
- 1 != cone
->Dimension
) {
451 Polyhedron
*tmp
= cone
;
452 cone
= triangularize_cone(cone
, MaxRays
);
453 Polyhedron_Free(tmp
);
455 Polyhedron
*polpos
= NULL
, *polneg
= NULL
;
456 *npos
= 0; *nneg
= 0;
457 for (Polyhedron
*Polar
= cone
; Polar
; Polar
= Polar
->next
)
458 barvinok_decompose(Polar
, &polpos
, &polneg
);
461 for (Polyhedron
*i
= polpos
; i
; i
= i
->next
) {
462 Polyhedron_Polarize(i
);
466 for (Polyhedron
*i
= polneg
; i
; i
= i
->next
) {
467 Polyhedron_Polarize(i
);
478 const int MAX_TRY
=10;
480 * Searches for a vector that is not othogonal to any
481 * of the rays in rays.
483 static void nonorthog(mat_ZZ
& rays
, vec_ZZ
& lambda
)
485 int dim
= rays
.NumCols();
487 lambda
.SetLength(dim
);
488 for (int i
= 2; !found
&& i
<= 50*dim
; i
+=4) {
489 for (int j
= 0; j
< MAX_TRY
; ++j
) {
490 for (int k
= 0; k
< dim
; ++k
) {
491 int r
= random_int(i
)+2;
492 int v
= (2*(r
%2)-1) * (r
>> 1);
496 for (; k
< rays
.NumRows(); ++k
)
497 if (lambda
* rays
[k
] == 0)
499 if (k
== rays
.NumRows()) {
508 static void add_rays(mat_ZZ
& rays
, Polyhedron
*i
, int *r
)
510 unsigned dim
= i
->Dimension
;
511 for (int k
= 0; k
< i
->NbRays
; ++k
) {
512 if (!value_zero_p(i
->Ray
[k
][dim
+1]))
514 values2zz(i
->Ray
[k
]+1, rays
[(*r
)++], dim
);
518 void lattice_point(Value
* values
, Polyhedron
*i
, vec_ZZ
& lambda
, ZZ
& num
)
521 unsigned dim
= i
->Dimension
;
522 if(!value_one_p(values
[dim
])) {
523 Matrix
* Rays
= rays(i
);
524 Matrix
*inv
= Matrix_Alloc(Rays
->NbRows
, Rays
->NbColumns
);
525 int ok
= Matrix_Inverse(Rays
, inv
);
529 Vector
*lambda
= Vector_Alloc(dim
+1);
530 Vector_Matrix_Product(values
, inv
, lambda
->p
);
532 for (int j
= 0; j
< dim
; ++j
)
533 mpz_cdiv_q(lambda
->p
[j
], lambda
->p
[j
], lambda
->p
[dim
]);
534 value_set_si(lambda
->p
[dim
], 1);
535 Vector
*A
= Vector_Alloc(dim
+1);
536 Vector_Matrix_Product(lambda
->p
, Rays
, A
->p
);
539 values2zz(A
->p
, vertex
, dim
);
542 values2zz(values
, vertex
, dim
);
544 num
= vertex
* lambda
;
547 static evalue
*term(int param
, ZZ
& c
, Value
*den
= NULL
)
549 evalue
*EP
= new evalue();
551 value_set_si(EP
->d
,0);
552 EP
->x
.p
= new_enode(polynomial
, 2, param
+ 1);
553 evalue_set_si(&EP
->x
.p
->arr
[0], 0, 1);
554 value_init(EP
->x
.p
->arr
[1].x
.n
);
556 value_set_si(EP
->x
.p
->arr
[1].d
, 1);
558 value_assign(EP
->x
.p
->arr
[1].d
, *den
);
559 zz2value(c
, EP
->x
.p
->arr
[1].x
.n
);
563 static void vertex_period(
564 Polyhedron
*i
, vec_ZZ
& lambda
, Matrix
*T
,
565 Value lcm
, int p
, Vector
*val
,
566 evalue
*E
, evalue
* ev
,
569 unsigned nparam
= T
->NbRows
- 1;
570 unsigned dim
= i
->Dimension
;
576 Vector
* values
= Vector_Alloc(dim
+ 1);
577 Vector_Matrix_Product(val
->p
, T
, values
->p
);
578 value_assign(values
->p
[dim
], lcm
);
579 lattice_point(values
->p
, i
, lambda
, num
);
584 zz2value(num
, ev
->x
.n
);
585 value_assign(ev
->d
, lcm
);
592 values2zz(T
->p
[p
], vertex
, dim
);
593 nump
= vertex
* lambda
;
594 if (First_Non_Zero(val
->p
, p
) == -1) {
595 value_assign(tmp
, lcm
);
596 evalue
*ET
= term(p
, nump
, &tmp
);
598 free_evalue_refs(ET
);
602 value_assign(tmp
, lcm
);
603 if (First_Non_Zero(T
->p
[p
], dim
) != -1)
604 Vector_Gcd(T
->p
[p
], dim
, &tmp
);
606 if (value_lt(tmp
, lcm
)) {
609 value_division(tmp
, lcm
, tmp
);
610 value_set_si(ev
->d
, 0);
611 ev
->x
.p
= new_enode(periodic
, VALUE_TO_INT(tmp
), p
+1);
612 value2zz(tmp
, count
);
614 value_decrement(tmp
, tmp
);
616 ZZ new_offset
= offset
- count
* nump
;
617 value_assign(val
->p
[p
], tmp
);
618 vertex_period(i
, lambda
, T
, lcm
, p
+1, val
, E
,
619 &ev
->x
.p
->arr
[VALUE_TO_INT(tmp
)], new_offset
);
620 } while (value_pos_p(tmp
));
622 vertex_period(i
, lambda
, T
, lcm
, p
+1, val
, E
, ev
, offset
);
626 static void mask_r(Matrix
*f
, int nr
, Vector
*lcm
, int p
, Vector
*val
, evalue
*ev
)
628 unsigned nparam
= lcm
->Size
;
631 Vector
* prod
= Vector_Alloc(f
->NbRows
);
632 Matrix_Vector_Product(f
, val
->p
, prod
->p
);
634 for (int i
= 0; i
< nr
; ++i
) {
635 value_modulus(prod
->p
[i
], prod
->p
[i
], f
->p
[i
][nparam
+1]);
636 isint
&= value_zero_p(prod
->p
[i
]);
638 value_set_si(ev
->d
, 1);
640 value_set_si(ev
->x
.n
, isint
);
647 if (value_one_p(lcm
->p
[p
]))
648 mask_r(f
, nr
, lcm
, p
+1, val
, ev
);
650 value_assign(tmp
, lcm
->p
[p
]);
651 value_set_si(ev
->d
, 0);
652 ev
->x
.p
= new_enode(periodic
, VALUE_TO_INT(tmp
), p
+1);
654 value_decrement(tmp
, tmp
);
655 value_assign(val
->p
[p
], tmp
);
656 mask_r(f
, nr
, lcm
, p
+1, val
, &ev
->x
.p
->arr
[VALUE_TO_INT(tmp
)]);
657 } while (value_pos_p(tmp
));
662 static evalue
*multi_monom(vec_ZZ
& p
)
664 evalue
*X
= new evalue();
667 unsigned nparam
= p
.length()-1;
668 zz2value(p
[nparam
], X
->x
.n
);
669 value_set_si(X
->d
, 1);
670 for (int i
= 0; i
< nparam
; ++i
) {
673 evalue
*T
= term(i
, p
[i
]);
682 static void mask(Matrix
*f
, evalue
*factor
)
684 int nr
= f
->NbRows
, nc
= f
->NbColumns
;
687 for (n
= 0; n
< nr
&& value_notzero_p(f
->p
[n
][nc
-1]); ++n
)
688 if (value_notone_p(f
->p
[n
][nc
-1]) &&
689 value_notmone_p(f
->p
[n
][nc
-1]))
697 for (n
= 0; n
< nr
; ++n
) {
698 if (value_one_p(f
->p
[n
][nc
-1]) ||
699 value_mone_p(f
->p
[n
][nc
-1]))
702 value_set_si(EP
.d
, 0);
703 EP
.x
.p
= new_enode(relation
, 2, 0);
704 value_clear(EP
.x
.p
->arr
[1].d
);
705 EP
.x
.p
->arr
[1] = *factor
;
706 evalue
*ev
= &EP
.x
.p
->arr
[0];
707 value_set_si(ev
->d
, 0);
708 ev
->x
.p
= new_enode(modulo
, 3, VALUE_TO_INT(f
->p
[n
][nc
-1]));
709 evalue_set_si(&ev
->x
.p
->arr
[1], 0, 1);
710 evalue_set_si(&ev
->x
.p
->arr
[2], 1, 1);
712 values2zz(f
->p
[n
], row
, nc
-1);
713 evalue
*E
= multi_monom(row
);
714 value_clear(ev
->x
.p
->arr
[0].d
);
715 ev
->x
.p
->arr
[0] = *E
;
724 static void mask(Matrix
*f
, evalue
*factor
)
726 int nr
= f
->NbRows
, nc
= f
->NbColumns
;
729 for (n
= 0; n
< nr
&& value_notzero_p(f
->p
[n
][nc
-1]); ++n
)
730 if (value_notone_p(f
->p
[n
][nc
-1]) &&
731 value_notmone_p(f
->p
[n
][nc
-1]))
739 unsigned np
= nc
- 2;
740 Vector
*lcm
= Vector_Alloc(np
);
741 Vector
*val
= Vector_Alloc(nc
);
742 Vector_Set(val
->p
, 0, nc
);
743 value_set_si(val
->p
[np
], 1);
744 Vector_Set(lcm
->p
, 1, np
);
745 for (n
= 0; n
< nr
; ++n
) {
746 if (value_one_p(f
->p
[n
][nc
-1]) ||
747 value_mone_p(f
->p
[n
][nc
-1]))
749 for (int j
= 0; j
< np
; ++j
)
750 if (value_notzero_p(f
->p
[n
][j
])) {
751 Gcd(f
->p
[n
][j
], f
->p
[n
][nc
-1], &tmp
);
752 value_division(tmp
, f
->p
[n
][nc
-1], tmp
);
753 value_lcm(tmp
, lcm
->p
[j
], &lcm
->p
[j
]);
758 mask_r(f
, nr
, lcm
, 0, val
, &EP
);
763 free_evalue_refs(&EP
);
775 void ceil_mod(Value
*coef
, int len
, Value d
, ZZ
& f
, evalue
*EP
)
781 value_set_si(mone
, -1);
785 Vector_Gcd(coef
, len
, &gcd
);
787 Vector_AntiScale(coef
, coef
, gcd
, len
);
788 Vector_Scale(coef
, coef
, mone
, len
);
789 values2zz(coef
, num
, len
);
791 value_division(gcd
, d
, gcd
);
794 if (value_one_p(gcd
))
798 for (j
= 0; j
< len
; ++j
)
800 for (j
= 0; j
< len
; ++j
)
808 evalue_set_si(&tmp
, 0, 1);
810 if (j
< len
-1 && num
[j
] > g
/2) {
811 for (int k
= j
; k
< len
-1; ++k
)
814 num
[len
-1] = g
- 1 - num
[len
-1];
815 value_assign(tmp
.d
, gcd
);
817 zz2value(t
, tmp
.x
.n
);
823 ZZ t
= num
[len
-1] * f
;
824 zz2value(t
, tmp
.x
.n
);
825 value_assign(tmp
.d
, gcd
);
828 evalue
*E
= multi_monom(num
);
832 value_set_si(EV
.d
, 0);
833 EV
.x
.p
= new_enode(modulo
, 3, VALUE_TO_INT(gcd
));
834 evalue_copy(&EV
.x
.p
->arr
[0], E
);
835 evalue_set_si(&EV
.x
.p
->arr
[1], 0, 1);
836 value_init(EV
.x
.p
->arr
[2].x
.n
);
837 zz2value(f
, EV
.x
.p
->arr
[2].x
.n
);
838 value_assign(EV
.x
.p
->arr
[2].d
, gcd
);
841 free_evalue_refs(&EV
);
846 free_evalue_refs(&tmp
);
853 evalue
* ceil3(Value
*coef
, int len
, Value d
)
855 Vector
*val
= Vector_Alloc(len
);
859 value_set_si(mone
, -1);
860 value_absolute(d
, d
);
861 Vector_Scale(coef
, val
->p
, mone
, len
);
865 values2zz(val
->p
, num
, len
);
866 evalue
*EP
= multi_monom(num
);
871 value_set_si(tmp
.x
.n
, 1);
872 value_assign(tmp
.d
, d
);
878 ceil_mod(val
->p
, len
, d
, one
, EP
);
880 /* copy EP to malloc'ed evalue */
886 free_evalue_refs(&tmp
);
891 evalue
* lattice_point(Polyhedron
*i
, vec_ZZ
& lambda
, Matrix
*W
, Value lcm
)
893 unsigned nparam
= W
->NbColumns
- 1;
895 Matrix
* Rays
= rays2(i
);
896 Matrix
*T
= Transpose(Rays
);
897 Matrix
*T2
= Matrix_Copy(T
);
898 Matrix
*inv
= Matrix_Alloc(T2
->NbRows
, T2
->NbColumns
);
899 int ok
= Matrix_Inverse(T2
, inv
);
904 matrix2zz(W
, vertex
, W
->NbRows
, W
->NbColumns
);
907 num
= lambda
* vertex
;
909 evalue
*EP
= multi_monom(num
);
914 value_set_si(tmp
.x
.n
, 1);
915 value_assign(tmp
.d
, lcm
);
919 Matrix
*L
= Matrix_Alloc(inv
->NbRows
, W
->NbColumns
);
920 Matrix_Product(inv
, W
, L
);
923 matrix2zz(T
, RT
, T
->NbRows
, T
->NbColumns
);
926 vec_ZZ p
= lambda
* RT
;
928 for (int i
= 0; i
< L
->NbRows
; ++i
) {
929 ceil_mod(L
->p
[i
], nparam
+1, lcm
, p
[i
], EP
);
935 free_evalue_refs(&tmp
);
939 evalue
* lattice_point(Polyhedron
*i
, vec_ZZ
& lambda
, Matrix
*W
, Value lcm
)
941 Matrix
*T
= Transpose(W
);
942 unsigned nparam
= T
->NbRows
- 1;
944 evalue
*EP
= new evalue();
946 evalue_set_si(EP
, 0, 1);
949 Vector
*val
= Vector_Alloc(nparam
+1);
950 value_set_si(val
->p
[nparam
], 1);
951 ZZ
offset(INIT_VAL
, 0);
953 vertex_period(i
, lambda
, T
, lcm
, 0, val
, EP
, &ev
, offset
);
956 free_evalue_refs(&ev
);
967 Param_Vertices
* V
, Polyhedron
*i
, vec_ZZ
& lambda
, term_info
* term
)
969 unsigned nparam
= V
->Vertex
->NbColumns
- 2;
970 unsigned dim
= i
->Dimension
;
972 vertex
.SetDims(V
->Vertex
->NbRows
, nparam
+1);
976 value_set_si(lcm
, 1);
977 for (int j
= 0; j
< V
->Vertex
->NbRows
; ++j
) {
978 value_lcm(lcm
, V
->Vertex
->p
[j
][nparam
+1], &lcm
);
980 if (value_notone_p(lcm
)) {
981 Matrix
* mv
= Matrix_Alloc(dim
, nparam
+1);
982 for (int j
= 0 ; j
< dim
; ++j
) {
983 value_division(tmp
, lcm
, V
->Vertex
->p
[j
][nparam
+1]);
984 Vector_Scale(V
->Vertex
->p
[j
], mv
->p
[j
], tmp
, nparam
+1);
987 term
->E
= lattice_point(i
, lambda
, mv
, lcm
);
995 for (int i
= 0; i
< V
->Vertex
->NbRows
; ++i
) {
996 assert(value_one_p(V
->Vertex
->p
[i
][nparam
+1])); // for now
997 values2zz(V
->Vertex
->p
[i
], vertex
[i
], nparam
+1);
1001 num
= lambda
* vertex
;
1005 for (int j
= 0; j
< nparam
; ++j
)
1011 term
->E
= multi_monom(num
);
1015 term
->constant
= num
[nparam
];
1018 term
->coeff
= num
[p
];
1025 void normalize(Polyhedron
*i
, vec_ZZ
& lambda
, ZZ
& sign
, ZZ
& num
, vec_ZZ
& den
)
1027 unsigned dim
= i
->Dimension
;
1031 rays
.SetDims(dim
, dim
);
1032 add_rays(rays
, i
, &r
);
1033 den
= rays
* lambda
;
1036 for (int j
= 0; j
< den
.length(); ++j
) {
1040 den
[j
] = abs(den
[j
]);
1048 void barvinok_count(Polyhedron
*P
, Value
* result
, unsigned NbMaxCons
)
1050 Polyhedron
** vcone
;
1053 sign
.SetLength(ncone
);
1061 value_set_si(*result
, 0);
1065 for (; r
< P
->NbRays
; ++r
)
1066 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1]))
1068 if (P
->NbBid
!=0 || r
< P
->NbRays
) {
1069 value_set_si(*result
, -1);
1073 P
= remove_equalities(P
);
1076 value_set_si(*result
, 0);
1082 value_set_si(factor
, 1);
1083 Q
= Polyhedron_Reduce(P
, &factor
);
1090 if (P
->Dimension
== 0) {
1091 value_assign(*result
, factor
);
1094 value_clear(factor
);
1099 vcone
= new (Polyhedron
*)[P
->NbRays
];
1101 for (int j
= 0; j
< P
->NbRays
; ++j
) {
1103 Polyhedron
*C
= supporting_cone(P
, j
);
1104 decompose(C
, &vcone
[j
], &npos
, &nneg
, NbMaxCons
);
1105 ncone
+= npos
+ nneg
;
1106 sign
.SetLength(ncone
);
1107 for (int k
= 0; k
< npos
; ++k
)
1108 sign
[ncone
-nneg
-k
-1] = 1;
1109 for (int k
= 0; k
< nneg
; ++k
)
1110 sign
[ncone
-k
-1] = -1;
1114 rays
.SetDims(ncone
* dim
, dim
);
1116 for (int j
= 0; j
< P
->NbRays
; ++j
) {
1117 for (Polyhedron
*i
= vcone
[j
]; i
; i
= i
->next
) {
1118 assert(i
->NbRays
-1 == dim
);
1119 add_rays(rays
, i
, &r
);
1123 nonorthog(rays
, lambda
);
1127 num
.SetLength(ncone
);
1128 den
.SetDims(ncone
,dim
);
1131 for (int j
= 0; j
< P
->NbRays
; ++j
) {
1132 for (Polyhedron
*i
= vcone
[j
]; i
; i
= i
->next
) {
1133 lattice_point(P
->Ray
[j
]+1, i
, lambda
, num
[f
]);
1134 normalize(i
, lambda
, sign
[f
], num
[f
], den
[f
]);
1139 for (int j
= 1; j
< num
.length(); ++j
)
1142 for (int j
= 0; j
< num
.length(); ++j
)
1148 for (int j
= 0; j
< P
->NbRays
; ++j
) {
1149 for (Polyhedron
*i
= vcone
[j
]; i
; i
= i
->next
) {
1150 dpoly
d(dim
, num
[f
]);
1151 dpoly
n(dim
, den
[f
][0], 1);
1152 for (int k
= 1; k
< dim
; ++k
) {
1153 dpoly
fact(dim
, den
[f
][k
], 1);
1156 d
.div(n
, count
, sign
[f
]);
1160 assert(value_one_p(&count
[0]._mp_den
));
1161 value_multiply(*result
, &count
[0]._mp_num
, factor
);
1164 for (int j
= 0; j
< P
->NbRays
; ++j
)
1165 Domain_Free(vcone
[j
]);
1171 value_clear(factor
);
1174 static void uni_polynom(int param
, Vector
*c
, evalue
*EP
)
1176 unsigned dim
= c
->Size
-2;
1178 value_set_si(EP
->d
,0);
1179 EP
->x
.p
= new_enode(polynomial
, dim
+1, param
+1);
1180 for (int j
= 0; j
<= dim
; ++j
)
1181 evalue_set(&EP
->x
.p
->arr
[j
], c
->p
[j
], c
->p
[dim
+1]);
1184 static void multi_polynom(Vector
*c
, evalue
* X
, evalue
*EP
)
1186 unsigned dim
= c
->Size
-2;
1190 evalue_set(&EC
, c
->p
[dim
], c
->p
[dim
+1]);
1193 evalue_set(EP
, c
->p
[dim
], c
->p
[dim
+1]);
1195 for (int i
= dim
-1; i
>= 0; --i
) {
1197 value_assign(EC
.x
.n
, c
->p
[i
]);
1200 free_evalue_refs(&EC
);
1204 Enumeration
* barvinok_enumerate(Polyhedron
*P
, Polyhedron
* C
, unsigned MaxRays
)
1206 Polyhedron
*CEq
= NULL
, *rVD
, *CA
;
1208 Param_Polyhedron
*PP
= NULL
;
1209 Param_Domain
*D
, *next
;
1211 Enumeration
*en
, *res
;
1213 unsigned nparam
= C
->Dimension
;
1215 value_init(factor
.d
);
1216 evalue_set_si(&factor
, 1, 1);
1220 CA
= align_context(C
, P
->Dimension
, MaxRays
);
1221 P
= DomainIntersection(P
, CA
, MaxRays
);
1222 Polyhedron_Free(CA
);
1224 if (C
->Dimension
== 0 || emptyQ(P
)) {
1226 res
= (Enumeration
*)malloc(sizeof(Enumeration
));
1227 res
->ValidityDomain
= CEq
? CEq
: Polyhedron_Copy(C
);
1229 value_init(res
->EP
.d
);
1230 value_set_si(res
->EP
.d
, 1);
1231 value_init(res
->EP
.x
.n
);
1233 value_set_si(res
->EP
.x
.n
, 0);
1235 barvinok_count(P
, &res
->EP
.x
.n
, MaxRays
);
1236 emul(&factor
, &res
->EP
);
1238 free_evalue_refs(&factor
);
1243 Param_Polyhedron_Free(PP
);
1250 P
= remove_equalities_p(P
, P
->Dimension
-nparam
, &f
);
1253 if (P
->Dimension
== nparam
) {
1255 P
= Universe_Polyhedron(0);
1261 Polyhedron
*Q
= ParamPolyhedron_Reduce(P
, P
->Dimension
-nparam
, &factor
);
1263 if (Q
->Dimension
== nparam
) {
1265 P
= Universe_Polyhedron(0);
1272 Polyhedron
*oldP
= P
;
1273 PP
= Polyhedron2Param_SimplifiedDomain(&P
,C
,MaxRays
,&CEq
,&CT
);
1275 Polyhedron_Free(oldP
);
1277 if (isIdentity(CT
)) {
1281 assert(CT
->NbRows
!= CT
->NbColumns
);
1282 if (CT
->NbRows
== 1) // no more parameters
1284 nparam
= CT
->NbRows
- 1;
1287 unsigned dim
= P
->Dimension
- nparam
;
1288 Polyhedron
** vcone
= new (Polyhedron
*)[PP
->nbV
];
1289 int * npos
= new int[PP
->nbV
];
1290 int * nneg
= new int[PP
->nbV
];
1294 for (i
= 0, V
= PP
->V
; V
; ++i
, V
= V
->next
) {
1295 Polyhedron
*C
= supporting_cone_p(P
, V
);
1296 decompose(C
, &vcone
[i
], &npos
[i
], &nneg
[i
], MaxRays
);
1299 Vector
*c
= Vector_Alloc(dim
+2);
1301 for(D
=PP
->D
; D
; D
=next
) {
1308 Dt
= CT
? Polyhedron_Preimage(D
->Domain
,CT
,MaxRays
) : D
->Domain
;
1309 rVD
= DomainIntersection(Dt
,CEq
,MaxRays
);
1311 /* if rVD is empty or too small in geometric dimension */
1312 if(!rVD
|| emptyQ(rVD
) ||
1313 (rVD
->Dimension
-rVD
->NbEq
< Dt
->Dimension
-Dt
->NbEq
-CEq
->NbEq
)) {
1315 Polyhedron_Free(rVD
);
1317 Polyhedron_Free(Dt
);
1318 continue; /* empty validity domain */
1321 Polyhedron_Free(Dt
);
1324 sign
.SetLength(ncone
);
1325 FORALL_PVertex_in_ParamPolyhedron(V
,D
,PP
) // _i is internal counter
1326 ncone
+= npos
[_i
] + nneg
[_i
];
1327 sign
.SetLength(ncone
);
1328 for (int k
= 0; k
< npos
[_i
]; ++k
)
1329 sign
[ncone
-nneg
[_i
]-k
-1] = 1;
1330 for (int k
= 0; k
< nneg
[_i
]; ++k
)
1331 sign
[ncone
-k
-1] = -1;
1332 END_FORALL_PVertex_in_ParamPolyhedron
;
1335 rays
.SetDims(ncone
* dim
, dim
);
1337 FORALL_PVertex_in_ParamPolyhedron(V
,D
,PP
) // _i is internal counter
1338 for (Polyhedron
*i
= vcone
[_i
]; i
; i
= i
->next
) {
1339 assert(i
->NbRays
-1 == dim
);
1340 add_rays(rays
, i
, &r
);
1342 END_FORALL_PVertex_in_ParamPolyhedron
;
1344 nonorthog(rays
, lambda
);
1347 den
.SetDims(ncone
,dim
);
1348 term_info
*num
= new term_info
[ncone
];
1351 FORALL_PVertex_in_ParamPolyhedron(V
,D
,PP
)
1352 for (Polyhedron
*i
= vcone
[_i
]; i
; i
= i
->next
) {
1353 lattice_point(V
, i
, lambda
, &num
[f
]);
1354 normalize(i
, lambda
, sign
[f
], num
[f
].constant
, den
[f
]);
1357 END_FORALL_PVertex_in_ParamPolyhedron
;
1358 ZZ min
= num
[0].constant
;
1359 for (int j
= 1; j
< ncone
; ++j
)
1360 if (num
[j
].constant
< min
)
1361 min
= num
[j
].constant
;
1362 for (int j
= 0; j
< ncone
; ++j
)
1363 num
[j
].constant
-= min
;
1367 evalue_set_si(&EP
, 0, 1);
1370 FORALL_PVertex_in_ParamPolyhedron(V
,D
,PP
)
1371 for (Polyhedron
*i
= vcone
[_i
]; i
; i
= i
->next
) {
1372 dpoly
n(dim
, den
[f
][0], 1);
1373 for (int k
= 1; k
< dim
; ++k
) {
1374 dpoly
fact(dim
, den
[f
][k
], 1);
1377 if (num
[f
].E
!= NULL
) {
1378 ZZ
one(INIT_VAL
, 1);
1379 dpoly_n
d(dim
, num
[f
].constant
, one
);
1380 d
.div(n
, c
, sign
[f
]);
1382 multi_polynom(c
, num
[f
].E
, &EV
);
1384 free_evalue_refs(&EV
);
1385 free_evalue_refs(num
[f
].E
);
1387 } else if (num
[f
].pos
!= -1) {
1388 dpoly_n
d(dim
, num
[f
].constant
, num
[f
].coeff
);
1389 d
.div(n
, c
, sign
[f
]);
1391 uni_polynom(num
[f
].pos
, c
, &EV
);
1393 free_evalue_refs(&EV
);
1395 mpq_set_si(count
, 0, 1);
1396 dpoly
d(dim
, num
[f
].constant
);
1397 d
.div(n
, count
, sign
[f
]);
1400 evalue_set(&EV
, &count
[0]._mp_num
, &count
[0]._mp_den
);
1402 free_evalue_refs(&EV
);
1406 END_FORALL_PVertex_in_ParamPolyhedron
;
1411 en
= (Enumeration
*)malloc(sizeof(Enumeration
));
1414 res
->ValidityDomain
= rVD
;
1416 addeliminatedparams_evalue(&EP
, CT
);
1419 reduce_evalue(&res
->EP
);
1424 for (int j
= 0; j
< PP
->nbV
; ++j
)
1425 Domain_Free(vcone
[j
]);
1431 Polyhedron_Free(CEq
);