07734willy

Linalg Encoding

Feb 10th, 2023
1,092
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.07 KB | None | 0 0
  1. from sympy import Matrix
  2. import numpy as np
  3.  
  4. # produce the original, "used" tape
  5. tape = np.random.randint(2, size=(8, ))
  6. zeros, ones = np.bincount(tape)
  7.  
  8. # produce new data to encode, for convenience make it exactly the same
  9. # length as the number of free bits in our tape
  10. data = np.random.randint(2, size=(zeros, ))
  11.  
  12. # produce the random matrix A
  13. matrix = np.random.randint(2, size=(zeros, 8))
  14.  
  15. # select columns in A corresponding to zero/free bits in the tape
  16. matrix_prime = matrix[:, tape == 0]
  17.  
  18. # create augmented matrix from A' and X'
  19. aug_matrix = Matrix(np.hstack((matrix_prime, data.reshape((zeros, 1)))))
  20.  
  21. # put it in reduced row echelon form
  22. rref, cols = aug_matrix.rref()
  23.  
  24. # solve for X', reduce mod 2
  25. x_prime = (1 - np.array(rref)[:, -1]) % 2
  26.  
  27. # ensure nonzero
  28. assert np.any(x_prime)
  29.  
  30. # insert X' into the tape at zero/free bits, producing X
  31. x = tape.copy()
  32. x[tape == 0] = x_prime
  33.  
  34. # multiply the matrix with 1-X, reduce mod 2
  35. result = matrix.dot(1 - x) % 2
  36.  
  37. # ensure X is not all 1s
  38. assert not np.all(x)
  39.  
  40. print(tape)
  41. print(data)
  42. print(result)
Advertisement
Add Comment
Please, Sign In to add comment