2 #include <NTL/mat_ZZ.h>
3 #include <NTL/vec_ZZ.h>
5 #include <polylib/polylibgmp.h>
7 #include <barvinok/barvinok.h>
8 #include <barvinok/evalue.h>
9 #include <barvinok/util.h>
11 #include "conversion.h"
12 #include "lattice_point.h"
14 #define ALLOC(type) (type*)malloc(sizeof(type))
16 /* returns an evalue that corresponds to
20 static evalue
*term(int param
, ZZ
& c
, Value
*den
= NULL
)
22 evalue
*EP
= new evalue();
24 value_set_si(EP
->d
,0);
25 EP
->x
.p
= new_enode(polynomial
, 2, param
+ 1);
26 evalue_set_si(&EP
->x
.p
->arr
[0], 0, 1);
27 value_init(EP
->x
.p
->arr
[1].x
.n
);
29 value_set_si(EP
->x
.p
->arr
[1].d
, 1);
31 value_assign(EP
->x
.p
->arr
[1].d
, *den
);
32 zz2value(c
, EP
->x
.p
->arr
[1].x
.n
);
36 /* returns an evalue that corresponds to
40 evalue
*multi_monom(vec_ZZ
& p
)
42 evalue
*X
= new evalue();
45 unsigned nparam
= p
.length()-1;
46 zz2value(p
[nparam
], X
->x
.n
);
47 value_set_si(X
->d
, 1);
48 for (int i
= 0; i
< nparam
; ++i
) {
51 evalue
*T
= term(i
, p
[i
]);
60 * Check whether mapping polyhedron P on the affine combination
61 * num yields a range that has a fixed quotient on integer
63 * If zero is true, then we are only interested in the quotient
64 * for the cases where the remainder is zero.
65 * Returns NULL if false and a newly allocated value if true.
67 static Value
*fixed_quotient(Polyhedron
*P
, vec_ZZ
& num
, Value d
, bool zero
)
70 int len
= num
.length();
71 Matrix
*T
= Matrix_Alloc(2, len
);
72 zz2values(num
, T
->p
[0]);
73 value_set_si(T
->p
[1][len
-1], 1);
74 Polyhedron
*I
= Polyhedron_Image(P
, T
, P
->NbConstraints
);
78 for (i
= 0; i
< I
->NbRays
; ++i
)
79 if (value_zero_p(I
->Ray
[i
][2])) {
87 int bounded
= line_minmax(I
, &min
, &max
);
91 mpz_cdiv_q(min
, min
, d
);
93 mpz_fdiv_q(min
, min
, d
);
94 mpz_fdiv_q(max
, max
, d
);
96 if (value_eq(min
, max
)) {
99 value_assign(*ret
, min
);
107 * Normalize linear expression coef modulo m
108 * Removes common factor and reduces coefficients
109 * Returns index of first non-zero coefficient or len
111 int normal_mod(Value
*coef
, int len
, Value
*m
)
116 Vector_Gcd(coef
, len
, &gcd
);
118 Vector_AntiScale(coef
, coef
, gcd
, len
);
120 value_division(*m
, *m
, gcd
);
127 for (j
= 0; j
< len
; ++j
)
128 mpz_fdiv_r(coef
[j
], coef
[j
], *m
);
129 for (j
= 0; j
< len
; ++j
)
130 if (value_notzero_p(coef
[j
]))
136 static bool mod_needed(Polyhedron
*PD
, vec_ZZ
& num
, Value d
, evalue
*E
)
138 Value
*q
= fixed_quotient(PD
, num
, d
, false);
143 value_oppose(*q
, *q
);
146 value_set_si(EV
.d
, 1);
148 value_multiply(EV
.x
.n
, *q
, d
);
150 free_evalue_refs(&EV
);
156 /* modifies f argument ! */
157 static void ceil_mod(Value
*coef
, int len
, Value d
, ZZ
& f
, evalue
*EP
, Polyhedron
*PD
)
163 Vector_Scale(coef
, coef
, m
, len
);
166 int j
= normal_mod(coef
, len
, &m
);
174 values2zz(coef
, num
, len
);
181 evalue_set_si(&tmp
, 0, 1);
185 while (j
< len
-1 && (num
[j
] == g
/2 || num
[j
] == 0))
187 if ((j
< len
-1 && num
[j
] > g
/2) || (j
== len
-1 && num
[j
] >= (g
+1)/2)) {
188 for (int k
= j
; k
< len
-1; ++k
)
191 num
[len
-1] = g
- 1 - num
[len
-1];
192 value_assign(tmp
.d
, m
);
194 zz2value(t
, tmp
.x
.n
);
200 ZZ t
= num
[len
-1] * f
;
201 zz2value(t
, tmp
.x
.n
);
202 value_assign(tmp
.d
, m
);
205 evalue
*E
= multi_monom(num
);
209 if (PD
&& !mod_needed(PD
, num
, m
, E
)) {
212 value_assign(EV
.d
, m
);
217 value_set_si(EV
.x
.n
, 1);
218 value_assign(EV
.d
, m
);
221 value_set_si(EV
.d
, 0);
222 EV
.x
.p
= new_enode(fractional
, 3, -1);
223 evalue_copy(&EV
.x
.p
->arr
[0], E
);
224 evalue_set_si(&EV
.x
.p
->arr
[1], 0, 1);
225 value_init(EV
.x
.p
->arr
[2].x
.n
);
226 zz2value(f
, EV
.x
.p
->arr
[2].x
.n
);
227 value_set_si(EV
.x
.p
->arr
[2].d
, 1);
232 free_evalue_refs(&EV
);
237 free_evalue_refs(&tmp
);
244 static void ceil(Value
*coef
, int len
, Value d
, ZZ
& f
,
245 evalue
*EP
, Polyhedron
*PD
) {
246 ceil_mod(coef
, len
, d
, f
, EP
, PD
);
249 static void ceil(Value
*coef
, int len
, Value d
, ZZ
& f
,
250 evalue
*EP
, Polyhedron
*PD
) {
251 ceil_mod(coef
, len
, d
, f
, EP
, PD
);
252 evalue_mod2table(EP
, len
-1);
256 evalue
* bv_ceil3(Value
*coef
, int len
, Value d
, Polyhedron
*P
)
258 Vector
*val
= Vector_Alloc(len
);
263 Vector_Scale(coef
, val
->p
, t
, len
);
264 value_absolute(t
, d
);
267 values2zz(val
->p
, num
, len
);
268 evalue
*EP
= multi_monom(num
);
273 value_set_si(tmp
.x
.n
, 1);
274 value_assign(tmp
.d
, t
);
280 ceil_mod(val
->p
, len
, t
, one
, EP
, P
);
283 /* copy EP to malloc'ed evalue */
284 evalue
*E
= ALLOC(evalue
);
288 free_evalue_refs(&tmp
);
294 void lattice_point(Value
* values
, Polyhedron
*i
, vec_ZZ
& vertex
)
296 unsigned dim
= i
->Dimension
;
297 if(!value_one_p(values
[dim
])) {
298 Matrix
* Rays
= rays(i
);
299 Matrix
*inv
= Matrix_Alloc(Rays
->NbRows
, Rays
->NbColumns
);
300 int ok
= Matrix_Inverse(Rays
, inv
);
304 Vector
*lambda
= Vector_Alloc(dim
+1);
305 Vector_Matrix_Product(values
, inv
, lambda
->p
);
307 for (int j
= 0; j
< dim
; ++j
)
308 mpz_cdiv_q(lambda
->p
[j
], lambda
->p
[j
], lambda
->p
[dim
]);
309 value_set_si(lambda
->p
[dim
], 1);
310 Vector
*A
= Vector_Alloc(dim
+1);
311 Vector_Matrix_Product(lambda
->p
, Rays
, A
->p
);
314 values2zz(A
->p
, vertex
, dim
);
317 values2zz(values
, vertex
, dim
);
320 static void vertex_period(
321 Polyhedron
*i
, vec_ZZ
& lambda
, Matrix
*T
,
322 Value lcm
, int p
, Vector
*val
,
323 evalue
*E
, evalue
* ev
,
326 unsigned nparam
= T
->NbRows
- 1;
327 unsigned dim
= i
->Dimension
;
334 Vector
* values
= Vector_Alloc(dim
+ 1);
335 Vector_Matrix_Product(val
->p
, T
, values
->p
);
336 value_assign(values
->p
[dim
], lcm
);
337 lattice_point(values
->p
, i
, vertex
);
338 num
= vertex
* lambda
;
343 zz2value(num
, ev
->x
.n
);
344 value_assign(ev
->d
, lcm
);
351 values2zz(T
->p
[p
], vertex
, dim
);
352 nump
= vertex
* lambda
;
353 if (First_Non_Zero(val
->p
, p
) == -1) {
354 value_assign(tmp
, lcm
);
355 evalue
*ET
= term(p
, nump
, &tmp
);
357 free_evalue_refs(ET
);
361 value_assign(tmp
, lcm
);
362 if (First_Non_Zero(T
->p
[p
], dim
) != -1)
363 Vector_Gcd(T
->p
[p
], dim
, &tmp
);
365 if (value_lt(tmp
, lcm
)) {
368 value_division(tmp
, lcm
, tmp
);
369 value_set_si(ev
->d
, 0);
370 ev
->x
.p
= new_enode(periodic
, VALUE_TO_INT(tmp
), p
+1);
371 value2zz(tmp
, count
);
373 value_decrement(tmp
, tmp
);
375 ZZ new_offset
= offset
- count
* nump
;
376 value_assign(val
->p
[p
], tmp
);
377 vertex_period(i
, lambda
, T
, lcm
, p
+1, val
, E
,
378 &ev
->x
.p
->arr
[VALUE_TO_INT(tmp
)], new_offset
);
379 } while (value_pos_p(tmp
));
381 vertex_period(i
, lambda
, T
, lcm
, p
+1, val
, E
, ev
, offset
);
385 /* Returns the power of (t+1) in the term of a rational generating function,
386 * i.e., the scalar product of the actual lattice point and lambda.
387 * The lattice point is the unique lattice point in the fundamental parallelepiped
388 * of the unimodual cone i shifted to the parametric vertex W/lcm.
390 * The rows of W refer to the coordinates of the vertex
391 * The first nparam columns are the coefficients of the parameters
392 * and the final column is the constant term.
393 * lcm is the common denominator of all coefficients.
395 * PD is the parameter domain, which, if != NULL, may be used to simply the
396 * resulting expression.
399 evalue
* lattice_point(
400 Polyhedron
*i
, vec_ZZ
& lambda
, Matrix
*W
, Value lcm
, Polyhedron
*PD
)
402 unsigned nparam
= W
->NbColumns
- 1;
404 Matrix
* Rays
= rays2(i
);
405 Matrix
*T
= Transpose(Rays
);
406 Matrix
*T2
= Matrix_Copy(T
);
407 Matrix
*inv
= Matrix_Alloc(T2
->NbRows
, T2
->NbColumns
);
408 int ok
= Matrix_Inverse(T2
, inv
);
413 matrix2zz(W
, vertex
, W
->NbRows
, W
->NbColumns
);
416 num
= lambda
* vertex
;
418 evalue
*EP
= multi_monom(num
);
423 value_set_si(tmp
.x
.n
, 1);
424 value_assign(tmp
.d
, lcm
);
428 Matrix
*L
= Matrix_Alloc(inv
->NbRows
, W
->NbColumns
);
429 Matrix_Product(inv
, W
, L
);
432 matrix2zz(T
, RT
, T
->NbRows
, T
->NbColumns
);
435 vec_ZZ p
= lambda
* RT
;
437 for (int i
= 0; i
< L
->NbRows
; ++i
) {
438 ceil_mod(L
->p
[i
], nparam
+1, lcm
, p
[i
], EP
, PD
);
444 free_evalue_refs(&tmp
);
448 evalue
* lattice_point(
449 Polyhedron
*i
, vec_ZZ
& lambda
, Matrix
*W
, Value lcm
, Polyhedron
*PD
)
451 Matrix
*T
= Transpose(W
);
452 unsigned nparam
= T
->NbRows
- 1;
454 evalue
*EP
= new evalue();
456 evalue_set_si(EP
, 0, 1);
459 Vector
*val
= Vector_Alloc(nparam
+1);
460 value_set_si(val
->p
[nparam
], 1);
461 ZZ
offset(INIT_VAL
, 0);
463 vertex_period(i
, lambda
, T
, lcm
, 0, val
, EP
, &ev
, offset
);
466 free_evalue_refs(&ev
);
476 /* returns the unique lattice point in the fundamental parallelepiped
477 * of the unimodual cone C shifted to the parametric vertex V.
479 * The return values num and E_vertex are such that
480 * coordinate i of this lattice point is equal to
482 * num[i] + E_vertex[i]
484 void lattice_point(Param_Vertices
*V
, Polyhedron
*C
, vec_ZZ
& num
,
487 unsigned nparam
= V
->Vertex
->NbColumns
- 2;
488 unsigned dim
= C
->Dimension
;
490 vertex
.SetLength(nparam
+1);
495 value_set_si(lcm
, 1);
497 for (int j
= 0; j
< V
->Vertex
->NbRows
; ++j
) {
498 value_lcm(lcm
, V
->Vertex
->p
[j
][nparam
+1], &lcm
);
501 if (value_notone_p(lcm
)) {
502 Matrix
* mv
= Matrix_Alloc(dim
, nparam
+1);
503 for (int j
= 0 ; j
< dim
; ++j
) {
504 value_division(tmp
, lcm
, V
->Vertex
->p
[j
][nparam
+1]);
505 Vector_Scale(V
->Vertex
->p
[j
], mv
->p
[j
], tmp
, nparam
+1);
508 Matrix
* Rays
= rays2(C
);
509 Matrix
*T
= Transpose(Rays
);
510 Matrix
*T2
= Matrix_Copy(T
);
511 Matrix
*inv
= Matrix_Alloc(T2
->NbRows
, T2
->NbColumns
);
512 int ok
= Matrix_Inverse(T2
, inv
);
516 Matrix
*L
= Matrix_Alloc(inv
->NbRows
, mv
->NbColumns
);
517 Matrix_Product(inv
, mv
, L
);
526 evalue
*remainders
[dim
];
527 for (int i
= 0; i
< dim
; ++i
) {
528 remainders
[i
] = evalue_zero();
530 ceil(L
->p
[i
], nparam
+1, lcm
, one
, remainders
[i
], 0);
535 for (int i
= 0; i
< V
->Vertex
->NbRows
; ++i
) {
536 values2zz(mv
->p
[i
], vertex
, nparam
+1);
537 E_vertex
[i
] = multi_monom(vertex
);
540 value_set_si(f
.x
.n
, 1);
541 value_assign(f
.d
, lcm
);
543 emul(&f
, E_vertex
[i
]);
545 for (int j
= 0; j
< dim
; ++j
) {
546 if (value_zero_p(T
->p
[i
][j
]))
550 evalue_copy(&cp
, remainders
[j
]);
551 if (value_notone_p(T
->p
[i
][j
])) {
552 value_set_si(f
.d
, 1);
553 value_assign(f
.x
.n
, T
->p
[i
][j
]);
556 eadd(&cp
, E_vertex
[i
]);
557 free_evalue_refs(&cp
);
560 for (int i
= 0; i
< dim
; ++i
) {
561 free_evalue_refs(remainders
[i
]);
565 free_evalue_refs(&f
);
576 for (int i
= 0; i
< V
->Vertex
->NbRows
; ++i
) {
578 if (First_Non_Zero(V
->Vertex
->p
[i
], nparam
) == -1) {
580 value2zz(V
->Vertex
->p
[i
][nparam
], num
[i
]);
582 values2zz(V
->Vertex
->p
[i
], vertex
, nparam
+1);
583 E_vertex
[i
] = multi_monom(vertex
);