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 /* Access the internal representation of a ZZ.
11 * In newer versions of NTL (since 8.0.0), the internal representation
12 * is wrapped inside a WrappedPtr, but it has an addess-of operator
13 * that returns the address of the actual internal representation.
15 #define REP(z) (*&(z).rep)
17 void value2zz(Value v
, ZZ
& z
)
19 int sa
= v
[0]._mp_size
;
20 int abs_sa
= sa
< 0 ? -sa
: sa
;
22 _ntl_gsetlength(&z
.rep
, abs_sa
);
23 mp_limb_t
* adata
= DATA(REP(z
));
24 for (int i
= 0; i
< abs_sa
; ++i
)
25 adata
[i
] = v
[0]._mp_d
[i
];
29 void zz2value(const ZZ
& z
, Value
& v
)
36 int sa
= SIZE(REP(z
));
37 int abs_sa
= sa
< 0 ? -sa
: sa
;
39 mp_limb_t
* adata
= DATA(REP(z
));
40 _mpz_realloc(v
, abs_sa
);
41 for (int i
= 0; i
< abs_sa
; ++i
)
42 v
[0]._mp_d
[i
] = adata
[i
];
46 void values2zz(Value
*p
, vec_ZZ
& v
, int len
)
50 for (int i
= 0; i
< len
; ++i
) {
57 void zz2values(const vec_ZZ
& v
, Value
*p
)
59 for (int i
= 0; i
< v
.length(); ++i
)
64 * We just ignore the last column and row
65 * If the final element is not equal to one
66 * then the result will actually be a multiple of the input
68 void matrix2zz(Matrix
*M
, mat_ZZ
& m
, unsigned nr
, unsigned nc
)
72 for (int i
= 0; i
< nr
; ++i
) {
73 // assert(value_one_p(M->p[i][M->NbColumns - 1]));
74 for (int j
= 0; j
< nc
; ++j
) {
75 value2zz(M
->p
[i
][j
], m
[i
][j
]);
80 Matrix
*zz2matrix(const mat_ZZ
& mat
)
82 Matrix
*M
= Matrix_Alloc(mat
.NumRows(), mat
.NumCols());
85 for (int i
= 0; i
< mat
.NumRows(); ++i
)
86 zz2values(mat
[i
], M
->p
[i
]);
90 Matrix
*rays(Polyhedron
*C
)
92 unsigned dim
= C
->NbRays
- 1; /* don't count zero vertex */
93 assert(C
->NbRays
- 1 == C
->Dimension
);
95 Matrix
*M
= Matrix_Alloc(dim
+1, dim
+1);
99 for (i
= 0, c
= 0; i
<= dim
&& c
< dim
; ++i
)
100 if (value_zero_p(C
->Ray
[i
][dim
+1])) {
101 Vector_Copy(C
->Ray
[i
] + 1, M
->p
[c
], dim
);
102 value_set_si(M
->p
[c
++][dim
], 0);
105 value_set_si(M
->p
[dim
][dim
], 1);
110 Matrix
*rays2(Polyhedron
*C
)
112 unsigned dim
= C
->NbRays
- 1; /* don't count zero vertex */
113 assert(C
->NbRays
- 1 == C
->Dimension
);
115 Matrix
*M
= Matrix_Alloc(dim
, dim
);
119 for (i
= 0, c
= 0; i
<= dim
&& c
< dim
; ++i
)
120 if (value_zero_p(C
->Ray
[i
][dim
+1]))
121 Vector_Copy(C
->Ray
[i
] + 1, M
->p
[c
++], dim
);
127 void rays(Polyhedron
*C
, mat_ZZ
& rays
)
129 unsigned dim
= C
->NbRays
- 1; /* don't count zero vertex */
130 assert(C
->NbRays
- 1 == C
->Dimension
);
131 rays
.SetDims(dim
, dim
);
134 for (i
= 0, j
= 0; i
< C
->NbRays
; ++i
) {
135 if (value_notzero_p(C
->Ray
[i
][dim
+1]))
137 values2zz(C
->Ray
[i
]+1, rays
[j
], dim
);
142 void randomvector(Polyhedron
*P
, vec_ZZ
& lambda
, int nvar
, int n_try
)
146 unsigned int dim
= P
->Dimension
;
149 for (int i
= 0; i
< P
->NbRays
; ++i
) {
150 for (int j
= 1; j
<= dim
; ++j
) {
151 value_absolute(tmp
, P
->Ray
[i
][j
]);
152 int t
= VALUE_TO_LONG(tmp
) * 16;
157 for (int i
= 0; i
< P
->NbConstraints
; ++i
) {
158 for (int j
= 1; j
<= dim
; ++j
) {
159 value_absolute(tmp
, P
->Constraint
[i
][j
]);
160 int t
= VALUE_TO_LONG(tmp
) * 16;
169 lambda
.SetLength(nvar
);
170 for (int k
= 0; k
< nvar
; ++k
) {
171 int r
= random_int(max
*dim
)+2;
172 int v
= (2*(r
%2)-1) * (max
/2*dim
+ (r
>> 1));