"""Independent small-dimensional tests of the construction and the audit normalizer.""" import json import sys from pathlib import Path import numpy as np from scipy.integrate import quad from xevarin import fit, null_score, repair_cost def orth(n,r,g): return np.linalg.qr(g.normal(size=(n,r)),mode='reduced')[0] def verify(): rng=np.random.default_rng(92049) cases=0 max_error=0. for d in range(1,8): for k in range(d+1): u,v=orth(d,k,rng),orth(d,k,rng) r=u@np.diag(np.linspace(.35,.67,k))@v.T a=np.eye(d)-r for p in range(k+1): for q in range(k+1): # No matrix-valued call to the world: actually evaluate each scalar. count=0 def separate(left,right): nonlocal count scalars=np.empty((left.shape[1],right.shape[1])) for i,lvec in enumerate(left.T): for j,rvec in enumerate(right.T): scalars[i,j]=lvec@(rvec-a@rvec) count+=1 return scalars model=fit(separate,d,k,rng,u[:,:p],v[:,:q]) child=model.compile(np.eye(d)) err=np.linalg.norm(np.eye(d)-a@child,2) max_error=max(err,max_error) assert count==repair_cost(d,k,p,q) assert err<1e-7,(d,k,p,q,err) cases+=1 # Noncommuting estimate: right-sided inverse in the residual identity. d=9 r,rhat=(rng.normal(size=(d,d))/16 for i in range(2)) parent=np.eye(d)+rng.normal(size=(d,d))/20 a=np.linalg.solve(parent.T,(np.eye(d)-r).T).T child=np.linalg.solve((np.eye(d)-rhat).T,parent.T).T difference=(np.eye(d)-a@child)-(r-rhat)@np.linalg.inv(np.eye(d)-rhat) identity_error=float(np.linalg.norm(difference)) assert identity_error<1e-13 quadrature=0. for tau,h,n in [(1.,.4,.1),(.03,.0075,0.),(1.,2.,.7),(1.,.08,0.)]: f=lambda z: np.sqrt(2/np.pi)*np.exp(-z*z/2)/np.sqrt(1+(n*n+tau*tau*z*z)/(h*h)) value=quad(f,0,np.inf,epsabs=1e-13)[0] quadrature=max(quadrature,abs(value-null_score(tau,h,n))) assert quadrature<1e-12 # The public, fixed basis is blind to an entire nonzero operator. hidden=np.array([[0.,0.],[.4,0.]]) e=np.array([1.,0.]) assert np.linalg.norm(e@hidden)==0 and np.isclose(np.linalg.norm(hidden,2),.4) return dict(scalar_loop_cases=cases,worst_small_case_op=float(max_error), residual_identity_error=identity_error,max_normalizer_quadrature_error=float(quadrature)) if __name__=='__main__': result=json.dumps(verify(),indent=2)+'\n' print(result,end='') if len(sys.argv)>1: Path(sys.argv[1]).write_text(result,encoding='utf-8')