Advertisement
Guest User

Untitled

a guest
Aug 5th, 2015
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.59 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. import numpy as np
  3. from functools import lru_cache
  4.  
  5. @lru_cache(100)
  6. def radix_matrix(shape):
  7.     return (2 ** np.arange(shape[0] * shape[1])).reshape(shape)
  8.  
  9. def binmtx2key(arr):
  10.     weights = radix_matrix(arr.shape)
  11.     return ((weights * arr).sum(),) + arr.shape
  12.  
  13. def key2binmtx(key):
  14.     shape = key[1:]
  15.     width = shape[0] * shape[1]
  16.     binstr = bin(key[0])[2:].rjust(width, '0')
  17.     return (np.array(binstr, dtype='c') == b'1').reshape(shape) + 0
  18.  
  19. k = np.array([[1, 1], [1, 0]])
  20. kencoded = binmtx2key(k)
  21. print(kencoded)
  22. print(key2binmtx(kencoded))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement