📚 The CoCalc Library - books, templates and other resources
License: OTHER
""" Code example from Complexity and Computation, a book about1exploring complexity science with Python. Available free from23http://greenteapress.com/complexity45Copyright 2016 Allen Downey6MIT License: http://opensource.org/licenses/MIT7"""8from __future__ import print_function, division910import sys1112import numpy as np13import matplotlib.pyplot as plt14from matplotlib import animation1516"""17For animation to work in the notebook, you might have to install18ffmpeg. On Ubuntu and Linux Mint, the following should work.1920sudo add-apt-repository ppa:mc3man/trusty-media21sudo apt-get update22sudo apt-get install ffmpeg23"""2425from Cell2D import Cell2D, Cell2DViewer26from scipy.signal import correlate2d272829class Life(Cell2D):30"""Implementation of Conway's Game of Life."""31kernel = np.array([[1, 1, 1],32[1,10, 1],33[1, 1, 1]])3435table = np.zeros(20, dtype=np.uint8)36table[[3, 12, 13]] = 13738def step(self):39"""Executes one time step."""40c = correlate2d(self.array, self.kernel, mode='same')41self.array = self.table[c]424344class LifeViewer(Cell2DViewer):45"""Viewer for Game of Life."""464748def main(script, *args):49"""Constructs a puffer train.5051Uses the entities in this file:52http://www.radicaleye.com/lifepage/patterns/puftrain.lif53"""54lwss = [55'0001',56'00001',57'10001',58'01111'59]6061bhep = [62'1',63'011',64'001',65'001',66'01'67]6869n = 40070m = 60071life = Life(n, m)7273col = 12074life.add_cells(n//2+12, col, *lwss)75life.add_cells(n//2+26, col, *lwss)76life.add_cells(n//2+19, col, *bhep)77viewer = LifeViewer(life)78anim = viewer.animate(frames=100, interval=1)79plt.subplots_adjust(left=0.01, right=0.99, bottom=0.01, top=0.99)80plt.show()818283if __name__ == '__main__':84main(*sys.argv)8586878889