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"
10 #include "param_util.h"
15 #define ALLOC(type) (type*)malloc(sizeof(type))
17 /* returns an evalue that corresponds to
21 static evalue
*term(int param
, ZZ
& c
, Value
*den
= NULL
)
23 evalue
*EP
= new evalue();
25 value_set_si(EP
->d
,0);
26 EP
->x
.p
= new_enode(polynomial
, 2, param
+ 1);
27 evalue_set_si(&EP
->x
.p
->arr
[0], 0, 1);
28 value_init(EP
->x
.p
->arr
[1].x
.n
);
30 value_set_si(EP
->x
.p
->arr
[1].d
, 1);
32 value_assign(EP
->x
.p
->arr
[1].d
, *den
);
33 zz2value(c
, EP
->x
.p
->arr
[1].x
.n
);
37 /* returns an evalue that corresponds to
41 evalue
*multi_monom(vec_ZZ
& p
)
43 evalue
*X
= ALLOC(evalue
);
46 unsigned nparam
= p
.length()-1;
47 zz2value(p
[nparam
], X
->x
.n
);
48 value_set_si(X
->d
, 1);
49 for (int i
= 0; i
< nparam
; ++i
) {
52 evalue
*T
= term(i
, p
[i
]);
61 * Check whether mapping polyhedron P on the affine combination
62 * num yields a range that has a fixed quotient on integer
64 * If zero is true, then we are only interested in the quotient
65 * for the cases where the remainder is zero.
66 * Returns NULL if false and a newly allocated value if true.
68 static Value
*fixed_quotient(Polyhedron
*P
, vec_ZZ
& num
, Value d
, bool zero
)
71 int len
= num
.length();
72 Matrix
*T
= Matrix_Alloc(2, len
);
73 zz2values(num
, T
->p
[0]);
74 value_set_si(T
->p
[1][len
-1], 1);
75 Polyhedron
*I
= Polyhedron_Image(P
, T
, P
->NbConstraints
);
79 for (i
= 0; i
< I
->NbRays
; ++i
)
80 if (value_zero_p(I
->Ray
[i
][2])) {
88 int bounded
= line_minmax(I
, &min
, &max
);
92 mpz_cdiv_q(min
, min
, d
);
94 mpz_fdiv_q(min
, min
, d
);
95 mpz_fdiv_q(max
, max
, d
);
97 if (value_eq(min
, max
)) {
100 value_assign(*ret
, min
);
108 * Normalize linear expression coef modulo m
109 * Removes common factor and reduces coefficients
110 * Returns index of first non-zero coefficient or len
112 int normal_mod(Value
*coef
, int len
, Value
*m
)
117 Vector_Gcd(coef
, len
, &gcd
);
118 value_gcd(gcd
, gcd
, *m
);
119 Vector_AntiScale(coef
, coef
, gcd
, len
);
121 value_division(*m
, *m
, gcd
);
128 for (j
= 0; j
< len
; ++j
)
129 mpz_fdiv_r(coef
[j
], coef
[j
], *m
);
130 for (j
= 0; j
< len
; ++j
)
131 if (value_notzero_p(coef
[j
]))
137 static bool mod_needed(Polyhedron
*PD
, vec_ZZ
& num
, Value d
, evalue
*E
)
139 Value
*q
= fixed_quotient(PD
, num
, d
, false);
144 value_oppose(*q
, *q
);
147 value_set_si(EV
.d
, 1);
149 value_multiply(EV
.x
.n
, *q
, d
);
151 free_evalue_refs(&EV
);
157 /* Computes the fractional part of the affine expression specified
158 * by coef (of length nvar+1) and the denominator denom.
159 * If PD is not NULL, then it specifies additional constraints
160 * on the variables that may be used to simplify the resulting
161 * fractional part expression.
163 * Modifies coef argument !
165 evalue
*fractional_part(Value
*coef
, Value denom
, int nvar
, Polyhedron
*PD
)
169 evalue
*EP
= evalue_zero();
172 value_assign(m
, denom
);
173 int j
= normal_mod(coef
, nvar
+1, &m
);
181 values2zz(coef
, num
, nvar
+1);
188 evalue_set_si(&tmp
, 0, 1);
192 while (j
< nvar
&& (num
[j
] == g
/2 || num
[j
] == 0))
194 if ((j
< nvar
&& num
[j
] > g
/2) || (j
== nvar
&& num
[j
] >= (g
+1)/2)) {
195 for (int k
= j
; k
< nvar
; ++k
)
198 num
[nvar
] = g
- 1 - num
[nvar
];
199 value_assign(tmp
.d
, m
);
201 zz2value(t
, tmp
.x
.n
);
207 ZZ t
= num
[nvar
] * sign
;
208 zz2value(t
, tmp
.x
.n
);
209 value_assign(tmp
.d
, m
);
212 evalue
*E
= multi_monom(num
);
216 if (PD
&& !mod_needed(PD
, num
, m
, E
)) {
218 value_set_si(EV
.x
.n
, sign
);
219 value_assign(EV
.d
, m
);
224 value_set_si(EV
.x
.n
, 1);
225 value_assign(EV
.d
, m
);
228 value_set_si(EV
.d
, 0);
229 EV
.x
.p
= new_enode(fractional
, 3, -1);
230 evalue_copy(&EV
.x
.p
->arr
[0], E
);
231 evalue_set_si(&EV
.x
.p
->arr
[1], 0, 1);
232 value_init(EV
.x
.p
->arr
[2].x
.n
);
233 value_set_si(EV
.x
.p
->arr
[2].x
.n
, sign
);
234 value_set_si(EV
.x
.p
->arr
[2].d
, 1);
239 free_evalue_refs(&EV
);
243 free_evalue_refs(&tmp
);
251 static evalue
*ceil(Value
*coef
, int len
, Value d
,
252 barvinok_options
*options
)
256 Vector_Oppose(coef
, coef
, len
);
257 c
= fractional_part(coef
, d
, len
-1, NULL
);
258 if (options
->lookup_table
)
259 evalue_mod2table(c
, len
-1);
263 evalue
* bv_ceil3(Value
*coef
, int len
, Value d
, Polyhedron
*P
)
265 Vector
*val
= Vector_Alloc(len
);
270 Vector_Scale(coef
, val
->p
, t
, len
);
271 value_absolute(t
, d
);
274 values2zz(val
->p
, num
, len
);
275 evalue
*EP
= multi_monom(num
);
280 value_set_si(tmp
.x
.n
, 1);
281 value_assign(tmp
.d
, t
);
285 Vector_Oppose(val
->p
, val
->p
, len
);
286 evalue
*f
= fractional_part(val
->p
, t
, len
-1, P
);
292 free_evalue_refs(&tmp
);
298 void lattice_point_fixed(Value
*vertex
, Value
*vertex_res
,
299 Matrix
*Rays
, Matrix
*Rays_res
,
302 unsigned dim
= Rays
->NbRows
;
303 if (value_one_p(vertex
[dim
]))
304 Vector_Copy(vertex_res
, point
, Rays_res
->NbColumns
);
306 Matrix
*R2
= Matrix_Copy(Rays
);
307 Matrix
*inv
= Matrix_Alloc(Rays
->NbRows
, Rays
->NbColumns
);
308 int ok
= Matrix_Inverse(R2
, inv
);
311 Vector
*lambda
= Vector_Alloc(dim
);
312 Vector_Matrix_Product(vertex
, inv
, lambda
->p
);
314 for (int j
= 0; j
< dim
; ++j
)
315 mpz_cdiv_q(lambda
->p
[j
], lambda
->p
[j
], vertex
[dim
]);
316 Vector_Matrix_Product(lambda
->p
, Rays_res
, point
);
321 static Matrix
*Matrix_AddRowColumn(Matrix
*M
)
323 Matrix
*M2
= Matrix_Alloc(M
->NbRows
+1, M
->NbColumns
+1);
324 for (int i
= 0; i
< M
->NbRows
; ++i
)
325 Vector_Copy(M
->p
[i
], M2
->p
[i
], M
->NbColumns
);
326 value_set_si(M2
->p
[M
->NbRows
][M
->NbColumns
], 1);
330 #define FORALL_COSETS(det,D,i,k) \
332 Vector *k = Vector_Alloc(D->NbRows+1); \
333 value_set_si(k->p[D->NbRows], 1); \
334 for (unsigned long i = 0; i < det; ++i) { \
336 for (int j = D->NbRows-1; j >= 0; --j) { \
337 value_increment(k->p[j], k->p[j]); \
338 if (value_eq(k->p[j], D->p[j][j])) \
339 value_set_si(k->p[j], 0); \
344 #define END_FORALL_COSETS \
350 /* Compute the lattice points in the vertex cone at "values" with rays "rays".
351 * The lattice points are returned in "vertex".
353 * Rays has the generators as rows and so does W.
354 * We first compute { m-v, u_i^* } with m = k W, where k runs through
357 * [k 1] [ d1*W 0 ] [ U' 0 ] = [k 1] T2
359 * where d1 and d2 are the denominators of v and U^{-1}=U'/d2.
360 * Then lambda = { k } (componentwise)
361 * We compute x - floor(x) = {x} = { a/b } as fdiv_r(a,b)/b
362 * For open rays/facets, we need values in (0,1] rather than [0,1),
363 * so we compute {{x}} = x - ceil(x-1) = a/b - ceil((a-b)/b)
364 * = (a - b cdiv_q(a-b,b) - b + b)/b
365 * = (cdiv_r(a,b)+b)/b
366 * Finally, we compute v + lambda * U
367 * The denominator of lambda can be d1*d2, that of lambda2 = lambda*U
368 * can be at most d1, since it is integer if v = 0.
369 * The denominator of v + lambda2 is 1.
371 * The _res variants of the input variables may have been multiplied with
372 * a (list of) nonorthogonal vector(s) and may therefore have fewer columns
373 * than their original counterparts.
375 void lattice_points_fixed(Value
*vertex
, Value
*vertex_res
,
376 Matrix
*Rays
, Matrix
*Rays_res
, Matrix
*points
,
379 unsigned dim
= Rays
->NbRows
;
381 lattice_point_fixed(vertex
, vertex_res
, Rays
, Rays_res
,
386 Smith(Rays
, &U
, &W
, &D
);
390 unsigned long det2
= 1;
391 for (int i
= 0 ; i
< D
->NbRows
; ++i
)
392 det2
*= mpz_get_ui(D
->p
[i
][i
]);
395 Matrix
*T
= Matrix_Alloc(W
->NbRows
+1, W
->NbColumns
+1);
396 for (int i
= 0; i
< W
->NbRows
; ++i
)
397 Vector_Scale(W
->p
[i
], T
->p
[i
], vertex
[dim
], W
->NbColumns
);
399 Vector_Oppose(vertex
, T
->p
[dim
], dim
);
400 value_assign(T
->p
[dim
][dim
], vertex
[dim
]);
402 Matrix
*R2
= Matrix_AddRowColumn(Rays
);
403 Matrix
*inv
= Matrix_Alloc(R2
->NbRows
, R2
->NbColumns
);
404 int ok
= Matrix_Inverse(R2
, inv
);
408 Matrix
*T2
= Matrix_Alloc(dim
+1, dim
+1);
409 Matrix_Product(T
, inv
, T2
);
412 Vector
*lambda
= Vector_Alloc(dim
+1);
413 Vector
*lambda2
= Vector_Alloc(Rays_res
->NbColumns
);
414 FORALL_COSETS(det
, D
, i
, k
)
415 Vector_Matrix_Product(k
->p
, T2
, lambda
->p
);
416 for (int j
= 0; j
< dim
; ++j
)
417 mpz_fdiv_r(lambda
->p
[j
], lambda
->p
[j
], lambda
->p
[dim
]);
418 Vector_Matrix_Product(lambda
->p
, Rays_res
, lambda2
->p
);
419 for (int j
= 0; j
< lambda2
->Size
; ++j
)
420 assert(mpz_divisible_p(lambda2
->p
[j
], inv
->p
[dim
][dim
]));
421 Vector_AntiScale(lambda2
->p
, lambda2
->p
, inv
->p
[dim
][dim
], lambda2
->Size
);
422 Vector_Add(lambda2
->p
, vertex_res
, lambda2
->p
, lambda2
->Size
);
423 for (int j
= 0; j
< lambda2
->Size
; ++j
)
424 assert(mpz_divisible_p(lambda2
->p
[j
], vertex
[dim
]));
425 Vector_AntiScale(lambda2
->p
, points
->p
[i
], vertex
[dim
], lambda2
->Size
);
428 Vector_Free(lambda2
);
435 /* Returns the power of (t+1) in the term of a rational generating function,
436 * i.e., the scalar product of the actual lattice point and lambda.
437 * The lattice point is the unique lattice point in the fundamental parallelepiped
438 * of the unimodual cone i shifted to the parametric vertex W/lcm.
440 * The rows of W refer to the coordinates of the vertex
441 * The first nparam columns are the coefficients of the parameters
442 * and the final column is the constant term.
443 * lcm is the common denominator of all coefficients.
445 static evalue
**lattice_point_fractional(const mat_ZZ
& rays
, vec_ZZ
& lambda
,
449 unsigned nparam
= V
->NbColumns
-2;
450 evalue
**E
= new evalue
*[det
];
452 Matrix
* Rays
= zz2matrix(rays
);
453 Matrix
*T
= Transpose(Rays
);
454 Matrix
*T2
= Matrix_Copy(T
);
455 Matrix
*inv
= Matrix_Alloc(T2
->NbRows
, T2
->NbColumns
);
456 int ok
= Matrix_Inverse(T2
, inv
);
460 matrix2zz(V
, vertex
, V
->NbRows
, V
->NbColumns
-1);
463 num
= lambda
* vertex
;
465 evalue
*EP
= multi_monom(num
);
467 evalue_div(EP
, V
->p
[0][nparam
+1]);
469 Matrix
*L
= Matrix_Alloc(inv
->NbRows
, V
->NbColumns
);
470 Matrix_Product(inv
, V
, L
);
473 matrix2zz(T
, RT
, T
->NbRows
, T
->NbColumns
);
476 vec_ZZ p
= lambda
* RT
;
482 for (int i
= 0; i
< L
->NbRows
; ++i
) {
484 Vector_Oppose(L
->p
[i
], L
->p
[i
], nparam
+1);
485 f
= fractional_part(L
->p
[i
], V
->p
[i
][nparam
+1], nparam
, NULL
);
493 for (int i
= 0; i
< L
->NbRows
; ++i
)
494 value_assign(L
->p
[i
][nparam
+1], V
->p
[i
][nparam
+1]);
498 mpz_set_ui(denom
, det
);
499 value_multiply(denom
, L
->p
[0][nparam
+1], denom
);
502 Smith(Rays
, &U
, &W
, &D
);
506 unsigned long det2
= 1;
507 for (int i
= 0 ; i
< D
->NbRows
; ++i
)
508 det2
*= mpz_get_ui(D
->p
[i
][i
]);
511 Matrix_Transposition(inv
);
512 Matrix
*T2
= Matrix_Alloc(W
->NbRows
, inv
->NbColumns
);
513 Matrix_Product(W
, inv
, T2
);
516 unsigned dim
= D
->NbRows
;
517 Vector
*lambda
= Vector_Alloc(dim
);
519 Vector
*row
= Vector_Alloc(nparam
+1);
520 FORALL_COSETS(det
, D
, i
, k
)
521 Vector_Matrix_Product(k
->p
, T2
, lambda
->p
);
522 E
[i
] = ALLOC(evalue
);
524 evalue_copy(E
[i
], EP
);
525 for (int j
= 0; j
< L
->NbRows
; ++j
) {
527 Vector_Oppose(L
->p
[j
], row
->p
, nparam
+1);
528 value_addmul(row
->p
[nparam
], L
->p
[j
][nparam
+1], lambda
->p
[j
]);
529 f
= fractional_part(row
->p
, denom
, nparam
, NULL
);
554 static evalue
**lattice_point(const mat_ZZ
& rays
, vec_ZZ
& lambda
,
557 barvinok_options
*options
)
559 evalue
**lp
= lattice_point_fractional(rays
, lambda
, V
->Vertex
, det
);
560 if (options
->lookup_table
) {
561 for (int i
= 0; i
< det
; ++i
)
562 evalue_mod2table(lp
[i
], V
->Vertex
->NbColumns
-2);
567 /* returns the unique lattice point in the fundamental parallelepiped
568 * of the unimodual cone C shifted to the parametric vertex V.
570 * The return values num and E_vertex are such that
571 * coordinate i of this lattice point is equal to
573 * num[i] + E_vertex[i]
575 void lattice_point(Param_Vertices
*V
, const mat_ZZ
& rays
, vec_ZZ
& num
,
576 evalue
**E_vertex
, barvinok_options
*options
)
578 unsigned nparam
= V
->Vertex
->NbColumns
- 2;
579 unsigned dim
= rays
.NumCols();
581 /* It doesn't really make sense to call lattice_point when dim == 0,
582 * but apparently it happens from indicator_constructor in lexmin.
588 vertex
.SetLength(nparam
+1);
593 assert(V
->Vertex
->NbRows
> 0);
594 Param_Vertex_Common_Denominator(V
);
596 if (value_notone_p(V
->Vertex
->p
[0][nparam
+1])) {
597 Matrix
* Rays
= zz2matrix(rays
);
598 Matrix
*T
= Transpose(Rays
);
599 Matrix
*T2
= Matrix_Copy(T
);
600 Matrix
*inv
= Matrix_Alloc(T2
->NbRows
, T2
->NbColumns
);
601 int ok
= Matrix_Inverse(T2
, inv
);
605 /* temporarily ignore (common) denominator */
606 V
->Vertex
->NbColumns
--;
607 Matrix
*L
= Matrix_Alloc(inv
->NbRows
, V
->Vertex
->NbColumns
);
608 Matrix_Product(inv
, V
->Vertex
, L
);
609 V
->Vertex
->NbColumns
++;
616 evalue
*remainders
[dim
];
617 for (int i
= 0; i
< dim
; ++i
)
618 remainders
[i
] = ceil(L
->p
[i
], nparam
+1, V
->Vertex
->p
[0][nparam
+1],
623 for (int i
= 0; i
< V
->Vertex
->NbRows
; ++i
) {
624 values2zz(V
->Vertex
->p
[i
], vertex
, nparam
+1);
625 E_vertex
[i
] = multi_monom(vertex
);
628 value_set_si(f
.x
.n
, 1);
629 value_assign(f
.d
, V
->Vertex
->p
[0][nparam
+1]);
631 emul(&f
, E_vertex
[i
]);
633 for (int j
= 0; j
< dim
; ++j
) {
634 if (value_zero_p(T
->p
[i
][j
]))
638 evalue_copy(&cp
, remainders
[j
]);
639 if (value_notone_p(T
->p
[i
][j
])) {
640 value_set_si(f
.d
, 1);
641 value_assign(f
.x
.n
, T
->p
[i
][j
]);
644 eadd(&cp
, E_vertex
[i
]);
645 free_evalue_refs(&cp
);
648 for (int i
= 0; i
< dim
; ++i
)
649 evalue_free(remainders
[i
]);
651 free_evalue_refs(&f
);
659 for (int i
= 0; i
< V
->Vertex
->NbRows
; ++i
) {
661 if (First_Non_Zero(V
->Vertex
->p
[i
], nparam
) == -1) {
663 value2zz(V
->Vertex
->p
[i
][nparam
], num
[i
]);
665 values2zz(V
->Vertex
->p
[i
], vertex
, nparam
+1);
666 E_vertex
[i
] = multi_monom(vertex
);
672 static int lattice_point_fixed(Param_Vertices
* V
, const mat_ZZ
& rays
,
673 vec_ZZ
& lambda
, term_info
* term
, unsigned long det
)
675 unsigned nparam
= V
->Vertex
->NbColumns
- 2;
676 unsigned dim
= rays
.NumCols();
678 for (int i
= 0; i
< dim
; ++i
)
679 if (First_Non_Zero(V
->Vertex
->p
[i
], nparam
) != -1)
682 Vector
*fixed
= Vector_Alloc(dim
+1);
683 for (int i
= 0; i
< dim
; ++i
)
684 value_assign(fixed
->p
[i
], V
->Vertex
->p
[i
][nparam
]);
685 value_assign(fixed
->p
[dim
], V
->Vertex
->p
[0][nparam
+1]);
688 Matrix
*points
= Matrix_Alloc(det
, dim
);
689 Matrix
* Rays
= zz2matrix(rays
);
690 lattice_points_fixed(fixed
->p
, fixed
->p
, Rays
, Rays
, points
, det
);
692 matrix2zz(points
, vertex
, points
->NbRows
, points
->NbColumns
);
695 term
->constant
= vertex
* lambda
;
701 /* Returns the power of (t+1) in the term of a rational generating function,
702 * i.e., the scalar product of the actual lattice point and lambda.
703 * The lattice point is the unique lattice point in the fundamental parallelepiped
704 * of the unimodual cone i shifted to the parametric vertex V.
706 * The result is returned in term.
708 void lattice_point(Param_Vertices
* V
, const mat_ZZ
& rays
, vec_ZZ
& lambda
,
709 term_info
* term
, unsigned long det
,
710 barvinok_options
*options
)
712 unsigned nparam
= V
->Vertex
->NbColumns
- 2;
713 unsigned dim
= rays
.NumCols();
715 vertex
.SetDims(V
->Vertex
->NbRows
, nparam
+1);
717 Param_Vertex_Common_Denominator(V
);
719 if (lattice_point_fixed(V
, rays
, lambda
, term
, det
))
722 if (det
!= 1 || value_notone_p(V
->Vertex
->p
[0][nparam
+1])) {
723 term
->E
= lattice_point(rays
, lambda
, V
, det
, options
);
726 for (int i
= 0; i
< V
->Vertex
->NbRows
; ++i
) {
727 assert(value_one_p(V
->Vertex
->p
[i
][nparam
+1])); // for now
728 values2zz(V
->Vertex
->p
[i
], vertex
[i
], nparam
+1);
732 num
= lambda
* vertex
;
735 for (int j
= 0; j
< nparam
; ++j
)
739 term
->E
= new evalue
*[1];
740 term
->E
[0] = multi_monom(num
);
743 term
->constant
.SetLength(1);
744 term
->constant
[0] = num
[nparam
];