import math, json def figure(id,title,xlabel,ylabel,x,series,caption): return dict(id=id,title=title,kind='analytical_curve',x_label=xlabel,y_label=ylabel,x=dict(name=xlabel,values=x),series=[dict(name=n,values=v)for n,v in series],caption=caption) def close(a,b,tol=1e-10): assert abs(a-b)<=tol*max(1,abs(a),abs(b)),(a,b) scales=[.1,.2,.5,1,2,5,10] theta=[1/(1+s*s)for s in scales] for s,t in zip(scales,theta): close(2*(t-1)+2*s*s*t,0) for x in [-2,0,.5,2]: close((x-1)**2+(1/s**2)*(s*x)**2,(x-1)**2+x*x) close(theta[0],100/101);close(theta[-1],1/101) assert abs(1-2*.1*(1+.1**2))<1 assert abs(1-2*.1*(1+10**2))>1 modes=[1,2,3,4,6,8,12] field=[1/(k*math.pi)**2 for k in modes] gradient=[1/(k*math.pi)for k in modes] for k,f,g in zip(modes,field,gradient): n=12000 amp=math.sqrt(2)/(k*math.pi)**2 norm=math.sqrt(sum((amp*math.sin(k*math.pi*(j+.5)/n))**2 for j in range(n))/n) close(norm,f,1e-9);close(g/f,k*math.pi) # Affine error: second derivative is zero but endpoints remain nonzero. for x in [0,.2,.8,1]: h=1e-3; e=lambda t:2+3*t close((e(x+h)-2*e(x)+e(x-h))/h**2,0,1e-8) figs=[ figure('penalty-unit-scale','Residual scaling changes the optimum','Residual scale s (dimensionless)','Parameter / unscaled absolute error',scales,[('Optimal theta',theta),('Data mismatch',[1-t for t in theta]),('Compensated theta',[.5]*len(scales))],'Constructed inconsistent scalar objective (theta-1)^2+(s theta)^2. Compensating the residual weight by 1/s^2 preserves theta=0.5. No neural benchmark.'), figure('poisson-mode-error','Equal residual norm, unequal field error','Positive Fourier-sine mode k','L2 norm (dimensionless)',modes,[('Field error, residual norm=1',field),('Gradient error, residual norm=1',gradient)],'Constructed homogeneous Dirichlet Poisson modes on [0,1]. Residual L2 norm fixed to one; field and gradient error scale as (k pi)^-2 and (k pi)^-1. No physical measurements.')] print(json.dumps(dict(status='PASS',checks=['optimum derivative','unit-compensated objective','stable/unstable steps','quadrature norm','gradient relation','affine null residual'],figures=figs)))