【发布时间】:2018-07-13 02:51:35
【问题描述】:
我想知道是否有一种方法可以实现康威的人生游戏,而无需借助 for 循环、if 语句和其他典型的编程控制结构。 对循环进行矢量化应该很容易,但是如何将邻域的检查转换为矩阵运算?
基本逻辑类似于this:
def neighbors(cell, distance=1):
"""Return the neighbors of cell."""
x, y = cell
r = xrange(0 - distance, 1 + distance)
return ((x + i, y + j) # new cell offset from center
for i in r for j in r # iterate over range in 2d
if not i == j == 0) # exclude the center cell
我希望模组不会认为这不是题外话,我是真正的古玩,我刚刚开始使用 CA。
干杯
【问题讨论】:
-
你将如何处理边缘?
-
在矢量化版本中以最简单的方式实现。
-
指向其他人的解决方案是否符合您的兴趣?作为他的书“从 Python 到 NumPy”的一部分,Nicolas Rougier 展示了一个矢量化的生活游戏 (direct link)
-
如果你在
[numpy] conway上进行搜索,你会得到很多帖子,包括我在stackoverflow.com/questions/47648106/… 中的回答 -
感谢您的回复!
标签: python numpy vectorization linear-algebra cellular-automata