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
) {
92 static void zz2values(vec_ZZ
& v
, Value
*p
)
94 for (int i
= 0; i
< v
.length(); ++i
)
98 static void rays(mat_ZZ
& r
, Polyhedron
*C
)
100 unsigned dim
= C
->NbRays
- 1; /* don't count zero vertex */
101 assert(C
->NbRays
- 1 == C
->Dimension
);
106 for (i
= 0, c
= 0; i
< dim
; ++i
)
107 if (value_zero_p(C
->Ray
[i
][dim
+1])) {
108 for (int j
= 0; j
< dim
; ++j
) {
109 value2zz(C
->Ray
[i
][j
+1], tmp
);
116 static Matrix
* rays(Polyhedron
*C
)
118 unsigned dim
= C
->NbRays
- 1; /* don't count zero vertex */
119 assert(C
->NbRays
- 1 == C
->Dimension
);
121 Matrix
*M
= Matrix_Alloc(dim
+1, dim
+1);
125 for (i
= 0, c
= 0; i
<= dim
&& c
< dim
; ++i
)
126 if (value_zero_p(C
->Ray
[i
][dim
+1])) {
127 Vector_Copy(C
->Ray
[i
] + 1, M
->p
[c
], dim
);
128 value_set_si(M
->p
[c
++][dim
], 0);
131 value_set_si(M
->p
[dim
][dim
], 1);
136 static Matrix
* rays2(Polyhedron
*C
)
138 unsigned dim
= C
->NbRays
- 1; /* don't count zero vertex */
139 assert(C
->NbRays
- 1 == C
->Dimension
);
141 Matrix
*M
= Matrix_Alloc(dim
, dim
);
145 for (i
= 0, c
= 0; i
<= dim
&& c
< dim
; ++i
)
146 if (value_zero_p(C
->Ray
[i
][dim
+1]))
147 Vector_Copy(C
->Ray
[i
] + 1, M
->p
[c
++], dim
);
154 * Returns the largest absolute value in the vector
156 static ZZ
max(vec_ZZ
& v
)
159 for (int i
= 1; i
< v
.length(); ++i
)
169 Rays
= Matrix_Copy(M
);
172 cone(Polyhedron
*C
) {
173 Cone
= Polyhedron_Copy(C
);
179 matrix2zz(Rays
, A
, Rays
->NbRows
- 1, Rays
->NbColumns
- 1);
180 det
= determinant(A
);
187 Vector
* short_vector(vec_ZZ
& lambda
) {
188 Matrix
*M
= Matrix_Copy(Rays
);
189 Matrix
*inv
= Matrix_Alloc(M
->NbRows
, M
->NbColumns
);
190 int ok
= Matrix_Inverse(M
, inv
);
197 matrix2zz(inv
, B
, inv
->NbRows
- 1, inv
->NbColumns
- 1);
198 long r
= LLL(det2
, B
, U
);
202 for (int i
= 1; i
< B
.NumRows(); ++i
) {
214 Vector
*z
= Vector_Alloc(U
[index
].length()+1);
216 zz2values(U
[index
], z
->p
);
217 value_set_si(z
->p
[U
[index
].length()], 0);
221 Polyhedron
*C
= poly();
223 for (i
= 0; i
< C
->NbConstraints
; ++i
) {
224 Inner_Product(z
->p
, C
->Constraint
[i
]+1, z
->Size
-1, &tmp
);
225 if (value_pos_p(tmp
))
228 if (i
== C
->NbConstraints
) {
229 value_set_si(tmp
, -1);
230 Vector_Scale(z
->p
, z
->p
, tmp
, z
->Size
-1);
237 Polyhedron_Free(Cone
);
243 Matrix
*M
= Matrix_Alloc(Rays
->NbRows
+1, Rays
->NbColumns
+1);
244 for (int i
= 0; i
< Rays
->NbRows
; ++i
) {
245 Vector_Copy(Rays
->p
[i
], M
->p
[i
]+1, Rays
->NbColumns
);
246 value_set_si(M
->p
[i
][0], 1);
248 Vector_Set(M
->p
[Rays
->NbRows
]+1, 0, Rays
->NbColumns
-1);
249 value_set_si(M
->p
[Rays
->NbRows
][0], 1);
250 value_set_si(M
->p
[Rays
->NbRows
][Rays
->NbColumns
], 1);
251 Cone
= Rays2Polyhedron(M
, M
->NbRows
+1);
252 assert(Cone
->NbConstraints
== Cone
->NbRays
);
266 dpoly(int d
, ZZ
& degree
, int offset
= 0) {
267 coeff
.SetLength(d
+1);
269 int min
= d
+ offset
;
270 if (degree
< ZZ(INIT_VAL
, min
))
271 min
= to_int(degree
);
273 ZZ c
= ZZ(INIT_VAL
, 1);
276 for (int i
= 1; i
<= min
; ++i
) {
277 c
*= (degree
-i
+ 1);
282 void operator *= (dpoly
& f
) {
283 assert(coeff
.length() == f
.coeff
.length());
285 coeff
= f
.coeff
[0] * coeff
;
286 for (int i
= 1; i
< coeff
.length(); ++i
)
287 for (int j
= 0; i
+j
< coeff
.length(); ++j
)
288 coeff
[i
+j
] += f
.coeff
[i
] * old
[j
];
290 void div(dpoly
& d
, mpq_t count
, ZZ
& sign
) {
291 int len
= coeff
.length();
294 mpq_t
* c
= new mpq_t
[coeff
.length()];
297 for (int i
= 0; i
< len
; ++i
) {
299 zz2value(coeff
[i
], tmp
);
300 mpq_set_z(c
[i
], tmp
);
302 for (int j
= 1; j
<= i
; ++j
) {
303 zz2value(d
.coeff
[j
], tmp
);
304 mpq_set_z(qtmp
, tmp
);
305 mpq_mul(qtmp
, qtmp
, c
[i
-j
]);
306 mpq_sub(c
[i
], c
[i
], qtmp
);
309 zz2value(d
.coeff
[0], tmp
);
310 mpq_set_z(qtmp
, tmp
);
311 mpq_div(c
[i
], c
[i
], qtmp
);
314 mpq_sub(count
, count
, c
[len
-1]);
316 mpq_add(count
, count
, c
[len
-1]);
320 for (int i
= 0; i
< len
; ++i
)
332 dpoly_n(int d
, ZZ
& degree_0
, ZZ
& degree_1
, int offset
= 0) {
336 zz2value(degree_0
, d0
);
337 zz2value(degree_1
, d1
);
338 coeff
= Matrix_Alloc(d
+1, d
+1+1);
339 value_set_si(coeff
->p
[0][0], 1);
340 value_set_si(coeff
->p
[0][d
+1], 1);
341 for (int i
= 1; i
<= d
; ++i
) {
342 value_multiply(coeff
->p
[i
][0], coeff
->p
[i
-1][0], d0
);
343 Vector_Combine(coeff
->p
[i
-1], coeff
->p
[i
-1]+1, coeff
->p
[i
]+1,
345 value_set_si(coeff
->p
[i
][d
+1], i
);
346 value_multiply(coeff
->p
[i
][d
+1], coeff
->p
[i
][d
+1], coeff
->p
[i
-1][d
+1]);
347 value_decrement(d0
, d0
);
352 void div(dpoly
& d
, Vector
*count
, ZZ
& sign
) {
353 int len
= coeff
->NbRows
;
354 Matrix
* c
= Matrix_Alloc(coeff
->NbRows
, coeff
->NbColumns
);
357 for (int i
= 0; i
< len
; ++i
) {
358 Vector_Copy(coeff
->p
[i
], c
->p
[i
], len
+1);
359 for (int j
= 1; j
<= i
; ++j
) {
360 zz2value(d
.coeff
[j
], tmp
);
361 value_multiply(tmp
, tmp
, c
->p
[i
][len
]);
362 value_oppose(tmp
, tmp
);
363 Vector_Combine(c
->p
[i
], c
->p
[i
-j
], c
->p
[i
],
364 c
->p
[i
-j
][len
], tmp
, len
);
365 value_multiply(c
->p
[i
][len
], c
->p
[i
][len
], c
->p
[i
-j
][len
]);
367 zz2value(d
.coeff
[0], tmp
);
368 value_multiply(c
->p
[i
][len
], c
->p
[i
][len
], tmp
);
371 value_set_si(tmp
, -1);
372 Vector_Scale(c
->p
[len
-1], count
->p
, tmp
, len
);
373 value_assign(count
->p
[len
], c
->p
[len
-1][len
]);
375 Vector_Copy(c
->p
[len
-1], count
->p
, len
+1);
376 Vector_Normalize(count
->p
, len
+1);
383 * Barvinok's Decomposition of a simplicial cone
385 * Returns two lists of polyhedra
387 void barvinok_decompose(Polyhedron
*C
, Polyhedron
**ppos
, Polyhedron
**pneg
)
389 Polyhedron
*pos
= *ppos
, *neg
= *pneg
;
390 vector
<cone
*> nonuni
;
391 cone
* c
= new cone(C
);
398 Polyhedron
*p
= Polyhedron_Copy(c
->Cone
);
404 while (!nonuni
.empty()) {
407 Vector
* v
= c
->short_vector(lambda
);
408 for (int i
= 0; i
< c
->Rays
->NbRows
- 1; ++i
) {
411 Matrix
* M
= Matrix_Copy(c
->Rays
);
412 Vector_Copy(v
->p
, M
->p
[i
], v
->Size
);
413 cone
* pc
= new cone(M
);
414 assert (pc
->det
!= 0);
415 if (abs(pc
->det
) > 1) {
416 assert(abs(pc
->det
) < abs(c
->det
));
417 nonuni
.push_back(pc
);
419 Polyhedron
*p
= pc
->poly();
421 if (sign(pc
->det
) == s
) {
440 * Returns a single list of npos "positive" cones followed by nneg
442 * The input cone is freed
444 void decompose(Polyhedron
*cone
, Polyhedron
**parts
, int *npos
, int *nneg
, unsigned MaxRays
)
446 Polyhedron_Polarize(cone
);
447 if (cone
->NbRays
- 1 != cone
->Dimension
) {
448 Polyhedron
*tmp
= cone
;
449 cone
= triangularize_cone(cone
, MaxRays
);
450 Polyhedron_Free(tmp
);
452 Polyhedron
*polpos
= NULL
, *polneg
= NULL
;
453 *npos
= 0; *nneg
= 0;
454 for (Polyhedron
*Polar
= cone
; Polar
; Polar
= Polar
->next
)
455 barvinok_decompose(Polar
, &polpos
, &polneg
);
458 for (Polyhedron
*i
= polpos
; i
; i
= i
->next
) {
459 Polyhedron_Polarize(i
);
463 for (Polyhedron
*i
= polneg
; i
; i
= i
->next
) {
464 Polyhedron_Polarize(i
);
475 const int MAX_TRY
=10;
477 * Searches for a vector that is not othogonal to any
478 * of the rays in rays.
480 static void nonorthog(mat_ZZ
& rays
, vec_ZZ
& lambda
)
482 int dim
= rays
.NumCols();
484 lambda
.SetLength(dim
);
485 for (int i
= 2; !found
&& i
<= 50*dim
; i
+=4) {
486 for (int j
= 0; j
< MAX_TRY
; ++j
) {
487 for (int k
= 0; k
< dim
; ++k
) {
488 int r
= random_int(i
)+2;
489 int v
= (2*(r
%2)-1) * (r
>> 1);
493 for (; k
< rays
.NumRows(); ++k
)
494 if (lambda
* rays
[k
] == 0)
496 if (k
== rays
.NumRows()) {
505 static void add_rays(mat_ZZ
& rays
, Polyhedron
*i
, int *r
)
507 unsigned dim
= i
->Dimension
;
508 for (int k
= 0; k
< i
->NbRays
; ++k
) {
509 if (!value_zero_p(i
->Ray
[k
][dim
+1]))
511 values2zz(i
->Ray
[k
]+1, rays
[(*r
)++], dim
);
515 void lattice_point(Value
* values
, Polyhedron
*i
, vec_ZZ
& lambda
, ZZ
& num
)
518 unsigned dim
= i
->Dimension
;
519 if(!value_one_p(values
[dim
])) {
520 Matrix
* Rays
= rays(i
);
521 Matrix
*inv
= Matrix_Alloc(Rays
->NbRows
, Rays
->NbColumns
);
522 int ok
= Matrix_Inverse(Rays
, inv
);
526 Vector
*lambda
= Vector_Alloc(dim
+1);
527 Vector_Matrix_Product(values
, inv
, lambda
->p
);
529 for (int j
= 0; j
< dim
; ++j
)
530 mpz_cdiv_q(lambda
->p
[j
], lambda
->p
[j
], lambda
->p
[dim
]);
531 value_set_si(lambda
->p
[dim
], 1);
532 Vector
*A
= Vector_Alloc(dim
+1);
533 Vector_Matrix_Product(lambda
->p
, Rays
, A
->p
);
536 values2zz(A
->p
, vertex
, dim
);
539 values2zz(values
, vertex
, dim
);
541 num
= vertex
* lambda
;
544 static evalue
*term(int param
, ZZ
& c
, Value
*den
= NULL
)
546 evalue
*EP
= new evalue();
548 value_set_si(EP
->d
,0);
549 EP
->x
.p
= new_enode(polynomial
, 2, param
+ 1);
550 evalue_set_si(&EP
->x
.p
->arr
[0], 0, 1);
551 value_init(EP
->x
.p
->arr
[1].x
.n
);
553 value_set_si(EP
->x
.p
->arr
[1].d
, 1);
555 value_assign(EP
->x
.p
->arr
[1].d
, *den
);
556 zz2value(c
, EP
->x
.p
->arr
[1].x
.n
);
560 static void vertex_period(
561 Polyhedron
*i
, vec_ZZ
& lambda
, Matrix
*T
,
562 Value lcm
, int p
, Vector
*val
,
563 evalue
*E
, evalue
* ev
,
566 unsigned nparam
= T
->NbRows
- 1;
567 unsigned dim
= i
->Dimension
;
573 Vector
* values
= Vector_Alloc(dim
+ 1);
574 Vector_Matrix_Product(val
->p
, T
, values
->p
);
575 value_assign(values
->p
[dim
], lcm
);
576 lattice_point(values
->p
, i
, lambda
, num
);
581 zz2value(num
, ev
->x
.n
);
582 value_assign(ev
->d
, lcm
);
589 values2zz(T
->p
[p
], vertex
, dim
);
590 nump
= vertex
* lambda
;
591 if (First_Non_Zero(val
->p
, p
) == -1) {
592 value_assign(tmp
, lcm
);
593 evalue
*ET
= term(p
, nump
, &tmp
);
595 free_evalue_refs(ET
);
599 value_assign(tmp
, lcm
);
600 if (First_Non_Zero(T
->p
[p
], dim
) != -1)
601 Vector_Gcd(T
->p
[p
], dim
, &tmp
);
603 if (value_lt(tmp
, lcm
)) {
606 value_division(tmp
, lcm
, tmp
);
607 value_set_si(ev
->d
, 0);
608 ev
->x
.p
= new_enode(periodic
, VALUE_TO_INT(tmp
), p
+1);
609 value2zz(tmp
, count
);
611 value_decrement(tmp
, tmp
);
613 ZZ new_offset
= offset
- count
* nump
;
614 value_assign(val
->p
[p
], tmp
);
615 vertex_period(i
, lambda
, T
, lcm
, p
+1, val
, E
,
616 &ev
->x
.p
->arr
[VALUE_TO_INT(tmp
)], new_offset
);
617 } while (value_pos_p(tmp
));
619 vertex_period(i
, lambda
, T
, lcm
, p
+1, val
, E
, ev
, offset
);
623 static void mask_r(Matrix
*f
, int nr
, Vector
*lcm
, int p
, Vector
*val
, evalue
*ev
)
625 unsigned nparam
= lcm
->Size
;
628 Vector
* prod
= Vector_Alloc(f
->NbRows
);
629 Matrix_Vector_Product(f
, val
->p
, prod
->p
);
631 for (int i
= 0; i
< nr
; ++i
) {
632 value_modulus(prod
->p
[i
], prod
->p
[i
], f
->p
[i
][nparam
+1]);
633 isint
&= value_zero_p(prod
->p
[i
]);
635 value_set_si(ev
->d
, 1);
637 value_set_si(ev
->x
.n
, isint
);
644 if (value_one_p(lcm
->p
[p
]))
645 mask_r(f
, nr
, lcm
, p
+1, val
, ev
);
647 value_assign(tmp
, lcm
->p
[p
]);
648 value_set_si(ev
->d
, 0);
649 ev
->x
.p
= new_enode(periodic
, VALUE_TO_INT(tmp
), p
+1);
651 value_decrement(tmp
, tmp
);
652 value_assign(val
->p
[p
], tmp
);
653 mask_r(f
, nr
, lcm
, p
+1, val
, &ev
->x
.p
->arr
[VALUE_TO_INT(tmp
)]);
654 } while (value_pos_p(tmp
));
659 static evalue
*multi_monom(vec_ZZ
& p
)
661 evalue
*X
= new evalue();
664 unsigned nparam
= p
.length()-1;
665 zz2value(p
[nparam
], X
->x
.n
);
666 value_set_si(X
->d
, 1);
667 for (int i
= 0; i
< nparam
; ++i
) {
670 evalue
*T
= term(i
, p
[i
]);
679 static void mask(Matrix
*f
, evalue
*factor
)
681 int nr
= f
->NbRows
, nc
= f
->NbColumns
;
684 for (n
= 0; n
< nr
&& value_notzero_p(f
->p
[n
][nc
-1]); ++n
)
685 if (value_notone_p(f
->p
[n
][nc
-1]) &&
686 value_notmone_p(f
->p
[n
][nc
-1]))
694 for (n
= 0; n
< nr
; ++n
) {
695 if (value_one_p(f
->p
[n
][nc
-1]) ||
696 value_mone_p(f
->p
[n
][nc
-1]))
699 value_set_si(EP
.d
, 0);
700 EP
.x
.p
= new_enode(relation
, 2, 0);
701 value_clear(EP
.x
.p
->arr
[1].d
);
702 EP
.x
.p
->arr
[1] = *factor
;
703 evalue
*ev
= &EP
.x
.p
->arr
[0];
704 value_set_si(ev
->d
, 0);
705 ev
->x
.p
= new_enode(modulo
, 3, VALUE_TO_INT(f
->p
[n
][nc
-1]));
706 evalue_set_si(&ev
->x
.p
->arr
[1], 0, 1);
707 evalue_set_si(&ev
->x
.p
->arr
[2], 1, 1);
709 values2zz(f
->p
[n
], row
, nc
-1);
710 evalue
*E
= multi_monom(row
);
711 value_clear(ev
->x
.p
->arr
[0].d
);
712 ev
->x
.p
->arr
[0] = *E
;
721 static void mask(Matrix
*f
, evalue
*factor
)
723 int nr
= f
->NbRows
, nc
= f
->NbColumns
;
726 for (n
= 0; n
< nr
&& value_notzero_p(f
->p
[n
][nc
-1]); ++n
)
727 if (value_notone_p(f
->p
[n
][nc
-1]) &&
728 value_notmone_p(f
->p
[n
][nc
-1]))
736 unsigned np
= nc
- 2;
737 Vector
*lcm
= Vector_Alloc(np
);
738 Vector
*val
= Vector_Alloc(nc
);
739 Vector_Set(val
->p
, 0, nc
);
740 value_set_si(val
->p
[np
], 1);
741 Vector_Set(lcm
->p
, 1, np
);
742 for (n
= 0; n
< nr
; ++n
) {
743 if (value_one_p(f
->p
[n
][nc
-1]) ||
744 value_mone_p(f
->p
[n
][nc
-1]))
746 for (int j
= 0; j
< np
; ++j
)
747 if (value_notzero_p(f
->p
[n
][j
])) {
748 Gcd(f
->p
[n
][j
], f
->p
[n
][nc
-1], &tmp
);
749 value_division(tmp
, f
->p
[n
][nc
-1], tmp
);
750 value_lcm(tmp
, lcm
->p
[j
], &lcm
->p
[j
]);
755 mask_r(f
, nr
, lcm
, 0, val
, &EP
);
760 free_evalue_refs(&EP
);
771 bool mod_needed(Polyhedron
*PD
, vec_ZZ
& num
, Value d
, evalue
*E
)
774 int len
= num
.length();
775 Matrix
*T
= Matrix_Alloc(2, len
);
776 zz2values(num
, T
->p
[0]);
777 value_set_si(T
->p
[1][len
-1], 1);
778 Polyhedron
*I
= Polyhedron_Image(PD
, T
, PD
->NbConstraints
);
782 for (i
= 0; i
< I
->NbRays
; ++i
)
783 if (value_zero_p(I
->Ray
[i
][2])) {
791 for (i
= 0; i
< I
->NbConstraints
; ++i
) {
792 value_oppose(I
->Constraint
[i
][2], I
->Constraint
[i
][2]);
793 if (value_pos_p(I
->Constraint
[i
][1]))
794 mpz_cdiv_q(min
, I
->Constraint
[i
][2], I
->Constraint
[i
][1]);
796 mpz_fdiv_q(max
, I
->Constraint
[i
][2], I
->Constraint
[i
][1]);
798 mpz_fdiv_q(min
, min
, d
);
799 mpz_fdiv_q(max
, max
, d
);
800 if (value_eq(min
, max
)) {
802 value_oppose(min
, min
);
805 value_set_si(EV
.d
, 1);
807 value_multiply(EV
.x
.n
, min
, d
);
809 free_evalue_refs(&EV
);
817 void ceil_mod(Value
*coef
, int len
, Value d
, ZZ
& f
, evalue
*EP
, Polyhedron
*PD
)
823 value_set_si(mone
, -1);
827 Vector_Gcd(coef
, len
, &gcd
);
829 Vector_AntiScale(coef
, coef
, gcd
, len
);
830 Vector_Scale(coef
, coef
, mone
, len
);
832 value_division(gcd
, d
, gcd
);
835 if (value_one_p(gcd
))
838 values2zz(coef
, num
, len
);
841 for (j
= 0; j
< len
; ++j
)
843 for (j
= 0; j
< len
; ++j
)
851 evalue_set_si(&tmp
, 0, 1);
853 if (j
< len
-1 && num
[j
] > g
/2) {
854 for (int k
= j
; k
< len
-1; ++k
)
857 num
[len
-1] = g
- 1 - num
[len
-1];
858 value_assign(tmp
.d
, gcd
);
860 zz2value(t
, tmp
.x
.n
);
866 ZZ t
= num
[len
-1] * f
;
867 zz2value(t
, tmp
.x
.n
);
868 value_assign(tmp
.d
, gcd
);
871 evalue
*E
= multi_monom(num
);
875 if (PD
&& !mod_needed(PD
, num
, gcd
, E
)) {
878 value_assign(EV
.d
, gcd
);
882 value_set_si(EV
.d
, 0);
883 EV
.x
.p
= new_enode(modulo
, 3, VALUE_TO_INT(gcd
));
884 evalue_copy(&EV
.x
.p
->arr
[0], E
);
885 evalue_set_si(&EV
.x
.p
->arr
[1], 0, 1);
886 value_init(EV
.x
.p
->arr
[2].x
.n
);
887 zz2value(f
, EV
.x
.p
->arr
[2].x
.n
);
888 value_assign(EV
.x
.p
->arr
[2].d
, gcd
);
893 free_evalue_refs(&EV
);
898 free_evalue_refs(&tmp
);
905 evalue
* ceil3(Value
*coef
, int len
, Value d
)
907 Vector
*val
= Vector_Alloc(len
);
912 Vector_Scale(coef
, val
->p
, t
, len
);
913 value_absolute(t
, d
);
916 values2zz(val
->p
, num
, len
);
917 evalue
*EP
= multi_monom(num
);
922 value_set_si(tmp
.x
.n
, 1);
923 value_assign(tmp
.d
, t
);
929 ceil_mod(val
->p
, len
, t
, one
, EP
, NULL
);
932 /* copy EP to malloc'ed evalue */
938 free_evalue_refs(&tmp
);
945 evalue
* lattice_point(
946 Polyhedron
*i
, vec_ZZ
& lambda
, Matrix
*W
, Value lcm
, Polyhedron
*PD
)
948 unsigned nparam
= W
->NbColumns
- 1;
950 Matrix
* Rays
= rays2(i
);
951 Matrix
*T
= Transpose(Rays
);
952 Matrix
*T2
= Matrix_Copy(T
);
953 Matrix
*inv
= Matrix_Alloc(T2
->NbRows
, T2
->NbColumns
);
954 int ok
= Matrix_Inverse(T2
, inv
);
959 matrix2zz(W
, vertex
, W
->NbRows
, W
->NbColumns
);
962 num
= lambda
* vertex
;
964 evalue
*EP
= multi_monom(num
);
969 value_set_si(tmp
.x
.n
, 1);
970 value_assign(tmp
.d
, lcm
);
974 Matrix
*L
= Matrix_Alloc(inv
->NbRows
, W
->NbColumns
);
975 Matrix_Product(inv
, W
, L
);
978 matrix2zz(T
, RT
, T
->NbRows
, T
->NbColumns
);
981 vec_ZZ p
= lambda
* RT
;
983 for (int i
= 0; i
< L
->NbRows
; ++i
) {
984 ceil_mod(L
->p
[i
], nparam
+1, lcm
, p
[i
], EP
, PD
);
990 free_evalue_refs(&tmp
);
994 evalue
* lattice_point(
995 Polyhedron
*i
, vec_ZZ
& lambda
, Matrix
*W
, Value lcm
, Polyhedron
*PD
)
997 Matrix
*T
= Transpose(W
);
998 unsigned nparam
= T
->NbRows
- 1;
1000 evalue
*EP
= new evalue();
1002 evalue_set_si(EP
, 0, 1);
1005 Vector
*val
= Vector_Alloc(nparam
+1);
1006 value_set_si(val
->p
[nparam
], 1);
1007 ZZ
offset(INIT_VAL
, 0);
1009 vertex_period(i
, lambda
, T
, lcm
, 0, val
, EP
, &ev
, offset
);
1012 free_evalue_refs(&ev
);
1023 Param_Vertices
* V
, Polyhedron
*i
, vec_ZZ
& lambda
, term_info
* term
,
1026 unsigned nparam
= V
->Vertex
->NbColumns
- 2;
1027 unsigned dim
= i
->Dimension
;
1029 vertex
.SetDims(V
->Vertex
->NbRows
, nparam
+1);
1033 value_set_si(lcm
, 1);
1034 for (int j
= 0; j
< V
->Vertex
->NbRows
; ++j
) {
1035 value_lcm(lcm
, V
->Vertex
->p
[j
][nparam
+1], &lcm
);
1037 if (value_notone_p(lcm
)) {
1038 Matrix
* mv
= Matrix_Alloc(dim
, nparam
+1);
1039 for (int j
= 0 ; j
< dim
; ++j
) {
1040 value_division(tmp
, lcm
, V
->Vertex
->p
[j
][nparam
+1]);
1041 Vector_Scale(V
->Vertex
->p
[j
], mv
->p
[j
], tmp
, nparam
+1);
1044 term
->E
= lattice_point(i
, lambda
, mv
, lcm
, PD
);
1052 for (int i
= 0; i
< V
->Vertex
->NbRows
; ++i
) {
1053 assert(value_one_p(V
->Vertex
->p
[i
][nparam
+1])); // for now
1054 values2zz(V
->Vertex
->p
[i
], vertex
[i
], nparam
+1);
1058 num
= lambda
* vertex
;
1062 for (int j
= 0; j
< nparam
; ++j
)
1068 term
->E
= multi_monom(num
);
1072 term
->constant
= num
[nparam
];
1075 term
->coeff
= num
[p
];
1082 void normalize(Polyhedron
*i
, vec_ZZ
& lambda
, ZZ
& sign
, ZZ
& num
, vec_ZZ
& den
)
1084 unsigned dim
= i
->Dimension
;
1088 rays
.SetDims(dim
, dim
);
1089 add_rays(rays
, i
, &r
);
1090 den
= rays
* lambda
;
1093 for (int j
= 0; j
< den
.length(); ++j
) {
1097 den
[j
] = abs(den
[j
]);
1105 void barvinok_count(Polyhedron
*P
, Value
* result
, unsigned NbMaxCons
)
1107 Polyhedron
** vcone
;
1110 sign
.SetLength(ncone
);
1118 value_set_si(*result
, 0);
1122 for (; r
< P
->NbRays
; ++r
)
1123 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1]))
1125 if (P
->NbBid
!=0 || r
< P
->NbRays
) {
1126 value_set_si(*result
, -1);
1130 P
= remove_equalities(P
);
1133 value_set_si(*result
, 0);
1139 value_set_si(factor
, 1);
1140 Q
= Polyhedron_Reduce(P
, &factor
);
1147 if (P
->Dimension
== 0) {
1148 value_assign(*result
, factor
);
1151 value_clear(factor
);
1156 vcone
= new (Polyhedron
*)[P
->NbRays
];
1158 for (int j
= 0; j
< P
->NbRays
; ++j
) {
1160 Polyhedron
*C
= supporting_cone(P
, j
);
1161 decompose(C
, &vcone
[j
], &npos
, &nneg
, NbMaxCons
);
1162 ncone
+= npos
+ nneg
;
1163 sign
.SetLength(ncone
);
1164 for (int k
= 0; k
< npos
; ++k
)
1165 sign
[ncone
-nneg
-k
-1] = 1;
1166 for (int k
= 0; k
< nneg
; ++k
)
1167 sign
[ncone
-k
-1] = -1;
1171 rays
.SetDims(ncone
* dim
, dim
);
1173 for (int j
= 0; j
< P
->NbRays
; ++j
) {
1174 for (Polyhedron
*i
= vcone
[j
]; i
; i
= i
->next
) {
1175 assert(i
->NbRays
-1 == dim
);
1176 add_rays(rays
, i
, &r
);
1180 nonorthog(rays
, lambda
);
1184 num
.SetLength(ncone
);
1185 den
.SetDims(ncone
,dim
);
1188 for (int j
= 0; j
< P
->NbRays
; ++j
) {
1189 for (Polyhedron
*i
= vcone
[j
]; i
; i
= i
->next
) {
1190 lattice_point(P
->Ray
[j
]+1, i
, lambda
, num
[f
]);
1191 normalize(i
, lambda
, sign
[f
], num
[f
], den
[f
]);
1196 for (int j
= 1; j
< num
.length(); ++j
)
1199 for (int j
= 0; j
< num
.length(); ++j
)
1205 for (int j
= 0; j
< P
->NbRays
; ++j
) {
1206 for (Polyhedron
*i
= vcone
[j
]; i
; i
= i
->next
) {
1207 dpoly
d(dim
, num
[f
]);
1208 dpoly
n(dim
, den
[f
][0], 1);
1209 for (int k
= 1; k
< dim
; ++k
) {
1210 dpoly
fact(dim
, den
[f
][k
], 1);
1213 d
.div(n
, count
, sign
[f
]);
1217 assert(value_one_p(&count
[0]._mp_den
));
1218 value_multiply(*result
, &count
[0]._mp_num
, factor
);
1221 for (int j
= 0; j
< P
->NbRays
; ++j
)
1222 Domain_Free(vcone
[j
]);
1228 value_clear(factor
);
1231 static void uni_polynom(int param
, Vector
*c
, evalue
*EP
)
1233 unsigned dim
= c
->Size
-2;
1235 value_set_si(EP
->d
,0);
1236 EP
->x
.p
= new_enode(polynomial
, dim
+1, param
+1);
1237 for (int j
= 0; j
<= dim
; ++j
)
1238 evalue_set(&EP
->x
.p
->arr
[j
], c
->p
[j
], c
->p
[dim
+1]);
1241 static void multi_polynom(Vector
*c
, evalue
* X
, evalue
*EP
)
1243 unsigned dim
= c
->Size
-2;
1247 evalue_set(&EC
, c
->p
[dim
], c
->p
[dim
+1]);
1250 evalue_set(EP
, c
->p
[dim
], c
->p
[dim
+1]);
1252 for (int i
= dim
-1; i
>= 0; --i
) {
1254 value_assign(EC
.x
.n
, c
->p
[i
]);
1257 free_evalue_refs(&EC
);
1261 Polyhedron
*unfringe (Polyhedron
*P
, unsigned MaxRays
)
1263 int len
= P
->Dimension
+2;
1264 Polyhedron
*T
, *R
= P
;
1268 Polyhedron_Print(stdout
, P_VALUE_FMT
, P
);
1269 Vector
*row
= Vector_Alloc(len
);
1270 value_set_si(row
->p
[0], 1);
1271 for (int r
= P
->NbEq
; r
< P
->NbConstraints
; ++r
) {
1272 Vector_Gcd(P
->Constraint
[r
]+1, len
- 2, &g
);
1273 if (value_notone_p(g
)) {
1274 value_print(stdout
, P_VALUE_FMT
, P
->Constraint
[r
][len
-1]);
1276 value_print(stdout
, P_VALUE_FMT
, g
);
1278 Vector_AntiScale(P
->Constraint
[r
]+1, row
->p
+1, g
, len
-2);
1279 mpz_fdiv_q(row
->p
[len
-1], P
->Constraint
[r
][len
-1], g
);
1281 Vector_Print(stdout
, P_VALUE_FMT
, row
);
1283 R
= AddConstraints(row
->p
, 1, R
, MaxRays
);
1288 Matrix
*M
= Matrix_Alloc(2, len
-1);
1289 value_set_si(M
->p
[1][len
-2], 1);
1290 for (int v
= 0; v
< P
->Dimension
; ++v
) {
1291 value_set_si(M
->p
[0][v
], 1);
1292 Polyhedron
*I
= Polyhedron_Image(P
, M
, 2+1);
1293 Polyhedron_Print(stdout
, P_VALUE_FMT
, I
);
1294 value_set_si(M
->p
[0][v
], 0);
1295 for (int r
= 0; r
< I
->NbConstraints
; ++r
) {
1296 if (value_zero_p(I
->Constraint
[r
][0]))
1298 if (value_zero_p(I
->Constraint
[r
][1]))
1300 if (value_one_p(I
->Constraint
[r
][1]))
1302 if (value_mone_p(I
->Constraint
[r
][1]))
1304 value_absolute(g
, I
->Constraint
[r
][1]);
1305 Vector_Set(row
->p
+1, 0, len
-2);
1306 value_division(row
->p
[1+v
], I
->Constraint
[r
][1], g
);
1307 mpz_fdiv_q(row
->p
[len
-1], I
->Constraint
[r
][2], g
);
1309 Vector_Print(stdout
, P_VALUE_FMT
, row
);
1311 R
= AddConstraints(row
->p
, 1, R
, MaxRays
);
1317 Polyhedron_Print(stdout
, P_VALUE_FMT
, R
);
1322 evalue
* barvinok_enumerate_ev(Polyhedron
*P
, Polyhedron
* C
, unsigned MaxRays
)
1324 //P = unfringe(P, MaxRays);
1325 Polyhedron
*CEq
= NULL
, *rVD
, *pVD
, *CA
;
1327 Param_Polyhedron
*PP
= NULL
;
1328 Param_Domain
*D
, *next
;
1331 unsigned nparam
= C
->Dimension
;
1332 evalue
*eres
= new evalue
;
1333 value_init(eres
->d
);
1334 value_set_si(eres
->d
, 0);
1337 value_init(factor
.d
);
1338 evalue_set_si(&factor
, 1, 1);
1340 CA
= align_context(C
, P
->Dimension
, MaxRays
);
1341 P
= DomainIntersection(P
, CA
, MaxRays
);
1342 Polyhedron_Free(CA
);
1344 if (C
->Dimension
== 0 || emptyQ(P
)) {
1346 eres
->x
.p
= new_enode(partition
, 2, -1);
1347 EVALUE_SET_DOMAIN(eres
->x
.p
->arr
[0], CEq
? CEq
: Polyhedron_Copy(C
));
1348 value_set_si(eres
->x
.p
->arr
[1].d
, 1);
1349 value_init(eres
->x
.p
->arr
[1].x
.n
);
1351 value_set_si(eres
->x
.p
->arr
[1].x
.n
, 0);
1353 barvinok_count(P
, &eres
->x
.p
->arr
[1].x
.n
, MaxRays
);
1354 emul(&factor
, eres
);
1356 free_evalue_refs(&factor
);
1361 Param_Polyhedron_Free(PP
);
1365 for (r
= 0; r
< P
->NbRays
; ++r
)
1366 if (value_zero_p(P
->Ray
[r
][0]) ||
1367 value_zero_p(P
->Ray
[r
][P
->Dimension
+1])) {
1369 for (i
= P
->Dimension
- nparam
; i
< P
->Dimension
; ++i
)
1370 if (value_notzero_p(P
->Ray
[r
][i
+1]))
1372 if (i
>= P
->Dimension
)
1380 P
= remove_equalities_p(P
, P
->Dimension
-nparam
, &f
);
1383 if (P
->Dimension
== nparam
) {
1385 P
= Universe_Polyhedron(0);
1390 Polyhedron
*Q
= ParamPolyhedron_Reduce(P
, P
->Dimension
-nparam
, &factor
);
1393 if (Q
->Dimension
== nparam
) {
1395 P
= Universe_Polyhedron(0);
1400 Polyhedron
*oldP
= P
;
1401 PP
= Polyhedron2Param_SimplifiedDomain(&P
,C
,MaxRays
,&CEq
,&CT
);
1403 Polyhedron_Free(oldP
);
1405 if (isIdentity(CT
)) {
1409 assert(CT
->NbRows
!= CT
->NbColumns
);
1410 if (CT
->NbRows
== 1) // no more parameters
1412 nparam
= CT
->NbRows
- 1;
1415 unsigned dim
= P
->Dimension
- nparam
;
1416 Polyhedron
** vcone
= new (Polyhedron
*)[PP
->nbV
];
1417 int * npos
= new int[PP
->nbV
];
1418 int * nneg
= new int[PP
->nbV
];
1422 for (i
= 0, V
= PP
->V
; V
; ++i
, V
= V
->next
) {
1423 Polyhedron
*C
= supporting_cone_p(P
, V
);
1424 decompose(C
, &vcone
[i
], &npos
[i
], &nneg
[i
], MaxRays
);
1427 Vector
*c
= Vector_Alloc(dim
+2);
1430 for (nd
= 0, D
=PP
->D
; D
; ++nd
, D
=D
->next
);
1431 struct section
{ Polyhedron
* D
; evalue E
; };
1432 section
*s
= new section
[nd
];
1434 for(nd
= 0, D
=PP
->D
; D
; D
=next
) {
1437 pVD
= rVD
= D
->Domain
;
1441 Dt
= CT
? DomainPreimage(D
->Domain
,CT
,MaxRays
) : D
->Domain
;
1442 rVD
= DomainIntersection(Dt
,CEq
,MaxRays
);
1444 /* if rVD is empty or too small in geometric dimension */
1445 if(!rVD
|| emptyQ(rVD
) ||
1446 (rVD
->Dimension
-rVD
->NbEq
< Dt
->Dimension
-Dt
->NbEq
-CEq
->NbEq
)) {
1451 continue; /* empty validity domain */
1455 pVD
= CT
? DomainImage(rVD
,CT
,MaxRays
) : rVD
;
1456 for (Param_Domain
*D2
= D
->next
; D2
; D2
=D2
->next
) {
1457 Polyhedron
*T
= D2
->Domain
;
1458 D2
->Domain
= DomainDifference(D2
->Domain
, D
->Domain
, MaxRays
);
1463 sign
.SetLength(ncone
);
1464 FORALL_PVertex_in_ParamPolyhedron(V
,D
,PP
) // _i is internal counter
1465 ncone
+= npos
[_i
] + nneg
[_i
];
1466 sign
.SetLength(ncone
);
1467 for (int k
= 0; k
< npos
[_i
]; ++k
)
1468 sign
[ncone
-nneg
[_i
]-k
-1] = 1;
1469 for (int k
= 0; k
< nneg
[_i
]; ++k
)
1470 sign
[ncone
-k
-1] = -1;
1471 END_FORALL_PVertex_in_ParamPolyhedron
;
1474 rays
.SetDims(ncone
* dim
, dim
);
1476 FORALL_PVertex_in_ParamPolyhedron(V
,D
,PP
) // _i is internal counter
1477 for (Polyhedron
*i
= vcone
[_i
]; i
; i
= i
->next
) {
1478 assert(i
->NbRays
-1 == dim
);
1479 add_rays(rays
, i
, &r
);
1481 END_FORALL_PVertex_in_ParamPolyhedron
;
1483 nonorthog(rays
, lambda
);
1486 den
.SetDims(ncone
,dim
);
1487 term_info
*num
= new term_info
[ncone
];
1490 FORALL_PVertex_in_ParamPolyhedron(V
,D
,PP
)
1491 for (Polyhedron
*i
= vcone
[_i
]; i
; i
= i
->next
) {
1492 lattice_point(V
, i
, lambda
, &num
[f
], pVD
);
1493 normalize(i
, lambda
, sign
[f
], num
[f
].constant
, den
[f
]);
1496 END_FORALL_PVertex_in_ParamPolyhedron
;
1497 ZZ min
= num
[0].constant
;
1498 for (int j
= 1; j
< ncone
; ++j
)
1499 if (num
[j
].constant
< min
)
1500 min
= num
[j
].constant
;
1501 for (int j
= 0; j
< ncone
; ++j
)
1502 num
[j
].constant
-= min
;
1504 value_init(s
[nd
].E
.d
);
1505 evalue_set_si(&s
[nd
].E
, 0, 1);
1508 FORALL_PVertex_in_ParamPolyhedron(V
,D
,PP
)
1509 for (Polyhedron
*i
= vcone
[_i
]; i
; i
= i
->next
) {
1510 dpoly
n(dim
, den
[f
][0], 1);
1511 for (int k
= 1; k
< dim
; ++k
) {
1512 dpoly
fact(dim
, den
[f
][k
], 1);
1515 if (num
[f
].E
!= NULL
) {
1516 ZZ
one(INIT_VAL
, 1);
1517 dpoly_n
d(dim
, num
[f
].constant
, one
);
1518 d
.div(n
, c
, sign
[f
]);
1520 multi_polynom(c
, num
[f
].E
, &EV
);
1521 eadd(&EV
, &s
[nd
].E
);
1522 free_evalue_refs(&EV
);
1523 free_evalue_refs(num
[f
].E
);
1525 } else if (num
[f
].pos
!= -1) {
1526 dpoly_n
d(dim
, num
[f
].constant
, num
[f
].coeff
);
1527 d
.div(n
, c
, sign
[f
]);
1529 uni_polynom(num
[f
].pos
, c
, &EV
);
1530 eadd(&EV
, &s
[nd
].E
);
1531 free_evalue_refs(&EV
);
1533 mpq_set_si(count
, 0, 1);
1534 dpoly
d(dim
, num
[f
].constant
);
1535 d
.div(n
, count
, sign
[f
]);
1538 evalue_set(&EV
, &count
[0]._mp_num
, &count
[0]._mp_den
);
1539 eadd(&EV
, &s
[nd
].E
);
1540 free_evalue_refs(&EV
);
1544 END_FORALL_PVertex_in_ParamPolyhedron
;
1550 addeliminatedparams_evalue(&s
[nd
].E
, CT
);
1551 emul(&factor
, &s
[nd
].E
);
1552 reduce_evalue(&s
[nd
].E
);
1559 eres
->x
.p
= new_enode(partition
, 2*nd
, -1);
1560 for (int j
= 0; j
< nd
; ++j
) {
1561 EVALUE_SET_DOMAIN(eres
->x
.p
->arr
[2*j
], s
[j
].D
);
1562 eres
->x
.p
->arr
[2*j
+1] = s
[j
].E
;
1568 for (int j
= 0; j
< PP
->nbV
; ++j
)
1569 Domain_Free(vcone
[j
]);
1575 Polyhedron_Free(CEq
);
1580 Enumeration
* barvinok_enumerate(Polyhedron
*P
, Polyhedron
* C
, unsigned MaxRays
)
1582 Enumeration
*en
, *res
= NULL
;
1583 evalue
*EP
= barvinok_enumerate_ev(P
, C
, MaxRays
);
1584 for (int i
= 0; i
< EP
->x
.p
->size
/2; ++i
) {
1585 en
= (Enumeration
*)malloc(sizeof(Enumeration
));
1588 res
->ValidityDomain
= EVALUE_DOMAIN(EP
->x
.p
->arr
[2*i
]);
1589 value_clear(EP
->x
.p
->arr
[2*i
].d
);
1590 res
->EP
= EP
->x
.p
->arr
[2*i
+1];
1598 static void SwapColumns(Value
**V
, int n
, int i
, int j
)
1600 for (int r
= 0; r
< n
; ++r
)
1601 value_swap(V
[r
][i
], V
[r
][j
]);
1604 static void SwapColumns(Polyhedron
*P
, int i
, int j
)
1606 SwapColumns(P
->Constraint
, P
->NbConstraints
, i
, j
);
1607 SwapColumns(P
->Ray
, P
->NbRays
, i
, j
);
1613 INDEPENDENT
= 1 << 2,
1616 evalue
* barvinok_enumerate_e(Polyhedron
*P
,
1617 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
1620 Polyhedron
*U
= Universe_Polyhedron(nparam
);
1621 evalue
*EP
= barvinok_enumerate_ev(P
, U
, MaxRays
);
1622 //char *param_name[] = {"P", "Q", "R", "S", "T" };
1623 //print_evalue(stdout, EP, param_name);
1628 int nvar
= P
->Dimension
- exist
- nparam
;
1629 int len
= P
->Dimension
+ 2;
1631 //printf("%d %d %d\n", nvar, exist, nparam);
1634 for (r
= 0; r
< P
->NbEq
; ++r
)
1635 if (First_Non_Zero(P
->Constraint
[r
]+1+nvar
, exist
) != -1)
1638 Vector
*row
= Vector_Alloc(exist
);
1639 Vector_Copy(P
->Constraint
[r
]+1+nvar
, row
->p
, exist
);
1640 Matrix
*M
= unimodular_complete(row
);
1641 Matrix
*M2
= Matrix_Alloc(P
->Dimension
+1, P
->Dimension
+1);
1642 for (r
= 0; r
< nvar
; ++r
)
1643 value_set_si(M2
->p
[r
][r
], 1);
1644 for ( ; r
< nvar
+exist
; ++r
)
1645 Vector_Copy(M
->p
[r
-nvar
], M2
->p
[r
]+nvar
, exist
);
1646 for ( ; r
< P
->Dimension
+1; ++r
)
1647 value_set_si(M2
->p
[r
][r
], 1);
1648 Polyhedron
*T
= Polyhedron_Image(P
, M2
, MaxRays
);
1649 evalue
*EP
= barvinok_enumerate_e(T
, exist
-1, nparam
, MaxRays
);
1657 Vector
*row
= Vector_Alloc(len
);
1658 value_set_si(row
->p
[0], 1);
1663 enum constraint info
[exist
];
1664 for (int i
= 0; i
< exist
; ++i
) {
1666 for (int l
= P
->NbEq
; l
< P
->NbConstraints
; ++l
) {
1667 if (value_negz_p(P
->Constraint
[l
][nvar
+i
+1]))
1669 for (int u
= P
->NbEq
; u
< P
->NbConstraints
; ++u
) {
1670 if (value_posz_p(P
->Constraint
[u
][nvar
+i
+1]))
1672 value_oppose(f
, P
->Constraint
[u
][nvar
+i
+1]);
1673 Vector_Combine(P
->Constraint
[l
]+1, P
->Constraint
[u
]+1, row
->p
+1,
1674 f
, P
->Constraint
[l
][nvar
+i
+1], len
-1);
1675 if (!(info
[i
] & INDEPENDENT
)) {
1677 for (j
= 0; j
< exist
; ++j
)
1678 if (j
!= i
&& value_notzero_p(row
->p
[nvar
+j
+1]))
1681 //printf("independent: i: %d, l: %d, u: %d\n", i, l, u);
1682 info
[i
] = (constraint
)(info
[i
] | INDEPENDENT
);
1685 if (info
[i
] & ALL_POS
) {
1686 value_addto(row
->p
[len
-1], row
->p
[len
-1],
1687 P
->Constraint
[l
][nvar
+i
+1]);
1688 value_addto(row
->p
[len
-1], row
->p
[len
-1], f
);
1689 value_multiply(f
, f
, P
->Constraint
[l
][nvar
+i
+1]);
1690 value_substract(row
->p
[len
-1], row
->p
[len
-1], f
);
1691 value_decrement(row
->p
[len
-1], row
->p
[len
-1]);
1692 Vector_Gcd(row
->p
+1, len
- 2, &f
);
1693 if (value_notone_p(f
)) {
1694 Vector_AntiScale(row
->p
+1, row
->p
+1, f
, len
-2);
1695 mpz_fdiv_q(row
->p
[len
-1], row
->p
[len
-1], f
);
1697 value_set_si(f
, -1);
1698 Vector_Scale(row
->p
+1, row
->p
+1, f
, len
-1);
1699 value_decrement(row
->p
[len
-1], row
->p
[len
-1]);
1700 Polyhedron
*T
= AddConstraints(row
->p
, 1, P
, MaxRays
);
1702 //printf("not all_pos: i: %d, l: %d, u: %d\n", i, l, u);
1703 info
[i
] = (constraint
)(info
[i
] ^ ALL_POS
);
1705 //puts("pos remainder");
1706 //Polyhedron_Print(stdout, P_VALUE_FMT, T);
1709 if (!(info
[i
] & ONE_NEG
)) {
1711 for (j
= 0; j
< exist
; ++j
)
1713 (value_notzero_p(P
->Constraint
[l
][nvar
+j
+1]) ||
1714 value_notzero_p(P
->Constraint
[u
][nvar
+j
+1])))
1717 /* recalculate constant */
1718 value_oppose(f
, P
->Constraint
[u
][nvar
+i
+1]);
1719 Vector_Combine(P
->Constraint
[l
]+len
-1,
1720 P
->Constraint
[u
]+len
-1, row
->p
+len
-1,
1721 f
, P
->Constraint
[l
][nvar
+i
+1], 1);
1722 value_multiply(f
, f
, P
->Constraint
[l
][nvar
+i
+1]);
1723 value_substract(row
->p
[len
-1], row
->p
[len
-1], f
);
1724 value_set_si(f
, -1);
1725 Vector_Scale(row
->p
+1, row
->p
+1, f
, len
-1);
1726 value_decrement(row
->p
[len
-1], row
->p
[len
-1]);
1727 Vector_Gcd(row
->p
+1, len
- 2, &f
);
1728 if (value_notone_p(f
)) {
1729 Vector_AntiScale(row
->p
+1, row
->p
+1, f
, len
-2);
1730 mpz_fdiv_q(row
->p
[len
-1], row
->p
[len
-1], f
);
1732 value_set_si(f
, -1);
1733 Vector_Scale(row
->p
+1, row
->p
+1, f
, len
-1);
1734 value_decrement(row
->p
[len
-1], row
->p
[len
-1]);
1736 //Vector_Print(stdout, P_VALUE_FMT, row);
1737 Polyhedron
*T
= AddConstraints(row
->p
, 1, P
, MaxRays
);
1739 //printf("one_neg i: %d, l: %d, u: %d\n", i, l, u);
1740 info
[i
] = (constraint
)(info
[i
] | ONE_NEG
);
1742 //puts("neg remainder");
1743 //Polyhedron_Print(stdout, P_VALUE_FMT, T);
1747 if (!(info
[i
] & ALL_POS
) && (info
[i
] & ONE_NEG
))
1751 if (info
[i
] & ALL_POS
)
1758 for (int i = 0; i < exist; ++i)
1759 printf("%i: %i\n", i, info[i]);
1761 for (int i
= 0; i
< exist
; ++i
)
1762 if (info
[i
] & ALL_POS
) {
1764 // Maybe we should chew off some of the fat here
1765 Matrix
*M
= Matrix_Alloc(P
->Dimension
, P
->Dimension
+1);
1766 for (int j
= 0; j
< P
->Dimension
; ++j
)
1767 value_set_si(M
->p
[j
][j
+ (j
>= i
+nvar
)], 1);
1768 Polyhedron
*T
= Polyhedron_Image(P
, M
, MaxRays
);
1770 evalue
*EP
= barvinok_enumerate_e(T
, exist
-1, nparam
, MaxRays
);
1774 for (int i
= 0; i
< exist
; ++i
)
1775 if (info
[i
] & ONE_NEG
) {
1778 return barvinok_enumerate_e(P
, exist
-1, nparam
, MaxRays
);
1780 Polyhedron
*T
= Polyhedron_Copy(P
);
1781 SwapColumns(T
, nvar
+1, nvar
+1+i
);
1782 evalue
*EP
= barvinok_enumerate_e(T
, exist
-1, nparam
, MaxRays
);
1787 for (int i
= 0; i
< exist
; ++i
)
1788 if (info
[i
] & INDEPENDENT
) {
1789 /* Find constraint again and split off negative part */
1791 for (int l
= P
->NbEq
; l
< P
->NbConstraints
; ++l
) {
1792 if (value_negz_p(P
->Constraint
[l
][nvar
+i
+1]))
1794 for (int u
= P
->NbEq
; u
< P
->NbConstraints
; ++u
) {
1795 if (value_posz_p(P
->Constraint
[u
][nvar
+i
+1]))
1797 value_oppose(f
, P
->Constraint
[u
][nvar
+i
+1]);
1798 Vector_Combine(P
->Constraint
[l
]+1, P
->Constraint
[u
]+1,
1800 f
, P
->Constraint
[l
][nvar
+i
+1], len
-1);
1803 for (j
= 0; j
< exist
; ++j
)
1804 if (j
!= i
&& value_notzero_p(row
->p
[nvar
+j
+1]))
1809 //printf("l: %d, u: %d\n", l, u);
1810 value_multiply(f
, f
, P
->Constraint
[l
][nvar
+i
+1]);
1811 value_substract(row
->p
[len
-1], row
->p
[len
-1], f
);
1812 value_set_si(f
, -1);
1813 Vector_Scale(row
->p
+1, row
->p
+1, f
, len
-1);
1814 value_decrement(row
->p
[len
-1], row
->p
[len
-1]);
1815 Vector_Gcd(row
->p
+1, len
- 2, &f
);
1816 if (value_notone_p(f
)) {
1817 Vector_AntiScale(row
->p
+1, row
->p
+1, f
, len
-2);
1818 mpz_fdiv_q(row
->p
[len
-1], row
->p
[len
-1], f
);
1820 Polyhedron
*neg
= AddConstraints(row
->p
, 1, P
, MaxRays
);
1821 value_set_si(f
, -1);
1822 Vector_Scale(row
->p
+1, row
->p
+1, f
, len
-1);
1823 value_decrement(row
->p
[len
-1], row
->p
[len
-1]);
1824 Polyhedron
*pos
= AddConstraints(row
->p
, 1, P
, MaxRays
);
1826 assert(i
== 0); // for now
1828 barvinok_enumerate_e(neg
, exist
-1, nparam
, MaxRays
);
1830 barvinok_enumerate_e(pos
, exist
, nparam
, MaxRays
);
1832 free_evalue_refs(E
);
1834 Polyhedron_Free(neg
);
1835 Polyhedron_Free(pos
);
1840 assert(0); // can't happen