Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- import numpy as np
- from functools import lru_cache
- @lru_cache(100)
- def radix_matrix(shape):
- return (2 ** np.arange(shape[0] * shape[1])).reshape(shape)
- def binmtx2key(arr):
- weights = radix_matrix(arr.shape)
- return ((weights * arr).sum(),) + arr.shape
- def key2binmtx(key):
- shape = key[1:]
- width = shape[0] * shape[1]
- binstr = bin(key[0])[2:].rjust(width, '0')
- return (np.array(binstr, dtype='c') == b'1').reshape(shape) + 0
- k = np.array([[1, 1], [1, 0]])
- kencoded = binmtx2key(k)
- print(kencoded)
- print(key2binmtx(kencoded))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement