Skip to content

Instantly share code, notes, and snippets.

@jannson
Last active February 23, 2021 10:09
Show Gist options
  • Save jannson/ac4585c7fa81a2d7d6a3d791f1725963 to your computer and use it in GitHub Desktop.
Save jannson/ac4585c7fa81a2d7d6a3d791f1725963 to your computer and use it in GitHub Desktop.
The simple Reed Solomon example: http://ju.outofmemory.cn/entry/87811
origin d:
[5 7 6 2 4]
b:
[[ 1. 0. 0. 0. 0.]
[ 0. 1. 0. 0. 0.]
[ 0. 0. 1. 0. 0.]
[ 0. 0. 0. 1. 0.]
[ 0. 0. 0. 0. 1.]
[ 1. 1. 1. 1. 1.]
[ 1. 2. 3. 4. 5.]
[ 1. 4. 9. 16. 25.]]
encode:
[ 5. 7. 6. 2. 4. 24. 65. 219.]
the lost result:(d1, d4, c2 lost)
[ 7 6 4 24 219]
b1:
[[ 0. 1. 0. 0. 0.]
[ 0. 0. 1. 0. 0.]
[ 0. 0. 0. 0. 1.]
[ 1. 1. 1. 1. 1.]
[ 1. 4. 9. 16. 25.]]
b1 inverse:
[[ -8.00000000e-01 -4.66666667e-01 6.00000000e-01 1.06666667e+00
-6.66666667e-02]
[ 1.00000000e+00 -3.70074342e-17 0.00000000e+00 -1.85037171e-17
1.85037171e-17]
[ 2.08166817e-17 1.00000000e+00 1.66533454e-16 5.20417043e-18
-5.20417043e-18]
[ -2.00000000e-01 -5.33333333e-01 -1.60000000e+00 -6.66666667e-02
6.66666667e-02]
[ 0.00000000e+00 0.00000000e+00 1.00000000e+00 0.00000000e+00
0.00000000e+00]]
decode:
[ 5. 7. 6. 2. 4.]
import numpy as np
#http://ju.outofmemory.cn/entry/87811
w = 3
elen = 5
emax = 2**w
vlen = emax - elen
d = np.array([5, 7, 6, 2, 4], dtype=np.int)
identify = np.identity(elen)
vander = np.vander([i+1 for i in range(elen)], vlen, increasing=True)
b = np.append(identify, vander.T, axis=0)
print "origin d:"
print d
#print identify
#print vander.T
#print vander.shape
print "b:"
print b
#encode
encode = np.dot(b, d.T)
print "encode:"
print encode
#make lost d1, d4
lost_rlt = []
for i in range(encode.shape[0]):
if i != 0 and i != 3 and i != 6:
lost_rlt.append(encode[i])
survivors = np.array(lost_rlt, dtype=np.int)
print "the lost result:(d1, d4, c2 lost)"
print survivors
b1 = np.copy(b)
b1 = np.delete(b1, (0, 3, 6), axis=0)
print "b1:"
print b1
b1_inverse = np.linalg.inv(b1)
print "b1 inverse:"
print b1_inverse
decode = np.dot(b1_inverse, survivors.T)
print "decode:"
print decode
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment