evalue.c: extract evalue_extract_affine from bernstein.cc
[barvinok.git] / conversion.cc
blob4e1d7a63b9d9d1f98a084f47e0b40c8f7ca5e32d
1 #include <assert.h>
2 #include <gmp.h>
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];
19 SIZE(z.rep) = sa;
22 void zz2value(const ZZ& z, Value& v)
24 if (!z.rep) {
25 value_set_si(v, 0);
26 return;
29 int sa = SIZE(z.rep);
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];
36 v[0]._mp_size = sa;
39 void values2zz(Value *p, vec_ZZ& v, int len)
41 v.SetLength(len);
43 for (int i = 0; i < len; ++i) {
44 value2zz(p[i], v[i]);
50 void zz2values(const vec_ZZ& v, Value *p)
52 for (int i = 0; i < v.length(); ++i)
53 zz2value(v[i], p[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)
63 m.SetDims(nr, 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 *rays2matrix(const mat_ZZ& rays)
75 unsigned dim = rays.NumRows();
77 Matrix *M = Matrix_Alloc(dim+1, dim+1);
78 assert(M);
80 for (int i = 0; i < dim; ++i)
81 zz2values(rays[i], M->p[i]);
82 value_set_si(M->p[dim][dim], 1);
83 return M;
86 Matrix *rays2matrix2(const mat_ZZ& rays)
88 unsigned dim = rays.NumRows();
90 Matrix *M = Matrix_Alloc(dim, dim);
91 assert(M);
93 for (int i = 0; i < dim; ++i)
94 zz2values(rays[i], M->p[i]);
95 return M;
98 Matrix *rays(Polyhedron *C)
100 unsigned dim = C->NbRays - 1; /* don't count zero vertex */
101 assert(C->NbRays - 1 == C->Dimension);
103 Matrix *M = Matrix_Alloc(dim+1, dim+1);
104 assert(M);
106 int i, c;
107 for (i = 0, c = 0; i <= dim && c < dim; ++i)
108 if (value_zero_p(C->Ray[i][dim+1])) {
109 Vector_Copy(C->Ray[i] + 1, M->p[c], dim);
110 value_set_si(M->p[c++][dim], 0);
112 assert(c == dim);
113 value_set_si(M->p[dim][dim], 1);
115 return M;
118 Matrix *rays2(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, dim);
124 assert(M);
126 int i, c;
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 assert(c == dim);
132 return M;
135 void rays(Polyhedron *C, mat_ZZ& rays)
137 unsigned dim = C->NbRays - 1; /* don't count zero vertex */
138 assert(C->NbRays - 1 == C->Dimension);
139 rays.SetDims(dim, dim);
141 int i, j;
142 for (i = 0, j = 0; i < C->NbRays; ++i) {
143 if (value_notzero_p(C->Ray[i][dim+1]))
144 continue;
145 values2zz(C->Ray[i]+1, rays[j], dim);
146 ++j;
150 void randomvector(Polyhedron *P, vec_ZZ& lambda, int nvar)
152 Value tmp;
153 int max = 10 * 16;
154 unsigned int dim = P->Dimension;
155 value_init(tmp);
157 for (int i = 0; i < P->NbRays; ++i) {
158 for (int j = 1; j <= dim; ++j) {
159 value_absolute(tmp, P->Ray[i][j]);
160 int t = VALUE_TO_LONG(tmp) * 16;
161 if (t > max)
162 max = t;
165 for (int i = 0; i < P->NbConstraints; ++i) {
166 for (int j = 1; j <= dim; ++j) {
167 value_absolute(tmp, P->Constraint[i][j]);
168 int t = VALUE_TO_LONG(tmp) * 16;
169 if (t > max)
170 max = t;
173 value_clear(tmp);
175 lambda.SetLength(nvar);
176 for (int k = 0; k < nvar; ++k) {
177 int r = random_int(max*dim)+2;
178 int v = (2*(r%2)-1) * (max/2*dim + (r >> 1));
179 lambda[k] = v;