ライフゲーム / Python / scipy.signal.convolve2d()版
描画の更新が遅いという難点はあるが現時点で固めておく。
import numpy as np import matplotlib.pyplot as plt from scipy.signal import convolve2d from matplotlib.animation import FuncAnimation class LifeGame: # 引数は左から(2次元排列, (表示領域の幅, 高さ), 更新ミリ秒) def __init__(self, mat, figsize=None, interval=1): self.mat = np.asarray(mat, dtype=np.uint8) self.Rules = np.array([[0,0,0,1,0,0,0,0,0], [0,0,1,1,0,0,0,0,0]], dtype=np.uint8) self.Weight = np.array([[1,1,1], [1,0,1], [1,1,1]], dtype=np.uint8) self.anim = FuncAnimation(plt.figure(figsize=figsize), self.__update, interval=interval) plt.show() def __update(self, i): plt.clf() plt.imshow(self.mat, cmap="binary") self.mat = self.Rules[self.mat, convolve2d(self.mat, self.Weight, mode="same", boundary="wrap")] if __name__ == "__main__": LifeGame(np.loadtxt("life_demo.csv", delimiter=",", dtype=np.uint8))