# designs.py -- the 2-valued complex Hadamard matrices coming from the difference sets with parameters # (v,k,lambda) = (4n-1,2n-1,n-1), in exact arithmetic. For a difference set D in an abelian group G of order v, the first # row is h = 1 on the complement of D, and h = z = (1-2n+i sqrt(v))/(2n) on D. We check that the matrix H_ij = h(j-i) is # Hadamard, and that sigma = S/v, at the prime P above any prime q with v_q(v) odd, belongs to the local ring, and is # -1 on the quadruples of distinct indices, -2 on those with a=d or b=c but not both, and -3 when a=d and b=c, modulo P, # which is the affine condition with X=Y=-1. The elements of Q(sqrt(-v)) are written as a+b sqrt(-v), with a,b rational, # the valuation at P being the q-adic valuation of the norm, and the residue of an element of the local ring being a # modulo q. The examples are the quadratic residues modulo p=7,11,19,23, the Singer difference sets in Z_15 and Z_31, # and the twin prime difference set in Z_5 x Z_7. from fractions import Fraction as Fr import itertools class Q: v=None def __init__(s,a,b=0): s.a=Fr(a); s.b=Fr(b) def __add__(s,o): return Q(s.a+o.a,s.b+o.b) def __mul__(s,o): return Q(s.a*o.a-Q.v*s.b*o.b,s.a*o.b+s.b*o.a) def conj(s): return Q(s.a,-s.b) def norm(s): return s.a*s.a+Q.v*s.b*s.b def val(x,q): if x==0: return 10**9 n,d,e=x.numerator,x.denominator,0 while n%q==0: n//=q; e+=1 while d%q==0: d//=q; e-=1 return e def check(name,G,D): # G: list of group elements as tuples, with addition componentwise modulo the moduli mods=G[0][1]; elems=[g for g,_ in G]; v=len(elems); n=(v+1)//4 add=lambda x,y: tuple((a+b)%m for a,b,m in zip(x,y,mods)); neg=lambda x: tuple((-a)%m for a,m in zip(x,mods)) Q.v=v; z=Q(Fr(1-2*n,2*n),Fr(1,2*n)); one=Q(1) h={g:(z if g in D else one) for g in elems}; hb={g:h[g].conj() for g in elems} sub=lambda x,y: add(x,neg(y)) had=all(sum((h[add(x,s)]*hb[x] for x in elems),Q(0)).norm()==0 for s in elems if s!=elems[0]) qs=[q for q in range(3,v+1) if v%q==0 and all(q%r for r in range(2,q)) and val(Fr(v),q)%2==1] out={} zero=elems[0] for b,c,d in itertools.product(elems,repeat=3): a=zero; S=Q(0) for x in elems: S=S+h[sub(x,a)]*hb[sub(x,b)]*hb[sub(x,c)]*h[sub(x,d)] sig=Q(S.a/v,S.b/v); idx=[a,b,c,d] if len(set(idx))==4: kind="distinct" elif a==d and b==c and a!=b: kind="a=d,b=c" elif ((a==d)!=(b==c)) and len(set(idx))==3: kind="a=d or b=c" else: continue for q in qs: ok=val(sig.norm(),q)>=0 r=(sig.a.numerator*pow(sig.a.denominator,-1,q))%q if ok else None out.setdefault((q,kind),set()).add(r) want={"distinct":-1,"a=d or b=c":-2,"a=d,b=c":-3} res=" ".join("q=%d: residues %s, affine with X=Y=-1: %s"%(q,"/".join(str(sorted(out[(q,k)],key=str)) for k in want), all(out[(q,k)]=={w%q} for k,w in want.items())) for q in qs) print("%-20s v=%2d Hadamard %s %s"%(name,v,had,res),flush=True) for p in (7,11,19,23): QR={(x*x)%p for x in range(1,p)} check("quadratic residues",[((x,),(p,)) for x in range(p)],{(x,) for x in QR}) for m,v in ((4,15),(5,31)): # Singer difference set: the exponents i with Tr(g^i)=0, g a primitive element of GF(2^m) poly={4:0b10011,5:0b100101}[m] def mul(a,b): r=0 while b: if b&1: r^=a b>>=1; a<<=1 if a>>m: a^=poly return r pw=[1] for i in range(1,v): pw.append(mul(pw[-1],2)) def tr(a): t,x=0,a for _ in range(m): t^=x; x=mul(x,x) return t&1 check("Singer",[((i,),(v,)) for i in range(v)],{(i,) for i in range(v) if tr(pw[i])==0}) p=5; r=7 chi=lambda x,q: 0 if x%q==0 else (1 if pow(x,(q-1)//2,q)==1 else -1) G=[((x,y),(p,r)) for x in range(p) for y in range(r)] D={(x,y) for x in range(p) for y in range(r) if (y==0) or (x!=0 and chi(x,p)*chi(y,r)==1)} check("twin primes",G,D)