rule_n

Elementary cellular automata in Python


License
GPL-3.0
Install
pip install rule_n==0.2.1

Documentation

rule_n

Travis CI Coveralls Code Climate PyPI Downloads License Issues

This is a Python implementation of elementary cellular automata.

Installation

Pip

pip install rule_n

Manual

Download rule_n.py and put it somewhere in your Python path.

Usage

import rule_n

rule_110 = rule_n.RuleN(110)
rule_30 = rule_n.RuleN(30)
rule_184 = rule_n.RuleN(184)  # Works with anything from 1 to 255
rule_110 = rule_n.RuleN()  # Default rule is 110, as that is the most common
from rule_n import rule_90   # Shorthand for rule_90 = rule_n.RuleN(90)
                             # Works with 110, 30, 90, 184
# You can also specify a list of rules
rule_110 = rule_n.RuleN([False, True, True, False, True, True, True, False])
# Or a string that summarizes the rule
rule_110 = rule_n.RuleN("01101110")
# See <https://en.wikipedia.org/wiki/Rule_110#Definition>
# You can also have a finite canvas
rule_110_finite_canvas = rule_n.RuleN(110, canvas_size=5)
# A canvas is finite if its size is over 0

data = rule_110.process([True, False, True])
len(data) == 5  # because a False is added to both sides
data == [True, True, True, True, False]

data_2 = rule_110.process([1, 0, 1])  # You can use any data type, as long
data == data_2                        # as the boolean values of these are
                                      # correct
                                      # Return values are always in boolean

# With a finite canvas, the output is always as big as the canvas
data = rule_110_finite_canvas.process([0, 0, 0, 0, 1])
data == [False, False, False, True, True]

data_3 = rule_110([True, False, True])  # Shorthand for
                                        # rule_110.process(state)
data == data_3

i = 0
for x in rule_110.iterate([1, 0, 1]):  # Repeatedly process a state
    print x
    i += 1
    if i == 10:
        break  # Please do this
# Note: Iteration on an infinte canvas seems to have some problems
# I recommend using a finite canvas
for x in rule_110_finite_canvas.iterate([0, 0, 0, 0, 1]):
    print x
    # This breaks automatically if the current state is equal to the
    # previous, which will probably happen at some point on a finite canvas