zz2value: make first argument const
[barvinok.git] / conversion.cc
blobaf044bbe1acd9c429699e4193759f8a53d23f002
1 #include <gmp.h>
2 #include <NTL/mat_ZZ.h>
3 extern "C" {
4 #include <polylib/polylibgmp.h>
6 #include "conversion.h"
8 #define SIZE(p) (((long *) (p))[1])
9 #define DATA(p) ((mp_limb_t *) (((long *) (p)) + 2))
11 void value2zz(Value v, ZZ& z)
13 int sa = v[0]._mp_size;
14 int abs_sa = sa < 0 ? -sa : sa;
16 _ntl_gsetlength(&z.rep, abs_sa);
17 mp_limb_t * adata = DATA(z.rep);
18 for (int i = 0; i < abs_sa; ++i)
19 adata[i] = v[0]._mp_d[i];
20 SIZE(z.rep) = sa;
23 void zz2value(const ZZ& z, Value& v)
25 if (!z.rep) {
26 value_set_si(v, 0);
27 return;
30 int sa = SIZE(z.rep);
31 int abs_sa = sa < 0 ? -sa : sa;
33 mp_limb_t * adata = DATA(z.rep);
34 _mpz_realloc(v, abs_sa);
35 for (int i = 0; i < abs_sa; ++i)
36 v[0]._mp_d[i] = adata[i];
37 v[0]._mp_size = sa;
40 void values2zz(Value *p, vec_ZZ& v, int len)
42 v.SetLength(len);
44 for (int i = 0; i < len; ++i) {
45 value2zz(p[i], v[i]);
51 void zz2values(vec_ZZ& v, Value *p)
53 for (int i = 0; i < v.length(); ++i)
54 zz2value(v[i], p[i]);
58 * We just ignore the last column and row
59 * If the final element is not equal to one
60 * then the result will actually be a multiple of the input
62 void matrix2zz(Matrix *M, mat_ZZ& m, unsigned nr, unsigned nc)
64 m.SetDims(nr, nc);
66 for (int i = 0; i < nr; ++i) {
67 // assert(value_one_p(M->p[i][M->NbColumns - 1]));
68 for (int j = 0; j < nc; ++j) {
69 value2zz(M->p[i][j], m[i][j]);
74 Matrix *rays(Polyhedron *C)
76 unsigned dim = C->NbRays - 1; /* don't count zero vertex */
77 assert(C->NbRays - 1 == C->Dimension);
79 Matrix *M = Matrix_Alloc(dim+1, dim+1);
80 assert(M);
82 int i, c;
83 for (i = 0, c = 0; i <= dim && c < dim; ++i)
84 if (value_zero_p(C->Ray[i][dim+1])) {
85 Vector_Copy(C->Ray[i] + 1, M->p[c], dim);
86 value_set_si(M->p[c++][dim], 0);
88 assert(c == dim);
89 value_set_si(M->p[dim][dim], 1);
91 return M;
94 Matrix * rays2(Polyhedron *C)
96 unsigned dim = C->NbRays - 1; /* don't count zero vertex */
97 assert(C->NbRays - 1 == C->Dimension);
99 Matrix *M = Matrix_Alloc(dim, dim);
100 assert(M);
102 int i, c;
103 for (i = 0, c = 0; i <= dim && c < dim; ++i)
104 if (value_zero_p(C->Ray[i][dim+1]))
105 Vector_Copy(C->Ray[i] + 1, M->p[c++], dim);
106 assert(c == dim);
108 return M;