⍝ CA 的原理很簡單,用 APL 的 primitive ⌺ (Stencil) 就可以表示
cell←{⍵⍪(⍺⍺⌺3),¯1↑⍵}
⍝ two-color ca 的映射
rule ← {⎕IO←0 ⋄ (2⊥⍵)⌷⌽(8⍴2)⊤⍺⍺}
⍝ three-color ca
code ← {⎕IO←0 ⋄ (+/⍵)⌷⌽(7⍴3)⊤⍺⍺}
⍝from APL matrics to Netpbm format
pgm ← {'P2'(⍕⌽⍴⍵)(⍕M),⍕¨↓⍵-⍨M←⌈/,⍵}
題图(code2040 size:1001×1001)生成方式
(⊂pgm ((2040 code)cell⍣1000) initi 500)⎕NPUT'a.pgm'
⍝ from shell $ pnmtopng a.pgm > z.png
不过我们可以先把 accumulator 给 abstract 出來。
acc ← {⍺←1 ⋄ ((⊢⍪(⍺⍺∘,¯1↑⊢))⍣⍺)⍵}
⍝ 这样之前的可以写成 (⊂pgm 1000 (2040 cell code acc) initi 500)⎕NPUT'a.pgm'
然后是 mobile automaton 的原理
mobile ← {((⍺⍺ ⍵⍵ ⍵⌷⍨⊂P)@P)⍵⊣P←¯1 0 1+⍸1<⍵
和替換规则
ma ← {((⌽⍣A)0 0 2)+(B@2)P⊣A B←(⍵⊤⍨2⍴255)rule¨⊂P←⍵-0 2 0}
于是可以用
100 (14570 mobile ma acc) 2 initi 100
画出