Skip to main content
Code Review

Return to Answer

replaced http://codereview.stackexchange.com/ with https://codereview.stackexchange.com/
Source Link

I'd consider using NumPy arrays to handle your matrices instead of native Python lists. It's tailored to use cases like this, and your to_next_generation() function will run considerably faster for larger matrices. With NumPy, you don't need to loop over rows or columns yourself. NumPy built-in functions will take care of it for you. It will be doing similar loops, but in C, so it will be faster than in Python.

Your code block:

# Creates the next generation of the matrix.
def to_next_generation(matrix, height, width):
 temp = matrix_copy(matrix, height, width)
 for i in range(1, height-1):
 for j in range(1, width-1):
 count = matrix[i-1][j] + matrix[i-1][j-1] + \
 matrix[i][j-1] + matrix[i+1][j-1] + matrix[i+1][j] + \
 matrix[i+1][j+1] + matrix[i][j+1] + matrix[i-1][j+1]
 if count == 2:
 temp[i][j] = matrix[i][j]
 elif count == 3:
 temp[i][j] = 1
 elif count < 2 or count > 3:
 temp[i][j] = 0
 matrix = matrix_copy(temp, height, width)
 return matrix

could become something like this in NumPy (code borrowed and modified from this this Code Review answer.)

import numpy as np
from scipy.signal import convolve2d
def iterate_game_of_life(matrix, neighbors_kernel=None):
 """Performs one iteration of Conway's game of life on a 2d numpy array of bools"""
 if neighbors_kernel is None:
 neighbors_kernel = np.ones(shape=(3, 3), dtype='int')
 neighbors_kernel[1, 1] = 0
 neighbors_count = convolve2d(matrix, neighbors_kernel, mode='same')
 has_three, has_two = neighbors_count == 3, neighbors_count == 2
 return np.logical_or(has_three, np.logical_and(matrix, has_two))

In NumPy, you can tell the interpreter that your game board will always consist of only 1s and 0s by setting the dtype to be bool. In contrast, native Python always has to be on guard for an addition to the list of arbitrary type, and so will take much larger amounts of memory than Python lists.

I'd consider using NumPy arrays to handle your matrices instead of native Python lists. It's tailored to use cases like this, and your to_next_generation() function will run considerably faster for larger matrices. With NumPy, you don't need to loop over rows or columns yourself. NumPy built-in functions will take care of it for you. It will be doing similar loops, but in C, so it will be faster than in Python.

Your code block:

# Creates the next generation of the matrix.
def to_next_generation(matrix, height, width):
 temp = matrix_copy(matrix, height, width)
 for i in range(1, height-1):
 for j in range(1, width-1):
 count = matrix[i-1][j] + matrix[i-1][j-1] + \
 matrix[i][j-1] + matrix[i+1][j-1] + matrix[i+1][j] + \
 matrix[i+1][j+1] + matrix[i][j+1] + matrix[i-1][j+1]
 if count == 2:
 temp[i][j] = matrix[i][j]
 elif count == 3:
 temp[i][j] = 1
 elif count < 2 or count > 3:
 temp[i][j] = 0
 matrix = matrix_copy(temp, height, width)
 return matrix

could become something like this in NumPy (code borrowed and modified from this Code Review answer.)

import numpy as np
from scipy.signal import convolve2d
def iterate_game_of_life(matrix, neighbors_kernel=None):
 """Performs one iteration of Conway's game of life on a 2d numpy array of bools"""
 if neighbors_kernel is None:
 neighbors_kernel = np.ones(shape=(3, 3), dtype='int')
 neighbors_kernel[1, 1] = 0
 neighbors_count = convolve2d(matrix, neighbors_kernel, mode='same')
 has_three, has_two = neighbors_count == 3, neighbors_count == 2
 return np.logical_or(has_three, np.logical_and(matrix, has_two))

In NumPy, you can tell the interpreter that your game board will always consist of only 1s and 0s by setting the dtype to be bool. In contrast, native Python always has to be on guard for an addition to the list of arbitrary type, and so will take much larger amounts of memory than Python lists.

