import json, math, time import numpy as np import torch from torch import nn SEED=2049 np.random.seed(SEED); torch.manual_seed(SEED) try: device=torch.device('cuda' if torch.cuda.is_available() else 'cpu') if device.type=='cuda': torch.zeros(1,device=device).sum().item() except Exception: device=torch.device('cpu') def directions(n,d,rng): q=rng.normal(size=(n,d)); return q/np.linalg.norm(q,axis=1,keepdims=True) def wos_ball(x, K, M, d, lam=0., delta=1e-4, corrected=False, seed=0): rng=np.random.default_rng(seed); x=np.asarray(x); B=len(x) z=np.repeat(x[:,None,:],K,axis=1); t=np.zeros((B,K)); y=np.zeros((B,K)) for j in range(M): r=1.-np.linalg.norm(z,axis=2) active=r>delta dt=r*r/((d if corrected else 2*d)) y += np.exp(-lam*t)*dt # h1=1, h0=0 u=directions(B*K,d,rng).reshape(B,K,d) z += r[...,None]*u; t += dt if not active.any(): break # zero boundary data, approximate truncation by zero return y.mean(axis=1), y, t def wos_square(x,K,M,delta=2e-3,seed=0): rng=np.random.default_rng(seed); x=np.asarray(x); B=len(x); d=2 z=np.repeat(x[:,None,:],K,axis=1); t=np.zeros((B,K)); y=np.zeros((B,K)) for j in range(M): r=np.minimum(z,1-z).min(axis=2); dt=r*r/(2*d) f=2*(z[:,:,0]*(1-z[:,:,0])+z[:,:,1]*(1-z[:,:,1])) y += f*dt u=directions(B*K,d,rng).reshape(B,K,d); z += r[...,None]*u; t += dt if np.max(r)