def coords(i,j,m,n):
coords = []
ioffs=[ 0, 1, 0, -1]
joffs=[-1, 0, 1, 0]
for joff, ioff in zip(joffs, ioffs):
coords.append(((j+joff)%m,(i+ioff)%n))
return coords
def step(A):
m = A.nrows()
n = A.ncols()
B = copy(A)
netFlow = 0.1
for i in range(A.nrows()):
for j in range(A.ncols()):
cs = coords(i,j,m,n)
perCellFlow = netFlow/len(cs)
for k,l in cs:
B[k,l] -= perCellFlow * A[k,l]
B[i,j] += perCellFlow * A[k,l]
return B
##
A = zero_matrix(RR, 5,5)
A[int(A.nrows()/2), int(A.ncols()/2)] = 1
l = []
print sum(sum(A))
a = animate([matrix_plot(A)])
for i in range(100):
A = step(A)
a = a * animate([matrix_plot(A)])
a.show()