8 #include <NTL/mat_ZZ.h>
12 #include <polylib/polylibgmp.h>
13 #include "ev_operations.h"
28 using std::ostringstream
;
30 #define ALLOC(p) (((long *) (p))[0])
31 #define SIZE(p) (((long *) (p))[1])
32 #define DATA(p) ((mp_limb_t *) (((long *) (p)) + 2))
34 static void value2zz(Value v
, ZZ
& z
)
36 int sa
= v
[0]._mp_size
;
37 int abs_sa
= sa
< 0 ? -sa
: sa
;
39 _ntl_gsetlength(&z
.rep
, abs_sa
);
40 mp_limb_t
* adata
= DATA(z
.rep
);
41 for (int i
= 0; i
< abs_sa
; ++i
)
42 adata
[i
] = v
[0]._mp_d
[i
];
46 void zz2value(ZZ
& z
, Value
& v
)
54 int abs_sa
= sa
< 0 ? -sa
: sa
;
56 mp_limb_t
* adata
= DATA(z
.rep
);
57 _mpz_realloc(v
, abs_sa
);
58 for (int i
= 0; i
< abs_sa
; ++i
)
59 v
[0]._mp_d
[i
] = adata
[i
];
64 #define ALLOC(t,p) p = (t*)malloc(sizeof(*p))
67 * We just ignore the last column and row
68 * If the final element is not equal to one
69 * then the result will actually be a multiple of the input
71 static void matrix2zz(Matrix
*M
, mat_ZZ
& m
, unsigned nr
, unsigned nc
)
75 for (int i
= 0; i
< nr
; ++i
) {
76 // assert(value_one_p(M->p[i][M->NbColumns - 1]));
77 for (int j
= 0; j
< nc
; ++j
) {
78 value2zz(M
->p
[i
][j
], m
[i
][j
]);
83 static void values2zz(Value
*p
, vec_ZZ
& v
, int len
)
87 for (int i
= 0; i
< len
; ++i
) {
94 static void zz2values(vec_ZZ
& v
, Value
*p
)
96 for (int i
= 0; i
< v
.length(); ++i
)
100 static void rays(mat_ZZ
& r
, Polyhedron
*C
)
102 unsigned dim
= C
->NbRays
- 1; /* don't count zero vertex */
103 assert(C
->NbRays
- 1 == C
->Dimension
);
108 for (i
= 0, c
= 0; i
< dim
; ++i
)
109 if (value_zero_p(C
->Ray
[i
][dim
+1])) {
110 for (int j
= 0; j
< dim
; ++j
) {
111 value2zz(C
->Ray
[i
][j
+1], tmp
);
118 static Matrix
* rays(Polyhedron
*C
)
120 unsigned dim
= C
->NbRays
- 1; /* don't count zero vertex */
121 assert(C
->NbRays
- 1 == C
->Dimension
);
123 Matrix
*M
= Matrix_Alloc(dim
+1, dim
+1);
127 for (i
= 0, c
= 0; i
<= dim
&& c
< dim
; ++i
)
128 if (value_zero_p(C
->Ray
[i
][dim
+1])) {
129 Vector_Copy(C
->Ray
[i
] + 1, M
->p
[c
], dim
);
130 value_set_si(M
->p
[c
++][dim
], 0);
133 value_set_si(M
->p
[dim
][dim
], 1);
138 static Matrix
* rays2(Polyhedron
*C
)
140 unsigned dim
= C
->NbRays
- 1; /* don't count zero vertex */
141 assert(C
->NbRays
- 1 == C
->Dimension
);
143 Matrix
*M
= Matrix_Alloc(dim
, dim
);
147 for (i
= 0, c
= 0; i
<= dim
&& c
< dim
; ++i
)
148 if (value_zero_p(C
->Ray
[i
][dim
+1]))
149 Vector_Copy(C
->Ray
[i
] + 1, M
->p
[c
++], dim
);
156 * Returns the largest absolute value in the vector
158 static ZZ
max(vec_ZZ
& v
)
161 for (int i
= 1; i
< v
.length(); ++i
)
171 Rays
= Matrix_Copy(M
);
174 cone(Polyhedron
*C
) {
175 Cone
= Polyhedron_Copy(C
);
181 matrix2zz(Rays
, A
, Rays
->NbRows
- 1, Rays
->NbColumns
- 1);
182 det
= determinant(A
);
185 Vector
* short_vector(vec_ZZ
& lambda
) {
186 Matrix
*M
= Matrix_Copy(Rays
);
187 Matrix
*inv
= Matrix_Alloc(M
->NbRows
, M
->NbColumns
);
188 int ok
= Matrix_Inverse(M
, inv
);
195 matrix2zz(inv
, B
, inv
->NbRows
- 1, inv
->NbColumns
- 1);
196 long r
= LLL(det2
, B
, U
);
200 for (int i
= 1; i
< B
.NumRows(); ++i
) {
212 Vector
*z
= Vector_Alloc(U
[index
].length()+1);
214 zz2values(U
[index
], z
->p
);
215 value_set_si(z
->p
[U
[index
].length()], 0);
219 Polyhedron
*C
= poly();
221 for (i
= 0; i
< C
->NbConstraints
; ++i
) {
222 Inner_Product(z
->p
, C
->Constraint
[i
]+1, z
->Size
-1, &tmp
);
223 if (value_pos_p(tmp
))
226 if (i
== C
->NbConstraints
) {
227 value_set_si(tmp
, -1);
228 Vector_Scale(z
->p
, z
->p
, tmp
, z
->Size
-1);
235 Polyhedron_Free(Cone
);
241 Matrix
*M
= Matrix_Alloc(Rays
->NbRows
+1, Rays
->NbColumns
+1);
242 for (int i
= 0; i
< Rays
->NbRows
; ++i
) {
243 Vector_Copy(Rays
->p
[i
], M
->p
[i
]+1, Rays
->NbColumns
);
244 value_set_si(M
->p
[i
][0], 1);
246 Vector_Set(M
->p
[Rays
->NbRows
]+1, 0, Rays
->NbColumns
-1);
247 value_set_si(M
->p
[Rays
->NbRows
][0], 1);
248 value_set_si(M
->p
[Rays
->NbRows
][Rays
->NbColumns
], 1);
249 Cone
= Rays2Polyhedron(M
, M
->NbRows
+1);
250 assert(Cone
->NbConstraints
== Cone
->NbRays
);
264 dpoly(int d
, ZZ
& degree
, int offset
= 0) {
265 coeff
.SetLength(d
+1);
267 int min
= d
+ offset
;
268 if (degree
>= 0 && degree
< ZZ(INIT_VAL
, min
))
269 min
= to_int(degree
);
271 ZZ c
= ZZ(INIT_VAL
, 1);
274 for (int i
= 1; i
<= min
; ++i
) {
275 c
*= (degree
-i
+ 1);
280 void operator *= (dpoly
& f
) {
281 assert(coeff
.length() == f
.coeff
.length());
283 coeff
= f
.coeff
[0] * coeff
;
284 for (int i
= 1; i
< coeff
.length(); ++i
)
285 for (int j
= 0; i
+j
< coeff
.length(); ++j
)
286 coeff
[i
+j
] += f
.coeff
[i
] * old
[j
];
288 void div(dpoly
& d
, mpq_t count
, ZZ
& sign
) {
289 int len
= coeff
.length();
292 mpq_t
* c
= new mpq_t
[coeff
.length()];
295 for (int i
= 0; i
< len
; ++i
) {
297 zz2value(coeff
[i
], tmp
);
298 mpq_set_z(c
[i
], tmp
);
300 for (int j
= 1; j
<= i
; ++j
) {
301 zz2value(d
.coeff
[j
], tmp
);
302 mpq_set_z(qtmp
, tmp
);
303 mpq_mul(qtmp
, qtmp
, c
[i
-j
]);
304 mpq_sub(c
[i
], c
[i
], qtmp
);
307 zz2value(d
.coeff
[0], tmp
);
308 mpq_set_z(qtmp
, tmp
);
309 mpq_div(c
[i
], c
[i
], qtmp
);
312 mpq_sub(count
, count
, c
[len
-1]);
314 mpq_add(count
, count
, c
[len
-1]);
318 for (int i
= 0; i
< len
; ++i
)
330 dpoly_n(int d
, ZZ
& degree_0
, ZZ
& degree_1
, int offset
= 0) {
334 zz2value(degree_0
, d0
);
335 zz2value(degree_1
, d1
);
336 coeff
= Matrix_Alloc(d
+1, d
+1+1);
337 value_set_si(coeff
->p
[0][0], 1);
338 value_set_si(coeff
->p
[0][d
+1], 1);
339 for (int i
= 1; i
<= d
; ++i
) {
340 value_multiply(coeff
->p
[i
][0], coeff
->p
[i
-1][0], d0
);
341 Vector_Combine(coeff
->p
[i
-1], coeff
->p
[i
-1]+1, coeff
->p
[i
]+1,
343 value_set_si(coeff
->p
[i
][d
+1], i
);
344 value_multiply(coeff
->p
[i
][d
+1], coeff
->p
[i
][d
+1], coeff
->p
[i
-1][d
+1]);
345 value_decrement(d0
, d0
);
350 void div(dpoly
& d
, Vector
*count
, ZZ
& sign
) {
351 int len
= coeff
->NbRows
;
352 Matrix
* c
= Matrix_Alloc(coeff
->NbRows
, coeff
->NbColumns
);
355 for (int i
= 0; i
< len
; ++i
) {
356 Vector_Copy(coeff
->p
[i
], c
->p
[i
], len
+1);
357 for (int j
= 1; j
<= i
; ++j
) {
358 zz2value(d
.coeff
[j
], tmp
);
359 value_multiply(tmp
, tmp
, c
->p
[i
][len
]);
360 value_oppose(tmp
, tmp
);
361 Vector_Combine(c
->p
[i
], c
->p
[i
-j
], c
->p
[i
],
362 c
->p
[i
-j
][len
], tmp
, len
);
363 value_multiply(c
->p
[i
][len
], c
->p
[i
][len
], c
->p
[i
-j
][len
]);
365 zz2value(d
.coeff
[0], tmp
);
366 value_multiply(c
->p
[i
][len
], c
->p
[i
][len
], tmp
);
369 value_set_si(tmp
, -1);
370 Vector_Scale(c
->p
[len
-1], count
->p
, tmp
, len
);
371 value_assign(count
->p
[len
], c
->p
[len
-1][len
]);
373 Vector_Copy(c
->p
[len
-1], count
->p
, len
+1);
374 Vector_Normalize(count
->p
, len
+1);
380 struct dpoly_r_term
{
385 /* len: number of elements in c
386 * each element in c is the coefficient of a power of t
387 * in the MacLaurin expansion
390 vector
< dpoly_r_term
* > *c
;
395 void add_term(int i
, int * powers
, ZZ
& coeff
) {
396 for (int k
= 0; k
< c
[i
].size(); ++k
) {
397 if (memcmp(c
[i
][k
]->powers
, powers
, dim
* sizeof(int)) == 0) {
398 c
[i
][k
]->coeff
+= coeff
;
402 dpoly_r_term
*t
= new dpoly_r_term
;
403 t
->powers
= new int[dim
];
404 memcpy(t
->powers
, powers
, dim
* sizeof(int));
408 dpoly_r(int len
, int dim
) {
412 c
= new vector
< dpoly_r_term
* > [len
];
414 dpoly_r(dpoly
& num
, dpoly
& den
, int pos
, int sign
, int dim
) {
416 len
= num
.coeff
.length();
417 c
= new vector
< dpoly_r_term
* > [len
];
421 for (int i
= 0; i
< len
; ++i
) {
422 ZZ coeff
= num
.coeff
[i
];
423 memset(powers
, 0, dim
* sizeof(int));
426 add_term(i
, powers
, coeff
);
428 for (int j
= 1; j
<= i
; ++j
) {
429 for (int k
= 0; k
< c
[i
-j
].size(); ++k
) {
430 memcpy(powers
, c
[i
-j
][k
]->powers
, dim
*sizeof(int));
432 coeff
= -den
.coeff
[j
-1] * c
[i
-j
][k
]->coeff
;
433 add_term(i
, powers
, coeff
);
439 dpoly_r(dpoly_r
* num
, dpoly
& den
, int pos
, int sign
, int dim
) {
442 c
= new vector
< dpoly_r_term
* > [len
];
447 for (int i
= 0 ; i
< len
; ++i
) {
448 for (int k
= 0; k
< num
->c
[i
].size(); ++k
) {
449 memcpy(powers
, num
->c
[i
][k
]->powers
, dim
*sizeof(int));
451 add_term(i
, powers
, num
->c
[i
][k
]->coeff
);
454 for (int j
= 1; j
<= i
; ++j
) {
455 for (int k
= 0; k
< c
[i
-j
].size(); ++k
) {
456 memcpy(powers
, c
[i
-j
][k
]->powers
, dim
*sizeof(int));
458 coeff
= -den
.coeff
[j
-1] * c
[i
-j
][k
]->coeff
;
459 add_term(i
, powers
, coeff
);
465 for (int i
= 0 ; i
< len
; ++i
)
466 for (int k
= 0; k
< c
[i
].size(); ++k
) {
467 delete [] c
[i
][k
]->powers
;
472 dpoly_r
*div(dpoly
& d
) {
473 dpoly_r
*rc
= new dpoly_r(len
, dim
);
474 rc
->denom
= power(d
.coeff
[0], len
);
475 ZZ inv_d
= rc
->denom
/ d
.coeff
[0];
478 for (int i
= 0; i
< len
; ++i
) {
479 for (int k
= 0; k
< c
[i
].size(); ++k
) {
480 coeff
= c
[i
][k
]->coeff
* inv_d
;
481 rc
->add_term(i
, c
[i
][k
]->powers
, coeff
);
484 for (int j
= 1; j
<= i
; ++j
) {
485 for (int k
= 0; k
< rc
->c
[i
-j
].size(); ++k
) {
486 coeff
= - d
.coeff
[j
] * rc
->c
[i
-j
][k
]->coeff
/ d
.coeff
[0];
487 rc
->add_term(i
, rc
->c
[i
-j
][k
]->powers
, coeff
);
493 void div(dpoly
& d
, ZZ
& sign
, gen_fun
*gf
, mat_ZZ
& pden
, mat_ZZ
& den
,
495 dpoly_r
* rc
= div(d
);
497 int common
= pden
.NumRows();
499 vector
< dpoly_r_term
* >& final
= rc
->c
[len
-1];
501 for (int j
= 0; j
< final
.size(); ++j
) {
503 pden
.SetDims(rows
, pden
.NumCols());
504 for (int k
= 0; k
< dim
; ++k
) {
505 int n
= final
[j
]->powers
[k
];
508 int abs_n
= n
< 0 ? -n
: n
;
509 pden
.SetDims(rows
+abs_n
, pden
.NumCols());
510 for (int l
= 0; l
< abs_n
; ++l
) {
512 pden
[rows
+l
] = den
[k
];
514 pden
[rows
+l
] = -den
[k
];
518 final
[j
]->coeff
*= sign
;
519 gf
->add(final
[j
]->coeff
, rc
->denom
, num_p
, pden
);
524 for (int i
= 0; i
< len
; ++i
) {
527 cout
<< c
[i
].size() << endl
;
528 for (int j
= 0; j
< c
[i
].size(); ++j
) {
529 for (int k
= 0; k
< dim
; ++k
) {
530 cout
<< c
[i
][j
]->powers
[k
] << " ";
532 cout
<< ": " << c
[i
][j
]->coeff
<< "/" << denom
<< endl
;
540 void decompose(Polyhedron
*C
);
541 virtual void handle(Polyhedron
*P
, int sign
) = 0;
544 struct polar_decomposer
: public decomposer
{
545 void decompose(Polyhedron
*C
, unsigned MaxRays
);
546 virtual void handle(Polyhedron
*P
, int sign
);
547 virtual void handle_polar(Polyhedron
*P
, int sign
) = 0;
550 void decomposer::decompose(Polyhedron
*C
)
552 vector
<cone
*> nonuni
;
553 cone
* c
= new cone(C
);
564 while (!nonuni
.empty()) {
567 Vector
* v
= c
->short_vector(lambda
);
568 for (int i
= 0; i
< c
->Rays
->NbRows
- 1; ++i
) {
571 Matrix
* M
= Matrix_Copy(c
->Rays
);
572 Vector_Copy(v
->p
, M
->p
[i
], v
->Size
);
573 cone
* pc
= new cone(M
);
574 assert (pc
->det
!= 0);
575 if (abs(pc
->det
) > 1) {
576 assert(abs(pc
->det
) < abs(c
->det
));
577 nonuni
.push_back(pc
);
579 handle(pc
->poly(), sign(pc
->det
) * s
);
589 void polar_decomposer::decompose(Polyhedron
*cone
, unsigned MaxRays
)
591 Polyhedron_Polarize(cone
);
592 if (cone
->NbRays
- 1 != cone
->Dimension
) {
593 Polyhedron
*tmp
= cone
;
594 cone
= triangularize_cone(cone
, MaxRays
);
595 Polyhedron_Free(tmp
);
597 for (Polyhedron
*Polar
= cone
; Polar
; Polar
= Polar
->next
)
598 decomposer::decompose(Polar
);
602 void polar_decomposer::handle(Polyhedron
*P
, int sign
)
604 Polyhedron_Polarize(P
);
605 handle_polar(P
, sign
);
609 * Barvinok's Decomposition of a simplicial cone
611 * Returns two lists of polyhedra
613 void barvinok_decompose(Polyhedron
*C
, Polyhedron
**ppos
, Polyhedron
**pneg
)
615 Polyhedron
*pos
= *ppos
, *neg
= *pneg
;
616 vector
<cone
*> nonuni
;
617 cone
* c
= new cone(C
);
624 Polyhedron
*p
= Polyhedron_Copy(c
->Cone
);
630 while (!nonuni
.empty()) {
633 Vector
* v
= c
->short_vector(lambda
);
634 for (int i
= 0; i
< c
->Rays
->NbRows
- 1; ++i
) {
637 Matrix
* M
= Matrix_Copy(c
->Rays
);
638 Vector_Copy(v
->p
, M
->p
[i
], v
->Size
);
639 cone
* pc
= new cone(M
);
640 assert (pc
->det
!= 0);
641 if (abs(pc
->det
) > 1) {
642 assert(abs(pc
->det
) < abs(c
->det
));
643 nonuni
.push_back(pc
);
645 Polyhedron
*p
= pc
->poly();
647 if (sign(pc
->det
) == s
) {
666 * Returns a single list of npos "positive" cones followed by nneg
668 * The input cone is freed
670 void decompose(Polyhedron
*cone
, Polyhedron
**parts
, int *npos
, int *nneg
, unsigned MaxRays
)
672 Polyhedron_Polarize(cone
);
673 if (cone
->NbRays
- 1 != cone
->Dimension
) {
674 Polyhedron
*tmp
= cone
;
675 cone
= triangularize_cone(cone
, MaxRays
);
676 Polyhedron_Free(tmp
);
678 Polyhedron
*polpos
= NULL
, *polneg
= NULL
;
679 *npos
= 0; *nneg
= 0;
680 for (Polyhedron
*Polar
= cone
; Polar
; Polar
= Polar
->next
)
681 barvinok_decompose(Polar
, &polpos
, &polneg
);
684 for (Polyhedron
*i
= polpos
; i
; i
= i
->next
) {
685 Polyhedron_Polarize(i
);
689 for (Polyhedron
*i
= polneg
; i
; i
= i
->next
) {
690 Polyhedron_Polarize(i
);
701 const int MAX_TRY
=10;
703 * Searches for a vector that is not orthogonal to any
704 * of the rays in rays.
706 static void nonorthog(mat_ZZ
& rays
, vec_ZZ
& lambda
)
708 int dim
= rays
.NumCols();
710 lambda
.SetLength(dim
);
714 for (int i
= 2; !found
&& i
<= 50*dim
; i
+=4) {
715 for (int j
= 0; j
< MAX_TRY
; ++j
) {
716 for (int k
= 0; k
< dim
; ++k
) {
717 int r
= random_int(i
)+2;
718 int v
= (2*(r
%2)-1) * (r
>> 1);
722 for (; k
< rays
.NumRows(); ++k
)
723 if (lambda
* rays
[k
] == 0)
725 if (k
== rays
.NumRows()) {
734 static void randomvector(Polyhedron
*P
, vec_ZZ
& lambda
, int nvar
)
738 unsigned int dim
= P
->Dimension
;
741 for (int i
= 0; i
< P
->NbRays
; ++i
) {
742 for (int j
= 1; j
<= dim
; ++j
) {
743 value_absolute(tmp
, P
->Ray
[i
][j
]);
744 int t
= VALUE_TO_LONG(tmp
) * 16;
749 for (int i
= 0; i
< P
->NbConstraints
; ++i
) {
750 for (int j
= 1; j
<= dim
; ++j
) {
751 value_absolute(tmp
, P
->Constraint
[i
][j
]);
752 int t
= VALUE_TO_LONG(tmp
) * 16;
759 lambda
.SetLength(nvar
);
760 for (int k
= 0; k
< nvar
; ++k
) {
761 int r
= random_int(max
*dim
)+2;
762 int v
= (2*(r
%2)-1) * (max
/2*dim
+ (r
>> 1));
767 static void add_rays(mat_ZZ
& rays
, Polyhedron
*i
, int *r
, int nvar
= -1,
770 unsigned dim
= i
->Dimension
;
773 for (int k
= 0; k
< i
->NbRays
; ++k
) {
774 if (!value_zero_p(i
->Ray
[k
][dim
+1]))
776 if (!all
&& nvar
!= dim
&& First_Non_Zero(i
->Ray
[k
]+1, nvar
) == -1)
778 values2zz(i
->Ray
[k
]+1, rays
[(*r
)++], nvar
);
782 void lattice_point(Value
* values
, Polyhedron
*i
, vec_ZZ
& vertex
)
784 unsigned dim
= i
->Dimension
;
785 if(!value_one_p(values
[dim
])) {
786 Matrix
* Rays
= rays(i
);
787 Matrix
*inv
= Matrix_Alloc(Rays
->NbRows
, Rays
->NbColumns
);
788 int ok
= Matrix_Inverse(Rays
, inv
);
792 Vector
*lambda
= Vector_Alloc(dim
+1);
793 Vector_Matrix_Product(values
, inv
, lambda
->p
);
795 for (int j
= 0; j
< dim
; ++j
)
796 mpz_cdiv_q(lambda
->p
[j
], lambda
->p
[j
], lambda
->p
[dim
]);
797 value_set_si(lambda
->p
[dim
], 1);
798 Vector
*A
= Vector_Alloc(dim
+1);
799 Vector_Matrix_Product(lambda
->p
, Rays
, A
->p
);
802 values2zz(A
->p
, vertex
, dim
);
805 values2zz(values
, vertex
, dim
);
808 static evalue
*term(int param
, ZZ
& c
, Value
*den
= NULL
)
810 evalue
*EP
= new evalue();
812 value_set_si(EP
->d
,0);
813 EP
->x
.p
= new_enode(polynomial
, 2, param
+ 1);
814 evalue_set_si(&EP
->x
.p
->arr
[0], 0, 1);
815 value_init(EP
->x
.p
->arr
[1].x
.n
);
817 value_set_si(EP
->x
.p
->arr
[1].d
, 1);
819 value_assign(EP
->x
.p
->arr
[1].d
, *den
);
820 zz2value(c
, EP
->x
.p
->arr
[1].x
.n
);
824 static void vertex_period(
825 Polyhedron
*i
, vec_ZZ
& lambda
, Matrix
*T
,
826 Value lcm
, int p
, Vector
*val
,
827 evalue
*E
, evalue
* ev
,
830 unsigned nparam
= T
->NbRows
- 1;
831 unsigned dim
= i
->Dimension
;
838 Vector
* values
= Vector_Alloc(dim
+ 1);
839 Vector_Matrix_Product(val
->p
, T
, values
->p
);
840 value_assign(values
->p
[dim
], lcm
);
841 lattice_point(values
->p
, i
, vertex
);
842 num
= vertex
* lambda
;
847 zz2value(num
, ev
->x
.n
);
848 value_assign(ev
->d
, lcm
);
855 values2zz(T
->p
[p
], vertex
, dim
);
856 nump
= vertex
* lambda
;
857 if (First_Non_Zero(val
->p
, p
) == -1) {
858 value_assign(tmp
, lcm
);
859 evalue
*ET
= term(p
, nump
, &tmp
);
861 free_evalue_refs(ET
);
865 value_assign(tmp
, lcm
);
866 if (First_Non_Zero(T
->p
[p
], dim
) != -1)
867 Vector_Gcd(T
->p
[p
], dim
, &tmp
);
869 if (value_lt(tmp
, lcm
)) {
872 value_division(tmp
, lcm
, tmp
);
873 value_set_si(ev
->d
, 0);
874 ev
->x
.p
= new_enode(periodic
, VALUE_TO_INT(tmp
), p
+1);
875 value2zz(tmp
, count
);
877 value_decrement(tmp
, tmp
);
879 ZZ new_offset
= offset
- count
* nump
;
880 value_assign(val
->p
[p
], tmp
);
881 vertex_period(i
, lambda
, T
, lcm
, p
+1, val
, E
,
882 &ev
->x
.p
->arr
[VALUE_TO_INT(tmp
)], new_offset
);
883 } while (value_pos_p(tmp
));
885 vertex_period(i
, lambda
, T
, lcm
, p
+1, val
, E
, ev
, offset
);
889 static void mask_r(Matrix
*f
, int nr
, Vector
*lcm
, int p
, Vector
*val
, evalue
*ev
)
891 unsigned nparam
= lcm
->Size
;
894 Vector
* prod
= Vector_Alloc(f
->NbRows
);
895 Matrix_Vector_Product(f
, val
->p
, prod
->p
);
897 for (int i
= 0; i
< nr
; ++i
) {
898 value_modulus(prod
->p
[i
], prod
->p
[i
], f
->p
[i
][nparam
+1]);
899 isint
&= value_zero_p(prod
->p
[i
]);
901 value_set_si(ev
->d
, 1);
903 value_set_si(ev
->x
.n
, isint
);
910 if (value_one_p(lcm
->p
[p
]))
911 mask_r(f
, nr
, lcm
, p
+1, val
, ev
);
913 value_assign(tmp
, lcm
->p
[p
]);
914 value_set_si(ev
->d
, 0);
915 ev
->x
.p
= new_enode(periodic
, VALUE_TO_INT(tmp
), p
+1);
917 value_decrement(tmp
, tmp
);
918 value_assign(val
->p
[p
], tmp
);
919 mask_r(f
, nr
, lcm
, p
+1, val
, &ev
->x
.p
->arr
[VALUE_TO_INT(tmp
)]);
920 } while (value_pos_p(tmp
));
925 static evalue
*multi_monom(vec_ZZ
& p
)
927 evalue
*X
= new evalue();
930 unsigned nparam
= p
.length()-1;
931 zz2value(p
[nparam
], X
->x
.n
);
932 value_set_si(X
->d
, 1);
933 for (int i
= 0; i
< nparam
; ++i
) {
936 evalue
*T
= term(i
, p
[i
]);
945 * Check whether mapping polyhedron P on the affine combination
946 * num yields a range that has a fixed quotient on integer
948 * If zero is true, then we are only interested in the quotient
949 * for the cases where the remainder is zero.
950 * Returns NULL if false and a newly allocated value if true.
952 static Value
*fixed_quotient(Polyhedron
*P
, vec_ZZ
& num
, Value d
, bool zero
)
955 int len
= num
.length();
956 Matrix
*T
= Matrix_Alloc(2, len
);
957 zz2values(num
, T
->p
[0]);
958 value_set_si(T
->p
[1][len
-1], 1);
959 Polyhedron
*I
= Polyhedron_Image(P
, T
, P
->NbConstraints
);
963 for (i
= 0; i
< I
->NbRays
; ++i
)
964 if (value_zero_p(I
->Ray
[i
][2])) {
972 int bounded
= line_minmax(I
, &min
, &max
);
976 mpz_cdiv_q(min
, min
, d
);
978 mpz_fdiv_q(min
, min
, d
);
979 mpz_fdiv_q(max
, max
, d
);
981 if (value_eq(min
, max
)) {
984 value_assign(*ret
, min
);
992 * Normalize linear expression coef modulo m
993 * Removes common factor and reduces coefficients
994 * Returns index of first non-zero coefficient or len
996 static int normal_mod(Value
*coef
, int len
, Value
*m
)
1001 Vector_Gcd(coef
, len
, &gcd
);
1003 Vector_AntiScale(coef
, coef
, gcd
, len
);
1005 value_division(*m
, *m
, gcd
);
1008 if (value_one_p(*m
))
1012 for (j
= 0; j
< len
; ++j
)
1013 mpz_fdiv_r(coef
[j
], coef
[j
], *m
);
1014 for (j
= 0; j
< len
; ++j
)
1015 if (value_notzero_p(coef
[j
]))
1022 static void mask(Matrix
*f
, evalue
*factor
)
1024 int nr
= f
->NbRows
, nc
= f
->NbColumns
;
1027 for (n
= 0; n
< nr
&& value_notzero_p(f
->p
[n
][nc
-1]); ++n
)
1028 if (value_notone_p(f
->p
[n
][nc
-1]) &&
1029 value_notmone_p(f
->p
[n
][nc
-1]))
1043 value_set_si(EV
.x
.n
, 1);
1045 for (n
= 0; n
< nr
; ++n
) {
1046 value_assign(m
, f
->p
[n
][nc
-1]);
1047 if (value_one_p(m
) || value_mone_p(m
))
1050 int j
= normal_mod(f
->p
[n
], nc
-1, &m
);
1052 free_evalue_refs(factor
);
1053 value_init(factor
->d
);
1054 evalue_set_si(factor
, 0, 1);
1058 values2zz(f
->p
[n
], row
, nc
-1);
1061 if (j
< (nc
-1)-1 && row
[j
] > g
/2) {
1062 for (int k
= j
; k
< (nc
-1); ++k
)
1064 row
[k
] = g
- row
[k
];
1068 value_set_si(EP
.d
, 0);
1069 EP
.x
.p
= new_enode(relation
, 2, 0);
1070 value_clear(EP
.x
.p
->arr
[1].d
);
1071 EP
.x
.p
->arr
[1] = *factor
;
1072 evalue
*ev
= &EP
.x
.p
->arr
[0];
1073 value_set_si(ev
->d
, 0);
1074 ev
->x
.p
= new_enode(fractional
, 3, -1);
1075 evalue_set_si(&ev
->x
.p
->arr
[1], 0, 1);
1076 evalue_set_si(&ev
->x
.p
->arr
[2], 1, 1);
1077 evalue
*E
= multi_monom(row
);
1078 value_assign(EV
.d
, m
);
1080 value_clear(ev
->x
.p
->arr
[0].d
);
1081 ev
->x
.p
->arr
[0] = *E
;
1087 free_evalue_refs(&EV
);
1093 static void mask(Matrix
*f
, evalue
*factor
)
1095 int nr
= f
->NbRows
, nc
= f
->NbColumns
;
1098 for (n
= 0; n
< nr
&& value_notzero_p(f
->p
[n
][nc
-1]); ++n
)
1099 if (value_notone_p(f
->p
[n
][nc
-1]) &&
1100 value_notmone_p(f
->p
[n
][nc
-1]))
1108 unsigned np
= nc
- 2;
1109 Vector
*lcm
= Vector_Alloc(np
);
1110 Vector
*val
= Vector_Alloc(nc
);
1111 Vector_Set(val
->p
, 0, nc
);
1112 value_set_si(val
->p
[np
], 1);
1113 Vector_Set(lcm
->p
, 1, np
);
1114 for (n
= 0; n
< nr
; ++n
) {
1115 if (value_one_p(f
->p
[n
][nc
-1]) ||
1116 value_mone_p(f
->p
[n
][nc
-1]))
1118 for (int j
= 0; j
< np
; ++j
)
1119 if (value_notzero_p(f
->p
[n
][j
])) {
1120 Gcd(f
->p
[n
][j
], f
->p
[n
][nc
-1], &tmp
);
1121 value_division(tmp
, f
->p
[n
][nc
-1], tmp
);
1122 value_lcm(tmp
, lcm
->p
[j
], &lcm
->p
[j
]);
1127 mask_r(f
, nr
, lcm
, 0, val
, &EP
);
1132 free_evalue_refs(&EP
);
1143 static bool mod_needed(Polyhedron
*PD
, vec_ZZ
& num
, Value d
, evalue
*E
)
1145 Value
*q
= fixed_quotient(PD
, num
, d
, false);
1150 value_oppose(*q
, *q
);
1153 value_set_si(EV
.d
, 1);
1155 value_multiply(EV
.x
.n
, *q
, d
);
1157 free_evalue_refs(&EV
);
1163 static void ceil_mod(Value
*coef
, int len
, Value d
, ZZ
& f
, evalue
*EP
, Polyhedron
*PD
)
1167 value_set_si(m
, -1);
1169 Vector_Scale(coef
, coef
, m
, len
);
1172 int j
= normal_mod(coef
, len
, &m
);
1180 values2zz(coef
, num
, len
);
1187 evalue_set_si(&tmp
, 0, 1);
1191 while (j
< len
-1 && (num
[j
] == g
/2 || num
[j
] == 0))
1193 if ((j
< len
-1 && num
[j
] > g
/2) || (j
== len
-1 && num
[j
] >= (g
+1)/2)) {
1194 for (int k
= j
; k
< len
-1; ++k
)
1196 num
[k
] = g
- num
[k
];
1197 num
[len
-1] = g
- 1 - num
[len
-1];
1198 value_assign(tmp
.d
, m
);
1200 zz2value(t
, tmp
.x
.n
);
1206 ZZ t
= num
[len
-1] * f
;
1207 zz2value(t
, tmp
.x
.n
);
1208 value_assign(tmp
.d
, m
);
1211 evalue
*E
= multi_monom(num
);
1215 if (PD
&& !mod_needed(PD
, num
, m
, E
)) {
1217 zz2value(f
, EV
.x
.n
);
1218 value_assign(EV
.d
, m
);
1223 value_set_si(EV
.x
.n
, 1);
1224 value_assign(EV
.d
, m
);
1226 value_clear(EV
.x
.n
);
1227 value_set_si(EV
.d
, 0);
1228 EV
.x
.p
= new_enode(fractional
, 3, -1);
1229 evalue_copy(&EV
.x
.p
->arr
[0], E
);
1230 evalue_set_si(&EV
.x
.p
->arr
[1], 0, 1);
1231 value_init(EV
.x
.p
->arr
[2].x
.n
);
1232 zz2value(f
, EV
.x
.p
->arr
[2].x
.n
);
1233 value_set_si(EV
.x
.p
->arr
[2].d
, 1);
1238 free_evalue_refs(&EV
);
1239 free_evalue_refs(E
);
1243 free_evalue_refs(&tmp
);
1249 evalue
* bv_ceil3(Value
*coef
, int len
, Value d
, Polyhedron
*P
)
1251 Vector
*val
= Vector_Alloc(len
);
1255 value_set_si(t
, -1);
1256 Vector_Scale(coef
, val
->p
, t
, len
);
1257 value_absolute(t
, d
);
1260 values2zz(val
->p
, num
, len
);
1261 evalue
*EP
= multi_monom(num
);
1265 value_init(tmp
.x
.n
);
1266 value_set_si(tmp
.x
.n
, 1);
1267 value_assign(tmp
.d
, t
);
1273 ceil_mod(val
->p
, len
, t
, one
, EP
, P
);
1276 /* copy EP to malloc'ed evalue */
1282 free_evalue_refs(&tmp
);
1289 evalue
* lattice_point(
1290 Polyhedron
*i
, vec_ZZ
& lambda
, Matrix
*W
, Value lcm
, Polyhedron
*PD
)
1292 unsigned nparam
= W
->NbColumns
- 1;
1294 Matrix
* Rays
= rays2(i
);
1295 Matrix
*T
= Transpose(Rays
);
1296 Matrix
*T2
= Matrix_Copy(T
);
1297 Matrix
*inv
= Matrix_Alloc(T2
->NbRows
, T2
->NbColumns
);
1298 int ok
= Matrix_Inverse(T2
, inv
);
1303 matrix2zz(W
, vertex
, W
->NbRows
, W
->NbColumns
);
1306 num
= lambda
* vertex
;
1308 evalue
*EP
= multi_monom(num
);
1312 value_init(tmp
.x
.n
);
1313 value_set_si(tmp
.x
.n
, 1);
1314 value_assign(tmp
.d
, lcm
);
1318 Matrix
*L
= Matrix_Alloc(inv
->NbRows
, W
->NbColumns
);
1319 Matrix_Product(inv
, W
, L
);
1322 matrix2zz(T
, RT
, T
->NbRows
, T
->NbColumns
);
1325 vec_ZZ p
= lambda
* RT
;
1327 for (int i
= 0; i
< L
->NbRows
; ++i
) {
1328 ceil_mod(L
->p
[i
], nparam
+1, lcm
, p
[i
], EP
, PD
);
1334 free_evalue_refs(&tmp
);
1338 evalue
* lattice_point(
1339 Polyhedron
*i
, vec_ZZ
& lambda
, Matrix
*W
, Value lcm
, Polyhedron
*PD
)
1341 Matrix
*T
= Transpose(W
);
1342 unsigned nparam
= T
->NbRows
- 1;
1344 evalue
*EP
= new evalue();
1346 evalue_set_si(EP
, 0, 1);
1349 Vector
*val
= Vector_Alloc(nparam
+1);
1350 value_set_si(val
->p
[nparam
], 1);
1351 ZZ
offset(INIT_VAL
, 0);
1353 vertex_period(i
, lambda
, T
, lcm
, 0, val
, EP
, &ev
, offset
);
1356 free_evalue_refs(&ev
);
1367 Param_Vertices
* V
, Polyhedron
*i
, vec_ZZ
& lambda
, term_info
* term
,
1370 unsigned nparam
= V
->Vertex
->NbColumns
- 2;
1371 unsigned dim
= i
->Dimension
;
1373 vertex
.SetDims(V
->Vertex
->NbRows
, nparam
+1);
1377 value_set_si(lcm
, 1);
1378 for (int j
= 0; j
< V
->Vertex
->NbRows
; ++j
) {
1379 value_lcm(lcm
, V
->Vertex
->p
[j
][nparam
+1], &lcm
);
1381 if (value_notone_p(lcm
)) {
1382 Matrix
* mv
= Matrix_Alloc(dim
, nparam
+1);
1383 for (int j
= 0 ; j
< dim
; ++j
) {
1384 value_division(tmp
, lcm
, V
->Vertex
->p
[j
][nparam
+1]);
1385 Vector_Scale(V
->Vertex
->p
[j
], mv
->p
[j
], tmp
, nparam
+1);
1388 term
->E
= lattice_point(i
, lambda
, mv
, lcm
, PD
);
1396 for (int i
= 0; i
< V
->Vertex
->NbRows
; ++i
) {
1397 assert(value_one_p(V
->Vertex
->p
[i
][nparam
+1])); // for now
1398 values2zz(V
->Vertex
->p
[i
], vertex
[i
], nparam
+1);
1402 num
= lambda
* vertex
;
1406 for (int j
= 0; j
< nparam
; ++j
)
1412 term
->E
= multi_monom(num
);
1416 term
->constant
= num
[nparam
];
1419 term
->coeff
= num
[p
];
1426 static void normalize(ZZ
& sign
, ZZ
& num
, vec_ZZ
& den
)
1428 unsigned dim
= den
.length();
1432 for (int j
= 0; j
< den
.length(); ++j
) {
1436 den
[j
] = abs(den
[j
]);
1445 * f: the powers in the denominator for the remaining vars
1446 * each row refers to a factor
1447 * den_s: for each factor, the power of (s+1)
1449 * num_s: powers in the numerator corresponding to the summed vars
1450 * num_p: powers in the numerator corresponidng to the remaining vars
1451 * number of rays in cone: "dim" = "k"
1452 * length of each ray: "dim" = "d"
1453 * for now, it is assume: k == d
1455 * den_p: for each factor
1456 * 0: independent of remaining vars
1457 * 1: power corresponds to corresponding row in f
1458 * -1: power is inverse of corresponding row in f
1460 static void normalize(ZZ
& sign
,
1461 ZZ
& num_s
, vec_ZZ
& num_p
, vec_ZZ
& den_s
, vec_ZZ
& den_p
,
1464 unsigned dim
= f
.NumRows();
1465 unsigned nparam
= num_p
.length();
1466 unsigned nvar
= dim
- nparam
;
1470 for (int j
= 0; j
< den_s
.length(); ++j
) {
1471 if (den_s
[j
] == 0) {
1476 for (k
= 0; k
< nparam
; ++k
)
1490 den_s
[j
] = abs(den_s
[j
]);
1499 struct counter
: public polar_decomposer
{
1511 counter(Polyhedron
*P
) {
1514 randomvector(P
, lambda
, dim
);
1515 rays
.SetDims(dim
, dim
);
1520 void start(unsigned MaxRays
);
1526 virtual void handle_polar(Polyhedron
*P
, int sign
);
1529 void counter::handle_polar(Polyhedron
*C
, int s
)
1532 assert(C
->NbRays
-1 == dim
);
1533 add_rays(rays
, C
, &r
);
1534 for (int k
= 0; k
< dim
; ++k
) {
1535 assert(lambda
* rays
[k
] != 0);
1540 lattice_point(P
->Ray
[j
]+1, C
, vertex
);
1541 num
= vertex
* lambda
;
1542 den
= rays
* lambda
;
1543 normalize(sign
, num
, den
);
1546 dpoly
n(dim
, den
[0], 1);
1547 for (int k
= 1; k
< dim
; ++k
) {
1548 dpoly
fact(dim
, den
[k
], 1);
1551 d
.div(n
, count
, sign
);
1554 void counter::start(unsigned MaxRays
)
1556 for (j
= 0; j
< P
->NbRays
; ++j
) {
1557 Polyhedron
*C
= supporting_cone(P
, j
);
1558 decompose(C
, MaxRays
);
1562 // incremental counter
1563 struct icounter
: public polar_decomposer
{
1578 icounter(Polyhedron
*P
) {
1581 lambda
.SetLength(1);
1583 //den.SetLength(dim);
1591 void start(unsigned MaxRays
);
1600 virtual void handle_polar(Polyhedron
*P
, int sign
);
1601 void reduce(ZZ
& c
, ZZ
& cd
, vec_ZZ
& num
, mat_ZZ
& den_f
);
1602 void recurse(ZZ c
, ZZ cd
, vec_ZZ
& num
, mat_ZZ
& den
);
1605 void icounter::recurse(ZZ c
, ZZ cd
, vec_ZZ
& num
, mat_ZZ
& den
)
1607 unsigned d
= num
.length();
1608 unsigned len
= den
.NumRows(); // number of factors in den
1610 reduce(c
, cd
, num
, den
);
1613 void icounter::reduce(ZZ
& c
, ZZ
& cd
, vec_ZZ
& num
, mat_ZZ
& den_f
)
1615 unsigned len
= den_f
.NumRows(); // number of factors in den
1616 unsigned d
= num
.length()-1;
1619 den_s
.SetLength(len
);
1621 den_r
.SetDims(len
, d
);
1624 for (r
= 0; r
< len
; ++r
) {
1625 den_s
[r
] = den_f
[r
][0];
1626 for (k
= 1; k
<= d
; ++k
)
1627 den_r
[r
][k
-1] = den_f
[r
][k
];
1632 normalize(c
, num_s
, den_s
);
1634 dpoly
n(len
, num_s
);
1635 dpoly
D(len
, den_s
[0], 1);
1636 for (int k
= 1; k
< len
; ++k
) {
1637 dpoly
fact(len
, den_s
[k
], 1);
1640 mpq_set_si(tcount
, 0, 1);
1641 n
.div(D
, tcount
, one
);
1644 mpz_mul(mpq_numref(tcount
), mpq_numref(tcount
), tn
);
1645 mpz_mul(mpq_denref(tcount
), mpq_denref(tcount
), td
);
1646 mpq_canonicalize(tcount
);
1647 mpq_add(count
, count
, tcount
);
1650 assert(num
.length() > 1);
1655 for (k
= 1 ; k
<= d
; ++k
)
1656 num_p
[k
-1] = num
[k
];
1659 den_p
.SetLength(len
);
1661 normalize(c
, num_s
, num_p
, den_s
, den_p
, den_r
);
1663 /* Since we're working incrementally, we should look
1664 * for the "easiest" parameter first.
1665 * In particular we should handle the parameters such
1666 * that no_param + only_param == len, since that allows
1667 * us to decouple the problem and the split off part
1668 * may very well be zero
1672 for (int k
= 0; k
< len
; ++k
) {
1675 else if (den_s
[k
] == 0)
1678 if (no_param
== 0) {
1679 for (int k
= 0; k
< len
; ++k
)
1681 den_r
[k
] = -den_r
[k
];
1682 recurse(c
, cd
, num_p
, den_r
);
1686 pden
.SetDims(only_param
, d
);
1688 for (k
= 0, l
= 0; k
< len
; ++k
)
1690 pden
[l
++] = den_r
[k
];
1692 for (k
= 0; k
< len
; ++k
)
1696 dpoly
n(no_param
, num_s
);
1697 dpoly
D(no_param
, den_s
[k
], 1);
1698 for ( ; ++k
< len
; )
1699 if (den_p
[k
] == 0) {
1700 dpoly
fact(no_param
, den_s
[k
], 1);
1704 if (no_param
+ only_param
== len
) {
1705 mpq_set_si(tcount
, 0, 1);
1706 n
.div(D
, tcount
, one
);
1709 value2zz(mpq_numref(tcount
), qn
);
1710 value2zz(mpq_denref(tcount
), qd
);
1716 recurse(qn
, qd
, num_p
, pden
);
1720 for (k
= 0; k
< len
; ++k
) {
1721 if (den_s
[k
] == 0 || den_p
[k
] == 0)
1724 dpoly
pd(no_param
-1, den_s
[k
], 1);
1725 int s
= den_p
[k
] < 0 ? -1 : 1;
1728 r
= new dpoly_r(n
, pd
, k
, s
, len
);
1730 dpoly_r
*nr
= new dpoly_r(r
, pd
, k
, s
, len
);
1736 dpoly_r
*rc
= r
->div(D
);
1740 int common
= pden
.NumRows();
1741 vector
< dpoly_r_term
* >& final
= rc
->c
[rc
->len
-1];
1743 for (int j
= 0; j
< final
.size(); ++j
) {
1744 if (final
[j
]->coeff
== 0)
1747 pden
.SetDims(rows
, pden
.NumCols());
1748 for (int k
= 0; k
< rc
->dim
; ++k
) {
1749 int n
= final
[j
]->powers
[k
];
1752 int abs_n
= n
< 0 ? -n
: n
;
1753 pden
.SetDims(rows
+abs_n
, pden
.NumCols());
1754 for (int l
= 0; l
< abs_n
; ++l
) {
1756 pden
[rows
+l
] = den_r
[k
];
1758 pden
[rows
+l
] = -den_r
[k
];
1762 final
[j
]->coeff
*= c
;
1763 recurse(final
[j
]->coeff
, rc
->denom
, num_p
, pden
);
1772 void icounter::handle_polar(Polyhedron
*C
, int s
)
1774 assert(C
->NbRays
-1 == dim
);
1778 lattice_point(P
->Ray
[j
]+1, C
, vertex
);
1781 den
.SetDims(dim
, dim
);
1784 for (r
= 0; r
< dim
; ++r
)
1785 values2zz(C
->Ray
[r
]+1, den
[r
], dim
);
1787 reduce(sgn
, one
, vertex
, den
);
1790 void icounter::start(unsigned MaxRays
)
1792 for (j
= 0; j
< P
->NbRays
; ++j
) {
1793 Polyhedron
*C
= supporting_cone(P
, j
);
1794 decompose(C
, MaxRays
);
1798 typedef Polyhedron
* Polyhedron_p
;
1800 void barvinok_count(Polyhedron
*P
, Value
* result
, unsigned NbMaxCons
)
1802 Polyhedron
** vcone
;
1811 value_set_si(*result
, 0);
1815 for (; r
< P
->NbRays
; ++r
)
1816 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1]))
1818 if (P
->NbBid
!=0 || r
< P
->NbRays
) {
1819 value_set_si(*result
, -1);
1823 P
= remove_equalities(P
);
1826 value_set_si(*result
, 0);
1832 value_set_si(factor
, 1);
1833 Q
= Polyhedron_Reduce(P
, &factor
);
1840 if (P
->Dimension
== 0) {
1841 value_assign(*result
, factor
);
1844 value_clear(factor
);
1849 cnt
.start(NbMaxCons
);
1851 assert(value_one_p(&cnt
.count
[0]._mp_den
));
1852 value_multiply(*result
, &cnt
.count
[0]._mp_num
, factor
);
1856 value_clear(factor
);
1859 static void uni_polynom(int param
, Vector
*c
, evalue
*EP
)
1861 unsigned dim
= c
->Size
-2;
1863 value_set_si(EP
->d
,0);
1864 EP
->x
.p
= new_enode(polynomial
, dim
+1, param
+1);
1865 for (int j
= 0; j
<= dim
; ++j
)
1866 evalue_set(&EP
->x
.p
->arr
[j
], c
->p
[j
], c
->p
[dim
+1]);
1869 static void multi_polynom(Vector
*c
, evalue
* X
, evalue
*EP
)
1871 unsigned dim
= c
->Size
-2;
1875 evalue_set(&EC
, c
->p
[dim
], c
->p
[dim
+1]);
1878 evalue_set(EP
, c
->p
[dim
], c
->p
[dim
+1]);
1880 for (int i
= dim
-1; i
>= 0; --i
) {
1882 value_assign(EC
.x
.n
, c
->p
[i
]);
1885 free_evalue_refs(&EC
);
1888 Polyhedron
*unfringe (Polyhedron
*P
, unsigned MaxRays
)
1890 int len
= P
->Dimension
+2;
1891 Polyhedron
*T
, *R
= P
;
1894 Vector
*row
= Vector_Alloc(len
);
1895 value_set_si(row
->p
[0], 1);
1897 R
= DomainConstraintSimplify(Polyhedron_Copy(P
), MaxRays
);
1899 Matrix
*M
= Matrix_Alloc(2, len
-1);
1900 value_set_si(M
->p
[1][len
-2], 1);
1901 for (int v
= 0; v
< P
->Dimension
; ++v
) {
1902 value_set_si(M
->p
[0][v
], 1);
1903 Polyhedron
*I
= Polyhedron_Image(P
, M
, 2+1);
1904 value_set_si(M
->p
[0][v
], 0);
1905 for (int r
= 0; r
< I
->NbConstraints
; ++r
) {
1906 if (value_zero_p(I
->Constraint
[r
][0]))
1908 if (value_zero_p(I
->Constraint
[r
][1]))
1910 if (value_one_p(I
->Constraint
[r
][1]))
1912 if (value_mone_p(I
->Constraint
[r
][1]))
1914 value_absolute(g
, I
->Constraint
[r
][1]);
1915 Vector_Set(row
->p
+1, 0, len
-2);
1916 value_division(row
->p
[1+v
], I
->Constraint
[r
][1], g
);
1917 mpz_fdiv_q(row
->p
[len
-1], I
->Constraint
[r
][2], g
);
1919 R
= AddConstraints(row
->p
, 1, R
, MaxRays
);
1931 static Polyhedron
*reduce_domain(Polyhedron
*D
, Matrix
*CT
, Polyhedron
*CEq
,
1932 Polyhedron
**fVD
, int nd
, unsigned MaxRays
)
1937 Dt
= CT
? DomainPreimage(D
, CT
, MaxRays
) : D
;
1938 Polyhedron
*rVD
= DomainIntersection(Dt
, CEq
, MaxRays
);
1940 /* if rVD is empty or too small in geometric dimension */
1941 if(!rVD
|| emptyQ(rVD
) ||
1942 (rVD
->Dimension
-rVD
->NbEq
< Dt
->Dimension
-Dt
->NbEq
-CEq
->NbEq
)) {
1947 return 0; /* empty validity domain */
1953 fVD
[nd
] = Domain_Copy(rVD
);
1954 for (int i
= 0 ; i
< nd
; ++i
) {
1955 Polyhedron
*I
= DomainIntersection(fVD
[nd
], fVD
[i
], MaxRays
);
1960 Polyhedron
*F
= DomainSimplify(I
, fVD
[nd
], MaxRays
);
1962 Polyhedron
*T
= rVD
;
1963 rVD
= DomainDifference(rVD
, F
, MaxRays
);
1970 rVD
= DomainConstraintSimplify(rVD
, MaxRays
);
1972 Domain_Free(fVD
[nd
]);
1979 barvinok_count(rVD
, &c
, MaxRays
);
1980 if (value_zero_p(c
)) {
1989 static bool Polyhedron_is_infinite(Polyhedron
*P
, unsigned nparam
)
1992 for (r
= 0; r
< P
->NbRays
; ++r
)
1993 if (value_zero_p(P
->Ray
[r
][0]) ||
1994 value_zero_p(P
->Ray
[r
][P
->Dimension
+1])) {
1996 for (i
= P
->Dimension
- nparam
; i
< P
->Dimension
; ++i
)
1997 if (value_notzero_p(P
->Ray
[r
][i
+1]))
1999 if (i
>= P
->Dimension
)
2002 return r
< P
->NbRays
;
2005 /* Check whether all rays point in the positive directions
2006 * for the parameters
2008 static bool Polyhedron_has_positive_rays(Polyhedron
*P
, unsigned nparam
)
2011 for (r
= 0; r
< P
->NbRays
; ++r
)
2012 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1])) {
2014 for (i
= P
->Dimension
- nparam
; i
< P
->Dimension
; ++i
)
2015 if (value_neg_p(P
->Ray
[r
][i
+1]))
2021 typedef evalue
* evalue_p
;
2023 struct enumerator
: public polar_decomposer
{
2037 enumerator(Polyhedron
*P
, unsigned dim
, unsigned nbV
) {
2041 randomvector(P
, lambda
, dim
);
2042 rays
.SetDims(dim
, dim
);
2044 c
= Vector_Alloc(dim
+2);
2046 vE
= new evalue_p
[nbV
];
2047 for (int j
= 0; j
< nbV
; ++j
)
2053 void decompose_at(Param_Vertices
*V
, int _i
, unsigned MaxRays
) {
2054 Polyhedron
*C
= supporting_cone_p(P
, V
);
2058 vE
[_i
] = new evalue
;
2059 value_init(vE
[_i
]->d
);
2060 evalue_set_si(vE
[_i
], 0, 1);
2062 decompose(C
, MaxRays
);
2069 for (int j
= 0; j
< nbV
; ++j
)
2071 free_evalue_refs(vE
[j
]);
2077 virtual void handle_polar(Polyhedron
*P
, int sign
);
2080 void enumerator::handle_polar(Polyhedron
*C
, int s
)
2083 assert(C
->NbRays
-1 == dim
);
2084 add_rays(rays
, C
, &r
);
2085 for (int k
= 0; k
< dim
; ++k
) {
2086 assert(lambda
* rays
[k
] != 0);
2091 lattice_point(V
, C
, lambda
, &num
, 0);
2092 den
= rays
* lambda
;
2093 normalize(sign
, num
.constant
, den
);
2095 dpoly
n(dim
, den
[0], 1);
2096 for (int k
= 1; k
< dim
; ++k
) {
2097 dpoly
fact(dim
, den
[k
], 1);
2100 if (num
.E
!= NULL
) {
2101 ZZ
one(INIT_VAL
, 1);
2102 dpoly_n
d(dim
, num
.constant
, one
);
2105 multi_polynom(c
, num
.E
, &EV
);
2107 free_evalue_refs(&EV
);
2108 free_evalue_refs(num
.E
);
2110 } else if (num
.pos
!= -1) {
2111 dpoly_n
d(dim
, num
.constant
, num
.coeff
);
2114 uni_polynom(num
.pos
, c
, &EV
);
2116 free_evalue_refs(&EV
);
2118 mpq_set_si(count
, 0, 1);
2119 dpoly
d(dim
, num
.constant
);
2120 d
.div(n
, count
, sign
);
2123 evalue_set(&EV
, &count
[0]._mp_num
, &count
[0]._mp_den
);
2125 free_evalue_refs(&EV
);
2129 evalue
* barvinok_enumerate_ev(Polyhedron
*P
, Polyhedron
* C
, unsigned MaxRays
)
2131 //P = unfringe(P, MaxRays);
2132 Polyhedron
*CEq
= NULL
, *rVD
, *pVD
, *CA
;
2134 Param_Polyhedron
*PP
= NULL
;
2135 Param_Domain
*D
, *next
;
2138 unsigned nparam
= C
->Dimension
;
2140 ALLOC(evalue
, eres
);
2141 value_init(eres
->d
);
2142 value_set_si(eres
->d
, 0);
2145 value_init(factor
.d
);
2146 evalue_set_si(&factor
, 1, 1);
2148 CA
= align_context(C
, P
->Dimension
, MaxRays
);
2149 P
= DomainIntersection(P
, CA
, MaxRays
);
2150 Polyhedron_Free(CA
);
2152 if (C
->Dimension
== 0 || emptyQ(P
)) {
2154 eres
->x
.p
= new_enode(partition
, 2, C
->Dimension
);
2155 EVALUE_SET_DOMAIN(eres
->x
.p
->arr
[0],
2156 DomainConstraintSimplify(CEq
? CEq
: Polyhedron_Copy(C
), MaxRays
));
2157 value_set_si(eres
->x
.p
->arr
[1].d
, 1);
2158 value_init(eres
->x
.p
->arr
[1].x
.n
);
2160 value_set_si(eres
->x
.p
->arr
[1].x
.n
, 0);
2162 barvinok_count(P
, &eres
->x
.p
->arr
[1].x
.n
, MaxRays
);
2164 emul(&factor
, eres
);
2165 reduce_evalue(eres
);
2166 free_evalue_refs(&factor
);
2171 Param_Polyhedron_Free(PP
);
2175 if (Polyhedron_is_infinite(P
, nparam
))
2180 P
= remove_equalities_p(P
, P
->Dimension
-nparam
, &f
);
2184 if (P
->Dimension
== nparam
) {
2186 P
= Universe_Polyhedron(0);
2190 Polyhedron
*Q
= ParamPolyhedron_Reduce(P
, P
->Dimension
-nparam
, &factor
);
2193 if (Q
->Dimension
== nparam
) {
2195 P
= Universe_Polyhedron(0);
2200 Polyhedron
*oldP
= P
;
2201 PP
= Polyhedron2Param_SimplifiedDomain(&P
,C
,MaxRays
,&CEq
,&CT
);
2203 Polyhedron_Free(oldP
);
2205 if (isIdentity(CT
)) {
2209 assert(CT
->NbRows
!= CT
->NbColumns
);
2210 if (CT
->NbRows
== 1) // no more parameters
2212 nparam
= CT
->NbRows
- 1;
2215 unsigned dim
= P
->Dimension
- nparam
;
2217 enumerator
et(P
, dim
, PP
->nbV
);
2220 for (nd
= 0, D
=PP
->D
; D
; ++nd
, D
=D
->next
);
2221 struct section
{ Polyhedron
*D
; evalue E
; };
2222 section
*s
= new section
[nd
];
2223 Polyhedron
**fVD
= new Polyhedron_p
[nd
];
2225 for(nd
= 0, D
=PP
->D
; D
; D
=next
) {
2228 Polyhedron
*rVD
= reduce_domain(D
->Domain
, CT
, CEq
,
2233 pVD
= CT
? DomainImage(rVD
,CT
,MaxRays
) : rVD
;
2235 value_init(s
[nd
].E
.d
);
2236 evalue_set_si(&s
[nd
].E
, 0, 1);
2238 FORALL_PVertex_in_ParamPolyhedron(V
,D
,PP
) // _i is internal counter
2240 et
.decompose_at(V
, _i
, MaxRays
);
2241 eadd(et
.vE
[_i
] , &s
[nd
].E
);
2242 END_FORALL_PVertex_in_ParamPolyhedron
;
2243 reduce_in_domain(&s
[nd
].E
, pVD
);
2246 addeliminatedparams_evalue(&s
[nd
].E
, CT
);
2254 evalue_set_si(eres
, 0, 1);
2256 eres
->x
.p
= new_enode(partition
, 2*nd
, C
->Dimension
);
2257 for (int j
= 0; j
< nd
; ++j
) {
2258 EVALUE_SET_DOMAIN(eres
->x
.p
->arr
[2*j
], s
[j
].D
);
2259 value_clear(eres
->x
.p
->arr
[2*j
+1].d
);
2260 eres
->x
.p
->arr
[2*j
+1] = s
[j
].E
;
2261 Domain_Free(fVD
[j
]);
2269 Polyhedron_Free(CEq
);
2274 Enumeration
* barvinok_enumerate(Polyhedron
*P
, Polyhedron
* C
, unsigned MaxRays
)
2276 evalue
*EP
= barvinok_enumerate_ev(P
, C
, MaxRays
);
2278 return partition2enumeration(EP
);
2281 static void SwapColumns(Value
**V
, int n
, int i
, int j
)
2283 for (int r
= 0; r
< n
; ++r
)
2284 value_swap(V
[r
][i
], V
[r
][j
]);
2287 static void SwapColumns(Polyhedron
*P
, int i
, int j
)
2289 SwapColumns(P
->Constraint
, P
->NbConstraints
, i
, j
);
2290 SwapColumns(P
->Ray
, P
->NbRays
, i
, j
);
2293 static void negative_test_constraint(Value
*l
, Value
*u
, Value
*c
, int pos
,
2296 value_oppose(*v
, u
[pos
+1]);
2297 Vector_Combine(l
+1, u
+1, c
+1, *v
, l
[pos
+1], len
-1);
2298 value_multiply(*v
, *v
, l
[pos
+1]);
2299 value_substract(c
[len
-1], c
[len
-1], *v
);
2300 value_set_si(*v
, -1);
2301 Vector_Scale(c
+1, c
+1, *v
, len
-1);
2302 value_decrement(c
[len
-1], c
[len
-1]);
2303 ConstraintSimplify(c
, c
, len
, v
);
2306 static bool parallel_constraints(Value
*l
, Value
*u
, Value
*c
, int pos
,
2315 Vector_Gcd(&l
[1+pos
], len
, &g1
);
2316 Vector_Gcd(&u
[1+pos
], len
, &g2
);
2317 Vector_Combine(l
+1+pos
, u
+1+pos
, c
+1, g2
, g1
, len
);
2318 parallel
= First_Non_Zero(c
+1, len
) == -1;
2326 static void negative_test_constraint7(Value
*l
, Value
*u
, Value
*c
, int pos
,
2327 int exist
, int len
, Value
*v
)
2332 Vector_Gcd(&u
[1+pos
], exist
, v
);
2333 Vector_Gcd(&l
[1+pos
], exist
, &g
);
2334 Vector_Combine(l
+1, u
+1, c
+1, *v
, g
, len
-1);
2335 value_multiply(*v
, *v
, g
);
2336 value_substract(c
[len
-1], c
[len
-1], *v
);
2337 value_set_si(*v
, -1);
2338 Vector_Scale(c
+1, c
+1, *v
, len
-1);
2339 value_decrement(c
[len
-1], c
[len
-1]);
2340 ConstraintSimplify(c
, c
, len
, v
);
2345 static void oppose_constraint(Value
*c
, int len
, Value
*v
)
2347 value_set_si(*v
, -1);
2348 Vector_Scale(c
+1, c
+1, *v
, len
-1);
2349 value_decrement(c
[len
-1], c
[len
-1]);
2352 static bool SplitOnConstraint(Polyhedron
*P
, int i
, int l
, int u
,
2353 int nvar
, int len
, int exist
, int MaxRays
,
2354 Vector
*row
, Value
& f
, bool independent
,
2355 Polyhedron
**pos
, Polyhedron
**neg
)
2357 negative_test_constraint(P
->Constraint
[l
], P
->Constraint
[u
],
2358 row
->p
, nvar
+i
, len
, &f
);
2359 *neg
= AddConstraints(row
->p
, 1, P
, MaxRays
);
2361 /* We found an independent, but useless constraint
2362 * Maybe we should detect this earlier and not
2363 * mark the variable as INDEPENDENT
2365 if (emptyQ((*neg
))) {
2366 Polyhedron_Free(*neg
);
2370 oppose_constraint(row
->p
, len
, &f
);
2371 *pos
= AddConstraints(row
->p
, 1, P
, MaxRays
);
2373 if (emptyQ((*pos
))) {
2374 Polyhedron_Free(*neg
);
2375 Polyhedron_Free(*pos
);
2383 * unimodularly transform P such that constraint r is transformed
2384 * into a constraint that involves only a single (the first)
2385 * existential variable
2388 static Polyhedron
*rotate_along(Polyhedron
*P
, int r
, int nvar
, int exist
,
2394 Vector
*row
= Vector_Alloc(exist
);
2395 Vector_Copy(P
->Constraint
[r
]+1+nvar
, row
->p
, exist
);
2396 Vector_Gcd(row
->p
, exist
, &g
);
2397 if (value_notone_p(g
))
2398 Vector_AntiScale(row
->p
, row
->p
, g
, exist
);
2401 Matrix
*M
= unimodular_complete(row
);
2402 Matrix
*M2
= Matrix_Alloc(P
->Dimension
+1, P
->Dimension
+1);
2403 for (r
= 0; r
< nvar
; ++r
)
2404 value_set_si(M2
->p
[r
][r
], 1);
2405 for ( ; r
< nvar
+exist
; ++r
)
2406 Vector_Copy(M
->p
[r
-nvar
], M2
->p
[r
]+nvar
, exist
);
2407 for ( ; r
< P
->Dimension
+1; ++r
)
2408 value_set_si(M2
->p
[r
][r
], 1);
2409 Polyhedron
*T
= Polyhedron_Image(P
, M2
, MaxRays
);
2418 static bool SplitOnVar(Polyhedron
*P
, int i
,
2419 int nvar
, int len
, int exist
, int MaxRays
,
2420 Vector
*row
, Value
& f
, bool independent
,
2421 Polyhedron
**pos
, Polyhedron
**neg
)
2425 for (int l
= P
->NbEq
; l
< P
->NbConstraints
; ++l
) {
2426 if (value_negz_p(P
->Constraint
[l
][nvar
+i
+1]))
2430 for (j
= 0; j
< exist
; ++j
)
2431 if (j
!= i
&& value_notzero_p(P
->Constraint
[l
][nvar
+j
+1]))
2437 for (int u
= P
->NbEq
; u
< P
->NbConstraints
; ++u
) {
2438 if (value_posz_p(P
->Constraint
[u
][nvar
+i
+1]))
2442 for (j
= 0; j
< exist
; ++j
)
2443 if (j
!= i
&& value_notzero_p(P
->Constraint
[u
][nvar
+j
+1]))
2449 if (SplitOnConstraint(P
, i
, l
, u
,
2450 nvar
, len
, exist
, MaxRays
,
2451 row
, f
, independent
,
2455 SwapColumns(*neg
, nvar
+1, nvar
+1+i
);
2465 static bool double_bound_pair(Polyhedron
*P
, int nvar
, int exist
,
2466 int i
, int l1
, int l2
,
2467 Polyhedron
**pos
, Polyhedron
**neg
)
2471 Vector
*row
= Vector_Alloc(P
->Dimension
+2);
2472 value_set_si(row
->p
[0], 1);
2473 value_oppose(f
, P
->Constraint
[l1
][nvar
+i
+1]);
2474 Vector_Combine(P
->Constraint
[l1
]+1, P
->Constraint
[l2
]+1,
2476 P
->Constraint
[l2
][nvar
+i
+1], f
,
2478 ConstraintSimplify(row
->p
, row
->p
, P
->Dimension
+2, &f
);
2479 *pos
= AddConstraints(row
->p
, 1, P
, 0);
2480 value_set_si(f
, -1);
2481 Vector_Scale(row
->p
+1, row
->p
+1, f
, P
->Dimension
+1);
2482 value_decrement(row
->p
[P
->Dimension
+1], row
->p
[P
->Dimension
+1]);
2483 *neg
= AddConstraints(row
->p
, 1, P
, 0);
2487 return !emptyQ((*pos
)) && !emptyQ((*neg
));
2490 static bool double_bound(Polyhedron
*P
, int nvar
, int exist
,
2491 Polyhedron
**pos
, Polyhedron
**neg
)
2493 for (int i
= 0; i
< exist
; ++i
) {
2495 for (l1
= P
->NbEq
; l1
< P
->NbConstraints
; ++l1
) {
2496 if (value_negz_p(P
->Constraint
[l1
][nvar
+i
+1]))
2498 for (l2
= l1
+ 1; l2
< P
->NbConstraints
; ++l2
) {
2499 if (value_negz_p(P
->Constraint
[l2
][nvar
+i
+1]))
2501 if (double_bound_pair(P
, nvar
, exist
, i
, l1
, l2
, pos
, neg
))
2505 for (l1
= P
->NbEq
; l1
< P
->NbConstraints
; ++l1
) {
2506 if (value_posz_p(P
->Constraint
[l1
][nvar
+i
+1]))
2508 if (l1
< P
->NbConstraints
)
2509 for (l2
= l1
+ 1; l2
< P
->NbConstraints
; ++l2
) {
2510 if (value_posz_p(P
->Constraint
[l2
][nvar
+i
+1]))
2512 if (double_bound_pair(P
, nvar
, exist
, i
, l1
, l2
, pos
, neg
))
2524 INDEPENDENT
= 1 << 2,
2528 static evalue
* enumerate_or(Polyhedron
*D
,
2529 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
2532 fprintf(stderr
, "\nER: Or\n");
2533 #endif /* DEBUG_ER */
2535 Polyhedron
*N
= D
->next
;
2538 barvinok_enumerate_e(D
, exist
, nparam
, MaxRays
);
2541 for (D
= N
; D
; D
= N
) {
2546 barvinok_enumerate_e(D
, exist
, nparam
, MaxRays
);
2549 free_evalue_refs(EN
);
2559 static evalue
* enumerate_sum(Polyhedron
*P
,
2560 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
2562 int nvar
= P
->Dimension
- exist
- nparam
;
2563 int toswap
= nvar
< exist
? nvar
: exist
;
2564 for (int i
= 0; i
< toswap
; ++i
)
2565 SwapColumns(P
, 1 + i
, nvar
+exist
- i
);
2569 fprintf(stderr
, "\nER: Sum\n");
2570 #endif /* DEBUG_ER */
2572 evalue
*EP
= barvinok_enumerate_e(P
, exist
, nparam
, MaxRays
);
2574 for (int i
= 0; i
< /* nvar */ nparam
; ++i
) {
2575 Matrix
*C
= Matrix_Alloc(1, 1 + nparam
+ 1);
2576 value_set_si(C
->p
[0][0], 1);
2578 value_init(split
.d
);
2579 value_set_si(split
.d
, 0);
2580 split
.x
.p
= new_enode(partition
, 4, nparam
);
2581 value_set_si(C
->p
[0][1+i
], 1);
2582 Matrix
*C2
= Matrix_Copy(C
);
2583 EVALUE_SET_DOMAIN(split
.x
.p
->arr
[0],
2584 Constraints2Polyhedron(C2
, MaxRays
));
2586 evalue_set_si(&split
.x
.p
->arr
[1], 1, 1);
2587 value_set_si(C
->p
[0][1+i
], -1);
2588 value_set_si(C
->p
[0][1+nparam
], -1);
2589 EVALUE_SET_DOMAIN(split
.x
.p
->arr
[2],
2590 Constraints2Polyhedron(C
, MaxRays
));
2591 evalue_set_si(&split
.x
.p
->arr
[3], 1, 1);
2593 free_evalue_refs(&split
);
2597 evalue_range_reduction(EP
);
2599 evalue_frac2floor(EP
);
2601 evalue
*sum
= esum(EP
, nvar
);
2603 free_evalue_refs(EP
);
2607 evalue_range_reduction(EP
);
2612 static evalue
* split_sure(Polyhedron
*P
, Polyhedron
*S
,
2613 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
2615 int nvar
= P
->Dimension
- exist
- nparam
;
2617 Matrix
*M
= Matrix_Alloc(exist
, S
->Dimension
+2);
2618 for (int i
= 0; i
< exist
; ++i
)
2619 value_set_si(M
->p
[i
][nvar
+i
+1], 1);
2621 S
= DomainAddRays(S
, M
, MaxRays
);
2623 Polyhedron
*F
= DomainAddRays(P
, M
, MaxRays
);
2624 Polyhedron
*D
= DomainDifference(F
, S
, MaxRays
);
2626 D
= Disjoint_Domain(D
, 0, MaxRays
);
2631 M
= Matrix_Alloc(P
->Dimension
+1-exist
, P
->Dimension
+1);
2632 for (int j
= 0; j
< nvar
; ++j
)
2633 value_set_si(M
->p
[j
][j
], 1);
2634 for (int j
= 0; j
< nparam
+1; ++j
)
2635 value_set_si(M
->p
[nvar
+j
][nvar
+exist
+j
], 1);
2636 Polyhedron
*T
= Polyhedron_Image(S
, M
, MaxRays
);
2637 evalue
*EP
= barvinok_enumerate_e(T
, 0, nparam
, MaxRays
);
2642 for (Polyhedron
*Q
= D
; Q
; Q
= Q
->next
) {
2643 Polyhedron
*N
= Q
->next
;
2645 T
= DomainIntersection(P
, Q
, MaxRays
);
2646 evalue
*E
= barvinok_enumerate_e(T
, exist
, nparam
, MaxRays
);
2648 free_evalue_refs(E
);
2657 static evalue
* enumerate_sure(Polyhedron
*P
,
2658 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
2662 int nvar
= P
->Dimension
- exist
- nparam
;
2668 for (i
= 0; i
< exist
; ++i
) {
2669 Matrix
*M
= Matrix_Alloc(S
->NbConstraints
, S
->Dimension
+2);
2671 value_set_si(lcm
, 1);
2672 for (int j
= 0; j
< S
->NbConstraints
; ++j
) {
2673 if (value_negz_p(S
->Constraint
[j
][1+nvar
+i
]))
2675 if (value_one_p(S
->Constraint
[j
][1+nvar
+i
]))
2677 value_lcm(lcm
, S
->Constraint
[j
][1+nvar
+i
], &lcm
);
2680 for (int j
= 0; j
< S
->NbConstraints
; ++j
) {
2681 if (value_negz_p(S
->Constraint
[j
][1+nvar
+i
]))
2683 if (value_one_p(S
->Constraint
[j
][1+nvar
+i
]))
2685 value_division(f
, lcm
, S
->Constraint
[j
][1+nvar
+i
]);
2686 Vector_Scale(S
->Constraint
[j
], M
->p
[c
], f
, S
->Dimension
+2);
2687 value_substract(M
->p
[c
][S
->Dimension
+1],
2688 M
->p
[c
][S
->Dimension
+1],
2690 value_increment(M
->p
[c
][S
->Dimension
+1],
2691 M
->p
[c
][S
->Dimension
+1]);
2695 S
= AddConstraints(M
->p
[0], c
, S
, MaxRays
);
2710 fprintf(stderr
, "\nER: Sure\n");
2711 #endif /* DEBUG_ER */
2713 return split_sure(P
, S
, exist
, nparam
, MaxRays
);
2716 static evalue
* enumerate_sure2(Polyhedron
*P
,
2717 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
2719 int nvar
= P
->Dimension
- exist
- nparam
;
2721 for (r
= 0; r
< P
->NbRays
; ++r
)
2722 if (value_one_p(P
->Ray
[r
][0]) &&
2723 value_one_p(P
->Ray
[r
][P
->Dimension
+1]))
2729 Matrix
*M
= Matrix_Alloc(nvar
+ 1 + nparam
, P
->Dimension
+2);
2730 for (int i
= 0; i
< nvar
; ++i
)
2731 value_set_si(M
->p
[i
][1+i
], 1);
2732 for (int i
= 0; i
< nparam
; ++i
)
2733 value_set_si(M
->p
[i
+nvar
][1+nvar
+exist
+i
], 1);
2734 Vector_Copy(P
->Ray
[r
]+1+nvar
, M
->p
[nvar
+nparam
]+1+nvar
, exist
);
2735 value_set_si(M
->p
[nvar
+nparam
][0], 1);
2736 value_set_si(M
->p
[nvar
+nparam
][P
->Dimension
+1], 1);
2737 Polyhedron
* F
= Rays2Polyhedron(M
, MaxRays
);
2740 Polyhedron
*I
= DomainIntersection(F
, P
, MaxRays
);
2744 fprintf(stderr
, "\nER: Sure2\n");
2745 #endif /* DEBUG_ER */
2747 return split_sure(P
, I
, exist
, nparam
, MaxRays
);
2750 static evalue
* enumerate_cyclic(Polyhedron
*P
,
2751 unsigned exist
, unsigned nparam
,
2752 evalue
* EP
, int r
, int p
, unsigned MaxRays
)
2754 int nvar
= P
->Dimension
- exist
- nparam
;
2756 /* If EP in its fractional maps only contains references
2757 * to the remainder parameter with appropriate coefficients
2758 * then we could in principle avoid adding existentially
2759 * quantified variables to the validity domains.
2760 * We'd have to replace the remainder by m { p/m }
2761 * and multiply with an appropriate factor that is one
2762 * only in the appropriate range.
2763 * This last multiplication can be avoided if EP
2764 * has a single validity domain with no (further)
2765 * constraints on the remainder parameter
2768 Matrix
*CT
= Matrix_Alloc(nparam
+1, nparam
+3);
2769 Matrix
*M
= Matrix_Alloc(1, 1+nparam
+3);
2770 for (int j
= 0; j
< nparam
; ++j
)
2772 value_set_si(CT
->p
[j
][j
], 1);
2773 value_set_si(CT
->p
[p
][nparam
+1], 1);
2774 value_set_si(CT
->p
[nparam
][nparam
+2], 1);
2775 value_set_si(M
->p
[0][1+p
], -1);
2776 value_absolute(M
->p
[0][1+nparam
], P
->Ray
[0][1+nvar
+exist
+p
]);
2777 value_set_si(M
->p
[0][1+nparam
+1], 1);
2778 Polyhedron
*CEq
= Constraints2Polyhedron(M
, 1);
2780 addeliminatedparams_enum(EP
, CT
, CEq
, MaxRays
, nparam
);
2781 Polyhedron_Free(CEq
);
2787 static void enumerate_vd_add_ray(evalue
*EP
, Matrix
*Rays
, unsigned MaxRays
)
2789 if (value_notzero_p(EP
->d
))
2792 assert(EP
->x
.p
->type
== partition
);
2793 assert(EP
->x
.p
->pos
== EVALUE_DOMAIN(EP
->x
.p
->arr
[0])->Dimension
);
2794 for (int i
= 0; i
< EP
->x
.p
->size
/2; ++i
) {
2795 Polyhedron
*D
= EVALUE_DOMAIN(EP
->x
.p
->arr
[2*i
]);
2796 Polyhedron
*N
= DomainAddRays(D
, Rays
, MaxRays
);
2797 EVALUE_SET_DOMAIN(EP
->x
.p
->arr
[2*i
], N
);
2802 static evalue
* enumerate_line(Polyhedron
*P
,
2803 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
2809 fprintf(stderr
, "\nER: Line\n");
2810 #endif /* DEBUG_ER */
2812 int nvar
= P
->Dimension
- exist
- nparam
;
2814 for (i
= 0; i
< nparam
; ++i
)
2815 if (value_notzero_p(P
->Ray
[0][1+nvar
+exist
+i
]))
2818 for (j
= i
+1; j
< nparam
; ++j
)
2819 if (value_notzero_p(P
->Ray
[0][1+nvar
+exist
+i
]))
2821 assert(j
>= nparam
); // for now
2823 Matrix
*M
= Matrix_Alloc(2, P
->Dimension
+2);
2824 value_set_si(M
->p
[0][0], 1);
2825 value_set_si(M
->p
[0][1+nvar
+exist
+i
], 1);
2826 value_set_si(M
->p
[1][0], 1);
2827 value_set_si(M
->p
[1][1+nvar
+exist
+i
], -1);
2828 value_absolute(M
->p
[1][1+P
->Dimension
], P
->Ray
[0][1+nvar
+exist
+i
]);
2829 value_decrement(M
->p
[1][1+P
->Dimension
], M
->p
[1][1+P
->Dimension
]);
2830 Polyhedron
*S
= AddConstraints(M
->p
[0], 2, P
, MaxRays
);
2831 evalue
*EP
= barvinok_enumerate_e(S
, exist
, nparam
, MaxRays
);
2835 return enumerate_cyclic(P
, exist
, nparam
, EP
, 0, i
, MaxRays
);
2838 static int single_param_pos(Polyhedron
*P
, unsigned exist
, unsigned nparam
,
2841 int nvar
= P
->Dimension
- exist
- nparam
;
2842 if (First_Non_Zero(P
->Ray
[r
]+1, nvar
) != -1)
2844 int i
= First_Non_Zero(P
->Ray
[r
]+1+nvar
+exist
, nparam
);
2847 if (First_Non_Zero(P
->Ray
[r
]+1+nvar
+exist
+1, nparam
-i
-1) != -1)
2852 static evalue
* enumerate_remove_ray(Polyhedron
*P
, int r
,
2853 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
2856 fprintf(stderr
, "\nER: RedundantRay\n");
2857 #endif /* DEBUG_ER */
2861 value_set_si(one
, 1);
2862 int len
= P
->NbRays
-1;
2863 Matrix
*M
= Matrix_Alloc(2 * len
, P
->Dimension
+2);
2864 Vector_Copy(P
->Ray
[0], M
->p
[0], r
* (P
->Dimension
+2));
2865 Vector_Copy(P
->Ray
[r
+1], M
->p
[r
], (len
-r
) * (P
->Dimension
+2));
2866 for (int j
= 0; j
< P
->NbRays
; ++j
) {
2869 Vector_Combine(P
->Ray
[j
], P
->Ray
[r
], M
->p
[len
+j
-(j
>r
)],
2870 one
, P
->Ray
[j
][P
->Dimension
+1], P
->Dimension
+2);
2873 P
= Rays2Polyhedron(M
, MaxRays
);
2875 evalue
*EP
= barvinok_enumerate_e(P
, exist
, nparam
, MaxRays
);
2882 static evalue
* enumerate_redundant_ray(Polyhedron
*P
,
2883 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
2885 assert(P
->NbBid
== 0);
2886 int nvar
= P
->Dimension
- exist
- nparam
;
2890 for (int r
= 0; r
< P
->NbRays
; ++r
) {
2891 if (value_notzero_p(P
->Ray
[r
][P
->Dimension
+1]))
2893 int i1
= single_param_pos(P
, exist
, nparam
, r
);
2896 for (int r2
= r
+1; r2
< P
->NbRays
; ++r2
) {
2897 if (value_notzero_p(P
->Ray
[r2
][P
->Dimension
+1]))
2899 int i2
= single_param_pos(P
, exist
, nparam
, r2
);
2905 value_division(m
, P
->Ray
[r
][1+nvar
+exist
+i1
],
2906 P
->Ray
[r2
][1+nvar
+exist
+i1
]);
2907 value_multiply(m
, m
, P
->Ray
[r2
][1+nvar
+exist
+i1
]);
2908 /* r2 divides r => r redundant */
2909 if (value_eq(m
, P
->Ray
[r
][1+nvar
+exist
+i1
])) {
2911 return enumerate_remove_ray(P
, r
, exist
, nparam
, MaxRays
);
2914 value_division(m
, P
->Ray
[r2
][1+nvar
+exist
+i1
],
2915 P
->Ray
[r
][1+nvar
+exist
+i1
]);
2916 value_multiply(m
, m
, P
->Ray
[r
][1+nvar
+exist
+i1
]);
2917 /* r divides r2 => r2 redundant */
2918 if (value_eq(m
, P
->Ray
[r2
][1+nvar
+exist
+i1
])) {
2920 return enumerate_remove_ray(P
, r2
, exist
, nparam
, MaxRays
);
2928 static Polyhedron
*upper_bound(Polyhedron
*P
,
2929 int pos
, Value
*max
, Polyhedron
**R
)
2938 for (Polyhedron
*Q
= P
; Q
; Q
= N
) {
2940 for (r
= 0; r
< P
->NbRays
; ++r
) {
2941 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1]) &&
2942 value_pos_p(P
->Ray
[r
][1+pos
]))
2945 if (r
< P
->NbRays
) {
2953 for (r
= 0; r
< P
->NbRays
; ++r
) {
2954 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1]))
2956 mpz_fdiv_q(v
, P
->Ray
[r
][1+pos
], P
->Ray
[r
][1+P
->Dimension
]);
2957 if ((!Q
->next
&& r
== 0) || value_gt(v
, *max
))
2958 value_assign(*max
, v
);
2965 static evalue
* enumerate_ray(Polyhedron
*P
,
2966 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
2968 assert(P
->NbBid
== 0);
2969 int nvar
= P
->Dimension
- exist
- nparam
;
2972 for (r
= 0; r
< P
->NbRays
; ++r
)
2973 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1]))
2979 for (r2
= r
+1; r2
< P
->NbRays
; ++r2
)
2980 if (value_zero_p(P
->Ray
[r2
][P
->Dimension
+1]))
2982 if (r2
< P
->NbRays
) {
2984 return enumerate_sum(P
, exist
, nparam
, MaxRays
);
2988 fprintf(stderr
, "\nER: Ray\n");
2989 #endif /* DEBUG_ER */
2995 value_set_si(one
, 1);
2996 int i
= single_param_pos(P
, exist
, nparam
, r
);
2997 assert(i
!= -1); // for now;
2999 Matrix
*M
= Matrix_Alloc(P
->NbRays
, P
->Dimension
+2);
3000 for (int j
= 0; j
< P
->NbRays
; ++j
) {
3001 Vector_Combine(P
->Ray
[j
], P
->Ray
[r
], M
->p
[j
],
3002 one
, P
->Ray
[j
][P
->Dimension
+1], P
->Dimension
+2);
3004 Polyhedron
*S
= Rays2Polyhedron(M
, MaxRays
);
3006 Polyhedron
*D
= DomainDifference(P
, S
, MaxRays
);
3008 // Polyhedron_Print(stderr, P_VALUE_FMT, D);
3009 assert(value_pos_p(P
->Ray
[r
][1+nvar
+exist
+i
])); // for now
3011 D
= upper_bound(D
, nvar
+exist
+i
, &m
, &R
);
3015 M
= Matrix_Alloc(2, P
->Dimension
+2);
3016 value_set_si(M
->p
[0][0], 1);
3017 value_set_si(M
->p
[1][0], 1);
3018 value_set_si(M
->p
[0][1+nvar
+exist
+i
], -1);
3019 value_set_si(M
->p
[1][1+nvar
+exist
+i
], 1);
3020 value_assign(M
->p
[0][1+P
->Dimension
], m
);
3021 value_oppose(M
->p
[1][1+P
->Dimension
], m
);
3022 value_addto(M
->p
[1][1+P
->Dimension
], M
->p
[1][1+P
->Dimension
],
3023 P
->Ray
[r
][1+nvar
+exist
+i
]);
3024 value_decrement(M
->p
[1][1+P
->Dimension
], M
->p
[1][1+P
->Dimension
]);
3025 // Matrix_Print(stderr, P_VALUE_FMT, M);
3026 D
= AddConstraints(M
->p
[0], 2, P
, MaxRays
);
3027 // Polyhedron_Print(stderr, P_VALUE_FMT, D);
3028 value_substract(M
->p
[0][1+P
->Dimension
], M
->p
[0][1+P
->Dimension
],
3029 P
->Ray
[r
][1+nvar
+exist
+i
]);
3030 // Matrix_Print(stderr, P_VALUE_FMT, M);
3031 S
= AddConstraints(M
->p
[0], 1, P
, MaxRays
);
3032 // Polyhedron_Print(stderr, P_VALUE_FMT, S);
3035 evalue
*EP
= barvinok_enumerate_e(D
, exist
, nparam
, MaxRays
);
3040 if (value_notone_p(P
->Ray
[r
][1+nvar
+exist
+i
]))
3041 EP
= enumerate_cyclic(P
, exist
, nparam
, EP
, r
, i
, MaxRays
);
3043 M
= Matrix_Alloc(1, nparam
+2);
3044 value_set_si(M
->p
[0][0], 1);
3045 value_set_si(M
->p
[0][1+i
], 1);
3046 enumerate_vd_add_ray(EP
, M
, MaxRays
);
3051 evalue
*E
= barvinok_enumerate_e(S
, exist
, nparam
, MaxRays
);
3053 free_evalue_refs(E
);
3060 evalue
*ER
= enumerate_or(R
, exist
, nparam
, MaxRays
);
3062 free_evalue_refs(ER
);
3069 static evalue
* new_zero_ep()
3074 evalue_set_si(EP
, 0, 1);
3078 static evalue
* enumerate_vd(Polyhedron
**PA
,
3079 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
3081 Polyhedron
*P
= *PA
;
3082 int nvar
= P
->Dimension
- exist
- nparam
;
3083 Param_Polyhedron
*PP
= NULL
;
3084 Polyhedron
*C
= Universe_Polyhedron(nparam
);
3088 PP
= Polyhedron2Param_SimplifiedDomain(&PR
,C
,MaxRays
,&CEq
,&CT
);
3092 Param_Domain
*D
, *last
;
3095 for (nd
= 0, D
=PP
->D
; D
; D
=D
->next
, ++nd
)
3098 Polyhedron
**VD
= new Polyhedron_p
[nd
];
3099 Polyhedron
**fVD
= new Polyhedron_p
[nd
];
3100 for(nd
= 0, D
=PP
->D
; D
; D
=D
->next
) {
3101 Polyhedron
*rVD
= reduce_domain(D
->Domain
, CT
, CEq
,
3115 /* This doesn't seem to have any effect */
3117 Polyhedron
*CA
= align_context(VD
[0], P
->Dimension
, MaxRays
);
3119 P
= DomainIntersection(P
, CA
, MaxRays
);
3122 Polyhedron_Free(CA
);
3127 if (!EP
&& CT
->NbColumns
!= CT
->NbRows
) {
3128 Polyhedron
*CEqr
= DomainImage(CEq
, CT
, MaxRays
);
3129 Polyhedron
*CA
= align_context(CEqr
, PR
->Dimension
, MaxRays
);
3130 Polyhedron
*I
= DomainIntersection(PR
, CA
, MaxRays
);
3131 Polyhedron_Free(CEqr
);
3132 Polyhedron_Free(CA
);
3134 fprintf(stderr
, "\nER: Eliminate\n");
3135 #endif /* DEBUG_ER */
3136 nparam
-= CT
->NbColumns
- CT
->NbRows
;
3137 EP
= barvinok_enumerate_e(I
, exist
, nparam
, MaxRays
);
3138 nparam
+= CT
->NbColumns
- CT
->NbRows
;
3139 addeliminatedparams_enum(EP
, CT
, CEq
, MaxRays
, nparam
);
3143 Polyhedron_Free(PR
);
3146 if (!EP
&& nd
> 1) {
3148 fprintf(stderr
, "\nER: VD\n");
3149 #endif /* DEBUG_ER */
3150 for (int i
= 0; i
< nd
; ++i
) {
3151 Polyhedron
*CA
= align_context(VD
[i
], P
->Dimension
, MaxRays
);
3152 Polyhedron
*I
= DomainIntersection(P
, CA
, MaxRays
);
3155 EP
= barvinok_enumerate_e(I
, exist
, nparam
, MaxRays
);
3157 evalue
*E
= barvinok_enumerate_e(I
, exist
, nparam
, MaxRays
);
3159 free_evalue_refs(E
);
3163 Polyhedron_Free(CA
);
3167 for (int i
= 0; i
< nd
; ++i
) {
3168 Polyhedron_Free(VD
[i
]);
3169 Polyhedron_Free(fVD
[i
]);
3175 if (!EP
&& nvar
== 0) {
3178 Param_Vertices
*V
, *V2
;
3179 Matrix
* M
= Matrix_Alloc(1, P
->Dimension
+2);
3181 FORALL_PVertex_in_ParamPolyhedron(V
, last
, PP
) {
3183 FORALL_PVertex_in_ParamPolyhedron(V2
, last
, PP
) {
3190 for (int i
= 0; i
< exist
; ++i
) {
3191 value_oppose(f
, V
->Vertex
->p
[i
][nparam
+1]);
3192 Vector_Combine(V
->Vertex
->p
[i
],
3194 M
->p
[0] + 1 + nvar
+ exist
,
3195 V2
->Vertex
->p
[i
][nparam
+1],
3199 for (j
= 0; j
< nparam
; ++j
)
3200 if (value_notzero_p(M
->p
[0][1+nvar
+exist
+j
]))
3204 ConstraintSimplify(M
->p
[0], M
->p
[0],
3205 P
->Dimension
+2, &f
);
3206 value_set_si(M
->p
[0][0], 0);
3207 Polyhedron
*para
= AddConstraints(M
->p
[0], 1, P
,
3210 Polyhedron_Free(para
);
3213 Polyhedron
*pos
, *neg
;
3214 value_set_si(M
->p
[0][0], 1);
3215 value_decrement(M
->p
[0][P
->Dimension
+1],
3216 M
->p
[0][P
->Dimension
+1]);
3217 neg
= AddConstraints(M
->p
[0], 1, P
, MaxRays
);
3218 value_set_si(f
, -1);
3219 Vector_Scale(M
->p
[0]+1, M
->p
[0]+1, f
,
3221 value_decrement(M
->p
[0][P
->Dimension
+1],
3222 M
->p
[0][P
->Dimension
+1]);
3223 value_decrement(M
->p
[0][P
->Dimension
+1],
3224 M
->p
[0][P
->Dimension
+1]);
3225 pos
= AddConstraints(M
->p
[0], 1, P
, MaxRays
);
3226 if (emptyQ(neg
) && emptyQ(pos
)) {
3227 Polyhedron_Free(para
);
3228 Polyhedron_Free(pos
);
3229 Polyhedron_Free(neg
);
3233 fprintf(stderr
, "\nER: Order\n");
3234 #endif /* DEBUG_ER */
3235 EP
= barvinok_enumerate_e(para
, exist
, nparam
, MaxRays
);
3238 E
= barvinok_enumerate_e(pos
, exist
, nparam
, MaxRays
);
3240 free_evalue_refs(E
);
3244 E
= barvinok_enumerate_e(neg
, exist
, nparam
, MaxRays
);
3246 free_evalue_refs(E
);
3249 Polyhedron_Free(para
);
3250 Polyhedron_Free(pos
);
3251 Polyhedron_Free(neg
);
3256 } END_FORALL_PVertex_in_ParamPolyhedron
;
3259 } END_FORALL_PVertex_in_ParamPolyhedron
;
3262 /* Search for vertex coordinate to split on */
3263 /* First look for one independent of the parameters */
3264 FORALL_PVertex_in_ParamPolyhedron(V
, last
, PP
) {
3265 for (int i
= 0; i
< exist
; ++i
) {
3267 for (j
= 0; j
< nparam
; ++j
)
3268 if (value_notzero_p(V
->Vertex
->p
[i
][j
]))
3272 value_set_si(M
->p
[0][0], 1);
3273 Vector_Set(M
->p
[0]+1, 0, nvar
+exist
);
3274 Vector_Copy(V
->Vertex
->p
[i
],
3275 M
->p
[0] + 1 + nvar
+ exist
, nparam
+1);
3276 value_oppose(M
->p
[0][1+nvar
+i
],
3277 V
->Vertex
->p
[i
][nparam
+1]);
3279 Polyhedron
*pos
, *neg
;
3280 value_set_si(M
->p
[0][0], 1);
3281 value_decrement(M
->p
[0][P
->Dimension
+1],
3282 M
->p
[0][P
->Dimension
+1]);
3283 neg
= AddConstraints(M
->p
[0], 1, P
, MaxRays
);
3284 value_set_si(f
, -1);
3285 Vector_Scale(M
->p
[0]+1, M
->p
[0]+1, f
,
3287 value_decrement(M
->p
[0][P
->Dimension
+1],
3288 M
->p
[0][P
->Dimension
+1]);
3289 value_decrement(M
->p
[0][P
->Dimension
+1],
3290 M
->p
[0][P
->Dimension
+1]);
3291 pos
= AddConstraints(M
->p
[0], 1, P
, MaxRays
);
3292 if (emptyQ(neg
) || emptyQ(pos
)) {
3293 Polyhedron_Free(pos
);
3294 Polyhedron_Free(neg
);
3297 Polyhedron_Free(pos
);
3298 value_increment(M
->p
[0][P
->Dimension
+1],
3299 M
->p
[0][P
->Dimension
+1]);
3300 pos
= AddConstraints(M
->p
[0], 1, P
, MaxRays
);
3302 fprintf(stderr
, "\nER: Vertex\n");
3303 #endif /* DEBUG_ER */
3305 EP
= enumerate_or(pos
, exist
, nparam
, MaxRays
);
3310 } END_FORALL_PVertex_in_ParamPolyhedron
;
3314 /* Search for vertex coordinate to split on */
3315 /* Now look for one that depends on the parameters */
3316 FORALL_PVertex_in_ParamPolyhedron(V
, last
, PP
) {
3317 for (int i
= 0; i
< exist
; ++i
) {
3318 value_set_si(M
->p
[0][0], 1);
3319 Vector_Set(M
->p
[0]+1, 0, nvar
+exist
);
3320 Vector_Copy(V
->Vertex
->p
[i
],
3321 M
->p
[0] + 1 + nvar
+ exist
, nparam
+1);
3322 value_oppose(M
->p
[0][1+nvar
+i
],
3323 V
->Vertex
->p
[i
][nparam
+1]);
3325 Polyhedron
*pos
, *neg
;
3326 value_set_si(M
->p
[0][0], 1);
3327 value_decrement(M
->p
[0][P
->Dimension
+1],
3328 M
->p
[0][P
->Dimension
+1]);
3329 neg
= AddConstraints(M
->p
[0], 1, P
, MaxRays
);
3330 value_set_si(f
, -1);
3331 Vector_Scale(M
->p
[0]+1, M
->p
[0]+1, f
,
3333 value_decrement(M
->p
[0][P
->Dimension
+1],
3334 M
->p
[0][P
->Dimension
+1]);
3335 value_decrement(M
->p
[0][P
->Dimension
+1],
3336 M
->p
[0][P
->Dimension
+1]);
3337 pos
= AddConstraints(M
->p
[0], 1, P
, MaxRays
);
3338 if (emptyQ(neg
) || emptyQ(pos
)) {
3339 Polyhedron_Free(pos
);
3340 Polyhedron_Free(neg
);
3343 Polyhedron_Free(pos
);
3344 value_increment(M
->p
[0][P
->Dimension
+1],
3345 M
->p
[0][P
->Dimension
+1]);
3346 pos
= AddConstraints(M
->p
[0], 1, P
, MaxRays
);
3348 fprintf(stderr
, "\nER: ParamVertex\n");
3349 #endif /* DEBUG_ER */
3351 EP
= enumerate_or(pos
, exist
, nparam
, MaxRays
);
3356 } END_FORALL_PVertex_in_ParamPolyhedron
;
3364 Polyhedron_Free(CEq
);
3368 Param_Polyhedron_Free(PP
);
3375 evalue
*barvinok_enumerate_pip(Polyhedron
*P
,
3376 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
3381 evalue
*barvinok_enumerate_pip(Polyhedron
*P
,
3382 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
3384 int nvar
= P
->Dimension
- exist
- nparam
;
3385 evalue
*EP
= new_zero_ep();
3386 Polyhedron
*Q
, *N
, *T
= 0;
3392 fprintf(stderr
, "\nER: PIP\n");
3393 #endif /* DEBUG_ER */
3395 for (int i
= 0; i
< P
->Dimension
; ++i
) {
3398 bool posray
= false;
3399 bool negray
= false;
3400 value_set_si(min
, 0);
3401 for (int j
= 0; j
< P
->NbRays
; ++j
) {
3402 if (value_pos_p(P
->Ray
[j
][1+i
])) {
3404 if (value_zero_p(P
->Ray
[j
][1+P
->Dimension
]))
3406 } else if (value_neg_p(P
->Ray
[j
][1+i
])) {
3408 if (value_zero_p(P
->Ray
[j
][1+P
->Dimension
]))
3412 P
->Ray
[j
][1+i
], P
->Ray
[j
][1+P
->Dimension
]);
3413 if (value_lt(tmp
, min
))
3414 value_assign(min
, tmp
);
3419 assert(!(posray
&& negray
));
3420 assert(!negray
); // for now
3421 Polyhedron
*O
= T
? T
: P
;
3422 /* shift by a safe amount */
3423 Matrix
*M
= Matrix_Alloc(O
->NbRays
, O
->Dimension
+2);
3424 Vector_Copy(O
->Ray
[0], M
->p
[0], O
->NbRays
* (O
->Dimension
+2));
3425 for (int j
= 0; j
< P
->NbRays
; ++j
) {
3426 if (value_notzero_p(M
->p
[j
][1+P
->Dimension
])) {
3427 value_multiply(tmp
, min
, M
->p
[j
][1+P
->Dimension
]);
3428 value_substract(M
->p
[j
][1+i
], M
->p
[j
][1+i
], tmp
);
3433 T
= Rays2Polyhedron(M
, MaxRays
);
3436 /* negating a parameter requires that we substitute in the
3437 * sign again afterwards.
3440 assert(i
< nvar
+exist
);
3442 T
= Polyhedron_Copy(P
);
3443 for (int j
= 0; j
< T
->NbRays
; ++j
)
3444 value_oppose(T
->Ray
[j
][1+i
], T
->Ray
[j
][1+i
]);
3445 for (int j
= 0; j
< T
->NbConstraints
; ++j
)
3446 value_oppose(T
->Constraint
[j
][1+i
], T
->Constraint
[j
][1+i
]);
3452 Polyhedron
*D
= pip_lexmin(T
? T
: P
, exist
, nparam
);
3453 for (Q
= D
; Q
; Q
= N
) {
3457 exist
= Q
->Dimension
- nvar
- nparam
;
3458 E
= barvinok_enumerate_e(Q
, exist
, nparam
, MaxRays
);
3461 free_evalue_refs(E
);
3473 static bool is_single(Value
*row
, int pos
, int len
)
3475 return First_Non_Zero(row
, pos
) == -1 &&
3476 First_Non_Zero(row
+pos
+1, len
-pos
-1) == -1;
3479 static evalue
* barvinok_enumerate_e_r(Polyhedron
*P
,
3480 unsigned exist
, unsigned nparam
, unsigned MaxRays
);
3483 static int er_level
= 0;
3485 evalue
* barvinok_enumerate_e(Polyhedron
*P
,
3486 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
3488 fprintf(stderr
, "\nER: level %i\n", er_level
);
3489 int nvar
= P
->Dimension
- exist
- nparam
;
3490 fprintf(stderr
, "%d %d %d\n", nvar
, exist
, nparam
);
3492 Polyhedron_Print(stderr
, P_VALUE_FMT
, P
);
3494 P
= DomainConstraintSimplify(Polyhedron_Copy(P
), MaxRays
);
3495 evalue
*EP
= barvinok_enumerate_e_r(P
, exist
, nparam
, MaxRays
);
3501 evalue
* barvinok_enumerate_e(Polyhedron
*P
,
3502 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
3504 P
= DomainConstraintSimplify(Polyhedron_Copy(P
), MaxRays
);
3505 evalue
*EP
= barvinok_enumerate_e_r(P
, exist
, nparam
, MaxRays
);
3511 static evalue
* barvinok_enumerate_e_r(Polyhedron
*P
,
3512 unsigned exist
, unsigned nparam
, unsigned MaxRays
)
3515 Polyhedron
*U
= Universe_Polyhedron(nparam
);
3516 evalue
*EP
= barvinok_enumerate_ev(P
, U
, MaxRays
);
3517 //char *param_name[] = {"P", "Q", "R", "S", "T" };
3518 //print_evalue(stdout, EP, param_name);
3523 int nvar
= P
->Dimension
- exist
- nparam
;
3524 int len
= P
->Dimension
+ 2;
3527 return new_zero_ep();
3529 if (nvar
== 0 && nparam
== 0) {
3530 evalue
*EP
= new_zero_ep();
3531 barvinok_count(P
, &EP
->x
.n
, MaxRays
);
3532 if (value_pos_p(EP
->x
.n
))
3533 value_set_si(EP
->x
.n
, 1);
3538 for (r
= 0; r
< P
->NbRays
; ++r
)
3539 if (value_zero_p(P
->Ray
[r
][0]) ||
3540 value_zero_p(P
->Ray
[r
][P
->Dimension
+1])) {
3542 for (i
= 0; i
< nvar
; ++i
)
3543 if (value_notzero_p(P
->Ray
[r
][i
+1]))
3547 for (i
= nvar
+ exist
; i
< nvar
+ exist
+ nparam
; ++i
)
3548 if (value_notzero_p(P
->Ray
[r
][i
+1]))
3550 if (i
>= nvar
+ exist
+ nparam
)
3553 if (r
< P
->NbRays
) {
3554 evalue
*EP
= new_zero_ep();
3555 value_set_si(EP
->x
.n
, -1);
3560 for (r
= 0; r
< P
->NbEq
; ++r
)
3561 if ((first
= First_Non_Zero(P
->Constraint
[r
]+1+nvar
, exist
)) != -1)
3564 if (First_Non_Zero(P
->Constraint
[r
]+1+nvar
+first
+1,
3565 exist
-first
-1) != -1) {
3566 Polyhedron
*T
= rotate_along(P
, r
, nvar
, exist
, MaxRays
);
3568 fprintf(stderr
, "\nER: Equality\n");
3569 #endif /* DEBUG_ER */
3570 evalue
*EP
= barvinok_enumerate_e(T
, exist
-1, nparam
, MaxRays
);
3575 fprintf(stderr
, "\nER: Fixed\n");
3576 #endif /* DEBUG_ER */
3578 return barvinok_enumerate_e(P
, exist
-1, nparam
, MaxRays
);
3580 Polyhedron
*T
= Polyhedron_Copy(P
);
3581 SwapColumns(T
, nvar
+1, nvar
+1+first
);
3582 evalue
*EP
= barvinok_enumerate_e(T
, exist
-1, nparam
, MaxRays
);
3589 Vector
*row
= Vector_Alloc(len
);
3590 value_set_si(row
->p
[0], 1);
3595 enum constraint
* info
= new constraint
[exist
];
3596 for (int i
= 0; i
< exist
; ++i
) {
3598 for (int l
= P
->NbEq
; l
< P
->NbConstraints
; ++l
) {
3599 if (value_negz_p(P
->Constraint
[l
][nvar
+i
+1]))
3601 bool l_parallel
= is_single(P
->Constraint
[l
]+nvar
+1, i
, exist
);
3602 for (int u
= P
->NbEq
; u
< P
->NbConstraints
; ++u
) {
3603 if (value_posz_p(P
->Constraint
[u
][nvar
+i
+1]))
3605 bool lu_parallel
= l_parallel
||
3606 is_single(P
->Constraint
[u
]+nvar
+1, i
, exist
);
3607 value_oppose(f
, P
->Constraint
[u
][nvar
+i
+1]);
3608 Vector_Combine(P
->Constraint
[l
]+1, P
->Constraint
[u
]+1, row
->p
+1,
3609 f
, P
->Constraint
[l
][nvar
+i
+1], len
-1);
3610 if (!(info
[i
] & INDEPENDENT
)) {
3612 for (j
= 0; j
< exist
; ++j
)
3613 if (j
!= i
&& value_notzero_p(row
->p
[nvar
+j
+1]))
3616 //printf("independent: i: %d, l: %d, u: %d\n", i, l, u);
3617 info
[i
] = (constraint
)(info
[i
] | INDEPENDENT
);
3620 if (info
[i
] & ALL_POS
) {
3621 value_addto(row
->p
[len
-1], row
->p
[len
-1],
3622 P
->Constraint
[l
][nvar
+i
+1]);
3623 value_addto(row
->p
[len
-1], row
->p
[len
-1], f
);
3624 value_multiply(f
, f
, P
->Constraint
[l
][nvar
+i
+1]);
3625 value_substract(row
->p
[len
-1], row
->p
[len
-1], f
);
3626 value_decrement(row
->p
[len
-1], row
->p
[len
-1]);
3627 ConstraintSimplify(row
->p
, row
->p
, len
, &f
);
3628 value_set_si(f
, -1);
3629 Vector_Scale(row
->p
+1, row
->p
+1, f
, len
-1);
3630 value_decrement(row
->p
[len
-1], row
->p
[len
-1]);
3631 Polyhedron
*T
= AddConstraints(row
->p
, 1, P
, MaxRays
);
3633 //printf("not all_pos: i: %d, l: %d, u: %d\n", i, l, u);
3634 info
[i
] = (constraint
)(info
[i
] ^ ALL_POS
);
3636 //puts("pos remainder");
3637 //Polyhedron_Print(stdout, P_VALUE_FMT, T);
3640 if (!(info
[i
] & ONE_NEG
)) {
3642 negative_test_constraint(P
->Constraint
[l
],
3644 row
->p
, nvar
+i
, len
, &f
);
3645 oppose_constraint(row
->p
, len
, &f
);
3646 Polyhedron
*T
= AddConstraints(row
->p
, 1, P
, MaxRays
);
3648 //printf("one_neg i: %d, l: %d, u: %d\n", i, l, u);
3649 info
[i
] = (constraint
)(info
[i
] | ONE_NEG
);
3651 //puts("neg remainder");
3652 //Polyhedron_Print(stdout, P_VALUE_FMT, T);
3654 } else if (!(info
[i
] & ROT_NEG
)) {
3655 if (parallel_constraints(P
->Constraint
[l
],
3657 row
->p
, nvar
, exist
)) {
3658 negative_test_constraint7(P
->Constraint
[l
],
3660 row
->p
, nvar
, exist
,
3662 oppose_constraint(row
->p
, len
, &f
);
3663 Polyhedron
*T
= AddConstraints(row
->p
, 1, P
, MaxRays
);
3665 // printf("rot_neg i: %d, l: %d, u: %d\n", i, l, u);
3666 info
[i
] = (constraint
)(info
[i
] | ROT_NEG
);
3669 //puts("neg remainder");
3670 //Polyhedron_Print(stdout, P_VALUE_FMT, T);
3675 if (!(info
[i
] & ALL_POS
) && (info
[i
] & (ONE_NEG
| ROT_NEG
)))
3679 if (info
[i
] & ALL_POS
)
3686 for (int i = 0; i < exist; ++i)
3687 printf("%i: %i\n", i, info[i]);
3689 for (int i
= 0; i
< exist
; ++i
)
3690 if (info
[i
] & ALL_POS
) {
3692 fprintf(stderr
, "\nER: Positive\n");
3693 #endif /* DEBUG_ER */
3695 // Maybe we should chew off some of the fat here
3696 Matrix
*M
= Matrix_Alloc(P
->Dimension
, P
->Dimension
+1);
3697 for (int j
= 0; j
< P
->Dimension
; ++j
)
3698 value_set_si(M
->p
[j
][j
+ (j
>= i
+nvar
)], 1);
3699 Polyhedron
*T
= Polyhedron_Image(P
, M
, MaxRays
);
3701 evalue
*EP
= barvinok_enumerate_e(T
, exist
-1, nparam
, MaxRays
);
3708 for (int i
= 0; i
< exist
; ++i
)
3709 if (info
[i
] & ONE_NEG
) {
3711 fprintf(stderr
, "\nER: Negative\n");
3712 #endif /* DEBUG_ER */
3717 return barvinok_enumerate_e(P
, exist
-1, nparam
, MaxRays
);
3719 Polyhedron
*T
= Polyhedron_Copy(P
);
3720 SwapColumns(T
, nvar
+1, nvar
+1+i
);
3721 evalue
*EP
= barvinok_enumerate_e(T
, exist
-1, nparam
, MaxRays
);
3726 for (int i
= 0; i
< exist
; ++i
)
3727 if (info
[i
] & ROT_NEG
) {
3729 fprintf(stderr
, "\nER: Rotate\n");
3730 #endif /* DEBUG_ER */
3734 Polyhedron
*T
= rotate_along(P
, r
, nvar
, exist
, MaxRays
);
3735 evalue
*EP
= barvinok_enumerate_e(T
, exist
-1, nparam
, MaxRays
);
3739 for (int i
= 0; i
< exist
; ++i
)
3740 if (info
[i
] & INDEPENDENT
) {
3741 Polyhedron
*pos
, *neg
;
3743 /* Find constraint again and split off negative part */
3745 if (SplitOnVar(P
, i
, nvar
, len
, exist
, MaxRays
,
3746 row
, f
, true, &pos
, &neg
)) {
3748 fprintf(stderr
, "\nER: Split\n");
3749 #endif /* DEBUG_ER */
3752 barvinok_enumerate_e(neg
, exist
-1, nparam
, MaxRays
);
3754 barvinok_enumerate_e(pos
, exist
, nparam
, MaxRays
);
3756 free_evalue_refs(E
);
3758 Polyhedron_Free(neg
);
3759 Polyhedron_Free(pos
);
3773 EP
= enumerate_line(P
, exist
, nparam
, MaxRays
);
3777 EP
= barvinok_enumerate_pip(P
, exist
, nparam
, MaxRays
);
3781 EP
= enumerate_redundant_ray(P
, exist
, nparam
, MaxRays
);
3785 EP
= enumerate_sure(P
, exist
, nparam
, MaxRays
);
3789 EP
= enumerate_ray(P
, exist
, nparam
, MaxRays
);
3793 EP
= enumerate_sure2(P
, exist
, nparam
, MaxRays
);
3797 F
= unfringe(P
, MaxRays
);
3798 if (!PolyhedronIncludes(F
, P
)) {
3800 fprintf(stderr
, "\nER: Fringed\n");
3801 #endif /* DEBUG_ER */
3802 EP
= barvinok_enumerate_e(F
, exist
, nparam
, MaxRays
);
3809 EP
= enumerate_vd(&P
, exist
, nparam
, MaxRays
);
3814 EP
= enumerate_sum(P
, exist
, nparam
, MaxRays
);
3821 Polyhedron
*pos
, *neg
;
3822 for (i
= 0; i
< exist
; ++i
)
3823 if (SplitOnVar(P
, i
, nvar
, len
, exist
, MaxRays
,
3824 row
, f
, false, &pos
, &neg
))
3830 EP
= enumerate_or(pos
, exist
, nparam
, MaxRays
);
3842 gen_fun
* barvinok_series(Polyhedron
*P
, Polyhedron
* C
, unsigned MaxRays
)
3844 Polyhedron
** vcone
;
3846 unsigned nparam
= C
->Dimension
;
3850 sign
.SetLength(ncone
);
3852 CA
= align_context(C
, P
->Dimension
, MaxRays
);
3853 P
= DomainIntersection(P
, CA
, MaxRays
);
3854 Polyhedron_Free(CA
);
3856 assert(!Polyhedron_is_infinite(P
, nparam
));
3857 assert(P
->NbBid
== 0);
3858 assert(Polyhedron_has_positive_rays(P
, nparam
));
3859 assert(P
->NbEq
== 0);
3862 nvar
= dim
- nparam
;
3863 vcone
= new Polyhedron_p
[P
->NbRays
];
3865 for (int j
= 0; j
< P
->NbRays
; ++j
) {
3866 if (!value_pos_p(P
->Ray
[j
][dim
+1]))
3870 Polyhedron
*C
= supporting_cone(P
, j
);
3871 decompose(C
, &vcone
[j
], &npos
, &nneg
, MaxRays
);
3872 ncone
+= npos
+ nneg
;
3873 sign
.SetLength(ncone
);
3874 for (int k
= 0; k
< npos
; ++k
)
3875 sign
[ncone
-nneg
-k
-1] = 1;
3876 for (int k
= 0; k
< nneg
; ++k
)
3877 sign
[ncone
-k
-1] = -1;
3881 rays
.SetDims(ncone
* dim
, nvar
);
3883 for (int j
= 0; j
< P
->NbRays
; ++j
) {
3884 if (!value_pos_p(P
->Ray
[j
][dim
+1]))
3887 for (Polyhedron
*i
= vcone
[j
]; i
; i
= i
->next
) {
3888 add_rays(rays
, i
, &r
, nvar
);
3891 rays
.SetDims(r
, nvar
);
3893 nonorthog(rays
, lambda
);
3894 //randomvector(P, lambda, nvar);
3897 cout << "rays: " << rays;
3898 cout << "lambda: " << lambda;
3904 num_p
.SetLength(nparam
);
3907 den_s
.SetLength(dim
);
3909 den_p
.SetLength(dim
);
3911 den
.SetDims(dim
, nparam
);
3917 gen_fun
* gf
= new gen_fun
;
3919 rays
.SetDims(dim
, nvar
);
3921 for (int j
= 0; j
< P
->NbRays
; ++j
) {
3922 if (!value_pos_p(P
->Ray
[j
][dim
+1]))
3925 for (Polyhedron
*i
= vcone
[j
]; i
; i
= i
->next
, ++f
) {
3926 lattice_point(P
->Ray
[j
]+1, i
, vertex
);
3929 for ( ; k
< nvar
; ++k
)
3930 num_s
+= vertex
[k
] * lambda
[k
];
3931 for ( ; k
< dim
; ++k
)
3932 num_p
[k
-nvar
] = vertex
[k
];
3935 add_rays(rays
, i
, &r
, nvar
, true);
3936 for (r
= 0; r
< dim
; ++r
)
3937 values2zz(i
->Ray
[r
]+1+nvar
, den
[r
], nparam
);
3938 den_s
= rays
* lambda
;
3940 normalize(sign
[f
], num_s
, num_p
, den_s
, den_p
, den
);
3944 for (int k
= 0; k
< dim
; ++k
) {
3947 else if (den_s
[k
] == 0)
3950 if (no_param
== 0) {
3951 for (int k
= 0; k
< dim
; ++k
)
3954 gf
->add(sign
[f
], one
, num_p
, den
);
3955 } else if (no_param
+ only_param
== dim
) {
3958 pden
.SetDims(only_param
, nparam
);
3960 for (k
= 0, l
= 0; k
< dim
; ++k
)
3964 for (k
= 0; k
< dim
; ++k
)
3968 dpoly
n(no_param
, num_s
);
3969 dpoly
d(no_param
, den_s
[k
], 1);
3970 for ( ; ++k
< dim
; k
)
3971 if (den_s
[k
] != 0) {
3972 dpoly
fact(no_param
, den_s
[k
], 1);
3976 mpq_set_si(count
, 0, 1);
3977 n
.div(d
, count
, sign
[f
]);
3980 value2zz(mpq_numref(count
), qn
);
3981 value2zz(mpq_denref(count
), qd
);
3983 gf
->add(qn
, qd
, num_p
, pden
);
3988 pden
.SetDims(only_param
, nparam
);
3990 for (k
= 0, l
= 0; k
< dim
; ++k
)
3994 for (k
= 0; k
< dim
; ++k
)
3998 dpoly
n(no_param
, num_s
);
3999 dpoly
d(no_param
, den_s
[k
], 1);
4000 for ( ; ++k
< dim
; )
4001 if (den_p
[k
] == 0) {
4002 dpoly
fact(no_param
, den_s
[k
], 1);
4006 for (k
= 0; k
< dim
; ++k
) {
4007 if (den_s
[k
] == 0 || den_p
[k
] == 0)
4010 dpoly
pd(no_param
-1, den_s
[k
], 1);
4011 int s
= den_p
[k
] < 0 ? -1 : 1;
4014 r
= new dpoly_r(n
, pd
, k
, s
, dim
);
4016 assert(0); // for now
4019 r
->div(d
, sign
[f
], gf
, pden
, den
, num_p
);
4023 cout << "sign: " << sign[f];
4024 cout << "num_s: " << num_s;
4025 cout << "num_p: " << num_p;
4026 cout << "den_s: " << den_s;
4027 cout << "den_p: " << den_p;
4028 cout << "den: " << den;
4029 cout << "only_param: " << only_param;
4030 cout << "no_param: " << no_param;