Created
August 24, 2018 19:05
-
-
Save jaklinger/1a3ba7d3b1801581f5ce3a072ff81027 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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