Last active
March 4, 2016 04:58
-
-
Save kosugitti/984c9fbdcb29f3943566 to your computer and use it in GitHub Desktop.
One dimensional Cell Automaton
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cellsize <- 160 | |
trace <- 50 | |
cell <- matrix(ncol=cellsize,nrow=trace) | |
# 初期化 | |
cell[1,] <- rbinom(cellsize,1,0.5) | |
### into 2bit | |
dec2bin <- function(num,digit=8){ | |
if(num <= 0 && digit <= 0){ | |
return(NULL) | |
} | |
return(append(Recall(num%/%2,digit-1),num%%2)) | |
} | |
### rule function | |
rule <- function(ruleid=30,code){ | |
next.stat <- dec2bin(ruleid,8) | |
ret <- next.stat[code] | |
return(ret) | |
} | |
for(turn in 1:(trace-1)){ | |
output <- "" | |
for(i in 1:ncol(cell)){ | |
cell.left <- i - 1; | |
cell.right<- i + 1; | |
if(cell.left<1){cell.left<-cellsize} | |
if(cell.right>cellsize){cell.right<-1} | |
code <- cell[turn,cell.left]*4+cell[turn,i]*2+cell[turn,cell.right]+1 | |
cell[turn+1,i] <- rule(code=code,ruleid=120) | |
output <- paste0(output,cell[turn+1,i]) | |
} | |
print(output) | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment