Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix quadratic memory of berlekamp_massey #36173

Merged
merged 4 commits into from
Sep 10, 2023
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 14 additions & 12 deletions src/sage/matrix/berlekamp_massey.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ def berlekamp_massey(a):
Traceback (most recent call last):
...
ValueError: argument must have an even number of terms

Check that :issue:`36172` is fixed::

sage: p = next_prime(2**64)
sage: ls = [GF(p).random_element() for _ in range(2000)]
sage: _ = berlekamp_massey(ls)
"""
grhkm21 marked this conversation as resolved.
Show resolved Hide resolved
if not isinstance(a, (list, tuple)):
raise TypeError("argument must be a list or tuple")
Expand All @@ -84,15 +90,11 @@ def berlekamp_massey(a):
K = a[0].parent().fraction_field()
except AttributeError:
K = sage.rings.rational_field.RationalField()
R = K['x']
x = R.gen()

f = {-1: R(a), 0: x**(2 * M)}
s = {-1: 1, 0: 0}
j = 0
while f[j].degree() >= M:
j += 1
qj, f[j] = f[j - 2].quo_rem(f[j - 1])
s[j] = s[j - 2] - qj * s[j - 1]
t = s[j].reverse()
return ~(t[t.degree()]) * t # make monic (~ is inverse in python)

R, x = K['x'].objgen()
f0, f1 = R(a), x**(2 * M)
s0, s1 = 1, 0
while f1.degree() >= M:
f0, (qj, f1) = f1, f0.quo_rem(f1)
s0, s1 = s1, s0 - qj * s1
return s1.reverse().monic()
grhkm21 marked this conversation as resolved.
Show resolved Hide resolved