natj/CellularAutomata.jl

Using modulus for boundary conditions?

Closed this issue · 5 comments

Not 100% sure, but I'm pretty sure that the conditional logic on the wrapping boundary conditions can by simplified by using a modulus.

Example

if q > w
 q = q - w
end

Replace with:

q = q % w

This would speed up the algorithm and also make easier to read.

natj commented

Yeah, you are correct!

Originally I wrote all of this as a proof of concept and I actually have quite a lot of unpublished stuff lying around but not enough free time. Feel free to comment the code and/or submit pull requests too, if you spot something else that could be improved. I am happy to receive improvements on this and also its nice to see other people interested in CAs too!

Awesome, I sent a pull request to master just now - mostly just added tests for high level behavior.

natj commented

Thanks for the pull request! It is now merged.

Awesome, great work on this project!

natj commented

Also one comment for the future generations: use of modulus in the boundary conditions does not work directly because array indexing starts from 1. So if we for example have width w=10 and index of q=10 we would have q % w = 0 whereas the if-statement gives 10 as it should be. One could of course avoid this by introducing an additional +1 to the indexing but in my opinion the if-statement is more clear.

Also in the far future if we ever want to introduce more complex boundary conditions it might be useful to have them written out explicitly in the code. I don't also think there is that much speed-up to be gained here as the bottlenecks of the algorithm are elsewhere.