# opencases.py -- the open cases of the table in Section 4, versus the affine profile criterion of Section 5. # Part 1: H_9, BN_10 (entries in Q(sqrt(-15))) and N_11, N_11' (entries in Q(sqrt(-7))), with the formulas from the # paper "Higher invariants of complex Hadamard matrices", reduce modulo the ramified prime above p = 5, 3, 7, 7 to a # matrix equivalent to J_N - 2I_N, with N = 4(p). R_7^q at q=1 lies in BH(7,6) and reduces modulo the prime above 3 # to a matrix equivalent to J_7 - 2I_7, and BN_6^q at q=i has entries +-1, +-i. # Part 2: zeros of S(a,b,c,d) on the quadruples of distinct indices, computed exactly: BF_6 (in Q(a,i), with # a^4 = 2a^3+2a-1), L_{8,30}, the families F_{8,2}, F_{8,4}, K_{8,2}, M_{8,1} (as formal Laurent polynomials in the # parameters), and the real matrices of order 0(8): S in 8Z, the sum of the squares S(a,b,c,d)^2 over d is N^2, and # zeros for the Paley matrices of orders 24, 32, 48, 104. I_{8,12} and K_{8,30} have no zeros. # Part 3: the gauge-invariant condition C1, |S|^2 = N^2 modulo P on the quadruples of distinct indices, for I_{8,12} # and K_{8,30}: the primes P where it holds, via norms and gcds of cyclotomic integers. # Part 4: residue sweeps over the finite fields F_q: the affine test at the primes P not dividing N, with the row gauge # solved from the quadruples of distinct indices, for all the residues of the parameters, and the relations # X+Y = N-2, XY = 1 on the affine profiles found. # Usage: python3 opencases.py import numpy as np, itertools, math, random, time from fractions import Fraction as Fr T0=time.time() def say(*a): print(*a,flush=True) # ---------------- exact cyclotomic tools ---------------- def polydiv_int(a,b): a=list(a); q=[0]*(max(len(a)-len(b)+1,1)) for i in range(len(a)-len(b),-1,-1): c=a[i+len(b)-1]; q[i]=c for j in range(len(b)): a[i+j]-=c*b[j] return q,a[:len(b)-1] _phi={} def phi(q): if q in _phi: return _phi[q] p=[-1]+[0]*(q-1)+[1] for d in range(1,q): if q%d==0: p,r=polydiv_int(p,phi(d)) while p and p[-1]==0: p.pop() _phi[q]=tuple(p); return _phi[q] def reduce_mod(a,m): a=list(a) for i in range(len(a)-1,len(m)-2,-1): c=a[i] if c: for j in range(len(m)): a[i-len(m)+1+j]-=c*m[j] return a[:len(m)-1]+[0]*max(0,len(m)-1-len(a)) def bareiss_det(M): M=[list(r) for r in M]; n=len(M); sign=1; prev=1 for k in range(n-1): if M[k][k]==0: for i in range(k+1,n): if M[i][k]!=0: M[k],M[i]=M[i],M[k]; sign=-sign; break else: return 0 for i in range(k+1,n): for j in range(k+1,n): M[i][j]=(M[i][j]*M[k][k]-M[i][k]*M[k][j])//prev prev=M[k][k] return sign*M[n-1][n-1] def cnorm(a,q): m=phi(q); d=len(m)-1; cur=reduce_mod(a,m); cols=[] for j in range(d): cols.append(cur); cur=reduce_mod([0]+cur,m) return bareiss_det([[cols[j][i] for j in range(d)] for i in range(d)]) def pmod_poly(a,p): a=[x%p for x in a] while a and a[-1]==0: a.pop() return a def pgcd(a,b,p): a=pmod_poly(a,p); b=pmod_poly(b,p) while b: inv=pow(b[-1],-1,p) while len(a)>=len(b) and a: c=a[-1]*inv%p; sh=len(a)-len(b) for i in range(len(b)): a[sh+i]=(a[sh+i]-c*b[i])%p while a and a[-1]==0: a.pop() a,b=b,a return a def primes_of(n): n=abs(n); out=[]; d=2 while d*d<=n: while n%d==0: out.append(d); n//=d d+=1 if n>1: out.append(n) return sorted(set(out)) def svec(E,q,a,b,c,d): return np.bincount((E[a]-E[b]-E[c]+E[d])%q,minlength=q) def iszero_cyc(v,q): return not any(reduce_mod([int(x) for x in v],phi(q))) def butson_hadamard(E,q): N=len(E); return all(iszero_cyc(np.bincount((E[i]-E[j])%q,minlength=q),q) for i in range(N) for j in range(N) if i!=j) # ---------------- the matrices ---------------- I812=np.array([[0,0,0,0,0,0,0,0],[0,0,0,3,6,6,6,9],[0,1,5,9,1,5,9,6],[0,4,8,6,10,2,3,9],[0,6,2,11,9,8,3,5],[0,6,11,5,3,11,9,5],[0,7,6,1,7,3,9,1],[0,10,6,7,4,9,3,1]]) K830=np.array([[0,0,0,0,0,0,0,0],[0,0,6,10,12,18,20,24],[0,6,13,28,27,21,12,15],[0,10,28,20,16,4,10,22],[0,12,27,16,1,15,24,9],[0,18,21,4,15,19,6,3],[0,20,12,10,24,6,0,18],[0,24,15,22,9,3,18,7]]) L830=np.array([[0,0,0,0,0,0,0,0],[0,0,6,10,12,18,20,24],[0,6,21,2,19,17,4,15],[0,10,17,25,8,2,15,23],[0,12,4,23,27,15,19,8],[0,18,2,17,15,3,1,16],[0,20,19,15,4,16,5,1],[0,24,15,8,23,1,16,9]]) def paley1(p): chi=lambda x: 0 if x%p==0 else (1 if pow(x%p,(p-1)//2,p)==1 else -1) Q=np.array([[chi(j-i) for j in range(p)] for i in range(p)],dtype=np.int64) S=np.zeros((p+1,p+1),dtype=np.int64); S[0,1:]=1; S[1:,0]=-1; S[1:,1:]=Q H=np.eye(p+1,dtype=np.int64)+S; assert (H@H.T==(p+1)*np.eye(p+1)).all(); return H class Q: # exact arithmetic in Q(sqrt(-d)) d=15 __slots__=('r','s') def __init__(s,r,t=0): s.r=Fr(r); s.s=Fr(t) def __add__(a,b): b=b if isinstance(b,Q) else Q(b); return Q(a.r+b.r,a.s+b.s) __radd__=__add__ def __neg__(a): return Q(-a.r,-a.s) def __mul__(a,b): b=b if isinstance(b,Q) else Q(b); return Q(a.r*b.r-Q.d*a.s*b.s,a.r*b.s+a.s*b.r) __rmul__=__mul__ def conj(a): return Q(a.r,-a.s) def nrm(a): return a.r*a.r+Q.d*a.s*a.s def inv(a): n=a.nrm(); return Q(a.r/n,-a.s/n) def __truediv__(a,b): b=b if isinstance(b,Q) else Q(b); return a*b.inv() def __pow__(a,k): if k<0: return a.inv()**(-k) r=Q(1) for _ in range(k): r=r*a return r def __eq__(a,b): b=b if isinstance(b,Q) else Q(b); return a.r==b.r and a.s==b.s def H9(): Q.d=15; y=Q(Fr(1,4),Fr(-1,4)); m=lambda k: y**k; one=Q(1) return [[one]*9,[one,-one,-one,m(1),m(-1),-m(-3),m(-3),m(1),m(-1)],[one,m(1),m(3),-m(2),m(3),m(1),-one,-m(1),m(1)], [one,m(1),m(1),-one,m(-1),m(-3),-m(-3),-one,m(-1)],[one,-m(1),-m(2),m(3),m(3),-one,m(1),m(1),m(1)], [one,m(1),m(3),m(3),m(1),m(-1),m(-1),m(1),m(2)],[one,m(3),m(1),m(1),m(2),m(-1),m(-1),m(3),m(1)], [one,-m(2),-m(1),m(1),m(1),-one,m(1),m(3),m(3)],[one,m(3),m(1),-m(1),m(1),m(1),-one,-m(2),m(3)]] def BN10(): Q.d=15; a=Q(Fr(-1,4),Fr(1,4)) pat=[[0]*10,[0,0,-2,-1,-2,2,0,2,0,1],[0,2,0,-2,-2,0,1,2,0,-1],[0,0,0,0,-2,2,2,1,-2,-1],[0,2,-2,0,0,1,0,2,-2,-1], [0,0,-1,-2,0,0,2,2,-2,1],[0,2,0,-2,-1,2,0,0,-2,1],[0,2,-2,0,-2,0,2,0,-1,1],[0,1,-2,-2,0,2,2,0,0,-1],[0,1,-1,-1,-1,1,1,1,-1,0]] return [[a**k for k in r] for r in pat] def N11(variant): Q.d=7; x=Q(Fr(3,4),Fr(-1,4)); one=Q(1); m1=-one a,b,y=(-x,x,-(x*x)) if variant==0 else (one,m1,x.conj()) X=lambda t: -(x/t) return [[one]*11,[one,y,y,a,b,b,m1,x,m1,x,m1],[one,y,b,b,a,y,x,x,m1,m1,m1],[one,a,b,y,y,b,m1,m1,x,m1,x],[one,b,a,y,b,y,x,m1,m1,m1,x], [one,b,y,b,y,a,m1,m1,x,x,m1],[one,m1,x,m1,x,m1,X(a),X(b),X(y),X(y),X(b)],[one,x,x,m1,m1,m1,X(b),X(y),X(b),X(y),X(a)], [one,m1,m1,x,m1,x,X(y),X(b),X(b),X(a),X(y)],[one,x,m1,m1,m1,x,X(y),X(y),X(a),X(b),X(b)],[one,m1,m1,x,x,m1,X(b),X(a),X(y),X(b),X(y)]] def exact_hadamard_Q(H): N=len(H) for i in range(N): for j in range(N): s=Q(0) for y in range(N): s=s+H[i][y]*H[j][y].conj() if not s==Q(N if i==j else 0): return False return all(e.nrm()==1 for r in H for e in r) def is_J2I(M,p): # M: matrix of residues 1, p-1; is it equivalent to J_N - 2I_N, by permutations and signs of rows and columns? N=len(M); M=[[1 if x==1 else -1 for x in row] for row in M] for j0 in range(N): c=[M[0][j]*(-1 if j==j0 else 1) for j in range(N)]; pos=[j0]; ok=True for i in range(1,N): v=[c[j]*M[i][j] for j in range(N)]; neg=[j for j in range(N) if v[j]==-1] if len(neg)==1: pos.append(neg[0]) elif len(neg)==N-1: pos.append([j for j in range(N) if v[j]==1][0]) else: ok=False; break if ok and len(set(pos))==N: return True return False def red(e,p): assert e.s.denominator%p and e.r.denominator%p return (e.r.numerator*pow(e.r.denominator,-1,p))%p # sqrt(-d) = 0 modulo the ramified prime w3=np.exp(2j*np.pi/3) def R7(q,w=w3): return np.array([[1,1,1,1,1,1,1],[1,1,1,w,-1,-1,w**2],[1,-w**2,w**2,w**2,q,-q,w],[1,w,-1,1,q*w**2,-q*w**2,w**2],[1,-1,-w,-1,-q,q,-w**2],[1,w**2,w,q*w,q,-q*w,-q],[1,w**2,w,-q*w,q*w,-q,q]]) def BN6(q): s=np.sqrt(2)*np.sqrt(1+2*q+2*q**3+q**4) x=(1+2*q+q**2-s)/(1+2*q-q**2); t=(1+2*q+q**2-s)/(-1+2*q+q**2); y=q; z=(1+2*q-q**2)/(q*(-1+2*q+q**2)); c=np.conj return np.array([[1,1,1,1,1,1],[1,-1,c(x),-y,-c(x),y],[1,x,-1,t,-t,-x],[1,-c(y),c(t),-1,c(y),-c(t)],[1,-x,-c(t),y,1,c(z)],[1,c(y),-c(x),-t,z,1]]) # ---------------- Part 1 ---------------- say("Part 1: matrices of the table reducing to J_N - 2I_N, and the specializations of the 2 families") for name,f,p in [("H_9",H9,5),("BN_10",BN10,3),("N_11",lambda: N11(0),7),("N_11'",lambda: N11(1),7)]: H=f(); N=len(H); M=[[red(e,p) for e in r] for r in H]; vals=sorted(set(x for r in M for x in r)) say(" %-6s exact Hadamard: %s, N=%d=4(%d): %s, entries modulo the prime above %d: %s, equivalent to J-2I: %s"%(name,exact_hadamard_Q(H),N,p,(N-4)%p==0,p,vals,is_J2I(M,p))) E=np.round(np.angle(R7(1.0))*6/(2*np.pi)).astype(int)%6 say(" R_7^1 in BH(7,6):",bool(np.allclose(np.exp(2j*np.pi*E/6),R7(1.0)) and butson_hadamard(E,6)), ", modulo the prime above 3 (w=1) equivalent to J_7-2I_7:",is_J2I([[1 if e%2==0 else 2 for e in r] for r in E],3)) Hi=BN6(1j); G=np.round(Hi).astype(complex); Gi=np.round(G.real).astype(int)+1j*np.round(G.imag).astype(int) say(" BN_6^i: entries %s, exact Hadamard: %s, distance to the formula %.1e"%(sorted(set(Gi.ravel().tolist()),key=lambda z:(z.real,z.imag)), bool((np.round((Gi@Gi.conj().T).real).astype(int)==6*np.eye(6)).all() and (np.round((Gi@Gi.conj().T).imag)==0).all()),abs(Hi-Gi).max())) ts=np.linspace(0,2*np.pi,7201)[:-1]; good=[] for t in ts: with np.errstate(all='ignore'): Hq=BN6(np.exp(1j*t)); good.append(bool(np.isfinite(Hq).all() and abs(abs(Hq)-1).max()<1e-9 and abs(Hq@Hq.conj().T-6*np.eye(6)).max()<1e-9)) good=np.array(good) say(" BN_6^q is Hadamard exactly on the grid points with Re q <= (sqrt3-1)/2: %s, and arg i=%.4f is inside, with Re i=0 < %.4f"%( bool((good==(np.cos(ts)<=(np.sqrt(3)-1)/2+1e-12)).all() or np.sum(good!=(np.cos(ts)<=(np.sqrt(3)-1)/2))<=2),np.pi/2,(np.sqrt(3)-1)/2)) # ---------------- Part 2 ---------------- say("Part 2: zeros of S on the quadruples of distinct indices") def zeros_butson(E,q): N=len(E); return sum(iszero_cyc(svec(E,q,*t),q) for t in itertools.permutations(range(N),4)) for name,q,E in [("L_{8,30}",30,L830),("K_{8,30}",30,K830),("I_{8,12}",12,I812)]: say(" %-9s exact Hadamard: %s, zeros of S for H and H^t: %d, %d of 1680"%(name,butson_hadamard(E,q),zeros_butson(E,q),zeros_butson(E.T.copy(),q))) # BF_6 in Q(a,i), a^4=2a^3+2a-1, elements as vectors over the basis i^e a^f, e<2, f<4 def bmul(x,y): r=[Fr(0)]*8 for e1 in range(2): for f1 in range(4): c1=x[4*e1+f1] if not c1: continue for e2 in range(2): for f2 in range(4): c2=y[4*e2+f2] if not c2: continue e=e1+e2; s=1 if e==2: e=0; s=-1 poly=[Fr(0)]*7; poly[f1+f2]=c1*c2*s for k in range(6,3,-1): if poly[k]: c=poly[k]; poly[k]=0; poly[k-1]+=2*c; poly[k-3]+=2*c; poly[k-4]-=c for k in range(4): r[4*e+k]+=poly[k] return r def bel(e,f,c=1): v=[Fr(0)]*8; v[4*e+f]=Fr(c); return v def badd(x,y): return [u+v for u,v in zip(x,y)] def bneg(x): return [-u for u in x] ONE=bel(0,0); I=bel(1,0); A=bel(0,1); ABAR=badd(badd(bel(0,3,-1),bel(0,2,2)),bel(0,0,2)) assert bmul(A,ABAR)==ONE row=[ONE,bmul(I,A),bneg(A),bneg(I),bneg(ABAR),bmul(I,ABAR)] rowc=[ONE,bmul(bneg(I),ABAR),bneg(ABAR),I,bneg(A),bmul(bneg(I),A)] BF=[[row[(j-i)%6] for j in range(6)] for i in range(6)]; BFc=[[rowc[(j-i)%6] for j in range(6)] for i in range(6)] def bS(H,Hc,a,b,c,d): t=[Fr(0)]*8 for y in range(6): t=badd(t,bmul(bmul(H[a][y],Hc[b][y]),bmul(Hc[c][y],H[d][y]))) return t ok=all(bS(BF,BFc,i,j,0,0)==[Fr(0)]*8 for i in range(6) for j in range(6) if i!=j) # rows orthogonal: S(i,j,0,0) = sum H_iy conj(H_jy) zBF=sum(not any(bS(BF,BFc,*t)) for t in itertools.permutations(range(6),4)) BFt=[list(r) for r in zip(*BF)]; BFct=[list(r) for r in zip(*BFc)] zBFt=sum(not any(bS(BFt,BFct,*t)) for t in itertools.permutations(range(6),4)) say(" BF_6 exact Hadamard: %s, zeros of S for H and H^t: %d, %d of 360"%(ok,zBF,zBFt)) # the order 8 families, as formal Laurent monomials (root of unity exponent mod 6, parameter exponents) class Mono: def __init__(s,e=0,x=(0,0,0,0)): s.e=e%6; s.x=tuple(x) def __mul__(a,b): b=b if isinstance(b,Mono) else C(b); return Mono(a.e+b.e,[u+v for u,v in zip(a.x,b.x)]) __rmul__=__mul__ def __truediv__(a,b): b=b if isinstance(b,Mono) else C(b); return Mono(a.e-b.e,[u-v for u,v in zip(a.x,b.x)]) def __rtruediv__(a,b): return C(b)/a def __neg__(a): return Mono(a.e+3,a.x) def __pow__(a,k): return Mono(a.e*k,[u*k for u in a.x]) def conj(a): return Mono(-a.e,[-u for u in a.x]) def C(c): return {1:Mono(0),-1:Mono(3)}[c] P=[Mono(0,[1 if j==i else 0 for j in range(4)]) for i in range(4)]; W=Mono(2) def F82(a,b): return [[1,1,1,1,1,1,1,1],[1,-1,b,-b,-b/a,b/a,a,-a],[1,-a,b/a,b,-b/a,-b,-1,a],[1,1/a,b/a**2,-b/a,b/a,-b/a**2,-1/a,-1], [1,1,-1,-1,-1,-1,1,1],[1,a,-b,b/a,-b/a,b,-a,-1],[1,-1/a,-b/a,-b/a**2,b/a,b/a**2,-1,1/a],[1,-1,-b/a**2,b/a**2,b/a,-b/a,1/a,-1/a]] def F84(a,b,c,d): return [[1,1,1,1,1,1,1,1],[1,-1,a*d,-a*d,-b*d,b*d,1,-1],[1,-1/c,d,d,-d,-d,-1,1/c],[1,1/(c*d),-a*d,a*d,-1,-1,1,-1/(c*d)], [1,1/c,a*d**2,-a*d**2,b*d**2,-b*d**2,-1,-1/c],[1,d,-d,-d,-b*d**2,b*d**2,-1,d],[1,-1/(c*d),-1,-1,b*d,-b*d,1,1/(c*d)],[1,-d,-a*d**2,a*d**2,d,d,-1,-d]] def K82(a,b): return [[1,1,1,1,1,1,1,1],[1,a,-b**2,-b**2/a,-1,-a,b**2/a,b**2],[1,a/b,-1,b/a,1,-a/b,-b/a,-1],[1,-b,b,b**2/a,-b/a,-1,b/a,-b**2/a], [1,-a,-b,b,-a/b,a/b,-1,a],[1,-1,b,-b/a,b/a,-b,-b**2/a,b**2/a],[1,-a/b,-b,-1,a/b,a,b,-a],[1,b,b**2,-b,-1,b,-b,-b**2]] def M81(a,w=W): return [[1,1,1,1,1,1,1,1],[1,-w*a**2,a,w*a,-1,w*a**2,-a,-w*a],[1,a,-a,a**3,-a**3,-a**4,a**4,-1],[1,w*a,-w/a,-w*a,w/a,w**2,-1,-w**2], [1,-1,w/a,-a**3,w*a**2,-w*a**2,a**3,-w/a],[1,w*a**2,w,-w*a**4,-w*a**2,-w**2*a**4,-a**4,w**2],[1,-a,-w,-1,-w/a,w,a,w/a],[1,-w*a,-1,w*a**4,a**3,-w*a**4,-a**3,w*a]] def formal(H): return [[e if isinstance(e,Mono) else C(e) for e in r] for r in H] def fS(H,a,b,c,d): acc={} for y in range(len(H)): m=H[a][y]*H[b][y].conj()*H[c][y].conj()*H[d][y] v=acc.setdefault(m.x,[0]*6); v[m.e]+=1 return all(not any(reduce_mod(v,phi(6))) for v in acc.values()) def fzeros(H): return sum(fS(H,*t) for t in itertools.permutations(range(len(H)),4)) def fortho(H): N=len(H) for i in range(N): for j in range(i+1,N): acc={} for y in range(N): m=H[i][y]*H[j][y].conj(); v=acc.setdefault(m.x,[0]*6); v[m.e]+=1 if not all(not any(reduce_mod(v,phi(6))) for v in acc.values()): return False return True for name,f,k in [("F_{8,2}",F82,2),("F_{8,4}",F84,4),("K_{8,2}",K82,2),("M_{8,1}",M81,1)]: H=formal(f(*P[:k])); Ht=[list(r) for r in zip(*H)] say(" %-9s Hadamard identically: %s, S identically 0 on %d, %d of 1680 quadruples, for H and H^t"%(name,fortho(H),fzeros(H),fzeros(Ht))) rng=random.Random(1) for p,tr in ((23,0),(31,0),(47,0),(103,0),(103,1)): H=paley1(p); N=p+1; cnt=0; zero=None; mod8=set(); sq=set() if tr: H=H.T.copy() for _ in range(300): a,b,c=rng.sample(range(N),3) Sd=[int((H[a]*H[b]*H[c]*H[d]).sum()) for d in range(N) if d not in (a,b,c)] mod8|={s%8 for s in Sd}; sq.add(sum(s*s for s in Sd)) if 0 in Sd and zero is None: d=[d for d in range(N) if d not in (a,b,c)][Sd.index(0)]; zero=sorted([a,b,c,d]) say(" P^1_%-3d%s S mod 8 on 300 random triples a,b,c and all d: %s, sum over d of S^2: %s, a zero at rows %s"%(N,"^t" if tr else " ",sorted(mod8),sorted(sq),zero)) say(" real N=0(8): 64(N-3) > N^2 for N=8,...,56: %s, fails at N=64: %s"%(all(64*(N-3)>N*N for N in range(8,57,8)),64*(64-3)>64*64)) # ---------------- Part 3 ---------------- say("Part 3: condition C1, |S|^2 = N^2 modulo P on the quadruples of distinct indices") def absq(v,q): w=[0]*q; vi=[int(x) for x in v] for i in range(q): if vi[i]: for j in range(q): if vi[j]: w[(i-j)%q]+=vi[i]*vi[j] return w def c1primes(E,q): N=len(E); els=[] for t in itertools.permutations(range(N),4): w=absq(svec(E,q,*t),q); w[0]-=N*N; els.append(w) g=0 for w in els[:8]: g=math.gcd(g,abs(cnorm(w,q))) out=[] for p in primes_of(g): qq=q; a=0 while qq%p==0: qq//=p; a+=1 s2=next(t for t in range(qq) if (p**a*t)%qq==1%qq) if qq>1 else 0 # modulo a prime above p, zeta_q = zeta_{p^a}^? zeta_{q'}^{s2} with zeta_{p^a} = 1 G=pmod_poly(list(phi(qq)),p) if qq>1 else None alive=True for w in els: if qq==1: if sum(w)%p: alive=False; break continue A=[0]*qq for j,c in enumerate(w): A[(j*s2)%qq]+=c G=pgcd(G,A,p) if len(G)<=1: alive=False; break if alive: out.append(p) return g,out for name,q,E in [("I_{8,12}",12,I812),("K_{8,30}",30,K830)]: for tr in (0,1): g,out=c1primes(E.T.copy() if tr else E,q) say(" %-9s %s: primes of the first 8 norms %s, primes where C1 holds: %s"%(name,"H^t" if tr else "H ",primes_of(g),out)) # ---------------- Part 4 ---------------- say("Part 4: residue sweeps of the affine test over F_q, at the primes not dividing N") class GF: def __init__(s,p,f=1): s.p,s.f,s.q=p,f,p**f; q=s.q mod=None if f>1: for c in itertools.product(range(p),repeat=f): if c[0]==0: continue poly=list(c)+[1] if all(sum(poly[i]*pow(x,i,p) for i in range(f+1))%p for x in range(p)) and (f<=3): mod=poly; break V=[[(x//p**i)%p for i in range(f)] for x in range(q)]; toint=lambda v: sum(c*p**i for i,c in enumerate(v)) s.ADD=np.array([[toint([(a+b)%p for a,b in zip(V[x],V[y])]) for y in range(q)] for x in range(q)]) def mul(x,y): pr=[0]*(2*f-1) for i,a in enumerate(V[x]): for j,b in enumerate(V[y]): pr[i+j]+=a*b for k in range(2*f-2,f-1,-1): c=pr[k] if c: for i in range(f+1): pr[k-f+i]-=c*mod[i] return toint([c%p for c in pr[:f]]) s.MUL=np.array([[mul(x,y) for y in range(q)] for x in range(q)]) s.NEG=np.array([toint([(-c)%p for c in V[x]]) for x in range(q)]) for g in range(2,q) if q>2 else [1]: x=1; seen=set() for k in range(q-1): seen.add(x); x=s.MUL[x,g] if len(seen)==q-1: break s.EXP=np.zeros(q-1,dtype=np.int64); s.LOG=np.full(q,-1,dtype=np.int64); x=1 for k in range(q-1): s.EXP[k]=x; s.LOG[x]=k; x=s.MUL[x,g] s.INV=np.zeros(q,dtype=np.int64) for x in range(1,q): s.INV[x]=s.EXP[(-s.LOG[x])%(q-1)] def m(s,*xs): r=1 for x in xs: r=int(s.MUL[r,x]) return r _setup={} def setup(N): if N not in _setup: QA=np.array(list(itertools.product(range(N),repeat=4))); a,b,c,d=QA.T D=(a!=b)&(a!=c)&(a!=d)&(b!=c)&(b!=d)&(c!=d); R=(a!=c)&(b!=d) TX=R&(a==d)&(b!=c); TY=R&(b==c)&(a!=d); TXY=R&(a==d)&(b==c) ix=lambda a,b,c,d: ((a*N+b)*N+c)*N+d GA=np.array([(ix(*([t for t in range(N) if t not in (0,dd)][:3]),dd),ix(*([t for t in range(N) if t not in (0,dd)][:3]),0)) for dd in range(1,N)]) _setup[N]=(a,b,c,d,D,TX,TY,TXY,GA) return _setup[N] def affine(F,R): # returns (X,Y) if the row gauged profile of R is affine, else None N=R.shape[0]; a,b,c,d,D,TX,TY,TXY,GA=setup(N); n=F.q-1; LR=F.LOG[R] S=np.zeros(len(a),dtype=np.int64) for y in range(N): S=F.ADD[S,F.EXP[(LR[a,y]-LR[b,y]-LR[c,y]+LR[d,y])%n]] sig=F.MUL[S,F.INV[N%F.p]] if (sig[D]==0).any(): return None L=(F.LOG[F.NEG[1]]-F.LOG[sig])%n x=np.zeros(N,dtype=np.int64); x[1:]=(L[GA[:,0]]-L[GA[:,1]])%n fac=(x[a]-x[b]-x[c]+x[d])%n if ((fac-L)[D]%n!=0).any(): return None out=[] for mask in (TX,TY,TXY): s=sig[mask]; f=fac[mask]; v=np.where(s==0,0,F.EXP[(F.LOG[np.maximum(s,1)]+f)%n]) if (v!=v[0]).any(): return None out.append(int(v[0])) X,Y=int(F.ADD[out[0],1]),int(F.ADD[out[1],1]) assert int(F.ADD[X,Y])==int(F.ADD[out[2],1]) return X,Y def relations(F,N,X,Y): Nk=N%F.p; return int(F.ADD[X,Y])==(Nk-2)%F.p and int(F.MUL[X,Y])==1 F=GF(7) for lam in (3,5): R=np.ones((8,8),dtype=np.int64); np.fill_diagonal(R,lam); X,Y=affine(F,R) say(" check: J_8+(%d-1)I_8 over F_7: X,Y=%d,%d, X=-lambda: %s, X+Y=N-2 and XY=1: %s"%(lam,X,Y,X==(-lam)%7,relations(F,8,X,Y))) def rF82(F,a,b): m=F.NEG[1]; ia=int(F.INV[a]); M=F.m return [[1]*8,[1,m,b,M(m,b),M(m,b,ia),M(b,ia),a,M(m,a)],[1,M(m,a),M(b,ia),b,M(m,b,ia),M(m,b),m,a],[1,ia,M(b,ia,ia),M(m,b,ia),M(b,ia),M(m,b,ia,ia),M(m,ia),m], [1,1,m,m,m,m,1,1],[1,a,M(m,b),M(b,ia),M(m,b,ia),b,M(m,a),m],[1,M(m,ia),M(m,b,ia),M(m,b,ia,ia),M(b,ia),M(b,ia,ia),m,ia],[1,m,M(m,b,ia,ia),M(b,ia,ia),M(b,ia),M(m,b,ia),ia,M(m,ia)]] def rK82(F,a,b): m=F.NEG[1]; ia=int(F.INV[a]); ib=int(F.INV[b]); M=F.m return [[1]*8,[1,a,M(m,b,b),M(m,b,b,ia),m,M(m,a),M(b,b,ia),M(b,b)],[1,M(a,ib),m,M(b,ia),1,M(m,a,ib),M(m,b,ia),m],[1,M(m,b),b,M(b,b,ia),M(m,b,ia),m,M(b,ia),M(m,b,b,ia)], [1,M(m,a),M(m,b),b,M(m,a,ib),M(a,ib),m,a],[1,m,b,M(m,b,ia),M(b,ia),M(m,b),M(m,b,b,ia),M(b,b,ia)],[1,M(m,a,ib),M(m,b),m,M(a,ib),a,b,M(m,a)],[1,b,M(b,b),M(m,b),m,b,M(m,b),M(m,b,b)]] def rR7(F,q,w): M=F.m; n=lambda x: int(F.NEG[x]); m=n(1); w2=M(w,w) return [[1]*7,[1,1,1,w,m,m,w2],[1,n(w2),w2,w2,q,n(q),w],[1,w,m,1,M(q,w2),n(M(q,w2)),w2],[1,m,n(w),m,n(q),q,n(w2)],[1,w2,w,M(q,w),q,n(M(q,w)),n(q)],[1,w2,w,n(M(q,w)),M(q,w),n(q),q]] for (p,f) in [(3,1),(5,1),(7,1),(11,1),(13,1),(3,2),(5,2)]: F=GF(p,f); U=range(1,F.q); res=[] for name,fam in (("F_{8,2}",rF82),("K_{8,2}",rK82)): hits=sum(affine(F,np.array(fam(F,a,b),dtype=np.int64).T.copy() if tr else np.array(fam(F,a,b),dtype=np.int64)) is not None for a in U for b in U for tr in (0,1)) res.append("%s %d"%(name,hits)) ws=[1] if p==3 else [w for w in U if w!=1 and F.m(w,w,w)==1] hits=[]; for w in ws: for q in U: R=np.array(rR7(F,q,w),dtype=np.int64) for tr in (0,1): r=affine(F,R.T.copy() if tr else R) if r: hits.append((q,tr,r,relations(F,7,*r))) say(" F_%-3d affine members: %s; R_7^q: %d affine residues, X+Y=N-2 and XY=1 on all: %s"%(F.q,", ".join(res),len(hits),all(h[3] for h in hits))) say("total time %.0fs"%(time.time()-T0))