--- a/fibre-sign-lag.py +++ b/fibre-sign-lag.py @@ -196,12 +196,48 @@ return float(np.mean(sgn[ii] * sgn[jj] < 0)) if len(ii) else float("nan") +def resolved_field(b, eps, support_mask=None): + """Resolve small floating coefficients symbolically on the requested domain. + + Nonempty integer-log vectors below the numeric threshold require a separate + high-precision path; refuse instead of treating an actual coefficient as zero. + """ + if not np.all(np.isin(eps, (-1.0, 0.0, 1.0))): + raise ValueError("The input must contain integer signs or zero") + active = np.ones(len(b["ns"]), dtype=bool) if support_mask is None else support_mask + arrays = [] + for name, idx in (("L", b["li"]), ("R", b["ri"])): + arr = coeff_from(b, name, eps) + A, B = b["spec"][name] + for i in np.flatnonzero(active & (np.abs(arr[idx]) <= 1e-9)): + mm = b["lo"] + int(idx[i]) + fact = b["facts"][mm] + weights = {} + for mask in range(1 << len(fact)): + d = math.prod(p for j, (p, e) in enumerate(fact) if mask >> j & 1) + if d <= A: + continue + for j, (p, e) in enumerate(fact): + for power in range(1, e - int(bool(mask >> j & 1)) + 1): + if p ** power > B: + weights[p] = weights.get(p, 0) + int(eps[d]) + if any(weights.values()): + raise ArithmeticError("Small nonzero coefficient needs higher precision") + arr[idx[i]] = 0.0 + arrays.append(arr) + return c_field(b, arrays[0], arrays[1]) + +def require_gates(gates): + for name in ("G1_factorization_ok", "G2_prime_member_zero_ok", "G3_independent_beta_ok", "G4_control_null_reproduces_true_ok"): + if gates.get(name) is not True: + raise ValueError("Instrument prerequisite failed: " + name) + def measure(x, draws): t0 = time.time() b = build(x) mu = mu_array(b["hi"]).astype(float) CL, CR = coeff_from(b, "L", mu), coeff_from(b, "R", mu) - c = c_field(b, CL, CR) + c = resolved_field(b, mu) ns = b["ns"] support = set(int(n) for n, v in zip(ns, c) if v != 0) sgn = np.sign(c) @@ -245,6 +281,7 @@ CLm, CRm = coeff_from(b, "L", mu), coeff_from(b, "R", mu) gates["G4_control_null_reproduces_true_ok"] = bool(np.allclose(c_field(b, CLm, CRm), c)) + require_gates(gates) pair_idx, observed = {}, {} for h in (2, 4, 6): ii, jj = pairs_of(ns, support, h) @@ -259,7 +296,7 @@ eps = (rng.integers(0, 2, size=b["hi"] + 1) * 2 - 1).astype(float) eps[np.logical_not(sqfree)] = 0.0 # a draw is a sign per SQUAREFREE d eps[1] = 1.0 - cd = c_field(b, coeff_from(b, "L", eps), coeff_from(b, "R", eps)) + cd = resolved_field(b, eps, s_mask) sd = np.sign(np.where(s_mask, cd, 0.0)) for h in (2, 4, 6): null[h].append(rate(sd, pair_idx[h][0], pair_idx[h][1]))