# simpleindex.py -- the circulant complex Hadamard matrices of prime order p = 1(4) coming from the cyclic p-roots of # simple index 2 of Bjorck, with first row h(0)=1, h(x)=a for x a nonzero square, h(x)=conj(a) otherwise, where # a = c+i sqrt(1-c^2) and c = 1/(1-s sqrt(p)), s = 1,-1. We check in exact arithmetic, in the field Q(sqrt p)(a), whose # elements are written as u+va with u,v in Q(sqrt p) and a^2 = 2ca-1, that the rows are orthogonal, for the primes # 13 <= p <= 127. Then, for each odd prime q dividing p-4, we check that 1+c belongs to the prime P of Q(sqrt p) # above q containing sqrt(p)-2s, so that (a+1)^2 = 2(1+c)a gives a = -1 modulo P, and the matrix reduces modulo P to # 2I-J, with p = 4 modulo q. Finally, we compare with the double precision values of ali.py, when its output is present. from fractions import Fraction as Fr import os, json, math class R: # x + y sqrt(p) p=None def __init__(s,x,y=0): s.x=Fr(x); s.y=Fr(y) def __add__(s,o): o=o if isinstance(o,R) else R(o); return R(s.x+o.x,s.y+o.y) def __sub__(s,o): o=o if isinstance(o,R) else R(o); return R(s.x-o.x,s.y-o.y) def __mul__(s,o): o=o if isinstance(o,R) else R(o); return R(s.x*o.x+R.p*s.y*o.y,s.x*o.y+s.y*o.x) def inv(s): n=s.x*s.x-R.p*s.y*s.y; return R(s.x/n,-s.y/n) def iszero(s): return s.x==0 and s.y==0 class K: # u + v a, a^2 = t a - 1, t = 2c t=None def __init__(s,u,v=None): s.u=u if isinstance(u,R) else R(u); s.v=v if v is not None else R(0) def __add__(s,o): return K(s.u+o.u,s.v+o.v) def __mul__(s,o): # (u+va)(u'+v'a) = uu' + (uv'+vu')a + vv'(ta-1) return K(s.u*o.u-s.v*o.v,s.u*o.v+s.v*o.u+s.v*o.v*K.t) def conj(s): # conj(a) = t - a return K(s.u+s.v*K.t,R(0)-s.v) def iszero(s): return s.u.iszero() and s.v.iszero() def primes(a,b): return [n for n in range(a,b) if n>1 and all(n%m for m in range(2,int(n**.5)+1))] ali={} if os.path.exists('ali_output/ali.json'): for t,v in json.load(open('ali_output/ali.json')).items(): if t.endswith(',2'): ali[v['p']]=sorted(r['row'][1][0] if v['p']>2 else 0 for r in v['classes']) for p in [q for q in primes(13,128) if q%4==1]: R.p=p; QR={(x*x)%p for x in range(1,p)}; lines=[] for s in (1,-1): c=(R(1)-R(0,s)).inv(); K.t=c+c a=K(R(0),R(1)); h=[K(R(1))]+[a if x in QR else a.conj() for x in range(1,p)] orth=all(sum_is_zero for sum_is_zero in [ (lambda tot: tot.iszero())(__import__('functools').reduce(lambda u,w:u+w,[h[(x+d)%p]*h[x].conj() for x in range(p)])) for d in range(1,p)]) qs=[q for q in primes(3,p) if (p-4)%q==0] red=[] for q in qs: # 1+c = (2-s sqrt p)/(1-s sqrt p): the numerator times its conjugate is 4-p, and the prime P above q containing # sqrt(p)-2s contains 2-s sqrt(p) = -s(sqrt(p)-2s), while 1-s sqrt(p) = 1-2 = -1 modulo P is a unit num=R(2,-s); den=R(1,-s) ok=(num*R(2,s)).x==4-p and (num*R(2,s)).y==0 and (4-p)%q==0 red.append("q=%d: %s"%(q,"a=-1 mod P, reduction to 2I-J, p=%d=4 mod %d"%(p,q) if ok else "no")) cval=1/(1-s*math.sqrt(p)) match=("" if p not in ali else ", matches ali.py: %s (classes of index 2 found by ali.py: %d)"%(any(abs(cval-v)<1e-9 for v in ali[p]),len(ali[p]))) lines.append(" s=%+d: c=%.9f, rows orthogonal: %s, %s%s"%(s,cval,orth,"; ".join(red),match)) print("p=%d"%p); print("\n".join(lines),flush=True)