Skip to content

Instantly share code, notes, and snippets.

@caffeine-potent
Created August 8, 2018 14:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save caffeine-potent/97697a9391df2815c6c6f20906672a27 to your computer and use it in GitHub Desktop.
Save caffeine-potent/97697a9391df2815c6c6f20906672a27 to your computer and use it in GitHub Desktop.
import numpy as np
def create_n_depth_matrix(n: int) -> np.array:
"""Function used to generate all binary permutations of length n.
Binary permutations of n=3 for example would include:
[0,0,0],[0,0,1],[0,1,0],[0,1,1],[1,0,0],[1,0,1],[1,1,0],[1,1,1]
Binary permuataions are arranged in a 3d matrix of shape (n, a, b)
such that a*b == (2^n)
Args:
n (int): depth of a column
Returns:
np.array: a (2^n) X (n) matrix.
"""
assert n > 2
d = n
layers = []
last_layer = np.tile(np.array([0,1]),2**(d-1))
for x in range(d-1):
layers.append(last_layer.copy())
last_layer = np.repeat(last_layer, 2)[:2**d]
layers.append(last_layer)
return np.stack(layers).T.reshape(2**(d//2), 2**(d- (d//2)),d)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment