Difference between revisions of "Simulating Rule 110"

From Predictive Chemistry
Jump to: navigation, search
(Created page with "<source lang="python"> from numpy import * import pylab as plt def update(a,b,c): if b == 0 and c == 0: return 0 if a == 1 and b == 1 and c == 1: retu...")
 
 
Line 30: Line 30:
 
print( sum(A == 0) / (17.0*8.0) )
 
print( sum(A == 0) / (17.0*8.0) )
 
</source>
 
</source>
  +
  +
  +
To show you understand this code, try replacing the dimensions 17,8 by larger numbers and remove all "hard-coded" 17s and 8s to replace them with A.shape, etc. Also, try implementing a different rule or plot using lines instead of imshow.

Latest revision as of 12:52, 4 April 2018

<source lang="python"> from numpy import * import pylab as plt

def update(a,b,c):

   if b == 0 and c == 0:
       return 0
   if a == 1 and b == 1 and c == 1:
       return 0
   return 1

def update_row(A, r):

 for i in range(8):
   A[r+1,i] = update(A[r,i-1], A[r,i], A[r,(i+1)%8])

""" 1. create an empty "output"

   2. start with an initial condition
   3. for step in times:
     i.   append state to the output
     ii.  update the state
   

""" A = zeros((17,8)) A[0,7] = 1 for step in range(16):

 update_row(A, step)

plt.imshow(1-A, cmap='bone', interpolation='nearest', aspect='auto') plt.show() print( sum(A == 0) / (17.0*8.0) ) </source>


To show you understand this code, try replacing the dimensions 17,8 by larger numbers and remove all "hard-coded" 17s and 8s to replace them with A.shape, etc. Also, try implementing a different rule or plot using lines instead of imshow.