Question
My code: import numpy as np def evolve( array ): nx,ny = array.shape # the shape of the input array neighbor_array = np.zeros((nx, ny), dtype=
My code:
import numpy as np def evolve( array ): nx,ny = array.shape # the shape of the input array neighbor_array = np.zeros((nx, ny), dtype= np.int) # should be zeros the same size as the input array evolved_array = np.zeros((nx, ny), dtype= np.int64) # should be zeros the same size as the input array, dtype of np.int64 for ix in range( 0,nx ): # loop over all x-values for iy in range( 0,ny ): # loop over all y-values lx,rx = ix-1,ix+1 # define the left and right neighbor indices uy,dy = iy-1,iy+1 # define the top and bottom neighbor indices rx %= nx # normalize the indices to fall in [0,nx) lx = (lx + nx) % nx # do this for the other three neighbor indices dy %= nx uy = (uy + ny) % ny # include all eight neighboring cells in an array and sum it up neighbors = [ array[ lx,iy ],array[ rx,iy ],array[ ix,uy ], array[ix, dy], array[lx, uy], array[lx, dy], array[rx, uy], array[rx, dy]] neighbor_array[ ix,iy ] = sum( neighbors ) # handle the case where the current cell is alive and has two neighbors if array[ ix,iy ] > 0 and neighbor_array[ ix,iy ] == 2: evolved_array[ ix,iy ] = 1 # handle the case where the current cell has three neighbors elif neighbor_array[ ix,iy ] == 3: evolved_array[ ix,iy ] = 1 # otherwise the cell is dead else: evolved_array[ ix,iy ] = 0 return evolved_array
1) I have to fill in ??? for this problem.
import numpy as np import matplotlib.pyplot as plt
pentadecathlon = np.zeros( ( 24,24 ) ) pentadecathlon[ 10:20,10 ] = 1 pentadecathlon[ 12, 9 ] = 1 pentadecathlon[ 12,10 ] = 0 pentadecathlon[ 12,11 ] = 1 pentadecathlon[ 17, 9 ] = 1 pentadecathlon[ 17,10 ] = 0 pentadecathlon[ 17,11 ] = 1
pd_list = ??? nt = 30 for t in ???: ??? # evolve the simulation and append it to `pd_list` plt.imshow( pd_list[ -1 ] ) plt.show()
2) I have to fill in ??? again for this problem.
import numpy as np import matplotlib.pyplot as plt
pentadecathlon = np.zeros( ( 24,24 ) ) pentadecathlon[ 10:20,10 ] = 1 pentadecathlon[ 12, 9 ] = 1 pentadecathlon[ 12,10 ] = 0 pentadecathlon[ 12,11 ] = 1 pentadecathlon[ 17, 9 ] = 1 pentadecathlon[ 17,10 ] = 0 pentadecathlon[ 17,11 ] = 1
pd_time = ??? evoln = pentadecathlon[ : ] nt = 30 for t in ???: ??? # evolve the system forward in `evoln` pd_time += evoln plt.imshow( pd_time ) plt.show()
Conway's Game of Life 10 points Our example starting configuration will be the R-pentomino, If we were to apply this set of rules to evolve the prior snapshot forward one step at a time, the following sequence would result: Your first challenge is to evolve an array one step forward by applying the rules to each cell. As a reminder, the rules are: 1. Any live cell with fewer than two live neighbours dies, as if subject to under-population. 2. Any live cell with two or three live neighbours lives to the next generation. 3. Any live cell with more than three live neighbours dies, as if subject to over-population. 4. Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction. Thus, we need only know the sum of the neighbors to determine which cells live, which die, and which are born. Compose a function evolve array which accepts an ndarray named array, and returns the next evolution of that array 1. We only need to know the sum of the neighbors to determine which cells live and which dieThe diagonals are neighbors as well.) Find the rules and apply boundary conditions. For instance, what happens to boundary cells? Are they considered as not having neighbors (dead boundary) or as wrapping around (periodic boundary)? For simplicity in this case, we'll go with the periodic boundary. (But we still have to handle the edge cases separately.) To implement the periodic boundary condition, we need to check if an index would be out of bounds; if so, wrap it around. This is a modulus operation! Thus for the left neighbor index x this looks like: 2. We then apply the rules to determine the next generation. 3. Return the evolved cellular array evolved_array Conway's Game of Life 10 points Our example starting configuration will be the R-pentomino, If we were to apply this set of rules to evolve the prior snapshot forward one step at a time, the following sequence would result: Your first challenge is to evolve an array one step forward by applying the rules to each cell. As a reminder, the rules are: 1. Any live cell with fewer than two live neighbours dies, as if subject to under-population. 2. Any live cell with two or three live neighbours lives to the next generation. 3. Any live cell with more than three live neighbours dies, as if subject to over-population. 4. Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction. Thus, we need only know the sum of the neighbors to determine which cells live, which die, and which are born. Compose a function evolve array which accepts an ndarray named array, and returns the next evolution of that array 1. We only need to know the sum of the neighbors to determine which cells live and which dieThe diagonals are neighbors as well.) Find the rules and apply boundary conditions. For instance, what happens to boundary cells? Are they considered as not having neighbors (dead boundary) or as wrapping around (periodic boundary)? For simplicity in this case, we'll go with the periodic boundary. (But we still have to handle the edge cases separately.) To implement the periodic boundary condition, we need to check if an index would be out of bounds; if so, wrap it around. This is a modulus operation! Thus for the left neighbor index x this looks like: 2. We then apply the rules to determine the next generation. 3. Return the evolved cellular array evolved_arrayStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started