"""Exact Cantor/operator finite codes illustrating the sharp information law. Uniform binary IFS x/4 and (x+3)/4: h=ln2, chi=ln4, D=1/2. Directional gain 2^(-n/2), natural physical precision, gives 1/4 bit/step. The implemented finite code is within two bits of the exact conditional-entropy lower bound. Compensation to theta=5/4 permits an empty tag in this example. All source points, cell boundaries, and tested noise values use Fraction. """ from collections import Counter from fractions import Fraction as F from pathlib import Path import json def numerator(word,length): out=0 for i in range(length-1,-1,-1): out=4*out+3*((word>>i)&1) return out def valid_cantor_numerator(value,length): if value<0 or value>=4**length:return False for _ in range(length): digit=value%4 if digit not in (0,3):return False value//=4 return True reports=[] for n in [4,8,12]: d=3*n//4;k=n-d gain=F(1,2**(n//2)) delta=F(1,4**d) physical_radius=F(1,4**n) assert physical_radius/gain==delta states=[numerator(word,n) for word in range(2**n)] bins=Counter(value//4**k for value in states) assert len(bins)==2**d and set(bins.values())=={2**k} checks=0 for word,value in enumerate(states): point=F(value,4**n) q=value//4**k tag=(q%4,word%(2**k)) assert len(format(tag[0],'02b')+format(tag[1],f'0{k}b'))==2+k for multiplier in [F(-1),F(-1,2),F(0),F(1,2),F(1)]: scalar_observation=point+multiplier*delta # Matched vector-line noise would have norm gain*abs(error). assert gain*abs(scalar_observation-point)<=physical_radius z=scalar_observation//delta candidates=[j for j in [z-1,z,z+1] if j%4==tag[0]] assert len(candidates)==1 and candidates[0]==q recovered=F(candidates[0]*4**k+numerator(tag[1],k),4**n) assert recovered==point checks+=1 # theta=5/4 compensates the attenuation: pulled-back radius 4^(-n). compensated_physical=F(1,2**(5*n//2)) assert compensated_physical/gain==F(1,4**n) for multiplier in [F(-1),F(-1,2),F(0),F(1,2),F(1)]: scaled_observation=F(value)+multiplier z=scaled_observation//1 candidates=[j for j in [z-1,z,z+1] if valid_cantor_numerator(j,n) and abs(F(j)-scaled_observation)<=1] assert candidates==[value] reports.append({'depth':n,'states_checked':2**n,'robust_boundary_checks':checks, 'observation_entropy_bits_exact':d, 'conditional_entropy_bits_exact':k, 'code_bits':k+2,'excess_bits_over_lower_bound':2, 'natural_metadata_rate_exact':'1/4', 'compensated_metadata_bits':0, 'arithmetic':'Fraction; no tolerance'}) Path(__file__).with_suffix('.json').write_text(json.dumps(reports,indent=2)+'\n') print('PASS: exact conditional-entropy counts, all source states and five closed-noise boundaries.') print('PASS: operator gain, quarter-bit rate, two-bit redundancy, and zero-tag compensation.')