Macaulay2 Engine
Loading...
Searching...
No Matches

◆ doLLL()

int LLLoperations::doLLL ( MutableMatrix * A,
MutableMatrix * Achange,
MutableMatrix * LLLstate,
int nsteps = -1 )
staticprivate

Definition at line 241 of file LLL.cpp.

245{
246 size_t n = A->n_cols();
247 if (n == 0) return COMP_DONE;
248
249 // Extract the state from LLLstate:
250 int k, kmax;
251 ring_elem a, alphaTop, alphaBottom;
252 buffer o;
253
254 if (LLLstate->get_entry(0, n, a))
255 {
256 std::pair<bool, long> res = globalZZ->coerceToLongInteger(a);
257 assert(res.first);
258 k = static_cast<int>(res.second);
259 }
260 else
261 k = 0;
262
263 if (LLLstate->get_entry(0, n + 1, a))
264 {
265 std::pair<bool, long> res = globalZZ->coerceToLongInteger(a);
266 assert(res.first);
267 kmax = static_cast<int>(res.second);
268 }
269 else
270 kmax = 0;
271
272 LLLstate->get_entry(0, n + 2, alphaTop); // Don't free alphaTop!
273 LLLstate->get_entry(0, n + 3, alphaBottom);
274
275 while (k < n && nsteps != 0 && !system_interrupted())
276 {
277 if (M2_gbTrace >= 1)
278 {
279 o.reset();
280 o << ".";
281 if (M2_gbTrace >= 2) o << k;
282 if (nsteps % 20 == 0) o << newline;
283 emit(o.str());
284 }
285 nsteps--;
286
287 if (k > kmax)
288 {
289 if (M2_gbTrace == 1)
290 {
291 o.reset();
292 o << "." << k;
293 if (nsteps % 20 == 0) o << newline;
294 emit(o.str());
295 }
296
297 kmax = k;
298 for (int j = 0; j <= k; j++)
299 {
300 ring_elem u;
301 A->dot_product(k, j, u);
302 for (int i = 0; i <= j - 1; i++)
303 {
304 // u = (D#i * u - lambda#(k,i) * lambda#(j,i)) // D#(i-1)
305 ring_elem Di, mki, mji, Di1;
306 LLLstate->get_entry(i, i, Di);
307 globalZZ->mult_to(u, Di);
308 if (LLLstate->get_entry(i, k, mki) &&
309 LLLstate->get_entry(i, j, mji))
310 {
311 ring_elem t1 = globalZZ->mult(mki, mji);
312 globalZZ->subtract_to(u, t1);
313 }
314 if (i > 0)
315 {
316 LLLstate->get_entry(
317 i - 1, i - 1, Di1); // Cannot be zero!!
318 ring_elem t1 = globalZZ->divide(u, Di1);
319 globalZZ->remove(u);
320 u = t1;
321 }
322 }
323 // At this point we have our element:
324 LLLstate->set_entry(j, k, u);
325 if (j == k && globalZZ->is_zero(u))
326 {
327 ERROR("LLL vectors not independent");
328 return COMP_ERROR;
329 }
330 }
331 } // end of the k>kmax initialization
332 REDI(k, k - 1, A, Achange, LLLstate);
333 if (Lovasz(LLLstate, k, alphaTop, alphaBottom))
334 {
335 SWAPI(k, kmax, A, Achange, LLLstate);
336 k--;
337 if (k == 0) k = 1;
338 }
339 else
340 {
341 for (int ell = k - 2; ell >= 0; ell--)
342 REDI(k, ell, A, Achange, LLLstate);
343 k++;
344 }
345 }
346
347 // Before returning, reset k,kmax:
348 LLLstate->set_entry(0, n, globalZZ->from_long(k));
349 LLLstate->set_entry(0, n + 1, globalZZ->from_long(kmax));
350
351 if (k >= n) return COMP_DONE;
352 if (nsteps == 0) return COMP_DONE_STEPS;
353 return COMP_INTERRUPTED;
354}
static void SWAPI(int k, int kmax, MutableMatrix *A, MutableMatrix *Achange, MutableMatrix *lambda)
Definition LLL.cpp:143
static bool Lovasz(MutableMatrix *lambda, int k, ring_elem alphaTop, ring_elem alphaBottom)
Definition LLL.cpp:72
static void REDI(int k, int ell, MutableMatrix *A, MutableMatrix *Achange, MutableMatrix *lambda)
Definition LLL.cpp:108
virtual bool dot_product(size_t i, size_t j, ring_elem &result) const =0
virtual size_t n_cols() const =0
virtual bool get_entry(size_t r, size_t c, ring_elem &result) const =0
virtual bool set_entry(size_t r, size_t c, const ring_elem a)=0
void subtract_to(ring_elem &f, const ring_elem &g) const
Definition ring.cpp:206
void mult_to(ring_elem &f, const ring_elem g) const
Definition ring.cpp:204
virtual bool is_zero(const ring_elem f) const
Definition ZZ.cpp:155
virtual ring_elem from_long(long n) const
Definition ZZ.cpp:110
virtual void remove(ring_elem &f) const
Definition ZZ.cpp:194
virtual ring_elem divide(const ring_elem f, const ring_elem g) const
Definition ZZ.cpp:304
virtual ring_elem mult(const ring_elem f, const ring_elem g) const
Definition ZZ.cpp:267
virtual std::pair< bool, long > coerceToLongInteger(ring_elem a) const
Definition ZZ.cpp:64
char * str()
Definition buffer.hpp:72
void reset()
Definition buffer.hpp:69
@ COMP_DONE_STEPS
Definition computation.h:69
@ COMP_DONE
Definition computation.h:60
@ COMP_ERROR
Definition computation.h:56
@ COMP_INTERRUPTED
Definition computation.h:57
RingZZ * globalZZ
Definition relem.cpp:13
bool system_interrupted()
const int ERROR
Definition m2-mem.cpp:55
char newline[]
Definition m2-types.cpp:49
int M2_gbTrace
Definition m2-types.cpp:52
void emit(const char *s)
Definition text-io.cpp:41

References COMP_DONE, COMP_DONE_STEPS, COMP_ERROR, COMP_INTERRUPTED, MutableMatrix::dot_product(), emit(), ERROR, MutableMatrix::get_entry(), globalZZ, Lovasz(), M2_gbTrace, MutableMatrix::n_cols(), newline, REDI(), buffer::reset(), MutableMatrix::set_entry(), buffer::str(), SWAPI(), and system_interrupted().

Referenced by LLL().