decode-simple.sage 748 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import itertools
  2. from tabulate import tabulate
  3. q = 5
  4. n = 4
  5. k = 2
  6. d = n - k + 1
  7. K = GF(q)
  8. R.<x> = K[]
  9. α = K(2)
  10. assert set(α^i for i in range(q - 1)) | {0} == set(K)
  11. # Pick a random codeword
  12. m = (3, 2)
  13. f = m[0] + m[1]*x
  14. c = [f(α^i) for i in range(n)]
  15. assert len(c) == n
  16. # We can tolerate <= (n-k)/2 = 1 error
  17. c[1] = 0
  18. # f is degree 3 so we need 4 points to reconstruct it
  19. table = []
  20. count = 0
  21. total = 0
  22. for i0 in range(n):
  23. for i1 in range(i0+1, n):
  24. g = R.lagrange_polynomial([(α^i0, c[i0]), (α^i1, c[i1])])
  25. table.append([
  26. (i0, i1),
  27. g,
  28. "*" if f == g else None
  29. ])
  30. if f == g:
  31. count += 1
  32. total += 1
  33. print(tabulate(table))
  34. print(f"{count} / {total}")