Skip to content

Commit

Permalink
gh-36173: Fix quadratic memory of berlekamp_massey
Browse files Browse the repository at this point in the history
    
Fix #36172.

Reduced berlekamp_massey memory consumption by replacing a dictionary
with temporary variables. The memory consumption of the line highlighted
below reduced from 42MB to 4MB (probably inaccurate, but significant
enough).

```python
from memory_profiler import profile
from sage.matrix.berlekamp_massey import berlekamp_massey
@Profile
def gen_data():
    p = random_prime(2**64)
    ls = [GF(p).random_element() for _ in range(20000)]
    berlekamp_massey(ls); # <--- this line
gen_data()
```

I am not sure if I have to include extra doctests or not, or how to test
memory consumptions, since the time complexity is also O(n^2).

### 📝 Checklist

- [x] The title is concise, informative, and self-explanatory.
- [x] The description explains in detail what this PR is about.
- [x] I have linked a relevant issue or discussion.
- [x] I have created tests covering the changes.
- [x] I have updated the documentation accordingly.
    
URL: #36173
Reported by: grhkm21
Reviewer(s): grhkm21, Kwankyu Lee
  • Loading branch information
Release Manager committed Sep 10, 2023
2 parents 1ebaec7 + 0293a38 commit f63a4d5
Showing 1 changed file with 8 additions and 12 deletions.
20 changes: 8 additions & 12 deletions src/sage/matrix/berlekamp_massey.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,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, (q, f1) = f1, f0.quo_rem(f1)
s0, s1 = s1, s0 - q * s1
return s1.reverse().monic()

0 comments on commit f63a4d5

Please sign in to comment.