I'd consider using NumPy arrays to handle your matrices instead of native Python lists. It's tailored to use cases like this, and your to_next_generation() function will run considerably faster for larger matrices. With NumPy, you don't need to loop over rows or columns yourself. NumPy built-in functions will take care of it for you. It will be doing similar loops, but in C, so it will be faster than in Python.

Your code block:

# Creates the next generation of the matrix.
def to_next_generation(matrix, height, width):
 temp = matrix_copy(matrix, height, width)
 for i in range(1, height-1):
 for j in range(1, width-1):
 count = matrix[i-1][j] + matrix[i-1][j-1] + \
 matrix[i][j-1] + matrix[i+1][j-1] + matrix[i+1][j] + \
 matrix[i+1][j+1] + matrix[i][j+1] + matrix[i-1][j+1]
 if count == 2:
 temp[i][j] = matrix[i][j]
 elif count == 3:
 temp[i][j] = 1
 elif count < 2 or count > 3:
 temp[i][j] = 0
 matrix = matrix_copy(temp, height, width)
 return matrix

could become something like this in NumPy (code borrowed and modified from this Code Review answer.)

import numpy as np
from scipy.signal import convolve2d
def iterate_game_of_life(matrix, neighbors_kernel=None):
 """Performs one iteration of Conway's game of life on a 2d numpy array of bools"""
 if neighbors_kernel is None:
 neighbors_kernel = np.ones(shape=(3, 3), dtype='int')
 neighbors_kernel[1, 1] = 0
 neighbors_count = convolve2d(matrix, neighbors_kernel, mode='same')
 has_three, has_two = neighbors_count == 3, neighbors_count == 2
 return np.logical_or(has_three, np.logical_and(matrix, has_two))

In NumPy, you can tell the interpreter that your game board will always consist of only 1s and 0s by setting the dtype to be bool. In contrast, native Python always has to be on guard for an addition to the list of arbitrary type, and so will take much larger amounts of memory than Python lists.

Source Link
Curt F.
  • 1.7k
  • 11
  • 22

I'd consider using NumPy arrays to handle your matrices instead of native Python lists. It's tailored to use cases like this, and your to_next_generation() function will run considerably faster for larger matrices. With NumPy, you don't need to loop over rows or columns yourself. NumPy built-in functions will take care of it for you. It will be doing similar loops, but in C, so it will be faster than in Python.

Your code block:

# Creates the next generation of the matrix.
def to_next_generation(matrix, height, width):
 temp = matrix_copy(matrix, height, width)
 for i in range(1, height-1):
 for j in range(1, width-1):
 count = matrix[i-1][j] + matrix[i-1][j-1] + \
 matrix[i][j-1] + matrix[i+1][j-1] + matrix[i+1][j] + \
 matrix[i+1][j+1] + matrix[i][j+1] + matrix[i-1][j+1]
 if count == 2:
 temp[i][j] = matrix[i][j]
 elif count == 3:
 temp[i][j] = 1
 elif count < 2 or count > 3:
 temp[i][j] = 0
 matrix = matrix_copy(temp, height, width)
 return matrix

could become something like this in NumPy (code borrowed and modified from this Code Review answer.)

import numpy as np
from scipy.signal import convolve2d
def iterate_game_of_life(matrix, neighbors_kernel=None):
 """Performs one iteration of Conway's game of life on a 2d numpy array of bools"""
 if neighbors_kernel is None:
 neighbors_kernel = np.ones(shape=(3, 3), dtype='int')
 neighbors_kernel[1, 1] = 0
 neighbors_count = convolve2d(matrix, neighbors_kernel, mode='same')
 has_three, has_two = neighbors_count == 3, neighbors_count == 2
 return np.logical_or(has_three, np.logical_and(matrix, has_two))

In NumPy, you can tell the interpreter that your game board will always consist of only 1s and 0s by setting the dtype to be bool. In contrast, native Python always has to be on guard for an addition to the list of arbitrary type, and so will take much larger amounts of memory than Python lists.

lang-py

AltStyle によって変換されたページ (->オリジナル) /