home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 9 / 09.iso / e / e007 / 3.img / FILES / EXAMPLES.PAK / CELLULAR.M next >
Encoding:
Text File  |  1992-07-29  |  1.7 KB  |  68 lines

  1.  
  2.  
  3. (** Operations on Cellular Automata 
  4. Copyright 1990, Wolfram Research, Inc.**)
  5. UpdateCA::usage =
  6.     "UpdateCA[list, rule] applies one time step of a CA rule to list.
  7.     The CA rule is given as a list of replacements for neighborhoods,
  8.     such as {{1,0,0}->1, {1,1,1}->1, {1,1,_}->0, {1,0,1}->1, 
  9.     {0,a_,b_}:>Mod[a+b,2]}."
  10.  
  11. EvolveCA::usage =
  12.     "EvolveCA[init, rule, t] evolves a CA for t time steps, giving a
  13.     list of the configurations produced."
  14.  
  15. ShowCA::usage =
  16.     "ShowCA[list] displays a cellular automaton evolution history
  17.     graphically."
  18.  
  19. NumberedRule::usage =
  20.     "NumberedRule[n] gives a rule table for a k=2, r=1 rule specified
  21.     by the integer n."
  22.  
  23. CenterSpot::usage =
  24.     "CenterSpot[list, n] generates a list of n zeroes, with the
  25.     elements of list in the middle."
  26.  
  27.  
  28. UpdateCA[list_List, rule_] :=
  29.     Module[ { t }, 
  30.         t = Join[ {Last[list]}, list, {First[list]} ] ;
  31.         t = Partition[t, 3, 1] ;
  32.         Map[Replace[#, rule]&, t]
  33.     ]
  34.  
  35. Pad[list_List, n_Integer] :=
  36.     If[Length[list] < n, Join[Table[0, {n-Length[list]}], list], list]
  37.         
  38.     
  39. Neighborhoods[k_Integer:2, rr_Integer:3] :=
  40.     Module[ { t },
  41.         t = Range[k^rr - 1, 0, -1] ;
  42.         Map[ Pad[ IntegerDigits[#, k], rr ] &, t ]
  43.     ]
  44.  
  45. EvolveCA[init_List, rule_, t_Integer] :=
  46.     Module[{ drule = Dispatch[rule] }, 
  47.         NestList[UpdateCA[#, drule]&, init, t]
  48.     ]
  49.  
  50. ShowCA[list_List] :=
  51.     Show[Graphics[Raster[
  52.         Reverse[  1 - list ] 
  53.     ]],AspectRatio -> 1]
  54.  
  55. NumberedRule[n_Integer, k_Integer:2, rr_Integer:3] :=
  56.     Thread[ Neighborhoods[k, rr] -> Pad[IntegerDigits[n, k], k^rr] ]
  57.  
  58. CenterSpot[list_List, n_Integer] :=
  59.     Module[ { i1 },
  60.         i1 = Floor[ (n - Length[list])/ 2 ] ;
  61.         Join[ Table[0, {i1}], 
  62.             list,
  63.               Table[0, {n-i1-Length[list]}]
  64.         ]
  65.     ]
  66.  
  67.  
  68.