# wanderlei.py -- had16.3: is the list of 10 classes of bistochastic forms complete? # Alternating iteration u = ph(Hb), b = ph(H^t u) from many starts of 6 kinds, aimed at small basins of attraction: # 0 uniform random phases, 1 random 16-th roots of unity, 2 16-th roots + noise, 3 Dita-paired starts # (b_{2a+1} ~ +-i b_{2a}), 4 sign vectors + noise, 5 perturbations of the 10 known forms. # Each converged form is classified by invariants (moduli of row/column sums of B^2, B^3) against the 10 known # classes (had163reps.txt); a form outside them is saved immediately in newforms-.txt and announced. # Also counts the distinct forms per class (canonical: b/b_1, phases rounded to 1e-6, min with conjugate). # Usage: /usr/bin/python3 wanderlei.py had163.txt had163reps.txt WORKERS HOURS # Output: folder wanderlei-out/: log-.txt (every 10 min), newforms-.txt, forms--class.txt, summary.txt import sys, os, time, numpy as np, multiprocessing as mp np.seterr(all="ignore") B=2048; ITERS=3000 def ph(z): return z/np.abs(z) def fingerprint(H,b): s=b@H.T; a=np.conj(ph(s)); M=a[:,:,None]*H[None]*b[:,None,:]; M2=M*M; M3=M2*M return np.concatenate([np.sort(abs(M2.sum(2)),1),np.sort(abs(M2.sum(1)),1),np.sort(abs(M3.sum(2)),1),np.sort(abs(M3.sum(1)),1)],1) def canon(b): t=np.round(((np.angle(b/b[:,:1])/(2*np.pi))%1)*1e6).astype(np.int64)%1000000 tc=(-t)%1000000 out=[] for x,y in zip(t,tc): sx=' '.join(map(str,x)); sy=' '.join(map(str,y)); out.append(min(sx,sy)) return out def starts(rng,kind,reps): N=16 if kind==0: return np.exp(2j*np.pi*rng.random((B,N))) if kind==1: return np.exp(2j*np.pi*rng.integers(0,16,(B,N))/16) if kind==2: return np.exp(2j*np.pi*(rng.integers(0,16,(B,N))/16+rng.normal(0,rng.choice([.01,.03,.08]),(B,N)))) if kind==3: a=2*np.pi*rng.random((B,8)); eps=rng.choice([-1,1],(B,8)); d=rng.normal(0,rng.choice([.05,.2,.5]),(B,8)) th=np.empty((B,N)); th[:,0::2]=a; th[:,1::2]=a+eps*np.pi/2+d; return np.exp(1j*th) if kind==4: return np.exp(1j*(np.pi*rng.integers(0,2,(B,N))+rng.normal(0,rng.choice([.2,.5,1.]),(B,N)))) R=reps[rng.integers(0,len(reps),B)]; return R*np.exp(1j*rng.normal(0,rng.choice([.1,.3,.7,1.2]),(B,N))) def worker(w,hfile,rfile,hours,outdir): H=np.loadtxt(hfile); N=16 reps=np.exp(2j*np.pi*np.loadtxt(rfile)); ref=fingerprint(H,reps) rng=np.random.default_rng(1000+w); t0=time.time(); tlog=t0 seen=[set() for _ in range(10)]; hits=np.zeros(10,np.int64); new=0; nst=0; nconv=0; kinds=np.zeros(6,np.int64); batch=0 log=open(f'{outdir}/log-{w}.txt','a'); nf=open(f'{outdir}/newforms-{w}.txt','a') def report(final=False): msg=(f"[worker {w}] {(time.time()-t0)/3600:.2f} h: {nst} starts, {nconv} converged, new-class forms {new}; " f"distinct forms per class {[len(s) for s in seen]}") print(msg,flush=True); log.write(time.strftime('%H:%M:%S ')+msg+'\n'); log.flush() for c in range(10): with open(f'{outdir}/forms-{w}-class{c}.txt','w') as f: f.write('\n'.join(sorted(seen[c]))+'\n') while time.time()-t0 < hours*3600: kind=batch%6; batch+=1; b=starts(rng,kind,reps); kinds[kind]+=B for it in range(ITERS): u=ph(b@H.T); b=ph(u@H) res=np.abs(np.abs(b@H.T)-4).max(1); ok=res<1e-9; nst+=B; nconv+=int(ok.sum()); b=b[ok] if len(b)==0: continue F=fingerprint(H,b); D=np.abs(F[:,None,:]-ref[None]).max(2); cl=D.argmin(1); dmin=D.min(1) for k in np.nonzero(dmin>1e-4)[0]: new+=1; nf.write(f"kind {kind} start-batch {batch} phases "+' '.join('%.12f'%x for x in (np.angle(b[k]/b[k][0])/(2*np.pi))%1)+ " fingerprint "+' '.join('%.6f'%x for x in F[k])+'\n'); nf.flush() print(f" !!! worker {w}: form outside the 10 known classes (start kind {kind}), saved in newforms-{w}.txt",flush=True) good=dmin<=1e-4 for c,s in zip(cl[good],canon(b[good])): seen[c].add(s); hits[c]+=1 if time.time()-tlog>600: tlog=time.time(); report() report(final=True); log.close(); nf.close() if __name__=='__main__': hfile,rfile,W,hours=sys.argv[1],sys.argv[2],int(sys.argv[3]),float(sys.argv[4]) outdir='wanderlei-out'; os.makedirs(outdir,exist_ok=True) print(f"wanderlei: {W} workers, {hours} h, batches of {B} starts x {ITERS} iterations; output in {outdir}/",flush=True) H=np.loadtxt(hfile); reps=np.exp(2j*np.pi*np.loadtxt(rfile)) print("self-test: the 10 known forms are bistochastic:",bool(np.all(np.abs(np.abs(reps@H.T)-4)<1e-9)),flush=True) ps=[mp.Process(target=worker,args=(w,hfile,rfile,hours,outdir)) for w in range(W)] for p in ps: p.start() for p in ps: p.join() # merge tot=[]; for c in range(10): s=set() for w in range(W): fn=f'{outdir}/forms-{w}-class{c}.txt' if os.path.exists(fn): s|=set(l.strip() for l in open(fn) if l.strip()) tot.append(len(s)) nnew=sum(sum(1 for _ in open(f'{outdir}/newforms-{w}.txt')) for w in range(W) if os.path.exists(f'{outdir}/newforms-{w}.txt')) msg=f"wanderlei finished. Distinct forms per class (all workers): {tot}, total {sum(tot)}. Forms outside the 10 classes: {nnew}." print(msg,flush=True); open(f'{outdir}/summary.txt','w').write(msg+'\n')