2 #include <NTL/mat_ZZ.h>
3 #include <NTL/vec_ZZ.h>
4 #include <barvinok/barvinok.h>
5 #include <barvinok/evalue.h>
6 #include <barvinok/util.h>
8 #include "conversion.h"
9 #include "lattice_point.h"
11 #define ALLOC(type) (type*)malloc(sizeof(type))
13 /* returns an evalue that corresponds to
17 static evalue
*term(int param
, ZZ
& c
, Value
*den
= NULL
)
19 evalue
*EP
= new evalue();
21 value_set_si(EP
->d
,0);
22 EP
->x
.p
= new_enode(polynomial
, 2, param
+ 1);
23 evalue_set_si(&EP
->x
.p
->arr
[0], 0, 1);
24 value_init(EP
->x
.p
->arr
[1].x
.n
);
26 value_set_si(EP
->x
.p
->arr
[1].d
, 1);
28 value_assign(EP
->x
.p
->arr
[1].d
, *den
);
29 zz2value(c
, EP
->x
.p
->arr
[1].x
.n
);
33 /* returns an evalue that corresponds to
37 evalue
*multi_monom(vec_ZZ
& p
)
39 evalue
*X
= new evalue();
42 unsigned nparam
= p
.length()-1;
43 zz2value(p
[nparam
], X
->x
.n
);
44 value_set_si(X
->d
, 1);
45 for (int i
= 0; i
< nparam
; ++i
) {
48 evalue
*T
= term(i
, p
[i
]);
57 * Check whether mapping polyhedron P on the affine combination
58 * num yields a range that has a fixed quotient on integer
60 * If zero is true, then we are only interested in the quotient
61 * for the cases where the remainder is zero.
62 * Returns NULL if false and a newly allocated value if true.
64 static Value
*fixed_quotient(Polyhedron
*P
, vec_ZZ
& num
, Value d
, bool zero
)
67 int len
= num
.length();
68 Matrix
*T
= Matrix_Alloc(2, len
);
69 zz2values(num
, T
->p
[0]);
70 value_set_si(T
->p
[1][len
-1], 1);
71 Polyhedron
*I
= Polyhedron_Image(P
, T
, P
->NbConstraints
);
75 for (i
= 0; i
< I
->NbRays
; ++i
)
76 if (value_zero_p(I
->Ray
[i
][2])) {
84 int bounded
= line_minmax(I
, &min
, &max
);
88 mpz_cdiv_q(min
, min
, d
);
90 mpz_fdiv_q(min
, min
, d
);
91 mpz_fdiv_q(max
, max
, d
);
93 if (value_eq(min
, max
)) {
96 value_assign(*ret
, min
);
104 * Normalize linear expression coef modulo m
105 * Removes common factor and reduces coefficients
106 * Returns index of first non-zero coefficient or len
108 int normal_mod(Value
*coef
, int len
, Value
*m
)
113 Vector_Gcd(coef
, len
, &gcd
);
115 Vector_AntiScale(coef
, coef
, gcd
, len
);
117 value_division(*m
, *m
, gcd
);
124 for (j
= 0; j
< len
; ++j
)
125 mpz_fdiv_r(coef
[j
], coef
[j
], *m
);
126 for (j
= 0; j
< len
; ++j
)
127 if (value_notzero_p(coef
[j
]))
133 static bool mod_needed(Polyhedron
*PD
, vec_ZZ
& num
, Value d
, evalue
*E
)
135 Value
*q
= fixed_quotient(PD
, num
, d
, false);
140 value_oppose(*q
, *q
);
143 value_set_si(EV
.d
, 1);
145 value_multiply(EV
.x
.n
, *q
, d
);
147 free_evalue_refs(&EV
);
153 /* modifies f argument ! */
154 static void ceil_mod(Value
*coef
, int len
, Value d
, ZZ
& f
, evalue
*EP
, Polyhedron
*PD
)
160 Vector_Scale(coef
, coef
, m
, len
);
163 int j
= normal_mod(coef
, len
, &m
);
171 values2zz(coef
, num
, len
);
178 evalue_set_si(&tmp
, 0, 1);
182 while (j
< len
-1 && (num
[j
] == g
/2 || num
[j
] == 0))
184 if ((j
< len
-1 && num
[j
] > g
/2) || (j
== len
-1 && num
[j
] >= (g
+1)/2)) {
185 for (int k
= j
; k
< len
-1; ++k
)
188 num
[len
-1] = g
- 1 - num
[len
-1];
189 value_assign(tmp
.d
, m
);
191 zz2value(t
, tmp
.x
.n
);
197 ZZ t
= num
[len
-1] * f
;
198 zz2value(t
, tmp
.x
.n
);
199 value_assign(tmp
.d
, m
);
202 evalue
*E
= multi_monom(num
);
206 if (PD
&& !mod_needed(PD
, num
, m
, E
)) {
209 value_assign(EV
.d
, m
);
214 value_set_si(EV
.x
.n
, 1);
215 value_assign(EV
.d
, m
);
218 value_set_si(EV
.d
, 0);
219 EV
.x
.p
= new_enode(fractional
, 3, -1);
220 evalue_copy(&EV
.x
.p
->arr
[0], E
);
221 evalue_set_si(&EV
.x
.p
->arr
[1], 0, 1);
222 value_init(EV
.x
.p
->arr
[2].x
.n
);
223 zz2value(f
, EV
.x
.p
->arr
[2].x
.n
);
224 value_set_si(EV
.x
.p
->arr
[2].d
, 1);
229 free_evalue_refs(&EV
);
234 free_evalue_refs(&tmp
);
240 static void ceil(Value
*coef
, int len
, Value d
, ZZ
& f
,
241 evalue
*EP
, Polyhedron
*PD
, barvinok_options
*options
)
243 ceil_mod(coef
, len
, d
, f
, EP
, PD
);
244 if (options
->lookup_table
)
245 evalue_mod2table(EP
, len
-1);
248 evalue
* bv_ceil3(Value
*coef
, int len
, Value d
, Polyhedron
*P
)
250 Vector
*val
= Vector_Alloc(len
);
255 Vector_Scale(coef
, val
->p
, t
, len
);
256 value_absolute(t
, d
);
259 values2zz(val
->p
, num
, len
);
260 evalue
*EP
= multi_monom(num
);
265 value_set_si(tmp
.x
.n
, 1);
266 value_assign(tmp
.d
, t
);
272 ceil_mod(val
->p
, len
, t
, one
, EP
, P
);
275 /* copy EP to malloc'ed evalue */
276 evalue
*E
= ALLOC(evalue
);
280 free_evalue_refs(&tmp
);
286 void lattice_point(Value
* values
, const mat_ZZ
& rays
, vec_ZZ
& vertex
, int *closed
)
288 unsigned dim
= rays
.NumRows();
289 if (value_one_p(values
[dim
]) && !closed
)
290 values2zz(values
, vertex
, dim
);
292 Matrix
* Rays
= rays2matrix(rays
);
293 Matrix
*inv
= Matrix_Alloc(Rays
->NbRows
, Rays
->NbColumns
);
294 int ok
= Matrix_Inverse(Rays
, inv
);
297 Rays
= rays2matrix(rays
);
298 Vector
*lambda
= Vector_Alloc(dim
+1);
299 Vector_Matrix_Product(values
, inv
, lambda
->p
);
301 for (int j
= 0; j
< dim
; ++j
)
302 if (!closed
|| closed
[j
])
303 mpz_cdiv_q(lambda
->p
[j
], lambda
->p
[j
], lambda
->p
[dim
]);
305 value_addto(lambda
->p
[j
], lambda
->p
[j
], lambda
->p
[dim
]);
306 mpz_fdiv_q(lambda
->p
[j
], lambda
->p
[j
], lambda
->p
[dim
]);
308 value_set_si(lambda
->p
[dim
], 1);
309 Vector
*A
= Vector_Alloc(dim
+1);
310 Vector_Matrix_Product(lambda
->p
, Rays
, A
->p
);
313 values2zz(A
->p
, vertex
, dim
);
318 static void vertex_period(
319 const mat_ZZ
& rays
, vec_ZZ
& lambda
, Matrix
*T
,
320 Value lcm
, int p
, Vector
*val
,
321 evalue
*E
, evalue
* ev
,
324 unsigned nparam
= T
->NbRows
- 1;
325 unsigned dim
= rays
.NumRows();
332 Vector
* values
= Vector_Alloc(dim
+ 1);
333 Vector_Matrix_Product(val
->p
, T
, values
->p
);
334 value_assign(values
->p
[dim
], lcm
);
335 lattice_point(values
->p
, rays
, vertex
, NULL
);
336 num
= vertex
* lambda
;
341 zz2value(num
, ev
->x
.n
);
342 value_assign(ev
->d
, lcm
);
349 values2zz(T
->p
[p
], vertex
, dim
);
350 nump
= vertex
* lambda
;
351 if (First_Non_Zero(val
->p
, p
) == -1) {
352 value_assign(tmp
, lcm
);
353 evalue
*ET
= term(p
, nump
, &tmp
);
355 free_evalue_refs(ET
);
359 value_assign(tmp
, lcm
);
360 if (First_Non_Zero(T
->p
[p
], dim
) != -1)
361 Vector_Gcd(T
->p
[p
], dim
, &tmp
);
363 if (value_lt(tmp
, lcm
)) {
366 value_division(tmp
, lcm
, tmp
);
367 value_set_si(ev
->d
, 0);
368 ev
->x
.p
= new_enode(periodic
, VALUE_TO_INT(tmp
), p
+1);
369 value2zz(tmp
, count
);
371 value_decrement(tmp
, tmp
);
373 ZZ new_offset
= offset
- count
* nump
;
374 value_assign(val
->p
[p
], tmp
);
375 vertex_period(rays
, lambda
, T
, lcm
, p
+1, val
, E
,
376 &ev
->x
.p
->arr
[VALUE_TO_INT(tmp
)], new_offset
);
377 } while (value_pos_p(tmp
));
379 vertex_period(rays
, lambda
, T
, lcm
, p
+1, val
, E
, ev
, offset
);
383 /* Returns the power of (t+1) in the term of a rational generating function,
384 * i.e., the scalar product of the actual lattice point and lambda.
385 * The lattice point is the unique lattice point in the fundamental parallelepiped
386 * of the unimodual cone i shifted to the parametric vertex W/lcm.
388 * The rows of W refer to the coordinates of the vertex
389 * The first nparam columns are the coefficients of the parameters
390 * and the final column is the constant term.
391 * lcm is the common denominator of all coefficients.
393 * PD is the parameter domain, which, if != NULL, may be used to simply the
394 * resulting expression.
396 static evalue
* lattice_point_fractional(const mat_ZZ
& rays
, vec_ZZ
& lambda
,
397 Matrix
*W
, Value lcm
, Polyhedron
*PD
)
399 unsigned nparam
= W
->NbColumns
- 1;
401 Matrix
* Rays
= rays2matrix2(rays
);
402 Matrix
*T
= Transpose(Rays
);
403 Matrix
*T2
= Matrix_Copy(T
);
404 Matrix
*inv
= Matrix_Alloc(T2
->NbRows
, T2
->NbColumns
);
405 int ok
= Matrix_Inverse(T2
, inv
);
410 matrix2zz(W
, vertex
, W
->NbRows
, W
->NbColumns
);
413 num
= lambda
* vertex
;
415 evalue
*EP
= multi_monom(num
);
420 value_set_si(tmp
.x
.n
, 1);
421 value_assign(tmp
.d
, lcm
);
425 Matrix
*L
= Matrix_Alloc(inv
->NbRows
, W
->NbColumns
);
426 Matrix_Product(inv
, W
, L
);
429 matrix2zz(T
, RT
, T
->NbRows
, T
->NbColumns
);
432 vec_ZZ p
= lambda
* RT
;
434 for (int i
= 0; i
< L
->NbRows
; ++i
) {
435 ceil_mod(L
->p
[i
], nparam
+1, lcm
, p
[i
], EP
, PD
);
441 free_evalue_refs(&tmp
);
445 static evalue
* lattice_point_table(const mat_ZZ
& rays
, vec_ZZ
& lambda
, Matrix
*W
,
446 Value lcm
, Polyhedron
*PD
)
448 Matrix
*T
= Transpose(W
);
449 unsigned nparam
= T
->NbRows
- 1;
451 evalue
*EP
= new evalue();
453 evalue_set_si(EP
, 0, 1);
456 Vector
*val
= Vector_Alloc(nparam
+1);
457 value_set_si(val
->p
[nparam
], 1);
458 ZZ
offset(INIT_VAL
, 0);
460 vertex_period(rays
, lambda
, T
, lcm
, 0, val
, EP
, &ev
, offset
);
463 free_evalue_refs(&ev
);
472 evalue
* lattice_point(const mat_ZZ
& rays
, vec_ZZ
& lambda
, Matrix
*W
,
473 Value lcm
, Polyhedron
*PD
, barvinok_options
*options
)
475 if (options
->lookup_table
)
476 return lattice_point_table(rays
, lambda
, W
, lcm
, PD
);
478 return lattice_point_fractional(rays
, lambda
, W
, lcm
, PD
);
481 /* returns the unique lattice point in the fundamental parallelepiped
482 * of the unimodual cone C shifted to the parametric vertex V.
484 * The return values num and E_vertex are such that
485 * coordinate i of this lattice point is equal to
487 * num[i] + E_vertex[i]
489 void lattice_point(Param_Vertices
*V
, const mat_ZZ
& rays
, vec_ZZ
& num
,
490 evalue
**E_vertex
, barvinok_options
*options
)
492 unsigned nparam
= V
->Vertex
->NbColumns
- 2;
493 unsigned dim
= rays
.NumCols();
495 vertex
.SetLength(nparam
+1);
500 value_set_si(lcm
, 1);
502 for (int j
= 0; j
< V
->Vertex
->NbRows
; ++j
) {
503 value_lcm(lcm
, V
->Vertex
->p
[j
][nparam
+1], &lcm
);
506 if (value_notone_p(lcm
)) {
507 Matrix
* mv
= Matrix_Alloc(dim
, nparam
+1);
508 for (int j
= 0 ; j
< dim
; ++j
) {
509 value_division(tmp
, lcm
, V
->Vertex
->p
[j
][nparam
+1]);
510 Vector_Scale(V
->Vertex
->p
[j
], mv
->p
[j
], tmp
, nparam
+1);
513 Matrix
* Rays
= rays2matrix2(rays
);
514 Matrix
*T
= Transpose(Rays
);
515 Matrix
*T2
= Matrix_Copy(T
);
516 Matrix
*inv
= Matrix_Alloc(T2
->NbRows
, T2
->NbColumns
);
517 int ok
= Matrix_Inverse(T2
, inv
);
521 Matrix
*L
= Matrix_Alloc(inv
->NbRows
, mv
->NbColumns
);
522 Matrix_Product(inv
, mv
, L
);
531 evalue
*remainders
[dim
];
532 for (int i
= 0; i
< dim
; ++i
) {
533 remainders
[i
] = evalue_zero();
535 ceil(L
->p
[i
], nparam
+1, lcm
, one
, remainders
[i
], 0, options
);
540 for (int i
= 0; i
< V
->Vertex
->NbRows
; ++i
) {
541 values2zz(mv
->p
[i
], vertex
, nparam
+1);
542 E_vertex
[i
] = multi_monom(vertex
);
545 value_set_si(f
.x
.n
, 1);
546 value_assign(f
.d
, lcm
);
548 emul(&f
, E_vertex
[i
]);
550 for (int j
= 0; j
< dim
; ++j
) {
551 if (value_zero_p(T
->p
[i
][j
]))
555 evalue_copy(&cp
, remainders
[j
]);
556 if (value_notone_p(T
->p
[i
][j
])) {
557 value_set_si(f
.d
, 1);
558 value_assign(f
.x
.n
, T
->p
[i
][j
]);
561 eadd(&cp
, E_vertex
[i
]);
562 free_evalue_refs(&cp
);
565 for (int i
= 0; i
< dim
; ++i
) {
566 free_evalue_refs(remainders
[i
]);
570 free_evalue_refs(&f
);
581 for (int i
= 0; i
< V
->Vertex
->NbRows
; ++i
) {
583 if (First_Non_Zero(V
->Vertex
->p
[i
], nparam
) == -1) {
585 value2zz(V
->Vertex
->p
[i
][nparam
], num
[i
]);
587 values2zz(V
->Vertex
->p
[i
], vertex
, nparam
+1);
588 E_vertex
[i
] = multi_monom(vertex
);