3 #include <NTL/mat_ZZ.h>
4 #include <barvinok/util.h>
5 #include "conversion.h"
7 #define SIZE(p) (((long *) (p))[1])
8 #define DATA(p) ((mp_limb_t *) (((long *) (p)) + 2))
10 void value2zz(Value v
, ZZ
& z
)
12 int sa
= v
[0]._mp_size
;
13 int abs_sa
= sa
< 0 ? -sa
: sa
;
15 _ntl_gsetlength(&z
.rep
, abs_sa
);
16 mp_limb_t
* adata
= DATA(z
.rep
);
17 for (int i
= 0; i
< abs_sa
; ++i
)
18 adata
[i
] = v
[0]._mp_d
[i
];
22 void zz2value(const ZZ
& z
, Value
& v
)
30 int abs_sa
= sa
< 0 ? -sa
: sa
;
32 mp_limb_t
* adata
= DATA(z
.rep
);
33 _mpz_realloc(v
, abs_sa
);
34 for (int i
= 0; i
< abs_sa
; ++i
)
35 v
[0]._mp_d
[i
] = adata
[i
];
39 void values2zz(Value
*p
, vec_ZZ
& v
, int len
)
43 for (int i
= 0; i
< len
; ++i
) {
50 void zz2values(const vec_ZZ
& v
, Value
*p
)
52 for (int i
= 0; i
< v
.length(); ++i
)
57 * We just ignore the last column and row
58 * If the final element is not equal to one
59 * then the result will actually be a multiple of the input
61 void matrix2zz(Matrix
*M
, mat_ZZ
& m
, unsigned nr
, unsigned nc
)
65 for (int i
= 0; i
< nr
; ++i
) {
66 // assert(value_one_p(M->p[i][M->NbColumns - 1]));
67 for (int j
= 0; j
< nc
; ++j
) {
68 value2zz(M
->p
[i
][j
], m
[i
][j
]);
73 Matrix
*zz2matrix(const mat_ZZ
& mat
)
75 Matrix
*M
= Matrix_Alloc(mat
.NumRows(), mat
.NumCols());
78 for (int i
= 0; i
< mat
.NumRows(); ++i
)
79 zz2values(mat
[i
], M
->p
[i
]);
83 Matrix
*rays(Polyhedron
*C
)
85 unsigned dim
= C
->NbRays
- 1; /* don't count zero vertex */
86 assert(C
->NbRays
- 1 == C
->Dimension
);
88 Matrix
*M
= Matrix_Alloc(dim
+1, dim
+1);
92 for (i
= 0, c
= 0; i
<= dim
&& c
< dim
; ++i
)
93 if (value_zero_p(C
->Ray
[i
][dim
+1])) {
94 Vector_Copy(C
->Ray
[i
] + 1, M
->p
[c
], dim
);
95 value_set_si(M
->p
[c
++][dim
], 0);
98 value_set_si(M
->p
[dim
][dim
], 1);
103 Matrix
*rays2(Polyhedron
*C
)
105 unsigned dim
= C
->NbRays
- 1; /* don't count zero vertex */
106 assert(C
->NbRays
- 1 == C
->Dimension
);
108 Matrix
*M
= Matrix_Alloc(dim
, dim
);
112 for (i
= 0, c
= 0; i
<= dim
&& c
< dim
; ++i
)
113 if (value_zero_p(C
->Ray
[i
][dim
+1]))
114 Vector_Copy(C
->Ray
[i
] + 1, M
->p
[c
++], dim
);
120 void rays(Polyhedron
*C
, mat_ZZ
& rays
)
122 unsigned dim
= C
->NbRays
- 1; /* don't count zero vertex */
123 assert(C
->NbRays
- 1 == C
->Dimension
);
124 rays
.SetDims(dim
, dim
);
127 for (i
= 0, j
= 0; i
< C
->NbRays
; ++i
) {
128 if (value_notzero_p(C
->Ray
[i
][dim
+1]))
130 values2zz(C
->Ray
[i
]+1, rays
[j
], dim
);
135 void randomvector(Polyhedron
*P
, vec_ZZ
& lambda
, int nvar
)
139 unsigned int dim
= P
->Dimension
;
142 for (int i
= 0; i
< P
->NbRays
; ++i
) {
143 for (int j
= 1; j
<= dim
; ++j
) {
144 value_absolute(tmp
, P
->Ray
[i
][j
]);
145 int t
= VALUE_TO_LONG(tmp
) * 16;
150 for (int i
= 0; i
< P
->NbConstraints
; ++i
) {
151 for (int j
= 1; j
<= dim
; ++j
) {
152 value_absolute(tmp
, P
->Constraint
[i
][j
]);
153 int t
= VALUE_TO_LONG(tmp
) * 16;
160 lambda
.SetLength(nvar
);
161 for (int k
= 0; k
< nvar
; ++k
) {
162 int r
= random_int(max
*dim
)+2;
163 int v
= (2*(r
%2)-1) * (max
/2*dim
+ (r
>> 1));