Game of Strife

171 days ago by gondii

def coords(i,j,m,n): coords = [] ioffs=[-1, 0, 1, 1, 1, 0, -1, -1] joffs=[-1, -1, -1, 0, 1, 1, 1, 0] for joff, ioff in zip(joffs, ioffs): if j + joff >= 0 and j + joff < m: if i + ioff >= 0 and i + ioff < m: coords.append((j+joff,i+ioff)) return coords 
       
def step(A): m = A.nrows() n = A.ncols() B = copy(A) for i in range(A.nrows()): for j in range(A.ncols()): cs = coords(i,j,m,n) for k,l in cs: if A[k,l] > 0: B[k,l] -= 0.1*A[k,l] B[i,j] += 0.1*A[k,l] return B 
       
A = zero_matrix(RR, 4,4) A[A.nrows()/2,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() 
       
1.00000000000000
1.00000000000000
"""Second attempt, trying to divert diffusion out of the edges""" 
       
'Second attempt, trying to divert diffusion out of the edges'
'Second attempt, trying to divert diffusion out of the edges'
def step2(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: if A[k,l] > 0: B[k,l] -= perCellFlow*A[k,l] B[i,j] += perCellFlow*A[k,l] return B 
       
A = zero_matrix(RR, 6,6) A[A.nrows()/2,A.ncols()/2] = 1 print sum(sum(A)) a = animate([matrix_plot(A)]) for i in range(100): A = step2(A) a = a * animate([matrix_plot(A)]) a.show() 
       
1.00000000000000
1.00000000000000
"""third attempt""" 
       
'third attempt'
'third attempt'
def pytha(k,l): if not 0 in (k,l): return sqrt(2) else: return 1 def step3(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: if A[k,l] > 0: B[k,l] -= perCellFlow * pytha(k,l) * A[k,l] B[i,j] += perCellFlow * pytha(k,l) * A[k,l] return B 
       
A = zero_matrix(RR, 6,6) A[A.nrows()/2,A.ncols()/2] = 1 print sum(sum(A)) a = animate([matrix_plot(A)]) for i in range(100): A = step3(A) a = a * animate([matrix_plot(A)]) a.show() 
       
1.00000000000000
1.00000000000000
"Fourth" 
       
'Fourth'
'Fourth'
def coords4(i,j,m,n): coords = [] ioffs=[ 0, 1, 0, -1] joffs=[-1, 0, 1, 0] for joff, ioff in zip(joffs, ioffs): if j + joff >= 0 and j + joff < m: if i + ioff >= 0 and i + ioff < m: coords.append((j+joff,i+ioff)) return coords def step4(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 = coords4(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, 6,6) A[A.nrows()/2,A.ncols()/2] = 1 l = [] print sum(sum(A)) a = animate([matrix_plot(A)]) for i in range(100): A = step4(A) a = a * animate([matrix_plot(A)]) a.show() 
       
1.00000000000000
1.00000000000000
"Fifth" 
       
'Fifth'
'Fifth'
def coords5(i,j,m,n): coords = [] ioffs=[ 0, 1, 0, -1] joffs=[-1, 0, 1, 0] for joff, ioff in zip(joffs, ioffs): if j + joff >= 0 and j + joff < m: if i + ioff >= 0 and i + ioff < m: coords.append((j+joff,i+ioff)) return coords def step5(A): m = A.nrows() n = A.ncols() B = copy(A) netFlow = 0.9 for i in range(A.nrows()): for j in range(A.ncols()): cs = coords5(i,j,m,n) perCellFlow = netFlow/len(cs) B[j,i] -= netFlow * A[j,i] for k,l in cs: B[k,l] += perCellFlow * A[j,i] return B A = zero_matrix(RR, 6,6) A[A.nrows()/2,A.ncols()/2] = 1 l = [] print sum(sum(A)) a = animate([matrix_plot(A)]) for i in range(10): A = step5(A) a = a * animate([matrix_plot(A)]) a.show() 
       
1.00000000000000
1.00000000000000
def coords6(j,i,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))S return coords def step6(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 = coords6(i,j,m,n) perCellFlow = netFlow/len(cs) B[j,i] -= netFlow * A[j,i] for k,l in cs: B[k,l] += perCellFlow * A[j,i] return B A = zero_matrix(RR, 6,6) A[A.nrows()/2,A.ncols()/2] = 1 print sum(sum(A)) a = animate([matrix_plot(A)]) for i in range(10): A = step5(A) a = a * animate([matrix_plot(A)]) a.show() 
       
Traceback (click to the left of this block for traceback)
...
SyntaxError: invalid syntax
Traceback (most recent call last):        return coords
  File "", line 1, in <module>
    
  File "/tmp/tmpCP6NYe/___code___.py", line 8
    coords.append(((j + joff) % m, (i+ioff)%n))S
                                               ^
SyntaxError: invalid syntax
def coords7(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 step7(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 = coords4(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, 6,6) A[A.nrows()/2,A.ncols()/2] = 1 l = [] print sum(sum(A)) a = animate([matrix_plot(A)]) for i in range(100): A = step7(A) a = a * animate([matrix_plot(A)]) a.show() 
       
1.00000000000000
1.00000000000000