#!/usr/bin/env python3 """C319 independent finite four-case sensitivity. --sources contains C317_DATA.json, C286_DATA.json; --out controls output. 20 annual configurations and40 exact same-phase configurations only. """ import argparse from fractions import Fraction as F from pathlib import Path import hashlib,json def rank(a): a=[[F(x) for x in row] for row in a];r=0 for c in range(len(a[0])): z=next((k for k in range(r,len(a)) if a[k][c]),None) if z is None:continue a[r],a[z]=a[z],a[r];v=a[r][c];a[r]=[x/v for x in a[r]] for k in range(len(a)): if k!=r: v=a[k][c];a[k]=[x-v*y for x,y in zip(a[k],a[r])] r+=1 return r def main(): ap=argparse.ArgumentParser();ap.add_argument('--sources',type=Path);ap.add_argument('--out',type=Path,default=Path(__file__).parent);a=ap.parse_args();root=Path(__file__).resolve().parents[1] paths={'C317_DATA.json':root/'C317/evidence/DATA.json','C286_DATA.json':root/'C313/sources/C286_DATA.json'};data={};hashes={} for n,p in paths.items(): raw=(a.sources/n if a.sources else p).read_bytes();data[n]=json.loads(raw);hashes[n]=hashlib.sha256(raw).hexdigest() reg=data['C286_DATA.json']['target_register'];cum={(r['Shem_Cainan'],r['Shem_index'],r['S']) for r in data['C286_DATA.json']['source_pairs']};old=data['C317_DATA.json'];rows=[];K=529 def rec(g,role,coord): return [r for r in reg if r['tradition']=='MT' and r['gear']==g and r['role']==role and r['Cainan']==1 and r['Terah60']==1 and r['full430']==1 and r['center']==coord] edge_pairs=[(0,1),(0,2),(0,3),(1,2),(1,3),(2,3)];inc=[] for u,v in edge_pairs: rr=[0]*4;rr[u]=-1;rr[v]=1;inc.append(rr) assert rank(inc)==3 for case in sorted(old['rows'],key=lambda r:r['i']): i,g=case['i'],case['gear'];p,q,f,n=case['p'],case['q'],case['f'],case['n'];role='Flood start' if case['rail']=='primary' else 'Flood close' for aa in [0,2]: for bb in [0,2]: S=p-460+aa;R=f-460+bb;left=q-S;right=n-R;cross=[[R-S,n-S],[R-q,n-q]];ca=(1,i+aa,S) in cum;rr=rec(g+bb//2,role,R);ra=len(rr)==1 assert len(rr) in [0,1] and all(x.get('rail') is None for x in rr) assert ca==(i+aa<=7) and ra==(g+bb//2<=3) both=cross[0][0]==cross[1][1]==17*K sides=left==right==2*K;allk=all(x%K==0 for row in cross for x in row) and left%K==right%K==0 assert both==(aa==bb) assert allk==sides==(aa==bb==2) assert [left,right]==[1060-aa,1060-bb] assert cross==[[8993+bb-aa,10053-aa],[7933+bb,8993]] coords=[S,q,R,n];dur=[coords[v]-coords[u] for u,v in edge_pairs] assert rank([row+[v] for row,v in zip(inc,dur)])==3 cp=[] for e in (F(-1,4),F(1,4)): Ss=F(S)+e;Rs=F(R)+e;qs=F(q)+e;ns=F(n)+e assert [Rs-Ss,ns-qs]==[8993+bb-aa,8993] assert qs-Ss==left and ns-Rs==right cp.append({'phase':str(e),'S':str(Ss),'q':str(qs),'R':str(Rs),'n':str(ns),'two17K':both,'allK':allk,'joint_source_admission':ca and ra}) rows.append({'i':i,'SP_gear':g,'SP_rail':case['rail'],'Flood_boundary':role,'a_cumulative_index_shift':aa,'b_regular_Gear_coordinate_shift':bb,'cumulative_ON_Shem_index':i+aa,'regular_MT_Gear':g+bb//2,'S':S,'q':q,'R':R,'n':n,'cumulative_source_admitted':ca,'regular_source_admitted':ra,'joint_source_admission':ca and ra,'regular_source_record':rr[0] if rr else None,'left_right_widths':[left,right],'crosswidths':cross,'two17K':both,'two2K_sides':sides,'allK':allk,'phases':cp}) assert len(rows)==20 summaries=[] for aa in [0,2]: for bb in [0,2]: xs=[r for r in rows if r['a_cumulative_index_shift']==aa and r['b_regular_Gear_coordinate_shift']==bb] summaries.append({'a':aa,'b':bb,'annual_cases':len(xs),'joint_admitted':sum(x['joint_source_admission'] for x in xs),'two17K':all(x['two17K'] for x in xs),'two2K_and_allK':all(x['allK'] for x in xs),'widths':xs[0]['left_right_widths'],'crosswidths':xs[0]['crosswidths']}) assert [s['joint_admitted'] for s in summaries]==[5,3,3,3] assert sum(x['joint_source_admission'] for x in rows)==14 sensitivity=[[-1,0],[-1,1],[-1,0],[0,1],[0,0],[0,-1]] assert rank(sensitivity)==2 out={'step':'C319','status':'passed','input_sha256':hashes,'scope':'Fixed five C317 cases, independent a,b choices0or2;40 same-phase variants.','rows':rows,'four_choice_summary':summaries,'counts':{'annual_configurations':20,'phase_configurations':40,'source_admitted_annual':14,'source_admitted_phases':28,'formal_two17K_annual':sum(r['two17K'] for r in rows),'source_admitted_two17K_annual':sum(r['two17K'] and r['joint_source_admission'] for r in rows),'formal_allK_annual':sum(r['allK'] for r in rows),'source_admitted_allK_annual':sum(r['allK'] and r['joint_source_admission'] for r in rows)},'rank_and_choice':{'duration_incidence_rank':3,'two_choice_sensitivity_rank':2,'equal17K_choice_constraint':'b−a=0, rank1: admits00and22','allK_choice_constraint':'a=2 and b=2, rank2: selects22 in this finite menu','a_b_are_source_distinct':True,'source_prescribed_selector_established':False,'new_independent_empirical_measurements':False},'interpretation':'Equal17K bridges alone do not select the allK rectangle. The00 route preserves equal17K in all five source cases with1060-year sides;22 alone yields2K sides/allK and has three admitted cases. The finite choice audit establishes conditional specificity, not a source rule prescribing22.','suggested_next_bounded_question':'Audit the minimal independent equation set and unresolved source-selector obligation after distinguishing the00and22 alternatives, retaining finite source boundaries and exact phase ancestry.','later_step_executed':False} a.out.mkdir(parents=True,exist_ok=True);(a.out/'C319_Independent_Math_Results.json').write_text(json.dumps(out,indent=2)+'\n');print(json.dumps({'status':'passed','annual_cases':20,'admitted':14,'admitted_two17K':8,'admitted_allK':3,'choice_rank':2})) if __name__=='__main__':main()