Macaulay2 Engine
Loading...
Searching...
No Matches
ntl-interface.cpp
Go to the documentation of this file.
1// Copyright 2005, Michael Stillman
2
3#include "ntl-interface.hpp"
4#include "mat.hpp"
5
7{
8 // Creates the TRANSPOSE of M
9
10 // We assume or check that the ring is ZZ
11
12 // const SparseMutableMatrix *A = M->cast_to_SparseMutableMatrix();
13 const MutableMatrix *A = M;
14
15 int ncols = static_cast<int>(A->n_rows());
16 int nrows = static_cast<int>(A->n_cols());
17
18 NTL::mat_ZZ *X = makeNTLMatrixZZ(nrows, ncols);
19 for (int i = 0; i < ncols; i++)
20 for (int j = 0; j < nrows; j++)
21 {
22 ring_elem a;
23 if (A->get_entry(i, j, a))
24 {
25 mat_ZZ_set_entry(X, j, i, a.get_mpz());
26 }
27 }
28
29 return X;
30}
32{
33 // AGAIN: form the TRANSPOSE of A
34 size_t ncols = A->NumRows();
35 size_t nrows = A->NumCols();
36 MutableMatrix *B = MutableMatrix::zero_matrix(globalZZ, nrows, ncols, false);
37
38 mpz_t a;
39 mpz_init(a);
40
41 for (size_t i = 0; i < ncols; i++)
42 for (size_t j = 0; j < nrows; j++)
43 {
44 if ((*A)(i + 1, j + 1) != 0)
45 {
46 mat_ZZ_get_entry(A, i, j, a);
47 B->set_entry(j, i, ring_elem(a));
48 }
49 }
50 mpz_clear(a);
51 return B;
52}
53
54#ifdef GS // Unfortunate Solaris macro
55#undef GS
56#endif
57
58static const int useNTL = 2;
59static const int GS = 0;
60static const int Givens = 4;
61static const int useLLL = 0;
62static const int useBKZ = 8;
63static const int FP = 16;
64static const int QP1 = 2 * 16;
65static const int QP = 3 * 16;
66static const int XD = 4 * 16;
67static const int useRR = 5 * 16;
68
71 long numer,
72 long denom,
73 int strategy)
74{
75 int nrows = static_cast<int>(M->n_rows());
76 int ncols = static_cast<int>(M->n_cols());
77
78 NTL::ZZ d;
79 // Note that the LLL routines all return the rank, but we ignore this return
80 // value.
81 double delta = static_cast<double>(numer) / static_cast<double>(denom);
82
83 if (M2_gbTrace >= 10) printf("LLL: using strategy %d\n", strategy);
84 NTL::mat_ZZ *A = mutableMatrix_to_NTL_mat_ZZ(M);
85 NTL::mat_ZZ *V = (U ? mutableMatrix_to_NTL_mat_ZZ(U) : nullptr);
86
87 switch (strategy)
88 {
89 case 2:
90 if (!V)
91 LLL(d, *A, numer, denom);
92 else
93 LLL(d, *A, *V, numer, denom);
94 break;
95
96 case useNTL + GS + useLLL + FP:
97 if (!V)
98 LLL_FP(*A, delta);
99 else
100 LLL_FP(*A, *V, delta);
101 break;
102 case useNTL + GS + useLLL + QP:
103 case useNTL + GS + useLLL + QP1:
104 if (!V)
105 LLL_QP(*A, delta);
106 else
107 LLL_QP(*A, *V, delta);
108 break;
109 case useNTL + GS + useLLL + XD:
110 if (!V)
111 LLL_XD(*A, delta);
112 else
113 LLL_XD(*A, *V, delta);
114 break;
115 case useNTL + GS + useLLL + useRR:
116 if (!V)
117 LLL_RR(*A, delta);
118 else
119 LLL_RR(*A, *V, delta);
120 break;
121
122 case useNTL + GS + useBKZ + FP:
123 if (!V)
124 BKZ_FP(*A, delta);
125 else
126 BKZ_FP(*A, *V, delta);
127 break;
128 case useNTL + GS + useBKZ + QP:
129 if (!V)
130 BKZ_QP(*A, delta);
131 else
132 BKZ_QP(*A, *V, delta);
133 break;
134 case useNTL + GS + useBKZ + QP1:
135 if (!V)
136 BKZ_QP1(*A, delta);
137 else
138 BKZ_QP1(*A, *V, delta);
139 break;
140 case useNTL + GS + useBKZ + XD:
141 if (!V)
142 BKZ_XD(*A, delta);
143 else
144 BKZ_XD(*A, *V, delta);
145 break;
146 case useNTL + GS + useBKZ + useRR:
147 if (!V)
148 BKZ_RR(*A, delta);
149 else
150 BKZ_RR(*A, *V, delta);
151 break;
152
153 case useNTL + Givens + useLLL + FP:
154 if (!V)
155 G_LLL_FP(*A, delta);
156 else
157 G_LLL_FP(*A, *V, delta);
158 break;
159 case useNTL + Givens + useLLL + QP:
160 case useNTL + Givens + useLLL + QP1:
161 if (!V)
162 G_LLL_QP(*A, delta);
163 else
164 G_LLL_QP(*A, *V, delta);
165 break;
166 case useNTL + Givens + useLLL + XD:
167 if (!V)
168 G_LLL_XD(*A, delta);
169 else
170 G_LLL_XD(*A, *V, delta);
171 break;
172 case useNTL + Givens + useLLL + useRR:
173 if (!V)
174 G_LLL_RR(*A, delta);
175 else
176 G_LLL_RR(*A, *V, delta);
177 break;
178
179 case useNTL + Givens + useBKZ + FP:
180 if (!V)
181 G_BKZ_FP(*A, delta);
182 else
183 G_BKZ_FP(*A, *V, delta);
184 break;
185 case useNTL + Givens + useBKZ + QP:
186 if (!V)
187 G_BKZ_QP(*A, delta);
188 else
189 G_BKZ_QP(*A, *V, delta);
190 break;
191 case useNTL + Givens + useBKZ + QP1:
192 if (!V)
193 G_BKZ_QP1(*A, delta);
194 else
195 G_BKZ_QP1(*A, *V, delta);
196 break;
197 case useNTL + Givens + useBKZ + XD:
198 if (!V)
199 G_BKZ_XD(*A, delta);
200 else
201 G_BKZ_XD(*A, *V, delta);
202 break;
203 case useNTL + Givens + useBKZ + useRR:
204 if (!V)
205 G_BKZ_RR(*A, delta);
206 else
207 G_BKZ_RR(*A, *V, delta);
208 break;
209 default:
210 delete A;
211 if (V) delete V;
212 ERROR("Strategy option to LLL not understood");
213 return false;
214 }
215
216 /* Put this back into M */
217 mpz_t a;
218 mpz_init(a);
219
220 for (int j = 0; j < ncols; j++)
221 for (int i = 0; i < nrows; i++)
222 {
223 mat_ZZ_get_entry(A, j, i, a);
224 ring_elem b = globalZZ->from_int(a);
225 M->set_entry(i, j, b);
226 }
227
228 if (U)
229 {
230 for (int j = 0; j < ncols; j++)
231 for (int i = 0; i < ncols; i++)
232 {
233 mat_ZZ_get_entry(V, j, i, a);
234 ring_elem b = globalZZ->from_int(a);
235 U->set_entry(i, j, b);
236 }
237 }
238 mpz_clear(a);
239 delete A;
240 if (V) delete V;
241 return true;
242}
243// Local Variables:
244// compile-command: "make -C $M2BUILDDIR/Macaulay2/e "
245// indent-tabs-mode: nil
246// End:
virtual size_t n_rows() const =0
virtual size_t n_cols() const =0
virtual bool get_entry(size_t r, size_t c, ring_elem &result) const =0
static MutableMatrix * zero_matrix(const Ring *R, size_t nrows, size_t ncols, bool dense)
Definition mat.cpp:54
virtual bool set_entry(size_t r, size_t c, const ring_elem a)=0
Abstract base class for mutable matrices over an arbitrary engine Ring, the in-place counterpart of t...
Definition mat.hpp:79
RingZZ * globalZZ
Definition relem.cpp:13
const int ERROR
Definition m2-mem.cpp:55
int M2_gbTrace
Definition m2-types.cpp:52
MutableMatrix — abstract base of every mutable matrix the engine hands across the boundary.
static const int useLLL
static const int GS
static const int Givens
static const int useNTL
static const int useRR
static const int FP
static const int QP
static const int useBKZ
NTL::mat_ZZ * mutableMatrix_to_NTL_mat_ZZ(const MutableMatrix *M)
MutableMatrix * mutableMatrix_from_NTL_mat_ZZ(const NTL::mat_ZZ *A)
static const int XD
static const int QP1
bool ntl_LLL(MutableMatrix *M, MutableMatrix *U, long numer, long denom, int strategy)
void mat_ZZ_get_entry(const NTL::mat_ZZ *A, long i, long j, mpz_t result)
void mat_ZZ_set_entry(NTL::mat_ZZ *A, long i, long j, mpz_srcptr a)
NTL::mat_ZZ * makeNTLMatrixZZ(int nrows, int ncols)
Engine bridge into NTL — type conversions and the NTL-backed LLL entry point.
mpz_srcptr get_mpz() const
Definition ringelem.hpp:127