#!/usr/bin/env python import numpy as np cimport numpy as np DTYPE = np.int ctypedef np.int_t DTYPE_t ''' we'd like to generate a rule table to do look-ups into. for FHP, state at each site is given by 6 bits: 1 3 \ / 0-O-5 / \ 2 4 001100:100001 or 010010 010010:100001 or 001100 100001:010010 or 001100 011001:011001 100110:100110 ''' t = [0,1] rules = np.array([[[[[[[kk,jj,ii,i,j,k] for kk in t] for jj in t] for ii in t] for i in t] for j in t] for k in t],dtype=DTYPE) #rules[0,0,1,1,0,0] = np.array([1,0,0,0,0,1],dtype=DTYPE) #or array([0,1,0,0,1,0],dtype=DTYPE) #rules[0,1,0,0,1,0] = np.array([1,0,0,0,0,1],dtype=DTYPE) #or array([0,0,1,1,0,0],dtype=DTYPE) #rules[1,0,0,0,0,1] = np.array([0,0,1,1,0,0],dtype=DTYPE) #or array([0,1,0,0,1,0],dtype=DTYPE) rules[0,1,1,0,0,1] = np.array([0,1,1,0,0,1],dtype=DTYPE) rules[1,0,0,1,1,0] = np.array([1,0,0,1,1,0],dtype=DTYPE) def apply_rules(np.ndarray[DTYPE_t,ndim=3] world): ''' world is a NxNx4 boolean numpy array specifying state at each lattice point rules is a 2x2x2x2x4 boolean numpy array specifying the update to make for each state ''' cdef int i,j cdef int N = np.shape(world)[-2] cdef np.ndarray[DTYPE_t,ndim=1] state cdef int flip = 0 #assert(np.shape(world[-3])==N) #make sure world is square for i in range(N): for j in range(N): state = world[i,j] if (state == np.array([0,0,1,1,0,0])).all(): world[i,j] = np.array([1,0,0,0,0,1],dtype=DTYPE) if flip==0 else np.array([0,1,0,0,1,0],dtype=DTYPE) flip = 1-flip elif (state == np.array([0,1,0,0,1,0])).all(): world[i,j] = np.array([1,0,0,0,0,1],dtype=DTYPE) if flip==0 else np.array([0,0,1,1,0,0],dtype=DTYPE) flip = 1-flip elif (state == np.array([1,0,0,0,0,1])).all(): world[i,j] = np.array([0,0,1,1,0,0],dtype=DTYPE) if flip==0 else np.array([0,1,0,0,1,0],dtype=DTYPE) else: world[i,j] = rules[tuple(state)]