用 APL 写 Cellular Automaton (A NEW KIND OF SCIENCE)

⍝ 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 mobile automaton 就相当于两个 two-color ca。 不过我们可以先把 accumulator 给 abstract 出來。 acc ← {⍺←1 ⋄ ((⊢⍪(⍺⍺∘,¯1↑⊢))⍣⍺)⍵} ⍝ 这样之前的可以写成 (⊂pgm 1000 (2040 cell code acc) initi 500)⎕NPUT'a....

February 2, 2020 · LdBeth · GNU FDL