Skip to content

Instantly share code, notes, and snippets.

@jasongrout
Last active December 15, 2015 14:39
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 jasongrout/5276508 to your computer and use it in GitHub Desktop.
Save jasongrout/5276508 to your computer and use it in GitHub Desktop.
def find_sets(F,M,d, xRy):
"""
Find M sets of d mutually orthogonal vectors from F (an iterable of vectors) so that each set is related pairwise by xRy
"""
def xRY(v,W):
"""
xRY(v,W) = is v related to every vector in W by the conditions you need
We could just inline this function below, but the code below is a little more readable if we abstract it out
"""
return all(xRy(v,w) for w in W)
bases = []
used = set() # we could also just keep track of the elements left if F is small enough
for m in range(M):
base = []
for v in F:
if tuple(v) in used:
continue
if all(v*w==0 for w in base) and all(xRY(v,S) for S in bases):
base.append(v)
used.add(tuple(v))
if len(base)==d:
break
else:
# we went all the way through N, but didn't find enough vectors
raise ValueError("couldn't find enough mutually orthogonal vectors")
bases.append(base)
return bases
@jasongrout
Copy link
Author

I see that my code may not find a list of sets because once a mutually orthogonal set is found, it is fixed. Instead, the sets themselves should be re-examined too. So, for example, if that ValueError is raised, you'd have to backtrack and try a different list of sets.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment