# irate.py -- the exponent I_30 = min { J(f) : f in V, T(f) >= 0 } of the rectangular paper, computed by convex duality: # min KL(f || uniform) subject to E_f[c_k] = 0 (k = 1..9: the 8 rectangle relations of A-B, and T at equality) equals # -min_lambda log E_unif[exp(lambda . c)], the minimizer being f_x = exp(lambda . c(x)) / Z. Newton's method on lambda. import numpy as np from scipy.optimize import minimize, LinearConstraint # root x in Z_30 <-> (sign, i mod 3, j mod 5) with x = 15*s + 10*i + 6*j mod 30, i.e. root = (-1)^s omega^i eta^j cell={} for s in range(2): for i in range(3): for j in range(5): cell[(15*s+10*i+6*j)%30]=(s,i,j) assert len(cell)==30 C=[] # rectangle relations: D_ij - D_i0 - D_0j + D_00 = 0, D = A - B, for i=1,2, j=1..4 for i in (1,2): for j in (1,2,3,4): c=np.zeros(30) for x,(s,a,b) in cell.items(): e=1 if s==0 else -1 w=(1 if (a,b)==(i,j) else 0)-(1 if (a,b)==(i,0) else 0)-(1 if (a,b)==(0,j) else 0)+(1 if (a,b)==(0,0) else 0) c[x]=e*w C.append(c) t=np.zeros(30) for x,(s,a,b) in cell.items(): if s==0 and (a,b)==(0,0): t[x]+=1 if s==1 and (a,b)==(0,0): t[x]-=1 if s==0 and (a,b) in ((0,1),(1,0)): t[x]-=1 C.append(t); C=np.array(C) lam=np.zeros(len(C)) for it in range(200): e=np.exp(C.T@lam); Z=e.sum(); f=e/Z g=C@f; Hs=(C*f)@C.T-np.outer(g,g) step=np.linalg.solve(Hs+1e-14*np.eye(len(C)),g); lam-=step if np.abs(g).max()<1e-14: break e=np.exp(C.T@lam); f=e/e.sum() J=float((f*np.log(30*f)).sum()) dual = -np.log(e.mean()) assert np.max(np.abs(C @ f)) < 1e-11 assert lam[-1] > 0 and f.min() > 0 assert abs(J-dual) < 1e-11 print("primal-dual difference", abs(J-dual)) print("max constraint violation",np.abs(C@f).max(),"lambda_T",lam[-1]) print("I_30 = %.12f (log 5 = %.6f, exp(-I_30) = %.6f)"%(J,np.log(5),np.exp(-J))) for x in range(30): print(x,cell[x],"%.6f"%f[x], "%.3f"%(30*f[x])) # Independent primal formulation: entropy on the probability simplex, # eight equalities, and the inequality T >= 0. Use fixed random starts. rng = np.random.default_rng(20261130) equalities = np.vstack([np.ones(30), C[:-1]]) target = np.r_[1., np.zeros(8)] constraints = [LinearConstraint(equalities, target, target), LinearConstraint(C[-1:], 0., np.inf)] def objective(p): return np.sum(p * np.log(30*p)) def gradient(p): return np.log(30*p) + 1 cost_errors = [] constraint_errors = [] for run in range(20): result = minimize(objective, rng.dirichlet(np.ones(30)), jac=gradient, method="SLSQP", constraints=constraints, bounds=[(1e-12, 1.)]*30, options={"ftol": 1e-13, "maxiter": 1000}) assert result.success, (run, result.message) residual = max(np.max(np.abs(equalities @ result.x-target)), max(0., -float(C[-1] @ result.x))) assert residual < 1e-9 assert abs(result.fun-J) < 1e-10 cost_errors.append(abs(result.fun-J)) constraint_errors.append(residual) print("20 primal SLSQP starts passed (seed 20261130)") print("max primal cost difference", max(cost_errors)) print("max primal constraint residual", max(constraint_errors))