Skip to content

Instantly share code, notes, and snippets.

@jaklinger
Created August 24, 2018 19:05
Show Gist options
  • Save jaklinger/1a3ba7d3b1801581f5ce3a072ff81027 to your computer and use it in GitHub Desktop.
Save jaklinger/1a3ba7d3b1801581f5ce3a072ff81027 to your computer and use it in GitHub Desktop.
from itertools import chain, combinations
def all_subsets(n):
return chain(*map(lambda x: combinations(range(0,n), x), range(2, n+1)))
def subset_matrix(n):
rows = []
for subset in all_subsets(n):
new_row = [0]*n
for i in subset:
new_row[i] = 1
rows.append(new_row)
return np.array(rows)
class SubsetMatrices(dict):
def __getitem__(self, n):
if n not in self:
self[n] = subset_matrix(n)
return super().__getitem__(n)
sm = SubsetMatrices()
print(sm[4])
print(sm[10])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment