class Systematic_Random_Code_Matrix_Solver:
def __init__(self):
self.__total_xor_row_operation = 0
def get_total_xor_row_operation(self):
return self.__total_xor_row_operation
def search_pivot_row(self, target_col, target_mat):
for row in range(target_mat.nrows()):
if target_mat[row, target_col] == 1:
return row
return -1
def form_triangle_matrix(self, idn_mat, rand_mat):
result_mat = matrix(GF(2), 0, idn_mat.ncols())
for pivot_col in range(idn_mat.ncols()):
pivot_row = self.search_pivot_row(pivot_col, idn_mat)
if pivot_row >=0 :
result_mat = result_mat.stack( idn_mat[pivot_row] )
else:
pivot_row = self.search_pivot_row(pivot_col, rand_mat)
assert (pivot_row >=0 )
result_mat = result_mat.stack( rand_mat[pivot_row] )
rand_mat = rand_mat.delete_rows([pivot_row])
for row in range(rand_mat.nrows()):
rand_mat[row] += result_mat[-1]
self.__total_xor_row_operation += 1
return result_mat
def calc_xor_row_operation(total_idn_row, k):
idn_mat = identity_matrix(GF(2), k)
idn_mat = idn_mat[range(total_idn_row), :]
total_rand_row = k+10 - idn_mat.nrows()
rand_mat = random_matrix(GF(2), total_rand_row, k)
solver = Systematic_Random_Code_Matrix_Solver()
result_mat = solver.form_triangle_matrix(idn_mat,rand_mat)
return solver.get_total_xor_row_operation()
import random
k = 40
for i in range(0, 10):
result = calc_xor_row_operation(i, k)
print 'idn_row = %d, rand_row = %d and XOR row operation = %d' % (i, k+10-i, result)
idn_row = 0, rand_row = 50 and XOR row operation = 1180
idn_row = 1, rand_row = 49 and XOR row operation = 1180
idn_row = 2, rand_row = 48 and XOR row operation = 1179
idn_row = 3, rand_row = 47 and XOR row operation = 1177
idn_row = 4, rand_row = 46 and XOR row operation = 1174
idn_row = 5, rand_row = 45 and XOR row operation = 1170
idn_row = 6, rand_row = 44 and XOR row operation = 1165
idn_row = 7, rand_row = 43 and XOR row operation = 1159
idn_row = 8, rand_row = 42 and XOR row operation = 1152
idn_row = 9, rand_row = 41 and XOR row operation = 1144