{{ message }}

Instantly share code, notes, and snippets.

# jasongrout/findsets.py

Last active Dec 15, 2015
This file contains 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
 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 commented Mar 30, 2013

 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.