1

I am coding a function to create generator matrix using Reed-Solomon encoding in Python. it is currently using for loops but i was wondering if there is a more efficient way to this. My code is:

def ReedSolomon(k,p):
    M = np.zeros((k,p))
    for i in range(k):
        for j in range(p):
            M[i][j] = j**i 
    return M

The encoding is: enter image description here

I believe my function works but may not scale well to large p and k

Pranav Hosangadi
  • 23,755
  • 7
  • 44
  • 70
AE93
  • 155
  • 8
  • It is a [Vandermonde matrix](https://en.wikipedia.org/wiki/Vandermonde_matrix), right? (See, e.g. ["Simple encoding procedure..."](https://en.wikipedia.org/wiki/Reed%E2%80%93Solomon_error_correction#Simple_encoding_procedure:_The_message_as_a_sequence_of_coefficients) in the wikipedia article on [Reed-Solomon error correction](https://en.wikipedia.org/wiki/Reed%E2%80%93Solomon_error_correction).) NumPy has the function [`numpy.vander`](https://numpy.org/doc/stable/reference/generated/numpy.vander.html). – Warren Weckesser Jan 27 '23 at 23:52

2 Answers2

3

The generalized equation for an element at index r, c in your matrix is c**r.

For a matrix of shape k, p, you can create two aranges -- a row vector from 0 to p-1, and a column vector from 0 to k-1, and have numpy automatically broadcast the shapes:

def ReedSolomon(k,p):
    rr = np.arange(k).reshape((-1, 1))
    cc = np.arange(p)
    return cc**rr

Calling this function with e.g. k=5, p=3 gives:

>>> ReedSolomon(5, 3)
array([[ 1,  1,  1],
       [ 0,  1,  2],
       [ 0,  1,  4],
       [ 0,  1,  8],
       [ 0,  1, 16]])
Pranav Hosangadi
  • 23,755
  • 7
  • 44
  • 70
0

you can use numpy

p=5
k=7
A = np.arange(p).reshape((1,p))
A = np.repeat(A, k, axis=0)
B = np.arange(k).reshape((k,1))
B = np.repeat(B, p, axis=1)

M = A**B
print(M)
[[   1    1    1    1    1]
 [   0    1    2    3    4]
 [   0    1    4    9   16]
 [   0    1    8   27   64]
 [   0    1   16   81  256]
 [   0    1   32  243 1024]
 [   0    1   64  729 4096]]
Daraan
  • 1,797
  • 13
  • 24