code wiki / (root) / nx_chaos.nx

nx_chaos.nx source

↩ module page · 87 lines · 4347 B

1// nx_chaos.nx -- Simulates chaotic behavior using integer arithmetic to demonstrate deterministic chaos and sensitive dependence on initial conditions. 2const O_MAGIC_500000: i64 = 500000 3const O_MAGIC_500001: i64 = 500001 4const O_MAGIC_3900: i64 = 3900 5const O_MAGIC_4096: i64 = 4096 6// nx_chaos.nx -- BROWSER-RUNNABLE deterministic-chaos sim (base-relative, integer/no-float, no JS logic). SAME code 7// runs native (base = mmap -> PNG) and in the browser. The logistic map x' = r x (1-x) -- the textbook route to 8// chaos. Two trajectories start ONE unit apart (x0 and x0+1) at r=3.9: they track, then DIVERGE exponentially 9// (sensitive dependence on initial conditions). The deep point: in integer/no-float arithmetic the sequence is 10// BIT-EXACT REPRODUCIBLE -- a chaotic system, run twice, gives identical results -- exactly where IEEE-754 float 11// sims diverge machine-to-machine. So this is the live face of the census's Reproducibility EXCEEDS. Validated by 12// nx_chaos_gate (fixed point / period-2 / chaos / sensitive-dependence / determinism). nx_wasm auto-exports all. 13// license_tier: ORIGINAL 14const W: i64 = 256 15const H: i64 = 256 16const O_FB: i64 = 0 17const O_XA: i64 = 524288 18const O_XB: i64 = 524296 19const O_R: i64 = 524304 // r * 1000 20const O_STEP: i64 = 524312 21const O_HA: i64 = 524320 // trajectory A history 22const O_HB: i64 = 524320 + 240*8 // trajectory B history 23const HISTN: i64 = 240 24const SC: i64 = 1000000 // x in [0, SC] == [0,1] 25const STEPS_PER_TICK: i64 = 1 26 27func rgb(r: i64, g: i64, b: i64) -> i64 { return (r & 255) | ((g & 255) << 8) | ((b & 255) << 16) } 28// logistic map x' = r x (1-x), integer: tmp = x(SC-x)/SC ; x' = tmp * (r*1000) / 1000 29func logistic(x: i64, R: i64) -> i64 { let tmp: i64 = x*(SC-x)/SC; return tmp*R/1000 } 30 31func putpx(base: i64, px: i64, py: i64, c: i64) -> i64 { 32 if px<0 { return 0 } if px>=W { return 0 } if py<0 { return 0 } if py>=H { return 0 } 33 let fb: *i64 = (base + O_FB) as *i64; fb[py*W+px]=c; return 0 34} 35func disc(base: i64, px: i64, py: i64, r: i64, c: i64) -> i64 { 36 var dy: i64=0-r 37 while dy<=r { var dx: i64=0-r 38 while dx<=r { if dx*dx+dy*dy<=r*r { putpx(base,px+dx,py+dy,c) } dx=dx+1 } 39 dy=dy+1 } 40 return 0 41} 42func clear_fb(base: i64, c: i64) -> i64 { let fb: *i64=(base+O_FB) as *i64; var i: i64=0; while i<W*H { fb[i]=c; i=i+1 } return 0 } 43 44func init_impl(base: i64) -> i64 { 45 let XA: *i64=(base+O_XA) as *i64; let XB: *i64=(base+O_XB) as *i64; let R: *i64=(base+O_R) as *i64 46 XA[0]=O_MAGIC_500000; XB[0]=O_MAGIC_500001; R[0]=O_MAGIC_3900 // x0 vs x0+1 (one unit apart), r=3.9 (chaotic) 47 let stp: *i64=(base+O_STEP) as *i64; stp[0]=0 48 let HA: *i64=(base+O_HA) as *i64; let HB: *i64=(base+O_HB) as *i64 49 var i: i64=0; while i<HISTN { HA[i]=0-1; HB[i]=0-1; i=i+1 } 50 return 0 51} 52func step_one_impl(base: i64) -> i64 { 53 let XA: *i64=(base+O_XA) as *i64; let XB: *i64=(base+O_XB) as *i64; let R: *i64=(base+O_R) as *i64 54 XA[0]=logistic(XA[0], R[0]); XB[0]=logistic(XB[0], R[0]) 55 return 0 56} 57func tick_impl(base: i64, cmd: i64) -> i64 { 58 if cmd==3 { init_impl(base); return 0 } 59 let stp: *i64=(base+O_STEP) as *i64 60 if stp[0]>=HISTN { init_impl(base); return 0 } 61 var s: i64=0; while s<STEPS_PER_TICK { step_one_impl(base); s=s+1 } 62 let XA: *i64=(base+O_XA) as *i64; let XB: *i64=(base+O_XB) as *i64 63 let HA: *i64=(base+O_HA) as *i64; let HB: *i64=(base+O_HB) as *i64 64 HA[stp[0]]=XA[0]; HB[stp[0]]=XB[0]; stp[0]=stp[0]+1 65 return 0 66} 67func render_impl(base: i64) -> i64 { 68 let HA: *i64=(base+O_HA) as *i64; let HB: *i64=(base+O_HB) as *i64 69 clear_fb(base, rgb(10,10,18)) 70 var x: i64=0 71 while x<HISTN { 72 let a: i64=HA[x]; let b: i64=HB[x] 73 if a>=0 { let ya: i64=(H-8) - a*(H-16)/SC; disc(base, x+8, ya, 1, rgb(90,170,255)) } // trajectory A blue 74 if b>=0 { let yb: i64=(H-8) - b*(H-16)/SC; disc(base, x+8, yb, 1, rgb(255,110,90)) } // trajectory B red 75 x=x+1 76 } 77 return 0 78} 79 80// ---- wasm interface ---- 81func ww() -> i64 { return W } 82func hh() -> i64 { return H } 83func fb_off() -> i64 { return O_FB } 84func init() -> i64 { return init_impl(0) } 85func tick(cmd: i64) -> i64 { return tick_impl(0, cmd) } 86func render() -> i64 { return render_impl(0) } 87func mem_bytes() -> i64 { return O_HB + HISTN*8 + O_MAGIC_4096 }