Jump to content

The Principia Machina/TPC-PY/gen sr8 order corpus.py

From The Invariance Itself
Revision as of 04:32, 28 September 2026 by BFP Rauch (talk | contribs) (1 revision imported)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
# -*- coding: utf-8 -*-
"""Generator for tpc/vectors/sr8_order_corpus.json: regression vectors for
the SR8 operation order. Found 2026-09-22 by a 20,000-case differential run:
TPC-PY's sr8_scale_factor did not follow TPC-PHP's operations one for one
(sum() instead of explicit accumulation, which Python 3.12 made
compensated; x/(u*u) instead of x*w; d**2 instead of d*d). 502 cases
differed in the last bits and these 5 after canon(), where the reduced
chi-square cancels badly. The expectations are TPC-PHP's, the residence of
record; "before" records what TPC-PY returned before the fix.
Run: cd tpc && python3 gen_sr8_order_corpus.py (Windows: py ...).
Recovered 2026-09-27 (BASE-GEN; record 0.18.0 section 7) from the TIDY 3
archive, tidy3_2026-09-25/gen_sr8_order_corpus.py, SHA-256
8b70b3b6c96936de2b16b250bacd0a048cf1d2b8bbad69b77678e9f5f70699de, where it
regenerates the archived corpus byte for byte. Changed in the tree: the
writer only (write_layout: id and fn; input; before_fix and expect;
RD-C8-2). No case changed."""
import json, os, sys
HERE = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, HERE)
import tpc_strike as S


def write_layout(path, corpus, groups):
    """Write the corpus in the layout the published file carries (the
    corpus is never re-laid; the writer follows it): the meta as
    json.dumps(indent=1); each case on the lines groups(case) names, the
    keys of a line joined by ", ", the lines by ",\n   "; values as
    json.dumps writes them, a non-ASCII character as itself; UTF-8, LF."""
    def item(k, v):
        return json.dumps(k) + ": " + json.dumps(v, ensure_ascii=False)
    rows = []
    for c in corpus["cases"]:
        gs = groups(c)
        assert [k for g in gs for k in g] == list(c), c.get("id")
        rows.append("  {" + ",\n   ".join(", ".join(item(k, c[k]) for k in g)
                                           for g in gs) + "}")
    text = ('{\n "meta": '
            + json.dumps(corpus["meta"], indent=1, ensure_ascii=False).replace("\n", "\n ")
            + ',\n "cases": [\n' + ",\n".join(rows) + "\n ]\n}\n")
    with open(path, "w", encoding="utf-8", newline="\n") as f:
        f.write(text)

CASES = [
    ("sr8/op-order-1", [{"x": 9846610.80984243, "u": 1.5822615395810278e-05}, {"x": 0.0038145315461680144, "u": 287923.4304921431}],
     "34.1987131412", "34.1987131414"),
    ("sr8/op-order-2", [{"x": -1584388362.27871, "u": 4.806467584521203e-06}, {"x": 22.70131971090118, "u": 2524031.3370337626}, {"x": 20615.91282076606, "u": 141208.2040439008}],
     "7946.40942162", "7946.4094217"),
    ("sr8/op-order-3", [{"x": -7716423.681290181, "u": 2.872899182820292e-06}, {"x": 2774253.1950992066, "u": 254364.93029610152}, {"x": -756347.9729775285, "u": 570079.7540444033}],
     "30.4139147233", "30.4139147242"),
    ("sr8/op-order-4", [{"x": 857.9928255687573, "u": 285226.0583909543}, {"x": 358803.2905123125, "u": 3.144464414394274e-08}],
     "1.25495300011", "1.25495436535"),
    ("sr8/op-order-5", [{"x": -0.004984302674057946, "u": 2661207.9023190914}, {"x": 140425686.29099628, "u": 0.0002930210897765783}],
     "52.7676496728", "52.7676496729"),
]

cases, bad = [], 0
for cid, ms, expect, before in CASES:
    got = S.canon(S.sr8_scale_factor(ms))
    if got != expect:
        bad += 1
        print("MISMATCH", cid, expect, got)
    cases.append({"id": cid, "fn": "sr8_scale_factor", "input": {"measurements": ms},
                  "before_fix": before, "expect": expect})
if bad:
    sys.exit("refusing to write: %d mismatches" % bad)
corpus = {"meta": {"name": "tpc_sr8_order_corpus", "version": "0.1.0",
                   "status": "VERIFIED (both residences)",
                   "conforms_tps": "0.1.0 revision 4 (SR8, TPS 4.4)",
                   "module": "tpc_strike 0.1.1",
                   "note": "Ill-conditioned SR8 inputs where the operation order shows after canon(); "
                           "expectations from TPC-PHP."},
          "cases": cases}
write_layout(os.path.join(HERE, "vectors", "sr8_order_corpus.json"), corpus,
             lambda c: [["id", "fn"], ["input"], ["before_fix", "expect"]])
print("wrote", len(cases), "SR8 order cases")