import json import time import numpy as np import sympy as s...
Prompt
import json import time import numpy as np import sympy as sp x, y = sp.symbols('x y') LOG = [] def say(*a): s = ' '.join(str(z) for z in a) print(s, flush=True) LOG.append(s) def build(d): """General (NON-normalized) coefficient ansatz, natively in x,y.""" mons = [(i, deg-i) for deg in range(d+1) for i in range(deg+1)] n = len(mons) pc = sp.symbols(f'p0:{n}') qc = sp.symbols(f'q0:{n}') P = sum(pc[k]*x**i*y**j for k, (i, j) in enumerate(mons)) Q = sum(qc[k]*x**i*y**j for k, (i, j) in enumerate(mons)) J = sp.expand(sp.diff(P, x)*sp.diff(Q, y) - sp.diff(P, y)*sp.diff(Q, x)) jeqs = [co for mo, co in sp.Poly(J-1, x, y).terms()] # ALL coeffs of J-1 coll = [sp.expand(P.subs({x: 0, y: 0}) - P.subs({x: 1, y: 0})), sp.expand(Q.subs({x: 0, y: 0}) - Q.subs({x: 1, y: 0}))] free = list(pc)+list(qc) return free, jeqs, coll, mons def make_system(free, jeqs, coll, lam): """R(z) holomorphic residual; JR(z) its EXACT symbolic Jacobian.""" lamc = sp.Float(0) if lam == 0 else sp.Float(lam) E = jeqs + [lamc*e for e in coll] fun_R = sp.lambdify(list(free), E, 'numpy') fun_J = sp.lambdify(list(free), sp.Matrix(E).jacobian(free), 'numpy') m, n = len(E), len(free) def R(z): return np.asarray(fun_R(*[complex(t) for t in z]), dtype=np.complex128).reshape(m) def JR(z): return np.asarray(fun_J(*[complex(t) for t in z]), dtype=np.complex128).reshape(m, n) return R, JR def newton(R, JR, z0, maxit=70, tol=1e-12): z = np.array(z0, dtype=np.complex128) hist = [] for it in range(maxit): try: r = R(z) except (OverflowError, ValueError, FloatingPointError): return z, np.inf, hist, 'overflow' nr = float(np.linalg.norm(r)) hist.append(nr) if not np.isfinite(nr): return z, nr, hist, 'overflow' if nr < tol: return z, nr, hist, 'converged' Jc = JR(z) try: dz, *_ = np.linalg.lstsq(Jc, -r, rcond=None) except np.linalg.LinAlgError: return z, nr, hist, 'linalg' accepted = False alpha = 1.0 for _ in range(14): z2 = z + alpha*dz try: n2 = float(np.linalg.norm(R(z2))) except (OverflowError, ValueError, FloatingPointError): n2 = np.inf if np.isfinite(n2) and n2 < nr*(1-1e-12): z, accepted = z2, True break alpha *= 0.5 if not accepted: return z, nr, hist, 'stalled' return z, nr, hist, 'maxit' def exact_try(d, z, free, mons): """On a numerical hit: rationalise coefficients and verify EXACTLY.""" sub = {} for c, val in zip(free, z): val = complex(val) re, im = round(val.real, 9), round(val.imag, 9) sub[c] = sp.nsimplify(re + sp.I*im, rational=True) P = sp.expand(sum(sub[free[k]]*x**i*y**j for k, (i, j) in enumerate(mons))) Q = sp.expand(sum(sub[free[len(mons)+k]]*x**i*y**j for k, (i, j) in enumerate(mons))) J = sp.expand(sp.diff(P, x)*sp.diff(Q, y) - sp.diff(P, y)*sp.diff(Q, x)) p0 = [sp.simplify(e.subs({x: 0, y: 0})) for e in (P, Q)] p1 = [sp.simplify(e.subs({x: 1, y: 0})) for e in (P, Q)] return dict(J=str(J)[:400], constant_nonzero=bool(J.is_number and J != 0), images_0_0=[str(t) for t in p0], images_1_0=[str(t) for t in p1], exact_collision=bool(p0 == p1), P=str(P)[:400], Q=str(Q)[:400]) say('='*78) say('ADVERSARIAL SEARCH: collision imposed FIRST, then solve for J = 1.') say('Built-in exact collision: (0,0) and (1,0) are DISTINCT points of C^2.') say('No triangular/Hamiltonian/tame restriction, no DF(0)=I normalization.') say('Newton with EXACT symbolic Jacobian (lambdified), line search.') say('='*78) rng = np.random.default_rng(20261027) summary = {} for d, nstarts, ncont in [(4, 200, 30), (5, 100, 20), (6, 40, 12), (7, 15, 6), (8, 6, 3)]: free, jeqs, coll, mons = build(d) n = len(free) say(f'--- degree d={d}: {n} complex unknowns, ' f'{len(jeqs)+len(coll)} complex equations ---') t0 = time.time() idbias = np.zeros(n, dtype=np.complex128) idbias[2] = 1.0 # coefficient of x in P (identity-like start) idbias[len(mons)+1] = 1.0 # coefficient of y in Q # POSITIVE CONTROL: lam=0, solutions definitely exist (automorphisms). R0, J0 = make_system(free, jeqs, coll, 0.0) ctrl, seeds = [], [] for k in range(min(25, nstarts)): z0 = idbias + 0.6*rng.standard_normal(n) + 0.6j*rng.standard_normal(n) z, nr, hist, status = newton(R0, J0, z0, maxit=50) ctrl.append(nr) if nr < 1e-9: seeds.append(z) say(f' control lam=0: best residual {min(ctrl):.3e}; ' f'{len(seeds)} verified solutions (solver demonstrably finds roots)') # DIRECT adversarial runs at lam=1 R1, J1 = make_system(free, jeqs, coll, 1.0) best, bestz = np.inf, None statuses = {} for k in range(nstarts): z0 = idbias + 0.9*rng.standard_normal(n) + 0.9j*rng.standard_normal(n) z, nr, hist, status = newton(R1, J1, z0, maxit=70) statuses[status] = statuses.get(status, 0)+1 if nr < best: best, bestz = nr, z.copy() say(f' direct lam=1: {nstarts} starts, best ||R|| = {best:.6g}; ' f'statuses {statuses}') # CONTINUATION from genuine lam=0 solutions towards the collision cont_best, cont_z = np.inf, None for z in seeds[:ncont]: zz = z.copy() nr = np.inf for lam in (0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0): Rl, Jl = make_system(free, jeqs, coll, lam) zz, nr, hist, status = newton(Rl, Jl, zz, maxit=40) if not np.isfinite(nr): break if np.isfinite(nr) and nr < cont_best: cont_best, cont_z = nr, zz.copy() say(f' continuation 0->1: {min(len(seeds), ncont)} tracks, ' f'best ||R|| at lam=1 = {cont_best:.6g}') overall = min(best, cont_best) zbest = bestz if best <= cont_best else cont_z hit = None if overall < 1e-9 and zbest is not None: hit = exact_try(d, zbest, free, mons) say(' *** NUMERICAL HIT — exact verification:', hit) else: say(' no solution at lam=1 (no counterexample at this degree).') summary[d] = dict(unknowns=n, equations=len(jeqs)+2, control_best=float(min(ctrl)), control_solutions=len(seeds), direct_best=float(best), continuation_best=float(cont_best), numerical_hit=hit, seconds=round(time.time()-t0, 1)) say(f' elapsed {time.time()-t0:.1f}s') say('='*78) say('SUMMARY') say(json.dumps(summary, indent=2, default=str)) with open('adversarial.json', 'w') as f: json.dump(summary, f, indent=2, default=str) with open('adversarial.log', 'w') as f: f.write('\n'.join(LOG)+'\n') say('wrote adversarial.json / adversarial.log') Send back the complete code with all the fixes. Fix each of the listed errors one by one, making sure to actually correct them so that there are 0 errors remaining. Keep the original imports, since the files exist. Write out every single character; do not abbreviate anything. Fix every error. There must be exactly one file. Do not write anything else; just output the complete code, and it must not contain any comments. Never, under any circumstances, use simplified, substitute, dummy, simulated, or fake code. Write the entire file as complete, unabridged, production-ready code in a single code block. It must be 100% error-free, a complete, error-free file, and must be submitted as a downloadable file. These requirements are mandatory and must be strictly adhered to. If no list of errors is provided, you must find all the errors and fix them. If there were comments in the original code, delete them. And most importantly: YOU MUST NEVER SIMPLIFY!
Response not available