from fractions import Fraction def reduce_matrix(matrix): a=[[Fraction(x) for x in row] for row in matrix];pivots=[];r=0 for c in range(len(a[0])): pivot=next((j for j in range(r,len(a)) if a[j][c]),None) if pivot is None:continue a[r],a[pivot]=a[pivot],a[r];v=a[r][c];a[r]=[x/v for x in a[r]] for j in range(len(a)): if j!=r: v=a[j][c];a[j]=[x-v*y for x,y in zip(a[j],a[r])] pivots.append(c);r+=1 if r==len(a):break return a,pivots def run(load,check): names=['q cumulative signed source','n reflectedSPNoah','r reflectedSPFlood by8395','b reflectedMTFlood by fixed comparison','p reflectedSPFlood same Gear asb'] A=[[-1,1,0,0,0],[-1,0,1,0,0],[1,0,0,1,0],[0,0,0,-1,1]];rhs=[8993,8395,-2832,650] dependent=[0,1,-1,0,0];exact600=[0,1,0,0,-1] rank=lambda m:len(reduce_matrix(m)[1]) aug=[a+[b] for a,b in zip(A,rhs)];sol,pivots=reduce_matrix(aug+[exact600+[600]]) solution=[sol[j][-1] for j in range(5)] rows=[] for old in load('C305_DATA.json')['annual_domain']: i=old['i'];q=Fraction(old['q']);x=[q,q+8993,q+8395,-q-2832,-q-2182] check('four source equations index'+str(i),all(sum(a*v for a,v in zip(row,x))==b for row,b in zip(A,rhs))) rows.append({'i':i,'variables':{names[j]:str(v) for j,v in enumerate(x)},'n_minus_r':str(x[1]-x[2]),'r_minus_p':str(x[2]-x[4]),'n_minus_p':str(x[1]-x[4]),'all_source_bindings':old['all_bound']}) check('base rank4one free scalar',rank(A)==4 and rank(aug)==4) check('598 equation isdependent',rank(A+[dependent])==4 and rank(aug+[dependent+[598]])==4) check('600 equation increases rank5',rank(A+[exact600])==5 and rank(aug+[exact600+[600]])==5) check('unique600solution all halfinteger',solution==list(map(Fraction,['-10575/2','7411/2','6215/2','4911/2','6211/2']))) check('600 solution outsideinteger field',all(v.denominator==2 for v in solution)) check('width decomposition allrows',all(Fraction(r['n_minus_r'])==598 and Fraction(r['r_minus_p'])==2*r['i']-3 and Fraction(r['n_minus_p'])==595+2*r['i'] for r in rows)) phase=[] for e in [Fraction(-1,4),Fraction(1,4)]: phase.append({'source_offset':str(e),'n_and_r_offset':str(e),'b_and_p_offset':str(-e),'n_minus_r_offset':str(e-e),'n_minus_p_offset':str(2*e)}) check('source transport andMirror haveopposite phaseactions',all(Fraction(z['n_and_r_offset'])==-Fraction(z['b_and_p_offset']) for z in phase)) return {'variables':names,'base_coefficients':A,'base_rhs':rhs,'base_rank':rank(A),'free_scalar_dimensions':1,'dependent598_row':dependent,'rank_with598':rank(A+[dependent]),'rank_with600':rank(A+[exact600]),'formal600_solution':{names[j]:str(v) for j,v in enumerate(solution)},'annual_rows':rows,'phase_actions':phase,'model_status':'Conditional source-qualified affine model; no global action or placement proof.','context_rules_kept_distinct':{'cumulative_Cainan_BC':460,'regular_Cainan_BC':130,'Terah_BC':60,'MT_to_SP_baseline_BC':650,'full430_to_minimum215_BC':-215}}